xref: /illumos-gate/usr/src/lib/libdtrace/common/dt_parser.c (revision 6eeafb34dceabceff80ed689002b6dc3e060f498)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
523a1cceaSRoger A. Faulkner  * Common Development and Distribution License (the "License").
623a1cceaSRoger A. Faulkner  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21e4586ebfSmws 
227c478bd9Sstevel@tonic-gate /*
2323a1cceaSRoger A. Faulkner  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24bc1f688bSRobert Mustacchi  * Copyright (c) 2015, Joyent Inc. All rights reserved.
25c3bd3abdSMatthew Ahrens  * Copyright (c) 2012, 2016 by Delphix. All rights reserved.
26*6eeafb34SRobert Mustacchi  * Copyright 2022 Oxide Computer Company
277c478bd9Sstevel@tonic-gate  */
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * DTrace D Language Parser
317c478bd9Sstevel@tonic-gate  *
327c478bd9Sstevel@tonic-gate  * The D Parser is a lex/yacc parser consisting of the lexer dt_lex.l, the
337c478bd9Sstevel@tonic-gate  * parsing grammar dt_grammar.y, and this file, dt_parser.c, which handles
347c478bd9Sstevel@tonic-gate  * the construction of the parse tree nodes and their syntactic validation.
357c478bd9Sstevel@tonic-gate  * The parse tree is constructed of dt_node_t structures (see <dt_parser.h>)
367c478bd9Sstevel@tonic-gate  * that are built in two passes: (1) the "create" pass, where the parse tree
377c478bd9Sstevel@tonic-gate  * nodes are allocated by calls from the grammar to dt_node_*() subroutines,
387c478bd9Sstevel@tonic-gate  * and (2) the "cook" pass, where nodes are coalesced, assigned D types, and
397c478bd9Sstevel@tonic-gate  * validated according to the syntactic rules of the language.
407c478bd9Sstevel@tonic-gate  *
417c478bd9Sstevel@tonic-gate  * All node allocations are performed using dt_node_alloc().  All node frees
427c478bd9Sstevel@tonic-gate  * during the parsing phase are performed by dt_node_free(), which frees node-
437c478bd9Sstevel@tonic-gate  * internal state but does not actually free the nodes.  All final node frees
447c478bd9Sstevel@tonic-gate  * are done as part of the end of dt_compile() or as part of destroying
457c478bd9Sstevel@tonic-gate  * persistent identifiers or translators which have embedded nodes.
467c478bd9Sstevel@tonic-gate  *
477c478bd9Sstevel@tonic-gate  * The dt_node_* routines that implement pass (1) may allocate new nodes.  The
487c478bd9Sstevel@tonic-gate  * dt_cook_* routines that implement pass (2) may *not* allocate new nodes.
497c478bd9Sstevel@tonic-gate  * They may free existing nodes using dt_node_free(), but they may not actually
507c478bd9Sstevel@tonic-gate  * deallocate any dt_node_t's.  Currently dt_cook_op2() is an exception to this
517c478bd9Sstevel@tonic-gate  * rule: see the comments therein for how this issue is resolved.
527c478bd9Sstevel@tonic-gate  *
537c478bd9Sstevel@tonic-gate  * The dt_cook_* routines are responsible for (at minimum) setting the final
547c478bd9Sstevel@tonic-gate  * node type (dn_ctfp/dn_type) and attributes (dn_attr).  If dn_ctfp/dn_type
557c478bd9Sstevel@tonic-gate  * are set manually (i.e. not by one of the type assignment functions), then
567c478bd9Sstevel@tonic-gate  * the DT_NF_COOKED flag must be set manually on the node.
577c478bd9Sstevel@tonic-gate  *
587c478bd9Sstevel@tonic-gate  * The cooking pass can be applied to the same parse tree more than once (used
597c478bd9Sstevel@tonic-gate  * in the case of a comma-separated list of probe descriptions).  As such, the
607c478bd9Sstevel@tonic-gate  * cook routines must not perform any parse tree transformations which would
617c478bd9Sstevel@tonic-gate  * be invalid if the tree were subsequently cooked using a different context.
627c478bd9Sstevel@tonic-gate  *
637c478bd9Sstevel@tonic-gate  * The dn_ctfp and dn_type fields form the type of the node.  This tuple can
647c478bd9Sstevel@tonic-gate  * take on the following set of values, which form our type invariants:
657c478bd9Sstevel@tonic-gate  *
667c478bd9Sstevel@tonic-gate  * 1. dn_ctfp = NULL, dn_type = CTF_ERR
677c478bd9Sstevel@tonic-gate  *
687c478bd9Sstevel@tonic-gate  *    In this state, the node has unknown type and is not yet cooked.  The
697c478bd9Sstevel@tonic-gate  *    DT_NF_COOKED flag is not yet set on the node.
707c478bd9Sstevel@tonic-gate  *
717c478bd9Sstevel@tonic-gate  * 2. dn_ctfp = DT_DYN_CTFP(dtp), dn_type = DT_DYN_TYPE(dtp)
727c478bd9Sstevel@tonic-gate  *
737c478bd9Sstevel@tonic-gate  *    In this state, the node is a dynamic D type.  This means that generic
747c478bd9Sstevel@tonic-gate  *    operations are not valid on this node and only code that knows how to
757c478bd9Sstevel@tonic-gate  *    examine the inner details of the node can operate on it.  A <DYN> node
767c478bd9Sstevel@tonic-gate  *    must have dn_ident set to point to an identifier describing the object
777c478bd9Sstevel@tonic-gate  *    and its type.  The DT_NF_REF flag is set for all nodes of type <DYN>.
787c478bd9Sstevel@tonic-gate  *    At present, the D compiler uses the <DYN> type for:
797c478bd9Sstevel@tonic-gate  *
807c478bd9Sstevel@tonic-gate  *    - associative arrays that do not yet have a value type defined
817c478bd9Sstevel@tonic-gate  *    - translated data (i.e. the result of the xlate operator)
827c478bd9Sstevel@tonic-gate  *    - aggregations
837c478bd9Sstevel@tonic-gate  *
847c478bd9Sstevel@tonic-gate  * 3. dn_ctfp = DT_STR_CTFP(dtp), dn_type = DT_STR_TYPE(dtp)
857c478bd9Sstevel@tonic-gate  *
867c478bd9Sstevel@tonic-gate  *    In this state, the node is of type D string.  The string type is really
877c478bd9Sstevel@tonic-gate  *    a char[0] typedef, but requires special handling throughout the compiler.
887c478bd9Sstevel@tonic-gate  *
897c478bd9Sstevel@tonic-gate  * 4. dn_ctfp != NULL, dn_type = any other type ID
907c478bd9Sstevel@tonic-gate  *
917c478bd9Sstevel@tonic-gate  *    In this state, the node is of some known D/CTF type.  The normal libctf
927c478bd9Sstevel@tonic-gate  *    APIs can be used to learn more about the type name or structure.  When
937c478bd9Sstevel@tonic-gate  *    the type is assigned, the DT_NF_SIGNED, DT_NF_REF, and DT_NF_BITFIELD
947c478bd9Sstevel@tonic-gate  *    flags cache the corresponding attributes of the underlying CTF type.
957c478bd9Sstevel@tonic-gate  */
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate #include <sys/param.h>
98e5803b76SAdam H. Leventhal #include <sys/sysmacros.h>
997c478bd9Sstevel@tonic-gate #include <limits.h>
1007c478bd9Sstevel@tonic-gate #include <setjmp.h>
1017c478bd9Sstevel@tonic-gate #include <strings.h>
1027c478bd9Sstevel@tonic-gate #include <assert.h>
1037c478bd9Sstevel@tonic-gate #include <alloca.h>
1047c478bd9Sstevel@tonic-gate #include <stdlib.h>
1057c478bd9Sstevel@tonic-gate #include <stdarg.h>
1067c478bd9Sstevel@tonic-gate #include <stdio.h>
1077c478bd9Sstevel@tonic-gate #include <errno.h>
1087c478bd9Sstevel@tonic-gate #include <ctype.h>
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate #include <dt_impl.h>
1117c478bd9Sstevel@tonic-gate #include <dt_grammar.h>
1127c478bd9Sstevel@tonic-gate #include <dt_module.h>
1137c478bd9Sstevel@tonic-gate #include <dt_provider.h>
1147c478bd9Sstevel@tonic-gate #include <dt_string.h>
1157c478bd9Sstevel@tonic-gate #include <dt_as.h>
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate dt_pcb_t *yypcb;	/* current control block for parser */
1187c478bd9Sstevel@tonic-gate dt_node_t *yypragma;	/* lex token list for control lines */
1197c478bd9Sstevel@tonic-gate char yyintprefix;	/* int token macro prefix (+/-) */
1207c478bd9Sstevel@tonic-gate char yyintsuffix[4];	/* int token suffix string [uU][lL] */
1217c478bd9Sstevel@tonic-gate int yyintdecimal;	/* int token format flag (1=decimal, 0=octal/hex) */
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate static const char *
opstr(int op)1247c478bd9Sstevel@tonic-gate opstr(int op)
1257c478bd9Sstevel@tonic-gate {
1267c478bd9Sstevel@tonic-gate 	switch (op) {
1277c478bd9Sstevel@tonic-gate 	case DT_TOK_COMMA:	return (",");
1287c478bd9Sstevel@tonic-gate 	case DT_TOK_ELLIPSIS:	return ("...");
1297c478bd9Sstevel@tonic-gate 	case DT_TOK_ASGN:	return ("=");
1307c478bd9Sstevel@tonic-gate 	case DT_TOK_ADD_EQ:	return ("+=");
1317c478bd9Sstevel@tonic-gate 	case DT_TOK_SUB_EQ:	return ("-=");
1327c478bd9Sstevel@tonic-gate 	case DT_TOK_MUL_EQ:	return ("*=");
1337c478bd9Sstevel@tonic-gate 	case DT_TOK_DIV_EQ:	return ("/=");
1347c478bd9Sstevel@tonic-gate 	case DT_TOK_MOD_EQ:	return ("%=");
1357c478bd9Sstevel@tonic-gate 	case DT_TOK_AND_EQ:	return ("&=");
1367c478bd9Sstevel@tonic-gate 	case DT_TOK_XOR_EQ:	return ("^=");
1377c478bd9Sstevel@tonic-gate 	case DT_TOK_OR_EQ:	return ("|=");
1387c478bd9Sstevel@tonic-gate 	case DT_TOK_LSH_EQ:	return ("<<=");
1397c478bd9Sstevel@tonic-gate 	case DT_TOK_RSH_EQ:	return (">>=");
1407c478bd9Sstevel@tonic-gate 	case DT_TOK_QUESTION:	return ("?");
1417c478bd9Sstevel@tonic-gate 	case DT_TOK_COLON:	return (":");
1427c478bd9Sstevel@tonic-gate 	case DT_TOK_LOR:	return ("||");
1437c478bd9Sstevel@tonic-gate 	case DT_TOK_LXOR:	return ("^^");
1447c478bd9Sstevel@tonic-gate 	case DT_TOK_LAND:	return ("&&");
1457c478bd9Sstevel@tonic-gate 	case DT_TOK_BOR:	return ("|");
1467c478bd9Sstevel@tonic-gate 	case DT_TOK_XOR:	return ("^");
1477c478bd9Sstevel@tonic-gate 	case DT_TOK_BAND:	return ("&");
1487c478bd9Sstevel@tonic-gate 	case DT_TOK_EQU:	return ("==");
1497c478bd9Sstevel@tonic-gate 	case DT_TOK_NEQ:	return ("!=");
1507c478bd9Sstevel@tonic-gate 	case DT_TOK_LT:		return ("<");
1517c478bd9Sstevel@tonic-gate 	case DT_TOK_LE:		return ("<=");
1527c478bd9Sstevel@tonic-gate 	case DT_TOK_GT:		return (">");
1537c478bd9Sstevel@tonic-gate 	case DT_TOK_GE:		return (">=");
1547c478bd9Sstevel@tonic-gate 	case DT_TOK_LSH:	return ("<<");
1557c478bd9Sstevel@tonic-gate 	case DT_TOK_RSH:	return (">>");
1567c478bd9Sstevel@tonic-gate 	case DT_TOK_ADD:	return ("+");
1577c478bd9Sstevel@tonic-gate 	case DT_TOK_SUB:	return ("-");
1587c478bd9Sstevel@tonic-gate 	case DT_TOK_MUL:	return ("*");
1597c478bd9Sstevel@tonic-gate 	case DT_TOK_DIV:	return ("/");
1607c478bd9Sstevel@tonic-gate 	case DT_TOK_MOD:	return ("%");
1617c478bd9Sstevel@tonic-gate 	case DT_TOK_LNEG:	return ("!");
1627c478bd9Sstevel@tonic-gate 	case DT_TOK_BNEG:	return ("~");
1637c478bd9Sstevel@tonic-gate 	case DT_TOK_ADDADD:	return ("++");
1647c478bd9Sstevel@tonic-gate 	case DT_TOK_PREINC:	return ("++");
1657c478bd9Sstevel@tonic-gate 	case DT_TOK_POSTINC:	return ("++");
1667c478bd9Sstevel@tonic-gate 	case DT_TOK_SUBSUB:	return ("--");
1677c478bd9Sstevel@tonic-gate 	case DT_TOK_PREDEC:	return ("--");
1687c478bd9Sstevel@tonic-gate 	case DT_TOK_POSTDEC:	return ("--");
1697c478bd9Sstevel@tonic-gate 	case DT_TOK_IPOS:	return ("+");
1707c478bd9Sstevel@tonic-gate 	case DT_TOK_INEG:	return ("-");
1717c478bd9Sstevel@tonic-gate 	case DT_TOK_DEREF:	return ("*");
1727c478bd9Sstevel@tonic-gate 	case DT_TOK_ADDROF:	return ("&");
1737c478bd9Sstevel@tonic-gate 	case DT_TOK_OFFSETOF:	return ("offsetof");
1747c478bd9Sstevel@tonic-gate 	case DT_TOK_SIZEOF:	return ("sizeof");
1757c478bd9Sstevel@tonic-gate 	case DT_TOK_STRINGOF:	return ("stringof");
1767c478bd9Sstevel@tonic-gate 	case DT_TOK_XLATE:	return ("xlate");
1777c478bd9Sstevel@tonic-gate 	case DT_TOK_LPAR:	return ("(");
1787c478bd9Sstevel@tonic-gate 	case DT_TOK_RPAR:	return (")");
1797c478bd9Sstevel@tonic-gate 	case DT_TOK_LBRAC:	return ("[");
1807c478bd9Sstevel@tonic-gate 	case DT_TOK_RBRAC:	return ("]");
1817c478bd9Sstevel@tonic-gate 	case DT_TOK_PTR:	return ("->");
1827c478bd9Sstevel@tonic-gate 	case DT_TOK_DOT:	return (".");
1837c478bd9Sstevel@tonic-gate 	case DT_TOK_STRING:	return ("<string>");
1847c478bd9Sstevel@tonic-gate 	case DT_TOK_IDENT:	return ("<ident>");
1857c478bd9Sstevel@tonic-gate 	case DT_TOK_TNAME:	return ("<type>");
1867c478bd9Sstevel@tonic-gate 	case DT_TOK_INT:	return ("<int>");
1877c478bd9Sstevel@tonic-gate 	default:		return ("<?>");
1887c478bd9Sstevel@tonic-gate 	}
1897c478bd9Sstevel@tonic-gate }
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate int
dt_type_lookup(const char * s,dtrace_typeinfo_t * tip)1927c478bd9Sstevel@tonic-gate dt_type_lookup(const char *s, dtrace_typeinfo_t *tip)
1937c478bd9Sstevel@tonic-gate {
1947c478bd9Sstevel@tonic-gate 	static const char delimiters[] = " \t\n\r\v\f*`";
1957c478bd9Sstevel@tonic-gate 	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
196a386cc11SRobert Mustacchi 	const char *p, *q, *r, *end, *obj;
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate 	for (p = s, end = s + strlen(s); *p != '\0'; p = q) {
1997c478bd9Sstevel@tonic-gate 		while (isspace(*p))
2007c478bd9Sstevel@tonic-gate 			p++;	/* skip leading whitespace prior to token */
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate 		if (p == end || (q = strpbrk(p + 1, delimiters)) == NULL)
2037c478bd9Sstevel@tonic-gate 			break;	/* empty string or single token remaining */
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 		if (*q == '`') {
2067c478bd9Sstevel@tonic-gate 			char *object = alloca((size_t)(q - p) + 1);
2077c478bd9Sstevel@tonic-gate 			char *type = alloca((size_t)(end - s) + 1);
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 			/*
2107c478bd9Sstevel@tonic-gate 			 * Copy from the start of the token (p) to the location
2117c478bd9Sstevel@tonic-gate 			 * backquote (q) to extract the nul-terminated object.
2127c478bd9Sstevel@tonic-gate 			 */
2137c478bd9Sstevel@tonic-gate 			bcopy(p, object, (size_t)(q - p));
2147c478bd9Sstevel@tonic-gate 			object[(size_t)(q - p)] = '\0';
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 			/*
2177c478bd9Sstevel@tonic-gate 			 * Copy the original string up to the start of this
2187c478bd9Sstevel@tonic-gate 			 * token (p) into type, and then concatenate everything
2197c478bd9Sstevel@tonic-gate 			 * after q.  This is the type name without the object.
2207c478bd9Sstevel@tonic-gate 			 */
2217c478bd9Sstevel@tonic-gate 			bcopy(s, type, (size_t)(p - s));
2227c478bd9Sstevel@tonic-gate 			bcopy(q + 1, type + (size_t)(p - s), strlen(q + 1) + 1);
2237c478bd9Sstevel@tonic-gate 
224a386cc11SRobert Mustacchi 			/*
225a386cc11SRobert Mustacchi 			 * There may be at most three delimeters. The second
226a386cc11SRobert Mustacchi 			 * delimeter is usually used to distinguish the type
227a386cc11SRobert Mustacchi 			 * within a given module, however, there could be a link
228a386cc11SRobert Mustacchi 			 * map id on the scene in which case that delimeter
229a386cc11SRobert Mustacchi 			 * would be the third. We determine presence of the lmid
230a386cc11SRobert Mustacchi 			 * if it rouglhly meets the from LM[0-9]
231a386cc11SRobert Mustacchi 			 */
232a386cc11SRobert Mustacchi 			if ((r = strchr(q + 1, '`')) != NULL &&
233a386cc11SRobert Mustacchi 			    ((r = strchr(r + 1, '`')) != NULL)) {
234a386cc11SRobert Mustacchi 				if (strchr(r + 1, '`') != NULL)
235a386cc11SRobert Mustacchi 					return (dt_set_errno(dtp,
236a386cc11SRobert Mustacchi 					    EDT_BADSCOPE));
237a386cc11SRobert Mustacchi 				if (q[1] != 'L' || q[2] != 'M')
238a386cc11SRobert Mustacchi 					return (dt_set_errno(dtp,
239a386cc11SRobert Mustacchi 					    EDT_BADSCOPE));
240a386cc11SRobert Mustacchi 			}
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate 			return (dtrace_lookup_by_type(dtp, object, type, tip));
2437c478bd9Sstevel@tonic-gate 		}
2447c478bd9Sstevel@tonic-gate 	}
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate 	if (yypcb->pcb_idepth != 0)
2477c478bd9Sstevel@tonic-gate 		obj = DTRACE_OBJ_CDEFS;
2487c478bd9Sstevel@tonic-gate 	else
2497c478bd9Sstevel@tonic-gate 		obj = DTRACE_OBJ_EVERY;
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate 	return (dtrace_lookup_by_type(dtp, obj, s, tip));
2527c478bd9Sstevel@tonic-gate }
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate /*
2557c478bd9Sstevel@tonic-gate  * When we parse type expressions or parse an expression with unary "&", we
2567c478bd9Sstevel@tonic-gate  * need to find a type that is a pointer to a previously known type.
2577c478bd9Sstevel@tonic-gate  * Unfortunately CTF is limited to a per-container view, so ctf_type_pointer()
2587c478bd9Sstevel@tonic-gate  * alone does not suffice for our needs.  We provide a more intelligent wrapper
2597c478bd9Sstevel@tonic-gate  * for the compiler that attempts to compute a pointer to either the given type
2607c478bd9Sstevel@tonic-gate  * or its base (that is, we try both "foo_t *" and "struct foo *"), and also
2617c478bd9Sstevel@tonic-gate  * to potentially construct the required type on-the-fly.
2627c478bd9Sstevel@tonic-gate  */
2637c478bd9Sstevel@tonic-gate int
dt_type_pointer(dtrace_typeinfo_t * tip)2647c478bd9Sstevel@tonic-gate dt_type_pointer(dtrace_typeinfo_t *tip)
2657c478bd9Sstevel@tonic-gate {
2667c478bd9Sstevel@tonic-gate 	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
2677c478bd9Sstevel@tonic-gate 	ctf_file_t *ctfp = tip->dtt_ctfp;
2687c478bd9Sstevel@tonic-gate 	ctf_id_t type = tip->dtt_type;
2697c478bd9Sstevel@tonic-gate 	ctf_id_t base = ctf_type_resolve(ctfp, type);
270a386cc11SRobert Mustacchi 	uint_t bflags = tip->dtt_flags;
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate 	dt_module_t *dmp;
2737c478bd9Sstevel@tonic-gate 	ctf_id_t ptr;
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate 	if ((ptr = ctf_type_pointer(ctfp, type)) != CTF_ERR ||
2767c478bd9Sstevel@tonic-gate 	    (ptr = ctf_type_pointer(ctfp, base)) != CTF_ERR) {
2777c478bd9Sstevel@tonic-gate 		tip->dtt_type = ptr;
2787c478bd9Sstevel@tonic-gate 		return (0);
2797c478bd9Sstevel@tonic-gate 	}
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate 	if (yypcb->pcb_idepth != 0)
2827c478bd9Sstevel@tonic-gate 		dmp = dtp->dt_cdefs;
2837c478bd9Sstevel@tonic-gate 	else
2847c478bd9Sstevel@tonic-gate 		dmp = dtp->dt_ddefs;
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate 	if (ctfp != dmp->dm_ctfp && ctfp != ctf_parent_file(dmp->dm_ctfp) &&
2877c478bd9Sstevel@tonic-gate 	    (type = ctf_add_type(dmp->dm_ctfp, ctfp, type)) == CTF_ERR) {
2887c478bd9Sstevel@tonic-gate 		dtp->dt_ctferr = ctf_errno(dmp->dm_ctfp);
2897c478bd9Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_CTF));
2907c478bd9Sstevel@tonic-gate 	}
2917c478bd9Sstevel@tonic-gate 
292bc1f688bSRobert Mustacchi 	ptr = ctf_add_pointer(dmp->dm_ctfp, CTF_ADD_ROOT, NULL, type);
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate 	if (ptr == CTF_ERR || ctf_update(dmp->dm_ctfp) == CTF_ERR) {
2957c478bd9Sstevel@tonic-gate 		dtp->dt_ctferr = ctf_errno(dmp->dm_ctfp);
2967c478bd9Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_CTF));
2977c478bd9Sstevel@tonic-gate 	}
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 	tip->dtt_object = dmp->dm_name;
3007c478bd9Sstevel@tonic-gate 	tip->dtt_ctfp = dmp->dm_ctfp;
3017c478bd9Sstevel@tonic-gate 	tip->dtt_type = ptr;
302a386cc11SRobert Mustacchi 	tip->dtt_flags = bflags;
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate 	return (0);
3057c478bd9Sstevel@tonic-gate }
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate const char *
dt_type_name(ctf_file_t * ctfp,ctf_id_t type,char * buf,size_t len)3087c478bd9Sstevel@tonic-gate dt_type_name(ctf_file_t *ctfp, ctf_id_t type, char *buf, size_t len)
3097c478bd9Sstevel@tonic-gate {
3107c478bd9Sstevel@tonic-gate 	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate 	if (ctfp == DT_FPTR_CTFP(dtp) && type == DT_FPTR_TYPE(dtp))
3137c478bd9Sstevel@tonic-gate 		(void) snprintf(buf, len, "function pointer");
3147c478bd9Sstevel@tonic-gate 	else if (ctfp == DT_FUNC_CTFP(dtp) && type == DT_FUNC_TYPE(dtp))
3157c478bd9Sstevel@tonic-gate 		(void) snprintf(buf, len, "function");
3167c478bd9Sstevel@tonic-gate 	else if (ctfp == DT_DYN_CTFP(dtp) && type == DT_DYN_TYPE(dtp))
3177c478bd9Sstevel@tonic-gate 		(void) snprintf(buf, len, "dynamic variable");
3187c478bd9Sstevel@tonic-gate 	else if (ctfp == NULL)
3197c478bd9Sstevel@tonic-gate 		(void) snprintf(buf, len, "<none>");
3207c478bd9Sstevel@tonic-gate 	else if (ctf_type_name(ctfp, type, buf, len) == NULL)
3217c478bd9Sstevel@tonic-gate 		(void) snprintf(buf, len, "unknown");
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate 	return (buf);
3247c478bd9Sstevel@tonic-gate }
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate /*
3277c478bd9Sstevel@tonic-gate  * Perform the "usual arithmetic conversions" to determine which of the two
3287c478bd9Sstevel@tonic-gate  * input operand types should be promoted and used as a result type.  The
3297c478bd9Sstevel@tonic-gate  * rules for this are described in ISOC[6.3.1.8] and K&R[A6.5].
3307c478bd9Sstevel@tonic-gate  */
3317c478bd9Sstevel@tonic-gate static void
dt_type_promote(dt_node_t * lp,dt_node_t * rp,ctf_file_t ** ofp,ctf_id_t * otype)3327c478bd9Sstevel@tonic-gate dt_type_promote(dt_node_t *lp, dt_node_t *rp, ctf_file_t **ofp, ctf_id_t *otype)
3337c478bd9Sstevel@tonic-gate {
3347c478bd9Sstevel@tonic-gate 	ctf_file_t *lfp = lp->dn_ctfp;
3357c478bd9Sstevel@tonic-gate 	ctf_id_t ltype = lp->dn_type;
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate 	ctf_file_t *rfp = rp->dn_ctfp;
3387c478bd9Sstevel@tonic-gate 	ctf_id_t rtype = rp->dn_type;
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate 	ctf_id_t lbase = ctf_type_resolve(lfp, ltype);
3417c478bd9Sstevel@tonic-gate 	uint_t lkind = ctf_type_kind(lfp, lbase);
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate 	ctf_id_t rbase = ctf_type_resolve(rfp, rtype);
3447c478bd9Sstevel@tonic-gate 	uint_t rkind = ctf_type_kind(rfp, rbase);
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate 	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
3477c478bd9Sstevel@tonic-gate 	ctf_encoding_t le, re;
3487c478bd9Sstevel@tonic-gate 	uint_t lrank, rrank;
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate 	assert(lkind == CTF_K_INTEGER || lkind == CTF_K_ENUM);
3517c478bd9Sstevel@tonic-gate 	assert(rkind == CTF_K_INTEGER || rkind == CTF_K_ENUM);
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate 	if (lkind == CTF_K_ENUM) {
3547c478bd9Sstevel@tonic-gate 		lfp = DT_INT_CTFP(dtp);
3557c478bd9Sstevel@tonic-gate 		ltype = lbase = DT_INT_TYPE(dtp);
3567c478bd9Sstevel@tonic-gate 	}
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate 	if (rkind == CTF_K_ENUM) {
3597c478bd9Sstevel@tonic-gate 		rfp = DT_INT_CTFP(dtp);
3607c478bd9Sstevel@tonic-gate 		rtype = rbase = DT_INT_TYPE(dtp);
3617c478bd9Sstevel@tonic-gate 	}
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate 	if (ctf_type_encoding(lfp, lbase, &le) == CTF_ERR) {
3647c478bd9Sstevel@tonic-gate 		yypcb->pcb_hdl->dt_ctferr = ctf_errno(lfp);
3657c478bd9Sstevel@tonic-gate 		longjmp(yypcb->pcb_jmpbuf, EDT_CTF);
3667c478bd9Sstevel@tonic-gate 	}
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate 	if (ctf_type_encoding(rfp, rbase, &re) == CTF_ERR) {
3697c478bd9Sstevel@tonic-gate 		yypcb->pcb_hdl->dt_ctferr = ctf_errno(rfp);
3707c478bd9Sstevel@tonic-gate 		longjmp(yypcb->pcb_jmpbuf, EDT_CTF);
3717c478bd9Sstevel@tonic-gate 	}
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate 	/*
3747c478bd9Sstevel@tonic-gate 	 * Compute an integer rank based on the size and unsigned status.
3757c478bd9Sstevel@tonic-gate 	 * If rank is identical, pick the "larger" of the equivalent types
3767c478bd9Sstevel@tonic-gate 	 * which we define as having a larger base ctf_id_t.  If rank is
3777c478bd9Sstevel@tonic-gate 	 * different, pick the type with the greater rank.
3787c478bd9Sstevel@tonic-gate 	 */
3797c478bd9Sstevel@tonic-gate 	lrank = le.cte_bits + ((le.cte_format & CTF_INT_SIGNED) == 0);
3807c478bd9Sstevel@tonic-gate 	rrank = re.cte_bits + ((re.cte_format & CTF_INT_SIGNED) == 0);
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate 	if (lrank == rrank) {
3837c478bd9Sstevel@tonic-gate 		if (lbase - rbase < 0)
3847c478bd9Sstevel@tonic-gate 			goto return_rtype;
3857c478bd9Sstevel@tonic-gate 		else
3867c478bd9Sstevel@tonic-gate 			goto return_ltype;
3877c478bd9Sstevel@tonic-gate 	} else if (lrank > rrank) {
3887c478bd9Sstevel@tonic-gate 		goto return_ltype;
3897c478bd9Sstevel@tonic-gate 	} else
3907c478bd9Sstevel@tonic-gate 		goto return_rtype;
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate return_ltype:
3937c478bd9Sstevel@tonic-gate 	*ofp = lfp;
3947c478bd9Sstevel@tonic-gate 	*otype = ltype;
3957c478bd9Sstevel@tonic-gate 	return;
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate return_rtype:
3987c478bd9Sstevel@tonic-gate 	*ofp = rfp;
3997c478bd9Sstevel@tonic-gate 	*otype = rtype;
4007c478bd9Sstevel@tonic-gate }
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate void
dt_node_promote(dt_node_t * lp,dt_node_t * rp,dt_node_t * dnp)4037c478bd9Sstevel@tonic-gate dt_node_promote(dt_node_t *lp, dt_node_t *rp, dt_node_t *dnp)
4047c478bd9Sstevel@tonic-gate {
4057c478bd9Sstevel@tonic-gate 	dt_type_promote(lp, rp, &dnp->dn_ctfp, &dnp->dn_type);
406a386cc11SRobert Mustacchi 	dt_node_type_assign(dnp, dnp->dn_ctfp, dnp->dn_type, B_FALSE);
4077c478bd9Sstevel@tonic-gate 	dt_node_attr_assign(dnp, dt_attr_min(lp->dn_attr, rp->dn_attr));
4087c478bd9Sstevel@tonic-gate }
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate const char *
dt_node_name(const dt_node_t * dnp,char * buf,size_t len)4117c478bd9Sstevel@tonic-gate dt_node_name(const dt_node_t *dnp, char *buf, size_t len)
4127c478bd9Sstevel@tonic-gate {
4137c478bd9Sstevel@tonic-gate 	char n1[DT_TYPE_NAMELEN];
4147c478bd9Sstevel@tonic-gate 	char n2[DT_TYPE_NAMELEN];
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate 	const char *prefix = "", *suffix = "";
4177c478bd9Sstevel@tonic-gate 	const dtrace_syminfo_t *dts;
4187c478bd9Sstevel@tonic-gate 	char *s;
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate 	switch (dnp->dn_kind) {
4217c478bd9Sstevel@tonic-gate 	case DT_NODE_INT:
4227c478bd9Sstevel@tonic-gate 		(void) snprintf(buf, len, "integer constant 0x%llx",
4237c478bd9Sstevel@tonic-gate 		    (u_longlong_t)dnp->dn_value);
4247c478bd9Sstevel@tonic-gate 		break;
4257c478bd9Sstevel@tonic-gate 	case DT_NODE_STRING:
4267c478bd9Sstevel@tonic-gate 		s = strchr2esc(dnp->dn_string, strlen(dnp->dn_string));
4277c478bd9Sstevel@tonic-gate 		(void) snprintf(buf, len, "string constant \"%s\"",
4287c478bd9Sstevel@tonic-gate 		    s != NULL ? s : dnp->dn_string);
4297c478bd9Sstevel@tonic-gate 		free(s);
4307c478bd9Sstevel@tonic-gate 		break;
4317c478bd9Sstevel@tonic-gate 	case DT_NODE_IDENT:
4327c478bd9Sstevel@tonic-gate 		(void) snprintf(buf, len, "identifier %s", dnp->dn_string);
4337c478bd9Sstevel@tonic-gate 		break;
4347c478bd9Sstevel@tonic-gate 	case DT_NODE_VAR:
4357c478bd9Sstevel@tonic-gate 	case DT_NODE_FUNC:
4367c478bd9Sstevel@tonic-gate 	case DT_NODE_AGG:
4377c478bd9Sstevel@tonic-gate 	case DT_NODE_INLINE:
4387c478bd9Sstevel@tonic-gate 		switch (dnp->dn_ident->di_kind) {
4397c478bd9Sstevel@tonic-gate 		case DT_IDENT_FUNC:
4407c478bd9Sstevel@tonic-gate 		case DT_IDENT_AGGFUNC:
4417c478bd9Sstevel@tonic-gate 		case DT_IDENT_ACTFUNC:
4427c478bd9Sstevel@tonic-gate 			suffix = "( )";
4437c478bd9Sstevel@tonic-gate 			break;
4447c478bd9Sstevel@tonic-gate 		case DT_IDENT_AGG:
4457c478bd9Sstevel@tonic-gate 			prefix = "@";
4467c478bd9Sstevel@tonic-gate 			break;
4477c478bd9Sstevel@tonic-gate 		}
4487c478bd9Sstevel@tonic-gate 		(void) snprintf(buf, len, "%s %s%s%s",
4497c478bd9Sstevel@tonic-gate 		    dt_idkind_name(dnp->dn_ident->di_kind),
4507c478bd9Sstevel@tonic-gate 		    prefix, dnp->dn_ident->di_name, suffix);
4517c478bd9Sstevel@tonic-gate 		break;
4527c478bd9Sstevel@tonic-gate 	case DT_NODE_SYM:
4537c478bd9Sstevel@tonic-gate 		dts = dnp->dn_ident->di_data;
4547c478bd9Sstevel@tonic-gate 		(void) snprintf(buf, len, "symbol %s`%s",
4557c478bd9Sstevel@tonic-gate 		    dts->dts_object, dts->dts_name);
4567c478bd9Sstevel@tonic-gate 		break;
4577c478bd9Sstevel@tonic-gate 	case DT_NODE_TYPE:
4587c478bd9Sstevel@tonic-gate 		(void) snprintf(buf, len, "type %s",
4597c478bd9Sstevel@tonic-gate 		    dt_node_type_name(dnp, n1, sizeof (n1)));
4607c478bd9Sstevel@tonic-gate 		break;
4617c478bd9Sstevel@tonic-gate 	case DT_NODE_OP1:
4627c478bd9Sstevel@tonic-gate 	case DT_NODE_OP2:
4637c478bd9Sstevel@tonic-gate 	case DT_NODE_OP3:
4647c478bd9Sstevel@tonic-gate 		(void) snprintf(buf, len, "operator %s", opstr(dnp->dn_op));
4657c478bd9Sstevel@tonic-gate 		break;
4667c478bd9Sstevel@tonic-gate 	case DT_NODE_DEXPR:
4677c478bd9Sstevel@tonic-gate 	case DT_NODE_DFUNC:
4687c478bd9Sstevel@tonic-gate 		if (dnp->dn_expr)
4697c478bd9Sstevel@tonic-gate 			return (dt_node_name(dnp->dn_expr, buf, len));
4707c478bd9Sstevel@tonic-gate 		(void) snprintf(buf, len, "%s", "statement");
4717c478bd9Sstevel@tonic-gate 		break;
4727c478bd9Sstevel@tonic-gate 	case DT_NODE_PDESC:
4737c478bd9Sstevel@tonic-gate 		if (dnp->dn_desc->dtpd_id == 0) {
4747c478bd9Sstevel@tonic-gate 			(void) snprintf(buf, len,
4757c478bd9Sstevel@tonic-gate 			    "probe description %s:%s:%s:%s",
4767c478bd9Sstevel@tonic-gate 			    dnp->dn_desc->dtpd_provider, dnp->dn_desc->dtpd_mod,
4777c478bd9Sstevel@tonic-gate 			    dnp->dn_desc->dtpd_func, dnp->dn_desc->dtpd_name);
4787c478bd9Sstevel@tonic-gate 		} else {
4797c478bd9Sstevel@tonic-gate 			(void) snprintf(buf, len, "probe description %u",
4807c478bd9Sstevel@tonic-gate 			    dnp->dn_desc->dtpd_id);
4817c478bd9Sstevel@tonic-gate 		}
4827c478bd9Sstevel@tonic-gate 		break;
4837c478bd9Sstevel@tonic-gate 	case DT_NODE_CLAUSE:
4847c478bd9Sstevel@tonic-gate 		(void) snprintf(buf, len, "%s", "clause");
4857c478bd9Sstevel@tonic-gate 		break;
4867c478bd9Sstevel@tonic-gate 	case DT_NODE_MEMBER:
4877c478bd9Sstevel@tonic-gate 		(void) snprintf(buf, len, "member %s", dnp->dn_membname);
4887c478bd9Sstevel@tonic-gate 		break;
4897c478bd9Sstevel@tonic-gate 	case DT_NODE_XLATOR:
4907c478bd9Sstevel@tonic-gate 		(void) snprintf(buf, len, "translator <%s> (%s)",
4917c478bd9Sstevel@tonic-gate 		    dt_type_name(dnp->dn_xlator->dx_dst_ctfp,
4927c478bd9Sstevel@tonic-gate 		    dnp->dn_xlator->dx_dst_type, n1, sizeof (n1)),
4937c478bd9Sstevel@tonic-gate 		    dt_type_name(dnp->dn_xlator->dx_src_ctfp,
4947c478bd9Sstevel@tonic-gate 		    dnp->dn_xlator->dx_src_type, n2, sizeof (n2)));
4957c478bd9Sstevel@tonic-gate 		break;
4967c478bd9Sstevel@tonic-gate 	case DT_NODE_PROG:
4977c478bd9Sstevel@tonic-gate 		(void) snprintf(buf, len, "%s", "program");
4987c478bd9Sstevel@tonic-gate 		break;
4997c478bd9Sstevel@tonic-gate 	default:
5007c478bd9Sstevel@tonic-gate 		(void) snprintf(buf, len, "node <%u>", dnp->dn_kind);
5017c478bd9Sstevel@tonic-gate 		break;
5027c478bd9Sstevel@tonic-gate 	}
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate 	return (buf);
5057c478bd9Sstevel@tonic-gate }
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate /*
5087c478bd9Sstevel@tonic-gate  * dt_node_xalloc() can be used to create new parse nodes from any libdtrace
5097c478bd9Sstevel@tonic-gate  * caller.  The caller is responsible for assigning dn_link appropriately.
5107c478bd9Sstevel@tonic-gate  */
5117c478bd9Sstevel@tonic-gate dt_node_t *
dt_node_xalloc(dtrace_hdl_t * dtp,int kind)5127c478bd9Sstevel@tonic-gate dt_node_xalloc(dtrace_hdl_t *dtp, int kind)
5137c478bd9Sstevel@tonic-gate {
5147c478bd9Sstevel@tonic-gate 	dt_node_t *dnp = dt_alloc(dtp, sizeof (dt_node_t));
5157c478bd9Sstevel@tonic-gate 
5167c478bd9Sstevel@tonic-gate 	if (dnp == NULL)
5177c478bd9Sstevel@tonic-gate 		return (NULL);
5187c478bd9Sstevel@tonic-gate 
5197c478bd9Sstevel@tonic-gate 	dnp->dn_ctfp = NULL;
5207c478bd9Sstevel@tonic-gate 	dnp->dn_type = CTF_ERR;
521*6eeafb34SRobert Mustacchi 	dnp->dn_bitoff = 0;
5227c478bd9Sstevel@tonic-gate 	dnp->dn_kind = (uchar_t)kind;
5237c478bd9Sstevel@tonic-gate 	dnp->dn_flags = 0;
5247c478bd9Sstevel@tonic-gate 	dnp->dn_op = 0;
5257c478bd9Sstevel@tonic-gate 	dnp->dn_line = -1;
5267c478bd9Sstevel@tonic-gate 	dnp->dn_reg = -1;
5277c478bd9Sstevel@tonic-gate 	dnp->dn_attr = _dtrace_defattr;
5287c478bd9Sstevel@tonic-gate 	dnp->dn_list = NULL;
5297c478bd9Sstevel@tonic-gate 	dnp->dn_link = NULL;
5307c478bd9Sstevel@tonic-gate 	bzero(&dnp->dn_u, sizeof (dnp->dn_u));
5317c478bd9Sstevel@tonic-gate 
5327c478bd9Sstevel@tonic-gate 	return (dnp);
5337c478bd9Sstevel@tonic-gate }
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate /*
5367c478bd9Sstevel@tonic-gate  * dt_node_alloc() is used to create new parse nodes from the parser.  It
5377c478bd9Sstevel@tonic-gate  * assigns the node location based on the current lexer line number and places
5387c478bd9Sstevel@tonic-gate  * the new node on the default allocation list.  If allocation fails, we
5397c478bd9Sstevel@tonic-gate  * automatically longjmp the caller back to the enclosing compilation call.
5407c478bd9Sstevel@tonic-gate  */
5417c478bd9Sstevel@tonic-gate static dt_node_t *
dt_node_alloc(int kind)5427c478bd9Sstevel@tonic-gate dt_node_alloc(int kind)
5437c478bd9Sstevel@tonic-gate {
5447c478bd9Sstevel@tonic-gate 	dt_node_t *dnp = dt_node_xalloc(yypcb->pcb_hdl, kind);
5457c478bd9Sstevel@tonic-gate 
5467c478bd9Sstevel@tonic-gate 	if (dnp == NULL)
5477c478bd9Sstevel@tonic-gate 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
5487c478bd9Sstevel@tonic-gate 
5497c478bd9Sstevel@tonic-gate 	dnp->dn_line = yylineno;
5507c478bd9Sstevel@tonic-gate 	dnp->dn_link = yypcb->pcb_list;
5517c478bd9Sstevel@tonic-gate 	yypcb->pcb_list = dnp;
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate 	return (dnp);
5547c478bd9Sstevel@tonic-gate }
5557c478bd9Sstevel@tonic-gate 
5567c478bd9Sstevel@tonic-gate void
dt_node_free(dt_node_t * dnp)5577c478bd9Sstevel@tonic-gate dt_node_free(dt_node_t *dnp)
5587c478bd9Sstevel@tonic-gate {
5597c478bd9Sstevel@tonic-gate 	uchar_t kind = dnp->dn_kind;
5607c478bd9Sstevel@tonic-gate 
5617c478bd9Sstevel@tonic-gate 	dnp->dn_kind = DT_NODE_FREE;
5627c478bd9Sstevel@tonic-gate 
5637c478bd9Sstevel@tonic-gate 	switch (kind) {
5647c478bd9Sstevel@tonic-gate 	case DT_NODE_STRING:
5657c478bd9Sstevel@tonic-gate 	case DT_NODE_IDENT:
5667c478bd9Sstevel@tonic-gate 	case DT_NODE_TYPE:
5677c478bd9Sstevel@tonic-gate 		free(dnp->dn_string);
5687c478bd9Sstevel@tonic-gate 		dnp->dn_string = NULL;
5697c478bd9Sstevel@tonic-gate 		break;
5707c478bd9Sstevel@tonic-gate 
5717c478bd9Sstevel@tonic-gate 	case DT_NODE_VAR:
5727c478bd9Sstevel@tonic-gate 	case DT_NODE_FUNC:
5737c478bd9Sstevel@tonic-gate 	case DT_NODE_PROBE:
5747c478bd9Sstevel@tonic-gate 		if (dnp->dn_ident != NULL) {
5757c478bd9Sstevel@tonic-gate 			if (dnp->dn_ident->di_flags & DT_IDFLG_ORPHAN)
5767c478bd9Sstevel@tonic-gate 				dt_ident_destroy(dnp->dn_ident);
5777c478bd9Sstevel@tonic-gate 			dnp->dn_ident = NULL;
5787c478bd9Sstevel@tonic-gate 		}
5797c478bd9Sstevel@tonic-gate 		dt_node_list_free(&dnp->dn_args);
5807c478bd9Sstevel@tonic-gate 		break;
5817c478bd9Sstevel@tonic-gate 
5827c478bd9Sstevel@tonic-gate 	case DT_NODE_OP1:
5837c478bd9Sstevel@tonic-gate 		if (dnp->dn_child != NULL) {
5847c478bd9Sstevel@tonic-gate 			dt_node_free(dnp->dn_child);
5857c478bd9Sstevel@tonic-gate 			dnp->dn_child = NULL;
5867c478bd9Sstevel@tonic-gate 		}
5877c478bd9Sstevel@tonic-gate 		break;
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate 	case DT_NODE_OP3:
5907c478bd9Sstevel@tonic-gate 		if (dnp->dn_expr != NULL) {
5917c478bd9Sstevel@tonic-gate 			dt_node_free(dnp->dn_expr);
5927c478bd9Sstevel@tonic-gate 			dnp->dn_expr = NULL;
5937c478bd9Sstevel@tonic-gate 		}
5947c478bd9Sstevel@tonic-gate 		/*FALLTHRU*/
5957c478bd9Sstevel@tonic-gate 	case DT_NODE_OP2:
5967c478bd9Sstevel@tonic-gate 		if (dnp->dn_left != NULL) {
5977c478bd9Sstevel@tonic-gate 			dt_node_free(dnp->dn_left);
5987c478bd9Sstevel@tonic-gate 			dnp->dn_left = NULL;
5997c478bd9Sstevel@tonic-gate 		}
6007c478bd9Sstevel@tonic-gate 		if (dnp->dn_right != NULL) {
6017c478bd9Sstevel@tonic-gate 			dt_node_free(dnp->dn_right);
6027c478bd9Sstevel@tonic-gate 			dnp->dn_right = NULL;
6037c478bd9Sstevel@tonic-gate 		}
6047c478bd9Sstevel@tonic-gate 		break;
6057c478bd9Sstevel@tonic-gate 
6067c478bd9Sstevel@tonic-gate 	case DT_NODE_DEXPR:
6077c478bd9Sstevel@tonic-gate 	case DT_NODE_DFUNC:
6087c478bd9Sstevel@tonic-gate 		if (dnp->dn_expr != NULL) {
6097c478bd9Sstevel@tonic-gate 			dt_node_free(dnp->dn_expr);
6107c478bd9Sstevel@tonic-gate 			dnp->dn_expr = NULL;
6117c478bd9Sstevel@tonic-gate 		}
6127c478bd9Sstevel@tonic-gate 		break;
6137c478bd9Sstevel@tonic-gate 
6147c478bd9Sstevel@tonic-gate 	case DT_NODE_AGG:
6157c478bd9Sstevel@tonic-gate 		if (dnp->dn_aggfun != NULL) {
6167c478bd9Sstevel@tonic-gate 			dt_node_free(dnp->dn_aggfun);
6177c478bd9Sstevel@tonic-gate 			dnp->dn_aggfun = NULL;
6187c478bd9Sstevel@tonic-gate 		}
6197c478bd9Sstevel@tonic-gate 		dt_node_list_free(&dnp->dn_aggtup);
6207c478bd9Sstevel@tonic-gate 		break;
6217c478bd9Sstevel@tonic-gate 
6227c478bd9Sstevel@tonic-gate 	case DT_NODE_PDESC:
6237c478bd9Sstevel@tonic-gate 		free(dnp->dn_spec);
6247c478bd9Sstevel@tonic-gate 		dnp->dn_spec = NULL;
6257c478bd9Sstevel@tonic-gate 		free(dnp->dn_desc);
6267c478bd9Sstevel@tonic-gate 		dnp->dn_desc = NULL;
6277c478bd9Sstevel@tonic-gate 		break;
6287c478bd9Sstevel@tonic-gate 
6297c478bd9Sstevel@tonic-gate 	case DT_NODE_CLAUSE:
6307c478bd9Sstevel@tonic-gate 		if (dnp->dn_pred != NULL)
6317c478bd9Sstevel@tonic-gate 			dt_node_free(dnp->dn_pred);
6327c478bd9Sstevel@tonic-gate 		if (dnp->dn_locals != NULL)
6337c478bd9Sstevel@tonic-gate 			dt_idhash_destroy(dnp->dn_locals);
6347c478bd9Sstevel@tonic-gate 		dt_node_list_free(&dnp->dn_pdescs);
6357c478bd9Sstevel@tonic-gate 		dt_node_list_free(&dnp->dn_acts);
6367c478bd9Sstevel@tonic-gate 		break;
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate 	case DT_NODE_MEMBER:
6397c478bd9Sstevel@tonic-gate 		free(dnp->dn_membname);
6407c478bd9Sstevel@tonic-gate 		dnp->dn_membname = NULL;
6417c478bd9Sstevel@tonic-gate 		if (dnp->dn_membexpr != NULL) {
6427c478bd9Sstevel@tonic-gate 			dt_node_free(dnp->dn_membexpr);
6437c478bd9Sstevel@tonic-gate 			dnp->dn_membexpr = NULL;
6447c478bd9Sstevel@tonic-gate 		}
6457c478bd9Sstevel@tonic-gate 		break;
6467c478bd9Sstevel@tonic-gate 
6477c478bd9Sstevel@tonic-gate 	case DT_NODE_PROVIDER:
6487c478bd9Sstevel@tonic-gate 		dt_node_list_free(&dnp->dn_probes);
6497c478bd9Sstevel@tonic-gate 		free(dnp->dn_provname);
6507c478bd9Sstevel@tonic-gate 		dnp->dn_provname = NULL;
6517c478bd9Sstevel@tonic-gate 		break;
6527c478bd9Sstevel@tonic-gate 
6537c478bd9Sstevel@tonic-gate 	case DT_NODE_PROG:
6547c478bd9Sstevel@tonic-gate 		dt_node_list_free(&dnp->dn_list);
6557c478bd9Sstevel@tonic-gate 		break;
6567c478bd9Sstevel@tonic-gate 	}
6577c478bd9Sstevel@tonic-gate }
6587c478bd9Sstevel@tonic-gate 
6597c478bd9Sstevel@tonic-gate void
dt_node_attr_assign(dt_node_t * dnp,dtrace_attribute_t attr)6607c478bd9Sstevel@tonic-gate dt_node_attr_assign(dt_node_t *dnp, dtrace_attribute_t attr)
6617c478bd9Sstevel@tonic-gate {
6627c478bd9Sstevel@tonic-gate 	if ((yypcb->pcb_cflags & DTRACE_C_EATTR) &&
6637c478bd9Sstevel@tonic-gate 	    (dt_attr_cmp(attr, yypcb->pcb_amin) < 0)) {
6647c478bd9Sstevel@tonic-gate 		char a[DTRACE_ATTR2STR_MAX];
6657c478bd9Sstevel@tonic-gate 		char s[BUFSIZ];
6667c478bd9Sstevel@tonic-gate 
6677c478bd9Sstevel@tonic-gate 		dnerror(dnp, D_ATTR_MIN, "attributes for %s (%s) are less than "
6687c478bd9Sstevel@tonic-gate 		    "predefined minimum\n", dt_node_name(dnp, s, sizeof (s)),
6697c478bd9Sstevel@tonic-gate 		    dtrace_attr2str(attr, a, sizeof (a)));
6707c478bd9Sstevel@tonic-gate 	}
6717c478bd9Sstevel@tonic-gate 
6727c478bd9Sstevel@tonic-gate 	dnp->dn_attr = attr;
6737c478bd9Sstevel@tonic-gate }
6747c478bd9Sstevel@tonic-gate 
6757c478bd9Sstevel@tonic-gate void
dt_node_type_assign_member(dt_node_t * dnp,ctf_file_t * fp,ctf_id_t type,boolean_t user,ulong_t bitoff)676*6eeafb34SRobert Mustacchi dt_node_type_assign_member(dt_node_t *dnp, ctf_file_t *fp, ctf_id_t type,
677*6eeafb34SRobert Mustacchi     boolean_t user, ulong_t bitoff)
6787c478bd9Sstevel@tonic-gate {
6797c478bd9Sstevel@tonic-gate 	ctf_id_t base = ctf_type_resolve(fp, type);
6807c478bd9Sstevel@tonic-gate 	uint_t kind = ctf_type_kind(fp, base);
6817c478bd9Sstevel@tonic-gate 	ctf_encoding_t e;
6827c478bd9Sstevel@tonic-gate 
6837c478bd9Sstevel@tonic-gate 	dnp->dn_flags &=
6847c478bd9Sstevel@tonic-gate 	    ~(DT_NF_SIGNED | DT_NF_REF | DT_NF_BITFIELD | DT_NF_USERLAND);
6857c478bd9Sstevel@tonic-gate 
6867c478bd9Sstevel@tonic-gate 	if (kind == CTF_K_INTEGER && ctf_type_encoding(fp, base, &e) == 0) {
687*6eeafb34SRobert Mustacchi 		if (dt_is_bitfield(&e, bitoff))
6887c478bd9Sstevel@tonic-gate 			dnp->dn_flags |= DT_NF_BITFIELD;
6897c478bd9Sstevel@tonic-gate 
6907c478bd9Sstevel@tonic-gate 		if (e.cte_format & CTF_INT_SIGNED)
6917c478bd9Sstevel@tonic-gate 			dnp->dn_flags |= DT_NF_SIGNED;
6927c478bd9Sstevel@tonic-gate 	}
6937c478bd9Sstevel@tonic-gate 
6947c478bd9Sstevel@tonic-gate 	if (kind == CTF_K_FLOAT && ctf_type_encoding(fp, base, &e) == 0) {
6957c478bd9Sstevel@tonic-gate 		if (e.cte_bits / NBBY > sizeof (uint64_t))
6967c478bd9Sstevel@tonic-gate 			dnp->dn_flags |= DT_NF_REF;
6977c478bd9Sstevel@tonic-gate 	}
6987c478bd9Sstevel@tonic-gate 
6997c478bd9Sstevel@tonic-gate 	if (kind == CTF_K_STRUCT || kind == CTF_K_UNION ||
7007c478bd9Sstevel@tonic-gate 	    kind == CTF_K_FORWARD ||
7017c478bd9Sstevel@tonic-gate 	    kind == CTF_K_ARRAY || kind == CTF_K_FUNCTION)
7027c478bd9Sstevel@tonic-gate 		dnp->dn_flags |= DT_NF_REF;
7037c478bd9Sstevel@tonic-gate 	else if (yypcb != NULL && fp == DT_DYN_CTFP(yypcb->pcb_hdl) &&
7047c478bd9Sstevel@tonic-gate 	    type == DT_DYN_TYPE(yypcb->pcb_hdl))
7057c478bd9Sstevel@tonic-gate 		dnp->dn_flags |= DT_NF_REF;
7067c478bd9Sstevel@tonic-gate 
707a386cc11SRobert Mustacchi 	if (user)
708a386cc11SRobert Mustacchi 		dnp->dn_flags |= DT_NF_USERLAND;
709a386cc11SRobert Mustacchi 
7107c478bd9Sstevel@tonic-gate 	dnp->dn_flags |= DT_NF_COOKED;
7117c478bd9Sstevel@tonic-gate 	dnp->dn_ctfp = fp;
7127c478bd9Sstevel@tonic-gate 	dnp->dn_type = type;
713*6eeafb34SRobert Mustacchi 	dnp->dn_bitoff = bitoff;
714*6eeafb34SRobert Mustacchi }
715*6eeafb34SRobert Mustacchi 
716*6eeafb34SRobert Mustacchi 
717*6eeafb34SRobert Mustacchi void
dt_node_type_assign(dt_node_t * dnp,ctf_file_t * fp,ctf_id_t type,boolean_t user)718*6eeafb34SRobert Mustacchi dt_node_type_assign(dt_node_t *dnp, ctf_file_t *fp, ctf_id_t type,
719*6eeafb34SRobert Mustacchi     boolean_t user)
720*6eeafb34SRobert Mustacchi {
721*6eeafb34SRobert Mustacchi 	return (dt_node_type_assign_member(dnp, fp, type, user, 0));
7227c478bd9Sstevel@tonic-gate }
7237c478bd9Sstevel@tonic-gate 
7247c478bd9Sstevel@tonic-gate void
dt_node_type_propagate(const dt_node_t * src,dt_node_t * dst)7257c478bd9Sstevel@tonic-gate dt_node_type_propagate(const dt_node_t *src, dt_node_t *dst)
7267c478bd9Sstevel@tonic-gate {
7277c478bd9Sstevel@tonic-gate 	assert(src->dn_flags & DT_NF_COOKED);
7287c478bd9Sstevel@tonic-gate 	dst->dn_flags = src->dn_flags & ~DT_NF_LVALUE;
7297c478bd9Sstevel@tonic-gate 	dst->dn_ctfp = src->dn_ctfp;
7307c478bd9Sstevel@tonic-gate 	dst->dn_type = src->dn_type;
731*6eeafb34SRobert Mustacchi 	dst->dn_bitoff = src->dn_bitoff;
7327c478bd9Sstevel@tonic-gate }
7337c478bd9Sstevel@tonic-gate 
7347c478bd9Sstevel@tonic-gate const char *
dt_node_type_name(const dt_node_t * dnp,char * buf,size_t len)7357c478bd9Sstevel@tonic-gate dt_node_type_name(const dt_node_t *dnp, char *buf, size_t len)
7367c478bd9Sstevel@tonic-gate {
7377c478bd9Sstevel@tonic-gate 	if (dt_node_is_dynamic(dnp) && dnp->dn_ident != NULL) {
7387c478bd9Sstevel@tonic-gate 		(void) snprintf(buf, len, "%s",
7397c478bd9Sstevel@tonic-gate 		    dt_idkind_name(dt_ident_resolve(dnp->dn_ident)->di_kind));
7407c478bd9Sstevel@tonic-gate 		return (buf);
7417c478bd9Sstevel@tonic-gate 	}
7427c478bd9Sstevel@tonic-gate 
7437c478bd9Sstevel@tonic-gate 	if (dnp->dn_flags & DT_NF_USERLAND) {
7447c478bd9Sstevel@tonic-gate 		size_t n = snprintf(buf, len, "userland ");
7457c478bd9Sstevel@tonic-gate 		len = len > n ? len - n : 0;
7467c478bd9Sstevel@tonic-gate 		(void) dt_type_name(dnp->dn_ctfp, dnp->dn_type, buf + n, len);
7477c478bd9Sstevel@tonic-gate 		return (buf);
7487c478bd9Sstevel@tonic-gate 	}
7497c478bd9Sstevel@tonic-gate 
7507c478bd9Sstevel@tonic-gate 	return (dt_type_name(dnp->dn_ctfp, dnp->dn_type, buf, len));
7517c478bd9Sstevel@tonic-gate }
7527c478bd9Sstevel@tonic-gate 
7537c478bd9Sstevel@tonic-gate size_t
dt_node_type_size(const dt_node_t * dnp)7547c478bd9Sstevel@tonic-gate dt_node_type_size(const dt_node_t *dnp)
7557c478bd9Sstevel@tonic-gate {
756ec57c73dSBryan Cantrill 	ctf_id_t base;
757a386cc11SRobert Mustacchi 	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
758ec57c73dSBryan Cantrill 
7597c478bd9Sstevel@tonic-gate 	if (dnp->dn_kind == DT_NODE_STRING)
7607c478bd9Sstevel@tonic-gate 		return (strlen(dnp->dn_string) + 1);
7617c478bd9Sstevel@tonic-gate 
7627c478bd9Sstevel@tonic-gate 	if (dt_node_is_dynamic(dnp) && dnp->dn_ident != NULL)
7637c478bd9Sstevel@tonic-gate 		return (dt_ident_size(dnp->dn_ident));
7647c478bd9Sstevel@tonic-gate 
765ec57c73dSBryan Cantrill 	base = ctf_type_resolve(dnp->dn_ctfp, dnp->dn_type);
766ec57c73dSBryan Cantrill 
767ec57c73dSBryan Cantrill 	if (ctf_type_kind(dnp->dn_ctfp, base) == CTF_K_FORWARD)
768ec57c73dSBryan Cantrill 		return (0);
769ec57c73dSBryan Cantrill 
770a386cc11SRobert Mustacchi 	/*
771a386cc11SRobert Mustacchi 	 * Here we have a 32-bit user pointer that is being used with a 64-bit
772a386cc11SRobert Mustacchi 	 * kernel. When we're using it and its tagged as a userland reference --
773a386cc11SRobert Mustacchi 	 * then we need to keep it as a 32-bit pointer. However, if we are
774a386cc11SRobert Mustacchi 	 * referring to it as a kernel address, eg. being used after a copyin()
775a386cc11SRobert Mustacchi 	 * then we need to make sure that we actually return the kernel's size
776a386cc11SRobert Mustacchi 	 * of a pointer, 8 bytes.
777a386cc11SRobert Mustacchi 	 */
778a386cc11SRobert Mustacchi 	if (ctf_type_kind(dnp->dn_ctfp, base) == CTF_K_POINTER &&
779a386cc11SRobert Mustacchi 	    ctf_getmodel(dnp->dn_ctfp) == CTF_MODEL_ILP32 &&
780a386cc11SRobert Mustacchi 	    !(dnp->dn_flags & DT_NF_USERLAND) &&
781a386cc11SRobert Mustacchi 	    dtp->dt_conf.dtc_ctfmodel == CTF_MODEL_LP64)
782a386cc11SRobert Mustacchi 			return (8);
783a386cc11SRobert Mustacchi 
7847c478bd9Sstevel@tonic-gate 	return (ctf_type_size(dnp->dn_ctfp, dnp->dn_type));
7857c478bd9Sstevel@tonic-gate }
7867c478bd9Sstevel@tonic-gate 
7877c478bd9Sstevel@tonic-gate /*
7887c478bd9Sstevel@tonic-gate  * Determine if the specified parse tree node references an identifier of the
7897c478bd9Sstevel@tonic-gate  * specified kind, and if so return a pointer to it; otherwise return NULL.
7907c478bd9Sstevel@tonic-gate  * This function resolves the identifier itself, following through any inlines.
7917c478bd9Sstevel@tonic-gate  */
7927c478bd9Sstevel@tonic-gate dt_ident_t *
dt_node_resolve(const dt_node_t * dnp,uint_t idkind)7937c478bd9Sstevel@tonic-gate dt_node_resolve(const dt_node_t *dnp, uint_t idkind)
7947c478bd9Sstevel@tonic-gate {
7957c478bd9Sstevel@tonic-gate 	dt_ident_t *idp;
7967c478bd9Sstevel@tonic-gate 
7977c478bd9Sstevel@tonic-gate 	switch (dnp->dn_kind) {
7987c478bd9Sstevel@tonic-gate 	case DT_NODE_VAR:
7997c478bd9Sstevel@tonic-gate 	case DT_NODE_SYM:
8007c478bd9Sstevel@tonic-gate 	case DT_NODE_FUNC:
8017c478bd9Sstevel@tonic-gate 	case DT_NODE_AGG:
8027c478bd9Sstevel@tonic-gate 	case DT_NODE_INLINE:
8037c478bd9Sstevel@tonic-gate 	case DT_NODE_PROBE:
8047c478bd9Sstevel@tonic-gate 		idp = dt_ident_resolve(dnp->dn_ident);
8057c478bd9Sstevel@tonic-gate 		return (idp->di_kind == idkind ? idp : NULL);
8067c478bd9Sstevel@tonic-gate 	}
8077c478bd9Sstevel@tonic-gate 
8087c478bd9Sstevel@tonic-gate 	if (dt_node_is_dynamic(dnp)) {
8097c478bd9Sstevel@tonic-gate 		idp = dt_ident_resolve(dnp->dn_ident);
8107c478bd9Sstevel@tonic-gate 		return (idp->di_kind == idkind ? idp : NULL);
8117c478bd9Sstevel@tonic-gate 	}
8127c478bd9Sstevel@tonic-gate 
8137c478bd9Sstevel@tonic-gate 	return (NULL);
8147c478bd9Sstevel@tonic-gate }
8157c478bd9Sstevel@tonic-gate 
8167c478bd9Sstevel@tonic-gate size_t
dt_node_sizeof(const dt_node_t * dnp)8177c478bd9Sstevel@tonic-gate dt_node_sizeof(const dt_node_t *dnp)
8187c478bd9Sstevel@tonic-gate {
8197c478bd9Sstevel@tonic-gate 	dtrace_syminfo_t *sip;
8207c478bd9Sstevel@tonic-gate 	GElf_Sym sym;
8217c478bd9Sstevel@tonic-gate 	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
8227c478bd9Sstevel@tonic-gate 
8237c478bd9Sstevel@tonic-gate 	/*
8247c478bd9Sstevel@tonic-gate 	 * The size of the node as used for the sizeof() operator depends on
8257c478bd9Sstevel@tonic-gate 	 * the kind of the node.  If the node is a SYM, the size is obtained
8267c478bd9Sstevel@tonic-gate 	 * from the symbol table; if it is not a SYM, the size is determined
8277c478bd9Sstevel@tonic-gate 	 * from the node's type.  This is slightly different from C's sizeof()
8287c478bd9Sstevel@tonic-gate 	 * operator in that (for example) when applied to a function, sizeof()
8297c478bd9Sstevel@tonic-gate 	 * will evaluate to the length of the function rather than the size of
8307c478bd9Sstevel@tonic-gate 	 * the function type.
8317c478bd9Sstevel@tonic-gate 	 */
8327c478bd9Sstevel@tonic-gate 	if (dnp->dn_kind != DT_NODE_SYM)
8337c478bd9Sstevel@tonic-gate 		return (dt_node_type_size(dnp));
8347c478bd9Sstevel@tonic-gate 
8357c478bd9Sstevel@tonic-gate 	sip = dnp->dn_ident->di_data;
8367c478bd9Sstevel@tonic-gate 
8377c478bd9Sstevel@tonic-gate 	if (dtrace_lookup_by_name(dtp, sip->dts_object,
8387c478bd9Sstevel@tonic-gate 	    sip->dts_name, &sym, NULL) == -1)
8397c478bd9Sstevel@tonic-gate 		return (0);
8407c478bd9Sstevel@tonic-gate 
8417c478bd9Sstevel@tonic-gate 	return (sym.st_size);
8427c478bd9Sstevel@tonic-gate }
8437c478bd9Sstevel@tonic-gate 
8447c478bd9Sstevel@tonic-gate int
dt_node_is_integer(const dt_node_t * dnp)8457c478bd9Sstevel@tonic-gate dt_node_is_integer(const dt_node_t *dnp)
8467c478bd9Sstevel@tonic-gate {
8477c478bd9Sstevel@tonic-gate 	ctf_file_t *fp = dnp->dn_ctfp;
8487c478bd9Sstevel@tonic-gate 	ctf_encoding_t e;
8497c478bd9Sstevel@tonic-gate 	ctf_id_t type;
8507c478bd9Sstevel@tonic-gate 	uint_t kind;
8517c478bd9Sstevel@tonic-gate 
8527c478bd9Sstevel@tonic-gate 	assert(dnp->dn_flags & DT_NF_COOKED);
8537c478bd9Sstevel@tonic-gate 
8547c478bd9Sstevel@tonic-gate 	type = ctf_type_resolve(fp, dnp->dn_type);
8557c478bd9Sstevel@tonic-gate 	kind = ctf_type_kind(fp, type);
8567c478bd9Sstevel@tonic-gate 
8577c478bd9Sstevel@tonic-gate 	if (kind == CTF_K_INTEGER &&
8587c478bd9Sstevel@tonic-gate 	    ctf_type_encoding(fp, type, &e) == 0 && IS_VOID(e))
8597c478bd9Sstevel@tonic-gate 		return (0); /* void integer */
8607c478bd9Sstevel@tonic-gate 
8617c478bd9Sstevel@tonic-gate 	return (kind == CTF_K_INTEGER || kind == CTF_K_ENUM);
8627c478bd9Sstevel@tonic-gate }
8637c478bd9Sstevel@tonic-gate 
8647c478bd9Sstevel@tonic-gate int
dt_node_is_float(const dt_node_t * dnp)8657c478bd9Sstevel@tonic-gate dt_node_is_float(const dt_node_t *dnp)
8667c478bd9Sstevel@tonic-gate {
8677c478bd9Sstevel@tonic-gate 	ctf_file_t *fp = dnp->dn_ctfp;
8687c478bd9Sstevel@tonic-gate 	ctf_encoding_t e;
8697c478bd9Sstevel@tonic-gate 	ctf_id_t type;
8707c478bd9Sstevel@tonic-gate 	uint_t kind;
8717c478bd9Sstevel@tonic-gate 
8727c478bd9Sstevel@tonic-gate 	assert(dnp->dn_flags & DT_NF_COOKED);
8737c478bd9Sstevel@tonic-gate 
8747c478bd9Sstevel@tonic-gate 	type = ctf_type_resolve(fp, dnp->dn_type);
8757c478bd9Sstevel@tonic-gate 	kind = ctf_type_kind(fp, type);
8767c478bd9Sstevel@tonic-gate 
8777c478bd9Sstevel@tonic-gate 	return (kind == CTF_K_FLOAT &&
8787c478bd9Sstevel@tonic-gate 	    ctf_type_encoding(dnp->dn_ctfp, type, &e) == 0 && (
8797c478bd9Sstevel@tonic-gate 	    e.cte_format == CTF_FP_SINGLE || e.cte_format == CTF_FP_DOUBLE ||
8807c478bd9Sstevel@tonic-gate 	    e.cte_format == CTF_FP_LDOUBLE));
8817c478bd9Sstevel@tonic-gate }
8827c478bd9Sstevel@tonic-gate 
8837c478bd9Sstevel@tonic-gate int
dt_node_is_scalar(const dt_node_t * dnp)8847c478bd9Sstevel@tonic-gate dt_node_is_scalar(const dt_node_t *dnp)
8857c478bd9Sstevel@tonic-gate {
8867c478bd9Sstevel@tonic-gate 	ctf_file_t *fp = dnp->dn_ctfp;
8877c478bd9Sstevel@tonic-gate 	ctf_encoding_t e;
8887c478bd9Sstevel@tonic-gate 	ctf_id_t type;
8897c478bd9Sstevel@tonic-gate 	uint_t kind;
8907c478bd9Sstevel@tonic-gate 
8917c478bd9Sstevel@tonic-gate 	assert(dnp->dn_flags & DT_NF_COOKED);
8927c478bd9Sstevel@tonic-gate 
8937c478bd9Sstevel@tonic-gate 	type = ctf_type_resolve(fp, dnp->dn_type);
8947c478bd9Sstevel@tonic-gate 	kind = ctf_type_kind(fp, type);
8957c478bd9Sstevel@tonic-gate 
8967c478bd9Sstevel@tonic-gate 	if (kind == CTF_K_INTEGER &&
8977c478bd9Sstevel@tonic-gate 	    ctf_type_encoding(fp, type, &e) == 0 && IS_VOID(e))
8987c478bd9Sstevel@tonic-gate 		return (0); /* void cannot be used as a scalar */
8997c478bd9Sstevel@tonic-gate 
9007c478bd9Sstevel@tonic-gate 	return (kind == CTF_K_INTEGER || kind == CTF_K_ENUM ||
9017c478bd9Sstevel@tonic-gate 	    kind == CTF_K_POINTER);
9027c478bd9Sstevel@tonic-gate }
9037c478bd9Sstevel@tonic-gate 
9047c478bd9Sstevel@tonic-gate int
dt_node_is_arith(const dt_node_t * dnp)9057c478bd9Sstevel@tonic-gate dt_node_is_arith(const dt_node_t *dnp)
9067c478bd9Sstevel@tonic-gate {
9077c478bd9Sstevel@tonic-gate 	ctf_file_t *fp = dnp->dn_ctfp;
9087c478bd9Sstevel@tonic-gate 	ctf_encoding_t e;
9097c478bd9Sstevel@tonic-gate 	ctf_id_t type;
9107c478bd9Sstevel@tonic-gate 	uint_t kind;
9117c478bd9Sstevel@tonic-gate 
9127c478bd9Sstevel@tonic-gate 	assert(dnp->dn_flags & DT_NF_COOKED);
9137c478bd9Sstevel@tonic-gate 
9147c478bd9Sstevel@tonic-gate 	type = ctf_type_resolve(fp, dnp->dn_type);
9157c478bd9Sstevel@tonic-gate 	kind = ctf_type_kind(fp, type);
9167c478bd9Sstevel@tonic-gate 
9177c478bd9Sstevel@tonic-gate 	if (kind == CTF_K_INTEGER)
9187c478bd9Sstevel@tonic-gate 		return (ctf_type_encoding(fp, type, &e) == 0 && !IS_VOID(e));
9197c478bd9Sstevel@tonic-gate 	else
9207c478bd9Sstevel@tonic-gate 		return (kind == CTF_K_ENUM);
9217c478bd9Sstevel@tonic-gate }
9227c478bd9Sstevel@tonic-gate 
9237c478bd9Sstevel@tonic-gate int
dt_node_is_vfptr(const dt_node_t * dnp)9247c478bd9Sstevel@tonic-gate dt_node_is_vfptr(const dt_node_t *dnp)
9257c478bd9Sstevel@tonic-gate {
9267c478bd9Sstevel@tonic-gate 	ctf_file_t *fp = dnp->dn_ctfp;
9277c478bd9Sstevel@tonic-gate 	ctf_encoding_t e;
9287c478bd9Sstevel@tonic-gate 	ctf_id_t type;
9297c478bd9Sstevel@tonic-gate 	uint_t kind;
9307c478bd9Sstevel@tonic-gate 
9317c478bd9Sstevel@tonic-gate 	assert(dnp->dn_flags & DT_NF_COOKED);
9327c478bd9Sstevel@tonic-gate 
9337c478bd9Sstevel@tonic-gate 	type = ctf_type_resolve(fp, dnp->dn_type);
9347c478bd9Sstevel@tonic-gate 	if (ctf_type_kind(fp, type) != CTF_K_POINTER)
9357c478bd9Sstevel@tonic-gate 		return (0); /* type is not a pointer */
9367c478bd9Sstevel@tonic-gate 
9377c478bd9Sstevel@tonic-gate 	type = ctf_type_resolve(fp, ctf_type_reference(fp, type));
9387c478bd9Sstevel@tonic-gate 	kind = ctf_type_kind(fp, type);
9397c478bd9Sstevel@tonic-gate 
9407c478bd9Sstevel@tonic-gate 	return (kind == CTF_K_FUNCTION || (kind == CTF_K_INTEGER &&
9417c478bd9Sstevel@tonic-gate 	    ctf_type_encoding(fp, type, &e) == 0 && IS_VOID(e)));
9427c478bd9Sstevel@tonic-gate }
9437c478bd9Sstevel@tonic-gate 
9447c478bd9Sstevel@tonic-gate int
dt_node_is_dynamic(const dt_node_t * dnp)9457c478bd9Sstevel@tonic-gate dt_node_is_dynamic(const dt_node_t *dnp)
9467c478bd9Sstevel@tonic-gate {
9477c478bd9Sstevel@tonic-gate 	if (dnp->dn_kind == DT_NODE_VAR &&
9487c478bd9Sstevel@tonic-gate 	    (dnp->dn_ident->di_flags & DT_IDFLG_INLINE)) {
9497c478bd9Sstevel@tonic-gate 		const dt_idnode_t *inp = dnp->dn_ident->di_iarg;
9501a7c1b72Smws 		return (inp->din_root ? dt_node_is_dynamic(inp->din_root) : 0);
9517c478bd9Sstevel@tonic-gate 	}
9527c478bd9Sstevel@tonic-gate 
9537c478bd9Sstevel@tonic-gate 	return (dnp->dn_ctfp == DT_DYN_CTFP(yypcb->pcb_hdl) &&
9547c478bd9Sstevel@tonic-gate 	    dnp->dn_type == DT_DYN_TYPE(yypcb->pcb_hdl));
9557c478bd9Sstevel@tonic-gate }
9567c478bd9Sstevel@tonic-gate 
9577c478bd9Sstevel@tonic-gate int
dt_node_is_string(const dt_node_t * dnp)9587c478bd9Sstevel@tonic-gate dt_node_is_string(const dt_node_t *dnp)
9597c478bd9Sstevel@tonic-gate {
9607c478bd9Sstevel@tonic-gate 	return (dnp->dn_ctfp == DT_STR_CTFP(yypcb->pcb_hdl) &&
9617c478bd9Sstevel@tonic-gate 	    dnp->dn_type == DT_STR_TYPE(yypcb->pcb_hdl));
9627c478bd9Sstevel@tonic-gate }
9637c478bd9Sstevel@tonic-gate 
9647c478bd9Sstevel@tonic-gate int
dt_node_is_stack(const dt_node_t * dnp)9657c478bd9Sstevel@tonic-gate dt_node_is_stack(const dt_node_t *dnp)
9667c478bd9Sstevel@tonic-gate {
9677c478bd9Sstevel@tonic-gate 	return (dnp->dn_ctfp == DT_STACK_CTFP(yypcb->pcb_hdl) &&
9687c478bd9Sstevel@tonic-gate 	    dnp->dn_type == DT_STACK_TYPE(yypcb->pcb_hdl));
9697c478bd9Sstevel@tonic-gate }
9707c478bd9Sstevel@tonic-gate 
9717c478bd9Sstevel@tonic-gate int
dt_node_is_symaddr(const dt_node_t * dnp)972a1b5e537Sbmc dt_node_is_symaddr(const dt_node_t *dnp)
973a1b5e537Sbmc {
974a1b5e537Sbmc 	return (dnp->dn_ctfp == DT_SYMADDR_CTFP(yypcb->pcb_hdl) &&
975a1b5e537Sbmc 	    dnp->dn_type == DT_SYMADDR_TYPE(yypcb->pcb_hdl));
976a1b5e537Sbmc }
977a1b5e537Sbmc 
978a1b5e537Sbmc int
dt_node_is_usymaddr(const dt_node_t * dnp)979a1b5e537Sbmc dt_node_is_usymaddr(const dt_node_t *dnp)
980a1b5e537Sbmc {
981a1b5e537Sbmc 	return (dnp->dn_ctfp == DT_USYMADDR_CTFP(yypcb->pcb_hdl) &&
982a1b5e537Sbmc 	    dnp->dn_type == DT_USYMADDR_TYPE(yypcb->pcb_hdl));
983a1b5e537Sbmc }
984a1b5e537Sbmc 
985a1b5e537Sbmc int
dt_node_is_strcompat(const dt_node_t * dnp)9867c478bd9Sstevel@tonic-gate dt_node_is_strcompat(const dt_node_t *dnp)
9877c478bd9Sstevel@tonic-gate {
9887c478bd9Sstevel@tonic-gate 	ctf_file_t *fp = dnp->dn_ctfp;
9897c478bd9Sstevel@tonic-gate 	ctf_encoding_t e;
9907c478bd9Sstevel@tonic-gate 	ctf_arinfo_t r;
9917c478bd9Sstevel@tonic-gate 	ctf_id_t base;
9927c478bd9Sstevel@tonic-gate 	uint_t kind;
9937c478bd9Sstevel@tonic-gate 
9947c478bd9Sstevel@tonic-gate 	assert(dnp->dn_flags & DT_NF_COOKED);
9957c478bd9Sstevel@tonic-gate 
9967c478bd9Sstevel@tonic-gate 	base = ctf_type_resolve(fp, dnp->dn_type);
9977c478bd9Sstevel@tonic-gate 	kind = ctf_type_kind(fp, base);
9987c478bd9Sstevel@tonic-gate 
9997c478bd9Sstevel@tonic-gate 	if (kind == CTF_K_POINTER &&
10007c478bd9Sstevel@tonic-gate 	    (base = ctf_type_reference(fp, base)) != CTF_ERR &&
10017c478bd9Sstevel@tonic-gate 	    (base = ctf_type_resolve(fp, base)) != CTF_ERR &&
10027c478bd9Sstevel@tonic-gate 	    ctf_type_encoding(fp, base, &e) == 0 && IS_CHAR(e))
10037c478bd9Sstevel@tonic-gate 		return (1); /* promote char pointer to string */
10047c478bd9Sstevel@tonic-gate 
10057c478bd9Sstevel@tonic-gate 	if (kind == CTF_K_ARRAY && ctf_array_info(fp, base, &r) == 0 &&
10067c478bd9Sstevel@tonic-gate 	    (base = ctf_type_resolve(fp, r.ctr_contents)) != CTF_ERR &&
10077c478bd9Sstevel@tonic-gate 	    ctf_type_encoding(fp, base, &e) == 0 && IS_CHAR(e))
10087c478bd9Sstevel@tonic-gate 		return (1); /* promote char array to string */
10097c478bd9Sstevel@tonic-gate 
10107c478bd9Sstevel@tonic-gate 	return (0);
10117c478bd9Sstevel@tonic-gate }
10127c478bd9Sstevel@tonic-gate 
10137c478bd9Sstevel@tonic-gate int
dt_node_is_pointer(const dt_node_t * dnp)10147c478bd9Sstevel@tonic-gate dt_node_is_pointer(const dt_node_t *dnp)
10157c478bd9Sstevel@tonic-gate {
10167c478bd9Sstevel@tonic-gate 	ctf_file_t *fp = dnp->dn_ctfp;
10177c478bd9Sstevel@tonic-gate 	uint_t kind;
10187c478bd9Sstevel@tonic-gate 
10197c478bd9Sstevel@tonic-gate 	assert(dnp->dn_flags & DT_NF_COOKED);
10207c478bd9Sstevel@tonic-gate 
10217c478bd9Sstevel@tonic-gate 	if (dt_node_is_string(dnp))
10227c478bd9Sstevel@tonic-gate 		return (0); /* string are pass-by-ref but act like structs */
10237c478bd9Sstevel@tonic-gate 
10247c478bd9Sstevel@tonic-gate 	kind = ctf_type_kind(fp, ctf_type_resolve(fp, dnp->dn_type));
10257c478bd9Sstevel@tonic-gate 	return (kind == CTF_K_POINTER || kind == CTF_K_ARRAY);
10267c478bd9Sstevel@tonic-gate }
10277c478bd9Sstevel@tonic-gate 
10287c478bd9Sstevel@tonic-gate int
dt_node_is_void(const dt_node_t * dnp)10297c478bd9Sstevel@tonic-gate dt_node_is_void(const dt_node_t *dnp)
10307c478bd9Sstevel@tonic-gate {
10317c478bd9Sstevel@tonic-gate 	ctf_file_t *fp = dnp->dn_ctfp;
10327c478bd9Sstevel@tonic-gate 	ctf_encoding_t e;
10337c478bd9Sstevel@tonic-gate 	ctf_id_t type;
10347c478bd9Sstevel@tonic-gate 
10357c478bd9Sstevel@tonic-gate 	if (dt_node_is_dynamic(dnp))
10367c478bd9Sstevel@tonic-gate 		return (0); /* <DYN> is an alias for void but not the same */
10377c478bd9Sstevel@tonic-gate 
10387c478bd9Sstevel@tonic-gate 	if (dt_node_is_stack(dnp))
10397c478bd9Sstevel@tonic-gate 		return (0);
10407c478bd9Sstevel@tonic-gate 
1041a1b5e537Sbmc 	if (dt_node_is_symaddr(dnp) || dt_node_is_usymaddr(dnp))
1042a1b5e537Sbmc 		return (0);
1043a1b5e537Sbmc 
10447c478bd9Sstevel@tonic-gate 	type = ctf_type_resolve(fp, dnp->dn_type);
10457c478bd9Sstevel@tonic-gate 
10467c478bd9Sstevel@tonic-gate 	return (ctf_type_kind(fp, type) == CTF_K_INTEGER &&
10477c478bd9Sstevel@tonic-gate 	    ctf_type_encoding(fp, type, &e) == 0 && IS_VOID(e));
10487c478bd9Sstevel@tonic-gate }
10497c478bd9Sstevel@tonic-gate 
10507c478bd9Sstevel@tonic-gate int
dt_node_is_ptrcompat(const dt_node_t * lp,const dt_node_t * rp,ctf_file_t ** fpp,ctf_id_t * tp)10517c478bd9Sstevel@tonic-gate dt_node_is_ptrcompat(const dt_node_t *lp, const dt_node_t *rp,
10527c478bd9Sstevel@tonic-gate     ctf_file_t **fpp, ctf_id_t *tp)
10537c478bd9Sstevel@tonic-gate {
10547c478bd9Sstevel@tonic-gate 	ctf_file_t *lfp = lp->dn_ctfp;
10557c478bd9Sstevel@tonic-gate 	ctf_file_t *rfp = rp->dn_ctfp;
10567c478bd9Sstevel@tonic-gate 
10577c478bd9Sstevel@tonic-gate 	ctf_id_t lbase = CTF_ERR, rbase = CTF_ERR;
10587c478bd9Sstevel@tonic-gate 	ctf_id_t lref = CTF_ERR, rref = CTF_ERR;
10597c478bd9Sstevel@tonic-gate 
10607c478bd9Sstevel@tonic-gate 	int lp_is_void, rp_is_void, lp_is_int, rp_is_int, compat;
10617c478bd9Sstevel@tonic-gate 	uint_t lkind, rkind;
10627c478bd9Sstevel@tonic-gate 	ctf_encoding_t e;
10637c478bd9Sstevel@tonic-gate 	ctf_arinfo_t r;
10647c478bd9Sstevel@tonic-gate 
10657c478bd9Sstevel@tonic-gate 	assert(lp->dn_flags & DT_NF_COOKED);
10667c478bd9Sstevel@tonic-gate 	assert(rp->dn_flags & DT_NF_COOKED);
10677c478bd9Sstevel@tonic-gate 
10687c478bd9Sstevel@tonic-gate 	if (dt_node_is_dynamic(lp) || dt_node_is_dynamic(rp))
10697c478bd9Sstevel@tonic-gate 		return (0); /* fail if either node is a dynamic variable */
10707c478bd9Sstevel@tonic-gate 
10717c478bd9Sstevel@tonic-gate 	lp_is_int = dt_node_is_integer(lp);
10727c478bd9Sstevel@tonic-gate 	rp_is_int = dt_node_is_integer(rp);
10737c478bd9Sstevel@tonic-gate 
10747c478bd9Sstevel@tonic-gate 	if (lp_is_int && rp_is_int)
10757c478bd9Sstevel@tonic-gate 		return (0); /* fail if both nodes are integers */
10767c478bd9Sstevel@tonic-gate 
10777c478bd9Sstevel@tonic-gate 	if (lp_is_int && (lp->dn_kind != DT_NODE_INT || lp->dn_value != 0))
10787c478bd9Sstevel@tonic-gate 		return (0); /* fail if lp is an integer that isn't 0 constant */
10797c478bd9Sstevel@tonic-gate 
10807c478bd9Sstevel@tonic-gate 	if (rp_is_int && (rp->dn_kind != DT_NODE_INT || rp->dn_value != 0))
10817c478bd9Sstevel@tonic-gate 		return (0); /* fail if rp is an integer that isn't 0 constant */
10827c478bd9Sstevel@tonic-gate 
10837c478bd9Sstevel@tonic-gate 	if ((lp_is_int == 0 && rp_is_int == 0) && (
10847c478bd9Sstevel@tonic-gate 	    (lp->dn_flags & DT_NF_USERLAND) ^ (rp->dn_flags & DT_NF_USERLAND)))
10857c478bd9Sstevel@tonic-gate 		return (0); /* fail if only one pointer is a userland address */
10867c478bd9Sstevel@tonic-gate 
10877c478bd9Sstevel@tonic-gate 	/*
10887c478bd9Sstevel@tonic-gate 	 * Resolve the left-hand and right-hand types to their base type, and
10897c478bd9Sstevel@tonic-gate 	 * then resolve the referenced type as well (assuming the base type
10907c478bd9Sstevel@tonic-gate 	 * is CTF_K_POINTER or CTF_K_ARRAY).  Otherwise [lr]ref = CTF_ERR.
10917c478bd9Sstevel@tonic-gate 	 */
10927c478bd9Sstevel@tonic-gate 	if (!lp_is_int) {
10937c478bd9Sstevel@tonic-gate 		lbase = ctf_type_resolve(lfp, lp->dn_type);
10947c478bd9Sstevel@tonic-gate 		lkind = ctf_type_kind(lfp, lbase);
10957c478bd9Sstevel@tonic-gate 
10967c478bd9Sstevel@tonic-gate 		if (lkind == CTF_K_POINTER) {
10977c478bd9Sstevel@tonic-gate 			lref = ctf_type_resolve(lfp,
10987c478bd9Sstevel@tonic-gate 			    ctf_type_reference(lfp, lbase));
10997c478bd9Sstevel@tonic-gate 		} else if (lkind == CTF_K_ARRAY &&
11007c478bd9Sstevel@tonic-gate 		    ctf_array_info(lfp, lbase, &r) == 0) {
11017c478bd9Sstevel@tonic-gate 			lref = ctf_type_resolve(lfp, r.ctr_contents);
11027c478bd9Sstevel@tonic-gate 		}
11037c478bd9Sstevel@tonic-gate 	}
11047c478bd9Sstevel@tonic-gate 
11057c478bd9Sstevel@tonic-gate 	if (!rp_is_int) {
11067c478bd9Sstevel@tonic-gate 		rbase = ctf_type_resolve(rfp, rp->dn_type);
11077c478bd9Sstevel@tonic-gate 		rkind = ctf_type_kind(rfp, rbase);
11087c478bd9Sstevel@tonic-gate 
11097c478bd9Sstevel@tonic-gate 		if (rkind == CTF_K_POINTER) {
11107c478bd9Sstevel@tonic-gate 			rref = ctf_type_resolve(rfp,
11117c478bd9Sstevel@tonic-gate 			    ctf_type_reference(rfp, rbase));
11127c478bd9Sstevel@tonic-gate 		} else if (rkind == CTF_K_ARRAY &&
11137c478bd9Sstevel@tonic-gate 		    ctf_array_info(rfp, rbase, &r) == 0) {
11147c478bd9Sstevel@tonic-gate 			rref = ctf_type_resolve(rfp, r.ctr_contents);
11157c478bd9Sstevel@tonic-gate 		}
11167c478bd9Sstevel@tonic-gate 	}
11177c478bd9Sstevel@tonic-gate 
11187c478bd9Sstevel@tonic-gate 	/*
11197c478bd9Sstevel@tonic-gate 	 * We know that one or the other type may still be a zero-valued
11207c478bd9Sstevel@tonic-gate 	 * integer constant.  To simplify the code below, set the integer
11217c478bd9Sstevel@tonic-gate 	 * type variables equal to the non-integer types and proceed.
11227c478bd9Sstevel@tonic-gate 	 */
11237c478bd9Sstevel@tonic-gate 	if (lp_is_int) {
11247c478bd9Sstevel@tonic-gate 		lbase = rbase;
11257c478bd9Sstevel@tonic-gate 		lkind = rkind;
11267c478bd9Sstevel@tonic-gate 		lref = rref;
11277c478bd9Sstevel@tonic-gate 		lfp = rfp;
11287c478bd9Sstevel@tonic-gate 	} else if (rp_is_int) {
11297c478bd9Sstevel@tonic-gate 		rbase = lbase;
11307c478bd9Sstevel@tonic-gate 		rkind = lkind;
11317c478bd9Sstevel@tonic-gate 		rref = lref;
11327c478bd9Sstevel@tonic-gate 		rfp = lfp;
11337c478bd9Sstevel@tonic-gate 	}
11347c478bd9Sstevel@tonic-gate 
11357c478bd9Sstevel@tonic-gate 	lp_is_void = ctf_type_encoding(lfp, lref, &e) == 0 && IS_VOID(e);
11367c478bd9Sstevel@tonic-gate 	rp_is_void = ctf_type_encoding(rfp, rref, &e) == 0 && IS_VOID(e);
11377c478bd9Sstevel@tonic-gate 
11387c478bd9Sstevel@tonic-gate 	/*
11397c478bd9Sstevel@tonic-gate 	 * The types are compatible if both are pointers to the same type, or
11407c478bd9Sstevel@tonic-gate 	 * if either pointer is a void pointer.  If they are compatible, set
11417c478bd9Sstevel@tonic-gate 	 * tp to point to the more specific pointer type and return it.
11427c478bd9Sstevel@tonic-gate 	 */
11437c478bd9Sstevel@tonic-gate 	compat = (lkind == CTF_K_POINTER || lkind == CTF_K_ARRAY) &&
11447c478bd9Sstevel@tonic-gate 	    (rkind == CTF_K_POINTER || rkind == CTF_K_ARRAY) &&
11457c478bd9Sstevel@tonic-gate 	    (lp_is_void || rp_is_void || ctf_type_compat(lfp, lref, rfp, rref));
11467c478bd9Sstevel@tonic-gate 
11477c478bd9Sstevel@tonic-gate 	if (compat) {
11487c478bd9Sstevel@tonic-gate 		if (fpp != NULL)
11497c478bd9Sstevel@tonic-gate 			*fpp = rp_is_void ? lfp : rfp;
11507c478bd9Sstevel@tonic-gate 		if (tp != NULL)
11517c478bd9Sstevel@tonic-gate 			*tp = rp_is_void ? lbase : rbase;
11527c478bd9Sstevel@tonic-gate 	}
11537c478bd9Sstevel@tonic-gate 
11547c478bd9Sstevel@tonic-gate 	return (compat);
11557c478bd9Sstevel@tonic-gate }
11567c478bd9Sstevel@tonic-gate 
11577c478bd9Sstevel@tonic-gate /*
11587c478bd9Sstevel@tonic-gate  * The rules for checking argument types against parameter types are described
11597c478bd9Sstevel@tonic-gate  * in the ANSI-C spec (see K&R[A7.3.2] and K&R[A7.17]).  We use the same rule
11607c478bd9Sstevel@tonic-gate  * set to determine whether associative array arguments match the prototype.
11617c478bd9Sstevel@tonic-gate  */
11627c478bd9Sstevel@tonic-gate int
dt_node_is_argcompat(const dt_node_t * lp,const dt_node_t * rp)11637c478bd9Sstevel@tonic-gate dt_node_is_argcompat(const dt_node_t *lp, const dt_node_t *rp)
11647c478bd9Sstevel@tonic-gate {
11657c478bd9Sstevel@tonic-gate 	ctf_file_t *lfp = lp->dn_ctfp;
11667c478bd9Sstevel@tonic-gate 	ctf_file_t *rfp = rp->dn_ctfp;
11677c478bd9Sstevel@tonic-gate 
11687c478bd9Sstevel@tonic-gate 	assert(lp->dn_flags & DT_NF_COOKED);
11697c478bd9Sstevel@tonic-gate 	assert(rp->dn_flags & DT_NF_COOKED);
11707c478bd9Sstevel@tonic-gate 
11717c478bd9Sstevel@tonic-gate 	if (dt_node_is_integer(lp) && dt_node_is_integer(rp))
11727c478bd9Sstevel@tonic-gate 		return (1); /* integer types are compatible */
11737c478bd9Sstevel@tonic-gate 
11747c478bd9Sstevel@tonic-gate 	if (dt_node_is_strcompat(lp) && dt_node_is_strcompat(rp))
11757c478bd9Sstevel@tonic-gate 		return (1); /* string types are compatible */
11767c478bd9Sstevel@tonic-gate 
11777c478bd9Sstevel@tonic-gate 	if (dt_node_is_stack(lp) && dt_node_is_stack(rp))
11787c478bd9Sstevel@tonic-gate 		return (1); /* stack types are compatible */
11797c478bd9Sstevel@tonic-gate 
1180a1b5e537Sbmc 	if (dt_node_is_symaddr(lp) && dt_node_is_symaddr(rp))
1181a1b5e537Sbmc 		return (1); /* symaddr types are compatible */
1182a1b5e537Sbmc 
1183a1b5e537Sbmc 	if (dt_node_is_usymaddr(lp) && dt_node_is_usymaddr(rp))
1184a1b5e537Sbmc 		return (1); /* usymaddr types are compatible */
1185a1b5e537Sbmc 
11867c478bd9Sstevel@tonic-gate 	switch (ctf_type_kind(lfp, ctf_type_resolve(lfp, lp->dn_type))) {
11877c478bd9Sstevel@tonic-gate 	case CTF_K_FUNCTION:
11887c478bd9Sstevel@tonic-gate 	case CTF_K_STRUCT:
11897c478bd9Sstevel@tonic-gate 	case CTF_K_UNION:
11907c478bd9Sstevel@tonic-gate 		return (ctf_type_compat(lfp, lp->dn_type, rfp, rp->dn_type));
11917c478bd9Sstevel@tonic-gate 	default:
11927c478bd9Sstevel@tonic-gate 		return (dt_node_is_ptrcompat(lp, rp, NULL, NULL));
11937c478bd9Sstevel@tonic-gate 	}
11947c478bd9Sstevel@tonic-gate }
11957c478bd9Sstevel@tonic-gate 
11967c478bd9Sstevel@tonic-gate /*
11977c478bd9Sstevel@tonic-gate  * We provide dt_node_is_posconst() as a convenience routine for callers who
11987c478bd9Sstevel@tonic-gate  * wish to verify that an argument is a positive non-zero integer constant.
11997c478bd9Sstevel@tonic-gate  */
12007c478bd9Sstevel@tonic-gate int
dt_node_is_posconst(const dt_node_t * dnp)12017c478bd9Sstevel@tonic-gate dt_node_is_posconst(const dt_node_t *dnp)
12027c478bd9Sstevel@tonic-gate {
12037c478bd9Sstevel@tonic-gate 	return (dnp->dn_kind == DT_NODE_INT && dnp->dn_value != 0 && (
12047c478bd9Sstevel@tonic-gate 	    (dnp->dn_flags & DT_NF_SIGNED) == 0 || (int64_t)dnp->dn_value > 0));
12057c478bd9Sstevel@tonic-gate }
12067c478bd9Sstevel@tonic-gate 
12077c478bd9Sstevel@tonic-gate int
dt_node_is_actfunc(const dt_node_t * dnp)12087c478bd9Sstevel@tonic-gate dt_node_is_actfunc(const dt_node_t *dnp)
12097c478bd9Sstevel@tonic-gate {
12107c478bd9Sstevel@tonic-gate 	return (dnp->dn_kind == DT_NODE_FUNC &&
12117c478bd9Sstevel@tonic-gate 	    dnp->dn_ident->di_kind == DT_IDENT_ACTFUNC);
12127c478bd9Sstevel@tonic-gate }
12137c478bd9Sstevel@tonic-gate 
12147c478bd9Sstevel@tonic-gate /*
12157c478bd9Sstevel@tonic-gate  * The original rules for integer constant typing are described in K&R[A2.5.1].
12167c478bd9Sstevel@tonic-gate  * However, since we support long long, we instead use the rules from ISO C99
12177c478bd9Sstevel@tonic-gate  * clause 6.4.4.1 since that is where long longs are formally described.  The
12187c478bd9Sstevel@tonic-gate  * rules require us to know whether the constant was specified in decimal or
12197c478bd9Sstevel@tonic-gate  * in octal or hex, which we do by looking at our lexer's 'yyintdecimal' flag.
12207c478bd9Sstevel@tonic-gate  * The type of an integer constant is the first of the corresponding list in
12217c478bd9Sstevel@tonic-gate  * which its value can be represented:
12227c478bd9Sstevel@tonic-gate  *
12237c478bd9Sstevel@tonic-gate  * unsuffixed decimal:   int, long, long long
12247c478bd9Sstevel@tonic-gate  * unsuffixed oct/hex:   int, unsigned int, long, unsigned long,
12257c478bd9Sstevel@tonic-gate  *                       long long, unsigned long long
12267c478bd9Sstevel@tonic-gate  * suffix [uU]:          unsigned int, unsigned long, unsigned long long
12277c478bd9Sstevel@tonic-gate  * suffix [lL] decimal:  long, long long
12287c478bd9Sstevel@tonic-gate  * suffix [lL] oct/hex:  long, unsigned long, long long, unsigned long long
12297c478bd9Sstevel@tonic-gate  * suffix [uU][Ll]:      unsigned long, unsigned long long
12307c478bd9Sstevel@tonic-gate  * suffix ll/LL decimal: long long
12317c478bd9Sstevel@tonic-gate  * suffix ll/LL oct/hex: long long, unsigned long long
12327c478bd9Sstevel@tonic-gate  * suffix [uU][ll/LL]:   unsigned long long
12337c478bd9Sstevel@tonic-gate  *
12347c478bd9Sstevel@tonic-gate  * Given that our lexer has already validated the suffixes by regexp matching,
12357c478bd9Sstevel@tonic-gate  * there is an obvious way to concisely encode these rules: construct an array
12367c478bd9Sstevel@tonic-gate  * of the types in the order int, unsigned int, long, unsigned long, long long,
12377c478bd9Sstevel@tonic-gate  * unsigned long long.  Compute an integer array starting index based on the
12387c478bd9Sstevel@tonic-gate  * suffix (e.g. none = 0, u = 1, ull = 5), and compute an increment based on
12397c478bd9Sstevel@tonic-gate  * the specifier (dec/oct/hex) and suffix (u).  Then iterate from the starting
12407c478bd9Sstevel@tonic-gate  * index to the end, advancing using the increment, and searching until we
12417c478bd9Sstevel@tonic-gate  * find a limit that matches or we run out of choices (overflow).  To make it
12427c478bd9Sstevel@tonic-gate  * even faster, we precompute the table of type information in dtrace_open().
12437c478bd9Sstevel@tonic-gate  */
12447c478bd9Sstevel@tonic-gate dt_node_t *
dt_node_int(uintmax_t value)12457c478bd9Sstevel@tonic-gate dt_node_int(uintmax_t value)
12467c478bd9Sstevel@tonic-gate {
12477c478bd9Sstevel@tonic-gate 	dt_node_t *dnp = dt_node_alloc(DT_NODE_INT);
12487c478bd9Sstevel@tonic-gate 	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
12497c478bd9Sstevel@tonic-gate 
12507c478bd9Sstevel@tonic-gate 	int n = (yyintdecimal | (yyintsuffix[0] == 'u')) + 1;
12517c478bd9Sstevel@tonic-gate 	int i = 0;
12527c478bd9Sstevel@tonic-gate 
12537c478bd9Sstevel@tonic-gate 	const char *p;
12547c478bd9Sstevel@tonic-gate 	char c;
12557c478bd9Sstevel@tonic-gate 
12567c478bd9Sstevel@tonic-gate 	dnp->dn_op = DT_TOK_INT;
12577c478bd9Sstevel@tonic-gate 	dnp->dn_value = value;
12587c478bd9Sstevel@tonic-gate 
12597c478bd9Sstevel@tonic-gate 	for (p = yyintsuffix; (c = *p) != '\0'; p++) {
12607c478bd9Sstevel@tonic-gate 		if (c == 'U' || c == 'u')
12617c478bd9Sstevel@tonic-gate 			i += 1;
12627c478bd9Sstevel@tonic-gate 		else if (c == 'L' || c == 'l')
12637c478bd9Sstevel@tonic-gate 			i += 2;
12647c478bd9Sstevel@tonic-gate 	}
12657c478bd9Sstevel@tonic-gate 
12667c478bd9Sstevel@tonic-gate 	for (; i < sizeof (dtp->dt_ints) / sizeof (dtp->dt_ints[0]); i += n) {
12677c478bd9Sstevel@tonic-gate 		if (value <= dtp->dt_ints[i].did_limit) {
12687c478bd9Sstevel@tonic-gate 			dt_node_type_assign(dnp,
12697c478bd9Sstevel@tonic-gate 			    dtp->dt_ints[i].did_ctfp,
1270a386cc11SRobert Mustacchi 			    dtp->dt_ints[i].did_type, B_FALSE);
12717c478bd9Sstevel@tonic-gate 
12727c478bd9Sstevel@tonic-gate 			/*
12737c478bd9Sstevel@tonic-gate 			 * If a prefix character is present in macro text, add
12747c478bd9Sstevel@tonic-gate 			 * in the corresponding operator node (see dt_lex.l).
12757c478bd9Sstevel@tonic-gate 			 */
12767c478bd9Sstevel@tonic-gate 			switch (yyintprefix) {
12777c478bd9Sstevel@tonic-gate 			case '+':
12787c478bd9Sstevel@tonic-gate 				return (dt_node_op1(DT_TOK_IPOS, dnp));
12797c478bd9Sstevel@tonic-gate 			case '-':
12807c478bd9Sstevel@tonic-gate 				return (dt_node_op1(DT_TOK_INEG, dnp));
12817c478bd9Sstevel@tonic-gate 			default:
12827c478bd9Sstevel@tonic-gate 				return (dnp);
12837c478bd9Sstevel@tonic-gate 			}
12847c478bd9Sstevel@tonic-gate 		}
12857c478bd9Sstevel@tonic-gate 	}
12867c478bd9Sstevel@tonic-gate 
12877c478bd9Sstevel@tonic-gate 	xyerror(D_INT_OFLOW, "integer constant 0x%llx cannot be represented "
12887c478bd9Sstevel@tonic-gate 	    "in any built-in integral type\n", (u_longlong_t)value);
12897c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
12907c478bd9Sstevel@tonic-gate 	return (NULL);		/* keep gcc happy */
12917c478bd9Sstevel@tonic-gate }
12927c478bd9Sstevel@tonic-gate 
12937c478bd9Sstevel@tonic-gate dt_node_t *
dt_node_string(char * string)12947c478bd9Sstevel@tonic-gate dt_node_string(char *string)
12957c478bd9Sstevel@tonic-gate {
12967c478bd9Sstevel@tonic-gate 	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
12977c478bd9Sstevel@tonic-gate 	dt_node_t *dnp;
12987c478bd9Sstevel@tonic-gate 
12997c478bd9Sstevel@tonic-gate 	if (string == NULL)
13007c478bd9Sstevel@tonic-gate 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
13017c478bd9Sstevel@tonic-gate 
13027c478bd9Sstevel@tonic-gate 	dnp = dt_node_alloc(DT_NODE_STRING);
13037c478bd9Sstevel@tonic-gate 	dnp->dn_op = DT_TOK_STRING;
13047c478bd9Sstevel@tonic-gate 	dnp->dn_string = string;
1305a386cc11SRobert Mustacchi 	dt_node_type_assign(dnp, DT_STR_CTFP(dtp), DT_STR_TYPE(dtp), B_FALSE);
13067c478bd9Sstevel@tonic-gate 
13077c478bd9Sstevel@tonic-gate 	return (dnp);
13087c478bd9Sstevel@tonic-gate }
13097c478bd9Sstevel@tonic-gate 
13107c478bd9Sstevel@tonic-gate dt_node_t *
dt_node_ident(char * name)13117c478bd9Sstevel@tonic-gate dt_node_ident(char *name)
13127c478bd9Sstevel@tonic-gate {
13137c478bd9Sstevel@tonic-gate 	dt_ident_t *idp;
13147c478bd9Sstevel@tonic-gate 	dt_node_t *dnp;
13157c478bd9Sstevel@tonic-gate 
13167c478bd9Sstevel@tonic-gate 	if (name == NULL)
13177c478bd9Sstevel@tonic-gate 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
13187c478bd9Sstevel@tonic-gate 
13197c478bd9Sstevel@tonic-gate 	/*
13207c478bd9Sstevel@tonic-gate 	 * If the identifier is an inlined integer constant, then create an INT
13217c478bd9Sstevel@tonic-gate 	 * node that is a clone of the inline parse tree node and return that
13227c478bd9Sstevel@tonic-gate 	 * immediately, allowing this inline to be used in parsing contexts
13237c478bd9Sstevel@tonic-gate 	 * that require constant expressions (e.g. scalar array sizes).
13247c478bd9Sstevel@tonic-gate 	 */
13257c478bd9Sstevel@tonic-gate 	if ((idp = dt_idstack_lookup(&yypcb->pcb_globals, name)) != NULL &&
13267c478bd9Sstevel@tonic-gate 	    (idp->di_flags & DT_IDFLG_INLINE)) {
13277c478bd9Sstevel@tonic-gate 		dt_idnode_t *inp = idp->di_iarg;
13287c478bd9Sstevel@tonic-gate 
13291a7c1b72Smws 		if (inp->din_root != NULL &&
13301a7c1b72Smws 		    inp->din_root->dn_kind == DT_NODE_INT) {
13317c478bd9Sstevel@tonic-gate 			free(name);
13327c478bd9Sstevel@tonic-gate 
13337c478bd9Sstevel@tonic-gate 			dnp = dt_node_alloc(DT_NODE_INT);
13347c478bd9Sstevel@tonic-gate 			dnp->dn_op = DT_TOK_INT;
13357c478bd9Sstevel@tonic-gate 			dnp->dn_value = inp->din_root->dn_value;
13367c478bd9Sstevel@tonic-gate 			dt_node_type_propagate(inp->din_root, dnp);
13377c478bd9Sstevel@tonic-gate 
13387c478bd9Sstevel@tonic-gate 			return (dnp);
13397c478bd9Sstevel@tonic-gate 		}
13407c478bd9Sstevel@tonic-gate 	}
13417c478bd9Sstevel@tonic-gate 
13427c478bd9Sstevel@tonic-gate 	dnp = dt_node_alloc(DT_NODE_IDENT);
13437c478bd9Sstevel@tonic-gate 	dnp->dn_op = name[0] == '@' ? DT_TOK_AGG : DT_TOK_IDENT;
13447c478bd9Sstevel@tonic-gate 	dnp->dn_string = name;
13457c478bd9Sstevel@tonic-gate 
13467c478bd9Sstevel@tonic-gate 	return (dnp);
13477c478bd9Sstevel@tonic-gate }
13487c478bd9Sstevel@tonic-gate 
13497c478bd9Sstevel@tonic-gate /*
13507c478bd9Sstevel@tonic-gate  * Create an empty node of type corresponding to the given declaration.
13517c478bd9Sstevel@tonic-gate  * Explicit references to user types (C or D) are assigned the default
13527c478bd9Sstevel@tonic-gate  * stability; references to other types are _dtrace_typattr (Private).
13537c478bd9Sstevel@tonic-gate  */
13547c478bd9Sstevel@tonic-gate dt_node_t *
dt_node_type(dt_decl_t * ddp)13557c478bd9Sstevel@tonic-gate dt_node_type(dt_decl_t *ddp)
13567c478bd9Sstevel@tonic-gate {
13577c478bd9Sstevel@tonic-gate 	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
13587c478bd9Sstevel@tonic-gate 	dtrace_typeinfo_t dtt;
13597c478bd9Sstevel@tonic-gate 	dt_node_t *dnp;
13607c478bd9Sstevel@tonic-gate 	char *name = NULL;
13617c478bd9Sstevel@tonic-gate 	int err;
13627c478bd9Sstevel@tonic-gate 
13637c478bd9Sstevel@tonic-gate 	/*
13647c478bd9Sstevel@tonic-gate 	 * If 'ddp' is NULL, we get a decl by popping the decl stack.  This
13657c478bd9Sstevel@tonic-gate 	 * form of dt_node_type() is used by parameter rules in dt_grammar.y.
13667c478bd9Sstevel@tonic-gate 	 */
13677c478bd9Sstevel@tonic-gate 	if (ddp == NULL)
13687c478bd9Sstevel@tonic-gate 		ddp = dt_decl_pop_param(&name);
13697c478bd9Sstevel@tonic-gate 
13707c478bd9Sstevel@tonic-gate 	err = dt_decl_type(ddp, &dtt);
13717c478bd9Sstevel@tonic-gate 	dt_decl_free(ddp);
13727c478bd9Sstevel@tonic-gate 
13737c478bd9Sstevel@tonic-gate 	if (err != 0) {
13747c478bd9Sstevel@tonic-gate 		free(name);
13757c478bd9Sstevel@tonic-gate 		longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
13767c478bd9Sstevel@tonic-gate 	}
13777c478bd9Sstevel@tonic-gate 
13787c478bd9Sstevel@tonic-gate 	dnp = dt_node_alloc(DT_NODE_TYPE);
13797c478bd9Sstevel@tonic-gate 	dnp->dn_op = DT_TOK_IDENT;
13807c478bd9Sstevel@tonic-gate 	dnp->dn_string = name;
1381a386cc11SRobert Mustacchi 
1382*6eeafb34SRobert Mustacchi 	dt_node_type_assign(dnp, dtt.dtt_ctfp, dtt.dtt_type,
1383*6eeafb34SRobert Mustacchi 	    (dtt.dtt_flags & DTT_FL_USER) != 0);
13847c478bd9Sstevel@tonic-gate 
13857c478bd9Sstevel@tonic-gate 	if (dtt.dtt_ctfp == dtp->dt_cdefs->dm_ctfp ||
13867c478bd9Sstevel@tonic-gate 	    dtt.dtt_ctfp == dtp->dt_ddefs->dm_ctfp)
13877c478bd9Sstevel@tonic-gate 		dt_node_attr_assign(dnp, _dtrace_defattr);
13887c478bd9Sstevel@tonic-gate 	else
13897c478bd9Sstevel@tonic-gate 		dt_node_attr_assign(dnp, _dtrace_typattr);
13907c478bd9Sstevel@tonic-gate 
13917c478bd9Sstevel@tonic-gate 	return (dnp);
13927c478bd9Sstevel@tonic-gate }
13937c478bd9Sstevel@tonic-gate 
13947c478bd9Sstevel@tonic-gate /*
13957c478bd9Sstevel@tonic-gate  * Create a type node corresponding to a varargs (...) parameter by just
13967c478bd9Sstevel@tonic-gate  * assigning it type CTF_ERR.  The decl processing code will handle this.
13977c478bd9Sstevel@tonic-gate  */
13987c478bd9Sstevel@tonic-gate dt_node_t *
dt_node_vatype(void)13997c478bd9Sstevel@tonic-gate dt_node_vatype(void)
14007c478bd9Sstevel@tonic-gate {
14017c478bd9Sstevel@tonic-gate 	dt_node_t *dnp = dt_node_alloc(DT_NODE_TYPE);
14027c478bd9Sstevel@tonic-gate 
14037c478bd9Sstevel@tonic-gate 	dnp->dn_op = DT_TOK_IDENT;
14047c478bd9Sstevel@tonic-gate 	dnp->dn_ctfp = yypcb->pcb_hdl->dt_cdefs->dm_ctfp;
14057c478bd9Sstevel@tonic-gate 	dnp->dn_type = CTF_ERR;
1406*6eeafb34SRobert Mustacchi 	dnp->dn_bitoff = 0;
14077c478bd9Sstevel@tonic-gate 	dnp->dn_attr = _dtrace_defattr;
14087c478bd9Sstevel@tonic-gate 
14097c478bd9Sstevel@tonic-gate 	return (dnp);
14107c478bd9Sstevel@tonic-gate }
14117c478bd9Sstevel@tonic-gate 
14127c478bd9Sstevel@tonic-gate /*
14137c478bd9Sstevel@tonic-gate  * Instantiate a decl using the contents of the current declaration stack.  As
14147c478bd9Sstevel@tonic-gate  * we do not currently permit decls to be initialized, this function currently
14157c478bd9Sstevel@tonic-gate  * returns NULL and no parse node is created.  When this function is called,
14167c478bd9Sstevel@tonic-gate  * the topmost scope's ds_ident pointer will be set to NULL (indicating no
14177c478bd9Sstevel@tonic-gate  * init_declarator rule was matched) or will point to the identifier to use.
14187c478bd9Sstevel@tonic-gate  */
14197c478bd9Sstevel@tonic-gate dt_node_t *
dt_node_decl(void)14207c478bd9Sstevel@tonic-gate dt_node_decl(void)
14217c478bd9Sstevel@tonic-gate {
14227c478bd9Sstevel@tonic-gate 	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
14237c478bd9Sstevel@tonic-gate 	dt_scope_t *dsp = &yypcb->pcb_dstack;
14247c478bd9Sstevel@tonic-gate 	dt_dclass_t class = dsp->ds_class;
14257c478bd9Sstevel@tonic-gate 	dt_decl_t *ddp = dt_decl_top();
14267c478bd9Sstevel@tonic-gate 
14277c478bd9Sstevel@tonic-gate 	dt_module_t *dmp;
14287c478bd9Sstevel@tonic-gate 	dtrace_typeinfo_t dtt;
14297c478bd9Sstevel@tonic-gate 	ctf_id_t type;
14307c478bd9Sstevel@tonic-gate 
14317c478bd9Sstevel@tonic-gate 	char n1[DT_TYPE_NAMELEN];
14327c478bd9Sstevel@tonic-gate 	char n2[DT_TYPE_NAMELEN];
14337c478bd9Sstevel@tonic-gate 
14347c478bd9Sstevel@tonic-gate 	if (dt_decl_type(ddp, &dtt) != 0)
14357c478bd9Sstevel@tonic-gate 		longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
14367c478bd9Sstevel@tonic-gate 
14377c478bd9Sstevel@tonic-gate 	/*
14387c478bd9Sstevel@tonic-gate 	 * If we have no declaration identifier, then this is either a spurious
14397c478bd9Sstevel@tonic-gate 	 * declaration of an intrinsic type (e.g. "extern int;") or declaration
14407c478bd9Sstevel@tonic-gate 	 * or redeclaration of a struct, union, or enum type or tag.
14417c478bd9Sstevel@tonic-gate 	 */
14427c478bd9Sstevel@tonic-gate 	if (dsp->ds_ident == NULL) {
14437c478bd9Sstevel@tonic-gate 		if (ddp->dd_kind != CTF_K_STRUCT &&
14447c478bd9Sstevel@tonic-gate 		    ddp->dd_kind != CTF_K_UNION && ddp->dd_kind != CTF_K_ENUM)
14457c478bd9Sstevel@tonic-gate 			xyerror(D_DECL_USELESS, "useless declaration\n");
14467c478bd9Sstevel@tonic-gate 
14477c478bd9Sstevel@tonic-gate 		dt_dprintf("type %s added as id %ld\n", dt_type_name(
14487c478bd9Sstevel@tonic-gate 		    ddp->dd_ctfp, ddp->dd_type, n1, sizeof (n1)), ddp->dd_type);
14497c478bd9Sstevel@tonic-gate 
14507c478bd9Sstevel@tonic-gate 		return (NULL);
14517c478bd9Sstevel@tonic-gate 	}
14527c478bd9Sstevel@tonic-gate 
14537c478bd9Sstevel@tonic-gate 	if (strchr(dsp->ds_ident, '`') != NULL) {
14547c478bd9Sstevel@tonic-gate 		xyerror(D_DECL_SCOPE, "D scoping operator may not be used in "
14557c478bd9Sstevel@tonic-gate 		    "a declaration name (%s)\n", dsp->ds_ident);
14567c478bd9Sstevel@tonic-gate 	}
14577c478bd9Sstevel@tonic-gate 
14587c478bd9Sstevel@tonic-gate 	/*
14597c478bd9Sstevel@tonic-gate 	 * If we are nested inside of a C include file, add the declaration to
14607c478bd9Sstevel@tonic-gate 	 * the C definition module; otherwise use the D definition module.
14617c478bd9Sstevel@tonic-gate 	 */
14627c478bd9Sstevel@tonic-gate 	if (yypcb->pcb_idepth != 0)
14637c478bd9Sstevel@tonic-gate 		dmp = dtp->dt_cdefs;
14647c478bd9Sstevel@tonic-gate 	else
14657c478bd9Sstevel@tonic-gate 		dmp = dtp->dt_ddefs;
14667c478bd9Sstevel@tonic-gate 
14677c478bd9Sstevel@tonic-gate 	/*
14687c478bd9Sstevel@tonic-gate 	 * If we see a global or static declaration of a function prototype,
14697c478bd9Sstevel@tonic-gate 	 * treat this as equivalent to a D extern declaration.
14707c478bd9Sstevel@tonic-gate 	 */
14717c478bd9Sstevel@tonic-gate 	if (ctf_type_kind(dtt.dtt_ctfp, dtt.dtt_type) == CTF_K_FUNCTION &&
14727c478bd9Sstevel@tonic-gate 	    (class == DT_DC_DEFAULT || class == DT_DC_STATIC))
14737c478bd9Sstevel@tonic-gate 		class = DT_DC_EXTERN;
14747c478bd9Sstevel@tonic-gate 
14757c478bd9Sstevel@tonic-gate 	switch (class) {
14767c478bd9Sstevel@tonic-gate 	case DT_DC_AUTO:
14777c478bd9Sstevel@tonic-gate 	case DT_DC_REGISTER:
14787c478bd9Sstevel@tonic-gate 	case DT_DC_STATIC:
14797c478bd9Sstevel@tonic-gate 		xyerror(D_DECL_BADCLASS, "specified storage class not "
14807c478bd9Sstevel@tonic-gate 		    "appropriate in D\n");
14817c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
14827c478bd9Sstevel@tonic-gate 
14837c478bd9Sstevel@tonic-gate 	case DT_DC_EXTERN: {
14847c478bd9Sstevel@tonic-gate 		dtrace_typeinfo_t ott;
14857c478bd9Sstevel@tonic-gate 		dtrace_syminfo_t dts;
14867c478bd9Sstevel@tonic-gate 		GElf_Sym sym;
14877c478bd9Sstevel@tonic-gate 
14887c478bd9Sstevel@tonic-gate 		int exists = dtrace_lookup_by_name(dtp,
14897c478bd9Sstevel@tonic-gate 		    dmp->dm_name, dsp->ds_ident, &sym, &dts) == 0;
14907c478bd9Sstevel@tonic-gate 
14917c478bd9Sstevel@tonic-gate 		if (exists && (dtrace_symbol_type(dtp, &sym, &dts, &ott) != 0 ||
14927c478bd9Sstevel@tonic-gate 		    ctf_type_cmp(dtt.dtt_ctfp, dtt.dtt_type,
14937c478bd9Sstevel@tonic-gate 		    ott.dtt_ctfp, ott.dtt_type) != 0)) {
14947c478bd9Sstevel@tonic-gate 			xyerror(D_DECL_IDRED, "identifier redeclared: %s`%s\n"
14957c478bd9Sstevel@tonic-gate 			    "\t current: %s\n\tprevious: %s\n",
14967c478bd9Sstevel@tonic-gate 			    dmp->dm_name, dsp->ds_ident,
14977c478bd9Sstevel@tonic-gate 			    dt_type_name(dtt.dtt_ctfp, dtt.dtt_type,
14987c478bd9Sstevel@tonic-gate 			    n1, sizeof (n1)),
14997c478bd9Sstevel@tonic-gate 			    dt_type_name(ott.dtt_ctfp, ott.dtt_type,
15007c478bd9Sstevel@tonic-gate 			    n2, sizeof (n2)));
15017c478bd9Sstevel@tonic-gate 		} else if (!exists && dt_module_extern(dtp, dmp,
15027c478bd9Sstevel@tonic-gate 		    dsp->ds_ident, &dtt) == NULL) {
15037c478bd9Sstevel@tonic-gate 			xyerror(D_UNKNOWN,
15047c478bd9Sstevel@tonic-gate 			    "failed to extern %s: %s\n", dsp->ds_ident,
15057c478bd9Sstevel@tonic-gate 			    dtrace_errmsg(dtp, dtrace_errno(dtp)));
15067c478bd9Sstevel@tonic-gate 		} else {
15077c478bd9Sstevel@tonic-gate 			dt_dprintf("extern %s`%s type=<%s>\n",
15087c478bd9Sstevel@tonic-gate 			    dmp->dm_name, dsp->ds_ident,
15097c478bd9Sstevel@tonic-gate 			    dt_type_name(dtt.dtt_ctfp, dtt.dtt_type,
15107c478bd9Sstevel@tonic-gate 			    n1, sizeof (n1)));
15117c478bd9Sstevel@tonic-gate 		}
15127c478bd9Sstevel@tonic-gate 		break;
15137c478bd9Sstevel@tonic-gate 	}
15147c478bd9Sstevel@tonic-gate 
15157c478bd9Sstevel@tonic-gate 	case DT_DC_TYPEDEF:
1516e4586ebfSmws 		if (dt_idstack_lookup(&yypcb->pcb_globals, dsp->ds_ident)) {
1517e4586ebfSmws 			xyerror(D_DECL_IDRED, "global variable identifier "
1518e4586ebfSmws 			    "redeclared: %s\n", dsp->ds_ident);
1519e4586ebfSmws 		}
1520e4586ebfSmws 
1521e4586ebfSmws 		if (ctf_lookup_by_name(dmp->dm_ctfp,
1522e4586ebfSmws 		    dsp->ds_ident) != CTF_ERR) {
1523e4586ebfSmws 			xyerror(D_DECL_IDRED,
1524e4586ebfSmws 			    "typedef redeclared: %s\n", dsp->ds_ident);
1525e4586ebfSmws 		}
1526e4586ebfSmws 
15277c478bd9Sstevel@tonic-gate 		/*
15287c478bd9Sstevel@tonic-gate 		 * If the source type for the typedef is not defined in the
15297c478bd9Sstevel@tonic-gate 		 * target container or its parent, copy the type to the target
15307c478bd9Sstevel@tonic-gate 		 * container and reset dtt_ctfp and dtt_type to the copy.
15317c478bd9Sstevel@tonic-gate 		 */
15327c478bd9Sstevel@tonic-gate 		if (dtt.dtt_ctfp != dmp->dm_ctfp &&
15337c478bd9Sstevel@tonic-gate 		    dtt.dtt_ctfp != ctf_parent_file(dmp->dm_ctfp)) {
15347c478bd9Sstevel@tonic-gate 
15357c478bd9Sstevel@tonic-gate 			dtt.dtt_type = ctf_add_type(dmp->dm_ctfp,
15367c478bd9Sstevel@tonic-gate 			    dtt.dtt_ctfp, dtt.dtt_type);
15377c478bd9Sstevel@tonic-gate 			dtt.dtt_ctfp = dmp->dm_ctfp;
15387c478bd9Sstevel@tonic-gate 
15397c478bd9Sstevel@tonic-gate 			if (dtt.dtt_type == CTF_ERR ||
15407c478bd9Sstevel@tonic-gate 			    ctf_update(dtt.dtt_ctfp) == CTF_ERR) {
15417c478bd9Sstevel@tonic-gate 				xyerror(D_UNKNOWN, "failed to copy typedef %s "
15427c478bd9Sstevel@tonic-gate 				    "source type: %s\n", dsp->ds_ident,
15437c478bd9Sstevel@tonic-gate 				    ctf_errmsg(ctf_errno(dtt.dtt_ctfp)));
15447c478bd9Sstevel@tonic-gate 			}
15457c478bd9Sstevel@tonic-gate 		}
15467c478bd9Sstevel@tonic-gate 
15477c478bd9Sstevel@tonic-gate 		type = ctf_add_typedef(dmp->dm_ctfp,
15487c478bd9Sstevel@tonic-gate 		    CTF_ADD_ROOT, dsp->ds_ident, dtt.dtt_type);
15497c478bd9Sstevel@tonic-gate 
15507c478bd9Sstevel@tonic-gate 		if (type == CTF_ERR || ctf_update(dmp->dm_ctfp) == CTF_ERR) {
15517c478bd9Sstevel@tonic-gate 			xyerror(D_UNKNOWN, "failed to typedef %s: %s\n",
15527c478bd9Sstevel@tonic-gate 			    dsp->ds_ident, ctf_errmsg(ctf_errno(dmp->dm_ctfp)));
15537c478bd9Sstevel@tonic-gate 		}
15547c478bd9Sstevel@tonic-gate 
15557c478bd9Sstevel@tonic-gate 		dt_dprintf("typedef %s added as id %ld\n", dsp->ds_ident, type);
15567c478bd9Sstevel@tonic-gate 		break;
15577c478bd9Sstevel@tonic-gate 
15587c478bd9Sstevel@tonic-gate 	default: {
15597c478bd9Sstevel@tonic-gate 		ctf_encoding_t cte;
15607c478bd9Sstevel@tonic-gate 		dt_idhash_t *dhp;
15617c478bd9Sstevel@tonic-gate 		dt_ident_t *idp;
15627c478bd9Sstevel@tonic-gate 		dt_node_t idn;
15637c478bd9Sstevel@tonic-gate 		int assc, idkind;
15647c478bd9Sstevel@tonic-gate 		uint_t id, kind;
15657c478bd9Sstevel@tonic-gate 		ushort_t idflags;
15667c478bd9Sstevel@tonic-gate 
15677c478bd9Sstevel@tonic-gate 		switch (class) {
15687c478bd9Sstevel@tonic-gate 		case DT_DC_THIS:
15697c478bd9Sstevel@tonic-gate 			dhp = yypcb->pcb_locals;
15707c478bd9Sstevel@tonic-gate 			idflags = DT_IDFLG_LOCAL;
15717c478bd9Sstevel@tonic-gate 			idp = dt_idhash_lookup(dhp, dsp->ds_ident);
15727c478bd9Sstevel@tonic-gate 			break;
15737c478bd9Sstevel@tonic-gate 		case DT_DC_SELF:
15747c478bd9Sstevel@tonic-gate 			dhp = dtp->dt_tls;
15757c478bd9Sstevel@tonic-gate 			idflags = DT_IDFLG_TLS;
15767c478bd9Sstevel@tonic-gate 			idp = dt_idhash_lookup(dhp, dsp->ds_ident);
15777c478bd9Sstevel@tonic-gate 			break;
15787c478bd9Sstevel@tonic-gate 		default:
15797c478bd9Sstevel@tonic-gate 			dhp = dtp->dt_globals;
15807c478bd9Sstevel@tonic-gate 			idflags = 0;
15817c478bd9Sstevel@tonic-gate 			idp = dt_idstack_lookup(
15827c478bd9Sstevel@tonic-gate 			    &yypcb->pcb_globals, dsp->ds_ident);
15837c478bd9Sstevel@tonic-gate 			break;
15847c478bd9Sstevel@tonic-gate 		}
15857c478bd9Sstevel@tonic-gate 
15867c478bd9Sstevel@tonic-gate 		if (ddp->dd_kind == CTF_K_ARRAY && ddp->dd_node == NULL) {
15877c478bd9Sstevel@tonic-gate 			xyerror(D_DECL_ARRNULL,
15887c478bd9Sstevel@tonic-gate 			    "array declaration requires array dimension or "
15897c478bd9Sstevel@tonic-gate 			    "tuple signature: %s\n", dsp->ds_ident);
15907c478bd9Sstevel@tonic-gate 		}
15917c478bd9Sstevel@tonic-gate 
15927c478bd9Sstevel@tonic-gate 		if (idp != NULL && idp->di_gen == 0) {
15937c478bd9Sstevel@tonic-gate 			xyerror(D_DECL_IDRED, "built-in identifier "
15947c478bd9Sstevel@tonic-gate 			    "redeclared: %s\n", idp->di_name);
15957c478bd9Sstevel@tonic-gate 		}
15967c478bd9Sstevel@tonic-gate 
1597e4586ebfSmws 		if (dtrace_lookup_by_type(dtp, DTRACE_OBJ_CDEFS,
1598e4586ebfSmws 		    dsp->ds_ident, NULL) == 0 ||
1599e4586ebfSmws 		    dtrace_lookup_by_type(dtp, DTRACE_OBJ_DDEFS,
1600e4586ebfSmws 		    dsp->ds_ident, NULL) == 0) {
1601e4586ebfSmws 			xyerror(D_DECL_IDRED, "typedef identifier "
1602e4586ebfSmws 			    "redeclared: %s\n", dsp->ds_ident);
1603e4586ebfSmws 		}
1604e4586ebfSmws 
16057c478bd9Sstevel@tonic-gate 		/*
16067c478bd9Sstevel@tonic-gate 		 * Cache some attributes of the decl to make the rest of this
16077c478bd9Sstevel@tonic-gate 		 * code simpler: if the decl is an array which is subscripted
16087c478bd9Sstevel@tonic-gate 		 * by a type rather than an integer, then it's an associative
16097c478bd9Sstevel@tonic-gate 		 * array (assc).  We then expect to match either DT_IDENT_ARRAY
16107c478bd9Sstevel@tonic-gate 		 * for associative arrays or DT_IDENT_SCALAR for anything else.
16117c478bd9Sstevel@tonic-gate 		 */
16127c478bd9Sstevel@tonic-gate 		assc = ddp->dd_kind == CTF_K_ARRAY &&
16137c478bd9Sstevel@tonic-gate 		    ddp->dd_node->dn_kind == DT_NODE_TYPE;
16147c478bd9Sstevel@tonic-gate 
16157c478bd9Sstevel@tonic-gate 		idkind = assc ? DT_IDENT_ARRAY : DT_IDENT_SCALAR;
16167c478bd9Sstevel@tonic-gate 
16177c478bd9Sstevel@tonic-gate 		/*
16187c478bd9Sstevel@tonic-gate 		 * Create a fake dt_node_t on the stack so we can determine the
16197c478bd9Sstevel@tonic-gate 		 * type of any matching identifier by assigning to this node.
16207c478bd9Sstevel@tonic-gate 		 * If the pre-existing ident has its di_type set, propagate
16217c478bd9Sstevel@tonic-gate 		 * the type by hand so as not to trigger a prototype check for
16227c478bd9Sstevel@tonic-gate 		 * arrays (yet); otherwise we use dt_ident_cook() on the ident
16237c478bd9Sstevel@tonic-gate 		 * to ensure it is fully initialized before looking at it.
16247c478bd9Sstevel@tonic-gate 		 */
16257c478bd9Sstevel@tonic-gate 		bzero(&idn, sizeof (dt_node_t));
16267c478bd9Sstevel@tonic-gate 
16277c478bd9Sstevel@tonic-gate 		if (idp != NULL && idp->di_type != CTF_ERR)
1628a386cc11SRobert Mustacchi 			dt_node_type_assign(&idn, idp->di_ctfp, idp->di_type,
1629a386cc11SRobert Mustacchi 			    B_FALSE);
16307c478bd9Sstevel@tonic-gate 		else if (idp != NULL)
16317c478bd9Sstevel@tonic-gate 			(void) dt_ident_cook(&idn, idp, NULL);
16327c478bd9Sstevel@tonic-gate 
16337c478bd9Sstevel@tonic-gate 		if (assc) {
16347c478bd9Sstevel@tonic-gate 			if (class == DT_DC_THIS) {
16357c478bd9Sstevel@tonic-gate 				xyerror(D_DECL_LOCASSC, "associative arrays "
16367c478bd9Sstevel@tonic-gate 				    "may not be declared as local variables:"
16377c478bd9Sstevel@tonic-gate 				    " %s\n", dsp->ds_ident);
16387c478bd9Sstevel@tonic-gate 			}
16397c478bd9Sstevel@tonic-gate 
16407c478bd9Sstevel@tonic-gate 			if (dt_decl_type(ddp->dd_next, &dtt) != 0)
16417c478bd9Sstevel@tonic-gate 				longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
16427c478bd9Sstevel@tonic-gate 		}
16437c478bd9Sstevel@tonic-gate 
16447c478bd9Sstevel@tonic-gate 		if (idp != NULL && (idp->di_kind != idkind ||
16457c478bd9Sstevel@tonic-gate 		    ctf_type_cmp(dtt.dtt_ctfp, dtt.dtt_type,
16467c478bd9Sstevel@tonic-gate 		    idn.dn_ctfp, idn.dn_type) != 0)) {
16477c478bd9Sstevel@tonic-gate 			xyerror(D_DECL_IDRED, "identifier redeclared: %s\n"
16487c478bd9Sstevel@tonic-gate 			    "\t current: %s %s\n\tprevious: %s %s\n",
16497c478bd9Sstevel@tonic-gate 			    dsp->ds_ident, dt_idkind_name(idkind),
16507c478bd9Sstevel@tonic-gate 			    dt_type_name(dtt.dtt_ctfp,
16517c478bd9Sstevel@tonic-gate 			    dtt.dtt_type, n1, sizeof (n1)),
16527c478bd9Sstevel@tonic-gate 			    dt_idkind_name(idp->di_kind),
16537c478bd9Sstevel@tonic-gate 			    dt_node_type_name(&idn, n2, sizeof (n2)));
16547c478bd9Sstevel@tonic-gate 
16557c478bd9Sstevel@tonic-gate 		} else if (idp != NULL && assc) {
16567c478bd9Sstevel@tonic-gate 			const dt_idsig_t *isp = idp->di_data;
16577c478bd9Sstevel@tonic-gate 			dt_node_t *dnp = ddp->dd_node;
16587c478bd9Sstevel@tonic-gate 			int argc = 0;
16597c478bd9Sstevel@tonic-gate 
16607c478bd9Sstevel@tonic-gate 			for (; dnp != NULL; dnp = dnp->dn_list, argc++) {
16617c478bd9Sstevel@tonic-gate 				const dt_node_t *pnp = &isp->dis_args[argc];
16627c478bd9Sstevel@tonic-gate 
16637c478bd9Sstevel@tonic-gate 				if (argc >= isp->dis_argc)
16647c478bd9Sstevel@tonic-gate 					continue; /* tuple length mismatch */
16657c478bd9Sstevel@tonic-gate 
16667c478bd9Sstevel@tonic-gate 				if (ctf_type_cmp(dnp->dn_ctfp, dnp->dn_type,
16677c478bd9Sstevel@tonic-gate 				    pnp->dn_ctfp, pnp->dn_type) == 0)
16687c478bd9Sstevel@tonic-gate 					continue;
16697c478bd9Sstevel@tonic-gate 
16707c478bd9Sstevel@tonic-gate 				xyerror(D_DECL_IDRED,
16717c478bd9Sstevel@tonic-gate 				    "identifier redeclared: %s\n"
16727c478bd9Sstevel@tonic-gate 				    "\t current: %s, key #%d of type %s\n"
16737c478bd9Sstevel@tonic-gate 				    "\tprevious: %s, key #%d of type %s\n",
16747c478bd9Sstevel@tonic-gate 				    dsp->ds_ident,
16757c478bd9Sstevel@tonic-gate 				    dt_idkind_name(idkind), argc + 1,
16767c478bd9Sstevel@tonic-gate 				    dt_node_type_name(dnp, n1, sizeof (n1)),
16777c478bd9Sstevel@tonic-gate 				    dt_idkind_name(idp->di_kind), argc + 1,
16787c478bd9Sstevel@tonic-gate 				    dt_node_type_name(pnp, n2, sizeof (n2)));
16797c478bd9Sstevel@tonic-gate 			}
16807c478bd9Sstevel@tonic-gate 
16817c478bd9Sstevel@tonic-gate 			if (isp->dis_argc != argc) {
16827c478bd9Sstevel@tonic-gate 				xyerror(D_DECL_IDRED,
16837c478bd9Sstevel@tonic-gate 				    "identifier redeclared: %s\n"
16847c478bd9Sstevel@tonic-gate 				    "\t current: %s of %s, tuple length %d\n"
16857c478bd9Sstevel@tonic-gate 				    "\tprevious: %s of %s, tuple length %d\n",
16867c478bd9Sstevel@tonic-gate 				    dsp->ds_ident, dt_idkind_name(idkind),
16877c478bd9Sstevel@tonic-gate 				    dt_type_name(dtt.dtt_ctfp, dtt.dtt_type,
16887c478bd9Sstevel@tonic-gate 				    n1, sizeof (n1)), argc,
16897c478bd9Sstevel@tonic-gate 				    dt_idkind_name(idp->di_kind),
16907c478bd9Sstevel@tonic-gate 				    dt_node_type_name(&idn, n2, sizeof (n2)),
16917c478bd9Sstevel@tonic-gate 				    isp->dis_argc);
16927c478bd9Sstevel@tonic-gate 			}
16937c478bd9Sstevel@tonic-gate 
16947c478bd9Sstevel@tonic-gate 		} else if (idp == NULL) {
16957c478bd9Sstevel@tonic-gate 			type = ctf_type_resolve(dtt.dtt_ctfp, dtt.dtt_type);
16967c478bd9Sstevel@tonic-gate 			kind = ctf_type_kind(dtt.dtt_ctfp, type);
16977c478bd9Sstevel@tonic-gate 
16987c478bd9Sstevel@tonic-gate 			switch (kind) {
16997c478bd9Sstevel@tonic-gate 			case CTF_K_INTEGER:
17007c478bd9Sstevel@tonic-gate 				if (ctf_type_encoding(dtt.dtt_ctfp, type,
17017c478bd9Sstevel@tonic-gate 				    &cte) == 0 && IS_VOID(cte)) {
17027c478bd9Sstevel@tonic-gate 					xyerror(D_DECL_VOIDOBJ, "cannot have "
17037c478bd9Sstevel@tonic-gate 					    "void object: %s\n", dsp->ds_ident);
17047c478bd9Sstevel@tonic-gate 				}
17057c478bd9Sstevel@tonic-gate 				break;
17067c478bd9Sstevel@tonic-gate 			case CTF_K_STRUCT:
17077c478bd9Sstevel@tonic-gate 			case CTF_K_UNION:
17087c478bd9Sstevel@tonic-gate 				if (ctf_type_size(dtt.dtt_ctfp, type) != 0)
17097c478bd9Sstevel@tonic-gate 					break; /* proceed to declaring */
17107c478bd9Sstevel@tonic-gate 				/*FALLTHRU*/
17117c478bd9Sstevel@tonic-gate 			case CTF_K_FORWARD:
17127c478bd9Sstevel@tonic-gate 				xyerror(D_DECL_INCOMPLETE,
17137c478bd9Sstevel@tonic-gate 				    "incomplete struct/union/enum %s: %s\n",
17147c478bd9Sstevel@tonic-gate 				    dt_type_name(dtt.dtt_ctfp, dtt.dtt_type,
17157c478bd9Sstevel@tonic-gate 				    n1, sizeof (n1)), dsp->ds_ident);
17167c478bd9Sstevel@tonic-gate 				/*NOTREACHED*/
17177c478bd9Sstevel@tonic-gate 			}
17187c478bd9Sstevel@tonic-gate 
17197c478bd9Sstevel@tonic-gate 			if (dt_idhash_nextid(dhp, &id) == -1) {
17207c478bd9Sstevel@tonic-gate 				xyerror(D_ID_OFLOW, "cannot create %s: limit "
17217c478bd9Sstevel@tonic-gate 				    "on number of %s variables exceeded\n",
17227c478bd9Sstevel@tonic-gate 				    dsp->ds_ident, dt_idhash_name(dhp));
17237c478bd9Sstevel@tonic-gate 			}
17247c478bd9Sstevel@tonic-gate 
17257c478bd9Sstevel@tonic-gate 			dt_dprintf("declare %s %s variable %s, id=%u\n",
17267c478bd9Sstevel@tonic-gate 			    dt_idhash_name(dhp), dt_idkind_name(idkind),
17277c478bd9Sstevel@tonic-gate 			    dsp->ds_ident, id);
17287c478bd9Sstevel@tonic-gate 
17297c478bd9Sstevel@tonic-gate 			idp = dt_idhash_insert(dhp, dsp->ds_ident, idkind,
17307c478bd9Sstevel@tonic-gate 			    idflags | DT_IDFLG_WRITE | DT_IDFLG_DECL, id,
17317c478bd9Sstevel@tonic-gate 			    _dtrace_defattr, 0, assc ? &dt_idops_assc :
17327c478bd9Sstevel@tonic-gate 			    &dt_idops_thaw, NULL, dtp->dt_gen);
17337c478bd9Sstevel@tonic-gate 
17347c478bd9Sstevel@tonic-gate 			if (idp == NULL)
17357c478bd9Sstevel@tonic-gate 				longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
17367c478bd9Sstevel@tonic-gate 
17377c478bd9Sstevel@tonic-gate 			dt_ident_type_assign(idp, dtt.dtt_ctfp, dtt.dtt_type);
17387c478bd9Sstevel@tonic-gate 
17397c478bd9Sstevel@tonic-gate 			/*
17407c478bd9Sstevel@tonic-gate 			 * If we are declaring an associative array, use our
17417c478bd9Sstevel@tonic-gate 			 * fake parse node to cook the new assoc identifier.
17427c478bd9Sstevel@tonic-gate 			 * This will force the ident code to instantiate the
17437c478bd9Sstevel@tonic-gate 			 * array type signature corresponding to the list of
17447c478bd9Sstevel@tonic-gate 			 * types pointed to by ddp->dd_node.  We also reset
17457c478bd9Sstevel@tonic-gate 			 * the identifier's attributes based upon the result.
17467c478bd9Sstevel@tonic-gate 			 */
17477c478bd9Sstevel@tonic-gate 			if (assc) {
17487c478bd9Sstevel@tonic-gate 				idp->di_attr =
17497c478bd9Sstevel@tonic-gate 				    dt_ident_cook(&idn, idp, &ddp->dd_node);
17507c478bd9Sstevel@tonic-gate 			}
17517c478bd9Sstevel@tonic-gate 		}
17527c478bd9Sstevel@tonic-gate 	}
17537c478bd9Sstevel@tonic-gate 
17547c478bd9Sstevel@tonic-gate 	} /* end of switch */
17557c478bd9Sstevel@tonic-gate 
17567c478bd9Sstevel@tonic-gate 	free(dsp->ds_ident);
17577c478bd9Sstevel@tonic-gate 	dsp->ds_ident = NULL;
17587c478bd9Sstevel@tonic-gate 
17597c478bd9Sstevel@tonic-gate 	return (NULL);
17607c478bd9Sstevel@tonic-gate }
17617c478bd9Sstevel@tonic-gate 
17627c478bd9Sstevel@tonic-gate dt_node_t *
dt_node_func(dt_node_t * dnp,dt_node_t * args)17637c478bd9Sstevel@tonic-gate dt_node_func(dt_node_t *dnp, dt_node_t *args)
17647c478bd9Sstevel@tonic-gate {
17657c478bd9Sstevel@tonic-gate 	dt_ident_t *idp;
17667c478bd9Sstevel@tonic-gate 
17677c478bd9Sstevel@tonic-gate 	if (dnp->dn_kind != DT_NODE_IDENT) {
17687c478bd9Sstevel@tonic-gate 		xyerror(D_FUNC_IDENT,
17697c478bd9Sstevel@tonic-gate 		    "function designator is not of function type\n");
17707c478bd9Sstevel@tonic-gate 	}
17717c478bd9Sstevel@tonic-gate 
17727c478bd9Sstevel@tonic-gate 	idp = dt_idstack_lookup(&yypcb->pcb_globals, dnp->dn_string);
17737c478bd9Sstevel@tonic-gate 
17747c478bd9Sstevel@tonic-gate 	if (idp == NULL) {
17757c478bd9Sstevel@tonic-gate 		xyerror(D_FUNC_UNDEF,
17767c478bd9Sstevel@tonic-gate 		    "undefined function name: %s\n", dnp->dn_string);
17777c478bd9Sstevel@tonic-gate 	}
17787c478bd9Sstevel@tonic-gate 
17797c478bd9Sstevel@tonic-gate 	if (idp->di_kind != DT_IDENT_FUNC &&
17807c478bd9Sstevel@tonic-gate 	    idp->di_kind != DT_IDENT_AGGFUNC &&
17817c478bd9Sstevel@tonic-gate 	    idp->di_kind != DT_IDENT_ACTFUNC) {
17827c478bd9Sstevel@tonic-gate 		xyerror(D_FUNC_IDKIND, "%s '%s' may not be referenced as a "
17837c478bd9Sstevel@tonic-gate 		    "function\n", dt_idkind_name(idp->di_kind), idp->di_name);
17847c478bd9Sstevel@tonic-gate 	}
17857c478bd9Sstevel@tonic-gate 
17867c478bd9Sstevel@tonic-gate 	free(dnp->dn_string);
17877c478bd9Sstevel@tonic-gate 	dnp->dn_string = NULL;
17887c478bd9Sstevel@tonic-gate 
17897c478bd9Sstevel@tonic-gate 	dnp->dn_kind = DT_NODE_FUNC;
17907c478bd9Sstevel@tonic-gate 	dnp->dn_flags &= ~DT_NF_COOKED;
17917c478bd9Sstevel@tonic-gate 	dnp->dn_ident = idp;
17927c478bd9Sstevel@tonic-gate 	dnp->dn_args = args;
17937c478bd9Sstevel@tonic-gate 	dnp->dn_list = NULL;
17947c478bd9Sstevel@tonic-gate 
17957c478bd9Sstevel@tonic-gate 	return (dnp);
17967c478bd9Sstevel@tonic-gate }
17977c478bd9Sstevel@tonic-gate 
17987c478bd9Sstevel@tonic-gate /*
17997c478bd9Sstevel@tonic-gate  * The offsetof() function is special because it takes a type name as an
18007c478bd9Sstevel@tonic-gate  * argument.  It does not actually construct its own node; after looking up the
18017c478bd9Sstevel@tonic-gate  * structure or union offset, we just return an integer node with the offset.
18027c478bd9Sstevel@tonic-gate  */
18037c478bd9Sstevel@tonic-gate dt_node_t *
dt_node_offsetof(dt_decl_t * ddp,char * s)18047c478bd9Sstevel@tonic-gate dt_node_offsetof(dt_decl_t *ddp, char *s)
18057c478bd9Sstevel@tonic-gate {
18067c478bd9Sstevel@tonic-gate 	dtrace_typeinfo_t dtt;
18077c478bd9Sstevel@tonic-gate 	dt_node_t dn;
18087c478bd9Sstevel@tonic-gate 	char *name;
18097c478bd9Sstevel@tonic-gate 	int err;
18107c478bd9Sstevel@tonic-gate 
18117c478bd9Sstevel@tonic-gate 	ctf_membinfo_t ctm;
18127c478bd9Sstevel@tonic-gate 	ctf_id_t type;
18137c478bd9Sstevel@tonic-gate 	uint_t kind;
18147c478bd9Sstevel@tonic-gate 
181523a1cceaSRoger A. Faulkner 	name = strdupa(s);
18167c478bd9Sstevel@tonic-gate 	free(s);
18177c478bd9Sstevel@tonic-gate 
18187c478bd9Sstevel@tonic-gate 	err = dt_decl_type(ddp, &dtt);
18197c478bd9Sstevel@tonic-gate 	dt_decl_free(ddp);
18207c478bd9Sstevel@tonic-gate 
18217c478bd9Sstevel@tonic-gate 	if (err != 0)
18227c478bd9Sstevel@tonic-gate 		longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
18237c478bd9Sstevel@tonic-gate 
18247c478bd9Sstevel@tonic-gate 	type = ctf_type_resolve(dtt.dtt_ctfp, dtt.dtt_type);
18257c478bd9Sstevel@tonic-gate 	kind = ctf_type_kind(dtt.dtt_ctfp, type);
18267c478bd9Sstevel@tonic-gate 
18277c478bd9Sstevel@tonic-gate 	if (kind != CTF_K_STRUCT && kind != CTF_K_UNION) {
18287c478bd9Sstevel@tonic-gate 		xyerror(D_OFFSETOF_TYPE,
18297c478bd9Sstevel@tonic-gate 		    "offsetof operand must be a struct or union type\n");
18307c478bd9Sstevel@tonic-gate 	}
18317c478bd9Sstevel@tonic-gate 
18327c478bd9Sstevel@tonic-gate 	if (ctf_member_info(dtt.dtt_ctfp, type, name, &ctm) == CTF_ERR) {
18337c478bd9Sstevel@tonic-gate 		xyerror(D_UNKNOWN, "failed to determine offset of %s: %s\n",
18347c478bd9Sstevel@tonic-gate 		    name, ctf_errmsg(ctf_errno(dtt.dtt_ctfp)));
18357c478bd9Sstevel@tonic-gate 	}
18367c478bd9Sstevel@tonic-gate 
18377c478bd9Sstevel@tonic-gate 	bzero(&dn, sizeof (dn));
1838a386cc11SRobert Mustacchi 	dt_node_type_assign(&dn, dtt.dtt_ctfp, ctm.ctm_type, B_FALSE);
18397c478bd9Sstevel@tonic-gate 
18407c478bd9Sstevel@tonic-gate 	if (dn.dn_flags & DT_NF_BITFIELD) {
18417c478bd9Sstevel@tonic-gate 		xyerror(D_OFFSETOF_BITFIELD,
18427c478bd9Sstevel@tonic-gate 		    "cannot take offset of a bit-field: %s\n", name);
18437c478bd9Sstevel@tonic-gate 	}
18447c478bd9Sstevel@tonic-gate 
18457c478bd9Sstevel@tonic-gate 	return (dt_node_int(ctm.ctm_offset / NBBY));
18467c478bd9Sstevel@tonic-gate }
18477c478bd9Sstevel@tonic-gate 
18487c478bd9Sstevel@tonic-gate dt_node_t *
dt_node_op1(int op,dt_node_t * cp)18497c478bd9Sstevel@tonic-gate dt_node_op1(int op, dt_node_t *cp)
18507c478bd9Sstevel@tonic-gate {
18517c478bd9Sstevel@tonic-gate 	dt_node_t *dnp;
18527c478bd9Sstevel@tonic-gate 
18537c478bd9Sstevel@tonic-gate 	if (cp->dn_kind == DT_NODE_INT) {
18547c478bd9Sstevel@tonic-gate 		switch (op) {
18557c478bd9Sstevel@tonic-gate 		case DT_TOK_INEG:
18567c478bd9Sstevel@tonic-gate 			/*
18577c478bd9Sstevel@tonic-gate 			 * If we're negating an unsigned integer, zero out any
18587c478bd9Sstevel@tonic-gate 			 * extra top bits to truncate the value to the size of
18597c478bd9Sstevel@tonic-gate 			 * the effective type determined by dt_node_int().
18607c478bd9Sstevel@tonic-gate 			 */
18617c478bd9Sstevel@tonic-gate 			cp->dn_value = -cp->dn_value;
18627c478bd9Sstevel@tonic-gate 			if (!(cp->dn_flags & DT_NF_SIGNED)) {
18637c478bd9Sstevel@tonic-gate 				cp->dn_value &= ~0ULL >>
18647c478bd9Sstevel@tonic-gate 				    (64 - dt_node_type_size(cp) * NBBY);
18657c478bd9Sstevel@tonic-gate 			}
18667c478bd9Sstevel@tonic-gate 			/*FALLTHRU*/
18677c478bd9Sstevel@tonic-gate 		case DT_TOK_IPOS:
18687c478bd9Sstevel@tonic-gate 			return (cp);
18697c478bd9Sstevel@tonic-gate 		case DT_TOK_BNEG:
18707c478bd9Sstevel@tonic-gate 			cp->dn_value = ~cp->dn_value;
18717c478bd9Sstevel@tonic-gate 			return (cp);
18727c478bd9Sstevel@tonic-gate 		case DT_TOK_LNEG:
18737c478bd9Sstevel@tonic-gate 			cp->dn_value = !cp->dn_value;
18747c478bd9Sstevel@tonic-gate 			return (cp);
18757c478bd9Sstevel@tonic-gate 		}
18767c478bd9Sstevel@tonic-gate 	}
18777c478bd9Sstevel@tonic-gate 
18787c478bd9Sstevel@tonic-gate 	/*
18797c478bd9Sstevel@tonic-gate 	 * If sizeof is applied to a type_name or string constant, we can
18807c478bd9Sstevel@tonic-gate 	 * transform 'cp' into an integer constant in the node construction
18817c478bd9Sstevel@tonic-gate 	 * pass so that it can then be used for arithmetic in this pass.
18827c478bd9Sstevel@tonic-gate 	 */
18837c478bd9Sstevel@tonic-gate 	if (op == DT_TOK_SIZEOF &&
18847c478bd9Sstevel@tonic-gate 	    (cp->dn_kind == DT_NODE_STRING || cp->dn_kind == DT_NODE_TYPE)) {
18857c478bd9Sstevel@tonic-gate 		dtrace_hdl_t *dtp = yypcb->pcb_hdl;
18867c478bd9Sstevel@tonic-gate 		size_t size = dt_node_type_size(cp);
18877c478bd9Sstevel@tonic-gate 
18887c478bd9Sstevel@tonic-gate 		if (size == 0) {
18897c478bd9Sstevel@tonic-gate 			xyerror(D_SIZEOF_TYPE, "cannot apply sizeof to an "
18907c478bd9Sstevel@tonic-gate 			    "operand of unknown size\n");
18917c478bd9Sstevel@tonic-gate 		}
18927c478bd9Sstevel@tonic-gate 
18937c478bd9Sstevel@tonic-gate 		dt_node_type_assign(cp, dtp->dt_ddefs->dm_ctfp,
1894a386cc11SRobert Mustacchi 		    ctf_lookup_by_name(dtp->dt_ddefs->dm_ctfp, "size_t"),
1895a386cc11SRobert Mustacchi 		    B_FALSE);
18967c478bd9Sstevel@tonic-gate 
18977c478bd9Sstevel@tonic-gate 		cp->dn_kind = DT_NODE_INT;
18987c478bd9Sstevel@tonic-gate 		cp->dn_op = DT_TOK_INT;
18997c478bd9Sstevel@tonic-gate 		cp->dn_value = size;
19007c478bd9Sstevel@tonic-gate 
19017c478bd9Sstevel@tonic-gate 		return (cp);
19027c478bd9Sstevel@tonic-gate 	}
19037c478bd9Sstevel@tonic-gate 
19047c478bd9Sstevel@tonic-gate 	dnp = dt_node_alloc(DT_NODE_OP1);
19057c478bd9Sstevel@tonic-gate 	assert(op <= USHRT_MAX);
19067c478bd9Sstevel@tonic-gate 	dnp->dn_op = (ushort_t)op;
19077c478bd9Sstevel@tonic-gate 	dnp->dn_child = cp;
19087c478bd9Sstevel@tonic-gate 
19097c478bd9Sstevel@tonic-gate 	return (dnp);
19107c478bd9Sstevel@tonic-gate }
19117c478bd9Sstevel@tonic-gate 
1912e5803b76SAdam H. Leventhal /*
1913e5803b76SAdam H. Leventhal  * If an integer constant is being cast to another integer type, we can
1914e5803b76SAdam H. Leventhal  * perform the cast as part of integer constant folding in this pass. We must
1915e5803b76SAdam H. Leventhal  * take action when the integer is being cast to a smaller type or if it is
1916e5803b76SAdam H. Leventhal  * changing signed-ness. If so, we first shift rp's bits bits high (losing
1917e5803b76SAdam H. Leventhal  * excess bits if narrowing) and then shift them down with either a logical
1918e5803b76SAdam H. Leventhal  * shift (unsigned) or arithmetic shift (signed).
1919e5803b76SAdam H. Leventhal  */
1920e5803b76SAdam H. Leventhal static void
dt_cast(dt_node_t * lp,dt_node_t * rp)1921e5803b76SAdam H. Leventhal dt_cast(dt_node_t *lp, dt_node_t *rp)
1922e5803b76SAdam H. Leventhal {
1923e5803b76SAdam H. Leventhal 	size_t srcsize = dt_node_type_size(rp);
1924e5803b76SAdam H. Leventhal 	size_t dstsize = dt_node_type_size(lp);
1925e5803b76SAdam H. Leventhal 
1926e5803b76SAdam H. Leventhal 	if (dstsize < srcsize) {
1927e5803b76SAdam H. Leventhal 		int n = (sizeof (uint64_t) - dstsize) * NBBY;
1928e5803b76SAdam H. Leventhal 		rp->dn_value <<= n;
1929e5803b76SAdam H. Leventhal 		rp->dn_value >>= n;
1930e5803b76SAdam H. Leventhal 	} else if (dstsize > srcsize) {
1931e5803b76SAdam H. Leventhal 		int n = (sizeof (uint64_t) - srcsize) * NBBY;
1932e5803b76SAdam H. Leventhal 		int s = (dstsize - srcsize) * NBBY;
1933e5803b76SAdam H. Leventhal 
1934e5803b76SAdam H. Leventhal 		rp->dn_value <<= n;
1935e5803b76SAdam H. Leventhal 		if (rp->dn_flags & DT_NF_SIGNED) {
1936e5803b76SAdam H. Leventhal 			rp->dn_value = (intmax_t)rp->dn_value >> s;
1937e5803b76SAdam H. Leventhal 			rp->dn_value >>= n - s;
1938e5803b76SAdam H. Leventhal 		} else {
1939e5803b76SAdam H. Leventhal 			rp->dn_value >>= n;
1940e5803b76SAdam H. Leventhal 		}
1941e5803b76SAdam H. Leventhal 	}
1942e5803b76SAdam H. Leventhal }
1943e5803b76SAdam H. Leventhal 
19447c478bd9Sstevel@tonic-gate dt_node_t *
dt_node_op2(int op,dt_node_t * lp,dt_node_t * rp)19457c478bd9Sstevel@tonic-gate dt_node_op2(int op, dt_node_t *lp, dt_node_t *rp)
19467c478bd9Sstevel@tonic-gate {
19477c478bd9Sstevel@tonic-gate 	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
19487c478bd9Sstevel@tonic-gate 	dt_node_t *dnp;
19497c478bd9Sstevel@tonic-gate 
19507c478bd9Sstevel@tonic-gate 	/*
19517c478bd9Sstevel@tonic-gate 	 * First we check for operations that are illegal -- namely those that
19527c478bd9Sstevel@tonic-gate 	 * might result in integer division by zero, and abort if one is found.
19537c478bd9Sstevel@tonic-gate 	 */
19547c478bd9Sstevel@tonic-gate 	if (rp->dn_kind == DT_NODE_INT && rp->dn_value == 0 &&
19557c478bd9Sstevel@tonic-gate 	    (op == DT_TOK_MOD || op == DT_TOK_DIV ||
19567c478bd9Sstevel@tonic-gate 	    op == DT_TOK_MOD_EQ || op == DT_TOK_DIV_EQ))
19577c478bd9Sstevel@tonic-gate 		xyerror(D_DIV_ZERO, "expression contains division by zero\n");
19587c478bd9Sstevel@tonic-gate 
19597c478bd9Sstevel@tonic-gate 	/*
19607c478bd9Sstevel@tonic-gate 	 * If both children are immediate values, we can just perform inline
19617c478bd9Sstevel@tonic-gate 	 * calculation and return a new immediate node with the result.
19627c478bd9Sstevel@tonic-gate 	 */
19637c478bd9Sstevel@tonic-gate 	if (lp->dn_kind == DT_NODE_INT && rp->dn_kind == DT_NODE_INT) {
19647c478bd9Sstevel@tonic-gate 		uintmax_t l = lp->dn_value;
19657c478bd9Sstevel@tonic-gate 		uintmax_t r = rp->dn_value;
19667c478bd9Sstevel@tonic-gate 
19677c478bd9Sstevel@tonic-gate 		dnp = dt_node_int(0); /* allocate new integer node for result */
19687c478bd9Sstevel@tonic-gate 
19697c478bd9Sstevel@tonic-gate 		switch (op) {
19707c478bd9Sstevel@tonic-gate 		case DT_TOK_LOR:
19717c478bd9Sstevel@tonic-gate 			dnp->dn_value = l || r;
19727c478bd9Sstevel@tonic-gate 			dt_node_type_assign(dnp,
1973a386cc11SRobert Mustacchi 			    DT_INT_CTFP(dtp), DT_INT_TYPE(dtp), B_FALSE);
19747c478bd9Sstevel@tonic-gate 			break;
19757c478bd9Sstevel@tonic-gate 		case DT_TOK_LXOR:
19767c478bd9Sstevel@tonic-gate 			dnp->dn_value = (l != 0) ^ (r != 0);
19777c478bd9Sstevel@tonic-gate 			dt_node_type_assign(dnp,
1978a386cc11SRobert Mustacchi 			    DT_INT_CTFP(dtp), DT_INT_TYPE(dtp), B_FALSE);
19797c478bd9Sstevel@tonic-gate 			break;
19807c478bd9Sstevel@tonic-gate 		case DT_TOK_LAND:
19817c478bd9Sstevel@tonic-gate 			dnp->dn_value = l && r;
19827c478bd9Sstevel@tonic-gate 			dt_node_type_assign(dnp,
1983a386cc11SRobert Mustacchi 			    DT_INT_CTFP(dtp), DT_INT_TYPE(dtp), B_FALSE);
19847c478bd9Sstevel@tonic-gate 			break;
19857c478bd9Sstevel@tonic-gate 		case DT_TOK_BOR:
19867c478bd9Sstevel@tonic-gate 			dnp->dn_value = l | r;
19877c478bd9Sstevel@tonic-gate 			dt_node_promote(lp, rp, dnp);
19887c478bd9Sstevel@tonic-gate 			break;
19897c478bd9Sstevel@tonic-gate 		case DT_TOK_XOR:
19907c478bd9Sstevel@tonic-gate 			dnp->dn_value = l ^ r;
19917c478bd9Sstevel@tonic-gate 			dt_node_promote(lp, rp, dnp);
19927c478bd9Sstevel@tonic-gate 			break;
19937c478bd9Sstevel@tonic-gate 		case DT_TOK_BAND:
19947c478bd9Sstevel@tonic-gate 			dnp->dn_value = l & r;
19957c478bd9Sstevel@tonic-gate 			dt_node_promote(lp, rp, dnp);
19967c478bd9Sstevel@tonic-gate 			break;
19977c478bd9Sstevel@tonic-gate 		case DT_TOK_EQU:
19987c478bd9Sstevel@tonic-gate 			dnp->dn_value = l == r;
19997c478bd9Sstevel@tonic-gate 			dt_node_type_assign(dnp,
2000a386cc11SRobert Mustacchi 			    DT_INT_CTFP(dtp), DT_INT_TYPE(dtp), B_FALSE);
20017c478bd9Sstevel@tonic-gate 			break;
20027c478bd9Sstevel@tonic-gate 		case DT_TOK_NEQ:
20037c478bd9Sstevel@tonic-gate 			dnp->dn_value = l != r;
20047c478bd9Sstevel@tonic-gate 			dt_node_type_assign(dnp,
2005a386cc11SRobert Mustacchi 			    DT_INT_CTFP(dtp), DT_INT_TYPE(dtp), B_FALSE);
20067c478bd9Sstevel@tonic-gate 			break;
20077c478bd9Sstevel@tonic-gate 		case DT_TOK_LT:
20087c478bd9Sstevel@tonic-gate 			dt_node_promote(lp, rp, dnp);
20097c478bd9Sstevel@tonic-gate 			if (dnp->dn_flags & DT_NF_SIGNED)
20107c478bd9Sstevel@tonic-gate 				dnp->dn_value = (intmax_t)l < (intmax_t)r;
20117c478bd9Sstevel@tonic-gate 			else
20127c478bd9Sstevel@tonic-gate 				dnp->dn_value = l < r;
20137c478bd9Sstevel@tonic-gate 			dt_node_type_assign(dnp,
2014a386cc11SRobert Mustacchi 			    DT_INT_CTFP(dtp), DT_INT_TYPE(dtp), B_FALSE);
20157c478bd9Sstevel@tonic-gate 			break;
20167c478bd9Sstevel@tonic-gate 		case DT_TOK_LE:
20177c478bd9Sstevel@tonic-gate 			dt_node_promote(lp, rp, dnp);
20187c478bd9Sstevel@tonic-gate 			if (dnp->dn_flags & DT_NF_SIGNED)
20197c478bd9Sstevel@tonic-gate 				dnp->dn_value = (intmax_t)l <= (intmax_t)r;
20207c478bd9Sstevel@tonic-gate 			else
20217c478bd9Sstevel@tonic-gate 				dnp->dn_value = l <= r;
20227c478bd9Sstevel@tonic-gate 			dt_node_type_assign(dnp,
2023a386cc11SRobert Mustacchi 			    DT_INT_CTFP(dtp), DT_INT_TYPE(dtp), B_FALSE);
20247c478bd9Sstevel@tonic-gate 			break;
20257c478bd9Sstevel@tonic-gate 		case DT_TOK_GT:
20267c478bd9Sstevel@tonic-gate 			dt_node_promote(lp, rp, dnp);
20277c478bd9Sstevel@tonic-gate 			if (dnp->dn_flags & DT_NF_SIGNED)
20287c478bd9Sstevel@tonic-gate 				dnp->dn_value = (intmax_t)l > (intmax_t)r;
20297c478bd9Sstevel@tonic-gate 			else
20307c478bd9Sstevel@tonic-gate 				dnp->dn_value = l > r;
20317c478bd9Sstevel@tonic-gate 			dt_node_type_assign(dnp,
2032a386cc11SRobert Mustacchi 			    DT_INT_CTFP(dtp), DT_INT_TYPE(dtp), B_FALSE);
20337c478bd9Sstevel@tonic-gate 			break;
20347c478bd9Sstevel@tonic-gate 		case DT_TOK_GE:
20357c478bd9Sstevel@tonic-gate 			dt_node_promote(lp, rp, dnp);
20367c478bd9Sstevel@tonic-gate 			if (dnp->dn_flags & DT_NF_SIGNED)
20377c478bd9Sstevel@tonic-gate 				dnp->dn_value = (intmax_t)l >= (intmax_t)r;
20387c478bd9Sstevel@tonic-gate 			else
20397c478bd9Sstevel@tonic-gate 				dnp->dn_value = l >= r;
20407c478bd9Sstevel@tonic-gate 			dt_node_type_assign(dnp,
2041a386cc11SRobert Mustacchi 			    DT_INT_CTFP(dtp), DT_INT_TYPE(dtp), B_FALSE);
20427c478bd9Sstevel@tonic-gate 			break;
20437c478bd9Sstevel@tonic-gate 		case DT_TOK_LSH:
20447c478bd9Sstevel@tonic-gate 			dnp->dn_value = l << r;
20457c478bd9Sstevel@tonic-gate 			dt_node_type_propagate(lp, dnp);
20467c478bd9Sstevel@tonic-gate 			dt_node_attr_assign(rp,
20477c478bd9Sstevel@tonic-gate 			    dt_attr_min(lp->dn_attr, rp->dn_attr));
20487c478bd9Sstevel@tonic-gate 			break;
20497c478bd9Sstevel@tonic-gate 		case DT_TOK_RSH:
20507c478bd9Sstevel@tonic-gate 			dnp->dn_value = l >> r;
20517c478bd9Sstevel@tonic-gate 			dt_node_type_propagate(lp, dnp);
20527c478bd9Sstevel@tonic-gate 			dt_node_attr_assign(rp,
20537c478bd9Sstevel@tonic-gate 			    dt_attr_min(lp->dn_attr, rp->dn_attr));
20547c478bd9Sstevel@tonic-gate 			break;
20557c478bd9Sstevel@tonic-gate 		case DT_TOK_ADD:
20567c478bd9Sstevel@tonic-gate 			dnp->dn_value = l + r;
20577c478bd9Sstevel@tonic-gate 			dt_node_promote(lp, rp, dnp);
20587c478bd9Sstevel@tonic-gate 			break;
20597c478bd9Sstevel@tonic-gate 		case DT_TOK_SUB:
20607c478bd9Sstevel@tonic-gate 			dnp->dn_value = l - r;
20617c478bd9Sstevel@tonic-gate 			dt_node_promote(lp, rp, dnp);
20627c478bd9Sstevel@tonic-gate 			break;
20637c478bd9Sstevel@tonic-gate 		case DT_TOK_MUL:
20647c478bd9Sstevel@tonic-gate 			dnp->dn_value = l * r;
20657c478bd9Sstevel@tonic-gate 			dt_node_promote(lp, rp, dnp);
20667c478bd9Sstevel@tonic-gate 			break;
20677c478bd9Sstevel@tonic-gate 		case DT_TOK_DIV:
20687c478bd9Sstevel@tonic-gate 			dt_node_promote(lp, rp, dnp);
20697c478bd9Sstevel@tonic-gate 			if (dnp->dn_flags & DT_NF_SIGNED)
20707c478bd9Sstevel@tonic-gate 				dnp->dn_value = (intmax_t)l / (intmax_t)r;
20717c478bd9Sstevel@tonic-gate 			else
20727c478bd9Sstevel@tonic-gate 				dnp->dn_value = l / r;
20737c478bd9Sstevel@tonic-gate 			break;
20747c478bd9Sstevel@tonic-gate 		case DT_TOK_MOD:
20757c478bd9Sstevel@tonic-gate 			dt_node_promote(lp, rp, dnp);
20767c478bd9Sstevel@tonic-gate 			if (dnp->dn_flags & DT_NF_SIGNED)
20777c478bd9Sstevel@tonic-gate 				dnp->dn_value = (intmax_t)l % (intmax_t)r;
20787c478bd9Sstevel@tonic-gate 			else
20797c478bd9Sstevel@tonic-gate 				dnp->dn_value = l % r;
20807c478bd9Sstevel@tonic-gate 			break;
20817c478bd9Sstevel@tonic-gate 		default:
20827c478bd9Sstevel@tonic-gate 			dt_node_free(dnp);
20837c478bd9Sstevel@tonic-gate 			dnp = NULL;
20847c478bd9Sstevel@tonic-gate 		}
20857c478bd9Sstevel@tonic-gate 
20867c478bd9Sstevel@tonic-gate 		if (dnp != NULL) {
20877c478bd9Sstevel@tonic-gate 			dt_node_free(lp);
20887c478bd9Sstevel@tonic-gate 			dt_node_free(rp);
20897c478bd9Sstevel@tonic-gate 			return (dnp);
20907c478bd9Sstevel@tonic-gate 		}
20917c478bd9Sstevel@tonic-gate 	}
20927c478bd9Sstevel@tonic-gate 
20937c478bd9Sstevel@tonic-gate 	if (op == DT_TOK_LPAR && rp->dn_kind == DT_NODE_INT &&
20947c478bd9Sstevel@tonic-gate 	    dt_node_is_integer(lp)) {
2095e5803b76SAdam H. Leventhal 		dt_cast(lp, rp);
20967c478bd9Sstevel@tonic-gate 		dt_node_type_propagate(lp, rp);
20977c478bd9Sstevel@tonic-gate 		dt_node_attr_assign(rp, dt_attr_min(lp->dn_attr, rp->dn_attr));
20987c478bd9Sstevel@tonic-gate 		dt_node_free(lp);
20997c478bd9Sstevel@tonic-gate 
21007c478bd9Sstevel@tonic-gate 		return (rp);
21017c478bd9Sstevel@tonic-gate 	}
21027c478bd9Sstevel@tonic-gate 
21037c478bd9Sstevel@tonic-gate 	/*
21047c478bd9Sstevel@tonic-gate 	 * If no immediate optimizations are available, create an new OP2 node
21057c478bd9Sstevel@tonic-gate 	 * and glue the left and right children into place and return.
21067c478bd9Sstevel@tonic-gate 	 */
21077c478bd9Sstevel@tonic-gate 	dnp = dt_node_alloc(DT_NODE_OP2);
21087c478bd9Sstevel@tonic-gate 	assert(op <= USHRT_MAX);
21097c478bd9Sstevel@tonic-gate 	dnp->dn_op = (ushort_t)op;
21107c478bd9Sstevel@tonic-gate 	dnp->dn_left = lp;
21117c478bd9Sstevel@tonic-gate 	dnp->dn_right = rp;
21127c478bd9Sstevel@tonic-gate 
21137c478bd9Sstevel@tonic-gate 	return (dnp);
21147c478bd9Sstevel@tonic-gate }
21157c478bd9Sstevel@tonic-gate 
21167c478bd9Sstevel@tonic-gate dt_node_t *
dt_node_op3(dt_node_t * expr,dt_node_t * lp,dt_node_t * rp)21177c478bd9Sstevel@tonic-gate dt_node_op3(dt_node_t *expr, dt_node_t *lp, dt_node_t *rp)
21187c478bd9Sstevel@tonic-gate {
21197c478bd9Sstevel@tonic-gate 	dt_node_t *dnp;
21207c478bd9Sstevel@tonic-gate 
21217c478bd9Sstevel@tonic-gate 	if (expr->dn_kind == DT_NODE_INT)
21227c478bd9Sstevel@tonic-gate 		return (expr->dn_value != 0 ? lp : rp);
21237c478bd9Sstevel@tonic-gate 
21247c478bd9Sstevel@tonic-gate 	dnp = dt_node_alloc(DT_NODE_OP3);
21257c478bd9Sstevel@tonic-gate 	dnp->dn_op = DT_TOK_QUESTION;
21267c478bd9Sstevel@tonic-gate 	dnp->dn_expr = expr;
21277c478bd9Sstevel@tonic-gate 	dnp->dn_left = lp;
21287c478bd9Sstevel@tonic-gate 	dnp->dn_right = rp;
21297c478bd9Sstevel@tonic-gate 
21307c478bd9Sstevel@tonic-gate 	return (dnp);
21317c478bd9Sstevel@tonic-gate }
21327c478bd9Sstevel@tonic-gate 
21337c478bd9Sstevel@tonic-gate dt_node_t *
dt_node_statement(dt_node_t * expr)21347c478bd9Sstevel@tonic-gate dt_node_statement(dt_node_t *expr)
21357c478bd9Sstevel@tonic-gate {
21367c478bd9Sstevel@tonic-gate 	dt_node_t *dnp;
21377c478bd9Sstevel@tonic-gate 
21387c478bd9Sstevel@tonic-gate 	if (expr->dn_kind == DT_NODE_AGG)
21397c478bd9Sstevel@tonic-gate 		return (expr);
21407c478bd9Sstevel@tonic-gate 
21417c478bd9Sstevel@tonic-gate 	if (expr->dn_kind == DT_NODE_FUNC &&
21427c478bd9Sstevel@tonic-gate 	    expr->dn_ident->di_kind == DT_IDENT_ACTFUNC)
21437c478bd9Sstevel@tonic-gate 		dnp = dt_node_alloc(DT_NODE_DFUNC);
21447c478bd9Sstevel@tonic-gate 	else
21457c478bd9Sstevel@tonic-gate 		dnp = dt_node_alloc(DT_NODE_DEXPR);
21467c478bd9Sstevel@tonic-gate 
21477c478bd9Sstevel@tonic-gate 	dnp->dn_expr = expr;
21487c478bd9Sstevel@tonic-gate 	return (dnp);
21497c478bd9Sstevel@tonic-gate }
21507c478bd9Sstevel@tonic-gate 
21517c478bd9Sstevel@tonic-gate dt_node_t *
dt_node_if(dt_node_t * pred,dt_node_t * acts,dt_node_t * else_acts)2152c3bd3abdSMatthew Ahrens dt_node_if(dt_node_t *pred, dt_node_t *acts, dt_node_t *else_acts)
2153c3bd3abdSMatthew Ahrens {
2154c3bd3abdSMatthew Ahrens 	dt_node_t *dnp = dt_node_alloc(DT_NODE_IF);
2155c3bd3abdSMatthew Ahrens 	dnp->dn_conditional = pred;
2156c3bd3abdSMatthew Ahrens 	dnp->dn_body = acts;
2157c3bd3abdSMatthew Ahrens 	dnp->dn_alternate_body = else_acts;
2158c3bd3abdSMatthew Ahrens 
2159c3bd3abdSMatthew Ahrens 	return (dnp);
2160c3bd3abdSMatthew Ahrens }
2161c3bd3abdSMatthew Ahrens 
2162c3bd3abdSMatthew Ahrens dt_node_t *
dt_node_pdesc_by_name(char * spec)21637c478bd9Sstevel@tonic-gate dt_node_pdesc_by_name(char *spec)
21647c478bd9Sstevel@tonic-gate {
21657c478bd9Sstevel@tonic-gate 	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
21667c478bd9Sstevel@tonic-gate 	dt_node_t *dnp;
21677c478bd9Sstevel@tonic-gate 
21687c478bd9Sstevel@tonic-gate 	if (spec == NULL)
21697c478bd9Sstevel@tonic-gate 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
21707c478bd9Sstevel@tonic-gate 
21717c478bd9Sstevel@tonic-gate 	dnp = dt_node_alloc(DT_NODE_PDESC);
21727c478bd9Sstevel@tonic-gate 	dnp->dn_spec = spec;
21737c478bd9Sstevel@tonic-gate 	dnp->dn_desc = malloc(sizeof (dtrace_probedesc_t));
21747c478bd9Sstevel@tonic-gate 
21757c478bd9Sstevel@tonic-gate 	if (dnp->dn_desc == NULL)
21767c478bd9Sstevel@tonic-gate 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
21777c478bd9Sstevel@tonic-gate 
21787c478bd9Sstevel@tonic-gate 	if (dtrace_xstr2desc(dtp, yypcb->pcb_pspec, dnp->dn_spec,
21797c478bd9Sstevel@tonic-gate 	    yypcb->pcb_sargc, yypcb->pcb_sargv, dnp->dn_desc) != 0) {
21807c478bd9Sstevel@tonic-gate 		xyerror(D_PDESC_INVAL, "invalid probe description \"%s\": %s\n",
21817c478bd9Sstevel@tonic-gate 		    dnp->dn_spec, dtrace_errmsg(dtp, dtrace_errno(dtp)));
21827c478bd9Sstevel@tonic-gate 	}
21837c478bd9Sstevel@tonic-gate 
21847c478bd9Sstevel@tonic-gate 	free(dnp->dn_spec);
21857c478bd9Sstevel@tonic-gate 	dnp->dn_spec = NULL;
21867c478bd9Sstevel@tonic-gate 
21877c478bd9Sstevel@tonic-gate 	return (dnp);
21887c478bd9Sstevel@tonic-gate }
21897c478bd9Sstevel@tonic-gate 
21907c478bd9Sstevel@tonic-gate dt_node_t *
dt_node_pdesc_by_id(uintmax_t id)21917c478bd9Sstevel@tonic-gate dt_node_pdesc_by_id(uintmax_t id)
21927c478bd9Sstevel@tonic-gate {
21937c478bd9Sstevel@tonic-gate 	static const char *const names[] = {
21947c478bd9Sstevel@tonic-gate 		"providers", "modules", "functions"
21957c478bd9Sstevel@tonic-gate 	};
21967c478bd9Sstevel@tonic-gate 
21977c478bd9Sstevel@tonic-gate 	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
21987c478bd9Sstevel@tonic-gate 	dt_node_t *dnp = dt_node_alloc(DT_NODE_PDESC);
21997c478bd9Sstevel@tonic-gate 
22007c478bd9Sstevel@tonic-gate 	if ((dnp->dn_desc = malloc(sizeof (dtrace_probedesc_t))) == NULL)
22017c478bd9Sstevel@tonic-gate 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
22027c478bd9Sstevel@tonic-gate 
22037c478bd9Sstevel@tonic-gate 	if (id > UINT_MAX) {
22047c478bd9Sstevel@tonic-gate 		xyerror(D_PDESC_INVAL, "identifier %llu exceeds maximum "
22057c478bd9Sstevel@tonic-gate 		    "probe id\n", (u_longlong_t)id);
22067c478bd9Sstevel@tonic-gate 	}
22077c478bd9Sstevel@tonic-gate 
22087c478bd9Sstevel@tonic-gate 	if (yypcb->pcb_pspec != DTRACE_PROBESPEC_NAME) {
22097c478bd9Sstevel@tonic-gate 		xyerror(D_PDESC_INVAL, "probe identifier %llu not permitted "
22107c478bd9Sstevel@tonic-gate 		    "when specifying %s\n", (u_longlong_t)id,
22117c478bd9Sstevel@tonic-gate 		    names[yypcb->pcb_pspec]);
22127c478bd9Sstevel@tonic-gate 	}
22137c478bd9Sstevel@tonic-gate 
22147c478bd9Sstevel@tonic-gate 	if (dtrace_id2desc(dtp, (dtrace_id_t)id, dnp->dn_desc) != 0) {
22157c478bd9Sstevel@tonic-gate 		xyerror(D_PDESC_INVAL, "invalid probe identifier %llu: %s\n",
22167c478bd9Sstevel@tonic-gate 		    (u_longlong_t)id, dtrace_errmsg(dtp, dtrace_errno(dtp)));
22177c478bd9Sstevel@tonic-gate 	}
22187c478bd9Sstevel@tonic-gate 
22197c478bd9Sstevel@tonic-gate 	return (dnp);
22207c478bd9Sstevel@tonic-gate }
22217c478bd9Sstevel@tonic-gate 
22227c478bd9Sstevel@tonic-gate dt_node_t *
dt_node_clause(dt_node_t * pdescs,dt_node_t * pred,dt_node_t * acts)22237c478bd9Sstevel@tonic-gate dt_node_clause(dt_node_t *pdescs, dt_node_t *pred, dt_node_t *acts)
22247c478bd9Sstevel@tonic-gate {
22257c478bd9Sstevel@tonic-gate 	dt_node_t *dnp = dt_node_alloc(DT_NODE_CLAUSE);
22267c478bd9Sstevel@tonic-gate 
22277c478bd9Sstevel@tonic-gate 	dnp->dn_pdescs = pdescs;
22287c478bd9Sstevel@tonic-gate 	dnp->dn_pred = pred;
22297c478bd9Sstevel@tonic-gate 	dnp->dn_acts = acts;
22307c478bd9Sstevel@tonic-gate 
22317c478bd9Sstevel@tonic-gate 	return (dnp);
22327c478bd9Sstevel@tonic-gate }
22337c478bd9Sstevel@tonic-gate 
22347c478bd9Sstevel@tonic-gate dt_node_t *
dt_node_inline(dt_node_t * expr)22357c478bd9Sstevel@tonic-gate dt_node_inline(dt_node_t *expr)
22367c478bd9Sstevel@tonic-gate {
22377c478bd9Sstevel@tonic-gate 	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
22387c478bd9Sstevel@tonic-gate 	dt_scope_t *dsp = &yypcb->pcb_dstack;
22397c478bd9Sstevel@tonic-gate 	dt_decl_t *ddp = dt_decl_top();
22407c478bd9Sstevel@tonic-gate 
22417c478bd9Sstevel@tonic-gate 	char n[DT_TYPE_NAMELEN];
22427c478bd9Sstevel@tonic-gate 	dtrace_typeinfo_t dtt;
22437c478bd9Sstevel@tonic-gate 
22447c478bd9Sstevel@tonic-gate 	dt_ident_t *idp, *rdp;
22457c478bd9Sstevel@tonic-gate 	dt_idnode_t *inp;
22467c478bd9Sstevel@tonic-gate 	dt_node_t *dnp;
22477c478bd9Sstevel@tonic-gate 
22487c478bd9Sstevel@tonic-gate 	if (dt_decl_type(ddp, &dtt) != 0)
22497c478bd9Sstevel@tonic-gate 		longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
22507c478bd9Sstevel@tonic-gate 
22517c478bd9Sstevel@tonic-gate 	if (dsp->ds_class != DT_DC_DEFAULT) {
22527c478bd9Sstevel@tonic-gate 		xyerror(D_DECL_BADCLASS, "specified storage class not "
22537c478bd9Sstevel@tonic-gate 		    "appropriate for inline declaration\n");
22547c478bd9Sstevel@tonic-gate 	}
22557c478bd9Sstevel@tonic-gate 
22567c478bd9Sstevel@tonic-gate 	if (dsp->ds_ident == NULL)
22577c478bd9Sstevel@tonic-gate 		xyerror(D_DECL_USELESS, "inline declaration requires a name\n");
22587c478bd9Sstevel@tonic-gate 
22597c478bd9Sstevel@tonic-gate 	if ((idp = dt_idstack_lookup(
22607c478bd9Sstevel@tonic-gate 	    &yypcb->pcb_globals, dsp->ds_ident)) != NULL) {
22617c478bd9Sstevel@tonic-gate 		xyerror(D_DECL_IDRED, "identifier redefined: %s\n\t current: "
22627c478bd9Sstevel@tonic-gate 		    "inline definition\n\tprevious: %s %s\n",
22637c478bd9Sstevel@tonic-gate 		    idp->di_name, dt_idkind_name(idp->di_kind),
22647c478bd9Sstevel@tonic-gate 		    (idp->di_flags & DT_IDFLG_INLINE) ? "inline" : "");
22657c478bd9Sstevel@tonic-gate 	}
22667c478bd9Sstevel@tonic-gate 
22677c478bd9Sstevel@tonic-gate 	/*
22687c478bd9Sstevel@tonic-gate 	 * If we are declaring an inlined array, verify that we have a tuple
22697c478bd9Sstevel@tonic-gate 	 * signature, and then recompute 'dtt' as the array's value type.
22707c478bd9Sstevel@tonic-gate 	 */
22717c478bd9Sstevel@tonic-gate 	if (ddp->dd_kind == CTF_K_ARRAY) {
22727c478bd9Sstevel@tonic-gate 		if (ddp->dd_node == NULL) {
22737c478bd9Sstevel@tonic-gate 			xyerror(D_DECL_ARRNULL, "inline declaration requires "
22747c478bd9Sstevel@tonic-gate 			    "array tuple signature: %s\n", dsp->ds_ident);
22757c478bd9Sstevel@tonic-gate 		}
22767c478bd9Sstevel@tonic-gate 
22777c478bd9Sstevel@tonic-gate 		if (ddp->dd_node->dn_kind != DT_NODE_TYPE) {
22787c478bd9Sstevel@tonic-gate 			xyerror(D_DECL_ARRNULL, "inline declaration cannot be "
22797c478bd9Sstevel@tonic-gate 			    "of scalar array type: %s\n", dsp->ds_ident);
22807c478bd9Sstevel@tonic-gate 		}
22817c478bd9Sstevel@tonic-gate 
22827c478bd9Sstevel@tonic-gate 		if (dt_decl_type(ddp->dd_next, &dtt) != 0)
22837c478bd9Sstevel@tonic-gate 			longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
22847c478bd9Sstevel@tonic-gate 	}
22857c478bd9Sstevel@tonic-gate 
22867c478bd9Sstevel@tonic-gate 	/*
22877c478bd9Sstevel@tonic-gate 	 * If the inline identifier is not defined, then create it with the
22887c478bd9Sstevel@tonic-gate 	 * orphan flag set.  We do not insert the identifier into dt_globals
22897c478bd9Sstevel@tonic-gate 	 * until we have successfully cooked the right-hand expression, below.
22907c478bd9Sstevel@tonic-gate 	 */
22917c478bd9Sstevel@tonic-gate 	dnp = dt_node_alloc(DT_NODE_INLINE);
2292a386cc11SRobert Mustacchi 	dt_node_type_assign(dnp, dtt.dtt_ctfp, dtt.dtt_type, B_FALSE);
22937c478bd9Sstevel@tonic-gate 	dt_node_attr_assign(dnp, _dtrace_defattr);
22947c478bd9Sstevel@tonic-gate 
22957c478bd9Sstevel@tonic-gate 	if (dt_node_is_void(dnp)) {
22967c478bd9Sstevel@tonic-gate 		xyerror(D_DECL_VOIDOBJ,
22977c478bd9Sstevel@tonic-gate 		    "cannot declare void inline: %s\n", dsp->ds_ident);
22987c478bd9Sstevel@tonic-gate 	}
22997c478bd9Sstevel@tonic-gate 
23007c478bd9Sstevel@tonic-gate 	if (ctf_type_kind(dnp->dn_ctfp, ctf_type_resolve(
23017c478bd9Sstevel@tonic-gate 	    dnp->dn_ctfp, dnp->dn_type)) == CTF_K_FORWARD) {
23027c478bd9Sstevel@tonic-gate 		xyerror(D_DECL_INCOMPLETE,
23037c478bd9Sstevel@tonic-gate 		    "incomplete struct/union/enum %s: %s\n",
23047c478bd9Sstevel@tonic-gate 		    dt_node_type_name(dnp, n, sizeof (n)), dsp->ds_ident);
23057c478bd9Sstevel@tonic-gate 	}
23067c478bd9Sstevel@tonic-gate 
23077c478bd9Sstevel@tonic-gate 	if ((inp = malloc(sizeof (dt_idnode_t))) == NULL)
23087c478bd9Sstevel@tonic-gate 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
23097c478bd9Sstevel@tonic-gate 
23107c478bd9Sstevel@tonic-gate 	bzero(inp, sizeof (dt_idnode_t));
23117c478bd9Sstevel@tonic-gate 
23127c478bd9Sstevel@tonic-gate 	idp = dnp->dn_ident = dt_ident_create(dsp->ds_ident,
23137c478bd9Sstevel@tonic-gate 	    ddp->dd_kind == CTF_K_ARRAY ? DT_IDENT_ARRAY : DT_IDENT_SCALAR,
23147c478bd9Sstevel@tonic-gate 	    DT_IDFLG_INLINE | DT_IDFLG_REF | DT_IDFLG_DECL | DT_IDFLG_ORPHAN, 0,
23157c478bd9Sstevel@tonic-gate 	    _dtrace_defattr, 0, &dt_idops_inline, inp, dtp->dt_gen);
23167c478bd9Sstevel@tonic-gate 
23177c478bd9Sstevel@tonic-gate 	if (idp == NULL) {
23187c478bd9Sstevel@tonic-gate 		free(inp);
23197c478bd9Sstevel@tonic-gate 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
23207c478bd9Sstevel@tonic-gate 	}
23217c478bd9Sstevel@tonic-gate 
23227c478bd9Sstevel@tonic-gate 	/*
23237c478bd9Sstevel@tonic-gate 	 * If we're inlining an associative array, create a private identifier
23247c478bd9Sstevel@tonic-gate 	 * hash containing the named parameters and store it in inp->din_hash.
23257c478bd9Sstevel@tonic-gate 	 * We then push this hash on to the top of the pcb_globals stack.
23267c478bd9Sstevel@tonic-gate 	 */
23277c478bd9Sstevel@tonic-gate 	if (ddp->dd_kind == CTF_K_ARRAY) {
23287c478bd9Sstevel@tonic-gate 		dt_idnode_t *pinp;
23297c478bd9Sstevel@tonic-gate 		dt_ident_t *pidp;
23307c478bd9Sstevel@tonic-gate 		dt_node_t *pnp;
23317c478bd9Sstevel@tonic-gate 		uint_t i = 0;
23327c478bd9Sstevel@tonic-gate 
23337c478bd9Sstevel@tonic-gate 		for (pnp = ddp->dd_node; pnp != NULL; pnp = pnp->dn_list)
23347c478bd9Sstevel@tonic-gate 			i++; /* count up parameters for din_argv[] */
23357c478bd9Sstevel@tonic-gate 
23367c478bd9Sstevel@tonic-gate 		inp->din_hash = dt_idhash_create("inline args", NULL, 0, 0);
23377c478bd9Sstevel@tonic-gate 		inp->din_argv = calloc(i, sizeof (dt_ident_t *));
23387c478bd9Sstevel@tonic-gate 
23397c478bd9Sstevel@tonic-gate 		if (inp->din_hash == NULL || inp->din_argv == NULL)
23407c478bd9Sstevel@tonic-gate 			longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
23417c478bd9Sstevel@tonic-gate 
23427c478bd9Sstevel@tonic-gate 		/*
23437c478bd9Sstevel@tonic-gate 		 * Create an identifier for each parameter as a scalar inline,
23447c478bd9Sstevel@tonic-gate 		 * and store it in din_hash and in position in din_argv[].  The
23457c478bd9Sstevel@tonic-gate 		 * parameter identifiers also use dt_idops_inline, but we leave
23467c478bd9Sstevel@tonic-gate 		 * the dt_idnode_t argument 'pinp' zeroed.  This will be filled
23477c478bd9Sstevel@tonic-gate 		 * in by the code generation pass with references to the args.
23487c478bd9Sstevel@tonic-gate 		 */
23497c478bd9Sstevel@tonic-gate 		for (i = 0, pnp = ddp->dd_node;
23507c478bd9Sstevel@tonic-gate 		    pnp != NULL; pnp = pnp->dn_list, i++) {
23517c478bd9Sstevel@tonic-gate 
23527c478bd9Sstevel@tonic-gate 			if (pnp->dn_string == NULL)
23537c478bd9Sstevel@tonic-gate 				continue; /* ignore anonymous parameters */
23547c478bd9Sstevel@tonic-gate 
23557c478bd9Sstevel@tonic-gate 			if ((pinp = malloc(sizeof (dt_idnode_t))) == NULL)
23567c478bd9Sstevel@tonic-gate 				longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
23577c478bd9Sstevel@tonic-gate 
23587c478bd9Sstevel@tonic-gate 			pidp = dt_idhash_insert(inp->din_hash, pnp->dn_string,
23597c478bd9Sstevel@tonic-gate 			    DT_IDENT_SCALAR, DT_IDFLG_DECL | DT_IDFLG_INLINE, 0,
23607c478bd9Sstevel@tonic-gate 			    _dtrace_defattr, 0, &dt_idops_inline,
23617c478bd9Sstevel@tonic-gate 			    pinp, dtp->dt_gen);
23627c478bd9Sstevel@tonic-gate 
23637c478bd9Sstevel@tonic-gate 			if (pidp == NULL) {
23647c478bd9Sstevel@tonic-gate 				free(pinp);
23657c478bd9Sstevel@tonic-gate 				longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
23667c478bd9Sstevel@tonic-gate 			}
23677c478bd9Sstevel@tonic-gate 
23687c478bd9Sstevel@tonic-gate 			inp->din_argv[i] = pidp;
23697c478bd9Sstevel@tonic-gate 			bzero(pinp, sizeof (dt_idnode_t));
23707c478bd9Sstevel@tonic-gate 			dt_ident_type_assign(pidp, pnp->dn_ctfp, pnp->dn_type);
23717c478bd9Sstevel@tonic-gate 		}
23727c478bd9Sstevel@tonic-gate 
23737c478bd9Sstevel@tonic-gate 		dt_idstack_push(&yypcb->pcb_globals, inp->din_hash);
23747c478bd9Sstevel@tonic-gate 	}
23757c478bd9Sstevel@tonic-gate 
23767c478bd9Sstevel@tonic-gate 	/*
23777c478bd9Sstevel@tonic-gate 	 * Unlike most constructors, we need to explicitly cook the right-hand
23787c478bd9Sstevel@tonic-gate 	 * side of the inline definition immediately to prevent recursion.  If
23797c478bd9Sstevel@tonic-gate 	 * the right-hand side uses the inline itself, the cook will fail.
23807c478bd9Sstevel@tonic-gate 	 */
23817c478bd9Sstevel@tonic-gate 	expr = dt_node_cook(expr, DT_IDFLG_REF);
23827c478bd9Sstevel@tonic-gate 
23837c478bd9Sstevel@tonic-gate 	if (ddp->dd_kind == CTF_K_ARRAY)
23847c478bd9Sstevel@tonic-gate 		dt_idstack_pop(&yypcb->pcb_globals, inp->din_hash);
23857c478bd9Sstevel@tonic-gate 
23867c478bd9Sstevel@tonic-gate 	/*
23877c478bd9Sstevel@tonic-gate 	 * Set the type, attributes, and flags for the inline.  If the right-
23887c478bd9Sstevel@tonic-gate 	 * hand expression has an identifier, propagate its flags.  Then cook
23897c478bd9Sstevel@tonic-gate 	 * the identifier to fully initialize it: if we're declaring an inline
23907c478bd9Sstevel@tonic-gate 	 * associative array this will construct a type signature from 'ddp'.
23917c478bd9Sstevel@tonic-gate 	 */
23927c478bd9Sstevel@tonic-gate 	if (dt_node_is_dynamic(expr))
23937c478bd9Sstevel@tonic-gate 		rdp = dt_ident_resolve(expr->dn_ident);
23947c478bd9Sstevel@tonic-gate 	else if (expr->dn_kind == DT_NODE_VAR || expr->dn_kind == DT_NODE_SYM)
23957c478bd9Sstevel@tonic-gate 		rdp = expr->dn_ident;
23967c478bd9Sstevel@tonic-gate 	else
23977c478bd9Sstevel@tonic-gate 		rdp = NULL;
23987c478bd9Sstevel@tonic-gate 
23997c478bd9Sstevel@tonic-gate 	if (rdp != NULL) {
24007c478bd9Sstevel@tonic-gate 		idp->di_flags |= (rdp->di_flags &
24017c478bd9Sstevel@tonic-gate 		    (DT_IDFLG_WRITE | DT_IDFLG_USER | DT_IDFLG_PRIM));
24027c478bd9Sstevel@tonic-gate 	}
24037c478bd9Sstevel@tonic-gate 
24047c478bd9Sstevel@tonic-gate 	idp->di_attr = dt_attr_min(_dtrace_defattr, expr->dn_attr);
24057c478bd9Sstevel@tonic-gate 	dt_ident_type_assign(idp, dtt.dtt_ctfp, dtt.dtt_type);
24067c478bd9Sstevel@tonic-gate 	(void) dt_ident_cook(dnp, idp, &ddp->dd_node);
24077c478bd9Sstevel@tonic-gate 
24087c478bd9Sstevel@tonic-gate 	/*
24097c478bd9Sstevel@tonic-gate 	 * Store the parse tree nodes for 'expr' inside of idp->di_data ('inp')
24107c478bd9Sstevel@tonic-gate 	 * so that they will be preserved with this identifier.  Then pop the
24117c478bd9Sstevel@tonic-gate 	 * inline declaration from the declaration stack and restore the lexer.
24127c478bd9Sstevel@tonic-gate 	 */
24137c478bd9Sstevel@tonic-gate 	inp->din_list = yypcb->pcb_list;
24147c478bd9Sstevel@tonic-gate 	inp->din_root = expr;
24157c478bd9Sstevel@tonic-gate 
24167c478bd9Sstevel@tonic-gate 	dt_decl_free(dt_decl_pop());
24177c478bd9Sstevel@tonic-gate 	yybegin(YYS_CLAUSE);
24187c478bd9Sstevel@tonic-gate 
24197c478bd9Sstevel@tonic-gate 	/*
24207c478bd9Sstevel@tonic-gate 	 * Finally, insert the inline identifier into dt_globals to make it
24217c478bd9Sstevel@tonic-gate 	 * visible, and then cook 'dnp' to check its type against 'expr'.
24227c478bd9Sstevel@tonic-gate 	 */
24237c478bd9Sstevel@tonic-gate 	dt_idhash_xinsert(dtp->dt_globals, idp);
24247c478bd9Sstevel@tonic-gate 	return (dt_node_cook(dnp, DT_IDFLG_REF));
24257c478bd9Sstevel@tonic-gate }
24267c478bd9Sstevel@tonic-gate 
24277c478bd9Sstevel@tonic-gate dt_node_t *
dt_node_member(dt_decl_t * ddp,char * name,dt_node_t * expr)24287c478bd9Sstevel@tonic-gate dt_node_member(dt_decl_t *ddp, char *name, dt_node_t *expr)
24297c478bd9Sstevel@tonic-gate {
24307c478bd9Sstevel@tonic-gate 	dtrace_typeinfo_t dtt;
24317c478bd9Sstevel@tonic-gate 	dt_node_t *dnp;
24327c478bd9Sstevel@tonic-gate 	int err;
24337c478bd9Sstevel@tonic-gate 
24347c478bd9Sstevel@tonic-gate 	if (ddp != NULL) {
24357c478bd9Sstevel@tonic-gate 		err = dt_decl_type(ddp, &dtt);
24367c478bd9Sstevel@tonic-gate 		dt_decl_free(ddp);
24377c478bd9Sstevel@tonic-gate 
24387c478bd9Sstevel@tonic-gate 		if (err != 0)
24397c478bd9Sstevel@tonic-gate 			longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
24407c478bd9Sstevel@tonic-gate 	}
24417c478bd9Sstevel@tonic-gate 
24427c478bd9Sstevel@tonic-gate 	dnp = dt_node_alloc(DT_NODE_MEMBER);
24437c478bd9Sstevel@tonic-gate 	dnp->dn_membname = name;
24447c478bd9Sstevel@tonic-gate 	dnp->dn_membexpr = expr;
24457c478bd9Sstevel@tonic-gate 
24467c478bd9Sstevel@tonic-gate 	if (ddp != NULL)
2447a386cc11SRobert Mustacchi 		dt_node_type_assign(dnp, dtt.dtt_ctfp, dtt.dtt_type,
2448a386cc11SRobert Mustacchi 		    dtt.dtt_flags);
24497c478bd9Sstevel@tonic-gate 
24507c478bd9Sstevel@tonic-gate 	return (dnp);
24517c478bd9Sstevel@tonic-gate }
24527c478bd9Sstevel@tonic-gate 
24537c478bd9Sstevel@tonic-gate dt_node_t *
dt_node_xlator(dt_decl_t * ddp,dt_decl_t * sdp,char * name,dt_node_t * members)24547c478bd9Sstevel@tonic-gate dt_node_xlator(dt_decl_t *ddp, dt_decl_t *sdp, char *name, dt_node_t *members)
24557c478bd9Sstevel@tonic-gate {
24567c478bd9Sstevel@tonic-gate 	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
24577c478bd9Sstevel@tonic-gate 	dtrace_typeinfo_t src, dst;
24587c478bd9Sstevel@tonic-gate 	dt_node_t sn, dn;
24597c478bd9Sstevel@tonic-gate 	dt_xlator_t *dxp;
24607c478bd9Sstevel@tonic-gate 	dt_node_t *dnp;
24617c478bd9Sstevel@tonic-gate 	int edst, esrc;
24621a7c1b72Smws 	uint_t kind;
24637c478bd9Sstevel@tonic-gate 
24647c478bd9Sstevel@tonic-gate 	char n1[DT_TYPE_NAMELEN];
24657c478bd9Sstevel@tonic-gate 	char n2[DT_TYPE_NAMELEN];
24667c478bd9Sstevel@tonic-gate 
24677c478bd9Sstevel@tonic-gate 	edst = dt_decl_type(ddp, &dst);
24687c478bd9Sstevel@tonic-gate 	dt_decl_free(ddp);
24697c478bd9Sstevel@tonic-gate 
24707c478bd9Sstevel@tonic-gate 	esrc = dt_decl_type(sdp, &src);
24717c478bd9Sstevel@tonic-gate 	dt_decl_free(sdp);
24727c478bd9Sstevel@tonic-gate 
24737c478bd9Sstevel@tonic-gate 	if (edst != 0 || esrc != 0) {
24747c478bd9Sstevel@tonic-gate 		free(name);
24757c478bd9Sstevel@tonic-gate 		longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
24767c478bd9Sstevel@tonic-gate 	}
24777c478bd9Sstevel@tonic-gate 
24787c478bd9Sstevel@tonic-gate 	bzero(&sn, sizeof (sn));
2479a386cc11SRobert Mustacchi 	dt_node_type_assign(&sn, src.dtt_ctfp, src.dtt_type, B_FALSE);
24807c478bd9Sstevel@tonic-gate 
24817c478bd9Sstevel@tonic-gate 	bzero(&dn, sizeof (dn));
2482a386cc11SRobert Mustacchi 	dt_node_type_assign(&dn, dst.dtt_ctfp, dst.dtt_type, B_FALSE);
24837c478bd9Sstevel@tonic-gate 
24847c478bd9Sstevel@tonic-gate 	if (dt_xlator_lookup(dtp, &sn, &dn, DT_XLATE_EXACT) != NULL) {
24857c478bd9Sstevel@tonic-gate 		xyerror(D_XLATE_REDECL,
24867c478bd9Sstevel@tonic-gate 		    "translator from %s to %s has already been declared\n",
24877c478bd9Sstevel@tonic-gate 		    dt_node_type_name(&sn, n1, sizeof (n1)),
24887c478bd9Sstevel@tonic-gate 		    dt_node_type_name(&dn, n2, sizeof (n2)));
24897c478bd9Sstevel@tonic-gate 	}
24907c478bd9Sstevel@tonic-gate 
24911a7c1b72Smws 	kind = ctf_type_kind(dst.dtt_ctfp,
24921a7c1b72Smws 	    ctf_type_resolve(dst.dtt_ctfp, dst.dtt_type));
24931a7c1b72Smws 
24941a7c1b72Smws 	if (kind == CTF_K_FORWARD) {
24951a7c1b72Smws 		xyerror(D_XLATE_SOU, "incomplete struct/union/enum %s\n",
24961a7c1b72Smws 		    dt_type_name(dst.dtt_ctfp, dst.dtt_type, n1, sizeof (n1)));
24971a7c1b72Smws 	}
24981a7c1b72Smws 
24991a7c1b72Smws 	if (kind != CTF_K_STRUCT && kind != CTF_K_UNION) {
25001a7c1b72Smws 		xyerror(D_XLATE_SOU,
25011a7c1b72Smws 		    "translator output type must be a struct or union\n");
25021a7c1b72Smws 	}
25031a7c1b72Smws 
25047c478bd9Sstevel@tonic-gate 	dxp = dt_xlator_create(dtp, &src, &dst, name, members, yypcb->pcb_list);
25057c478bd9Sstevel@tonic-gate 	yybegin(YYS_CLAUSE);
25067c478bd9Sstevel@tonic-gate 	free(name);
25077c478bd9Sstevel@tonic-gate 
25087c478bd9Sstevel@tonic-gate 	if (dxp == NULL)
25097c478bd9Sstevel@tonic-gate 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
25107c478bd9Sstevel@tonic-gate 
25117c478bd9Sstevel@tonic-gate 	dnp = dt_node_alloc(DT_NODE_XLATOR);
25127c478bd9Sstevel@tonic-gate 	dnp->dn_xlator = dxp;
25137c478bd9Sstevel@tonic-gate 	dnp->dn_members = members;
25147c478bd9Sstevel@tonic-gate 
25157c478bd9Sstevel@tonic-gate 	return (dt_node_cook(dnp, DT_IDFLG_REF));
25167c478bd9Sstevel@tonic-gate }
25177c478bd9Sstevel@tonic-gate 
25187c478bd9Sstevel@tonic-gate dt_node_t *
dt_node_probe(char * s,int protoc,dt_node_t * nargs,dt_node_t * xargs)25191a7c1b72Smws dt_node_probe(char *s, int protoc, dt_node_t *nargs, dt_node_t *xargs)
25207c478bd9Sstevel@tonic-gate {
25217c478bd9Sstevel@tonic-gate 	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
25227c478bd9Sstevel@tonic-gate 	int nargc, xargc;
25237c478bd9Sstevel@tonic-gate 	dt_node_t *dnp;
25247c478bd9Sstevel@tonic-gate 
25257c478bd9Sstevel@tonic-gate 	size_t len = strlen(s) + 3; /* +3 for :: and \0 */
25267c478bd9Sstevel@tonic-gate 	char *name = alloca(len);
25277c478bd9Sstevel@tonic-gate 
25287c478bd9Sstevel@tonic-gate 	(void) snprintf(name, len, "::%s", s);
25297c478bd9Sstevel@tonic-gate 	(void) strhyphenate(name);
25307c478bd9Sstevel@tonic-gate 	free(s);
25317c478bd9Sstevel@tonic-gate 
25327c478bd9Sstevel@tonic-gate 	if (strchr(name, '`') != NULL) {
25337c478bd9Sstevel@tonic-gate 		xyerror(D_PROV_BADNAME, "probe name may not "
25347c478bd9Sstevel@tonic-gate 		    "contain scoping operator: %s\n", name);
25357c478bd9Sstevel@tonic-gate 	}
25367c478bd9Sstevel@tonic-gate 
25377c478bd9Sstevel@tonic-gate 	if (strlen(name) - 2 >= DTRACE_NAMELEN) {
25387c478bd9Sstevel@tonic-gate 		xyerror(D_PROV_BADNAME, "probe name may not exceed %d "
25397c478bd9Sstevel@tonic-gate 		    "characters: %s\n", DTRACE_NAMELEN - 1, name);
25407c478bd9Sstevel@tonic-gate 	}
25417c478bd9Sstevel@tonic-gate 
25427c478bd9Sstevel@tonic-gate 	dnp = dt_node_alloc(DT_NODE_PROBE);
25437c478bd9Sstevel@tonic-gate 
25447c478bd9Sstevel@tonic-gate 	dnp->dn_ident = dt_ident_create(name, DT_IDENT_PROBE,
25457c478bd9Sstevel@tonic-gate 	    DT_IDFLG_ORPHAN, DTRACE_IDNONE, _dtrace_defattr, 0,
25467c478bd9Sstevel@tonic-gate 	    &dt_idops_probe, NULL, dtp->dt_gen);
25477c478bd9Sstevel@tonic-gate 
25487c478bd9Sstevel@tonic-gate 	nargc = dt_decl_prototype(nargs, nargs,
25497c478bd9Sstevel@tonic-gate 	    "probe input", DT_DP_VOID | DT_DP_ANON);
25507c478bd9Sstevel@tonic-gate 
25517c478bd9Sstevel@tonic-gate 	xargc = dt_decl_prototype(xargs, nargs,
25527c478bd9Sstevel@tonic-gate 	    "probe output", DT_DP_VOID);
25537c478bd9Sstevel@tonic-gate 
25547c478bd9Sstevel@tonic-gate 	if (nargc > UINT8_MAX) {
25557c478bd9Sstevel@tonic-gate 		xyerror(D_PROV_PRARGLEN, "probe %s input prototype exceeds %u "
25567c478bd9Sstevel@tonic-gate 		    "parameters: %d params used\n", name, UINT8_MAX, nargc);
25577c478bd9Sstevel@tonic-gate 	}
25587c478bd9Sstevel@tonic-gate 
25597c478bd9Sstevel@tonic-gate 	if (xargc > UINT8_MAX) {
25607c478bd9Sstevel@tonic-gate 		xyerror(D_PROV_PRARGLEN, "probe %s output prototype exceeds %u "
25617c478bd9Sstevel@tonic-gate 		    "parameters: %d params used\n", name, UINT8_MAX, xargc);
25627c478bd9Sstevel@tonic-gate 	}
25637c478bd9Sstevel@tonic-gate 
25647c478bd9Sstevel@tonic-gate 	if (dnp->dn_ident == NULL || dt_probe_create(dtp,
25651a7c1b72Smws 	    dnp->dn_ident, protoc, nargs, nargc, xargs, xargc) == NULL)
25667c478bd9Sstevel@tonic-gate 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
25677c478bd9Sstevel@tonic-gate 
25687c478bd9Sstevel@tonic-gate 	return (dnp);
25697c478bd9Sstevel@tonic-gate }
25707c478bd9Sstevel@tonic-gate 
25717c478bd9Sstevel@tonic-gate dt_node_t *
dt_node_provider(char * name,dt_node_t * probes)25727c478bd9Sstevel@tonic-gate dt_node_provider(char *name, dt_node_t *probes)
25737c478bd9Sstevel@tonic-gate {
25747c478bd9Sstevel@tonic-gate 	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
25757c478bd9Sstevel@tonic-gate 	dt_node_t *dnp = dt_node_alloc(DT_NODE_PROVIDER);
25761a7c1b72Smws 	dt_node_t *lnp;
2577900524f3Sahl 	size_t len;
25787c478bd9Sstevel@tonic-gate 
25797c478bd9Sstevel@tonic-gate 	dnp->dn_provname = name;
25807c478bd9Sstevel@tonic-gate 	dnp->dn_probes = probes;
25817c478bd9Sstevel@tonic-gate 
25827c478bd9Sstevel@tonic-gate 	if (strchr(name, '`') != NULL) {
25837c478bd9Sstevel@tonic-gate 		dnerror(dnp, D_PROV_BADNAME, "provider name may not "
25847c478bd9Sstevel@tonic-gate 		    "contain scoping operator: %s\n", name);
25857c478bd9Sstevel@tonic-gate 	}
25867c478bd9Sstevel@tonic-gate 
2587900524f3Sahl 	if ((len = strlen(name)) >= DTRACE_PROVNAMELEN) {
25887c478bd9Sstevel@tonic-gate 		dnerror(dnp, D_PROV_BADNAME, "provider name may not exceed %d "
25897c478bd9Sstevel@tonic-gate 		    "characters: %s\n", DTRACE_PROVNAMELEN - 1, name);
25907c478bd9Sstevel@tonic-gate 	}
25917c478bd9Sstevel@tonic-gate 
2592900524f3Sahl 	if (isdigit(name[len - 1])) {
2593900524f3Sahl 		dnerror(dnp, D_PROV_BADNAME, "provider name may not "
2594900524f3Sahl 		    "end with a digit: %s\n", name);
2595900524f3Sahl 	}
2596900524f3Sahl 
25977c478bd9Sstevel@tonic-gate 	/*
25987c478bd9Sstevel@tonic-gate 	 * Check to see if the provider is already defined or visible through
2599bbf21555SRichard Lowe 	 * dtrace(4D).  If so, set dn_provred to treat it as a re-declaration.
26007c478bd9Sstevel@tonic-gate 	 * If not, create a new provider and set its interface-only flag.  This
26017c478bd9Sstevel@tonic-gate 	 * flag may be cleared later by calls made to dt_probe_declare().
26027c478bd9Sstevel@tonic-gate 	 */
26037c478bd9Sstevel@tonic-gate 	if ((dnp->dn_provider = dt_provider_lookup(dtp, name)) != NULL)
26047c478bd9Sstevel@tonic-gate 		dnp->dn_provred = B_TRUE;
26057c478bd9Sstevel@tonic-gate 	else if ((dnp->dn_provider = dt_provider_create(dtp, name)) == NULL)
26067c478bd9Sstevel@tonic-gate 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
26077c478bd9Sstevel@tonic-gate 	else
26087c478bd9Sstevel@tonic-gate 		dnp->dn_provider->pv_flags |= DT_PROVIDER_INTF;
26097c478bd9Sstevel@tonic-gate 
26107c478bd9Sstevel@tonic-gate 	/*
26111a7c1b72Smws 	 * Store all parse nodes created since we consumed the DT_KEY_PROVIDER
26127c478bd9Sstevel@tonic-gate 	 * token with the provider and then restore our lexing state to CLAUSE.
26131a7c1b72Smws 	 * Note that if dnp->dn_provred is true, we may end up storing dups of
26141a7c1b72Smws 	 * a provider's interface and implementation: we eat this space because
26151a7c1b72Smws 	 * the implementation will likely need to redeclare probe members, and
26161a7c1b72Smws 	 * therefore may result in those member nodes becoming persistent.
26177c478bd9Sstevel@tonic-gate 	 */
26181a7c1b72Smws 	for (lnp = yypcb->pcb_list; lnp->dn_link != NULL; lnp = lnp->dn_link)
26191a7c1b72Smws 		continue; /* skip to end of allocation list */
26201a7c1b72Smws 
26211a7c1b72Smws 	lnp->dn_link = dnp->dn_provider->pv_nodes;
26227c478bd9Sstevel@tonic-gate 	dnp->dn_provider->pv_nodes = yypcb->pcb_list;
26237c478bd9Sstevel@tonic-gate 
26247c478bd9Sstevel@tonic-gate 	yybegin(YYS_CLAUSE);
26257c478bd9Sstevel@tonic-gate 	return (dnp);
26267c478bd9Sstevel@tonic-gate }
26277c478bd9Sstevel@tonic-gate 
26287c478bd9Sstevel@tonic-gate dt_node_t *
dt_node_program(dt_node_t * lnp)26297c478bd9Sstevel@tonic-gate dt_node_program(dt_node_t *lnp)
26307c478bd9Sstevel@tonic-gate {
26317c478bd9Sstevel@tonic-gate 	dt_node_t *dnp = dt_node_alloc(DT_NODE_PROG);
26327c478bd9Sstevel@tonic-gate 	dnp->dn_list = lnp;
26337c478bd9Sstevel@tonic-gate 	return (dnp);
26347c478bd9Sstevel@tonic-gate }
26357c478bd9Sstevel@tonic-gate 
26367c478bd9Sstevel@tonic-gate /*
26377c478bd9Sstevel@tonic-gate  * This function provides the underlying implementation of cooking an
26387c478bd9Sstevel@tonic-gate  * identifier given its node, a hash of dynamic identifiers, an identifier
26397c478bd9Sstevel@tonic-gate  * kind, and a boolean flag indicating whether we are allowed to instantiate
26407c478bd9Sstevel@tonic-gate  * a new identifier if the string is not found.  This function is either
26417c478bd9Sstevel@tonic-gate  * called from dt_cook_ident(), below, or directly by the various cooking
26427c478bd9Sstevel@tonic-gate  * routines that are allowed to instantiate identifiers (e.g. op2 TOK_ASGN).
26437c478bd9Sstevel@tonic-gate  */
26447c478bd9Sstevel@tonic-gate static void
dt_xcook_ident(dt_node_t * dnp,dt_idhash_t * dhp,uint_t idkind,int create)26457c478bd9Sstevel@tonic-gate dt_xcook_ident(dt_node_t *dnp, dt_idhash_t *dhp, uint_t idkind, int create)
26467c478bd9Sstevel@tonic-gate {
26477c478bd9Sstevel@tonic-gate 	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
26487c478bd9Sstevel@tonic-gate 	const char *sname = dt_idhash_name(dhp);
26497c478bd9Sstevel@tonic-gate 	int uref = 0;
26507c478bd9Sstevel@tonic-gate 
26517c478bd9Sstevel@tonic-gate 	dtrace_attribute_t attr = _dtrace_defattr;
26527c478bd9Sstevel@tonic-gate 	dt_ident_t *idp;
26537c478bd9Sstevel@tonic-gate 	dtrace_syminfo_t dts;
26547c478bd9Sstevel@tonic-gate 	GElf_Sym sym;
26557c478bd9Sstevel@tonic-gate 
26567c478bd9Sstevel@tonic-gate 	const char *scope, *mark;
26577c478bd9Sstevel@tonic-gate 	uchar_t dnkind;
26587c478bd9Sstevel@tonic-gate 	char *name;
26597c478bd9Sstevel@tonic-gate 
26607c478bd9Sstevel@tonic-gate 	/*
26617c478bd9Sstevel@tonic-gate 	 * Look for scoping marks in the identifier.  If one is found, set our
26627c478bd9Sstevel@tonic-gate 	 * scope to either DTRACE_OBJ_KMODS or UMODS or to the first part of
26637c478bd9Sstevel@tonic-gate 	 * the string that specifies the scope using an explicit module name.
26647c478bd9Sstevel@tonic-gate 	 * If two marks in a row are found, set 'uref' (user symbol reference).
26657c478bd9Sstevel@tonic-gate 	 * Otherwise we set scope to DTRACE_OBJ_EXEC, indicating that normal
26667c478bd9Sstevel@tonic-gate 	 * scope is desired and we should search the specified idhash.
26677c478bd9Sstevel@tonic-gate 	 */
26687c478bd9Sstevel@tonic-gate 	if ((name = strrchr(dnp->dn_string, '`')) != NULL) {
26697c478bd9Sstevel@tonic-gate 		if (name > dnp->dn_string && name[-1] == '`') {
26707c478bd9Sstevel@tonic-gate 			uref++;
26717c478bd9Sstevel@tonic-gate 			name[-1] = '\0';
26727c478bd9Sstevel@tonic-gate 		}
26737c478bd9Sstevel@tonic-gate 
26747c478bd9Sstevel@tonic-gate 		if (name == dnp->dn_string + uref)
26757c478bd9Sstevel@tonic-gate 			scope = uref ? DTRACE_OBJ_UMODS : DTRACE_OBJ_KMODS;
26767c478bd9Sstevel@tonic-gate 		else
26777c478bd9Sstevel@tonic-gate 			scope = dnp->dn_string;
26787c478bd9Sstevel@tonic-gate 
26797c478bd9Sstevel@tonic-gate 		*name++ = '\0'; /* leave name pointing after scoping mark */
26807c478bd9Sstevel@tonic-gate 		dnkind = DT_NODE_VAR;
26817c478bd9Sstevel@tonic-gate 
26827c478bd9Sstevel@tonic-gate 	} else if (idkind == DT_IDENT_AGG) {
26837c478bd9Sstevel@tonic-gate 		scope = DTRACE_OBJ_EXEC;
26847c478bd9Sstevel@tonic-gate 		name = dnp->dn_string + 1;
26857c478bd9Sstevel@tonic-gate 		dnkind = DT_NODE_AGG;
26867c478bd9Sstevel@tonic-gate 	} else {
26877c478bd9Sstevel@tonic-gate 		scope = DTRACE_OBJ_EXEC;
26887c478bd9Sstevel@tonic-gate 		name = dnp->dn_string;
26897c478bd9Sstevel@tonic-gate 		dnkind = DT_NODE_VAR;
26907c478bd9Sstevel@tonic-gate 	}
26917c478bd9Sstevel@tonic-gate 
26927c478bd9Sstevel@tonic-gate 	/*
26937c478bd9Sstevel@tonic-gate 	 * If create is set to false, and we fail our idhash lookup, preset
26947c478bd9Sstevel@tonic-gate 	 * the errno code to EDT_NOVAR for our final error message below.
26957c478bd9Sstevel@tonic-gate 	 * If we end up calling dtrace_lookup_by_name(), it will reset the
26967c478bd9Sstevel@tonic-gate 	 * errno appropriately and that error will be reported instead.
26977c478bd9Sstevel@tonic-gate 	 */
26987c478bd9Sstevel@tonic-gate 	(void) dt_set_errno(dtp, EDT_NOVAR);
26997c478bd9Sstevel@tonic-gate 	mark = uref ? "``" : "`";
27007c478bd9Sstevel@tonic-gate 
27017c478bd9Sstevel@tonic-gate 	if (scope == DTRACE_OBJ_EXEC && (
27027c478bd9Sstevel@tonic-gate 	    (dhp != dtp->dt_globals &&
27037c478bd9Sstevel@tonic-gate 	    (idp = dt_idhash_lookup(dhp, name)) != NULL) ||
27047c478bd9Sstevel@tonic-gate 	    (dhp == dtp->dt_globals &&
27057c478bd9Sstevel@tonic-gate 	    (idp = dt_idstack_lookup(&yypcb->pcb_globals, name)) != NULL))) {
27067c478bd9Sstevel@tonic-gate 		/*
27077c478bd9Sstevel@tonic-gate 		 * Check that we are referencing the ident in the manner that
27087c478bd9Sstevel@tonic-gate 		 * matches its type if this is a global lookup.  In the TLS or
27097c478bd9Sstevel@tonic-gate 		 * local case, we don't know how the ident will be used until
27107c478bd9Sstevel@tonic-gate 		 * the time operator -> is seen; more parsing is needed.
27117c478bd9Sstevel@tonic-gate 		 */
27127c478bd9Sstevel@tonic-gate 		if (idp->di_kind != idkind && dhp == dtp->dt_globals) {
27137c478bd9Sstevel@tonic-gate 			xyerror(D_IDENT_BADREF, "%s '%s' may not be referenced "
27147c478bd9Sstevel@tonic-gate 			    "as %s\n", dt_idkind_name(idp->di_kind),
27157c478bd9Sstevel@tonic-gate 			    idp->di_name, dt_idkind_name(idkind));
27167c478bd9Sstevel@tonic-gate 		}
27177c478bd9Sstevel@tonic-gate 
27187c478bd9Sstevel@tonic-gate 		/*
27197c478bd9Sstevel@tonic-gate 		 * Arrays and aggregations are not cooked individually. They
27207c478bd9Sstevel@tonic-gate 		 * have dynamic types and must be referenced using operator [].
27217c478bd9Sstevel@tonic-gate 		 * This is handled explicitly by the code for DT_TOK_LBRAC.
27227c478bd9Sstevel@tonic-gate 		 */
27237c478bd9Sstevel@tonic-gate 		if (idp->di_kind != DT_IDENT_ARRAY &&
27247c478bd9Sstevel@tonic-gate 		    idp->di_kind != DT_IDENT_AGG)
27257c478bd9Sstevel@tonic-gate 			attr = dt_ident_cook(dnp, idp, NULL);
27267c478bd9Sstevel@tonic-gate 		else {
27277c478bd9Sstevel@tonic-gate 			dt_node_type_assign(dnp,
2728a386cc11SRobert Mustacchi 			    DT_DYN_CTFP(dtp), DT_DYN_TYPE(dtp), B_FALSE);
27297c478bd9Sstevel@tonic-gate 			attr = idp->di_attr;
27307c478bd9Sstevel@tonic-gate 		}
27317c478bd9Sstevel@tonic-gate 
27327c478bd9Sstevel@tonic-gate 		free(dnp->dn_string);
27337c478bd9Sstevel@tonic-gate 		dnp->dn_string = NULL;
27347c478bd9Sstevel@tonic-gate 		dnp->dn_kind = dnkind;
27357c478bd9Sstevel@tonic-gate 		dnp->dn_ident = idp;
27367c478bd9Sstevel@tonic-gate 		dnp->dn_flags |= DT_NF_LVALUE;
27377c478bd9Sstevel@tonic-gate 
27387c478bd9Sstevel@tonic-gate 		if (idp->di_flags & DT_IDFLG_WRITE)
27397c478bd9Sstevel@tonic-gate 			dnp->dn_flags |= DT_NF_WRITABLE;
27407c478bd9Sstevel@tonic-gate 
27417c478bd9Sstevel@tonic-gate 		dt_node_attr_assign(dnp, attr);
27427c478bd9Sstevel@tonic-gate 
27437c478bd9Sstevel@tonic-gate 	} else if (dhp == dtp->dt_globals && scope != DTRACE_OBJ_EXEC &&
27447c478bd9Sstevel@tonic-gate 	    dtrace_lookup_by_name(dtp, scope, name, &sym, &dts) == 0) {
27457c478bd9Sstevel@tonic-gate 
27467c478bd9Sstevel@tonic-gate 		dt_module_t *mp = dt_module_lookup_by_name(dtp, dts.dts_object);
27477c478bd9Sstevel@tonic-gate 		int umod = (mp->dm_flags & DT_DM_KERNEL) == 0;
27487c478bd9Sstevel@tonic-gate 		static const char *const kunames[] = { "kernel", "user" };
27497c478bd9Sstevel@tonic-gate 
27507c478bd9Sstevel@tonic-gate 		dtrace_typeinfo_t dtt;
27517c478bd9Sstevel@tonic-gate 		dtrace_syminfo_t *sip;
27527c478bd9Sstevel@tonic-gate 
27537c478bd9Sstevel@tonic-gate 		if (uref ^ umod) {
27547c478bd9Sstevel@tonic-gate 			xyerror(D_SYM_BADREF, "%s module '%s' symbol '%s' may "
27557c478bd9Sstevel@tonic-gate 			    "not be referenced as a %s symbol\n", kunames[umod],
27567c478bd9Sstevel@tonic-gate 			    dts.dts_object, dts.dts_name, kunames[uref]);
27577c478bd9Sstevel@tonic-gate 		}
27587c478bd9Sstevel@tonic-gate 
27597c478bd9Sstevel@tonic-gate 		if (dtrace_symbol_type(dtp, &sym, &dts, &dtt) != 0) {
27607c478bd9Sstevel@tonic-gate 			/*
27617c478bd9Sstevel@tonic-gate 			 * For now, we special-case EDT_DATAMODEL to clarify
27627c478bd9Sstevel@tonic-gate 			 * that mixed data models are not currently supported.
27637c478bd9Sstevel@tonic-gate 			 */
27647c478bd9Sstevel@tonic-gate 			if (dtp->dt_errno == EDT_DATAMODEL) {
27657c478bd9Sstevel@tonic-gate 				xyerror(D_SYM_MODEL, "cannot use %s symbol "
27667c478bd9Sstevel@tonic-gate 				    "%s%s%s in a %s D program\n",
27677c478bd9Sstevel@tonic-gate 				    dt_module_modelname(mp),
27687c478bd9Sstevel@tonic-gate 				    dts.dts_object, mark, dts.dts_name,
27697c478bd9Sstevel@tonic-gate 				    dt_module_modelname(dtp->dt_ddefs));
27707c478bd9Sstevel@tonic-gate 			}
27717c478bd9Sstevel@tonic-gate 
27727c478bd9Sstevel@tonic-gate 			xyerror(D_SYM_NOTYPES,
27737c478bd9Sstevel@tonic-gate 			    "no symbolic type information is available for "
27747c478bd9Sstevel@tonic-gate 			    "%s%s%s: %s\n", dts.dts_object, mark, dts.dts_name,
27757c478bd9Sstevel@tonic-gate 			    dtrace_errmsg(dtp, dtrace_errno(dtp)));
27767c478bd9Sstevel@tonic-gate 		}
27777c478bd9Sstevel@tonic-gate 
27787c478bd9Sstevel@tonic-gate 		idp = dt_ident_create(name, DT_IDENT_SYMBOL, 0, 0,
27797c478bd9Sstevel@tonic-gate 		    _dtrace_symattr, 0, &dt_idops_thaw, NULL, dtp->dt_gen);
27807c478bd9Sstevel@tonic-gate 
27817c478bd9Sstevel@tonic-gate 		if (idp == NULL)
27827c478bd9Sstevel@tonic-gate 			longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
27837c478bd9Sstevel@tonic-gate 
27847c478bd9Sstevel@tonic-gate 		if (mp->dm_flags & DT_DM_PRIMARY)
27857c478bd9Sstevel@tonic-gate 			idp->di_flags |= DT_IDFLG_PRIM;
27867c478bd9Sstevel@tonic-gate 
27877c478bd9Sstevel@tonic-gate 		idp->di_next = dtp->dt_externs;
27887c478bd9Sstevel@tonic-gate 		dtp->dt_externs = idp;
27897c478bd9Sstevel@tonic-gate 
27907c478bd9Sstevel@tonic-gate 		if ((sip = malloc(sizeof (dtrace_syminfo_t))) == NULL)
27917c478bd9Sstevel@tonic-gate 			longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
27927c478bd9Sstevel@tonic-gate 
27937c478bd9Sstevel@tonic-gate 		bcopy(&dts, sip, sizeof (dtrace_syminfo_t));
27947c478bd9Sstevel@tonic-gate 		idp->di_data = sip;
27957c478bd9Sstevel@tonic-gate 		idp->di_ctfp = dtt.dtt_ctfp;
27967c478bd9Sstevel@tonic-gate 		idp->di_type = dtt.dtt_type;
27977c478bd9Sstevel@tonic-gate 
27987c478bd9Sstevel@tonic-gate 		free(dnp->dn_string);
27997c478bd9Sstevel@tonic-gate 		dnp->dn_string = NULL;
28007c478bd9Sstevel@tonic-gate 		dnp->dn_kind = DT_NODE_SYM;
28017c478bd9Sstevel@tonic-gate 		dnp->dn_ident = idp;
28027c478bd9Sstevel@tonic-gate 		dnp->dn_flags |= DT_NF_LVALUE;
28037c478bd9Sstevel@tonic-gate 
2804a386cc11SRobert Mustacchi 		dt_node_type_assign(dnp, dtt.dtt_ctfp, dtt.dtt_type,
2805a386cc11SRobert Mustacchi 		    dtt.dtt_flags);
28067c478bd9Sstevel@tonic-gate 		dt_node_attr_assign(dnp, _dtrace_symattr);
28077c478bd9Sstevel@tonic-gate 
28087c478bd9Sstevel@tonic-gate 		if (uref) {
28097c478bd9Sstevel@tonic-gate 			idp->di_flags |= DT_IDFLG_USER;
28107c478bd9Sstevel@tonic-gate 			dnp->dn_flags |= DT_NF_USERLAND;
28117c478bd9Sstevel@tonic-gate 		}
28127c478bd9Sstevel@tonic-gate 
28137c478bd9Sstevel@tonic-gate 	} else if (scope == DTRACE_OBJ_EXEC && create == B_TRUE) {
28147c478bd9Sstevel@tonic-gate 		uint_t flags = DT_IDFLG_WRITE;
28157c478bd9Sstevel@tonic-gate 		uint_t id;
28167c478bd9Sstevel@tonic-gate 
28177c478bd9Sstevel@tonic-gate 		if (dt_idhash_nextid(dhp, &id) == -1) {
28187c478bd9Sstevel@tonic-gate 			xyerror(D_ID_OFLOW, "cannot create %s: limit on number "
28197c478bd9Sstevel@tonic-gate 			    "of %s variables exceeded\n", name, sname);
28207c478bd9Sstevel@tonic-gate 		}
28217c478bd9Sstevel@tonic-gate 
28227c478bd9Sstevel@tonic-gate 		if (dhp == yypcb->pcb_locals)
28237c478bd9Sstevel@tonic-gate 			flags |= DT_IDFLG_LOCAL;
28247c478bd9Sstevel@tonic-gate 		else if (dhp == dtp->dt_tls)
28257c478bd9Sstevel@tonic-gate 			flags |= DT_IDFLG_TLS;
28267c478bd9Sstevel@tonic-gate 
28277c478bd9Sstevel@tonic-gate 		dt_dprintf("create %s %s variable %s, id=%u\n",
28287c478bd9Sstevel@tonic-gate 		    sname, dt_idkind_name(idkind), name, id);
28297c478bd9Sstevel@tonic-gate 
28307c478bd9Sstevel@tonic-gate 		if (idkind == DT_IDENT_ARRAY || idkind == DT_IDENT_AGG) {
28317c478bd9Sstevel@tonic-gate 			idp = dt_idhash_insert(dhp, name,
28327c478bd9Sstevel@tonic-gate 			    idkind, flags, id, _dtrace_defattr, 0,
28337c478bd9Sstevel@tonic-gate 			    &dt_idops_assc, NULL, dtp->dt_gen);
28347c478bd9Sstevel@tonic-gate 		} else {
28357c478bd9Sstevel@tonic-gate 			idp = dt_idhash_insert(dhp, name,
28367c478bd9Sstevel@tonic-gate 			    idkind, flags, id, _dtrace_defattr, 0,
28377c478bd9Sstevel@tonic-gate 			    &dt_idops_thaw, NULL, dtp->dt_gen);
28387c478bd9Sstevel@tonic-gate 		}
28397c478bd9Sstevel@tonic-gate 
28407c478bd9Sstevel@tonic-gate 		if (idp == NULL)
28417c478bd9Sstevel@tonic-gate 			longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
28427c478bd9Sstevel@tonic-gate 
28437c478bd9Sstevel@tonic-gate 		/*
28447c478bd9Sstevel@tonic-gate 		 * Arrays and aggregations are not cooked individually. They
28457c478bd9Sstevel@tonic-gate 		 * have dynamic types and must be referenced using operator [].
28467c478bd9Sstevel@tonic-gate 		 * This is handled explicitly by the code for DT_TOK_LBRAC.
28477c478bd9Sstevel@tonic-gate 		 */
28487c478bd9Sstevel@tonic-gate 		if (idp->di_kind != DT_IDENT_ARRAY &&
28497c478bd9Sstevel@tonic-gate 		    idp->di_kind != DT_IDENT_AGG)
28507c478bd9Sstevel@tonic-gate 			attr = dt_ident_cook(dnp, idp, NULL);
28517c478bd9Sstevel@tonic-gate 		else {
28527c478bd9Sstevel@tonic-gate 			dt_node_type_assign(dnp,
2853a386cc11SRobert Mustacchi 			    DT_DYN_CTFP(dtp), DT_DYN_TYPE(dtp), B_FALSE);
28547c478bd9Sstevel@tonic-gate 			attr = idp->di_attr;
28557c478bd9Sstevel@tonic-gate 		}
28567c478bd9Sstevel@tonic-gate 
28577c478bd9Sstevel@tonic-gate 		free(dnp->dn_string);
28587c478bd9Sstevel@tonic-gate 		dnp->dn_string = NULL;
28597c478bd9Sstevel@tonic-gate 		dnp->dn_kind = dnkind;
28607c478bd9Sstevel@tonic-gate 		dnp->dn_ident = idp;
28617c478bd9Sstevel@tonic-gate 		dnp->dn_flags |= DT_NF_LVALUE | DT_NF_WRITABLE;
28627c478bd9Sstevel@tonic-gate 
28637c478bd9Sstevel@tonic-gate 		dt_node_attr_assign(dnp, attr);
28647c478bd9Sstevel@tonic-gate 
28657c478bd9Sstevel@tonic-gate 	} else if (scope != DTRACE_OBJ_EXEC) {
28667c478bd9Sstevel@tonic-gate 		xyerror(D_IDENT_UNDEF, "failed to resolve %s%s%s: %s\n",
28677c478bd9Sstevel@tonic-gate 		    dnp->dn_string, mark, name,
28687c478bd9Sstevel@tonic-gate 		    dtrace_errmsg(dtp, dtrace_errno(dtp)));
28697c478bd9Sstevel@tonic-gate 	} else {
28707c478bd9Sstevel@tonic-gate 		xyerror(D_IDENT_UNDEF, "failed to resolve %s: %s\n",
28717c478bd9Sstevel@tonic-gate 		    dnp->dn_string, dtrace_errmsg(dtp, dtrace_errno(dtp)));
28727c478bd9Sstevel@tonic-gate 	}
28737c478bd9Sstevel@tonic-gate }
28747c478bd9Sstevel@tonic-gate 
28757c478bd9Sstevel@tonic-gate static dt_node_t *
dt_cook_ident(dt_node_t * dnp,uint_t idflags)28767c478bd9Sstevel@tonic-gate dt_cook_ident(dt_node_t *dnp, uint_t idflags)
28777c478bd9Sstevel@tonic-gate {
28787c478bd9Sstevel@tonic-gate 	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
28797c478bd9Sstevel@tonic-gate 
28807c478bd9Sstevel@tonic-gate 	if (dnp->dn_op == DT_TOK_AGG)
28817c478bd9Sstevel@tonic-gate 		dt_xcook_ident(dnp, dtp->dt_aggs, DT_IDENT_AGG, B_FALSE);
28827c478bd9Sstevel@tonic-gate 	else
28837c478bd9Sstevel@tonic-gate 		dt_xcook_ident(dnp, dtp->dt_globals, DT_IDENT_SCALAR, B_FALSE);
28847c478bd9Sstevel@tonic-gate 
28857c478bd9Sstevel@tonic-gate 	return (dt_node_cook(dnp, idflags));
28867c478bd9Sstevel@tonic-gate }
28877c478bd9Sstevel@tonic-gate 
28887c478bd9Sstevel@tonic-gate /*
28897c478bd9Sstevel@tonic-gate  * Since operators [ and -> can instantiate new variables before we know
28907c478bd9Sstevel@tonic-gate  * whether the reference is for a read or a write, we need to check read
28917c478bd9Sstevel@tonic-gate  * references to determine if the identifier is currently dt_ident_unref().
28927c478bd9Sstevel@tonic-gate  * If so, we report that this first access was to an undefined variable.
28937c478bd9Sstevel@tonic-gate  */
28947c478bd9Sstevel@tonic-gate static dt_node_t *
dt_cook_var(dt_node_t * dnp,uint_t idflags)28957c478bd9Sstevel@tonic-gate dt_cook_var(dt_node_t *dnp, uint_t idflags)
28967c478bd9Sstevel@tonic-gate {
28977c478bd9Sstevel@tonic-gate 	dt_ident_t *idp = dnp->dn_ident;
28987c478bd9Sstevel@tonic-gate 
28997c478bd9Sstevel@tonic-gate 	if ((idflags & DT_IDFLG_REF) && dt_ident_unref(idp)) {
29007c478bd9Sstevel@tonic-gate 		dnerror(dnp, D_VAR_UNDEF,
29017c478bd9Sstevel@tonic-gate 		    "%s%s has not yet been declared or assigned\n",
29027c478bd9Sstevel@tonic-gate 		    (idp->di_flags & DT_IDFLG_LOCAL) ? "this->" :
29037c478bd9Sstevel@tonic-gate 		    (idp->di_flags & DT_IDFLG_TLS) ? "self->" : "",
29047c478bd9Sstevel@tonic-gate 		    idp->di_name);
29057c478bd9Sstevel@tonic-gate 	}
29067c478bd9Sstevel@tonic-gate 
29077c478bd9Sstevel@tonic-gate 	dt_node_attr_assign(dnp, dt_ident_cook(dnp, idp, &dnp->dn_args));
29087c478bd9Sstevel@tonic-gate 	return (dnp);
29097c478bd9Sstevel@tonic-gate }
29107c478bd9Sstevel@tonic-gate 
29117c478bd9Sstevel@tonic-gate /*ARGSUSED*/
29127c478bd9Sstevel@tonic-gate static dt_node_t *
dt_cook_func(dt_node_t * dnp,uint_t idflags)29137c478bd9Sstevel@tonic-gate dt_cook_func(dt_node_t *dnp, uint_t idflags)
29147c478bd9Sstevel@tonic-gate {
29157c478bd9Sstevel@tonic-gate 	dt_node_attr_assign(dnp,
29167c478bd9Sstevel@tonic-gate 	    dt_ident_cook(dnp, dnp->dn_ident, &dnp->dn_args));
29177c478bd9Sstevel@tonic-gate 
29187c478bd9Sstevel@tonic-gate 	return (dnp);
29197c478bd9Sstevel@tonic-gate }
29207c478bd9Sstevel@tonic-gate 
29217c478bd9Sstevel@tonic-gate static dt_node_t *
dt_cook_op1(dt_node_t * dnp,uint_t idflags)29227c478bd9Sstevel@tonic-gate dt_cook_op1(dt_node_t *dnp, uint_t idflags)
29237c478bd9Sstevel@tonic-gate {
29247c478bd9Sstevel@tonic-gate 	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
29257c478bd9Sstevel@tonic-gate 	dt_node_t *cp = dnp->dn_child;
29267c478bd9Sstevel@tonic-gate 
29277c478bd9Sstevel@tonic-gate 	char n[DT_TYPE_NAMELEN];
29287c478bd9Sstevel@tonic-gate 	dtrace_typeinfo_t dtt;
29297c478bd9Sstevel@tonic-gate 	dt_ident_t *idp;
29307c478bd9Sstevel@tonic-gate 
29317c478bd9Sstevel@tonic-gate 	ctf_encoding_t e;
29327c478bd9Sstevel@tonic-gate 	ctf_arinfo_t r;
29337c478bd9Sstevel@tonic-gate 	ctf_id_t type, base;
29347c478bd9Sstevel@tonic-gate 	uint_t kind;
29357c478bd9Sstevel@tonic-gate 
29367c478bd9Sstevel@tonic-gate 	if (dnp->dn_op == DT_TOK_PREINC || dnp->dn_op == DT_TOK_POSTINC ||
29377c478bd9Sstevel@tonic-gate 	    dnp->dn_op == DT_TOK_PREDEC || dnp->dn_op == DT_TOK_POSTDEC)
29387c478bd9Sstevel@tonic-gate 		idflags = DT_IDFLG_REF | DT_IDFLG_MOD;
29397c478bd9Sstevel@tonic-gate 	else
29407c478bd9Sstevel@tonic-gate 		idflags = DT_IDFLG_REF;
29417c478bd9Sstevel@tonic-gate 
29427c478bd9Sstevel@tonic-gate 	/*
29437c478bd9Sstevel@tonic-gate 	 * We allow the unary ++ and -- operators to instantiate new scalar
29447c478bd9Sstevel@tonic-gate 	 * variables if applied to an identifier; otherwise just cook as usual.
29457c478bd9Sstevel@tonic-gate 	 */
29467c478bd9Sstevel@tonic-gate 	if (cp->dn_kind == DT_NODE_IDENT && (idflags & DT_IDFLG_MOD))
29477c478bd9Sstevel@tonic-gate 		dt_xcook_ident(cp, dtp->dt_globals, DT_IDENT_SCALAR, B_TRUE);
29487c478bd9Sstevel@tonic-gate 
29497c478bd9Sstevel@tonic-gate 	cp = dnp->dn_child = dt_node_cook(cp, 0); /* don't set idflags yet */
29507c478bd9Sstevel@tonic-gate 
29517c478bd9Sstevel@tonic-gate 	if (cp->dn_kind == DT_NODE_VAR && dt_ident_unref(cp->dn_ident)) {
29527c478bd9Sstevel@tonic-gate 		if (dt_type_lookup("int64_t", &dtt) != 0)
29537c478bd9Sstevel@tonic-gate 			xyerror(D_TYPE_ERR, "failed to lookup int64_t\n");
29547c478bd9Sstevel@tonic-gate 
29557c478bd9Sstevel@tonic-gate 		dt_ident_type_assign(cp->dn_ident, dtt.dtt_ctfp, dtt.dtt_type);
2956a386cc11SRobert Mustacchi 		dt_node_type_assign(cp, dtt.dtt_ctfp, dtt.dtt_type,
2957a386cc11SRobert Mustacchi 		    dtt.dtt_flags);
29587c478bd9Sstevel@tonic-gate 	}
29597c478bd9Sstevel@tonic-gate 
29607c478bd9Sstevel@tonic-gate 	if (cp->dn_kind == DT_NODE_VAR)
29617c478bd9Sstevel@tonic-gate 		cp->dn_ident->di_flags |= idflags;
29627c478bd9Sstevel@tonic-gate 
29637c478bd9Sstevel@tonic-gate 	switch (dnp->dn_op) {
29647c478bd9Sstevel@tonic-gate 	case DT_TOK_DEREF:
29657c478bd9Sstevel@tonic-gate 		/*
29667c478bd9Sstevel@tonic-gate 		 * If the deref operator is applied to a translated pointer,
2967e5803b76SAdam H. Leventhal 		 * we set our output type to the output of the translation.
29687c478bd9Sstevel@tonic-gate 		 */
29697c478bd9Sstevel@tonic-gate 		if ((idp = dt_node_resolve(cp, DT_IDENT_XLPTR)) != NULL) {
29707c478bd9Sstevel@tonic-gate 			dt_xlator_t *dxp = idp->di_data;
29717c478bd9Sstevel@tonic-gate 
29727c478bd9Sstevel@tonic-gate 			dnp->dn_ident = &dxp->dx_souid;
29737c478bd9Sstevel@tonic-gate 			dt_node_type_assign(dnp,
2974a386cc11SRobert Mustacchi 			    dnp->dn_ident->di_ctfp, dnp->dn_ident->di_type,
2975a386cc11SRobert Mustacchi 			    cp->dn_flags & DT_NF_USERLAND);
29767c478bd9Sstevel@tonic-gate 			break;
29777c478bd9Sstevel@tonic-gate 		}
29787c478bd9Sstevel@tonic-gate 
29797c478bd9Sstevel@tonic-gate 		type = ctf_type_resolve(cp->dn_ctfp, cp->dn_type);
29807c478bd9Sstevel@tonic-gate 		kind = ctf_type_kind(cp->dn_ctfp, type);
29817c478bd9Sstevel@tonic-gate 
29827c478bd9Sstevel@tonic-gate 		if (kind == CTF_K_ARRAY) {
29837c478bd9Sstevel@tonic-gate 			if (ctf_array_info(cp->dn_ctfp, type, &r) != 0) {
29847c478bd9Sstevel@tonic-gate 				dtp->dt_ctferr = ctf_errno(cp->dn_ctfp);
29857c478bd9Sstevel@tonic-gate 				longjmp(yypcb->pcb_jmpbuf, EDT_CTF);
29867c478bd9Sstevel@tonic-gate 			} else
29877c478bd9Sstevel@tonic-gate 				type = r.ctr_contents;
29887c478bd9Sstevel@tonic-gate 		} else if (kind == CTF_K_POINTER) {
29897c478bd9Sstevel@tonic-gate 			type = ctf_type_reference(cp->dn_ctfp, type);
29907c478bd9Sstevel@tonic-gate 		} else {
29917c478bd9Sstevel@tonic-gate 			xyerror(D_DEREF_NONPTR,
29927c478bd9Sstevel@tonic-gate 			    "cannot dereference non-pointer type\n");
29937c478bd9Sstevel@tonic-gate 		}
29947c478bd9Sstevel@tonic-gate 
2995a386cc11SRobert Mustacchi 		dt_node_type_assign(dnp, cp->dn_ctfp, type,
2996a386cc11SRobert Mustacchi 		    cp->dn_flags & DT_NF_USERLAND);
29977c478bd9Sstevel@tonic-gate 		base = ctf_type_resolve(cp->dn_ctfp, type);
29987c478bd9Sstevel@tonic-gate 		kind = ctf_type_kind(cp->dn_ctfp, base);
29997c478bd9Sstevel@tonic-gate 
30007c478bd9Sstevel@tonic-gate 		if (kind == CTF_K_INTEGER && ctf_type_encoding(cp->dn_ctfp,
30017c478bd9Sstevel@tonic-gate 		    base, &e) == 0 && IS_VOID(e)) {
30027c478bd9Sstevel@tonic-gate 			xyerror(D_DEREF_VOID,
30037c478bd9Sstevel@tonic-gate 			    "cannot dereference pointer to void\n");
30047c478bd9Sstevel@tonic-gate 		}
30057c478bd9Sstevel@tonic-gate 
30067c478bd9Sstevel@tonic-gate 		if (kind == CTF_K_FUNCTION) {
30077c478bd9Sstevel@tonic-gate 			xyerror(D_DEREF_FUNC,
30087c478bd9Sstevel@tonic-gate 			    "cannot dereference pointer to function\n");
30097c478bd9Sstevel@tonic-gate 		}
30107c478bd9Sstevel@tonic-gate 
30117c478bd9Sstevel@tonic-gate 		if (kind != CTF_K_ARRAY || dt_node_is_string(dnp))
30127c478bd9Sstevel@tonic-gate 			dnp->dn_flags |= DT_NF_LVALUE; /* see K&R[A7.4.3] */
30137c478bd9Sstevel@tonic-gate 
30147c478bd9Sstevel@tonic-gate 		/*
30157c478bd9Sstevel@tonic-gate 		 * If we propagated the l-value bit and the child operand was
30167c478bd9Sstevel@tonic-gate 		 * a writable D variable or a binary operation of the form
30177c478bd9Sstevel@tonic-gate 		 * a + b where a is writable, then propagate the writable bit.
30187c478bd9Sstevel@tonic-gate 		 * This is necessary to permit assignments to scalar arrays,
30197c478bd9Sstevel@tonic-gate 		 * which are converted to expressions of the form *(a + i).
30207c478bd9Sstevel@tonic-gate 		 */
30217c478bd9Sstevel@tonic-gate 		if ((cp->dn_flags & DT_NF_WRITABLE) ||
30227c478bd9Sstevel@tonic-gate 		    (cp->dn_kind == DT_NODE_OP2 && cp->dn_op == DT_TOK_ADD &&
30237c478bd9Sstevel@tonic-gate 		    (cp->dn_left->dn_flags & DT_NF_WRITABLE)))
30247c478bd9Sstevel@tonic-gate 			dnp->dn_flags |= DT_NF_WRITABLE;
30257c478bd9Sstevel@tonic-gate 
30267c478bd9Sstevel@tonic-gate 		if ((cp->dn_flags & DT_NF_USERLAND) &&
30277c478bd9Sstevel@tonic-gate 		    (kind == CTF_K_POINTER || (dnp->dn_flags & DT_NF_REF)))
30287c478bd9Sstevel@tonic-gate 			dnp->dn_flags |= DT_NF_USERLAND;
30297c478bd9Sstevel@tonic-gate 		break;
30307c478bd9Sstevel@tonic-gate 
30317c478bd9Sstevel@tonic-gate 	case DT_TOK_IPOS:
30327c478bd9Sstevel@tonic-gate 	case DT_TOK_INEG:
30337c478bd9Sstevel@tonic-gate 		if (!dt_node_is_arith(cp)) {
30347c478bd9Sstevel@tonic-gate 			xyerror(D_OP_ARITH, "operator %s requires an operand "
30357c478bd9Sstevel@tonic-gate 			    "of arithmetic type\n", opstr(dnp->dn_op));
30367c478bd9Sstevel@tonic-gate 		}
30377c478bd9Sstevel@tonic-gate 		dt_node_type_propagate(cp, dnp); /* see K&R[A7.4.4-6] */
30387c478bd9Sstevel@tonic-gate 		break;
30397c478bd9Sstevel@tonic-gate 
30407c478bd9Sstevel@tonic-gate 	case DT_TOK_BNEG:
30417c478bd9Sstevel@tonic-gate 		if (!dt_node_is_integer(cp)) {
30427c478bd9Sstevel@tonic-gate 			xyerror(D_OP_INT, "operator %s requires an operand of "
30437c478bd9Sstevel@tonic-gate 			    "integral type\n", opstr(dnp->dn_op));
30447c478bd9Sstevel@tonic-gate 		}
30457c478bd9Sstevel@tonic-gate 		dt_node_type_propagate(cp, dnp); /* see K&R[A7.4.4-6] */
30467c478bd9Sstevel@tonic-gate 		break;
30477c478bd9Sstevel@tonic-gate 
30487c478bd9Sstevel@tonic-gate 	case DT_TOK_LNEG:
30497c478bd9Sstevel@tonic-gate 		if (!dt_node_is_scalar(cp)) {
30507c478bd9Sstevel@tonic-gate 			xyerror(D_OP_SCALAR, "operator %s requires an operand "
30517c478bd9Sstevel@tonic-gate 			    "of scalar type\n", opstr(dnp->dn_op));
30527c478bd9Sstevel@tonic-gate 		}
3053a386cc11SRobert Mustacchi 		dt_node_type_assign(dnp, DT_INT_CTFP(dtp), DT_INT_TYPE(dtp),
3054a386cc11SRobert Mustacchi 		    B_FALSE);
30557c478bd9Sstevel@tonic-gate 		break;
30567c478bd9Sstevel@tonic-gate 
30577c478bd9Sstevel@tonic-gate 	case DT_TOK_ADDROF:
30587c478bd9Sstevel@tonic-gate 		if (cp->dn_kind == DT_NODE_VAR || cp->dn_kind == DT_NODE_AGG) {
30597c478bd9Sstevel@tonic-gate 			xyerror(D_ADDROF_VAR,
30607c478bd9Sstevel@tonic-gate 			    "cannot take address of dynamic variable\n");
30617c478bd9Sstevel@tonic-gate 		}
30627c478bd9Sstevel@tonic-gate 
30637c478bd9Sstevel@tonic-gate 		if (dt_node_is_dynamic(cp)) {
30647c478bd9Sstevel@tonic-gate 			xyerror(D_ADDROF_VAR,
30657c478bd9Sstevel@tonic-gate 			    "cannot take address of dynamic object\n");
30667c478bd9Sstevel@tonic-gate 		}
30677c478bd9Sstevel@tonic-gate 
30687c478bd9Sstevel@tonic-gate 		if (!(cp->dn_flags & DT_NF_LVALUE)) {
30697c478bd9Sstevel@tonic-gate 			xyerror(D_ADDROF_LVAL, /* see K&R[A7.4.2] */
30707c478bd9Sstevel@tonic-gate 			    "unacceptable operand for unary & operator\n");
30717c478bd9Sstevel@tonic-gate 		}
30727c478bd9Sstevel@tonic-gate 
30737c478bd9Sstevel@tonic-gate 		if (cp->dn_flags & DT_NF_BITFIELD) {
30747c478bd9Sstevel@tonic-gate 			xyerror(D_ADDROF_BITFIELD,
30757c478bd9Sstevel@tonic-gate 			    "cannot take address of bit-field\n");
30767c478bd9Sstevel@tonic-gate 		}
30777c478bd9Sstevel@tonic-gate 
30787c478bd9Sstevel@tonic-gate 		dtt.dtt_object = NULL;
30797c478bd9Sstevel@tonic-gate 		dtt.dtt_ctfp = cp->dn_ctfp;
30807c478bd9Sstevel@tonic-gate 		dtt.dtt_type = cp->dn_type;
30817c478bd9Sstevel@tonic-gate 
30827c478bd9Sstevel@tonic-gate 		if (dt_type_pointer(&dtt) == -1) {
30837c478bd9Sstevel@tonic-gate 			xyerror(D_TYPE_ERR, "cannot find type for \"&\": %s*\n",
30847c478bd9Sstevel@tonic-gate 			    dt_node_type_name(cp, n, sizeof (n)));
30857c478bd9Sstevel@tonic-gate 		}
30867c478bd9Sstevel@tonic-gate 
3087a386cc11SRobert Mustacchi 		dt_node_type_assign(dnp, dtt.dtt_ctfp, dtt.dtt_type,
3088a386cc11SRobert Mustacchi 		    cp->dn_flags & DT_NF_USERLAND);
30897c478bd9Sstevel@tonic-gate 		break;
30907c478bd9Sstevel@tonic-gate 
30917c478bd9Sstevel@tonic-gate 	case DT_TOK_SIZEOF:
30927c478bd9Sstevel@tonic-gate 		if (cp->dn_flags & DT_NF_BITFIELD) {
30937c478bd9Sstevel@tonic-gate 			xyerror(D_SIZEOF_BITFIELD,
30947c478bd9Sstevel@tonic-gate 			    "cannot apply sizeof to a bit-field\n");
30957c478bd9Sstevel@tonic-gate 		}
30967c478bd9Sstevel@tonic-gate 
30977c478bd9Sstevel@tonic-gate 		if (dt_node_sizeof(cp) == 0) {
30987c478bd9Sstevel@tonic-gate 			xyerror(D_SIZEOF_TYPE, "cannot apply sizeof to an "
30997c478bd9Sstevel@tonic-gate 			    "operand of unknown size\n");
31007c478bd9Sstevel@tonic-gate 		}
31017c478bd9Sstevel@tonic-gate 
31027c478bd9Sstevel@tonic-gate 		dt_node_type_assign(dnp, dtp->dt_ddefs->dm_ctfp,
3103a386cc11SRobert Mustacchi 		    ctf_lookup_by_name(dtp->dt_ddefs->dm_ctfp, "size_t"),
3104a386cc11SRobert Mustacchi 		    B_FALSE);
31057c478bd9Sstevel@tonic-gate 		break;
31067c478bd9Sstevel@tonic-gate 
31077c478bd9Sstevel@tonic-gate 	case DT_TOK_STRINGOF:
31087c478bd9Sstevel@tonic-gate 		if (!dt_node_is_scalar(cp) && !dt_node_is_pointer(cp) &&
31097c478bd9Sstevel@tonic-gate 		    !dt_node_is_strcompat(cp)) {
31107c478bd9Sstevel@tonic-gate 			xyerror(D_STRINGOF_TYPE,
31117c478bd9Sstevel@tonic-gate 			    "cannot apply stringof to a value of type %s\n",
31127c478bd9Sstevel@tonic-gate 			    dt_node_type_name(cp, n, sizeof (n)));
31137c478bd9Sstevel@tonic-gate 		}
3114a386cc11SRobert Mustacchi 		dt_node_type_assign(dnp, DT_STR_CTFP(dtp), DT_STR_TYPE(dtp),
3115a386cc11SRobert Mustacchi 		    cp->dn_flags & DT_NF_USERLAND);
31167c478bd9Sstevel@tonic-gate 		break;
31177c478bd9Sstevel@tonic-gate 
31187c478bd9Sstevel@tonic-gate 	case DT_TOK_PREINC:
31197c478bd9Sstevel@tonic-gate 	case DT_TOK_POSTINC:
31207c478bd9Sstevel@tonic-gate 	case DT_TOK_PREDEC:
31217c478bd9Sstevel@tonic-gate 	case DT_TOK_POSTDEC:
31227c478bd9Sstevel@tonic-gate 		if (dt_node_is_scalar(cp) == 0) {
31237c478bd9Sstevel@tonic-gate 			xyerror(D_OP_SCALAR, "operator %s requires operand of "
31247c478bd9Sstevel@tonic-gate 			    "scalar type\n", opstr(dnp->dn_op));
31257c478bd9Sstevel@tonic-gate 		}
31267c478bd9Sstevel@tonic-gate 
31277c478bd9Sstevel@tonic-gate 		if (dt_node_is_vfptr(cp)) {
31287c478bd9Sstevel@tonic-gate 			xyerror(D_OP_VFPTR, "operator %s requires an operand "
31297c478bd9Sstevel@tonic-gate 			    "of known size\n", opstr(dnp->dn_op));
31307c478bd9Sstevel@tonic-gate 		}
31317c478bd9Sstevel@tonic-gate 
31327c478bd9Sstevel@tonic-gate 		if (!(cp->dn_flags & DT_NF_LVALUE)) {
31337c478bd9Sstevel@tonic-gate 			xyerror(D_OP_LVAL, "operator %s requires modifiable "
31347c478bd9Sstevel@tonic-gate 			    "lvalue as an operand\n", opstr(dnp->dn_op));
31357c478bd9Sstevel@tonic-gate 		}
31367c478bd9Sstevel@tonic-gate 
31377c478bd9Sstevel@tonic-gate 		if (!(cp->dn_flags & DT_NF_WRITABLE)) {
31387c478bd9Sstevel@tonic-gate 			xyerror(D_OP_WRITE, "operator %s can only be applied "
31397c478bd9Sstevel@tonic-gate 			    "to a writable variable\n", opstr(dnp->dn_op));
31407c478bd9Sstevel@tonic-gate 		}
31417c478bd9Sstevel@tonic-gate 
31427c478bd9Sstevel@tonic-gate 		dt_node_type_propagate(cp, dnp); /* see K&R[A7.4.1] */
31437c478bd9Sstevel@tonic-gate 		break;
31447c478bd9Sstevel@tonic-gate 
31457c478bd9Sstevel@tonic-gate 	default:
31467c478bd9Sstevel@tonic-gate 		xyerror(D_UNKNOWN, "invalid unary op %s\n", opstr(dnp->dn_op));
31477c478bd9Sstevel@tonic-gate 	}
31487c478bd9Sstevel@tonic-gate 
31497c478bd9Sstevel@tonic-gate 	dt_node_attr_assign(dnp, cp->dn_attr);
31507c478bd9Sstevel@tonic-gate 	return (dnp);
31517c478bd9Sstevel@tonic-gate }
31527c478bd9Sstevel@tonic-gate 
3153e5803b76SAdam H. Leventhal static void
dt_assign_common(dt_node_t * dnp)3154e5803b76SAdam H. Leventhal dt_assign_common(dt_node_t *dnp)
3155e5803b76SAdam H. Leventhal {
3156e5803b76SAdam H. Leventhal 	dt_node_t *lp = dnp->dn_left;
3157e5803b76SAdam H. Leventhal 	dt_node_t *rp = dnp->dn_right;
3158e5803b76SAdam H. Leventhal 	int op = dnp->dn_op;
3159e5803b76SAdam H. Leventhal 
3160e5803b76SAdam H. Leventhal 	if (rp->dn_kind == DT_NODE_INT)
3161e5803b76SAdam H. Leventhal 		dt_cast(lp, rp);
3162e5803b76SAdam H. Leventhal 
3163e5803b76SAdam H. Leventhal 	if (!(lp->dn_flags & DT_NF_LVALUE)) {
3164e5803b76SAdam H. Leventhal 		xyerror(D_OP_LVAL, "operator %s requires modifiable "
3165e5803b76SAdam H. Leventhal 		    "lvalue as an operand\n", opstr(op));
3166e5803b76SAdam H. Leventhal 		/* see K&R[A7.17] */
3167e5803b76SAdam H. Leventhal 	}
3168e5803b76SAdam H. Leventhal 
3169e5803b76SAdam H. Leventhal 	if (!(lp->dn_flags & DT_NF_WRITABLE)) {
3170e5803b76SAdam H. Leventhal 		xyerror(D_OP_WRITE, "operator %s can only be applied "
3171e5803b76SAdam H. Leventhal 		    "to a writable variable\n", opstr(op));
3172e5803b76SAdam H. Leventhal 	}
3173e5803b76SAdam H. Leventhal 
3174e5803b76SAdam H. Leventhal 	dt_node_type_propagate(lp, dnp); /* see K&R[A7.17] */
3175e5803b76SAdam H. Leventhal 	dt_node_attr_assign(dnp, dt_attr_min(lp->dn_attr, rp->dn_attr));
3176e5803b76SAdam H. Leventhal }
3177e5803b76SAdam H. Leventhal 
31787c478bd9Sstevel@tonic-gate static dt_node_t *
dt_cook_op2(dt_node_t * dnp,uint_t idflags)31797c478bd9Sstevel@tonic-gate dt_cook_op2(dt_node_t *dnp, uint_t idflags)
31807c478bd9Sstevel@tonic-gate {
31817c478bd9Sstevel@tonic-gate 	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
31827c478bd9Sstevel@tonic-gate 	dt_node_t *lp = dnp->dn_left;
31837c478bd9Sstevel@tonic-gate 	dt_node_t *rp = dnp->dn_right;
31847c478bd9Sstevel@tonic-gate 	int op = dnp->dn_op;
31857c478bd9Sstevel@tonic-gate 
31867c478bd9Sstevel@tonic-gate 	ctf_membinfo_t m;
31877c478bd9Sstevel@tonic-gate 	ctf_file_t *ctfp;
31887c478bd9Sstevel@tonic-gate 	ctf_id_t type;
31897c478bd9Sstevel@tonic-gate 	int kind, val, uref;
31907c478bd9Sstevel@tonic-gate 	dt_ident_t *idp;
31917c478bd9Sstevel@tonic-gate 
31927c478bd9Sstevel@tonic-gate 	char n1[DT_TYPE_NAMELEN];
31937c478bd9Sstevel@tonic-gate 	char n2[DT_TYPE_NAMELEN];
31947c478bd9Sstevel@tonic-gate 
31957c478bd9Sstevel@tonic-gate 	/*
31967c478bd9Sstevel@tonic-gate 	 * The expression E1[E2] is identical by definition to *((E1)+(E2)) so
31977c478bd9Sstevel@tonic-gate 	 * we convert "[" to "+" and glue on "*" at the end (see K&R[A7.3.1])
31987c478bd9Sstevel@tonic-gate 	 * unless the left-hand side is an untyped D scalar, associative array,
31997c478bd9Sstevel@tonic-gate 	 * or aggregation.  In these cases, we proceed to case DT_TOK_LBRAC and
32007c478bd9Sstevel@tonic-gate 	 * handle associative array and aggregation references there.
32017c478bd9Sstevel@tonic-gate 	 */
32027c478bd9Sstevel@tonic-gate 	if (op == DT_TOK_LBRAC) {
32037c478bd9Sstevel@tonic-gate 		if (lp->dn_kind == DT_NODE_IDENT) {
32047c478bd9Sstevel@tonic-gate 			dt_idhash_t *dhp;
32057c478bd9Sstevel@tonic-gate 			uint_t idkind;
32067c478bd9Sstevel@tonic-gate 
32077c478bd9Sstevel@tonic-gate 			if (lp->dn_op == DT_TOK_AGG) {
32087c478bd9Sstevel@tonic-gate 				dhp = dtp->dt_aggs;
32097c478bd9Sstevel@tonic-gate 				idp = dt_idhash_lookup(dhp, lp->dn_string + 1);
32107c478bd9Sstevel@tonic-gate 				idkind = DT_IDENT_AGG;
32117c478bd9Sstevel@tonic-gate 			} else {
32127c478bd9Sstevel@tonic-gate 				dhp = dtp->dt_globals;
32137c478bd9Sstevel@tonic-gate 				idp = dt_idstack_lookup(
32147c478bd9Sstevel@tonic-gate 				    &yypcb->pcb_globals, lp->dn_string);
32157c478bd9Sstevel@tonic-gate 				idkind = DT_IDENT_ARRAY;
32167c478bd9Sstevel@tonic-gate 			}
32177c478bd9Sstevel@tonic-gate 
32187c478bd9Sstevel@tonic-gate 			if (idp == NULL || dt_ident_unref(idp))
32197c478bd9Sstevel@tonic-gate 				dt_xcook_ident(lp, dhp, idkind, B_TRUE);
32207c478bd9Sstevel@tonic-gate 			else
32217c478bd9Sstevel@tonic-gate 				dt_xcook_ident(lp, dhp, idp->di_kind, B_FALSE);
3222c3bd3abdSMatthew Ahrens 		} else {
32237c478bd9Sstevel@tonic-gate 			lp = dnp->dn_left = dt_node_cook(lp, 0);
3224c3bd3abdSMatthew Ahrens 		}
32257c478bd9Sstevel@tonic-gate 
32267c478bd9Sstevel@tonic-gate 		/*
32277c478bd9Sstevel@tonic-gate 		 * Switch op to '+' for *(E1 + E2) array mode in these cases:
32287c478bd9Sstevel@tonic-gate 		 * (a) lp is a DT_IDENT_ARRAY variable that has already been
32297c478bd9Sstevel@tonic-gate 		 *	referenced using [] notation (dn_args != NULL).
32307c478bd9Sstevel@tonic-gate 		 * (b) lp is a non-ARRAY variable that has already been given
32317c478bd9Sstevel@tonic-gate 		 *	a type by assignment or declaration (!dt_ident_unref())
32327c478bd9Sstevel@tonic-gate 		 * (c) lp is neither a variable nor an aggregation
32337c478bd9Sstevel@tonic-gate 		 */
32347c478bd9Sstevel@tonic-gate 		if (lp->dn_kind == DT_NODE_VAR) {
32357c478bd9Sstevel@tonic-gate 			if (lp->dn_ident->di_kind == DT_IDENT_ARRAY) {
32367c478bd9Sstevel@tonic-gate 				if (lp->dn_args != NULL)
32377c478bd9Sstevel@tonic-gate 					op = DT_TOK_ADD;
3238c3bd3abdSMatthew Ahrens 			} else if (!dt_ident_unref(lp->dn_ident)) {
32397c478bd9Sstevel@tonic-gate 				op = DT_TOK_ADD;
3240c3bd3abdSMatthew Ahrens 			}
3241c3bd3abdSMatthew Ahrens 		} else if (lp->dn_kind != DT_NODE_AGG) {
32427c478bd9Sstevel@tonic-gate 			op = DT_TOK_ADD;
32437c478bd9Sstevel@tonic-gate 		}
3244c3bd3abdSMatthew Ahrens 	}
32457c478bd9Sstevel@tonic-gate 
32467c478bd9Sstevel@tonic-gate 	switch (op) {
32477c478bd9Sstevel@tonic-gate 	case DT_TOK_BAND:
32487c478bd9Sstevel@tonic-gate 	case DT_TOK_XOR:
32497c478bd9Sstevel@tonic-gate 	case DT_TOK_BOR:
32507c478bd9Sstevel@tonic-gate 		lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF);
32517c478bd9Sstevel@tonic-gate 		rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
32527c478bd9Sstevel@tonic-gate 
32537c478bd9Sstevel@tonic-gate 		if (!dt_node_is_integer(lp) || !dt_node_is_integer(rp)) {
32547c478bd9Sstevel@tonic-gate 			xyerror(D_OP_INT, "operator %s requires operands of "
32557c478bd9Sstevel@tonic-gate 			    "integral type\n", opstr(op));
32567c478bd9Sstevel@tonic-gate 		}
32577c478bd9Sstevel@tonic-gate 
32587c478bd9Sstevel@tonic-gate 		dt_node_promote(lp, rp, dnp); /* see K&R[A7.11-13] */
32597c478bd9Sstevel@tonic-gate 		break;
32607c478bd9Sstevel@tonic-gate 
32617c478bd9Sstevel@tonic-gate 	case DT_TOK_LSH:
32627c478bd9Sstevel@tonic-gate 	case DT_TOK_RSH:
32637c478bd9Sstevel@tonic-gate 		lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF);
32647c478bd9Sstevel@tonic-gate 		rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
32657c478bd9Sstevel@tonic-gate 
32667c478bd9Sstevel@tonic-gate 		if (!dt_node_is_integer(lp) || !dt_node_is_integer(rp)) {
32677c478bd9Sstevel@tonic-gate 			xyerror(D_OP_INT, "operator %s requires operands of "
32687c478bd9Sstevel@tonic-gate 			    "integral type\n", opstr(op));
32697c478bd9Sstevel@tonic-gate 		}
32707c478bd9Sstevel@tonic-gate 
32717c478bd9Sstevel@tonic-gate 		dt_node_type_propagate(lp, dnp); /* see K&R[A7.8] */
32727c478bd9Sstevel@tonic-gate 		dt_node_attr_assign(dnp, dt_attr_min(lp->dn_attr, rp->dn_attr));
32737c478bd9Sstevel@tonic-gate 		break;
32747c478bd9Sstevel@tonic-gate 
32757c478bd9Sstevel@tonic-gate 	case DT_TOK_MOD:
32767c478bd9Sstevel@tonic-gate 		lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF);
32777c478bd9Sstevel@tonic-gate 		rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
32787c478bd9Sstevel@tonic-gate 
32797c478bd9Sstevel@tonic-gate 		if (!dt_node_is_integer(lp) || !dt_node_is_integer(rp)) {
32807c478bd9Sstevel@tonic-gate 			xyerror(D_OP_INT, "operator %s requires operands of "
32817c478bd9Sstevel@tonic-gate 			    "integral type\n", opstr(op));
32827c478bd9Sstevel@tonic-gate 		}
32837c478bd9Sstevel@tonic-gate 
32847c478bd9Sstevel@tonic-gate 		dt_node_promote(lp, rp, dnp); /* see K&R[A7.6] */
32857c478bd9Sstevel@tonic-gate 		break;
32867c478bd9Sstevel@tonic-gate 
32877c478bd9Sstevel@tonic-gate 	case DT_TOK_MUL:
32887c478bd9Sstevel@tonic-gate 	case DT_TOK_DIV:
32897c478bd9Sstevel@tonic-gate 		lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF);
32907c478bd9Sstevel@tonic-gate 		rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
32917c478bd9Sstevel@tonic-gate 
32927c478bd9Sstevel@tonic-gate 		if (!dt_node_is_arith(lp) || !dt_node_is_arith(rp)) {
32937c478bd9Sstevel@tonic-gate 			xyerror(D_OP_ARITH, "operator %s requires operands of "
32947c478bd9Sstevel@tonic-gate 			    "arithmetic type\n", opstr(op));
32957c478bd9Sstevel@tonic-gate 		}
32967c478bd9Sstevel@tonic-gate 
32977c478bd9Sstevel@tonic-gate 		dt_node_promote(lp, rp, dnp); /* see K&R[A7.6] */
32987c478bd9Sstevel@tonic-gate 		break;
32997c478bd9Sstevel@tonic-gate 
33007c478bd9Sstevel@tonic-gate 	case DT_TOK_LAND:
33017c478bd9Sstevel@tonic-gate 	case DT_TOK_LXOR:
33027c478bd9Sstevel@tonic-gate 	case DT_TOK_LOR:
33037c478bd9Sstevel@tonic-gate 		lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF);
33047c478bd9Sstevel@tonic-gate 		rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
33057c478bd9Sstevel@tonic-gate 
33067c478bd9Sstevel@tonic-gate 		if (!dt_node_is_scalar(lp) || !dt_node_is_scalar(rp)) {
33077c478bd9Sstevel@tonic-gate 			xyerror(D_OP_SCALAR, "operator %s requires operands "
33087c478bd9Sstevel@tonic-gate 			    "of scalar type\n", opstr(op));
33097c478bd9Sstevel@tonic-gate 		}
33107c478bd9Sstevel@tonic-gate 
3311a386cc11SRobert Mustacchi 		dt_node_type_assign(dnp, DT_INT_CTFP(dtp), DT_INT_TYPE(dtp),
3312a386cc11SRobert Mustacchi 		    B_FALSE);
33137c478bd9Sstevel@tonic-gate 		dt_node_attr_assign(dnp, dt_attr_min(lp->dn_attr, rp->dn_attr));
33147c478bd9Sstevel@tonic-gate 		break;
33157c478bd9Sstevel@tonic-gate 
33167c478bd9Sstevel@tonic-gate 	case DT_TOK_LT:
33177c478bd9Sstevel@tonic-gate 	case DT_TOK_LE:
33187c478bd9Sstevel@tonic-gate 	case DT_TOK_GT:
33197c478bd9Sstevel@tonic-gate 	case DT_TOK_GE:
33207c478bd9Sstevel@tonic-gate 	case DT_TOK_EQU:
33217c478bd9Sstevel@tonic-gate 	case DT_TOK_NEQ:
33227c478bd9Sstevel@tonic-gate 		/*
33237c478bd9Sstevel@tonic-gate 		 * The D comparison operators provide the ability to transform
33247c478bd9Sstevel@tonic-gate 		 * a right-hand identifier into a corresponding enum tag value
33257c478bd9Sstevel@tonic-gate 		 * if the left-hand side is an enum type.  To do this, we cook
33267c478bd9Sstevel@tonic-gate 		 * the left-hand side, and then see if the right-hand side is
33277c478bd9Sstevel@tonic-gate 		 * an unscoped identifier defined in the enum.  If so, we
33287c478bd9Sstevel@tonic-gate 		 * convert into an integer constant node with the tag's value.
33297c478bd9Sstevel@tonic-gate 		 */
33307c478bd9Sstevel@tonic-gate 		lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF);
33317c478bd9Sstevel@tonic-gate 
33327c478bd9Sstevel@tonic-gate 		kind = ctf_type_kind(lp->dn_ctfp,
33337c478bd9Sstevel@tonic-gate 		    ctf_type_resolve(lp->dn_ctfp, lp->dn_type));
33347c478bd9Sstevel@tonic-gate 
33357c478bd9Sstevel@tonic-gate 		if (kind == CTF_K_ENUM && rp->dn_kind == DT_NODE_IDENT &&
33367c478bd9Sstevel@tonic-gate 		    strchr(rp->dn_string, '`') == NULL && ctf_enum_value(
33377c478bd9Sstevel@tonic-gate 		    lp->dn_ctfp, lp->dn_type, rp->dn_string, &val) == 0) {
33387c478bd9Sstevel@tonic-gate 
33397c478bd9Sstevel@tonic-gate 			if ((idp = dt_idstack_lookup(&yypcb->pcb_globals,
33407c478bd9Sstevel@tonic-gate 			    rp->dn_string)) != NULL) {
33417c478bd9Sstevel@tonic-gate 				xyerror(D_IDENT_AMBIG,
33427c478bd9Sstevel@tonic-gate 				    "ambiguous use of operator %s: %s is "
33437c478bd9Sstevel@tonic-gate 				    "both a %s enum tag and a global %s\n",
33447c478bd9Sstevel@tonic-gate 				    opstr(op), rp->dn_string,
33457c478bd9Sstevel@tonic-gate 				    dt_node_type_name(lp, n1, sizeof (n1)),
33467c478bd9Sstevel@tonic-gate 				    dt_idkind_name(idp->di_kind));
33477c478bd9Sstevel@tonic-gate 			}
33487c478bd9Sstevel@tonic-gate 
33497c478bd9Sstevel@tonic-gate 			free(rp->dn_string);
33507c478bd9Sstevel@tonic-gate 			rp->dn_string = NULL;
33517c478bd9Sstevel@tonic-gate 			rp->dn_kind = DT_NODE_INT;
33527c478bd9Sstevel@tonic-gate 			rp->dn_flags |= DT_NF_COOKED;
33537c478bd9Sstevel@tonic-gate 			rp->dn_op = DT_TOK_INT;
33547c478bd9Sstevel@tonic-gate 			rp->dn_value = (intmax_t)val;
33557c478bd9Sstevel@tonic-gate 
3356a386cc11SRobert Mustacchi 			dt_node_type_assign(rp, lp->dn_ctfp, lp->dn_type,
3357a386cc11SRobert Mustacchi 			    B_FALSE);
33587c478bd9Sstevel@tonic-gate 			dt_node_attr_assign(rp, _dtrace_symattr);
33597c478bd9Sstevel@tonic-gate 		}
33607c478bd9Sstevel@tonic-gate 
33617c478bd9Sstevel@tonic-gate 		rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
33627c478bd9Sstevel@tonic-gate 
33637c478bd9Sstevel@tonic-gate 		/*
33647c478bd9Sstevel@tonic-gate 		 * The rules for type checking for the relational operators are
33657c478bd9Sstevel@tonic-gate 		 * described in the ANSI-C spec (see K&R[A7.9-10]).  We perform
33667c478bd9Sstevel@tonic-gate 		 * the various tests in order from least to most expensive.  We
33677c478bd9Sstevel@tonic-gate 		 * also allow derived strings to be compared as a first-class
33687c478bd9Sstevel@tonic-gate 		 * type (resulting in a strcmp(3C)-style comparison), and we
33697c478bd9Sstevel@tonic-gate 		 * slightly relax the A7.9 rules to permit void pointer
33707c478bd9Sstevel@tonic-gate 		 * comparisons as in A7.10.  Our users won't be confused by
33717c478bd9Sstevel@tonic-gate 		 * this since they understand pointers are just numbers, and
33727c478bd9Sstevel@tonic-gate 		 * relaxing this constraint simplifies the implementation.
33737c478bd9Sstevel@tonic-gate 		 */
33747c478bd9Sstevel@tonic-gate 		if (ctf_type_compat(lp->dn_ctfp, lp->dn_type,
33757c478bd9Sstevel@tonic-gate 		    rp->dn_ctfp, rp->dn_type))
33767c478bd9Sstevel@tonic-gate 			/*EMPTY*/;
33777c478bd9Sstevel@tonic-gate 		else if (dt_node_is_integer(lp) && dt_node_is_integer(rp))
33787c478bd9Sstevel@tonic-gate 			/*EMPTY*/;
33797c478bd9Sstevel@tonic-gate 		else if (dt_node_is_strcompat(lp) && dt_node_is_strcompat(rp) &&
33807c478bd9Sstevel@tonic-gate 		    (dt_node_is_string(lp) || dt_node_is_string(rp)))
33817c478bd9Sstevel@tonic-gate 			/*EMPTY*/;
33827c478bd9Sstevel@tonic-gate 		else if (dt_node_is_ptrcompat(lp, rp, NULL, NULL) == 0) {
33837c478bd9Sstevel@tonic-gate 			xyerror(D_OP_INCOMPAT, "operands have "
33847c478bd9Sstevel@tonic-gate 			    "incompatible types: \"%s\" %s \"%s\"\n",
33857c478bd9Sstevel@tonic-gate 			    dt_node_type_name(lp, n1, sizeof (n1)), opstr(op),
33867c478bd9Sstevel@tonic-gate 			    dt_node_type_name(rp, n2, sizeof (n2)));
33877c478bd9Sstevel@tonic-gate 		}
33887c478bd9Sstevel@tonic-gate 
3389a386cc11SRobert Mustacchi 		dt_node_type_assign(dnp, DT_INT_CTFP(dtp), DT_INT_TYPE(dtp),
3390a386cc11SRobert Mustacchi 		    B_FALSE);
33917c478bd9Sstevel@tonic-gate 		dt_node_attr_assign(dnp, dt_attr_min(lp->dn_attr, rp->dn_attr));
33927c478bd9Sstevel@tonic-gate 		break;
33937c478bd9Sstevel@tonic-gate 
33947c478bd9Sstevel@tonic-gate 	case DT_TOK_ADD:
33957c478bd9Sstevel@tonic-gate 	case DT_TOK_SUB: {
33967c478bd9Sstevel@tonic-gate 		/*
33977c478bd9Sstevel@tonic-gate 		 * The rules for type checking for the additive operators are
33987c478bd9Sstevel@tonic-gate 		 * described in the ANSI-C spec (see K&R[A7.7]).  Pointers and
3399e4586ebfSmws 		 * integers may be manipulated according to specific rules.  In
3400e4586ebfSmws 		 * these cases D permits strings to be treated as pointers.
34017c478bd9Sstevel@tonic-gate 		 */
34027c478bd9Sstevel@tonic-gate 		int lp_is_ptr, lp_is_int, rp_is_ptr, rp_is_int;
34037c478bd9Sstevel@tonic-gate 
34047c478bd9Sstevel@tonic-gate 		lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF);
34057c478bd9Sstevel@tonic-gate 		rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
34067c478bd9Sstevel@tonic-gate 
3407e4586ebfSmws 		lp_is_ptr = dt_node_is_string(lp) ||
3408e4586ebfSmws 		    (dt_node_is_pointer(lp) && !dt_node_is_vfptr(lp));
34097c478bd9Sstevel@tonic-gate 		lp_is_int = dt_node_is_integer(lp);
34107c478bd9Sstevel@tonic-gate 
3411e4586ebfSmws 		rp_is_ptr = dt_node_is_string(rp) ||
3412e4586ebfSmws 		    (dt_node_is_pointer(rp) && !dt_node_is_vfptr(rp));
34137c478bd9Sstevel@tonic-gate 		rp_is_int = dt_node_is_integer(rp);
34147c478bd9Sstevel@tonic-gate 
34157c478bd9Sstevel@tonic-gate 		if (lp_is_int && rp_is_int) {
34167c478bd9Sstevel@tonic-gate 			dt_type_promote(lp, rp, &ctfp, &type);
34177c478bd9Sstevel@tonic-gate 			uref = 0;
34187c478bd9Sstevel@tonic-gate 		} else if (lp_is_ptr && rp_is_int) {
34197c478bd9Sstevel@tonic-gate 			ctfp = lp->dn_ctfp;
34207c478bd9Sstevel@tonic-gate 			type = lp->dn_type;
34217c478bd9Sstevel@tonic-gate 			uref = lp->dn_flags & DT_NF_USERLAND;
34227c478bd9Sstevel@tonic-gate 		} else if (lp_is_int && rp_is_ptr && op == DT_TOK_ADD) {
34237c478bd9Sstevel@tonic-gate 			ctfp = rp->dn_ctfp;
34247c478bd9Sstevel@tonic-gate 			type = rp->dn_type;
34257c478bd9Sstevel@tonic-gate 			uref = rp->dn_flags & DT_NF_USERLAND;
34267c478bd9Sstevel@tonic-gate 		} else if (lp_is_ptr && rp_is_ptr && op == DT_TOK_SUB &&
34277c478bd9Sstevel@tonic-gate 		    dt_node_is_ptrcompat(lp, rp, NULL, NULL)) {
34287c478bd9Sstevel@tonic-gate 			ctfp = dtp->dt_ddefs->dm_ctfp;
34297c478bd9Sstevel@tonic-gate 			type = ctf_lookup_by_name(ctfp, "ptrdiff_t");
34307c478bd9Sstevel@tonic-gate 			uref = 0;
34317c478bd9Sstevel@tonic-gate 		} else {
34327c478bd9Sstevel@tonic-gate 			xyerror(D_OP_INCOMPAT, "operands have incompatible "
34337c478bd9Sstevel@tonic-gate 			    "types: \"%s\" %s \"%s\"\n",
34347c478bd9Sstevel@tonic-gate 			    dt_node_type_name(lp, n1, sizeof (n1)), opstr(op),
34357c478bd9Sstevel@tonic-gate 			    dt_node_type_name(rp, n2, sizeof (n2)));
34367c478bd9Sstevel@tonic-gate 		}
34377c478bd9Sstevel@tonic-gate 
3438a386cc11SRobert Mustacchi 		dt_node_type_assign(dnp, ctfp, type, B_FALSE);
34397c478bd9Sstevel@tonic-gate 		dt_node_attr_assign(dnp, dt_attr_min(lp->dn_attr, rp->dn_attr));
34407c478bd9Sstevel@tonic-gate 
34417c478bd9Sstevel@tonic-gate 		if (uref)
34427c478bd9Sstevel@tonic-gate 			dnp->dn_flags |= DT_NF_USERLAND;
34437c478bd9Sstevel@tonic-gate 		break;
34447c478bd9Sstevel@tonic-gate 	}
34457c478bd9Sstevel@tonic-gate 
34467c478bd9Sstevel@tonic-gate 	case DT_TOK_OR_EQ:
34477c478bd9Sstevel@tonic-gate 	case DT_TOK_XOR_EQ:
34487c478bd9Sstevel@tonic-gate 	case DT_TOK_AND_EQ:
34497c478bd9Sstevel@tonic-gate 	case DT_TOK_LSH_EQ:
34507c478bd9Sstevel@tonic-gate 	case DT_TOK_RSH_EQ:
34517c478bd9Sstevel@tonic-gate 	case DT_TOK_MOD_EQ:
34527c478bd9Sstevel@tonic-gate 		if (lp->dn_kind == DT_NODE_IDENT) {
34537c478bd9Sstevel@tonic-gate 			dt_xcook_ident(lp, dtp->dt_globals,
34547c478bd9Sstevel@tonic-gate 			    DT_IDENT_SCALAR, B_TRUE);
34557c478bd9Sstevel@tonic-gate 		}
34567c478bd9Sstevel@tonic-gate 
34577c478bd9Sstevel@tonic-gate 		lp = dnp->dn_left =
34587c478bd9Sstevel@tonic-gate 		    dt_node_cook(lp, DT_IDFLG_REF | DT_IDFLG_MOD);
34597c478bd9Sstevel@tonic-gate 
34607c478bd9Sstevel@tonic-gate 		rp = dnp->dn_right =
34617c478bd9Sstevel@tonic-gate 		    dt_node_cook(rp, DT_IDFLG_REF | DT_IDFLG_MOD);
34627c478bd9Sstevel@tonic-gate 
34637c478bd9Sstevel@tonic-gate 		if (!dt_node_is_integer(lp) || !dt_node_is_integer(rp)) {
34647c478bd9Sstevel@tonic-gate 			xyerror(D_OP_INT, "operator %s requires operands of "
34657c478bd9Sstevel@tonic-gate 			    "integral type\n", opstr(op));
34667c478bd9Sstevel@tonic-gate 		}
34677c478bd9Sstevel@tonic-gate 		goto asgn_common;
34687c478bd9Sstevel@tonic-gate 
34697c478bd9Sstevel@tonic-gate 	case DT_TOK_MUL_EQ:
34707c478bd9Sstevel@tonic-gate 	case DT_TOK_DIV_EQ:
34717c478bd9Sstevel@tonic-gate 		if (lp->dn_kind == DT_NODE_IDENT) {
34727c478bd9Sstevel@tonic-gate 			dt_xcook_ident(lp, dtp->dt_globals,
34737c478bd9Sstevel@tonic-gate 			    DT_IDENT_SCALAR, B_TRUE);
34747c478bd9Sstevel@tonic-gate 		}
34757c478bd9Sstevel@tonic-gate 
34767c478bd9Sstevel@tonic-gate 		lp = dnp->dn_left =
34777c478bd9Sstevel@tonic-gate 		    dt_node_cook(lp, DT_IDFLG_REF | DT_IDFLG_MOD);
34787c478bd9Sstevel@tonic-gate 
34797c478bd9Sstevel@tonic-gate 		rp = dnp->dn_right =
34807c478bd9Sstevel@tonic-gate 		    dt_node_cook(rp, DT_IDFLG_REF | DT_IDFLG_MOD);
34817c478bd9Sstevel@tonic-gate 
34827c478bd9Sstevel@tonic-gate 		if (!dt_node_is_arith(lp) || !dt_node_is_arith(rp)) {
34837c478bd9Sstevel@tonic-gate 			xyerror(D_OP_ARITH, "operator %s requires operands of "
34847c478bd9Sstevel@tonic-gate 			    "arithmetic type\n", opstr(op));
34857c478bd9Sstevel@tonic-gate 		}
34867c478bd9Sstevel@tonic-gate 		goto asgn_common;
34877c478bd9Sstevel@tonic-gate 
34887c478bd9Sstevel@tonic-gate 	case DT_TOK_ASGN:
34897c478bd9Sstevel@tonic-gate 		/*
34907c478bd9Sstevel@tonic-gate 		 * If the left-hand side is an identifier, attempt to resolve
34917c478bd9Sstevel@tonic-gate 		 * it as either an aggregation or scalar variable.  We pass
34927c478bd9Sstevel@tonic-gate 		 * B_TRUE to dt_xcook_ident to indicate that a new variable can
34937c478bd9Sstevel@tonic-gate 		 * be created if no matching variable exists in the namespace.
34947c478bd9Sstevel@tonic-gate 		 */
34957c478bd9Sstevel@tonic-gate 		if (lp->dn_kind == DT_NODE_IDENT) {
34967c478bd9Sstevel@tonic-gate 			if (lp->dn_op == DT_TOK_AGG) {
34977c478bd9Sstevel@tonic-gate 				dt_xcook_ident(lp, dtp->dt_aggs,
34987c478bd9Sstevel@tonic-gate 				    DT_IDENT_AGG, B_TRUE);
34997c478bd9Sstevel@tonic-gate 			} else {
35007c478bd9Sstevel@tonic-gate 				dt_xcook_ident(lp, dtp->dt_globals,
35017c478bd9Sstevel@tonic-gate 				    DT_IDENT_SCALAR, B_TRUE);
35027c478bd9Sstevel@tonic-gate 			}
35037c478bd9Sstevel@tonic-gate 		}
35047c478bd9Sstevel@tonic-gate 
35057c478bd9Sstevel@tonic-gate 		lp = dnp->dn_left = dt_node_cook(lp, 0); /* don't set mod yet */
35067c478bd9Sstevel@tonic-gate 		rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
35077c478bd9Sstevel@tonic-gate 
35087c478bd9Sstevel@tonic-gate 		/*
35097c478bd9Sstevel@tonic-gate 		 * If the left-hand side is an aggregation, verify that we are
35107c478bd9Sstevel@tonic-gate 		 * assigning it the result of an aggregating function.  Once
35117c478bd9Sstevel@tonic-gate 		 * we've done so, hide the func node in the aggregation and
35127c478bd9Sstevel@tonic-gate 		 * return the aggregation itself up to the parse tree parent.
35137c478bd9Sstevel@tonic-gate 		 * This transformation is legal since the assigned function
35147c478bd9Sstevel@tonic-gate 		 * cannot change identity across disjoint cooking passes and
35157c478bd9Sstevel@tonic-gate 		 * the argument list subtree is retained for later cooking.
35167c478bd9Sstevel@tonic-gate 		 */
35177c478bd9Sstevel@tonic-gate 		if (lp->dn_kind == DT_NODE_AGG) {
35187c478bd9Sstevel@tonic-gate 			const char *aname = lp->dn_ident->di_name;
35197c478bd9Sstevel@tonic-gate 			dt_ident_t *oid = lp->dn_ident->di_iarg;
35207c478bd9Sstevel@tonic-gate 
35217c478bd9Sstevel@tonic-gate 			if (rp->dn_kind != DT_NODE_FUNC ||
35227c478bd9Sstevel@tonic-gate 			    rp->dn_ident->di_kind != DT_IDENT_AGGFUNC) {
35237c478bd9Sstevel@tonic-gate 				xyerror(D_AGG_FUNC,
35247c478bd9Sstevel@tonic-gate 				    "@%s must be assigned the result of "
35257c478bd9Sstevel@tonic-gate 				    "an aggregating function\n", aname);
35267c478bd9Sstevel@tonic-gate 			}
35277c478bd9Sstevel@tonic-gate 
35287c478bd9Sstevel@tonic-gate 			if (oid != NULL && oid != rp->dn_ident) {
35297c478bd9Sstevel@tonic-gate 				xyerror(D_AGG_REDEF,
35307c478bd9Sstevel@tonic-gate 				    "aggregation redefined: @%s\n\t "
35317c478bd9Sstevel@tonic-gate 				    "current: @%s = %s( )\n\tprevious: @%s = "
35327c478bd9Sstevel@tonic-gate 				    "%s( ) : line %d\n", aname, aname,
35337c478bd9Sstevel@tonic-gate 				    rp->dn_ident->di_name, aname, oid->di_name,
35347c478bd9Sstevel@tonic-gate 				    lp->dn_ident->di_lineno);
35357c478bd9Sstevel@tonic-gate 			} else if (oid == NULL)
35367c478bd9Sstevel@tonic-gate 				lp->dn_ident->di_iarg = rp->dn_ident;
35377c478bd9Sstevel@tonic-gate 
35387c478bd9Sstevel@tonic-gate 			/*
35397c478bd9Sstevel@tonic-gate 			 * Do not allow multiple aggregation assignments in a
35407c478bd9Sstevel@tonic-gate 			 * single statement, e.g. (@a = count()) = count();
35417c478bd9Sstevel@tonic-gate 			 * We produce a message as if the result of aggregating
35427c478bd9Sstevel@tonic-gate 			 * function does not propagate DT_NF_LVALUE.
35437c478bd9Sstevel@tonic-gate 			 */
35447c478bd9Sstevel@tonic-gate 			if (lp->dn_aggfun != NULL) {
35457c478bd9Sstevel@tonic-gate 				xyerror(D_OP_LVAL, "operator = requires "
35467c478bd9Sstevel@tonic-gate 				    "modifiable lvalue as an operand\n");
35477c478bd9Sstevel@tonic-gate 			}
35487c478bd9Sstevel@tonic-gate 
35497c478bd9Sstevel@tonic-gate 			lp->dn_aggfun = rp;
35507c478bd9Sstevel@tonic-gate 			lp = dt_node_cook(lp, DT_IDFLG_MOD);
35517c478bd9Sstevel@tonic-gate 
35527c478bd9Sstevel@tonic-gate 			dnp->dn_left = dnp->dn_right = NULL;
35537c478bd9Sstevel@tonic-gate 			dt_node_free(dnp);
35547c478bd9Sstevel@tonic-gate 
35557c478bd9Sstevel@tonic-gate 			return (lp);
35567c478bd9Sstevel@tonic-gate 		}
35577c478bd9Sstevel@tonic-gate 
35587c478bd9Sstevel@tonic-gate 		/*
35597c478bd9Sstevel@tonic-gate 		 * If the right-hand side is a dynamic variable that is the
35607c478bd9Sstevel@tonic-gate 		 * output of a translator, our result is the translated type.
35617c478bd9Sstevel@tonic-gate 		 */
35627c478bd9Sstevel@tonic-gate 		if ((idp = dt_node_resolve(rp, DT_IDENT_XLSOU)) != NULL) {
35637c478bd9Sstevel@tonic-gate 			ctfp = idp->di_ctfp;
35647c478bd9Sstevel@tonic-gate 			type = idp->di_type;
35657c478bd9Sstevel@tonic-gate 			uref = idp->di_flags & DT_IDFLG_USER;
35667c478bd9Sstevel@tonic-gate 		} else {
35677c478bd9Sstevel@tonic-gate 			ctfp = rp->dn_ctfp;
35687c478bd9Sstevel@tonic-gate 			type = rp->dn_type;
35697c478bd9Sstevel@tonic-gate 			uref = rp->dn_flags & DT_NF_USERLAND;
35707c478bd9Sstevel@tonic-gate 		}
35717c478bd9Sstevel@tonic-gate 
35727c478bd9Sstevel@tonic-gate 		/*
35737c478bd9Sstevel@tonic-gate 		 * If the left-hand side of an assignment statement is a virgin
35747c478bd9Sstevel@tonic-gate 		 * variable created by this compilation pass, reset the type of
35757c478bd9Sstevel@tonic-gate 		 * this variable to the type of the right-hand side.
35767c478bd9Sstevel@tonic-gate 		 */
35777c478bd9Sstevel@tonic-gate 		if (lp->dn_kind == DT_NODE_VAR &&
35787c478bd9Sstevel@tonic-gate 		    dt_ident_unref(lp->dn_ident)) {
3579a386cc11SRobert Mustacchi 			dt_node_type_assign(lp, ctfp, type, B_FALSE);
35807c478bd9Sstevel@tonic-gate 			dt_ident_type_assign(lp->dn_ident, ctfp, type);
35817c478bd9Sstevel@tonic-gate 
35827c478bd9Sstevel@tonic-gate 			if (uref) {
35837c478bd9Sstevel@tonic-gate 				lp->dn_flags |= DT_NF_USERLAND;
35847c478bd9Sstevel@tonic-gate 				lp->dn_ident->di_flags |= DT_IDFLG_USER;
35857c478bd9Sstevel@tonic-gate 			}
35867c478bd9Sstevel@tonic-gate 		}
35877c478bd9Sstevel@tonic-gate 
35887c478bd9Sstevel@tonic-gate 		if (lp->dn_kind == DT_NODE_VAR)
35897c478bd9Sstevel@tonic-gate 			lp->dn_ident->di_flags |= DT_IDFLG_MOD;
35907c478bd9Sstevel@tonic-gate 
35917c478bd9Sstevel@tonic-gate 		/*
35927c478bd9Sstevel@tonic-gate 		 * The rules for type checking for the assignment operators are
35937c478bd9Sstevel@tonic-gate 		 * described in the ANSI-C spec (see K&R[A7.17]).  We share
35947c478bd9Sstevel@tonic-gate 		 * most of this code with the argument list checking code.
35957c478bd9Sstevel@tonic-gate 		 */
35967c478bd9Sstevel@tonic-gate 		if (!dt_node_is_string(lp)) {
35977c478bd9Sstevel@tonic-gate 			kind = ctf_type_kind(lp->dn_ctfp,
35987c478bd9Sstevel@tonic-gate 			    ctf_type_resolve(lp->dn_ctfp, lp->dn_type));
35997c478bd9Sstevel@tonic-gate 
36007c478bd9Sstevel@tonic-gate 			if (kind == CTF_K_ARRAY || kind == CTF_K_FUNCTION) {
36017c478bd9Sstevel@tonic-gate 				xyerror(D_OP_ARRFUN, "operator %s may not be "
36027c478bd9Sstevel@tonic-gate 				    "applied to operand of type \"%s\"\n",
36037c478bd9Sstevel@tonic-gate 				    opstr(op),
36047c478bd9Sstevel@tonic-gate 				    dt_node_type_name(lp, n1, sizeof (n1)));
36057c478bd9Sstevel@tonic-gate 			}
36067c478bd9Sstevel@tonic-gate 		}
36077c478bd9Sstevel@tonic-gate 
36087c478bd9Sstevel@tonic-gate 		if (idp != NULL && idp->di_kind == DT_IDENT_XLSOU &&
36097c478bd9Sstevel@tonic-gate 		    ctf_type_compat(lp->dn_ctfp, lp->dn_type, ctfp, type))
36107c478bd9Sstevel@tonic-gate 			goto asgn_common;
36117c478bd9Sstevel@tonic-gate 
36127c478bd9Sstevel@tonic-gate 		if (dt_node_is_argcompat(lp, rp))
36137c478bd9Sstevel@tonic-gate 			goto asgn_common;
36147c478bd9Sstevel@tonic-gate 
36157c478bd9Sstevel@tonic-gate 		xyerror(D_OP_INCOMPAT,
36167c478bd9Sstevel@tonic-gate 		    "operands have incompatible types: \"%s\" %s \"%s\"\n",
36177c478bd9Sstevel@tonic-gate 		    dt_node_type_name(lp, n1, sizeof (n1)), opstr(op),
36187c478bd9Sstevel@tonic-gate 		    dt_node_type_name(rp, n2, sizeof (n2)));
36197c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
36207c478bd9Sstevel@tonic-gate 
36217c478bd9Sstevel@tonic-gate 	case DT_TOK_ADD_EQ:
36227c478bd9Sstevel@tonic-gate 	case DT_TOK_SUB_EQ:
36237c478bd9Sstevel@tonic-gate 		if (lp->dn_kind == DT_NODE_IDENT) {
36247c478bd9Sstevel@tonic-gate 			dt_xcook_ident(lp, dtp->dt_globals,
36257c478bd9Sstevel@tonic-gate 			    DT_IDENT_SCALAR, B_TRUE);
36267c478bd9Sstevel@tonic-gate 		}
36277c478bd9Sstevel@tonic-gate 
36287c478bd9Sstevel@tonic-gate 		lp = dnp->dn_left =
36297c478bd9Sstevel@tonic-gate 		    dt_node_cook(lp, DT_IDFLG_REF | DT_IDFLG_MOD);
36307c478bd9Sstevel@tonic-gate 
36317c478bd9Sstevel@tonic-gate 		rp = dnp->dn_right =
36327c478bd9Sstevel@tonic-gate 		    dt_node_cook(rp, DT_IDFLG_REF | DT_IDFLG_MOD);
36337c478bd9Sstevel@tonic-gate 
36347c478bd9Sstevel@tonic-gate 		if (dt_node_is_string(lp) || dt_node_is_string(rp)) {
36357c478bd9Sstevel@tonic-gate 			xyerror(D_OP_INCOMPAT, "operands have "
36367c478bd9Sstevel@tonic-gate 			    "incompatible types: \"%s\" %s \"%s\"\n",
36377c478bd9Sstevel@tonic-gate 			    dt_node_type_name(lp, n1, sizeof (n1)), opstr(op),
36387c478bd9Sstevel@tonic-gate 			    dt_node_type_name(rp, n2, sizeof (n2)));
36397c478bd9Sstevel@tonic-gate 		}
36407c478bd9Sstevel@tonic-gate 
36417c478bd9Sstevel@tonic-gate 		/*
36427c478bd9Sstevel@tonic-gate 		 * The rules for type checking for the assignment operators are
36437c478bd9Sstevel@tonic-gate 		 * described in the ANSI-C spec (see K&R[A7.17]).  To these
36447c478bd9Sstevel@tonic-gate 		 * rules we add that only writable D nodes can be modified.
36457c478bd9Sstevel@tonic-gate 		 */
36467c478bd9Sstevel@tonic-gate 		if (dt_node_is_integer(lp) == 0 ||
36477c478bd9Sstevel@tonic-gate 		    dt_node_is_integer(rp) == 0) {
36487c478bd9Sstevel@tonic-gate 			if (!dt_node_is_pointer(lp) || dt_node_is_vfptr(lp)) {
36497c478bd9Sstevel@tonic-gate 				xyerror(D_OP_VFPTR,
36507c478bd9Sstevel@tonic-gate 				    "operator %s requires left-hand scalar "
36517c478bd9Sstevel@tonic-gate 				    "operand of known size\n", opstr(op));
36527c478bd9Sstevel@tonic-gate 			} else if (dt_node_is_integer(rp) == 0 &&
36537c478bd9Sstevel@tonic-gate 			    dt_node_is_ptrcompat(lp, rp, NULL, NULL) == 0) {
36547c478bd9Sstevel@tonic-gate 				xyerror(D_OP_INCOMPAT, "operands have "
36557c478bd9Sstevel@tonic-gate 				    "incompatible types: \"%s\" %s \"%s\"\n",
36567c478bd9Sstevel@tonic-gate 				    dt_node_type_name(lp, n1, sizeof (n1)),
36577c478bd9Sstevel@tonic-gate 				    opstr(op),
36587c478bd9Sstevel@tonic-gate 				    dt_node_type_name(rp, n2, sizeof (n2)));
36597c478bd9Sstevel@tonic-gate 			}
36607c478bd9Sstevel@tonic-gate 		}
36617c478bd9Sstevel@tonic-gate asgn_common:
3662e5803b76SAdam H. Leventhal 		dt_assign_common(dnp);
36637c478bd9Sstevel@tonic-gate 		break;
36647c478bd9Sstevel@tonic-gate 
36657c478bd9Sstevel@tonic-gate 	case DT_TOK_PTR:
36667c478bd9Sstevel@tonic-gate 		/*
3667c3bd3abdSMatthew Ahrens 		 * If the left-hand side of operator -> is one of the scoping
3668c3bd3abdSMatthew Ahrens 		 * keywords, permit a local or thread variable to be created or
3669c3bd3abdSMatthew Ahrens 		 * referenced.
36707c478bd9Sstevel@tonic-gate 		 */
3671c3bd3abdSMatthew Ahrens 		if (lp->dn_kind == DT_NODE_IDENT) {
3672c3bd3abdSMatthew Ahrens 			dt_idhash_t *dhp = NULL;
3673c3bd3abdSMatthew Ahrens 
3674c3bd3abdSMatthew Ahrens 			if (strcmp(lp->dn_string, "self") == 0) {
3675c3bd3abdSMatthew Ahrens 				dhp = dtp->dt_tls;
3676c3bd3abdSMatthew Ahrens 			} else if (strcmp(lp->dn_string, "this") == 0) {
3677c3bd3abdSMatthew Ahrens 				dhp = yypcb->pcb_locals;
3678c3bd3abdSMatthew Ahrens 			}
3679c3bd3abdSMatthew Ahrens 			if (dhp != NULL) {
36807c478bd9Sstevel@tonic-gate 				if (rp->dn_kind != DT_NODE_VAR) {
3681c3bd3abdSMatthew Ahrens 					dt_xcook_ident(rp, dhp,
36827c478bd9Sstevel@tonic-gate 					    DT_IDENT_SCALAR, B_TRUE);
36837c478bd9Sstevel@tonic-gate 				}
36847c478bd9Sstevel@tonic-gate 
36857c478bd9Sstevel@tonic-gate 				if (idflags != 0)
36867c478bd9Sstevel@tonic-gate 					rp = dt_node_cook(rp, idflags);
36877c478bd9Sstevel@tonic-gate 
3688c3bd3abdSMatthew Ahrens 				/* avoid freeing rp */
3689c3bd3abdSMatthew Ahrens 				dnp->dn_right = dnp->dn_left;
36907c478bd9Sstevel@tonic-gate 				dt_node_free(dnp);
36917c478bd9Sstevel@tonic-gate 				return (rp);
36927c478bd9Sstevel@tonic-gate 			}
36937c478bd9Sstevel@tonic-gate 		}
36947c478bd9Sstevel@tonic-gate 		/*FALLTHRU*/
36957c478bd9Sstevel@tonic-gate 	case DT_TOK_DOT:
36967c478bd9Sstevel@tonic-gate 		lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF);
36977c478bd9Sstevel@tonic-gate 
36987c478bd9Sstevel@tonic-gate 		if (rp->dn_kind != DT_NODE_IDENT) {
36997c478bd9Sstevel@tonic-gate 			xyerror(D_OP_IDENT, "operator %s must be followed by "
37007c478bd9Sstevel@tonic-gate 			    "an identifier\n", opstr(op));
37017c478bd9Sstevel@tonic-gate 		}
37027c478bd9Sstevel@tonic-gate 
37037c478bd9Sstevel@tonic-gate 		if ((idp = dt_node_resolve(lp, DT_IDENT_XLSOU)) != NULL ||
37047c478bd9Sstevel@tonic-gate 		    (idp = dt_node_resolve(lp, DT_IDENT_XLPTR)) != NULL) {
37057c478bd9Sstevel@tonic-gate 			/*
37067c478bd9Sstevel@tonic-gate 			 * If the left-hand side is a translated struct or ptr,
37077c478bd9Sstevel@tonic-gate 			 * the type of the left is the translation output type.
37087c478bd9Sstevel@tonic-gate 			 */
37097c478bd9Sstevel@tonic-gate 			dt_xlator_t *dxp = idp->di_data;
37107c478bd9Sstevel@tonic-gate 
37117c478bd9Sstevel@tonic-gate 			if (dt_xlator_member(dxp, rp->dn_string) == NULL) {
37127c478bd9Sstevel@tonic-gate 				xyerror(D_XLATE_NOCONV,
37137c478bd9Sstevel@tonic-gate 				    "translator does not define conversion "
37147c478bd9Sstevel@tonic-gate 				    "for member: %s\n", rp->dn_string);
37157c478bd9Sstevel@tonic-gate 			}
37167c478bd9Sstevel@tonic-gate 
37177c478bd9Sstevel@tonic-gate 			ctfp = idp->di_ctfp;
37187c478bd9Sstevel@tonic-gate 			type = ctf_type_resolve(ctfp, idp->di_type);
37197c478bd9Sstevel@tonic-gate 			uref = idp->di_flags & DT_IDFLG_USER;
37207c478bd9Sstevel@tonic-gate 		} else {
37217c478bd9Sstevel@tonic-gate 			ctfp = lp->dn_ctfp;
37227c478bd9Sstevel@tonic-gate 			type = ctf_type_resolve(ctfp, lp->dn_type);
37237c478bd9Sstevel@tonic-gate 			uref = lp->dn_flags & DT_NF_USERLAND;
37247c478bd9Sstevel@tonic-gate 		}
37257c478bd9Sstevel@tonic-gate 
37267c478bd9Sstevel@tonic-gate 		kind = ctf_type_kind(ctfp, type);
37277c478bd9Sstevel@tonic-gate 
37287c478bd9Sstevel@tonic-gate 		if (op == DT_TOK_PTR) {
37297c478bd9Sstevel@tonic-gate 			if (kind != CTF_K_POINTER) {
37307c478bd9Sstevel@tonic-gate 				xyerror(D_OP_PTR, "operator %s must be "
37317c478bd9Sstevel@tonic-gate 				    "applied to a pointer\n", opstr(op));
37327c478bd9Sstevel@tonic-gate 			}
37337c478bd9Sstevel@tonic-gate 			type = ctf_type_reference(ctfp, type);
37347c478bd9Sstevel@tonic-gate 			type = ctf_type_resolve(ctfp, type);
37357c478bd9Sstevel@tonic-gate 			kind = ctf_type_kind(ctfp, type);
37367c478bd9Sstevel@tonic-gate 		}
37377c478bd9Sstevel@tonic-gate 
37387c478bd9Sstevel@tonic-gate 		/*
37397c478bd9Sstevel@tonic-gate 		 * If we follow a reference to a forward declaration tag,
37407c478bd9Sstevel@tonic-gate 		 * search the entire type space for the actual definition.
37417c478bd9Sstevel@tonic-gate 		 */
37427c478bd9Sstevel@tonic-gate 		while (kind == CTF_K_FORWARD) {
37437c478bd9Sstevel@tonic-gate 			char *tag = ctf_type_name(ctfp, type, n1, sizeof (n1));
37447c478bd9Sstevel@tonic-gate 			dtrace_typeinfo_t dtt;
37457c478bd9Sstevel@tonic-gate 
37467c478bd9Sstevel@tonic-gate 			if (tag != NULL && dt_type_lookup(tag, &dtt) == 0 &&
37477c478bd9Sstevel@tonic-gate 			    (dtt.dtt_ctfp != ctfp || dtt.dtt_type != type)) {
37487c478bd9Sstevel@tonic-gate 				ctfp = dtt.dtt_ctfp;
37497c478bd9Sstevel@tonic-gate 				type = ctf_type_resolve(ctfp, dtt.dtt_type);
37507c478bd9Sstevel@tonic-gate 				kind = ctf_type_kind(ctfp, type);
37517c478bd9Sstevel@tonic-gate 			} else {
37527c478bd9Sstevel@tonic-gate 				xyerror(D_OP_INCOMPLETE,
37537c478bd9Sstevel@tonic-gate 				    "operator %s cannot be applied to a "
37547c478bd9Sstevel@tonic-gate 				    "forward declaration: no %s definition "
37557c478bd9Sstevel@tonic-gate 				    "is available\n", opstr(op), tag);
37567c478bd9Sstevel@tonic-gate 			}
37577c478bd9Sstevel@tonic-gate 		}
37587c478bd9Sstevel@tonic-gate 
37597c478bd9Sstevel@tonic-gate 		if (kind != CTF_K_STRUCT && kind != CTF_K_UNION) {
37607c478bd9Sstevel@tonic-gate 			if (op == DT_TOK_PTR) {
37617c478bd9Sstevel@tonic-gate 				xyerror(D_OP_SOU, "operator -> cannot be "
37627c478bd9Sstevel@tonic-gate 				    "applied to pointer to type \"%s\"; must "
37637c478bd9Sstevel@tonic-gate 				    "be applied to a struct or union pointer\n",
37647c478bd9Sstevel@tonic-gate 				    ctf_type_name(ctfp, type, n1, sizeof (n1)));
37657c478bd9Sstevel@tonic-gate 			} else {
37667c478bd9Sstevel@tonic-gate 				xyerror(D_OP_SOU, "operator %s cannot be "
37677c478bd9Sstevel@tonic-gate 				    "applied to type \"%s\"; must be applied "
37687c478bd9Sstevel@tonic-gate 				    "to a struct or union\n", opstr(op),
37697c478bd9Sstevel@tonic-gate 				    ctf_type_name(ctfp, type, n1, sizeof (n1)));
37707c478bd9Sstevel@tonic-gate 			}
37717c478bd9Sstevel@tonic-gate 		}
37727c478bd9Sstevel@tonic-gate 
37737c478bd9Sstevel@tonic-gate 		if (ctf_member_info(ctfp, type, rp->dn_string, &m) == CTF_ERR) {
37747c478bd9Sstevel@tonic-gate 			xyerror(D_TYPE_MEMBER,
37757c478bd9Sstevel@tonic-gate 			    "%s is not a member of %s\n", rp->dn_string,
37767c478bd9Sstevel@tonic-gate 			    ctf_type_name(ctfp, type, n1, sizeof (n1)));
37777c478bd9Sstevel@tonic-gate 		}
37787c478bd9Sstevel@tonic-gate 
37797c478bd9Sstevel@tonic-gate 		type = ctf_type_resolve(ctfp, m.ctm_type);
37807c478bd9Sstevel@tonic-gate 		kind = ctf_type_kind(ctfp, type);
37817c478bd9Sstevel@tonic-gate 
3782*6eeafb34SRobert Mustacchi 		dt_node_type_assign_member(dnp, ctfp, m.ctm_type, B_FALSE,
3783*6eeafb34SRobert Mustacchi 		    m.ctm_offset);
37847c478bd9Sstevel@tonic-gate 		dt_node_attr_assign(dnp, lp->dn_attr);
37857c478bd9Sstevel@tonic-gate 
37867c478bd9Sstevel@tonic-gate 		if (op == DT_TOK_PTR && (kind != CTF_K_ARRAY ||
37877c478bd9Sstevel@tonic-gate 		    dt_node_is_string(dnp)))
37887c478bd9Sstevel@tonic-gate 			dnp->dn_flags |= DT_NF_LVALUE; /* see K&R[A7.3.3] */
37897c478bd9Sstevel@tonic-gate 
37907c478bd9Sstevel@tonic-gate 		if (op == DT_TOK_DOT && (lp->dn_flags & DT_NF_LVALUE) &&
37917c478bd9Sstevel@tonic-gate 		    (kind != CTF_K_ARRAY || dt_node_is_string(dnp)))
37927c478bd9Sstevel@tonic-gate 			dnp->dn_flags |= DT_NF_LVALUE; /* see K&R[A7.3.3] */
37937c478bd9Sstevel@tonic-gate 
37947c478bd9Sstevel@tonic-gate 		if (lp->dn_flags & DT_NF_WRITABLE)
37957c478bd9Sstevel@tonic-gate 			dnp->dn_flags |= DT_NF_WRITABLE;
37967c478bd9Sstevel@tonic-gate 
37977c478bd9Sstevel@tonic-gate 		if (uref && (kind == CTF_K_POINTER ||
37987c478bd9Sstevel@tonic-gate 		    (dnp->dn_flags & DT_NF_REF)))
37997c478bd9Sstevel@tonic-gate 			dnp->dn_flags |= DT_NF_USERLAND;
38007c478bd9Sstevel@tonic-gate 		break;
38017c478bd9Sstevel@tonic-gate 
38027c478bd9Sstevel@tonic-gate 	case DT_TOK_LBRAC: {
38037c478bd9Sstevel@tonic-gate 		/*
38047c478bd9Sstevel@tonic-gate 		 * If op is DT_TOK_LBRAC, we know from the special-case code at
38057c478bd9Sstevel@tonic-gate 		 * the top that lp is either a D variable or an aggregation.
38067c478bd9Sstevel@tonic-gate 		 */
38077c478bd9Sstevel@tonic-gate 		dt_node_t *lnp;
38087c478bd9Sstevel@tonic-gate 
38097c478bd9Sstevel@tonic-gate 		/*
38107c478bd9Sstevel@tonic-gate 		 * If the left-hand side is an aggregation, just set dn_aggtup
38117c478bd9Sstevel@tonic-gate 		 * to the right-hand side and return the cooked aggregation.
38127c478bd9Sstevel@tonic-gate 		 * This transformation is legal since we are just collapsing
38137c478bd9Sstevel@tonic-gate 		 * nodes to simplify later processing, and the entire aggtup
38147c478bd9Sstevel@tonic-gate 		 * parse subtree is retained for subsequent cooking passes.
38157c478bd9Sstevel@tonic-gate 		 */
38167c478bd9Sstevel@tonic-gate 		if (lp->dn_kind == DT_NODE_AGG) {
38177c478bd9Sstevel@tonic-gate 			if (lp->dn_aggtup != NULL) {
38187c478bd9Sstevel@tonic-gate 				xyerror(D_AGG_MDIM, "improper attempt to "
38197c478bd9Sstevel@tonic-gate 				    "reference @%s as a multi-dimensional "
38207c478bd9Sstevel@tonic-gate 				    "array\n", lp->dn_ident->di_name);
38217c478bd9Sstevel@tonic-gate 			}
38227c478bd9Sstevel@tonic-gate 
38237c478bd9Sstevel@tonic-gate 			lp->dn_aggtup = rp;
38247c478bd9Sstevel@tonic-gate 			lp = dt_node_cook(lp, 0);
38257c478bd9Sstevel@tonic-gate 
38267c478bd9Sstevel@tonic-gate 			dnp->dn_left = dnp->dn_right = NULL;
38277c478bd9Sstevel@tonic-gate 			dt_node_free(dnp);
38287c478bd9Sstevel@tonic-gate 
38297c478bd9Sstevel@tonic-gate 			return (lp);
38307c478bd9Sstevel@tonic-gate 		}
38317c478bd9Sstevel@tonic-gate 
38327c478bd9Sstevel@tonic-gate 		assert(lp->dn_kind == DT_NODE_VAR);
38337c478bd9Sstevel@tonic-gate 		idp = lp->dn_ident;
38347c478bd9Sstevel@tonic-gate 
38357c478bd9Sstevel@tonic-gate 		/*
38367c478bd9Sstevel@tonic-gate 		 * If the left-hand side is a non-global scalar that hasn't yet
38377c478bd9Sstevel@tonic-gate 		 * been referenced or modified, it was just created by self->
38387c478bd9Sstevel@tonic-gate 		 * or this-> and we can convert it from scalar to assoc array.
38397c478bd9Sstevel@tonic-gate 		 */
38407c478bd9Sstevel@tonic-gate 		if (idp->di_kind == DT_IDENT_SCALAR && dt_ident_unref(idp) &&
38417c478bd9Sstevel@tonic-gate 		    (idp->di_flags & (DT_IDFLG_LOCAL | DT_IDFLG_TLS)) != 0) {
38427c478bd9Sstevel@tonic-gate 
38437c478bd9Sstevel@tonic-gate 			if (idp->di_flags & DT_IDFLG_LOCAL) {
38447c478bd9Sstevel@tonic-gate 				xyerror(D_ARR_LOCAL,
38457c478bd9Sstevel@tonic-gate 				    "local variables may not be used as "
38467c478bd9Sstevel@tonic-gate 				    "associative arrays: %s\n", idp->di_name);
38477c478bd9Sstevel@tonic-gate 			}
38487c478bd9Sstevel@tonic-gate 
38497c478bd9Sstevel@tonic-gate 			dt_dprintf("morph variable %s (id %u) from scalar to "
38507c478bd9Sstevel@tonic-gate 			    "array\n", idp->di_name, idp->di_id);
38517c478bd9Sstevel@tonic-gate 
38527c478bd9Sstevel@tonic-gate 			dt_ident_morph(idp, DT_IDENT_ARRAY,
38537c478bd9Sstevel@tonic-gate 			    &dt_idops_assc, NULL);
38547c478bd9Sstevel@tonic-gate 		}
38557c478bd9Sstevel@tonic-gate 
38567c478bd9Sstevel@tonic-gate 		if (idp->di_kind != DT_IDENT_ARRAY) {
38577c478bd9Sstevel@tonic-gate 			xyerror(D_IDENT_BADREF, "%s '%s' may not be referenced "
38587c478bd9Sstevel@tonic-gate 			    "as %s\n", dt_idkind_name(idp->di_kind),
38597c478bd9Sstevel@tonic-gate 			    idp->di_name, dt_idkind_name(DT_IDENT_ARRAY));
38607c478bd9Sstevel@tonic-gate 		}
38617c478bd9Sstevel@tonic-gate 
38627c478bd9Sstevel@tonic-gate 		/*
38637c478bd9Sstevel@tonic-gate 		 * Now that we've confirmed our left-hand side is a DT_NODE_VAR
38647c478bd9Sstevel@tonic-gate 		 * of idkind DT_IDENT_ARRAY, we need to splice the [ node from
38657c478bd9Sstevel@tonic-gate 		 * the parse tree and leave a cooked DT_NODE_VAR in its place
38667c478bd9Sstevel@tonic-gate 		 * where dn_args for the VAR node is the right-hand 'rp' tree,
38677c478bd9Sstevel@tonic-gate 		 * as shown in the parse tree diagram below:
38687c478bd9Sstevel@tonic-gate 		 *
38697c478bd9Sstevel@tonic-gate 		 *	  /			    /
38707c478bd9Sstevel@tonic-gate 		 * [ OP2 "[" ]=dnp		[ VAR ]=dnp
38717c478bd9Sstevel@tonic-gate 		 *	 /	\	  =>	   |
38727c478bd9Sstevel@tonic-gate 		 *	/	 \		   +- dn_args -> [ ??? ]=rp
38737c478bd9Sstevel@tonic-gate 		 * [ VAR ]=lp  [ ??? ]=rp
38747c478bd9Sstevel@tonic-gate 		 *
38757c478bd9Sstevel@tonic-gate 		 * Since the final dt_node_cook(dnp) can fail using longjmp we
38767c478bd9Sstevel@tonic-gate 		 * must perform the transformations as a group first by over-
38777c478bd9Sstevel@tonic-gate 		 * writing 'dnp' to become the VAR node, so that the parse tree
38787c478bd9Sstevel@tonic-gate 		 * is guaranteed to be in a consistent state if the cook fails.
38797c478bd9Sstevel@tonic-gate 		 */
38807c478bd9Sstevel@tonic-gate 		assert(lp->dn_kind == DT_NODE_VAR);
38817c478bd9Sstevel@tonic-gate 		assert(lp->dn_args == NULL);
38827c478bd9Sstevel@tonic-gate 
38837c478bd9Sstevel@tonic-gate 		lnp = dnp->dn_link;
38847c478bd9Sstevel@tonic-gate 		bcopy(lp, dnp, sizeof (dt_node_t));
38857c478bd9Sstevel@tonic-gate 		dnp->dn_link = lnp;
38867c478bd9Sstevel@tonic-gate 
38877c478bd9Sstevel@tonic-gate 		dnp->dn_args = rp;
38887c478bd9Sstevel@tonic-gate 		dnp->dn_list = NULL;
38897c478bd9Sstevel@tonic-gate 
38907c478bd9Sstevel@tonic-gate 		dt_node_free(lp);
38917c478bd9Sstevel@tonic-gate 		return (dt_node_cook(dnp, idflags));
38927c478bd9Sstevel@tonic-gate 	}
38937c478bd9Sstevel@tonic-gate 
38947c478bd9Sstevel@tonic-gate 	case DT_TOK_XLATE: {
38957c478bd9Sstevel@tonic-gate 		dt_xlator_t *dxp;
38967c478bd9Sstevel@tonic-gate 
38977c478bd9Sstevel@tonic-gate 		assert(lp->dn_kind == DT_NODE_TYPE);
38987c478bd9Sstevel@tonic-gate 		rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
38997c478bd9Sstevel@tonic-gate 		dxp = dt_xlator_lookup(dtp, rp, lp, DT_XLATE_FUZZY);
39007c478bd9Sstevel@tonic-gate 
39017c478bd9Sstevel@tonic-gate 		if (dxp == NULL) {
39027c478bd9Sstevel@tonic-gate 			xyerror(D_XLATE_NONE,
39037c478bd9Sstevel@tonic-gate 			    "cannot translate from \"%s\" to \"%s\"\n",
39047c478bd9Sstevel@tonic-gate 			    dt_node_type_name(rp, n1, sizeof (n1)),
39057c478bd9Sstevel@tonic-gate 			    dt_node_type_name(lp, n2, sizeof (n2)));
39067c478bd9Sstevel@tonic-gate 		}
39077c478bd9Sstevel@tonic-gate 
39087c478bd9Sstevel@tonic-gate 		dnp->dn_ident = dt_xlator_ident(dxp, lp->dn_ctfp, lp->dn_type);
3909a386cc11SRobert Mustacchi 		dt_node_type_assign(dnp, DT_DYN_CTFP(dtp), DT_DYN_TYPE(dtp),
3910a386cc11SRobert Mustacchi 		    B_FALSE);
39117c478bd9Sstevel@tonic-gate 		dt_node_attr_assign(dnp,
39127c478bd9Sstevel@tonic-gate 		    dt_attr_min(rp->dn_attr, dnp->dn_ident->di_attr));
39137c478bd9Sstevel@tonic-gate 		break;
39147c478bd9Sstevel@tonic-gate 	}
39157c478bd9Sstevel@tonic-gate 
39167c478bd9Sstevel@tonic-gate 	case DT_TOK_LPAR: {
39177c478bd9Sstevel@tonic-gate 		ctf_id_t ltype, rtype;
39187c478bd9Sstevel@tonic-gate 		uint_t lkind, rkind;
39197c478bd9Sstevel@tonic-gate 
39207c478bd9Sstevel@tonic-gate 		assert(lp->dn_kind == DT_NODE_TYPE);
39217c478bd9Sstevel@tonic-gate 		rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
39227c478bd9Sstevel@tonic-gate 
39237c478bd9Sstevel@tonic-gate 		ltype = ctf_type_resolve(lp->dn_ctfp, lp->dn_type);
39247c478bd9Sstevel@tonic-gate 		lkind = ctf_type_kind(lp->dn_ctfp, ltype);
39257c478bd9Sstevel@tonic-gate 
39267c478bd9Sstevel@tonic-gate 		rtype = ctf_type_resolve(rp->dn_ctfp, rp->dn_type);
39277c478bd9Sstevel@tonic-gate 		rkind = ctf_type_kind(rp->dn_ctfp, rtype);
39287c478bd9Sstevel@tonic-gate 
39297c478bd9Sstevel@tonic-gate 		/*
39307c478bd9Sstevel@tonic-gate 		 * The rules for casting are loosely explained in K&R[A7.5]
39317c478bd9Sstevel@tonic-gate 		 * and K&R[A6].  Basically, we can cast to the same type or
39327c478bd9Sstevel@tonic-gate 		 * same base type, between any kind of scalar values, from
39337c478bd9Sstevel@tonic-gate 		 * arrays to pointers, and we can cast anything to void.
39347c478bd9Sstevel@tonic-gate 		 * To these rules D adds casts from scalars to strings.
39357c478bd9Sstevel@tonic-gate 		 */
39367c478bd9Sstevel@tonic-gate 		if (ctf_type_compat(lp->dn_ctfp, lp->dn_type,
39377c478bd9Sstevel@tonic-gate 		    rp->dn_ctfp, rp->dn_type))
39387c478bd9Sstevel@tonic-gate 			/*EMPTY*/;
39397c478bd9Sstevel@tonic-gate 		else if (dt_node_is_scalar(lp) &&
39407c478bd9Sstevel@tonic-gate 		    (dt_node_is_scalar(rp) || rkind == CTF_K_FUNCTION))
39417c478bd9Sstevel@tonic-gate 			/*EMPTY*/;
39427c478bd9Sstevel@tonic-gate 		else if (dt_node_is_void(lp))
39437c478bd9Sstevel@tonic-gate 			/*EMPTY*/;
39447c478bd9Sstevel@tonic-gate 		else if (lkind == CTF_K_POINTER && dt_node_is_pointer(rp))
39457c478bd9Sstevel@tonic-gate 			/*EMPTY*/;
39467c478bd9Sstevel@tonic-gate 		else if (dt_node_is_string(lp) && (dt_node_is_scalar(rp) ||
39477c478bd9Sstevel@tonic-gate 		    dt_node_is_pointer(rp) || dt_node_is_strcompat(rp)))
39487c478bd9Sstevel@tonic-gate 			/*EMPTY*/;
39497c478bd9Sstevel@tonic-gate 		else {
39507c478bd9Sstevel@tonic-gate 			xyerror(D_CAST_INVAL,
39517c478bd9Sstevel@tonic-gate 			    "invalid cast expression: \"%s\" to \"%s\"\n",
39527c478bd9Sstevel@tonic-gate 			    dt_node_type_name(rp, n1, sizeof (n1)),
39537c478bd9Sstevel@tonic-gate 			    dt_node_type_name(lp, n2, sizeof (n2)));
39547c478bd9Sstevel@tonic-gate 		}
39557c478bd9Sstevel@tonic-gate 
39567c478bd9Sstevel@tonic-gate 		dt_node_type_propagate(lp, dnp); /* see K&R[A7.5] */
39577c478bd9Sstevel@tonic-gate 		dt_node_attr_assign(dnp, dt_attr_min(lp->dn_attr, rp->dn_attr));
3958e5803b76SAdam H. Leventhal 
3959e5803b76SAdam H. Leventhal 		/*
3960e5803b76SAdam H. Leventhal 		 * If it's a pointer then should be able to (attempt to)
3961e5803b76SAdam H. Leventhal 		 * assign to it.
3962e5803b76SAdam H. Leventhal 		 */
3963e5803b76SAdam H. Leventhal 		if (lkind == CTF_K_POINTER)
3964e5803b76SAdam H. Leventhal 			dnp->dn_flags |= DT_NF_WRITABLE;
3965e5803b76SAdam H. Leventhal 
39667c478bd9Sstevel@tonic-gate 		break;
39677c478bd9Sstevel@tonic-gate 	}
39687c478bd9Sstevel@tonic-gate 
39697c478bd9Sstevel@tonic-gate 	case DT_TOK_COMMA:
39707c478bd9Sstevel@tonic-gate 		lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF);
39717c478bd9Sstevel@tonic-gate 		rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
39727c478bd9Sstevel@tonic-gate 
39737c478bd9Sstevel@tonic-gate 		if (dt_node_is_dynamic(lp) || dt_node_is_dynamic(rp)) {
39747c478bd9Sstevel@tonic-gate 			xyerror(D_OP_DYN, "operator %s operands "
39757c478bd9Sstevel@tonic-gate 			    "cannot be of dynamic type\n", opstr(op));
39767c478bd9Sstevel@tonic-gate 		}
39777c478bd9Sstevel@tonic-gate 
39787c478bd9Sstevel@tonic-gate 		if (dt_node_is_actfunc(lp) || dt_node_is_actfunc(rp)) {
39797c478bd9Sstevel@tonic-gate 			xyerror(D_OP_ACT, "operator %s operands "
39807c478bd9Sstevel@tonic-gate 			    "cannot be actions\n", opstr(op));
39817c478bd9Sstevel@tonic-gate 		}
39827c478bd9Sstevel@tonic-gate 
39837c478bd9Sstevel@tonic-gate 		dt_node_type_propagate(rp, dnp); /* see K&R[A7.18] */
39847c478bd9Sstevel@tonic-gate 		dt_node_attr_assign(dnp, dt_attr_min(lp->dn_attr, rp->dn_attr));
39857c478bd9Sstevel@tonic-gate 		break;
39867c478bd9Sstevel@tonic-gate 
39877c478bd9Sstevel@tonic-gate 	default:
39887c478bd9Sstevel@tonic-gate 		xyerror(D_UNKNOWN, "invalid binary op %s\n", opstr(op));
39897c478bd9Sstevel@tonic-gate 	}
39907c478bd9Sstevel@tonic-gate 
39917c478bd9Sstevel@tonic-gate 	/*
39927c478bd9Sstevel@tonic-gate 	 * Complete the conversion of E1[E2] to *((E1)+(E2)) that we started
39937c478bd9Sstevel@tonic-gate 	 * at the top of our switch() above (see K&R[A7.3.1]).  Since E2 is
39947c478bd9Sstevel@tonic-gate 	 * parsed as an argument_expression_list by dt_grammar.y, we can
39957c478bd9Sstevel@tonic-gate 	 * end up with a comma-separated list inside of a non-associative
39967c478bd9Sstevel@tonic-gate 	 * array reference.  We check for this and report an appropriate error.
39977c478bd9Sstevel@tonic-gate 	 */
39987c478bd9Sstevel@tonic-gate 	if (dnp->dn_op == DT_TOK_LBRAC && op == DT_TOK_ADD) {
39997c478bd9Sstevel@tonic-gate 		dt_node_t *pnp;
40007c478bd9Sstevel@tonic-gate 
40017c478bd9Sstevel@tonic-gate 		if (rp->dn_list != NULL) {
40027c478bd9Sstevel@tonic-gate 			xyerror(D_ARR_BADREF,
40037c478bd9Sstevel@tonic-gate 			    "cannot access %s as an associative array\n",
40047c478bd9Sstevel@tonic-gate 			    dt_node_name(lp, n1, sizeof (n1)));
40057c478bd9Sstevel@tonic-gate 		}
40067c478bd9Sstevel@tonic-gate 
40077c478bd9Sstevel@tonic-gate 		dnp->dn_op = DT_TOK_ADD;
40087c478bd9Sstevel@tonic-gate 		pnp = dt_node_op1(DT_TOK_DEREF, dnp);
40097c478bd9Sstevel@tonic-gate 
40107c478bd9Sstevel@tonic-gate 		/*
40117c478bd9Sstevel@tonic-gate 		 * Cook callbacks are not typically permitted to allocate nodes.
40127c478bd9Sstevel@tonic-gate 		 * When we do, we must insert them in the middle of an existing
40137c478bd9Sstevel@tonic-gate 		 * allocation list rather than having them appended to the pcb
40147c478bd9Sstevel@tonic-gate 		 * list because the sub-expression may be part of a definition.
40157c478bd9Sstevel@tonic-gate 		 */
40167c478bd9Sstevel@tonic-gate 		assert(yypcb->pcb_list == pnp);
40177c478bd9Sstevel@tonic-gate 		yypcb->pcb_list = pnp->dn_link;
40187c478bd9Sstevel@tonic-gate 
40197c478bd9Sstevel@tonic-gate 		pnp->dn_link = dnp->dn_link;
40207c478bd9Sstevel@tonic-gate 		dnp->dn_link = pnp;
40217c478bd9Sstevel@tonic-gate 
40227c478bd9Sstevel@tonic-gate 		return (dt_node_cook(pnp, DT_IDFLG_REF));
40237c478bd9Sstevel@tonic-gate 	}
40247c478bd9Sstevel@tonic-gate 
40257c478bd9Sstevel@tonic-gate 	return (dnp);
40267c478bd9Sstevel@tonic-gate }
40277c478bd9Sstevel@tonic-gate 
40287c478bd9Sstevel@tonic-gate /*ARGSUSED*/
40297c478bd9Sstevel@tonic-gate static dt_node_t *
dt_cook_op3(dt_node_t * dnp,uint_t idflags)40307c478bd9Sstevel@tonic-gate dt_cook_op3(dt_node_t *dnp, uint_t idflags)
40317c478bd9Sstevel@tonic-gate {
40327c478bd9Sstevel@tonic-gate 	dt_node_t *lp, *rp;
40337c478bd9Sstevel@tonic-gate 	ctf_file_t *ctfp;
40347c478bd9Sstevel@tonic-gate 	ctf_id_t type;
40357c478bd9Sstevel@tonic-gate 
40367c478bd9Sstevel@tonic-gate 	dnp->dn_expr = dt_node_cook(dnp->dn_expr, DT_IDFLG_REF);
40377c478bd9Sstevel@tonic-gate 	lp = dnp->dn_left = dt_node_cook(dnp->dn_left, DT_IDFLG_REF);
40387c478bd9Sstevel@tonic-gate 	rp = dnp->dn_right = dt_node_cook(dnp->dn_right, DT_IDFLG_REF);
40397c478bd9Sstevel@tonic-gate 
40407c478bd9Sstevel@tonic-gate 	if (!dt_node_is_scalar(dnp->dn_expr)) {
40417c478bd9Sstevel@tonic-gate 		xyerror(D_OP_SCALAR,
40427c478bd9Sstevel@tonic-gate 		    "operator ?: expression must be of scalar type\n");
40437c478bd9Sstevel@tonic-gate 	}
40447c478bd9Sstevel@tonic-gate 
40457c478bd9Sstevel@tonic-gate 	if (dt_node_is_dynamic(lp) || dt_node_is_dynamic(rp)) {
40467c478bd9Sstevel@tonic-gate 		xyerror(D_OP_DYN,
40477c478bd9Sstevel@tonic-gate 		    "operator ?: operands cannot be of dynamic type\n");
40487c478bd9Sstevel@tonic-gate 	}
40497c478bd9Sstevel@tonic-gate 
40507c478bd9Sstevel@tonic-gate 	/*
40517c478bd9Sstevel@tonic-gate 	 * The rules for type checking for the ternary operator are complex and
40527c478bd9Sstevel@tonic-gate 	 * are described in the ANSI-C spec (see K&R[A7.16]).  We implement
40537c478bd9Sstevel@tonic-gate 	 * the various tests in order from least to most expensive.
40547c478bd9Sstevel@tonic-gate 	 */
40557c478bd9Sstevel@tonic-gate 	if (ctf_type_compat(lp->dn_ctfp, lp->dn_type,
40567c478bd9Sstevel@tonic-gate 	    rp->dn_ctfp, rp->dn_type)) {
40577c478bd9Sstevel@tonic-gate 		ctfp = lp->dn_ctfp;
40587c478bd9Sstevel@tonic-gate 		type = lp->dn_type;
40597c478bd9Sstevel@tonic-gate 	} else if (dt_node_is_integer(lp) && dt_node_is_integer(rp)) {
40607c478bd9Sstevel@tonic-gate 		dt_type_promote(lp, rp, &ctfp, &type);
40617c478bd9Sstevel@tonic-gate 	} else if (dt_node_is_strcompat(lp) && dt_node_is_strcompat(rp) &&
40627c478bd9Sstevel@tonic-gate 	    (dt_node_is_string(lp) || dt_node_is_string(rp))) {
40637c478bd9Sstevel@tonic-gate 		ctfp = DT_STR_CTFP(yypcb->pcb_hdl);
40647c478bd9Sstevel@tonic-gate 		type = DT_STR_TYPE(yypcb->pcb_hdl);
40657c478bd9Sstevel@tonic-gate 	} else if (dt_node_is_ptrcompat(lp, rp, &ctfp, &type) == 0) {
40667c478bd9Sstevel@tonic-gate 		xyerror(D_OP_INCOMPAT,
40677c478bd9Sstevel@tonic-gate 		    "operator ?: operands must have compatible types\n");
40687c478bd9Sstevel@tonic-gate 	}
40697c478bd9Sstevel@tonic-gate 
40707c478bd9Sstevel@tonic-gate 	if (dt_node_is_actfunc(lp) || dt_node_is_actfunc(rp)) {
40717c478bd9Sstevel@tonic-gate 		xyerror(D_OP_ACT, "action cannot be "
40727c478bd9Sstevel@tonic-gate 		    "used in a conditional context\n");
40737c478bd9Sstevel@tonic-gate 	}
40747c478bd9Sstevel@tonic-gate 
4075a386cc11SRobert Mustacchi 	dt_node_type_assign(dnp, ctfp, type, B_FALSE);
40767c478bd9Sstevel@tonic-gate 	dt_node_attr_assign(dnp, dt_attr_min(dnp->dn_expr->dn_attr,
40777c478bd9Sstevel@tonic-gate 	    dt_attr_min(lp->dn_attr, rp->dn_attr)));
40787c478bd9Sstevel@tonic-gate 
40797c478bd9Sstevel@tonic-gate 	return (dnp);
40807c478bd9Sstevel@tonic-gate }
40817c478bd9Sstevel@tonic-gate 
40827c478bd9Sstevel@tonic-gate static dt_node_t *
dt_cook_statement(dt_node_t * dnp,uint_t idflags)40837c478bd9Sstevel@tonic-gate dt_cook_statement(dt_node_t *dnp, uint_t idflags)
40847c478bd9Sstevel@tonic-gate {
40857c478bd9Sstevel@tonic-gate 	dnp->dn_expr = dt_node_cook(dnp->dn_expr, idflags);
40867c478bd9Sstevel@tonic-gate 	dt_node_attr_assign(dnp, dnp->dn_expr->dn_attr);
40877c478bd9Sstevel@tonic-gate 
40887c478bd9Sstevel@tonic-gate 	return (dnp);
40897c478bd9Sstevel@tonic-gate }
40907c478bd9Sstevel@tonic-gate 
40917c478bd9Sstevel@tonic-gate /*
40927c478bd9Sstevel@tonic-gate  * If dn_aggfun is set, this node is a collapsed aggregation assignment (see
40937c478bd9Sstevel@tonic-gate  * the special case code for DT_TOK_ASGN in dt_cook_op2() above), in which
40947c478bd9Sstevel@tonic-gate  * case we cook both the tuple and the function call.  If dn_aggfun is NULL,
40957c478bd9Sstevel@tonic-gate  * this node is just a reference to the aggregation's type and attributes.
40967c478bd9Sstevel@tonic-gate  */
40977c478bd9Sstevel@tonic-gate /*ARGSUSED*/
40987c478bd9Sstevel@tonic-gate static dt_node_t *
dt_cook_aggregation(dt_node_t * dnp,uint_t idflags)40997c478bd9Sstevel@tonic-gate dt_cook_aggregation(dt_node_t *dnp, uint_t idflags)
41007c478bd9Sstevel@tonic-gate {
41017c478bd9Sstevel@tonic-gate 	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
41027c478bd9Sstevel@tonic-gate 
41037c478bd9Sstevel@tonic-gate 	if (dnp->dn_aggfun != NULL) {
41047c478bd9Sstevel@tonic-gate 		dnp->dn_aggfun = dt_node_cook(dnp->dn_aggfun, DT_IDFLG_REF);
41057c478bd9Sstevel@tonic-gate 		dt_node_attr_assign(dnp, dt_ident_cook(dnp,
41067c478bd9Sstevel@tonic-gate 		    dnp->dn_ident, &dnp->dn_aggtup));
41077c478bd9Sstevel@tonic-gate 	} else {
4108a386cc11SRobert Mustacchi 		dt_node_type_assign(dnp, DT_DYN_CTFP(dtp), DT_DYN_TYPE(dtp),
4109a386cc11SRobert Mustacchi 		    B_FALSE);
41107c478bd9Sstevel@tonic-gate 		dt_node_attr_assign(dnp, dnp->dn_ident->di_attr);
41117c478bd9Sstevel@tonic-gate 	}
41127c478bd9Sstevel@tonic-gate 
41137c478bd9Sstevel@tonic-gate 	return (dnp);
41147c478bd9Sstevel@tonic-gate }
41157c478bd9Sstevel@tonic-gate 
41167c478bd9Sstevel@tonic-gate /*
41177c478bd9Sstevel@tonic-gate  * Since D permits new variable identifiers to be instantiated in any program
41187c478bd9Sstevel@tonic-gate  * expression, we may need to cook a clause's predicate either before or after
41197c478bd9Sstevel@tonic-gate  * the action list depending on the program code in question.  Consider:
41207c478bd9Sstevel@tonic-gate  *
41217c478bd9Sstevel@tonic-gate  * probe-description-list	probe-description-list
41227c478bd9Sstevel@tonic-gate  * /x++/			/x == 0/
41237c478bd9Sstevel@tonic-gate  * {				{
41247c478bd9Sstevel@tonic-gate  *     trace(x);		    trace(x++);
41257c478bd9Sstevel@tonic-gate  * }				}
41267c478bd9Sstevel@tonic-gate  *
41277c478bd9Sstevel@tonic-gate  * In the left-hand example, the predicate uses operator ++ to instantiate 'x'
41287c478bd9Sstevel@tonic-gate  * as a variable of type int64_t.  The predicate must be cooked first because
41297c478bd9Sstevel@tonic-gate  * otherwise the statement trace(x) refers to an unknown identifier.  In the
41307c478bd9Sstevel@tonic-gate  * right-hand example, the action list uses ++ to instantiate 'x'; the action
41317c478bd9Sstevel@tonic-gate  * list must be cooked first because otherwise the predicate x == 0 refers to
41327c478bd9Sstevel@tonic-gate  * an unknown identifier.  In order to simplify programming, we support both.
41337c478bd9Sstevel@tonic-gate  *
41347c478bd9Sstevel@tonic-gate  * When cooking a clause, we cook the action statements before the predicate by
41357c478bd9Sstevel@tonic-gate  * default, since it seems more common to create or modify identifiers in the
41367c478bd9Sstevel@tonic-gate  * action list.  If cooking fails due to an unknown identifier, we attempt to
41377c478bd9Sstevel@tonic-gate  * cook the predicate (i.e. do it first) and then go back and cook the actions.
41387c478bd9Sstevel@tonic-gate  * If this, too, fails (or if we get an error other than D_IDENT_UNDEF) we give
41397c478bd9Sstevel@tonic-gate  * up and report failure back to the user.  There are five possible paths:
41407c478bd9Sstevel@tonic-gate  *
41417c478bd9Sstevel@tonic-gate  * cook actions = OK, cook predicate = OK -> OK
41427c478bd9Sstevel@tonic-gate  * cook actions = OK, cook predicate = ERR -> ERR
41437c478bd9Sstevel@tonic-gate  * cook actions = ERR, cook predicate = ERR -> ERR
41447c478bd9Sstevel@tonic-gate  * cook actions = ERR, cook predicate = OK, cook actions = OK -> OK
41457c478bd9Sstevel@tonic-gate  * cook actions = ERR, cook predicate = OK, cook actions = ERR -> ERR
41467c478bd9Sstevel@tonic-gate  *
41477c478bd9Sstevel@tonic-gate  * The programmer can still defeat our scheme by creating circular definition
41487c478bd9Sstevel@tonic-gate  * dependencies between predicates and actions, as in this example clause:
41497c478bd9Sstevel@tonic-gate  *
41507c478bd9Sstevel@tonic-gate  * probe-description-list
41517c478bd9Sstevel@tonic-gate  * /x++ && y == 0/
41527c478bd9Sstevel@tonic-gate  * {
41537c478bd9Sstevel@tonic-gate  *	trace(x + y++);
41547c478bd9Sstevel@tonic-gate  * }
41557c478bd9Sstevel@tonic-gate  *
41567c478bd9Sstevel@tonic-gate  * but it doesn't seem worth the complexity to handle such rare cases.  The
41577c478bd9Sstevel@tonic-gate  * user can simply use the D variable declaration syntax to work around them.
41587c478bd9Sstevel@tonic-gate  */
41597c478bd9Sstevel@tonic-gate static dt_node_t *
dt_cook_clause(dt_node_t * dnp,uint_t idflags)41607c478bd9Sstevel@tonic-gate dt_cook_clause(dt_node_t *dnp, uint_t idflags)
41617c478bd9Sstevel@tonic-gate {
41627c478bd9Sstevel@tonic-gate 	volatile int err, tries;
41637c478bd9Sstevel@tonic-gate 	jmp_buf ojb;
41647c478bd9Sstevel@tonic-gate 
41657c478bd9Sstevel@tonic-gate 	/*
41667c478bd9Sstevel@tonic-gate 	 * Before assigning dn_ctxattr, temporarily assign the probe attribute
41677c478bd9Sstevel@tonic-gate 	 * to 'dnp' itself to force an attribute check and minimum violation.
41687c478bd9Sstevel@tonic-gate 	 */
41697c478bd9Sstevel@tonic-gate 	dt_node_attr_assign(dnp, yypcb->pcb_pinfo.dtp_attr);
41707c478bd9Sstevel@tonic-gate 	dnp->dn_ctxattr = yypcb->pcb_pinfo.dtp_attr;
41717c478bd9Sstevel@tonic-gate 
41727c478bd9Sstevel@tonic-gate 	bcopy(yypcb->pcb_jmpbuf, ojb, sizeof (jmp_buf));
41737c478bd9Sstevel@tonic-gate 	tries = 0;
41747c478bd9Sstevel@tonic-gate 
41757c478bd9Sstevel@tonic-gate 	if (dnp->dn_pred != NULL && (err = setjmp(yypcb->pcb_jmpbuf)) != 0) {
41767c478bd9Sstevel@tonic-gate 		bcopy(ojb, yypcb->pcb_jmpbuf, sizeof (jmp_buf));
41777c478bd9Sstevel@tonic-gate 		if (tries++ != 0 || err != EDT_COMPILER || (
41787c478bd9Sstevel@tonic-gate 		    yypcb->pcb_hdl->dt_errtag != dt_errtag(D_IDENT_UNDEF) &&
41797c478bd9Sstevel@tonic-gate 		    yypcb->pcb_hdl->dt_errtag != dt_errtag(D_VAR_UNDEF)))
41807c478bd9Sstevel@tonic-gate 			longjmp(yypcb->pcb_jmpbuf, err);
41817c478bd9Sstevel@tonic-gate 	}
41827c478bd9Sstevel@tonic-gate 
41837c478bd9Sstevel@tonic-gate 	if (tries == 0) {
41847c478bd9Sstevel@tonic-gate 		yylabel("action list");
41857c478bd9Sstevel@tonic-gate 
41867c478bd9Sstevel@tonic-gate 		dt_node_attr_assign(dnp,
41877c478bd9Sstevel@tonic-gate 		    dt_node_list_cook(&dnp->dn_acts, idflags));
41887c478bd9Sstevel@tonic-gate 
41897c478bd9Sstevel@tonic-gate 		bcopy(ojb, yypcb->pcb_jmpbuf, sizeof (jmp_buf));
41907c478bd9Sstevel@tonic-gate 		yylabel(NULL);
41917c478bd9Sstevel@tonic-gate 	}
41927c478bd9Sstevel@tonic-gate 
41937c478bd9Sstevel@tonic-gate 	if (dnp->dn_pred != NULL) {
41947c478bd9Sstevel@tonic-gate 		yylabel("predicate");
41957c478bd9Sstevel@tonic-gate 
41967c478bd9Sstevel@tonic-gate 		dnp->dn_pred = dt_node_cook(dnp->dn_pred, idflags);
41977c478bd9Sstevel@tonic-gate 		dt_node_attr_assign(dnp,
41987c478bd9Sstevel@tonic-gate 		    dt_attr_min(dnp->dn_attr, dnp->dn_pred->dn_attr));
41997c478bd9Sstevel@tonic-gate 
42007c478bd9Sstevel@tonic-gate 		if (!dt_node_is_scalar(dnp->dn_pred)) {
42017c478bd9Sstevel@tonic-gate 			xyerror(D_PRED_SCALAR,
42027c478bd9Sstevel@tonic-gate 			    "predicate result must be of scalar type\n");
42037c478bd9Sstevel@tonic-gate 		}
42047c478bd9Sstevel@tonic-gate 
42057c478bd9Sstevel@tonic-gate 		yylabel(NULL);
42067c478bd9Sstevel@tonic-gate 	}
42077c478bd9Sstevel@tonic-gate 
42087c478bd9Sstevel@tonic-gate 	if (tries != 0) {
42097c478bd9Sstevel@tonic-gate 		yylabel("action list");
42107c478bd9Sstevel@tonic-gate 
42117c478bd9Sstevel@tonic-gate 		dt_node_attr_assign(dnp,
42127c478bd9Sstevel@tonic-gate 		    dt_node_list_cook(&dnp->dn_acts, idflags));
42137c478bd9Sstevel@tonic-gate 
42147c478bd9Sstevel@tonic-gate 		yylabel(NULL);
42157c478bd9Sstevel@tonic-gate 	}
42167c478bd9Sstevel@tonic-gate 
42177c478bd9Sstevel@tonic-gate 	return (dnp);
42187c478bd9Sstevel@tonic-gate }
42197c478bd9Sstevel@tonic-gate 
42207c478bd9Sstevel@tonic-gate /*ARGSUSED*/
42217c478bd9Sstevel@tonic-gate static dt_node_t *
dt_cook_inline(dt_node_t * dnp,uint_t idflags)42227c478bd9Sstevel@tonic-gate dt_cook_inline(dt_node_t *dnp, uint_t idflags)
42237c478bd9Sstevel@tonic-gate {
42247c478bd9Sstevel@tonic-gate 	dt_idnode_t *inp = dnp->dn_ident->di_iarg;
42257c478bd9Sstevel@tonic-gate 	dt_ident_t *rdp;
42267c478bd9Sstevel@tonic-gate 
42277c478bd9Sstevel@tonic-gate 	char n1[DT_TYPE_NAMELEN];
42287c478bd9Sstevel@tonic-gate 	char n2[DT_TYPE_NAMELEN];
42297c478bd9Sstevel@tonic-gate 
42307c478bd9Sstevel@tonic-gate 	assert(dnp->dn_ident->di_flags & DT_IDFLG_INLINE);
42317c478bd9Sstevel@tonic-gate 	assert(inp->din_root->dn_flags & DT_NF_COOKED);
42327c478bd9Sstevel@tonic-gate 
42337c478bd9Sstevel@tonic-gate 	/*
42347c478bd9Sstevel@tonic-gate 	 * If we are inlining a translation, verify that the inline declaration
42357c478bd9Sstevel@tonic-gate 	 * type exactly matches the type that is returned by the translation.
42367c478bd9Sstevel@tonic-gate 	 * Otherwise just use dt_node_is_argcompat() to check the types.
42377c478bd9Sstevel@tonic-gate 	 */
42387c478bd9Sstevel@tonic-gate 	if ((rdp = dt_node_resolve(inp->din_root, DT_IDENT_XLSOU)) != NULL ||
42397c478bd9Sstevel@tonic-gate 	    (rdp = dt_node_resolve(inp->din_root, DT_IDENT_XLPTR)) != NULL) {
42407c478bd9Sstevel@tonic-gate 
42417c478bd9Sstevel@tonic-gate 		ctf_file_t *lctfp = dnp->dn_ctfp;
42427c478bd9Sstevel@tonic-gate 		ctf_id_t ltype = ctf_type_resolve(lctfp, dnp->dn_type);
42437c478bd9Sstevel@tonic-gate 
42447c478bd9Sstevel@tonic-gate 		dt_xlator_t *dxp = rdp->di_data;
42457c478bd9Sstevel@tonic-gate 		ctf_file_t *rctfp = dxp->dx_dst_ctfp;
42467c478bd9Sstevel@tonic-gate 		ctf_id_t rtype = dxp->dx_dst_base;
42477c478bd9Sstevel@tonic-gate 
42487c478bd9Sstevel@tonic-gate 		if (ctf_type_kind(lctfp, ltype) == CTF_K_POINTER) {
42497c478bd9Sstevel@tonic-gate 			ltype = ctf_type_reference(lctfp, ltype);
42507c478bd9Sstevel@tonic-gate 			ltype = ctf_type_resolve(lctfp, ltype);
42517c478bd9Sstevel@tonic-gate 		}
42527c478bd9Sstevel@tonic-gate 
42537c478bd9Sstevel@tonic-gate 		if (ctf_type_compat(lctfp, ltype, rctfp, rtype) == 0) {
42547c478bd9Sstevel@tonic-gate 			dnerror(dnp, D_OP_INCOMPAT,
42557c478bd9Sstevel@tonic-gate 			    "inline %s definition uses incompatible types: "
42567c478bd9Sstevel@tonic-gate 			    "\"%s\" = \"%s\"\n", dnp->dn_ident->di_name,
42577c478bd9Sstevel@tonic-gate 			    dt_type_name(lctfp, ltype, n1, sizeof (n1)),
42587c478bd9Sstevel@tonic-gate 			    dt_type_name(rctfp, rtype, n2, sizeof (n2)));
42597c478bd9Sstevel@tonic-gate 		}
42607c478bd9Sstevel@tonic-gate 
42617c478bd9Sstevel@tonic-gate 	} else if (dt_node_is_argcompat(dnp, inp->din_root) == 0) {
42627c478bd9Sstevel@tonic-gate 		dnerror(dnp, D_OP_INCOMPAT,
42637c478bd9Sstevel@tonic-gate 		    "inline %s definition uses incompatible types: "
42647c478bd9Sstevel@tonic-gate 		    "\"%s\" = \"%s\"\n", dnp->dn_ident->di_name,
42657c478bd9Sstevel@tonic-gate 		    dt_node_type_name(dnp, n1, sizeof (n1)),
42667c478bd9Sstevel@tonic-gate 		    dt_node_type_name(inp->din_root, n2, sizeof (n2)));
42677c478bd9Sstevel@tonic-gate 	}
42687c478bd9Sstevel@tonic-gate 
42697c478bd9Sstevel@tonic-gate 	return (dnp);
42707c478bd9Sstevel@tonic-gate }
42717c478bd9Sstevel@tonic-gate 
42727c478bd9Sstevel@tonic-gate static dt_node_t *
dt_cook_member(dt_node_t * dnp,uint_t idflags)42737c478bd9Sstevel@tonic-gate dt_cook_member(dt_node_t *dnp, uint_t idflags)
42747c478bd9Sstevel@tonic-gate {
42757c478bd9Sstevel@tonic-gate 	dnp->dn_membexpr = dt_node_cook(dnp->dn_membexpr, idflags);
42767c478bd9Sstevel@tonic-gate 	dt_node_attr_assign(dnp, dnp->dn_membexpr->dn_attr);
42777c478bd9Sstevel@tonic-gate 	return (dnp);
42787c478bd9Sstevel@tonic-gate }
42797c478bd9Sstevel@tonic-gate 
42807c478bd9Sstevel@tonic-gate /*ARGSUSED*/
42817c478bd9Sstevel@tonic-gate static dt_node_t *
dt_cook_xlator(dt_node_t * dnp,uint_t idflags)42827c478bd9Sstevel@tonic-gate dt_cook_xlator(dt_node_t *dnp, uint_t idflags)
42837c478bd9Sstevel@tonic-gate {
42847c478bd9Sstevel@tonic-gate 	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
42857c478bd9Sstevel@tonic-gate 	dt_xlator_t *dxp = dnp->dn_xlator;
42867c478bd9Sstevel@tonic-gate 	dt_node_t *mnp;
42877c478bd9Sstevel@tonic-gate 
42887c478bd9Sstevel@tonic-gate 	char n1[DT_TYPE_NAMELEN];
42897c478bd9Sstevel@tonic-gate 	char n2[DT_TYPE_NAMELEN];
42907c478bd9Sstevel@tonic-gate 
42917c478bd9Sstevel@tonic-gate 	dtrace_attribute_t attr = _dtrace_maxattr;
42927c478bd9Sstevel@tonic-gate 	ctf_membinfo_t ctm;
42937c478bd9Sstevel@tonic-gate 
42947c478bd9Sstevel@tonic-gate 	/*
42957c478bd9Sstevel@tonic-gate 	 * Before cooking each translator member, we push a reference to the
42967c478bd9Sstevel@tonic-gate 	 * hash containing translator-local identifiers on to pcb_globals to
42977c478bd9Sstevel@tonic-gate 	 * temporarily interpose these identifiers in front of other globals.
42987c478bd9Sstevel@tonic-gate 	 */
42997c478bd9Sstevel@tonic-gate 	dt_idstack_push(&yypcb->pcb_globals, dxp->dx_locals);
43007c478bd9Sstevel@tonic-gate 
43017c478bd9Sstevel@tonic-gate 	for (mnp = dnp->dn_members; mnp != NULL; mnp = mnp->dn_list) {
43027c478bd9Sstevel@tonic-gate 		if (ctf_member_info(dxp->dx_dst_ctfp, dxp->dx_dst_type,
43037c478bd9Sstevel@tonic-gate 		    mnp->dn_membname, &ctm) == CTF_ERR) {
43047c478bd9Sstevel@tonic-gate 			xyerror(D_XLATE_MEMB,
43057c478bd9Sstevel@tonic-gate 			    "translator member %s is not a member of %s\n",
43067c478bd9Sstevel@tonic-gate 			    mnp->dn_membname, ctf_type_name(dxp->dx_dst_ctfp,
43077c478bd9Sstevel@tonic-gate 			    dxp->dx_dst_type, n1, sizeof (n1)));
43087c478bd9Sstevel@tonic-gate 		}
43097c478bd9Sstevel@tonic-gate 
43107c478bd9Sstevel@tonic-gate 		(void) dt_node_cook(mnp, DT_IDFLG_REF);
4311a386cc11SRobert Mustacchi 		dt_node_type_assign(mnp, dxp->dx_dst_ctfp, ctm.ctm_type,
4312a386cc11SRobert Mustacchi 		    B_FALSE);
43137c478bd9Sstevel@tonic-gate 		attr = dt_attr_min(attr, mnp->dn_attr);
43147c478bd9Sstevel@tonic-gate 
43157c478bd9Sstevel@tonic-gate 		if (dt_node_is_argcompat(mnp, mnp->dn_membexpr) == 0) {
43167c478bd9Sstevel@tonic-gate 			xyerror(D_XLATE_INCOMPAT,
43177c478bd9Sstevel@tonic-gate 			    "translator member %s definition uses "
43187c478bd9Sstevel@tonic-gate 			    "incompatible types: \"%s\" = \"%s\"\n",
43197c478bd9Sstevel@tonic-gate 			    mnp->dn_membname,
43207c478bd9Sstevel@tonic-gate 			    dt_node_type_name(mnp, n1, sizeof (n1)),
43217c478bd9Sstevel@tonic-gate 			    dt_node_type_name(mnp->dn_membexpr,
43227c478bd9Sstevel@tonic-gate 			    n2, sizeof (n2)));
43237c478bd9Sstevel@tonic-gate 		}
43247c478bd9Sstevel@tonic-gate 	}
43257c478bd9Sstevel@tonic-gate 
43267c478bd9Sstevel@tonic-gate 	dt_idstack_pop(&yypcb->pcb_globals, dxp->dx_locals);
43277c478bd9Sstevel@tonic-gate 
43287c478bd9Sstevel@tonic-gate 	dxp->dx_souid.di_attr = attr;
43297c478bd9Sstevel@tonic-gate 	dxp->dx_ptrid.di_attr = attr;
43307c478bd9Sstevel@tonic-gate 
4331a386cc11SRobert Mustacchi 	dt_node_type_assign(dnp, DT_DYN_CTFP(dtp), DT_DYN_TYPE(dtp), B_FALSE);
43327c478bd9Sstevel@tonic-gate 	dt_node_attr_assign(dnp, _dtrace_defattr);
43337c478bd9Sstevel@tonic-gate 
43347c478bd9Sstevel@tonic-gate 	return (dnp);
43357c478bd9Sstevel@tonic-gate }
43367c478bd9Sstevel@tonic-gate 
43377c478bd9Sstevel@tonic-gate static void
dt_node_provider_cmp_argv(dt_provider_t * pvp,dt_node_t * pnp,const char * kind,uint_t old_argc,dt_node_t * old_argv,uint_t new_argc,dt_node_t * new_argv)43387c478bd9Sstevel@tonic-gate dt_node_provider_cmp_argv(dt_provider_t *pvp, dt_node_t *pnp, const char *kind,
43397c478bd9Sstevel@tonic-gate     uint_t old_argc, dt_node_t *old_argv, uint_t new_argc, dt_node_t *new_argv)
43407c478bd9Sstevel@tonic-gate {
43417c478bd9Sstevel@tonic-gate 	dt_probe_t *prp = pnp->dn_ident->di_data;
43427c478bd9Sstevel@tonic-gate 	uint_t i;
43437c478bd9Sstevel@tonic-gate 
43447c478bd9Sstevel@tonic-gate 	char n1[DT_TYPE_NAMELEN];
43457c478bd9Sstevel@tonic-gate 	char n2[DT_TYPE_NAMELEN];
43467c478bd9Sstevel@tonic-gate 
43477c478bd9Sstevel@tonic-gate 	if (old_argc != new_argc) {
43487c478bd9Sstevel@tonic-gate 		dnerror(pnp, D_PROV_INCOMPAT,
43497c478bd9Sstevel@tonic-gate 		    "probe %s:%s %s prototype mismatch:\n"
43507c478bd9Sstevel@tonic-gate 		    "\t current: %u arg%s\n\tprevious: %u arg%s\n",
43517c478bd9Sstevel@tonic-gate 		    pvp->pv_desc.dtvd_name, prp->pr_ident->di_name, kind,
43527c478bd9Sstevel@tonic-gate 		    new_argc, new_argc != 1 ? "s" : "",
43537c478bd9Sstevel@tonic-gate 		    old_argc, old_argc != 1 ? "s" : "");
43547c478bd9Sstevel@tonic-gate 	}
43557c478bd9Sstevel@tonic-gate 
43567c478bd9Sstevel@tonic-gate 	for (i = 0; i < old_argc; i++,
43577c478bd9Sstevel@tonic-gate 	    old_argv = old_argv->dn_list, new_argv = new_argv->dn_list) {
43587c478bd9Sstevel@tonic-gate 		if (ctf_type_cmp(old_argv->dn_ctfp, old_argv->dn_type,
43597c478bd9Sstevel@tonic-gate 		    new_argv->dn_ctfp, new_argv->dn_type) == 0)
43607c478bd9Sstevel@tonic-gate 			continue;
43617c478bd9Sstevel@tonic-gate 
43627c478bd9Sstevel@tonic-gate 		dnerror(pnp, D_PROV_INCOMPAT,
43637c478bd9Sstevel@tonic-gate 		    "probe %s:%s %s prototype argument #%u mismatch:\n"
43647c478bd9Sstevel@tonic-gate 		    "\t current: %s\n\tprevious: %s\n",
43657c478bd9Sstevel@tonic-gate 		    pvp->pv_desc.dtvd_name, prp->pr_ident->di_name, kind, i + 1,
43667c478bd9Sstevel@tonic-gate 		    dt_node_type_name(new_argv, n1, sizeof (n1)),
43677c478bd9Sstevel@tonic-gate 		    dt_node_type_name(old_argv, n2, sizeof (n2)));
43687c478bd9Sstevel@tonic-gate 	}
43697c478bd9Sstevel@tonic-gate }
43707c478bd9Sstevel@tonic-gate 
43717c478bd9Sstevel@tonic-gate /*
43727c478bd9Sstevel@tonic-gate  * Compare a new probe declaration with an existing probe definition (either
43731a7c1b72Smws  * from a previous declaration or cached from the kernel).  If the existing
43741a7c1b72Smws  * definition and declaration both have an input and output parameter list,
43751a7c1b72Smws  * compare both lists.  Otherwise compare only the output parameter lists.
43767c478bd9Sstevel@tonic-gate  */
43777c478bd9Sstevel@tonic-gate static void
dt_node_provider_cmp(dt_provider_t * pvp,dt_node_t * pnp,dt_probe_t * old,dt_probe_t * new)43787c478bd9Sstevel@tonic-gate dt_node_provider_cmp(dt_provider_t *pvp, dt_node_t *pnp,
43797c478bd9Sstevel@tonic-gate     dt_probe_t *old, dt_probe_t *new)
43807c478bd9Sstevel@tonic-gate {
43817c478bd9Sstevel@tonic-gate 	dt_node_provider_cmp_argv(pvp, pnp, "output",
43827c478bd9Sstevel@tonic-gate 	    old->pr_xargc, old->pr_xargs, new->pr_xargc, new->pr_xargs);
43837c478bd9Sstevel@tonic-gate 
43841a7c1b72Smws 	if (old->pr_nargs != old->pr_xargs && new->pr_nargs != new->pr_xargs) {
43857c478bd9Sstevel@tonic-gate 		dt_node_provider_cmp_argv(pvp, pnp, "input",
43867c478bd9Sstevel@tonic-gate 		    old->pr_nargc, old->pr_nargs, new->pr_nargc, new->pr_nargs);
43877c478bd9Sstevel@tonic-gate 	}
43881a7c1b72Smws 
43891a7c1b72Smws 	if (old->pr_nargs == old->pr_xargs && new->pr_nargs != new->pr_xargs) {
43901a7c1b72Smws 		if (pvp->pv_flags & DT_PROVIDER_IMPL) {
43911a7c1b72Smws 			dnerror(pnp, D_PROV_INCOMPAT,
43921a7c1b72Smws 			    "provider interface mismatch: %s\n"
43931a7c1b72Smws 			    "\t current: probe %s:%s has an output prototype\n"
43941a7c1b72Smws 			    "\tprevious: probe %s:%s has no output prototype\n",
43951a7c1b72Smws 			    pvp->pv_desc.dtvd_name, pvp->pv_desc.dtvd_name,
43961a7c1b72Smws 			    new->pr_ident->di_name, pvp->pv_desc.dtvd_name,
43971a7c1b72Smws 			    old->pr_ident->di_name);
43981a7c1b72Smws 		}
43991a7c1b72Smws 
44001a7c1b72Smws 		if (old->pr_ident->di_gen == yypcb->pcb_hdl->dt_gen)
44011a7c1b72Smws 			old->pr_ident->di_flags |= DT_IDFLG_ORPHAN;
44021a7c1b72Smws 
44031a7c1b72Smws 		dt_idhash_delete(pvp->pv_probes, old->pr_ident);
44041a7c1b72Smws 		dt_probe_declare(pvp, new);
44051a7c1b72Smws 	}
44061a7c1b72Smws }
44071a7c1b72Smws 
44081a7c1b72Smws static void
dt_cook_probe(dt_node_t * dnp,dt_provider_t * pvp)44091a7c1b72Smws dt_cook_probe(dt_node_t *dnp, dt_provider_t *pvp)
44101a7c1b72Smws {
44111a7c1b72Smws 	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
44121a7c1b72Smws 	dt_probe_t *prp = dnp->dn_ident->di_data;
44131a7c1b72Smws 
44141a7c1b72Smws 	dt_xlator_t *dxp;
44151a7c1b72Smws 	uint_t i;
44161a7c1b72Smws 
44171a7c1b72Smws 	char n1[DT_TYPE_NAMELEN];
44181a7c1b72Smws 	char n2[DT_TYPE_NAMELEN];
44191a7c1b72Smws 
44201a7c1b72Smws 	if (prp->pr_nargs == prp->pr_xargs)
44211a7c1b72Smws 		return;
44221a7c1b72Smws 
44231a7c1b72Smws 	for (i = 0; i < prp->pr_xargc; i++) {
44241a7c1b72Smws 		dt_node_t *xnp = prp->pr_xargv[i];
44251a7c1b72Smws 		dt_node_t *nnp = prp->pr_nargv[prp->pr_mapping[i]];
44261a7c1b72Smws 
44271a7c1b72Smws 		if ((dxp = dt_xlator_lookup(dtp,
44281a7c1b72Smws 		    nnp, xnp, DT_XLATE_FUZZY)) != NULL) {
44291a7c1b72Smws 			if (dt_provider_xref(dtp, pvp, dxp->dx_id) != 0)
44301a7c1b72Smws 				longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
44311a7c1b72Smws 			continue;
44321a7c1b72Smws 		}
44331a7c1b72Smws 
44341a7c1b72Smws 		if (dt_node_is_argcompat(nnp, xnp))
44351a7c1b72Smws 			continue; /* no translator defined and none required */
44361a7c1b72Smws 
44371a7c1b72Smws 		dnerror(dnp, D_PROV_PRXLATOR, "translator for %s:%s output "
44381a7c1b72Smws 		    "argument #%u from %s to %s is not defined\n",
44391a7c1b72Smws 		    pvp->pv_desc.dtvd_name, dnp->dn_ident->di_name, i + 1,
44401a7c1b72Smws 		    dt_node_type_name(nnp, n1, sizeof (n1)),
44411a7c1b72Smws 		    dt_node_type_name(xnp, n2, sizeof (n2)));
44421a7c1b72Smws 	}
44437c478bd9Sstevel@tonic-gate }
44447c478bd9Sstevel@tonic-gate 
44457c478bd9Sstevel@tonic-gate /*ARGSUSED*/
44467c478bd9Sstevel@tonic-gate static dt_node_t *
dt_cook_provider(dt_node_t * dnp,uint_t idflags)44477c478bd9Sstevel@tonic-gate dt_cook_provider(dt_node_t *dnp, uint_t idflags)
44487c478bd9Sstevel@tonic-gate {
44497c478bd9Sstevel@tonic-gate 	dt_provider_t *pvp = dnp->dn_provider;
44507c478bd9Sstevel@tonic-gate 	dt_node_t *pnp;
44517c478bd9Sstevel@tonic-gate 
44527c478bd9Sstevel@tonic-gate 	/*
44537c478bd9Sstevel@tonic-gate 	 * If we're declaring a provider for the first time and it is unknown
4454bbf21555SRichard Lowe 	 * to dtrace(4D), insert the probe definitions into the provider's hash.
44557c478bd9Sstevel@tonic-gate 	 * If we're redeclaring a known provider, verify the interface matches.
44567c478bd9Sstevel@tonic-gate 	 */
44577c478bd9Sstevel@tonic-gate 	for (pnp = dnp->dn_probes; pnp != NULL; pnp = pnp->dn_list) {
44587c478bd9Sstevel@tonic-gate 		const char *probename = pnp->dn_ident->di_name;
44597c478bd9Sstevel@tonic-gate 		dt_probe_t *prp = dt_probe_lookup(pvp, probename);
44607c478bd9Sstevel@tonic-gate 
44617c478bd9Sstevel@tonic-gate 		assert(pnp->dn_kind == DT_NODE_PROBE);
44627c478bd9Sstevel@tonic-gate 
44637c478bd9Sstevel@tonic-gate 		if (prp != NULL && dnp->dn_provred) {
44647c478bd9Sstevel@tonic-gate 			dt_node_provider_cmp(pvp, pnp,
44657c478bd9Sstevel@tonic-gate 			    prp, pnp->dn_ident->di_data);
44667c478bd9Sstevel@tonic-gate 		} else if (prp == NULL && dnp->dn_provred) {
44677c478bd9Sstevel@tonic-gate 			dnerror(pnp, D_PROV_INCOMPAT,
44687c478bd9Sstevel@tonic-gate 			    "provider interface mismatch: %s\n"
44697c478bd9Sstevel@tonic-gate 			    "\t current: probe %s:%s defined\n"
44707c478bd9Sstevel@tonic-gate 			    "\tprevious: probe %s:%s not defined\n",
44717c478bd9Sstevel@tonic-gate 			    dnp->dn_provname, dnp->dn_provname,
44727c478bd9Sstevel@tonic-gate 			    probename, dnp->dn_provname, probename);
44737c478bd9Sstevel@tonic-gate 		} else if (prp != NULL) {
44747c478bd9Sstevel@tonic-gate 			dnerror(pnp, D_PROV_PRDUP, "probe redeclared: %s:%s\n",
44757c478bd9Sstevel@tonic-gate 			    dnp->dn_provname, probename);
44767c478bd9Sstevel@tonic-gate 		} else
44777c478bd9Sstevel@tonic-gate 			dt_probe_declare(pvp, pnp->dn_ident->di_data);
44781a7c1b72Smws 
44791a7c1b72Smws 		dt_cook_probe(pnp, pvp);
44807c478bd9Sstevel@tonic-gate 	}
44817c478bd9Sstevel@tonic-gate 
44827c478bd9Sstevel@tonic-gate 	return (dnp);
44837c478bd9Sstevel@tonic-gate }
44847c478bd9Sstevel@tonic-gate 
44857c478bd9Sstevel@tonic-gate /*ARGSUSED*/
44867c478bd9Sstevel@tonic-gate static dt_node_t *
dt_cook_none(dt_node_t * dnp,uint_t idflags)44877c478bd9Sstevel@tonic-gate dt_cook_none(dt_node_t *dnp, uint_t idflags)
44887c478bd9Sstevel@tonic-gate {
44897c478bd9Sstevel@tonic-gate 	return (dnp);
44907c478bd9Sstevel@tonic-gate }
44917c478bd9Sstevel@tonic-gate 
44927c478bd9Sstevel@tonic-gate static dt_node_t *(*dt_cook_funcs[])(dt_node_t *, uint_t) = {
44937c478bd9Sstevel@tonic-gate 	dt_cook_none,		/* DT_NODE_FREE */
44947c478bd9Sstevel@tonic-gate 	dt_cook_none,		/* DT_NODE_INT */
44957c478bd9Sstevel@tonic-gate 	dt_cook_none,		/* DT_NODE_STRING */
44967c478bd9Sstevel@tonic-gate 	dt_cook_ident,		/* DT_NODE_IDENT */
44977c478bd9Sstevel@tonic-gate 	dt_cook_var,		/* DT_NODE_VAR */
44987c478bd9Sstevel@tonic-gate 	dt_cook_none,		/* DT_NODE_SYM */
44997c478bd9Sstevel@tonic-gate 	dt_cook_none,		/* DT_NODE_TYPE */
45007c478bd9Sstevel@tonic-gate 	dt_cook_func,		/* DT_NODE_FUNC */
45017c478bd9Sstevel@tonic-gate 	dt_cook_op1,		/* DT_NODE_OP1 */
45027c478bd9Sstevel@tonic-gate 	dt_cook_op2,		/* DT_NODE_OP2 */
45037c478bd9Sstevel@tonic-gate 	dt_cook_op3,		/* DT_NODE_OP3 */
45047c478bd9Sstevel@tonic-gate 	dt_cook_statement,	/* DT_NODE_DEXPR */
45057c478bd9Sstevel@tonic-gate 	dt_cook_statement,	/* DT_NODE_DFUNC */
45067c478bd9Sstevel@tonic-gate 	dt_cook_aggregation,	/* DT_NODE_AGG */
45077c478bd9Sstevel@tonic-gate 	dt_cook_none,		/* DT_NODE_PDESC */
45087c478bd9Sstevel@tonic-gate 	dt_cook_clause,		/* DT_NODE_CLAUSE */
45097c478bd9Sstevel@tonic-gate 	dt_cook_inline,		/* DT_NODE_INLINE */
45107c478bd9Sstevel@tonic-gate 	dt_cook_member,		/* DT_NODE_MEMBER */
45117c478bd9Sstevel@tonic-gate 	dt_cook_xlator,		/* DT_NODE_XLATOR */
45127c478bd9Sstevel@tonic-gate 	dt_cook_none,		/* DT_NODE_PROBE */
45137c478bd9Sstevel@tonic-gate 	dt_cook_provider,	/* DT_NODE_PROVIDER */
4514c3bd3abdSMatthew Ahrens 	dt_cook_none,		/* DT_NODE_PROG */
4515c3bd3abdSMatthew Ahrens 	dt_cook_none,		/* DT_NODE_IF */
45167c478bd9Sstevel@tonic-gate };
45177c478bd9Sstevel@tonic-gate 
45187c478bd9Sstevel@tonic-gate /*
45197c478bd9Sstevel@tonic-gate  * Recursively cook the parse tree starting at the specified node.  The idflags
45207c478bd9Sstevel@tonic-gate  * parameter is used to indicate the type of reference (r/w) and is applied to
45217c478bd9Sstevel@tonic-gate  * the resulting identifier if it is a D variable or D aggregation.
45227c478bd9Sstevel@tonic-gate  */
45237c478bd9Sstevel@tonic-gate dt_node_t *
dt_node_cook(dt_node_t * dnp,uint_t idflags)45247c478bd9Sstevel@tonic-gate dt_node_cook(dt_node_t *dnp, uint_t idflags)
45257c478bd9Sstevel@tonic-gate {
45267c478bd9Sstevel@tonic-gate 	int oldlineno = yylineno;
45277c478bd9Sstevel@tonic-gate 
45287c478bd9Sstevel@tonic-gate 	yylineno = dnp->dn_line;
45297c478bd9Sstevel@tonic-gate 
4530c3bd3abdSMatthew Ahrens 	assert(dnp->dn_kind <
4531c3bd3abdSMatthew Ahrens 	    sizeof (dt_cook_funcs) / sizeof (dt_cook_funcs[0]));
45327c478bd9Sstevel@tonic-gate 	dnp = dt_cook_funcs[dnp->dn_kind](dnp, idflags);
45337c478bd9Sstevel@tonic-gate 	dnp->dn_flags |= DT_NF_COOKED;
45347c478bd9Sstevel@tonic-gate 
45357c478bd9Sstevel@tonic-gate 	if (dnp->dn_kind == DT_NODE_VAR || dnp->dn_kind == DT_NODE_AGG)
45367c478bd9Sstevel@tonic-gate 		dnp->dn_ident->di_flags |= idflags;
45377c478bd9Sstevel@tonic-gate 
45387c478bd9Sstevel@tonic-gate 	yylineno = oldlineno;
45397c478bd9Sstevel@tonic-gate 	return (dnp);
45407c478bd9Sstevel@tonic-gate }
45417c478bd9Sstevel@tonic-gate 
45427c478bd9Sstevel@tonic-gate dtrace_attribute_t
dt_node_list_cook(dt_node_t ** pnp,uint_t idflags)45437c478bd9Sstevel@tonic-gate dt_node_list_cook(dt_node_t **pnp, uint_t idflags)
45447c478bd9Sstevel@tonic-gate {
45457c478bd9Sstevel@tonic-gate 	dtrace_attribute_t attr = _dtrace_defattr;
45467c478bd9Sstevel@tonic-gate 	dt_node_t *dnp, *nnp;
45477c478bd9Sstevel@tonic-gate 
45487c478bd9Sstevel@tonic-gate 	for (dnp = (pnp != NULL ? *pnp : NULL); dnp != NULL; dnp = nnp) {
45497c478bd9Sstevel@tonic-gate 		nnp = dnp->dn_list;
45507c478bd9Sstevel@tonic-gate 		dnp = *pnp = dt_node_cook(dnp, idflags);
45517c478bd9Sstevel@tonic-gate 		attr = dt_attr_min(attr, dnp->dn_attr);
45527c478bd9Sstevel@tonic-gate 		dnp->dn_list = nnp;
45537c478bd9Sstevel@tonic-gate 		pnp = &dnp->dn_list;
45547c478bd9Sstevel@tonic-gate 	}
45557c478bd9Sstevel@tonic-gate 
45567c478bd9Sstevel@tonic-gate 	return (attr);
45577c478bd9Sstevel@tonic-gate }
45587c478bd9Sstevel@tonic-gate 
45597c478bd9Sstevel@tonic-gate void
dt_node_list_free(dt_node_t ** pnp)45607c478bd9Sstevel@tonic-gate dt_node_list_free(dt_node_t **pnp)
45617c478bd9Sstevel@tonic-gate {
45627c478bd9Sstevel@tonic-gate 	dt_node_t *dnp, *nnp;
45637c478bd9Sstevel@tonic-gate 
45647c478bd9Sstevel@tonic-gate 	for (dnp = (pnp != NULL ? *pnp : NULL); dnp != NULL; dnp = nnp) {
45657c478bd9Sstevel@tonic-gate 		nnp = dnp->dn_list;
45667c478bd9Sstevel@tonic-gate 		dt_node_free(dnp);
45677c478bd9Sstevel@tonic-gate 	}
45687c478bd9Sstevel@tonic-gate 
45697c478bd9Sstevel@tonic-gate 	if (pnp != NULL)
45707c478bd9Sstevel@tonic-gate 		*pnp = NULL;
45717c478bd9Sstevel@tonic-gate }
45727c478bd9Sstevel@tonic-gate 
45737c478bd9Sstevel@tonic-gate void
dt_node_link_free(dt_node_t ** pnp)45747c478bd9Sstevel@tonic-gate dt_node_link_free(dt_node_t **pnp)
45757c478bd9Sstevel@tonic-gate {
45767c478bd9Sstevel@tonic-gate 	dt_node_t *dnp, *nnp;
45777c478bd9Sstevel@tonic-gate 
45787c478bd9Sstevel@tonic-gate 	for (dnp = (pnp != NULL ? *pnp : NULL); dnp != NULL; dnp = nnp) {
45797c478bd9Sstevel@tonic-gate 		nnp = dnp->dn_link;
45807c478bd9Sstevel@tonic-gate 		dt_node_free(dnp);
45817c478bd9Sstevel@tonic-gate 	}
45827c478bd9Sstevel@tonic-gate 
45837c478bd9Sstevel@tonic-gate 	for (dnp = (pnp != NULL ? *pnp : NULL); dnp != NULL; dnp = nnp) {
45847c478bd9Sstevel@tonic-gate 		nnp = dnp->dn_link;
45857c478bd9Sstevel@tonic-gate 		free(dnp);
45867c478bd9Sstevel@tonic-gate 	}
45877c478bd9Sstevel@tonic-gate 
45887c478bd9Sstevel@tonic-gate 	if (pnp != NULL)
45897c478bd9Sstevel@tonic-gate 		*pnp = NULL;
45907c478bd9Sstevel@tonic-gate }
45917c478bd9Sstevel@tonic-gate 
45927c478bd9Sstevel@tonic-gate dt_node_t *
dt_node_link(dt_node_t * lp,dt_node_t * rp)45937c478bd9Sstevel@tonic-gate dt_node_link(dt_node_t *lp, dt_node_t *rp)
45947c478bd9Sstevel@tonic-gate {
45957c478bd9Sstevel@tonic-gate 	dt_node_t *dnp;
45967c478bd9Sstevel@tonic-gate 
45977c478bd9Sstevel@tonic-gate 	if (lp == NULL)
45987c478bd9Sstevel@tonic-gate 		return (rp);
45997c478bd9Sstevel@tonic-gate 	else if (rp == NULL)
46007c478bd9Sstevel@tonic-gate 		return (lp);
46017c478bd9Sstevel@tonic-gate 
46027c478bd9Sstevel@tonic-gate 	for (dnp = lp; dnp->dn_list != NULL; dnp = dnp->dn_list)
46037c478bd9Sstevel@tonic-gate 		continue;
46047c478bd9Sstevel@tonic-gate 
46057c478bd9Sstevel@tonic-gate 	dnp->dn_list = rp;
46067c478bd9Sstevel@tonic-gate 	return (lp);
46077c478bd9Sstevel@tonic-gate }
46087c478bd9Sstevel@tonic-gate 
46091a7c1b72Smws /*
46101a7c1b72Smws  * Compute the DOF dtrace_diftype_t representation of a node's type.  This is
46111a7c1b72Smws  * called from a variety of places in the library so it cannot assume yypcb
46121a7c1b72Smws  * is valid: any references to handle-specific data must be made through 'dtp'.
46131a7c1b72Smws  */
46147c478bd9Sstevel@tonic-gate void
dt_node_diftype(dtrace_hdl_t * dtp,const dt_node_t * dnp,dtrace_diftype_t * tp)46151a7c1b72Smws dt_node_diftype(dtrace_hdl_t *dtp, const dt_node_t *dnp, dtrace_diftype_t *tp)
46167c478bd9Sstevel@tonic-gate {
46171a7c1b72Smws 	if (dnp->dn_ctfp == DT_STR_CTFP(dtp) &&
46181a7c1b72Smws 	    dnp->dn_type == DT_STR_TYPE(dtp)) {
46197c478bd9Sstevel@tonic-gate 		tp->dtdt_kind = DIF_TYPE_STRING;
46207c478bd9Sstevel@tonic-gate 		tp->dtdt_ckind = CTF_K_UNKNOWN;
46217c478bd9Sstevel@tonic-gate 	} else {
46227c478bd9Sstevel@tonic-gate 		tp->dtdt_kind = DIF_TYPE_CTF;
46237c478bd9Sstevel@tonic-gate 		tp->dtdt_ckind = ctf_type_kind(dnp->dn_ctfp,
46247c478bd9Sstevel@tonic-gate 		    ctf_type_resolve(dnp->dn_ctfp, dnp->dn_type));
46257c478bd9Sstevel@tonic-gate 	}
46267c478bd9Sstevel@tonic-gate 
4627a386cc11SRobert Mustacchi 	tp->dtdt_flags = (dnp->dn_flags & DT_NF_REF) ?
4628a386cc11SRobert Mustacchi 	    (dnp->dn_flags & DT_NF_USERLAND) ? DIF_TF_BYUREF :
4629a386cc11SRobert Mustacchi 	    DIF_TF_BYREF : 0;
46307c478bd9Sstevel@tonic-gate 	tp->dtdt_pad = 0;
46317c478bd9Sstevel@tonic-gate 	tp->dtdt_size = ctf_type_size(dnp->dn_ctfp, dnp->dn_type);
46327c478bd9Sstevel@tonic-gate }
46337c478bd9Sstevel@tonic-gate 
4634c3bd3abdSMatthew Ahrens /*
4635c3bd3abdSMatthew Ahrens  * Output the parse tree as D.  The "-xtree=8" argument will call this
4636c3bd3abdSMatthew Ahrens  * function to print out the program after any syntactic sugar
4637c3bd3abdSMatthew Ahrens  * transformations have been applied (e.g. to implement "if").  The
4638c3bd3abdSMatthew Ahrens  * resulting output can be used to understand the transformations
4639c3bd3abdSMatthew Ahrens  * applied by these features, or to run such a script on a system that
4640c3bd3abdSMatthew Ahrens  * does not support these features
4641c3bd3abdSMatthew Ahrens  *
4642c3bd3abdSMatthew Ahrens  * Note that the output does not express precisely the same program as
4643c3bd3abdSMatthew Ahrens  * the input.  In particular:
4644c3bd3abdSMatthew Ahrens  *  - Only the clauses are output.  #pragma options, variable
4645c3bd3abdSMatthew Ahrens  *    declarations, etc. are excluded.
4646c3bd3abdSMatthew Ahrens  *  - Command argument substitution has already been done, so the output
4647c3bd3abdSMatthew Ahrens  *    will not contain e.g. $$1, but rather the substituted string.
4648c3bd3abdSMatthew Ahrens  */
4649c3bd3abdSMatthew Ahrens void
dt_printd(dt_node_t * dnp,FILE * fp,int depth)4650c3bd3abdSMatthew Ahrens dt_printd(dt_node_t *dnp, FILE *fp, int depth)
4651c3bd3abdSMatthew Ahrens {
4652c3bd3abdSMatthew Ahrens 	dt_node_t *arg;
4653c3bd3abdSMatthew Ahrens 
4654c3bd3abdSMatthew Ahrens 	switch (dnp->dn_kind) {
4655c3bd3abdSMatthew Ahrens 	case DT_NODE_INT:
4656c3bd3abdSMatthew Ahrens 		(void) fprintf(fp, "0x%llx", (u_longlong_t)dnp->dn_value);
4657c3bd3abdSMatthew Ahrens 		if (!(dnp->dn_flags & DT_NF_SIGNED))
4658c3bd3abdSMatthew Ahrens 			(void) fprintf(fp, "u");
4659c3bd3abdSMatthew Ahrens 		break;
4660c3bd3abdSMatthew Ahrens 
4661c3bd3abdSMatthew Ahrens 	case DT_NODE_STRING: {
4662c3bd3abdSMatthew Ahrens 		char *escd = strchr2esc(dnp->dn_string, strlen(dnp->dn_string));
4663c3bd3abdSMatthew Ahrens 		(void) fprintf(fp, "\"%s\"", escd);
4664c3bd3abdSMatthew Ahrens 		free(escd);
4665c3bd3abdSMatthew Ahrens 		break;
4666c3bd3abdSMatthew Ahrens 	}
4667c3bd3abdSMatthew Ahrens 
4668c3bd3abdSMatthew Ahrens 	case DT_NODE_IDENT:
4669c3bd3abdSMatthew Ahrens 		(void) fprintf(fp, "%s", dnp->dn_string);
4670c3bd3abdSMatthew Ahrens 		break;
4671c3bd3abdSMatthew Ahrens 
4672c3bd3abdSMatthew Ahrens 	case DT_NODE_VAR:
4673c3bd3abdSMatthew Ahrens 		(void) fprintf(fp, "%s%s",
4674c3bd3abdSMatthew Ahrens 		    (dnp->dn_ident->di_flags & DT_IDFLG_LOCAL) ? "this->" :
4675c3bd3abdSMatthew Ahrens 		    (dnp->dn_ident->di_flags & DT_IDFLG_TLS) ? "self->" : "",
4676c3bd3abdSMatthew Ahrens 		    dnp->dn_ident->di_name);
4677c3bd3abdSMatthew Ahrens 
4678c3bd3abdSMatthew Ahrens 		if (dnp->dn_args != NULL) {
4679c3bd3abdSMatthew Ahrens 			(void) fprintf(fp, "[");
4680c3bd3abdSMatthew Ahrens 
4681c3bd3abdSMatthew Ahrens 			for (arg = dnp->dn_args; arg != NULL;
4682c3bd3abdSMatthew Ahrens 			    arg = arg->dn_list) {
4683c3bd3abdSMatthew Ahrens 				dt_printd(arg, fp, 0);
4684c3bd3abdSMatthew Ahrens 				if (arg->dn_list != NULL)
4685c3bd3abdSMatthew Ahrens 					(void) fprintf(fp, ", ");
4686c3bd3abdSMatthew Ahrens 			}
4687c3bd3abdSMatthew Ahrens 
4688c3bd3abdSMatthew Ahrens 			(void) fprintf(fp, "]");
4689c3bd3abdSMatthew Ahrens 		}
4690c3bd3abdSMatthew Ahrens 		break;
4691c3bd3abdSMatthew Ahrens 
4692c3bd3abdSMatthew Ahrens 	case DT_NODE_SYM: {
4693c3bd3abdSMatthew Ahrens 		const dtrace_syminfo_t *dts = dnp->dn_ident->di_data;
4694c3bd3abdSMatthew Ahrens 		(void) fprintf(fp, "%s`%s", dts->dts_object, dts->dts_name);
4695c3bd3abdSMatthew Ahrens 		break;
4696c3bd3abdSMatthew Ahrens 	}
4697c3bd3abdSMatthew Ahrens 	case DT_NODE_FUNC:
4698c3bd3abdSMatthew Ahrens 		(void) fprintf(fp, "%s(", dnp->dn_ident->di_name);
4699c3bd3abdSMatthew Ahrens 
4700c3bd3abdSMatthew Ahrens 		for (arg = dnp->dn_args; arg != NULL; arg = arg->dn_list) {
4701c3bd3abdSMatthew Ahrens 			dt_printd(arg, fp, 0);
4702c3bd3abdSMatthew Ahrens 			if (arg->dn_list != NULL)
4703c3bd3abdSMatthew Ahrens 				(void) fprintf(fp, ", ");
4704c3bd3abdSMatthew Ahrens 		}
4705c3bd3abdSMatthew Ahrens 		(void) fprintf(fp, ")");
4706c3bd3abdSMatthew Ahrens 		break;
4707c3bd3abdSMatthew Ahrens 
4708c3bd3abdSMatthew Ahrens 	case DT_NODE_OP1:
4709c3bd3abdSMatthew Ahrens 		(void) fprintf(fp, "%s(", opstr(dnp->dn_op));
4710c3bd3abdSMatthew Ahrens 		dt_printd(dnp->dn_child, fp, 0);
4711c3bd3abdSMatthew Ahrens 		(void) fprintf(fp, ")");
4712c3bd3abdSMatthew Ahrens 		break;
4713c3bd3abdSMatthew Ahrens 
4714c3bd3abdSMatthew Ahrens 	case DT_NODE_OP2:
4715c3bd3abdSMatthew Ahrens 		(void) fprintf(fp, "(");
4716c3bd3abdSMatthew Ahrens 		dt_printd(dnp->dn_left, fp, 0);
4717c3bd3abdSMatthew Ahrens 		if (dnp->dn_op == DT_TOK_LPAR) {
4718c3bd3abdSMatthew Ahrens 			(void) fprintf(fp, ")");
4719c3bd3abdSMatthew Ahrens 			dt_printd(dnp->dn_right, fp, 0);
4720c3bd3abdSMatthew Ahrens 			break;
4721c3bd3abdSMatthew Ahrens 		}
4722c3bd3abdSMatthew Ahrens 		if (dnp->dn_op == DT_TOK_PTR || dnp->dn_op == DT_TOK_DOT ||
4723c3bd3abdSMatthew Ahrens 		    dnp->dn_op == DT_TOK_LBRAC)
4724c3bd3abdSMatthew Ahrens 			(void) fprintf(fp, "%s", opstr(dnp->dn_op));
4725c3bd3abdSMatthew Ahrens 		else
4726c3bd3abdSMatthew Ahrens 			(void) fprintf(fp, " %s ", opstr(dnp->dn_op));
4727c3bd3abdSMatthew Ahrens 		dt_printd(dnp->dn_right, fp, 0);
4728c3bd3abdSMatthew Ahrens 		if (dnp->dn_op == DT_TOK_LBRAC) {
4729c3bd3abdSMatthew Ahrens 			dt_node_t *ln = dnp->dn_right;
4730c3bd3abdSMatthew Ahrens 			while (ln->dn_list != NULL) {
4731c3bd3abdSMatthew Ahrens 				(void) fprintf(fp, ", ");
4732c3bd3abdSMatthew Ahrens 				dt_printd(ln->dn_list, fp, depth);
4733c3bd3abdSMatthew Ahrens 				ln = ln->dn_list;
4734c3bd3abdSMatthew Ahrens 			}
4735c3bd3abdSMatthew Ahrens 			(void) fprintf(fp, "]");
4736c3bd3abdSMatthew Ahrens 		}
4737c3bd3abdSMatthew Ahrens 		(void) fprintf(fp, ")");
4738c3bd3abdSMatthew Ahrens 		break;
4739c3bd3abdSMatthew Ahrens 
4740c3bd3abdSMatthew Ahrens 	case DT_NODE_OP3:
4741c3bd3abdSMatthew Ahrens 		(void) fprintf(fp, "(");
4742c3bd3abdSMatthew Ahrens 		dt_printd(dnp->dn_expr, fp, 0);
4743c3bd3abdSMatthew Ahrens 		(void) fprintf(fp, " ? ");
4744c3bd3abdSMatthew Ahrens 		dt_printd(dnp->dn_left, fp, 0);
4745c3bd3abdSMatthew Ahrens 		(void) fprintf(fp, " : ");
4746c3bd3abdSMatthew Ahrens 		dt_printd(dnp->dn_right, fp, 0);
4747c3bd3abdSMatthew Ahrens 		(void) fprintf(fp, ")");
4748c3bd3abdSMatthew Ahrens 		break;
4749c3bd3abdSMatthew Ahrens 
4750c3bd3abdSMatthew Ahrens 	case DT_NODE_DEXPR:
4751c3bd3abdSMatthew Ahrens 	case DT_NODE_DFUNC:
4752c3bd3abdSMatthew Ahrens 		(void) fprintf(fp, "%*s", depth * 8, "");
4753c3bd3abdSMatthew Ahrens 		dt_printd(dnp->dn_expr, fp, depth + 1);
4754c3bd3abdSMatthew Ahrens 		(void) fprintf(fp, ";\n");
4755c3bd3abdSMatthew Ahrens 		break;
4756c3bd3abdSMatthew Ahrens 
4757c3bd3abdSMatthew Ahrens 	case DT_NODE_PDESC:
4758c3bd3abdSMatthew Ahrens 		(void) fprintf(fp, "%s:%s:%s:%s",
4759c3bd3abdSMatthew Ahrens 		    dnp->dn_desc->dtpd_provider, dnp->dn_desc->dtpd_mod,
4760c3bd3abdSMatthew Ahrens 		    dnp->dn_desc->dtpd_func, dnp->dn_desc->dtpd_name);
4761c3bd3abdSMatthew Ahrens 		break;
4762c3bd3abdSMatthew Ahrens 
4763c3bd3abdSMatthew Ahrens 	case DT_NODE_CLAUSE:
4764c3bd3abdSMatthew Ahrens 		for (arg = dnp->dn_pdescs; arg != NULL; arg = arg->dn_list) {
4765c3bd3abdSMatthew Ahrens 			dt_printd(arg, fp, 0);
4766c3bd3abdSMatthew Ahrens 			if (arg->dn_list != NULL)
4767c3bd3abdSMatthew Ahrens 				(void) fprintf(fp, ",");
4768c3bd3abdSMatthew Ahrens 			(void) fprintf(fp, "\n");
4769c3bd3abdSMatthew Ahrens 		}
4770c3bd3abdSMatthew Ahrens 
4771c3bd3abdSMatthew Ahrens 		if (dnp->dn_pred != NULL) {
4772c3bd3abdSMatthew Ahrens 			(void) fprintf(fp, "/");
4773c3bd3abdSMatthew Ahrens 			dt_printd(dnp->dn_pred, fp, 0);
4774c3bd3abdSMatthew Ahrens 			(void) fprintf(fp, "/\n");
4775c3bd3abdSMatthew Ahrens 		}
4776c3bd3abdSMatthew Ahrens 			(void) fprintf(fp, "{\n");
4777c3bd3abdSMatthew Ahrens 
4778c3bd3abdSMatthew Ahrens 		for (arg = dnp->dn_acts; arg != NULL; arg = arg->dn_list)
4779c3bd3abdSMatthew Ahrens 			dt_printd(arg, fp, depth + 1);
4780c3bd3abdSMatthew Ahrens 		(void) fprintf(fp, "}\n");
4781c3bd3abdSMatthew Ahrens 		(void) fprintf(fp, "\n");
4782c3bd3abdSMatthew Ahrens 		break;
4783c3bd3abdSMatthew Ahrens 
4784c3bd3abdSMatthew Ahrens 	case DT_NODE_IF:
4785c3bd3abdSMatthew Ahrens 		(void) fprintf(fp, "%*sif (", depth * 8, "");
4786c3bd3abdSMatthew Ahrens 		dt_printd(dnp->dn_conditional, fp, 0);
4787c3bd3abdSMatthew Ahrens 		(void) fprintf(fp, ") {\n");
4788c3bd3abdSMatthew Ahrens 
4789c3bd3abdSMatthew Ahrens 		for (arg = dnp->dn_body; arg != NULL; arg = arg->dn_list)
4790c3bd3abdSMatthew Ahrens 			dt_printd(arg, fp, depth + 1);
4791c3bd3abdSMatthew Ahrens 		if (dnp->dn_alternate_body == NULL) {
4792c3bd3abdSMatthew Ahrens 			(void) fprintf(fp, "%*s}\n", depth * 8, "");
4793c3bd3abdSMatthew Ahrens 		} else {
4794c3bd3abdSMatthew Ahrens 			(void) fprintf(fp, "%*s} else {\n", depth * 8, "");
4795c3bd3abdSMatthew Ahrens 			for (arg = dnp->dn_alternate_body; arg != NULL;
4796c3bd3abdSMatthew Ahrens 			    arg = arg->dn_list)
4797c3bd3abdSMatthew Ahrens 				dt_printd(arg, fp, depth + 1);
4798c3bd3abdSMatthew Ahrens 			(void) fprintf(fp, "%*s}\n", depth * 8, "");
4799c3bd3abdSMatthew Ahrens 		}
4800c3bd3abdSMatthew Ahrens 
4801c3bd3abdSMatthew Ahrens 		break;
4802c3bd3abdSMatthew Ahrens 
4803c3bd3abdSMatthew Ahrens 	default:
4804c3bd3abdSMatthew Ahrens 		(void) fprintf(fp, "/* bad node %p, kind %d */\n",
4805c3bd3abdSMatthew Ahrens 		    (void *)dnp, dnp->dn_kind);
4806c3bd3abdSMatthew Ahrens 	}
4807c3bd3abdSMatthew Ahrens }
4808c3bd3abdSMatthew Ahrens 
48097c478bd9Sstevel@tonic-gate void
dt_node_printr(dt_node_t * dnp,FILE * fp,int depth)48107c478bd9Sstevel@tonic-gate dt_node_printr(dt_node_t *dnp, FILE *fp, int depth)
48117c478bd9Sstevel@tonic-gate {
48127c478bd9Sstevel@tonic-gate 	char n[DT_TYPE_NAMELEN], buf[BUFSIZ], a[8];
48137c478bd9Sstevel@tonic-gate 	const dtrace_syminfo_t *dts;
48147c478bd9Sstevel@tonic-gate 	const dt_idnode_t *inp;
48157c478bd9Sstevel@tonic-gate 	dt_node_t *arg;
48167c478bd9Sstevel@tonic-gate 
48177c478bd9Sstevel@tonic-gate 	(void) fprintf(fp, "%*s", depth * 2, "");
48187c478bd9Sstevel@tonic-gate 	(void) dt_attr_str(dnp->dn_attr, a, sizeof (a));
48197c478bd9Sstevel@tonic-gate 
48207c478bd9Sstevel@tonic-gate 	if (dnp->dn_ctfp != NULL && dnp->dn_type != CTF_ERR &&
48217c478bd9Sstevel@tonic-gate 	    ctf_type_name(dnp->dn_ctfp, dnp->dn_type, n, sizeof (n)) != NULL) {
48227c478bd9Sstevel@tonic-gate 		(void) snprintf(buf, BUFSIZ, "type=<%s> attr=%s flags=", n, a);
48237c478bd9Sstevel@tonic-gate 	} else {
48247c478bd9Sstevel@tonic-gate 		(void) snprintf(buf, BUFSIZ, "type=<%ld> attr=%s flags=",
48257c478bd9Sstevel@tonic-gate 		    dnp->dn_type, a);
48267c478bd9Sstevel@tonic-gate 	}
48277c478bd9Sstevel@tonic-gate 
48287c478bd9Sstevel@tonic-gate 	if (dnp->dn_flags != 0) {
48297c478bd9Sstevel@tonic-gate 		n[0] = '\0';
48307c478bd9Sstevel@tonic-gate 		if (dnp->dn_flags & DT_NF_SIGNED)
48317c478bd9Sstevel@tonic-gate 			(void) strcat(n, ",SIGN");
48327c478bd9Sstevel@tonic-gate 		if (dnp->dn_flags & DT_NF_COOKED)
48337c478bd9Sstevel@tonic-gate 			(void) strcat(n, ",COOK");
48347c478bd9Sstevel@tonic-gate 		if (dnp->dn_flags & DT_NF_REF)
48357c478bd9Sstevel@tonic-gate 			(void) strcat(n, ",REF");
48367c478bd9Sstevel@tonic-gate 		if (dnp->dn_flags & DT_NF_LVALUE)
48377c478bd9Sstevel@tonic-gate 			(void) strcat(n, ",LVAL");
48387c478bd9Sstevel@tonic-gate 		if (dnp->dn_flags & DT_NF_WRITABLE)
48397c478bd9Sstevel@tonic-gate 			(void) strcat(n, ",WRITE");
48407c478bd9Sstevel@tonic-gate 		if (dnp->dn_flags & DT_NF_BITFIELD)
48417c478bd9Sstevel@tonic-gate 			(void) strcat(n, ",BITF");
48427c478bd9Sstevel@tonic-gate 		if (dnp->dn_flags & DT_NF_USERLAND)
48437c478bd9Sstevel@tonic-gate 			(void) strcat(n, ",USER");
48447c478bd9Sstevel@tonic-gate 		(void) strcat(buf, n + 1);
48457c478bd9Sstevel@tonic-gate 	} else
48467c478bd9Sstevel@tonic-gate 		(void) strcat(buf, "0");
48477c478bd9Sstevel@tonic-gate 
48487c478bd9Sstevel@tonic-gate 	switch (dnp->dn_kind) {
48497c478bd9Sstevel@tonic-gate 	case DT_NODE_FREE:
48507c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, "FREE <node %p>\n", (void *)dnp);
48517c478bd9Sstevel@tonic-gate 		break;
48527c478bd9Sstevel@tonic-gate 
48537c478bd9Sstevel@tonic-gate 	case DT_NODE_INT:
48547c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, "INT 0x%llx (%s)\n",
48557c478bd9Sstevel@tonic-gate 		    (u_longlong_t)dnp->dn_value, buf);
48567c478bd9Sstevel@tonic-gate 		break;
48577c478bd9Sstevel@tonic-gate 
4858*6eeafb34SRobert Mustacchi 	case DT_NODE_STRING: {
4859*6eeafb34SRobert Mustacchi 		char *escd = strchr2esc(dnp->dn_string, strlen(dnp->dn_string));
4860*6eeafb34SRobert Mustacchi 		(void) fprintf(fp, "STRING \"%s\" (%s)\n", escd, buf);
4861*6eeafb34SRobert Mustacchi 		free(escd);
48627c478bd9Sstevel@tonic-gate 		break;
4863*6eeafb34SRobert Mustacchi 	}
48647c478bd9Sstevel@tonic-gate 
48657c478bd9Sstevel@tonic-gate 	case DT_NODE_IDENT:
48667c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, "IDENT %s (%s)\n", dnp->dn_string, buf);
48677c478bd9Sstevel@tonic-gate 		break;
48687c478bd9Sstevel@tonic-gate 
48697c478bd9Sstevel@tonic-gate 	case DT_NODE_VAR:
48707c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, "VARIABLE %s%s (%s)\n",
48717c478bd9Sstevel@tonic-gate 		    (dnp->dn_ident->di_flags & DT_IDFLG_LOCAL) ? "this->" :
48727c478bd9Sstevel@tonic-gate 		    (dnp->dn_ident->di_flags & DT_IDFLG_TLS) ? "self->" : "",
48737c478bd9Sstevel@tonic-gate 		    dnp->dn_ident->di_name, buf);
48747c478bd9Sstevel@tonic-gate 
48757c478bd9Sstevel@tonic-gate 		if (dnp->dn_args != NULL)
48767c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, "%*s[\n", depth * 2, "");
48777c478bd9Sstevel@tonic-gate 
48787c478bd9Sstevel@tonic-gate 		for (arg = dnp->dn_args; arg != NULL; arg = arg->dn_list) {
48797c478bd9Sstevel@tonic-gate 			dt_node_printr(arg, fp, depth + 1);
48807c478bd9Sstevel@tonic-gate 			if (arg->dn_list != NULL)
48817c478bd9Sstevel@tonic-gate 				(void) fprintf(fp, "%*s,\n", depth * 2, "");
48827c478bd9Sstevel@tonic-gate 		}
48837c478bd9Sstevel@tonic-gate 
48847c478bd9Sstevel@tonic-gate 		if (dnp->dn_args != NULL)
48857c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, "%*s]\n", depth * 2, "");
48867c478bd9Sstevel@tonic-gate 		break;
48877c478bd9Sstevel@tonic-gate 
48887c478bd9Sstevel@tonic-gate 	case DT_NODE_SYM:
48897c478bd9Sstevel@tonic-gate 		dts = dnp->dn_ident->di_data;
48907c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, "SYMBOL %s`%s (%s)\n",
48917c478bd9Sstevel@tonic-gate 		    dts->dts_object, dts->dts_name, buf);
48927c478bd9Sstevel@tonic-gate 		break;
48937c478bd9Sstevel@tonic-gate 
48947c478bd9Sstevel@tonic-gate 	case DT_NODE_TYPE:
48957c478bd9Sstevel@tonic-gate 		if (dnp->dn_string != NULL) {
48967c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, "TYPE (%s) %s\n",
48977c478bd9Sstevel@tonic-gate 			    buf, dnp->dn_string);
48987c478bd9Sstevel@tonic-gate 		} else
48997c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, "TYPE (%s)\n", buf);
49007c478bd9Sstevel@tonic-gate 		break;
49017c478bd9Sstevel@tonic-gate 
49027c478bd9Sstevel@tonic-gate 	case DT_NODE_FUNC:
49037c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, "FUNC %s (%s)\n",
49047c478bd9Sstevel@tonic-gate 		    dnp->dn_ident->di_name, buf);
49057c478bd9Sstevel@tonic-gate 
49067c478bd9Sstevel@tonic-gate 		for (arg = dnp->dn_args; arg != NULL; arg = arg->dn_list) {
49077c478bd9Sstevel@tonic-gate 			dt_node_printr(arg, fp, depth + 1);
49087c478bd9Sstevel@tonic-gate 			if (arg->dn_list != NULL)
49097c478bd9Sstevel@tonic-gate 				(void) fprintf(fp, "%*s,\n", depth * 2, "");
49107c478bd9Sstevel@tonic-gate 		}
49117c478bd9Sstevel@tonic-gate 		break;
49127c478bd9Sstevel@tonic-gate 
49137c478bd9Sstevel@tonic-gate 	case DT_NODE_OP1:
49147c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, "OP1 %s (%s)\n", opstr(dnp->dn_op), buf);
49157c478bd9Sstevel@tonic-gate 		dt_node_printr(dnp->dn_child, fp, depth + 1);
49167c478bd9Sstevel@tonic-gate 		break;
49177c478bd9Sstevel@tonic-gate 
49187c478bd9Sstevel@tonic-gate 	case DT_NODE_OP2:
49197c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, "OP2 %s (%s)\n", opstr(dnp->dn_op), buf);
49207c478bd9Sstevel@tonic-gate 		dt_node_printr(dnp->dn_left, fp, depth + 1);
49217c478bd9Sstevel@tonic-gate 		dt_node_printr(dnp->dn_right, fp, depth + 1);
4922c3bd3abdSMatthew Ahrens 		if (dnp->dn_op == DT_TOK_LBRAC) {
4923c3bd3abdSMatthew Ahrens 			dt_node_t *ln = dnp->dn_right;
4924c3bd3abdSMatthew Ahrens 			while (ln->dn_list != NULL) {
4925c3bd3abdSMatthew Ahrens 				dt_node_printr(ln->dn_list, fp, depth + 1);
4926c3bd3abdSMatthew Ahrens 				ln = ln->dn_list;
4927c3bd3abdSMatthew Ahrens 			}
4928c3bd3abdSMatthew Ahrens 		}
49297c478bd9Sstevel@tonic-gate 		break;
49307c478bd9Sstevel@tonic-gate 
49317c478bd9Sstevel@tonic-gate 	case DT_NODE_OP3:
49327c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, "OP3 (%s)\n", buf);
49337c478bd9Sstevel@tonic-gate 		dt_node_printr(dnp->dn_expr, fp, depth + 1);
49347c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, "%*s?\n", depth * 2, "");
49357c478bd9Sstevel@tonic-gate 		dt_node_printr(dnp->dn_left, fp, depth + 1);
49367c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, "%*s:\n", depth * 2, "");
49377c478bd9Sstevel@tonic-gate 		dt_node_printr(dnp->dn_right, fp, depth + 1);
49387c478bd9Sstevel@tonic-gate 		break;
49397c478bd9Sstevel@tonic-gate 
49407c478bd9Sstevel@tonic-gate 	case DT_NODE_DEXPR:
49417c478bd9Sstevel@tonic-gate 	case DT_NODE_DFUNC:
49427c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, "D EXPRESSION attr=%s\n", a);
49437c478bd9Sstevel@tonic-gate 		dt_node_printr(dnp->dn_expr, fp, depth + 1);
49447c478bd9Sstevel@tonic-gate 		break;
49457c478bd9Sstevel@tonic-gate 
49467c478bd9Sstevel@tonic-gate 	case DT_NODE_AGG:
49477c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, "AGGREGATE @%s attr=%s [\n",
49487c478bd9Sstevel@tonic-gate 		    dnp->dn_ident->di_name, a);
49497c478bd9Sstevel@tonic-gate 
49507c478bd9Sstevel@tonic-gate 		for (arg = dnp->dn_aggtup; arg != NULL; arg = arg->dn_list) {
49517c478bd9Sstevel@tonic-gate 			dt_node_printr(arg, fp, depth + 1);
49527c478bd9Sstevel@tonic-gate 			if (arg->dn_list != NULL)
49537c478bd9Sstevel@tonic-gate 				(void) fprintf(fp, "%*s,\n", depth * 2, "");
49547c478bd9Sstevel@tonic-gate 		}
49557c478bd9Sstevel@tonic-gate 
49567c478bd9Sstevel@tonic-gate 		if (dnp->dn_aggfun) {
49577c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, "%*s] = ", depth * 2, "");
49587c478bd9Sstevel@tonic-gate 			dt_node_printr(dnp->dn_aggfun, fp, depth + 1);
49597c478bd9Sstevel@tonic-gate 		} else
49607c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, "%*s]\n", depth * 2, "");
49617c478bd9Sstevel@tonic-gate 
49627c478bd9Sstevel@tonic-gate 		if (dnp->dn_aggfun)
49637c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, "%*s)\n", depth * 2, "");
49647c478bd9Sstevel@tonic-gate 		break;
49657c478bd9Sstevel@tonic-gate 
49667c478bd9Sstevel@tonic-gate 	case DT_NODE_PDESC:
49677c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, "PDESC %s:%s:%s:%s [%u]\n",
49687c478bd9Sstevel@tonic-gate 		    dnp->dn_desc->dtpd_provider, dnp->dn_desc->dtpd_mod,
49697c478bd9Sstevel@tonic-gate 		    dnp->dn_desc->dtpd_func, dnp->dn_desc->dtpd_name,
49707c478bd9Sstevel@tonic-gate 		    dnp->dn_desc->dtpd_id);
49717c478bd9Sstevel@tonic-gate 		break;
49727c478bd9Sstevel@tonic-gate 
49737c478bd9Sstevel@tonic-gate 	case DT_NODE_CLAUSE:
49747c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, "CLAUSE attr=%s\n", a);
49757c478bd9Sstevel@tonic-gate 
49767c478bd9Sstevel@tonic-gate 		for (arg = dnp->dn_pdescs; arg != NULL; arg = arg->dn_list)
49777c478bd9Sstevel@tonic-gate 			dt_node_printr(arg, fp, depth + 1);
49787c478bd9Sstevel@tonic-gate 
49797c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, "%*sCTXATTR %s\n", depth * 2, "",
49807c478bd9Sstevel@tonic-gate 		    dt_attr_str(dnp->dn_ctxattr, a, sizeof (a)));
49817c478bd9Sstevel@tonic-gate 
49827c478bd9Sstevel@tonic-gate 		if (dnp->dn_pred != NULL) {
49837c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, "%*sPREDICATE /\n", depth * 2, "");
49847c478bd9Sstevel@tonic-gate 			dt_node_printr(dnp->dn_pred, fp, depth + 1);
49857c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, "%*s/\n", depth * 2, "");
49867c478bd9Sstevel@tonic-gate 		}
49877c478bd9Sstevel@tonic-gate 
49887c478bd9Sstevel@tonic-gate 		for (arg = dnp->dn_acts; arg != NULL; arg = arg->dn_list)
49897c478bd9Sstevel@tonic-gate 			dt_node_printr(arg, fp, depth + 1);
4990c3bd3abdSMatthew Ahrens 		(void) fprintf(fp, "\n");
49917c478bd9Sstevel@tonic-gate 		break;
49927c478bd9Sstevel@tonic-gate 
49937c478bd9Sstevel@tonic-gate 	case DT_NODE_INLINE:
49947c478bd9Sstevel@tonic-gate 		inp = dnp->dn_ident->di_iarg;
49957c478bd9Sstevel@tonic-gate 
49967c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, "INLINE %s (%s)\n",
49977c478bd9Sstevel@tonic-gate 		    dnp->dn_ident->di_name, buf);
49987c478bd9Sstevel@tonic-gate 		dt_node_printr(inp->din_root, fp, depth + 1);
49997c478bd9Sstevel@tonic-gate 		break;
50007c478bd9Sstevel@tonic-gate 
50017c478bd9Sstevel@tonic-gate 	case DT_NODE_MEMBER:
50027c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, "MEMBER %s (%s)\n", dnp->dn_membname, buf);
50037c478bd9Sstevel@tonic-gate 		if (dnp->dn_membexpr)
50047c478bd9Sstevel@tonic-gate 			dt_node_printr(dnp->dn_membexpr, fp, depth + 1);
50057c478bd9Sstevel@tonic-gate 		break;
50067c478bd9Sstevel@tonic-gate 
50077c478bd9Sstevel@tonic-gate 	case DT_NODE_XLATOR:
50087c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, "XLATOR (%s)", buf);
50097c478bd9Sstevel@tonic-gate 
50107c478bd9Sstevel@tonic-gate 		if (ctf_type_name(dnp->dn_xlator->dx_src_ctfp,
50117c478bd9Sstevel@tonic-gate 		    dnp->dn_xlator->dx_src_type, n, sizeof (n)) != NULL)
50127c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, " from <%s>", n);
50137c478bd9Sstevel@tonic-gate 
50147c478bd9Sstevel@tonic-gate 		if (ctf_type_name(dnp->dn_xlator->dx_dst_ctfp,
50157c478bd9Sstevel@tonic-gate 		    dnp->dn_xlator->dx_dst_type, n, sizeof (n)) != NULL)
50167c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, " to <%s>", n);
50177c478bd9Sstevel@tonic-gate 
50187c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, "\n");
50197c478bd9Sstevel@tonic-gate 
50207c478bd9Sstevel@tonic-gate 		for (arg = dnp->dn_members; arg != NULL; arg = arg->dn_list)
50217c478bd9Sstevel@tonic-gate 			dt_node_printr(arg, fp, depth + 1);
50227c478bd9Sstevel@tonic-gate 		break;
50237c478bd9Sstevel@tonic-gate 
50247c478bd9Sstevel@tonic-gate 	case DT_NODE_PROBE:
50257c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, "PROBE %s\n", dnp->dn_ident->di_name);
50267c478bd9Sstevel@tonic-gate 		break;
50277c478bd9Sstevel@tonic-gate 
50287c478bd9Sstevel@tonic-gate 	case DT_NODE_PROVIDER:
50297c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, "PROVIDER %s (%s)\n",
50307c478bd9Sstevel@tonic-gate 		    dnp->dn_provname, dnp->dn_provred ? "redecl" : "decl");
50317c478bd9Sstevel@tonic-gate 		for (arg = dnp->dn_probes; arg != NULL; arg = arg->dn_list)
50327c478bd9Sstevel@tonic-gate 			dt_node_printr(arg, fp, depth + 1);
50337c478bd9Sstevel@tonic-gate 		break;
50347c478bd9Sstevel@tonic-gate 
50357c478bd9Sstevel@tonic-gate 	case DT_NODE_PROG:
50367c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, "PROGRAM attr=%s\n", a);
50377c478bd9Sstevel@tonic-gate 		for (arg = dnp->dn_list; arg != NULL; arg = arg->dn_list)
50387c478bd9Sstevel@tonic-gate 			dt_node_printr(arg, fp, depth + 1);
50397c478bd9Sstevel@tonic-gate 		break;
50407c478bd9Sstevel@tonic-gate 
5041c3bd3abdSMatthew Ahrens 	case DT_NODE_IF:
5042c3bd3abdSMatthew Ahrens 		(void) fprintf(fp, "IF attr=%s CONDITION:\n", a);
5043c3bd3abdSMatthew Ahrens 
5044c3bd3abdSMatthew Ahrens 		dt_node_printr(dnp->dn_conditional, fp, depth + 1);
5045c3bd3abdSMatthew Ahrens 
5046c3bd3abdSMatthew Ahrens 		(void) fprintf(fp, "%*sIF BODY: \n", depth * 2, "");
5047c3bd3abdSMatthew Ahrens 		for (arg = dnp->dn_body; arg != NULL; arg = arg->dn_list)
5048c3bd3abdSMatthew Ahrens 			dt_node_printr(arg, fp, depth + 1);
5049c3bd3abdSMatthew Ahrens 
5050c3bd3abdSMatthew Ahrens 		if (dnp->dn_alternate_body != NULL) {
5051c3bd3abdSMatthew Ahrens 			(void) fprintf(fp, "%*sIF ELSE: \n", depth * 2, "");
5052c3bd3abdSMatthew Ahrens 			for (arg = dnp->dn_alternate_body; arg != NULL;
5053c3bd3abdSMatthew Ahrens 			    arg = arg->dn_list)
5054c3bd3abdSMatthew Ahrens 				dt_node_printr(arg, fp, depth + 1);
5055c3bd3abdSMatthew Ahrens 		}
5056c3bd3abdSMatthew Ahrens 
5057c3bd3abdSMatthew Ahrens 		break;
5058c3bd3abdSMatthew Ahrens 
50597c478bd9Sstevel@tonic-gate 	default:
50607c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, "<bad node %p, kind %d>\n",
50617c478bd9Sstevel@tonic-gate 		    (void *)dnp, dnp->dn_kind);
50627c478bd9Sstevel@tonic-gate 	}
50637c478bd9Sstevel@tonic-gate }
50647c478bd9Sstevel@tonic-gate 
50657c478bd9Sstevel@tonic-gate int
dt_node_root(dt_node_t * dnp)50667c478bd9Sstevel@tonic-gate dt_node_root(dt_node_t *dnp)
50677c478bd9Sstevel@tonic-gate {
50687c478bd9Sstevel@tonic-gate 	yypcb->pcb_root = dnp;
50697c478bd9Sstevel@tonic-gate 	return (0);
50707c478bd9Sstevel@tonic-gate }
50717c478bd9Sstevel@tonic-gate 
50727c478bd9Sstevel@tonic-gate /*PRINTFLIKE3*/
50737c478bd9Sstevel@tonic-gate void
dnerror(const dt_node_t * dnp,dt_errtag_t tag,const char * format,...)50747c478bd9Sstevel@tonic-gate dnerror(const dt_node_t *dnp, dt_errtag_t tag, const char *format, ...)
50757c478bd9Sstevel@tonic-gate {
50767c478bd9Sstevel@tonic-gate 	int oldlineno = yylineno;
50777c478bd9Sstevel@tonic-gate 	va_list ap;
50787c478bd9Sstevel@tonic-gate 
50797c478bd9Sstevel@tonic-gate 	yylineno = dnp->dn_line;
50807c478bd9Sstevel@tonic-gate 
50817c478bd9Sstevel@tonic-gate 	va_start(ap, format);
50827c478bd9Sstevel@tonic-gate 	xyvwarn(tag, format, ap);
50837c478bd9Sstevel@tonic-gate 	va_end(ap);
50847c478bd9Sstevel@tonic-gate 
50857c478bd9Sstevel@tonic-gate 	yylineno = oldlineno;
50867c478bd9Sstevel@tonic-gate 	longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
50877c478bd9Sstevel@tonic-gate }
50887c478bd9Sstevel@tonic-gate 
50897c478bd9Sstevel@tonic-gate /*PRINTFLIKE3*/
50907c478bd9Sstevel@tonic-gate void
dnwarn(const dt_node_t * dnp,dt_errtag_t tag,const char * format,...)50917c478bd9Sstevel@tonic-gate dnwarn(const dt_node_t *dnp, dt_errtag_t tag, const char *format, ...)
50927c478bd9Sstevel@tonic-gate {
50937c478bd9Sstevel@tonic-gate 	int oldlineno = yylineno;
50947c478bd9Sstevel@tonic-gate 	va_list ap;
50957c478bd9Sstevel@tonic-gate 
50967c478bd9Sstevel@tonic-gate 	yylineno = dnp->dn_line;
50977c478bd9Sstevel@tonic-gate 
50987c478bd9Sstevel@tonic-gate 	va_start(ap, format);
50997c478bd9Sstevel@tonic-gate 	xyvwarn(tag, format, ap);
51007c478bd9Sstevel@tonic-gate 	va_end(ap);
51017c478bd9Sstevel@tonic-gate 
51027c478bd9Sstevel@tonic-gate 	yylineno = oldlineno;
51037c478bd9Sstevel@tonic-gate }
51047c478bd9Sstevel@tonic-gate 
51057c478bd9Sstevel@tonic-gate /*PRINTFLIKE2*/
51067c478bd9Sstevel@tonic-gate void
xyerror(dt_errtag_t tag,const char * format,...)51077c478bd9Sstevel@tonic-gate xyerror(dt_errtag_t tag, const char *format, ...)
51087c478bd9Sstevel@tonic-gate {
51097c478bd9Sstevel@tonic-gate 	va_list ap;
51107c478bd9Sstevel@tonic-gate 
51117c478bd9Sstevel@tonic-gate 	va_start(ap, format);
51127c478bd9Sstevel@tonic-gate 	xyvwarn(tag, format, ap);
51137c478bd9Sstevel@tonic-gate 	va_end(ap);
51147c478bd9Sstevel@tonic-gate 
51157c478bd9Sstevel@tonic-gate 	longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
51167c478bd9Sstevel@tonic-gate }
51177c478bd9Sstevel@tonic-gate 
51187c478bd9Sstevel@tonic-gate /*PRINTFLIKE2*/
51197c478bd9Sstevel@tonic-gate void
xywarn(dt_errtag_t tag,const char * format,...)51207c478bd9Sstevel@tonic-gate xywarn(dt_errtag_t tag, const char *format, ...)
51217c478bd9Sstevel@tonic-gate {
51227c478bd9Sstevel@tonic-gate 	va_list ap;
51237c478bd9Sstevel@tonic-gate 
51247c478bd9Sstevel@tonic-gate 	va_start(ap, format);
51257c478bd9Sstevel@tonic-gate 	xyvwarn(tag, format, ap);
51267c478bd9Sstevel@tonic-gate 	va_end(ap);
51277c478bd9Sstevel@tonic-gate }
51287c478bd9Sstevel@tonic-gate 
51297c478bd9Sstevel@tonic-gate void
xyvwarn(dt_errtag_t tag,const char * format,va_list ap)51307c478bd9Sstevel@tonic-gate xyvwarn(dt_errtag_t tag, const char *format, va_list ap)
51317c478bd9Sstevel@tonic-gate {
51327c478bd9Sstevel@tonic-gate 	if (yypcb == NULL)
51337c478bd9Sstevel@tonic-gate 		return; /* compiler is not currently active: act as a no-op */
51347c478bd9Sstevel@tonic-gate 
51357c478bd9Sstevel@tonic-gate 	dt_set_errmsg(yypcb->pcb_hdl, dt_errtag(tag), yypcb->pcb_region,
51367c478bd9Sstevel@tonic-gate 	    yypcb->pcb_filetag, yypcb->pcb_fileptr ? yylineno : 0, format, ap);
51377c478bd9Sstevel@tonic-gate }
51387c478bd9Sstevel@tonic-gate 
51397c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
51407c478bd9Sstevel@tonic-gate void
yyerror(const char * format,...)51417c478bd9Sstevel@tonic-gate yyerror(const char *format, ...)
51427c478bd9Sstevel@tonic-gate {
51437c478bd9Sstevel@tonic-gate 	va_list ap;
51447c478bd9Sstevel@tonic-gate 
51457c478bd9Sstevel@tonic-gate 	va_start(ap, format);
51467c478bd9Sstevel@tonic-gate 	yyvwarn(format, ap);
51477c478bd9Sstevel@tonic-gate 	va_end(ap);
51487c478bd9Sstevel@tonic-gate 
51497c478bd9Sstevel@tonic-gate 	longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
51507c478bd9Sstevel@tonic-gate }
51517c478bd9Sstevel@tonic-gate 
51527c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
51537c478bd9Sstevel@tonic-gate void
yywarn(const char * format,...)51547c478bd9Sstevel@tonic-gate yywarn(const char *format, ...)
51557c478bd9Sstevel@tonic-gate {
51567c478bd9Sstevel@tonic-gate 	va_list ap;
51577c478bd9Sstevel@tonic-gate 
51587c478bd9Sstevel@tonic-gate 	va_start(ap, format);
51597c478bd9Sstevel@tonic-gate 	yyvwarn(format, ap);
51607c478bd9Sstevel@tonic-gate 	va_end(ap);
51617c478bd9Sstevel@tonic-gate }
51627c478bd9Sstevel@tonic-gate 
51637c478bd9Sstevel@tonic-gate void
yyvwarn(const char * format,va_list ap)51647c478bd9Sstevel@tonic-gate yyvwarn(const char *format, va_list ap)
51657c478bd9Sstevel@tonic-gate {
51667c478bd9Sstevel@tonic-gate 	if (yypcb == NULL)
51677c478bd9Sstevel@tonic-gate 		return; /* compiler is not currently active: act as a no-op */
51687c478bd9Sstevel@tonic-gate 
51697c478bd9Sstevel@tonic-gate 	dt_set_errmsg(yypcb->pcb_hdl, dt_errtag(D_SYNTAX), yypcb->pcb_region,
51707c478bd9Sstevel@tonic-gate 	    yypcb->pcb_filetag, yypcb->pcb_fileptr ? yylineno : 0, format, ap);
51717c478bd9Sstevel@tonic-gate 
51727c478bd9Sstevel@tonic-gate 	if (strchr(format, '\n') == NULL) {
51737c478bd9Sstevel@tonic-gate 		dtrace_hdl_t *dtp = yypcb->pcb_hdl;
51747c478bd9Sstevel@tonic-gate 		size_t len = strlen(dtp->dt_errmsg);
51757c478bd9Sstevel@tonic-gate 		char *p, *s = dtp->dt_errmsg + len;
51767c478bd9Sstevel@tonic-gate 		size_t n = sizeof (dtp->dt_errmsg) - len;
51777c478bd9Sstevel@tonic-gate 
51787c478bd9Sstevel@tonic-gate 		if (yytext[0] == '\0')
51797c478bd9Sstevel@tonic-gate 			(void) snprintf(s, n, " near end of input");
51807c478bd9Sstevel@tonic-gate 		else if (yytext[0] == '\n')
51817c478bd9Sstevel@tonic-gate 			(void) snprintf(s, n, " near end of line");
51827c478bd9Sstevel@tonic-gate 		else {
51837c478bd9Sstevel@tonic-gate 			if ((p = strchr(yytext, '\n')) != NULL)
51847c478bd9Sstevel@tonic-gate 				*p = '\0'; /* crop at newline */
51857c478bd9Sstevel@tonic-gate 			(void) snprintf(s, n, " near \"%s\"", yytext);
51867c478bd9Sstevel@tonic-gate 		}
51877c478bd9Sstevel@tonic-gate 	}
51887c478bd9Sstevel@tonic-gate }
51897c478bd9Sstevel@tonic-gate 
51907c478bd9Sstevel@tonic-gate void
yylabel(const char * label)51917c478bd9Sstevel@tonic-gate yylabel(const char *label)
51927c478bd9Sstevel@tonic-gate {
51937c478bd9Sstevel@tonic-gate 	dt_dprintf("set label to <%s>\n", label ? label : "NULL");
51947c478bd9Sstevel@tonic-gate 	yypcb->pcb_region = label;
51957c478bd9Sstevel@tonic-gate }
51967c478bd9Sstevel@tonic-gate 
51977c478bd9Sstevel@tonic-gate int
yywrap(void)51987c478bd9Sstevel@tonic-gate yywrap(void)
51997c478bd9Sstevel@tonic-gate {
52007c478bd9Sstevel@tonic-gate 	return (1); /* indicate that lex should return a zero token for EOF */
52017c478bd9Sstevel@tonic-gate }
5202