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