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
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.
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]
24 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
25 * Copyright (c) 2011, Joyent Inc. All rights reserved.
26 * Copyright (c) 2012 by Delphix. All rights reserved.
29 #pragma ident "%Z%%M% %I% %E% SMI"
32 * DTrace D Language Parser
34 * The D Parser is a lex/yacc parser consisting of the lexer dt_lex.l, the
35 * parsing grammar dt_grammar.y, and this file, dt_parser.c, which handles
36 * the construction of the parse tree nodes and their syntactic validation.
37 * The parse tree is constructed of dt_node_t structures (see <dt_parser.h>)
38 * that are built in two passes: (1) the "create" pass, where the parse tree
39 * nodes are allocated by calls from the grammar to dt_node_*() subroutines,
40 * and (2) the "cook" pass, where nodes are coalesced, assigned D types, and
41 * validated according to the syntactic rules of the language.
43 * All node allocations are performed using dt_node_alloc(). All node frees
44 * during the parsing phase are performed by dt_node_free(), which frees node-
45 * internal state but does not actually free the nodes. All final node frees
46 * are done as part of the end of dt_compile() or as part of destroying
47 * persistent identifiers or translators which have embedded nodes.
49 * The dt_node_* routines that implement pass (1) may allocate new nodes. The
50 * dt_cook_* routines that implement pass (2) may *not* allocate new nodes.
51 * They may free existing nodes using dt_node_free(), but they may not actually
52 * deallocate any dt_node_t's. Currently dt_cook_op2() is an exception to this
53 * rule: see the comments therein for how this issue is resolved.
55 * The dt_cook_* routines are responsible for (at minimum) setting the final
56 * node type (dn_ctfp/dn_type) and attributes (dn_attr). If dn_ctfp/dn_type
57 * are set manually (i.e. not by one of the type assignment functions), then
58 * the DT_NF_COOKED flag must be set manually on the node.
60 * The cooking pass can be applied to the same parse tree more than once (used
61 * in the case of a comma-separated list of probe descriptions). As such, the
62 * cook routines must not perform any parse tree transformations which would
63 * be invalid if the tree were subsequently cooked using a different context.
65 * The dn_ctfp and dn_type fields form the type of the node. This tuple can
66 * take on the following set of values, which form our type invariants:
68 * 1. dn_ctfp = NULL, dn_type = CTF_ERR
70 * In this state, the node has unknown type and is not yet cooked. The
71 * DT_NF_COOKED flag is not yet set on the node.
73 * 2. dn_ctfp = DT_DYN_CTFP(dtp), dn_type = DT_DYN_TYPE(dtp)
75 * In this state, the node is a dynamic D type. This means that generic
76 * operations are not valid on this node and only code that knows how to
77 * examine the inner details of the node can operate on it. A <DYN> node
78 * must have dn_ident set to point to an identifier describing the object
79 * and its type. The DT_NF_REF flag is set for all nodes of type <DYN>.
80 * At present, the D compiler uses the <DYN> type for:
82 * - associative arrays that do not yet have a value type defined
83 * - translated data (i.e. the result of the xlate operator)
86 * 3. dn_ctfp = DT_STR_CTFP(dtp), dn_type = DT_STR_TYPE(dtp)
88 * In this state, the node is of type D string. The string type is really
89 * a char[0] typedef, but requires special handling throughout the compiler.
91 * 4. dn_ctfp != NULL, dn_type = any other type ID
93 * In this state, the node is of some known D/CTF type. The normal libctf
94 * APIs can be used to learn more about the type name or structure. When
95 * the type is assigned, the DT_NF_SIGNED, DT_NF_REF, and DT_NF_BITFIELD
96 * flags cache the corresponding attributes of the underlying CTF type.
99 #include <sys/param.h>
100 #include <sys/sysmacros.h>
115 #include <dt_grammar.h>
116 #include <dt_module.h>
117 #include <dt_provider.h>
118 #include <dt_string.h>
121 dt_pcb_t *yypcb; /* current control block for parser */
122 dt_node_t *yypragma; /* lex token list for control lines */
123 char yyintprefix; /* int token macro prefix (+/-) */
124 char yyintsuffix[4]; /* int token suffix string [uU][lL] */
125 int yyintdecimal; /* int token format flag (1=decimal, 0=octal/hex) */
131 case DT_TOK_COMMA: return (",");
132 case DT_TOK_ELLIPSIS: return ("...");
133 case DT_TOK_ASGN: return ("=");
134 case DT_TOK_ADD_EQ: return ("+=");
135 case DT_TOK_SUB_EQ: return ("-=");
136 case DT_TOK_MUL_EQ: return ("*=");
137 case DT_TOK_DIV_EQ: return ("/=");
138 case DT_TOK_MOD_EQ: return ("%=");
139 case DT_TOK_AND_EQ: return ("&=");
140 case DT_TOK_XOR_EQ: return ("^=");
141 case DT_TOK_OR_EQ: return ("|=");
142 case DT_TOK_LSH_EQ: return ("<<=");
143 case DT_TOK_RSH_EQ: return (">>=");
144 case DT_TOK_QUESTION: return ("?");
145 case DT_TOK_COLON: return (":");
146 case DT_TOK_LOR: return ("||");
147 case DT_TOK_LXOR: return ("^^");
148 case DT_TOK_LAND: return ("&&");
149 case DT_TOK_BOR: return ("|");
150 case DT_TOK_XOR: return ("^");
151 case DT_TOK_BAND: return ("&");
152 case DT_TOK_EQU: return ("==");
153 case DT_TOK_NEQ: return ("!=");
154 case DT_TOK_LT: return ("<");
155 case DT_TOK_LE: return ("<=");
156 case DT_TOK_GT: return (">");
157 case DT_TOK_GE: return (">=");
158 case DT_TOK_LSH: return ("<<");
159 case DT_TOK_RSH: return (">>");
160 case DT_TOK_ADD: return ("+");
161 case DT_TOK_SUB: return ("-");
162 case DT_TOK_MUL: return ("*");
163 case DT_TOK_DIV: return ("/");
164 case DT_TOK_MOD: return ("%");
165 case DT_TOK_LNEG: return ("!");
166 case DT_TOK_BNEG: return ("~");
167 case DT_TOK_ADDADD: return ("++");
168 case DT_TOK_PREINC: return ("++");
169 case DT_TOK_POSTINC: return ("++");
170 case DT_TOK_SUBSUB: return ("--");
171 case DT_TOK_PREDEC: return ("--");
172 case DT_TOK_POSTDEC: return ("--");
173 case DT_TOK_IPOS: return ("+");
174 case DT_TOK_INEG: return ("-");
175 case DT_TOK_DEREF: return ("*");
176 case DT_TOK_ADDROF: return ("&");
177 case DT_TOK_OFFSETOF: return ("offsetof");
178 case DT_TOK_SIZEOF: return ("sizeof");
179 case DT_TOK_STRINGOF: return ("stringof");
180 case DT_TOK_XLATE: return ("xlate");
181 case DT_TOK_LPAR: return ("(");
182 case DT_TOK_RPAR: return (")");
183 case DT_TOK_LBRAC: return ("[");
184 case DT_TOK_RBRAC: return ("]");
185 case DT_TOK_PTR: return ("->");
186 case DT_TOK_DOT: return (".");
187 case DT_TOK_STRING: return ("<string>");
188 case DT_TOK_IDENT: return ("<ident>");
189 case DT_TOK_TNAME: return ("<type>");
190 case DT_TOK_INT: return ("<int>");
191 default: return ("<?>");
196 dt_type_lookup(const char *s, dtrace_typeinfo_t *tip)
198 static const char delimiters[] = " \t\n\r\v\f*`";
199 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
200 const char *p, *q, *end, *obj;
202 for (p = s, end = s + strlen(s); *p != '\0'; p = q) {
204 p++; /* skip leading whitespace prior to token */
206 if (p == end || (q = strpbrk(p + 1, delimiters)) == NULL)
207 break; /* empty string or single token remaining */
210 char *object = alloca((size_t)(q - p) + 1);
211 char *type = alloca((size_t)(end - s) + 1);
214 * Copy from the start of the token (p) to the location
215 * backquote (q) to extract the nul-terminated object.
217 bcopy(p, object, (size_t)(q - p));
218 object[(size_t)(q - p)] = '\0';
221 * Copy the original string up to the start of this
222 * token (p) into type, and then concatenate everything
223 * after q. This is the type name without the object.
225 bcopy(s, type, (size_t)(p - s));
226 bcopy(q + 1, type + (size_t)(p - s), strlen(q + 1) + 1);
228 if (strchr(q + 1, '`') != NULL)
229 return (dt_set_errno(dtp, EDT_BADSCOPE));
231 return (dtrace_lookup_by_type(dtp, object, type, tip));
235 if (yypcb->pcb_idepth != 0)
236 obj = DTRACE_OBJ_CDEFS;
238 obj = DTRACE_OBJ_EVERY;
240 return (dtrace_lookup_by_type(dtp, obj, s, tip));
244 * When we parse type expressions or parse an expression with unary "&", we
245 * need to find a type that is a pointer to a previously known type.
246 * Unfortunately CTF is limited to a per-container view, so ctf_type_pointer()
247 * alone does not suffice for our needs. We provide a more intelligent wrapper
248 * for the compiler that attempts to compute a pointer to either the given type
249 * or its base (that is, we try both "foo_t *" and "struct foo *"), and also
250 * to potentially construct the required type on-the-fly.
253 dt_type_pointer(dtrace_typeinfo_t *tip)
255 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
256 ctf_file_t *ctfp = tip->dtt_ctfp;
257 ctf_id_t type = tip->dtt_type;
258 ctf_id_t base = ctf_type_resolve(ctfp, type);
263 if ((ptr = ctf_type_pointer(ctfp, type)) != CTF_ERR ||
264 (ptr = ctf_type_pointer(ctfp, base)) != CTF_ERR) {
269 if (yypcb->pcb_idepth != 0)
274 if (ctfp != dmp->dm_ctfp && ctfp != ctf_parent_file(dmp->dm_ctfp) &&
275 (type = ctf_add_type(dmp->dm_ctfp, ctfp, type)) == CTF_ERR) {
276 dtp->dt_ctferr = ctf_errno(dmp->dm_ctfp);
277 return (dt_set_errno(dtp, EDT_CTF));
280 ptr = ctf_add_pointer(dmp->dm_ctfp, CTF_ADD_ROOT, type);
282 if (ptr == CTF_ERR || ctf_update(dmp->dm_ctfp) == CTF_ERR) {
283 dtp->dt_ctferr = ctf_errno(dmp->dm_ctfp);
284 return (dt_set_errno(dtp, EDT_CTF));
287 tip->dtt_object = dmp->dm_name;
288 tip->dtt_ctfp = dmp->dm_ctfp;
295 dt_type_name(ctf_file_t *ctfp, ctf_id_t type, char *buf, size_t len)
297 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
299 if (ctfp == DT_FPTR_CTFP(dtp) && type == DT_FPTR_TYPE(dtp))
300 (void) snprintf(buf, len, "function pointer");
301 else if (ctfp == DT_FUNC_CTFP(dtp) && type == DT_FUNC_TYPE(dtp))
302 (void) snprintf(buf, len, "function");
303 else if (ctfp == DT_DYN_CTFP(dtp) && type == DT_DYN_TYPE(dtp))
304 (void) snprintf(buf, len, "dynamic variable");
305 else if (ctfp == NULL)
306 (void) snprintf(buf, len, "<none>");
307 else if (ctf_type_name(ctfp, type, buf, len) == NULL)
308 (void) snprintf(buf, len, "unknown");
314 * Perform the "usual arithmetic conversions" to determine which of the two
315 * input operand types should be promoted and used as a result type. The
316 * rules for this are described in ISOC[6.3.1.8] and K&R[A6.5].
319 dt_type_promote(dt_node_t *lp, dt_node_t *rp, ctf_file_t **ofp, ctf_id_t *otype)
321 ctf_file_t *lfp = lp->dn_ctfp;
322 ctf_id_t ltype = lp->dn_type;
324 ctf_file_t *rfp = rp->dn_ctfp;
325 ctf_id_t rtype = rp->dn_type;
327 ctf_id_t lbase = ctf_type_resolve(lfp, ltype);
328 uint_t lkind = ctf_type_kind(lfp, lbase);
330 ctf_id_t rbase = ctf_type_resolve(rfp, rtype);
331 uint_t rkind = ctf_type_kind(rfp, rbase);
333 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
334 ctf_encoding_t le, re;
337 assert(lkind == CTF_K_INTEGER || lkind == CTF_K_ENUM);
338 assert(rkind == CTF_K_INTEGER || rkind == CTF_K_ENUM);
340 if (lkind == CTF_K_ENUM) {
341 lfp = DT_INT_CTFP(dtp);
342 ltype = lbase = DT_INT_TYPE(dtp);
345 if (rkind == CTF_K_ENUM) {
346 rfp = DT_INT_CTFP(dtp);
347 rtype = rbase = DT_INT_TYPE(dtp);
350 if (ctf_type_encoding(lfp, lbase, &le) == CTF_ERR) {
351 yypcb->pcb_hdl->dt_ctferr = ctf_errno(lfp);
352 longjmp(yypcb->pcb_jmpbuf, EDT_CTF);
355 if (ctf_type_encoding(rfp, rbase, &re) == CTF_ERR) {
356 yypcb->pcb_hdl->dt_ctferr = ctf_errno(rfp);
357 longjmp(yypcb->pcb_jmpbuf, EDT_CTF);
361 * Compute an integer rank based on the size and unsigned status.
362 * If rank is identical, pick the "larger" of the equivalent types
363 * which we define as having a larger base ctf_id_t. If rank is
364 * different, pick the type with the greater rank.
366 lrank = le.cte_bits + ((le.cte_format & CTF_INT_SIGNED) == 0);
367 rrank = re.cte_bits + ((re.cte_format & CTF_INT_SIGNED) == 0);
369 if (lrank == rrank) {
370 if (lbase - rbase < 0)
374 } else if (lrank > rrank) {
390 dt_node_promote(dt_node_t *lp, dt_node_t *rp, dt_node_t *dnp)
392 dt_type_promote(lp, rp, &dnp->dn_ctfp, &dnp->dn_type);
393 dt_node_type_assign(dnp, dnp->dn_ctfp, dnp->dn_type);
394 dt_node_attr_assign(dnp, dt_attr_min(lp->dn_attr, rp->dn_attr));
398 dt_node_name(const dt_node_t *dnp, char *buf, size_t len)
400 char n1[DT_TYPE_NAMELEN];
401 char n2[DT_TYPE_NAMELEN];
403 const char *prefix = "", *suffix = "";
404 const dtrace_syminfo_t *dts;
407 switch (dnp->dn_kind) {
409 (void) snprintf(buf, len, "integer constant 0x%llx",
410 (u_longlong_t)dnp->dn_value);
413 s = strchr2esc(dnp->dn_string, strlen(dnp->dn_string));
414 (void) snprintf(buf, len, "string constant \"%s\"",
415 s != NULL ? s : dnp->dn_string);
419 (void) snprintf(buf, len, "identifier %s", dnp->dn_string);
425 switch (dnp->dn_ident->di_kind) {
427 case DT_IDENT_AGGFUNC:
428 case DT_IDENT_ACTFUNC:
435 (void) snprintf(buf, len, "%s %s%s%s",
436 dt_idkind_name(dnp->dn_ident->di_kind),
437 prefix, dnp->dn_ident->di_name, suffix);
440 dts = dnp->dn_ident->di_data;
441 (void) snprintf(buf, len, "symbol %s`%s",
442 dts->dts_object, dts->dts_name);
445 (void) snprintf(buf, len, "type %s",
446 dt_node_type_name(dnp, n1, sizeof (n1)));
451 (void) snprintf(buf, len, "operator %s", opstr(dnp->dn_op));
456 return (dt_node_name(dnp->dn_expr, buf, len));
457 (void) snprintf(buf, len, "%s", "statement");
460 if (dnp->dn_desc->dtpd_id == 0) {
461 (void) snprintf(buf, len,
462 "probe description %s:%s:%s:%s",
463 dnp->dn_desc->dtpd_provider, dnp->dn_desc->dtpd_mod,
464 dnp->dn_desc->dtpd_func, dnp->dn_desc->dtpd_name);
466 (void) snprintf(buf, len, "probe description %u",
467 dnp->dn_desc->dtpd_id);
471 (void) snprintf(buf, len, "%s", "clause");
474 (void) snprintf(buf, len, "member %s", dnp->dn_membname);
477 (void) snprintf(buf, len, "translator <%s> (%s)",
478 dt_type_name(dnp->dn_xlator->dx_dst_ctfp,
479 dnp->dn_xlator->dx_dst_type, n1, sizeof (n1)),
480 dt_type_name(dnp->dn_xlator->dx_src_ctfp,
481 dnp->dn_xlator->dx_src_type, n2, sizeof (n2)));
484 (void) snprintf(buf, len, "%s", "program");
487 (void) snprintf(buf, len, "node <%u>", dnp->dn_kind);
495 * dt_node_xalloc() can be used to create new parse nodes from any libdtrace
496 * caller. The caller is responsible for assigning dn_link appropriately.
499 dt_node_xalloc(dtrace_hdl_t *dtp, int kind)
501 dt_node_t *dnp = dt_alloc(dtp, sizeof (dt_node_t));
507 dnp->dn_type = CTF_ERR;
508 dnp->dn_kind = (uchar_t)kind;
513 dnp->dn_attr = _dtrace_defattr;
516 bzero(&dnp->dn_u, sizeof (dnp->dn_u));
522 * dt_node_alloc() is used to create new parse nodes from the parser. It
523 * assigns the node location based on the current lexer line number and places
524 * the new node on the default allocation list. If allocation fails, we
525 * automatically longjmp the caller back to the enclosing compilation call.
528 dt_node_alloc(int kind)
530 dt_node_t *dnp = dt_node_xalloc(yypcb->pcb_hdl, kind);
533 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
535 dnp->dn_line = yylineno;
536 dnp->dn_link = yypcb->pcb_list;
537 yypcb->pcb_list = dnp;
543 dt_node_free(dt_node_t *dnp)
545 uchar_t kind = dnp->dn_kind;
547 dnp->dn_kind = DT_NODE_FREE;
553 free(dnp->dn_string);
554 dnp->dn_string = NULL;
560 if (dnp->dn_ident != NULL) {
561 if (dnp->dn_ident->di_flags & DT_IDFLG_ORPHAN)
562 dt_ident_destroy(dnp->dn_ident);
563 dnp->dn_ident = NULL;
565 dt_node_list_free(&dnp->dn_args);
569 if (dnp->dn_child != NULL) {
570 dt_node_free(dnp->dn_child);
571 dnp->dn_child = NULL;
576 if (dnp->dn_expr != NULL) {
577 dt_node_free(dnp->dn_expr);
582 if (dnp->dn_left != NULL) {
583 dt_node_free(dnp->dn_left);
586 if (dnp->dn_right != NULL) {
587 dt_node_free(dnp->dn_right);
588 dnp->dn_right = NULL;
594 if (dnp->dn_expr != NULL) {
595 dt_node_free(dnp->dn_expr);
601 if (dnp->dn_aggfun != NULL) {
602 dt_node_free(dnp->dn_aggfun);
603 dnp->dn_aggfun = NULL;
605 dt_node_list_free(&dnp->dn_aggtup);
616 if (dnp->dn_pred != NULL)
617 dt_node_free(dnp->dn_pred);
618 if (dnp->dn_locals != NULL)
619 dt_idhash_destroy(dnp->dn_locals);
620 dt_node_list_free(&dnp->dn_pdescs);
621 dt_node_list_free(&dnp->dn_acts);
625 free(dnp->dn_membname);
626 dnp->dn_membname = NULL;
627 if (dnp->dn_membexpr != NULL) {
628 dt_node_free(dnp->dn_membexpr);
629 dnp->dn_membexpr = NULL;
633 case DT_NODE_PROVIDER:
634 dt_node_list_free(&dnp->dn_probes);
635 free(dnp->dn_provname);
636 dnp->dn_provname = NULL;
640 dt_node_list_free(&dnp->dn_list);
646 dt_node_attr_assign(dt_node_t *dnp, dtrace_attribute_t attr)
648 if ((yypcb->pcb_cflags & DTRACE_C_EATTR) &&
649 (dt_attr_cmp(attr, yypcb->pcb_amin) < 0)) {
650 char a[DTRACE_ATTR2STR_MAX];
653 dnerror(dnp, D_ATTR_MIN, "attributes for %s (%s) are less than "
654 "predefined minimum\n", dt_node_name(dnp, s, sizeof (s)),
655 dtrace_attr2str(attr, a, sizeof (a)));
662 dt_node_type_assign(dt_node_t *dnp, ctf_file_t *fp, ctf_id_t type)
664 ctf_id_t base = ctf_type_resolve(fp, type);
665 uint_t kind = ctf_type_kind(fp, base);
669 ~(DT_NF_SIGNED | DT_NF_REF | DT_NF_BITFIELD | DT_NF_USERLAND);
671 if (kind == CTF_K_INTEGER && ctf_type_encoding(fp, base, &e) == 0) {
672 size_t size = e.cte_bits / NBBY;
674 if (size > 8 || (e.cte_bits % NBBY) != 0 || (size & (size - 1)))
675 dnp->dn_flags |= DT_NF_BITFIELD;
677 if (e.cte_format & CTF_INT_SIGNED)
678 dnp->dn_flags |= DT_NF_SIGNED;
681 if (kind == CTF_K_FLOAT && ctf_type_encoding(fp, base, &e) == 0) {
682 if (e.cte_bits / NBBY > sizeof (uint64_t))
683 dnp->dn_flags |= DT_NF_REF;
686 if (kind == CTF_K_STRUCT || kind == CTF_K_UNION ||
687 kind == CTF_K_FORWARD ||
688 kind == CTF_K_ARRAY || kind == CTF_K_FUNCTION)
689 dnp->dn_flags |= DT_NF_REF;
690 else if (yypcb != NULL && fp == DT_DYN_CTFP(yypcb->pcb_hdl) &&
691 type == DT_DYN_TYPE(yypcb->pcb_hdl))
692 dnp->dn_flags |= DT_NF_REF;
694 dnp->dn_flags |= DT_NF_COOKED;
700 dt_node_type_propagate(const dt_node_t *src, dt_node_t *dst)
702 assert(src->dn_flags & DT_NF_COOKED);
703 dst->dn_flags = src->dn_flags & ~DT_NF_LVALUE;
704 dst->dn_ctfp = src->dn_ctfp;
705 dst->dn_type = src->dn_type;
709 dt_node_type_name(const dt_node_t *dnp, char *buf, size_t len)
711 if (dt_node_is_dynamic(dnp) && dnp->dn_ident != NULL) {
712 (void) snprintf(buf, len, "%s",
713 dt_idkind_name(dt_ident_resolve(dnp->dn_ident)->di_kind));
717 if (dnp->dn_flags & DT_NF_USERLAND) {
718 size_t n = snprintf(buf, len, "userland ");
719 len = len > n ? len - n : 0;
720 (void) dt_type_name(dnp->dn_ctfp, dnp->dn_type, buf + n, len);
724 return (dt_type_name(dnp->dn_ctfp, dnp->dn_type, buf, len));
728 dt_node_type_size(const dt_node_t *dnp)
732 if (dnp->dn_kind == DT_NODE_STRING)
733 return (strlen(dnp->dn_string) + 1);
735 if (dt_node_is_dynamic(dnp) && dnp->dn_ident != NULL)
736 return (dt_ident_size(dnp->dn_ident));
738 base = ctf_type_resolve(dnp->dn_ctfp, dnp->dn_type);
740 if (ctf_type_kind(dnp->dn_ctfp, base) == CTF_K_FORWARD)
743 return (ctf_type_size(dnp->dn_ctfp, dnp->dn_type));
747 * Determine if the specified parse tree node references an identifier of the
748 * specified kind, and if so return a pointer to it; otherwise return NULL.
749 * This function resolves the identifier itself, following through any inlines.
752 dt_node_resolve(const dt_node_t *dnp, uint_t idkind)
756 switch (dnp->dn_kind) {
763 idp = dt_ident_resolve(dnp->dn_ident);
764 return (idp->di_kind == idkind ? idp : NULL);
767 if (dt_node_is_dynamic(dnp)) {
768 idp = dt_ident_resolve(dnp->dn_ident);
769 return (idp->di_kind == idkind ? idp : NULL);
776 dt_node_sizeof(const dt_node_t *dnp)
778 dtrace_syminfo_t *sip;
780 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
783 * The size of the node as used for the sizeof() operator depends on
784 * the kind of the node. If the node is a SYM, the size is obtained
785 * from the symbol table; if it is not a SYM, the size is determined
786 * from the node's type. This is slightly different from C's sizeof()
787 * operator in that (for example) when applied to a function, sizeof()
788 * will evaluate to the length of the function rather than the size of
791 if (dnp->dn_kind != DT_NODE_SYM)
792 return (dt_node_type_size(dnp));
794 sip = dnp->dn_ident->di_data;
796 if (dtrace_lookup_by_name(dtp, sip->dts_object,
797 sip->dts_name, &sym, NULL) == -1)
800 return (sym.st_size);
804 dt_node_is_integer(const dt_node_t *dnp)
806 ctf_file_t *fp = dnp->dn_ctfp;
811 assert(dnp->dn_flags & DT_NF_COOKED);
813 type = ctf_type_resolve(fp, dnp->dn_type);
814 kind = ctf_type_kind(fp, type);
816 if (kind == CTF_K_INTEGER &&
817 ctf_type_encoding(fp, type, &e) == 0 && IS_VOID(e))
818 return (0); /* void integer */
820 return (kind == CTF_K_INTEGER || kind == CTF_K_ENUM);
824 dt_node_is_float(const dt_node_t *dnp)
826 ctf_file_t *fp = dnp->dn_ctfp;
831 assert(dnp->dn_flags & DT_NF_COOKED);
833 type = ctf_type_resolve(fp, dnp->dn_type);
834 kind = ctf_type_kind(fp, type);
836 return (kind == CTF_K_FLOAT &&
837 ctf_type_encoding(dnp->dn_ctfp, type, &e) == 0 && (
838 e.cte_format == CTF_FP_SINGLE || e.cte_format == CTF_FP_DOUBLE ||
839 e.cte_format == CTF_FP_LDOUBLE));
843 dt_node_is_scalar(const dt_node_t *dnp)
845 ctf_file_t *fp = dnp->dn_ctfp;
850 assert(dnp->dn_flags & DT_NF_COOKED);
852 type = ctf_type_resolve(fp, dnp->dn_type);
853 kind = ctf_type_kind(fp, type);
855 if (kind == CTF_K_INTEGER &&
856 ctf_type_encoding(fp, type, &e) == 0 && IS_VOID(e))
857 return (0); /* void cannot be used as a scalar */
859 return (kind == CTF_K_INTEGER || kind == CTF_K_ENUM ||
860 kind == CTF_K_POINTER);
864 dt_node_is_arith(const dt_node_t *dnp)
866 ctf_file_t *fp = dnp->dn_ctfp;
871 assert(dnp->dn_flags & DT_NF_COOKED);
873 type = ctf_type_resolve(fp, dnp->dn_type);
874 kind = ctf_type_kind(fp, type);
876 if (kind == CTF_K_INTEGER)
877 return (ctf_type_encoding(fp, type, &e) == 0 && !IS_VOID(e));
879 return (kind == CTF_K_ENUM);
883 dt_node_is_vfptr(const dt_node_t *dnp)
885 ctf_file_t *fp = dnp->dn_ctfp;
890 assert(dnp->dn_flags & DT_NF_COOKED);
892 type = ctf_type_resolve(fp, dnp->dn_type);
893 if (ctf_type_kind(fp, type) != CTF_K_POINTER)
894 return (0); /* type is not a pointer */
896 type = ctf_type_resolve(fp, ctf_type_reference(fp, type));
897 kind = ctf_type_kind(fp, type);
899 return (kind == CTF_K_FUNCTION || (kind == CTF_K_INTEGER &&
900 ctf_type_encoding(fp, type, &e) == 0 && IS_VOID(e)));
904 dt_node_is_dynamic(const dt_node_t *dnp)
906 if (dnp->dn_kind == DT_NODE_VAR &&
907 (dnp->dn_ident->di_flags & DT_IDFLG_INLINE)) {
908 const dt_idnode_t *inp = dnp->dn_ident->di_iarg;
909 return (inp->din_root ? dt_node_is_dynamic(inp->din_root) : 0);
912 return (dnp->dn_ctfp == DT_DYN_CTFP(yypcb->pcb_hdl) &&
913 dnp->dn_type == DT_DYN_TYPE(yypcb->pcb_hdl));
917 dt_node_is_string(const dt_node_t *dnp)
919 return (dnp->dn_ctfp == DT_STR_CTFP(yypcb->pcb_hdl) &&
920 dnp->dn_type == DT_STR_TYPE(yypcb->pcb_hdl));
924 dt_node_is_stack(const dt_node_t *dnp)
926 return (dnp->dn_ctfp == DT_STACK_CTFP(yypcb->pcb_hdl) &&
927 dnp->dn_type == DT_STACK_TYPE(yypcb->pcb_hdl));
931 dt_node_is_symaddr(const dt_node_t *dnp)
933 return (dnp->dn_ctfp == DT_SYMADDR_CTFP(yypcb->pcb_hdl) &&
934 dnp->dn_type == DT_SYMADDR_TYPE(yypcb->pcb_hdl));
938 dt_node_is_usymaddr(const dt_node_t *dnp)
940 return (dnp->dn_ctfp == DT_USYMADDR_CTFP(yypcb->pcb_hdl) &&
941 dnp->dn_type == DT_USYMADDR_TYPE(yypcb->pcb_hdl));
945 dt_node_is_strcompat(const dt_node_t *dnp)
947 ctf_file_t *fp = dnp->dn_ctfp;
953 assert(dnp->dn_flags & DT_NF_COOKED);
955 base = ctf_type_resolve(fp, dnp->dn_type);
956 kind = ctf_type_kind(fp, base);
958 if (kind == CTF_K_POINTER &&
959 (base = ctf_type_reference(fp, base)) != CTF_ERR &&
960 (base = ctf_type_resolve(fp, base)) != CTF_ERR &&
961 ctf_type_encoding(fp, base, &e) == 0 && IS_CHAR(e))
962 return (1); /* promote char pointer to string */
964 if (kind == CTF_K_ARRAY && ctf_array_info(fp, base, &r) == 0 &&
965 (base = ctf_type_resolve(fp, r.ctr_contents)) != CTF_ERR &&
966 ctf_type_encoding(fp, base, &e) == 0 && IS_CHAR(e))
967 return (1); /* promote char array to string */
973 dt_node_is_pointer(const dt_node_t *dnp)
975 ctf_file_t *fp = dnp->dn_ctfp;
978 assert(dnp->dn_flags & DT_NF_COOKED);
980 if (dt_node_is_string(dnp))
981 return (0); /* string are pass-by-ref but act like structs */
983 kind = ctf_type_kind(fp, ctf_type_resolve(fp, dnp->dn_type));
984 return (kind == CTF_K_POINTER || kind == CTF_K_ARRAY);
988 dt_node_is_void(const dt_node_t *dnp)
990 ctf_file_t *fp = dnp->dn_ctfp;
994 if (dt_node_is_dynamic(dnp))
995 return (0); /* <DYN> is an alias for void but not the same */
997 if (dt_node_is_stack(dnp))
1000 if (dt_node_is_symaddr(dnp) || dt_node_is_usymaddr(dnp))
1003 type = ctf_type_resolve(fp, dnp->dn_type);
1005 return (ctf_type_kind(fp, type) == CTF_K_INTEGER &&
1006 ctf_type_encoding(fp, type, &e) == 0 && IS_VOID(e));
1010 dt_node_is_ptrcompat(const dt_node_t *lp, const dt_node_t *rp,
1011 ctf_file_t **fpp, ctf_id_t *tp)
1013 ctf_file_t *lfp = lp->dn_ctfp;
1014 ctf_file_t *rfp = rp->dn_ctfp;
1016 ctf_id_t lbase = CTF_ERR, rbase = CTF_ERR;
1017 ctf_id_t lref = CTF_ERR, rref = CTF_ERR;
1019 int lp_is_void, rp_is_void, lp_is_int, rp_is_int, compat;
1020 uint_t lkind, rkind;
1024 assert(lp->dn_flags & DT_NF_COOKED);
1025 assert(rp->dn_flags & DT_NF_COOKED);
1027 if (dt_node_is_dynamic(lp) || dt_node_is_dynamic(rp))
1028 return (0); /* fail if either node is a dynamic variable */
1030 lp_is_int = dt_node_is_integer(lp);
1031 rp_is_int = dt_node_is_integer(rp);
1033 if (lp_is_int && rp_is_int)
1034 return (0); /* fail if both nodes are integers */
1036 if (lp_is_int && (lp->dn_kind != DT_NODE_INT || lp->dn_value != 0))
1037 return (0); /* fail if lp is an integer that isn't 0 constant */
1039 if (rp_is_int && (rp->dn_kind != DT_NODE_INT || rp->dn_value != 0))
1040 return (0); /* fail if rp is an integer that isn't 0 constant */
1042 if ((lp_is_int == 0 && rp_is_int == 0) && (
1043 (lp->dn_flags & DT_NF_USERLAND) ^ (rp->dn_flags & DT_NF_USERLAND)))
1044 return (0); /* fail if only one pointer is a userland address */
1047 * Resolve the left-hand and right-hand types to their base type, and
1048 * then resolve the referenced type as well (assuming the base type
1049 * is CTF_K_POINTER or CTF_K_ARRAY). Otherwise [lr]ref = CTF_ERR.
1052 lbase = ctf_type_resolve(lfp, lp->dn_type);
1053 lkind = ctf_type_kind(lfp, lbase);
1055 if (lkind == CTF_K_POINTER) {
1056 lref = ctf_type_resolve(lfp,
1057 ctf_type_reference(lfp, lbase));
1058 } else if (lkind == CTF_K_ARRAY &&
1059 ctf_array_info(lfp, lbase, &r) == 0) {
1060 lref = ctf_type_resolve(lfp, r.ctr_contents);
1065 rbase = ctf_type_resolve(rfp, rp->dn_type);
1066 rkind = ctf_type_kind(rfp, rbase);
1068 if (rkind == CTF_K_POINTER) {
1069 rref = ctf_type_resolve(rfp,
1070 ctf_type_reference(rfp, rbase));
1071 } else if (rkind == CTF_K_ARRAY &&
1072 ctf_array_info(rfp, rbase, &r) == 0) {
1073 rref = ctf_type_resolve(rfp, r.ctr_contents);
1078 * We know that one or the other type may still be a zero-valued
1079 * integer constant. To simplify the code below, set the integer
1080 * type variables equal to the non-integer types and proceed.
1087 } else if (rp_is_int) {
1094 lp_is_void = ctf_type_encoding(lfp, lref, &e) == 0 && IS_VOID(e);
1095 rp_is_void = ctf_type_encoding(rfp, rref, &e) == 0 && IS_VOID(e);
1098 * The types are compatible if both are pointers to the same type, or
1099 * if either pointer is a void pointer. If they are compatible, set
1100 * tp to point to the more specific pointer type and return it.
1102 compat = (lkind == CTF_K_POINTER || lkind == CTF_K_ARRAY) &&
1103 (rkind == CTF_K_POINTER || rkind == CTF_K_ARRAY) &&
1104 (lp_is_void || rp_is_void || ctf_type_compat(lfp, lref, rfp, rref));
1108 *fpp = rp_is_void ? lfp : rfp;
1110 *tp = rp_is_void ? lbase : rbase;
1117 * The rules for checking argument types against parameter types are described
1118 * in the ANSI-C spec (see K&R[A7.3.2] and K&R[A7.17]). We use the same rule
1119 * set to determine whether associative array arguments match the prototype.
1122 dt_node_is_argcompat(const dt_node_t *lp, const dt_node_t *rp)
1124 ctf_file_t *lfp = lp->dn_ctfp;
1125 ctf_file_t *rfp = rp->dn_ctfp;
1127 assert(lp->dn_flags & DT_NF_COOKED);
1128 assert(rp->dn_flags & DT_NF_COOKED);
1130 if (dt_node_is_integer(lp) && dt_node_is_integer(rp))
1131 return (1); /* integer types are compatible */
1133 if (dt_node_is_strcompat(lp) && dt_node_is_strcompat(rp))
1134 return (1); /* string types are compatible */
1136 if (dt_node_is_stack(lp) && dt_node_is_stack(rp))
1137 return (1); /* stack types are compatible */
1139 if (dt_node_is_symaddr(lp) && dt_node_is_symaddr(rp))
1140 return (1); /* symaddr types are compatible */
1142 if (dt_node_is_usymaddr(lp) && dt_node_is_usymaddr(rp))
1143 return (1); /* usymaddr types are compatible */
1145 switch (ctf_type_kind(lfp, ctf_type_resolve(lfp, lp->dn_type))) {
1146 case CTF_K_FUNCTION:
1149 return (ctf_type_compat(lfp, lp->dn_type, rfp, rp->dn_type));
1151 return (dt_node_is_ptrcompat(lp, rp, NULL, NULL));
1156 * We provide dt_node_is_posconst() as a convenience routine for callers who
1157 * wish to verify that an argument is a positive non-zero integer constant.
1160 dt_node_is_posconst(const dt_node_t *dnp)
1162 return (dnp->dn_kind == DT_NODE_INT && dnp->dn_value != 0 && (
1163 (dnp->dn_flags & DT_NF_SIGNED) == 0 || (int64_t)dnp->dn_value > 0));
1167 dt_node_is_actfunc(const dt_node_t *dnp)
1169 return (dnp->dn_kind == DT_NODE_FUNC &&
1170 dnp->dn_ident->di_kind == DT_IDENT_ACTFUNC);
1174 * The original rules for integer constant typing are described in K&R[A2.5.1].
1175 * However, since we support long long, we instead use the rules from ISO C99
1176 * clause 6.4.4.1 since that is where long longs are formally described. The
1177 * rules require us to know whether the constant was specified in decimal or
1178 * in octal or hex, which we do by looking at our lexer's 'yyintdecimal' flag.
1179 * The type of an integer constant is the first of the corresponding list in
1180 * which its value can be represented:
1182 * unsuffixed decimal: int, long, long long
1183 * unsuffixed oct/hex: int, unsigned int, long, unsigned long,
1184 * long long, unsigned long long
1185 * suffix [uU]: unsigned int, unsigned long, unsigned long long
1186 * suffix [lL] decimal: long, long long
1187 * suffix [lL] oct/hex: long, unsigned long, long long, unsigned long long
1188 * suffix [uU][Ll]: unsigned long, unsigned long long
1189 * suffix ll/LL decimal: long long
1190 * suffix ll/LL oct/hex: long long, unsigned long long
1191 * suffix [uU][ll/LL]: unsigned long long
1193 * Given that our lexer has already validated the suffixes by regexp matching,
1194 * there is an obvious way to concisely encode these rules: construct an array
1195 * of the types in the order int, unsigned int, long, unsigned long, long long,
1196 * unsigned long long. Compute an integer array starting index based on the
1197 * suffix (e.g. none = 0, u = 1, ull = 5), and compute an increment based on
1198 * the specifier (dec/oct/hex) and suffix (u). Then iterate from the starting
1199 * index to the end, advancing using the increment, and searching until we
1200 * find a limit that matches or we run out of choices (overflow). To make it
1201 * even faster, we precompute the table of type information in dtrace_open().
1204 dt_node_int(uintmax_t value)
1206 dt_node_t *dnp = dt_node_alloc(DT_NODE_INT);
1207 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
1209 int n = (yyintdecimal | (yyintsuffix[0] == 'u')) + 1;
1215 dnp->dn_op = DT_TOK_INT;
1216 dnp->dn_value = value;
1218 for (p = yyintsuffix; (c = *p) != '\0'; p++) {
1219 if (c == 'U' || c == 'u')
1221 else if (c == 'L' || c == 'l')
1225 for (; i < sizeof (dtp->dt_ints) / sizeof (dtp->dt_ints[0]); i += n) {
1226 if (value <= dtp->dt_ints[i].did_limit) {
1227 dt_node_type_assign(dnp,
1228 dtp->dt_ints[i].did_ctfp,
1229 dtp->dt_ints[i].did_type);
1232 * If a prefix character is present in macro text, add
1233 * in the corresponding operator node (see dt_lex.l).
1235 switch (yyintprefix) {
1237 return (dt_node_op1(DT_TOK_IPOS, dnp));
1239 return (dt_node_op1(DT_TOK_INEG, dnp));
1246 xyerror(D_INT_OFLOW, "integer constant 0x%llx cannot be represented "
1247 "in any built-in integral type\n", (u_longlong_t)value);
1249 return (NULL); /* keep gcc happy */
1253 dt_node_string(char *string)
1255 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
1259 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
1261 dnp = dt_node_alloc(DT_NODE_STRING);
1262 dnp->dn_op = DT_TOK_STRING;
1263 dnp->dn_string = string;
1264 dt_node_type_assign(dnp, DT_STR_CTFP(dtp), DT_STR_TYPE(dtp));
1270 dt_node_ident(char *name)
1276 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
1279 * If the identifier is an inlined integer constant, then create an INT
1280 * node that is a clone of the inline parse tree node and return that
1281 * immediately, allowing this inline to be used in parsing contexts
1282 * that require constant expressions (e.g. scalar array sizes).
1284 if ((idp = dt_idstack_lookup(&yypcb->pcb_globals, name)) != NULL &&
1285 (idp->di_flags & DT_IDFLG_INLINE)) {
1286 dt_idnode_t *inp = idp->di_iarg;
1288 if (inp->din_root != NULL &&
1289 inp->din_root->dn_kind == DT_NODE_INT) {
1292 dnp = dt_node_alloc(DT_NODE_INT);
1293 dnp->dn_op = DT_TOK_INT;
1294 dnp->dn_value = inp->din_root->dn_value;
1295 dt_node_type_propagate(inp->din_root, dnp);
1301 dnp = dt_node_alloc(DT_NODE_IDENT);
1302 dnp->dn_op = name[0] == '@' ? DT_TOK_AGG : DT_TOK_IDENT;
1303 dnp->dn_string = name;
1309 * Create an empty node of type corresponding to the given declaration.
1310 * Explicit references to user types (C or D) are assigned the default
1311 * stability; references to other types are _dtrace_typattr (Private).
1314 dt_node_type(dt_decl_t *ddp)
1316 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
1317 dtrace_typeinfo_t dtt;
1323 * If 'ddp' is NULL, we get a decl by popping the decl stack. This
1324 * form of dt_node_type() is used by parameter rules in dt_grammar.y.
1327 ddp = dt_decl_pop_param(&name);
1329 err = dt_decl_type(ddp, &dtt);
1334 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
1337 dnp = dt_node_alloc(DT_NODE_TYPE);
1338 dnp->dn_op = DT_TOK_IDENT;
1339 dnp->dn_string = name;
1340 dt_node_type_assign(dnp, dtt.dtt_ctfp, dtt.dtt_type);
1342 if (dtt.dtt_ctfp == dtp->dt_cdefs->dm_ctfp ||
1343 dtt.dtt_ctfp == dtp->dt_ddefs->dm_ctfp)
1344 dt_node_attr_assign(dnp, _dtrace_defattr);
1346 dt_node_attr_assign(dnp, _dtrace_typattr);
1352 * Create a type node corresponding to a varargs (...) parameter by just
1353 * assigning it type CTF_ERR. The decl processing code will handle this.
1356 dt_node_vatype(void)
1358 dt_node_t *dnp = dt_node_alloc(DT_NODE_TYPE);
1360 dnp->dn_op = DT_TOK_IDENT;
1361 dnp->dn_ctfp = yypcb->pcb_hdl->dt_cdefs->dm_ctfp;
1362 dnp->dn_type = CTF_ERR;
1363 dnp->dn_attr = _dtrace_defattr;
1369 * Instantiate a decl using the contents of the current declaration stack. As
1370 * we do not currently permit decls to be initialized, this function currently
1371 * returns NULL and no parse node is created. When this function is called,
1372 * the topmost scope's ds_ident pointer will be set to NULL (indicating no
1373 * init_declarator rule was matched) or will point to the identifier to use.
1378 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
1379 dt_scope_t *dsp = &yypcb->pcb_dstack;
1380 dt_dclass_t class = dsp->ds_class;
1381 dt_decl_t *ddp = dt_decl_top();
1384 dtrace_typeinfo_t dtt;
1387 char n1[DT_TYPE_NAMELEN];
1388 char n2[DT_TYPE_NAMELEN];
1390 if (dt_decl_type(ddp, &dtt) != 0)
1391 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
1394 * If we have no declaration identifier, then this is either a spurious
1395 * declaration of an intrinsic type (e.g. "extern int;") or declaration
1396 * or redeclaration of a struct, union, or enum type or tag.
1398 if (dsp->ds_ident == NULL) {
1399 if (ddp->dd_kind != CTF_K_STRUCT &&
1400 ddp->dd_kind != CTF_K_UNION && ddp->dd_kind != CTF_K_ENUM)
1401 xyerror(D_DECL_USELESS, "useless declaration\n");
1403 dt_dprintf("type %s added as id %ld\n", dt_type_name(
1404 ddp->dd_ctfp, ddp->dd_type, n1, sizeof (n1)), ddp->dd_type);
1409 if (strchr(dsp->ds_ident, '`') != NULL) {
1410 xyerror(D_DECL_SCOPE, "D scoping operator may not be used in "
1411 "a declaration name (%s)\n", dsp->ds_ident);
1415 * If we are nested inside of a C include file, add the declaration to
1416 * the C definition module; otherwise use the D definition module.
1418 if (yypcb->pcb_idepth != 0)
1419 dmp = dtp->dt_cdefs;
1421 dmp = dtp->dt_ddefs;
1424 * If we see a global or static declaration of a function prototype,
1425 * treat this as equivalent to a D extern declaration.
1427 if (ctf_type_kind(dtt.dtt_ctfp, dtt.dtt_type) == CTF_K_FUNCTION &&
1428 (class == DT_DC_DEFAULT || class == DT_DC_STATIC))
1429 class = DT_DC_EXTERN;
1433 case DT_DC_REGISTER:
1435 xyerror(D_DECL_BADCLASS, "specified storage class not "
1436 "appropriate in D\n");
1439 case DT_DC_EXTERN: {
1440 dtrace_typeinfo_t ott;
1441 dtrace_syminfo_t dts;
1444 int exists = dtrace_lookup_by_name(dtp,
1445 dmp->dm_name, dsp->ds_ident, &sym, &dts) == 0;
1447 if (exists && (dtrace_symbol_type(dtp, &sym, &dts, &ott) != 0 ||
1448 ctf_type_cmp(dtt.dtt_ctfp, dtt.dtt_type,
1449 ott.dtt_ctfp, ott.dtt_type) != 0)) {
1450 xyerror(D_DECL_IDRED, "identifier redeclared: %s`%s\n"
1451 "\t current: %s\n\tprevious: %s\n",
1452 dmp->dm_name, dsp->ds_ident,
1453 dt_type_name(dtt.dtt_ctfp, dtt.dtt_type,
1455 dt_type_name(ott.dtt_ctfp, ott.dtt_type,
1457 } else if (!exists && dt_module_extern(dtp, dmp,
1458 dsp->ds_ident, &dtt) == NULL) {
1460 "failed to extern %s: %s\n", dsp->ds_ident,
1461 dtrace_errmsg(dtp, dtrace_errno(dtp)));
1463 dt_dprintf("extern %s`%s type=<%s>\n",
1464 dmp->dm_name, dsp->ds_ident,
1465 dt_type_name(dtt.dtt_ctfp, dtt.dtt_type,
1472 if (dt_idstack_lookup(&yypcb->pcb_globals, dsp->ds_ident)) {
1473 xyerror(D_DECL_IDRED, "global variable identifier "
1474 "redeclared: %s\n", dsp->ds_ident);
1477 if (ctf_lookup_by_name(dmp->dm_ctfp,
1478 dsp->ds_ident) != CTF_ERR) {
1479 xyerror(D_DECL_IDRED,
1480 "typedef redeclared: %s\n", dsp->ds_ident);
1484 * If the source type for the typedef is not defined in the
1485 * target container or its parent, copy the type to the target
1486 * container and reset dtt_ctfp and dtt_type to the copy.
1488 if (dtt.dtt_ctfp != dmp->dm_ctfp &&
1489 dtt.dtt_ctfp != ctf_parent_file(dmp->dm_ctfp)) {
1491 dtt.dtt_type = ctf_add_type(dmp->dm_ctfp,
1492 dtt.dtt_ctfp, dtt.dtt_type);
1493 dtt.dtt_ctfp = dmp->dm_ctfp;
1495 if (dtt.dtt_type == CTF_ERR ||
1496 ctf_update(dtt.dtt_ctfp) == CTF_ERR) {
1497 xyerror(D_UNKNOWN, "failed to copy typedef %s "
1498 "source type: %s\n", dsp->ds_ident,
1499 ctf_errmsg(ctf_errno(dtt.dtt_ctfp)));
1503 type = ctf_add_typedef(dmp->dm_ctfp,
1504 CTF_ADD_ROOT, dsp->ds_ident, dtt.dtt_type);
1506 if (type == CTF_ERR || ctf_update(dmp->dm_ctfp) == CTF_ERR) {
1507 xyerror(D_UNKNOWN, "failed to typedef %s: %s\n",
1508 dsp->ds_ident, ctf_errmsg(ctf_errno(dmp->dm_ctfp)));
1511 dt_dprintf("typedef %s added as id %ld\n", dsp->ds_ident, type);
1525 dhp = yypcb->pcb_locals;
1526 idflags = DT_IDFLG_LOCAL;
1527 idp = dt_idhash_lookup(dhp, dsp->ds_ident);
1531 idflags = DT_IDFLG_TLS;
1532 idp = dt_idhash_lookup(dhp, dsp->ds_ident);
1535 dhp = dtp->dt_globals;
1537 idp = dt_idstack_lookup(
1538 &yypcb->pcb_globals, dsp->ds_ident);
1542 if (ddp->dd_kind == CTF_K_ARRAY && ddp->dd_node == NULL) {
1543 xyerror(D_DECL_ARRNULL,
1544 "array declaration requires array dimension or "
1545 "tuple signature: %s\n", dsp->ds_ident);
1548 if (idp != NULL && idp->di_gen == 0) {
1549 xyerror(D_DECL_IDRED, "built-in identifier "
1550 "redeclared: %s\n", idp->di_name);
1553 if (dtrace_lookup_by_type(dtp, DTRACE_OBJ_CDEFS,
1554 dsp->ds_ident, NULL) == 0 ||
1555 dtrace_lookup_by_type(dtp, DTRACE_OBJ_DDEFS,
1556 dsp->ds_ident, NULL) == 0) {
1557 xyerror(D_DECL_IDRED, "typedef identifier "
1558 "redeclared: %s\n", dsp->ds_ident);
1562 * Cache some attributes of the decl to make the rest of this
1563 * code simpler: if the decl is an array which is subscripted
1564 * by a type rather than an integer, then it's an associative
1565 * array (assc). We then expect to match either DT_IDENT_ARRAY
1566 * for associative arrays or DT_IDENT_SCALAR for anything else.
1568 assc = ddp->dd_kind == CTF_K_ARRAY &&
1569 ddp->dd_node->dn_kind == DT_NODE_TYPE;
1571 idkind = assc ? DT_IDENT_ARRAY : DT_IDENT_SCALAR;
1574 * Create a fake dt_node_t on the stack so we can determine the
1575 * type of any matching identifier by assigning to this node.
1576 * If the pre-existing ident has its di_type set, propagate
1577 * the type by hand so as not to trigger a prototype check for
1578 * arrays (yet); otherwise we use dt_ident_cook() on the ident
1579 * to ensure it is fully initialized before looking at it.
1581 bzero(&idn, sizeof (dt_node_t));
1583 if (idp != NULL && idp->di_type != CTF_ERR)
1584 dt_node_type_assign(&idn, idp->di_ctfp, idp->di_type);
1585 else if (idp != NULL)
1586 (void) dt_ident_cook(&idn, idp, NULL);
1589 if (class == DT_DC_THIS) {
1590 xyerror(D_DECL_LOCASSC, "associative arrays "
1591 "may not be declared as local variables:"
1592 " %s\n", dsp->ds_ident);
1595 if (dt_decl_type(ddp->dd_next, &dtt) != 0)
1596 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
1599 if (idp != NULL && (idp->di_kind != idkind ||
1600 ctf_type_cmp(dtt.dtt_ctfp, dtt.dtt_type,
1601 idn.dn_ctfp, idn.dn_type) != 0)) {
1602 xyerror(D_DECL_IDRED, "identifier redeclared: %s\n"
1603 "\t current: %s %s\n\tprevious: %s %s\n",
1604 dsp->ds_ident, dt_idkind_name(idkind),
1605 dt_type_name(dtt.dtt_ctfp,
1606 dtt.dtt_type, n1, sizeof (n1)),
1607 dt_idkind_name(idp->di_kind),
1608 dt_node_type_name(&idn, n2, sizeof (n2)));
1610 } else if (idp != NULL && assc) {
1611 const dt_idsig_t *isp = idp->di_data;
1612 dt_node_t *dnp = ddp->dd_node;
1615 for (; dnp != NULL; dnp = dnp->dn_list, argc++) {
1616 const dt_node_t *pnp = &isp->dis_args[argc];
1618 if (argc >= isp->dis_argc)
1619 continue; /* tuple length mismatch */
1621 if (ctf_type_cmp(dnp->dn_ctfp, dnp->dn_type,
1622 pnp->dn_ctfp, pnp->dn_type) == 0)
1625 xyerror(D_DECL_IDRED,
1626 "identifier redeclared: %s\n"
1627 "\t current: %s, key #%d of type %s\n"
1628 "\tprevious: %s, key #%d of type %s\n",
1630 dt_idkind_name(idkind), argc + 1,
1631 dt_node_type_name(dnp, n1, sizeof (n1)),
1632 dt_idkind_name(idp->di_kind), argc + 1,
1633 dt_node_type_name(pnp, n2, sizeof (n2)));
1636 if (isp->dis_argc != argc) {
1637 xyerror(D_DECL_IDRED,
1638 "identifier redeclared: %s\n"
1639 "\t current: %s of %s, tuple length %d\n"
1640 "\tprevious: %s of %s, tuple length %d\n",
1641 dsp->ds_ident, dt_idkind_name(idkind),
1642 dt_type_name(dtt.dtt_ctfp, dtt.dtt_type,
1643 n1, sizeof (n1)), argc,
1644 dt_idkind_name(idp->di_kind),
1645 dt_node_type_name(&idn, n2, sizeof (n2)),
1649 } else if (idp == NULL) {
1650 type = ctf_type_resolve(dtt.dtt_ctfp, dtt.dtt_type);
1651 kind = ctf_type_kind(dtt.dtt_ctfp, type);
1655 if (ctf_type_encoding(dtt.dtt_ctfp, type,
1656 &cte) == 0 && IS_VOID(cte)) {
1657 xyerror(D_DECL_VOIDOBJ, "cannot have "
1658 "void object: %s\n", dsp->ds_ident);
1663 if (ctf_type_size(dtt.dtt_ctfp, type) != 0)
1664 break; /* proceed to declaring */
1667 xyerror(D_DECL_INCOMPLETE,
1668 "incomplete struct/union/enum %s: %s\n",
1669 dt_type_name(dtt.dtt_ctfp, dtt.dtt_type,
1670 n1, sizeof (n1)), dsp->ds_ident);
1674 if (dt_idhash_nextid(dhp, &id) == -1) {
1675 xyerror(D_ID_OFLOW, "cannot create %s: limit "
1676 "on number of %s variables exceeded\n",
1677 dsp->ds_ident, dt_idhash_name(dhp));
1680 dt_dprintf("declare %s %s variable %s, id=%u\n",
1681 dt_idhash_name(dhp), dt_idkind_name(idkind),
1684 idp = dt_idhash_insert(dhp, dsp->ds_ident, idkind,
1685 idflags | DT_IDFLG_WRITE | DT_IDFLG_DECL, id,
1686 _dtrace_defattr, 0, assc ? &dt_idops_assc :
1687 &dt_idops_thaw, NULL, dtp->dt_gen);
1690 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
1692 dt_ident_type_assign(idp, dtt.dtt_ctfp, dtt.dtt_type);
1695 * If we are declaring an associative array, use our
1696 * fake parse node to cook the new assoc identifier.
1697 * This will force the ident code to instantiate the
1698 * array type signature corresponding to the list of
1699 * types pointed to by ddp->dd_node. We also reset
1700 * the identifier's attributes based upon the result.
1704 dt_ident_cook(&idn, idp, &ddp->dd_node);
1709 } /* end of switch */
1711 free(dsp->ds_ident);
1712 dsp->ds_ident = NULL;
1718 dt_node_func(dt_node_t *dnp, dt_node_t *args)
1722 if (dnp->dn_kind != DT_NODE_IDENT) {
1723 xyerror(D_FUNC_IDENT,
1724 "function designator is not of function type\n");
1727 idp = dt_idstack_lookup(&yypcb->pcb_globals, dnp->dn_string);
1730 xyerror(D_FUNC_UNDEF,
1731 "undefined function name: %s\n", dnp->dn_string);
1734 if (idp->di_kind != DT_IDENT_FUNC &&
1735 idp->di_kind != DT_IDENT_AGGFUNC &&
1736 idp->di_kind != DT_IDENT_ACTFUNC) {
1737 xyerror(D_FUNC_IDKIND, "%s '%s' may not be referenced as a "
1738 "function\n", dt_idkind_name(idp->di_kind), idp->di_name);
1741 free(dnp->dn_string);
1742 dnp->dn_string = NULL;
1744 dnp->dn_kind = DT_NODE_FUNC;
1745 dnp->dn_flags &= ~DT_NF_COOKED;
1746 dnp->dn_ident = idp;
1747 dnp->dn_args = args;
1748 dnp->dn_list = NULL;
1754 * The offsetof() function is special because it takes a type name as an
1755 * argument. It does not actually construct its own node; after looking up the
1756 * structure or union offset, we just return an integer node with the offset.
1759 dt_node_offsetof(dt_decl_t *ddp, char *s)
1761 dtrace_typeinfo_t dtt;
1770 name = alloca(strlen(s) + 1);
1771 (void) strcpy(name, s);
1774 err = dt_decl_type(ddp, &dtt);
1778 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
1780 type = ctf_type_resolve(dtt.dtt_ctfp, dtt.dtt_type);
1781 kind = ctf_type_kind(dtt.dtt_ctfp, type);
1783 if (kind != CTF_K_STRUCT && kind != CTF_K_UNION) {
1784 xyerror(D_OFFSETOF_TYPE,
1785 "offsetof operand must be a struct or union type\n");
1788 if (ctf_member_info(dtt.dtt_ctfp, type, name, &ctm) == CTF_ERR) {
1789 xyerror(D_UNKNOWN, "failed to determine offset of %s: %s\n",
1790 name, ctf_errmsg(ctf_errno(dtt.dtt_ctfp)));
1793 bzero(&dn, sizeof (dn));
1794 dt_node_type_assign(&dn, dtt.dtt_ctfp, ctm.ctm_type);
1796 if (dn.dn_flags & DT_NF_BITFIELD) {
1797 xyerror(D_OFFSETOF_BITFIELD,
1798 "cannot take offset of a bit-field: %s\n", name);
1801 return (dt_node_int(ctm.ctm_offset / NBBY));
1805 dt_node_op1(int op, dt_node_t *cp)
1809 if (cp->dn_kind == DT_NODE_INT) {
1813 * If we're negating an unsigned integer, zero out any
1814 * extra top bits to truncate the value to the size of
1815 * the effective type determined by dt_node_int().
1817 cp->dn_value = -cp->dn_value;
1818 if (!(cp->dn_flags & DT_NF_SIGNED)) {
1819 cp->dn_value &= ~0ULL >>
1820 (64 - dt_node_type_size(cp) * NBBY);
1826 cp->dn_value = ~cp->dn_value;
1829 cp->dn_value = !cp->dn_value;
1835 * If sizeof is applied to a type_name or string constant, we can
1836 * transform 'cp' into an integer constant in the node construction
1837 * pass so that it can then be used for arithmetic in this pass.
1839 if (op == DT_TOK_SIZEOF &&
1840 (cp->dn_kind == DT_NODE_STRING || cp->dn_kind == DT_NODE_TYPE)) {
1841 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
1842 size_t size = dt_node_type_size(cp);
1845 xyerror(D_SIZEOF_TYPE, "cannot apply sizeof to an "
1846 "operand of unknown size\n");
1849 dt_node_type_assign(cp, dtp->dt_ddefs->dm_ctfp,
1850 ctf_lookup_by_name(dtp->dt_ddefs->dm_ctfp, "size_t"));
1852 cp->dn_kind = DT_NODE_INT;
1853 cp->dn_op = DT_TOK_INT;
1854 cp->dn_value = size;
1859 dnp = dt_node_alloc(DT_NODE_OP1);
1860 assert(op <= USHRT_MAX);
1861 dnp->dn_op = (ushort_t)op;
1868 * If an integer constant is being cast to another integer type, we can
1869 * perform the cast as part of integer constant folding in this pass. We must
1870 * take action when the integer is being cast to a smaller type or if it is
1871 * changing signed-ness. If so, we first shift rp's bits bits high (losing
1872 * excess bits if narrowing) and then shift them down with either a logical
1873 * shift (unsigned) or arithmetic shift (signed).
1876 dt_cast(dt_node_t *lp, dt_node_t *rp)
1878 size_t srcsize = dt_node_type_size(rp);
1879 size_t dstsize = dt_node_type_size(lp);
1881 if (dstsize < srcsize) {
1882 int n = (sizeof (uint64_t) - dstsize) * NBBY;
1885 } else if (dstsize > srcsize) {
1886 int n = (sizeof (uint64_t) - srcsize) * NBBY;
1887 int s = (dstsize - srcsize) * NBBY;
1890 if (rp->dn_flags & DT_NF_SIGNED) {
1891 rp->dn_value = (intmax_t)rp->dn_value >> s;
1892 rp->dn_value >>= n - s;
1900 dt_node_op2(int op, dt_node_t *lp, dt_node_t *rp)
1902 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
1906 * First we check for operations that are illegal -- namely those that
1907 * might result in integer division by zero, and abort if one is found.
1909 if (rp->dn_kind == DT_NODE_INT && rp->dn_value == 0 &&
1910 (op == DT_TOK_MOD || op == DT_TOK_DIV ||
1911 op == DT_TOK_MOD_EQ || op == DT_TOK_DIV_EQ))
1912 xyerror(D_DIV_ZERO, "expression contains division by zero\n");
1915 * If both children are immediate values, we can just perform inline
1916 * calculation and return a new immediate node with the result.
1918 if (lp->dn_kind == DT_NODE_INT && rp->dn_kind == DT_NODE_INT) {
1919 uintmax_t l = lp->dn_value;
1920 uintmax_t r = rp->dn_value;
1922 dnp = dt_node_int(0); /* allocate new integer node for result */
1926 dnp->dn_value = l || r;
1927 dt_node_type_assign(dnp,
1928 DT_INT_CTFP(dtp), DT_INT_TYPE(dtp));
1931 dnp->dn_value = (l != 0) ^ (r != 0);
1932 dt_node_type_assign(dnp,
1933 DT_INT_CTFP(dtp), DT_INT_TYPE(dtp));
1936 dnp->dn_value = l && r;
1937 dt_node_type_assign(dnp,
1938 DT_INT_CTFP(dtp), DT_INT_TYPE(dtp));
1941 dnp->dn_value = l | r;
1942 dt_node_promote(lp, rp, dnp);
1945 dnp->dn_value = l ^ r;
1946 dt_node_promote(lp, rp, dnp);
1949 dnp->dn_value = l & r;
1950 dt_node_promote(lp, rp, dnp);
1953 dnp->dn_value = l == r;
1954 dt_node_type_assign(dnp,
1955 DT_INT_CTFP(dtp), DT_INT_TYPE(dtp));
1958 dnp->dn_value = l != r;
1959 dt_node_type_assign(dnp,
1960 DT_INT_CTFP(dtp), DT_INT_TYPE(dtp));
1963 dt_node_promote(lp, rp, dnp);
1964 if (dnp->dn_flags & DT_NF_SIGNED)
1965 dnp->dn_value = (intmax_t)l < (intmax_t)r;
1967 dnp->dn_value = l < r;
1968 dt_node_type_assign(dnp,
1969 DT_INT_CTFP(dtp), DT_INT_TYPE(dtp));
1972 dt_node_promote(lp, rp, dnp);
1973 if (dnp->dn_flags & DT_NF_SIGNED)
1974 dnp->dn_value = (intmax_t)l <= (intmax_t)r;
1976 dnp->dn_value = l <= r;
1977 dt_node_type_assign(dnp,
1978 DT_INT_CTFP(dtp), DT_INT_TYPE(dtp));
1981 dt_node_promote(lp, rp, dnp);
1982 if (dnp->dn_flags & DT_NF_SIGNED)
1983 dnp->dn_value = (intmax_t)l > (intmax_t)r;
1985 dnp->dn_value = l > r;
1986 dt_node_type_assign(dnp,
1987 DT_INT_CTFP(dtp), DT_INT_TYPE(dtp));
1990 dt_node_promote(lp, rp, dnp);
1991 if (dnp->dn_flags & DT_NF_SIGNED)
1992 dnp->dn_value = (intmax_t)l >= (intmax_t)r;
1994 dnp->dn_value = l >= r;
1995 dt_node_type_assign(dnp,
1996 DT_INT_CTFP(dtp), DT_INT_TYPE(dtp));
1999 dnp->dn_value = l << r;
2000 dt_node_type_propagate(lp, dnp);
2001 dt_node_attr_assign(rp,
2002 dt_attr_min(lp->dn_attr, rp->dn_attr));
2005 dnp->dn_value = l >> r;
2006 dt_node_type_propagate(lp, dnp);
2007 dt_node_attr_assign(rp,
2008 dt_attr_min(lp->dn_attr, rp->dn_attr));
2011 dnp->dn_value = l + r;
2012 dt_node_promote(lp, rp, dnp);
2015 dnp->dn_value = l - r;
2016 dt_node_promote(lp, rp, dnp);
2019 dnp->dn_value = l * r;
2020 dt_node_promote(lp, rp, dnp);
2023 dt_node_promote(lp, rp, dnp);
2024 if (dnp->dn_flags & DT_NF_SIGNED)
2025 dnp->dn_value = (intmax_t)l / (intmax_t)r;
2027 dnp->dn_value = l / r;
2030 dt_node_promote(lp, rp, dnp);
2031 if (dnp->dn_flags & DT_NF_SIGNED)
2032 dnp->dn_value = (intmax_t)l % (intmax_t)r;
2034 dnp->dn_value = l % r;
2048 if (op == DT_TOK_LPAR && rp->dn_kind == DT_NODE_INT &&
2049 dt_node_is_integer(lp)) {
2051 dt_node_type_propagate(lp, rp);
2052 dt_node_attr_assign(rp, dt_attr_min(lp->dn_attr, rp->dn_attr));
2059 * If no immediate optimizations are available, create an new OP2 node
2060 * and glue the left and right children into place and return.
2062 dnp = dt_node_alloc(DT_NODE_OP2);
2063 assert(op <= USHRT_MAX);
2064 dnp->dn_op = (ushort_t)op;
2072 dt_node_op3(dt_node_t *expr, dt_node_t *lp, dt_node_t *rp)
2076 if (expr->dn_kind == DT_NODE_INT)
2077 return (expr->dn_value != 0 ? lp : rp);
2079 dnp = dt_node_alloc(DT_NODE_OP3);
2080 dnp->dn_op = DT_TOK_QUESTION;
2081 dnp->dn_expr = expr;
2089 dt_node_statement(dt_node_t *expr)
2093 if (expr->dn_kind == DT_NODE_AGG)
2096 if (expr->dn_kind == DT_NODE_FUNC &&
2097 expr->dn_ident->di_kind == DT_IDENT_ACTFUNC)
2098 dnp = dt_node_alloc(DT_NODE_DFUNC);
2100 dnp = dt_node_alloc(DT_NODE_DEXPR);
2102 dnp->dn_expr = expr;
2107 dt_node_pdesc_by_name(char *spec)
2109 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
2113 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2115 dnp = dt_node_alloc(DT_NODE_PDESC);
2116 dnp->dn_spec = spec;
2117 dnp->dn_desc = malloc(sizeof (dtrace_probedesc_t));
2119 if (dnp->dn_desc == NULL)
2120 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2122 if (dtrace_xstr2desc(dtp, yypcb->pcb_pspec, dnp->dn_spec,
2123 yypcb->pcb_sargc, yypcb->pcb_sargv, dnp->dn_desc) != 0) {
2124 xyerror(D_PDESC_INVAL, "invalid probe description \"%s\": %s\n",
2125 dnp->dn_spec, dtrace_errmsg(dtp, dtrace_errno(dtp)));
2129 dnp->dn_spec = NULL;
2135 dt_node_pdesc_by_id(uintmax_t id)
2137 static const char *const names[] = {
2138 "providers", "modules", "functions"
2141 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
2142 dt_node_t *dnp = dt_node_alloc(DT_NODE_PDESC);
2144 if ((dnp->dn_desc = malloc(sizeof (dtrace_probedesc_t))) == NULL)
2145 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2147 if (id > UINT_MAX) {
2148 xyerror(D_PDESC_INVAL, "identifier %llu exceeds maximum "
2149 "probe id\n", (u_longlong_t)id);
2152 if (yypcb->pcb_pspec != DTRACE_PROBESPEC_NAME) {
2153 xyerror(D_PDESC_INVAL, "probe identifier %llu not permitted "
2154 "when specifying %s\n", (u_longlong_t)id,
2155 names[yypcb->pcb_pspec]);
2158 if (dtrace_id2desc(dtp, (dtrace_id_t)id, dnp->dn_desc) != 0) {
2159 xyerror(D_PDESC_INVAL, "invalid probe identifier %llu: %s\n",
2160 (u_longlong_t)id, dtrace_errmsg(dtp, dtrace_errno(dtp)));
2167 dt_node_clause(dt_node_t *pdescs, dt_node_t *pred, dt_node_t *acts)
2169 dt_node_t *dnp = dt_node_alloc(DT_NODE_CLAUSE);
2171 dnp->dn_pdescs = pdescs;
2172 dnp->dn_pred = pred;
2173 dnp->dn_acts = acts;
2175 yybegin(YYS_CLAUSE);
2180 dt_node_inline(dt_node_t *expr)
2182 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
2183 dt_scope_t *dsp = &yypcb->pcb_dstack;
2184 dt_decl_t *ddp = dt_decl_top();
2186 char n[DT_TYPE_NAMELEN];
2187 dtrace_typeinfo_t dtt;
2189 dt_ident_t *idp, *rdp;
2193 if (dt_decl_type(ddp, &dtt) != 0)
2194 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
2196 if (dsp->ds_class != DT_DC_DEFAULT) {
2197 xyerror(D_DECL_BADCLASS, "specified storage class not "
2198 "appropriate for inline declaration\n");
2201 if (dsp->ds_ident == NULL)
2202 xyerror(D_DECL_USELESS, "inline declaration requires a name\n");
2204 if ((idp = dt_idstack_lookup(
2205 &yypcb->pcb_globals, dsp->ds_ident)) != NULL) {
2206 xyerror(D_DECL_IDRED, "identifier redefined: %s\n\t current: "
2207 "inline definition\n\tprevious: %s %s\n",
2208 idp->di_name, dt_idkind_name(idp->di_kind),
2209 (idp->di_flags & DT_IDFLG_INLINE) ? "inline" : "");
2213 * If we are declaring an inlined array, verify that we have a tuple
2214 * signature, and then recompute 'dtt' as the array's value type.
2216 if (ddp->dd_kind == CTF_K_ARRAY) {
2217 if (ddp->dd_node == NULL) {
2218 xyerror(D_DECL_ARRNULL, "inline declaration requires "
2219 "array tuple signature: %s\n", dsp->ds_ident);
2222 if (ddp->dd_node->dn_kind != DT_NODE_TYPE) {
2223 xyerror(D_DECL_ARRNULL, "inline declaration cannot be "
2224 "of scalar array type: %s\n", dsp->ds_ident);
2227 if (dt_decl_type(ddp->dd_next, &dtt) != 0)
2228 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
2232 * If the inline identifier is not defined, then create it with the
2233 * orphan flag set. We do not insert the identifier into dt_globals
2234 * until we have successfully cooked the right-hand expression, below.
2236 dnp = dt_node_alloc(DT_NODE_INLINE);
2237 dt_node_type_assign(dnp, dtt.dtt_ctfp, dtt.dtt_type);
2238 dt_node_attr_assign(dnp, _dtrace_defattr);
2240 if (dt_node_is_void(dnp)) {
2241 xyerror(D_DECL_VOIDOBJ,
2242 "cannot declare void inline: %s\n", dsp->ds_ident);
2245 if (ctf_type_kind(dnp->dn_ctfp, ctf_type_resolve(
2246 dnp->dn_ctfp, dnp->dn_type)) == CTF_K_FORWARD) {
2247 xyerror(D_DECL_INCOMPLETE,
2248 "incomplete struct/union/enum %s: %s\n",
2249 dt_node_type_name(dnp, n, sizeof (n)), dsp->ds_ident);
2252 if ((inp = malloc(sizeof (dt_idnode_t))) == NULL)
2253 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2255 bzero(inp, sizeof (dt_idnode_t));
2257 idp = dnp->dn_ident = dt_ident_create(dsp->ds_ident,
2258 ddp->dd_kind == CTF_K_ARRAY ? DT_IDENT_ARRAY : DT_IDENT_SCALAR,
2259 DT_IDFLG_INLINE | DT_IDFLG_REF | DT_IDFLG_DECL | DT_IDFLG_ORPHAN, 0,
2260 _dtrace_defattr, 0, &dt_idops_inline, inp, dtp->dt_gen);
2264 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2268 * If we're inlining an associative array, create a private identifier
2269 * hash containing the named parameters and store it in inp->din_hash.
2270 * We then push this hash on to the top of the pcb_globals stack.
2272 if (ddp->dd_kind == CTF_K_ARRAY) {
2278 for (pnp = ddp->dd_node; pnp != NULL; pnp = pnp->dn_list)
2279 i++; /* count up parameters for din_argv[] */
2281 inp->din_hash = dt_idhash_create("inline args", NULL, 0, 0);
2282 inp->din_argv = calloc(i, sizeof (dt_ident_t *));
2284 if (inp->din_hash == NULL || inp->din_argv == NULL)
2285 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2288 * Create an identifier for each parameter as a scalar inline,
2289 * and store it in din_hash and in position in din_argv[]. The
2290 * parameter identifiers also use dt_idops_inline, but we leave
2291 * the dt_idnode_t argument 'pinp' zeroed. This will be filled
2292 * in by the code generation pass with references to the args.
2294 for (i = 0, pnp = ddp->dd_node;
2295 pnp != NULL; pnp = pnp->dn_list, i++) {
2297 if (pnp->dn_string == NULL)
2298 continue; /* ignore anonymous parameters */
2300 if ((pinp = malloc(sizeof (dt_idnode_t))) == NULL)
2301 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2303 pidp = dt_idhash_insert(inp->din_hash, pnp->dn_string,
2304 DT_IDENT_SCALAR, DT_IDFLG_DECL | DT_IDFLG_INLINE, 0,
2305 _dtrace_defattr, 0, &dt_idops_inline,
2310 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2313 inp->din_argv[i] = pidp;
2314 bzero(pinp, sizeof (dt_idnode_t));
2315 dt_ident_type_assign(pidp, pnp->dn_ctfp, pnp->dn_type);
2318 dt_idstack_push(&yypcb->pcb_globals, inp->din_hash);
2322 * Unlike most constructors, we need to explicitly cook the right-hand
2323 * side of the inline definition immediately to prevent recursion. If
2324 * the right-hand side uses the inline itself, the cook will fail.
2326 expr = dt_node_cook(expr, DT_IDFLG_REF);
2328 if (ddp->dd_kind == CTF_K_ARRAY)
2329 dt_idstack_pop(&yypcb->pcb_globals, inp->din_hash);
2332 * Set the type, attributes, and flags for the inline. If the right-
2333 * hand expression has an identifier, propagate its flags. Then cook
2334 * the identifier to fully initialize it: if we're declaring an inline
2335 * associative array this will construct a type signature from 'ddp'.
2337 if (dt_node_is_dynamic(expr))
2338 rdp = dt_ident_resolve(expr->dn_ident);
2339 else if (expr->dn_kind == DT_NODE_VAR || expr->dn_kind == DT_NODE_SYM)
2340 rdp = expr->dn_ident;
2345 idp->di_flags |= (rdp->di_flags &
2346 (DT_IDFLG_WRITE | DT_IDFLG_USER | DT_IDFLG_PRIM));
2349 idp->di_attr = dt_attr_min(_dtrace_defattr, expr->dn_attr);
2350 dt_ident_type_assign(idp, dtt.dtt_ctfp, dtt.dtt_type);
2351 (void) dt_ident_cook(dnp, idp, &ddp->dd_node);
2354 * Store the parse tree nodes for 'expr' inside of idp->di_data ('inp')
2355 * so that they will be preserved with this identifier. Then pop the
2356 * inline declaration from the declaration stack and restore the lexer.
2358 inp->din_list = yypcb->pcb_list;
2359 inp->din_root = expr;
2361 dt_decl_free(dt_decl_pop());
2362 yybegin(YYS_CLAUSE);
2365 * Finally, insert the inline identifier into dt_globals to make it
2366 * visible, and then cook 'dnp' to check its type against 'expr'.
2368 dt_idhash_xinsert(dtp->dt_globals, idp);
2369 return (dt_node_cook(dnp, DT_IDFLG_REF));
2373 dt_node_member(dt_decl_t *ddp, char *name, dt_node_t *expr)
2375 dtrace_typeinfo_t dtt;
2380 err = dt_decl_type(ddp, &dtt);
2384 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
2387 dnp = dt_node_alloc(DT_NODE_MEMBER);
2388 dnp->dn_membname = name;
2389 dnp->dn_membexpr = expr;
2392 dt_node_type_assign(dnp, dtt.dtt_ctfp, dtt.dtt_type);
2398 dt_node_xlator(dt_decl_t *ddp, dt_decl_t *sdp, char *name, dt_node_t *members)
2400 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
2401 dtrace_typeinfo_t src, dst;
2408 char n1[DT_TYPE_NAMELEN];
2409 char n2[DT_TYPE_NAMELEN];
2411 edst = dt_decl_type(ddp, &dst);
2414 esrc = dt_decl_type(sdp, &src);
2417 if (edst != 0 || esrc != 0) {
2419 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
2422 bzero(&sn, sizeof (sn));
2423 dt_node_type_assign(&sn, src.dtt_ctfp, src.dtt_type);
2425 bzero(&dn, sizeof (dn));
2426 dt_node_type_assign(&dn, dst.dtt_ctfp, dst.dtt_type);
2428 if (dt_xlator_lookup(dtp, &sn, &dn, DT_XLATE_EXACT) != NULL) {
2429 xyerror(D_XLATE_REDECL,
2430 "translator from %s to %s has already been declared\n",
2431 dt_node_type_name(&sn, n1, sizeof (n1)),
2432 dt_node_type_name(&dn, n2, sizeof (n2)));
2435 kind = ctf_type_kind(dst.dtt_ctfp,
2436 ctf_type_resolve(dst.dtt_ctfp, dst.dtt_type));
2438 if (kind == CTF_K_FORWARD) {
2439 xyerror(D_XLATE_SOU, "incomplete struct/union/enum %s\n",
2440 dt_type_name(dst.dtt_ctfp, dst.dtt_type, n1, sizeof (n1)));
2443 if (kind != CTF_K_STRUCT && kind != CTF_K_UNION) {
2444 xyerror(D_XLATE_SOU,
2445 "translator output type must be a struct or union\n");
2448 dxp = dt_xlator_create(dtp, &src, &dst, name, members, yypcb->pcb_list);
2449 yybegin(YYS_CLAUSE);
2453 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2455 dnp = dt_node_alloc(DT_NODE_XLATOR);
2456 dnp->dn_xlator = dxp;
2457 dnp->dn_members = members;
2459 return (dt_node_cook(dnp, DT_IDFLG_REF));
2463 dt_node_probe(char *s, int protoc, dt_node_t *nargs, dt_node_t *xargs)
2465 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
2469 size_t len = strlen(s) + 3; /* +3 for :: and \0 */
2470 char *name = alloca(len);
2472 (void) snprintf(name, len, "::%s", s);
2473 (void) strhyphenate(name);
2476 if (strchr(name, '`') != NULL) {
2477 xyerror(D_PROV_BADNAME, "probe name may not "
2478 "contain scoping operator: %s\n", name);
2481 if (strlen(name) - 2 >= DTRACE_NAMELEN) {
2482 xyerror(D_PROV_BADNAME, "probe name may not exceed %d "
2483 "characters: %s\n", DTRACE_NAMELEN - 1, name);
2486 dnp = dt_node_alloc(DT_NODE_PROBE);
2488 dnp->dn_ident = dt_ident_create(name, DT_IDENT_PROBE,
2489 DT_IDFLG_ORPHAN, DTRACE_IDNONE, _dtrace_defattr, 0,
2490 &dt_idops_probe, NULL, dtp->dt_gen);
2492 nargc = dt_decl_prototype(nargs, nargs,
2493 "probe input", DT_DP_VOID | DT_DP_ANON);
2495 xargc = dt_decl_prototype(xargs, nargs,
2496 "probe output", DT_DP_VOID);
2498 if (nargc > UINT8_MAX) {
2499 xyerror(D_PROV_PRARGLEN, "probe %s input prototype exceeds %u "
2500 "parameters: %d params used\n", name, UINT8_MAX, nargc);
2503 if (xargc > UINT8_MAX) {
2504 xyerror(D_PROV_PRARGLEN, "probe %s output prototype exceeds %u "
2505 "parameters: %d params used\n", name, UINT8_MAX, xargc);
2508 if (dnp->dn_ident == NULL || dt_probe_create(dtp,
2509 dnp->dn_ident, protoc, nargs, nargc, xargs, xargc) == NULL)
2510 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2516 dt_node_provider(char *name, dt_node_t *probes)
2518 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
2519 dt_node_t *dnp = dt_node_alloc(DT_NODE_PROVIDER);
2523 dnp->dn_provname = name;
2524 dnp->dn_probes = probes;
2526 if (strchr(name, '`') != NULL) {
2527 dnerror(dnp, D_PROV_BADNAME, "provider name may not "
2528 "contain scoping operator: %s\n", name);
2531 if ((len = strlen(name)) >= DTRACE_PROVNAMELEN) {
2532 dnerror(dnp, D_PROV_BADNAME, "provider name may not exceed %d "
2533 "characters: %s\n", DTRACE_PROVNAMELEN - 1, name);
2536 if (isdigit(name[len - 1])) {
2537 dnerror(dnp, D_PROV_BADNAME, "provider name may not "
2538 "end with a digit: %s\n", name);
2542 * Check to see if the provider is already defined or visible through
2543 * dtrace(7D). If so, set dn_provred to treat it as a re-declaration.
2544 * If not, create a new provider and set its interface-only flag. This
2545 * flag may be cleared later by calls made to dt_probe_declare().
2547 if ((dnp->dn_provider = dt_provider_lookup(dtp, name)) != NULL)
2548 dnp->dn_provred = B_TRUE;
2549 else if ((dnp->dn_provider = dt_provider_create(dtp, name)) == NULL)
2550 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2552 dnp->dn_provider->pv_flags |= DT_PROVIDER_INTF;
2555 * Store all parse nodes created since we consumed the DT_KEY_PROVIDER
2556 * token with the provider and then restore our lexing state to CLAUSE.
2557 * Note that if dnp->dn_provred is true, we may end up storing dups of
2558 * a provider's interface and implementation: we eat this space because
2559 * the implementation will likely need to redeclare probe members, and
2560 * therefore may result in those member nodes becoming persistent.
2562 for (lnp = yypcb->pcb_list; lnp->dn_link != NULL; lnp = lnp->dn_link)
2563 continue; /* skip to end of allocation list */
2565 lnp->dn_link = dnp->dn_provider->pv_nodes;
2566 dnp->dn_provider->pv_nodes = yypcb->pcb_list;
2568 yybegin(YYS_CLAUSE);
2573 dt_node_program(dt_node_t *lnp)
2575 dt_node_t *dnp = dt_node_alloc(DT_NODE_PROG);
2581 * This function provides the underlying implementation of cooking an
2582 * identifier given its node, a hash of dynamic identifiers, an identifier
2583 * kind, and a boolean flag indicating whether we are allowed to instantiate
2584 * a new identifier if the string is not found. This function is either
2585 * called from dt_cook_ident(), below, or directly by the various cooking
2586 * routines that are allowed to instantiate identifiers (e.g. op2 TOK_ASGN).
2589 dt_xcook_ident(dt_node_t *dnp, dt_idhash_t *dhp, uint_t idkind, int create)
2591 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
2592 const char *sname = dt_idhash_name(dhp);
2595 dtrace_attribute_t attr = _dtrace_defattr;
2597 dtrace_syminfo_t dts;
2600 const char *scope, *mark;
2605 * Look for scoping marks in the identifier. If one is found, set our
2606 * scope to either DTRACE_OBJ_KMODS or UMODS or to the first part of
2607 * the string that specifies the scope using an explicit module name.
2608 * If two marks in a row are found, set 'uref' (user symbol reference).
2609 * Otherwise we set scope to DTRACE_OBJ_EXEC, indicating that normal
2610 * scope is desired and we should search the specified idhash.
2612 if ((name = strrchr(dnp->dn_string, '`')) != NULL) {
2613 if (name > dnp->dn_string && name[-1] == '`') {
2618 if (name == dnp->dn_string + uref)
2619 scope = uref ? DTRACE_OBJ_UMODS : DTRACE_OBJ_KMODS;
2621 scope = dnp->dn_string;
2623 *name++ = '\0'; /* leave name pointing after scoping mark */
2624 dnkind = DT_NODE_VAR;
2626 } else if (idkind == DT_IDENT_AGG) {
2627 scope = DTRACE_OBJ_EXEC;
2628 name = dnp->dn_string + 1;
2629 dnkind = DT_NODE_AGG;
2631 scope = DTRACE_OBJ_EXEC;
2632 name = dnp->dn_string;
2633 dnkind = DT_NODE_VAR;
2637 * If create is set to false, and we fail our idhash lookup, preset
2638 * the errno code to EDT_NOVAR for our final error message below.
2639 * If we end up calling dtrace_lookup_by_name(), it will reset the
2640 * errno appropriately and that error will be reported instead.
2642 (void) dt_set_errno(dtp, EDT_NOVAR);
2643 mark = uref ? "``" : "`";
2645 if (scope == DTRACE_OBJ_EXEC && (
2646 (dhp != dtp->dt_globals &&
2647 (idp = dt_idhash_lookup(dhp, name)) != NULL) ||
2648 (dhp == dtp->dt_globals &&
2649 (idp = dt_idstack_lookup(&yypcb->pcb_globals, name)) != NULL))) {
2651 * Check that we are referencing the ident in the manner that
2652 * matches its type if this is a global lookup. In the TLS or
2653 * local case, we don't know how the ident will be used until
2654 * the time operator -> is seen; more parsing is needed.
2656 if (idp->di_kind != idkind && dhp == dtp->dt_globals) {
2657 xyerror(D_IDENT_BADREF, "%s '%s' may not be referenced "
2658 "as %s\n", dt_idkind_name(idp->di_kind),
2659 idp->di_name, dt_idkind_name(idkind));
2663 * Arrays and aggregations are not cooked individually. They
2664 * have dynamic types and must be referenced using operator [].
2665 * This is handled explicitly by the code for DT_TOK_LBRAC.
2667 if (idp->di_kind != DT_IDENT_ARRAY &&
2668 idp->di_kind != DT_IDENT_AGG)
2669 attr = dt_ident_cook(dnp, idp, NULL);
2671 dt_node_type_assign(dnp,
2672 DT_DYN_CTFP(dtp), DT_DYN_TYPE(dtp));
2673 attr = idp->di_attr;
2676 free(dnp->dn_string);
2677 dnp->dn_string = NULL;
2678 dnp->dn_kind = dnkind;
2679 dnp->dn_ident = idp;
2680 dnp->dn_flags |= DT_NF_LVALUE;
2682 if (idp->di_flags & DT_IDFLG_WRITE)
2683 dnp->dn_flags |= DT_NF_WRITABLE;
2685 dt_node_attr_assign(dnp, attr);
2687 } else if (dhp == dtp->dt_globals && scope != DTRACE_OBJ_EXEC &&
2688 dtrace_lookup_by_name(dtp, scope, name, &sym, &dts) == 0) {
2690 dt_module_t *mp = dt_module_lookup_by_name(dtp, dts.dts_object);
2691 int umod = (mp->dm_flags & DT_DM_KERNEL) == 0;
2692 static const char *const kunames[] = { "kernel", "user" };
2694 dtrace_typeinfo_t dtt;
2695 dtrace_syminfo_t *sip;
2698 xyerror(D_SYM_BADREF, "%s module '%s' symbol '%s' may "
2699 "not be referenced as a %s symbol\n", kunames[umod],
2700 dts.dts_object, dts.dts_name, kunames[uref]);
2703 if (dtrace_symbol_type(dtp, &sym, &dts, &dtt) != 0) {
2705 * For now, we special-case EDT_DATAMODEL to clarify
2706 * that mixed data models are not currently supported.
2708 if (dtp->dt_errno == EDT_DATAMODEL) {
2709 xyerror(D_SYM_MODEL, "cannot use %s symbol "
2710 "%s%s%s in a %s D program\n",
2711 dt_module_modelname(mp),
2712 dts.dts_object, mark, dts.dts_name,
2713 dt_module_modelname(dtp->dt_ddefs));
2716 xyerror(D_SYM_NOTYPES,
2717 "no symbolic type information is available for "
2718 "%s%s%s: %s\n", dts.dts_object, mark, dts.dts_name,
2719 dtrace_errmsg(dtp, dtrace_errno(dtp)));
2722 idp = dt_ident_create(name, DT_IDENT_SYMBOL, 0, 0,
2723 _dtrace_symattr, 0, &dt_idops_thaw, NULL, dtp->dt_gen);
2726 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2728 if (mp->dm_flags & DT_DM_PRIMARY)
2729 idp->di_flags |= DT_IDFLG_PRIM;
2731 idp->di_next = dtp->dt_externs;
2732 dtp->dt_externs = idp;
2734 if ((sip = malloc(sizeof (dtrace_syminfo_t))) == NULL)
2735 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2737 bcopy(&dts, sip, sizeof (dtrace_syminfo_t));
2739 idp->di_ctfp = dtt.dtt_ctfp;
2740 idp->di_type = dtt.dtt_type;
2742 free(dnp->dn_string);
2743 dnp->dn_string = NULL;
2744 dnp->dn_kind = DT_NODE_SYM;
2745 dnp->dn_ident = idp;
2746 dnp->dn_flags |= DT_NF_LVALUE;
2748 dt_node_type_assign(dnp, dtt.dtt_ctfp, dtt.dtt_type);
2749 dt_node_attr_assign(dnp, _dtrace_symattr);
2752 idp->di_flags |= DT_IDFLG_USER;
2753 dnp->dn_flags |= DT_NF_USERLAND;
2756 } else if (scope == DTRACE_OBJ_EXEC && create == B_TRUE) {
2757 uint_t flags = DT_IDFLG_WRITE;
2760 if (dt_idhash_nextid(dhp, &id) == -1) {
2761 xyerror(D_ID_OFLOW, "cannot create %s: limit on number "
2762 "of %s variables exceeded\n", name, sname);
2765 if (dhp == yypcb->pcb_locals)
2766 flags |= DT_IDFLG_LOCAL;
2767 else if (dhp == dtp->dt_tls)
2768 flags |= DT_IDFLG_TLS;
2770 dt_dprintf("create %s %s variable %s, id=%u\n",
2771 sname, dt_idkind_name(idkind), name, id);
2773 if (idkind == DT_IDENT_ARRAY || idkind == DT_IDENT_AGG) {
2774 idp = dt_idhash_insert(dhp, name,
2775 idkind, flags, id, _dtrace_defattr, 0,
2776 &dt_idops_assc, NULL, dtp->dt_gen);
2778 idp = dt_idhash_insert(dhp, name,
2779 idkind, flags, id, _dtrace_defattr, 0,
2780 &dt_idops_thaw, NULL, dtp->dt_gen);
2784 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2787 * Arrays and aggregations are not cooked individually. They
2788 * have dynamic types and must be referenced using operator [].
2789 * This is handled explicitly by the code for DT_TOK_LBRAC.
2791 if (idp->di_kind != DT_IDENT_ARRAY &&
2792 idp->di_kind != DT_IDENT_AGG)
2793 attr = dt_ident_cook(dnp, idp, NULL);
2795 dt_node_type_assign(dnp,
2796 DT_DYN_CTFP(dtp), DT_DYN_TYPE(dtp));
2797 attr = idp->di_attr;
2800 free(dnp->dn_string);
2801 dnp->dn_string = NULL;
2802 dnp->dn_kind = dnkind;
2803 dnp->dn_ident = idp;
2804 dnp->dn_flags |= DT_NF_LVALUE | DT_NF_WRITABLE;
2806 dt_node_attr_assign(dnp, attr);
2808 } else if (scope != DTRACE_OBJ_EXEC) {
2809 xyerror(D_IDENT_UNDEF, "failed to resolve %s%s%s: %s\n",
2810 dnp->dn_string, mark, name,
2811 dtrace_errmsg(dtp, dtrace_errno(dtp)));
2813 xyerror(D_IDENT_UNDEF, "failed to resolve %s: %s\n",
2814 dnp->dn_string, dtrace_errmsg(dtp, dtrace_errno(dtp)));
2819 dt_cook_ident(dt_node_t *dnp, uint_t idflags)
2821 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
2823 if (dnp->dn_op == DT_TOK_AGG)
2824 dt_xcook_ident(dnp, dtp->dt_aggs, DT_IDENT_AGG, B_FALSE);
2826 dt_xcook_ident(dnp, dtp->dt_globals, DT_IDENT_SCALAR, B_FALSE);
2828 return (dt_node_cook(dnp, idflags));
2832 * Since operators [ and -> can instantiate new variables before we know
2833 * whether the reference is for a read or a write, we need to check read
2834 * references to determine if the identifier is currently dt_ident_unref().
2835 * If so, we report that this first access was to an undefined variable.
2838 dt_cook_var(dt_node_t *dnp, uint_t idflags)
2840 dt_ident_t *idp = dnp->dn_ident;
2842 if ((idflags & DT_IDFLG_REF) && dt_ident_unref(idp)) {
2843 dnerror(dnp, D_VAR_UNDEF,
2844 "%s%s has not yet been declared or assigned\n",
2845 (idp->di_flags & DT_IDFLG_LOCAL) ? "this->" :
2846 (idp->di_flags & DT_IDFLG_TLS) ? "self->" : "",
2850 dt_node_attr_assign(dnp, dt_ident_cook(dnp, idp, &dnp->dn_args));
2856 dt_cook_func(dt_node_t *dnp, uint_t idflags)
2858 dt_node_attr_assign(dnp,
2859 dt_ident_cook(dnp, dnp->dn_ident, &dnp->dn_args));
2865 dt_cook_op1(dt_node_t *dnp, uint_t idflags)
2867 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
2868 dt_node_t *cp = dnp->dn_child;
2870 char n[DT_TYPE_NAMELEN];
2871 dtrace_typeinfo_t dtt;
2876 ctf_id_t type, base;
2879 if (dnp->dn_op == DT_TOK_PREINC || dnp->dn_op == DT_TOK_POSTINC ||
2880 dnp->dn_op == DT_TOK_PREDEC || dnp->dn_op == DT_TOK_POSTDEC)
2881 idflags = DT_IDFLG_REF | DT_IDFLG_MOD;
2883 idflags = DT_IDFLG_REF;
2886 * We allow the unary ++ and -- operators to instantiate new scalar
2887 * variables if applied to an identifier; otherwise just cook as usual.
2889 if (cp->dn_kind == DT_NODE_IDENT && (idflags & DT_IDFLG_MOD))
2890 dt_xcook_ident(cp, dtp->dt_globals, DT_IDENT_SCALAR, B_TRUE);
2892 cp = dnp->dn_child = dt_node_cook(cp, 0); /* don't set idflags yet */
2894 if (cp->dn_kind == DT_NODE_VAR && dt_ident_unref(cp->dn_ident)) {
2895 if (dt_type_lookup("int64_t", &dtt) != 0)
2896 xyerror(D_TYPE_ERR, "failed to lookup int64_t\n");
2898 dt_ident_type_assign(cp->dn_ident, dtt.dtt_ctfp, dtt.dtt_type);
2899 dt_node_type_assign(cp, dtt.dtt_ctfp, dtt.dtt_type);
2902 if (cp->dn_kind == DT_NODE_VAR)
2903 cp->dn_ident->di_flags |= idflags;
2905 switch (dnp->dn_op) {
2908 * If the deref operator is applied to a translated pointer,
2909 * we set our output type to the output of the translation.
2911 if ((idp = dt_node_resolve(cp, DT_IDENT_XLPTR)) != NULL) {
2912 dt_xlator_t *dxp = idp->di_data;
2914 dnp->dn_ident = &dxp->dx_souid;
2915 dt_node_type_assign(dnp,
2916 dnp->dn_ident->di_ctfp, dnp->dn_ident->di_type);
2920 type = ctf_type_resolve(cp->dn_ctfp, cp->dn_type);
2921 kind = ctf_type_kind(cp->dn_ctfp, type);
2923 if (kind == CTF_K_ARRAY) {
2924 if (ctf_array_info(cp->dn_ctfp, type, &r) != 0) {
2925 dtp->dt_ctferr = ctf_errno(cp->dn_ctfp);
2926 longjmp(yypcb->pcb_jmpbuf, EDT_CTF);
2928 type = r.ctr_contents;
2929 } else if (kind == CTF_K_POINTER) {
2930 type = ctf_type_reference(cp->dn_ctfp, type);
2932 xyerror(D_DEREF_NONPTR,
2933 "cannot dereference non-pointer type\n");
2936 dt_node_type_assign(dnp, cp->dn_ctfp, type);
2937 base = ctf_type_resolve(cp->dn_ctfp, type);
2938 kind = ctf_type_kind(cp->dn_ctfp, base);
2940 if (kind == CTF_K_INTEGER && ctf_type_encoding(cp->dn_ctfp,
2941 base, &e) == 0 && IS_VOID(e)) {
2942 xyerror(D_DEREF_VOID,
2943 "cannot dereference pointer to void\n");
2946 if (kind == CTF_K_FUNCTION) {
2947 xyerror(D_DEREF_FUNC,
2948 "cannot dereference pointer to function\n");
2951 if (kind != CTF_K_ARRAY || dt_node_is_string(dnp))
2952 dnp->dn_flags |= DT_NF_LVALUE; /* see K&R[A7.4.3] */
2955 * If we propagated the l-value bit and the child operand was
2956 * a writable D variable or a binary operation of the form
2957 * a + b where a is writable, then propagate the writable bit.
2958 * This is necessary to permit assignments to scalar arrays,
2959 * which are converted to expressions of the form *(a + i).
2961 if ((cp->dn_flags & DT_NF_WRITABLE) ||
2962 (cp->dn_kind == DT_NODE_OP2 && cp->dn_op == DT_TOK_ADD &&
2963 (cp->dn_left->dn_flags & DT_NF_WRITABLE)))
2964 dnp->dn_flags |= DT_NF_WRITABLE;
2966 if ((cp->dn_flags & DT_NF_USERLAND) &&
2967 (kind == CTF_K_POINTER || (dnp->dn_flags & DT_NF_REF)))
2968 dnp->dn_flags |= DT_NF_USERLAND;
2973 if (!dt_node_is_arith(cp)) {
2974 xyerror(D_OP_ARITH, "operator %s requires an operand "
2975 "of arithmetic type\n", opstr(dnp->dn_op));
2977 dt_node_type_propagate(cp, dnp); /* see K&R[A7.4.4-6] */
2981 if (!dt_node_is_integer(cp)) {
2982 xyerror(D_OP_INT, "operator %s requires an operand of "
2983 "integral type\n", opstr(dnp->dn_op));
2985 dt_node_type_propagate(cp, dnp); /* see K&R[A7.4.4-6] */
2989 if (!dt_node_is_scalar(cp)) {
2990 xyerror(D_OP_SCALAR, "operator %s requires an operand "
2991 "of scalar type\n", opstr(dnp->dn_op));
2993 dt_node_type_assign(dnp, DT_INT_CTFP(dtp), DT_INT_TYPE(dtp));
2997 if (cp->dn_kind == DT_NODE_VAR || cp->dn_kind == DT_NODE_AGG) {
2998 xyerror(D_ADDROF_VAR,
2999 "cannot take address of dynamic variable\n");
3002 if (dt_node_is_dynamic(cp)) {
3003 xyerror(D_ADDROF_VAR,
3004 "cannot take address of dynamic object\n");
3007 if (!(cp->dn_flags & DT_NF_LVALUE)) {
3008 xyerror(D_ADDROF_LVAL, /* see K&R[A7.4.2] */
3009 "unacceptable operand for unary & operator\n");
3012 if (cp->dn_flags & DT_NF_BITFIELD) {
3013 xyerror(D_ADDROF_BITFIELD,
3014 "cannot take address of bit-field\n");
3017 dtt.dtt_object = NULL;
3018 dtt.dtt_ctfp = cp->dn_ctfp;
3019 dtt.dtt_type = cp->dn_type;
3021 if (dt_type_pointer(&dtt) == -1) {
3022 xyerror(D_TYPE_ERR, "cannot find type for \"&\": %s*\n",
3023 dt_node_type_name(cp, n, sizeof (n)));
3026 dt_node_type_assign(dnp, dtt.dtt_ctfp, dtt.dtt_type);
3028 if (cp->dn_flags & DT_NF_USERLAND)
3029 dnp->dn_flags |= DT_NF_USERLAND;
3033 if (cp->dn_flags & DT_NF_BITFIELD) {
3034 xyerror(D_SIZEOF_BITFIELD,
3035 "cannot apply sizeof to a bit-field\n");
3038 if (dt_node_sizeof(cp) == 0) {
3039 xyerror(D_SIZEOF_TYPE, "cannot apply sizeof to an "
3040 "operand of unknown size\n");
3043 dt_node_type_assign(dnp, dtp->dt_ddefs->dm_ctfp,
3044 ctf_lookup_by_name(dtp->dt_ddefs->dm_ctfp, "size_t"));
3047 case DT_TOK_STRINGOF:
3048 if (!dt_node_is_scalar(cp) && !dt_node_is_pointer(cp) &&
3049 !dt_node_is_strcompat(cp)) {
3050 xyerror(D_STRINGOF_TYPE,
3051 "cannot apply stringof to a value of type %s\n",
3052 dt_node_type_name(cp, n, sizeof (n)));
3054 dt_node_type_assign(dnp, DT_STR_CTFP(dtp), DT_STR_TYPE(dtp));
3058 case DT_TOK_POSTINC:
3060 case DT_TOK_POSTDEC:
3061 if (dt_node_is_scalar(cp) == 0) {
3062 xyerror(D_OP_SCALAR, "operator %s requires operand of "
3063 "scalar type\n", opstr(dnp->dn_op));
3066 if (dt_node_is_vfptr(cp)) {
3067 xyerror(D_OP_VFPTR, "operator %s requires an operand "
3068 "of known size\n", opstr(dnp->dn_op));
3071 if (!(cp->dn_flags & DT_NF_LVALUE)) {
3072 xyerror(D_OP_LVAL, "operator %s requires modifiable "
3073 "lvalue as an operand\n", opstr(dnp->dn_op));
3076 if (!(cp->dn_flags & DT_NF_WRITABLE)) {
3077 xyerror(D_OP_WRITE, "operator %s can only be applied "
3078 "to a writable variable\n", opstr(dnp->dn_op));
3081 dt_node_type_propagate(cp, dnp); /* see K&R[A7.4.1] */
3085 xyerror(D_UNKNOWN, "invalid unary op %s\n", opstr(dnp->dn_op));
3088 dt_node_attr_assign(dnp, cp->dn_attr);
3093 dt_assign_common(dt_node_t *dnp)
3095 dt_node_t *lp = dnp->dn_left;
3096 dt_node_t *rp = dnp->dn_right;
3097 int op = dnp->dn_op;
3099 if (rp->dn_kind == DT_NODE_INT)
3102 if (!(lp->dn_flags & DT_NF_LVALUE)) {
3103 xyerror(D_OP_LVAL, "operator %s requires modifiable "
3104 "lvalue as an operand\n", opstr(op));
3105 /* see K&R[A7.17] */
3108 if (!(lp->dn_flags & DT_NF_WRITABLE)) {
3109 xyerror(D_OP_WRITE, "operator %s can only be applied "
3110 "to a writable variable\n", opstr(op));
3113 dt_node_type_propagate(lp, dnp); /* see K&R[A7.17] */
3114 dt_node_attr_assign(dnp, dt_attr_min(lp->dn_attr, rp->dn_attr));
3118 dt_cook_op2(dt_node_t *dnp, uint_t idflags)
3120 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
3121 dt_node_t *lp = dnp->dn_left;
3122 dt_node_t *rp = dnp->dn_right;
3123 int op = dnp->dn_op;
3128 int kind, val, uref;
3131 char n1[DT_TYPE_NAMELEN];
3132 char n2[DT_TYPE_NAMELEN];
3135 * The expression E1[E2] is identical by definition to *((E1)+(E2)) so
3136 * we convert "[" to "+" and glue on "*" at the end (see K&R[A7.3.1])
3137 * unless the left-hand side is an untyped D scalar, associative array,
3138 * or aggregation. In these cases, we proceed to case DT_TOK_LBRAC and
3139 * handle associative array and aggregation references there.
3141 if (op == DT_TOK_LBRAC) {
3142 if (lp->dn_kind == DT_NODE_IDENT) {
3146 if (lp->dn_op == DT_TOK_AGG) {
3148 idp = dt_idhash_lookup(dhp, lp->dn_string + 1);
3149 idkind = DT_IDENT_AGG;
3151 dhp = dtp->dt_globals;
3152 idp = dt_idstack_lookup(
3153 &yypcb->pcb_globals, lp->dn_string);
3154 idkind = DT_IDENT_ARRAY;
3157 if (idp == NULL || dt_ident_unref(idp))
3158 dt_xcook_ident(lp, dhp, idkind, B_TRUE);
3160 dt_xcook_ident(lp, dhp, idp->di_kind, B_FALSE);
3162 lp = dnp->dn_left = dt_node_cook(lp, 0);
3165 * Switch op to '+' for *(E1 + E2) array mode in these cases:
3166 * (a) lp is a DT_IDENT_ARRAY variable that has already been
3167 * referenced using [] notation (dn_args != NULL).
3168 * (b) lp is a non-ARRAY variable that has already been given
3169 * a type by assignment or declaration (!dt_ident_unref())
3170 * (c) lp is neither a variable nor an aggregation
3172 if (lp->dn_kind == DT_NODE_VAR) {
3173 if (lp->dn_ident->di_kind == DT_IDENT_ARRAY) {
3174 if (lp->dn_args != NULL)
3176 } else if (!dt_ident_unref(lp->dn_ident))
3178 } else if (lp->dn_kind != DT_NODE_AGG)
3186 lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF);
3187 rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
3189 if (!dt_node_is_integer(lp) || !dt_node_is_integer(rp)) {
3190 xyerror(D_OP_INT, "operator %s requires operands of "
3191 "integral type\n", opstr(op));
3194 dt_node_promote(lp, rp, dnp); /* see K&R[A7.11-13] */
3199 lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF);
3200 rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
3202 if (!dt_node_is_integer(lp) || !dt_node_is_integer(rp)) {
3203 xyerror(D_OP_INT, "operator %s requires operands of "
3204 "integral type\n", opstr(op));
3207 dt_node_type_propagate(lp, dnp); /* see K&R[A7.8] */
3208 dt_node_attr_assign(dnp, dt_attr_min(lp->dn_attr, rp->dn_attr));
3212 lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF);
3213 rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
3215 if (!dt_node_is_integer(lp) || !dt_node_is_integer(rp)) {
3216 xyerror(D_OP_INT, "operator %s requires operands of "
3217 "integral type\n", opstr(op));
3220 dt_node_promote(lp, rp, dnp); /* see K&R[A7.6] */
3225 lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF);
3226 rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
3228 if (!dt_node_is_arith(lp) || !dt_node_is_arith(rp)) {
3229 xyerror(D_OP_ARITH, "operator %s requires operands of "
3230 "arithmetic type\n", opstr(op));
3233 dt_node_promote(lp, rp, dnp); /* see K&R[A7.6] */
3239 lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF);
3240 rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
3242 if (!dt_node_is_scalar(lp) || !dt_node_is_scalar(rp)) {
3243 xyerror(D_OP_SCALAR, "operator %s requires operands "
3244 "of scalar type\n", opstr(op));
3247 dt_node_type_assign(dnp, DT_INT_CTFP(dtp), DT_INT_TYPE(dtp));
3248 dt_node_attr_assign(dnp, dt_attr_min(lp->dn_attr, rp->dn_attr));
3258 * The D comparison operators provide the ability to transform
3259 * a right-hand identifier into a corresponding enum tag value
3260 * if the left-hand side is an enum type. To do this, we cook
3261 * the left-hand side, and then see if the right-hand side is
3262 * an unscoped identifier defined in the enum. If so, we
3263 * convert into an integer constant node with the tag's value.
3265 lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF);
3267 kind = ctf_type_kind(lp->dn_ctfp,
3268 ctf_type_resolve(lp->dn_ctfp, lp->dn_type));
3270 if (kind == CTF_K_ENUM && rp->dn_kind == DT_NODE_IDENT &&
3271 strchr(rp->dn_string, '`') == NULL && ctf_enum_value(
3272 lp->dn_ctfp, lp->dn_type, rp->dn_string, &val) == 0) {
3274 if ((idp = dt_idstack_lookup(&yypcb->pcb_globals,
3275 rp->dn_string)) != NULL) {
3276 xyerror(D_IDENT_AMBIG,
3277 "ambiguous use of operator %s: %s is "
3278 "both a %s enum tag and a global %s\n",
3279 opstr(op), rp->dn_string,
3280 dt_node_type_name(lp, n1, sizeof (n1)),
3281 dt_idkind_name(idp->di_kind));
3284 free(rp->dn_string);
3285 rp->dn_string = NULL;
3286 rp->dn_kind = DT_NODE_INT;
3287 rp->dn_flags |= DT_NF_COOKED;
3288 rp->dn_op = DT_TOK_INT;
3289 rp->dn_value = (intmax_t)val;
3291 dt_node_type_assign(rp, lp->dn_ctfp, lp->dn_type);
3292 dt_node_attr_assign(rp, _dtrace_symattr);
3295 rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
3298 * The rules for type checking for the relational operators are
3299 * described in the ANSI-C spec (see K&R[A7.9-10]). We perform
3300 * the various tests in order from least to most expensive. We
3301 * also allow derived strings to be compared as a first-class
3302 * type (resulting in a strcmp(3C)-style comparison), and we
3303 * slightly relax the A7.9 rules to permit void pointer
3304 * comparisons as in A7.10. Our users won't be confused by
3305 * this since they understand pointers are just numbers, and
3306 * relaxing this constraint simplifies the implementation.
3308 if (ctf_type_compat(lp->dn_ctfp, lp->dn_type,
3309 rp->dn_ctfp, rp->dn_type))
3311 else if (dt_node_is_integer(lp) && dt_node_is_integer(rp))
3313 else if (dt_node_is_strcompat(lp) && dt_node_is_strcompat(rp) &&
3314 (dt_node_is_string(lp) || dt_node_is_string(rp)))
3316 else if (dt_node_is_ptrcompat(lp, rp, NULL, NULL) == 0) {
3317 xyerror(D_OP_INCOMPAT, "operands have "
3318 "incompatible types: \"%s\" %s \"%s\"\n",
3319 dt_node_type_name(lp, n1, sizeof (n1)), opstr(op),
3320 dt_node_type_name(rp, n2, sizeof (n2)));
3323 dt_node_type_assign(dnp, DT_INT_CTFP(dtp), DT_INT_TYPE(dtp));
3324 dt_node_attr_assign(dnp, dt_attr_min(lp->dn_attr, rp->dn_attr));
3330 * The rules for type checking for the additive operators are
3331 * described in the ANSI-C spec (see K&R[A7.7]). Pointers and
3332 * integers may be manipulated according to specific rules. In
3333 * these cases D permits strings to be treated as pointers.
3335 int lp_is_ptr, lp_is_int, rp_is_ptr, rp_is_int;
3337 lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF);
3338 rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
3340 lp_is_ptr = dt_node_is_string(lp) ||
3341 (dt_node_is_pointer(lp) && !dt_node_is_vfptr(lp));
3342 lp_is_int = dt_node_is_integer(lp);
3344 rp_is_ptr = dt_node_is_string(rp) ||
3345 (dt_node_is_pointer(rp) && !dt_node_is_vfptr(rp));
3346 rp_is_int = dt_node_is_integer(rp);
3348 if (lp_is_int && rp_is_int) {
3349 dt_type_promote(lp, rp, &ctfp, &type);
3351 } else if (lp_is_ptr && rp_is_int) {
3354 uref = lp->dn_flags & DT_NF_USERLAND;
3355 } else if (lp_is_int && rp_is_ptr && op == DT_TOK_ADD) {
3358 uref = rp->dn_flags & DT_NF_USERLAND;
3359 } else if (lp_is_ptr && rp_is_ptr && op == DT_TOK_SUB &&
3360 dt_node_is_ptrcompat(lp, rp, NULL, NULL)) {
3361 ctfp = dtp->dt_ddefs->dm_ctfp;
3362 type = ctf_lookup_by_name(ctfp, "ptrdiff_t");
3365 xyerror(D_OP_INCOMPAT, "operands have incompatible "
3366 "types: \"%s\" %s \"%s\"\n",
3367 dt_node_type_name(lp, n1, sizeof (n1)), opstr(op),
3368 dt_node_type_name(rp, n2, sizeof (n2)));
3371 dt_node_type_assign(dnp, ctfp, type);
3372 dt_node_attr_assign(dnp, dt_attr_min(lp->dn_attr, rp->dn_attr));
3375 dnp->dn_flags |= DT_NF_USERLAND;
3385 if (lp->dn_kind == DT_NODE_IDENT) {
3386 dt_xcook_ident(lp, dtp->dt_globals,
3387 DT_IDENT_SCALAR, B_TRUE);
3391 dt_node_cook(lp, DT_IDFLG_REF | DT_IDFLG_MOD);
3393 rp = dnp->dn_right =
3394 dt_node_cook(rp, DT_IDFLG_REF | DT_IDFLG_MOD);
3396 if (!dt_node_is_integer(lp) || !dt_node_is_integer(rp)) {
3397 xyerror(D_OP_INT, "operator %s requires operands of "
3398 "integral type\n", opstr(op));
3404 if (lp->dn_kind == DT_NODE_IDENT) {
3405 dt_xcook_ident(lp, dtp->dt_globals,
3406 DT_IDENT_SCALAR, B_TRUE);
3410 dt_node_cook(lp, DT_IDFLG_REF | DT_IDFLG_MOD);
3412 rp = dnp->dn_right =
3413 dt_node_cook(rp, DT_IDFLG_REF | DT_IDFLG_MOD);
3415 if (!dt_node_is_arith(lp) || !dt_node_is_arith(rp)) {
3416 xyerror(D_OP_ARITH, "operator %s requires operands of "
3417 "arithmetic type\n", opstr(op));
3423 * If the left-hand side is an identifier, attempt to resolve
3424 * it as either an aggregation or scalar variable. We pass
3425 * B_TRUE to dt_xcook_ident to indicate that a new variable can
3426 * be created if no matching variable exists in the namespace.
3428 if (lp->dn_kind == DT_NODE_IDENT) {
3429 if (lp->dn_op == DT_TOK_AGG) {
3430 dt_xcook_ident(lp, dtp->dt_aggs,
3431 DT_IDENT_AGG, B_TRUE);
3433 dt_xcook_ident(lp, dtp->dt_globals,
3434 DT_IDENT_SCALAR, B_TRUE);
3438 lp = dnp->dn_left = dt_node_cook(lp, 0); /* don't set mod yet */
3439 rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
3442 * If the left-hand side is an aggregation, verify that we are
3443 * assigning it the result of an aggregating function. Once
3444 * we've done so, hide the func node in the aggregation and
3445 * return the aggregation itself up to the parse tree parent.
3446 * This transformation is legal since the assigned function
3447 * cannot change identity across disjoint cooking passes and
3448 * the argument list subtree is retained for later cooking.
3450 if (lp->dn_kind == DT_NODE_AGG) {
3451 const char *aname = lp->dn_ident->di_name;
3452 dt_ident_t *oid = lp->dn_ident->di_iarg;
3454 if (rp->dn_kind != DT_NODE_FUNC ||
3455 rp->dn_ident->di_kind != DT_IDENT_AGGFUNC) {
3457 "@%s must be assigned the result of "
3458 "an aggregating function\n", aname);
3461 if (oid != NULL && oid != rp->dn_ident) {
3462 xyerror(D_AGG_REDEF,
3463 "aggregation redefined: @%s\n\t "
3464 "current: @%s = %s( )\n\tprevious: @%s = "
3465 "%s( ) : line %d\n", aname, aname,
3466 rp->dn_ident->di_name, aname, oid->di_name,
3467 lp->dn_ident->di_lineno);
3468 } else if (oid == NULL)
3469 lp->dn_ident->di_iarg = rp->dn_ident;
3472 * Do not allow multiple aggregation assignments in a
3473 * single statement, e.g. (@a = count()) = count();
3474 * We produce a message as if the result of aggregating
3475 * function does not propagate DT_NF_LVALUE.
3477 if (lp->dn_aggfun != NULL) {
3478 xyerror(D_OP_LVAL, "operator = requires "
3479 "modifiable lvalue as an operand\n");
3483 lp = dt_node_cook(lp, DT_IDFLG_MOD);
3485 dnp->dn_left = dnp->dn_right = NULL;
3492 * If the right-hand side is a dynamic variable that is the
3493 * output of a translator, our result is the translated type.
3495 if ((idp = dt_node_resolve(rp, DT_IDENT_XLSOU)) != NULL) {
3496 ctfp = idp->di_ctfp;
3497 type = idp->di_type;
3498 uref = idp->di_flags & DT_IDFLG_USER;
3502 uref = rp->dn_flags & DT_NF_USERLAND;
3506 * If the left-hand side of an assignment statement is a virgin
3507 * variable created by this compilation pass, reset the type of
3508 * this variable to the type of the right-hand side.
3510 if (lp->dn_kind == DT_NODE_VAR &&
3511 dt_ident_unref(lp->dn_ident)) {
3512 dt_node_type_assign(lp, ctfp, type);
3513 dt_ident_type_assign(lp->dn_ident, ctfp, type);
3516 lp->dn_flags |= DT_NF_USERLAND;
3517 lp->dn_ident->di_flags |= DT_IDFLG_USER;
3521 if (lp->dn_kind == DT_NODE_VAR)
3522 lp->dn_ident->di_flags |= DT_IDFLG_MOD;
3525 * The rules for type checking for the assignment operators are
3526 * described in the ANSI-C spec (see K&R[A7.17]). We share
3527 * most of this code with the argument list checking code.
3529 if (!dt_node_is_string(lp)) {
3530 kind = ctf_type_kind(lp->dn_ctfp,
3531 ctf_type_resolve(lp->dn_ctfp, lp->dn_type));
3533 if (kind == CTF_K_ARRAY || kind == CTF_K_FUNCTION) {
3534 xyerror(D_OP_ARRFUN, "operator %s may not be "
3535 "applied to operand of type \"%s\"\n",
3537 dt_node_type_name(lp, n1, sizeof (n1)));
3541 if (idp != NULL && idp->di_kind == DT_IDENT_XLSOU &&
3542 ctf_type_compat(lp->dn_ctfp, lp->dn_type, ctfp, type))
3545 if (dt_node_is_argcompat(lp, rp))
3548 xyerror(D_OP_INCOMPAT,
3549 "operands have incompatible types: \"%s\" %s \"%s\"\n",
3550 dt_node_type_name(lp, n1, sizeof (n1)), opstr(op),
3551 dt_node_type_name(rp, n2, sizeof (n2)));
3556 if (lp->dn_kind == DT_NODE_IDENT) {
3557 dt_xcook_ident(lp, dtp->dt_globals,
3558 DT_IDENT_SCALAR, B_TRUE);
3562 dt_node_cook(lp, DT_IDFLG_REF | DT_IDFLG_MOD);
3564 rp = dnp->dn_right =
3565 dt_node_cook(rp, DT_IDFLG_REF | DT_IDFLG_MOD);
3567 if (dt_node_is_string(lp) || dt_node_is_string(rp)) {
3568 xyerror(D_OP_INCOMPAT, "operands have "
3569 "incompatible types: \"%s\" %s \"%s\"\n",
3570 dt_node_type_name(lp, n1, sizeof (n1)), opstr(op),
3571 dt_node_type_name(rp, n2, sizeof (n2)));
3575 * The rules for type checking for the assignment operators are
3576 * described in the ANSI-C spec (see K&R[A7.17]). To these
3577 * rules we add that only writable D nodes can be modified.
3579 if (dt_node_is_integer(lp) == 0 ||
3580 dt_node_is_integer(rp) == 0) {
3581 if (!dt_node_is_pointer(lp) || dt_node_is_vfptr(lp)) {
3583 "operator %s requires left-hand scalar "
3584 "operand of known size\n", opstr(op));
3585 } else if (dt_node_is_integer(rp) == 0 &&
3586 dt_node_is_ptrcompat(lp, rp, NULL, NULL) == 0) {
3587 xyerror(D_OP_INCOMPAT, "operands have "
3588 "incompatible types: \"%s\" %s \"%s\"\n",
3589 dt_node_type_name(lp, n1, sizeof (n1)),
3591 dt_node_type_name(rp, n2, sizeof (n2)));
3595 dt_assign_common(dnp);
3600 * If the left-hand side of operator -> is the name "self",
3601 * then we permit a TLS variable to be created or referenced.
3603 if (lp->dn_kind == DT_NODE_IDENT &&
3604 strcmp(lp->dn_string, "self") == 0) {
3605 if (rp->dn_kind != DT_NODE_VAR) {
3606 dt_xcook_ident(rp, dtp->dt_tls,
3607 DT_IDENT_SCALAR, B_TRUE);
3611 rp = dt_node_cook(rp, idflags);
3613 dnp->dn_right = dnp->dn_left; /* avoid freeing rp */
3619 * If the left-hand side of operator -> is the name "this",
3620 * then we permit a local variable to be created or referenced.
3622 if (lp->dn_kind == DT_NODE_IDENT &&
3623 strcmp(lp->dn_string, "this") == 0) {
3624 if (rp->dn_kind != DT_NODE_VAR) {
3625 dt_xcook_ident(rp, yypcb->pcb_locals,
3626 DT_IDENT_SCALAR, B_TRUE);
3630 rp = dt_node_cook(rp, idflags);
3632 dnp->dn_right = dnp->dn_left; /* avoid freeing rp */
3640 lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF);
3642 if (rp->dn_kind != DT_NODE_IDENT) {
3643 xyerror(D_OP_IDENT, "operator %s must be followed by "
3644 "an identifier\n", opstr(op));
3647 if ((idp = dt_node_resolve(lp, DT_IDENT_XLSOU)) != NULL ||
3648 (idp = dt_node_resolve(lp, DT_IDENT_XLPTR)) != NULL) {
3650 * If the left-hand side is a translated struct or ptr,
3651 * the type of the left is the translation output type.
3653 dt_xlator_t *dxp = idp->di_data;
3655 if (dt_xlator_member(dxp, rp->dn_string) == NULL) {
3656 xyerror(D_XLATE_NOCONV,
3657 "translator does not define conversion "
3658 "for member: %s\n", rp->dn_string);
3661 ctfp = idp->di_ctfp;
3662 type = ctf_type_resolve(ctfp, idp->di_type);
3663 uref = idp->di_flags & DT_IDFLG_USER;
3666 type = ctf_type_resolve(ctfp, lp->dn_type);
3667 uref = lp->dn_flags & DT_NF_USERLAND;
3670 kind = ctf_type_kind(ctfp, type);
3672 if (op == DT_TOK_PTR) {
3673 if (kind != CTF_K_POINTER) {
3674 xyerror(D_OP_PTR, "operator %s must be "
3675 "applied to a pointer\n", opstr(op));
3677 type = ctf_type_reference(ctfp, type);
3678 type = ctf_type_resolve(ctfp, type);
3679 kind = ctf_type_kind(ctfp, type);
3683 * If we follow a reference to a forward declaration tag,
3684 * search the entire type space for the actual definition.
3686 while (kind == CTF_K_FORWARD) {
3687 char *tag = ctf_type_name(ctfp, type, n1, sizeof (n1));
3688 dtrace_typeinfo_t dtt;
3690 if (tag != NULL && dt_type_lookup(tag, &dtt) == 0 &&
3691 (dtt.dtt_ctfp != ctfp || dtt.dtt_type != type)) {
3692 ctfp = dtt.dtt_ctfp;
3693 type = ctf_type_resolve(ctfp, dtt.dtt_type);
3694 kind = ctf_type_kind(ctfp, type);
3696 xyerror(D_OP_INCOMPLETE,
3697 "operator %s cannot be applied to a "
3698 "forward declaration: no %s definition "
3699 "is available\n", opstr(op), tag);
3703 if (kind != CTF_K_STRUCT && kind != CTF_K_UNION) {
3704 if (op == DT_TOK_PTR) {
3705 xyerror(D_OP_SOU, "operator -> cannot be "
3706 "applied to pointer to type \"%s\"; must "
3707 "be applied to a struct or union pointer\n",
3708 ctf_type_name(ctfp, type, n1, sizeof (n1)));
3710 xyerror(D_OP_SOU, "operator %s cannot be "
3711 "applied to type \"%s\"; must be applied "
3712 "to a struct or union\n", opstr(op),
3713 ctf_type_name(ctfp, type, n1, sizeof (n1)));
3717 if (ctf_member_info(ctfp, type, rp->dn_string, &m) == CTF_ERR) {
3718 xyerror(D_TYPE_MEMBER,
3719 "%s is not a member of %s\n", rp->dn_string,
3720 ctf_type_name(ctfp, type, n1, sizeof (n1)));
3723 type = ctf_type_resolve(ctfp, m.ctm_type);
3724 kind = ctf_type_kind(ctfp, type);
3726 dt_node_type_assign(dnp, ctfp, m.ctm_type);
3727 dt_node_attr_assign(dnp, lp->dn_attr);
3729 if (op == DT_TOK_PTR && (kind != CTF_K_ARRAY ||
3730 dt_node_is_string(dnp)))
3731 dnp->dn_flags |= DT_NF_LVALUE; /* see K&R[A7.3.3] */
3733 if (op == DT_TOK_DOT && (lp->dn_flags & DT_NF_LVALUE) &&
3734 (kind != CTF_K_ARRAY || dt_node_is_string(dnp)))
3735 dnp->dn_flags |= DT_NF_LVALUE; /* see K&R[A7.3.3] */
3737 if (lp->dn_flags & DT_NF_WRITABLE)
3738 dnp->dn_flags |= DT_NF_WRITABLE;
3740 if (uref && (kind == CTF_K_POINTER ||
3741 (dnp->dn_flags & DT_NF_REF)))
3742 dnp->dn_flags |= DT_NF_USERLAND;
3745 case DT_TOK_LBRAC: {
3747 * If op is DT_TOK_LBRAC, we know from the special-case code at
3748 * the top that lp is either a D variable or an aggregation.
3753 * If the left-hand side is an aggregation, just set dn_aggtup
3754 * to the right-hand side and return the cooked aggregation.
3755 * This transformation is legal since we are just collapsing
3756 * nodes to simplify later processing, and the entire aggtup
3757 * parse subtree is retained for subsequent cooking passes.
3759 if (lp->dn_kind == DT_NODE_AGG) {
3760 if (lp->dn_aggtup != NULL) {
3761 xyerror(D_AGG_MDIM, "improper attempt to "
3762 "reference @%s as a multi-dimensional "
3763 "array\n", lp->dn_ident->di_name);
3767 lp = dt_node_cook(lp, 0);
3769 dnp->dn_left = dnp->dn_right = NULL;
3775 assert(lp->dn_kind == DT_NODE_VAR);
3779 * If the left-hand side is a non-global scalar that hasn't yet
3780 * been referenced or modified, it was just created by self->
3781 * or this-> and we can convert it from scalar to assoc array.
3783 if (idp->di_kind == DT_IDENT_SCALAR && dt_ident_unref(idp) &&
3784 (idp->di_flags & (DT_IDFLG_LOCAL | DT_IDFLG_TLS)) != 0) {
3786 if (idp->di_flags & DT_IDFLG_LOCAL) {
3787 xyerror(D_ARR_LOCAL,
3788 "local variables may not be used as "
3789 "associative arrays: %s\n", idp->di_name);
3792 dt_dprintf("morph variable %s (id %u) from scalar to "
3793 "array\n", idp->di_name, idp->di_id);
3795 dt_ident_morph(idp, DT_IDENT_ARRAY,
3796 &dt_idops_assc, NULL);
3799 if (idp->di_kind != DT_IDENT_ARRAY) {
3800 xyerror(D_IDENT_BADREF, "%s '%s' may not be referenced "
3801 "as %s\n", dt_idkind_name(idp->di_kind),
3802 idp->di_name, dt_idkind_name(DT_IDENT_ARRAY));
3806 * Now that we've confirmed our left-hand side is a DT_NODE_VAR
3807 * of idkind DT_IDENT_ARRAY, we need to splice the [ node from
3808 * the parse tree and leave a cooked DT_NODE_VAR in its place
3809 * where dn_args for the VAR node is the right-hand 'rp' tree,
3810 * as shown in the parse tree diagram below:
3813 * [ OP2 "[" ]=dnp [ VAR ]=dnp
3815 * / \ +- dn_args -> [ ??? ]=rp
3816 * [ VAR ]=lp [ ??? ]=rp
3818 * Since the final dt_node_cook(dnp) can fail using longjmp we
3819 * must perform the transformations as a group first by over-
3820 * writing 'dnp' to become the VAR node, so that the parse tree
3821 * is guaranteed to be in a consistent state if the cook fails.
3823 assert(lp->dn_kind == DT_NODE_VAR);
3824 assert(lp->dn_args == NULL);
3827 bcopy(lp, dnp, sizeof (dt_node_t));
3831 dnp->dn_list = NULL;
3834 return (dt_node_cook(dnp, idflags));
3837 case DT_TOK_XLATE: {
3840 assert(lp->dn_kind == DT_NODE_TYPE);
3841 rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
3842 dxp = dt_xlator_lookup(dtp, rp, lp, DT_XLATE_FUZZY);
3845 xyerror(D_XLATE_NONE,
3846 "cannot translate from \"%s\" to \"%s\"\n",
3847 dt_node_type_name(rp, n1, sizeof (n1)),
3848 dt_node_type_name(lp, n2, sizeof (n2)));
3851 dnp->dn_ident = dt_xlator_ident(dxp, lp->dn_ctfp, lp->dn_type);
3852 dt_node_type_assign(dnp, DT_DYN_CTFP(dtp), DT_DYN_TYPE(dtp));
3853 dt_node_attr_assign(dnp,
3854 dt_attr_min(rp->dn_attr, dnp->dn_ident->di_attr));
3859 ctf_id_t ltype, rtype;
3860 uint_t lkind, rkind;
3862 assert(lp->dn_kind == DT_NODE_TYPE);
3863 rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
3865 ltype = ctf_type_resolve(lp->dn_ctfp, lp->dn_type);
3866 lkind = ctf_type_kind(lp->dn_ctfp, ltype);
3868 rtype = ctf_type_resolve(rp->dn_ctfp, rp->dn_type);
3869 rkind = ctf_type_kind(rp->dn_ctfp, rtype);
3872 * The rules for casting are loosely explained in K&R[A7.5]
3873 * and K&R[A6]. Basically, we can cast to the same type or
3874 * same base type, between any kind of scalar values, from
3875 * arrays to pointers, and we can cast anything to void.
3876 * To these rules D adds casts from scalars to strings.
3878 if (ctf_type_compat(lp->dn_ctfp, lp->dn_type,
3879 rp->dn_ctfp, rp->dn_type))
3881 else if (dt_node_is_scalar(lp) &&
3882 (dt_node_is_scalar(rp) || rkind == CTF_K_FUNCTION))
3884 else if (dt_node_is_void(lp))
3886 else if (lkind == CTF_K_POINTER && dt_node_is_pointer(rp))
3888 else if (dt_node_is_string(lp) && (dt_node_is_scalar(rp) ||
3889 dt_node_is_pointer(rp) || dt_node_is_strcompat(rp)))
3892 xyerror(D_CAST_INVAL,
3893 "invalid cast expression: \"%s\" to \"%s\"\n",
3894 dt_node_type_name(rp, n1, sizeof (n1)),
3895 dt_node_type_name(lp, n2, sizeof (n2)));
3898 dt_node_type_propagate(lp, dnp); /* see K&R[A7.5] */
3899 dt_node_attr_assign(dnp, dt_attr_min(lp->dn_attr, rp->dn_attr));
3902 * If it's a pointer then should be able to (attempt to)
3905 if (lkind == CTF_K_POINTER)
3906 dnp->dn_flags |= DT_NF_WRITABLE;
3912 lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF);
3913 rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
3915 if (dt_node_is_dynamic(lp) || dt_node_is_dynamic(rp)) {
3916 xyerror(D_OP_DYN, "operator %s operands "
3917 "cannot be of dynamic type\n", opstr(op));
3920 if (dt_node_is_actfunc(lp) || dt_node_is_actfunc(rp)) {
3921 xyerror(D_OP_ACT, "operator %s operands "
3922 "cannot be actions\n", opstr(op));
3925 dt_node_type_propagate(rp, dnp); /* see K&R[A7.18] */
3926 dt_node_attr_assign(dnp, dt_attr_min(lp->dn_attr, rp->dn_attr));
3930 xyerror(D_UNKNOWN, "invalid binary op %s\n", opstr(op));
3934 * Complete the conversion of E1[E2] to *((E1)+(E2)) that we started
3935 * at the top of our switch() above (see K&R[A7.3.1]). Since E2 is
3936 * parsed as an argument_expression_list by dt_grammar.y, we can
3937 * end up with a comma-separated list inside of a non-associative
3938 * array reference. We check for this and report an appropriate error.
3940 if (dnp->dn_op == DT_TOK_LBRAC && op == DT_TOK_ADD) {
3943 if (rp->dn_list != NULL) {
3944 xyerror(D_ARR_BADREF,
3945 "cannot access %s as an associative array\n",
3946 dt_node_name(lp, n1, sizeof (n1)));
3949 dnp->dn_op = DT_TOK_ADD;
3950 pnp = dt_node_op1(DT_TOK_DEREF, dnp);
3953 * Cook callbacks are not typically permitted to allocate nodes.
3954 * When we do, we must insert them in the middle of an existing
3955 * allocation list rather than having them appended to the pcb
3956 * list because the sub-expression may be part of a definition.
3958 assert(yypcb->pcb_list == pnp);
3959 yypcb->pcb_list = pnp->dn_link;
3961 pnp->dn_link = dnp->dn_link;
3964 return (dt_node_cook(pnp, DT_IDFLG_REF));
3972 dt_cook_op3(dt_node_t *dnp, uint_t idflags)
3978 dnp->dn_expr = dt_node_cook(dnp->dn_expr, DT_IDFLG_REF);
3979 lp = dnp->dn_left = dt_node_cook(dnp->dn_left, DT_IDFLG_REF);
3980 rp = dnp->dn_right = dt_node_cook(dnp->dn_right, DT_IDFLG_REF);
3982 if (!dt_node_is_scalar(dnp->dn_expr)) {
3983 xyerror(D_OP_SCALAR,
3984 "operator ?: expression must be of scalar type\n");
3987 if (dt_node_is_dynamic(lp) || dt_node_is_dynamic(rp)) {
3989 "operator ?: operands cannot be of dynamic type\n");
3993 * The rules for type checking for the ternary operator are complex and
3994 * are described in the ANSI-C spec (see K&R[A7.16]). We implement
3995 * the various tests in order from least to most expensive.
3997 if (ctf_type_compat(lp->dn_ctfp, lp->dn_type,
3998 rp->dn_ctfp, rp->dn_type)) {
4001 } else if (dt_node_is_integer(lp) && dt_node_is_integer(rp)) {
4002 dt_type_promote(lp, rp, &ctfp, &type);
4003 } else if (dt_node_is_strcompat(lp) && dt_node_is_strcompat(rp) &&
4004 (dt_node_is_string(lp) || dt_node_is_string(rp))) {
4005 ctfp = DT_STR_CTFP(yypcb->pcb_hdl);
4006 type = DT_STR_TYPE(yypcb->pcb_hdl);
4007 } else if (dt_node_is_ptrcompat(lp, rp, &ctfp, &type) == 0) {
4008 xyerror(D_OP_INCOMPAT,
4009 "operator ?: operands must have compatible types\n");
4012 if (dt_node_is_actfunc(lp) || dt_node_is_actfunc(rp)) {
4013 xyerror(D_OP_ACT, "action cannot be "
4014 "used in a conditional context\n");
4017 dt_node_type_assign(dnp, ctfp, type);
4018 dt_node_attr_assign(dnp, dt_attr_min(dnp->dn_expr->dn_attr,
4019 dt_attr_min(lp->dn_attr, rp->dn_attr)));
4025 dt_cook_statement(dt_node_t *dnp, uint_t idflags)
4027 dnp->dn_expr = dt_node_cook(dnp->dn_expr, idflags);
4028 dt_node_attr_assign(dnp, dnp->dn_expr->dn_attr);
4034 * If dn_aggfun is set, this node is a collapsed aggregation assignment (see
4035 * the special case code for DT_TOK_ASGN in dt_cook_op2() above), in which
4036 * case we cook both the tuple and the function call. If dn_aggfun is NULL,
4037 * this node is just a reference to the aggregation's type and attributes.
4041 dt_cook_aggregation(dt_node_t *dnp, uint_t idflags)
4043 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
4045 if (dnp->dn_aggfun != NULL) {
4046 dnp->dn_aggfun = dt_node_cook(dnp->dn_aggfun, DT_IDFLG_REF);
4047 dt_node_attr_assign(dnp, dt_ident_cook(dnp,
4048 dnp->dn_ident, &dnp->dn_aggtup));
4050 dt_node_type_assign(dnp, DT_DYN_CTFP(dtp), DT_DYN_TYPE(dtp));
4051 dt_node_attr_assign(dnp, dnp->dn_ident->di_attr);
4058 * Since D permits new variable identifiers to be instantiated in any program
4059 * expression, we may need to cook a clause's predicate either before or after
4060 * the action list depending on the program code in question. Consider:
4062 * probe-description-list probe-description-list
4065 * trace(x); trace(x++);
4068 * In the left-hand example, the predicate uses operator ++ to instantiate 'x'
4069 * as a variable of type int64_t. The predicate must be cooked first because
4070 * otherwise the statement trace(x) refers to an unknown identifier. In the
4071 * right-hand example, the action list uses ++ to instantiate 'x'; the action
4072 * list must be cooked first because otherwise the predicate x == 0 refers to
4073 * an unknown identifier. In order to simplify programming, we support both.
4075 * When cooking a clause, we cook the action statements before the predicate by
4076 * default, since it seems more common to create or modify identifiers in the
4077 * action list. If cooking fails due to an unknown identifier, we attempt to
4078 * cook the predicate (i.e. do it first) and then go back and cook the actions.
4079 * If this, too, fails (or if we get an error other than D_IDENT_UNDEF) we give
4080 * up and report failure back to the user. There are five possible paths:
4082 * cook actions = OK, cook predicate = OK -> OK
4083 * cook actions = OK, cook predicate = ERR -> ERR
4084 * cook actions = ERR, cook predicate = ERR -> ERR
4085 * cook actions = ERR, cook predicate = OK, cook actions = OK -> OK
4086 * cook actions = ERR, cook predicate = OK, cook actions = ERR -> ERR
4088 * The programmer can still defeat our scheme by creating circular definition
4089 * dependencies between predicates and actions, as in this example clause:
4091 * probe-description-list
4097 * but it doesn't seem worth the complexity to handle such rare cases. The
4098 * user can simply use the D variable declaration syntax to work around them.
4101 dt_cook_clause(dt_node_t *dnp, uint_t idflags)
4103 volatile int err, tries;
4107 * Before assigning dn_ctxattr, temporarily assign the probe attribute
4108 * to 'dnp' itself to force an attribute check and minimum violation.
4110 dt_node_attr_assign(dnp, yypcb->pcb_pinfo.dtp_attr);
4111 dnp->dn_ctxattr = yypcb->pcb_pinfo.dtp_attr;
4113 bcopy(yypcb->pcb_jmpbuf, ojb, sizeof (jmp_buf));
4116 if (dnp->dn_pred != NULL && (err = setjmp(yypcb->pcb_jmpbuf)) != 0) {
4117 bcopy(ojb, yypcb->pcb_jmpbuf, sizeof (jmp_buf));
4118 if (tries++ != 0 || err != EDT_COMPILER || (
4119 yypcb->pcb_hdl->dt_errtag != dt_errtag(D_IDENT_UNDEF) &&
4120 yypcb->pcb_hdl->dt_errtag != dt_errtag(D_VAR_UNDEF)))
4121 longjmp(yypcb->pcb_jmpbuf, err);
4125 yylabel("action list");
4127 dt_node_attr_assign(dnp,
4128 dt_node_list_cook(&dnp->dn_acts, idflags));
4130 bcopy(ojb, yypcb->pcb_jmpbuf, sizeof (jmp_buf));
4134 if (dnp->dn_pred != NULL) {
4135 yylabel("predicate");
4137 dnp->dn_pred = dt_node_cook(dnp->dn_pred, idflags);
4138 dt_node_attr_assign(dnp,
4139 dt_attr_min(dnp->dn_attr, dnp->dn_pred->dn_attr));
4141 if (!dt_node_is_scalar(dnp->dn_pred)) {
4142 xyerror(D_PRED_SCALAR,
4143 "predicate result must be of scalar type\n");
4150 yylabel("action list");
4152 dt_node_attr_assign(dnp,
4153 dt_node_list_cook(&dnp->dn_acts, idflags));
4163 dt_cook_inline(dt_node_t *dnp, uint_t idflags)
4165 dt_idnode_t *inp = dnp->dn_ident->di_iarg;
4168 char n1[DT_TYPE_NAMELEN];
4169 char n2[DT_TYPE_NAMELEN];
4171 assert(dnp->dn_ident->di_flags & DT_IDFLG_INLINE);
4172 assert(inp->din_root->dn_flags & DT_NF_COOKED);
4175 * If we are inlining a translation, verify that the inline declaration
4176 * type exactly matches the type that is returned by the translation.
4177 * Otherwise just use dt_node_is_argcompat() to check the types.
4179 if ((rdp = dt_node_resolve(inp->din_root, DT_IDENT_XLSOU)) != NULL ||
4180 (rdp = dt_node_resolve(inp->din_root, DT_IDENT_XLPTR)) != NULL) {
4182 ctf_file_t *lctfp = dnp->dn_ctfp;
4183 ctf_id_t ltype = ctf_type_resolve(lctfp, dnp->dn_type);
4185 dt_xlator_t *dxp = rdp->di_data;
4186 ctf_file_t *rctfp = dxp->dx_dst_ctfp;
4187 ctf_id_t rtype = dxp->dx_dst_base;
4189 if (ctf_type_kind(lctfp, ltype) == CTF_K_POINTER) {
4190 ltype = ctf_type_reference(lctfp, ltype);
4191 ltype = ctf_type_resolve(lctfp, ltype);
4194 if (ctf_type_compat(lctfp, ltype, rctfp, rtype) == 0) {
4195 dnerror(dnp, D_OP_INCOMPAT,
4196 "inline %s definition uses incompatible types: "
4197 "\"%s\" = \"%s\"\n", dnp->dn_ident->di_name,
4198 dt_type_name(lctfp, ltype, n1, sizeof (n1)),
4199 dt_type_name(rctfp, rtype, n2, sizeof (n2)));
4202 } else if (dt_node_is_argcompat(dnp, inp->din_root) == 0) {
4203 dnerror(dnp, D_OP_INCOMPAT,
4204 "inline %s definition uses incompatible types: "
4205 "\"%s\" = \"%s\"\n", dnp->dn_ident->di_name,
4206 dt_node_type_name(dnp, n1, sizeof (n1)),
4207 dt_node_type_name(inp->din_root, n2, sizeof (n2)));
4214 dt_cook_member(dt_node_t *dnp, uint_t idflags)
4216 dnp->dn_membexpr = dt_node_cook(dnp->dn_membexpr, idflags);
4217 dt_node_attr_assign(dnp, dnp->dn_membexpr->dn_attr);
4223 dt_cook_xlator(dt_node_t *dnp, uint_t idflags)
4225 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
4226 dt_xlator_t *dxp = dnp->dn_xlator;
4229 char n1[DT_TYPE_NAMELEN];
4230 char n2[DT_TYPE_NAMELEN];
4232 dtrace_attribute_t attr = _dtrace_maxattr;
4236 * Before cooking each translator member, we push a reference to the
4237 * hash containing translator-local identifiers on to pcb_globals to
4238 * temporarily interpose these identifiers in front of other globals.
4240 dt_idstack_push(&yypcb->pcb_globals, dxp->dx_locals);
4242 for (mnp = dnp->dn_members; mnp != NULL; mnp = mnp->dn_list) {
4243 if (ctf_member_info(dxp->dx_dst_ctfp, dxp->dx_dst_type,
4244 mnp->dn_membname, &ctm) == CTF_ERR) {
4245 xyerror(D_XLATE_MEMB,
4246 "translator member %s is not a member of %s\n",
4247 mnp->dn_membname, ctf_type_name(dxp->dx_dst_ctfp,
4248 dxp->dx_dst_type, n1, sizeof (n1)));
4251 (void) dt_node_cook(mnp, DT_IDFLG_REF);
4252 dt_node_type_assign(mnp, dxp->dx_dst_ctfp, ctm.ctm_type);
4253 attr = dt_attr_min(attr, mnp->dn_attr);
4255 if (dt_node_is_argcompat(mnp, mnp->dn_membexpr) == 0) {
4256 xyerror(D_XLATE_INCOMPAT,
4257 "translator member %s definition uses "
4258 "incompatible types: \"%s\" = \"%s\"\n",
4260 dt_node_type_name(mnp, n1, sizeof (n1)),
4261 dt_node_type_name(mnp->dn_membexpr,
4266 dt_idstack_pop(&yypcb->pcb_globals, dxp->dx_locals);
4268 dxp->dx_souid.di_attr = attr;
4269 dxp->dx_ptrid.di_attr = attr;
4271 dt_node_type_assign(dnp, DT_DYN_CTFP(dtp), DT_DYN_TYPE(dtp));
4272 dt_node_attr_assign(dnp, _dtrace_defattr);
4278 dt_node_provider_cmp_argv(dt_provider_t *pvp, dt_node_t *pnp, const char *kind,
4279 uint_t old_argc, dt_node_t *old_argv, uint_t new_argc, dt_node_t *new_argv)
4281 dt_probe_t *prp = pnp->dn_ident->di_data;
4284 char n1[DT_TYPE_NAMELEN];
4285 char n2[DT_TYPE_NAMELEN];
4287 if (old_argc != new_argc) {
4288 dnerror(pnp, D_PROV_INCOMPAT,
4289 "probe %s:%s %s prototype mismatch:\n"
4290 "\t current: %u arg%s\n\tprevious: %u arg%s\n",
4291 pvp->pv_desc.dtvd_name, prp->pr_ident->di_name, kind,
4292 new_argc, new_argc != 1 ? "s" : "",
4293 old_argc, old_argc != 1 ? "s" : "");
4296 for (i = 0; i < old_argc; i++,
4297 old_argv = old_argv->dn_list, new_argv = new_argv->dn_list) {
4298 if (ctf_type_cmp(old_argv->dn_ctfp, old_argv->dn_type,
4299 new_argv->dn_ctfp, new_argv->dn_type) == 0)
4302 dnerror(pnp, D_PROV_INCOMPAT,
4303 "probe %s:%s %s prototype argument #%u mismatch:\n"
4304 "\t current: %s\n\tprevious: %s\n",
4305 pvp->pv_desc.dtvd_name, prp->pr_ident->di_name, kind, i + 1,
4306 dt_node_type_name(new_argv, n1, sizeof (n1)),
4307 dt_node_type_name(old_argv, n2, sizeof (n2)));
4312 * Compare a new probe declaration with an existing probe definition (either
4313 * from a previous declaration or cached from the kernel). If the existing
4314 * definition and declaration both have an input and output parameter list,
4315 * compare both lists. Otherwise compare only the output parameter lists.
4318 dt_node_provider_cmp(dt_provider_t *pvp, dt_node_t *pnp,
4319 dt_probe_t *old, dt_probe_t *new)
4321 dt_node_provider_cmp_argv(pvp, pnp, "output",
4322 old->pr_xargc, old->pr_xargs, new->pr_xargc, new->pr_xargs);
4324 if (old->pr_nargs != old->pr_xargs && new->pr_nargs != new->pr_xargs) {
4325 dt_node_provider_cmp_argv(pvp, pnp, "input",
4326 old->pr_nargc, old->pr_nargs, new->pr_nargc, new->pr_nargs);
4329 if (old->pr_nargs == old->pr_xargs && new->pr_nargs != new->pr_xargs) {
4330 if (pvp->pv_flags & DT_PROVIDER_IMPL) {
4331 dnerror(pnp, D_PROV_INCOMPAT,
4332 "provider interface mismatch: %s\n"
4333 "\t current: probe %s:%s has an output prototype\n"
4334 "\tprevious: probe %s:%s has no output prototype\n",
4335 pvp->pv_desc.dtvd_name, pvp->pv_desc.dtvd_name,
4336 new->pr_ident->di_name, pvp->pv_desc.dtvd_name,
4337 old->pr_ident->di_name);
4340 if (old->pr_ident->di_gen == yypcb->pcb_hdl->dt_gen)
4341 old->pr_ident->di_flags |= DT_IDFLG_ORPHAN;
4343 dt_idhash_delete(pvp->pv_probes, old->pr_ident);
4344 dt_probe_declare(pvp, new);
4349 dt_cook_probe(dt_node_t *dnp, dt_provider_t *pvp)
4351 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
4352 dt_probe_t *prp = dnp->dn_ident->di_data;
4357 char n1[DT_TYPE_NAMELEN];
4358 char n2[DT_TYPE_NAMELEN];
4360 if (prp->pr_nargs == prp->pr_xargs)
4363 for (i = 0; i < prp->pr_xargc; i++) {
4364 dt_node_t *xnp = prp->pr_xargv[i];
4365 dt_node_t *nnp = prp->pr_nargv[prp->pr_mapping[i]];
4367 if ((dxp = dt_xlator_lookup(dtp,
4368 nnp, xnp, DT_XLATE_FUZZY)) != NULL) {
4369 if (dt_provider_xref(dtp, pvp, dxp->dx_id) != 0)
4370 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
4374 if (dt_node_is_argcompat(nnp, xnp))
4375 continue; /* no translator defined and none required */
4377 dnerror(dnp, D_PROV_PRXLATOR, "translator for %s:%s output "
4378 "argument #%u from %s to %s is not defined\n",
4379 pvp->pv_desc.dtvd_name, dnp->dn_ident->di_name, i + 1,
4380 dt_node_type_name(nnp, n1, sizeof (n1)),
4381 dt_node_type_name(xnp, n2, sizeof (n2)));
4387 dt_cook_provider(dt_node_t *dnp, uint_t idflags)
4389 dt_provider_t *pvp = dnp->dn_provider;
4393 * If we're declaring a provider for the first time and it is unknown
4394 * to dtrace(7D), insert the probe definitions into the provider's hash.
4395 * If we're redeclaring a known provider, verify the interface matches.
4397 for (pnp = dnp->dn_probes; pnp != NULL; pnp = pnp->dn_list) {
4398 const char *probename = pnp->dn_ident->di_name;
4399 dt_probe_t *prp = dt_probe_lookup(pvp, probename);
4401 assert(pnp->dn_kind == DT_NODE_PROBE);
4403 if (prp != NULL && dnp->dn_provred) {
4404 dt_node_provider_cmp(pvp, pnp,
4405 prp, pnp->dn_ident->di_data);
4406 } else if (prp == NULL && dnp->dn_provred) {
4407 dnerror(pnp, D_PROV_INCOMPAT,
4408 "provider interface mismatch: %s\n"
4409 "\t current: probe %s:%s defined\n"
4410 "\tprevious: probe %s:%s not defined\n",
4411 dnp->dn_provname, dnp->dn_provname,
4412 probename, dnp->dn_provname, probename);
4413 } else if (prp != NULL) {
4414 dnerror(pnp, D_PROV_PRDUP, "probe redeclared: %s:%s\n",
4415 dnp->dn_provname, probename);
4417 dt_probe_declare(pvp, pnp->dn_ident->di_data);
4419 dt_cook_probe(pnp, pvp);
4427 dt_cook_none(dt_node_t *dnp, uint_t idflags)
4432 static dt_node_t *(*dt_cook_funcs[])(dt_node_t *, uint_t) = {
4433 dt_cook_none, /* DT_NODE_FREE */
4434 dt_cook_none, /* DT_NODE_INT */
4435 dt_cook_none, /* DT_NODE_STRING */
4436 dt_cook_ident, /* DT_NODE_IDENT */
4437 dt_cook_var, /* DT_NODE_VAR */
4438 dt_cook_none, /* DT_NODE_SYM */
4439 dt_cook_none, /* DT_NODE_TYPE */
4440 dt_cook_func, /* DT_NODE_FUNC */
4441 dt_cook_op1, /* DT_NODE_OP1 */
4442 dt_cook_op2, /* DT_NODE_OP2 */
4443 dt_cook_op3, /* DT_NODE_OP3 */
4444 dt_cook_statement, /* DT_NODE_DEXPR */
4445 dt_cook_statement, /* DT_NODE_DFUNC */
4446 dt_cook_aggregation, /* DT_NODE_AGG */
4447 dt_cook_none, /* DT_NODE_PDESC */
4448 dt_cook_clause, /* DT_NODE_CLAUSE */
4449 dt_cook_inline, /* DT_NODE_INLINE */
4450 dt_cook_member, /* DT_NODE_MEMBER */
4451 dt_cook_xlator, /* DT_NODE_XLATOR */
4452 dt_cook_none, /* DT_NODE_PROBE */
4453 dt_cook_provider, /* DT_NODE_PROVIDER */
4454 dt_cook_none /* DT_NODE_PROG */
4458 * Recursively cook the parse tree starting at the specified node. The idflags
4459 * parameter is used to indicate the type of reference (r/w) and is applied to
4460 * the resulting identifier if it is a D variable or D aggregation.
4463 dt_node_cook(dt_node_t *dnp, uint_t idflags)
4465 int oldlineno = yylineno;
4467 yylineno = dnp->dn_line;
4469 dnp = dt_cook_funcs[dnp->dn_kind](dnp, idflags);
4470 dnp->dn_flags |= DT_NF_COOKED;
4472 if (dnp->dn_kind == DT_NODE_VAR || dnp->dn_kind == DT_NODE_AGG)
4473 dnp->dn_ident->di_flags |= idflags;
4475 yylineno = oldlineno;
4480 dt_node_list_cook(dt_node_t **pnp, uint_t idflags)
4482 dtrace_attribute_t attr = _dtrace_defattr;
4483 dt_node_t *dnp, *nnp;
4485 for (dnp = (pnp != NULL ? *pnp : NULL); dnp != NULL; dnp = nnp) {
4487 dnp = *pnp = dt_node_cook(dnp, idflags);
4488 attr = dt_attr_min(attr, dnp->dn_attr);
4490 pnp = &dnp->dn_list;
4497 dt_node_list_free(dt_node_t **pnp)
4499 dt_node_t *dnp, *nnp;
4501 for (dnp = (pnp != NULL ? *pnp : NULL); dnp != NULL; dnp = nnp) {
4511 dt_node_link_free(dt_node_t **pnp)
4513 dt_node_t *dnp, *nnp;
4515 for (dnp = (pnp != NULL ? *pnp : NULL); dnp != NULL; dnp = nnp) {
4520 for (dnp = (pnp != NULL ? *pnp : NULL); dnp != NULL; dnp = nnp) {
4530 dt_node_link(dt_node_t *lp, dt_node_t *rp)
4536 else if (rp == NULL)
4539 for (dnp = lp; dnp->dn_list != NULL; dnp = dnp->dn_list)
4547 * Compute the DOF dtrace_diftype_t representation of a node's type. This is
4548 * called from a variety of places in the library so it cannot assume yypcb
4549 * is valid: any references to handle-specific data must be made through 'dtp'.
4552 dt_node_diftype(dtrace_hdl_t *dtp, const dt_node_t *dnp, dtrace_diftype_t *tp)
4554 if (dnp->dn_ctfp == DT_STR_CTFP(dtp) &&
4555 dnp->dn_type == DT_STR_TYPE(dtp)) {
4556 tp->dtdt_kind = DIF_TYPE_STRING;
4557 tp->dtdt_ckind = CTF_K_UNKNOWN;
4559 tp->dtdt_kind = DIF_TYPE_CTF;
4560 tp->dtdt_ckind = ctf_type_kind(dnp->dn_ctfp,
4561 ctf_type_resolve(dnp->dn_ctfp, dnp->dn_type));
4564 tp->dtdt_flags = (dnp->dn_flags & DT_NF_REF) ? DIF_TF_BYREF : 0;
4566 tp->dtdt_size = ctf_type_size(dnp->dn_ctfp, dnp->dn_type);
4570 dt_node_printr(dt_node_t *dnp, FILE *fp, int depth)
4572 char n[DT_TYPE_NAMELEN], buf[BUFSIZ], a[8];
4573 const dtrace_syminfo_t *dts;
4574 const dt_idnode_t *inp;
4577 (void) fprintf(fp, "%*s", depth * 2, "");
4578 (void) dt_attr_str(dnp->dn_attr, a, sizeof (a));
4580 if (dnp->dn_ctfp != NULL && dnp->dn_type != CTF_ERR &&
4581 ctf_type_name(dnp->dn_ctfp, dnp->dn_type, n, sizeof (n)) != NULL) {
4582 (void) snprintf(buf, BUFSIZ, "type=<%s> attr=%s flags=", n, a);
4584 (void) snprintf(buf, BUFSIZ, "type=<%ld> attr=%s flags=",
4588 if (dnp->dn_flags != 0) {
4590 if (dnp->dn_flags & DT_NF_SIGNED)
4591 (void) strcat(n, ",SIGN");
4592 if (dnp->dn_flags & DT_NF_COOKED)
4593 (void) strcat(n, ",COOK");
4594 if (dnp->dn_flags & DT_NF_REF)
4595 (void) strcat(n, ",REF");
4596 if (dnp->dn_flags & DT_NF_LVALUE)
4597 (void) strcat(n, ",LVAL");
4598 if (dnp->dn_flags & DT_NF_WRITABLE)
4599 (void) strcat(n, ",WRITE");
4600 if (dnp->dn_flags & DT_NF_BITFIELD)
4601 (void) strcat(n, ",BITF");
4602 if (dnp->dn_flags & DT_NF_USERLAND)
4603 (void) strcat(n, ",USER");
4604 (void) strcat(buf, n + 1);
4606 (void) strcat(buf, "0");
4608 switch (dnp->dn_kind) {
4610 (void) fprintf(fp, "FREE <node %p>\n", (void *)dnp);
4614 (void) fprintf(fp, "INT 0x%llx (%s)\n",
4615 (u_longlong_t)dnp->dn_value, buf);
4618 case DT_NODE_STRING:
4619 (void) fprintf(fp, "STRING \"%s\" (%s)\n", dnp->dn_string, buf);
4623 (void) fprintf(fp, "IDENT %s (%s)\n", dnp->dn_string, buf);
4627 (void) fprintf(fp, "VARIABLE %s%s (%s)\n",
4628 (dnp->dn_ident->di_flags & DT_IDFLG_LOCAL) ? "this->" :
4629 (dnp->dn_ident->di_flags & DT_IDFLG_TLS) ? "self->" : "",
4630 dnp->dn_ident->di_name, buf);
4632 if (dnp->dn_args != NULL)
4633 (void) fprintf(fp, "%*s[\n", depth * 2, "");
4635 for (arg = dnp->dn_args; arg != NULL; arg = arg->dn_list) {
4636 dt_node_printr(arg, fp, depth + 1);
4637 if (arg->dn_list != NULL)
4638 (void) fprintf(fp, "%*s,\n", depth * 2, "");
4641 if (dnp->dn_args != NULL)
4642 (void) fprintf(fp, "%*s]\n", depth * 2, "");
4646 dts = dnp->dn_ident->di_data;
4647 (void) fprintf(fp, "SYMBOL %s`%s (%s)\n",
4648 dts->dts_object, dts->dts_name, buf);
4652 if (dnp->dn_string != NULL) {
4653 (void) fprintf(fp, "TYPE (%s) %s\n",
4654 buf, dnp->dn_string);
4656 (void) fprintf(fp, "TYPE (%s)\n", buf);
4660 (void) fprintf(fp, "FUNC %s (%s)\n",
4661 dnp->dn_ident->di_name, buf);
4663 for (arg = dnp->dn_args; arg != NULL; arg = arg->dn_list) {
4664 dt_node_printr(arg, fp, depth + 1);
4665 if (arg->dn_list != NULL)
4666 (void) fprintf(fp, "%*s,\n", depth * 2, "");
4671 (void) fprintf(fp, "OP1 %s (%s)\n", opstr(dnp->dn_op), buf);
4672 dt_node_printr(dnp->dn_child, fp, depth + 1);
4676 (void) fprintf(fp, "OP2 %s (%s)\n", opstr(dnp->dn_op), buf);
4677 dt_node_printr(dnp->dn_left, fp, depth + 1);
4678 dt_node_printr(dnp->dn_right, fp, depth + 1);
4682 (void) fprintf(fp, "OP3 (%s)\n", buf);
4683 dt_node_printr(dnp->dn_expr, fp, depth + 1);
4684 (void) fprintf(fp, "%*s?\n", depth * 2, "");
4685 dt_node_printr(dnp->dn_left, fp, depth + 1);
4686 (void) fprintf(fp, "%*s:\n", depth * 2, "");
4687 dt_node_printr(dnp->dn_right, fp, depth + 1);
4692 (void) fprintf(fp, "D EXPRESSION attr=%s\n", a);
4693 dt_node_printr(dnp->dn_expr, fp, depth + 1);
4697 (void) fprintf(fp, "AGGREGATE @%s attr=%s [\n",
4698 dnp->dn_ident->di_name, a);
4700 for (arg = dnp->dn_aggtup; arg != NULL; arg = arg->dn_list) {
4701 dt_node_printr(arg, fp, depth + 1);
4702 if (arg->dn_list != NULL)
4703 (void) fprintf(fp, "%*s,\n", depth * 2, "");
4706 if (dnp->dn_aggfun) {
4707 (void) fprintf(fp, "%*s] = ", depth * 2, "");
4708 dt_node_printr(dnp->dn_aggfun, fp, depth + 1);
4710 (void) fprintf(fp, "%*s]\n", depth * 2, "");
4713 (void) fprintf(fp, "%*s)\n", depth * 2, "");
4717 (void) fprintf(fp, "PDESC %s:%s:%s:%s [%u]\n",
4718 dnp->dn_desc->dtpd_provider, dnp->dn_desc->dtpd_mod,
4719 dnp->dn_desc->dtpd_func, dnp->dn_desc->dtpd_name,
4720 dnp->dn_desc->dtpd_id);
4723 case DT_NODE_CLAUSE:
4724 (void) fprintf(fp, "CLAUSE attr=%s\n", a);
4726 for (arg = dnp->dn_pdescs; arg != NULL; arg = arg->dn_list)
4727 dt_node_printr(arg, fp, depth + 1);
4729 (void) fprintf(fp, "%*sCTXATTR %s\n", depth * 2, "",
4730 dt_attr_str(dnp->dn_ctxattr, a, sizeof (a)));
4732 if (dnp->dn_pred != NULL) {
4733 (void) fprintf(fp, "%*sPREDICATE /\n", depth * 2, "");
4734 dt_node_printr(dnp->dn_pred, fp, depth + 1);
4735 (void) fprintf(fp, "%*s/\n", depth * 2, "");
4738 for (arg = dnp->dn_acts; arg != NULL; arg = arg->dn_list)
4739 dt_node_printr(arg, fp, depth + 1);
4742 case DT_NODE_INLINE:
4743 inp = dnp->dn_ident->di_iarg;
4745 (void) fprintf(fp, "INLINE %s (%s)\n",
4746 dnp->dn_ident->di_name, buf);
4747 dt_node_printr(inp->din_root, fp, depth + 1);
4750 case DT_NODE_MEMBER:
4751 (void) fprintf(fp, "MEMBER %s (%s)\n", dnp->dn_membname, buf);
4752 if (dnp->dn_membexpr)
4753 dt_node_printr(dnp->dn_membexpr, fp, depth + 1);
4756 case DT_NODE_XLATOR:
4757 (void) fprintf(fp, "XLATOR (%s)", buf);
4759 if (ctf_type_name(dnp->dn_xlator->dx_src_ctfp,
4760 dnp->dn_xlator->dx_src_type, n, sizeof (n)) != NULL)
4761 (void) fprintf(fp, " from <%s>", n);
4763 if (ctf_type_name(dnp->dn_xlator->dx_dst_ctfp,
4764 dnp->dn_xlator->dx_dst_type, n, sizeof (n)) != NULL)
4765 (void) fprintf(fp, " to <%s>", n);
4767 (void) fprintf(fp, "\n");
4769 for (arg = dnp->dn_members; arg != NULL; arg = arg->dn_list)
4770 dt_node_printr(arg, fp, depth + 1);
4774 (void) fprintf(fp, "PROBE %s\n", dnp->dn_ident->di_name);
4777 case DT_NODE_PROVIDER:
4778 (void) fprintf(fp, "PROVIDER %s (%s)\n",
4779 dnp->dn_provname, dnp->dn_provred ? "redecl" : "decl");
4780 for (arg = dnp->dn_probes; arg != NULL; arg = arg->dn_list)
4781 dt_node_printr(arg, fp, depth + 1);
4785 (void) fprintf(fp, "PROGRAM attr=%s\n", a);
4786 for (arg = dnp->dn_list; arg != NULL; arg = arg->dn_list)
4787 dt_node_printr(arg, fp, depth + 1);
4791 (void) fprintf(fp, "<bad node %p, kind %d>\n",
4792 (void *)dnp, dnp->dn_kind);
4797 dt_node_root(dt_node_t *dnp)
4799 yypcb->pcb_root = dnp;
4805 dnerror(const dt_node_t *dnp, dt_errtag_t tag, const char *format, ...)
4807 int oldlineno = yylineno;
4810 yylineno = dnp->dn_line;
4812 va_start(ap, format);
4813 xyvwarn(tag, format, ap);
4816 yylineno = oldlineno;
4817 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
4822 dnwarn(const dt_node_t *dnp, dt_errtag_t tag, const char *format, ...)
4824 int oldlineno = yylineno;
4827 yylineno = dnp->dn_line;
4829 va_start(ap, format);
4830 xyvwarn(tag, format, ap);
4833 yylineno = oldlineno;
4838 xyerror(dt_errtag_t tag, const char *format, ...)
4842 va_start(ap, format);
4843 xyvwarn(tag, format, ap);
4846 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
4851 xywarn(dt_errtag_t tag, const char *format, ...)
4855 va_start(ap, format);
4856 xyvwarn(tag, format, ap);
4861 xyvwarn(dt_errtag_t tag, const char *format, va_list ap)
4864 return; /* compiler is not currently active: act as a no-op */
4866 dt_set_errmsg(yypcb->pcb_hdl, dt_errtag(tag), yypcb->pcb_region,
4867 yypcb->pcb_filetag, yypcb->pcb_fileptr ? yylineno : 0, format, ap);
4872 yyerror(const char *format, ...)
4876 va_start(ap, format);
4877 yyvwarn(format, ap);
4880 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
4885 yywarn(const char *format, ...)
4889 va_start(ap, format);
4890 yyvwarn(format, ap);
4895 yyvwarn(const char *format, va_list ap)
4898 return; /* compiler is not currently active: act as a no-op */
4900 dt_set_errmsg(yypcb->pcb_hdl, dt_errtag(D_SYNTAX), yypcb->pcb_region,
4901 yypcb->pcb_filetag, yypcb->pcb_fileptr ? yylineno : 0, format, ap);
4903 if (strchr(format, '\n') == NULL) {
4904 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
4905 size_t len = strlen(dtp->dt_errmsg);
4906 char *p, *s = dtp->dt_errmsg + len;
4907 size_t n = sizeof (dtp->dt_errmsg) - len;
4909 if (yytext[0] == '\0')
4910 (void) snprintf(s, n, " near end of input");
4911 else if (yytext[0] == '\n')
4912 (void) snprintf(s, n, " near end of line");
4914 if ((p = strchr(yytext, '\n')) != NULL)
4915 *p = '\0'; /* crop at newline */
4916 (void) snprintf(s, n, " near \"%s\"", yytext);
4922 yylabel(const char *label)
4924 dt_dprintf("set label to <%s>\n", label ? label : "NULL");
4925 yypcb->pcb_region = label;
4931 return (1); /* indicate that lex should return a zero token for EOF */