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