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