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