xref: /titanic_41/usr/src/lib/libdtrace/common/dt_parser.h (revision 8eea8e29cc4374d1ee24c25a07f45af132db3499)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #ifndef	_DT_PARSER_H
28 #define	_DT_PARSER_H
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 #include <sys/types.h>
33 #include <sys/dtrace.h>
34 
35 #include <libctf.h>
36 #include <stdarg.h>
37 #include <stdio.h>
38 
39 #ifdef	__cplusplus
40 extern "C" {
41 #endif
42 
43 #include <dt_errtags.h>
44 #include <dt_ident.h>
45 #include <dt_decl.h>
46 #include <dt_xlator.h>
47 #include <dt_list.h>
48 
49 typedef struct dt_node {
50 	ctf_file_t *dn_ctfp;	/* CTF type container for node's type */
51 	ctf_id_t dn_type;	/* CTF type reference for node's type */
52 	uchar_t dn_kind;	/* node kind (DT_NODE_*, defined below) */
53 	uchar_t dn_flags;	/* node flags (DT_NF_*, defined below) */
54 	ushort_t dn_op;		/* operator (DT_TOK_*, defined by lex) */
55 	int dn_line;		/* line number for error messages */
56 	int dn_reg;		/* register allocated by cg */
57 	dtrace_attribute_t dn_attr; /* node stability attributes */
58 
59 	/*
60 	 * D compiler nodes, as is the usual style, contain a union of the
61 	 * different sub-elements required by the various kinds of nodes.
62 	 * These sub-elements are accessed using the macros defined below.
63 	 */
64 	union {
65 		struct {
66 			uintmax_t _value;	/* integer value */
67 			char *_string;		/* string value */
68 		} _const;
69 
70 		struct {
71 			dt_ident_t *_ident;	/* identifier reference */
72 			struct dt_node *_links[3]; /* child node pointers */
73 		} _nodes;
74 
75 		struct {
76 			struct dt_node *_descs;	/* list of descriptions */
77 			struct dt_node *_pred;	/* predicate expression */
78 			struct dt_node *_acts;	/* action statement list */
79 			dt_idhash_t *_locals;	/* local variable hash */
80 			dtrace_attribute_t _attr; /* context attributes */
81 		} _clause;
82 
83 		struct {
84 			char *_spec;		/* specifier string (if any) */
85 			dtrace_probedesc_t *_desc; /* final probe description */
86 		} _pdesc;
87 
88 		struct {
89 			char *_name;		/* string name of member */
90 			struct dt_node *_expr;	/* expression node pointer */
91 		} _member;
92 
93 		struct {
94 			dt_xlator_t *_xlator;	/* translator reference */
95 			struct dt_node *_membs;	/* list of member nodes */
96 		} _xlator;
97 
98 		struct {
99 			char *_name;		/* string name of provider */
100 			struct dt_provider *_pvp; /* provider references */
101 			struct dt_node *_probes;  /* list of probe nodes */
102 			int _redecl;		/* provider redeclared */
103 		} _provider;
104 	} dn_u;
105 
106 	struct dt_node *dn_list; /* parse tree list link */
107 	struct dt_node *dn_link; /* allocation list link */
108 } dt_node_t;
109 
110 #define	dn_value	dn_u._const._value	/* DT_NODE_INT */
111 #define	dn_string	dn_u._const._string	/* STRING, IDENT, TYPE */
112 #define	dn_ident	dn_u._nodes._ident	/* VAR,SYM,FUN,AGG,INL,PROBE */
113 #define	dn_args		dn_u._nodes._links[0]	/* DT_NODE_VAR, FUNC */
114 #define	dn_child	dn_u._nodes._links[0]	/* DT_NODE_OP1 */
115 #define	dn_left		dn_u._nodes._links[0]	/* DT_NODE_OP2, OP3 */
116 #define	dn_right	dn_u._nodes._links[1]	/* DT_NODE_OP2, OP3 */
117 #define	dn_expr		dn_u._nodes._links[2]	/* DT_NODE_OP3, DEXPR */
118 #define	dn_aggfun	dn_u._nodes._links[0]	/* DT_NODE_AGG */
119 #define	dn_aggtup	dn_u._nodes._links[1]	/* DT_NODE_AGG */
120 #define	dn_pdescs	dn_u._clause._descs	/* DT_NODE_CLAUSE */
121 #define	dn_pred		dn_u._clause._pred	/* DT_NODE_CLAUSE */
122 #define	dn_acts		dn_u._clause._acts	/* DT_NODE_CLAUSE */
123 #define	dn_locals	dn_u._clause._locals	/* DT_NODE_CLAUSE */
124 #define	dn_ctxattr	dn_u._clause._attr	/* DT_NODE_CLAUSE */
125 #define	dn_spec		dn_u._pdesc._spec	/* DT_NODE_PDESC */
126 #define	dn_desc		dn_u._pdesc._desc	/* DT_NODE_PDESC */
127 #define	dn_membname	dn_u._member._name	/* DT_NODE_MEMBER */
128 #define	dn_membexpr	dn_u._member._expr	/* DT_NODE_MEMBER */
129 #define	dn_xlator	dn_u._xlator._xlator	/* DT_NODE_XLATOR */
130 #define	dn_members	dn_u._xlator._membs	/* DT_NODE_XLATOR */
131 #define	dn_provname	dn_u._provider._name	/* DT_NODE_PROVIDER */
132 #define	dn_provider	dn_u._provider._pvp	/* DT_NODE_PROVIDER */
133 #define	dn_provred	dn_u._provider._redecl	/* DT_NODE_PROVIDER */
134 #define	dn_probes	dn_u._provider._probes	/* DT_NODE_PROVIDER */
135 
136 #define	DT_NODE_FREE	0	/* unused node (waiting to be freed) */
137 #define	DT_NODE_INT	1	/* integer value */
138 #define	DT_NODE_STRING	2	/* string value */
139 #define	DT_NODE_IDENT	3	/* identifier */
140 #define	DT_NODE_VAR	4	/* variable reference */
141 #define	DT_NODE_SYM	5	/* symbol reference */
142 #define	DT_NODE_TYPE	6	/* type reference or formal parameter */
143 #define	DT_NODE_FUNC	7	/* function call */
144 #define	DT_NODE_OP1	8	/* unary operator */
145 #define	DT_NODE_OP2	9	/* binary operator */
146 #define	DT_NODE_OP3	10	/* ternary operator */
147 #define	DT_NODE_DEXPR	11	/* D expression action */
148 #define	DT_NODE_DFUNC	12	/* D function action */
149 #define	DT_NODE_AGG	13	/* aggregation */
150 #define	DT_NODE_PDESC	14	/* probe description */
151 #define	DT_NODE_CLAUSE	15	/* clause definition */
152 #define	DT_NODE_INLINE	16	/* inline definition */
153 #define	DT_NODE_MEMBER	17	/* member definition */
154 #define	DT_NODE_XLATOR	18	/* translator definition */
155 #define	DT_NODE_PROBE	19	/* probe definition */
156 #define	DT_NODE_PROVIDER 20	/* provider definition */
157 #define	DT_NODE_PROG	21	/* program translation unit */
158 
159 #define	DT_NF_SIGNED	0x01	/* data is a signed quantity (else unsigned) */
160 #define	DT_NF_COOKED	0x02	/* data is a known type (else still cooking) */
161 #define	DT_NF_REF	0x04	/* pass by reference (array, struct, union) */
162 #define	DT_NF_LVALUE	0x08	/* node is an l-value according to ANSI-C */
163 #define	DT_NF_WRITABLE	0x10	/* node is writable (can be modified) */
164 #define	DT_NF_BITFIELD	0x20	/* node is an integer bitfield */
165 #define	DT_NF_USERLAND	0x40	/* data is a userland address */
166 
167 #define	DT_TYPE_NAMELEN	128	/* reasonable size for ctf_type_name() */
168 
169 extern int dt_node_is_integer(const dt_node_t *);
170 extern int dt_node_is_float(const dt_node_t *);
171 extern int dt_node_is_scalar(const dt_node_t *);
172 extern int dt_node_is_arith(const dt_node_t *);
173 extern int dt_node_is_vfptr(const dt_node_t *);
174 extern int dt_node_is_dynamic(const dt_node_t *);
175 extern int dt_node_is_string(const dt_node_t *);
176 extern int dt_node_is_strcompat(const dt_node_t *);
177 extern int dt_node_is_pointer(const dt_node_t *);
178 extern int dt_node_is_void(const dt_node_t *);
179 extern int dt_node_is_ptrcompat(const dt_node_t *, const dt_node_t *,
180 	ctf_file_t **, ctf_id_t *);
181 extern int dt_node_is_argcompat(const dt_node_t *, const dt_node_t *);
182 extern int dt_node_is_posconst(const dt_node_t *);
183 extern int dt_node_is_actfunc(const dt_node_t *);
184 
185 extern dt_node_t *dt_node_int(uintmax_t);
186 extern dt_node_t *dt_node_string(char *);
187 extern dt_node_t *dt_node_ident(char *);
188 extern dt_node_t *dt_node_type(dt_decl_t *);
189 extern dt_node_t *dt_node_vatype(void);
190 extern dt_node_t *dt_node_decl(void);
191 extern dt_node_t *dt_node_func(dt_node_t *, dt_node_t *);
192 extern dt_node_t *dt_node_offsetof(dt_decl_t *, char *);
193 extern dt_node_t *dt_node_op1(int, dt_node_t *);
194 extern dt_node_t *dt_node_op2(int, dt_node_t *, dt_node_t *);
195 extern dt_node_t *dt_node_op3(dt_node_t *, dt_node_t *, dt_node_t *);
196 extern dt_node_t *dt_node_statement(dt_node_t *);
197 extern dt_node_t *dt_node_pdesc_by_name(char *);
198 extern dt_node_t *dt_node_pdesc_by_id(uintmax_t);
199 extern dt_node_t *dt_node_clause(dt_node_t *, dt_node_t *, dt_node_t *);
200 extern dt_node_t *dt_node_inline(dt_node_t *);
201 extern dt_node_t *dt_node_member(dt_decl_t *, char *, dt_node_t *);
202 extern dt_node_t *dt_node_xlator(dt_decl_t *, dt_decl_t *, char *, dt_node_t *);
203 extern dt_node_t *dt_node_probe(char *, dt_node_t *, dt_node_t *);
204 extern dt_node_t *dt_node_provider(char *, dt_node_t *);
205 extern dt_node_t *dt_node_program(dt_node_t *);
206 
207 extern dt_node_t *dt_node_link(dt_node_t *, dt_node_t *);
208 extern dt_node_t *dt_node_cook(dt_node_t *, uint_t);
209 
210 extern dt_node_t *dt_node_xalloc(dtrace_hdl_t *, int);
211 extern void dt_node_free(dt_node_t *);
212 
213 extern dtrace_attribute_t dt_node_list_cook(dt_node_t **, uint_t);
214 extern void dt_node_list_free(dt_node_t **);
215 extern void dt_node_link_free(dt_node_t **);
216 
217 extern void dt_node_attr_assign(dt_node_t *, dtrace_attribute_t);
218 extern void dt_node_type_assign(dt_node_t *, ctf_file_t *, ctf_id_t);
219 extern void dt_node_type_propagate(const dt_node_t *, dt_node_t *);
220 extern const char *dt_node_type_name(const dt_node_t *, char *, size_t);
221 extern size_t dt_node_type_size(const dt_node_t *);
222 
223 extern dt_ident_t *dt_node_resolve(const dt_node_t *, uint_t);
224 extern size_t dt_node_sizeof(const dt_node_t *);
225 extern void dt_node_promote(dt_node_t *, dt_node_t *, dt_node_t *);
226 
227 extern void dt_node_diftype(const dt_node_t *, dtrace_diftype_t *);
228 extern void dt_node_printr(dt_node_t *, FILE *, int);
229 extern const char *dt_node_name(const dt_node_t *, char *, size_t);
230 extern int dt_node_root(dt_node_t *);
231 
232 struct dtrace_typeinfo;	/* see <dtrace.h> */
233 struct dt_pcb;		/* see <dt_impl.h> */
234 
235 #define	IS_CHAR(e) \
236 	(((e).cte_format & (CTF_INT_CHAR | CTF_INT_SIGNED)) == \
237 	(CTF_INT_CHAR | CTF_INT_SIGNED) && (e).cte_bits == NBBY)
238 
239 #define	IS_VOID(e) \
240 	((e).cte_offset == 0 && (e).cte_bits == 0)
241 
242 extern int dt_type_lookup(const char *, struct dtrace_typeinfo *);
243 extern int dt_type_pointer(struct dtrace_typeinfo *);
244 extern const char *dt_type_name(ctf_file_t *, ctf_id_t, char *, size_t);
245 
246 typedef enum {
247 	YYS_CLAUSE,	/* lex/yacc state for finding program clauses */
248 	YYS_DEFINE,	/* lex/yacc state for parsing persistent definitions */
249 	YYS_EXPR,	/* lex/yacc state for parsing D expressions */
250 	YYS_DONE	/* lex/yacc state for indicating parse tree is done */
251 } yystate_t;
252 
253 extern void dnerror(const dt_node_t *, dt_errtag_t, const char *, ...);
254 extern void dnwarn(const dt_node_t *, dt_errtag_t, const char *, ...);
255 
256 extern void xyerror(dt_errtag_t, const char *, ...);
257 extern void xywarn(dt_errtag_t, const char *, ...);
258 extern void xyvwarn(dt_errtag_t, const char *, va_list);
259 
260 extern void yyerror(const char *, ...);
261 extern void yywarn(const char *, ...);
262 extern void yyvwarn(const char *, va_list);
263 
264 extern void yylabel(const char *);
265 extern void yybegin(yystate_t);
266 extern void yyinit(struct dt_pcb *);
267 
268 extern int yyparse(void);
269 extern int yyinput(void);
270 
271 #ifdef	__cplusplus
272 }
273 #endif
274 
275 #endif	/* _DT_PARSER_H */
276