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.
28 #pragma ident "%Z%%M% %I% %E% SMI"
31 * DTrace D Language Parser
33 * The D Parser is a lex/yacc parser consisting of the lexer dt_lex.l, the
34 * parsing grammar dt_grammar.y, and this file, dt_parser.c, which handles
35 * the construction of the parse tree nodes and their syntactic validation.
36 * The parse tree is constructed of dt_node_t structures (see <dt_parser.h>)
37 * that are built in two passes: (1) the "create" pass, where the parse tree
38 * nodes are allocated by calls from the grammar to dt_node_*() subroutines,
39 * and (2) the "cook" pass, where nodes are coalesced, assigned D types, and
40 * validated according to the syntactic rules of the language.
42 * All node allocations are performed using dt_node_alloc(). All node frees
43 * during the parsing phase are performed by dt_node_free(), which frees node-
44 * internal state but does not actually free the nodes. All final node frees
45 * are done as part of the end of dt_compile() or as part of destroying
46 * persistent identifiers or translators which have embedded nodes.
48 * The dt_node_* routines that implement pass (1) may allocate new nodes. The
49 * dt_cook_* routines that implement pass (2) may *not* allocate new nodes.
50 * They may free existing nodes using dt_node_free(), but they may not actually
51 * deallocate any dt_node_t's. Currently dt_cook_op2() is an exception to this
52 * rule: see the comments therein for how this issue is resolved.
54 * The dt_cook_* routines are responsible for (at minimum) setting the final
55 * node type (dn_ctfp/dn_type) and attributes (dn_attr). If dn_ctfp/dn_type
56 * are set manually (i.e. not by one of the type assignment functions), then
57 * the DT_NF_COOKED flag must be set manually on the node.
59 * The cooking pass can be applied to the same parse tree more than once (used
60 * in the case of a comma-separated list of probe descriptions). As such, the
61 * cook routines must not perform any parse tree transformations which would
62 * be invalid if the tree were subsequently cooked using a different context.
64 * The dn_ctfp and dn_type fields form the type of the node. This tuple can
65 * take on the following set of values, which form our type invariants:
67 * 1. dn_ctfp = NULL, dn_type = CTF_ERR
69 * In this state, the node has unknown type and is not yet cooked. The
70 * DT_NF_COOKED flag is not yet set on the node.
72 * 2. dn_ctfp = DT_DYN_CTFP(dtp), dn_type = DT_DYN_TYPE(dtp)
74 * In this state, the node is a dynamic D type. This means that generic
75 * operations are not valid on this node and only code that knows how to
76 * examine the inner details of the node can operate on it. A <DYN> node
77 * must have dn_ident set to point to an identifier describing the object
78 * and its type. The DT_NF_REF flag is set for all nodes of type <DYN>.
79 * At present, the D compiler uses the <DYN> type for:
81 * - associative arrays that do not yet have a value type defined
82 * - translated data (i.e. the result of the xlate operator)
85 * 3. dn_ctfp = DT_STR_CTFP(dtp), dn_type = DT_STR_TYPE(dtp)
87 * In this state, the node is of type D string. The string type is really
88 * a char[0] typedef, but requires special handling throughout the compiler.
90 * 4. dn_ctfp != NULL, dn_type = any other type ID
92 * In this state, the node is of some known D/CTF type. The normal libctf
93 * APIs can be used to learn more about the type name or structure. When
94 * the type is assigned, the DT_NF_SIGNED, DT_NF_REF, and DT_NF_BITFIELD
95 * flags cache the corresponding attributes of the underlying CTF type.
98 #include <sys/param.h>
113 #include <dt_grammar.h>
114 #include <dt_module.h>
115 #include <dt_provider.h>
116 #include <dt_string.h>
119 dt_pcb_t *yypcb; /* current control block for parser */
120 dt_node_t *yypragma; /* lex token list for control lines */
121 char yyintprefix; /* int token macro prefix (+/-) */
122 char yyintsuffix[4]; /* int token suffix string [uU][lL] */
123 int yyintdecimal; /* int token format flag (1=decimal, 0=octal/hex) */
129 case DT_TOK_COMMA: return (",");
130 case DT_TOK_ELLIPSIS: return ("...");
131 case DT_TOK_ASGN: return ("=");
132 case DT_TOK_ADD_EQ: return ("+=");
133 case DT_TOK_SUB_EQ: return ("-=");
134 case DT_TOK_MUL_EQ: return ("*=");
135 case DT_TOK_DIV_EQ: return ("/=");
136 case DT_TOK_MOD_EQ: return ("%=");
137 case DT_TOK_AND_EQ: return ("&=");
138 case DT_TOK_XOR_EQ: return ("^=");
139 case DT_TOK_OR_EQ: return ("|=");
140 case DT_TOK_LSH_EQ: return ("<<=");
141 case DT_TOK_RSH_EQ: return (">>=");
142 case DT_TOK_QUESTION: return ("?");
143 case DT_TOK_COLON: return (":");
144 case DT_TOK_LOR: return ("||");
145 case DT_TOK_LXOR: return ("^^");
146 case DT_TOK_LAND: return ("&&");
147 case DT_TOK_BOR: return ("|");
148 case DT_TOK_XOR: return ("^");
149 case DT_TOK_BAND: return ("&");
150 case DT_TOK_EQU: return ("==");
151 case DT_TOK_NEQ: return ("!=");
152 case DT_TOK_LT: return ("<");
153 case DT_TOK_LE: return ("<=");
154 case DT_TOK_GT: return (">");
155 case DT_TOK_GE: return (">=");
156 case DT_TOK_LSH: return ("<<");
157 case DT_TOK_RSH: return (">>");
158 case DT_TOK_ADD: return ("+");
159 case DT_TOK_SUB: return ("-");
160 case DT_TOK_MUL: return ("*");
161 case DT_TOK_DIV: return ("/");
162 case DT_TOK_MOD: return ("%");
163 case DT_TOK_LNEG: return ("!");
164 case DT_TOK_BNEG: return ("~");
165 case DT_TOK_ADDADD: return ("++");
166 case DT_TOK_PREINC: return ("++");
167 case DT_TOK_POSTINC: return ("++");
168 case DT_TOK_SUBSUB: return ("--");
169 case DT_TOK_PREDEC: return ("--");
170 case DT_TOK_POSTDEC: return ("--");
171 case DT_TOK_IPOS: return ("+");
172 case DT_TOK_INEG: return ("-");
173 case DT_TOK_DEREF: return ("*");
174 case DT_TOK_ADDROF: return ("&");
175 case DT_TOK_OFFSETOF: return ("offsetof");
176 case DT_TOK_SIZEOF: return ("sizeof");
177 case DT_TOK_STRINGOF: return ("stringof");
178 case DT_TOK_XLATE: return ("xlate");
179 case DT_TOK_LPAR: return ("(");
180 case DT_TOK_RPAR: return (")");
181 case DT_TOK_LBRAC: return ("[");
182 case DT_TOK_RBRAC: return ("]");
183 case DT_TOK_PTR: return ("->");
184 case DT_TOK_DOT: return (".");
185 case DT_TOK_STRING: return ("<string>");
186 case DT_TOK_IDENT: return ("<ident>");
187 case DT_TOK_TNAME: return ("<type>");
188 case DT_TOK_INT: return ("<int>");
189 default: return ("<?>");
194 dt_type_lookup(const char *s, dtrace_typeinfo_t *tip)
196 static const char delimiters[] = " \t\n\r\v\f*`";
197 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
198 const char *p, *q, *end, *obj;
200 for (p = s, end = s + strlen(s); *p != '\0'; p = q) {
202 p++; /* skip leading whitespace prior to token */
204 if (p == end || (q = strpbrk(p + 1, delimiters)) == NULL)
205 break; /* empty string or single token remaining */
208 char *object = alloca((size_t)(q - p) + 1);
209 char *type = alloca((size_t)(end - s) + 1);
212 * Copy from the start of the token (p) to the location
213 * backquote (q) to extract the nul-terminated object.
215 bcopy(p, object, (size_t)(q - p));
216 object[(size_t)(q - p)] = '\0';
219 * Copy the original string up to the start of this
220 * token (p) into type, and then concatenate everything
221 * after q. This is the type name without the object.
223 bcopy(s, type, (size_t)(p - s));
224 bcopy(q + 1, type + (size_t)(p - s), strlen(q + 1) + 1);
226 if (strchr(q + 1, '`') != NULL)
227 return (dt_set_errno(dtp, EDT_BADSCOPE));
229 return (dtrace_lookup_by_type(dtp, object, type, tip));
233 if (yypcb->pcb_idepth != 0)
234 obj = DTRACE_OBJ_CDEFS;
236 obj = DTRACE_OBJ_EVERY;
238 return (dtrace_lookup_by_type(dtp, obj, s, tip));
242 * When we parse type expressions or parse an expression with unary "&", we
243 * need to find a type that is a pointer to a previously known type.
244 * Unfortunately CTF is limited to a per-container view, so ctf_type_pointer()
245 * alone does not suffice for our needs. We provide a more intelligent wrapper
246 * for the compiler that attempts to compute a pointer to either the given type
247 * or its base (that is, we try both "foo_t *" and "struct foo *"), and also
248 * to potentially construct the required type on-the-fly.
251 dt_type_pointer(dtrace_typeinfo_t *tip)
253 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
254 ctf_file_t *ctfp = tip->dtt_ctfp;
255 ctf_id_t type = tip->dtt_type;
256 ctf_id_t base = ctf_type_resolve(ctfp, type);
261 if ((ptr = ctf_type_pointer(ctfp, type)) != CTF_ERR ||
262 (ptr = ctf_type_pointer(ctfp, base)) != CTF_ERR) {
267 if (yypcb->pcb_idepth != 0)
272 if (ctfp != dmp->dm_ctfp && ctfp != ctf_parent_file(dmp->dm_ctfp) &&
273 (type = ctf_add_type(dmp->dm_ctfp, ctfp, type)) == CTF_ERR) {
274 dtp->dt_ctferr = ctf_errno(dmp->dm_ctfp);
275 return (dt_set_errno(dtp, EDT_CTF));
278 ptr = ctf_add_pointer(dmp->dm_ctfp, CTF_ADD_ROOT, type);
280 if (ptr == CTF_ERR || ctf_update(dmp->dm_ctfp) == CTF_ERR) {
281 dtp->dt_ctferr = ctf_errno(dmp->dm_ctfp);
282 return (dt_set_errno(dtp, EDT_CTF));
285 tip->dtt_object = dmp->dm_name;
286 tip->dtt_ctfp = dmp->dm_ctfp;
293 dt_type_name(ctf_file_t *ctfp, ctf_id_t type, char *buf, size_t len)
295 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
297 if (ctfp == DT_FPTR_CTFP(dtp) && type == DT_FPTR_TYPE(dtp))
298 (void) snprintf(buf, len, "function pointer");
299 else if (ctfp == DT_FUNC_CTFP(dtp) && type == DT_FUNC_TYPE(dtp))
300 (void) snprintf(buf, len, "function");
301 else if (ctfp == DT_DYN_CTFP(dtp) && type == DT_DYN_TYPE(dtp))
302 (void) snprintf(buf, len, "dynamic variable");
303 else if (ctfp == NULL)
304 (void) snprintf(buf, len, "<none>");
305 else if (ctf_type_name(ctfp, type, buf, len) == NULL)
306 (void) snprintf(buf, len, "unknown");
312 * Perform the "usual arithmetic conversions" to determine which of the two
313 * input operand types should be promoted and used as a result type. The
314 * rules for this are described in ISOC[6.3.1.8] and K&R[A6.5].
317 dt_type_promote(dt_node_t *lp, dt_node_t *rp, ctf_file_t **ofp, ctf_id_t *otype)
319 ctf_file_t *lfp = lp->dn_ctfp;
320 ctf_id_t ltype = lp->dn_type;
322 ctf_file_t *rfp = rp->dn_ctfp;
323 ctf_id_t rtype = rp->dn_type;
325 ctf_id_t lbase = ctf_type_resolve(lfp, ltype);
326 uint_t lkind = ctf_type_kind(lfp, lbase);
328 ctf_id_t rbase = ctf_type_resolve(rfp, rtype);
329 uint_t rkind = ctf_type_kind(rfp, rbase);
331 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
332 ctf_encoding_t le, re;
335 assert(lkind == CTF_K_INTEGER || lkind == CTF_K_ENUM);
336 assert(rkind == CTF_K_INTEGER || rkind == CTF_K_ENUM);
338 if (lkind == CTF_K_ENUM) {
339 lfp = DT_INT_CTFP(dtp);
340 ltype = lbase = DT_INT_TYPE(dtp);
343 if (rkind == CTF_K_ENUM) {
344 rfp = DT_INT_CTFP(dtp);
345 rtype = rbase = DT_INT_TYPE(dtp);
348 if (ctf_type_encoding(lfp, lbase, &le) == CTF_ERR) {
349 yypcb->pcb_hdl->dt_ctferr = ctf_errno(lfp);
350 longjmp(yypcb->pcb_jmpbuf, EDT_CTF);
353 if (ctf_type_encoding(rfp, rbase, &re) == CTF_ERR) {
354 yypcb->pcb_hdl->dt_ctferr = ctf_errno(rfp);
355 longjmp(yypcb->pcb_jmpbuf, EDT_CTF);
359 * Compute an integer rank based on the size and unsigned status.
360 * If rank is identical, pick the "larger" of the equivalent types
361 * which we define as having a larger base ctf_id_t. If rank is
362 * different, pick the type with the greater rank.
364 lrank = le.cte_bits + ((le.cte_format & CTF_INT_SIGNED) == 0);
365 rrank = re.cte_bits + ((re.cte_format & CTF_INT_SIGNED) == 0);
367 if (lrank == rrank) {
368 if (lbase - rbase < 0)
372 } else if (lrank > rrank) {
388 dt_node_promote(dt_node_t *lp, dt_node_t *rp, dt_node_t *dnp)
390 dt_type_promote(lp, rp, &dnp->dn_ctfp, &dnp->dn_type);
391 dt_node_type_assign(dnp, dnp->dn_ctfp, dnp->dn_type);
392 dt_node_attr_assign(dnp, dt_attr_min(lp->dn_attr, rp->dn_attr));
396 dt_node_name(const dt_node_t *dnp, char *buf, size_t len)
398 char n1[DT_TYPE_NAMELEN];
399 char n2[DT_TYPE_NAMELEN];
401 const char *prefix = "", *suffix = "";
402 const dtrace_syminfo_t *dts;
405 switch (dnp->dn_kind) {
407 (void) snprintf(buf, len, "integer constant 0x%llx",
408 (u_longlong_t)dnp->dn_value);
411 s = strchr2esc(dnp->dn_string, strlen(dnp->dn_string));
412 (void) snprintf(buf, len, "string constant \"%s\"",
413 s != NULL ? s : dnp->dn_string);
417 (void) snprintf(buf, len, "identifier %s", dnp->dn_string);
423 switch (dnp->dn_ident->di_kind) {
425 case DT_IDENT_AGGFUNC:
426 case DT_IDENT_ACTFUNC:
433 (void) snprintf(buf, len, "%s %s%s%s",
434 dt_idkind_name(dnp->dn_ident->di_kind),
435 prefix, dnp->dn_ident->di_name, suffix);
438 dts = dnp->dn_ident->di_data;
439 (void) snprintf(buf, len, "symbol %s`%s",
440 dts->dts_object, dts->dts_name);
443 (void) snprintf(buf, len, "type %s",
444 dt_node_type_name(dnp, n1, sizeof (n1)));
449 (void) snprintf(buf, len, "operator %s", opstr(dnp->dn_op));
454 return (dt_node_name(dnp->dn_expr, buf, len));
455 (void) snprintf(buf, len, "%s", "statement");
458 if (dnp->dn_desc->dtpd_id == 0) {
459 (void) snprintf(buf, len,
460 "probe description %s:%s:%s:%s",
461 dnp->dn_desc->dtpd_provider, dnp->dn_desc->dtpd_mod,
462 dnp->dn_desc->dtpd_func, dnp->dn_desc->dtpd_name);
464 (void) snprintf(buf, len, "probe description %u",
465 dnp->dn_desc->dtpd_id);
469 (void) snprintf(buf, len, "%s", "clause");
472 (void) snprintf(buf, len, "member %s", dnp->dn_membname);
475 (void) snprintf(buf, len, "translator <%s> (%s)",
476 dt_type_name(dnp->dn_xlator->dx_dst_ctfp,
477 dnp->dn_xlator->dx_dst_type, n1, sizeof (n1)),
478 dt_type_name(dnp->dn_xlator->dx_src_ctfp,
479 dnp->dn_xlator->dx_src_type, n2, sizeof (n2)));
482 (void) snprintf(buf, len, "%s", "program");
485 (void) snprintf(buf, len, "node <%u>", dnp->dn_kind);
493 * dt_node_xalloc() can be used to create new parse nodes from any libdtrace
494 * caller. The caller is responsible for assigning dn_link appropriately.
497 dt_node_xalloc(dtrace_hdl_t *dtp, int kind)
499 dt_node_t *dnp = dt_alloc(dtp, sizeof (dt_node_t));
505 dnp->dn_type = CTF_ERR;
506 dnp->dn_kind = (uchar_t)kind;
511 dnp->dn_attr = _dtrace_defattr;
514 bzero(&dnp->dn_u, sizeof (dnp->dn_u));
520 * dt_node_alloc() is used to create new parse nodes from the parser. It
521 * assigns the node location based on the current lexer line number and places
522 * the new node on the default allocation list. If allocation fails, we
523 * automatically longjmp the caller back to the enclosing compilation call.
526 dt_node_alloc(int kind)
528 dt_node_t *dnp = dt_node_xalloc(yypcb->pcb_hdl, kind);
531 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
533 dnp->dn_line = yylineno;
534 dnp->dn_link = yypcb->pcb_list;
535 yypcb->pcb_list = dnp;
541 dt_node_free(dt_node_t *dnp)
543 uchar_t kind = dnp->dn_kind;
545 dnp->dn_kind = DT_NODE_FREE;
551 free(dnp->dn_string);
552 dnp->dn_string = NULL;
558 if (dnp->dn_ident != NULL) {
559 if (dnp->dn_ident->di_flags & DT_IDFLG_ORPHAN)
560 dt_ident_destroy(dnp->dn_ident);
561 dnp->dn_ident = NULL;
563 dt_node_list_free(&dnp->dn_args);
567 if (dnp->dn_child != NULL) {
568 dt_node_free(dnp->dn_child);
569 dnp->dn_child = NULL;
574 if (dnp->dn_expr != NULL) {
575 dt_node_free(dnp->dn_expr);
580 if (dnp->dn_left != NULL) {
581 dt_node_free(dnp->dn_left);
584 if (dnp->dn_right != NULL) {
585 dt_node_free(dnp->dn_right);
586 dnp->dn_right = NULL;
592 if (dnp->dn_expr != NULL) {
593 dt_node_free(dnp->dn_expr);
599 if (dnp->dn_aggfun != NULL) {
600 dt_node_free(dnp->dn_aggfun);
601 dnp->dn_aggfun = NULL;
603 dt_node_list_free(&dnp->dn_aggtup);
614 if (dnp->dn_pred != NULL)
615 dt_node_free(dnp->dn_pred);
616 if (dnp->dn_locals != NULL)
617 dt_idhash_destroy(dnp->dn_locals);
618 dt_node_list_free(&dnp->dn_pdescs);
619 dt_node_list_free(&dnp->dn_acts);
623 free(dnp->dn_membname);
624 dnp->dn_membname = NULL;
625 if (dnp->dn_membexpr != NULL) {
626 dt_node_free(dnp->dn_membexpr);
627 dnp->dn_membexpr = NULL;
631 case DT_NODE_PROVIDER:
632 dt_node_list_free(&dnp->dn_probes);
633 free(dnp->dn_provname);
634 dnp->dn_provname = NULL;
638 dt_node_list_free(&dnp->dn_list);
644 dt_node_attr_assign(dt_node_t *dnp, dtrace_attribute_t attr)
646 if ((yypcb->pcb_cflags & DTRACE_C_EATTR) &&
647 (dt_attr_cmp(attr, yypcb->pcb_amin) < 0)) {
648 char a[DTRACE_ATTR2STR_MAX];
651 dnerror(dnp, D_ATTR_MIN, "attributes for %s (%s) are less than "
652 "predefined minimum\n", dt_node_name(dnp, s, sizeof (s)),
653 dtrace_attr2str(attr, a, sizeof (a)));
660 dt_node_type_assign(dt_node_t *dnp, ctf_file_t *fp, ctf_id_t type)
662 ctf_id_t base = ctf_type_resolve(fp, type);
663 uint_t kind = ctf_type_kind(fp, base);
667 ~(DT_NF_SIGNED | DT_NF_REF | DT_NF_BITFIELD | DT_NF_USERLAND);
669 if (kind == CTF_K_INTEGER && ctf_type_encoding(fp, base, &e) == 0) {
670 size_t size = e.cte_bits / NBBY;
672 if (size > 8 || (e.cte_bits % NBBY) != 0 || (size & (size - 1)))
673 dnp->dn_flags |= DT_NF_BITFIELD;
675 if (e.cte_format & CTF_INT_SIGNED)
676 dnp->dn_flags |= DT_NF_SIGNED;
679 if (kind == CTF_K_FLOAT && ctf_type_encoding(fp, base, &e) == 0) {
680 if (e.cte_bits / NBBY > sizeof (uint64_t))
681 dnp->dn_flags |= DT_NF_REF;
684 if (kind == CTF_K_STRUCT || kind == CTF_K_UNION ||
685 kind == CTF_K_FORWARD ||
686 kind == CTF_K_ARRAY || kind == CTF_K_FUNCTION)
687 dnp->dn_flags |= DT_NF_REF;
688 else if (yypcb != NULL && fp == DT_DYN_CTFP(yypcb->pcb_hdl) &&
689 type == DT_DYN_TYPE(yypcb->pcb_hdl))
690 dnp->dn_flags |= DT_NF_REF;
692 dnp->dn_flags |= DT_NF_COOKED;
698 dt_node_type_propagate(const dt_node_t *src, dt_node_t *dst)
700 assert(src->dn_flags & DT_NF_COOKED);
701 dst->dn_flags = src->dn_flags & ~DT_NF_LVALUE;
702 dst->dn_ctfp = src->dn_ctfp;
703 dst->dn_type = src->dn_type;
707 dt_node_type_name(const dt_node_t *dnp, char *buf, size_t len)
709 if (dt_node_is_dynamic(dnp) && dnp->dn_ident != NULL) {
710 (void) snprintf(buf, len, "%s",
711 dt_idkind_name(dt_ident_resolve(dnp->dn_ident)->di_kind));
715 if (dnp->dn_flags & DT_NF_USERLAND) {
716 size_t n = snprintf(buf, len, "userland ");
717 len = len > n ? len - n : 0;
718 (void) dt_type_name(dnp->dn_ctfp, dnp->dn_type, buf + n, len);
722 return (dt_type_name(dnp->dn_ctfp, dnp->dn_type, buf, len));
726 dt_node_type_size(const dt_node_t *dnp)
730 if (dnp->dn_kind == DT_NODE_STRING)
731 return (strlen(dnp->dn_string) + 1);
733 if (dt_node_is_dynamic(dnp) && dnp->dn_ident != NULL)
734 return (dt_ident_size(dnp->dn_ident));
736 base = ctf_type_resolve(dnp->dn_ctfp, dnp->dn_type);
738 if (ctf_type_kind(dnp->dn_ctfp, base) == CTF_K_FORWARD)
741 return (ctf_type_size(dnp->dn_ctfp, dnp->dn_type));
745 * Determine if the specified parse tree node references an identifier of the
746 * specified kind, and if so return a pointer to it; otherwise return NULL.
747 * This function resolves the identifier itself, following through any inlines.
750 dt_node_resolve(const dt_node_t *dnp, uint_t idkind)
754 switch (dnp->dn_kind) {
761 idp = dt_ident_resolve(dnp->dn_ident);
762 return (idp->di_kind == idkind ? idp : NULL);
765 if (dt_node_is_dynamic(dnp)) {
766 idp = dt_ident_resolve(dnp->dn_ident);
767 return (idp->di_kind == idkind ? idp : NULL);
774 dt_node_sizeof(const dt_node_t *dnp)
776 dtrace_syminfo_t *sip;
778 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
781 * The size of the node as used for the sizeof() operator depends on
782 * the kind of the node. If the node is a SYM, the size is obtained
783 * from the symbol table; if it is not a SYM, the size is determined
784 * from the node's type. This is slightly different from C's sizeof()
785 * operator in that (for example) when applied to a function, sizeof()
786 * will evaluate to the length of the function rather than the size of
789 if (dnp->dn_kind != DT_NODE_SYM)
790 return (dt_node_type_size(dnp));
792 sip = dnp->dn_ident->di_data;
794 if (dtrace_lookup_by_name(dtp, sip->dts_object,
795 sip->dts_name, &sym, NULL) == -1)
798 return (sym.st_size);
802 dt_node_is_integer(const dt_node_t *dnp)
804 ctf_file_t *fp = dnp->dn_ctfp;
809 assert(dnp->dn_flags & DT_NF_COOKED);
811 type = ctf_type_resolve(fp, dnp->dn_type);
812 kind = ctf_type_kind(fp, type);
814 if (kind == CTF_K_INTEGER &&
815 ctf_type_encoding(fp, type, &e) == 0 && IS_VOID(e))
816 return (0); /* void integer */
818 return (kind == CTF_K_INTEGER || kind == CTF_K_ENUM);
822 dt_node_is_float(const dt_node_t *dnp)
824 ctf_file_t *fp = dnp->dn_ctfp;
829 assert(dnp->dn_flags & DT_NF_COOKED);
831 type = ctf_type_resolve(fp, dnp->dn_type);
832 kind = ctf_type_kind(fp, type);
834 return (kind == CTF_K_FLOAT &&
835 ctf_type_encoding(dnp->dn_ctfp, type, &e) == 0 && (
836 e.cte_format == CTF_FP_SINGLE || e.cte_format == CTF_FP_DOUBLE ||
837 e.cte_format == CTF_FP_LDOUBLE));
841 dt_node_is_scalar(const dt_node_t *dnp)
843 ctf_file_t *fp = dnp->dn_ctfp;
848 assert(dnp->dn_flags & DT_NF_COOKED);
850 type = ctf_type_resolve(fp, dnp->dn_type);
851 kind = ctf_type_kind(fp, type);
853 if (kind == CTF_K_INTEGER &&
854 ctf_type_encoding(fp, type, &e) == 0 && IS_VOID(e))
855 return (0); /* void cannot be used as a scalar */
857 return (kind == CTF_K_INTEGER || kind == CTF_K_ENUM ||
858 kind == CTF_K_POINTER);
862 dt_node_is_arith(const dt_node_t *dnp)
864 ctf_file_t *fp = dnp->dn_ctfp;
869 assert(dnp->dn_flags & DT_NF_COOKED);
871 type = ctf_type_resolve(fp, dnp->dn_type);
872 kind = ctf_type_kind(fp, type);
874 if (kind == CTF_K_INTEGER)
875 return (ctf_type_encoding(fp, type, &e) == 0 && !IS_VOID(e));
877 return (kind == CTF_K_ENUM);
881 dt_node_is_vfptr(const dt_node_t *dnp)
883 ctf_file_t *fp = dnp->dn_ctfp;
888 assert(dnp->dn_flags & DT_NF_COOKED);
890 type = ctf_type_resolve(fp, dnp->dn_type);
891 if (ctf_type_kind(fp, type) != CTF_K_POINTER)
892 return (0); /* type is not a pointer */
894 type = ctf_type_resolve(fp, ctf_type_reference(fp, type));
895 kind = ctf_type_kind(fp, type);
897 return (kind == CTF_K_FUNCTION || (kind == CTF_K_INTEGER &&
898 ctf_type_encoding(fp, type, &e) == 0 && IS_VOID(e)));
902 dt_node_is_dynamic(const dt_node_t *dnp)
904 if (dnp->dn_kind == DT_NODE_VAR &&
905 (dnp->dn_ident->di_flags & DT_IDFLG_INLINE)) {
906 const dt_idnode_t *inp = dnp->dn_ident->di_iarg;
907 return (inp->din_root ? dt_node_is_dynamic(inp->din_root) : 0);
910 return (dnp->dn_ctfp == DT_DYN_CTFP(yypcb->pcb_hdl) &&
911 dnp->dn_type == DT_DYN_TYPE(yypcb->pcb_hdl));
915 dt_node_is_string(const dt_node_t *dnp)
917 return (dnp->dn_ctfp == DT_STR_CTFP(yypcb->pcb_hdl) &&
918 dnp->dn_type == DT_STR_TYPE(yypcb->pcb_hdl));
922 dt_node_is_stack(const dt_node_t *dnp)
924 return (dnp->dn_ctfp == DT_STACK_CTFP(yypcb->pcb_hdl) &&
925 dnp->dn_type == DT_STACK_TYPE(yypcb->pcb_hdl));
929 dt_node_is_symaddr(const dt_node_t *dnp)
931 return (dnp->dn_ctfp == DT_SYMADDR_CTFP(yypcb->pcb_hdl) &&
932 dnp->dn_type == DT_SYMADDR_TYPE(yypcb->pcb_hdl));
936 dt_node_is_usymaddr(const dt_node_t *dnp)
938 return (dnp->dn_ctfp == DT_USYMADDR_CTFP(yypcb->pcb_hdl) &&
939 dnp->dn_type == DT_USYMADDR_TYPE(yypcb->pcb_hdl));
943 dt_node_is_strcompat(const dt_node_t *dnp)
945 ctf_file_t *fp = dnp->dn_ctfp;
951 assert(dnp->dn_flags & DT_NF_COOKED);
953 base = ctf_type_resolve(fp, dnp->dn_type);
954 kind = ctf_type_kind(fp, base);
956 if (kind == CTF_K_POINTER &&
957 (base = ctf_type_reference(fp, base)) != CTF_ERR &&
958 (base = ctf_type_resolve(fp, base)) != CTF_ERR &&
959 ctf_type_encoding(fp, base, &e) == 0 && IS_CHAR(e))
960 return (1); /* promote char pointer to string */
962 if (kind == CTF_K_ARRAY && ctf_array_info(fp, base, &r) == 0 &&
963 (base = ctf_type_resolve(fp, r.ctr_contents)) != CTF_ERR &&
964 ctf_type_encoding(fp, base, &e) == 0 && IS_CHAR(e))
965 return (1); /* promote char array to string */
971 dt_node_is_pointer(const dt_node_t *dnp)
973 ctf_file_t *fp = dnp->dn_ctfp;
976 assert(dnp->dn_flags & DT_NF_COOKED);
978 if (dt_node_is_string(dnp))
979 return (0); /* string are pass-by-ref but act like structs */
981 kind = ctf_type_kind(fp, ctf_type_resolve(fp, dnp->dn_type));
982 return (kind == CTF_K_POINTER || kind == CTF_K_ARRAY);
986 dt_node_is_void(const dt_node_t *dnp)
988 ctf_file_t *fp = dnp->dn_ctfp;
992 if (dt_node_is_dynamic(dnp))
993 return (0); /* <DYN> is an alias for void but not the same */
995 if (dt_node_is_stack(dnp))
998 if (dt_node_is_symaddr(dnp) || dt_node_is_usymaddr(dnp))
1001 type = ctf_type_resolve(fp, dnp->dn_type);
1003 return (ctf_type_kind(fp, type) == CTF_K_INTEGER &&
1004 ctf_type_encoding(fp, type, &e) == 0 && IS_VOID(e));
1008 dt_node_is_ptrcompat(const dt_node_t *lp, const dt_node_t *rp,
1009 ctf_file_t **fpp, ctf_id_t *tp)
1011 ctf_file_t *lfp = lp->dn_ctfp;
1012 ctf_file_t *rfp = rp->dn_ctfp;
1014 ctf_id_t lbase = CTF_ERR, rbase = CTF_ERR;
1015 ctf_id_t lref = CTF_ERR, rref = CTF_ERR;
1017 int lp_is_void, rp_is_void, lp_is_int, rp_is_int, compat;
1018 uint_t lkind, rkind;
1022 assert(lp->dn_flags & DT_NF_COOKED);
1023 assert(rp->dn_flags & DT_NF_COOKED);
1025 if (dt_node_is_dynamic(lp) || dt_node_is_dynamic(rp))
1026 return (0); /* fail if either node is a dynamic variable */
1028 lp_is_int = dt_node_is_integer(lp);
1029 rp_is_int = dt_node_is_integer(rp);
1031 if (lp_is_int && rp_is_int)
1032 return (0); /* fail if both nodes are integers */
1034 if (lp_is_int && (lp->dn_kind != DT_NODE_INT || lp->dn_value != 0))
1035 return (0); /* fail if lp is an integer that isn't 0 constant */
1037 if (rp_is_int && (rp->dn_kind != DT_NODE_INT || rp->dn_value != 0))
1038 return (0); /* fail if rp is an integer that isn't 0 constant */
1040 if ((lp_is_int == 0 && rp_is_int == 0) && (
1041 (lp->dn_flags & DT_NF_USERLAND) ^ (rp->dn_flags & DT_NF_USERLAND)))
1042 return (0); /* fail if only one pointer is a userland address */
1045 * Resolve the left-hand and right-hand types to their base type, and
1046 * then resolve the referenced type as well (assuming the base type
1047 * is CTF_K_POINTER or CTF_K_ARRAY). Otherwise [lr]ref = CTF_ERR.
1050 lbase = ctf_type_resolve(lfp, lp->dn_type);
1051 lkind = ctf_type_kind(lfp, lbase);
1053 if (lkind == CTF_K_POINTER) {
1054 lref = ctf_type_resolve(lfp,
1055 ctf_type_reference(lfp, lbase));
1056 } else if (lkind == CTF_K_ARRAY &&
1057 ctf_array_info(lfp, lbase, &r) == 0) {
1058 lref = ctf_type_resolve(lfp, r.ctr_contents);
1063 rbase = ctf_type_resolve(rfp, rp->dn_type);
1064 rkind = ctf_type_kind(rfp, rbase);
1066 if (rkind == CTF_K_POINTER) {
1067 rref = ctf_type_resolve(rfp,
1068 ctf_type_reference(rfp, rbase));
1069 } else if (rkind == CTF_K_ARRAY &&
1070 ctf_array_info(rfp, rbase, &r) == 0) {
1071 rref = ctf_type_resolve(rfp, r.ctr_contents);
1076 * We know that one or the other type may still be a zero-valued
1077 * integer constant. To simplify the code below, set the integer
1078 * type variables equal to the non-integer types and proceed.
1085 } else if (rp_is_int) {
1092 lp_is_void = ctf_type_encoding(lfp, lref, &e) == 0 && IS_VOID(e);
1093 rp_is_void = ctf_type_encoding(rfp, rref, &e) == 0 && IS_VOID(e);
1096 * The types are compatible if both are pointers to the same type, or
1097 * if either pointer is a void pointer. If they are compatible, set
1098 * tp to point to the more specific pointer type and return it.
1100 compat = (lkind == CTF_K_POINTER || lkind == CTF_K_ARRAY) &&
1101 (rkind == CTF_K_POINTER || rkind == CTF_K_ARRAY) &&
1102 (lp_is_void || rp_is_void || ctf_type_compat(lfp, lref, rfp, rref));
1106 *fpp = rp_is_void ? lfp : rfp;
1108 *tp = rp_is_void ? lbase : rbase;
1115 * The rules for checking argument types against parameter types are described
1116 * in the ANSI-C spec (see K&R[A7.3.2] and K&R[A7.17]). We use the same rule
1117 * set to determine whether associative array arguments match the prototype.
1120 dt_node_is_argcompat(const dt_node_t *lp, const dt_node_t *rp)
1122 ctf_file_t *lfp = lp->dn_ctfp;
1123 ctf_file_t *rfp = rp->dn_ctfp;
1125 assert(lp->dn_flags & DT_NF_COOKED);
1126 assert(rp->dn_flags & DT_NF_COOKED);
1128 if (dt_node_is_integer(lp) && dt_node_is_integer(rp))
1129 return (1); /* integer types are compatible */
1131 if (dt_node_is_strcompat(lp) && dt_node_is_strcompat(rp))
1132 return (1); /* string types are compatible */
1134 if (dt_node_is_stack(lp) && dt_node_is_stack(rp))
1135 return (1); /* stack types are compatible */
1137 if (dt_node_is_symaddr(lp) && dt_node_is_symaddr(rp))
1138 return (1); /* symaddr types are compatible */
1140 if (dt_node_is_usymaddr(lp) && dt_node_is_usymaddr(rp))
1141 return (1); /* usymaddr types are compatible */
1143 switch (ctf_type_kind(lfp, ctf_type_resolve(lfp, lp->dn_type))) {
1144 case CTF_K_FUNCTION:
1147 return (ctf_type_compat(lfp, lp->dn_type, rfp, rp->dn_type));
1149 return (dt_node_is_ptrcompat(lp, rp, NULL, NULL));
1154 * We provide dt_node_is_posconst() as a convenience routine for callers who
1155 * wish to verify that an argument is a positive non-zero integer constant.
1158 dt_node_is_posconst(const dt_node_t *dnp)
1160 return (dnp->dn_kind == DT_NODE_INT && dnp->dn_value != 0 && (
1161 (dnp->dn_flags & DT_NF_SIGNED) == 0 || (int64_t)dnp->dn_value > 0));
1165 dt_node_is_actfunc(const dt_node_t *dnp)
1167 return (dnp->dn_kind == DT_NODE_FUNC &&
1168 dnp->dn_ident->di_kind == DT_IDENT_ACTFUNC);
1172 * The original rules for integer constant typing are described in K&R[A2.5.1].
1173 * However, since we support long long, we instead use the rules from ISO C99
1174 * clause 6.4.4.1 since that is where long longs are formally described. The
1175 * rules require us to know whether the constant was specified in decimal or
1176 * in octal or hex, which we do by looking at our lexer's 'yyintdecimal' flag.
1177 * The type of an integer constant is the first of the corresponding list in
1178 * which its value can be represented:
1180 * unsuffixed decimal: int, long, long long
1181 * unsuffixed oct/hex: int, unsigned int, long, unsigned long,
1182 * long long, unsigned long long
1183 * suffix [uU]: unsigned int, unsigned long, unsigned long long
1184 * suffix [lL] decimal: long, long long
1185 * suffix [lL] oct/hex: long, unsigned long, long long, unsigned long long
1186 * suffix [uU][Ll]: unsigned long, unsigned long long
1187 * suffix ll/LL decimal: long long
1188 * suffix ll/LL oct/hex: long long, unsigned long long
1189 * suffix [uU][ll/LL]: unsigned long long
1191 * Given that our lexer has already validated the suffixes by regexp matching,
1192 * there is an obvious way to concisely encode these rules: construct an array
1193 * of the types in the order int, unsigned int, long, unsigned long, long long,
1194 * unsigned long long. Compute an integer array starting index based on the
1195 * suffix (e.g. none = 0, u = 1, ull = 5), and compute an increment based on
1196 * the specifier (dec/oct/hex) and suffix (u). Then iterate from the starting
1197 * index to the end, advancing using the increment, and searching until we
1198 * find a limit that matches or we run out of choices (overflow). To make it
1199 * even faster, we precompute the table of type information in dtrace_open().
1202 dt_node_int(uintmax_t value)
1204 dt_node_t *dnp = dt_node_alloc(DT_NODE_INT);
1205 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
1207 int n = (yyintdecimal | (yyintsuffix[0] == 'u')) + 1;
1213 dnp->dn_op = DT_TOK_INT;
1214 dnp->dn_value = value;
1216 for (p = yyintsuffix; (c = *p) != '\0'; p++) {
1217 if (c == 'U' || c == 'u')
1219 else if (c == 'L' || c == 'l')
1223 for (; i < sizeof (dtp->dt_ints) / sizeof (dtp->dt_ints[0]); i += n) {
1224 if (value <= dtp->dt_ints[i].did_limit) {
1225 dt_node_type_assign(dnp,
1226 dtp->dt_ints[i].did_ctfp,
1227 dtp->dt_ints[i].did_type);
1230 * If a prefix character is present in macro text, add
1231 * in the corresponding operator node (see dt_lex.l).
1233 switch (yyintprefix) {
1235 return (dt_node_op1(DT_TOK_IPOS, dnp));
1237 return (dt_node_op1(DT_TOK_INEG, dnp));
1244 xyerror(D_INT_OFLOW, "integer constant 0x%llx cannot be represented "
1245 "in any built-in integral type\n", (u_longlong_t)value);
1247 return (NULL); /* keep gcc happy */
1251 dt_node_string(char *string)
1253 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
1257 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
1259 dnp = dt_node_alloc(DT_NODE_STRING);
1260 dnp->dn_op = DT_TOK_STRING;
1261 dnp->dn_string = string;
1262 dt_node_type_assign(dnp, DT_STR_CTFP(dtp), DT_STR_TYPE(dtp));
1268 dt_node_ident(char *name)
1274 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
1277 * If the identifier is an inlined integer constant, then create an INT
1278 * node that is a clone of the inline parse tree node and return that
1279 * immediately, allowing this inline to be used in parsing contexts
1280 * that require constant expressions (e.g. scalar array sizes).
1282 if ((idp = dt_idstack_lookup(&yypcb->pcb_globals, name)) != NULL &&
1283 (idp->di_flags & DT_IDFLG_INLINE)) {
1284 dt_idnode_t *inp = idp->di_iarg;
1286 if (inp->din_root != NULL &&
1287 inp->din_root->dn_kind == DT_NODE_INT) {
1290 dnp = dt_node_alloc(DT_NODE_INT);
1291 dnp->dn_op = DT_TOK_INT;
1292 dnp->dn_value = inp->din_root->dn_value;
1293 dt_node_type_propagate(inp->din_root, dnp);
1299 dnp = dt_node_alloc(DT_NODE_IDENT);
1300 dnp->dn_op = name[0] == '@' ? DT_TOK_AGG : DT_TOK_IDENT;
1301 dnp->dn_string = name;
1307 * Create an empty node of type corresponding to the given declaration.
1308 * Explicit references to user types (C or D) are assigned the default
1309 * stability; references to other types are _dtrace_typattr (Private).
1312 dt_node_type(dt_decl_t *ddp)
1314 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
1315 dtrace_typeinfo_t dtt;
1321 * If 'ddp' is NULL, we get a decl by popping the decl stack. This
1322 * form of dt_node_type() is used by parameter rules in dt_grammar.y.
1325 ddp = dt_decl_pop_param(&name);
1327 err = dt_decl_type(ddp, &dtt);
1332 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
1335 dnp = dt_node_alloc(DT_NODE_TYPE);
1336 dnp->dn_op = DT_TOK_IDENT;
1337 dnp->dn_string = name;
1338 dt_node_type_assign(dnp, dtt.dtt_ctfp, dtt.dtt_type);
1340 if (dtt.dtt_ctfp == dtp->dt_cdefs->dm_ctfp ||
1341 dtt.dtt_ctfp == dtp->dt_ddefs->dm_ctfp)
1342 dt_node_attr_assign(dnp, _dtrace_defattr);
1344 dt_node_attr_assign(dnp, _dtrace_typattr);
1350 * Create a type node corresponding to a varargs (...) parameter by just
1351 * assigning it type CTF_ERR. The decl processing code will handle this.
1354 dt_node_vatype(void)
1356 dt_node_t *dnp = dt_node_alloc(DT_NODE_TYPE);
1358 dnp->dn_op = DT_TOK_IDENT;
1359 dnp->dn_ctfp = yypcb->pcb_hdl->dt_cdefs->dm_ctfp;
1360 dnp->dn_type = CTF_ERR;
1361 dnp->dn_attr = _dtrace_defattr;
1367 * Instantiate a decl using the contents of the current declaration stack. As
1368 * we do not currently permit decls to be initialized, this function currently
1369 * returns NULL and no parse node is created. When this function is called,
1370 * the topmost scope's ds_ident pointer will be set to NULL (indicating no
1371 * init_declarator rule was matched) or will point to the identifier to use.
1376 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
1377 dt_scope_t *dsp = &yypcb->pcb_dstack;
1378 dt_dclass_t class = dsp->ds_class;
1379 dt_decl_t *ddp = dt_decl_top();
1382 dtrace_typeinfo_t dtt;
1385 char n1[DT_TYPE_NAMELEN];
1386 char n2[DT_TYPE_NAMELEN];
1388 if (dt_decl_type(ddp, &dtt) != 0)
1389 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
1392 * If we have no declaration identifier, then this is either a spurious
1393 * declaration of an intrinsic type (e.g. "extern int;") or declaration
1394 * or redeclaration of a struct, union, or enum type or tag.
1396 if (dsp->ds_ident == NULL) {
1397 if (ddp->dd_kind != CTF_K_STRUCT &&
1398 ddp->dd_kind != CTF_K_UNION && ddp->dd_kind != CTF_K_ENUM)
1399 xyerror(D_DECL_USELESS, "useless declaration\n");
1401 dt_dprintf("type %s added as id %ld\n", dt_type_name(
1402 ddp->dd_ctfp, ddp->dd_type, n1, sizeof (n1)), ddp->dd_type);
1407 if (strchr(dsp->ds_ident, '`') != NULL) {
1408 xyerror(D_DECL_SCOPE, "D scoping operator may not be used in "
1409 "a declaration name (%s)\n", dsp->ds_ident);
1413 * If we are nested inside of a C include file, add the declaration to
1414 * the C definition module; otherwise use the D definition module.
1416 if (yypcb->pcb_idepth != 0)
1417 dmp = dtp->dt_cdefs;
1419 dmp = dtp->dt_ddefs;
1422 * If we see a global or static declaration of a function prototype,
1423 * treat this as equivalent to a D extern declaration.
1425 if (ctf_type_kind(dtt.dtt_ctfp, dtt.dtt_type) == CTF_K_FUNCTION &&
1426 (class == DT_DC_DEFAULT || class == DT_DC_STATIC))
1427 class = DT_DC_EXTERN;
1431 case DT_DC_REGISTER:
1433 xyerror(D_DECL_BADCLASS, "specified storage class not "
1434 "appropriate in D\n");
1437 case DT_DC_EXTERN: {
1438 dtrace_typeinfo_t ott;
1439 dtrace_syminfo_t dts;
1442 int exists = dtrace_lookup_by_name(dtp,
1443 dmp->dm_name, dsp->ds_ident, &sym, &dts) == 0;
1445 if (exists && (dtrace_symbol_type(dtp, &sym, &dts, &ott) != 0 ||
1446 ctf_type_cmp(dtt.dtt_ctfp, dtt.dtt_type,
1447 ott.dtt_ctfp, ott.dtt_type) != 0)) {
1448 xyerror(D_DECL_IDRED, "identifier redeclared: %s`%s\n"
1449 "\t current: %s\n\tprevious: %s\n",
1450 dmp->dm_name, dsp->ds_ident,
1451 dt_type_name(dtt.dtt_ctfp, dtt.dtt_type,
1453 dt_type_name(ott.dtt_ctfp, ott.dtt_type,
1455 } else if (!exists && dt_module_extern(dtp, dmp,
1456 dsp->ds_ident, &dtt) == NULL) {
1458 "failed to extern %s: %s\n", dsp->ds_ident,
1459 dtrace_errmsg(dtp, dtrace_errno(dtp)));
1461 dt_dprintf("extern %s`%s type=<%s>\n",
1462 dmp->dm_name, dsp->ds_ident,
1463 dt_type_name(dtt.dtt_ctfp, dtt.dtt_type,
1470 if (dt_idstack_lookup(&yypcb->pcb_globals, dsp->ds_ident)) {
1471 xyerror(D_DECL_IDRED, "global variable identifier "
1472 "redeclared: %s\n", dsp->ds_ident);
1475 if (ctf_lookup_by_name(dmp->dm_ctfp,
1476 dsp->ds_ident) != CTF_ERR) {
1477 xyerror(D_DECL_IDRED,
1478 "typedef redeclared: %s\n", dsp->ds_ident);
1482 * If the source type for the typedef is not defined in the
1483 * target container or its parent, copy the type to the target
1484 * container and reset dtt_ctfp and dtt_type to the copy.
1486 if (dtt.dtt_ctfp != dmp->dm_ctfp &&
1487 dtt.dtt_ctfp != ctf_parent_file(dmp->dm_ctfp)) {
1489 dtt.dtt_type = ctf_add_type(dmp->dm_ctfp,
1490 dtt.dtt_ctfp, dtt.dtt_type);
1491 dtt.dtt_ctfp = dmp->dm_ctfp;
1493 if (dtt.dtt_type == CTF_ERR ||
1494 ctf_update(dtt.dtt_ctfp) == CTF_ERR) {
1495 xyerror(D_UNKNOWN, "failed to copy typedef %s "
1496 "source type: %s\n", dsp->ds_ident,
1497 ctf_errmsg(ctf_errno(dtt.dtt_ctfp)));
1501 type = ctf_add_typedef(dmp->dm_ctfp,
1502 CTF_ADD_ROOT, dsp->ds_ident, dtt.dtt_type);
1504 if (type == CTF_ERR || ctf_update(dmp->dm_ctfp) == CTF_ERR) {
1505 xyerror(D_UNKNOWN, "failed to typedef %s: %s\n",
1506 dsp->ds_ident, ctf_errmsg(ctf_errno(dmp->dm_ctfp)));
1509 dt_dprintf("typedef %s added as id %ld\n", dsp->ds_ident, type);
1523 dhp = yypcb->pcb_locals;
1524 idflags = DT_IDFLG_LOCAL;
1525 idp = dt_idhash_lookup(dhp, dsp->ds_ident);
1529 idflags = DT_IDFLG_TLS;
1530 idp = dt_idhash_lookup(dhp, dsp->ds_ident);
1533 dhp = dtp->dt_globals;
1535 idp = dt_idstack_lookup(
1536 &yypcb->pcb_globals, dsp->ds_ident);
1540 if (ddp->dd_kind == CTF_K_ARRAY && ddp->dd_node == NULL) {
1541 xyerror(D_DECL_ARRNULL,
1542 "array declaration requires array dimension or "
1543 "tuple signature: %s\n", dsp->ds_ident);
1546 if (idp != NULL && idp->di_gen == 0) {
1547 xyerror(D_DECL_IDRED, "built-in identifier "
1548 "redeclared: %s\n", idp->di_name);
1551 if (dtrace_lookup_by_type(dtp, DTRACE_OBJ_CDEFS,
1552 dsp->ds_ident, NULL) == 0 ||
1553 dtrace_lookup_by_type(dtp, DTRACE_OBJ_DDEFS,
1554 dsp->ds_ident, NULL) == 0) {
1555 xyerror(D_DECL_IDRED, "typedef identifier "
1556 "redeclared: %s\n", dsp->ds_ident);
1560 * Cache some attributes of the decl to make the rest of this
1561 * code simpler: if the decl is an array which is subscripted
1562 * by a type rather than an integer, then it's an associative
1563 * array (assc). We then expect to match either DT_IDENT_ARRAY
1564 * for associative arrays or DT_IDENT_SCALAR for anything else.
1566 assc = ddp->dd_kind == CTF_K_ARRAY &&
1567 ddp->dd_node->dn_kind == DT_NODE_TYPE;
1569 idkind = assc ? DT_IDENT_ARRAY : DT_IDENT_SCALAR;
1572 * Create a fake dt_node_t on the stack so we can determine the
1573 * type of any matching identifier by assigning to this node.
1574 * If the pre-existing ident has its di_type set, propagate
1575 * the type by hand so as not to trigger a prototype check for
1576 * arrays (yet); otherwise we use dt_ident_cook() on the ident
1577 * to ensure it is fully initialized before looking at it.
1579 bzero(&idn, sizeof (dt_node_t));
1581 if (idp != NULL && idp->di_type != CTF_ERR)
1582 dt_node_type_assign(&idn, idp->di_ctfp, idp->di_type);
1583 else if (idp != NULL)
1584 (void) dt_ident_cook(&idn, idp, NULL);
1587 if (class == DT_DC_THIS) {
1588 xyerror(D_DECL_LOCASSC, "associative arrays "
1589 "may not be declared as local variables:"
1590 " %s\n", dsp->ds_ident);
1593 if (dt_decl_type(ddp->dd_next, &dtt) != 0)
1594 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
1597 if (idp != NULL && (idp->di_kind != idkind ||
1598 ctf_type_cmp(dtt.dtt_ctfp, dtt.dtt_type,
1599 idn.dn_ctfp, idn.dn_type) != 0)) {
1600 xyerror(D_DECL_IDRED, "identifier redeclared: %s\n"
1601 "\t current: %s %s\n\tprevious: %s %s\n",
1602 dsp->ds_ident, dt_idkind_name(idkind),
1603 dt_type_name(dtt.dtt_ctfp,
1604 dtt.dtt_type, n1, sizeof (n1)),
1605 dt_idkind_name(idp->di_kind),
1606 dt_node_type_name(&idn, n2, sizeof (n2)));
1608 } else if (idp != NULL && assc) {
1609 const dt_idsig_t *isp = idp->di_data;
1610 dt_node_t *dnp = ddp->dd_node;
1613 for (; dnp != NULL; dnp = dnp->dn_list, argc++) {
1614 const dt_node_t *pnp = &isp->dis_args[argc];
1616 if (argc >= isp->dis_argc)
1617 continue; /* tuple length mismatch */
1619 if (ctf_type_cmp(dnp->dn_ctfp, dnp->dn_type,
1620 pnp->dn_ctfp, pnp->dn_type) == 0)
1623 xyerror(D_DECL_IDRED,
1624 "identifier redeclared: %s\n"
1625 "\t current: %s, key #%d of type %s\n"
1626 "\tprevious: %s, key #%d of type %s\n",
1628 dt_idkind_name(idkind), argc + 1,
1629 dt_node_type_name(dnp, n1, sizeof (n1)),
1630 dt_idkind_name(idp->di_kind), argc + 1,
1631 dt_node_type_name(pnp, n2, sizeof (n2)));
1634 if (isp->dis_argc != argc) {
1635 xyerror(D_DECL_IDRED,
1636 "identifier redeclared: %s\n"
1637 "\t current: %s of %s, tuple length %d\n"
1638 "\tprevious: %s of %s, tuple length %d\n",
1639 dsp->ds_ident, dt_idkind_name(idkind),
1640 dt_type_name(dtt.dtt_ctfp, dtt.dtt_type,
1641 n1, sizeof (n1)), argc,
1642 dt_idkind_name(idp->di_kind),
1643 dt_node_type_name(&idn, n2, sizeof (n2)),
1647 } else if (idp == NULL) {
1648 type = ctf_type_resolve(dtt.dtt_ctfp, dtt.dtt_type);
1649 kind = ctf_type_kind(dtt.dtt_ctfp, type);
1653 if (ctf_type_encoding(dtt.dtt_ctfp, type,
1654 &cte) == 0 && IS_VOID(cte)) {
1655 xyerror(D_DECL_VOIDOBJ, "cannot have "
1656 "void object: %s\n", dsp->ds_ident);
1661 if (ctf_type_size(dtt.dtt_ctfp, type) != 0)
1662 break; /* proceed to declaring */
1665 xyerror(D_DECL_INCOMPLETE,
1666 "incomplete struct/union/enum %s: %s\n",
1667 dt_type_name(dtt.dtt_ctfp, dtt.dtt_type,
1668 n1, sizeof (n1)), dsp->ds_ident);
1672 if (dt_idhash_nextid(dhp, &id) == -1) {
1673 xyerror(D_ID_OFLOW, "cannot create %s: limit "
1674 "on number of %s variables exceeded\n",
1675 dsp->ds_ident, dt_idhash_name(dhp));
1678 dt_dprintf("declare %s %s variable %s, id=%u\n",
1679 dt_idhash_name(dhp), dt_idkind_name(idkind),
1682 idp = dt_idhash_insert(dhp, dsp->ds_ident, idkind,
1683 idflags | DT_IDFLG_WRITE | DT_IDFLG_DECL, id,
1684 _dtrace_defattr, 0, assc ? &dt_idops_assc :
1685 &dt_idops_thaw, NULL, dtp->dt_gen);
1688 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
1690 dt_ident_type_assign(idp, dtt.dtt_ctfp, dtt.dtt_type);
1693 * If we are declaring an associative array, use our
1694 * fake parse node to cook the new assoc identifier.
1695 * This will force the ident code to instantiate the
1696 * array type signature corresponding to the list of
1697 * types pointed to by ddp->dd_node. We also reset
1698 * the identifier's attributes based upon the result.
1702 dt_ident_cook(&idn, idp, &ddp->dd_node);
1707 } /* end of switch */
1709 free(dsp->ds_ident);
1710 dsp->ds_ident = NULL;
1716 dt_node_func(dt_node_t *dnp, dt_node_t *args)
1720 if (dnp->dn_kind != DT_NODE_IDENT) {
1721 xyerror(D_FUNC_IDENT,
1722 "function designator is not of function type\n");
1725 idp = dt_idstack_lookup(&yypcb->pcb_globals, dnp->dn_string);
1728 xyerror(D_FUNC_UNDEF,
1729 "undefined function name: %s\n", dnp->dn_string);
1732 if (idp->di_kind != DT_IDENT_FUNC &&
1733 idp->di_kind != DT_IDENT_AGGFUNC &&
1734 idp->di_kind != DT_IDENT_ACTFUNC) {
1735 xyerror(D_FUNC_IDKIND, "%s '%s' may not be referenced as a "
1736 "function\n", dt_idkind_name(idp->di_kind), idp->di_name);
1739 free(dnp->dn_string);
1740 dnp->dn_string = NULL;
1742 dnp->dn_kind = DT_NODE_FUNC;
1743 dnp->dn_flags &= ~DT_NF_COOKED;
1744 dnp->dn_ident = idp;
1745 dnp->dn_args = args;
1746 dnp->dn_list = NULL;
1752 * The offsetof() function is special because it takes a type name as an
1753 * argument. It does not actually construct its own node; after looking up the
1754 * structure or union offset, we just return an integer node with the offset.
1757 dt_node_offsetof(dt_decl_t *ddp, char *s)
1759 dtrace_typeinfo_t dtt;
1768 name = alloca(strlen(s) + 1);
1769 (void) strcpy(name, s);
1772 err = dt_decl_type(ddp, &dtt);
1776 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
1778 type = ctf_type_resolve(dtt.dtt_ctfp, dtt.dtt_type);
1779 kind = ctf_type_kind(dtt.dtt_ctfp, type);
1781 if (kind != CTF_K_STRUCT && kind != CTF_K_UNION) {
1782 xyerror(D_OFFSETOF_TYPE,
1783 "offsetof operand must be a struct or union type\n");
1786 if (ctf_member_info(dtt.dtt_ctfp, type, name, &ctm) == CTF_ERR) {
1787 xyerror(D_UNKNOWN, "failed to determine offset of %s: %s\n",
1788 name, ctf_errmsg(ctf_errno(dtt.dtt_ctfp)));
1791 bzero(&dn, sizeof (dn));
1792 dt_node_type_assign(&dn, dtt.dtt_ctfp, ctm.ctm_type);
1794 if (dn.dn_flags & DT_NF_BITFIELD) {
1795 xyerror(D_OFFSETOF_BITFIELD,
1796 "cannot take offset of a bit-field: %s\n", name);
1799 return (dt_node_int(ctm.ctm_offset / NBBY));
1803 dt_node_op1(int op, dt_node_t *cp)
1807 if (cp->dn_kind == DT_NODE_INT) {
1811 * If we're negating an unsigned integer, zero out any
1812 * extra top bits to truncate the value to the size of
1813 * the effective type determined by dt_node_int().
1815 cp->dn_value = -cp->dn_value;
1816 if (!(cp->dn_flags & DT_NF_SIGNED)) {
1817 cp->dn_value &= ~0ULL >>
1818 (64 - dt_node_type_size(cp) * NBBY);
1824 cp->dn_value = ~cp->dn_value;
1827 cp->dn_value = !cp->dn_value;
1833 * If sizeof is applied to a type_name or string constant, we can
1834 * transform 'cp' into an integer constant in the node construction
1835 * pass so that it can then be used for arithmetic in this pass.
1837 if (op == DT_TOK_SIZEOF &&
1838 (cp->dn_kind == DT_NODE_STRING || cp->dn_kind == DT_NODE_TYPE)) {
1839 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
1840 size_t size = dt_node_type_size(cp);
1843 xyerror(D_SIZEOF_TYPE, "cannot apply sizeof to an "
1844 "operand of unknown size\n");
1847 dt_node_type_assign(cp, dtp->dt_ddefs->dm_ctfp,
1848 ctf_lookup_by_name(dtp->dt_ddefs->dm_ctfp, "size_t"));
1850 cp->dn_kind = DT_NODE_INT;
1851 cp->dn_op = DT_TOK_INT;
1852 cp->dn_value = size;
1857 dnp = dt_node_alloc(DT_NODE_OP1);
1858 assert(op <= USHRT_MAX);
1859 dnp->dn_op = (ushort_t)op;
1866 dt_node_op2(int op, dt_node_t *lp, dt_node_t *rp)
1868 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
1872 * First we check for operations that are illegal -- namely those that
1873 * might result in integer division by zero, and abort if one is found.
1875 if (rp->dn_kind == DT_NODE_INT && rp->dn_value == 0 &&
1876 (op == DT_TOK_MOD || op == DT_TOK_DIV ||
1877 op == DT_TOK_MOD_EQ || op == DT_TOK_DIV_EQ))
1878 xyerror(D_DIV_ZERO, "expression contains division by zero\n");
1881 * If both children are immediate values, we can just perform inline
1882 * calculation and return a new immediate node with the result.
1884 if (lp->dn_kind == DT_NODE_INT && rp->dn_kind == DT_NODE_INT) {
1885 uintmax_t l = lp->dn_value;
1886 uintmax_t r = rp->dn_value;
1888 dnp = dt_node_int(0); /* allocate new integer node for result */
1892 dnp->dn_value = l || r;
1893 dt_node_type_assign(dnp,
1894 DT_INT_CTFP(dtp), DT_INT_TYPE(dtp));
1897 dnp->dn_value = (l != 0) ^ (r != 0);
1898 dt_node_type_assign(dnp,
1899 DT_INT_CTFP(dtp), DT_INT_TYPE(dtp));
1902 dnp->dn_value = l && r;
1903 dt_node_type_assign(dnp,
1904 DT_INT_CTFP(dtp), DT_INT_TYPE(dtp));
1907 dnp->dn_value = l | r;
1908 dt_node_promote(lp, rp, dnp);
1911 dnp->dn_value = l ^ r;
1912 dt_node_promote(lp, rp, dnp);
1915 dnp->dn_value = l & r;
1916 dt_node_promote(lp, rp, dnp);
1919 dnp->dn_value = l == r;
1920 dt_node_type_assign(dnp,
1921 DT_INT_CTFP(dtp), DT_INT_TYPE(dtp));
1924 dnp->dn_value = l != r;
1925 dt_node_type_assign(dnp,
1926 DT_INT_CTFP(dtp), DT_INT_TYPE(dtp));
1929 dt_node_promote(lp, rp, dnp);
1930 if (dnp->dn_flags & DT_NF_SIGNED)
1931 dnp->dn_value = (intmax_t)l < (intmax_t)r;
1933 dnp->dn_value = l < r;
1934 dt_node_type_assign(dnp,
1935 DT_INT_CTFP(dtp), DT_INT_TYPE(dtp));
1938 dt_node_promote(lp, rp, dnp);
1939 if (dnp->dn_flags & DT_NF_SIGNED)
1940 dnp->dn_value = (intmax_t)l <= (intmax_t)r;
1942 dnp->dn_value = l <= r;
1943 dt_node_type_assign(dnp,
1944 DT_INT_CTFP(dtp), DT_INT_TYPE(dtp));
1947 dt_node_promote(lp, rp, dnp);
1948 if (dnp->dn_flags & DT_NF_SIGNED)
1949 dnp->dn_value = (intmax_t)l > (intmax_t)r;
1951 dnp->dn_value = l > r;
1952 dt_node_type_assign(dnp,
1953 DT_INT_CTFP(dtp), DT_INT_TYPE(dtp));
1956 dt_node_promote(lp, rp, dnp);
1957 if (dnp->dn_flags & DT_NF_SIGNED)
1958 dnp->dn_value = (intmax_t)l >= (intmax_t)r;
1960 dnp->dn_value = l >= r;
1961 dt_node_type_assign(dnp,
1962 DT_INT_CTFP(dtp), DT_INT_TYPE(dtp));
1965 dnp->dn_value = l << r;
1966 dt_node_type_propagate(lp, dnp);
1967 dt_node_attr_assign(rp,
1968 dt_attr_min(lp->dn_attr, rp->dn_attr));
1971 dnp->dn_value = l >> r;
1972 dt_node_type_propagate(lp, dnp);
1973 dt_node_attr_assign(rp,
1974 dt_attr_min(lp->dn_attr, rp->dn_attr));
1977 dnp->dn_value = l + r;
1978 dt_node_promote(lp, rp, dnp);
1981 dnp->dn_value = l - r;
1982 dt_node_promote(lp, rp, dnp);
1985 dnp->dn_value = l * r;
1986 dt_node_promote(lp, rp, dnp);
1989 dt_node_promote(lp, rp, dnp);
1990 if (dnp->dn_flags & DT_NF_SIGNED)
1991 dnp->dn_value = (intmax_t)l / (intmax_t)r;
1993 dnp->dn_value = l / r;
1996 dt_node_promote(lp, rp, dnp);
1997 if (dnp->dn_flags & DT_NF_SIGNED)
1998 dnp->dn_value = (intmax_t)l % (intmax_t)r;
2000 dnp->dn_value = l % r;
2015 * If an integer constant is being cast to another integer type, we can
2016 * perform the cast as part of integer constant folding in this pass.
2017 * We must take action when the integer is being cast to a smaller type
2018 * or if it is changing signed-ness. If so, we first shift rp's bits
2019 * bits high (losing excess bits if narrowing) and then shift them down
2020 * with either a logical shift (unsigned) or arithmetic shift (signed).
2022 if (op == DT_TOK_LPAR && rp->dn_kind == DT_NODE_INT &&
2023 dt_node_is_integer(lp)) {
2024 size_t srcsize = dt_node_type_size(rp);
2025 size_t dstsize = dt_node_type_size(lp);
2027 if ((dstsize < srcsize) || ((lp->dn_flags & DT_NF_SIGNED) ^
2028 (rp->dn_flags & DT_NF_SIGNED))) {
2029 int n = dstsize < srcsize ?
2030 (sizeof (uint64_t) * NBBY - dstsize * NBBY) :
2031 (sizeof (uint64_t) * NBBY - srcsize * NBBY);
2034 if (lp->dn_flags & DT_NF_SIGNED)
2035 rp->dn_value = (intmax_t)rp->dn_value >> n;
2037 rp->dn_value = rp->dn_value >> n;
2040 dt_node_type_propagate(lp, rp);
2041 dt_node_attr_assign(rp, dt_attr_min(lp->dn_attr, rp->dn_attr));
2048 * If no immediate optimizations are available, create an new OP2 node
2049 * and glue the left and right children into place and return.
2051 dnp = dt_node_alloc(DT_NODE_OP2);
2052 assert(op <= USHRT_MAX);
2053 dnp->dn_op = (ushort_t)op;
2061 dt_node_op3(dt_node_t *expr, dt_node_t *lp, dt_node_t *rp)
2065 if (expr->dn_kind == DT_NODE_INT)
2066 return (expr->dn_value != 0 ? lp : rp);
2068 dnp = dt_node_alloc(DT_NODE_OP3);
2069 dnp->dn_op = DT_TOK_QUESTION;
2070 dnp->dn_expr = expr;
2078 dt_node_statement(dt_node_t *expr)
2082 if (expr->dn_kind == DT_NODE_AGG)
2085 if (expr->dn_kind == DT_NODE_FUNC &&
2086 expr->dn_ident->di_kind == DT_IDENT_ACTFUNC)
2087 dnp = dt_node_alloc(DT_NODE_DFUNC);
2089 dnp = dt_node_alloc(DT_NODE_DEXPR);
2091 dnp->dn_expr = expr;
2096 dt_node_pdesc_by_name(char *spec)
2098 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
2102 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2104 dnp = dt_node_alloc(DT_NODE_PDESC);
2105 dnp->dn_spec = spec;
2106 dnp->dn_desc = malloc(sizeof (dtrace_probedesc_t));
2108 if (dnp->dn_desc == NULL)
2109 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2111 if (dtrace_xstr2desc(dtp, yypcb->pcb_pspec, dnp->dn_spec,
2112 yypcb->pcb_sargc, yypcb->pcb_sargv, dnp->dn_desc) != 0) {
2113 xyerror(D_PDESC_INVAL, "invalid probe description \"%s\": %s\n",
2114 dnp->dn_spec, dtrace_errmsg(dtp, dtrace_errno(dtp)));
2118 dnp->dn_spec = NULL;
2124 dt_node_pdesc_by_id(uintmax_t id)
2126 static const char *const names[] = {
2127 "providers", "modules", "functions"
2130 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
2131 dt_node_t *dnp = dt_node_alloc(DT_NODE_PDESC);
2133 if ((dnp->dn_desc = malloc(sizeof (dtrace_probedesc_t))) == NULL)
2134 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2136 if (id > UINT_MAX) {
2137 xyerror(D_PDESC_INVAL, "identifier %llu exceeds maximum "
2138 "probe id\n", (u_longlong_t)id);
2141 if (yypcb->pcb_pspec != DTRACE_PROBESPEC_NAME) {
2142 xyerror(D_PDESC_INVAL, "probe identifier %llu not permitted "
2143 "when specifying %s\n", (u_longlong_t)id,
2144 names[yypcb->pcb_pspec]);
2147 if (dtrace_id2desc(dtp, (dtrace_id_t)id, dnp->dn_desc) != 0) {
2148 xyerror(D_PDESC_INVAL, "invalid probe identifier %llu: %s\n",
2149 (u_longlong_t)id, dtrace_errmsg(dtp, dtrace_errno(dtp)));
2156 dt_node_clause(dt_node_t *pdescs, dt_node_t *pred, dt_node_t *acts)
2158 dt_node_t *dnp = dt_node_alloc(DT_NODE_CLAUSE);
2160 dnp->dn_pdescs = pdescs;
2161 dnp->dn_pred = pred;
2162 dnp->dn_acts = acts;
2164 yybegin(YYS_CLAUSE);
2169 dt_node_inline(dt_node_t *expr)
2171 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
2172 dt_scope_t *dsp = &yypcb->pcb_dstack;
2173 dt_decl_t *ddp = dt_decl_top();
2175 char n[DT_TYPE_NAMELEN];
2176 dtrace_typeinfo_t dtt;
2178 dt_ident_t *idp, *rdp;
2182 if (dt_decl_type(ddp, &dtt) != 0)
2183 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
2185 if (dsp->ds_class != DT_DC_DEFAULT) {
2186 xyerror(D_DECL_BADCLASS, "specified storage class not "
2187 "appropriate for inline declaration\n");
2190 if (dsp->ds_ident == NULL)
2191 xyerror(D_DECL_USELESS, "inline declaration requires a name\n");
2193 if ((idp = dt_idstack_lookup(
2194 &yypcb->pcb_globals, dsp->ds_ident)) != NULL) {
2195 xyerror(D_DECL_IDRED, "identifier redefined: %s\n\t current: "
2196 "inline definition\n\tprevious: %s %s\n",
2197 idp->di_name, dt_idkind_name(idp->di_kind),
2198 (idp->di_flags & DT_IDFLG_INLINE) ? "inline" : "");
2202 * If we are declaring an inlined array, verify that we have a tuple
2203 * signature, and then recompute 'dtt' as the array's value type.
2205 if (ddp->dd_kind == CTF_K_ARRAY) {
2206 if (ddp->dd_node == NULL) {
2207 xyerror(D_DECL_ARRNULL, "inline declaration requires "
2208 "array tuple signature: %s\n", dsp->ds_ident);
2211 if (ddp->dd_node->dn_kind != DT_NODE_TYPE) {
2212 xyerror(D_DECL_ARRNULL, "inline declaration cannot be "
2213 "of scalar array type: %s\n", dsp->ds_ident);
2216 if (dt_decl_type(ddp->dd_next, &dtt) != 0)
2217 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
2221 * If the inline identifier is not defined, then create it with the
2222 * orphan flag set. We do not insert the identifier into dt_globals
2223 * until we have successfully cooked the right-hand expression, below.
2225 dnp = dt_node_alloc(DT_NODE_INLINE);
2226 dt_node_type_assign(dnp, dtt.dtt_ctfp, dtt.dtt_type);
2227 dt_node_attr_assign(dnp, _dtrace_defattr);
2229 if (dt_node_is_void(dnp)) {
2230 xyerror(D_DECL_VOIDOBJ,
2231 "cannot declare void inline: %s\n", dsp->ds_ident);
2234 if (ctf_type_kind(dnp->dn_ctfp, ctf_type_resolve(
2235 dnp->dn_ctfp, dnp->dn_type)) == CTF_K_FORWARD) {
2236 xyerror(D_DECL_INCOMPLETE,
2237 "incomplete struct/union/enum %s: %s\n",
2238 dt_node_type_name(dnp, n, sizeof (n)), dsp->ds_ident);
2241 if ((inp = malloc(sizeof (dt_idnode_t))) == NULL)
2242 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2244 bzero(inp, sizeof (dt_idnode_t));
2246 idp = dnp->dn_ident = dt_ident_create(dsp->ds_ident,
2247 ddp->dd_kind == CTF_K_ARRAY ? DT_IDENT_ARRAY : DT_IDENT_SCALAR,
2248 DT_IDFLG_INLINE | DT_IDFLG_REF | DT_IDFLG_DECL | DT_IDFLG_ORPHAN, 0,
2249 _dtrace_defattr, 0, &dt_idops_inline, inp, dtp->dt_gen);
2253 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2257 * If we're inlining an associative array, create a private identifier
2258 * hash containing the named parameters and store it in inp->din_hash.
2259 * We then push this hash on to the top of the pcb_globals stack.
2261 if (ddp->dd_kind == CTF_K_ARRAY) {
2267 for (pnp = ddp->dd_node; pnp != NULL; pnp = pnp->dn_list)
2268 i++; /* count up parameters for din_argv[] */
2270 inp->din_hash = dt_idhash_create("inline args", NULL, 0, 0);
2271 inp->din_argv = calloc(i, sizeof (dt_ident_t *));
2273 if (inp->din_hash == NULL || inp->din_argv == NULL)
2274 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2277 * Create an identifier for each parameter as a scalar inline,
2278 * and store it in din_hash and in position in din_argv[]. The
2279 * parameter identifiers also use dt_idops_inline, but we leave
2280 * the dt_idnode_t argument 'pinp' zeroed. This will be filled
2281 * in by the code generation pass with references to the args.
2283 for (i = 0, pnp = ddp->dd_node;
2284 pnp != NULL; pnp = pnp->dn_list, i++) {
2286 if (pnp->dn_string == NULL)
2287 continue; /* ignore anonymous parameters */
2289 if ((pinp = malloc(sizeof (dt_idnode_t))) == NULL)
2290 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2292 pidp = dt_idhash_insert(inp->din_hash, pnp->dn_string,
2293 DT_IDENT_SCALAR, DT_IDFLG_DECL | DT_IDFLG_INLINE, 0,
2294 _dtrace_defattr, 0, &dt_idops_inline,
2299 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2302 inp->din_argv[i] = pidp;
2303 bzero(pinp, sizeof (dt_idnode_t));
2304 dt_ident_type_assign(pidp, pnp->dn_ctfp, pnp->dn_type);
2307 dt_idstack_push(&yypcb->pcb_globals, inp->din_hash);
2311 * Unlike most constructors, we need to explicitly cook the right-hand
2312 * side of the inline definition immediately to prevent recursion. If
2313 * the right-hand side uses the inline itself, the cook will fail.
2315 expr = dt_node_cook(expr, DT_IDFLG_REF);
2317 if (ddp->dd_kind == CTF_K_ARRAY)
2318 dt_idstack_pop(&yypcb->pcb_globals, inp->din_hash);
2321 * Set the type, attributes, and flags for the inline. If the right-
2322 * hand expression has an identifier, propagate its flags. Then cook
2323 * the identifier to fully initialize it: if we're declaring an inline
2324 * associative array this will construct a type signature from 'ddp'.
2326 if (dt_node_is_dynamic(expr))
2327 rdp = dt_ident_resolve(expr->dn_ident);
2328 else if (expr->dn_kind == DT_NODE_VAR || expr->dn_kind == DT_NODE_SYM)
2329 rdp = expr->dn_ident;
2334 idp->di_flags |= (rdp->di_flags &
2335 (DT_IDFLG_WRITE | DT_IDFLG_USER | DT_IDFLG_PRIM));
2338 idp->di_attr = dt_attr_min(_dtrace_defattr, expr->dn_attr);
2339 dt_ident_type_assign(idp, dtt.dtt_ctfp, dtt.dtt_type);
2340 (void) dt_ident_cook(dnp, idp, &ddp->dd_node);
2343 * Store the parse tree nodes for 'expr' inside of idp->di_data ('inp')
2344 * so that they will be preserved with this identifier. Then pop the
2345 * inline declaration from the declaration stack and restore the lexer.
2347 inp->din_list = yypcb->pcb_list;
2348 inp->din_root = expr;
2350 dt_decl_free(dt_decl_pop());
2351 yybegin(YYS_CLAUSE);
2354 * Finally, insert the inline identifier into dt_globals to make it
2355 * visible, and then cook 'dnp' to check its type against 'expr'.
2357 dt_idhash_xinsert(dtp->dt_globals, idp);
2358 return (dt_node_cook(dnp, DT_IDFLG_REF));
2362 dt_node_member(dt_decl_t *ddp, char *name, dt_node_t *expr)
2364 dtrace_typeinfo_t dtt;
2369 err = dt_decl_type(ddp, &dtt);
2373 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
2376 dnp = dt_node_alloc(DT_NODE_MEMBER);
2377 dnp->dn_membname = name;
2378 dnp->dn_membexpr = expr;
2381 dt_node_type_assign(dnp, dtt.dtt_ctfp, dtt.dtt_type);
2387 dt_node_xlator(dt_decl_t *ddp, dt_decl_t *sdp, char *name, dt_node_t *members)
2389 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
2390 dtrace_typeinfo_t src, dst;
2397 char n1[DT_TYPE_NAMELEN];
2398 char n2[DT_TYPE_NAMELEN];
2400 edst = dt_decl_type(ddp, &dst);
2403 esrc = dt_decl_type(sdp, &src);
2406 if (edst != 0 || esrc != 0) {
2408 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
2411 bzero(&sn, sizeof (sn));
2412 dt_node_type_assign(&sn, src.dtt_ctfp, src.dtt_type);
2414 bzero(&dn, sizeof (dn));
2415 dt_node_type_assign(&dn, dst.dtt_ctfp, dst.dtt_type);
2417 if (dt_xlator_lookup(dtp, &sn, &dn, DT_XLATE_EXACT) != NULL) {
2418 xyerror(D_XLATE_REDECL,
2419 "translator from %s to %s has already been declared\n",
2420 dt_node_type_name(&sn, n1, sizeof (n1)),
2421 dt_node_type_name(&dn, n2, sizeof (n2)));
2424 kind = ctf_type_kind(dst.dtt_ctfp,
2425 ctf_type_resolve(dst.dtt_ctfp, dst.dtt_type));
2427 if (kind == CTF_K_FORWARD) {
2428 xyerror(D_XLATE_SOU, "incomplete struct/union/enum %s\n",
2429 dt_type_name(dst.dtt_ctfp, dst.dtt_type, n1, sizeof (n1)));
2432 if (kind != CTF_K_STRUCT && kind != CTF_K_UNION) {
2433 xyerror(D_XLATE_SOU,
2434 "translator output type must be a struct or union\n");
2437 dxp = dt_xlator_create(dtp, &src, &dst, name, members, yypcb->pcb_list);
2438 yybegin(YYS_CLAUSE);
2442 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2444 dnp = dt_node_alloc(DT_NODE_XLATOR);
2445 dnp->dn_xlator = dxp;
2446 dnp->dn_members = members;
2448 return (dt_node_cook(dnp, DT_IDFLG_REF));
2452 dt_node_probe(char *s, int protoc, dt_node_t *nargs, dt_node_t *xargs)
2454 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
2458 size_t len = strlen(s) + 3; /* +3 for :: and \0 */
2459 char *name = alloca(len);
2461 (void) snprintf(name, len, "::%s", s);
2462 (void) strhyphenate(name);
2465 if (strchr(name, '`') != NULL) {
2466 xyerror(D_PROV_BADNAME, "probe name may not "
2467 "contain scoping operator: %s\n", name);
2470 if (strlen(name) - 2 >= DTRACE_NAMELEN) {
2471 xyerror(D_PROV_BADNAME, "probe name may not exceed %d "
2472 "characters: %s\n", DTRACE_NAMELEN - 1, name);
2475 dnp = dt_node_alloc(DT_NODE_PROBE);
2477 dnp->dn_ident = dt_ident_create(name, DT_IDENT_PROBE,
2478 DT_IDFLG_ORPHAN, DTRACE_IDNONE, _dtrace_defattr, 0,
2479 &dt_idops_probe, NULL, dtp->dt_gen);
2481 nargc = dt_decl_prototype(nargs, nargs,
2482 "probe input", DT_DP_VOID | DT_DP_ANON);
2484 xargc = dt_decl_prototype(xargs, nargs,
2485 "probe output", DT_DP_VOID);
2487 if (nargc > UINT8_MAX) {
2488 xyerror(D_PROV_PRARGLEN, "probe %s input prototype exceeds %u "
2489 "parameters: %d params used\n", name, UINT8_MAX, nargc);
2492 if (xargc > UINT8_MAX) {
2493 xyerror(D_PROV_PRARGLEN, "probe %s output prototype exceeds %u "
2494 "parameters: %d params used\n", name, UINT8_MAX, xargc);
2497 if (dnp->dn_ident == NULL || dt_probe_create(dtp,
2498 dnp->dn_ident, protoc, nargs, nargc, xargs, xargc) == NULL)
2499 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2505 dt_node_provider(char *name, dt_node_t *probes)
2507 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
2508 dt_node_t *dnp = dt_node_alloc(DT_NODE_PROVIDER);
2512 dnp->dn_provname = name;
2513 dnp->dn_probes = probes;
2515 if (strchr(name, '`') != NULL) {
2516 dnerror(dnp, D_PROV_BADNAME, "provider name may not "
2517 "contain scoping operator: %s\n", name);
2520 if ((len = strlen(name)) >= DTRACE_PROVNAMELEN) {
2521 dnerror(dnp, D_PROV_BADNAME, "provider name may not exceed %d "
2522 "characters: %s\n", DTRACE_PROVNAMELEN - 1, name);
2525 if (isdigit(name[len - 1])) {
2526 dnerror(dnp, D_PROV_BADNAME, "provider name may not "
2527 "end with a digit: %s\n", name);
2531 * Check to see if the provider is already defined or visible through
2532 * dtrace(7D). If so, set dn_provred to treat it as a re-declaration.
2533 * If not, create a new provider and set its interface-only flag. This
2534 * flag may be cleared later by calls made to dt_probe_declare().
2536 if ((dnp->dn_provider = dt_provider_lookup(dtp, name)) != NULL)
2537 dnp->dn_provred = B_TRUE;
2538 else if ((dnp->dn_provider = dt_provider_create(dtp, name)) == NULL)
2539 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2541 dnp->dn_provider->pv_flags |= DT_PROVIDER_INTF;
2544 * Store all parse nodes created since we consumed the DT_KEY_PROVIDER
2545 * token with the provider and then restore our lexing state to CLAUSE.
2546 * Note that if dnp->dn_provred is true, we may end up storing dups of
2547 * a provider's interface and implementation: we eat this space because
2548 * the implementation will likely need to redeclare probe members, and
2549 * therefore may result in those member nodes becoming persistent.
2551 for (lnp = yypcb->pcb_list; lnp->dn_link != NULL; lnp = lnp->dn_link)
2552 continue; /* skip to end of allocation list */
2554 lnp->dn_link = dnp->dn_provider->pv_nodes;
2555 dnp->dn_provider->pv_nodes = yypcb->pcb_list;
2557 yybegin(YYS_CLAUSE);
2562 dt_node_program(dt_node_t *lnp)
2564 dt_node_t *dnp = dt_node_alloc(DT_NODE_PROG);
2570 * This function provides the underlying implementation of cooking an
2571 * identifier given its node, a hash of dynamic identifiers, an identifier
2572 * kind, and a boolean flag indicating whether we are allowed to instantiate
2573 * a new identifier if the string is not found. This function is either
2574 * called from dt_cook_ident(), below, or directly by the various cooking
2575 * routines that are allowed to instantiate identifiers (e.g. op2 TOK_ASGN).
2578 dt_xcook_ident(dt_node_t *dnp, dt_idhash_t *dhp, uint_t idkind, int create)
2580 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
2581 const char *sname = dt_idhash_name(dhp);
2584 dtrace_attribute_t attr = _dtrace_defattr;
2586 dtrace_syminfo_t dts;
2589 const char *scope, *mark;
2594 * Look for scoping marks in the identifier. If one is found, set our
2595 * scope to either DTRACE_OBJ_KMODS or UMODS or to the first part of
2596 * the string that specifies the scope using an explicit module name.
2597 * If two marks in a row are found, set 'uref' (user symbol reference).
2598 * Otherwise we set scope to DTRACE_OBJ_EXEC, indicating that normal
2599 * scope is desired and we should search the specified idhash.
2601 if ((name = strrchr(dnp->dn_string, '`')) != NULL) {
2602 if (name > dnp->dn_string && name[-1] == '`') {
2607 if (name == dnp->dn_string + uref)
2608 scope = uref ? DTRACE_OBJ_UMODS : DTRACE_OBJ_KMODS;
2610 scope = dnp->dn_string;
2612 *name++ = '\0'; /* leave name pointing after scoping mark */
2613 dnkind = DT_NODE_VAR;
2615 } else if (idkind == DT_IDENT_AGG) {
2616 scope = DTRACE_OBJ_EXEC;
2617 name = dnp->dn_string + 1;
2618 dnkind = DT_NODE_AGG;
2620 scope = DTRACE_OBJ_EXEC;
2621 name = dnp->dn_string;
2622 dnkind = DT_NODE_VAR;
2626 * If create is set to false, and we fail our idhash lookup, preset
2627 * the errno code to EDT_NOVAR for our final error message below.
2628 * If we end up calling dtrace_lookup_by_name(), it will reset the
2629 * errno appropriately and that error will be reported instead.
2631 (void) dt_set_errno(dtp, EDT_NOVAR);
2632 mark = uref ? "``" : "`";
2634 if (scope == DTRACE_OBJ_EXEC && (
2635 (dhp != dtp->dt_globals &&
2636 (idp = dt_idhash_lookup(dhp, name)) != NULL) ||
2637 (dhp == dtp->dt_globals &&
2638 (idp = dt_idstack_lookup(&yypcb->pcb_globals, name)) != NULL))) {
2640 * Check that we are referencing the ident in the manner that
2641 * matches its type if this is a global lookup. In the TLS or
2642 * local case, we don't know how the ident will be used until
2643 * the time operator -> is seen; more parsing is needed.
2645 if (idp->di_kind != idkind && dhp == dtp->dt_globals) {
2646 xyerror(D_IDENT_BADREF, "%s '%s' may not be referenced "
2647 "as %s\n", dt_idkind_name(idp->di_kind),
2648 idp->di_name, dt_idkind_name(idkind));
2652 * Arrays and aggregations are not cooked individually. They
2653 * have dynamic types and must be referenced using operator [].
2654 * This is handled explicitly by the code for DT_TOK_LBRAC.
2656 if (idp->di_kind != DT_IDENT_ARRAY &&
2657 idp->di_kind != DT_IDENT_AGG)
2658 attr = dt_ident_cook(dnp, idp, NULL);
2660 dt_node_type_assign(dnp,
2661 DT_DYN_CTFP(dtp), DT_DYN_TYPE(dtp));
2662 attr = idp->di_attr;
2665 free(dnp->dn_string);
2666 dnp->dn_string = NULL;
2667 dnp->dn_kind = dnkind;
2668 dnp->dn_ident = idp;
2669 dnp->dn_flags |= DT_NF_LVALUE;
2671 if (idp->di_flags & DT_IDFLG_WRITE)
2672 dnp->dn_flags |= DT_NF_WRITABLE;
2674 dt_node_attr_assign(dnp, attr);
2676 } else if (dhp == dtp->dt_globals && scope != DTRACE_OBJ_EXEC &&
2677 dtrace_lookup_by_name(dtp, scope, name, &sym, &dts) == 0) {
2679 dt_module_t *mp = dt_module_lookup_by_name(dtp, dts.dts_object);
2680 int umod = (mp->dm_flags & DT_DM_KERNEL) == 0;
2681 static const char *const kunames[] = { "kernel", "user" };
2683 dtrace_typeinfo_t dtt;
2684 dtrace_syminfo_t *sip;
2687 xyerror(D_SYM_BADREF, "%s module '%s' symbol '%s' may "
2688 "not be referenced as a %s symbol\n", kunames[umod],
2689 dts.dts_object, dts.dts_name, kunames[uref]);
2692 if (dtrace_symbol_type(dtp, &sym, &dts, &dtt) != 0) {
2694 * For now, we special-case EDT_DATAMODEL to clarify
2695 * that mixed data models are not currently supported.
2697 if (dtp->dt_errno == EDT_DATAMODEL) {
2698 xyerror(D_SYM_MODEL, "cannot use %s symbol "
2699 "%s%s%s in a %s D program\n",
2700 dt_module_modelname(mp),
2701 dts.dts_object, mark, dts.dts_name,
2702 dt_module_modelname(dtp->dt_ddefs));
2705 xyerror(D_SYM_NOTYPES,
2706 "no symbolic type information is available for "
2707 "%s%s%s: %s\n", dts.dts_object, mark, dts.dts_name,
2708 dtrace_errmsg(dtp, dtrace_errno(dtp)));
2711 idp = dt_ident_create(name, DT_IDENT_SYMBOL, 0, 0,
2712 _dtrace_symattr, 0, &dt_idops_thaw, NULL, dtp->dt_gen);
2715 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2717 if (mp->dm_flags & DT_DM_PRIMARY)
2718 idp->di_flags |= DT_IDFLG_PRIM;
2720 idp->di_next = dtp->dt_externs;
2721 dtp->dt_externs = idp;
2723 if ((sip = malloc(sizeof (dtrace_syminfo_t))) == NULL)
2724 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2726 bcopy(&dts, sip, sizeof (dtrace_syminfo_t));
2728 idp->di_ctfp = dtt.dtt_ctfp;
2729 idp->di_type = dtt.dtt_type;
2731 free(dnp->dn_string);
2732 dnp->dn_string = NULL;
2733 dnp->dn_kind = DT_NODE_SYM;
2734 dnp->dn_ident = idp;
2735 dnp->dn_flags |= DT_NF_LVALUE;
2737 dt_node_type_assign(dnp, dtt.dtt_ctfp, dtt.dtt_type);
2738 dt_node_attr_assign(dnp, _dtrace_symattr);
2741 idp->di_flags |= DT_IDFLG_USER;
2742 dnp->dn_flags |= DT_NF_USERLAND;
2745 } else if (scope == DTRACE_OBJ_EXEC && create == B_TRUE) {
2746 uint_t flags = DT_IDFLG_WRITE;
2749 if (dt_idhash_nextid(dhp, &id) == -1) {
2750 xyerror(D_ID_OFLOW, "cannot create %s: limit on number "
2751 "of %s variables exceeded\n", name, sname);
2754 if (dhp == yypcb->pcb_locals)
2755 flags |= DT_IDFLG_LOCAL;
2756 else if (dhp == dtp->dt_tls)
2757 flags |= DT_IDFLG_TLS;
2759 dt_dprintf("create %s %s variable %s, id=%u\n",
2760 sname, dt_idkind_name(idkind), name, id);
2762 if (idkind == DT_IDENT_ARRAY || idkind == DT_IDENT_AGG) {
2763 idp = dt_idhash_insert(dhp, name,
2764 idkind, flags, id, _dtrace_defattr, 0,
2765 &dt_idops_assc, NULL, dtp->dt_gen);
2767 idp = dt_idhash_insert(dhp, name,
2768 idkind, flags, id, _dtrace_defattr, 0,
2769 &dt_idops_thaw, NULL, dtp->dt_gen);
2773 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2776 * Arrays and aggregations are not cooked individually. They
2777 * have dynamic types and must be referenced using operator [].
2778 * This is handled explicitly by the code for DT_TOK_LBRAC.
2780 if (idp->di_kind != DT_IDENT_ARRAY &&
2781 idp->di_kind != DT_IDENT_AGG)
2782 attr = dt_ident_cook(dnp, idp, NULL);
2784 dt_node_type_assign(dnp,
2785 DT_DYN_CTFP(dtp), DT_DYN_TYPE(dtp));
2786 attr = idp->di_attr;
2789 free(dnp->dn_string);
2790 dnp->dn_string = NULL;
2791 dnp->dn_kind = dnkind;
2792 dnp->dn_ident = idp;
2793 dnp->dn_flags |= DT_NF_LVALUE | DT_NF_WRITABLE;
2795 dt_node_attr_assign(dnp, attr);
2797 } else if (scope != DTRACE_OBJ_EXEC) {
2798 xyerror(D_IDENT_UNDEF, "failed to resolve %s%s%s: %s\n",
2799 dnp->dn_string, mark, name,
2800 dtrace_errmsg(dtp, dtrace_errno(dtp)));
2802 xyerror(D_IDENT_UNDEF, "failed to resolve %s: %s\n",
2803 dnp->dn_string, dtrace_errmsg(dtp, dtrace_errno(dtp)));
2808 dt_cook_ident(dt_node_t *dnp, uint_t idflags)
2810 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
2812 if (dnp->dn_op == DT_TOK_AGG)
2813 dt_xcook_ident(dnp, dtp->dt_aggs, DT_IDENT_AGG, B_FALSE);
2815 dt_xcook_ident(dnp, dtp->dt_globals, DT_IDENT_SCALAR, B_FALSE);
2817 return (dt_node_cook(dnp, idflags));
2821 * Since operators [ and -> can instantiate new variables before we know
2822 * whether the reference is for a read or a write, we need to check read
2823 * references to determine if the identifier is currently dt_ident_unref().
2824 * If so, we report that this first access was to an undefined variable.
2827 dt_cook_var(dt_node_t *dnp, uint_t idflags)
2829 dt_ident_t *idp = dnp->dn_ident;
2831 if ((idflags & DT_IDFLG_REF) && dt_ident_unref(idp)) {
2832 dnerror(dnp, D_VAR_UNDEF,
2833 "%s%s has not yet been declared or assigned\n",
2834 (idp->di_flags & DT_IDFLG_LOCAL) ? "this->" :
2835 (idp->di_flags & DT_IDFLG_TLS) ? "self->" : "",
2839 dt_node_attr_assign(dnp, dt_ident_cook(dnp, idp, &dnp->dn_args));
2845 dt_cook_func(dt_node_t *dnp, uint_t idflags)
2847 dt_node_attr_assign(dnp,
2848 dt_ident_cook(dnp, dnp->dn_ident, &dnp->dn_args));
2854 dt_cook_op1(dt_node_t *dnp, uint_t idflags)
2856 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
2857 dt_node_t *cp = dnp->dn_child;
2859 char n[DT_TYPE_NAMELEN];
2860 dtrace_typeinfo_t dtt;
2865 ctf_id_t type, base;
2868 if (dnp->dn_op == DT_TOK_PREINC || dnp->dn_op == DT_TOK_POSTINC ||
2869 dnp->dn_op == DT_TOK_PREDEC || dnp->dn_op == DT_TOK_POSTDEC)
2870 idflags = DT_IDFLG_REF | DT_IDFLG_MOD;
2872 idflags = DT_IDFLG_REF;
2875 * We allow the unary ++ and -- operators to instantiate new scalar
2876 * variables if applied to an identifier; otherwise just cook as usual.
2878 if (cp->dn_kind == DT_NODE_IDENT && (idflags & DT_IDFLG_MOD))
2879 dt_xcook_ident(cp, dtp->dt_globals, DT_IDENT_SCALAR, B_TRUE);
2881 cp = dnp->dn_child = dt_node_cook(cp, 0); /* don't set idflags yet */
2883 if (cp->dn_kind == DT_NODE_VAR && dt_ident_unref(cp->dn_ident)) {
2884 if (dt_type_lookup("int64_t", &dtt) != 0)
2885 xyerror(D_TYPE_ERR, "failed to lookup int64_t\n");
2887 dt_ident_type_assign(cp->dn_ident, dtt.dtt_ctfp, dtt.dtt_type);
2888 dt_node_type_assign(cp, dtt.dtt_ctfp, dtt.dtt_type);
2891 if (cp->dn_kind == DT_NODE_VAR)
2892 cp->dn_ident->di_flags |= idflags;
2894 switch (dnp->dn_op) {
2897 * If the deref operator is applied to a translated pointer,
2898 * we can just set our output type to the base translation.
2900 if ((idp = dt_node_resolve(cp, DT_IDENT_XLPTR)) != NULL) {
2901 dt_xlator_t *dxp = idp->di_data;
2903 dnp->dn_ident = &dxp->dx_souid;
2904 dt_node_type_assign(dnp,
2905 DT_DYN_CTFP(dtp), DT_DYN_TYPE(dtp));
2909 type = ctf_type_resolve(cp->dn_ctfp, cp->dn_type);
2910 kind = ctf_type_kind(cp->dn_ctfp, type);
2912 if (kind == CTF_K_ARRAY) {
2913 if (ctf_array_info(cp->dn_ctfp, type, &r) != 0) {
2914 dtp->dt_ctferr = ctf_errno(cp->dn_ctfp);
2915 longjmp(yypcb->pcb_jmpbuf, EDT_CTF);
2917 type = r.ctr_contents;
2918 } else if (kind == CTF_K_POINTER) {
2919 type = ctf_type_reference(cp->dn_ctfp, type);
2921 xyerror(D_DEREF_NONPTR,
2922 "cannot dereference non-pointer type\n");
2925 dt_node_type_assign(dnp, cp->dn_ctfp, type);
2926 base = ctf_type_resolve(cp->dn_ctfp, type);
2927 kind = ctf_type_kind(cp->dn_ctfp, base);
2929 if (kind == CTF_K_INTEGER && ctf_type_encoding(cp->dn_ctfp,
2930 base, &e) == 0 && IS_VOID(e)) {
2931 xyerror(D_DEREF_VOID,
2932 "cannot dereference pointer to void\n");
2935 if (kind == CTF_K_FUNCTION) {
2936 xyerror(D_DEREF_FUNC,
2937 "cannot dereference pointer to function\n");
2940 if (kind != CTF_K_ARRAY || dt_node_is_string(dnp))
2941 dnp->dn_flags |= DT_NF_LVALUE; /* see K&R[A7.4.3] */
2944 * If we propagated the l-value bit and the child operand was
2945 * a writable D variable or a binary operation of the form
2946 * a + b where a is writable, then propagate the writable bit.
2947 * This is necessary to permit assignments to scalar arrays,
2948 * which are converted to expressions of the form *(a + i).
2950 if ((cp->dn_flags & DT_NF_WRITABLE) ||
2951 (cp->dn_kind == DT_NODE_OP2 && cp->dn_op == DT_TOK_ADD &&
2952 (cp->dn_left->dn_flags & DT_NF_WRITABLE)))
2953 dnp->dn_flags |= DT_NF_WRITABLE;
2955 if ((cp->dn_flags & DT_NF_USERLAND) &&
2956 (kind == CTF_K_POINTER || (dnp->dn_flags & DT_NF_REF)))
2957 dnp->dn_flags |= DT_NF_USERLAND;
2962 if (!dt_node_is_arith(cp)) {
2963 xyerror(D_OP_ARITH, "operator %s requires an operand "
2964 "of arithmetic type\n", opstr(dnp->dn_op));
2966 dt_node_type_propagate(cp, dnp); /* see K&R[A7.4.4-6] */
2970 if (!dt_node_is_integer(cp)) {
2971 xyerror(D_OP_INT, "operator %s requires an operand of "
2972 "integral type\n", opstr(dnp->dn_op));
2974 dt_node_type_propagate(cp, dnp); /* see K&R[A7.4.4-6] */
2978 if (!dt_node_is_scalar(cp)) {
2979 xyerror(D_OP_SCALAR, "operator %s requires an operand "
2980 "of scalar type\n", opstr(dnp->dn_op));
2982 dt_node_type_assign(dnp, DT_INT_CTFP(dtp), DT_INT_TYPE(dtp));
2986 if (cp->dn_kind == DT_NODE_VAR || cp->dn_kind == DT_NODE_AGG) {
2987 xyerror(D_ADDROF_VAR,
2988 "cannot take address of dynamic variable\n");
2991 if (dt_node_is_dynamic(cp)) {
2992 xyerror(D_ADDROF_VAR,
2993 "cannot take address of dynamic object\n");
2996 if (!(cp->dn_flags & DT_NF_LVALUE)) {
2997 xyerror(D_ADDROF_LVAL, /* see K&R[A7.4.2] */
2998 "unacceptable operand for unary & operator\n");
3001 if (cp->dn_flags & DT_NF_BITFIELD) {
3002 xyerror(D_ADDROF_BITFIELD,
3003 "cannot take address of bit-field\n");
3006 dtt.dtt_object = NULL;
3007 dtt.dtt_ctfp = cp->dn_ctfp;
3008 dtt.dtt_type = cp->dn_type;
3010 if (dt_type_pointer(&dtt) == -1) {
3011 xyerror(D_TYPE_ERR, "cannot find type for \"&\": %s*\n",
3012 dt_node_type_name(cp, n, sizeof (n)));
3015 dt_node_type_assign(dnp, dtt.dtt_ctfp, dtt.dtt_type);
3017 if (cp->dn_flags & DT_NF_USERLAND)
3018 dnp->dn_flags |= DT_NF_USERLAND;
3022 if (cp->dn_flags & DT_NF_BITFIELD) {
3023 xyerror(D_SIZEOF_BITFIELD,
3024 "cannot apply sizeof to a bit-field\n");
3027 if (dt_node_sizeof(cp) == 0) {
3028 xyerror(D_SIZEOF_TYPE, "cannot apply sizeof to an "
3029 "operand of unknown size\n");
3032 dt_node_type_assign(dnp, dtp->dt_ddefs->dm_ctfp,
3033 ctf_lookup_by_name(dtp->dt_ddefs->dm_ctfp, "size_t"));
3036 case DT_TOK_STRINGOF:
3037 if (!dt_node_is_scalar(cp) && !dt_node_is_pointer(cp) &&
3038 !dt_node_is_strcompat(cp)) {
3039 xyerror(D_STRINGOF_TYPE,
3040 "cannot apply stringof to a value of type %s\n",
3041 dt_node_type_name(cp, n, sizeof (n)));
3043 dt_node_type_assign(dnp, DT_STR_CTFP(dtp), DT_STR_TYPE(dtp));
3047 case DT_TOK_POSTINC:
3049 case DT_TOK_POSTDEC:
3050 if (dt_node_is_scalar(cp) == 0) {
3051 xyerror(D_OP_SCALAR, "operator %s requires operand of "
3052 "scalar type\n", opstr(dnp->dn_op));
3055 if (dt_node_is_vfptr(cp)) {
3056 xyerror(D_OP_VFPTR, "operator %s requires an operand "
3057 "of known size\n", opstr(dnp->dn_op));
3060 if (!(cp->dn_flags & DT_NF_LVALUE)) {
3061 xyerror(D_OP_LVAL, "operator %s requires modifiable "
3062 "lvalue as an operand\n", opstr(dnp->dn_op));
3065 if (!(cp->dn_flags & DT_NF_WRITABLE)) {
3066 xyerror(D_OP_WRITE, "operator %s can only be applied "
3067 "to a writable variable\n", opstr(dnp->dn_op));
3070 dt_node_type_propagate(cp, dnp); /* see K&R[A7.4.1] */
3074 xyerror(D_UNKNOWN, "invalid unary op %s\n", opstr(dnp->dn_op));
3077 dt_node_attr_assign(dnp, cp->dn_attr);
3082 dt_cook_op2(dt_node_t *dnp, uint_t idflags)
3084 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
3085 dt_node_t *lp = dnp->dn_left;
3086 dt_node_t *rp = dnp->dn_right;
3087 int op = dnp->dn_op;
3092 int kind, val, uref;
3095 char n1[DT_TYPE_NAMELEN];
3096 char n2[DT_TYPE_NAMELEN];
3099 * The expression E1[E2] is identical by definition to *((E1)+(E2)) so
3100 * we convert "[" to "+" and glue on "*" at the end (see K&R[A7.3.1])
3101 * unless the left-hand side is an untyped D scalar, associative array,
3102 * or aggregation. In these cases, we proceed to case DT_TOK_LBRAC and
3103 * handle associative array and aggregation references there.
3105 if (op == DT_TOK_LBRAC) {
3106 if (lp->dn_kind == DT_NODE_IDENT) {
3110 if (lp->dn_op == DT_TOK_AGG) {
3112 idp = dt_idhash_lookup(dhp, lp->dn_string + 1);
3113 idkind = DT_IDENT_AGG;
3115 dhp = dtp->dt_globals;
3116 idp = dt_idstack_lookup(
3117 &yypcb->pcb_globals, lp->dn_string);
3118 idkind = DT_IDENT_ARRAY;
3121 if (idp == NULL || dt_ident_unref(idp))
3122 dt_xcook_ident(lp, dhp, idkind, B_TRUE);
3124 dt_xcook_ident(lp, dhp, idp->di_kind, B_FALSE);
3126 lp = dnp->dn_left = dt_node_cook(lp, 0);
3129 * Switch op to '+' for *(E1 + E2) array mode in these cases:
3130 * (a) lp is a DT_IDENT_ARRAY variable that has already been
3131 * referenced using [] notation (dn_args != NULL).
3132 * (b) lp is a non-ARRAY variable that has already been given
3133 * a type by assignment or declaration (!dt_ident_unref())
3134 * (c) lp is neither a variable nor an aggregation
3136 if (lp->dn_kind == DT_NODE_VAR) {
3137 if (lp->dn_ident->di_kind == DT_IDENT_ARRAY) {
3138 if (lp->dn_args != NULL)
3140 } else if (!dt_ident_unref(lp->dn_ident))
3142 } else if (lp->dn_kind != DT_NODE_AGG)
3150 lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF);
3151 rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
3153 if (!dt_node_is_integer(lp) || !dt_node_is_integer(rp)) {
3154 xyerror(D_OP_INT, "operator %s requires operands of "
3155 "integral type\n", opstr(op));
3158 dt_node_promote(lp, rp, dnp); /* see K&R[A7.11-13] */
3163 lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF);
3164 rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
3166 if (!dt_node_is_integer(lp) || !dt_node_is_integer(rp)) {
3167 xyerror(D_OP_INT, "operator %s requires operands of "
3168 "integral type\n", opstr(op));
3171 dt_node_type_propagate(lp, dnp); /* see K&R[A7.8] */
3172 dt_node_attr_assign(dnp, dt_attr_min(lp->dn_attr, rp->dn_attr));
3176 lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF);
3177 rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
3179 if (!dt_node_is_integer(lp) || !dt_node_is_integer(rp)) {
3180 xyerror(D_OP_INT, "operator %s requires operands of "
3181 "integral type\n", opstr(op));
3184 dt_node_promote(lp, rp, dnp); /* see K&R[A7.6] */
3189 lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF);
3190 rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
3192 if (!dt_node_is_arith(lp) || !dt_node_is_arith(rp)) {
3193 xyerror(D_OP_ARITH, "operator %s requires operands of "
3194 "arithmetic type\n", opstr(op));
3197 dt_node_promote(lp, rp, dnp); /* see K&R[A7.6] */
3203 lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF);
3204 rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
3206 if (!dt_node_is_scalar(lp) || !dt_node_is_scalar(rp)) {
3207 xyerror(D_OP_SCALAR, "operator %s requires operands "
3208 "of scalar type\n", opstr(op));
3211 dt_node_type_assign(dnp, DT_INT_CTFP(dtp), DT_INT_TYPE(dtp));
3212 dt_node_attr_assign(dnp, dt_attr_min(lp->dn_attr, rp->dn_attr));
3222 * The D comparison operators provide the ability to transform
3223 * a right-hand identifier into a corresponding enum tag value
3224 * if the left-hand side is an enum type. To do this, we cook
3225 * the left-hand side, and then see if the right-hand side is
3226 * an unscoped identifier defined in the enum. If so, we
3227 * convert into an integer constant node with the tag's value.
3229 lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF);
3231 kind = ctf_type_kind(lp->dn_ctfp,
3232 ctf_type_resolve(lp->dn_ctfp, lp->dn_type));
3234 if (kind == CTF_K_ENUM && rp->dn_kind == DT_NODE_IDENT &&
3235 strchr(rp->dn_string, '`') == NULL && ctf_enum_value(
3236 lp->dn_ctfp, lp->dn_type, rp->dn_string, &val) == 0) {
3238 if ((idp = dt_idstack_lookup(&yypcb->pcb_globals,
3239 rp->dn_string)) != NULL) {
3240 xyerror(D_IDENT_AMBIG,
3241 "ambiguous use of operator %s: %s is "
3242 "both a %s enum tag and a global %s\n",
3243 opstr(op), rp->dn_string,
3244 dt_node_type_name(lp, n1, sizeof (n1)),
3245 dt_idkind_name(idp->di_kind));
3248 free(rp->dn_string);
3249 rp->dn_string = NULL;
3250 rp->dn_kind = DT_NODE_INT;
3251 rp->dn_flags |= DT_NF_COOKED;
3252 rp->dn_op = DT_TOK_INT;
3253 rp->dn_value = (intmax_t)val;
3255 dt_node_type_assign(rp, lp->dn_ctfp, lp->dn_type);
3256 dt_node_attr_assign(rp, _dtrace_symattr);
3259 rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
3262 * The rules for type checking for the relational operators are
3263 * described in the ANSI-C spec (see K&R[A7.9-10]). We perform
3264 * the various tests in order from least to most expensive. We
3265 * also allow derived strings to be compared as a first-class
3266 * type (resulting in a strcmp(3C)-style comparison), and we
3267 * slightly relax the A7.9 rules to permit void pointer
3268 * comparisons as in A7.10. Our users won't be confused by
3269 * this since they understand pointers are just numbers, and
3270 * relaxing this constraint simplifies the implementation.
3272 if (ctf_type_compat(lp->dn_ctfp, lp->dn_type,
3273 rp->dn_ctfp, rp->dn_type))
3275 else if (dt_node_is_integer(lp) && dt_node_is_integer(rp))
3277 else if (dt_node_is_strcompat(lp) && dt_node_is_strcompat(rp) &&
3278 (dt_node_is_string(lp) || dt_node_is_string(rp)))
3280 else if (dt_node_is_ptrcompat(lp, rp, NULL, NULL) == 0) {
3281 xyerror(D_OP_INCOMPAT, "operands have "
3282 "incompatible types: \"%s\" %s \"%s\"\n",
3283 dt_node_type_name(lp, n1, sizeof (n1)), opstr(op),
3284 dt_node_type_name(rp, n2, sizeof (n2)));
3287 dt_node_type_assign(dnp, DT_INT_CTFP(dtp), DT_INT_TYPE(dtp));
3288 dt_node_attr_assign(dnp, dt_attr_min(lp->dn_attr, rp->dn_attr));
3294 * The rules for type checking for the additive operators are
3295 * described in the ANSI-C spec (see K&R[A7.7]). Pointers and
3296 * integers may be manipulated according to specific rules. In
3297 * these cases D permits strings to be treated as pointers.
3299 int lp_is_ptr, lp_is_int, rp_is_ptr, rp_is_int;
3301 lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF);
3302 rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
3304 lp_is_ptr = dt_node_is_string(lp) ||
3305 (dt_node_is_pointer(lp) && !dt_node_is_vfptr(lp));
3306 lp_is_int = dt_node_is_integer(lp);
3308 rp_is_ptr = dt_node_is_string(rp) ||
3309 (dt_node_is_pointer(rp) && !dt_node_is_vfptr(rp));
3310 rp_is_int = dt_node_is_integer(rp);
3312 if (lp_is_int && rp_is_int) {
3313 dt_type_promote(lp, rp, &ctfp, &type);
3315 } else if (lp_is_ptr && rp_is_int) {
3318 uref = lp->dn_flags & DT_NF_USERLAND;
3319 } else if (lp_is_int && rp_is_ptr && op == DT_TOK_ADD) {
3322 uref = rp->dn_flags & DT_NF_USERLAND;
3323 } else if (lp_is_ptr && rp_is_ptr && op == DT_TOK_SUB &&
3324 dt_node_is_ptrcompat(lp, rp, NULL, NULL)) {
3325 ctfp = dtp->dt_ddefs->dm_ctfp;
3326 type = ctf_lookup_by_name(ctfp, "ptrdiff_t");
3329 xyerror(D_OP_INCOMPAT, "operands have incompatible "
3330 "types: \"%s\" %s \"%s\"\n",
3331 dt_node_type_name(lp, n1, sizeof (n1)), opstr(op),
3332 dt_node_type_name(rp, n2, sizeof (n2)));
3335 dt_node_type_assign(dnp, ctfp, type);
3336 dt_node_attr_assign(dnp, dt_attr_min(lp->dn_attr, rp->dn_attr));
3339 dnp->dn_flags |= DT_NF_USERLAND;
3349 if (lp->dn_kind == DT_NODE_IDENT) {
3350 dt_xcook_ident(lp, dtp->dt_globals,
3351 DT_IDENT_SCALAR, B_TRUE);
3355 dt_node_cook(lp, DT_IDFLG_REF | DT_IDFLG_MOD);
3357 rp = dnp->dn_right =
3358 dt_node_cook(rp, DT_IDFLG_REF | DT_IDFLG_MOD);
3360 if (!dt_node_is_integer(lp) || !dt_node_is_integer(rp)) {
3361 xyerror(D_OP_INT, "operator %s requires operands of "
3362 "integral type\n", opstr(op));
3368 if (lp->dn_kind == DT_NODE_IDENT) {
3369 dt_xcook_ident(lp, dtp->dt_globals,
3370 DT_IDENT_SCALAR, B_TRUE);
3374 dt_node_cook(lp, DT_IDFLG_REF | DT_IDFLG_MOD);
3376 rp = dnp->dn_right =
3377 dt_node_cook(rp, DT_IDFLG_REF | DT_IDFLG_MOD);
3379 if (!dt_node_is_arith(lp) || !dt_node_is_arith(rp)) {
3380 xyerror(D_OP_ARITH, "operator %s requires operands of "
3381 "arithmetic type\n", opstr(op));
3387 * If the left-hand side is an identifier, attempt to resolve
3388 * it as either an aggregation or scalar variable. We pass
3389 * B_TRUE to dt_xcook_ident to indicate that a new variable can
3390 * be created if no matching variable exists in the namespace.
3392 if (lp->dn_kind == DT_NODE_IDENT) {
3393 if (lp->dn_op == DT_TOK_AGG) {
3394 dt_xcook_ident(lp, dtp->dt_aggs,
3395 DT_IDENT_AGG, B_TRUE);
3397 dt_xcook_ident(lp, dtp->dt_globals,
3398 DT_IDENT_SCALAR, B_TRUE);
3402 lp = dnp->dn_left = dt_node_cook(lp, 0); /* don't set mod yet */
3403 rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
3406 * If the left-hand side is an aggregation, verify that we are
3407 * assigning it the result of an aggregating function. Once
3408 * we've done so, hide the func node in the aggregation and
3409 * return the aggregation itself up to the parse tree parent.
3410 * This transformation is legal since the assigned function
3411 * cannot change identity across disjoint cooking passes and
3412 * the argument list subtree is retained for later cooking.
3414 if (lp->dn_kind == DT_NODE_AGG) {
3415 const char *aname = lp->dn_ident->di_name;
3416 dt_ident_t *oid = lp->dn_ident->di_iarg;
3418 if (rp->dn_kind != DT_NODE_FUNC ||
3419 rp->dn_ident->di_kind != DT_IDENT_AGGFUNC) {
3421 "@%s must be assigned the result of "
3422 "an aggregating function\n", aname);
3425 if (oid != NULL && oid != rp->dn_ident) {
3426 xyerror(D_AGG_REDEF,
3427 "aggregation redefined: @%s\n\t "
3428 "current: @%s = %s( )\n\tprevious: @%s = "
3429 "%s( ) : line %d\n", aname, aname,
3430 rp->dn_ident->di_name, aname, oid->di_name,
3431 lp->dn_ident->di_lineno);
3432 } else if (oid == NULL)
3433 lp->dn_ident->di_iarg = rp->dn_ident;
3436 * Do not allow multiple aggregation assignments in a
3437 * single statement, e.g. (@a = count()) = count();
3438 * We produce a message as if the result of aggregating
3439 * function does not propagate DT_NF_LVALUE.
3441 if (lp->dn_aggfun != NULL) {
3442 xyerror(D_OP_LVAL, "operator = requires "
3443 "modifiable lvalue as an operand\n");
3447 lp = dt_node_cook(lp, DT_IDFLG_MOD);
3449 dnp->dn_left = dnp->dn_right = NULL;
3456 * If the right-hand side is a dynamic variable that is the
3457 * output of a translator, our result is the translated type.
3459 if ((idp = dt_node_resolve(rp, DT_IDENT_XLSOU)) != NULL) {
3460 ctfp = idp->di_ctfp;
3461 type = idp->di_type;
3462 uref = idp->di_flags & DT_IDFLG_USER;
3466 uref = rp->dn_flags & DT_NF_USERLAND;
3470 * If the left-hand side of an assignment statement is a virgin
3471 * variable created by this compilation pass, reset the type of
3472 * this variable to the type of the right-hand side.
3474 if (lp->dn_kind == DT_NODE_VAR &&
3475 dt_ident_unref(lp->dn_ident)) {
3476 dt_node_type_assign(lp, ctfp, type);
3477 dt_ident_type_assign(lp->dn_ident, ctfp, type);
3480 lp->dn_flags |= DT_NF_USERLAND;
3481 lp->dn_ident->di_flags |= DT_IDFLG_USER;
3485 if (lp->dn_kind == DT_NODE_VAR)
3486 lp->dn_ident->di_flags |= DT_IDFLG_MOD;
3489 * The rules for type checking for the assignment operators are
3490 * described in the ANSI-C spec (see K&R[A7.17]). We share
3491 * most of this code with the argument list checking code.
3493 if (!dt_node_is_string(lp)) {
3494 kind = ctf_type_kind(lp->dn_ctfp,
3495 ctf_type_resolve(lp->dn_ctfp, lp->dn_type));
3497 if (kind == CTF_K_ARRAY || kind == CTF_K_FUNCTION) {
3498 xyerror(D_OP_ARRFUN, "operator %s may not be "
3499 "applied to operand of type \"%s\"\n",
3501 dt_node_type_name(lp, n1, sizeof (n1)));
3505 if (idp != NULL && idp->di_kind == DT_IDENT_XLSOU &&
3506 ctf_type_compat(lp->dn_ctfp, lp->dn_type, ctfp, type))
3509 if (dt_node_is_argcompat(lp, rp))
3512 xyerror(D_OP_INCOMPAT,
3513 "operands have incompatible types: \"%s\" %s \"%s\"\n",
3514 dt_node_type_name(lp, n1, sizeof (n1)), opstr(op),
3515 dt_node_type_name(rp, n2, sizeof (n2)));
3520 if (lp->dn_kind == DT_NODE_IDENT) {
3521 dt_xcook_ident(lp, dtp->dt_globals,
3522 DT_IDENT_SCALAR, B_TRUE);
3526 dt_node_cook(lp, DT_IDFLG_REF | DT_IDFLG_MOD);
3528 rp = dnp->dn_right =
3529 dt_node_cook(rp, DT_IDFLG_REF | DT_IDFLG_MOD);
3531 if (dt_node_is_string(lp) || dt_node_is_string(rp)) {
3532 xyerror(D_OP_INCOMPAT, "operands have "
3533 "incompatible types: \"%s\" %s \"%s\"\n",
3534 dt_node_type_name(lp, n1, sizeof (n1)), opstr(op),
3535 dt_node_type_name(rp, n2, sizeof (n2)));
3539 * The rules for type checking for the assignment operators are
3540 * described in the ANSI-C spec (see K&R[A7.17]). To these
3541 * rules we add that only writable D nodes can be modified.
3543 if (dt_node_is_integer(lp) == 0 ||
3544 dt_node_is_integer(rp) == 0) {
3545 if (!dt_node_is_pointer(lp) || dt_node_is_vfptr(lp)) {
3547 "operator %s requires left-hand scalar "
3548 "operand of known size\n", opstr(op));
3549 } else if (dt_node_is_integer(rp) == 0 &&
3550 dt_node_is_ptrcompat(lp, rp, NULL, NULL) == 0) {
3551 xyerror(D_OP_INCOMPAT, "operands have "
3552 "incompatible types: \"%s\" %s \"%s\"\n",
3553 dt_node_type_name(lp, n1, sizeof (n1)),
3555 dt_node_type_name(rp, n2, sizeof (n2)));
3559 if (!(lp->dn_flags & DT_NF_LVALUE)) {
3560 xyerror(D_OP_LVAL, "operator %s requires modifiable "
3561 "lvalue as an operand\n", opstr(op));
3562 /* see K&R[A7.17] */
3565 if (!(lp->dn_flags & DT_NF_WRITABLE)) {
3566 xyerror(D_OP_WRITE, "operator %s can only be applied "
3567 "to a writable variable\n", opstr(op));
3570 dt_node_type_propagate(lp, dnp); /* see K&R[A7.17] */
3571 dt_node_attr_assign(dnp, dt_attr_min(lp->dn_attr, rp->dn_attr));
3576 * If the left-hand side of operator -> is the name "self",
3577 * then we permit a TLS variable to be created or referenced.
3579 if (lp->dn_kind == DT_NODE_IDENT &&
3580 strcmp(lp->dn_string, "self") == 0) {
3581 if (rp->dn_kind != DT_NODE_VAR) {
3582 dt_xcook_ident(rp, dtp->dt_tls,
3583 DT_IDENT_SCALAR, B_TRUE);
3587 rp = dt_node_cook(rp, idflags);
3589 dnp->dn_right = dnp->dn_left; /* avoid freeing rp */
3595 * If the left-hand side of operator -> is the name "this",
3596 * then we permit a local variable to be created or referenced.
3598 if (lp->dn_kind == DT_NODE_IDENT &&
3599 strcmp(lp->dn_string, "this") == 0) {
3600 if (rp->dn_kind != DT_NODE_VAR) {
3601 dt_xcook_ident(rp, yypcb->pcb_locals,
3602 DT_IDENT_SCALAR, B_TRUE);
3606 rp = dt_node_cook(rp, idflags);
3608 dnp->dn_right = dnp->dn_left; /* avoid freeing rp */
3616 lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF);
3618 if (rp->dn_kind != DT_NODE_IDENT) {
3619 xyerror(D_OP_IDENT, "operator %s must be followed by "
3620 "an identifier\n", opstr(op));
3623 if ((idp = dt_node_resolve(lp, DT_IDENT_XLSOU)) != NULL ||
3624 (idp = dt_node_resolve(lp, DT_IDENT_XLPTR)) != NULL) {
3626 * If the left-hand side is a translated struct or ptr,
3627 * the type of the left is the translation output type.
3629 dt_xlator_t *dxp = idp->di_data;
3631 if (dt_xlator_member(dxp, rp->dn_string) == NULL) {
3632 xyerror(D_XLATE_NOCONV,
3633 "translator does not define conversion "
3634 "for member: %s\n", rp->dn_string);
3637 ctfp = idp->di_ctfp;
3638 type = ctf_type_resolve(ctfp, idp->di_type);
3639 uref = idp->di_flags & DT_IDFLG_USER;
3642 type = ctf_type_resolve(ctfp, lp->dn_type);
3643 uref = lp->dn_flags & DT_NF_USERLAND;
3646 kind = ctf_type_kind(ctfp, type);
3648 if (op == DT_TOK_PTR) {
3649 if (kind != CTF_K_POINTER) {
3650 xyerror(D_OP_PTR, "operator %s must be "
3651 "applied to a pointer\n", opstr(op));
3653 type = ctf_type_reference(ctfp, type);
3654 type = ctf_type_resolve(ctfp, type);
3655 kind = ctf_type_kind(ctfp, type);
3659 * If we follow a reference to a forward declaration tag,
3660 * search the entire type space for the actual definition.
3662 while (kind == CTF_K_FORWARD) {
3663 char *tag = ctf_type_name(ctfp, type, n1, sizeof (n1));
3664 dtrace_typeinfo_t dtt;
3666 if (tag != NULL && dt_type_lookup(tag, &dtt) == 0 &&
3667 (dtt.dtt_ctfp != ctfp || dtt.dtt_type != type)) {
3668 ctfp = dtt.dtt_ctfp;
3669 type = ctf_type_resolve(ctfp, dtt.dtt_type);
3670 kind = ctf_type_kind(ctfp, type);
3672 xyerror(D_OP_INCOMPLETE,
3673 "operator %s cannot be applied to a "
3674 "forward declaration: no %s definition "
3675 "is available\n", opstr(op), tag);
3679 if (kind != CTF_K_STRUCT && kind != CTF_K_UNION) {
3680 if (op == DT_TOK_PTR) {
3681 xyerror(D_OP_SOU, "operator -> cannot be "
3682 "applied to pointer to type \"%s\"; must "
3683 "be applied to a struct or union pointer\n",
3684 ctf_type_name(ctfp, type, n1, sizeof (n1)));
3686 xyerror(D_OP_SOU, "operator %s cannot be "
3687 "applied to type \"%s\"; must be applied "
3688 "to a struct or union\n", opstr(op),
3689 ctf_type_name(ctfp, type, n1, sizeof (n1)));
3693 if (ctf_member_info(ctfp, type, rp->dn_string, &m) == CTF_ERR) {
3694 xyerror(D_TYPE_MEMBER,
3695 "%s is not a member of %s\n", rp->dn_string,
3696 ctf_type_name(ctfp, type, n1, sizeof (n1)));
3699 type = ctf_type_resolve(ctfp, m.ctm_type);
3700 kind = ctf_type_kind(ctfp, type);
3702 dt_node_type_assign(dnp, ctfp, m.ctm_type);
3703 dt_node_attr_assign(dnp, lp->dn_attr);
3705 if (op == DT_TOK_PTR && (kind != CTF_K_ARRAY ||
3706 dt_node_is_string(dnp)))
3707 dnp->dn_flags |= DT_NF_LVALUE; /* see K&R[A7.3.3] */
3709 if (op == DT_TOK_DOT && (lp->dn_flags & DT_NF_LVALUE) &&
3710 (kind != CTF_K_ARRAY || dt_node_is_string(dnp)))
3711 dnp->dn_flags |= DT_NF_LVALUE; /* see K&R[A7.3.3] */
3713 if (lp->dn_flags & DT_NF_WRITABLE)
3714 dnp->dn_flags |= DT_NF_WRITABLE;
3716 if (uref && (kind == CTF_K_POINTER ||
3717 (dnp->dn_flags & DT_NF_REF)))
3718 dnp->dn_flags |= DT_NF_USERLAND;
3721 case DT_TOK_LBRAC: {
3723 * If op is DT_TOK_LBRAC, we know from the special-case code at
3724 * the top that lp is either a D variable or an aggregation.
3729 * If the left-hand side is an aggregation, just set dn_aggtup
3730 * to the right-hand side and return the cooked aggregation.
3731 * This transformation is legal since we are just collapsing
3732 * nodes to simplify later processing, and the entire aggtup
3733 * parse subtree is retained for subsequent cooking passes.
3735 if (lp->dn_kind == DT_NODE_AGG) {
3736 if (lp->dn_aggtup != NULL) {
3737 xyerror(D_AGG_MDIM, "improper attempt to "
3738 "reference @%s as a multi-dimensional "
3739 "array\n", lp->dn_ident->di_name);
3743 lp = dt_node_cook(lp, 0);
3745 dnp->dn_left = dnp->dn_right = NULL;
3751 assert(lp->dn_kind == DT_NODE_VAR);
3755 * If the left-hand side is a non-global scalar that hasn't yet
3756 * been referenced or modified, it was just created by self->
3757 * or this-> and we can convert it from scalar to assoc array.
3759 if (idp->di_kind == DT_IDENT_SCALAR && dt_ident_unref(idp) &&
3760 (idp->di_flags & (DT_IDFLG_LOCAL | DT_IDFLG_TLS)) != 0) {
3762 if (idp->di_flags & DT_IDFLG_LOCAL) {
3763 xyerror(D_ARR_LOCAL,
3764 "local variables may not be used as "
3765 "associative arrays: %s\n", idp->di_name);
3768 dt_dprintf("morph variable %s (id %u) from scalar to "
3769 "array\n", idp->di_name, idp->di_id);
3771 dt_ident_morph(idp, DT_IDENT_ARRAY,
3772 &dt_idops_assc, NULL);
3775 if (idp->di_kind != DT_IDENT_ARRAY) {
3776 xyerror(D_IDENT_BADREF, "%s '%s' may not be referenced "
3777 "as %s\n", dt_idkind_name(idp->di_kind),
3778 idp->di_name, dt_idkind_name(DT_IDENT_ARRAY));
3782 * Now that we've confirmed our left-hand side is a DT_NODE_VAR
3783 * of idkind DT_IDENT_ARRAY, we need to splice the [ node from
3784 * the parse tree and leave a cooked DT_NODE_VAR in its place
3785 * where dn_args for the VAR node is the right-hand 'rp' tree,
3786 * as shown in the parse tree diagram below:
3789 * [ OP2 "[" ]=dnp [ VAR ]=dnp
3791 * / \ +- dn_args -> [ ??? ]=rp
3792 * [ VAR ]=lp [ ??? ]=rp
3794 * Since the final dt_node_cook(dnp) can fail using longjmp we
3795 * must perform the transformations as a group first by over-
3796 * writing 'dnp' to become the VAR node, so that the parse tree
3797 * is guaranteed to be in a consistent state if the cook fails.
3799 assert(lp->dn_kind == DT_NODE_VAR);
3800 assert(lp->dn_args == NULL);
3803 bcopy(lp, dnp, sizeof (dt_node_t));
3807 dnp->dn_list = NULL;
3810 return (dt_node_cook(dnp, idflags));
3813 case DT_TOK_XLATE: {
3816 assert(lp->dn_kind == DT_NODE_TYPE);
3817 rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
3818 dxp = dt_xlator_lookup(dtp, rp, lp, DT_XLATE_FUZZY);
3821 xyerror(D_XLATE_NONE,
3822 "cannot translate from \"%s\" to \"%s\"\n",
3823 dt_node_type_name(rp, n1, sizeof (n1)),
3824 dt_node_type_name(lp, n2, sizeof (n2)));
3827 dnp->dn_ident = dt_xlator_ident(dxp, lp->dn_ctfp, lp->dn_type);
3828 dt_node_type_assign(dnp, DT_DYN_CTFP(dtp), DT_DYN_TYPE(dtp));
3829 dt_node_attr_assign(dnp,
3830 dt_attr_min(rp->dn_attr, dnp->dn_ident->di_attr));
3835 ctf_id_t ltype, rtype;
3836 uint_t lkind, rkind;
3838 assert(lp->dn_kind == DT_NODE_TYPE);
3839 rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
3841 ltype = ctf_type_resolve(lp->dn_ctfp, lp->dn_type);
3842 lkind = ctf_type_kind(lp->dn_ctfp, ltype);
3844 rtype = ctf_type_resolve(rp->dn_ctfp, rp->dn_type);
3845 rkind = ctf_type_kind(rp->dn_ctfp, rtype);
3848 * The rules for casting are loosely explained in K&R[A7.5]
3849 * and K&R[A6]. Basically, we can cast to the same type or
3850 * same base type, between any kind of scalar values, from
3851 * arrays to pointers, and we can cast anything to void.
3852 * To these rules D adds casts from scalars to strings.
3854 if (ctf_type_compat(lp->dn_ctfp, lp->dn_type,
3855 rp->dn_ctfp, rp->dn_type))
3857 else if (dt_node_is_scalar(lp) &&
3858 (dt_node_is_scalar(rp) || rkind == CTF_K_FUNCTION))
3860 else if (dt_node_is_void(lp))
3862 else if (lkind == CTF_K_POINTER && dt_node_is_pointer(rp))
3864 else if (dt_node_is_string(lp) && (dt_node_is_scalar(rp) ||
3865 dt_node_is_pointer(rp) || dt_node_is_strcompat(rp)))
3868 xyerror(D_CAST_INVAL,
3869 "invalid cast expression: \"%s\" to \"%s\"\n",
3870 dt_node_type_name(rp, n1, sizeof (n1)),
3871 dt_node_type_name(lp, n2, sizeof (n2)));
3874 dt_node_type_propagate(lp, dnp); /* see K&R[A7.5] */
3875 dt_node_attr_assign(dnp, dt_attr_min(lp->dn_attr, rp->dn_attr));
3880 lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF);
3881 rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
3883 if (dt_node_is_dynamic(lp) || dt_node_is_dynamic(rp)) {
3884 xyerror(D_OP_DYN, "operator %s operands "
3885 "cannot be of dynamic type\n", opstr(op));
3888 if (dt_node_is_actfunc(lp) || dt_node_is_actfunc(rp)) {
3889 xyerror(D_OP_ACT, "operator %s operands "
3890 "cannot be actions\n", opstr(op));
3893 dt_node_type_propagate(rp, dnp); /* see K&R[A7.18] */
3894 dt_node_attr_assign(dnp, dt_attr_min(lp->dn_attr, rp->dn_attr));
3898 xyerror(D_UNKNOWN, "invalid binary op %s\n", opstr(op));
3902 * Complete the conversion of E1[E2] to *((E1)+(E2)) that we started
3903 * at the top of our switch() above (see K&R[A7.3.1]). Since E2 is
3904 * parsed as an argument_expression_list by dt_grammar.y, we can
3905 * end up with a comma-separated list inside of a non-associative
3906 * array reference. We check for this and report an appropriate error.
3908 if (dnp->dn_op == DT_TOK_LBRAC && op == DT_TOK_ADD) {
3911 if (rp->dn_list != NULL) {
3912 xyerror(D_ARR_BADREF,
3913 "cannot access %s as an associative array\n",
3914 dt_node_name(lp, n1, sizeof (n1)));
3917 dnp->dn_op = DT_TOK_ADD;
3918 pnp = dt_node_op1(DT_TOK_DEREF, dnp);
3921 * Cook callbacks are not typically permitted to allocate nodes.
3922 * When we do, we must insert them in the middle of an existing
3923 * allocation list rather than having them appended to the pcb
3924 * list because the sub-expression may be part of a definition.
3926 assert(yypcb->pcb_list == pnp);
3927 yypcb->pcb_list = pnp->dn_link;
3929 pnp->dn_link = dnp->dn_link;
3932 return (dt_node_cook(pnp, DT_IDFLG_REF));
3940 dt_cook_op3(dt_node_t *dnp, uint_t idflags)
3946 dnp->dn_expr = dt_node_cook(dnp->dn_expr, DT_IDFLG_REF);
3947 lp = dnp->dn_left = dt_node_cook(dnp->dn_left, DT_IDFLG_REF);
3948 rp = dnp->dn_right = dt_node_cook(dnp->dn_right, DT_IDFLG_REF);
3950 if (!dt_node_is_scalar(dnp->dn_expr)) {
3951 xyerror(D_OP_SCALAR,
3952 "operator ?: expression must be of scalar type\n");
3955 if (dt_node_is_dynamic(lp) || dt_node_is_dynamic(rp)) {
3957 "operator ?: operands cannot be of dynamic type\n");
3961 * The rules for type checking for the ternary operator are complex and
3962 * are described in the ANSI-C spec (see K&R[A7.16]). We implement
3963 * the various tests in order from least to most expensive.
3965 if (ctf_type_compat(lp->dn_ctfp, lp->dn_type,
3966 rp->dn_ctfp, rp->dn_type)) {
3969 } else if (dt_node_is_integer(lp) && dt_node_is_integer(rp)) {
3970 dt_type_promote(lp, rp, &ctfp, &type);
3971 } else if (dt_node_is_strcompat(lp) && dt_node_is_strcompat(rp) &&
3972 (dt_node_is_string(lp) || dt_node_is_string(rp))) {
3973 ctfp = DT_STR_CTFP(yypcb->pcb_hdl);
3974 type = DT_STR_TYPE(yypcb->pcb_hdl);
3975 } else if (dt_node_is_ptrcompat(lp, rp, &ctfp, &type) == 0) {
3976 xyerror(D_OP_INCOMPAT,
3977 "operator ?: operands must have compatible types\n");
3980 if (dt_node_is_actfunc(lp) || dt_node_is_actfunc(rp)) {
3981 xyerror(D_OP_ACT, "action cannot be "
3982 "used in a conditional context\n");
3985 dt_node_type_assign(dnp, ctfp, type);
3986 dt_node_attr_assign(dnp, dt_attr_min(dnp->dn_expr->dn_attr,
3987 dt_attr_min(lp->dn_attr, rp->dn_attr)));
3993 dt_cook_statement(dt_node_t *dnp, uint_t idflags)
3995 dnp->dn_expr = dt_node_cook(dnp->dn_expr, idflags);
3996 dt_node_attr_assign(dnp, dnp->dn_expr->dn_attr);
4002 * If dn_aggfun is set, this node is a collapsed aggregation assignment (see
4003 * the special case code for DT_TOK_ASGN in dt_cook_op2() above), in which
4004 * case we cook both the tuple and the function call. If dn_aggfun is NULL,
4005 * this node is just a reference to the aggregation's type and attributes.
4009 dt_cook_aggregation(dt_node_t *dnp, uint_t idflags)
4011 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
4013 if (dnp->dn_aggfun != NULL) {
4014 dnp->dn_aggfun = dt_node_cook(dnp->dn_aggfun, DT_IDFLG_REF);
4015 dt_node_attr_assign(dnp, dt_ident_cook(dnp,
4016 dnp->dn_ident, &dnp->dn_aggtup));
4018 dt_node_type_assign(dnp, DT_DYN_CTFP(dtp), DT_DYN_TYPE(dtp));
4019 dt_node_attr_assign(dnp, dnp->dn_ident->di_attr);
4026 * Since D permits new variable identifiers to be instantiated in any program
4027 * expression, we may need to cook a clause's predicate either before or after
4028 * the action list depending on the program code in question. Consider:
4030 * probe-description-list probe-description-list
4033 * trace(x); trace(x++);
4036 * In the left-hand example, the predicate uses operator ++ to instantiate 'x'
4037 * as a variable of type int64_t. The predicate must be cooked first because
4038 * otherwise the statement trace(x) refers to an unknown identifier. In the
4039 * right-hand example, the action list uses ++ to instantiate 'x'; the action
4040 * list must be cooked first because otherwise the predicate x == 0 refers to
4041 * an unknown identifier. In order to simplify programming, we support both.
4043 * When cooking a clause, we cook the action statements before the predicate by
4044 * default, since it seems more common to create or modify identifiers in the
4045 * action list. If cooking fails due to an unknown identifier, we attempt to
4046 * cook the predicate (i.e. do it first) and then go back and cook the actions.
4047 * If this, too, fails (or if we get an error other than D_IDENT_UNDEF) we give
4048 * up and report failure back to the user. There are five possible paths:
4050 * cook actions = OK, cook predicate = OK -> OK
4051 * cook actions = OK, cook predicate = ERR -> ERR
4052 * cook actions = ERR, cook predicate = ERR -> ERR
4053 * cook actions = ERR, cook predicate = OK, cook actions = OK -> OK
4054 * cook actions = ERR, cook predicate = OK, cook actions = ERR -> ERR
4056 * The programmer can still defeat our scheme by creating circular definition
4057 * dependencies between predicates and actions, as in this example clause:
4059 * probe-description-list
4065 * but it doesn't seem worth the complexity to handle such rare cases. The
4066 * user can simply use the D variable declaration syntax to work around them.
4069 dt_cook_clause(dt_node_t *dnp, uint_t idflags)
4071 volatile int err, tries;
4075 * Before assigning dn_ctxattr, temporarily assign the probe attribute
4076 * to 'dnp' itself to force an attribute check and minimum violation.
4078 dt_node_attr_assign(dnp, yypcb->pcb_pinfo.dtp_attr);
4079 dnp->dn_ctxattr = yypcb->pcb_pinfo.dtp_attr;
4081 bcopy(yypcb->pcb_jmpbuf, ojb, sizeof (jmp_buf));
4084 if (dnp->dn_pred != NULL && (err = setjmp(yypcb->pcb_jmpbuf)) != 0) {
4085 bcopy(ojb, yypcb->pcb_jmpbuf, sizeof (jmp_buf));
4086 if (tries++ != 0 || err != EDT_COMPILER || (
4087 yypcb->pcb_hdl->dt_errtag != dt_errtag(D_IDENT_UNDEF) &&
4088 yypcb->pcb_hdl->dt_errtag != dt_errtag(D_VAR_UNDEF)))
4089 longjmp(yypcb->pcb_jmpbuf, err);
4093 yylabel("action list");
4095 dt_node_attr_assign(dnp,
4096 dt_node_list_cook(&dnp->dn_acts, idflags));
4098 bcopy(ojb, yypcb->pcb_jmpbuf, sizeof (jmp_buf));
4102 if (dnp->dn_pred != NULL) {
4103 yylabel("predicate");
4105 dnp->dn_pred = dt_node_cook(dnp->dn_pred, idflags);
4106 dt_node_attr_assign(dnp,
4107 dt_attr_min(dnp->dn_attr, dnp->dn_pred->dn_attr));
4109 if (!dt_node_is_scalar(dnp->dn_pred)) {
4110 xyerror(D_PRED_SCALAR,
4111 "predicate result must be of scalar type\n");
4118 yylabel("action list");
4120 dt_node_attr_assign(dnp,
4121 dt_node_list_cook(&dnp->dn_acts, idflags));
4131 dt_cook_inline(dt_node_t *dnp, uint_t idflags)
4133 dt_idnode_t *inp = dnp->dn_ident->di_iarg;
4136 char n1[DT_TYPE_NAMELEN];
4137 char n2[DT_TYPE_NAMELEN];
4139 assert(dnp->dn_ident->di_flags & DT_IDFLG_INLINE);
4140 assert(inp->din_root->dn_flags & DT_NF_COOKED);
4143 * If we are inlining a translation, verify that the inline declaration
4144 * type exactly matches the type that is returned by the translation.
4145 * Otherwise just use dt_node_is_argcompat() to check the types.
4147 if ((rdp = dt_node_resolve(inp->din_root, DT_IDENT_XLSOU)) != NULL ||
4148 (rdp = dt_node_resolve(inp->din_root, DT_IDENT_XLPTR)) != NULL) {
4150 ctf_file_t *lctfp = dnp->dn_ctfp;
4151 ctf_id_t ltype = ctf_type_resolve(lctfp, dnp->dn_type);
4153 dt_xlator_t *dxp = rdp->di_data;
4154 ctf_file_t *rctfp = dxp->dx_dst_ctfp;
4155 ctf_id_t rtype = dxp->dx_dst_base;
4157 if (ctf_type_kind(lctfp, ltype) == CTF_K_POINTER) {
4158 ltype = ctf_type_reference(lctfp, ltype);
4159 ltype = ctf_type_resolve(lctfp, ltype);
4162 if (ctf_type_compat(lctfp, ltype, rctfp, rtype) == 0) {
4163 dnerror(dnp, D_OP_INCOMPAT,
4164 "inline %s definition uses incompatible types: "
4165 "\"%s\" = \"%s\"\n", dnp->dn_ident->di_name,
4166 dt_type_name(lctfp, ltype, n1, sizeof (n1)),
4167 dt_type_name(rctfp, rtype, n2, sizeof (n2)));
4170 } else if (dt_node_is_argcompat(dnp, inp->din_root) == 0) {
4171 dnerror(dnp, D_OP_INCOMPAT,
4172 "inline %s definition uses incompatible types: "
4173 "\"%s\" = \"%s\"\n", dnp->dn_ident->di_name,
4174 dt_node_type_name(dnp, n1, sizeof (n1)),
4175 dt_node_type_name(inp->din_root, n2, sizeof (n2)));
4182 dt_cook_member(dt_node_t *dnp, uint_t idflags)
4184 dnp->dn_membexpr = dt_node_cook(dnp->dn_membexpr, idflags);
4185 dt_node_attr_assign(dnp, dnp->dn_membexpr->dn_attr);
4191 dt_cook_xlator(dt_node_t *dnp, uint_t idflags)
4193 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
4194 dt_xlator_t *dxp = dnp->dn_xlator;
4197 char n1[DT_TYPE_NAMELEN];
4198 char n2[DT_TYPE_NAMELEN];
4200 dtrace_attribute_t attr = _dtrace_maxattr;
4204 * Before cooking each translator member, we push a reference to the
4205 * hash containing translator-local identifiers on to pcb_globals to
4206 * temporarily interpose these identifiers in front of other globals.
4208 dt_idstack_push(&yypcb->pcb_globals, dxp->dx_locals);
4210 for (mnp = dnp->dn_members; mnp != NULL; mnp = mnp->dn_list) {
4211 if (ctf_member_info(dxp->dx_dst_ctfp, dxp->dx_dst_type,
4212 mnp->dn_membname, &ctm) == CTF_ERR) {
4213 xyerror(D_XLATE_MEMB,
4214 "translator member %s is not a member of %s\n",
4215 mnp->dn_membname, ctf_type_name(dxp->dx_dst_ctfp,
4216 dxp->dx_dst_type, n1, sizeof (n1)));
4219 (void) dt_node_cook(mnp, DT_IDFLG_REF);
4220 dt_node_type_assign(mnp, dxp->dx_dst_ctfp, ctm.ctm_type);
4221 attr = dt_attr_min(attr, mnp->dn_attr);
4223 if (dt_node_is_argcompat(mnp, mnp->dn_membexpr) == 0) {
4224 xyerror(D_XLATE_INCOMPAT,
4225 "translator member %s definition uses "
4226 "incompatible types: \"%s\" = \"%s\"\n",
4228 dt_node_type_name(mnp, n1, sizeof (n1)),
4229 dt_node_type_name(mnp->dn_membexpr,
4234 dt_idstack_pop(&yypcb->pcb_globals, dxp->dx_locals);
4236 dxp->dx_souid.di_attr = attr;
4237 dxp->dx_ptrid.di_attr = attr;
4239 dt_node_type_assign(dnp, DT_DYN_CTFP(dtp), DT_DYN_TYPE(dtp));
4240 dt_node_attr_assign(dnp, _dtrace_defattr);
4246 dt_node_provider_cmp_argv(dt_provider_t *pvp, dt_node_t *pnp, const char *kind,
4247 uint_t old_argc, dt_node_t *old_argv, uint_t new_argc, dt_node_t *new_argv)
4249 dt_probe_t *prp = pnp->dn_ident->di_data;
4252 char n1[DT_TYPE_NAMELEN];
4253 char n2[DT_TYPE_NAMELEN];
4255 if (old_argc != new_argc) {
4256 dnerror(pnp, D_PROV_INCOMPAT,
4257 "probe %s:%s %s prototype mismatch:\n"
4258 "\t current: %u arg%s\n\tprevious: %u arg%s\n",
4259 pvp->pv_desc.dtvd_name, prp->pr_ident->di_name, kind,
4260 new_argc, new_argc != 1 ? "s" : "",
4261 old_argc, old_argc != 1 ? "s" : "");
4264 for (i = 0; i < old_argc; i++,
4265 old_argv = old_argv->dn_list, new_argv = new_argv->dn_list) {
4266 if (ctf_type_cmp(old_argv->dn_ctfp, old_argv->dn_type,
4267 new_argv->dn_ctfp, new_argv->dn_type) == 0)
4270 dnerror(pnp, D_PROV_INCOMPAT,
4271 "probe %s:%s %s prototype argument #%u mismatch:\n"
4272 "\t current: %s\n\tprevious: %s\n",
4273 pvp->pv_desc.dtvd_name, prp->pr_ident->di_name, kind, i + 1,
4274 dt_node_type_name(new_argv, n1, sizeof (n1)),
4275 dt_node_type_name(old_argv, n2, sizeof (n2)));
4280 * Compare a new probe declaration with an existing probe definition (either
4281 * from a previous declaration or cached from the kernel). If the existing
4282 * definition and declaration both have an input and output parameter list,
4283 * compare both lists. Otherwise compare only the output parameter lists.
4286 dt_node_provider_cmp(dt_provider_t *pvp, dt_node_t *pnp,
4287 dt_probe_t *old, dt_probe_t *new)
4289 dt_node_provider_cmp_argv(pvp, pnp, "output",
4290 old->pr_xargc, old->pr_xargs, new->pr_xargc, new->pr_xargs);
4292 if (old->pr_nargs != old->pr_xargs && new->pr_nargs != new->pr_xargs) {
4293 dt_node_provider_cmp_argv(pvp, pnp, "input",
4294 old->pr_nargc, old->pr_nargs, new->pr_nargc, new->pr_nargs);
4297 if (old->pr_nargs == old->pr_xargs && new->pr_nargs != new->pr_xargs) {
4298 if (pvp->pv_flags & DT_PROVIDER_IMPL) {
4299 dnerror(pnp, D_PROV_INCOMPAT,
4300 "provider interface mismatch: %s\n"
4301 "\t current: probe %s:%s has an output prototype\n"
4302 "\tprevious: probe %s:%s has no output prototype\n",
4303 pvp->pv_desc.dtvd_name, pvp->pv_desc.dtvd_name,
4304 new->pr_ident->di_name, pvp->pv_desc.dtvd_name,
4305 old->pr_ident->di_name);
4308 if (old->pr_ident->di_gen == yypcb->pcb_hdl->dt_gen)
4309 old->pr_ident->di_flags |= DT_IDFLG_ORPHAN;
4311 dt_idhash_delete(pvp->pv_probes, old->pr_ident);
4312 dt_probe_declare(pvp, new);
4317 dt_cook_probe(dt_node_t *dnp, dt_provider_t *pvp)
4319 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
4320 dt_probe_t *prp = dnp->dn_ident->di_data;
4325 char n1[DT_TYPE_NAMELEN];
4326 char n2[DT_TYPE_NAMELEN];
4328 if (prp->pr_nargs == prp->pr_xargs)
4331 for (i = 0; i < prp->pr_xargc; i++) {
4332 dt_node_t *xnp = prp->pr_xargv[i];
4333 dt_node_t *nnp = prp->pr_nargv[prp->pr_mapping[i]];
4335 if ((dxp = dt_xlator_lookup(dtp,
4336 nnp, xnp, DT_XLATE_FUZZY)) != NULL) {
4337 if (dt_provider_xref(dtp, pvp, dxp->dx_id) != 0)
4338 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
4342 if (dt_node_is_argcompat(nnp, xnp))
4343 continue; /* no translator defined and none required */
4345 dnerror(dnp, D_PROV_PRXLATOR, "translator for %s:%s output "
4346 "argument #%u from %s to %s is not defined\n",
4347 pvp->pv_desc.dtvd_name, dnp->dn_ident->di_name, i + 1,
4348 dt_node_type_name(nnp, n1, sizeof (n1)),
4349 dt_node_type_name(xnp, n2, sizeof (n2)));
4355 dt_cook_provider(dt_node_t *dnp, uint_t idflags)
4357 dt_provider_t *pvp = dnp->dn_provider;
4361 * If we're declaring a provider for the first time and it is unknown
4362 * to dtrace(7D), insert the probe definitions into the provider's hash.
4363 * If we're redeclaring a known provider, verify the interface matches.
4365 for (pnp = dnp->dn_probes; pnp != NULL; pnp = pnp->dn_list) {
4366 const char *probename = pnp->dn_ident->di_name;
4367 dt_probe_t *prp = dt_probe_lookup(pvp, probename);
4369 assert(pnp->dn_kind == DT_NODE_PROBE);
4371 if (prp != NULL && dnp->dn_provred) {
4372 dt_node_provider_cmp(pvp, pnp,
4373 prp, pnp->dn_ident->di_data);
4374 } else if (prp == NULL && dnp->dn_provred) {
4375 dnerror(pnp, D_PROV_INCOMPAT,
4376 "provider interface mismatch: %s\n"
4377 "\t current: probe %s:%s defined\n"
4378 "\tprevious: probe %s:%s not defined\n",
4379 dnp->dn_provname, dnp->dn_provname,
4380 probename, dnp->dn_provname, probename);
4381 } else if (prp != NULL) {
4382 dnerror(pnp, D_PROV_PRDUP, "probe redeclared: %s:%s\n",
4383 dnp->dn_provname, probename);
4385 dt_probe_declare(pvp, pnp->dn_ident->di_data);
4387 dt_cook_probe(pnp, pvp);
4395 dt_cook_none(dt_node_t *dnp, uint_t idflags)
4400 static dt_node_t *(*dt_cook_funcs[])(dt_node_t *, uint_t) = {
4401 dt_cook_none, /* DT_NODE_FREE */
4402 dt_cook_none, /* DT_NODE_INT */
4403 dt_cook_none, /* DT_NODE_STRING */
4404 dt_cook_ident, /* DT_NODE_IDENT */
4405 dt_cook_var, /* DT_NODE_VAR */
4406 dt_cook_none, /* DT_NODE_SYM */
4407 dt_cook_none, /* DT_NODE_TYPE */
4408 dt_cook_func, /* DT_NODE_FUNC */
4409 dt_cook_op1, /* DT_NODE_OP1 */
4410 dt_cook_op2, /* DT_NODE_OP2 */
4411 dt_cook_op3, /* DT_NODE_OP3 */
4412 dt_cook_statement, /* DT_NODE_DEXPR */
4413 dt_cook_statement, /* DT_NODE_DFUNC */
4414 dt_cook_aggregation, /* DT_NODE_AGG */
4415 dt_cook_none, /* DT_NODE_PDESC */
4416 dt_cook_clause, /* DT_NODE_CLAUSE */
4417 dt_cook_inline, /* DT_NODE_INLINE */
4418 dt_cook_member, /* DT_NODE_MEMBER */
4419 dt_cook_xlator, /* DT_NODE_XLATOR */
4420 dt_cook_none, /* DT_NODE_PROBE */
4421 dt_cook_provider, /* DT_NODE_PROVIDER */
4422 dt_cook_none /* DT_NODE_PROG */
4426 * Recursively cook the parse tree starting at the specified node. The idflags
4427 * parameter is used to indicate the type of reference (r/w) and is applied to
4428 * the resulting identifier if it is a D variable or D aggregation.
4431 dt_node_cook(dt_node_t *dnp, uint_t idflags)
4433 int oldlineno = yylineno;
4435 yylineno = dnp->dn_line;
4437 dnp = dt_cook_funcs[dnp->dn_kind](dnp, idflags);
4438 dnp->dn_flags |= DT_NF_COOKED;
4440 if (dnp->dn_kind == DT_NODE_VAR || dnp->dn_kind == DT_NODE_AGG)
4441 dnp->dn_ident->di_flags |= idflags;
4443 yylineno = oldlineno;
4448 dt_node_list_cook(dt_node_t **pnp, uint_t idflags)
4450 dtrace_attribute_t attr = _dtrace_defattr;
4451 dt_node_t *dnp, *nnp;
4453 for (dnp = (pnp != NULL ? *pnp : NULL); dnp != NULL; dnp = nnp) {
4455 dnp = *pnp = dt_node_cook(dnp, idflags);
4456 attr = dt_attr_min(attr, dnp->dn_attr);
4458 pnp = &dnp->dn_list;
4465 dt_node_list_free(dt_node_t **pnp)
4467 dt_node_t *dnp, *nnp;
4469 for (dnp = (pnp != NULL ? *pnp : NULL); dnp != NULL; dnp = nnp) {
4479 dt_node_link_free(dt_node_t **pnp)
4481 dt_node_t *dnp, *nnp;
4483 for (dnp = (pnp != NULL ? *pnp : NULL); dnp != NULL; dnp = nnp) {
4488 for (dnp = (pnp != NULL ? *pnp : NULL); dnp != NULL; dnp = nnp) {
4498 dt_node_link(dt_node_t *lp, dt_node_t *rp)
4504 else if (rp == NULL)
4507 for (dnp = lp; dnp->dn_list != NULL; dnp = dnp->dn_list)
4515 * Compute the DOF dtrace_diftype_t representation of a node's type. This is
4516 * called from a variety of places in the library so it cannot assume yypcb
4517 * is valid: any references to handle-specific data must be made through 'dtp'.
4520 dt_node_diftype(dtrace_hdl_t *dtp, const dt_node_t *dnp, dtrace_diftype_t *tp)
4522 if (dnp->dn_ctfp == DT_STR_CTFP(dtp) &&
4523 dnp->dn_type == DT_STR_TYPE(dtp)) {
4524 tp->dtdt_kind = DIF_TYPE_STRING;
4525 tp->dtdt_ckind = CTF_K_UNKNOWN;
4527 tp->dtdt_kind = DIF_TYPE_CTF;
4528 tp->dtdt_ckind = ctf_type_kind(dnp->dn_ctfp,
4529 ctf_type_resolve(dnp->dn_ctfp, dnp->dn_type));
4532 tp->dtdt_flags = (dnp->dn_flags & DT_NF_REF) ? DIF_TF_BYREF : 0;
4534 tp->dtdt_size = ctf_type_size(dnp->dn_ctfp, dnp->dn_type);
4538 dt_node_printr(dt_node_t *dnp, FILE *fp, int depth)
4540 char n[DT_TYPE_NAMELEN], buf[BUFSIZ], a[8];
4541 const dtrace_syminfo_t *dts;
4542 const dt_idnode_t *inp;
4545 (void) fprintf(fp, "%*s", depth * 2, "");
4546 (void) dt_attr_str(dnp->dn_attr, a, sizeof (a));
4548 if (dnp->dn_ctfp != NULL && dnp->dn_type != CTF_ERR &&
4549 ctf_type_name(dnp->dn_ctfp, dnp->dn_type, n, sizeof (n)) != NULL) {
4550 (void) snprintf(buf, BUFSIZ, "type=<%s> attr=%s flags=", n, a);
4552 (void) snprintf(buf, BUFSIZ, "type=<%ld> attr=%s flags=",
4556 if (dnp->dn_flags != 0) {
4558 if (dnp->dn_flags & DT_NF_SIGNED)
4559 (void) strcat(n, ",SIGN");
4560 if (dnp->dn_flags & DT_NF_COOKED)
4561 (void) strcat(n, ",COOK");
4562 if (dnp->dn_flags & DT_NF_REF)
4563 (void) strcat(n, ",REF");
4564 if (dnp->dn_flags & DT_NF_LVALUE)
4565 (void) strcat(n, ",LVAL");
4566 if (dnp->dn_flags & DT_NF_WRITABLE)
4567 (void) strcat(n, ",WRITE");
4568 if (dnp->dn_flags & DT_NF_BITFIELD)
4569 (void) strcat(n, ",BITF");
4570 if (dnp->dn_flags & DT_NF_USERLAND)
4571 (void) strcat(n, ",USER");
4572 (void) strcat(buf, n + 1);
4574 (void) strcat(buf, "0");
4576 switch (dnp->dn_kind) {
4578 (void) fprintf(fp, "FREE <node %p>\n", (void *)dnp);
4582 (void) fprintf(fp, "INT 0x%llx (%s)\n",
4583 (u_longlong_t)dnp->dn_value, buf);
4586 case DT_NODE_STRING:
4587 (void) fprintf(fp, "STRING \"%s\" (%s)\n", dnp->dn_string, buf);
4591 (void) fprintf(fp, "IDENT %s (%s)\n", dnp->dn_string, buf);
4595 (void) fprintf(fp, "VARIABLE %s%s (%s)\n",
4596 (dnp->dn_ident->di_flags & DT_IDFLG_LOCAL) ? "this->" :
4597 (dnp->dn_ident->di_flags & DT_IDFLG_TLS) ? "self->" : "",
4598 dnp->dn_ident->di_name, buf);
4600 if (dnp->dn_args != NULL)
4601 (void) fprintf(fp, "%*s[\n", depth * 2, "");
4603 for (arg = dnp->dn_args; arg != NULL; arg = arg->dn_list) {
4604 dt_node_printr(arg, fp, depth + 1);
4605 if (arg->dn_list != NULL)
4606 (void) fprintf(fp, "%*s,\n", depth * 2, "");
4609 if (dnp->dn_args != NULL)
4610 (void) fprintf(fp, "%*s]\n", depth * 2, "");
4614 dts = dnp->dn_ident->di_data;
4615 (void) fprintf(fp, "SYMBOL %s`%s (%s)\n",
4616 dts->dts_object, dts->dts_name, buf);
4620 if (dnp->dn_string != NULL) {
4621 (void) fprintf(fp, "TYPE (%s) %s\n",
4622 buf, dnp->dn_string);
4624 (void) fprintf(fp, "TYPE (%s)\n", buf);
4628 (void) fprintf(fp, "FUNC %s (%s)\n",
4629 dnp->dn_ident->di_name, buf);
4631 for (arg = dnp->dn_args; arg != NULL; arg = arg->dn_list) {
4632 dt_node_printr(arg, fp, depth + 1);
4633 if (arg->dn_list != NULL)
4634 (void) fprintf(fp, "%*s,\n", depth * 2, "");
4639 (void) fprintf(fp, "OP1 %s (%s)\n", opstr(dnp->dn_op), buf);
4640 dt_node_printr(dnp->dn_child, fp, depth + 1);
4644 (void) fprintf(fp, "OP2 %s (%s)\n", opstr(dnp->dn_op), buf);
4645 dt_node_printr(dnp->dn_left, fp, depth + 1);
4646 dt_node_printr(dnp->dn_right, fp, depth + 1);
4650 (void) fprintf(fp, "OP3 (%s)\n", buf);
4651 dt_node_printr(dnp->dn_expr, fp, depth + 1);
4652 (void) fprintf(fp, "%*s?\n", depth * 2, "");
4653 dt_node_printr(dnp->dn_left, fp, depth + 1);
4654 (void) fprintf(fp, "%*s:\n", depth * 2, "");
4655 dt_node_printr(dnp->dn_right, fp, depth + 1);
4660 (void) fprintf(fp, "D EXPRESSION attr=%s\n", a);
4661 dt_node_printr(dnp->dn_expr, fp, depth + 1);
4665 (void) fprintf(fp, "AGGREGATE @%s attr=%s [\n",
4666 dnp->dn_ident->di_name, a);
4668 for (arg = dnp->dn_aggtup; arg != NULL; arg = arg->dn_list) {
4669 dt_node_printr(arg, fp, depth + 1);
4670 if (arg->dn_list != NULL)
4671 (void) fprintf(fp, "%*s,\n", depth * 2, "");
4674 if (dnp->dn_aggfun) {
4675 (void) fprintf(fp, "%*s] = ", depth * 2, "");
4676 dt_node_printr(dnp->dn_aggfun, fp, depth + 1);
4678 (void) fprintf(fp, "%*s]\n", depth * 2, "");
4681 (void) fprintf(fp, "%*s)\n", depth * 2, "");
4685 (void) fprintf(fp, "PDESC %s:%s:%s:%s [%u]\n",
4686 dnp->dn_desc->dtpd_provider, dnp->dn_desc->dtpd_mod,
4687 dnp->dn_desc->dtpd_func, dnp->dn_desc->dtpd_name,
4688 dnp->dn_desc->dtpd_id);
4691 case DT_NODE_CLAUSE:
4692 (void) fprintf(fp, "CLAUSE attr=%s\n", a);
4694 for (arg = dnp->dn_pdescs; arg != NULL; arg = arg->dn_list)
4695 dt_node_printr(arg, fp, depth + 1);
4697 (void) fprintf(fp, "%*sCTXATTR %s\n", depth * 2, "",
4698 dt_attr_str(dnp->dn_ctxattr, a, sizeof (a)));
4700 if (dnp->dn_pred != NULL) {
4701 (void) fprintf(fp, "%*sPREDICATE /\n", depth * 2, "");
4702 dt_node_printr(dnp->dn_pred, fp, depth + 1);
4703 (void) fprintf(fp, "%*s/\n", depth * 2, "");
4706 for (arg = dnp->dn_acts; arg != NULL; arg = arg->dn_list)
4707 dt_node_printr(arg, fp, depth + 1);
4710 case DT_NODE_INLINE:
4711 inp = dnp->dn_ident->di_iarg;
4713 (void) fprintf(fp, "INLINE %s (%s)\n",
4714 dnp->dn_ident->di_name, buf);
4715 dt_node_printr(inp->din_root, fp, depth + 1);
4718 case DT_NODE_MEMBER:
4719 (void) fprintf(fp, "MEMBER %s (%s)\n", dnp->dn_membname, buf);
4720 if (dnp->dn_membexpr)
4721 dt_node_printr(dnp->dn_membexpr, fp, depth + 1);
4724 case DT_NODE_XLATOR:
4725 (void) fprintf(fp, "XLATOR (%s)", buf);
4727 if (ctf_type_name(dnp->dn_xlator->dx_src_ctfp,
4728 dnp->dn_xlator->dx_src_type, n, sizeof (n)) != NULL)
4729 (void) fprintf(fp, " from <%s>", n);
4731 if (ctf_type_name(dnp->dn_xlator->dx_dst_ctfp,
4732 dnp->dn_xlator->dx_dst_type, n, sizeof (n)) != NULL)
4733 (void) fprintf(fp, " to <%s>", n);
4735 (void) fprintf(fp, "\n");
4737 for (arg = dnp->dn_members; arg != NULL; arg = arg->dn_list)
4738 dt_node_printr(arg, fp, depth + 1);
4742 (void) fprintf(fp, "PROBE %s\n", dnp->dn_ident->di_name);
4745 case DT_NODE_PROVIDER:
4746 (void) fprintf(fp, "PROVIDER %s (%s)\n",
4747 dnp->dn_provname, dnp->dn_provred ? "redecl" : "decl");
4748 for (arg = dnp->dn_probes; arg != NULL; arg = arg->dn_list)
4749 dt_node_printr(arg, fp, depth + 1);
4753 (void) fprintf(fp, "PROGRAM attr=%s\n", a);
4754 for (arg = dnp->dn_list; arg != NULL; arg = arg->dn_list)
4755 dt_node_printr(arg, fp, depth + 1);
4759 (void) fprintf(fp, "<bad node %p, kind %d>\n",
4760 (void *)dnp, dnp->dn_kind);
4765 dt_node_root(dt_node_t *dnp)
4767 yypcb->pcb_root = dnp;
4773 dnerror(const dt_node_t *dnp, dt_errtag_t tag, const char *format, ...)
4775 int oldlineno = yylineno;
4778 yylineno = dnp->dn_line;
4780 va_start(ap, format);
4781 xyvwarn(tag, format, ap);
4784 yylineno = oldlineno;
4785 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
4790 dnwarn(const dt_node_t *dnp, dt_errtag_t tag, const char *format, ...)
4792 int oldlineno = yylineno;
4795 yylineno = dnp->dn_line;
4797 va_start(ap, format);
4798 xyvwarn(tag, format, ap);
4801 yylineno = oldlineno;
4806 xyerror(dt_errtag_t tag, const char *format, ...)
4810 va_start(ap, format);
4811 xyvwarn(tag, format, ap);
4814 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
4819 xywarn(dt_errtag_t tag, const char *format, ...)
4823 va_start(ap, format);
4824 xyvwarn(tag, format, ap);
4829 xyvwarn(dt_errtag_t tag, const char *format, va_list ap)
4832 return; /* compiler is not currently active: act as a no-op */
4834 dt_set_errmsg(yypcb->pcb_hdl, dt_errtag(tag), yypcb->pcb_region,
4835 yypcb->pcb_filetag, yypcb->pcb_fileptr ? yylineno : 0, format, ap);
4840 yyerror(const char *format, ...)
4844 va_start(ap, format);
4845 yyvwarn(format, ap);
4848 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
4853 yywarn(const char *format, ...)
4857 va_start(ap, format);
4858 yyvwarn(format, ap);
4863 yyvwarn(const char *format, va_list ap)
4866 return; /* compiler is not currently active: act as a no-op */
4868 dt_set_errmsg(yypcb->pcb_hdl, dt_errtag(D_SYNTAX), yypcb->pcb_region,
4869 yypcb->pcb_filetag, yypcb->pcb_fileptr ? yylineno : 0, format, ap);
4871 if (strchr(format, '\n') == NULL) {
4872 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
4873 size_t len = strlen(dtp->dt_errmsg);
4874 char *p, *s = dtp->dt_errmsg + len;
4875 size_t n = sizeof (dtp->dt_errmsg) - len;
4877 if (yytext[0] == '\0')
4878 (void) snprintf(s, n, " near end of input");
4879 else if (yytext[0] == '\n')
4880 (void) snprintf(s, n, " near end of line");
4882 if ((p = strchr(yytext, '\n')) != NULL)
4883 *p = '\0'; /* crop at newline */
4884 (void) snprintf(s, n, " near \"%s\"", yytext);
4890 yylabel(const char *label)
4892 dt_dprintf("set label to <%s>\n", label ? label : "NULL");
4893 yypcb->pcb_region = label;
4899 return (1); /* indicate that lex should return a zero token for EOF */