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