xref: /freebsd-src/cddl/contrib/opensolaris/lib/libdtrace/common/dt_parser.c (revision 4196f227aa84448ef3cae9e99678fbe787726cf7)
16ff6d951SJohn Birrell /*
26ff6d951SJohn Birrell  * CDDL HEADER START
36ff6d951SJohn Birrell  *
46ff6d951SJohn Birrell  * The contents of this file are subject to the terms of the
56ff6d951SJohn Birrell  * Common Development and Distribution License, Version 1.0 only
66ff6d951SJohn Birrell  * (the "License").  You may not use this file except in compliance
76ff6d951SJohn Birrell  * with the License.
86ff6d951SJohn Birrell  *
96ff6d951SJohn Birrell  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
106ff6d951SJohn Birrell  * or http://www.opensolaris.org/os/licensing.
116ff6d951SJohn Birrell  * See the License for the specific language governing permissions
126ff6d951SJohn Birrell  * and limitations under the License.
136ff6d951SJohn Birrell  *
146ff6d951SJohn Birrell  * When distributing Covered Code, include this CDDL HEADER in each
156ff6d951SJohn Birrell  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
166ff6d951SJohn Birrell  * If applicable, add the following below this CDDL HEADER, with the
176ff6d951SJohn Birrell  * fields enclosed by brackets "[]" replaced with your own identifying
186ff6d951SJohn Birrell  * information: Portions Copyright [yyyy] [name of copyright owner]
196ff6d951SJohn Birrell  *
206ff6d951SJohn Birrell  * CDDL HEADER END
216ff6d951SJohn Birrell  */
226ff6d951SJohn Birrell 
236ff6d951SJohn Birrell /*
246ff6d951SJohn Birrell  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
258e648814SRui Paulo  * Copyright (c) 2013, Joyent Inc. All rights reserved.
26650f66acSMark Johnston  * Copyright (c) 2012, 2016 by Delphix. All rights reserved.
276ff6d951SJohn Birrell  */
286ff6d951SJohn Birrell 
296ff6d951SJohn Birrell #pragma ident	"%Z%%M%	%I%	%E% SMI"
306ff6d951SJohn Birrell 
316ff6d951SJohn Birrell /*
326ff6d951SJohn Birrell  * DTrace D Language Parser
336ff6d951SJohn Birrell  *
346ff6d951SJohn Birrell  * The D Parser is a lex/yacc parser consisting of the lexer dt_lex.l, the
356ff6d951SJohn Birrell  * parsing grammar dt_grammar.y, and this file, dt_parser.c, which handles
366ff6d951SJohn Birrell  * the construction of the parse tree nodes and their syntactic validation.
376ff6d951SJohn Birrell  * The parse tree is constructed of dt_node_t structures (see <dt_parser.h>)
386ff6d951SJohn Birrell  * that are built in two passes: (1) the "create" pass, where the parse tree
396ff6d951SJohn Birrell  * nodes are allocated by calls from the grammar to dt_node_*() subroutines,
406ff6d951SJohn Birrell  * and (2) the "cook" pass, where nodes are coalesced, assigned D types, and
416ff6d951SJohn Birrell  * validated according to the syntactic rules of the language.
426ff6d951SJohn Birrell  *
436ff6d951SJohn Birrell  * All node allocations are performed using dt_node_alloc().  All node frees
446ff6d951SJohn Birrell  * during the parsing phase are performed by dt_node_free(), which frees node-
456ff6d951SJohn Birrell  * internal state but does not actually free the nodes.  All final node frees
466ff6d951SJohn Birrell  * are done as part of the end of dt_compile() or as part of destroying
476ff6d951SJohn Birrell  * persistent identifiers or translators which have embedded nodes.
486ff6d951SJohn Birrell  *
496ff6d951SJohn Birrell  * The dt_node_* routines that implement pass (1) may allocate new nodes.  The
506ff6d951SJohn Birrell  * dt_cook_* routines that implement pass (2) may *not* allocate new nodes.
516ff6d951SJohn Birrell  * They may free existing nodes using dt_node_free(), but they may not actually
526ff6d951SJohn Birrell  * deallocate any dt_node_t's.  Currently dt_cook_op2() is an exception to this
536ff6d951SJohn Birrell  * rule: see the comments therein for how this issue is resolved.
546ff6d951SJohn Birrell  *
556ff6d951SJohn Birrell  * The dt_cook_* routines are responsible for (at minimum) setting the final
566ff6d951SJohn Birrell  * node type (dn_ctfp/dn_type) and attributes (dn_attr).  If dn_ctfp/dn_type
576ff6d951SJohn Birrell  * are set manually (i.e. not by one of the type assignment functions), then
586ff6d951SJohn Birrell  * the DT_NF_COOKED flag must be set manually on the node.
596ff6d951SJohn Birrell  *
606ff6d951SJohn Birrell  * The cooking pass can be applied to the same parse tree more than once (used
616ff6d951SJohn Birrell  * in the case of a comma-separated list of probe descriptions).  As such, the
626ff6d951SJohn Birrell  * cook routines must not perform any parse tree transformations which would
636ff6d951SJohn Birrell  * be invalid if the tree were subsequently cooked using a different context.
646ff6d951SJohn Birrell  *
656ff6d951SJohn Birrell  * The dn_ctfp and dn_type fields form the type of the node.  This tuple can
666ff6d951SJohn Birrell  * take on the following set of values, which form our type invariants:
676ff6d951SJohn Birrell  *
686ff6d951SJohn Birrell  * 1. dn_ctfp = NULL, dn_type = CTF_ERR
696ff6d951SJohn Birrell  *
706ff6d951SJohn Birrell  *    In this state, the node has unknown type and is not yet cooked.  The
716ff6d951SJohn Birrell  *    DT_NF_COOKED flag is not yet set on the node.
726ff6d951SJohn Birrell  *
736ff6d951SJohn Birrell  * 2. dn_ctfp = DT_DYN_CTFP(dtp), dn_type = DT_DYN_TYPE(dtp)
746ff6d951SJohn Birrell  *
756ff6d951SJohn Birrell  *    In this state, the node is a dynamic D type.  This means that generic
766ff6d951SJohn Birrell  *    operations are not valid on this node and only code that knows how to
776ff6d951SJohn Birrell  *    examine the inner details of the node can operate on it.  A <DYN> node
786ff6d951SJohn Birrell  *    must have dn_ident set to point to an identifier describing the object
796ff6d951SJohn Birrell  *    and its type.  The DT_NF_REF flag is set for all nodes of type <DYN>.
806ff6d951SJohn Birrell  *    At present, the D compiler uses the <DYN> type for:
816ff6d951SJohn Birrell  *
826ff6d951SJohn Birrell  *    - associative arrays that do not yet have a value type defined
836ff6d951SJohn Birrell  *    - translated data (i.e. the result of the xlate operator)
846ff6d951SJohn Birrell  *    - aggregations
856ff6d951SJohn Birrell  *
866ff6d951SJohn Birrell  * 3. dn_ctfp = DT_STR_CTFP(dtp), dn_type = DT_STR_TYPE(dtp)
876ff6d951SJohn Birrell  *
886ff6d951SJohn Birrell  *    In this state, the node is of type D string.  The string type is really
896ff6d951SJohn Birrell  *    a char[0] typedef, but requires special handling throughout the compiler.
906ff6d951SJohn Birrell  *
916ff6d951SJohn Birrell  * 4. dn_ctfp != NULL, dn_type = any other type ID
926ff6d951SJohn Birrell  *
936ff6d951SJohn Birrell  *    In this state, the node is of some known D/CTF type.  The normal libctf
946ff6d951SJohn Birrell  *    APIs can be used to learn more about the type name or structure.  When
956ff6d951SJohn Birrell  *    the type is assigned, the DT_NF_SIGNED, DT_NF_REF, and DT_NF_BITFIELD
966ff6d951SJohn Birrell  *    flags cache the corresponding attributes of the underlying CTF type.
976ff6d951SJohn Birrell  */
986ff6d951SJohn Birrell 
996ff6d951SJohn Birrell #include <sys/param.h>
100a98ff317SPedro F. Giffuni #include <sys/sysmacros.h>
1016ff6d951SJohn Birrell #include <limits.h>
1026ff6d951SJohn Birrell #include <setjmp.h>
1036ff6d951SJohn Birrell #include <strings.h>
1046ff6d951SJohn Birrell #include <assert.h>
105bc96366cSSteven Hartland #ifdef illumos
1066ff6d951SJohn Birrell #include <alloca.h>
10784098f48SJohn Birrell #endif
1086ff6d951SJohn Birrell #include <stdlib.h>
1096ff6d951SJohn Birrell #include <stdarg.h>
1106ff6d951SJohn Birrell #include <stdio.h>
1116ff6d951SJohn Birrell #include <errno.h>
1126ff6d951SJohn Birrell #include <ctype.h>
1136ff6d951SJohn Birrell 
1146ff6d951SJohn Birrell #include <dt_impl.h>
1156ff6d951SJohn Birrell #include <dt_grammar.h>
1166ff6d951SJohn Birrell #include <dt_module.h>
1176ff6d951SJohn Birrell #include <dt_provider.h>
1186ff6d951SJohn Birrell #include <dt_string.h>
1196ff6d951SJohn Birrell #include <dt_as.h>
1206ff6d951SJohn Birrell 
1216ff6d951SJohn Birrell dt_pcb_t *yypcb;	/* current control block for parser */
1226ff6d951SJohn Birrell dt_node_t *yypragma;	/* lex token list for control lines */
1236ff6d951SJohn Birrell char yyintprefix;	/* int token macro prefix (+/-) */
1246ff6d951SJohn Birrell char yyintsuffix[4];	/* int token suffix string [uU][lL] */
1256ff6d951SJohn Birrell int yyintdecimal;	/* int token format flag (1=decimal, 0=octal/hex) */
1266ff6d951SJohn Birrell 
1276ff6d951SJohn Birrell static const char *
1286ff6d951SJohn Birrell opstr(int op)
1296ff6d951SJohn Birrell {
1306ff6d951SJohn Birrell 	switch (op) {
1316ff6d951SJohn Birrell 	case DT_TOK_COMMA:	return (",");
1326ff6d951SJohn Birrell 	case DT_TOK_ELLIPSIS:	return ("...");
1336ff6d951SJohn Birrell 	case DT_TOK_ASGN:	return ("=");
1346ff6d951SJohn Birrell 	case DT_TOK_ADD_EQ:	return ("+=");
1356ff6d951SJohn Birrell 	case DT_TOK_SUB_EQ:	return ("-=");
1366ff6d951SJohn Birrell 	case DT_TOK_MUL_EQ:	return ("*=");
1376ff6d951SJohn Birrell 	case DT_TOK_DIV_EQ:	return ("/=");
1386ff6d951SJohn Birrell 	case DT_TOK_MOD_EQ:	return ("%=");
1396ff6d951SJohn Birrell 	case DT_TOK_AND_EQ:	return ("&=");
1406ff6d951SJohn Birrell 	case DT_TOK_XOR_EQ:	return ("^=");
1416ff6d951SJohn Birrell 	case DT_TOK_OR_EQ:	return ("|=");
1426ff6d951SJohn Birrell 	case DT_TOK_LSH_EQ:	return ("<<=");
1436ff6d951SJohn Birrell 	case DT_TOK_RSH_EQ:	return (">>=");
1446ff6d951SJohn Birrell 	case DT_TOK_QUESTION:	return ("?");
1456ff6d951SJohn Birrell 	case DT_TOK_COLON:	return (":");
1466ff6d951SJohn Birrell 	case DT_TOK_LOR:	return ("||");
1476ff6d951SJohn Birrell 	case DT_TOK_LXOR:	return ("^^");
1486ff6d951SJohn Birrell 	case DT_TOK_LAND:	return ("&&");
1496ff6d951SJohn Birrell 	case DT_TOK_BOR:	return ("|");
1506ff6d951SJohn Birrell 	case DT_TOK_XOR:	return ("^");
1516ff6d951SJohn Birrell 	case DT_TOK_BAND:	return ("&");
1526ff6d951SJohn Birrell 	case DT_TOK_EQU:	return ("==");
1536ff6d951SJohn Birrell 	case DT_TOK_NEQ:	return ("!=");
1546ff6d951SJohn Birrell 	case DT_TOK_LT:		return ("<");
1556ff6d951SJohn Birrell 	case DT_TOK_LE:		return ("<=");
1566ff6d951SJohn Birrell 	case DT_TOK_GT:		return (">");
1576ff6d951SJohn Birrell 	case DT_TOK_GE:		return (">=");
1586ff6d951SJohn Birrell 	case DT_TOK_LSH:	return ("<<");
1596ff6d951SJohn Birrell 	case DT_TOK_RSH:	return (">>");
1606ff6d951SJohn Birrell 	case DT_TOK_ADD:	return ("+");
1616ff6d951SJohn Birrell 	case DT_TOK_SUB:	return ("-");
1626ff6d951SJohn Birrell 	case DT_TOK_MUL:	return ("*");
1636ff6d951SJohn Birrell 	case DT_TOK_DIV:	return ("/");
1646ff6d951SJohn Birrell 	case DT_TOK_MOD:	return ("%");
1656ff6d951SJohn Birrell 	case DT_TOK_LNEG:	return ("!");
1666ff6d951SJohn Birrell 	case DT_TOK_BNEG:	return ("~");
1676ff6d951SJohn Birrell 	case DT_TOK_ADDADD:	return ("++");
1686ff6d951SJohn Birrell 	case DT_TOK_PREINC:	return ("++");
1696ff6d951SJohn Birrell 	case DT_TOK_POSTINC:	return ("++");
1706ff6d951SJohn Birrell 	case DT_TOK_SUBSUB:	return ("--");
1716ff6d951SJohn Birrell 	case DT_TOK_PREDEC:	return ("--");
1726ff6d951SJohn Birrell 	case DT_TOK_POSTDEC:	return ("--");
1736ff6d951SJohn Birrell 	case DT_TOK_IPOS:	return ("+");
1746ff6d951SJohn Birrell 	case DT_TOK_INEG:	return ("-");
1756ff6d951SJohn Birrell 	case DT_TOK_DEREF:	return ("*");
1766ff6d951SJohn Birrell 	case DT_TOK_ADDROF:	return ("&");
1776ff6d951SJohn Birrell 	case DT_TOK_OFFSETOF:	return ("offsetof");
1786ff6d951SJohn Birrell 	case DT_TOK_SIZEOF:	return ("sizeof");
1796ff6d951SJohn Birrell 	case DT_TOK_STRINGOF:	return ("stringof");
1806ff6d951SJohn Birrell 	case DT_TOK_XLATE:	return ("xlate");
1816ff6d951SJohn Birrell 	case DT_TOK_LPAR:	return ("(");
1826ff6d951SJohn Birrell 	case DT_TOK_RPAR:	return (")");
1836ff6d951SJohn Birrell 	case DT_TOK_LBRAC:	return ("[");
1846ff6d951SJohn Birrell 	case DT_TOK_RBRAC:	return ("]");
1856ff6d951SJohn Birrell 	case DT_TOK_PTR:	return ("->");
1866ff6d951SJohn Birrell 	case DT_TOK_DOT:	return (".");
1876ff6d951SJohn Birrell 	case DT_TOK_STRING:	return ("<string>");
1886ff6d951SJohn Birrell 	case DT_TOK_IDENT:	return ("<ident>");
1896ff6d951SJohn Birrell 	case DT_TOK_TNAME:	return ("<type>");
1906ff6d951SJohn Birrell 	case DT_TOK_INT:	return ("<int>");
1916ff6d951SJohn Birrell 	default:		return ("<?>");
1926ff6d951SJohn Birrell 	}
1936ff6d951SJohn Birrell }
1946ff6d951SJohn Birrell 
1956ff6d951SJohn Birrell int
1966ff6d951SJohn Birrell dt_type_lookup(const char *s, dtrace_typeinfo_t *tip)
1976ff6d951SJohn Birrell {
1986ff6d951SJohn Birrell 	static const char delimiters[] = " \t\n\r\v\f*`";
1996ff6d951SJohn Birrell 	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
2008e648814SRui Paulo 	const char *p, *q, *r, *end, *obj;
2016ff6d951SJohn Birrell 
2026ff6d951SJohn Birrell 	for (p = s, end = s + strlen(s); *p != '\0'; p = q) {
2036ff6d951SJohn Birrell 		while (isspace(*p))
2046ff6d951SJohn Birrell 			p++;	/* skip leading whitespace prior to token */
2056ff6d951SJohn Birrell 
2066ff6d951SJohn Birrell 		if (p == end || (q = strpbrk(p + 1, delimiters)) == NULL)
2076ff6d951SJohn Birrell 			break;	/* empty string or single token remaining */
2086ff6d951SJohn Birrell 
2096ff6d951SJohn Birrell 		if (*q == '`') {
2106ff6d951SJohn Birrell 			char *object = alloca((size_t)(q - p) + 1);
2116ff6d951SJohn Birrell 			char *type = alloca((size_t)(end - s) + 1);
2126ff6d951SJohn Birrell 
2136ff6d951SJohn Birrell 			/*
2146ff6d951SJohn Birrell 			 * Copy from the start of the token (p) to the location
2156ff6d951SJohn Birrell 			 * backquote (q) to extract the nul-terminated object.
2166ff6d951SJohn Birrell 			 */
2176ff6d951SJohn Birrell 			bcopy(p, object, (size_t)(q - p));
2186ff6d951SJohn Birrell 			object[(size_t)(q - p)] = '\0';
2196ff6d951SJohn Birrell 
2206ff6d951SJohn Birrell 			/*
2216ff6d951SJohn Birrell 			 * Copy the original string up to the start of this
2226ff6d951SJohn Birrell 			 * token (p) into type, and then concatenate everything
2236ff6d951SJohn Birrell 			 * after q.  This is the type name without the object.
2246ff6d951SJohn Birrell 			 */
2256ff6d951SJohn Birrell 			bcopy(s, type, (size_t)(p - s));
2266ff6d951SJohn Birrell 			bcopy(q + 1, type + (size_t)(p - s), strlen(q + 1) + 1);
2276ff6d951SJohn Birrell 
2288e648814SRui Paulo 			/*
2298e648814SRui Paulo 			 * There may be at most three delimeters. The second
2308e648814SRui Paulo 			 * delimeter is usually used to distinguish the type
2318e648814SRui Paulo 			 * within a given module, however, there could be a link
2328e648814SRui Paulo 			 * map id on the scene in which case that delimeter
2338e648814SRui Paulo 			 * would be the third. We determine presence of the lmid
2348e648814SRui Paulo 			 * if it rouglhly meets the from LM[0-9]
2358e648814SRui Paulo 			 */
2368e648814SRui Paulo 			if ((r = strchr(q + 1, '`')) != NULL &&
2378e648814SRui Paulo 			    ((r = strchr(r + 1, '`')) != NULL)) {
2388e648814SRui Paulo 				if (strchr(r + 1, '`') != NULL)
2398e648814SRui Paulo 					return (dt_set_errno(dtp,
2408e648814SRui Paulo 					    EDT_BADSCOPE));
2418e648814SRui Paulo 				if (q[1] != 'L' || q[2] != 'M')
2428e648814SRui Paulo 					return (dt_set_errno(dtp,
2438e648814SRui Paulo 					    EDT_BADSCOPE));
2448e648814SRui Paulo 			}
2456ff6d951SJohn Birrell 
2466ff6d951SJohn Birrell 			return (dtrace_lookup_by_type(dtp, object, type, tip));
2476ff6d951SJohn Birrell 		}
2486ff6d951SJohn Birrell 	}
2496ff6d951SJohn Birrell 
2506ff6d951SJohn Birrell 	if (yypcb->pcb_idepth != 0)
2516ff6d951SJohn Birrell 		obj = DTRACE_OBJ_CDEFS;
2526ff6d951SJohn Birrell 	else
2536ff6d951SJohn Birrell 		obj = DTRACE_OBJ_EVERY;
2546ff6d951SJohn Birrell 
2556ff6d951SJohn Birrell 	return (dtrace_lookup_by_type(dtp, obj, s, tip));
2566ff6d951SJohn Birrell }
2576ff6d951SJohn Birrell 
2586ff6d951SJohn Birrell /*
2596ff6d951SJohn Birrell  * When we parse type expressions or parse an expression with unary "&", we
2606ff6d951SJohn Birrell  * need to find a type that is a pointer to a previously known type.
2616ff6d951SJohn Birrell  * Unfortunately CTF is limited to a per-container view, so ctf_type_pointer()
2626ff6d951SJohn Birrell  * alone does not suffice for our needs.  We provide a more intelligent wrapper
2636ff6d951SJohn Birrell  * for the compiler that attempts to compute a pointer to either the given type
2646ff6d951SJohn Birrell  * or its base (that is, we try both "foo_t *" and "struct foo *"), and also
2656ff6d951SJohn Birrell  * to potentially construct the required type on-the-fly.
2666ff6d951SJohn Birrell  */
2676ff6d951SJohn Birrell int
2686ff6d951SJohn Birrell dt_type_pointer(dtrace_typeinfo_t *tip)
2696ff6d951SJohn Birrell {
2706ff6d951SJohn Birrell 	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
2716ff6d951SJohn Birrell 	ctf_file_t *ctfp = tip->dtt_ctfp;
2726ff6d951SJohn Birrell 	ctf_id_t type = tip->dtt_type;
2736ff6d951SJohn Birrell 	ctf_id_t base = ctf_type_resolve(ctfp, type);
2748e648814SRui Paulo 	uint_t bflags = tip->dtt_flags;
2756ff6d951SJohn Birrell 
2766ff6d951SJohn Birrell 	dt_module_t *dmp;
2776ff6d951SJohn Birrell 	ctf_id_t ptr;
2786ff6d951SJohn Birrell 
2796ff6d951SJohn Birrell 	if ((ptr = ctf_type_pointer(ctfp, type)) != CTF_ERR ||
2806ff6d951SJohn Birrell 	    (ptr = ctf_type_pointer(ctfp, base)) != CTF_ERR) {
2816ff6d951SJohn Birrell 		tip->dtt_type = ptr;
2826ff6d951SJohn Birrell 		return (0);
2836ff6d951SJohn Birrell 	}
2846ff6d951SJohn Birrell 
2856ff6d951SJohn Birrell 	if (yypcb->pcb_idepth != 0)
2866ff6d951SJohn Birrell 		dmp = dtp->dt_cdefs;
2876ff6d951SJohn Birrell 	else
2886ff6d951SJohn Birrell 		dmp = dtp->dt_ddefs;
2896ff6d951SJohn Birrell 
2906ff6d951SJohn Birrell 	if (ctfp != dmp->dm_ctfp && ctfp != ctf_parent_file(dmp->dm_ctfp) &&
2916ff6d951SJohn Birrell 	    (type = ctf_add_type(dmp->dm_ctfp, ctfp, type)) == CTF_ERR) {
2926ff6d951SJohn Birrell 		dtp->dt_ctferr = ctf_errno(dmp->dm_ctfp);
2936ff6d951SJohn Birrell 		return (dt_set_errno(dtp, EDT_CTF));
2946ff6d951SJohn Birrell 	}
2956ff6d951SJohn Birrell 
2966ff6d951SJohn Birrell 	ptr = ctf_add_pointer(dmp->dm_ctfp, CTF_ADD_ROOT, type);
2976ff6d951SJohn Birrell 
2986ff6d951SJohn Birrell 	if (ptr == CTF_ERR || ctf_update(dmp->dm_ctfp) == CTF_ERR) {
2996ff6d951SJohn Birrell 		dtp->dt_ctferr = ctf_errno(dmp->dm_ctfp);
3006ff6d951SJohn Birrell 		return (dt_set_errno(dtp, EDT_CTF));
3016ff6d951SJohn Birrell 	}
3026ff6d951SJohn Birrell 
3036ff6d951SJohn Birrell 	tip->dtt_object = dmp->dm_name;
3046ff6d951SJohn Birrell 	tip->dtt_ctfp = dmp->dm_ctfp;
3056ff6d951SJohn Birrell 	tip->dtt_type = ptr;
3068e648814SRui Paulo 	tip->dtt_flags = bflags;
3076ff6d951SJohn Birrell 
3086ff6d951SJohn Birrell 	return (0);
3096ff6d951SJohn Birrell }
3106ff6d951SJohn Birrell 
3116ff6d951SJohn Birrell const char *
3126ff6d951SJohn Birrell dt_type_name(ctf_file_t *ctfp, ctf_id_t type, char *buf, size_t len)
3136ff6d951SJohn Birrell {
3146ff6d951SJohn Birrell 	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
3156ff6d951SJohn Birrell 
3166ff6d951SJohn Birrell 	if (ctfp == DT_FPTR_CTFP(dtp) && type == DT_FPTR_TYPE(dtp))
3176ff6d951SJohn Birrell 		(void) snprintf(buf, len, "function pointer");
3186ff6d951SJohn Birrell 	else if (ctfp == DT_FUNC_CTFP(dtp) && type == DT_FUNC_TYPE(dtp))
3196ff6d951SJohn Birrell 		(void) snprintf(buf, len, "function");
3206ff6d951SJohn Birrell 	else if (ctfp == DT_DYN_CTFP(dtp) && type == DT_DYN_TYPE(dtp))
3216ff6d951SJohn Birrell 		(void) snprintf(buf, len, "dynamic variable");
3226ff6d951SJohn Birrell 	else if (ctfp == NULL)
3236ff6d951SJohn Birrell 		(void) snprintf(buf, len, "<none>");
3246ff6d951SJohn Birrell 	else if (ctf_type_name(ctfp, type, buf, len) == NULL)
3256ff6d951SJohn Birrell 		(void) snprintf(buf, len, "unknown");
3266ff6d951SJohn Birrell 
3276ff6d951SJohn Birrell 	return (buf);
3286ff6d951SJohn Birrell }
3296ff6d951SJohn Birrell 
3306ff6d951SJohn Birrell /*
3316ff6d951SJohn Birrell  * Perform the "usual arithmetic conversions" to determine which of the two
3326ff6d951SJohn Birrell  * input operand types should be promoted and used as a result type.  The
3336ff6d951SJohn Birrell  * rules for this are described in ISOC[6.3.1.8] and K&R[A6.5].
3346ff6d951SJohn Birrell  */
3356ff6d951SJohn Birrell static void
3366ff6d951SJohn Birrell dt_type_promote(dt_node_t *lp, dt_node_t *rp, ctf_file_t **ofp, ctf_id_t *otype)
3376ff6d951SJohn Birrell {
3386ff6d951SJohn Birrell 	ctf_file_t *lfp = lp->dn_ctfp;
3396ff6d951SJohn Birrell 	ctf_id_t ltype = lp->dn_type;
3406ff6d951SJohn Birrell 
3416ff6d951SJohn Birrell 	ctf_file_t *rfp = rp->dn_ctfp;
3426ff6d951SJohn Birrell 	ctf_id_t rtype = rp->dn_type;
3436ff6d951SJohn Birrell 
3446ff6d951SJohn Birrell 	ctf_id_t lbase = ctf_type_resolve(lfp, ltype);
3456ff6d951SJohn Birrell 	uint_t lkind = ctf_type_kind(lfp, lbase);
3466ff6d951SJohn Birrell 
3476ff6d951SJohn Birrell 	ctf_id_t rbase = ctf_type_resolve(rfp, rtype);
3486ff6d951SJohn Birrell 	uint_t rkind = ctf_type_kind(rfp, rbase);
3496ff6d951SJohn Birrell 
3506ff6d951SJohn Birrell 	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
3516ff6d951SJohn Birrell 	ctf_encoding_t le, re;
3526ff6d951SJohn Birrell 	uint_t lrank, rrank;
3536ff6d951SJohn Birrell 
3546ff6d951SJohn Birrell 	assert(lkind == CTF_K_INTEGER || lkind == CTF_K_ENUM);
3556ff6d951SJohn Birrell 	assert(rkind == CTF_K_INTEGER || rkind == CTF_K_ENUM);
3566ff6d951SJohn Birrell 
3576ff6d951SJohn Birrell 	if (lkind == CTF_K_ENUM) {
3586ff6d951SJohn Birrell 		lfp = DT_INT_CTFP(dtp);
3596ff6d951SJohn Birrell 		ltype = lbase = DT_INT_TYPE(dtp);
3606ff6d951SJohn Birrell 	}
3616ff6d951SJohn Birrell 
3626ff6d951SJohn Birrell 	if (rkind == CTF_K_ENUM) {
3636ff6d951SJohn Birrell 		rfp = DT_INT_CTFP(dtp);
3646ff6d951SJohn Birrell 		rtype = rbase = DT_INT_TYPE(dtp);
3656ff6d951SJohn Birrell 	}
3666ff6d951SJohn Birrell 
3676ff6d951SJohn Birrell 	if (ctf_type_encoding(lfp, lbase, &le) == CTF_ERR) {
3686ff6d951SJohn Birrell 		yypcb->pcb_hdl->dt_ctferr = ctf_errno(lfp);
3696ff6d951SJohn Birrell 		longjmp(yypcb->pcb_jmpbuf, EDT_CTF);
3706ff6d951SJohn Birrell 	}
3716ff6d951SJohn Birrell 
3726ff6d951SJohn Birrell 	if (ctf_type_encoding(rfp, rbase, &re) == CTF_ERR) {
3736ff6d951SJohn Birrell 		yypcb->pcb_hdl->dt_ctferr = ctf_errno(rfp);
3746ff6d951SJohn Birrell 		longjmp(yypcb->pcb_jmpbuf, EDT_CTF);
3756ff6d951SJohn Birrell 	}
3766ff6d951SJohn Birrell 
3776ff6d951SJohn Birrell 	/*
3786ff6d951SJohn Birrell 	 * Compute an integer rank based on the size and unsigned status.
3796ff6d951SJohn Birrell 	 * If rank is identical, pick the "larger" of the equivalent types
3806ff6d951SJohn Birrell 	 * which we define as having a larger base ctf_id_t.  If rank is
3816ff6d951SJohn Birrell 	 * different, pick the type with the greater rank.
3826ff6d951SJohn Birrell 	 */
3836ff6d951SJohn Birrell 	lrank = le.cte_bits + ((le.cte_format & CTF_INT_SIGNED) == 0);
3846ff6d951SJohn Birrell 	rrank = re.cte_bits + ((re.cte_format & CTF_INT_SIGNED) == 0);
3856ff6d951SJohn Birrell 
3866ff6d951SJohn Birrell 	if (lrank == rrank) {
3876ff6d951SJohn Birrell 		if (lbase - rbase < 0)
3886ff6d951SJohn Birrell 			goto return_rtype;
3896ff6d951SJohn Birrell 		else
3906ff6d951SJohn Birrell 			goto return_ltype;
3916ff6d951SJohn Birrell 	} else if (lrank > rrank) {
3926ff6d951SJohn Birrell 		goto return_ltype;
3936ff6d951SJohn Birrell 	} else
3946ff6d951SJohn Birrell 		goto return_rtype;
3956ff6d951SJohn Birrell 
3966ff6d951SJohn Birrell return_ltype:
3976ff6d951SJohn Birrell 	*ofp = lfp;
3986ff6d951SJohn Birrell 	*otype = ltype;
3996ff6d951SJohn Birrell 	return;
4006ff6d951SJohn Birrell 
4016ff6d951SJohn Birrell return_rtype:
4026ff6d951SJohn Birrell 	*ofp = rfp;
4036ff6d951SJohn Birrell 	*otype = rtype;
4046ff6d951SJohn Birrell }
4056ff6d951SJohn Birrell 
4066ff6d951SJohn Birrell void
4076ff6d951SJohn Birrell dt_node_promote(dt_node_t *lp, dt_node_t *rp, dt_node_t *dnp)
4086ff6d951SJohn Birrell {
4096ff6d951SJohn Birrell 	dt_type_promote(lp, rp, &dnp->dn_ctfp, &dnp->dn_type);
4108e648814SRui Paulo 	dt_node_type_assign(dnp, dnp->dn_ctfp, dnp->dn_type, B_FALSE);
4116ff6d951SJohn Birrell 	dt_node_attr_assign(dnp, dt_attr_min(lp->dn_attr, rp->dn_attr));
4126ff6d951SJohn Birrell }
4136ff6d951SJohn Birrell 
4146ff6d951SJohn Birrell const char *
4156ff6d951SJohn Birrell dt_node_name(const dt_node_t *dnp, char *buf, size_t len)
4166ff6d951SJohn Birrell {
4176ff6d951SJohn Birrell 	char n1[DT_TYPE_NAMELEN];
4186ff6d951SJohn Birrell 	char n2[DT_TYPE_NAMELEN];
4196ff6d951SJohn Birrell 
4206ff6d951SJohn Birrell 	const char *prefix = "", *suffix = "";
4216ff6d951SJohn Birrell 	const dtrace_syminfo_t *dts;
4226ff6d951SJohn Birrell 	char *s;
4236ff6d951SJohn Birrell 
4246ff6d951SJohn Birrell 	switch (dnp->dn_kind) {
4256ff6d951SJohn Birrell 	case DT_NODE_INT:
4266ff6d951SJohn Birrell 		(void) snprintf(buf, len, "integer constant 0x%llx",
4276ff6d951SJohn Birrell 		    (u_longlong_t)dnp->dn_value);
4286ff6d951SJohn Birrell 		break;
4296ff6d951SJohn Birrell 	case DT_NODE_STRING:
4306ff6d951SJohn Birrell 		s = strchr2esc(dnp->dn_string, strlen(dnp->dn_string));
4316ff6d951SJohn Birrell 		(void) snprintf(buf, len, "string constant \"%s\"",
4326ff6d951SJohn Birrell 		    s != NULL ? s : dnp->dn_string);
4336ff6d951SJohn Birrell 		free(s);
4346ff6d951SJohn Birrell 		break;
4356ff6d951SJohn Birrell 	case DT_NODE_IDENT:
4366ff6d951SJohn Birrell 		(void) snprintf(buf, len, "identifier %s", dnp->dn_string);
4376ff6d951SJohn Birrell 		break;
4386ff6d951SJohn Birrell 	case DT_NODE_VAR:
4396ff6d951SJohn Birrell 	case DT_NODE_FUNC:
4406ff6d951SJohn Birrell 	case DT_NODE_AGG:
4416ff6d951SJohn Birrell 	case DT_NODE_INLINE:
4426ff6d951SJohn Birrell 		switch (dnp->dn_ident->di_kind) {
4436ff6d951SJohn Birrell 		case DT_IDENT_FUNC:
4446ff6d951SJohn Birrell 		case DT_IDENT_AGGFUNC:
4456ff6d951SJohn Birrell 		case DT_IDENT_ACTFUNC:
4466ff6d951SJohn Birrell 			suffix = "( )";
4476ff6d951SJohn Birrell 			break;
4486ff6d951SJohn Birrell 		case DT_IDENT_AGG:
4496ff6d951SJohn Birrell 			prefix = "@";
4506ff6d951SJohn Birrell 			break;
4516ff6d951SJohn Birrell 		}
4526ff6d951SJohn Birrell 		(void) snprintf(buf, len, "%s %s%s%s",
4536ff6d951SJohn Birrell 		    dt_idkind_name(dnp->dn_ident->di_kind),
4546ff6d951SJohn Birrell 		    prefix, dnp->dn_ident->di_name, suffix);
4556ff6d951SJohn Birrell 		break;
4566ff6d951SJohn Birrell 	case DT_NODE_SYM:
4576ff6d951SJohn Birrell 		dts = dnp->dn_ident->di_data;
4586ff6d951SJohn Birrell 		(void) snprintf(buf, len, "symbol %s`%s",
4596ff6d951SJohn Birrell 		    dts->dts_object, dts->dts_name);
4606ff6d951SJohn Birrell 		break;
4616ff6d951SJohn Birrell 	case DT_NODE_TYPE:
4626ff6d951SJohn Birrell 		(void) snprintf(buf, len, "type %s",
4636ff6d951SJohn Birrell 		    dt_node_type_name(dnp, n1, sizeof (n1)));
4646ff6d951SJohn Birrell 		break;
4656ff6d951SJohn Birrell 	case DT_NODE_OP1:
4666ff6d951SJohn Birrell 	case DT_NODE_OP2:
4676ff6d951SJohn Birrell 	case DT_NODE_OP3:
4686ff6d951SJohn Birrell 		(void) snprintf(buf, len, "operator %s", opstr(dnp->dn_op));
4696ff6d951SJohn Birrell 		break;
4706ff6d951SJohn Birrell 	case DT_NODE_DEXPR:
4716ff6d951SJohn Birrell 	case DT_NODE_DFUNC:
4726ff6d951SJohn Birrell 		if (dnp->dn_expr)
4736ff6d951SJohn Birrell 			return (dt_node_name(dnp->dn_expr, buf, len));
4746ff6d951SJohn Birrell 		(void) snprintf(buf, len, "%s", "statement");
4756ff6d951SJohn Birrell 		break;
4766ff6d951SJohn Birrell 	case DT_NODE_PDESC:
4776ff6d951SJohn Birrell 		if (dnp->dn_desc->dtpd_id == 0) {
4786ff6d951SJohn Birrell 			(void) snprintf(buf, len,
4796ff6d951SJohn Birrell 			    "probe description %s:%s:%s:%s",
4806ff6d951SJohn Birrell 			    dnp->dn_desc->dtpd_provider, dnp->dn_desc->dtpd_mod,
4816ff6d951SJohn Birrell 			    dnp->dn_desc->dtpd_func, dnp->dn_desc->dtpd_name);
4826ff6d951SJohn Birrell 		} else {
4836ff6d951SJohn Birrell 			(void) snprintf(buf, len, "probe description %u",
4846ff6d951SJohn Birrell 			    dnp->dn_desc->dtpd_id);
4856ff6d951SJohn Birrell 		}
4866ff6d951SJohn Birrell 		break;
4876ff6d951SJohn Birrell 	case DT_NODE_CLAUSE:
4886ff6d951SJohn Birrell 		(void) snprintf(buf, len, "%s", "clause");
4896ff6d951SJohn Birrell 		break;
4906ff6d951SJohn Birrell 	case DT_NODE_MEMBER:
4916ff6d951SJohn Birrell 		(void) snprintf(buf, len, "member %s", dnp->dn_membname);
4926ff6d951SJohn Birrell 		break;
4936ff6d951SJohn Birrell 	case DT_NODE_XLATOR:
4946ff6d951SJohn Birrell 		(void) snprintf(buf, len, "translator <%s> (%s)",
4956ff6d951SJohn Birrell 		    dt_type_name(dnp->dn_xlator->dx_dst_ctfp,
4966ff6d951SJohn Birrell 			dnp->dn_xlator->dx_dst_type, n1, sizeof (n1)),
4976ff6d951SJohn Birrell 		    dt_type_name(dnp->dn_xlator->dx_src_ctfp,
4986ff6d951SJohn Birrell 			dnp->dn_xlator->dx_src_type, n2, sizeof (n2)));
4996ff6d951SJohn Birrell 		break;
5006ff6d951SJohn Birrell 	case DT_NODE_PROG:
5016ff6d951SJohn Birrell 		(void) snprintf(buf, len, "%s", "program");
5026ff6d951SJohn Birrell 		break;
5036ff6d951SJohn Birrell 	default:
5046ff6d951SJohn Birrell 		(void) snprintf(buf, len, "node <%u>", dnp->dn_kind);
5056ff6d951SJohn Birrell 		break;
5066ff6d951SJohn Birrell 	}
5076ff6d951SJohn Birrell 
5086ff6d951SJohn Birrell 	return (buf);
5096ff6d951SJohn Birrell }
5106ff6d951SJohn Birrell 
5116ff6d951SJohn Birrell /*
5126ff6d951SJohn Birrell  * dt_node_xalloc() can be used to create new parse nodes from any libdtrace
5136ff6d951SJohn Birrell  * caller.  The caller is responsible for assigning dn_link appropriately.
5146ff6d951SJohn Birrell  */
5156ff6d951SJohn Birrell dt_node_t *
5166ff6d951SJohn Birrell dt_node_xalloc(dtrace_hdl_t *dtp, int kind)
5176ff6d951SJohn Birrell {
5186ff6d951SJohn Birrell 	dt_node_t *dnp = dt_alloc(dtp, sizeof (dt_node_t));
5196ff6d951SJohn Birrell 
5206ff6d951SJohn Birrell 	if (dnp == NULL)
5216ff6d951SJohn Birrell 		return (NULL);
5226ff6d951SJohn Birrell 
5236ff6d951SJohn Birrell 	dnp->dn_ctfp = NULL;
5246ff6d951SJohn Birrell 	dnp->dn_type = CTF_ERR;
5256ff6d951SJohn Birrell 	dnp->dn_kind = (uchar_t)kind;
5266ff6d951SJohn Birrell 	dnp->dn_flags = 0;
5276ff6d951SJohn Birrell 	dnp->dn_op = 0;
5286ff6d951SJohn Birrell 	dnp->dn_line = -1;
5296ff6d951SJohn Birrell 	dnp->dn_reg = -1;
5306ff6d951SJohn Birrell 	dnp->dn_attr = _dtrace_defattr;
5316ff6d951SJohn Birrell 	dnp->dn_list = NULL;
5326ff6d951SJohn Birrell 	dnp->dn_link = NULL;
5336ff6d951SJohn Birrell 	bzero(&dnp->dn_u, sizeof (dnp->dn_u));
5346ff6d951SJohn Birrell 
5356ff6d951SJohn Birrell 	return (dnp);
5366ff6d951SJohn Birrell }
5376ff6d951SJohn Birrell 
5386ff6d951SJohn Birrell /*
5396ff6d951SJohn Birrell  * dt_node_alloc() is used to create new parse nodes from the parser.  It
5406ff6d951SJohn Birrell  * assigns the node location based on the current lexer line number and places
5416ff6d951SJohn Birrell  * the new node on the default allocation list.  If allocation fails, we
5426ff6d951SJohn Birrell  * automatically longjmp the caller back to the enclosing compilation call.
5436ff6d951SJohn Birrell  */
5446ff6d951SJohn Birrell static dt_node_t *
5456ff6d951SJohn Birrell dt_node_alloc(int kind)
5466ff6d951SJohn Birrell {
5476ff6d951SJohn Birrell 	dt_node_t *dnp = dt_node_xalloc(yypcb->pcb_hdl, kind);
5486ff6d951SJohn Birrell 
5496ff6d951SJohn Birrell 	if (dnp == NULL)
5506ff6d951SJohn Birrell 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
5516ff6d951SJohn Birrell 
5526ff6d951SJohn Birrell 	dnp->dn_line = yylineno;
5536ff6d951SJohn Birrell 	dnp->dn_link = yypcb->pcb_list;
5546ff6d951SJohn Birrell 	yypcb->pcb_list = dnp;
5556ff6d951SJohn Birrell 
5566ff6d951SJohn Birrell 	return (dnp);
5576ff6d951SJohn Birrell }
5586ff6d951SJohn Birrell 
5596ff6d951SJohn Birrell void
5606ff6d951SJohn Birrell dt_node_free(dt_node_t *dnp)
5616ff6d951SJohn Birrell {
5626ff6d951SJohn Birrell 	uchar_t kind = dnp->dn_kind;
5636ff6d951SJohn Birrell 
5646ff6d951SJohn Birrell 	dnp->dn_kind = DT_NODE_FREE;
5656ff6d951SJohn Birrell 
5666ff6d951SJohn Birrell 	switch (kind) {
5676ff6d951SJohn Birrell 	case DT_NODE_STRING:
5686ff6d951SJohn Birrell 	case DT_NODE_IDENT:
5696ff6d951SJohn Birrell 	case DT_NODE_TYPE:
5706ff6d951SJohn Birrell 		free(dnp->dn_string);
5716ff6d951SJohn Birrell 		dnp->dn_string = NULL;
5726ff6d951SJohn Birrell 		break;
5736ff6d951SJohn Birrell 
5746ff6d951SJohn Birrell 	case DT_NODE_VAR:
5756ff6d951SJohn Birrell 	case DT_NODE_FUNC:
5766ff6d951SJohn Birrell 	case DT_NODE_PROBE:
5776ff6d951SJohn Birrell 		if (dnp->dn_ident != NULL) {
5786ff6d951SJohn Birrell 			if (dnp->dn_ident->di_flags & DT_IDFLG_ORPHAN)
5796ff6d951SJohn Birrell 				dt_ident_destroy(dnp->dn_ident);
5806ff6d951SJohn Birrell 			dnp->dn_ident = NULL;
5816ff6d951SJohn Birrell 		}
5826ff6d951SJohn Birrell 		dt_node_list_free(&dnp->dn_args);
5836ff6d951SJohn Birrell 		break;
5846ff6d951SJohn Birrell 
5856ff6d951SJohn Birrell 	case DT_NODE_OP1:
5866ff6d951SJohn Birrell 		if (dnp->dn_child != NULL) {
5876ff6d951SJohn Birrell 			dt_node_free(dnp->dn_child);
5886ff6d951SJohn Birrell 			dnp->dn_child = NULL;
5896ff6d951SJohn Birrell 		}
5906ff6d951SJohn Birrell 		break;
5916ff6d951SJohn Birrell 
5926ff6d951SJohn Birrell 	case DT_NODE_OP3:
5936ff6d951SJohn Birrell 		if (dnp->dn_expr != NULL) {
5946ff6d951SJohn Birrell 			dt_node_free(dnp->dn_expr);
5956ff6d951SJohn Birrell 			dnp->dn_expr = NULL;
5966ff6d951SJohn Birrell 		}
5976ff6d951SJohn Birrell 		/*FALLTHRU*/
5986ff6d951SJohn Birrell 	case DT_NODE_OP2:
5996ff6d951SJohn Birrell 		if (dnp->dn_left != NULL) {
6006ff6d951SJohn Birrell 			dt_node_free(dnp->dn_left);
6016ff6d951SJohn Birrell 			dnp->dn_left = NULL;
6026ff6d951SJohn Birrell 		}
6036ff6d951SJohn Birrell 		if (dnp->dn_right != NULL) {
6046ff6d951SJohn Birrell 			dt_node_free(dnp->dn_right);
6056ff6d951SJohn Birrell 			dnp->dn_right = NULL;
6066ff6d951SJohn Birrell 		}
6076ff6d951SJohn Birrell 		break;
6086ff6d951SJohn Birrell 
6096ff6d951SJohn Birrell 	case DT_NODE_DEXPR:
6106ff6d951SJohn Birrell 	case DT_NODE_DFUNC:
6116ff6d951SJohn Birrell 		if (dnp->dn_expr != NULL) {
6126ff6d951SJohn Birrell 			dt_node_free(dnp->dn_expr);
6136ff6d951SJohn Birrell 			dnp->dn_expr = NULL;
6146ff6d951SJohn Birrell 		}
6156ff6d951SJohn Birrell 		break;
6166ff6d951SJohn Birrell 
6176ff6d951SJohn Birrell 	case DT_NODE_AGG:
6186ff6d951SJohn Birrell 		if (dnp->dn_aggfun != NULL) {
6196ff6d951SJohn Birrell 			dt_node_free(dnp->dn_aggfun);
6206ff6d951SJohn Birrell 			dnp->dn_aggfun = NULL;
6216ff6d951SJohn Birrell 		}
6226ff6d951SJohn Birrell 		dt_node_list_free(&dnp->dn_aggtup);
6236ff6d951SJohn Birrell 		break;
6246ff6d951SJohn Birrell 
6256ff6d951SJohn Birrell 	case DT_NODE_PDESC:
6266ff6d951SJohn Birrell 		free(dnp->dn_spec);
6276ff6d951SJohn Birrell 		dnp->dn_spec = NULL;
6286ff6d951SJohn Birrell 		free(dnp->dn_desc);
6296ff6d951SJohn Birrell 		dnp->dn_desc = NULL;
6306ff6d951SJohn Birrell 		break;
6316ff6d951SJohn Birrell 
6326ff6d951SJohn Birrell 	case DT_NODE_CLAUSE:
6336ff6d951SJohn Birrell 		if (dnp->dn_pred != NULL)
6346ff6d951SJohn Birrell 			dt_node_free(dnp->dn_pred);
6356ff6d951SJohn Birrell 		if (dnp->dn_locals != NULL)
6366ff6d951SJohn Birrell 			dt_idhash_destroy(dnp->dn_locals);
6376ff6d951SJohn Birrell 		dt_node_list_free(&dnp->dn_pdescs);
6386ff6d951SJohn Birrell 		dt_node_list_free(&dnp->dn_acts);
6396ff6d951SJohn Birrell 		break;
6406ff6d951SJohn Birrell 
6416ff6d951SJohn Birrell 	case DT_NODE_MEMBER:
6426ff6d951SJohn Birrell 		free(dnp->dn_membname);
6436ff6d951SJohn Birrell 		dnp->dn_membname = NULL;
6446ff6d951SJohn Birrell 		if (dnp->dn_membexpr != NULL) {
6456ff6d951SJohn Birrell 			dt_node_free(dnp->dn_membexpr);
6466ff6d951SJohn Birrell 			dnp->dn_membexpr = NULL;
6476ff6d951SJohn Birrell 		}
6486ff6d951SJohn Birrell 		break;
6496ff6d951SJohn Birrell 
6506ff6d951SJohn Birrell 	case DT_NODE_PROVIDER:
6516ff6d951SJohn Birrell 		dt_node_list_free(&dnp->dn_probes);
6526ff6d951SJohn Birrell 		free(dnp->dn_provname);
6536ff6d951SJohn Birrell 		dnp->dn_provname = NULL;
6546ff6d951SJohn Birrell 		break;
6556ff6d951SJohn Birrell 
6566ff6d951SJohn Birrell 	case DT_NODE_PROG:
6576ff6d951SJohn Birrell 		dt_node_list_free(&dnp->dn_list);
6586ff6d951SJohn Birrell 		break;
6596ff6d951SJohn Birrell 	}
6606ff6d951SJohn Birrell }
6616ff6d951SJohn Birrell 
6626ff6d951SJohn Birrell void
6636ff6d951SJohn Birrell dt_node_attr_assign(dt_node_t *dnp, dtrace_attribute_t attr)
6646ff6d951SJohn Birrell {
6656ff6d951SJohn Birrell 	if ((yypcb->pcb_cflags & DTRACE_C_EATTR) &&
6666ff6d951SJohn Birrell 	    (dt_attr_cmp(attr, yypcb->pcb_amin) < 0)) {
6676ff6d951SJohn Birrell 		char a[DTRACE_ATTR2STR_MAX];
6686ff6d951SJohn Birrell 		char s[BUFSIZ];
6696ff6d951SJohn Birrell 
6706ff6d951SJohn Birrell 		dnerror(dnp, D_ATTR_MIN, "attributes for %s (%s) are less than "
6716ff6d951SJohn Birrell 		    "predefined minimum\n", dt_node_name(dnp, s, sizeof (s)),
6726ff6d951SJohn Birrell 		    dtrace_attr2str(attr, a, sizeof (a)));
6736ff6d951SJohn Birrell 	}
6746ff6d951SJohn Birrell 
6756ff6d951SJohn Birrell 	dnp->dn_attr = attr;
6766ff6d951SJohn Birrell }
6776ff6d951SJohn Birrell 
6786ff6d951SJohn Birrell void
6798e648814SRui Paulo dt_node_type_assign(dt_node_t *dnp, ctf_file_t *fp, ctf_id_t type,
6808e648814SRui Paulo     boolean_t user)
6816ff6d951SJohn Birrell {
6826ff6d951SJohn Birrell 	ctf_id_t base = ctf_type_resolve(fp, type);
6836ff6d951SJohn Birrell 	uint_t kind = ctf_type_kind(fp, base);
6846ff6d951SJohn Birrell 	ctf_encoding_t e;
6856ff6d951SJohn Birrell 
6866ff6d951SJohn Birrell 	dnp->dn_flags &=
6876ff6d951SJohn Birrell 	    ~(DT_NF_SIGNED | DT_NF_REF | DT_NF_BITFIELD | DT_NF_USERLAND);
6886ff6d951SJohn Birrell 
6896ff6d951SJohn Birrell 	if (kind == CTF_K_INTEGER && ctf_type_encoding(fp, base, &e) == 0) {
6906ff6d951SJohn Birrell 		size_t size = e.cte_bits / NBBY;
6916ff6d951SJohn Birrell 
6926ff6d951SJohn Birrell 		if (size > 8 || (e.cte_bits % NBBY) != 0 || (size & (size - 1)))
6936ff6d951SJohn Birrell 			dnp->dn_flags |= DT_NF_BITFIELD;
6946ff6d951SJohn Birrell 
6956ff6d951SJohn Birrell 		if (e.cte_format & CTF_INT_SIGNED)
6966ff6d951SJohn Birrell 			dnp->dn_flags |= DT_NF_SIGNED;
6976ff6d951SJohn Birrell 	}
6986ff6d951SJohn Birrell 
6996ff6d951SJohn Birrell 	if (kind == CTF_K_FLOAT && ctf_type_encoding(fp, base, &e) == 0) {
7006ff6d951SJohn Birrell 		if (e.cte_bits / NBBY > sizeof (uint64_t))
7016ff6d951SJohn Birrell 			dnp->dn_flags |= DT_NF_REF;
7026ff6d951SJohn Birrell 	}
7036ff6d951SJohn Birrell 
7046ff6d951SJohn Birrell 	if (kind == CTF_K_STRUCT || kind == CTF_K_UNION ||
7056ff6d951SJohn Birrell 	    kind == CTF_K_FORWARD ||
7066ff6d951SJohn Birrell 	    kind == CTF_K_ARRAY || kind == CTF_K_FUNCTION)
7076ff6d951SJohn Birrell 		dnp->dn_flags |= DT_NF_REF;
7086ff6d951SJohn Birrell 	else if (yypcb != NULL && fp == DT_DYN_CTFP(yypcb->pcb_hdl) &&
7096ff6d951SJohn Birrell 	    type == DT_DYN_TYPE(yypcb->pcb_hdl))
7106ff6d951SJohn Birrell 		dnp->dn_flags |= DT_NF_REF;
7116ff6d951SJohn Birrell 
7128e648814SRui Paulo 	if (user)
7138e648814SRui Paulo 		dnp->dn_flags |= DT_NF_USERLAND;
7148e648814SRui Paulo 
7156ff6d951SJohn Birrell 	dnp->dn_flags |= DT_NF_COOKED;
7166ff6d951SJohn Birrell 	dnp->dn_ctfp = fp;
7176ff6d951SJohn Birrell 	dnp->dn_type = type;
7186ff6d951SJohn Birrell }
7196ff6d951SJohn Birrell 
7206ff6d951SJohn Birrell void
7216ff6d951SJohn Birrell dt_node_type_propagate(const dt_node_t *src, dt_node_t *dst)
7226ff6d951SJohn Birrell {
7236ff6d951SJohn Birrell 	assert(src->dn_flags & DT_NF_COOKED);
7246ff6d951SJohn Birrell 	dst->dn_flags = src->dn_flags & ~DT_NF_LVALUE;
7256ff6d951SJohn Birrell 	dst->dn_ctfp = src->dn_ctfp;
7266ff6d951SJohn Birrell 	dst->dn_type = src->dn_type;
7276ff6d951SJohn Birrell }
7286ff6d951SJohn Birrell 
7296ff6d951SJohn Birrell const char *
7306ff6d951SJohn Birrell dt_node_type_name(const dt_node_t *dnp, char *buf, size_t len)
7316ff6d951SJohn Birrell {
7326ff6d951SJohn Birrell 	if (dt_node_is_dynamic(dnp) && dnp->dn_ident != NULL) {
7336ff6d951SJohn Birrell 		(void) snprintf(buf, len, "%s",
7346ff6d951SJohn Birrell 		    dt_idkind_name(dt_ident_resolve(dnp->dn_ident)->di_kind));
7356ff6d951SJohn Birrell 		return (buf);
7366ff6d951SJohn Birrell 	}
7376ff6d951SJohn Birrell 
7386ff6d951SJohn Birrell 	if (dnp->dn_flags & DT_NF_USERLAND) {
7396ff6d951SJohn Birrell 		size_t n = snprintf(buf, len, "userland ");
7406ff6d951SJohn Birrell 		len = len > n ? len - n : 0;
7416ff6d951SJohn Birrell 		(void) dt_type_name(dnp->dn_ctfp, dnp->dn_type, buf + n, len);
7426ff6d951SJohn Birrell 		return (buf);
7436ff6d951SJohn Birrell 	}
7446ff6d951SJohn Birrell 
7456ff6d951SJohn Birrell 	return (dt_type_name(dnp->dn_ctfp, dnp->dn_type, buf, len));
7466ff6d951SJohn Birrell }
7476ff6d951SJohn Birrell 
7486ff6d951SJohn Birrell size_t
7496ff6d951SJohn Birrell dt_node_type_size(const dt_node_t *dnp)
7506ff6d951SJohn Birrell {
751b99795d6SPedro F. Giffuni 	ctf_id_t base;
7528e648814SRui Paulo 	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
753b99795d6SPedro F. Giffuni 
7546ff6d951SJohn Birrell 	if (dnp->dn_kind == DT_NODE_STRING)
7556ff6d951SJohn Birrell 		return (strlen(dnp->dn_string) + 1);
7566ff6d951SJohn Birrell 
7576ff6d951SJohn Birrell 	if (dt_node_is_dynamic(dnp) && dnp->dn_ident != NULL)
7586ff6d951SJohn Birrell 		return (dt_ident_size(dnp->dn_ident));
7596ff6d951SJohn Birrell 
760b99795d6SPedro F. Giffuni 	base = ctf_type_resolve(dnp->dn_ctfp, dnp->dn_type);
761b99795d6SPedro F. Giffuni 
762b99795d6SPedro F. Giffuni 	if (ctf_type_kind(dnp->dn_ctfp, base) == CTF_K_FORWARD)
763b99795d6SPedro F. Giffuni 		return (0);
764b99795d6SPedro F. Giffuni 
7658e648814SRui Paulo 	/*
7668e648814SRui Paulo 	 * Here we have a 32-bit user pointer that is being used with a 64-bit
7678e648814SRui Paulo 	 * kernel. When we're using it and its tagged as a userland reference --
7688e648814SRui Paulo 	 * then we need to keep it as a 32-bit pointer. However, if we are
7698e648814SRui Paulo 	 * referring to it as a kernel address, eg. being used after a copyin()
7708e648814SRui Paulo 	 * then we need to make sure that we actually return the kernel's size
7718e648814SRui Paulo 	 * of a pointer, 8 bytes.
7728e648814SRui Paulo 	 */
7738e648814SRui Paulo 	if (ctf_type_kind(dnp->dn_ctfp, base) == CTF_K_POINTER &&
7748e648814SRui Paulo 	    ctf_getmodel(dnp->dn_ctfp) == CTF_MODEL_ILP32 &&
7758e648814SRui Paulo 	    !(dnp->dn_flags & DT_NF_USERLAND) &&
7768e648814SRui Paulo 	    dtp->dt_conf.dtc_ctfmodel == CTF_MODEL_LP64)
7778e648814SRui Paulo 			return (8);
7788e648814SRui Paulo 
7796ff6d951SJohn Birrell 	return (ctf_type_size(dnp->dn_ctfp, dnp->dn_type));
7806ff6d951SJohn Birrell }
7816ff6d951SJohn Birrell 
7826ff6d951SJohn Birrell /*
7836ff6d951SJohn Birrell  * Determine if the specified parse tree node references an identifier of the
7846ff6d951SJohn Birrell  * specified kind, and if so return a pointer to it; otherwise return NULL.
7856ff6d951SJohn Birrell  * This function resolves the identifier itself, following through any inlines.
7866ff6d951SJohn Birrell  */
7876ff6d951SJohn Birrell dt_ident_t *
7886ff6d951SJohn Birrell dt_node_resolve(const dt_node_t *dnp, uint_t idkind)
7896ff6d951SJohn Birrell {
7906ff6d951SJohn Birrell 	dt_ident_t *idp;
7916ff6d951SJohn Birrell 
7926ff6d951SJohn Birrell 	switch (dnp->dn_kind) {
7936ff6d951SJohn Birrell 	case DT_NODE_VAR:
7946ff6d951SJohn Birrell 	case DT_NODE_SYM:
7956ff6d951SJohn Birrell 	case DT_NODE_FUNC:
7966ff6d951SJohn Birrell 	case DT_NODE_AGG:
7976ff6d951SJohn Birrell 	case DT_NODE_INLINE:
7986ff6d951SJohn Birrell 	case DT_NODE_PROBE:
7996ff6d951SJohn Birrell 		idp = dt_ident_resolve(dnp->dn_ident);
8006ff6d951SJohn Birrell 		return (idp->di_kind == idkind ? idp : NULL);
8016ff6d951SJohn Birrell 	}
8026ff6d951SJohn Birrell 
8036ff6d951SJohn Birrell 	if (dt_node_is_dynamic(dnp)) {
8046ff6d951SJohn Birrell 		idp = dt_ident_resolve(dnp->dn_ident);
8056ff6d951SJohn Birrell 		return (idp->di_kind == idkind ? idp : NULL);
8066ff6d951SJohn Birrell 	}
8076ff6d951SJohn Birrell 
8086ff6d951SJohn Birrell 	return (NULL);
8096ff6d951SJohn Birrell }
8106ff6d951SJohn Birrell 
8116ff6d951SJohn Birrell size_t
8126ff6d951SJohn Birrell dt_node_sizeof(const dt_node_t *dnp)
8136ff6d951SJohn Birrell {
8146ff6d951SJohn Birrell 	dtrace_syminfo_t *sip;
8156ff6d951SJohn Birrell 	GElf_Sym sym;
8166ff6d951SJohn Birrell 	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
8176ff6d951SJohn Birrell 
8186ff6d951SJohn Birrell 	/*
8196ff6d951SJohn Birrell 	 * The size of the node as used for the sizeof() operator depends on
8206ff6d951SJohn Birrell 	 * the kind of the node.  If the node is a SYM, the size is obtained
8216ff6d951SJohn Birrell 	 * from the symbol table; if it is not a SYM, the size is determined
8226ff6d951SJohn Birrell 	 * from the node's type.  This is slightly different from C's sizeof()
8236ff6d951SJohn Birrell 	 * operator in that (for example) when applied to a function, sizeof()
8246ff6d951SJohn Birrell 	 * will evaluate to the length of the function rather than the size of
8256ff6d951SJohn Birrell 	 * the function type.
8266ff6d951SJohn Birrell 	 */
8276ff6d951SJohn Birrell 	if (dnp->dn_kind != DT_NODE_SYM)
8286ff6d951SJohn Birrell 		return (dt_node_type_size(dnp));
8296ff6d951SJohn Birrell 
8306ff6d951SJohn Birrell 	sip = dnp->dn_ident->di_data;
8316ff6d951SJohn Birrell 
8326ff6d951SJohn Birrell 	if (dtrace_lookup_by_name(dtp, sip->dts_object,
8336ff6d951SJohn Birrell 	    sip->dts_name, &sym, NULL) == -1)
8346ff6d951SJohn Birrell 		return (0);
8356ff6d951SJohn Birrell 
8366ff6d951SJohn Birrell 	return (sym.st_size);
8376ff6d951SJohn Birrell }
8386ff6d951SJohn Birrell 
8396ff6d951SJohn Birrell int
8406ff6d951SJohn Birrell dt_node_is_integer(const dt_node_t *dnp)
8416ff6d951SJohn Birrell {
8426ff6d951SJohn Birrell 	ctf_file_t *fp = dnp->dn_ctfp;
8436ff6d951SJohn Birrell 	ctf_encoding_t e;
8446ff6d951SJohn Birrell 	ctf_id_t type;
8456ff6d951SJohn Birrell 	uint_t kind;
8466ff6d951SJohn Birrell 
8476ff6d951SJohn Birrell 	assert(dnp->dn_flags & DT_NF_COOKED);
8486ff6d951SJohn Birrell 
8496ff6d951SJohn Birrell 	type = ctf_type_resolve(fp, dnp->dn_type);
8506ff6d951SJohn Birrell 	kind = ctf_type_kind(fp, type);
8516ff6d951SJohn Birrell 
8526ff6d951SJohn Birrell 	if (kind == CTF_K_INTEGER &&
8536ff6d951SJohn Birrell 	    ctf_type_encoding(fp, type, &e) == 0 && IS_VOID(e))
8546ff6d951SJohn Birrell 		return (0); /* void integer */
8556ff6d951SJohn Birrell 
8566ff6d951SJohn Birrell 	return (kind == CTF_K_INTEGER || kind == CTF_K_ENUM);
8576ff6d951SJohn Birrell }
8586ff6d951SJohn Birrell 
8596ff6d951SJohn Birrell int
8606ff6d951SJohn Birrell dt_node_is_float(const dt_node_t *dnp)
8616ff6d951SJohn Birrell {
8626ff6d951SJohn Birrell 	ctf_file_t *fp = dnp->dn_ctfp;
8636ff6d951SJohn Birrell 	ctf_encoding_t e;
8646ff6d951SJohn Birrell 	ctf_id_t type;
8656ff6d951SJohn Birrell 	uint_t kind;
8666ff6d951SJohn Birrell 
8676ff6d951SJohn Birrell 	assert(dnp->dn_flags & DT_NF_COOKED);
8686ff6d951SJohn Birrell 
8696ff6d951SJohn Birrell 	type = ctf_type_resolve(fp, dnp->dn_type);
8706ff6d951SJohn Birrell 	kind = ctf_type_kind(fp, type);
8716ff6d951SJohn Birrell 
8726ff6d951SJohn Birrell 	return (kind == CTF_K_FLOAT &&
8736ff6d951SJohn Birrell 	    ctf_type_encoding(dnp->dn_ctfp, type, &e) == 0 && (
8746ff6d951SJohn Birrell 	    e.cte_format == CTF_FP_SINGLE || e.cte_format == CTF_FP_DOUBLE ||
8756ff6d951SJohn Birrell 	    e.cte_format == CTF_FP_LDOUBLE));
8766ff6d951SJohn Birrell }
8776ff6d951SJohn Birrell 
8786ff6d951SJohn Birrell int
8796ff6d951SJohn Birrell dt_node_is_scalar(const dt_node_t *dnp)
8806ff6d951SJohn Birrell {
8816ff6d951SJohn Birrell 	ctf_file_t *fp = dnp->dn_ctfp;
8826ff6d951SJohn Birrell 	ctf_encoding_t e;
8836ff6d951SJohn Birrell 	ctf_id_t type;
8846ff6d951SJohn Birrell 	uint_t kind;
8856ff6d951SJohn Birrell 
8866ff6d951SJohn Birrell 	assert(dnp->dn_flags & DT_NF_COOKED);
8876ff6d951SJohn Birrell 
8886ff6d951SJohn Birrell 	type = ctf_type_resolve(fp, dnp->dn_type);
8896ff6d951SJohn Birrell 	kind = ctf_type_kind(fp, type);
8906ff6d951SJohn Birrell 
8916ff6d951SJohn Birrell 	if (kind == CTF_K_INTEGER &&
8926ff6d951SJohn Birrell 	    ctf_type_encoding(fp, type, &e) == 0 && IS_VOID(e))
8936ff6d951SJohn Birrell 		return (0); /* void cannot be used as a scalar */
8946ff6d951SJohn Birrell 
8956ff6d951SJohn Birrell 	return (kind == CTF_K_INTEGER || kind == CTF_K_ENUM ||
8966ff6d951SJohn Birrell 	    kind == CTF_K_POINTER);
8976ff6d951SJohn Birrell }
8986ff6d951SJohn Birrell 
8996ff6d951SJohn Birrell int
9006ff6d951SJohn Birrell dt_node_is_arith(const dt_node_t *dnp)
9016ff6d951SJohn Birrell {
9026ff6d951SJohn Birrell 	ctf_file_t *fp = dnp->dn_ctfp;
9036ff6d951SJohn Birrell 	ctf_encoding_t e;
9046ff6d951SJohn Birrell 	ctf_id_t type;
9056ff6d951SJohn Birrell 	uint_t kind;
9066ff6d951SJohn Birrell 
9076ff6d951SJohn Birrell 	assert(dnp->dn_flags & DT_NF_COOKED);
9086ff6d951SJohn Birrell 
9096ff6d951SJohn Birrell 	type = ctf_type_resolve(fp, dnp->dn_type);
9106ff6d951SJohn Birrell 	kind = ctf_type_kind(fp, type);
9116ff6d951SJohn Birrell 
9126ff6d951SJohn Birrell 	if (kind == CTF_K_INTEGER)
9136ff6d951SJohn Birrell 		return (ctf_type_encoding(fp, type, &e) == 0 && !IS_VOID(e));
9146ff6d951SJohn Birrell 	else
9156ff6d951SJohn Birrell 		return (kind == CTF_K_ENUM);
9166ff6d951SJohn Birrell }
9176ff6d951SJohn Birrell 
9186ff6d951SJohn Birrell int
9196ff6d951SJohn Birrell dt_node_is_vfptr(const dt_node_t *dnp)
9206ff6d951SJohn Birrell {
9216ff6d951SJohn Birrell 	ctf_file_t *fp = dnp->dn_ctfp;
9226ff6d951SJohn Birrell 	ctf_encoding_t e;
9236ff6d951SJohn Birrell 	ctf_id_t type;
9246ff6d951SJohn Birrell 	uint_t kind;
9256ff6d951SJohn Birrell 
9266ff6d951SJohn Birrell 	assert(dnp->dn_flags & DT_NF_COOKED);
9276ff6d951SJohn Birrell 
9286ff6d951SJohn Birrell 	type = ctf_type_resolve(fp, dnp->dn_type);
9296ff6d951SJohn Birrell 	if (ctf_type_kind(fp, type) != CTF_K_POINTER)
9306ff6d951SJohn Birrell 		return (0); /* type is not a pointer */
9316ff6d951SJohn Birrell 
9326ff6d951SJohn Birrell 	type = ctf_type_resolve(fp, ctf_type_reference(fp, type));
9336ff6d951SJohn Birrell 	kind = ctf_type_kind(fp, type);
9346ff6d951SJohn Birrell 
9356ff6d951SJohn Birrell 	return (kind == CTF_K_FUNCTION || (kind == CTF_K_INTEGER &&
9366ff6d951SJohn Birrell 	    ctf_type_encoding(fp, type, &e) == 0 && IS_VOID(e)));
9376ff6d951SJohn Birrell }
9386ff6d951SJohn Birrell 
9396ff6d951SJohn Birrell int
9406ff6d951SJohn Birrell dt_node_is_dynamic(const dt_node_t *dnp)
9416ff6d951SJohn Birrell {
9426ff6d951SJohn Birrell 	if (dnp->dn_kind == DT_NODE_VAR &&
9436ff6d951SJohn Birrell 	    (dnp->dn_ident->di_flags & DT_IDFLG_INLINE)) {
9446ff6d951SJohn Birrell 		const dt_idnode_t *inp = dnp->dn_ident->di_iarg;
9456ff6d951SJohn Birrell 		return (inp->din_root ? dt_node_is_dynamic(inp->din_root) : 0);
9466ff6d951SJohn Birrell 	}
9476ff6d951SJohn Birrell 
9486ff6d951SJohn Birrell 	return (dnp->dn_ctfp == DT_DYN_CTFP(yypcb->pcb_hdl) &&
9496ff6d951SJohn Birrell 	    dnp->dn_type == DT_DYN_TYPE(yypcb->pcb_hdl));
9506ff6d951SJohn Birrell }
9516ff6d951SJohn Birrell 
9526ff6d951SJohn Birrell int
9536ff6d951SJohn Birrell dt_node_is_string(const dt_node_t *dnp)
9546ff6d951SJohn Birrell {
9556ff6d951SJohn Birrell 	return (dnp->dn_ctfp == DT_STR_CTFP(yypcb->pcb_hdl) &&
9566ff6d951SJohn Birrell 	    dnp->dn_type == DT_STR_TYPE(yypcb->pcb_hdl));
9576ff6d951SJohn Birrell }
9586ff6d951SJohn Birrell 
9596ff6d951SJohn Birrell int
9606ff6d951SJohn Birrell dt_node_is_stack(const dt_node_t *dnp)
9616ff6d951SJohn Birrell {
9626ff6d951SJohn Birrell 	return (dnp->dn_ctfp == DT_STACK_CTFP(yypcb->pcb_hdl) &&
9636ff6d951SJohn Birrell 	    dnp->dn_type == DT_STACK_TYPE(yypcb->pcb_hdl));
9646ff6d951SJohn Birrell }
9656ff6d951SJohn Birrell 
9666ff6d951SJohn Birrell int
9676ff6d951SJohn Birrell dt_node_is_symaddr(const dt_node_t *dnp)
9686ff6d951SJohn Birrell {
9696ff6d951SJohn Birrell 	return (dnp->dn_ctfp == DT_SYMADDR_CTFP(yypcb->pcb_hdl) &&
9706ff6d951SJohn Birrell 	    dnp->dn_type == DT_SYMADDR_TYPE(yypcb->pcb_hdl));
9716ff6d951SJohn Birrell }
9726ff6d951SJohn Birrell 
9736ff6d951SJohn Birrell int
9746ff6d951SJohn Birrell dt_node_is_usymaddr(const dt_node_t *dnp)
9756ff6d951SJohn Birrell {
9766ff6d951SJohn Birrell 	return (dnp->dn_ctfp == DT_USYMADDR_CTFP(yypcb->pcb_hdl) &&
9776ff6d951SJohn Birrell 	    dnp->dn_type == DT_USYMADDR_TYPE(yypcb->pcb_hdl));
9786ff6d951SJohn Birrell }
9796ff6d951SJohn Birrell 
9806ff6d951SJohn Birrell int
9816ff6d951SJohn Birrell dt_node_is_strcompat(const dt_node_t *dnp)
9826ff6d951SJohn Birrell {
9836ff6d951SJohn Birrell 	ctf_file_t *fp = dnp->dn_ctfp;
9846ff6d951SJohn Birrell 	ctf_encoding_t e;
9856ff6d951SJohn Birrell 	ctf_arinfo_t r;
9866ff6d951SJohn Birrell 	ctf_id_t base;
9876ff6d951SJohn Birrell 	uint_t kind;
9886ff6d951SJohn Birrell 
9896ff6d951SJohn Birrell 	assert(dnp->dn_flags & DT_NF_COOKED);
9906ff6d951SJohn Birrell 
9916ff6d951SJohn Birrell 	base = ctf_type_resolve(fp, dnp->dn_type);
9926ff6d951SJohn Birrell 	kind = ctf_type_kind(fp, base);
9936ff6d951SJohn Birrell 
9946ff6d951SJohn Birrell 	if (kind == CTF_K_POINTER &&
9956ff6d951SJohn Birrell 	    (base = ctf_type_reference(fp, base)) != CTF_ERR &&
9966ff6d951SJohn Birrell 	    (base = ctf_type_resolve(fp, base)) != CTF_ERR &&
9976ff6d951SJohn Birrell 	    ctf_type_encoding(fp, base, &e) == 0 && IS_CHAR(e))
9986ff6d951SJohn Birrell 		return (1); /* promote char pointer to string */
9996ff6d951SJohn Birrell 
10006ff6d951SJohn Birrell 	if (kind == CTF_K_ARRAY && ctf_array_info(fp, base, &r) == 0 &&
10016ff6d951SJohn Birrell 	    (base = ctf_type_resolve(fp, r.ctr_contents)) != CTF_ERR &&
10026ff6d951SJohn Birrell 	    ctf_type_encoding(fp, base, &e) == 0 && IS_CHAR(e))
10036ff6d951SJohn Birrell 		return (1); /* promote char array to string */
10046ff6d951SJohn Birrell 
10056ff6d951SJohn Birrell 	return (0);
10066ff6d951SJohn Birrell }
10076ff6d951SJohn Birrell 
10086ff6d951SJohn Birrell int
10096ff6d951SJohn Birrell dt_node_is_pointer(const dt_node_t *dnp)
10106ff6d951SJohn Birrell {
10116ff6d951SJohn Birrell 	ctf_file_t *fp = dnp->dn_ctfp;
10126ff6d951SJohn Birrell 	uint_t kind;
10136ff6d951SJohn Birrell 
10146ff6d951SJohn Birrell 	assert(dnp->dn_flags & DT_NF_COOKED);
10156ff6d951SJohn Birrell 
10166ff6d951SJohn Birrell 	if (dt_node_is_string(dnp))
10176ff6d951SJohn Birrell 		return (0); /* string are pass-by-ref but act like structs */
10186ff6d951SJohn Birrell 
10196ff6d951SJohn Birrell 	kind = ctf_type_kind(fp, ctf_type_resolve(fp, dnp->dn_type));
10206ff6d951SJohn Birrell 	return (kind == CTF_K_POINTER || kind == CTF_K_ARRAY);
10216ff6d951SJohn Birrell }
10226ff6d951SJohn Birrell 
10236ff6d951SJohn Birrell int
10246ff6d951SJohn Birrell dt_node_is_void(const dt_node_t *dnp)
10256ff6d951SJohn Birrell {
10266ff6d951SJohn Birrell 	ctf_file_t *fp = dnp->dn_ctfp;
10276ff6d951SJohn Birrell 	ctf_encoding_t e;
10286ff6d951SJohn Birrell 	ctf_id_t type;
10296ff6d951SJohn Birrell 
10306ff6d951SJohn Birrell 	if (dt_node_is_dynamic(dnp))
10316ff6d951SJohn Birrell 		return (0); /* <DYN> is an alias for void but not the same */
10326ff6d951SJohn Birrell 
10336ff6d951SJohn Birrell 	if (dt_node_is_stack(dnp))
10346ff6d951SJohn Birrell 		return (0);
10356ff6d951SJohn Birrell 
10366ff6d951SJohn Birrell 	if (dt_node_is_symaddr(dnp) || dt_node_is_usymaddr(dnp))
10376ff6d951SJohn Birrell 		return (0);
10386ff6d951SJohn Birrell 
10396ff6d951SJohn Birrell 	type = ctf_type_resolve(fp, dnp->dn_type);
10406ff6d951SJohn Birrell 
10416ff6d951SJohn Birrell 	return (ctf_type_kind(fp, type) == CTF_K_INTEGER &&
10426ff6d951SJohn Birrell 	    ctf_type_encoding(fp, type, &e) == 0 && IS_VOID(e));
10436ff6d951SJohn Birrell }
10446ff6d951SJohn Birrell 
10456ff6d951SJohn Birrell int
10466ff6d951SJohn Birrell dt_node_is_ptrcompat(const dt_node_t *lp, const dt_node_t *rp,
10476ff6d951SJohn Birrell     ctf_file_t **fpp, ctf_id_t *tp)
10486ff6d951SJohn Birrell {
10496ff6d951SJohn Birrell 	ctf_file_t *lfp = lp->dn_ctfp;
10506ff6d951SJohn Birrell 	ctf_file_t *rfp = rp->dn_ctfp;
10516ff6d951SJohn Birrell 
10526ff6d951SJohn Birrell 	ctf_id_t lbase = CTF_ERR, rbase = CTF_ERR;
10536ff6d951SJohn Birrell 	ctf_id_t lref = CTF_ERR, rref = CTF_ERR;
10546ff6d951SJohn Birrell 
10556ff6d951SJohn Birrell 	int lp_is_void, rp_is_void, lp_is_int, rp_is_int, compat;
10566ff6d951SJohn Birrell 	uint_t lkind, rkind;
10576ff6d951SJohn Birrell 	ctf_encoding_t e;
10586ff6d951SJohn Birrell 	ctf_arinfo_t r;
10596ff6d951SJohn Birrell 
10606ff6d951SJohn Birrell 	assert(lp->dn_flags & DT_NF_COOKED);
10616ff6d951SJohn Birrell 	assert(rp->dn_flags & DT_NF_COOKED);
10626ff6d951SJohn Birrell 
10636ff6d951SJohn Birrell 	if (dt_node_is_dynamic(lp) || dt_node_is_dynamic(rp))
10646ff6d951SJohn Birrell 		return (0); /* fail if either node is a dynamic variable */
10656ff6d951SJohn Birrell 
10666ff6d951SJohn Birrell 	lp_is_int = dt_node_is_integer(lp);
10676ff6d951SJohn Birrell 	rp_is_int = dt_node_is_integer(rp);
10686ff6d951SJohn Birrell 
10696ff6d951SJohn Birrell 	if (lp_is_int && rp_is_int)
10706ff6d951SJohn Birrell 		return (0); /* fail if both nodes are integers */
10716ff6d951SJohn Birrell 
10726ff6d951SJohn Birrell 	if (lp_is_int && (lp->dn_kind != DT_NODE_INT || lp->dn_value != 0))
10736ff6d951SJohn Birrell 		return (0); /* fail if lp is an integer that isn't 0 constant */
10746ff6d951SJohn Birrell 
10756ff6d951SJohn Birrell 	if (rp_is_int && (rp->dn_kind != DT_NODE_INT || rp->dn_value != 0))
10766ff6d951SJohn Birrell 		return (0); /* fail if rp is an integer that isn't 0 constant */
10776ff6d951SJohn Birrell 
10786ff6d951SJohn Birrell 	if ((lp_is_int == 0 && rp_is_int == 0) && (
10796ff6d951SJohn Birrell 	    (lp->dn_flags & DT_NF_USERLAND) ^ (rp->dn_flags & DT_NF_USERLAND)))
10806ff6d951SJohn Birrell 		return (0); /* fail if only one pointer is a userland address */
10816ff6d951SJohn Birrell 
10826ff6d951SJohn Birrell 	/*
10836ff6d951SJohn Birrell 	 * Resolve the left-hand and right-hand types to their base type, and
10846ff6d951SJohn Birrell 	 * then resolve the referenced type as well (assuming the base type
10856ff6d951SJohn Birrell 	 * is CTF_K_POINTER or CTF_K_ARRAY).  Otherwise [lr]ref = CTF_ERR.
10866ff6d951SJohn Birrell 	 */
10876ff6d951SJohn Birrell 	if (!lp_is_int) {
10886ff6d951SJohn Birrell 		lbase = ctf_type_resolve(lfp, lp->dn_type);
10896ff6d951SJohn Birrell 		lkind = ctf_type_kind(lfp, lbase);
10906ff6d951SJohn Birrell 
10916ff6d951SJohn Birrell 		if (lkind == CTF_K_POINTER) {
10926ff6d951SJohn Birrell 			lref = ctf_type_resolve(lfp,
10936ff6d951SJohn Birrell 			    ctf_type_reference(lfp, lbase));
10946ff6d951SJohn Birrell 		} else if (lkind == CTF_K_ARRAY &&
10956ff6d951SJohn Birrell 		    ctf_array_info(lfp, lbase, &r) == 0) {
10966ff6d951SJohn Birrell 			lref = ctf_type_resolve(lfp, r.ctr_contents);
10976ff6d951SJohn Birrell 		}
10986ff6d951SJohn Birrell 	}
10996ff6d951SJohn Birrell 
11006ff6d951SJohn Birrell 	if (!rp_is_int) {
11016ff6d951SJohn Birrell 		rbase = ctf_type_resolve(rfp, rp->dn_type);
11026ff6d951SJohn Birrell 		rkind = ctf_type_kind(rfp, rbase);
11036ff6d951SJohn Birrell 
11046ff6d951SJohn Birrell 		if (rkind == CTF_K_POINTER) {
11056ff6d951SJohn Birrell 			rref = ctf_type_resolve(rfp,
11066ff6d951SJohn Birrell 			    ctf_type_reference(rfp, rbase));
11076ff6d951SJohn Birrell 		} else if (rkind == CTF_K_ARRAY &&
11086ff6d951SJohn Birrell 		    ctf_array_info(rfp, rbase, &r) == 0) {
11096ff6d951SJohn Birrell 			rref = ctf_type_resolve(rfp, r.ctr_contents);
11106ff6d951SJohn Birrell 		}
11116ff6d951SJohn Birrell 	}
11126ff6d951SJohn Birrell 
11136ff6d951SJohn Birrell 	/*
11146ff6d951SJohn Birrell 	 * We know that one or the other type may still be a zero-valued
11156ff6d951SJohn Birrell 	 * integer constant.  To simplify the code below, set the integer
11166ff6d951SJohn Birrell 	 * type variables equal to the non-integer types and proceed.
11176ff6d951SJohn Birrell 	 */
11186ff6d951SJohn Birrell 	if (lp_is_int) {
11196ff6d951SJohn Birrell 		lbase = rbase;
11206ff6d951SJohn Birrell 		lkind = rkind;
11216ff6d951SJohn Birrell 		lref = rref;
11226ff6d951SJohn Birrell 		lfp = rfp;
11236ff6d951SJohn Birrell 	} else if (rp_is_int) {
11246ff6d951SJohn Birrell 		rbase = lbase;
11256ff6d951SJohn Birrell 		rkind = lkind;
11266ff6d951SJohn Birrell 		rref = lref;
11276ff6d951SJohn Birrell 		rfp = lfp;
11286ff6d951SJohn Birrell 	}
11296ff6d951SJohn Birrell 
11306ff6d951SJohn Birrell 	lp_is_void = ctf_type_encoding(lfp, lref, &e) == 0 && IS_VOID(e);
11316ff6d951SJohn Birrell 	rp_is_void = ctf_type_encoding(rfp, rref, &e) == 0 && IS_VOID(e);
11326ff6d951SJohn Birrell 
11336ff6d951SJohn Birrell 	/*
1134*4196f227SMark Johnston 	 * Let a pointer to a forward declaration be compatible with a pointer
1135*4196f227SMark Johnston 	 * to a struct or union of the same name.
1136*4196f227SMark Johnston 	 */
1137*4196f227SMark Johnston 	if (lkind == CTF_K_POINTER && rkind == CTF_K_POINTER) {
1138*4196f227SMark Johnston 		int lrkind, rrkind;
1139*4196f227SMark Johnston 
1140*4196f227SMark Johnston 		lrkind = ctf_type_kind(lfp, lref);
1141*4196f227SMark Johnston 		rrkind = ctf_type_kind(rfp, rref);
1142*4196f227SMark Johnston 		if (lrkind == CTF_K_FORWARD || rrkind == CTF_K_FORWARD) {
1143*4196f227SMark Johnston 			const char *lname, *rname;
1144*4196f227SMark Johnston 			char ln[DT_TYPE_NAMELEN], rn[DT_TYPE_NAMELEN];
1145*4196f227SMark Johnston 
1146*4196f227SMark Johnston 			lname = ctf_type_name(lfp, lref, ln, sizeof (ln));
1147*4196f227SMark Johnston 			rname = ctf_type_name(rfp, rref, rn, sizeof (rn));
1148*4196f227SMark Johnston 			if (lname != NULL && rname != NULL &&
1149*4196f227SMark Johnston 			    strcmp(lname, rname) == 0) {
1150*4196f227SMark Johnston 				lp_is_void = lrkind == CTF_K_FORWARD;
1151*4196f227SMark Johnston 				rp_is_void = rrkind == CTF_K_FORWARD;
1152*4196f227SMark Johnston 			}
1153*4196f227SMark Johnston 		}
1154*4196f227SMark Johnston 	}
1155*4196f227SMark Johnston 
1156*4196f227SMark Johnston 	/*
11576ff6d951SJohn Birrell 	 * The types are compatible if both are pointers to the same type, or
11586ff6d951SJohn Birrell 	 * if either pointer is a void pointer.  If they are compatible, set
11596ff6d951SJohn Birrell 	 * tp to point to the more specific pointer type and return it.
11606ff6d951SJohn Birrell 	 */
11616ff6d951SJohn Birrell 	compat = (lkind == CTF_K_POINTER || lkind == CTF_K_ARRAY) &&
11626ff6d951SJohn Birrell 	    (rkind == CTF_K_POINTER || rkind == CTF_K_ARRAY) &&
11636ff6d951SJohn Birrell 	    (lp_is_void || rp_is_void || ctf_type_compat(lfp, lref, rfp, rref));
11646ff6d951SJohn Birrell 
11656ff6d951SJohn Birrell 	if (compat) {
11666ff6d951SJohn Birrell 		if (fpp != NULL)
11676ff6d951SJohn Birrell 			*fpp = rp_is_void ? lfp : rfp;
11686ff6d951SJohn Birrell 		if (tp != NULL)
11696ff6d951SJohn Birrell 			*tp = rp_is_void ? lbase : rbase;
11706ff6d951SJohn Birrell 	}
11716ff6d951SJohn Birrell 
11726ff6d951SJohn Birrell 	return (compat);
11736ff6d951SJohn Birrell }
11746ff6d951SJohn Birrell 
11756ff6d951SJohn Birrell /*
11766ff6d951SJohn Birrell  * The rules for checking argument types against parameter types are described
11776ff6d951SJohn Birrell  * in the ANSI-C spec (see K&R[A7.3.2] and K&R[A7.17]).  We use the same rule
11786ff6d951SJohn Birrell  * set to determine whether associative array arguments match the prototype.
11796ff6d951SJohn Birrell  */
11806ff6d951SJohn Birrell int
11816ff6d951SJohn Birrell dt_node_is_argcompat(const dt_node_t *lp, const dt_node_t *rp)
11826ff6d951SJohn Birrell {
11836ff6d951SJohn Birrell 	ctf_file_t *lfp = lp->dn_ctfp;
11846ff6d951SJohn Birrell 	ctf_file_t *rfp = rp->dn_ctfp;
11856ff6d951SJohn Birrell 
11866ff6d951SJohn Birrell 	assert(lp->dn_flags & DT_NF_COOKED);
11876ff6d951SJohn Birrell 	assert(rp->dn_flags & DT_NF_COOKED);
11886ff6d951SJohn Birrell 
11896ff6d951SJohn Birrell 	if (dt_node_is_integer(lp) && dt_node_is_integer(rp))
11906ff6d951SJohn Birrell 		return (1); /* integer types are compatible */
11916ff6d951SJohn Birrell 
11926ff6d951SJohn Birrell 	if (dt_node_is_strcompat(lp) && dt_node_is_strcompat(rp))
11936ff6d951SJohn Birrell 		return (1); /* string types are compatible */
11946ff6d951SJohn Birrell 
11956ff6d951SJohn Birrell 	if (dt_node_is_stack(lp) && dt_node_is_stack(rp))
11966ff6d951SJohn Birrell 		return (1); /* stack types are compatible */
11976ff6d951SJohn Birrell 
11986ff6d951SJohn Birrell 	if (dt_node_is_symaddr(lp) && dt_node_is_symaddr(rp))
11996ff6d951SJohn Birrell 		return (1); /* symaddr types are compatible */
12006ff6d951SJohn Birrell 
12016ff6d951SJohn Birrell 	if (dt_node_is_usymaddr(lp) && dt_node_is_usymaddr(rp))
12026ff6d951SJohn Birrell 		return (1); /* usymaddr types are compatible */
12036ff6d951SJohn Birrell 
12046ff6d951SJohn Birrell 	switch (ctf_type_kind(lfp, ctf_type_resolve(lfp, lp->dn_type))) {
12056ff6d951SJohn Birrell 	case CTF_K_FUNCTION:
12066ff6d951SJohn Birrell 	case CTF_K_STRUCT:
12076ff6d951SJohn Birrell 	case CTF_K_UNION:
12086ff6d951SJohn Birrell 		return (ctf_type_compat(lfp, lp->dn_type, rfp, rp->dn_type));
12096ff6d951SJohn Birrell 	default:
12106ff6d951SJohn Birrell 		return (dt_node_is_ptrcompat(lp, rp, NULL, NULL));
12116ff6d951SJohn Birrell 	}
12126ff6d951SJohn Birrell }
12136ff6d951SJohn Birrell 
12146ff6d951SJohn Birrell /*
12156ff6d951SJohn Birrell  * We provide dt_node_is_posconst() as a convenience routine for callers who
12166ff6d951SJohn Birrell  * wish to verify that an argument is a positive non-zero integer constant.
12176ff6d951SJohn Birrell  */
12186ff6d951SJohn Birrell int
12196ff6d951SJohn Birrell dt_node_is_posconst(const dt_node_t *dnp)
12206ff6d951SJohn Birrell {
12216ff6d951SJohn Birrell 	return (dnp->dn_kind == DT_NODE_INT && dnp->dn_value != 0 && (
12226ff6d951SJohn Birrell 	    (dnp->dn_flags & DT_NF_SIGNED) == 0 || (int64_t)dnp->dn_value > 0));
12236ff6d951SJohn Birrell }
12246ff6d951SJohn Birrell 
12256ff6d951SJohn Birrell int
12266ff6d951SJohn Birrell dt_node_is_actfunc(const dt_node_t *dnp)
12276ff6d951SJohn Birrell {
12286ff6d951SJohn Birrell 	return (dnp->dn_kind == DT_NODE_FUNC &&
12296ff6d951SJohn Birrell 	    dnp->dn_ident->di_kind == DT_IDENT_ACTFUNC);
12306ff6d951SJohn Birrell }
12316ff6d951SJohn Birrell 
12326ff6d951SJohn Birrell /*
12336ff6d951SJohn Birrell  * The original rules for integer constant typing are described in K&R[A2.5.1].
12346ff6d951SJohn Birrell  * However, since we support long long, we instead use the rules from ISO C99
12356ff6d951SJohn Birrell  * clause 6.4.4.1 since that is where long longs are formally described.  The
12366ff6d951SJohn Birrell  * rules require us to know whether the constant was specified in decimal or
12376ff6d951SJohn Birrell  * in octal or hex, which we do by looking at our lexer's 'yyintdecimal' flag.
12386ff6d951SJohn Birrell  * The type of an integer constant is the first of the corresponding list in
12396ff6d951SJohn Birrell  * which its value can be represented:
12406ff6d951SJohn Birrell  *
12416ff6d951SJohn Birrell  * unsuffixed decimal:   int, long, long long
12426ff6d951SJohn Birrell  * unsuffixed oct/hex:   int, unsigned int, long, unsigned long,
12436ff6d951SJohn Birrell  *                       long long, unsigned long long
12446ff6d951SJohn Birrell  * suffix [uU]:          unsigned int, unsigned long, unsigned long long
12456ff6d951SJohn Birrell  * suffix [lL] decimal:  long, long long
12466ff6d951SJohn Birrell  * suffix [lL] oct/hex:  long, unsigned long, long long, unsigned long long
12476ff6d951SJohn Birrell  * suffix [uU][Ll]:      unsigned long, unsigned long long
12486ff6d951SJohn Birrell  * suffix ll/LL decimal: long long
12496ff6d951SJohn Birrell  * suffix ll/LL oct/hex: long long, unsigned long long
12506ff6d951SJohn Birrell  * suffix [uU][ll/LL]:   unsigned long long
12516ff6d951SJohn Birrell  *
12526ff6d951SJohn Birrell  * Given that our lexer has already validated the suffixes by regexp matching,
12536ff6d951SJohn Birrell  * there is an obvious way to concisely encode these rules: construct an array
12546ff6d951SJohn Birrell  * of the types in the order int, unsigned int, long, unsigned long, long long,
12556ff6d951SJohn Birrell  * unsigned long long.  Compute an integer array starting index based on the
12566ff6d951SJohn Birrell  * suffix (e.g. none = 0, u = 1, ull = 5), and compute an increment based on
12576ff6d951SJohn Birrell  * the specifier (dec/oct/hex) and suffix (u).  Then iterate from the starting
12586ff6d951SJohn Birrell  * index to the end, advancing using the increment, and searching until we
12596ff6d951SJohn Birrell  * find a limit that matches or we run out of choices (overflow).  To make it
12606ff6d951SJohn Birrell  * even faster, we precompute the table of type information in dtrace_open().
12616ff6d951SJohn Birrell  */
12626ff6d951SJohn Birrell dt_node_t *
12636ff6d951SJohn Birrell dt_node_int(uintmax_t value)
12646ff6d951SJohn Birrell {
12656ff6d951SJohn Birrell 	dt_node_t *dnp = dt_node_alloc(DT_NODE_INT);
12666ff6d951SJohn Birrell 	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
12676ff6d951SJohn Birrell 
12686ff6d951SJohn Birrell 	int n = (yyintdecimal | (yyintsuffix[0] == 'u')) + 1;
12696ff6d951SJohn Birrell 	int i = 0;
12706ff6d951SJohn Birrell 
12716ff6d951SJohn Birrell 	const char *p;
12726ff6d951SJohn Birrell 	char c;
12736ff6d951SJohn Birrell 
12746ff6d951SJohn Birrell 	dnp->dn_op = DT_TOK_INT;
12756ff6d951SJohn Birrell 	dnp->dn_value = value;
12766ff6d951SJohn Birrell 
12776ff6d951SJohn Birrell 	for (p = yyintsuffix; (c = *p) != '\0'; p++) {
12786ff6d951SJohn Birrell 		if (c == 'U' || c == 'u')
12796ff6d951SJohn Birrell 			i += 1;
12806ff6d951SJohn Birrell 		else if (c == 'L' || c == 'l')
12816ff6d951SJohn Birrell 			i += 2;
12826ff6d951SJohn Birrell 	}
12836ff6d951SJohn Birrell 
12846ff6d951SJohn Birrell 	for (; i < sizeof (dtp->dt_ints) / sizeof (dtp->dt_ints[0]); i += n) {
12856ff6d951SJohn Birrell 		if (value <= dtp->dt_ints[i].did_limit) {
12866ff6d951SJohn Birrell 			dt_node_type_assign(dnp,
12876ff6d951SJohn Birrell 			    dtp->dt_ints[i].did_ctfp,
12888e648814SRui Paulo 			    dtp->dt_ints[i].did_type, B_FALSE);
12896ff6d951SJohn Birrell 
12906ff6d951SJohn Birrell 			/*
12916ff6d951SJohn Birrell 			 * If a prefix character is present in macro text, add
12926ff6d951SJohn Birrell 			 * in the corresponding operator node (see dt_lex.l).
12936ff6d951SJohn Birrell 			 */
12946ff6d951SJohn Birrell 			switch (yyintprefix) {
12956ff6d951SJohn Birrell 			case '+':
12966ff6d951SJohn Birrell 				return (dt_node_op1(DT_TOK_IPOS, dnp));
12976ff6d951SJohn Birrell 			case '-':
12986ff6d951SJohn Birrell 				return (dt_node_op1(DT_TOK_INEG, dnp));
12996ff6d951SJohn Birrell 			default:
13006ff6d951SJohn Birrell 				return (dnp);
13016ff6d951SJohn Birrell 			}
13026ff6d951SJohn Birrell 		}
13036ff6d951SJohn Birrell 	}
13046ff6d951SJohn Birrell 
13056ff6d951SJohn Birrell 	xyerror(D_INT_OFLOW, "integer constant 0x%llx cannot be represented "
13066ff6d951SJohn Birrell 	    "in any built-in integral type\n", (u_longlong_t)value);
13076ff6d951SJohn Birrell 	/*NOTREACHED*/
13086ff6d951SJohn Birrell 	return (NULL);		/* keep gcc happy */
13096ff6d951SJohn Birrell }
13106ff6d951SJohn Birrell 
13116ff6d951SJohn Birrell dt_node_t *
13126ff6d951SJohn Birrell dt_node_string(char *string)
13136ff6d951SJohn Birrell {
13146ff6d951SJohn Birrell 	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
13156ff6d951SJohn Birrell 	dt_node_t *dnp;
13166ff6d951SJohn Birrell 
13176ff6d951SJohn Birrell 	if (string == NULL)
13186ff6d951SJohn Birrell 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
13196ff6d951SJohn Birrell 
13206ff6d951SJohn Birrell 	dnp = dt_node_alloc(DT_NODE_STRING);
13216ff6d951SJohn Birrell 	dnp->dn_op = DT_TOK_STRING;
13226ff6d951SJohn Birrell 	dnp->dn_string = string;
13238e648814SRui Paulo 	dt_node_type_assign(dnp, DT_STR_CTFP(dtp), DT_STR_TYPE(dtp), B_FALSE);
13246ff6d951SJohn Birrell 
13256ff6d951SJohn Birrell 	return (dnp);
13266ff6d951SJohn Birrell }
13276ff6d951SJohn Birrell 
13286ff6d951SJohn Birrell dt_node_t *
13296ff6d951SJohn Birrell dt_node_ident(char *name)
13306ff6d951SJohn Birrell {
13316ff6d951SJohn Birrell 	dt_ident_t *idp;
13326ff6d951SJohn Birrell 	dt_node_t *dnp;
13336ff6d951SJohn Birrell 
13346ff6d951SJohn Birrell 	if (name == NULL)
13356ff6d951SJohn Birrell 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
13366ff6d951SJohn Birrell 
13376ff6d951SJohn Birrell 	/*
13386ff6d951SJohn Birrell 	 * If the identifier is an inlined integer constant, then create an INT
13396ff6d951SJohn Birrell 	 * node that is a clone of the inline parse tree node and return that
13406ff6d951SJohn Birrell 	 * immediately, allowing this inline to be used in parsing contexts
13416ff6d951SJohn Birrell 	 * that require constant expressions (e.g. scalar array sizes).
13426ff6d951SJohn Birrell 	 */
13436ff6d951SJohn Birrell 	if ((idp = dt_idstack_lookup(&yypcb->pcb_globals, name)) != NULL &&
13446ff6d951SJohn Birrell 	    (idp->di_flags & DT_IDFLG_INLINE)) {
13456ff6d951SJohn Birrell 		dt_idnode_t *inp = idp->di_iarg;
13466ff6d951SJohn Birrell 
13476ff6d951SJohn Birrell 		if (inp->din_root != NULL &&
13486ff6d951SJohn Birrell 		    inp->din_root->dn_kind == DT_NODE_INT) {
13496ff6d951SJohn Birrell 			free(name);
13506ff6d951SJohn Birrell 
13516ff6d951SJohn Birrell 			dnp = dt_node_alloc(DT_NODE_INT);
13526ff6d951SJohn Birrell 			dnp->dn_op = DT_TOK_INT;
13536ff6d951SJohn Birrell 			dnp->dn_value = inp->din_root->dn_value;
13546ff6d951SJohn Birrell 			dt_node_type_propagate(inp->din_root, dnp);
13556ff6d951SJohn Birrell 
13566ff6d951SJohn Birrell 			return (dnp);
13576ff6d951SJohn Birrell 		}
13586ff6d951SJohn Birrell 	}
13596ff6d951SJohn Birrell 
13606ff6d951SJohn Birrell 	dnp = dt_node_alloc(DT_NODE_IDENT);
13616ff6d951SJohn Birrell 	dnp->dn_op = name[0] == '@' ? DT_TOK_AGG : DT_TOK_IDENT;
13626ff6d951SJohn Birrell 	dnp->dn_string = name;
13636ff6d951SJohn Birrell 
13646ff6d951SJohn Birrell 	return (dnp);
13656ff6d951SJohn Birrell }
13666ff6d951SJohn Birrell 
13676ff6d951SJohn Birrell /*
13686ff6d951SJohn Birrell  * Create an empty node of type corresponding to the given declaration.
13696ff6d951SJohn Birrell  * Explicit references to user types (C or D) are assigned the default
13706ff6d951SJohn Birrell  * stability; references to other types are _dtrace_typattr (Private).
13716ff6d951SJohn Birrell  */
13726ff6d951SJohn Birrell dt_node_t *
13736ff6d951SJohn Birrell dt_node_type(dt_decl_t *ddp)
13746ff6d951SJohn Birrell {
13756ff6d951SJohn Birrell 	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
13766ff6d951SJohn Birrell 	dtrace_typeinfo_t dtt;
13776ff6d951SJohn Birrell 	dt_node_t *dnp;
13786ff6d951SJohn Birrell 	char *name = NULL;
13796ff6d951SJohn Birrell 	int err;
13806ff6d951SJohn Birrell 
13816ff6d951SJohn Birrell 	/*
13826ff6d951SJohn Birrell 	 * If 'ddp' is NULL, we get a decl by popping the decl stack.  This
13836ff6d951SJohn Birrell 	 * form of dt_node_type() is used by parameter rules in dt_grammar.y.
13846ff6d951SJohn Birrell 	 */
13856ff6d951SJohn Birrell 	if (ddp == NULL)
13866ff6d951SJohn Birrell 		ddp = dt_decl_pop_param(&name);
13876ff6d951SJohn Birrell 
13886ff6d951SJohn Birrell 	err = dt_decl_type(ddp, &dtt);
13896ff6d951SJohn Birrell 	dt_decl_free(ddp);
13906ff6d951SJohn Birrell 
13916ff6d951SJohn Birrell 	if (err != 0) {
13926ff6d951SJohn Birrell 		free(name);
13936ff6d951SJohn Birrell 		longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
13946ff6d951SJohn Birrell 	}
13956ff6d951SJohn Birrell 
13966ff6d951SJohn Birrell 	dnp = dt_node_alloc(DT_NODE_TYPE);
13976ff6d951SJohn Birrell 	dnp->dn_op = DT_TOK_IDENT;
13986ff6d951SJohn Birrell 	dnp->dn_string = name;
13998e648814SRui Paulo 
14008e648814SRui Paulo 	dt_node_type_assign(dnp, dtt.dtt_ctfp, dtt.dtt_type, dtt.dtt_flags);
14016ff6d951SJohn Birrell 
14026ff6d951SJohn Birrell 	if (dtt.dtt_ctfp == dtp->dt_cdefs->dm_ctfp ||
14036ff6d951SJohn Birrell 	    dtt.dtt_ctfp == dtp->dt_ddefs->dm_ctfp)
14046ff6d951SJohn Birrell 		dt_node_attr_assign(dnp, _dtrace_defattr);
14056ff6d951SJohn Birrell 	else
14066ff6d951SJohn Birrell 		dt_node_attr_assign(dnp, _dtrace_typattr);
14076ff6d951SJohn Birrell 
14086ff6d951SJohn Birrell 	return (dnp);
14096ff6d951SJohn Birrell }
14106ff6d951SJohn Birrell 
14116ff6d951SJohn Birrell /*
14126ff6d951SJohn Birrell  * Create a type node corresponding to a varargs (...) parameter by just
14136ff6d951SJohn Birrell  * assigning it type CTF_ERR.  The decl processing code will handle this.
14146ff6d951SJohn Birrell  */
14156ff6d951SJohn Birrell dt_node_t *
14166ff6d951SJohn Birrell dt_node_vatype(void)
14176ff6d951SJohn Birrell {
14186ff6d951SJohn Birrell 	dt_node_t *dnp = dt_node_alloc(DT_NODE_TYPE);
14196ff6d951SJohn Birrell 
14206ff6d951SJohn Birrell 	dnp->dn_op = DT_TOK_IDENT;
14216ff6d951SJohn Birrell 	dnp->dn_ctfp = yypcb->pcb_hdl->dt_cdefs->dm_ctfp;
14226ff6d951SJohn Birrell 	dnp->dn_type = CTF_ERR;
14236ff6d951SJohn Birrell 	dnp->dn_attr = _dtrace_defattr;
14246ff6d951SJohn Birrell 
14256ff6d951SJohn Birrell 	return (dnp);
14266ff6d951SJohn Birrell }
14276ff6d951SJohn Birrell 
14286ff6d951SJohn Birrell /*
14296ff6d951SJohn Birrell  * Instantiate a decl using the contents of the current declaration stack.  As
14306ff6d951SJohn Birrell  * we do not currently permit decls to be initialized, this function currently
14316ff6d951SJohn Birrell  * returns NULL and no parse node is created.  When this function is called,
14326ff6d951SJohn Birrell  * the topmost scope's ds_ident pointer will be set to NULL (indicating no
14336ff6d951SJohn Birrell  * init_declarator rule was matched) or will point to the identifier to use.
14346ff6d951SJohn Birrell  */
14356ff6d951SJohn Birrell dt_node_t *
14366ff6d951SJohn Birrell dt_node_decl(void)
14376ff6d951SJohn Birrell {
14386ff6d951SJohn Birrell 	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
14396ff6d951SJohn Birrell 	dt_scope_t *dsp = &yypcb->pcb_dstack;
14406ff6d951SJohn Birrell 	dt_dclass_t class = dsp->ds_class;
14416ff6d951SJohn Birrell 	dt_decl_t *ddp = dt_decl_top();
14426ff6d951SJohn Birrell 
14436ff6d951SJohn Birrell 	dt_module_t *dmp;
14446ff6d951SJohn Birrell 	dtrace_typeinfo_t dtt;
14456ff6d951SJohn Birrell 	ctf_id_t type;
14466ff6d951SJohn Birrell 
14476ff6d951SJohn Birrell 	char n1[DT_TYPE_NAMELEN];
14486ff6d951SJohn Birrell 	char n2[DT_TYPE_NAMELEN];
14496ff6d951SJohn Birrell 
14506ff6d951SJohn Birrell 	if (dt_decl_type(ddp, &dtt) != 0)
14516ff6d951SJohn Birrell 		longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
14526ff6d951SJohn Birrell 
14536ff6d951SJohn Birrell 	/*
14546ff6d951SJohn Birrell 	 * If we have no declaration identifier, then this is either a spurious
14556ff6d951SJohn Birrell 	 * declaration of an intrinsic type (e.g. "extern int;") or declaration
14566ff6d951SJohn Birrell 	 * or redeclaration of a struct, union, or enum type or tag.
14576ff6d951SJohn Birrell 	 */
14586ff6d951SJohn Birrell 	if (dsp->ds_ident == NULL) {
14596ff6d951SJohn Birrell 		if (ddp->dd_kind != CTF_K_STRUCT &&
14606ff6d951SJohn Birrell 		    ddp->dd_kind != CTF_K_UNION && ddp->dd_kind != CTF_K_ENUM)
14616ff6d951SJohn Birrell 			xyerror(D_DECL_USELESS, "useless declaration\n");
14626ff6d951SJohn Birrell 
14636ff6d951SJohn Birrell 		dt_dprintf("type %s added as id %ld\n", dt_type_name(
14646ff6d951SJohn Birrell 		    ddp->dd_ctfp, ddp->dd_type, n1, sizeof (n1)), ddp->dd_type);
14656ff6d951SJohn Birrell 
14666ff6d951SJohn Birrell 		return (NULL);
14676ff6d951SJohn Birrell 	}
14686ff6d951SJohn Birrell 
14696ff6d951SJohn Birrell 	if (strchr(dsp->ds_ident, '`') != NULL) {
14706ff6d951SJohn Birrell 		xyerror(D_DECL_SCOPE, "D scoping operator may not be used in "
14716ff6d951SJohn Birrell 		    "a declaration name (%s)\n", dsp->ds_ident);
14726ff6d951SJohn Birrell 	}
14736ff6d951SJohn Birrell 
14746ff6d951SJohn Birrell 	/*
14756ff6d951SJohn Birrell 	 * If we are nested inside of a C include file, add the declaration to
14766ff6d951SJohn Birrell 	 * the C definition module; otherwise use the D definition module.
14776ff6d951SJohn Birrell 	 */
14786ff6d951SJohn Birrell 	if (yypcb->pcb_idepth != 0)
14796ff6d951SJohn Birrell 		dmp = dtp->dt_cdefs;
14806ff6d951SJohn Birrell 	else
14816ff6d951SJohn Birrell 		dmp = dtp->dt_ddefs;
14826ff6d951SJohn Birrell 
14836ff6d951SJohn Birrell 	/*
14846ff6d951SJohn Birrell 	 * If we see a global or static declaration of a function prototype,
14856ff6d951SJohn Birrell 	 * treat this as equivalent to a D extern declaration.
14866ff6d951SJohn Birrell 	 */
14876ff6d951SJohn Birrell 	if (ctf_type_kind(dtt.dtt_ctfp, dtt.dtt_type) == CTF_K_FUNCTION &&
14886ff6d951SJohn Birrell 	    (class == DT_DC_DEFAULT || class == DT_DC_STATIC))
14896ff6d951SJohn Birrell 		class = DT_DC_EXTERN;
14906ff6d951SJohn Birrell 
14916ff6d951SJohn Birrell 	switch (class) {
14926ff6d951SJohn Birrell 	case DT_DC_AUTO:
14936ff6d951SJohn Birrell 	case DT_DC_REGISTER:
14946ff6d951SJohn Birrell 	case DT_DC_STATIC:
14956ff6d951SJohn Birrell 		xyerror(D_DECL_BADCLASS, "specified storage class not "
14966ff6d951SJohn Birrell 		    "appropriate in D\n");
14976ff6d951SJohn Birrell 		/*NOTREACHED*/
14986ff6d951SJohn Birrell 
14996ff6d951SJohn Birrell 	case DT_DC_EXTERN: {
15006ff6d951SJohn Birrell 		dtrace_typeinfo_t ott;
15016ff6d951SJohn Birrell 		dtrace_syminfo_t dts;
15026ff6d951SJohn Birrell 		GElf_Sym sym;
15036ff6d951SJohn Birrell 
15046ff6d951SJohn Birrell 		int exists = dtrace_lookup_by_name(dtp,
15056ff6d951SJohn Birrell 		    dmp->dm_name, dsp->ds_ident, &sym, &dts) == 0;
15066ff6d951SJohn Birrell 
15076ff6d951SJohn Birrell 		if (exists && (dtrace_symbol_type(dtp, &sym, &dts, &ott) != 0 ||
15086ff6d951SJohn Birrell 		    ctf_type_cmp(dtt.dtt_ctfp, dtt.dtt_type,
15096ff6d951SJohn Birrell 		    ott.dtt_ctfp, ott.dtt_type) != 0)) {
15106ff6d951SJohn Birrell 			xyerror(D_DECL_IDRED, "identifier redeclared: %s`%s\n"
15116ff6d951SJohn Birrell 			    "\t current: %s\n\tprevious: %s\n",
15126ff6d951SJohn Birrell 			    dmp->dm_name, dsp->ds_ident,
15136ff6d951SJohn Birrell 			    dt_type_name(dtt.dtt_ctfp, dtt.dtt_type,
15146ff6d951SJohn Birrell 				n1, sizeof (n1)),
15156ff6d951SJohn Birrell 			    dt_type_name(ott.dtt_ctfp, ott.dtt_type,
15166ff6d951SJohn Birrell 				n2, sizeof (n2)));
15176ff6d951SJohn Birrell 		} else if (!exists && dt_module_extern(dtp, dmp,
15186ff6d951SJohn Birrell 		    dsp->ds_ident, &dtt) == NULL) {
15196ff6d951SJohn Birrell 			xyerror(D_UNKNOWN,
15206ff6d951SJohn Birrell 			    "failed to extern %s: %s\n", dsp->ds_ident,
15216ff6d951SJohn Birrell 			    dtrace_errmsg(dtp, dtrace_errno(dtp)));
15226ff6d951SJohn Birrell 		} else {
15236ff6d951SJohn Birrell 			dt_dprintf("extern %s`%s type=<%s>\n",
15246ff6d951SJohn Birrell 			    dmp->dm_name, dsp->ds_ident,
15256ff6d951SJohn Birrell 			    dt_type_name(dtt.dtt_ctfp, dtt.dtt_type,
15266ff6d951SJohn Birrell 				n1, sizeof (n1)));
15276ff6d951SJohn Birrell 		}
15286ff6d951SJohn Birrell 		break;
15296ff6d951SJohn Birrell 	}
15306ff6d951SJohn Birrell 
15316ff6d951SJohn Birrell 	case DT_DC_TYPEDEF:
15326ff6d951SJohn Birrell 		if (dt_idstack_lookup(&yypcb->pcb_globals, dsp->ds_ident)) {
15336ff6d951SJohn Birrell 			xyerror(D_DECL_IDRED, "global variable identifier "
15346ff6d951SJohn Birrell 			    "redeclared: %s\n", dsp->ds_ident);
15356ff6d951SJohn Birrell 		}
15366ff6d951SJohn Birrell 
15376ff6d951SJohn Birrell 		if (ctf_lookup_by_name(dmp->dm_ctfp,
15386ff6d951SJohn Birrell 		    dsp->ds_ident) != CTF_ERR) {
15396ff6d951SJohn Birrell 			xyerror(D_DECL_IDRED,
15406ff6d951SJohn Birrell 			    "typedef redeclared: %s\n", dsp->ds_ident);
15416ff6d951SJohn Birrell 		}
15426ff6d951SJohn Birrell 
15436ff6d951SJohn Birrell 		/*
15446ff6d951SJohn Birrell 		 * If the source type for the typedef is not defined in the
15456ff6d951SJohn Birrell 		 * target container or its parent, copy the type to the target
15466ff6d951SJohn Birrell 		 * container and reset dtt_ctfp and dtt_type to the copy.
15476ff6d951SJohn Birrell 		 */
15486ff6d951SJohn Birrell 		if (dtt.dtt_ctfp != dmp->dm_ctfp &&
15496ff6d951SJohn Birrell 		    dtt.dtt_ctfp != ctf_parent_file(dmp->dm_ctfp)) {
15506ff6d951SJohn Birrell 
15516ff6d951SJohn Birrell 			dtt.dtt_type = ctf_add_type(dmp->dm_ctfp,
15526ff6d951SJohn Birrell 			    dtt.dtt_ctfp, dtt.dtt_type);
15536ff6d951SJohn Birrell 			dtt.dtt_ctfp = dmp->dm_ctfp;
15546ff6d951SJohn Birrell 
15556ff6d951SJohn Birrell 			if (dtt.dtt_type == CTF_ERR ||
15566ff6d951SJohn Birrell 			    ctf_update(dtt.dtt_ctfp) == CTF_ERR) {
15576ff6d951SJohn Birrell 				xyerror(D_UNKNOWN, "failed to copy typedef %s "
15586ff6d951SJohn Birrell 				    "source type: %s\n", dsp->ds_ident,
15596ff6d951SJohn Birrell 				    ctf_errmsg(ctf_errno(dtt.dtt_ctfp)));
15606ff6d951SJohn Birrell 			}
15616ff6d951SJohn Birrell 		}
15626ff6d951SJohn Birrell 
15636ff6d951SJohn Birrell 		type = ctf_add_typedef(dmp->dm_ctfp,
15646ff6d951SJohn Birrell 		    CTF_ADD_ROOT, dsp->ds_ident, dtt.dtt_type);
15656ff6d951SJohn Birrell 
15666ff6d951SJohn Birrell 		if (type == CTF_ERR || ctf_update(dmp->dm_ctfp) == CTF_ERR) {
15676ff6d951SJohn Birrell 			xyerror(D_UNKNOWN, "failed to typedef %s: %s\n",
15686ff6d951SJohn Birrell 			    dsp->ds_ident, ctf_errmsg(ctf_errno(dmp->dm_ctfp)));
15696ff6d951SJohn Birrell 		}
15706ff6d951SJohn Birrell 
15716ff6d951SJohn Birrell 		dt_dprintf("typedef %s added as id %ld\n", dsp->ds_ident, type);
15726ff6d951SJohn Birrell 		break;
15736ff6d951SJohn Birrell 
15746ff6d951SJohn Birrell 	default: {
15756ff6d951SJohn Birrell 		ctf_encoding_t cte;
15766ff6d951SJohn Birrell 		dt_idhash_t *dhp;
15776ff6d951SJohn Birrell 		dt_ident_t *idp;
15786ff6d951SJohn Birrell 		dt_node_t idn;
15796ff6d951SJohn Birrell 		int assc, idkind;
15806ff6d951SJohn Birrell 		uint_t id, kind;
15816ff6d951SJohn Birrell 		ushort_t idflags;
15826ff6d951SJohn Birrell 
15836ff6d951SJohn Birrell 		switch (class) {
15846ff6d951SJohn Birrell 		case DT_DC_THIS:
15856ff6d951SJohn Birrell 			dhp = yypcb->pcb_locals;
15866ff6d951SJohn Birrell 			idflags = DT_IDFLG_LOCAL;
15876ff6d951SJohn Birrell 			idp = dt_idhash_lookup(dhp, dsp->ds_ident);
15886ff6d951SJohn Birrell 			break;
15896ff6d951SJohn Birrell 		case DT_DC_SELF:
15906ff6d951SJohn Birrell 			dhp = dtp->dt_tls;
15916ff6d951SJohn Birrell 			idflags = DT_IDFLG_TLS;
15926ff6d951SJohn Birrell 			idp = dt_idhash_lookup(dhp, dsp->ds_ident);
15936ff6d951SJohn Birrell 			break;
15946ff6d951SJohn Birrell 		default:
15956ff6d951SJohn Birrell 			dhp = dtp->dt_globals;
15966ff6d951SJohn Birrell 			idflags = 0;
15976ff6d951SJohn Birrell 			idp = dt_idstack_lookup(
15986ff6d951SJohn Birrell 			    &yypcb->pcb_globals, dsp->ds_ident);
15996ff6d951SJohn Birrell 			break;
16006ff6d951SJohn Birrell 		}
16016ff6d951SJohn Birrell 
16026ff6d951SJohn Birrell 		if (ddp->dd_kind == CTF_K_ARRAY && ddp->dd_node == NULL) {
16036ff6d951SJohn Birrell 			xyerror(D_DECL_ARRNULL,
16046ff6d951SJohn Birrell 			    "array declaration requires array dimension or "
16056ff6d951SJohn Birrell 			    "tuple signature: %s\n", dsp->ds_ident);
16066ff6d951SJohn Birrell 		}
16076ff6d951SJohn Birrell 
16086ff6d951SJohn Birrell 		if (idp != NULL && idp->di_gen == 0) {
16096ff6d951SJohn Birrell 			xyerror(D_DECL_IDRED, "built-in identifier "
16106ff6d951SJohn Birrell 			    "redeclared: %s\n", idp->di_name);
16116ff6d951SJohn Birrell 		}
16126ff6d951SJohn Birrell 
16136ff6d951SJohn Birrell 		if (dtrace_lookup_by_type(dtp, DTRACE_OBJ_CDEFS,
16146ff6d951SJohn Birrell 		    dsp->ds_ident, NULL) == 0 ||
16156ff6d951SJohn Birrell 		    dtrace_lookup_by_type(dtp, DTRACE_OBJ_DDEFS,
16166ff6d951SJohn Birrell 		    dsp->ds_ident, NULL) == 0) {
16176ff6d951SJohn Birrell 			xyerror(D_DECL_IDRED, "typedef identifier "
16186ff6d951SJohn Birrell 			    "redeclared: %s\n", dsp->ds_ident);
16196ff6d951SJohn Birrell 		}
16206ff6d951SJohn Birrell 
16216ff6d951SJohn Birrell 		/*
16226ff6d951SJohn Birrell 		 * Cache some attributes of the decl to make the rest of this
16236ff6d951SJohn Birrell 		 * code simpler: if the decl is an array which is subscripted
16246ff6d951SJohn Birrell 		 * by a type rather than an integer, then it's an associative
16256ff6d951SJohn Birrell 		 * array (assc).  We then expect to match either DT_IDENT_ARRAY
16266ff6d951SJohn Birrell 		 * for associative arrays or DT_IDENT_SCALAR for anything else.
16276ff6d951SJohn Birrell 		 */
16286ff6d951SJohn Birrell 		assc = ddp->dd_kind == CTF_K_ARRAY &&
16296ff6d951SJohn Birrell 		    ddp->dd_node->dn_kind == DT_NODE_TYPE;
16306ff6d951SJohn Birrell 
16316ff6d951SJohn Birrell 		idkind = assc ? DT_IDENT_ARRAY : DT_IDENT_SCALAR;
16326ff6d951SJohn Birrell 
16336ff6d951SJohn Birrell 		/*
16346ff6d951SJohn Birrell 		 * Create a fake dt_node_t on the stack so we can determine the
16356ff6d951SJohn Birrell 		 * type of any matching identifier by assigning to this node.
16366ff6d951SJohn Birrell 		 * If the pre-existing ident has its di_type set, propagate
16376ff6d951SJohn Birrell 		 * the type by hand so as not to trigger a prototype check for
16386ff6d951SJohn Birrell 		 * arrays (yet); otherwise we use dt_ident_cook() on the ident
16396ff6d951SJohn Birrell 		 * to ensure it is fully initialized before looking at it.
16406ff6d951SJohn Birrell 		 */
16416ff6d951SJohn Birrell 		bzero(&idn, sizeof (dt_node_t));
16426ff6d951SJohn Birrell 
16436ff6d951SJohn Birrell 		if (idp != NULL && idp->di_type != CTF_ERR)
16448e648814SRui Paulo 			dt_node_type_assign(&idn, idp->di_ctfp, idp->di_type,
16458e648814SRui Paulo 			    B_FALSE);
16466ff6d951SJohn Birrell 		else if (idp != NULL)
16476ff6d951SJohn Birrell 			(void) dt_ident_cook(&idn, idp, NULL);
16486ff6d951SJohn Birrell 
16496ff6d951SJohn Birrell 		if (assc) {
16506ff6d951SJohn Birrell 			if (class == DT_DC_THIS) {
16516ff6d951SJohn Birrell 				xyerror(D_DECL_LOCASSC, "associative arrays "
16526ff6d951SJohn Birrell 				    "may not be declared as local variables:"
16536ff6d951SJohn Birrell 				    " %s\n", dsp->ds_ident);
16546ff6d951SJohn Birrell 			}
16556ff6d951SJohn Birrell 
16566ff6d951SJohn Birrell 			if (dt_decl_type(ddp->dd_next, &dtt) != 0)
16576ff6d951SJohn Birrell 				longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
16586ff6d951SJohn Birrell 		}
16596ff6d951SJohn Birrell 
16606ff6d951SJohn Birrell 		if (idp != NULL && (idp->di_kind != idkind ||
16616ff6d951SJohn Birrell 		    ctf_type_cmp(dtt.dtt_ctfp, dtt.dtt_type,
16626ff6d951SJohn Birrell 		    idn.dn_ctfp, idn.dn_type) != 0)) {
16636ff6d951SJohn Birrell 			xyerror(D_DECL_IDRED, "identifier redeclared: %s\n"
16646ff6d951SJohn Birrell 			    "\t current: %s %s\n\tprevious: %s %s\n",
16656ff6d951SJohn Birrell 			    dsp->ds_ident, dt_idkind_name(idkind),
16666ff6d951SJohn Birrell 			    dt_type_name(dtt.dtt_ctfp,
16676ff6d951SJohn Birrell 			    dtt.dtt_type, n1, sizeof (n1)),
16686ff6d951SJohn Birrell 			    dt_idkind_name(idp->di_kind),
16696ff6d951SJohn Birrell 			    dt_node_type_name(&idn, n2, sizeof (n2)));
16706ff6d951SJohn Birrell 
16716ff6d951SJohn Birrell 		} else if (idp != NULL && assc) {
16726ff6d951SJohn Birrell 			const dt_idsig_t *isp = idp->di_data;
16736ff6d951SJohn Birrell 			dt_node_t *dnp = ddp->dd_node;
16746ff6d951SJohn Birrell 			int argc = 0;
16756ff6d951SJohn Birrell 
16766ff6d951SJohn Birrell 			for (; dnp != NULL; dnp = dnp->dn_list, argc++) {
16776ff6d951SJohn Birrell 				const dt_node_t *pnp = &isp->dis_args[argc];
16786ff6d951SJohn Birrell 
16796ff6d951SJohn Birrell 				if (argc >= isp->dis_argc)
16806ff6d951SJohn Birrell 					continue; /* tuple length mismatch */
16816ff6d951SJohn Birrell 
16826ff6d951SJohn Birrell 				if (ctf_type_cmp(dnp->dn_ctfp, dnp->dn_type,
16836ff6d951SJohn Birrell 				    pnp->dn_ctfp, pnp->dn_type) == 0)
16846ff6d951SJohn Birrell 					continue;
16856ff6d951SJohn Birrell 
16866ff6d951SJohn Birrell 				xyerror(D_DECL_IDRED,
16876ff6d951SJohn Birrell 				    "identifier redeclared: %s\n"
16886ff6d951SJohn Birrell 				    "\t current: %s, key #%d of type %s\n"
16896ff6d951SJohn Birrell 				    "\tprevious: %s, key #%d of type %s\n",
16906ff6d951SJohn Birrell 				    dsp->ds_ident,
16916ff6d951SJohn Birrell 				    dt_idkind_name(idkind), argc + 1,
16926ff6d951SJohn Birrell 				    dt_node_type_name(dnp, n1, sizeof (n1)),
16936ff6d951SJohn Birrell 				    dt_idkind_name(idp->di_kind), argc + 1,
16946ff6d951SJohn Birrell 				    dt_node_type_name(pnp, n2, sizeof (n2)));
16956ff6d951SJohn Birrell 			}
16966ff6d951SJohn Birrell 
16976ff6d951SJohn Birrell 			if (isp->dis_argc != argc) {
16986ff6d951SJohn Birrell 				xyerror(D_DECL_IDRED,
16996ff6d951SJohn Birrell 				    "identifier redeclared: %s\n"
17006ff6d951SJohn Birrell 				    "\t current: %s of %s, tuple length %d\n"
17016ff6d951SJohn Birrell 				    "\tprevious: %s of %s, tuple length %d\n",
17026ff6d951SJohn Birrell 				    dsp->ds_ident, dt_idkind_name(idkind),
17036ff6d951SJohn Birrell 				    dt_type_name(dtt.dtt_ctfp, dtt.dtt_type,
17046ff6d951SJohn Birrell 				    n1, sizeof (n1)), argc,
17056ff6d951SJohn Birrell 				    dt_idkind_name(idp->di_kind),
17066ff6d951SJohn Birrell 				    dt_node_type_name(&idn, n2, sizeof (n2)),
17076ff6d951SJohn Birrell 				    isp->dis_argc);
17086ff6d951SJohn Birrell 			}
17096ff6d951SJohn Birrell 
17106ff6d951SJohn Birrell 		} else if (idp == NULL) {
17116ff6d951SJohn Birrell 			type = ctf_type_resolve(dtt.dtt_ctfp, dtt.dtt_type);
17126ff6d951SJohn Birrell 			kind = ctf_type_kind(dtt.dtt_ctfp, type);
17136ff6d951SJohn Birrell 
17146ff6d951SJohn Birrell 			switch (kind) {
17156ff6d951SJohn Birrell 			case CTF_K_INTEGER:
17166ff6d951SJohn Birrell 				if (ctf_type_encoding(dtt.dtt_ctfp, type,
17176ff6d951SJohn Birrell 				    &cte) == 0 && IS_VOID(cte)) {
17186ff6d951SJohn Birrell 					xyerror(D_DECL_VOIDOBJ, "cannot have "
17196ff6d951SJohn Birrell 					    "void object: %s\n", dsp->ds_ident);
17206ff6d951SJohn Birrell 				}
17216ff6d951SJohn Birrell 				break;
17226ff6d951SJohn Birrell 			case CTF_K_STRUCT:
17236ff6d951SJohn Birrell 			case CTF_K_UNION:
17246ff6d951SJohn Birrell 				if (ctf_type_size(dtt.dtt_ctfp, type) != 0)
17256ff6d951SJohn Birrell 					break; /* proceed to declaring */
17266ff6d951SJohn Birrell 				/*FALLTHRU*/
17276ff6d951SJohn Birrell 			case CTF_K_FORWARD:
17286ff6d951SJohn Birrell 				xyerror(D_DECL_INCOMPLETE,
17296ff6d951SJohn Birrell 				    "incomplete struct/union/enum %s: %s\n",
17306ff6d951SJohn Birrell 				    dt_type_name(dtt.dtt_ctfp, dtt.dtt_type,
17316ff6d951SJohn Birrell 				    n1, sizeof (n1)), dsp->ds_ident);
17326ff6d951SJohn Birrell 				/*NOTREACHED*/
17336ff6d951SJohn Birrell 			}
17346ff6d951SJohn Birrell 
17356ff6d951SJohn Birrell 			if (dt_idhash_nextid(dhp, &id) == -1) {
17366ff6d951SJohn Birrell 				xyerror(D_ID_OFLOW, "cannot create %s: limit "
17376ff6d951SJohn Birrell 				    "on number of %s variables exceeded\n",
17386ff6d951SJohn Birrell 				    dsp->ds_ident, dt_idhash_name(dhp));
17396ff6d951SJohn Birrell 			}
17406ff6d951SJohn Birrell 
17416ff6d951SJohn Birrell 			dt_dprintf("declare %s %s variable %s, id=%u\n",
17426ff6d951SJohn Birrell 			    dt_idhash_name(dhp), dt_idkind_name(idkind),
17436ff6d951SJohn Birrell 			    dsp->ds_ident, id);
17446ff6d951SJohn Birrell 
17456ff6d951SJohn Birrell 			idp = dt_idhash_insert(dhp, dsp->ds_ident, idkind,
17466ff6d951SJohn Birrell 			    idflags | DT_IDFLG_WRITE | DT_IDFLG_DECL, id,
17476ff6d951SJohn Birrell 			    _dtrace_defattr, 0, assc ? &dt_idops_assc :
17486ff6d951SJohn Birrell 			    &dt_idops_thaw, NULL, dtp->dt_gen);
17496ff6d951SJohn Birrell 
17506ff6d951SJohn Birrell 			if (idp == NULL)
17516ff6d951SJohn Birrell 				longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
17526ff6d951SJohn Birrell 
17536ff6d951SJohn Birrell 			dt_ident_type_assign(idp, dtt.dtt_ctfp, dtt.dtt_type);
17546ff6d951SJohn Birrell 
17556ff6d951SJohn Birrell 			/*
17566ff6d951SJohn Birrell 			 * If we are declaring an associative array, use our
17576ff6d951SJohn Birrell 			 * fake parse node to cook the new assoc identifier.
17586ff6d951SJohn Birrell 			 * This will force the ident code to instantiate the
17596ff6d951SJohn Birrell 			 * array type signature corresponding to the list of
17606ff6d951SJohn Birrell 			 * types pointed to by ddp->dd_node.  We also reset
17616ff6d951SJohn Birrell 			 * the identifier's attributes based upon the result.
17626ff6d951SJohn Birrell 			 */
17636ff6d951SJohn Birrell 			if (assc) {
17646ff6d951SJohn Birrell 				idp->di_attr =
17656ff6d951SJohn Birrell 				    dt_ident_cook(&idn, idp, &ddp->dd_node);
17666ff6d951SJohn Birrell 			}
17676ff6d951SJohn Birrell 		}
17686ff6d951SJohn Birrell 	}
17696ff6d951SJohn Birrell 
17706ff6d951SJohn Birrell 	} /* end of switch */
17716ff6d951SJohn Birrell 
17726ff6d951SJohn Birrell 	free(dsp->ds_ident);
17736ff6d951SJohn Birrell 	dsp->ds_ident = NULL;
17746ff6d951SJohn Birrell 
17756ff6d951SJohn Birrell 	return (NULL);
17766ff6d951SJohn Birrell }
17776ff6d951SJohn Birrell 
17786ff6d951SJohn Birrell dt_node_t *
17796ff6d951SJohn Birrell dt_node_func(dt_node_t *dnp, dt_node_t *args)
17806ff6d951SJohn Birrell {
17816ff6d951SJohn Birrell 	dt_ident_t *idp;
17826ff6d951SJohn Birrell 
17836ff6d951SJohn Birrell 	if (dnp->dn_kind != DT_NODE_IDENT) {
17846ff6d951SJohn Birrell 		xyerror(D_FUNC_IDENT,
17856ff6d951SJohn Birrell 		    "function designator is not of function type\n");
17866ff6d951SJohn Birrell 	}
17876ff6d951SJohn Birrell 
17886ff6d951SJohn Birrell 	idp = dt_idstack_lookup(&yypcb->pcb_globals, dnp->dn_string);
17896ff6d951SJohn Birrell 
17906ff6d951SJohn Birrell 	if (idp == NULL) {
17916ff6d951SJohn Birrell 		xyerror(D_FUNC_UNDEF,
17926ff6d951SJohn Birrell 		    "undefined function name: %s\n", dnp->dn_string);
17936ff6d951SJohn Birrell 	}
17946ff6d951SJohn Birrell 
17956ff6d951SJohn Birrell 	if (idp->di_kind != DT_IDENT_FUNC &&
17966ff6d951SJohn Birrell 	    idp->di_kind != DT_IDENT_AGGFUNC &&
17976ff6d951SJohn Birrell 	    idp->di_kind != DT_IDENT_ACTFUNC) {
17986ff6d951SJohn Birrell 		xyerror(D_FUNC_IDKIND, "%s '%s' may not be referenced as a "
17996ff6d951SJohn Birrell 		    "function\n", dt_idkind_name(idp->di_kind), idp->di_name);
18006ff6d951SJohn Birrell 	}
18016ff6d951SJohn Birrell 
18026ff6d951SJohn Birrell 	free(dnp->dn_string);
18036ff6d951SJohn Birrell 	dnp->dn_string = NULL;
18046ff6d951SJohn Birrell 
18056ff6d951SJohn Birrell 	dnp->dn_kind = DT_NODE_FUNC;
18066ff6d951SJohn Birrell 	dnp->dn_flags &= ~DT_NF_COOKED;
18076ff6d951SJohn Birrell 	dnp->dn_ident = idp;
18086ff6d951SJohn Birrell 	dnp->dn_args = args;
18096ff6d951SJohn Birrell 	dnp->dn_list = NULL;
18106ff6d951SJohn Birrell 
18116ff6d951SJohn Birrell 	return (dnp);
18126ff6d951SJohn Birrell }
18136ff6d951SJohn Birrell 
18146ff6d951SJohn Birrell /*
18156ff6d951SJohn Birrell  * The offsetof() function is special because it takes a type name as an
18166ff6d951SJohn Birrell  * argument.  It does not actually construct its own node; after looking up the
18176ff6d951SJohn Birrell  * structure or union offset, we just return an integer node with the offset.
18186ff6d951SJohn Birrell  */
18196ff6d951SJohn Birrell dt_node_t *
18206ff6d951SJohn Birrell dt_node_offsetof(dt_decl_t *ddp, char *s)
18216ff6d951SJohn Birrell {
18226ff6d951SJohn Birrell 	dtrace_typeinfo_t dtt;
18236ff6d951SJohn Birrell 	dt_node_t dn;
18246ff6d951SJohn Birrell 	char *name;
18256ff6d951SJohn Birrell 	int err;
18266ff6d951SJohn Birrell 
18276ff6d951SJohn Birrell 	ctf_membinfo_t ctm;
18286ff6d951SJohn Birrell 	ctf_id_t type;
18296ff6d951SJohn Birrell 	uint_t kind;
18306ff6d951SJohn Birrell 
18316ff6d951SJohn Birrell 	name = alloca(strlen(s) + 1);
18326ff6d951SJohn Birrell 	(void) strcpy(name, s);
18336ff6d951SJohn Birrell 	free(s);
18346ff6d951SJohn Birrell 
18356ff6d951SJohn Birrell 	err = dt_decl_type(ddp, &dtt);
18366ff6d951SJohn Birrell 	dt_decl_free(ddp);
18376ff6d951SJohn Birrell 
18386ff6d951SJohn Birrell 	if (err != 0)
18396ff6d951SJohn Birrell 		longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
18406ff6d951SJohn Birrell 
18416ff6d951SJohn Birrell 	type = ctf_type_resolve(dtt.dtt_ctfp, dtt.dtt_type);
18426ff6d951SJohn Birrell 	kind = ctf_type_kind(dtt.dtt_ctfp, type);
18436ff6d951SJohn Birrell 
18446ff6d951SJohn Birrell 	if (kind != CTF_K_STRUCT && kind != CTF_K_UNION) {
18456ff6d951SJohn Birrell 		xyerror(D_OFFSETOF_TYPE,
18466ff6d951SJohn Birrell 		    "offsetof operand must be a struct or union type\n");
18476ff6d951SJohn Birrell 	}
18486ff6d951SJohn Birrell 
18496ff6d951SJohn Birrell 	if (ctf_member_info(dtt.dtt_ctfp, type, name, &ctm) == CTF_ERR) {
18506ff6d951SJohn Birrell 		xyerror(D_UNKNOWN, "failed to determine offset of %s: %s\n",
18516ff6d951SJohn Birrell 		    name, ctf_errmsg(ctf_errno(dtt.dtt_ctfp)));
18526ff6d951SJohn Birrell 	}
18536ff6d951SJohn Birrell 
18546ff6d951SJohn Birrell 	bzero(&dn, sizeof (dn));
18558e648814SRui Paulo 	dt_node_type_assign(&dn, dtt.dtt_ctfp, ctm.ctm_type, B_FALSE);
18566ff6d951SJohn Birrell 
18576ff6d951SJohn Birrell 	if (dn.dn_flags & DT_NF_BITFIELD) {
18586ff6d951SJohn Birrell 		xyerror(D_OFFSETOF_BITFIELD,
18596ff6d951SJohn Birrell 		    "cannot take offset of a bit-field: %s\n", name);
18606ff6d951SJohn Birrell 	}
18616ff6d951SJohn Birrell 
18626ff6d951SJohn Birrell 	return (dt_node_int(ctm.ctm_offset / NBBY));
18636ff6d951SJohn Birrell }
18646ff6d951SJohn Birrell 
18656ff6d951SJohn Birrell dt_node_t *
18666ff6d951SJohn Birrell dt_node_op1(int op, dt_node_t *cp)
18676ff6d951SJohn Birrell {
18686ff6d951SJohn Birrell 	dt_node_t *dnp;
18696ff6d951SJohn Birrell 
18706ff6d951SJohn Birrell 	if (cp->dn_kind == DT_NODE_INT) {
18716ff6d951SJohn Birrell 		switch (op) {
18726ff6d951SJohn Birrell 		case DT_TOK_INEG:
18736ff6d951SJohn Birrell 			/*
18746ff6d951SJohn Birrell 			 * If we're negating an unsigned integer, zero out any
18756ff6d951SJohn Birrell 			 * extra top bits to truncate the value to the size of
18766ff6d951SJohn Birrell 			 * the effective type determined by dt_node_int().
18776ff6d951SJohn Birrell 			 */
18786ff6d951SJohn Birrell 			cp->dn_value = -cp->dn_value;
18796ff6d951SJohn Birrell 			if (!(cp->dn_flags & DT_NF_SIGNED)) {
18806ff6d951SJohn Birrell 				cp->dn_value &= ~0ULL >>
18816ff6d951SJohn Birrell 				    (64 - dt_node_type_size(cp) * NBBY);
18826ff6d951SJohn Birrell 			}
18836ff6d951SJohn Birrell 			/*FALLTHRU*/
18846ff6d951SJohn Birrell 		case DT_TOK_IPOS:
18856ff6d951SJohn Birrell 			return (cp);
18866ff6d951SJohn Birrell 		case DT_TOK_BNEG:
18876ff6d951SJohn Birrell 			cp->dn_value = ~cp->dn_value;
18886ff6d951SJohn Birrell 			return (cp);
18896ff6d951SJohn Birrell 		case DT_TOK_LNEG:
18906ff6d951SJohn Birrell 			cp->dn_value = !cp->dn_value;
18916ff6d951SJohn Birrell 			return (cp);
18926ff6d951SJohn Birrell 		}
18936ff6d951SJohn Birrell 	}
18946ff6d951SJohn Birrell 
18956ff6d951SJohn Birrell 	/*
18966ff6d951SJohn Birrell 	 * If sizeof is applied to a type_name or string constant, we can
18976ff6d951SJohn Birrell 	 * transform 'cp' into an integer constant in the node construction
18986ff6d951SJohn Birrell 	 * pass so that it can then be used for arithmetic in this pass.
18996ff6d951SJohn Birrell 	 */
19006ff6d951SJohn Birrell 	if (op == DT_TOK_SIZEOF &&
19016ff6d951SJohn Birrell 	    (cp->dn_kind == DT_NODE_STRING || cp->dn_kind == DT_NODE_TYPE)) {
19026ff6d951SJohn Birrell 		dtrace_hdl_t *dtp = yypcb->pcb_hdl;
19036ff6d951SJohn Birrell 		size_t size = dt_node_type_size(cp);
19046ff6d951SJohn Birrell 
19056ff6d951SJohn Birrell 		if (size == 0) {
19066ff6d951SJohn Birrell 			xyerror(D_SIZEOF_TYPE, "cannot apply sizeof to an "
19076ff6d951SJohn Birrell 			    "operand of unknown size\n");
19086ff6d951SJohn Birrell 		}
19096ff6d951SJohn Birrell 
19106ff6d951SJohn Birrell 		dt_node_type_assign(cp, dtp->dt_ddefs->dm_ctfp,
19118e648814SRui Paulo 		    ctf_lookup_by_name(dtp->dt_ddefs->dm_ctfp, "size_t"),
19128e648814SRui Paulo 		    B_FALSE);
19136ff6d951SJohn Birrell 
19146ff6d951SJohn Birrell 		cp->dn_kind = DT_NODE_INT;
19156ff6d951SJohn Birrell 		cp->dn_op = DT_TOK_INT;
19166ff6d951SJohn Birrell 		cp->dn_value = size;
19176ff6d951SJohn Birrell 
19186ff6d951SJohn Birrell 		return (cp);
19196ff6d951SJohn Birrell 	}
19206ff6d951SJohn Birrell 
19216ff6d951SJohn Birrell 	dnp = dt_node_alloc(DT_NODE_OP1);
19226ff6d951SJohn Birrell 	assert(op <= USHRT_MAX);
19236ff6d951SJohn Birrell 	dnp->dn_op = (ushort_t)op;
19246ff6d951SJohn Birrell 	dnp->dn_child = cp;
19256ff6d951SJohn Birrell 
19266ff6d951SJohn Birrell 	return (dnp);
19276ff6d951SJohn Birrell }
19286ff6d951SJohn Birrell 
1929a98ff317SPedro F. Giffuni /*
1930a98ff317SPedro F. Giffuni  * If an integer constant is being cast to another integer type, we can
1931a98ff317SPedro F. Giffuni  * perform the cast as part of integer constant folding in this pass. We must
1932a98ff317SPedro F. Giffuni  * take action when the integer is being cast to a smaller type or if it is
1933a98ff317SPedro F. Giffuni  * changing signed-ness. If so, we first shift rp's bits bits high (losing
1934a98ff317SPedro F. Giffuni  * excess bits if narrowing) and then shift them down with either a logical
1935a98ff317SPedro F. Giffuni  * shift (unsigned) or arithmetic shift (signed).
1936a98ff317SPedro F. Giffuni  */
1937a98ff317SPedro F. Giffuni static void
1938a98ff317SPedro F. Giffuni dt_cast(dt_node_t *lp, dt_node_t *rp)
1939a98ff317SPedro F. Giffuni {
1940a98ff317SPedro F. Giffuni 	size_t srcsize = dt_node_type_size(rp);
1941a98ff317SPedro F. Giffuni 	size_t dstsize = dt_node_type_size(lp);
1942a98ff317SPedro F. Giffuni 
1943a98ff317SPedro F. Giffuni 	if (dstsize < srcsize) {
1944a98ff317SPedro F. Giffuni 		int n = (sizeof (uint64_t) - dstsize) * NBBY;
1945a98ff317SPedro F. Giffuni 		rp->dn_value <<= n;
1946a98ff317SPedro F. Giffuni 		rp->dn_value >>= n;
1947a98ff317SPedro F. Giffuni 	} else if (dstsize > srcsize) {
1948a98ff317SPedro F. Giffuni 		int n = (sizeof (uint64_t) - srcsize) * NBBY;
1949a98ff317SPedro F. Giffuni 		int s = (dstsize - srcsize) * NBBY;
1950a98ff317SPedro F. Giffuni 
1951a98ff317SPedro F. Giffuni 		rp->dn_value <<= n;
1952a98ff317SPedro F. Giffuni 		if (rp->dn_flags & DT_NF_SIGNED) {
1953a98ff317SPedro F. Giffuni 			rp->dn_value = (intmax_t)rp->dn_value >> s;
1954a98ff317SPedro F. Giffuni 			rp->dn_value >>= n - s;
1955a98ff317SPedro F. Giffuni 		} else {
1956a98ff317SPedro F. Giffuni 			rp->dn_value >>= n;
1957a98ff317SPedro F. Giffuni 		}
1958a98ff317SPedro F. Giffuni 	}
1959a98ff317SPedro F. Giffuni }
1960a98ff317SPedro F. Giffuni 
19616ff6d951SJohn Birrell dt_node_t *
19626ff6d951SJohn Birrell dt_node_op2(int op, dt_node_t *lp, dt_node_t *rp)
19636ff6d951SJohn Birrell {
19646ff6d951SJohn Birrell 	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
19656ff6d951SJohn Birrell 	dt_node_t *dnp;
19666ff6d951SJohn Birrell 
19676ff6d951SJohn Birrell 	/*
19686ff6d951SJohn Birrell 	 * First we check for operations that are illegal -- namely those that
19696ff6d951SJohn Birrell 	 * might result in integer division by zero, and abort if one is found.
19706ff6d951SJohn Birrell 	 */
19716ff6d951SJohn Birrell 	if (rp->dn_kind == DT_NODE_INT && rp->dn_value == 0 &&
19726ff6d951SJohn Birrell 	    (op == DT_TOK_MOD || op == DT_TOK_DIV ||
19736ff6d951SJohn Birrell 	    op == DT_TOK_MOD_EQ || op == DT_TOK_DIV_EQ))
19746ff6d951SJohn Birrell 		xyerror(D_DIV_ZERO, "expression contains division by zero\n");
19756ff6d951SJohn Birrell 
19766ff6d951SJohn Birrell 	/*
19776ff6d951SJohn Birrell 	 * If both children are immediate values, we can just perform inline
19786ff6d951SJohn Birrell 	 * calculation and return a new immediate node with the result.
19796ff6d951SJohn Birrell 	 */
19806ff6d951SJohn Birrell 	if (lp->dn_kind == DT_NODE_INT && rp->dn_kind == DT_NODE_INT) {
19816ff6d951SJohn Birrell 		uintmax_t l = lp->dn_value;
19826ff6d951SJohn Birrell 		uintmax_t r = rp->dn_value;
19836ff6d951SJohn Birrell 
19846ff6d951SJohn Birrell 		dnp = dt_node_int(0); /* allocate new integer node for result */
19856ff6d951SJohn Birrell 
19866ff6d951SJohn Birrell 		switch (op) {
19876ff6d951SJohn Birrell 		case DT_TOK_LOR:
19886ff6d951SJohn Birrell 			dnp->dn_value = l || r;
19896ff6d951SJohn Birrell 			dt_node_type_assign(dnp,
19908e648814SRui Paulo 			    DT_INT_CTFP(dtp), DT_INT_TYPE(dtp), B_FALSE);
19916ff6d951SJohn Birrell 			break;
19926ff6d951SJohn Birrell 		case DT_TOK_LXOR:
19936ff6d951SJohn Birrell 			dnp->dn_value = (l != 0) ^ (r != 0);
19946ff6d951SJohn Birrell 			dt_node_type_assign(dnp,
19958e648814SRui Paulo 			    DT_INT_CTFP(dtp), DT_INT_TYPE(dtp), B_FALSE);
19966ff6d951SJohn Birrell 			break;
19976ff6d951SJohn Birrell 		case DT_TOK_LAND:
19986ff6d951SJohn Birrell 			dnp->dn_value = l && r;
19996ff6d951SJohn Birrell 			dt_node_type_assign(dnp,
20008e648814SRui Paulo 			    DT_INT_CTFP(dtp), DT_INT_TYPE(dtp), B_FALSE);
20016ff6d951SJohn Birrell 			break;
20026ff6d951SJohn Birrell 		case DT_TOK_BOR:
20036ff6d951SJohn Birrell 			dnp->dn_value = l | r;
20046ff6d951SJohn Birrell 			dt_node_promote(lp, rp, dnp);
20056ff6d951SJohn Birrell 			break;
20066ff6d951SJohn Birrell 		case DT_TOK_XOR:
20076ff6d951SJohn Birrell 			dnp->dn_value = l ^ r;
20086ff6d951SJohn Birrell 			dt_node_promote(lp, rp, dnp);
20096ff6d951SJohn Birrell 			break;
20106ff6d951SJohn Birrell 		case DT_TOK_BAND:
20116ff6d951SJohn Birrell 			dnp->dn_value = l & r;
20126ff6d951SJohn Birrell 			dt_node_promote(lp, rp, dnp);
20136ff6d951SJohn Birrell 			break;
20146ff6d951SJohn Birrell 		case DT_TOK_EQU:
20156ff6d951SJohn Birrell 			dnp->dn_value = l == r;
20166ff6d951SJohn Birrell 			dt_node_type_assign(dnp,
20178e648814SRui Paulo 			    DT_INT_CTFP(dtp), DT_INT_TYPE(dtp), B_FALSE);
20186ff6d951SJohn Birrell 			break;
20196ff6d951SJohn Birrell 		case DT_TOK_NEQ:
20206ff6d951SJohn Birrell 			dnp->dn_value = l != r;
20216ff6d951SJohn Birrell 			dt_node_type_assign(dnp,
20228e648814SRui Paulo 			    DT_INT_CTFP(dtp), DT_INT_TYPE(dtp), B_FALSE);
20236ff6d951SJohn Birrell 			break;
20246ff6d951SJohn Birrell 		case DT_TOK_LT:
20256ff6d951SJohn Birrell 			dt_node_promote(lp, rp, dnp);
20266ff6d951SJohn Birrell 			if (dnp->dn_flags & DT_NF_SIGNED)
20276ff6d951SJohn Birrell 				dnp->dn_value = (intmax_t)l < (intmax_t)r;
20286ff6d951SJohn Birrell 			else
20296ff6d951SJohn Birrell 				dnp->dn_value = l < r;
20306ff6d951SJohn Birrell 			dt_node_type_assign(dnp,
20318e648814SRui Paulo 			    DT_INT_CTFP(dtp), DT_INT_TYPE(dtp), B_FALSE);
20326ff6d951SJohn Birrell 			break;
20336ff6d951SJohn Birrell 		case DT_TOK_LE:
20346ff6d951SJohn Birrell 			dt_node_promote(lp, rp, dnp);
20356ff6d951SJohn Birrell 			if (dnp->dn_flags & DT_NF_SIGNED)
20366ff6d951SJohn Birrell 				dnp->dn_value = (intmax_t)l <= (intmax_t)r;
20376ff6d951SJohn Birrell 			else
20386ff6d951SJohn Birrell 				dnp->dn_value = l <= r;
20396ff6d951SJohn Birrell 			dt_node_type_assign(dnp,
20408e648814SRui Paulo 			    DT_INT_CTFP(dtp), DT_INT_TYPE(dtp), B_FALSE);
20416ff6d951SJohn Birrell 			break;
20426ff6d951SJohn Birrell 		case DT_TOK_GT:
20436ff6d951SJohn Birrell 			dt_node_promote(lp, rp, dnp);
20446ff6d951SJohn Birrell 			if (dnp->dn_flags & DT_NF_SIGNED)
20456ff6d951SJohn Birrell 				dnp->dn_value = (intmax_t)l > (intmax_t)r;
20466ff6d951SJohn Birrell 			else
20476ff6d951SJohn Birrell 				dnp->dn_value = l > r;
20486ff6d951SJohn Birrell 			dt_node_type_assign(dnp,
20498e648814SRui Paulo 			    DT_INT_CTFP(dtp), DT_INT_TYPE(dtp), B_FALSE);
20506ff6d951SJohn Birrell 			break;
20516ff6d951SJohn Birrell 		case DT_TOK_GE:
20526ff6d951SJohn Birrell 			dt_node_promote(lp, rp, dnp);
20536ff6d951SJohn Birrell 			if (dnp->dn_flags & DT_NF_SIGNED)
20546ff6d951SJohn Birrell 				dnp->dn_value = (intmax_t)l >= (intmax_t)r;
20556ff6d951SJohn Birrell 			else
20566ff6d951SJohn Birrell 				dnp->dn_value = l >= r;
20576ff6d951SJohn Birrell 			dt_node_type_assign(dnp,
20588e648814SRui Paulo 			    DT_INT_CTFP(dtp), DT_INT_TYPE(dtp), B_FALSE);
20596ff6d951SJohn Birrell 			break;
20606ff6d951SJohn Birrell 		case DT_TOK_LSH:
20616ff6d951SJohn Birrell 			dnp->dn_value = l << r;
20626ff6d951SJohn Birrell 			dt_node_type_propagate(lp, dnp);
20636ff6d951SJohn Birrell 			dt_node_attr_assign(rp,
20646ff6d951SJohn Birrell 			    dt_attr_min(lp->dn_attr, rp->dn_attr));
20656ff6d951SJohn Birrell 			break;
20666ff6d951SJohn Birrell 		case DT_TOK_RSH:
20676ff6d951SJohn Birrell 			dnp->dn_value = l >> r;
20686ff6d951SJohn Birrell 			dt_node_type_propagate(lp, dnp);
20696ff6d951SJohn Birrell 			dt_node_attr_assign(rp,
20706ff6d951SJohn Birrell 			    dt_attr_min(lp->dn_attr, rp->dn_attr));
20716ff6d951SJohn Birrell 			break;
20726ff6d951SJohn Birrell 		case DT_TOK_ADD:
20736ff6d951SJohn Birrell 			dnp->dn_value = l + r;
20746ff6d951SJohn Birrell 			dt_node_promote(lp, rp, dnp);
20756ff6d951SJohn Birrell 			break;
20766ff6d951SJohn Birrell 		case DT_TOK_SUB:
20776ff6d951SJohn Birrell 			dnp->dn_value = l - r;
20786ff6d951SJohn Birrell 			dt_node_promote(lp, rp, dnp);
20796ff6d951SJohn Birrell 			break;
20806ff6d951SJohn Birrell 		case DT_TOK_MUL:
20816ff6d951SJohn Birrell 			dnp->dn_value = l * r;
20826ff6d951SJohn Birrell 			dt_node_promote(lp, rp, dnp);
20836ff6d951SJohn Birrell 			break;
20846ff6d951SJohn Birrell 		case DT_TOK_DIV:
20856ff6d951SJohn Birrell 			dt_node_promote(lp, rp, dnp);
20866ff6d951SJohn Birrell 			if (dnp->dn_flags & DT_NF_SIGNED)
20876ff6d951SJohn Birrell 				dnp->dn_value = (intmax_t)l / (intmax_t)r;
20886ff6d951SJohn Birrell 			else
20896ff6d951SJohn Birrell 				dnp->dn_value = l / r;
20906ff6d951SJohn Birrell 			break;
20916ff6d951SJohn Birrell 		case DT_TOK_MOD:
20926ff6d951SJohn Birrell 			dt_node_promote(lp, rp, dnp);
20936ff6d951SJohn Birrell 			if (dnp->dn_flags & DT_NF_SIGNED)
20946ff6d951SJohn Birrell 				dnp->dn_value = (intmax_t)l % (intmax_t)r;
20956ff6d951SJohn Birrell 			else
20966ff6d951SJohn Birrell 				dnp->dn_value = l % r;
20976ff6d951SJohn Birrell 			break;
20986ff6d951SJohn Birrell 		default:
20996ff6d951SJohn Birrell 			dt_node_free(dnp);
21006ff6d951SJohn Birrell 			dnp = NULL;
21016ff6d951SJohn Birrell 		}
21026ff6d951SJohn Birrell 
21036ff6d951SJohn Birrell 		if (dnp != NULL) {
21046ff6d951SJohn Birrell 			dt_node_free(lp);
21056ff6d951SJohn Birrell 			dt_node_free(rp);
21066ff6d951SJohn Birrell 			return (dnp);
21076ff6d951SJohn Birrell 		}
21086ff6d951SJohn Birrell 	}
21096ff6d951SJohn Birrell 
21106ff6d951SJohn Birrell 	if (op == DT_TOK_LPAR && rp->dn_kind == DT_NODE_INT &&
21116ff6d951SJohn Birrell 	    dt_node_is_integer(lp)) {
2112a98ff317SPedro F. Giffuni 		dt_cast(lp, rp);
21136ff6d951SJohn Birrell 		dt_node_type_propagate(lp, rp);
21146ff6d951SJohn Birrell 		dt_node_attr_assign(rp, dt_attr_min(lp->dn_attr, rp->dn_attr));
21156ff6d951SJohn Birrell 		dt_node_free(lp);
21166ff6d951SJohn Birrell 
21176ff6d951SJohn Birrell 		return (rp);
21186ff6d951SJohn Birrell 	}
21196ff6d951SJohn Birrell 
21206ff6d951SJohn Birrell 	/*
21216ff6d951SJohn Birrell 	 * If no immediate optimizations are available, create an new OP2 node
21226ff6d951SJohn Birrell 	 * and glue the left and right children into place and return.
21236ff6d951SJohn Birrell 	 */
21246ff6d951SJohn Birrell 	dnp = dt_node_alloc(DT_NODE_OP2);
21256ff6d951SJohn Birrell 	assert(op <= USHRT_MAX);
21266ff6d951SJohn Birrell 	dnp->dn_op = (ushort_t)op;
21276ff6d951SJohn Birrell 	dnp->dn_left = lp;
21286ff6d951SJohn Birrell 	dnp->dn_right = rp;
21296ff6d951SJohn Birrell 
21306ff6d951SJohn Birrell 	return (dnp);
21316ff6d951SJohn Birrell }
21326ff6d951SJohn Birrell 
21336ff6d951SJohn Birrell dt_node_t *
21346ff6d951SJohn Birrell dt_node_op3(dt_node_t *expr, dt_node_t *lp, dt_node_t *rp)
21356ff6d951SJohn Birrell {
21366ff6d951SJohn Birrell 	dt_node_t *dnp;
21376ff6d951SJohn Birrell 
21386ff6d951SJohn Birrell 	if (expr->dn_kind == DT_NODE_INT)
21396ff6d951SJohn Birrell 		return (expr->dn_value != 0 ? lp : rp);
21406ff6d951SJohn Birrell 
21416ff6d951SJohn Birrell 	dnp = dt_node_alloc(DT_NODE_OP3);
21426ff6d951SJohn Birrell 	dnp->dn_op = DT_TOK_QUESTION;
21436ff6d951SJohn Birrell 	dnp->dn_expr = expr;
21446ff6d951SJohn Birrell 	dnp->dn_left = lp;
21456ff6d951SJohn Birrell 	dnp->dn_right = rp;
21466ff6d951SJohn Birrell 
21476ff6d951SJohn Birrell 	return (dnp);
21486ff6d951SJohn Birrell }
21496ff6d951SJohn Birrell 
21506ff6d951SJohn Birrell dt_node_t *
21516ff6d951SJohn Birrell dt_node_statement(dt_node_t *expr)
21526ff6d951SJohn Birrell {
21536ff6d951SJohn Birrell 	dt_node_t *dnp;
21546ff6d951SJohn Birrell 
21556ff6d951SJohn Birrell 	if (expr->dn_kind == DT_NODE_AGG)
21566ff6d951SJohn Birrell 		return (expr);
21576ff6d951SJohn Birrell 
21586ff6d951SJohn Birrell 	if (expr->dn_kind == DT_NODE_FUNC &&
21596ff6d951SJohn Birrell 	    expr->dn_ident->di_kind == DT_IDENT_ACTFUNC)
21606ff6d951SJohn Birrell 		dnp = dt_node_alloc(DT_NODE_DFUNC);
21616ff6d951SJohn Birrell 	else
21626ff6d951SJohn Birrell 		dnp = dt_node_alloc(DT_NODE_DEXPR);
21636ff6d951SJohn Birrell 
21646ff6d951SJohn Birrell 	dnp->dn_expr = expr;
21656ff6d951SJohn Birrell 	return (dnp);
21666ff6d951SJohn Birrell }
21676ff6d951SJohn Birrell 
21686ff6d951SJohn Birrell dt_node_t *
2169650f66acSMark Johnston dt_node_if(dt_node_t *pred, dt_node_t *acts, dt_node_t *else_acts)
2170650f66acSMark Johnston {
2171650f66acSMark Johnston 	dt_node_t *dnp = dt_node_alloc(DT_NODE_IF);
2172650f66acSMark Johnston 	dnp->dn_conditional = pred;
2173650f66acSMark Johnston 	dnp->dn_body = acts;
2174650f66acSMark Johnston 	dnp->dn_alternate_body = else_acts;
2175650f66acSMark Johnston 
2176650f66acSMark Johnston 	return (dnp);
2177650f66acSMark Johnston }
2178650f66acSMark Johnston 
2179650f66acSMark Johnston dt_node_t *
21806ff6d951SJohn Birrell dt_node_pdesc_by_name(char *spec)
21816ff6d951SJohn Birrell {
21826ff6d951SJohn Birrell 	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
21836ff6d951SJohn Birrell 	dt_node_t *dnp;
21846ff6d951SJohn Birrell 
21856ff6d951SJohn Birrell 	if (spec == NULL)
21866ff6d951SJohn Birrell 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
21876ff6d951SJohn Birrell 
21886ff6d951SJohn Birrell 	dnp = dt_node_alloc(DT_NODE_PDESC);
21896ff6d951SJohn Birrell 	dnp->dn_spec = spec;
21906ff6d951SJohn Birrell 	dnp->dn_desc = malloc(sizeof (dtrace_probedesc_t));
21916ff6d951SJohn Birrell 
21926ff6d951SJohn Birrell 	if (dnp->dn_desc == NULL)
21936ff6d951SJohn Birrell 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
21946ff6d951SJohn Birrell 
21956ff6d951SJohn Birrell 	if (dtrace_xstr2desc(dtp, yypcb->pcb_pspec, dnp->dn_spec,
21966ff6d951SJohn Birrell 	    yypcb->pcb_sargc, yypcb->pcb_sargv, dnp->dn_desc) != 0) {
21976ff6d951SJohn Birrell 		xyerror(D_PDESC_INVAL, "invalid probe description \"%s\": %s\n",
21986ff6d951SJohn Birrell 		    dnp->dn_spec, dtrace_errmsg(dtp, dtrace_errno(dtp)));
21996ff6d951SJohn Birrell 	}
22006ff6d951SJohn Birrell 
22016ff6d951SJohn Birrell 	free(dnp->dn_spec);
22026ff6d951SJohn Birrell 	dnp->dn_spec = NULL;
22036ff6d951SJohn Birrell 
22046ff6d951SJohn Birrell 	return (dnp);
22056ff6d951SJohn Birrell }
22066ff6d951SJohn Birrell 
22076ff6d951SJohn Birrell dt_node_t *
22086ff6d951SJohn Birrell dt_node_pdesc_by_id(uintmax_t id)
22096ff6d951SJohn Birrell {
22106ff6d951SJohn Birrell 	static const char *const names[] = {
22116ff6d951SJohn Birrell 		"providers", "modules", "functions"
22126ff6d951SJohn Birrell 	};
22136ff6d951SJohn Birrell 
22146ff6d951SJohn Birrell 	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
22156ff6d951SJohn Birrell 	dt_node_t *dnp = dt_node_alloc(DT_NODE_PDESC);
22166ff6d951SJohn Birrell 
22176ff6d951SJohn Birrell 	if ((dnp->dn_desc = malloc(sizeof (dtrace_probedesc_t))) == NULL)
22186ff6d951SJohn Birrell 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
22196ff6d951SJohn Birrell 
22206ff6d951SJohn Birrell 	if (id > UINT_MAX) {
22216ff6d951SJohn Birrell 		xyerror(D_PDESC_INVAL, "identifier %llu exceeds maximum "
22226ff6d951SJohn Birrell 		    "probe id\n", (u_longlong_t)id);
22236ff6d951SJohn Birrell 	}
22246ff6d951SJohn Birrell 
22256ff6d951SJohn Birrell 	if (yypcb->pcb_pspec != DTRACE_PROBESPEC_NAME) {
22266ff6d951SJohn Birrell 		xyerror(D_PDESC_INVAL, "probe identifier %llu not permitted "
22276ff6d951SJohn Birrell 		    "when specifying %s\n", (u_longlong_t)id,
22286ff6d951SJohn Birrell 		    names[yypcb->pcb_pspec]);
22296ff6d951SJohn Birrell 	}
22306ff6d951SJohn Birrell 
22316ff6d951SJohn Birrell 	if (dtrace_id2desc(dtp, (dtrace_id_t)id, dnp->dn_desc) != 0) {
22326ff6d951SJohn Birrell 		xyerror(D_PDESC_INVAL, "invalid probe identifier %llu: %s\n",
22336ff6d951SJohn Birrell 		    (u_longlong_t)id, dtrace_errmsg(dtp, dtrace_errno(dtp)));
22346ff6d951SJohn Birrell 	}
22356ff6d951SJohn Birrell 
22366ff6d951SJohn Birrell 	return (dnp);
22376ff6d951SJohn Birrell }
22386ff6d951SJohn Birrell 
22396ff6d951SJohn Birrell dt_node_t *
22406ff6d951SJohn Birrell dt_node_clause(dt_node_t *pdescs, dt_node_t *pred, dt_node_t *acts)
22416ff6d951SJohn Birrell {
22426ff6d951SJohn Birrell 	dt_node_t *dnp = dt_node_alloc(DT_NODE_CLAUSE);
22436ff6d951SJohn Birrell 
22446ff6d951SJohn Birrell 	dnp->dn_pdescs = pdescs;
22456ff6d951SJohn Birrell 	dnp->dn_pred = pred;
22466ff6d951SJohn Birrell 	dnp->dn_acts = acts;
22476ff6d951SJohn Birrell 
22486ff6d951SJohn Birrell 	return (dnp);
22496ff6d951SJohn Birrell }
22506ff6d951SJohn Birrell 
22516ff6d951SJohn Birrell dt_node_t *
22526ff6d951SJohn Birrell dt_node_inline(dt_node_t *expr)
22536ff6d951SJohn Birrell {
22546ff6d951SJohn Birrell 	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
22556ff6d951SJohn Birrell 	dt_scope_t *dsp = &yypcb->pcb_dstack;
22566ff6d951SJohn Birrell 	dt_decl_t *ddp = dt_decl_top();
22576ff6d951SJohn Birrell 
22586ff6d951SJohn Birrell 	char n[DT_TYPE_NAMELEN];
22596ff6d951SJohn Birrell 	dtrace_typeinfo_t dtt;
22606ff6d951SJohn Birrell 
22616ff6d951SJohn Birrell 	dt_ident_t *idp, *rdp;
22626ff6d951SJohn Birrell 	dt_idnode_t *inp;
22636ff6d951SJohn Birrell 	dt_node_t *dnp;
22646ff6d951SJohn Birrell 
22656ff6d951SJohn Birrell 	if (dt_decl_type(ddp, &dtt) != 0)
22666ff6d951SJohn Birrell 		longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
22676ff6d951SJohn Birrell 
22686ff6d951SJohn Birrell 	if (dsp->ds_class != DT_DC_DEFAULT) {
22696ff6d951SJohn Birrell 		xyerror(D_DECL_BADCLASS, "specified storage class not "
22706ff6d951SJohn Birrell 		    "appropriate for inline declaration\n");
22716ff6d951SJohn Birrell 	}
22726ff6d951SJohn Birrell 
22736ff6d951SJohn Birrell 	if (dsp->ds_ident == NULL)
22746ff6d951SJohn Birrell 		xyerror(D_DECL_USELESS, "inline declaration requires a name\n");
22756ff6d951SJohn Birrell 
22766ff6d951SJohn Birrell 	if ((idp = dt_idstack_lookup(
22776ff6d951SJohn Birrell 	    &yypcb->pcb_globals, dsp->ds_ident)) != NULL) {
22786ff6d951SJohn Birrell 		xyerror(D_DECL_IDRED, "identifier redefined: %s\n\t current: "
22796ff6d951SJohn Birrell 		    "inline definition\n\tprevious: %s %s\n",
22806ff6d951SJohn Birrell 		    idp->di_name, dt_idkind_name(idp->di_kind),
22816ff6d951SJohn Birrell 		    (idp->di_flags & DT_IDFLG_INLINE) ? "inline" : "");
22826ff6d951SJohn Birrell 	}
22836ff6d951SJohn Birrell 
22846ff6d951SJohn Birrell 	/*
22856ff6d951SJohn Birrell 	 * If we are declaring an inlined array, verify that we have a tuple
22866ff6d951SJohn Birrell 	 * signature, and then recompute 'dtt' as the array's value type.
22876ff6d951SJohn Birrell 	 */
22886ff6d951SJohn Birrell 	if (ddp->dd_kind == CTF_K_ARRAY) {
22896ff6d951SJohn Birrell 		if (ddp->dd_node == NULL) {
22906ff6d951SJohn Birrell 			xyerror(D_DECL_ARRNULL, "inline declaration requires "
22916ff6d951SJohn Birrell 			    "array tuple signature: %s\n", dsp->ds_ident);
22926ff6d951SJohn Birrell 		}
22936ff6d951SJohn Birrell 
22946ff6d951SJohn Birrell 		if (ddp->dd_node->dn_kind != DT_NODE_TYPE) {
22956ff6d951SJohn Birrell 			xyerror(D_DECL_ARRNULL, "inline declaration cannot be "
22966ff6d951SJohn Birrell 			    "of scalar array type: %s\n", dsp->ds_ident);
22976ff6d951SJohn Birrell 		}
22986ff6d951SJohn Birrell 
22996ff6d951SJohn Birrell 		if (dt_decl_type(ddp->dd_next, &dtt) != 0)
23006ff6d951SJohn Birrell 			longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
23016ff6d951SJohn Birrell 	}
23026ff6d951SJohn Birrell 
23036ff6d951SJohn Birrell 	/*
23046ff6d951SJohn Birrell 	 * If the inline identifier is not defined, then create it with the
23056ff6d951SJohn Birrell 	 * orphan flag set.  We do not insert the identifier into dt_globals
23066ff6d951SJohn Birrell 	 * until we have successfully cooked the right-hand expression, below.
23076ff6d951SJohn Birrell 	 */
23086ff6d951SJohn Birrell 	dnp = dt_node_alloc(DT_NODE_INLINE);
23098e648814SRui Paulo 	dt_node_type_assign(dnp, dtt.dtt_ctfp, dtt.dtt_type, B_FALSE);
23106ff6d951SJohn Birrell 	dt_node_attr_assign(dnp, _dtrace_defattr);
23116ff6d951SJohn Birrell 
23126ff6d951SJohn Birrell 	if (dt_node_is_void(dnp)) {
23136ff6d951SJohn Birrell 		xyerror(D_DECL_VOIDOBJ,
23146ff6d951SJohn Birrell 		    "cannot declare void inline: %s\n", dsp->ds_ident);
23156ff6d951SJohn Birrell 	}
23166ff6d951SJohn Birrell 
23176ff6d951SJohn Birrell 	if (ctf_type_kind(dnp->dn_ctfp, ctf_type_resolve(
23186ff6d951SJohn Birrell 	    dnp->dn_ctfp, dnp->dn_type)) == CTF_K_FORWARD) {
23196ff6d951SJohn Birrell 		xyerror(D_DECL_INCOMPLETE,
23206ff6d951SJohn Birrell 		    "incomplete struct/union/enum %s: %s\n",
23216ff6d951SJohn Birrell 		    dt_node_type_name(dnp, n, sizeof (n)), dsp->ds_ident);
23226ff6d951SJohn Birrell 	}
23236ff6d951SJohn Birrell 
23246ff6d951SJohn Birrell 	if ((inp = malloc(sizeof (dt_idnode_t))) == NULL)
23256ff6d951SJohn Birrell 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
23266ff6d951SJohn Birrell 
23276ff6d951SJohn Birrell 	bzero(inp, sizeof (dt_idnode_t));
23286ff6d951SJohn Birrell 
23296ff6d951SJohn Birrell 	idp = dnp->dn_ident = dt_ident_create(dsp->ds_ident,
23306ff6d951SJohn Birrell 	    ddp->dd_kind == CTF_K_ARRAY ? DT_IDENT_ARRAY : DT_IDENT_SCALAR,
23316ff6d951SJohn Birrell 	    DT_IDFLG_INLINE | DT_IDFLG_REF | DT_IDFLG_DECL | DT_IDFLG_ORPHAN, 0,
23326ff6d951SJohn Birrell 	    _dtrace_defattr, 0, &dt_idops_inline, inp, dtp->dt_gen);
23336ff6d951SJohn Birrell 
23346ff6d951SJohn Birrell 	if (idp == NULL) {
23356ff6d951SJohn Birrell 		free(inp);
23366ff6d951SJohn Birrell 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
23376ff6d951SJohn Birrell 	}
23386ff6d951SJohn Birrell 
23396ff6d951SJohn Birrell 	/*
23406ff6d951SJohn Birrell 	 * If we're inlining an associative array, create a private identifier
23416ff6d951SJohn Birrell 	 * hash containing the named parameters and store it in inp->din_hash.
23426ff6d951SJohn Birrell 	 * We then push this hash on to the top of the pcb_globals stack.
23436ff6d951SJohn Birrell 	 */
23446ff6d951SJohn Birrell 	if (ddp->dd_kind == CTF_K_ARRAY) {
23456ff6d951SJohn Birrell 		dt_idnode_t *pinp;
23466ff6d951SJohn Birrell 		dt_ident_t *pidp;
23476ff6d951SJohn Birrell 		dt_node_t *pnp;
23486ff6d951SJohn Birrell 		uint_t i = 0;
23496ff6d951SJohn Birrell 
23506ff6d951SJohn Birrell 		for (pnp = ddp->dd_node; pnp != NULL; pnp = pnp->dn_list)
23516ff6d951SJohn Birrell 			i++; /* count up parameters for din_argv[] */
23526ff6d951SJohn Birrell 
23536ff6d951SJohn Birrell 		inp->din_hash = dt_idhash_create("inline args", NULL, 0, 0);
23546ff6d951SJohn Birrell 		inp->din_argv = calloc(i, sizeof (dt_ident_t *));
23556ff6d951SJohn Birrell 
23566ff6d951SJohn Birrell 		if (inp->din_hash == NULL || inp->din_argv == NULL)
23576ff6d951SJohn Birrell 			longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
23586ff6d951SJohn Birrell 
23596ff6d951SJohn Birrell 		/*
23606ff6d951SJohn Birrell 		 * Create an identifier for each parameter as a scalar inline,
23616ff6d951SJohn Birrell 		 * and store it in din_hash and in position in din_argv[].  The
23626ff6d951SJohn Birrell 		 * parameter identifiers also use dt_idops_inline, but we leave
23636ff6d951SJohn Birrell 		 * the dt_idnode_t argument 'pinp' zeroed.  This will be filled
23646ff6d951SJohn Birrell 		 * in by the code generation pass with references to the args.
23656ff6d951SJohn Birrell 		 */
23666ff6d951SJohn Birrell 		for (i = 0, pnp = ddp->dd_node;
23676ff6d951SJohn Birrell 		    pnp != NULL; pnp = pnp->dn_list, i++) {
23686ff6d951SJohn Birrell 
23696ff6d951SJohn Birrell 			if (pnp->dn_string == NULL)
23706ff6d951SJohn Birrell 				continue; /* ignore anonymous parameters */
23716ff6d951SJohn Birrell 
23726ff6d951SJohn Birrell 			if ((pinp = malloc(sizeof (dt_idnode_t))) == NULL)
23736ff6d951SJohn Birrell 				longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
23746ff6d951SJohn Birrell 
23756ff6d951SJohn Birrell 			pidp = dt_idhash_insert(inp->din_hash, pnp->dn_string,
23766ff6d951SJohn Birrell 			    DT_IDENT_SCALAR, DT_IDFLG_DECL | DT_IDFLG_INLINE, 0,
23776ff6d951SJohn Birrell 			    _dtrace_defattr, 0, &dt_idops_inline,
23786ff6d951SJohn Birrell 			    pinp, dtp->dt_gen);
23796ff6d951SJohn Birrell 
23806ff6d951SJohn Birrell 			if (pidp == NULL) {
23816ff6d951SJohn Birrell 				free(pinp);
23826ff6d951SJohn Birrell 				longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
23836ff6d951SJohn Birrell 			}
23846ff6d951SJohn Birrell 
23856ff6d951SJohn Birrell 			inp->din_argv[i] = pidp;
23866ff6d951SJohn Birrell 			bzero(pinp, sizeof (dt_idnode_t));
23876ff6d951SJohn Birrell 			dt_ident_type_assign(pidp, pnp->dn_ctfp, pnp->dn_type);
23886ff6d951SJohn Birrell 		}
23896ff6d951SJohn Birrell 
23906ff6d951SJohn Birrell 		dt_idstack_push(&yypcb->pcb_globals, inp->din_hash);
23916ff6d951SJohn Birrell 	}
23926ff6d951SJohn Birrell 
23936ff6d951SJohn Birrell 	/*
23946ff6d951SJohn Birrell 	 * Unlike most constructors, we need to explicitly cook the right-hand
23956ff6d951SJohn Birrell 	 * side of the inline definition immediately to prevent recursion.  If
23966ff6d951SJohn Birrell 	 * the right-hand side uses the inline itself, the cook will fail.
23976ff6d951SJohn Birrell 	 */
23986ff6d951SJohn Birrell 	expr = dt_node_cook(expr, DT_IDFLG_REF);
23996ff6d951SJohn Birrell 
24006ff6d951SJohn Birrell 	if (ddp->dd_kind == CTF_K_ARRAY)
24016ff6d951SJohn Birrell 		dt_idstack_pop(&yypcb->pcb_globals, inp->din_hash);
24026ff6d951SJohn Birrell 
24036ff6d951SJohn Birrell 	/*
24046ff6d951SJohn Birrell 	 * Set the type, attributes, and flags for the inline.  If the right-
24056ff6d951SJohn Birrell 	 * hand expression has an identifier, propagate its flags.  Then cook
24066ff6d951SJohn Birrell 	 * the identifier to fully initialize it: if we're declaring an inline
24076ff6d951SJohn Birrell 	 * associative array this will construct a type signature from 'ddp'.
24086ff6d951SJohn Birrell 	 */
24096ff6d951SJohn Birrell 	if (dt_node_is_dynamic(expr))
24106ff6d951SJohn Birrell 		rdp = dt_ident_resolve(expr->dn_ident);
24116ff6d951SJohn Birrell 	else if (expr->dn_kind == DT_NODE_VAR || expr->dn_kind == DT_NODE_SYM)
24126ff6d951SJohn Birrell 		rdp = expr->dn_ident;
24136ff6d951SJohn Birrell 	else
24146ff6d951SJohn Birrell 		rdp = NULL;
24156ff6d951SJohn Birrell 
24166ff6d951SJohn Birrell 	if (rdp != NULL) {
24176ff6d951SJohn Birrell 		idp->di_flags |= (rdp->di_flags &
24186ff6d951SJohn Birrell 		    (DT_IDFLG_WRITE | DT_IDFLG_USER | DT_IDFLG_PRIM));
24196ff6d951SJohn Birrell 	}
24206ff6d951SJohn Birrell 
24216ff6d951SJohn Birrell 	idp->di_attr = dt_attr_min(_dtrace_defattr, expr->dn_attr);
24226ff6d951SJohn Birrell 	dt_ident_type_assign(idp, dtt.dtt_ctfp, dtt.dtt_type);
24236ff6d951SJohn Birrell 	(void) dt_ident_cook(dnp, idp, &ddp->dd_node);
24246ff6d951SJohn Birrell 
24256ff6d951SJohn Birrell 	/*
24266ff6d951SJohn Birrell 	 * Store the parse tree nodes for 'expr' inside of idp->di_data ('inp')
24276ff6d951SJohn Birrell 	 * so that they will be preserved with this identifier.  Then pop the
24286ff6d951SJohn Birrell 	 * inline declaration from the declaration stack and restore the lexer.
24296ff6d951SJohn Birrell 	 */
24306ff6d951SJohn Birrell 	inp->din_list = yypcb->pcb_list;
24316ff6d951SJohn Birrell 	inp->din_root = expr;
24326ff6d951SJohn Birrell 
24336ff6d951SJohn Birrell 	dt_decl_free(dt_decl_pop());
24346ff6d951SJohn Birrell 	yybegin(YYS_CLAUSE);
24356ff6d951SJohn Birrell 
24366ff6d951SJohn Birrell 	/*
24376ff6d951SJohn Birrell 	 * Finally, insert the inline identifier into dt_globals to make it
24386ff6d951SJohn Birrell 	 * visible, and then cook 'dnp' to check its type against 'expr'.
24396ff6d951SJohn Birrell 	 */
24406ff6d951SJohn Birrell 	dt_idhash_xinsert(dtp->dt_globals, idp);
24416ff6d951SJohn Birrell 	return (dt_node_cook(dnp, DT_IDFLG_REF));
24426ff6d951SJohn Birrell }
24436ff6d951SJohn Birrell 
24446ff6d951SJohn Birrell dt_node_t *
24456ff6d951SJohn Birrell dt_node_member(dt_decl_t *ddp, char *name, dt_node_t *expr)
24466ff6d951SJohn Birrell {
24476ff6d951SJohn Birrell 	dtrace_typeinfo_t dtt;
24486ff6d951SJohn Birrell 	dt_node_t *dnp;
24496ff6d951SJohn Birrell 	int err;
24506ff6d951SJohn Birrell 
24516ff6d951SJohn Birrell 	if (ddp != NULL) {
24526ff6d951SJohn Birrell 		err = dt_decl_type(ddp, &dtt);
24536ff6d951SJohn Birrell 		dt_decl_free(ddp);
24546ff6d951SJohn Birrell 
24556ff6d951SJohn Birrell 		if (err != 0)
24566ff6d951SJohn Birrell 			longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
24576ff6d951SJohn Birrell 	}
24586ff6d951SJohn Birrell 
24596ff6d951SJohn Birrell 	dnp = dt_node_alloc(DT_NODE_MEMBER);
24606ff6d951SJohn Birrell 	dnp->dn_membname = name;
24616ff6d951SJohn Birrell 	dnp->dn_membexpr = expr;
24626ff6d951SJohn Birrell 
24636ff6d951SJohn Birrell 	if (ddp != NULL)
24648e648814SRui Paulo 		dt_node_type_assign(dnp, dtt.dtt_ctfp, dtt.dtt_type,
24658e648814SRui Paulo 		    dtt.dtt_flags);
24666ff6d951SJohn Birrell 
24676ff6d951SJohn Birrell 	return (dnp);
24686ff6d951SJohn Birrell }
24696ff6d951SJohn Birrell 
24706ff6d951SJohn Birrell dt_node_t *
24716ff6d951SJohn Birrell dt_node_xlator(dt_decl_t *ddp, dt_decl_t *sdp, char *name, dt_node_t *members)
24726ff6d951SJohn Birrell {
24736ff6d951SJohn Birrell 	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
24746ff6d951SJohn Birrell 	dtrace_typeinfo_t src, dst;
24756ff6d951SJohn Birrell 	dt_node_t sn, dn;
24766ff6d951SJohn Birrell 	dt_xlator_t *dxp;
24776ff6d951SJohn Birrell 	dt_node_t *dnp;
24786ff6d951SJohn Birrell 	int edst, esrc;
24796ff6d951SJohn Birrell 	uint_t kind;
24806ff6d951SJohn Birrell 
24816ff6d951SJohn Birrell 	char n1[DT_TYPE_NAMELEN];
24826ff6d951SJohn Birrell 	char n2[DT_TYPE_NAMELEN];
24836ff6d951SJohn Birrell 
24846ff6d951SJohn Birrell 	edst = dt_decl_type(ddp, &dst);
24856ff6d951SJohn Birrell 	dt_decl_free(ddp);
24866ff6d951SJohn Birrell 
24876ff6d951SJohn Birrell 	esrc = dt_decl_type(sdp, &src);
24886ff6d951SJohn Birrell 	dt_decl_free(sdp);
24896ff6d951SJohn Birrell 
24906ff6d951SJohn Birrell 	if (edst != 0 || esrc != 0) {
24916ff6d951SJohn Birrell 		free(name);
24926ff6d951SJohn Birrell 		longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
24936ff6d951SJohn Birrell 	}
24946ff6d951SJohn Birrell 
24956ff6d951SJohn Birrell 	bzero(&sn, sizeof (sn));
24968e648814SRui Paulo 	dt_node_type_assign(&sn, src.dtt_ctfp, src.dtt_type, B_FALSE);
24976ff6d951SJohn Birrell 
24986ff6d951SJohn Birrell 	bzero(&dn, sizeof (dn));
24998e648814SRui Paulo 	dt_node_type_assign(&dn, dst.dtt_ctfp, dst.dtt_type, B_FALSE);
25006ff6d951SJohn Birrell 
25016ff6d951SJohn Birrell 	if (dt_xlator_lookup(dtp, &sn, &dn, DT_XLATE_EXACT) != NULL) {
25026ff6d951SJohn Birrell 		xyerror(D_XLATE_REDECL,
25036ff6d951SJohn Birrell 		    "translator from %s to %s has already been declared\n",
25046ff6d951SJohn Birrell 		    dt_node_type_name(&sn, n1, sizeof (n1)),
25056ff6d951SJohn Birrell 		    dt_node_type_name(&dn, n2, sizeof (n2)));
25066ff6d951SJohn Birrell 	}
25076ff6d951SJohn Birrell 
25086ff6d951SJohn Birrell 	kind = ctf_type_kind(dst.dtt_ctfp,
25096ff6d951SJohn Birrell 	    ctf_type_resolve(dst.dtt_ctfp, dst.dtt_type));
25106ff6d951SJohn Birrell 
25116ff6d951SJohn Birrell 	if (kind == CTF_K_FORWARD) {
25126ff6d951SJohn Birrell 		xyerror(D_XLATE_SOU, "incomplete struct/union/enum %s\n",
25136ff6d951SJohn Birrell 		    dt_type_name(dst.dtt_ctfp, dst.dtt_type, n1, sizeof (n1)));
25146ff6d951SJohn Birrell 	}
25156ff6d951SJohn Birrell 
25166ff6d951SJohn Birrell 	if (kind != CTF_K_STRUCT && kind != CTF_K_UNION) {
25176ff6d951SJohn Birrell 		xyerror(D_XLATE_SOU,
25186ff6d951SJohn Birrell 		    "translator output type must be a struct or union\n");
25196ff6d951SJohn Birrell 	}
25206ff6d951SJohn Birrell 
25216ff6d951SJohn Birrell 	dxp = dt_xlator_create(dtp, &src, &dst, name, members, yypcb->pcb_list);
25226ff6d951SJohn Birrell 	yybegin(YYS_CLAUSE);
25236ff6d951SJohn Birrell 	free(name);
25246ff6d951SJohn Birrell 
25256ff6d951SJohn Birrell 	if (dxp == NULL)
25266ff6d951SJohn Birrell 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
25276ff6d951SJohn Birrell 
25286ff6d951SJohn Birrell 	dnp = dt_node_alloc(DT_NODE_XLATOR);
25296ff6d951SJohn Birrell 	dnp->dn_xlator = dxp;
25306ff6d951SJohn Birrell 	dnp->dn_members = members;
25316ff6d951SJohn Birrell 
25326ff6d951SJohn Birrell 	return (dt_node_cook(dnp, DT_IDFLG_REF));
25336ff6d951SJohn Birrell }
25346ff6d951SJohn Birrell 
25356ff6d951SJohn Birrell dt_node_t *
25366ff6d951SJohn Birrell dt_node_probe(char *s, int protoc, dt_node_t *nargs, dt_node_t *xargs)
25376ff6d951SJohn Birrell {
25386ff6d951SJohn Birrell 	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
25396ff6d951SJohn Birrell 	int nargc, xargc;
25406ff6d951SJohn Birrell 	dt_node_t *dnp;
25416ff6d951SJohn Birrell 
25426ff6d951SJohn Birrell 	size_t len = strlen(s) + 3; /* +3 for :: and \0 */
25436ff6d951SJohn Birrell 	char *name = alloca(len);
25446ff6d951SJohn Birrell 
25456ff6d951SJohn Birrell 	(void) snprintf(name, len, "::%s", s);
25466ff6d951SJohn Birrell 	(void) strhyphenate(name);
25476ff6d951SJohn Birrell 	free(s);
25486ff6d951SJohn Birrell 
25496ff6d951SJohn Birrell 	if (strchr(name, '`') != NULL) {
25506ff6d951SJohn Birrell 		xyerror(D_PROV_BADNAME, "probe name may not "
25516ff6d951SJohn Birrell 		    "contain scoping operator: %s\n", name);
25526ff6d951SJohn Birrell 	}
25536ff6d951SJohn Birrell 
25546ff6d951SJohn Birrell 	if (strlen(name) - 2 >= DTRACE_NAMELEN) {
25556ff6d951SJohn Birrell 		xyerror(D_PROV_BADNAME, "probe name may not exceed %d "
25566ff6d951SJohn Birrell 		    "characters: %s\n", DTRACE_NAMELEN - 1, name);
25576ff6d951SJohn Birrell 	}
25586ff6d951SJohn Birrell 
25596ff6d951SJohn Birrell 	dnp = dt_node_alloc(DT_NODE_PROBE);
25606ff6d951SJohn Birrell 
25616ff6d951SJohn Birrell 	dnp->dn_ident = dt_ident_create(name, DT_IDENT_PROBE,
25626ff6d951SJohn Birrell 	    DT_IDFLG_ORPHAN, DTRACE_IDNONE, _dtrace_defattr, 0,
25636ff6d951SJohn Birrell 	    &dt_idops_probe, NULL, dtp->dt_gen);
25646ff6d951SJohn Birrell 
25656ff6d951SJohn Birrell 	nargc = dt_decl_prototype(nargs, nargs,
25666ff6d951SJohn Birrell 	    "probe input", DT_DP_VOID | DT_DP_ANON);
25676ff6d951SJohn Birrell 
25686ff6d951SJohn Birrell 	xargc = dt_decl_prototype(xargs, nargs,
25696ff6d951SJohn Birrell 	    "probe output", DT_DP_VOID);
25706ff6d951SJohn Birrell 
25716ff6d951SJohn Birrell 	if (nargc > UINT8_MAX) {
25726ff6d951SJohn Birrell 		xyerror(D_PROV_PRARGLEN, "probe %s input prototype exceeds %u "
25736ff6d951SJohn Birrell 		    "parameters: %d params used\n", name, UINT8_MAX, nargc);
25746ff6d951SJohn Birrell 	}
25756ff6d951SJohn Birrell 
25766ff6d951SJohn Birrell 	if (xargc > UINT8_MAX) {
25776ff6d951SJohn Birrell 		xyerror(D_PROV_PRARGLEN, "probe %s output prototype exceeds %u "
25786ff6d951SJohn Birrell 		    "parameters: %d params used\n", name, UINT8_MAX, xargc);
25796ff6d951SJohn Birrell 	}
25806ff6d951SJohn Birrell 
25816ff6d951SJohn Birrell 	if (dnp->dn_ident == NULL || dt_probe_create(dtp,
25826ff6d951SJohn Birrell 	    dnp->dn_ident, protoc, nargs, nargc, xargs, xargc) == NULL)
25836ff6d951SJohn Birrell 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
25846ff6d951SJohn Birrell 
25856ff6d951SJohn Birrell 	return (dnp);
25866ff6d951SJohn Birrell }
25876ff6d951SJohn Birrell 
25886ff6d951SJohn Birrell dt_node_t *
25896ff6d951SJohn Birrell dt_node_provider(char *name, dt_node_t *probes)
25906ff6d951SJohn Birrell {
25916ff6d951SJohn Birrell 	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
25926ff6d951SJohn Birrell 	dt_node_t *dnp = dt_node_alloc(DT_NODE_PROVIDER);
25936ff6d951SJohn Birrell 	dt_node_t *lnp;
25946ff6d951SJohn Birrell 	size_t len;
25956ff6d951SJohn Birrell 
25966ff6d951SJohn Birrell 	dnp->dn_provname = name;
25976ff6d951SJohn Birrell 	dnp->dn_probes = probes;
25986ff6d951SJohn Birrell 
25996ff6d951SJohn Birrell 	if (strchr(name, '`') != NULL) {
26006ff6d951SJohn Birrell 		dnerror(dnp, D_PROV_BADNAME, "provider name may not "
26016ff6d951SJohn Birrell 		    "contain scoping operator: %s\n", name);
26026ff6d951SJohn Birrell 	}
26036ff6d951SJohn Birrell 
26046ff6d951SJohn Birrell 	if ((len = strlen(name)) >= DTRACE_PROVNAMELEN) {
26056ff6d951SJohn Birrell 		dnerror(dnp, D_PROV_BADNAME, "provider name may not exceed %d "
26066ff6d951SJohn Birrell 		    "characters: %s\n", DTRACE_PROVNAMELEN - 1, name);
26076ff6d951SJohn Birrell 	}
26086ff6d951SJohn Birrell 
26096ff6d951SJohn Birrell 	if (isdigit(name[len - 1])) {
26106ff6d951SJohn Birrell 		dnerror(dnp, D_PROV_BADNAME, "provider name may not "
26116ff6d951SJohn Birrell 		    "end with a digit: %s\n", name);
26126ff6d951SJohn Birrell 	}
26136ff6d951SJohn Birrell 
26146ff6d951SJohn Birrell 	/*
26156ff6d951SJohn Birrell 	 * Check to see if the provider is already defined or visible through
26166ff6d951SJohn Birrell 	 * dtrace(7D).  If so, set dn_provred to treat it as a re-declaration.
26176ff6d951SJohn Birrell 	 * If not, create a new provider and set its interface-only flag.  This
26186ff6d951SJohn Birrell 	 * flag may be cleared later by calls made to dt_probe_declare().
26196ff6d951SJohn Birrell 	 */
26206ff6d951SJohn Birrell 	if ((dnp->dn_provider = dt_provider_lookup(dtp, name)) != NULL)
26216ff6d951SJohn Birrell 		dnp->dn_provred = B_TRUE;
26226ff6d951SJohn Birrell 	else if ((dnp->dn_provider = dt_provider_create(dtp, name)) == NULL)
26236ff6d951SJohn Birrell 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
26246ff6d951SJohn Birrell 	else
26256ff6d951SJohn Birrell 		dnp->dn_provider->pv_flags |= DT_PROVIDER_INTF;
26266ff6d951SJohn Birrell 
26276ff6d951SJohn Birrell 	/*
26286ff6d951SJohn Birrell 	 * Store all parse nodes created since we consumed the DT_KEY_PROVIDER
26296ff6d951SJohn Birrell 	 * token with the provider and then restore our lexing state to CLAUSE.
26306ff6d951SJohn Birrell 	 * Note that if dnp->dn_provred is true, we may end up storing dups of
26316ff6d951SJohn Birrell 	 * a provider's interface and implementation: we eat this space because
26326ff6d951SJohn Birrell 	 * the implementation will likely need to redeclare probe members, and
26336ff6d951SJohn Birrell 	 * therefore may result in those member nodes becoming persistent.
26346ff6d951SJohn Birrell 	 */
26356ff6d951SJohn Birrell 	for (lnp = yypcb->pcb_list; lnp->dn_link != NULL; lnp = lnp->dn_link)
26366ff6d951SJohn Birrell 		continue; /* skip to end of allocation list */
26376ff6d951SJohn Birrell 
26386ff6d951SJohn Birrell 	lnp->dn_link = dnp->dn_provider->pv_nodes;
26396ff6d951SJohn Birrell 	dnp->dn_provider->pv_nodes = yypcb->pcb_list;
26406ff6d951SJohn Birrell 
26416ff6d951SJohn Birrell 	yybegin(YYS_CLAUSE);
26426ff6d951SJohn Birrell 	return (dnp);
26436ff6d951SJohn Birrell }
26446ff6d951SJohn Birrell 
26456ff6d951SJohn Birrell dt_node_t *
26466ff6d951SJohn Birrell dt_node_program(dt_node_t *lnp)
26476ff6d951SJohn Birrell {
26486ff6d951SJohn Birrell 	dt_node_t *dnp = dt_node_alloc(DT_NODE_PROG);
26496ff6d951SJohn Birrell 	dnp->dn_list = lnp;
26506ff6d951SJohn Birrell 	return (dnp);
26516ff6d951SJohn Birrell }
26526ff6d951SJohn Birrell 
26536ff6d951SJohn Birrell /*
26546ff6d951SJohn Birrell  * This function provides the underlying implementation of cooking an
26556ff6d951SJohn Birrell  * identifier given its node, a hash of dynamic identifiers, an identifier
26566ff6d951SJohn Birrell  * kind, and a boolean flag indicating whether we are allowed to instantiate
26576ff6d951SJohn Birrell  * a new identifier if the string is not found.  This function is either
26586ff6d951SJohn Birrell  * called from dt_cook_ident(), below, or directly by the various cooking
26596ff6d951SJohn Birrell  * routines that are allowed to instantiate identifiers (e.g. op2 TOK_ASGN).
26606ff6d951SJohn Birrell  */
26616ff6d951SJohn Birrell static void
26626ff6d951SJohn Birrell dt_xcook_ident(dt_node_t *dnp, dt_idhash_t *dhp, uint_t idkind, int create)
26636ff6d951SJohn Birrell {
26646ff6d951SJohn Birrell 	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
26656ff6d951SJohn Birrell 	const char *sname = dt_idhash_name(dhp);
26666ff6d951SJohn Birrell 	int uref = 0;
26676ff6d951SJohn Birrell 
26686ff6d951SJohn Birrell 	dtrace_attribute_t attr = _dtrace_defattr;
26696ff6d951SJohn Birrell 	dt_ident_t *idp;
26706ff6d951SJohn Birrell 	dtrace_syminfo_t dts;
26716ff6d951SJohn Birrell 	GElf_Sym sym;
26726ff6d951SJohn Birrell 
26736ff6d951SJohn Birrell 	const char *scope, *mark;
26746ff6d951SJohn Birrell 	uchar_t dnkind;
26756ff6d951SJohn Birrell 	char *name;
26766ff6d951SJohn Birrell 
26776ff6d951SJohn Birrell 	/*
26786ff6d951SJohn Birrell 	 * Look for scoping marks in the identifier.  If one is found, set our
26796ff6d951SJohn Birrell 	 * scope to either DTRACE_OBJ_KMODS or UMODS or to the first part of
26806ff6d951SJohn Birrell 	 * the string that specifies the scope using an explicit module name.
26816ff6d951SJohn Birrell 	 * If two marks in a row are found, set 'uref' (user symbol reference).
26826ff6d951SJohn Birrell 	 * Otherwise we set scope to DTRACE_OBJ_EXEC, indicating that normal
26836ff6d951SJohn Birrell 	 * scope is desired and we should search the specified idhash.
26846ff6d951SJohn Birrell 	 */
26856ff6d951SJohn Birrell 	if ((name = strrchr(dnp->dn_string, '`')) != NULL) {
26866ff6d951SJohn Birrell 		if (name > dnp->dn_string && name[-1] == '`') {
26876ff6d951SJohn Birrell 			uref++;
26886ff6d951SJohn Birrell 			name[-1] = '\0';
26896ff6d951SJohn Birrell 		}
26906ff6d951SJohn Birrell 
26916ff6d951SJohn Birrell 		if (name == dnp->dn_string + uref)
26926ff6d951SJohn Birrell 			scope = uref ? DTRACE_OBJ_UMODS : DTRACE_OBJ_KMODS;
26936ff6d951SJohn Birrell 		else
26946ff6d951SJohn Birrell 			scope = dnp->dn_string;
26956ff6d951SJohn Birrell 
26966ff6d951SJohn Birrell 		*name++ = '\0'; /* leave name pointing after scoping mark */
26976ff6d951SJohn Birrell 		dnkind = DT_NODE_VAR;
26986ff6d951SJohn Birrell 
26996ff6d951SJohn Birrell 	} else if (idkind == DT_IDENT_AGG) {
27006ff6d951SJohn Birrell 		scope = DTRACE_OBJ_EXEC;
27016ff6d951SJohn Birrell 		name = dnp->dn_string + 1;
27026ff6d951SJohn Birrell 		dnkind = DT_NODE_AGG;
27036ff6d951SJohn Birrell 	} else {
27046ff6d951SJohn Birrell 		scope = DTRACE_OBJ_EXEC;
27056ff6d951SJohn Birrell 		name = dnp->dn_string;
27066ff6d951SJohn Birrell 		dnkind = DT_NODE_VAR;
27076ff6d951SJohn Birrell 	}
27086ff6d951SJohn Birrell 
27096ff6d951SJohn Birrell 	/*
27106ff6d951SJohn Birrell 	 * If create is set to false, and we fail our idhash lookup, preset
27116ff6d951SJohn Birrell 	 * the errno code to EDT_NOVAR for our final error message below.
27126ff6d951SJohn Birrell 	 * If we end up calling dtrace_lookup_by_name(), it will reset the
27136ff6d951SJohn Birrell 	 * errno appropriately and that error will be reported instead.
27146ff6d951SJohn Birrell 	 */
27156ff6d951SJohn Birrell 	(void) dt_set_errno(dtp, EDT_NOVAR);
27166ff6d951SJohn Birrell 	mark = uref ? "``" : "`";
27176ff6d951SJohn Birrell 
27186ff6d951SJohn Birrell 	if (scope == DTRACE_OBJ_EXEC && (
27196ff6d951SJohn Birrell 	    (dhp != dtp->dt_globals &&
27206ff6d951SJohn Birrell 	    (idp = dt_idhash_lookup(dhp, name)) != NULL) ||
27216ff6d951SJohn Birrell 	    (dhp == dtp->dt_globals &&
27226ff6d951SJohn Birrell 	    (idp = dt_idstack_lookup(&yypcb->pcb_globals, name)) != NULL))) {
27236ff6d951SJohn Birrell 		/*
27246ff6d951SJohn Birrell 		 * Check that we are referencing the ident in the manner that
27256ff6d951SJohn Birrell 		 * matches its type if this is a global lookup.  In the TLS or
27266ff6d951SJohn Birrell 		 * local case, we don't know how the ident will be used until
27276ff6d951SJohn Birrell 		 * the time operator -> is seen; more parsing is needed.
27286ff6d951SJohn Birrell 		 */
27296ff6d951SJohn Birrell 		if (idp->di_kind != idkind && dhp == dtp->dt_globals) {
27306ff6d951SJohn Birrell 			xyerror(D_IDENT_BADREF, "%s '%s' may not be referenced "
27316ff6d951SJohn Birrell 			    "as %s\n", dt_idkind_name(idp->di_kind),
27326ff6d951SJohn Birrell 			    idp->di_name, dt_idkind_name(idkind));
27336ff6d951SJohn Birrell 		}
27346ff6d951SJohn Birrell 
27356ff6d951SJohn Birrell 		/*
27366ff6d951SJohn Birrell 		 * Arrays and aggregations are not cooked individually. They
27376ff6d951SJohn Birrell 		 * have dynamic types and must be referenced using operator [].
27386ff6d951SJohn Birrell 		 * This is handled explicitly by the code for DT_TOK_LBRAC.
27396ff6d951SJohn Birrell 		 */
27406ff6d951SJohn Birrell 		if (idp->di_kind != DT_IDENT_ARRAY &&
27416ff6d951SJohn Birrell 		    idp->di_kind != DT_IDENT_AGG)
27426ff6d951SJohn Birrell 			attr = dt_ident_cook(dnp, idp, NULL);
27436ff6d951SJohn Birrell 		else {
27446ff6d951SJohn Birrell 			dt_node_type_assign(dnp,
27458e648814SRui Paulo 			    DT_DYN_CTFP(dtp), DT_DYN_TYPE(dtp), B_FALSE);
27466ff6d951SJohn Birrell 			attr = idp->di_attr;
27476ff6d951SJohn Birrell 		}
27486ff6d951SJohn Birrell 
27496ff6d951SJohn Birrell 		free(dnp->dn_string);
27506ff6d951SJohn Birrell 		dnp->dn_string = NULL;
27516ff6d951SJohn Birrell 		dnp->dn_kind = dnkind;
27526ff6d951SJohn Birrell 		dnp->dn_ident = idp;
27536ff6d951SJohn Birrell 		dnp->dn_flags |= DT_NF_LVALUE;
27546ff6d951SJohn Birrell 
27556ff6d951SJohn Birrell 		if (idp->di_flags & DT_IDFLG_WRITE)
27566ff6d951SJohn Birrell 			dnp->dn_flags |= DT_NF_WRITABLE;
27576ff6d951SJohn Birrell 
27586ff6d951SJohn Birrell 		dt_node_attr_assign(dnp, attr);
27596ff6d951SJohn Birrell 
27606ff6d951SJohn Birrell 	} else if (dhp == dtp->dt_globals && scope != DTRACE_OBJ_EXEC &&
27616ff6d951SJohn Birrell 	    dtrace_lookup_by_name(dtp, scope, name, &sym, &dts) == 0) {
27626ff6d951SJohn Birrell 
27636ff6d951SJohn Birrell 		dt_module_t *mp = dt_module_lookup_by_name(dtp, dts.dts_object);
27646ff6d951SJohn Birrell 		int umod = (mp->dm_flags & DT_DM_KERNEL) == 0;
27656ff6d951SJohn Birrell 		static const char *const kunames[] = { "kernel", "user" };
27666ff6d951SJohn Birrell 
27676ff6d951SJohn Birrell 		dtrace_typeinfo_t dtt;
27686ff6d951SJohn Birrell 		dtrace_syminfo_t *sip;
27696ff6d951SJohn Birrell 
27706ff6d951SJohn Birrell 		if (uref ^ umod) {
27716ff6d951SJohn Birrell 			xyerror(D_SYM_BADREF, "%s module '%s' symbol '%s' may "
27726ff6d951SJohn Birrell 			    "not be referenced as a %s symbol\n", kunames[umod],
27736ff6d951SJohn Birrell 			    dts.dts_object, dts.dts_name, kunames[uref]);
27746ff6d951SJohn Birrell 		}
27756ff6d951SJohn Birrell 
27766ff6d951SJohn Birrell 		if (dtrace_symbol_type(dtp, &sym, &dts, &dtt) != 0) {
27776ff6d951SJohn Birrell 			/*
27786ff6d951SJohn Birrell 			 * For now, we special-case EDT_DATAMODEL to clarify
27796ff6d951SJohn Birrell 			 * that mixed data models are not currently supported.
27806ff6d951SJohn Birrell 			 */
27816ff6d951SJohn Birrell 			if (dtp->dt_errno == EDT_DATAMODEL) {
27826ff6d951SJohn Birrell 				xyerror(D_SYM_MODEL, "cannot use %s symbol "
27836ff6d951SJohn Birrell 				    "%s%s%s in a %s D program\n",
27846ff6d951SJohn Birrell 				    dt_module_modelname(mp),
27856ff6d951SJohn Birrell 				    dts.dts_object, mark, dts.dts_name,
27866ff6d951SJohn Birrell 				    dt_module_modelname(dtp->dt_ddefs));
27876ff6d951SJohn Birrell 			}
27886ff6d951SJohn Birrell 
27896ff6d951SJohn Birrell 			xyerror(D_SYM_NOTYPES,
27906ff6d951SJohn Birrell 			    "no symbolic type information is available for "
27916ff6d951SJohn Birrell 			    "%s%s%s: %s\n", dts.dts_object, mark, dts.dts_name,
27926ff6d951SJohn Birrell 			    dtrace_errmsg(dtp, dtrace_errno(dtp)));
27936ff6d951SJohn Birrell 		}
27946ff6d951SJohn Birrell 
27956ff6d951SJohn Birrell 		idp = dt_ident_create(name, DT_IDENT_SYMBOL, 0, 0,
27966ff6d951SJohn Birrell 		    _dtrace_symattr, 0, &dt_idops_thaw, NULL, dtp->dt_gen);
27976ff6d951SJohn Birrell 
27986ff6d951SJohn Birrell 		if (idp == NULL)
27996ff6d951SJohn Birrell 			longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
28006ff6d951SJohn Birrell 
28016ff6d951SJohn Birrell 		if (mp->dm_flags & DT_DM_PRIMARY)
28026ff6d951SJohn Birrell 			idp->di_flags |= DT_IDFLG_PRIM;
28036ff6d951SJohn Birrell 
28046ff6d951SJohn Birrell 		idp->di_next = dtp->dt_externs;
28056ff6d951SJohn Birrell 		dtp->dt_externs = idp;
28066ff6d951SJohn Birrell 
28076ff6d951SJohn Birrell 		if ((sip = malloc(sizeof (dtrace_syminfo_t))) == NULL)
28086ff6d951SJohn Birrell 			longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
28096ff6d951SJohn Birrell 
28106ff6d951SJohn Birrell 		bcopy(&dts, sip, sizeof (dtrace_syminfo_t));
28116ff6d951SJohn Birrell 		idp->di_data = sip;
28126ff6d951SJohn Birrell 		idp->di_ctfp = dtt.dtt_ctfp;
28136ff6d951SJohn Birrell 		idp->di_type = dtt.dtt_type;
28146ff6d951SJohn Birrell 
28156ff6d951SJohn Birrell 		free(dnp->dn_string);
28166ff6d951SJohn Birrell 		dnp->dn_string = NULL;
28176ff6d951SJohn Birrell 		dnp->dn_kind = DT_NODE_SYM;
28186ff6d951SJohn Birrell 		dnp->dn_ident = idp;
28196ff6d951SJohn Birrell 		dnp->dn_flags |= DT_NF_LVALUE;
28206ff6d951SJohn Birrell 
28218e648814SRui Paulo 		dt_node_type_assign(dnp, dtt.dtt_ctfp, dtt.dtt_type,
28228e648814SRui Paulo 		    dtt.dtt_flags);
28236ff6d951SJohn Birrell 		dt_node_attr_assign(dnp, _dtrace_symattr);
28246ff6d951SJohn Birrell 
28256ff6d951SJohn Birrell 		if (uref) {
28266ff6d951SJohn Birrell 			idp->di_flags |= DT_IDFLG_USER;
28276ff6d951SJohn Birrell 			dnp->dn_flags |= DT_NF_USERLAND;
28286ff6d951SJohn Birrell 		}
28296ff6d951SJohn Birrell 
28306ff6d951SJohn Birrell 	} else if (scope == DTRACE_OBJ_EXEC && create == B_TRUE) {
28316ff6d951SJohn Birrell 		uint_t flags = DT_IDFLG_WRITE;
28326ff6d951SJohn Birrell 		uint_t id;
28336ff6d951SJohn Birrell 
28346ff6d951SJohn Birrell 		if (dt_idhash_nextid(dhp, &id) == -1) {
28356ff6d951SJohn Birrell 			xyerror(D_ID_OFLOW, "cannot create %s: limit on number "
28366ff6d951SJohn Birrell 			    "of %s variables exceeded\n", name, sname);
28376ff6d951SJohn Birrell 		}
28386ff6d951SJohn Birrell 
28396ff6d951SJohn Birrell 		if (dhp == yypcb->pcb_locals)
28406ff6d951SJohn Birrell 			flags |= DT_IDFLG_LOCAL;
28416ff6d951SJohn Birrell 		else if (dhp == dtp->dt_tls)
28426ff6d951SJohn Birrell 			flags |= DT_IDFLG_TLS;
28436ff6d951SJohn Birrell 
28446ff6d951SJohn Birrell 		dt_dprintf("create %s %s variable %s, id=%u\n",
28456ff6d951SJohn Birrell 		    sname, dt_idkind_name(idkind), name, id);
28466ff6d951SJohn Birrell 
28476ff6d951SJohn Birrell 		if (idkind == DT_IDENT_ARRAY || idkind == DT_IDENT_AGG) {
28486ff6d951SJohn Birrell 			idp = dt_idhash_insert(dhp, name,
28496ff6d951SJohn Birrell 			    idkind, flags, id, _dtrace_defattr, 0,
28506ff6d951SJohn Birrell 			    &dt_idops_assc, NULL, dtp->dt_gen);
28516ff6d951SJohn Birrell 		} else {
28526ff6d951SJohn Birrell 			idp = dt_idhash_insert(dhp, name,
28536ff6d951SJohn Birrell 			    idkind, flags, id, _dtrace_defattr, 0,
28546ff6d951SJohn Birrell 			    &dt_idops_thaw, NULL, dtp->dt_gen);
28556ff6d951SJohn Birrell 		}
28566ff6d951SJohn Birrell 
28576ff6d951SJohn Birrell 		if (idp == NULL)
28586ff6d951SJohn Birrell 			longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
28596ff6d951SJohn Birrell 
28606ff6d951SJohn Birrell 		/*
28616ff6d951SJohn Birrell 		 * Arrays and aggregations are not cooked individually. They
28626ff6d951SJohn Birrell 		 * have dynamic types and must be referenced using operator [].
28636ff6d951SJohn Birrell 		 * This is handled explicitly by the code for DT_TOK_LBRAC.
28646ff6d951SJohn Birrell 		 */
28656ff6d951SJohn Birrell 		if (idp->di_kind != DT_IDENT_ARRAY &&
28666ff6d951SJohn Birrell 		    idp->di_kind != DT_IDENT_AGG)
28676ff6d951SJohn Birrell 			attr = dt_ident_cook(dnp, idp, NULL);
28686ff6d951SJohn Birrell 		else {
28696ff6d951SJohn Birrell 			dt_node_type_assign(dnp,
28708e648814SRui Paulo 			    DT_DYN_CTFP(dtp), DT_DYN_TYPE(dtp), B_FALSE);
28716ff6d951SJohn Birrell 			attr = idp->di_attr;
28726ff6d951SJohn Birrell 		}
28736ff6d951SJohn Birrell 
28746ff6d951SJohn Birrell 		free(dnp->dn_string);
28756ff6d951SJohn Birrell 		dnp->dn_string = NULL;
28766ff6d951SJohn Birrell 		dnp->dn_kind = dnkind;
28776ff6d951SJohn Birrell 		dnp->dn_ident = idp;
28786ff6d951SJohn Birrell 		dnp->dn_flags |= DT_NF_LVALUE | DT_NF_WRITABLE;
28796ff6d951SJohn Birrell 
28806ff6d951SJohn Birrell 		dt_node_attr_assign(dnp, attr);
28816ff6d951SJohn Birrell 
28826ff6d951SJohn Birrell 	} else if (scope != DTRACE_OBJ_EXEC) {
28836ff6d951SJohn Birrell 		xyerror(D_IDENT_UNDEF, "failed to resolve %s%s%s: %s\n",
28846ff6d951SJohn Birrell 		    dnp->dn_string, mark, name,
28856ff6d951SJohn Birrell 		    dtrace_errmsg(dtp, dtrace_errno(dtp)));
28866ff6d951SJohn Birrell 	} else {
28876ff6d951SJohn Birrell 		xyerror(D_IDENT_UNDEF, "failed to resolve %s: %s\n",
28886ff6d951SJohn Birrell 		    dnp->dn_string, dtrace_errmsg(dtp, dtrace_errno(dtp)));
28896ff6d951SJohn Birrell 	}
28906ff6d951SJohn Birrell }
28916ff6d951SJohn Birrell 
28926ff6d951SJohn Birrell static dt_node_t *
28936ff6d951SJohn Birrell dt_cook_ident(dt_node_t *dnp, uint_t idflags)
28946ff6d951SJohn Birrell {
28956ff6d951SJohn Birrell 	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
28966ff6d951SJohn Birrell 
28976ff6d951SJohn Birrell 	if (dnp->dn_op == DT_TOK_AGG)
28986ff6d951SJohn Birrell 		dt_xcook_ident(dnp, dtp->dt_aggs, DT_IDENT_AGG, B_FALSE);
28996ff6d951SJohn Birrell 	else
29006ff6d951SJohn Birrell 		dt_xcook_ident(dnp, dtp->dt_globals, DT_IDENT_SCALAR, B_FALSE);
29016ff6d951SJohn Birrell 
29026ff6d951SJohn Birrell 	return (dt_node_cook(dnp, idflags));
29036ff6d951SJohn Birrell }
29046ff6d951SJohn Birrell 
29056ff6d951SJohn Birrell /*
29066ff6d951SJohn Birrell  * Since operators [ and -> can instantiate new variables before we know
29076ff6d951SJohn Birrell  * whether the reference is for a read or a write, we need to check read
29086ff6d951SJohn Birrell  * references to determine if the identifier is currently dt_ident_unref().
29096ff6d951SJohn Birrell  * If so, we report that this first access was to an undefined variable.
29106ff6d951SJohn Birrell  */
29116ff6d951SJohn Birrell static dt_node_t *
29126ff6d951SJohn Birrell dt_cook_var(dt_node_t *dnp, uint_t idflags)
29136ff6d951SJohn Birrell {
29146ff6d951SJohn Birrell 	dt_ident_t *idp = dnp->dn_ident;
29156ff6d951SJohn Birrell 
29166ff6d951SJohn Birrell 	if ((idflags & DT_IDFLG_REF) && dt_ident_unref(idp)) {
29176ff6d951SJohn Birrell 		dnerror(dnp, D_VAR_UNDEF,
29186ff6d951SJohn Birrell 		    "%s%s has not yet been declared or assigned\n",
29196ff6d951SJohn Birrell 		    (idp->di_flags & DT_IDFLG_LOCAL) ? "this->" :
29206ff6d951SJohn Birrell 		    (idp->di_flags & DT_IDFLG_TLS) ? "self->" : "",
29216ff6d951SJohn Birrell 		    idp->di_name);
29226ff6d951SJohn Birrell 	}
29236ff6d951SJohn Birrell 
29246ff6d951SJohn Birrell 	dt_node_attr_assign(dnp, dt_ident_cook(dnp, idp, &dnp->dn_args));
29256ff6d951SJohn Birrell 	return (dnp);
29266ff6d951SJohn Birrell }
29276ff6d951SJohn Birrell 
29286ff6d951SJohn Birrell /*ARGSUSED*/
29296ff6d951SJohn Birrell static dt_node_t *
29306ff6d951SJohn Birrell dt_cook_func(dt_node_t *dnp, uint_t idflags)
29316ff6d951SJohn Birrell {
29326ff6d951SJohn Birrell 	dt_node_attr_assign(dnp,
29336ff6d951SJohn Birrell 	    dt_ident_cook(dnp, dnp->dn_ident, &dnp->dn_args));
29346ff6d951SJohn Birrell 
29356ff6d951SJohn Birrell 	return (dnp);
29366ff6d951SJohn Birrell }
29376ff6d951SJohn Birrell 
29386ff6d951SJohn Birrell static dt_node_t *
29396ff6d951SJohn Birrell dt_cook_op1(dt_node_t *dnp, uint_t idflags)
29406ff6d951SJohn Birrell {
29416ff6d951SJohn Birrell 	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
29426ff6d951SJohn Birrell 	dt_node_t *cp = dnp->dn_child;
29436ff6d951SJohn Birrell 
29446ff6d951SJohn Birrell 	char n[DT_TYPE_NAMELEN];
29456ff6d951SJohn Birrell 	dtrace_typeinfo_t dtt;
29466ff6d951SJohn Birrell 	dt_ident_t *idp;
29476ff6d951SJohn Birrell 
29486ff6d951SJohn Birrell 	ctf_encoding_t e;
29496ff6d951SJohn Birrell 	ctf_arinfo_t r;
29506ff6d951SJohn Birrell 	ctf_id_t type, base;
29516ff6d951SJohn Birrell 	uint_t kind;
29526ff6d951SJohn Birrell 
29536ff6d951SJohn Birrell 	if (dnp->dn_op == DT_TOK_PREINC || dnp->dn_op == DT_TOK_POSTINC ||
29546ff6d951SJohn Birrell 	    dnp->dn_op == DT_TOK_PREDEC || dnp->dn_op == DT_TOK_POSTDEC)
29556ff6d951SJohn Birrell 		idflags = DT_IDFLG_REF | DT_IDFLG_MOD;
29566ff6d951SJohn Birrell 	else
29576ff6d951SJohn Birrell 		idflags = DT_IDFLG_REF;
29586ff6d951SJohn Birrell 
29596ff6d951SJohn Birrell 	/*
29606ff6d951SJohn Birrell 	 * We allow the unary ++ and -- operators to instantiate new scalar
29616ff6d951SJohn Birrell 	 * variables if applied to an identifier; otherwise just cook as usual.
29626ff6d951SJohn Birrell 	 */
29636ff6d951SJohn Birrell 	if (cp->dn_kind == DT_NODE_IDENT && (idflags & DT_IDFLG_MOD))
29646ff6d951SJohn Birrell 		dt_xcook_ident(cp, dtp->dt_globals, DT_IDENT_SCALAR, B_TRUE);
29656ff6d951SJohn Birrell 
29666ff6d951SJohn Birrell 	cp = dnp->dn_child = dt_node_cook(cp, 0); /* don't set idflags yet */
29676ff6d951SJohn Birrell 
29686ff6d951SJohn Birrell 	if (cp->dn_kind == DT_NODE_VAR && dt_ident_unref(cp->dn_ident)) {
29696ff6d951SJohn Birrell 		if (dt_type_lookup("int64_t", &dtt) != 0)
29706ff6d951SJohn Birrell 			xyerror(D_TYPE_ERR, "failed to lookup int64_t\n");
29716ff6d951SJohn Birrell 
29726ff6d951SJohn Birrell 		dt_ident_type_assign(cp->dn_ident, dtt.dtt_ctfp, dtt.dtt_type);
29738e648814SRui Paulo 		dt_node_type_assign(cp, dtt.dtt_ctfp, dtt.dtt_type,
29748e648814SRui Paulo 		    dtt.dtt_flags);
29756ff6d951SJohn Birrell 	}
29766ff6d951SJohn Birrell 
29776ff6d951SJohn Birrell 	if (cp->dn_kind == DT_NODE_VAR)
29786ff6d951SJohn Birrell 		cp->dn_ident->di_flags |= idflags;
29796ff6d951SJohn Birrell 
29806ff6d951SJohn Birrell 	switch (dnp->dn_op) {
29816ff6d951SJohn Birrell 	case DT_TOK_DEREF:
29826ff6d951SJohn Birrell 		/*
29836ff6d951SJohn Birrell 		 * If the deref operator is applied to a translated pointer,
2984a98ff317SPedro F. Giffuni 		 * we set our output type to the output of the translation.
29856ff6d951SJohn Birrell 		 */
29866ff6d951SJohn Birrell 		if ((idp = dt_node_resolve(cp, DT_IDENT_XLPTR)) != NULL) {
29876ff6d951SJohn Birrell 			dt_xlator_t *dxp = idp->di_data;
29886ff6d951SJohn Birrell 
29896ff6d951SJohn Birrell 			dnp->dn_ident = &dxp->dx_souid;
29906ff6d951SJohn Birrell 			dt_node_type_assign(dnp,
29918e648814SRui Paulo 			    dnp->dn_ident->di_ctfp, dnp->dn_ident->di_type,
29928e648814SRui Paulo 			    cp->dn_flags & DT_NF_USERLAND);
29936ff6d951SJohn Birrell 			break;
29946ff6d951SJohn Birrell 		}
29956ff6d951SJohn Birrell 
29966ff6d951SJohn Birrell 		type = ctf_type_resolve(cp->dn_ctfp, cp->dn_type);
29976ff6d951SJohn Birrell 		kind = ctf_type_kind(cp->dn_ctfp, type);
29986ff6d951SJohn Birrell 
29996ff6d951SJohn Birrell 		if (kind == CTF_K_ARRAY) {
30006ff6d951SJohn Birrell 			if (ctf_array_info(cp->dn_ctfp, type, &r) != 0) {
30016ff6d951SJohn Birrell 				dtp->dt_ctferr = ctf_errno(cp->dn_ctfp);
30026ff6d951SJohn Birrell 				longjmp(yypcb->pcb_jmpbuf, EDT_CTF);
30036ff6d951SJohn Birrell 			} else
30046ff6d951SJohn Birrell 				type = r.ctr_contents;
30056ff6d951SJohn Birrell 		} else if (kind == CTF_K_POINTER) {
30066ff6d951SJohn Birrell 			type = ctf_type_reference(cp->dn_ctfp, type);
30076ff6d951SJohn Birrell 		} else {
30086ff6d951SJohn Birrell 			xyerror(D_DEREF_NONPTR,
30096ff6d951SJohn Birrell 			    "cannot dereference non-pointer type\n");
30106ff6d951SJohn Birrell 		}
30116ff6d951SJohn Birrell 
30128e648814SRui Paulo 		dt_node_type_assign(dnp, cp->dn_ctfp, type,
30138e648814SRui Paulo 		    cp->dn_flags & DT_NF_USERLAND);
30146ff6d951SJohn Birrell 		base = ctf_type_resolve(cp->dn_ctfp, type);
30156ff6d951SJohn Birrell 		kind = ctf_type_kind(cp->dn_ctfp, base);
30166ff6d951SJohn Birrell 
30176ff6d951SJohn Birrell 		if (kind == CTF_K_INTEGER && ctf_type_encoding(cp->dn_ctfp,
30186ff6d951SJohn Birrell 		    base, &e) == 0 && IS_VOID(e)) {
30196ff6d951SJohn Birrell 			xyerror(D_DEREF_VOID,
30206ff6d951SJohn Birrell 			    "cannot dereference pointer to void\n");
30216ff6d951SJohn Birrell 		}
30226ff6d951SJohn Birrell 
30236ff6d951SJohn Birrell 		if (kind == CTF_K_FUNCTION) {
30246ff6d951SJohn Birrell 			xyerror(D_DEREF_FUNC,
30256ff6d951SJohn Birrell 			    "cannot dereference pointer to function\n");
30266ff6d951SJohn Birrell 		}
30276ff6d951SJohn Birrell 
30286ff6d951SJohn Birrell 		if (kind != CTF_K_ARRAY || dt_node_is_string(dnp))
30296ff6d951SJohn Birrell 			dnp->dn_flags |= DT_NF_LVALUE; /* see K&R[A7.4.3] */
30306ff6d951SJohn Birrell 
30316ff6d951SJohn Birrell 		/*
30326ff6d951SJohn Birrell 		 * If we propagated the l-value bit and the child operand was
30336ff6d951SJohn Birrell 		 * a writable D variable or a binary operation of the form
30346ff6d951SJohn Birrell 		 * a + b where a is writable, then propagate the writable bit.
30356ff6d951SJohn Birrell 		 * This is necessary to permit assignments to scalar arrays,
30366ff6d951SJohn Birrell 		 * which are converted to expressions of the form *(a + i).
30376ff6d951SJohn Birrell 		 */
30386ff6d951SJohn Birrell 		if ((cp->dn_flags & DT_NF_WRITABLE) ||
30396ff6d951SJohn Birrell 		    (cp->dn_kind == DT_NODE_OP2 && cp->dn_op == DT_TOK_ADD &&
30406ff6d951SJohn Birrell 		    (cp->dn_left->dn_flags & DT_NF_WRITABLE)))
30416ff6d951SJohn Birrell 			dnp->dn_flags |= DT_NF_WRITABLE;
30426ff6d951SJohn Birrell 
30436ff6d951SJohn Birrell 		if ((cp->dn_flags & DT_NF_USERLAND) &&
30446ff6d951SJohn Birrell 		    (kind == CTF_K_POINTER || (dnp->dn_flags & DT_NF_REF)))
30456ff6d951SJohn Birrell 			dnp->dn_flags |= DT_NF_USERLAND;
30466ff6d951SJohn Birrell 		break;
30476ff6d951SJohn Birrell 
30486ff6d951SJohn Birrell 	case DT_TOK_IPOS:
30496ff6d951SJohn Birrell 	case DT_TOK_INEG:
30506ff6d951SJohn Birrell 		if (!dt_node_is_arith(cp)) {
30516ff6d951SJohn Birrell 			xyerror(D_OP_ARITH, "operator %s requires an operand "
30526ff6d951SJohn Birrell 			    "of arithmetic type\n", opstr(dnp->dn_op));
30536ff6d951SJohn Birrell 		}
30546ff6d951SJohn Birrell 		dt_node_type_propagate(cp, dnp); /* see K&R[A7.4.4-6] */
30556ff6d951SJohn Birrell 		break;
30566ff6d951SJohn Birrell 
30576ff6d951SJohn Birrell 	case DT_TOK_BNEG:
30586ff6d951SJohn Birrell 		if (!dt_node_is_integer(cp)) {
30596ff6d951SJohn Birrell 			xyerror(D_OP_INT, "operator %s requires an operand of "
30606ff6d951SJohn Birrell 			    "integral type\n", opstr(dnp->dn_op));
30616ff6d951SJohn Birrell 		}
30626ff6d951SJohn Birrell 		dt_node_type_propagate(cp, dnp); /* see K&R[A7.4.4-6] */
30636ff6d951SJohn Birrell 		break;
30646ff6d951SJohn Birrell 
30656ff6d951SJohn Birrell 	case DT_TOK_LNEG:
30666ff6d951SJohn Birrell 		if (!dt_node_is_scalar(cp)) {
30676ff6d951SJohn Birrell 			xyerror(D_OP_SCALAR, "operator %s requires an operand "
30686ff6d951SJohn Birrell 			    "of scalar type\n", opstr(dnp->dn_op));
30696ff6d951SJohn Birrell 		}
30708e648814SRui Paulo 		dt_node_type_assign(dnp, DT_INT_CTFP(dtp), DT_INT_TYPE(dtp),
30718e648814SRui Paulo 		    B_FALSE);
30726ff6d951SJohn Birrell 		break;
30736ff6d951SJohn Birrell 
30746ff6d951SJohn Birrell 	case DT_TOK_ADDROF:
30756ff6d951SJohn Birrell 		if (cp->dn_kind == DT_NODE_VAR || cp->dn_kind == DT_NODE_AGG) {
30766ff6d951SJohn Birrell 			xyerror(D_ADDROF_VAR,
30776ff6d951SJohn Birrell 			    "cannot take address of dynamic variable\n");
30786ff6d951SJohn Birrell 		}
30796ff6d951SJohn Birrell 
30806ff6d951SJohn Birrell 		if (dt_node_is_dynamic(cp)) {
30816ff6d951SJohn Birrell 			xyerror(D_ADDROF_VAR,
30826ff6d951SJohn Birrell 			    "cannot take address of dynamic object\n");
30836ff6d951SJohn Birrell 		}
30846ff6d951SJohn Birrell 
30856ff6d951SJohn Birrell 		if (!(cp->dn_flags & DT_NF_LVALUE)) {
30866ff6d951SJohn Birrell 			xyerror(D_ADDROF_LVAL, /* see K&R[A7.4.2] */
30876ff6d951SJohn Birrell 			    "unacceptable operand for unary & operator\n");
30886ff6d951SJohn Birrell 		}
30896ff6d951SJohn Birrell 
30906ff6d951SJohn Birrell 		if (cp->dn_flags & DT_NF_BITFIELD) {
30916ff6d951SJohn Birrell 			xyerror(D_ADDROF_BITFIELD,
30926ff6d951SJohn Birrell 			    "cannot take address of bit-field\n");
30936ff6d951SJohn Birrell 		}
30946ff6d951SJohn Birrell 
30951ad2da03SConrad Meyer 		dtt = (dtrace_typeinfo_t){
30961ad2da03SConrad Meyer 			.dtt_ctfp = cp->dn_ctfp,
30971ad2da03SConrad Meyer 			.dtt_type = cp->dn_type,
30981ad2da03SConrad Meyer 		};
30996ff6d951SJohn Birrell 
31006ff6d951SJohn Birrell 		if (dt_type_pointer(&dtt) == -1) {
31016ff6d951SJohn Birrell 			xyerror(D_TYPE_ERR, "cannot find type for \"&\": %s*\n",
31026ff6d951SJohn Birrell 			    dt_node_type_name(cp, n, sizeof (n)));
31036ff6d951SJohn Birrell 		}
31046ff6d951SJohn Birrell 
31058e648814SRui Paulo 		dt_node_type_assign(dnp, dtt.dtt_ctfp, dtt.dtt_type,
31068e648814SRui Paulo 		    cp->dn_flags & DT_NF_USERLAND);
31076ff6d951SJohn Birrell 		break;
31086ff6d951SJohn Birrell 
31096ff6d951SJohn Birrell 	case DT_TOK_SIZEOF:
31106ff6d951SJohn Birrell 		if (cp->dn_flags & DT_NF_BITFIELD) {
31116ff6d951SJohn Birrell 			xyerror(D_SIZEOF_BITFIELD,
31126ff6d951SJohn Birrell 			    "cannot apply sizeof to a bit-field\n");
31136ff6d951SJohn Birrell 		}
31146ff6d951SJohn Birrell 
31156ff6d951SJohn Birrell 		if (dt_node_sizeof(cp) == 0) {
31166ff6d951SJohn Birrell 			xyerror(D_SIZEOF_TYPE, "cannot apply sizeof to an "
31176ff6d951SJohn Birrell 			    "operand of unknown size\n");
31186ff6d951SJohn Birrell 		}
31196ff6d951SJohn Birrell 
31206ff6d951SJohn Birrell 		dt_node_type_assign(dnp, dtp->dt_ddefs->dm_ctfp,
31218e648814SRui Paulo 		    ctf_lookup_by_name(dtp->dt_ddefs->dm_ctfp, "size_t"),
31228e648814SRui Paulo 		    B_FALSE);
31236ff6d951SJohn Birrell 		break;
31246ff6d951SJohn Birrell 
31256ff6d951SJohn Birrell 	case DT_TOK_STRINGOF:
31266ff6d951SJohn Birrell 		if (!dt_node_is_scalar(cp) && !dt_node_is_pointer(cp) &&
31276ff6d951SJohn Birrell 		    !dt_node_is_strcompat(cp)) {
31286ff6d951SJohn Birrell 			xyerror(D_STRINGOF_TYPE,
31296ff6d951SJohn Birrell 			    "cannot apply stringof to a value of type %s\n",
31306ff6d951SJohn Birrell 			    dt_node_type_name(cp, n, sizeof (n)));
31316ff6d951SJohn Birrell 		}
31328e648814SRui Paulo 		dt_node_type_assign(dnp, DT_STR_CTFP(dtp), DT_STR_TYPE(dtp),
31338e648814SRui Paulo 		    cp->dn_flags & DT_NF_USERLAND);
31346ff6d951SJohn Birrell 		break;
31356ff6d951SJohn Birrell 
31366ff6d951SJohn Birrell 	case DT_TOK_PREINC:
31376ff6d951SJohn Birrell 	case DT_TOK_POSTINC:
31386ff6d951SJohn Birrell 	case DT_TOK_PREDEC:
31396ff6d951SJohn Birrell 	case DT_TOK_POSTDEC:
31406ff6d951SJohn Birrell 		if (dt_node_is_scalar(cp) == 0) {
31416ff6d951SJohn Birrell 			xyerror(D_OP_SCALAR, "operator %s requires operand of "
31426ff6d951SJohn Birrell 			    "scalar type\n", opstr(dnp->dn_op));
31436ff6d951SJohn Birrell 		}
31446ff6d951SJohn Birrell 
31456ff6d951SJohn Birrell 		if (dt_node_is_vfptr(cp)) {
31466ff6d951SJohn Birrell 			xyerror(D_OP_VFPTR, "operator %s requires an operand "
31476ff6d951SJohn Birrell 			    "of known size\n", opstr(dnp->dn_op));
31486ff6d951SJohn Birrell 		}
31496ff6d951SJohn Birrell 
31506ff6d951SJohn Birrell 		if (!(cp->dn_flags & DT_NF_LVALUE)) {
31516ff6d951SJohn Birrell 			xyerror(D_OP_LVAL, "operator %s requires modifiable "
31526ff6d951SJohn Birrell 			    "lvalue as an operand\n", opstr(dnp->dn_op));
31536ff6d951SJohn Birrell 		}
31546ff6d951SJohn Birrell 
31556ff6d951SJohn Birrell 		if (!(cp->dn_flags & DT_NF_WRITABLE)) {
31566ff6d951SJohn Birrell 			xyerror(D_OP_WRITE, "operator %s can only be applied "
31576ff6d951SJohn Birrell 			    "to a writable variable\n", opstr(dnp->dn_op));
31586ff6d951SJohn Birrell 		}
31596ff6d951SJohn Birrell 
31606ff6d951SJohn Birrell 		dt_node_type_propagate(cp, dnp); /* see K&R[A7.4.1] */
31616ff6d951SJohn Birrell 		break;
31626ff6d951SJohn Birrell 
31636ff6d951SJohn Birrell 	default:
31646ff6d951SJohn Birrell 		xyerror(D_UNKNOWN, "invalid unary op %s\n", opstr(dnp->dn_op));
31656ff6d951SJohn Birrell 	}
31666ff6d951SJohn Birrell 
31676ff6d951SJohn Birrell 	dt_node_attr_assign(dnp, cp->dn_attr);
31686ff6d951SJohn Birrell 	return (dnp);
31696ff6d951SJohn Birrell }
31706ff6d951SJohn Birrell 
3171a98ff317SPedro F. Giffuni static void
3172a98ff317SPedro F. Giffuni dt_assign_common(dt_node_t *dnp)
3173a98ff317SPedro F. Giffuni {
3174a98ff317SPedro F. Giffuni 	dt_node_t *lp = dnp->dn_left;
3175a98ff317SPedro F. Giffuni 	dt_node_t *rp = dnp->dn_right;
3176a98ff317SPedro F. Giffuni 	int op = dnp->dn_op;
3177a98ff317SPedro F. Giffuni 
3178a98ff317SPedro F. Giffuni 	if (rp->dn_kind == DT_NODE_INT)
3179a98ff317SPedro F. Giffuni 		dt_cast(lp, rp);
3180a98ff317SPedro F. Giffuni 
3181a98ff317SPedro F. Giffuni 	if (!(lp->dn_flags & DT_NF_LVALUE)) {
3182a98ff317SPedro F. Giffuni 		xyerror(D_OP_LVAL, "operator %s requires modifiable "
3183a98ff317SPedro F. Giffuni 		    "lvalue as an operand\n", opstr(op));
3184a98ff317SPedro F. Giffuni 		/* see K&R[A7.17] */
3185a98ff317SPedro F. Giffuni 	}
3186a98ff317SPedro F. Giffuni 
3187a98ff317SPedro F. Giffuni 	if (!(lp->dn_flags & DT_NF_WRITABLE)) {
3188a98ff317SPedro F. Giffuni 		xyerror(D_OP_WRITE, "operator %s can only be applied "
3189a98ff317SPedro F. Giffuni 		    "to a writable variable\n", opstr(op));
3190a98ff317SPedro F. Giffuni 	}
3191a98ff317SPedro F. Giffuni 
3192a98ff317SPedro F. Giffuni 	dt_node_type_propagate(lp, dnp); /* see K&R[A7.17] */
3193a98ff317SPedro F. Giffuni 	dt_node_attr_assign(dnp, dt_attr_min(lp->dn_attr, rp->dn_attr));
3194a98ff317SPedro F. Giffuni }
3195a98ff317SPedro F. Giffuni 
31966ff6d951SJohn Birrell static dt_node_t *
31976ff6d951SJohn Birrell dt_cook_op2(dt_node_t *dnp, uint_t idflags)
31986ff6d951SJohn Birrell {
31996ff6d951SJohn Birrell 	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
32006ff6d951SJohn Birrell 	dt_node_t *lp = dnp->dn_left;
32016ff6d951SJohn Birrell 	dt_node_t *rp = dnp->dn_right;
32026ff6d951SJohn Birrell 	int op = dnp->dn_op;
32036ff6d951SJohn Birrell 
32046ff6d951SJohn Birrell 	ctf_membinfo_t m;
32056ff6d951SJohn Birrell 	ctf_file_t *ctfp;
32066ff6d951SJohn Birrell 	ctf_id_t type;
32076ff6d951SJohn Birrell 	int kind, val, uref;
32086ff6d951SJohn Birrell 	dt_ident_t *idp;
32096ff6d951SJohn Birrell 
32106ff6d951SJohn Birrell 	char n1[DT_TYPE_NAMELEN];
32116ff6d951SJohn Birrell 	char n2[DT_TYPE_NAMELEN];
32126ff6d951SJohn Birrell 
32136ff6d951SJohn Birrell 	/*
32146ff6d951SJohn Birrell 	 * The expression E1[E2] is identical by definition to *((E1)+(E2)) so
32156ff6d951SJohn Birrell 	 * we convert "[" to "+" and glue on "*" at the end (see K&R[A7.3.1])
32166ff6d951SJohn Birrell 	 * unless the left-hand side is an untyped D scalar, associative array,
32176ff6d951SJohn Birrell 	 * or aggregation.  In these cases, we proceed to case DT_TOK_LBRAC and
32186ff6d951SJohn Birrell 	 * handle associative array and aggregation references there.
32196ff6d951SJohn Birrell 	 */
32206ff6d951SJohn Birrell 	if (op == DT_TOK_LBRAC) {
32216ff6d951SJohn Birrell 		if (lp->dn_kind == DT_NODE_IDENT) {
32226ff6d951SJohn Birrell 			dt_idhash_t *dhp;
32236ff6d951SJohn Birrell 			uint_t idkind;
32246ff6d951SJohn Birrell 
32256ff6d951SJohn Birrell 			if (lp->dn_op == DT_TOK_AGG) {
32266ff6d951SJohn Birrell 				dhp = dtp->dt_aggs;
32276ff6d951SJohn Birrell 				idp = dt_idhash_lookup(dhp, lp->dn_string + 1);
32286ff6d951SJohn Birrell 				idkind = DT_IDENT_AGG;
32296ff6d951SJohn Birrell 			} else {
32306ff6d951SJohn Birrell 				dhp = dtp->dt_globals;
32316ff6d951SJohn Birrell 				idp = dt_idstack_lookup(
32326ff6d951SJohn Birrell 				    &yypcb->pcb_globals, lp->dn_string);
32336ff6d951SJohn Birrell 				idkind = DT_IDENT_ARRAY;
32346ff6d951SJohn Birrell 			}
32356ff6d951SJohn Birrell 
32366ff6d951SJohn Birrell 			if (idp == NULL || dt_ident_unref(idp))
32376ff6d951SJohn Birrell 				dt_xcook_ident(lp, dhp, idkind, B_TRUE);
32386ff6d951SJohn Birrell 			else
32396ff6d951SJohn Birrell 				dt_xcook_ident(lp, dhp, idp->di_kind, B_FALSE);
3240650f66acSMark Johnston 		} else {
32416ff6d951SJohn Birrell 			lp = dnp->dn_left = dt_node_cook(lp, 0);
3242650f66acSMark Johnston 		}
32436ff6d951SJohn Birrell 
32446ff6d951SJohn Birrell 		/*
32456ff6d951SJohn Birrell 		 * Switch op to '+' for *(E1 + E2) array mode in these cases:
32466ff6d951SJohn Birrell 		 * (a) lp is a DT_IDENT_ARRAY variable that has already been
32476ff6d951SJohn Birrell 		 *	referenced using [] notation (dn_args != NULL).
32486ff6d951SJohn Birrell 		 * (b) lp is a non-ARRAY variable that has already been given
32496ff6d951SJohn Birrell 		 *	a type by assignment or declaration (!dt_ident_unref())
32506ff6d951SJohn Birrell 		 * (c) lp is neither a variable nor an aggregation
32516ff6d951SJohn Birrell 		 */
32526ff6d951SJohn Birrell 		if (lp->dn_kind == DT_NODE_VAR) {
32536ff6d951SJohn Birrell 			if (lp->dn_ident->di_kind == DT_IDENT_ARRAY) {
32546ff6d951SJohn Birrell 				if (lp->dn_args != NULL)
32556ff6d951SJohn Birrell 					op = DT_TOK_ADD;
3256650f66acSMark Johnston 			} else if (!dt_ident_unref(lp->dn_ident)) {
32576ff6d951SJohn Birrell 				op = DT_TOK_ADD;
3258650f66acSMark Johnston 			}
3259650f66acSMark Johnston 		} else if (lp->dn_kind != DT_NODE_AGG) {
32606ff6d951SJohn Birrell 			op = DT_TOK_ADD;
32616ff6d951SJohn Birrell 		}
3262650f66acSMark Johnston 	}
32636ff6d951SJohn Birrell 
32646ff6d951SJohn Birrell 	switch (op) {
32656ff6d951SJohn Birrell 	case DT_TOK_BAND:
32666ff6d951SJohn Birrell 	case DT_TOK_XOR:
32676ff6d951SJohn Birrell 	case DT_TOK_BOR:
32686ff6d951SJohn Birrell 		lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF);
32696ff6d951SJohn Birrell 		rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
32706ff6d951SJohn Birrell 
32716ff6d951SJohn Birrell 		if (!dt_node_is_integer(lp) || !dt_node_is_integer(rp)) {
32726ff6d951SJohn Birrell 			xyerror(D_OP_INT, "operator %s requires operands of "
32736ff6d951SJohn Birrell 			    "integral type\n", opstr(op));
32746ff6d951SJohn Birrell 		}
32756ff6d951SJohn Birrell 
32766ff6d951SJohn Birrell 		dt_node_promote(lp, rp, dnp); /* see K&R[A7.11-13] */
32776ff6d951SJohn Birrell 		break;
32786ff6d951SJohn Birrell 
32796ff6d951SJohn Birrell 	case DT_TOK_LSH:
32806ff6d951SJohn Birrell 	case DT_TOK_RSH:
32816ff6d951SJohn Birrell 		lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF);
32826ff6d951SJohn Birrell 		rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
32836ff6d951SJohn Birrell 
32846ff6d951SJohn Birrell 		if (!dt_node_is_integer(lp) || !dt_node_is_integer(rp)) {
32856ff6d951SJohn Birrell 			xyerror(D_OP_INT, "operator %s requires operands of "
32866ff6d951SJohn Birrell 			    "integral type\n", opstr(op));
32876ff6d951SJohn Birrell 		}
32886ff6d951SJohn Birrell 
32896ff6d951SJohn Birrell 		dt_node_type_propagate(lp, dnp); /* see K&R[A7.8] */
32906ff6d951SJohn Birrell 		dt_node_attr_assign(dnp, dt_attr_min(lp->dn_attr, rp->dn_attr));
32916ff6d951SJohn Birrell 		break;
32926ff6d951SJohn Birrell 
32936ff6d951SJohn Birrell 	case DT_TOK_MOD:
32946ff6d951SJohn Birrell 		lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF);
32956ff6d951SJohn Birrell 		rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
32966ff6d951SJohn Birrell 
32976ff6d951SJohn Birrell 		if (!dt_node_is_integer(lp) || !dt_node_is_integer(rp)) {
32986ff6d951SJohn Birrell 			xyerror(D_OP_INT, "operator %s requires operands of "
32996ff6d951SJohn Birrell 			    "integral type\n", opstr(op));
33006ff6d951SJohn Birrell 		}
33016ff6d951SJohn Birrell 
33026ff6d951SJohn Birrell 		dt_node_promote(lp, rp, dnp); /* see K&R[A7.6] */
33036ff6d951SJohn Birrell 		break;
33046ff6d951SJohn Birrell 
33056ff6d951SJohn Birrell 	case DT_TOK_MUL:
33066ff6d951SJohn Birrell 	case DT_TOK_DIV:
33076ff6d951SJohn Birrell 		lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF);
33086ff6d951SJohn Birrell 		rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
33096ff6d951SJohn Birrell 
33106ff6d951SJohn Birrell 		if (!dt_node_is_arith(lp) || !dt_node_is_arith(rp)) {
33116ff6d951SJohn Birrell 			xyerror(D_OP_ARITH, "operator %s requires operands of "
33126ff6d951SJohn Birrell 			    "arithmetic type\n", opstr(op));
33136ff6d951SJohn Birrell 		}
33146ff6d951SJohn Birrell 
33156ff6d951SJohn Birrell 		dt_node_promote(lp, rp, dnp); /* see K&R[A7.6] */
33166ff6d951SJohn Birrell 		break;
33176ff6d951SJohn Birrell 
33186ff6d951SJohn Birrell 	case DT_TOK_LAND:
33196ff6d951SJohn Birrell 	case DT_TOK_LXOR:
33206ff6d951SJohn Birrell 	case DT_TOK_LOR:
33216ff6d951SJohn Birrell 		lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF);
33226ff6d951SJohn Birrell 		rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
33236ff6d951SJohn Birrell 
33246ff6d951SJohn Birrell 		if (!dt_node_is_scalar(lp) || !dt_node_is_scalar(rp)) {
33256ff6d951SJohn Birrell 			xyerror(D_OP_SCALAR, "operator %s requires operands "
33266ff6d951SJohn Birrell 			    "of scalar type\n", opstr(op));
33276ff6d951SJohn Birrell 		}
33286ff6d951SJohn Birrell 
33298e648814SRui Paulo 		dt_node_type_assign(dnp, DT_INT_CTFP(dtp), DT_INT_TYPE(dtp),
33308e648814SRui Paulo 		    B_FALSE);
33316ff6d951SJohn Birrell 		dt_node_attr_assign(dnp, dt_attr_min(lp->dn_attr, rp->dn_attr));
33326ff6d951SJohn Birrell 		break;
33336ff6d951SJohn Birrell 
33346ff6d951SJohn Birrell 	case DT_TOK_LT:
33356ff6d951SJohn Birrell 	case DT_TOK_LE:
33366ff6d951SJohn Birrell 	case DT_TOK_GT:
33376ff6d951SJohn Birrell 	case DT_TOK_GE:
33386ff6d951SJohn Birrell 	case DT_TOK_EQU:
33396ff6d951SJohn Birrell 	case DT_TOK_NEQ:
33406ff6d951SJohn Birrell 		/*
33416ff6d951SJohn Birrell 		 * The D comparison operators provide the ability to transform
33426ff6d951SJohn Birrell 		 * a right-hand identifier into a corresponding enum tag value
33436ff6d951SJohn Birrell 		 * if the left-hand side is an enum type.  To do this, we cook
33446ff6d951SJohn Birrell 		 * the left-hand side, and then see if the right-hand side is
33456ff6d951SJohn Birrell 		 * an unscoped identifier defined in the enum.  If so, we
33466ff6d951SJohn Birrell 		 * convert into an integer constant node with the tag's value.
33476ff6d951SJohn Birrell 		 */
33486ff6d951SJohn Birrell 		lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF);
33496ff6d951SJohn Birrell 
33506ff6d951SJohn Birrell 		kind = ctf_type_kind(lp->dn_ctfp,
33516ff6d951SJohn Birrell 		    ctf_type_resolve(lp->dn_ctfp, lp->dn_type));
33526ff6d951SJohn Birrell 
33536ff6d951SJohn Birrell 		if (kind == CTF_K_ENUM && rp->dn_kind == DT_NODE_IDENT &&
33546ff6d951SJohn Birrell 		    strchr(rp->dn_string, '`') == NULL && ctf_enum_value(
33556ff6d951SJohn Birrell 		    lp->dn_ctfp, lp->dn_type, rp->dn_string, &val) == 0) {
33566ff6d951SJohn Birrell 
33576ff6d951SJohn Birrell 			if ((idp = dt_idstack_lookup(&yypcb->pcb_globals,
33586ff6d951SJohn Birrell 			    rp->dn_string)) != NULL) {
33596ff6d951SJohn Birrell 				xyerror(D_IDENT_AMBIG,
33606ff6d951SJohn Birrell 				    "ambiguous use of operator %s: %s is "
33616ff6d951SJohn Birrell 				    "both a %s enum tag and a global %s\n",
33626ff6d951SJohn Birrell 				    opstr(op), rp->dn_string,
33636ff6d951SJohn Birrell 				    dt_node_type_name(lp, n1, sizeof (n1)),
33646ff6d951SJohn Birrell 				    dt_idkind_name(idp->di_kind));
33656ff6d951SJohn Birrell 			}
33666ff6d951SJohn Birrell 
33676ff6d951SJohn Birrell 			free(rp->dn_string);
33686ff6d951SJohn Birrell 			rp->dn_string = NULL;
33696ff6d951SJohn Birrell 			rp->dn_kind = DT_NODE_INT;
33706ff6d951SJohn Birrell 			rp->dn_flags |= DT_NF_COOKED;
33716ff6d951SJohn Birrell 			rp->dn_op = DT_TOK_INT;
33726ff6d951SJohn Birrell 			rp->dn_value = (intmax_t)val;
33736ff6d951SJohn Birrell 
33748e648814SRui Paulo 			dt_node_type_assign(rp, lp->dn_ctfp, lp->dn_type,
33758e648814SRui Paulo 			    B_FALSE);
33766ff6d951SJohn Birrell 			dt_node_attr_assign(rp, _dtrace_symattr);
33776ff6d951SJohn Birrell 		}
33786ff6d951SJohn Birrell 
33796ff6d951SJohn Birrell 		rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
33806ff6d951SJohn Birrell 
33816ff6d951SJohn Birrell 		/*
33826ff6d951SJohn Birrell 		 * The rules for type checking for the relational operators are
33836ff6d951SJohn Birrell 		 * described in the ANSI-C spec (see K&R[A7.9-10]).  We perform
33846ff6d951SJohn Birrell 		 * the various tests in order from least to most expensive.  We
33856ff6d951SJohn Birrell 		 * also allow derived strings to be compared as a first-class
33866ff6d951SJohn Birrell 		 * type (resulting in a strcmp(3C)-style comparison), and we
33876ff6d951SJohn Birrell 		 * slightly relax the A7.9 rules to permit void pointer
33886ff6d951SJohn Birrell 		 * comparisons as in A7.10.  Our users won't be confused by
33896ff6d951SJohn Birrell 		 * this since they understand pointers are just numbers, and
33906ff6d951SJohn Birrell 		 * relaxing this constraint simplifies the implementation.
33916ff6d951SJohn Birrell 		 */
33926ff6d951SJohn Birrell 		if (ctf_type_compat(lp->dn_ctfp, lp->dn_type,
33936ff6d951SJohn Birrell 		    rp->dn_ctfp, rp->dn_type))
33946ff6d951SJohn Birrell 			/*EMPTY*/;
33956ff6d951SJohn Birrell 		else if (dt_node_is_integer(lp) && dt_node_is_integer(rp))
33966ff6d951SJohn Birrell 			/*EMPTY*/;
33976ff6d951SJohn Birrell 		else if (dt_node_is_strcompat(lp) && dt_node_is_strcompat(rp) &&
33986ff6d951SJohn Birrell 		    (dt_node_is_string(lp) || dt_node_is_string(rp)))
33996ff6d951SJohn Birrell 			/*EMPTY*/;
34006ff6d951SJohn Birrell 		else if (dt_node_is_ptrcompat(lp, rp, NULL, NULL) == 0) {
34016ff6d951SJohn Birrell 			xyerror(D_OP_INCOMPAT, "operands have "
34026ff6d951SJohn Birrell 			    "incompatible types: \"%s\" %s \"%s\"\n",
34036ff6d951SJohn Birrell 			    dt_node_type_name(lp, n1, sizeof (n1)), opstr(op),
34046ff6d951SJohn Birrell 			    dt_node_type_name(rp, n2, sizeof (n2)));
34056ff6d951SJohn Birrell 		}
34066ff6d951SJohn Birrell 
34078e648814SRui Paulo 		dt_node_type_assign(dnp, DT_INT_CTFP(dtp), DT_INT_TYPE(dtp),
34088e648814SRui Paulo 		    B_FALSE);
34096ff6d951SJohn Birrell 		dt_node_attr_assign(dnp, dt_attr_min(lp->dn_attr, rp->dn_attr));
34106ff6d951SJohn Birrell 		break;
34116ff6d951SJohn Birrell 
34126ff6d951SJohn Birrell 	case DT_TOK_ADD:
34136ff6d951SJohn Birrell 	case DT_TOK_SUB: {
34146ff6d951SJohn Birrell 		/*
34156ff6d951SJohn Birrell 		 * The rules for type checking for the additive operators are
34166ff6d951SJohn Birrell 		 * described in the ANSI-C spec (see K&R[A7.7]).  Pointers and
34176ff6d951SJohn Birrell 		 * integers may be manipulated according to specific rules.  In
34186ff6d951SJohn Birrell 		 * these cases D permits strings to be treated as pointers.
34196ff6d951SJohn Birrell 		 */
34206ff6d951SJohn Birrell 		int lp_is_ptr, lp_is_int, rp_is_ptr, rp_is_int;
34216ff6d951SJohn Birrell 
34226ff6d951SJohn Birrell 		lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF);
34236ff6d951SJohn Birrell 		rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
34246ff6d951SJohn Birrell 
34256ff6d951SJohn Birrell 		lp_is_ptr = dt_node_is_string(lp) ||
34266ff6d951SJohn Birrell 		    (dt_node_is_pointer(lp) && !dt_node_is_vfptr(lp));
34276ff6d951SJohn Birrell 		lp_is_int = dt_node_is_integer(lp);
34286ff6d951SJohn Birrell 
34296ff6d951SJohn Birrell 		rp_is_ptr = dt_node_is_string(rp) ||
34306ff6d951SJohn Birrell 		    (dt_node_is_pointer(rp) && !dt_node_is_vfptr(rp));
34316ff6d951SJohn Birrell 		rp_is_int = dt_node_is_integer(rp);
34326ff6d951SJohn Birrell 
34336ff6d951SJohn Birrell 		if (lp_is_int && rp_is_int) {
34346ff6d951SJohn Birrell 			dt_type_promote(lp, rp, &ctfp, &type);
34356ff6d951SJohn Birrell 			uref = 0;
34366ff6d951SJohn Birrell 		} else if (lp_is_ptr && rp_is_int) {
34376ff6d951SJohn Birrell 			ctfp = lp->dn_ctfp;
34386ff6d951SJohn Birrell 			type = lp->dn_type;
34396ff6d951SJohn Birrell 			uref = lp->dn_flags & DT_NF_USERLAND;
34406ff6d951SJohn Birrell 		} else if (lp_is_int && rp_is_ptr && op == DT_TOK_ADD) {
34416ff6d951SJohn Birrell 			ctfp = rp->dn_ctfp;
34426ff6d951SJohn Birrell 			type = rp->dn_type;
34436ff6d951SJohn Birrell 			uref = rp->dn_flags & DT_NF_USERLAND;
34446ff6d951SJohn Birrell 		} else if (lp_is_ptr && rp_is_ptr && op == DT_TOK_SUB &&
34456ff6d951SJohn Birrell 		    dt_node_is_ptrcompat(lp, rp, NULL, NULL)) {
34466ff6d951SJohn Birrell 			ctfp = dtp->dt_ddefs->dm_ctfp;
34476ff6d951SJohn Birrell 			type = ctf_lookup_by_name(ctfp, "ptrdiff_t");
34486ff6d951SJohn Birrell 			uref = 0;
34496ff6d951SJohn Birrell 		} else {
34506ff6d951SJohn Birrell 			xyerror(D_OP_INCOMPAT, "operands have incompatible "
34516ff6d951SJohn Birrell 			    "types: \"%s\" %s \"%s\"\n",
34526ff6d951SJohn Birrell 			    dt_node_type_name(lp, n1, sizeof (n1)), opstr(op),
34536ff6d951SJohn Birrell 			    dt_node_type_name(rp, n2, sizeof (n2)));
34546ff6d951SJohn Birrell 		}
34556ff6d951SJohn Birrell 
34568e648814SRui Paulo 		dt_node_type_assign(dnp, ctfp, type, B_FALSE);
34576ff6d951SJohn Birrell 		dt_node_attr_assign(dnp, dt_attr_min(lp->dn_attr, rp->dn_attr));
34586ff6d951SJohn Birrell 
34596ff6d951SJohn Birrell 		if (uref)
34606ff6d951SJohn Birrell 			dnp->dn_flags |= DT_NF_USERLAND;
34616ff6d951SJohn Birrell 		break;
34626ff6d951SJohn Birrell 	}
34636ff6d951SJohn Birrell 
34646ff6d951SJohn Birrell 	case DT_TOK_OR_EQ:
34656ff6d951SJohn Birrell 	case DT_TOK_XOR_EQ:
34666ff6d951SJohn Birrell 	case DT_TOK_AND_EQ:
34676ff6d951SJohn Birrell 	case DT_TOK_LSH_EQ:
34686ff6d951SJohn Birrell 	case DT_TOK_RSH_EQ:
34696ff6d951SJohn Birrell 	case DT_TOK_MOD_EQ:
34706ff6d951SJohn Birrell 		if (lp->dn_kind == DT_NODE_IDENT) {
34716ff6d951SJohn Birrell 			dt_xcook_ident(lp, dtp->dt_globals,
34726ff6d951SJohn Birrell 			    DT_IDENT_SCALAR, B_TRUE);
34736ff6d951SJohn Birrell 		}
34746ff6d951SJohn Birrell 
34756ff6d951SJohn Birrell 		lp = dnp->dn_left =
34766ff6d951SJohn Birrell 		    dt_node_cook(lp, DT_IDFLG_REF | DT_IDFLG_MOD);
34776ff6d951SJohn Birrell 
34786ff6d951SJohn Birrell 		rp = dnp->dn_right =
34796ff6d951SJohn Birrell 		    dt_node_cook(rp, DT_IDFLG_REF | DT_IDFLG_MOD);
34806ff6d951SJohn Birrell 
34816ff6d951SJohn Birrell 		if (!dt_node_is_integer(lp) || !dt_node_is_integer(rp)) {
34826ff6d951SJohn Birrell 			xyerror(D_OP_INT, "operator %s requires operands of "
34836ff6d951SJohn Birrell 			    "integral type\n", opstr(op));
34846ff6d951SJohn Birrell 		}
34856ff6d951SJohn Birrell 		goto asgn_common;
34866ff6d951SJohn Birrell 
34876ff6d951SJohn Birrell 	case DT_TOK_MUL_EQ:
34886ff6d951SJohn Birrell 	case DT_TOK_DIV_EQ:
34896ff6d951SJohn Birrell 		if (lp->dn_kind == DT_NODE_IDENT) {
34906ff6d951SJohn Birrell 			dt_xcook_ident(lp, dtp->dt_globals,
34916ff6d951SJohn Birrell 			    DT_IDENT_SCALAR, B_TRUE);
34926ff6d951SJohn Birrell 		}
34936ff6d951SJohn Birrell 
34946ff6d951SJohn Birrell 		lp = dnp->dn_left =
34956ff6d951SJohn Birrell 		    dt_node_cook(lp, DT_IDFLG_REF | DT_IDFLG_MOD);
34966ff6d951SJohn Birrell 
34976ff6d951SJohn Birrell 		rp = dnp->dn_right =
34986ff6d951SJohn Birrell 		    dt_node_cook(rp, DT_IDFLG_REF | DT_IDFLG_MOD);
34996ff6d951SJohn Birrell 
35006ff6d951SJohn Birrell 		if (!dt_node_is_arith(lp) || !dt_node_is_arith(rp)) {
35016ff6d951SJohn Birrell 			xyerror(D_OP_ARITH, "operator %s requires operands of "
35026ff6d951SJohn Birrell 			    "arithmetic type\n", opstr(op));
35036ff6d951SJohn Birrell 		}
35046ff6d951SJohn Birrell 		goto asgn_common;
35056ff6d951SJohn Birrell 
35066ff6d951SJohn Birrell 	case DT_TOK_ASGN:
35076ff6d951SJohn Birrell 		/*
35086ff6d951SJohn Birrell 		 * If the left-hand side is an identifier, attempt to resolve
35096ff6d951SJohn Birrell 		 * it as either an aggregation or scalar variable.  We pass
35106ff6d951SJohn Birrell 		 * B_TRUE to dt_xcook_ident to indicate that a new variable can
35116ff6d951SJohn Birrell 		 * be created if no matching variable exists in the namespace.
35126ff6d951SJohn Birrell 		 */
35136ff6d951SJohn Birrell 		if (lp->dn_kind == DT_NODE_IDENT) {
35146ff6d951SJohn Birrell 			if (lp->dn_op == DT_TOK_AGG) {
35156ff6d951SJohn Birrell 				dt_xcook_ident(lp, dtp->dt_aggs,
35166ff6d951SJohn Birrell 				    DT_IDENT_AGG, B_TRUE);
35176ff6d951SJohn Birrell 			} else {
35186ff6d951SJohn Birrell 				dt_xcook_ident(lp, dtp->dt_globals,
35196ff6d951SJohn Birrell 				    DT_IDENT_SCALAR, B_TRUE);
35206ff6d951SJohn Birrell 			}
35216ff6d951SJohn Birrell 		}
35226ff6d951SJohn Birrell 
35236ff6d951SJohn Birrell 		lp = dnp->dn_left = dt_node_cook(lp, 0); /* don't set mod yet */
35246ff6d951SJohn Birrell 		rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
35256ff6d951SJohn Birrell 
35266ff6d951SJohn Birrell 		/*
35276ff6d951SJohn Birrell 		 * If the left-hand side is an aggregation, verify that we are
35286ff6d951SJohn Birrell 		 * assigning it the result of an aggregating function.  Once
35296ff6d951SJohn Birrell 		 * we've done so, hide the func node in the aggregation and
35306ff6d951SJohn Birrell 		 * return the aggregation itself up to the parse tree parent.
35316ff6d951SJohn Birrell 		 * This transformation is legal since the assigned function
35326ff6d951SJohn Birrell 		 * cannot change identity across disjoint cooking passes and
35336ff6d951SJohn Birrell 		 * the argument list subtree is retained for later cooking.
35346ff6d951SJohn Birrell 		 */
35356ff6d951SJohn Birrell 		if (lp->dn_kind == DT_NODE_AGG) {
35366ff6d951SJohn Birrell 			const char *aname = lp->dn_ident->di_name;
35376ff6d951SJohn Birrell 			dt_ident_t *oid = lp->dn_ident->di_iarg;
35386ff6d951SJohn Birrell 
35396ff6d951SJohn Birrell 			if (rp->dn_kind != DT_NODE_FUNC ||
35406ff6d951SJohn Birrell 			    rp->dn_ident->di_kind != DT_IDENT_AGGFUNC) {
35416ff6d951SJohn Birrell 				xyerror(D_AGG_FUNC,
35426ff6d951SJohn Birrell 				    "@%s must be assigned the result of "
35436ff6d951SJohn Birrell 				    "an aggregating function\n", aname);
35446ff6d951SJohn Birrell 			}
35456ff6d951SJohn Birrell 
35466ff6d951SJohn Birrell 			if (oid != NULL && oid != rp->dn_ident) {
35476ff6d951SJohn Birrell 				xyerror(D_AGG_REDEF,
35486ff6d951SJohn Birrell 				    "aggregation redefined: @%s\n\t "
35496ff6d951SJohn Birrell 				    "current: @%s = %s( )\n\tprevious: @%s = "
35506ff6d951SJohn Birrell 				    "%s( ) : line %d\n", aname, aname,
35516ff6d951SJohn Birrell 				    rp->dn_ident->di_name, aname, oid->di_name,
35526ff6d951SJohn Birrell 				    lp->dn_ident->di_lineno);
35536ff6d951SJohn Birrell 			} else if (oid == NULL)
35546ff6d951SJohn Birrell 				lp->dn_ident->di_iarg = rp->dn_ident;
35556ff6d951SJohn Birrell 
35566ff6d951SJohn Birrell 			/*
35576ff6d951SJohn Birrell 			 * Do not allow multiple aggregation assignments in a
35586ff6d951SJohn Birrell 			 * single statement, e.g. (@a = count()) = count();
35596ff6d951SJohn Birrell 			 * We produce a message as if the result of aggregating
35606ff6d951SJohn Birrell 			 * function does not propagate DT_NF_LVALUE.
35616ff6d951SJohn Birrell 			 */
35626ff6d951SJohn Birrell 			if (lp->dn_aggfun != NULL) {
35636ff6d951SJohn Birrell 				xyerror(D_OP_LVAL, "operator = requires "
35646ff6d951SJohn Birrell 				    "modifiable lvalue as an operand\n");
35656ff6d951SJohn Birrell 			}
35666ff6d951SJohn Birrell 
35676ff6d951SJohn Birrell 			lp->dn_aggfun = rp;
35686ff6d951SJohn Birrell 			lp = dt_node_cook(lp, DT_IDFLG_MOD);
35696ff6d951SJohn Birrell 
35706ff6d951SJohn Birrell 			dnp->dn_left = dnp->dn_right = NULL;
35716ff6d951SJohn Birrell 			dt_node_free(dnp);
35726ff6d951SJohn Birrell 
35736ff6d951SJohn Birrell 			return (lp);
35746ff6d951SJohn Birrell 		}
35756ff6d951SJohn Birrell 
35766ff6d951SJohn Birrell 		/*
35776ff6d951SJohn Birrell 		 * If the right-hand side is a dynamic variable that is the
35786ff6d951SJohn Birrell 		 * output of a translator, our result is the translated type.
35796ff6d951SJohn Birrell 		 */
35806ff6d951SJohn Birrell 		if ((idp = dt_node_resolve(rp, DT_IDENT_XLSOU)) != NULL) {
35816ff6d951SJohn Birrell 			ctfp = idp->di_ctfp;
35826ff6d951SJohn Birrell 			type = idp->di_type;
35836ff6d951SJohn Birrell 			uref = idp->di_flags & DT_IDFLG_USER;
35846ff6d951SJohn Birrell 		} else {
35856ff6d951SJohn Birrell 			ctfp = rp->dn_ctfp;
35866ff6d951SJohn Birrell 			type = rp->dn_type;
35876ff6d951SJohn Birrell 			uref = rp->dn_flags & DT_NF_USERLAND;
35886ff6d951SJohn Birrell 		}
35896ff6d951SJohn Birrell 
35906ff6d951SJohn Birrell 		/*
35916ff6d951SJohn Birrell 		 * If the left-hand side of an assignment statement is a virgin
35926ff6d951SJohn Birrell 		 * variable created by this compilation pass, reset the type of
35936ff6d951SJohn Birrell 		 * this variable to the type of the right-hand side.
35946ff6d951SJohn Birrell 		 */
35956ff6d951SJohn Birrell 		if (lp->dn_kind == DT_NODE_VAR &&
35966ff6d951SJohn Birrell 		    dt_ident_unref(lp->dn_ident)) {
35978e648814SRui Paulo 			dt_node_type_assign(lp, ctfp, type, B_FALSE);
35986ff6d951SJohn Birrell 			dt_ident_type_assign(lp->dn_ident, ctfp, type);
35996ff6d951SJohn Birrell 
36006ff6d951SJohn Birrell 			if (uref) {
36016ff6d951SJohn Birrell 				lp->dn_flags |= DT_NF_USERLAND;
36026ff6d951SJohn Birrell 				lp->dn_ident->di_flags |= DT_IDFLG_USER;
36036ff6d951SJohn Birrell 			}
36046ff6d951SJohn Birrell 		}
36056ff6d951SJohn Birrell 
36066ff6d951SJohn Birrell 		if (lp->dn_kind == DT_NODE_VAR)
36076ff6d951SJohn Birrell 			lp->dn_ident->di_flags |= DT_IDFLG_MOD;
36086ff6d951SJohn Birrell 
36096ff6d951SJohn Birrell 		/*
36106ff6d951SJohn Birrell 		 * The rules for type checking for the assignment operators are
36116ff6d951SJohn Birrell 		 * described in the ANSI-C spec (see K&R[A7.17]).  We share
36126ff6d951SJohn Birrell 		 * most of this code with the argument list checking code.
36136ff6d951SJohn Birrell 		 */
36146ff6d951SJohn Birrell 		if (!dt_node_is_string(lp)) {
36156ff6d951SJohn Birrell 			kind = ctf_type_kind(lp->dn_ctfp,
36166ff6d951SJohn Birrell 			    ctf_type_resolve(lp->dn_ctfp, lp->dn_type));
36176ff6d951SJohn Birrell 
36186ff6d951SJohn Birrell 			if (kind == CTF_K_ARRAY || kind == CTF_K_FUNCTION) {
36196ff6d951SJohn Birrell 				xyerror(D_OP_ARRFUN, "operator %s may not be "
36206ff6d951SJohn Birrell 				    "applied to operand of type \"%s\"\n",
36216ff6d951SJohn Birrell 				    opstr(op),
36226ff6d951SJohn Birrell 				    dt_node_type_name(lp, n1, sizeof (n1)));
36236ff6d951SJohn Birrell 			}
36246ff6d951SJohn Birrell 		}
36256ff6d951SJohn Birrell 
36266ff6d951SJohn Birrell 		if (idp != NULL && idp->di_kind == DT_IDENT_XLSOU &&
36276ff6d951SJohn Birrell 		    ctf_type_compat(lp->dn_ctfp, lp->dn_type, ctfp, type))
36286ff6d951SJohn Birrell 			goto asgn_common;
36296ff6d951SJohn Birrell 
36306ff6d951SJohn Birrell 		if (dt_node_is_argcompat(lp, rp))
36316ff6d951SJohn Birrell 			goto asgn_common;
36326ff6d951SJohn Birrell 
36336ff6d951SJohn Birrell 		xyerror(D_OP_INCOMPAT,
36346ff6d951SJohn Birrell 		    "operands have incompatible types: \"%s\" %s \"%s\"\n",
36356ff6d951SJohn Birrell 		    dt_node_type_name(lp, n1, sizeof (n1)), opstr(op),
36366ff6d951SJohn Birrell 		    dt_node_type_name(rp, n2, sizeof (n2)));
36376ff6d951SJohn Birrell 		/*NOTREACHED*/
36386ff6d951SJohn Birrell 
36396ff6d951SJohn Birrell 	case DT_TOK_ADD_EQ:
36406ff6d951SJohn Birrell 	case DT_TOK_SUB_EQ:
36416ff6d951SJohn Birrell 		if (lp->dn_kind == DT_NODE_IDENT) {
36426ff6d951SJohn Birrell 			dt_xcook_ident(lp, dtp->dt_globals,
36436ff6d951SJohn Birrell 			    DT_IDENT_SCALAR, B_TRUE);
36446ff6d951SJohn Birrell 		}
36456ff6d951SJohn Birrell 
36466ff6d951SJohn Birrell 		lp = dnp->dn_left =
36476ff6d951SJohn Birrell 		    dt_node_cook(lp, DT_IDFLG_REF | DT_IDFLG_MOD);
36486ff6d951SJohn Birrell 
36496ff6d951SJohn Birrell 		rp = dnp->dn_right =
36506ff6d951SJohn Birrell 		    dt_node_cook(rp, DT_IDFLG_REF | DT_IDFLG_MOD);
36516ff6d951SJohn Birrell 
36526ff6d951SJohn Birrell 		if (dt_node_is_string(lp) || dt_node_is_string(rp)) {
36536ff6d951SJohn Birrell 			xyerror(D_OP_INCOMPAT, "operands have "
36546ff6d951SJohn Birrell 			    "incompatible types: \"%s\" %s \"%s\"\n",
36556ff6d951SJohn Birrell 			    dt_node_type_name(lp, n1, sizeof (n1)), opstr(op),
36566ff6d951SJohn Birrell 			    dt_node_type_name(rp, n2, sizeof (n2)));
36576ff6d951SJohn Birrell 		}
36586ff6d951SJohn Birrell 
36596ff6d951SJohn Birrell 		/*
36606ff6d951SJohn Birrell 		 * The rules for type checking for the assignment operators are
36616ff6d951SJohn Birrell 		 * described in the ANSI-C spec (see K&R[A7.17]).  To these
36626ff6d951SJohn Birrell 		 * rules we add that only writable D nodes can be modified.
36636ff6d951SJohn Birrell 		 */
36646ff6d951SJohn Birrell 		if (dt_node_is_integer(lp) == 0 ||
36656ff6d951SJohn Birrell 		    dt_node_is_integer(rp) == 0) {
36666ff6d951SJohn Birrell 			if (!dt_node_is_pointer(lp) || dt_node_is_vfptr(lp)) {
36676ff6d951SJohn Birrell 				xyerror(D_OP_VFPTR,
36686ff6d951SJohn Birrell 				    "operator %s requires left-hand scalar "
36696ff6d951SJohn Birrell 				    "operand of known size\n", opstr(op));
36706ff6d951SJohn Birrell 			} else if (dt_node_is_integer(rp) == 0 &&
36716ff6d951SJohn Birrell 			    dt_node_is_ptrcompat(lp, rp, NULL, NULL) == 0) {
36726ff6d951SJohn Birrell 				xyerror(D_OP_INCOMPAT, "operands have "
36736ff6d951SJohn Birrell 				    "incompatible types: \"%s\" %s \"%s\"\n",
36746ff6d951SJohn Birrell 				    dt_node_type_name(lp, n1, sizeof (n1)),
36756ff6d951SJohn Birrell 				    opstr(op),
36766ff6d951SJohn Birrell 				    dt_node_type_name(rp, n2, sizeof (n2)));
36776ff6d951SJohn Birrell 			}
36786ff6d951SJohn Birrell 		}
36796ff6d951SJohn Birrell asgn_common:
3680a98ff317SPedro F. Giffuni 		dt_assign_common(dnp);
36816ff6d951SJohn Birrell 		break;
36826ff6d951SJohn Birrell 
36836ff6d951SJohn Birrell 	case DT_TOK_PTR:
36846ff6d951SJohn Birrell 		/*
3685650f66acSMark Johnston 		 * If the left-hand side of operator -> is one of the scoping
3686650f66acSMark Johnston 		 * keywords, permit a local or thread variable to be created or
3687650f66acSMark Johnston 		 * referenced.
36886ff6d951SJohn Birrell 		 */
3689650f66acSMark Johnston 		if (lp->dn_kind == DT_NODE_IDENT) {
3690650f66acSMark Johnston 			dt_idhash_t *dhp = NULL;
3691650f66acSMark Johnston 
3692650f66acSMark Johnston 			if (strcmp(lp->dn_string, "self") == 0) {
3693650f66acSMark Johnston 				dhp = dtp->dt_tls;
3694650f66acSMark Johnston 			} else if (strcmp(lp->dn_string, "this") == 0) {
3695650f66acSMark Johnston 				dhp = yypcb->pcb_locals;
3696650f66acSMark Johnston 			}
3697650f66acSMark Johnston 			if (dhp != NULL) {
36986ff6d951SJohn Birrell 				if (rp->dn_kind != DT_NODE_VAR) {
3699650f66acSMark Johnston 					dt_xcook_ident(rp, dhp,
37006ff6d951SJohn Birrell 					    DT_IDENT_SCALAR, B_TRUE);
37016ff6d951SJohn Birrell 				}
37026ff6d951SJohn Birrell 
37036ff6d951SJohn Birrell 				if (idflags != 0)
37046ff6d951SJohn Birrell 					rp = dt_node_cook(rp, idflags);
37056ff6d951SJohn Birrell 
3706650f66acSMark Johnston 				/* avoid freeing rp */
3707650f66acSMark Johnston 				dnp->dn_right = dnp->dn_left;
37086ff6d951SJohn Birrell 				dt_node_free(dnp);
37096ff6d951SJohn Birrell 				return (rp);
37106ff6d951SJohn Birrell 			}
37116ff6d951SJohn Birrell 		}
37126ff6d951SJohn Birrell 		/*FALLTHRU*/
37136ff6d951SJohn Birrell 	case DT_TOK_DOT:
37146ff6d951SJohn Birrell 		lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF);
37156ff6d951SJohn Birrell 
37166ff6d951SJohn Birrell 		if (rp->dn_kind != DT_NODE_IDENT) {
37176ff6d951SJohn Birrell 			xyerror(D_OP_IDENT, "operator %s must be followed by "
37186ff6d951SJohn Birrell 			    "an identifier\n", opstr(op));
37196ff6d951SJohn Birrell 		}
37206ff6d951SJohn Birrell 
37216ff6d951SJohn Birrell 		if ((idp = dt_node_resolve(lp, DT_IDENT_XLSOU)) != NULL ||
37226ff6d951SJohn Birrell 		    (idp = dt_node_resolve(lp, DT_IDENT_XLPTR)) != NULL) {
37236ff6d951SJohn Birrell 			/*
37246ff6d951SJohn Birrell 			 * If the left-hand side is a translated struct or ptr,
37256ff6d951SJohn Birrell 			 * the type of the left is the translation output type.
37266ff6d951SJohn Birrell 			 */
37276ff6d951SJohn Birrell 			dt_xlator_t *dxp = idp->di_data;
37286ff6d951SJohn Birrell 
37296ff6d951SJohn Birrell 			if (dt_xlator_member(dxp, rp->dn_string) == NULL) {
37306ff6d951SJohn Birrell 				xyerror(D_XLATE_NOCONV,
37316ff6d951SJohn Birrell 				    "translator does not define conversion "
37326ff6d951SJohn Birrell 				    "for member: %s\n", rp->dn_string);
37336ff6d951SJohn Birrell 			}
37346ff6d951SJohn Birrell 
37356ff6d951SJohn Birrell 			ctfp = idp->di_ctfp;
37366ff6d951SJohn Birrell 			type = ctf_type_resolve(ctfp, idp->di_type);
37376ff6d951SJohn Birrell 			uref = idp->di_flags & DT_IDFLG_USER;
37386ff6d951SJohn Birrell 		} else {
37396ff6d951SJohn Birrell 			ctfp = lp->dn_ctfp;
37406ff6d951SJohn Birrell 			type = ctf_type_resolve(ctfp, lp->dn_type);
37416ff6d951SJohn Birrell 			uref = lp->dn_flags & DT_NF_USERLAND;
37426ff6d951SJohn Birrell 		}
37436ff6d951SJohn Birrell 
37446ff6d951SJohn Birrell 		kind = ctf_type_kind(ctfp, type);
37456ff6d951SJohn Birrell 
37466ff6d951SJohn Birrell 		if (op == DT_TOK_PTR) {
37476ff6d951SJohn Birrell 			if (kind != CTF_K_POINTER) {
37486ff6d951SJohn Birrell 				xyerror(D_OP_PTR, "operator %s must be "
37496ff6d951SJohn Birrell 				    "applied to a pointer\n", opstr(op));
37506ff6d951SJohn Birrell 			}
37516ff6d951SJohn Birrell 			type = ctf_type_reference(ctfp, type);
37526ff6d951SJohn Birrell 			type = ctf_type_resolve(ctfp, type);
37536ff6d951SJohn Birrell 			kind = ctf_type_kind(ctfp, type);
37546ff6d951SJohn Birrell 		}
37556ff6d951SJohn Birrell 
37566ff6d951SJohn Birrell 		/*
37576ff6d951SJohn Birrell 		 * If we follow a reference to a forward declaration tag,
37586ff6d951SJohn Birrell 		 * search the entire type space for the actual definition.
37596ff6d951SJohn Birrell 		 */
37606ff6d951SJohn Birrell 		while (kind == CTF_K_FORWARD) {
37616ff6d951SJohn Birrell 			char *tag = ctf_type_name(ctfp, type, n1, sizeof (n1));
37626ff6d951SJohn Birrell 			dtrace_typeinfo_t dtt;
37636ff6d951SJohn Birrell 
37646ff6d951SJohn Birrell 			if (tag != NULL && dt_type_lookup(tag, &dtt) == 0 &&
37656ff6d951SJohn Birrell 			    (dtt.dtt_ctfp != ctfp || dtt.dtt_type != type)) {
37666ff6d951SJohn Birrell 				ctfp = dtt.dtt_ctfp;
37676ff6d951SJohn Birrell 				type = ctf_type_resolve(ctfp, dtt.dtt_type);
37686ff6d951SJohn Birrell 				kind = ctf_type_kind(ctfp, type);
37696ff6d951SJohn Birrell 			} else {
37706ff6d951SJohn Birrell 				xyerror(D_OP_INCOMPLETE,
37716ff6d951SJohn Birrell 				    "operator %s cannot be applied to a "
37726ff6d951SJohn Birrell 				    "forward declaration: no %s definition "
37736ff6d951SJohn Birrell 				    "is available\n", opstr(op), tag);
37746ff6d951SJohn Birrell 			}
37756ff6d951SJohn Birrell 		}
37766ff6d951SJohn Birrell 
37776ff6d951SJohn Birrell 		if (kind != CTF_K_STRUCT && kind != CTF_K_UNION) {
37786ff6d951SJohn Birrell 			if (op == DT_TOK_PTR) {
37796ff6d951SJohn Birrell 				xyerror(D_OP_SOU, "operator -> cannot be "
37806ff6d951SJohn Birrell 				    "applied to pointer to type \"%s\"; must "
37816ff6d951SJohn Birrell 				    "be applied to a struct or union pointer\n",
37826ff6d951SJohn Birrell 				    ctf_type_name(ctfp, type, n1, sizeof (n1)));
37836ff6d951SJohn Birrell 			} else {
37846ff6d951SJohn Birrell 				xyerror(D_OP_SOU, "operator %s cannot be "
37856ff6d951SJohn Birrell 				    "applied to type \"%s\"; must be applied "
37866ff6d951SJohn Birrell 				    "to a struct or union\n", opstr(op),
37876ff6d951SJohn Birrell 				    ctf_type_name(ctfp, type, n1, sizeof (n1)));
37886ff6d951SJohn Birrell 			}
37896ff6d951SJohn Birrell 		}
37906ff6d951SJohn Birrell 
37916ff6d951SJohn Birrell 		if (ctf_member_info(ctfp, type, rp->dn_string, &m) == CTF_ERR) {
37926ff6d951SJohn Birrell 			xyerror(D_TYPE_MEMBER,
37936ff6d951SJohn Birrell 			    "%s is not a member of %s\n", rp->dn_string,
37946ff6d951SJohn Birrell 			    ctf_type_name(ctfp, type, n1, sizeof (n1)));
37956ff6d951SJohn Birrell 		}
37966ff6d951SJohn Birrell 
37976ff6d951SJohn Birrell 		type = ctf_type_resolve(ctfp, m.ctm_type);
37986ff6d951SJohn Birrell 		kind = ctf_type_kind(ctfp, type);
37996ff6d951SJohn Birrell 
38008e648814SRui Paulo 		dt_node_type_assign(dnp, ctfp, m.ctm_type, B_FALSE);
38016ff6d951SJohn Birrell 		dt_node_attr_assign(dnp, lp->dn_attr);
38026ff6d951SJohn Birrell 
38036ff6d951SJohn Birrell 		if (op == DT_TOK_PTR && (kind != CTF_K_ARRAY ||
38046ff6d951SJohn Birrell 		    dt_node_is_string(dnp)))
38056ff6d951SJohn Birrell 			dnp->dn_flags |= DT_NF_LVALUE; /* see K&R[A7.3.3] */
38066ff6d951SJohn Birrell 
38076ff6d951SJohn Birrell 		if (op == DT_TOK_DOT && (lp->dn_flags & DT_NF_LVALUE) &&
38086ff6d951SJohn Birrell 		    (kind != CTF_K_ARRAY || dt_node_is_string(dnp)))
38096ff6d951SJohn Birrell 			dnp->dn_flags |= DT_NF_LVALUE; /* see K&R[A7.3.3] */
38106ff6d951SJohn Birrell 
38116ff6d951SJohn Birrell 		if (lp->dn_flags & DT_NF_WRITABLE)
38126ff6d951SJohn Birrell 			dnp->dn_flags |= DT_NF_WRITABLE;
38136ff6d951SJohn Birrell 
38146ff6d951SJohn Birrell 		if (uref && (kind == CTF_K_POINTER ||
38156ff6d951SJohn Birrell 		    (dnp->dn_flags & DT_NF_REF)))
38166ff6d951SJohn Birrell 			dnp->dn_flags |= DT_NF_USERLAND;
38176ff6d951SJohn Birrell 		break;
38186ff6d951SJohn Birrell 
38196ff6d951SJohn Birrell 	case DT_TOK_LBRAC: {
38206ff6d951SJohn Birrell 		/*
38216ff6d951SJohn Birrell 		 * If op is DT_TOK_LBRAC, we know from the special-case code at
38226ff6d951SJohn Birrell 		 * the top that lp is either a D variable or an aggregation.
38236ff6d951SJohn Birrell 		 */
38246ff6d951SJohn Birrell 		dt_node_t *lnp;
38256ff6d951SJohn Birrell 
38266ff6d951SJohn Birrell 		/*
38276ff6d951SJohn Birrell 		 * If the left-hand side is an aggregation, just set dn_aggtup
38286ff6d951SJohn Birrell 		 * to the right-hand side and return the cooked aggregation.
38296ff6d951SJohn Birrell 		 * This transformation is legal since we are just collapsing
38306ff6d951SJohn Birrell 		 * nodes to simplify later processing, and the entire aggtup
38316ff6d951SJohn Birrell 		 * parse subtree is retained for subsequent cooking passes.
38326ff6d951SJohn Birrell 		 */
38336ff6d951SJohn Birrell 		if (lp->dn_kind == DT_NODE_AGG) {
38346ff6d951SJohn Birrell 			if (lp->dn_aggtup != NULL) {
38356ff6d951SJohn Birrell 				xyerror(D_AGG_MDIM, "improper attempt to "
38366ff6d951SJohn Birrell 				    "reference @%s as a multi-dimensional "
38376ff6d951SJohn Birrell 				    "array\n", lp->dn_ident->di_name);
38386ff6d951SJohn Birrell 			}
38396ff6d951SJohn Birrell 
38406ff6d951SJohn Birrell 			lp->dn_aggtup = rp;
38416ff6d951SJohn Birrell 			lp = dt_node_cook(lp, 0);
38426ff6d951SJohn Birrell 
38436ff6d951SJohn Birrell 			dnp->dn_left = dnp->dn_right = NULL;
38446ff6d951SJohn Birrell 			dt_node_free(dnp);
38456ff6d951SJohn Birrell 
38466ff6d951SJohn Birrell 			return (lp);
38476ff6d951SJohn Birrell 		}
38486ff6d951SJohn Birrell 
38496ff6d951SJohn Birrell 		assert(lp->dn_kind == DT_NODE_VAR);
38506ff6d951SJohn Birrell 		idp = lp->dn_ident;
38516ff6d951SJohn Birrell 
38526ff6d951SJohn Birrell 		/*
38536ff6d951SJohn Birrell 		 * If the left-hand side is a non-global scalar that hasn't yet
38546ff6d951SJohn Birrell 		 * been referenced or modified, it was just created by self->
38556ff6d951SJohn Birrell 		 * or this-> and we can convert it from scalar to assoc array.
38566ff6d951SJohn Birrell 		 */
38576ff6d951SJohn Birrell 		if (idp->di_kind == DT_IDENT_SCALAR && dt_ident_unref(idp) &&
38586ff6d951SJohn Birrell 		    (idp->di_flags & (DT_IDFLG_LOCAL | DT_IDFLG_TLS)) != 0) {
38596ff6d951SJohn Birrell 
38606ff6d951SJohn Birrell 			if (idp->di_flags & DT_IDFLG_LOCAL) {
38616ff6d951SJohn Birrell 				xyerror(D_ARR_LOCAL,
38626ff6d951SJohn Birrell 				    "local variables may not be used as "
38636ff6d951SJohn Birrell 				    "associative arrays: %s\n", idp->di_name);
38646ff6d951SJohn Birrell 			}
38656ff6d951SJohn Birrell 
38666ff6d951SJohn Birrell 			dt_dprintf("morph variable %s (id %u) from scalar to "
38676ff6d951SJohn Birrell 			    "array\n", idp->di_name, idp->di_id);
38686ff6d951SJohn Birrell 
38696ff6d951SJohn Birrell 			dt_ident_morph(idp, DT_IDENT_ARRAY,
38706ff6d951SJohn Birrell 			    &dt_idops_assc, NULL);
38716ff6d951SJohn Birrell 		}
38726ff6d951SJohn Birrell 
38736ff6d951SJohn Birrell 		if (idp->di_kind != DT_IDENT_ARRAY) {
38746ff6d951SJohn Birrell 			xyerror(D_IDENT_BADREF, "%s '%s' may not be referenced "
38756ff6d951SJohn Birrell 			    "as %s\n", dt_idkind_name(idp->di_kind),
38766ff6d951SJohn Birrell 			    idp->di_name, dt_idkind_name(DT_IDENT_ARRAY));
38776ff6d951SJohn Birrell 		}
38786ff6d951SJohn Birrell 
38796ff6d951SJohn Birrell 		/*
38806ff6d951SJohn Birrell 		 * Now that we've confirmed our left-hand side is a DT_NODE_VAR
38816ff6d951SJohn Birrell 		 * of idkind DT_IDENT_ARRAY, we need to splice the [ node from
38826ff6d951SJohn Birrell 		 * the parse tree and leave a cooked DT_NODE_VAR in its place
38836ff6d951SJohn Birrell 		 * where dn_args for the VAR node is the right-hand 'rp' tree,
38846ff6d951SJohn Birrell 		 * as shown in the parse tree diagram below:
38856ff6d951SJohn Birrell 		 *
38866ff6d951SJohn Birrell 		 *	  /			    /
38876ff6d951SJohn Birrell 		 * [ OP2 "[" ]=dnp		[ VAR ]=dnp
38886ff6d951SJohn Birrell 		 *	 /	\	  =>	   |
38896ff6d951SJohn Birrell 		 *	/	 \		   +- dn_args -> [ ??? ]=rp
38906ff6d951SJohn Birrell 		 * [ VAR ]=lp  [ ??? ]=rp
38916ff6d951SJohn Birrell 		 *
38926ff6d951SJohn Birrell 		 * Since the final dt_node_cook(dnp) can fail using longjmp we
38936ff6d951SJohn Birrell 		 * must perform the transformations as a group first by over-
38946ff6d951SJohn Birrell 		 * writing 'dnp' to become the VAR node, so that the parse tree
38956ff6d951SJohn Birrell 		 * is guaranteed to be in a consistent state if the cook fails.
38966ff6d951SJohn Birrell 		 */
38976ff6d951SJohn Birrell 		assert(lp->dn_kind == DT_NODE_VAR);
38986ff6d951SJohn Birrell 		assert(lp->dn_args == NULL);
38996ff6d951SJohn Birrell 
39006ff6d951SJohn Birrell 		lnp = dnp->dn_link;
39016ff6d951SJohn Birrell 		bcopy(lp, dnp, sizeof (dt_node_t));
39026ff6d951SJohn Birrell 		dnp->dn_link = lnp;
39036ff6d951SJohn Birrell 
39046ff6d951SJohn Birrell 		dnp->dn_args = rp;
39056ff6d951SJohn Birrell 		dnp->dn_list = NULL;
39066ff6d951SJohn Birrell 
39076ff6d951SJohn Birrell 		dt_node_free(lp);
39086ff6d951SJohn Birrell 		return (dt_node_cook(dnp, idflags));
39096ff6d951SJohn Birrell 	}
39106ff6d951SJohn Birrell 
39116ff6d951SJohn Birrell 	case DT_TOK_XLATE: {
39126ff6d951SJohn Birrell 		dt_xlator_t *dxp;
39136ff6d951SJohn Birrell 
39146ff6d951SJohn Birrell 		assert(lp->dn_kind == DT_NODE_TYPE);
39156ff6d951SJohn Birrell 		rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
39166ff6d951SJohn Birrell 		dxp = dt_xlator_lookup(dtp, rp, lp, DT_XLATE_FUZZY);
39176ff6d951SJohn Birrell 
39186ff6d951SJohn Birrell 		if (dxp == NULL) {
39196ff6d951SJohn Birrell 			xyerror(D_XLATE_NONE,
39206ff6d951SJohn Birrell 			    "cannot translate from \"%s\" to \"%s\"\n",
39216ff6d951SJohn Birrell 			    dt_node_type_name(rp, n1, sizeof (n1)),
39226ff6d951SJohn Birrell 			    dt_node_type_name(lp, n2, sizeof (n2)));
39236ff6d951SJohn Birrell 		}
39246ff6d951SJohn Birrell 
39256ff6d951SJohn Birrell 		dnp->dn_ident = dt_xlator_ident(dxp, lp->dn_ctfp, lp->dn_type);
39268e648814SRui Paulo 		dt_node_type_assign(dnp, DT_DYN_CTFP(dtp), DT_DYN_TYPE(dtp),
39278e648814SRui Paulo 		    B_FALSE);
39286ff6d951SJohn Birrell 		dt_node_attr_assign(dnp,
39296ff6d951SJohn Birrell 		    dt_attr_min(rp->dn_attr, dnp->dn_ident->di_attr));
39306ff6d951SJohn Birrell 		break;
39316ff6d951SJohn Birrell 	}
39326ff6d951SJohn Birrell 
39336ff6d951SJohn Birrell 	case DT_TOK_LPAR: {
39346ff6d951SJohn Birrell 		ctf_id_t ltype, rtype;
39356ff6d951SJohn Birrell 		uint_t lkind, rkind;
39366ff6d951SJohn Birrell 
39376ff6d951SJohn Birrell 		assert(lp->dn_kind == DT_NODE_TYPE);
39386ff6d951SJohn Birrell 		rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
39396ff6d951SJohn Birrell 
39406ff6d951SJohn Birrell 		ltype = ctf_type_resolve(lp->dn_ctfp, lp->dn_type);
39416ff6d951SJohn Birrell 		lkind = ctf_type_kind(lp->dn_ctfp, ltype);
39426ff6d951SJohn Birrell 
39436ff6d951SJohn Birrell 		rtype = ctf_type_resolve(rp->dn_ctfp, rp->dn_type);
39446ff6d951SJohn Birrell 		rkind = ctf_type_kind(rp->dn_ctfp, rtype);
39456ff6d951SJohn Birrell 
39466ff6d951SJohn Birrell 		/*
39476ff6d951SJohn Birrell 		 * The rules for casting are loosely explained in K&R[A7.5]
39486ff6d951SJohn Birrell 		 * and K&R[A6].  Basically, we can cast to the same type or
39496ff6d951SJohn Birrell 		 * same base type, between any kind of scalar values, from
39506ff6d951SJohn Birrell 		 * arrays to pointers, and we can cast anything to void.
39516ff6d951SJohn Birrell 		 * To these rules D adds casts from scalars to strings.
39526ff6d951SJohn Birrell 		 */
39536ff6d951SJohn Birrell 		if (ctf_type_compat(lp->dn_ctfp, lp->dn_type,
39546ff6d951SJohn Birrell 		    rp->dn_ctfp, rp->dn_type))
39556ff6d951SJohn Birrell 			/*EMPTY*/;
39566ff6d951SJohn Birrell 		else if (dt_node_is_scalar(lp) &&
39576ff6d951SJohn Birrell 		    (dt_node_is_scalar(rp) || rkind == CTF_K_FUNCTION))
39586ff6d951SJohn Birrell 			/*EMPTY*/;
39596ff6d951SJohn Birrell 		else if (dt_node_is_void(lp))
39606ff6d951SJohn Birrell 			/*EMPTY*/;
39616ff6d951SJohn Birrell 		else if (lkind == CTF_K_POINTER && dt_node_is_pointer(rp))
39626ff6d951SJohn Birrell 			/*EMPTY*/;
39636ff6d951SJohn Birrell 		else if (dt_node_is_string(lp) && (dt_node_is_scalar(rp) ||
39646ff6d951SJohn Birrell 		    dt_node_is_pointer(rp) || dt_node_is_strcompat(rp)))
39656ff6d951SJohn Birrell 			/*EMPTY*/;
39666ff6d951SJohn Birrell 		else {
39676ff6d951SJohn Birrell 			xyerror(D_CAST_INVAL,
39686ff6d951SJohn Birrell 			    "invalid cast expression: \"%s\" to \"%s\"\n",
39696ff6d951SJohn Birrell 			    dt_node_type_name(rp, n1, sizeof (n1)),
39706ff6d951SJohn Birrell 			    dt_node_type_name(lp, n2, sizeof (n2)));
39716ff6d951SJohn Birrell 		}
39726ff6d951SJohn Birrell 
39736ff6d951SJohn Birrell 		dt_node_type_propagate(lp, dnp); /* see K&R[A7.5] */
39746ff6d951SJohn Birrell 		dt_node_attr_assign(dnp, dt_attr_min(lp->dn_attr, rp->dn_attr));
3975a98ff317SPedro F. Giffuni 
3976a98ff317SPedro F. Giffuni 		/*
3977a98ff317SPedro F. Giffuni 		 * If it's a pointer then should be able to (attempt to)
3978a98ff317SPedro F. Giffuni 		 * assign to it.
3979a98ff317SPedro F. Giffuni 		 */
3980a98ff317SPedro F. Giffuni 		if (lkind == CTF_K_POINTER)
3981a98ff317SPedro F. Giffuni 			dnp->dn_flags |= DT_NF_WRITABLE;
3982a98ff317SPedro F. Giffuni 
39836ff6d951SJohn Birrell 		break;
39846ff6d951SJohn Birrell 	}
39856ff6d951SJohn Birrell 
39866ff6d951SJohn Birrell 	case DT_TOK_COMMA:
39876ff6d951SJohn Birrell 		lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF);
39886ff6d951SJohn Birrell 		rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
39896ff6d951SJohn Birrell 
39906ff6d951SJohn Birrell 		if (dt_node_is_dynamic(lp) || dt_node_is_dynamic(rp)) {
39916ff6d951SJohn Birrell 			xyerror(D_OP_DYN, "operator %s operands "
39926ff6d951SJohn Birrell 			    "cannot be of dynamic type\n", opstr(op));
39936ff6d951SJohn Birrell 		}
39946ff6d951SJohn Birrell 
39956ff6d951SJohn Birrell 		if (dt_node_is_actfunc(lp) || dt_node_is_actfunc(rp)) {
39966ff6d951SJohn Birrell 			xyerror(D_OP_ACT, "operator %s operands "
39976ff6d951SJohn Birrell 			    "cannot be actions\n", opstr(op));
39986ff6d951SJohn Birrell 		}
39996ff6d951SJohn Birrell 
40006ff6d951SJohn Birrell 		dt_node_type_propagate(rp, dnp); /* see K&R[A7.18] */
40016ff6d951SJohn Birrell 		dt_node_attr_assign(dnp, dt_attr_min(lp->dn_attr, rp->dn_attr));
40026ff6d951SJohn Birrell 		break;
40036ff6d951SJohn Birrell 
40046ff6d951SJohn Birrell 	default:
40056ff6d951SJohn Birrell 		xyerror(D_UNKNOWN, "invalid binary op %s\n", opstr(op));
40066ff6d951SJohn Birrell 	}
40076ff6d951SJohn Birrell 
40086ff6d951SJohn Birrell 	/*
40096ff6d951SJohn Birrell 	 * Complete the conversion of E1[E2] to *((E1)+(E2)) that we started
40106ff6d951SJohn Birrell 	 * at the top of our switch() above (see K&R[A7.3.1]).  Since E2 is
40116ff6d951SJohn Birrell 	 * parsed as an argument_expression_list by dt_grammar.y, we can
40126ff6d951SJohn Birrell 	 * end up with a comma-separated list inside of a non-associative
40136ff6d951SJohn Birrell 	 * array reference.  We check for this and report an appropriate error.
40146ff6d951SJohn Birrell 	 */
40156ff6d951SJohn Birrell 	if (dnp->dn_op == DT_TOK_LBRAC && op == DT_TOK_ADD) {
40166ff6d951SJohn Birrell 		dt_node_t *pnp;
40176ff6d951SJohn Birrell 
40186ff6d951SJohn Birrell 		if (rp->dn_list != NULL) {
40196ff6d951SJohn Birrell 			xyerror(D_ARR_BADREF,
40206ff6d951SJohn Birrell 			    "cannot access %s as an associative array\n",
40216ff6d951SJohn Birrell 			    dt_node_name(lp, n1, sizeof (n1)));
40226ff6d951SJohn Birrell 		}
40236ff6d951SJohn Birrell 
40246ff6d951SJohn Birrell 		dnp->dn_op = DT_TOK_ADD;
40256ff6d951SJohn Birrell 		pnp = dt_node_op1(DT_TOK_DEREF, dnp);
40266ff6d951SJohn Birrell 
40276ff6d951SJohn Birrell 		/*
40286ff6d951SJohn Birrell 		 * Cook callbacks are not typically permitted to allocate nodes.
40296ff6d951SJohn Birrell 		 * When we do, we must insert them in the middle of an existing
40306ff6d951SJohn Birrell 		 * allocation list rather than having them appended to the pcb
40316ff6d951SJohn Birrell 		 * list because the sub-expression may be part of a definition.
40326ff6d951SJohn Birrell 		 */
40336ff6d951SJohn Birrell 		assert(yypcb->pcb_list == pnp);
40346ff6d951SJohn Birrell 		yypcb->pcb_list = pnp->dn_link;
40356ff6d951SJohn Birrell 
40366ff6d951SJohn Birrell 		pnp->dn_link = dnp->dn_link;
40376ff6d951SJohn Birrell 		dnp->dn_link = pnp;
40386ff6d951SJohn Birrell 
40396ff6d951SJohn Birrell 		return (dt_node_cook(pnp, DT_IDFLG_REF));
40406ff6d951SJohn Birrell 	}
40416ff6d951SJohn Birrell 
40426ff6d951SJohn Birrell 	return (dnp);
40436ff6d951SJohn Birrell }
40446ff6d951SJohn Birrell 
40456ff6d951SJohn Birrell /*ARGSUSED*/
40466ff6d951SJohn Birrell static dt_node_t *
40476ff6d951SJohn Birrell dt_cook_op3(dt_node_t *dnp, uint_t idflags)
40486ff6d951SJohn Birrell {
40496ff6d951SJohn Birrell 	dt_node_t *lp, *rp;
40506ff6d951SJohn Birrell 	ctf_file_t *ctfp;
40516ff6d951SJohn Birrell 	ctf_id_t type;
40526ff6d951SJohn Birrell 
40536ff6d951SJohn Birrell 	dnp->dn_expr = dt_node_cook(dnp->dn_expr, DT_IDFLG_REF);
40546ff6d951SJohn Birrell 	lp = dnp->dn_left = dt_node_cook(dnp->dn_left, DT_IDFLG_REF);
40556ff6d951SJohn Birrell 	rp = dnp->dn_right = dt_node_cook(dnp->dn_right, DT_IDFLG_REF);
40566ff6d951SJohn Birrell 
40576ff6d951SJohn Birrell 	if (!dt_node_is_scalar(dnp->dn_expr)) {
40586ff6d951SJohn Birrell 		xyerror(D_OP_SCALAR,
40596ff6d951SJohn Birrell 		    "operator ?: expression must be of scalar type\n");
40606ff6d951SJohn Birrell 	}
40616ff6d951SJohn Birrell 
40626ff6d951SJohn Birrell 	if (dt_node_is_dynamic(lp) || dt_node_is_dynamic(rp)) {
40636ff6d951SJohn Birrell 		xyerror(D_OP_DYN,
40646ff6d951SJohn Birrell 		    "operator ?: operands cannot be of dynamic type\n");
40656ff6d951SJohn Birrell 	}
40666ff6d951SJohn Birrell 
40676ff6d951SJohn Birrell 	/*
40686ff6d951SJohn Birrell 	 * The rules for type checking for the ternary operator are complex and
40696ff6d951SJohn Birrell 	 * are described in the ANSI-C spec (see K&R[A7.16]).  We implement
40706ff6d951SJohn Birrell 	 * the various tests in order from least to most expensive.
40716ff6d951SJohn Birrell 	 */
40726ff6d951SJohn Birrell 	if (ctf_type_compat(lp->dn_ctfp, lp->dn_type,
40736ff6d951SJohn Birrell 	    rp->dn_ctfp, rp->dn_type)) {
40746ff6d951SJohn Birrell 		ctfp = lp->dn_ctfp;
40756ff6d951SJohn Birrell 		type = lp->dn_type;
40766ff6d951SJohn Birrell 	} else if (dt_node_is_integer(lp) && dt_node_is_integer(rp)) {
40776ff6d951SJohn Birrell 		dt_type_promote(lp, rp, &ctfp, &type);
40786ff6d951SJohn Birrell 	} else if (dt_node_is_strcompat(lp) && dt_node_is_strcompat(rp) &&
40796ff6d951SJohn Birrell 	    (dt_node_is_string(lp) || dt_node_is_string(rp))) {
40806ff6d951SJohn Birrell 		ctfp = DT_STR_CTFP(yypcb->pcb_hdl);
40816ff6d951SJohn Birrell 		type = DT_STR_TYPE(yypcb->pcb_hdl);
40826ff6d951SJohn Birrell 	} else if (dt_node_is_ptrcompat(lp, rp, &ctfp, &type) == 0) {
40836ff6d951SJohn Birrell 		xyerror(D_OP_INCOMPAT,
40846ff6d951SJohn Birrell 		    "operator ?: operands must have compatible types\n");
40856ff6d951SJohn Birrell 	}
40866ff6d951SJohn Birrell 
40876ff6d951SJohn Birrell 	if (dt_node_is_actfunc(lp) || dt_node_is_actfunc(rp)) {
40886ff6d951SJohn Birrell 		xyerror(D_OP_ACT, "action cannot be "
40896ff6d951SJohn Birrell 		    "used in a conditional context\n");
40906ff6d951SJohn Birrell 	}
40916ff6d951SJohn Birrell 
40928e648814SRui Paulo 	dt_node_type_assign(dnp, ctfp, type, B_FALSE);
40936ff6d951SJohn Birrell 	dt_node_attr_assign(dnp, dt_attr_min(dnp->dn_expr->dn_attr,
40946ff6d951SJohn Birrell 	    dt_attr_min(lp->dn_attr, rp->dn_attr)));
40956ff6d951SJohn Birrell 
40966ff6d951SJohn Birrell 	return (dnp);
40976ff6d951SJohn Birrell }
40986ff6d951SJohn Birrell 
40996ff6d951SJohn Birrell static dt_node_t *
41006ff6d951SJohn Birrell dt_cook_statement(dt_node_t *dnp, uint_t idflags)
41016ff6d951SJohn Birrell {
41026ff6d951SJohn Birrell 	dnp->dn_expr = dt_node_cook(dnp->dn_expr, idflags);
41036ff6d951SJohn Birrell 	dt_node_attr_assign(dnp, dnp->dn_expr->dn_attr);
41046ff6d951SJohn Birrell 
41056ff6d951SJohn Birrell 	return (dnp);
41066ff6d951SJohn Birrell }
41076ff6d951SJohn Birrell 
41086ff6d951SJohn Birrell /*
41096ff6d951SJohn Birrell  * If dn_aggfun is set, this node is a collapsed aggregation assignment (see
41106ff6d951SJohn Birrell  * the special case code for DT_TOK_ASGN in dt_cook_op2() above), in which
41116ff6d951SJohn Birrell  * case we cook both the tuple and the function call.  If dn_aggfun is NULL,
41126ff6d951SJohn Birrell  * this node is just a reference to the aggregation's type and attributes.
41136ff6d951SJohn Birrell  */
41146ff6d951SJohn Birrell /*ARGSUSED*/
41156ff6d951SJohn Birrell static dt_node_t *
41166ff6d951SJohn Birrell dt_cook_aggregation(dt_node_t *dnp, uint_t idflags)
41176ff6d951SJohn Birrell {
41186ff6d951SJohn Birrell 	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
41196ff6d951SJohn Birrell 
41206ff6d951SJohn Birrell 	if (dnp->dn_aggfun != NULL) {
41216ff6d951SJohn Birrell 		dnp->dn_aggfun = dt_node_cook(dnp->dn_aggfun, DT_IDFLG_REF);
41226ff6d951SJohn Birrell 		dt_node_attr_assign(dnp, dt_ident_cook(dnp,
41236ff6d951SJohn Birrell 		    dnp->dn_ident, &dnp->dn_aggtup));
41246ff6d951SJohn Birrell 	} else {
41258e648814SRui Paulo 		dt_node_type_assign(dnp, DT_DYN_CTFP(dtp), DT_DYN_TYPE(dtp),
41268e648814SRui Paulo 		    B_FALSE);
41276ff6d951SJohn Birrell 		dt_node_attr_assign(dnp, dnp->dn_ident->di_attr);
41286ff6d951SJohn Birrell 	}
41296ff6d951SJohn Birrell 
41306ff6d951SJohn Birrell 	return (dnp);
41316ff6d951SJohn Birrell }
41326ff6d951SJohn Birrell 
41336ff6d951SJohn Birrell /*
41346ff6d951SJohn Birrell  * Since D permits new variable identifiers to be instantiated in any program
41356ff6d951SJohn Birrell  * expression, we may need to cook a clause's predicate either before or after
41366ff6d951SJohn Birrell  * the action list depending on the program code in question.  Consider:
41376ff6d951SJohn Birrell  *
41386ff6d951SJohn Birrell  * probe-description-list	probe-description-list
41396ff6d951SJohn Birrell  * /x++/			/x == 0/
41406ff6d951SJohn Birrell  * {				{
41416ff6d951SJohn Birrell  *     trace(x);		    trace(x++);
41426ff6d951SJohn Birrell  * }				}
41436ff6d951SJohn Birrell  *
41446ff6d951SJohn Birrell  * In the left-hand example, the predicate uses operator ++ to instantiate 'x'
41456ff6d951SJohn Birrell  * as a variable of type int64_t.  The predicate must be cooked first because
41466ff6d951SJohn Birrell  * otherwise the statement trace(x) refers to an unknown identifier.  In the
41476ff6d951SJohn Birrell  * right-hand example, the action list uses ++ to instantiate 'x'; the action
41486ff6d951SJohn Birrell  * list must be cooked first because otherwise the predicate x == 0 refers to
41496ff6d951SJohn Birrell  * an unknown identifier.  In order to simplify programming, we support both.
41506ff6d951SJohn Birrell  *
41516ff6d951SJohn Birrell  * When cooking a clause, we cook the action statements before the predicate by
41526ff6d951SJohn Birrell  * default, since it seems more common to create or modify identifiers in the
41536ff6d951SJohn Birrell  * action list.  If cooking fails due to an unknown identifier, we attempt to
41546ff6d951SJohn Birrell  * cook the predicate (i.e. do it first) and then go back and cook the actions.
41556ff6d951SJohn Birrell  * If this, too, fails (or if we get an error other than D_IDENT_UNDEF) we give
41566ff6d951SJohn Birrell  * up and report failure back to the user.  There are five possible paths:
41576ff6d951SJohn Birrell  *
41586ff6d951SJohn Birrell  * cook actions = OK, cook predicate = OK -> OK
41596ff6d951SJohn Birrell  * cook actions = OK, cook predicate = ERR -> ERR
41606ff6d951SJohn Birrell  * cook actions = ERR, cook predicate = ERR -> ERR
41616ff6d951SJohn Birrell  * cook actions = ERR, cook predicate = OK, cook actions = OK -> OK
41626ff6d951SJohn Birrell  * cook actions = ERR, cook predicate = OK, cook actions = ERR -> ERR
41636ff6d951SJohn Birrell  *
41646ff6d951SJohn Birrell  * The programmer can still defeat our scheme by creating circular definition
41656ff6d951SJohn Birrell  * dependencies between predicates and actions, as in this example clause:
41666ff6d951SJohn Birrell  *
41676ff6d951SJohn Birrell  * probe-description-list
41686ff6d951SJohn Birrell  * /x++ && y == 0/
41696ff6d951SJohn Birrell  * {
41706ff6d951SJohn Birrell  * 	trace(x + y++);
41716ff6d951SJohn Birrell  * }
41726ff6d951SJohn Birrell  *
41736ff6d951SJohn Birrell  * but it doesn't seem worth the complexity to handle such rare cases.  The
41746ff6d951SJohn Birrell  * user can simply use the D variable declaration syntax to work around them.
41756ff6d951SJohn Birrell  */
41766ff6d951SJohn Birrell static dt_node_t *
41776ff6d951SJohn Birrell dt_cook_clause(dt_node_t *dnp, uint_t idflags)
41786ff6d951SJohn Birrell {
41796ff6d951SJohn Birrell 	volatile int err, tries;
41806ff6d951SJohn Birrell 	jmp_buf ojb;
41816ff6d951SJohn Birrell 
41826ff6d951SJohn Birrell 	/*
41836ff6d951SJohn Birrell 	 * Before assigning dn_ctxattr, temporarily assign the probe attribute
41846ff6d951SJohn Birrell 	 * to 'dnp' itself to force an attribute check and minimum violation.
41856ff6d951SJohn Birrell 	 */
41866ff6d951SJohn Birrell 	dt_node_attr_assign(dnp, yypcb->pcb_pinfo.dtp_attr);
41876ff6d951SJohn Birrell 	dnp->dn_ctxattr = yypcb->pcb_pinfo.dtp_attr;
41886ff6d951SJohn Birrell 
41896ff6d951SJohn Birrell 	bcopy(yypcb->pcb_jmpbuf, ojb, sizeof (jmp_buf));
41906ff6d951SJohn Birrell 	tries = 0;
41916ff6d951SJohn Birrell 
41926ff6d951SJohn Birrell 	if (dnp->dn_pred != NULL && (err = setjmp(yypcb->pcb_jmpbuf)) != 0) {
41936ff6d951SJohn Birrell 		bcopy(ojb, yypcb->pcb_jmpbuf, sizeof (jmp_buf));
41946ff6d951SJohn Birrell 		if (tries++ != 0 || err != EDT_COMPILER || (
41956ff6d951SJohn Birrell 		    yypcb->pcb_hdl->dt_errtag != dt_errtag(D_IDENT_UNDEF) &&
41966ff6d951SJohn Birrell 		    yypcb->pcb_hdl->dt_errtag != dt_errtag(D_VAR_UNDEF)))
41976ff6d951SJohn Birrell 			longjmp(yypcb->pcb_jmpbuf, err);
41986ff6d951SJohn Birrell 	}
41996ff6d951SJohn Birrell 
42006ff6d951SJohn Birrell 	if (tries == 0) {
42016ff6d951SJohn Birrell 		yylabel("action list");
42026ff6d951SJohn Birrell 
42036ff6d951SJohn Birrell 		dt_node_attr_assign(dnp,
42046ff6d951SJohn Birrell 		    dt_node_list_cook(&dnp->dn_acts, idflags));
42056ff6d951SJohn Birrell 
42066ff6d951SJohn Birrell 		bcopy(ojb, yypcb->pcb_jmpbuf, sizeof (jmp_buf));
42076ff6d951SJohn Birrell 		yylabel(NULL);
42086ff6d951SJohn Birrell 	}
42096ff6d951SJohn Birrell 
42106ff6d951SJohn Birrell 	if (dnp->dn_pred != NULL) {
42116ff6d951SJohn Birrell 		yylabel("predicate");
42126ff6d951SJohn Birrell 
42136ff6d951SJohn Birrell 		dnp->dn_pred = dt_node_cook(dnp->dn_pred, idflags);
42146ff6d951SJohn Birrell 		dt_node_attr_assign(dnp,
42156ff6d951SJohn Birrell 		    dt_attr_min(dnp->dn_attr, dnp->dn_pred->dn_attr));
42166ff6d951SJohn Birrell 
42176ff6d951SJohn Birrell 		if (!dt_node_is_scalar(dnp->dn_pred)) {
42186ff6d951SJohn Birrell 			xyerror(D_PRED_SCALAR,
42196ff6d951SJohn Birrell 			    "predicate result must be of scalar type\n");
42206ff6d951SJohn Birrell 		}
42216ff6d951SJohn Birrell 
42226ff6d951SJohn Birrell 		yylabel(NULL);
42236ff6d951SJohn Birrell 	}
42246ff6d951SJohn Birrell 
42256ff6d951SJohn Birrell 	if (tries != 0) {
42266ff6d951SJohn Birrell 		yylabel("action list");
42276ff6d951SJohn Birrell 
42286ff6d951SJohn Birrell 		dt_node_attr_assign(dnp,
42296ff6d951SJohn Birrell 		    dt_node_list_cook(&dnp->dn_acts, idflags));
42306ff6d951SJohn Birrell 
42316ff6d951SJohn Birrell 		yylabel(NULL);
42326ff6d951SJohn Birrell 	}
42336ff6d951SJohn Birrell 
42346ff6d951SJohn Birrell 	return (dnp);
42356ff6d951SJohn Birrell }
42366ff6d951SJohn Birrell 
42376ff6d951SJohn Birrell /*ARGSUSED*/
42386ff6d951SJohn Birrell static dt_node_t *
42396ff6d951SJohn Birrell dt_cook_inline(dt_node_t *dnp, uint_t idflags)
42406ff6d951SJohn Birrell {
42416ff6d951SJohn Birrell 	dt_idnode_t *inp = dnp->dn_ident->di_iarg;
42426ff6d951SJohn Birrell 	dt_ident_t *rdp;
42436ff6d951SJohn Birrell 
42446ff6d951SJohn Birrell 	char n1[DT_TYPE_NAMELEN];
42456ff6d951SJohn Birrell 	char n2[DT_TYPE_NAMELEN];
42466ff6d951SJohn Birrell 
42476ff6d951SJohn Birrell 	assert(dnp->dn_ident->di_flags & DT_IDFLG_INLINE);
42486ff6d951SJohn Birrell 	assert(inp->din_root->dn_flags & DT_NF_COOKED);
42496ff6d951SJohn Birrell 
42506ff6d951SJohn Birrell 	/*
42516ff6d951SJohn Birrell 	 * If we are inlining a translation, verify that the inline declaration
42526ff6d951SJohn Birrell 	 * type exactly matches the type that is returned by the translation.
42536ff6d951SJohn Birrell 	 * Otherwise just use dt_node_is_argcompat() to check the types.
42546ff6d951SJohn Birrell 	 */
42556ff6d951SJohn Birrell 	if ((rdp = dt_node_resolve(inp->din_root, DT_IDENT_XLSOU)) != NULL ||
42566ff6d951SJohn Birrell 	    (rdp = dt_node_resolve(inp->din_root, DT_IDENT_XLPTR)) != NULL) {
42576ff6d951SJohn Birrell 
42586ff6d951SJohn Birrell 		ctf_file_t *lctfp = dnp->dn_ctfp;
42596ff6d951SJohn Birrell 		ctf_id_t ltype = ctf_type_resolve(lctfp, dnp->dn_type);
42606ff6d951SJohn Birrell 
42616ff6d951SJohn Birrell 		dt_xlator_t *dxp = rdp->di_data;
42626ff6d951SJohn Birrell 		ctf_file_t *rctfp = dxp->dx_dst_ctfp;
42636ff6d951SJohn Birrell 		ctf_id_t rtype = dxp->dx_dst_base;
42646ff6d951SJohn Birrell 
42656ff6d951SJohn Birrell 		if (ctf_type_kind(lctfp, ltype) == CTF_K_POINTER) {
42666ff6d951SJohn Birrell 			ltype = ctf_type_reference(lctfp, ltype);
42676ff6d951SJohn Birrell 			ltype = ctf_type_resolve(lctfp, ltype);
42686ff6d951SJohn Birrell 		}
42696ff6d951SJohn Birrell 
42706ff6d951SJohn Birrell 		if (ctf_type_compat(lctfp, ltype, rctfp, rtype) == 0) {
42716ff6d951SJohn Birrell 			dnerror(dnp, D_OP_INCOMPAT,
42726ff6d951SJohn Birrell 			    "inline %s definition uses incompatible types: "
42736ff6d951SJohn Birrell 			    "\"%s\" = \"%s\"\n", dnp->dn_ident->di_name,
42746ff6d951SJohn Birrell 			    dt_type_name(lctfp, ltype, n1, sizeof (n1)),
42756ff6d951SJohn Birrell 			    dt_type_name(rctfp, rtype, n2, sizeof (n2)));
42766ff6d951SJohn Birrell 		}
42776ff6d951SJohn Birrell 
42786ff6d951SJohn Birrell 	} else if (dt_node_is_argcompat(dnp, inp->din_root) == 0) {
42796ff6d951SJohn Birrell 		dnerror(dnp, D_OP_INCOMPAT,
42806ff6d951SJohn Birrell 		    "inline %s definition uses incompatible types: "
42816ff6d951SJohn Birrell 		    "\"%s\" = \"%s\"\n", dnp->dn_ident->di_name,
42826ff6d951SJohn Birrell 		    dt_node_type_name(dnp, n1, sizeof (n1)),
42836ff6d951SJohn Birrell 		    dt_node_type_name(inp->din_root, n2, sizeof (n2)));
42846ff6d951SJohn Birrell 	}
42856ff6d951SJohn Birrell 
42866ff6d951SJohn Birrell 	return (dnp);
42876ff6d951SJohn Birrell }
42886ff6d951SJohn Birrell 
42896ff6d951SJohn Birrell static dt_node_t *
42906ff6d951SJohn Birrell dt_cook_member(dt_node_t *dnp, uint_t idflags)
42916ff6d951SJohn Birrell {
42926ff6d951SJohn Birrell 	dnp->dn_membexpr = dt_node_cook(dnp->dn_membexpr, idflags);
42936ff6d951SJohn Birrell 	dt_node_attr_assign(dnp, dnp->dn_membexpr->dn_attr);
42946ff6d951SJohn Birrell 	return (dnp);
42956ff6d951SJohn Birrell }
42966ff6d951SJohn Birrell 
42976ff6d951SJohn Birrell /*ARGSUSED*/
42986ff6d951SJohn Birrell static dt_node_t *
42996ff6d951SJohn Birrell dt_cook_xlator(dt_node_t *dnp, uint_t idflags)
43006ff6d951SJohn Birrell {
43016ff6d951SJohn Birrell 	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
43026ff6d951SJohn Birrell 	dt_xlator_t *dxp = dnp->dn_xlator;
43036ff6d951SJohn Birrell 	dt_node_t *mnp;
43046ff6d951SJohn Birrell 
43056ff6d951SJohn Birrell 	char n1[DT_TYPE_NAMELEN];
43066ff6d951SJohn Birrell 	char n2[DT_TYPE_NAMELEN];
43076ff6d951SJohn Birrell 
43086ff6d951SJohn Birrell 	dtrace_attribute_t attr = _dtrace_maxattr;
43096ff6d951SJohn Birrell 	ctf_membinfo_t ctm;
43106ff6d951SJohn Birrell 
43116ff6d951SJohn Birrell 	/*
43126ff6d951SJohn Birrell 	 * Before cooking each translator member, we push a reference to the
43136ff6d951SJohn Birrell 	 * hash containing translator-local identifiers on to pcb_globals to
43146ff6d951SJohn Birrell 	 * temporarily interpose these identifiers in front of other globals.
43156ff6d951SJohn Birrell 	 */
43166ff6d951SJohn Birrell 	dt_idstack_push(&yypcb->pcb_globals, dxp->dx_locals);
43176ff6d951SJohn Birrell 
43186ff6d951SJohn Birrell 	for (mnp = dnp->dn_members; mnp != NULL; mnp = mnp->dn_list) {
43196ff6d951SJohn Birrell 		if (ctf_member_info(dxp->dx_dst_ctfp, dxp->dx_dst_type,
43206ff6d951SJohn Birrell 		    mnp->dn_membname, &ctm) == CTF_ERR) {
43216ff6d951SJohn Birrell 			xyerror(D_XLATE_MEMB,
43226ff6d951SJohn Birrell 			    "translator member %s is not a member of %s\n",
43236ff6d951SJohn Birrell 			    mnp->dn_membname, ctf_type_name(dxp->dx_dst_ctfp,
43246ff6d951SJohn Birrell 			    dxp->dx_dst_type, n1, sizeof (n1)));
43256ff6d951SJohn Birrell 		}
43266ff6d951SJohn Birrell 
43276ff6d951SJohn Birrell 		(void) dt_node_cook(mnp, DT_IDFLG_REF);
43288e648814SRui Paulo 		dt_node_type_assign(mnp, dxp->dx_dst_ctfp, ctm.ctm_type,
43298e648814SRui Paulo 		    B_FALSE);
43306ff6d951SJohn Birrell 		attr = dt_attr_min(attr, mnp->dn_attr);
43316ff6d951SJohn Birrell 
43326ff6d951SJohn Birrell 		if (dt_node_is_argcompat(mnp, mnp->dn_membexpr) == 0) {
43336ff6d951SJohn Birrell 			xyerror(D_XLATE_INCOMPAT,
43346ff6d951SJohn Birrell 			    "translator member %s definition uses "
43356ff6d951SJohn Birrell 			    "incompatible types: \"%s\" = \"%s\"\n",
43366ff6d951SJohn Birrell 			    mnp->dn_membname,
43376ff6d951SJohn Birrell 			    dt_node_type_name(mnp, n1, sizeof (n1)),
43386ff6d951SJohn Birrell 			    dt_node_type_name(mnp->dn_membexpr,
43396ff6d951SJohn Birrell 			    n2, sizeof (n2)));
43406ff6d951SJohn Birrell 		}
43416ff6d951SJohn Birrell 	}
43426ff6d951SJohn Birrell 
43436ff6d951SJohn Birrell 	dt_idstack_pop(&yypcb->pcb_globals, dxp->dx_locals);
43446ff6d951SJohn Birrell 
43456ff6d951SJohn Birrell 	dxp->dx_souid.di_attr = attr;
43466ff6d951SJohn Birrell 	dxp->dx_ptrid.di_attr = attr;
43476ff6d951SJohn Birrell 
43488e648814SRui Paulo 	dt_node_type_assign(dnp, DT_DYN_CTFP(dtp), DT_DYN_TYPE(dtp), B_FALSE);
43496ff6d951SJohn Birrell 	dt_node_attr_assign(dnp, _dtrace_defattr);
43506ff6d951SJohn Birrell 
43516ff6d951SJohn Birrell 	return (dnp);
43526ff6d951SJohn Birrell }
43536ff6d951SJohn Birrell 
43546ff6d951SJohn Birrell static void
43556ff6d951SJohn Birrell dt_node_provider_cmp_argv(dt_provider_t *pvp, dt_node_t *pnp, const char *kind,
43566ff6d951SJohn Birrell     uint_t old_argc, dt_node_t *old_argv, uint_t new_argc, dt_node_t *new_argv)
43576ff6d951SJohn Birrell {
43586ff6d951SJohn Birrell 	dt_probe_t *prp = pnp->dn_ident->di_data;
43596ff6d951SJohn Birrell 	uint_t i;
43606ff6d951SJohn Birrell 
43616ff6d951SJohn Birrell 	char n1[DT_TYPE_NAMELEN];
43626ff6d951SJohn Birrell 	char n2[DT_TYPE_NAMELEN];
43636ff6d951SJohn Birrell 
43646ff6d951SJohn Birrell 	if (old_argc != new_argc) {
43656ff6d951SJohn Birrell 		dnerror(pnp, D_PROV_INCOMPAT,
43666ff6d951SJohn Birrell 		    "probe %s:%s %s prototype mismatch:\n"
43676ff6d951SJohn Birrell 		    "\t current: %u arg%s\n\tprevious: %u arg%s\n",
43686ff6d951SJohn Birrell 		    pvp->pv_desc.dtvd_name, prp->pr_ident->di_name, kind,
43696ff6d951SJohn Birrell 		    new_argc, new_argc != 1 ? "s" : "",
43706ff6d951SJohn Birrell 		    old_argc, old_argc != 1 ? "s" : "");
43716ff6d951SJohn Birrell 	}
43726ff6d951SJohn Birrell 
43736ff6d951SJohn Birrell 	for (i = 0; i < old_argc; i++,
43746ff6d951SJohn Birrell 	    old_argv = old_argv->dn_list, new_argv = new_argv->dn_list) {
43756ff6d951SJohn Birrell 		if (ctf_type_cmp(old_argv->dn_ctfp, old_argv->dn_type,
43766ff6d951SJohn Birrell 		    new_argv->dn_ctfp, new_argv->dn_type) == 0)
43776ff6d951SJohn Birrell 			continue;
43786ff6d951SJohn Birrell 
43796ff6d951SJohn Birrell 		dnerror(pnp, D_PROV_INCOMPAT,
43806ff6d951SJohn Birrell 		    "probe %s:%s %s prototype argument #%u mismatch:\n"
43816ff6d951SJohn Birrell 		    "\t current: %s\n\tprevious: %s\n",
43826ff6d951SJohn Birrell 		    pvp->pv_desc.dtvd_name, prp->pr_ident->di_name, kind, i + 1,
43836ff6d951SJohn Birrell 		    dt_node_type_name(new_argv, n1, sizeof (n1)),
43846ff6d951SJohn Birrell 		    dt_node_type_name(old_argv, n2, sizeof (n2)));
43856ff6d951SJohn Birrell 	}
43866ff6d951SJohn Birrell }
43876ff6d951SJohn Birrell 
43886ff6d951SJohn Birrell /*
43896ff6d951SJohn Birrell  * Compare a new probe declaration with an existing probe definition (either
43906ff6d951SJohn Birrell  * from a previous declaration or cached from the kernel).  If the existing
43916ff6d951SJohn Birrell  * definition and declaration both have an input and output parameter list,
43926ff6d951SJohn Birrell  * compare both lists.  Otherwise compare only the output parameter lists.
43936ff6d951SJohn Birrell  */
43946ff6d951SJohn Birrell static void
43956ff6d951SJohn Birrell dt_node_provider_cmp(dt_provider_t *pvp, dt_node_t *pnp,
43966ff6d951SJohn Birrell     dt_probe_t *old, dt_probe_t *new)
43976ff6d951SJohn Birrell {
43986ff6d951SJohn Birrell 	dt_node_provider_cmp_argv(pvp, pnp, "output",
43996ff6d951SJohn Birrell 	    old->pr_xargc, old->pr_xargs, new->pr_xargc, new->pr_xargs);
44006ff6d951SJohn Birrell 
44016ff6d951SJohn Birrell 	if (old->pr_nargs != old->pr_xargs && new->pr_nargs != new->pr_xargs) {
44026ff6d951SJohn Birrell 		dt_node_provider_cmp_argv(pvp, pnp, "input",
44036ff6d951SJohn Birrell 		    old->pr_nargc, old->pr_nargs, new->pr_nargc, new->pr_nargs);
44046ff6d951SJohn Birrell 	}
44056ff6d951SJohn Birrell 
44066ff6d951SJohn Birrell 	if (old->pr_nargs == old->pr_xargs && new->pr_nargs != new->pr_xargs) {
44076ff6d951SJohn Birrell 		if (pvp->pv_flags & DT_PROVIDER_IMPL) {
44086ff6d951SJohn Birrell 			dnerror(pnp, D_PROV_INCOMPAT,
44096ff6d951SJohn Birrell 			    "provider interface mismatch: %s\n"
44106ff6d951SJohn Birrell 			    "\t current: probe %s:%s has an output prototype\n"
44116ff6d951SJohn Birrell 			    "\tprevious: probe %s:%s has no output prototype\n",
44126ff6d951SJohn Birrell 			    pvp->pv_desc.dtvd_name, pvp->pv_desc.dtvd_name,
44136ff6d951SJohn Birrell 			    new->pr_ident->di_name, pvp->pv_desc.dtvd_name,
44146ff6d951SJohn Birrell 			    old->pr_ident->di_name);
44156ff6d951SJohn Birrell 		}
44166ff6d951SJohn Birrell 
44176ff6d951SJohn Birrell 		if (old->pr_ident->di_gen == yypcb->pcb_hdl->dt_gen)
44186ff6d951SJohn Birrell 			old->pr_ident->di_flags |= DT_IDFLG_ORPHAN;
44196ff6d951SJohn Birrell 
44206ff6d951SJohn Birrell 		dt_idhash_delete(pvp->pv_probes, old->pr_ident);
44216ff6d951SJohn Birrell 		dt_probe_declare(pvp, new);
44226ff6d951SJohn Birrell 	}
44236ff6d951SJohn Birrell }
44246ff6d951SJohn Birrell 
44256ff6d951SJohn Birrell static void
44266ff6d951SJohn Birrell dt_cook_probe(dt_node_t *dnp, dt_provider_t *pvp)
44276ff6d951SJohn Birrell {
44286ff6d951SJohn Birrell 	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
44296ff6d951SJohn Birrell 	dt_probe_t *prp = dnp->dn_ident->di_data;
44306ff6d951SJohn Birrell 
44316ff6d951SJohn Birrell 	dt_xlator_t *dxp;
44326ff6d951SJohn Birrell 	uint_t i;
44336ff6d951SJohn Birrell 
44346ff6d951SJohn Birrell 	char n1[DT_TYPE_NAMELEN];
44356ff6d951SJohn Birrell 	char n2[DT_TYPE_NAMELEN];
44366ff6d951SJohn Birrell 
44376ff6d951SJohn Birrell 	if (prp->pr_nargs == prp->pr_xargs)
44386ff6d951SJohn Birrell 		return;
44396ff6d951SJohn Birrell 
44406ff6d951SJohn Birrell 	for (i = 0; i < prp->pr_xargc; i++) {
44416ff6d951SJohn Birrell 		dt_node_t *xnp = prp->pr_xargv[i];
44426ff6d951SJohn Birrell 		dt_node_t *nnp = prp->pr_nargv[prp->pr_mapping[i]];
44436ff6d951SJohn Birrell 
44446ff6d951SJohn Birrell 		if ((dxp = dt_xlator_lookup(dtp,
44456ff6d951SJohn Birrell 		    nnp, xnp, DT_XLATE_FUZZY)) != NULL) {
44466ff6d951SJohn Birrell 			if (dt_provider_xref(dtp, pvp, dxp->dx_id) != 0)
44476ff6d951SJohn Birrell 				longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
44486ff6d951SJohn Birrell 			continue;
44496ff6d951SJohn Birrell 		}
44506ff6d951SJohn Birrell 
44516ff6d951SJohn Birrell 		if (dt_node_is_argcompat(nnp, xnp))
44526ff6d951SJohn Birrell 			continue; /* no translator defined and none required */
44536ff6d951SJohn Birrell 
44546ff6d951SJohn Birrell 		dnerror(dnp, D_PROV_PRXLATOR, "translator for %s:%s output "
44556ff6d951SJohn Birrell 		    "argument #%u from %s to %s is not defined\n",
44566ff6d951SJohn Birrell 		    pvp->pv_desc.dtvd_name, dnp->dn_ident->di_name, i + 1,
44576ff6d951SJohn Birrell 		    dt_node_type_name(nnp, n1, sizeof (n1)),
44586ff6d951SJohn Birrell 		    dt_node_type_name(xnp, n2, sizeof (n2)));
44596ff6d951SJohn Birrell 	}
44606ff6d951SJohn Birrell }
44616ff6d951SJohn Birrell 
44626ff6d951SJohn Birrell /*ARGSUSED*/
44636ff6d951SJohn Birrell static dt_node_t *
44646ff6d951SJohn Birrell dt_cook_provider(dt_node_t *dnp, uint_t idflags)
44656ff6d951SJohn Birrell {
44666ff6d951SJohn Birrell 	dt_provider_t *pvp = dnp->dn_provider;
44676ff6d951SJohn Birrell 	dt_node_t *pnp;
44686ff6d951SJohn Birrell 
44696ff6d951SJohn Birrell 	/*
44706ff6d951SJohn Birrell 	 * If we're declaring a provider for the first time and it is unknown
44716ff6d951SJohn Birrell 	 * to dtrace(7D), insert the probe definitions into the provider's hash.
44726ff6d951SJohn Birrell 	 * If we're redeclaring a known provider, verify the interface matches.
44736ff6d951SJohn Birrell 	 */
44746ff6d951SJohn Birrell 	for (pnp = dnp->dn_probes; pnp != NULL; pnp = pnp->dn_list) {
44756ff6d951SJohn Birrell 		const char *probename = pnp->dn_ident->di_name;
44766ff6d951SJohn Birrell 		dt_probe_t *prp = dt_probe_lookup(pvp, probename);
44776ff6d951SJohn Birrell 
44786ff6d951SJohn Birrell 		assert(pnp->dn_kind == DT_NODE_PROBE);
44796ff6d951SJohn Birrell 
44806ff6d951SJohn Birrell 		if (prp != NULL && dnp->dn_provred) {
44816ff6d951SJohn Birrell 			dt_node_provider_cmp(pvp, pnp,
44826ff6d951SJohn Birrell 			    prp, pnp->dn_ident->di_data);
44836ff6d951SJohn Birrell 		} else if (prp == NULL && dnp->dn_provred) {
44846ff6d951SJohn Birrell 			dnerror(pnp, D_PROV_INCOMPAT,
44856ff6d951SJohn Birrell 			    "provider interface mismatch: %s\n"
44866ff6d951SJohn Birrell 			    "\t current: probe %s:%s defined\n"
44876ff6d951SJohn Birrell 			    "\tprevious: probe %s:%s not defined\n",
44886ff6d951SJohn Birrell 			    dnp->dn_provname, dnp->dn_provname,
44896ff6d951SJohn Birrell 			    probename, dnp->dn_provname, probename);
44906ff6d951SJohn Birrell 		} else if (prp != NULL) {
44916ff6d951SJohn Birrell 			dnerror(pnp, D_PROV_PRDUP, "probe redeclared: %s:%s\n",
44926ff6d951SJohn Birrell 			    dnp->dn_provname, probename);
44936ff6d951SJohn Birrell 		} else
44946ff6d951SJohn Birrell 			dt_probe_declare(pvp, pnp->dn_ident->di_data);
44956ff6d951SJohn Birrell 
44966ff6d951SJohn Birrell 		dt_cook_probe(pnp, pvp);
44976ff6d951SJohn Birrell 	}
44986ff6d951SJohn Birrell 
44996ff6d951SJohn Birrell 	return (dnp);
45006ff6d951SJohn Birrell }
45016ff6d951SJohn Birrell 
45026ff6d951SJohn Birrell /*ARGSUSED*/
45036ff6d951SJohn Birrell static dt_node_t *
45046ff6d951SJohn Birrell dt_cook_none(dt_node_t *dnp, uint_t idflags)
45056ff6d951SJohn Birrell {
45066ff6d951SJohn Birrell 	return (dnp);
45076ff6d951SJohn Birrell }
45086ff6d951SJohn Birrell 
45096ff6d951SJohn Birrell static dt_node_t *(*dt_cook_funcs[])(dt_node_t *, uint_t) = {
45106ff6d951SJohn Birrell 	dt_cook_none,		/* DT_NODE_FREE */
45116ff6d951SJohn Birrell 	dt_cook_none,		/* DT_NODE_INT */
45126ff6d951SJohn Birrell 	dt_cook_none,		/* DT_NODE_STRING */
45136ff6d951SJohn Birrell 	dt_cook_ident,		/* DT_NODE_IDENT */
45146ff6d951SJohn Birrell 	dt_cook_var,		/* DT_NODE_VAR */
45156ff6d951SJohn Birrell 	dt_cook_none,		/* DT_NODE_SYM */
45166ff6d951SJohn Birrell 	dt_cook_none,		/* DT_NODE_TYPE */
45176ff6d951SJohn Birrell 	dt_cook_func,		/* DT_NODE_FUNC */
45186ff6d951SJohn Birrell 	dt_cook_op1,		/* DT_NODE_OP1 */
45196ff6d951SJohn Birrell 	dt_cook_op2,		/* DT_NODE_OP2 */
45206ff6d951SJohn Birrell 	dt_cook_op3,		/* DT_NODE_OP3 */
45216ff6d951SJohn Birrell 	dt_cook_statement,	/* DT_NODE_DEXPR */
45226ff6d951SJohn Birrell 	dt_cook_statement,	/* DT_NODE_DFUNC */
45236ff6d951SJohn Birrell 	dt_cook_aggregation,	/* DT_NODE_AGG */
45246ff6d951SJohn Birrell 	dt_cook_none,		/* DT_NODE_PDESC */
45256ff6d951SJohn Birrell 	dt_cook_clause,		/* DT_NODE_CLAUSE */
45266ff6d951SJohn Birrell 	dt_cook_inline,		/* DT_NODE_INLINE */
45276ff6d951SJohn Birrell 	dt_cook_member,		/* DT_NODE_MEMBER */
45286ff6d951SJohn Birrell 	dt_cook_xlator,		/* DT_NODE_XLATOR */
45296ff6d951SJohn Birrell 	dt_cook_none,		/* DT_NODE_PROBE */
45306ff6d951SJohn Birrell 	dt_cook_provider,	/* DT_NODE_PROVIDER */
4531650f66acSMark Johnston 	dt_cook_none,		/* DT_NODE_PROG */
4532650f66acSMark Johnston 	dt_cook_none,		/* DT_NODE_IF */
45336ff6d951SJohn Birrell };
45346ff6d951SJohn Birrell 
45356ff6d951SJohn Birrell /*
45366ff6d951SJohn Birrell  * Recursively cook the parse tree starting at the specified node.  The idflags
45376ff6d951SJohn Birrell  * parameter is used to indicate the type of reference (r/w) and is applied to
45386ff6d951SJohn Birrell  * the resulting identifier if it is a D variable or D aggregation.
45396ff6d951SJohn Birrell  */
45406ff6d951SJohn Birrell dt_node_t *
45416ff6d951SJohn Birrell dt_node_cook(dt_node_t *dnp, uint_t idflags)
45426ff6d951SJohn Birrell {
45436ff6d951SJohn Birrell 	int oldlineno = yylineno;
45446ff6d951SJohn Birrell 
45456ff6d951SJohn Birrell 	yylineno = dnp->dn_line;
45466ff6d951SJohn Birrell 
4547650f66acSMark Johnston 	assert(dnp->dn_kind <
4548650f66acSMark Johnston 	    sizeof (dt_cook_funcs) / sizeof (dt_cook_funcs[0]));
45496ff6d951SJohn Birrell 	dnp = dt_cook_funcs[dnp->dn_kind](dnp, idflags);
45506ff6d951SJohn Birrell 	dnp->dn_flags |= DT_NF_COOKED;
45516ff6d951SJohn Birrell 
45526ff6d951SJohn Birrell 	if (dnp->dn_kind == DT_NODE_VAR || dnp->dn_kind == DT_NODE_AGG)
45536ff6d951SJohn Birrell 		dnp->dn_ident->di_flags |= idflags;
45546ff6d951SJohn Birrell 
45556ff6d951SJohn Birrell 	yylineno = oldlineno;
45566ff6d951SJohn Birrell 	return (dnp);
45576ff6d951SJohn Birrell }
45586ff6d951SJohn Birrell 
45596ff6d951SJohn Birrell dtrace_attribute_t
45606ff6d951SJohn Birrell dt_node_list_cook(dt_node_t **pnp, uint_t idflags)
45616ff6d951SJohn Birrell {
45626ff6d951SJohn Birrell 	dtrace_attribute_t attr = _dtrace_defattr;
45636ff6d951SJohn Birrell 	dt_node_t *dnp, *nnp;
45646ff6d951SJohn Birrell 
45656ff6d951SJohn Birrell 	for (dnp = (pnp != NULL ? *pnp : NULL); dnp != NULL; dnp = nnp) {
45666ff6d951SJohn Birrell 		nnp = dnp->dn_list;
45676ff6d951SJohn Birrell 		dnp = *pnp = dt_node_cook(dnp, idflags);
45686ff6d951SJohn Birrell 		attr = dt_attr_min(attr, dnp->dn_attr);
45696ff6d951SJohn Birrell 		dnp->dn_list = nnp;
45706ff6d951SJohn Birrell 		pnp = &dnp->dn_list;
45716ff6d951SJohn Birrell 	}
45726ff6d951SJohn Birrell 
45736ff6d951SJohn Birrell 	return (attr);
45746ff6d951SJohn Birrell }
45756ff6d951SJohn Birrell 
45766ff6d951SJohn Birrell void
45776ff6d951SJohn Birrell dt_node_list_free(dt_node_t **pnp)
45786ff6d951SJohn Birrell {
45796ff6d951SJohn Birrell 	dt_node_t *dnp, *nnp;
45806ff6d951SJohn Birrell 
45816ff6d951SJohn Birrell 	for (dnp = (pnp != NULL ? *pnp : NULL); dnp != NULL; dnp = nnp) {
45826ff6d951SJohn Birrell 		nnp = dnp->dn_list;
45836ff6d951SJohn Birrell 		dt_node_free(dnp);
45846ff6d951SJohn Birrell 	}
45856ff6d951SJohn Birrell 
45866ff6d951SJohn Birrell 	if (pnp != NULL)
45876ff6d951SJohn Birrell 		*pnp = NULL;
45886ff6d951SJohn Birrell }
45896ff6d951SJohn Birrell 
45906ff6d951SJohn Birrell void
45916ff6d951SJohn Birrell dt_node_link_free(dt_node_t **pnp)
45926ff6d951SJohn Birrell {
45936ff6d951SJohn Birrell 	dt_node_t *dnp, *nnp;
45946ff6d951SJohn Birrell 
45956ff6d951SJohn Birrell 	for (dnp = (pnp != NULL ? *pnp : NULL); dnp != NULL; dnp = nnp) {
45966ff6d951SJohn Birrell 		nnp = dnp->dn_link;
45976ff6d951SJohn Birrell 		dt_node_free(dnp);
45986ff6d951SJohn Birrell 	}
45996ff6d951SJohn Birrell 
46006ff6d951SJohn Birrell 	for (dnp = (pnp != NULL ? *pnp : NULL); dnp != NULL; dnp = nnp) {
46016ff6d951SJohn Birrell 		nnp = dnp->dn_link;
46026ff6d951SJohn Birrell 		free(dnp);
46036ff6d951SJohn Birrell 	}
46046ff6d951SJohn Birrell 
46056ff6d951SJohn Birrell 	if (pnp != NULL)
46066ff6d951SJohn Birrell 		*pnp = NULL;
46076ff6d951SJohn Birrell }
46086ff6d951SJohn Birrell 
46096ff6d951SJohn Birrell dt_node_t *
46106ff6d951SJohn Birrell dt_node_link(dt_node_t *lp, dt_node_t *rp)
46116ff6d951SJohn Birrell {
46126ff6d951SJohn Birrell 	dt_node_t *dnp;
46136ff6d951SJohn Birrell 
46146ff6d951SJohn Birrell 	if (lp == NULL)
46156ff6d951SJohn Birrell 		return (rp);
46166ff6d951SJohn Birrell 	else if (rp == NULL)
46176ff6d951SJohn Birrell 		return (lp);
46186ff6d951SJohn Birrell 
46196ff6d951SJohn Birrell 	for (dnp = lp; dnp->dn_list != NULL; dnp = dnp->dn_list)
46206ff6d951SJohn Birrell 		continue;
46216ff6d951SJohn Birrell 
46226ff6d951SJohn Birrell 	dnp->dn_list = rp;
46236ff6d951SJohn Birrell 	return (lp);
46246ff6d951SJohn Birrell }
46256ff6d951SJohn Birrell 
46266ff6d951SJohn Birrell /*
46276ff6d951SJohn Birrell  * Compute the DOF dtrace_diftype_t representation of a node's type.  This is
46286ff6d951SJohn Birrell  * called from a variety of places in the library so it cannot assume yypcb
46296ff6d951SJohn Birrell  * is valid: any references to handle-specific data must be made through 'dtp'.
46306ff6d951SJohn Birrell  */
46316ff6d951SJohn Birrell void
46326ff6d951SJohn Birrell dt_node_diftype(dtrace_hdl_t *dtp, const dt_node_t *dnp, dtrace_diftype_t *tp)
46336ff6d951SJohn Birrell {
46346ff6d951SJohn Birrell 	if (dnp->dn_ctfp == DT_STR_CTFP(dtp) &&
46356ff6d951SJohn Birrell 	    dnp->dn_type == DT_STR_TYPE(dtp)) {
46366ff6d951SJohn Birrell 		tp->dtdt_kind = DIF_TYPE_STRING;
46376ff6d951SJohn Birrell 		tp->dtdt_ckind = CTF_K_UNKNOWN;
46386ff6d951SJohn Birrell 	} else {
46396ff6d951SJohn Birrell 		tp->dtdt_kind = DIF_TYPE_CTF;
46406ff6d951SJohn Birrell 		tp->dtdt_ckind = ctf_type_kind(dnp->dn_ctfp,
46416ff6d951SJohn Birrell 		    ctf_type_resolve(dnp->dn_ctfp, dnp->dn_type));
46426ff6d951SJohn Birrell 	}
46436ff6d951SJohn Birrell 
46448e648814SRui Paulo 	tp->dtdt_flags = (dnp->dn_flags & DT_NF_REF) ?
46458e648814SRui Paulo 	    (dnp->dn_flags & DT_NF_USERLAND) ? DIF_TF_BYUREF :
46468e648814SRui Paulo 	    DIF_TF_BYREF : 0;
46476ff6d951SJohn Birrell 	tp->dtdt_pad = 0;
46486ff6d951SJohn Birrell 	tp->dtdt_size = ctf_type_size(dnp->dn_ctfp, dnp->dn_type);
46496ff6d951SJohn Birrell }
46506ff6d951SJohn Birrell 
4651650f66acSMark Johnston /*
4652650f66acSMark Johnston  * Output the parse tree as D.  The "-xtree=8" argument will call this
4653650f66acSMark Johnston  * function to print out the program after any syntactic sugar
4654650f66acSMark Johnston  * transformations have been applied (e.g. to implement "if").  The
4655650f66acSMark Johnston  * resulting output can be used to understand the transformations
4656650f66acSMark Johnston  * applied by these features, or to run such a script on a system that
4657650f66acSMark Johnston  * does not support these features
4658650f66acSMark Johnston  *
4659650f66acSMark Johnston  * Note that the output does not express precisely the same program as
4660650f66acSMark Johnston  * the input.  In particular:
4661650f66acSMark Johnston  *  - Only the clauses are output.  #pragma options, variable
4662650f66acSMark Johnston  *    declarations, etc. are excluded.
4663650f66acSMark Johnston  *  - Command argument substitution has already been done, so the output
4664650f66acSMark Johnston  *    will not contain e.g. $$1, but rather the substituted string.
4665650f66acSMark Johnston  */
4666650f66acSMark Johnston void
4667650f66acSMark Johnston dt_printd(dt_node_t *dnp, FILE *fp, int depth)
4668650f66acSMark Johnston {
4669650f66acSMark Johnston 	dt_node_t *arg;
4670650f66acSMark Johnston 
4671650f66acSMark Johnston 	switch (dnp->dn_kind) {
4672650f66acSMark Johnston 	case DT_NODE_INT:
4673650f66acSMark Johnston 		(void) fprintf(fp, "0x%llx", (u_longlong_t)dnp->dn_value);
4674650f66acSMark Johnston 		if (!(dnp->dn_flags & DT_NF_SIGNED))
4675650f66acSMark Johnston 			(void) fprintf(fp, "u");
4676650f66acSMark Johnston 		break;
4677650f66acSMark Johnston 
4678650f66acSMark Johnston 	case DT_NODE_STRING: {
4679650f66acSMark Johnston 		char *escd = strchr2esc(dnp->dn_string, strlen(dnp->dn_string));
4680650f66acSMark Johnston 		(void) fprintf(fp, "\"%s\"", escd);
4681650f66acSMark Johnston 		free(escd);
4682650f66acSMark Johnston 		break;
4683650f66acSMark Johnston 	}
4684650f66acSMark Johnston 
4685650f66acSMark Johnston 	case DT_NODE_IDENT:
4686650f66acSMark Johnston 		(void) fprintf(fp, "%s", dnp->dn_string);
4687650f66acSMark Johnston 		break;
4688650f66acSMark Johnston 
4689650f66acSMark Johnston 	case DT_NODE_VAR:
4690650f66acSMark Johnston 		(void) fprintf(fp, "%s%s",
4691650f66acSMark Johnston 		    (dnp->dn_ident->di_flags & DT_IDFLG_LOCAL) ? "this->" :
4692650f66acSMark Johnston 		    (dnp->dn_ident->di_flags & DT_IDFLG_TLS) ? "self->" : "",
4693650f66acSMark Johnston 		    dnp->dn_ident->di_name);
4694650f66acSMark Johnston 
4695650f66acSMark Johnston 		if (dnp->dn_args != NULL) {
4696650f66acSMark Johnston 			(void) fprintf(fp, "[");
4697650f66acSMark Johnston 
4698650f66acSMark Johnston 			for (arg = dnp->dn_args; arg != NULL;
4699650f66acSMark Johnston 			    arg = arg->dn_list) {
4700650f66acSMark Johnston 				dt_printd(arg, fp, 0);
4701650f66acSMark Johnston 				if (arg->dn_list != NULL)
4702650f66acSMark Johnston 					(void) fprintf(fp, ", ");
4703650f66acSMark Johnston 			}
4704650f66acSMark Johnston 
4705650f66acSMark Johnston 			(void) fprintf(fp, "]");
4706650f66acSMark Johnston 		}
4707650f66acSMark Johnston 		break;
4708650f66acSMark Johnston 
4709650f66acSMark Johnston 	case DT_NODE_SYM: {
4710650f66acSMark Johnston 		const dtrace_syminfo_t *dts = dnp->dn_ident->di_data;
4711650f66acSMark Johnston 		(void) fprintf(fp, "%s`%s", dts->dts_object, dts->dts_name);
4712650f66acSMark Johnston 		break;
4713650f66acSMark Johnston 	}
4714650f66acSMark Johnston 	case DT_NODE_FUNC:
4715650f66acSMark Johnston 		(void) fprintf(fp, "%s(", dnp->dn_ident->di_name);
4716650f66acSMark Johnston 
4717650f66acSMark Johnston 		for (arg = dnp->dn_args; arg != NULL; arg = arg->dn_list) {
4718650f66acSMark Johnston 			dt_printd(arg, fp, 0);
4719650f66acSMark Johnston 			if (arg->dn_list != NULL)
4720650f66acSMark Johnston 				(void) fprintf(fp, ", ");
4721650f66acSMark Johnston 		}
4722650f66acSMark Johnston 		(void) fprintf(fp, ")");
4723650f66acSMark Johnston 		break;
4724650f66acSMark Johnston 
4725650f66acSMark Johnston 	case DT_NODE_OP1:
4726650f66acSMark Johnston 		(void) fprintf(fp, "%s(", opstr(dnp->dn_op));
4727650f66acSMark Johnston 		dt_printd(dnp->dn_child, fp, 0);
4728650f66acSMark Johnston 		(void) fprintf(fp, ")");
4729650f66acSMark Johnston 		break;
4730650f66acSMark Johnston 
4731650f66acSMark Johnston 	case DT_NODE_OP2:
4732650f66acSMark Johnston 		(void) fprintf(fp, "(");
4733650f66acSMark Johnston 		dt_printd(dnp->dn_left, fp, 0);
4734650f66acSMark Johnston 		if (dnp->dn_op == DT_TOK_LPAR) {
4735650f66acSMark Johnston 			(void) fprintf(fp, ")");
4736650f66acSMark Johnston 			dt_printd(dnp->dn_right, fp, 0);
4737650f66acSMark Johnston 			break;
4738650f66acSMark Johnston 		}
4739650f66acSMark Johnston 		if (dnp->dn_op == DT_TOK_PTR || dnp->dn_op == DT_TOK_DOT ||
4740650f66acSMark Johnston 		    dnp->dn_op == DT_TOK_LBRAC)
4741650f66acSMark Johnston 			(void) fprintf(fp, "%s", opstr(dnp->dn_op));
4742650f66acSMark Johnston 		else
4743650f66acSMark Johnston 			(void) fprintf(fp, " %s ", opstr(dnp->dn_op));
4744650f66acSMark Johnston 		dt_printd(dnp->dn_right, fp, 0);
4745650f66acSMark Johnston 		if (dnp->dn_op == DT_TOK_LBRAC) {
4746650f66acSMark Johnston 			dt_node_t *ln = dnp->dn_right;
4747650f66acSMark Johnston 			while (ln->dn_list != NULL) {
4748650f66acSMark Johnston 				(void) fprintf(fp, ", ");
4749650f66acSMark Johnston 				dt_printd(ln->dn_list, fp, depth);
4750650f66acSMark Johnston 				ln = ln->dn_list;
4751650f66acSMark Johnston 			}
4752650f66acSMark Johnston 			(void) fprintf(fp, "]");
4753650f66acSMark Johnston 		}
4754650f66acSMark Johnston 		(void) fprintf(fp, ")");
4755650f66acSMark Johnston 		break;
4756650f66acSMark Johnston 
4757650f66acSMark Johnston 	case DT_NODE_OP3:
4758650f66acSMark Johnston 		(void) fprintf(fp, "(");
4759650f66acSMark Johnston 		dt_printd(dnp->dn_expr, fp, 0);
4760650f66acSMark Johnston 		(void) fprintf(fp, " ? ");
4761650f66acSMark Johnston 		dt_printd(dnp->dn_left, fp, 0);
4762650f66acSMark Johnston 		(void) fprintf(fp, " : ");
4763650f66acSMark Johnston 		dt_printd(dnp->dn_right, fp, 0);
4764650f66acSMark Johnston 		(void) fprintf(fp, ")");
4765650f66acSMark Johnston 		break;
4766650f66acSMark Johnston 
4767650f66acSMark Johnston 	case DT_NODE_DEXPR:
4768650f66acSMark Johnston 	case DT_NODE_DFUNC:
4769650f66acSMark Johnston 		(void) fprintf(fp, "%*s", depth * 8, "");
4770650f66acSMark Johnston 		dt_printd(dnp->dn_expr, fp, depth + 1);
4771650f66acSMark Johnston 		(void) fprintf(fp, ";\n");
4772650f66acSMark Johnston 		break;
4773650f66acSMark Johnston 
4774650f66acSMark Johnston 	case DT_NODE_PDESC:
4775650f66acSMark Johnston 		(void) fprintf(fp, "%s:%s:%s:%s",
4776650f66acSMark Johnston 		    dnp->dn_desc->dtpd_provider, dnp->dn_desc->dtpd_mod,
4777650f66acSMark Johnston 		    dnp->dn_desc->dtpd_func, dnp->dn_desc->dtpd_name);
4778650f66acSMark Johnston 		break;
4779650f66acSMark Johnston 
4780650f66acSMark Johnston 	case DT_NODE_CLAUSE:
4781650f66acSMark Johnston 		for (arg = dnp->dn_pdescs; arg != NULL; arg = arg->dn_list) {
4782650f66acSMark Johnston 			dt_printd(arg, fp, 0);
4783650f66acSMark Johnston 			if (arg->dn_list != NULL)
4784650f66acSMark Johnston 				(void) fprintf(fp, ",");
4785650f66acSMark Johnston 			(void) fprintf(fp, "\n");
4786650f66acSMark Johnston 		}
4787650f66acSMark Johnston 
4788650f66acSMark Johnston 		if (dnp->dn_pred != NULL) {
4789650f66acSMark Johnston 			(void) fprintf(fp, "/");
4790650f66acSMark Johnston 			dt_printd(dnp->dn_pred, fp, 0);
4791650f66acSMark Johnston 			(void) fprintf(fp, "/\n");
4792650f66acSMark Johnston 		}
4793650f66acSMark Johnston 
47943afba490SChristos Margiolis 		(void) fprintf(fp, "{\n");
4795650f66acSMark Johnston 		for (arg = dnp->dn_acts; arg != NULL; arg = arg->dn_list)
4796650f66acSMark Johnston 			dt_printd(arg, fp, depth + 1);
4797650f66acSMark Johnston 		(void) fprintf(fp, "}\n");
4798650f66acSMark Johnston 		(void) fprintf(fp, "\n");
4799650f66acSMark Johnston 		break;
4800650f66acSMark Johnston 
4801650f66acSMark Johnston 	case DT_NODE_IF:
4802650f66acSMark Johnston 		(void) fprintf(fp, "%*sif (", depth * 8, "");
4803650f66acSMark Johnston 		dt_printd(dnp->dn_conditional, fp, 0);
4804650f66acSMark Johnston 		(void) fprintf(fp, ") {\n");
4805650f66acSMark Johnston 
4806650f66acSMark Johnston 		for (arg = dnp->dn_body; arg != NULL; arg = arg->dn_list)
4807650f66acSMark Johnston 			dt_printd(arg, fp, depth + 1);
4808650f66acSMark Johnston 		if (dnp->dn_alternate_body == NULL) {
4809650f66acSMark Johnston 			(void) fprintf(fp, "%*s}\n", depth * 8, "");
4810650f66acSMark Johnston 		} else {
4811650f66acSMark Johnston 			(void) fprintf(fp, "%*s} else {\n", depth * 8, "");
4812650f66acSMark Johnston 			for (arg = dnp->dn_alternate_body; arg != NULL;
4813650f66acSMark Johnston 			    arg = arg->dn_list)
4814650f66acSMark Johnston 				dt_printd(arg, fp, depth + 1);
4815650f66acSMark Johnston 			(void) fprintf(fp, "%*s}\n", depth * 8, "");
4816650f66acSMark Johnston 		}
4817650f66acSMark Johnston 
4818650f66acSMark Johnston 		break;
4819650f66acSMark Johnston 
4820650f66acSMark Johnston 	default:
4821650f66acSMark Johnston 		(void) fprintf(fp, "/* bad node %p, kind %d */\n",
4822650f66acSMark Johnston 		    (void *)dnp, dnp->dn_kind);
4823650f66acSMark Johnston 	}
4824650f66acSMark Johnston }
4825650f66acSMark Johnston 
48266ff6d951SJohn Birrell void
48276ff6d951SJohn Birrell dt_node_printr(dt_node_t *dnp, FILE *fp, int depth)
48286ff6d951SJohn Birrell {
48296ff6d951SJohn Birrell 	char n[DT_TYPE_NAMELEN], buf[BUFSIZ], a[8];
48306ff6d951SJohn Birrell 	const dtrace_syminfo_t *dts;
48316ff6d951SJohn Birrell 	const dt_idnode_t *inp;
48326ff6d951SJohn Birrell 	dt_node_t *arg;
48336ff6d951SJohn Birrell 
48346ff6d951SJohn Birrell 	(void) fprintf(fp, "%*s", depth * 2, "");
48356ff6d951SJohn Birrell 	(void) dt_attr_str(dnp->dn_attr, a, sizeof (a));
48366ff6d951SJohn Birrell 
48376ff6d951SJohn Birrell 	if (dnp->dn_ctfp != NULL && dnp->dn_type != CTF_ERR &&
48386ff6d951SJohn Birrell 	    ctf_type_name(dnp->dn_ctfp, dnp->dn_type, n, sizeof (n)) != NULL) {
48396ff6d951SJohn Birrell 		(void) snprintf(buf, BUFSIZ, "type=<%s> attr=%s flags=", n, a);
48406ff6d951SJohn Birrell 	} else {
48416ff6d951SJohn Birrell 		(void) snprintf(buf, BUFSIZ, "type=<%ld> attr=%s flags=",
48426ff6d951SJohn Birrell 		    dnp->dn_type, a);
48436ff6d951SJohn Birrell 	}
48446ff6d951SJohn Birrell 
48456ff6d951SJohn Birrell 	if (dnp->dn_flags != 0) {
48466ff6d951SJohn Birrell 		n[0] = '\0';
48476ff6d951SJohn Birrell 		if (dnp->dn_flags & DT_NF_SIGNED)
48486ff6d951SJohn Birrell 			(void) strcat(n, ",SIGN");
48496ff6d951SJohn Birrell 		if (dnp->dn_flags & DT_NF_COOKED)
48506ff6d951SJohn Birrell 			(void) strcat(n, ",COOK");
48516ff6d951SJohn Birrell 		if (dnp->dn_flags & DT_NF_REF)
48526ff6d951SJohn Birrell 			(void) strcat(n, ",REF");
48536ff6d951SJohn Birrell 		if (dnp->dn_flags & DT_NF_LVALUE)
48546ff6d951SJohn Birrell 			(void) strcat(n, ",LVAL");
48556ff6d951SJohn Birrell 		if (dnp->dn_flags & DT_NF_WRITABLE)
48566ff6d951SJohn Birrell 			(void) strcat(n, ",WRITE");
48576ff6d951SJohn Birrell 		if (dnp->dn_flags & DT_NF_BITFIELD)
48586ff6d951SJohn Birrell 			(void) strcat(n, ",BITF");
48596ff6d951SJohn Birrell 		if (dnp->dn_flags & DT_NF_USERLAND)
48606ff6d951SJohn Birrell 			(void) strcat(n, ",USER");
48616ff6d951SJohn Birrell 		(void) strcat(buf, n + 1);
48626ff6d951SJohn Birrell 	} else
48636ff6d951SJohn Birrell 		(void) strcat(buf, "0");
48646ff6d951SJohn Birrell 
48656ff6d951SJohn Birrell 	switch (dnp->dn_kind) {
48666ff6d951SJohn Birrell 	case DT_NODE_FREE:
48676ff6d951SJohn Birrell 		(void) fprintf(fp, "FREE <node %p>\n", (void *)dnp);
48686ff6d951SJohn Birrell 		break;
48696ff6d951SJohn Birrell 
48706ff6d951SJohn Birrell 	case DT_NODE_INT:
48716ff6d951SJohn Birrell 		(void) fprintf(fp, "INT 0x%llx (%s)\n",
48726ff6d951SJohn Birrell 		    (u_longlong_t)dnp->dn_value, buf);
48736ff6d951SJohn Birrell 		break;
48746ff6d951SJohn Birrell 
48756ff6d951SJohn Birrell 	case DT_NODE_STRING:
48766ff6d951SJohn Birrell 		(void) fprintf(fp, "STRING \"%s\" (%s)\n", dnp->dn_string, buf);
48776ff6d951SJohn Birrell 		break;
48786ff6d951SJohn Birrell 
48796ff6d951SJohn Birrell 	case DT_NODE_IDENT:
48806ff6d951SJohn Birrell 		(void) fprintf(fp, "IDENT %s (%s)\n", dnp->dn_string, buf);
48816ff6d951SJohn Birrell 		break;
48826ff6d951SJohn Birrell 
48836ff6d951SJohn Birrell 	case DT_NODE_VAR:
48846ff6d951SJohn Birrell 		(void) fprintf(fp, "VARIABLE %s%s (%s)\n",
48856ff6d951SJohn Birrell 		    (dnp->dn_ident->di_flags & DT_IDFLG_LOCAL) ? "this->" :
48866ff6d951SJohn Birrell 		    (dnp->dn_ident->di_flags & DT_IDFLG_TLS) ? "self->" : "",
48876ff6d951SJohn Birrell 		    dnp->dn_ident->di_name, buf);
48886ff6d951SJohn Birrell 
48896ff6d951SJohn Birrell 		if (dnp->dn_args != NULL)
48906ff6d951SJohn Birrell 			(void) fprintf(fp, "%*s[\n", depth * 2, "");
48916ff6d951SJohn Birrell 
48926ff6d951SJohn Birrell 		for (arg = dnp->dn_args; arg != NULL; arg = arg->dn_list) {
48936ff6d951SJohn Birrell 			dt_node_printr(arg, fp, depth + 1);
48946ff6d951SJohn Birrell 			if (arg->dn_list != NULL)
48956ff6d951SJohn Birrell 				(void) fprintf(fp, "%*s,\n", depth * 2, "");
48966ff6d951SJohn Birrell 		}
48976ff6d951SJohn Birrell 
48986ff6d951SJohn Birrell 		if (dnp->dn_args != NULL)
48996ff6d951SJohn Birrell 			(void) fprintf(fp, "%*s]\n", depth * 2, "");
49006ff6d951SJohn Birrell 		break;
49016ff6d951SJohn Birrell 
49026ff6d951SJohn Birrell 	case DT_NODE_SYM:
49036ff6d951SJohn Birrell 		dts = dnp->dn_ident->di_data;
49046ff6d951SJohn Birrell 		(void) fprintf(fp, "SYMBOL %s`%s (%s)\n",
49056ff6d951SJohn Birrell 		    dts->dts_object, dts->dts_name, buf);
49066ff6d951SJohn Birrell 		break;
49076ff6d951SJohn Birrell 
49086ff6d951SJohn Birrell 	case DT_NODE_TYPE:
49096ff6d951SJohn Birrell 		if (dnp->dn_string != NULL) {
49106ff6d951SJohn Birrell 			(void) fprintf(fp, "TYPE (%s) %s\n",
49116ff6d951SJohn Birrell 			    buf, dnp->dn_string);
49126ff6d951SJohn Birrell 		} else
49136ff6d951SJohn Birrell 			(void) fprintf(fp, "TYPE (%s)\n", buf);
49146ff6d951SJohn Birrell 		break;
49156ff6d951SJohn Birrell 
49166ff6d951SJohn Birrell 	case DT_NODE_FUNC:
49176ff6d951SJohn Birrell 		(void) fprintf(fp, "FUNC %s (%s)\n",
49186ff6d951SJohn Birrell 		    dnp->dn_ident->di_name, buf);
49196ff6d951SJohn Birrell 
49206ff6d951SJohn Birrell 		for (arg = dnp->dn_args; arg != NULL; arg = arg->dn_list) {
49216ff6d951SJohn Birrell 			dt_node_printr(arg, fp, depth + 1);
49226ff6d951SJohn Birrell 			if (arg->dn_list != NULL)
49236ff6d951SJohn Birrell 				(void) fprintf(fp, "%*s,\n", depth * 2, "");
49246ff6d951SJohn Birrell 		}
49256ff6d951SJohn Birrell 		break;
49266ff6d951SJohn Birrell 
49276ff6d951SJohn Birrell 	case DT_NODE_OP1:
49286ff6d951SJohn Birrell 		(void) fprintf(fp, "OP1 %s (%s)\n", opstr(dnp->dn_op), buf);
49296ff6d951SJohn Birrell 		dt_node_printr(dnp->dn_child, fp, depth + 1);
49306ff6d951SJohn Birrell 		break;
49316ff6d951SJohn Birrell 
49326ff6d951SJohn Birrell 	case DT_NODE_OP2:
49336ff6d951SJohn Birrell 		(void) fprintf(fp, "OP2 %s (%s)\n", opstr(dnp->dn_op), buf);
49346ff6d951SJohn Birrell 		dt_node_printr(dnp->dn_left, fp, depth + 1);
49356ff6d951SJohn Birrell 		dt_node_printr(dnp->dn_right, fp, depth + 1);
4936650f66acSMark Johnston 		if (dnp->dn_op == DT_TOK_LBRAC) {
4937650f66acSMark Johnston 			dt_node_t *ln = dnp->dn_right;
4938650f66acSMark Johnston 			while (ln->dn_list != NULL) {
4939650f66acSMark Johnston 				dt_node_printr(ln->dn_list, fp, depth + 1);
4940650f66acSMark Johnston 				ln = ln->dn_list;
4941650f66acSMark Johnston 			}
4942650f66acSMark Johnston 		}
49436ff6d951SJohn Birrell 		break;
49446ff6d951SJohn Birrell 
49456ff6d951SJohn Birrell 	case DT_NODE_OP3:
49466ff6d951SJohn Birrell 		(void) fprintf(fp, "OP3 (%s)\n", buf);
49476ff6d951SJohn Birrell 		dt_node_printr(dnp->dn_expr, fp, depth + 1);
49486ff6d951SJohn Birrell 		(void) fprintf(fp, "%*s?\n", depth * 2, "");
49496ff6d951SJohn Birrell 		dt_node_printr(dnp->dn_left, fp, depth + 1);
49506ff6d951SJohn Birrell 		(void) fprintf(fp, "%*s:\n", depth * 2, "");
49516ff6d951SJohn Birrell 		dt_node_printr(dnp->dn_right, fp, depth + 1);
49526ff6d951SJohn Birrell 		break;
49536ff6d951SJohn Birrell 
49546ff6d951SJohn Birrell 	case DT_NODE_DEXPR:
49556ff6d951SJohn Birrell 	case DT_NODE_DFUNC:
49566ff6d951SJohn Birrell 		(void) fprintf(fp, "D EXPRESSION attr=%s\n", a);
49576ff6d951SJohn Birrell 		dt_node_printr(dnp->dn_expr, fp, depth + 1);
49586ff6d951SJohn Birrell 		break;
49596ff6d951SJohn Birrell 
49606ff6d951SJohn Birrell 	case DT_NODE_AGG:
49616ff6d951SJohn Birrell 		(void) fprintf(fp, "AGGREGATE @%s attr=%s [\n",
49626ff6d951SJohn Birrell 		    dnp->dn_ident->di_name, a);
49636ff6d951SJohn Birrell 
49646ff6d951SJohn Birrell 		for (arg = dnp->dn_aggtup; arg != NULL; arg = arg->dn_list) {
49656ff6d951SJohn Birrell 			dt_node_printr(arg, fp, depth + 1);
49666ff6d951SJohn Birrell 			if (arg->dn_list != NULL)
49676ff6d951SJohn Birrell 				(void) fprintf(fp, "%*s,\n", depth * 2, "");
49686ff6d951SJohn Birrell 		}
49696ff6d951SJohn Birrell 
49706ff6d951SJohn Birrell 		if (dnp->dn_aggfun) {
49716ff6d951SJohn Birrell 			(void) fprintf(fp, "%*s] = ", depth * 2, "");
49726ff6d951SJohn Birrell 			dt_node_printr(dnp->dn_aggfun, fp, depth + 1);
49736ff6d951SJohn Birrell 		} else
49746ff6d951SJohn Birrell 			(void) fprintf(fp, "%*s]\n", depth * 2, "");
49756ff6d951SJohn Birrell 
49766ff6d951SJohn Birrell 		if (dnp->dn_aggfun)
49776ff6d951SJohn Birrell 			(void) fprintf(fp, "%*s)\n", depth * 2, "");
49786ff6d951SJohn Birrell 		break;
49796ff6d951SJohn Birrell 
49806ff6d951SJohn Birrell 	case DT_NODE_PDESC:
49816ff6d951SJohn Birrell 		(void) fprintf(fp, "PDESC %s:%s:%s:%s [%u]\n",
49826ff6d951SJohn Birrell 		    dnp->dn_desc->dtpd_provider, dnp->dn_desc->dtpd_mod,
49836ff6d951SJohn Birrell 		    dnp->dn_desc->dtpd_func, dnp->dn_desc->dtpd_name,
49846ff6d951SJohn Birrell 		    dnp->dn_desc->dtpd_id);
49856ff6d951SJohn Birrell 		break;
49866ff6d951SJohn Birrell 
49876ff6d951SJohn Birrell 	case DT_NODE_CLAUSE:
49886ff6d951SJohn Birrell 		(void) fprintf(fp, "CLAUSE attr=%s\n", a);
49896ff6d951SJohn Birrell 
49906ff6d951SJohn Birrell 		for (arg = dnp->dn_pdescs; arg != NULL; arg = arg->dn_list)
49916ff6d951SJohn Birrell 			dt_node_printr(arg, fp, depth + 1);
49926ff6d951SJohn Birrell 
49936ff6d951SJohn Birrell 		(void) fprintf(fp, "%*sCTXATTR %s\n", depth * 2, "",
49946ff6d951SJohn Birrell 		    dt_attr_str(dnp->dn_ctxattr, a, sizeof (a)));
49956ff6d951SJohn Birrell 
49966ff6d951SJohn Birrell 		if (dnp->dn_pred != NULL) {
49976ff6d951SJohn Birrell 			(void) fprintf(fp, "%*sPREDICATE /\n", depth * 2, "");
49986ff6d951SJohn Birrell 			dt_node_printr(dnp->dn_pred, fp, depth + 1);
49996ff6d951SJohn Birrell 			(void) fprintf(fp, "%*s/\n", depth * 2, "");
50006ff6d951SJohn Birrell 		}
50016ff6d951SJohn Birrell 
50026ff6d951SJohn Birrell 		for (arg = dnp->dn_acts; arg != NULL; arg = arg->dn_list)
50036ff6d951SJohn Birrell 			dt_node_printr(arg, fp, depth + 1);
5004650f66acSMark Johnston 		(void) fprintf(fp, "\n");
50056ff6d951SJohn Birrell 		break;
50066ff6d951SJohn Birrell 
50076ff6d951SJohn Birrell 	case DT_NODE_INLINE:
50086ff6d951SJohn Birrell 		inp = dnp->dn_ident->di_iarg;
50096ff6d951SJohn Birrell 
50106ff6d951SJohn Birrell 		(void) fprintf(fp, "INLINE %s (%s)\n",
50116ff6d951SJohn Birrell 		    dnp->dn_ident->di_name, buf);
50126ff6d951SJohn Birrell 		dt_node_printr(inp->din_root, fp, depth + 1);
50136ff6d951SJohn Birrell 		break;
50146ff6d951SJohn Birrell 
50156ff6d951SJohn Birrell 	case DT_NODE_MEMBER:
50166ff6d951SJohn Birrell 		(void) fprintf(fp, "MEMBER %s (%s)\n", dnp->dn_membname, buf);
50176ff6d951SJohn Birrell 		if (dnp->dn_membexpr)
50186ff6d951SJohn Birrell 			dt_node_printr(dnp->dn_membexpr, fp, depth + 1);
50196ff6d951SJohn Birrell 		break;
50206ff6d951SJohn Birrell 
50216ff6d951SJohn Birrell 	case DT_NODE_XLATOR:
50226ff6d951SJohn Birrell 		(void) fprintf(fp, "XLATOR (%s)", buf);
50236ff6d951SJohn Birrell 
50246ff6d951SJohn Birrell 		if (ctf_type_name(dnp->dn_xlator->dx_src_ctfp,
50256ff6d951SJohn Birrell 		    dnp->dn_xlator->dx_src_type, n, sizeof (n)) != NULL)
50266ff6d951SJohn Birrell 			(void) fprintf(fp, " from <%s>", n);
50276ff6d951SJohn Birrell 
50286ff6d951SJohn Birrell 		if (ctf_type_name(dnp->dn_xlator->dx_dst_ctfp,
50296ff6d951SJohn Birrell 		    dnp->dn_xlator->dx_dst_type, n, sizeof (n)) != NULL)
50306ff6d951SJohn Birrell 			(void) fprintf(fp, " to <%s>", n);
50316ff6d951SJohn Birrell 
50326ff6d951SJohn Birrell 		(void) fprintf(fp, "\n");
50336ff6d951SJohn Birrell 
50346ff6d951SJohn Birrell 		for (arg = dnp->dn_members; arg != NULL; arg = arg->dn_list)
50356ff6d951SJohn Birrell 			dt_node_printr(arg, fp, depth + 1);
50366ff6d951SJohn Birrell 		break;
50376ff6d951SJohn Birrell 
50386ff6d951SJohn Birrell 	case DT_NODE_PROBE:
50396ff6d951SJohn Birrell 		(void) fprintf(fp, "PROBE %s\n", dnp->dn_ident->di_name);
50406ff6d951SJohn Birrell 		break;
50416ff6d951SJohn Birrell 
50426ff6d951SJohn Birrell 	case DT_NODE_PROVIDER:
50436ff6d951SJohn Birrell 		(void) fprintf(fp, "PROVIDER %s (%s)\n",
50446ff6d951SJohn Birrell 		    dnp->dn_provname, dnp->dn_provred ? "redecl" : "decl");
50456ff6d951SJohn Birrell 		for (arg = dnp->dn_probes; arg != NULL; arg = arg->dn_list)
50466ff6d951SJohn Birrell 			dt_node_printr(arg, fp, depth + 1);
50476ff6d951SJohn Birrell 		break;
50486ff6d951SJohn Birrell 
50496ff6d951SJohn Birrell 	case DT_NODE_PROG:
50506ff6d951SJohn Birrell 		(void) fprintf(fp, "PROGRAM attr=%s\n", a);
50516ff6d951SJohn Birrell 		for (arg = dnp->dn_list; arg != NULL; arg = arg->dn_list)
50526ff6d951SJohn Birrell 			dt_node_printr(arg, fp, depth + 1);
50536ff6d951SJohn Birrell 		break;
50546ff6d951SJohn Birrell 
5055650f66acSMark Johnston 	case DT_NODE_IF:
5056650f66acSMark Johnston 		(void) fprintf(fp, "IF attr=%s CONDITION:\n", a);
5057650f66acSMark Johnston 
5058650f66acSMark Johnston 		dt_node_printr(dnp->dn_conditional, fp, depth + 1);
5059650f66acSMark Johnston 
5060650f66acSMark Johnston 		(void) fprintf(fp, "%*sIF BODY: \n", depth * 2, "");
5061650f66acSMark Johnston 		for (arg = dnp->dn_body; arg != NULL; arg = arg->dn_list)
5062650f66acSMark Johnston 			dt_node_printr(arg, fp, depth + 1);
5063650f66acSMark Johnston 
5064650f66acSMark Johnston 		if (dnp->dn_alternate_body != NULL) {
5065650f66acSMark Johnston 			(void) fprintf(fp, "%*sIF ELSE: \n", depth * 2, "");
5066650f66acSMark Johnston 			for (arg = dnp->dn_alternate_body; arg != NULL;
5067650f66acSMark Johnston 			    arg = arg->dn_list)
5068650f66acSMark Johnston 				dt_node_printr(arg, fp, depth + 1);
5069650f66acSMark Johnston 		}
5070650f66acSMark Johnston 
5071650f66acSMark Johnston 		break;
5072650f66acSMark Johnston 
50736ff6d951SJohn Birrell 	default:
50746ff6d951SJohn Birrell 		(void) fprintf(fp, "<bad node %p, kind %d>\n",
50756ff6d951SJohn Birrell 		    (void *)dnp, dnp->dn_kind);
50766ff6d951SJohn Birrell 	}
50776ff6d951SJohn Birrell }
50786ff6d951SJohn Birrell 
50796ff6d951SJohn Birrell int
50806ff6d951SJohn Birrell dt_node_root(dt_node_t *dnp)
50816ff6d951SJohn Birrell {
50826ff6d951SJohn Birrell 	yypcb->pcb_root = dnp;
50836ff6d951SJohn Birrell 	return (0);
50846ff6d951SJohn Birrell }
50856ff6d951SJohn Birrell 
50866ff6d951SJohn Birrell /*PRINTFLIKE3*/
50876ff6d951SJohn Birrell void
50886ff6d951SJohn Birrell dnerror(const dt_node_t *dnp, dt_errtag_t tag, const char *format, ...)
50896ff6d951SJohn Birrell {
50906ff6d951SJohn Birrell 	int oldlineno = yylineno;
50916ff6d951SJohn Birrell 	va_list ap;
50926ff6d951SJohn Birrell 
50936ff6d951SJohn Birrell 	yylineno = dnp->dn_line;
50946ff6d951SJohn Birrell 
50956ff6d951SJohn Birrell 	va_start(ap, format);
50966ff6d951SJohn Birrell 	xyvwarn(tag, format, ap);
50976ff6d951SJohn Birrell 	va_end(ap);
50986ff6d951SJohn Birrell 
50996ff6d951SJohn Birrell 	yylineno = oldlineno;
51006ff6d951SJohn Birrell 	longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
51016ff6d951SJohn Birrell }
51026ff6d951SJohn Birrell 
51036ff6d951SJohn Birrell /*PRINTFLIKE3*/
51046ff6d951SJohn Birrell void
51056ff6d951SJohn Birrell dnwarn(const dt_node_t *dnp, dt_errtag_t tag, const char *format, ...)
51066ff6d951SJohn Birrell {
51076ff6d951SJohn Birrell 	int oldlineno = yylineno;
51086ff6d951SJohn Birrell 	va_list ap;
51096ff6d951SJohn Birrell 
51106ff6d951SJohn Birrell 	yylineno = dnp->dn_line;
51116ff6d951SJohn Birrell 
51126ff6d951SJohn Birrell 	va_start(ap, format);
51136ff6d951SJohn Birrell 	xyvwarn(tag, format, ap);
51146ff6d951SJohn Birrell 	va_end(ap);
51156ff6d951SJohn Birrell 
51166ff6d951SJohn Birrell 	yylineno = oldlineno;
51176ff6d951SJohn Birrell }
51186ff6d951SJohn Birrell 
51196ff6d951SJohn Birrell /*PRINTFLIKE2*/
51206ff6d951SJohn Birrell void
51216ff6d951SJohn Birrell xyerror(dt_errtag_t tag, const char *format, ...)
51226ff6d951SJohn Birrell {
51236ff6d951SJohn Birrell 	va_list ap;
51246ff6d951SJohn Birrell 
51256ff6d951SJohn Birrell 	va_start(ap, format);
51266ff6d951SJohn Birrell 	xyvwarn(tag, format, ap);
51276ff6d951SJohn Birrell 	va_end(ap);
51286ff6d951SJohn Birrell 
51296ff6d951SJohn Birrell 	longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
51306ff6d951SJohn Birrell }
51316ff6d951SJohn Birrell 
51326ff6d951SJohn Birrell /*PRINTFLIKE2*/
51336ff6d951SJohn Birrell void
51346ff6d951SJohn Birrell xywarn(dt_errtag_t tag, const char *format, ...)
51356ff6d951SJohn Birrell {
51366ff6d951SJohn Birrell 	va_list ap;
51376ff6d951SJohn Birrell 
51386ff6d951SJohn Birrell 	va_start(ap, format);
51396ff6d951SJohn Birrell 	xyvwarn(tag, format, ap);
51406ff6d951SJohn Birrell 	va_end(ap);
51416ff6d951SJohn Birrell }
51426ff6d951SJohn Birrell 
51436ff6d951SJohn Birrell void
51446ff6d951SJohn Birrell xyvwarn(dt_errtag_t tag, const char *format, va_list ap)
51456ff6d951SJohn Birrell {
51466ff6d951SJohn Birrell 	if (yypcb == NULL)
51476ff6d951SJohn Birrell 		return; /* compiler is not currently active: act as a no-op */
51486ff6d951SJohn Birrell 
51496ff6d951SJohn Birrell 	dt_set_errmsg(yypcb->pcb_hdl, dt_errtag(tag), yypcb->pcb_region,
51506ff6d951SJohn Birrell 	    yypcb->pcb_filetag, yypcb->pcb_fileptr ? yylineno : 0, format, ap);
51516ff6d951SJohn Birrell }
51526ff6d951SJohn Birrell 
51536ff6d951SJohn Birrell /*PRINTFLIKE1*/
51546ff6d951SJohn Birrell void
51556ff6d951SJohn Birrell yyerror(const char *format, ...)
51566ff6d951SJohn Birrell {
51576ff6d951SJohn Birrell 	va_list ap;
51586ff6d951SJohn Birrell 
51596ff6d951SJohn Birrell 	va_start(ap, format);
51606ff6d951SJohn Birrell 	yyvwarn(format, ap);
51616ff6d951SJohn Birrell 	va_end(ap);
51626ff6d951SJohn Birrell 
51636ff6d951SJohn Birrell 	longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
51646ff6d951SJohn Birrell }
51656ff6d951SJohn Birrell 
51666ff6d951SJohn Birrell /*PRINTFLIKE1*/
51676ff6d951SJohn Birrell void
51686ff6d951SJohn Birrell yywarn(const char *format, ...)
51696ff6d951SJohn Birrell {
51706ff6d951SJohn Birrell 	va_list ap;
51716ff6d951SJohn Birrell 
51726ff6d951SJohn Birrell 	va_start(ap, format);
51736ff6d951SJohn Birrell 	yyvwarn(format, ap);
51746ff6d951SJohn Birrell 	va_end(ap);
51756ff6d951SJohn Birrell }
51766ff6d951SJohn Birrell 
51776ff6d951SJohn Birrell void
51786ff6d951SJohn Birrell yyvwarn(const char *format, va_list ap)
51796ff6d951SJohn Birrell {
51806ff6d951SJohn Birrell 	if (yypcb == NULL)
51816ff6d951SJohn Birrell 		return; /* compiler is not currently active: act as a no-op */
51826ff6d951SJohn Birrell 
51836ff6d951SJohn Birrell 	dt_set_errmsg(yypcb->pcb_hdl, dt_errtag(D_SYNTAX), yypcb->pcb_region,
51846ff6d951SJohn Birrell 	    yypcb->pcb_filetag, yypcb->pcb_fileptr ? yylineno : 0, format, ap);
51856ff6d951SJohn Birrell 
51866ff6d951SJohn Birrell 	if (strchr(format, '\n') == NULL) {
51876ff6d951SJohn Birrell 		dtrace_hdl_t *dtp = yypcb->pcb_hdl;
51886ff6d951SJohn Birrell 		size_t len = strlen(dtp->dt_errmsg);
51896ff6d951SJohn Birrell 		char *p, *s = dtp->dt_errmsg + len;
51906ff6d951SJohn Birrell 		size_t n = sizeof (dtp->dt_errmsg) - len;
51916ff6d951SJohn Birrell 
51926ff6d951SJohn Birrell 		if (yytext[0] == '\0')
51936ff6d951SJohn Birrell 			(void) snprintf(s, n, " near end of input");
51946ff6d951SJohn Birrell 		else if (yytext[0] == '\n')
51956ff6d951SJohn Birrell 			(void) snprintf(s, n, " near end of line");
51966ff6d951SJohn Birrell 		else {
51976ff6d951SJohn Birrell 			if ((p = strchr(yytext, '\n')) != NULL)
51986ff6d951SJohn Birrell 				*p = '\0'; /* crop at newline */
51996ff6d951SJohn Birrell 			(void) snprintf(s, n, " near \"%s\"", yytext);
52006ff6d951SJohn Birrell 		}
52016ff6d951SJohn Birrell 	}
52026ff6d951SJohn Birrell }
52036ff6d951SJohn Birrell 
52046ff6d951SJohn Birrell void
52056ff6d951SJohn Birrell yylabel(const char *label)
52066ff6d951SJohn Birrell {
52076ff6d951SJohn Birrell 	dt_dprintf("set label to <%s>\n", label ? label : "NULL");
52086ff6d951SJohn Birrell 	yypcb->pcb_region = label;
52096ff6d951SJohn Birrell }
52106ff6d951SJohn Birrell 
52116ff6d951SJohn Birrell int
52126ff6d951SJohn Birrell yywrap(void)
52136ff6d951SJohn Birrell {
52146ff6d951SJohn Birrell 	return (1); /* indicate that lex should return a zero token for EOF */
52156ff6d951SJohn Birrell }
5216