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