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