10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*13093SRoger.Faulkner@Oracle.COM * Common Development and Distribution License (the "License").
6*13093SRoger.Faulkner@Oracle.COM * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
211222Smws
220Sstevel@tonic-gate /*
23*13093SRoger.Faulkner@Oracle.COM * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate /*
270Sstevel@tonic-gate * DTrace D Language Parser
280Sstevel@tonic-gate *
290Sstevel@tonic-gate * The D Parser is a lex/yacc parser consisting of the lexer dt_lex.l, the
300Sstevel@tonic-gate * parsing grammar dt_grammar.y, and this file, dt_parser.c, which handles
310Sstevel@tonic-gate * the construction of the parse tree nodes and their syntactic validation.
320Sstevel@tonic-gate * The parse tree is constructed of dt_node_t structures (see <dt_parser.h>)
330Sstevel@tonic-gate * that are built in two passes: (1) the "create" pass, where the parse tree
340Sstevel@tonic-gate * nodes are allocated by calls from the grammar to dt_node_*() subroutines,
350Sstevel@tonic-gate * and (2) the "cook" pass, where nodes are coalesced, assigned D types, and
360Sstevel@tonic-gate * validated according to the syntactic rules of the language.
370Sstevel@tonic-gate *
380Sstevel@tonic-gate * All node allocations are performed using dt_node_alloc(). All node frees
390Sstevel@tonic-gate * during the parsing phase are performed by dt_node_free(), which frees node-
400Sstevel@tonic-gate * internal state but does not actually free the nodes. All final node frees
410Sstevel@tonic-gate * are done as part of the end of dt_compile() or as part of destroying
420Sstevel@tonic-gate * persistent identifiers or translators which have embedded nodes.
430Sstevel@tonic-gate *
440Sstevel@tonic-gate * The dt_node_* routines that implement pass (1) may allocate new nodes. The
450Sstevel@tonic-gate * dt_cook_* routines that implement pass (2) may *not* allocate new nodes.
460Sstevel@tonic-gate * They may free existing nodes using dt_node_free(), but they may not actually
470Sstevel@tonic-gate * deallocate any dt_node_t's. Currently dt_cook_op2() is an exception to this
480Sstevel@tonic-gate * rule: see the comments therein for how this issue is resolved.
490Sstevel@tonic-gate *
500Sstevel@tonic-gate * The dt_cook_* routines are responsible for (at minimum) setting the final
510Sstevel@tonic-gate * node type (dn_ctfp/dn_type) and attributes (dn_attr). If dn_ctfp/dn_type
520Sstevel@tonic-gate * are set manually (i.e. not by one of the type assignment functions), then
530Sstevel@tonic-gate * the DT_NF_COOKED flag must be set manually on the node.
540Sstevel@tonic-gate *
550Sstevel@tonic-gate * The cooking pass can be applied to the same parse tree more than once (used
560Sstevel@tonic-gate * in the case of a comma-separated list of probe descriptions). As such, the
570Sstevel@tonic-gate * cook routines must not perform any parse tree transformations which would
580Sstevel@tonic-gate * be invalid if the tree were subsequently cooked using a different context.
590Sstevel@tonic-gate *
600Sstevel@tonic-gate * The dn_ctfp and dn_type fields form the type of the node. This tuple can
610Sstevel@tonic-gate * take on the following set of values, which form our type invariants:
620Sstevel@tonic-gate *
630Sstevel@tonic-gate * 1. dn_ctfp = NULL, dn_type = CTF_ERR
640Sstevel@tonic-gate *
650Sstevel@tonic-gate * In this state, the node has unknown type and is not yet cooked. The
660Sstevel@tonic-gate * DT_NF_COOKED flag is not yet set on the node.
670Sstevel@tonic-gate *
680Sstevel@tonic-gate * 2. dn_ctfp = DT_DYN_CTFP(dtp), dn_type = DT_DYN_TYPE(dtp)
690Sstevel@tonic-gate *
700Sstevel@tonic-gate * In this state, the node is a dynamic D type. This means that generic
710Sstevel@tonic-gate * operations are not valid on this node and only code that knows how to
720Sstevel@tonic-gate * examine the inner details of the node can operate on it. A <DYN> node
730Sstevel@tonic-gate * must have dn_ident set to point to an identifier describing the object
740Sstevel@tonic-gate * and its type. The DT_NF_REF flag is set for all nodes of type <DYN>.
750Sstevel@tonic-gate * At present, the D compiler uses the <DYN> type for:
760Sstevel@tonic-gate *
770Sstevel@tonic-gate * - associative arrays that do not yet have a value type defined
780Sstevel@tonic-gate * - translated data (i.e. the result of the xlate operator)
790Sstevel@tonic-gate * - aggregations
800Sstevel@tonic-gate *
810Sstevel@tonic-gate * 3. dn_ctfp = DT_STR_CTFP(dtp), dn_type = DT_STR_TYPE(dtp)
820Sstevel@tonic-gate *
830Sstevel@tonic-gate * In this state, the node is of type D string. The string type is really
840Sstevel@tonic-gate * a char[0] typedef, but requires special handling throughout the compiler.
850Sstevel@tonic-gate *
860Sstevel@tonic-gate * 4. dn_ctfp != NULL, dn_type = any other type ID
870Sstevel@tonic-gate *
880Sstevel@tonic-gate * In this state, the node is of some known D/CTF type. The normal libctf
890Sstevel@tonic-gate * APIs can be used to learn more about the type name or structure. When
900Sstevel@tonic-gate * the type is assigned, the DT_NF_SIGNED, DT_NF_REF, and DT_NF_BITFIELD
910Sstevel@tonic-gate * flags cache the corresponding attributes of the underlying CTF type.
920Sstevel@tonic-gate */
930Sstevel@tonic-gate
940Sstevel@tonic-gate #include <sys/param.h>
950Sstevel@tonic-gate #include <limits.h>
960Sstevel@tonic-gate #include <setjmp.h>
970Sstevel@tonic-gate #include <strings.h>
980Sstevel@tonic-gate #include <assert.h>
990Sstevel@tonic-gate #include <alloca.h>
1000Sstevel@tonic-gate #include <stdlib.h>
1010Sstevel@tonic-gate #include <stdarg.h>
1020Sstevel@tonic-gate #include <stdio.h>
1030Sstevel@tonic-gate #include <errno.h>
1040Sstevel@tonic-gate #include <ctype.h>
1050Sstevel@tonic-gate
1060Sstevel@tonic-gate #include <dt_impl.h>
1070Sstevel@tonic-gate #include <dt_grammar.h>
1080Sstevel@tonic-gate #include <dt_module.h>
1090Sstevel@tonic-gate #include <dt_provider.h>
1100Sstevel@tonic-gate #include <dt_string.h>
1110Sstevel@tonic-gate #include <dt_as.h>
1120Sstevel@tonic-gate
1130Sstevel@tonic-gate dt_pcb_t *yypcb; /* current control block for parser */
1140Sstevel@tonic-gate dt_node_t *yypragma; /* lex token list for control lines */
1150Sstevel@tonic-gate char yyintprefix; /* int token macro prefix (+/-) */
1160Sstevel@tonic-gate char yyintsuffix[4]; /* int token suffix string [uU][lL] */
1170Sstevel@tonic-gate int yyintdecimal; /* int token format flag (1=decimal, 0=octal/hex) */
1180Sstevel@tonic-gate
1190Sstevel@tonic-gate static const char *
opstr(int op)1200Sstevel@tonic-gate opstr(int op)
1210Sstevel@tonic-gate {
1220Sstevel@tonic-gate switch (op) {
1230Sstevel@tonic-gate case DT_TOK_COMMA: return (",");
1240Sstevel@tonic-gate case DT_TOK_ELLIPSIS: return ("...");
1250Sstevel@tonic-gate case DT_TOK_ASGN: return ("=");
1260Sstevel@tonic-gate case DT_TOK_ADD_EQ: return ("+=");
1270Sstevel@tonic-gate case DT_TOK_SUB_EQ: return ("-=");
1280Sstevel@tonic-gate case DT_TOK_MUL_EQ: return ("*=");
1290Sstevel@tonic-gate case DT_TOK_DIV_EQ: return ("/=");
1300Sstevel@tonic-gate case DT_TOK_MOD_EQ: return ("%=");
1310Sstevel@tonic-gate case DT_TOK_AND_EQ: return ("&=");
1320Sstevel@tonic-gate case DT_TOK_XOR_EQ: return ("^=");
1330Sstevel@tonic-gate case DT_TOK_OR_EQ: return ("|=");
1340Sstevel@tonic-gate case DT_TOK_LSH_EQ: return ("<<=");
1350Sstevel@tonic-gate case DT_TOK_RSH_EQ: return (">>=");
1360Sstevel@tonic-gate case DT_TOK_QUESTION: return ("?");
1370Sstevel@tonic-gate case DT_TOK_COLON: return (":");
1380Sstevel@tonic-gate case DT_TOK_LOR: return ("||");
1390Sstevel@tonic-gate case DT_TOK_LXOR: return ("^^");
1400Sstevel@tonic-gate case DT_TOK_LAND: return ("&&");
1410Sstevel@tonic-gate case DT_TOK_BOR: return ("|");
1420Sstevel@tonic-gate case DT_TOK_XOR: return ("^");
1430Sstevel@tonic-gate case DT_TOK_BAND: return ("&");
1440Sstevel@tonic-gate case DT_TOK_EQU: return ("==");
1450Sstevel@tonic-gate case DT_TOK_NEQ: return ("!=");
1460Sstevel@tonic-gate case DT_TOK_LT: return ("<");
1470Sstevel@tonic-gate case DT_TOK_LE: return ("<=");
1480Sstevel@tonic-gate case DT_TOK_GT: return (">");
1490Sstevel@tonic-gate case DT_TOK_GE: return (">=");
1500Sstevel@tonic-gate case DT_TOK_LSH: return ("<<");
1510Sstevel@tonic-gate case DT_TOK_RSH: return (">>");
1520Sstevel@tonic-gate case DT_TOK_ADD: return ("+");
1530Sstevel@tonic-gate case DT_TOK_SUB: return ("-");
1540Sstevel@tonic-gate case DT_TOK_MUL: return ("*");
1550Sstevel@tonic-gate case DT_TOK_DIV: return ("/");
1560Sstevel@tonic-gate case DT_TOK_MOD: return ("%");
1570Sstevel@tonic-gate case DT_TOK_LNEG: return ("!");
1580Sstevel@tonic-gate case DT_TOK_BNEG: return ("~");
1590Sstevel@tonic-gate case DT_TOK_ADDADD: return ("++");
1600Sstevel@tonic-gate case DT_TOK_PREINC: return ("++");
1610Sstevel@tonic-gate case DT_TOK_POSTINC: return ("++");
1620Sstevel@tonic-gate case DT_TOK_SUBSUB: return ("--");
1630Sstevel@tonic-gate case DT_TOK_PREDEC: return ("--");
1640Sstevel@tonic-gate case DT_TOK_POSTDEC: return ("--");
1650Sstevel@tonic-gate case DT_TOK_IPOS: return ("+");
1660Sstevel@tonic-gate case DT_TOK_INEG: return ("-");
1670Sstevel@tonic-gate case DT_TOK_DEREF: return ("*");
1680Sstevel@tonic-gate case DT_TOK_ADDROF: return ("&");
1690Sstevel@tonic-gate case DT_TOK_OFFSETOF: return ("offsetof");
1700Sstevel@tonic-gate case DT_TOK_SIZEOF: return ("sizeof");
1710Sstevel@tonic-gate case DT_TOK_STRINGOF: return ("stringof");
1720Sstevel@tonic-gate case DT_TOK_XLATE: return ("xlate");
1730Sstevel@tonic-gate case DT_TOK_LPAR: return ("(");
1740Sstevel@tonic-gate case DT_TOK_RPAR: return (")");
1750Sstevel@tonic-gate case DT_TOK_LBRAC: return ("[");
1760Sstevel@tonic-gate case DT_TOK_RBRAC: return ("]");
1770Sstevel@tonic-gate case DT_TOK_PTR: return ("->");
1780Sstevel@tonic-gate case DT_TOK_DOT: return (".");
1790Sstevel@tonic-gate case DT_TOK_STRING: return ("<string>");
1800Sstevel@tonic-gate case DT_TOK_IDENT: return ("<ident>");
1810Sstevel@tonic-gate case DT_TOK_TNAME: return ("<type>");
1820Sstevel@tonic-gate case DT_TOK_INT: return ("<int>");
1830Sstevel@tonic-gate default: return ("<?>");
1840Sstevel@tonic-gate }
1850Sstevel@tonic-gate }
1860Sstevel@tonic-gate
1870Sstevel@tonic-gate int
dt_type_lookup(const char * s,dtrace_typeinfo_t * tip)1880Sstevel@tonic-gate dt_type_lookup(const char *s, dtrace_typeinfo_t *tip)
1890Sstevel@tonic-gate {
1900Sstevel@tonic-gate static const char delimiters[] = " \t\n\r\v\f*`";
1910Sstevel@tonic-gate dtrace_hdl_t *dtp = yypcb->pcb_hdl;
1920Sstevel@tonic-gate const char *p, *q, *end, *obj;
1930Sstevel@tonic-gate
1940Sstevel@tonic-gate for (p = s, end = s + strlen(s); *p != '\0'; p = q) {
1950Sstevel@tonic-gate while (isspace(*p))
1960Sstevel@tonic-gate p++; /* skip leading whitespace prior to token */
1970Sstevel@tonic-gate
1980Sstevel@tonic-gate if (p == end || (q = strpbrk(p + 1, delimiters)) == NULL)
1990Sstevel@tonic-gate break; /* empty string or single token remaining */
2000Sstevel@tonic-gate
2010Sstevel@tonic-gate if (*q == '`') {
2020Sstevel@tonic-gate char *object = alloca((size_t)(q - p) + 1);
2030Sstevel@tonic-gate char *type = alloca((size_t)(end - s) + 1);
2040Sstevel@tonic-gate
2050Sstevel@tonic-gate /*
2060Sstevel@tonic-gate * Copy from the start of the token (p) to the location
2070Sstevel@tonic-gate * backquote (q) to extract the nul-terminated object.
2080Sstevel@tonic-gate */
2090Sstevel@tonic-gate bcopy(p, object, (size_t)(q - p));
2100Sstevel@tonic-gate object[(size_t)(q - p)] = '\0';
2110Sstevel@tonic-gate
2120Sstevel@tonic-gate /*
2130Sstevel@tonic-gate * Copy the original string up to the start of this
2140Sstevel@tonic-gate * token (p) into type, and then concatenate everything
2150Sstevel@tonic-gate * after q. This is the type name without the object.
2160Sstevel@tonic-gate */
2170Sstevel@tonic-gate bcopy(s, type, (size_t)(p - s));
2180Sstevel@tonic-gate bcopy(q + 1, type + (size_t)(p - s), strlen(q + 1) + 1);
2190Sstevel@tonic-gate
2200Sstevel@tonic-gate if (strchr(q + 1, '`') != NULL)
2210Sstevel@tonic-gate return (dt_set_errno(dtp, EDT_BADSCOPE));
2220Sstevel@tonic-gate
2230Sstevel@tonic-gate return (dtrace_lookup_by_type(dtp, object, type, tip));
2240Sstevel@tonic-gate }
2250Sstevel@tonic-gate }
2260Sstevel@tonic-gate
2270Sstevel@tonic-gate if (yypcb->pcb_idepth != 0)
2280Sstevel@tonic-gate obj = DTRACE_OBJ_CDEFS;
2290Sstevel@tonic-gate else
2300Sstevel@tonic-gate obj = DTRACE_OBJ_EVERY;
2310Sstevel@tonic-gate
2320Sstevel@tonic-gate return (dtrace_lookup_by_type(dtp, obj, s, tip));
2330Sstevel@tonic-gate }
2340Sstevel@tonic-gate
2350Sstevel@tonic-gate /*
2360Sstevel@tonic-gate * When we parse type expressions or parse an expression with unary "&", we
2370Sstevel@tonic-gate * need to find a type that is a pointer to a previously known type.
2380Sstevel@tonic-gate * Unfortunately CTF is limited to a per-container view, so ctf_type_pointer()
2390Sstevel@tonic-gate * alone does not suffice for our needs. We provide a more intelligent wrapper
2400Sstevel@tonic-gate * for the compiler that attempts to compute a pointer to either the given type
2410Sstevel@tonic-gate * or its base (that is, we try both "foo_t *" and "struct foo *"), and also
2420Sstevel@tonic-gate * to potentially construct the required type on-the-fly.
2430Sstevel@tonic-gate */
2440Sstevel@tonic-gate int
dt_type_pointer(dtrace_typeinfo_t * tip)2450Sstevel@tonic-gate dt_type_pointer(dtrace_typeinfo_t *tip)
2460Sstevel@tonic-gate {
2470Sstevel@tonic-gate dtrace_hdl_t *dtp = yypcb->pcb_hdl;
2480Sstevel@tonic-gate ctf_file_t *ctfp = tip->dtt_ctfp;
2490Sstevel@tonic-gate ctf_id_t type = tip->dtt_type;
2500Sstevel@tonic-gate ctf_id_t base = ctf_type_resolve(ctfp, type);
2510Sstevel@tonic-gate
2520Sstevel@tonic-gate dt_module_t *dmp;
2530Sstevel@tonic-gate ctf_id_t ptr;
2540Sstevel@tonic-gate
2550Sstevel@tonic-gate if ((ptr = ctf_type_pointer(ctfp, type)) != CTF_ERR ||
2560Sstevel@tonic-gate (ptr = ctf_type_pointer(ctfp, base)) != CTF_ERR) {
2570Sstevel@tonic-gate tip->dtt_type = ptr;
2580Sstevel@tonic-gate return (0);
2590Sstevel@tonic-gate }
2600Sstevel@tonic-gate
2610Sstevel@tonic-gate if (yypcb->pcb_idepth != 0)
2620Sstevel@tonic-gate dmp = dtp->dt_cdefs;
2630Sstevel@tonic-gate else
2640Sstevel@tonic-gate dmp = dtp->dt_ddefs;
2650Sstevel@tonic-gate
2660Sstevel@tonic-gate if (ctfp != dmp->dm_ctfp && ctfp != ctf_parent_file(dmp->dm_ctfp) &&
2670Sstevel@tonic-gate (type = ctf_add_type(dmp->dm_ctfp, ctfp, type)) == CTF_ERR) {
2680Sstevel@tonic-gate dtp->dt_ctferr = ctf_errno(dmp->dm_ctfp);
2690Sstevel@tonic-gate return (dt_set_errno(dtp, EDT_CTF));
2700Sstevel@tonic-gate }
2710Sstevel@tonic-gate
2720Sstevel@tonic-gate ptr = ctf_add_pointer(dmp->dm_ctfp, CTF_ADD_ROOT, type);
2730Sstevel@tonic-gate
2740Sstevel@tonic-gate if (ptr == CTF_ERR || ctf_update(dmp->dm_ctfp) == CTF_ERR) {
2750Sstevel@tonic-gate dtp->dt_ctferr = ctf_errno(dmp->dm_ctfp);
2760Sstevel@tonic-gate return (dt_set_errno(dtp, EDT_CTF));
2770Sstevel@tonic-gate }
2780Sstevel@tonic-gate
2790Sstevel@tonic-gate tip->dtt_object = dmp->dm_name;
2800Sstevel@tonic-gate tip->dtt_ctfp = dmp->dm_ctfp;
2810Sstevel@tonic-gate tip->dtt_type = ptr;
2820Sstevel@tonic-gate
2830Sstevel@tonic-gate return (0);
2840Sstevel@tonic-gate }
2850Sstevel@tonic-gate
2860Sstevel@tonic-gate const char *
dt_type_name(ctf_file_t * ctfp,ctf_id_t type,char * buf,size_t len)2870Sstevel@tonic-gate dt_type_name(ctf_file_t *ctfp, ctf_id_t type, char *buf, size_t len)
2880Sstevel@tonic-gate {
2890Sstevel@tonic-gate dtrace_hdl_t *dtp = yypcb->pcb_hdl;
2900Sstevel@tonic-gate
2910Sstevel@tonic-gate if (ctfp == DT_FPTR_CTFP(dtp) && type == DT_FPTR_TYPE(dtp))
2920Sstevel@tonic-gate (void) snprintf(buf, len, "function pointer");
2930Sstevel@tonic-gate else if (ctfp == DT_FUNC_CTFP(dtp) && type == DT_FUNC_TYPE(dtp))
2940Sstevel@tonic-gate (void) snprintf(buf, len, "function");
2950Sstevel@tonic-gate else if (ctfp == DT_DYN_CTFP(dtp) && type == DT_DYN_TYPE(dtp))
2960Sstevel@tonic-gate (void) snprintf(buf, len, "dynamic variable");
2970Sstevel@tonic-gate else if (ctfp == NULL)
2980Sstevel@tonic-gate (void) snprintf(buf, len, "<none>");
2990Sstevel@tonic-gate else if (ctf_type_name(ctfp, type, buf, len) == NULL)
3000Sstevel@tonic-gate (void) snprintf(buf, len, "unknown");
3010Sstevel@tonic-gate
3020Sstevel@tonic-gate return (buf);
3030Sstevel@tonic-gate }
3040Sstevel@tonic-gate
3050Sstevel@tonic-gate /*
3060Sstevel@tonic-gate * Perform the "usual arithmetic conversions" to determine which of the two
3070Sstevel@tonic-gate * input operand types should be promoted and used as a result type. The
3080Sstevel@tonic-gate * rules for this are described in ISOC[6.3.1.8] and K&R[A6.5].
3090Sstevel@tonic-gate */
3100Sstevel@tonic-gate static void
dt_type_promote(dt_node_t * lp,dt_node_t * rp,ctf_file_t ** ofp,ctf_id_t * otype)3110Sstevel@tonic-gate dt_type_promote(dt_node_t *lp, dt_node_t *rp, ctf_file_t **ofp, ctf_id_t *otype)
3120Sstevel@tonic-gate {
3130Sstevel@tonic-gate ctf_file_t *lfp = lp->dn_ctfp;
3140Sstevel@tonic-gate ctf_id_t ltype = lp->dn_type;
3150Sstevel@tonic-gate
3160Sstevel@tonic-gate ctf_file_t *rfp = rp->dn_ctfp;
3170Sstevel@tonic-gate ctf_id_t rtype = rp->dn_type;
3180Sstevel@tonic-gate
3190Sstevel@tonic-gate ctf_id_t lbase = ctf_type_resolve(lfp, ltype);
3200Sstevel@tonic-gate uint_t lkind = ctf_type_kind(lfp, lbase);
3210Sstevel@tonic-gate
3220Sstevel@tonic-gate ctf_id_t rbase = ctf_type_resolve(rfp, rtype);
3230Sstevel@tonic-gate uint_t rkind = ctf_type_kind(rfp, rbase);
3240Sstevel@tonic-gate
3250Sstevel@tonic-gate dtrace_hdl_t *dtp = yypcb->pcb_hdl;
3260Sstevel@tonic-gate ctf_encoding_t le, re;
3270Sstevel@tonic-gate uint_t lrank, rrank;
3280Sstevel@tonic-gate
3290Sstevel@tonic-gate assert(lkind == CTF_K_INTEGER || lkind == CTF_K_ENUM);
3300Sstevel@tonic-gate assert(rkind == CTF_K_INTEGER || rkind == CTF_K_ENUM);
3310Sstevel@tonic-gate
3320Sstevel@tonic-gate if (lkind == CTF_K_ENUM) {
3330Sstevel@tonic-gate lfp = DT_INT_CTFP(dtp);
3340Sstevel@tonic-gate ltype = lbase = DT_INT_TYPE(dtp);
3350Sstevel@tonic-gate }
3360Sstevel@tonic-gate
3370Sstevel@tonic-gate if (rkind == CTF_K_ENUM) {
3380Sstevel@tonic-gate rfp = DT_INT_CTFP(dtp);
3390Sstevel@tonic-gate rtype = rbase = DT_INT_TYPE(dtp);
3400Sstevel@tonic-gate }
3410Sstevel@tonic-gate
3420Sstevel@tonic-gate if (ctf_type_encoding(lfp, lbase, &le) == CTF_ERR) {
3430Sstevel@tonic-gate yypcb->pcb_hdl->dt_ctferr = ctf_errno(lfp);
3440Sstevel@tonic-gate longjmp(yypcb->pcb_jmpbuf, EDT_CTF);
3450Sstevel@tonic-gate }
3460Sstevel@tonic-gate
3470Sstevel@tonic-gate if (ctf_type_encoding(rfp, rbase, &re) == CTF_ERR) {
3480Sstevel@tonic-gate yypcb->pcb_hdl->dt_ctferr = ctf_errno(rfp);
3490Sstevel@tonic-gate longjmp(yypcb->pcb_jmpbuf, EDT_CTF);
3500Sstevel@tonic-gate }
3510Sstevel@tonic-gate
3520Sstevel@tonic-gate /*
3530Sstevel@tonic-gate * Compute an integer rank based on the size and unsigned status.
3540Sstevel@tonic-gate * If rank is identical, pick the "larger" of the equivalent types
3550Sstevel@tonic-gate * which we define as having a larger base ctf_id_t. If rank is
3560Sstevel@tonic-gate * different, pick the type with the greater rank.
3570Sstevel@tonic-gate */
3580Sstevel@tonic-gate lrank = le.cte_bits + ((le.cte_format & CTF_INT_SIGNED) == 0);
3590Sstevel@tonic-gate rrank = re.cte_bits + ((re.cte_format & CTF_INT_SIGNED) == 0);
3600Sstevel@tonic-gate
3610Sstevel@tonic-gate if (lrank == rrank) {
3620Sstevel@tonic-gate if (lbase - rbase < 0)
3630Sstevel@tonic-gate goto return_rtype;
3640Sstevel@tonic-gate else
3650Sstevel@tonic-gate goto return_ltype;
3660Sstevel@tonic-gate } else if (lrank > rrank) {
3670Sstevel@tonic-gate goto return_ltype;
3680Sstevel@tonic-gate } else
3690Sstevel@tonic-gate goto return_rtype;
3700Sstevel@tonic-gate
3710Sstevel@tonic-gate return_ltype:
3720Sstevel@tonic-gate *ofp = lfp;
3730Sstevel@tonic-gate *otype = ltype;
3740Sstevel@tonic-gate return;
3750Sstevel@tonic-gate
3760Sstevel@tonic-gate return_rtype:
3770Sstevel@tonic-gate *ofp = rfp;
3780Sstevel@tonic-gate *otype = rtype;
3790Sstevel@tonic-gate }
3800Sstevel@tonic-gate
3810Sstevel@tonic-gate void
dt_node_promote(dt_node_t * lp,dt_node_t * rp,dt_node_t * dnp)3820Sstevel@tonic-gate dt_node_promote(dt_node_t *lp, dt_node_t *rp, dt_node_t *dnp)
3830Sstevel@tonic-gate {
3840Sstevel@tonic-gate dt_type_promote(lp, rp, &dnp->dn_ctfp, &dnp->dn_type);
3850Sstevel@tonic-gate dt_node_type_assign(dnp, dnp->dn_ctfp, dnp->dn_type);
3860Sstevel@tonic-gate dt_node_attr_assign(dnp, dt_attr_min(lp->dn_attr, rp->dn_attr));
3870Sstevel@tonic-gate }
3880Sstevel@tonic-gate
3890Sstevel@tonic-gate const char *
dt_node_name(const dt_node_t * dnp,char * buf,size_t len)3900Sstevel@tonic-gate dt_node_name(const dt_node_t *dnp, char *buf, size_t len)
3910Sstevel@tonic-gate {
3920Sstevel@tonic-gate char n1[DT_TYPE_NAMELEN];
3930Sstevel@tonic-gate char n2[DT_TYPE_NAMELEN];
3940Sstevel@tonic-gate
3950Sstevel@tonic-gate const char *prefix = "", *suffix = "";
3960Sstevel@tonic-gate const dtrace_syminfo_t *dts;
3970Sstevel@tonic-gate char *s;
3980Sstevel@tonic-gate
3990Sstevel@tonic-gate switch (dnp->dn_kind) {
4000Sstevel@tonic-gate case DT_NODE_INT:
4010Sstevel@tonic-gate (void) snprintf(buf, len, "integer constant 0x%llx",
4020Sstevel@tonic-gate (u_longlong_t)dnp->dn_value);
4030Sstevel@tonic-gate break;
4040Sstevel@tonic-gate case DT_NODE_STRING:
4050Sstevel@tonic-gate s = strchr2esc(dnp->dn_string, strlen(dnp->dn_string));
4060Sstevel@tonic-gate (void) snprintf(buf, len, "string constant \"%s\"",
4070Sstevel@tonic-gate s != NULL ? s : dnp->dn_string);
4080Sstevel@tonic-gate free(s);
4090Sstevel@tonic-gate break;
4100Sstevel@tonic-gate case DT_NODE_IDENT:
4110Sstevel@tonic-gate (void) snprintf(buf, len, "identifier %s", dnp->dn_string);
4120Sstevel@tonic-gate break;
4130Sstevel@tonic-gate case DT_NODE_VAR:
4140Sstevel@tonic-gate case DT_NODE_FUNC:
4150Sstevel@tonic-gate case DT_NODE_AGG:
4160Sstevel@tonic-gate case DT_NODE_INLINE:
4170Sstevel@tonic-gate switch (dnp->dn_ident->di_kind) {
4180Sstevel@tonic-gate case DT_IDENT_FUNC:
4190Sstevel@tonic-gate case DT_IDENT_AGGFUNC:
4200Sstevel@tonic-gate case DT_IDENT_ACTFUNC:
4210Sstevel@tonic-gate suffix = "( )";
4220Sstevel@tonic-gate break;
4230Sstevel@tonic-gate case DT_IDENT_AGG:
4240Sstevel@tonic-gate prefix = "@";
4250Sstevel@tonic-gate break;
4260Sstevel@tonic-gate }
4270Sstevel@tonic-gate (void) snprintf(buf, len, "%s %s%s%s",
4280Sstevel@tonic-gate dt_idkind_name(dnp->dn_ident->di_kind),
4290Sstevel@tonic-gate prefix, dnp->dn_ident->di_name, suffix);
4300Sstevel@tonic-gate break;
4310Sstevel@tonic-gate case DT_NODE_SYM:
4320Sstevel@tonic-gate dts = dnp->dn_ident->di_data;
4330Sstevel@tonic-gate (void) snprintf(buf, len, "symbol %s`%s",
4340Sstevel@tonic-gate dts->dts_object, dts->dts_name);
4350Sstevel@tonic-gate break;
4360Sstevel@tonic-gate case DT_NODE_TYPE:
4370Sstevel@tonic-gate (void) snprintf(buf, len, "type %s",
4380Sstevel@tonic-gate dt_node_type_name(dnp, n1, sizeof (n1)));
4390Sstevel@tonic-gate break;
4400Sstevel@tonic-gate case DT_NODE_OP1:
4410Sstevel@tonic-gate case DT_NODE_OP2:
4420Sstevel@tonic-gate case DT_NODE_OP3:
4430Sstevel@tonic-gate (void) snprintf(buf, len, "operator %s", opstr(dnp->dn_op));
4440Sstevel@tonic-gate break;
4450Sstevel@tonic-gate case DT_NODE_DEXPR:
4460Sstevel@tonic-gate case DT_NODE_DFUNC:
4470Sstevel@tonic-gate if (dnp->dn_expr)
4480Sstevel@tonic-gate return (dt_node_name(dnp->dn_expr, buf, len));
4490Sstevel@tonic-gate (void) snprintf(buf, len, "%s", "statement");
4500Sstevel@tonic-gate break;
4510Sstevel@tonic-gate case DT_NODE_PDESC:
4520Sstevel@tonic-gate if (dnp->dn_desc->dtpd_id == 0) {
4530Sstevel@tonic-gate (void) snprintf(buf, len,
4540Sstevel@tonic-gate "probe description %s:%s:%s:%s",
4550Sstevel@tonic-gate dnp->dn_desc->dtpd_provider, dnp->dn_desc->dtpd_mod,
4560Sstevel@tonic-gate dnp->dn_desc->dtpd_func, dnp->dn_desc->dtpd_name);
4570Sstevel@tonic-gate } else {
4580Sstevel@tonic-gate (void) snprintf(buf, len, "probe description %u",
4590Sstevel@tonic-gate dnp->dn_desc->dtpd_id);
4600Sstevel@tonic-gate }
4610Sstevel@tonic-gate break;
4620Sstevel@tonic-gate case DT_NODE_CLAUSE:
4630Sstevel@tonic-gate (void) snprintf(buf, len, "%s", "clause");
4640Sstevel@tonic-gate break;
4650Sstevel@tonic-gate case DT_NODE_MEMBER:
4660Sstevel@tonic-gate (void) snprintf(buf, len, "member %s", dnp->dn_membname);
4670Sstevel@tonic-gate break;
4680Sstevel@tonic-gate case DT_NODE_XLATOR:
4690Sstevel@tonic-gate (void) snprintf(buf, len, "translator <%s> (%s)",
4700Sstevel@tonic-gate dt_type_name(dnp->dn_xlator->dx_dst_ctfp,
471*13093SRoger.Faulkner@Oracle.COM dnp->dn_xlator->dx_dst_type, n1, sizeof (n1)),
4720Sstevel@tonic-gate dt_type_name(dnp->dn_xlator->dx_src_ctfp,
473*13093SRoger.Faulkner@Oracle.COM dnp->dn_xlator->dx_src_type, n2, sizeof (n2)));
4740Sstevel@tonic-gate break;
4750Sstevel@tonic-gate case DT_NODE_PROG:
4760Sstevel@tonic-gate (void) snprintf(buf, len, "%s", "program");
4770Sstevel@tonic-gate break;
4780Sstevel@tonic-gate default:
4790Sstevel@tonic-gate (void) snprintf(buf, len, "node <%u>", dnp->dn_kind);
4800Sstevel@tonic-gate break;
4810Sstevel@tonic-gate }
4820Sstevel@tonic-gate
4830Sstevel@tonic-gate return (buf);
4840Sstevel@tonic-gate }
4850Sstevel@tonic-gate
4860Sstevel@tonic-gate /*
4870Sstevel@tonic-gate * dt_node_xalloc() can be used to create new parse nodes from any libdtrace
4880Sstevel@tonic-gate * caller. The caller is responsible for assigning dn_link appropriately.
4890Sstevel@tonic-gate */
4900Sstevel@tonic-gate dt_node_t *
dt_node_xalloc(dtrace_hdl_t * dtp,int kind)4910Sstevel@tonic-gate dt_node_xalloc(dtrace_hdl_t *dtp, int kind)
4920Sstevel@tonic-gate {
4930Sstevel@tonic-gate dt_node_t *dnp = dt_alloc(dtp, sizeof (dt_node_t));
4940Sstevel@tonic-gate
4950Sstevel@tonic-gate if (dnp == NULL)
4960Sstevel@tonic-gate return (NULL);
4970Sstevel@tonic-gate
4980Sstevel@tonic-gate dnp->dn_ctfp = NULL;
4990Sstevel@tonic-gate dnp->dn_type = CTF_ERR;
5000Sstevel@tonic-gate dnp->dn_kind = (uchar_t)kind;
5010Sstevel@tonic-gate dnp->dn_flags = 0;
5020Sstevel@tonic-gate dnp->dn_op = 0;
5030Sstevel@tonic-gate dnp->dn_line = -1;
5040Sstevel@tonic-gate dnp->dn_reg = -1;
5050Sstevel@tonic-gate dnp->dn_attr = _dtrace_defattr;
5060Sstevel@tonic-gate dnp->dn_list = NULL;
5070Sstevel@tonic-gate dnp->dn_link = NULL;
5080Sstevel@tonic-gate bzero(&dnp->dn_u, sizeof (dnp->dn_u));
5090Sstevel@tonic-gate
5100Sstevel@tonic-gate return (dnp);
5110Sstevel@tonic-gate }
5120Sstevel@tonic-gate
5130Sstevel@tonic-gate /*
5140Sstevel@tonic-gate * dt_node_alloc() is used to create new parse nodes from the parser. It
5150Sstevel@tonic-gate * assigns the node location based on the current lexer line number and places
5160Sstevel@tonic-gate * the new node on the default allocation list. If allocation fails, we
5170Sstevel@tonic-gate * automatically longjmp the caller back to the enclosing compilation call.
5180Sstevel@tonic-gate */
5190Sstevel@tonic-gate static dt_node_t *
dt_node_alloc(int kind)5200Sstevel@tonic-gate dt_node_alloc(int kind)
5210Sstevel@tonic-gate {
5220Sstevel@tonic-gate dt_node_t *dnp = dt_node_xalloc(yypcb->pcb_hdl, kind);
5230Sstevel@tonic-gate
5240Sstevel@tonic-gate if (dnp == NULL)
5250Sstevel@tonic-gate longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
5260Sstevel@tonic-gate
5270Sstevel@tonic-gate dnp->dn_line = yylineno;
5280Sstevel@tonic-gate dnp->dn_link = yypcb->pcb_list;
5290Sstevel@tonic-gate yypcb->pcb_list = dnp;
5300Sstevel@tonic-gate
5310Sstevel@tonic-gate return (dnp);
5320Sstevel@tonic-gate }
5330Sstevel@tonic-gate
5340Sstevel@tonic-gate void
dt_node_free(dt_node_t * dnp)5350Sstevel@tonic-gate dt_node_free(dt_node_t *dnp)
5360Sstevel@tonic-gate {
5370Sstevel@tonic-gate uchar_t kind = dnp->dn_kind;
5380Sstevel@tonic-gate
5390Sstevel@tonic-gate dnp->dn_kind = DT_NODE_FREE;
5400Sstevel@tonic-gate
5410Sstevel@tonic-gate switch (kind) {
5420Sstevel@tonic-gate case DT_NODE_STRING:
5430Sstevel@tonic-gate case DT_NODE_IDENT:
5440Sstevel@tonic-gate case DT_NODE_TYPE:
5450Sstevel@tonic-gate free(dnp->dn_string);
5460Sstevel@tonic-gate dnp->dn_string = NULL;
5470Sstevel@tonic-gate break;
5480Sstevel@tonic-gate
5490Sstevel@tonic-gate case DT_NODE_VAR:
5500Sstevel@tonic-gate case DT_NODE_FUNC:
5510Sstevel@tonic-gate case DT_NODE_PROBE:
5520Sstevel@tonic-gate if (dnp->dn_ident != NULL) {
5530Sstevel@tonic-gate if (dnp->dn_ident->di_flags & DT_IDFLG_ORPHAN)
5540Sstevel@tonic-gate dt_ident_destroy(dnp->dn_ident);
5550Sstevel@tonic-gate dnp->dn_ident = NULL;
5560Sstevel@tonic-gate }
5570Sstevel@tonic-gate dt_node_list_free(&dnp->dn_args);
5580Sstevel@tonic-gate break;
5590Sstevel@tonic-gate
5600Sstevel@tonic-gate case DT_NODE_OP1:
5610Sstevel@tonic-gate if (dnp->dn_child != NULL) {
5620Sstevel@tonic-gate dt_node_free(dnp->dn_child);
5630Sstevel@tonic-gate dnp->dn_child = NULL;
5640Sstevel@tonic-gate }
5650Sstevel@tonic-gate break;
5660Sstevel@tonic-gate
5670Sstevel@tonic-gate case DT_NODE_OP3:
5680Sstevel@tonic-gate if (dnp->dn_expr != NULL) {
5690Sstevel@tonic-gate dt_node_free(dnp->dn_expr);
5700Sstevel@tonic-gate dnp->dn_expr = NULL;
5710Sstevel@tonic-gate }
5720Sstevel@tonic-gate /*FALLTHRU*/
5730Sstevel@tonic-gate case DT_NODE_OP2:
5740Sstevel@tonic-gate if (dnp->dn_left != NULL) {
5750Sstevel@tonic-gate dt_node_free(dnp->dn_left);
5760Sstevel@tonic-gate dnp->dn_left = NULL;
5770Sstevel@tonic-gate }
5780Sstevel@tonic-gate if (dnp->dn_right != NULL) {
5790Sstevel@tonic-gate dt_node_free(dnp->dn_right);
5800Sstevel@tonic-gate dnp->dn_right = NULL;
5810Sstevel@tonic-gate }
5820Sstevel@tonic-gate break;
5830Sstevel@tonic-gate
5840Sstevel@tonic-gate case DT_NODE_DEXPR:
5850Sstevel@tonic-gate case DT_NODE_DFUNC:
5860Sstevel@tonic-gate if (dnp->dn_expr != NULL) {
5870Sstevel@tonic-gate dt_node_free(dnp->dn_expr);
5880Sstevel@tonic-gate dnp->dn_expr = NULL;
5890Sstevel@tonic-gate }
5900Sstevel@tonic-gate break;
5910Sstevel@tonic-gate
5920Sstevel@tonic-gate case DT_NODE_AGG:
5930Sstevel@tonic-gate if (dnp->dn_aggfun != NULL) {
5940Sstevel@tonic-gate dt_node_free(dnp->dn_aggfun);
5950Sstevel@tonic-gate dnp->dn_aggfun = NULL;
5960Sstevel@tonic-gate }
5970Sstevel@tonic-gate dt_node_list_free(&dnp->dn_aggtup);
5980Sstevel@tonic-gate break;
5990Sstevel@tonic-gate
6000Sstevel@tonic-gate case DT_NODE_PDESC:
6010Sstevel@tonic-gate free(dnp->dn_spec);
6020Sstevel@tonic-gate dnp->dn_spec = NULL;
6030Sstevel@tonic-gate free(dnp->dn_desc);
6040Sstevel@tonic-gate dnp->dn_desc = NULL;
6050Sstevel@tonic-gate break;
6060Sstevel@tonic-gate
6070Sstevel@tonic-gate case DT_NODE_CLAUSE:
6080Sstevel@tonic-gate if (dnp->dn_pred != NULL)
6090Sstevel@tonic-gate dt_node_free(dnp->dn_pred);
6100Sstevel@tonic-gate if (dnp->dn_locals != NULL)
6110Sstevel@tonic-gate dt_idhash_destroy(dnp->dn_locals);
6120Sstevel@tonic-gate dt_node_list_free(&dnp->dn_pdescs);
6130Sstevel@tonic-gate dt_node_list_free(&dnp->dn_acts);
6140Sstevel@tonic-gate break;
6150Sstevel@tonic-gate
6160Sstevel@tonic-gate case DT_NODE_MEMBER:
6170Sstevel@tonic-gate free(dnp->dn_membname);
6180Sstevel@tonic-gate dnp->dn_membname = NULL;
6190Sstevel@tonic-gate if (dnp->dn_membexpr != NULL) {
6200Sstevel@tonic-gate dt_node_free(dnp->dn_membexpr);
6210Sstevel@tonic-gate dnp->dn_membexpr = NULL;
6220Sstevel@tonic-gate }
6230Sstevel@tonic-gate break;
6240Sstevel@tonic-gate
6250Sstevel@tonic-gate case DT_NODE_PROVIDER:
6260Sstevel@tonic-gate dt_node_list_free(&dnp->dn_probes);
6270Sstevel@tonic-gate free(dnp->dn_provname);
6280Sstevel@tonic-gate dnp->dn_provname = NULL;
6290Sstevel@tonic-gate break;
6300Sstevel@tonic-gate
6310Sstevel@tonic-gate case DT_NODE_PROG:
6320Sstevel@tonic-gate dt_node_list_free(&dnp->dn_list);
6330Sstevel@tonic-gate break;
6340Sstevel@tonic-gate }
6350Sstevel@tonic-gate }
6360Sstevel@tonic-gate
6370Sstevel@tonic-gate void
dt_node_attr_assign(dt_node_t * dnp,dtrace_attribute_t attr)6380Sstevel@tonic-gate dt_node_attr_assign(dt_node_t *dnp, dtrace_attribute_t attr)
6390Sstevel@tonic-gate {
6400Sstevel@tonic-gate if ((yypcb->pcb_cflags & DTRACE_C_EATTR) &&
6410Sstevel@tonic-gate (dt_attr_cmp(attr, yypcb->pcb_amin) < 0)) {
6420Sstevel@tonic-gate char a[DTRACE_ATTR2STR_MAX];
6430Sstevel@tonic-gate char s[BUFSIZ];
6440Sstevel@tonic-gate
6450Sstevel@tonic-gate dnerror(dnp, D_ATTR_MIN, "attributes for %s (%s) are less than "
6460Sstevel@tonic-gate "predefined minimum\n", dt_node_name(dnp, s, sizeof (s)),
6470Sstevel@tonic-gate dtrace_attr2str(attr, a, sizeof (a)));
6480Sstevel@tonic-gate }
6490Sstevel@tonic-gate
6500Sstevel@tonic-gate dnp->dn_attr = attr;
6510Sstevel@tonic-gate }
6520Sstevel@tonic-gate
6530Sstevel@tonic-gate void
dt_node_type_assign(dt_node_t * dnp,ctf_file_t * fp,ctf_id_t type)6540Sstevel@tonic-gate dt_node_type_assign(dt_node_t *dnp, ctf_file_t *fp, ctf_id_t type)
6550Sstevel@tonic-gate {
6560Sstevel@tonic-gate ctf_id_t base = ctf_type_resolve(fp, type);
6570Sstevel@tonic-gate uint_t kind = ctf_type_kind(fp, base);
6580Sstevel@tonic-gate ctf_encoding_t e;
6590Sstevel@tonic-gate
6600Sstevel@tonic-gate dnp->dn_flags &=
6610Sstevel@tonic-gate ~(DT_NF_SIGNED | DT_NF_REF | DT_NF_BITFIELD | DT_NF_USERLAND);
6620Sstevel@tonic-gate
6630Sstevel@tonic-gate if (kind == CTF_K_INTEGER && ctf_type_encoding(fp, base, &e) == 0) {
6640Sstevel@tonic-gate size_t size = e.cte_bits / NBBY;
6650Sstevel@tonic-gate
6660Sstevel@tonic-gate if (size > 8 || (e.cte_bits % NBBY) != 0 || (size & (size - 1)))
6670Sstevel@tonic-gate dnp->dn_flags |= DT_NF_BITFIELD;
6680Sstevel@tonic-gate
6690Sstevel@tonic-gate if (e.cte_format & CTF_INT_SIGNED)
6700Sstevel@tonic-gate dnp->dn_flags |= DT_NF_SIGNED;
6710Sstevel@tonic-gate }
6720Sstevel@tonic-gate
6730Sstevel@tonic-gate if (kind == CTF_K_FLOAT && ctf_type_encoding(fp, base, &e) == 0) {
6740Sstevel@tonic-gate if (e.cte_bits / NBBY > sizeof (uint64_t))
6750Sstevel@tonic-gate dnp->dn_flags |= DT_NF_REF;
6760Sstevel@tonic-gate }
6770Sstevel@tonic-gate
6780Sstevel@tonic-gate if (kind == CTF_K_STRUCT || kind == CTF_K_UNION ||
6790Sstevel@tonic-gate kind == CTF_K_FORWARD ||
6800Sstevel@tonic-gate kind == CTF_K_ARRAY || kind == CTF_K_FUNCTION)
6810Sstevel@tonic-gate dnp->dn_flags |= DT_NF_REF;
6820Sstevel@tonic-gate else if (yypcb != NULL && fp == DT_DYN_CTFP(yypcb->pcb_hdl) &&
6830Sstevel@tonic-gate type == DT_DYN_TYPE(yypcb->pcb_hdl))
6840Sstevel@tonic-gate dnp->dn_flags |= DT_NF_REF;
6850Sstevel@tonic-gate
6860Sstevel@tonic-gate dnp->dn_flags |= DT_NF_COOKED;
6870Sstevel@tonic-gate dnp->dn_ctfp = fp;
6880Sstevel@tonic-gate dnp->dn_type = type;
6890Sstevel@tonic-gate }
6900Sstevel@tonic-gate
6910Sstevel@tonic-gate void
dt_node_type_propagate(const dt_node_t * src,dt_node_t * dst)6920Sstevel@tonic-gate dt_node_type_propagate(const dt_node_t *src, dt_node_t *dst)
6930Sstevel@tonic-gate {
6940Sstevel@tonic-gate assert(src->dn_flags & DT_NF_COOKED);
6950Sstevel@tonic-gate dst->dn_flags = src->dn_flags & ~DT_NF_LVALUE;
6960Sstevel@tonic-gate dst->dn_ctfp = src->dn_ctfp;
6970Sstevel@tonic-gate dst->dn_type = src->dn_type;
6980Sstevel@tonic-gate }
6990Sstevel@tonic-gate
7000Sstevel@tonic-gate const char *
dt_node_type_name(const dt_node_t * dnp,char * buf,size_t len)7010Sstevel@tonic-gate dt_node_type_name(const dt_node_t *dnp, char *buf, size_t len)
7020Sstevel@tonic-gate {
7030Sstevel@tonic-gate if (dt_node_is_dynamic(dnp) && dnp->dn_ident != NULL) {
7040Sstevel@tonic-gate (void) snprintf(buf, len, "%s",
7050Sstevel@tonic-gate dt_idkind_name(dt_ident_resolve(dnp->dn_ident)->di_kind));
7060Sstevel@tonic-gate return (buf);
7070Sstevel@tonic-gate }
7080Sstevel@tonic-gate
7090Sstevel@tonic-gate if (dnp->dn_flags & DT_NF_USERLAND) {
7100Sstevel@tonic-gate size_t n = snprintf(buf, len, "userland ");
7110Sstevel@tonic-gate len = len > n ? len - n : 0;
7120Sstevel@tonic-gate (void) dt_type_name(dnp->dn_ctfp, dnp->dn_type, buf + n, len);
7130Sstevel@tonic-gate return (buf);
7140Sstevel@tonic-gate }
7150Sstevel@tonic-gate
7160Sstevel@tonic-gate return (dt_type_name(dnp->dn_ctfp, dnp->dn_type, buf, len));
7170Sstevel@tonic-gate }
7180Sstevel@tonic-gate
7190Sstevel@tonic-gate size_t
dt_node_type_size(const dt_node_t * dnp)7200Sstevel@tonic-gate dt_node_type_size(const dt_node_t *dnp)
7210Sstevel@tonic-gate {
7220Sstevel@tonic-gate if (dnp->dn_kind == DT_NODE_STRING)
7230Sstevel@tonic-gate return (strlen(dnp->dn_string) + 1);
7240Sstevel@tonic-gate
7250Sstevel@tonic-gate if (dt_node_is_dynamic(dnp) && dnp->dn_ident != NULL)
7260Sstevel@tonic-gate return (dt_ident_size(dnp->dn_ident));
7270Sstevel@tonic-gate
7280Sstevel@tonic-gate return (ctf_type_size(dnp->dn_ctfp, dnp->dn_type));
7290Sstevel@tonic-gate }
7300Sstevel@tonic-gate
7310Sstevel@tonic-gate /*
7320Sstevel@tonic-gate * Determine if the specified parse tree node references an identifier of the
7330Sstevel@tonic-gate * specified kind, and if so return a pointer to it; otherwise return NULL.
7340Sstevel@tonic-gate * This function resolves the identifier itself, following through any inlines.
7350Sstevel@tonic-gate */
7360Sstevel@tonic-gate dt_ident_t *
dt_node_resolve(const dt_node_t * dnp,uint_t idkind)7370Sstevel@tonic-gate dt_node_resolve(const dt_node_t *dnp, uint_t idkind)
7380Sstevel@tonic-gate {
7390Sstevel@tonic-gate dt_ident_t *idp;
7400Sstevel@tonic-gate
7410Sstevel@tonic-gate switch (dnp->dn_kind) {
7420Sstevel@tonic-gate case DT_NODE_VAR:
7430Sstevel@tonic-gate case DT_NODE_SYM:
7440Sstevel@tonic-gate case DT_NODE_FUNC:
7450Sstevel@tonic-gate case DT_NODE_AGG:
7460Sstevel@tonic-gate case DT_NODE_INLINE:
7470Sstevel@tonic-gate case DT_NODE_PROBE:
7480Sstevel@tonic-gate idp = dt_ident_resolve(dnp->dn_ident);
7490Sstevel@tonic-gate return (idp->di_kind == idkind ? idp : NULL);
7500Sstevel@tonic-gate }
7510Sstevel@tonic-gate
7520Sstevel@tonic-gate if (dt_node_is_dynamic(dnp)) {
7530Sstevel@tonic-gate idp = dt_ident_resolve(dnp->dn_ident);
7540Sstevel@tonic-gate return (idp->di_kind == idkind ? idp : NULL);
7550Sstevel@tonic-gate }
7560Sstevel@tonic-gate
7570Sstevel@tonic-gate return (NULL);
7580Sstevel@tonic-gate }
7590Sstevel@tonic-gate
7600Sstevel@tonic-gate size_t
dt_node_sizeof(const dt_node_t * dnp)7610Sstevel@tonic-gate dt_node_sizeof(const dt_node_t *dnp)
7620Sstevel@tonic-gate {
7630Sstevel@tonic-gate dtrace_syminfo_t *sip;
7640Sstevel@tonic-gate GElf_Sym sym;
7650Sstevel@tonic-gate dtrace_hdl_t *dtp = yypcb->pcb_hdl;
7660Sstevel@tonic-gate
7670Sstevel@tonic-gate /*
7680Sstevel@tonic-gate * The size of the node as used for the sizeof() operator depends on
7690Sstevel@tonic-gate * the kind of the node. If the node is a SYM, the size is obtained
7700Sstevel@tonic-gate * from the symbol table; if it is not a SYM, the size is determined
7710Sstevel@tonic-gate * from the node's type. This is slightly different from C's sizeof()
7720Sstevel@tonic-gate * operator in that (for example) when applied to a function, sizeof()
7730Sstevel@tonic-gate * will evaluate to the length of the function rather than the size of
7740Sstevel@tonic-gate * the function type.
7750Sstevel@tonic-gate */
7760Sstevel@tonic-gate if (dnp->dn_kind != DT_NODE_SYM)
7770Sstevel@tonic-gate return (dt_node_type_size(dnp));
7780Sstevel@tonic-gate
7790Sstevel@tonic-gate sip = dnp->dn_ident->di_data;
7800Sstevel@tonic-gate
7810Sstevel@tonic-gate if (dtrace_lookup_by_name(dtp, sip->dts_object,
7820Sstevel@tonic-gate sip->dts_name, &sym, NULL) == -1)
7830Sstevel@tonic-gate return (0);
7840Sstevel@tonic-gate
7850Sstevel@tonic-gate return (sym.st_size);
7860Sstevel@tonic-gate }
7870Sstevel@tonic-gate
7880Sstevel@tonic-gate int
dt_node_is_integer(const dt_node_t * dnp)7890Sstevel@tonic-gate dt_node_is_integer(const dt_node_t *dnp)
7900Sstevel@tonic-gate {
7910Sstevel@tonic-gate ctf_file_t *fp = dnp->dn_ctfp;
7920Sstevel@tonic-gate ctf_encoding_t e;
7930Sstevel@tonic-gate ctf_id_t type;
7940Sstevel@tonic-gate uint_t kind;
7950Sstevel@tonic-gate
7960Sstevel@tonic-gate assert(dnp->dn_flags & DT_NF_COOKED);
7970Sstevel@tonic-gate
7980Sstevel@tonic-gate type = ctf_type_resolve(fp, dnp->dn_type);
7990Sstevel@tonic-gate kind = ctf_type_kind(fp, type);
8000Sstevel@tonic-gate
8010Sstevel@tonic-gate if (kind == CTF_K_INTEGER &&
8020Sstevel@tonic-gate ctf_type_encoding(fp, type, &e) == 0 && IS_VOID(e))
8030Sstevel@tonic-gate return (0); /* void integer */
8040Sstevel@tonic-gate
8050Sstevel@tonic-gate return (kind == CTF_K_INTEGER || kind == CTF_K_ENUM);
8060Sstevel@tonic-gate }
8070Sstevel@tonic-gate
8080Sstevel@tonic-gate int
dt_node_is_float(const dt_node_t * dnp)8090Sstevel@tonic-gate dt_node_is_float(const dt_node_t *dnp)
8100Sstevel@tonic-gate {
8110Sstevel@tonic-gate ctf_file_t *fp = dnp->dn_ctfp;
8120Sstevel@tonic-gate ctf_encoding_t e;
8130Sstevel@tonic-gate ctf_id_t type;
8140Sstevel@tonic-gate uint_t kind;
8150Sstevel@tonic-gate
8160Sstevel@tonic-gate assert(dnp->dn_flags & DT_NF_COOKED);
8170Sstevel@tonic-gate
8180Sstevel@tonic-gate type = ctf_type_resolve(fp, dnp->dn_type);
8190Sstevel@tonic-gate kind = ctf_type_kind(fp, type);
8200Sstevel@tonic-gate
8210Sstevel@tonic-gate return (kind == CTF_K_FLOAT &&
8220Sstevel@tonic-gate ctf_type_encoding(dnp->dn_ctfp, type, &e) == 0 && (
8230Sstevel@tonic-gate e.cte_format == CTF_FP_SINGLE || e.cte_format == CTF_FP_DOUBLE ||
8240Sstevel@tonic-gate e.cte_format == CTF_FP_LDOUBLE));
8250Sstevel@tonic-gate }
8260Sstevel@tonic-gate
8270Sstevel@tonic-gate int
dt_node_is_scalar(const dt_node_t * dnp)8280Sstevel@tonic-gate dt_node_is_scalar(const dt_node_t *dnp)
8290Sstevel@tonic-gate {
8300Sstevel@tonic-gate ctf_file_t *fp = dnp->dn_ctfp;
8310Sstevel@tonic-gate ctf_encoding_t e;
8320Sstevel@tonic-gate ctf_id_t type;
8330Sstevel@tonic-gate uint_t kind;
8340Sstevel@tonic-gate
8350Sstevel@tonic-gate assert(dnp->dn_flags & DT_NF_COOKED);
8360Sstevel@tonic-gate
8370Sstevel@tonic-gate type = ctf_type_resolve(fp, dnp->dn_type);
8380Sstevel@tonic-gate kind = ctf_type_kind(fp, type);
8390Sstevel@tonic-gate
8400Sstevel@tonic-gate if (kind == CTF_K_INTEGER &&
8410Sstevel@tonic-gate ctf_type_encoding(fp, type, &e) == 0 && IS_VOID(e))
8420Sstevel@tonic-gate return (0); /* void cannot be used as a scalar */
8430Sstevel@tonic-gate
8440Sstevel@tonic-gate return (kind == CTF_K_INTEGER || kind == CTF_K_ENUM ||
8450Sstevel@tonic-gate kind == CTF_K_POINTER);
8460Sstevel@tonic-gate }
8470Sstevel@tonic-gate
8480Sstevel@tonic-gate int
dt_node_is_arith(const dt_node_t * dnp)8490Sstevel@tonic-gate dt_node_is_arith(const dt_node_t *dnp)
8500Sstevel@tonic-gate {
8510Sstevel@tonic-gate ctf_file_t *fp = dnp->dn_ctfp;
8520Sstevel@tonic-gate ctf_encoding_t e;
8530Sstevel@tonic-gate ctf_id_t type;
8540Sstevel@tonic-gate uint_t kind;
8550Sstevel@tonic-gate
8560Sstevel@tonic-gate assert(dnp->dn_flags & DT_NF_COOKED);
8570Sstevel@tonic-gate
8580Sstevel@tonic-gate type = ctf_type_resolve(fp, dnp->dn_type);
8590Sstevel@tonic-gate kind = ctf_type_kind(fp, type);
8600Sstevel@tonic-gate
8610Sstevel@tonic-gate if (kind == CTF_K_INTEGER)
8620Sstevel@tonic-gate return (ctf_type_encoding(fp, type, &e) == 0 && !IS_VOID(e));
8630Sstevel@tonic-gate else
8640Sstevel@tonic-gate return (kind == CTF_K_ENUM);
8650Sstevel@tonic-gate }
8660Sstevel@tonic-gate
8670Sstevel@tonic-gate int
dt_node_is_vfptr(const dt_node_t * dnp)8680Sstevel@tonic-gate dt_node_is_vfptr(const dt_node_t *dnp)
8690Sstevel@tonic-gate {
8700Sstevel@tonic-gate ctf_file_t *fp = dnp->dn_ctfp;
8710Sstevel@tonic-gate ctf_encoding_t e;
8720Sstevel@tonic-gate ctf_id_t type;
8730Sstevel@tonic-gate uint_t kind;
8740Sstevel@tonic-gate
8750Sstevel@tonic-gate assert(dnp->dn_flags & DT_NF_COOKED);
8760Sstevel@tonic-gate
8770Sstevel@tonic-gate type = ctf_type_resolve(fp, dnp->dn_type);
8780Sstevel@tonic-gate if (ctf_type_kind(fp, type) != CTF_K_POINTER)
8790Sstevel@tonic-gate return (0); /* type is not a pointer */
8800Sstevel@tonic-gate
8810Sstevel@tonic-gate type = ctf_type_resolve(fp, ctf_type_reference(fp, type));
8820Sstevel@tonic-gate kind = ctf_type_kind(fp, type);
8830Sstevel@tonic-gate
8840Sstevel@tonic-gate return (kind == CTF_K_FUNCTION || (kind == CTF_K_INTEGER &&
8850Sstevel@tonic-gate ctf_type_encoding(fp, type, &e) == 0 && IS_VOID(e)));
8860Sstevel@tonic-gate }
8870Sstevel@tonic-gate
8880Sstevel@tonic-gate int
dt_node_is_dynamic(const dt_node_t * dnp)8890Sstevel@tonic-gate dt_node_is_dynamic(const dt_node_t *dnp)
8900Sstevel@tonic-gate {
8910Sstevel@tonic-gate if (dnp->dn_kind == DT_NODE_VAR &&
8920Sstevel@tonic-gate (dnp->dn_ident->di_flags & DT_IDFLG_INLINE)) {
8930Sstevel@tonic-gate const dt_idnode_t *inp = dnp->dn_ident->di_iarg;
894265Smws return (inp->din_root ? dt_node_is_dynamic(inp->din_root) : 0);
8950Sstevel@tonic-gate }
8960Sstevel@tonic-gate
8970Sstevel@tonic-gate return (dnp->dn_ctfp == DT_DYN_CTFP(yypcb->pcb_hdl) &&
8980Sstevel@tonic-gate dnp->dn_type == DT_DYN_TYPE(yypcb->pcb_hdl));
8990Sstevel@tonic-gate }
9000Sstevel@tonic-gate
9010Sstevel@tonic-gate int
dt_node_is_string(const dt_node_t * dnp)9020Sstevel@tonic-gate dt_node_is_string(const dt_node_t *dnp)
9030Sstevel@tonic-gate {
9040Sstevel@tonic-gate return (dnp->dn_ctfp == DT_STR_CTFP(yypcb->pcb_hdl) &&
9050Sstevel@tonic-gate dnp->dn_type == DT_STR_TYPE(yypcb->pcb_hdl));
9060Sstevel@tonic-gate }
9070Sstevel@tonic-gate
9080Sstevel@tonic-gate int
dt_node_is_stack(const dt_node_t * dnp)9090Sstevel@tonic-gate dt_node_is_stack(const dt_node_t *dnp)
9100Sstevel@tonic-gate {
9110Sstevel@tonic-gate return (dnp->dn_ctfp == DT_STACK_CTFP(yypcb->pcb_hdl) &&
9120Sstevel@tonic-gate dnp->dn_type == DT_STACK_TYPE(yypcb->pcb_hdl));
9130Sstevel@tonic-gate }
9140Sstevel@tonic-gate
9150Sstevel@tonic-gate int
dt_node_is_symaddr(const dt_node_t * dnp)916457Sbmc dt_node_is_symaddr(const dt_node_t *dnp)
917457Sbmc {
918457Sbmc return (dnp->dn_ctfp == DT_SYMADDR_CTFP(yypcb->pcb_hdl) &&
919457Sbmc dnp->dn_type == DT_SYMADDR_TYPE(yypcb->pcb_hdl));
920457Sbmc }
921457Sbmc
922457Sbmc int
dt_node_is_usymaddr(const dt_node_t * dnp)923457Sbmc dt_node_is_usymaddr(const dt_node_t *dnp)
924457Sbmc {
925457Sbmc return (dnp->dn_ctfp == DT_USYMADDR_CTFP(yypcb->pcb_hdl) &&
926457Sbmc dnp->dn_type == DT_USYMADDR_TYPE(yypcb->pcb_hdl));
927457Sbmc }
928457Sbmc
929457Sbmc int
dt_node_is_strcompat(const dt_node_t * dnp)9300Sstevel@tonic-gate dt_node_is_strcompat(const dt_node_t *dnp)
9310Sstevel@tonic-gate {
9320Sstevel@tonic-gate ctf_file_t *fp = dnp->dn_ctfp;
9330Sstevel@tonic-gate ctf_encoding_t e;
9340Sstevel@tonic-gate ctf_arinfo_t r;
9350Sstevel@tonic-gate ctf_id_t base;
9360Sstevel@tonic-gate uint_t kind;
9370Sstevel@tonic-gate
9380Sstevel@tonic-gate assert(dnp->dn_flags & DT_NF_COOKED);
9390Sstevel@tonic-gate
9400Sstevel@tonic-gate base = ctf_type_resolve(fp, dnp->dn_type);
9410Sstevel@tonic-gate kind = ctf_type_kind(fp, base);
9420Sstevel@tonic-gate
9430Sstevel@tonic-gate if (kind == CTF_K_POINTER &&
9440Sstevel@tonic-gate (base = ctf_type_reference(fp, base)) != CTF_ERR &&
9450Sstevel@tonic-gate (base = ctf_type_resolve(fp, base)) != CTF_ERR &&
9460Sstevel@tonic-gate ctf_type_encoding(fp, base, &e) == 0 && IS_CHAR(e))
9470Sstevel@tonic-gate return (1); /* promote char pointer to string */
9480Sstevel@tonic-gate
9490Sstevel@tonic-gate if (kind == CTF_K_ARRAY && ctf_array_info(fp, base, &r) == 0 &&
9500Sstevel@tonic-gate (base = ctf_type_resolve(fp, r.ctr_contents)) != CTF_ERR &&
9510Sstevel@tonic-gate ctf_type_encoding(fp, base, &e) == 0 && IS_CHAR(e))
9520Sstevel@tonic-gate return (1); /* promote char array to string */
9530Sstevel@tonic-gate
9540Sstevel@tonic-gate return (0);
9550Sstevel@tonic-gate }
9560Sstevel@tonic-gate
9570Sstevel@tonic-gate int
dt_node_is_pointer(const dt_node_t * dnp)9580Sstevel@tonic-gate dt_node_is_pointer(const dt_node_t *dnp)
9590Sstevel@tonic-gate {
9600Sstevel@tonic-gate ctf_file_t *fp = dnp->dn_ctfp;
9610Sstevel@tonic-gate uint_t kind;
9620Sstevel@tonic-gate
9630Sstevel@tonic-gate assert(dnp->dn_flags & DT_NF_COOKED);
9640Sstevel@tonic-gate
9650Sstevel@tonic-gate if (dt_node_is_string(dnp))
9660Sstevel@tonic-gate return (0); /* string are pass-by-ref but act like structs */
9670Sstevel@tonic-gate
9680Sstevel@tonic-gate kind = ctf_type_kind(fp, ctf_type_resolve(fp, dnp->dn_type));
9690Sstevel@tonic-gate return (kind == CTF_K_POINTER || kind == CTF_K_ARRAY);
9700Sstevel@tonic-gate }
9710Sstevel@tonic-gate
9720Sstevel@tonic-gate int
dt_node_is_void(const dt_node_t * dnp)9730Sstevel@tonic-gate dt_node_is_void(const dt_node_t *dnp)
9740Sstevel@tonic-gate {
9750Sstevel@tonic-gate ctf_file_t *fp = dnp->dn_ctfp;
9760Sstevel@tonic-gate ctf_encoding_t e;
9770Sstevel@tonic-gate ctf_id_t type;
9780Sstevel@tonic-gate
9790Sstevel@tonic-gate if (dt_node_is_dynamic(dnp))
9800Sstevel@tonic-gate return (0); /* <DYN> is an alias for void but not the same */
9810Sstevel@tonic-gate
9820Sstevel@tonic-gate if (dt_node_is_stack(dnp))
9830Sstevel@tonic-gate return (0);
9840Sstevel@tonic-gate
985457Sbmc if (dt_node_is_symaddr(dnp) || dt_node_is_usymaddr(dnp))
986457Sbmc return (0);
987457Sbmc
9880Sstevel@tonic-gate type = ctf_type_resolve(fp, dnp->dn_type);
9890Sstevel@tonic-gate
9900Sstevel@tonic-gate return (ctf_type_kind(fp, type) == CTF_K_INTEGER &&
9910Sstevel@tonic-gate ctf_type_encoding(fp, type, &e) == 0 && IS_VOID(e));
9920Sstevel@tonic-gate }
9930Sstevel@tonic-gate
9940Sstevel@tonic-gate int
dt_node_is_ptrcompat(const dt_node_t * lp,const dt_node_t * rp,ctf_file_t ** fpp,ctf_id_t * tp)9950Sstevel@tonic-gate dt_node_is_ptrcompat(const dt_node_t *lp, const dt_node_t *rp,
9960Sstevel@tonic-gate ctf_file_t **fpp, ctf_id_t *tp)
9970Sstevel@tonic-gate {
9980Sstevel@tonic-gate ctf_file_t *lfp = lp->dn_ctfp;
9990Sstevel@tonic-gate ctf_file_t *rfp = rp->dn_ctfp;
10000Sstevel@tonic-gate
10010Sstevel@tonic-gate ctf_id_t lbase = CTF_ERR, rbase = CTF_ERR;
10020Sstevel@tonic-gate ctf_id_t lref = CTF_ERR, rref = CTF_ERR;
10030Sstevel@tonic-gate
10040Sstevel@tonic-gate int lp_is_void, rp_is_void, lp_is_int, rp_is_int, compat;
10050Sstevel@tonic-gate uint_t lkind, rkind;
10060Sstevel@tonic-gate ctf_encoding_t e;
10070Sstevel@tonic-gate ctf_arinfo_t r;
10080Sstevel@tonic-gate
10090Sstevel@tonic-gate assert(lp->dn_flags & DT_NF_COOKED);
10100Sstevel@tonic-gate assert(rp->dn_flags & DT_NF_COOKED);
10110Sstevel@tonic-gate
10120Sstevel@tonic-gate if (dt_node_is_dynamic(lp) || dt_node_is_dynamic(rp))
10130Sstevel@tonic-gate return (0); /* fail if either node is a dynamic variable */
10140Sstevel@tonic-gate
10150Sstevel@tonic-gate lp_is_int = dt_node_is_integer(lp);
10160Sstevel@tonic-gate rp_is_int = dt_node_is_integer(rp);
10170Sstevel@tonic-gate
10180Sstevel@tonic-gate if (lp_is_int && rp_is_int)
10190Sstevel@tonic-gate return (0); /* fail if both nodes are integers */
10200Sstevel@tonic-gate
10210Sstevel@tonic-gate if (lp_is_int && (lp->dn_kind != DT_NODE_INT || lp->dn_value != 0))
10220Sstevel@tonic-gate return (0); /* fail if lp is an integer that isn't 0 constant */
10230Sstevel@tonic-gate
10240Sstevel@tonic-gate if (rp_is_int && (rp->dn_kind != DT_NODE_INT || rp->dn_value != 0))
10250Sstevel@tonic-gate return (0); /* fail if rp is an integer that isn't 0 constant */
10260Sstevel@tonic-gate
10270Sstevel@tonic-gate if ((lp_is_int == 0 && rp_is_int == 0) && (
10280Sstevel@tonic-gate (lp->dn_flags & DT_NF_USERLAND) ^ (rp->dn_flags & DT_NF_USERLAND)))
10290Sstevel@tonic-gate return (0); /* fail if only one pointer is a userland address */
10300Sstevel@tonic-gate
10310Sstevel@tonic-gate /*
10320Sstevel@tonic-gate * Resolve the left-hand and right-hand types to their base type, and
10330Sstevel@tonic-gate * then resolve the referenced type as well (assuming the base type
10340Sstevel@tonic-gate * is CTF_K_POINTER or CTF_K_ARRAY). Otherwise [lr]ref = CTF_ERR.
10350Sstevel@tonic-gate */
10360Sstevel@tonic-gate if (!lp_is_int) {
10370Sstevel@tonic-gate lbase = ctf_type_resolve(lfp, lp->dn_type);
10380Sstevel@tonic-gate lkind = ctf_type_kind(lfp, lbase);
10390Sstevel@tonic-gate
10400Sstevel@tonic-gate if (lkind == CTF_K_POINTER) {
10410Sstevel@tonic-gate lref = ctf_type_resolve(lfp,
10420Sstevel@tonic-gate ctf_type_reference(lfp, lbase));
10430Sstevel@tonic-gate } else if (lkind == CTF_K_ARRAY &&
10440Sstevel@tonic-gate ctf_array_info(lfp, lbase, &r) == 0) {
10450Sstevel@tonic-gate lref = ctf_type_resolve(lfp, r.ctr_contents);
10460Sstevel@tonic-gate }
10470Sstevel@tonic-gate }
10480Sstevel@tonic-gate
10490Sstevel@tonic-gate if (!rp_is_int) {
10500Sstevel@tonic-gate rbase = ctf_type_resolve(rfp, rp->dn_type);
10510Sstevel@tonic-gate rkind = ctf_type_kind(rfp, rbase);
10520Sstevel@tonic-gate
10530Sstevel@tonic-gate if (rkind == CTF_K_POINTER) {
10540Sstevel@tonic-gate rref = ctf_type_resolve(rfp,
10550Sstevel@tonic-gate ctf_type_reference(rfp, rbase));
10560Sstevel@tonic-gate } else if (rkind == CTF_K_ARRAY &&
10570Sstevel@tonic-gate ctf_array_info(rfp, rbase, &r) == 0) {
10580Sstevel@tonic-gate rref = ctf_type_resolve(rfp, r.ctr_contents);
10590Sstevel@tonic-gate }
10600Sstevel@tonic-gate }
10610Sstevel@tonic-gate
10620Sstevel@tonic-gate /*
10630Sstevel@tonic-gate * We know that one or the other type may still be a zero-valued
10640Sstevel@tonic-gate * integer constant. To simplify the code below, set the integer
10650Sstevel@tonic-gate * type variables equal to the non-integer types and proceed.
10660Sstevel@tonic-gate */
10670Sstevel@tonic-gate if (lp_is_int) {
10680Sstevel@tonic-gate lbase = rbase;
10690Sstevel@tonic-gate lkind = rkind;
10700Sstevel@tonic-gate lref = rref;
10710Sstevel@tonic-gate lfp = rfp;
10720Sstevel@tonic-gate } else if (rp_is_int) {
10730Sstevel@tonic-gate rbase = lbase;
10740Sstevel@tonic-gate rkind = lkind;
10750Sstevel@tonic-gate rref = lref;
10760Sstevel@tonic-gate rfp = lfp;
10770Sstevel@tonic-gate }
10780Sstevel@tonic-gate
10790Sstevel@tonic-gate lp_is_void = ctf_type_encoding(lfp, lref, &e) == 0 && IS_VOID(e);
10800Sstevel@tonic-gate rp_is_void = ctf_type_encoding(rfp, rref, &e) == 0 && IS_VOID(e);
10810Sstevel@tonic-gate
10820Sstevel@tonic-gate /*
10830Sstevel@tonic-gate * The types are compatible if both are pointers to the same type, or
10840Sstevel@tonic-gate * if either pointer is a void pointer. If they are compatible, set
10850Sstevel@tonic-gate * tp to point to the more specific pointer type and return it.
10860Sstevel@tonic-gate */
10870Sstevel@tonic-gate compat = (lkind == CTF_K_POINTER || lkind == CTF_K_ARRAY) &&
10880Sstevel@tonic-gate (rkind == CTF_K_POINTER || rkind == CTF_K_ARRAY) &&
10890Sstevel@tonic-gate (lp_is_void || rp_is_void || ctf_type_compat(lfp, lref, rfp, rref));
10900Sstevel@tonic-gate
10910Sstevel@tonic-gate if (compat) {
10920Sstevel@tonic-gate if (fpp != NULL)
10930Sstevel@tonic-gate *fpp = rp_is_void ? lfp : rfp;
10940Sstevel@tonic-gate if (tp != NULL)
10950Sstevel@tonic-gate *tp = rp_is_void ? lbase : rbase;
10960Sstevel@tonic-gate }
10970Sstevel@tonic-gate
10980Sstevel@tonic-gate return (compat);
10990Sstevel@tonic-gate }
11000Sstevel@tonic-gate
11010Sstevel@tonic-gate /*
11020Sstevel@tonic-gate * The rules for checking argument types against parameter types are described
11030Sstevel@tonic-gate * in the ANSI-C spec (see K&R[A7.3.2] and K&R[A7.17]). We use the same rule
11040Sstevel@tonic-gate * set to determine whether associative array arguments match the prototype.
11050Sstevel@tonic-gate */
11060Sstevel@tonic-gate int
dt_node_is_argcompat(const dt_node_t * lp,const dt_node_t * rp)11070Sstevel@tonic-gate dt_node_is_argcompat(const dt_node_t *lp, const dt_node_t *rp)
11080Sstevel@tonic-gate {
11090Sstevel@tonic-gate ctf_file_t *lfp = lp->dn_ctfp;
11100Sstevel@tonic-gate ctf_file_t *rfp = rp->dn_ctfp;
11110Sstevel@tonic-gate
11120Sstevel@tonic-gate assert(lp->dn_flags & DT_NF_COOKED);
11130Sstevel@tonic-gate assert(rp->dn_flags & DT_NF_COOKED);
11140Sstevel@tonic-gate
11150Sstevel@tonic-gate if (dt_node_is_integer(lp) && dt_node_is_integer(rp))
11160Sstevel@tonic-gate return (1); /* integer types are compatible */
11170Sstevel@tonic-gate
11180Sstevel@tonic-gate if (dt_node_is_strcompat(lp) && dt_node_is_strcompat(rp))
11190Sstevel@tonic-gate return (1); /* string types are compatible */
11200Sstevel@tonic-gate
11210Sstevel@tonic-gate if (dt_node_is_stack(lp) && dt_node_is_stack(rp))
11220Sstevel@tonic-gate return (1); /* stack types are compatible */
11230Sstevel@tonic-gate
1124457Sbmc if (dt_node_is_symaddr(lp) && dt_node_is_symaddr(rp))
1125457Sbmc return (1); /* symaddr types are compatible */
1126457Sbmc
1127457Sbmc if (dt_node_is_usymaddr(lp) && dt_node_is_usymaddr(rp))
1128457Sbmc return (1); /* usymaddr types are compatible */
1129457Sbmc
11300Sstevel@tonic-gate switch (ctf_type_kind(lfp, ctf_type_resolve(lfp, lp->dn_type))) {
11310Sstevel@tonic-gate case CTF_K_FUNCTION:
11320Sstevel@tonic-gate case CTF_K_STRUCT:
11330Sstevel@tonic-gate case CTF_K_UNION:
11340Sstevel@tonic-gate return (ctf_type_compat(lfp, lp->dn_type, rfp, rp->dn_type));
11350Sstevel@tonic-gate default:
11360Sstevel@tonic-gate return (dt_node_is_ptrcompat(lp, rp, NULL, NULL));
11370Sstevel@tonic-gate }
11380Sstevel@tonic-gate }
11390Sstevel@tonic-gate
11400Sstevel@tonic-gate /*
11410Sstevel@tonic-gate * We provide dt_node_is_posconst() as a convenience routine for callers who
11420Sstevel@tonic-gate * wish to verify that an argument is a positive non-zero integer constant.
11430Sstevel@tonic-gate */
11440Sstevel@tonic-gate int
dt_node_is_posconst(const dt_node_t * dnp)11450Sstevel@tonic-gate dt_node_is_posconst(const dt_node_t *dnp)
11460Sstevel@tonic-gate {
11470Sstevel@tonic-gate return (dnp->dn_kind == DT_NODE_INT && dnp->dn_value != 0 && (
11480Sstevel@tonic-gate (dnp->dn_flags & DT_NF_SIGNED) == 0 || (int64_t)dnp->dn_value > 0));
11490Sstevel@tonic-gate }
11500Sstevel@tonic-gate
11510Sstevel@tonic-gate int
dt_node_is_actfunc(const dt_node_t * dnp)11520Sstevel@tonic-gate dt_node_is_actfunc(const dt_node_t *dnp)
11530Sstevel@tonic-gate {
11540Sstevel@tonic-gate return (dnp->dn_kind == DT_NODE_FUNC &&
11550Sstevel@tonic-gate dnp->dn_ident->di_kind == DT_IDENT_ACTFUNC);
11560Sstevel@tonic-gate }
11570Sstevel@tonic-gate
11580Sstevel@tonic-gate /*
11590Sstevel@tonic-gate * The original rules for integer constant typing are described in K&R[A2.5.1].
11600Sstevel@tonic-gate * However, since we support long long, we instead use the rules from ISO C99
11610Sstevel@tonic-gate * clause 6.4.4.1 since that is where long longs are formally described. The
11620Sstevel@tonic-gate * rules require us to know whether the constant was specified in decimal or
11630Sstevel@tonic-gate * in octal or hex, which we do by looking at our lexer's 'yyintdecimal' flag.
11640Sstevel@tonic-gate * The type of an integer constant is the first of the corresponding list in
11650Sstevel@tonic-gate * which its value can be represented:
11660Sstevel@tonic-gate *
11670Sstevel@tonic-gate * unsuffixed decimal: int, long, long long
11680Sstevel@tonic-gate * unsuffixed oct/hex: int, unsigned int, long, unsigned long,
11690Sstevel@tonic-gate * long long, unsigned long long
11700Sstevel@tonic-gate * suffix [uU]: unsigned int, unsigned long, unsigned long long
11710Sstevel@tonic-gate * suffix [lL] decimal: long, long long
11720Sstevel@tonic-gate * suffix [lL] oct/hex: long, unsigned long, long long, unsigned long long
11730Sstevel@tonic-gate * suffix [uU][Ll]: unsigned long, unsigned long long
11740Sstevel@tonic-gate * suffix ll/LL decimal: long long
11750Sstevel@tonic-gate * suffix ll/LL oct/hex: long long, unsigned long long
11760Sstevel@tonic-gate * suffix [uU][ll/LL]: unsigned long long
11770Sstevel@tonic-gate *
11780Sstevel@tonic-gate * Given that our lexer has already validated the suffixes by regexp matching,
11790Sstevel@tonic-gate * there is an obvious way to concisely encode these rules: construct an array
11800Sstevel@tonic-gate * of the types in the order int, unsigned int, long, unsigned long, long long,
11810Sstevel@tonic-gate * unsigned long long. Compute an integer array starting index based on the
11820Sstevel@tonic-gate * suffix (e.g. none = 0, u = 1, ull = 5), and compute an increment based on
11830Sstevel@tonic-gate * the specifier (dec/oct/hex) and suffix (u). Then iterate from the starting
11840Sstevel@tonic-gate * index to the end, advancing using the increment, and searching until we
11850Sstevel@tonic-gate * find a limit that matches or we run out of choices (overflow). To make it
11860Sstevel@tonic-gate * even faster, we precompute the table of type information in dtrace_open().
11870Sstevel@tonic-gate */
11880Sstevel@tonic-gate dt_node_t *
dt_node_int(uintmax_t value)11890Sstevel@tonic-gate dt_node_int(uintmax_t value)
11900Sstevel@tonic-gate {
11910Sstevel@tonic-gate dt_node_t *dnp = dt_node_alloc(DT_NODE_INT);
11920Sstevel@tonic-gate dtrace_hdl_t *dtp = yypcb->pcb_hdl;
11930Sstevel@tonic-gate
11940Sstevel@tonic-gate int n = (yyintdecimal | (yyintsuffix[0] == 'u')) + 1;
11950Sstevel@tonic-gate int i = 0;
11960Sstevel@tonic-gate
11970Sstevel@tonic-gate const char *p;
11980Sstevel@tonic-gate char c;
11990Sstevel@tonic-gate
12000Sstevel@tonic-gate dnp->dn_op = DT_TOK_INT;
12010Sstevel@tonic-gate dnp->dn_value = value;
12020Sstevel@tonic-gate
12030Sstevel@tonic-gate for (p = yyintsuffix; (c = *p) != '\0'; p++) {
12040Sstevel@tonic-gate if (c == 'U' || c == 'u')
12050Sstevel@tonic-gate i += 1;
12060Sstevel@tonic-gate else if (c == 'L' || c == 'l')
12070Sstevel@tonic-gate i += 2;
12080Sstevel@tonic-gate }
12090Sstevel@tonic-gate
12100Sstevel@tonic-gate for (; i < sizeof (dtp->dt_ints) / sizeof (dtp->dt_ints[0]); i += n) {
12110Sstevel@tonic-gate if (value <= dtp->dt_ints[i].did_limit) {
12120Sstevel@tonic-gate dt_node_type_assign(dnp,
12130Sstevel@tonic-gate dtp->dt_ints[i].did_ctfp,
12140Sstevel@tonic-gate dtp->dt_ints[i].did_type);
12150Sstevel@tonic-gate
12160Sstevel@tonic-gate /*
12170Sstevel@tonic-gate * If a prefix character is present in macro text, add
12180Sstevel@tonic-gate * in the corresponding operator node (see dt_lex.l).
12190Sstevel@tonic-gate */
12200Sstevel@tonic-gate switch (yyintprefix) {
12210Sstevel@tonic-gate case '+':
12220Sstevel@tonic-gate return (dt_node_op1(DT_TOK_IPOS, dnp));
12230Sstevel@tonic-gate case '-':
12240Sstevel@tonic-gate return (dt_node_op1(DT_TOK_INEG, dnp));
12250Sstevel@tonic-gate default:
12260Sstevel@tonic-gate return (dnp);
12270Sstevel@tonic-gate }
12280Sstevel@tonic-gate }
12290Sstevel@tonic-gate }
12300Sstevel@tonic-gate
12310Sstevel@tonic-gate xyerror(D_INT_OFLOW, "integer constant 0x%llx cannot be represented "
12320Sstevel@tonic-gate "in any built-in integral type\n", (u_longlong_t)value);
12330Sstevel@tonic-gate /*NOTREACHED*/
12340Sstevel@tonic-gate return (NULL); /* keep gcc happy */
12350Sstevel@tonic-gate }
12360Sstevel@tonic-gate
12370Sstevel@tonic-gate dt_node_t *
dt_node_string(char * string)12380Sstevel@tonic-gate dt_node_string(char *string)
12390Sstevel@tonic-gate {
12400Sstevel@tonic-gate dtrace_hdl_t *dtp = yypcb->pcb_hdl;
12410Sstevel@tonic-gate dt_node_t *dnp;
12420Sstevel@tonic-gate
12430Sstevel@tonic-gate if (string == NULL)
12440Sstevel@tonic-gate longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
12450Sstevel@tonic-gate
12460Sstevel@tonic-gate dnp = dt_node_alloc(DT_NODE_STRING);
12470Sstevel@tonic-gate dnp->dn_op = DT_TOK_STRING;
12480Sstevel@tonic-gate dnp->dn_string = string;
12490Sstevel@tonic-gate dt_node_type_assign(dnp, DT_STR_CTFP(dtp), DT_STR_TYPE(dtp));
12500Sstevel@tonic-gate
12510Sstevel@tonic-gate return (dnp);
12520Sstevel@tonic-gate }
12530Sstevel@tonic-gate
12540Sstevel@tonic-gate dt_node_t *
dt_node_ident(char * name)12550Sstevel@tonic-gate dt_node_ident(char *name)
12560Sstevel@tonic-gate {
12570Sstevel@tonic-gate dt_ident_t *idp;
12580Sstevel@tonic-gate dt_node_t *dnp;
12590Sstevel@tonic-gate
12600Sstevel@tonic-gate if (name == NULL)
12610Sstevel@tonic-gate longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
12620Sstevel@tonic-gate
12630Sstevel@tonic-gate /*
12640Sstevel@tonic-gate * If the identifier is an inlined integer constant, then create an INT
12650Sstevel@tonic-gate * node that is a clone of the inline parse tree node and return that
12660Sstevel@tonic-gate * immediately, allowing this inline to be used in parsing contexts
12670Sstevel@tonic-gate * that require constant expressions (e.g. scalar array sizes).
12680Sstevel@tonic-gate */
12690Sstevel@tonic-gate if ((idp = dt_idstack_lookup(&yypcb->pcb_globals, name)) != NULL &&
12700Sstevel@tonic-gate (idp->di_flags & DT_IDFLG_INLINE)) {
12710Sstevel@tonic-gate dt_idnode_t *inp = idp->di_iarg;
12720Sstevel@tonic-gate
1273265Smws if (inp->din_root != NULL &&
1274265Smws inp->din_root->dn_kind == DT_NODE_INT) {
12750Sstevel@tonic-gate free(name);
12760Sstevel@tonic-gate
12770Sstevel@tonic-gate dnp = dt_node_alloc(DT_NODE_INT);
12780Sstevel@tonic-gate dnp->dn_op = DT_TOK_INT;
12790Sstevel@tonic-gate dnp->dn_value = inp->din_root->dn_value;
12800Sstevel@tonic-gate dt_node_type_propagate(inp->din_root, dnp);
12810Sstevel@tonic-gate
12820Sstevel@tonic-gate return (dnp);
12830Sstevel@tonic-gate }
12840Sstevel@tonic-gate }
12850Sstevel@tonic-gate
12860Sstevel@tonic-gate dnp = dt_node_alloc(DT_NODE_IDENT);
12870Sstevel@tonic-gate dnp->dn_op = name[0] == '@' ? DT_TOK_AGG : DT_TOK_IDENT;
12880Sstevel@tonic-gate dnp->dn_string = name;
12890Sstevel@tonic-gate
12900Sstevel@tonic-gate return (dnp);
12910Sstevel@tonic-gate }
12920Sstevel@tonic-gate
12930Sstevel@tonic-gate /*
12940Sstevel@tonic-gate * Create an empty node of type corresponding to the given declaration.
12950Sstevel@tonic-gate * Explicit references to user types (C or D) are assigned the default
12960Sstevel@tonic-gate * stability; references to other types are _dtrace_typattr (Private).
12970Sstevel@tonic-gate */
12980Sstevel@tonic-gate dt_node_t *
dt_node_type(dt_decl_t * ddp)12990Sstevel@tonic-gate dt_node_type(dt_decl_t *ddp)
13000Sstevel@tonic-gate {
13010Sstevel@tonic-gate dtrace_hdl_t *dtp = yypcb->pcb_hdl;
13020Sstevel@tonic-gate dtrace_typeinfo_t dtt;
13030Sstevel@tonic-gate dt_node_t *dnp;
13040Sstevel@tonic-gate char *name = NULL;
13050Sstevel@tonic-gate int err;
13060Sstevel@tonic-gate
13070Sstevel@tonic-gate /*
13080Sstevel@tonic-gate * If 'ddp' is NULL, we get a decl by popping the decl stack. This
13090Sstevel@tonic-gate * form of dt_node_type() is used by parameter rules in dt_grammar.y.
13100Sstevel@tonic-gate */
13110Sstevel@tonic-gate if (ddp == NULL)
13120Sstevel@tonic-gate ddp = dt_decl_pop_param(&name);
13130Sstevel@tonic-gate
13140Sstevel@tonic-gate err = dt_decl_type(ddp, &dtt);
13150Sstevel@tonic-gate dt_decl_free(ddp);
13160Sstevel@tonic-gate
13170Sstevel@tonic-gate if (err != 0) {
13180Sstevel@tonic-gate free(name);
13190Sstevel@tonic-gate longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
13200Sstevel@tonic-gate }
13210Sstevel@tonic-gate
13220Sstevel@tonic-gate dnp = dt_node_alloc(DT_NODE_TYPE);
13230Sstevel@tonic-gate dnp->dn_op = DT_TOK_IDENT;
13240Sstevel@tonic-gate dnp->dn_string = name;
13250Sstevel@tonic-gate dt_node_type_assign(dnp, dtt.dtt_ctfp, dtt.dtt_type);
13260Sstevel@tonic-gate
13270Sstevel@tonic-gate if (dtt.dtt_ctfp == dtp->dt_cdefs->dm_ctfp ||
13280Sstevel@tonic-gate dtt.dtt_ctfp == dtp->dt_ddefs->dm_ctfp)
13290Sstevel@tonic-gate dt_node_attr_assign(dnp, _dtrace_defattr);
13300Sstevel@tonic-gate else
13310Sstevel@tonic-gate dt_node_attr_assign(dnp, _dtrace_typattr);
13320Sstevel@tonic-gate
13330Sstevel@tonic-gate return (dnp);
13340Sstevel@tonic-gate }
13350Sstevel@tonic-gate
13360Sstevel@tonic-gate /*
13370Sstevel@tonic-gate * Create a type node corresponding to a varargs (...) parameter by just
13380Sstevel@tonic-gate * assigning it type CTF_ERR. The decl processing code will handle this.
13390Sstevel@tonic-gate */
13400Sstevel@tonic-gate dt_node_t *
dt_node_vatype(void)13410Sstevel@tonic-gate dt_node_vatype(void)
13420Sstevel@tonic-gate {
13430Sstevel@tonic-gate dt_node_t *dnp = dt_node_alloc(DT_NODE_TYPE);
13440Sstevel@tonic-gate
13450Sstevel@tonic-gate dnp->dn_op = DT_TOK_IDENT;
13460Sstevel@tonic-gate dnp->dn_ctfp = yypcb->pcb_hdl->dt_cdefs->dm_ctfp;
13470Sstevel@tonic-gate dnp->dn_type = CTF_ERR;
13480Sstevel@tonic-gate dnp->dn_attr = _dtrace_defattr;
13490Sstevel@tonic-gate
13500Sstevel@tonic-gate return (dnp);
13510Sstevel@tonic-gate }
13520Sstevel@tonic-gate
13530Sstevel@tonic-gate /*
13540Sstevel@tonic-gate * Instantiate a decl using the contents of the current declaration stack. As
13550Sstevel@tonic-gate * we do not currently permit decls to be initialized, this function currently
13560Sstevel@tonic-gate * returns NULL and no parse node is created. When this function is called,
13570Sstevel@tonic-gate * the topmost scope's ds_ident pointer will be set to NULL (indicating no
13580Sstevel@tonic-gate * init_declarator rule was matched) or will point to the identifier to use.
13590Sstevel@tonic-gate */
13600Sstevel@tonic-gate dt_node_t *
dt_node_decl(void)13610Sstevel@tonic-gate dt_node_decl(void)
13620Sstevel@tonic-gate {
13630Sstevel@tonic-gate dtrace_hdl_t *dtp = yypcb->pcb_hdl;
13640Sstevel@tonic-gate dt_scope_t *dsp = &yypcb->pcb_dstack;
13650Sstevel@tonic-gate dt_dclass_t class = dsp->ds_class;
13660Sstevel@tonic-gate dt_decl_t *ddp = dt_decl_top();
13670Sstevel@tonic-gate
13680Sstevel@tonic-gate dt_module_t *dmp;
13690Sstevel@tonic-gate dtrace_typeinfo_t dtt;
13700Sstevel@tonic-gate ctf_id_t type;
13710Sstevel@tonic-gate
13720Sstevel@tonic-gate char n1[DT_TYPE_NAMELEN];
13730Sstevel@tonic-gate char n2[DT_TYPE_NAMELEN];
13740Sstevel@tonic-gate
13750Sstevel@tonic-gate if (dt_decl_type(ddp, &dtt) != 0)
13760Sstevel@tonic-gate longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
13770Sstevel@tonic-gate
13780Sstevel@tonic-gate /*
13790Sstevel@tonic-gate * If we have no declaration identifier, then this is either a spurious
13800Sstevel@tonic-gate * declaration of an intrinsic type (e.g. "extern int;") or declaration
13810Sstevel@tonic-gate * or redeclaration of a struct, union, or enum type or tag.
13820Sstevel@tonic-gate */
13830Sstevel@tonic-gate if (dsp->ds_ident == NULL) {
13840Sstevel@tonic-gate if (ddp->dd_kind != CTF_K_STRUCT &&
13850Sstevel@tonic-gate ddp->dd_kind != CTF_K_UNION && ddp->dd_kind != CTF_K_ENUM)
13860Sstevel@tonic-gate xyerror(D_DECL_USELESS, "useless declaration\n");
13870Sstevel@tonic-gate
13880Sstevel@tonic-gate dt_dprintf("type %s added as id %ld\n", dt_type_name(
13890Sstevel@tonic-gate ddp->dd_ctfp, ddp->dd_type, n1, sizeof (n1)), ddp->dd_type);
13900Sstevel@tonic-gate
13910Sstevel@tonic-gate return (NULL);
13920Sstevel@tonic-gate }
13930Sstevel@tonic-gate
13940Sstevel@tonic-gate if (strchr(dsp->ds_ident, '`') != NULL) {
13950Sstevel@tonic-gate xyerror(D_DECL_SCOPE, "D scoping operator may not be used in "
13960Sstevel@tonic-gate "a declaration name (%s)\n", dsp->ds_ident);
13970Sstevel@tonic-gate }
13980Sstevel@tonic-gate
13990Sstevel@tonic-gate /*
14000Sstevel@tonic-gate * If we are nested inside of a C include file, add the declaration to
14010Sstevel@tonic-gate * the C definition module; otherwise use the D definition module.
14020Sstevel@tonic-gate */
14030Sstevel@tonic-gate if (yypcb->pcb_idepth != 0)
14040Sstevel@tonic-gate dmp = dtp->dt_cdefs;
14050Sstevel@tonic-gate else
14060Sstevel@tonic-gate dmp = dtp->dt_ddefs;
14070Sstevel@tonic-gate
14080Sstevel@tonic-gate /*
14090Sstevel@tonic-gate * If we see a global or static declaration of a function prototype,
14100Sstevel@tonic-gate * treat this as equivalent to a D extern declaration.
14110Sstevel@tonic-gate */
14120Sstevel@tonic-gate if (ctf_type_kind(dtt.dtt_ctfp, dtt.dtt_type) == CTF_K_FUNCTION &&
14130Sstevel@tonic-gate (class == DT_DC_DEFAULT || class == DT_DC_STATIC))
14140Sstevel@tonic-gate class = DT_DC_EXTERN;
14150Sstevel@tonic-gate
14160Sstevel@tonic-gate switch (class) {
14170Sstevel@tonic-gate case DT_DC_AUTO:
14180Sstevel@tonic-gate case DT_DC_REGISTER:
14190Sstevel@tonic-gate case DT_DC_STATIC:
14200Sstevel@tonic-gate xyerror(D_DECL_BADCLASS, "specified storage class not "
14210Sstevel@tonic-gate "appropriate in D\n");
14220Sstevel@tonic-gate /*NOTREACHED*/
14230Sstevel@tonic-gate
14240Sstevel@tonic-gate case DT_DC_EXTERN: {
14250Sstevel@tonic-gate dtrace_typeinfo_t ott;
14260Sstevel@tonic-gate dtrace_syminfo_t dts;
14270Sstevel@tonic-gate GElf_Sym sym;
14280Sstevel@tonic-gate
14290Sstevel@tonic-gate int exists = dtrace_lookup_by_name(dtp,
14300Sstevel@tonic-gate dmp->dm_name, dsp->ds_ident, &sym, &dts) == 0;
14310Sstevel@tonic-gate
14320Sstevel@tonic-gate if (exists && (dtrace_symbol_type(dtp, &sym, &dts, &ott) != 0 ||
14330Sstevel@tonic-gate ctf_type_cmp(dtt.dtt_ctfp, dtt.dtt_type,
14340Sstevel@tonic-gate ott.dtt_ctfp, ott.dtt_type) != 0)) {
14350Sstevel@tonic-gate xyerror(D_DECL_IDRED, "identifier redeclared: %s`%s\n"
14360Sstevel@tonic-gate "\t current: %s\n\tprevious: %s\n",
14370Sstevel@tonic-gate dmp->dm_name, dsp->ds_ident,
14380Sstevel@tonic-gate dt_type_name(dtt.dtt_ctfp, dtt.dtt_type,
1439*13093SRoger.Faulkner@Oracle.COM n1, sizeof (n1)),
14400Sstevel@tonic-gate dt_type_name(ott.dtt_ctfp, ott.dtt_type,
1441*13093SRoger.Faulkner@Oracle.COM n2, sizeof (n2)));
14420Sstevel@tonic-gate } else if (!exists && dt_module_extern(dtp, dmp,
14430Sstevel@tonic-gate dsp->ds_ident, &dtt) == NULL) {
14440Sstevel@tonic-gate xyerror(D_UNKNOWN,
14450Sstevel@tonic-gate "failed to extern %s: %s\n", dsp->ds_ident,
14460Sstevel@tonic-gate dtrace_errmsg(dtp, dtrace_errno(dtp)));
14470Sstevel@tonic-gate } else {
14480Sstevel@tonic-gate dt_dprintf("extern %s`%s type=<%s>\n",
14490Sstevel@tonic-gate dmp->dm_name, dsp->ds_ident,
14500Sstevel@tonic-gate dt_type_name(dtt.dtt_ctfp, dtt.dtt_type,
1451*13093SRoger.Faulkner@Oracle.COM n1, sizeof (n1)));
14520Sstevel@tonic-gate }
14530Sstevel@tonic-gate break;
14540Sstevel@tonic-gate }
14550Sstevel@tonic-gate
14560Sstevel@tonic-gate case DT_DC_TYPEDEF:
14571222Smws if (dt_idstack_lookup(&yypcb->pcb_globals, dsp->ds_ident)) {
14581222Smws xyerror(D_DECL_IDRED, "global variable identifier "
14591222Smws "redeclared: %s\n", dsp->ds_ident);
14601222Smws }
14611222Smws
14621222Smws if (ctf_lookup_by_name(dmp->dm_ctfp,
14631222Smws dsp->ds_ident) != CTF_ERR) {
14641222Smws xyerror(D_DECL_IDRED,
14651222Smws "typedef redeclared: %s\n", dsp->ds_ident);
14661222Smws }
14671222Smws
14680Sstevel@tonic-gate /*
14690Sstevel@tonic-gate * If the source type for the typedef is not defined in the
14700Sstevel@tonic-gate * target container or its parent, copy the type to the target
14710Sstevel@tonic-gate * container and reset dtt_ctfp and dtt_type to the copy.
14720Sstevel@tonic-gate */
14730Sstevel@tonic-gate if (dtt.dtt_ctfp != dmp->dm_ctfp &&
14740Sstevel@tonic-gate dtt.dtt_ctfp != ctf_parent_file(dmp->dm_ctfp)) {
14750Sstevel@tonic-gate
14760Sstevel@tonic-gate dtt.dtt_type = ctf_add_type(dmp->dm_ctfp,
14770Sstevel@tonic-gate dtt.dtt_ctfp, dtt.dtt_type);
14780Sstevel@tonic-gate dtt.dtt_ctfp = dmp->dm_ctfp;
14790Sstevel@tonic-gate
14800Sstevel@tonic-gate if (dtt.dtt_type == CTF_ERR ||
14810Sstevel@tonic-gate ctf_update(dtt.dtt_ctfp) == CTF_ERR) {
14820Sstevel@tonic-gate xyerror(D_UNKNOWN, "failed to copy typedef %s "
14830Sstevel@tonic-gate "source type: %s\n", dsp->ds_ident,
14840Sstevel@tonic-gate ctf_errmsg(ctf_errno(dtt.dtt_ctfp)));
14850Sstevel@tonic-gate }
14860Sstevel@tonic-gate }
14870Sstevel@tonic-gate
14880Sstevel@tonic-gate type = ctf_add_typedef(dmp->dm_ctfp,
14890Sstevel@tonic-gate CTF_ADD_ROOT, dsp->ds_ident, dtt.dtt_type);
14900Sstevel@tonic-gate
14910Sstevel@tonic-gate if (type == CTF_ERR || ctf_update(dmp->dm_ctfp) == CTF_ERR) {
14920Sstevel@tonic-gate xyerror(D_UNKNOWN, "failed to typedef %s: %s\n",
14930Sstevel@tonic-gate dsp->ds_ident, ctf_errmsg(ctf_errno(dmp->dm_ctfp)));
14940Sstevel@tonic-gate }
14950Sstevel@tonic-gate
14960Sstevel@tonic-gate dt_dprintf("typedef %s added as id %ld\n", dsp->ds_ident, type);
14970Sstevel@tonic-gate break;
14980Sstevel@tonic-gate
14990Sstevel@tonic-gate default: {
15000Sstevel@tonic-gate ctf_encoding_t cte;
15010Sstevel@tonic-gate dt_idhash_t *dhp;
15020Sstevel@tonic-gate dt_ident_t *idp;
15030Sstevel@tonic-gate dt_node_t idn;
15040Sstevel@tonic-gate int assc, idkind;
15050Sstevel@tonic-gate uint_t id, kind;
15060Sstevel@tonic-gate ushort_t idflags;
15070Sstevel@tonic-gate
15080Sstevel@tonic-gate switch (class) {
15090Sstevel@tonic-gate case DT_DC_THIS:
15100Sstevel@tonic-gate dhp = yypcb->pcb_locals;
15110Sstevel@tonic-gate idflags = DT_IDFLG_LOCAL;
15120Sstevel@tonic-gate idp = dt_idhash_lookup(dhp, dsp->ds_ident);
15130Sstevel@tonic-gate break;
15140Sstevel@tonic-gate case DT_DC_SELF:
15150Sstevel@tonic-gate dhp = dtp->dt_tls;
15160Sstevel@tonic-gate idflags = DT_IDFLG_TLS;
15170Sstevel@tonic-gate idp = dt_idhash_lookup(dhp, dsp->ds_ident);
15180Sstevel@tonic-gate break;
15190Sstevel@tonic-gate default:
15200Sstevel@tonic-gate dhp = dtp->dt_globals;
15210Sstevel@tonic-gate idflags = 0;
15220Sstevel@tonic-gate idp = dt_idstack_lookup(
15230Sstevel@tonic-gate &yypcb->pcb_globals, dsp->ds_ident);
15240Sstevel@tonic-gate break;
15250Sstevel@tonic-gate }
15260Sstevel@tonic-gate
15270Sstevel@tonic-gate if (ddp->dd_kind == CTF_K_ARRAY && ddp->dd_node == NULL) {
15280Sstevel@tonic-gate xyerror(D_DECL_ARRNULL,
15290Sstevel@tonic-gate "array declaration requires array dimension or "
15300Sstevel@tonic-gate "tuple signature: %s\n", dsp->ds_ident);
15310Sstevel@tonic-gate }
15320Sstevel@tonic-gate
15330Sstevel@tonic-gate if (idp != NULL && idp->di_gen == 0) {
15340Sstevel@tonic-gate xyerror(D_DECL_IDRED, "built-in identifier "
15350Sstevel@tonic-gate "redeclared: %s\n", idp->di_name);
15360Sstevel@tonic-gate }
15370Sstevel@tonic-gate
15381222Smws if (dtrace_lookup_by_type(dtp, DTRACE_OBJ_CDEFS,
15391222Smws dsp->ds_ident, NULL) == 0 ||
15401222Smws dtrace_lookup_by_type(dtp, DTRACE_OBJ_DDEFS,
15411222Smws dsp->ds_ident, NULL) == 0) {
15421222Smws xyerror(D_DECL_IDRED, "typedef identifier "
15431222Smws "redeclared: %s\n", dsp->ds_ident);
15441222Smws }
15451222Smws
15460Sstevel@tonic-gate /*
15470Sstevel@tonic-gate * Cache some attributes of the decl to make the rest of this
15480Sstevel@tonic-gate * code simpler: if the decl is an array which is subscripted
15490Sstevel@tonic-gate * by a type rather than an integer, then it's an associative
15500Sstevel@tonic-gate * array (assc). We then expect to match either DT_IDENT_ARRAY
15510Sstevel@tonic-gate * for associative arrays or DT_IDENT_SCALAR for anything else.
15520Sstevel@tonic-gate */
15530Sstevel@tonic-gate assc = ddp->dd_kind == CTF_K_ARRAY &&
15540Sstevel@tonic-gate ddp->dd_node->dn_kind == DT_NODE_TYPE;
15550Sstevel@tonic-gate
15560Sstevel@tonic-gate idkind = assc ? DT_IDENT_ARRAY : DT_IDENT_SCALAR;
15570Sstevel@tonic-gate
15580Sstevel@tonic-gate /*
15590Sstevel@tonic-gate * Create a fake dt_node_t on the stack so we can determine the
15600Sstevel@tonic-gate * type of any matching identifier by assigning to this node.
15610Sstevel@tonic-gate * If the pre-existing ident has its di_type set, propagate
15620Sstevel@tonic-gate * the type by hand so as not to trigger a prototype check for
15630Sstevel@tonic-gate * arrays (yet); otherwise we use dt_ident_cook() on the ident
15640Sstevel@tonic-gate * to ensure it is fully initialized before looking at it.
15650Sstevel@tonic-gate */
15660Sstevel@tonic-gate bzero(&idn, sizeof (dt_node_t));
15670Sstevel@tonic-gate
15680Sstevel@tonic-gate if (idp != NULL && idp->di_type != CTF_ERR)
15690Sstevel@tonic-gate dt_node_type_assign(&idn, idp->di_ctfp, idp->di_type);
15700Sstevel@tonic-gate else if (idp != NULL)
15710Sstevel@tonic-gate (void) dt_ident_cook(&idn, idp, NULL);
15720Sstevel@tonic-gate
15730Sstevel@tonic-gate if (assc) {
15740Sstevel@tonic-gate if (class == DT_DC_THIS) {
15750Sstevel@tonic-gate xyerror(D_DECL_LOCASSC, "associative arrays "
15760Sstevel@tonic-gate "may not be declared as local variables:"
15770Sstevel@tonic-gate " %s\n", dsp->ds_ident);
15780Sstevel@tonic-gate }
15790Sstevel@tonic-gate
15800Sstevel@tonic-gate if (dt_decl_type(ddp->dd_next, &dtt) != 0)
15810Sstevel@tonic-gate longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
15820Sstevel@tonic-gate }
15830Sstevel@tonic-gate
15840Sstevel@tonic-gate if (idp != NULL && (idp->di_kind != idkind ||
15850Sstevel@tonic-gate ctf_type_cmp(dtt.dtt_ctfp, dtt.dtt_type,
15860Sstevel@tonic-gate idn.dn_ctfp, idn.dn_type) != 0)) {
15870Sstevel@tonic-gate xyerror(D_DECL_IDRED, "identifier redeclared: %s\n"
15880Sstevel@tonic-gate "\t current: %s %s\n\tprevious: %s %s\n",
15890Sstevel@tonic-gate dsp->ds_ident, dt_idkind_name(idkind),
15900Sstevel@tonic-gate dt_type_name(dtt.dtt_ctfp,
15910Sstevel@tonic-gate dtt.dtt_type, n1, sizeof (n1)),
15920Sstevel@tonic-gate dt_idkind_name(idp->di_kind),
15930Sstevel@tonic-gate dt_node_type_name(&idn, n2, sizeof (n2)));
15940Sstevel@tonic-gate
15950Sstevel@tonic-gate } else if (idp != NULL && assc) {
15960Sstevel@tonic-gate const dt_idsig_t *isp = idp->di_data;
15970Sstevel@tonic-gate dt_node_t *dnp = ddp->dd_node;
15980Sstevel@tonic-gate int argc = 0;
15990Sstevel@tonic-gate
16000Sstevel@tonic-gate for (; dnp != NULL; dnp = dnp->dn_list, argc++) {
16010Sstevel@tonic-gate const dt_node_t *pnp = &isp->dis_args[argc];
16020Sstevel@tonic-gate
16030Sstevel@tonic-gate if (argc >= isp->dis_argc)
16040Sstevel@tonic-gate continue; /* tuple length mismatch */
16050Sstevel@tonic-gate
16060Sstevel@tonic-gate if (ctf_type_cmp(dnp->dn_ctfp, dnp->dn_type,
16070Sstevel@tonic-gate pnp->dn_ctfp, pnp->dn_type) == 0)
16080Sstevel@tonic-gate continue;
16090Sstevel@tonic-gate
16100Sstevel@tonic-gate xyerror(D_DECL_IDRED,
16110Sstevel@tonic-gate "identifier redeclared: %s\n"
16120Sstevel@tonic-gate "\t current: %s, key #%d of type %s\n"
16130Sstevel@tonic-gate "\tprevious: %s, key #%d of type %s\n",
16140Sstevel@tonic-gate dsp->ds_ident,
16150Sstevel@tonic-gate dt_idkind_name(idkind), argc + 1,
16160Sstevel@tonic-gate dt_node_type_name(dnp, n1, sizeof (n1)),
16170Sstevel@tonic-gate dt_idkind_name(idp->di_kind), argc + 1,
16180Sstevel@tonic-gate dt_node_type_name(pnp, n2, sizeof (n2)));
16190Sstevel@tonic-gate }
16200Sstevel@tonic-gate
16210Sstevel@tonic-gate if (isp->dis_argc != argc) {
16220Sstevel@tonic-gate xyerror(D_DECL_IDRED,
16230Sstevel@tonic-gate "identifier redeclared: %s\n"
16240Sstevel@tonic-gate "\t current: %s of %s, tuple length %d\n"
16250Sstevel@tonic-gate "\tprevious: %s of %s, tuple length %d\n",
16260Sstevel@tonic-gate dsp->ds_ident, dt_idkind_name(idkind),
16270Sstevel@tonic-gate dt_type_name(dtt.dtt_ctfp, dtt.dtt_type,
16280Sstevel@tonic-gate n1, sizeof (n1)), argc,
16290Sstevel@tonic-gate dt_idkind_name(idp->di_kind),
16300Sstevel@tonic-gate dt_node_type_name(&idn, n2, sizeof (n2)),
16310Sstevel@tonic-gate isp->dis_argc);
16320Sstevel@tonic-gate }
16330Sstevel@tonic-gate
16340Sstevel@tonic-gate } else if (idp == NULL) {
16350Sstevel@tonic-gate type = ctf_type_resolve(dtt.dtt_ctfp, dtt.dtt_type);
16360Sstevel@tonic-gate kind = ctf_type_kind(dtt.dtt_ctfp, type);
16370Sstevel@tonic-gate
16380Sstevel@tonic-gate switch (kind) {
16390Sstevel@tonic-gate case CTF_K_INTEGER:
16400Sstevel@tonic-gate if (ctf_type_encoding(dtt.dtt_ctfp, type,
16410Sstevel@tonic-gate &cte) == 0 && IS_VOID(cte)) {
16420Sstevel@tonic-gate xyerror(D_DECL_VOIDOBJ, "cannot have "
16430Sstevel@tonic-gate "void object: %s\n", dsp->ds_ident);
16440Sstevel@tonic-gate }
16450Sstevel@tonic-gate break;
16460Sstevel@tonic-gate case CTF_K_STRUCT:
16470Sstevel@tonic-gate case CTF_K_UNION:
16480Sstevel@tonic-gate if (ctf_type_size(dtt.dtt_ctfp, type) != 0)
16490Sstevel@tonic-gate break; /* proceed to declaring */
16500Sstevel@tonic-gate /*FALLTHRU*/
16510Sstevel@tonic-gate case CTF_K_FORWARD:
16520Sstevel@tonic-gate xyerror(D_DECL_INCOMPLETE,
16530Sstevel@tonic-gate "incomplete struct/union/enum %s: %s\n",
16540Sstevel@tonic-gate dt_type_name(dtt.dtt_ctfp, dtt.dtt_type,
16550Sstevel@tonic-gate n1, sizeof (n1)), dsp->ds_ident);
16560Sstevel@tonic-gate /*NOTREACHED*/
16570Sstevel@tonic-gate }
16580Sstevel@tonic-gate
16590Sstevel@tonic-gate if (dt_idhash_nextid(dhp, &id) == -1) {
16600Sstevel@tonic-gate xyerror(D_ID_OFLOW, "cannot create %s: limit "
16610Sstevel@tonic-gate "on number of %s variables exceeded\n",
16620Sstevel@tonic-gate dsp->ds_ident, dt_idhash_name(dhp));
16630Sstevel@tonic-gate }
16640Sstevel@tonic-gate
16650Sstevel@tonic-gate dt_dprintf("declare %s %s variable %s, id=%u\n",
16660Sstevel@tonic-gate dt_idhash_name(dhp), dt_idkind_name(idkind),
16670Sstevel@tonic-gate dsp->ds_ident, id);
16680Sstevel@tonic-gate
16690Sstevel@tonic-gate idp = dt_idhash_insert(dhp, dsp->ds_ident, idkind,
16700Sstevel@tonic-gate idflags | DT_IDFLG_WRITE | DT_IDFLG_DECL, id,
16710Sstevel@tonic-gate _dtrace_defattr, 0, assc ? &dt_idops_assc :
16720Sstevel@tonic-gate &dt_idops_thaw, NULL, dtp->dt_gen);
16730Sstevel@tonic-gate
16740Sstevel@tonic-gate if (idp == NULL)
16750Sstevel@tonic-gate longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
16760Sstevel@tonic-gate
16770Sstevel@tonic-gate dt_ident_type_assign(idp, dtt.dtt_ctfp, dtt.dtt_type);
16780Sstevel@tonic-gate
16790Sstevel@tonic-gate /*
16800Sstevel@tonic-gate * If we are declaring an associative array, use our
16810Sstevel@tonic-gate * fake parse node to cook the new assoc identifier.
16820Sstevel@tonic-gate * This will force the ident code to instantiate the
16830Sstevel@tonic-gate * array type signature corresponding to the list of
16840Sstevel@tonic-gate * types pointed to by ddp->dd_node. We also reset
16850Sstevel@tonic-gate * the identifier's attributes based upon the result.
16860Sstevel@tonic-gate */
16870Sstevel@tonic-gate if (assc) {
16880Sstevel@tonic-gate idp->di_attr =
16890Sstevel@tonic-gate dt_ident_cook(&idn, idp, &ddp->dd_node);
16900Sstevel@tonic-gate }
16910Sstevel@tonic-gate }
16920Sstevel@tonic-gate }
16930Sstevel@tonic-gate
16940Sstevel@tonic-gate } /* end of switch */
16950Sstevel@tonic-gate
16960Sstevel@tonic-gate free(dsp->ds_ident);
16970Sstevel@tonic-gate dsp->ds_ident = NULL;
16980Sstevel@tonic-gate
16990Sstevel@tonic-gate return (NULL);
17000Sstevel@tonic-gate }
17010Sstevel@tonic-gate
17020Sstevel@tonic-gate dt_node_t *
dt_node_func(dt_node_t * dnp,dt_node_t * args)17030Sstevel@tonic-gate dt_node_func(dt_node_t *dnp, dt_node_t *args)
17040Sstevel@tonic-gate {
17050Sstevel@tonic-gate dt_ident_t *idp;
17060Sstevel@tonic-gate
17070Sstevel@tonic-gate if (dnp->dn_kind != DT_NODE_IDENT) {
17080Sstevel@tonic-gate xyerror(D_FUNC_IDENT,
17090Sstevel@tonic-gate "function designator is not of function type\n");
17100Sstevel@tonic-gate }
17110Sstevel@tonic-gate
17120Sstevel@tonic-gate idp = dt_idstack_lookup(&yypcb->pcb_globals, dnp->dn_string);
17130Sstevel@tonic-gate
17140Sstevel@tonic-gate if (idp == NULL) {
17150Sstevel@tonic-gate xyerror(D_FUNC_UNDEF,
17160Sstevel@tonic-gate "undefined function name: %s\n", dnp->dn_string);
17170Sstevel@tonic-gate }
17180Sstevel@tonic-gate
17190Sstevel@tonic-gate if (idp->di_kind != DT_IDENT_FUNC &&
17200Sstevel@tonic-gate idp->di_kind != DT_IDENT_AGGFUNC &&
17210Sstevel@tonic-gate idp->di_kind != DT_IDENT_ACTFUNC) {
17220Sstevel@tonic-gate xyerror(D_FUNC_IDKIND, "%s '%s' may not be referenced as a "
17230Sstevel@tonic-gate "function\n", dt_idkind_name(idp->di_kind), idp->di_name);
17240Sstevel@tonic-gate }
17250Sstevel@tonic-gate
17260Sstevel@tonic-gate free(dnp->dn_string);
17270Sstevel@tonic-gate dnp->dn_string = NULL;
17280Sstevel@tonic-gate
17290Sstevel@tonic-gate dnp->dn_kind = DT_NODE_FUNC;
17300Sstevel@tonic-gate dnp->dn_flags &= ~DT_NF_COOKED;
17310Sstevel@tonic-gate dnp->dn_ident = idp;
17320Sstevel@tonic-gate dnp->dn_args = args;
17330Sstevel@tonic-gate dnp->dn_list = NULL;
17340Sstevel@tonic-gate
17350Sstevel@tonic-gate return (dnp);
17360Sstevel@tonic-gate }
17370Sstevel@tonic-gate
17380Sstevel@tonic-gate /*
17390Sstevel@tonic-gate * The offsetof() function is special because it takes a type name as an
17400Sstevel@tonic-gate * argument. It does not actually construct its own node; after looking up the
17410Sstevel@tonic-gate * structure or union offset, we just return an integer node with the offset.
17420Sstevel@tonic-gate */
17430Sstevel@tonic-gate dt_node_t *
dt_node_offsetof(dt_decl_t * ddp,char * s)17440Sstevel@tonic-gate dt_node_offsetof(dt_decl_t *ddp, char *s)
17450Sstevel@tonic-gate {
17460Sstevel@tonic-gate dtrace_typeinfo_t dtt;
17470Sstevel@tonic-gate dt_node_t dn;
17480Sstevel@tonic-gate char *name;
17490Sstevel@tonic-gate int err;
17500Sstevel@tonic-gate
17510Sstevel@tonic-gate ctf_membinfo_t ctm;
17520Sstevel@tonic-gate ctf_id_t type;
17530Sstevel@tonic-gate uint_t kind;
17540Sstevel@tonic-gate
1755*13093SRoger.Faulkner@Oracle.COM name = strdupa(s);
17560Sstevel@tonic-gate free(s);
17570Sstevel@tonic-gate
17580Sstevel@tonic-gate err = dt_decl_type(ddp, &dtt);
17590Sstevel@tonic-gate dt_decl_free(ddp);
17600Sstevel@tonic-gate
17610Sstevel@tonic-gate if (err != 0)
17620Sstevel@tonic-gate longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
17630Sstevel@tonic-gate
17640Sstevel@tonic-gate type = ctf_type_resolve(dtt.dtt_ctfp, dtt.dtt_type);
17650Sstevel@tonic-gate kind = ctf_type_kind(dtt.dtt_ctfp, type);
17660Sstevel@tonic-gate
17670Sstevel@tonic-gate if (kind != CTF_K_STRUCT && kind != CTF_K_UNION) {
17680Sstevel@tonic-gate xyerror(D_OFFSETOF_TYPE,
17690Sstevel@tonic-gate "offsetof operand must be a struct or union type\n");
17700Sstevel@tonic-gate }
17710Sstevel@tonic-gate
17720Sstevel@tonic-gate if (ctf_member_info(dtt.dtt_ctfp, type, name, &ctm) == CTF_ERR) {
17730Sstevel@tonic-gate xyerror(D_UNKNOWN, "failed to determine offset of %s: %s\n",
17740Sstevel@tonic-gate name, ctf_errmsg(ctf_errno(dtt.dtt_ctfp)));
17750Sstevel@tonic-gate }
17760Sstevel@tonic-gate
17770Sstevel@tonic-gate bzero(&dn, sizeof (dn));
17780Sstevel@tonic-gate dt_node_type_assign(&dn, dtt.dtt_ctfp, ctm.ctm_type);
17790Sstevel@tonic-gate
17800Sstevel@tonic-gate if (dn.dn_flags & DT_NF_BITFIELD) {
17810Sstevel@tonic-gate xyerror(D_OFFSETOF_BITFIELD,
17820Sstevel@tonic-gate "cannot take offset of a bit-field: %s\n", name);
17830Sstevel@tonic-gate }
17840Sstevel@tonic-gate
17850Sstevel@tonic-gate return (dt_node_int(ctm.ctm_offset / NBBY));
17860Sstevel@tonic-gate }
17870Sstevel@tonic-gate
17880Sstevel@tonic-gate dt_node_t *
dt_node_op1(int op,dt_node_t * cp)17890Sstevel@tonic-gate dt_node_op1(int op, dt_node_t *cp)
17900Sstevel@tonic-gate {
17910Sstevel@tonic-gate dt_node_t *dnp;
17920Sstevel@tonic-gate
17930Sstevel@tonic-gate if (cp->dn_kind == DT_NODE_INT) {
17940Sstevel@tonic-gate switch (op) {
17950Sstevel@tonic-gate case DT_TOK_INEG:
17960Sstevel@tonic-gate /*
17970Sstevel@tonic-gate * If we're negating an unsigned integer, zero out any
17980Sstevel@tonic-gate * extra top bits to truncate the value to the size of
17990Sstevel@tonic-gate * the effective type determined by dt_node_int().
18000Sstevel@tonic-gate */
18010Sstevel@tonic-gate cp->dn_value = -cp->dn_value;
18020Sstevel@tonic-gate if (!(cp->dn_flags & DT_NF_SIGNED)) {
18030Sstevel@tonic-gate cp->dn_value &= ~0ULL >>
18040Sstevel@tonic-gate (64 - dt_node_type_size(cp) * NBBY);
18050Sstevel@tonic-gate }
18060Sstevel@tonic-gate /*FALLTHRU*/
18070Sstevel@tonic-gate case DT_TOK_IPOS:
18080Sstevel@tonic-gate return (cp);
18090Sstevel@tonic-gate case DT_TOK_BNEG:
18100Sstevel@tonic-gate cp->dn_value = ~cp->dn_value;
18110Sstevel@tonic-gate return (cp);
18120Sstevel@tonic-gate case DT_TOK_LNEG:
18130Sstevel@tonic-gate cp->dn_value = !cp->dn_value;
18140Sstevel@tonic-gate return (cp);
18150Sstevel@tonic-gate }
18160Sstevel@tonic-gate }
18170Sstevel@tonic-gate
18180Sstevel@tonic-gate /*
18190Sstevel@tonic-gate * If sizeof is applied to a type_name or string constant, we can
18200Sstevel@tonic-gate * transform 'cp' into an integer constant in the node construction
18210Sstevel@tonic-gate * pass so that it can then be used for arithmetic in this pass.
18220Sstevel@tonic-gate */
18230Sstevel@tonic-gate if (op == DT_TOK_SIZEOF &&
18240Sstevel@tonic-gate (cp->dn_kind == DT_NODE_STRING || cp->dn_kind == DT_NODE_TYPE)) {
18250Sstevel@tonic-gate dtrace_hdl_t *dtp = yypcb->pcb_hdl;
18260Sstevel@tonic-gate size_t size = dt_node_type_size(cp);
18270Sstevel@tonic-gate
18280Sstevel@tonic-gate if (size == 0) {
18290Sstevel@tonic-gate xyerror(D_SIZEOF_TYPE, "cannot apply sizeof to an "
18300Sstevel@tonic-gate "operand of unknown size\n");
18310Sstevel@tonic-gate }
18320Sstevel@tonic-gate
18330Sstevel@tonic-gate dt_node_type_assign(cp, dtp->dt_ddefs->dm_ctfp,
18340Sstevel@tonic-gate ctf_lookup_by_name(dtp->dt_ddefs->dm_ctfp, "size_t"));
18350Sstevel@tonic-gate
18360Sstevel@tonic-gate cp->dn_kind = DT_NODE_INT;
18370Sstevel@tonic-gate cp->dn_op = DT_TOK_INT;
18380Sstevel@tonic-gate cp->dn_value = size;
18390Sstevel@tonic-gate
18400Sstevel@tonic-gate return (cp);
18410Sstevel@tonic-gate }
18420Sstevel@tonic-gate
18430Sstevel@tonic-gate dnp = dt_node_alloc(DT_NODE_OP1);
18440Sstevel@tonic-gate assert(op <= USHRT_MAX);
18450Sstevel@tonic-gate dnp->dn_op = (ushort_t)op;
18460Sstevel@tonic-gate dnp->dn_child = cp;
18470Sstevel@tonic-gate
18480Sstevel@tonic-gate return (dnp);
18490Sstevel@tonic-gate }
18500Sstevel@tonic-gate
18510Sstevel@tonic-gate dt_node_t *
dt_node_op2(int op,dt_node_t * lp,dt_node_t * rp)18520Sstevel@tonic-gate dt_node_op2(int op, dt_node_t *lp, dt_node_t *rp)
18530Sstevel@tonic-gate {
18540Sstevel@tonic-gate dtrace_hdl_t *dtp = yypcb->pcb_hdl;
18550Sstevel@tonic-gate dt_node_t *dnp;
18560Sstevel@tonic-gate
18570Sstevel@tonic-gate /*
18580Sstevel@tonic-gate * First we check for operations that are illegal -- namely those that
18590Sstevel@tonic-gate * might result in integer division by zero, and abort if one is found.
18600Sstevel@tonic-gate */
18610Sstevel@tonic-gate if (rp->dn_kind == DT_NODE_INT && rp->dn_value == 0 &&
18620Sstevel@tonic-gate (op == DT_TOK_MOD || op == DT_TOK_DIV ||
18630Sstevel@tonic-gate op == DT_TOK_MOD_EQ || op == DT_TOK_DIV_EQ))
18640Sstevel@tonic-gate xyerror(D_DIV_ZERO, "expression contains division by zero\n");
18650Sstevel@tonic-gate
18660Sstevel@tonic-gate /*
18670Sstevel@tonic-gate * If both children are immediate values, we can just perform inline
18680Sstevel@tonic-gate * calculation and return a new immediate node with the result.
18690Sstevel@tonic-gate */
18700Sstevel@tonic-gate if (lp->dn_kind == DT_NODE_INT && rp->dn_kind == DT_NODE_INT) {
18710Sstevel@tonic-gate uintmax_t l = lp->dn_value;
18720Sstevel@tonic-gate uintmax_t r = rp->dn_value;
18730Sstevel@tonic-gate
18740Sstevel@tonic-gate dnp = dt_node_int(0); /* allocate new integer node for result */
18750Sstevel@tonic-gate
18760Sstevel@tonic-gate switch (op) {
18770Sstevel@tonic-gate case DT_TOK_LOR:
18780Sstevel@tonic-gate dnp->dn_value = l || r;
18790Sstevel@tonic-gate dt_node_type_assign(dnp,
18800Sstevel@tonic-gate DT_INT_CTFP(dtp), DT_INT_TYPE(dtp));
18810Sstevel@tonic-gate break;
18820Sstevel@tonic-gate case DT_TOK_LXOR:
18830Sstevel@tonic-gate dnp->dn_value = (l != 0) ^ (r != 0);
18840Sstevel@tonic-gate dt_node_type_assign(dnp,
18850Sstevel@tonic-gate DT_INT_CTFP(dtp), DT_INT_TYPE(dtp));
18860Sstevel@tonic-gate break;
18870Sstevel@tonic-gate case DT_TOK_LAND:
18880Sstevel@tonic-gate dnp->dn_value = l && r;
18890Sstevel@tonic-gate dt_node_type_assign(dnp,
18900Sstevel@tonic-gate DT_INT_CTFP(dtp), DT_INT_TYPE(dtp));
18910Sstevel@tonic-gate break;
18920Sstevel@tonic-gate case DT_TOK_BOR:
18930Sstevel@tonic-gate dnp->dn_value = l | r;
18940Sstevel@tonic-gate dt_node_promote(lp, rp, dnp);
18950Sstevel@tonic-gate break;
18960Sstevel@tonic-gate case DT_TOK_XOR:
18970Sstevel@tonic-gate dnp->dn_value = l ^ r;
18980Sstevel@tonic-gate dt_node_promote(lp, rp, dnp);
18990Sstevel@tonic-gate break;
19000Sstevel@tonic-gate case DT_TOK_BAND:
19010Sstevel@tonic-gate dnp->dn_value = l & r;
19020Sstevel@tonic-gate dt_node_promote(lp, rp, dnp);
19030Sstevel@tonic-gate break;
19040Sstevel@tonic-gate case DT_TOK_EQU:
19050Sstevel@tonic-gate dnp->dn_value = l == r;
19060Sstevel@tonic-gate dt_node_type_assign(dnp,
19070Sstevel@tonic-gate DT_INT_CTFP(dtp), DT_INT_TYPE(dtp));
19080Sstevel@tonic-gate break;
19090Sstevel@tonic-gate case DT_TOK_NEQ:
19100Sstevel@tonic-gate dnp->dn_value = l != r;
19110Sstevel@tonic-gate dt_node_type_assign(dnp,
19120Sstevel@tonic-gate DT_INT_CTFP(dtp), DT_INT_TYPE(dtp));
19130Sstevel@tonic-gate break;
19140Sstevel@tonic-gate case DT_TOK_LT:
19150Sstevel@tonic-gate dt_node_promote(lp, rp, dnp);
19160Sstevel@tonic-gate if (dnp->dn_flags & DT_NF_SIGNED)
19170Sstevel@tonic-gate dnp->dn_value = (intmax_t)l < (intmax_t)r;
19180Sstevel@tonic-gate else
19190Sstevel@tonic-gate dnp->dn_value = l < r;
19200Sstevel@tonic-gate dt_node_type_assign(dnp,
19210Sstevel@tonic-gate DT_INT_CTFP(dtp), DT_INT_TYPE(dtp));
19220Sstevel@tonic-gate break;
19230Sstevel@tonic-gate case DT_TOK_LE:
19240Sstevel@tonic-gate dt_node_promote(lp, rp, dnp);
19250Sstevel@tonic-gate if (dnp->dn_flags & DT_NF_SIGNED)
19260Sstevel@tonic-gate dnp->dn_value = (intmax_t)l <= (intmax_t)r;
19270Sstevel@tonic-gate else
19280Sstevel@tonic-gate dnp->dn_value = l <= r;
19290Sstevel@tonic-gate dt_node_type_assign(dnp,
19300Sstevel@tonic-gate DT_INT_CTFP(dtp), DT_INT_TYPE(dtp));
19310Sstevel@tonic-gate break;
19320Sstevel@tonic-gate case DT_TOK_GT:
19330Sstevel@tonic-gate dt_node_promote(lp, rp, dnp);
19340Sstevel@tonic-gate if (dnp->dn_flags & DT_NF_SIGNED)
19350Sstevel@tonic-gate dnp->dn_value = (intmax_t)l > (intmax_t)r;
19360Sstevel@tonic-gate else
19370Sstevel@tonic-gate dnp->dn_value = l > r;
19380Sstevel@tonic-gate dt_node_type_assign(dnp,
19390Sstevel@tonic-gate DT_INT_CTFP(dtp), DT_INT_TYPE(dtp));
19400Sstevel@tonic-gate break;
19410Sstevel@tonic-gate case DT_TOK_GE:
19420Sstevel@tonic-gate dt_node_promote(lp, rp, dnp);
19430Sstevel@tonic-gate if (dnp->dn_flags & DT_NF_SIGNED)
19440Sstevel@tonic-gate dnp->dn_value = (intmax_t)l >= (intmax_t)r;
19450Sstevel@tonic-gate else
19460Sstevel@tonic-gate dnp->dn_value = l >= r;
19470Sstevel@tonic-gate dt_node_type_assign(dnp,
19480Sstevel@tonic-gate DT_INT_CTFP(dtp), DT_INT_TYPE(dtp));
19490Sstevel@tonic-gate break;
19500Sstevel@tonic-gate case DT_TOK_LSH:
19510Sstevel@tonic-gate dnp->dn_value = l << r;
19520Sstevel@tonic-gate dt_node_type_propagate(lp, dnp);
19530Sstevel@tonic-gate dt_node_attr_assign(rp,
19540Sstevel@tonic-gate dt_attr_min(lp->dn_attr, rp->dn_attr));
19550Sstevel@tonic-gate break;
19560Sstevel@tonic-gate case DT_TOK_RSH:
19570Sstevel@tonic-gate dnp->dn_value = l >> r;
19580Sstevel@tonic-gate dt_node_type_propagate(lp, dnp);
19590Sstevel@tonic-gate dt_node_attr_assign(rp,
19600Sstevel@tonic-gate dt_attr_min(lp->dn_attr, rp->dn_attr));
19610Sstevel@tonic-gate break;
19620Sstevel@tonic-gate case DT_TOK_ADD:
19630Sstevel@tonic-gate dnp->dn_value = l + r;
19640Sstevel@tonic-gate dt_node_promote(lp, rp, dnp);
19650Sstevel@tonic-gate break;
19660Sstevel@tonic-gate case DT_TOK_SUB:
19670Sstevel@tonic-gate dnp->dn_value = l - r;
19680Sstevel@tonic-gate dt_node_promote(lp, rp, dnp);
19690Sstevel@tonic-gate break;
19700Sstevel@tonic-gate case DT_TOK_MUL:
19710Sstevel@tonic-gate dnp->dn_value = l * r;
19720Sstevel@tonic-gate dt_node_promote(lp, rp, dnp);
19730Sstevel@tonic-gate break;
19740Sstevel@tonic-gate case DT_TOK_DIV:
19750Sstevel@tonic-gate dt_node_promote(lp, rp, dnp);
19760Sstevel@tonic-gate if (dnp->dn_flags & DT_NF_SIGNED)
19770Sstevel@tonic-gate dnp->dn_value = (intmax_t)l / (intmax_t)r;
19780Sstevel@tonic-gate else
19790Sstevel@tonic-gate dnp->dn_value = l / r;
19800Sstevel@tonic-gate break;
19810Sstevel@tonic-gate case DT_TOK_MOD:
19820Sstevel@tonic-gate dt_node_promote(lp, rp, dnp);
19830Sstevel@tonic-gate if (dnp->dn_flags & DT_NF_SIGNED)
19840Sstevel@tonic-gate dnp->dn_value = (intmax_t)l % (intmax_t)r;
19850Sstevel@tonic-gate else
19860Sstevel@tonic-gate dnp->dn_value = l % r;
19870Sstevel@tonic-gate break;
19880Sstevel@tonic-gate default:
19890Sstevel@tonic-gate dt_node_free(dnp);
19900Sstevel@tonic-gate dnp = NULL;
19910Sstevel@tonic-gate }
19920Sstevel@tonic-gate
19930Sstevel@tonic-gate if (dnp != NULL) {
19940Sstevel@tonic-gate dt_node_free(lp);
19950Sstevel@tonic-gate dt_node_free(rp);
19960Sstevel@tonic-gate return (dnp);
19970Sstevel@tonic-gate }
19980Sstevel@tonic-gate }
19990Sstevel@tonic-gate
20000Sstevel@tonic-gate /*
20010Sstevel@tonic-gate * If an integer constant is being cast to another integer type, we can
20020Sstevel@tonic-gate * perform the cast as part of integer constant folding in this pass.
20030Sstevel@tonic-gate * We must take action when the integer is being cast to a smaller type
20040Sstevel@tonic-gate * or if it is changing signed-ness. If so, we first shift rp's bits
20050Sstevel@tonic-gate * bits high (losing excess bits if narrowing) and then shift them down
20060Sstevel@tonic-gate * with either a logical shift (unsigned) or arithmetic shift (signed).
20070Sstevel@tonic-gate */
20080Sstevel@tonic-gate if (op == DT_TOK_LPAR && rp->dn_kind == DT_NODE_INT &&
20090Sstevel@tonic-gate dt_node_is_integer(lp)) {
20100Sstevel@tonic-gate size_t srcsize = dt_node_type_size(rp);
20110Sstevel@tonic-gate size_t dstsize = dt_node_type_size(lp);
20120Sstevel@tonic-gate
20130Sstevel@tonic-gate if ((dstsize < srcsize) || ((lp->dn_flags & DT_NF_SIGNED) ^
20140Sstevel@tonic-gate (rp->dn_flags & DT_NF_SIGNED))) {
20150Sstevel@tonic-gate int n = dstsize < srcsize ?
20160Sstevel@tonic-gate (sizeof (uint64_t) * NBBY - dstsize * NBBY) :
20170Sstevel@tonic-gate (sizeof (uint64_t) * NBBY - srcsize * NBBY);
20180Sstevel@tonic-gate
20190Sstevel@tonic-gate rp->dn_value <<= n;
20200Sstevel@tonic-gate if (lp->dn_flags & DT_NF_SIGNED)
20210Sstevel@tonic-gate rp->dn_value = (intmax_t)rp->dn_value >> n;
20220Sstevel@tonic-gate else
20230Sstevel@tonic-gate rp->dn_value = rp->dn_value >> n;
20240Sstevel@tonic-gate }
20250Sstevel@tonic-gate
20260Sstevel@tonic-gate dt_node_type_propagate(lp, rp);
20270Sstevel@tonic-gate dt_node_attr_assign(rp, dt_attr_min(lp->dn_attr, rp->dn_attr));
20280Sstevel@tonic-gate dt_node_free(lp);
20290Sstevel@tonic-gate
20300Sstevel@tonic-gate return (rp);
20310Sstevel@tonic-gate }
20320Sstevel@tonic-gate
20330Sstevel@tonic-gate /*
20340Sstevel@tonic-gate * If no immediate optimizations are available, create an new OP2 node
20350Sstevel@tonic-gate * and glue the left and right children into place and return.
20360Sstevel@tonic-gate */
20370Sstevel@tonic-gate dnp = dt_node_alloc(DT_NODE_OP2);
20380Sstevel@tonic-gate assert(op <= USHRT_MAX);
20390Sstevel@tonic-gate dnp->dn_op = (ushort_t)op;
20400Sstevel@tonic-gate dnp->dn_left = lp;
20410Sstevel@tonic-gate dnp->dn_right = rp;
20420Sstevel@tonic-gate
20430Sstevel@tonic-gate return (dnp);
20440Sstevel@tonic-gate }
20450Sstevel@tonic-gate
20460Sstevel@tonic-gate dt_node_t *
dt_node_op3(dt_node_t * expr,dt_node_t * lp,dt_node_t * rp)20470Sstevel@tonic-gate dt_node_op3(dt_node_t *expr, dt_node_t *lp, dt_node_t *rp)
20480Sstevel@tonic-gate {
20490Sstevel@tonic-gate dt_node_t *dnp;
20500Sstevel@tonic-gate
20510Sstevel@tonic-gate if (expr->dn_kind == DT_NODE_INT)
20520Sstevel@tonic-gate return (expr->dn_value != 0 ? lp : rp);
20530Sstevel@tonic-gate
20540Sstevel@tonic-gate dnp = dt_node_alloc(DT_NODE_OP3);
20550Sstevel@tonic-gate dnp->dn_op = DT_TOK_QUESTION;
20560Sstevel@tonic-gate dnp->dn_expr = expr;
20570Sstevel@tonic-gate dnp->dn_left = lp;
20580Sstevel@tonic-gate dnp->dn_right = rp;
20590Sstevel@tonic-gate
20600Sstevel@tonic-gate return (dnp);
20610Sstevel@tonic-gate }
20620Sstevel@tonic-gate
20630Sstevel@tonic-gate dt_node_t *
dt_node_statement(dt_node_t * expr)20640Sstevel@tonic-gate dt_node_statement(dt_node_t *expr)
20650Sstevel@tonic-gate {
20660Sstevel@tonic-gate dt_node_t *dnp;
20670Sstevel@tonic-gate
20680Sstevel@tonic-gate if (expr->dn_kind == DT_NODE_AGG)
20690Sstevel@tonic-gate return (expr);
20700Sstevel@tonic-gate
20710Sstevel@tonic-gate if (expr->dn_kind == DT_NODE_FUNC &&
20720Sstevel@tonic-gate expr->dn_ident->di_kind == DT_IDENT_ACTFUNC)
20730Sstevel@tonic-gate dnp = dt_node_alloc(DT_NODE_DFUNC);
20740Sstevel@tonic-gate else
20750Sstevel@tonic-gate dnp = dt_node_alloc(DT_NODE_DEXPR);
20760Sstevel@tonic-gate
20770Sstevel@tonic-gate dnp->dn_expr = expr;
20780Sstevel@tonic-gate return (dnp);
20790Sstevel@tonic-gate }
20800Sstevel@tonic-gate
20810Sstevel@tonic-gate dt_node_t *
dt_node_pdesc_by_name(char * spec)20820Sstevel@tonic-gate dt_node_pdesc_by_name(char *spec)
20830Sstevel@tonic-gate {
20840Sstevel@tonic-gate dtrace_hdl_t *dtp = yypcb->pcb_hdl;
20850Sstevel@tonic-gate dt_node_t *dnp;
20860Sstevel@tonic-gate
20870Sstevel@tonic-gate if (spec == NULL)
20880Sstevel@tonic-gate longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
20890Sstevel@tonic-gate
20900Sstevel@tonic-gate dnp = dt_node_alloc(DT_NODE_PDESC);
20910Sstevel@tonic-gate dnp->dn_spec = spec;
20920Sstevel@tonic-gate dnp->dn_desc = malloc(sizeof (dtrace_probedesc_t));
20930Sstevel@tonic-gate
20940Sstevel@tonic-gate if (dnp->dn_desc == NULL)
20950Sstevel@tonic-gate longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
20960Sstevel@tonic-gate
20970Sstevel@tonic-gate if (dtrace_xstr2desc(dtp, yypcb->pcb_pspec, dnp->dn_spec,
20980Sstevel@tonic-gate yypcb->pcb_sargc, yypcb->pcb_sargv, dnp->dn_desc) != 0) {
20990Sstevel@tonic-gate xyerror(D_PDESC_INVAL, "invalid probe description \"%s\": %s\n",
21000Sstevel@tonic-gate dnp->dn_spec, dtrace_errmsg(dtp, dtrace_errno(dtp)));
21010Sstevel@tonic-gate }
21020Sstevel@tonic-gate
21030Sstevel@tonic-gate free(dnp->dn_spec);
21040Sstevel@tonic-gate dnp->dn_spec = NULL;
21050Sstevel@tonic-gate
21060Sstevel@tonic-gate return (dnp);
21070Sstevel@tonic-gate }
21080Sstevel@tonic-gate
21090Sstevel@tonic-gate dt_node_t *
dt_node_pdesc_by_id(uintmax_t id)21100Sstevel@tonic-gate dt_node_pdesc_by_id(uintmax_t id)
21110Sstevel@tonic-gate {
21120Sstevel@tonic-gate static const char *const names[] = {
21130Sstevel@tonic-gate "providers", "modules", "functions"
21140Sstevel@tonic-gate };
21150Sstevel@tonic-gate
21160Sstevel@tonic-gate dtrace_hdl_t *dtp = yypcb->pcb_hdl;
21170Sstevel@tonic-gate dt_node_t *dnp = dt_node_alloc(DT_NODE_PDESC);
21180Sstevel@tonic-gate
21190Sstevel@tonic-gate if ((dnp->dn_desc = malloc(sizeof (dtrace_probedesc_t))) == NULL)
21200Sstevel@tonic-gate longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
21210Sstevel@tonic-gate
21220Sstevel@tonic-gate if (id > UINT_MAX) {
21230Sstevel@tonic-gate xyerror(D_PDESC_INVAL, "identifier %llu exceeds maximum "
21240Sstevel@tonic-gate "probe id\n", (u_longlong_t)id);
21250Sstevel@tonic-gate }
21260Sstevel@tonic-gate
21270Sstevel@tonic-gate if (yypcb->pcb_pspec != DTRACE_PROBESPEC_NAME) {
21280Sstevel@tonic-gate xyerror(D_PDESC_INVAL, "probe identifier %llu not permitted "
21290Sstevel@tonic-gate "when specifying %s\n", (u_longlong_t)id,
21300Sstevel@tonic-gate names[yypcb->pcb_pspec]);
21310Sstevel@tonic-gate }
21320Sstevel@tonic-gate
21330Sstevel@tonic-gate if (dtrace_id2desc(dtp, (dtrace_id_t)id, dnp->dn_desc) != 0) {
21340Sstevel@tonic-gate xyerror(D_PDESC_INVAL, "invalid probe identifier %llu: %s\n",
21350Sstevel@tonic-gate (u_longlong_t)id, dtrace_errmsg(dtp, dtrace_errno(dtp)));
21360Sstevel@tonic-gate }
21370Sstevel@tonic-gate
21380Sstevel@tonic-gate return (dnp);
21390Sstevel@tonic-gate }
21400Sstevel@tonic-gate
21410Sstevel@tonic-gate dt_node_t *
dt_node_clause(dt_node_t * pdescs,dt_node_t * pred,dt_node_t * acts)21420Sstevel@tonic-gate dt_node_clause(dt_node_t *pdescs, dt_node_t *pred, dt_node_t *acts)
21430Sstevel@tonic-gate {
21440Sstevel@tonic-gate dt_node_t *dnp = dt_node_alloc(DT_NODE_CLAUSE);
21450Sstevel@tonic-gate
21460Sstevel@tonic-gate dnp->dn_pdescs = pdescs;
21470Sstevel@tonic-gate dnp->dn_pred = pred;
21480Sstevel@tonic-gate dnp->dn_acts = acts;
21490Sstevel@tonic-gate
21500Sstevel@tonic-gate yybegin(YYS_CLAUSE);
21510Sstevel@tonic-gate return (dnp);
21520Sstevel@tonic-gate }
21530Sstevel@tonic-gate
21540Sstevel@tonic-gate dt_node_t *
dt_node_inline(dt_node_t * expr)21550Sstevel@tonic-gate dt_node_inline(dt_node_t *expr)
21560Sstevel@tonic-gate {
21570Sstevel@tonic-gate dtrace_hdl_t *dtp = yypcb->pcb_hdl;
21580Sstevel@tonic-gate dt_scope_t *dsp = &yypcb->pcb_dstack;
21590Sstevel@tonic-gate dt_decl_t *ddp = dt_decl_top();
21600Sstevel@tonic-gate
21610Sstevel@tonic-gate char n[DT_TYPE_NAMELEN];
21620Sstevel@tonic-gate dtrace_typeinfo_t dtt;
21630Sstevel@tonic-gate
21640Sstevel@tonic-gate dt_ident_t *idp, *rdp;
21650Sstevel@tonic-gate dt_idnode_t *inp;
21660Sstevel@tonic-gate dt_node_t *dnp;
21670Sstevel@tonic-gate
21680Sstevel@tonic-gate if (dt_decl_type(ddp, &dtt) != 0)
21690Sstevel@tonic-gate longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
21700Sstevel@tonic-gate
21710Sstevel@tonic-gate if (dsp->ds_class != DT_DC_DEFAULT) {
21720Sstevel@tonic-gate xyerror(D_DECL_BADCLASS, "specified storage class not "
21730Sstevel@tonic-gate "appropriate for inline declaration\n");
21740Sstevel@tonic-gate }
21750Sstevel@tonic-gate
21760Sstevel@tonic-gate if (dsp->ds_ident == NULL)
21770Sstevel@tonic-gate xyerror(D_DECL_USELESS, "inline declaration requires a name\n");
21780Sstevel@tonic-gate
21790Sstevel@tonic-gate if ((idp = dt_idstack_lookup(
21800Sstevel@tonic-gate &yypcb->pcb_globals, dsp->ds_ident)) != NULL) {
21810Sstevel@tonic-gate xyerror(D_DECL_IDRED, "identifier redefined: %s\n\t current: "
21820Sstevel@tonic-gate "inline definition\n\tprevious: %s %s\n",
21830Sstevel@tonic-gate idp->di_name, dt_idkind_name(idp->di_kind),
21840Sstevel@tonic-gate (idp->di_flags & DT_IDFLG_INLINE) ? "inline" : "");
21850Sstevel@tonic-gate }
21860Sstevel@tonic-gate
21870Sstevel@tonic-gate /*
21880Sstevel@tonic-gate * If we are declaring an inlined array, verify that we have a tuple
21890Sstevel@tonic-gate * signature, and then recompute 'dtt' as the array's value type.
21900Sstevel@tonic-gate */
21910Sstevel@tonic-gate if (ddp->dd_kind == CTF_K_ARRAY) {
21920Sstevel@tonic-gate if (ddp->dd_node == NULL) {
21930Sstevel@tonic-gate xyerror(D_DECL_ARRNULL, "inline declaration requires "
21940Sstevel@tonic-gate "array tuple signature: %s\n", dsp->ds_ident);
21950Sstevel@tonic-gate }
21960Sstevel@tonic-gate
21970Sstevel@tonic-gate if (ddp->dd_node->dn_kind != DT_NODE_TYPE) {
21980Sstevel@tonic-gate xyerror(D_DECL_ARRNULL, "inline declaration cannot be "
21990Sstevel@tonic-gate "of scalar array type: %s\n", dsp->ds_ident);
22000Sstevel@tonic-gate }
22010Sstevel@tonic-gate
22020Sstevel@tonic-gate if (dt_decl_type(ddp->dd_next, &dtt) != 0)
22030Sstevel@tonic-gate longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
22040Sstevel@tonic-gate }
22050Sstevel@tonic-gate
22060Sstevel@tonic-gate /*
22070Sstevel@tonic-gate * If the inline identifier is not defined, then create it with the
22080Sstevel@tonic-gate * orphan flag set. We do not insert the identifier into dt_globals
22090Sstevel@tonic-gate * until we have successfully cooked the right-hand expression, below.
22100Sstevel@tonic-gate */
22110Sstevel@tonic-gate dnp = dt_node_alloc(DT_NODE_INLINE);
22120Sstevel@tonic-gate dt_node_type_assign(dnp, dtt.dtt_ctfp, dtt.dtt_type);
22130Sstevel@tonic-gate dt_node_attr_assign(dnp, _dtrace_defattr);
22140Sstevel@tonic-gate
22150Sstevel@tonic-gate if (dt_node_is_void(dnp)) {
22160Sstevel@tonic-gate xyerror(D_DECL_VOIDOBJ,
22170Sstevel@tonic-gate "cannot declare void inline: %s\n", dsp->ds_ident);
22180Sstevel@tonic-gate }
22190Sstevel@tonic-gate
22200Sstevel@tonic-gate if (ctf_type_kind(dnp->dn_ctfp, ctf_type_resolve(
22210Sstevel@tonic-gate dnp->dn_ctfp, dnp->dn_type)) == CTF_K_FORWARD) {
22220Sstevel@tonic-gate xyerror(D_DECL_INCOMPLETE,
22230Sstevel@tonic-gate "incomplete struct/union/enum %s: %s\n",
22240Sstevel@tonic-gate dt_node_type_name(dnp, n, sizeof (n)), dsp->ds_ident);
22250Sstevel@tonic-gate }
22260Sstevel@tonic-gate
22270Sstevel@tonic-gate if ((inp = malloc(sizeof (dt_idnode_t))) == NULL)
22280Sstevel@tonic-gate longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
22290Sstevel@tonic-gate
22300Sstevel@tonic-gate bzero(inp, sizeof (dt_idnode_t));
22310Sstevel@tonic-gate
22320Sstevel@tonic-gate idp = dnp->dn_ident = dt_ident_create(dsp->ds_ident,
22330Sstevel@tonic-gate ddp->dd_kind == CTF_K_ARRAY ? DT_IDENT_ARRAY : DT_IDENT_SCALAR,
22340Sstevel@tonic-gate DT_IDFLG_INLINE | DT_IDFLG_REF | DT_IDFLG_DECL | DT_IDFLG_ORPHAN, 0,
22350Sstevel@tonic-gate _dtrace_defattr, 0, &dt_idops_inline, inp, dtp->dt_gen);
22360Sstevel@tonic-gate
22370Sstevel@tonic-gate if (idp == NULL) {
22380Sstevel@tonic-gate free(inp);
22390Sstevel@tonic-gate longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
22400Sstevel@tonic-gate }
22410Sstevel@tonic-gate
22420Sstevel@tonic-gate /*
22430Sstevel@tonic-gate * If we're inlining an associative array, create a private identifier
22440Sstevel@tonic-gate * hash containing the named parameters and store it in inp->din_hash.
22450Sstevel@tonic-gate * We then push this hash on to the top of the pcb_globals stack.
22460Sstevel@tonic-gate */
22470Sstevel@tonic-gate if (ddp->dd_kind == CTF_K_ARRAY) {
22480Sstevel@tonic-gate dt_idnode_t *pinp;
22490Sstevel@tonic-gate dt_ident_t *pidp;
22500Sstevel@tonic-gate dt_node_t *pnp;
22510Sstevel@tonic-gate uint_t i = 0;
22520Sstevel@tonic-gate
22530Sstevel@tonic-gate for (pnp = ddp->dd_node; pnp != NULL; pnp = pnp->dn_list)
22540Sstevel@tonic-gate i++; /* count up parameters for din_argv[] */
22550Sstevel@tonic-gate
22560Sstevel@tonic-gate inp->din_hash = dt_idhash_create("inline args", NULL, 0, 0);
22570Sstevel@tonic-gate inp->din_argv = calloc(i, sizeof (dt_ident_t *));
22580Sstevel@tonic-gate
22590Sstevel@tonic-gate if (inp->din_hash == NULL || inp->din_argv == NULL)
22600Sstevel@tonic-gate longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
22610Sstevel@tonic-gate
22620Sstevel@tonic-gate /*
22630Sstevel@tonic-gate * Create an identifier for each parameter as a scalar inline,
22640Sstevel@tonic-gate * and store it in din_hash and in position in din_argv[]. The
22650Sstevel@tonic-gate * parameter identifiers also use dt_idops_inline, but we leave
22660Sstevel@tonic-gate * the dt_idnode_t argument 'pinp' zeroed. This will be filled
22670Sstevel@tonic-gate * in by the code generation pass with references to the args.
22680Sstevel@tonic-gate */
22690Sstevel@tonic-gate for (i = 0, pnp = ddp->dd_node;
22700Sstevel@tonic-gate pnp != NULL; pnp = pnp->dn_list, i++) {
22710Sstevel@tonic-gate
22720Sstevel@tonic-gate if (pnp->dn_string == NULL)
22730Sstevel@tonic-gate continue; /* ignore anonymous parameters */
22740Sstevel@tonic-gate
22750Sstevel@tonic-gate if ((pinp = malloc(sizeof (dt_idnode_t))) == NULL)
22760Sstevel@tonic-gate longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
22770Sstevel@tonic-gate
22780Sstevel@tonic-gate pidp = dt_idhash_insert(inp->din_hash, pnp->dn_string,
22790Sstevel@tonic-gate DT_IDENT_SCALAR, DT_IDFLG_DECL | DT_IDFLG_INLINE, 0,
22800Sstevel@tonic-gate _dtrace_defattr, 0, &dt_idops_inline,
22810Sstevel@tonic-gate pinp, dtp->dt_gen);
22820Sstevel@tonic-gate
22830Sstevel@tonic-gate if (pidp == NULL) {
22840Sstevel@tonic-gate free(pinp);
22850Sstevel@tonic-gate longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
22860Sstevel@tonic-gate }
22870Sstevel@tonic-gate
22880Sstevel@tonic-gate inp->din_argv[i] = pidp;
22890Sstevel@tonic-gate bzero(pinp, sizeof (dt_idnode_t));
22900Sstevel@tonic-gate dt_ident_type_assign(pidp, pnp->dn_ctfp, pnp->dn_type);
22910Sstevel@tonic-gate }
22920Sstevel@tonic-gate
22930Sstevel@tonic-gate dt_idstack_push(&yypcb->pcb_globals, inp->din_hash);
22940Sstevel@tonic-gate }
22950Sstevel@tonic-gate
22960Sstevel@tonic-gate /*
22970Sstevel@tonic-gate * Unlike most constructors, we need to explicitly cook the right-hand
22980Sstevel@tonic-gate * side of the inline definition immediately to prevent recursion. If
22990Sstevel@tonic-gate * the right-hand side uses the inline itself, the cook will fail.
23000Sstevel@tonic-gate */
23010Sstevel@tonic-gate expr = dt_node_cook(expr, DT_IDFLG_REF);
23020Sstevel@tonic-gate
23030Sstevel@tonic-gate if (ddp->dd_kind == CTF_K_ARRAY)
23040Sstevel@tonic-gate dt_idstack_pop(&yypcb->pcb_globals, inp->din_hash);
23050Sstevel@tonic-gate
23060Sstevel@tonic-gate /*
23070Sstevel@tonic-gate * Set the type, attributes, and flags for the inline. If the right-
23080Sstevel@tonic-gate * hand expression has an identifier, propagate its flags. Then cook
23090Sstevel@tonic-gate * the identifier to fully initialize it: if we're declaring an inline
23100Sstevel@tonic-gate * associative array this will construct a type signature from 'ddp'.
23110Sstevel@tonic-gate */
23120Sstevel@tonic-gate if (dt_node_is_dynamic(expr))
23130Sstevel@tonic-gate rdp = dt_ident_resolve(expr->dn_ident);
23140Sstevel@tonic-gate else if (expr->dn_kind == DT_NODE_VAR || expr->dn_kind == DT_NODE_SYM)
23150Sstevel@tonic-gate rdp = expr->dn_ident;
23160Sstevel@tonic-gate else
23170Sstevel@tonic-gate rdp = NULL;
23180Sstevel@tonic-gate
23190Sstevel@tonic-gate if (rdp != NULL) {
23200Sstevel@tonic-gate idp->di_flags |= (rdp->di_flags &
23210Sstevel@tonic-gate (DT_IDFLG_WRITE | DT_IDFLG_USER | DT_IDFLG_PRIM));
23220Sstevel@tonic-gate }
23230Sstevel@tonic-gate
23240Sstevel@tonic-gate idp->di_attr = dt_attr_min(_dtrace_defattr, expr->dn_attr);
23250Sstevel@tonic-gate dt_ident_type_assign(idp, dtt.dtt_ctfp, dtt.dtt_type);
23260Sstevel@tonic-gate (void) dt_ident_cook(dnp, idp, &ddp->dd_node);
23270Sstevel@tonic-gate
23280Sstevel@tonic-gate /*
23290Sstevel@tonic-gate * Store the parse tree nodes for 'expr' inside of idp->di_data ('inp')
23300Sstevel@tonic-gate * so that they will be preserved with this identifier. Then pop the
23310Sstevel@tonic-gate * inline declaration from the declaration stack and restore the lexer.
23320Sstevel@tonic-gate */
23330Sstevel@tonic-gate inp->din_list = yypcb->pcb_list;
23340Sstevel@tonic-gate inp->din_root = expr;
23350Sstevel@tonic-gate
23360Sstevel@tonic-gate dt_decl_free(dt_decl_pop());
23370Sstevel@tonic-gate yybegin(YYS_CLAUSE);
23380Sstevel@tonic-gate
23390Sstevel@tonic-gate /*
23400Sstevel@tonic-gate * Finally, insert the inline identifier into dt_globals to make it
23410Sstevel@tonic-gate * visible, and then cook 'dnp' to check its type against 'expr'.
23420Sstevel@tonic-gate */
23430Sstevel@tonic-gate dt_idhash_xinsert(dtp->dt_globals, idp);
23440Sstevel@tonic-gate return (dt_node_cook(dnp, DT_IDFLG_REF));
23450Sstevel@tonic-gate }
23460Sstevel@tonic-gate
23470Sstevel@tonic-gate dt_node_t *
dt_node_member(dt_decl_t * ddp,char * name,dt_node_t * expr)23480Sstevel@tonic-gate dt_node_member(dt_decl_t *ddp, char *name, dt_node_t *expr)
23490Sstevel@tonic-gate {
23500Sstevel@tonic-gate dtrace_typeinfo_t dtt;
23510Sstevel@tonic-gate dt_node_t *dnp;
23520Sstevel@tonic-gate int err;
23530Sstevel@tonic-gate
23540Sstevel@tonic-gate if (ddp != NULL) {
23550Sstevel@tonic-gate err = dt_decl_type(ddp, &dtt);
23560Sstevel@tonic-gate dt_decl_free(ddp);
23570Sstevel@tonic-gate
23580Sstevel@tonic-gate if (err != 0)
23590Sstevel@tonic-gate longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
23600Sstevel@tonic-gate }
23610Sstevel@tonic-gate
23620Sstevel@tonic-gate dnp = dt_node_alloc(DT_NODE_MEMBER);
23630Sstevel@tonic-gate dnp->dn_membname = name;
23640Sstevel@tonic-gate dnp->dn_membexpr = expr;
23650Sstevel@tonic-gate
23660Sstevel@tonic-gate if (ddp != NULL)
23670Sstevel@tonic-gate dt_node_type_assign(dnp, dtt.dtt_ctfp, dtt.dtt_type);
23680Sstevel@tonic-gate
23690Sstevel@tonic-gate return (dnp);
23700Sstevel@tonic-gate }
23710Sstevel@tonic-gate
23720Sstevel@tonic-gate dt_node_t *
dt_node_xlator(dt_decl_t * ddp,dt_decl_t * sdp,char * name,dt_node_t * members)23730Sstevel@tonic-gate dt_node_xlator(dt_decl_t *ddp, dt_decl_t *sdp, char *name, dt_node_t *members)
23740Sstevel@tonic-gate {
23750Sstevel@tonic-gate dtrace_hdl_t *dtp = yypcb->pcb_hdl;
23760Sstevel@tonic-gate dtrace_typeinfo_t src, dst;
23770Sstevel@tonic-gate dt_node_t sn, dn;
23780Sstevel@tonic-gate dt_xlator_t *dxp;
23790Sstevel@tonic-gate dt_node_t *dnp;
23800Sstevel@tonic-gate int edst, esrc;
2381265Smws uint_t kind;
23820Sstevel@tonic-gate
23830Sstevel@tonic-gate char n1[DT_TYPE_NAMELEN];
23840Sstevel@tonic-gate char n2[DT_TYPE_NAMELEN];
23850Sstevel@tonic-gate
23860Sstevel@tonic-gate edst = dt_decl_type(ddp, &dst);
23870Sstevel@tonic-gate dt_decl_free(ddp);
23880Sstevel@tonic-gate
23890Sstevel@tonic-gate esrc = dt_decl_type(sdp, &src);
23900Sstevel@tonic-gate dt_decl_free(sdp);
23910Sstevel@tonic-gate
23920Sstevel@tonic-gate if (edst != 0 || esrc != 0) {
23930Sstevel@tonic-gate free(name);
23940Sstevel@tonic-gate longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
23950Sstevel@tonic-gate }
23960Sstevel@tonic-gate
23970Sstevel@tonic-gate bzero(&sn, sizeof (sn));
23980Sstevel@tonic-gate dt_node_type_assign(&sn, src.dtt_ctfp, src.dtt_type);
23990Sstevel@tonic-gate
24000Sstevel@tonic-gate bzero(&dn, sizeof (dn));
24010Sstevel@tonic-gate dt_node_type_assign(&dn, dst.dtt_ctfp, dst.dtt_type);
24020Sstevel@tonic-gate
24030Sstevel@tonic-gate if (dt_xlator_lookup(dtp, &sn, &dn, DT_XLATE_EXACT) != NULL) {
24040Sstevel@tonic-gate xyerror(D_XLATE_REDECL,
24050Sstevel@tonic-gate "translator from %s to %s has already been declared\n",
24060Sstevel@tonic-gate dt_node_type_name(&sn, n1, sizeof (n1)),
24070Sstevel@tonic-gate dt_node_type_name(&dn, n2, sizeof (n2)));
24080Sstevel@tonic-gate }
24090Sstevel@tonic-gate
2410265Smws kind = ctf_type_kind(dst.dtt_ctfp,
2411265Smws ctf_type_resolve(dst.dtt_ctfp, dst.dtt_type));
2412265Smws
2413265Smws if (kind == CTF_K_FORWARD) {
2414265Smws xyerror(D_XLATE_SOU, "incomplete struct/union/enum %s\n",
2415265Smws dt_type_name(dst.dtt_ctfp, dst.dtt_type, n1, sizeof (n1)));
2416265Smws }
2417265Smws
2418265Smws if (kind != CTF_K_STRUCT && kind != CTF_K_UNION) {
2419265Smws xyerror(D_XLATE_SOU,
2420265Smws "translator output type must be a struct or union\n");
2421265Smws }
2422265Smws
24230Sstevel@tonic-gate dxp = dt_xlator_create(dtp, &src, &dst, name, members, yypcb->pcb_list);
24240Sstevel@tonic-gate yybegin(YYS_CLAUSE);
24250Sstevel@tonic-gate free(name);
24260Sstevel@tonic-gate
24270Sstevel@tonic-gate if (dxp == NULL)
24280Sstevel@tonic-gate longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
24290Sstevel@tonic-gate
24300Sstevel@tonic-gate dnp = dt_node_alloc(DT_NODE_XLATOR);
24310Sstevel@tonic-gate dnp->dn_xlator = dxp;
24320Sstevel@tonic-gate dnp->dn_members = members;
24330Sstevel@tonic-gate
24340Sstevel@tonic-gate return (dt_node_cook(dnp, DT_IDFLG_REF));
24350Sstevel@tonic-gate }
24360Sstevel@tonic-gate
24370Sstevel@tonic-gate dt_node_t *
dt_node_probe(char * s,int protoc,dt_node_t * nargs,dt_node_t * xargs)2438265Smws dt_node_probe(char *s, int protoc, dt_node_t *nargs, dt_node_t *xargs)
24390Sstevel@tonic-gate {
24400Sstevel@tonic-gate dtrace_hdl_t *dtp = yypcb->pcb_hdl;
24410Sstevel@tonic-gate int nargc, xargc;
24420Sstevel@tonic-gate dt_node_t *dnp;
24430Sstevel@tonic-gate
24440Sstevel@tonic-gate size_t len = strlen(s) + 3; /* +3 for :: and \0 */
24450Sstevel@tonic-gate char *name = alloca(len);
24460Sstevel@tonic-gate
24470Sstevel@tonic-gate (void) snprintf(name, len, "::%s", s);
24480Sstevel@tonic-gate (void) strhyphenate(name);
24490Sstevel@tonic-gate free(s);
24500Sstevel@tonic-gate
24510Sstevel@tonic-gate if (strchr(name, '`') != NULL) {
24520Sstevel@tonic-gate xyerror(D_PROV_BADNAME, "probe name may not "
24530Sstevel@tonic-gate "contain scoping operator: %s\n", name);
24540Sstevel@tonic-gate }
24550Sstevel@tonic-gate
24560Sstevel@tonic-gate if (strlen(name) - 2 >= DTRACE_NAMELEN) {
24570Sstevel@tonic-gate xyerror(D_PROV_BADNAME, "probe name may not exceed %d "
24580Sstevel@tonic-gate "characters: %s\n", DTRACE_NAMELEN - 1, name);
24590Sstevel@tonic-gate }
24600Sstevel@tonic-gate
24610Sstevel@tonic-gate dnp = dt_node_alloc(DT_NODE_PROBE);
24620Sstevel@tonic-gate
24630Sstevel@tonic-gate dnp->dn_ident = dt_ident_create(name, DT_IDENT_PROBE,
24640Sstevel@tonic-gate DT_IDFLG_ORPHAN, DTRACE_IDNONE, _dtrace_defattr, 0,
24650Sstevel@tonic-gate &dt_idops_probe, NULL, dtp->dt_gen);
24660Sstevel@tonic-gate
24670Sstevel@tonic-gate nargc = dt_decl_prototype(nargs, nargs,
24680Sstevel@tonic-gate "probe input", DT_DP_VOID | DT_DP_ANON);
24690Sstevel@tonic-gate
24700Sstevel@tonic-gate xargc = dt_decl_prototype(xargs, nargs,
24710Sstevel@tonic-gate "probe output", DT_DP_VOID);
24720Sstevel@tonic-gate
24730Sstevel@tonic-gate if (nargc > UINT8_MAX) {
24740Sstevel@tonic-gate xyerror(D_PROV_PRARGLEN, "probe %s input prototype exceeds %u "
24750Sstevel@tonic-gate "parameters: %d params used\n", name, UINT8_MAX, nargc);
24760Sstevel@tonic-gate }
24770Sstevel@tonic-gate
24780Sstevel@tonic-gate if (xargc > UINT8_MAX) {
24790Sstevel@tonic-gate xyerror(D_PROV_PRARGLEN, "probe %s output prototype exceeds %u "
24800Sstevel@tonic-gate "parameters: %d params used\n", name, UINT8_MAX, xargc);
24810Sstevel@tonic-gate }
24820Sstevel@tonic-gate
24830Sstevel@tonic-gate if (dnp->dn_ident == NULL || dt_probe_create(dtp,
2484265Smws dnp->dn_ident, protoc, nargs, nargc, xargs, xargc) == NULL)
24850Sstevel@tonic-gate longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
24860Sstevel@tonic-gate
24870Sstevel@tonic-gate return (dnp);
24880Sstevel@tonic-gate }
24890Sstevel@tonic-gate
24900Sstevel@tonic-gate dt_node_t *
dt_node_provider(char * name,dt_node_t * probes)24910Sstevel@tonic-gate dt_node_provider(char *name, dt_node_t *probes)
24920Sstevel@tonic-gate {
24930Sstevel@tonic-gate dtrace_hdl_t *dtp = yypcb->pcb_hdl;
24940Sstevel@tonic-gate dt_node_t *dnp = dt_node_alloc(DT_NODE_PROVIDER);
2495265Smws dt_node_t *lnp;
24961399Sahl size_t len;
24970Sstevel@tonic-gate
24980Sstevel@tonic-gate dnp->dn_provname = name;
24990Sstevel@tonic-gate dnp->dn_probes = probes;
25000Sstevel@tonic-gate
25010Sstevel@tonic-gate if (strchr(name, '`') != NULL) {
25020Sstevel@tonic-gate dnerror(dnp, D_PROV_BADNAME, "provider name may not "
25030Sstevel@tonic-gate "contain scoping operator: %s\n", name);
25040Sstevel@tonic-gate }
25050Sstevel@tonic-gate
25061399Sahl if ((len = strlen(name)) >= DTRACE_PROVNAMELEN) {
25070Sstevel@tonic-gate dnerror(dnp, D_PROV_BADNAME, "provider name may not exceed %d "
25080Sstevel@tonic-gate "characters: %s\n", DTRACE_PROVNAMELEN - 1, name);
25090Sstevel@tonic-gate }
25100Sstevel@tonic-gate
25111399Sahl if (isdigit(name[len - 1])) {
25121399Sahl dnerror(dnp, D_PROV_BADNAME, "provider name may not "
25131399Sahl "end with a digit: %s\n", name);
25141399Sahl }
25151399Sahl
25160Sstevel@tonic-gate /*
25170Sstevel@tonic-gate * Check to see if the provider is already defined or visible through
25180Sstevel@tonic-gate * dtrace(7D). If so, set dn_provred to treat it as a re-declaration.
25190Sstevel@tonic-gate * If not, create a new provider and set its interface-only flag. This
25200Sstevel@tonic-gate * flag may be cleared later by calls made to dt_probe_declare().
25210Sstevel@tonic-gate */
25220Sstevel@tonic-gate if ((dnp->dn_provider = dt_provider_lookup(dtp, name)) != NULL)
25230Sstevel@tonic-gate dnp->dn_provred = B_TRUE;
25240Sstevel@tonic-gate else if ((dnp->dn_provider = dt_provider_create(dtp, name)) == NULL)
25250Sstevel@tonic-gate longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
25260Sstevel@tonic-gate else
25270Sstevel@tonic-gate dnp->dn_provider->pv_flags |= DT_PROVIDER_INTF;
25280Sstevel@tonic-gate
25290Sstevel@tonic-gate /*
2530265Smws * Store all parse nodes created since we consumed the DT_KEY_PROVIDER
25310Sstevel@tonic-gate * token with the provider and then restore our lexing state to CLAUSE.
2532265Smws * Note that if dnp->dn_provred is true, we may end up storing dups of
2533265Smws * a provider's interface and implementation: we eat this space because
2534265Smws * the implementation will likely need to redeclare probe members, and
2535265Smws * therefore may result in those member nodes becoming persistent.
25360Sstevel@tonic-gate */
2537265Smws for (lnp = yypcb->pcb_list; lnp->dn_link != NULL; lnp = lnp->dn_link)
2538265Smws continue; /* skip to end of allocation list */
2539265Smws
2540265Smws lnp->dn_link = dnp->dn_provider->pv_nodes;
2541265Smws dnp->dn_provider->pv_nodes = yypcb->pcb_list;
25420Sstevel@tonic-gate
25430Sstevel@tonic-gate yybegin(YYS_CLAUSE);
25440Sstevel@tonic-gate return (dnp);
25450Sstevel@tonic-gate }
25460Sstevel@tonic-gate
25470Sstevel@tonic-gate dt_node_t *
dt_node_program(dt_node_t * lnp)25480Sstevel@tonic-gate dt_node_program(dt_node_t *lnp)
25490Sstevel@tonic-gate {
25500Sstevel@tonic-gate dt_node_t *dnp = dt_node_alloc(DT_NODE_PROG);
25510Sstevel@tonic-gate dnp->dn_list = lnp;
25520Sstevel@tonic-gate return (dnp);
25530Sstevel@tonic-gate }
25540Sstevel@tonic-gate
25550Sstevel@tonic-gate /*
25560Sstevel@tonic-gate * This function provides the underlying implementation of cooking an
25570Sstevel@tonic-gate * identifier given its node, a hash of dynamic identifiers, an identifier
25580Sstevel@tonic-gate * kind, and a boolean flag indicating whether we are allowed to instantiate
25590Sstevel@tonic-gate * a new identifier if the string is not found. This function is either
25600Sstevel@tonic-gate * called from dt_cook_ident(), below, or directly by the various cooking
25610Sstevel@tonic-gate * routines that are allowed to instantiate identifiers (e.g. op2 TOK_ASGN).
25620Sstevel@tonic-gate */
25630Sstevel@tonic-gate static void
dt_xcook_ident(dt_node_t * dnp,dt_idhash_t * dhp,uint_t idkind,int create)25640Sstevel@tonic-gate dt_xcook_ident(dt_node_t *dnp, dt_idhash_t *dhp, uint_t idkind, int create)
25650Sstevel@tonic-gate {
25660Sstevel@tonic-gate dtrace_hdl_t *dtp = yypcb->pcb_hdl;
25670Sstevel@tonic-gate const char *sname = dt_idhash_name(dhp);
25680Sstevel@tonic-gate int uref = 0;
25690Sstevel@tonic-gate
25700Sstevel@tonic-gate dtrace_attribute_t attr = _dtrace_defattr;
25710Sstevel@tonic-gate dt_ident_t *idp;
25720Sstevel@tonic-gate dtrace_syminfo_t dts;
25730Sstevel@tonic-gate GElf_Sym sym;
25740Sstevel@tonic-gate
25750Sstevel@tonic-gate const char *scope, *mark;
25760Sstevel@tonic-gate uchar_t dnkind;
25770Sstevel@tonic-gate char *name;
25780Sstevel@tonic-gate
25790Sstevel@tonic-gate /*
25800Sstevel@tonic-gate * Look for scoping marks in the identifier. If one is found, set our
25810Sstevel@tonic-gate * scope to either DTRACE_OBJ_KMODS or UMODS or to the first part of
25820Sstevel@tonic-gate * the string that specifies the scope using an explicit module name.
25830Sstevel@tonic-gate * If two marks in a row are found, set 'uref' (user symbol reference).
25840Sstevel@tonic-gate * Otherwise we set scope to DTRACE_OBJ_EXEC, indicating that normal
25850Sstevel@tonic-gate * scope is desired and we should search the specified idhash.
25860Sstevel@tonic-gate */
25870Sstevel@tonic-gate if ((name = strrchr(dnp->dn_string, '`')) != NULL) {
25880Sstevel@tonic-gate if (name > dnp->dn_string && name[-1] == '`') {
25890Sstevel@tonic-gate uref++;
25900Sstevel@tonic-gate name[-1] = '\0';
25910Sstevel@tonic-gate }
25920Sstevel@tonic-gate
25930Sstevel@tonic-gate if (name == dnp->dn_string + uref)
25940Sstevel@tonic-gate scope = uref ? DTRACE_OBJ_UMODS : DTRACE_OBJ_KMODS;
25950Sstevel@tonic-gate else
25960Sstevel@tonic-gate scope = dnp->dn_string;
25970Sstevel@tonic-gate
25980Sstevel@tonic-gate *name++ = '\0'; /* leave name pointing after scoping mark */
25990Sstevel@tonic-gate dnkind = DT_NODE_VAR;
26000Sstevel@tonic-gate
26010Sstevel@tonic-gate } else if (idkind == DT_IDENT_AGG) {
26020Sstevel@tonic-gate scope = DTRACE_OBJ_EXEC;
26030Sstevel@tonic-gate name = dnp->dn_string + 1;
26040Sstevel@tonic-gate dnkind = DT_NODE_AGG;
26050Sstevel@tonic-gate } else {
26060Sstevel@tonic-gate scope = DTRACE_OBJ_EXEC;
26070Sstevel@tonic-gate name = dnp->dn_string;
26080Sstevel@tonic-gate dnkind = DT_NODE_VAR;
26090Sstevel@tonic-gate }
26100Sstevel@tonic-gate
26110Sstevel@tonic-gate /*
26120Sstevel@tonic-gate * If create is set to false, and we fail our idhash lookup, preset
26130Sstevel@tonic-gate * the errno code to EDT_NOVAR for our final error message below.
26140Sstevel@tonic-gate * If we end up calling dtrace_lookup_by_name(), it will reset the
26150Sstevel@tonic-gate * errno appropriately and that error will be reported instead.
26160Sstevel@tonic-gate */
26170Sstevel@tonic-gate (void) dt_set_errno(dtp, EDT_NOVAR);
26180Sstevel@tonic-gate mark = uref ? "``" : "`";
26190Sstevel@tonic-gate
26200Sstevel@tonic-gate if (scope == DTRACE_OBJ_EXEC && (
26210Sstevel@tonic-gate (dhp != dtp->dt_globals &&
26220Sstevel@tonic-gate (idp = dt_idhash_lookup(dhp, name)) != NULL) ||
26230Sstevel@tonic-gate (dhp == dtp->dt_globals &&
26240Sstevel@tonic-gate (idp = dt_idstack_lookup(&yypcb->pcb_globals, name)) != NULL))) {
26250Sstevel@tonic-gate /*
26260Sstevel@tonic-gate * Check that we are referencing the ident in the manner that
26270Sstevel@tonic-gate * matches its type if this is a global lookup. In the TLS or
26280Sstevel@tonic-gate * local case, we don't know how the ident will be used until
26290Sstevel@tonic-gate * the time operator -> is seen; more parsing is needed.
26300Sstevel@tonic-gate */
26310Sstevel@tonic-gate if (idp->di_kind != idkind && dhp == dtp->dt_globals) {
26320Sstevel@tonic-gate xyerror(D_IDENT_BADREF, "%s '%s' may not be referenced "
26330Sstevel@tonic-gate "as %s\n", dt_idkind_name(idp->di_kind),
26340Sstevel@tonic-gate idp->di_name, dt_idkind_name(idkind));
26350Sstevel@tonic-gate }
26360Sstevel@tonic-gate
26370Sstevel@tonic-gate /*
26380Sstevel@tonic-gate * Arrays and aggregations are not cooked individually. They
26390Sstevel@tonic-gate * have dynamic types and must be referenced using operator [].
26400Sstevel@tonic-gate * This is handled explicitly by the code for DT_TOK_LBRAC.
26410Sstevel@tonic-gate */
26420Sstevel@tonic-gate if (idp->di_kind != DT_IDENT_ARRAY &&
26430Sstevel@tonic-gate idp->di_kind != DT_IDENT_AGG)
26440Sstevel@tonic-gate attr = dt_ident_cook(dnp, idp, NULL);
26450Sstevel@tonic-gate else {
26460Sstevel@tonic-gate dt_node_type_assign(dnp,
26470Sstevel@tonic-gate DT_DYN_CTFP(dtp), DT_DYN_TYPE(dtp));
26480Sstevel@tonic-gate attr = idp->di_attr;
26490Sstevel@tonic-gate }
26500Sstevel@tonic-gate
26510Sstevel@tonic-gate free(dnp->dn_string);
26520Sstevel@tonic-gate dnp->dn_string = NULL;
26530Sstevel@tonic-gate dnp->dn_kind = dnkind;
26540Sstevel@tonic-gate dnp->dn_ident = idp;
26550Sstevel@tonic-gate dnp->dn_flags |= DT_NF_LVALUE;
26560Sstevel@tonic-gate
26570Sstevel@tonic-gate if (idp->di_flags & DT_IDFLG_WRITE)
26580Sstevel@tonic-gate dnp->dn_flags |= DT_NF_WRITABLE;
26590Sstevel@tonic-gate
26600Sstevel@tonic-gate dt_node_attr_assign(dnp, attr);
26610Sstevel@tonic-gate
26620Sstevel@tonic-gate } else if (dhp == dtp->dt_globals && scope != DTRACE_OBJ_EXEC &&
26630Sstevel@tonic-gate dtrace_lookup_by_name(dtp, scope, name, &sym, &dts) == 0) {
26640Sstevel@tonic-gate
26650Sstevel@tonic-gate dt_module_t *mp = dt_module_lookup_by_name(dtp, dts.dts_object);
26660Sstevel@tonic-gate int umod = (mp->dm_flags & DT_DM_KERNEL) == 0;
26670Sstevel@tonic-gate static const char *const kunames[] = { "kernel", "user" };
26680Sstevel@tonic-gate
26690Sstevel@tonic-gate dtrace_typeinfo_t dtt;
26700Sstevel@tonic-gate dtrace_syminfo_t *sip;
26710Sstevel@tonic-gate
26720Sstevel@tonic-gate if (uref ^ umod) {
26730Sstevel@tonic-gate xyerror(D_SYM_BADREF, "%s module '%s' symbol '%s' may "
26740Sstevel@tonic-gate "not be referenced as a %s symbol\n", kunames[umod],
26750Sstevel@tonic-gate dts.dts_object, dts.dts_name, kunames[uref]);
26760Sstevel@tonic-gate }
26770Sstevel@tonic-gate
26780Sstevel@tonic-gate if (dtrace_symbol_type(dtp, &sym, &dts, &dtt) != 0) {
26790Sstevel@tonic-gate /*
26800Sstevel@tonic-gate * For now, we special-case EDT_DATAMODEL to clarify
26810Sstevel@tonic-gate * that mixed data models are not currently supported.
26820Sstevel@tonic-gate */
26830Sstevel@tonic-gate if (dtp->dt_errno == EDT_DATAMODEL) {
26840Sstevel@tonic-gate xyerror(D_SYM_MODEL, "cannot use %s symbol "
26850Sstevel@tonic-gate "%s%s%s in a %s D program\n",
26860Sstevel@tonic-gate dt_module_modelname(mp),
26870Sstevel@tonic-gate dts.dts_object, mark, dts.dts_name,
26880Sstevel@tonic-gate dt_module_modelname(dtp->dt_ddefs));
26890Sstevel@tonic-gate }
26900Sstevel@tonic-gate
26910Sstevel@tonic-gate xyerror(D_SYM_NOTYPES,
26920Sstevel@tonic-gate "no symbolic type information is available for "
26930Sstevel@tonic-gate "%s%s%s: %s\n", dts.dts_object, mark, dts.dts_name,
26940Sstevel@tonic-gate dtrace_errmsg(dtp, dtrace_errno(dtp)));
26950Sstevel@tonic-gate }
26960Sstevel@tonic-gate
26970Sstevel@tonic-gate idp = dt_ident_create(name, DT_IDENT_SYMBOL, 0, 0,
26980Sstevel@tonic-gate _dtrace_symattr, 0, &dt_idops_thaw, NULL, dtp->dt_gen);
26990Sstevel@tonic-gate
27000Sstevel@tonic-gate if (idp == NULL)
27010Sstevel@tonic-gate longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
27020Sstevel@tonic-gate
27030Sstevel@tonic-gate if (mp->dm_flags & DT_DM_PRIMARY)
27040Sstevel@tonic-gate idp->di_flags |= DT_IDFLG_PRIM;
27050Sstevel@tonic-gate
27060Sstevel@tonic-gate idp->di_next = dtp->dt_externs;
27070Sstevel@tonic-gate dtp->dt_externs = idp;
27080Sstevel@tonic-gate
27090Sstevel@tonic-gate if ((sip = malloc(sizeof (dtrace_syminfo_t))) == NULL)
27100Sstevel@tonic-gate longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
27110Sstevel@tonic-gate
27120Sstevel@tonic-gate bcopy(&dts, sip, sizeof (dtrace_syminfo_t));
27130Sstevel@tonic-gate idp->di_data = sip;
27140Sstevel@tonic-gate idp->di_ctfp = dtt.dtt_ctfp;
27150Sstevel@tonic-gate idp->di_type = dtt.dtt_type;
27160Sstevel@tonic-gate
27170Sstevel@tonic-gate free(dnp->dn_string);
27180Sstevel@tonic-gate dnp->dn_string = NULL;
27190Sstevel@tonic-gate dnp->dn_kind = DT_NODE_SYM;
27200Sstevel@tonic-gate dnp->dn_ident = idp;
27210Sstevel@tonic-gate dnp->dn_flags |= DT_NF_LVALUE;
27220Sstevel@tonic-gate
27230Sstevel@tonic-gate dt_node_type_assign(dnp, dtt.dtt_ctfp, dtt.dtt_type);
27240Sstevel@tonic-gate dt_node_attr_assign(dnp, _dtrace_symattr);
27250Sstevel@tonic-gate
27260Sstevel@tonic-gate if (uref) {
27270Sstevel@tonic-gate idp->di_flags |= DT_IDFLG_USER;
27280Sstevel@tonic-gate dnp->dn_flags |= DT_NF_USERLAND;
27290Sstevel@tonic-gate }
27300Sstevel@tonic-gate
27310Sstevel@tonic-gate } else if (scope == DTRACE_OBJ_EXEC && create == B_TRUE) {
27320Sstevel@tonic-gate uint_t flags = DT_IDFLG_WRITE;
27330Sstevel@tonic-gate uint_t id;
27340Sstevel@tonic-gate
27350Sstevel@tonic-gate if (dt_idhash_nextid(dhp, &id) == -1) {
27360Sstevel@tonic-gate xyerror(D_ID_OFLOW, "cannot create %s: limit on number "
27370Sstevel@tonic-gate "of %s variables exceeded\n", name, sname);
27380Sstevel@tonic-gate }
27390Sstevel@tonic-gate
27400Sstevel@tonic-gate if (dhp == yypcb->pcb_locals)
27410Sstevel@tonic-gate flags |= DT_IDFLG_LOCAL;
27420Sstevel@tonic-gate else if (dhp == dtp->dt_tls)
27430Sstevel@tonic-gate flags |= DT_IDFLG_TLS;
27440Sstevel@tonic-gate
27450Sstevel@tonic-gate dt_dprintf("create %s %s variable %s, id=%u\n",
27460Sstevel@tonic-gate sname, dt_idkind_name(idkind), name, id);
27470Sstevel@tonic-gate
27480Sstevel@tonic-gate if (idkind == DT_IDENT_ARRAY || idkind == DT_IDENT_AGG) {
27490Sstevel@tonic-gate idp = dt_idhash_insert(dhp, name,
27500Sstevel@tonic-gate idkind, flags, id, _dtrace_defattr, 0,
27510Sstevel@tonic-gate &dt_idops_assc, NULL, dtp->dt_gen);
27520Sstevel@tonic-gate } else {
27530Sstevel@tonic-gate idp = dt_idhash_insert(dhp, name,
27540Sstevel@tonic-gate idkind, flags, id, _dtrace_defattr, 0,
27550Sstevel@tonic-gate &dt_idops_thaw, NULL, dtp->dt_gen);
27560Sstevel@tonic-gate }
27570Sstevel@tonic-gate
27580Sstevel@tonic-gate if (idp == NULL)
27590Sstevel@tonic-gate longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
27600Sstevel@tonic-gate
27610Sstevel@tonic-gate /*
27620Sstevel@tonic-gate * Arrays and aggregations are not cooked individually. They
27630Sstevel@tonic-gate * have dynamic types and must be referenced using operator [].
27640Sstevel@tonic-gate * This is handled explicitly by the code for DT_TOK_LBRAC.
27650Sstevel@tonic-gate */
27660Sstevel@tonic-gate if (idp->di_kind != DT_IDENT_ARRAY &&
27670Sstevel@tonic-gate idp->di_kind != DT_IDENT_AGG)
27680Sstevel@tonic-gate attr = dt_ident_cook(dnp, idp, NULL);
27690Sstevel@tonic-gate else {
27700Sstevel@tonic-gate dt_node_type_assign(dnp,
27710Sstevel@tonic-gate DT_DYN_CTFP(dtp), DT_DYN_TYPE(dtp));
27720Sstevel@tonic-gate attr = idp->di_attr;
27730Sstevel@tonic-gate }
27740Sstevel@tonic-gate
27750Sstevel@tonic-gate free(dnp->dn_string);
27760Sstevel@tonic-gate dnp->dn_string = NULL;
27770Sstevel@tonic-gate dnp->dn_kind = dnkind;
27780Sstevel@tonic-gate dnp->dn_ident = idp;
27790Sstevel@tonic-gate dnp->dn_flags |= DT_NF_LVALUE | DT_NF_WRITABLE;
27800Sstevel@tonic-gate
27810Sstevel@tonic-gate dt_node_attr_assign(dnp, attr);
27820Sstevel@tonic-gate
27830Sstevel@tonic-gate } else if (scope != DTRACE_OBJ_EXEC) {
27840Sstevel@tonic-gate xyerror(D_IDENT_UNDEF, "failed to resolve %s%s%s: %s\n",
27850Sstevel@tonic-gate dnp->dn_string, mark, name,
27860Sstevel@tonic-gate dtrace_errmsg(dtp, dtrace_errno(dtp)));
27870Sstevel@tonic-gate } else {
27880Sstevel@tonic-gate xyerror(D_IDENT_UNDEF, "failed to resolve %s: %s\n",
27890Sstevel@tonic-gate dnp->dn_string, dtrace_errmsg(dtp, dtrace_errno(dtp)));
27900Sstevel@tonic-gate }
27910Sstevel@tonic-gate }
27920Sstevel@tonic-gate
27930Sstevel@tonic-gate static dt_node_t *
dt_cook_ident(dt_node_t * dnp,uint_t idflags)27940Sstevel@tonic-gate dt_cook_ident(dt_node_t *dnp, uint_t idflags)
27950Sstevel@tonic-gate {
27960Sstevel@tonic-gate dtrace_hdl_t *dtp = yypcb->pcb_hdl;
27970Sstevel@tonic-gate
27980Sstevel@tonic-gate if (dnp->dn_op == DT_TOK_AGG)
27990Sstevel@tonic-gate dt_xcook_ident(dnp, dtp->dt_aggs, DT_IDENT_AGG, B_FALSE);
28000Sstevel@tonic-gate else
28010Sstevel@tonic-gate dt_xcook_ident(dnp, dtp->dt_globals, DT_IDENT_SCALAR, B_FALSE);
28020Sstevel@tonic-gate
28030Sstevel@tonic-gate return (dt_node_cook(dnp, idflags));
28040Sstevel@tonic-gate }
28050Sstevel@tonic-gate
28060Sstevel@tonic-gate /*
28070Sstevel@tonic-gate * Since operators [ and -> can instantiate new variables before we know
28080Sstevel@tonic-gate * whether the reference is for a read or a write, we need to check read
28090Sstevel@tonic-gate * references to determine if the identifier is currently dt_ident_unref().
28100Sstevel@tonic-gate * If so, we report that this first access was to an undefined variable.
28110Sstevel@tonic-gate */
28120Sstevel@tonic-gate static dt_node_t *
dt_cook_var(dt_node_t * dnp,uint_t idflags)28130Sstevel@tonic-gate dt_cook_var(dt_node_t *dnp, uint_t idflags)
28140Sstevel@tonic-gate {
28150Sstevel@tonic-gate dt_ident_t *idp = dnp->dn_ident;
28160Sstevel@tonic-gate
28170Sstevel@tonic-gate if ((idflags & DT_IDFLG_REF) && dt_ident_unref(idp)) {
28180Sstevel@tonic-gate dnerror(dnp, D_VAR_UNDEF,
28190Sstevel@tonic-gate "%s%s has not yet been declared or assigned\n",
28200Sstevel@tonic-gate (idp->di_flags & DT_IDFLG_LOCAL) ? "this->" :
28210Sstevel@tonic-gate (idp->di_flags & DT_IDFLG_TLS) ? "self->" : "",
28220Sstevel@tonic-gate idp->di_name);
28230Sstevel@tonic-gate }
28240Sstevel@tonic-gate
28250Sstevel@tonic-gate dt_node_attr_assign(dnp, dt_ident_cook(dnp, idp, &dnp->dn_args));
28260Sstevel@tonic-gate return (dnp);
28270Sstevel@tonic-gate }
28280Sstevel@tonic-gate
28290Sstevel@tonic-gate /*ARGSUSED*/
28300Sstevel@tonic-gate static dt_node_t *
dt_cook_func(dt_node_t * dnp,uint_t idflags)28310Sstevel@tonic-gate dt_cook_func(dt_node_t *dnp, uint_t idflags)
28320Sstevel@tonic-gate {
28330Sstevel@tonic-gate dt_node_attr_assign(dnp,
28340Sstevel@tonic-gate dt_ident_cook(dnp, dnp->dn_ident, &dnp->dn_args));
28350Sstevel@tonic-gate
28360Sstevel@tonic-gate return (dnp);
28370Sstevel@tonic-gate }
28380Sstevel@tonic-gate
28390Sstevel@tonic-gate static dt_node_t *
dt_cook_op1(dt_node_t * dnp,uint_t idflags)28400Sstevel@tonic-gate dt_cook_op1(dt_node_t *dnp, uint_t idflags)
28410Sstevel@tonic-gate {
28420Sstevel@tonic-gate dtrace_hdl_t *dtp = yypcb->pcb_hdl;
28430Sstevel@tonic-gate dt_node_t *cp = dnp->dn_child;
28440Sstevel@tonic-gate
28450Sstevel@tonic-gate char n[DT_TYPE_NAMELEN];
28460Sstevel@tonic-gate dtrace_typeinfo_t dtt;
28470Sstevel@tonic-gate dt_ident_t *idp;
28480Sstevel@tonic-gate
28490Sstevel@tonic-gate ctf_encoding_t e;
28500Sstevel@tonic-gate ctf_arinfo_t r;
28510Sstevel@tonic-gate ctf_id_t type, base;
28520Sstevel@tonic-gate uint_t kind;
28530Sstevel@tonic-gate
28540Sstevel@tonic-gate if (dnp->dn_op == DT_TOK_PREINC || dnp->dn_op == DT_TOK_POSTINC ||
28550Sstevel@tonic-gate dnp->dn_op == DT_TOK_PREDEC || dnp->dn_op == DT_TOK_POSTDEC)
28560Sstevel@tonic-gate idflags = DT_IDFLG_REF | DT_IDFLG_MOD;
28570Sstevel@tonic-gate else
28580Sstevel@tonic-gate idflags = DT_IDFLG_REF;
28590Sstevel@tonic-gate
28600Sstevel@tonic-gate /*
28610Sstevel@tonic-gate * We allow the unary ++ and -- operators to instantiate new scalar
28620Sstevel@tonic-gate * variables if applied to an identifier; otherwise just cook as usual.
28630Sstevel@tonic-gate */
28640Sstevel@tonic-gate if (cp->dn_kind == DT_NODE_IDENT && (idflags & DT_IDFLG_MOD))
28650Sstevel@tonic-gate dt_xcook_ident(cp, dtp->dt_globals, DT_IDENT_SCALAR, B_TRUE);
28660Sstevel@tonic-gate
28670Sstevel@tonic-gate cp = dnp->dn_child = dt_node_cook(cp, 0); /* don't set idflags yet */
28680Sstevel@tonic-gate
28690Sstevel@tonic-gate if (cp->dn_kind == DT_NODE_VAR && dt_ident_unref(cp->dn_ident)) {
28700Sstevel@tonic-gate if (dt_type_lookup("int64_t", &dtt) != 0)
28710Sstevel@tonic-gate xyerror(D_TYPE_ERR, "failed to lookup int64_t\n");
28720Sstevel@tonic-gate
28730Sstevel@tonic-gate dt_ident_type_assign(cp->dn_ident, dtt.dtt_ctfp, dtt.dtt_type);
28740Sstevel@tonic-gate dt_node_type_assign(cp, dtt.dtt_ctfp, dtt.dtt_type);
28750Sstevel@tonic-gate }
28760Sstevel@tonic-gate
28770Sstevel@tonic-gate if (cp->dn_kind == DT_NODE_VAR)
28780Sstevel@tonic-gate cp->dn_ident->di_flags |= idflags;
28790Sstevel@tonic-gate
28800Sstevel@tonic-gate switch (dnp->dn_op) {
28810Sstevel@tonic-gate case DT_TOK_DEREF:
28820Sstevel@tonic-gate /*
28830Sstevel@tonic-gate * If the deref operator is applied to a translated pointer,
28840Sstevel@tonic-gate * we can just set our output type to the base translation.
28850Sstevel@tonic-gate */
28860Sstevel@tonic-gate if ((idp = dt_node_resolve(cp, DT_IDENT_XLPTR)) != NULL) {
28870Sstevel@tonic-gate dt_xlator_t *dxp = idp->di_data;
28880Sstevel@tonic-gate
28890Sstevel@tonic-gate dnp->dn_ident = &dxp->dx_souid;
28900Sstevel@tonic-gate dt_node_type_assign(dnp,
28910Sstevel@tonic-gate DT_DYN_CTFP(dtp), DT_DYN_TYPE(dtp));
28920Sstevel@tonic-gate break;
28930Sstevel@tonic-gate }
28940Sstevel@tonic-gate
28950Sstevel@tonic-gate type = ctf_type_resolve(cp->dn_ctfp, cp->dn_type);
28960Sstevel@tonic-gate kind = ctf_type_kind(cp->dn_ctfp, type);
28970Sstevel@tonic-gate
28980Sstevel@tonic-gate if (kind == CTF_K_ARRAY) {
28990Sstevel@tonic-gate if (ctf_array_info(cp->dn_ctfp, type, &r) != 0) {
29000Sstevel@tonic-gate dtp->dt_ctferr = ctf_errno(cp->dn_ctfp);
29010Sstevel@tonic-gate longjmp(yypcb->pcb_jmpbuf, EDT_CTF);
29020Sstevel@tonic-gate } else
29030Sstevel@tonic-gate type = r.ctr_contents;
29040Sstevel@tonic-gate } else if (kind == CTF_K_POINTER) {
29050Sstevel@tonic-gate type = ctf_type_reference(cp->dn_ctfp, type);
29060Sstevel@tonic-gate } else {
29070Sstevel@tonic-gate xyerror(D_DEREF_NONPTR,
29080Sstevel@tonic-gate "cannot dereference non-pointer type\n");
29090Sstevel@tonic-gate }
29100Sstevel@tonic-gate
29110Sstevel@tonic-gate dt_node_type_assign(dnp, cp->dn_ctfp, type);
29120Sstevel@tonic-gate base = ctf_type_resolve(cp->dn_ctfp, type);
29130Sstevel@tonic-gate kind = ctf_type_kind(cp->dn_ctfp, base);
29140Sstevel@tonic-gate
29150Sstevel@tonic-gate if (kind == CTF_K_INTEGER && ctf_type_encoding(cp->dn_ctfp,
29160Sstevel@tonic-gate base, &e) == 0 && IS_VOID(e)) {
29170Sstevel@tonic-gate xyerror(D_DEREF_VOID,
29180Sstevel@tonic-gate "cannot dereference pointer to void\n");
29190Sstevel@tonic-gate }
29200Sstevel@tonic-gate
29210Sstevel@tonic-gate if (kind == CTF_K_FUNCTION) {
29220Sstevel@tonic-gate xyerror(D_DEREF_FUNC,
29230Sstevel@tonic-gate "cannot dereference pointer to function\n");
29240Sstevel@tonic-gate }
29250Sstevel@tonic-gate
29260Sstevel@tonic-gate if (kind != CTF_K_ARRAY || dt_node_is_string(dnp))
29270Sstevel@tonic-gate dnp->dn_flags |= DT_NF_LVALUE; /* see K&R[A7.4.3] */
29280Sstevel@tonic-gate
29290Sstevel@tonic-gate /*
29300Sstevel@tonic-gate * If we propagated the l-value bit and the child operand was
29310Sstevel@tonic-gate * a writable D variable or a binary operation of the form
29320Sstevel@tonic-gate * a + b where a is writable, then propagate the writable bit.
29330Sstevel@tonic-gate * This is necessary to permit assignments to scalar arrays,
29340Sstevel@tonic-gate * which are converted to expressions of the form *(a + i).
29350Sstevel@tonic-gate */
29360Sstevel@tonic-gate if ((cp->dn_flags & DT_NF_WRITABLE) ||
29370Sstevel@tonic-gate (cp->dn_kind == DT_NODE_OP2 && cp->dn_op == DT_TOK_ADD &&
29380Sstevel@tonic-gate (cp->dn_left->dn_flags & DT_NF_WRITABLE)))
29390Sstevel@tonic-gate dnp->dn_flags |= DT_NF_WRITABLE;
29400Sstevel@tonic-gate
29410Sstevel@tonic-gate if ((cp->dn_flags & DT_NF_USERLAND) &&
29420Sstevel@tonic-gate (kind == CTF_K_POINTER || (dnp->dn_flags & DT_NF_REF)))
29430Sstevel@tonic-gate dnp->dn_flags |= DT_NF_USERLAND;
29440Sstevel@tonic-gate break;
29450Sstevel@tonic-gate
29460Sstevel@tonic-gate case DT_TOK_IPOS:
29470Sstevel@tonic-gate case DT_TOK_INEG:
29480Sstevel@tonic-gate if (!dt_node_is_arith(cp)) {
29490Sstevel@tonic-gate xyerror(D_OP_ARITH, "operator %s requires an operand "
29500Sstevel@tonic-gate "of arithmetic type\n", opstr(dnp->dn_op));
29510Sstevel@tonic-gate }
29520Sstevel@tonic-gate dt_node_type_propagate(cp, dnp); /* see K&R[A7.4.4-6] */
29530Sstevel@tonic-gate break;
29540Sstevel@tonic-gate
29550Sstevel@tonic-gate case DT_TOK_BNEG:
29560Sstevel@tonic-gate if (!dt_node_is_integer(cp)) {
29570Sstevel@tonic-gate xyerror(D_OP_INT, "operator %s requires an operand of "
29580Sstevel@tonic-gate "integral type\n", opstr(dnp->dn_op));
29590Sstevel@tonic-gate }
29600Sstevel@tonic-gate dt_node_type_propagate(cp, dnp); /* see K&R[A7.4.4-6] */
29610Sstevel@tonic-gate break;
29620Sstevel@tonic-gate
29630Sstevel@tonic-gate case DT_TOK_LNEG:
29640Sstevel@tonic-gate if (!dt_node_is_scalar(cp)) {
29650Sstevel@tonic-gate xyerror(D_OP_SCALAR, "operator %s requires an operand "
29660Sstevel@tonic-gate "of scalar type\n", opstr(dnp->dn_op));
29670Sstevel@tonic-gate }
29680Sstevel@tonic-gate dt_node_type_assign(dnp, DT_INT_CTFP(dtp), DT_INT_TYPE(dtp));
29690Sstevel@tonic-gate break;
29700Sstevel@tonic-gate
29710Sstevel@tonic-gate case DT_TOK_ADDROF:
29720Sstevel@tonic-gate if (cp->dn_kind == DT_NODE_VAR || cp->dn_kind == DT_NODE_AGG) {
29730Sstevel@tonic-gate xyerror(D_ADDROF_VAR,
29740Sstevel@tonic-gate "cannot take address of dynamic variable\n");
29750Sstevel@tonic-gate }
29760Sstevel@tonic-gate
29770Sstevel@tonic-gate if (dt_node_is_dynamic(cp)) {
29780Sstevel@tonic-gate xyerror(D_ADDROF_VAR,
29790Sstevel@tonic-gate "cannot take address of dynamic object\n");
29800Sstevel@tonic-gate }
29810Sstevel@tonic-gate
29820Sstevel@tonic-gate if (!(cp->dn_flags & DT_NF_LVALUE)) {
29830Sstevel@tonic-gate xyerror(D_ADDROF_LVAL, /* see K&R[A7.4.2] */
29840Sstevel@tonic-gate "unacceptable operand for unary & operator\n");
29850Sstevel@tonic-gate }
29860Sstevel@tonic-gate
29870Sstevel@tonic-gate if (cp->dn_flags & DT_NF_BITFIELD) {
29880Sstevel@tonic-gate xyerror(D_ADDROF_BITFIELD,
29890Sstevel@tonic-gate "cannot take address of bit-field\n");
29900Sstevel@tonic-gate }
29910Sstevel@tonic-gate
29920Sstevel@tonic-gate dtt.dtt_object = NULL;
29930Sstevel@tonic-gate dtt.dtt_ctfp = cp->dn_ctfp;
29940Sstevel@tonic-gate dtt.dtt_type = cp->dn_type;
29950Sstevel@tonic-gate
29960Sstevel@tonic-gate if (dt_type_pointer(&dtt) == -1) {
29970Sstevel@tonic-gate xyerror(D_TYPE_ERR, "cannot find type for \"&\": %s*\n",
29980Sstevel@tonic-gate dt_node_type_name(cp, n, sizeof (n)));
29990Sstevel@tonic-gate }
30000Sstevel@tonic-gate
30010Sstevel@tonic-gate dt_node_type_assign(dnp, dtt.dtt_ctfp, dtt.dtt_type);
30020Sstevel@tonic-gate
30030Sstevel@tonic-gate if (cp->dn_flags & DT_NF_USERLAND)
30040Sstevel@tonic-gate dnp->dn_flags |= DT_NF_USERLAND;
30050Sstevel@tonic-gate break;
30060Sstevel@tonic-gate
30070Sstevel@tonic-gate case DT_TOK_SIZEOF:
30080Sstevel@tonic-gate if (cp->dn_flags & DT_NF_BITFIELD) {
30090Sstevel@tonic-gate xyerror(D_SIZEOF_BITFIELD,
30100Sstevel@tonic-gate "cannot apply sizeof to a bit-field\n");
30110Sstevel@tonic-gate }
30120Sstevel@tonic-gate
30130Sstevel@tonic-gate if (dt_node_sizeof(cp) == 0) {
30140Sstevel@tonic-gate xyerror(D_SIZEOF_TYPE, "cannot apply sizeof to an "
30150Sstevel@tonic-gate "operand of unknown size\n");
30160Sstevel@tonic-gate }
30170Sstevel@tonic-gate
30180Sstevel@tonic-gate dt_node_type_assign(dnp, dtp->dt_ddefs->dm_ctfp,
30190Sstevel@tonic-gate ctf_lookup_by_name(dtp->dt_ddefs->dm_ctfp, "size_t"));
30200Sstevel@tonic-gate break;
30210Sstevel@tonic-gate
30220Sstevel@tonic-gate case DT_TOK_STRINGOF:
30230Sstevel@tonic-gate if (!dt_node_is_scalar(cp) && !dt_node_is_pointer(cp) &&
30240Sstevel@tonic-gate !dt_node_is_strcompat(cp)) {
30250Sstevel@tonic-gate xyerror(D_STRINGOF_TYPE,
30260Sstevel@tonic-gate "cannot apply stringof to a value of type %s\n",
30270Sstevel@tonic-gate dt_node_type_name(cp, n, sizeof (n)));
30280Sstevel@tonic-gate }
30290Sstevel@tonic-gate dt_node_type_assign(dnp, DT_STR_CTFP(dtp), DT_STR_TYPE(dtp));
30300Sstevel@tonic-gate break;
30310Sstevel@tonic-gate
30320Sstevel@tonic-gate case DT_TOK_PREINC:
30330Sstevel@tonic-gate case DT_TOK_POSTINC:
30340Sstevel@tonic-gate case DT_TOK_PREDEC:
30350Sstevel@tonic-gate case DT_TOK_POSTDEC:
30360Sstevel@tonic-gate if (dt_node_is_scalar(cp) == 0) {
30370Sstevel@tonic-gate xyerror(D_OP_SCALAR, "operator %s requires operand of "
30380Sstevel@tonic-gate "scalar type\n", opstr(dnp->dn_op));
30390Sstevel@tonic-gate }
30400Sstevel@tonic-gate
30410Sstevel@tonic-gate if (dt_node_is_vfptr(cp)) {
30420Sstevel@tonic-gate xyerror(D_OP_VFPTR, "operator %s requires an operand "
30430Sstevel@tonic-gate "of known size\n", opstr(dnp->dn_op));
30440Sstevel@tonic-gate }
30450Sstevel@tonic-gate
30460Sstevel@tonic-gate if (!(cp->dn_flags & DT_NF_LVALUE)) {
30470Sstevel@tonic-gate xyerror(D_OP_LVAL, "operator %s requires modifiable "
30480Sstevel@tonic-gate "lvalue as an operand\n", opstr(dnp->dn_op));
30490Sstevel@tonic-gate }
30500Sstevel@tonic-gate
30510Sstevel@tonic-gate if (!(cp->dn_flags & DT_NF_WRITABLE)) {
30520Sstevel@tonic-gate xyerror(D_OP_WRITE, "operator %s can only be applied "
30530Sstevel@tonic-gate "to a writable variable\n", opstr(dnp->dn_op));
30540Sstevel@tonic-gate }
30550Sstevel@tonic-gate
30560Sstevel@tonic-gate dt_node_type_propagate(cp, dnp); /* see K&R[A7.4.1] */
30570Sstevel@tonic-gate break;
30580Sstevel@tonic-gate
30590Sstevel@tonic-gate default:
30600Sstevel@tonic-gate xyerror(D_UNKNOWN, "invalid unary op %s\n", opstr(dnp->dn_op));
30610Sstevel@tonic-gate }
30620Sstevel@tonic-gate
30630Sstevel@tonic-gate dt_node_attr_assign(dnp, cp->dn_attr);
30640Sstevel@tonic-gate return (dnp);
30650Sstevel@tonic-gate }
30660Sstevel@tonic-gate
30670Sstevel@tonic-gate static dt_node_t *
dt_cook_op2(dt_node_t * dnp,uint_t idflags)30680Sstevel@tonic-gate dt_cook_op2(dt_node_t *dnp, uint_t idflags)
30690Sstevel@tonic-gate {
30700Sstevel@tonic-gate dtrace_hdl_t *dtp = yypcb->pcb_hdl;
30710Sstevel@tonic-gate dt_node_t *lp = dnp->dn_left;
30720Sstevel@tonic-gate dt_node_t *rp = dnp->dn_right;
30730Sstevel@tonic-gate int op = dnp->dn_op;
30740Sstevel@tonic-gate
30750Sstevel@tonic-gate ctf_membinfo_t m;
30760Sstevel@tonic-gate ctf_file_t *ctfp;
30770Sstevel@tonic-gate ctf_id_t type;
30780Sstevel@tonic-gate int kind, val, uref;
30790Sstevel@tonic-gate dt_ident_t *idp;
30800Sstevel@tonic-gate
30810Sstevel@tonic-gate char n1[DT_TYPE_NAMELEN];
30820Sstevel@tonic-gate char n2[DT_TYPE_NAMELEN];
30830Sstevel@tonic-gate
30840Sstevel@tonic-gate /*
30850Sstevel@tonic-gate * The expression E1[E2] is identical by definition to *((E1)+(E2)) so
30860Sstevel@tonic-gate * we convert "[" to "+" and glue on "*" at the end (see K&R[A7.3.1])
30870Sstevel@tonic-gate * unless the left-hand side is an untyped D scalar, associative array,
30880Sstevel@tonic-gate * or aggregation. In these cases, we proceed to case DT_TOK_LBRAC and
30890Sstevel@tonic-gate * handle associative array and aggregation references there.
30900Sstevel@tonic-gate */
30910Sstevel@tonic-gate if (op == DT_TOK_LBRAC) {
30920Sstevel@tonic-gate if (lp->dn_kind == DT_NODE_IDENT) {
30930Sstevel@tonic-gate dt_idhash_t *dhp;
30940Sstevel@tonic-gate uint_t idkind;
30950Sstevel@tonic-gate
30960Sstevel@tonic-gate if (lp->dn_op == DT_TOK_AGG) {
30970Sstevel@tonic-gate dhp = dtp->dt_aggs;
30980Sstevel@tonic-gate idp = dt_idhash_lookup(dhp, lp->dn_string + 1);
30990Sstevel@tonic-gate idkind = DT_IDENT_AGG;
31000Sstevel@tonic-gate } else {
31010Sstevel@tonic-gate dhp = dtp->dt_globals;
31020Sstevel@tonic-gate idp = dt_idstack_lookup(
31030Sstevel@tonic-gate &yypcb->pcb_globals, lp->dn_string);
31040Sstevel@tonic-gate idkind = DT_IDENT_ARRAY;
31050Sstevel@tonic-gate }
31060Sstevel@tonic-gate
31070Sstevel@tonic-gate if (idp == NULL || dt_ident_unref(idp))
31080Sstevel@tonic-gate dt_xcook_ident(lp, dhp, idkind, B_TRUE);
31090Sstevel@tonic-gate else
31100Sstevel@tonic-gate dt_xcook_ident(lp, dhp, idp->di_kind, B_FALSE);
31110Sstevel@tonic-gate } else
31120Sstevel@tonic-gate lp = dnp->dn_left = dt_node_cook(lp, 0);
31130Sstevel@tonic-gate
31140Sstevel@tonic-gate /*
31150Sstevel@tonic-gate * Switch op to '+' for *(E1 + E2) array mode in these cases:
31160Sstevel@tonic-gate * (a) lp is a DT_IDENT_ARRAY variable that has already been
31170Sstevel@tonic-gate * referenced using [] notation (dn_args != NULL).
31180Sstevel@tonic-gate * (b) lp is a non-ARRAY variable that has already been given
31190Sstevel@tonic-gate * a type by assignment or declaration (!dt_ident_unref())
31200Sstevel@tonic-gate * (c) lp is neither a variable nor an aggregation
31210Sstevel@tonic-gate */
31220Sstevel@tonic-gate if (lp->dn_kind == DT_NODE_VAR) {
31230Sstevel@tonic-gate if (lp->dn_ident->di_kind == DT_IDENT_ARRAY) {
31240Sstevel@tonic-gate if (lp->dn_args != NULL)
31250Sstevel@tonic-gate op = DT_TOK_ADD;
31260Sstevel@tonic-gate } else if (!dt_ident_unref(lp->dn_ident))
31270Sstevel@tonic-gate op = DT_TOK_ADD;
31280Sstevel@tonic-gate } else if (lp->dn_kind != DT_NODE_AGG)
31290Sstevel@tonic-gate op = DT_TOK_ADD;
31300Sstevel@tonic-gate }
31310Sstevel@tonic-gate
31320Sstevel@tonic-gate switch (op) {
31330Sstevel@tonic-gate case DT_TOK_BAND:
31340Sstevel@tonic-gate case DT_TOK_XOR:
31350Sstevel@tonic-gate case DT_TOK_BOR:
31360Sstevel@tonic-gate lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF);
31370Sstevel@tonic-gate rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
31380Sstevel@tonic-gate
31390Sstevel@tonic-gate if (!dt_node_is_integer(lp) || !dt_node_is_integer(rp)) {
31400Sstevel@tonic-gate xyerror(D_OP_INT, "operator %s requires operands of "
31410Sstevel@tonic-gate "integral type\n", opstr(op));
31420Sstevel@tonic-gate }
31430Sstevel@tonic-gate
31440Sstevel@tonic-gate dt_node_promote(lp, rp, dnp); /* see K&R[A7.11-13] */
31450Sstevel@tonic-gate break;
31460Sstevel@tonic-gate
31470Sstevel@tonic-gate case DT_TOK_LSH:
31480Sstevel@tonic-gate case DT_TOK_RSH:
31490Sstevel@tonic-gate lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF);
31500Sstevel@tonic-gate rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
31510Sstevel@tonic-gate
31520Sstevel@tonic-gate if (!dt_node_is_integer(lp) || !dt_node_is_integer(rp)) {
31530Sstevel@tonic-gate xyerror(D_OP_INT, "operator %s requires operands of "
31540Sstevel@tonic-gate "integral type\n", opstr(op));
31550Sstevel@tonic-gate }
31560Sstevel@tonic-gate
31570Sstevel@tonic-gate dt_node_type_propagate(lp, dnp); /* see K&R[A7.8] */
31580Sstevel@tonic-gate dt_node_attr_assign(dnp, dt_attr_min(lp->dn_attr, rp->dn_attr));
31590Sstevel@tonic-gate break;
31600Sstevel@tonic-gate
31610Sstevel@tonic-gate case DT_TOK_MOD:
31620Sstevel@tonic-gate lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF);
31630Sstevel@tonic-gate rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
31640Sstevel@tonic-gate
31650Sstevel@tonic-gate if (!dt_node_is_integer(lp) || !dt_node_is_integer(rp)) {
31660Sstevel@tonic-gate xyerror(D_OP_INT, "operator %s requires operands of "
31670Sstevel@tonic-gate "integral type\n", opstr(op));
31680Sstevel@tonic-gate }
31690Sstevel@tonic-gate
31700Sstevel@tonic-gate dt_node_promote(lp, rp, dnp); /* see K&R[A7.6] */
31710Sstevel@tonic-gate break;
31720Sstevel@tonic-gate
31730Sstevel@tonic-gate case DT_TOK_MUL:
31740Sstevel@tonic-gate case DT_TOK_DIV:
31750Sstevel@tonic-gate lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF);
31760Sstevel@tonic-gate rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
31770Sstevel@tonic-gate
31780Sstevel@tonic-gate if (!dt_node_is_arith(lp) || !dt_node_is_arith(rp)) {
31790Sstevel@tonic-gate xyerror(D_OP_ARITH, "operator %s requires operands of "
31800Sstevel@tonic-gate "arithmetic type\n", opstr(op));
31810Sstevel@tonic-gate }
31820Sstevel@tonic-gate
31830Sstevel@tonic-gate dt_node_promote(lp, rp, dnp); /* see K&R[A7.6] */
31840Sstevel@tonic-gate break;
31850Sstevel@tonic-gate
31860Sstevel@tonic-gate case DT_TOK_LAND:
31870Sstevel@tonic-gate case DT_TOK_LXOR:
31880Sstevel@tonic-gate case DT_TOK_LOR:
31890Sstevel@tonic-gate lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF);
31900Sstevel@tonic-gate rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
31910Sstevel@tonic-gate
31920Sstevel@tonic-gate if (!dt_node_is_scalar(lp) || !dt_node_is_scalar(rp)) {
31930Sstevel@tonic-gate xyerror(D_OP_SCALAR, "operator %s requires operands "
31940Sstevel@tonic-gate "of scalar type\n", opstr(op));
31950Sstevel@tonic-gate }
31960Sstevel@tonic-gate
31970Sstevel@tonic-gate dt_node_type_assign(dnp, DT_INT_CTFP(dtp), DT_INT_TYPE(dtp));
31980Sstevel@tonic-gate dt_node_attr_assign(dnp, dt_attr_min(lp->dn_attr, rp->dn_attr));
31990Sstevel@tonic-gate break;
32000Sstevel@tonic-gate
32010Sstevel@tonic-gate case DT_TOK_LT:
32020Sstevel@tonic-gate case DT_TOK_LE:
32030Sstevel@tonic-gate case DT_TOK_GT:
32040Sstevel@tonic-gate case DT_TOK_GE:
32050Sstevel@tonic-gate case DT_TOK_EQU:
32060Sstevel@tonic-gate case DT_TOK_NEQ:
32070Sstevel@tonic-gate /*
32080Sstevel@tonic-gate * The D comparison operators provide the ability to transform
32090Sstevel@tonic-gate * a right-hand identifier into a corresponding enum tag value
32100Sstevel@tonic-gate * if the left-hand side is an enum type. To do this, we cook
32110Sstevel@tonic-gate * the left-hand side, and then see if the right-hand side is
32120Sstevel@tonic-gate * an unscoped identifier defined in the enum. If so, we
32130Sstevel@tonic-gate * convert into an integer constant node with the tag's value.
32140Sstevel@tonic-gate */
32150Sstevel@tonic-gate lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF);
32160Sstevel@tonic-gate
32170Sstevel@tonic-gate kind = ctf_type_kind(lp->dn_ctfp,
32180Sstevel@tonic-gate ctf_type_resolve(lp->dn_ctfp, lp->dn_type));
32190Sstevel@tonic-gate
32200Sstevel@tonic-gate if (kind == CTF_K_ENUM && rp->dn_kind == DT_NODE_IDENT &&
32210Sstevel@tonic-gate strchr(rp->dn_string, '`') == NULL && ctf_enum_value(
32220Sstevel@tonic-gate lp->dn_ctfp, lp->dn_type, rp->dn_string, &val) == 0) {
32230Sstevel@tonic-gate
32240Sstevel@tonic-gate if ((idp = dt_idstack_lookup(&yypcb->pcb_globals,
32250Sstevel@tonic-gate rp->dn_string)) != NULL) {
32260Sstevel@tonic-gate xyerror(D_IDENT_AMBIG,
32270Sstevel@tonic-gate "ambiguous use of operator %s: %s is "
32280Sstevel@tonic-gate "both a %s enum tag and a global %s\n",
32290Sstevel@tonic-gate opstr(op), rp->dn_string,
32300Sstevel@tonic-gate dt_node_type_name(lp, n1, sizeof (n1)),
32310Sstevel@tonic-gate dt_idkind_name(idp->di_kind));
32320Sstevel@tonic-gate }
32330Sstevel@tonic-gate
32340Sstevel@tonic-gate free(rp->dn_string);
32350Sstevel@tonic-gate rp->dn_string = NULL;
32360Sstevel@tonic-gate rp->dn_kind = DT_NODE_INT;
32370Sstevel@tonic-gate rp->dn_flags |= DT_NF_COOKED;
32380Sstevel@tonic-gate rp->dn_op = DT_TOK_INT;
32390Sstevel@tonic-gate rp->dn_value = (intmax_t)val;
32400Sstevel@tonic-gate
32410Sstevel@tonic-gate dt_node_type_assign(rp, lp->dn_ctfp, lp->dn_type);
32420Sstevel@tonic-gate dt_node_attr_assign(rp, _dtrace_symattr);
32430Sstevel@tonic-gate }
32440Sstevel@tonic-gate
32450Sstevel@tonic-gate rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
32460Sstevel@tonic-gate
32470Sstevel@tonic-gate /*
32480Sstevel@tonic-gate * The rules for type checking for the relational operators are
32490Sstevel@tonic-gate * described in the ANSI-C spec (see K&R[A7.9-10]). We perform
32500Sstevel@tonic-gate * the various tests in order from least to most expensive. We
32510Sstevel@tonic-gate * also allow derived strings to be compared as a first-class
32520Sstevel@tonic-gate * type (resulting in a strcmp(3C)-style comparison), and we
32530Sstevel@tonic-gate * slightly relax the A7.9 rules to permit void pointer
32540Sstevel@tonic-gate * comparisons as in A7.10. Our users won't be confused by
32550Sstevel@tonic-gate * this since they understand pointers are just numbers, and
32560Sstevel@tonic-gate * relaxing this constraint simplifies the implementation.
32570Sstevel@tonic-gate */
32580Sstevel@tonic-gate if (ctf_type_compat(lp->dn_ctfp, lp->dn_type,
32590Sstevel@tonic-gate rp->dn_ctfp, rp->dn_type))
32600Sstevel@tonic-gate /*EMPTY*/;
32610Sstevel@tonic-gate else if (dt_node_is_integer(lp) && dt_node_is_integer(rp))
32620Sstevel@tonic-gate /*EMPTY*/;
32630Sstevel@tonic-gate else if (dt_node_is_strcompat(lp) && dt_node_is_strcompat(rp) &&
32640Sstevel@tonic-gate (dt_node_is_string(lp) || dt_node_is_string(rp)))
32650Sstevel@tonic-gate /*EMPTY*/;
32660Sstevel@tonic-gate else if (dt_node_is_ptrcompat(lp, rp, NULL, NULL) == 0) {
32670Sstevel@tonic-gate xyerror(D_OP_INCOMPAT, "operands have "
32680Sstevel@tonic-gate "incompatible types: \"%s\" %s \"%s\"\n",
32690Sstevel@tonic-gate dt_node_type_name(lp, n1, sizeof (n1)), opstr(op),
32700Sstevel@tonic-gate dt_node_type_name(rp, n2, sizeof (n2)));
32710Sstevel@tonic-gate }
32720Sstevel@tonic-gate
32730Sstevel@tonic-gate dt_node_type_assign(dnp, DT_INT_CTFP(dtp), DT_INT_TYPE(dtp));
32740Sstevel@tonic-gate dt_node_attr_assign(dnp, dt_attr_min(lp->dn_attr, rp->dn_attr));
32750Sstevel@tonic-gate break;
32760Sstevel@tonic-gate
32770Sstevel@tonic-gate case DT_TOK_ADD:
32780Sstevel@tonic-gate case DT_TOK_SUB: {
32790Sstevel@tonic-gate /*
32800Sstevel@tonic-gate * The rules for type checking for the additive operators are
32810Sstevel@tonic-gate * described in the ANSI-C spec (see K&R[A7.7]). Pointers and
32821222Smws * integers may be manipulated according to specific rules. In
32831222Smws * these cases D permits strings to be treated as pointers.
32840Sstevel@tonic-gate */
32850Sstevel@tonic-gate int lp_is_ptr, lp_is_int, rp_is_ptr, rp_is_int;
32860Sstevel@tonic-gate
32870Sstevel@tonic-gate lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF);
32880Sstevel@tonic-gate rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
32890Sstevel@tonic-gate
32901222Smws lp_is_ptr = dt_node_is_string(lp) ||
32911222Smws (dt_node_is_pointer(lp) && !dt_node_is_vfptr(lp));
32920Sstevel@tonic-gate lp_is_int = dt_node_is_integer(lp);
32930Sstevel@tonic-gate
32941222Smws rp_is_ptr = dt_node_is_string(rp) ||
32951222Smws (dt_node_is_pointer(rp) && !dt_node_is_vfptr(rp));
32960Sstevel@tonic-gate rp_is_int = dt_node_is_integer(rp);
32970Sstevel@tonic-gate
32980Sstevel@tonic-gate if (lp_is_int && rp_is_int) {
32990Sstevel@tonic-gate dt_type_promote(lp, rp, &ctfp, &type);
33000Sstevel@tonic-gate uref = 0;
33010Sstevel@tonic-gate } else if (lp_is_ptr && rp_is_int) {
33020Sstevel@tonic-gate ctfp = lp->dn_ctfp;
33030Sstevel@tonic-gate type = lp->dn_type;
33040Sstevel@tonic-gate uref = lp->dn_flags & DT_NF_USERLAND;
33050Sstevel@tonic-gate } else if (lp_is_int && rp_is_ptr && op == DT_TOK_ADD) {
33060Sstevel@tonic-gate ctfp = rp->dn_ctfp;
33070Sstevel@tonic-gate type = rp->dn_type;
33080Sstevel@tonic-gate uref = rp->dn_flags & DT_NF_USERLAND;
33090Sstevel@tonic-gate } else if (lp_is_ptr && rp_is_ptr && op == DT_TOK_SUB &&
33100Sstevel@tonic-gate dt_node_is_ptrcompat(lp, rp, NULL, NULL)) {
33110Sstevel@tonic-gate ctfp = dtp->dt_ddefs->dm_ctfp;
33120Sstevel@tonic-gate type = ctf_lookup_by_name(ctfp, "ptrdiff_t");
33130Sstevel@tonic-gate uref = 0;
33140Sstevel@tonic-gate } else {
33150Sstevel@tonic-gate xyerror(D_OP_INCOMPAT, "operands have incompatible "
33160Sstevel@tonic-gate "types: \"%s\" %s \"%s\"\n",
33170Sstevel@tonic-gate dt_node_type_name(lp, n1, sizeof (n1)), opstr(op),
33180Sstevel@tonic-gate dt_node_type_name(rp, n2, sizeof (n2)));
33190Sstevel@tonic-gate }
33200Sstevel@tonic-gate
33210Sstevel@tonic-gate dt_node_type_assign(dnp, ctfp, type);
33220Sstevel@tonic-gate dt_node_attr_assign(dnp, dt_attr_min(lp->dn_attr, rp->dn_attr));
33230Sstevel@tonic-gate
33240Sstevel@tonic-gate if (uref)
33250Sstevel@tonic-gate dnp->dn_flags |= DT_NF_USERLAND;
33260Sstevel@tonic-gate break;
33270Sstevel@tonic-gate }
33280Sstevel@tonic-gate
33290Sstevel@tonic-gate case DT_TOK_OR_EQ:
33300Sstevel@tonic-gate case DT_TOK_XOR_EQ:
33310Sstevel@tonic-gate case DT_TOK_AND_EQ:
33320Sstevel@tonic-gate case DT_TOK_LSH_EQ:
33330Sstevel@tonic-gate case DT_TOK_RSH_EQ:
33340Sstevel@tonic-gate case DT_TOK_MOD_EQ:
33350Sstevel@tonic-gate if (lp->dn_kind == DT_NODE_IDENT) {
33360Sstevel@tonic-gate dt_xcook_ident(lp, dtp->dt_globals,
33370Sstevel@tonic-gate DT_IDENT_SCALAR, B_TRUE);
33380Sstevel@tonic-gate }
33390Sstevel@tonic-gate
33400Sstevel@tonic-gate lp = dnp->dn_left =
33410Sstevel@tonic-gate dt_node_cook(lp, DT_IDFLG_REF | DT_IDFLG_MOD);
33420Sstevel@tonic-gate
33430Sstevel@tonic-gate rp = dnp->dn_right =
33440Sstevel@tonic-gate dt_node_cook(rp, DT_IDFLG_REF | DT_IDFLG_MOD);
33450Sstevel@tonic-gate
33460Sstevel@tonic-gate if (!dt_node_is_integer(lp) || !dt_node_is_integer(rp)) {
33470Sstevel@tonic-gate xyerror(D_OP_INT, "operator %s requires operands of "
33480Sstevel@tonic-gate "integral type\n", opstr(op));
33490Sstevel@tonic-gate }
33500Sstevel@tonic-gate goto asgn_common;
33510Sstevel@tonic-gate
33520Sstevel@tonic-gate case DT_TOK_MUL_EQ:
33530Sstevel@tonic-gate case DT_TOK_DIV_EQ:
33540Sstevel@tonic-gate if (lp->dn_kind == DT_NODE_IDENT) {
33550Sstevel@tonic-gate dt_xcook_ident(lp, dtp->dt_globals,
33560Sstevel@tonic-gate DT_IDENT_SCALAR, B_TRUE);
33570Sstevel@tonic-gate }
33580Sstevel@tonic-gate
33590Sstevel@tonic-gate lp = dnp->dn_left =
33600Sstevel@tonic-gate dt_node_cook(lp, DT_IDFLG_REF | DT_IDFLG_MOD);
33610Sstevel@tonic-gate
33620Sstevel@tonic-gate rp = dnp->dn_right =
33630Sstevel@tonic-gate dt_node_cook(rp, DT_IDFLG_REF | DT_IDFLG_MOD);
33640Sstevel@tonic-gate
33650Sstevel@tonic-gate if (!dt_node_is_arith(lp) || !dt_node_is_arith(rp)) {
33660Sstevel@tonic-gate xyerror(D_OP_ARITH, "operator %s requires operands of "
33670Sstevel@tonic-gate "arithmetic type\n", opstr(op));
33680Sstevel@tonic-gate }
33690Sstevel@tonic-gate goto asgn_common;
33700Sstevel@tonic-gate
33710Sstevel@tonic-gate case DT_TOK_ASGN:
33720Sstevel@tonic-gate /*
33730Sstevel@tonic-gate * If the left-hand side is an identifier, attempt to resolve
33740Sstevel@tonic-gate * it as either an aggregation or scalar variable. We pass
33750Sstevel@tonic-gate * B_TRUE to dt_xcook_ident to indicate that a new variable can
33760Sstevel@tonic-gate * be created if no matching variable exists in the namespace.
33770Sstevel@tonic-gate */
33780Sstevel@tonic-gate if (lp->dn_kind == DT_NODE_IDENT) {
33790Sstevel@tonic-gate if (lp->dn_op == DT_TOK_AGG) {
33800Sstevel@tonic-gate dt_xcook_ident(lp, dtp->dt_aggs,
33810Sstevel@tonic-gate DT_IDENT_AGG, B_TRUE);
33820Sstevel@tonic-gate } else {
33830Sstevel@tonic-gate dt_xcook_ident(lp, dtp->dt_globals,
33840Sstevel@tonic-gate DT_IDENT_SCALAR, B_TRUE);
33850Sstevel@tonic-gate }
33860Sstevel@tonic-gate }
33870Sstevel@tonic-gate
33880Sstevel@tonic-gate lp = dnp->dn_left = dt_node_cook(lp, 0); /* don't set mod yet */
33890Sstevel@tonic-gate rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
33900Sstevel@tonic-gate
33910Sstevel@tonic-gate /*
33920Sstevel@tonic-gate * If the left-hand side is an aggregation, verify that we are
33930Sstevel@tonic-gate * assigning it the result of an aggregating function. Once
33940Sstevel@tonic-gate * we've done so, hide the func node in the aggregation and
33950Sstevel@tonic-gate * return the aggregation itself up to the parse tree parent.
33960Sstevel@tonic-gate * This transformation is legal since the assigned function
33970Sstevel@tonic-gate * cannot change identity across disjoint cooking passes and
33980Sstevel@tonic-gate * the argument list subtree is retained for later cooking.
33990Sstevel@tonic-gate */
34000Sstevel@tonic-gate if (lp->dn_kind == DT_NODE_AGG) {
34010Sstevel@tonic-gate const char *aname = lp->dn_ident->di_name;
34020Sstevel@tonic-gate dt_ident_t *oid = lp->dn_ident->di_iarg;
34030Sstevel@tonic-gate
34040Sstevel@tonic-gate if (rp->dn_kind != DT_NODE_FUNC ||
34050Sstevel@tonic-gate rp->dn_ident->di_kind != DT_IDENT_AGGFUNC) {
34060Sstevel@tonic-gate xyerror(D_AGG_FUNC,
34070Sstevel@tonic-gate "@%s must be assigned the result of "
34080Sstevel@tonic-gate "an aggregating function\n", aname);
34090Sstevel@tonic-gate }
34100Sstevel@tonic-gate
34110Sstevel@tonic-gate if (oid != NULL && oid != rp->dn_ident) {
34120Sstevel@tonic-gate xyerror(D_AGG_REDEF,
34130Sstevel@tonic-gate "aggregation redefined: @%s\n\t "
34140Sstevel@tonic-gate "current: @%s = %s( )\n\tprevious: @%s = "
34150Sstevel@tonic-gate "%s( ) : line %d\n", aname, aname,
34160Sstevel@tonic-gate rp->dn_ident->di_name, aname, oid->di_name,
34170Sstevel@tonic-gate lp->dn_ident->di_lineno);
34180Sstevel@tonic-gate } else if (oid == NULL)
34190Sstevel@tonic-gate lp->dn_ident->di_iarg = rp->dn_ident;
34200Sstevel@tonic-gate
34210Sstevel@tonic-gate /*
34220Sstevel@tonic-gate * Do not allow multiple aggregation assignments in a
34230Sstevel@tonic-gate * single statement, e.g. (@a = count()) = count();
34240Sstevel@tonic-gate * We produce a message as if the result of aggregating
34250Sstevel@tonic-gate * function does not propagate DT_NF_LVALUE.
34260Sstevel@tonic-gate */
34270Sstevel@tonic-gate if (lp->dn_aggfun != NULL) {
34280Sstevel@tonic-gate xyerror(D_OP_LVAL, "operator = requires "
34290Sstevel@tonic-gate "modifiable lvalue as an operand\n");
34300Sstevel@tonic-gate }
34310Sstevel@tonic-gate
34320Sstevel@tonic-gate lp->dn_aggfun = rp;
34330Sstevel@tonic-gate lp = dt_node_cook(lp, DT_IDFLG_MOD);
34340Sstevel@tonic-gate
34350Sstevel@tonic-gate dnp->dn_left = dnp->dn_right = NULL;
34360Sstevel@tonic-gate dt_node_free(dnp);
34370Sstevel@tonic-gate
34380Sstevel@tonic-gate return (lp);
34390Sstevel@tonic-gate }
34400Sstevel@tonic-gate
34410Sstevel@tonic-gate /*
34420Sstevel@tonic-gate * If the right-hand side is a dynamic variable that is the
34430Sstevel@tonic-gate * output of a translator, our result is the translated type.
34440Sstevel@tonic-gate */
34450Sstevel@tonic-gate if ((idp = dt_node_resolve(rp, DT_IDENT_XLSOU)) != NULL) {
34460Sstevel@tonic-gate ctfp = idp->di_ctfp;
34470Sstevel@tonic-gate type = idp->di_type;
34480Sstevel@tonic-gate uref = idp->di_flags & DT_IDFLG_USER;
34490Sstevel@tonic-gate } else {
34500Sstevel@tonic-gate ctfp = rp->dn_ctfp;
34510Sstevel@tonic-gate type = rp->dn_type;
34520Sstevel@tonic-gate uref = rp->dn_flags & DT_NF_USERLAND;
34530Sstevel@tonic-gate }
34540Sstevel@tonic-gate
34550Sstevel@tonic-gate /*
34560Sstevel@tonic-gate * If the left-hand side of an assignment statement is a virgin
34570Sstevel@tonic-gate * variable created by this compilation pass, reset the type of
34580Sstevel@tonic-gate * this variable to the type of the right-hand side.
34590Sstevel@tonic-gate */
34600Sstevel@tonic-gate if (lp->dn_kind == DT_NODE_VAR &&
34610Sstevel@tonic-gate dt_ident_unref(lp->dn_ident)) {
34620Sstevel@tonic-gate dt_node_type_assign(lp, ctfp, type);
34630Sstevel@tonic-gate dt_ident_type_assign(lp->dn_ident, ctfp, type);
34640Sstevel@tonic-gate
34650Sstevel@tonic-gate if (uref) {
34660Sstevel@tonic-gate lp->dn_flags |= DT_NF_USERLAND;
34670Sstevel@tonic-gate lp->dn_ident->di_flags |= DT_IDFLG_USER;
34680Sstevel@tonic-gate }
34690Sstevel@tonic-gate }
34700Sstevel@tonic-gate
34710Sstevel@tonic-gate if (lp->dn_kind == DT_NODE_VAR)
34720Sstevel@tonic-gate lp->dn_ident->di_flags |= DT_IDFLG_MOD;
34730Sstevel@tonic-gate
34740Sstevel@tonic-gate /*
34750Sstevel@tonic-gate * The rules for type checking for the assignment operators are
34760Sstevel@tonic-gate * described in the ANSI-C spec (see K&R[A7.17]). We share
34770Sstevel@tonic-gate * most of this code with the argument list checking code.
34780Sstevel@tonic-gate */
34790Sstevel@tonic-gate if (!dt_node_is_string(lp)) {
34800Sstevel@tonic-gate kind = ctf_type_kind(lp->dn_ctfp,
34810Sstevel@tonic-gate ctf_type_resolve(lp->dn_ctfp, lp->dn_type));
34820Sstevel@tonic-gate
34830Sstevel@tonic-gate if (kind == CTF_K_ARRAY || kind == CTF_K_FUNCTION) {
34840Sstevel@tonic-gate xyerror(D_OP_ARRFUN, "operator %s may not be "
34850Sstevel@tonic-gate "applied to operand of type \"%s\"\n",
34860Sstevel@tonic-gate opstr(op),
34870Sstevel@tonic-gate dt_node_type_name(lp, n1, sizeof (n1)));
34880Sstevel@tonic-gate }
34890Sstevel@tonic-gate }
34900Sstevel@tonic-gate
34910Sstevel@tonic-gate if (idp != NULL && idp->di_kind == DT_IDENT_XLSOU &&
34920Sstevel@tonic-gate ctf_type_compat(lp->dn_ctfp, lp->dn_type, ctfp, type))
34930Sstevel@tonic-gate goto asgn_common;
34940Sstevel@tonic-gate
34950Sstevel@tonic-gate if (dt_node_is_argcompat(lp, rp))
34960Sstevel@tonic-gate goto asgn_common;
34970Sstevel@tonic-gate
34980Sstevel@tonic-gate xyerror(D_OP_INCOMPAT,
34990Sstevel@tonic-gate "operands have incompatible types: \"%s\" %s \"%s\"\n",
35000Sstevel@tonic-gate dt_node_type_name(lp, n1, sizeof (n1)), opstr(op),
35010Sstevel@tonic-gate dt_node_type_name(rp, n2, sizeof (n2)));
35020Sstevel@tonic-gate /*NOTREACHED*/
35030Sstevel@tonic-gate
35040Sstevel@tonic-gate case DT_TOK_ADD_EQ:
35050Sstevel@tonic-gate case DT_TOK_SUB_EQ:
35060Sstevel@tonic-gate if (lp->dn_kind == DT_NODE_IDENT) {
35070Sstevel@tonic-gate dt_xcook_ident(lp, dtp->dt_globals,
35080Sstevel@tonic-gate DT_IDENT_SCALAR, B_TRUE);
35090Sstevel@tonic-gate }
35100Sstevel@tonic-gate
35110Sstevel@tonic-gate lp = dnp->dn_left =
35120Sstevel@tonic-gate dt_node_cook(lp, DT_IDFLG_REF | DT_IDFLG_MOD);
35130Sstevel@tonic-gate
35140Sstevel@tonic-gate rp = dnp->dn_right =
35150Sstevel@tonic-gate dt_node_cook(rp, DT_IDFLG_REF | DT_IDFLG_MOD);
35160Sstevel@tonic-gate
35170Sstevel@tonic-gate if (dt_node_is_string(lp) || dt_node_is_string(rp)) {
35180Sstevel@tonic-gate xyerror(D_OP_INCOMPAT, "operands have "
35190Sstevel@tonic-gate "incompatible types: \"%s\" %s \"%s\"\n",
35200Sstevel@tonic-gate dt_node_type_name(lp, n1, sizeof (n1)), opstr(op),
35210Sstevel@tonic-gate dt_node_type_name(rp, n2, sizeof (n2)));
35220Sstevel@tonic-gate }
35230Sstevel@tonic-gate
35240Sstevel@tonic-gate /*
35250Sstevel@tonic-gate * The rules for type checking for the assignment operators are
35260Sstevel@tonic-gate * described in the ANSI-C spec (see K&R[A7.17]). To these
35270Sstevel@tonic-gate * rules we add that only writable D nodes can be modified.
35280Sstevel@tonic-gate */
35290Sstevel@tonic-gate if (dt_node_is_integer(lp) == 0 ||
35300Sstevel@tonic-gate dt_node_is_integer(rp) == 0) {
35310Sstevel@tonic-gate if (!dt_node_is_pointer(lp) || dt_node_is_vfptr(lp)) {
35320Sstevel@tonic-gate xyerror(D_OP_VFPTR,
35330Sstevel@tonic-gate "operator %s requires left-hand scalar "
35340Sstevel@tonic-gate "operand of known size\n", opstr(op));
35350Sstevel@tonic-gate } else if (dt_node_is_integer(rp) == 0 &&
35360Sstevel@tonic-gate dt_node_is_ptrcompat(lp, rp, NULL, NULL) == 0) {
35370Sstevel@tonic-gate xyerror(D_OP_INCOMPAT, "operands have "
35380Sstevel@tonic-gate "incompatible types: \"%s\" %s \"%s\"\n",
35390Sstevel@tonic-gate dt_node_type_name(lp, n1, sizeof (n1)),
35400Sstevel@tonic-gate opstr(op),
35410Sstevel@tonic-gate dt_node_type_name(rp, n2, sizeof (n2)));
35420Sstevel@tonic-gate }
35430Sstevel@tonic-gate }
35440Sstevel@tonic-gate asgn_common:
35450Sstevel@tonic-gate if (!(lp->dn_flags & DT_NF_LVALUE)) {
35460Sstevel@tonic-gate xyerror(D_OP_LVAL, "operator %s requires modifiable "
35470Sstevel@tonic-gate "lvalue as an operand\n", opstr(op));
35480Sstevel@tonic-gate /* see K&R[A7.17] */
35490Sstevel@tonic-gate }
35500Sstevel@tonic-gate
35510Sstevel@tonic-gate if (!(lp->dn_flags & DT_NF_WRITABLE)) {
35520Sstevel@tonic-gate xyerror(D_OP_WRITE, "operator %s can only be applied "
35530Sstevel@tonic-gate "to a writable variable\n", opstr(op));
35540Sstevel@tonic-gate }
35550Sstevel@tonic-gate
35560Sstevel@tonic-gate dt_node_type_propagate(lp, dnp); /* see K&R[A7.17] */
35570Sstevel@tonic-gate dt_node_attr_assign(dnp, dt_attr_min(lp->dn_attr, rp->dn_attr));
35580Sstevel@tonic-gate break;
35590Sstevel@tonic-gate
35600Sstevel@tonic-gate case DT_TOK_PTR:
35610Sstevel@tonic-gate /*
35620Sstevel@tonic-gate * If the left-hand side of operator -> is the name "self",
35630Sstevel@tonic-gate * then we permit a TLS variable to be created or referenced.
35640Sstevel@tonic-gate */
35650Sstevel@tonic-gate if (lp->dn_kind == DT_NODE_IDENT &&
35660Sstevel@tonic-gate strcmp(lp->dn_string, "self") == 0) {
35670Sstevel@tonic-gate if (rp->dn_kind != DT_NODE_VAR) {
35680Sstevel@tonic-gate dt_xcook_ident(rp, dtp->dt_tls,
35690Sstevel@tonic-gate DT_IDENT_SCALAR, B_TRUE);
35700Sstevel@tonic-gate }
35710Sstevel@tonic-gate
35720Sstevel@tonic-gate if (idflags != 0)
35730Sstevel@tonic-gate rp = dt_node_cook(rp, idflags);
35740Sstevel@tonic-gate
35750Sstevel@tonic-gate dnp->dn_right = dnp->dn_left; /* avoid freeing rp */
35760Sstevel@tonic-gate dt_node_free(dnp);
35770Sstevel@tonic-gate return (rp);
35780Sstevel@tonic-gate }
35790Sstevel@tonic-gate
35800Sstevel@tonic-gate /*
35810Sstevel@tonic-gate * If the left-hand side of operator -> is the name "this",
35820Sstevel@tonic-gate * then we permit a local variable to be created or referenced.
35830Sstevel@tonic-gate */
35840Sstevel@tonic-gate if (lp->dn_kind == DT_NODE_IDENT &&
35850Sstevel@tonic-gate strcmp(lp->dn_string, "this") == 0) {
35860Sstevel@tonic-gate if (rp->dn_kind != DT_NODE_VAR) {
35870Sstevel@tonic-gate dt_xcook_ident(rp, yypcb->pcb_locals,
35880Sstevel@tonic-gate DT_IDENT_SCALAR, B_TRUE);
35890Sstevel@tonic-gate }
35900Sstevel@tonic-gate
35910Sstevel@tonic-gate if (idflags != 0)
35920Sstevel@tonic-gate rp = dt_node_cook(rp, idflags);
35930Sstevel@tonic-gate
35940Sstevel@tonic-gate dnp->dn_right = dnp->dn_left; /* avoid freeing rp */
35950Sstevel@tonic-gate dt_node_free(dnp);
35960Sstevel@tonic-gate return (rp);
35970Sstevel@tonic-gate }
35980Sstevel@tonic-gate
35990Sstevel@tonic-gate /*FALLTHRU*/
36000Sstevel@tonic-gate
36010Sstevel@tonic-gate case DT_TOK_DOT:
36020Sstevel@tonic-gate lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF);
36030Sstevel@tonic-gate
36040Sstevel@tonic-gate if (rp->dn_kind != DT_NODE_IDENT) {
36050Sstevel@tonic-gate xyerror(D_OP_IDENT, "operator %s must be followed by "
36060Sstevel@tonic-gate "an identifier\n", opstr(op));
36070Sstevel@tonic-gate }
36080Sstevel@tonic-gate
36090Sstevel@tonic-gate if ((idp = dt_node_resolve(lp, DT_IDENT_XLSOU)) != NULL ||
36100Sstevel@tonic-gate (idp = dt_node_resolve(lp, DT_IDENT_XLPTR)) != NULL) {
36110Sstevel@tonic-gate /*
36120Sstevel@tonic-gate * If the left-hand side is a translated struct or ptr,
36130Sstevel@tonic-gate * the type of the left is the translation output type.
36140Sstevel@tonic-gate */
36150Sstevel@tonic-gate dt_xlator_t *dxp = idp->di_data;
36160Sstevel@tonic-gate
36170Sstevel@tonic-gate if (dt_xlator_member(dxp, rp->dn_string) == NULL) {
36180Sstevel@tonic-gate xyerror(D_XLATE_NOCONV,
36190Sstevel@tonic-gate "translator does not define conversion "
36200Sstevel@tonic-gate "for member: %s\n", rp->dn_string);
36210Sstevel@tonic-gate }
36220Sstevel@tonic-gate
36230Sstevel@tonic-gate ctfp = idp->di_ctfp;
36240Sstevel@tonic-gate type = ctf_type_resolve(ctfp, idp->di_type);
36250Sstevel@tonic-gate uref = idp->di_flags & DT_IDFLG_USER;
36260Sstevel@tonic-gate } else {
36270Sstevel@tonic-gate ctfp = lp->dn_ctfp;
36280Sstevel@tonic-gate type = ctf_type_resolve(ctfp, lp->dn_type);
36290Sstevel@tonic-gate uref = lp->dn_flags & DT_NF_USERLAND;
36300Sstevel@tonic-gate }
36310Sstevel@tonic-gate
36320Sstevel@tonic-gate kind = ctf_type_kind(ctfp, type);
36330Sstevel@tonic-gate
36340Sstevel@tonic-gate if (op == DT_TOK_PTR) {
36350Sstevel@tonic-gate if (kind != CTF_K_POINTER) {
36360Sstevel@tonic-gate xyerror(D_OP_PTR, "operator %s must be "
36370Sstevel@tonic-gate "applied to a pointer\n", opstr(op));
36380Sstevel@tonic-gate }
36390Sstevel@tonic-gate type = ctf_type_reference(ctfp, type);
36400Sstevel@tonic-gate type = ctf_type_resolve(ctfp, type);
36410Sstevel@tonic-gate kind = ctf_type_kind(ctfp, type);
36420Sstevel@tonic-gate }
36430Sstevel@tonic-gate
36440Sstevel@tonic-gate /*
36450Sstevel@tonic-gate * If we follow a reference to a forward declaration tag,
36460Sstevel@tonic-gate * search the entire type space for the actual definition.
36470Sstevel@tonic-gate */
36480Sstevel@tonic-gate while (kind == CTF_K_FORWARD) {
36490Sstevel@tonic-gate char *tag = ctf_type_name(ctfp, type, n1, sizeof (n1));
36500Sstevel@tonic-gate dtrace_typeinfo_t dtt;
36510Sstevel@tonic-gate
36520Sstevel@tonic-gate if (tag != NULL && dt_type_lookup(tag, &dtt) == 0 &&
36530Sstevel@tonic-gate (dtt.dtt_ctfp != ctfp || dtt.dtt_type != type)) {
36540Sstevel@tonic-gate ctfp = dtt.dtt_ctfp;
36550Sstevel@tonic-gate type = ctf_type_resolve(ctfp, dtt.dtt_type);
36560Sstevel@tonic-gate kind = ctf_type_kind(ctfp, type);
36570Sstevel@tonic-gate } else {
36580Sstevel@tonic-gate xyerror(D_OP_INCOMPLETE,
36590Sstevel@tonic-gate "operator %s cannot be applied to a "
36600Sstevel@tonic-gate "forward declaration: no %s definition "
36610Sstevel@tonic-gate "is available\n", opstr(op), tag);
36620Sstevel@tonic-gate }
36630Sstevel@tonic-gate }
36640Sstevel@tonic-gate
36650Sstevel@tonic-gate if (kind != CTF_K_STRUCT && kind != CTF_K_UNION) {
36660Sstevel@tonic-gate if (op == DT_TOK_PTR) {
36670Sstevel@tonic-gate xyerror(D_OP_SOU, "operator -> cannot be "
36680Sstevel@tonic-gate "applied to pointer to type \"%s\"; must "
36690Sstevel@tonic-gate "be applied to a struct or union pointer\n",
36700Sstevel@tonic-gate ctf_type_name(ctfp, type, n1, sizeof (n1)));
36710Sstevel@tonic-gate } else {
36720Sstevel@tonic-gate xyerror(D_OP_SOU, "operator %s cannot be "
36730Sstevel@tonic-gate "applied to type \"%s\"; must be applied "
36740Sstevel@tonic-gate "to a struct or union\n", opstr(op),
36750Sstevel@tonic-gate ctf_type_name(ctfp, type, n1, sizeof (n1)));
36760Sstevel@tonic-gate }
36770Sstevel@tonic-gate }
36780Sstevel@tonic-gate
36790Sstevel@tonic-gate if (ctf_member_info(ctfp, type, rp->dn_string, &m) == CTF_ERR) {
36800Sstevel@tonic-gate xyerror(D_TYPE_MEMBER,
36810Sstevel@tonic-gate "%s is not a member of %s\n", rp->dn_string,
36820Sstevel@tonic-gate ctf_type_name(ctfp, type, n1, sizeof (n1)));
36830Sstevel@tonic-gate }
36840Sstevel@tonic-gate
36850Sstevel@tonic-gate type = ctf_type_resolve(ctfp, m.ctm_type);
36860Sstevel@tonic-gate kind = ctf_type_kind(ctfp, type);
36870Sstevel@tonic-gate
36880Sstevel@tonic-gate dt_node_type_assign(dnp, ctfp, m.ctm_type);
36890Sstevel@tonic-gate dt_node_attr_assign(dnp, lp->dn_attr);
36900Sstevel@tonic-gate
36910Sstevel@tonic-gate if (op == DT_TOK_PTR && (kind != CTF_K_ARRAY ||
36920Sstevel@tonic-gate dt_node_is_string(dnp)))
36930Sstevel@tonic-gate dnp->dn_flags |= DT_NF_LVALUE; /* see K&R[A7.3.3] */
36940Sstevel@tonic-gate
36950Sstevel@tonic-gate if (op == DT_TOK_DOT && (lp->dn_flags & DT_NF_LVALUE) &&
36960Sstevel@tonic-gate (kind != CTF_K_ARRAY || dt_node_is_string(dnp)))
36970Sstevel@tonic-gate dnp->dn_flags |= DT_NF_LVALUE; /* see K&R[A7.3.3] */
36980Sstevel@tonic-gate
36990Sstevel@tonic-gate if (lp->dn_flags & DT_NF_WRITABLE)
37000Sstevel@tonic-gate dnp->dn_flags |= DT_NF_WRITABLE;
37010Sstevel@tonic-gate
37020Sstevel@tonic-gate if (uref && (kind == CTF_K_POINTER ||
37030Sstevel@tonic-gate (dnp->dn_flags & DT_NF_REF)))
37040Sstevel@tonic-gate dnp->dn_flags |= DT_NF_USERLAND;
37050Sstevel@tonic-gate break;
37060Sstevel@tonic-gate
37070Sstevel@tonic-gate case DT_TOK_LBRAC: {
37080Sstevel@tonic-gate /*
37090Sstevel@tonic-gate * If op is DT_TOK_LBRAC, we know from the special-case code at
37100Sstevel@tonic-gate * the top that lp is either a D variable or an aggregation.
37110Sstevel@tonic-gate */
37120Sstevel@tonic-gate dt_node_t *lnp;
37130Sstevel@tonic-gate
37140Sstevel@tonic-gate /*
37150Sstevel@tonic-gate * If the left-hand side is an aggregation, just set dn_aggtup
37160Sstevel@tonic-gate * to the right-hand side and return the cooked aggregation.
37170Sstevel@tonic-gate * This transformation is legal since we are just collapsing
37180Sstevel@tonic-gate * nodes to simplify later processing, and the entire aggtup
37190Sstevel@tonic-gate * parse subtree is retained for subsequent cooking passes.
37200Sstevel@tonic-gate */
37210Sstevel@tonic-gate if (lp->dn_kind == DT_NODE_AGG) {
37220Sstevel@tonic-gate if (lp->dn_aggtup != NULL) {
37230Sstevel@tonic-gate xyerror(D_AGG_MDIM, "improper attempt to "
37240Sstevel@tonic-gate "reference @%s as a multi-dimensional "
37250Sstevel@tonic-gate "array\n", lp->dn_ident->di_name);
37260Sstevel@tonic-gate }
37270Sstevel@tonic-gate
37280Sstevel@tonic-gate lp->dn_aggtup = rp;
37290Sstevel@tonic-gate lp = dt_node_cook(lp, 0);
37300Sstevel@tonic-gate
37310Sstevel@tonic-gate dnp->dn_left = dnp->dn_right = NULL;
37320Sstevel@tonic-gate dt_node_free(dnp);
37330Sstevel@tonic-gate
37340Sstevel@tonic-gate return (lp);
37350Sstevel@tonic-gate }
37360Sstevel@tonic-gate
37370Sstevel@tonic-gate assert(lp->dn_kind == DT_NODE_VAR);
37380Sstevel@tonic-gate idp = lp->dn_ident;
37390Sstevel@tonic-gate
37400Sstevel@tonic-gate /*
37410Sstevel@tonic-gate * If the left-hand side is a non-global scalar that hasn't yet
37420Sstevel@tonic-gate * been referenced or modified, it was just created by self->
37430Sstevel@tonic-gate * or this-> and we can convert it from scalar to assoc array.
37440Sstevel@tonic-gate */
37450Sstevel@tonic-gate if (idp->di_kind == DT_IDENT_SCALAR && dt_ident_unref(idp) &&
37460Sstevel@tonic-gate (idp->di_flags & (DT_IDFLG_LOCAL | DT_IDFLG_TLS)) != 0) {
37470Sstevel@tonic-gate
37480Sstevel@tonic-gate if (idp->di_flags & DT_IDFLG_LOCAL) {
37490Sstevel@tonic-gate xyerror(D_ARR_LOCAL,
37500Sstevel@tonic-gate "local variables may not be used as "
37510Sstevel@tonic-gate "associative arrays: %s\n", idp->di_name);
37520Sstevel@tonic-gate }
37530Sstevel@tonic-gate
37540Sstevel@tonic-gate dt_dprintf("morph variable %s (id %u) from scalar to "
37550Sstevel@tonic-gate "array\n", idp->di_name, idp->di_id);
37560Sstevel@tonic-gate
37570Sstevel@tonic-gate dt_ident_morph(idp, DT_IDENT_ARRAY,
37580Sstevel@tonic-gate &dt_idops_assc, NULL);
37590Sstevel@tonic-gate }
37600Sstevel@tonic-gate
37610Sstevel@tonic-gate if (idp->di_kind != DT_IDENT_ARRAY) {
37620Sstevel@tonic-gate xyerror(D_IDENT_BADREF, "%s '%s' may not be referenced "
37630Sstevel@tonic-gate "as %s\n", dt_idkind_name(idp->di_kind),
37640Sstevel@tonic-gate idp->di_name, dt_idkind_name(DT_IDENT_ARRAY));
37650Sstevel@tonic-gate }
37660Sstevel@tonic-gate
37670Sstevel@tonic-gate /*
37680Sstevel@tonic-gate * Now that we've confirmed our left-hand side is a DT_NODE_VAR
37690Sstevel@tonic-gate * of idkind DT_IDENT_ARRAY, we need to splice the [ node from
37700Sstevel@tonic-gate * the parse tree and leave a cooked DT_NODE_VAR in its place
37710Sstevel@tonic-gate * where dn_args for the VAR node is the right-hand 'rp' tree,
37720Sstevel@tonic-gate * as shown in the parse tree diagram below:
37730Sstevel@tonic-gate *
37740Sstevel@tonic-gate * / /
37750Sstevel@tonic-gate * [ OP2 "[" ]=dnp [ VAR ]=dnp
37760Sstevel@tonic-gate * / \ => |
37770Sstevel@tonic-gate * / \ +- dn_args -> [ ??? ]=rp
37780Sstevel@tonic-gate * [ VAR ]=lp [ ??? ]=rp
37790Sstevel@tonic-gate *
37800Sstevel@tonic-gate * Since the final dt_node_cook(dnp) can fail using longjmp we
37810Sstevel@tonic-gate * must perform the transformations as a group first by over-
37820Sstevel@tonic-gate * writing 'dnp' to become the VAR node, so that the parse tree
37830Sstevel@tonic-gate * is guaranteed to be in a consistent state if the cook fails.
37840Sstevel@tonic-gate */
37850Sstevel@tonic-gate assert(lp->dn_kind == DT_NODE_VAR);
37860Sstevel@tonic-gate assert(lp->dn_args == NULL);
37870Sstevel@tonic-gate
37880Sstevel@tonic-gate lnp = dnp->dn_link;
37890Sstevel@tonic-gate bcopy(lp, dnp, sizeof (dt_node_t));
37900Sstevel@tonic-gate dnp->dn_link = lnp;
37910Sstevel@tonic-gate
37920Sstevel@tonic-gate dnp->dn_args = rp;
37930Sstevel@tonic-gate dnp->dn_list = NULL;
37940Sstevel@tonic-gate
37950Sstevel@tonic-gate dt_node_free(lp);
37960Sstevel@tonic-gate return (dt_node_cook(dnp, idflags));
37970Sstevel@tonic-gate }
37980Sstevel@tonic-gate
37990Sstevel@tonic-gate case DT_TOK_XLATE: {
38000Sstevel@tonic-gate dt_xlator_t *dxp;
38010Sstevel@tonic-gate
38020Sstevel@tonic-gate assert(lp->dn_kind == DT_NODE_TYPE);
38030Sstevel@tonic-gate rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
38040Sstevel@tonic-gate dxp = dt_xlator_lookup(dtp, rp, lp, DT_XLATE_FUZZY);
38050Sstevel@tonic-gate
38060Sstevel@tonic-gate if (dxp == NULL) {
38070Sstevel@tonic-gate xyerror(D_XLATE_NONE,
38080Sstevel@tonic-gate "cannot translate from \"%s\" to \"%s\"\n",
38090Sstevel@tonic-gate dt_node_type_name(rp, n1, sizeof (n1)),
38100Sstevel@tonic-gate dt_node_type_name(lp, n2, sizeof (n2)));
38110Sstevel@tonic-gate }
38120Sstevel@tonic-gate
38130Sstevel@tonic-gate dnp->dn_ident = dt_xlator_ident(dxp, lp->dn_ctfp, lp->dn_type);
38140Sstevel@tonic-gate dt_node_type_assign(dnp, DT_DYN_CTFP(dtp), DT_DYN_TYPE(dtp));
38150Sstevel@tonic-gate dt_node_attr_assign(dnp,
38160Sstevel@tonic-gate dt_attr_min(rp->dn_attr, dnp->dn_ident->di_attr));
38170Sstevel@tonic-gate break;
38180Sstevel@tonic-gate }
38190Sstevel@tonic-gate
38200Sstevel@tonic-gate case DT_TOK_LPAR: {
38210Sstevel@tonic-gate ctf_id_t ltype, rtype;
38220Sstevel@tonic-gate uint_t lkind, rkind;
38230Sstevel@tonic-gate
38240Sstevel@tonic-gate assert(lp->dn_kind == DT_NODE_TYPE);
38250Sstevel@tonic-gate rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
38260Sstevel@tonic-gate
38270Sstevel@tonic-gate ltype = ctf_type_resolve(lp->dn_ctfp, lp->dn_type);
38280Sstevel@tonic-gate lkind = ctf_type_kind(lp->dn_ctfp, ltype);
38290Sstevel@tonic-gate
38300Sstevel@tonic-gate rtype = ctf_type_resolve(rp->dn_ctfp, rp->dn_type);
38310Sstevel@tonic-gate rkind = ctf_type_kind(rp->dn_ctfp, rtype);
38320Sstevel@tonic-gate
38330Sstevel@tonic-gate /*
38340Sstevel@tonic-gate * The rules for casting are loosely explained in K&R[A7.5]
38350Sstevel@tonic-gate * and K&R[A6]. Basically, we can cast to the same type or
38360Sstevel@tonic-gate * same base type, between any kind of scalar values, from
38370Sstevel@tonic-gate * arrays to pointers, and we can cast anything to void.
38380Sstevel@tonic-gate * To these rules D adds casts from scalars to strings.
38390Sstevel@tonic-gate */
38400Sstevel@tonic-gate if (ctf_type_compat(lp->dn_ctfp, lp->dn_type,
38410Sstevel@tonic-gate rp->dn_ctfp, rp->dn_type))
38420Sstevel@tonic-gate /*EMPTY*/;
38430Sstevel@tonic-gate else if (dt_node_is_scalar(lp) &&
38440Sstevel@tonic-gate (dt_node_is_scalar(rp) || rkind == CTF_K_FUNCTION))
38450Sstevel@tonic-gate /*EMPTY*/;
38460Sstevel@tonic-gate else if (dt_node_is_void(lp))
38470Sstevel@tonic-gate /*EMPTY*/;
38480Sstevel@tonic-gate else if (lkind == CTF_K_POINTER && dt_node_is_pointer(rp))
38490Sstevel@tonic-gate /*EMPTY*/;
38500Sstevel@tonic-gate else if (dt_node_is_string(lp) && (dt_node_is_scalar(rp) ||
38510Sstevel@tonic-gate dt_node_is_pointer(rp) || dt_node_is_strcompat(rp)))
38520Sstevel@tonic-gate /*EMPTY*/;
38530Sstevel@tonic-gate else {
38540Sstevel@tonic-gate xyerror(D_CAST_INVAL,
38550Sstevel@tonic-gate "invalid cast expression: \"%s\" to \"%s\"\n",
38560Sstevel@tonic-gate dt_node_type_name(rp, n1, sizeof (n1)),
38570Sstevel@tonic-gate dt_node_type_name(lp, n2, sizeof (n2)));
38580Sstevel@tonic-gate }
38590Sstevel@tonic-gate
38600Sstevel@tonic-gate dt_node_type_propagate(lp, dnp); /* see K&R[A7.5] */
38610Sstevel@tonic-gate dt_node_attr_assign(dnp, dt_attr_min(lp->dn_attr, rp->dn_attr));
38620Sstevel@tonic-gate break;
38630Sstevel@tonic-gate }
38640Sstevel@tonic-gate
38650Sstevel@tonic-gate case DT_TOK_COMMA:
38660Sstevel@tonic-gate lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF);
38670Sstevel@tonic-gate rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
38680Sstevel@tonic-gate
38690Sstevel@tonic-gate if (dt_node_is_dynamic(lp) || dt_node_is_dynamic(rp)) {
38700Sstevel@tonic-gate xyerror(D_OP_DYN, "operator %s operands "
38710Sstevel@tonic-gate "cannot be of dynamic type\n", opstr(op));
38720Sstevel@tonic-gate }
38730Sstevel@tonic-gate
38740Sstevel@tonic-gate if (dt_node_is_actfunc(lp) || dt_node_is_actfunc(rp)) {
38750Sstevel@tonic-gate xyerror(D_OP_ACT, "operator %s operands "
38760Sstevel@tonic-gate "cannot be actions\n", opstr(op));
38770Sstevel@tonic-gate }
38780Sstevel@tonic-gate
38790Sstevel@tonic-gate dt_node_type_propagate(rp, dnp); /* see K&R[A7.18] */
38800Sstevel@tonic-gate dt_node_attr_assign(dnp, dt_attr_min(lp->dn_attr, rp->dn_attr));
38810Sstevel@tonic-gate break;
38820Sstevel@tonic-gate
38830Sstevel@tonic-gate default:
38840Sstevel@tonic-gate xyerror(D_UNKNOWN, "invalid binary op %s\n", opstr(op));
38850Sstevel@tonic-gate }
38860Sstevel@tonic-gate
38870Sstevel@tonic-gate /*
38880Sstevel@tonic-gate * Complete the conversion of E1[E2] to *((E1)+(E2)) that we started
38890Sstevel@tonic-gate * at the top of our switch() above (see K&R[A7.3.1]). Since E2 is
38900Sstevel@tonic-gate * parsed as an argument_expression_list by dt_grammar.y, we can
38910Sstevel@tonic-gate * end up with a comma-separated list inside of a non-associative
38920Sstevel@tonic-gate * array reference. We check for this and report an appropriate error.
38930Sstevel@tonic-gate */
38940Sstevel@tonic-gate if (dnp->dn_op == DT_TOK_LBRAC && op == DT_TOK_ADD) {
38950Sstevel@tonic-gate dt_node_t *pnp;
38960Sstevel@tonic-gate
38970Sstevel@tonic-gate if (rp->dn_list != NULL) {
38980Sstevel@tonic-gate xyerror(D_ARR_BADREF,
38990Sstevel@tonic-gate "cannot access %s as an associative array\n",
39000Sstevel@tonic-gate dt_node_name(lp, n1, sizeof (n1)));
39010Sstevel@tonic-gate }
39020Sstevel@tonic-gate
39030Sstevel@tonic-gate dnp->dn_op = DT_TOK_ADD;
39040Sstevel@tonic-gate pnp = dt_node_op1(DT_TOK_DEREF, dnp);
39050Sstevel@tonic-gate
39060Sstevel@tonic-gate /*
39070Sstevel@tonic-gate * Cook callbacks are not typically permitted to allocate nodes.
39080Sstevel@tonic-gate * When we do, we must insert them in the middle of an existing
39090Sstevel@tonic-gate * allocation list rather than having them appended to the pcb
39100Sstevel@tonic-gate * list because the sub-expression may be part of a definition.
39110Sstevel@tonic-gate */
39120Sstevel@tonic-gate assert(yypcb->pcb_list == pnp);
39130Sstevel@tonic-gate yypcb->pcb_list = pnp->dn_link;
39140Sstevel@tonic-gate
39150Sstevel@tonic-gate pnp->dn_link = dnp->dn_link;
39160Sstevel@tonic-gate dnp->dn_link = pnp;
39170Sstevel@tonic-gate
39180Sstevel@tonic-gate return (dt_node_cook(pnp, DT_IDFLG_REF));
39190Sstevel@tonic-gate }
39200Sstevel@tonic-gate
39210Sstevel@tonic-gate return (dnp);
39220Sstevel@tonic-gate }
39230Sstevel@tonic-gate
39240Sstevel@tonic-gate /*ARGSUSED*/
39250Sstevel@tonic-gate static dt_node_t *
dt_cook_op3(dt_node_t * dnp,uint_t idflags)39260Sstevel@tonic-gate dt_cook_op3(dt_node_t *dnp, uint_t idflags)
39270Sstevel@tonic-gate {
39280Sstevel@tonic-gate dt_node_t *lp, *rp;
39290Sstevel@tonic-gate ctf_file_t *ctfp;
39300Sstevel@tonic-gate ctf_id_t type;
39310Sstevel@tonic-gate
39320Sstevel@tonic-gate dnp->dn_expr = dt_node_cook(dnp->dn_expr, DT_IDFLG_REF);
39330Sstevel@tonic-gate lp = dnp->dn_left = dt_node_cook(dnp->dn_left, DT_IDFLG_REF);
39340Sstevel@tonic-gate rp = dnp->dn_right = dt_node_cook(dnp->dn_right, DT_IDFLG_REF);
39350Sstevel@tonic-gate
39360Sstevel@tonic-gate if (!dt_node_is_scalar(dnp->dn_expr)) {
39370Sstevel@tonic-gate xyerror(D_OP_SCALAR,
39380Sstevel@tonic-gate "operator ?: expression must be of scalar type\n");
39390Sstevel@tonic-gate }
39400Sstevel@tonic-gate
39410Sstevel@tonic-gate if (dt_node_is_dynamic(lp) || dt_node_is_dynamic(rp)) {
39420Sstevel@tonic-gate xyerror(D_OP_DYN,
39430Sstevel@tonic-gate "operator ?: operands cannot be of dynamic type\n");
39440Sstevel@tonic-gate }
39450Sstevel@tonic-gate
39460Sstevel@tonic-gate /*
39470Sstevel@tonic-gate * The rules for type checking for the ternary operator are complex and
39480Sstevel@tonic-gate * are described in the ANSI-C spec (see K&R[A7.16]). We implement
39490Sstevel@tonic-gate * the various tests in order from least to most expensive.
39500Sstevel@tonic-gate */
39510Sstevel@tonic-gate if (ctf_type_compat(lp->dn_ctfp, lp->dn_type,
39520Sstevel@tonic-gate rp->dn_ctfp, rp->dn_type)) {
39530Sstevel@tonic-gate ctfp = lp->dn_ctfp;
39540Sstevel@tonic-gate type = lp->dn_type;
39550Sstevel@tonic-gate } else if (dt_node_is_integer(lp) && dt_node_is_integer(rp)) {
39560Sstevel@tonic-gate dt_type_promote(lp, rp, &ctfp, &type);
39570Sstevel@tonic-gate } else if (dt_node_is_strcompat(lp) && dt_node_is_strcompat(rp) &&
39580Sstevel@tonic-gate (dt_node_is_string(lp) || dt_node_is_string(rp))) {
39590Sstevel@tonic-gate ctfp = DT_STR_CTFP(yypcb->pcb_hdl);
39600Sstevel@tonic-gate type = DT_STR_TYPE(yypcb->pcb_hdl);
39610Sstevel@tonic-gate } else if (dt_node_is_ptrcompat(lp, rp, &ctfp, &type) == 0) {
39620Sstevel@tonic-gate xyerror(D_OP_INCOMPAT,
39630Sstevel@tonic-gate "operator ?: operands must have compatible types\n");
39640Sstevel@tonic-gate }
39650Sstevel@tonic-gate
39660Sstevel@tonic-gate if (dt_node_is_actfunc(lp) || dt_node_is_actfunc(rp)) {
39670Sstevel@tonic-gate xyerror(D_OP_ACT, "action cannot be "
39680Sstevel@tonic-gate "used in a conditional context\n");
39690Sstevel@tonic-gate }
39700Sstevel@tonic-gate
39710Sstevel@tonic-gate dt_node_type_assign(dnp, ctfp, type);
39720Sstevel@tonic-gate dt_node_attr_assign(dnp, dt_attr_min(dnp->dn_expr->dn_attr,
39730Sstevel@tonic-gate dt_attr_min(lp->dn_attr, rp->dn_attr)));
39740Sstevel@tonic-gate
39750Sstevel@tonic-gate return (dnp);
39760Sstevel@tonic-gate }
39770Sstevel@tonic-gate
39780Sstevel@tonic-gate static dt_node_t *
dt_cook_statement(dt_node_t * dnp,uint_t idflags)39790Sstevel@tonic-gate dt_cook_statement(dt_node_t *dnp, uint_t idflags)
39800Sstevel@tonic-gate {
39810Sstevel@tonic-gate dnp->dn_expr = dt_node_cook(dnp->dn_expr, idflags);
39820Sstevel@tonic-gate dt_node_attr_assign(dnp, dnp->dn_expr->dn_attr);
39830Sstevel@tonic-gate
39840Sstevel@tonic-gate return (dnp);
39850Sstevel@tonic-gate }
39860Sstevel@tonic-gate
39870Sstevel@tonic-gate /*
39880Sstevel@tonic-gate * If dn_aggfun is set, this node is a collapsed aggregation assignment (see
39890Sstevel@tonic-gate * the special case code for DT_TOK_ASGN in dt_cook_op2() above), in which
39900Sstevel@tonic-gate * case we cook both the tuple and the function call. If dn_aggfun is NULL,
39910Sstevel@tonic-gate * this node is just a reference to the aggregation's type and attributes.
39920Sstevel@tonic-gate */
39930Sstevel@tonic-gate /*ARGSUSED*/
39940Sstevel@tonic-gate static dt_node_t *
dt_cook_aggregation(dt_node_t * dnp,uint_t idflags)39950Sstevel@tonic-gate dt_cook_aggregation(dt_node_t *dnp, uint_t idflags)
39960Sstevel@tonic-gate {
39970Sstevel@tonic-gate dtrace_hdl_t *dtp = yypcb->pcb_hdl;
39980Sstevel@tonic-gate
39990Sstevel@tonic-gate if (dnp->dn_aggfun != NULL) {
40000Sstevel@tonic-gate dnp->dn_aggfun = dt_node_cook(dnp->dn_aggfun, DT_IDFLG_REF);
40010Sstevel@tonic-gate dt_node_attr_assign(dnp, dt_ident_cook(dnp,
40020Sstevel@tonic-gate dnp->dn_ident, &dnp->dn_aggtup));
40030Sstevel@tonic-gate } else {
40040Sstevel@tonic-gate dt_node_type_assign(dnp, DT_DYN_CTFP(dtp), DT_DYN_TYPE(dtp));
40050Sstevel@tonic-gate dt_node_attr_assign(dnp, dnp->dn_ident->di_attr);
40060Sstevel@tonic-gate }
40070Sstevel@tonic-gate
40080Sstevel@tonic-gate return (dnp);
40090Sstevel@tonic-gate }
40100Sstevel@tonic-gate
40110Sstevel@tonic-gate /*
40120Sstevel@tonic-gate * Since D permits new variable identifiers to be instantiated in any program
40130Sstevel@tonic-gate * expression, we may need to cook a clause's predicate either before or after
40140Sstevel@tonic-gate * the action list depending on the program code in question. Consider:
40150Sstevel@tonic-gate *
40160Sstevel@tonic-gate * probe-description-list probe-description-list
40170Sstevel@tonic-gate * /x++/ /x == 0/
40180Sstevel@tonic-gate * { {
40190Sstevel@tonic-gate * trace(x); trace(x++);
40200Sstevel@tonic-gate * } }
40210Sstevel@tonic-gate *
40220Sstevel@tonic-gate * In the left-hand example, the predicate uses operator ++ to instantiate 'x'
40230Sstevel@tonic-gate * as a variable of type int64_t. The predicate must be cooked first because
40240Sstevel@tonic-gate * otherwise the statement trace(x) refers to an unknown identifier. In the
40250Sstevel@tonic-gate * right-hand example, the action list uses ++ to instantiate 'x'; the action
40260Sstevel@tonic-gate * list must be cooked first because otherwise the predicate x == 0 refers to
40270Sstevel@tonic-gate * an unknown identifier. In order to simplify programming, we support both.
40280Sstevel@tonic-gate *
40290Sstevel@tonic-gate * When cooking a clause, we cook the action statements before the predicate by
40300Sstevel@tonic-gate * default, since it seems more common to create or modify identifiers in the
40310Sstevel@tonic-gate * action list. If cooking fails due to an unknown identifier, we attempt to
40320Sstevel@tonic-gate * cook the predicate (i.e. do it first) and then go back and cook the actions.
40330Sstevel@tonic-gate * If this, too, fails (or if we get an error other than D_IDENT_UNDEF) we give
40340Sstevel@tonic-gate * up and report failure back to the user. There are five possible paths:
40350Sstevel@tonic-gate *
40360Sstevel@tonic-gate * cook actions = OK, cook predicate = OK -> OK
40370Sstevel@tonic-gate * cook actions = OK, cook predicate = ERR -> ERR
40380Sstevel@tonic-gate * cook actions = ERR, cook predicate = ERR -> ERR
40390Sstevel@tonic-gate * cook actions = ERR, cook predicate = OK, cook actions = OK -> OK
40400Sstevel@tonic-gate * cook actions = ERR, cook predicate = OK, cook actions = ERR -> ERR
40410Sstevel@tonic-gate *
40420Sstevel@tonic-gate * The programmer can still defeat our scheme by creating circular definition
40430Sstevel@tonic-gate * dependencies between predicates and actions, as in this example clause:
40440Sstevel@tonic-gate *
40450Sstevel@tonic-gate * probe-description-list
40460Sstevel@tonic-gate * /x++ && y == 0/
40470Sstevel@tonic-gate * {
40480Sstevel@tonic-gate * trace(x + y++);
40490Sstevel@tonic-gate * }
40500Sstevel@tonic-gate *
40510Sstevel@tonic-gate * but it doesn't seem worth the complexity to handle such rare cases. The
40520Sstevel@tonic-gate * user can simply use the D variable declaration syntax to work around them.
40530Sstevel@tonic-gate */
40540Sstevel@tonic-gate static dt_node_t *
dt_cook_clause(dt_node_t * dnp,uint_t idflags)40550Sstevel@tonic-gate dt_cook_clause(dt_node_t *dnp, uint_t idflags)
40560Sstevel@tonic-gate {
40570Sstevel@tonic-gate volatile int err, tries;
40580Sstevel@tonic-gate jmp_buf ojb;
40590Sstevel@tonic-gate
40600Sstevel@tonic-gate /*
40610Sstevel@tonic-gate * Before assigning dn_ctxattr, temporarily assign the probe attribute
40620Sstevel@tonic-gate * to 'dnp' itself to force an attribute check and minimum violation.
40630Sstevel@tonic-gate */
40640Sstevel@tonic-gate dt_node_attr_assign(dnp, yypcb->pcb_pinfo.dtp_attr);
40650Sstevel@tonic-gate dnp->dn_ctxattr = yypcb->pcb_pinfo.dtp_attr;
40660Sstevel@tonic-gate
40670Sstevel@tonic-gate bcopy(yypcb->pcb_jmpbuf, ojb, sizeof (jmp_buf));
40680Sstevel@tonic-gate tries = 0;
40690Sstevel@tonic-gate
40700Sstevel@tonic-gate if (dnp->dn_pred != NULL && (err = setjmp(yypcb->pcb_jmpbuf)) != 0) {
40710Sstevel@tonic-gate bcopy(ojb, yypcb->pcb_jmpbuf, sizeof (jmp_buf));
40720Sstevel@tonic-gate if (tries++ != 0 || err != EDT_COMPILER || (
40730Sstevel@tonic-gate yypcb->pcb_hdl->dt_errtag != dt_errtag(D_IDENT_UNDEF) &&
40740Sstevel@tonic-gate yypcb->pcb_hdl->dt_errtag != dt_errtag(D_VAR_UNDEF)))
40750Sstevel@tonic-gate longjmp(yypcb->pcb_jmpbuf, err);
40760Sstevel@tonic-gate }
40770Sstevel@tonic-gate
40780Sstevel@tonic-gate if (tries == 0) {
40790Sstevel@tonic-gate yylabel("action list");
40800Sstevel@tonic-gate
40810Sstevel@tonic-gate dt_node_attr_assign(dnp,
40820Sstevel@tonic-gate dt_node_list_cook(&dnp->dn_acts, idflags));
40830Sstevel@tonic-gate
40840Sstevel@tonic-gate bcopy(ojb, yypcb->pcb_jmpbuf, sizeof (jmp_buf));
40850Sstevel@tonic-gate yylabel(NULL);
40860Sstevel@tonic-gate }
40870Sstevel@tonic-gate
40880Sstevel@tonic-gate if (dnp->dn_pred != NULL) {
40890Sstevel@tonic-gate yylabel("predicate");
40900Sstevel@tonic-gate
40910Sstevel@tonic-gate dnp->dn_pred = dt_node_cook(dnp->dn_pred, idflags);
40920Sstevel@tonic-gate dt_node_attr_assign(dnp,
40930Sstevel@tonic-gate dt_attr_min(dnp->dn_attr, dnp->dn_pred->dn_attr));
40940Sstevel@tonic-gate
40950Sstevel@tonic-gate if (!dt_node_is_scalar(dnp->dn_pred)) {
40960Sstevel@tonic-gate xyerror(D_PRED_SCALAR,
40970Sstevel@tonic-gate "predicate result must be of scalar type\n");
40980Sstevel@tonic-gate }
40990Sstevel@tonic-gate
41000Sstevel@tonic-gate yylabel(NULL);
41010Sstevel@tonic-gate }
41020Sstevel@tonic-gate
41030Sstevel@tonic-gate if (tries != 0) {
41040Sstevel@tonic-gate yylabel("action list");
41050Sstevel@tonic-gate
41060Sstevel@tonic-gate dt_node_attr_assign(dnp,
41070Sstevel@tonic-gate dt_node_list_cook(&dnp->dn_acts, idflags));
41080Sstevel@tonic-gate
41090Sstevel@tonic-gate yylabel(NULL);
41100Sstevel@tonic-gate }
41110Sstevel@tonic-gate
41120Sstevel@tonic-gate return (dnp);
41130Sstevel@tonic-gate }
41140Sstevel@tonic-gate
41150Sstevel@tonic-gate /*ARGSUSED*/
41160Sstevel@tonic-gate static dt_node_t *
dt_cook_inline(dt_node_t * dnp,uint_t idflags)41170Sstevel@tonic-gate dt_cook_inline(dt_node_t *dnp, uint_t idflags)
41180Sstevel@tonic-gate {
41190Sstevel@tonic-gate dt_idnode_t *inp = dnp->dn_ident->di_iarg;
41200Sstevel@tonic-gate dt_ident_t *rdp;
41210Sstevel@tonic-gate
41220Sstevel@tonic-gate char n1[DT_TYPE_NAMELEN];
41230Sstevel@tonic-gate char n2[DT_TYPE_NAMELEN];
41240Sstevel@tonic-gate
41250Sstevel@tonic-gate assert(dnp->dn_ident->di_flags & DT_IDFLG_INLINE);
41260Sstevel@tonic-gate assert(inp->din_root->dn_flags & DT_NF_COOKED);
41270Sstevel@tonic-gate
41280Sstevel@tonic-gate /*
41290Sstevel@tonic-gate * If we are inlining a translation, verify that the inline declaration
41300Sstevel@tonic-gate * type exactly matches the type that is returned by the translation.
41310Sstevel@tonic-gate * Otherwise just use dt_node_is_argcompat() to check the types.
41320Sstevel@tonic-gate */
41330Sstevel@tonic-gate if ((rdp = dt_node_resolve(inp->din_root, DT_IDENT_XLSOU)) != NULL ||
41340Sstevel@tonic-gate (rdp = dt_node_resolve(inp->din_root, DT_IDENT_XLPTR)) != NULL) {
41350Sstevel@tonic-gate
41360Sstevel@tonic-gate ctf_file_t *lctfp = dnp->dn_ctfp;
41370Sstevel@tonic-gate ctf_id_t ltype = ctf_type_resolve(lctfp, dnp->dn_type);
41380Sstevel@tonic-gate
41390Sstevel@tonic-gate dt_xlator_t *dxp = rdp->di_data;
41400Sstevel@tonic-gate ctf_file_t *rctfp = dxp->dx_dst_ctfp;
41410Sstevel@tonic-gate ctf_id_t rtype = dxp->dx_dst_base;
41420Sstevel@tonic-gate
41430Sstevel@tonic-gate if (ctf_type_kind(lctfp, ltype) == CTF_K_POINTER) {
41440Sstevel@tonic-gate ltype = ctf_type_reference(lctfp, ltype);
41450Sstevel@tonic-gate ltype = ctf_type_resolve(lctfp, ltype);
41460Sstevel@tonic-gate }
41470Sstevel@tonic-gate
41480Sstevel@tonic-gate if (ctf_type_compat(lctfp, ltype, rctfp, rtype) == 0) {
41490Sstevel@tonic-gate dnerror(dnp, D_OP_INCOMPAT,
41500Sstevel@tonic-gate "inline %s definition uses incompatible types: "
41510Sstevel@tonic-gate "\"%s\" = \"%s\"\n", dnp->dn_ident->di_name,
41520Sstevel@tonic-gate dt_type_name(lctfp, ltype, n1, sizeof (n1)),
41530Sstevel@tonic-gate dt_type_name(rctfp, rtype, n2, sizeof (n2)));
41540Sstevel@tonic-gate }
41550Sstevel@tonic-gate
41560Sstevel@tonic-gate } else if (dt_node_is_argcompat(dnp, inp->din_root) == 0) {
41570Sstevel@tonic-gate dnerror(dnp, D_OP_INCOMPAT,
41580Sstevel@tonic-gate "inline %s definition uses incompatible types: "
41590Sstevel@tonic-gate "\"%s\" = \"%s\"\n", dnp->dn_ident->di_name,
41600Sstevel@tonic-gate dt_node_type_name(dnp, n1, sizeof (n1)),
41610Sstevel@tonic-gate dt_node_type_name(inp->din_root, n2, sizeof (n2)));
41620Sstevel@tonic-gate }
41630Sstevel@tonic-gate
41640Sstevel@tonic-gate return (dnp);
41650Sstevel@tonic-gate }
41660Sstevel@tonic-gate
41670Sstevel@tonic-gate static dt_node_t *
dt_cook_member(dt_node_t * dnp,uint_t idflags)41680Sstevel@tonic-gate dt_cook_member(dt_node_t *dnp, uint_t idflags)
41690Sstevel@tonic-gate {
41700Sstevel@tonic-gate dnp->dn_membexpr = dt_node_cook(dnp->dn_membexpr, idflags);
41710Sstevel@tonic-gate dt_node_attr_assign(dnp, dnp->dn_membexpr->dn_attr);
41720Sstevel@tonic-gate return (dnp);
41730Sstevel@tonic-gate }
41740Sstevel@tonic-gate
41750Sstevel@tonic-gate /*ARGSUSED*/
41760Sstevel@tonic-gate static dt_node_t *
dt_cook_xlator(dt_node_t * dnp,uint_t idflags)41770Sstevel@tonic-gate dt_cook_xlator(dt_node_t *dnp, uint_t idflags)
41780Sstevel@tonic-gate {
41790Sstevel@tonic-gate dtrace_hdl_t *dtp = yypcb->pcb_hdl;
41800Sstevel@tonic-gate dt_xlator_t *dxp = dnp->dn_xlator;
41810Sstevel@tonic-gate dt_node_t *mnp;
41820Sstevel@tonic-gate
41830Sstevel@tonic-gate char n1[DT_TYPE_NAMELEN];
41840Sstevel@tonic-gate char n2[DT_TYPE_NAMELEN];
41850Sstevel@tonic-gate
41860Sstevel@tonic-gate dtrace_attribute_t attr = _dtrace_maxattr;
41870Sstevel@tonic-gate ctf_membinfo_t ctm;
41880Sstevel@tonic-gate
41890Sstevel@tonic-gate /*
41900Sstevel@tonic-gate * Before cooking each translator member, we push a reference to the
41910Sstevel@tonic-gate * hash containing translator-local identifiers on to pcb_globals to
41920Sstevel@tonic-gate * temporarily interpose these identifiers in front of other globals.
41930Sstevel@tonic-gate */
41940Sstevel@tonic-gate dt_idstack_push(&yypcb->pcb_globals, dxp->dx_locals);
41950Sstevel@tonic-gate
41960Sstevel@tonic-gate for (mnp = dnp->dn_members; mnp != NULL; mnp = mnp->dn_list) {
41970Sstevel@tonic-gate if (ctf_member_info(dxp->dx_dst_ctfp, dxp->dx_dst_type,
41980Sstevel@tonic-gate mnp->dn_membname, &ctm) == CTF_ERR) {
41990Sstevel@tonic-gate xyerror(D_XLATE_MEMB,
42000Sstevel@tonic-gate "translator member %s is not a member of %s\n",
42010Sstevel@tonic-gate mnp->dn_membname, ctf_type_name(dxp->dx_dst_ctfp,
42020Sstevel@tonic-gate dxp->dx_dst_type, n1, sizeof (n1)));
42030Sstevel@tonic-gate }
42040Sstevel@tonic-gate
42050Sstevel@tonic-gate (void) dt_node_cook(mnp, DT_IDFLG_REF);
42060Sstevel@tonic-gate dt_node_type_assign(mnp, dxp->dx_dst_ctfp, ctm.ctm_type);
42070Sstevel@tonic-gate attr = dt_attr_min(attr, mnp->dn_attr);
42080Sstevel@tonic-gate
42090Sstevel@tonic-gate if (dt_node_is_argcompat(mnp, mnp->dn_membexpr) == 0) {
42100Sstevel@tonic-gate xyerror(D_XLATE_INCOMPAT,
42110Sstevel@tonic-gate "translator member %s definition uses "
42120Sstevel@tonic-gate "incompatible types: \"%s\" = \"%s\"\n",
42130Sstevel@tonic-gate mnp->dn_membname,
42140Sstevel@tonic-gate dt_node_type_name(mnp, n1, sizeof (n1)),
42150Sstevel@tonic-gate dt_node_type_name(mnp->dn_membexpr,
42160Sstevel@tonic-gate n2, sizeof (n2)));
42170Sstevel@tonic-gate }
42180Sstevel@tonic-gate }
42190Sstevel@tonic-gate
42200Sstevel@tonic-gate dt_idstack_pop(&yypcb->pcb_globals, dxp->dx_locals);
42210Sstevel@tonic-gate
42220Sstevel@tonic-gate dxp->dx_souid.di_attr = attr;
42230Sstevel@tonic-gate dxp->dx_ptrid.di_attr = attr;
42240Sstevel@tonic-gate
42250Sstevel@tonic-gate dt_node_type_assign(dnp, DT_DYN_CTFP(dtp), DT_DYN_TYPE(dtp));
42260Sstevel@tonic-gate dt_node_attr_assign(dnp, _dtrace_defattr);
42270Sstevel@tonic-gate
42280Sstevel@tonic-gate return (dnp);
42290Sstevel@tonic-gate }
42300Sstevel@tonic-gate
42310Sstevel@tonic-gate static void
dt_node_provider_cmp_argv(dt_provider_t * pvp,dt_node_t * pnp,const char * kind,uint_t old_argc,dt_node_t * old_argv,uint_t new_argc,dt_node_t * new_argv)42320Sstevel@tonic-gate dt_node_provider_cmp_argv(dt_provider_t *pvp, dt_node_t *pnp, const char *kind,
42330Sstevel@tonic-gate uint_t old_argc, dt_node_t *old_argv, uint_t new_argc, dt_node_t *new_argv)
42340Sstevel@tonic-gate {
42350Sstevel@tonic-gate dt_probe_t *prp = pnp->dn_ident->di_data;
42360Sstevel@tonic-gate uint_t i;
42370Sstevel@tonic-gate
42380Sstevel@tonic-gate char n1[DT_TYPE_NAMELEN];
42390Sstevel@tonic-gate char n2[DT_TYPE_NAMELEN];
42400Sstevel@tonic-gate
42410Sstevel@tonic-gate if (old_argc != new_argc) {
42420Sstevel@tonic-gate dnerror(pnp, D_PROV_INCOMPAT,
42430Sstevel@tonic-gate "probe %s:%s %s prototype mismatch:\n"
42440Sstevel@tonic-gate "\t current: %u arg%s\n\tprevious: %u arg%s\n",
42450Sstevel@tonic-gate pvp->pv_desc.dtvd_name, prp->pr_ident->di_name, kind,
42460Sstevel@tonic-gate new_argc, new_argc != 1 ? "s" : "",
42470Sstevel@tonic-gate old_argc, old_argc != 1 ? "s" : "");
42480Sstevel@tonic-gate }
42490Sstevel@tonic-gate
42500Sstevel@tonic-gate for (i = 0; i < old_argc; i++,
42510Sstevel@tonic-gate old_argv = old_argv->dn_list, new_argv = new_argv->dn_list) {
42520Sstevel@tonic-gate if (ctf_type_cmp(old_argv->dn_ctfp, old_argv->dn_type,
42530Sstevel@tonic-gate new_argv->dn_ctfp, new_argv->dn_type) == 0)
42540Sstevel@tonic-gate continue;
42550Sstevel@tonic-gate
42560Sstevel@tonic-gate dnerror(pnp, D_PROV_INCOMPAT,
42570Sstevel@tonic-gate "probe %s:%s %s prototype argument #%u mismatch:\n"
42580Sstevel@tonic-gate "\t current: %s\n\tprevious: %s\n",
42590Sstevel@tonic-gate pvp->pv_desc.dtvd_name, prp->pr_ident->di_name, kind, i + 1,
42600Sstevel@tonic-gate dt_node_type_name(new_argv, n1, sizeof (n1)),
42610Sstevel@tonic-gate dt_node_type_name(old_argv, n2, sizeof (n2)));
42620Sstevel@tonic-gate }
42630Sstevel@tonic-gate }
42640Sstevel@tonic-gate
42650Sstevel@tonic-gate /*
42660Sstevel@tonic-gate * Compare a new probe declaration with an existing probe definition (either
4267265Smws * from a previous declaration or cached from the kernel). If the existing
4268265Smws * definition and declaration both have an input and output parameter list,
4269265Smws * compare both lists. Otherwise compare only the output parameter lists.
42700Sstevel@tonic-gate */
42710Sstevel@tonic-gate static void
dt_node_provider_cmp(dt_provider_t * pvp,dt_node_t * pnp,dt_probe_t * old,dt_probe_t * new)42720Sstevel@tonic-gate dt_node_provider_cmp(dt_provider_t *pvp, dt_node_t *pnp,
42730Sstevel@tonic-gate dt_probe_t *old, dt_probe_t *new)
42740Sstevel@tonic-gate {
42750Sstevel@tonic-gate dt_node_provider_cmp_argv(pvp, pnp, "output",
42760Sstevel@tonic-gate old->pr_xargc, old->pr_xargs, new->pr_xargc, new->pr_xargs);
42770Sstevel@tonic-gate
4278265Smws if (old->pr_nargs != old->pr_xargs && new->pr_nargs != new->pr_xargs) {
42790Sstevel@tonic-gate dt_node_provider_cmp_argv(pvp, pnp, "input",
42800Sstevel@tonic-gate old->pr_nargc, old->pr_nargs, new->pr_nargc, new->pr_nargs);
42810Sstevel@tonic-gate }
4282265Smws
4283265Smws if (old->pr_nargs == old->pr_xargs && new->pr_nargs != new->pr_xargs) {
4284265Smws if (pvp->pv_flags & DT_PROVIDER_IMPL) {
4285265Smws dnerror(pnp, D_PROV_INCOMPAT,
4286265Smws "provider interface mismatch: %s\n"
4287265Smws "\t current: probe %s:%s has an output prototype\n"
4288265Smws "\tprevious: probe %s:%s has no output prototype\n",
4289265Smws pvp->pv_desc.dtvd_name, pvp->pv_desc.dtvd_name,
4290265Smws new->pr_ident->di_name, pvp->pv_desc.dtvd_name,
4291265Smws old->pr_ident->di_name);
4292265Smws }
4293265Smws
4294265Smws if (old->pr_ident->di_gen == yypcb->pcb_hdl->dt_gen)
4295265Smws old->pr_ident->di_flags |= DT_IDFLG_ORPHAN;
4296265Smws
4297265Smws dt_idhash_delete(pvp->pv_probes, old->pr_ident);
4298265Smws dt_probe_declare(pvp, new);
4299265Smws }
4300265Smws }
4301265Smws
4302265Smws static void
dt_cook_probe(dt_node_t * dnp,dt_provider_t * pvp)4303265Smws dt_cook_probe(dt_node_t *dnp, dt_provider_t *pvp)
4304265Smws {
4305265Smws dtrace_hdl_t *dtp = yypcb->pcb_hdl;
4306265Smws dt_probe_t *prp = dnp->dn_ident->di_data;
4307265Smws
4308265Smws dt_xlator_t *dxp;
4309265Smws uint_t i;
4310265Smws
4311265Smws char n1[DT_TYPE_NAMELEN];
4312265Smws char n2[DT_TYPE_NAMELEN];
4313265Smws
4314265Smws if (prp->pr_nargs == prp->pr_xargs)
4315265Smws return;
4316265Smws
4317265Smws for (i = 0; i < prp->pr_xargc; i++) {
4318265Smws dt_node_t *xnp = prp->pr_xargv[i];
4319265Smws dt_node_t *nnp = prp->pr_nargv[prp->pr_mapping[i]];
4320265Smws
4321265Smws if ((dxp = dt_xlator_lookup(dtp,
4322265Smws nnp, xnp, DT_XLATE_FUZZY)) != NULL) {
4323265Smws if (dt_provider_xref(dtp, pvp, dxp->dx_id) != 0)
4324265Smws longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
4325265Smws continue;
4326265Smws }
4327265Smws
4328265Smws if (dt_node_is_argcompat(nnp, xnp))
4329265Smws continue; /* no translator defined and none required */
4330265Smws
4331265Smws dnerror(dnp, D_PROV_PRXLATOR, "translator for %s:%s output "
4332265Smws "argument #%u from %s to %s is not defined\n",
4333265Smws pvp->pv_desc.dtvd_name, dnp->dn_ident->di_name, i + 1,
4334265Smws dt_node_type_name(nnp, n1, sizeof (n1)),
4335265Smws dt_node_type_name(xnp, n2, sizeof (n2)));
4336265Smws }
43370Sstevel@tonic-gate }
43380Sstevel@tonic-gate
43390Sstevel@tonic-gate /*ARGSUSED*/
43400Sstevel@tonic-gate static dt_node_t *
dt_cook_provider(dt_node_t * dnp,uint_t idflags)43410Sstevel@tonic-gate dt_cook_provider(dt_node_t *dnp, uint_t idflags)
43420Sstevel@tonic-gate {
43430Sstevel@tonic-gate dt_provider_t *pvp = dnp->dn_provider;
43440Sstevel@tonic-gate dt_node_t *pnp;
43450Sstevel@tonic-gate
43460Sstevel@tonic-gate /*
43470Sstevel@tonic-gate * If we're declaring a provider for the first time and it is unknown
43480Sstevel@tonic-gate * to dtrace(7D), insert the probe definitions into the provider's hash.
43490Sstevel@tonic-gate * If we're redeclaring a known provider, verify the interface matches.
43500Sstevel@tonic-gate */
43510Sstevel@tonic-gate for (pnp = dnp->dn_probes; pnp != NULL; pnp = pnp->dn_list) {
43520Sstevel@tonic-gate const char *probename = pnp->dn_ident->di_name;
43530Sstevel@tonic-gate dt_probe_t *prp = dt_probe_lookup(pvp, probename);
43540Sstevel@tonic-gate
43550Sstevel@tonic-gate assert(pnp->dn_kind == DT_NODE_PROBE);
43560Sstevel@tonic-gate
43570Sstevel@tonic-gate if (prp != NULL && dnp->dn_provred) {
43580Sstevel@tonic-gate dt_node_provider_cmp(pvp, pnp,
43590Sstevel@tonic-gate prp, pnp->dn_ident->di_data);
43600Sstevel@tonic-gate } else if (prp == NULL && dnp->dn_provred) {
43610Sstevel@tonic-gate dnerror(pnp, D_PROV_INCOMPAT,
43620Sstevel@tonic-gate "provider interface mismatch: %s\n"
43630Sstevel@tonic-gate "\t current: probe %s:%s defined\n"
43640Sstevel@tonic-gate "\tprevious: probe %s:%s not defined\n",
43650Sstevel@tonic-gate dnp->dn_provname, dnp->dn_provname,
43660Sstevel@tonic-gate probename, dnp->dn_provname, probename);
43670Sstevel@tonic-gate } else if (prp != NULL) {
43680Sstevel@tonic-gate dnerror(pnp, D_PROV_PRDUP, "probe redeclared: %s:%s\n",
43690Sstevel@tonic-gate dnp->dn_provname, probename);
43700Sstevel@tonic-gate } else
43710Sstevel@tonic-gate dt_probe_declare(pvp, pnp->dn_ident->di_data);
4372265Smws
4373265Smws dt_cook_probe(pnp, pvp);
43740Sstevel@tonic-gate }
43750Sstevel@tonic-gate
43760Sstevel@tonic-gate return (dnp);
43770Sstevel@tonic-gate }
43780Sstevel@tonic-gate
43790Sstevel@tonic-gate /*ARGSUSED*/
43800Sstevel@tonic-gate static dt_node_t *
dt_cook_none(dt_node_t * dnp,uint_t idflags)43810Sstevel@tonic-gate dt_cook_none(dt_node_t *dnp, uint_t idflags)
43820Sstevel@tonic-gate {
43830Sstevel@tonic-gate return (dnp);
43840Sstevel@tonic-gate }
43850Sstevel@tonic-gate
43860Sstevel@tonic-gate static dt_node_t *(*dt_cook_funcs[])(dt_node_t *, uint_t) = {
43870Sstevel@tonic-gate dt_cook_none, /* DT_NODE_FREE */
43880Sstevel@tonic-gate dt_cook_none, /* DT_NODE_INT */
43890Sstevel@tonic-gate dt_cook_none, /* DT_NODE_STRING */
43900Sstevel@tonic-gate dt_cook_ident, /* DT_NODE_IDENT */
43910Sstevel@tonic-gate dt_cook_var, /* DT_NODE_VAR */
43920Sstevel@tonic-gate dt_cook_none, /* DT_NODE_SYM */
43930Sstevel@tonic-gate dt_cook_none, /* DT_NODE_TYPE */
43940Sstevel@tonic-gate dt_cook_func, /* DT_NODE_FUNC */
43950Sstevel@tonic-gate dt_cook_op1, /* DT_NODE_OP1 */
43960Sstevel@tonic-gate dt_cook_op2, /* DT_NODE_OP2 */
43970Sstevel@tonic-gate dt_cook_op3, /* DT_NODE_OP3 */
43980Sstevel@tonic-gate dt_cook_statement, /* DT_NODE_DEXPR */
43990Sstevel@tonic-gate dt_cook_statement, /* DT_NODE_DFUNC */
44000Sstevel@tonic-gate dt_cook_aggregation, /* DT_NODE_AGG */
44010Sstevel@tonic-gate dt_cook_none, /* DT_NODE_PDESC */
44020Sstevel@tonic-gate dt_cook_clause, /* DT_NODE_CLAUSE */
44030Sstevel@tonic-gate dt_cook_inline, /* DT_NODE_INLINE */
44040Sstevel@tonic-gate dt_cook_member, /* DT_NODE_MEMBER */
44050Sstevel@tonic-gate dt_cook_xlator, /* DT_NODE_XLATOR */
44060Sstevel@tonic-gate dt_cook_none, /* DT_NODE_PROBE */
44070Sstevel@tonic-gate dt_cook_provider, /* DT_NODE_PROVIDER */
44080Sstevel@tonic-gate dt_cook_none /* DT_NODE_PROG */
44090Sstevel@tonic-gate };
44100Sstevel@tonic-gate
44110Sstevel@tonic-gate /*
44120Sstevel@tonic-gate * Recursively cook the parse tree starting at the specified node. The idflags
44130Sstevel@tonic-gate * parameter is used to indicate the type of reference (r/w) and is applied to
44140Sstevel@tonic-gate * the resulting identifier if it is a D variable or D aggregation.
44150Sstevel@tonic-gate */
44160Sstevel@tonic-gate dt_node_t *
dt_node_cook(dt_node_t * dnp,uint_t idflags)44170Sstevel@tonic-gate dt_node_cook(dt_node_t *dnp, uint_t idflags)
44180Sstevel@tonic-gate {
44190Sstevel@tonic-gate int oldlineno = yylineno;
44200Sstevel@tonic-gate
44210Sstevel@tonic-gate yylineno = dnp->dn_line;
44220Sstevel@tonic-gate
44230Sstevel@tonic-gate dnp = dt_cook_funcs[dnp->dn_kind](dnp, idflags);
44240Sstevel@tonic-gate dnp->dn_flags |= DT_NF_COOKED;
44250Sstevel@tonic-gate
44260Sstevel@tonic-gate if (dnp->dn_kind == DT_NODE_VAR || dnp->dn_kind == DT_NODE_AGG)
44270Sstevel@tonic-gate dnp->dn_ident->di_flags |= idflags;
44280Sstevel@tonic-gate
44290Sstevel@tonic-gate yylineno = oldlineno;
44300Sstevel@tonic-gate return (dnp);
44310Sstevel@tonic-gate }
44320Sstevel@tonic-gate
44330Sstevel@tonic-gate dtrace_attribute_t
dt_node_list_cook(dt_node_t ** pnp,uint_t idflags)44340Sstevel@tonic-gate dt_node_list_cook(dt_node_t **pnp, uint_t idflags)
44350Sstevel@tonic-gate {
44360Sstevel@tonic-gate dtrace_attribute_t attr = _dtrace_defattr;
44370Sstevel@tonic-gate dt_node_t *dnp, *nnp;
44380Sstevel@tonic-gate
44390Sstevel@tonic-gate for (dnp = (pnp != NULL ? *pnp : NULL); dnp != NULL; dnp = nnp) {
44400Sstevel@tonic-gate nnp = dnp->dn_list;
44410Sstevel@tonic-gate dnp = *pnp = dt_node_cook(dnp, idflags);
44420Sstevel@tonic-gate attr = dt_attr_min(attr, dnp->dn_attr);
44430Sstevel@tonic-gate dnp->dn_list = nnp;
44440Sstevel@tonic-gate pnp = &dnp->dn_list;
44450Sstevel@tonic-gate }
44460Sstevel@tonic-gate
44470Sstevel@tonic-gate return (attr);
44480Sstevel@tonic-gate }
44490Sstevel@tonic-gate
44500Sstevel@tonic-gate void
dt_node_list_free(dt_node_t ** pnp)44510Sstevel@tonic-gate dt_node_list_free(dt_node_t **pnp)
44520Sstevel@tonic-gate {
44530Sstevel@tonic-gate dt_node_t *dnp, *nnp;
44540Sstevel@tonic-gate
44550Sstevel@tonic-gate for (dnp = (pnp != NULL ? *pnp : NULL); dnp != NULL; dnp = nnp) {
44560Sstevel@tonic-gate nnp = dnp->dn_list;
44570Sstevel@tonic-gate dt_node_free(dnp);
44580Sstevel@tonic-gate }
44590Sstevel@tonic-gate
44600Sstevel@tonic-gate if (pnp != NULL)
44610Sstevel@tonic-gate *pnp = NULL;
44620Sstevel@tonic-gate }
44630Sstevel@tonic-gate
44640Sstevel@tonic-gate void
dt_node_link_free(dt_node_t ** pnp)44650Sstevel@tonic-gate dt_node_link_free(dt_node_t **pnp)
44660Sstevel@tonic-gate {
44670Sstevel@tonic-gate dt_node_t *dnp, *nnp;
44680Sstevel@tonic-gate
44690Sstevel@tonic-gate for (dnp = (pnp != NULL ? *pnp : NULL); dnp != NULL; dnp = nnp) {
44700Sstevel@tonic-gate nnp = dnp->dn_link;
44710Sstevel@tonic-gate dt_node_free(dnp);
44720Sstevel@tonic-gate }
44730Sstevel@tonic-gate
44740Sstevel@tonic-gate for (dnp = (pnp != NULL ? *pnp : NULL); dnp != NULL; dnp = nnp) {
44750Sstevel@tonic-gate nnp = dnp->dn_link;
44760Sstevel@tonic-gate free(dnp);
44770Sstevel@tonic-gate }
44780Sstevel@tonic-gate
44790Sstevel@tonic-gate if (pnp != NULL)
44800Sstevel@tonic-gate *pnp = NULL;
44810Sstevel@tonic-gate }
44820Sstevel@tonic-gate
44830Sstevel@tonic-gate dt_node_t *
dt_node_link(dt_node_t * lp,dt_node_t * rp)44840Sstevel@tonic-gate dt_node_link(dt_node_t *lp, dt_node_t *rp)
44850Sstevel@tonic-gate {
44860Sstevel@tonic-gate dt_node_t *dnp;
44870Sstevel@tonic-gate
44880Sstevel@tonic-gate if (lp == NULL)
44890Sstevel@tonic-gate return (rp);
44900Sstevel@tonic-gate else if (rp == NULL)
44910Sstevel@tonic-gate return (lp);
44920Sstevel@tonic-gate
44930Sstevel@tonic-gate for (dnp = lp; dnp->dn_list != NULL; dnp = dnp->dn_list)
44940Sstevel@tonic-gate continue;
44950Sstevel@tonic-gate
44960Sstevel@tonic-gate dnp->dn_list = rp;
44970Sstevel@tonic-gate return (lp);
44980Sstevel@tonic-gate }
44990Sstevel@tonic-gate
4500265Smws /*
4501265Smws * Compute the DOF dtrace_diftype_t representation of a node's type. This is
4502265Smws * called from a variety of places in the library so it cannot assume yypcb
4503265Smws * is valid: any references to handle-specific data must be made through 'dtp'.
4504265Smws */
45050Sstevel@tonic-gate void
dt_node_diftype(dtrace_hdl_t * dtp,const dt_node_t * dnp,dtrace_diftype_t * tp)4506265Smws dt_node_diftype(dtrace_hdl_t *dtp, const dt_node_t *dnp, dtrace_diftype_t *tp)
45070Sstevel@tonic-gate {
4508265Smws if (dnp->dn_ctfp == DT_STR_CTFP(dtp) &&
4509265Smws dnp->dn_type == DT_STR_TYPE(dtp)) {
45100Sstevel@tonic-gate tp->dtdt_kind = DIF_TYPE_STRING;
45110Sstevel@tonic-gate tp->dtdt_ckind = CTF_K_UNKNOWN;
45120Sstevel@tonic-gate } else {
45130Sstevel@tonic-gate tp->dtdt_kind = DIF_TYPE_CTF;
45140Sstevel@tonic-gate tp->dtdt_ckind = ctf_type_kind(dnp->dn_ctfp,
45150Sstevel@tonic-gate ctf_type_resolve(dnp->dn_ctfp, dnp->dn_type));
45160Sstevel@tonic-gate }
45170Sstevel@tonic-gate
45180Sstevel@tonic-gate tp->dtdt_flags = (dnp->dn_flags & DT_NF_REF) ? DIF_TF_BYREF : 0;
45190Sstevel@tonic-gate tp->dtdt_pad = 0;
45200Sstevel@tonic-gate tp->dtdt_size = ctf_type_size(dnp->dn_ctfp, dnp->dn_type);
45210Sstevel@tonic-gate }
45220Sstevel@tonic-gate
45230Sstevel@tonic-gate void
dt_node_printr(dt_node_t * dnp,FILE * fp,int depth)45240Sstevel@tonic-gate dt_node_printr(dt_node_t *dnp, FILE *fp, int depth)
45250Sstevel@tonic-gate {
45260Sstevel@tonic-gate char n[DT_TYPE_NAMELEN], buf[BUFSIZ], a[8];
45270Sstevel@tonic-gate const dtrace_syminfo_t *dts;
45280Sstevel@tonic-gate const dt_idnode_t *inp;
45290Sstevel@tonic-gate dt_node_t *arg;
45300Sstevel@tonic-gate
45310Sstevel@tonic-gate (void) fprintf(fp, "%*s", depth * 2, "");
45320Sstevel@tonic-gate (void) dt_attr_str(dnp->dn_attr, a, sizeof (a));
45330Sstevel@tonic-gate
45340Sstevel@tonic-gate if (dnp->dn_ctfp != NULL && dnp->dn_type != CTF_ERR &&
45350Sstevel@tonic-gate ctf_type_name(dnp->dn_ctfp, dnp->dn_type, n, sizeof (n)) != NULL) {
45360Sstevel@tonic-gate (void) snprintf(buf, BUFSIZ, "type=<%s> attr=%s flags=", n, a);
45370Sstevel@tonic-gate } else {
45380Sstevel@tonic-gate (void) snprintf(buf, BUFSIZ, "type=<%ld> attr=%s flags=",
45390Sstevel@tonic-gate dnp->dn_type, a);
45400Sstevel@tonic-gate }
45410Sstevel@tonic-gate
45420Sstevel@tonic-gate if (dnp->dn_flags != 0) {
45430Sstevel@tonic-gate n[0] = '\0';
45440Sstevel@tonic-gate if (dnp->dn_flags & DT_NF_SIGNED)
45450Sstevel@tonic-gate (void) strcat(n, ",SIGN");
45460Sstevel@tonic-gate if (dnp->dn_flags & DT_NF_COOKED)
45470Sstevel@tonic-gate (void) strcat(n, ",COOK");
45480Sstevel@tonic-gate if (dnp->dn_flags & DT_NF_REF)
45490Sstevel@tonic-gate (void) strcat(n, ",REF");
45500Sstevel@tonic-gate if (dnp->dn_flags & DT_NF_LVALUE)
45510Sstevel@tonic-gate (void) strcat(n, ",LVAL");
45520Sstevel@tonic-gate if (dnp->dn_flags & DT_NF_WRITABLE)
45530Sstevel@tonic-gate (void) strcat(n, ",WRITE");
45540Sstevel@tonic-gate if (dnp->dn_flags & DT_NF_BITFIELD)
45550Sstevel@tonic-gate (void) strcat(n, ",BITF");
45560Sstevel@tonic-gate if (dnp->dn_flags & DT_NF_USERLAND)
45570Sstevel@tonic-gate (void) strcat(n, ",USER");
45580Sstevel@tonic-gate (void) strcat(buf, n + 1);
45590Sstevel@tonic-gate } else
45600Sstevel@tonic-gate (void) strcat(buf, "0");
45610Sstevel@tonic-gate
45620Sstevel@tonic-gate switch (dnp->dn_kind) {
45630Sstevel@tonic-gate case DT_NODE_FREE:
45640Sstevel@tonic-gate (void) fprintf(fp, "FREE <node %p>\n", (void *)dnp);
45650Sstevel@tonic-gate break;
45660Sstevel@tonic-gate
45670Sstevel@tonic-gate case DT_NODE_INT:
45680Sstevel@tonic-gate (void) fprintf(fp, "INT 0x%llx (%s)\n",
45690Sstevel@tonic-gate (u_longlong_t)dnp->dn_value, buf);
45700Sstevel@tonic-gate break;
45710Sstevel@tonic-gate
45720Sstevel@tonic-gate case DT_NODE_STRING:
45730Sstevel@tonic-gate (void) fprintf(fp, "STRING \"%s\" (%s)\n", dnp->dn_string, buf);
45740Sstevel@tonic-gate break;
45750Sstevel@tonic-gate
45760Sstevel@tonic-gate case DT_NODE_IDENT:
45770Sstevel@tonic-gate (void) fprintf(fp, "IDENT %s (%s)\n", dnp->dn_string, buf);
45780Sstevel@tonic-gate break;
45790Sstevel@tonic-gate
45800Sstevel@tonic-gate case DT_NODE_VAR:
45810Sstevel@tonic-gate (void) fprintf(fp, "VARIABLE %s%s (%s)\n",
45820Sstevel@tonic-gate (dnp->dn_ident->di_flags & DT_IDFLG_LOCAL) ? "this->" :
45830Sstevel@tonic-gate (dnp->dn_ident->di_flags & DT_IDFLG_TLS) ? "self->" : "",
45840Sstevel@tonic-gate dnp->dn_ident->di_name, buf);
45850Sstevel@tonic-gate
45860Sstevel@tonic-gate if (dnp->dn_args != NULL)
45870Sstevel@tonic-gate (void) fprintf(fp, "%*s[\n", depth * 2, "");
45880Sstevel@tonic-gate
45890Sstevel@tonic-gate for (arg = dnp->dn_args; arg != NULL; arg = arg->dn_list) {
45900Sstevel@tonic-gate dt_node_printr(arg, fp, depth + 1);
45910Sstevel@tonic-gate if (arg->dn_list != NULL)
45920Sstevel@tonic-gate (void) fprintf(fp, "%*s,\n", depth * 2, "");
45930Sstevel@tonic-gate }
45940Sstevel@tonic-gate
45950Sstevel@tonic-gate if (dnp->dn_args != NULL)
45960Sstevel@tonic-gate (void) fprintf(fp, "%*s]\n", depth * 2, "");
45970Sstevel@tonic-gate break;
45980Sstevel@tonic-gate
45990Sstevel@tonic-gate case DT_NODE_SYM:
46000Sstevel@tonic-gate dts = dnp->dn_ident->di_data;
46010Sstevel@tonic-gate (void) fprintf(fp, "SYMBOL %s`%s (%s)\n",
46020Sstevel@tonic-gate dts->dts_object, dts->dts_name, buf);
46030Sstevel@tonic-gate break;
46040Sstevel@tonic-gate
46050Sstevel@tonic-gate case DT_NODE_TYPE:
46060Sstevel@tonic-gate if (dnp->dn_string != NULL) {
46070Sstevel@tonic-gate (void) fprintf(fp, "TYPE (%s) %s\n",
46080Sstevel@tonic-gate buf, dnp->dn_string);
46090Sstevel@tonic-gate } else
46100Sstevel@tonic-gate (void) fprintf(fp, "TYPE (%s)\n", buf);
46110Sstevel@tonic-gate break;
46120Sstevel@tonic-gate
46130Sstevel@tonic-gate case DT_NODE_FUNC:
46140Sstevel@tonic-gate (void) fprintf(fp, "FUNC %s (%s)\n",
46150Sstevel@tonic-gate dnp->dn_ident->di_name, buf);
46160Sstevel@tonic-gate
46170Sstevel@tonic-gate for (arg = dnp->dn_args; arg != NULL; arg = arg->dn_list) {
46180Sstevel@tonic-gate dt_node_printr(arg, fp, depth + 1);
46190Sstevel@tonic-gate if (arg->dn_list != NULL)
46200Sstevel@tonic-gate (void) fprintf(fp, "%*s,\n", depth * 2, "");
46210Sstevel@tonic-gate }
46220Sstevel@tonic-gate break;
46230Sstevel@tonic-gate
46240Sstevel@tonic-gate case DT_NODE_OP1:
46250Sstevel@tonic-gate (void) fprintf(fp, "OP1 %s (%s)\n", opstr(dnp->dn_op), buf);
46260Sstevel@tonic-gate dt_node_printr(dnp->dn_child, fp, depth + 1);
46270Sstevel@tonic-gate break;
46280Sstevel@tonic-gate
46290Sstevel@tonic-gate case DT_NODE_OP2:
46300Sstevel@tonic-gate (void) fprintf(fp, "OP2 %s (%s)\n", opstr(dnp->dn_op), buf);
46310Sstevel@tonic-gate dt_node_printr(dnp->dn_left, fp, depth + 1);
46320Sstevel@tonic-gate dt_node_printr(dnp->dn_right, fp, depth + 1);
46330Sstevel@tonic-gate break;
46340Sstevel@tonic-gate
46350Sstevel@tonic-gate case DT_NODE_OP3:
46360Sstevel@tonic-gate (void) fprintf(fp, "OP3 (%s)\n", buf);
46370Sstevel@tonic-gate dt_node_printr(dnp->dn_expr, fp, depth + 1);
46380Sstevel@tonic-gate (void) fprintf(fp, "%*s?\n", depth * 2, "");
46390Sstevel@tonic-gate dt_node_printr(dnp->dn_left, fp, depth + 1);
46400Sstevel@tonic-gate (void) fprintf(fp, "%*s:\n", depth * 2, "");
46410Sstevel@tonic-gate dt_node_printr(dnp->dn_right, fp, depth + 1);
46420Sstevel@tonic-gate break;
46430Sstevel@tonic-gate
46440Sstevel@tonic-gate case DT_NODE_DEXPR:
46450Sstevel@tonic-gate case DT_NODE_DFUNC:
46460Sstevel@tonic-gate (void) fprintf(fp, "D EXPRESSION attr=%s\n", a);
46470Sstevel@tonic-gate dt_node_printr(dnp->dn_expr, fp, depth + 1);
46480Sstevel@tonic-gate break;
46490Sstevel@tonic-gate
46500Sstevel@tonic-gate case DT_NODE_AGG:
46510Sstevel@tonic-gate (void) fprintf(fp, "AGGREGATE @%s attr=%s [\n",
46520Sstevel@tonic-gate dnp->dn_ident->di_name, a);
46530Sstevel@tonic-gate
46540Sstevel@tonic-gate for (arg = dnp->dn_aggtup; arg != NULL; arg = arg->dn_list) {
46550Sstevel@tonic-gate dt_node_printr(arg, fp, depth + 1);
46560Sstevel@tonic-gate if (arg->dn_list != NULL)
46570Sstevel@tonic-gate (void) fprintf(fp, "%*s,\n", depth * 2, "");
46580Sstevel@tonic-gate }
46590Sstevel@tonic-gate
46600Sstevel@tonic-gate if (dnp->dn_aggfun) {
46610Sstevel@tonic-gate (void) fprintf(fp, "%*s] = ", depth * 2, "");
46620Sstevel@tonic-gate dt_node_printr(dnp->dn_aggfun, fp, depth + 1);
46630Sstevel@tonic-gate } else
46640Sstevel@tonic-gate (void) fprintf(fp, "%*s]\n", depth * 2, "");
46650Sstevel@tonic-gate
46660Sstevel@tonic-gate if (dnp->dn_aggfun)
46670Sstevel@tonic-gate (void) fprintf(fp, "%*s)\n", depth * 2, "");
46680Sstevel@tonic-gate break;
46690Sstevel@tonic-gate
46700Sstevel@tonic-gate case DT_NODE_PDESC:
46710Sstevel@tonic-gate (void) fprintf(fp, "PDESC %s:%s:%s:%s [%u]\n",
46720Sstevel@tonic-gate dnp->dn_desc->dtpd_provider, dnp->dn_desc->dtpd_mod,
46730Sstevel@tonic-gate dnp->dn_desc->dtpd_func, dnp->dn_desc->dtpd_name,
46740Sstevel@tonic-gate dnp->dn_desc->dtpd_id);
46750Sstevel@tonic-gate break;
46760Sstevel@tonic-gate
46770Sstevel@tonic-gate case DT_NODE_CLAUSE:
46780Sstevel@tonic-gate (void) fprintf(fp, "CLAUSE attr=%s\n", a);
46790Sstevel@tonic-gate
46800Sstevel@tonic-gate for (arg = dnp->dn_pdescs; arg != NULL; arg = arg->dn_list)
46810Sstevel@tonic-gate dt_node_printr(arg, fp, depth + 1);
46820Sstevel@tonic-gate
46830Sstevel@tonic-gate (void) fprintf(fp, "%*sCTXATTR %s\n", depth * 2, "",
46840Sstevel@tonic-gate dt_attr_str(dnp->dn_ctxattr, a, sizeof (a)));
46850Sstevel@tonic-gate
46860Sstevel@tonic-gate if (dnp->dn_pred != NULL) {
46870Sstevel@tonic-gate (void) fprintf(fp, "%*sPREDICATE /\n", depth * 2, "");
46880Sstevel@tonic-gate dt_node_printr(dnp->dn_pred, fp, depth + 1);
46890Sstevel@tonic-gate (void) fprintf(fp, "%*s/\n", depth * 2, "");
46900Sstevel@tonic-gate }
46910Sstevel@tonic-gate
46920Sstevel@tonic-gate for (arg = dnp->dn_acts; arg != NULL; arg = arg->dn_list)
46930Sstevel@tonic-gate dt_node_printr(arg, fp, depth + 1);
46940Sstevel@tonic-gate break;
46950Sstevel@tonic-gate
46960Sstevel@tonic-gate case DT_NODE_INLINE:
46970Sstevel@tonic-gate inp = dnp->dn_ident->di_iarg;
46980Sstevel@tonic-gate
46990Sstevel@tonic-gate (void) fprintf(fp, "INLINE %s (%s)\n",
47000Sstevel@tonic-gate dnp->dn_ident->di_name, buf);
47010Sstevel@tonic-gate dt_node_printr(inp->din_root, fp, depth + 1);
47020Sstevel@tonic-gate break;
47030Sstevel@tonic-gate
47040Sstevel@tonic-gate case DT_NODE_MEMBER:
47050Sstevel@tonic-gate (void) fprintf(fp, "MEMBER %s (%s)\n", dnp->dn_membname, buf);
47060Sstevel@tonic-gate if (dnp->dn_membexpr)
47070Sstevel@tonic-gate dt_node_printr(dnp->dn_membexpr, fp, depth + 1);
47080Sstevel@tonic-gate break;
47090Sstevel@tonic-gate
47100Sstevel@tonic-gate case DT_NODE_XLATOR:
47110Sstevel@tonic-gate (void) fprintf(fp, "XLATOR (%s)", buf);
47120Sstevel@tonic-gate
47130Sstevel@tonic-gate if (ctf_type_name(dnp->dn_xlator->dx_src_ctfp,
47140Sstevel@tonic-gate dnp->dn_xlator->dx_src_type, n, sizeof (n)) != NULL)
47150Sstevel@tonic-gate (void) fprintf(fp, " from <%s>", n);
47160Sstevel@tonic-gate
47170Sstevel@tonic-gate if (ctf_type_name(dnp->dn_xlator->dx_dst_ctfp,
47180Sstevel@tonic-gate dnp->dn_xlator->dx_dst_type, n, sizeof (n)) != NULL)
47190Sstevel@tonic-gate (void) fprintf(fp, " to <%s>", n);
47200Sstevel@tonic-gate
47210Sstevel@tonic-gate (void) fprintf(fp, "\n");
47220Sstevel@tonic-gate
47230Sstevel@tonic-gate for (arg = dnp->dn_members; arg != NULL; arg = arg->dn_list)
47240Sstevel@tonic-gate dt_node_printr(arg, fp, depth + 1);
47250Sstevel@tonic-gate break;
47260Sstevel@tonic-gate
47270Sstevel@tonic-gate case DT_NODE_PROBE:
47280Sstevel@tonic-gate (void) fprintf(fp, "PROBE %s\n", dnp->dn_ident->di_name);
47290Sstevel@tonic-gate break;
47300Sstevel@tonic-gate
47310Sstevel@tonic-gate case DT_NODE_PROVIDER:
47320Sstevel@tonic-gate (void) fprintf(fp, "PROVIDER %s (%s)\n",
47330Sstevel@tonic-gate dnp->dn_provname, dnp->dn_provred ? "redecl" : "decl");
47340Sstevel@tonic-gate for (arg = dnp->dn_probes; arg != NULL; arg = arg->dn_list)
47350Sstevel@tonic-gate dt_node_printr(arg, fp, depth + 1);
47360Sstevel@tonic-gate break;
47370Sstevel@tonic-gate
47380Sstevel@tonic-gate case DT_NODE_PROG:
47390Sstevel@tonic-gate (void) fprintf(fp, "PROGRAM attr=%s\n", a);
47400Sstevel@tonic-gate for (arg = dnp->dn_list; arg != NULL; arg = arg->dn_list)
47410Sstevel@tonic-gate dt_node_printr(arg, fp, depth + 1);
47420Sstevel@tonic-gate break;
47430Sstevel@tonic-gate
47440Sstevel@tonic-gate default:
47450Sstevel@tonic-gate (void) fprintf(fp, "<bad node %p, kind %d>\n",
47460Sstevel@tonic-gate (void *)dnp, dnp->dn_kind);
47470Sstevel@tonic-gate }
47480Sstevel@tonic-gate }
47490Sstevel@tonic-gate
47500Sstevel@tonic-gate int
dt_node_root(dt_node_t * dnp)47510Sstevel@tonic-gate dt_node_root(dt_node_t *dnp)
47520Sstevel@tonic-gate {
47530Sstevel@tonic-gate yypcb->pcb_root = dnp;
47540Sstevel@tonic-gate return (0);
47550Sstevel@tonic-gate }
47560Sstevel@tonic-gate
47570Sstevel@tonic-gate /*PRINTFLIKE3*/
47580Sstevel@tonic-gate void
dnerror(const dt_node_t * dnp,dt_errtag_t tag,const char * format,...)47590Sstevel@tonic-gate dnerror(const dt_node_t *dnp, dt_errtag_t tag, const char *format, ...)
47600Sstevel@tonic-gate {
47610Sstevel@tonic-gate int oldlineno = yylineno;
47620Sstevel@tonic-gate va_list ap;
47630Sstevel@tonic-gate
47640Sstevel@tonic-gate yylineno = dnp->dn_line;
47650Sstevel@tonic-gate
47660Sstevel@tonic-gate va_start(ap, format);
47670Sstevel@tonic-gate xyvwarn(tag, format, ap);
47680Sstevel@tonic-gate va_end(ap);
47690Sstevel@tonic-gate
47700Sstevel@tonic-gate yylineno = oldlineno;
47710Sstevel@tonic-gate longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
47720Sstevel@tonic-gate }
47730Sstevel@tonic-gate
47740Sstevel@tonic-gate /*PRINTFLIKE3*/
47750Sstevel@tonic-gate void
dnwarn(const dt_node_t * dnp,dt_errtag_t tag,const char * format,...)47760Sstevel@tonic-gate dnwarn(const dt_node_t *dnp, dt_errtag_t tag, const char *format, ...)
47770Sstevel@tonic-gate {
47780Sstevel@tonic-gate int oldlineno = yylineno;
47790Sstevel@tonic-gate va_list ap;
47800Sstevel@tonic-gate
47810Sstevel@tonic-gate yylineno = dnp->dn_line;
47820Sstevel@tonic-gate
47830Sstevel@tonic-gate va_start(ap, format);
47840Sstevel@tonic-gate xyvwarn(tag, format, ap);
47850Sstevel@tonic-gate va_end(ap);
47860Sstevel@tonic-gate
47870Sstevel@tonic-gate yylineno = oldlineno;
47880Sstevel@tonic-gate }
47890Sstevel@tonic-gate
47900Sstevel@tonic-gate /*PRINTFLIKE2*/
47910Sstevel@tonic-gate void
xyerror(dt_errtag_t tag,const char * format,...)47920Sstevel@tonic-gate xyerror(dt_errtag_t tag, const char *format, ...)
47930Sstevel@tonic-gate {
47940Sstevel@tonic-gate va_list ap;
47950Sstevel@tonic-gate
47960Sstevel@tonic-gate va_start(ap, format);
47970Sstevel@tonic-gate xyvwarn(tag, format, ap);
47980Sstevel@tonic-gate va_end(ap);
47990Sstevel@tonic-gate
48000Sstevel@tonic-gate longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
48010Sstevel@tonic-gate }
48020Sstevel@tonic-gate
48030Sstevel@tonic-gate /*PRINTFLIKE2*/
48040Sstevel@tonic-gate void
xywarn(dt_errtag_t tag,const char * format,...)48050Sstevel@tonic-gate xywarn(dt_errtag_t tag, const char *format, ...)
48060Sstevel@tonic-gate {
48070Sstevel@tonic-gate va_list ap;
48080Sstevel@tonic-gate
48090Sstevel@tonic-gate va_start(ap, format);
48100Sstevel@tonic-gate xyvwarn(tag, format, ap);
48110Sstevel@tonic-gate va_end(ap);
48120Sstevel@tonic-gate }
48130Sstevel@tonic-gate
48140Sstevel@tonic-gate void
xyvwarn(dt_errtag_t tag,const char * format,va_list ap)48150Sstevel@tonic-gate xyvwarn(dt_errtag_t tag, const char *format, va_list ap)
48160Sstevel@tonic-gate {
48170Sstevel@tonic-gate if (yypcb == NULL)
48180Sstevel@tonic-gate return; /* compiler is not currently active: act as a no-op */
48190Sstevel@tonic-gate
48200Sstevel@tonic-gate dt_set_errmsg(yypcb->pcb_hdl, dt_errtag(tag), yypcb->pcb_region,
48210Sstevel@tonic-gate yypcb->pcb_filetag, yypcb->pcb_fileptr ? yylineno : 0, format, ap);
48220Sstevel@tonic-gate }
48230Sstevel@tonic-gate
48240Sstevel@tonic-gate /*PRINTFLIKE1*/
48250Sstevel@tonic-gate void
yyerror(const char * format,...)48260Sstevel@tonic-gate yyerror(const char *format, ...)
48270Sstevel@tonic-gate {
48280Sstevel@tonic-gate va_list ap;
48290Sstevel@tonic-gate
48300Sstevel@tonic-gate va_start(ap, format);
48310Sstevel@tonic-gate yyvwarn(format, ap);
48320Sstevel@tonic-gate va_end(ap);
48330Sstevel@tonic-gate
48340Sstevel@tonic-gate longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
48350Sstevel@tonic-gate }
48360Sstevel@tonic-gate
48370Sstevel@tonic-gate /*PRINTFLIKE1*/
48380Sstevel@tonic-gate void
yywarn(const char * format,...)48390Sstevel@tonic-gate yywarn(const char *format, ...)
48400Sstevel@tonic-gate {
48410Sstevel@tonic-gate va_list ap;
48420Sstevel@tonic-gate
48430Sstevel@tonic-gate va_start(ap, format);
48440Sstevel@tonic-gate yyvwarn(format, ap);
48450Sstevel@tonic-gate va_end(ap);
48460Sstevel@tonic-gate }
48470Sstevel@tonic-gate
48480Sstevel@tonic-gate void
yyvwarn(const char * format,va_list ap)48490Sstevel@tonic-gate yyvwarn(const char *format, va_list ap)
48500Sstevel@tonic-gate {
48510Sstevel@tonic-gate if (yypcb == NULL)
48520Sstevel@tonic-gate return; /* compiler is not currently active: act as a no-op */
48530Sstevel@tonic-gate
48540Sstevel@tonic-gate dt_set_errmsg(yypcb->pcb_hdl, dt_errtag(D_SYNTAX), yypcb->pcb_region,
48550Sstevel@tonic-gate yypcb->pcb_filetag, yypcb->pcb_fileptr ? yylineno : 0, format, ap);
48560Sstevel@tonic-gate
48570Sstevel@tonic-gate if (strchr(format, '\n') == NULL) {
48580Sstevel@tonic-gate dtrace_hdl_t *dtp = yypcb->pcb_hdl;
48590Sstevel@tonic-gate size_t len = strlen(dtp->dt_errmsg);
48600Sstevel@tonic-gate char *p, *s = dtp->dt_errmsg + len;
48610Sstevel@tonic-gate size_t n = sizeof (dtp->dt_errmsg) - len;
48620Sstevel@tonic-gate
48630Sstevel@tonic-gate if (yytext[0] == '\0')
48640Sstevel@tonic-gate (void) snprintf(s, n, " near end of input");
48650Sstevel@tonic-gate else if (yytext[0] == '\n')
48660Sstevel@tonic-gate (void) snprintf(s, n, " near end of line");
48670Sstevel@tonic-gate else {
48680Sstevel@tonic-gate if ((p = strchr(yytext, '\n')) != NULL)
48690Sstevel@tonic-gate *p = '\0'; /* crop at newline */
48700Sstevel@tonic-gate (void) snprintf(s, n, " near \"%s\"", yytext);
48710Sstevel@tonic-gate }
48720Sstevel@tonic-gate }
48730Sstevel@tonic-gate }
48740Sstevel@tonic-gate
48750Sstevel@tonic-gate void
yylabel(const char * label)48760Sstevel@tonic-gate yylabel(const char *label)
48770Sstevel@tonic-gate {
48780Sstevel@tonic-gate dt_dprintf("set label to <%s>\n", label ? label : "NULL");
48790Sstevel@tonic-gate yypcb->pcb_region = label;
48800Sstevel@tonic-gate }
48810Sstevel@tonic-gate
48820Sstevel@tonic-gate int
yywrap(void)48830Sstevel@tonic-gate yywrap(void)
48840Sstevel@tonic-gate {
48850Sstevel@tonic-gate return (1); /* indicate that lex should return a zero token for EOF */
48860Sstevel@tonic-gate }
4887