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