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