Deleted Added
full compact
dt_parser.c (256281) dt_parser.c (268578)
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/*
24 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
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/*
24 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
25 * Copyright (c) 2011, Joyent Inc. All rights reserved.
26 * Copyright (c) 2012 by Delphix. All rights reserved.
25 * Copyright (c) 2013, Joyent Inc. All rights reserved.
26 * Copyright (c) 2013 by Delphix. All rights reserved.
27 */
28
29#pragma ident "%Z%%M% %I% %E% SMI"
30
31/*
32 * DTrace D Language Parser
33 *
34 * The D Parser is a lex/yacc parser consisting of the lexer dt_lex.l, the
35 * parsing grammar dt_grammar.y, and this file, dt_parser.c, which handles
36 * the construction of the parse tree nodes and their syntactic validation.
37 * The parse tree is constructed of dt_node_t structures (see <dt_parser.h>)
38 * that are built in two passes: (1) the "create" pass, where the parse tree
39 * nodes are allocated by calls from the grammar to dt_node_*() subroutines,
40 * and (2) the "cook" pass, where nodes are coalesced, assigned D types, and
41 * validated according to the syntactic rules of the language.
42 *
43 * All node allocations are performed using dt_node_alloc(). All node frees
44 * during the parsing phase are performed by dt_node_free(), which frees node-
45 * internal state but does not actually free the nodes. All final node frees
46 * are done as part of the end of dt_compile() or as part of destroying
47 * persistent identifiers or translators which have embedded nodes.
48 *
49 * The dt_node_* routines that implement pass (1) may allocate new nodes. The
50 * dt_cook_* routines that implement pass (2) may *not* allocate new nodes.
51 * They may free existing nodes using dt_node_free(), but they may not actually
52 * deallocate any dt_node_t's. Currently dt_cook_op2() is an exception to this
53 * rule: see the comments therein for how this issue is resolved.
54 *
55 * The dt_cook_* routines are responsible for (at minimum) setting the final
56 * node type (dn_ctfp/dn_type) and attributes (dn_attr). If dn_ctfp/dn_type
57 * are set manually (i.e. not by one of the type assignment functions), then
58 * the DT_NF_COOKED flag must be set manually on the node.
59 *
60 * The cooking pass can be applied to the same parse tree more than once (used
61 * in the case of a comma-separated list of probe descriptions). As such, the
62 * cook routines must not perform any parse tree transformations which would
63 * be invalid if the tree were subsequently cooked using a different context.
64 *
65 * The dn_ctfp and dn_type fields form the type of the node. This tuple can
66 * take on the following set of values, which form our type invariants:
67 *
68 * 1. dn_ctfp = NULL, dn_type = CTF_ERR
69 *
70 * In this state, the node has unknown type and is not yet cooked. The
71 * DT_NF_COOKED flag is not yet set on the node.
72 *
73 * 2. dn_ctfp = DT_DYN_CTFP(dtp), dn_type = DT_DYN_TYPE(dtp)
74 *
75 * In this state, the node is a dynamic D type. This means that generic
76 * operations are not valid on this node and only code that knows how to
77 * examine the inner details of the node can operate on it. A <DYN> node
78 * must have dn_ident set to point to an identifier describing the object
79 * and its type. The DT_NF_REF flag is set for all nodes of type <DYN>.
80 * At present, the D compiler uses the <DYN> type for:
81 *
82 * - associative arrays that do not yet have a value type defined
83 * - translated data (i.e. the result of the xlate operator)
84 * - aggregations
85 *
86 * 3. dn_ctfp = DT_STR_CTFP(dtp), dn_type = DT_STR_TYPE(dtp)
87 *
88 * In this state, the node is of type D string. The string type is really
89 * a char[0] typedef, but requires special handling throughout the compiler.
90 *
91 * 4. dn_ctfp != NULL, dn_type = any other type ID
92 *
93 * In this state, the node is of some known D/CTF type. The normal libctf
94 * APIs can be used to learn more about the type name or structure. When
95 * the type is assigned, the DT_NF_SIGNED, DT_NF_REF, and DT_NF_BITFIELD
96 * flags cache the corresponding attributes of the underlying CTF type.
97 */
98
99#include <sys/param.h>
100#include <sys/sysmacros.h>
101#include <limits.h>
102#include <setjmp.h>
103#include <strings.h>
104#include <assert.h>
105#if defined(sun)
106#include <alloca.h>
107#endif
108#include <stdlib.h>
109#include <stdarg.h>
110#include <stdio.h>
111#include <errno.h>
112#include <ctype.h>
113
114#include <dt_impl.h>
115#include <dt_grammar.h>
116#include <dt_module.h>
117#include <dt_provider.h>
118#include <dt_string.h>
119#include <dt_as.h>
120
121dt_pcb_t *yypcb; /* current control block for parser */
122dt_node_t *yypragma; /* lex token list for control lines */
123char yyintprefix; /* int token macro prefix (+/-) */
124char yyintsuffix[4]; /* int token suffix string [uU][lL] */
125int yyintdecimal; /* int token format flag (1=decimal, 0=octal/hex) */
126
127static const char *
128opstr(int op)
129{
130 switch (op) {
131 case DT_TOK_COMMA: return (",");
132 case DT_TOK_ELLIPSIS: return ("...");
133 case DT_TOK_ASGN: return ("=");
134 case DT_TOK_ADD_EQ: return ("+=");
135 case DT_TOK_SUB_EQ: return ("-=");
136 case DT_TOK_MUL_EQ: return ("*=");
137 case DT_TOK_DIV_EQ: return ("/=");
138 case DT_TOK_MOD_EQ: return ("%=");
139 case DT_TOK_AND_EQ: return ("&=");
140 case DT_TOK_XOR_EQ: return ("^=");
141 case DT_TOK_OR_EQ: return ("|=");
142 case DT_TOK_LSH_EQ: return ("<<=");
143 case DT_TOK_RSH_EQ: return (">>=");
144 case DT_TOK_QUESTION: return ("?");
145 case DT_TOK_COLON: return (":");
146 case DT_TOK_LOR: return ("||");
147 case DT_TOK_LXOR: return ("^^");
148 case DT_TOK_LAND: return ("&&");
149 case DT_TOK_BOR: return ("|");
150 case DT_TOK_XOR: return ("^");
151 case DT_TOK_BAND: return ("&");
152 case DT_TOK_EQU: return ("==");
153 case DT_TOK_NEQ: return ("!=");
154 case DT_TOK_LT: return ("<");
155 case DT_TOK_LE: return ("<=");
156 case DT_TOK_GT: return (">");
157 case DT_TOK_GE: return (">=");
158 case DT_TOK_LSH: return ("<<");
159 case DT_TOK_RSH: return (">>");
160 case DT_TOK_ADD: return ("+");
161 case DT_TOK_SUB: return ("-");
162 case DT_TOK_MUL: return ("*");
163 case DT_TOK_DIV: return ("/");
164 case DT_TOK_MOD: return ("%");
165 case DT_TOK_LNEG: return ("!");
166 case DT_TOK_BNEG: return ("~");
167 case DT_TOK_ADDADD: return ("++");
168 case DT_TOK_PREINC: return ("++");
169 case DT_TOK_POSTINC: return ("++");
170 case DT_TOK_SUBSUB: return ("--");
171 case DT_TOK_PREDEC: return ("--");
172 case DT_TOK_POSTDEC: return ("--");
173 case DT_TOK_IPOS: return ("+");
174 case DT_TOK_INEG: return ("-");
175 case DT_TOK_DEREF: return ("*");
176 case DT_TOK_ADDROF: return ("&");
177 case DT_TOK_OFFSETOF: return ("offsetof");
178 case DT_TOK_SIZEOF: return ("sizeof");
179 case DT_TOK_STRINGOF: return ("stringof");
180 case DT_TOK_XLATE: return ("xlate");
181 case DT_TOK_LPAR: return ("(");
182 case DT_TOK_RPAR: return (")");
183 case DT_TOK_LBRAC: return ("[");
184 case DT_TOK_RBRAC: return ("]");
185 case DT_TOK_PTR: return ("->");
186 case DT_TOK_DOT: return (".");
187 case DT_TOK_STRING: return ("<string>");
188 case DT_TOK_IDENT: return ("<ident>");
189 case DT_TOK_TNAME: return ("<type>");
190 case DT_TOK_INT: return ("<int>");
191 default: return ("<?>");
192 }
193}
194
195int
196dt_type_lookup(const char *s, dtrace_typeinfo_t *tip)
197{
198 static const char delimiters[] = " \t\n\r\v\f*`";
199 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
27 */
28
29#pragma ident "%Z%%M% %I% %E% SMI"
30
31/*
32 * DTrace D Language Parser
33 *
34 * The D Parser is a lex/yacc parser consisting of the lexer dt_lex.l, the
35 * parsing grammar dt_grammar.y, and this file, dt_parser.c, which handles
36 * the construction of the parse tree nodes and their syntactic validation.
37 * The parse tree is constructed of dt_node_t structures (see <dt_parser.h>)
38 * that are built in two passes: (1) the "create" pass, where the parse tree
39 * nodes are allocated by calls from the grammar to dt_node_*() subroutines,
40 * and (2) the "cook" pass, where nodes are coalesced, assigned D types, and
41 * validated according to the syntactic rules of the language.
42 *
43 * All node allocations are performed using dt_node_alloc(). All node frees
44 * during the parsing phase are performed by dt_node_free(), which frees node-
45 * internal state but does not actually free the nodes. All final node frees
46 * are done as part of the end of dt_compile() or as part of destroying
47 * persistent identifiers or translators which have embedded nodes.
48 *
49 * The dt_node_* routines that implement pass (1) may allocate new nodes. The
50 * dt_cook_* routines that implement pass (2) may *not* allocate new nodes.
51 * They may free existing nodes using dt_node_free(), but they may not actually
52 * deallocate any dt_node_t's. Currently dt_cook_op2() is an exception to this
53 * rule: see the comments therein for how this issue is resolved.
54 *
55 * The dt_cook_* routines are responsible for (at minimum) setting the final
56 * node type (dn_ctfp/dn_type) and attributes (dn_attr). If dn_ctfp/dn_type
57 * are set manually (i.e. not by one of the type assignment functions), then
58 * the DT_NF_COOKED flag must be set manually on the node.
59 *
60 * The cooking pass can be applied to the same parse tree more than once (used
61 * in the case of a comma-separated list of probe descriptions). As such, the
62 * cook routines must not perform any parse tree transformations which would
63 * be invalid if the tree were subsequently cooked using a different context.
64 *
65 * The dn_ctfp and dn_type fields form the type of the node. This tuple can
66 * take on the following set of values, which form our type invariants:
67 *
68 * 1. dn_ctfp = NULL, dn_type = CTF_ERR
69 *
70 * In this state, the node has unknown type and is not yet cooked. The
71 * DT_NF_COOKED flag is not yet set on the node.
72 *
73 * 2. dn_ctfp = DT_DYN_CTFP(dtp), dn_type = DT_DYN_TYPE(dtp)
74 *
75 * In this state, the node is a dynamic D type. This means that generic
76 * operations are not valid on this node and only code that knows how to
77 * examine the inner details of the node can operate on it. A <DYN> node
78 * must have dn_ident set to point to an identifier describing the object
79 * and its type. The DT_NF_REF flag is set for all nodes of type <DYN>.
80 * At present, the D compiler uses the <DYN> type for:
81 *
82 * - associative arrays that do not yet have a value type defined
83 * - translated data (i.e. the result of the xlate operator)
84 * - aggregations
85 *
86 * 3. dn_ctfp = DT_STR_CTFP(dtp), dn_type = DT_STR_TYPE(dtp)
87 *
88 * In this state, the node is of type D string. The string type is really
89 * a char[0] typedef, but requires special handling throughout the compiler.
90 *
91 * 4. dn_ctfp != NULL, dn_type = any other type ID
92 *
93 * In this state, the node is of some known D/CTF type. The normal libctf
94 * APIs can be used to learn more about the type name or structure. When
95 * the type is assigned, the DT_NF_SIGNED, DT_NF_REF, and DT_NF_BITFIELD
96 * flags cache the corresponding attributes of the underlying CTF type.
97 */
98
99#include <sys/param.h>
100#include <sys/sysmacros.h>
101#include <limits.h>
102#include <setjmp.h>
103#include <strings.h>
104#include <assert.h>
105#if defined(sun)
106#include <alloca.h>
107#endif
108#include <stdlib.h>
109#include <stdarg.h>
110#include <stdio.h>
111#include <errno.h>
112#include <ctype.h>
113
114#include <dt_impl.h>
115#include <dt_grammar.h>
116#include <dt_module.h>
117#include <dt_provider.h>
118#include <dt_string.h>
119#include <dt_as.h>
120
121dt_pcb_t *yypcb; /* current control block for parser */
122dt_node_t *yypragma; /* lex token list for control lines */
123char yyintprefix; /* int token macro prefix (+/-) */
124char yyintsuffix[4]; /* int token suffix string [uU][lL] */
125int yyintdecimal; /* int token format flag (1=decimal, 0=octal/hex) */
126
127static const char *
128opstr(int op)
129{
130 switch (op) {
131 case DT_TOK_COMMA: return (",");
132 case DT_TOK_ELLIPSIS: return ("...");
133 case DT_TOK_ASGN: return ("=");
134 case DT_TOK_ADD_EQ: return ("+=");
135 case DT_TOK_SUB_EQ: return ("-=");
136 case DT_TOK_MUL_EQ: return ("*=");
137 case DT_TOK_DIV_EQ: return ("/=");
138 case DT_TOK_MOD_EQ: return ("%=");
139 case DT_TOK_AND_EQ: return ("&=");
140 case DT_TOK_XOR_EQ: return ("^=");
141 case DT_TOK_OR_EQ: return ("|=");
142 case DT_TOK_LSH_EQ: return ("<<=");
143 case DT_TOK_RSH_EQ: return (">>=");
144 case DT_TOK_QUESTION: return ("?");
145 case DT_TOK_COLON: return (":");
146 case DT_TOK_LOR: return ("||");
147 case DT_TOK_LXOR: return ("^^");
148 case DT_TOK_LAND: return ("&&");
149 case DT_TOK_BOR: return ("|");
150 case DT_TOK_XOR: return ("^");
151 case DT_TOK_BAND: return ("&");
152 case DT_TOK_EQU: return ("==");
153 case DT_TOK_NEQ: return ("!=");
154 case DT_TOK_LT: return ("<");
155 case DT_TOK_LE: return ("<=");
156 case DT_TOK_GT: return (">");
157 case DT_TOK_GE: return (">=");
158 case DT_TOK_LSH: return ("<<");
159 case DT_TOK_RSH: return (">>");
160 case DT_TOK_ADD: return ("+");
161 case DT_TOK_SUB: return ("-");
162 case DT_TOK_MUL: return ("*");
163 case DT_TOK_DIV: return ("/");
164 case DT_TOK_MOD: return ("%");
165 case DT_TOK_LNEG: return ("!");
166 case DT_TOK_BNEG: return ("~");
167 case DT_TOK_ADDADD: return ("++");
168 case DT_TOK_PREINC: return ("++");
169 case DT_TOK_POSTINC: return ("++");
170 case DT_TOK_SUBSUB: return ("--");
171 case DT_TOK_PREDEC: return ("--");
172 case DT_TOK_POSTDEC: return ("--");
173 case DT_TOK_IPOS: return ("+");
174 case DT_TOK_INEG: return ("-");
175 case DT_TOK_DEREF: return ("*");
176 case DT_TOK_ADDROF: return ("&");
177 case DT_TOK_OFFSETOF: return ("offsetof");
178 case DT_TOK_SIZEOF: return ("sizeof");
179 case DT_TOK_STRINGOF: return ("stringof");
180 case DT_TOK_XLATE: return ("xlate");
181 case DT_TOK_LPAR: return ("(");
182 case DT_TOK_RPAR: return (")");
183 case DT_TOK_LBRAC: return ("[");
184 case DT_TOK_RBRAC: return ("]");
185 case DT_TOK_PTR: return ("->");
186 case DT_TOK_DOT: return (".");
187 case DT_TOK_STRING: return ("<string>");
188 case DT_TOK_IDENT: return ("<ident>");
189 case DT_TOK_TNAME: return ("<type>");
190 case DT_TOK_INT: return ("<int>");
191 default: return ("<?>");
192 }
193}
194
195int
196dt_type_lookup(const char *s, dtrace_typeinfo_t *tip)
197{
198 static const char delimiters[] = " \t\n\r\v\f*`";
199 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
200 const char *p, *q, *end, *obj;
200 const char *p, *q, *r, *end, *obj;
201
202 for (p = s, end = s + strlen(s); *p != '\0'; p = q) {
203 while (isspace(*p))
204 p++; /* skip leading whitespace prior to token */
205
206 if (p == end || (q = strpbrk(p + 1, delimiters)) == NULL)
207 break; /* empty string or single token remaining */
208
209 if (*q == '`') {
210 char *object = alloca((size_t)(q - p) + 1);
211 char *type = alloca((size_t)(end - s) + 1);
212
213 /*
214 * Copy from the start of the token (p) to the location
215 * backquote (q) to extract the nul-terminated object.
216 */
217 bcopy(p, object, (size_t)(q - p));
218 object[(size_t)(q - p)] = '\0';
219
220 /*
221 * Copy the original string up to the start of this
222 * token (p) into type, and then concatenate everything
223 * after q. This is the type name without the object.
224 */
225 bcopy(s, type, (size_t)(p - s));
226 bcopy(q + 1, type + (size_t)(p - s), strlen(q + 1) + 1);
227
201
202 for (p = s, end = s + strlen(s); *p != '\0'; p = q) {
203 while (isspace(*p))
204 p++; /* skip leading whitespace prior to token */
205
206 if (p == end || (q = strpbrk(p + 1, delimiters)) == NULL)
207 break; /* empty string or single token remaining */
208
209 if (*q == '`') {
210 char *object = alloca((size_t)(q - p) + 1);
211 char *type = alloca((size_t)(end - s) + 1);
212
213 /*
214 * Copy from the start of the token (p) to the location
215 * backquote (q) to extract the nul-terminated object.
216 */
217 bcopy(p, object, (size_t)(q - p));
218 object[(size_t)(q - p)] = '\0';
219
220 /*
221 * Copy the original string up to the start of this
222 * token (p) into type, and then concatenate everything
223 * after q. This is the type name without the object.
224 */
225 bcopy(s, type, (size_t)(p - s));
226 bcopy(q + 1, type + (size_t)(p - s), strlen(q + 1) + 1);
227
228 if (strchr(q + 1, '`') != NULL)
229 return (dt_set_errno(dtp, EDT_BADSCOPE));
228 /*
229 * There may be at most three delimeters. The second
230 * delimeter is usually used to distinguish the type
231 * within a given module, however, there could be a link
232 * map id on the scene in which case that delimeter
233 * would be the third. We determine presence of the lmid
234 * if it rouglhly meets the from LM[0-9]
235 */
236 if ((r = strchr(q + 1, '`')) != NULL &&
237 ((r = strchr(r + 1, '`')) != NULL)) {
238 if (strchr(r + 1, '`') != NULL)
239 return (dt_set_errno(dtp,
240 EDT_BADSCOPE));
241 if (q[1] != 'L' || q[2] != 'M')
242 return (dt_set_errno(dtp,
243 EDT_BADSCOPE));
244 }
230
231 return (dtrace_lookup_by_type(dtp, object, type, tip));
232 }
233 }
234
235 if (yypcb->pcb_idepth != 0)
236 obj = DTRACE_OBJ_CDEFS;
237 else
238 obj = DTRACE_OBJ_EVERY;
239
240 return (dtrace_lookup_by_type(dtp, obj, s, tip));
241}
242
243/*
244 * When we parse type expressions or parse an expression with unary "&", we
245 * need to find a type that is a pointer to a previously known type.
246 * Unfortunately CTF is limited to a per-container view, so ctf_type_pointer()
247 * alone does not suffice for our needs. We provide a more intelligent wrapper
248 * for the compiler that attempts to compute a pointer to either the given type
249 * or its base (that is, we try both "foo_t *" and "struct foo *"), and also
250 * to potentially construct the required type on-the-fly.
251 */
252int
253dt_type_pointer(dtrace_typeinfo_t *tip)
254{
255 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
256 ctf_file_t *ctfp = tip->dtt_ctfp;
257 ctf_id_t type = tip->dtt_type;
258 ctf_id_t base = ctf_type_resolve(ctfp, type);
245
246 return (dtrace_lookup_by_type(dtp, object, type, tip));
247 }
248 }
249
250 if (yypcb->pcb_idepth != 0)
251 obj = DTRACE_OBJ_CDEFS;
252 else
253 obj = DTRACE_OBJ_EVERY;
254
255 return (dtrace_lookup_by_type(dtp, obj, s, tip));
256}
257
258/*
259 * When we parse type expressions or parse an expression with unary "&", we
260 * need to find a type that is a pointer to a previously known type.
261 * Unfortunately CTF is limited to a per-container view, so ctf_type_pointer()
262 * alone does not suffice for our needs. We provide a more intelligent wrapper
263 * for the compiler that attempts to compute a pointer to either the given type
264 * or its base (that is, we try both "foo_t *" and "struct foo *"), and also
265 * to potentially construct the required type on-the-fly.
266 */
267int
268dt_type_pointer(dtrace_typeinfo_t *tip)
269{
270 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
271 ctf_file_t *ctfp = tip->dtt_ctfp;
272 ctf_id_t type = tip->dtt_type;
273 ctf_id_t base = ctf_type_resolve(ctfp, type);
274 uint_t bflags = tip->dtt_flags;
259
260 dt_module_t *dmp;
261 ctf_id_t ptr;
262
263 if ((ptr = ctf_type_pointer(ctfp, type)) != CTF_ERR ||
264 (ptr = ctf_type_pointer(ctfp, base)) != CTF_ERR) {
265 tip->dtt_type = ptr;
266 return (0);
267 }
268
269 if (yypcb->pcb_idepth != 0)
270 dmp = dtp->dt_cdefs;
271 else
272 dmp = dtp->dt_ddefs;
273
274 if (ctfp != dmp->dm_ctfp && ctfp != ctf_parent_file(dmp->dm_ctfp) &&
275 (type = ctf_add_type(dmp->dm_ctfp, ctfp, type)) == CTF_ERR) {
276 dtp->dt_ctferr = ctf_errno(dmp->dm_ctfp);
277 return (dt_set_errno(dtp, EDT_CTF));
278 }
279
280 ptr = ctf_add_pointer(dmp->dm_ctfp, CTF_ADD_ROOT, type);
281
282 if (ptr == CTF_ERR || ctf_update(dmp->dm_ctfp) == CTF_ERR) {
283 dtp->dt_ctferr = ctf_errno(dmp->dm_ctfp);
284 return (dt_set_errno(dtp, EDT_CTF));
285 }
286
287 tip->dtt_object = dmp->dm_name;
288 tip->dtt_ctfp = dmp->dm_ctfp;
289 tip->dtt_type = ptr;
275
276 dt_module_t *dmp;
277 ctf_id_t ptr;
278
279 if ((ptr = ctf_type_pointer(ctfp, type)) != CTF_ERR ||
280 (ptr = ctf_type_pointer(ctfp, base)) != CTF_ERR) {
281 tip->dtt_type = ptr;
282 return (0);
283 }
284
285 if (yypcb->pcb_idepth != 0)
286 dmp = dtp->dt_cdefs;
287 else
288 dmp = dtp->dt_ddefs;
289
290 if (ctfp != dmp->dm_ctfp && ctfp != ctf_parent_file(dmp->dm_ctfp) &&
291 (type = ctf_add_type(dmp->dm_ctfp, ctfp, type)) == CTF_ERR) {
292 dtp->dt_ctferr = ctf_errno(dmp->dm_ctfp);
293 return (dt_set_errno(dtp, EDT_CTF));
294 }
295
296 ptr = ctf_add_pointer(dmp->dm_ctfp, CTF_ADD_ROOT, type);
297
298 if (ptr == CTF_ERR || ctf_update(dmp->dm_ctfp) == CTF_ERR) {
299 dtp->dt_ctferr = ctf_errno(dmp->dm_ctfp);
300 return (dt_set_errno(dtp, EDT_CTF));
301 }
302
303 tip->dtt_object = dmp->dm_name;
304 tip->dtt_ctfp = dmp->dm_ctfp;
305 tip->dtt_type = ptr;
306 tip->dtt_flags = bflags;
290
291 return (0);
292}
293
294const char *
295dt_type_name(ctf_file_t *ctfp, ctf_id_t type, char *buf, size_t len)
296{
297 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
298
299 if (ctfp == DT_FPTR_CTFP(dtp) && type == DT_FPTR_TYPE(dtp))
300 (void) snprintf(buf, len, "function pointer");
301 else if (ctfp == DT_FUNC_CTFP(dtp) && type == DT_FUNC_TYPE(dtp))
302 (void) snprintf(buf, len, "function");
303 else if (ctfp == DT_DYN_CTFP(dtp) && type == DT_DYN_TYPE(dtp))
304 (void) snprintf(buf, len, "dynamic variable");
305 else if (ctfp == NULL)
306 (void) snprintf(buf, len, "<none>");
307 else if (ctf_type_name(ctfp, type, buf, len) == NULL)
308 (void) snprintf(buf, len, "unknown");
309
310 return (buf);
311}
312
313/*
314 * Perform the "usual arithmetic conversions" to determine which of the two
315 * input operand types should be promoted and used as a result type. The
316 * rules for this are described in ISOC[6.3.1.8] and K&R[A6.5].
317 */
318static void
319dt_type_promote(dt_node_t *lp, dt_node_t *rp, ctf_file_t **ofp, ctf_id_t *otype)
320{
321 ctf_file_t *lfp = lp->dn_ctfp;
322 ctf_id_t ltype = lp->dn_type;
323
324 ctf_file_t *rfp = rp->dn_ctfp;
325 ctf_id_t rtype = rp->dn_type;
326
327 ctf_id_t lbase = ctf_type_resolve(lfp, ltype);
328 uint_t lkind = ctf_type_kind(lfp, lbase);
329
330 ctf_id_t rbase = ctf_type_resolve(rfp, rtype);
331 uint_t rkind = ctf_type_kind(rfp, rbase);
332
333 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
334 ctf_encoding_t le, re;
335 uint_t lrank, rrank;
336
337 assert(lkind == CTF_K_INTEGER || lkind == CTF_K_ENUM);
338 assert(rkind == CTF_K_INTEGER || rkind == CTF_K_ENUM);
339
340 if (lkind == CTF_K_ENUM) {
341 lfp = DT_INT_CTFP(dtp);
342 ltype = lbase = DT_INT_TYPE(dtp);
343 }
344
345 if (rkind == CTF_K_ENUM) {
346 rfp = DT_INT_CTFP(dtp);
347 rtype = rbase = DT_INT_TYPE(dtp);
348 }
349
350 if (ctf_type_encoding(lfp, lbase, &le) == CTF_ERR) {
351 yypcb->pcb_hdl->dt_ctferr = ctf_errno(lfp);
352 longjmp(yypcb->pcb_jmpbuf, EDT_CTF);
353 }
354
355 if (ctf_type_encoding(rfp, rbase, &re) == CTF_ERR) {
356 yypcb->pcb_hdl->dt_ctferr = ctf_errno(rfp);
357 longjmp(yypcb->pcb_jmpbuf, EDT_CTF);
358 }
359
360 /*
361 * Compute an integer rank based on the size and unsigned status.
362 * If rank is identical, pick the "larger" of the equivalent types
363 * which we define as having a larger base ctf_id_t. If rank is
364 * different, pick the type with the greater rank.
365 */
366 lrank = le.cte_bits + ((le.cte_format & CTF_INT_SIGNED) == 0);
367 rrank = re.cte_bits + ((re.cte_format & CTF_INT_SIGNED) == 0);
368
369 if (lrank == rrank) {
370 if (lbase - rbase < 0)
371 goto return_rtype;
372 else
373 goto return_ltype;
374 } else if (lrank > rrank) {
375 goto return_ltype;
376 } else
377 goto return_rtype;
378
379return_ltype:
380 *ofp = lfp;
381 *otype = ltype;
382 return;
383
384return_rtype:
385 *ofp = rfp;
386 *otype = rtype;
387}
388
389void
390dt_node_promote(dt_node_t *lp, dt_node_t *rp, dt_node_t *dnp)
391{
392 dt_type_promote(lp, rp, &dnp->dn_ctfp, &dnp->dn_type);
307
308 return (0);
309}
310
311const char *
312dt_type_name(ctf_file_t *ctfp, ctf_id_t type, char *buf, size_t len)
313{
314 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
315
316 if (ctfp == DT_FPTR_CTFP(dtp) && type == DT_FPTR_TYPE(dtp))
317 (void) snprintf(buf, len, "function pointer");
318 else if (ctfp == DT_FUNC_CTFP(dtp) && type == DT_FUNC_TYPE(dtp))
319 (void) snprintf(buf, len, "function");
320 else if (ctfp == DT_DYN_CTFP(dtp) && type == DT_DYN_TYPE(dtp))
321 (void) snprintf(buf, len, "dynamic variable");
322 else if (ctfp == NULL)
323 (void) snprintf(buf, len, "<none>");
324 else if (ctf_type_name(ctfp, type, buf, len) == NULL)
325 (void) snprintf(buf, len, "unknown");
326
327 return (buf);
328}
329
330/*
331 * Perform the "usual arithmetic conversions" to determine which of the two
332 * input operand types should be promoted and used as a result type. The
333 * rules for this are described in ISOC[6.3.1.8] and K&R[A6.5].
334 */
335static void
336dt_type_promote(dt_node_t *lp, dt_node_t *rp, ctf_file_t **ofp, ctf_id_t *otype)
337{
338 ctf_file_t *lfp = lp->dn_ctfp;
339 ctf_id_t ltype = lp->dn_type;
340
341 ctf_file_t *rfp = rp->dn_ctfp;
342 ctf_id_t rtype = rp->dn_type;
343
344 ctf_id_t lbase = ctf_type_resolve(lfp, ltype);
345 uint_t lkind = ctf_type_kind(lfp, lbase);
346
347 ctf_id_t rbase = ctf_type_resolve(rfp, rtype);
348 uint_t rkind = ctf_type_kind(rfp, rbase);
349
350 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
351 ctf_encoding_t le, re;
352 uint_t lrank, rrank;
353
354 assert(lkind == CTF_K_INTEGER || lkind == CTF_K_ENUM);
355 assert(rkind == CTF_K_INTEGER || rkind == CTF_K_ENUM);
356
357 if (lkind == CTF_K_ENUM) {
358 lfp = DT_INT_CTFP(dtp);
359 ltype = lbase = DT_INT_TYPE(dtp);
360 }
361
362 if (rkind == CTF_K_ENUM) {
363 rfp = DT_INT_CTFP(dtp);
364 rtype = rbase = DT_INT_TYPE(dtp);
365 }
366
367 if (ctf_type_encoding(lfp, lbase, &le) == CTF_ERR) {
368 yypcb->pcb_hdl->dt_ctferr = ctf_errno(lfp);
369 longjmp(yypcb->pcb_jmpbuf, EDT_CTF);
370 }
371
372 if (ctf_type_encoding(rfp, rbase, &re) == CTF_ERR) {
373 yypcb->pcb_hdl->dt_ctferr = ctf_errno(rfp);
374 longjmp(yypcb->pcb_jmpbuf, EDT_CTF);
375 }
376
377 /*
378 * Compute an integer rank based on the size and unsigned status.
379 * If rank is identical, pick the "larger" of the equivalent types
380 * which we define as having a larger base ctf_id_t. If rank is
381 * different, pick the type with the greater rank.
382 */
383 lrank = le.cte_bits + ((le.cte_format & CTF_INT_SIGNED) == 0);
384 rrank = re.cte_bits + ((re.cte_format & CTF_INT_SIGNED) == 0);
385
386 if (lrank == rrank) {
387 if (lbase - rbase < 0)
388 goto return_rtype;
389 else
390 goto return_ltype;
391 } else if (lrank > rrank) {
392 goto return_ltype;
393 } else
394 goto return_rtype;
395
396return_ltype:
397 *ofp = lfp;
398 *otype = ltype;
399 return;
400
401return_rtype:
402 *ofp = rfp;
403 *otype = rtype;
404}
405
406void
407dt_node_promote(dt_node_t *lp, dt_node_t *rp, dt_node_t *dnp)
408{
409 dt_type_promote(lp, rp, &dnp->dn_ctfp, &dnp->dn_type);
393 dt_node_type_assign(dnp, dnp->dn_ctfp, dnp->dn_type);
410 dt_node_type_assign(dnp, dnp->dn_ctfp, dnp->dn_type, B_FALSE);
394 dt_node_attr_assign(dnp, dt_attr_min(lp->dn_attr, rp->dn_attr));
395}
396
397const char *
398dt_node_name(const dt_node_t *dnp, char *buf, size_t len)
399{
400 char n1[DT_TYPE_NAMELEN];
401 char n2[DT_TYPE_NAMELEN];
402
403 const char *prefix = "", *suffix = "";
404 const dtrace_syminfo_t *dts;
405 char *s;
406
407 switch (dnp->dn_kind) {
408 case DT_NODE_INT:
409 (void) snprintf(buf, len, "integer constant 0x%llx",
410 (u_longlong_t)dnp->dn_value);
411 break;
412 case DT_NODE_STRING:
413 s = strchr2esc(dnp->dn_string, strlen(dnp->dn_string));
414 (void) snprintf(buf, len, "string constant \"%s\"",
415 s != NULL ? s : dnp->dn_string);
416 free(s);
417 break;
418 case DT_NODE_IDENT:
419 (void) snprintf(buf, len, "identifier %s", dnp->dn_string);
420 break;
421 case DT_NODE_VAR:
422 case DT_NODE_FUNC:
423 case DT_NODE_AGG:
424 case DT_NODE_INLINE:
425 switch (dnp->dn_ident->di_kind) {
426 case DT_IDENT_FUNC:
427 case DT_IDENT_AGGFUNC:
428 case DT_IDENT_ACTFUNC:
429 suffix = "( )";
430 break;
431 case DT_IDENT_AGG:
432 prefix = "@";
433 break;
434 }
435 (void) snprintf(buf, len, "%s %s%s%s",
436 dt_idkind_name(dnp->dn_ident->di_kind),
437 prefix, dnp->dn_ident->di_name, suffix);
438 break;
439 case DT_NODE_SYM:
440 dts = dnp->dn_ident->di_data;
441 (void) snprintf(buf, len, "symbol %s`%s",
442 dts->dts_object, dts->dts_name);
443 break;
444 case DT_NODE_TYPE:
445 (void) snprintf(buf, len, "type %s",
446 dt_node_type_name(dnp, n1, sizeof (n1)));
447 break;
448 case DT_NODE_OP1:
449 case DT_NODE_OP2:
450 case DT_NODE_OP3:
451 (void) snprintf(buf, len, "operator %s", opstr(dnp->dn_op));
452 break;
453 case DT_NODE_DEXPR:
454 case DT_NODE_DFUNC:
455 if (dnp->dn_expr)
456 return (dt_node_name(dnp->dn_expr, buf, len));
457 (void) snprintf(buf, len, "%s", "statement");
458 break;
459 case DT_NODE_PDESC:
460 if (dnp->dn_desc->dtpd_id == 0) {
461 (void) snprintf(buf, len,
462 "probe description %s:%s:%s:%s",
463 dnp->dn_desc->dtpd_provider, dnp->dn_desc->dtpd_mod,
464 dnp->dn_desc->dtpd_func, dnp->dn_desc->dtpd_name);
465 } else {
466 (void) snprintf(buf, len, "probe description %u",
467 dnp->dn_desc->dtpd_id);
468 }
469 break;
470 case DT_NODE_CLAUSE:
471 (void) snprintf(buf, len, "%s", "clause");
472 break;
473 case DT_NODE_MEMBER:
474 (void) snprintf(buf, len, "member %s", dnp->dn_membname);
475 break;
476 case DT_NODE_XLATOR:
477 (void) snprintf(buf, len, "translator <%s> (%s)",
478 dt_type_name(dnp->dn_xlator->dx_dst_ctfp,
479 dnp->dn_xlator->dx_dst_type, n1, sizeof (n1)),
480 dt_type_name(dnp->dn_xlator->dx_src_ctfp,
481 dnp->dn_xlator->dx_src_type, n2, sizeof (n2)));
482 break;
483 case DT_NODE_PROG:
484 (void) snprintf(buf, len, "%s", "program");
485 break;
486 default:
487 (void) snprintf(buf, len, "node <%u>", dnp->dn_kind);
488 break;
489 }
490
491 return (buf);
492}
493
494/*
495 * dt_node_xalloc() can be used to create new parse nodes from any libdtrace
496 * caller. The caller is responsible for assigning dn_link appropriately.
497 */
498dt_node_t *
499dt_node_xalloc(dtrace_hdl_t *dtp, int kind)
500{
501 dt_node_t *dnp = dt_alloc(dtp, sizeof (dt_node_t));
502
503 if (dnp == NULL)
504 return (NULL);
505
506 dnp->dn_ctfp = NULL;
507 dnp->dn_type = CTF_ERR;
508 dnp->dn_kind = (uchar_t)kind;
509 dnp->dn_flags = 0;
510 dnp->dn_op = 0;
511 dnp->dn_line = -1;
512 dnp->dn_reg = -1;
513 dnp->dn_attr = _dtrace_defattr;
514 dnp->dn_list = NULL;
515 dnp->dn_link = NULL;
516 bzero(&dnp->dn_u, sizeof (dnp->dn_u));
517
518 return (dnp);
519}
520
521/*
522 * dt_node_alloc() is used to create new parse nodes from the parser. It
523 * assigns the node location based on the current lexer line number and places
524 * the new node on the default allocation list. If allocation fails, we
525 * automatically longjmp the caller back to the enclosing compilation call.
526 */
527static dt_node_t *
528dt_node_alloc(int kind)
529{
530 dt_node_t *dnp = dt_node_xalloc(yypcb->pcb_hdl, kind);
531
532 if (dnp == NULL)
533 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
534
535 dnp->dn_line = yylineno;
536 dnp->dn_link = yypcb->pcb_list;
537 yypcb->pcb_list = dnp;
538
539 return (dnp);
540}
541
542void
543dt_node_free(dt_node_t *dnp)
544{
545 uchar_t kind = dnp->dn_kind;
546
547 dnp->dn_kind = DT_NODE_FREE;
548
549 switch (kind) {
550 case DT_NODE_STRING:
551 case DT_NODE_IDENT:
552 case DT_NODE_TYPE:
553 free(dnp->dn_string);
554 dnp->dn_string = NULL;
555 break;
556
557 case DT_NODE_VAR:
558 case DT_NODE_FUNC:
559 case DT_NODE_PROBE:
560 if (dnp->dn_ident != NULL) {
561 if (dnp->dn_ident->di_flags & DT_IDFLG_ORPHAN)
562 dt_ident_destroy(dnp->dn_ident);
563 dnp->dn_ident = NULL;
564 }
565 dt_node_list_free(&dnp->dn_args);
566 break;
567
568 case DT_NODE_OP1:
569 if (dnp->dn_child != NULL) {
570 dt_node_free(dnp->dn_child);
571 dnp->dn_child = NULL;
572 }
573 break;
574
575 case DT_NODE_OP3:
576 if (dnp->dn_expr != NULL) {
577 dt_node_free(dnp->dn_expr);
578 dnp->dn_expr = NULL;
579 }
580 /*FALLTHRU*/
581 case DT_NODE_OP2:
582 if (dnp->dn_left != NULL) {
583 dt_node_free(dnp->dn_left);
584 dnp->dn_left = NULL;
585 }
586 if (dnp->dn_right != NULL) {
587 dt_node_free(dnp->dn_right);
588 dnp->dn_right = NULL;
589 }
590 break;
591
592 case DT_NODE_DEXPR:
593 case DT_NODE_DFUNC:
594 if (dnp->dn_expr != NULL) {
595 dt_node_free(dnp->dn_expr);
596 dnp->dn_expr = NULL;
597 }
598 break;
599
600 case DT_NODE_AGG:
601 if (dnp->dn_aggfun != NULL) {
602 dt_node_free(dnp->dn_aggfun);
603 dnp->dn_aggfun = NULL;
604 }
605 dt_node_list_free(&dnp->dn_aggtup);
606 break;
607
608 case DT_NODE_PDESC:
609 free(dnp->dn_spec);
610 dnp->dn_spec = NULL;
611 free(dnp->dn_desc);
612 dnp->dn_desc = NULL;
613 break;
614
615 case DT_NODE_CLAUSE:
616 if (dnp->dn_pred != NULL)
617 dt_node_free(dnp->dn_pred);
618 if (dnp->dn_locals != NULL)
619 dt_idhash_destroy(dnp->dn_locals);
620 dt_node_list_free(&dnp->dn_pdescs);
621 dt_node_list_free(&dnp->dn_acts);
622 break;
623
624 case DT_NODE_MEMBER:
625 free(dnp->dn_membname);
626 dnp->dn_membname = NULL;
627 if (dnp->dn_membexpr != NULL) {
628 dt_node_free(dnp->dn_membexpr);
629 dnp->dn_membexpr = NULL;
630 }
631 break;
632
633 case DT_NODE_PROVIDER:
634 dt_node_list_free(&dnp->dn_probes);
635 free(dnp->dn_provname);
636 dnp->dn_provname = NULL;
637 break;
638
639 case DT_NODE_PROG:
640 dt_node_list_free(&dnp->dn_list);
641 break;
642 }
643}
644
645void
646dt_node_attr_assign(dt_node_t *dnp, dtrace_attribute_t attr)
647{
648 if ((yypcb->pcb_cflags & DTRACE_C_EATTR) &&
649 (dt_attr_cmp(attr, yypcb->pcb_amin) < 0)) {
650 char a[DTRACE_ATTR2STR_MAX];
651 char s[BUFSIZ];
652
653 dnerror(dnp, D_ATTR_MIN, "attributes for %s (%s) are less than "
654 "predefined minimum\n", dt_node_name(dnp, s, sizeof (s)),
655 dtrace_attr2str(attr, a, sizeof (a)));
656 }
657
658 dnp->dn_attr = attr;
659}
660
661void
411 dt_node_attr_assign(dnp, dt_attr_min(lp->dn_attr, rp->dn_attr));
412}
413
414const char *
415dt_node_name(const dt_node_t *dnp, char *buf, size_t len)
416{
417 char n1[DT_TYPE_NAMELEN];
418 char n2[DT_TYPE_NAMELEN];
419
420 const char *prefix = "", *suffix = "";
421 const dtrace_syminfo_t *dts;
422 char *s;
423
424 switch (dnp->dn_kind) {
425 case DT_NODE_INT:
426 (void) snprintf(buf, len, "integer constant 0x%llx",
427 (u_longlong_t)dnp->dn_value);
428 break;
429 case DT_NODE_STRING:
430 s = strchr2esc(dnp->dn_string, strlen(dnp->dn_string));
431 (void) snprintf(buf, len, "string constant \"%s\"",
432 s != NULL ? s : dnp->dn_string);
433 free(s);
434 break;
435 case DT_NODE_IDENT:
436 (void) snprintf(buf, len, "identifier %s", dnp->dn_string);
437 break;
438 case DT_NODE_VAR:
439 case DT_NODE_FUNC:
440 case DT_NODE_AGG:
441 case DT_NODE_INLINE:
442 switch (dnp->dn_ident->di_kind) {
443 case DT_IDENT_FUNC:
444 case DT_IDENT_AGGFUNC:
445 case DT_IDENT_ACTFUNC:
446 suffix = "( )";
447 break;
448 case DT_IDENT_AGG:
449 prefix = "@";
450 break;
451 }
452 (void) snprintf(buf, len, "%s %s%s%s",
453 dt_idkind_name(dnp->dn_ident->di_kind),
454 prefix, dnp->dn_ident->di_name, suffix);
455 break;
456 case DT_NODE_SYM:
457 dts = dnp->dn_ident->di_data;
458 (void) snprintf(buf, len, "symbol %s`%s",
459 dts->dts_object, dts->dts_name);
460 break;
461 case DT_NODE_TYPE:
462 (void) snprintf(buf, len, "type %s",
463 dt_node_type_name(dnp, n1, sizeof (n1)));
464 break;
465 case DT_NODE_OP1:
466 case DT_NODE_OP2:
467 case DT_NODE_OP3:
468 (void) snprintf(buf, len, "operator %s", opstr(dnp->dn_op));
469 break;
470 case DT_NODE_DEXPR:
471 case DT_NODE_DFUNC:
472 if (dnp->dn_expr)
473 return (dt_node_name(dnp->dn_expr, buf, len));
474 (void) snprintf(buf, len, "%s", "statement");
475 break;
476 case DT_NODE_PDESC:
477 if (dnp->dn_desc->dtpd_id == 0) {
478 (void) snprintf(buf, len,
479 "probe description %s:%s:%s:%s",
480 dnp->dn_desc->dtpd_provider, dnp->dn_desc->dtpd_mod,
481 dnp->dn_desc->dtpd_func, dnp->dn_desc->dtpd_name);
482 } else {
483 (void) snprintf(buf, len, "probe description %u",
484 dnp->dn_desc->dtpd_id);
485 }
486 break;
487 case DT_NODE_CLAUSE:
488 (void) snprintf(buf, len, "%s", "clause");
489 break;
490 case DT_NODE_MEMBER:
491 (void) snprintf(buf, len, "member %s", dnp->dn_membname);
492 break;
493 case DT_NODE_XLATOR:
494 (void) snprintf(buf, len, "translator <%s> (%s)",
495 dt_type_name(dnp->dn_xlator->dx_dst_ctfp,
496 dnp->dn_xlator->dx_dst_type, n1, sizeof (n1)),
497 dt_type_name(dnp->dn_xlator->dx_src_ctfp,
498 dnp->dn_xlator->dx_src_type, n2, sizeof (n2)));
499 break;
500 case DT_NODE_PROG:
501 (void) snprintf(buf, len, "%s", "program");
502 break;
503 default:
504 (void) snprintf(buf, len, "node <%u>", dnp->dn_kind);
505 break;
506 }
507
508 return (buf);
509}
510
511/*
512 * dt_node_xalloc() can be used to create new parse nodes from any libdtrace
513 * caller. The caller is responsible for assigning dn_link appropriately.
514 */
515dt_node_t *
516dt_node_xalloc(dtrace_hdl_t *dtp, int kind)
517{
518 dt_node_t *dnp = dt_alloc(dtp, sizeof (dt_node_t));
519
520 if (dnp == NULL)
521 return (NULL);
522
523 dnp->dn_ctfp = NULL;
524 dnp->dn_type = CTF_ERR;
525 dnp->dn_kind = (uchar_t)kind;
526 dnp->dn_flags = 0;
527 dnp->dn_op = 0;
528 dnp->dn_line = -1;
529 dnp->dn_reg = -1;
530 dnp->dn_attr = _dtrace_defattr;
531 dnp->dn_list = NULL;
532 dnp->dn_link = NULL;
533 bzero(&dnp->dn_u, sizeof (dnp->dn_u));
534
535 return (dnp);
536}
537
538/*
539 * dt_node_alloc() is used to create new parse nodes from the parser. It
540 * assigns the node location based on the current lexer line number and places
541 * the new node on the default allocation list. If allocation fails, we
542 * automatically longjmp the caller back to the enclosing compilation call.
543 */
544static dt_node_t *
545dt_node_alloc(int kind)
546{
547 dt_node_t *dnp = dt_node_xalloc(yypcb->pcb_hdl, kind);
548
549 if (dnp == NULL)
550 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
551
552 dnp->dn_line = yylineno;
553 dnp->dn_link = yypcb->pcb_list;
554 yypcb->pcb_list = dnp;
555
556 return (dnp);
557}
558
559void
560dt_node_free(dt_node_t *dnp)
561{
562 uchar_t kind = dnp->dn_kind;
563
564 dnp->dn_kind = DT_NODE_FREE;
565
566 switch (kind) {
567 case DT_NODE_STRING:
568 case DT_NODE_IDENT:
569 case DT_NODE_TYPE:
570 free(dnp->dn_string);
571 dnp->dn_string = NULL;
572 break;
573
574 case DT_NODE_VAR:
575 case DT_NODE_FUNC:
576 case DT_NODE_PROBE:
577 if (dnp->dn_ident != NULL) {
578 if (dnp->dn_ident->di_flags & DT_IDFLG_ORPHAN)
579 dt_ident_destroy(dnp->dn_ident);
580 dnp->dn_ident = NULL;
581 }
582 dt_node_list_free(&dnp->dn_args);
583 break;
584
585 case DT_NODE_OP1:
586 if (dnp->dn_child != NULL) {
587 dt_node_free(dnp->dn_child);
588 dnp->dn_child = NULL;
589 }
590 break;
591
592 case DT_NODE_OP3:
593 if (dnp->dn_expr != NULL) {
594 dt_node_free(dnp->dn_expr);
595 dnp->dn_expr = NULL;
596 }
597 /*FALLTHRU*/
598 case DT_NODE_OP2:
599 if (dnp->dn_left != NULL) {
600 dt_node_free(dnp->dn_left);
601 dnp->dn_left = NULL;
602 }
603 if (dnp->dn_right != NULL) {
604 dt_node_free(dnp->dn_right);
605 dnp->dn_right = NULL;
606 }
607 break;
608
609 case DT_NODE_DEXPR:
610 case DT_NODE_DFUNC:
611 if (dnp->dn_expr != NULL) {
612 dt_node_free(dnp->dn_expr);
613 dnp->dn_expr = NULL;
614 }
615 break;
616
617 case DT_NODE_AGG:
618 if (dnp->dn_aggfun != NULL) {
619 dt_node_free(dnp->dn_aggfun);
620 dnp->dn_aggfun = NULL;
621 }
622 dt_node_list_free(&dnp->dn_aggtup);
623 break;
624
625 case DT_NODE_PDESC:
626 free(dnp->dn_spec);
627 dnp->dn_spec = NULL;
628 free(dnp->dn_desc);
629 dnp->dn_desc = NULL;
630 break;
631
632 case DT_NODE_CLAUSE:
633 if (dnp->dn_pred != NULL)
634 dt_node_free(dnp->dn_pred);
635 if (dnp->dn_locals != NULL)
636 dt_idhash_destroy(dnp->dn_locals);
637 dt_node_list_free(&dnp->dn_pdescs);
638 dt_node_list_free(&dnp->dn_acts);
639 break;
640
641 case DT_NODE_MEMBER:
642 free(dnp->dn_membname);
643 dnp->dn_membname = NULL;
644 if (dnp->dn_membexpr != NULL) {
645 dt_node_free(dnp->dn_membexpr);
646 dnp->dn_membexpr = NULL;
647 }
648 break;
649
650 case DT_NODE_PROVIDER:
651 dt_node_list_free(&dnp->dn_probes);
652 free(dnp->dn_provname);
653 dnp->dn_provname = NULL;
654 break;
655
656 case DT_NODE_PROG:
657 dt_node_list_free(&dnp->dn_list);
658 break;
659 }
660}
661
662void
663dt_node_attr_assign(dt_node_t *dnp, dtrace_attribute_t attr)
664{
665 if ((yypcb->pcb_cflags & DTRACE_C_EATTR) &&
666 (dt_attr_cmp(attr, yypcb->pcb_amin) < 0)) {
667 char a[DTRACE_ATTR2STR_MAX];
668 char s[BUFSIZ];
669
670 dnerror(dnp, D_ATTR_MIN, "attributes for %s (%s) are less than "
671 "predefined minimum\n", dt_node_name(dnp, s, sizeof (s)),
672 dtrace_attr2str(attr, a, sizeof (a)));
673 }
674
675 dnp->dn_attr = attr;
676}
677
678void
662dt_node_type_assign(dt_node_t *dnp, ctf_file_t *fp, ctf_id_t type)
679dt_node_type_assign(dt_node_t *dnp, ctf_file_t *fp, ctf_id_t type,
680 boolean_t user)
663{
664 ctf_id_t base = ctf_type_resolve(fp, type);
665 uint_t kind = ctf_type_kind(fp, base);
666 ctf_encoding_t e;
667
668 dnp->dn_flags &=
669 ~(DT_NF_SIGNED | DT_NF_REF | DT_NF_BITFIELD | DT_NF_USERLAND);
670
671 if (kind == CTF_K_INTEGER && ctf_type_encoding(fp, base, &e) == 0) {
672 size_t size = e.cte_bits / NBBY;
673
674 if (size > 8 || (e.cte_bits % NBBY) != 0 || (size & (size - 1)))
675 dnp->dn_flags |= DT_NF_BITFIELD;
676
677 if (e.cte_format & CTF_INT_SIGNED)
678 dnp->dn_flags |= DT_NF_SIGNED;
679 }
680
681 if (kind == CTF_K_FLOAT && ctf_type_encoding(fp, base, &e) == 0) {
682 if (e.cte_bits / NBBY > sizeof (uint64_t))
683 dnp->dn_flags |= DT_NF_REF;
684 }
685
686 if (kind == CTF_K_STRUCT || kind == CTF_K_UNION ||
687 kind == CTF_K_FORWARD ||
688 kind == CTF_K_ARRAY || kind == CTF_K_FUNCTION)
689 dnp->dn_flags |= DT_NF_REF;
690 else if (yypcb != NULL && fp == DT_DYN_CTFP(yypcb->pcb_hdl) &&
691 type == DT_DYN_TYPE(yypcb->pcb_hdl))
692 dnp->dn_flags |= DT_NF_REF;
693
681{
682 ctf_id_t base = ctf_type_resolve(fp, type);
683 uint_t kind = ctf_type_kind(fp, base);
684 ctf_encoding_t e;
685
686 dnp->dn_flags &=
687 ~(DT_NF_SIGNED | DT_NF_REF | DT_NF_BITFIELD | DT_NF_USERLAND);
688
689 if (kind == CTF_K_INTEGER && ctf_type_encoding(fp, base, &e) == 0) {
690 size_t size = e.cte_bits / NBBY;
691
692 if (size > 8 || (e.cte_bits % NBBY) != 0 || (size & (size - 1)))
693 dnp->dn_flags |= DT_NF_BITFIELD;
694
695 if (e.cte_format & CTF_INT_SIGNED)
696 dnp->dn_flags |= DT_NF_SIGNED;
697 }
698
699 if (kind == CTF_K_FLOAT && ctf_type_encoding(fp, base, &e) == 0) {
700 if (e.cte_bits / NBBY > sizeof (uint64_t))
701 dnp->dn_flags |= DT_NF_REF;
702 }
703
704 if (kind == CTF_K_STRUCT || kind == CTF_K_UNION ||
705 kind == CTF_K_FORWARD ||
706 kind == CTF_K_ARRAY || kind == CTF_K_FUNCTION)
707 dnp->dn_flags |= DT_NF_REF;
708 else if (yypcb != NULL && fp == DT_DYN_CTFP(yypcb->pcb_hdl) &&
709 type == DT_DYN_TYPE(yypcb->pcb_hdl))
710 dnp->dn_flags |= DT_NF_REF;
711
712 if (user)
713 dnp->dn_flags |= DT_NF_USERLAND;
714
694 dnp->dn_flags |= DT_NF_COOKED;
695 dnp->dn_ctfp = fp;
696 dnp->dn_type = type;
697}
698
699void
700dt_node_type_propagate(const dt_node_t *src, dt_node_t *dst)
701{
702 assert(src->dn_flags & DT_NF_COOKED);
703 dst->dn_flags = src->dn_flags & ~DT_NF_LVALUE;
704 dst->dn_ctfp = src->dn_ctfp;
705 dst->dn_type = src->dn_type;
706}
707
708const char *
709dt_node_type_name(const dt_node_t *dnp, char *buf, size_t len)
710{
711 if (dt_node_is_dynamic(dnp) && dnp->dn_ident != NULL) {
712 (void) snprintf(buf, len, "%s",
713 dt_idkind_name(dt_ident_resolve(dnp->dn_ident)->di_kind));
714 return (buf);
715 }
716
717 if (dnp->dn_flags & DT_NF_USERLAND) {
718 size_t n = snprintf(buf, len, "userland ");
719 len = len > n ? len - n : 0;
720 (void) dt_type_name(dnp->dn_ctfp, dnp->dn_type, buf + n, len);
721 return (buf);
722 }
723
724 return (dt_type_name(dnp->dn_ctfp, dnp->dn_type, buf, len));
725}
726
727size_t
728dt_node_type_size(const dt_node_t *dnp)
729{
730 ctf_id_t base;
715 dnp->dn_flags |= DT_NF_COOKED;
716 dnp->dn_ctfp = fp;
717 dnp->dn_type = type;
718}
719
720void
721dt_node_type_propagate(const dt_node_t *src, dt_node_t *dst)
722{
723 assert(src->dn_flags & DT_NF_COOKED);
724 dst->dn_flags = src->dn_flags & ~DT_NF_LVALUE;
725 dst->dn_ctfp = src->dn_ctfp;
726 dst->dn_type = src->dn_type;
727}
728
729const char *
730dt_node_type_name(const dt_node_t *dnp, char *buf, size_t len)
731{
732 if (dt_node_is_dynamic(dnp) && dnp->dn_ident != NULL) {
733 (void) snprintf(buf, len, "%s",
734 dt_idkind_name(dt_ident_resolve(dnp->dn_ident)->di_kind));
735 return (buf);
736 }
737
738 if (dnp->dn_flags & DT_NF_USERLAND) {
739 size_t n = snprintf(buf, len, "userland ");
740 len = len > n ? len - n : 0;
741 (void) dt_type_name(dnp->dn_ctfp, dnp->dn_type, buf + n, len);
742 return (buf);
743 }
744
745 return (dt_type_name(dnp->dn_ctfp, dnp->dn_type, buf, len));
746}
747
748size_t
749dt_node_type_size(const dt_node_t *dnp)
750{
751 ctf_id_t base;
752 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
731
732 if (dnp->dn_kind == DT_NODE_STRING)
733 return (strlen(dnp->dn_string) + 1);
734
735 if (dt_node_is_dynamic(dnp) && dnp->dn_ident != NULL)
736 return (dt_ident_size(dnp->dn_ident));
737
738 base = ctf_type_resolve(dnp->dn_ctfp, dnp->dn_type);
739
740 if (ctf_type_kind(dnp->dn_ctfp, base) == CTF_K_FORWARD)
741 return (0);
742
753
754 if (dnp->dn_kind == DT_NODE_STRING)
755 return (strlen(dnp->dn_string) + 1);
756
757 if (dt_node_is_dynamic(dnp) && dnp->dn_ident != NULL)
758 return (dt_ident_size(dnp->dn_ident));
759
760 base = ctf_type_resolve(dnp->dn_ctfp, dnp->dn_type);
761
762 if (ctf_type_kind(dnp->dn_ctfp, base) == CTF_K_FORWARD)
763 return (0);
764
765 /*
766 * Here we have a 32-bit user pointer that is being used with a 64-bit
767 * kernel. When we're using it and its tagged as a userland reference --
768 * then we need to keep it as a 32-bit pointer. However, if we are
769 * referring to it as a kernel address, eg. being used after a copyin()
770 * then we need to make sure that we actually return the kernel's size
771 * of a pointer, 8 bytes.
772 */
773 if (ctf_type_kind(dnp->dn_ctfp, base) == CTF_K_POINTER &&
774 ctf_getmodel(dnp->dn_ctfp) == CTF_MODEL_ILP32 &&
775 !(dnp->dn_flags & DT_NF_USERLAND) &&
776 dtp->dt_conf.dtc_ctfmodel == CTF_MODEL_LP64)
777 return (8);
778
743 return (ctf_type_size(dnp->dn_ctfp, dnp->dn_type));
744}
745
746/*
747 * Determine if the specified parse tree node references an identifier of the
748 * specified kind, and if so return a pointer to it; otherwise return NULL.
749 * This function resolves the identifier itself, following through any inlines.
750 */
751dt_ident_t *
752dt_node_resolve(const dt_node_t *dnp, uint_t idkind)
753{
754 dt_ident_t *idp;
755
756 switch (dnp->dn_kind) {
757 case DT_NODE_VAR:
758 case DT_NODE_SYM:
759 case DT_NODE_FUNC:
760 case DT_NODE_AGG:
761 case DT_NODE_INLINE:
762 case DT_NODE_PROBE:
763 idp = dt_ident_resolve(dnp->dn_ident);
764 return (idp->di_kind == idkind ? idp : NULL);
765 }
766
767 if (dt_node_is_dynamic(dnp)) {
768 idp = dt_ident_resolve(dnp->dn_ident);
769 return (idp->di_kind == idkind ? idp : NULL);
770 }
771
772 return (NULL);
773}
774
775size_t
776dt_node_sizeof(const dt_node_t *dnp)
777{
778 dtrace_syminfo_t *sip;
779 GElf_Sym sym;
780 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
781
782 /*
783 * The size of the node as used for the sizeof() operator depends on
784 * the kind of the node. If the node is a SYM, the size is obtained
785 * from the symbol table; if it is not a SYM, the size is determined
786 * from the node's type. This is slightly different from C's sizeof()
787 * operator in that (for example) when applied to a function, sizeof()
788 * will evaluate to the length of the function rather than the size of
789 * the function type.
790 */
791 if (dnp->dn_kind != DT_NODE_SYM)
792 return (dt_node_type_size(dnp));
793
794 sip = dnp->dn_ident->di_data;
795
796 if (dtrace_lookup_by_name(dtp, sip->dts_object,
797 sip->dts_name, &sym, NULL) == -1)
798 return (0);
799
800 return (sym.st_size);
801}
802
803int
804dt_node_is_integer(const dt_node_t *dnp)
805{
806 ctf_file_t *fp = dnp->dn_ctfp;
807 ctf_encoding_t e;
808 ctf_id_t type;
809 uint_t kind;
810
811 assert(dnp->dn_flags & DT_NF_COOKED);
812
813 type = ctf_type_resolve(fp, dnp->dn_type);
814 kind = ctf_type_kind(fp, type);
815
816 if (kind == CTF_K_INTEGER &&
817 ctf_type_encoding(fp, type, &e) == 0 && IS_VOID(e))
818 return (0); /* void integer */
819
820 return (kind == CTF_K_INTEGER || kind == CTF_K_ENUM);
821}
822
823int
824dt_node_is_float(const dt_node_t *dnp)
825{
826 ctf_file_t *fp = dnp->dn_ctfp;
827 ctf_encoding_t e;
828 ctf_id_t type;
829 uint_t kind;
830
831 assert(dnp->dn_flags & DT_NF_COOKED);
832
833 type = ctf_type_resolve(fp, dnp->dn_type);
834 kind = ctf_type_kind(fp, type);
835
836 return (kind == CTF_K_FLOAT &&
837 ctf_type_encoding(dnp->dn_ctfp, type, &e) == 0 && (
838 e.cte_format == CTF_FP_SINGLE || e.cte_format == CTF_FP_DOUBLE ||
839 e.cte_format == CTF_FP_LDOUBLE));
840}
841
842int
843dt_node_is_scalar(const dt_node_t *dnp)
844{
845 ctf_file_t *fp = dnp->dn_ctfp;
846 ctf_encoding_t e;
847 ctf_id_t type;
848 uint_t kind;
849
850 assert(dnp->dn_flags & DT_NF_COOKED);
851
852 type = ctf_type_resolve(fp, dnp->dn_type);
853 kind = ctf_type_kind(fp, type);
854
855 if (kind == CTF_K_INTEGER &&
856 ctf_type_encoding(fp, type, &e) == 0 && IS_VOID(e))
857 return (0); /* void cannot be used as a scalar */
858
859 return (kind == CTF_K_INTEGER || kind == CTF_K_ENUM ||
860 kind == CTF_K_POINTER);
861}
862
863int
864dt_node_is_arith(const dt_node_t *dnp)
865{
866 ctf_file_t *fp = dnp->dn_ctfp;
867 ctf_encoding_t e;
868 ctf_id_t type;
869 uint_t kind;
870
871 assert(dnp->dn_flags & DT_NF_COOKED);
872
873 type = ctf_type_resolve(fp, dnp->dn_type);
874 kind = ctf_type_kind(fp, type);
875
876 if (kind == CTF_K_INTEGER)
877 return (ctf_type_encoding(fp, type, &e) == 0 && !IS_VOID(e));
878 else
879 return (kind == CTF_K_ENUM);
880}
881
882int
883dt_node_is_vfptr(const dt_node_t *dnp)
884{
885 ctf_file_t *fp = dnp->dn_ctfp;
886 ctf_encoding_t e;
887 ctf_id_t type;
888 uint_t kind;
889
890 assert(dnp->dn_flags & DT_NF_COOKED);
891
892 type = ctf_type_resolve(fp, dnp->dn_type);
893 if (ctf_type_kind(fp, type) != CTF_K_POINTER)
894 return (0); /* type is not a pointer */
895
896 type = ctf_type_resolve(fp, ctf_type_reference(fp, type));
897 kind = ctf_type_kind(fp, type);
898
899 return (kind == CTF_K_FUNCTION || (kind == CTF_K_INTEGER &&
900 ctf_type_encoding(fp, type, &e) == 0 && IS_VOID(e)));
901}
902
903int
904dt_node_is_dynamic(const dt_node_t *dnp)
905{
906 if (dnp->dn_kind == DT_NODE_VAR &&
907 (dnp->dn_ident->di_flags & DT_IDFLG_INLINE)) {
908 const dt_idnode_t *inp = dnp->dn_ident->di_iarg;
909 return (inp->din_root ? dt_node_is_dynamic(inp->din_root) : 0);
910 }
911
912 return (dnp->dn_ctfp == DT_DYN_CTFP(yypcb->pcb_hdl) &&
913 dnp->dn_type == DT_DYN_TYPE(yypcb->pcb_hdl));
914}
915
916int
917dt_node_is_string(const dt_node_t *dnp)
918{
919 return (dnp->dn_ctfp == DT_STR_CTFP(yypcb->pcb_hdl) &&
920 dnp->dn_type == DT_STR_TYPE(yypcb->pcb_hdl));
921}
922
923int
924dt_node_is_stack(const dt_node_t *dnp)
925{
926 return (dnp->dn_ctfp == DT_STACK_CTFP(yypcb->pcb_hdl) &&
927 dnp->dn_type == DT_STACK_TYPE(yypcb->pcb_hdl));
928}
929
930int
931dt_node_is_symaddr(const dt_node_t *dnp)
932{
933 return (dnp->dn_ctfp == DT_SYMADDR_CTFP(yypcb->pcb_hdl) &&
934 dnp->dn_type == DT_SYMADDR_TYPE(yypcb->pcb_hdl));
935}
936
937int
938dt_node_is_usymaddr(const dt_node_t *dnp)
939{
940 return (dnp->dn_ctfp == DT_USYMADDR_CTFP(yypcb->pcb_hdl) &&
941 dnp->dn_type == DT_USYMADDR_TYPE(yypcb->pcb_hdl));
942}
943
944int
945dt_node_is_strcompat(const dt_node_t *dnp)
946{
947 ctf_file_t *fp = dnp->dn_ctfp;
948 ctf_encoding_t e;
949 ctf_arinfo_t r;
950 ctf_id_t base;
951 uint_t kind;
952
953 assert(dnp->dn_flags & DT_NF_COOKED);
954
955 base = ctf_type_resolve(fp, dnp->dn_type);
956 kind = ctf_type_kind(fp, base);
957
958 if (kind == CTF_K_POINTER &&
959 (base = ctf_type_reference(fp, base)) != CTF_ERR &&
960 (base = ctf_type_resolve(fp, base)) != CTF_ERR &&
961 ctf_type_encoding(fp, base, &e) == 0 && IS_CHAR(e))
962 return (1); /* promote char pointer to string */
963
964 if (kind == CTF_K_ARRAY && ctf_array_info(fp, base, &r) == 0 &&
965 (base = ctf_type_resolve(fp, r.ctr_contents)) != CTF_ERR &&
966 ctf_type_encoding(fp, base, &e) == 0 && IS_CHAR(e))
967 return (1); /* promote char array to string */
968
969 return (0);
970}
971
972int
973dt_node_is_pointer(const dt_node_t *dnp)
974{
975 ctf_file_t *fp = dnp->dn_ctfp;
976 uint_t kind;
977
978 assert(dnp->dn_flags & DT_NF_COOKED);
979
980 if (dt_node_is_string(dnp))
981 return (0); /* string are pass-by-ref but act like structs */
982
983 kind = ctf_type_kind(fp, ctf_type_resolve(fp, dnp->dn_type));
984 return (kind == CTF_K_POINTER || kind == CTF_K_ARRAY);
985}
986
987int
988dt_node_is_void(const dt_node_t *dnp)
989{
990 ctf_file_t *fp = dnp->dn_ctfp;
991 ctf_encoding_t e;
992 ctf_id_t type;
993
994 if (dt_node_is_dynamic(dnp))
995 return (0); /* <DYN> is an alias for void but not the same */
996
997 if (dt_node_is_stack(dnp))
998 return (0);
999
1000 if (dt_node_is_symaddr(dnp) || dt_node_is_usymaddr(dnp))
1001 return (0);
1002
1003 type = ctf_type_resolve(fp, dnp->dn_type);
1004
1005 return (ctf_type_kind(fp, type) == CTF_K_INTEGER &&
1006 ctf_type_encoding(fp, type, &e) == 0 && IS_VOID(e));
1007}
1008
1009int
1010dt_node_is_ptrcompat(const dt_node_t *lp, const dt_node_t *rp,
1011 ctf_file_t **fpp, ctf_id_t *tp)
1012{
1013 ctf_file_t *lfp = lp->dn_ctfp;
1014 ctf_file_t *rfp = rp->dn_ctfp;
1015
1016 ctf_id_t lbase = CTF_ERR, rbase = CTF_ERR;
1017 ctf_id_t lref = CTF_ERR, rref = CTF_ERR;
1018
1019 int lp_is_void, rp_is_void, lp_is_int, rp_is_int, compat;
1020 uint_t lkind, rkind;
1021 ctf_encoding_t e;
1022 ctf_arinfo_t r;
1023
1024 assert(lp->dn_flags & DT_NF_COOKED);
1025 assert(rp->dn_flags & DT_NF_COOKED);
1026
1027 if (dt_node_is_dynamic(lp) || dt_node_is_dynamic(rp))
1028 return (0); /* fail if either node is a dynamic variable */
1029
1030 lp_is_int = dt_node_is_integer(lp);
1031 rp_is_int = dt_node_is_integer(rp);
1032
1033 if (lp_is_int && rp_is_int)
1034 return (0); /* fail if both nodes are integers */
1035
1036 if (lp_is_int && (lp->dn_kind != DT_NODE_INT || lp->dn_value != 0))
1037 return (0); /* fail if lp is an integer that isn't 0 constant */
1038
1039 if (rp_is_int && (rp->dn_kind != DT_NODE_INT || rp->dn_value != 0))
1040 return (0); /* fail if rp is an integer that isn't 0 constant */
1041
1042 if ((lp_is_int == 0 && rp_is_int == 0) && (
1043 (lp->dn_flags & DT_NF_USERLAND) ^ (rp->dn_flags & DT_NF_USERLAND)))
1044 return (0); /* fail if only one pointer is a userland address */
1045
1046 /*
1047 * Resolve the left-hand and right-hand types to their base type, and
1048 * then resolve the referenced type as well (assuming the base type
1049 * is CTF_K_POINTER or CTF_K_ARRAY). Otherwise [lr]ref = CTF_ERR.
1050 */
1051 if (!lp_is_int) {
1052 lbase = ctf_type_resolve(lfp, lp->dn_type);
1053 lkind = ctf_type_kind(lfp, lbase);
1054
1055 if (lkind == CTF_K_POINTER) {
1056 lref = ctf_type_resolve(lfp,
1057 ctf_type_reference(lfp, lbase));
1058 } else if (lkind == CTF_K_ARRAY &&
1059 ctf_array_info(lfp, lbase, &r) == 0) {
1060 lref = ctf_type_resolve(lfp, r.ctr_contents);
1061 }
1062 }
1063
1064 if (!rp_is_int) {
1065 rbase = ctf_type_resolve(rfp, rp->dn_type);
1066 rkind = ctf_type_kind(rfp, rbase);
1067
1068 if (rkind == CTF_K_POINTER) {
1069 rref = ctf_type_resolve(rfp,
1070 ctf_type_reference(rfp, rbase));
1071 } else if (rkind == CTF_K_ARRAY &&
1072 ctf_array_info(rfp, rbase, &r) == 0) {
1073 rref = ctf_type_resolve(rfp, r.ctr_contents);
1074 }
1075 }
1076
1077 /*
1078 * We know that one or the other type may still be a zero-valued
1079 * integer constant. To simplify the code below, set the integer
1080 * type variables equal to the non-integer types and proceed.
1081 */
1082 if (lp_is_int) {
1083 lbase = rbase;
1084 lkind = rkind;
1085 lref = rref;
1086 lfp = rfp;
1087 } else if (rp_is_int) {
1088 rbase = lbase;
1089 rkind = lkind;
1090 rref = lref;
1091 rfp = lfp;
1092 }
1093
1094 lp_is_void = ctf_type_encoding(lfp, lref, &e) == 0 && IS_VOID(e);
1095 rp_is_void = ctf_type_encoding(rfp, rref, &e) == 0 && IS_VOID(e);
1096
1097 /*
1098 * The types are compatible if both are pointers to the same type, or
1099 * if either pointer is a void pointer. If they are compatible, set
1100 * tp to point to the more specific pointer type and return it.
1101 */
1102 compat = (lkind == CTF_K_POINTER || lkind == CTF_K_ARRAY) &&
1103 (rkind == CTF_K_POINTER || rkind == CTF_K_ARRAY) &&
1104 (lp_is_void || rp_is_void || ctf_type_compat(lfp, lref, rfp, rref));
1105
1106 if (compat) {
1107 if (fpp != NULL)
1108 *fpp = rp_is_void ? lfp : rfp;
1109 if (tp != NULL)
1110 *tp = rp_is_void ? lbase : rbase;
1111 }
1112
1113 return (compat);
1114}
1115
1116/*
1117 * The rules for checking argument types against parameter types are described
1118 * in the ANSI-C spec (see K&R[A7.3.2] and K&R[A7.17]). We use the same rule
1119 * set to determine whether associative array arguments match the prototype.
1120 */
1121int
1122dt_node_is_argcompat(const dt_node_t *lp, const dt_node_t *rp)
1123{
1124 ctf_file_t *lfp = lp->dn_ctfp;
1125 ctf_file_t *rfp = rp->dn_ctfp;
1126
1127 assert(lp->dn_flags & DT_NF_COOKED);
1128 assert(rp->dn_flags & DT_NF_COOKED);
1129
1130 if (dt_node_is_integer(lp) && dt_node_is_integer(rp))
1131 return (1); /* integer types are compatible */
1132
1133 if (dt_node_is_strcompat(lp) && dt_node_is_strcompat(rp))
1134 return (1); /* string types are compatible */
1135
1136 if (dt_node_is_stack(lp) && dt_node_is_stack(rp))
1137 return (1); /* stack types are compatible */
1138
1139 if (dt_node_is_symaddr(lp) && dt_node_is_symaddr(rp))
1140 return (1); /* symaddr types are compatible */
1141
1142 if (dt_node_is_usymaddr(lp) && dt_node_is_usymaddr(rp))
1143 return (1); /* usymaddr types are compatible */
1144
1145 switch (ctf_type_kind(lfp, ctf_type_resolve(lfp, lp->dn_type))) {
1146 case CTF_K_FUNCTION:
1147 case CTF_K_STRUCT:
1148 case CTF_K_UNION:
1149 return (ctf_type_compat(lfp, lp->dn_type, rfp, rp->dn_type));
1150 default:
1151 return (dt_node_is_ptrcompat(lp, rp, NULL, NULL));
1152 }
1153}
1154
1155/*
1156 * We provide dt_node_is_posconst() as a convenience routine for callers who
1157 * wish to verify that an argument is a positive non-zero integer constant.
1158 */
1159int
1160dt_node_is_posconst(const dt_node_t *dnp)
1161{
1162 return (dnp->dn_kind == DT_NODE_INT && dnp->dn_value != 0 && (
1163 (dnp->dn_flags & DT_NF_SIGNED) == 0 || (int64_t)dnp->dn_value > 0));
1164}
1165
1166int
1167dt_node_is_actfunc(const dt_node_t *dnp)
1168{
1169 return (dnp->dn_kind == DT_NODE_FUNC &&
1170 dnp->dn_ident->di_kind == DT_IDENT_ACTFUNC);
1171}
1172
1173/*
1174 * The original rules for integer constant typing are described in K&R[A2.5.1].
1175 * However, since we support long long, we instead use the rules from ISO C99
1176 * clause 6.4.4.1 since that is where long longs are formally described. The
1177 * rules require us to know whether the constant was specified in decimal or
1178 * in octal or hex, which we do by looking at our lexer's 'yyintdecimal' flag.
1179 * The type of an integer constant is the first of the corresponding list in
1180 * which its value can be represented:
1181 *
1182 * unsuffixed decimal: int, long, long long
1183 * unsuffixed oct/hex: int, unsigned int, long, unsigned long,
1184 * long long, unsigned long long
1185 * suffix [uU]: unsigned int, unsigned long, unsigned long long
1186 * suffix [lL] decimal: long, long long
1187 * suffix [lL] oct/hex: long, unsigned long, long long, unsigned long long
1188 * suffix [uU][Ll]: unsigned long, unsigned long long
1189 * suffix ll/LL decimal: long long
1190 * suffix ll/LL oct/hex: long long, unsigned long long
1191 * suffix [uU][ll/LL]: unsigned long long
1192 *
1193 * Given that our lexer has already validated the suffixes by regexp matching,
1194 * there is an obvious way to concisely encode these rules: construct an array
1195 * of the types in the order int, unsigned int, long, unsigned long, long long,
1196 * unsigned long long. Compute an integer array starting index based on the
1197 * suffix (e.g. none = 0, u = 1, ull = 5), and compute an increment based on
1198 * the specifier (dec/oct/hex) and suffix (u). Then iterate from the starting
1199 * index to the end, advancing using the increment, and searching until we
1200 * find a limit that matches or we run out of choices (overflow). To make it
1201 * even faster, we precompute the table of type information in dtrace_open().
1202 */
1203dt_node_t *
1204dt_node_int(uintmax_t value)
1205{
1206 dt_node_t *dnp = dt_node_alloc(DT_NODE_INT);
1207 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
1208
1209 int n = (yyintdecimal | (yyintsuffix[0] == 'u')) + 1;
1210 int i = 0;
1211
1212 const char *p;
1213 char c;
1214
1215 dnp->dn_op = DT_TOK_INT;
1216 dnp->dn_value = value;
1217
1218 for (p = yyintsuffix; (c = *p) != '\0'; p++) {
1219 if (c == 'U' || c == 'u')
1220 i += 1;
1221 else if (c == 'L' || c == 'l')
1222 i += 2;
1223 }
1224
1225 for (; i < sizeof (dtp->dt_ints) / sizeof (dtp->dt_ints[0]); i += n) {
1226 if (value <= dtp->dt_ints[i].did_limit) {
1227 dt_node_type_assign(dnp,
1228 dtp->dt_ints[i].did_ctfp,
779 return (ctf_type_size(dnp->dn_ctfp, dnp->dn_type));
780}
781
782/*
783 * Determine if the specified parse tree node references an identifier of the
784 * specified kind, and if so return a pointer to it; otherwise return NULL.
785 * This function resolves the identifier itself, following through any inlines.
786 */
787dt_ident_t *
788dt_node_resolve(const dt_node_t *dnp, uint_t idkind)
789{
790 dt_ident_t *idp;
791
792 switch (dnp->dn_kind) {
793 case DT_NODE_VAR:
794 case DT_NODE_SYM:
795 case DT_NODE_FUNC:
796 case DT_NODE_AGG:
797 case DT_NODE_INLINE:
798 case DT_NODE_PROBE:
799 idp = dt_ident_resolve(dnp->dn_ident);
800 return (idp->di_kind == idkind ? idp : NULL);
801 }
802
803 if (dt_node_is_dynamic(dnp)) {
804 idp = dt_ident_resolve(dnp->dn_ident);
805 return (idp->di_kind == idkind ? idp : NULL);
806 }
807
808 return (NULL);
809}
810
811size_t
812dt_node_sizeof(const dt_node_t *dnp)
813{
814 dtrace_syminfo_t *sip;
815 GElf_Sym sym;
816 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
817
818 /*
819 * The size of the node as used for the sizeof() operator depends on
820 * the kind of the node. If the node is a SYM, the size is obtained
821 * from the symbol table; if it is not a SYM, the size is determined
822 * from the node's type. This is slightly different from C's sizeof()
823 * operator in that (for example) when applied to a function, sizeof()
824 * will evaluate to the length of the function rather than the size of
825 * the function type.
826 */
827 if (dnp->dn_kind != DT_NODE_SYM)
828 return (dt_node_type_size(dnp));
829
830 sip = dnp->dn_ident->di_data;
831
832 if (dtrace_lookup_by_name(dtp, sip->dts_object,
833 sip->dts_name, &sym, NULL) == -1)
834 return (0);
835
836 return (sym.st_size);
837}
838
839int
840dt_node_is_integer(const dt_node_t *dnp)
841{
842 ctf_file_t *fp = dnp->dn_ctfp;
843 ctf_encoding_t e;
844 ctf_id_t type;
845 uint_t kind;
846
847 assert(dnp->dn_flags & DT_NF_COOKED);
848
849 type = ctf_type_resolve(fp, dnp->dn_type);
850 kind = ctf_type_kind(fp, type);
851
852 if (kind == CTF_K_INTEGER &&
853 ctf_type_encoding(fp, type, &e) == 0 && IS_VOID(e))
854 return (0); /* void integer */
855
856 return (kind == CTF_K_INTEGER || kind == CTF_K_ENUM);
857}
858
859int
860dt_node_is_float(const dt_node_t *dnp)
861{
862 ctf_file_t *fp = dnp->dn_ctfp;
863 ctf_encoding_t e;
864 ctf_id_t type;
865 uint_t kind;
866
867 assert(dnp->dn_flags & DT_NF_COOKED);
868
869 type = ctf_type_resolve(fp, dnp->dn_type);
870 kind = ctf_type_kind(fp, type);
871
872 return (kind == CTF_K_FLOAT &&
873 ctf_type_encoding(dnp->dn_ctfp, type, &e) == 0 && (
874 e.cte_format == CTF_FP_SINGLE || e.cte_format == CTF_FP_DOUBLE ||
875 e.cte_format == CTF_FP_LDOUBLE));
876}
877
878int
879dt_node_is_scalar(const dt_node_t *dnp)
880{
881 ctf_file_t *fp = dnp->dn_ctfp;
882 ctf_encoding_t e;
883 ctf_id_t type;
884 uint_t kind;
885
886 assert(dnp->dn_flags & DT_NF_COOKED);
887
888 type = ctf_type_resolve(fp, dnp->dn_type);
889 kind = ctf_type_kind(fp, type);
890
891 if (kind == CTF_K_INTEGER &&
892 ctf_type_encoding(fp, type, &e) == 0 && IS_VOID(e))
893 return (0); /* void cannot be used as a scalar */
894
895 return (kind == CTF_K_INTEGER || kind == CTF_K_ENUM ||
896 kind == CTF_K_POINTER);
897}
898
899int
900dt_node_is_arith(const dt_node_t *dnp)
901{
902 ctf_file_t *fp = dnp->dn_ctfp;
903 ctf_encoding_t e;
904 ctf_id_t type;
905 uint_t kind;
906
907 assert(dnp->dn_flags & DT_NF_COOKED);
908
909 type = ctf_type_resolve(fp, dnp->dn_type);
910 kind = ctf_type_kind(fp, type);
911
912 if (kind == CTF_K_INTEGER)
913 return (ctf_type_encoding(fp, type, &e) == 0 && !IS_VOID(e));
914 else
915 return (kind == CTF_K_ENUM);
916}
917
918int
919dt_node_is_vfptr(const dt_node_t *dnp)
920{
921 ctf_file_t *fp = dnp->dn_ctfp;
922 ctf_encoding_t e;
923 ctf_id_t type;
924 uint_t kind;
925
926 assert(dnp->dn_flags & DT_NF_COOKED);
927
928 type = ctf_type_resolve(fp, dnp->dn_type);
929 if (ctf_type_kind(fp, type) != CTF_K_POINTER)
930 return (0); /* type is not a pointer */
931
932 type = ctf_type_resolve(fp, ctf_type_reference(fp, type));
933 kind = ctf_type_kind(fp, type);
934
935 return (kind == CTF_K_FUNCTION || (kind == CTF_K_INTEGER &&
936 ctf_type_encoding(fp, type, &e) == 0 && IS_VOID(e)));
937}
938
939int
940dt_node_is_dynamic(const dt_node_t *dnp)
941{
942 if (dnp->dn_kind == DT_NODE_VAR &&
943 (dnp->dn_ident->di_flags & DT_IDFLG_INLINE)) {
944 const dt_idnode_t *inp = dnp->dn_ident->di_iarg;
945 return (inp->din_root ? dt_node_is_dynamic(inp->din_root) : 0);
946 }
947
948 return (dnp->dn_ctfp == DT_DYN_CTFP(yypcb->pcb_hdl) &&
949 dnp->dn_type == DT_DYN_TYPE(yypcb->pcb_hdl));
950}
951
952int
953dt_node_is_string(const dt_node_t *dnp)
954{
955 return (dnp->dn_ctfp == DT_STR_CTFP(yypcb->pcb_hdl) &&
956 dnp->dn_type == DT_STR_TYPE(yypcb->pcb_hdl));
957}
958
959int
960dt_node_is_stack(const dt_node_t *dnp)
961{
962 return (dnp->dn_ctfp == DT_STACK_CTFP(yypcb->pcb_hdl) &&
963 dnp->dn_type == DT_STACK_TYPE(yypcb->pcb_hdl));
964}
965
966int
967dt_node_is_symaddr(const dt_node_t *dnp)
968{
969 return (dnp->dn_ctfp == DT_SYMADDR_CTFP(yypcb->pcb_hdl) &&
970 dnp->dn_type == DT_SYMADDR_TYPE(yypcb->pcb_hdl));
971}
972
973int
974dt_node_is_usymaddr(const dt_node_t *dnp)
975{
976 return (dnp->dn_ctfp == DT_USYMADDR_CTFP(yypcb->pcb_hdl) &&
977 dnp->dn_type == DT_USYMADDR_TYPE(yypcb->pcb_hdl));
978}
979
980int
981dt_node_is_strcompat(const dt_node_t *dnp)
982{
983 ctf_file_t *fp = dnp->dn_ctfp;
984 ctf_encoding_t e;
985 ctf_arinfo_t r;
986 ctf_id_t base;
987 uint_t kind;
988
989 assert(dnp->dn_flags & DT_NF_COOKED);
990
991 base = ctf_type_resolve(fp, dnp->dn_type);
992 kind = ctf_type_kind(fp, base);
993
994 if (kind == CTF_K_POINTER &&
995 (base = ctf_type_reference(fp, base)) != CTF_ERR &&
996 (base = ctf_type_resolve(fp, base)) != CTF_ERR &&
997 ctf_type_encoding(fp, base, &e) == 0 && IS_CHAR(e))
998 return (1); /* promote char pointer to string */
999
1000 if (kind == CTF_K_ARRAY && ctf_array_info(fp, base, &r) == 0 &&
1001 (base = ctf_type_resolve(fp, r.ctr_contents)) != CTF_ERR &&
1002 ctf_type_encoding(fp, base, &e) == 0 && IS_CHAR(e))
1003 return (1); /* promote char array to string */
1004
1005 return (0);
1006}
1007
1008int
1009dt_node_is_pointer(const dt_node_t *dnp)
1010{
1011 ctf_file_t *fp = dnp->dn_ctfp;
1012 uint_t kind;
1013
1014 assert(dnp->dn_flags & DT_NF_COOKED);
1015
1016 if (dt_node_is_string(dnp))
1017 return (0); /* string are pass-by-ref but act like structs */
1018
1019 kind = ctf_type_kind(fp, ctf_type_resolve(fp, dnp->dn_type));
1020 return (kind == CTF_K_POINTER || kind == CTF_K_ARRAY);
1021}
1022
1023int
1024dt_node_is_void(const dt_node_t *dnp)
1025{
1026 ctf_file_t *fp = dnp->dn_ctfp;
1027 ctf_encoding_t e;
1028 ctf_id_t type;
1029
1030 if (dt_node_is_dynamic(dnp))
1031 return (0); /* <DYN> is an alias for void but not the same */
1032
1033 if (dt_node_is_stack(dnp))
1034 return (0);
1035
1036 if (dt_node_is_symaddr(dnp) || dt_node_is_usymaddr(dnp))
1037 return (0);
1038
1039 type = ctf_type_resolve(fp, dnp->dn_type);
1040
1041 return (ctf_type_kind(fp, type) == CTF_K_INTEGER &&
1042 ctf_type_encoding(fp, type, &e) == 0 && IS_VOID(e));
1043}
1044
1045int
1046dt_node_is_ptrcompat(const dt_node_t *lp, const dt_node_t *rp,
1047 ctf_file_t **fpp, ctf_id_t *tp)
1048{
1049 ctf_file_t *lfp = lp->dn_ctfp;
1050 ctf_file_t *rfp = rp->dn_ctfp;
1051
1052 ctf_id_t lbase = CTF_ERR, rbase = CTF_ERR;
1053 ctf_id_t lref = CTF_ERR, rref = CTF_ERR;
1054
1055 int lp_is_void, rp_is_void, lp_is_int, rp_is_int, compat;
1056 uint_t lkind, rkind;
1057 ctf_encoding_t e;
1058 ctf_arinfo_t r;
1059
1060 assert(lp->dn_flags & DT_NF_COOKED);
1061 assert(rp->dn_flags & DT_NF_COOKED);
1062
1063 if (dt_node_is_dynamic(lp) || dt_node_is_dynamic(rp))
1064 return (0); /* fail if either node is a dynamic variable */
1065
1066 lp_is_int = dt_node_is_integer(lp);
1067 rp_is_int = dt_node_is_integer(rp);
1068
1069 if (lp_is_int && rp_is_int)
1070 return (0); /* fail if both nodes are integers */
1071
1072 if (lp_is_int && (lp->dn_kind != DT_NODE_INT || lp->dn_value != 0))
1073 return (0); /* fail if lp is an integer that isn't 0 constant */
1074
1075 if (rp_is_int && (rp->dn_kind != DT_NODE_INT || rp->dn_value != 0))
1076 return (0); /* fail if rp is an integer that isn't 0 constant */
1077
1078 if ((lp_is_int == 0 && rp_is_int == 0) && (
1079 (lp->dn_flags & DT_NF_USERLAND) ^ (rp->dn_flags & DT_NF_USERLAND)))
1080 return (0); /* fail if only one pointer is a userland address */
1081
1082 /*
1083 * Resolve the left-hand and right-hand types to their base type, and
1084 * then resolve the referenced type as well (assuming the base type
1085 * is CTF_K_POINTER or CTF_K_ARRAY). Otherwise [lr]ref = CTF_ERR.
1086 */
1087 if (!lp_is_int) {
1088 lbase = ctf_type_resolve(lfp, lp->dn_type);
1089 lkind = ctf_type_kind(lfp, lbase);
1090
1091 if (lkind == CTF_K_POINTER) {
1092 lref = ctf_type_resolve(lfp,
1093 ctf_type_reference(lfp, lbase));
1094 } else if (lkind == CTF_K_ARRAY &&
1095 ctf_array_info(lfp, lbase, &r) == 0) {
1096 lref = ctf_type_resolve(lfp, r.ctr_contents);
1097 }
1098 }
1099
1100 if (!rp_is_int) {
1101 rbase = ctf_type_resolve(rfp, rp->dn_type);
1102 rkind = ctf_type_kind(rfp, rbase);
1103
1104 if (rkind == CTF_K_POINTER) {
1105 rref = ctf_type_resolve(rfp,
1106 ctf_type_reference(rfp, rbase));
1107 } else if (rkind == CTF_K_ARRAY &&
1108 ctf_array_info(rfp, rbase, &r) == 0) {
1109 rref = ctf_type_resolve(rfp, r.ctr_contents);
1110 }
1111 }
1112
1113 /*
1114 * We know that one or the other type may still be a zero-valued
1115 * integer constant. To simplify the code below, set the integer
1116 * type variables equal to the non-integer types and proceed.
1117 */
1118 if (lp_is_int) {
1119 lbase = rbase;
1120 lkind = rkind;
1121 lref = rref;
1122 lfp = rfp;
1123 } else if (rp_is_int) {
1124 rbase = lbase;
1125 rkind = lkind;
1126 rref = lref;
1127 rfp = lfp;
1128 }
1129
1130 lp_is_void = ctf_type_encoding(lfp, lref, &e) == 0 && IS_VOID(e);
1131 rp_is_void = ctf_type_encoding(rfp, rref, &e) == 0 && IS_VOID(e);
1132
1133 /*
1134 * The types are compatible if both are pointers to the same type, or
1135 * if either pointer is a void pointer. If they are compatible, set
1136 * tp to point to the more specific pointer type and return it.
1137 */
1138 compat = (lkind == CTF_K_POINTER || lkind == CTF_K_ARRAY) &&
1139 (rkind == CTF_K_POINTER || rkind == CTF_K_ARRAY) &&
1140 (lp_is_void || rp_is_void || ctf_type_compat(lfp, lref, rfp, rref));
1141
1142 if (compat) {
1143 if (fpp != NULL)
1144 *fpp = rp_is_void ? lfp : rfp;
1145 if (tp != NULL)
1146 *tp = rp_is_void ? lbase : rbase;
1147 }
1148
1149 return (compat);
1150}
1151
1152/*
1153 * The rules for checking argument types against parameter types are described
1154 * in the ANSI-C spec (see K&R[A7.3.2] and K&R[A7.17]). We use the same rule
1155 * set to determine whether associative array arguments match the prototype.
1156 */
1157int
1158dt_node_is_argcompat(const dt_node_t *lp, const dt_node_t *rp)
1159{
1160 ctf_file_t *lfp = lp->dn_ctfp;
1161 ctf_file_t *rfp = rp->dn_ctfp;
1162
1163 assert(lp->dn_flags & DT_NF_COOKED);
1164 assert(rp->dn_flags & DT_NF_COOKED);
1165
1166 if (dt_node_is_integer(lp) && dt_node_is_integer(rp))
1167 return (1); /* integer types are compatible */
1168
1169 if (dt_node_is_strcompat(lp) && dt_node_is_strcompat(rp))
1170 return (1); /* string types are compatible */
1171
1172 if (dt_node_is_stack(lp) && dt_node_is_stack(rp))
1173 return (1); /* stack types are compatible */
1174
1175 if (dt_node_is_symaddr(lp) && dt_node_is_symaddr(rp))
1176 return (1); /* symaddr types are compatible */
1177
1178 if (dt_node_is_usymaddr(lp) && dt_node_is_usymaddr(rp))
1179 return (1); /* usymaddr types are compatible */
1180
1181 switch (ctf_type_kind(lfp, ctf_type_resolve(lfp, lp->dn_type))) {
1182 case CTF_K_FUNCTION:
1183 case CTF_K_STRUCT:
1184 case CTF_K_UNION:
1185 return (ctf_type_compat(lfp, lp->dn_type, rfp, rp->dn_type));
1186 default:
1187 return (dt_node_is_ptrcompat(lp, rp, NULL, NULL));
1188 }
1189}
1190
1191/*
1192 * We provide dt_node_is_posconst() as a convenience routine for callers who
1193 * wish to verify that an argument is a positive non-zero integer constant.
1194 */
1195int
1196dt_node_is_posconst(const dt_node_t *dnp)
1197{
1198 return (dnp->dn_kind == DT_NODE_INT && dnp->dn_value != 0 && (
1199 (dnp->dn_flags & DT_NF_SIGNED) == 0 || (int64_t)dnp->dn_value > 0));
1200}
1201
1202int
1203dt_node_is_actfunc(const dt_node_t *dnp)
1204{
1205 return (dnp->dn_kind == DT_NODE_FUNC &&
1206 dnp->dn_ident->di_kind == DT_IDENT_ACTFUNC);
1207}
1208
1209/*
1210 * The original rules for integer constant typing are described in K&R[A2.5.1].
1211 * However, since we support long long, we instead use the rules from ISO C99
1212 * clause 6.4.4.1 since that is where long longs are formally described. The
1213 * rules require us to know whether the constant was specified in decimal or
1214 * in octal or hex, which we do by looking at our lexer's 'yyintdecimal' flag.
1215 * The type of an integer constant is the first of the corresponding list in
1216 * which its value can be represented:
1217 *
1218 * unsuffixed decimal: int, long, long long
1219 * unsuffixed oct/hex: int, unsigned int, long, unsigned long,
1220 * long long, unsigned long long
1221 * suffix [uU]: unsigned int, unsigned long, unsigned long long
1222 * suffix [lL] decimal: long, long long
1223 * suffix [lL] oct/hex: long, unsigned long, long long, unsigned long long
1224 * suffix [uU][Ll]: unsigned long, unsigned long long
1225 * suffix ll/LL decimal: long long
1226 * suffix ll/LL oct/hex: long long, unsigned long long
1227 * suffix [uU][ll/LL]: unsigned long long
1228 *
1229 * Given that our lexer has already validated the suffixes by regexp matching,
1230 * there is an obvious way to concisely encode these rules: construct an array
1231 * of the types in the order int, unsigned int, long, unsigned long, long long,
1232 * unsigned long long. Compute an integer array starting index based on the
1233 * suffix (e.g. none = 0, u = 1, ull = 5), and compute an increment based on
1234 * the specifier (dec/oct/hex) and suffix (u). Then iterate from the starting
1235 * index to the end, advancing using the increment, and searching until we
1236 * find a limit that matches or we run out of choices (overflow). To make it
1237 * even faster, we precompute the table of type information in dtrace_open().
1238 */
1239dt_node_t *
1240dt_node_int(uintmax_t value)
1241{
1242 dt_node_t *dnp = dt_node_alloc(DT_NODE_INT);
1243 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
1244
1245 int n = (yyintdecimal | (yyintsuffix[0] == 'u')) + 1;
1246 int i = 0;
1247
1248 const char *p;
1249 char c;
1250
1251 dnp->dn_op = DT_TOK_INT;
1252 dnp->dn_value = value;
1253
1254 for (p = yyintsuffix; (c = *p) != '\0'; p++) {
1255 if (c == 'U' || c == 'u')
1256 i += 1;
1257 else if (c == 'L' || c == 'l')
1258 i += 2;
1259 }
1260
1261 for (; i < sizeof (dtp->dt_ints) / sizeof (dtp->dt_ints[0]); i += n) {
1262 if (value <= dtp->dt_ints[i].did_limit) {
1263 dt_node_type_assign(dnp,
1264 dtp->dt_ints[i].did_ctfp,
1229 dtp->dt_ints[i].did_type);
1265 dtp->dt_ints[i].did_type, B_FALSE);
1230
1231 /*
1232 * If a prefix character is present in macro text, add
1233 * in the corresponding operator node (see dt_lex.l).
1234 */
1235 switch (yyintprefix) {
1236 case '+':
1237 return (dt_node_op1(DT_TOK_IPOS, dnp));
1238 case '-':
1239 return (dt_node_op1(DT_TOK_INEG, dnp));
1240 default:
1241 return (dnp);
1242 }
1243 }
1244 }
1245
1246 xyerror(D_INT_OFLOW, "integer constant 0x%llx cannot be represented "
1247 "in any built-in integral type\n", (u_longlong_t)value);
1248 /*NOTREACHED*/
1249 return (NULL); /* keep gcc happy */
1250}
1251
1252dt_node_t *
1253dt_node_string(char *string)
1254{
1255 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
1256 dt_node_t *dnp;
1257
1258 if (string == NULL)
1259 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
1260
1261 dnp = dt_node_alloc(DT_NODE_STRING);
1262 dnp->dn_op = DT_TOK_STRING;
1263 dnp->dn_string = string;
1266
1267 /*
1268 * If a prefix character is present in macro text, add
1269 * in the corresponding operator node (see dt_lex.l).
1270 */
1271 switch (yyintprefix) {
1272 case '+':
1273 return (dt_node_op1(DT_TOK_IPOS, dnp));
1274 case '-':
1275 return (dt_node_op1(DT_TOK_INEG, dnp));
1276 default:
1277 return (dnp);
1278 }
1279 }
1280 }
1281
1282 xyerror(D_INT_OFLOW, "integer constant 0x%llx cannot be represented "
1283 "in any built-in integral type\n", (u_longlong_t)value);
1284 /*NOTREACHED*/
1285 return (NULL); /* keep gcc happy */
1286}
1287
1288dt_node_t *
1289dt_node_string(char *string)
1290{
1291 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
1292 dt_node_t *dnp;
1293
1294 if (string == NULL)
1295 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
1296
1297 dnp = dt_node_alloc(DT_NODE_STRING);
1298 dnp->dn_op = DT_TOK_STRING;
1299 dnp->dn_string = string;
1264 dt_node_type_assign(dnp, DT_STR_CTFP(dtp), DT_STR_TYPE(dtp));
1300 dt_node_type_assign(dnp, DT_STR_CTFP(dtp), DT_STR_TYPE(dtp), B_FALSE);
1265
1266 return (dnp);
1267}
1268
1269dt_node_t *
1270dt_node_ident(char *name)
1271{
1272 dt_ident_t *idp;
1273 dt_node_t *dnp;
1274
1275 if (name == NULL)
1276 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
1277
1278 /*
1279 * If the identifier is an inlined integer constant, then create an INT
1280 * node that is a clone of the inline parse tree node and return that
1281 * immediately, allowing this inline to be used in parsing contexts
1282 * that require constant expressions (e.g. scalar array sizes).
1283 */
1284 if ((idp = dt_idstack_lookup(&yypcb->pcb_globals, name)) != NULL &&
1285 (idp->di_flags & DT_IDFLG_INLINE)) {
1286 dt_idnode_t *inp = idp->di_iarg;
1287
1288 if (inp->din_root != NULL &&
1289 inp->din_root->dn_kind == DT_NODE_INT) {
1290 free(name);
1291
1292 dnp = dt_node_alloc(DT_NODE_INT);
1293 dnp->dn_op = DT_TOK_INT;
1294 dnp->dn_value = inp->din_root->dn_value;
1295 dt_node_type_propagate(inp->din_root, dnp);
1296
1297 return (dnp);
1298 }
1299 }
1300
1301 dnp = dt_node_alloc(DT_NODE_IDENT);
1302 dnp->dn_op = name[0] == '@' ? DT_TOK_AGG : DT_TOK_IDENT;
1303 dnp->dn_string = name;
1304
1305 return (dnp);
1306}
1307
1308/*
1309 * Create an empty node of type corresponding to the given declaration.
1310 * Explicit references to user types (C or D) are assigned the default
1311 * stability; references to other types are _dtrace_typattr (Private).
1312 */
1313dt_node_t *
1314dt_node_type(dt_decl_t *ddp)
1315{
1316 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
1317 dtrace_typeinfo_t dtt;
1318 dt_node_t *dnp;
1319 char *name = NULL;
1320 int err;
1321
1322 /*
1323 * If 'ddp' is NULL, we get a decl by popping the decl stack. This
1324 * form of dt_node_type() is used by parameter rules in dt_grammar.y.
1325 */
1326 if (ddp == NULL)
1327 ddp = dt_decl_pop_param(&name);
1328
1329 err = dt_decl_type(ddp, &dtt);
1330 dt_decl_free(ddp);
1331
1332 if (err != 0) {
1333 free(name);
1334 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
1335 }
1336
1337 dnp = dt_node_alloc(DT_NODE_TYPE);
1338 dnp->dn_op = DT_TOK_IDENT;
1339 dnp->dn_string = name;
1301
1302 return (dnp);
1303}
1304
1305dt_node_t *
1306dt_node_ident(char *name)
1307{
1308 dt_ident_t *idp;
1309 dt_node_t *dnp;
1310
1311 if (name == NULL)
1312 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
1313
1314 /*
1315 * If the identifier is an inlined integer constant, then create an INT
1316 * node that is a clone of the inline parse tree node and return that
1317 * immediately, allowing this inline to be used in parsing contexts
1318 * that require constant expressions (e.g. scalar array sizes).
1319 */
1320 if ((idp = dt_idstack_lookup(&yypcb->pcb_globals, name)) != NULL &&
1321 (idp->di_flags & DT_IDFLG_INLINE)) {
1322 dt_idnode_t *inp = idp->di_iarg;
1323
1324 if (inp->din_root != NULL &&
1325 inp->din_root->dn_kind == DT_NODE_INT) {
1326 free(name);
1327
1328 dnp = dt_node_alloc(DT_NODE_INT);
1329 dnp->dn_op = DT_TOK_INT;
1330 dnp->dn_value = inp->din_root->dn_value;
1331 dt_node_type_propagate(inp->din_root, dnp);
1332
1333 return (dnp);
1334 }
1335 }
1336
1337 dnp = dt_node_alloc(DT_NODE_IDENT);
1338 dnp->dn_op = name[0] == '@' ? DT_TOK_AGG : DT_TOK_IDENT;
1339 dnp->dn_string = name;
1340
1341 return (dnp);
1342}
1343
1344/*
1345 * Create an empty node of type corresponding to the given declaration.
1346 * Explicit references to user types (C or D) are assigned the default
1347 * stability; references to other types are _dtrace_typattr (Private).
1348 */
1349dt_node_t *
1350dt_node_type(dt_decl_t *ddp)
1351{
1352 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
1353 dtrace_typeinfo_t dtt;
1354 dt_node_t *dnp;
1355 char *name = NULL;
1356 int err;
1357
1358 /*
1359 * If 'ddp' is NULL, we get a decl by popping the decl stack. This
1360 * form of dt_node_type() is used by parameter rules in dt_grammar.y.
1361 */
1362 if (ddp == NULL)
1363 ddp = dt_decl_pop_param(&name);
1364
1365 err = dt_decl_type(ddp, &dtt);
1366 dt_decl_free(ddp);
1367
1368 if (err != 0) {
1369 free(name);
1370 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
1371 }
1372
1373 dnp = dt_node_alloc(DT_NODE_TYPE);
1374 dnp->dn_op = DT_TOK_IDENT;
1375 dnp->dn_string = name;
1340 dt_node_type_assign(dnp, dtt.dtt_ctfp, dtt.dtt_type);
1341
1376
1377 dt_node_type_assign(dnp, dtt.dtt_ctfp, dtt.dtt_type, dtt.dtt_flags);
1378
1342 if (dtt.dtt_ctfp == dtp->dt_cdefs->dm_ctfp ||
1343 dtt.dtt_ctfp == dtp->dt_ddefs->dm_ctfp)
1344 dt_node_attr_assign(dnp, _dtrace_defattr);
1345 else
1346 dt_node_attr_assign(dnp, _dtrace_typattr);
1347
1348 return (dnp);
1349}
1350
1351/*
1352 * Create a type node corresponding to a varargs (...) parameter by just
1353 * assigning it type CTF_ERR. The decl processing code will handle this.
1354 */
1355dt_node_t *
1356dt_node_vatype(void)
1357{
1358 dt_node_t *dnp = dt_node_alloc(DT_NODE_TYPE);
1359
1360 dnp->dn_op = DT_TOK_IDENT;
1361 dnp->dn_ctfp = yypcb->pcb_hdl->dt_cdefs->dm_ctfp;
1362 dnp->dn_type = CTF_ERR;
1363 dnp->dn_attr = _dtrace_defattr;
1364
1365 return (dnp);
1366}
1367
1368/*
1369 * Instantiate a decl using the contents of the current declaration stack. As
1370 * we do not currently permit decls to be initialized, this function currently
1371 * returns NULL and no parse node is created. When this function is called,
1372 * the topmost scope's ds_ident pointer will be set to NULL (indicating no
1373 * init_declarator rule was matched) or will point to the identifier to use.
1374 */
1375dt_node_t *
1376dt_node_decl(void)
1377{
1378 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
1379 dt_scope_t *dsp = &yypcb->pcb_dstack;
1380 dt_dclass_t class = dsp->ds_class;
1381 dt_decl_t *ddp = dt_decl_top();
1382
1383 dt_module_t *dmp;
1384 dtrace_typeinfo_t dtt;
1385 ctf_id_t type;
1386
1387 char n1[DT_TYPE_NAMELEN];
1388 char n2[DT_TYPE_NAMELEN];
1389
1390 if (dt_decl_type(ddp, &dtt) != 0)
1391 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
1392
1393 /*
1394 * If we have no declaration identifier, then this is either a spurious
1395 * declaration of an intrinsic type (e.g. "extern int;") or declaration
1396 * or redeclaration of a struct, union, or enum type or tag.
1397 */
1398 if (dsp->ds_ident == NULL) {
1399 if (ddp->dd_kind != CTF_K_STRUCT &&
1400 ddp->dd_kind != CTF_K_UNION && ddp->dd_kind != CTF_K_ENUM)
1401 xyerror(D_DECL_USELESS, "useless declaration\n");
1402
1403 dt_dprintf("type %s added as id %ld\n", dt_type_name(
1404 ddp->dd_ctfp, ddp->dd_type, n1, sizeof (n1)), ddp->dd_type);
1405
1406 return (NULL);
1407 }
1408
1409 if (strchr(dsp->ds_ident, '`') != NULL) {
1410 xyerror(D_DECL_SCOPE, "D scoping operator may not be used in "
1411 "a declaration name (%s)\n", dsp->ds_ident);
1412 }
1413
1414 /*
1415 * If we are nested inside of a C include file, add the declaration to
1416 * the C definition module; otherwise use the D definition module.
1417 */
1418 if (yypcb->pcb_idepth != 0)
1419 dmp = dtp->dt_cdefs;
1420 else
1421 dmp = dtp->dt_ddefs;
1422
1423 /*
1424 * If we see a global or static declaration of a function prototype,
1425 * treat this as equivalent to a D extern declaration.
1426 */
1427 if (ctf_type_kind(dtt.dtt_ctfp, dtt.dtt_type) == CTF_K_FUNCTION &&
1428 (class == DT_DC_DEFAULT || class == DT_DC_STATIC))
1429 class = DT_DC_EXTERN;
1430
1431 switch (class) {
1432 case DT_DC_AUTO:
1433 case DT_DC_REGISTER:
1434 case DT_DC_STATIC:
1435 xyerror(D_DECL_BADCLASS, "specified storage class not "
1436 "appropriate in D\n");
1437 /*NOTREACHED*/
1438
1439 case DT_DC_EXTERN: {
1440 dtrace_typeinfo_t ott;
1441 dtrace_syminfo_t dts;
1442 GElf_Sym sym;
1443
1444 int exists = dtrace_lookup_by_name(dtp,
1445 dmp->dm_name, dsp->ds_ident, &sym, &dts) == 0;
1446
1447 if (exists && (dtrace_symbol_type(dtp, &sym, &dts, &ott) != 0 ||
1448 ctf_type_cmp(dtt.dtt_ctfp, dtt.dtt_type,
1449 ott.dtt_ctfp, ott.dtt_type) != 0)) {
1450 xyerror(D_DECL_IDRED, "identifier redeclared: %s`%s\n"
1451 "\t current: %s\n\tprevious: %s\n",
1452 dmp->dm_name, dsp->ds_ident,
1453 dt_type_name(dtt.dtt_ctfp, dtt.dtt_type,
1454 n1, sizeof (n1)),
1455 dt_type_name(ott.dtt_ctfp, ott.dtt_type,
1456 n2, sizeof (n2)));
1457 } else if (!exists && dt_module_extern(dtp, dmp,
1458 dsp->ds_ident, &dtt) == NULL) {
1459 xyerror(D_UNKNOWN,
1460 "failed to extern %s: %s\n", dsp->ds_ident,
1461 dtrace_errmsg(dtp, dtrace_errno(dtp)));
1462 } else {
1463 dt_dprintf("extern %s`%s type=<%s>\n",
1464 dmp->dm_name, dsp->ds_ident,
1465 dt_type_name(dtt.dtt_ctfp, dtt.dtt_type,
1466 n1, sizeof (n1)));
1467 }
1468 break;
1469 }
1470
1471 case DT_DC_TYPEDEF:
1472 if (dt_idstack_lookup(&yypcb->pcb_globals, dsp->ds_ident)) {
1473 xyerror(D_DECL_IDRED, "global variable identifier "
1474 "redeclared: %s\n", dsp->ds_ident);
1475 }
1476
1477 if (ctf_lookup_by_name(dmp->dm_ctfp,
1478 dsp->ds_ident) != CTF_ERR) {
1479 xyerror(D_DECL_IDRED,
1480 "typedef redeclared: %s\n", dsp->ds_ident);
1481 }
1482
1483 /*
1484 * If the source type for the typedef is not defined in the
1485 * target container or its parent, copy the type to the target
1486 * container and reset dtt_ctfp and dtt_type to the copy.
1487 */
1488 if (dtt.dtt_ctfp != dmp->dm_ctfp &&
1489 dtt.dtt_ctfp != ctf_parent_file(dmp->dm_ctfp)) {
1490
1491 dtt.dtt_type = ctf_add_type(dmp->dm_ctfp,
1492 dtt.dtt_ctfp, dtt.dtt_type);
1493 dtt.dtt_ctfp = dmp->dm_ctfp;
1494
1495 if (dtt.dtt_type == CTF_ERR ||
1496 ctf_update(dtt.dtt_ctfp) == CTF_ERR) {
1497 xyerror(D_UNKNOWN, "failed to copy typedef %s "
1498 "source type: %s\n", dsp->ds_ident,
1499 ctf_errmsg(ctf_errno(dtt.dtt_ctfp)));
1500 }
1501 }
1502
1503 type = ctf_add_typedef(dmp->dm_ctfp,
1504 CTF_ADD_ROOT, dsp->ds_ident, dtt.dtt_type);
1505
1506 if (type == CTF_ERR || ctf_update(dmp->dm_ctfp) == CTF_ERR) {
1507 xyerror(D_UNKNOWN, "failed to typedef %s: %s\n",
1508 dsp->ds_ident, ctf_errmsg(ctf_errno(dmp->dm_ctfp)));
1509 }
1510
1511 dt_dprintf("typedef %s added as id %ld\n", dsp->ds_ident, type);
1512 break;
1513
1514 default: {
1515 ctf_encoding_t cte;
1516 dt_idhash_t *dhp;
1517 dt_ident_t *idp;
1518 dt_node_t idn;
1519 int assc, idkind;
1520 uint_t id, kind;
1521 ushort_t idflags;
1522
1523 switch (class) {
1524 case DT_DC_THIS:
1525 dhp = yypcb->pcb_locals;
1526 idflags = DT_IDFLG_LOCAL;
1527 idp = dt_idhash_lookup(dhp, dsp->ds_ident);
1528 break;
1529 case DT_DC_SELF:
1530 dhp = dtp->dt_tls;
1531 idflags = DT_IDFLG_TLS;
1532 idp = dt_idhash_lookup(dhp, dsp->ds_ident);
1533 break;
1534 default:
1535 dhp = dtp->dt_globals;
1536 idflags = 0;
1537 idp = dt_idstack_lookup(
1538 &yypcb->pcb_globals, dsp->ds_ident);
1539 break;
1540 }
1541
1542 if (ddp->dd_kind == CTF_K_ARRAY && ddp->dd_node == NULL) {
1543 xyerror(D_DECL_ARRNULL,
1544 "array declaration requires array dimension or "
1545 "tuple signature: %s\n", dsp->ds_ident);
1546 }
1547
1548 if (idp != NULL && idp->di_gen == 0) {
1549 xyerror(D_DECL_IDRED, "built-in identifier "
1550 "redeclared: %s\n", idp->di_name);
1551 }
1552
1553 if (dtrace_lookup_by_type(dtp, DTRACE_OBJ_CDEFS,
1554 dsp->ds_ident, NULL) == 0 ||
1555 dtrace_lookup_by_type(dtp, DTRACE_OBJ_DDEFS,
1556 dsp->ds_ident, NULL) == 0) {
1557 xyerror(D_DECL_IDRED, "typedef identifier "
1558 "redeclared: %s\n", dsp->ds_ident);
1559 }
1560
1561 /*
1562 * Cache some attributes of the decl to make the rest of this
1563 * code simpler: if the decl is an array which is subscripted
1564 * by a type rather than an integer, then it's an associative
1565 * array (assc). We then expect to match either DT_IDENT_ARRAY
1566 * for associative arrays or DT_IDENT_SCALAR for anything else.
1567 */
1568 assc = ddp->dd_kind == CTF_K_ARRAY &&
1569 ddp->dd_node->dn_kind == DT_NODE_TYPE;
1570
1571 idkind = assc ? DT_IDENT_ARRAY : DT_IDENT_SCALAR;
1572
1573 /*
1574 * Create a fake dt_node_t on the stack so we can determine the
1575 * type of any matching identifier by assigning to this node.
1576 * If the pre-existing ident has its di_type set, propagate
1577 * the type by hand so as not to trigger a prototype check for
1578 * arrays (yet); otherwise we use dt_ident_cook() on the ident
1579 * to ensure it is fully initialized before looking at it.
1580 */
1581 bzero(&idn, sizeof (dt_node_t));
1582
1583 if (idp != NULL && idp->di_type != CTF_ERR)
1379 if (dtt.dtt_ctfp == dtp->dt_cdefs->dm_ctfp ||
1380 dtt.dtt_ctfp == dtp->dt_ddefs->dm_ctfp)
1381 dt_node_attr_assign(dnp, _dtrace_defattr);
1382 else
1383 dt_node_attr_assign(dnp, _dtrace_typattr);
1384
1385 return (dnp);
1386}
1387
1388/*
1389 * Create a type node corresponding to a varargs (...) parameter by just
1390 * assigning it type CTF_ERR. The decl processing code will handle this.
1391 */
1392dt_node_t *
1393dt_node_vatype(void)
1394{
1395 dt_node_t *dnp = dt_node_alloc(DT_NODE_TYPE);
1396
1397 dnp->dn_op = DT_TOK_IDENT;
1398 dnp->dn_ctfp = yypcb->pcb_hdl->dt_cdefs->dm_ctfp;
1399 dnp->dn_type = CTF_ERR;
1400 dnp->dn_attr = _dtrace_defattr;
1401
1402 return (dnp);
1403}
1404
1405/*
1406 * Instantiate a decl using the contents of the current declaration stack. As
1407 * we do not currently permit decls to be initialized, this function currently
1408 * returns NULL and no parse node is created. When this function is called,
1409 * the topmost scope's ds_ident pointer will be set to NULL (indicating no
1410 * init_declarator rule was matched) or will point to the identifier to use.
1411 */
1412dt_node_t *
1413dt_node_decl(void)
1414{
1415 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
1416 dt_scope_t *dsp = &yypcb->pcb_dstack;
1417 dt_dclass_t class = dsp->ds_class;
1418 dt_decl_t *ddp = dt_decl_top();
1419
1420 dt_module_t *dmp;
1421 dtrace_typeinfo_t dtt;
1422 ctf_id_t type;
1423
1424 char n1[DT_TYPE_NAMELEN];
1425 char n2[DT_TYPE_NAMELEN];
1426
1427 if (dt_decl_type(ddp, &dtt) != 0)
1428 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
1429
1430 /*
1431 * If we have no declaration identifier, then this is either a spurious
1432 * declaration of an intrinsic type (e.g. "extern int;") or declaration
1433 * or redeclaration of a struct, union, or enum type or tag.
1434 */
1435 if (dsp->ds_ident == NULL) {
1436 if (ddp->dd_kind != CTF_K_STRUCT &&
1437 ddp->dd_kind != CTF_K_UNION && ddp->dd_kind != CTF_K_ENUM)
1438 xyerror(D_DECL_USELESS, "useless declaration\n");
1439
1440 dt_dprintf("type %s added as id %ld\n", dt_type_name(
1441 ddp->dd_ctfp, ddp->dd_type, n1, sizeof (n1)), ddp->dd_type);
1442
1443 return (NULL);
1444 }
1445
1446 if (strchr(dsp->ds_ident, '`') != NULL) {
1447 xyerror(D_DECL_SCOPE, "D scoping operator may not be used in "
1448 "a declaration name (%s)\n", dsp->ds_ident);
1449 }
1450
1451 /*
1452 * If we are nested inside of a C include file, add the declaration to
1453 * the C definition module; otherwise use the D definition module.
1454 */
1455 if (yypcb->pcb_idepth != 0)
1456 dmp = dtp->dt_cdefs;
1457 else
1458 dmp = dtp->dt_ddefs;
1459
1460 /*
1461 * If we see a global or static declaration of a function prototype,
1462 * treat this as equivalent to a D extern declaration.
1463 */
1464 if (ctf_type_kind(dtt.dtt_ctfp, dtt.dtt_type) == CTF_K_FUNCTION &&
1465 (class == DT_DC_DEFAULT || class == DT_DC_STATIC))
1466 class = DT_DC_EXTERN;
1467
1468 switch (class) {
1469 case DT_DC_AUTO:
1470 case DT_DC_REGISTER:
1471 case DT_DC_STATIC:
1472 xyerror(D_DECL_BADCLASS, "specified storage class not "
1473 "appropriate in D\n");
1474 /*NOTREACHED*/
1475
1476 case DT_DC_EXTERN: {
1477 dtrace_typeinfo_t ott;
1478 dtrace_syminfo_t dts;
1479 GElf_Sym sym;
1480
1481 int exists = dtrace_lookup_by_name(dtp,
1482 dmp->dm_name, dsp->ds_ident, &sym, &dts) == 0;
1483
1484 if (exists && (dtrace_symbol_type(dtp, &sym, &dts, &ott) != 0 ||
1485 ctf_type_cmp(dtt.dtt_ctfp, dtt.dtt_type,
1486 ott.dtt_ctfp, ott.dtt_type) != 0)) {
1487 xyerror(D_DECL_IDRED, "identifier redeclared: %s`%s\n"
1488 "\t current: %s\n\tprevious: %s\n",
1489 dmp->dm_name, dsp->ds_ident,
1490 dt_type_name(dtt.dtt_ctfp, dtt.dtt_type,
1491 n1, sizeof (n1)),
1492 dt_type_name(ott.dtt_ctfp, ott.dtt_type,
1493 n2, sizeof (n2)));
1494 } else if (!exists && dt_module_extern(dtp, dmp,
1495 dsp->ds_ident, &dtt) == NULL) {
1496 xyerror(D_UNKNOWN,
1497 "failed to extern %s: %s\n", dsp->ds_ident,
1498 dtrace_errmsg(dtp, dtrace_errno(dtp)));
1499 } else {
1500 dt_dprintf("extern %s`%s type=<%s>\n",
1501 dmp->dm_name, dsp->ds_ident,
1502 dt_type_name(dtt.dtt_ctfp, dtt.dtt_type,
1503 n1, sizeof (n1)));
1504 }
1505 break;
1506 }
1507
1508 case DT_DC_TYPEDEF:
1509 if (dt_idstack_lookup(&yypcb->pcb_globals, dsp->ds_ident)) {
1510 xyerror(D_DECL_IDRED, "global variable identifier "
1511 "redeclared: %s\n", dsp->ds_ident);
1512 }
1513
1514 if (ctf_lookup_by_name(dmp->dm_ctfp,
1515 dsp->ds_ident) != CTF_ERR) {
1516 xyerror(D_DECL_IDRED,
1517 "typedef redeclared: %s\n", dsp->ds_ident);
1518 }
1519
1520 /*
1521 * If the source type for the typedef is not defined in the
1522 * target container or its parent, copy the type to the target
1523 * container and reset dtt_ctfp and dtt_type to the copy.
1524 */
1525 if (dtt.dtt_ctfp != dmp->dm_ctfp &&
1526 dtt.dtt_ctfp != ctf_parent_file(dmp->dm_ctfp)) {
1527
1528 dtt.dtt_type = ctf_add_type(dmp->dm_ctfp,
1529 dtt.dtt_ctfp, dtt.dtt_type);
1530 dtt.dtt_ctfp = dmp->dm_ctfp;
1531
1532 if (dtt.dtt_type == CTF_ERR ||
1533 ctf_update(dtt.dtt_ctfp) == CTF_ERR) {
1534 xyerror(D_UNKNOWN, "failed to copy typedef %s "
1535 "source type: %s\n", dsp->ds_ident,
1536 ctf_errmsg(ctf_errno(dtt.dtt_ctfp)));
1537 }
1538 }
1539
1540 type = ctf_add_typedef(dmp->dm_ctfp,
1541 CTF_ADD_ROOT, dsp->ds_ident, dtt.dtt_type);
1542
1543 if (type == CTF_ERR || ctf_update(dmp->dm_ctfp) == CTF_ERR) {
1544 xyerror(D_UNKNOWN, "failed to typedef %s: %s\n",
1545 dsp->ds_ident, ctf_errmsg(ctf_errno(dmp->dm_ctfp)));
1546 }
1547
1548 dt_dprintf("typedef %s added as id %ld\n", dsp->ds_ident, type);
1549 break;
1550
1551 default: {
1552 ctf_encoding_t cte;
1553 dt_idhash_t *dhp;
1554 dt_ident_t *idp;
1555 dt_node_t idn;
1556 int assc, idkind;
1557 uint_t id, kind;
1558 ushort_t idflags;
1559
1560 switch (class) {
1561 case DT_DC_THIS:
1562 dhp = yypcb->pcb_locals;
1563 idflags = DT_IDFLG_LOCAL;
1564 idp = dt_idhash_lookup(dhp, dsp->ds_ident);
1565 break;
1566 case DT_DC_SELF:
1567 dhp = dtp->dt_tls;
1568 idflags = DT_IDFLG_TLS;
1569 idp = dt_idhash_lookup(dhp, dsp->ds_ident);
1570 break;
1571 default:
1572 dhp = dtp->dt_globals;
1573 idflags = 0;
1574 idp = dt_idstack_lookup(
1575 &yypcb->pcb_globals, dsp->ds_ident);
1576 break;
1577 }
1578
1579 if (ddp->dd_kind == CTF_K_ARRAY && ddp->dd_node == NULL) {
1580 xyerror(D_DECL_ARRNULL,
1581 "array declaration requires array dimension or "
1582 "tuple signature: %s\n", dsp->ds_ident);
1583 }
1584
1585 if (idp != NULL && idp->di_gen == 0) {
1586 xyerror(D_DECL_IDRED, "built-in identifier "
1587 "redeclared: %s\n", idp->di_name);
1588 }
1589
1590 if (dtrace_lookup_by_type(dtp, DTRACE_OBJ_CDEFS,
1591 dsp->ds_ident, NULL) == 0 ||
1592 dtrace_lookup_by_type(dtp, DTRACE_OBJ_DDEFS,
1593 dsp->ds_ident, NULL) == 0) {
1594 xyerror(D_DECL_IDRED, "typedef identifier "
1595 "redeclared: %s\n", dsp->ds_ident);
1596 }
1597
1598 /*
1599 * Cache some attributes of the decl to make the rest of this
1600 * code simpler: if the decl is an array which is subscripted
1601 * by a type rather than an integer, then it's an associative
1602 * array (assc). We then expect to match either DT_IDENT_ARRAY
1603 * for associative arrays or DT_IDENT_SCALAR for anything else.
1604 */
1605 assc = ddp->dd_kind == CTF_K_ARRAY &&
1606 ddp->dd_node->dn_kind == DT_NODE_TYPE;
1607
1608 idkind = assc ? DT_IDENT_ARRAY : DT_IDENT_SCALAR;
1609
1610 /*
1611 * Create a fake dt_node_t on the stack so we can determine the
1612 * type of any matching identifier by assigning to this node.
1613 * If the pre-existing ident has its di_type set, propagate
1614 * the type by hand so as not to trigger a prototype check for
1615 * arrays (yet); otherwise we use dt_ident_cook() on the ident
1616 * to ensure it is fully initialized before looking at it.
1617 */
1618 bzero(&idn, sizeof (dt_node_t));
1619
1620 if (idp != NULL && idp->di_type != CTF_ERR)
1584 dt_node_type_assign(&idn, idp->di_ctfp, idp->di_type);
1621 dt_node_type_assign(&idn, idp->di_ctfp, idp->di_type,
1622 B_FALSE);
1585 else if (idp != NULL)
1586 (void) dt_ident_cook(&idn, idp, NULL);
1587
1588 if (assc) {
1589 if (class == DT_DC_THIS) {
1590 xyerror(D_DECL_LOCASSC, "associative arrays "
1591 "may not be declared as local variables:"
1592 " %s\n", dsp->ds_ident);
1593 }
1594
1595 if (dt_decl_type(ddp->dd_next, &dtt) != 0)
1596 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
1597 }
1598
1599 if (idp != NULL && (idp->di_kind != idkind ||
1600 ctf_type_cmp(dtt.dtt_ctfp, dtt.dtt_type,
1601 idn.dn_ctfp, idn.dn_type) != 0)) {
1602 xyerror(D_DECL_IDRED, "identifier redeclared: %s\n"
1603 "\t current: %s %s\n\tprevious: %s %s\n",
1604 dsp->ds_ident, dt_idkind_name(idkind),
1605 dt_type_name(dtt.dtt_ctfp,
1606 dtt.dtt_type, n1, sizeof (n1)),
1607 dt_idkind_name(idp->di_kind),
1608 dt_node_type_name(&idn, n2, sizeof (n2)));
1609
1610 } else if (idp != NULL && assc) {
1611 const dt_idsig_t *isp = idp->di_data;
1612 dt_node_t *dnp = ddp->dd_node;
1613 int argc = 0;
1614
1615 for (; dnp != NULL; dnp = dnp->dn_list, argc++) {
1616 const dt_node_t *pnp = &isp->dis_args[argc];
1617
1618 if (argc >= isp->dis_argc)
1619 continue; /* tuple length mismatch */
1620
1621 if (ctf_type_cmp(dnp->dn_ctfp, dnp->dn_type,
1622 pnp->dn_ctfp, pnp->dn_type) == 0)
1623 continue;
1624
1625 xyerror(D_DECL_IDRED,
1626 "identifier redeclared: %s\n"
1627 "\t current: %s, key #%d of type %s\n"
1628 "\tprevious: %s, key #%d of type %s\n",
1629 dsp->ds_ident,
1630 dt_idkind_name(idkind), argc + 1,
1631 dt_node_type_name(dnp, n1, sizeof (n1)),
1632 dt_idkind_name(idp->di_kind), argc + 1,
1633 dt_node_type_name(pnp, n2, sizeof (n2)));
1634 }
1635
1636 if (isp->dis_argc != argc) {
1637 xyerror(D_DECL_IDRED,
1638 "identifier redeclared: %s\n"
1639 "\t current: %s of %s, tuple length %d\n"
1640 "\tprevious: %s of %s, tuple length %d\n",
1641 dsp->ds_ident, dt_idkind_name(idkind),
1642 dt_type_name(dtt.dtt_ctfp, dtt.dtt_type,
1643 n1, sizeof (n1)), argc,
1644 dt_idkind_name(idp->di_kind),
1645 dt_node_type_name(&idn, n2, sizeof (n2)),
1646 isp->dis_argc);
1647 }
1648
1649 } else if (idp == NULL) {
1650 type = ctf_type_resolve(dtt.dtt_ctfp, dtt.dtt_type);
1651 kind = ctf_type_kind(dtt.dtt_ctfp, type);
1652
1653 switch (kind) {
1654 case CTF_K_INTEGER:
1655 if (ctf_type_encoding(dtt.dtt_ctfp, type,
1656 &cte) == 0 && IS_VOID(cte)) {
1657 xyerror(D_DECL_VOIDOBJ, "cannot have "
1658 "void object: %s\n", dsp->ds_ident);
1659 }
1660 break;
1661 case CTF_K_STRUCT:
1662 case CTF_K_UNION:
1663 if (ctf_type_size(dtt.dtt_ctfp, type) != 0)
1664 break; /* proceed to declaring */
1665 /*FALLTHRU*/
1666 case CTF_K_FORWARD:
1667 xyerror(D_DECL_INCOMPLETE,
1668 "incomplete struct/union/enum %s: %s\n",
1669 dt_type_name(dtt.dtt_ctfp, dtt.dtt_type,
1670 n1, sizeof (n1)), dsp->ds_ident);
1671 /*NOTREACHED*/
1672 }
1673
1674 if (dt_idhash_nextid(dhp, &id) == -1) {
1675 xyerror(D_ID_OFLOW, "cannot create %s: limit "
1676 "on number of %s variables exceeded\n",
1677 dsp->ds_ident, dt_idhash_name(dhp));
1678 }
1679
1680 dt_dprintf("declare %s %s variable %s, id=%u\n",
1681 dt_idhash_name(dhp), dt_idkind_name(idkind),
1682 dsp->ds_ident, id);
1683
1684 idp = dt_idhash_insert(dhp, dsp->ds_ident, idkind,
1685 idflags | DT_IDFLG_WRITE | DT_IDFLG_DECL, id,
1686 _dtrace_defattr, 0, assc ? &dt_idops_assc :
1687 &dt_idops_thaw, NULL, dtp->dt_gen);
1688
1689 if (idp == NULL)
1690 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
1691
1692 dt_ident_type_assign(idp, dtt.dtt_ctfp, dtt.dtt_type);
1693
1694 /*
1695 * If we are declaring an associative array, use our
1696 * fake parse node to cook the new assoc identifier.
1697 * This will force the ident code to instantiate the
1698 * array type signature corresponding to the list of
1699 * types pointed to by ddp->dd_node. We also reset
1700 * the identifier's attributes based upon the result.
1701 */
1702 if (assc) {
1703 idp->di_attr =
1704 dt_ident_cook(&idn, idp, &ddp->dd_node);
1705 }
1706 }
1707 }
1708
1709 } /* end of switch */
1710
1711 free(dsp->ds_ident);
1712 dsp->ds_ident = NULL;
1713
1714 return (NULL);
1715}
1716
1717dt_node_t *
1718dt_node_func(dt_node_t *dnp, dt_node_t *args)
1719{
1720 dt_ident_t *idp;
1721
1722 if (dnp->dn_kind != DT_NODE_IDENT) {
1723 xyerror(D_FUNC_IDENT,
1724 "function designator is not of function type\n");
1725 }
1726
1727 idp = dt_idstack_lookup(&yypcb->pcb_globals, dnp->dn_string);
1728
1729 if (idp == NULL) {
1730 xyerror(D_FUNC_UNDEF,
1731 "undefined function name: %s\n", dnp->dn_string);
1732 }
1733
1734 if (idp->di_kind != DT_IDENT_FUNC &&
1735 idp->di_kind != DT_IDENT_AGGFUNC &&
1736 idp->di_kind != DT_IDENT_ACTFUNC) {
1737 xyerror(D_FUNC_IDKIND, "%s '%s' may not be referenced as a "
1738 "function\n", dt_idkind_name(idp->di_kind), idp->di_name);
1739 }
1740
1741 free(dnp->dn_string);
1742 dnp->dn_string = NULL;
1743
1744 dnp->dn_kind = DT_NODE_FUNC;
1745 dnp->dn_flags &= ~DT_NF_COOKED;
1746 dnp->dn_ident = idp;
1747 dnp->dn_args = args;
1748 dnp->dn_list = NULL;
1749
1750 return (dnp);
1751}
1752
1753/*
1754 * The offsetof() function is special because it takes a type name as an
1755 * argument. It does not actually construct its own node; after looking up the
1756 * structure or union offset, we just return an integer node with the offset.
1757 */
1758dt_node_t *
1759dt_node_offsetof(dt_decl_t *ddp, char *s)
1760{
1761 dtrace_typeinfo_t dtt;
1762 dt_node_t dn;
1763 char *name;
1764 int err;
1765
1766 ctf_membinfo_t ctm;
1767 ctf_id_t type;
1768 uint_t kind;
1769
1770 name = alloca(strlen(s) + 1);
1771 (void) strcpy(name, s);
1772 free(s);
1773
1774 err = dt_decl_type(ddp, &dtt);
1775 dt_decl_free(ddp);
1776
1777 if (err != 0)
1778 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
1779
1780 type = ctf_type_resolve(dtt.dtt_ctfp, dtt.dtt_type);
1781 kind = ctf_type_kind(dtt.dtt_ctfp, type);
1782
1783 if (kind != CTF_K_STRUCT && kind != CTF_K_UNION) {
1784 xyerror(D_OFFSETOF_TYPE,
1785 "offsetof operand must be a struct or union type\n");
1786 }
1787
1788 if (ctf_member_info(dtt.dtt_ctfp, type, name, &ctm) == CTF_ERR) {
1789 xyerror(D_UNKNOWN, "failed to determine offset of %s: %s\n",
1790 name, ctf_errmsg(ctf_errno(dtt.dtt_ctfp)));
1791 }
1792
1793 bzero(&dn, sizeof (dn));
1623 else if (idp != NULL)
1624 (void) dt_ident_cook(&idn, idp, NULL);
1625
1626 if (assc) {
1627 if (class == DT_DC_THIS) {
1628 xyerror(D_DECL_LOCASSC, "associative arrays "
1629 "may not be declared as local variables:"
1630 " %s\n", dsp->ds_ident);
1631 }
1632
1633 if (dt_decl_type(ddp->dd_next, &dtt) != 0)
1634 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
1635 }
1636
1637 if (idp != NULL && (idp->di_kind != idkind ||
1638 ctf_type_cmp(dtt.dtt_ctfp, dtt.dtt_type,
1639 idn.dn_ctfp, idn.dn_type) != 0)) {
1640 xyerror(D_DECL_IDRED, "identifier redeclared: %s\n"
1641 "\t current: %s %s\n\tprevious: %s %s\n",
1642 dsp->ds_ident, dt_idkind_name(idkind),
1643 dt_type_name(dtt.dtt_ctfp,
1644 dtt.dtt_type, n1, sizeof (n1)),
1645 dt_idkind_name(idp->di_kind),
1646 dt_node_type_name(&idn, n2, sizeof (n2)));
1647
1648 } else if (idp != NULL && assc) {
1649 const dt_idsig_t *isp = idp->di_data;
1650 dt_node_t *dnp = ddp->dd_node;
1651 int argc = 0;
1652
1653 for (; dnp != NULL; dnp = dnp->dn_list, argc++) {
1654 const dt_node_t *pnp = &isp->dis_args[argc];
1655
1656 if (argc >= isp->dis_argc)
1657 continue; /* tuple length mismatch */
1658
1659 if (ctf_type_cmp(dnp->dn_ctfp, dnp->dn_type,
1660 pnp->dn_ctfp, pnp->dn_type) == 0)
1661 continue;
1662
1663 xyerror(D_DECL_IDRED,
1664 "identifier redeclared: %s\n"
1665 "\t current: %s, key #%d of type %s\n"
1666 "\tprevious: %s, key #%d of type %s\n",
1667 dsp->ds_ident,
1668 dt_idkind_name(idkind), argc + 1,
1669 dt_node_type_name(dnp, n1, sizeof (n1)),
1670 dt_idkind_name(idp->di_kind), argc + 1,
1671 dt_node_type_name(pnp, n2, sizeof (n2)));
1672 }
1673
1674 if (isp->dis_argc != argc) {
1675 xyerror(D_DECL_IDRED,
1676 "identifier redeclared: %s\n"
1677 "\t current: %s of %s, tuple length %d\n"
1678 "\tprevious: %s of %s, tuple length %d\n",
1679 dsp->ds_ident, dt_idkind_name(idkind),
1680 dt_type_name(dtt.dtt_ctfp, dtt.dtt_type,
1681 n1, sizeof (n1)), argc,
1682 dt_idkind_name(idp->di_kind),
1683 dt_node_type_name(&idn, n2, sizeof (n2)),
1684 isp->dis_argc);
1685 }
1686
1687 } else if (idp == NULL) {
1688 type = ctf_type_resolve(dtt.dtt_ctfp, dtt.dtt_type);
1689 kind = ctf_type_kind(dtt.dtt_ctfp, type);
1690
1691 switch (kind) {
1692 case CTF_K_INTEGER:
1693 if (ctf_type_encoding(dtt.dtt_ctfp, type,
1694 &cte) == 0 && IS_VOID(cte)) {
1695 xyerror(D_DECL_VOIDOBJ, "cannot have "
1696 "void object: %s\n", dsp->ds_ident);
1697 }
1698 break;
1699 case CTF_K_STRUCT:
1700 case CTF_K_UNION:
1701 if (ctf_type_size(dtt.dtt_ctfp, type) != 0)
1702 break; /* proceed to declaring */
1703 /*FALLTHRU*/
1704 case CTF_K_FORWARD:
1705 xyerror(D_DECL_INCOMPLETE,
1706 "incomplete struct/union/enum %s: %s\n",
1707 dt_type_name(dtt.dtt_ctfp, dtt.dtt_type,
1708 n1, sizeof (n1)), dsp->ds_ident);
1709 /*NOTREACHED*/
1710 }
1711
1712 if (dt_idhash_nextid(dhp, &id) == -1) {
1713 xyerror(D_ID_OFLOW, "cannot create %s: limit "
1714 "on number of %s variables exceeded\n",
1715 dsp->ds_ident, dt_idhash_name(dhp));
1716 }
1717
1718 dt_dprintf("declare %s %s variable %s, id=%u\n",
1719 dt_idhash_name(dhp), dt_idkind_name(idkind),
1720 dsp->ds_ident, id);
1721
1722 idp = dt_idhash_insert(dhp, dsp->ds_ident, idkind,
1723 idflags | DT_IDFLG_WRITE | DT_IDFLG_DECL, id,
1724 _dtrace_defattr, 0, assc ? &dt_idops_assc :
1725 &dt_idops_thaw, NULL, dtp->dt_gen);
1726
1727 if (idp == NULL)
1728 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
1729
1730 dt_ident_type_assign(idp, dtt.dtt_ctfp, dtt.dtt_type);
1731
1732 /*
1733 * If we are declaring an associative array, use our
1734 * fake parse node to cook the new assoc identifier.
1735 * This will force the ident code to instantiate the
1736 * array type signature corresponding to the list of
1737 * types pointed to by ddp->dd_node. We also reset
1738 * the identifier's attributes based upon the result.
1739 */
1740 if (assc) {
1741 idp->di_attr =
1742 dt_ident_cook(&idn, idp, &ddp->dd_node);
1743 }
1744 }
1745 }
1746
1747 } /* end of switch */
1748
1749 free(dsp->ds_ident);
1750 dsp->ds_ident = NULL;
1751
1752 return (NULL);
1753}
1754
1755dt_node_t *
1756dt_node_func(dt_node_t *dnp, dt_node_t *args)
1757{
1758 dt_ident_t *idp;
1759
1760 if (dnp->dn_kind != DT_NODE_IDENT) {
1761 xyerror(D_FUNC_IDENT,
1762 "function designator is not of function type\n");
1763 }
1764
1765 idp = dt_idstack_lookup(&yypcb->pcb_globals, dnp->dn_string);
1766
1767 if (idp == NULL) {
1768 xyerror(D_FUNC_UNDEF,
1769 "undefined function name: %s\n", dnp->dn_string);
1770 }
1771
1772 if (idp->di_kind != DT_IDENT_FUNC &&
1773 idp->di_kind != DT_IDENT_AGGFUNC &&
1774 idp->di_kind != DT_IDENT_ACTFUNC) {
1775 xyerror(D_FUNC_IDKIND, "%s '%s' may not be referenced as a "
1776 "function\n", dt_idkind_name(idp->di_kind), idp->di_name);
1777 }
1778
1779 free(dnp->dn_string);
1780 dnp->dn_string = NULL;
1781
1782 dnp->dn_kind = DT_NODE_FUNC;
1783 dnp->dn_flags &= ~DT_NF_COOKED;
1784 dnp->dn_ident = idp;
1785 dnp->dn_args = args;
1786 dnp->dn_list = NULL;
1787
1788 return (dnp);
1789}
1790
1791/*
1792 * The offsetof() function is special because it takes a type name as an
1793 * argument. It does not actually construct its own node; after looking up the
1794 * structure or union offset, we just return an integer node with the offset.
1795 */
1796dt_node_t *
1797dt_node_offsetof(dt_decl_t *ddp, char *s)
1798{
1799 dtrace_typeinfo_t dtt;
1800 dt_node_t dn;
1801 char *name;
1802 int err;
1803
1804 ctf_membinfo_t ctm;
1805 ctf_id_t type;
1806 uint_t kind;
1807
1808 name = alloca(strlen(s) + 1);
1809 (void) strcpy(name, s);
1810 free(s);
1811
1812 err = dt_decl_type(ddp, &dtt);
1813 dt_decl_free(ddp);
1814
1815 if (err != 0)
1816 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
1817
1818 type = ctf_type_resolve(dtt.dtt_ctfp, dtt.dtt_type);
1819 kind = ctf_type_kind(dtt.dtt_ctfp, type);
1820
1821 if (kind != CTF_K_STRUCT && kind != CTF_K_UNION) {
1822 xyerror(D_OFFSETOF_TYPE,
1823 "offsetof operand must be a struct or union type\n");
1824 }
1825
1826 if (ctf_member_info(dtt.dtt_ctfp, type, name, &ctm) == CTF_ERR) {
1827 xyerror(D_UNKNOWN, "failed to determine offset of %s: %s\n",
1828 name, ctf_errmsg(ctf_errno(dtt.dtt_ctfp)));
1829 }
1830
1831 bzero(&dn, sizeof (dn));
1794 dt_node_type_assign(&dn, dtt.dtt_ctfp, ctm.ctm_type);
1832 dt_node_type_assign(&dn, dtt.dtt_ctfp, ctm.ctm_type, B_FALSE);
1795
1796 if (dn.dn_flags & DT_NF_BITFIELD) {
1797 xyerror(D_OFFSETOF_BITFIELD,
1798 "cannot take offset of a bit-field: %s\n", name);
1799 }
1800
1801 return (dt_node_int(ctm.ctm_offset / NBBY));
1802}
1803
1804dt_node_t *
1805dt_node_op1(int op, dt_node_t *cp)
1806{
1807 dt_node_t *dnp;
1808
1809 if (cp->dn_kind == DT_NODE_INT) {
1810 switch (op) {
1811 case DT_TOK_INEG:
1812 /*
1813 * If we're negating an unsigned integer, zero out any
1814 * extra top bits to truncate the value to the size of
1815 * the effective type determined by dt_node_int().
1816 */
1817 cp->dn_value = -cp->dn_value;
1818 if (!(cp->dn_flags & DT_NF_SIGNED)) {
1819 cp->dn_value &= ~0ULL >>
1820 (64 - dt_node_type_size(cp) * NBBY);
1821 }
1822 /*FALLTHRU*/
1823 case DT_TOK_IPOS:
1824 return (cp);
1825 case DT_TOK_BNEG:
1826 cp->dn_value = ~cp->dn_value;
1827 return (cp);
1828 case DT_TOK_LNEG:
1829 cp->dn_value = !cp->dn_value;
1830 return (cp);
1831 }
1832 }
1833
1834 /*
1835 * If sizeof is applied to a type_name or string constant, we can
1836 * transform 'cp' into an integer constant in the node construction
1837 * pass so that it can then be used for arithmetic in this pass.
1838 */
1839 if (op == DT_TOK_SIZEOF &&
1840 (cp->dn_kind == DT_NODE_STRING || cp->dn_kind == DT_NODE_TYPE)) {
1841 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
1842 size_t size = dt_node_type_size(cp);
1843
1844 if (size == 0) {
1845 xyerror(D_SIZEOF_TYPE, "cannot apply sizeof to an "
1846 "operand of unknown size\n");
1847 }
1848
1849 dt_node_type_assign(cp, dtp->dt_ddefs->dm_ctfp,
1833
1834 if (dn.dn_flags & DT_NF_BITFIELD) {
1835 xyerror(D_OFFSETOF_BITFIELD,
1836 "cannot take offset of a bit-field: %s\n", name);
1837 }
1838
1839 return (dt_node_int(ctm.ctm_offset / NBBY));
1840}
1841
1842dt_node_t *
1843dt_node_op1(int op, dt_node_t *cp)
1844{
1845 dt_node_t *dnp;
1846
1847 if (cp->dn_kind == DT_NODE_INT) {
1848 switch (op) {
1849 case DT_TOK_INEG:
1850 /*
1851 * If we're negating an unsigned integer, zero out any
1852 * extra top bits to truncate the value to the size of
1853 * the effective type determined by dt_node_int().
1854 */
1855 cp->dn_value = -cp->dn_value;
1856 if (!(cp->dn_flags & DT_NF_SIGNED)) {
1857 cp->dn_value &= ~0ULL >>
1858 (64 - dt_node_type_size(cp) * NBBY);
1859 }
1860 /*FALLTHRU*/
1861 case DT_TOK_IPOS:
1862 return (cp);
1863 case DT_TOK_BNEG:
1864 cp->dn_value = ~cp->dn_value;
1865 return (cp);
1866 case DT_TOK_LNEG:
1867 cp->dn_value = !cp->dn_value;
1868 return (cp);
1869 }
1870 }
1871
1872 /*
1873 * If sizeof is applied to a type_name or string constant, we can
1874 * transform 'cp' into an integer constant in the node construction
1875 * pass so that it can then be used for arithmetic in this pass.
1876 */
1877 if (op == DT_TOK_SIZEOF &&
1878 (cp->dn_kind == DT_NODE_STRING || cp->dn_kind == DT_NODE_TYPE)) {
1879 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
1880 size_t size = dt_node_type_size(cp);
1881
1882 if (size == 0) {
1883 xyerror(D_SIZEOF_TYPE, "cannot apply sizeof to an "
1884 "operand of unknown size\n");
1885 }
1886
1887 dt_node_type_assign(cp, dtp->dt_ddefs->dm_ctfp,
1850 ctf_lookup_by_name(dtp->dt_ddefs->dm_ctfp, "size_t"));
1888 ctf_lookup_by_name(dtp->dt_ddefs->dm_ctfp, "size_t"),
1889 B_FALSE);
1851
1852 cp->dn_kind = DT_NODE_INT;
1853 cp->dn_op = DT_TOK_INT;
1854 cp->dn_value = size;
1855
1856 return (cp);
1857 }
1858
1859 dnp = dt_node_alloc(DT_NODE_OP1);
1860 assert(op <= USHRT_MAX);
1861 dnp->dn_op = (ushort_t)op;
1862 dnp->dn_child = cp;
1863
1864 return (dnp);
1865}
1866
1867/*
1868 * If an integer constant is being cast to another integer type, we can
1869 * perform the cast as part of integer constant folding in this pass. We must
1870 * take action when the integer is being cast to a smaller type or if it is
1871 * changing signed-ness. If so, we first shift rp's bits bits high (losing
1872 * excess bits if narrowing) and then shift them down with either a logical
1873 * shift (unsigned) or arithmetic shift (signed).
1874 */
1875static void
1876dt_cast(dt_node_t *lp, dt_node_t *rp)
1877{
1878 size_t srcsize = dt_node_type_size(rp);
1879 size_t dstsize = dt_node_type_size(lp);
1880
1881 if (dstsize < srcsize) {
1882 int n = (sizeof (uint64_t) - dstsize) * NBBY;
1883 rp->dn_value <<= n;
1884 rp->dn_value >>= n;
1885 } else if (dstsize > srcsize) {
1886 int n = (sizeof (uint64_t) - srcsize) * NBBY;
1887 int s = (dstsize - srcsize) * NBBY;
1888
1889 rp->dn_value <<= n;
1890 if (rp->dn_flags & DT_NF_SIGNED) {
1891 rp->dn_value = (intmax_t)rp->dn_value >> s;
1892 rp->dn_value >>= n - s;
1893 } else {
1894 rp->dn_value >>= n;
1895 }
1896 }
1897}
1898
1899dt_node_t *
1900dt_node_op2(int op, dt_node_t *lp, dt_node_t *rp)
1901{
1902 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
1903 dt_node_t *dnp;
1904
1905 /*
1906 * First we check for operations that are illegal -- namely those that
1907 * might result in integer division by zero, and abort if one is found.
1908 */
1909 if (rp->dn_kind == DT_NODE_INT && rp->dn_value == 0 &&
1910 (op == DT_TOK_MOD || op == DT_TOK_DIV ||
1911 op == DT_TOK_MOD_EQ || op == DT_TOK_DIV_EQ))
1912 xyerror(D_DIV_ZERO, "expression contains division by zero\n");
1913
1914 /*
1915 * If both children are immediate values, we can just perform inline
1916 * calculation and return a new immediate node with the result.
1917 */
1918 if (lp->dn_kind == DT_NODE_INT && rp->dn_kind == DT_NODE_INT) {
1919 uintmax_t l = lp->dn_value;
1920 uintmax_t r = rp->dn_value;
1921
1922 dnp = dt_node_int(0); /* allocate new integer node for result */
1923
1924 switch (op) {
1925 case DT_TOK_LOR:
1926 dnp->dn_value = l || r;
1927 dt_node_type_assign(dnp,
1890
1891 cp->dn_kind = DT_NODE_INT;
1892 cp->dn_op = DT_TOK_INT;
1893 cp->dn_value = size;
1894
1895 return (cp);
1896 }
1897
1898 dnp = dt_node_alloc(DT_NODE_OP1);
1899 assert(op <= USHRT_MAX);
1900 dnp->dn_op = (ushort_t)op;
1901 dnp->dn_child = cp;
1902
1903 return (dnp);
1904}
1905
1906/*
1907 * If an integer constant is being cast to another integer type, we can
1908 * perform the cast as part of integer constant folding in this pass. We must
1909 * take action when the integer is being cast to a smaller type or if it is
1910 * changing signed-ness. If so, we first shift rp's bits bits high (losing
1911 * excess bits if narrowing) and then shift them down with either a logical
1912 * shift (unsigned) or arithmetic shift (signed).
1913 */
1914static void
1915dt_cast(dt_node_t *lp, dt_node_t *rp)
1916{
1917 size_t srcsize = dt_node_type_size(rp);
1918 size_t dstsize = dt_node_type_size(lp);
1919
1920 if (dstsize < srcsize) {
1921 int n = (sizeof (uint64_t) - dstsize) * NBBY;
1922 rp->dn_value <<= n;
1923 rp->dn_value >>= n;
1924 } else if (dstsize > srcsize) {
1925 int n = (sizeof (uint64_t) - srcsize) * NBBY;
1926 int s = (dstsize - srcsize) * NBBY;
1927
1928 rp->dn_value <<= n;
1929 if (rp->dn_flags & DT_NF_SIGNED) {
1930 rp->dn_value = (intmax_t)rp->dn_value >> s;
1931 rp->dn_value >>= n - s;
1932 } else {
1933 rp->dn_value >>= n;
1934 }
1935 }
1936}
1937
1938dt_node_t *
1939dt_node_op2(int op, dt_node_t *lp, dt_node_t *rp)
1940{
1941 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
1942 dt_node_t *dnp;
1943
1944 /*
1945 * First we check for operations that are illegal -- namely those that
1946 * might result in integer division by zero, and abort if one is found.
1947 */
1948 if (rp->dn_kind == DT_NODE_INT && rp->dn_value == 0 &&
1949 (op == DT_TOK_MOD || op == DT_TOK_DIV ||
1950 op == DT_TOK_MOD_EQ || op == DT_TOK_DIV_EQ))
1951 xyerror(D_DIV_ZERO, "expression contains division by zero\n");
1952
1953 /*
1954 * If both children are immediate values, we can just perform inline
1955 * calculation and return a new immediate node with the result.
1956 */
1957 if (lp->dn_kind == DT_NODE_INT && rp->dn_kind == DT_NODE_INT) {
1958 uintmax_t l = lp->dn_value;
1959 uintmax_t r = rp->dn_value;
1960
1961 dnp = dt_node_int(0); /* allocate new integer node for result */
1962
1963 switch (op) {
1964 case DT_TOK_LOR:
1965 dnp->dn_value = l || r;
1966 dt_node_type_assign(dnp,
1928 DT_INT_CTFP(dtp), DT_INT_TYPE(dtp));
1967 DT_INT_CTFP(dtp), DT_INT_TYPE(dtp), B_FALSE);
1929 break;
1930 case DT_TOK_LXOR:
1931 dnp->dn_value = (l != 0) ^ (r != 0);
1932 dt_node_type_assign(dnp,
1968 break;
1969 case DT_TOK_LXOR:
1970 dnp->dn_value = (l != 0) ^ (r != 0);
1971 dt_node_type_assign(dnp,
1933 DT_INT_CTFP(dtp), DT_INT_TYPE(dtp));
1972 DT_INT_CTFP(dtp), DT_INT_TYPE(dtp), B_FALSE);
1934 break;
1935 case DT_TOK_LAND:
1936 dnp->dn_value = l && r;
1937 dt_node_type_assign(dnp,
1973 break;
1974 case DT_TOK_LAND:
1975 dnp->dn_value = l && r;
1976 dt_node_type_assign(dnp,
1938 DT_INT_CTFP(dtp), DT_INT_TYPE(dtp));
1977 DT_INT_CTFP(dtp), DT_INT_TYPE(dtp), B_FALSE);
1939 break;
1940 case DT_TOK_BOR:
1941 dnp->dn_value = l | r;
1942 dt_node_promote(lp, rp, dnp);
1943 break;
1944 case DT_TOK_XOR:
1945 dnp->dn_value = l ^ r;
1946 dt_node_promote(lp, rp, dnp);
1947 break;
1948 case DT_TOK_BAND:
1949 dnp->dn_value = l & r;
1950 dt_node_promote(lp, rp, dnp);
1951 break;
1952 case DT_TOK_EQU:
1953 dnp->dn_value = l == r;
1954 dt_node_type_assign(dnp,
1978 break;
1979 case DT_TOK_BOR:
1980 dnp->dn_value = l | r;
1981 dt_node_promote(lp, rp, dnp);
1982 break;
1983 case DT_TOK_XOR:
1984 dnp->dn_value = l ^ r;
1985 dt_node_promote(lp, rp, dnp);
1986 break;
1987 case DT_TOK_BAND:
1988 dnp->dn_value = l & r;
1989 dt_node_promote(lp, rp, dnp);
1990 break;
1991 case DT_TOK_EQU:
1992 dnp->dn_value = l == r;
1993 dt_node_type_assign(dnp,
1955 DT_INT_CTFP(dtp), DT_INT_TYPE(dtp));
1994 DT_INT_CTFP(dtp), DT_INT_TYPE(dtp), B_FALSE);
1956 break;
1957 case DT_TOK_NEQ:
1958 dnp->dn_value = l != r;
1959 dt_node_type_assign(dnp,
1995 break;
1996 case DT_TOK_NEQ:
1997 dnp->dn_value = l != r;
1998 dt_node_type_assign(dnp,
1960 DT_INT_CTFP(dtp), DT_INT_TYPE(dtp));
1999 DT_INT_CTFP(dtp), DT_INT_TYPE(dtp), B_FALSE);
1961 break;
1962 case DT_TOK_LT:
1963 dt_node_promote(lp, rp, dnp);
1964 if (dnp->dn_flags & DT_NF_SIGNED)
1965 dnp->dn_value = (intmax_t)l < (intmax_t)r;
1966 else
1967 dnp->dn_value = l < r;
1968 dt_node_type_assign(dnp,
2000 break;
2001 case DT_TOK_LT:
2002 dt_node_promote(lp, rp, dnp);
2003 if (dnp->dn_flags & DT_NF_SIGNED)
2004 dnp->dn_value = (intmax_t)l < (intmax_t)r;
2005 else
2006 dnp->dn_value = l < r;
2007 dt_node_type_assign(dnp,
1969 DT_INT_CTFP(dtp), DT_INT_TYPE(dtp));
2008 DT_INT_CTFP(dtp), DT_INT_TYPE(dtp), B_FALSE);
1970 break;
1971 case DT_TOK_LE:
1972 dt_node_promote(lp, rp, dnp);
1973 if (dnp->dn_flags & DT_NF_SIGNED)
1974 dnp->dn_value = (intmax_t)l <= (intmax_t)r;
1975 else
1976 dnp->dn_value = l <= r;
1977 dt_node_type_assign(dnp,
2009 break;
2010 case DT_TOK_LE:
2011 dt_node_promote(lp, rp, dnp);
2012 if (dnp->dn_flags & DT_NF_SIGNED)
2013 dnp->dn_value = (intmax_t)l <= (intmax_t)r;
2014 else
2015 dnp->dn_value = l <= r;
2016 dt_node_type_assign(dnp,
1978 DT_INT_CTFP(dtp), DT_INT_TYPE(dtp));
2017 DT_INT_CTFP(dtp), DT_INT_TYPE(dtp), B_FALSE);
1979 break;
1980 case DT_TOK_GT:
1981 dt_node_promote(lp, rp, dnp);
1982 if (dnp->dn_flags & DT_NF_SIGNED)
1983 dnp->dn_value = (intmax_t)l > (intmax_t)r;
1984 else
1985 dnp->dn_value = l > r;
1986 dt_node_type_assign(dnp,
2018 break;
2019 case DT_TOK_GT:
2020 dt_node_promote(lp, rp, dnp);
2021 if (dnp->dn_flags & DT_NF_SIGNED)
2022 dnp->dn_value = (intmax_t)l > (intmax_t)r;
2023 else
2024 dnp->dn_value = l > r;
2025 dt_node_type_assign(dnp,
1987 DT_INT_CTFP(dtp), DT_INT_TYPE(dtp));
2026 DT_INT_CTFP(dtp), DT_INT_TYPE(dtp), B_FALSE);
1988 break;
1989 case DT_TOK_GE:
1990 dt_node_promote(lp, rp, dnp);
1991 if (dnp->dn_flags & DT_NF_SIGNED)
1992 dnp->dn_value = (intmax_t)l >= (intmax_t)r;
1993 else
1994 dnp->dn_value = l >= r;
1995 dt_node_type_assign(dnp,
2027 break;
2028 case DT_TOK_GE:
2029 dt_node_promote(lp, rp, dnp);
2030 if (dnp->dn_flags & DT_NF_SIGNED)
2031 dnp->dn_value = (intmax_t)l >= (intmax_t)r;
2032 else
2033 dnp->dn_value = l >= r;
2034 dt_node_type_assign(dnp,
1996 DT_INT_CTFP(dtp), DT_INT_TYPE(dtp));
2035 DT_INT_CTFP(dtp), DT_INT_TYPE(dtp), B_FALSE);
1997 break;
1998 case DT_TOK_LSH:
1999 dnp->dn_value = l << r;
2000 dt_node_type_propagate(lp, dnp);
2001 dt_node_attr_assign(rp,
2002 dt_attr_min(lp->dn_attr, rp->dn_attr));
2003 break;
2004 case DT_TOK_RSH:
2005 dnp->dn_value = l >> r;
2006 dt_node_type_propagate(lp, dnp);
2007 dt_node_attr_assign(rp,
2008 dt_attr_min(lp->dn_attr, rp->dn_attr));
2009 break;
2010 case DT_TOK_ADD:
2011 dnp->dn_value = l + r;
2012 dt_node_promote(lp, rp, dnp);
2013 break;
2014 case DT_TOK_SUB:
2015 dnp->dn_value = l - r;
2016 dt_node_promote(lp, rp, dnp);
2017 break;
2018 case DT_TOK_MUL:
2019 dnp->dn_value = l * r;
2020 dt_node_promote(lp, rp, dnp);
2021 break;
2022 case DT_TOK_DIV:
2023 dt_node_promote(lp, rp, dnp);
2024 if (dnp->dn_flags & DT_NF_SIGNED)
2025 dnp->dn_value = (intmax_t)l / (intmax_t)r;
2026 else
2027 dnp->dn_value = l / r;
2028 break;
2029 case DT_TOK_MOD:
2030 dt_node_promote(lp, rp, dnp);
2031 if (dnp->dn_flags & DT_NF_SIGNED)
2032 dnp->dn_value = (intmax_t)l % (intmax_t)r;
2033 else
2034 dnp->dn_value = l % r;
2035 break;
2036 default:
2037 dt_node_free(dnp);
2038 dnp = NULL;
2039 }
2040
2041 if (dnp != NULL) {
2042 dt_node_free(lp);
2043 dt_node_free(rp);
2044 return (dnp);
2045 }
2046 }
2047
2048 if (op == DT_TOK_LPAR && rp->dn_kind == DT_NODE_INT &&
2049 dt_node_is_integer(lp)) {
2050 dt_cast(lp, rp);
2051 dt_node_type_propagate(lp, rp);
2052 dt_node_attr_assign(rp, dt_attr_min(lp->dn_attr, rp->dn_attr));
2053 dt_node_free(lp);
2054
2055 return (rp);
2056 }
2057
2058 /*
2059 * If no immediate optimizations are available, create an new OP2 node
2060 * and glue the left and right children into place and return.
2061 */
2062 dnp = dt_node_alloc(DT_NODE_OP2);
2063 assert(op <= USHRT_MAX);
2064 dnp->dn_op = (ushort_t)op;
2065 dnp->dn_left = lp;
2066 dnp->dn_right = rp;
2067
2068 return (dnp);
2069}
2070
2071dt_node_t *
2072dt_node_op3(dt_node_t *expr, dt_node_t *lp, dt_node_t *rp)
2073{
2074 dt_node_t *dnp;
2075
2076 if (expr->dn_kind == DT_NODE_INT)
2077 return (expr->dn_value != 0 ? lp : rp);
2078
2079 dnp = dt_node_alloc(DT_NODE_OP3);
2080 dnp->dn_op = DT_TOK_QUESTION;
2081 dnp->dn_expr = expr;
2082 dnp->dn_left = lp;
2083 dnp->dn_right = rp;
2084
2085 return (dnp);
2086}
2087
2088dt_node_t *
2089dt_node_statement(dt_node_t *expr)
2090{
2091 dt_node_t *dnp;
2092
2093 if (expr->dn_kind == DT_NODE_AGG)
2094 return (expr);
2095
2096 if (expr->dn_kind == DT_NODE_FUNC &&
2097 expr->dn_ident->di_kind == DT_IDENT_ACTFUNC)
2098 dnp = dt_node_alloc(DT_NODE_DFUNC);
2099 else
2100 dnp = dt_node_alloc(DT_NODE_DEXPR);
2101
2102 dnp->dn_expr = expr;
2103 return (dnp);
2104}
2105
2106dt_node_t *
2107dt_node_pdesc_by_name(char *spec)
2108{
2109 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
2110 dt_node_t *dnp;
2111
2112 if (spec == NULL)
2113 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2114
2115 dnp = dt_node_alloc(DT_NODE_PDESC);
2116 dnp->dn_spec = spec;
2117 dnp->dn_desc = malloc(sizeof (dtrace_probedesc_t));
2118
2119 if (dnp->dn_desc == NULL)
2120 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2121
2122 if (dtrace_xstr2desc(dtp, yypcb->pcb_pspec, dnp->dn_spec,
2123 yypcb->pcb_sargc, yypcb->pcb_sargv, dnp->dn_desc) != 0) {
2124 xyerror(D_PDESC_INVAL, "invalid probe description \"%s\": %s\n",
2125 dnp->dn_spec, dtrace_errmsg(dtp, dtrace_errno(dtp)));
2126 }
2127
2128 free(dnp->dn_spec);
2129 dnp->dn_spec = NULL;
2130
2131 return (dnp);
2132}
2133
2134dt_node_t *
2135dt_node_pdesc_by_id(uintmax_t id)
2136{
2137 static const char *const names[] = {
2138 "providers", "modules", "functions"
2139 };
2140
2141 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
2142 dt_node_t *dnp = dt_node_alloc(DT_NODE_PDESC);
2143
2144 if ((dnp->dn_desc = malloc(sizeof (dtrace_probedesc_t))) == NULL)
2145 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2146
2147 if (id > UINT_MAX) {
2148 xyerror(D_PDESC_INVAL, "identifier %llu exceeds maximum "
2149 "probe id\n", (u_longlong_t)id);
2150 }
2151
2152 if (yypcb->pcb_pspec != DTRACE_PROBESPEC_NAME) {
2153 xyerror(D_PDESC_INVAL, "probe identifier %llu not permitted "
2154 "when specifying %s\n", (u_longlong_t)id,
2155 names[yypcb->pcb_pspec]);
2156 }
2157
2158 if (dtrace_id2desc(dtp, (dtrace_id_t)id, dnp->dn_desc) != 0) {
2159 xyerror(D_PDESC_INVAL, "invalid probe identifier %llu: %s\n",
2160 (u_longlong_t)id, dtrace_errmsg(dtp, dtrace_errno(dtp)));
2161 }
2162
2163 return (dnp);
2164}
2165
2166dt_node_t *
2167dt_node_clause(dt_node_t *pdescs, dt_node_t *pred, dt_node_t *acts)
2168{
2169 dt_node_t *dnp = dt_node_alloc(DT_NODE_CLAUSE);
2170
2171 dnp->dn_pdescs = pdescs;
2172 dnp->dn_pred = pred;
2173 dnp->dn_acts = acts;
2174
2175 yybegin(YYS_CLAUSE);
2176 return (dnp);
2177}
2178
2179dt_node_t *
2180dt_node_inline(dt_node_t *expr)
2181{
2182 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
2183 dt_scope_t *dsp = &yypcb->pcb_dstack;
2184 dt_decl_t *ddp = dt_decl_top();
2185
2186 char n[DT_TYPE_NAMELEN];
2187 dtrace_typeinfo_t dtt;
2188
2189 dt_ident_t *idp, *rdp;
2190 dt_idnode_t *inp;
2191 dt_node_t *dnp;
2192
2193 if (dt_decl_type(ddp, &dtt) != 0)
2194 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
2195
2196 if (dsp->ds_class != DT_DC_DEFAULT) {
2197 xyerror(D_DECL_BADCLASS, "specified storage class not "
2198 "appropriate for inline declaration\n");
2199 }
2200
2201 if (dsp->ds_ident == NULL)
2202 xyerror(D_DECL_USELESS, "inline declaration requires a name\n");
2203
2204 if ((idp = dt_idstack_lookup(
2205 &yypcb->pcb_globals, dsp->ds_ident)) != NULL) {
2206 xyerror(D_DECL_IDRED, "identifier redefined: %s\n\t current: "
2207 "inline definition\n\tprevious: %s %s\n",
2208 idp->di_name, dt_idkind_name(idp->di_kind),
2209 (idp->di_flags & DT_IDFLG_INLINE) ? "inline" : "");
2210 }
2211
2212 /*
2213 * If we are declaring an inlined array, verify that we have a tuple
2214 * signature, and then recompute 'dtt' as the array's value type.
2215 */
2216 if (ddp->dd_kind == CTF_K_ARRAY) {
2217 if (ddp->dd_node == NULL) {
2218 xyerror(D_DECL_ARRNULL, "inline declaration requires "
2219 "array tuple signature: %s\n", dsp->ds_ident);
2220 }
2221
2222 if (ddp->dd_node->dn_kind != DT_NODE_TYPE) {
2223 xyerror(D_DECL_ARRNULL, "inline declaration cannot be "
2224 "of scalar array type: %s\n", dsp->ds_ident);
2225 }
2226
2227 if (dt_decl_type(ddp->dd_next, &dtt) != 0)
2228 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
2229 }
2230
2231 /*
2232 * If the inline identifier is not defined, then create it with the
2233 * orphan flag set. We do not insert the identifier into dt_globals
2234 * until we have successfully cooked the right-hand expression, below.
2235 */
2236 dnp = dt_node_alloc(DT_NODE_INLINE);
2036 break;
2037 case DT_TOK_LSH:
2038 dnp->dn_value = l << r;
2039 dt_node_type_propagate(lp, dnp);
2040 dt_node_attr_assign(rp,
2041 dt_attr_min(lp->dn_attr, rp->dn_attr));
2042 break;
2043 case DT_TOK_RSH:
2044 dnp->dn_value = l >> r;
2045 dt_node_type_propagate(lp, dnp);
2046 dt_node_attr_assign(rp,
2047 dt_attr_min(lp->dn_attr, rp->dn_attr));
2048 break;
2049 case DT_TOK_ADD:
2050 dnp->dn_value = l + r;
2051 dt_node_promote(lp, rp, dnp);
2052 break;
2053 case DT_TOK_SUB:
2054 dnp->dn_value = l - r;
2055 dt_node_promote(lp, rp, dnp);
2056 break;
2057 case DT_TOK_MUL:
2058 dnp->dn_value = l * r;
2059 dt_node_promote(lp, rp, dnp);
2060 break;
2061 case DT_TOK_DIV:
2062 dt_node_promote(lp, rp, dnp);
2063 if (dnp->dn_flags & DT_NF_SIGNED)
2064 dnp->dn_value = (intmax_t)l / (intmax_t)r;
2065 else
2066 dnp->dn_value = l / r;
2067 break;
2068 case DT_TOK_MOD:
2069 dt_node_promote(lp, rp, dnp);
2070 if (dnp->dn_flags & DT_NF_SIGNED)
2071 dnp->dn_value = (intmax_t)l % (intmax_t)r;
2072 else
2073 dnp->dn_value = l % r;
2074 break;
2075 default:
2076 dt_node_free(dnp);
2077 dnp = NULL;
2078 }
2079
2080 if (dnp != NULL) {
2081 dt_node_free(lp);
2082 dt_node_free(rp);
2083 return (dnp);
2084 }
2085 }
2086
2087 if (op == DT_TOK_LPAR && rp->dn_kind == DT_NODE_INT &&
2088 dt_node_is_integer(lp)) {
2089 dt_cast(lp, rp);
2090 dt_node_type_propagate(lp, rp);
2091 dt_node_attr_assign(rp, dt_attr_min(lp->dn_attr, rp->dn_attr));
2092 dt_node_free(lp);
2093
2094 return (rp);
2095 }
2096
2097 /*
2098 * If no immediate optimizations are available, create an new OP2 node
2099 * and glue the left and right children into place and return.
2100 */
2101 dnp = dt_node_alloc(DT_NODE_OP2);
2102 assert(op <= USHRT_MAX);
2103 dnp->dn_op = (ushort_t)op;
2104 dnp->dn_left = lp;
2105 dnp->dn_right = rp;
2106
2107 return (dnp);
2108}
2109
2110dt_node_t *
2111dt_node_op3(dt_node_t *expr, dt_node_t *lp, dt_node_t *rp)
2112{
2113 dt_node_t *dnp;
2114
2115 if (expr->dn_kind == DT_NODE_INT)
2116 return (expr->dn_value != 0 ? lp : rp);
2117
2118 dnp = dt_node_alloc(DT_NODE_OP3);
2119 dnp->dn_op = DT_TOK_QUESTION;
2120 dnp->dn_expr = expr;
2121 dnp->dn_left = lp;
2122 dnp->dn_right = rp;
2123
2124 return (dnp);
2125}
2126
2127dt_node_t *
2128dt_node_statement(dt_node_t *expr)
2129{
2130 dt_node_t *dnp;
2131
2132 if (expr->dn_kind == DT_NODE_AGG)
2133 return (expr);
2134
2135 if (expr->dn_kind == DT_NODE_FUNC &&
2136 expr->dn_ident->di_kind == DT_IDENT_ACTFUNC)
2137 dnp = dt_node_alloc(DT_NODE_DFUNC);
2138 else
2139 dnp = dt_node_alloc(DT_NODE_DEXPR);
2140
2141 dnp->dn_expr = expr;
2142 return (dnp);
2143}
2144
2145dt_node_t *
2146dt_node_pdesc_by_name(char *spec)
2147{
2148 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
2149 dt_node_t *dnp;
2150
2151 if (spec == NULL)
2152 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2153
2154 dnp = dt_node_alloc(DT_NODE_PDESC);
2155 dnp->dn_spec = spec;
2156 dnp->dn_desc = malloc(sizeof (dtrace_probedesc_t));
2157
2158 if (dnp->dn_desc == NULL)
2159 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2160
2161 if (dtrace_xstr2desc(dtp, yypcb->pcb_pspec, dnp->dn_spec,
2162 yypcb->pcb_sargc, yypcb->pcb_sargv, dnp->dn_desc) != 0) {
2163 xyerror(D_PDESC_INVAL, "invalid probe description \"%s\": %s\n",
2164 dnp->dn_spec, dtrace_errmsg(dtp, dtrace_errno(dtp)));
2165 }
2166
2167 free(dnp->dn_spec);
2168 dnp->dn_spec = NULL;
2169
2170 return (dnp);
2171}
2172
2173dt_node_t *
2174dt_node_pdesc_by_id(uintmax_t id)
2175{
2176 static const char *const names[] = {
2177 "providers", "modules", "functions"
2178 };
2179
2180 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
2181 dt_node_t *dnp = dt_node_alloc(DT_NODE_PDESC);
2182
2183 if ((dnp->dn_desc = malloc(sizeof (dtrace_probedesc_t))) == NULL)
2184 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2185
2186 if (id > UINT_MAX) {
2187 xyerror(D_PDESC_INVAL, "identifier %llu exceeds maximum "
2188 "probe id\n", (u_longlong_t)id);
2189 }
2190
2191 if (yypcb->pcb_pspec != DTRACE_PROBESPEC_NAME) {
2192 xyerror(D_PDESC_INVAL, "probe identifier %llu not permitted "
2193 "when specifying %s\n", (u_longlong_t)id,
2194 names[yypcb->pcb_pspec]);
2195 }
2196
2197 if (dtrace_id2desc(dtp, (dtrace_id_t)id, dnp->dn_desc) != 0) {
2198 xyerror(D_PDESC_INVAL, "invalid probe identifier %llu: %s\n",
2199 (u_longlong_t)id, dtrace_errmsg(dtp, dtrace_errno(dtp)));
2200 }
2201
2202 return (dnp);
2203}
2204
2205dt_node_t *
2206dt_node_clause(dt_node_t *pdescs, dt_node_t *pred, dt_node_t *acts)
2207{
2208 dt_node_t *dnp = dt_node_alloc(DT_NODE_CLAUSE);
2209
2210 dnp->dn_pdescs = pdescs;
2211 dnp->dn_pred = pred;
2212 dnp->dn_acts = acts;
2213
2214 yybegin(YYS_CLAUSE);
2215 return (dnp);
2216}
2217
2218dt_node_t *
2219dt_node_inline(dt_node_t *expr)
2220{
2221 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
2222 dt_scope_t *dsp = &yypcb->pcb_dstack;
2223 dt_decl_t *ddp = dt_decl_top();
2224
2225 char n[DT_TYPE_NAMELEN];
2226 dtrace_typeinfo_t dtt;
2227
2228 dt_ident_t *idp, *rdp;
2229 dt_idnode_t *inp;
2230 dt_node_t *dnp;
2231
2232 if (dt_decl_type(ddp, &dtt) != 0)
2233 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
2234
2235 if (dsp->ds_class != DT_DC_DEFAULT) {
2236 xyerror(D_DECL_BADCLASS, "specified storage class not "
2237 "appropriate for inline declaration\n");
2238 }
2239
2240 if (dsp->ds_ident == NULL)
2241 xyerror(D_DECL_USELESS, "inline declaration requires a name\n");
2242
2243 if ((idp = dt_idstack_lookup(
2244 &yypcb->pcb_globals, dsp->ds_ident)) != NULL) {
2245 xyerror(D_DECL_IDRED, "identifier redefined: %s\n\t current: "
2246 "inline definition\n\tprevious: %s %s\n",
2247 idp->di_name, dt_idkind_name(idp->di_kind),
2248 (idp->di_flags & DT_IDFLG_INLINE) ? "inline" : "");
2249 }
2250
2251 /*
2252 * If we are declaring an inlined array, verify that we have a tuple
2253 * signature, and then recompute 'dtt' as the array's value type.
2254 */
2255 if (ddp->dd_kind == CTF_K_ARRAY) {
2256 if (ddp->dd_node == NULL) {
2257 xyerror(D_DECL_ARRNULL, "inline declaration requires "
2258 "array tuple signature: %s\n", dsp->ds_ident);
2259 }
2260
2261 if (ddp->dd_node->dn_kind != DT_NODE_TYPE) {
2262 xyerror(D_DECL_ARRNULL, "inline declaration cannot be "
2263 "of scalar array type: %s\n", dsp->ds_ident);
2264 }
2265
2266 if (dt_decl_type(ddp->dd_next, &dtt) != 0)
2267 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
2268 }
2269
2270 /*
2271 * If the inline identifier is not defined, then create it with the
2272 * orphan flag set. We do not insert the identifier into dt_globals
2273 * until we have successfully cooked the right-hand expression, below.
2274 */
2275 dnp = dt_node_alloc(DT_NODE_INLINE);
2237 dt_node_type_assign(dnp, dtt.dtt_ctfp, dtt.dtt_type);
2276 dt_node_type_assign(dnp, dtt.dtt_ctfp, dtt.dtt_type, B_FALSE);
2238 dt_node_attr_assign(dnp, _dtrace_defattr);
2239
2240 if (dt_node_is_void(dnp)) {
2241 xyerror(D_DECL_VOIDOBJ,
2242 "cannot declare void inline: %s\n", dsp->ds_ident);
2243 }
2244
2245 if (ctf_type_kind(dnp->dn_ctfp, ctf_type_resolve(
2246 dnp->dn_ctfp, dnp->dn_type)) == CTF_K_FORWARD) {
2247 xyerror(D_DECL_INCOMPLETE,
2248 "incomplete struct/union/enum %s: %s\n",
2249 dt_node_type_name(dnp, n, sizeof (n)), dsp->ds_ident);
2250 }
2251
2252 if ((inp = malloc(sizeof (dt_idnode_t))) == NULL)
2253 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2254
2255 bzero(inp, sizeof (dt_idnode_t));
2256
2257 idp = dnp->dn_ident = dt_ident_create(dsp->ds_ident,
2258 ddp->dd_kind == CTF_K_ARRAY ? DT_IDENT_ARRAY : DT_IDENT_SCALAR,
2259 DT_IDFLG_INLINE | DT_IDFLG_REF | DT_IDFLG_DECL | DT_IDFLG_ORPHAN, 0,
2260 _dtrace_defattr, 0, &dt_idops_inline, inp, dtp->dt_gen);
2261
2262 if (idp == NULL) {
2263 free(inp);
2264 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2265 }
2266
2267 /*
2268 * If we're inlining an associative array, create a private identifier
2269 * hash containing the named parameters and store it in inp->din_hash.
2270 * We then push this hash on to the top of the pcb_globals stack.
2271 */
2272 if (ddp->dd_kind == CTF_K_ARRAY) {
2273 dt_idnode_t *pinp;
2274 dt_ident_t *pidp;
2275 dt_node_t *pnp;
2276 uint_t i = 0;
2277
2278 for (pnp = ddp->dd_node; pnp != NULL; pnp = pnp->dn_list)
2279 i++; /* count up parameters for din_argv[] */
2280
2281 inp->din_hash = dt_idhash_create("inline args", NULL, 0, 0);
2282 inp->din_argv = calloc(i, sizeof (dt_ident_t *));
2283
2284 if (inp->din_hash == NULL || inp->din_argv == NULL)
2285 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2286
2287 /*
2288 * Create an identifier for each parameter as a scalar inline,
2289 * and store it in din_hash and in position in din_argv[]. The
2290 * parameter identifiers also use dt_idops_inline, but we leave
2291 * the dt_idnode_t argument 'pinp' zeroed. This will be filled
2292 * in by the code generation pass with references to the args.
2293 */
2294 for (i = 0, pnp = ddp->dd_node;
2295 pnp != NULL; pnp = pnp->dn_list, i++) {
2296
2297 if (pnp->dn_string == NULL)
2298 continue; /* ignore anonymous parameters */
2299
2300 if ((pinp = malloc(sizeof (dt_idnode_t))) == NULL)
2301 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2302
2303 pidp = dt_idhash_insert(inp->din_hash, pnp->dn_string,
2304 DT_IDENT_SCALAR, DT_IDFLG_DECL | DT_IDFLG_INLINE, 0,
2305 _dtrace_defattr, 0, &dt_idops_inline,
2306 pinp, dtp->dt_gen);
2307
2308 if (pidp == NULL) {
2309 free(pinp);
2310 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2311 }
2312
2313 inp->din_argv[i] = pidp;
2314 bzero(pinp, sizeof (dt_idnode_t));
2315 dt_ident_type_assign(pidp, pnp->dn_ctfp, pnp->dn_type);
2316 }
2317
2318 dt_idstack_push(&yypcb->pcb_globals, inp->din_hash);
2319 }
2320
2321 /*
2322 * Unlike most constructors, we need to explicitly cook the right-hand
2323 * side of the inline definition immediately to prevent recursion. If
2324 * the right-hand side uses the inline itself, the cook will fail.
2325 */
2326 expr = dt_node_cook(expr, DT_IDFLG_REF);
2327
2328 if (ddp->dd_kind == CTF_K_ARRAY)
2329 dt_idstack_pop(&yypcb->pcb_globals, inp->din_hash);
2330
2331 /*
2332 * Set the type, attributes, and flags for the inline. If the right-
2333 * hand expression has an identifier, propagate its flags. Then cook
2334 * the identifier to fully initialize it: if we're declaring an inline
2335 * associative array this will construct a type signature from 'ddp'.
2336 */
2337 if (dt_node_is_dynamic(expr))
2338 rdp = dt_ident_resolve(expr->dn_ident);
2339 else if (expr->dn_kind == DT_NODE_VAR || expr->dn_kind == DT_NODE_SYM)
2340 rdp = expr->dn_ident;
2341 else
2342 rdp = NULL;
2343
2344 if (rdp != NULL) {
2345 idp->di_flags |= (rdp->di_flags &
2346 (DT_IDFLG_WRITE | DT_IDFLG_USER | DT_IDFLG_PRIM));
2347 }
2348
2349 idp->di_attr = dt_attr_min(_dtrace_defattr, expr->dn_attr);
2350 dt_ident_type_assign(idp, dtt.dtt_ctfp, dtt.dtt_type);
2351 (void) dt_ident_cook(dnp, idp, &ddp->dd_node);
2352
2353 /*
2354 * Store the parse tree nodes for 'expr' inside of idp->di_data ('inp')
2355 * so that they will be preserved with this identifier. Then pop the
2356 * inline declaration from the declaration stack and restore the lexer.
2357 */
2358 inp->din_list = yypcb->pcb_list;
2359 inp->din_root = expr;
2360
2361 dt_decl_free(dt_decl_pop());
2362 yybegin(YYS_CLAUSE);
2363
2364 /*
2365 * Finally, insert the inline identifier into dt_globals to make it
2366 * visible, and then cook 'dnp' to check its type against 'expr'.
2367 */
2368 dt_idhash_xinsert(dtp->dt_globals, idp);
2369 return (dt_node_cook(dnp, DT_IDFLG_REF));
2370}
2371
2372dt_node_t *
2373dt_node_member(dt_decl_t *ddp, char *name, dt_node_t *expr)
2374{
2375 dtrace_typeinfo_t dtt;
2376 dt_node_t *dnp;
2377 int err;
2378
2379 if (ddp != NULL) {
2380 err = dt_decl_type(ddp, &dtt);
2381 dt_decl_free(ddp);
2382
2383 if (err != 0)
2384 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
2385 }
2386
2387 dnp = dt_node_alloc(DT_NODE_MEMBER);
2388 dnp->dn_membname = name;
2389 dnp->dn_membexpr = expr;
2390
2391 if (ddp != NULL)
2277 dt_node_attr_assign(dnp, _dtrace_defattr);
2278
2279 if (dt_node_is_void(dnp)) {
2280 xyerror(D_DECL_VOIDOBJ,
2281 "cannot declare void inline: %s\n", dsp->ds_ident);
2282 }
2283
2284 if (ctf_type_kind(dnp->dn_ctfp, ctf_type_resolve(
2285 dnp->dn_ctfp, dnp->dn_type)) == CTF_K_FORWARD) {
2286 xyerror(D_DECL_INCOMPLETE,
2287 "incomplete struct/union/enum %s: %s\n",
2288 dt_node_type_name(dnp, n, sizeof (n)), dsp->ds_ident);
2289 }
2290
2291 if ((inp = malloc(sizeof (dt_idnode_t))) == NULL)
2292 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2293
2294 bzero(inp, sizeof (dt_idnode_t));
2295
2296 idp = dnp->dn_ident = dt_ident_create(dsp->ds_ident,
2297 ddp->dd_kind == CTF_K_ARRAY ? DT_IDENT_ARRAY : DT_IDENT_SCALAR,
2298 DT_IDFLG_INLINE | DT_IDFLG_REF | DT_IDFLG_DECL | DT_IDFLG_ORPHAN, 0,
2299 _dtrace_defattr, 0, &dt_idops_inline, inp, dtp->dt_gen);
2300
2301 if (idp == NULL) {
2302 free(inp);
2303 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2304 }
2305
2306 /*
2307 * If we're inlining an associative array, create a private identifier
2308 * hash containing the named parameters and store it in inp->din_hash.
2309 * We then push this hash on to the top of the pcb_globals stack.
2310 */
2311 if (ddp->dd_kind == CTF_K_ARRAY) {
2312 dt_idnode_t *pinp;
2313 dt_ident_t *pidp;
2314 dt_node_t *pnp;
2315 uint_t i = 0;
2316
2317 for (pnp = ddp->dd_node; pnp != NULL; pnp = pnp->dn_list)
2318 i++; /* count up parameters for din_argv[] */
2319
2320 inp->din_hash = dt_idhash_create("inline args", NULL, 0, 0);
2321 inp->din_argv = calloc(i, sizeof (dt_ident_t *));
2322
2323 if (inp->din_hash == NULL || inp->din_argv == NULL)
2324 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2325
2326 /*
2327 * Create an identifier for each parameter as a scalar inline,
2328 * and store it in din_hash and in position in din_argv[]. The
2329 * parameter identifiers also use dt_idops_inline, but we leave
2330 * the dt_idnode_t argument 'pinp' zeroed. This will be filled
2331 * in by the code generation pass with references to the args.
2332 */
2333 for (i = 0, pnp = ddp->dd_node;
2334 pnp != NULL; pnp = pnp->dn_list, i++) {
2335
2336 if (pnp->dn_string == NULL)
2337 continue; /* ignore anonymous parameters */
2338
2339 if ((pinp = malloc(sizeof (dt_idnode_t))) == NULL)
2340 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2341
2342 pidp = dt_idhash_insert(inp->din_hash, pnp->dn_string,
2343 DT_IDENT_SCALAR, DT_IDFLG_DECL | DT_IDFLG_INLINE, 0,
2344 _dtrace_defattr, 0, &dt_idops_inline,
2345 pinp, dtp->dt_gen);
2346
2347 if (pidp == NULL) {
2348 free(pinp);
2349 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2350 }
2351
2352 inp->din_argv[i] = pidp;
2353 bzero(pinp, sizeof (dt_idnode_t));
2354 dt_ident_type_assign(pidp, pnp->dn_ctfp, pnp->dn_type);
2355 }
2356
2357 dt_idstack_push(&yypcb->pcb_globals, inp->din_hash);
2358 }
2359
2360 /*
2361 * Unlike most constructors, we need to explicitly cook the right-hand
2362 * side of the inline definition immediately to prevent recursion. If
2363 * the right-hand side uses the inline itself, the cook will fail.
2364 */
2365 expr = dt_node_cook(expr, DT_IDFLG_REF);
2366
2367 if (ddp->dd_kind == CTF_K_ARRAY)
2368 dt_idstack_pop(&yypcb->pcb_globals, inp->din_hash);
2369
2370 /*
2371 * Set the type, attributes, and flags for the inline. If the right-
2372 * hand expression has an identifier, propagate its flags. Then cook
2373 * the identifier to fully initialize it: if we're declaring an inline
2374 * associative array this will construct a type signature from 'ddp'.
2375 */
2376 if (dt_node_is_dynamic(expr))
2377 rdp = dt_ident_resolve(expr->dn_ident);
2378 else if (expr->dn_kind == DT_NODE_VAR || expr->dn_kind == DT_NODE_SYM)
2379 rdp = expr->dn_ident;
2380 else
2381 rdp = NULL;
2382
2383 if (rdp != NULL) {
2384 idp->di_flags |= (rdp->di_flags &
2385 (DT_IDFLG_WRITE | DT_IDFLG_USER | DT_IDFLG_PRIM));
2386 }
2387
2388 idp->di_attr = dt_attr_min(_dtrace_defattr, expr->dn_attr);
2389 dt_ident_type_assign(idp, dtt.dtt_ctfp, dtt.dtt_type);
2390 (void) dt_ident_cook(dnp, idp, &ddp->dd_node);
2391
2392 /*
2393 * Store the parse tree nodes for 'expr' inside of idp->di_data ('inp')
2394 * so that they will be preserved with this identifier. Then pop the
2395 * inline declaration from the declaration stack and restore the lexer.
2396 */
2397 inp->din_list = yypcb->pcb_list;
2398 inp->din_root = expr;
2399
2400 dt_decl_free(dt_decl_pop());
2401 yybegin(YYS_CLAUSE);
2402
2403 /*
2404 * Finally, insert the inline identifier into dt_globals to make it
2405 * visible, and then cook 'dnp' to check its type against 'expr'.
2406 */
2407 dt_idhash_xinsert(dtp->dt_globals, idp);
2408 return (dt_node_cook(dnp, DT_IDFLG_REF));
2409}
2410
2411dt_node_t *
2412dt_node_member(dt_decl_t *ddp, char *name, dt_node_t *expr)
2413{
2414 dtrace_typeinfo_t dtt;
2415 dt_node_t *dnp;
2416 int err;
2417
2418 if (ddp != NULL) {
2419 err = dt_decl_type(ddp, &dtt);
2420 dt_decl_free(ddp);
2421
2422 if (err != 0)
2423 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
2424 }
2425
2426 dnp = dt_node_alloc(DT_NODE_MEMBER);
2427 dnp->dn_membname = name;
2428 dnp->dn_membexpr = expr;
2429
2430 if (ddp != NULL)
2392 dt_node_type_assign(dnp, dtt.dtt_ctfp, dtt.dtt_type);
2431 dt_node_type_assign(dnp, dtt.dtt_ctfp, dtt.dtt_type,
2432 dtt.dtt_flags);
2393
2394 return (dnp);
2395}
2396
2397dt_node_t *
2398dt_node_xlator(dt_decl_t *ddp, dt_decl_t *sdp, char *name, dt_node_t *members)
2399{
2400 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
2401 dtrace_typeinfo_t src, dst;
2402 dt_node_t sn, dn;
2403 dt_xlator_t *dxp;
2404 dt_node_t *dnp;
2405 int edst, esrc;
2406 uint_t kind;
2407
2408 char n1[DT_TYPE_NAMELEN];
2409 char n2[DT_TYPE_NAMELEN];
2410
2411 edst = dt_decl_type(ddp, &dst);
2412 dt_decl_free(ddp);
2413
2414 esrc = dt_decl_type(sdp, &src);
2415 dt_decl_free(sdp);
2416
2417 if (edst != 0 || esrc != 0) {
2418 free(name);
2419 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
2420 }
2421
2422 bzero(&sn, sizeof (sn));
2433
2434 return (dnp);
2435}
2436
2437dt_node_t *
2438dt_node_xlator(dt_decl_t *ddp, dt_decl_t *sdp, char *name, dt_node_t *members)
2439{
2440 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
2441 dtrace_typeinfo_t src, dst;
2442 dt_node_t sn, dn;
2443 dt_xlator_t *dxp;
2444 dt_node_t *dnp;
2445 int edst, esrc;
2446 uint_t kind;
2447
2448 char n1[DT_TYPE_NAMELEN];
2449 char n2[DT_TYPE_NAMELEN];
2450
2451 edst = dt_decl_type(ddp, &dst);
2452 dt_decl_free(ddp);
2453
2454 esrc = dt_decl_type(sdp, &src);
2455 dt_decl_free(sdp);
2456
2457 if (edst != 0 || esrc != 0) {
2458 free(name);
2459 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
2460 }
2461
2462 bzero(&sn, sizeof (sn));
2423 dt_node_type_assign(&sn, src.dtt_ctfp, src.dtt_type);
2463 dt_node_type_assign(&sn, src.dtt_ctfp, src.dtt_type, B_FALSE);
2424
2425 bzero(&dn, sizeof (dn));
2464
2465 bzero(&dn, sizeof (dn));
2426 dt_node_type_assign(&dn, dst.dtt_ctfp, dst.dtt_type);
2466 dt_node_type_assign(&dn, dst.dtt_ctfp, dst.dtt_type, B_FALSE);
2427
2428 if (dt_xlator_lookup(dtp, &sn, &dn, DT_XLATE_EXACT) != NULL) {
2429 xyerror(D_XLATE_REDECL,
2430 "translator from %s to %s has already been declared\n",
2431 dt_node_type_name(&sn, n1, sizeof (n1)),
2432 dt_node_type_name(&dn, n2, sizeof (n2)));
2433 }
2434
2435 kind = ctf_type_kind(dst.dtt_ctfp,
2436 ctf_type_resolve(dst.dtt_ctfp, dst.dtt_type));
2437
2438 if (kind == CTF_K_FORWARD) {
2439 xyerror(D_XLATE_SOU, "incomplete struct/union/enum %s\n",
2440 dt_type_name(dst.dtt_ctfp, dst.dtt_type, n1, sizeof (n1)));
2441 }
2442
2443 if (kind != CTF_K_STRUCT && kind != CTF_K_UNION) {
2444 xyerror(D_XLATE_SOU,
2445 "translator output type must be a struct or union\n");
2446 }
2447
2448 dxp = dt_xlator_create(dtp, &src, &dst, name, members, yypcb->pcb_list);
2449 yybegin(YYS_CLAUSE);
2450 free(name);
2451
2452 if (dxp == NULL)
2453 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2454
2455 dnp = dt_node_alloc(DT_NODE_XLATOR);
2456 dnp->dn_xlator = dxp;
2457 dnp->dn_members = members;
2458
2459 return (dt_node_cook(dnp, DT_IDFLG_REF));
2460}
2461
2462dt_node_t *
2463dt_node_probe(char *s, int protoc, dt_node_t *nargs, dt_node_t *xargs)
2464{
2465 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
2466 int nargc, xargc;
2467 dt_node_t *dnp;
2468
2469 size_t len = strlen(s) + 3; /* +3 for :: and \0 */
2470 char *name = alloca(len);
2471
2472 (void) snprintf(name, len, "::%s", s);
2473 (void) strhyphenate(name);
2474 free(s);
2475
2476 if (strchr(name, '`') != NULL) {
2477 xyerror(D_PROV_BADNAME, "probe name may not "
2478 "contain scoping operator: %s\n", name);
2479 }
2480
2481 if (strlen(name) - 2 >= DTRACE_NAMELEN) {
2482 xyerror(D_PROV_BADNAME, "probe name may not exceed %d "
2483 "characters: %s\n", DTRACE_NAMELEN - 1, name);
2484 }
2485
2486 dnp = dt_node_alloc(DT_NODE_PROBE);
2487
2488 dnp->dn_ident = dt_ident_create(name, DT_IDENT_PROBE,
2489 DT_IDFLG_ORPHAN, DTRACE_IDNONE, _dtrace_defattr, 0,
2490 &dt_idops_probe, NULL, dtp->dt_gen);
2491
2492 nargc = dt_decl_prototype(nargs, nargs,
2493 "probe input", DT_DP_VOID | DT_DP_ANON);
2494
2495 xargc = dt_decl_prototype(xargs, nargs,
2496 "probe output", DT_DP_VOID);
2497
2498 if (nargc > UINT8_MAX) {
2499 xyerror(D_PROV_PRARGLEN, "probe %s input prototype exceeds %u "
2500 "parameters: %d params used\n", name, UINT8_MAX, nargc);
2501 }
2502
2503 if (xargc > UINT8_MAX) {
2504 xyerror(D_PROV_PRARGLEN, "probe %s output prototype exceeds %u "
2505 "parameters: %d params used\n", name, UINT8_MAX, xargc);
2506 }
2507
2508 if (dnp->dn_ident == NULL || dt_probe_create(dtp,
2509 dnp->dn_ident, protoc, nargs, nargc, xargs, xargc) == NULL)
2510 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2511
2512 return (dnp);
2513}
2514
2515dt_node_t *
2516dt_node_provider(char *name, dt_node_t *probes)
2517{
2518 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
2519 dt_node_t *dnp = dt_node_alloc(DT_NODE_PROVIDER);
2520 dt_node_t *lnp;
2521 size_t len;
2522
2523 dnp->dn_provname = name;
2524 dnp->dn_probes = probes;
2525
2526 if (strchr(name, '`') != NULL) {
2527 dnerror(dnp, D_PROV_BADNAME, "provider name may not "
2528 "contain scoping operator: %s\n", name);
2529 }
2530
2531 if ((len = strlen(name)) >= DTRACE_PROVNAMELEN) {
2532 dnerror(dnp, D_PROV_BADNAME, "provider name may not exceed %d "
2533 "characters: %s\n", DTRACE_PROVNAMELEN - 1, name);
2534 }
2535
2536 if (isdigit(name[len - 1])) {
2537 dnerror(dnp, D_PROV_BADNAME, "provider name may not "
2538 "end with a digit: %s\n", name);
2539 }
2540
2541 /*
2542 * Check to see if the provider is already defined or visible through
2543 * dtrace(7D). If so, set dn_provred to treat it as a re-declaration.
2544 * If not, create a new provider and set its interface-only flag. This
2545 * flag may be cleared later by calls made to dt_probe_declare().
2546 */
2547 if ((dnp->dn_provider = dt_provider_lookup(dtp, name)) != NULL)
2548 dnp->dn_provred = B_TRUE;
2549 else if ((dnp->dn_provider = dt_provider_create(dtp, name)) == NULL)
2550 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2551 else
2552 dnp->dn_provider->pv_flags |= DT_PROVIDER_INTF;
2553
2554 /*
2555 * Store all parse nodes created since we consumed the DT_KEY_PROVIDER
2556 * token with the provider and then restore our lexing state to CLAUSE.
2557 * Note that if dnp->dn_provred is true, we may end up storing dups of
2558 * a provider's interface and implementation: we eat this space because
2559 * the implementation will likely need to redeclare probe members, and
2560 * therefore may result in those member nodes becoming persistent.
2561 */
2562 for (lnp = yypcb->pcb_list; lnp->dn_link != NULL; lnp = lnp->dn_link)
2563 continue; /* skip to end of allocation list */
2564
2565 lnp->dn_link = dnp->dn_provider->pv_nodes;
2566 dnp->dn_provider->pv_nodes = yypcb->pcb_list;
2567
2568 yybegin(YYS_CLAUSE);
2569 return (dnp);
2570}
2571
2572dt_node_t *
2573dt_node_program(dt_node_t *lnp)
2574{
2575 dt_node_t *dnp = dt_node_alloc(DT_NODE_PROG);
2576 dnp->dn_list = lnp;
2577 return (dnp);
2578}
2579
2580/*
2581 * This function provides the underlying implementation of cooking an
2582 * identifier given its node, a hash of dynamic identifiers, an identifier
2583 * kind, and a boolean flag indicating whether we are allowed to instantiate
2584 * a new identifier if the string is not found. This function is either
2585 * called from dt_cook_ident(), below, or directly by the various cooking
2586 * routines that are allowed to instantiate identifiers (e.g. op2 TOK_ASGN).
2587 */
2588static void
2589dt_xcook_ident(dt_node_t *dnp, dt_idhash_t *dhp, uint_t idkind, int create)
2590{
2591 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
2592 const char *sname = dt_idhash_name(dhp);
2593 int uref = 0;
2594
2595 dtrace_attribute_t attr = _dtrace_defattr;
2596 dt_ident_t *idp;
2597 dtrace_syminfo_t dts;
2598 GElf_Sym sym;
2599
2600 const char *scope, *mark;
2601 uchar_t dnkind;
2602 char *name;
2603
2604 /*
2605 * Look for scoping marks in the identifier. If one is found, set our
2606 * scope to either DTRACE_OBJ_KMODS or UMODS or to the first part of
2607 * the string that specifies the scope using an explicit module name.
2608 * If two marks in a row are found, set 'uref' (user symbol reference).
2609 * Otherwise we set scope to DTRACE_OBJ_EXEC, indicating that normal
2610 * scope is desired and we should search the specified idhash.
2611 */
2612 if ((name = strrchr(dnp->dn_string, '`')) != NULL) {
2613 if (name > dnp->dn_string && name[-1] == '`') {
2614 uref++;
2615 name[-1] = '\0';
2616 }
2617
2618 if (name == dnp->dn_string + uref)
2619 scope = uref ? DTRACE_OBJ_UMODS : DTRACE_OBJ_KMODS;
2620 else
2621 scope = dnp->dn_string;
2622
2623 *name++ = '\0'; /* leave name pointing after scoping mark */
2624 dnkind = DT_NODE_VAR;
2625
2626 } else if (idkind == DT_IDENT_AGG) {
2627 scope = DTRACE_OBJ_EXEC;
2628 name = dnp->dn_string + 1;
2629 dnkind = DT_NODE_AGG;
2630 } else {
2631 scope = DTRACE_OBJ_EXEC;
2632 name = dnp->dn_string;
2633 dnkind = DT_NODE_VAR;
2634 }
2635
2636 /*
2637 * If create is set to false, and we fail our idhash lookup, preset
2638 * the errno code to EDT_NOVAR for our final error message below.
2639 * If we end up calling dtrace_lookup_by_name(), it will reset the
2640 * errno appropriately and that error will be reported instead.
2641 */
2642 (void) dt_set_errno(dtp, EDT_NOVAR);
2643 mark = uref ? "``" : "`";
2644
2645 if (scope == DTRACE_OBJ_EXEC && (
2646 (dhp != dtp->dt_globals &&
2647 (idp = dt_idhash_lookup(dhp, name)) != NULL) ||
2648 (dhp == dtp->dt_globals &&
2649 (idp = dt_idstack_lookup(&yypcb->pcb_globals, name)) != NULL))) {
2650 /*
2651 * Check that we are referencing the ident in the manner that
2652 * matches its type if this is a global lookup. In the TLS or
2653 * local case, we don't know how the ident will be used until
2654 * the time operator -> is seen; more parsing is needed.
2655 */
2656 if (idp->di_kind != idkind && dhp == dtp->dt_globals) {
2657 xyerror(D_IDENT_BADREF, "%s '%s' may not be referenced "
2658 "as %s\n", dt_idkind_name(idp->di_kind),
2659 idp->di_name, dt_idkind_name(idkind));
2660 }
2661
2662 /*
2663 * Arrays and aggregations are not cooked individually. They
2664 * have dynamic types and must be referenced using operator [].
2665 * This is handled explicitly by the code for DT_TOK_LBRAC.
2666 */
2667 if (idp->di_kind != DT_IDENT_ARRAY &&
2668 idp->di_kind != DT_IDENT_AGG)
2669 attr = dt_ident_cook(dnp, idp, NULL);
2670 else {
2671 dt_node_type_assign(dnp,
2467
2468 if (dt_xlator_lookup(dtp, &sn, &dn, DT_XLATE_EXACT) != NULL) {
2469 xyerror(D_XLATE_REDECL,
2470 "translator from %s to %s has already been declared\n",
2471 dt_node_type_name(&sn, n1, sizeof (n1)),
2472 dt_node_type_name(&dn, n2, sizeof (n2)));
2473 }
2474
2475 kind = ctf_type_kind(dst.dtt_ctfp,
2476 ctf_type_resolve(dst.dtt_ctfp, dst.dtt_type));
2477
2478 if (kind == CTF_K_FORWARD) {
2479 xyerror(D_XLATE_SOU, "incomplete struct/union/enum %s\n",
2480 dt_type_name(dst.dtt_ctfp, dst.dtt_type, n1, sizeof (n1)));
2481 }
2482
2483 if (kind != CTF_K_STRUCT && kind != CTF_K_UNION) {
2484 xyerror(D_XLATE_SOU,
2485 "translator output type must be a struct or union\n");
2486 }
2487
2488 dxp = dt_xlator_create(dtp, &src, &dst, name, members, yypcb->pcb_list);
2489 yybegin(YYS_CLAUSE);
2490 free(name);
2491
2492 if (dxp == NULL)
2493 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2494
2495 dnp = dt_node_alloc(DT_NODE_XLATOR);
2496 dnp->dn_xlator = dxp;
2497 dnp->dn_members = members;
2498
2499 return (dt_node_cook(dnp, DT_IDFLG_REF));
2500}
2501
2502dt_node_t *
2503dt_node_probe(char *s, int protoc, dt_node_t *nargs, dt_node_t *xargs)
2504{
2505 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
2506 int nargc, xargc;
2507 dt_node_t *dnp;
2508
2509 size_t len = strlen(s) + 3; /* +3 for :: and \0 */
2510 char *name = alloca(len);
2511
2512 (void) snprintf(name, len, "::%s", s);
2513 (void) strhyphenate(name);
2514 free(s);
2515
2516 if (strchr(name, '`') != NULL) {
2517 xyerror(D_PROV_BADNAME, "probe name may not "
2518 "contain scoping operator: %s\n", name);
2519 }
2520
2521 if (strlen(name) - 2 >= DTRACE_NAMELEN) {
2522 xyerror(D_PROV_BADNAME, "probe name may not exceed %d "
2523 "characters: %s\n", DTRACE_NAMELEN - 1, name);
2524 }
2525
2526 dnp = dt_node_alloc(DT_NODE_PROBE);
2527
2528 dnp->dn_ident = dt_ident_create(name, DT_IDENT_PROBE,
2529 DT_IDFLG_ORPHAN, DTRACE_IDNONE, _dtrace_defattr, 0,
2530 &dt_idops_probe, NULL, dtp->dt_gen);
2531
2532 nargc = dt_decl_prototype(nargs, nargs,
2533 "probe input", DT_DP_VOID | DT_DP_ANON);
2534
2535 xargc = dt_decl_prototype(xargs, nargs,
2536 "probe output", DT_DP_VOID);
2537
2538 if (nargc > UINT8_MAX) {
2539 xyerror(D_PROV_PRARGLEN, "probe %s input prototype exceeds %u "
2540 "parameters: %d params used\n", name, UINT8_MAX, nargc);
2541 }
2542
2543 if (xargc > UINT8_MAX) {
2544 xyerror(D_PROV_PRARGLEN, "probe %s output prototype exceeds %u "
2545 "parameters: %d params used\n", name, UINT8_MAX, xargc);
2546 }
2547
2548 if (dnp->dn_ident == NULL || dt_probe_create(dtp,
2549 dnp->dn_ident, protoc, nargs, nargc, xargs, xargc) == NULL)
2550 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2551
2552 return (dnp);
2553}
2554
2555dt_node_t *
2556dt_node_provider(char *name, dt_node_t *probes)
2557{
2558 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
2559 dt_node_t *dnp = dt_node_alloc(DT_NODE_PROVIDER);
2560 dt_node_t *lnp;
2561 size_t len;
2562
2563 dnp->dn_provname = name;
2564 dnp->dn_probes = probes;
2565
2566 if (strchr(name, '`') != NULL) {
2567 dnerror(dnp, D_PROV_BADNAME, "provider name may not "
2568 "contain scoping operator: %s\n", name);
2569 }
2570
2571 if ((len = strlen(name)) >= DTRACE_PROVNAMELEN) {
2572 dnerror(dnp, D_PROV_BADNAME, "provider name may not exceed %d "
2573 "characters: %s\n", DTRACE_PROVNAMELEN - 1, name);
2574 }
2575
2576 if (isdigit(name[len - 1])) {
2577 dnerror(dnp, D_PROV_BADNAME, "provider name may not "
2578 "end with a digit: %s\n", name);
2579 }
2580
2581 /*
2582 * Check to see if the provider is already defined or visible through
2583 * dtrace(7D). If so, set dn_provred to treat it as a re-declaration.
2584 * If not, create a new provider and set its interface-only flag. This
2585 * flag may be cleared later by calls made to dt_probe_declare().
2586 */
2587 if ((dnp->dn_provider = dt_provider_lookup(dtp, name)) != NULL)
2588 dnp->dn_provred = B_TRUE;
2589 else if ((dnp->dn_provider = dt_provider_create(dtp, name)) == NULL)
2590 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2591 else
2592 dnp->dn_provider->pv_flags |= DT_PROVIDER_INTF;
2593
2594 /*
2595 * Store all parse nodes created since we consumed the DT_KEY_PROVIDER
2596 * token with the provider and then restore our lexing state to CLAUSE.
2597 * Note that if dnp->dn_provred is true, we may end up storing dups of
2598 * a provider's interface and implementation: we eat this space because
2599 * the implementation will likely need to redeclare probe members, and
2600 * therefore may result in those member nodes becoming persistent.
2601 */
2602 for (lnp = yypcb->pcb_list; lnp->dn_link != NULL; lnp = lnp->dn_link)
2603 continue; /* skip to end of allocation list */
2604
2605 lnp->dn_link = dnp->dn_provider->pv_nodes;
2606 dnp->dn_provider->pv_nodes = yypcb->pcb_list;
2607
2608 yybegin(YYS_CLAUSE);
2609 return (dnp);
2610}
2611
2612dt_node_t *
2613dt_node_program(dt_node_t *lnp)
2614{
2615 dt_node_t *dnp = dt_node_alloc(DT_NODE_PROG);
2616 dnp->dn_list = lnp;
2617 return (dnp);
2618}
2619
2620/*
2621 * This function provides the underlying implementation of cooking an
2622 * identifier given its node, a hash of dynamic identifiers, an identifier
2623 * kind, and a boolean flag indicating whether we are allowed to instantiate
2624 * a new identifier if the string is not found. This function is either
2625 * called from dt_cook_ident(), below, or directly by the various cooking
2626 * routines that are allowed to instantiate identifiers (e.g. op2 TOK_ASGN).
2627 */
2628static void
2629dt_xcook_ident(dt_node_t *dnp, dt_idhash_t *dhp, uint_t idkind, int create)
2630{
2631 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
2632 const char *sname = dt_idhash_name(dhp);
2633 int uref = 0;
2634
2635 dtrace_attribute_t attr = _dtrace_defattr;
2636 dt_ident_t *idp;
2637 dtrace_syminfo_t dts;
2638 GElf_Sym sym;
2639
2640 const char *scope, *mark;
2641 uchar_t dnkind;
2642 char *name;
2643
2644 /*
2645 * Look for scoping marks in the identifier. If one is found, set our
2646 * scope to either DTRACE_OBJ_KMODS or UMODS or to the first part of
2647 * the string that specifies the scope using an explicit module name.
2648 * If two marks in a row are found, set 'uref' (user symbol reference).
2649 * Otherwise we set scope to DTRACE_OBJ_EXEC, indicating that normal
2650 * scope is desired and we should search the specified idhash.
2651 */
2652 if ((name = strrchr(dnp->dn_string, '`')) != NULL) {
2653 if (name > dnp->dn_string && name[-1] == '`') {
2654 uref++;
2655 name[-1] = '\0';
2656 }
2657
2658 if (name == dnp->dn_string + uref)
2659 scope = uref ? DTRACE_OBJ_UMODS : DTRACE_OBJ_KMODS;
2660 else
2661 scope = dnp->dn_string;
2662
2663 *name++ = '\0'; /* leave name pointing after scoping mark */
2664 dnkind = DT_NODE_VAR;
2665
2666 } else if (idkind == DT_IDENT_AGG) {
2667 scope = DTRACE_OBJ_EXEC;
2668 name = dnp->dn_string + 1;
2669 dnkind = DT_NODE_AGG;
2670 } else {
2671 scope = DTRACE_OBJ_EXEC;
2672 name = dnp->dn_string;
2673 dnkind = DT_NODE_VAR;
2674 }
2675
2676 /*
2677 * If create is set to false, and we fail our idhash lookup, preset
2678 * the errno code to EDT_NOVAR for our final error message below.
2679 * If we end up calling dtrace_lookup_by_name(), it will reset the
2680 * errno appropriately and that error will be reported instead.
2681 */
2682 (void) dt_set_errno(dtp, EDT_NOVAR);
2683 mark = uref ? "``" : "`";
2684
2685 if (scope == DTRACE_OBJ_EXEC && (
2686 (dhp != dtp->dt_globals &&
2687 (idp = dt_idhash_lookup(dhp, name)) != NULL) ||
2688 (dhp == dtp->dt_globals &&
2689 (idp = dt_idstack_lookup(&yypcb->pcb_globals, name)) != NULL))) {
2690 /*
2691 * Check that we are referencing the ident in the manner that
2692 * matches its type if this is a global lookup. In the TLS or
2693 * local case, we don't know how the ident will be used until
2694 * the time operator -> is seen; more parsing is needed.
2695 */
2696 if (idp->di_kind != idkind && dhp == dtp->dt_globals) {
2697 xyerror(D_IDENT_BADREF, "%s '%s' may not be referenced "
2698 "as %s\n", dt_idkind_name(idp->di_kind),
2699 idp->di_name, dt_idkind_name(idkind));
2700 }
2701
2702 /*
2703 * Arrays and aggregations are not cooked individually. They
2704 * have dynamic types and must be referenced using operator [].
2705 * This is handled explicitly by the code for DT_TOK_LBRAC.
2706 */
2707 if (idp->di_kind != DT_IDENT_ARRAY &&
2708 idp->di_kind != DT_IDENT_AGG)
2709 attr = dt_ident_cook(dnp, idp, NULL);
2710 else {
2711 dt_node_type_assign(dnp,
2672 DT_DYN_CTFP(dtp), DT_DYN_TYPE(dtp));
2712 DT_DYN_CTFP(dtp), DT_DYN_TYPE(dtp), B_FALSE);
2673 attr = idp->di_attr;
2674 }
2675
2676 free(dnp->dn_string);
2677 dnp->dn_string = NULL;
2678 dnp->dn_kind = dnkind;
2679 dnp->dn_ident = idp;
2680 dnp->dn_flags |= DT_NF_LVALUE;
2681
2682 if (idp->di_flags & DT_IDFLG_WRITE)
2683 dnp->dn_flags |= DT_NF_WRITABLE;
2684
2685 dt_node_attr_assign(dnp, attr);
2686
2687 } else if (dhp == dtp->dt_globals && scope != DTRACE_OBJ_EXEC &&
2688 dtrace_lookup_by_name(dtp, scope, name, &sym, &dts) == 0) {
2689
2690 dt_module_t *mp = dt_module_lookup_by_name(dtp, dts.dts_object);
2691 int umod = (mp->dm_flags & DT_DM_KERNEL) == 0;
2692 static const char *const kunames[] = { "kernel", "user" };
2693
2694 dtrace_typeinfo_t dtt;
2695 dtrace_syminfo_t *sip;
2696
2697 if (uref ^ umod) {
2698 xyerror(D_SYM_BADREF, "%s module '%s' symbol '%s' may "
2699 "not be referenced as a %s symbol\n", kunames[umod],
2700 dts.dts_object, dts.dts_name, kunames[uref]);
2701 }
2702
2703 if (dtrace_symbol_type(dtp, &sym, &dts, &dtt) != 0) {
2704 /*
2705 * For now, we special-case EDT_DATAMODEL to clarify
2706 * that mixed data models are not currently supported.
2707 */
2708 if (dtp->dt_errno == EDT_DATAMODEL) {
2709 xyerror(D_SYM_MODEL, "cannot use %s symbol "
2710 "%s%s%s in a %s D program\n",
2711 dt_module_modelname(mp),
2712 dts.dts_object, mark, dts.dts_name,
2713 dt_module_modelname(dtp->dt_ddefs));
2714 }
2715
2716 xyerror(D_SYM_NOTYPES,
2717 "no symbolic type information is available for "
2718 "%s%s%s: %s\n", dts.dts_object, mark, dts.dts_name,
2719 dtrace_errmsg(dtp, dtrace_errno(dtp)));
2720 }
2721
2722 idp = dt_ident_create(name, DT_IDENT_SYMBOL, 0, 0,
2723 _dtrace_symattr, 0, &dt_idops_thaw, NULL, dtp->dt_gen);
2724
2725 if (idp == NULL)
2726 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2727
2728 if (mp->dm_flags & DT_DM_PRIMARY)
2729 idp->di_flags |= DT_IDFLG_PRIM;
2730
2731 idp->di_next = dtp->dt_externs;
2732 dtp->dt_externs = idp;
2733
2734 if ((sip = malloc(sizeof (dtrace_syminfo_t))) == NULL)
2735 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2736
2737 bcopy(&dts, sip, sizeof (dtrace_syminfo_t));
2738 idp->di_data = sip;
2739 idp->di_ctfp = dtt.dtt_ctfp;
2740 idp->di_type = dtt.dtt_type;
2741
2742 free(dnp->dn_string);
2743 dnp->dn_string = NULL;
2744 dnp->dn_kind = DT_NODE_SYM;
2745 dnp->dn_ident = idp;
2746 dnp->dn_flags |= DT_NF_LVALUE;
2747
2713 attr = idp->di_attr;
2714 }
2715
2716 free(dnp->dn_string);
2717 dnp->dn_string = NULL;
2718 dnp->dn_kind = dnkind;
2719 dnp->dn_ident = idp;
2720 dnp->dn_flags |= DT_NF_LVALUE;
2721
2722 if (idp->di_flags & DT_IDFLG_WRITE)
2723 dnp->dn_flags |= DT_NF_WRITABLE;
2724
2725 dt_node_attr_assign(dnp, attr);
2726
2727 } else if (dhp == dtp->dt_globals && scope != DTRACE_OBJ_EXEC &&
2728 dtrace_lookup_by_name(dtp, scope, name, &sym, &dts) == 0) {
2729
2730 dt_module_t *mp = dt_module_lookup_by_name(dtp, dts.dts_object);
2731 int umod = (mp->dm_flags & DT_DM_KERNEL) == 0;
2732 static const char *const kunames[] = { "kernel", "user" };
2733
2734 dtrace_typeinfo_t dtt;
2735 dtrace_syminfo_t *sip;
2736
2737 if (uref ^ umod) {
2738 xyerror(D_SYM_BADREF, "%s module '%s' symbol '%s' may "
2739 "not be referenced as a %s symbol\n", kunames[umod],
2740 dts.dts_object, dts.dts_name, kunames[uref]);
2741 }
2742
2743 if (dtrace_symbol_type(dtp, &sym, &dts, &dtt) != 0) {
2744 /*
2745 * For now, we special-case EDT_DATAMODEL to clarify
2746 * that mixed data models are not currently supported.
2747 */
2748 if (dtp->dt_errno == EDT_DATAMODEL) {
2749 xyerror(D_SYM_MODEL, "cannot use %s symbol "
2750 "%s%s%s in a %s D program\n",
2751 dt_module_modelname(mp),
2752 dts.dts_object, mark, dts.dts_name,
2753 dt_module_modelname(dtp->dt_ddefs));
2754 }
2755
2756 xyerror(D_SYM_NOTYPES,
2757 "no symbolic type information is available for "
2758 "%s%s%s: %s\n", dts.dts_object, mark, dts.dts_name,
2759 dtrace_errmsg(dtp, dtrace_errno(dtp)));
2760 }
2761
2762 idp = dt_ident_create(name, DT_IDENT_SYMBOL, 0, 0,
2763 _dtrace_symattr, 0, &dt_idops_thaw, NULL, dtp->dt_gen);
2764
2765 if (idp == NULL)
2766 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2767
2768 if (mp->dm_flags & DT_DM_PRIMARY)
2769 idp->di_flags |= DT_IDFLG_PRIM;
2770
2771 idp->di_next = dtp->dt_externs;
2772 dtp->dt_externs = idp;
2773
2774 if ((sip = malloc(sizeof (dtrace_syminfo_t))) == NULL)
2775 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2776
2777 bcopy(&dts, sip, sizeof (dtrace_syminfo_t));
2778 idp->di_data = sip;
2779 idp->di_ctfp = dtt.dtt_ctfp;
2780 idp->di_type = dtt.dtt_type;
2781
2782 free(dnp->dn_string);
2783 dnp->dn_string = NULL;
2784 dnp->dn_kind = DT_NODE_SYM;
2785 dnp->dn_ident = idp;
2786 dnp->dn_flags |= DT_NF_LVALUE;
2787
2748 dt_node_type_assign(dnp, dtt.dtt_ctfp, dtt.dtt_type);
2788 dt_node_type_assign(dnp, dtt.dtt_ctfp, dtt.dtt_type,
2789 dtt.dtt_flags);
2749 dt_node_attr_assign(dnp, _dtrace_symattr);
2750
2751 if (uref) {
2752 idp->di_flags |= DT_IDFLG_USER;
2753 dnp->dn_flags |= DT_NF_USERLAND;
2754 }
2755
2756 } else if (scope == DTRACE_OBJ_EXEC && create == B_TRUE) {
2757 uint_t flags = DT_IDFLG_WRITE;
2758 uint_t id;
2759
2760 if (dt_idhash_nextid(dhp, &id) == -1) {
2761 xyerror(D_ID_OFLOW, "cannot create %s: limit on number "
2762 "of %s variables exceeded\n", name, sname);
2763 }
2764
2765 if (dhp == yypcb->pcb_locals)
2766 flags |= DT_IDFLG_LOCAL;
2767 else if (dhp == dtp->dt_tls)
2768 flags |= DT_IDFLG_TLS;
2769
2770 dt_dprintf("create %s %s variable %s, id=%u\n",
2771 sname, dt_idkind_name(idkind), name, id);
2772
2773 if (idkind == DT_IDENT_ARRAY || idkind == DT_IDENT_AGG) {
2774 idp = dt_idhash_insert(dhp, name,
2775 idkind, flags, id, _dtrace_defattr, 0,
2776 &dt_idops_assc, NULL, dtp->dt_gen);
2777 } else {
2778 idp = dt_idhash_insert(dhp, name,
2779 idkind, flags, id, _dtrace_defattr, 0,
2780 &dt_idops_thaw, NULL, dtp->dt_gen);
2781 }
2782
2783 if (idp == NULL)
2784 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2785
2786 /*
2787 * Arrays and aggregations are not cooked individually. They
2788 * have dynamic types and must be referenced using operator [].
2789 * This is handled explicitly by the code for DT_TOK_LBRAC.
2790 */
2791 if (idp->di_kind != DT_IDENT_ARRAY &&
2792 idp->di_kind != DT_IDENT_AGG)
2793 attr = dt_ident_cook(dnp, idp, NULL);
2794 else {
2795 dt_node_type_assign(dnp,
2790 dt_node_attr_assign(dnp, _dtrace_symattr);
2791
2792 if (uref) {
2793 idp->di_flags |= DT_IDFLG_USER;
2794 dnp->dn_flags |= DT_NF_USERLAND;
2795 }
2796
2797 } else if (scope == DTRACE_OBJ_EXEC && create == B_TRUE) {
2798 uint_t flags = DT_IDFLG_WRITE;
2799 uint_t id;
2800
2801 if (dt_idhash_nextid(dhp, &id) == -1) {
2802 xyerror(D_ID_OFLOW, "cannot create %s: limit on number "
2803 "of %s variables exceeded\n", name, sname);
2804 }
2805
2806 if (dhp == yypcb->pcb_locals)
2807 flags |= DT_IDFLG_LOCAL;
2808 else if (dhp == dtp->dt_tls)
2809 flags |= DT_IDFLG_TLS;
2810
2811 dt_dprintf("create %s %s variable %s, id=%u\n",
2812 sname, dt_idkind_name(idkind), name, id);
2813
2814 if (idkind == DT_IDENT_ARRAY || idkind == DT_IDENT_AGG) {
2815 idp = dt_idhash_insert(dhp, name,
2816 idkind, flags, id, _dtrace_defattr, 0,
2817 &dt_idops_assc, NULL, dtp->dt_gen);
2818 } else {
2819 idp = dt_idhash_insert(dhp, name,
2820 idkind, flags, id, _dtrace_defattr, 0,
2821 &dt_idops_thaw, NULL, dtp->dt_gen);
2822 }
2823
2824 if (idp == NULL)
2825 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2826
2827 /*
2828 * Arrays and aggregations are not cooked individually. They
2829 * have dynamic types and must be referenced using operator [].
2830 * This is handled explicitly by the code for DT_TOK_LBRAC.
2831 */
2832 if (idp->di_kind != DT_IDENT_ARRAY &&
2833 idp->di_kind != DT_IDENT_AGG)
2834 attr = dt_ident_cook(dnp, idp, NULL);
2835 else {
2836 dt_node_type_assign(dnp,
2796 DT_DYN_CTFP(dtp), DT_DYN_TYPE(dtp));
2837 DT_DYN_CTFP(dtp), DT_DYN_TYPE(dtp), B_FALSE);
2797 attr = idp->di_attr;
2798 }
2799
2800 free(dnp->dn_string);
2801 dnp->dn_string = NULL;
2802 dnp->dn_kind = dnkind;
2803 dnp->dn_ident = idp;
2804 dnp->dn_flags |= DT_NF_LVALUE | DT_NF_WRITABLE;
2805
2806 dt_node_attr_assign(dnp, attr);
2807
2808 } else if (scope != DTRACE_OBJ_EXEC) {
2809 xyerror(D_IDENT_UNDEF, "failed to resolve %s%s%s: %s\n",
2810 dnp->dn_string, mark, name,
2811 dtrace_errmsg(dtp, dtrace_errno(dtp)));
2812 } else {
2813 xyerror(D_IDENT_UNDEF, "failed to resolve %s: %s\n",
2814 dnp->dn_string, dtrace_errmsg(dtp, dtrace_errno(dtp)));
2815 }
2816}
2817
2818static dt_node_t *
2819dt_cook_ident(dt_node_t *dnp, uint_t idflags)
2820{
2821 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
2822
2823 if (dnp->dn_op == DT_TOK_AGG)
2824 dt_xcook_ident(dnp, dtp->dt_aggs, DT_IDENT_AGG, B_FALSE);
2825 else
2826 dt_xcook_ident(dnp, dtp->dt_globals, DT_IDENT_SCALAR, B_FALSE);
2827
2828 return (dt_node_cook(dnp, idflags));
2829}
2830
2831/*
2832 * Since operators [ and -> can instantiate new variables before we know
2833 * whether the reference is for a read or a write, we need to check read
2834 * references to determine if the identifier is currently dt_ident_unref().
2835 * If so, we report that this first access was to an undefined variable.
2836 */
2837static dt_node_t *
2838dt_cook_var(dt_node_t *dnp, uint_t idflags)
2839{
2840 dt_ident_t *idp = dnp->dn_ident;
2841
2842 if ((idflags & DT_IDFLG_REF) && dt_ident_unref(idp)) {
2843 dnerror(dnp, D_VAR_UNDEF,
2844 "%s%s has not yet been declared or assigned\n",
2845 (idp->di_flags & DT_IDFLG_LOCAL) ? "this->" :
2846 (idp->di_flags & DT_IDFLG_TLS) ? "self->" : "",
2847 idp->di_name);
2848 }
2849
2850 dt_node_attr_assign(dnp, dt_ident_cook(dnp, idp, &dnp->dn_args));
2851 return (dnp);
2852}
2853
2854/*ARGSUSED*/
2855static dt_node_t *
2856dt_cook_func(dt_node_t *dnp, uint_t idflags)
2857{
2858 dt_node_attr_assign(dnp,
2859 dt_ident_cook(dnp, dnp->dn_ident, &dnp->dn_args));
2860
2861 return (dnp);
2862}
2863
2864static dt_node_t *
2865dt_cook_op1(dt_node_t *dnp, uint_t idflags)
2866{
2867 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
2868 dt_node_t *cp = dnp->dn_child;
2869
2870 char n[DT_TYPE_NAMELEN];
2871 dtrace_typeinfo_t dtt;
2872 dt_ident_t *idp;
2873
2874 ctf_encoding_t e;
2875 ctf_arinfo_t r;
2876 ctf_id_t type, base;
2877 uint_t kind;
2878
2879 if (dnp->dn_op == DT_TOK_PREINC || dnp->dn_op == DT_TOK_POSTINC ||
2880 dnp->dn_op == DT_TOK_PREDEC || dnp->dn_op == DT_TOK_POSTDEC)
2881 idflags = DT_IDFLG_REF | DT_IDFLG_MOD;
2882 else
2883 idflags = DT_IDFLG_REF;
2884
2885 /*
2886 * We allow the unary ++ and -- operators to instantiate new scalar
2887 * variables if applied to an identifier; otherwise just cook as usual.
2888 */
2889 if (cp->dn_kind == DT_NODE_IDENT && (idflags & DT_IDFLG_MOD))
2890 dt_xcook_ident(cp, dtp->dt_globals, DT_IDENT_SCALAR, B_TRUE);
2891
2892 cp = dnp->dn_child = dt_node_cook(cp, 0); /* don't set idflags yet */
2893
2894 if (cp->dn_kind == DT_NODE_VAR && dt_ident_unref(cp->dn_ident)) {
2895 if (dt_type_lookup("int64_t", &dtt) != 0)
2896 xyerror(D_TYPE_ERR, "failed to lookup int64_t\n");
2897
2898 dt_ident_type_assign(cp->dn_ident, dtt.dtt_ctfp, dtt.dtt_type);
2838 attr = idp->di_attr;
2839 }
2840
2841 free(dnp->dn_string);
2842 dnp->dn_string = NULL;
2843 dnp->dn_kind = dnkind;
2844 dnp->dn_ident = idp;
2845 dnp->dn_flags |= DT_NF_LVALUE | DT_NF_WRITABLE;
2846
2847 dt_node_attr_assign(dnp, attr);
2848
2849 } else if (scope != DTRACE_OBJ_EXEC) {
2850 xyerror(D_IDENT_UNDEF, "failed to resolve %s%s%s: %s\n",
2851 dnp->dn_string, mark, name,
2852 dtrace_errmsg(dtp, dtrace_errno(dtp)));
2853 } else {
2854 xyerror(D_IDENT_UNDEF, "failed to resolve %s: %s\n",
2855 dnp->dn_string, dtrace_errmsg(dtp, dtrace_errno(dtp)));
2856 }
2857}
2858
2859static dt_node_t *
2860dt_cook_ident(dt_node_t *dnp, uint_t idflags)
2861{
2862 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
2863
2864 if (dnp->dn_op == DT_TOK_AGG)
2865 dt_xcook_ident(dnp, dtp->dt_aggs, DT_IDENT_AGG, B_FALSE);
2866 else
2867 dt_xcook_ident(dnp, dtp->dt_globals, DT_IDENT_SCALAR, B_FALSE);
2868
2869 return (dt_node_cook(dnp, idflags));
2870}
2871
2872/*
2873 * Since operators [ and -> can instantiate new variables before we know
2874 * whether the reference is for a read or a write, we need to check read
2875 * references to determine if the identifier is currently dt_ident_unref().
2876 * If so, we report that this first access was to an undefined variable.
2877 */
2878static dt_node_t *
2879dt_cook_var(dt_node_t *dnp, uint_t idflags)
2880{
2881 dt_ident_t *idp = dnp->dn_ident;
2882
2883 if ((idflags & DT_IDFLG_REF) && dt_ident_unref(idp)) {
2884 dnerror(dnp, D_VAR_UNDEF,
2885 "%s%s has not yet been declared or assigned\n",
2886 (idp->di_flags & DT_IDFLG_LOCAL) ? "this->" :
2887 (idp->di_flags & DT_IDFLG_TLS) ? "self->" : "",
2888 idp->di_name);
2889 }
2890
2891 dt_node_attr_assign(dnp, dt_ident_cook(dnp, idp, &dnp->dn_args));
2892 return (dnp);
2893}
2894
2895/*ARGSUSED*/
2896static dt_node_t *
2897dt_cook_func(dt_node_t *dnp, uint_t idflags)
2898{
2899 dt_node_attr_assign(dnp,
2900 dt_ident_cook(dnp, dnp->dn_ident, &dnp->dn_args));
2901
2902 return (dnp);
2903}
2904
2905static dt_node_t *
2906dt_cook_op1(dt_node_t *dnp, uint_t idflags)
2907{
2908 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
2909 dt_node_t *cp = dnp->dn_child;
2910
2911 char n[DT_TYPE_NAMELEN];
2912 dtrace_typeinfo_t dtt;
2913 dt_ident_t *idp;
2914
2915 ctf_encoding_t e;
2916 ctf_arinfo_t r;
2917 ctf_id_t type, base;
2918 uint_t kind;
2919
2920 if (dnp->dn_op == DT_TOK_PREINC || dnp->dn_op == DT_TOK_POSTINC ||
2921 dnp->dn_op == DT_TOK_PREDEC || dnp->dn_op == DT_TOK_POSTDEC)
2922 idflags = DT_IDFLG_REF | DT_IDFLG_MOD;
2923 else
2924 idflags = DT_IDFLG_REF;
2925
2926 /*
2927 * We allow the unary ++ and -- operators to instantiate new scalar
2928 * variables if applied to an identifier; otherwise just cook as usual.
2929 */
2930 if (cp->dn_kind == DT_NODE_IDENT && (idflags & DT_IDFLG_MOD))
2931 dt_xcook_ident(cp, dtp->dt_globals, DT_IDENT_SCALAR, B_TRUE);
2932
2933 cp = dnp->dn_child = dt_node_cook(cp, 0); /* don't set idflags yet */
2934
2935 if (cp->dn_kind == DT_NODE_VAR && dt_ident_unref(cp->dn_ident)) {
2936 if (dt_type_lookup("int64_t", &dtt) != 0)
2937 xyerror(D_TYPE_ERR, "failed to lookup int64_t\n");
2938
2939 dt_ident_type_assign(cp->dn_ident, dtt.dtt_ctfp, dtt.dtt_type);
2899 dt_node_type_assign(cp, dtt.dtt_ctfp, dtt.dtt_type);
2940 dt_node_type_assign(cp, dtt.dtt_ctfp, dtt.dtt_type,
2941 dtt.dtt_flags);
2900 }
2901
2902 if (cp->dn_kind == DT_NODE_VAR)
2903 cp->dn_ident->di_flags |= idflags;
2904
2905 switch (dnp->dn_op) {
2906 case DT_TOK_DEREF:
2907 /*
2908 * If the deref operator is applied to a translated pointer,
2909 * we set our output type to the output of the translation.
2910 */
2911 if ((idp = dt_node_resolve(cp, DT_IDENT_XLPTR)) != NULL) {
2912 dt_xlator_t *dxp = idp->di_data;
2913
2914 dnp->dn_ident = &dxp->dx_souid;
2915 dt_node_type_assign(dnp,
2942 }
2943
2944 if (cp->dn_kind == DT_NODE_VAR)
2945 cp->dn_ident->di_flags |= idflags;
2946
2947 switch (dnp->dn_op) {
2948 case DT_TOK_DEREF:
2949 /*
2950 * If the deref operator is applied to a translated pointer,
2951 * we set our output type to the output of the translation.
2952 */
2953 if ((idp = dt_node_resolve(cp, DT_IDENT_XLPTR)) != NULL) {
2954 dt_xlator_t *dxp = idp->di_data;
2955
2956 dnp->dn_ident = &dxp->dx_souid;
2957 dt_node_type_assign(dnp,
2916 dnp->dn_ident->di_ctfp, dnp->dn_ident->di_type);
2958 dnp->dn_ident->di_ctfp, dnp->dn_ident->di_type,
2959 cp->dn_flags & DT_NF_USERLAND);
2917 break;
2918 }
2919
2920 type = ctf_type_resolve(cp->dn_ctfp, cp->dn_type);
2921 kind = ctf_type_kind(cp->dn_ctfp, type);
2922
2923 if (kind == CTF_K_ARRAY) {
2924 if (ctf_array_info(cp->dn_ctfp, type, &r) != 0) {
2925 dtp->dt_ctferr = ctf_errno(cp->dn_ctfp);
2926 longjmp(yypcb->pcb_jmpbuf, EDT_CTF);
2927 } else
2928 type = r.ctr_contents;
2929 } else if (kind == CTF_K_POINTER) {
2930 type = ctf_type_reference(cp->dn_ctfp, type);
2931 } else {
2932 xyerror(D_DEREF_NONPTR,
2933 "cannot dereference non-pointer type\n");
2934 }
2935
2960 break;
2961 }
2962
2963 type = ctf_type_resolve(cp->dn_ctfp, cp->dn_type);
2964 kind = ctf_type_kind(cp->dn_ctfp, type);
2965
2966 if (kind == CTF_K_ARRAY) {
2967 if (ctf_array_info(cp->dn_ctfp, type, &r) != 0) {
2968 dtp->dt_ctferr = ctf_errno(cp->dn_ctfp);
2969 longjmp(yypcb->pcb_jmpbuf, EDT_CTF);
2970 } else
2971 type = r.ctr_contents;
2972 } else if (kind == CTF_K_POINTER) {
2973 type = ctf_type_reference(cp->dn_ctfp, type);
2974 } else {
2975 xyerror(D_DEREF_NONPTR,
2976 "cannot dereference non-pointer type\n");
2977 }
2978
2936 dt_node_type_assign(dnp, cp->dn_ctfp, type);
2979 dt_node_type_assign(dnp, cp->dn_ctfp, type,
2980 cp->dn_flags & DT_NF_USERLAND);
2937 base = ctf_type_resolve(cp->dn_ctfp, type);
2938 kind = ctf_type_kind(cp->dn_ctfp, base);
2939
2940 if (kind == CTF_K_INTEGER && ctf_type_encoding(cp->dn_ctfp,
2941 base, &e) == 0 && IS_VOID(e)) {
2942 xyerror(D_DEREF_VOID,
2943 "cannot dereference pointer to void\n");
2944 }
2945
2946 if (kind == CTF_K_FUNCTION) {
2947 xyerror(D_DEREF_FUNC,
2948 "cannot dereference pointer to function\n");
2949 }
2950
2951 if (kind != CTF_K_ARRAY || dt_node_is_string(dnp))
2952 dnp->dn_flags |= DT_NF_LVALUE; /* see K&R[A7.4.3] */
2953
2954 /*
2955 * If we propagated the l-value bit and the child operand was
2956 * a writable D variable or a binary operation of the form
2957 * a + b where a is writable, then propagate the writable bit.
2958 * This is necessary to permit assignments to scalar arrays,
2959 * which are converted to expressions of the form *(a + i).
2960 */
2961 if ((cp->dn_flags & DT_NF_WRITABLE) ||
2962 (cp->dn_kind == DT_NODE_OP2 && cp->dn_op == DT_TOK_ADD &&
2963 (cp->dn_left->dn_flags & DT_NF_WRITABLE)))
2964 dnp->dn_flags |= DT_NF_WRITABLE;
2965
2966 if ((cp->dn_flags & DT_NF_USERLAND) &&
2967 (kind == CTF_K_POINTER || (dnp->dn_flags & DT_NF_REF)))
2968 dnp->dn_flags |= DT_NF_USERLAND;
2969 break;
2970
2971 case DT_TOK_IPOS:
2972 case DT_TOK_INEG:
2973 if (!dt_node_is_arith(cp)) {
2974 xyerror(D_OP_ARITH, "operator %s requires an operand "
2975 "of arithmetic type\n", opstr(dnp->dn_op));
2976 }
2977 dt_node_type_propagate(cp, dnp); /* see K&R[A7.4.4-6] */
2978 break;
2979
2980 case DT_TOK_BNEG:
2981 if (!dt_node_is_integer(cp)) {
2982 xyerror(D_OP_INT, "operator %s requires an operand of "
2983 "integral type\n", opstr(dnp->dn_op));
2984 }
2985 dt_node_type_propagate(cp, dnp); /* see K&R[A7.4.4-6] */
2986 break;
2987
2988 case DT_TOK_LNEG:
2989 if (!dt_node_is_scalar(cp)) {
2990 xyerror(D_OP_SCALAR, "operator %s requires an operand "
2991 "of scalar type\n", opstr(dnp->dn_op));
2992 }
2981 base = ctf_type_resolve(cp->dn_ctfp, type);
2982 kind = ctf_type_kind(cp->dn_ctfp, base);
2983
2984 if (kind == CTF_K_INTEGER && ctf_type_encoding(cp->dn_ctfp,
2985 base, &e) == 0 && IS_VOID(e)) {
2986 xyerror(D_DEREF_VOID,
2987 "cannot dereference pointer to void\n");
2988 }
2989
2990 if (kind == CTF_K_FUNCTION) {
2991 xyerror(D_DEREF_FUNC,
2992 "cannot dereference pointer to function\n");
2993 }
2994
2995 if (kind != CTF_K_ARRAY || dt_node_is_string(dnp))
2996 dnp->dn_flags |= DT_NF_LVALUE; /* see K&R[A7.4.3] */
2997
2998 /*
2999 * If we propagated the l-value bit and the child operand was
3000 * a writable D variable or a binary operation of the form
3001 * a + b where a is writable, then propagate the writable bit.
3002 * This is necessary to permit assignments to scalar arrays,
3003 * which are converted to expressions of the form *(a + i).
3004 */
3005 if ((cp->dn_flags & DT_NF_WRITABLE) ||
3006 (cp->dn_kind == DT_NODE_OP2 && cp->dn_op == DT_TOK_ADD &&
3007 (cp->dn_left->dn_flags & DT_NF_WRITABLE)))
3008 dnp->dn_flags |= DT_NF_WRITABLE;
3009
3010 if ((cp->dn_flags & DT_NF_USERLAND) &&
3011 (kind == CTF_K_POINTER || (dnp->dn_flags & DT_NF_REF)))
3012 dnp->dn_flags |= DT_NF_USERLAND;
3013 break;
3014
3015 case DT_TOK_IPOS:
3016 case DT_TOK_INEG:
3017 if (!dt_node_is_arith(cp)) {
3018 xyerror(D_OP_ARITH, "operator %s requires an operand "
3019 "of arithmetic type\n", opstr(dnp->dn_op));
3020 }
3021 dt_node_type_propagate(cp, dnp); /* see K&R[A7.4.4-6] */
3022 break;
3023
3024 case DT_TOK_BNEG:
3025 if (!dt_node_is_integer(cp)) {
3026 xyerror(D_OP_INT, "operator %s requires an operand of "
3027 "integral type\n", opstr(dnp->dn_op));
3028 }
3029 dt_node_type_propagate(cp, dnp); /* see K&R[A7.4.4-6] */
3030 break;
3031
3032 case DT_TOK_LNEG:
3033 if (!dt_node_is_scalar(cp)) {
3034 xyerror(D_OP_SCALAR, "operator %s requires an operand "
3035 "of scalar type\n", opstr(dnp->dn_op));
3036 }
2993 dt_node_type_assign(dnp, DT_INT_CTFP(dtp), DT_INT_TYPE(dtp));
3037 dt_node_type_assign(dnp, DT_INT_CTFP(dtp), DT_INT_TYPE(dtp),
3038 B_FALSE);
2994 break;
2995
2996 case DT_TOK_ADDROF:
2997 if (cp->dn_kind == DT_NODE_VAR || cp->dn_kind == DT_NODE_AGG) {
2998 xyerror(D_ADDROF_VAR,
2999 "cannot take address of dynamic variable\n");
3000 }
3001
3002 if (dt_node_is_dynamic(cp)) {
3003 xyerror(D_ADDROF_VAR,
3004 "cannot take address of dynamic object\n");
3005 }
3006
3007 if (!(cp->dn_flags & DT_NF_LVALUE)) {
3008 xyerror(D_ADDROF_LVAL, /* see K&R[A7.4.2] */
3009 "unacceptable operand for unary & operator\n");
3010 }
3011
3012 if (cp->dn_flags & DT_NF_BITFIELD) {
3013 xyerror(D_ADDROF_BITFIELD,
3014 "cannot take address of bit-field\n");
3015 }
3016
3017 dtt.dtt_object = NULL;
3018 dtt.dtt_ctfp = cp->dn_ctfp;
3019 dtt.dtt_type = cp->dn_type;
3020
3021 if (dt_type_pointer(&dtt) == -1) {
3022 xyerror(D_TYPE_ERR, "cannot find type for \"&\": %s*\n",
3023 dt_node_type_name(cp, n, sizeof (n)));
3024 }
3025
3039 break;
3040
3041 case DT_TOK_ADDROF:
3042 if (cp->dn_kind == DT_NODE_VAR || cp->dn_kind == DT_NODE_AGG) {
3043 xyerror(D_ADDROF_VAR,
3044 "cannot take address of dynamic variable\n");
3045 }
3046
3047 if (dt_node_is_dynamic(cp)) {
3048 xyerror(D_ADDROF_VAR,
3049 "cannot take address of dynamic object\n");
3050 }
3051
3052 if (!(cp->dn_flags & DT_NF_LVALUE)) {
3053 xyerror(D_ADDROF_LVAL, /* see K&R[A7.4.2] */
3054 "unacceptable operand for unary & operator\n");
3055 }
3056
3057 if (cp->dn_flags & DT_NF_BITFIELD) {
3058 xyerror(D_ADDROF_BITFIELD,
3059 "cannot take address of bit-field\n");
3060 }
3061
3062 dtt.dtt_object = NULL;
3063 dtt.dtt_ctfp = cp->dn_ctfp;
3064 dtt.dtt_type = cp->dn_type;
3065
3066 if (dt_type_pointer(&dtt) == -1) {
3067 xyerror(D_TYPE_ERR, "cannot find type for \"&\": %s*\n",
3068 dt_node_type_name(cp, n, sizeof (n)));
3069 }
3070
3026 dt_node_type_assign(dnp, dtt.dtt_ctfp, dtt.dtt_type);
3027
3028 if (cp->dn_flags & DT_NF_USERLAND)
3029 dnp->dn_flags |= DT_NF_USERLAND;
3071 dt_node_type_assign(dnp, dtt.dtt_ctfp, dtt.dtt_type,
3072 cp->dn_flags & DT_NF_USERLAND);
3030 break;
3031
3032 case DT_TOK_SIZEOF:
3033 if (cp->dn_flags & DT_NF_BITFIELD) {
3034 xyerror(D_SIZEOF_BITFIELD,
3035 "cannot apply sizeof to a bit-field\n");
3036 }
3037
3038 if (dt_node_sizeof(cp) == 0) {
3039 xyerror(D_SIZEOF_TYPE, "cannot apply sizeof to an "
3040 "operand of unknown size\n");
3041 }
3042
3043 dt_node_type_assign(dnp, dtp->dt_ddefs->dm_ctfp,
3073 break;
3074
3075 case DT_TOK_SIZEOF:
3076 if (cp->dn_flags & DT_NF_BITFIELD) {
3077 xyerror(D_SIZEOF_BITFIELD,
3078 "cannot apply sizeof to a bit-field\n");
3079 }
3080
3081 if (dt_node_sizeof(cp) == 0) {
3082 xyerror(D_SIZEOF_TYPE, "cannot apply sizeof to an "
3083 "operand of unknown size\n");
3084 }
3085
3086 dt_node_type_assign(dnp, dtp->dt_ddefs->dm_ctfp,
3044 ctf_lookup_by_name(dtp->dt_ddefs->dm_ctfp, "size_t"));
3087 ctf_lookup_by_name(dtp->dt_ddefs->dm_ctfp, "size_t"),
3088 B_FALSE);
3045 break;
3046
3047 case DT_TOK_STRINGOF:
3048 if (!dt_node_is_scalar(cp) && !dt_node_is_pointer(cp) &&
3049 !dt_node_is_strcompat(cp)) {
3050 xyerror(D_STRINGOF_TYPE,
3051 "cannot apply stringof to a value of type %s\n",
3052 dt_node_type_name(cp, n, sizeof (n)));
3053 }
3089 break;
3090
3091 case DT_TOK_STRINGOF:
3092 if (!dt_node_is_scalar(cp) && !dt_node_is_pointer(cp) &&
3093 !dt_node_is_strcompat(cp)) {
3094 xyerror(D_STRINGOF_TYPE,
3095 "cannot apply stringof to a value of type %s\n",
3096 dt_node_type_name(cp, n, sizeof (n)));
3097 }
3054 dt_node_type_assign(dnp, DT_STR_CTFP(dtp), DT_STR_TYPE(dtp));
3098 dt_node_type_assign(dnp, DT_STR_CTFP(dtp), DT_STR_TYPE(dtp),
3099 cp->dn_flags & DT_NF_USERLAND);
3055 break;
3056
3057 case DT_TOK_PREINC:
3058 case DT_TOK_POSTINC:
3059 case DT_TOK_PREDEC:
3060 case DT_TOK_POSTDEC:
3061 if (dt_node_is_scalar(cp) == 0) {
3062 xyerror(D_OP_SCALAR, "operator %s requires operand of "
3063 "scalar type\n", opstr(dnp->dn_op));
3064 }
3065
3066 if (dt_node_is_vfptr(cp)) {
3067 xyerror(D_OP_VFPTR, "operator %s requires an operand "
3068 "of known size\n", opstr(dnp->dn_op));
3069 }
3070
3071 if (!(cp->dn_flags & DT_NF_LVALUE)) {
3072 xyerror(D_OP_LVAL, "operator %s requires modifiable "
3073 "lvalue as an operand\n", opstr(dnp->dn_op));
3074 }
3075
3076 if (!(cp->dn_flags & DT_NF_WRITABLE)) {
3077 xyerror(D_OP_WRITE, "operator %s can only be applied "
3078 "to a writable variable\n", opstr(dnp->dn_op));
3079 }
3080
3081 dt_node_type_propagate(cp, dnp); /* see K&R[A7.4.1] */
3082 break;
3083
3084 default:
3085 xyerror(D_UNKNOWN, "invalid unary op %s\n", opstr(dnp->dn_op));
3086 }
3087
3088 dt_node_attr_assign(dnp, cp->dn_attr);
3089 return (dnp);
3090}
3091
3092static void
3093dt_assign_common(dt_node_t *dnp)
3094{
3095 dt_node_t *lp = dnp->dn_left;
3096 dt_node_t *rp = dnp->dn_right;
3097 int op = dnp->dn_op;
3098
3099 if (rp->dn_kind == DT_NODE_INT)
3100 dt_cast(lp, rp);
3101
3102 if (!(lp->dn_flags & DT_NF_LVALUE)) {
3103 xyerror(D_OP_LVAL, "operator %s requires modifiable "
3104 "lvalue as an operand\n", opstr(op));
3105 /* see K&R[A7.17] */
3106 }
3107
3108 if (!(lp->dn_flags & DT_NF_WRITABLE)) {
3109 xyerror(D_OP_WRITE, "operator %s can only be applied "
3110 "to a writable variable\n", opstr(op));
3111 }
3112
3113 dt_node_type_propagate(lp, dnp); /* see K&R[A7.17] */
3114 dt_node_attr_assign(dnp, dt_attr_min(lp->dn_attr, rp->dn_attr));
3115}
3116
3117static dt_node_t *
3118dt_cook_op2(dt_node_t *dnp, uint_t idflags)
3119{
3120 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
3121 dt_node_t *lp = dnp->dn_left;
3122 dt_node_t *rp = dnp->dn_right;
3123 int op = dnp->dn_op;
3124
3125 ctf_membinfo_t m;
3126 ctf_file_t *ctfp;
3127 ctf_id_t type;
3128 int kind, val, uref;
3129 dt_ident_t *idp;
3130
3131 char n1[DT_TYPE_NAMELEN];
3132 char n2[DT_TYPE_NAMELEN];
3133
3134 /*
3135 * The expression E1[E2] is identical by definition to *((E1)+(E2)) so
3136 * we convert "[" to "+" and glue on "*" at the end (see K&R[A7.3.1])
3137 * unless the left-hand side is an untyped D scalar, associative array,
3138 * or aggregation. In these cases, we proceed to case DT_TOK_LBRAC and
3139 * handle associative array and aggregation references there.
3140 */
3141 if (op == DT_TOK_LBRAC) {
3142 if (lp->dn_kind == DT_NODE_IDENT) {
3143 dt_idhash_t *dhp;
3144 uint_t idkind;
3145
3146 if (lp->dn_op == DT_TOK_AGG) {
3147 dhp = dtp->dt_aggs;
3148 idp = dt_idhash_lookup(dhp, lp->dn_string + 1);
3149 idkind = DT_IDENT_AGG;
3150 } else {
3151 dhp = dtp->dt_globals;
3152 idp = dt_idstack_lookup(
3153 &yypcb->pcb_globals, lp->dn_string);
3154 idkind = DT_IDENT_ARRAY;
3155 }
3156
3157 if (idp == NULL || dt_ident_unref(idp))
3158 dt_xcook_ident(lp, dhp, idkind, B_TRUE);
3159 else
3160 dt_xcook_ident(lp, dhp, idp->di_kind, B_FALSE);
3161 } else
3162 lp = dnp->dn_left = dt_node_cook(lp, 0);
3163
3164 /*
3165 * Switch op to '+' for *(E1 + E2) array mode in these cases:
3166 * (a) lp is a DT_IDENT_ARRAY variable that has already been
3167 * referenced using [] notation (dn_args != NULL).
3168 * (b) lp is a non-ARRAY variable that has already been given
3169 * a type by assignment or declaration (!dt_ident_unref())
3170 * (c) lp is neither a variable nor an aggregation
3171 */
3172 if (lp->dn_kind == DT_NODE_VAR) {
3173 if (lp->dn_ident->di_kind == DT_IDENT_ARRAY) {
3174 if (lp->dn_args != NULL)
3175 op = DT_TOK_ADD;
3176 } else if (!dt_ident_unref(lp->dn_ident))
3177 op = DT_TOK_ADD;
3178 } else if (lp->dn_kind != DT_NODE_AGG)
3179 op = DT_TOK_ADD;
3180 }
3181
3182 switch (op) {
3183 case DT_TOK_BAND:
3184 case DT_TOK_XOR:
3185 case DT_TOK_BOR:
3186 lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF);
3187 rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
3188
3189 if (!dt_node_is_integer(lp) || !dt_node_is_integer(rp)) {
3190 xyerror(D_OP_INT, "operator %s requires operands of "
3191 "integral type\n", opstr(op));
3192 }
3193
3194 dt_node_promote(lp, rp, dnp); /* see K&R[A7.11-13] */
3195 break;
3196
3197 case DT_TOK_LSH:
3198 case DT_TOK_RSH:
3199 lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF);
3200 rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
3201
3202 if (!dt_node_is_integer(lp) || !dt_node_is_integer(rp)) {
3203 xyerror(D_OP_INT, "operator %s requires operands of "
3204 "integral type\n", opstr(op));
3205 }
3206
3207 dt_node_type_propagate(lp, dnp); /* see K&R[A7.8] */
3208 dt_node_attr_assign(dnp, dt_attr_min(lp->dn_attr, rp->dn_attr));
3209 break;
3210
3211 case DT_TOK_MOD:
3212 lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF);
3213 rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
3214
3215 if (!dt_node_is_integer(lp) || !dt_node_is_integer(rp)) {
3216 xyerror(D_OP_INT, "operator %s requires operands of "
3217 "integral type\n", opstr(op));
3218 }
3219
3220 dt_node_promote(lp, rp, dnp); /* see K&R[A7.6] */
3221 break;
3222
3223 case DT_TOK_MUL:
3224 case DT_TOK_DIV:
3225 lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF);
3226 rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
3227
3228 if (!dt_node_is_arith(lp) || !dt_node_is_arith(rp)) {
3229 xyerror(D_OP_ARITH, "operator %s requires operands of "
3230 "arithmetic type\n", opstr(op));
3231 }
3232
3233 dt_node_promote(lp, rp, dnp); /* see K&R[A7.6] */
3234 break;
3235
3236 case DT_TOK_LAND:
3237 case DT_TOK_LXOR:
3238 case DT_TOK_LOR:
3239 lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF);
3240 rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
3241
3242 if (!dt_node_is_scalar(lp) || !dt_node_is_scalar(rp)) {
3243 xyerror(D_OP_SCALAR, "operator %s requires operands "
3244 "of scalar type\n", opstr(op));
3245 }
3246
3100 break;
3101
3102 case DT_TOK_PREINC:
3103 case DT_TOK_POSTINC:
3104 case DT_TOK_PREDEC:
3105 case DT_TOK_POSTDEC:
3106 if (dt_node_is_scalar(cp) == 0) {
3107 xyerror(D_OP_SCALAR, "operator %s requires operand of "
3108 "scalar type\n", opstr(dnp->dn_op));
3109 }
3110
3111 if (dt_node_is_vfptr(cp)) {
3112 xyerror(D_OP_VFPTR, "operator %s requires an operand "
3113 "of known size\n", opstr(dnp->dn_op));
3114 }
3115
3116 if (!(cp->dn_flags & DT_NF_LVALUE)) {
3117 xyerror(D_OP_LVAL, "operator %s requires modifiable "
3118 "lvalue as an operand\n", opstr(dnp->dn_op));
3119 }
3120
3121 if (!(cp->dn_flags & DT_NF_WRITABLE)) {
3122 xyerror(D_OP_WRITE, "operator %s can only be applied "
3123 "to a writable variable\n", opstr(dnp->dn_op));
3124 }
3125
3126 dt_node_type_propagate(cp, dnp); /* see K&R[A7.4.1] */
3127 break;
3128
3129 default:
3130 xyerror(D_UNKNOWN, "invalid unary op %s\n", opstr(dnp->dn_op));
3131 }
3132
3133 dt_node_attr_assign(dnp, cp->dn_attr);
3134 return (dnp);
3135}
3136
3137static void
3138dt_assign_common(dt_node_t *dnp)
3139{
3140 dt_node_t *lp = dnp->dn_left;
3141 dt_node_t *rp = dnp->dn_right;
3142 int op = dnp->dn_op;
3143
3144 if (rp->dn_kind == DT_NODE_INT)
3145 dt_cast(lp, rp);
3146
3147 if (!(lp->dn_flags & DT_NF_LVALUE)) {
3148 xyerror(D_OP_LVAL, "operator %s requires modifiable "
3149 "lvalue as an operand\n", opstr(op));
3150 /* see K&R[A7.17] */
3151 }
3152
3153 if (!(lp->dn_flags & DT_NF_WRITABLE)) {
3154 xyerror(D_OP_WRITE, "operator %s can only be applied "
3155 "to a writable variable\n", opstr(op));
3156 }
3157
3158 dt_node_type_propagate(lp, dnp); /* see K&R[A7.17] */
3159 dt_node_attr_assign(dnp, dt_attr_min(lp->dn_attr, rp->dn_attr));
3160}
3161
3162static dt_node_t *
3163dt_cook_op2(dt_node_t *dnp, uint_t idflags)
3164{
3165 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
3166 dt_node_t *lp = dnp->dn_left;
3167 dt_node_t *rp = dnp->dn_right;
3168 int op = dnp->dn_op;
3169
3170 ctf_membinfo_t m;
3171 ctf_file_t *ctfp;
3172 ctf_id_t type;
3173 int kind, val, uref;
3174 dt_ident_t *idp;
3175
3176 char n1[DT_TYPE_NAMELEN];
3177 char n2[DT_TYPE_NAMELEN];
3178
3179 /*
3180 * The expression E1[E2] is identical by definition to *((E1)+(E2)) so
3181 * we convert "[" to "+" and glue on "*" at the end (see K&R[A7.3.1])
3182 * unless the left-hand side is an untyped D scalar, associative array,
3183 * or aggregation. In these cases, we proceed to case DT_TOK_LBRAC and
3184 * handle associative array and aggregation references there.
3185 */
3186 if (op == DT_TOK_LBRAC) {
3187 if (lp->dn_kind == DT_NODE_IDENT) {
3188 dt_idhash_t *dhp;
3189 uint_t idkind;
3190
3191 if (lp->dn_op == DT_TOK_AGG) {
3192 dhp = dtp->dt_aggs;
3193 idp = dt_idhash_lookup(dhp, lp->dn_string + 1);
3194 idkind = DT_IDENT_AGG;
3195 } else {
3196 dhp = dtp->dt_globals;
3197 idp = dt_idstack_lookup(
3198 &yypcb->pcb_globals, lp->dn_string);
3199 idkind = DT_IDENT_ARRAY;
3200 }
3201
3202 if (idp == NULL || dt_ident_unref(idp))
3203 dt_xcook_ident(lp, dhp, idkind, B_TRUE);
3204 else
3205 dt_xcook_ident(lp, dhp, idp->di_kind, B_FALSE);
3206 } else
3207 lp = dnp->dn_left = dt_node_cook(lp, 0);
3208
3209 /*
3210 * Switch op to '+' for *(E1 + E2) array mode in these cases:
3211 * (a) lp is a DT_IDENT_ARRAY variable that has already been
3212 * referenced using [] notation (dn_args != NULL).
3213 * (b) lp is a non-ARRAY variable that has already been given
3214 * a type by assignment or declaration (!dt_ident_unref())
3215 * (c) lp is neither a variable nor an aggregation
3216 */
3217 if (lp->dn_kind == DT_NODE_VAR) {
3218 if (lp->dn_ident->di_kind == DT_IDENT_ARRAY) {
3219 if (lp->dn_args != NULL)
3220 op = DT_TOK_ADD;
3221 } else if (!dt_ident_unref(lp->dn_ident))
3222 op = DT_TOK_ADD;
3223 } else if (lp->dn_kind != DT_NODE_AGG)
3224 op = DT_TOK_ADD;
3225 }
3226
3227 switch (op) {
3228 case DT_TOK_BAND:
3229 case DT_TOK_XOR:
3230 case DT_TOK_BOR:
3231 lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF);
3232 rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
3233
3234 if (!dt_node_is_integer(lp) || !dt_node_is_integer(rp)) {
3235 xyerror(D_OP_INT, "operator %s requires operands of "
3236 "integral type\n", opstr(op));
3237 }
3238
3239 dt_node_promote(lp, rp, dnp); /* see K&R[A7.11-13] */
3240 break;
3241
3242 case DT_TOK_LSH:
3243 case DT_TOK_RSH:
3244 lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF);
3245 rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
3246
3247 if (!dt_node_is_integer(lp) || !dt_node_is_integer(rp)) {
3248 xyerror(D_OP_INT, "operator %s requires operands of "
3249 "integral type\n", opstr(op));
3250 }
3251
3252 dt_node_type_propagate(lp, dnp); /* see K&R[A7.8] */
3253 dt_node_attr_assign(dnp, dt_attr_min(lp->dn_attr, rp->dn_attr));
3254 break;
3255
3256 case DT_TOK_MOD:
3257 lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF);
3258 rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
3259
3260 if (!dt_node_is_integer(lp) || !dt_node_is_integer(rp)) {
3261 xyerror(D_OP_INT, "operator %s requires operands of "
3262 "integral type\n", opstr(op));
3263 }
3264
3265 dt_node_promote(lp, rp, dnp); /* see K&R[A7.6] */
3266 break;
3267
3268 case DT_TOK_MUL:
3269 case DT_TOK_DIV:
3270 lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF);
3271 rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
3272
3273 if (!dt_node_is_arith(lp) || !dt_node_is_arith(rp)) {
3274 xyerror(D_OP_ARITH, "operator %s requires operands of "
3275 "arithmetic type\n", opstr(op));
3276 }
3277
3278 dt_node_promote(lp, rp, dnp); /* see K&R[A7.6] */
3279 break;
3280
3281 case DT_TOK_LAND:
3282 case DT_TOK_LXOR:
3283 case DT_TOK_LOR:
3284 lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF);
3285 rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
3286
3287 if (!dt_node_is_scalar(lp) || !dt_node_is_scalar(rp)) {
3288 xyerror(D_OP_SCALAR, "operator %s requires operands "
3289 "of scalar type\n", opstr(op));
3290 }
3291
3247 dt_node_type_assign(dnp, DT_INT_CTFP(dtp), DT_INT_TYPE(dtp));
3292 dt_node_type_assign(dnp, DT_INT_CTFP(dtp), DT_INT_TYPE(dtp),
3293 B_FALSE);
3248 dt_node_attr_assign(dnp, dt_attr_min(lp->dn_attr, rp->dn_attr));
3249 break;
3250
3251 case DT_TOK_LT:
3252 case DT_TOK_LE:
3253 case DT_TOK_GT:
3254 case DT_TOK_GE:
3255 case DT_TOK_EQU:
3256 case DT_TOK_NEQ:
3257 /*
3258 * The D comparison operators provide the ability to transform
3259 * a right-hand identifier into a corresponding enum tag value
3260 * if the left-hand side is an enum type. To do this, we cook
3261 * the left-hand side, and then see if the right-hand side is
3262 * an unscoped identifier defined in the enum. If so, we
3263 * convert into an integer constant node with the tag's value.
3264 */
3265 lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF);
3266
3267 kind = ctf_type_kind(lp->dn_ctfp,
3268 ctf_type_resolve(lp->dn_ctfp, lp->dn_type));
3269
3270 if (kind == CTF_K_ENUM && rp->dn_kind == DT_NODE_IDENT &&
3271 strchr(rp->dn_string, '`') == NULL && ctf_enum_value(
3272 lp->dn_ctfp, lp->dn_type, rp->dn_string, &val) == 0) {
3273
3274 if ((idp = dt_idstack_lookup(&yypcb->pcb_globals,
3275 rp->dn_string)) != NULL) {
3276 xyerror(D_IDENT_AMBIG,
3277 "ambiguous use of operator %s: %s is "
3278 "both a %s enum tag and a global %s\n",
3279 opstr(op), rp->dn_string,
3280 dt_node_type_name(lp, n1, sizeof (n1)),
3281 dt_idkind_name(idp->di_kind));
3282 }
3283
3284 free(rp->dn_string);
3285 rp->dn_string = NULL;
3286 rp->dn_kind = DT_NODE_INT;
3287 rp->dn_flags |= DT_NF_COOKED;
3288 rp->dn_op = DT_TOK_INT;
3289 rp->dn_value = (intmax_t)val;
3290
3294 dt_node_attr_assign(dnp, dt_attr_min(lp->dn_attr, rp->dn_attr));
3295 break;
3296
3297 case DT_TOK_LT:
3298 case DT_TOK_LE:
3299 case DT_TOK_GT:
3300 case DT_TOK_GE:
3301 case DT_TOK_EQU:
3302 case DT_TOK_NEQ:
3303 /*
3304 * The D comparison operators provide the ability to transform
3305 * a right-hand identifier into a corresponding enum tag value
3306 * if the left-hand side is an enum type. To do this, we cook
3307 * the left-hand side, and then see if the right-hand side is
3308 * an unscoped identifier defined in the enum. If so, we
3309 * convert into an integer constant node with the tag's value.
3310 */
3311 lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF);
3312
3313 kind = ctf_type_kind(lp->dn_ctfp,
3314 ctf_type_resolve(lp->dn_ctfp, lp->dn_type));
3315
3316 if (kind == CTF_K_ENUM && rp->dn_kind == DT_NODE_IDENT &&
3317 strchr(rp->dn_string, '`') == NULL && ctf_enum_value(
3318 lp->dn_ctfp, lp->dn_type, rp->dn_string, &val) == 0) {
3319
3320 if ((idp = dt_idstack_lookup(&yypcb->pcb_globals,
3321 rp->dn_string)) != NULL) {
3322 xyerror(D_IDENT_AMBIG,
3323 "ambiguous use of operator %s: %s is "
3324 "both a %s enum tag and a global %s\n",
3325 opstr(op), rp->dn_string,
3326 dt_node_type_name(lp, n1, sizeof (n1)),
3327 dt_idkind_name(idp->di_kind));
3328 }
3329
3330 free(rp->dn_string);
3331 rp->dn_string = NULL;
3332 rp->dn_kind = DT_NODE_INT;
3333 rp->dn_flags |= DT_NF_COOKED;
3334 rp->dn_op = DT_TOK_INT;
3335 rp->dn_value = (intmax_t)val;
3336
3291 dt_node_type_assign(rp, lp->dn_ctfp, lp->dn_type);
3337 dt_node_type_assign(rp, lp->dn_ctfp, lp->dn_type,
3338 B_FALSE);
3292 dt_node_attr_assign(rp, _dtrace_symattr);
3293 }
3294
3295 rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
3296
3297 /*
3298 * The rules for type checking for the relational operators are
3299 * described in the ANSI-C spec (see K&R[A7.9-10]). We perform
3300 * the various tests in order from least to most expensive. We
3301 * also allow derived strings to be compared as a first-class
3302 * type (resulting in a strcmp(3C)-style comparison), and we
3303 * slightly relax the A7.9 rules to permit void pointer
3304 * comparisons as in A7.10. Our users won't be confused by
3305 * this since they understand pointers are just numbers, and
3306 * relaxing this constraint simplifies the implementation.
3307 */
3308 if (ctf_type_compat(lp->dn_ctfp, lp->dn_type,
3309 rp->dn_ctfp, rp->dn_type))
3310 /*EMPTY*/;
3311 else if (dt_node_is_integer(lp) && dt_node_is_integer(rp))
3312 /*EMPTY*/;
3313 else if (dt_node_is_strcompat(lp) && dt_node_is_strcompat(rp) &&
3314 (dt_node_is_string(lp) || dt_node_is_string(rp)))
3315 /*EMPTY*/;
3316 else if (dt_node_is_ptrcompat(lp, rp, NULL, NULL) == 0) {
3317 xyerror(D_OP_INCOMPAT, "operands have "
3318 "incompatible types: \"%s\" %s \"%s\"\n",
3319 dt_node_type_name(lp, n1, sizeof (n1)), opstr(op),
3320 dt_node_type_name(rp, n2, sizeof (n2)));
3321 }
3322
3339 dt_node_attr_assign(rp, _dtrace_symattr);
3340 }
3341
3342 rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
3343
3344 /*
3345 * The rules for type checking for the relational operators are
3346 * described in the ANSI-C spec (see K&R[A7.9-10]). We perform
3347 * the various tests in order from least to most expensive. We
3348 * also allow derived strings to be compared as a first-class
3349 * type (resulting in a strcmp(3C)-style comparison), and we
3350 * slightly relax the A7.9 rules to permit void pointer
3351 * comparisons as in A7.10. Our users won't be confused by
3352 * this since they understand pointers are just numbers, and
3353 * relaxing this constraint simplifies the implementation.
3354 */
3355 if (ctf_type_compat(lp->dn_ctfp, lp->dn_type,
3356 rp->dn_ctfp, rp->dn_type))
3357 /*EMPTY*/;
3358 else if (dt_node_is_integer(lp) && dt_node_is_integer(rp))
3359 /*EMPTY*/;
3360 else if (dt_node_is_strcompat(lp) && dt_node_is_strcompat(rp) &&
3361 (dt_node_is_string(lp) || dt_node_is_string(rp)))
3362 /*EMPTY*/;
3363 else if (dt_node_is_ptrcompat(lp, rp, NULL, NULL) == 0) {
3364 xyerror(D_OP_INCOMPAT, "operands have "
3365 "incompatible types: \"%s\" %s \"%s\"\n",
3366 dt_node_type_name(lp, n1, sizeof (n1)), opstr(op),
3367 dt_node_type_name(rp, n2, sizeof (n2)));
3368 }
3369
3323 dt_node_type_assign(dnp, DT_INT_CTFP(dtp), DT_INT_TYPE(dtp));
3370 dt_node_type_assign(dnp, DT_INT_CTFP(dtp), DT_INT_TYPE(dtp),
3371 B_FALSE);
3324 dt_node_attr_assign(dnp, dt_attr_min(lp->dn_attr, rp->dn_attr));
3325 break;
3326
3327 case DT_TOK_ADD:
3328 case DT_TOK_SUB: {
3329 /*
3330 * The rules for type checking for the additive operators are
3331 * described in the ANSI-C spec (see K&R[A7.7]). Pointers and
3332 * integers may be manipulated according to specific rules. In
3333 * these cases D permits strings to be treated as pointers.
3334 */
3335 int lp_is_ptr, lp_is_int, rp_is_ptr, rp_is_int;
3336
3337 lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF);
3338 rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
3339
3340 lp_is_ptr = dt_node_is_string(lp) ||
3341 (dt_node_is_pointer(lp) && !dt_node_is_vfptr(lp));
3342 lp_is_int = dt_node_is_integer(lp);
3343
3344 rp_is_ptr = dt_node_is_string(rp) ||
3345 (dt_node_is_pointer(rp) && !dt_node_is_vfptr(rp));
3346 rp_is_int = dt_node_is_integer(rp);
3347
3348 if (lp_is_int && rp_is_int) {
3349 dt_type_promote(lp, rp, &ctfp, &type);
3350 uref = 0;
3351 } else if (lp_is_ptr && rp_is_int) {
3352 ctfp = lp->dn_ctfp;
3353 type = lp->dn_type;
3354 uref = lp->dn_flags & DT_NF_USERLAND;
3355 } else if (lp_is_int && rp_is_ptr && op == DT_TOK_ADD) {
3356 ctfp = rp->dn_ctfp;
3357 type = rp->dn_type;
3358 uref = rp->dn_flags & DT_NF_USERLAND;
3359 } else if (lp_is_ptr && rp_is_ptr && op == DT_TOK_SUB &&
3360 dt_node_is_ptrcompat(lp, rp, NULL, NULL)) {
3361 ctfp = dtp->dt_ddefs->dm_ctfp;
3362 type = ctf_lookup_by_name(ctfp, "ptrdiff_t");
3363 uref = 0;
3364 } else {
3365 xyerror(D_OP_INCOMPAT, "operands have incompatible "
3366 "types: \"%s\" %s \"%s\"\n",
3367 dt_node_type_name(lp, n1, sizeof (n1)), opstr(op),
3368 dt_node_type_name(rp, n2, sizeof (n2)));
3369 }
3370
3372 dt_node_attr_assign(dnp, dt_attr_min(lp->dn_attr, rp->dn_attr));
3373 break;
3374
3375 case DT_TOK_ADD:
3376 case DT_TOK_SUB: {
3377 /*
3378 * The rules for type checking for the additive operators are
3379 * described in the ANSI-C spec (see K&R[A7.7]). Pointers and
3380 * integers may be manipulated according to specific rules. In
3381 * these cases D permits strings to be treated as pointers.
3382 */
3383 int lp_is_ptr, lp_is_int, rp_is_ptr, rp_is_int;
3384
3385 lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF);
3386 rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
3387
3388 lp_is_ptr = dt_node_is_string(lp) ||
3389 (dt_node_is_pointer(lp) && !dt_node_is_vfptr(lp));
3390 lp_is_int = dt_node_is_integer(lp);
3391
3392 rp_is_ptr = dt_node_is_string(rp) ||
3393 (dt_node_is_pointer(rp) && !dt_node_is_vfptr(rp));
3394 rp_is_int = dt_node_is_integer(rp);
3395
3396 if (lp_is_int && rp_is_int) {
3397 dt_type_promote(lp, rp, &ctfp, &type);
3398 uref = 0;
3399 } else if (lp_is_ptr && rp_is_int) {
3400 ctfp = lp->dn_ctfp;
3401 type = lp->dn_type;
3402 uref = lp->dn_flags & DT_NF_USERLAND;
3403 } else if (lp_is_int && rp_is_ptr && op == DT_TOK_ADD) {
3404 ctfp = rp->dn_ctfp;
3405 type = rp->dn_type;
3406 uref = rp->dn_flags & DT_NF_USERLAND;
3407 } else if (lp_is_ptr && rp_is_ptr && op == DT_TOK_SUB &&
3408 dt_node_is_ptrcompat(lp, rp, NULL, NULL)) {
3409 ctfp = dtp->dt_ddefs->dm_ctfp;
3410 type = ctf_lookup_by_name(ctfp, "ptrdiff_t");
3411 uref = 0;
3412 } else {
3413 xyerror(D_OP_INCOMPAT, "operands have incompatible "
3414 "types: \"%s\" %s \"%s\"\n",
3415 dt_node_type_name(lp, n1, sizeof (n1)), opstr(op),
3416 dt_node_type_name(rp, n2, sizeof (n2)));
3417 }
3418
3371 dt_node_type_assign(dnp, ctfp, type);
3419 dt_node_type_assign(dnp, ctfp, type, B_FALSE);
3372 dt_node_attr_assign(dnp, dt_attr_min(lp->dn_attr, rp->dn_attr));
3373
3374 if (uref)
3375 dnp->dn_flags |= DT_NF_USERLAND;
3376 break;
3377 }
3378
3379 case DT_TOK_OR_EQ:
3380 case DT_TOK_XOR_EQ:
3381 case DT_TOK_AND_EQ:
3382 case DT_TOK_LSH_EQ:
3383 case DT_TOK_RSH_EQ:
3384 case DT_TOK_MOD_EQ:
3385 if (lp->dn_kind == DT_NODE_IDENT) {
3386 dt_xcook_ident(lp, dtp->dt_globals,
3387 DT_IDENT_SCALAR, B_TRUE);
3388 }
3389
3390 lp = dnp->dn_left =
3391 dt_node_cook(lp, DT_IDFLG_REF | DT_IDFLG_MOD);
3392
3393 rp = dnp->dn_right =
3394 dt_node_cook(rp, DT_IDFLG_REF | DT_IDFLG_MOD);
3395
3396 if (!dt_node_is_integer(lp) || !dt_node_is_integer(rp)) {
3397 xyerror(D_OP_INT, "operator %s requires operands of "
3398 "integral type\n", opstr(op));
3399 }
3400 goto asgn_common;
3401
3402 case DT_TOK_MUL_EQ:
3403 case DT_TOK_DIV_EQ:
3404 if (lp->dn_kind == DT_NODE_IDENT) {
3405 dt_xcook_ident(lp, dtp->dt_globals,
3406 DT_IDENT_SCALAR, B_TRUE);
3407 }
3408
3409 lp = dnp->dn_left =
3410 dt_node_cook(lp, DT_IDFLG_REF | DT_IDFLG_MOD);
3411
3412 rp = dnp->dn_right =
3413 dt_node_cook(rp, DT_IDFLG_REF | DT_IDFLG_MOD);
3414
3415 if (!dt_node_is_arith(lp) || !dt_node_is_arith(rp)) {
3416 xyerror(D_OP_ARITH, "operator %s requires operands of "
3417 "arithmetic type\n", opstr(op));
3418 }
3419 goto asgn_common;
3420
3421 case DT_TOK_ASGN:
3422 /*
3423 * If the left-hand side is an identifier, attempt to resolve
3424 * it as either an aggregation or scalar variable. We pass
3425 * B_TRUE to dt_xcook_ident to indicate that a new variable can
3426 * be created if no matching variable exists in the namespace.
3427 */
3428 if (lp->dn_kind == DT_NODE_IDENT) {
3429 if (lp->dn_op == DT_TOK_AGG) {
3430 dt_xcook_ident(lp, dtp->dt_aggs,
3431 DT_IDENT_AGG, B_TRUE);
3432 } else {
3433 dt_xcook_ident(lp, dtp->dt_globals,
3434 DT_IDENT_SCALAR, B_TRUE);
3435 }
3436 }
3437
3438 lp = dnp->dn_left = dt_node_cook(lp, 0); /* don't set mod yet */
3439 rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
3440
3441 /*
3442 * If the left-hand side is an aggregation, verify that we are
3443 * assigning it the result of an aggregating function. Once
3444 * we've done so, hide the func node in the aggregation and
3445 * return the aggregation itself up to the parse tree parent.
3446 * This transformation is legal since the assigned function
3447 * cannot change identity across disjoint cooking passes and
3448 * the argument list subtree is retained for later cooking.
3449 */
3450 if (lp->dn_kind == DT_NODE_AGG) {
3451 const char *aname = lp->dn_ident->di_name;
3452 dt_ident_t *oid = lp->dn_ident->di_iarg;
3453
3454 if (rp->dn_kind != DT_NODE_FUNC ||
3455 rp->dn_ident->di_kind != DT_IDENT_AGGFUNC) {
3456 xyerror(D_AGG_FUNC,
3457 "@%s must be assigned the result of "
3458 "an aggregating function\n", aname);
3459 }
3460
3461 if (oid != NULL && oid != rp->dn_ident) {
3462 xyerror(D_AGG_REDEF,
3463 "aggregation redefined: @%s\n\t "
3464 "current: @%s = %s( )\n\tprevious: @%s = "
3465 "%s( ) : line %d\n", aname, aname,
3466 rp->dn_ident->di_name, aname, oid->di_name,
3467 lp->dn_ident->di_lineno);
3468 } else if (oid == NULL)
3469 lp->dn_ident->di_iarg = rp->dn_ident;
3470
3471 /*
3472 * Do not allow multiple aggregation assignments in a
3473 * single statement, e.g. (@a = count()) = count();
3474 * We produce a message as if the result of aggregating
3475 * function does not propagate DT_NF_LVALUE.
3476 */
3477 if (lp->dn_aggfun != NULL) {
3478 xyerror(D_OP_LVAL, "operator = requires "
3479 "modifiable lvalue as an operand\n");
3480 }
3481
3482 lp->dn_aggfun = rp;
3483 lp = dt_node_cook(lp, DT_IDFLG_MOD);
3484
3485 dnp->dn_left = dnp->dn_right = NULL;
3486 dt_node_free(dnp);
3487
3488 return (lp);
3489 }
3490
3491 /*
3492 * If the right-hand side is a dynamic variable that is the
3493 * output of a translator, our result is the translated type.
3494 */
3495 if ((idp = dt_node_resolve(rp, DT_IDENT_XLSOU)) != NULL) {
3496 ctfp = idp->di_ctfp;
3497 type = idp->di_type;
3498 uref = idp->di_flags & DT_IDFLG_USER;
3499 } else {
3500 ctfp = rp->dn_ctfp;
3501 type = rp->dn_type;
3502 uref = rp->dn_flags & DT_NF_USERLAND;
3503 }
3504
3505 /*
3506 * If the left-hand side of an assignment statement is a virgin
3507 * variable created by this compilation pass, reset the type of
3508 * this variable to the type of the right-hand side.
3509 */
3510 if (lp->dn_kind == DT_NODE_VAR &&
3511 dt_ident_unref(lp->dn_ident)) {
3420 dt_node_attr_assign(dnp, dt_attr_min(lp->dn_attr, rp->dn_attr));
3421
3422 if (uref)
3423 dnp->dn_flags |= DT_NF_USERLAND;
3424 break;
3425 }
3426
3427 case DT_TOK_OR_EQ:
3428 case DT_TOK_XOR_EQ:
3429 case DT_TOK_AND_EQ:
3430 case DT_TOK_LSH_EQ:
3431 case DT_TOK_RSH_EQ:
3432 case DT_TOK_MOD_EQ:
3433 if (lp->dn_kind == DT_NODE_IDENT) {
3434 dt_xcook_ident(lp, dtp->dt_globals,
3435 DT_IDENT_SCALAR, B_TRUE);
3436 }
3437
3438 lp = dnp->dn_left =
3439 dt_node_cook(lp, DT_IDFLG_REF | DT_IDFLG_MOD);
3440
3441 rp = dnp->dn_right =
3442 dt_node_cook(rp, DT_IDFLG_REF | DT_IDFLG_MOD);
3443
3444 if (!dt_node_is_integer(lp) || !dt_node_is_integer(rp)) {
3445 xyerror(D_OP_INT, "operator %s requires operands of "
3446 "integral type\n", opstr(op));
3447 }
3448 goto asgn_common;
3449
3450 case DT_TOK_MUL_EQ:
3451 case DT_TOK_DIV_EQ:
3452 if (lp->dn_kind == DT_NODE_IDENT) {
3453 dt_xcook_ident(lp, dtp->dt_globals,
3454 DT_IDENT_SCALAR, B_TRUE);
3455 }
3456
3457 lp = dnp->dn_left =
3458 dt_node_cook(lp, DT_IDFLG_REF | DT_IDFLG_MOD);
3459
3460 rp = dnp->dn_right =
3461 dt_node_cook(rp, DT_IDFLG_REF | DT_IDFLG_MOD);
3462
3463 if (!dt_node_is_arith(lp) || !dt_node_is_arith(rp)) {
3464 xyerror(D_OP_ARITH, "operator %s requires operands of "
3465 "arithmetic type\n", opstr(op));
3466 }
3467 goto asgn_common;
3468
3469 case DT_TOK_ASGN:
3470 /*
3471 * If the left-hand side is an identifier, attempt to resolve
3472 * it as either an aggregation or scalar variable. We pass
3473 * B_TRUE to dt_xcook_ident to indicate that a new variable can
3474 * be created if no matching variable exists in the namespace.
3475 */
3476 if (lp->dn_kind == DT_NODE_IDENT) {
3477 if (lp->dn_op == DT_TOK_AGG) {
3478 dt_xcook_ident(lp, dtp->dt_aggs,
3479 DT_IDENT_AGG, B_TRUE);
3480 } else {
3481 dt_xcook_ident(lp, dtp->dt_globals,
3482 DT_IDENT_SCALAR, B_TRUE);
3483 }
3484 }
3485
3486 lp = dnp->dn_left = dt_node_cook(lp, 0); /* don't set mod yet */
3487 rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
3488
3489 /*
3490 * If the left-hand side is an aggregation, verify that we are
3491 * assigning it the result of an aggregating function. Once
3492 * we've done so, hide the func node in the aggregation and
3493 * return the aggregation itself up to the parse tree parent.
3494 * This transformation is legal since the assigned function
3495 * cannot change identity across disjoint cooking passes and
3496 * the argument list subtree is retained for later cooking.
3497 */
3498 if (lp->dn_kind == DT_NODE_AGG) {
3499 const char *aname = lp->dn_ident->di_name;
3500 dt_ident_t *oid = lp->dn_ident->di_iarg;
3501
3502 if (rp->dn_kind != DT_NODE_FUNC ||
3503 rp->dn_ident->di_kind != DT_IDENT_AGGFUNC) {
3504 xyerror(D_AGG_FUNC,
3505 "@%s must be assigned the result of "
3506 "an aggregating function\n", aname);
3507 }
3508
3509 if (oid != NULL && oid != rp->dn_ident) {
3510 xyerror(D_AGG_REDEF,
3511 "aggregation redefined: @%s\n\t "
3512 "current: @%s = %s( )\n\tprevious: @%s = "
3513 "%s( ) : line %d\n", aname, aname,
3514 rp->dn_ident->di_name, aname, oid->di_name,
3515 lp->dn_ident->di_lineno);
3516 } else if (oid == NULL)
3517 lp->dn_ident->di_iarg = rp->dn_ident;
3518
3519 /*
3520 * Do not allow multiple aggregation assignments in a
3521 * single statement, e.g. (@a = count()) = count();
3522 * We produce a message as if the result of aggregating
3523 * function does not propagate DT_NF_LVALUE.
3524 */
3525 if (lp->dn_aggfun != NULL) {
3526 xyerror(D_OP_LVAL, "operator = requires "
3527 "modifiable lvalue as an operand\n");
3528 }
3529
3530 lp->dn_aggfun = rp;
3531 lp = dt_node_cook(lp, DT_IDFLG_MOD);
3532
3533 dnp->dn_left = dnp->dn_right = NULL;
3534 dt_node_free(dnp);
3535
3536 return (lp);
3537 }
3538
3539 /*
3540 * If the right-hand side is a dynamic variable that is the
3541 * output of a translator, our result is the translated type.
3542 */
3543 if ((idp = dt_node_resolve(rp, DT_IDENT_XLSOU)) != NULL) {
3544 ctfp = idp->di_ctfp;
3545 type = idp->di_type;
3546 uref = idp->di_flags & DT_IDFLG_USER;
3547 } else {
3548 ctfp = rp->dn_ctfp;
3549 type = rp->dn_type;
3550 uref = rp->dn_flags & DT_NF_USERLAND;
3551 }
3552
3553 /*
3554 * If the left-hand side of an assignment statement is a virgin
3555 * variable created by this compilation pass, reset the type of
3556 * this variable to the type of the right-hand side.
3557 */
3558 if (lp->dn_kind == DT_NODE_VAR &&
3559 dt_ident_unref(lp->dn_ident)) {
3512 dt_node_type_assign(lp, ctfp, type);
3560 dt_node_type_assign(lp, ctfp, type, B_FALSE);
3513 dt_ident_type_assign(lp->dn_ident, ctfp, type);
3514
3515 if (uref) {
3516 lp->dn_flags |= DT_NF_USERLAND;
3517 lp->dn_ident->di_flags |= DT_IDFLG_USER;
3518 }
3519 }
3520
3521 if (lp->dn_kind == DT_NODE_VAR)
3522 lp->dn_ident->di_flags |= DT_IDFLG_MOD;
3523
3524 /*
3525 * The rules for type checking for the assignment operators are
3526 * described in the ANSI-C spec (see K&R[A7.17]). We share
3527 * most of this code with the argument list checking code.
3528 */
3529 if (!dt_node_is_string(lp)) {
3530 kind = ctf_type_kind(lp->dn_ctfp,
3531 ctf_type_resolve(lp->dn_ctfp, lp->dn_type));
3532
3533 if (kind == CTF_K_ARRAY || kind == CTF_K_FUNCTION) {
3534 xyerror(D_OP_ARRFUN, "operator %s may not be "
3535 "applied to operand of type \"%s\"\n",
3536 opstr(op),
3537 dt_node_type_name(lp, n1, sizeof (n1)));
3538 }
3539 }
3540
3541 if (idp != NULL && idp->di_kind == DT_IDENT_XLSOU &&
3542 ctf_type_compat(lp->dn_ctfp, lp->dn_type, ctfp, type))
3543 goto asgn_common;
3544
3545 if (dt_node_is_argcompat(lp, rp))
3546 goto asgn_common;
3547
3548 xyerror(D_OP_INCOMPAT,
3549 "operands have incompatible types: \"%s\" %s \"%s\"\n",
3550 dt_node_type_name(lp, n1, sizeof (n1)), opstr(op),
3551 dt_node_type_name(rp, n2, sizeof (n2)));
3552 /*NOTREACHED*/
3553
3554 case DT_TOK_ADD_EQ:
3555 case DT_TOK_SUB_EQ:
3556 if (lp->dn_kind == DT_NODE_IDENT) {
3557 dt_xcook_ident(lp, dtp->dt_globals,
3558 DT_IDENT_SCALAR, B_TRUE);
3559 }
3560
3561 lp = dnp->dn_left =
3562 dt_node_cook(lp, DT_IDFLG_REF | DT_IDFLG_MOD);
3563
3564 rp = dnp->dn_right =
3565 dt_node_cook(rp, DT_IDFLG_REF | DT_IDFLG_MOD);
3566
3567 if (dt_node_is_string(lp) || dt_node_is_string(rp)) {
3568 xyerror(D_OP_INCOMPAT, "operands have "
3569 "incompatible types: \"%s\" %s \"%s\"\n",
3570 dt_node_type_name(lp, n1, sizeof (n1)), opstr(op),
3571 dt_node_type_name(rp, n2, sizeof (n2)));
3572 }
3573
3574 /*
3575 * The rules for type checking for the assignment operators are
3576 * described in the ANSI-C spec (see K&R[A7.17]). To these
3577 * rules we add that only writable D nodes can be modified.
3578 */
3579 if (dt_node_is_integer(lp) == 0 ||
3580 dt_node_is_integer(rp) == 0) {
3581 if (!dt_node_is_pointer(lp) || dt_node_is_vfptr(lp)) {
3582 xyerror(D_OP_VFPTR,
3583 "operator %s requires left-hand scalar "
3584 "operand of known size\n", opstr(op));
3585 } else if (dt_node_is_integer(rp) == 0 &&
3586 dt_node_is_ptrcompat(lp, rp, NULL, NULL) == 0) {
3587 xyerror(D_OP_INCOMPAT, "operands have "
3588 "incompatible types: \"%s\" %s \"%s\"\n",
3589 dt_node_type_name(lp, n1, sizeof (n1)),
3590 opstr(op),
3591 dt_node_type_name(rp, n2, sizeof (n2)));
3592 }
3593 }
3594asgn_common:
3595 dt_assign_common(dnp);
3596 break;
3597
3598 case DT_TOK_PTR:
3599 /*
3600 * If the left-hand side of operator -> is the name "self",
3601 * then we permit a TLS variable to be created or referenced.
3602 */
3603 if (lp->dn_kind == DT_NODE_IDENT &&
3604 strcmp(lp->dn_string, "self") == 0) {
3605 if (rp->dn_kind != DT_NODE_VAR) {
3606 dt_xcook_ident(rp, dtp->dt_tls,
3607 DT_IDENT_SCALAR, B_TRUE);
3608 }
3609
3610 if (idflags != 0)
3611 rp = dt_node_cook(rp, idflags);
3612
3613 dnp->dn_right = dnp->dn_left; /* avoid freeing rp */
3614 dt_node_free(dnp);
3615 return (rp);
3616 }
3617
3618 /*
3619 * If the left-hand side of operator -> is the name "this",
3620 * then we permit a local variable to be created or referenced.
3621 */
3622 if (lp->dn_kind == DT_NODE_IDENT &&
3623 strcmp(lp->dn_string, "this") == 0) {
3624 if (rp->dn_kind != DT_NODE_VAR) {
3625 dt_xcook_ident(rp, yypcb->pcb_locals,
3626 DT_IDENT_SCALAR, B_TRUE);
3627 }
3628
3629 if (idflags != 0)
3630 rp = dt_node_cook(rp, idflags);
3631
3632 dnp->dn_right = dnp->dn_left; /* avoid freeing rp */
3633 dt_node_free(dnp);
3634 return (rp);
3635 }
3636
3637 /*FALLTHRU*/
3638
3639 case DT_TOK_DOT:
3640 lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF);
3641
3642 if (rp->dn_kind != DT_NODE_IDENT) {
3643 xyerror(D_OP_IDENT, "operator %s must be followed by "
3644 "an identifier\n", opstr(op));
3645 }
3646
3647 if ((idp = dt_node_resolve(lp, DT_IDENT_XLSOU)) != NULL ||
3648 (idp = dt_node_resolve(lp, DT_IDENT_XLPTR)) != NULL) {
3649 /*
3650 * If the left-hand side is a translated struct or ptr,
3651 * the type of the left is the translation output type.
3652 */
3653 dt_xlator_t *dxp = idp->di_data;
3654
3655 if (dt_xlator_member(dxp, rp->dn_string) == NULL) {
3656 xyerror(D_XLATE_NOCONV,
3657 "translator does not define conversion "
3658 "for member: %s\n", rp->dn_string);
3659 }
3660
3661 ctfp = idp->di_ctfp;
3662 type = ctf_type_resolve(ctfp, idp->di_type);
3663 uref = idp->di_flags & DT_IDFLG_USER;
3664 } else {
3665 ctfp = lp->dn_ctfp;
3666 type = ctf_type_resolve(ctfp, lp->dn_type);
3667 uref = lp->dn_flags & DT_NF_USERLAND;
3668 }
3669
3670 kind = ctf_type_kind(ctfp, type);
3671
3672 if (op == DT_TOK_PTR) {
3673 if (kind != CTF_K_POINTER) {
3674 xyerror(D_OP_PTR, "operator %s must be "
3675 "applied to a pointer\n", opstr(op));
3676 }
3677 type = ctf_type_reference(ctfp, type);
3678 type = ctf_type_resolve(ctfp, type);
3679 kind = ctf_type_kind(ctfp, type);
3680 }
3681
3682 /*
3683 * If we follow a reference to a forward declaration tag,
3684 * search the entire type space for the actual definition.
3685 */
3686 while (kind == CTF_K_FORWARD) {
3687 char *tag = ctf_type_name(ctfp, type, n1, sizeof (n1));
3688 dtrace_typeinfo_t dtt;
3689
3690 if (tag != NULL && dt_type_lookup(tag, &dtt) == 0 &&
3691 (dtt.dtt_ctfp != ctfp || dtt.dtt_type != type)) {
3692 ctfp = dtt.dtt_ctfp;
3693 type = ctf_type_resolve(ctfp, dtt.dtt_type);
3694 kind = ctf_type_kind(ctfp, type);
3695 } else {
3696 xyerror(D_OP_INCOMPLETE,
3697 "operator %s cannot be applied to a "
3698 "forward declaration: no %s definition "
3699 "is available\n", opstr(op), tag);
3700 }
3701 }
3702
3703 if (kind != CTF_K_STRUCT && kind != CTF_K_UNION) {
3704 if (op == DT_TOK_PTR) {
3705 xyerror(D_OP_SOU, "operator -> cannot be "
3706 "applied to pointer to type \"%s\"; must "
3707 "be applied to a struct or union pointer\n",
3708 ctf_type_name(ctfp, type, n1, sizeof (n1)));
3709 } else {
3710 xyerror(D_OP_SOU, "operator %s cannot be "
3711 "applied to type \"%s\"; must be applied "
3712 "to a struct or union\n", opstr(op),
3713 ctf_type_name(ctfp, type, n1, sizeof (n1)));
3714 }
3715 }
3716
3717 if (ctf_member_info(ctfp, type, rp->dn_string, &m) == CTF_ERR) {
3718 xyerror(D_TYPE_MEMBER,
3719 "%s is not a member of %s\n", rp->dn_string,
3720 ctf_type_name(ctfp, type, n1, sizeof (n1)));
3721 }
3722
3723 type = ctf_type_resolve(ctfp, m.ctm_type);
3724 kind = ctf_type_kind(ctfp, type);
3725
3561 dt_ident_type_assign(lp->dn_ident, ctfp, type);
3562
3563 if (uref) {
3564 lp->dn_flags |= DT_NF_USERLAND;
3565 lp->dn_ident->di_flags |= DT_IDFLG_USER;
3566 }
3567 }
3568
3569 if (lp->dn_kind == DT_NODE_VAR)
3570 lp->dn_ident->di_flags |= DT_IDFLG_MOD;
3571
3572 /*
3573 * The rules for type checking for the assignment operators are
3574 * described in the ANSI-C spec (see K&R[A7.17]). We share
3575 * most of this code with the argument list checking code.
3576 */
3577 if (!dt_node_is_string(lp)) {
3578 kind = ctf_type_kind(lp->dn_ctfp,
3579 ctf_type_resolve(lp->dn_ctfp, lp->dn_type));
3580
3581 if (kind == CTF_K_ARRAY || kind == CTF_K_FUNCTION) {
3582 xyerror(D_OP_ARRFUN, "operator %s may not be "
3583 "applied to operand of type \"%s\"\n",
3584 opstr(op),
3585 dt_node_type_name(lp, n1, sizeof (n1)));
3586 }
3587 }
3588
3589 if (idp != NULL && idp->di_kind == DT_IDENT_XLSOU &&
3590 ctf_type_compat(lp->dn_ctfp, lp->dn_type, ctfp, type))
3591 goto asgn_common;
3592
3593 if (dt_node_is_argcompat(lp, rp))
3594 goto asgn_common;
3595
3596 xyerror(D_OP_INCOMPAT,
3597 "operands have incompatible types: \"%s\" %s \"%s\"\n",
3598 dt_node_type_name(lp, n1, sizeof (n1)), opstr(op),
3599 dt_node_type_name(rp, n2, sizeof (n2)));
3600 /*NOTREACHED*/
3601
3602 case DT_TOK_ADD_EQ:
3603 case DT_TOK_SUB_EQ:
3604 if (lp->dn_kind == DT_NODE_IDENT) {
3605 dt_xcook_ident(lp, dtp->dt_globals,
3606 DT_IDENT_SCALAR, B_TRUE);
3607 }
3608
3609 lp = dnp->dn_left =
3610 dt_node_cook(lp, DT_IDFLG_REF | DT_IDFLG_MOD);
3611
3612 rp = dnp->dn_right =
3613 dt_node_cook(rp, DT_IDFLG_REF | DT_IDFLG_MOD);
3614
3615 if (dt_node_is_string(lp) || dt_node_is_string(rp)) {
3616 xyerror(D_OP_INCOMPAT, "operands have "
3617 "incompatible types: \"%s\" %s \"%s\"\n",
3618 dt_node_type_name(lp, n1, sizeof (n1)), opstr(op),
3619 dt_node_type_name(rp, n2, sizeof (n2)));
3620 }
3621
3622 /*
3623 * The rules for type checking for the assignment operators are
3624 * described in the ANSI-C spec (see K&R[A7.17]). To these
3625 * rules we add that only writable D nodes can be modified.
3626 */
3627 if (dt_node_is_integer(lp) == 0 ||
3628 dt_node_is_integer(rp) == 0) {
3629 if (!dt_node_is_pointer(lp) || dt_node_is_vfptr(lp)) {
3630 xyerror(D_OP_VFPTR,
3631 "operator %s requires left-hand scalar "
3632 "operand of known size\n", opstr(op));
3633 } else if (dt_node_is_integer(rp) == 0 &&
3634 dt_node_is_ptrcompat(lp, rp, NULL, NULL) == 0) {
3635 xyerror(D_OP_INCOMPAT, "operands have "
3636 "incompatible types: \"%s\" %s \"%s\"\n",
3637 dt_node_type_name(lp, n1, sizeof (n1)),
3638 opstr(op),
3639 dt_node_type_name(rp, n2, sizeof (n2)));
3640 }
3641 }
3642asgn_common:
3643 dt_assign_common(dnp);
3644 break;
3645
3646 case DT_TOK_PTR:
3647 /*
3648 * If the left-hand side of operator -> is the name "self",
3649 * then we permit a TLS variable to be created or referenced.
3650 */
3651 if (lp->dn_kind == DT_NODE_IDENT &&
3652 strcmp(lp->dn_string, "self") == 0) {
3653 if (rp->dn_kind != DT_NODE_VAR) {
3654 dt_xcook_ident(rp, dtp->dt_tls,
3655 DT_IDENT_SCALAR, B_TRUE);
3656 }
3657
3658 if (idflags != 0)
3659 rp = dt_node_cook(rp, idflags);
3660
3661 dnp->dn_right = dnp->dn_left; /* avoid freeing rp */
3662 dt_node_free(dnp);
3663 return (rp);
3664 }
3665
3666 /*
3667 * If the left-hand side of operator -> is the name "this",
3668 * then we permit a local variable to be created or referenced.
3669 */
3670 if (lp->dn_kind == DT_NODE_IDENT &&
3671 strcmp(lp->dn_string, "this") == 0) {
3672 if (rp->dn_kind != DT_NODE_VAR) {
3673 dt_xcook_ident(rp, yypcb->pcb_locals,
3674 DT_IDENT_SCALAR, B_TRUE);
3675 }
3676
3677 if (idflags != 0)
3678 rp = dt_node_cook(rp, idflags);
3679
3680 dnp->dn_right = dnp->dn_left; /* avoid freeing rp */
3681 dt_node_free(dnp);
3682 return (rp);
3683 }
3684
3685 /*FALLTHRU*/
3686
3687 case DT_TOK_DOT:
3688 lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF);
3689
3690 if (rp->dn_kind != DT_NODE_IDENT) {
3691 xyerror(D_OP_IDENT, "operator %s must be followed by "
3692 "an identifier\n", opstr(op));
3693 }
3694
3695 if ((idp = dt_node_resolve(lp, DT_IDENT_XLSOU)) != NULL ||
3696 (idp = dt_node_resolve(lp, DT_IDENT_XLPTR)) != NULL) {
3697 /*
3698 * If the left-hand side is a translated struct or ptr,
3699 * the type of the left is the translation output type.
3700 */
3701 dt_xlator_t *dxp = idp->di_data;
3702
3703 if (dt_xlator_member(dxp, rp->dn_string) == NULL) {
3704 xyerror(D_XLATE_NOCONV,
3705 "translator does not define conversion "
3706 "for member: %s\n", rp->dn_string);
3707 }
3708
3709 ctfp = idp->di_ctfp;
3710 type = ctf_type_resolve(ctfp, idp->di_type);
3711 uref = idp->di_flags & DT_IDFLG_USER;
3712 } else {
3713 ctfp = lp->dn_ctfp;
3714 type = ctf_type_resolve(ctfp, lp->dn_type);
3715 uref = lp->dn_flags & DT_NF_USERLAND;
3716 }
3717
3718 kind = ctf_type_kind(ctfp, type);
3719
3720 if (op == DT_TOK_PTR) {
3721 if (kind != CTF_K_POINTER) {
3722 xyerror(D_OP_PTR, "operator %s must be "
3723 "applied to a pointer\n", opstr(op));
3724 }
3725 type = ctf_type_reference(ctfp, type);
3726 type = ctf_type_resolve(ctfp, type);
3727 kind = ctf_type_kind(ctfp, type);
3728 }
3729
3730 /*
3731 * If we follow a reference to a forward declaration tag,
3732 * search the entire type space for the actual definition.
3733 */
3734 while (kind == CTF_K_FORWARD) {
3735 char *tag = ctf_type_name(ctfp, type, n1, sizeof (n1));
3736 dtrace_typeinfo_t dtt;
3737
3738 if (tag != NULL && dt_type_lookup(tag, &dtt) == 0 &&
3739 (dtt.dtt_ctfp != ctfp || dtt.dtt_type != type)) {
3740 ctfp = dtt.dtt_ctfp;
3741 type = ctf_type_resolve(ctfp, dtt.dtt_type);
3742 kind = ctf_type_kind(ctfp, type);
3743 } else {
3744 xyerror(D_OP_INCOMPLETE,
3745 "operator %s cannot be applied to a "
3746 "forward declaration: no %s definition "
3747 "is available\n", opstr(op), tag);
3748 }
3749 }
3750
3751 if (kind != CTF_K_STRUCT && kind != CTF_K_UNION) {
3752 if (op == DT_TOK_PTR) {
3753 xyerror(D_OP_SOU, "operator -> cannot be "
3754 "applied to pointer to type \"%s\"; must "
3755 "be applied to a struct or union pointer\n",
3756 ctf_type_name(ctfp, type, n1, sizeof (n1)));
3757 } else {
3758 xyerror(D_OP_SOU, "operator %s cannot be "
3759 "applied to type \"%s\"; must be applied "
3760 "to a struct or union\n", opstr(op),
3761 ctf_type_name(ctfp, type, n1, sizeof (n1)));
3762 }
3763 }
3764
3765 if (ctf_member_info(ctfp, type, rp->dn_string, &m) == CTF_ERR) {
3766 xyerror(D_TYPE_MEMBER,
3767 "%s is not a member of %s\n", rp->dn_string,
3768 ctf_type_name(ctfp, type, n1, sizeof (n1)));
3769 }
3770
3771 type = ctf_type_resolve(ctfp, m.ctm_type);
3772 kind = ctf_type_kind(ctfp, type);
3773
3726 dt_node_type_assign(dnp, ctfp, m.ctm_type);
3774 dt_node_type_assign(dnp, ctfp, m.ctm_type, B_FALSE);
3727 dt_node_attr_assign(dnp, lp->dn_attr);
3728
3729 if (op == DT_TOK_PTR && (kind != CTF_K_ARRAY ||
3730 dt_node_is_string(dnp)))
3731 dnp->dn_flags |= DT_NF_LVALUE; /* see K&R[A7.3.3] */
3732
3733 if (op == DT_TOK_DOT && (lp->dn_flags & DT_NF_LVALUE) &&
3734 (kind != CTF_K_ARRAY || dt_node_is_string(dnp)))
3735 dnp->dn_flags |= DT_NF_LVALUE; /* see K&R[A7.3.3] */
3736
3737 if (lp->dn_flags & DT_NF_WRITABLE)
3738 dnp->dn_flags |= DT_NF_WRITABLE;
3739
3740 if (uref && (kind == CTF_K_POINTER ||
3741 (dnp->dn_flags & DT_NF_REF)))
3742 dnp->dn_flags |= DT_NF_USERLAND;
3743 break;
3744
3745 case DT_TOK_LBRAC: {
3746 /*
3747 * If op is DT_TOK_LBRAC, we know from the special-case code at
3748 * the top that lp is either a D variable or an aggregation.
3749 */
3750 dt_node_t *lnp;
3751
3752 /*
3753 * If the left-hand side is an aggregation, just set dn_aggtup
3754 * to the right-hand side and return the cooked aggregation.
3755 * This transformation is legal since we are just collapsing
3756 * nodes to simplify later processing, and the entire aggtup
3757 * parse subtree is retained for subsequent cooking passes.
3758 */
3759 if (lp->dn_kind == DT_NODE_AGG) {
3760 if (lp->dn_aggtup != NULL) {
3761 xyerror(D_AGG_MDIM, "improper attempt to "
3762 "reference @%s as a multi-dimensional "
3763 "array\n", lp->dn_ident->di_name);
3764 }
3765
3766 lp->dn_aggtup = rp;
3767 lp = dt_node_cook(lp, 0);
3768
3769 dnp->dn_left = dnp->dn_right = NULL;
3770 dt_node_free(dnp);
3771
3772 return (lp);
3773 }
3774
3775 assert(lp->dn_kind == DT_NODE_VAR);
3776 idp = lp->dn_ident;
3777
3778 /*
3779 * If the left-hand side is a non-global scalar that hasn't yet
3780 * been referenced or modified, it was just created by self->
3781 * or this-> and we can convert it from scalar to assoc array.
3782 */
3783 if (idp->di_kind == DT_IDENT_SCALAR && dt_ident_unref(idp) &&
3784 (idp->di_flags & (DT_IDFLG_LOCAL | DT_IDFLG_TLS)) != 0) {
3785
3786 if (idp->di_flags & DT_IDFLG_LOCAL) {
3787 xyerror(D_ARR_LOCAL,
3788 "local variables may not be used as "
3789 "associative arrays: %s\n", idp->di_name);
3790 }
3791
3792 dt_dprintf("morph variable %s (id %u) from scalar to "
3793 "array\n", idp->di_name, idp->di_id);
3794
3795 dt_ident_morph(idp, DT_IDENT_ARRAY,
3796 &dt_idops_assc, NULL);
3797 }
3798
3799 if (idp->di_kind != DT_IDENT_ARRAY) {
3800 xyerror(D_IDENT_BADREF, "%s '%s' may not be referenced "
3801 "as %s\n", dt_idkind_name(idp->di_kind),
3802 idp->di_name, dt_idkind_name(DT_IDENT_ARRAY));
3803 }
3804
3805 /*
3806 * Now that we've confirmed our left-hand side is a DT_NODE_VAR
3807 * of idkind DT_IDENT_ARRAY, we need to splice the [ node from
3808 * the parse tree and leave a cooked DT_NODE_VAR in its place
3809 * where dn_args for the VAR node is the right-hand 'rp' tree,
3810 * as shown in the parse tree diagram below:
3811 *
3812 * / /
3813 * [ OP2 "[" ]=dnp [ VAR ]=dnp
3814 * / \ => |
3815 * / \ +- dn_args -> [ ??? ]=rp
3816 * [ VAR ]=lp [ ??? ]=rp
3817 *
3818 * Since the final dt_node_cook(dnp) can fail using longjmp we
3819 * must perform the transformations as a group first by over-
3820 * writing 'dnp' to become the VAR node, so that the parse tree
3821 * is guaranteed to be in a consistent state if the cook fails.
3822 */
3823 assert(lp->dn_kind == DT_NODE_VAR);
3824 assert(lp->dn_args == NULL);
3825
3826 lnp = dnp->dn_link;
3827 bcopy(lp, dnp, sizeof (dt_node_t));
3828 dnp->dn_link = lnp;
3829
3830 dnp->dn_args = rp;
3831 dnp->dn_list = NULL;
3832
3833 dt_node_free(lp);
3834 return (dt_node_cook(dnp, idflags));
3835 }
3836
3837 case DT_TOK_XLATE: {
3838 dt_xlator_t *dxp;
3839
3840 assert(lp->dn_kind == DT_NODE_TYPE);
3841 rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
3842 dxp = dt_xlator_lookup(dtp, rp, lp, DT_XLATE_FUZZY);
3843
3844 if (dxp == NULL) {
3845 xyerror(D_XLATE_NONE,
3846 "cannot translate from \"%s\" to \"%s\"\n",
3847 dt_node_type_name(rp, n1, sizeof (n1)),
3848 dt_node_type_name(lp, n2, sizeof (n2)));
3849 }
3850
3851 dnp->dn_ident = dt_xlator_ident(dxp, lp->dn_ctfp, lp->dn_type);
3775 dt_node_attr_assign(dnp, lp->dn_attr);
3776
3777 if (op == DT_TOK_PTR && (kind != CTF_K_ARRAY ||
3778 dt_node_is_string(dnp)))
3779 dnp->dn_flags |= DT_NF_LVALUE; /* see K&R[A7.3.3] */
3780
3781 if (op == DT_TOK_DOT && (lp->dn_flags & DT_NF_LVALUE) &&
3782 (kind != CTF_K_ARRAY || dt_node_is_string(dnp)))
3783 dnp->dn_flags |= DT_NF_LVALUE; /* see K&R[A7.3.3] */
3784
3785 if (lp->dn_flags & DT_NF_WRITABLE)
3786 dnp->dn_flags |= DT_NF_WRITABLE;
3787
3788 if (uref && (kind == CTF_K_POINTER ||
3789 (dnp->dn_flags & DT_NF_REF)))
3790 dnp->dn_flags |= DT_NF_USERLAND;
3791 break;
3792
3793 case DT_TOK_LBRAC: {
3794 /*
3795 * If op is DT_TOK_LBRAC, we know from the special-case code at
3796 * the top that lp is either a D variable or an aggregation.
3797 */
3798 dt_node_t *lnp;
3799
3800 /*
3801 * If the left-hand side is an aggregation, just set dn_aggtup
3802 * to the right-hand side and return the cooked aggregation.
3803 * This transformation is legal since we are just collapsing
3804 * nodes to simplify later processing, and the entire aggtup
3805 * parse subtree is retained for subsequent cooking passes.
3806 */
3807 if (lp->dn_kind == DT_NODE_AGG) {
3808 if (lp->dn_aggtup != NULL) {
3809 xyerror(D_AGG_MDIM, "improper attempt to "
3810 "reference @%s as a multi-dimensional "
3811 "array\n", lp->dn_ident->di_name);
3812 }
3813
3814 lp->dn_aggtup = rp;
3815 lp = dt_node_cook(lp, 0);
3816
3817 dnp->dn_left = dnp->dn_right = NULL;
3818 dt_node_free(dnp);
3819
3820 return (lp);
3821 }
3822
3823 assert(lp->dn_kind == DT_NODE_VAR);
3824 idp = lp->dn_ident;
3825
3826 /*
3827 * If the left-hand side is a non-global scalar that hasn't yet
3828 * been referenced or modified, it was just created by self->
3829 * or this-> and we can convert it from scalar to assoc array.
3830 */
3831 if (idp->di_kind == DT_IDENT_SCALAR && dt_ident_unref(idp) &&
3832 (idp->di_flags & (DT_IDFLG_LOCAL | DT_IDFLG_TLS)) != 0) {
3833
3834 if (idp->di_flags & DT_IDFLG_LOCAL) {
3835 xyerror(D_ARR_LOCAL,
3836 "local variables may not be used as "
3837 "associative arrays: %s\n", idp->di_name);
3838 }
3839
3840 dt_dprintf("morph variable %s (id %u) from scalar to "
3841 "array\n", idp->di_name, idp->di_id);
3842
3843 dt_ident_morph(idp, DT_IDENT_ARRAY,
3844 &dt_idops_assc, NULL);
3845 }
3846
3847 if (idp->di_kind != DT_IDENT_ARRAY) {
3848 xyerror(D_IDENT_BADREF, "%s '%s' may not be referenced "
3849 "as %s\n", dt_idkind_name(idp->di_kind),
3850 idp->di_name, dt_idkind_name(DT_IDENT_ARRAY));
3851 }
3852
3853 /*
3854 * Now that we've confirmed our left-hand side is a DT_NODE_VAR
3855 * of idkind DT_IDENT_ARRAY, we need to splice the [ node from
3856 * the parse tree and leave a cooked DT_NODE_VAR in its place
3857 * where dn_args for the VAR node is the right-hand 'rp' tree,
3858 * as shown in the parse tree diagram below:
3859 *
3860 * / /
3861 * [ OP2 "[" ]=dnp [ VAR ]=dnp
3862 * / \ => |
3863 * / \ +- dn_args -> [ ??? ]=rp
3864 * [ VAR ]=lp [ ??? ]=rp
3865 *
3866 * Since the final dt_node_cook(dnp) can fail using longjmp we
3867 * must perform the transformations as a group first by over-
3868 * writing 'dnp' to become the VAR node, so that the parse tree
3869 * is guaranteed to be in a consistent state if the cook fails.
3870 */
3871 assert(lp->dn_kind == DT_NODE_VAR);
3872 assert(lp->dn_args == NULL);
3873
3874 lnp = dnp->dn_link;
3875 bcopy(lp, dnp, sizeof (dt_node_t));
3876 dnp->dn_link = lnp;
3877
3878 dnp->dn_args = rp;
3879 dnp->dn_list = NULL;
3880
3881 dt_node_free(lp);
3882 return (dt_node_cook(dnp, idflags));
3883 }
3884
3885 case DT_TOK_XLATE: {
3886 dt_xlator_t *dxp;
3887
3888 assert(lp->dn_kind == DT_NODE_TYPE);
3889 rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
3890 dxp = dt_xlator_lookup(dtp, rp, lp, DT_XLATE_FUZZY);
3891
3892 if (dxp == NULL) {
3893 xyerror(D_XLATE_NONE,
3894 "cannot translate from \"%s\" to \"%s\"\n",
3895 dt_node_type_name(rp, n1, sizeof (n1)),
3896 dt_node_type_name(lp, n2, sizeof (n2)));
3897 }
3898
3899 dnp->dn_ident = dt_xlator_ident(dxp, lp->dn_ctfp, lp->dn_type);
3852 dt_node_type_assign(dnp, DT_DYN_CTFP(dtp), DT_DYN_TYPE(dtp));
3900 dt_node_type_assign(dnp, DT_DYN_CTFP(dtp), DT_DYN_TYPE(dtp),
3901 B_FALSE);
3853 dt_node_attr_assign(dnp,
3854 dt_attr_min(rp->dn_attr, dnp->dn_ident->di_attr));
3855 break;
3856 }
3857
3858 case DT_TOK_LPAR: {
3859 ctf_id_t ltype, rtype;
3860 uint_t lkind, rkind;
3861
3862 assert(lp->dn_kind == DT_NODE_TYPE);
3863 rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
3864
3865 ltype = ctf_type_resolve(lp->dn_ctfp, lp->dn_type);
3866 lkind = ctf_type_kind(lp->dn_ctfp, ltype);
3867
3868 rtype = ctf_type_resolve(rp->dn_ctfp, rp->dn_type);
3869 rkind = ctf_type_kind(rp->dn_ctfp, rtype);
3870
3871 /*
3872 * The rules for casting are loosely explained in K&R[A7.5]
3873 * and K&R[A6]. Basically, we can cast to the same type or
3874 * same base type, between any kind of scalar values, from
3875 * arrays to pointers, and we can cast anything to void.
3876 * To these rules D adds casts from scalars to strings.
3877 */
3878 if (ctf_type_compat(lp->dn_ctfp, lp->dn_type,
3879 rp->dn_ctfp, rp->dn_type))
3880 /*EMPTY*/;
3881 else if (dt_node_is_scalar(lp) &&
3882 (dt_node_is_scalar(rp) || rkind == CTF_K_FUNCTION))
3883 /*EMPTY*/;
3884 else if (dt_node_is_void(lp))
3885 /*EMPTY*/;
3886 else if (lkind == CTF_K_POINTER && dt_node_is_pointer(rp))
3887 /*EMPTY*/;
3888 else if (dt_node_is_string(lp) && (dt_node_is_scalar(rp) ||
3889 dt_node_is_pointer(rp) || dt_node_is_strcompat(rp)))
3890 /*EMPTY*/;
3891 else {
3892 xyerror(D_CAST_INVAL,
3893 "invalid cast expression: \"%s\" to \"%s\"\n",
3894 dt_node_type_name(rp, n1, sizeof (n1)),
3895 dt_node_type_name(lp, n2, sizeof (n2)));
3896 }
3897
3898 dt_node_type_propagate(lp, dnp); /* see K&R[A7.5] */
3899 dt_node_attr_assign(dnp, dt_attr_min(lp->dn_attr, rp->dn_attr));
3900
3901 /*
3902 * If it's a pointer then should be able to (attempt to)
3903 * assign to it.
3904 */
3905 if (lkind == CTF_K_POINTER)
3906 dnp->dn_flags |= DT_NF_WRITABLE;
3907
3908 break;
3909 }
3910
3911 case DT_TOK_COMMA:
3912 lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF);
3913 rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
3914
3915 if (dt_node_is_dynamic(lp) || dt_node_is_dynamic(rp)) {
3916 xyerror(D_OP_DYN, "operator %s operands "
3917 "cannot be of dynamic type\n", opstr(op));
3918 }
3919
3920 if (dt_node_is_actfunc(lp) || dt_node_is_actfunc(rp)) {
3921 xyerror(D_OP_ACT, "operator %s operands "
3922 "cannot be actions\n", opstr(op));
3923 }
3924
3925 dt_node_type_propagate(rp, dnp); /* see K&R[A7.18] */
3926 dt_node_attr_assign(dnp, dt_attr_min(lp->dn_attr, rp->dn_attr));
3927 break;
3928
3929 default:
3930 xyerror(D_UNKNOWN, "invalid binary op %s\n", opstr(op));
3931 }
3932
3933 /*
3934 * Complete the conversion of E1[E2] to *((E1)+(E2)) that we started
3935 * at the top of our switch() above (see K&R[A7.3.1]). Since E2 is
3936 * parsed as an argument_expression_list by dt_grammar.y, we can
3937 * end up with a comma-separated list inside of a non-associative
3938 * array reference. We check for this and report an appropriate error.
3939 */
3940 if (dnp->dn_op == DT_TOK_LBRAC && op == DT_TOK_ADD) {
3941 dt_node_t *pnp;
3942
3943 if (rp->dn_list != NULL) {
3944 xyerror(D_ARR_BADREF,
3945 "cannot access %s as an associative array\n",
3946 dt_node_name(lp, n1, sizeof (n1)));
3947 }
3948
3949 dnp->dn_op = DT_TOK_ADD;
3950 pnp = dt_node_op1(DT_TOK_DEREF, dnp);
3951
3952 /*
3953 * Cook callbacks are not typically permitted to allocate nodes.
3954 * When we do, we must insert them in the middle of an existing
3955 * allocation list rather than having them appended to the pcb
3956 * list because the sub-expression may be part of a definition.
3957 */
3958 assert(yypcb->pcb_list == pnp);
3959 yypcb->pcb_list = pnp->dn_link;
3960
3961 pnp->dn_link = dnp->dn_link;
3962 dnp->dn_link = pnp;
3963
3964 return (dt_node_cook(pnp, DT_IDFLG_REF));
3965 }
3966
3967 return (dnp);
3968}
3969
3970/*ARGSUSED*/
3971static dt_node_t *
3972dt_cook_op3(dt_node_t *dnp, uint_t idflags)
3973{
3974 dt_node_t *lp, *rp;
3975 ctf_file_t *ctfp;
3976 ctf_id_t type;
3977
3978 dnp->dn_expr = dt_node_cook(dnp->dn_expr, DT_IDFLG_REF);
3979 lp = dnp->dn_left = dt_node_cook(dnp->dn_left, DT_IDFLG_REF);
3980 rp = dnp->dn_right = dt_node_cook(dnp->dn_right, DT_IDFLG_REF);
3981
3982 if (!dt_node_is_scalar(dnp->dn_expr)) {
3983 xyerror(D_OP_SCALAR,
3984 "operator ?: expression must be of scalar type\n");
3985 }
3986
3987 if (dt_node_is_dynamic(lp) || dt_node_is_dynamic(rp)) {
3988 xyerror(D_OP_DYN,
3989 "operator ?: operands cannot be of dynamic type\n");
3990 }
3991
3992 /*
3993 * The rules for type checking for the ternary operator are complex and
3994 * are described in the ANSI-C spec (see K&R[A7.16]). We implement
3995 * the various tests in order from least to most expensive.
3996 */
3997 if (ctf_type_compat(lp->dn_ctfp, lp->dn_type,
3998 rp->dn_ctfp, rp->dn_type)) {
3999 ctfp = lp->dn_ctfp;
4000 type = lp->dn_type;
4001 } else if (dt_node_is_integer(lp) && dt_node_is_integer(rp)) {
4002 dt_type_promote(lp, rp, &ctfp, &type);
4003 } else if (dt_node_is_strcompat(lp) && dt_node_is_strcompat(rp) &&
4004 (dt_node_is_string(lp) || dt_node_is_string(rp))) {
4005 ctfp = DT_STR_CTFP(yypcb->pcb_hdl);
4006 type = DT_STR_TYPE(yypcb->pcb_hdl);
4007 } else if (dt_node_is_ptrcompat(lp, rp, &ctfp, &type) == 0) {
4008 xyerror(D_OP_INCOMPAT,
4009 "operator ?: operands must have compatible types\n");
4010 }
4011
4012 if (dt_node_is_actfunc(lp) || dt_node_is_actfunc(rp)) {
4013 xyerror(D_OP_ACT, "action cannot be "
4014 "used in a conditional context\n");
4015 }
4016
3902 dt_node_attr_assign(dnp,
3903 dt_attr_min(rp->dn_attr, dnp->dn_ident->di_attr));
3904 break;
3905 }
3906
3907 case DT_TOK_LPAR: {
3908 ctf_id_t ltype, rtype;
3909 uint_t lkind, rkind;
3910
3911 assert(lp->dn_kind == DT_NODE_TYPE);
3912 rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
3913
3914 ltype = ctf_type_resolve(lp->dn_ctfp, lp->dn_type);
3915 lkind = ctf_type_kind(lp->dn_ctfp, ltype);
3916
3917 rtype = ctf_type_resolve(rp->dn_ctfp, rp->dn_type);
3918 rkind = ctf_type_kind(rp->dn_ctfp, rtype);
3919
3920 /*
3921 * The rules for casting are loosely explained in K&R[A7.5]
3922 * and K&R[A6]. Basically, we can cast to the same type or
3923 * same base type, between any kind of scalar values, from
3924 * arrays to pointers, and we can cast anything to void.
3925 * To these rules D adds casts from scalars to strings.
3926 */
3927 if (ctf_type_compat(lp->dn_ctfp, lp->dn_type,
3928 rp->dn_ctfp, rp->dn_type))
3929 /*EMPTY*/;
3930 else if (dt_node_is_scalar(lp) &&
3931 (dt_node_is_scalar(rp) || rkind == CTF_K_FUNCTION))
3932 /*EMPTY*/;
3933 else if (dt_node_is_void(lp))
3934 /*EMPTY*/;
3935 else if (lkind == CTF_K_POINTER && dt_node_is_pointer(rp))
3936 /*EMPTY*/;
3937 else if (dt_node_is_string(lp) && (dt_node_is_scalar(rp) ||
3938 dt_node_is_pointer(rp) || dt_node_is_strcompat(rp)))
3939 /*EMPTY*/;
3940 else {
3941 xyerror(D_CAST_INVAL,
3942 "invalid cast expression: \"%s\" to \"%s\"\n",
3943 dt_node_type_name(rp, n1, sizeof (n1)),
3944 dt_node_type_name(lp, n2, sizeof (n2)));
3945 }
3946
3947 dt_node_type_propagate(lp, dnp); /* see K&R[A7.5] */
3948 dt_node_attr_assign(dnp, dt_attr_min(lp->dn_attr, rp->dn_attr));
3949
3950 /*
3951 * If it's a pointer then should be able to (attempt to)
3952 * assign to it.
3953 */
3954 if (lkind == CTF_K_POINTER)
3955 dnp->dn_flags |= DT_NF_WRITABLE;
3956
3957 break;
3958 }
3959
3960 case DT_TOK_COMMA:
3961 lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF);
3962 rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
3963
3964 if (dt_node_is_dynamic(lp) || dt_node_is_dynamic(rp)) {
3965 xyerror(D_OP_DYN, "operator %s operands "
3966 "cannot be of dynamic type\n", opstr(op));
3967 }
3968
3969 if (dt_node_is_actfunc(lp) || dt_node_is_actfunc(rp)) {
3970 xyerror(D_OP_ACT, "operator %s operands "
3971 "cannot be actions\n", opstr(op));
3972 }
3973
3974 dt_node_type_propagate(rp, dnp); /* see K&R[A7.18] */
3975 dt_node_attr_assign(dnp, dt_attr_min(lp->dn_attr, rp->dn_attr));
3976 break;
3977
3978 default:
3979 xyerror(D_UNKNOWN, "invalid binary op %s\n", opstr(op));
3980 }
3981
3982 /*
3983 * Complete the conversion of E1[E2] to *((E1)+(E2)) that we started
3984 * at the top of our switch() above (see K&R[A7.3.1]). Since E2 is
3985 * parsed as an argument_expression_list by dt_grammar.y, we can
3986 * end up with a comma-separated list inside of a non-associative
3987 * array reference. We check for this and report an appropriate error.
3988 */
3989 if (dnp->dn_op == DT_TOK_LBRAC && op == DT_TOK_ADD) {
3990 dt_node_t *pnp;
3991
3992 if (rp->dn_list != NULL) {
3993 xyerror(D_ARR_BADREF,
3994 "cannot access %s as an associative array\n",
3995 dt_node_name(lp, n1, sizeof (n1)));
3996 }
3997
3998 dnp->dn_op = DT_TOK_ADD;
3999 pnp = dt_node_op1(DT_TOK_DEREF, dnp);
4000
4001 /*
4002 * Cook callbacks are not typically permitted to allocate nodes.
4003 * When we do, we must insert them in the middle of an existing
4004 * allocation list rather than having them appended to the pcb
4005 * list because the sub-expression may be part of a definition.
4006 */
4007 assert(yypcb->pcb_list == pnp);
4008 yypcb->pcb_list = pnp->dn_link;
4009
4010 pnp->dn_link = dnp->dn_link;
4011 dnp->dn_link = pnp;
4012
4013 return (dt_node_cook(pnp, DT_IDFLG_REF));
4014 }
4015
4016 return (dnp);
4017}
4018
4019/*ARGSUSED*/
4020static dt_node_t *
4021dt_cook_op3(dt_node_t *dnp, uint_t idflags)
4022{
4023 dt_node_t *lp, *rp;
4024 ctf_file_t *ctfp;
4025 ctf_id_t type;
4026
4027 dnp->dn_expr = dt_node_cook(dnp->dn_expr, DT_IDFLG_REF);
4028 lp = dnp->dn_left = dt_node_cook(dnp->dn_left, DT_IDFLG_REF);
4029 rp = dnp->dn_right = dt_node_cook(dnp->dn_right, DT_IDFLG_REF);
4030
4031 if (!dt_node_is_scalar(dnp->dn_expr)) {
4032 xyerror(D_OP_SCALAR,
4033 "operator ?: expression must be of scalar type\n");
4034 }
4035
4036 if (dt_node_is_dynamic(lp) || dt_node_is_dynamic(rp)) {
4037 xyerror(D_OP_DYN,
4038 "operator ?: operands cannot be of dynamic type\n");
4039 }
4040
4041 /*
4042 * The rules for type checking for the ternary operator are complex and
4043 * are described in the ANSI-C spec (see K&R[A7.16]). We implement
4044 * the various tests in order from least to most expensive.
4045 */
4046 if (ctf_type_compat(lp->dn_ctfp, lp->dn_type,
4047 rp->dn_ctfp, rp->dn_type)) {
4048 ctfp = lp->dn_ctfp;
4049 type = lp->dn_type;
4050 } else if (dt_node_is_integer(lp) && dt_node_is_integer(rp)) {
4051 dt_type_promote(lp, rp, &ctfp, &type);
4052 } else if (dt_node_is_strcompat(lp) && dt_node_is_strcompat(rp) &&
4053 (dt_node_is_string(lp) || dt_node_is_string(rp))) {
4054 ctfp = DT_STR_CTFP(yypcb->pcb_hdl);
4055 type = DT_STR_TYPE(yypcb->pcb_hdl);
4056 } else if (dt_node_is_ptrcompat(lp, rp, &ctfp, &type) == 0) {
4057 xyerror(D_OP_INCOMPAT,
4058 "operator ?: operands must have compatible types\n");
4059 }
4060
4061 if (dt_node_is_actfunc(lp) || dt_node_is_actfunc(rp)) {
4062 xyerror(D_OP_ACT, "action cannot be "
4063 "used in a conditional context\n");
4064 }
4065
4017 dt_node_type_assign(dnp, ctfp, type);
4066 dt_node_type_assign(dnp, ctfp, type, B_FALSE);
4018 dt_node_attr_assign(dnp, dt_attr_min(dnp->dn_expr->dn_attr,
4019 dt_attr_min(lp->dn_attr, rp->dn_attr)));
4020
4021 return (dnp);
4022}
4023
4024static dt_node_t *
4025dt_cook_statement(dt_node_t *dnp, uint_t idflags)
4026{
4027 dnp->dn_expr = dt_node_cook(dnp->dn_expr, idflags);
4028 dt_node_attr_assign(dnp, dnp->dn_expr->dn_attr);
4029
4030 return (dnp);
4031}
4032
4033/*
4034 * If dn_aggfun is set, this node is a collapsed aggregation assignment (see
4035 * the special case code for DT_TOK_ASGN in dt_cook_op2() above), in which
4036 * case we cook both the tuple and the function call. If dn_aggfun is NULL,
4037 * this node is just a reference to the aggregation's type and attributes.
4038 */
4039/*ARGSUSED*/
4040static dt_node_t *
4041dt_cook_aggregation(dt_node_t *dnp, uint_t idflags)
4042{
4043 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
4044
4045 if (dnp->dn_aggfun != NULL) {
4046 dnp->dn_aggfun = dt_node_cook(dnp->dn_aggfun, DT_IDFLG_REF);
4047 dt_node_attr_assign(dnp, dt_ident_cook(dnp,
4048 dnp->dn_ident, &dnp->dn_aggtup));
4049 } else {
4067 dt_node_attr_assign(dnp, dt_attr_min(dnp->dn_expr->dn_attr,
4068 dt_attr_min(lp->dn_attr, rp->dn_attr)));
4069
4070 return (dnp);
4071}
4072
4073static dt_node_t *
4074dt_cook_statement(dt_node_t *dnp, uint_t idflags)
4075{
4076 dnp->dn_expr = dt_node_cook(dnp->dn_expr, idflags);
4077 dt_node_attr_assign(dnp, dnp->dn_expr->dn_attr);
4078
4079 return (dnp);
4080}
4081
4082/*
4083 * If dn_aggfun is set, this node is a collapsed aggregation assignment (see
4084 * the special case code for DT_TOK_ASGN in dt_cook_op2() above), in which
4085 * case we cook both the tuple and the function call. If dn_aggfun is NULL,
4086 * this node is just a reference to the aggregation's type and attributes.
4087 */
4088/*ARGSUSED*/
4089static dt_node_t *
4090dt_cook_aggregation(dt_node_t *dnp, uint_t idflags)
4091{
4092 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
4093
4094 if (dnp->dn_aggfun != NULL) {
4095 dnp->dn_aggfun = dt_node_cook(dnp->dn_aggfun, DT_IDFLG_REF);
4096 dt_node_attr_assign(dnp, dt_ident_cook(dnp,
4097 dnp->dn_ident, &dnp->dn_aggtup));
4098 } else {
4050 dt_node_type_assign(dnp, DT_DYN_CTFP(dtp), DT_DYN_TYPE(dtp));
4099 dt_node_type_assign(dnp, DT_DYN_CTFP(dtp), DT_DYN_TYPE(dtp),
4100 B_FALSE);
4051 dt_node_attr_assign(dnp, dnp->dn_ident->di_attr);
4052 }
4053
4054 return (dnp);
4055}
4056
4057/*
4058 * Since D permits new variable identifiers to be instantiated in any program
4059 * expression, we may need to cook a clause's predicate either before or after
4060 * the action list depending on the program code in question. Consider:
4061 *
4062 * probe-description-list probe-description-list
4063 * /x++/ /x == 0/
4064 * { {
4065 * trace(x); trace(x++);
4066 * } }
4067 *
4068 * In the left-hand example, the predicate uses operator ++ to instantiate 'x'
4069 * as a variable of type int64_t. The predicate must be cooked first because
4070 * otherwise the statement trace(x) refers to an unknown identifier. In the
4071 * right-hand example, the action list uses ++ to instantiate 'x'; the action
4072 * list must be cooked first because otherwise the predicate x == 0 refers to
4073 * an unknown identifier. In order to simplify programming, we support both.
4074 *
4075 * When cooking a clause, we cook the action statements before the predicate by
4076 * default, since it seems more common to create or modify identifiers in the
4077 * action list. If cooking fails due to an unknown identifier, we attempt to
4078 * cook the predicate (i.e. do it first) and then go back and cook the actions.
4079 * If this, too, fails (or if we get an error other than D_IDENT_UNDEF) we give
4080 * up and report failure back to the user. There are five possible paths:
4081 *
4082 * cook actions = OK, cook predicate = OK -> OK
4083 * cook actions = OK, cook predicate = ERR -> ERR
4084 * cook actions = ERR, cook predicate = ERR -> ERR
4085 * cook actions = ERR, cook predicate = OK, cook actions = OK -> OK
4086 * cook actions = ERR, cook predicate = OK, cook actions = ERR -> ERR
4087 *
4088 * The programmer can still defeat our scheme by creating circular definition
4089 * dependencies between predicates and actions, as in this example clause:
4090 *
4091 * probe-description-list
4092 * /x++ && y == 0/
4093 * {
4094 * trace(x + y++);
4095 * }
4096 *
4097 * but it doesn't seem worth the complexity to handle such rare cases. The
4098 * user can simply use the D variable declaration syntax to work around them.
4099 */
4100static dt_node_t *
4101dt_cook_clause(dt_node_t *dnp, uint_t idflags)
4102{
4103 volatile int err, tries;
4104 jmp_buf ojb;
4105
4106 /*
4107 * Before assigning dn_ctxattr, temporarily assign the probe attribute
4108 * to 'dnp' itself to force an attribute check and minimum violation.
4109 */
4110 dt_node_attr_assign(dnp, yypcb->pcb_pinfo.dtp_attr);
4111 dnp->dn_ctxattr = yypcb->pcb_pinfo.dtp_attr;
4112
4113 bcopy(yypcb->pcb_jmpbuf, ojb, sizeof (jmp_buf));
4114 tries = 0;
4115
4116 if (dnp->dn_pred != NULL && (err = setjmp(yypcb->pcb_jmpbuf)) != 0) {
4117 bcopy(ojb, yypcb->pcb_jmpbuf, sizeof (jmp_buf));
4118 if (tries++ != 0 || err != EDT_COMPILER || (
4119 yypcb->pcb_hdl->dt_errtag != dt_errtag(D_IDENT_UNDEF) &&
4120 yypcb->pcb_hdl->dt_errtag != dt_errtag(D_VAR_UNDEF)))
4121 longjmp(yypcb->pcb_jmpbuf, err);
4122 }
4123
4124 if (tries == 0) {
4125 yylabel("action list");
4126
4127 dt_node_attr_assign(dnp,
4128 dt_node_list_cook(&dnp->dn_acts, idflags));
4129
4130 bcopy(ojb, yypcb->pcb_jmpbuf, sizeof (jmp_buf));
4131 yylabel(NULL);
4132 }
4133
4134 if (dnp->dn_pred != NULL) {
4135 yylabel("predicate");
4136
4137 dnp->dn_pred = dt_node_cook(dnp->dn_pred, idflags);
4138 dt_node_attr_assign(dnp,
4139 dt_attr_min(dnp->dn_attr, dnp->dn_pred->dn_attr));
4140
4141 if (!dt_node_is_scalar(dnp->dn_pred)) {
4142 xyerror(D_PRED_SCALAR,
4143 "predicate result must be of scalar type\n");
4144 }
4145
4146 yylabel(NULL);
4147 }
4148
4149 if (tries != 0) {
4150 yylabel("action list");
4151
4152 dt_node_attr_assign(dnp,
4153 dt_node_list_cook(&dnp->dn_acts, idflags));
4154
4155 yylabel(NULL);
4156 }
4157
4158 return (dnp);
4159}
4160
4161/*ARGSUSED*/
4162static dt_node_t *
4163dt_cook_inline(dt_node_t *dnp, uint_t idflags)
4164{
4165 dt_idnode_t *inp = dnp->dn_ident->di_iarg;
4166 dt_ident_t *rdp;
4167
4168 char n1[DT_TYPE_NAMELEN];
4169 char n2[DT_TYPE_NAMELEN];
4170
4171 assert(dnp->dn_ident->di_flags & DT_IDFLG_INLINE);
4172 assert(inp->din_root->dn_flags & DT_NF_COOKED);
4173
4174 /*
4175 * If we are inlining a translation, verify that the inline declaration
4176 * type exactly matches the type that is returned by the translation.
4177 * Otherwise just use dt_node_is_argcompat() to check the types.
4178 */
4179 if ((rdp = dt_node_resolve(inp->din_root, DT_IDENT_XLSOU)) != NULL ||
4180 (rdp = dt_node_resolve(inp->din_root, DT_IDENT_XLPTR)) != NULL) {
4181
4182 ctf_file_t *lctfp = dnp->dn_ctfp;
4183 ctf_id_t ltype = ctf_type_resolve(lctfp, dnp->dn_type);
4184
4185 dt_xlator_t *dxp = rdp->di_data;
4186 ctf_file_t *rctfp = dxp->dx_dst_ctfp;
4187 ctf_id_t rtype = dxp->dx_dst_base;
4188
4189 if (ctf_type_kind(lctfp, ltype) == CTF_K_POINTER) {
4190 ltype = ctf_type_reference(lctfp, ltype);
4191 ltype = ctf_type_resolve(lctfp, ltype);
4192 }
4193
4194 if (ctf_type_compat(lctfp, ltype, rctfp, rtype) == 0) {
4195 dnerror(dnp, D_OP_INCOMPAT,
4196 "inline %s definition uses incompatible types: "
4197 "\"%s\" = \"%s\"\n", dnp->dn_ident->di_name,
4198 dt_type_name(lctfp, ltype, n1, sizeof (n1)),
4199 dt_type_name(rctfp, rtype, n2, sizeof (n2)));
4200 }
4201
4202 } else if (dt_node_is_argcompat(dnp, inp->din_root) == 0) {
4203 dnerror(dnp, D_OP_INCOMPAT,
4204 "inline %s definition uses incompatible types: "
4205 "\"%s\" = \"%s\"\n", dnp->dn_ident->di_name,
4206 dt_node_type_name(dnp, n1, sizeof (n1)),
4207 dt_node_type_name(inp->din_root, n2, sizeof (n2)));
4208 }
4209
4210 return (dnp);
4211}
4212
4213static dt_node_t *
4214dt_cook_member(dt_node_t *dnp, uint_t idflags)
4215{
4216 dnp->dn_membexpr = dt_node_cook(dnp->dn_membexpr, idflags);
4217 dt_node_attr_assign(dnp, dnp->dn_membexpr->dn_attr);
4218 return (dnp);
4219}
4220
4221/*ARGSUSED*/
4222static dt_node_t *
4223dt_cook_xlator(dt_node_t *dnp, uint_t idflags)
4224{
4225 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
4226 dt_xlator_t *dxp = dnp->dn_xlator;
4227 dt_node_t *mnp;
4228
4229 char n1[DT_TYPE_NAMELEN];
4230 char n2[DT_TYPE_NAMELEN];
4231
4232 dtrace_attribute_t attr = _dtrace_maxattr;
4233 ctf_membinfo_t ctm;
4234
4235 /*
4236 * Before cooking each translator member, we push a reference to the
4237 * hash containing translator-local identifiers on to pcb_globals to
4238 * temporarily interpose these identifiers in front of other globals.
4239 */
4240 dt_idstack_push(&yypcb->pcb_globals, dxp->dx_locals);
4241
4242 for (mnp = dnp->dn_members; mnp != NULL; mnp = mnp->dn_list) {
4243 if (ctf_member_info(dxp->dx_dst_ctfp, dxp->dx_dst_type,
4244 mnp->dn_membname, &ctm) == CTF_ERR) {
4245 xyerror(D_XLATE_MEMB,
4246 "translator member %s is not a member of %s\n",
4247 mnp->dn_membname, ctf_type_name(dxp->dx_dst_ctfp,
4248 dxp->dx_dst_type, n1, sizeof (n1)));
4249 }
4250
4251 (void) dt_node_cook(mnp, DT_IDFLG_REF);
4101 dt_node_attr_assign(dnp, dnp->dn_ident->di_attr);
4102 }
4103
4104 return (dnp);
4105}
4106
4107/*
4108 * Since D permits new variable identifiers to be instantiated in any program
4109 * expression, we may need to cook a clause's predicate either before or after
4110 * the action list depending on the program code in question. Consider:
4111 *
4112 * probe-description-list probe-description-list
4113 * /x++/ /x == 0/
4114 * { {
4115 * trace(x); trace(x++);
4116 * } }
4117 *
4118 * In the left-hand example, the predicate uses operator ++ to instantiate 'x'
4119 * as a variable of type int64_t. The predicate must be cooked first because
4120 * otherwise the statement trace(x) refers to an unknown identifier. In the
4121 * right-hand example, the action list uses ++ to instantiate 'x'; the action
4122 * list must be cooked first because otherwise the predicate x == 0 refers to
4123 * an unknown identifier. In order to simplify programming, we support both.
4124 *
4125 * When cooking a clause, we cook the action statements before the predicate by
4126 * default, since it seems more common to create or modify identifiers in the
4127 * action list. If cooking fails due to an unknown identifier, we attempt to
4128 * cook the predicate (i.e. do it first) and then go back and cook the actions.
4129 * If this, too, fails (or if we get an error other than D_IDENT_UNDEF) we give
4130 * up and report failure back to the user. There are five possible paths:
4131 *
4132 * cook actions = OK, cook predicate = OK -> OK
4133 * cook actions = OK, cook predicate = ERR -> ERR
4134 * cook actions = ERR, cook predicate = ERR -> ERR
4135 * cook actions = ERR, cook predicate = OK, cook actions = OK -> OK
4136 * cook actions = ERR, cook predicate = OK, cook actions = ERR -> ERR
4137 *
4138 * The programmer can still defeat our scheme by creating circular definition
4139 * dependencies between predicates and actions, as in this example clause:
4140 *
4141 * probe-description-list
4142 * /x++ && y == 0/
4143 * {
4144 * trace(x + y++);
4145 * }
4146 *
4147 * but it doesn't seem worth the complexity to handle such rare cases. The
4148 * user can simply use the D variable declaration syntax to work around them.
4149 */
4150static dt_node_t *
4151dt_cook_clause(dt_node_t *dnp, uint_t idflags)
4152{
4153 volatile int err, tries;
4154 jmp_buf ojb;
4155
4156 /*
4157 * Before assigning dn_ctxattr, temporarily assign the probe attribute
4158 * to 'dnp' itself to force an attribute check and minimum violation.
4159 */
4160 dt_node_attr_assign(dnp, yypcb->pcb_pinfo.dtp_attr);
4161 dnp->dn_ctxattr = yypcb->pcb_pinfo.dtp_attr;
4162
4163 bcopy(yypcb->pcb_jmpbuf, ojb, sizeof (jmp_buf));
4164 tries = 0;
4165
4166 if (dnp->dn_pred != NULL && (err = setjmp(yypcb->pcb_jmpbuf)) != 0) {
4167 bcopy(ojb, yypcb->pcb_jmpbuf, sizeof (jmp_buf));
4168 if (tries++ != 0 || err != EDT_COMPILER || (
4169 yypcb->pcb_hdl->dt_errtag != dt_errtag(D_IDENT_UNDEF) &&
4170 yypcb->pcb_hdl->dt_errtag != dt_errtag(D_VAR_UNDEF)))
4171 longjmp(yypcb->pcb_jmpbuf, err);
4172 }
4173
4174 if (tries == 0) {
4175 yylabel("action list");
4176
4177 dt_node_attr_assign(dnp,
4178 dt_node_list_cook(&dnp->dn_acts, idflags));
4179
4180 bcopy(ojb, yypcb->pcb_jmpbuf, sizeof (jmp_buf));
4181 yylabel(NULL);
4182 }
4183
4184 if (dnp->dn_pred != NULL) {
4185 yylabel("predicate");
4186
4187 dnp->dn_pred = dt_node_cook(dnp->dn_pred, idflags);
4188 dt_node_attr_assign(dnp,
4189 dt_attr_min(dnp->dn_attr, dnp->dn_pred->dn_attr));
4190
4191 if (!dt_node_is_scalar(dnp->dn_pred)) {
4192 xyerror(D_PRED_SCALAR,
4193 "predicate result must be of scalar type\n");
4194 }
4195
4196 yylabel(NULL);
4197 }
4198
4199 if (tries != 0) {
4200 yylabel("action list");
4201
4202 dt_node_attr_assign(dnp,
4203 dt_node_list_cook(&dnp->dn_acts, idflags));
4204
4205 yylabel(NULL);
4206 }
4207
4208 return (dnp);
4209}
4210
4211/*ARGSUSED*/
4212static dt_node_t *
4213dt_cook_inline(dt_node_t *dnp, uint_t idflags)
4214{
4215 dt_idnode_t *inp = dnp->dn_ident->di_iarg;
4216 dt_ident_t *rdp;
4217
4218 char n1[DT_TYPE_NAMELEN];
4219 char n2[DT_TYPE_NAMELEN];
4220
4221 assert(dnp->dn_ident->di_flags & DT_IDFLG_INLINE);
4222 assert(inp->din_root->dn_flags & DT_NF_COOKED);
4223
4224 /*
4225 * If we are inlining a translation, verify that the inline declaration
4226 * type exactly matches the type that is returned by the translation.
4227 * Otherwise just use dt_node_is_argcompat() to check the types.
4228 */
4229 if ((rdp = dt_node_resolve(inp->din_root, DT_IDENT_XLSOU)) != NULL ||
4230 (rdp = dt_node_resolve(inp->din_root, DT_IDENT_XLPTR)) != NULL) {
4231
4232 ctf_file_t *lctfp = dnp->dn_ctfp;
4233 ctf_id_t ltype = ctf_type_resolve(lctfp, dnp->dn_type);
4234
4235 dt_xlator_t *dxp = rdp->di_data;
4236 ctf_file_t *rctfp = dxp->dx_dst_ctfp;
4237 ctf_id_t rtype = dxp->dx_dst_base;
4238
4239 if (ctf_type_kind(lctfp, ltype) == CTF_K_POINTER) {
4240 ltype = ctf_type_reference(lctfp, ltype);
4241 ltype = ctf_type_resolve(lctfp, ltype);
4242 }
4243
4244 if (ctf_type_compat(lctfp, ltype, rctfp, rtype) == 0) {
4245 dnerror(dnp, D_OP_INCOMPAT,
4246 "inline %s definition uses incompatible types: "
4247 "\"%s\" = \"%s\"\n", dnp->dn_ident->di_name,
4248 dt_type_name(lctfp, ltype, n1, sizeof (n1)),
4249 dt_type_name(rctfp, rtype, n2, sizeof (n2)));
4250 }
4251
4252 } else if (dt_node_is_argcompat(dnp, inp->din_root) == 0) {
4253 dnerror(dnp, D_OP_INCOMPAT,
4254 "inline %s definition uses incompatible types: "
4255 "\"%s\" = \"%s\"\n", dnp->dn_ident->di_name,
4256 dt_node_type_name(dnp, n1, sizeof (n1)),
4257 dt_node_type_name(inp->din_root, n2, sizeof (n2)));
4258 }
4259
4260 return (dnp);
4261}
4262
4263static dt_node_t *
4264dt_cook_member(dt_node_t *dnp, uint_t idflags)
4265{
4266 dnp->dn_membexpr = dt_node_cook(dnp->dn_membexpr, idflags);
4267 dt_node_attr_assign(dnp, dnp->dn_membexpr->dn_attr);
4268 return (dnp);
4269}
4270
4271/*ARGSUSED*/
4272static dt_node_t *
4273dt_cook_xlator(dt_node_t *dnp, uint_t idflags)
4274{
4275 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
4276 dt_xlator_t *dxp = dnp->dn_xlator;
4277 dt_node_t *mnp;
4278
4279 char n1[DT_TYPE_NAMELEN];
4280 char n2[DT_TYPE_NAMELEN];
4281
4282 dtrace_attribute_t attr = _dtrace_maxattr;
4283 ctf_membinfo_t ctm;
4284
4285 /*
4286 * Before cooking each translator member, we push a reference to the
4287 * hash containing translator-local identifiers on to pcb_globals to
4288 * temporarily interpose these identifiers in front of other globals.
4289 */
4290 dt_idstack_push(&yypcb->pcb_globals, dxp->dx_locals);
4291
4292 for (mnp = dnp->dn_members; mnp != NULL; mnp = mnp->dn_list) {
4293 if (ctf_member_info(dxp->dx_dst_ctfp, dxp->dx_dst_type,
4294 mnp->dn_membname, &ctm) == CTF_ERR) {
4295 xyerror(D_XLATE_MEMB,
4296 "translator member %s is not a member of %s\n",
4297 mnp->dn_membname, ctf_type_name(dxp->dx_dst_ctfp,
4298 dxp->dx_dst_type, n1, sizeof (n1)));
4299 }
4300
4301 (void) dt_node_cook(mnp, DT_IDFLG_REF);
4252 dt_node_type_assign(mnp, dxp->dx_dst_ctfp, ctm.ctm_type);
4302 dt_node_type_assign(mnp, dxp->dx_dst_ctfp, ctm.ctm_type,
4303 B_FALSE);
4253 attr = dt_attr_min(attr, mnp->dn_attr);
4254
4255 if (dt_node_is_argcompat(mnp, mnp->dn_membexpr) == 0) {
4256 xyerror(D_XLATE_INCOMPAT,
4257 "translator member %s definition uses "
4258 "incompatible types: \"%s\" = \"%s\"\n",
4259 mnp->dn_membname,
4260 dt_node_type_name(mnp, n1, sizeof (n1)),
4261 dt_node_type_name(mnp->dn_membexpr,
4262 n2, sizeof (n2)));
4263 }
4264 }
4265
4266 dt_idstack_pop(&yypcb->pcb_globals, dxp->dx_locals);
4267
4268 dxp->dx_souid.di_attr = attr;
4269 dxp->dx_ptrid.di_attr = attr;
4270
4304 attr = dt_attr_min(attr, mnp->dn_attr);
4305
4306 if (dt_node_is_argcompat(mnp, mnp->dn_membexpr) == 0) {
4307 xyerror(D_XLATE_INCOMPAT,
4308 "translator member %s definition uses "
4309 "incompatible types: \"%s\" = \"%s\"\n",
4310 mnp->dn_membname,
4311 dt_node_type_name(mnp, n1, sizeof (n1)),
4312 dt_node_type_name(mnp->dn_membexpr,
4313 n2, sizeof (n2)));
4314 }
4315 }
4316
4317 dt_idstack_pop(&yypcb->pcb_globals, dxp->dx_locals);
4318
4319 dxp->dx_souid.di_attr = attr;
4320 dxp->dx_ptrid.di_attr = attr;
4321
4271 dt_node_type_assign(dnp, DT_DYN_CTFP(dtp), DT_DYN_TYPE(dtp));
4322 dt_node_type_assign(dnp, DT_DYN_CTFP(dtp), DT_DYN_TYPE(dtp), B_FALSE);
4272 dt_node_attr_assign(dnp, _dtrace_defattr);
4273
4274 return (dnp);
4275}
4276
4277static void
4278dt_node_provider_cmp_argv(dt_provider_t *pvp, dt_node_t *pnp, const char *kind,
4279 uint_t old_argc, dt_node_t *old_argv, uint_t new_argc, dt_node_t *new_argv)
4280{
4281 dt_probe_t *prp = pnp->dn_ident->di_data;
4282 uint_t i;
4283
4284 char n1[DT_TYPE_NAMELEN];
4285 char n2[DT_TYPE_NAMELEN];
4286
4287 if (old_argc != new_argc) {
4288 dnerror(pnp, D_PROV_INCOMPAT,
4289 "probe %s:%s %s prototype mismatch:\n"
4290 "\t current: %u arg%s\n\tprevious: %u arg%s\n",
4291 pvp->pv_desc.dtvd_name, prp->pr_ident->di_name, kind,
4292 new_argc, new_argc != 1 ? "s" : "",
4293 old_argc, old_argc != 1 ? "s" : "");
4294 }
4295
4296 for (i = 0; i < old_argc; i++,
4297 old_argv = old_argv->dn_list, new_argv = new_argv->dn_list) {
4298 if (ctf_type_cmp(old_argv->dn_ctfp, old_argv->dn_type,
4299 new_argv->dn_ctfp, new_argv->dn_type) == 0)
4300 continue;
4301
4302 dnerror(pnp, D_PROV_INCOMPAT,
4303 "probe %s:%s %s prototype argument #%u mismatch:\n"
4304 "\t current: %s\n\tprevious: %s\n",
4305 pvp->pv_desc.dtvd_name, prp->pr_ident->di_name, kind, i + 1,
4306 dt_node_type_name(new_argv, n1, sizeof (n1)),
4307 dt_node_type_name(old_argv, n2, sizeof (n2)));
4308 }
4309}
4310
4311/*
4312 * Compare a new probe declaration with an existing probe definition (either
4313 * from a previous declaration or cached from the kernel). If the existing
4314 * definition and declaration both have an input and output parameter list,
4315 * compare both lists. Otherwise compare only the output parameter lists.
4316 */
4317static void
4318dt_node_provider_cmp(dt_provider_t *pvp, dt_node_t *pnp,
4319 dt_probe_t *old, dt_probe_t *new)
4320{
4321 dt_node_provider_cmp_argv(pvp, pnp, "output",
4322 old->pr_xargc, old->pr_xargs, new->pr_xargc, new->pr_xargs);
4323
4324 if (old->pr_nargs != old->pr_xargs && new->pr_nargs != new->pr_xargs) {
4325 dt_node_provider_cmp_argv(pvp, pnp, "input",
4326 old->pr_nargc, old->pr_nargs, new->pr_nargc, new->pr_nargs);
4327 }
4328
4329 if (old->pr_nargs == old->pr_xargs && new->pr_nargs != new->pr_xargs) {
4330 if (pvp->pv_flags & DT_PROVIDER_IMPL) {
4331 dnerror(pnp, D_PROV_INCOMPAT,
4332 "provider interface mismatch: %s\n"
4333 "\t current: probe %s:%s has an output prototype\n"
4334 "\tprevious: probe %s:%s has no output prototype\n",
4335 pvp->pv_desc.dtvd_name, pvp->pv_desc.dtvd_name,
4336 new->pr_ident->di_name, pvp->pv_desc.dtvd_name,
4337 old->pr_ident->di_name);
4338 }
4339
4340 if (old->pr_ident->di_gen == yypcb->pcb_hdl->dt_gen)
4341 old->pr_ident->di_flags |= DT_IDFLG_ORPHAN;
4342
4343 dt_idhash_delete(pvp->pv_probes, old->pr_ident);
4344 dt_probe_declare(pvp, new);
4345 }
4346}
4347
4348static void
4349dt_cook_probe(dt_node_t *dnp, dt_provider_t *pvp)
4350{
4351 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
4352 dt_probe_t *prp = dnp->dn_ident->di_data;
4353
4354 dt_xlator_t *dxp;
4355 uint_t i;
4356
4357 char n1[DT_TYPE_NAMELEN];
4358 char n2[DT_TYPE_NAMELEN];
4359
4360 if (prp->pr_nargs == prp->pr_xargs)
4361 return;
4362
4363 for (i = 0; i < prp->pr_xargc; i++) {
4364 dt_node_t *xnp = prp->pr_xargv[i];
4365 dt_node_t *nnp = prp->pr_nargv[prp->pr_mapping[i]];
4366
4367 if ((dxp = dt_xlator_lookup(dtp,
4368 nnp, xnp, DT_XLATE_FUZZY)) != NULL) {
4369 if (dt_provider_xref(dtp, pvp, dxp->dx_id) != 0)
4370 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
4371 continue;
4372 }
4373
4374 if (dt_node_is_argcompat(nnp, xnp))
4375 continue; /* no translator defined and none required */
4376
4377 dnerror(dnp, D_PROV_PRXLATOR, "translator for %s:%s output "
4378 "argument #%u from %s to %s is not defined\n",
4379 pvp->pv_desc.dtvd_name, dnp->dn_ident->di_name, i + 1,
4380 dt_node_type_name(nnp, n1, sizeof (n1)),
4381 dt_node_type_name(xnp, n2, sizeof (n2)));
4382 }
4383}
4384
4385/*ARGSUSED*/
4386static dt_node_t *
4387dt_cook_provider(dt_node_t *dnp, uint_t idflags)
4388{
4389 dt_provider_t *pvp = dnp->dn_provider;
4390 dt_node_t *pnp;
4391
4392 /*
4393 * If we're declaring a provider for the first time and it is unknown
4394 * to dtrace(7D), insert the probe definitions into the provider's hash.
4395 * If we're redeclaring a known provider, verify the interface matches.
4396 */
4397 for (pnp = dnp->dn_probes; pnp != NULL; pnp = pnp->dn_list) {
4398 const char *probename = pnp->dn_ident->di_name;
4399 dt_probe_t *prp = dt_probe_lookup(pvp, probename);
4400
4401 assert(pnp->dn_kind == DT_NODE_PROBE);
4402
4403 if (prp != NULL && dnp->dn_provred) {
4404 dt_node_provider_cmp(pvp, pnp,
4405 prp, pnp->dn_ident->di_data);
4406 } else if (prp == NULL && dnp->dn_provred) {
4407 dnerror(pnp, D_PROV_INCOMPAT,
4408 "provider interface mismatch: %s\n"
4409 "\t current: probe %s:%s defined\n"
4410 "\tprevious: probe %s:%s not defined\n",
4411 dnp->dn_provname, dnp->dn_provname,
4412 probename, dnp->dn_provname, probename);
4413 } else if (prp != NULL) {
4414 dnerror(pnp, D_PROV_PRDUP, "probe redeclared: %s:%s\n",
4415 dnp->dn_provname, probename);
4416 } else
4417 dt_probe_declare(pvp, pnp->dn_ident->di_data);
4418
4419 dt_cook_probe(pnp, pvp);
4420 }
4421
4422 return (dnp);
4423}
4424
4425/*ARGSUSED*/
4426static dt_node_t *
4427dt_cook_none(dt_node_t *dnp, uint_t idflags)
4428{
4429 return (dnp);
4430}
4431
4432static dt_node_t *(*dt_cook_funcs[])(dt_node_t *, uint_t) = {
4433 dt_cook_none, /* DT_NODE_FREE */
4434 dt_cook_none, /* DT_NODE_INT */
4435 dt_cook_none, /* DT_NODE_STRING */
4436 dt_cook_ident, /* DT_NODE_IDENT */
4437 dt_cook_var, /* DT_NODE_VAR */
4438 dt_cook_none, /* DT_NODE_SYM */
4439 dt_cook_none, /* DT_NODE_TYPE */
4440 dt_cook_func, /* DT_NODE_FUNC */
4441 dt_cook_op1, /* DT_NODE_OP1 */
4442 dt_cook_op2, /* DT_NODE_OP2 */
4443 dt_cook_op3, /* DT_NODE_OP3 */
4444 dt_cook_statement, /* DT_NODE_DEXPR */
4445 dt_cook_statement, /* DT_NODE_DFUNC */
4446 dt_cook_aggregation, /* DT_NODE_AGG */
4447 dt_cook_none, /* DT_NODE_PDESC */
4448 dt_cook_clause, /* DT_NODE_CLAUSE */
4449 dt_cook_inline, /* DT_NODE_INLINE */
4450 dt_cook_member, /* DT_NODE_MEMBER */
4451 dt_cook_xlator, /* DT_NODE_XLATOR */
4452 dt_cook_none, /* DT_NODE_PROBE */
4453 dt_cook_provider, /* DT_NODE_PROVIDER */
4454 dt_cook_none /* DT_NODE_PROG */
4455};
4456
4457/*
4458 * Recursively cook the parse tree starting at the specified node. The idflags
4459 * parameter is used to indicate the type of reference (r/w) and is applied to
4460 * the resulting identifier if it is a D variable or D aggregation.
4461 */
4462dt_node_t *
4463dt_node_cook(dt_node_t *dnp, uint_t idflags)
4464{
4465 int oldlineno = yylineno;
4466
4467 yylineno = dnp->dn_line;
4468
4469 dnp = dt_cook_funcs[dnp->dn_kind](dnp, idflags);
4470 dnp->dn_flags |= DT_NF_COOKED;
4471
4472 if (dnp->dn_kind == DT_NODE_VAR || dnp->dn_kind == DT_NODE_AGG)
4473 dnp->dn_ident->di_flags |= idflags;
4474
4475 yylineno = oldlineno;
4476 return (dnp);
4477}
4478
4479dtrace_attribute_t
4480dt_node_list_cook(dt_node_t **pnp, uint_t idflags)
4481{
4482 dtrace_attribute_t attr = _dtrace_defattr;
4483 dt_node_t *dnp, *nnp;
4484
4485 for (dnp = (pnp != NULL ? *pnp : NULL); dnp != NULL; dnp = nnp) {
4486 nnp = dnp->dn_list;
4487 dnp = *pnp = dt_node_cook(dnp, idflags);
4488 attr = dt_attr_min(attr, dnp->dn_attr);
4489 dnp->dn_list = nnp;
4490 pnp = &dnp->dn_list;
4491 }
4492
4493 return (attr);
4494}
4495
4496void
4497dt_node_list_free(dt_node_t **pnp)
4498{
4499 dt_node_t *dnp, *nnp;
4500
4501 for (dnp = (pnp != NULL ? *pnp : NULL); dnp != NULL; dnp = nnp) {
4502 nnp = dnp->dn_list;
4503 dt_node_free(dnp);
4504 }
4505
4506 if (pnp != NULL)
4507 *pnp = NULL;
4508}
4509
4510void
4511dt_node_link_free(dt_node_t **pnp)
4512{
4513 dt_node_t *dnp, *nnp;
4514
4515 for (dnp = (pnp != NULL ? *pnp : NULL); dnp != NULL; dnp = nnp) {
4516 nnp = dnp->dn_link;
4517 dt_node_free(dnp);
4518 }
4519
4520 for (dnp = (pnp != NULL ? *pnp : NULL); dnp != NULL; dnp = nnp) {
4521 nnp = dnp->dn_link;
4522 free(dnp);
4523 }
4524
4525 if (pnp != NULL)
4526 *pnp = NULL;
4527}
4528
4529dt_node_t *
4530dt_node_link(dt_node_t *lp, dt_node_t *rp)
4531{
4532 dt_node_t *dnp;
4533
4534 if (lp == NULL)
4535 return (rp);
4536 else if (rp == NULL)
4537 return (lp);
4538
4539 for (dnp = lp; dnp->dn_list != NULL; dnp = dnp->dn_list)
4540 continue;
4541
4542 dnp->dn_list = rp;
4543 return (lp);
4544}
4545
4546/*
4547 * Compute the DOF dtrace_diftype_t representation of a node's type. This is
4548 * called from a variety of places in the library so it cannot assume yypcb
4549 * is valid: any references to handle-specific data must be made through 'dtp'.
4550 */
4551void
4552dt_node_diftype(dtrace_hdl_t *dtp, const dt_node_t *dnp, dtrace_diftype_t *tp)
4553{
4554 if (dnp->dn_ctfp == DT_STR_CTFP(dtp) &&
4555 dnp->dn_type == DT_STR_TYPE(dtp)) {
4556 tp->dtdt_kind = DIF_TYPE_STRING;
4557 tp->dtdt_ckind = CTF_K_UNKNOWN;
4558 } else {
4559 tp->dtdt_kind = DIF_TYPE_CTF;
4560 tp->dtdt_ckind = ctf_type_kind(dnp->dn_ctfp,
4561 ctf_type_resolve(dnp->dn_ctfp, dnp->dn_type));
4562 }
4563
4323 dt_node_attr_assign(dnp, _dtrace_defattr);
4324
4325 return (dnp);
4326}
4327
4328static void
4329dt_node_provider_cmp_argv(dt_provider_t *pvp, dt_node_t *pnp, const char *kind,
4330 uint_t old_argc, dt_node_t *old_argv, uint_t new_argc, dt_node_t *new_argv)
4331{
4332 dt_probe_t *prp = pnp->dn_ident->di_data;
4333 uint_t i;
4334
4335 char n1[DT_TYPE_NAMELEN];
4336 char n2[DT_TYPE_NAMELEN];
4337
4338 if (old_argc != new_argc) {
4339 dnerror(pnp, D_PROV_INCOMPAT,
4340 "probe %s:%s %s prototype mismatch:\n"
4341 "\t current: %u arg%s\n\tprevious: %u arg%s\n",
4342 pvp->pv_desc.dtvd_name, prp->pr_ident->di_name, kind,
4343 new_argc, new_argc != 1 ? "s" : "",
4344 old_argc, old_argc != 1 ? "s" : "");
4345 }
4346
4347 for (i = 0; i < old_argc; i++,
4348 old_argv = old_argv->dn_list, new_argv = new_argv->dn_list) {
4349 if (ctf_type_cmp(old_argv->dn_ctfp, old_argv->dn_type,
4350 new_argv->dn_ctfp, new_argv->dn_type) == 0)
4351 continue;
4352
4353 dnerror(pnp, D_PROV_INCOMPAT,
4354 "probe %s:%s %s prototype argument #%u mismatch:\n"
4355 "\t current: %s\n\tprevious: %s\n",
4356 pvp->pv_desc.dtvd_name, prp->pr_ident->di_name, kind, i + 1,
4357 dt_node_type_name(new_argv, n1, sizeof (n1)),
4358 dt_node_type_name(old_argv, n2, sizeof (n2)));
4359 }
4360}
4361
4362/*
4363 * Compare a new probe declaration with an existing probe definition (either
4364 * from a previous declaration or cached from the kernel). If the existing
4365 * definition and declaration both have an input and output parameter list,
4366 * compare both lists. Otherwise compare only the output parameter lists.
4367 */
4368static void
4369dt_node_provider_cmp(dt_provider_t *pvp, dt_node_t *pnp,
4370 dt_probe_t *old, dt_probe_t *new)
4371{
4372 dt_node_provider_cmp_argv(pvp, pnp, "output",
4373 old->pr_xargc, old->pr_xargs, new->pr_xargc, new->pr_xargs);
4374
4375 if (old->pr_nargs != old->pr_xargs && new->pr_nargs != new->pr_xargs) {
4376 dt_node_provider_cmp_argv(pvp, pnp, "input",
4377 old->pr_nargc, old->pr_nargs, new->pr_nargc, new->pr_nargs);
4378 }
4379
4380 if (old->pr_nargs == old->pr_xargs && new->pr_nargs != new->pr_xargs) {
4381 if (pvp->pv_flags & DT_PROVIDER_IMPL) {
4382 dnerror(pnp, D_PROV_INCOMPAT,
4383 "provider interface mismatch: %s\n"
4384 "\t current: probe %s:%s has an output prototype\n"
4385 "\tprevious: probe %s:%s has no output prototype\n",
4386 pvp->pv_desc.dtvd_name, pvp->pv_desc.dtvd_name,
4387 new->pr_ident->di_name, pvp->pv_desc.dtvd_name,
4388 old->pr_ident->di_name);
4389 }
4390
4391 if (old->pr_ident->di_gen == yypcb->pcb_hdl->dt_gen)
4392 old->pr_ident->di_flags |= DT_IDFLG_ORPHAN;
4393
4394 dt_idhash_delete(pvp->pv_probes, old->pr_ident);
4395 dt_probe_declare(pvp, new);
4396 }
4397}
4398
4399static void
4400dt_cook_probe(dt_node_t *dnp, dt_provider_t *pvp)
4401{
4402 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
4403 dt_probe_t *prp = dnp->dn_ident->di_data;
4404
4405 dt_xlator_t *dxp;
4406 uint_t i;
4407
4408 char n1[DT_TYPE_NAMELEN];
4409 char n2[DT_TYPE_NAMELEN];
4410
4411 if (prp->pr_nargs == prp->pr_xargs)
4412 return;
4413
4414 for (i = 0; i < prp->pr_xargc; i++) {
4415 dt_node_t *xnp = prp->pr_xargv[i];
4416 dt_node_t *nnp = prp->pr_nargv[prp->pr_mapping[i]];
4417
4418 if ((dxp = dt_xlator_lookup(dtp,
4419 nnp, xnp, DT_XLATE_FUZZY)) != NULL) {
4420 if (dt_provider_xref(dtp, pvp, dxp->dx_id) != 0)
4421 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
4422 continue;
4423 }
4424
4425 if (dt_node_is_argcompat(nnp, xnp))
4426 continue; /* no translator defined and none required */
4427
4428 dnerror(dnp, D_PROV_PRXLATOR, "translator for %s:%s output "
4429 "argument #%u from %s to %s is not defined\n",
4430 pvp->pv_desc.dtvd_name, dnp->dn_ident->di_name, i + 1,
4431 dt_node_type_name(nnp, n1, sizeof (n1)),
4432 dt_node_type_name(xnp, n2, sizeof (n2)));
4433 }
4434}
4435
4436/*ARGSUSED*/
4437static dt_node_t *
4438dt_cook_provider(dt_node_t *dnp, uint_t idflags)
4439{
4440 dt_provider_t *pvp = dnp->dn_provider;
4441 dt_node_t *pnp;
4442
4443 /*
4444 * If we're declaring a provider for the first time and it is unknown
4445 * to dtrace(7D), insert the probe definitions into the provider's hash.
4446 * If we're redeclaring a known provider, verify the interface matches.
4447 */
4448 for (pnp = dnp->dn_probes; pnp != NULL; pnp = pnp->dn_list) {
4449 const char *probename = pnp->dn_ident->di_name;
4450 dt_probe_t *prp = dt_probe_lookup(pvp, probename);
4451
4452 assert(pnp->dn_kind == DT_NODE_PROBE);
4453
4454 if (prp != NULL && dnp->dn_provred) {
4455 dt_node_provider_cmp(pvp, pnp,
4456 prp, pnp->dn_ident->di_data);
4457 } else if (prp == NULL && dnp->dn_provred) {
4458 dnerror(pnp, D_PROV_INCOMPAT,
4459 "provider interface mismatch: %s\n"
4460 "\t current: probe %s:%s defined\n"
4461 "\tprevious: probe %s:%s not defined\n",
4462 dnp->dn_provname, dnp->dn_provname,
4463 probename, dnp->dn_provname, probename);
4464 } else if (prp != NULL) {
4465 dnerror(pnp, D_PROV_PRDUP, "probe redeclared: %s:%s\n",
4466 dnp->dn_provname, probename);
4467 } else
4468 dt_probe_declare(pvp, pnp->dn_ident->di_data);
4469
4470 dt_cook_probe(pnp, pvp);
4471 }
4472
4473 return (dnp);
4474}
4475
4476/*ARGSUSED*/
4477static dt_node_t *
4478dt_cook_none(dt_node_t *dnp, uint_t idflags)
4479{
4480 return (dnp);
4481}
4482
4483static dt_node_t *(*dt_cook_funcs[])(dt_node_t *, uint_t) = {
4484 dt_cook_none, /* DT_NODE_FREE */
4485 dt_cook_none, /* DT_NODE_INT */
4486 dt_cook_none, /* DT_NODE_STRING */
4487 dt_cook_ident, /* DT_NODE_IDENT */
4488 dt_cook_var, /* DT_NODE_VAR */
4489 dt_cook_none, /* DT_NODE_SYM */
4490 dt_cook_none, /* DT_NODE_TYPE */
4491 dt_cook_func, /* DT_NODE_FUNC */
4492 dt_cook_op1, /* DT_NODE_OP1 */
4493 dt_cook_op2, /* DT_NODE_OP2 */
4494 dt_cook_op3, /* DT_NODE_OP3 */
4495 dt_cook_statement, /* DT_NODE_DEXPR */
4496 dt_cook_statement, /* DT_NODE_DFUNC */
4497 dt_cook_aggregation, /* DT_NODE_AGG */
4498 dt_cook_none, /* DT_NODE_PDESC */
4499 dt_cook_clause, /* DT_NODE_CLAUSE */
4500 dt_cook_inline, /* DT_NODE_INLINE */
4501 dt_cook_member, /* DT_NODE_MEMBER */
4502 dt_cook_xlator, /* DT_NODE_XLATOR */
4503 dt_cook_none, /* DT_NODE_PROBE */
4504 dt_cook_provider, /* DT_NODE_PROVIDER */
4505 dt_cook_none /* DT_NODE_PROG */
4506};
4507
4508/*
4509 * Recursively cook the parse tree starting at the specified node. The idflags
4510 * parameter is used to indicate the type of reference (r/w) and is applied to
4511 * the resulting identifier if it is a D variable or D aggregation.
4512 */
4513dt_node_t *
4514dt_node_cook(dt_node_t *dnp, uint_t idflags)
4515{
4516 int oldlineno = yylineno;
4517
4518 yylineno = dnp->dn_line;
4519
4520 dnp = dt_cook_funcs[dnp->dn_kind](dnp, idflags);
4521 dnp->dn_flags |= DT_NF_COOKED;
4522
4523 if (dnp->dn_kind == DT_NODE_VAR || dnp->dn_kind == DT_NODE_AGG)
4524 dnp->dn_ident->di_flags |= idflags;
4525
4526 yylineno = oldlineno;
4527 return (dnp);
4528}
4529
4530dtrace_attribute_t
4531dt_node_list_cook(dt_node_t **pnp, uint_t idflags)
4532{
4533 dtrace_attribute_t attr = _dtrace_defattr;
4534 dt_node_t *dnp, *nnp;
4535
4536 for (dnp = (pnp != NULL ? *pnp : NULL); dnp != NULL; dnp = nnp) {
4537 nnp = dnp->dn_list;
4538 dnp = *pnp = dt_node_cook(dnp, idflags);
4539 attr = dt_attr_min(attr, dnp->dn_attr);
4540 dnp->dn_list = nnp;
4541 pnp = &dnp->dn_list;
4542 }
4543
4544 return (attr);
4545}
4546
4547void
4548dt_node_list_free(dt_node_t **pnp)
4549{
4550 dt_node_t *dnp, *nnp;
4551
4552 for (dnp = (pnp != NULL ? *pnp : NULL); dnp != NULL; dnp = nnp) {
4553 nnp = dnp->dn_list;
4554 dt_node_free(dnp);
4555 }
4556
4557 if (pnp != NULL)
4558 *pnp = NULL;
4559}
4560
4561void
4562dt_node_link_free(dt_node_t **pnp)
4563{
4564 dt_node_t *dnp, *nnp;
4565
4566 for (dnp = (pnp != NULL ? *pnp : NULL); dnp != NULL; dnp = nnp) {
4567 nnp = dnp->dn_link;
4568 dt_node_free(dnp);
4569 }
4570
4571 for (dnp = (pnp != NULL ? *pnp : NULL); dnp != NULL; dnp = nnp) {
4572 nnp = dnp->dn_link;
4573 free(dnp);
4574 }
4575
4576 if (pnp != NULL)
4577 *pnp = NULL;
4578}
4579
4580dt_node_t *
4581dt_node_link(dt_node_t *lp, dt_node_t *rp)
4582{
4583 dt_node_t *dnp;
4584
4585 if (lp == NULL)
4586 return (rp);
4587 else if (rp == NULL)
4588 return (lp);
4589
4590 for (dnp = lp; dnp->dn_list != NULL; dnp = dnp->dn_list)
4591 continue;
4592
4593 dnp->dn_list = rp;
4594 return (lp);
4595}
4596
4597/*
4598 * Compute the DOF dtrace_diftype_t representation of a node's type. This is
4599 * called from a variety of places in the library so it cannot assume yypcb
4600 * is valid: any references to handle-specific data must be made through 'dtp'.
4601 */
4602void
4603dt_node_diftype(dtrace_hdl_t *dtp, const dt_node_t *dnp, dtrace_diftype_t *tp)
4604{
4605 if (dnp->dn_ctfp == DT_STR_CTFP(dtp) &&
4606 dnp->dn_type == DT_STR_TYPE(dtp)) {
4607 tp->dtdt_kind = DIF_TYPE_STRING;
4608 tp->dtdt_ckind = CTF_K_UNKNOWN;
4609 } else {
4610 tp->dtdt_kind = DIF_TYPE_CTF;
4611 tp->dtdt_ckind = ctf_type_kind(dnp->dn_ctfp,
4612 ctf_type_resolve(dnp->dn_ctfp, dnp->dn_type));
4613 }
4614
4564 tp->dtdt_flags = (dnp->dn_flags & DT_NF_REF) ? DIF_TF_BYREF : 0;
4615 tp->dtdt_flags = (dnp->dn_flags & DT_NF_REF) ?
4616 (dnp->dn_flags & DT_NF_USERLAND) ? DIF_TF_BYUREF :
4617 DIF_TF_BYREF : 0;
4565 tp->dtdt_pad = 0;
4566 tp->dtdt_size = ctf_type_size(dnp->dn_ctfp, dnp->dn_type);
4567}
4568
4569void
4570dt_node_printr(dt_node_t *dnp, FILE *fp, int depth)
4571{
4572 char n[DT_TYPE_NAMELEN], buf[BUFSIZ], a[8];
4573 const dtrace_syminfo_t *dts;
4574 const dt_idnode_t *inp;
4575 dt_node_t *arg;
4576
4577 (void) fprintf(fp, "%*s", depth * 2, "");
4578 (void) dt_attr_str(dnp->dn_attr, a, sizeof (a));
4579
4580 if (dnp->dn_ctfp != NULL && dnp->dn_type != CTF_ERR &&
4581 ctf_type_name(dnp->dn_ctfp, dnp->dn_type, n, sizeof (n)) != NULL) {
4582 (void) snprintf(buf, BUFSIZ, "type=<%s> attr=%s flags=", n, a);
4583 } else {
4584 (void) snprintf(buf, BUFSIZ, "type=<%ld> attr=%s flags=",
4585 dnp->dn_type, a);
4586 }
4587
4588 if (dnp->dn_flags != 0) {
4589 n[0] = '\0';
4590 if (dnp->dn_flags & DT_NF_SIGNED)
4591 (void) strcat(n, ",SIGN");
4592 if (dnp->dn_flags & DT_NF_COOKED)
4593 (void) strcat(n, ",COOK");
4594 if (dnp->dn_flags & DT_NF_REF)
4595 (void) strcat(n, ",REF");
4596 if (dnp->dn_flags & DT_NF_LVALUE)
4597 (void) strcat(n, ",LVAL");
4598 if (dnp->dn_flags & DT_NF_WRITABLE)
4599 (void) strcat(n, ",WRITE");
4600 if (dnp->dn_flags & DT_NF_BITFIELD)
4601 (void) strcat(n, ",BITF");
4602 if (dnp->dn_flags & DT_NF_USERLAND)
4603 (void) strcat(n, ",USER");
4604 (void) strcat(buf, n + 1);
4605 } else
4606 (void) strcat(buf, "0");
4607
4608 switch (dnp->dn_kind) {
4609 case DT_NODE_FREE:
4610 (void) fprintf(fp, "FREE <node %p>\n", (void *)dnp);
4611 break;
4612
4613 case DT_NODE_INT:
4614 (void) fprintf(fp, "INT 0x%llx (%s)\n",
4615 (u_longlong_t)dnp->dn_value, buf);
4616 break;
4617
4618 case DT_NODE_STRING:
4619 (void) fprintf(fp, "STRING \"%s\" (%s)\n", dnp->dn_string, buf);
4620 break;
4621
4622 case DT_NODE_IDENT:
4623 (void) fprintf(fp, "IDENT %s (%s)\n", dnp->dn_string, buf);
4624 break;
4625
4626 case DT_NODE_VAR:
4627 (void) fprintf(fp, "VARIABLE %s%s (%s)\n",
4628 (dnp->dn_ident->di_flags & DT_IDFLG_LOCAL) ? "this->" :
4629 (dnp->dn_ident->di_flags & DT_IDFLG_TLS) ? "self->" : "",
4630 dnp->dn_ident->di_name, buf);
4631
4632 if (dnp->dn_args != NULL)
4633 (void) fprintf(fp, "%*s[\n", depth * 2, "");
4634
4635 for (arg = dnp->dn_args; arg != NULL; arg = arg->dn_list) {
4636 dt_node_printr(arg, fp, depth + 1);
4637 if (arg->dn_list != NULL)
4638 (void) fprintf(fp, "%*s,\n", depth * 2, "");
4639 }
4640
4641 if (dnp->dn_args != NULL)
4642 (void) fprintf(fp, "%*s]\n", depth * 2, "");
4643 break;
4644
4645 case DT_NODE_SYM:
4646 dts = dnp->dn_ident->di_data;
4647 (void) fprintf(fp, "SYMBOL %s`%s (%s)\n",
4648 dts->dts_object, dts->dts_name, buf);
4649 break;
4650
4651 case DT_NODE_TYPE:
4652 if (dnp->dn_string != NULL) {
4653 (void) fprintf(fp, "TYPE (%s) %s\n",
4654 buf, dnp->dn_string);
4655 } else
4656 (void) fprintf(fp, "TYPE (%s)\n", buf);
4657 break;
4658
4659 case DT_NODE_FUNC:
4660 (void) fprintf(fp, "FUNC %s (%s)\n",
4661 dnp->dn_ident->di_name, buf);
4662
4663 for (arg = dnp->dn_args; arg != NULL; arg = arg->dn_list) {
4664 dt_node_printr(arg, fp, depth + 1);
4665 if (arg->dn_list != NULL)
4666 (void) fprintf(fp, "%*s,\n", depth * 2, "");
4667 }
4668 break;
4669
4670 case DT_NODE_OP1:
4671 (void) fprintf(fp, "OP1 %s (%s)\n", opstr(dnp->dn_op), buf);
4672 dt_node_printr(dnp->dn_child, fp, depth + 1);
4673 break;
4674
4675 case DT_NODE_OP2:
4676 (void) fprintf(fp, "OP2 %s (%s)\n", opstr(dnp->dn_op), buf);
4677 dt_node_printr(dnp->dn_left, fp, depth + 1);
4678 dt_node_printr(dnp->dn_right, fp, depth + 1);
4679 break;
4680
4681 case DT_NODE_OP3:
4682 (void) fprintf(fp, "OP3 (%s)\n", buf);
4683 dt_node_printr(dnp->dn_expr, fp, depth + 1);
4684 (void) fprintf(fp, "%*s?\n", depth * 2, "");
4685 dt_node_printr(dnp->dn_left, fp, depth + 1);
4686 (void) fprintf(fp, "%*s:\n", depth * 2, "");
4687 dt_node_printr(dnp->dn_right, fp, depth + 1);
4688 break;
4689
4690 case DT_NODE_DEXPR:
4691 case DT_NODE_DFUNC:
4692 (void) fprintf(fp, "D EXPRESSION attr=%s\n", a);
4693 dt_node_printr(dnp->dn_expr, fp, depth + 1);
4694 break;
4695
4696 case DT_NODE_AGG:
4697 (void) fprintf(fp, "AGGREGATE @%s attr=%s [\n",
4698 dnp->dn_ident->di_name, a);
4699
4700 for (arg = dnp->dn_aggtup; arg != NULL; arg = arg->dn_list) {
4701 dt_node_printr(arg, fp, depth + 1);
4702 if (arg->dn_list != NULL)
4703 (void) fprintf(fp, "%*s,\n", depth * 2, "");
4704 }
4705
4706 if (dnp->dn_aggfun) {
4707 (void) fprintf(fp, "%*s] = ", depth * 2, "");
4708 dt_node_printr(dnp->dn_aggfun, fp, depth + 1);
4709 } else
4710 (void) fprintf(fp, "%*s]\n", depth * 2, "");
4711
4712 if (dnp->dn_aggfun)
4713 (void) fprintf(fp, "%*s)\n", depth * 2, "");
4714 break;
4715
4716 case DT_NODE_PDESC:
4717 (void) fprintf(fp, "PDESC %s:%s:%s:%s [%u]\n",
4718 dnp->dn_desc->dtpd_provider, dnp->dn_desc->dtpd_mod,
4719 dnp->dn_desc->dtpd_func, dnp->dn_desc->dtpd_name,
4720 dnp->dn_desc->dtpd_id);
4721 break;
4722
4723 case DT_NODE_CLAUSE:
4724 (void) fprintf(fp, "CLAUSE attr=%s\n", a);
4725
4726 for (arg = dnp->dn_pdescs; arg != NULL; arg = arg->dn_list)
4727 dt_node_printr(arg, fp, depth + 1);
4728
4729 (void) fprintf(fp, "%*sCTXATTR %s\n", depth * 2, "",
4730 dt_attr_str(dnp->dn_ctxattr, a, sizeof (a)));
4731
4732 if (dnp->dn_pred != NULL) {
4733 (void) fprintf(fp, "%*sPREDICATE /\n", depth * 2, "");
4734 dt_node_printr(dnp->dn_pred, fp, depth + 1);
4735 (void) fprintf(fp, "%*s/\n", depth * 2, "");
4736 }
4737
4738 for (arg = dnp->dn_acts; arg != NULL; arg = arg->dn_list)
4739 dt_node_printr(arg, fp, depth + 1);
4740 break;
4741
4742 case DT_NODE_INLINE:
4743 inp = dnp->dn_ident->di_iarg;
4744
4745 (void) fprintf(fp, "INLINE %s (%s)\n",
4746 dnp->dn_ident->di_name, buf);
4747 dt_node_printr(inp->din_root, fp, depth + 1);
4748 break;
4749
4750 case DT_NODE_MEMBER:
4751 (void) fprintf(fp, "MEMBER %s (%s)\n", dnp->dn_membname, buf);
4752 if (dnp->dn_membexpr)
4753 dt_node_printr(dnp->dn_membexpr, fp, depth + 1);
4754 break;
4755
4756 case DT_NODE_XLATOR:
4757 (void) fprintf(fp, "XLATOR (%s)", buf);
4758
4759 if (ctf_type_name(dnp->dn_xlator->dx_src_ctfp,
4760 dnp->dn_xlator->dx_src_type, n, sizeof (n)) != NULL)
4761 (void) fprintf(fp, " from <%s>", n);
4762
4763 if (ctf_type_name(dnp->dn_xlator->dx_dst_ctfp,
4764 dnp->dn_xlator->dx_dst_type, n, sizeof (n)) != NULL)
4765 (void) fprintf(fp, " to <%s>", n);
4766
4767 (void) fprintf(fp, "\n");
4768
4769 for (arg = dnp->dn_members; arg != NULL; arg = arg->dn_list)
4770 dt_node_printr(arg, fp, depth + 1);
4771 break;
4772
4773 case DT_NODE_PROBE:
4774 (void) fprintf(fp, "PROBE %s\n", dnp->dn_ident->di_name);
4775 break;
4776
4777 case DT_NODE_PROVIDER:
4778 (void) fprintf(fp, "PROVIDER %s (%s)\n",
4779 dnp->dn_provname, dnp->dn_provred ? "redecl" : "decl");
4780 for (arg = dnp->dn_probes; arg != NULL; arg = arg->dn_list)
4781 dt_node_printr(arg, fp, depth + 1);
4782 break;
4783
4784 case DT_NODE_PROG:
4785 (void) fprintf(fp, "PROGRAM attr=%s\n", a);
4786 for (arg = dnp->dn_list; arg != NULL; arg = arg->dn_list)
4787 dt_node_printr(arg, fp, depth + 1);
4788 break;
4789
4790 default:
4791 (void) fprintf(fp, "<bad node %p, kind %d>\n",
4792 (void *)dnp, dnp->dn_kind);
4793 }
4794}
4795
4796int
4797dt_node_root(dt_node_t *dnp)
4798{
4799 yypcb->pcb_root = dnp;
4800 return (0);
4801}
4802
4803/*PRINTFLIKE3*/
4804void
4805dnerror(const dt_node_t *dnp, dt_errtag_t tag, const char *format, ...)
4806{
4807 int oldlineno = yylineno;
4808 va_list ap;
4809
4810 yylineno = dnp->dn_line;
4811
4812 va_start(ap, format);
4813 xyvwarn(tag, format, ap);
4814 va_end(ap);
4815
4816 yylineno = oldlineno;
4817 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
4818}
4819
4820/*PRINTFLIKE3*/
4821void
4822dnwarn(const dt_node_t *dnp, dt_errtag_t tag, const char *format, ...)
4823{
4824 int oldlineno = yylineno;
4825 va_list ap;
4826
4827 yylineno = dnp->dn_line;
4828
4829 va_start(ap, format);
4830 xyvwarn(tag, format, ap);
4831 va_end(ap);
4832
4833 yylineno = oldlineno;
4834}
4835
4836/*PRINTFLIKE2*/
4837void
4838xyerror(dt_errtag_t tag, const char *format, ...)
4839{
4840 va_list ap;
4841
4842 va_start(ap, format);
4843 xyvwarn(tag, format, ap);
4844 va_end(ap);
4845
4846 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
4847}
4848
4849/*PRINTFLIKE2*/
4850void
4851xywarn(dt_errtag_t tag, const char *format, ...)
4852{
4853 va_list ap;
4854
4855 va_start(ap, format);
4856 xyvwarn(tag, format, ap);
4857 va_end(ap);
4858}
4859
4860void
4861xyvwarn(dt_errtag_t tag, const char *format, va_list ap)
4862{
4863 if (yypcb == NULL)
4864 return; /* compiler is not currently active: act as a no-op */
4865
4866 dt_set_errmsg(yypcb->pcb_hdl, dt_errtag(tag), yypcb->pcb_region,
4867 yypcb->pcb_filetag, yypcb->pcb_fileptr ? yylineno : 0, format, ap);
4868}
4869
4870/*PRINTFLIKE1*/
4871void
4872yyerror(const char *format, ...)
4873{
4874 va_list ap;
4875
4876 va_start(ap, format);
4877 yyvwarn(format, ap);
4878 va_end(ap);
4879
4880 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
4881}
4882
4883/*PRINTFLIKE1*/
4884void
4885yywarn(const char *format, ...)
4886{
4887 va_list ap;
4888
4889 va_start(ap, format);
4890 yyvwarn(format, ap);
4891 va_end(ap);
4892}
4893
4894void
4895yyvwarn(const char *format, va_list ap)
4896{
4897 if (yypcb == NULL)
4898 return; /* compiler is not currently active: act as a no-op */
4899
4900 dt_set_errmsg(yypcb->pcb_hdl, dt_errtag(D_SYNTAX), yypcb->pcb_region,
4901 yypcb->pcb_filetag, yypcb->pcb_fileptr ? yylineno : 0, format, ap);
4902
4903 if (strchr(format, '\n') == NULL) {
4904 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
4905 size_t len = strlen(dtp->dt_errmsg);
4906 char *p, *s = dtp->dt_errmsg + len;
4907 size_t n = sizeof (dtp->dt_errmsg) - len;
4908
4909 if (yytext[0] == '\0')
4910 (void) snprintf(s, n, " near end of input");
4911 else if (yytext[0] == '\n')
4912 (void) snprintf(s, n, " near end of line");
4913 else {
4914 if ((p = strchr(yytext, '\n')) != NULL)
4915 *p = '\0'; /* crop at newline */
4916 (void) snprintf(s, n, " near \"%s\"", yytext);
4917 }
4918 }
4919}
4920
4921void
4922yylabel(const char *label)
4923{
4924 dt_dprintf("set label to <%s>\n", label ? label : "NULL");
4925 yypcb->pcb_region = label;
4926}
4927
4928int
4929yywrap(void)
4930{
4931 return (1); /* indicate that lex should return a zero token for EOF */
4932}
4618 tp->dtdt_pad = 0;
4619 tp->dtdt_size = ctf_type_size(dnp->dn_ctfp, dnp->dn_type);
4620}
4621
4622void
4623dt_node_printr(dt_node_t *dnp, FILE *fp, int depth)
4624{
4625 char n[DT_TYPE_NAMELEN], buf[BUFSIZ], a[8];
4626 const dtrace_syminfo_t *dts;
4627 const dt_idnode_t *inp;
4628 dt_node_t *arg;
4629
4630 (void) fprintf(fp, "%*s", depth * 2, "");
4631 (void) dt_attr_str(dnp->dn_attr, a, sizeof (a));
4632
4633 if (dnp->dn_ctfp != NULL && dnp->dn_type != CTF_ERR &&
4634 ctf_type_name(dnp->dn_ctfp, dnp->dn_type, n, sizeof (n)) != NULL) {
4635 (void) snprintf(buf, BUFSIZ, "type=<%s> attr=%s flags=", n, a);
4636 } else {
4637 (void) snprintf(buf, BUFSIZ, "type=<%ld> attr=%s flags=",
4638 dnp->dn_type, a);
4639 }
4640
4641 if (dnp->dn_flags != 0) {
4642 n[0] = '\0';
4643 if (dnp->dn_flags & DT_NF_SIGNED)
4644 (void) strcat(n, ",SIGN");
4645 if (dnp->dn_flags & DT_NF_COOKED)
4646 (void) strcat(n, ",COOK");
4647 if (dnp->dn_flags & DT_NF_REF)
4648 (void) strcat(n, ",REF");
4649 if (dnp->dn_flags & DT_NF_LVALUE)
4650 (void) strcat(n, ",LVAL");
4651 if (dnp->dn_flags & DT_NF_WRITABLE)
4652 (void) strcat(n, ",WRITE");
4653 if (dnp->dn_flags & DT_NF_BITFIELD)
4654 (void) strcat(n, ",BITF");
4655 if (dnp->dn_flags & DT_NF_USERLAND)
4656 (void) strcat(n, ",USER");
4657 (void) strcat(buf, n + 1);
4658 } else
4659 (void) strcat(buf, "0");
4660
4661 switch (dnp->dn_kind) {
4662 case DT_NODE_FREE:
4663 (void) fprintf(fp, "FREE <node %p>\n", (void *)dnp);
4664 break;
4665
4666 case DT_NODE_INT:
4667 (void) fprintf(fp, "INT 0x%llx (%s)\n",
4668 (u_longlong_t)dnp->dn_value, buf);
4669 break;
4670
4671 case DT_NODE_STRING:
4672 (void) fprintf(fp, "STRING \"%s\" (%s)\n", dnp->dn_string, buf);
4673 break;
4674
4675 case DT_NODE_IDENT:
4676 (void) fprintf(fp, "IDENT %s (%s)\n", dnp->dn_string, buf);
4677 break;
4678
4679 case DT_NODE_VAR:
4680 (void) fprintf(fp, "VARIABLE %s%s (%s)\n",
4681 (dnp->dn_ident->di_flags & DT_IDFLG_LOCAL) ? "this->" :
4682 (dnp->dn_ident->di_flags & DT_IDFLG_TLS) ? "self->" : "",
4683 dnp->dn_ident->di_name, buf);
4684
4685 if (dnp->dn_args != NULL)
4686 (void) fprintf(fp, "%*s[\n", depth * 2, "");
4687
4688 for (arg = dnp->dn_args; arg != NULL; arg = arg->dn_list) {
4689 dt_node_printr(arg, fp, depth + 1);
4690 if (arg->dn_list != NULL)
4691 (void) fprintf(fp, "%*s,\n", depth * 2, "");
4692 }
4693
4694 if (dnp->dn_args != NULL)
4695 (void) fprintf(fp, "%*s]\n", depth * 2, "");
4696 break;
4697
4698 case DT_NODE_SYM:
4699 dts = dnp->dn_ident->di_data;
4700 (void) fprintf(fp, "SYMBOL %s`%s (%s)\n",
4701 dts->dts_object, dts->dts_name, buf);
4702 break;
4703
4704 case DT_NODE_TYPE:
4705 if (dnp->dn_string != NULL) {
4706 (void) fprintf(fp, "TYPE (%s) %s\n",
4707 buf, dnp->dn_string);
4708 } else
4709 (void) fprintf(fp, "TYPE (%s)\n", buf);
4710 break;
4711
4712 case DT_NODE_FUNC:
4713 (void) fprintf(fp, "FUNC %s (%s)\n",
4714 dnp->dn_ident->di_name, buf);
4715
4716 for (arg = dnp->dn_args; arg != NULL; arg = arg->dn_list) {
4717 dt_node_printr(arg, fp, depth + 1);
4718 if (arg->dn_list != NULL)
4719 (void) fprintf(fp, "%*s,\n", depth * 2, "");
4720 }
4721 break;
4722
4723 case DT_NODE_OP1:
4724 (void) fprintf(fp, "OP1 %s (%s)\n", opstr(dnp->dn_op), buf);
4725 dt_node_printr(dnp->dn_child, fp, depth + 1);
4726 break;
4727
4728 case DT_NODE_OP2:
4729 (void) fprintf(fp, "OP2 %s (%s)\n", opstr(dnp->dn_op), buf);
4730 dt_node_printr(dnp->dn_left, fp, depth + 1);
4731 dt_node_printr(dnp->dn_right, fp, depth + 1);
4732 break;
4733
4734 case DT_NODE_OP3:
4735 (void) fprintf(fp, "OP3 (%s)\n", buf);
4736 dt_node_printr(dnp->dn_expr, fp, depth + 1);
4737 (void) fprintf(fp, "%*s?\n", depth * 2, "");
4738 dt_node_printr(dnp->dn_left, fp, depth + 1);
4739 (void) fprintf(fp, "%*s:\n", depth * 2, "");
4740 dt_node_printr(dnp->dn_right, fp, depth + 1);
4741 break;
4742
4743 case DT_NODE_DEXPR:
4744 case DT_NODE_DFUNC:
4745 (void) fprintf(fp, "D EXPRESSION attr=%s\n", a);
4746 dt_node_printr(dnp->dn_expr, fp, depth + 1);
4747 break;
4748
4749 case DT_NODE_AGG:
4750 (void) fprintf(fp, "AGGREGATE @%s attr=%s [\n",
4751 dnp->dn_ident->di_name, a);
4752
4753 for (arg = dnp->dn_aggtup; arg != NULL; arg = arg->dn_list) {
4754 dt_node_printr(arg, fp, depth + 1);
4755 if (arg->dn_list != NULL)
4756 (void) fprintf(fp, "%*s,\n", depth * 2, "");
4757 }
4758
4759 if (dnp->dn_aggfun) {
4760 (void) fprintf(fp, "%*s] = ", depth * 2, "");
4761 dt_node_printr(dnp->dn_aggfun, fp, depth + 1);
4762 } else
4763 (void) fprintf(fp, "%*s]\n", depth * 2, "");
4764
4765 if (dnp->dn_aggfun)
4766 (void) fprintf(fp, "%*s)\n", depth * 2, "");
4767 break;
4768
4769 case DT_NODE_PDESC:
4770 (void) fprintf(fp, "PDESC %s:%s:%s:%s [%u]\n",
4771 dnp->dn_desc->dtpd_provider, dnp->dn_desc->dtpd_mod,
4772 dnp->dn_desc->dtpd_func, dnp->dn_desc->dtpd_name,
4773 dnp->dn_desc->dtpd_id);
4774 break;
4775
4776 case DT_NODE_CLAUSE:
4777 (void) fprintf(fp, "CLAUSE attr=%s\n", a);
4778
4779 for (arg = dnp->dn_pdescs; arg != NULL; arg = arg->dn_list)
4780 dt_node_printr(arg, fp, depth + 1);
4781
4782 (void) fprintf(fp, "%*sCTXATTR %s\n", depth * 2, "",
4783 dt_attr_str(dnp->dn_ctxattr, a, sizeof (a)));
4784
4785 if (dnp->dn_pred != NULL) {
4786 (void) fprintf(fp, "%*sPREDICATE /\n", depth * 2, "");
4787 dt_node_printr(dnp->dn_pred, fp, depth + 1);
4788 (void) fprintf(fp, "%*s/\n", depth * 2, "");
4789 }
4790
4791 for (arg = dnp->dn_acts; arg != NULL; arg = arg->dn_list)
4792 dt_node_printr(arg, fp, depth + 1);
4793 break;
4794
4795 case DT_NODE_INLINE:
4796 inp = dnp->dn_ident->di_iarg;
4797
4798 (void) fprintf(fp, "INLINE %s (%s)\n",
4799 dnp->dn_ident->di_name, buf);
4800 dt_node_printr(inp->din_root, fp, depth + 1);
4801 break;
4802
4803 case DT_NODE_MEMBER:
4804 (void) fprintf(fp, "MEMBER %s (%s)\n", dnp->dn_membname, buf);
4805 if (dnp->dn_membexpr)
4806 dt_node_printr(dnp->dn_membexpr, fp, depth + 1);
4807 break;
4808
4809 case DT_NODE_XLATOR:
4810 (void) fprintf(fp, "XLATOR (%s)", buf);
4811
4812 if (ctf_type_name(dnp->dn_xlator->dx_src_ctfp,
4813 dnp->dn_xlator->dx_src_type, n, sizeof (n)) != NULL)
4814 (void) fprintf(fp, " from <%s>", n);
4815
4816 if (ctf_type_name(dnp->dn_xlator->dx_dst_ctfp,
4817 dnp->dn_xlator->dx_dst_type, n, sizeof (n)) != NULL)
4818 (void) fprintf(fp, " to <%s>", n);
4819
4820 (void) fprintf(fp, "\n");
4821
4822 for (arg = dnp->dn_members; arg != NULL; arg = arg->dn_list)
4823 dt_node_printr(arg, fp, depth + 1);
4824 break;
4825
4826 case DT_NODE_PROBE:
4827 (void) fprintf(fp, "PROBE %s\n", dnp->dn_ident->di_name);
4828 break;
4829
4830 case DT_NODE_PROVIDER:
4831 (void) fprintf(fp, "PROVIDER %s (%s)\n",
4832 dnp->dn_provname, dnp->dn_provred ? "redecl" : "decl");
4833 for (arg = dnp->dn_probes; arg != NULL; arg = arg->dn_list)
4834 dt_node_printr(arg, fp, depth + 1);
4835 break;
4836
4837 case DT_NODE_PROG:
4838 (void) fprintf(fp, "PROGRAM attr=%s\n", a);
4839 for (arg = dnp->dn_list; arg != NULL; arg = arg->dn_list)
4840 dt_node_printr(arg, fp, depth + 1);
4841 break;
4842
4843 default:
4844 (void) fprintf(fp, "<bad node %p, kind %d>\n",
4845 (void *)dnp, dnp->dn_kind);
4846 }
4847}
4848
4849int
4850dt_node_root(dt_node_t *dnp)
4851{
4852 yypcb->pcb_root = dnp;
4853 return (0);
4854}
4855
4856/*PRINTFLIKE3*/
4857void
4858dnerror(const dt_node_t *dnp, dt_errtag_t tag, const char *format, ...)
4859{
4860 int oldlineno = yylineno;
4861 va_list ap;
4862
4863 yylineno = dnp->dn_line;
4864
4865 va_start(ap, format);
4866 xyvwarn(tag, format, ap);
4867 va_end(ap);
4868
4869 yylineno = oldlineno;
4870 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
4871}
4872
4873/*PRINTFLIKE3*/
4874void
4875dnwarn(const dt_node_t *dnp, dt_errtag_t tag, const char *format, ...)
4876{
4877 int oldlineno = yylineno;
4878 va_list ap;
4879
4880 yylineno = dnp->dn_line;
4881
4882 va_start(ap, format);
4883 xyvwarn(tag, format, ap);
4884 va_end(ap);
4885
4886 yylineno = oldlineno;
4887}
4888
4889/*PRINTFLIKE2*/
4890void
4891xyerror(dt_errtag_t tag, const char *format, ...)
4892{
4893 va_list ap;
4894
4895 va_start(ap, format);
4896 xyvwarn(tag, format, ap);
4897 va_end(ap);
4898
4899 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
4900}
4901
4902/*PRINTFLIKE2*/
4903void
4904xywarn(dt_errtag_t tag, const char *format, ...)
4905{
4906 va_list ap;
4907
4908 va_start(ap, format);
4909 xyvwarn(tag, format, ap);
4910 va_end(ap);
4911}
4912
4913void
4914xyvwarn(dt_errtag_t tag, const char *format, va_list ap)
4915{
4916 if (yypcb == NULL)
4917 return; /* compiler is not currently active: act as a no-op */
4918
4919 dt_set_errmsg(yypcb->pcb_hdl, dt_errtag(tag), yypcb->pcb_region,
4920 yypcb->pcb_filetag, yypcb->pcb_fileptr ? yylineno : 0, format, ap);
4921}
4922
4923/*PRINTFLIKE1*/
4924void
4925yyerror(const char *format, ...)
4926{
4927 va_list ap;
4928
4929 va_start(ap, format);
4930 yyvwarn(format, ap);
4931 va_end(ap);
4932
4933 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
4934}
4935
4936/*PRINTFLIKE1*/
4937void
4938yywarn(const char *format, ...)
4939{
4940 va_list ap;
4941
4942 va_start(ap, format);
4943 yyvwarn(format, ap);
4944 va_end(ap);
4945}
4946
4947void
4948yyvwarn(const char *format, va_list ap)
4949{
4950 if (yypcb == NULL)
4951 return; /* compiler is not currently active: act as a no-op */
4952
4953 dt_set_errmsg(yypcb->pcb_hdl, dt_errtag(D_SYNTAX), yypcb->pcb_region,
4954 yypcb->pcb_filetag, yypcb->pcb_fileptr ? yylineno : 0, format, ap);
4955
4956 if (strchr(format, '\n') == NULL) {
4957 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
4958 size_t len = strlen(dtp->dt_errmsg);
4959 char *p, *s = dtp->dt_errmsg + len;
4960 size_t n = sizeof (dtp->dt_errmsg) - len;
4961
4962 if (yytext[0] == '\0')
4963 (void) snprintf(s, n, " near end of input");
4964 else if (yytext[0] == '\n')
4965 (void) snprintf(s, n, " near end of line");
4966 else {
4967 if ((p = strchr(yytext, '\n')) != NULL)
4968 *p = '\0'; /* crop at newline */
4969 (void) snprintf(s, n, " near \"%s\"", yytext);
4970 }
4971 }
4972}
4973
4974void
4975yylabel(const char *label)
4976{
4977 dt_dprintf("set label to <%s>\n", label ? label : "NULL");
4978 yypcb->pcb_region = label;
4979}
4980
4981int
4982yywrap(void)
4983{
4984 return (1); /* indicate that lex should return a zero token for EOF */
4985}