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