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