1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 23 /* 24 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 25 * Use is subject to license terms. 26 */ 27 28 #pragma ident "%Z%%M% %I% %E% SMI" 29 30 /* 31 * DTrace D Language Parser 32 * 33 * The D Parser is a lex/yacc parser consisting of the lexer dt_lex.l, the 34 * parsing grammar dt_grammar.y, and this file, dt_parser.c, which handles 35 * the construction of the parse tree nodes and their syntactic validation. 36 * The parse tree is constructed of dt_node_t structures (see <dt_parser.h>) 37 * that are built in two passes: (1) the "create" pass, where the parse tree 38 * nodes are allocated by calls from the grammar to dt_node_*() subroutines, 39 * and (2) the "cook" pass, where nodes are coalesced, assigned D types, and 40 * validated according to the syntactic rules of the language. 41 * 42 * All node allocations are performed using dt_node_alloc(). All node frees 43 * during the parsing phase are performed by dt_node_free(), which frees node- 44 * internal state but does not actually free the nodes. All final node frees 45 * are done as part of the end of dt_compile() or as part of destroying 46 * persistent identifiers or translators which have embedded nodes. 47 * 48 * The dt_node_* routines that implement pass (1) may allocate new nodes. The 49 * dt_cook_* routines that implement pass (2) may *not* allocate new nodes. 50 * They may free existing nodes using dt_node_free(), but they may not actually 51 * deallocate any dt_node_t's. Currently dt_cook_op2() is an exception to this 52 * rule: see the comments therein for how this issue is resolved. 53 * 54 * The dt_cook_* routines are responsible for (at minimum) setting the final 55 * node type (dn_ctfp/dn_type) and attributes (dn_attr). If dn_ctfp/dn_type 56 * are set manually (i.e. not by one of the type assignment functions), then 57 * the DT_NF_COOKED flag must be set manually on the node. 58 * 59 * The cooking pass can be applied to the same parse tree more than once (used 60 * in the case of a comma-separated list of probe descriptions). As such, the 61 * cook routines must not perform any parse tree transformations which would 62 * be invalid if the tree were subsequently cooked using a different context. 63 * 64 * The dn_ctfp and dn_type fields form the type of the node. This tuple can 65 * take on the following set of values, which form our type invariants: 66 * 67 * 1. dn_ctfp = NULL, dn_type = CTF_ERR 68 * 69 * In this state, the node has unknown type and is not yet cooked. The 70 * DT_NF_COOKED flag is not yet set on the node. 71 * 72 * 2. dn_ctfp = DT_DYN_CTFP(dtp), dn_type = DT_DYN_TYPE(dtp) 73 * 74 * In this state, the node is a dynamic D type. This means that generic 75 * operations are not valid on this node and only code that knows how to 76 * examine the inner details of the node can operate on it. A <DYN> node 77 * must have dn_ident set to point to an identifier describing the object 78 * and its type. The DT_NF_REF flag is set for all nodes of type <DYN>. 79 * At present, the D compiler uses the <DYN> type for: 80 * 81 * - associative arrays that do not yet have a value type defined 82 * - translated data (i.e. the result of the xlate operator) 83 * - aggregations 84 * 85 * 3. dn_ctfp = DT_STR_CTFP(dtp), dn_type = DT_STR_TYPE(dtp) 86 * 87 * In this state, the node is of type D string. The string type is really 88 * a char[0] typedef, but requires special handling throughout the compiler. 89 * 90 * 4. dn_ctfp != NULL, dn_type = any other type ID 91 * 92 * In this state, the node is of some known D/CTF type. The normal libctf 93 * APIs can be used to learn more about the type name or structure. When 94 * the type is assigned, the DT_NF_SIGNED, DT_NF_REF, and DT_NF_BITFIELD 95 * flags cache the corresponding attributes of the underlying CTF type. 96 */ 97 98 #include <sys/param.h> 99 #include <limits.h> 100 #include <setjmp.h> 101 #include <strings.h> 102 #include <assert.h> 103 #include <alloca.h> 104 #include <stdlib.h> 105 #include <stdarg.h> 106 #include <stdio.h> 107 #include <errno.h> 108 #include <ctype.h> 109 110 #include <dt_impl.h> 111 #include <dt_grammar.h> 112 #include <dt_module.h> 113 #include <dt_provider.h> 114 #include <dt_string.h> 115 #include <dt_as.h> 116 117 dt_pcb_t *yypcb; /* current control block for parser */ 118 dt_node_t *yypragma; /* lex token list for control lines */ 119 char yyintprefix; /* int token macro prefix (+/-) */ 120 char yyintsuffix[4]; /* int token suffix string [uU][lL] */ 121 int yyintdecimal; /* int token format flag (1=decimal, 0=octal/hex) */ 122 123 static const char * 124 opstr(int op) 125 { 126 switch (op) { 127 case DT_TOK_COMMA: return (","); 128 case DT_TOK_ELLIPSIS: return ("..."); 129 case DT_TOK_ASGN: return ("="); 130 case DT_TOK_ADD_EQ: return ("+="); 131 case DT_TOK_SUB_EQ: return ("-="); 132 case DT_TOK_MUL_EQ: return ("*="); 133 case DT_TOK_DIV_EQ: return ("/="); 134 case DT_TOK_MOD_EQ: return ("%="); 135 case DT_TOK_AND_EQ: return ("&="); 136 case DT_TOK_XOR_EQ: return ("^="); 137 case DT_TOK_OR_EQ: return ("|="); 138 case DT_TOK_LSH_EQ: return ("<<="); 139 case DT_TOK_RSH_EQ: return (">>="); 140 case DT_TOK_QUESTION: return ("?"); 141 case DT_TOK_COLON: return (":"); 142 case DT_TOK_LOR: return ("||"); 143 case DT_TOK_LXOR: return ("^^"); 144 case DT_TOK_LAND: return ("&&"); 145 case DT_TOK_BOR: return ("|"); 146 case DT_TOK_XOR: return ("^"); 147 case DT_TOK_BAND: return ("&"); 148 case DT_TOK_EQU: return ("=="); 149 case DT_TOK_NEQ: return ("!="); 150 case DT_TOK_LT: return ("<"); 151 case DT_TOK_LE: return ("<="); 152 case DT_TOK_GT: return (">"); 153 case DT_TOK_GE: return (">="); 154 case DT_TOK_LSH: return ("<<"); 155 case DT_TOK_RSH: return (">>"); 156 case DT_TOK_ADD: return ("+"); 157 case DT_TOK_SUB: return ("-"); 158 case DT_TOK_MUL: return ("*"); 159 case DT_TOK_DIV: return ("/"); 160 case DT_TOK_MOD: return ("%"); 161 case DT_TOK_LNEG: return ("!"); 162 case DT_TOK_BNEG: return ("~"); 163 case DT_TOK_ADDADD: return ("++"); 164 case DT_TOK_PREINC: return ("++"); 165 case DT_TOK_POSTINC: return ("++"); 166 case DT_TOK_SUBSUB: return ("--"); 167 case DT_TOK_PREDEC: return ("--"); 168 case DT_TOK_POSTDEC: return ("--"); 169 case DT_TOK_IPOS: return ("+"); 170 case DT_TOK_INEG: return ("-"); 171 case DT_TOK_DEREF: return ("*"); 172 case DT_TOK_ADDROF: return ("&"); 173 case DT_TOK_OFFSETOF: return ("offsetof"); 174 case DT_TOK_SIZEOF: return ("sizeof"); 175 case DT_TOK_STRINGOF: return ("stringof"); 176 case DT_TOK_XLATE: return ("xlate"); 177 case DT_TOK_LPAR: return ("("); 178 case DT_TOK_RPAR: return (")"); 179 case DT_TOK_LBRAC: return ("["); 180 case DT_TOK_RBRAC: return ("]"); 181 case DT_TOK_PTR: return ("->"); 182 case DT_TOK_DOT: return ("."); 183 case DT_TOK_STRING: return ("<string>"); 184 case DT_TOK_IDENT: return ("<ident>"); 185 case DT_TOK_TNAME: return ("<type>"); 186 case DT_TOK_INT: return ("<int>"); 187 default: return ("<?>"); 188 } 189 } 190 191 int 192 dt_type_lookup(const char *s, dtrace_typeinfo_t *tip) 193 { 194 static const char delimiters[] = " \t\n\r\v\f*`"; 195 dtrace_hdl_t *dtp = yypcb->pcb_hdl; 196 const char *p, *q, *end, *obj; 197 198 for (p = s, end = s + strlen(s); *p != '\0'; p = q) { 199 while (isspace(*p)) 200 p++; /* skip leading whitespace prior to token */ 201 202 if (p == end || (q = strpbrk(p + 1, delimiters)) == NULL) 203 break; /* empty string or single token remaining */ 204 205 if (*q == '`') { 206 char *object = alloca((size_t)(q - p) + 1); 207 char *type = alloca((size_t)(end - s) + 1); 208 209 /* 210 * Copy from the start of the token (p) to the location 211 * backquote (q) to extract the nul-terminated object. 212 */ 213 bcopy(p, object, (size_t)(q - p)); 214 object[(size_t)(q - p)] = '\0'; 215 216 /* 217 * Copy the original string up to the start of this 218 * token (p) into type, and then concatenate everything 219 * after q. This is the type name without the object. 220 */ 221 bcopy(s, type, (size_t)(p - s)); 222 bcopy(q + 1, type + (size_t)(p - s), strlen(q + 1) + 1); 223 224 if (strchr(q + 1, '`') != NULL) 225 return (dt_set_errno(dtp, EDT_BADSCOPE)); 226 227 return (dtrace_lookup_by_type(dtp, object, type, tip)); 228 } 229 } 230 231 if (yypcb->pcb_idepth != 0) 232 obj = DTRACE_OBJ_CDEFS; 233 else 234 obj = DTRACE_OBJ_EVERY; 235 236 return (dtrace_lookup_by_type(dtp, obj, s, tip)); 237 } 238 239 /* 240 * When we parse type expressions or parse an expression with unary "&", we 241 * need to find a type that is a pointer to a previously known type. 242 * Unfortunately CTF is limited to a per-container view, so ctf_type_pointer() 243 * alone does not suffice for our needs. We provide a more intelligent wrapper 244 * for the compiler that attempts to compute a pointer to either the given type 245 * or its base (that is, we try both "foo_t *" and "struct foo *"), and also 246 * to potentially construct the required type on-the-fly. 247 */ 248 int 249 dt_type_pointer(dtrace_typeinfo_t *tip) 250 { 251 dtrace_hdl_t *dtp = yypcb->pcb_hdl; 252 ctf_file_t *ctfp = tip->dtt_ctfp; 253 ctf_id_t type = tip->dtt_type; 254 ctf_id_t base = ctf_type_resolve(ctfp, type); 255 256 dt_module_t *dmp; 257 ctf_id_t ptr; 258 259 if ((ptr = ctf_type_pointer(ctfp, type)) != CTF_ERR || 260 (ptr = ctf_type_pointer(ctfp, base)) != CTF_ERR) { 261 tip->dtt_type = ptr; 262 return (0); 263 } 264 265 if (yypcb->pcb_idepth != 0) 266 dmp = dtp->dt_cdefs; 267 else 268 dmp = dtp->dt_ddefs; 269 270 if (ctfp != dmp->dm_ctfp && ctfp != ctf_parent_file(dmp->dm_ctfp) && 271 (type = ctf_add_type(dmp->dm_ctfp, ctfp, type)) == CTF_ERR) { 272 dtp->dt_ctferr = ctf_errno(dmp->dm_ctfp); 273 return (dt_set_errno(dtp, EDT_CTF)); 274 } 275 276 ptr = ctf_add_pointer(dmp->dm_ctfp, CTF_ADD_ROOT, type); 277 278 if (ptr == CTF_ERR || ctf_update(dmp->dm_ctfp) == CTF_ERR) { 279 dtp->dt_ctferr = ctf_errno(dmp->dm_ctfp); 280 return (dt_set_errno(dtp, EDT_CTF)); 281 } 282 283 tip->dtt_object = dmp->dm_name; 284 tip->dtt_ctfp = dmp->dm_ctfp; 285 tip->dtt_type = ptr; 286 287 return (0); 288 } 289 290 const char * 291 dt_type_name(ctf_file_t *ctfp, ctf_id_t type, char *buf, size_t len) 292 { 293 dtrace_hdl_t *dtp = yypcb->pcb_hdl; 294 295 if (ctfp == DT_FPTR_CTFP(dtp) && type == DT_FPTR_TYPE(dtp)) 296 (void) snprintf(buf, len, "function pointer"); 297 else if (ctfp == DT_FUNC_CTFP(dtp) && type == DT_FUNC_TYPE(dtp)) 298 (void) snprintf(buf, len, "function"); 299 else if (ctfp == DT_DYN_CTFP(dtp) && type == DT_DYN_TYPE(dtp)) 300 (void) snprintf(buf, len, "dynamic variable"); 301 else if (ctfp == NULL) 302 (void) snprintf(buf, len, "<none>"); 303 else if (ctf_type_name(ctfp, type, buf, len) == NULL) 304 (void) snprintf(buf, len, "unknown"); 305 306 return (buf); 307 } 308 309 /* 310 * Perform the "usual arithmetic conversions" to determine which of the two 311 * input operand types should be promoted and used as a result type. The 312 * rules for this are described in ISOC[6.3.1.8] and K&R[A6.5]. 313 */ 314 static void 315 dt_type_promote(dt_node_t *lp, dt_node_t *rp, ctf_file_t **ofp, ctf_id_t *otype) 316 { 317 ctf_file_t *lfp = lp->dn_ctfp; 318 ctf_id_t ltype = lp->dn_type; 319 320 ctf_file_t *rfp = rp->dn_ctfp; 321 ctf_id_t rtype = rp->dn_type; 322 323 ctf_id_t lbase = ctf_type_resolve(lfp, ltype); 324 uint_t lkind = ctf_type_kind(lfp, lbase); 325 326 ctf_id_t rbase = ctf_type_resolve(rfp, rtype); 327 uint_t rkind = ctf_type_kind(rfp, rbase); 328 329 dtrace_hdl_t *dtp = yypcb->pcb_hdl; 330 ctf_encoding_t le, re; 331 uint_t lrank, rrank; 332 333 assert(lkind == CTF_K_INTEGER || lkind == CTF_K_ENUM); 334 assert(rkind == CTF_K_INTEGER || rkind == CTF_K_ENUM); 335 336 if (lkind == CTF_K_ENUM) { 337 lfp = DT_INT_CTFP(dtp); 338 ltype = lbase = DT_INT_TYPE(dtp); 339 } 340 341 if (rkind == CTF_K_ENUM) { 342 rfp = DT_INT_CTFP(dtp); 343 rtype = rbase = DT_INT_TYPE(dtp); 344 } 345 346 if (ctf_type_encoding(lfp, lbase, &le) == CTF_ERR) { 347 yypcb->pcb_hdl->dt_ctferr = ctf_errno(lfp); 348 longjmp(yypcb->pcb_jmpbuf, EDT_CTF); 349 } 350 351 if (ctf_type_encoding(rfp, rbase, &re) == CTF_ERR) { 352 yypcb->pcb_hdl->dt_ctferr = ctf_errno(rfp); 353 longjmp(yypcb->pcb_jmpbuf, EDT_CTF); 354 } 355 356 /* 357 * Compute an integer rank based on the size and unsigned status. 358 * If rank is identical, pick the "larger" of the equivalent types 359 * which we define as having a larger base ctf_id_t. If rank is 360 * different, pick the type with the greater rank. 361 */ 362 lrank = le.cte_bits + ((le.cte_format & CTF_INT_SIGNED) == 0); 363 rrank = re.cte_bits + ((re.cte_format & CTF_INT_SIGNED) == 0); 364 365 if (lrank == rrank) { 366 if (lbase - rbase < 0) 367 goto return_rtype; 368 else 369 goto return_ltype; 370 } else if (lrank > rrank) { 371 goto return_ltype; 372 } else 373 goto return_rtype; 374 375 return_ltype: 376 *ofp = lfp; 377 *otype = ltype; 378 return; 379 380 return_rtype: 381 *ofp = rfp; 382 *otype = rtype; 383 } 384 385 void 386 dt_node_promote(dt_node_t *lp, dt_node_t *rp, dt_node_t *dnp) 387 { 388 dt_type_promote(lp, rp, &dnp->dn_ctfp, &dnp->dn_type); 389 dt_node_type_assign(dnp, dnp->dn_ctfp, dnp->dn_type); 390 dt_node_attr_assign(dnp, dt_attr_min(lp->dn_attr, rp->dn_attr)); 391 } 392 393 const char * 394 dt_node_name(const dt_node_t *dnp, char *buf, size_t len) 395 { 396 char n1[DT_TYPE_NAMELEN]; 397 char n2[DT_TYPE_NAMELEN]; 398 399 const char *prefix = "", *suffix = ""; 400 const dtrace_syminfo_t *dts; 401 char *s; 402 403 switch (dnp->dn_kind) { 404 case DT_NODE_INT: 405 (void) snprintf(buf, len, "integer constant 0x%llx", 406 (u_longlong_t)dnp->dn_value); 407 break; 408 case DT_NODE_STRING: 409 s = strchr2esc(dnp->dn_string, strlen(dnp->dn_string)); 410 (void) snprintf(buf, len, "string constant \"%s\"", 411 s != NULL ? s : dnp->dn_string); 412 free(s); 413 break; 414 case DT_NODE_IDENT: 415 (void) snprintf(buf, len, "identifier %s", dnp->dn_string); 416 break; 417 case DT_NODE_VAR: 418 case DT_NODE_FUNC: 419 case DT_NODE_AGG: 420 case DT_NODE_INLINE: 421 switch (dnp->dn_ident->di_kind) { 422 case DT_IDENT_FUNC: 423 case DT_IDENT_AGGFUNC: 424 case DT_IDENT_ACTFUNC: 425 suffix = "( )"; 426 break; 427 case DT_IDENT_AGG: 428 prefix = "@"; 429 break; 430 } 431 (void) snprintf(buf, len, "%s %s%s%s", 432 dt_idkind_name(dnp->dn_ident->di_kind), 433 prefix, dnp->dn_ident->di_name, suffix); 434 break; 435 case DT_NODE_SYM: 436 dts = dnp->dn_ident->di_data; 437 (void) snprintf(buf, len, "symbol %s`%s", 438 dts->dts_object, dts->dts_name); 439 break; 440 case DT_NODE_TYPE: 441 (void) snprintf(buf, len, "type %s", 442 dt_node_type_name(dnp, n1, sizeof (n1))); 443 break; 444 case DT_NODE_OP1: 445 case DT_NODE_OP2: 446 case DT_NODE_OP3: 447 (void) snprintf(buf, len, "operator %s", opstr(dnp->dn_op)); 448 break; 449 case DT_NODE_DEXPR: 450 case DT_NODE_DFUNC: 451 if (dnp->dn_expr) 452 return (dt_node_name(dnp->dn_expr, buf, len)); 453 (void) snprintf(buf, len, "%s", "statement"); 454 break; 455 case DT_NODE_PDESC: 456 if (dnp->dn_desc->dtpd_id == 0) { 457 (void) snprintf(buf, len, 458 "probe description %s:%s:%s:%s", 459 dnp->dn_desc->dtpd_provider, dnp->dn_desc->dtpd_mod, 460 dnp->dn_desc->dtpd_func, dnp->dn_desc->dtpd_name); 461 } else { 462 (void) snprintf(buf, len, "probe description %u", 463 dnp->dn_desc->dtpd_id); 464 } 465 break; 466 case DT_NODE_CLAUSE: 467 (void) snprintf(buf, len, "%s", "clause"); 468 break; 469 case DT_NODE_MEMBER: 470 (void) snprintf(buf, len, "member %s", dnp->dn_membname); 471 break; 472 case DT_NODE_XLATOR: 473 (void) snprintf(buf, len, "translator <%s> (%s)", 474 dt_type_name(dnp->dn_xlator->dx_dst_ctfp, 475 dnp->dn_xlator->dx_dst_type, n1, sizeof (n1)), 476 dt_type_name(dnp->dn_xlator->dx_src_ctfp, 477 dnp->dn_xlator->dx_src_type, n2, sizeof (n2))); 478 break; 479 case DT_NODE_PROG: 480 (void) snprintf(buf, len, "%s", "program"); 481 break; 482 default: 483 (void) snprintf(buf, len, "node <%u>", dnp->dn_kind); 484 break; 485 } 486 487 return (buf); 488 } 489 490 /* 491 * dt_node_xalloc() can be used to create new parse nodes from any libdtrace 492 * caller. The caller is responsible for assigning dn_link appropriately. 493 */ 494 dt_node_t * 495 dt_node_xalloc(dtrace_hdl_t *dtp, int kind) 496 { 497 dt_node_t *dnp = dt_alloc(dtp, sizeof (dt_node_t)); 498 499 if (dnp == NULL) 500 return (NULL); 501 502 dnp->dn_ctfp = NULL; 503 dnp->dn_type = CTF_ERR; 504 dnp->dn_kind = (uchar_t)kind; 505 dnp->dn_flags = 0; 506 dnp->dn_op = 0; 507 dnp->dn_line = -1; 508 dnp->dn_reg = -1; 509 dnp->dn_attr = _dtrace_defattr; 510 dnp->dn_list = NULL; 511 dnp->dn_link = NULL; 512 bzero(&dnp->dn_u, sizeof (dnp->dn_u)); 513 514 return (dnp); 515 } 516 517 /* 518 * dt_node_alloc() is used to create new parse nodes from the parser. It 519 * assigns the node location based on the current lexer line number and places 520 * the new node on the default allocation list. If allocation fails, we 521 * automatically longjmp the caller back to the enclosing compilation call. 522 */ 523 static dt_node_t * 524 dt_node_alloc(int kind) 525 { 526 dt_node_t *dnp = dt_node_xalloc(yypcb->pcb_hdl, kind); 527 528 if (dnp == NULL) 529 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM); 530 531 dnp->dn_line = yylineno; 532 dnp->dn_link = yypcb->pcb_list; 533 yypcb->pcb_list = dnp; 534 535 return (dnp); 536 } 537 538 void 539 dt_node_free(dt_node_t *dnp) 540 { 541 uchar_t kind = dnp->dn_kind; 542 543 dnp->dn_kind = DT_NODE_FREE; 544 545 switch (kind) { 546 case DT_NODE_STRING: 547 case DT_NODE_IDENT: 548 case DT_NODE_TYPE: 549 free(dnp->dn_string); 550 dnp->dn_string = NULL; 551 break; 552 553 case DT_NODE_VAR: 554 case DT_NODE_FUNC: 555 case DT_NODE_PROBE: 556 if (dnp->dn_ident != NULL) { 557 if (dnp->dn_ident->di_flags & DT_IDFLG_ORPHAN) 558 dt_ident_destroy(dnp->dn_ident); 559 dnp->dn_ident = NULL; 560 } 561 dt_node_list_free(&dnp->dn_args); 562 break; 563 564 case DT_NODE_OP1: 565 if (dnp->dn_child != NULL) { 566 dt_node_free(dnp->dn_child); 567 dnp->dn_child = NULL; 568 } 569 break; 570 571 case DT_NODE_OP3: 572 if (dnp->dn_expr != NULL) { 573 dt_node_free(dnp->dn_expr); 574 dnp->dn_expr = NULL; 575 } 576 /*FALLTHRU*/ 577 case DT_NODE_OP2: 578 if (dnp->dn_left != NULL) { 579 dt_node_free(dnp->dn_left); 580 dnp->dn_left = NULL; 581 } 582 if (dnp->dn_right != NULL) { 583 dt_node_free(dnp->dn_right); 584 dnp->dn_right = NULL; 585 } 586 break; 587 588 case DT_NODE_DEXPR: 589 case DT_NODE_DFUNC: 590 if (dnp->dn_expr != NULL) { 591 dt_node_free(dnp->dn_expr); 592 dnp->dn_expr = NULL; 593 } 594 break; 595 596 case DT_NODE_AGG: 597 if (dnp->dn_aggfun != NULL) { 598 dt_node_free(dnp->dn_aggfun); 599 dnp->dn_aggfun = NULL; 600 } 601 dt_node_list_free(&dnp->dn_aggtup); 602 break; 603 604 case DT_NODE_PDESC: 605 free(dnp->dn_spec); 606 dnp->dn_spec = NULL; 607 free(dnp->dn_desc); 608 dnp->dn_desc = NULL; 609 break; 610 611 case DT_NODE_CLAUSE: 612 if (dnp->dn_pred != NULL) 613 dt_node_free(dnp->dn_pred); 614 if (dnp->dn_locals != NULL) 615 dt_idhash_destroy(dnp->dn_locals); 616 dt_node_list_free(&dnp->dn_pdescs); 617 dt_node_list_free(&dnp->dn_acts); 618 break; 619 620 case DT_NODE_MEMBER: 621 free(dnp->dn_membname); 622 dnp->dn_membname = NULL; 623 if (dnp->dn_membexpr != NULL) { 624 dt_node_free(dnp->dn_membexpr); 625 dnp->dn_membexpr = NULL; 626 } 627 break; 628 629 case DT_NODE_PROVIDER: 630 dt_node_list_free(&dnp->dn_probes); 631 free(dnp->dn_provname); 632 dnp->dn_provname = NULL; 633 break; 634 635 case DT_NODE_PROG: 636 dt_node_list_free(&dnp->dn_list); 637 break; 638 } 639 } 640 641 void 642 dt_node_attr_assign(dt_node_t *dnp, dtrace_attribute_t attr) 643 { 644 if ((yypcb->pcb_cflags & DTRACE_C_EATTR) && 645 (dt_attr_cmp(attr, yypcb->pcb_amin) < 0)) { 646 char a[DTRACE_ATTR2STR_MAX]; 647 char s[BUFSIZ]; 648 649 dnerror(dnp, D_ATTR_MIN, "attributes for %s (%s) are less than " 650 "predefined minimum\n", dt_node_name(dnp, s, sizeof (s)), 651 dtrace_attr2str(attr, a, sizeof (a))); 652 } 653 654 dnp->dn_attr = attr; 655 } 656 657 void 658 dt_node_type_assign(dt_node_t *dnp, ctf_file_t *fp, ctf_id_t type) 659 { 660 ctf_id_t base = ctf_type_resolve(fp, type); 661 uint_t kind = ctf_type_kind(fp, base); 662 ctf_encoding_t e; 663 664 dnp->dn_flags &= 665 ~(DT_NF_SIGNED | DT_NF_REF | DT_NF_BITFIELD | DT_NF_USERLAND); 666 667 if (kind == CTF_K_INTEGER && ctf_type_encoding(fp, base, &e) == 0) { 668 size_t size = e.cte_bits / NBBY; 669 670 if (size > 8 || (e.cte_bits % NBBY) != 0 || (size & (size - 1))) 671 dnp->dn_flags |= DT_NF_BITFIELD; 672 673 if (e.cte_format & CTF_INT_SIGNED) 674 dnp->dn_flags |= DT_NF_SIGNED; 675 } 676 677 if (kind == CTF_K_FLOAT && ctf_type_encoding(fp, base, &e) == 0) { 678 if (e.cte_bits / NBBY > sizeof (uint64_t)) 679 dnp->dn_flags |= DT_NF_REF; 680 } 681 682 if (kind == CTF_K_STRUCT || kind == CTF_K_UNION || 683 kind == CTF_K_FORWARD || 684 kind == CTF_K_ARRAY || kind == CTF_K_FUNCTION) 685 dnp->dn_flags |= DT_NF_REF; 686 else if (yypcb != NULL && fp == DT_DYN_CTFP(yypcb->pcb_hdl) && 687 type == DT_DYN_TYPE(yypcb->pcb_hdl)) 688 dnp->dn_flags |= DT_NF_REF; 689 690 dnp->dn_flags |= DT_NF_COOKED; 691 dnp->dn_ctfp = fp; 692 dnp->dn_type = type; 693 } 694 695 void 696 dt_node_type_propagate(const dt_node_t *src, dt_node_t *dst) 697 { 698 assert(src->dn_flags & DT_NF_COOKED); 699 dst->dn_flags = src->dn_flags & ~DT_NF_LVALUE; 700 dst->dn_ctfp = src->dn_ctfp; 701 dst->dn_type = src->dn_type; 702 } 703 704 const char * 705 dt_node_type_name(const dt_node_t *dnp, char *buf, size_t len) 706 { 707 if (dt_node_is_dynamic(dnp) && dnp->dn_ident != NULL) { 708 (void) snprintf(buf, len, "%s", 709 dt_idkind_name(dt_ident_resolve(dnp->dn_ident)->di_kind)); 710 return (buf); 711 } 712 713 if (dnp->dn_flags & DT_NF_USERLAND) { 714 size_t n = snprintf(buf, len, "userland "); 715 len = len > n ? len - n : 0; 716 (void) dt_type_name(dnp->dn_ctfp, dnp->dn_type, buf + n, len); 717 return (buf); 718 } 719 720 return (dt_type_name(dnp->dn_ctfp, dnp->dn_type, buf, len)); 721 } 722 723 size_t 724 dt_node_type_size(const dt_node_t *dnp) 725 { 726 if (dnp->dn_kind == DT_NODE_STRING) 727 return (strlen(dnp->dn_string) + 1); 728 729 if (dt_node_is_dynamic(dnp) && dnp->dn_ident != NULL) 730 return (dt_ident_size(dnp->dn_ident)); 731 732 return (ctf_type_size(dnp->dn_ctfp, dnp->dn_type)); 733 } 734 735 /* 736 * Determine if the specified parse tree node references an identifier of the 737 * specified kind, and if so return a pointer to it; otherwise return NULL. 738 * This function resolves the identifier itself, following through any inlines. 739 */ 740 dt_ident_t * 741 dt_node_resolve(const dt_node_t *dnp, uint_t idkind) 742 { 743 dt_ident_t *idp; 744 745 switch (dnp->dn_kind) { 746 case DT_NODE_VAR: 747 case DT_NODE_SYM: 748 case DT_NODE_FUNC: 749 case DT_NODE_AGG: 750 case DT_NODE_INLINE: 751 case DT_NODE_PROBE: 752 idp = dt_ident_resolve(dnp->dn_ident); 753 return (idp->di_kind == idkind ? idp : NULL); 754 } 755 756 if (dt_node_is_dynamic(dnp)) { 757 idp = dt_ident_resolve(dnp->dn_ident); 758 return (idp->di_kind == idkind ? idp : NULL); 759 } 760 761 return (NULL); 762 } 763 764 size_t 765 dt_node_sizeof(const dt_node_t *dnp) 766 { 767 dtrace_syminfo_t *sip; 768 GElf_Sym sym; 769 dtrace_hdl_t *dtp = yypcb->pcb_hdl; 770 771 /* 772 * The size of the node as used for the sizeof() operator depends on 773 * the kind of the node. If the node is a SYM, the size is obtained 774 * from the symbol table; if it is not a SYM, the size is determined 775 * from the node's type. This is slightly different from C's sizeof() 776 * operator in that (for example) when applied to a function, sizeof() 777 * will evaluate to the length of the function rather than the size of 778 * the function type. 779 */ 780 if (dnp->dn_kind != DT_NODE_SYM) 781 return (dt_node_type_size(dnp)); 782 783 sip = dnp->dn_ident->di_data; 784 785 if (dtrace_lookup_by_name(dtp, sip->dts_object, 786 sip->dts_name, &sym, NULL) == -1) 787 return (0); 788 789 return (sym.st_size); 790 } 791 792 int 793 dt_node_is_integer(const dt_node_t *dnp) 794 { 795 ctf_file_t *fp = dnp->dn_ctfp; 796 ctf_encoding_t e; 797 ctf_id_t type; 798 uint_t kind; 799 800 assert(dnp->dn_flags & DT_NF_COOKED); 801 802 type = ctf_type_resolve(fp, dnp->dn_type); 803 kind = ctf_type_kind(fp, type); 804 805 if (kind == CTF_K_INTEGER && 806 ctf_type_encoding(fp, type, &e) == 0 && IS_VOID(e)) 807 return (0); /* void integer */ 808 809 return (kind == CTF_K_INTEGER || kind == CTF_K_ENUM); 810 } 811 812 int 813 dt_node_is_float(const dt_node_t *dnp) 814 { 815 ctf_file_t *fp = dnp->dn_ctfp; 816 ctf_encoding_t e; 817 ctf_id_t type; 818 uint_t kind; 819 820 assert(dnp->dn_flags & DT_NF_COOKED); 821 822 type = ctf_type_resolve(fp, dnp->dn_type); 823 kind = ctf_type_kind(fp, type); 824 825 return (kind == CTF_K_FLOAT && 826 ctf_type_encoding(dnp->dn_ctfp, type, &e) == 0 && ( 827 e.cte_format == CTF_FP_SINGLE || e.cte_format == CTF_FP_DOUBLE || 828 e.cte_format == CTF_FP_LDOUBLE)); 829 } 830 831 int 832 dt_node_is_scalar(const dt_node_t *dnp) 833 { 834 ctf_file_t *fp = dnp->dn_ctfp; 835 ctf_encoding_t e; 836 ctf_id_t type; 837 uint_t kind; 838 839 assert(dnp->dn_flags & DT_NF_COOKED); 840 841 type = ctf_type_resolve(fp, dnp->dn_type); 842 kind = ctf_type_kind(fp, type); 843 844 if (kind == CTF_K_INTEGER && 845 ctf_type_encoding(fp, type, &e) == 0 && IS_VOID(e)) 846 return (0); /* void cannot be used as a scalar */ 847 848 return (kind == CTF_K_INTEGER || kind == CTF_K_ENUM || 849 kind == CTF_K_POINTER); 850 } 851 852 int 853 dt_node_is_arith(const dt_node_t *dnp) 854 { 855 ctf_file_t *fp = dnp->dn_ctfp; 856 ctf_encoding_t e; 857 ctf_id_t type; 858 uint_t kind; 859 860 assert(dnp->dn_flags & DT_NF_COOKED); 861 862 type = ctf_type_resolve(fp, dnp->dn_type); 863 kind = ctf_type_kind(fp, type); 864 865 if (kind == CTF_K_INTEGER) 866 return (ctf_type_encoding(fp, type, &e) == 0 && !IS_VOID(e)); 867 else 868 return (kind == CTF_K_ENUM); 869 } 870 871 int 872 dt_node_is_vfptr(const dt_node_t *dnp) 873 { 874 ctf_file_t *fp = dnp->dn_ctfp; 875 ctf_encoding_t e; 876 ctf_id_t type; 877 uint_t kind; 878 879 assert(dnp->dn_flags & DT_NF_COOKED); 880 881 type = ctf_type_resolve(fp, dnp->dn_type); 882 if (ctf_type_kind(fp, type) != CTF_K_POINTER) 883 return (0); /* type is not a pointer */ 884 885 type = ctf_type_resolve(fp, ctf_type_reference(fp, type)); 886 kind = ctf_type_kind(fp, type); 887 888 return (kind == CTF_K_FUNCTION || (kind == CTF_K_INTEGER && 889 ctf_type_encoding(fp, type, &e) == 0 && IS_VOID(e))); 890 } 891 892 int 893 dt_node_is_dynamic(const dt_node_t *dnp) 894 { 895 if (dnp->dn_kind == DT_NODE_VAR && 896 (dnp->dn_ident->di_flags & DT_IDFLG_INLINE)) { 897 const dt_idnode_t *inp = dnp->dn_ident->di_iarg; 898 return (inp->din_root ? dt_node_is_dynamic(inp->din_root) : 0); 899 } 900 901 return (dnp->dn_ctfp == DT_DYN_CTFP(yypcb->pcb_hdl) && 902 dnp->dn_type == DT_DYN_TYPE(yypcb->pcb_hdl)); 903 } 904 905 int 906 dt_node_is_string(const dt_node_t *dnp) 907 { 908 return (dnp->dn_ctfp == DT_STR_CTFP(yypcb->pcb_hdl) && 909 dnp->dn_type == DT_STR_TYPE(yypcb->pcb_hdl)); 910 } 911 912 int 913 dt_node_is_stack(const dt_node_t *dnp) 914 { 915 return (dnp->dn_ctfp == DT_STACK_CTFP(yypcb->pcb_hdl) && 916 dnp->dn_type == DT_STACK_TYPE(yypcb->pcb_hdl)); 917 } 918 919 int 920 dt_node_is_symaddr(const dt_node_t *dnp) 921 { 922 return (dnp->dn_ctfp == DT_SYMADDR_CTFP(yypcb->pcb_hdl) && 923 dnp->dn_type == DT_SYMADDR_TYPE(yypcb->pcb_hdl)); 924 } 925 926 int 927 dt_node_is_usymaddr(const dt_node_t *dnp) 928 { 929 return (dnp->dn_ctfp == DT_USYMADDR_CTFP(yypcb->pcb_hdl) && 930 dnp->dn_type == DT_USYMADDR_TYPE(yypcb->pcb_hdl)); 931 } 932 933 int 934 dt_node_is_strcompat(const dt_node_t *dnp) 935 { 936 ctf_file_t *fp = dnp->dn_ctfp; 937 ctf_encoding_t e; 938 ctf_arinfo_t r; 939 ctf_id_t base; 940 uint_t kind; 941 942 assert(dnp->dn_flags & DT_NF_COOKED); 943 944 base = ctf_type_resolve(fp, dnp->dn_type); 945 kind = ctf_type_kind(fp, base); 946 947 if (kind == CTF_K_POINTER && 948 (base = ctf_type_reference(fp, base)) != CTF_ERR && 949 (base = ctf_type_resolve(fp, base)) != CTF_ERR && 950 ctf_type_encoding(fp, base, &e) == 0 && IS_CHAR(e)) 951 return (1); /* promote char pointer to string */ 952 953 if (kind == CTF_K_ARRAY && ctf_array_info(fp, base, &r) == 0 && 954 (base = ctf_type_resolve(fp, r.ctr_contents)) != CTF_ERR && 955 ctf_type_encoding(fp, base, &e) == 0 && IS_CHAR(e)) 956 return (1); /* promote char array to string */ 957 958 return (0); 959 } 960 961 int 962 dt_node_is_pointer(const dt_node_t *dnp) 963 { 964 ctf_file_t *fp = dnp->dn_ctfp; 965 uint_t kind; 966 967 assert(dnp->dn_flags & DT_NF_COOKED); 968 969 if (dt_node_is_string(dnp)) 970 return (0); /* string are pass-by-ref but act like structs */ 971 972 kind = ctf_type_kind(fp, ctf_type_resolve(fp, dnp->dn_type)); 973 return (kind == CTF_K_POINTER || kind == CTF_K_ARRAY); 974 } 975 976 int 977 dt_node_is_void(const dt_node_t *dnp) 978 { 979 ctf_file_t *fp = dnp->dn_ctfp; 980 ctf_encoding_t e; 981 ctf_id_t type; 982 983 if (dt_node_is_dynamic(dnp)) 984 return (0); /* <DYN> is an alias for void but not the same */ 985 986 if (dt_node_is_stack(dnp)) 987 return (0); 988 989 if (dt_node_is_symaddr(dnp) || dt_node_is_usymaddr(dnp)) 990 return (0); 991 992 type = ctf_type_resolve(fp, dnp->dn_type); 993 994 return (ctf_type_kind(fp, type) == CTF_K_INTEGER && 995 ctf_type_encoding(fp, type, &e) == 0 && IS_VOID(e)); 996 } 997 998 int 999 dt_node_is_ptrcompat(const dt_node_t *lp, const dt_node_t *rp, 1000 ctf_file_t **fpp, ctf_id_t *tp) 1001 { 1002 ctf_file_t *lfp = lp->dn_ctfp; 1003 ctf_file_t *rfp = rp->dn_ctfp; 1004 1005 ctf_id_t lbase = CTF_ERR, rbase = CTF_ERR; 1006 ctf_id_t lref = CTF_ERR, rref = CTF_ERR; 1007 1008 int lp_is_void, rp_is_void, lp_is_int, rp_is_int, compat; 1009 uint_t lkind, rkind; 1010 ctf_encoding_t e; 1011 ctf_arinfo_t r; 1012 1013 assert(lp->dn_flags & DT_NF_COOKED); 1014 assert(rp->dn_flags & DT_NF_COOKED); 1015 1016 if (dt_node_is_dynamic(lp) || dt_node_is_dynamic(rp)) 1017 return (0); /* fail if either node is a dynamic variable */ 1018 1019 lp_is_int = dt_node_is_integer(lp); 1020 rp_is_int = dt_node_is_integer(rp); 1021 1022 if (lp_is_int && rp_is_int) 1023 return (0); /* fail if both nodes are integers */ 1024 1025 if (lp_is_int && (lp->dn_kind != DT_NODE_INT || lp->dn_value != 0)) 1026 return (0); /* fail if lp is an integer that isn't 0 constant */ 1027 1028 if (rp_is_int && (rp->dn_kind != DT_NODE_INT || rp->dn_value != 0)) 1029 return (0); /* fail if rp is an integer that isn't 0 constant */ 1030 1031 if ((lp_is_int == 0 && rp_is_int == 0) && ( 1032 (lp->dn_flags & DT_NF_USERLAND) ^ (rp->dn_flags & DT_NF_USERLAND))) 1033 return (0); /* fail if only one pointer is a userland address */ 1034 1035 /* 1036 * Resolve the left-hand and right-hand types to their base type, and 1037 * then resolve the referenced type as well (assuming the base type 1038 * is CTF_K_POINTER or CTF_K_ARRAY). Otherwise [lr]ref = CTF_ERR. 1039 */ 1040 if (!lp_is_int) { 1041 lbase = ctf_type_resolve(lfp, lp->dn_type); 1042 lkind = ctf_type_kind(lfp, lbase); 1043 1044 if (lkind == CTF_K_POINTER) { 1045 lref = ctf_type_resolve(lfp, 1046 ctf_type_reference(lfp, lbase)); 1047 } else if (lkind == CTF_K_ARRAY && 1048 ctf_array_info(lfp, lbase, &r) == 0) { 1049 lref = ctf_type_resolve(lfp, r.ctr_contents); 1050 } 1051 } 1052 1053 if (!rp_is_int) { 1054 rbase = ctf_type_resolve(rfp, rp->dn_type); 1055 rkind = ctf_type_kind(rfp, rbase); 1056 1057 if (rkind == CTF_K_POINTER) { 1058 rref = ctf_type_resolve(rfp, 1059 ctf_type_reference(rfp, rbase)); 1060 } else if (rkind == CTF_K_ARRAY && 1061 ctf_array_info(rfp, rbase, &r) == 0) { 1062 rref = ctf_type_resolve(rfp, r.ctr_contents); 1063 } 1064 } 1065 1066 /* 1067 * We know that one or the other type may still be a zero-valued 1068 * integer constant. To simplify the code below, set the integer 1069 * type variables equal to the non-integer types and proceed. 1070 */ 1071 if (lp_is_int) { 1072 lbase = rbase; 1073 lkind = rkind; 1074 lref = rref; 1075 lfp = rfp; 1076 } else if (rp_is_int) { 1077 rbase = lbase; 1078 rkind = lkind; 1079 rref = lref; 1080 rfp = lfp; 1081 } 1082 1083 lp_is_void = ctf_type_encoding(lfp, lref, &e) == 0 && IS_VOID(e); 1084 rp_is_void = ctf_type_encoding(rfp, rref, &e) == 0 && IS_VOID(e); 1085 1086 /* 1087 * The types are compatible if both are pointers to the same type, or 1088 * if either pointer is a void pointer. If they are compatible, set 1089 * tp to point to the more specific pointer type and return it. 1090 */ 1091 compat = (lkind == CTF_K_POINTER || lkind == CTF_K_ARRAY) && 1092 (rkind == CTF_K_POINTER || rkind == CTF_K_ARRAY) && 1093 (lp_is_void || rp_is_void || ctf_type_compat(lfp, lref, rfp, rref)); 1094 1095 if (compat) { 1096 if (fpp != NULL) 1097 *fpp = rp_is_void ? lfp : rfp; 1098 if (tp != NULL) 1099 *tp = rp_is_void ? lbase : rbase; 1100 } 1101 1102 return (compat); 1103 } 1104 1105 /* 1106 * The rules for checking argument types against parameter types are described 1107 * in the ANSI-C spec (see K&R[A7.3.2] and K&R[A7.17]). We use the same rule 1108 * set to determine whether associative array arguments match the prototype. 1109 */ 1110 int 1111 dt_node_is_argcompat(const dt_node_t *lp, const dt_node_t *rp) 1112 { 1113 ctf_file_t *lfp = lp->dn_ctfp; 1114 ctf_file_t *rfp = rp->dn_ctfp; 1115 1116 assert(lp->dn_flags & DT_NF_COOKED); 1117 assert(rp->dn_flags & DT_NF_COOKED); 1118 1119 if (dt_node_is_integer(lp) && dt_node_is_integer(rp)) 1120 return (1); /* integer types are compatible */ 1121 1122 if (dt_node_is_strcompat(lp) && dt_node_is_strcompat(rp)) 1123 return (1); /* string types are compatible */ 1124 1125 if (dt_node_is_stack(lp) && dt_node_is_stack(rp)) 1126 return (1); /* stack types are compatible */ 1127 1128 if (dt_node_is_symaddr(lp) && dt_node_is_symaddr(rp)) 1129 return (1); /* symaddr types are compatible */ 1130 1131 if (dt_node_is_usymaddr(lp) && dt_node_is_usymaddr(rp)) 1132 return (1); /* usymaddr types are compatible */ 1133 1134 switch (ctf_type_kind(lfp, ctf_type_resolve(lfp, lp->dn_type))) { 1135 case CTF_K_FUNCTION: 1136 case CTF_K_STRUCT: 1137 case CTF_K_UNION: 1138 return (ctf_type_compat(lfp, lp->dn_type, rfp, rp->dn_type)); 1139 default: 1140 return (dt_node_is_ptrcompat(lp, rp, NULL, NULL)); 1141 } 1142 } 1143 1144 /* 1145 * We provide dt_node_is_posconst() as a convenience routine for callers who 1146 * wish to verify that an argument is a positive non-zero integer constant. 1147 */ 1148 int 1149 dt_node_is_posconst(const dt_node_t *dnp) 1150 { 1151 return (dnp->dn_kind == DT_NODE_INT && dnp->dn_value != 0 && ( 1152 (dnp->dn_flags & DT_NF_SIGNED) == 0 || (int64_t)dnp->dn_value > 0)); 1153 } 1154 1155 int 1156 dt_node_is_actfunc(const dt_node_t *dnp) 1157 { 1158 return (dnp->dn_kind == DT_NODE_FUNC && 1159 dnp->dn_ident->di_kind == DT_IDENT_ACTFUNC); 1160 } 1161 1162 /* 1163 * The original rules for integer constant typing are described in K&R[A2.5.1]. 1164 * However, since we support long long, we instead use the rules from ISO C99 1165 * clause 6.4.4.1 since that is where long longs are formally described. The 1166 * rules require us to know whether the constant was specified in decimal or 1167 * in octal or hex, which we do by looking at our lexer's 'yyintdecimal' flag. 1168 * The type of an integer constant is the first of the corresponding list in 1169 * which its value can be represented: 1170 * 1171 * unsuffixed decimal: int, long, long long 1172 * unsuffixed oct/hex: int, unsigned int, long, unsigned long, 1173 * long long, unsigned long long 1174 * suffix [uU]: unsigned int, unsigned long, unsigned long long 1175 * suffix [lL] decimal: long, long long 1176 * suffix [lL] oct/hex: long, unsigned long, long long, unsigned long long 1177 * suffix [uU][Ll]: unsigned long, unsigned long long 1178 * suffix ll/LL decimal: long long 1179 * suffix ll/LL oct/hex: long long, unsigned long long 1180 * suffix [uU][ll/LL]: unsigned long long 1181 * 1182 * Given that our lexer has already validated the suffixes by regexp matching, 1183 * there is an obvious way to concisely encode these rules: construct an array 1184 * of the types in the order int, unsigned int, long, unsigned long, long long, 1185 * unsigned long long. Compute an integer array starting index based on the 1186 * suffix (e.g. none = 0, u = 1, ull = 5), and compute an increment based on 1187 * the specifier (dec/oct/hex) and suffix (u). Then iterate from the starting 1188 * index to the end, advancing using the increment, and searching until we 1189 * find a limit that matches or we run out of choices (overflow). To make it 1190 * even faster, we precompute the table of type information in dtrace_open(). 1191 */ 1192 dt_node_t * 1193 dt_node_int(uintmax_t value) 1194 { 1195 dt_node_t *dnp = dt_node_alloc(DT_NODE_INT); 1196 dtrace_hdl_t *dtp = yypcb->pcb_hdl; 1197 1198 int n = (yyintdecimal | (yyintsuffix[0] == 'u')) + 1; 1199 int i = 0; 1200 1201 const char *p; 1202 char c; 1203 1204 dnp->dn_op = DT_TOK_INT; 1205 dnp->dn_value = value; 1206 1207 for (p = yyintsuffix; (c = *p) != '\0'; p++) { 1208 if (c == 'U' || c == 'u') 1209 i += 1; 1210 else if (c == 'L' || c == 'l') 1211 i += 2; 1212 } 1213 1214 for (; i < sizeof (dtp->dt_ints) / sizeof (dtp->dt_ints[0]); i += n) { 1215 if (value <= dtp->dt_ints[i].did_limit) { 1216 dt_node_type_assign(dnp, 1217 dtp->dt_ints[i].did_ctfp, 1218 dtp->dt_ints[i].did_type); 1219 1220 /* 1221 * If a prefix character is present in macro text, add 1222 * in the corresponding operator node (see dt_lex.l). 1223 */ 1224 switch (yyintprefix) { 1225 case '+': 1226 return (dt_node_op1(DT_TOK_IPOS, dnp)); 1227 case '-': 1228 return (dt_node_op1(DT_TOK_INEG, dnp)); 1229 default: 1230 return (dnp); 1231 } 1232 } 1233 } 1234 1235 xyerror(D_INT_OFLOW, "integer constant 0x%llx cannot be represented " 1236 "in any built-in integral type\n", (u_longlong_t)value); 1237 /*NOTREACHED*/ 1238 return (NULL); /* keep gcc happy */ 1239 } 1240 1241 dt_node_t * 1242 dt_node_string(char *string) 1243 { 1244 dtrace_hdl_t *dtp = yypcb->pcb_hdl; 1245 dt_node_t *dnp; 1246 1247 if (string == NULL) 1248 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM); 1249 1250 dnp = dt_node_alloc(DT_NODE_STRING); 1251 dnp->dn_op = DT_TOK_STRING; 1252 dnp->dn_string = string; 1253 dt_node_type_assign(dnp, DT_STR_CTFP(dtp), DT_STR_TYPE(dtp)); 1254 1255 return (dnp); 1256 } 1257 1258 dt_node_t * 1259 dt_node_ident(char *name) 1260 { 1261 dt_ident_t *idp; 1262 dt_node_t *dnp; 1263 1264 if (name == NULL) 1265 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM); 1266 1267 /* 1268 * If the identifier is an inlined integer constant, then create an INT 1269 * node that is a clone of the inline parse tree node and return that 1270 * immediately, allowing this inline to be used in parsing contexts 1271 * that require constant expressions (e.g. scalar array sizes). 1272 */ 1273 if ((idp = dt_idstack_lookup(&yypcb->pcb_globals, name)) != NULL && 1274 (idp->di_flags & DT_IDFLG_INLINE)) { 1275 dt_idnode_t *inp = idp->di_iarg; 1276 1277 if (inp->din_root != NULL && 1278 inp->din_root->dn_kind == DT_NODE_INT) { 1279 free(name); 1280 1281 dnp = dt_node_alloc(DT_NODE_INT); 1282 dnp->dn_op = DT_TOK_INT; 1283 dnp->dn_value = inp->din_root->dn_value; 1284 dt_node_type_propagate(inp->din_root, dnp); 1285 1286 return (dnp); 1287 } 1288 } 1289 1290 dnp = dt_node_alloc(DT_NODE_IDENT); 1291 dnp->dn_op = name[0] == '@' ? DT_TOK_AGG : DT_TOK_IDENT; 1292 dnp->dn_string = name; 1293 1294 return (dnp); 1295 } 1296 1297 /* 1298 * Create an empty node of type corresponding to the given declaration. 1299 * Explicit references to user types (C or D) are assigned the default 1300 * stability; references to other types are _dtrace_typattr (Private). 1301 */ 1302 dt_node_t * 1303 dt_node_type(dt_decl_t *ddp) 1304 { 1305 dtrace_hdl_t *dtp = yypcb->pcb_hdl; 1306 dtrace_typeinfo_t dtt; 1307 dt_node_t *dnp; 1308 char *name = NULL; 1309 int err; 1310 1311 /* 1312 * If 'ddp' is NULL, we get a decl by popping the decl stack. This 1313 * form of dt_node_type() is used by parameter rules in dt_grammar.y. 1314 */ 1315 if (ddp == NULL) 1316 ddp = dt_decl_pop_param(&name); 1317 1318 err = dt_decl_type(ddp, &dtt); 1319 dt_decl_free(ddp); 1320 1321 if (err != 0) { 1322 free(name); 1323 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER); 1324 } 1325 1326 dnp = dt_node_alloc(DT_NODE_TYPE); 1327 dnp->dn_op = DT_TOK_IDENT; 1328 dnp->dn_string = name; 1329 dt_node_type_assign(dnp, dtt.dtt_ctfp, dtt.dtt_type); 1330 1331 if (dtt.dtt_ctfp == dtp->dt_cdefs->dm_ctfp || 1332 dtt.dtt_ctfp == dtp->dt_ddefs->dm_ctfp) 1333 dt_node_attr_assign(dnp, _dtrace_defattr); 1334 else 1335 dt_node_attr_assign(dnp, _dtrace_typattr); 1336 1337 return (dnp); 1338 } 1339 1340 /* 1341 * Create a type node corresponding to a varargs (...) parameter by just 1342 * assigning it type CTF_ERR. The decl processing code will handle this. 1343 */ 1344 dt_node_t * 1345 dt_node_vatype(void) 1346 { 1347 dt_node_t *dnp = dt_node_alloc(DT_NODE_TYPE); 1348 1349 dnp->dn_op = DT_TOK_IDENT; 1350 dnp->dn_ctfp = yypcb->pcb_hdl->dt_cdefs->dm_ctfp; 1351 dnp->dn_type = CTF_ERR; 1352 dnp->dn_attr = _dtrace_defattr; 1353 1354 return (dnp); 1355 } 1356 1357 /* 1358 * Instantiate a decl using the contents of the current declaration stack. As 1359 * we do not currently permit decls to be initialized, this function currently 1360 * returns NULL and no parse node is created. When this function is called, 1361 * the topmost scope's ds_ident pointer will be set to NULL (indicating no 1362 * init_declarator rule was matched) or will point to the identifier to use. 1363 */ 1364 dt_node_t * 1365 dt_node_decl(void) 1366 { 1367 dtrace_hdl_t *dtp = yypcb->pcb_hdl; 1368 dt_scope_t *dsp = &yypcb->pcb_dstack; 1369 dt_dclass_t class = dsp->ds_class; 1370 dt_decl_t *ddp = dt_decl_top(); 1371 1372 dt_module_t *dmp; 1373 dtrace_typeinfo_t dtt; 1374 ctf_id_t type; 1375 1376 char n1[DT_TYPE_NAMELEN]; 1377 char n2[DT_TYPE_NAMELEN]; 1378 1379 if (dt_decl_type(ddp, &dtt) != 0) 1380 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER); 1381 1382 /* 1383 * If we have no declaration identifier, then this is either a spurious 1384 * declaration of an intrinsic type (e.g. "extern int;") or declaration 1385 * or redeclaration of a struct, union, or enum type or tag. 1386 */ 1387 if (dsp->ds_ident == NULL) { 1388 if (ddp->dd_kind != CTF_K_STRUCT && 1389 ddp->dd_kind != CTF_K_UNION && ddp->dd_kind != CTF_K_ENUM) 1390 xyerror(D_DECL_USELESS, "useless declaration\n"); 1391 1392 dt_dprintf("type %s added as id %ld\n", dt_type_name( 1393 ddp->dd_ctfp, ddp->dd_type, n1, sizeof (n1)), ddp->dd_type); 1394 1395 return (NULL); 1396 } 1397 1398 if (strchr(dsp->ds_ident, '`') != NULL) { 1399 xyerror(D_DECL_SCOPE, "D scoping operator may not be used in " 1400 "a declaration name (%s)\n", dsp->ds_ident); 1401 } 1402 1403 /* 1404 * If we are nested inside of a C include file, add the declaration to 1405 * the C definition module; otherwise use the D definition module. 1406 */ 1407 if (yypcb->pcb_idepth != 0) 1408 dmp = dtp->dt_cdefs; 1409 else 1410 dmp = dtp->dt_ddefs; 1411 1412 /* 1413 * If we see a global or static declaration of a function prototype, 1414 * treat this as equivalent to a D extern declaration. 1415 */ 1416 if (ctf_type_kind(dtt.dtt_ctfp, dtt.dtt_type) == CTF_K_FUNCTION && 1417 (class == DT_DC_DEFAULT || class == DT_DC_STATIC)) 1418 class = DT_DC_EXTERN; 1419 1420 switch (class) { 1421 case DT_DC_AUTO: 1422 case DT_DC_REGISTER: 1423 case DT_DC_STATIC: 1424 xyerror(D_DECL_BADCLASS, "specified storage class not " 1425 "appropriate in D\n"); 1426 /*NOTREACHED*/ 1427 1428 case DT_DC_EXTERN: { 1429 dtrace_typeinfo_t ott; 1430 dtrace_syminfo_t dts; 1431 GElf_Sym sym; 1432 1433 int exists = dtrace_lookup_by_name(dtp, 1434 dmp->dm_name, dsp->ds_ident, &sym, &dts) == 0; 1435 1436 if (exists && (dtrace_symbol_type(dtp, &sym, &dts, &ott) != 0 || 1437 ctf_type_cmp(dtt.dtt_ctfp, dtt.dtt_type, 1438 ott.dtt_ctfp, ott.dtt_type) != 0)) { 1439 xyerror(D_DECL_IDRED, "identifier redeclared: %s`%s\n" 1440 "\t current: %s\n\tprevious: %s\n", 1441 dmp->dm_name, dsp->ds_ident, 1442 dt_type_name(dtt.dtt_ctfp, dtt.dtt_type, 1443 n1, sizeof (n1)), 1444 dt_type_name(ott.dtt_ctfp, ott.dtt_type, 1445 n2, sizeof (n2))); 1446 } else if (!exists && dt_module_extern(dtp, dmp, 1447 dsp->ds_ident, &dtt) == NULL) { 1448 xyerror(D_UNKNOWN, 1449 "failed to extern %s: %s\n", dsp->ds_ident, 1450 dtrace_errmsg(dtp, dtrace_errno(dtp))); 1451 } else { 1452 dt_dprintf("extern %s`%s type=<%s>\n", 1453 dmp->dm_name, dsp->ds_ident, 1454 dt_type_name(dtt.dtt_ctfp, dtt.dtt_type, 1455 n1, sizeof (n1))); 1456 } 1457 break; 1458 } 1459 1460 case DT_DC_TYPEDEF: 1461 if (dt_idstack_lookup(&yypcb->pcb_globals, dsp->ds_ident)) { 1462 xyerror(D_DECL_IDRED, "global variable identifier " 1463 "redeclared: %s\n", dsp->ds_ident); 1464 } 1465 1466 if (ctf_lookup_by_name(dmp->dm_ctfp, 1467 dsp->ds_ident) != CTF_ERR) { 1468 xyerror(D_DECL_IDRED, 1469 "typedef redeclared: %s\n", dsp->ds_ident); 1470 } 1471 1472 /* 1473 * If the source type for the typedef is not defined in the 1474 * target container or its parent, copy the type to the target 1475 * container and reset dtt_ctfp and dtt_type to the copy. 1476 */ 1477 if (dtt.dtt_ctfp != dmp->dm_ctfp && 1478 dtt.dtt_ctfp != ctf_parent_file(dmp->dm_ctfp)) { 1479 1480 dtt.dtt_type = ctf_add_type(dmp->dm_ctfp, 1481 dtt.dtt_ctfp, dtt.dtt_type); 1482 dtt.dtt_ctfp = dmp->dm_ctfp; 1483 1484 if (dtt.dtt_type == CTF_ERR || 1485 ctf_update(dtt.dtt_ctfp) == CTF_ERR) { 1486 xyerror(D_UNKNOWN, "failed to copy typedef %s " 1487 "source type: %s\n", dsp->ds_ident, 1488 ctf_errmsg(ctf_errno(dtt.dtt_ctfp))); 1489 } 1490 } 1491 1492 type = ctf_add_typedef(dmp->dm_ctfp, 1493 CTF_ADD_ROOT, dsp->ds_ident, dtt.dtt_type); 1494 1495 if (type == CTF_ERR || ctf_update(dmp->dm_ctfp) == CTF_ERR) { 1496 xyerror(D_UNKNOWN, "failed to typedef %s: %s\n", 1497 dsp->ds_ident, ctf_errmsg(ctf_errno(dmp->dm_ctfp))); 1498 } 1499 1500 dt_dprintf("typedef %s added as id %ld\n", dsp->ds_ident, type); 1501 break; 1502 1503 default: { 1504 ctf_encoding_t cte; 1505 dt_idhash_t *dhp; 1506 dt_ident_t *idp; 1507 dt_node_t idn; 1508 int assc, idkind; 1509 uint_t id, kind; 1510 ushort_t idflags; 1511 1512 switch (class) { 1513 case DT_DC_THIS: 1514 dhp = yypcb->pcb_locals; 1515 idflags = DT_IDFLG_LOCAL; 1516 idp = dt_idhash_lookup(dhp, dsp->ds_ident); 1517 break; 1518 case DT_DC_SELF: 1519 dhp = dtp->dt_tls; 1520 idflags = DT_IDFLG_TLS; 1521 idp = dt_idhash_lookup(dhp, dsp->ds_ident); 1522 break; 1523 default: 1524 dhp = dtp->dt_globals; 1525 idflags = 0; 1526 idp = dt_idstack_lookup( 1527 &yypcb->pcb_globals, dsp->ds_ident); 1528 break; 1529 } 1530 1531 if (ddp->dd_kind == CTF_K_ARRAY && ddp->dd_node == NULL) { 1532 xyerror(D_DECL_ARRNULL, 1533 "array declaration requires array dimension or " 1534 "tuple signature: %s\n", dsp->ds_ident); 1535 } 1536 1537 if (idp != NULL && idp->di_gen == 0) { 1538 xyerror(D_DECL_IDRED, "built-in identifier " 1539 "redeclared: %s\n", idp->di_name); 1540 } 1541 1542 if (dtrace_lookup_by_type(dtp, DTRACE_OBJ_CDEFS, 1543 dsp->ds_ident, NULL) == 0 || 1544 dtrace_lookup_by_type(dtp, DTRACE_OBJ_DDEFS, 1545 dsp->ds_ident, NULL) == 0) { 1546 xyerror(D_DECL_IDRED, "typedef identifier " 1547 "redeclared: %s\n", dsp->ds_ident); 1548 } 1549 1550 /* 1551 * Cache some attributes of the decl to make the rest of this 1552 * code simpler: if the decl is an array which is subscripted 1553 * by a type rather than an integer, then it's an associative 1554 * array (assc). We then expect to match either DT_IDENT_ARRAY 1555 * for associative arrays or DT_IDENT_SCALAR for anything else. 1556 */ 1557 assc = ddp->dd_kind == CTF_K_ARRAY && 1558 ddp->dd_node->dn_kind == DT_NODE_TYPE; 1559 1560 idkind = assc ? DT_IDENT_ARRAY : DT_IDENT_SCALAR; 1561 1562 /* 1563 * Create a fake dt_node_t on the stack so we can determine the 1564 * type of any matching identifier by assigning to this node. 1565 * If the pre-existing ident has its di_type set, propagate 1566 * the type by hand so as not to trigger a prototype check for 1567 * arrays (yet); otherwise we use dt_ident_cook() on the ident 1568 * to ensure it is fully initialized before looking at it. 1569 */ 1570 bzero(&idn, sizeof (dt_node_t)); 1571 1572 if (idp != NULL && idp->di_type != CTF_ERR) 1573 dt_node_type_assign(&idn, idp->di_ctfp, idp->di_type); 1574 else if (idp != NULL) 1575 (void) dt_ident_cook(&idn, idp, NULL); 1576 1577 if (assc) { 1578 if (class == DT_DC_THIS) { 1579 xyerror(D_DECL_LOCASSC, "associative arrays " 1580 "may not be declared as local variables:" 1581 " %s\n", dsp->ds_ident); 1582 } 1583 1584 if (dt_decl_type(ddp->dd_next, &dtt) != 0) 1585 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER); 1586 } 1587 1588 if (idp != NULL && (idp->di_kind != idkind || 1589 ctf_type_cmp(dtt.dtt_ctfp, dtt.dtt_type, 1590 idn.dn_ctfp, idn.dn_type) != 0)) { 1591 xyerror(D_DECL_IDRED, "identifier redeclared: %s\n" 1592 "\t current: %s %s\n\tprevious: %s %s\n", 1593 dsp->ds_ident, dt_idkind_name(idkind), 1594 dt_type_name(dtt.dtt_ctfp, 1595 dtt.dtt_type, n1, sizeof (n1)), 1596 dt_idkind_name(idp->di_kind), 1597 dt_node_type_name(&idn, n2, sizeof (n2))); 1598 1599 } else if (idp != NULL && assc) { 1600 const dt_idsig_t *isp = idp->di_data; 1601 dt_node_t *dnp = ddp->dd_node; 1602 int argc = 0; 1603 1604 for (; dnp != NULL; dnp = dnp->dn_list, argc++) { 1605 const dt_node_t *pnp = &isp->dis_args[argc]; 1606 1607 if (argc >= isp->dis_argc) 1608 continue; /* tuple length mismatch */ 1609 1610 if (ctf_type_cmp(dnp->dn_ctfp, dnp->dn_type, 1611 pnp->dn_ctfp, pnp->dn_type) == 0) 1612 continue; 1613 1614 xyerror(D_DECL_IDRED, 1615 "identifier redeclared: %s\n" 1616 "\t current: %s, key #%d of type %s\n" 1617 "\tprevious: %s, key #%d of type %s\n", 1618 dsp->ds_ident, 1619 dt_idkind_name(idkind), argc + 1, 1620 dt_node_type_name(dnp, n1, sizeof (n1)), 1621 dt_idkind_name(idp->di_kind), argc + 1, 1622 dt_node_type_name(pnp, n2, sizeof (n2))); 1623 } 1624 1625 if (isp->dis_argc != argc) { 1626 xyerror(D_DECL_IDRED, 1627 "identifier redeclared: %s\n" 1628 "\t current: %s of %s, tuple length %d\n" 1629 "\tprevious: %s of %s, tuple length %d\n", 1630 dsp->ds_ident, dt_idkind_name(idkind), 1631 dt_type_name(dtt.dtt_ctfp, dtt.dtt_type, 1632 n1, sizeof (n1)), argc, 1633 dt_idkind_name(idp->di_kind), 1634 dt_node_type_name(&idn, n2, sizeof (n2)), 1635 isp->dis_argc); 1636 } 1637 1638 } else if (idp == NULL) { 1639 type = ctf_type_resolve(dtt.dtt_ctfp, dtt.dtt_type); 1640 kind = ctf_type_kind(dtt.dtt_ctfp, type); 1641 1642 switch (kind) { 1643 case CTF_K_INTEGER: 1644 if (ctf_type_encoding(dtt.dtt_ctfp, type, 1645 &cte) == 0 && IS_VOID(cte)) { 1646 xyerror(D_DECL_VOIDOBJ, "cannot have " 1647 "void object: %s\n", dsp->ds_ident); 1648 } 1649 break; 1650 case CTF_K_STRUCT: 1651 case CTF_K_UNION: 1652 if (ctf_type_size(dtt.dtt_ctfp, type) != 0) 1653 break; /* proceed to declaring */ 1654 /*FALLTHRU*/ 1655 case CTF_K_FORWARD: 1656 xyerror(D_DECL_INCOMPLETE, 1657 "incomplete struct/union/enum %s: %s\n", 1658 dt_type_name(dtt.dtt_ctfp, dtt.dtt_type, 1659 n1, sizeof (n1)), dsp->ds_ident); 1660 /*NOTREACHED*/ 1661 } 1662 1663 if (dt_idhash_nextid(dhp, &id) == -1) { 1664 xyerror(D_ID_OFLOW, "cannot create %s: limit " 1665 "on number of %s variables exceeded\n", 1666 dsp->ds_ident, dt_idhash_name(dhp)); 1667 } 1668 1669 dt_dprintf("declare %s %s variable %s, id=%u\n", 1670 dt_idhash_name(dhp), dt_idkind_name(idkind), 1671 dsp->ds_ident, id); 1672 1673 idp = dt_idhash_insert(dhp, dsp->ds_ident, idkind, 1674 idflags | DT_IDFLG_WRITE | DT_IDFLG_DECL, id, 1675 _dtrace_defattr, 0, assc ? &dt_idops_assc : 1676 &dt_idops_thaw, NULL, dtp->dt_gen); 1677 1678 if (idp == NULL) 1679 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM); 1680 1681 dt_ident_type_assign(idp, dtt.dtt_ctfp, dtt.dtt_type); 1682 1683 /* 1684 * If we are declaring an associative array, use our 1685 * fake parse node to cook the new assoc identifier. 1686 * This will force the ident code to instantiate the 1687 * array type signature corresponding to the list of 1688 * types pointed to by ddp->dd_node. We also reset 1689 * the identifier's attributes based upon the result. 1690 */ 1691 if (assc) { 1692 idp->di_attr = 1693 dt_ident_cook(&idn, idp, &ddp->dd_node); 1694 } 1695 } 1696 } 1697 1698 } /* end of switch */ 1699 1700 free(dsp->ds_ident); 1701 dsp->ds_ident = NULL; 1702 1703 return (NULL); 1704 } 1705 1706 dt_node_t * 1707 dt_node_func(dt_node_t *dnp, dt_node_t *args) 1708 { 1709 dt_ident_t *idp; 1710 1711 if (dnp->dn_kind != DT_NODE_IDENT) { 1712 xyerror(D_FUNC_IDENT, 1713 "function designator is not of function type\n"); 1714 } 1715 1716 idp = dt_idstack_lookup(&yypcb->pcb_globals, dnp->dn_string); 1717 1718 if (idp == NULL) { 1719 xyerror(D_FUNC_UNDEF, 1720 "undefined function name: %s\n", dnp->dn_string); 1721 } 1722 1723 if (idp->di_kind != DT_IDENT_FUNC && 1724 idp->di_kind != DT_IDENT_AGGFUNC && 1725 idp->di_kind != DT_IDENT_ACTFUNC) { 1726 xyerror(D_FUNC_IDKIND, "%s '%s' may not be referenced as a " 1727 "function\n", dt_idkind_name(idp->di_kind), idp->di_name); 1728 } 1729 1730 free(dnp->dn_string); 1731 dnp->dn_string = NULL; 1732 1733 dnp->dn_kind = DT_NODE_FUNC; 1734 dnp->dn_flags &= ~DT_NF_COOKED; 1735 dnp->dn_ident = idp; 1736 dnp->dn_args = args; 1737 dnp->dn_list = NULL; 1738 1739 return (dnp); 1740 } 1741 1742 /* 1743 * The offsetof() function is special because it takes a type name as an 1744 * argument. It does not actually construct its own node; after looking up the 1745 * structure or union offset, we just return an integer node with the offset. 1746 */ 1747 dt_node_t * 1748 dt_node_offsetof(dt_decl_t *ddp, char *s) 1749 { 1750 dtrace_typeinfo_t dtt; 1751 dt_node_t dn; 1752 char *name; 1753 int err; 1754 1755 ctf_membinfo_t ctm; 1756 ctf_id_t type; 1757 uint_t kind; 1758 1759 name = alloca(strlen(s) + 1); 1760 (void) strcpy(name, s); 1761 free(s); 1762 1763 err = dt_decl_type(ddp, &dtt); 1764 dt_decl_free(ddp); 1765 1766 if (err != 0) 1767 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER); 1768 1769 type = ctf_type_resolve(dtt.dtt_ctfp, dtt.dtt_type); 1770 kind = ctf_type_kind(dtt.dtt_ctfp, type); 1771 1772 if (kind != CTF_K_STRUCT && kind != CTF_K_UNION) { 1773 xyerror(D_OFFSETOF_TYPE, 1774 "offsetof operand must be a struct or union type\n"); 1775 } 1776 1777 if (ctf_member_info(dtt.dtt_ctfp, type, name, &ctm) == CTF_ERR) { 1778 xyerror(D_UNKNOWN, "failed to determine offset of %s: %s\n", 1779 name, ctf_errmsg(ctf_errno(dtt.dtt_ctfp))); 1780 } 1781 1782 bzero(&dn, sizeof (dn)); 1783 dt_node_type_assign(&dn, dtt.dtt_ctfp, ctm.ctm_type); 1784 1785 if (dn.dn_flags & DT_NF_BITFIELD) { 1786 xyerror(D_OFFSETOF_BITFIELD, 1787 "cannot take offset of a bit-field: %s\n", name); 1788 } 1789 1790 return (dt_node_int(ctm.ctm_offset / NBBY)); 1791 } 1792 1793 dt_node_t * 1794 dt_node_op1(int op, dt_node_t *cp) 1795 { 1796 dt_node_t *dnp; 1797 1798 if (cp->dn_kind == DT_NODE_INT) { 1799 switch (op) { 1800 case DT_TOK_INEG: 1801 /* 1802 * If we're negating an unsigned integer, zero out any 1803 * extra top bits to truncate the value to the size of 1804 * the effective type determined by dt_node_int(). 1805 */ 1806 cp->dn_value = -cp->dn_value; 1807 if (!(cp->dn_flags & DT_NF_SIGNED)) { 1808 cp->dn_value &= ~0ULL >> 1809 (64 - dt_node_type_size(cp) * NBBY); 1810 } 1811 /*FALLTHRU*/ 1812 case DT_TOK_IPOS: 1813 return (cp); 1814 case DT_TOK_BNEG: 1815 cp->dn_value = ~cp->dn_value; 1816 return (cp); 1817 case DT_TOK_LNEG: 1818 cp->dn_value = !cp->dn_value; 1819 return (cp); 1820 } 1821 } 1822 1823 /* 1824 * If sizeof is applied to a type_name or string constant, we can 1825 * transform 'cp' into an integer constant in the node construction 1826 * pass so that it can then be used for arithmetic in this pass. 1827 */ 1828 if (op == DT_TOK_SIZEOF && 1829 (cp->dn_kind == DT_NODE_STRING || cp->dn_kind == DT_NODE_TYPE)) { 1830 dtrace_hdl_t *dtp = yypcb->pcb_hdl; 1831 size_t size = dt_node_type_size(cp); 1832 1833 if (size == 0) { 1834 xyerror(D_SIZEOF_TYPE, "cannot apply sizeof to an " 1835 "operand of unknown size\n"); 1836 } 1837 1838 dt_node_type_assign(cp, dtp->dt_ddefs->dm_ctfp, 1839 ctf_lookup_by_name(dtp->dt_ddefs->dm_ctfp, "size_t")); 1840 1841 cp->dn_kind = DT_NODE_INT; 1842 cp->dn_op = DT_TOK_INT; 1843 cp->dn_value = size; 1844 1845 return (cp); 1846 } 1847 1848 dnp = dt_node_alloc(DT_NODE_OP1); 1849 assert(op <= USHRT_MAX); 1850 dnp->dn_op = (ushort_t)op; 1851 dnp->dn_child = cp; 1852 1853 return (dnp); 1854 } 1855 1856 dt_node_t * 1857 dt_node_op2(int op, dt_node_t *lp, dt_node_t *rp) 1858 { 1859 dtrace_hdl_t *dtp = yypcb->pcb_hdl; 1860 dt_node_t *dnp; 1861 1862 /* 1863 * First we check for operations that are illegal -- namely those that 1864 * might result in integer division by zero, and abort if one is found. 1865 */ 1866 if (rp->dn_kind == DT_NODE_INT && rp->dn_value == 0 && 1867 (op == DT_TOK_MOD || op == DT_TOK_DIV || 1868 op == DT_TOK_MOD_EQ || op == DT_TOK_DIV_EQ)) 1869 xyerror(D_DIV_ZERO, "expression contains division by zero\n"); 1870 1871 /* 1872 * If both children are immediate values, we can just perform inline 1873 * calculation and return a new immediate node with the result. 1874 */ 1875 if (lp->dn_kind == DT_NODE_INT && rp->dn_kind == DT_NODE_INT) { 1876 uintmax_t l = lp->dn_value; 1877 uintmax_t r = rp->dn_value; 1878 1879 dnp = dt_node_int(0); /* allocate new integer node for result */ 1880 1881 switch (op) { 1882 case DT_TOK_LOR: 1883 dnp->dn_value = l || r; 1884 dt_node_type_assign(dnp, 1885 DT_INT_CTFP(dtp), DT_INT_TYPE(dtp)); 1886 break; 1887 case DT_TOK_LXOR: 1888 dnp->dn_value = (l != 0) ^ (r != 0); 1889 dt_node_type_assign(dnp, 1890 DT_INT_CTFP(dtp), DT_INT_TYPE(dtp)); 1891 break; 1892 case DT_TOK_LAND: 1893 dnp->dn_value = l && r; 1894 dt_node_type_assign(dnp, 1895 DT_INT_CTFP(dtp), DT_INT_TYPE(dtp)); 1896 break; 1897 case DT_TOK_BOR: 1898 dnp->dn_value = l | r; 1899 dt_node_promote(lp, rp, dnp); 1900 break; 1901 case DT_TOK_XOR: 1902 dnp->dn_value = l ^ r; 1903 dt_node_promote(lp, rp, dnp); 1904 break; 1905 case DT_TOK_BAND: 1906 dnp->dn_value = l & r; 1907 dt_node_promote(lp, rp, dnp); 1908 break; 1909 case DT_TOK_EQU: 1910 dnp->dn_value = l == r; 1911 dt_node_type_assign(dnp, 1912 DT_INT_CTFP(dtp), DT_INT_TYPE(dtp)); 1913 break; 1914 case DT_TOK_NEQ: 1915 dnp->dn_value = l != r; 1916 dt_node_type_assign(dnp, 1917 DT_INT_CTFP(dtp), DT_INT_TYPE(dtp)); 1918 break; 1919 case DT_TOK_LT: 1920 dt_node_promote(lp, rp, dnp); 1921 if (dnp->dn_flags & DT_NF_SIGNED) 1922 dnp->dn_value = (intmax_t)l < (intmax_t)r; 1923 else 1924 dnp->dn_value = l < r; 1925 dt_node_type_assign(dnp, 1926 DT_INT_CTFP(dtp), DT_INT_TYPE(dtp)); 1927 break; 1928 case DT_TOK_LE: 1929 dt_node_promote(lp, rp, dnp); 1930 if (dnp->dn_flags & DT_NF_SIGNED) 1931 dnp->dn_value = (intmax_t)l <= (intmax_t)r; 1932 else 1933 dnp->dn_value = l <= r; 1934 dt_node_type_assign(dnp, 1935 DT_INT_CTFP(dtp), DT_INT_TYPE(dtp)); 1936 break; 1937 case DT_TOK_GT: 1938 dt_node_promote(lp, rp, dnp); 1939 if (dnp->dn_flags & DT_NF_SIGNED) 1940 dnp->dn_value = (intmax_t)l > (intmax_t)r; 1941 else 1942 dnp->dn_value = l > r; 1943 dt_node_type_assign(dnp, 1944 DT_INT_CTFP(dtp), DT_INT_TYPE(dtp)); 1945 break; 1946 case DT_TOK_GE: 1947 dt_node_promote(lp, rp, dnp); 1948 if (dnp->dn_flags & DT_NF_SIGNED) 1949 dnp->dn_value = (intmax_t)l >= (intmax_t)r; 1950 else 1951 dnp->dn_value = l >= r; 1952 dt_node_type_assign(dnp, 1953 DT_INT_CTFP(dtp), DT_INT_TYPE(dtp)); 1954 break; 1955 case DT_TOK_LSH: 1956 dnp->dn_value = l << r; 1957 dt_node_type_propagate(lp, dnp); 1958 dt_node_attr_assign(rp, 1959 dt_attr_min(lp->dn_attr, rp->dn_attr)); 1960 break; 1961 case DT_TOK_RSH: 1962 dnp->dn_value = l >> r; 1963 dt_node_type_propagate(lp, dnp); 1964 dt_node_attr_assign(rp, 1965 dt_attr_min(lp->dn_attr, rp->dn_attr)); 1966 break; 1967 case DT_TOK_ADD: 1968 dnp->dn_value = l + r; 1969 dt_node_promote(lp, rp, dnp); 1970 break; 1971 case DT_TOK_SUB: 1972 dnp->dn_value = l - r; 1973 dt_node_promote(lp, rp, dnp); 1974 break; 1975 case DT_TOK_MUL: 1976 dnp->dn_value = l * r; 1977 dt_node_promote(lp, rp, dnp); 1978 break; 1979 case DT_TOK_DIV: 1980 dt_node_promote(lp, rp, dnp); 1981 if (dnp->dn_flags & DT_NF_SIGNED) 1982 dnp->dn_value = (intmax_t)l / (intmax_t)r; 1983 else 1984 dnp->dn_value = l / r; 1985 break; 1986 case DT_TOK_MOD: 1987 dt_node_promote(lp, rp, dnp); 1988 if (dnp->dn_flags & DT_NF_SIGNED) 1989 dnp->dn_value = (intmax_t)l % (intmax_t)r; 1990 else 1991 dnp->dn_value = l % r; 1992 break; 1993 default: 1994 dt_node_free(dnp); 1995 dnp = NULL; 1996 } 1997 1998 if (dnp != NULL) { 1999 dt_node_free(lp); 2000 dt_node_free(rp); 2001 return (dnp); 2002 } 2003 } 2004 2005 /* 2006 * If an integer constant is being cast to another integer type, we can 2007 * perform the cast as part of integer constant folding in this pass. 2008 * We must take action when the integer is being cast to a smaller type 2009 * or if it is changing signed-ness. If so, we first shift rp's bits 2010 * bits high (losing excess bits if narrowing) and then shift them down 2011 * with either a logical shift (unsigned) or arithmetic shift (signed). 2012 */ 2013 if (op == DT_TOK_LPAR && rp->dn_kind == DT_NODE_INT && 2014 dt_node_is_integer(lp)) { 2015 size_t srcsize = dt_node_type_size(rp); 2016 size_t dstsize = dt_node_type_size(lp); 2017 2018 if ((dstsize < srcsize) || ((lp->dn_flags & DT_NF_SIGNED) ^ 2019 (rp->dn_flags & DT_NF_SIGNED))) { 2020 int n = dstsize < srcsize ? 2021 (sizeof (uint64_t) * NBBY - dstsize * NBBY) : 2022 (sizeof (uint64_t) * NBBY - srcsize * NBBY); 2023 2024 rp->dn_value <<= n; 2025 if (lp->dn_flags & DT_NF_SIGNED) 2026 rp->dn_value = (intmax_t)rp->dn_value >> n; 2027 else 2028 rp->dn_value = rp->dn_value >> n; 2029 } 2030 2031 dt_node_type_propagate(lp, rp); 2032 dt_node_attr_assign(rp, dt_attr_min(lp->dn_attr, rp->dn_attr)); 2033 dt_node_free(lp); 2034 2035 return (rp); 2036 } 2037 2038 /* 2039 * If no immediate optimizations are available, create an new OP2 node 2040 * and glue the left and right children into place and return. 2041 */ 2042 dnp = dt_node_alloc(DT_NODE_OP2); 2043 assert(op <= USHRT_MAX); 2044 dnp->dn_op = (ushort_t)op; 2045 dnp->dn_left = lp; 2046 dnp->dn_right = rp; 2047 2048 return (dnp); 2049 } 2050 2051 dt_node_t * 2052 dt_node_op3(dt_node_t *expr, dt_node_t *lp, dt_node_t *rp) 2053 { 2054 dt_node_t *dnp; 2055 2056 if (expr->dn_kind == DT_NODE_INT) 2057 return (expr->dn_value != 0 ? lp : rp); 2058 2059 dnp = dt_node_alloc(DT_NODE_OP3); 2060 dnp->dn_op = DT_TOK_QUESTION; 2061 dnp->dn_expr = expr; 2062 dnp->dn_left = lp; 2063 dnp->dn_right = rp; 2064 2065 return (dnp); 2066 } 2067 2068 dt_node_t * 2069 dt_node_statement(dt_node_t *expr) 2070 { 2071 dt_node_t *dnp; 2072 2073 if (expr->dn_kind == DT_NODE_AGG) 2074 return (expr); 2075 2076 if (expr->dn_kind == DT_NODE_FUNC && 2077 expr->dn_ident->di_kind == DT_IDENT_ACTFUNC) 2078 dnp = dt_node_alloc(DT_NODE_DFUNC); 2079 else 2080 dnp = dt_node_alloc(DT_NODE_DEXPR); 2081 2082 dnp->dn_expr = expr; 2083 return (dnp); 2084 } 2085 2086 dt_node_t * 2087 dt_node_pdesc_by_name(char *spec) 2088 { 2089 dtrace_hdl_t *dtp = yypcb->pcb_hdl; 2090 dt_node_t *dnp; 2091 2092 if (spec == NULL) 2093 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM); 2094 2095 dnp = dt_node_alloc(DT_NODE_PDESC); 2096 dnp->dn_spec = spec; 2097 dnp->dn_desc = malloc(sizeof (dtrace_probedesc_t)); 2098 2099 if (dnp->dn_desc == NULL) 2100 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM); 2101 2102 if (dtrace_xstr2desc(dtp, yypcb->pcb_pspec, dnp->dn_spec, 2103 yypcb->pcb_sargc, yypcb->pcb_sargv, dnp->dn_desc) != 0) { 2104 xyerror(D_PDESC_INVAL, "invalid probe description \"%s\": %s\n", 2105 dnp->dn_spec, dtrace_errmsg(dtp, dtrace_errno(dtp))); 2106 } 2107 2108 free(dnp->dn_spec); 2109 dnp->dn_spec = NULL; 2110 2111 return (dnp); 2112 } 2113 2114 dt_node_t * 2115 dt_node_pdesc_by_id(uintmax_t id) 2116 { 2117 static const char *const names[] = { 2118 "providers", "modules", "functions" 2119 }; 2120 2121 dtrace_hdl_t *dtp = yypcb->pcb_hdl; 2122 dt_node_t *dnp = dt_node_alloc(DT_NODE_PDESC); 2123 2124 if ((dnp->dn_desc = malloc(sizeof (dtrace_probedesc_t))) == NULL) 2125 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM); 2126 2127 if (id > UINT_MAX) { 2128 xyerror(D_PDESC_INVAL, "identifier %llu exceeds maximum " 2129 "probe id\n", (u_longlong_t)id); 2130 } 2131 2132 if (yypcb->pcb_pspec != DTRACE_PROBESPEC_NAME) { 2133 xyerror(D_PDESC_INVAL, "probe identifier %llu not permitted " 2134 "when specifying %s\n", (u_longlong_t)id, 2135 names[yypcb->pcb_pspec]); 2136 } 2137 2138 if (dtrace_id2desc(dtp, (dtrace_id_t)id, dnp->dn_desc) != 0) { 2139 xyerror(D_PDESC_INVAL, "invalid probe identifier %llu: %s\n", 2140 (u_longlong_t)id, dtrace_errmsg(dtp, dtrace_errno(dtp))); 2141 } 2142 2143 return (dnp); 2144 } 2145 2146 dt_node_t * 2147 dt_node_clause(dt_node_t *pdescs, dt_node_t *pred, dt_node_t *acts) 2148 { 2149 dt_node_t *dnp = dt_node_alloc(DT_NODE_CLAUSE); 2150 2151 dnp->dn_pdescs = pdescs; 2152 dnp->dn_pred = pred; 2153 dnp->dn_acts = acts; 2154 2155 yybegin(YYS_CLAUSE); 2156 return (dnp); 2157 } 2158 2159 dt_node_t * 2160 dt_node_inline(dt_node_t *expr) 2161 { 2162 dtrace_hdl_t *dtp = yypcb->pcb_hdl; 2163 dt_scope_t *dsp = &yypcb->pcb_dstack; 2164 dt_decl_t *ddp = dt_decl_top(); 2165 2166 char n[DT_TYPE_NAMELEN]; 2167 dtrace_typeinfo_t dtt; 2168 2169 dt_ident_t *idp, *rdp; 2170 dt_idnode_t *inp; 2171 dt_node_t *dnp; 2172 2173 if (dt_decl_type(ddp, &dtt) != 0) 2174 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER); 2175 2176 if (dsp->ds_class != DT_DC_DEFAULT) { 2177 xyerror(D_DECL_BADCLASS, "specified storage class not " 2178 "appropriate for inline declaration\n"); 2179 } 2180 2181 if (dsp->ds_ident == NULL) 2182 xyerror(D_DECL_USELESS, "inline declaration requires a name\n"); 2183 2184 if ((idp = dt_idstack_lookup( 2185 &yypcb->pcb_globals, dsp->ds_ident)) != NULL) { 2186 xyerror(D_DECL_IDRED, "identifier redefined: %s\n\t current: " 2187 "inline definition\n\tprevious: %s %s\n", 2188 idp->di_name, dt_idkind_name(idp->di_kind), 2189 (idp->di_flags & DT_IDFLG_INLINE) ? "inline" : ""); 2190 } 2191 2192 /* 2193 * If we are declaring an inlined array, verify that we have a tuple 2194 * signature, and then recompute 'dtt' as the array's value type. 2195 */ 2196 if (ddp->dd_kind == CTF_K_ARRAY) { 2197 if (ddp->dd_node == NULL) { 2198 xyerror(D_DECL_ARRNULL, "inline declaration requires " 2199 "array tuple signature: %s\n", dsp->ds_ident); 2200 } 2201 2202 if (ddp->dd_node->dn_kind != DT_NODE_TYPE) { 2203 xyerror(D_DECL_ARRNULL, "inline declaration cannot be " 2204 "of scalar array type: %s\n", dsp->ds_ident); 2205 } 2206 2207 if (dt_decl_type(ddp->dd_next, &dtt) != 0) 2208 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER); 2209 } 2210 2211 /* 2212 * If the inline identifier is not defined, then create it with the 2213 * orphan flag set. We do not insert the identifier into dt_globals 2214 * until we have successfully cooked the right-hand expression, below. 2215 */ 2216 dnp = dt_node_alloc(DT_NODE_INLINE); 2217 dt_node_type_assign(dnp, dtt.dtt_ctfp, dtt.dtt_type); 2218 dt_node_attr_assign(dnp, _dtrace_defattr); 2219 2220 if (dt_node_is_void(dnp)) { 2221 xyerror(D_DECL_VOIDOBJ, 2222 "cannot declare void inline: %s\n", dsp->ds_ident); 2223 } 2224 2225 if (ctf_type_kind(dnp->dn_ctfp, ctf_type_resolve( 2226 dnp->dn_ctfp, dnp->dn_type)) == CTF_K_FORWARD) { 2227 xyerror(D_DECL_INCOMPLETE, 2228 "incomplete struct/union/enum %s: %s\n", 2229 dt_node_type_name(dnp, n, sizeof (n)), dsp->ds_ident); 2230 } 2231 2232 if ((inp = malloc(sizeof (dt_idnode_t))) == NULL) 2233 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM); 2234 2235 bzero(inp, sizeof (dt_idnode_t)); 2236 2237 idp = dnp->dn_ident = dt_ident_create(dsp->ds_ident, 2238 ddp->dd_kind == CTF_K_ARRAY ? DT_IDENT_ARRAY : DT_IDENT_SCALAR, 2239 DT_IDFLG_INLINE | DT_IDFLG_REF | DT_IDFLG_DECL | DT_IDFLG_ORPHAN, 0, 2240 _dtrace_defattr, 0, &dt_idops_inline, inp, dtp->dt_gen); 2241 2242 if (idp == NULL) { 2243 free(inp); 2244 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM); 2245 } 2246 2247 /* 2248 * If we're inlining an associative array, create a private identifier 2249 * hash containing the named parameters and store it in inp->din_hash. 2250 * We then push this hash on to the top of the pcb_globals stack. 2251 */ 2252 if (ddp->dd_kind == CTF_K_ARRAY) { 2253 dt_idnode_t *pinp; 2254 dt_ident_t *pidp; 2255 dt_node_t *pnp; 2256 uint_t i = 0; 2257 2258 for (pnp = ddp->dd_node; pnp != NULL; pnp = pnp->dn_list) 2259 i++; /* count up parameters for din_argv[] */ 2260 2261 inp->din_hash = dt_idhash_create("inline args", NULL, 0, 0); 2262 inp->din_argv = calloc(i, sizeof (dt_ident_t *)); 2263 2264 if (inp->din_hash == NULL || inp->din_argv == NULL) 2265 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM); 2266 2267 /* 2268 * Create an identifier for each parameter as a scalar inline, 2269 * and store it in din_hash and in position in din_argv[]. The 2270 * parameter identifiers also use dt_idops_inline, but we leave 2271 * the dt_idnode_t argument 'pinp' zeroed. This will be filled 2272 * in by the code generation pass with references to the args. 2273 */ 2274 for (i = 0, pnp = ddp->dd_node; 2275 pnp != NULL; pnp = pnp->dn_list, i++) { 2276 2277 if (pnp->dn_string == NULL) 2278 continue; /* ignore anonymous parameters */ 2279 2280 if ((pinp = malloc(sizeof (dt_idnode_t))) == NULL) 2281 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM); 2282 2283 pidp = dt_idhash_insert(inp->din_hash, pnp->dn_string, 2284 DT_IDENT_SCALAR, DT_IDFLG_DECL | DT_IDFLG_INLINE, 0, 2285 _dtrace_defattr, 0, &dt_idops_inline, 2286 pinp, dtp->dt_gen); 2287 2288 if (pidp == NULL) { 2289 free(pinp); 2290 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM); 2291 } 2292 2293 inp->din_argv[i] = pidp; 2294 bzero(pinp, sizeof (dt_idnode_t)); 2295 dt_ident_type_assign(pidp, pnp->dn_ctfp, pnp->dn_type); 2296 } 2297 2298 dt_idstack_push(&yypcb->pcb_globals, inp->din_hash); 2299 } 2300 2301 /* 2302 * Unlike most constructors, we need to explicitly cook the right-hand 2303 * side of the inline definition immediately to prevent recursion. If 2304 * the right-hand side uses the inline itself, the cook will fail. 2305 */ 2306 expr = dt_node_cook(expr, DT_IDFLG_REF); 2307 2308 if (ddp->dd_kind == CTF_K_ARRAY) 2309 dt_idstack_pop(&yypcb->pcb_globals, inp->din_hash); 2310 2311 /* 2312 * Set the type, attributes, and flags for the inline. If the right- 2313 * hand expression has an identifier, propagate its flags. Then cook 2314 * the identifier to fully initialize it: if we're declaring an inline 2315 * associative array this will construct a type signature from 'ddp'. 2316 */ 2317 if (dt_node_is_dynamic(expr)) 2318 rdp = dt_ident_resolve(expr->dn_ident); 2319 else if (expr->dn_kind == DT_NODE_VAR || expr->dn_kind == DT_NODE_SYM) 2320 rdp = expr->dn_ident; 2321 else 2322 rdp = NULL; 2323 2324 if (rdp != NULL) { 2325 idp->di_flags |= (rdp->di_flags & 2326 (DT_IDFLG_WRITE | DT_IDFLG_USER | DT_IDFLG_PRIM)); 2327 } 2328 2329 idp->di_attr = dt_attr_min(_dtrace_defattr, expr->dn_attr); 2330 dt_ident_type_assign(idp, dtt.dtt_ctfp, dtt.dtt_type); 2331 (void) dt_ident_cook(dnp, idp, &ddp->dd_node); 2332 2333 /* 2334 * Store the parse tree nodes for 'expr' inside of idp->di_data ('inp') 2335 * so that they will be preserved with this identifier. Then pop the 2336 * inline declaration from the declaration stack and restore the lexer. 2337 */ 2338 inp->din_list = yypcb->pcb_list; 2339 inp->din_root = expr; 2340 2341 dt_decl_free(dt_decl_pop()); 2342 yybegin(YYS_CLAUSE); 2343 2344 /* 2345 * Finally, insert the inline identifier into dt_globals to make it 2346 * visible, and then cook 'dnp' to check its type against 'expr'. 2347 */ 2348 dt_idhash_xinsert(dtp->dt_globals, idp); 2349 return (dt_node_cook(dnp, DT_IDFLG_REF)); 2350 } 2351 2352 dt_node_t * 2353 dt_node_member(dt_decl_t *ddp, char *name, dt_node_t *expr) 2354 { 2355 dtrace_typeinfo_t dtt; 2356 dt_node_t *dnp; 2357 int err; 2358 2359 if (ddp != NULL) { 2360 err = dt_decl_type(ddp, &dtt); 2361 dt_decl_free(ddp); 2362 2363 if (err != 0) 2364 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER); 2365 } 2366 2367 dnp = dt_node_alloc(DT_NODE_MEMBER); 2368 dnp->dn_membname = name; 2369 dnp->dn_membexpr = expr; 2370 2371 if (ddp != NULL) 2372 dt_node_type_assign(dnp, dtt.dtt_ctfp, dtt.dtt_type); 2373 2374 return (dnp); 2375 } 2376 2377 dt_node_t * 2378 dt_node_xlator(dt_decl_t *ddp, dt_decl_t *sdp, char *name, dt_node_t *members) 2379 { 2380 dtrace_hdl_t *dtp = yypcb->pcb_hdl; 2381 dtrace_typeinfo_t src, dst; 2382 dt_node_t sn, dn; 2383 dt_xlator_t *dxp; 2384 dt_node_t *dnp; 2385 int edst, esrc; 2386 uint_t kind; 2387 2388 char n1[DT_TYPE_NAMELEN]; 2389 char n2[DT_TYPE_NAMELEN]; 2390 2391 edst = dt_decl_type(ddp, &dst); 2392 dt_decl_free(ddp); 2393 2394 esrc = dt_decl_type(sdp, &src); 2395 dt_decl_free(sdp); 2396 2397 if (edst != 0 || esrc != 0) { 2398 free(name); 2399 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER); 2400 } 2401 2402 bzero(&sn, sizeof (sn)); 2403 dt_node_type_assign(&sn, src.dtt_ctfp, src.dtt_type); 2404 2405 bzero(&dn, sizeof (dn)); 2406 dt_node_type_assign(&dn, dst.dtt_ctfp, dst.dtt_type); 2407 2408 if (dt_xlator_lookup(dtp, &sn, &dn, DT_XLATE_EXACT) != NULL) { 2409 xyerror(D_XLATE_REDECL, 2410 "translator from %s to %s has already been declared\n", 2411 dt_node_type_name(&sn, n1, sizeof (n1)), 2412 dt_node_type_name(&dn, n2, sizeof (n2))); 2413 } 2414 2415 kind = ctf_type_kind(dst.dtt_ctfp, 2416 ctf_type_resolve(dst.dtt_ctfp, dst.dtt_type)); 2417 2418 if (kind == CTF_K_FORWARD) { 2419 xyerror(D_XLATE_SOU, "incomplete struct/union/enum %s\n", 2420 dt_type_name(dst.dtt_ctfp, dst.dtt_type, n1, sizeof (n1))); 2421 } 2422 2423 if (kind != CTF_K_STRUCT && kind != CTF_K_UNION) { 2424 xyerror(D_XLATE_SOU, 2425 "translator output type must be a struct or union\n"); 2426 } 2427 2428 dxp = dt_xlator_create(dtp, &src, &dst, name, members, yypcb->pcb_list); 2429 yybegin(YYS_CLAUSE); 2430 free(name); 2431 2432 if (dxp == NULL) 2433 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM); 2434 2435 dnp = dt_node_alloc(DT_NODE_XLATOR); 2436 dnp->dn_xlator = dxp; 2437 dnp->dn_members = members; 2438 2439 return (dt_node_cook(dnp, DT_IDFLG_REF)); 2440 } 2441 2442 dt_node_t * 2443 dt_node_probe(char *s, int protoc, dt_node_t *nargs, dt_node_t *xargs) 2444 { 2445 dtrace_hdl_t *dtp = yypcb->pcb_hdl; 2446 int nargc, xargc; 2447 dt_node_t *dnp; 2448 2449 size_t len = strlen(s) + 3; /* +3 for :: and \0 */ 2450 char *name = alloca(len); 2451 2452 (void) snprintf(name, len, "::%s", s); 2453 (void) strhyphenate(name); 2454 free(s); 2455 2456 if (strchr(name, '`') != NULL) { 2457 xyerror(D_PROV_BADNAME, "probe name may not " 2458 "contain scoping operator: %s\n", name); 2459 } 2460 2461 if (strlen(name) - 2 >= DTRACE_NAMELEN) { 2462 xyerror(D_PROV_BADNAME, "probe name may not exceed %d " 2463 "characters: %s\n", DTRACE_NAMELEN - 1, name); 2464 } 2465 2466 dnp = dt_node_alloc(DT_NODE_PROBE); 2467 2468 dnp->dn_ident = dt_ident_create(name, DT_IDENT_PROBE, 2469 DT_IDFLG_ORPHAN, DTRACE_IDNONE, _dtrace_defattr, 0, 2470 &dt_idops_probe, NULL, dtp->dt_gen); 2471 2472 nargc = dt_decl_prototype(nargs, nargs, 2473 "probe input", DT_DP_VOID | DT_DP_ANON); 2474 2475 xargc = dt_decl_prototype(xargs, nargs, 2476 "probe output", DT_DP_VOID); 2477 2478 if (nargc > UINT8_MAX) { 2479 xyerror(D_PROV_PRARGLEN, "probe %s input prototype exceeds %u " 2480 "parameters: %d params used\n", name, UINT8_MAX, nargc); 2481 } 2482 2483 if (xargc > UINT8_MAX) { 2484 xyerror(D_PROV_PRARGLEN, "probe %s output prototype exceeds %u " 2485 "parameters: %d params used\n", name, UINT8_MAX, xargc); 2486 } 2487 2488 if (dnp->dn_ident == NULL || dt_probe_create(dtp, 2489 dnp->dn_ident, protoc, nargs, nargc, xargs, xargc) == NULL) 2490 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM); 2491 2492 return (dnp); 2493 } 2494 2495 dt_node_t * 2496 dt_node_provider(char *name, dt_node_t *probes) 2497 { 2498 dtrace_hdl_t *dtp = yypcb->pcb_hdl; 2499 dt_node_t *dnp = dt_node_alloc(DT_NODE_PROVIDER); 2500 dt_node_t *lnp; 2501 2502 dnp->dn_provname = name; 2503 dnp->dn_probes = probes; 2504 2505 if (strchr(name, '`') != NULL) { 2506 dnerror(dnp, D_PROV_BADNAME, "provider name may not " 2507 "contain scoping operator: %s\n", name); 2508 } 2509 2510 if (strlen(name) >= DTRACE_PROVNAMELEN) { 2511 dnerror(dnp, D_PROV_BADNAME, "provider name may not exceed %d " 2512 "characters: %s\n", DTRACE_PROVNAMELEN - 1, name); 2513 } 2514 2515 /* 2516 * Check to see if the provider is already defined or visible through 2517 * dtrace(7D). If so, set dn_provred to treat it as a re-declaration. 2518 * If not, create a new provider and set its interface-only flag. This 2519 * flag may be cleared later by calls made to dt_probe_declare(). 2520 */ 2521 if ((dnp->dn_provider = dt_provider_lookup(dtp, name)) != NULL) 2522 dnp->dn_provred = B_TRUE; 2523 else if ((dnp->dn_provider = dt_provider_create(dtp, name)) == NULL) 2524 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM); 2525 else 2526 dnp->dn_provider->pv_flags |= DT_PROVIDER_INTF; 2527 2528 /* 2529 * Store all parse nodes created since we consumed the DT_KEY_PROVIDER 2530 * token with the provider and then restore our lexing state to CLAUSE. 2531 * Note that if dnp->dn_provred is true, we may end up storing dups of 2532 * a provider's interface and implementation: we eat this space because 2533 * the implementation will likely need to redeclare probe members, and 2534 * therefore may result in those member nodes becoming persistent. 2535 */ 2536 for (lnp = yypcb->pcb_list; lnp->dn_link != NULL; lnp = lnp->dn_link) 2537 continue; /* skip to end of allocation list */ 2538 2539 lnp->dn_link = dnp->dn_provider->pv_nodes; 2540 dnp->dn_provider->pv_nodes = yypcb->pcb_list; 2541 2542 yybegin(YYS_CLAUSE); 2543 return (dnp); 2544 } 2545 2546 dt_node_t * 2547 dt_node_program(dt_node_t *lnp) 2548 { 2549 dt_node_t *dnp = dt_node_alloc(DT_NODE_PROG); 2550 dnp->dn_list = lnp; 2551 return (dnp); 2552 } 2553 2554 /* 2555 * This function provides the underlying implementation of cooking an 2556 * identifier given its node, a hash of dynamic identifiers, an identifier 2557 * kind, and a boolean flag indicating whether we are allowed to instantiate 2558 * a new identifier if the string is not found. This function is either 2559 * called from dt_cook_ident(), below, or directly by the various cooking 2560 * routines that are allowed to instantiate identifiers (e.g. op2 TOK_ASGN). 2561 */ 2562 static void 2563 dt_xcook_ident(dt_node_t *dnp, dt_idhash_t *dhp, uint_t idkind, int create) 2564 { 2565 dtrace_hdl_t *dtp = yypcb->pcb_hdl; 2566 const char *sname = dt_idhash_name(dhp); 2567 int uref = 0; 2568 2569 dtrace_attribute_t attr = _dtrace_defattr; 2570 dt_ident_t *idp; 2571 dtrace_syminfo_t dts; 2572 GElf_Sym sym; 2573 2574 const char *scope, *mark; 2575 uchar_t dnkind; 2576 char *name; 2577 2578 /* 2579 * Look for scoping marks in the identifier. If one is found, set our 2580 * scope to either DTRACE_OBJ_KMODS or UMODS or to the first part of 2581 * the string that specifies the scope using an explicit module name. 2582 * If two marks in a row are found, set 'uref' (user symbol reference). 2583 * Otherwise we set scope to DTRACE_OBJ_EXEC, indicating that normal 2584 * scope is desired and we should search the specified idhash. 2585 */ 2586 if ((name = strrchr(dnp->dn_string, '`')) != NULL) { 2587 if (name > dnp->dn_string && name[-1] == '`') { 2588 uref++; 2589 name[-1] = '\0'; 2590 } 2591 2592 if (name == dnp->dn_string + uref) 2593 scope = uref ? DTRACE_OBJ_UMODS : DTRACE_OBJ_KMODS; 2594 else 2595 scope = dnp->dn_string; 2596 2597 *name++ = '\0'; /* leave name pointing after scoping mark */ 2598 dnkind = DT_NODE_VAR; 2599 2600 } else if (idkind == DT_IDENT_AGG) { 2601 scope = DTRACE_OBJ_EXEC; 2602 name = dnp->dn_string + 1; 2603 dnkind = DT_NODE_AGG; 2604 } else { 2605 scope = DTRACE_OBJ_EXEC; 2606 name = dnp->dn_string; 2607 dnkind = DT_NODE_VAR; 2608 } 2609 2610 /* 2611 * If create is set to false, and we fail our idhash lookup, preset 2612 * the errno code to EDT_NOVAR for our final error message below. 2613 * If we end up calling dtrace_lookup_by_name(), it will reset the 2614 * errno appropriately and that error will be reported instead. 2615 */ 2616 (void) dt_set_errno(dtp, EDT_NOVAR); 2617 mark = uref ? "``" : "`"; 2618 2619 if (scope == DTRACE_OBJ_EXEC && ( 2620 (dhp != dtp->dt_globals && 2621 (idp = dt_idhash_lookup(dhp, name)) != NULL) || 2622 (dhp == dtp->dt_globals && 2623 (idp = dt_idstack_lookup(&yypcb->pcb_globals, name)) != NULL))) { 2624 /* 2625 * Check that we are referencing the ident in the manner that 2626 * matches its type if this is a global lookup. In the TLS or 2627 * local case, we don't know how the ident will be used until 2628 * the time operator -> is seen; more parsing is needed. 2629 */ 2630 if (idp->di_kind != idkind && dhp == dtp->dt_globals) { 2631 xyerror(D_IDENT_BADREF, "%s '%s' may not be referenced " 2632 "as %s\n", dt_idkind_name(idp->di_kind), 2633 idp->di_name, dt_idkind_name(idkind)); 2634 } 2635 2636 /* 2637 * Arrays and aggregations are not cooked individually. They 2638 * have dynamic types and must be referenced using operator []. 2639 * This is handled explicitly by the code for DT_TOK_LBRAC. 2640 */ 2641 if (idp->di_kind != DT_IDENT_ARRAY && 2642 idp->di_kind != DT_IDENT_AGG) 2643 attr = dt_ident_cook(dnp, idp, NULL); 2644 else { 2645 dt_node_type_assign(dnp, 2646 DT_DYN_CTFP(dtp), DT_DYN_TYPE(dtp)); 2647 attr = idp->di_attr; 2648 } 2649 2650 free(dnp->dn_string); 2651 dnp->dn_string = NULL; 2652 dnp->dn_kind = dnkind; 2653 dnp->dn_ident = idp; 2654 dnp->dn_flags |= DT_NF_LVALUE; 2655 2656 if (idp->di_flags & DT_IDFLG_WRITE) 2657 dnp->dn_flags |= DT_NF_WRITABLE; 2658 2659 dt_node_attr_assign(dnp, attr); 2660 2661 } else if (dhp == dtp->dt_globals && scope != DTRACE_OBJ_EXEC && 2662 dtrace_lookup_by_name(dtp, scope, name, &sym, &dts) == 0) { 2663 2664 dt_module_t *mp = dt_module_lookup_by_name(dtp, dts.dts_object); 2665 int umod = (mp->dm_flags & DT_DM_KERNEL) == 0; 2666 static const char *const kunames[] = { "kernel", "user" }; 2667 2668 dtrace_typeinfo_t dtt; 2669 dtrace_syminfo_t *sip; 2670 2671 if (uref ^ umod) { 2672 xyerror(D_SYM_BADREF, "%s module '%s' symbol '%s' may " 2673 "not be referenced as a %s symbol\n", kunames[umod], 2674 dts.dts_object, dts.dts_name, kunames[uref]); 2675 } 2676 2677 if (dtrace_symbol_type(dtp, &sym, &dts, &dtt) != 0) { 2678 /* 2679 * For now, we special-case EDT_DATAMODEL to clarify 2680 * that mixed data models are not currently supported. 2681 */ 2682 if (dtp->dt_errno == EDT_DATAMODEL) { 2683 xyerror(D_SYM_MODEL, "cannot use %s symbol " 2684 "%s%s%s in a %s D program\n", 2685 dt_module_modelname(mp), 2686 dts.dts_object, mark, dts.dts_name, 2687 dt_module_modelname(dtp->dt_ddefs)); 2688 } 2689 2690 xyerror(D_SYM_NOTYPES, 2691 "no symbolic type information is available for " 2692 "%s%s%s: %s\n", dts.dts_object, mark, dts.dts_name, 2693 dtrace_errmsg(dtp, dtrace_errno(dtp))); 2694 } 2695 2696 idp = dt_ident_create(name, DT_IDENT_SYMBOL, 0, 0, 2697 _dtrace_symattr, 0, &dt_idops_thaw, NULL, dtp->dt_gen); 2698 2699 if (idp == NULL) 2700 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM); 2701 2702 if (mp->dm_flags & DT_DM_PRIMARY) 2703 idp->di_flags |= DT_IDFLG_PRIM; 2704 2705 idp->di_next = dtp->dt_externs; 2706 dtp->dt_externs = idp; 2707 2708 if ((sip = malloc(sizeof (dtrace_syminfo_t))) == NULL) 2709 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM); 2710 2711 bcopy(&dts, sip, sizeof (dtrace_syminfo_t)); 2712 idp->di_data = sip; 2713 idp->di_ctfp = dtt.dtt_ctfp; 2714 idp->di_type = dtt.dtt_type; 2715 2716 free(dnp->dn_string); 2717 dnp->dn_string = NULL; 2718 dnp->dn_kind = DT_NODE_SYM; 2719 dnp->dn_ident = idp; 2720 dnp->dn_flags |= DT_NF_LVALUE; 2721 2722 dt_node_type_assign(dnp, dtt.dtt_ctfp, dtt.dtt_type); 2723 dt_node_attr_assign(dnp, _dtrace_symattr); 2724 2725 if (uref) { 2726 idp->di_flags |= DT_IDFLG_USER; 2727 dnp->dn_flags |= DT_NF_USERLAND; 2728 } 2729 2730 } else if (scope == DTRACE_OBJ_EXEC && create == B_TRUE) { 2731 uint_t flags = DT_IDFLG_WRITE; 2732 uint_t id; 2733 2734 if (dt_idhash_nextid(dhp, &id) == -1) { 2735 xyerror(D_ID_OFLOW, "cannot create %s: limit on number " 2736 "of %s variables exceeded\n", name, sname); 2737 } 2738 2739 if (dhp == yypcb->pcb_locals) 2740 flags |= DT_IDFLG_LOCAL; 2741 else if (dhp == dtp->dt_tls) 2742 flags |= DT_IDFLG_TLS; 2743 2744 dt_dprintf("create %s %s variable %s, id=%u\n", 2745 sname, dt_idkind_name(idkind), name, id); 2746 2747 if (idkind == DT_IDENT_ARRAY || idkind == DT_IDENT_AGG) { 2748 idp = dt_idhash_insert(dhp, name, 2749 idkind, flags, id, _dtrace_defattr, 0, 2750 &dt_idops_assc, NULL, dtp->dt_gen); 2751 } else { 2752 idp = dt_idhash_insert(dhp, name, 2753 idkind, flags, id, _dtrace_defattr, 0, 2754 &dt_idops_thaw, NULL, dtp->dt_gen); 2755 } 2756 2757 if (idp == NULL) 2758 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM); 2759 2760 /* 2761 * Arrays and aggregations are not cooked individually. They 2762 * have dynamic types and must be referenced using operator []. 2763 * This is handled explicitly by the code for DT_TOK_LBRAC. 2764 */ 2765 if (idp->di_kind != DT_IDENT_ARRAY && 2766 idp->di_kind != DT_IDENT_AGG) 2767 attr = dt_ident_cook(dnp, idp, NULL); 2768 else { 2769 dt_node_type_assign(dnp, 2770 DT_DYN_CTFP(dtp), DT_DYN_TYPE(dtp)); 2771 attr = idp->di_attr; 2772 } 2773 2774 free(dnp->dn_string); 2775 dnp->dn_string = NULL; 2776 dnp->dn_kind = dnkind; 2777 dnp->dn_ident = idp; 2778 dnp->dn_flags |= DT_NF_LVALUE | DT_NF_WRITABLE; 2779 2780 dt_node_attr_assign(dnp, attr); 2781 2782 } else if (scope != DTRACE_OBJ_EXEC) { 2783 xyerror(D_IDENT_UNDEF, "failed to resolve %s%s%s: %s\n", 2784 dnp->dn_string, mark, name, 2785 dtrace_errmsg(dtp, dtrace_errno(dtp))); 2786 } else { 2787 xyerror(D_IDENT_UNDEF, "failed to resolve %s: %s\n", 2788 dnp->dn_string, dtrace_errmsg(dtp, dtrace_errno(dtp))); 2789 } 2790 } 2791 2792 static dt_node_t * 2793 dt_cook_ident(dt_node_t *dnp, uint_t idflags) 2794 { 2795 dtrace_hdl_t *dtp = yypcb->pcb_hdl; 2796 2797 if (dnp->dn_op == DT_TOK_AGG) 2798 dt_xcook_ident(dnp, dtp->dt_aggs, DT_IDENT_AGG, B_FALSE); 2799 else 2800 dt_xcook_ident(dnp, dtp->dt_globals, DT_IDENT_SCALAR, B_FALSE); 2801 2802 return (dt_node_cook(dnp, idflags)); 2803 } 2804 2805 /* 2806 * Since operators [ and -> can instantiate new variables before we know 2807 * whether the reference is for a read or a write, we need to check read 2808 * references to determine if the identifier is currently dt_ident_unref(). 2809 * If so, we report that this first access was to an undefined variable. 2810 */ 2811 static dt_node_t * 2812 dt_cook_var(dt_node_t *dnp, uint_t idflags) 2813 { 2814 dt_ident_t *idp = dnp->dn_ident; 2815 2816 if ((idflags & DT_IDFLG_REF) && dt_ident_unref(idp)) { 2817 dnerror(dnp, D_VAR_UNDEF, 2818 "%s%s has not yet been declared or assigned\n", 2819 (idp->di_flags & DT_IDFLG_LOCAL) ? "this->" : 2820 (idp->di_flags & DT_IDFLG_TLS) ? "self->" : "", 2821 idp->di_name); 2822 } 2823 2824 dt_node_attr_assign(dnp, dt_ident_cook(dnp, idp, &dnp->dn_args)); 2825 return (dnp); 2826 } 2827 2828 /*ARGSUSED*/ 2829 static dt_node_t * 2830 dt_cook_func(dt_node_t *dnp, uint_t idflags) 2831 { 2832 dt_node_attr_assign(dnp, 2833 dt_ident_cook(dnp, dnp->dn_ident, &dnp->dn_args)); 2834 2835 return (dnp); 2836 } 2837 2838 static dt_node_t * 2839 dt_cook_op1(dt_node_t *dnp, uint_t idflags) 2840 { 2841 dtrace_hdl_t *dtp = yypcb->pcb_hdl; 2842 dt_node_t *cp = dnp->dn_child; 2843 2844 char n[DT_TYPE_NAMELEN]; 2845 dtrace_typeinfo_t dtt; 2846 dt_ident_t *idp; 2847 2848 ctf_encoding_t e; 2849 ctf_arinfo_t r; 2850 ctf_id_t type, base; 2851 uint_t kind; 2852 2853 if (dnp->dn_op == DT_TOK_PREINC || dnp->dn_op == DT_TOK_POSTINC || 2854 dnp->dn_op == DT_TOK_PREDEC || dnp->dn_op == DT_TOK_POSTDEC) 2855 idflags = DT_IDFLG_REF | DT_IDFLG_MOD; 2856 else 2857 idflags = DT_IDFLG_REF; 2858 2859 /* 2860 * We allow the unary ++ and -- operators to instantiate new scalar 2861 * variables if applied to an identifier; otherwise just cook as usual. 2862 */ 2863 if (cp->dn_kind == DT_NODE_IDENT && (idflags & DT_IDFLG_MOD)) 2864 dt_xcook_ident(cp, dtp->dt_globals, DT_IDENT_SCALAR, B_TRUE); 2865 2866 cp = dnp->dn_child = dt_node_cook(cp, 0); /* don't set idflags yet */ 2867 2868 if (cp->dn_kind == DT_NODE_VAR && dt_ident_unref(cp->dn_ident)) { 2869 if (dt_type_lookup("int64_t", &dtt) != 0) 2870 xyerror(D_TYPE_ERR, "failed to lookup int64_t\n"); 2871 2872 dt_ident_type_assign(cp->dn_ident, dtt.dtt_ctfp, dtt.dtt_type); 2873 dt_node_type_assign(cp, dtt.dtt_ctfp, dtt.dtt_type); 2874 } 2875 2876 if (cp->dn_kind == DT_NODE_VAR) 2877 cp->dn_ident->di_flags |= idflags; 2878 2879 switch (dnp->dn_op) { 2880 case DT_TOK_DEREF: 2881 /* 2882 * If the deref operator is applied to a translated pointer, 2883 * we can just set our output type to the base translation. 2884 */ 2885 if ((idp = dt_node_resolve(cp, DT_IDENT_XLPTR)) != NULL) { 2886 dt_xlator_t *dxp = idp->di_data; 2887 2888 dnp->dn_ident = &dxp->dx_souid; 2889 dt_node_type_assign(dnp, 2890 DT_DYN_CTFP(dtp), DT_DYN_TYPE(dtp)); 2891 break; 2892 } 2893 2894 type = ctf_type_resolve(cp->dn_ctfp, cp->dn_type); 2895 kind = ctf_type_kind(cp->dn_ctfp, type); 2896 2897 if (kind == CTF_K_ARRAY) { 2898 if (ctf_array_info(cp->dn_ctfp, type, &r) != 0) { 2899 dtp->dt_ctferr = ctf_errno(cp->dn_ctfp); 2900 longjmp(yypcb->pcb_jmpbuf, EDT_CTF); 2901 } else 2902 type = r.ctr_contents; 2903 } else if (kind == CTF_K_POINTER) { 2904 type = ctf_type_reference(cp->dn_ctfp, type); 2905 } else { 2906 xyerror(D_DEREF_NONPTR, 2907 "cannot dereference non-pointer type\n"); 2908 } 2909 2910 dt_node_type_assign(dnp, cp->dn_ctfp, type); 2911 base = ctf_type_resolve(cp->dn_ctfp, type); 2912 kind = ctf_type_kind(cp->dn_ctfp, base); 2913 2914 if (kind == CTF_K_INTEGER && ctf_type_encoding(cp->dn_ctfp, 2915 base, &e) == 0 && IS_VOID(e)) { 2916 xyerror(D_DEREF_VOID, 2917 "cannot dereference pointer to void\n"); 2918 } 2919 2920 if (kind == CTF_K_FUNCTION) { 2921 xyerror(D_DEREF_FUNC, 2922 "cannot dereference pointer to function\n"); 2923 } 2924 2925 if (kind != CTF_K_ARRAY || dt_node_is_string(dnp)) 2926 dnp->dn_flags |= DT_NF_LVALUE; /* see K&R[A7.4.3] */ 2927 2928 /* 2929 * If we propagated the l-value bit and the child operand was 2930 * a writable D variable or a binary operation of the form 2931 * a + b where a is writable, then propagate the writable bit. 2932 * This is necessary to permit assignments to scalar arrays, 2933 * which are converted to expressions of the form *(a + i). 2934 */ 2935 if ((cp->dn_flags & DT_NF_WRITABLE) || 2936 (cp->dn_kind == DT_NODE_OP2 && cp->dn_op == DT_TOK_ADD && 2937 (cp->dn_left->dn_flags & DT_NF_WRITABLE))) 2938 dnp->dn_flags |= DT_NF_WRITABLE; 2939 2940 if ((cp->dn_flags & DT_NF_USERLAND) && 2941 (kind == CTF_K_POINTER || (dnp->dn_flags & DT_NF_REF))) 2942 dnp->dn_flags |= DT_NF_USERLAND; 2943 break; 2944 2945 case DT_TOK_IPOS: 2946 case DT_TOK_INEG: 2947 if (!dt_node_is_arith(cp)) { 2948 xyerror(D_OP_ARITH, "operator %s requires an operand " 2949 "of arithmetic type\n", opstr(dnp->dn_op)); 2950 } 2951 dt_node_type_propagate(cp, dnp); /* see K&R[A7.4.4-6] */ 2952 break; 2953 2954 case DT_TOK_BNEG: 2955 if (!dt_node_is_integer(cp)) { 2956 xyerror(D_OP_INT, "operator %s requires an operand of " 2957 "integral type\n", opstr(dnp->dn_op)); 2958 } 2959 dt_node_type_propagate(cp, dnp); /* see K&R[A7.4.4-6] */ 2960 break; 2961 2962 case DT_TOK_LNEG: 2963 if (!dt_node_is_scalar(cp)) { 2964 xyerror(D_OP_SCALAR, "operator %s requires an operand " 2965 "of scalar type\n", opstr(dnp->dn_op)); 2966 } 2967 dt_node_type_assign(dnp, DT_INT_CTFP(dtp), DT_INT_TYPE(dtp)); 2968 break; 2969 2970 case DT_TOK_ADDROF: 2971 if (cp->dn_kind == DT_NODE_VAR || cp->dn_kind == DT_NODE_AGG) { 2972 xyerror(D_ADDROF_VAR, 2973 "cannot take address of dynamic variable\n"); 2974 } 2975 2976 if (dt_node_is_dynamic(cp)) { 2977 xyerror(D_ADDROF_VAR, 2978 "cannot take address of dynamic object\n"); 2979 } 2980 2981 if (!(cp->dn_flags & DT_NF_LVALUE)) { 2982 xyerror(D_ADDROF_LVAL, /* see K&R[A7.4.2] */ 2983 "unacceptable operand for unary & operator\n"); 2984 } 2985 2986 if (cp->dn_flags & DT_NF_BITFIELD) { 2987 xyerror(D_ADDROF_BITFIELD, 2988 "cannot take address of bit-field\n"); 2989 } 2990 2991 dtt.dtt_object = NULL; 2992 dtt.dtt_ctfp = cp->dn_ctfp; 2993 dtt.dtt_type = cp->dn_type; 2994 2995 if (dt_type_pointer(&dtt) == -1) { 2996 xyerror(D_TYPE_ERR, "cannot find type for \"&\": %s*\n", 2997 dt_node_type_name(cp, n, sizeof (n))); 2998 } 2999 3000 dt_node_type_assign(dnp, dtt.dtt_ctfp, dtt.dtt_type); 3001 3002 if (cp->dn_flags & DT_NF_USERLAND) 3003 dnp->dn_flags |= DT_NF_USERLAND; 3004 break; 3005 3006 case DT_TOK_SIZEOF: 3007 if (cp->dn_flags & DT_NF_BITFIELD) { 3008 xyerror(D_SIZEOF_BITFIELD, 3009 "cannot apply sizeof to a bit-field\n"); 3010 } 3011 3012 if (dt_node_sizeof(cp) == 0) { 3013 xyerror(D_SIZEOF_TYPE, "cannot apply sizeof to an " 3014 "operand of unknown size\n"); 3015 } 3016 3017 dt_node_type_assign(dnp, dtp->dt_ddefs->dm_ctfp, 3018 ctf_lookup_by_name(dtp->dt_ddefs->dm_ctfp, "size_t")); 3019 break; 3020 3021 case DT_TOK_STRINGOF: 3022 if (!dt_node_is_scalar(cp) && !dt_node_is_pointer(cp) && 3023 !dt_node_is_strcompat(cp)) { 3024 xyerror(D_STRINGOF_TYPE, 3025 "cannot apply stringof to a value of type %s\n", 3026 dt_node_type_name(cp, n, sizeof (n))); 3027 } 3028 dt_node_type_assign(dnp, DT_STR_CTFP(dtp), DT_STR_TYPE(dtp)); 3029 break; 3030 3031 case DT_TOK_PREINC: 3032 case DT_TOK_POSTINC: 3033 case DT_TOK_PREDEC: 3034 case DT_TOK_POSTDEC: 3035 if (dt_node_is_scalar(cp) == 0) { 3036 xyerror(D_OP_SCALAR, "operator %s requires operand of " 3037 "scalar type\n", opstr(dnp->dn_op)); 3038 } 3039 3040 if (dt_node_is_vfptr(cp)) { 3041 xyerror(D_OP_VFPTR, "operator %s requires an operand " 3042 "of known size\n", opstr(dnp->dn_op)); 3043 } 3044 3045 if (!(cp->dn_flags & DT_NF_LVALUE)) { 3046 xyerror(D_OP_LVAL, "operator %s requires modifiable " 3047 "lvalue as an operand\n", opstr(dnp->dn_op)); 3048 } 3049 3050 if (!(cp->dn_flags & DT_NF_WRITABLE)) { 3051 xyerror(D_OP_WRITE, "operator %s can only be applied " 3052 "to a writable variable\n", opstr(dnp->dn_op)); 3053 } 3054 3055 dt_node_type_propagate(cp, dnp); /* see K&R[A7.4.1] */ 3056 break; 3057 3058 default: 3059 xyerror(D_UNKNOWN, "invalid unary op %s\n", opstr(dnp->dn_op)); 3060 } 3061 3062 dt_node_attr_assign(dnp, cp->dn_attr); 3063 return (dnp); 3064 } 3065 3066 static dt_node_t * 3067 dt_cook_op2(dt_node_t *dnp, uint_t idflags) 3068 { 3069 dtrace_hdl_t *dtp = yypcb->pcb_hdl; 3070 dt_node_t *lp = dnp->dn_left; 3071 dt_node_t *rp = dnp->dn_right; 3072 int op = dnp->dn_op; 3073 3074 ctf_membinfo_t m; 3075 ctf_file_t *ctfp; 3076 ctf_id_t type; 3077 int kind, val, uref; 3078 dt_ident_t *idp; 3079 3080 char n1[DT_TYPE_NAMELEN]; 3081 char n2[DT_TYPE_NAMELEN]; 3082 3083 /* 3084 * The expression E1[E2] is identical by definition to *((E1)+(E2)) so 3085 * we convert "[" to "+" and glue on "*" at the end (see K&R[A7.3.1]) 3086 * unless the left-hand side is an untyped D scalar, associative array, 3087 * or aggregation. In these cases, we proceed to case DT_TOK_LBRAC and 3088 * handle associative array and aggregation references there. 3089 */ 3090 if (op == DT_TOK_LBRAC) { 3091 if (lp->dn_kind == DT_NODE_IDENT) { 3092 dt_idhash_t *dhp; 3093 uint_t idkind; 3094 3095 if (lp->dn_op == DT_TOK_AGG) { 3096 dhp = dtp->dt_aggs; 3097 idp = dt_idhash_lookup(dhp, lp->dn_string + 1); 3098 idkind = DT_IDENT_AGG; 3099 } else { 3100 dhp = dtp->dt_globals; 3101 idp = dt_idstack_lookup( 3102 &yypcb->pcb_globals, lp->dn_string); 3103 idkind = DT_IDENT_ARRAY; 3104 } 3105 3106 if (idp == NULL || dt_ident_unref(idp)) 3107 dt_xcook_ident(lp, dhp, idkind, B_TRUE); 3108 else 3109 dt_xcook_ident(lp, dhp, idp->di_kind, B_FALSE); 3110 } else 3111 lp = dnp->dn_left = dt_node_cook(lp, 0); 3112 3113 /* 3114 * Switch op to '+' for *(E1 + E2) array mode in these cases: 3115 * (a) lp is a DT_IDENT_ARRAY variable that has already been 3116 * referenced using [] notation (dn_args != NULL). 3117 * (b) lp is a non-ARRAY variable that has already been given 3118 * a type by assignment or declaration (!dt_ident_unref()) 3119 * (c) lp is neither a variable nor an aggregation 3120 */ 3121 if (lp->dn_kind == DT_NODE_VAR) { 3122 if (lp->dn_ident->di_kind == DT_IDENT_ARRAY) { 3123 if (lp->dn_args != NULL) 3124 op = DT_TOK_ADD; 3125 } else if (!dt_ident_unref(lp->dn_ident)) 3126 op = DT_TOK_ADD; 3127 } else if (lp->dn_kind != DT_NODE_AGG) 3128 op = DT_TOK_ADD; 3129 } 3130 3131 switch (op) { 3132 case DT_TOK_BAND: 3133 case DT_TOK_XOR: 3134 case DT_TOK_BOR: 3135 lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF); 3136 rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF); 3137 3138 if (!dt_node_is_integer(lp) || !dt_node_is_integer(rp)) { 3139 xyerror(D_OP_INT, "operator %s requires operands of " 3140 "integral type\n", opstr(op)); 3141 } 3142 3143 dt_node_promote(lp, rp, dnp); /* see K&R[A7.11-13] */ 3144 break; 3145 3146 case DT_TOK_LSH: 3147 case DT_TOK_RSH: 3148 lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF); 3149 rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF); 3150 3151 if (!dt_node_is_integer(lp) || !dt_node_is_integer(rp)) { 3152 xyerror(D_OP_INT, "operator %s requires operands of " 3153 "integral type\n", opstr(op)); 3154 } 3155 3156 dt_node_type_propagate(lp, dnp); /* see K&R[A7.8] */ 3157 dt_node_attr_assign(dnp, dt_attr_min(lp->dn_attr, rp->dn_attr)); 3158 break; 3159 3160 case DT_TOK_MOD: 3161 lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF); 3162 rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF); 3163 3164 if (!dt_node_is_integer(lp) || !dt_node_is_integer(rp)) { 3165 xyerror(D_OP_INT, "operator %s requires operands of " 3166 "integral type\n", opstr(op)); 3167 } 3168 3169 dt_node_promote(lp, rp, dnp); /* see K&R[A7.6] */ 3170 break; 3171 3172 case DT_TOK_MUL: 3173 case DT_TOK_DIV: 3174 lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF); 3175 rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF); 3176 3177 if (!dt_node_is_arith(lp) || !dt_node_is_arith(rp)) { 3178 xyerror(D_OP_ARITH, "operator %s requires operands of " 3179 "arithmetic type\n", opstr(op)); 3180 } 3181 3182 dt_node_promote(lp, rp, dnp); /* see K&R[A7.6] */ 3183 break; 3184 3185 case DT_TOK_LAND: 3186 case DT_TOK_LXOR: 3187 case DT_TOK_LOR: 3188 lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF); 3189 rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF); 3190 3191 if (!dt_node_is_scalar(lp) || !dt_node_is_scalar(rp)) { 3192 xyerror(D_OP_SCALAR, "operator %s requires operands " 3193 "of scalar type\n", opstr(op)); 3194 } 3195 3196 dt_node_type_assign(dnp, DT_INT_CTFP(dtp), DT_INT_TYPE(dtp)); 3197 dt_node_attr_assign(dnp, dt_attr_min(lp->dn_attr, rp->dn_attr)); 3198 break; 3199 3200 case DT_TOK_LT: 3201 case DT_TOK_LE: 3202 case DT_TOK_GT: 3203 case DT_TOK_GE: 3204 case DT_TOK_EQU: 3205 case DT_TOK_NEQ: 3206 /* 3207 * The D comparison operators provide the ability to transform 3208 * a right-hand identifier into a corresponding enum tag value 3209 * if the left-hand side is an enum type. To do this, we cook 3210 * the left-hand side, and then see if the right-hand side is 3211 * an unscoped identifier defined in the enum. If so, we 3212 * convert into an integer constant node with the tag's value. 3213 */ 3214 lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF); 3215 3216 kind = ctf_type_kind(lp->dn_ctfp, 3217 ctf_type_resolve(lp->dn_ctfp, lp->dn_type)); 3218 3219 if (kind == CTF_K_ENUM && rp->dn_kind == DT_NODE_IDENT && 3220 strchr(rp->dn_string, '`') == NULL && ctf_enum_value( 3221 lp->dn_ctfp, lp->dn_type, rp->dn_string, &val) == 0) { 3222 3223 if ((idp = dt_idstack_lookup(&yypcb->pcb_globals, 3224 rp->dn_string)) != NULL) { 3225 xyerror(D_IDENT_AMBIG, 3226 "ambiguous use of operator %s: %s is " 3227 "both a %s enum tag and a global %s\n", 3228 opstr(op), rp->dn_string, 3229 dt_node_type_name(lp, n1, sizeof (n1)), 3230 dt_idkind_name(idp->di_kind)); 3231 } 3232 3233 free(rp->dn_string); 3234 rp->dn_string = NULL; 3235 rp->dn_kind = DT_NODE_INT; 3236 rp->dn_flags |= DT_NF_COOKED; 3237 rp->dn_op = DT_TOK_INT; 3238 rp->dn_value = (intmax_t)val; 3239 3240 dt_node_type_assign(rp, lp->dn_ctfp, lp->dn_type); 3241 dt_node_attr_assign(rp, _dtrace_symattr); 3242 } 3243 3244 rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF); 3245 3246 /* 3247 * The rules for type checking for the relational operators are 3248 * described in the ANSI-C spec (see K&R[A7.9-10]). We perform 3249 * the various tests in order from least to most expensive. We 3250 * also allow derived strings to be compared as a first-class 3251 * type (resulting in a strcmp(3C)-style comparison), and we 3252 * slightly relax the A7.9 rules to permit void pointer 3253 * comparisons as in A7.10. Our users won't be confused by 3254 * this since they understand pointers are just numbers, and 3255 * relaxing this constraint simplifies the implementation. 3256 */ 3257 if (ctf_type_compat(lp->dn_ctfp, lp->dn_type, 3258 rp->dn_ctfp, rp->dn_type)) 3259 /*EMPTY*/; 3260 else if (dt_node_is_integer(lp) && dt_node_is_integer(rp)) 3261 /*EMPTY*/; 3262 else if (dt_node_is_strcompat(lp) && dt_node_is_strcompat(rp) && 3263 (dt_node_is_string(lp) || dt_node_is_string(rp))) 3264 /*EMPTY*/; 3265 else if (dt_node_is_ptrcompat(lp, rp, NULL, NULL) == 0) { 3266 xyerror(D_OP_INCOMPAT, "operands have " 3267 "incompatible types: \"%s\" %s \"%s\"\n", 3268 dt_node_type_name(lp, n1, sizeof (n1)), opstr(op), 3269 dt_node_type_name(rp, n2, sizeof (n2))); 3270 } 3271 3272 dt_node_type_assign(dnp, DT_INT_CTFP(dtp), DT_INT_TYPE(dtp)); 3273 dt_node_attr_assign(dnp, dt_attr_min(lp->dn_attr, rp->dn_attr)); 3274 break; 3275 3276 case DT_TOK_ADD: 3277 case DT_TOK_SUB: { 3278 /* 3279 * The rules for type checking for the additive operators are 3280 * described in the ANSI-C spec (see K&R[A7.7]). Pointers and 3281 * integers may be manipulated according to specific rules. In 3282 * these cases D permits strings to be treated as pointers. 3283 */ 3284 int lp_is_ptr, lp_is_int, rp_is_ptr, rp_is_int; 3285 3286 lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF); 3287 rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF); 3288 3289 lp_is_ptr = dt_node_is_string(lp) || 3290 (dt_node_is_pointer(lp) && !dt_node_is_vfptr(lp)); 3291 lp_is_int = dt_node_is_integer(lp); 3292 3293 rp_is_ptr = dt_node_is_string(rp) || 3294 (dt_node_is_pointer(rp) && !dt_node_is_vfptr(rp)); 3295 rp_is_int = dt_node_is_integer(rp); 3296 3297 if (lp_is_int && rp_is_int) { 3298 dt_type_promote(lp, rp, &ctfp, &type); 3299 uref = 0; 3300 } else if (lp_is_ptr && rp_is_int) { 3301 ctfp = lp->dn_ctfp; 3302 type = lp->dn_type; 3303 uref = lp->dn_flags & DT_NF_USERLAND; 3304 } else if (lp_is_int && rp_is_ptr && op == DT_TOK_ADD) { 3305 ctfp = rp->dn_ctfp; 3306 type = rp->dn_type; 3307 uref = rp->dn_flags & DT_NF_USERLAND; 3308 } else if (lp_is_ptr && rp_is_ptr && op == DT_TOK_SUB && 3309 dt_node_is_ptrcompat(lp, rp, NULL, NULL)) { 3310 ctfp = dtp->dt_ddefs->dm_ctfp; 3311 type = ctf_lookup_by_name(ctfp, "ptrdiff_t"); 3312 uref = 0; 3313 } else { 3314 xyerror(D_OP_INCOMPAT, "operands have incompatible " 3315 "types: \"%s\" %s \"%s\"\n", 3316 dt_node_type_name(lp, n1, sizeof (n1)), opstr(op), 3317 dt_node_type_name(rp, n2, sizeof (n2))); 3318 } 3319 3320 dt_node_type_assign(dnp, ctfp, type); 3321 dt_node_attr_assign(dnp, dt_attr_min(lp->dn_attr, rp->dn_attr)); 3322 3323 if (uref) 3324 dnp->dn_flags |= DT_NF_USERLAND; 3325 break; 3326 } 3327 3328 case DT_TOK_OR_EQ: 3329 case DT_TOK_XOR_EQ: 3330 case DT_TOK_AND_EQ: 3331 case DT_TOK_LSH_EQ: 3332 case DT_TOK_RSH_EQ: 3333 case DT_TOK_MOD_EQ: 3334 if (lp->dn_kind == DT_NODE_IDENT) { 3335 dt_xcook_ident(lp, dtp->dt_globals, 3336 DT_IDENT_SCALAR, B_TRUE); 3337 } 3338 3339 lp = dnp->dn_left = 3340 dt_node_cook(lp, DT_IDFLG_REF | DT_IDFLG_MOD); 3341 3342 rp = dnp->dn_right = 3343 dt_node_cook(rp, DT_IDFLG_REF | DT_IDFLG_MOD); 3344 3345 if (!dt_node_is_integer(lp) || !dt_node_is_integer(rp)) { 3346 xyerror(D_OP_INT, "operator %s requires operands of " 3347 "integral type\n", opstr(op)); 3348 } 3349 goto asgn_common; 3350 3351 case DT_TOK_MUL_EQ: 3352 case DT_TOK_DIV_EQ: 3353 if (lp->dn_kind == DT_NODE_IDENT) { 3354 dt_xcook_ident(lp, dtp->dt_globals, 3355 DT_IDENT_SCALAR, B_TRUE); 3356 } 3357 3358 lp = dnp->dn_left = 3359 dt_node_cook(lp, DT_IDFLG_REF | DT_IDFLG_MOD); 3360 3361 rp = dnp->dn_right = 3362 dt_node_cook(rp, DT_IDFLG_REF | DT_IDFLG_MOD); 3363 3364 if (!dt_node_is_arith(lp) || !dt_node_is_arith(rp)) { 3365 xyerror(D_OP_ARITH, "operator %s requires operands of " 3366 "arithmetic type\n", opstr(op)); 3367 } 3368 goto asgn_common; 3369 3370 case DT_TOK_ASGN: 3371 /* 3372 * If the left-hand side is an identifier, attempt to resolve 3373 * it as either an aggregation or scalar variable. We pass 3374 * B_TRUE to dt_xcook_ident to indicate that a new variable can 3375 * be created if no matching variable exists in the namespace. 3376 */ 3377 if (lp->dn_kind == DT_NODE_IDENT) { 3378 if (lp->dn_op == DT_TOK_AGG) { 3379 dt_xcook_ident(lp, dtp->dt_aggs, 3380 DT_IDENT_AGG, B_TRUE); 3381 } else { 3382 dt_xcook_ident(lp, dtp->dt_globals, 3383 DT_IDENT_SCALAR, B_TRUE); 3384 } 3385 } 3386 3387 lp = dnp->dn_left = dt_node_cook(lp, 0); /* don't set mod yet */ 3388 rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF); 3389 3390 /* 3391 * If the left-hand side is an aggregation, verify that we are 3392 * assigning it the result of an aggregating function. Once 3393 * we've done so, hide the func node in the aggregation and 3394 * return the aggregation itself up to the parse tree parent. 3395 * This transformation is legal since the assigned function 3396 * cannot change identity across disjoint cooking passes and 3397 * the argument list subtree is retained for later cooking. 3398 */ 3399 if (lp->dn_kind == DT_NODE_AGG) { 3400 const char *aname = lp->dn_ident->di_name; 3401 dt_ident_t *oid = lp->dn_ident->di_iarg; 3402 3403 if (rp->dn_kind != DT_NODE_FUNC || 3404 rp->dn_ident->di_kind != DT_IDENT_AGGFUNC) { 3405 xyerror(D_AGG_FUNC, 3406 "@%s must be assigned the result of " 3407 "an aggregating function\n", aname); 3408 } 3409 3410 if (oid != NULL && oid != rp->dn_ident) { 3411 xyerror(D_AGG_REDEF, 3412 "aggregation redefined: @%s\n\t " 3413 "current: @%s = %s( )\n\tprevious: @%s = " 3414 "%s( ) : line %d\n", aname, aname, 3415 rp->dn_ident->di_name, aname, oid->di_name, 3416 lp->dn_ident->di_lineno); 3417 } else if (oid == NULL) 3418 lp->dn_ident->di_iarg = rp->dn_ident; 3419 3420 /* 3421 * Do not allow multiple aggregation assignments in a 3422 * single statement, e.g. (@a = count()) = count(); 3423 * We produce a message as if the result of aggregating 3424 * function does not propagate DT_NF_LVALUE. 3425 */ 3426 if (lp->dn_aggfun != NULL) { 3427 xyerror(D_OP_LVAL, "operator = requires " 3428 "modifiable lvalue as an operand\n"); 3429 } 3430 3431 lp->dn_aggfun = rp; 3432 lp = dt_node_cook(lp, DT_IDFLG_MOD); 3433 3434 dnp->dn_left = dnp->dn_right = NULL; 3435 dt_node_free(dnp); 3436 3437 return (lp); 3438 } 3439 3440 /* 3441 * If the right-hand side is a dynamic variable that is the 3442 * output of a translator, our result is the translated type. 3443 */ 3444 if ((idp = dt_node_resolve(rp, DT_IDENT_XLSOU)) != NULL) { 3445 ctfp = idp->di_ctfp; 3446 type = idp->di_type; 3447 uref = idp->di_flags & DT_IDFLG_USER; 3448 } else { 3449 ctfp = rp->dn_ctfp; 3450 type = rp->dn_type; 3451 uref = rp->dn_flags & DT_NF_USERLAND; 3452 } 3453 3454 /* 3455 * If the left-hand side of an assignment statement is a virgin 3456 * variable created by this compilation pass, reset the type of 3457 * this variable to the type of the right-hand side. 3458 */ 3459 if (lp->dn_kind == DT_NODE_VAR && 3460 dt_ident_unref(lp->dn_ident)) { 3461 dt_node_type_assign(lp, ctfp, type); 3462 dt_ident_type_assign(lp->dn_ident, ctfp, type); 3463 3464 if (uref) { 3465 lp->dn_flags |= DT_NF_USERLAND; 3466 lp->dn_ident->di_flags |= DT_IDFLG_USER; 3467 } 3468 } 3469 3470 if (lp->dn_kind == DT_NODE_VAR) 3471 lp->dn_ident->di_flags |= DT_IDFLG_MOD; 3472 3473 /* 3474 * The rules for type checking for the assignment operators are 3475 * described in the ANSI-C spec (see K&R[A7.17]). We share 3476 * most of this code with the argument list checking code. 3477 */ 3478 if (!dt_node_is_string(lp)) { 3479 kind = ctf_type_kind(lp->dn_ctfp, 3480 ctf_type_resolve(lp->dn_ctfp, lp->dn_type)); 3481 3482 if (kind == CTF_K_ARRAY || kind == CTF_K_FUNCTION) { 3483 xyerror(D_OP_ARRFUN, "operator %s may not be " 3484 "applied to operand of type \"%s\"\n", 3485 opstr(op), 3486 dt_node_type_name(lp, n1, sizeof (n1))); 3487 } 3488 } 3489 3490 if (idp != NULL && idp->di_kind == DT_IDENT_XLSOU && 3491 ctf_type_compat(lp->dn_ctfp, lp->dn_type, ctfp, type)) 3492 goto asgn_common; 3493 3494 if (dt_node_is_argcompat(lp, rp)) 3495 goto asgn_common; 3496 3497 xyerror(D_OP_INCOMPAT, 3498 "operands have incompatible types: \"%s\" %s \"%s\"\n", 3499 dt_node_type_name(lp, n1, sizeof (n1)), opstr(op), 3500 dt_node_type_name(rp, n2, sizeof (n2))); 3501 /*NOTREACHED*/ 3502 3503 case DT_TOK_ADD_EQ: 3504 case DT_TOK_SUB_EQ: 3505 if (lp->dn_kind == DT_NODE_IDENT) { 3506 dt_xcook_ident(lp, dtp->dt_globals, 3507 DT_IDENT_SCALAR, B_TRUE); 3508 } 3509 3510 lp = dnp->dn_left = 3511 dt_node_cook(lp, DT_IDFLG_REF | DT_IDFLG_MOD); 3512 3513 rp = dnp->dn_right = 3514 dt_node_cook(rp, DT_IDFLG_REF | DT_IDFLG_MOD); 3515 3516 if (dt_node_is_string(lp) || dt_node_is_string(rp)) { 3517 xyerror(D_OP_INCOMPAT, "operands have " 3518 "incompatible types: \"%s\" %s \"%s\"\n", 3519 dt_node_type_name(lp, n1, sizeof (n1)), opstr(op), 3520 dt_node_type_name(rp, n2, sizeof (n2))); 3521 } 3522 3523 /* 3524 * The rules for type checking for the assignment operators are 3525 * described in the ANSI-C spec (see K&R[A7.17]). To these 3526 * rules we add that only writable D nodes can be modified. 3527 */ 3528 if (dt_node_is_integer(lp) == 0 || 3529 dt_node_is_integer(rp) == 0) { 3530 if (!dt_node_is_pointer(lp) || dt_node_is_vfptr(lp)) { 3531 xyerror(D_OP_VFPTR, 3532 "operator %s requires left-hand scalar " 3533 "operand of known size\n", opstr(op)); 3534 } else if (dt_node_is_integer(rp) == 0 && 3535 dt_node_is_ptrcompat(lp, rp, NULL, NULL) == 0) { 3536 xyerror(D_OP_INCOMPAT, "operands have " 3537 "incompatible types: \"%s\" %s \"%s\"\n", 3538 dt_node_type_name(lp, n1, sizeof (n1)), 3539 opstr(op), 3540 dt_node_type_name(rp, n2, sizeof (n2))); 3541 } 3542 } 3543 asgn_common: 3544 if (!(lp->dn_flags & DT_NF_LVALUE)) { 3545 xyerror(D_OP_LVAL, "operator %s requires modifiable " 3546 "lvalue as an operand\n", opstr(op)); 3547 /* see K&R[A7.17] */ 3548 } 3549 3550 if (!(lp->dn_flags & DT_NF_WRITABLE)) { 3551 xyerror(D_OP_WRITE, "operator %s can only be applied " 3552 "to a writable variable\n", opstr(op)); 3553 } 3554 3555 dt_node_type_propagate(lp, dnp); /* see K&R[A7.17] */ 3556 dt_node_attr_assign(dnp, dt_attr_min(lp->dn_attr, rp->dn_attr)); 3557 break; 3558 3559 case DT_TOK_PTR: 3560 /* 3561 * If the left-hand side of operator -> is the name "self", 3562 * then we permit a TLS variable to be created or referenced. 3563 */ 3564 if (lp->dn_kind == DT_NODE_IDENT && 3565 strcmp(lp->dn_string, "self") == 0) { 3566 if (rp->dn_kind != DT_NODE_VAR) { 3567 dt_xcook_ident(rp, dtp->dt_tls, 3568 DT_IDENT_SCALAR, B_TRUE); 3569 } 3570 3571 if (idflags != 0) 3572 rp = dt_node_cook(rp, idflags); 3573 3574 dnp->dn_right = dnp->dn_left; /* avoid freeing rp */ 3575 dt_node_free(dnp); 3576 return (rp); 3577 } 3578 3579 /* 3580 * If the left-hand side of operator -> is the name "this", 3581 * then we permit a local variable to be created or referenced. 3582 */ 3583 if (lp->dn_kind == DT_NODE_IDENT && 3584 strcmp(lp->dn_string, "this") == 0) { 3585 if (rp->dn_kind != DT_NODE_VAR) { 3586 dt_xcook_ident(rp, yypcb->pcb_locals, 3587 DT_IDENT_SCALAR, B_TRUE); 3588 } 3589 3590 if (idflags != 0) 3591 rp = dt_node_cook(rp, idflags); 3592 3593 dnp->dn_right = dnp->dn_left; /* avoid freeing rp */ 3594 dt_node_free(dnp); 3595 return (rp); 3596 } 3597 3598 /*FALLTHRU*/ 3599 3600 case DT_TOK_DOT: 3601 lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF); 3602 3603 if (rp->dn_kind != DT_NODE_IDENT) { 3604 xyerror(D_OP_IDENT, "operator %s must be followed by " 3605 "an identifier\n", opstr(op)); 3606 } 3607 3608 if ((idp = dt_node_resolve(lp, DT_IDENT_XLSOU)) != NULL || 3609 (idp = dt_node_resolve(lp, DT_IDENT_XLPTR)) != NULL) { 3610 /* 3611 * If the left-hand side is a translated struct or ptr, 3612 * the type of the left is the translation output type. 3613 */ 3614 dt_xlator_t *dxp = idp->di_data; 3615 3616 if (dt_xlator_member(dxp, rp->dn_string) == NULL) { 3617 xyerror(D_XLATE_NOCONV, 3618 "translator does not define conversion " 3619 "for member: %s\n", rp->dn_string); 3620 } 3621 3622 ctfp = idp->di_ctfp; 3623 type = ctf_type_resolve(ctfp, idp->di_type); 3624 uref = idp->di_flags & DT_IDFLG_USER; 3625 } else { 3626 ctfp = lp->dn_ctfp; 3627 type = ctf_type_resolve(ctfp, lp->dn_type); 3628 uref = lp->dn_flags & DT_NF_USERLAND; 3629 } 3630 3631 kind = ctf_type_kind(ctfp, type); 3632 3633 if (op == DT_TOK_PTR) { 3634 if (kind != CTF_K_POINTER) { 3635 xyerror(D_OP_PTR, "operator %s must be " 3636 "applied to a pointer\n", opstr(op)); 3637 } 3638 type = ctf_type_reference(ctfp, type); 3639 type = ctf_type_resolve(ctfp, type); 3640 kind = ctf_type_kind(ctfp, type); 3641 } 3642 3643 /* 3644 * If we follow a reference to a forward declaration tag, 3645 * search the entire type space for the actual definition. 3646 */ 3647 while (kind == CTF_K_FORWARD) { 3648 char *tag = ctf_type_name(ctfp, type, n1, sizeof (n1)); 3649 dtrace_typeinfo_t dtt; 3650 3651 if (tag != NULL && dt_type_lookup(tag, &dtt) == 0 && 3652 (dtt.dtt_ctfp != ctfp || dtt.dtt_type != type)) { 3653 ctfp = dtt.dtt_ctfp; 3654 type = ctf_type_resolve(ctfp, dtt.dtt_type); 3655 kind = ctf_type_kind(ctfp, type); 3656 } else { 3657 xyerror(D_OP_INCOMPLETE, 3658 "operator %s cannot be applied to a " 3659 "forward declaration: no %s definition " 3660 "is available\n", opstr(op), tag); 3661 } 3662 } 3663 3664 if (kind != CTF_K_STRUCT && kind != CTF_K_UNION) { 3665 if (op == DT_TOK_PTR) { 3666 xyerror(D_OP_SOU, "operator -> cannot be " 3667 "applied to pointer to type \"%s\"; must " 3668 "be applied to a struct or union pointer\n", 3669 ctf_type_name(ctfp, type, n1, sizeof (n1))); 3670 } else { 3671 xyerror(D_OP_SOU, "operator %s cannot be " 3672 "applied to type \"%s\"; must be applied " 3673 "to a struct or union\n", opstr(op), 3674 ctf_type_name(ctfp, type, n1, sizeof (n1))); 3675 } 3676 } 3677 3678 if (ctf_member_info(ctfp, type, rp->dn_string, &m) == CTF_ERR) { 3679 xyerror(D_TYPE_MEMBER, 3680 "%s is not a member of %s\n", rp->dn_string, 3681 ctf_type_name(ctfp, type, n1, sizeof (n1))); 3682 } 3683 3684 type = ctf_type_resolve(ctfp, m.ctm_type); 3685 kind = ctf_type_kind(ctfp, type); 3686 3687 dt_node_type_assign(dnp, ctfp, m.ctm_type); 3688 dt_node_attr_assign(dnp, lp->dn_attr); 3689 3690 if (op == DT_TOK_PTR && (kind != CTF_K_ARRAY || 3691 dt_node_is_string(dnp))) 3692 dnp->dn_flags |= DT_NF_LVALUE; /* see K&R[A7.3.3] */ 3693 3694 if (op == DT_TOK_DOT && (lp->dn_flags & DT_NF_LVALUE) && 3695 (kind != CTF_K_ARRAY || dt_node_is_string(dnp))) 3696 dnp->dn_flags |= DT_NF_LVALUE; /* see K&R[A7.3.3] */ 3697 3698 if (lp->dn_flags & DT_NF_WRITABLE) 3699 dnp->dn_flags |= DT_NF_WRITABLE; 3700 3701 if (uref && (kind == CTF_K_POINTER || 3702 (dnp->dn_flags & DT_NF_REF))) 3703 dnp->dn_flags |= DT_NF_USERLAND; 3704 break; 3705 3706 case DT_TOK_LBRAC: { 3707 /* 3708 * If op is DT_TOK_LBRAC, we know from the special-case code at 3709 * the top that lp is either a D variable or an aggregation. 3710 */ 3711 dt_node_t *lnp; 3712 3713 /* 3714 * If the left-hand side is an aggregation, just set dn_aggtup 3715 * to the right-hand side and return the cooked aggregation. 3716 * This transformation is legal since we are just collapsing 3717 * nodes to simplify later processing, and the entire aggtup 3718 * parse subtree is retained for subsequent cooking passes. 3719 */ 3720 if (lp->dn_kind == DT_NODE_AGG) { 3721 if (lp->dn_aggtup != NULL) { 3722 xyerror(D_AGG_MDIM, "improper attempt to " 3723 "reference @%s as a multi-dimensional " 3724 "array\n", lp->dn_ident->di_name); 3725 } 3726 3727 lp->dn_aggtup = rp; 3728 lp = dt_node_cook(lp, 0); 3729 3730 dnp->dn_left = dnp->dn_right = NULL; 3731 dt_node_free(dnp); 3732 3733 return (lp); 3734 } 3735 3736 assert(lp->dn_kind == DT_NODE_VAR); 3737 idp = lp->dn_ident; 3738 3739 /* 3740 * If the left-hand side is a non-global scalar that hasn't yet 3741 * been referenced or modified, it was just created by self-> 3742 * or this-> and we can convert it from scalar to assoc array. 3743 */ 3744 if (idp->di_kind == DT_IDENT_SCALAR && dt_ident_unref(idp) && 3745 (idp->di_flags & (DT_IDFLG_LOCAL | DT_IDFLG_TLS)) != 0) { 3746 3747 if (idp->di_flags & DT_IDFLG_LOCAL) { 3748 xyerror(D_ARR_LOCAL, 3749 "local variables may not be used as " 3750 "associative arrays: %s\n", idp->di_name); 3751 } 3752 3753 dt_dprintf("morph variable %s (id %u) from scalar to " 3754 "array\n", idp->di_name, idp->di_id); 3755 3756 dt_ident_morph(idp, DT_IDENT_ARRAY, 3757 &dt_idops_assc, NULL); 3758 } 3759 3760 if (idp->di_kind != DT_IDENT_ARRAY) { 3761 xyerror(D_IDENT_BADREF, "%s '%s' may not be referenced " 3762 "as %s\n", dt_idkind_name(idp->di_kind), 3763 idp->di_name, dt_idkind_name(DT_IDENT_ARRAY)); 3764 } 3765 3766 /* 3767 * Now that we've confirmed our left-hand side is a DT_NODE_VAR 3768 * of idkind DT_IDENT_ARRAY, we need to splice the [ node from 3769 * the parse tree and leave a cooked DT_NODE_VAR in its place 3770 * where dn_args for the VAR node is the right-hand 'rp' tree, 3771 * as shown in the parse tree diagram below: 3772 * 3773 * / / 3774 * [ OP2 "[" ]=dnp [ VAR ]=dnp 3775 * / \ => | 3776 * / \ +- dn_args -> [ ??? ]=rp 3777 * [ VAR ]=lp [ ??? ]=rp 3778 * 3779 * Since the final dt_node_cook(dnp) can fail using longjmp we 3780 * must perform the transformations as a group first by over- 3781 * writing 'dnp' to become the VAR node, so that the parse tree 3782 * is guaranteed to be in a consistent state if the cook fails. 3783 */ 3784 assert(lp->dn_kind == DT_NODE_VAR); 3785 assert(lp->dn_args == NULL); 3786 3787 lnp = dnp->dn_link; 3788 bcopy(lp, dnp, sizeof (dt_node_t)); 3789 dnp->dn_link = lnp; 3790 3791 dnp->dn_args = rp; 3792 dnp->dn_list = NULL; 3793 3794 dt_node_free(lp); 3795 return (dt_node_cook(dnp, idflags)); 3796 } 3797 3798 case DT_TOK_XLATE: { 3799 dt_xlator_t *dxp; 3800 3801 assert(lp->dn_kind == DT_NODE_TYPE); 3802 rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF); 3803 dxp = dt_xlator_lookup(dtp, rp, lp, DT_XLATE_FUZZY); 3804 3805 if (dxp == NULL) { 3806 xyerror(D_XLATE_NONE, 3807 "cannot translate from \"%s\" to \"%s\"\n", 3808 dt_node_type_name(rp, n1, sizeof (n1)), 3809 dt_node_type_name(lp, n2, sizeof (n2))); 3810 } 3811 3812 dnp->dn_ident = dt_xlator_ident(dxp, lp->dn_ctfp, lp->dn_type); 3813 dt_node_type_assign(dnp, DT_DYN_CTFP(dtp), DT_DYN_TYPE(dtp)); 3814 dt_node_attr_assign(dnp, 3815 dt_attr_min(rp->dn_attr, dnp->dn_ident->di_attr)); 3816 break; 3817 } 3818 3819 case DT_TOK_LPAR: { 3820 ctf_id_t ltype, rtype; 3821 uint_t lkind, rkind; 3822 3823 assert(lp->dn_kind == DT_NODE_TYPE); 3824 rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF); 3825 3826 ltype = ctf_type_resolve(lp->dn_ctfp, lp->dn_type); 3827 lkind = ctf_type_kind(lp->dn_ctfp, ltype); 3828 3829 rtype = ctf_type_resolve(rp->dn_ctfp, rp->dn_type); 3830 rkind = ctf_type_kind(rp->dn_ctfp, rtype); 3831 3832 /* 3833 * The rules for casting are loosely explained in K&R[A7.5] 3834 * and K&R[A6]. Basically, we can cast to the same type or 3835 * same base type, between any kind of scalar values, from 3836 * arrays to pointers, and we can cast anything to void. 3837 * To these rules D adds casts from scalars to strings. 3838 */ 3839 if (ctf_type_compat(lp->dn_ctfp, lp->dn_type, 3840 rp->dn_ctfp, rp->dn_type)) 3841 /*EMPTY*/; 3842 else if (dt_node_is_scalar(lp) && 3843 (dt_node_is_scalar(rp) || rkind == CTF_K_FUNCTION)) 3844 /*EMPTY*/; 3845 else if (dt_node_is_void(lp)) 3846 /*EMPTY*/; 3847 else if (lkind == CTF_K_POINTER && dt_node_is_pointer(rp)) 3848 /*EMPTY*/; 3849 else if (dt_node_is_string(lp) && (dt_node_is_scalar(rp) || 3850 dt_node_is_pointer(rp) || dt_node_is_strcompat(rp))) 3851 /*EMPTY*/; 3852 else { 3853 xyerror(D_CAST_INVAL, 3854 "invalid cast expression: \"%s\" to \"%s\"\n", 3855 dt_node_type_name(rp, n1, sizeof (n1)), 3856 dt_node_type_name(lp, n2, sizeof (n2))); 3857 } 3858 3859 dt_node_type_propagate(lp, dnp); /* see K&R[A7.5] */ 3860 dt_node_attr_assign(dnp, dt_attr_min(lp->dn_attr, rp->dn_attr)); 3861 break; 3862 } 3863 3864 case DT_TOK_COMMA: 3865 lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF); 3866 rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF); 3867 3868 if (dt_node_is_dynamic(lp) || dt_node_is_dynamic(rp)) { 3869 xyerror(D_OP_DYN, "operator %s operands " 3870 "cannot be of dynamic type\n", opstr(op)); 3871 } 3872 3873 if (dt_node_is_actfunc(lp) || dt_node_is_actfunc(rp)) { 3874 xyerror(D_OP_ACT, "operator %s operands " 3875 "cannot be actions\n", opstr(op)); 3876 } 3877 3878 dt_node_type_propagate(rp, dnp); /* see K&R[A7.18] */ 3879 dt_node_attr_assign(dnp, dt_attr_min(lp->dn_attr, rp->dn_attr)); 3880 break; 3881 3882 default: 3883 xyerror(D_UNKNOWN, "invalid binary op %s\n", opstr(op)); 3884 } 3885 3886 /* 3887 * Complete the conversion of E1[E2] to *((E1)+(E2)) that we started 3888 * at the top of our switch() above (see K&R[A7.3.1]). Since E2 is 3889 * parsed as an argument_expression_list by dt_grammar.y, we can 3890 * end up with a comma-separated list inside of a non-associative 3891 * array reference. We check for this and report an appropriate error. 3892 */ 3893 if (dnp->dn_op == DT_TOK_LBRAC && op == DT_TOK_ADD) { 3894 dt_node_t *pnp; 3895 3896 if (rp->dn_list != NULL) { 3897 xyerror(D_ARR_BADREF, 3898 "cannot access %s as an associative array\n", 3899 dt_node_name(lp, n1, sizeof (n1))); 3900 } 3901 3902 dnp->dn_op = DT_TOK_ADD; 3903 pnp = dt_node_op1(DT_TOK_DEREF, dnp); 3904 3905 /* 3906 * Cook callbacks are not typically permitted to allocate nodes. 3907 * When we do, we must insert them in the middle of an existing 3908 * allocation list rather than having them appended to the pcb 3909 * list because the sub-expression may be part of a definition. 3910 */ 3911 assert(yypcb->pcb_list == pnp); 3912 yypcb->pcb_list = pnp->dn_link; 3913 3914 pnp->dn_link = dnp->dn_link; 3915 dnp->dn_link = pnp; 3916 3917 return (dt_node_cook(pnp, DT_IDFLG_REF)); 3918 } 3919 3920 return (dnp); 3921 } 3922 3923 /*ARGSUSED*/ 3924 static dt_node_t * 3925 dt_cook_op3(dt_node_t *dnp, uint_t idflags) 3926 { 3927 dt_node_t *lp, *rp; 3928 ctf_file_t *ctfp; 3929 ctf_id_t type; 3930 3931 dnp->dn_expr = dt_node_cook(dnp->dn_expr, DT_IDFLG_REF); 3932 lp = dnp->dn_left = dt_node_cook(dnp->dn_left, DT_IDFLG_REF); 3933 rp = dnp->dn_right = dt_node_cook(dnp->dn_right, DT_IDFLG_REF); 3934 3935 if (!dt_node_is_scalar(dnp->dn_expr)) { 3936 xyerror(D_OP_SCALAR, 3937 "operator ?: expression must be of scalar type\n"); 3938 } 3939 3940 if (dt_node_is_dynamic(lp) || dt_node_is_dynamic(rp)) { 3941 xyerror(D_OP_DYN, 3942 "operator ?: operands cannot be of dynamic type\n"); 3943 } 3944 3945 /* 3946 * The rules for type checking for the ternary operator are complex and 3947 * are described in the ANSI-C spec (see K&R[A7.16]). We implement 3948 * the various tests in order from least to most expensive. 3949 */ 3950 if (ctf_type_compat(lp->dn_ctfp, lp->dn_type, 3951 rp->dn_ctfp, rp->dn_type)) { 3952 ctfp = lp->dn_ctfp; 3953 type = lp->dn_type; 3954 } else if (dt_node_is_integer(lp) && dt_node_is_integer(rp)) { 3955 dt_type_promote(lp, rp, &ctfp, &type); 3956 } else if (dt_node_is_strcompat(lp) && dt_node_is_strcompat(rp) && 3957 (dt_node_is_string(lp) || dt_node_is_string(rp))) { 3958 ctfp = DT_STR_CTFP(yypcb->pcb_hdl); 3959 type = DT_STR_TYPE(yypcb->pcb_hdl); 3960 } else if (dt_node_is_ptrcompat(lp, rp, &ctfp, &type) == 0) { 3961 xyerror(D_OP_INCOMPAT, 3962 "operator ?: operands must have compatible types\n"); 3963 } 3964 3965 if (dt_node_is_actfunc(lp) || dt_node_is_actfunc(rp)) { 3966 xyerror(D_OP_ACT, "action cannot be " 3967 "used in a conditional context\n"); 3968 } 3969 3970 dt_node_type_assign(dnp, ctfp, type); 3971 dt_node_attr_assign(dnp, dt_attr_min(dnp->dn_expr->dn_attr, 3972 dt_attr_min(lp->dn_attr, rp->dn_attr))); 3973 3974 return (dnp); 3975 } 3976 3977 static dt_node_t * 3978 dt_cook_statement(dt_node_t *dnp, uint_t idflags) 3979 { 3980 dnp->dn_expr = dt_node_cook(dnp->dn_expr, idflags); 3981 dt_node_attr_assign(dnp, dnp->dn_expr->dn_attr); 3982 3983 return (dnp); 3984 } 3985 3986 /* 3987 * If dn_aggfun is set, this node is a collapsed aggregation assignment (see 3988 * the special case code for DT_TOK_ASGN in dt_cook_op2() above), in which 3989 * case we cook both the tuple and the function call. If dn_aggfun is NULL, 3990 * this node is just a reference to the aggregation's type and attributes. 3991 */ 3992 /*ARGSUSED*/ 3993 static dt_node_t * 3994 dt_cook_aggregation(dt_node_t *dnp, uint_t idflags) 3995 { 3996 dtrace_hdl_t *dtp = yypcb->pcb_hdl; 3997 3998 if (dnp->dn_aggfun != NULL) { 3999 dnp->dn_aggfun = dt_node_cook(dnp->dn_aggfun, DT_IDFLG_REF); 4000 dt_node_attr_assign(dnp, dt_ident_cook(dnp, 4001 dnp->dn_ident, &dnp->dn_aggtup)); 4002 } else { 4003 dt_node_type_assign(dnp, DT_DYN_CTFP(dtp), DT_DYN_TYPE(dtp)); 4004 dt_node_attr_assign(dnp, dnp->dn_ident->di_attr); 4005 } 4006 4007 return (dnp); 4008 } 4009 4010 /* 4011 * Since D permits new variable identifiers to be instantiated in any program 4012 * expression, we may need to cook a clause's predicate either before or after 4013 * the action list depending on the program code in question. Consider: 4014 * 4015 * probe-description-list probe-description-list 4016 * /x++/ /x == 0/ 4017 * { { 4018 * trace(x); trace(x++); 4019 * } } 4020 * 4021 * In the left-hand example, the predicate uses operator ++ to instantiate 'x' 4022 * as a variable of type int64_t. The predicate must be cooked first because 4023 * otherwise the statement trace(x) refers to an unknown identifier. In the 4024 * right-hand example, the action list uses ++ to instantiate 'x'; the action 4025 * list must be cooked first because otherwise the predicate x == 0 refers to 4026 * an unknown identifier. In order to simplify programming, we support both. 4027 * 4028 * When cooking a clause, we cook the action statements before the predicate by 4029 * default, since it seems more common to create or modify identifiers in the 4030 * action list. If cooking fails due to an unknown identifier, we attempt to 4031 * cook the predicate (i.e. do it first) and then go back and cook the actions. 4032 * If this, too, fails (or if we get an error other than D_IDENT_UNDEF) we give 4033 * up and report failure back to the user. There are five possible paths: 4034 * 4035 * cook actions = OK, cook predicate = OK -> OK 4036 * cook actions = OK, cook predicate = ERR -> ERR 4037 * cook actions = ERR, cook predicate = ERR -> ERR 4038 * cook actions = ERR, cook predicate = OK, cook actions = OK -> OK 4039 * cook actions = ERR, cook predicate = OK, cook actions = ERR -> ERR 4040 * 4041 * The programmer can still defeat our scheme by creating circular definition 4042 * dependencies between predicates and actions, as in this example clause: 4043 * 4044 * probe-description-list 4045 * /x++ && y == 0/ 4046 * { 4047 * trace(x + y++); 4048 * } 4049 * 4050 * but it doesn't seem worth the complexity to handle such rare cases. The 4051 * user can simply use the D variable declaration syntax to work around them. 4052 */ 4053 static dt_node_t * 4054 dt_cook_clause(dt_node_t *dnp, uint_t idflags) 4055 { 4056 volatile int err, tries; 4057 jmp_buf ojb; 4058 4059 /* 4060 * Before assigning dn_ctxattr, temporarily assign the probe attribute 4061 * to 'dnp' itself to force an attribute check and minimum violation. 4062 */ 4063 dt_node_attr_assign(dnp, yypcb->pcb_pinfo.dtp_attr); 4064 dnp->dn_ctxattr = yypcb->pcb_pinfo.dtp_attr; 4065 4066 bcopy(yypcb->pcb_jmpbuf, ojb, sizeof (jmp_buf)); 4067 tries = 0; 4068 4069 if (dnp->dn_pred != NULL && (err = setjmp(yypcb->pcb_jmpbuf)) != 0) { 4070 bcopy(ojb, yypcb->pcb_jmpbuf, sizeof (jmp_buf)); 4071 if (tries++ != 0 || err != EDT_COMPILER || ( 4072 yypcb->pcb_hdl->dt_errtag != dt_errtag(D_IDENT_UNDEF) && 4073 yypcb->pcb_hdl->dt_errtag != dt_errtag(D_VAR_UNDEF))) 4074 longjmp(yypcb->pcb_jmpbuf, err); 4075 } 4076 4077 if (tries == 0) { 4078 yylabel("action list"); 4079 4080 dt_node_attr_assign(dnp, 4081 dt_node_list_cook(&dnp->dn_acts, idflags)); 4082 4083 bcopy(ojb, yypcb->pcb_jmpbuf, sizeof (jmp_buf)); 4084 yylabel(NULL); 4085 } 4086 4087 if (dnp->dn_pred != NULL) { 4088 yylabel("predicate"); 4089 4090 dnp->dn_pred = dt_node_cook(dnp->dn_pred, idflags); 4091 dt_node_attr_assign(dnp, 4092 dt_attr_min(dnp->dn_attr, dnp->dn_pred->dn_attr)); 4093 4094 if (!dt_node_is_scalar(dnp->dn_pred)) { 4095 xyerror(D_PRED_SCALAR, 4096 "predicate result must be of scalar type\n"); 4097 } 4098 4099 yylabel(NULL); 4100 } 4101 4102 if (tries != 0) { 4103 yylabel("action list"); 4104 4105 dt_node_attr_assign(dnp, 4106 dt_node_list_cook(&dnp->dn_acts, idflags)); 4107 4108 yylabel(NULL); 4109 } 4110 4111 return (dnp); 4112 } 4113 4114 /*ARGSUSED*/ 4115 static dt_node_t * 4116 dt_cook_inline(dt_node_t *dnp, uint_t idflags) 4117 { 4118 dt_idnode_t *inp = dnp->dn_ident->di_iarg; 4119 dt_ident_t *rdp; 4120 4121 char n1[DT_TYPE_NAMELEN]; 4122 char n2[DT_TYPE_NAMELEN]; 4123 4124 assert(dnp->dn_ident->di_flags & DT_IDFLG_INLINE); 4125 assert(inp->din_root->dn_flags & DT_NF_COOKED); 4126 4127 /* 4128 * If we are inlining a translation, verify that the inline declaration 4129 * type exactly matches the type that is returned by the translation. 4130 * Otherwise just use dt_node_is_argcompat() to check the types. 4131 */ 4132 if ((rdp = dt_node_resolve(inp->din_root, DT_IDENT_XLSOU)) != NULL || 4133 (rdp = dt_node_resolve(inp->din_root, DT_IDENT_XLPTR)) != NULL) { 4134 4135 ctf_file_t *lctfp = dnp->dn_ctfp; 4136 ctf_id_t ltype = ctf_type_resolve(lctfp, dnp->dn_type); 4137 4138 dt_xlator_t *dxp = rdp->di_data; 4139 ctf_file_t *rctfp = dxp->dx_dst_ctfp; 4140 ctf_id_t rtype = dxp->dx_dst_base; 4141 4142 if (ctf_type_kind(lctfp, ltype) == CTF_K_POINTER) { 4143 ltype = ctf_type_reference(lctfp, ltype); 4144 ltype = ctf_type_resolve(lctfp, ltype); 4145 } 4146 4147 if (ctf_type_compat(lctfp, ltype, rctfp, rtype) == 0) { 4148 dnerror(dnp, D_OP_INCOMPAT, 4149 "inline %s definition uses incompatible types: " 4150 "\"%s\" = \"%s\"\n", dnp->dn_ident->di_name, 4151 dt_type_name(lctfp, ltype, n1, sizeof (n1)), 4152 dt_type_name(rctfp, rtype, n2, sizeof (n2))); 4153 } 4154 4155 } else if (dt_node_is_argcompat(dnp, inp->din_root) == 0) { 4156 dnerror(dnp, D_OP_INCOMPAT, 4157 "inline %s definition uses incompatible types: " 4158 "\"%s\" = \"%s\"\n", dnp->dn_ident->di_name, 4159 dt_node_type_name(dnp, n1, sizeof (n1)), 4160 dt_node_type_name(inp->din_root, n2, sizeof (n2))); 4161 } 4162 4163 return (dnp); 4164 } 4165 4166 static dt_node_t * 4167 dt_cook_member(dt_node_t *dnp, uint_t idflags) 4168 { 4169 dnp->dn_membexpr = dt_node_cook(dnp->dn_membexpr, idflags); 4170 dt_node_attr_assign(dnp, dnp->dn_membexpr->dn_attr); 4171 return (dnp); 4172 } 4173 4174 /*ARGSUSED*/ 4175 static dt_node_t * 4176 dt_cook_xlator(dt_node_t *dnp, uint_t idflags) 4177 { 4178 dtrace_hdl_t *dtp = yypcb->pcb_hdl; 4179 dt_xlator_t *dxp = dnp->dn_xlator; 4180 dt_node_t *mnp; 4181 4182 char n1[DT_TYPE_NAMELEN]; 4183 char n2[DT_TYPE_NAMELEN]; 4184 4185 dtrace_attribute_t attr = _dtrace_maxattr; 4186 ctf_membinfo_t ctm; 4187 4188 /* 4189 * Before cooking each translator member, we push a reference to the 4190 * hash containing translator-local identifiers on to pcb_globals to 4191 * temporarily interpose these identifiers in front of other globals. 4192 */ 4193 dt_idstack_push(&yypcb->pcb_globals, dxp->dx_locals); 4194 4195 for (mnp = dnp->dn_members; mnp != NULL; mnp = mnp->dn_list) { 4196 if (ctf_member_info(dxp->dx_dst_ctfp, dxp->dx_dst_type, 4197 mnp->dn_membname, &ctm) == CTF_ERR) { 4198 xyerror(D_XLATE_MEMB, 4199 "translator member %s is not a member of %s\n", 4200 mnp->dn_membname, ctf_type_name(dxp->dx_dst_ctfp, 4201 dxp->dx_dst_type, n1, sizeof (n1))); 4202 } 4203 4204 (void) dt_node_cook(mnp, DT_IDFLG_REF); 4205 dt_node_type_assign(mnp, dxp->dx_dst_ctfp, ctm.ctm_type); 4206 attr = dt_attr_min(attr, mnp->dn_attr); 4207 4208 if (dt_node_is_argcompat(mnp, mnp->dn_membexpr) == 0) { 4209 xyerror(D_XLATE_INCOMPAT, 4210 "translator member %s definition uses " 4211 "incompatible types: \"%s\" = \"%s\"\n", 4212 mnp->dn_membname, 4213 dt_node_type_name(mnp, n1, sizeof (n1)), 4214 dt_node_type_name(mnp->dn_membexpr, 4215 n2, sizeof (n2))); 4216 } 4217 } 4218 4219 dt_idstack_pop(&yypcb->pcb_globals, dxp->dx_locals); 4220 4221 dxp->dx_souid.di_attr = attr; 4222 dxp->dx_ptrid.di_attr = attr; 4223 4224 dt_node_type_assign(dnp, DT_DYN_CTFP(dtp), DT_DYN_TYPE(dtp)); 4225 dt_node_attr_assign(dnp, _dtrace_defattr); 4226 4227 return (dnp); 4228 } 4229 4230 static void 4231 dt_node_provider_cmp_argv(dt_provider_t *pvp, dt_node_t *pnp, const char *kind, 4232 uint_t old_argc, dt_node_t *old_argv, uint_t new_argc, dt_node_t *new_argv) 4233 { 4234 dt_probe_t *prp = pnp->dn_ident->di_data; 4235 uint_t i; 4236 4237 char n1[DT_TYPE_NAMELEN]; 4238 char n2[DT_TYPE_NAMELEN]; 4239 4240 if (old_argc != new_argc) { 4241 dnerror(pnp, D_PROV_INCOMPAT, 4242 "probe %s:%s %s prototype mismatch:\n" 4243 "\t current: %u arg%s\n\tprevious: %u arg%s\n", 4244 pvp->pv_desc.dtvd_name, prp->pr_ident->di_name, kind, 4245 new_argc, new_argc != 1 ? "s" : "", 4246 old_argc, old_argc != 1 ? "s" : ""); 4247 } 4248 4249 for (i = 0; i < old_argc; i++, 4250 old_argv = old_argv->dn_list, new_argv = new_argv->dn_list) { 4251 if (ctf_type_cmp(old_argv->dn_ctfp, old_argv->dn_type, 4252 new_argv->dn_ctfp, new_argv->dn_type) == 0) 4253 continue; 4254 4255 dnerror(pnp, D_PROV_INCOMPAT, 4256 "probe %s:%s %s prototype argument #%u mismatch:\n" 4257 "\t current: %s\n\tprevious: %s\n", 4258 pvp->pv_desc.dtvd_name, prp->pr_ident->di_name, kind, i + 1, 4259 dt_node_type_name(new_argv, n1, sizeof (n1)), 4260 dt_node_type_name(old_argv, n2, sizeof (n2))); 4261 } 4262 } 4263 4264 /* 4265 * Compare a new probe declaration with an existing probe definition (either 4266 * from a previous declaration or cached from the kernel). If the existing 4267 * definition and declaration both have an input and output parameter list, 4268 * compare both lists. Otherwise compare only the output parameter lists. 4269 */ 4270 static void 4271 dt_node_provider_cmp(dt_provider_t *pvp, dt_node_t *pnp, 4272 dt_probe_t *old, dt_probe_t *new) 4273 { 4274 dt_node_provider_cmp_argv(pvp, pnp, "output", 4275 old->pr_xargc, old->pr_xargs, new->pr_xargc, new->pr_xargs); 4276 4277 if (old->pr_nargs != old->pr_xargs && new->pr_nargs != new->pr_xargs) { 4278 dt_node_provider_cmp_argv(pvp, pnp, "input", 4279 old->pr_nargc, old->pr_nargs, new->pr_nargc, new->pr_nargs); 4280 } 4281 4282 if (old->pr_nargs == old->pr_xargs && new->pr_nargs != new->pr_xargs) { 4283 if (pvp->pv_flags & DT_PROVIDER_IMPL) { 4284 dnerror(pnp, D_PROV_INCOMPAT, 4285 "provider interface mismatch: %s\n" 4286 "\t current: probe %s:%s has an output prototype\n" 4287 "\tprevious: probe %s:%s has no output prototype\n", 4288 pvp->pv_desc.dtvd_name, pvp->pv_desc.dtvd_name, 4289 new->pr_ident->di_name, pvp->pv_desc.dtvd_name, 4290 old->pr_ident->di_name); 4291 } 4292 4293 if (old->pr_ident->di_gen == yypcb->pcb_hdl->dt_gen) 4294 old->pr_ident->di_flags |= DT_IDFLG_ORPHAN; 4295 4296 dt_idhash_delete(pvp->pv_probes, old->pr_ident); 4297 dt_probe_declare(pvp, new); 4298 } 4299 } 4300 4301 static void 4302 dt_cook_probe(dt_node_t *dnp, dt_provider_t *pvp) 4303 { 4304 dtrace_hdl_t *dtp = yypcb->pcb_hdl; 4305 dt_probe_t *prp = dnp->dn_ident->di_data; 4306 4307 dt_xlator_t *dxp; 4308 uint_t i; 4309 4310 char n1[DT_TYPE_NAMELEN]; 4311 char n2[DT_TYPE_NAMELEN]; 4312 4313 if (prp->pr_nargs == prp->pr_xargs) 4314 return; 4315 4316 for (i = 0; i < prp->pr_xargc; i++) { 4317 dt_node_t *xnp = prp->pr_xargv[i]; 4318 dt_node_t *nnp = prp->pr_nargv[prp->pr_mapping[i]]; 4319 4320 if ((dxp = dt_xlator_lookup(dtp, 4321 nnp, xnp, DT_XLATE_FUZZY)) != NULL) { 4322 if (dt_provider_xref(dtp, pvp, dxp->dx_id) != 0) 4323 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM); 4324 continue; 4325 } 4326 4327 if (dt_node_is_argcompat(nnp, xnp)) 4328 continue; /* no translator defined and none required */ 4329 4330 dnerror(dnp, D_PROV_PRXLATOR, "translator for %s:%s output " 4331 "argument #%u from %s to %s is not defined\n", 4332 pvp->pv_desc.dtvd_name, dnp->dn_ident->di_name, i + 1, 4333 dt_node_type_name(nnp, n1, sizeof (n1)), 4334 dt_node_type_name(xnp, n2, sizeof (n2))); 4335 } 4336 } 4337 4338 /*ARGSUSED*/ 4339 static dt_node_t * 4340 dt_cook_provider(dt_node_t *dnp, uint_t idflags) 4341 { 4342 dt_provider_t *pvp = dnp->dn_provider; 4343 dt_node_t *pnp; 4344 4345 /* 4346 * If we're declaring a provider for the first time and it is unknown 4347 * to dtrace(7D), insert the probe definitions into the provider's hash. 4348 * If we're redeclaring a known provider, verify the interface matches. 4349 */ 4350 for (pnp = dnp->dn_probes; pnp != NULL; pnp = pnp->dn_list) { 4351 const char *probename = pnp->dn_ident->di_name; 4352 dt_probe_t *prp = dt_probe_lookup(pvp, probename); 4353 4354 assert(pnp->dn_kind == DT_NODE_PROBE); 4355 4356 if (prp != NULL && dnp->dn_provred) { 4357 dt_node_provider_cmp(pvp, pnp, 4358 prp, pnp->dn_ident->di_data); 4359 } else if (prp == NULL && dnp->dn_provred) { 4360 dnerror(pnp, D_PROV_INCOMPAT, 4361 "provider interface mismatch: %s\n" 4362 "\t current: probe %s:%s defined\n" 4363 "\tprevious: probe %s:%s not defined\n", 4364 dnp->dn_provname, dnp->dn_provname, 4365 probename, dnp->dn_provname, probename); 4366 } else if (prp != NULL) { 4367 dnerror(pnp, D_PROV_PRDUP, "probe redeclared: %s:%s\n", 4368 dnp->dn_provname, probename); 4369 } else 4370 dt_probe_declare(pvp, pnp->dn_ident->di_data); 4371 4372 dt_cook_probe(pnp, pvp); 4373 } 4374 4375 return (dnp); 4376 } 4377 4378 /*ARGSUSED*/ 4379 static dt_node_t * 4380 dt_cook_none(dt_node_t *dnp, uint_t idflags) 4381 { 4382 return (dnp); 4383 } 4384 4385 static dt_node_t *(*dt_cook_funcs[])(dt_node_t *, uint_t) = { 4386 dt_cook_none, /* DT_NODE_FREE */ 4387 dt_cook_none, /* DT_NODE_INT */ 4388 dt_cook_none, /* DT_NODE_STRING */ 4389 dt_cook_ident, /* DT_NODE_IDENT */ 4390 dt_cook_var, /* DT_NODE_VAR */ 4391 dt_cook_none, /* DT_NODE_SYM */ 4392 dt_cook_none, /* DT_NODE_TYPE */ 4393 dt_cook_func, /* DT_NODE_FUNC */ 4394 dt_cook_op1, /* DT_NODE_OP1 */ 4395 dt_cook_op2, /* DT_NODE_OP2 */ 4396 dt_cook_op3, /* DT_NODE_OP3 */ 4397 dt_cook_statement, /* DT_NODE_DEXPR */ 4398 dt_cook_statement, /* DT_NODE_DFUNC */ 4399 dt_cook_aggregation, /* DT_NODE_AGG */ 4400 dt_cook_none, /* DT_NODE_PDESC */ 4401 dt_cook_clause, /* DT_NODE_CLAUSE */ 4402 dt_cook_inline, /* DT_NODE_INLINE */ 4403 dt_cook_member, /* DT_NODE_MEMBER */ 4404 dt_cook_xlator, /* DT_NODE_XLATOR */ 4405 dt_cook_none, /* DT_NODE_PROBE */ 4406 dt_cook_provider, /* DT_NODE_PROVIDER */ 4407 dt_cook_none /* DT_NODE_PROG */ 4408 }; 4409 4410 /* 4411 * Recursively cook the parse tree starting at the specified node. The idflags 4412 * parameter is used to indicate the type of reference (r/w) and is applied to 4413 * the resulting identifier if it is a D variable or D aggregation. 4414 */ 4415 dt_node_t * 4416 dt_node_cook(dt_node_t *dnp, uint_t idflags) 4417 { 4418 int oldlineno = yylineno; 4419 4420 yylineno = dnp->dn_line; 4421 4422 dnp = dt_cook_funcs[dnp->dn_kind](dnp, idflags); 4423 dnp->dn_flags |= DT_NF_COOKED; 4424 4425 if (dnp->dn_kind == DT_NODE_VAR || dnp->dn_kind == DT_NODE_AGG) 4426 dnp->dn_ident->di_flags |= idflags; 4427 4428 yylineno = oldlineno; 4429 return (dnp); 4430 } 4431 4432 dtrace_attribute_t 4433 dt_node_list_cook(dt_node_t **pnp, uint_t idflags) 4434 { 4435 dtrace_attribute_t attr = _dtrace_defattr; 4436 dt_node_t *dnp, *nnp; 4437 4438 for (dnp = (pnp != NULL ? *pnp : NULL); dnp != NULL; dnp = nnp) { 4439 nnp = dnp->dn_list; 4440 dnp = *pnp = dt_node_cook(dnp, idflags); 4441 attr = dt_attr_min(attr, dnp->dn_attr); 4442 dnp->dn_list = nnp; 4443 pnp = &dnp->dn_list; 4444 } 4445 4446 return (attr); 4447 } 4448 4449 void 4450 dt_node_list_free(dt_node_t **pnp) 4451 { 4452 dt_node_t *dnp, *nnp; 4453 4454 for (dnp = (pnp != NULL ? *pnp : NULL); dnp != NULL; dnp = nnp) { 4455 nnp = dnp->dn_list; 4456 dt_node_free(dnp); 4457 } 4458 4459 if (pnp != NULL) 4460 *pnp = NULL; 4461 } 4462 4463 void 4464 dt_node_link_free(dt_node_t **pnp) 4465 { 4466 dt_node_t *dnp, *nnp; 4467 4468 for (dnp = (pnp != NULL ? *pnp : NULL); dnp != NULL; dnp = nnp) { 4469 nnp = dnp->dn_link; 4470 dt_node_free(dnp); 4471 } 4472 4473 for (dnp = (pnp != NULL ? *pnp : NULL); dnp != NULL; dnp = nnp) { 4474 nnp = dnp->dn_link; 4475 free(dnp); 4476 } 4477 4478 if (pnp != NULL) 4479 *pnp = NULL; 4480 } 4481 4482 dt_node_t * 4483 dt_node_link(dt_node_t *lp, dt_node_t *rp) 4484 { 4485 dt_node_t *dnp; 4486 4487 if (lp == NULL) 4488 return (rp); 4489 else if (rp == NULL) 4490 return (lp); 4491 4492 for (dnp = lp; dnp->dn_list != NULL; dnp = dnp->dn_list) 4493 continue; 4494 4495 dnp->dn_list = rp; 4496 return (lp); 4497 } 4498 4499 /* 4500 * Compute the DOF dtrace_diftype_t representation of a node's type. This is 4501 * called from a variety of places in the library so it cannot assume yypcb 4502 * is valid: any references to handle-specific data must be made through 'dtp'. 4503 */ 4504 void 4505 dt_node_diftype(dtrace_hdl_t *dtp, const dt_node_t *dnp, dtrace_diftype_t *tp) 4506 { 4507 if (dnp->dn_ctfp == DT_STR_CTFP(dtp) && 4508 dnp->dn_type == DT_STR_TYPE(dtp)) { 4509 tp->dtdt_kind = DIF_TYPE_STRING; 4510 tp->dtdt_ckind = CTF_K_UNKNOWN; 4511 } else { 4512 tp->dtdt_kind = DIF_TYPE_CTF; 4513 tp->dtdt_ckind = ctf_type_kind(dnp->dn_ctfp, 4514 ctf_type_resolve(dnp->dn_ctfp, dnp->dn_type)); 4515 } 4516 4517 tp->dtdt_flags = (dnp->dn_flags & DT_NF_REF) ? DIF_TF_BYREF : 0; 4518 tp->dtdt_pad = 0; 4519 tp->dtdt_size = ctf_type_size(dnp->dn_ctfp, dnp->dn_type); 4520 } 4521 4522 void 4523 dt_node_printr(dt_node_t *dnp, FILE *fp, int depth) 4524 { 4525 char n[DT_TYPE_NAMELEN], buf[BUFSIZ], a[8]; 4526 const dtrace_syminfo_t *dts; 4527 const dt_idnode_t *inp; 4528 dt_node_t *arg; 4529 4530 (void) fprintf(fp, "%*s", depth * 2, ""); 4531 (void) dt_attr_str(dnp->dn_attr, a, sizeof (a)); 4532 4533 if (dnp->dn_ctfp != NULL && dnp->dn_type != CTF_ERR && 4534 ctf_type_name(dnp->dn_ctfp, dnp->dn_type, n, sizeof (n)) != NULL) { 4535 (void) snprintf(buf, BUFSIZ, "type=<%s> attr=%s flags=", n, a); 4536 } else { 4537 (void) snprintf(buf, BUFSIZ, "type=<%ld> attr=%s flags=", 4538 dnp->dn_type, a); 4539 } 4540 4541 if (dnp->dn_flags != 0) { 4542 n[0] = '\0'; 4543 if (dnp->dn_flags & DT_NF_SIGNED) 4544 (void) strcat(n, ",SIGN"); 4545 if (dnp->dn_flags & DT_NF_COOKED) 4546 (void) strcat(n, ",COOK"); 4547 if (dnp->dn_flags & DT_NF_REF) 4548 (void) strcat(n, ",REF"); 4549 if (dnp->dn_flags & DT_NF_LVALUE) 4550 (void) strcat(n, ",LVAL"); 4551 if (dnp->dn_flags & DT_NF_WRITABLE) 4552 (void) strcat(n, ",WRITE"); 4553 if (dnp->dn_flags & DT_NF_BITFIELD) 4554 (void) strcat(n, ",BITF"); 4555 if (dnp->dn_flags & DT_NF_USERLAND) 4556 (void) strcat(n, ",USER"); 4557 (void) strcat(buf, n + 1); 4558 } else 4559 (void) strcat(buf, "0"); 4560 4561 switch (dnp->dn_kind) { 4562 case DT_NODE_FREE: 4563 (void) fprintf(fp, "FREE <node %p>\n", (void *)dnp); 4564 break; 4565 4566 case DT_NODE_INT: 4567 (void) fprintf(fp, "INT 0x%llx (%s)\n", 4568 (u_longlong_t)dnp->dn_value, buf); 4569 break; 4570 4571 case DT_NODE_STRING: 4572 (void) fprintf(fp, "STRING \"%s\" (%s)\n", dnp->dn_string, buf); 4573 break; 4574 4575 case DT_NODE_IDENT: 4576 (void) fprintf(fp, "IDENT %s (%s)\n", dnp->dn_string, buf); 4577 break; 4578 4579 case DT_NODE_VAR: 4580 (void) fprintf(fp, "VARIABLE %s%s (%s)\n", 4581 (dnp->dn_ident->di_flags & DT_IDFLG_LOCAL) ? "this->" : 4582 (dnp->dn_ident->di_flags & DT_IDFLG_TLS) ? "self->" : "", 4583 dnp->dn_ident->di_name, buf); 4584 4585 if (dnp->dn_args != NULL) 4586 (void) fprintf(fp, "%*s[\n", depth * 2, ""); 4587 4588 for (arg = dnp->dn_args; arg != NULL; arg = arg->dn_list) { 4589 dt_node_printr(arg, fp, depth + 1); 4590 if (arg->dn_list != NULL) 4591 (void) fprintf(fp, "%*s,\n", depth * 2, ""); 4592 } 4593 4594 if (dnp->dn_args != NULL) 4595 (void) fprintf(fp, "%*s]\n", depth * 2, ""); 4596 break; 4597 4598 case DT_NODE_SYM: 4599 dts = dnp->dn_ident->di_data; 4600 (void) fprintf(fp, "SYMBOL %s`%s (%s)\n", 4601 dts->dts_object, dts->dts_name, buf); 4602 break; 4603 4604 case DT_NODE_TYPE: 4605 if (dnp->dn_string != NULL) { 4606 (void) fprintf(fp, "TYPE (%s) %s\n", 4607 buf, dnp->dn_string); 4608 } else 4609 (void) fprintf(fp, "TYPE (%s)\n", buf); 4610 break; 4611 4612 case DT_NODE_FUNC: 4613 (void) fprintf(fp, "FUNC %s (%s)\n", 4614 dnp->dn_ident->di_name, buf); 4615 4616 for (arg = dnp->dn_args; arg != NULL; arg = arg->dn_list) { 4617 dt_node_printr(arg, fp, depth + 1); 4618 if (arg->dn_list != NULL) 4619 (void) fprintf(fp, "%*s,\n", depth * 2, ""); 4620 } 4621 break; 4622 4623 case DT_NODE_OP1: 4624 (void) fprintf(fp, "OP1 %s (%s)\n", opstr(dnp->dn_op), buf); 4625 dt_node_printr(dnp->dn_child, fp, depth + 1); 4626 break; 4627 4628 case DT_NODE_OP2: 4629 (void) fprintf(fp, "OP2 %s (%s)\n", opstr(dnp->dn_op), buf); 4630 dt_node_printr(dnp->dn_left, fp, depth + 1); 4631 dt_node_printr(dnp->dn_right, fp, depth + 1); 4632 break; 4633 4634 case DT_NODE_OP3: 4635 (void) fprintf(fp, "OP3 (%s)\n", buf); 4636 dt_node_printr(dnp->dn_expr, fp, depth + 1); 4637 (void) fprintf(fp, "%*s?\n", depth * 2, ""); 4638 dt_node_printr(dnp->dn_left, fp, depth + 1); 4639 (void) fprintf(fp, "%*s:\n", depth * 2, ""); 4640 dt_node_printr(dnp->dn_right, fp, depth + 1); 4641 break; 4642 4643 case DT_NODE_DEXPR: 4644 case DT_NODE_DFUNC: 4645 (void) fprintf(fp, "D EXPRESSION attr=%s\n", a); 4646 dt_node_printr(dnp->dn_expr, fp, depth + 1); 4647 break; 4648 4649 case DT_NODE_AGG: 4650 (void) fprintf(fp, "AGGREGATE @%s attr=%s [\n", 4651 dnp->dn_ident->di_name, a); 4652 4653 for (arg = dnp->dn_aggtup; arg != NULL; arg = arg->dn_list) { 4654 dt_node_printr(arg, fp, depth + 1); 4655 if (arg->dn_list != NULL) 4656 (void) fprintf(fp, "%*s,\n", depth * 2, ""); 4657 } 4658 4659 if (dnp->dn_aggfun) { 4660 (void) fprintf(fp, "%*s] = ", depth * 2, ""); 4661 dt_node_printr(dnp->dn_aggfun, fp, depth + 1); 4662 } else 4663 (void) fprintf(fp, "%*s]\n", depth * 2, ""); 4664 4665 if (dnp->dn_aggfun) 4666 (void) fprintf(fp, "%*s)\n", depth * 2, ""); 4667 break; 4668 4669 case DT_NODE_PDESC: 4670 (void) fprintf(fp, "PDESC %s:%s:%s:%s [%u]\n", 4671 dnp->dn_desc->dtpd_provider, dnp->dn_desc->dtpd_mod, 4672 dnp->dn_desc->dtpd_func, dnp->dn_desc->dtpd_name, 4673 dnp->dn_desc->dtpd_id); 4674 break; 4675 4676 case DT_NODE_CLAUSE: 4677 (void) fprintf(fp, "CLAUSE attr=%s\n", a); 4678 4679 for (arg = dnp->dn_pdescs; arg != NULL; arg = arg->dn_list) 4680 dt_node_printr(arg, fp, depth + 1); 4681 4682 (void) fprintf(fp, "%*sCTXATTR %s\n", depth * 2, "", 4683 dt_attr_str(dnp->dn_ctxattr, a, sizeof (a))); 4684 4685 if (dnp->dn_pred != NULL) { 4686 (void) fprintf(fp, "%*sPREDICATE /\n", depth * 2, ""); 4687 dt_node_printr(dnp->dn_pred, fp, depth + 1); 4688 (void) fprintf(fp, "%*s/\n", depth * 2, ""); 4689 } 4690 4691 for (arg = dnp->dn_acts; arg != NULL; arg = arg->dn_list) 4692 dt_node_printr(arg, fp, depth + 1); 4693 break; 4694 4695 case DT_NODE_INLINE: 4696 inp = dnp->dn_ident->di_iarg; 4697 4698 (void) fprintf(fp, "INLINE %s (%s)\n", 4699 dnp->dn_ident->di_name, buf); 4700 dt_node_printr(inp->din_root, fp, depth + 1); 4701 break; 4702 4703 case DT_NODE_MEMBER: 4704 (void) fprintf(fp, "MEMBER %s (%s)\n", dnp->dn_membname, buf); 4705 if (dnp->dn_membexpr) 4706 dt_node_printr(dnp->dn_membexpr, fp, depth + 1); 4707 break; 4708 4709 case DT_NODE_XLATOR: 4710 (void) fprintf(fp, "XLATOR (%s)", buf); 4711 4712 if (ctf_type_name(dnp->dn_xlator->dx_src_ctfp, 4713 dnp->dn_xlator->dx_src_type, n, sizeof (n)) != NULL) 4714 (void) fprintf(fp, " from <%s>", n); 4715 4716 if (ctf_type_name(dnp->dn_xlator->dx_dst_ctfp, 4717 dnp->dn_xlator->dx_dst_type, n, sizeof (n)) != NULL) 4718 (void) fprintf(fp, " to <%s>", n); 4719 4720 (void) fprintf(fp, "\n"); 4721 4722 for (arg = dnp->dn_members; arg != NULL; arg = arg->dn_list) 4723 dt_node_printr(arg, fp, depth + 1); 4724 break; 4725 4726 case DT_NODE_PROBE: 4727 (void) fprintf(fp, "PROBE %s\n", dnp->dn_ident->di_name); 4728 break; 4729 4730 case DT_NODE_PROVIDER: 4731 (void) fprintf(fp, "PROVIDER %s (%s)\n", 4732 dnp->dn_provname, dnp->dn_provred ? "redecl" : "decl"); 4733 for (arg = dnp->dn_probes; arg != NULL; arg = arg->dn_list) 4734 dt_node_printr(arg, fp, depth + 1); 4735 break; 4736 4737 case DT_NODE_PROG: 4738 (void) fprintf(fp, "PROGRAM attr=%s\n", a); 4739 for (arg = dnp->dn_list; arg != NULL; arg = arg->dn_list) 4740 dt_node_printr(arg, fp, depth + 1); 4741 break; 4742 4743 default: 4744 (void) fprintf(fp, "<bad node %p, kind %d>\n", 4745 (void *)dnp, dnp->dn_kind); 4746 } 4747 } 4748 4749 int 4750 dt_node_root(dt_node_t *dnp) 4751 { 4752 yypcb->pcb_root = dnp; 4753 return (0); 4754 } 4755 4756 /*PRINTFLIKE3*/ 4757 void 4758 dnerror(const dt_node_t *dnp, dt_errtag_t tag, const char *format, ...) 4759 { 4760 int oldlineno = yylineno; 4761 va_list ap; 4762 4763 yylineno = dnp->dn_line; 4764 4765 va_start(ap, format); 4766 xyvwarn(tag, format, ap); 4767 va_end(ap); 4768 4769 yylineno = oldlineno; 4770 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER); 4771 } 4772 4773 /*PRINTFLIKE3*/ 4774 void 4775 dnwarn(const dt_node_t *dnp, dt_errtag_t tag, const char *format, ...) 4776 { 4777 int oldlineno = yylineno; 4778 va_list ap; 4779 4780 yylineno = dnp->dn_line; 4781 4782 va_start(ap, format); 4783 xyvwarn(tag, format, ap); 4784 va_end(ap); 4785 4786 yylineno = oldlineno; 4787 } 4788 4789 /*PRINTFLIKE2*/ 4790 void 4791 xyerror(dt_errtag_t tag, const char *format, ...) 4792 { 4793 va_list ap; 4794 4795 va_start(ap, format); 4796 xyvwarn(tag, format, ap); 4797 va_end(ap); 4798 4799 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER); 4800 } 4801 4802 /*PRINTFLIKE2*/ 4803 void 4804 xywarn(dt_errtag_t tag, const char *format, ...) 4805 { 4806 va_list ap; 4807 4808 va_start(ap, format); 4809 xyvwarn(tag, format, ap); 4810 va_end(ap); 4811 } 4812 4813 void 4814 xyvwarn(dt_errtag_t tag, const char *format, va_list ap) 4815 { 4816 if (yypcb == NULL) 4817 return; /* compiler is not currently active: act as a no-op */ 4818 4819 dt_set_errmsg(yypcb->pcb_hdl, dt_errtag(tag), yypcb->pcb_region, 4820 yypcb->pcb_filetag, yypcb->pcb_fileptr ? yylineno : 0, format, ap); 4821 } 4822 4823 /*PRINTFLIKE1*/ 4824 void 4825 yyerror(const char *format, ...) 4826 { 4827 va_list ap; 4828 4829 va_start(ap, format); 4830 yyvwarn(format, ap); 4831 va_end(ap); 4832 4833 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER); 4834 } 4835 4836 /*PRINTFLIKE1*/ 4837 void 4838 yywarn(const char *format, ...) 4839 { 4840 va_list ap; 4841 4842 va_start(ap, format); 4843 yyvwarn(format, ap); 4844 va_end(ap); 4845 } 4846 4847 void 4848 yyvwarn(const char *format, va_list ap) 4849 { 4850 if (yypcb == NULL) 4851 return; /* compiler is not currently active: act as a no-op */ 4852 4853 dt_set_errmsg(yypcb->pcb_hdl, dt_errtag(D_SYNTAX), yypcb->pcb_region, 4854 yypcb->pcb_filetag, yypcb->pcb_fileptr ? yylineno : 0, format, ap); 4855 4856 if (strchr(format, '\n') == NULL) { 4857 dtrace_hdl_t *dtp = yypcb->pcb_hdl; 4858 size_t len = strlen(dtp->dt_errmsg); 4859 char *p, *s = dtp->dt_errmsg + len; 4860 size_t n = sizeof (dtp->dt_errmsg) - len; 4861 4862 if (yytext[0] == '\0') 4863 (void) snprintf(s, n, " near end of input"); 4864 else if (yytext[0] == '\n') 4865 (void) snprintf(s, n, " near end of line"); 4866 else { 4867 if ((p = strchr(yytext, '\n')) != NULL) 4868 *p = '\0'; /* crop at newline */ 4869 (void) snprintf(s, n, " near \"%s\"", yytext); 4870 } 4871 } 4872 } 4873 4874 void 4875 yylabel(const char *label) 4876 { 4877 dt_dprintf("set label to <%s>\n", label ? label : "NULL"); 4878 yypcb->pcb_region = label; 4879 } 4880 4881 int 4882 yywrap(void) 4883 { 4884 return (1); /* indicate that lex should return a zero token for EOF */ 4885 } 4886