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