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