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 * Use is subject to license terms.
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)
728 if (dnp->dn_kind == DT_NODE_STRING)
729 return (strlen(dnp->dn_string) + 1);
731 if (dt_node_is_dynamic(dnp) && dnp->dn_ident != NULL)
732 return (dt_ident_size(dnp->dn_ident));
734 return (ctf_type_size(dnp->dn_ctfp, dnp->dn_type));
738 * Determine if the specified parse tree node references an identifier of the
739 * specified kind, and if so return a pointer to it; otherwise return NULL.
740 * This function resolves the identifier itself, following through any inlines.
743 dt_node_resolve(const dt_node_t *dnp, uint_t idkind)
747 switch (dnp->dn_kind) {
754 idp = dt_ident_resolve(dnp->dn_ident);
755 return (idp->di_kind == idkind ? idp : NULL);
758 if (dt_node_is_dynamic(dnp)) {
759 idp = dt_ident_resolve(dnp->dn_ident);
760 return (idp->di_kind == idkind ? idp : NULL);
767 dt_node_sizeof(const dt_node_t *dnp)
769 dtrace_syminfo_t *sip;
771 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
774 * The size of the node as used for the sizeof() operator depends on
775 * the kind of the node. If the node is a SYM, the size is obtained
776 * from the symbol table; if it is not a SYM, the size is determined
777 * from the node's type. This is slightly different from C's sizeof()
778 * operator in that (for example) when applied to a function, sizeof()
779 * will evaluate to the length of the function rather than the size of
782 if (dnp->dn_kind != DT_NODE_SYM)
783 return (dt_node_type_size(dnp));
785 sip = dnp->dn_ident->di_data;
787 if (dtrace_lookup_by_name(dtp, sip->dts_object,
788 sip->dts_name, &sym, NULL) == -1)
791 return (sym.st_size);
795 dt_node_is_integer(const dt_node_t *dnp)
797 ctf_file_t *fp = dnp->dn_ctfp;
802 assert(dnp->dn_flags & DT_NF_COOKED);
804 type = ctf_type_resolve(fp, dnp->dn_type);
805 kind = ctf_type_kind(fp, type);
807 if (kind == CTF_K_INTEGER &&
808 ctf_type_encoding(fp, type, &e) == 0 && IS_VOID(e))
809 return (0); /* void integer */
811 return (kind == CTF_K_INTEGER || kind == CTF_K_ENUM);
815 dt_node_is_float(const dt_node_t *dnp)
817 ctf_file_t *fp = dnp->dn_ctfp;
822 assert(dnp->dn_flags & DT_NF_COOKED);
824 type = ctf_type_resolve(fp, dnp->dn_type);
825 kind = ctf_type_kind(fp, type);
827 return (kind == CTF_K_FLOAT &&
828 ctf_type_encoding(dnp->dn_ctfp, type, &e) == 0 && (
829 e.cte_format == CTF_FP_SINGLE || e.cte_format == CTF_FP_DOUBLE ||
830 e.cte_format == CTF_FP_LDOUBLE));
834 dt_node_is_scalar(const dt_node_t *dnp)
836 ctf_file_t *fp = dnp->dn_ctfp;
841 assert(dnp->dn_flags & DT_NF_COOKED);
843 type = ctf_type_resolve(fp, dnp->dn_type);
844 kind = ctf_type_kind(fp, type);
846 if (kind == CTF_K_INTEGER &&
847 ctf_type_encoding(fp, type, &e) == 0 && IS_VOID(e))
848 return (0); /* void cannot be used as a scalar */
850 return (kind == CTF_K_INTEGER || kind == CTF_K_ENUM ||
851 kind == CTF_K_POINTER);
855 dt_node_is_arith(const dt_node_t *dnp)
857 ctf_file_t *fp = dnp->dn_ctfp;
862 assert(dnp->dn_flags & DT_NF_COOKED);
864 type = ctf_type_resolve(fp, dnp->dn_type);
865 kind = ctf_type_kind(fp, type);
867 if (kind == CTF_K_INTEGER)
868 return (ctf_type_encoding(fp, type, &e) == 0 && !IS_VOID(e));
870 return (kind == CTF_K_ENUM);
874 dt_node_is_vfptr(const dt_node_t *dnp)
876 ctf_file_t *fp = dnp->dn_ctfp;
881 assert(dnp->dn_flags & DT_NF_COOKED);
883 type = ctf_type_resolve(fp, dnp->dn_type);
884 if (ctf_type_kind(fp, type) != CTF_K_POINTER)
885 return (0); /* type is not a pointer */
887 type = ctf_type_resolve(fp, ctf_type_reference(fp, type));
888 kind = ctf_type_kind(fp, type);
890 return (kind == CTF_K_FUNCTION || (kind == CTF_K_INTEGER &&
891 ctf_type_encoding(fp, type, &e) == 0 && IS_VOID(e)));
895 dt_node_is_dynamic(const dt_node_t *dnp)
897 if (dnp->dn_kind == DT_NODE_VAR &&
898 (dnp->dn_ident->di_flags & DT_IDFLG_INLINE)) {
899 const dt_idnode_t *inp = dnp->dn_ident->di_iarg;
900 return (inp->din_root ? dt_node_is_dynamic(inp->din_root) : 0);
903 return (dnp->dn_ctfp == DT_DYN_CTFP(yypcb->pcb_hdl) &&
904 dnp->dn_type == DT_DYN_TYPE(yypcb->pcb_hdl));
908 dt_node_is_string(const dt_node_t *dnp)
910 return (dnp->dn_ctfp == DT_STR_CTFP(yypcb->pcb_hdl) &&
911 dnp->dn_type == DT_STR_TYPE(yypcb->pcb_hdl));
915 dt_node_is_stack(const dt_node_t *dnp)
917 return (dnp->dn_ctfp == DT_STACK_CTFP(yypcb->pcb_hdl) &&
918 dnp->dn_type == DT_STACK_TYPE(yypcb->pcb_hdl));
922 dt_node_is_symaddr(const dt_node_t *dnp)
924 return (dnp->dn_ctfp == DT_SYMADDR_CTFP(yypcb->pcb_hdl) &&
925 dnp->dn_type == DT_SYMADDR_TYPE(yypcb->pcb_hdl));
929 dt_node_is_usymaddr(const dt_node_t *dnp)
931 return (dnp->dn_ctfp == DT_USYMADDR_CTFP(yypcb->pcb_hdl) &&
932 dnp->dn_type == DT_USYMADDR_TYPE(yypcb->pcb_hdl));
936 dt_node_is_strcompat(const dt_node_t *dnp)
938 ctf_file_t *fp = dnp->dn_ctfp;
944 assert(dnp->dn_flags & DT_NF_COOKED);
946 base = ctf_type_resolve(fp, dnp->dn_type);
947 kind = ctf_type_kind(fp, base);
949 if (kind == CTF_K_POINTER &&
950 (base = ctf_type_reference(fp, base)) != CTF_ERR &&
951 (base = ctf_type_resolve(fp, base)) != CTF_ERR &&
952 ctf_type_encoding(fp, base, &e) == 0 && IS_CHAR(e))
953 return (1); /* promote char pointer to string */
955 if (kind == CTF_K_ARRAY && ctf_array_info(fp, base, &r) == 0 &&
956 (base = ctf_type_resolve(fp, r.ctr_contents)) != CTF_ERR &&
957 ctf_type_encoding(fp, base, &e) == 0 && IS_CHAR(e))
958 return (1); /* promote char array to string */
964 dt_node_is_pointer(const dt_node_t *dnp)
966 ctf_file_t *fp = dnp->dn_ctfp;
969 assert(dnp->dn_flags & DT_NF_COOKED);
971 if (dt_node_is_string(dnp))
972 return (0); /* string are pass-by-ref but act like structs */
974 kind = ctf_type_kind(fp, ctf_type_resolve(fp, dnp->dn_type));
975 return (kind == CTF_K_POINTER || kind == CTF_K_ARRAY);
979 dt_node_is_void(const dt_node_t *dnp)
981 ctf_file_t *fp = dnp->dn_ctfp;
985 if (dt_node_is_dynamic(dnp))
986 return (0); /* <DYN> is an alias for void but not the same */
988 if (dt_node_is_stack(dnp))
991 if (dt_node_is_symaddr(dnp) || dt_node_is_usymaddr(dnp))
994 type = ctf_type_resolve(fp, dnp->dn_type);
996 return (ctf_type_kind(fp, type) == CTF_K_INTEGER &&
997 ctf_type_encoding(fp, type, &e) == 0 && IS_VOID(e));
1001 dt_node_is_ptrcompat(const dt_node_t *lp, const dt_node_t *rp,
1002 ctf_file_t **fpp, ctf_id_t *tp)
1004 ctf_file_t *lfp = lp->dn_ctfp;
1005 ctf_file_t *rfp = rp->dn_ctfp;
1007 ctf_id_t lbase = CTF_ERR, rbase = CTF_ERR;
1008 ctf_id_t lref = CTF_ERR, rref = CTF_ERR;
1010 int lp_is_void, rp_is_void, lp_is_int, rp_is_int, compat;
1011 uint_t lkind, rkind;
1015 assert(lp->dn_flags & DT_NF_COOKED);
1016 assert(rp->dn_flags & DT_NF_COOKED);
1018 if (dt_node_is_dynamic(lp) || dt_node_is_dynamic(rp))
1019 return (0); /* fail if either node is a dynamic variable */
1021 lp_is_int = dt_node_is_integer(lp);
1022 rp_is_int = dt_node_is_integer(rp);
1024 if (lp_is_int && rp_is_int)
1025 return (0); /* fail if both nodes are integers */
1027 if (lp_is_int && (lp->dn_kind != DT_NODE_INT || lp->dn_value != 0))
1028 return (0); /* fail if lp is an integer that isn't 0 constant */
1030 if (rp_is_int && (rp->dn_kind != DT_NODE_INT || rp->dn_value != 0))
1031 return (0); /* fail if rp is an integer that isn't 0 constant */
1033 if ((lp_is_int == 0 && rp_is_int == 0) && (
1034 (lp->dn_flags & DT_NF_USERLAND) ^ (rp->dn_flags & DT_NF_USERLAND)))
1035 return (0); /* fail if only one pointer is a userland address */
1038 * Resolve the left-hand and right-hand types to their base type, and
1039 * then resolve the referenced type as well (assuming the base type
1040 * is CTF_K_POINTER or CTF_K_ARRAY). Otherwise [lr]ref = CTF_ERR.
1043 lbase = ctf_type_resolve(lfp, lp->dn_type);
1044 lkind = ctf_type_kind(lfp, lbase);
1046 if (lkind == CTF_K_POINTER) {
1047 lref = ctf_type_resolve(lfp,
1048 ctf_type_reference(lfp, lbase));
1049 } else if (lkind == CTF_K_ARRAY &&
1050 ctf_array_info(lfp, lbase, &r) == 0) {
1051 lref = ctf_type_resolve(lfp, r.ctr_contents);
1056 rbase = ctf_type_resolve(rfp, rp->dn_type);
1057 rkind = ctf_type_kind(rfp, rbase);
1059 if (rkind == CTF_K_POINTER) {
1060 rref = ctf_type_resolve(rfp,
1061 ctf_type_reference(rfp, rbase));
1062 } else if (rkind == CTF_K_ARRAY &&
1063 ctf_array_info(rfp, rbase, &r) == 0) {
1064 rref = ctf_type_resolve(rfp, r.ctr_contents);
1069 * We know that one or the other type may still be a zero-valued
1070 * integer constant. To simplify the code below, set the integer
1071 * type variables equal to the non-integer types and proceed.
1078 } else if (rp_is_int) {
1085 lp_is_void = ctf_type_encoding(lfp, lref, &e) == 0 && IS_VOID(e);
1086 rp_is_void = ctf_type_encoding(rfp, rref, &e) == 0 && IS_VOID(e);
1089 * The types are compatible if both are pointers to the same type, or
1090 * if either pointer is a void pointer. If they are compatible, set
1091 * tp to point to the more specific pointer type and return it.
1093 compat = (lkind == CTF_K_POINTER || lkind == CTF_K_ARRAY) &&
1094 (rkind == CTF_K_POINTER || rkind == CTF_K_ARRAY) &&
1095 (lp_is_void || rp_is_void || ctf_type_compat(lfp, lref, rfp, rref));
1099 *fpp = rp_is_void ? lfp : rfp;
1101 *tp = rp_is_void ? lbase : rbase;
1108 * The rules for checking argument types against parameter types are described
1109 * in the ANSI-C spec (see K&R[A7.3.2] and K&R[A7.17]). We use the same rule
1110 * set to determine whether associative array arguments match the prototype.
1113 dt_node_is_argcompat(const dt_node_t *lp, const dt_node_t *rp)
1115 ctf_file_t *lfp = lp->dn_ctfp;
1116 ctf_file_t *rfp = rp->dn_ctfp;
1118 assert(lp->dn_flags & DT_NF_COOKED);
1119 assert(rp->dn_flags & DT_NF_COOKED);
1121 if (dt_node_is_integer(lp) && dt_node_is_integer(rp))
1122 return (1); /* integer types are compatible */
1124 if (dt_node_is_strcompat(lp) && dt_node_is_strcompat(rp))
1125 return (1); /* string types are compatible */
1127 if (dt_node_is_stack(lp) && dt_node_is_stack(rp))
1128 return (1); /* stack types are compatible */
1130 if (dt_node_is_symaddr(lp) && dt_node_is_symaddr(rp))
1131 return (1); /* symaddr types are compatible */
1133 if (dt_node_is_usymaddr(lp) && dt_node_is_usymaddr(rp))
1134 return (1); /* usymaddr types are compatible */
1136 switch (ctf_type_kind(lfp, ctf_type_resolve(lfp, lp->dn_type))) {
1137 case CTF_K_FUNCTION:
1140 return (ctf_type_compat(lfp, lp->dn_type, rfp, rp->dn_type));
1142 return (dt_node_is_ptrcompat(lp, rp, NULL, NULL));
1147 * We provide dt_node_is_posconst() as a convenience routine for callers who
1148 * wish to verify that an argument is a positive non-zero integer constant.
1151 dt_node_is_posconst(const dt_node_t *dnp)
1153 return (dnp->dn_kind == DT_NODE_INT && dnp->dn_value != 0 && (
1154 (dnp->dn_flags & DT_NF_SIGNED) == 0 || (int64_t)dnp->dn_value > 0));
1158 dt_node_is_actfunc(const dt_node_t *dnp)
1160 return (dnp->dn_kind == DT_NODE_FUNC &&
1161 dnp->dn_ident->di_kind == DT_IDENT_ACTFUNC);
1165 * The original rules for integer constant typing are described in K&R[A2.5.1].
1166 * However, since we support long long, we instead use the rules from ISO C99
1167 * clause 6.4.4.1 since that is where long longs are formally described. The
1168 * rules require us to know whether the constant was specified in decimal or
1169 * in octal or hex, which we do by looking at our lexer's 'yyintdecimal' flag.
1170 * The type of an integer constant is the first of the corresponding list in
1171 * which its value can be represented:
1173 * unsuffixed decimal: int, long, long long
1174 * unsuffixed oct/hex: int, unsigned int, long, unsigned long,
1175 * long long, unsigned long long
1176 * suffix [uU]: unsigned int, unsigned long, unsigned long long
1177 * suffix [lL] decimal: long, long long
1178 * suffix [lL] oct/hex: long, unsigned long, long long, unsigned long long
1179 * suffix [uU][Ll]: unsigned long, unsigned long long
1180 * suffix ll/LL decimal: long long
1181 * suffix ll/LL oct/hex: long long, unsigned long long
1182 * suffix [uU][ll/LL]: unsigned long long
1184 * Given that our lexer has already validated the suffixes by regexp matching,
1185 * there is an obvious way to concisely encode these rules: construct an array
1186 * of the types in the order int, unsigned int, long, unsigned long, long long,
1187 * unsigned long long. Compute an integer array starting index based on the
1188 * suffix (e.g. none = 0, u = 1, ull = 5), and compute an increment based on
1189 * the specifier (dec/oct/hex) and suffix (u). Then iterate from the starting
1190 * index to the end, advancing using the increment, and searching until we
1191 * find a limit that matches or we run out of choices (overflow). To make it
1192 * even faster, we precompute the table of type information in dtrace_open().
1195 dt_node_int(uintmax_t value)
1197 dt_node_t *dnp = dt_node_alloc(DT_NODE_INT);
1198 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
1200 int n = (yyintdecimal | (yyintsuffix[0] == 'u')) + 1;
1206 dnp->dn_op = DT_TOK_INT;
1207 dnp->dn_value = value;
1209 for (p = yyintsuffix; (c = *p) != '\0'; p++) {
1210 if (c == 'U' || c == 'u')
1212 else if (c == 'L' || c == 'l')
1216 for (; i < sizeof (dtp->dt_ints) / sizeof (dtp->dt_ints[0]); i += n) {
1217 if (value <= dtp->dt_ints[i].did_limit) {
1218 dt_node_type_assign(dnp,
1219 dtp->dt_ints[i].did_ctfp,
1220 dtp->dt_ints[i].did_type);
1223 * If a prefix character is present in macro text, add
1224 * in the corresponding operator node (see dt_lex.l).
1226 switch (yyintprefix) {
1228 return (dt_node_op1(DT_TOK_IPOS, dnp));
1230 return (dt_node_op1(DT_TOK_INEG, dnp));
1237 xyerror(D_INT_OFLOW, "integer constant 0x%llx cannot be represented "
1238 "in any built-in integral type\n", (u_longlong_t)value);
1240 return (NULL); /* keep gcc happy */
1244 dt_node_string(char *string)
1246 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
1250 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
1252 dnp = dt_node_alloc(DT_NODE_STRING);
1253 dnp->dn_op = DT_TOK_STRING;
1254 dnp->dn_string = string;
1255 dt_node_type_assign(dnp, DT_STR_CTFP(dtp), DT_STR_TYPE(dtp));
1261 dt_node_ident(char *name)
1267 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
1270 * If the identifier is an inlined integer constant, then create an INT
1271 * node that is a clone of the inline parse tree node and return that
1272 * immediately, allowing this inline to be used in parsing contexts
1273 * that require constant expressions (e.g. scalar array sizes).
1275 if ((idp = dt_idstack_lookup(&yypcb->pcb_globals, name)) != NULL &&
1276 (idp->di_flags & DT_IDFLG_INLINE)) {
1277 dt_idnode_t *inp = idp->di_iarg;
1279 if (inp->din_root != NULL &&
1280 inp->din_root->dn_kind == DT_NODE_INT) {
1283 dnp = dt_node_alloc(DT_NODE_INT);
1284 dnp->dn_op = DT_TOK_INT;
1285 dnp->dn_value = inp->din_root->dn_value;
1286 dt_node_type_propagate(inp->din_root, dnp);
1292 dnp = dt_node_alloc(DT_NODE_IDENT);
1293 dnp->dn_op = name[0] == '@' ? DT_TOK_AGG : DT_TOK_IDENT;
1294 dnp->dn_string = name;
1300 * Create an empty node of type corresponding to the given declaration.
1301 * Explicit references to user types (C or D) are assigned the default
1302 * stability; references to other types are _dtrace_typattr (Private).
1305 dt_node_type(dt_decl_t *ddp)
1307 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
1308 dtrace_typeinfo_t dtt;
1314 * If 'ddp' is NULL, we get a decl by popping the decl stack. This
1315 * form of dt_node_type() is used by parameter rules in dt_grammar.y.
1318 ddp = dt_decl_pop_param(&name);
1320 err = dt_decl_type(ddp, &dtt);
1325 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
1328 dnp = dt_node_alloc(DT_NODE_TYPE);
1329 dnp->dn_op = DT_TOK_IDENT;
1330 dnp->dn_string = name;
1331 dt_node_type_assign(dnp, dtt.dtt_ctfp, dtt.dtt_type);
1333 if (dtt.dtt_ctfp == dtp->dt_cdefs->dm_ctfp ||
1334 dtt.dtt_ctfp == dtp->dt_ddefs->dm_ctfp)
1335 dt_node_attr_assign(dnp, _dtrace_defattr);
1337 dt_node_attr_assign(dnp, _dtrace_typattr);
1343 * Create a type node corresponding to a varargs (...) parameter by just
1344 * assigning it type CTF_ERR. The decl processing code will handle this.
1347 dt_node_vatype(void)
1349 dt_node_t *dnp = dt_node_alloc(DT_NODE_TYPE);
1351 dnp->dn_op = DT_TOK_IDENT;
1352 dnp->dn_ctfp = yypcb->pcb_hdl->dt_cdefs->dm_ctfp;
1353 dnp->dn_type = CTF_ERR;
1354 dnp->dn_attr = _dtrace_defattr;
1360 * Instantiate a decl using the contents of the current declaration stack. As
1361 * we do not currently permit decls to be initialized, this function currently
1362 * returns NULL and no parse node is created. When this function is called,
1363 * the topmost scope's ds_ident pointer will be set to NULL (indicating no
1364 * init_declarator rule was matched) or will point to the identifier to use.
1369 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
1370 dt_scope_t *dsp = &yypcb->pcb_dstack;
1371 dt_dclass_t class = dsp->ds_class;
1372 dt_decl_t *ddp = dt_decl_top();
1375 dtrace_typeinfo_t dtt;
1378 char n1[DT_TYPE_NAMELEN];
1379 char n2[DT_TYPE_NAMELEN];
1381 if (dt_decl_type(ddp, &dtt) != 0)
1382 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
1385 * If we have no declaration identifier, then this is either a spurious
1386 * declaration of an intrinsic type (e.g. "extern int;") or declaration
1387 * or redeclaration of a struct, union, or enum type or tag.
1389 if (dsp->ds_ident == NULL) {
1390 if (ddp->dd_kind != CTF_K_STRUCT &&
1391 ddp->dd_kind != CTF_K_UNION && ddp->dd_kind != CTF_K_ENUM)
1392 xyerror(D_DECL_USELESS, "useless declaration\n");
1394 dt_dprintf("type %s added as id %ld\n", dt_type_name(
1395 ddp->dd_ctfp, ddp->dd_type, n1, sizeof (n1)), ddp->dd_type);
1400 if (strchr(dsp->ds_ident, '`') != NULL) {
1401 xyerror(D_DECL_SCOPE, "D scoping operator may not be used in "
1402 "a declaration name (%s)\n", dsp->ds_ident);
1406 * If we are nested inside of a C include file, add the declaration to
1407 * the C definition module; otherwise use the D definition module.
1409 if (yypcb->pcb_idepth != 0)
1410 dmp = dtp->dt_cdefs;
1412 dmp = dtp->dt_ddefs;
1415 * If we see a global or static declaration of a function prototype,
1416 * treat this as equivalent to a D extern declaration.
1418 if (ctf_type_kind(dtt.dtt_ctfp, dtt.dtt_type) == CTF_K_FUNCTION &&
1419 (class == DT_DC_DEFAULT || class == DT_DC_STATIC))
1420 class = DT_DC_EXTERN;
1424 case DT_DC_REGISTER:
1426 xyerror(D_DECL_BADCLASS, "specified storage class not "
1427 "appropriate in D\n");
1430 case DT_DC_EXTERN: {
1431 dtrace_typeinfo_t ott;
1432 dtrace_syminfo_t dts;
1435 int exists = dtrace_lookup_by_name(dtp,
1436 dmp->dm_name, dsp->ds_ident, &sym, &dts) == 0;
1438 if (exists && (dtrace_symbol_type(dtp, &sym, &dts, &ott) != 0 ||
1439 ctf_type_cmp(dtt.dtt_ctfp, dtt.dtt_type,
1440 ott.dtt_ctfp, ott.dtt_type) != 0)) {
1441 xyerror(D_DECL_IDRED, "identifier redeclared: %s`%s\n"
1442 "\t current: %s\n\tprevious: %s\n",
1443 dmp->dm_name, dsp->ds_ident,
1444 dt_type_name(dtt.dtt_ctfp, dtt.dtt_type,
1446 dt_type_name(ott.dtt_ctfp, ott.dtt_type,
1448 } else if (!exists && dt_module_extern(dtp, dmp,
1449 dsp->ds_ident, &dtt) == NULL) {
1451 "failed to extern %s: %s\n", dsp->ds_ident,
1452 dtrace_errmsg(dtp, dtrace_errno(dtp)));
1454 dt_dprintf("extern %s`%s type=<%s>\n",
1455 dmp->dm_name, dsp->ds_ident,
1456 dt_type_name(dtt.dtt_ctfp, dtt.dtt_type,
1463 if (dt_idstack_lookup(&yypcb->pcb_globals, dsp->ds_ident)) {
1464 xyerror(D_DECL_IDRED, "global variable identifier "
1465 "redeclared: %s\n", dsp->ds_ident);
1468 if (ctf_lookup_by_name(dmp->dm_ctfp,
1469 dsp->ds_ident) != CTF_ERR) {
1470 xyerror(D_DECL_IDRED,
1471 "typedef redeclared: %s\n", dsp->ds_ident);
1475 * If the source type for the typedef is not defined in the
1476 * target container or its parent, copy the type to the target
1477 * container and reset dtt_ctfp and dtt_type to the copy.
1479 if (dtt.dtt_ctfp != dmp->dm_ctfp &&
1480 dtt.dtt_ctfp != ctf_parent_file(dmp->dm_ctfp)) {
1482 dtt.dtt_type = ctf_add_type(dmp->dm_ctfp,
1483 dtt.dtt_ctfp, dtt.dtt_type);
1484 dtt.dtt_ctfp = dmp->dm_ctfp;
1486 if (dtt.dtt_type == CTF_ERR ||
1487 ctf_update(dtt.dtt_ctfp) == CTF_ERR) {
1488 xyerror(D_UNKNOWN, "failed to copy typedef %s "
1489 "source type: %s\n", dsp->ds_ident,
1490 ctf_errmsg(ctf_errno(dtt.dtt_ctfp)));
1494 type = ctf_add_typedef(dmp->dm_ctfp,
1495 CTF_ADD_ROOT, dsp->ds_ident, dtt.dtt_type);
1497 if (type == CTF_ERR || ctf_update(dmp->dm_ctfp) == CTF_ERR) {
1498 xyerror(D_UNKNOWN, "failed to typedef %s: %s\n",
1499 dsp->ds_ident, ctf_errmsg(ctf_errno(dmp->dm_ctfp)));
1502 dt_dprintf("typedef %s added as id %ld\n", dsp->ds_ident, type);
1516 dhp = yypcb->pcb_locals;
1517 idflags = DT_IDFLG_LOCAL;
1518 idp = dt_idhash_lookup(dhp, dsp->ds_ident);
1522 idflags = DT_IDFLG_TLS;
1523 idp = dt_idhash_lookup(dhp, dsp->ds_ident);
1526 dhp = dtp->dt_globals;
1528 idp = dt_idstack_lookup(
1529 &yypcb->pcb_globals, dsp->ds_ident);
1533 if (ddp->dd_kind == CTF_K_ARRAY && ddp->dd_node == NULL) {
1534 xyerror(D_DECL_ARRNULL,
1535 "array declaration requires array dimension or "
1536 "tuple signature: %s\n", dsp->ds_ident);
1539 if (idp != NULL && idp->di_gen == 0) {
1540 xyerror(D_DECL_IDRED, "built-in identifier "
1541 "redeclared: %s\n", idp->di_name);
1544 if (dtrace_lookup_by_type(dtp, DTRACE_OBJ_CDEFS,
1545 dsp->ds_ident, NULL) == 0 ||
1546 dtrace_lookup_by_type(dtp, DTRACE_OBJ_DDEFS,
1547 dsp->ds_ident, NULL) == 0) {
1548 xyerror(D_DECL_IDRED, "typedef identifier "
1549 "redeclared: %s\n", dsp->ds_ident);
1553 * Cache some attributes of the decl to make the rest of this
1554 * code simpler: if the decl is an array which is subscripted
1555 * by a type rather than an integer, then it's an associative
1556 * array (assc). We then expect to match either DT_IDENT_ARRAY
1557 * for associative arrays or DT_IDENT_SCALAR for anything else.
1559 assc = ddp->dd_kind == CTF_K_ARRAY &&
1560 ddp->dd_node->dn_kind == DT_NODE_TYPE;
1562 idkind = assc ? DT_IDENT_ARRAY : DT_IDENT_SCALAR;
1565 * Create a fake dt_node_t on the stack so we can determine the
1566 * type of any matching identifier by assigning to this node.
1567 * If the pre-existing ident has its di_type set, propagate
1568 * the type by hand so as not to trigger a prototype check for
1569 * arrays (yet); otherwise we use dt_ident_cook() on the ident
1570 * to ensure it is fully initialized before looking at it.
1572 bzero(&idn, sizeof (dt_node_t));
1574 if (idp != NULL && idp->di_type != CTF_ERR)
1575 dt_node_type_assign(&idn, idp->di_ctfp, idp->di_type);
1576 else if (idp != NULL)
1577 (void) dt_ident_cook(&idn, idp, NULL);
1580 if (class == DT_DC_THIS) {
1581 xyerror(D_DECL_LOCASSC, "associative arrays "
1582 "may not be declared as local variables:"
1583 " %s\n", dsp->ds_ident);
1586 if (dt_decl_type(ddp->dd_next, &dtt) != 0)
1587 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
1590 if (idp != NULL && (idp->di_kind != idkind ||
1591 ctf_type_cmp(dtt.dtt_ctfp, dtt.dtt_type,
1592 idn.dn_ctfp, idn.dn_type) != 0)) {
1593 xyerror(D_DECL_IDRED, "identifier redeclared: %s\n"
1594 "\t current: %s %s\n\tprevious: %s %s\n",
1595 dsp->ds_ident, dt_idkind_name(idkind),
1596 dt_type_name(dtt.dtt_ctfp,
1597 dtt.dtt_type, n1, sizeof (n1)),
1598 dt_idkind_name(idp->di_kind),
1599 dt_node_type_name(&idn, n2, sizeof (n2)));
1601 } else if (idp != NULL && assc) {
1602 const dt_idsig_t *isp = idp->di_data;
1603 dt_node_t *dnp = ddp->dd_node;
1606 for (; dnp != NULL; dnp = dnp->dn_list, argc++) {
1607 const dt_node_t *pnp = &isp->dis_args[argc];
1609 if (argc >= isp->dis_argc)
1610 continue; /* tuple length mismatch */
1612 if (ctf_type_cmp(dnp->dn_ctfp, dnp->dn_type,
1613 pnp->dn_ctfp, pnp->dn_type) == 0)
1616 xyerror(D_DECL_IDRED,
1617 "identifier redeclared: %s\n"
1618 "\t current: %s, key #%d of type %s\n"
1619 "\tprevious: %s, key #%d of type %s\n",
1621 dt_idkind_name(idkind), argc + 1,
1622 dt_node_type_name(dnp, n1, sizeof (n1)),
1623 dt_idkind_name(idp->di_kind), argc + 1,
1624 dt_node_type_name(pnp, n2, sizeof (n2)));
1627 if (isp->dis_argc != argc) {
1628 xyerror(D_DECL_IDRED,
1629 "identifier redeclared: %s\n"
1630 "\t current: %s of %s, tuple length %d\n"
1631 "\tprevious: %s of %s, tuple length %d\n",
1632 dsp->ds_ident, dt_idkind_name(idkind),
1633 dt_type_name(dtt.dtt_ctfp, dtt.dtt_type,
1634 n1, sizeof (n1)), argc,
1635 dt_idkind_name(idp->di_kind),
1636 dt_node_type_name(&idn, n2, sizeof (n2)),
1640 } else if (idp == NULL) {
1641 type = ctf_type_resolve(dtt.dtt_ctfp, dtt.dtt_type);
1642 kind = ctf_type_kind(dtt.dtt_ctfp, type);
1646 if (ctf_type_encoding(dtt.dtt_ctfp, type,
1647 &cte) == 0 && IS_VOID(cte)) {
1648 xyerror(D_DECL_VOIDOBJ, "cannot have "
1649 "void object: %s\n", dsp->ds_ident);
1654 if (ctf_type_size(dtt.dtt_ctfp, type) != 0)
1655 break; /* proceed to declaring */
1658 xyerror(D_DECL_INCOMPLETE,
1659 "incomplete struct/union/enum %s: %s\n",
1660 dt_type_name(dtt.dtt_ctfp, dtt.dtt_type,
1661 n1, sizeof (n1)), dsp->ds_ident);
1665 if (dt_idhash_nextid(dhp, &id) == -1) {
1666 xyerror(D_ID_OFLOW, "cannot create %s: limit "
1667 "on number of %s variables exceeded\n",
1668 dsp->ds_ident, dt_idhash_name(dhp));
1671 dt_dprintf("declare %s %s variable %s, id=%u\n",
1672 dt_idhash_name(dhp), dt_idkind_name(idkind),
1675 idp = dt_idhash_insert(dhp, dsp->ds_ident, idkind,
1676 idflags | DT_IDFLG_WRITE | DT_IDFLG_DECL, id,
1677 _dtrace_defattr, 0, assc ? &dt_idops_assc :
1678 &dt_idops_thaw, NULL, dtp->dt_gen);
1681 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
1683 dt_ident_type_assign(idp, dtt.dtt_ctfp, dtt.dtt_type);
1686 * If we are declaring an associative array, use our
1687 * fake parse node to cook the new assoc identifier.
1688 * This will force the ident code to instantiate the
1689 * array type signature corresponding to the list of
1690 * types pointed to by ddp->dd_node. We also reset
1691 * the identifier's attributes based upon the result.
1695 dt_ident_cook(&idn, idp, &ddp->dd_node);
1700 } /* end of switch */
1702 free(dsp->ds_ident);
1703 dsp->ds_ident = NULL;
1709 dt_node_func(dt_node_t *dnp, dt_node_t *args)
1713 if (dnp->dn_kind != DT_NODE_IDENT) {
1714 xyerror(D_FUNC_IDENT,
1715 "function designator is not of function type\n");
1718 idp = dt_idstack_lookup(&yypcb->pcb_globals, dnp->dn_string);
1721 xyerror(D_FUNC_UNDEF,
1722 "undefined function name: %s\n", dnp->dn_string);
1725 if (idp->di_kind != DT_IDENT_FUNC &&
1726 idp->di_kind != DT_IDENT_AGGFUNC &&
1727 idp->di_kind != DT_IDENT_ACTFUNC) {
1728 xyerror(D_FUNC_IDKIND, "%s '%s' may not be referenced as a "
1729 "function\n", dt_idkind_name(idp->di_kind), idp->di_name);
1732 free(dnp->dn_string);
1733 dnp->dn_string = NULL;
1735 dnp->dn_kind = DT_NODE_FUNC;
1736 dnp->dn_flags &= ~DT_NF_COOKED;
1737 dnp->dn_ident = idp;
1738 dnp->dn_args = args;
1739 dnp->dn_list = NULL;
1745 * The offsetof() function is special because it takes a type name as an
1746 * argument. It does not actually construct its own node; after looking up the
1747 * structure or union offset, we just return an integer node with the offset.
1750 dt_node_offsetof(dt_decl_t *ddp, char *s)
1752 dtrace_typeinfo_t dtt;
1761 name = alloca(strlen(s) + 1);
1762 (void) strcpy(name, s);
1765 err = dt_decl_type(ddp, &dtt);
1769 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
1771 type = ctf_type_resolve(dtt.dtt_ctfp, dtt.dtt_type);
1772 kind = ctf_type_kind(dtt.dtt_ctfp, type);
1774 if (kind != CTF_K_STRUCT && kind != CTF_K_UNION) {
1775 xyerror(D_OFFSETOF_TYPE,
1776 "offsetof operand must be a struct or union type\n");
1779 if (ctf_member_info(dtt.dtt_ctfp, type, name, &ctm) == CTF_ERR) {
1780 xyerror(D_UNKNOWN, "failed to determine offset of %s: %s\n",
1781 name, ctf_errmsg(ctf_errno(dtt.dtt_ctfp)));
1784 bzero(&dn, sizeof (dn));
1785 dt_node_type_assign(&dn, dtt.dtt_ctfp, ctm.ctm_type);
1787 if (dn.dn_flags & DT_NF_BITFIELD) {
1788 xyerror(D_OFFSETOF_BITFIELD,
1789 "cannot take offset of a bit-field: %s\n", name);
1792 return (dt_node_int(ctm.ctm_offset / NBBY));
1796 dt_node_op1(int op, dt_node_t *cp)
1800 if (cp->dn_kind == DT_NODE_INT) {
1804 * If we're negating an unsigned integer, zero out any
1805 * extra top bits to truncate the value to the size of
1806 * the effective type determined by dt_node_int().
1808 cp->dn_value = -cp->dn_value;
1809 if (!(cp->dn_flags & DT_NF_SIGNED)) {
1810 cp->dn_value &= ~0ULL >>
1811 (64 - dt_node_type_size(cp) * NBBY);
1817 cp->dn_value = ~cp->dn_value;
1820 cp->dn_value = !cp->dn_value;
1826 * If sizeof is applied to a type_name or string constant, we can
1827 * transform 'cp' into an integer constant in the node construction
1828 * pass so that it can then be used for arithmetic in this pass.
1830 if (op == DT_TOK_SIZEOF &&
1831 (cp->dn_kind == DT_NODE_STRING || cp->dn_kind == DT_NODE_TYPE)) {
1832 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
1833 size_t size = dt_node_type_size(cp);
1836 xyerror(D_SIZEOF_TYPE, "cannot apply sizeof to an "
1837 "operand of unknown size\n");
1840 dt_node_type_assign(cp, dtp->dt_ddefs->dm_ctfp,
1841 ctf_lookup_by_name(dtp->dt_ddefs->dm_ctfp, "size_t"));
1843 cp->dn_kind = DT_NODE_INT;
1844 cp->dn_op = DT_TOK_INT;
1845 cp->dn_value = size;
1850 dnp = dt_node_alloc(DT_NODE_OP1);
1851 assert(op <= USHRT_MAX);
1852 dnp->dn_op = (ushort_t)op;
1859 dt_node_op2(int op, dt_node_t *lp, dt_node_t *rp)
1861 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
1865 * First we check for operations that are illegal -- namely those that
1866 * might result in integer division by zero, and abort if one is found.
1868 if (rp->dn_kind == DT_NODE_INT && rp->dn_value == 0 &&
1869 (op == DT_TOK_MOD || op == DT_TOK_DIV ||
1870 op == DT_TOK_MOD_EQ || op == DT_TOK_DIV_EQ))
1871 xyerror(D_DIV_ZERO, "expression contains division by zero\n");
1874 * If both children are immediate values, we can just perform inline
1875 * calculation and return a new immediate node with the result.
1877 if (lp->dn_kind == DT_NODE_INT && rp->dn_kind == DT_NODE_INT) {
1878 uintmax_t l = lp->dn_value;
1879 uintmax_t r = rp->dn_value;
1881 dnp = dt_node_int(0); /* allocate new integer node for result */
1885 dnp->dn_value = l || r;
1886 dt_node_type_assign(dnp,
1887 DT_INT_CTFP(dtp), DT_INT_TYPE(dtp));
1890 dnp->dn_value = (l != 0) ^ (r != 0);
1891 dt_node_type_assign(dnp,
1892 DT_INT_CTFP(dtp), DT_INT_TYPE(dtp));
1895 dnp->dn_value = l && r;
1896 dt_node_type_assign(dnp,
1897 DT_INT_CTFP(dtp), DT_INT_TYPE(dtp));
1900 dnp->dn_value = l | r;
1901 dt_node_promote(lp, rp, dnp);
1904 dnp->dn_value = l ^ r;
1905 dt_node_promote(lp, rp, dnp);
1908 dnp->dn_value = l & r;
1909 dt_node_promote(lp, rp, dnp);
1912 dnp->dn_value = l == r;
1913 dt_node_type_assign(dnp,
1914 DT_INT_CTFP(dtp), DT_INT_TYPE(dtp));
1917 dnp->dn_value = l != r;
1918 dt_node_type_assign(dnp,
1919 DT_INT_CTFP(dtp), DT_INT_TYPE(dtp));
1922 dt_node_promote(lp, rp, dnp);
1923 if (dnp->dn_flags & DT_NF_SIGNED)
1924 dnp->dn_value = (intmax_t)l < (intmax_t)r;
1926 dnp->dn_value = l < r;
1927 dt_node_type_assign(dnp,
1928 DT_INT_CTFP(dtp), DT_INT_TYPE(dtp));
1931 dt_node_promote(lp, rp, dnp);
1932 if (dnp->dn_flags & DT_NF_SIGNED)
1933 dnp->dn_value = (intmax_t)l <= (intmax_t)r;
1935 dnp->dn_value = l <= r;
1936 dt_node_type_assign(dnp,
1937 DT_INT_CTFP(dtp), DT_INT_TYPE(dtp));
1940 dt_node_promote(lp, rp, dnp);
1941 if (dnp->dn_flags & DT_NF_SIGNED)
1942 dnp->dn_value = (intmax_t)l > (intmax_t)r;
1944 dnp->dn_value = l > r;
1945 dt_node_type_assign(dnp,
1946 DT_INT_CTFP(dtp), DT_INT_TYPE(dtp));
1949 dt_node_promote(lp, rp, dnp);
1950 if (dnp->dn_flags & DT_NF_SIGNED)
1951 dnp->dn_value = (intmax_t)l >= (intmax_t)r;
1953 dnp->dn_value = l >= r;
1954 dt_node_type_assign(dnp,
1955 DT_INT_CTFP(dtp), DT_INT_TYPE(dtp));
1958 dnp->dn_value = l << r;
1959 dt_node_type_propagate(lp, dnp);
1960 dt_node_attr_assign(rp,
1961 dt_attr_min(lp->dn_attr, rp->dn_attr));
1964 dnp->dn_value = l >> r;
1965 dt_node_type_propagate(lp, dnp);
1966 dt_node_attr_assign(rp,
1967 dt_attr_min(lp->dn_attr, rp->dn_attr));
1970 dnp->dn_value = l + r;
1971 dt_node_promote(lp, rp, dnp);
1974 dnp->dn_value = l - r;
1975 dt_node_promote(lp, rp, dnp);
1978 dnp->dn_value = l * r;
1979 dt_node_promote(lp, rp, dnp);
1982 dt_node_promote(lp, rp, dnp);
1983 if (dnp->dn_flags & DT_NF_SIGNED)
1984 dnp->dn_value = (intmax_t)l / (intmax_t)r;
1986 dnp->dn_value = l / r;
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;
2008 * If an integer constant is being cast to another integer type, we can
2009 * perform the cast as part of integer constant folding in this pass.
2010 * We must take action when the integer is being cast to a smaller type
2011 * or if it is changing signed-ness. If so, we first shift rp's bits
2012 * bits high (losing excess bits if narrowing) and then shift them down
2013 * with either a logical shift (unsigned) or arithmetic shift (signed).
2015 if (op == DT_TOK_LPAR && rp->dn_kind == DT_NODE_INT &&
2016 dt_node_is_integer(lp)) {
2017 size_t srcsize = dt_node_type_size(rp);
2018 size_t dstsize = dt_node_type_size(lp);
2020 if ((dstsize < srcsize) || ((lp->dn_flags & DT_NF_SIGNED) ^
2021 (rp->dn_flags & DT_NF_SIGNED))) {
2022 int n = dstsize < srcsize ?
2023 (sizeof (uint64_t) * NBBY - dstsize * NBBY) :
2024 (sizeof (uint64_t) * NBBY - srcsize * NBBY);
2027 if (lp->dn_flags & DT_NF_SIGNED)
2028 rp->dn_value = (intmax_t)rp->dn_value >> n;
2030 rp->dn_value = rp->dn_value >> n;
2033 dt_node_type_propagate(lp, rp);
2034 dt_node_attr_assign(rp, dt_attr_min(lp->dn_attr, rp->dn_attr));
2041 * If no immediate optimizations are available, create an new OP2 node
2042 * and glue the left and right children into place and return.
2044 dnp = dt_node_alloc(DT_NODE_OP2);
2045 assert(op <= USHRT_MAX);
2046 dnp->dn_op = (ushort_t)op;
2054 dt_node_op3(dt_node_t *expr, dt_node_t *lp, dt_node_t *rp)
2058 if (expr->dn_kind == DT_NODE_INT)
2059 return (expr->dn_value != 0 ? lp : rp);
2061 dnp = dt_node_alloc(DT_NODE_OP3);
2062 dnp->dn_op = DT_TOK_QUESTION;
2063 dnp->dn_expr = expr;
2071 dt_node_statement(dt_node_t *expr)
2075 if (expr->dn_kind == DT_NODE_AGG)
2078 if (expr->dn_kind == DT_NODE_FUNC &&
2079 expr->dn_ident->di_kind == DT_IDENT_ACTFUNC)
2080 dnp = dt_node_alloc(DT_NODE_DFUNC);
2082 dnp = dt_node_alloc(DT_NODE_DEXPR);
2084 dnp->dn_expr = expr;
2089 dt_node_pdesc_by_name(char *spec)
2091 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
2095 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2097 dnp = dt_node_alloc(DT_NODE_PDESC);
2098 dnp->dn_spec = spec;
2099 dnp->dn_desc = malloc(sizeof (dtrace_probedesc_t));
2101 if (dnp->dn_desc == NULL)
2102 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2104 if (dtrace_xstr2desc(dtp, yypcb->pcb_pspec, dnp->dn_spec,
2105 yypcb->pcb_sargc, yypcb->pcb_sargv, dnp->dn_desc) != 0) {
2106 xyerror(D_PDESC_INVAL, "invalid probe description \"%s\": %s\n",
2107 dnp->dn_spec, dtrace_errmsg(dtp, dtrace_errno(dtp)));
2111 dnp->dn_spec = NULL;
2117 dt_node_pdesc_by_id(uintmax_t id)
2119 static const char *const names[] = {
2120 "providers", "modules", "functions"
2123 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
2124 dt_node_t *dnp = dt_node_alloc(DT_NODE_PDESC);
2126 if ((dnp->dn_desc = malloc(sizeof (dtrace_probedesc_t))) == NULL)
2127 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2129 if (id > UINT_MAX) {
2130 xyerror(D_PDESC_INVAL, "identifier %llu exceeds maximum "
2131 "probe id\n", (u_longlong_t)id);
2134 if (yypcb->pcb_pspec != DTRACE_PROBESPEC_NAME) {
2135 xyerror(D_PDESC_INVAL, "probe identifier %llu not permitted "
2136 "when specifying %s\n", (u_longlong_t)id,
2137 names[yypcb->pcb_pspec]);
2140 if (dtrace_id2desc(dtp, (dtrace_id_t)id, dnp->dn_desc) != 0) {
2141 xyerror(D_PDESC_INVAL, "invalid probe identifier %llu: %s\n",
2142 (u_longlong_t)id, dtrace_errmsg(dtp, dtrace_errno(dtp)));
2149 dt_node_clause(dt_node_t *pdescs, dt_node_t *pred, dt_node_t *acts)
2151 dt_node_t *dnp = dt_node_alloc(DT_NODE_CLAUSE);
2153 dnp->dn_pdescs = pdescs;
2154 dnp->dn_pred = pred;
2155 dnp->dn_acts = acts;
2157 yybegin(YYS_CLAUSE);
2162 dt_node_inline(dt_node_t *expr)
2164 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
2165 dt_scope_t *dsp = &yypcb->pcb_dstack;
2166 dt_decl_t *ddp = dt_decl_top();
2168 char n[DT_TYPE_NAMELEN];
2169 dtrace_typeinfo_t dtt;
2171 dt_ident_t *idp, *rdp;
2175 if (dt_decl_type(ddp, &dtt) != 0)
2176 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
2178 if (dsp->ds_class != DT_DC_DEFAULT) {
2179 xyerror(D_DECL_BADCLASS, "specified storage class not "
2180 "appropriate for inline declaration\n");
2183 if (dsp->ds_ident == NULL)
2184 xyerror(D_DECL_USELESS, "inline declaration requires a name\n");
2186 if ((idp = dt_idstack_lookup(
2187 &yypcb->pcb_globals, dsp->ds_ident)) != NULL) {
2188 xyerror(D_DECL_IDRED, "identifier redefined: %s\n\t current: "
2189 "inline definition\n\tprevious: %s %s\n",
2190 idp->di_name, dt_idkind_name(idp->di_kind),
2191 (idp->di_flags & DT_IDFLG_INLINE) ? "inline" : "");
2195 * If we are declaring an inlined array, verify that we have a tuple
2196 * signature, and then recompute 'dtt' as the array's value type.
2198 if (ddp->dd_kind == CTF_K_ARRAY) {
2199 if (ddp->dd_node == NULL) {
2200 xyerror(D_DECL_ARRNULL, "inline declaration requires "
2201 "array tuple signature: %s\n", dsp->ds_ident);
2204 if (ddp->dd_node->dn_kind != DT_NODE_TYPE) {
2205 xyerror(D_DECL_ARRNULL, "inline declaration cannot be "
2206 "of scalar array type: %s\n", dsp->ds_ident);
2209 if (dt_decl_type(ddp->dd_next, &dtt) != 0)
2210 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
2214 * If the inline identifier is not defined, then create it with the
2215 * orphan flag set. We do not insert the identifier into dt_globals
2216 * until we have successfully cooked the right-hand expression, below.
2218 dnp = dt_node_alloc(DT_NODE_INLINE);
2219 dt_node_type_assign(dnp, dtt.dtt_ctfp, dtt.dtt_type);
2220 dt_node_attr_assign(dnp, _dtrace_defattr);
2222 if (dt_node_is_void(dnp)) {
2223 xyerror(D_DECL_VOIDOBJ,
2224 "cannot declare void inline: %s\n", dsp->ds_ident);
2227 if (ctf_type_kind(dnp->dn_ctfp, ctf_type_resolve(
2228 dnp->dn_ctfp, dnp->dn_type)) == CTF_K_FORWARD) {
2229 xyerror(D_DECL_INCOMPLETE,
2230 "incomplete struct/union/enum %s: %s\n",
2231 dt_node_type_name(dnp, n, sizeof (n)), dsp->ds_ident);
2234 if ((inp = malloc(sizeof (dt_idnode_t))) == NULL)
2235 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2237 bzero(inp, sizeof (dt_idnode_t));
2239 idp = dnp->dn_ident = dt_ident_create(dsp->ds_ident,
2240 ddp->dd_kind == CTF_K_ARRAY ? DT_IDENT_ARRAY : DT_IDENT_SCALAR,
2241 DT_IDFLG_INLINE | DT_IDFLG_REF | DT_IDFLG_DECL | DT_IDFLG_ORPHAN, 0,
2242 _dtrace_defattr, 0, &dt_idops_inline, inp, dtp->dt_gen);
2246 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2250 * If we're inlining an associative array, create a private identifier
2251 * hash containing the named parameters and store it in inp->din_hash.
2252 * We then push this hash on to the top of the pcb_globals stack.
2254 if (ddp->dd_kind == CTF_K_ARRAY) {
2260 for (pnp = ddp->dd_node; pnp != NULL; pnp = pnp->dn_list)
2261 i++; /* count up parameters for din_argv[] */
2263 inp->din_hash = dt_idhash_create("inline args", NULL, 0, 0);
2264 inp->din_argv = calloc(i, sizeof (dt_ident_t *));
2266 if (inp->din_hash == NULL || inp->din_argv == NULL)
2267 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2270 * Create an identifier for each parameter as a scalar inline,
2271 * and store it in din_hash and in position in din_argv[]. The
2272 * parameter identifiers also use dt_idops_inline, but we leave
2273 * the dt_idnode_t argument 'pinp' zeroed. This will be filled
2274 * in by the code generation pass with references to the args.
2276 for (i = 0, pnp = ddp->dd_node;
2277 pnp != NULL; pnp = pnp->dn_list, i++) {
2279 if (pnp->dn_string == NULL)
2280 continue; /* ignore anonymous parameters */
2282 if ((pinp = malloc(sizeof (dt_idnode_t))) == NULL)
2283 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2285 pidp = dt_idhash_insert(inp->din_hash, pnp->dn_string,
2286 DT_IDENT_SCALAR, DT_IDFLG_DECL | DT_IDFLG_INLINE, 0,
2287 _dtrace_defattr, 0, &dt_idops_inline,
2292 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2295 inp->din_argv[i] = pidp;
2296 bzero(pinp, sizeof (dt_idnode_t));
2297 dt_ident_type_assign(pidp, pnp->dn_ctfp, pnp->dn_type);
2300 dt_idstack_push(&yypcb->pcb_globals, inp->din_hash);
2304 * Unlike most constructors, we need to explicitly cook the right-hand
2305 * side of the inline definition immediately to prevent recursion. If
2306 * the right-hand side uses the inline itself, the cook will fail.
2308 expr = dt_node_cook(expr, DT_IDFLG_REF);
2310 if (ddp->dd_kind == CTF_K_ARRAY)
2311 dt_idstack_pop(&yypcb->pcb_globals, inp->din_hash);
2314 * Set the type, attributes, and flags for the inline. If the right-
2315 * hand expression has an identifier, propagate its flags. Then cook
2316 * the identifier to fully initialize it: if we're declaring an inline
2317 * associative array this will construct a type signature from 'ddp'.
2319 if (dt_node_is_dynamic(expr))
2320 rdp = dt_ident_resolve(expr->dn_ident);
2321 else if (expr->dn_kind == DT_NODE_VAR || expr->dn_kind == DT_NODE_SYM)
2322 rdp = expr->dn_ident;
2327 idp->di_flags |= (rdp->di_flags &
2328 (DT_IDFLG_WRITE | DT_IDFLG_USER | DT_IDFLG_PRIM));
2331 idp->di_attr = dt_attr_min(_dtrace_defattr, expr->dn_attr);
2332 dt_ident_type_assign(idp, dtt.dtt_ctfp, dtt.dtt_type);
2333 (void) dt_ident_cook(dnp, idp, &ddp->dd_node);
2336 * Store the parse tree nodes for 'expr' inside of idp->di_data ('inp')
2337 * so that they will be preserved with this identifier. Then pop the
2338 * inline declaration from the declaration stack and restore the lexer.
2340 inp->din_list = yypcb->pcb_list;
2341 inp->din_root = expr;
2343 dt_decl_free(dt_decl_pop());
2344 yybegin(YYS_CLAUSE);
2347 * Finally, insert the inline identifier into dt_globals to make it
2348 * visible, and then cook 'dnp' to check its type against 'expr'.
2350 dt_idhash_xinsert(dtp->dt_globals, idp);
2351 return (dt_node_cook(dnp, DT_IDFLG_REF));
2355 dt_node_member(dt_decl_t *ddp, char *name, dt_node_t *expr)
2357 dtrace_typeinfo_t dtt;
2362 err = dt_decl_type(ddp, &dtt);
2366 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
2369 dnp = dt_node_alloc(DT_NODE_MEMBER);
2370 dnp->dn_membname = name;
2371 dnp->dn_membexpr = expr;
2374 dt_node_type_assign(dnp, dtt.dtt_ctfp, dtt.dtt_type);
2380 dt_node_xlator(dt_decl_t *ddp, dt_decl_t *sdp, char *name, dt_node_t *members)
2382 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
2383 dtrace_typeinfo_t src, dst;
2390 char n1[DT_TYPE_NAMELEN];
2391 char n2[DT_TYPE_NAMELEN];
2393 edst = dt_decl_type(ddp, &dst);
2396 esrc = dt_decl_type(sdp, &src);
2399 if (edst != 0 || esrc != 0) {
2401 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
2404 bzero(&sn, sizeof (sn));
2405 dt_node_type_assign(&sn, src.dtt_ctfp, src.dtt_type);
2407 bzero(&dn, sizeof (dn));
2408 dt_node_type_assign(&dn, dst.dtt_ctfp, dst.dtt_type);
2410 if (dt_xlator_lookup(dtp, &sn, &dn, DT_XLATE_EXACT) != NULL) {
2411 xyerror(D_XLATE_REDECL,
2412 "translator from %s to %s has already been declared\n",
2413 dt_node_type_name(&sn, n1, sizeof (n1)),
2414 dt_node_type_name(&dn, n2, sizeof (n2)));
2417 kind = ctf_type_kind(dst.dtt_ctfp,
2418 ctf_type_resolve(dst.dtt_ctfp, dst.dtt_type));
2420 if (kind == CTF_K_FORWARD) {
2421 xyerror(D_XLATE_SOU, "incomplete struct/union/enum %s\n",
2422 dt_type_name(dst.dtt_ctfp, dst.dtt_type, n1, sizeof (n1)));
2425 if (kind != CTF_K_STRUCT && kind != CTF_K_UNION) {
2426 xyerror(D_XLATE_SOU,
2427 "translator output type must be a struct or union\n");
2430 dxp = dt_xlator_create(dtp, &src, &dst, name, members, yypcb->pcb_list);
2431 yybegin(YYS_CLAUSE);
2435 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2437 dnp = dt_node_alloc(DT_NODE_XLATOR);
2438 dnp->dn_xlator = dxp;
2439 dnp->dn_members = members;
2441 return (dt_node_cook(dnp, DT_IDFLG_REF));
2445 dt_node_probe(char *s, int protoc, dt_node_t *nargs, dt_node_t *xargs)
2447 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
2451 size_t len = strlen(s) + 3; /* +3 for :: and \0 */
2452 char *name = alloca(len);
2454 (void) snprintf(name, len, "::%s", s);
2455 (void) strhyphenate(name);
2458 if (strchr(name, '`') != NULL) {
2459 xyerror(D_PROV_BADNAME, "probe name may not "
2460 "contain scoping operator: %s\n", name);
2463 if (strlen(name) - 2 >= DTRACE_NAMELEN) {
2464 xyerror(D_PROV_BADNAME, "probe name may not exceed %d "
2465 "characters: %s\n", DTRACE_NAMELEN - 1, name);
2468 dnp = dt_node_alloc(DT_NODE_PROBE);
2470 dnp->dn_ident = dt_ident_create(name, DT_IDENT_PROBE,
2471 DT_IDFLG_ORPHAN, DTRACE_IDNONE, _dtrace_defattr, 0,
2472 &dt_idops_probe, NULL, dtp->dt_gen);
2474 nargc = dt_decl_prototype(nargs, nargs,
2475 "probe input", DT_DP_VOID | DT_DP_ANON);
2477 xargc = dt_decl_prototype(xargs, nargs,
2478 "probe output", DT_DP_VOID);
2480 if (nargc > UINT8_MAX) {
2481 xyerror(D_PROV_PRARGLEN, "probe %s input prototype exceeds %u "
2482 "parameters: %d params used\n", name, UINT8_MAX, nargc);
2485 if (xargc > UINT8_MAX) {
2486 xyerror(D_PROV_PRARGLEN, "probe %s output prototype exceeds %u "
2487 "parameters: %d params used\n", name, UINT8_MAX, xargc);
2490 if (dnp->dn_ident == NULL || dt_probe_create(dtp,
2491 dnp->dn_ident, protoc, nargs, nargc, xargs, xargc) == NULL)
2492 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2498 dt_node_provider(char *name, dt_node_t *probes)
2500 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
2501 dt_node_t *dnp = dt_node_alloc(DT_NODE_PROVIDER);
2505 dnp->dn_provname = name;
2506 dnp->dn_probes = probes;
2508 if (strchr(name, '`') != NULL) {
2509 dnerror(dnp, D_PROV_BADNAME, "provider name may not "
2510 "contain scoping operator: %s\n", name);
2513 if ((len = strlen(name)) >= DTRACE_PROVNAMELEN) {
2514 dnerror(dnp, D_PROV_BADNAME, "provider name may not exceed %d "
2515 "characters: %s\n", DTRACE_PROVNAMELEN - 1, name);
2518 if (isdigit(name[len - 1])) {
2519 dnerror(dnp, D_PROV_BADNAME, "provider name may not "
2520 "end with a digit: %s\n", name);
2524 * Check to see if the provider is already defined or visible through
2525 * dtrace(7D). If so, set dn_provred to treat it as a re-declaration.
2526 * If not, create a new provider and set its interface-only flag. This
2527 * flag may be cleared later by calls made to dt_probe_declare().
2529 if ((dnp->dn_provider = dt_provider_lookup(dtp, name)) != NULL)
2530 dnp->dn_provred = B_TRUE;
2531 else if ((dnp->dn_provider = dt_provider_create(dtp, name)) == NULL)
2532 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2534 dnp->dn_provider->pv_flags |= DT_PROVIDER_INTF;
2537 * Store all parse nodes created since we consumed the DT_KEY_PROVIDER
2538 * token with the provider and then restore our lexing state to CLAUSE.
2539 * Note that if dnp->dn_provred is true, we may end up storing dups of
2540 * a provider's interface and implementation: we eat this space because
2541 * the implementation will likely need to redeclare probe members, and
2542 * therefore may result in those member nodes becoming persistent.
2544 for (lnp = yypcb->pcb_list; lnp->dn_link != NULL; lnp = lnp->dn_link)
2545 continue; /* skip to end of allocation list */
2547 lnp->dn_link = dnp->dn_provider->pv_nodes;
2548 dnp->dn_provider->pv_nodes = yypcb->pcb_list;
2550 yybegin(YYS_CLAUSE);
2555 dt_node_program(dt_node_t *lnp)
2557 dt_node_t *dnp = dt_node_alloc(DT_NODE_PROG);
2563 * This function provides the underlying implementation of cooking an
2564 * identifier given its node, a hash of dynamic identifiers, an identifier
2565 * kind, and a boolean flag indicating whether we are allowed to instantiate
2566 * a new identifier if the string is not found. This function is either
2567 * called from dt_cook_ident(), below, or directly by the various cooking
2568 * routines that are allowed to instantiate identifiers (e.g. op2 TOK_ASGN).
2571 dt_xcook_ident(dt_node_t *dnp, dt_idhash_t *dhp, uint_t idkind, int create)
2573 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
2574 const char *sname = dt_idhash_name(dhp);
2577 dtrace_attribute_t attr = _dtrace_defattr;
2579 dtrace_syminfo_t dts;
2582 const char *scope, *mark;
2587 * Look for scoping marks in the identifier. If one is found, set our
2588 * scope to either DTRACE_OBJ_KMODS or UMODS or to the first part of
2589 * the string that specifies the scope using an explicit module name.
2590 * If two marks in a row are found, set 'uref' (user symbol reference).
2591 * Otherwise we set scope to DTRACE_OBJ_EXEC, indicating that normal
2592 * scope is desired and we should search the specified idhash.
2594 if ((name = strrchr(dnp->dn_string, '`')) != NULL) {
2595 if (name > dnp->dn_string && name[-1] == '`') {
2600 if (name == dnp->dn_string + uref)
2601 scope = uref ? DTRACE_OBJ_UMODS : DTRACE_OBJ_KMODS;
2603 scope = dnp->dn_string;
2605 *name++ = '\0'; /* leave name pointing after scoping mark */
2606 dnkind = DT_NODE_VAR;
2608 } else if (idkind == DT_IDENT_AGG) {
2609 scope = DTRACE_OBJ_EXEC;
2610 name = dnp->dn_string + 1;
2611 dnkind = DT_NODE_AGG;
2613 scope = DTRACE_OBJ_EXEC;
2614 name = dnp->dn_string;
2615 dnkind = DT_NODE_VAR;
2619 * If create is set to false, and we fail our idhash lookup, preset
2620 * the errno code to EDT_NOVAR for our final error message below.
2621 * If we end up calling dtrace_lookup_by_name(), it will reset the
2622 * errno appropriately and that error will be reported instead.
2624 (void) dt_set_errno(dtp, EDT_NOVAR);
2625 mark = uref ? "``" : "`";
2627 if (scope == DTRACE_OBJ_EXEC && (
2628 (dhp != dtp->dt_globals &&
2629 (idp = dt_idhash_lookup(dhp, name)) != NULL) ||
2630 (dhp == dtp->dt_globals &&
2631 (idp = dt_idstack_lookup(&yypcb->pcb_globals, name)) != NULL))) {
2633 * Check that we are referencing the ident in the manner that
2634 * matches its type if this is a global lookup. In the TLS or
2635 * local case, we don't know how the ident will be used until
2636 * the time operator -> is seen; more parsing is needed.
2638 if (idp->di_kind != idkind && dhp == dtp->dt_globals) {
2639 xyerror(D_IDENT_BADREF, "%s '%s' may not be referenced "
2640 "as %s\n", dt_idkind_name(idp->di_kind),
2641 idp->di_name, dt_idkind_name(idkind));
2645 * Arrays and aggregations are not cooked individually. They
2646 * have dynamic types and must be referenced using operator [].
2647 * This is handled explicitly by the code for DT_TOK_LBRAC.
2649 if (idp->di_kind != DT_IDENT_ARRAY &&
2650 idp->di_kind != DT_IDENT_AGG)
2651 attr = dt_ident_cook(dnp, idp, NULL);
2653 dt_node_type_assign(dnp,
2654 DT_DYN_CTFP(dtp), DT_DYN_TYPE(dtp));
2655 attr = idp->di_attr;
2658 free(dnp->dn_string);
2659 dnp->dn_string = NULL;
2660 dnp->dn_kind = dnkind;
2661 dnp->dn_ident = idp;
2662 dnp->dn_flags |= DT_NF_LVALUE;
2664 if (idp->di_flags & DT_IDFLG_WRITE)
2665 dnp->dn_flags |= DT_NF_WRITABLE;
2667 dt_node_attr_assign(dnp, attr);
2669 } else if (dhp == dtp->dt_globals && scope != DTRACE_OBJ_EXEC &&
2670 dtrace_lookup_by_name(dtp, scope, name, &sym, &dts) == 0) {
2672 dt_module_t *mp = dt_module_lookup_by_name(dtp, dts.dts_object);
2673 int umod = (mp->dm_flags & DT_DM_KERNEL) == 0;
2674 static const char *const kunames[] = { "kernel", "user" };
2676 dtrace_typeinfo_t dtt;
2677 dtrace_syminfo_t *sip;
2680 xyerror(D_SYM_BADREF, "%s module '%s' symbol '%s' may "
2681 "not be referenced as a %s symbol\n", kunames[umod],
2682 dts.dts_object, dts.dts_name, kunames[uref]);
2685 if (dtrace_symbol_type(dtp, &sym, &dts, &dtt) != 0) {
2687 * For now, we special-case EDT_DATAMODEL to clarify
2688 * that mixed data models are not currently supported.
2690 if (dtp->dt_errno == EDT_DATAMODEL) {
2691 xyerror(D_SYM_MODEL, "cannot use %s symbol "
2692 "%s%s%s in a %s D program\n",
2693 dt_module_modelname(mp),
2694 dts.dts_object, mark, dts.dts_name,
2695 dt_module_modelname(dtp->dt_ddefs));
2698 xyerror(D_SYM_NOTYPES,
2699 "no symbolic type information is available for "
2700 "%s%s%s: %s\n", dts.dts_object, mark, dts.dts_name,
2701 dtrace_errmsg(dtp, dtrace_errno(dtp)));
2704 idp = dt_ident_create(name, DT_IDENT_SYMBOL, 0, 0,
2705 _dtrace_symattr, 0, &dt_idops_thaw, NULL, dtp->dt_gen);
2708 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2710 if (mp->dm_flags & DT_DM_PRIMARY)
2711 idp->di_flags |= DT_IDFLG_PRIM;
2713 idp->di_next = dtp->dt_externs;
2714 dtp->dt_externs = idp;
2716 if ((sip = malloc(sizeof (dtrace_syminfo_t))) == NULL)
2717 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2719 bcopy(&dts, sip, sizeof (dtrace_syminfo_t));
2721 idp->di_ctfp = dtt.dtt_ctfp;
2722 idp->di_type = dtt.dtt_type;
2724 free(dnp->dn_string);
2725 dnp->dn_string = NULL;
2726 dnp->dn_kind = DT_NODE_SYM;
2727 dnp->dn_ident = idp;
2728 dnp->dn_flags |= DT_NF_LVALUE;
2730 dt_node_type_assign(dnp, dtt.dtt_ctfp, dtt.dtt_type);
2731 dt_node_attr_assign(dnp, _dtrace_symattr);
2734 idp->di_flags |= DT_IDFLG_USER;
2735 dnp->dn_flags |= DT_NF_USERLAND;
2738 } else if (scope == DTRACE_OBJ_EXEC && create == B_TRUE) {
2739 uint_t flags = DT_IDFLG_WRITE;
2742 if (dt_idhash_nextid(dhp, &id) == -1) {
2743 xyerror(D_ID_OFLOW, "cannot create %s: limit on number "
2744 "of %s variables exceeded\n", name, sname);
2747 if (dhp == yypcb->pcb_locals)
2748 flags |= DT_IDFLG_LOCAL;
2749 else if (dhp == dtp->dt_tls)
2750 flags |= DT_IDFLG_TLS;
2752 dt_dprintf("create %s %s variable %s, id=%u\n",
2753 sname, dt_idkind_name(idkind), name, id);
2755 if (idkind == DT_IDENT_ARRAY || idkind == DT_IDENT_AGG) {
2756 idp = dt_idhash_insert(dhp, name,
2757 idkind, flags, id, _dtrace_defattr, 0,
2758 &dt_idops_assc, NULL, dtp->dt_gen);
2760 idp = dt_idhash_insert(dhp, name,
2761 idkind, flags, id, _dtrace_defattr, 0,
2762 &dt_idops_thaw, NULL, dtp->dt_gen);
2766 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2769 * Arrays and aggregations are not cooked individually. They
2770 * have dynamic types and must be referenced using operator [].
2771 * This is handled explicitly by the code for DT_TOK_LBRAC.
2773 if (idp->di_kind != DT_IDENT_ARRAY &&
2774 idp->di_kind != DT_IDENT_AGG)
2775 attr = dt_ident_cook(dnp, idp, NULL);
2777 dt_node_type_assign(dnp,
2778 DT_DYN_CTFP(dtp), DT_DYN_TYPE(dtp));
2779 attr = idp->di_attr;
2782 free(dnp->dn_string);
2783 dnp->dn_string = NULL;
2784 dnp->dn_kind = dnkind;
2785 dnp->dn_ident = idp;
2786 dnp->dn_flags |= DT_NF_LVALUE | DT_NF_WRITABLE;
2788 dt_node_attr_assign(dnp, attr);
2790 } else if (scope != DTRACE_OBJ_EXEC) {
2791 xyerror(D_IDENT_UNDEF, "failed to resolve %s%s%s: %s\n",
2792 dnp->dn_string, mark, name,
2793 dtrace_errmsg(dtp, dtrace_errno(dtp)));
2795 xyerror(D_IDENT_UNDEF, "failed to resolve %s: %s\n",
2796 dnp->dn_string, dtrace_errmsg(dtp, dtrace_errno(dtp)));
2801 dt_cook_ident(dt_node_t *dnp, uint_t idflags)
2803 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
2805 if (dnp->dn_op == DT_TOK_AGG)
2806 dt_xcook_ident(dnp, dtp->dt_aggs, DT_IDENT_AGG, B_FALSE);
2808 dt_xcook_ident(dnp, dtp->dt_globals, DT_IDENT_SCALAR, B_FALSE);
2810 return (dt_node_cook(dnp, idflags));
2814 * Since operators [ and -> can instantiate new variables before we know
2815 * whether the reference is for a read or a write, we need to check read
2816 * references to determine if the identifier is currently dt_ident_unref().
2817 * If so, we report that this first access was to an undefined variable.
2820 dt_cook_var(dt_node_t *dnp, uint_t idflags)
2822 dt_ident_t *idp = dnp->dn_ident;
2824 if ((idflags & DT_IDFLG_REF) && dt_ident_unref(idp)) {
2825 dnerror(dnp, D_VAR_UNDEF,
2826 "%s%s has not yet been declared or assigned\n",
2827 (idp->di_flags & DT_IDFLG_LOCAL) ? "this->" :
2828 (idp->di_flags & DT_IDFLG_TLS) ? "self->" : "",
2832 dt_node_attr_assign(dnp, dt_ident_cook(dnp, idp, &dnp->dn_args));
2838 dt_cook_func(dt_node_t *dnp, uint_t idflags)
2840 dt_node_attr_assign(dnp,
2841 dt_ident_cook(dnp, dnp->dn_ident, &dnp->dn_args));
2847 dt_cook_op1(dt_node_t *dnp, uint_t idflags)
2849 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
2850 dt_node_t *cp = dnp->dn_child;
2852 char n[DT_TYPE_NAMELEN];
2853 dtrace_typeinfo_t dtt;
2858 ctf_id_t type, base;
2861 if (dnp->dn_op == DT_TOK_PREINC || dnp->dn_op == DT_TOK_POSTINC ||
2862 dnp->dn_op == DT_TOK_PREDEC || dnp->dn_op == DT_TOK_POSTDEC)
2863 idflags = DT_IDFLG_REF | DT_IDFLG_MOD;
2865 idflags = DT_IDFLG_REF;
2868 * We allow the unary ++ and -- operators to instantiate new scalar
2869 * variables if applied to an identifier; otherwise just cook as usual.
2871 if (cp->dn_kind == DT_NODE_IDENT && (idflags & DT_IDFLG_MOD))
2872 dt_xcook_ident(cp, dtp->dt_globals, DT_IDENT_SCALAR, B_TRUE);
2874 cp = dnp->dn_child = dt_node_cook(cp, 0); /* don't set idflags yet */
2876 if (cp->dn_kind == DT_NODE_VAR && dt_ident_unref(cp->dn_ident)) {
2877 if (dt_type_lookup("int64_t", &dtt) != 0)
2878 xyerror(D_TYPE_ERR, "failed to lookup int64_t\n");
2880 dt_ident_type_assign(cp->dn_ident, dtt.dtt_ctfp, dtt.dtt_type);
2881 dt_node_type_assign(cp, dtt.dtt_ctfp, dtt.dtt_type);
2884 if (cp->dn_kind == DT_NODE_VAR)
2885 cp->dn_ident->di_flags |= idflags;
2887 switch (dnp->dn_op) {
2890 * If the deref operator is applied to a translated pointer,
2891 * we can just set our output type to the base translation.
2893 if ((idp = dt_node_resolve(cp, DT_IDENT_XLPTR)) != NULL) {
2894 dt_xlator_t *dxp = idp->di_data;
2896 dnp->dn_ident = &dxp->dx_souid;
2897 dt_node_type_assign(dnp,
2898 DT_DYN_CTFP(dtp), DT_DYN_TYPE(dtp));
2902 type = ctf_type_resolve(cp->dn_ctfp, cp->dn_type);
2903 kind = ctf_type_kind(cp->dn_ctfp, type);
2905 if (kind == CTF_K_ARRAY) {
2906 if (ctf_array_info(cp->dn_ctfp, type, &r) != 0) {
2907 dtp->dt_ctferr = ctf_errno(cp->dn_ctfp);
2908 longjmp(yypcb->pcb_jmpbuf, EDT_CTF);
2910 type = r.ctr_contents;
2911 } else if (kind == CTF_K_POINTER) {
2912 type = ctf_type_reference(cp->dn_ctfp, type);
2914 xyerror(D_DEREF_NONPTR,
2915 "cannot dereference non-pointer type\n");
2918 dt_node_type_assign(dnp, cp->dn_ctfp, type);
2919 base = ctf_type_resolve(cp->dn_ctfp, type);
2920 kind = ctf_type_kind(cp->dn_ctfp, base);
2922 if (kind == CTF_K_INTEGER && ctf_type_encoding(cp->dn_ctfp,
2923 base, &e) == 0 && IS_VOID(e)) {
2924 xyerror(D_DEREF_VOID,
2925 "cannot dereference pointer to void\n");
2928 if (kind == CTF_K_FUNCTION) {
2929 xyerror(D_DEREF_FUNC,
2930 "cannot dereference pointer to function\n");
2933 if (kind != CTF_K_ARRAY || dt_node_is_string(dnp))
2934 dnp->dn_flags |= DT_NF_LVALUE; /* see K&R[A7.4.3] */
2937 * If we propagated the l-value bit and the child operand was
2938 * a writable D variable or a binary operation of the form
2939 * a + b where a is writable, then propagate the writable bit.
2940 * This is necessary to permit assignments to scalar arrays,
2941 * which are converted to expressions of the form *(a + i).
2943 if ((cp->dn_flags & DT_NF_WRITABLE) ||
2944 (cp->dn_kind == DT_NODE_OP2 && cp->dn_op == DT_TOK_ADD &&
2945 (cp->dn_left->dn_flags & DT_NF_WRITABLE)))
2946 dnp->dn_flags |= DT_NF_WRITABLE;
2948 if ((cp->dn_flags & DT_NF_USERLAND) &&
2949 (kind == CTF_K_POINTER || (dnp->dn_flags & DT_NF_REF)))
2950 dnp->dn_flags |= DT_NF_USERLAND;
2955 if (!dt_node_is_arith(cp)) {
2956 xyerror(D_OP_ARITH, "operator %s requires an operand "
2957 "of arithmetic type\n", opstr(dnp->dn_op));
2959 dt_node_type_propagate(cp, dnp); /* see K&R[A7.4.4-6] */
2963 if (!dt_node_is_integer(cp)) {
2964 xyerror(D_OP_INT, "operator %s requires an operand of "
2965 "integral type\n", opstr(dnp->dn_op));
2967 dt_node_type_propagate(cp, dnp); /* see K&R[A7.4.4-6] */
2971 if (!dt_node_is_scalar(cp)) {
2972 xyerror(D_OP_SCALAR, "operator %s requires an operand "
2973 "of scalar type\n", opstr(dnp->dn_op));
2975 dt_node_type_assign(dnp, DT_INT_CTFP(dtp), DT_INT_TYPE(dtp));
2979 if (cp->dn_kind == DT_NODE_VAR || cp->dn_kind == DT_NODE_AGG) {
2980 xyerror(D_ADDROF_VAR,
2981 "cannot take address of dynamic variable\n");
2984 if (dt_node_is_dynamic(cp)) {
2985 xyerror(D_ADDROF_VAR,
2986 "cannot take address of dynamic object\n");
2989 if (!(cp->dn_flags & DT_NF_LVALUE)) {
2990 xyerror(D_ADDROF_LVAL, /* see K&R[A7.4.2] */
2991 "unacceptable operand for unary & operator\n");
2994 if (cp->dn_flags & DT_NF_BITFIELD) {
2995 xyerror(D_ADDROF_BITFIELD,
2996 "cannot take address of bit-field\n");
2999 dtt.dtt_object = NULL;
3000 dtt.dtt_ctfp = cp->dn_ctfp;
3001 dtt.dtt_type = cp->dn_type;
3003 if (dt_type_pointer(&dtt) == -1) {
3004 xyerror(D_TYPE_ERR, "cannot find type for \"&\": %s*\n",
3005 dt_node_type_name(cp, n, sizeof (n)));
3008 dt_node_type_assign(dnp, dtt.dtt_ctfp, dtt.dtt_type);
3010 if (cp->dn_flags & DT_NF_USERLAND)
3011 dnp->dn_flags |= DT_NF_USERLAND;
3015 if (cp->dn_flags & DT_NF_BITFIELD) {
3016 xyerror(D_SIZEOF_BITFIELD,
3017 "cannot apply sizeof to a bit-field\n");
3020 if (dt_node_sizeof(cp) == 0) {
3021 xyerror(D_SIZEOF_TYPE, "cannot apply sizeof to an "
3022 "operand of unknown size\n");
3025 dt_node_type_assign(dnp, dtp->dt_ddefs->dm_ctfp,
3026 ctf_lookup_by_name(dtp->dt_ddefs->dm_ctfp, "size_t"));
3029 case DT_TOK_STRINGOF:
3030 if (!dt_node_is_scalar(cp) && !dt_node_is_pointer(cp) &&
3031 !dt_node_is_strcompat(cp)) {
3032 xyerror(D_STRINGOF_TYPE,
3033 "cannot apply stringof to a value of type %s\n",
3034 dt_node_type_name(cp, n, sizeof (n)));
3036 dt_node_type_assign(dnp, DT_STR_CTFP(dtp), DT_STR_TYPE(dtp));
3040 case DT_TOK_POSTINC:
3042 case DT_TOK_POSTDEC:
3043 if (dt_node_is_scalar(cp) == 0) {
3044 xyerror(D_OP_SCALAR, "operator %s requires operand of "
3045 "scalar type\n", opstr(dnp->dn_op));
3048 if (dt_node_is_vfptr(cp)) {
3049 xyerror(D_OP_VFPTR, "operator %s requires an operand "
3050 "of known size\n", opstr(dnp->dn_op));
3053 if (!(cp->dn_flags & DT_NF_LVALUE)) {
3054 xyerror(D_OP_LVAL, "operator %s requires modifiable "
3055 "lvalue as an operand\n", opstr(dnp->dn_op));
3058 if (!(cp->dn_flags & DT_NF_WRITABLE)) {
3059 xyerror(D_OP_WRITE, "operator %s can only be applied "
3060 "to a writable variable\n", opstr(dnp->dn_op));
3063 dt_node_type_propagate(cp, dnp); /* see K&R[A7.4.1] */
3067 xyerror(D_UNKNOWN, "invalid unary op %s\n", opstr(dnp->dn_op));
3070 dt_node_attr_assign(dnp, cp->dn_attr);
3075 dt_cook_op2(dt_node_t *dnp, uint_t idflags)
3077 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
3078 dt_node_t *lp = dnp->dn_left;
3079 dt_node_t *rp = dnp->dn_right;
3080 int op = dnp->dn_op;
3085 int kind, val, uref;
3088 char n1[DT_TYPE_NAMELEN];
3089 char n2[DT_TYPE_NAMELEN];
3092 * The expression E1[E2] is identical by definition to *((E1)+(E2)) so
3093 * we convert "[" to "+" and glue on "*" at the end (see K&R[A7.3.1])
3094 * unless the left-hand side is an untyped D scalar, associative array,
3095 * or aggregation. In these cases, we proceed to case DT_TOK_LBRAC and
3096 * handle associative array and aggregation references there.
3098 if (op == DT_TOK_LBRAC) {
3099 if (lp->dn_kind == DT_NODE_IDENT) {
3103 if (lp->dn_op == DT_TOK_AGG) {
3105 idp = dt_idhash_lookup(dhp, lp->dn_string + 1);
3106 idkind = DT_IDENT_AGG;
3108 dhp = dtp->dt_globals;
3109 idp = dt_idstack_lookup(
3110 &yypcb->pcb_globals, lp->dn_string);
3111 idkind = DT_IDENT_ARRAY;
3114 if (idp == NULL || dt_ident_unref(idp))
3115 dt_xcook_ident(lp, dhp, idkind, B_TRUE);
3117 dt_xcook_ident(lp, dhp, idp->di_kind, B_FALSE);
3119 lp = dnp->dn_left = dt_node_cook(lp, 0);
3122 * Switch op to '+' for *(E1 + E2) array mode in these cases:
3123 * (a) lp is a DT_IDENT_ARRAY variable that has already been
3124 * referenced using [] notation (dn_args != NULL).
3125 * (b) lp is a non-ARRAY variable that has already been given
3126 * a type by assignment or declaration (!dt_ident_unref())
3127 * (c) lp is neither a variable nor an aggregation
3129 if (lp->dn_kind == DT_NODE_VAR) {
3130 if (lp->dn_ident->di_kind == DT_IDENT_ARRAY) {
3131 if (lp->dn_args != NULL)
3133 } else if (!dt_ident_unref(lp->dn_ident))
3135 } else if (lp->dn_kind != DT_NODE_AGG)
3143 lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF);
3144 rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
3146 if (!dt_node_is_integer(lp) || !dt_node_is_integer(rp)) {
3147 xyerror(D_OP_INT, "operator %s requires operands of "
3148 "integral type\n", opstr(op));
3151 dt_node_promote(lp, rp, dnp); /* see K&R[A7.11-13] */
3156 lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF);
3157 rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
3159 if (!dt_node_is_integer(lp) || !dt_node_is_integer(rp)) {
3160 xyerror(D_OP_INT, "operator %s requires operands of "
3161 "integral type\n", opstr(op));
3164 dt_node_type_propagate(lp, dnp); /* see K&R[A7.8] */
3165 dt_node_attr_assign(dnp, dt_attr_min(lp->dn_attr, rp->dn_attr));
3169 lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF);
3170 rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
3172 if (!dt_node_is_integer(lp) || !dt_node_is_integer(rp)) {
3173 xyerror(D_OP_INT, "operator %s requires operands of "
3174 "integral type\n", opstr(op));
3177 dt_node_promote(lp, rp, dnp); /* see K&R[A7.6] */
3182 lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF);
3183 rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
3185 if (!dt_node_is_arith(lp) || !dt_node_is_arith(rp)) {
3186 xyerror(D_OP_ARITH, "operator %s requires operands of "
3187 "arithmetic type\n", opstr(op));
3190 dt_node_promote(lp, rp, dnp); /* see K&R[A7.6] */
3196 lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF);
3197 rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
3199 if (!dt_node_is_scalar(lp) || !dt_node_is_scalar(rp)) {
3200 xyerror(D_OP_SCALAR, "operator %s requires operands "
3201 "of scalar type\n", opstr(op));
3204 dt_node_type_assign(dnp, DT_INT_CTFP(dtp), DT_INT_TYPE(dtp));
3205 dt_node_attr_assign(dnp, dt_attr_min(lp->dn_attr, rp->dn_attr));
3215 * The D comparison operators provide the ability to transform
3216 * a right-hand identifier into a corresponding enum tag value
3217 * if the left-hand side is an enum type. To do this, we cook
3218 * the left-hand side, and then see if the right-hand side is
3219 * an unscoped identifier defined in the enum. If so, we
3220 * convert into an integer constant node with the tag's value.
3222 lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF);
3224 kind = ctf_type_kind(lp->dn_ctfp,
3225 ctf_type_resolve(lp->dn_ctfp, lp->dn_type));
3227 if (kind == CTF_K_ENUM && rp->dn_kind == DT_NODE_IDENT &&
3228 strchr(rp->dn_string, '`') == NULL && ctf_enum_value(
3229 lp->dn_ctfp, lp->dn_type, rp->dn_string, &val) == 0) {
3231 if ((idp = dt_idstack_lookup(&yypcb->pcb_globals,
3232 rp->dn_string)) != NULL) {
3233 xyerror(D_IDENT_AMBIG,
3234 "ambiguous use of operator %s: %s is "
3235 "both a %s enum tag and a global %s\n",
3236 opstr(op), rp->dn_string,
3237 dt_node_type_name(lp, n1, sizeof (n1)),
3238 dt_idkind_name(idp->di_kind));
3241 free(rp->dn_string);
3242 rp->dn_string = NULL;
3243 rp->dn_kind = DT_NODE_INT;
3244 rp->dn_flags |= DT_NF_COOKED;
3245 rp->dn_op = DT_TOK_INT;
3246 rp->dn_value = (intmax_t)val;
3248 dt_node_type_assign(rp, lp->dn_ctfp, lp->dn_type);
3249 dt_node_attr_assign(rp, _dtrace_symattr);
3252 rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
3255 * The rules for type checking for the relational operators are
3256 * described in the ANSI-C spec (see K&R[A7.9-10]). We perform
3257 * the various tests in order from least to most expensive. We
3258 * also allow derived strings to be compared as a first-class
3259 * type (resulting in a strcmp(3C)-style comparison), and we
3260 * slightly relax the A7.9 rules to permit void pointer
3261 * comparisons as in A7.10. Our users won't be confused by
3262 * this since they understand pointers are just numbers, and
3263 * relaxing this constraint simplifies the implementation.
3265 if (ctf_type_compat(lp->dn_ctfp, lp->dn_type,
3266 rp->dn_ctfp, rp->dn_type))
3268 else if (dt_node_is_integer(lp) && dt_node_is_integer(rp))
3270 else if (dt_node_is_strcompat(lp) && dt_node_is_strcompat(rp) &&
3271 (dt_node_is_string(lp) || dt_node_is_string(rp)))
3273 else if (dt_node_is_ptrcompat(lp, rp, NULL, NULL) == 0) {
3274 xyerror(D_OP_INCOMPAT, "operands have "
3275 "incompatible types: \"%s\" %s \"%s\"\n",
3276 dt_node_type_name(lp, n1, sizeof (n1)), opstr(op),
3277 dt_node_type_name(rp, n2, sizeof (n2)));
3280 dt_node_type_assign(dnp, DT_INT_CTFP(dtp), DT_INT_TYPE(dtp));
3281 dt_node_attr_assign(dnp, dt_attr_min(lp->dn_attr, rp->dn_attr));
3287 * The rules for type checking for the additive operators are
3288 * described in the ANSI-C spec (see K&R[A7.7]). Pointers and
3289 * integers may be manipulated according to specific rules. In
3290 * these cases D permits strings to be treated as pointers.
3292 int lp_is_ptr, lp_is_int, rp_is_ptr, rp_is_int;
3294 lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF);
3295 rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
3297 lp_is_ptr = dt_node_is_string(lp) ||
3298 (dt_node_is_pointer(lp) && !dt_node_is_vfptr(lp));
3299 lp_is_int = dt_node_is_integer(lp);
3301 rp_is_ptr = dt_node_is_string(rp) ||
3302 (dt_node_is_pointer(rp) && !dt_node_is_vfptr(rp));
3303 rp_is_int = dt_node_is_integer(rp);
3305 if (lp_is_int && rp_is_int) {
3306 dt_type_promote(lp, rp, &ctfp, &type);
3308 } else if (lp_is_ptr && rp_is_int) {
3311 uref = lp->dn_flags & DT_NF_USERLAND;
3312 } else if (lp_is_int && rp_is_ptr && op == DT_TOK_ADD) {
3315 uref = rp->dn_flags & DT_NF_USERLAND;
3316 } else if (lp_is_ptr && rp_is_ptr && op == DT_TOK_SUB &&
3317 dt_node_is_ptrcompat(lp, rp, NULL, NULL)) {
3318 ctfp = dtp->dt_ddefs->dm_ctfp;
3319 type = ctf_lookup_by_name(ctfp, "ptrdiff_t");
3322 xyerror(D_OP_INCOMPAT, "operands have incompatible "
3323 "types: \"%s\" %s \"%s\"\n",
3324 dt_node_type_name(lp, n1, sizeof (n1)), opstr(op),
3325 dt_node_type_name(rp, n2, sizeof (n2)));
3328 dt_node_type_assign(dnp, ctfp, type);
3329 dt_node_attr_assign(dnp, dt_attr_min(lp->dn_attr, rp->dn_attr));
3332 dnp->dn_flags |= DT_NF_USERLAND;
3342 if (lp->dn_kind == DT_NODE_IDENT) {
3343 dt_xcook_ident(lp, dtp->dt_globals,
3344 DT_IDENT_SCALAR, B_TRUE);
3348 dt_node_cook(lp, DT_IDFLG_REF | DT_IDFLG_MOD);
3350 rp = dnp->dn_right =
3351 dt_node_cook(rp, DT_IDFLG_REF | DT_IDFLG_MOD);
3353 if (!dt_node_is_integer(lp) || !dt_node_is_integer(rp)) {
3354 xyerror(D_OP_INT, "operator %s requires operands of "
3355 "integral type\n", opstr(op));
3361 if (lp->dn_kind == DT_NODE_IDENT) {
3362 dt_xcook_ident(lp, dtp->dt_globals,
3363 DT_IDENT_SCALAR, B_TRUE);
3367 dt_node_cook(lp, DT_IDFLG_REF | DT_IDFLG_MOD);
3369 rp = dnp->dn_right =
3370 dt_node_cook(rp, DT_IDFLG_REF | DT_IDFLG_MOD);
3372 if (!dt_node_is_arith(lp) || !dt_node_is_arith(rp)) {
3373 xyerror(D_OP_ARITH, "operator %s requires operands of "
3374 "arithmetic type\n", opstr(op));
3380 * If the left-hand side is an identifier, attempt to resolve
3381 * it as either an aggregation or scalar variable. We pass
3382 * B_TRUE to dt_xcook_ident to indicate that a new variable can
3383 * be created if no matching variable exists in the namespace.
3385 if (lp->dn_kind == DT_NODE_IDENT) {
3386 if (lp->dn_op == DT_TOK_AGG) {
3387 dt_xcook_ident(lp, dtp->dt_aggs,
3388 DT_IDENT_AGG, B_TRUE);
3390 dt_xcook_ident(lp, dtp->dt_globals,
3391 DT_IDENT_SCALAR, B_TRUE);
3395 lp = dnp->dn_left = dt_node_cook(lp, 0); /* don't set mod yet */
3396 rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
3399 * If the left-hand side is an aggregation, verify that we are
3400 * assigning it the result of an aggregating function. Once
3401 * we've done so, hide the func node in the aggregation and
3402 * return the aggregation itself up to the parse tree parent.
3403 * This transformation is legal since the assigned function
3404 * cannot change identity across disjoint cooking passes and
3405 * the argument list subtree is retained for later cooking.
3407 if (lp->dn_kind == DT_NODE_AGG) {
3408 const char *aname = lp->dn_ident->di_name;
3409 dt_ident_t *oid = lp->dn_ident->di_iarg;
3411 if (rp->dn_kind != DT_NODE_FUNC ||
3412 rp->dn_ident->di_kind != DT_IDENT_AGGFUNC) {
3414 "@%s must be assigned the result of "
3415 "an aggregating function\n", aname);
3418 if (oid != NULL && oid != rp->dn_ident) {
3419 xyerror(D_AGG_REDEF,
3420 "aggregation redefined: @%s\n\t "
3421 "current: @%s = %s( )\n\tprevious: @%s = "
3422 "%s( ) : line %d\n", aname, aname,
3423 rp->dn_ident->di_name, aname, oid->di_name,
3424 lp->dn_ident->di_lineno);
3425 } else if (oid == NULL)
3426 lp->dn_ident->di_iarg = rp->dn_ident;
3429 * Do not allow multiple aggregation assignments in a
3430 * single statement, e.g. (@a = count()) = count();
3431 * We produce a message as if the result of aggregating
3432 * function does not propagate DT_NF_LVALUE.
3434 if (lp->dn_aggfun != NULL) {
3435 xyerror(D_OP_LVAL, "operator = requires "
3436 "modifiable lvalue as an operand\n");
3440 lp = dt_node_cook(lp, DT_IDFLG_MOD);
3442 dnp->dn_left = dnp->dn_right = NULL;
3449 * If the right-hand side is a dynamic variable that is the
3450 * output of a translator, our result is the translated type.
3452 if ((idp = dt_node_resolve(rp, DT_IDENT_XLSOU)) != NULL) {
3453 ctfp = idp->di_ctfp;
3454 type = idp->di_type;
3455 uref = idp->di_flags & DT_IDFLG_USER;
3459 uref = rp->dn_flags & DT_NF_USERLAND;
3463 * If the left-hand side of an assignment statement is a virgin
3464 * variable created by this compilation pass, reset the type of
3465 * this variable to the type of the right-hand side.
3467 if (lp->dn_kind == DT_NODE_VAR &&
3468 dt_ident_unref(lp->dn_ident)) {
3469 dt_node_type_assign(lp, ctfp, type);
3470 dt_ident_type_assign(lp->dn_ident, ctfp, type);
3473 lp->dn_flags |= DT_NF_USERLAND;
3474 lp->dn_ident->di_flags |= DT_IDFLG_USER;
3478 if (lp->dn_kind == DT_NODE_VAR)
3479 lp->dn_ident->di_flags |= DT_IDFLG_MOD;
3482 * The rules for type checking for the assignment operators are
3483 * described in the ANSI-C spec (see K&R[A7.17]). We share
3484 * most of this code with the argument list checking code.
3486 if (!dt_node_is_string(lp)) {
3487 kind = ctf_type_kind(lp->dn_ctfp,
3488 ctf_type_resolve(lp->dn_ctfp, lp->dn_type));
3490 if (kind == CTF_K_ARRAY || kind == CTF_K_FUNCTION) {
3491 xyerror(D_OP_ARRFUN, "operator %s may not be "
3492 "applied to operand of type \"%s\"\n",
3494 dt_node_type_name(lp, n1, sizeof (n1)));
3498 if (idp != NULL && idp->di_kind == DT_IDENT_XLSOU &&
3499 ctf_type_compat(lp->dn_ctfp, lp->dn_type, ctfp, type))
3502 if (dt_node_is_argcompat(lp, rp))
3505 xyerror(D_OP_INCOMPAT,
3506 "operands have incompatible types: \"%s\" %s \"%s\"\n",
3507 dt_node_type_name(lp, n1, sizeof (n1)), opstr(op),
3508 dt_node_type_name(rp, n2, sizeof (n2)));
3513 if (lp->dn_kind == DT_NODE_IDENT) {
3514 dt_xcook_ident(lp, dtp->dt_globals,
3515 DT_IDENT_SCALAR, B_TRUE);
3519 dt_node_cook(lp, DT_IDFLG_REF | DT_IDFLG_MOD);
3521 rp = dnp->dn_right =
3522 dt_node_cook(rp, DT_IDFLG_REF | DT_IDFLG_MOD);
3524 if (dt_node_is_string(lp) || dt_node_is_string(rp)) {
3525 xyerror(D_OP_INCOMPAT, "operands have "
3526 "incompatible types: \"%s\" %s \"%s\"\n",
3527 dt_node_type_name(lp, n1, sizeof (n1)), opstr(op),
3528 dt_node_type_name(rp, n2, sizeof (n2)));
3532 * The rules for type checking for the assignment operators are
3533 * described in the ANSI-C spec (see K&R[A7.17]). To these
3534 * rules we add that only writable D nodes can be modified.
3536 if (dt_node_is_integer(lp) == 0 ||
3537 dt_node_is_integer(rp) == 0) {
3538 if (!dt_node_is_pointer(lp) || dt_node_is_vfptr(lp)) {
3540 "operator %s requires left-hand scalar "
3541 "operand of known size\n", opstr(op));
3542 } else if (dt_node_is_integer(rp) == 0 &&
3543 dt_node_is_ptrcompat(lp, rp, NULL, NULL) == 0) {
3544 xyerror(D_OP_INCOMPAT, "operands have "
3545 "incompatible types: \"%s\" %s \"%s\"\n",
3546 dt_node_type_name(lp, n1, sizeof (n1)),
3548 dt_node_type_name(rp, n2, sizeof (n2)));
3552 if (!(lp->dn_flags & DT_NF_LVALUE)) {
3553 xyerror(D_OP_LVAL, "operator %s requires modifiable "
3554 "lvalue as an operand\n", opstr(op));
3555 /* see K&R[A7.17] */
3558 if (!(lp->dn_flags & DT_NF_WRITABLE)) {
3559 xyerror(D_OP_WRITE, "operator %s can only be applied "
3560 "to a writable variable\n", opstr(op));
3563 dt_node_type_propagate(lp, dnp); /* see K&R[A7.17] */
3564 dt_node_attr_assign(dnp, dt_attr_min(lp->dn_attr, rp->dn_attr));
3569 * If the left-hand side of operator -> is the name "self",
3570 * then we permit a TLS variable to be created or referenced.
3572 if (lp->dn_kind == DT_NODE_IDENT &&
3573 strcmp(lp->dn_string, "self") == 0) {
3574 if (rp->dn_kind != DT_NODE_VAR) {
3575 dt_xcook_ident(rp, dtp->dt_tls,
3576 DT_IDENT_SCALAR, B_TRUE);
3580 rp = dt_node_cook(rp, idflags);
3582 dnp->dn_right = dnp->dn_left; /* avoid freeing rp */
3588 * If the left-hand side of operator -> is the name "this",
3589 * then we permit a local variable to be created or referenced.
3591 if (lp->dn_kind == DT_NODE_IDENT &&
3592 strcmp(lp->dn_string, "this") == 0) {
3593 if (rp->dn_kind != DT_NODE_VAR) {
3594 dt_xcook_ident(rp, yypcb->pcb_locals,
3595 DT_IDENT_SCALAR, B_TRUE);
3599 rp = dt_node_cook(rp, idflags);
3601 dnp->dn_right = dnp->dn_left; /* avoid freeing rp */
3609 lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF);
3611 if (rp->dn_kind != DT_NODE_IDENT) {
3612 xyerror(D_OP_IDENT, "operator %s must be followed by "
3613 "an identifier\n", opstr(op));
3616 if ((idp = dt_node_resolve(lp, DT_IDENT_XLSOU)) != NULL ||
3617 (idp = dt_node_resolve(lp, DT_IDENT_XLPTR)) != NULL) {
3619 * If the left-hand side is a translated struct or ptr,
3620 * the type of the left is the translation output type.
3622 dt_xlator_t *dxp = idp->di_data;
3624 if (dt_xlator_member(dxp, rp->dn_string) == NULL) {
3625 xyerror(D_XLATE_NOCONV,
3626 "translator does not define conversion "
3627 "for member: %s\n", rp->dn_string);
3630 ctfp = idp->di_ctfp;
3631 type = ctf_type_resolve(ctfp, idp->di_type);
3632 uref = idp->di_flags & DT_IDFLG_USER;
3635 type = ctf_type_resolve(ctfp, lp->dn_type);
3636 uref = lp->dn_flags & DT_NF_USERLAND;
3639 kind = ctf_type_kind(ctfp, type);
3641 if (op == DT_TOK_PTR) {
3642 if (kind != CTF_K_POINTER) {
3643 xyerror(D_OP_PTR, "operator %s must be "
3644 "applied to a pointer\n", opstr(op));
3646 type = ctf_type_reference(ctfp, type);
3647 type = ctf_type_resolve(ctfp, type);
3648 kind = ctf_type_kind(ctfp, type);
3652 * If we follow a reference to a forward declaration tag,
3653 * search the entire type space for the actual definition.
3655 while (kind == CTF_K_FORWARD) {
3656 char *tag = ctf_type_name(ctfp, type, n1, sizeof (n1));
3657 dtrace_typeinfo_t dtt;
3659 if (tag != NULL && dt_type_lookup(tag, &dtt) == 0 &&
3660 (dtt.dtt_ctfp != ctfp || dtt.dtt_type != type)) {
3661 ctfp = dtt.dtt_ctfp;
3662 type = ctf_type_resolve(ctfp, dtt.dtt_type);
3663 kind = ctf_type_kind(ctfp, type);
3665 xyerror(D_OP_INCOMPLETE,
3666 "operator %s cannot be applied to a "
3667 "forward declaration: no %s definition "
3668 "is available\n", opstr(op), tag);
3672 if (kind != CTF_K_STRUCT && kind != CTF_K_UNION) {
3673 if (op == DT_TOK_PTR) {
3674 xyerror(D_OP_SOU, "operator -> cannot be "
3675 "applied to pointer to type \"%s\"; must "
3676 "be applied to a struct or union pointer\n",
3677 ctf_type_name(ctfp, type, n1, sizeof (n1)));
3679 xyerror(D_OP_SOU, "operator %s cannot be "
3680 "applied to type \"%s\"; must be applied "
3681 "to a struct or union\n", opstr(op),
3682 ctf_type_name(ctfp, type, n1, sizeof (n1)));
3686 if (ctf_member_info(ctfp, type, rp->dn_string, &m) == CTF_ERR) {
3687 xyerror(D_TYPE_MEMBER,
3688 "%s is not a member of %s\n", rp->dn_string,
3689 ctf_type_name(ctfp, type, n1, sizeof (n1)));
3692 type = ctf_type_resolve(ctfp, m.ctm_type);
3693 kind = ctf_type_kind(ctfp, type);
3695 dt_node_type_assign(dnp, ctfp, m.ctm_type);
3696 dt_node_attr_assign(dnp, lp->dn_attr);
3698 if (op == DT_TOK_PTR && (kind != CTF_K_ARRAY ||
3699 dt_node_is_string(dnp)))
3700 dnp->dn_flags |= DT_NF_LVALUE; /* see K&R[A7.3.3] */
3702 if (op == DT_TOK_DOT && (lp->dn_flags & DT_NF_LVALUE) &&
3703 (kind != CTF_K_ARRAY || dt_node_is_string(dnp)))
3704 dnp->dn_flags |= DT_NF_LVALUE; /* see K&R[A7.3.3] */
3706 if (lp->dn_flags & DT_NF_WRITABLE)
3707 dnp->dn_flags |= DT_NF_WRITABLE;
3709 if (uref && (kind == CTF_K_POINTER ||
3710 (dnp->dn_flags & DT_NF_REF)))
3711 dnp->dn_flags |= DT_NF_USERLAND;
3714 case DT_TOK_LBRAC: {
3716 * If op is DT_TOK_LBRAC, we know from the special-case code at
3717 * the top that lp is either a D variable or an aggregation.
3722 * If the left-hand side is an aggregation, just set dn_aggtup
3723 * to the right-hand side and return the cooked aggregation.
3724 * This transformation is legal since we are just collapsing
3725 * nodes to simplify later processing, and the entire aggtup
3726 * parse subtree is retained for subsequent cooking passes.
3728 if (lp->dn_kind == DT_NODE_AGG) {
3729 if (lp->dn_aggtup != NULL) {
3730 xyerror(D_AGG_MDIM, "improper attempt to "
3731 "reference @%s as a multi-dimensional "
3732 "array\n", lp->dn_ident->di_name);
3736 lp = dt_node_cook(lp, 0);
3738 dnp->dn_left = dnp->dn_right = NULL;
3744 assert(lp->dn_kind == DT_NODE_VAR);
3748 * If the left-hand side is a non-global scalar that hasn't yet
3749 * been referenced or modified, it was just created by self->
3750 * or this-> and we can convert it from scalar to assoc array.
3752 if (idp->di_kind == DT_IDENT_SCALAR && dt_ident_unref(idp) &&
3753 (idp->di_flags & (DT_IDFLG_LOCAL | DT_IDFLG_TLS)) != 0) {
3755 if (idp->di_flags & DT_IDFLG_LOCAL) {
3756 xyerror(D_ARR_LOCAL,
3757 "local variables may not be used as "
3758 "associative arrays: %s\n", idp->di_name);
3761 dt_dprintf("morph variable %s (id %u) from scalar to "
3762 "array\n", idp->di_name, idp->di_id);
3764 dt_ident_morph(idp, DT_IDENT_ARRAY,
3765 &dt_idops_assc, NULL);
3768 if (idp->di_kind != DT_IDENT_ARRAY) {
3769 xyerror(D_IDENT_BADREF, "%s '%s' may not be referenced "
3770 "as %s\n", dt_idkind_name(idp->di_kind),
3771 idp->di_name, dt_idkind_name(DT_IDENT_ARRAY));
3775 * Now that we've confirmed our left-hand side is a DT_NODE_VAR
3776 * of idkind DT_IDENT_ARRAY, we need to splice the [ node from
3777 * the parse tree and leave a cooked DT_NODE_VAR in its place
3778 * where dn_args for the VAR node is the right-hand 'rp' tree,
3779 * as shown in the parse tree diagram below:
3782 * [ OP2 "[" ]=dnp [ VAR ]=dnp
3784 * / \ +- dn_args -> [ ??? ]=rp
3785 * [ VAR ]=lp [ ??? ]=rp
3787 * Since the final dt_node_cook(dnp) can fail using longjmp we
3788 * must perform the transformations as a group first by over-
3789 * writing 'dnp' to become the VAR node, so that the parse tree
3790 * is guaranteed to be in a consistent state if the cook fails.
3792 assert(lp->dn_kind == DT_NODE_VAR);
3793 assert(lp->dn_args == NULL);
3796 bcopy(lp, dnp, sizeof (dt_node_t));
3800 dnp->dn_list = NULL;
3803 return (dt_node_cook(dnp, idflags));
3806 case DT_TOK_XLATE: {
3809 assert(lp->dn_kind == DT_NODE_TYPE);
3810 rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
3811 dxp = dt_xlator_lookup(dtp, rp, lp, DT_XLATE_FUZZY);
3814 xyerror(D_XLATE_NONE,
3815 "cannot translate from \"%s\" to \"%s\"\n",
3816 dt_node_type_name(rp, n1, sizeof (n1)),
3817 dt_node_type_name(lp, n2, sizeof (n2)));
3820 dnp->dn_ident = dt_xlator_ident(dxp, lp->dn_ctfp, lp->dn_type);
3821 dt_node_type_assign(dnp, DT_DYN_CTFP(dtp), DT_DYN_TYPE(dtp));
3822 dt_node_attr_assign(dnp,
3823 dt_attr_min(rp->dn_attr, dnp->dn_ident->di_attr));
3828 ctf_id_t ltype, rtype;
3829 uint_t lkind, rkind;
3831 assert(lp->dn_kind == DT_NODE_TYPE);
3832 rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
3834 ltype = ctf_type_resolve(lp->dn_ctfp, lp->dn_type);
3835 lkind = ctf_type_kind(lp->dn_ctfp, ltype);
3837 rtype = ctf_type_resolve(rp->dn_ctfp, rp->dn_type);
3838 rkind = ctf_type_kind(rp->dn_ctfp, rtype);
3841 * The rules for casting are loosely explained in K&R[A7.5]
3842 * and K&R[A6]. Basically, we can cast to the same type or
3843 * same base type, between any kind of scalar values, from
3844 * arrays to pointers, and we can cast anything to void.
3845 * To these rules D adds casts from scalars to strings.
3847 if (ctf_type_compat(lp->dn_ctfp, lp->dn_type,
3848 rp->dn_ctfp, rp->dn_type))
3850 else if (dt_node_is_scalar(lp) &&
3851 (dt_node_is_scalar(rp) || rkind == CTF_K_FUNCTION))
3853 else if (dt_node_is_void(lp))
3855 else if (lkind == CTF_K_POINTER && dt_node_is_pointer(rp))
3857 else if (dt_node_is_string(lp) && (dt_node_is_scalar(rp) ||
3858 dt_node_is_pointer(rp) || dt_node_is_strcompat(rp)))
3861 xyerror(D_CAST_INVAL,
3862 "invalid cast expression: \"%s\" to \"%s\"\n",
3863 dt_node_type_name(rp, n1, sizeof (n1)),
3864 dt_node_type_name(lp, n2, sizeof (n2)));
3867 dt_node_type_propagate(lp, dnp); /* see K&R[A7.5] */
3868 dt_node_attr_assign(dnp, dt_attr_min(lp->dn_attr, rp->dn_attr));
3873 lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF);
3874 rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
3876 if (dt_node_is_dynamic(lp) || dt_node_is_dynamic(rp)) {
3877 xyerror(D_OP_DYN, "operator %s operands "
3878 "cannot be of dynamic type\n", opstr(op));
3881 if (dt_node_is_actfunc(lp) || dt_node_is_actfunc(rp)) {
3882 xyerror(D_OP_ACT, "operator %s operands "
3883 "cannot be actions\n", opstr(op));
3886 dt_node_type_propagate(rp, dnp); /* see K&R[A7.18] */
3887 dt_node_attr_assign(dnp, dt_attr_min(lp->dn_attr, rp->dn_attr));
3891 xyerror(D_UNKNOWN, "invalid binary op %s\n", opstr(op));
3895 * Complete the conversion of E1[E2] to *((E1)+(E2)) that we started
3896 * at the top of our switch() above (see K&R[A7.3.1]). Since E2 is
3897 * parsed as an argument_expression_list by dt_grammar.y, we can
3898 * end up with a comma-separated list inside of a non-associative
3899 * array reference. We check for this and report an appropriate error.
3901 if (dnp->dn_op == DT_TOK_LBRAC && op == DT_TOK_ADD) {
3904 if (rp->dn_list != NULL) {
3905 xyerror(D_ARR_BADREF,
3906 "cannot access %s as an associative array\n",
3907 dt_node_name(lp, n1, sizeof (n1)));
3910 dnp->dn_op = DT_TOK_ADD;
3911 pnp = dt_node_op1(DT_TOK_DEREF, dnp);
3914 * Cook callbacks are not typically permitted to allocate nodes.
3915 * When we do, we must insert them in the middle of an existing
3916 * allocation list rather than having them appended to the pcb
3917 * list because the sub-expression may be part of a definition.
3919 assert(yypcb->pcb_list == pnp);
3920 yypcb->pcb_list = pnp->dn_link;
3922 pnp->dn_link = dnp->dn_link;
3925 return (dt_node_cook(pnp, DT_IDFLG_REF));
3933 dt_cook_op3(dt_node_t *dnp, uint_t idflags)
3939 dnp->dn_expr = dt_node_cook(dnp->dn_expr, DT_IDFLG_REF);
3940 lp = dnp->dn_left = dt_node_cook(dnp->dn_left, DT_IDFLG_REF);
3941 rp = dnp->dn_right = dt_node_cook(dnp->dn_right, DT_IDFLG_REF);
3943 if (!dt_node_is_scalar(dnp->dn_expr)) {
3944 xyerror(D_OP_SCALAR,
3945 "operator ?: expression must be of scalar type\n");
3948 if (dt_node_is_dynamic(lp) || dt_node_is_dynamic(rp)) {
3950 "operator ?: operands cannot be of dynamic type\n");
3954 * The rules for type checking for the ternary operator are complex and
3955 * are described in the ANSI-C spec (see K&R[A7.16]). We implement
3956 * the various tests in order from least to most expensive.
3958 if (ctf_type_compat(lp->dn_ctfp, lp->dn_type,
3959 rp->dn_ctfp, rp->dn_type)) {
3962 } else if (dt_node_is_integer(lp) && dt_node_is_integer(rp)) {
3963 dt_type_promote(lp, rp, &ctfp, &type);
3964 } else if (dt_node_is_strcompat(lp) && dt_node_is_strcompat(rp) &&
3965 (dt_node_is_string(lp) || dt_node_is_string(rp))) {
3966 ctfp = DT_STR_CTFP(yypcb->pcb_hdl);
3967 type = DT_STR_TYPE(yypcb->pcb_hdl);
3968 } else if (dt_node_is_ptrcompat(lp, rp, &ctfp, &type) == 0) {
3969 xyerror(D_OP_INCOMPAT,
3970 "operator ?: operands must have compatible types\n");
3973 if (dt_node_is_actfunc(lp) || dt_node_is_actfunc(rp)) {
3974 xyerror(D_OP_ACT, "action cannot be "
3975 "used in a conditional context\n");
3978 dt_node_type_assign(dnp, ctfp, type);
3979 dt_node_attr_assign(dnp, dt_attr_min(dnp->dn_expr->dn_attr,
3980 dt_attr_min(lp->dn_attr, rp->dn_attr)));
3986 dt_cook_statement(dt_node_t *dnp, uint_t idflags)
3988 dnp->dn_expr = dt_node_cook(dnp->dn_expr, idflags);
3989 dt_node_attr_assign(dnp, dnp->dn_expr->dn_attr);
3995 * If dn_aggfun is set, this node is a collapsed aggregation assignment (see
3996 * the special case code for DT_TOK_ASGN in dt_cook_op2() above), in which
3997 * case we cook both the tuple and the function call. If dn_aggfun is NULL,
3998 * this node is just a reference to the aggregation's type and attributes.
4002 dt_cook_aggregation(dt_node_t *dnp, uint_t idflags)
4004 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
4006 if (dnp->dn_aggfun != NULL) {
4007 dnp->dn_aggfun = dt_node_cook(dnp->dn_aggfun, DT_IDFLG_REF);
4008 dt_node_attr_assign(dnp, dt_ident_cook(dnp,
4009 dnp->dn_ident, &dnp->dn_aggtup));
4011 dt_node_type_assign(dnp, DT_DYN_CTFP(dtp), DT_DYN_TYPE(dtp));
4012 dt_node_attr_assign(dnp, dnp->dn_ident->di_attr);
4019 * Since D permits new variable identifiers to be instantiated in any program
4020 * expression, we may need to cook a clause's predicate either before or after
4021 * the action list depending on the program code in question. Consider:
4023 * probe-description-list probe-description-list
4026 * trace(x); trace(x++);
4029 * In the left-hand example, the predicate uses operator ++ to instantiate 'x'
4030 * as a variable of type int64_t. The predicate must be cooked first because
4031 * otherwise the statement trace(x) refers to an unknown identifier. In the
4032 * right-hand example, the action list uses ++ to instantiate 'x'; the action
4033 * list must be cooked first because otherwise the predicate x == 0 refers to
4034 * an unknown identifier. In order to simplify programming, we support both.
4036 * When cooking a clause, we cook the action statements before the predicate by
4037 * default, since it seems more common to create or modify identifiers in the
4038 * action list. If cooking fails due to an unknown identifier, we attempt to
4039 * cook the predicate (i.e. do it first) and then go back and cook the actions.
4040 * If this, too, fails (or if we get an error other than D_IDENT_UNDEF) we give
4041 * up and report failure back to the user. There are five possible paths:
4043 * cook actions = OK, cook predicate = OK -> OK
4044 * cook actions = OK, cook predicate = ERR -> ERR
4045 * cook actions = ERR, cook predicate = ERR -> ERR
4046 * cook actions = ERR, cook predicate = OK, cook actions = OK -> OK
4047 * cook actions = ERR, cook predicate = OK, cook actions = ERR -> ERR
4049 * The programmer can still defeat our scheme by creating circular definition
4050 * dependencies between predicates and actions, as in this example clause:
4052 * probe-description-list
4058 * but it doesn't seem worth the complexity to handle such rare cases. The
4059 * user can simply use the D variable declaration syntax to work around them.
4062 dt_cook_clause(dt_node_t *dnp, uint_t idflags)
4064 volatile int err, tries;
4068 * Before assigning dn_ctxattr, temporarily assign the probe attribute
4069 * to 'dnp' itself to force an attribute check and minimum violation.
4071 dt_node_attr_assign(dnp, yypcb->pcb_pinfo.dtp_attr);
4072 dnp->dn_ctxattr = yypcb->pcb_pinfo.dtp_attr;
4074 bcopy(yypcb->pcb_jmpbuf, ojb, sizeof (jmp_buf));
4077 if (dnp->dn_pred != NULL && (err = setjmp(yypcb->pcb_jmpbuf)) != 0) {
4078 bcopy(ojb, yypcb->pcb_jmpbuf, sizeof (jmp_buf));
4079 if (tries++ != 0 || err != EDT_COMPILER || (
4080 yypcb->pcb_hdl->dt_errtag != dt_errtag(D_IDENT_UNDEF) &&
4081 yypcb->pcb_hdl->dt_errtag != dt_errtag(D_VAR_UNDEF)))
4082 longjmp(yypcb->pcb_jmpbuf, err);
4086 yylabel("action list");
4088 dt_node_attr_assign(dnp,
4089 dt_node_list_cook(&dnp->dn_acts, idflags));
4091 bcopy(ojb, yypcb->pcb_jmpbuf, sizeof (jmp_buf));
4095 if (dnp->dn_pred != NULL) {
4096 yylabel("predicate");
4098 dnp->dn_pred = dt_node_cook(dnp->dn_pred, idflags);
4099 dt_node_attr_assign(dnp,
4100 dt_attr_min(dnp->dn_attr, dnp->dn_pred->dn_attr));
4102 if (!dt_node_is_scalar(dnp->dn_pred)) {
4103 xyerror(D_PRED_SCALAR,
4104 "predicate result must be of scalar type\n");
4111 yylabel("action list");
4113 dt_node_attr_assign(dnp,
4114 dt_node_list_cook(&dnp->dn_acts, idflags));
4124 dt_cook_inline(dt_node_t *dnp, uint_t idflags)
4126 dt_idnode_t *inp = dnp->dn_ident->di_iarg;
4129 char n1[DT_TYPE_NAMELEN];
4130 char n2[DT_TYPE_NAMELEN];
4132 assert(dnp->dn_ident->di_flags & DT_IDFLG_INLINE);
4133 assert(inp->din_root->dn_flags & DT_NF_COOKED);
4136 * If we are inlining a translation, verify that the inline declaration
4137 * type exactly matches the type that is returned by the translation.
4138 * Otherwise just use dt_node_is_argcompat() to check the types.
4140 if ((rdp = dt_node_resolve(inp->din_root, DT_IDENT_XLSOU)) != NULL ||
4141 (rdp = dt_node_resolve(inp->din_root, DT_IDENT_XLPTR)) != NULL) {
4143 ctf_file_t *lctfp = dnp->dn_ctfp;
4144 ctf_id_t ltype = ctf_type_resolve(lctfp, dnp->dn_type);
4146 dt_xlator_t *dxp = rdp->di_data;
4147 ctf_file_t *rctfp = dxp->dx_dst_ctfp;
4148 ctf_id_t rtype = dxp->dx_dst_base;
4150 if (ctf_type_kind(lctfp, ltype) == CTF_K_POINTER) {
4151 ltype = ctf_type_reference(lctfp, ltype);
4152 ltype = ctf_type_resolve(lctfp, ltype);
4155 if (ctf_type_compat(lctfp, ltype, rctfp, rtype) == 0) {
4156 dnerror(dnp, D_OP_INCOMPAT,
4157 "inline %s definition uses incompatible types: "
4158 "\"%s\" = \"%s\"\n", dnp->dn_ident->di_name,
4159 dt_type_name(lctfp, ltype, n1, sizeof (n1)),
4160 dt_type_name(rctfp, rtype, n2, sizeof (n2)));
4163 } else if (dt_node_is_argcompat(dnp, inp->din_root) == 0) {
4164 dnerror(dnp, D_OP_INCOMPAT,
4165 "inline %s definition uses incompatible types: "
4166 "\"%s\" = \"%s\"\n", dnp->dn_ident->di_name,
4167 dt_node_type_name(dnp, n1, sizeof (n1)),
4168 dt_node_type_name(inp->din_root, n2, sizeof (n2)));
4175 dt_cook_member(dt_node_t *dnp, uint_t idflags)
4177 dnp->dn_membexpr = dt_node_cook(dnp->dn_membexpr, idflags);
4178 dt_node_attr_assign(dnp, dnp->dn_membexpr->dn_attr);
4184 dt_cook_xlator(dt_node_t *dnp, uint_t idflags)
4186 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
4187 dt_xlator_t *dxp = dnp->dn_xlator;
4190 char n1[DT_TYPE_NAMELEN];
4191 char n2[DT_TYPE_NAMELEN];
4193 dtrace_attribute_t attr = _dtrace_maxattr;
4197 * Before cooking each translator member, we push a reference to the
4198 * hash containing translator-local identifiers on to pcb_globals to
4199 * temporarily interpose these identifiers in front of other globals.
4201 dt_idstack_push(&yypcb->pcb_globals, dxp->dx_locals);
4203 for (mnp = dnp->dn_members; mnp != NULL; mnp = mnp->dn_list) {
4204 if (ctf_member_info(dxp->dx_dst_ctfp, dxp->dx_dst_type,
4205 mnp->dn_membname, &ctm) == CTF_ERR) {
4206 xyerror(D_XLATE_MEMB,
4207 "translator member %s is not a member of %s\n",
4208 mnp->dn_membname, ctf_type_name(dxp->dx_dst_ctfp,
4209 dxp->dx_dst_type, n1, sizeof (n1)));
4212 (void) dt_node_cook(mnp, DT_IDFLG_REF);
4213 dt_node_type_assign(mnp, dxp->dx_dst_ctfp, ctm.ctm_type);
4214 attr = dt_attr_min(attr, mnp->dn_attr);
4216 if (dt_node_is_argcompat(mnp, mnp->dn_membexpr) == 0) {
4217 xyerror(D_XLATE_INCOMPAT,
4218 "translator member %s definition uses "
4219 "incompatible types: \"%s\" = \"%s\"\n",
4221 dt_node_type_name(mnp, n1, sizeof (n1)),
4222 dt_node_type_name(mnp->dn_membexpr,
4227 dt_idstack_pop(&yypcb->pcb_globals, dxp->dx_locals);
4229 dxp->dx_souid.di_attr = attr;
4230 dxp->dx_ptrid.di_attr = attr;
4232 dt_node_type_assign(dnp, DT_DYN_CTFP(dtp), DT_DYN_TYPE(dtp));
4233 dt_node_attr_assign(dnp, _dtrace_defattr);
4239 dt_node_provider_cmp_argv(dt_provider_t *pvp, dt_node_t *pnp, const char *kind,
4240 uint_t old_argc, dt_node_t *old_argv, uint_t new_argc, dt_node_t *new_argv)
4242 dt_probe_t *prp = pnp->dn_ident->di_data;
4245 char n1[DT_TYPE_NAMELEN];
4246 char n2[DT_TYPE_NAMELEN];
4248 if (old_argc != new_argc) {
4249 dnerror(pnp, D_PROV_INCOMPAT,
4250 "probe %s:%s %s prototype mismatch:\n"
4251 "\t current: %u arg%s\n\tprevious: %u arg%s\n",
4252 pvp->pv_desc.dtvd_name, prp->pr_ident->di_name, kind,
4253 new_argc, new_argc != 1 ? "s" : "",
4254 old_argc, old_argc != 1 ? "s" : "");
4257 for (i = 0; i < old_argc; i++,
4258 old_argv = old_argv->dn_list, new_argv = new_argv->dn_list) {
4259 if (ctf_type_cmp(old_argv->dn_ctfp, old_argv->dn_type,
4260 new_argv->dn_ctfp, new_argv->dn_type) == 0)
4263 dnerror(pnp, D_PROV_INCOMPAT,
4264 "probe %s:%s %s prototype argument #%u mismatch:\n"
4265 "\t current: %s\n\tprevious: %s\n",
4266 pvp->pv_desc.dtvd_name, prp->pr_ident->di_name, kind, i + 1,
4267 dt_node_type_name(new_argv, n1, sizeof (n1)),
4268 dt_node_type_name(old_argv, n2, sizeof (n2)));
4273 * Compare a new probe declaration with an existing probe definition (either
4274 * from a previous declaration or cached from the kernel). If the existing
4275 * definition and declaration both have an input and output parameter list,
4276 * compare both lists. Otherwise compare only the output parameter lists.
4279 dt_node_provider_cmp(dt_provider_t *pvp, dt_node_t *pnp,
4280 dt_probe_t *old, dt_probe_t *new)
4282 dt_node_provider_cmp_argv(pvp, pnp, "output",
4283 old->pr_xargc, old->pr_xargs, new->pr_xargc, new->pr_xargs);
4285 if (old->pr_nargs != old->pr_xargs && new->pr_nargs != new->pr_xargs) {
4286 dt_node_provider_cmp_argv(pvp, pnp, "input",
4287 old->pr_nargc, old->pr_nargs, new->pr_nargc, new->pr_nargs);
4290 if (old->pr_nargs == old->pr_xargs && new->pr_nargs != new->pr_xargs) {
4291 if (pvp->pv_flags & DT_PROVIDER_IMPL) {
4292 dnerror(pnp, D_PROV_INCOMPAT,
4293 "provider interface mismatch: %s\n"
4294 "\t current: probe %s:%s has an output prototype\n"
4295 "\tprevious: probe %s:%s has no output prototype\n",
4296 pvp->pv_desc.dtvd_name, pvp->pv_desc.dtvd_name,
4297 new->pr_ident->di_name, pvp->pv_desc.dtvd_name,
4298 old->pr_ident->di_name);
4301 if (old->pr_ident->di_gen == yypcb->pcb_hdl->dt_gen)
4302 old->pr_ident->di_flags |= DT_IDFLG_ORPHAN;
4304 dt_idhash_delete(pvp->pv_probes, old->pr_ident);
4305 dt_probe_declare(pvp, new);
4310 dt_cook_probe(dt_node_t *dnp, dt_provider_t *pvp)
4312 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
4313 dt_probe_t *prp = dnp->dn_ident->di_data;
4318 char n1[DT_TYPE_NAMELEN];
4319 char n2[DT_TYPE_NAMELEN];
4321 if (prp->pr_nargs == prp->pr_xargs)
4324 for (i = 0; i < prp->pr_xargc; i++) {
4325 dt_node_t *xnp = prp->pr_xargv[i];
4326 dt_node_t *nnp = prp->pr_nargv[prp->pr_mapping[i]];
4328 if ((dxp = dt_xlator_lookup(dtp,
4329 nnp, xnp, DT_XLATE_FUZZY)) != NULL) {
4330 if (dt_provider_xref(dtp, pvp, dxp->dx_id) != 0)
4331 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
4335 if (dt_node_is_argcompat(nnp, xnp))
4336 continue; /* no translator defined and none required */
4338 dnerror(dnp, D_PROV_PRXLATOR, "translator for %s:%s output "
4339 "argument #%u from %s to %s is not defined\n",
4340 pvp->pv_desc.dtvd_name, dnp->dn_ident->di_name, i + 1,
4341 dt_node_type_name(nnp, n1, sizeof (n1)),
4342 dt_node_type_name(xnp, n2, sizeof (n2)));
4348 dt_cook_provider(dt_node_t *dnp, uint_t idflags)
4350 dt_provider_t *pvp = dnp->dn_provider;
4354 * If we're declaring a provider for the first time and it is unknown
4355 * to dtrace(7D), insert the probe definitions into the provider's hash.
4356 * If we're redeclaring a known provider, verify the interface matches.
4358 for (pnp = dnp->dn_probes; pnp != NULL; pnp = pnp->dn_list) {
4359 const char *probename = pnp->dn_ident->di_name;
4360 dt_probe_t *prp = dt_probe_lookup(pvp, probename);
4362 assert(pnp->dn_kind == DT_NODE_PROBE);
4364 if (prp != NULL && dnp->dn_provred) {
4365 dt_node_provider_cmp(pvp, pnp,
4366 prp, pnp->dn_ident->di_data);
4367 } else if (prp == NULL && dnp->dn_provred) {
4368 dnerror(pnp, D_PROV_INCOMPAT,
4369 "provider interface mismatch: %s\n"
4370 "\t current: probe %s:%s defined\n"
4371 "\tprevious: probe %s:%s not defined\n",
4372 dnp->dn_provname, dnp->dn_provname,
4373 probename, dnp->dn_provname, probename);
4374 } else if (prp != NULL) {
4375 dnerror(pnp, D_PROV_PRDUP, "probe redeclared: %s:%s\n",
4376 dnp->dn_provname, probename);
4378 dt_probe_declare(pvp, pnp->dn_ident->di_data);
4380 dt_cook_probe(pnp, pvp);
4388 dt_cook_none(dt_node_t *dnp, uint_t idflags)
4393 static dt_node_t *(*dt_cook_funcs[])(dt_node_t *, uint_t) = {
4394 dt_cook_none, /* DT_NODE_FREE */
4395 dt_cook_none, /* DT_NODE_INT */
4396 dt_cook_none, /* DT_NODE_STRING */
4397 dt_cook_ident, /* DT_NODE_IDENT */
4398 dt_cook_var, /* DT_NODE_VAR */
4399 dt_cook_none, /* DT_NODE_SYM */
4400 dt_cook_none, /* DT_NODE_TYPE */
4401 dt_cook_func, /* DT_NODE_FUNC */
4402 dt_cook_op1, /* DT_NODE_OP1 */
4403 dt_cook_op2, /* DT_NODE_OP2 */
4404 dt_cook_op3, /* DT_NODE_OP3 */
4405 dt_cook_statement, /* DT_NODE_DEXPR */
4406 dt_cook_statement, /* DT_NODE_DFUNC */
4407 dt_cook_aggregation, /* DT_NODE_AGG */
4408 dt_cook_none, /* DT_NODE_PDESC */
4409 dt_cook_clause, /* DT_NODE_CLAUSE */
4410 dt_cook_inline, /* DT_NODE_INLINE */
4411 dt_cook_member, /* DT_NODE_MEMBER */
4412 dt_cook_xlator, /* DT_NODE_XLATOR */
4413 dt_cook_none, /* DT_NODE_PROBE */
4414 dt_cook_provider, /* DT_NODE_PROVIDER */
4415 dt_cook_none /* DT_NODE_PROG */
4419 * Recursively cook the parse tree starting at the specified node. The idflags
4420 * parameter is used to indicate the type of reference (r/w) and is applied to
4421 * the resulting identifier if it is a D variable or D aggregation.
4424 dt_node_cook(dt_node_t *dnp, uint_t idflags)
4426 int oldlineno = yylineno;
4428 yylineno = dnp->dn_line;
4430 dnp = dt_cook_funcs[dnp->dn_kind](dnp, idflags);
4431 dnp->dn_flags |= DT_NF_COOKED;
4433 if (dnp->dn_kind == DT_NODE_VAR || dnp->dn_kind == DT_NODE_AGG)
4434 dnp->dn_ident->di_flags |= idflags;
4436 yylineno = oldlineno;
4441 dt_node_list_cook(dt_node_t **pnp, uint_t idflags)
4443 dtrace_attribute_t attr = _dtrace_defattr;
4444 dt_node_t *dnp, *nnp;
4446 for (dnp = (pnp != NULL ? *pnp : NULL); dnp != NULL; dnp = nnp) {
4448 dnp = *pnp = dt_node_cook(dnp, idflags);
4449 attr = dt_attr_min(attr, dnp->dn_attr);
4451 pnp = &dnp->dn_list;
4458 dt_node_list_free(dt_node_t **pnp)
4460 dt_node_t *dnp, *nnp;
4462 for (dnp = (pnp != NULL ? *pnp : NULL); dnp != NULL; dnp = nnp) {
4472 dt_node_link_free(dt_node_t **pnp)
4474 dt_node_t *dnp, *nnp;
4476 for (dnp = (pnp != NULL ? *pnp : NULL); dnp != NULL; dnp = nnp) {
4481 for (dnp = (pnp != NULL ? *pnp : NULL); dnp != NULL; dnp = nnp) {
4491 dt_node_link(dt_node_t *lp, dt_node_t *rp)
4497 else if (rp == NULL)
4500 for (dnp = lp; dnp->dn_list != NULL; dnp = dnp->dn_list)
4508 * Compute the DOF dtrace_diftype_t representation of a node's type. This is
4509 * called from a variety of places in the library so it cannot assume yypcb
4510 * is valid: any references to handle-specific data must be made through 'dtp'.
4513 dt_node_diftype(dtrace_hdl_t *dtp, const dt_node_t *dnp, dtrace_diftype_t *tp)
4515 if (dnp->dn_ctfp == DT_STR_CTFP(dtp) &&
4516 dnp->dn_type == DT_STR_TYPE(dtp)) {
4517 tp->dtdt_kind = DIF_TYPE_STRING;
4518 tp->dtdt_ckind = CTF_K_UNKNOWN;
4520 tp->dtdt_kind = DIF_TYPE_CTF;
4521 tp->dtdt_ckind = ctf_type_kind(dnp->dn_ctfp,
4522 ctf_type_resolve(dnp->dn_ctfp, dnp->dn_type));
4525 tp->dtdt_flags = (dnp->dn_flags & DT_NF_REF) ? DIF_TF_BYREF : 0;
4527 tp->dtdt_size = ctf_type_size(dnp->dn_ctfp, dnp->dn_type);
4531 dt_node_printr(dt_node_t *dnp, FILE *fp, int depth)
4533 char n[DT_TYPE_NAMELEN], buf[BUFSIZ], a[8];
4534 const dtrace_syminfo_t *dts;
4535 const dt_idnode_t *inp;
4538 (void) fprintf(fp, "%*s", depth * 2, "");
4539 (void) dt_attr_str(dnp->dn_attr, a, sizeof (a));
4541 if (dnp->dn_ctfp != NULL && dnp->dn_type != CTF_ERR &&
4542 ctf_type_name(dnp->dn_ctfp, dnp->dn_type, n, sizeof (n)) != NULL) {
4543 (void) snprintf(buf, BUFSIZ, "type=<%s> attr=%s flags=", n, a);
4545 (void) snprintf(buf, BUFSIZ, "type=<%ld> attr=%s flags=",
4549 if (dnp->dn_flags != 0) {
4551 if (dnp->dn_flags & DT_NF_SIGNED)
4552 (void) strcat(n, ",SIGN");
4553 if (dnp->dn_flags & DT_NF_COOKED)
4554 (void) strcat(n, ",COOK");
4555 if (dnp->dn_flags & DT_NF_REF)
4556 (void) strcat(n, ",REF");
4557 if (dnp->dn_flags & DT_NF_LVALUE)
4558 (void) strcat(n, ",LVAL");
4559 if (dnp->dn_flags & DT_NF_WRITABLE)
4560 (void) strcat(n, ",WRITE");
4561 if (dnp->dn_flags & DT_NF_BITFIELD)
4562 (void) strcat(n, ",BITF");
4563 if (dnp->dn_flags & DT_NF_USERLAND)
4564 (void) strcat(n, ",USER");
4565 (void) strcat(buf, n + 1);
4567 (void) strcat(buf, "0");
4569 switch (dnp->dn_kind) {
4571 (void) fprintf(fp, "FREE <node %p>\n", (void *)dnp);
4575 (void) fprintf(fp, "INT 0x%llx (%s)\n",
4576 (u_longlong_t)dnp->dn_value, buf);
4579 case DT_NODE_STRING:
4580 (void) fprintf(fp, "STRING \"%s\" (%s)\n", dnp->dn_string, buf);
4584 (void) fprintf(fp, "IDENT %s (%s)\n", dnp->dn_string, buf);
4588 (void) fprintf(fp, "VARIABLE %s%s (%s)\n",
4589 (dnp->dn_ident->di_flags & DT_IDFLG_LOCAL) ? "this->" :
4590 (dnp->dn_ident->di_flags & DT_IDFLG_TLS) ? "self->" : "",
4591 dnp->dn_ident->di_name, buf);
4593 if (dnp->dn_args != NULL)
4594 (void) fprintf(fp, "%*s[\n", depth * 2, "");
4596 for (arg = dnp->dn_args; arg != NULL; arg = arg->dn_list) {
4597 dt_node_printr(arg, fp, depth + 1);
4598 if (arg->dn_list != NULL)
4599 (void) fprintf(fp, "%*s,\n", depth * 2, "");
4602 if (dnp->dn_args != NULL)
4603 (void) fprintf(fp, "%*s]\n", depth * 2, "");
4607 dts = dnp->dn_ident->di_data;
4608 (void) fprintf(fp, "SYMBOL %s`%s (%s)\n",
4609 dts->dts_object, dts->dts_name, buf);
4613 if (dnp->dn_string != NULL) {
4614 (void) fprintf(fp, "TYPE (%s) %s\n",
4615 buf, dnp->dn_string);
4617 (void) fprintf(fp, "TYPE (%s)\n", buf);
4621 (void) fprintf(fp, "FUNC %s (%s)\n",
4622 dnp->dn_ident->di_name, buf);
4624 for (arg = dnp->dn_args; arg != NULL; arg = arg->dn_list) {
4625 dt_node_printr(arg, fp, depth + 1);
4626 if (arg->dn_list != NULL)
4627 (void) fprintf(fp, "%*s,\n", depth * 2, "");
4632 (void) fprintf(fp, "OP1 %s (%s)\n", opstr(dnp->dn_op), buf);
4633 dt_node_printr(dnp->dn_child, fp, depth + 1);
4637 (void) fprintf(fp, "OP2 %s (%s)\n", opstr(dnp->dn_op), buf);
4638 dt_node_printr(dnp->dn_left, fp, depth + 1);
4639 dt_node_printr(dnp->dn_right, fp, depth + 1);
4643 (void) fprintf(fp, "OP3 (%s)\n", buf);
4644 dt_node_printr(dnp->dn_expr, fp, depth + 1);
4645 (void) fprintf(fp, "%*s?\n", depth * 2, "");
4646 dt_node_printr(dnp->dn_left, fp, depth + 1);
4647 (void) fprintf(fp, "%*s:\n", depth * 2, "");
4648 dt_node_printr(dnp->dn_right, fp, depth + 1);
4653 (void) fprintf(fp, "D EXPRESSION attr=%s\n", a);
4654 dt_node_printr(dnp->dn_expr, fp, depth + 1);
4658 (void) fprintf(fp, "AGGREGATE @%s attr=%s [\n",
4659 dnp->dn_ident->di_name, a);
4661 for (arg = dnp->dn_aggtup; arg != NULL; arg = arg->dn_list) {
4662 dt_node_printr(arg, fp, depth + 1);
4663 if (arg->dn_list != NULL)
4664 (void) fprintf(fp, "%*s,\n", depth * 2, "");
4667 if (dnp->dn_aggfun) {
4668 (void) fprintf(fp, "%*s] = ", depth * 2, "");
4669 dt_node_printr(dnp->dn_aggfun, fp, depth + 1);
4671 (void) fprintf(fp, "%*s]\n", depth * 2, "");
4674 (void) fprintf(fp, "%*s)\n", depth * 2, "");
4678 (void) fprintf(fp, "PDESC %s:%s:%s:%s [%u]\n",
4679 dnp->dn_desc->dtpd_provider, dnp->dn_desc->dtpd_mod,
4680 dnp->dn_desc->dtpd_func, dnp->dn_desc->dtpd_name,
4681 dnp->dn_desc->dtpd_id);
4684 case DT_NODE_CLAUSE:
4685 (void) fprintf(fp, "CLAUSE attr=%s\n", a);
4687 for (arg = dnp->dn_pdescs; arg != NULL; arg = arg->dn_list)
4688 dt_node_printr(arg, fp, depth + 1);
4690 (void) fprintf(fp, "%*sCTXATTR %s\n", depth * 2, "",
4691 dt_attr_str(dnp->dn_ctxattr, a, sizeof (a)));
4693 if (dnp->dn_pred != NULL) {
4694 (void) fprintf(fp, "%*sPREDICATE /\n", depth * 2, "");
4695 dt_node_printr(dnp->dn_pred, fp, depth + 1);
4696 (void) fprintf(fp, "%*s/\n", depth * 2, "");
4699 for (arg = dnp->dn_acts; arg != NULL; arg = arg->dn_list)
4700 dt_node_printr(arg, fp, depth + 1);
4703 case DT_NODE_INLINE:
4704 inp = dnp->dn_ident->di_iarg;
4706 (void) fprintf(fp, "INLINE %s (%s)\n",
4707 dnp->dn_ident->di_name, buf);
4708 dt_node_printr(inp->din_root, fp, depth + 1);
4711 case DT_NODE_MEMBER:
4712 (void) fprintf(fp, "MEMBER %s (%s)\n", dnp->dn_membname, buf);
4713 if (dnp->dn_membexpr)
4714 dt_node_printr(dnp->dn_membexpr, fp, depth + 1);
4717 case DT_NODE_XLATOR:
4718 (void) fprintf(fp, "XLATOR (%s)", buf);
4720 if (ctf_type_name(dnp->dn_xlator->dx_src_ctfp,
4721 dnp->dn_xlator->dx_src_type, n, sizeof (n)) != NULL)
4722 (void) fprintf(fp, " from <%s>", n);
4724 if (ctf_type_name(dnp->dn_xlator->dx_dst_ctfp,
4725 dnp->dn_xlator->dx_dst_type, n, sizeof (n)) != NULL)
4726 (void) fprintf(fp, " to <%s>", n);
4728 (void) fprintf(fp, "\n");
4730 for (arg = dnp->dn_members; arg != NULL; arg = arg->dn_list)
4731 dt_node_printr(arg, fp, depth + 1);
4735 (void) fprintf(fp, "PROBE %s\n", dnp->dn_ident->di_name);
4738 case DT_NODE_PROVIDER:
4739 (void) fprintf(fp, "PROVIDER %s (%s)\n",
4740 dnp->dn_provname, dnp->dn_provred ? "redecl" : "decl");
4741 for (arg = dnp->dn_probes; arg != NULL; arg = arg->dn_list)
4742 dt_node_printr(arg, fp, depth + 1);
4746 (void) fprintf(fp, "PROGRAM attr=%s\n", a);
4747 for (arg = dnp->dn_list; arg != NULL; arg = arg->dn_list)
4748 dt_node_printr(arg, fp, depth + 1);
4752 (void) fprintf(fp, "<bad node %p, kind %d>\n",
4753 (void *)dnp, dnp->dn_kind);
4758 dt_node_root(dt_node_t *dnp)
4760 yypcb->pcb_root = dnp;
4766 dnerror(const dt_node_t *dnp, dt_errtag_t tag, const char *format, ...)
4768 int oldlineno = yylineno;
4771 yylineno = dnp->dn_line;
4773 va_start(ap, format);
4774 xyvwarn(tag, format, ap);
4777 yylineno = oldlineno;
4778 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
4783 dnwarn(const dt_node_t *dnp, dt_errtag_t tag, const char *format, ...)
4785 int oldlineno = yylineno;
4788 yylineno = dnp->dn_line;
4790 va_start(ap, format);
4791 xyvwarn(tag, format, ap);
4794 yylineno = oldlineno;
4799 xyerror(dt_errtag_t tag, const char *format, ...)
4803 va_start(ap, format);
4804 xyvwarn(tag, format, ap);
4807 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
4812 xywarn(dt_errtag_t tag, const char *format, ...)
4816 va_start(ap, format);
4817 xyvwarn(tag, format, ap);
4822 xyvwarn(dt_errtag_t tag, const char *format, va_list ap)
4825 return; /* compiler is not currently active: act as a no-op */
4827 dt_set_errmsg(yypcb->pcb_hdl, dt_errtag(tag), yypcb->pcb_region,
4828 yypcb->pcb_filetag, yypcb->pcb_fileptr ? yylineno : 0, format, ap);
4833 yyerror(const char *format, ...)
4837 va_start(ap, format);
4838 yyvwarn(format, ap);
4841 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
4846 yywarn(const char *format, ...)
4850 va_start(ap, format);
4851 yyvwarn(format, ap);
4856 yyvwarn(const char *format, va_list ap)
4859 return; /* compiler is not currently active: act as a no-op */
4861 dt_set_errmsg(yypcb->pcb_hdl, dt_errtag(D_SYNTAX), yypcb->pcb_region,
4862 yypcb->pcb_filetag, yypcb->pcb_fileptr ? yylineno : 0, format, ap);
4864 if (strchr(format, '\n') == NULL) {
4865 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
4866 size_t len = strlen(dtp->dt_errmsg);
4867 char *p, *s = dtp->dt_errmsg + len;
4868 size_t n = sizeof (dtp->dt_errmsg) - len;
4870 if (yytext[0] == '\0')
4871 (void) snprintf(s, n, " near end of input");
4872 else if (yytext[0] == '\n')
4873 (void) snprintf(s, n, " near end of line");
4875 if ((p = strchr(yytext, '\n')) != NULL)
4876 *p = '\0'; /* crop at newline */
4877 (void) snprintf(s, n, " near \"%s\"", yytext);
4883 yylabel(const char *label)
4885 dt_dprintf("set label to <%s>\n", label ? label : "NULL");
4886 yypcb->pcb_region = label;
4892 return (1); /* indicate that lex should return a zero token for EOF */