import less(1)
[unleashed/tickless.git] / usr / src / lib / libdtrace / common / dt_parser.c
blob223f966b56a405891a487dbbbdbd25b9c404da01
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
23 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2015, Joyent Inc. All rights reserved.
25 * Copyright (c) 2012, 2016 by Delphix. All rights reserved.
29 * DTrace D Language Parser
31 * The D Parser is a lex/yacc parser consisting of the lexer dt_lex.l, the
32 * parsing grammar dt_grammar.y, and this file, dt_parser.c, which handles
33 * the construction of the parse tree nodes and their syntactic validation.
34 * The parse tree is constructed of dt_node_t structures (see <dt_parser.h>)
35 * that are built in two passes: (1) the "create" pass, where the parse tree
36 * nodes are allocated by calls from the grammar to dt_node_*() subroutines,
37 * and (2) the "cook" pass, where nodes are coalesced, assigned D types, and
38 * validated according to the syntactic rules of the language.
40 * All node allocations are performed using dt_node_alloc(). All node frees
41 * during the parsing phase are performed by dt_node_free(), which frees node-
42 * internal state but does not actually free the nodes. All final node frees
43 * are done as part of the end of dt_compile() or as part of destroying
44 * persistent identifiers or translators which have embedded nodes.
46 * The dt_node_* routines that implement pass (1) may allocate new nodes. The
47 * dt_cook_* routines that implement pass (2) may *not* allocate new nodes.
48 * They may free existing nodes using dt_node_free(), but they may not actually
49 * deallocate any dt_node_t's. Currently dt_cook_op2() is an exception to this
50 * rule: see the comments therein for how this issue is resolved.
52 * The dt_cook_* routines are responsible for (at minimum) setting the final
53 * node type (dn_ctfp/dn_type) and attributes (dn_attr). If dn_ctfp/dn_type
54 * are set manually (i.e. not by one of the type assignment functions), then
55 * the DT_NF_COOKED flag must be set manually on the node.
57 * The cooking pass can be applied to the same parse tree more than once (used
58 * in the case of a comma-separated list of probe descriptions). As such, the
59 * cook routines must not perform any parse tree transformations which would
60 * be invalid if the tree were subsequently cooked using a different context.
62 * The dn_ctfp and dn_type fields form the type of the node. This tuple can
63 * take on the following set of values, which form our type invariants:
65 * 1. dn_ctfp = NULL, dn_type = CTF_ERR
67 * In this state, the node has unknown type and is not yet cooked. The
68 * DT_NF_COOKED flag is not yet set on the node.
70 * 2. dn_ctfp = DT_DYN_CTFP(dtp), dn_type = DT_DYN_TYPE(dtp)
72 * In this state, the node is a dynamic D type. This means that generic
73 * operations are not valid on this node and only code that knows how to
74 * examine the inner details of the node can operate on it. A <DYN> node
75 * must have dn_ident set to point to an identifier describing the object
76 * and its type. The DT_NF_REF flag is set for all nodes of type <DYN>.
77 * At present, the D compiler uses the <DYN> type for:
79 * - associative arrays that do not yet have a value type defined
80 * - translated data (i.e. the result of the xlate operator)
81 * - aggregations
83 * 3. dn_ctfp = DT_STR_CTFP(dtp), dn_type = DT_STR_TYPE(dtp)
85 * In this state, the node is of type D string. The string type is really
86 * a char[0] typedef, but requires special handling throughout the compiler.
88 * 4. dn_ctfp != NULL, dn_type = any other type ID
90 * In this state, the node is of some known D/CTF type. The normal libctf
91 * APIs can be used to learn more about the type name or structure. When
92 * the type is assigned, the DT_NF_SIGNED, DT_NF_REF, and DT_NF_BITFIELD
93 * flags cache the corresponding attributes of the underlying CTF type.
96 #include <sys/param.h>
97 #include <sys/sysmacros.h>
98 #include <limits.h>
99 #include <setjmp.h>
100 #include <strings.h>
101 #include <assert.h>
102 #include <alloca.h>
103 #include <stdlib.h>
104 #include <stdarg.h>
105 #include <stdio.h>
106 #include <errno.h>
107 #include <ctype.h>
109 #include <dt_impl.h>
110 #include <dt_grammar.h>
111 #include <dt_module.h>
112 #include <dt_provider.h>
113 #include <dt_string.h>
114 #include <dt_as.h>
116 dt_pcb_t *yypcb; /* current control block for parser */
117 dt_node_t *yypragma; /* lex token list for control lines */
118 char yyintprefix; /* int token macro prefix (+/-) */
119 char yyintsuffix[4]; /* int token suffix string [uU][lL] */
120 int yyintdecimal; /* int token format flag (1=decimal, 0=octal/hex) */
122 static const char *
123 opstr(int op)
125 switch (op) {
126 case DT_TOK_COMMA: return (",");
127 case DT_TOK_ELLIPSIS: return ("...");
128 case DT_TOK_ASGN: return ("=");
129 case DT_TOK_ADD_EQ: return ("+=");
130 case DT_TOK_SUB_EQ: return ("-=");
131 case DT_TOK_MUL_EQ: return ("*=");
132 case DT_TOK_DIV_EQ: return ("/=");
133 case DT_TOK_MOD_EQ: return ("%=");
134 case DT_TOK_AND_EQ: return ("&=");
135 case DT_TOK_XOR_EQ: return ("^=");
136 case DT_TOK_OR_EQ: return ("|=");
137 case DT_TOK_LSH_EQ: return ("<<=");
138 case DT_TOK_RSH_EQ: return (">>=");
139 case DT_TOK_QUESTION: return ("?");
140 case DT_TOK_COLON: return (":");
141 case DT_TOK_LOR: return ("||");
142 case DT_TOK_LXOR: return ("^^");
143 case DT_TOK_LAND: return ("&&");
144 case DT_TOK_BOR: return ("|");
145 case DT_TOK_XOR: return ("^");
146 case DT_TOK_BAND: return ("&");
147 case DT_TOK_EQU: return ("==");
148 case DT_TOK_NEQ: return ("!=");
149 case DT_TOK_LT: return ("<");
150 case DT_TOK_LE: return ("<=");
151 case DT_TOK_GT: return (">");
152 case DT_TOK_GE: return (">=");
153 case DT_TOK_LSH: return ("<<");
154 case DT_TOK_RSH: return (">>");
155 case DT_TOK_ADD: return ("+");
156 case DT_TOK_SUB: return ("-");
157 case DT_TOK_MUL: return ("*");
158 case DT_TOK_DIV: return ("/");
159 case DT_TOK_MOD: return ("%");
160 case DT_TOK_LNEG: return ("!");
161 case DT_TOK_BNEG: return ("~");
162 case DT_TOK_ADDADD: return ("++");
163 case DT_TOK_PREINC: return ("++");
164 case DT_TOK_POSTINC: return ("++");
165 case DT_TOK_SUBSUB: return ("--");
166 case DT_TOK_PREDEC: return ("--");
167 case DT_TOK_POSTDEC: return ("--");
168 case DT_TOK_IPOS: return ("+");
169 case DT_TOK_INEG: return ("-");
170 case DT_TOK_DEREF: return ("*");
171 case DT_TOK_ADDROF: return ("&");
172 case DT_TOK_OFFSETOF: return ("offsetof");
173 case DT_TOK_SIZEOF: return ("sizeof");
174 case DT_TOK_STRINGOF: return ("stringof");
175 case DT_TOK_XLATE: return ("xlate");
176 case DT_TOK_LPAR: return ("(");
177 case DT_TOK_RPAR: return (")");
178 case DT_TOK_LBRAC: return ("[");
179 case DT_TOK_RBRAC: return ("]");
180 case DT_TOK_PTR: return ("->");
181 case DT_TOK_DOT: return (".");
182 case DT_TOK_STRING: return ("<string>");
183 case DT_TOK_IDENT: return ("<ident>");
184 case DT_TOK_TNAME: return ("<type>");
185 case DT_TOK_INT: return ("<int>");
186 default: return ("<?>");
191 dt_type_lookup(const char *s, dtrace_typeinfo_t *tip)
193 static const char delimiters[] = " \t\n\r\v\f*`";
194 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
195 const char *p, *q, *r, *end, *obj;
197 for (p = s, end = s + strlen(s); *p != '\0'; p = q) {
198 while (isspace(*p))
199 p++; /* skip leading whitespace prior to token */
201 if (p == end || (q = strpbrk(p + 1, delimiters)) == NULL)
202 break; /* empty string or single token remaining */
204 if (*q == '`') {
205 char *object = alloca((size_t)(q - p) + 1);
206 char *type = alloca((size_t)(end - s) + 1);
209 * Copy from the start of the token (p) to the location
210 * backquote (q) to extract the nul-terminated object.
212 bcopy(p, object, (size_t)(q - p));
213 object[(size_t)(q - p)] = '\0';
216 * Copy the original string up to the start of this
217 * token (p) into type, and then concatenate everything
218 * after q. This is the type name without the object.
220 bcopy(s, type, (size_t)(p - s));
221 bcopy(q + 1, type + (size_t)(p - s), strlen(q + 1) + 1);
224 * There may be at most three delimeters. The second
225 * delimeter is usually used to distinguish the type
226 * within a given module, however, there could be a link
227 * map id on the scene in which case that delimeter
228 * would be the third. We determine presence of the lmid
229 * if it rouglhly meets the from LM[0-9]
231 if ((r = strchr(q + 1, '`')) != NULL &&
232 ((r = strchr(r + 1, '`')) != NULL)) {
233 if (strchr(r + 1, '`') != NULL)
234 return (dt_set_errno(dtp,
235 EDT_BADSCOPE));
236 if (q[1] != 'L' || q[2] != 'M')
237 return (dt_set_errno(dtp,
238 EDT_BADSCOPE));
241 return (dtrace_lookup_by_type(dtp, object, type, tip));
245 if (yypcb->pcb_idepth != 0)
246 obj = DTRACE_OBJ_CDEFS;
247 else
248 obj = DTRACE_OBJ_EVERY;
250 return (dtrace_lookup_by_type(dtp, obj, s, tip));
254 * When we parse type expressions or parse an expression with unary "&", we
255 * need to find a type that is a pointer to a previously known type.
256 * Unfortunately CTF is limited to a per-container view, so ctf_type_pointer()
257 * alone does not suffice for our needs. We provide a more intelligent wrapper
258 * for the compiler that attempts to compute a pointer to either the given type
259 * or its base (that is, we try both "foo_t *" and "struct foo *"), and also
260 * to potentially construct the required type on-the-fly.
263 dt_type_pointer(dtrace_typeinfo_t *tip)
265 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
266 ctf_file_t *ctfp = tip->dtt_ctfp;
267 ctf_id_t type = tip->dtt_type;
268 ctf_id_t base = ctf_type_resolve(ctfp, type);
269 uint_t bflags = tip->dtt_flags;
271 dt_module_t *dmp;
272 ctf_id_t ptr;
274 if ((ptr = ctf_type_pointer(ctfp, type)) != CTF_ERR ||
275 (ptr = ctf_type_pointer(ctfp, base)) != CTF_ERR) {
276 tip->dtt_type = ptr;
277 return (0);
280 if (yypcb->pcb_idepth != 0)
281 dmp = dtp->dt_cdefs;
282 else
283 dmp = dtp->dt_ddefs;
285 if (ctfp != dmp->dm_ctfp && ctfp != ctf_parent_file(dmp->dm_ctfp) &&
286 (type = ctf_add_type(dmp->dm_ctfp, ctfp, type)) == CTF_ERR) {
287 dtp->dt_ctferr = ctf_errno(dmp->dm_ctfp);
288 return (dt_set_errno(dtp, EDT_CTF));
291 ptr = ctf_add_pointer(dmp->dm_ctfp, CTF_ADD_ROOT, NULL, type);
293 if (ptr == CTF_ERR || ctf_update(dmp->dm_ctfp) == CTF_ERR) {
294 dtp->dt_ctferr = ctf_errno(dmp->dm_ctfp);
295 return (dt_set_errno(dtp, EDT_CTF));
298 tip->dtt_object = dmp->dm_name;
299 tip->dtt_ctfp = dmp->dm_ctfp;
300 tip->dtt_type = ptr;
301 tip->dtt_flags = bflags;
303 return (0);
306 const char *
307 dt_type_name(ctf_file_t *ctfp, ctf_id_t type, char *buf, size_t len)
309 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
311 if (ctfp == DT_FPTR_CTFP(dtp) && type == DT_FPTR_TYPE(dtp))
312 (void) snprintf(buf, len, "function pointer");
313 else if (ctfp == DT_FUNC_CTFP(dtp) && type == DT_FUNC_TYPE(dtp))
314 (void) snprintf(buf, len, "function");
315 else if (ctfp == DT_DYN_CTFP(dtp) && type == DT_DYN_TYPE(dtp))
316 (void) snprintf(buf, len, "dynamic variable");
317 else if (ctfp == NULL)
318 (void) snprintf(buf, len, "<none>");
319 else if (ctf_type_name(ctfp, type, buf, len) == NULL)
320 (void) snprintf(buf, len, "unknown");
322 return (buf);
326 * Perform the "usual arithmetic conversions" to determine which of the two
327 * input operand types should be promoted and used as a result type. The
328 * rules for this are described in ISOC[6.3.1.8] and K&R[A6.5].
330 static void
331 dt_type_promote(dt_node_t *lp, dt_node_t *rp, ctf_file_t **ofp, ctf_id_t *otype)
333 ctf_file_t *lfp = lp->dn_ctfp;
334 ctf_id_t ltype = lp->dn_type;
336 ctf_file_t *rfp = rp->dn_ctfp;
337 ctf_id_t rtype = rp->dn_type;
339 ctf_id_t lbase = ctf_type_resolve(lfp, ltype);
340 uint_t lkind = ctf_type_kind(lfp, lbase);
342 ctf_id_t rbase = ctf_type_resolve(rfp, rtype);
343 uint_t rkind = ctf_type_kind(rfp, rbase);
345 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
346 ctf_encoding_t le, re;
347 uint_t lrank, rrank;
349 assert(lkind == CTF_K_INTEGER || lkind == CTF_K_ENUM);
350 assert(rkind == CTF_K_INTEGER || rkind == CTF_K_ENUM);
352 if (lkind == CTF_K_ENUM) {
353 lfp = DT_INT_CTFP(dtp);
354 ltype = lbase = DT_INT_TYPE(dtp);
357 if (rkind == CTF_K_ENUM) {
358 rfp = DT_INT_CTFP(dtp);
359 rtype = rbase = DT_INT_TYPE(dtp);
362 if (ctf_type_encoding(lfp, lbase, &le) == CTF_ERR) {
363 yypcb->pcb_hdl->dt_ctferr = ctf_errno(lfp);
364 longjmp(yypcb->pcb_jmpbuf, EDT_CTF);
367 if (ctf_type_encoding(rfp, rbase, &re) == CTF_ERR) {
368 yypcb->pcb_hdl->dt_ctferr = ctf_errno(rfp);
369 longjmp(yypcb->pcb_jmpbuf, EDT_CTF);
373 * Compute an integer rank based on the size and unsigned status.
374 * If rank is identical, pick the "larger" of the equivalent types
375 * which we define as having a larger base ctf_id_t. If rank is
376 * different, pick the type with the greater rank.
378 lrank = le.cte_bits + ((le.cte_format & CTF_INT_SIGNED) == 0);
379 rrank = re.cte_bits + ((re.cte_format & CTF_INT_SIGNED) == 0);
381 if (lrank == rrank) {
382 if (lbase - rbase < 0)
383 goto return_rtype;
384 else
385 goto return_ltype;
386 } else if (lrank > rrank) {
387 goto return_ltype;
388 } else
389 goto return_rtype;
391 return_ltype:
392 *ofp = lfp;
393 *otype = ltype;
394 return;
396 return_rtype:
397 *ofp = rfp;
398 *otype = rtype;
401 void
402 dt_node_promote(dt_node_t *lp, dt_node_t *rp, dt_node_t *dnp)
404 dt_type_promote(lp, rp, &dnp->dn_ctfp, &dnp->dn_type);
405 dt_node_type_assign(dnp, dnp->dn_ctfp, dnp->dn_type, B_FALSE);
406 dt_node_attr_assign(dnp, dt_attr_min(lp->dn_attr, rp->dn_attr));
409 const char *
410 dt_node_name(const dt_node_t *dnp, char *buf, size_t len)
412 char n1[DT_TYPE_NAMELEN];
413 char n2[DT_TYPE_NAMELEN];
415 const char *prefix = "", *suffix = "";
416 const dtrace_syminfo_t *dts;
417 char *s;
419 switch (dnp->dn_kind) {
420 case DT_NODE_INT:
421 (void) snprintf(buf, len, "integer constant 0x%llx",
422 (u_longlong_t)dnp->dn_value);
423 break;
424 case DT_NODE_STRING:
425 s = strchr2esc(dnp->dn_string, strlen(dnp->dn_string));
426 (void) snprintf(buf, len, "string constant \"%s\"",
427 s != NULL ? s : dnp->dn_string);
428 free(s);
429 break;
430 case DT_NODE_IDENT:
431 (void) snprintf(buf, len, "identifier %s", dnp->dn_string);
432 break;
433 case DT_NODE_VAR:
434 case DT_NODE_FUNC:
435 case DT_NODE_AGG:
436 case DT_NODE_INLINE:
437 switch (dnp->dn_ident->di_kind) {
438 case DT_IDENT_FUNC:
439 case DT_IDENT_AGGFUNC:
440 case DT_IDENT_ACTFUNC:
441 suffix = "( )";
442 break;
443 case DT_IDENT_AGG:
444 prefix = "@";
445 break;
447 (void) snprintf(buf, len, "%s %s%s%s",
448 dt_idkind_name(dnp->dn_ident->di_kind),
449 prefix, dnp->dn_ident->di_name, suffix);
450 break;
451 case DT_NODE_SYM:
452 dts = dnp->dn_ident->di_data;
453 (void) snprintf(buf, len, "symbol %s`%s",
454 dts->dts_object, dts->dts_name);
455 break;
456 case DT_NODE_TYPE:
457 (void) snprintf(buf, len, "type %s",
458 dt_node_type_name(dnp, n1, sizeof (n1)));
459 break;
460 case DT_NODE_OP1:
461 case DT_NODE_OP2:
462 case DT_NODE_OP3:
463 (void) snprintf(buf, len, "operator %s", opstr(dnp->dn_op));
464 break;
465 case DT_NODE_DEXPR:
466 case DT_NODE_DFUNC:
467 if (dnp->dn_expr)
468 return (dt_node_name(dnp->dn_expr, buf, len));
469 (void) snprintf(buf, len, "%s", "statement");
470 break;
471 case DT_NODE_PDESC:
472 if (dnp->dn_desc->dtpd_id == 0) {
473 (void) snprintf(buf, len,
474 "probe description %s:%s:%s:%s",
475 dnp->dn_desc->dtpd_provider, dnp->dn_desc->dtpd_mod,
476 dnp->dn_desc->dtpd_func, dnp->dn_desc->dtpd_name);
477 } else {
478 (void) snprintf(buf, len, "probe description %u",
479 dnp->dn_desc->dtpd_id);
481 break;
482 case DT_NODE_CLAUSE:
483 (void) snprintf(buf, len, "%s", "clause");
484 break;
485 case DT_NODE_MEMBER:
486 (void) snprintf(buf, len, "member %s", dnp->dn_membname);
487 break;
488 case DT_NODE_XLATOR:
489 (void) snprintf(buf, len, "translator <%s> (%s)",
490 dt_type_name(dnp->dn_xlator->dx_dst_ctfp,
491 dnp->dn_xlator->dx_dst_type, n1, sizeof (n1)),
492 dt_type_name(dnp->dn_xlator->dx_src_ctfp,
493 dnp->dn_xlator->dx_src_type, n2, sizeof (n2)));
494 break;
495 case DT_NODE_PROG:
496 (void) snprintf(buf, len, "%s", "program");
497 break;
498 default:
499 (void) snprintf(buf, len, "node <%u>", dnp->dn_kind);
500 break;
503 return (buf);
507 * dt_node_xalloc() can be used to create new parse nodes from any libdtrace
508 * caller. The caller is responsible for assigning dn_link appropriately.
510 dt_node_t *
511 dt_node_xalloc(dtrace_hdl_t *dtp, int kind)
513 dt_node_t *dnp = dt_alloc(dtp, sizeof (dt_node_t));
515 if (dnp == NULL)
516 return (NULL);
518 dnp->dn_ctfp = NULL;
519 dnp->dn_type = CTF_ERR;
520 dnp->dn_kind = (uchar_t)kind;
521 dnp->dn_flags = 0;
522 dnp->dn_op = 0;
523 dnp->dn_line = -1;
524 dnp->dn_reg = -1;
525 dnp->dn_attr = _dtrace_defattr;
526 dnp->dn_list = NULL;
527 dnp->dn_link = NULL;
528 bzero(&dnp->dn_u, sizeof (dnp->dn_u));
530 return (dnp);
534 * dt_node_alloc() is used to create new parse nodes from the parser. It
535 * assigns the node location based on the current lexer line number and places
536 * the new node on the default allocation list. If allocation fails, we
537 * automatically longjmp the caller back to the enclosing compilation call.
539 static dt_node_t *
540 dt_node_alloc(int kind)
542 dt_node_t *dnp = dt_node_xalloc(yypcb->pcb_hdl, kind);
544 if (dnp == NULL)
545 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
547 dnp->dn_line = yylineno;
548 dnp->dn_link = yypcb->pcb_list;
549 yypcb->pcb_list = dnp;
551 return (dnp);
554 void
555 dt_node_free(dt_node_t *dnp)
557 uchar_t kind = dnp->dn_kind;
559 dnp->dn_kind = DT_NODE_FREE;
561 switch (kind) {
562 case DT_NODE_STRING:
563 case DT_NODE_IDENT:
564 case DT_NODE_TYPE:
565 free(dnp->dn_string);
566 dnp->dn_string = NULL;
567 break;
569 case DT_NODE_VAR:
570 case DT_NODE_FUNC:
571 case DT_NODE_PROBE:
572 if (dnp->dn_ident != NULL) {
573 if (dnp->dn_ident->di_flags & DT_IDFLG_ORPHAN)
574 dt_ident_destroy(dnp->dn_ident);
575 dnp->dn_ident = NULL;
577 dt_node_list_free(&dnp->dn_args);
578 break;
580 case DT_NODE_OP1:
581 if (dnp->dn_child != NULL) {
582 dt_node_free(dnp->dn_child);
583 dnp->dn_child = NULL;
585 break;
587 case DT_NODE_OP3:
588 if (dnp->dn_expr != NULL) {
589 dt_node_free(dnp->dn_expr);
590 dnp->dn_expr = NULL;
592 /*FALLTHRU*/
593 case DT_NODE_OP2:
594 if (dnp->dn_left != NULL) {
595 dt_node_free(dnp->dn_left);
596 dnp->dn_left = NULL;
598 if (dnp->dn_right != NULL) {
599 dt_node_free(dnp->dn_right);
600 dnp->dn_right = NULL;
602 break;
604 case DT_NODE_DEXPR:
605 case DT_NODE_DFUNC:
606 if (dnp->dn_expr != NULL) {
607 dt_node_free(dnp->dn_expr);
608 dnp->dn_expr = NULL;
610 break;
612 case DT_NODE_AGG:
613 if (dnp->dn_aggfun != NULL) {
614 dt_node_free(dnp->dn_aggfun);
615 dnp->dn_aggfun = NULL;
617 dt_node_list_free(&dnp->dn_aggtup);
618 break;
620 case DT_NODE_PDESC:
621 free(dnp->dn_spec);
622 dnp->dn_spec = NULL;
623 free(dnp->dn_desc);
624 dnp->dn_desc = NULL;
625 break;
627 case DT_NODE_CLAUSE:
628 if (dnp->dn_pred != NULL)
629 dt_node_free(dnp->dn_pred);
630 if (dnp->dn_locals != NULL)
631 dt_idhash_destroy(dnp->dn_locals);
632 dt_node_list_free(&dnp->dn_pdescs);
633 dt_node_list_free(&dnp->dn_acts);
634 break;
636 case DT_NODE_MEMBER:
637 free(dnp->dn_membname);
638 dnp->dn_membname = NULL;
639 if (dnp->dn_membexpr != NULL) {
640 dt_node_free(dnp->dn_membexpr);
641 dnp->dn_membexpr = NULL;
643 break;
645 case DT_NODE_PROVIDER:
646 dt_node_list_free(&dnp->dn_probes);
647 free(dnp->dn_provname);
648 dnp->dn_provname = NULL;
649 break;
651 case DT_NODE_PROG:
652 dt_node_list_free(&dnp->dn_list);
653 break;
657 void
658 dt_node_attr_assign(dt_node_t *dnp, dtrace_attribute_t attr)
660 if ((yypcb->pcb_cflags & DTRACE_C_EATTR) &&
661 (dt_attr_cmp(attr, yypcb->pcb_amin) < 0)) {
662 char a[DTRACE_ATTR2STR_MAX];
663 char s[BUFSIZ];
665 dnerror(dnp, D_ATTR_MIN, "attributes for %s (%s) are less than "
666 "predefined minimum\n", dt_node_name(dnp, s, sizeof (s)),
667 dtrace_attr2str(attr, a, sizeof (a)));
670 dnp->dn_attr = attr;
673 void
674 dt_node_type_assign(dt_node_t *dnp, ctf_file_t *fp, ctf_id_t type,
675 boolean_t user)
677 ctf_id_t base = ctf_type_resolve(fp, type);
678 uint_t kind = ctf_type_kind(fp, base);
679 ctf_encoding_t e;
681 dnp->dn_flags &=
682 ~(DT_NF_SIGNED | DT_NF_REF | DT_NF_BITFIELD | DT_NF_USERLAND);
684 if (kind == CTF_K_INTEGER && ctf_type_encoding(fp, base, &e) == 0) {
685 size_t size = e.cte_bits / NBBY;
687 if (size > 8 || (e.cte_bits % NBBY) != 0 || (size & (size - 1)))
688 dnp->dn_flags |= DT_NF_BITFIELD;
690 if (e.cte_format & CTF_INT_SIGNED)
691 dnp->dn_flags |= DT_NF_SIGNED;
694 if (kind == CTF_K_FLOAT && ctf_type_encoding(fp, base, &e) == 0) {
695 if (e.cte_bits / NBBY > sizeof (uint64_t))
696 dnp->dn_flags |= DT_NF_REF;
699 if (kind == CTF_K_STRUCT || kind == CTF_K_UNION ||
700 kind == CTF_K_FORWARD ||
701 kind == CTF_K_ARRAY || kind == CTF_K_FUNCTION)
702 dnp->dn_flags |= DT_NF_REF;
703 else if (yypcb != NULL && fp == DT_DYN_CTFP(yypcb->pcb_hdl) &&
704 type == DT_DYN_TYPE(yypcb->pcb_hdl))
705 dnp->dn_flags |= DT_NF_REF;
707 if (user)
708 dnp->dn_flags |= DT_NF_USERLAND;
710 dnp->dn_flags |= DT_NF_COOKED;
711 dnp->dn_ctfp = fp;
712 dnp->dn_type = type;
715 void
716 dt_node_type_propagate(const dt_node_t *src, dt_node_t *dst)
718 assert(src->dn_flags & DT_NF_COOKED);
719 dst->dn_flags = src->dn_flags & ~DT_NF_LVALUE;
720 dst->dn_ctfp = src->dn_ctfp;
721 dst->dn_type = src->dn_type;
724 const char *
725 dt_node_type_name(const dt_node_t *dnp, char *buf, size_t len)
727 if (dt_node_is_dynamic(dnp) && dnp->dn_ident != NULL) {
728 (void) snprintf(buf, len, "%s",
729 dt_idkind_name(dt_ident_resolve(dnp->dn_ident)->di_kind));
730 return (buf);
733 if (dnp->dn_flags & DT_NF_USERLAND) {
734 size_t n = snprintf(buf, len, "userland ");
735 len = len > n ? len - n : 0;
736 (void) dt_type_name(dnp->dn_ctfp, dnp->dn_type, buf + n, len);
737 return (buf);
740 return (dt_type_name(dnp->dn_ctfp, dnp->dn_type, buf, len));
743 size_t
744 dt_node_type_size(const dt_node_t *dnp)
746 ctf_id_t base;
747 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
749 if (dnp->dn_kind == DT_NODE_STRING)
750 return (strlen(dnp->dn_string) + 1);
752 if (dt_node_is_dynamic(dnp) && dnp->dn_ident != NULL)
753 return (dt_ident_size(dnp->dn_ident));
755 base = ctf_type_resolve(dnp->dn_ctfp, dnp->dn_type);
757 if (ctf_type_kind(dnp->dn_ctfp, base) == CTF_K_FORWARD)
758 return (0);
761 * Here we have a 32-bit user pointer that is being used with a 64-bit
762 * kernel. When we're using it and its tagged as a userland reference --
763 * then we need to keep it as a 32-bit pointer. However, if we are
764 * referring to it as a kernel address, eg. being used after a copyin()
765 * then we need to make sure that we actually return the kernel's size
766 * of a pointer, 8 bytes.
768 if (ctf_type_kind(dnp->dn_ctfp, base) == CTF_K_POINTER &&
769 ctf_getmodel(dnp->dn_ctfp) == CTF_MODEL_ILP32 &&
770 !(dnp->dn_flags & DT_NF_USERLAND) &&
771 dtp->dt_conf.dtc_ctfmodel == CTF_MODEL_LP64)
772 return (8);
774 return (ctf_type_size(dnp->dn_ctfp, dnp->dn_type));
778 * Determine if the specified parse tree node references an identifier of the
779 * specified kind, and if so return a pointer to it; otherwise return NULL.
780 * This function resolves the identifier itself, following through any inlines.
782 dt_ident_t *
783 dt_node_resolve(const dt_node_t *dnp, uint_t idkind)
785 dt_ident_t *idp;
787 switch (dnp->dn_kind) {
788 case DT_NODE_VAR:
789 case DT_NODE_SYM:
790 case DT_NODE_FUNC:
791 case DT_NODE_AGG:
792 case DT_NODE_INLINE:
793 case DT_NODE_PROBE:
794 idp = dt_ident_resolve(dnp->dn_ident);
795 return (idp->di_kind == idkind ? idp : NULL);
798 if (dt_node_is_dynamic(dnp)) {
799 idp = dt_ident_resolve(dnp->dn_ident);
800 return (idp->di_kind == idkind ? idp : NULL);
803 return (NULL);
806 size_t
807 dt_node_sizeof(const dt_node_t *dnp)
809 dtrace_syminfo_t *sip;
810 GElf_Sym sym;
811 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
814 * The size of the node as used for the sizeof() operator depends on
815 * the kind of the node. If the node is a SYM, the size is obtained
816 * from the symbol table; if it is not a SYM, the size is determined
817 * from the node's type. This is slightly different from C's sizeof()
818 * operator in that (for example) when applied to a function, sizeof()
819 * will evaluate to the length of the function rather than the size of
820 * the function type.
822 if (dnp->dn_kind != DT_NODE_SYM)
823 return (dt_node_type_size(dnp));
825 sip = dnp->dn_ident->di_data;
827 if (dtrace_lookup_by_name(dtp, sip->dts_object,
828 sip->dts_name, &sym, NULL) == -1)
829 return (0);
831 return (sym.st_size);
835 dt_node_is_integer(const dt_node_t *dnp)
837 ctf_file_t *fp = dnp->dn_ctfp;
838 ctf_encoding_t e;
839 ctf_id_t type;
840 uint_t kind;
842 assert(dnp->dn_flags & DT_NF_COOKED);
844 type = ctf_type_resolve(fp, dnp->dn_type);
845 kind = ctf_type_kind(fp, type);
847 if (kind == CTF_K_INTEGER &&
848 ctf_type_encoding(fp, type, &e) == 0 && IS_VOID(e))
849 return (0); /* void integer */
851 return (kind == CTF_K_INTEGER || kind == CTF_K_ENUM);
855 dt_node_is_float(const dt_node_t *dnp)
857 ctf_file_t *fp = dnp->dn_ctfp;
858 ctf_encoding_t e;
859 ctf_id_t type;
860 uint_t kind;
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 return (kind == CTF_K_FLOAT &&
868 ctf_type_encoding(dnp->dn_ctfp, type, &e) == 0 && (
869 e.cte_format == CTF_FP_SINGLE || e.cte_format == CTF_FP_DOUBLE ||
870 e.cte_format == CTF_FP_LDOUBLE));
874 dt_node_is_scalar(const dt_node_t *dnp)
876 ctf_file_t *fp = dnp->dn_ctfp;
877 ctf_encoding_t e;
878 ctf_id_t type;
879 uint_t kind;
881 assert(dnp->dn_flags & DT_NF_COOKED);
883 type = ctf_type_resolve(fp, dnp->dn_type);
884 kind = ctf_type_kind(fp, type);
886 if (kind == CTF_K_INTEGER &&
887 ctf_type_encoding(fp, type, &e) == 0 && IS_VOID(e))
888 return (0); /* void cannot be used as a scalar */
890 return (kind == CTF_K_INTEGER || kind == CTF_K_ENUM ||
891 kind == CTF_K_POINTER);
895 dt_node_is_arith(const dt_node_t *dnp)
897 ctf_file_t *fp = dnp->dn_ctfp;
898 ctf_encoding_t e;
899 ctf_id_t type;
900 uint_t kind;
902 assert(dnp->dn_flags & DT_NF_COOKED);
904 type = ctf_type_resolve(fp, dnp->dn_type);
905 kind = ctf_type_kind(fp, type);
907 if (kind == CTF_K_INTEGER)
908 return (ctf_type_encoding(fp, type, &e) == 0 && !IS_VOID(e));
909 else
910 return (kind == CTF_K_ENUM);
914 dt_node_is_vfptr(const dt_node_t *dnp)
916 ctf_file_t *fp = dnp->dn_ctfp;
917 ctf_encoding_t e;
918 ctf_id_t type;
919 uint_t kind;
921 assert(dnp->dn_flags & DT_NF_COOKED);
923 type = ctf_type_resolve(fp, dnp->dn_type);
924 if (ctf_type_kind(fp, type) != CTF_K_POINTER)
925 return (0); /* type is not a pointer */
927 type = ctf_type_resolve(fp, ctf_type_reference(fp, type));
928 kind = ctf_type_kind(fp, type);
930 return (kind == CTF_K_FUNCTION || (kind == CTF_K_INTEGER &&
931 ctf_type_encoding(fp, type, &e) == 0 && IS_VOID(e)));
935 dt_node_is_dynamic(const dt_node_t *dnp)
937 if (dnp->dn_kind == DT_NODE_VAR &&
938 (dnp->dn_ident->di_flags & DT_IDFLG_INLINE)) {
939 const dt_idnode_t *inp = dnp->dn_ident->di_iarg;
940 return (inp->din_root ? dt_node_is_dynamic(inp->din_root) : 0);
943 return (dnp->dn_ctfp == DT_DYN_CTFP(yypcb->pcb_hdl) &&
944 dnp->dn_type == DT_DYN_TYPE(yypcb->pcb_hdl));
948 dt_node_is_string(const dt_node_t *dnp)
950 return (dnp->dn_ctfp == DT_STR_CTFP(yypcb->pcb_hdl) &&
951 dnp->dn_type == DT_STR_TYPE(yypcb->pcb_hdl));
955 dt_node_is_stack(const dt_node_t *dnp)
957 return (dnp->dn_ctfp == DT_STACK_CTFP(yypcb->pcb_hdl) &&
958 dnp->dn_type == DT_STACK_TYPE(yypcb->pcb_hdl));
962 dt_node_is_symaddr(const dt_node_t *dnp)
964 return (dnp->dn_ctfp == DT_SYMADDR_CTFP(yypcb->pcb_hdl) &&
965 dnp->dn_type == DT_SYMADDR_TYPE(yypcb->pcb_hdl));
969 dt_node_is_usymaddr(const dt_node_t *dnp)
971 return (dnp->dn_ctfp == DT_USYMADDR_CTFP(yypcb->pcb_hdl) &&
972 dnp->dn_type == DT_USYMADDR_TYPE(yypcb->pcb_hdl));
976 dt_node_is_strcompat(const dt_node_t *dnp)
978 ctf_file_t *fp = dnp->dn_ctfp;
979 ctf_encoding_t e;
980 ctf_arinfo_t r;
981 ctf_id_t base;
982 uint_t kind;
984 assert(dnp->dn_flags & DT_NF_COOKED);
986 base = ctf_type_resolve(fp, dnp->dn_type);
987 kind = ctf_type_kind(fp, base);
989 if (kind == CTF_K_POINTER &&
990 (base = ctf_type_reference(fp, base)) != CTF_ERR &&
991 (base = ctf_type_resolve(fp, base)) != CTF_ERR &&
992 ctf_type_encoding(fp, base, &e) == 0 && IS_CHAR(e))
993 return (1); /* promote char pointer to string */
995 if (kind == CTF_K_ARRAY && ctf_array_info(fp, base, &r) == 0 &&
996 (base = ctf_type_resolve(fp, r.ctr_contents)) != CTF_ERR &&
997 ctf_type_encoding(fp, base, &e) == 0 && IS_CHAR(e))
998 return (1); /* promote char array to string */
1000 return (0);
1004 dt_node_is_pointer(const dt_node_t *dnp)
1006 ctf_file_t *fp = dnp->dn_ctfp;
1007 uint_t kind;
1009 assert(dnp->dn_flags & DT_NF_COOKED);
1011 if (dt_node_is_string(dnp))
1012 return (0); /* string are pass-by-ref but act like structs */
1014 kind = ctf_type_kind(fp, ctf_type_resolve(fp, dnp->dn_type));
1015 return (kind == CTF_K_POINTER || kind == CTF_K_ARRAY);
1019 dt_node_is_void(const dt_node_t *dnp)
1021 ctf_file_t *fp = dnp->dn_ctfp;
1022 ctf_encoding_t e;
1023 ctf_id_t type;
1025 if (dt_node_is_dynamic(dnp))
1026 return (0); /* <DYN> is an alias for void but not the same */
1028 if (dt_node_is_stack(dnp))
1029 return (0);
1031 if (dt_node_is_symaddr(dnp) || dt_node_is_usymaddr(dnp))
1032 return (0);
1034 type = ctf_type_resolve(fp, dnp->dn_type);
1036 return (ctf_type_kind(fp, type) == CTF_K_INTEGER &&
1037 ctf_type_encoding(fp, type, &e) == 0 && IS_VOID(e));
1041 dt_node_is_ptrcompat(const dt_node_t *lp, const dt_node_t *rp,
1042 ctf_file_t **fpp, ctf_id_t *tp)
1044 ctf_file_t *lfp = lp->dn_ctfp;
1045 ctf_file_t *rfp = rp->dn_ctfp;
1047 ctf_id_t lbase = CTF_ERR, rbase = CTF_ERR;
1048 ctf_id_t lref = CTF_ERR, rref = CTF_ERR;
1050 int lp_is_void, rp_is_void, lp_is_int, rp_is_int, compat;
1051 uint_t lkind, rkind;
1052 ctf_encoding_t e;
1053 ctf_arinfo_t r;
1055 assert(lp->dn_flags & DT_NF_COOKED);
1056 assert(rp->dn_flags & DT_NF_COOKED);
1058 if (dt_node_is_dynamic(lp) || dt_node_is_dynamic(rp))
1059 return (0); /* fail if either node is a dynamic variable */
1061 lp_is_int = dt_node_is_integer(lp);
1062 rp_is_int = dt_node_is_integer(rp);
1064 if (lp_is_int && rp_is_int)
1065 return (0); /* fail if both nodes are integers */
1067 if (lp_is_int && (lp->dn_kind != DT_NODE_INT || lp->dn_value != 0))
1068 return (0); /* fail if lp is an integer that isn't 0 constant */
1070 if (rp_is_int && (rp->dn_kind != DT_NODE_INT || rp->dn_value != 0))
1071 return (0); /* fail if rp is an integer that isn't 0 constant */
1073 if ((lp_is_int == 0 && rp_is_int == 0) && (
1074 (lp->dn_flags & DT_NF_USERLAND) ^ (rp->dn_flags & DT_NF_USERLAND)))
1075 return (0); /* fail if only one pointer is a userland address */
1078 * Resolve the left-hand and right-hand types to their base type, and
1079 * then resolve the referenced type as well (assuming the base type
1080 * is CTF_K_POINTER or CTF_K_ARRAY). Otherwise [lr]ref = CTF_ERR.
1082 if (!lp_is_int) {
1083 lbase = ctf_type_resolve(lfp, lp->dn_type);
1084 lkind = ctf_type_kind(lfp, lbase);
1086 if (lkind == CTF_K_POINTER) {
1087 lref = ctf_type_resolve(lfp,
1088 ctf_type_reference(lfp, lbase));
1089 } else if (lkind == CTF_K_ARRAY &&
1090 ctf_array_info(lfp, lbase, &r) == 0) {
1091 lref = ctf_type_resolve(lfp, r.ctr_contents);
1095 if (!rp_is_int) {
1096 rbase = ctf_type_resolve(rfp, rp->dn_type);
1097 rkind = ctf_type_kind(rfp, rbase);
1099 if (rkind == CTF_K_POINTER) {
1100 rref = ctf_type_resolve(rfp,
1101 ctf_type_reference(rfp, rbase));
1102 } else if (rkind == CTF_K_ARRAY &&
1103 ctf_array_info(rfp, rbase, &r) == 0) {
1104 rref = ctf_type_resolve(rfp, r.ctr_contents);
1109 * We know that one or the other type may still be a zero-valued
1110 * integer constant. To simplify the code below, set the integer
1111 * type variables equal to the non-integer types and proceed.
1113 if (lp_is_int) {
1114 lbase = rbase;
1115 lkind = rkind;
1116 lref = rref;
1117 lfp = rfp;
1118 } else if (rp_is_int) {
1119 rbase = lbase;
1120 rkind = lkind;
1121 rref = lref;
1122 rfp = lfp;
1125 lp_is_void = ctf_type_encoding(lfp, lref, &e) == 0 && IS_VOID(e);
1126 rp_is_void = ctf_type_encoding(rfp, rref, &e) == 0 && IS_VOID(e);
1129 * The types are compatible if both are pointers to the same type, or
1130 * if either pointer is a void pointer. If they are compatible, set
1131 * tp to point to the more specific pointer type and return it.
1133 compat = (lkind == CTF_K_POINTER || lkind == CTF_K_ARRAY) &&
1134 (rkind == CTF_K_POINTER || rkind == CTF_K_ARRAY) &&
1135 (lp_is_void || rp_is_void || ctf_type_compat(lfp, lref, rfp, rref));
1137 if (compat) {
1138 if (fpp != NULL)
1139 *fpp = rp_is_void ? lfp : rfp;
1140 if (tp != NULL)
1141 *tp = rp_is_void ? lbase : rbase;
1144 return (compat);
1148 * The rules for checking argument types against parameter types are described
1149 * in the ANSI-C spec (see K&R[A7.3.2] and K&R[A7.17]). We use the same rule
1150 * set to determine whether associative array arguments match the prototype.
1153 dt_node_is_argcompat(const dt_node_t *lp, const dt_node_t *rp)
1155 ctf_file_t *lfp = lp->dn_ctfp;
1156 ctf_file_t *rfp = rp->dn_ctfp;
1158 assert(lp->dn_flags & DT_NF_COOKED);
1159 assert(rp->dn_flags & DT_NF_COOKED);
1161 if (dt_node_is_integer(lp) && dt_node_is_integer(rp))
1162 return (1); /* integer types are compatible */
1164 if (dt_node_is_strcompat(lp) && dt_node_is_strcompat(rp))
1165 return (1); /* string types are compatible */
1167 if (dt_node_is_stack(lp) && dt_node_is_stack(rp))
1168 return (1); /* stack types are compatible */
1170 if (dt_node_is_symaddr(lp) && dt_node_is_symaddr(rp))
1171 return (1); /* symaddr types are compatible */
1173 if (dt_node_is_usymaddr(lp) && dt_node_is_usymaddr(rp))
1174 return (1); /* usymaddr types are compatible */
1176 switch (ctf_type_kind(lfp, ctf_type_resolve(lfp, lp->dn_type))) {
1177 case CTF_K_FUNCTION:
1178 case CTF_K_STRUCT:
1179 case CTF_K_UNION:
1180 return (ctf_type_compat(lfp, lp->dn_type, rfp, rp->dn_type));
1181 default:
1182 return (dt_node_is_ptrcompat(lp, rp, NULL, NULL));
1187 * We provide dt_node_is_posconst() as a convenience routine for callers who
1188 * wish to verify that an argument is a positive non-zero integer constant.
1191 dt_node_is_posconst(const dt_node_t *dnp)
1193 return (dnp->dn_kind == DT_NODE_INT && dnp->dn_value != 0 && (
1194 (dnp->dn_flags & DT_NF_SIGNED) == 0 || (int64_t)dnp->dn_value > 0));
1198 dt_node_is_actfunc(const dt_node_t *dnp)
1200 return (dnp->dn_kind == DT_NODE_FUNC &&
1201 dnp->dn_ident->di_kind == DT_IDENT_ACTFUNC);
1205 * The original rules for integer constant typing are described in K&R[A2.5.1].
1206 * However, since we support long long, we instead use the rules from ISO C99
1207 * clause 6.4.4.1 since that is where long longs are formally described. The
1208 * rules require us to know whether the constant was specified in decimal or
1209 * in octal or hex, which we do by looking at our lexer's 'yyintdecimal' flag.
1210 * The type of an integer constant is the first of the corresponding list in
1211 * which its value can be represented:
1213 * unsuffixed decimal: int, long, long long
1214 * unsuffixed oct/hex: int, unsigned int, long, unsigned long,
1215 * long long, unsigned long long
1216 * suffix [uU]: unsigned int, unsigned long, unsigned long long
1217 * suffix [lL] decimal: long, long long
1218 * suffix [lL] oct/hex: long, unsigned long, long long, unsigned long long
1219 * suffix [uU][Ll]: unsigned long, unsigned long long
1220 * suffix ll/LL decimal: long long
1221 * suffix ll/LL oct/hex: long long, unsigned long long
1222 * suffix [uU][ll/LL]: unsigned long long
1224 * Given that our lexer has already validated the suffixes by regexp matching,
1225 * there is an obvious way to concisely encode these rules: construct an array
1226 * of the types in the order int, unsigned int, long, unsigned long, long long,
1227 * unsigned long long. Compute an integer array starting index based on the
1228 * suffix (e.g. none = 0, u = 1, ull = 5), and compute an increment based on
1229 * the specifier (dec/oct/hex) and suffix (u). Then iterate from the starting
1230 * index to the end, advancing using the increment, and searching until we
1231 * find a limit that matches or we run out of choices (overflow). To make it
1232 * even faster, we precompute the table of type information in dtrace_open().
1234 dt_node_t *
1235 dt_node_int(uintmax_t value)
1237 dt_node_t *dnp = dt_node_alloc(DT_NODE_INT);
1238 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
1240 int n = (yyintdecimal | (yyintsuffix[0] == 'u')) + 1;
1241 int i = 0;
1243 const char *p;
1244 char c;
1246 dnp->dn_op = DT_TOK_INT;
1247 dnp->dn_value = value;
1249 for (p = yyintsuffix; (c = *p) != '\0'; p++) {
1250 if (c == 'U' || c == 'u')
1251 i += 1;
1252 else if (c == 'L' || c == 'l')
1253 i += 2;
1256 for (; i < sizeof (dtp->dt_ints) / sizeof (dtp->dt_ints[0]); i += n) {
1257 if (value <= dtp->dt_ints[i].did_limit) {
1258 dt_node_type_assign(dnp,
1259 dtp->dt_ints[i].did_ctfp,
1260 dtp->dt_ints[i].did_type, B_FALSE);
1263 * If a prefix character is present in macro text, add
1264 * in the corresponding operator node (see dt_lex.l).
1266 switch (yyintprefix) {
1267 case '+':
1268 return (dt_node_op1(DT_TOK_IPOS, dnp));
1269 case '-':
1270 return (dt_node_op1(DT_TOK_INEG, dnp));
1271 default:
1272 return (dnp);
1277 xyerror(D_INT_OFLOW, "integer constant 0x%llx cannot be represented "
1278 "in any built-in integral type\n", (u_longlong_t)value);
1279 /*NOTREACHED*/
1280 return (NULL); /* keep gcc happy */
1283 dt_node_t *
1284 dt_node_string(char *string)
1286 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
1287 dt_node_t *dnp;
1289 if (string == NULL)
1290 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
1292 dnp = dt_node_alloc(DT_NODE_STRING);
1293 dnp->dn_op = DT_TOK_STRING;
1294 dnp->dn_string = string;
1295 dt_node_type_assign(dnp, DT_STR_CTFP(dtp), DT_STR_TYPE(dtp), B_FALSE);
1297 return (dnp);
1300 dt_node_t *
1301 dt_node_ident(char *name)
1303 dt_ident_t *idp;
1304 dt_node_t *dnp;
1306 if (name == NULL)
1307 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
1310 * If the identifier is an inlined integer constant, then create an INT
1311 * node that is a clone of the inline parse tree node and return that
1312 * immediately, allowing this inline to be used in parsing contexts
1313 * that require constant expressions (e.g. scalar array sizes).
1315 if ((idp = dt_idstack_lookup(&yypcb->pcb_globals, name)) != NULL &&
1316 (idp->di_flags & DT_IDFLG_INLINE)) {
1317 dt_idnode_t *inp = idp->di_iarg;
1319 if (inp->din_root != NULL &&
1320 inp->din_root->dn_kind == DT_NODE_INT) {
1321 free(name);
1323 dnp = dt_node_alloc(DT_NODE_INT);
1324 dnp->dn_op = DT_TOK_INT;
1325 dnp->dn_value = inp->din_root->dn_value;
1326 dt_node_type_propagate(inp->din_root, dnp);
1328 return (dnp);
1332 dnp = dt_node_alloc(DT_NODE_IDENT);
1333 dnp->dn_op = name[0] == '@' ? DT_TOK_AGG : DT_TOK_IDENT;
1334 dnp->dn_string = name;
1336 return (dnp);
1340 * Create an empty node of type corresponding to the given declaration.
1341 * Explicit references to user types (C or D) are assigned the default
1342 * stability; references to other types are _dtrace_typattr (Private).
1344 dt_node_t *
1345 dt_node_type(dt_decl_t *ddp)
1347 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
1348 dtrace_typeinfo_t dtt;
1349 dt_node_t *dnp;
1350 char *name = NULL;
1351 int err;
1354 * If 'ddp' is NULL, we get a decl by popping the decl stack. This
1355 * form of dt_node_type() is used by parameter rules in dt_grammar.y.
1357 if (ddp == NULL)
1358 ddp = dt_decl_pop_param(&name);
1360 err = dt_decl_type(ddp, &dtt);
1361 dt_decl_free(ddp);
1363 if (err != 0) {
1364 free(name);
1365 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
1368 dnp = dt_node_alloc(DT_NODE_TYPE);
1369 dnp->dn_op = DT_TOK_IDENT;
1370 dnp->dn_string = name;
1372 dt_node_type_assign(dnp, dtt.dtt_ctfp, dtt.dtt_type,
1373 dtt.dtt_flags & DTT_FL_USER ? B_TRUE : B_FALSE);
1375 if (dtt.dtt_ctfp == dtp->dt_cdefs->dm_ctfp ||
1376 dtt.dtt_ctfp == dtp->dt_ddefs->dm_ctfp)
1377 dt_node_attr_assign(dnp, _dtrace_defattr);
1378 else
1379 dt_node_attr_assign(dnp, _dtrace_typattr);
1381 return (dnp);
1385 * Create a type node corresponding to a varargs (...) parameter by just
1386 * assigning it type CTF_ERR. The decl processing code will handle this.
1388 dt_node_t *
1389 dt_node_vatype(void)
1391 dt_node_t *dnp = dt_node_alloc(DT_NODE_TYPE);
1393 dnp->dn_op = DT_TOK_IDENT;
1394 dnp->dn_ctfp = yypcb->pcb_hdl->dt_cdefs->dm_ctfp;
1395 dnp->dn_type = CTF_ERR;
1396 dnp->dn_attr = _dtrace_defattr;
1398 return (dnp);
1402 * Instantiate a decl using the contents of the current declaration stack. As
1403 * we do not currently permit decls to be initialized, this function currently
1404 * returns NULL and no parse node is created. When this function is called,
1405 * the topmost scope's ds_ident pointer will be set to NULL (indicating no
1406 * init_declarator rule was matched) or will point to the identifier to use.
1408 dt_node_t *
1409 dt_node_decl(void)
1411 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
1412 dt_scope_t *dsp = &yypcb->pcb_dstack;
1413 dt_dclass_t class = dsp->ds_class;
1414 dt_decl_t *ddp = dt_decl_top();
1416 dt_module_t *dmp;
1417 dtrace_typeinfo_t dtt;
1418 ctf_id_t type;
1420 char n1[DT_TYPE_NAMELEN];
1421 char n2[DT_TYPE_NAMELEN];
1423 if (dt_decl_type(ddp, &dtt) != 0)
1424 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
1427 * If we have no declaration identifier, then this is either a spurious
1428 * declaration of an intrinsic type (e.g. "extern int;") or declaration
1429 * or redeclaration of a struct, union, or enum type or tag.
1431 if (dsp->ds_ident == NULL) {
1432 if (ddp->dd_kind != CTF_K_STRUCT &&
1433 ddp->dd_kind != CTF_K_UNION && ddp->dd_kind != CTF_K_ENUM)
1434 xyerror(D_DECL_USELESS, "useless declaration\n");
1436 dt_dprintf("type %s added as id %ld\n", dt_type_name(
1437 ddp->dd_ctfp, ddp->dd_type, n1, sizeof (n1)), ddp->dd_type);
1439 return (NULL);
1442 if (strchr(dsp->ds_ident, '`') != NULL) {
1443 xyerror(D_DECL_SCOPE, "D scoping operator may not be used in "
1444 "a declaration name (%s)\n", dsp->ds_ident);
1448 * If we are nested inside of a C include file, add the declaration to
1449 * the C definition module; otherwise use the D definition module.
1451 if (yypcb->pcb_idepth != 0)
1452 dmp = dtp->dt_cdefs;
1453 else
1454 dmp = dtp->dt_ddefs;
1457 * If we see a global or static declaration of a function prototype,
1458 * treat this as equivalent to a D extern declaration.
1460 if (ctf_type_kind(dtt.dtt_ctfp, dtt.dtt_type) == CTF_K_FUNCTION &&
1461 (class == DT_DC_DEFAULT || class == DT_DC_STATIC))
1462 class = DT_DC_EXTERN;
1464 switch (class) {
1465 case DT_DC_AUTO:
1466 case DT_DC_REGISTER:
1467 case DT_DC_STATIC:
1468 xyerror(D_DECL_BADCLASS, "specified storage class not "
1469 "appropriate in D\n");
1470 /*NOTREACHED*/
1472 case DT_DC_EXTERN: {
1473 dtrace_typeinfo_t ott;
1474 dtrace_syminfo_t dts;
1475 GElf_Sym sym;
1477 int exists = dtrace_lookup_by_name(dtp,
1478 dmp->dm_name, dsp->ds_ident, &sym, &dts) == 0;
1480 if (exists && (dtrace_symbol_type(dtp, &sym, &dts, &ott) != 0 ||
1481 ctf_type_cmp(dtt.dtt_ctfp, dtt.dtt_type,
1482 ott.dtt_ctfp, ott.dtt_type) != 0)) {
1483 xyerror(D_DECL_IDRED, "identifier redeclared: %s`%s\n"
1484 "\t current: %s\n\tprevious: %s\n",
1485 dmp->dm_name, dsp->ds_ident,
1486 dt_type_name(dtt.dtt_ctfp, dtt.dtt_type,
1487 n1, sizeof (n1)),
1488 dt_type_name(ott.dtt_ctfp, ott.dtt_type,
1489 n2, sizeof (n2)));
1490 } else if (!exists && dt_module_extern(dtp, dmp,
1491 dsp->ds_ident, &dtt) == NULL) {
1492 xyerror(D_UNKNOWN,
1493 "failed to extern %s: %s\n", dsp->ds_ident,
1494 dtrace_errmsg(dtp, dtrace_errno(dtp)));
1495 } else {
1496 dt_dprintf("extern %s`%s type=<%s>\n",
1497 dmp->dm_name, dsp->ds_ident,
1498 dt_type_name(dtt.dtt_ctfp, dtt.dtt_type,
1499 n1, sizeof (n1)));
1501 break;
1504 case DT_DC_TYPEDEF:
1505 if (dt_idstack_lookup(&yypcb->pcb_globals, dsp->ds_ident)) {
1506 xyerror(D_DECL_IDRED, "global variable identifier "
1507 "redeclared: %s\n", dsp->ds_ident);
1510 if (ctf_lookup_by_name(dmp->dm_ctfp,
1511 dsp->ds_ident) != CTF_ERR) {
1512 xyerror(D_DECL_IDRED,
1513 "typedef redeclared: %s\n", dsp->ds_ident);
1517 * If the source type for the typedef is not defined in the
1518 * target container or its parent, copy the type to the target
1519 * container and reset dtt_ctfp and dtt_type to the copy.
1521 if (dtt.dtt_ctfp != dmp->dm_ctfp &&
1522 dtt.dtt_ctfp != ctf_parent_file(dmp->dm_ctfp)) {
1524 dtt.dtt_type = ctf_add_type(dmp->dm_ctfp,
1525 dtt.dtt_ctfp, dtt.dtt_type);
1526 dtt.dtt_ctfp = dmp->dm_ctfp;
1528 if (dtt.dtt_type == CTF_ERR ||
1529 ctf_update(dtt.dtt_ctfp) == CTF_ERR) {
1530 xyerror(D_UNKNOWN, "failed to copy typedef %s "
1531 "source type: %s\n", dsp->ds_ident,
1532 ctf_errmsg(ctf_errno(dtt.dtt_ctfp)));
1536 type = ctf_add_typedef(dmp->dm_ctfp,
1537 CTF_ADD_ROOT, dsp->ds_ident, dtt.dtt_type);
1539 if (type == CTF_ERR || ctf_update(dmp->dm_ctfp) == CTF_ERR) {
1540 xyerror(D_UNKNOWN, "failed to typedef %s: %s\n",
1541 dsp->ds_ident, ctf_errmsg(ctf_errno(dmp->dm_ctfp)));
1544 dt_dprintf("typedef %s added as id %ld\n", dsp->ds_ident, type);
1545 break;
1547 default: {
1548 ctf_encoding_t cte;
1549 dt_idhash_t *dhp;
1550 dt_ident_t *idp;
1551 dt_node_t idn;
1552 int assc, idkind;
1553 uint_t id, kind;
1554 ushort_t idflags;
1556 switch (class) {
1557 case DT_DC_THIS:
1558 dhp = yypcb->pcb_locals;
1559 idflags = DT_IDFLG_LOCAL;
1560 idp = dt_idhash_lookup(dhp, dsp->ds_ident);
1561 break;
1562 case DT_DC_SELF:
1563 dhp = dtp->dt_tls;
1564 idflags = DT_IDFLG_TLS;
1565 idp = dt_idhash_lookup(dhp, dsp->ds_ident);
1566 break;
1567 default:
1568 dhp = dtp->dt_globals;
1569 idflags = 0;
1570 idp = dt_idstack_lookup(
1571 &yypcb->pcb_globals, dsp->ds_ident);
1572 break;
1575 if (ddp->dd_kind == CTF_K_ARRAY && ddp->dd_node == NULL) {
1576 xyerror(D_DECL_ARRNULL,
1577 "array declaration requires array dimension or "
1578 "tuple signature: %s\n", dsp->ds_ident);
1581 if (idp != NULL && idp->di_gen == 0) {
1582 xyerror(D_DECL_IDRED, "built-in identifier "
1583 "redeclared: %s\n", idp->di_name);
1586 if (dtrace_lookup_by_type(dtp, DTRACE_OBJ_CDEFS,
1587 dsp->ds_ident, NULL) == 0 ||
1588 dtrace_lookup_by_type(dtp, DTRACE_OBJ_DDEFS,
1589 dsp->ds_ident, NULL) == 0) {
1590 xyerror(D_DECL_IDRED, "typedef identifier "
1591 "redeclared: %s\n", dsp->ds_ident);
1595 * Cache some attributes of the decl to make the rest of this
1596 * code simpler: if the decl is an array which is subscripted
1597 * by a type rather than an integer, then it's an associative
1598 * array (assc). We then expect to match either DT_IDENT_ARRAY
1599 * for associative arrays or DT_IDENT_SCALAR for anything else.
1601 assc = ddp->dd_kind == CTF_K_ARRAY &&
1602 ddp->dd_node->dn_kind == DT_NODE_TYPE;
1604 idkind = assc ? DT_IDENT_ARRAY : DT_IDENT_SCALAR;
1607 * Create a fake dt_node_t on the stack so we can determine the
1608 * type of any matching identifier by assigning to this node.
1609 * If the pre-existing ident has its di_type set, propagate
1610 * the type by hand so as not to trigger a prototype check for
1611 * arrays (yet); otherwise we use dt_ident_cook() on the ident
1612 * to ensure it is fully initialized before looking at it.
1614 bzero(&idn, sizeof (dt_node_t));
1616 if (idp != NULL && idp->di_type != CTF_ERR)
1617 dt_node_type_assign(&idn, idp->di_ctfp, idp->di_type,
1618 B_FALSE);
1619 else if (idp != NULL)
1620 (void) dt_ident_cook(&idn, idp, NULL);
1622 if (assc) {
1623 if (class == DT_DC_THIS) {
1624 xyerror(D_DECL_LOCASSC, "associative arrays "
1625 "may not be declared as local variables:"
1626 " %s\n", dsp->ds_ident);
1629 if (dt_decl_type(ddp->dd_next, &dtt) != 0)
1630 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
1633 if (idp != NULL && (idp->di_kind != idkind ||
1634 ctf_type_cmp(dtt.dtt_ctfp, dtt.dtt_type,
1635 idn.dn_ctfp, idn.dn_type) != 0)) {
1636 xyerror(D_DECL_IDRED, "identifier redeclared: %s\n"
1637 "\t current: %s %s\n\tprevious: %s %s\n",
1638 dsp->ds_ident, dt_idkind_name(idkind),
1639 dt_type_name(dtt.dtt_ctfp,
1640 dtt.dtt_type, n1, sizeof (n1)),
1641 dt_idkind_name(idp->di_kind),
1642 dt_node_type_name(&idn, n2, sizeof (n2)));
1644 } else if (idp != NULL && assc) {
1645 const dt_idsig_t *isp = idp->di_data;
1646 dt_node_t *dnp = ddp->dd_node;
1647 int argc = 0;
1649 for (; dnp != NULL; dnp = dnp->dn_list, argc++) {
1650 const dt_node_t *pnp = &isp->dis_args[argc];
1652 if (argc >= isp->dis_argc)
1653 continue; /* tuple length mismatch */
1655 if (ctf_type_cmp(dnp->dn_ctfp, dnp->dn_type,
1656 pnp->dn_ctfp, pnp->dn_type) == 0)
1657 continue;
1659 xyerror(D_DECL_IDRED,
1660 "identifier redeclared: %s\n"
1661 "\t current: %s, key #%d of type %s\n"
1662 "\tprevious: %s, key #%d of type %s\n",
1663 dsp->ds_ident,
1664 dt_idkind_name(idkind), argc + 1,
1665 dt_node_type_name(dnp, n1, sizeof (n1)),
1666 dt_idkind_name(idp->di_kind), argc + 1,
1667 dt_node_type_name(pnp, n2, sizeof (n2)));
1670 if (isp->dis_argc != argc) {
1671 xyerror(D_DECL_IDRED,
1672 "identifier redeclared: %s\n"
1673 "\t current: %s of %s, tuple length %d\n"
1674 "\tprevious: %s of %s, tuple length %d\n",
1675 dsp->ds_ident, dt_idkind_name(idkind),
1676 dt_type_name(dtt.dtt_ctfp, dtt.dtt_type,
1677 n1, sizeof (n1)), argc,
1678 dt_idkind_name(idp->di_kind),
1679 dt_node_type_name(&idn, n2, sizeof (n2)),
1680 isp->dis_argc);
1683 } else if (idp == NULL) {
1684 type = ctf_type_resolve(dtt.dtt_ctfp, dtt.dtt_type);
1685 kind = ctf_type_kind(dtt.dtt_ctfp, type);
1687 switch (kind) {
1688 case CTF_K_INTEGER:
1689 if (ctf_type_encoding(dtt.dtt_ctfp, type,
1690 &cte) == 0 && IS_VOID(cte)) {
1691 xyerror(D_DECL_VOIDOBJ, "cannot have "
1692 "void object: %s\n", dsp->ds_ident);
1694 break;
1695 case CTF_K_STRUCT:
1696 case CTF_K_UNION:
1697 if (ctf_type_size(dtt.dtt_ctfp, type) != 0)
1698 break; /* proceed to declaring */
1699 /*FALLTHRU*/
1700 case CTF_K_FORWARD:
1701 xyerror(D_DECL_INCOMPLETE,
1702 "incomplete struct/union/enum %s: %s\n",
1703 dt_type_name(dtt.dtt_ctfp, dtt.dtt_type,
1704 n1, sizeof (n1)), dsp->ds_ident);
1705 /*NOTREACHED*/
1708 if (dt_idhash_nextid(dhp, &id) == -1) {
1709 xyerror(D_ID_OFLOW, "cannot create %s: limit "
1710 "on number of %s variables exceeded\n",
1711 dsp->ds_ident, dt_idhash_name(dhp));
1714 dt_dprintf("declare %s %s variable %s, id=%u\n",
1715 dt_idhash_name(dhp), dt_idkind_name(idkind),
1716 dsp->ds_ident, id);
1718 idp = dt_idhash_insert(dhp, dsp->ds_ident, idkind,
1719 idflags | DT_IDFLG_WRITE | DT_IDFLG_DECL, id,
1720 _dtrace_defattr, 0, assc ? &dt_idops_assc :
1721 &dt_idops_thaw, NULL, dtp->dt_gen);
1723 if (idp == NULL)
1724 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
1726 dt_ident_type_assign(idp, dtt.dtt_ctfp, dtt.dtt_type);
1729 * If we are declaring an associative array, use our
1730 * fake parse node to cook the new assoc identifier.
1731 * This will force the ident code to instantiate the
1732 * array type signature corresponding to the list of
1733 * types pointed to by ddp->dd_node. We also reset
1734 * the identifier's attributes based upon the result.
1736 if (assc) {
1737 idp->di_attr =
1738 dt_ident_cook(&idn, idp, &ddp->dd_node);
1743 } /* end of switch */
1745 free(dsp->ds_ident);
1746 dsp->ds_ident = NULL;
1748 return (NULL);
1751 dt_node_t *
1752 dt_node_func(dt_node_t *dnp, dt_node_t *args)
1754 dt_ident_t *idp;
1756 if (dnp->dn_kind != DT_NODE_IDENT) {
1757 xyerror(D_FUNC_IDENT,
1758 "function designator is not of function type\n");
1761 idp = dt_idstack_lookup(&yypcb->pcb_globals, dnp->dn_string);
1763 if (idp == NULL) {
1764 xyerror(D_FUNC_UNDEF,
1765 "undefined function name: %s\n", dnp->dn_string);
1768 if (idp->di_kind != DT_IDENT_FUNC &&
1769 idp->di_kind != DT_IDENT_AGGFUNC &&
1770 idp->di_kind != DT_IDENT_ACTFUNC) {
1771 xyerror(D_FUNC_IDKIND, "%s '%s' may not be referenced as a "
1772 "function\n", dt_idkind_name(idp->di_kind), idp->di_name);
1775 free(dnp->dn_string);
1776 dnp->dn_string = NULL;
1778 dnp->dn_kind = DT_NODE_FUNC;
1779 dnp->dn_flags &= ~DT_NF_COOKED;
1780 dnp->dn_ident = idp;
1781 dnp->dn_args = args;
1782 dnp->dn_list = NULL;
1784 return (dnp);
1788 * The offsetof() function is special because it takes a type name as an
1789 * argument. It does not actually construct its own node; after looking up the
1790 * structure or union offset, we just return an integer node with the offset.
1792 dt_node_t *
1793 dt_node_offsetof(dt_decl_t *ddp, char *s)
1795 dtrace_typeinfo_t dtt;
1796 dt_node_t dn;
1797 char *name;
1798 int err;
1800 ctf_membinfo_t ctm;
1801 ctf_id_t type;
1802 ctf_file_t *ctfp;
1803 uint_t kind;
1805 name = strdupa(s);
1806 free(s);
1808 err = dt_decl_type(ddp, &dtt);
1809 dt_decl_free(ddp);
1811 if (err != 0)
1812 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
1814 type = ctf_type_resolve(dtt.dtt_ctfp, dtt.dtt_type);
1815 kind = ctf_type_kind(dtt.dtt_ctfp, type);
1817 if (kind != CTF_K_STRUCT && kind != CTF_K_UNION) {
1818 xyerror(D_OFFSETOF_TYPE,
1819 "offsetof operand must be a struct or union type\n");
1822 if (ctf_member_info(dtt.dtt_ctfp, type, name, &ctm) == CTF_ERR) {
1823 xyerror(D_UNKNOWN, "failed to determine offset of %s: %s\n",
1824 name, ctf_errmsg(ctf_errno(dtt.dtt_ctfp)));
1827 bzero(&dn, sizeof (dn));
1829 * Resolution of CTF_K_FORWARD is unnecessary here, since it can't be
1830 * both forward _and_ a bitfield, but is done for completeness.
1832 type = ctm.ctm_type;
1833 ctfp = dtt.dtt_ctfp;
1835 dt_resolve_forward_decl(&ctfp, &type);
1836 dt_node_type_assign(&dn, ctfp, type, B_FALSE);
1838 if (dn.dn_flags & DT_NF_BITFIELD) {
1839 xyerror(D_OFFSETOF_BITFIELD,
1840 "cannot take offset of a bit-field: %s\n", name);
1843 return (dt_node_int(ctm.ctm_offset / NBBY));
1846 dt_node_t *
1847 dt_node_op1(int op, dt_node_t *cp)
1849 dt_node_t *dnp;
1851 if (cp->dn_kind == DT_NODE_INT) {
1852 switch (op) {
1853 case DT_TOK_INEG:
1855 * If we're negating an unsigned integer, zero out any
1856 * extra top bits to truncate the value to the size of
1857 * the effective type determined by dt_node_int().
1859 cp->dn_value = -cp->dn_value;
1860 if (!(cp->dn_flags & DT_NF_SIGNED)) {
1861 cp->dn_value &= ~0ULL >>
1862 (64 - dt_node_type_size(cp) * NBBY);
1864 /*FALLTHRU*/
1865 case DT_TOK_IPOS:
1866 return (cp);
1867 case DT_TOK_BNEG:
1868 cp->dn_value = ~cp->dn_value;
1869 return (cp);
1870 case DT_TOK_LNEG:
1871 cp->dn_value = !cp->dn_value;
1872 return (cp);
1877 * If sizeof is applied to a type_name or string constant, we can
1878 * transform 'cp' into an integer constant in the node construction
1879 * pass so that it can then be used for arithmetic in this pass.
1881 if (op == DT_TOK_SIZEOF &&
1882 (cp->dn_kind == DT_NODE_STRING || cp->dn_kind == DT_NODE_TYPE)) {
1883 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
1884 size_t size = dt_node_type_size(cp);
1886 if (size == 0) {
1887 xyerror(D_SIZEOF_TYPE, "cannot apply sizeof to an "
1888 "operand of unknown size\n");
1891 dt_node_type_assign(cp, dtp->dt_ddefs->dm_ctfp,
1892 ctf_lookup_by_name(dtp->dt_ddefs->dm_ctfp, "size_t"),
1893 B_FALSE);
1895 cp->dn_kind = DT_NODE_INT;
1896 cp->dn_op = DT_TOK_INT;
1897 cp->dn_value = size;
1899 return (cp);
1902 dnp = dt_node_alloc(DT_NODE_OP1);
1903 assert(op <= USHRT_MAX);
1904 dnp->dn_op = (ushort_t)op;
1905 dnp->dn_child = cp;
1907 return (dnp);
1911 * If an integer constant is being cast to another integer type, we can
1912 * perform the cast as part of integer constant folding in this pass. We must
1913 * take action when the integer is being cast to a smaller type or if it is
1914 * changing signed-ness. If so, we first shift rp's bits bits high (losing
1915 * excess bits if narrowing) and then shift them down with either a logical
1916 * shift (unsigned) or arithmetic shift (signed).
1918 static void
1919 dt_cast(dt_node_t *lp, dt_node_t *rp)
1921 size_t srcsize = dt_node_type_size(rp);
1922 size_t dstsize = dt_node_type_size(lp);
1924 if (dstsize < srcsize) {
1925 int n = (sizeof (uint64_t) - dstsize) * NBBY;
1926 rp->dn_value <<= n;
1927 rp->dn_value >>= n;
1928 } else if (dstsize > srcsize) {
1929 int n = (sizeof (uint64_t) - srcsize) * NBBY;
1930 int s = (dstsize - srcsize) * NBBY;
1932 rp->dn_value <<= n;
1933 if (rp->dn_flags & DT_NF_SIGNED) {
1934 rp->dn_value = (intmax_t)rp->dn_value >> s;
1935 rp->dn_value >>= n - s;
1936 } else {
1937 rp->dn_value >>= n;
1942 dt_node_t *
1943 dt_node_op2(int op, dt_node_t *lp, dt_node_t *rp)
1945 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
1946 dt_node_t *dnp;
1949 * First we check for operations that are illegal -- namely those that
1950 * might result in integer division by zero, and abort if one is found.
1952 if (rp->dn_kind == DT_NODE_INT && rp->dn_value == 0 &&
1953 (op == DT_TOK_MOD || op == DT_TOK_DIV ||
1954 op == DT_TOK_MOD_EQ || op == DT_TOK_DIV_EQ))
1955 xyerror(D_DIV_ZERO, "expression contains division by zero\n");
1958 * If both children are immediate values, we can just perform inline
1959 * calculation and return a new immediate node with the result.
1961 if (lp->dn_kind == DT_NODE_INT && rp->dn_kind == DT_NODE_INT) {
1962 uintmax_t l = lp->dn_value;
1963 uintmax_t r = rp->dn_value;
1965 dnp = dt_node_int(0); /* allocate new integer node for result */
1967 switch (op) {
1968 case DT_TOK_LOR:
1969 dnp->dn_value = l || r;
1970 dt_node_type_assign(dnp,
1971 DT_INT_CTFP(dtp), DT_INT_TYPE(dtp), B_FALSE);
1972 break;
1973 case DT_TOK_LXOR:
1974 dnp->dn_value = (l != 0) ^ (r != 0);
1975 dt_node_type_assign(dnp,
1976 DT_INT_CTFP(dtp), DT_INT_TYPE(dtp), B_FALSE);
1977 break;
1978 case DT_TOK_LAND:
1979 dnp->dn_value = l && r;
1980 dt_node_type_assign(dnp,
1981 DT_INT_CTFP(dtp), DT_INT_TYPE(dtp), B_FALSE);
1982 break;
1983 case DT_TOK_BOR:
1984 dnp->dn_value = l | r;
1985 dt_node_promote(lp, rp, dnp);
1986 break;
1987 case DT_TOK_XOR:
1988 dnp->dn_value = l ^ r;
1989 dt_node_promote(lp, rp, dnp);
1990 break;
1991 case DT_TOK_BAND:
1992 dnp->dn_value = l & r;
1993 dt_node_promote(lp, rp, dnp);
1994 break;
1995 case DT_TOK_EQU:
1996 dnp->dn_value = l == r;
1997 dt_node_type_assign(dnp,
1998 DT_INT_CTFP(dtp), DT_INT_TYPE(dtp), B_FALSE);
1999 break;
2000 case DT_TOK_NEQ:
2001 dnp->dn_value = l != r;
2002 dt_node_type_assign(dnp,
2003 DT_INT_CTFP(dtp), DT_INT_TYPE(dtp), B_FALSE);
2004 break;
2005 case DT_TOK_LT:
2006 dt_node_promote(lp, rp, dnp);
2007 if (dnp->dn_flags & DT_NF_SIGNED)
2008 dnp->dn_value = (intmax_t)l < (intmax_t)r;
2009 else
2010 dnp->dn_value = l < r;
2011 dt_node_type_assign(dnp,
2012 DT_INT_CTFP(dtp), DT_INT_TYPE(dtp), B_FALSE);
2013 break;
2014 case DT_TOK_LE:
2015 dt_node_promote(lp, rp, dnp);
2016 if (dnp->dn_flags & DT_NF_SIGNED)
2017 dnp->dn_value = (intmax_t)l <= (intmax_t)r;
2018 else
2019 dnp->dn_value = l <= r;
2020 dt_node_type_assign(dnp,
2021 DT_INT_CTFP(dtp), DT_INT_TYPE(dtp), B_FALSE);
2022 break;
2023 case DT_TOK_GT:
2024 dt_node_promote(lp, rp, dnp);
2025 if (dnp->dn_flags & DT_NF_SIGNED)
2026 dnp->dn_value = (intmax_t)l > (intmax_t)r;
2027 else
2028 dnp->dn_value = l > r;
2029 dt_node_type_assign(dnp,
2030 DT_INT_CTFP(dtp), DT_INT_TYPE(dtp), B_FALSE);
2031 break;
2032 case DT_TOK_GE:
2033 dt_node_promote(lp, rp, dnp);
2034 if (dnp->dn_flags & DT_NF_SIGNED)
2035 dnp->dn_value = (intmax_t)l >= (intmax_t)r;
2036 else
2037 dnp->dn_value = l >= r;
2038 dt_node_type_assign(dnp,
2039 DT_INT_CTFP(dtp), DT_INT_TYPE(dtp), B_FALSE);
2040 break;
2041 case DT_TOK_LSH:
2042 dnp->dn_value = l << r;
2043 dt_node_type_propagate(lp, dnp);
2044 dt_node_attr_assign(rp,
2045 dt_attr_min(lp->dn_attr, rp->dn_attr));
2046 break;
2047 case DT_TOK_RSH:
2048 dnp->dn_value = l >> r;
2049 dt_node_type_propagate(lp, dnp);
2050 dt_node_attr_assign(rp,
2051 dt_attr_min(lp->dn_attr, rp->dn_attr));
2052 break;
2053 case DT_TOK_ADD:
2054 dnp->dn_value = l + r;
2055 dt_node_promote(lp, rp, dnp);
2056 break;
2057 case DT_TOK_SUB:
2058 dnp->dn_value = l - r;
2059 dt_node_promote(lp, rp, dnp);
2060 break;
2061 case DT_TOK_MUL:
2062 dnp->dn_value = l * r;
2063 dt_node_promote(lp, rp, dnp);
2064 break;
2065 case DT_TOK_DIV:
2066 dt_node_promote(lp, rp, dnp);
2067 if (dnp->dn_flags & DT_NF_SIGNED)
2068 dnp->dn_value = (intmax_t)l / (intmax_t)r;
2069 else
2070 dnp->dn_value = l / r;
2071 break;
2072 case DT_TOK_MOD:
2073 dt_node_promote(lp, rp, dnp);
2074 if (dnp->dn_flags & DT_NF_SIGNED)
2075 dnp->dn_value = (intmax_t)l % (intmax_t)r;
2076 else
2077 dnp->dn_value = l % r;
2078 break;
2079 default:
2080 dt_node_free(dnp);
2081 dnp = NULL;
2084 if (dnp != NULL) {
2085 dt_node_free(lp);
2086 dt_node_free(rp);
2087 return (dnp);
2091 if (op == DT_TOK_LPAR && rp->dn_kind == DT_NODE_INT &&
2092 dt_node_is_integer(lp)) {
2093 dt_cast(lp, rp);
2094 dt_node_type_propagate(lp, rp);
2095 dt_node_attr_assign(rp, dt_attr_min(lp->dn_attr, rp->dn_attr));
2096 dt_node_free(lp);
2098 return (rp);
2102 * If no immediate optimizations are available, create an new OP2 node
2103 * and glue the left and right children into place and return.
2105 dnp = dt_node_alloc(DT_NODE_OP2);
2106 assert(op <= USHRT_MAX);
2107 dnp->dn_op = (ushort_t)op;
2108 dnp->dn_left = lp;
2109 dnp->dn_right = rp;
2111 return (dnp);
2114 dt_node_t *
2115 dt_node_op3(dt_node_t *expr, dt_node_t *lp, dt_node_t *rp)
2117 dt_node_t *dnp;
2119 if (expr->dn_kind == DT_NODE_INT)
2120 return (expr->dn_value != 0 ? lp : rp);
2122 dnp = dt_node_alloc(DT_NODE_OP3);
2123 dnp->dn_op = DT_TOK_QUESTION;
2124 dnp->dn_expr = expr;
2125 dnp->dn_left = lp;
2126 dnp->dn_right = rp;
2128 return (dnp);
2131 dt_node_t *
2132 dt_node_statement(dt_node_t *expr)
2134 dt_node_t *dnp;
2136 if (expr->dn_kind == DT_NODE_AGG)
2137 return (expr);
2139 if (expr->dn_kind == DT_NODE_FUNC &&
2140 expr->dn_ident->di_kind == DT_IDENT_ACTFUNC)
2141 dnp = dt_node_alloc(DT_NODE_DFUNC);
2142 else
2143 dnp = dt_node_alloc(DT_NODE_DEXPR);
2145 dnp->dn_expr = expr;
2146 return (dnp);
2149 dt_node_t *
2150 dt_node_if(dt_node_t *pred, dt_node_t *acts, dt_node_t *else_acts)
2152 dt_node_t *dnp = dt_node_alloc(DT_NODE_IF);
2153 dnp->dn_conditional = pred;
2154 dnp->dn_body = acts;
2155 dnp->dn_alternate_body = else_acts;
2157 return (dnp);
2160 dt_node_t *
2161 dt_node_pdesc_by_name(char *spec)
2163 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
2164 dt_node_t *dnp;
2166 if (spec == NULL)
2167 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2169 dnp = dt_node_alloc(DT_NODE_PDESC);
2170 dnp->dn_spec = spec;
2171 dnp->dn_desc = malloc(sizeof (dtrace_probedesc_t));
2173 if (dnp->dn_desc == NULL)
2174 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2176 if (dtrace_xstr2desc(dtp, yypcb->pcb_pspec, dnp->dn_spec,
2177 yypcb->pcb_sargc, yypcb->pcb_sargv, dnp->dn_desc) != 0) {
2178 xyerror(D_PDESC_INVAL, "invalid probe description \"%s\": %s\n",
2179 dnp->dn_spec, dtrace_errmsg(dtp, dtrace_errno(dtp)));
2182 free(dnp->dn_spec);
2183 dnp->dn_spec = NULL;
2185 return (dnp);
2188 dt_node_t *
2189 dt_node_pdesc_by_id(uintmax_t id)
2191 static const char *const names[] = {
2192 "providers", "modules", "functions"
2195 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
2196 dt_node_t *dnp = dt_node_alloc(DT_NODE_PDESC);
2198 if ((dnp->dn_desc = malloc(sizeof (dtrace_probedesc_t))) == NULL)
2199 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2201 if (id > UINT_MAX) {
2202 xyerror(D_PDESC_INVAL, "identifier %llu exceeds maximum "
2203 "probe id\n", (u_longlong_t)id);
2206 if (yypcb->pcb_pspec != DTRACE_PROBESPEC_NAME) {
2207 xyerror(D_PDESC_INVAL, "probe identifier %llu not permitted "
2208 "when specifying %s\n", (u_longlong_t)id,
2209 names[yypcb->pcb_pspec]);
2212 if (dtrace_id2desc(dtp, (dtrace_id_t)id, dnp->dn_desc) != 0) {
2213 xyerror(D_PDESC_INVAL, "invalid probe identifier %llu: %s\n",
2214 (u_longlong_t)id, dtrace_errmsg(dtp, dtrace_errno(dtp)));
2217 return (dnp);
2220 dt_node_t *
2221 dt_node_clause(dt_node_t *pdescs, dt_node_t *pred, dt_node_t *acts)
2223 dt_node_t *dnp = dt_node_alloc(DT_NODE_CLAUSE);
2225 dnp->dn_pdescs = pdescs;
2226 dnp->dn_pred = pred;
2227 dnp->dn_acts = acts;
2229 return (dnp);
2232 dt_node_t *
2233 dt_node_inline(dt_node_t *expr)
2235 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
2236 dt_scope_t *dsp = &yypcb->pcb_dstack;
2237 dt_decl_t *ddp = dt_decl_top();
2239 char n[DT_TYPE_NAMELEN];
2240 dtrace_typeinfo_t dtt;
2242 dt_ident_t *idp, *rdp;
2243 dt_idnode_t *inp;
2244 dt_node_t *dnp;
2246 if (dt_decl_type(ddp, &dtt) != 0)
2247 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
2249 if (dsp->ds_class != DT_DC_DEFAULT) {
2250 xyerror(D_DECL_BADCLASS, "specified storage class not "
2251 "appropriate for inline declaration\n");
2254 if (dsp->ds_ident == NULL)
2255 xyerror(D_DECL_USELESS, "inline declaration requires a name\n");
2257 if ((idp = dt_idstack_lookup(
2258 &yypcb->pcb_globals, dsp->ds_ident)) != NULL) {
2259 xyerror(D_DECL_IDRED, "identifier redefined: %s\n\t current: "
2260 "inline definition\n\tprevious: %s %s\n",
2261 idp->di_name, dt_idkind_name(idp->di_kind),
2262 (idp->di_flags & DT_IDFLG_INLINE) ? "inline" : "");
2266 * If we are declaring an inlined array, verify that we have a tuple
2267 * signature, and then recompute 'dtt' as the array's value type.
2269 if (ddp->dd_kind == CTF_K_ARRAY) {
2270 if (ddp->dd_node == NULL) {
2271 xyerror(D_DECL_ARRNULL, "inline declaration requires "
2272 "array tuple signature: %s\n", dsp->ds_ident);
2275 if (ddp->dd_node->dn_kind != DT_NODE_TYPE) {
2276 xyerror(D_DECL_ARRNULL, "inline declaration cannot be "
2277 "of scalar array type: %s\n", dsp->ds_ident);
2280 if (dt_decl_type(ddp->dd_next, &dtt) != 0)
2281 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
2285 * If the inline identifier is not defined, then create it with the
2286 * orphan flag set. We do not insert the identifier into dt_globals
2287 * until we have successfully cooked the right-hand expression, below.
2289 dnp = dt_node_alloc(DT_NODE_INLINE);
2290 dt_node_type_assign(dnp, dtt.dtt_ctfp, dtt.dtt_type, B_FALSE);
2291 dt_node_attr_assign(dnp, _dtrace_defattr);
2293 if (dt_node_is_void(dnp)) {
2294 xyerror(D_DECL_VOIDOBJ,
2295 "cannot declare void inline: %s\n", dsp->ds_ident);
2298 if (ctf_type_kind(dnp->dn_ctfp, ctf_type_resolve(
2299 dnp->dn_ctfp, dnp->dn_type)) == CTF_K_FORWARD) {
2300 xyerror(D_DECL_INCOMPLETE,
2301 "incomplete struct/union/enum %s: %s\n",
2302 dt_node_type_name(dnp, n, sizeof (n)), dsp->ds_ident);
2305 if ((inp = malloc(sizeof (dt_idnode_t))) == NULL)
2306 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2308 bzero(inp, sizeof (dt_idnode_t));
2310 idp = dnp->dn_ident = dt_ident_create(dsp->ds_ident,
2311 ddp->dd_kind == CTF_K_ARRAY ? DT_IDENT_ARRAY : DT_IDENT_SCALAR,
2312 DT_IDFLG_INLINE | DT_IDFLG_REF | DT_IDFLG_DECL | DT_IDFLG_ORPHAN, 0,
2313 _dtrace_defattr, 0, &dt_idops_inline, inp, dtp->dt_gen);
2315 if (idp == NULL) {
2316 free(inp);
2317 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2321 * If we're inlining an associative array, create a private identifier
2322 * hash containing the named parameters and store it in inp->din_hash.
2323 * We then push this hash on to the top of the pcb_globals stack.
2325 if (ddp->dd_kind == CTF_K_ARRAY) {
2326 dt_idnode_t *pinp;
2327 dt_ident_t *pidp;
2328 dt_node_t *pnp;
2329 uint_t i = 0;
2331 for (pnp = ddp->dd_node; pnp != NULL; pnp = pnp->dn_list)
2332 i++; /* count up parameters for din_argv[] */
2334 inp->din_hash = dt_idhash_create("inline args", NULL, 0, 0);
2335 inp->din_argv = calloc(i, sizeof (dt_ident_t *));
2337 if (inp->din_hash == NULL || inp->din_argv == NULL)
2338 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2341 * Create an identifier for each parameter as a scalar inline,
2342 * and store it in din_hash and in position in din_argv[]. The
2343 * parameter identifiers also use dt_idops_inline, but we leave
2344 * the dt_idnode_t argument 'pinp' zeroed. This will be filled
2345 * in by the code generation pass with references to the args.
2347 for (i = 0, pnp = ddp->dd_node;
2348 pnp != NULL; pnp = pnp->dn_list, i++) {
2350 if (pnp->dn_string == NULL)
2351 continue; /* ignore anonymous parameters */
2353 if ((pinp = malloc(sizeof (dt_idnode_t))) == NULL)
2354 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2356 pidp = dt_idhash_insert(inp->din_hash, pnp->dn_string,
2357 DT_IDENT_SCALAR, DT_IDFLG_DECL | DT_IDFLG_INLINE, 0,
2358 _dtrace_defattr, 0, &dt_idops_inline,
2359 pinp, dtp->dt_gen);
2361 if (pidp == NULL) {
2362 free(pinp);
2363 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2366 inp->din_argv[i] = pidp;
2367 bzero(pinp, sizeof (dt_idnode_t));
2368 dt_ident_type_assign(pidp, pnp->dn_ctfp, pnp->dn_type);
2371 dt_idstack_push(&yypcb->pcb_globals, inp->din_hash);
2375 * Unlike most constructors, we need to explicitly cook the right-hand
2376 * side of the inline definition immediately to prevent recursion. If
2377 * the right-hand side uses the inline itself, the cook will fail.
2379 expr = dt_node_cook(expr, DT_IDFLG_REF);
2381 if (ddp->dd_kind == CTF_K_ARRAY)
2382 dt_idstack_pop(&yypcb->pcb_globals, inp->din_hash);
2385 * Set the type, attributes, and flags for the inline. If the right-
2386 * hand expression has an identifier, propagate its flags. Then cook
2387 * the identifier to fully initialize it: if we're declaring an inline
2388 * associative array this will construct a type signature from 'ddp'.
2390 if (dt_node_is_dynamic(expr))
2391 rdp = dt_ident_resolve(expr->dn_ident);
2392 else if (expr->dn_kind == DT_NODE_VAR || expr->dn_kind == DT_NODE_SYM)
2393 rdp = expr->dn_ident;
2394 else
2395 rdp = NULL;
2397 if (rdp != NULL) {
2398 idp->di_flags |= (rdp->di_flags &
2399 (DT_IDFLG_WRITE | DT_IDFLG_USER | DT_IDFLG_PRIM));
2402 idp->di_attr = dt_attr_min(_dtrace_defattr, expr->dn_attr);
2403 dt_ident_type_assign(idp, dtt.dtt_ctfp, dtt.dtt_type);
2404 (void) dt_ident_cook(dnp, idp, &ddp->dd_node);
2407 * Store the parse tree nodes for 'expr' inside of idp->di_data ('inp')
2408 * so that they will be preserved with this identifier. Then pop the
2409 * inline declaration from the declaration stack and restore the lexer.
2411 inp->din_list = yypcb->pcb_list;
2412 inp->din_root = expr;
2414 dt_decl_free(dt_decl_pop());
2415 yybegin(YYS_CLAUSE);
2418 * Finally, insert the inline identifier into dt_globals to make it
2419 * visible, and then cook 'dnp' to check its type against 'expr'.
2421 dt_idhash_xinsert(dtp->dt_globals, idp);
2422 return (dt_node_cook(dnp, DT_IDFLG_REF));
2425 dt_node_t *
2426 dt_node_member(dt_decl_t *ddp, char *name, dt_node_t *expr)
2428 dtrace_typeinfo_t dtt;
2429 dt_node_t *dnp;
2430 int err;
2432 if (ddp != NULL) {
2433 err = dt_decl_type(ddp, &dtt);
2434 dt_decl_free(ddp);
2436 if (err != 0)
2437 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
2440 dnp = dt_node_alloc(DT_NODE_MEMBER);
2441 dnp->dn_membname = name;
2442 dnp->dn_membexpr = expr;
2444 if (ddp != NULL)
2445 dt_node_type_assign(dnp, dtt.dtt_ctfp, dtt.dtt_type,
2446 dtt.dtt_flags & DTT_FL_USER ? B_TRUE : B_FALSE);
2448 return (dnp);
2451 dt_node_t *
2452 dt_node_xlator(dt_decl_t *ddp, dt_decl_t *sdp, char *name, dt_node_t *members)
2454 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
2455 dtrace_typeinfo_t src, dst;
2456 dt_node_t sn, dn;
2457 dt_xlator_t *dxp;
2458 dt_node_t *dnp;
2459 int edst, esrc;
2460 uint_t kind;
2462 char n1[DT_TYPE_NAMELEN];
2463 char n2[DT_TYPE_NAMELEN];
2465 edst = dt_decl_type(ddp, &dst);
2466 dt_decl_free(ddp);
2468 esrc = dt_decl_type(sdp, &src);
2469 dt_decl_free(sdp);
2471 if (edst != 0 || esrc != 0) {
2472 free(name);
2473 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
2476 bzero(&sn, sizeof (sn));
2477 dt_node_type_assign(&sn, src.dtt_ctfp, src.dtt_type, B_FALSE);
2479 bzero(&dn, sizeof (dn));
2480 dt_node_type_assign(&dn, dst.dtt_ctfp, dst.dtt_type, B_FALSE);
2482 if (dt_xlator_lookup(dtp, &sn, &dn, DT_XLATE_EXACT) != NULL) {
2483 xyerror(D_XLATE_REDECL,
2484 "translator from %s to %s has already been declared\n",
2485 dt_node_type_name(&sn, n1, sizeof (n1)),
2486 dt_node_type_name(&dn, n2, sizeof (n2)));
2489 kind = ctf_type_kind(dst.dtt_ctfp,
2490 ctf_type_resolve(dst.dtt_ctfp, dst.dtt_type));
2492 if (kind == CTF_K_FORWARD) {
2493 xyerror(D_XLATE_SOU, "incomplete struct/union/enum %s\n",
2494 dt_type_name(dst.dtt_ctfp, dst.dtt_type, n1, sizeof (n1)));
2497 if (kind != CTF_K_STRUCT && kind != CTF_K_UNION) {
2498 xyerror(D_XLATE_SOU,
2499 "translator output type must be a struct or union\n");
2502 dxp = dt_xlator_create(dtp, &src, &dst, name, members, yypcb->pcb_list);
2503 yybegin(YYS_CLAUSE);
2504 free(name);
2506 if (dxp == NULL)
2507 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2509 dnp = dt_node_alloc(DT_NODE_XLATOR);
2510 dnp->dn_xlator = dxp;
2511 dnp->dn_members = members;
2513 return (dt_node_cook(dnp, DT_IDFLG_REF));
2516 dt_node_t *
2517 dt_node_probe(char *s, int protoc, dt_node_t *nargs, dt_node_t *xargs)
2519 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
2520 int nargc, xargc;
2521 dt_node_t *dnp;
2523 size_t len = strlen(s) + 3; /* +3 for :: and \0 */
2524 char *name = alloca(len);
2526 (void) snprintf(name, len, "::%s", s);
2527 (void) strhyphenate(name);
2528 free(s);
2530 if (strchr(name, '`') != NULL) {
2531 xyerror(D_PROV_BADNAME, "probe name may not "
2532 "contain scoping operator: %s\n", name);
2535 if (strlen(name) - 2 >= DTRACE_NAMELEN) {
2536 xyerror(D_PROV_BADNAME, "probe name may not exceed %d "
2537 "characters: %s\n", DTRACE_NAMELEN - 1, name);
2540 dnp = dt_node_alloc(DT_NODE_PROBE);
2542 dnp->dn_ident = dt_ident_create(name, DT_IDENT_PROBE,
2543 DT_IDFLG_ORPHAN, DTRACE_IDNONE, _dtrace_defattr, 0,
2544 &dt_idops_probe, NULL, dtp->dt_gen);
2546 nargc = dt_decl_prototype(nargs, nargs,
2547 "probe input", DT_DP_VOID | DT_DP_ANON);
2549 xargc = dt_decl_prototype(xargs, nargs,
2550 "probe output", DT_DP_VOID);
2552 if (nargc > UINT8_MAX) {
2553 xyerror(D_PROV_PRARGLEN, "probe %s input prototype exceeds %u "
2554 "parameters: %d params used\n", name, UINT8_MAX, nargc);
2557 if (xargc > UINT8_MAX) {
2558 xyerror(D_PROV_PRARGLEN, "probe %s output prototype exceeds %u "
2559 "parameters: %d params used\n", name, UINT8_MAX, xargc);
2562 if (dnp->dn_ident == NULL || dt_probe_create(dtp,
2563 dnp->dn_ident, protoc, nargs, nargc, xargs, xargc) == NULL)
2564 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2566 return (dnp);
2569 dt_node_t *
2570 dt_node_provider(char *name, dt_node_t *probes)
2572 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
2573 dt_node_t *dnp = dt_node_alloc(DT_NODE_PROVIDER);
2574 dt_node_t *lnp;
2575 size_t len;
2577 dnp->dn_provname = name;
2578 dnp->dn_probes = probes;
2580 if (strchr(name, '`') != NULL) {
2581 dnerror(dnp, D_PROV_BADNAME, "provider name may not "
2582 "contain scoping operator: %s\n", name);
2585 if ((len = strlen(name)) >= DTRACE_PROVNAMELEN) {
2586 dnerror(dnp, D_PROV_BADNAME, "provider name may not exceed %d "
2587 "characters: %s\n", DTRACE_PROVNAMELEN - 1, name);
2590 if (isdigit(name[len - 1])) {
2591 dnerror(dnp, D_PROV_BADNAME, "provider name may not "
2592 "end with a digit: %s\n", name);
2596 * Check to see if the provider is already defined or visible through
2597 * dtrace(7D). If so, set dn_provred to treat it as a re-declaration.
2598 * If not, create a new provider and set its interface-only flag. This
2599 * flag may be cleared later by calls made to dt_probe_declare().
2601 if ((dnp->dn_provider = dt_provider_lookup(dtp, name)) != NULL)
2602 dnp->dn_provred = B_TRUE;
2603 else if ((dnp->dn_provider = dt_provider_create(dtp, name)) == NULL)
2604 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2605 else
2606 dnp->dn_provider->pv_flags |= DT_PROVIDER_INTF;
2609 * Store all parse nodes created since we consumed the DT_KEY_PROVIDER
2610 * token with the provider and then restore our lexing state to CLAUSE.
2611 * Note that if dnp->dn_provred is true, we may end up storing dups of
2612 * a provider's interface and implementation: we eat this space because
2613 * the implementation will likely need to redeclare probe members, and
2614 * therefore may result in those member nodes becoming persistent.
2616 for (lnp = yypcb->pcb_list; lnp->dn_link != NULL; lnp = lnp->dn_link)
2617 continue; /* skip to end of allocation list */
2619 lnp->dn_link = dnp->dn_provider->pv_nodes;
2620 dnp->dn_provider->pv_nodes = yypcb->pcb_list;
2622 yybegin(YYS_CLAUSE);
2623 return (dnp);
2626 dt_node_t *
2627 dt_node_program(dt_node_t *lnp)
2629 dt_node_t *dnp = dt_node_alloc(DT_NODE_PROG);
2630 dnp->dn_list = lnp;
2631 return (dnp);
2635 * This function provides the underlying implementation of cooking an
2636 * identifier given its node, a hash of dynamic identifiers, an identifier
2637 * kind, and a boolean flag indicating whether we are allowed to instantiate
2638 * a new identifier if the string is not found. This function is either
2639 * called from dt_cook_ident(), below, or directly by the various cooking
2640 * routines that are allowed to instantiate identifiers (e.g. op2 TOK_ASGN).
2642 static void
2643 dt_xcook_ident(dt_node_t *dnp, dt_idhash_t *dhp, uint_t idkind, int create)
2645 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
2646 const char *sname = dt_idhash_name(dhp);
2647 int uref = 0;
2649 dtrace_attribute_t attr = _dtrace_defattr;
2650 dt_ident_t *idp;
2651 dtrace_syminfo_t dts;
2652 GElf_Sym sym;
2654 const char *scope, *mark;
2655 uchar_t dnkind;
2656 char *name;
2659 * Look for scoping marks in the identifier. If one is found, set our
2660 * scope to either DTRACE_OBJ_KMODS or UMODS or to the first part of
2661 * the string that specifies the scope using an explicit module name.
2662 * If two marks in a row are found, set 'uref' (user symbol reference).
2663 * Otherwise we set scope to DTRACE_OBJ_EXEC, indicating that normal
2664 * scope is desired and we should search the specified idhash.
2666 if ((name = strrchr(dnp->dn_string, '`')) != NULL) {
2667 if (name > dnp->dn_string && name[-1] == '`') {
2668 uref++;
2669 name[-1] = '\0';
2672 if (name == dnp->dn_string + uref)
2673 scope = uref ? DTRACE_OBJ_UMODS : DTRACE_OBJ_KMODS;
2674 else
2675 scope = dnp->dn_string;
2677 *name++ = '\0'; /* leave name pointing after scoping mark */
2678 dnkind = DT_NODE_VAR;
2680 } else if (idkind == DT_IDENT_AGG) {
2681 scope = DTRACE_OBJ_EXEC;
2682 name = dnp->dn_string + 1;
2683 dnkind = DT_NODE_AGG;
2684 } else {
2685 scope = DTRACE_OBJ_EXEC;
2686 name = dnp->dn_string;
2687 dnkind = DT_NODE_VAR;
2691 * If create is set to false, and we fail our idhash lookup, preset
2692 * the errno code to EDT_NOVAR for our final error message below.
2693 * If we end up calling dtrace_lookup_by_name(), it will reset the
2694 * errno appropriately and that error will be reported instead.
2696 (void) dt_set_errno(dtp, EDT_NOVAR);
2697 mark = uref ? "``" : "`";
2699 if (scope == DTRACE_OBJ_EXEC && (
2700 (dhp != dtp->dt_globals &&
2701 (idp = dt_idhash_lookup(dhp, name)) != NULL) ||
2702 (dhp == dtp->dt_globals &&
2703 (idp = dt_idstack_lookup(&yypcb->pcb_globals, name)) != NULL))) {
2705 * Check that we are referencing the ident in the manner that
2706 * matches its type if this is a global lookup. In the TLS or
2707 * local case, we don't know how the ident will be used until
2708 * the time operator -> is seen; more parsing is needed.
2710 if (idp->di_kind != idkind && dhp == dtp->dt_globals) {
2711 xyerror(D_IDENT_BADREF, "%s '%s' may not be referenced "
2712 "as %s\n", dt_idkind_name(idp->di_kind),
2713 idp->di_name, dt_idkind_name(idkind));
2717 * Arrays and aggregations are not cooked individually. They
2718 * have dynamic types and must be referenced using operator [].
2719 * This is handled explicitly by the code for DT_TOK_LBRAC.
2721 if (idp->di_kind != DT_IDENT_ARRAY &&
2722 idp->di_kind != DT_IDENT_AGG)
2723 attr = dt_ident_cook(dnp, idp, NULL);
2724 else {
2725 dt_node_type_assign(dnp,
2726 DT_DYN_CTFP(dtp), DT_DYN_TYPE(dtp), B_FALSE);
2727 attr = idp->di_attr;
2730 free(dnp->dn_string);
2731 dnp->dn_string = NULL;
2732 dnp->dn_kind = dnkind;
2733 dnp->dn_ident = idp;
2734 dnp->dn_flags |= DT_NF_LVALUE;
2736 if (idp->di_flags & DT_IDFLG_WRITE)
2737 dnp->dn_flags |= DT_NF_WRITABLE;
2739 dt_node_attr_assign(dnp, attr);
2741 } else if (dhp == dtp->dt_globals && scope != DTRACE_OBJ_EXEC &&
2742 dtrace_lookup_by_name(dtp, scope, name, &sym, &dts) == 0) {
2744 dt_module_t *mp = dt_module_lookup_by_name(dtp, dts.dts_object);
2745 int umod = (mp->dm_flags & DT_DM_KERNEL) == 0;
2746 static const char *const kunames[] = { "kernel", "user" };
2748 dtrace_typeinfo_t dtt;
2749 dtrace_syminfo_t *sip;
2751 if (uref ^ umod) {
2752 xyerror(D_SYM_BADREF, "%s module '%s' symbol '%s' may "
2753 "not be referenced as a %s symbol\n", kunames[umod],
2754 dts.dts_object, dts.dts_name, kunames[uref]);
2757 if (dtrace_symbol_type(dtp, &sym, &dts, &dtt) != 0) {
2759 * For now, we special-case EDT_DATAMODEL to clarify
2760 * that mixed data models are not currently supported.
2762 if (dtp->dt_errno == EDT_DATAMODEL) {
2763 xyerror(D_SYM_MODEL, "cannot use %s symbol "
2764 "%s%s%s in a %s D program\n",
2765 dt_module_modelname(mp),
2766 dts.dts_object, mark, dts.dts_name,
2767 dt_module_modelname(dtp->dt_ddefs));
2770 xyerror(D_SYM_NOTYPES,
2771 "no symbolic type information is available for "
2772 "%s%s%s: %s\n", dts.dts_object, mark, dts.dts_name,
2773 dtrace_errmsg(dtp, dtrace_errno(dtp)));
2776 idp = dt_ident_create(name, DT_IDENT_SYMBOL, 0, 0,
2777 _dtrace_symattr, 0, &dt_idops_thaw, NULL, dtp->dt_gen);
2779 if (idp == NULL)
2780 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2782 if (mp->dm_flags & DT_DM_PRIMARY)
2783 idp->di_flags |= DT_IDFLG_PRIM;
2785 idp->di_next = dtp->dt_externs;
2786 dtp->dt_externs = idp;
2788 if ((sip = malloc(sizeof (dtrace_syminfo_t))) == NULL)
2789 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2791 bcopy(&dts, sip, sizeof (dtrace_syminfo_t));
2792 idp->di_data = sip;
2793 idp->di_ctfp = dtt.dtt_ctfp;
2794 idp->di_type = dtt.dtt_type;
2796 free(dnp->dn_string);
2797 dnp->dn_string = NULL;
2798 dnp->dn_kind = DT_NODE_SYM;
2799 dnp->dn_ident = idp;
2800 dnp->dn_flags |= DT_NF_LVALUE;
2802 dt_node_type_assign(dnp, dtt.dtt_ctfp, dtt.dtt_type,
2803 dtt.dtt_flags & DTT_FL_USER ? B_TRUE : B_FALSE);
2804 dt_node_attr_assign(dnp, _dtrace_symattr);
2806 if (uref) {
2807 idp->di_flags |= DT_IDFLG_USER;
2808 dnp->dn_flags |= DT_NF_USERLAND;
2811 } else if (scope == DTRACE_OBJ_EXEC && create == B_TRUE) {
2812 uint_t flags = DT_IDFLG_WRITE;
2813 uint_t id;
2815 if (dt_idhash_nextid(dhp, &id) == -1) {
2816 xyerror(D_ID_OFLOW, "cannot create %s: limit on number "
2817 "of %s variables exceeded\n", name, sname);
2820 if (dhp == yypcb->pcb_locals)
2821 flags |= DT_IDFLG_LOCAL;
2822 else if (dhp == dtp->dt_tls)
2823 flags |= DT_IDFLG_TLS;
2825 dt_dprintf("create %s %s variable %s, id=%u\n",
2826 sname, dt_idkind_name(idkind), name, id);
2828 if (idkind == DT_IDENT_ARRAY || idkind == DT_IDENT_AGG) {
2829 idp = dt_idhash_insert(dhp, name,
2830 idkind, flags, id, _dtrace_defattr, 0,
2831 &dt_idops_assc, NULL, dtp->dt_gen);
2832 } else {
2833 idp = dt_idhash_insert(dhp, name,
2834 idkind, flags, id, _dtrace_defattr, 0,
2835 &dt_idops_thaw, NULL, dtp->dt_gen);
2838 if (idp == NULL)
2839 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2842 * Arrays and aggregations are not cooked individually. They
2843 * have dynamic types and must be referenced using operator [].
2844 * This is handled explicitly by the code for DT_TOK_LBRAC.
2846 if (idp->di_kind != DT_IDENT_ARRAY &&
2847 idp->di_kind != DT_IDENT_AGG)
2848 attr = dt_ident_cook(dnp, idp, NULL);
2849 else {
2850 dt_node_type_assign(dnp,
2851 DT_DYN_CTFP(dtp), DT_DYN_TYPE(dtp), B_FALSE);
2852 attr = idp->di_attr;
2855 free(dnp->dn_string);
2856 dnp->dn_string = NULL;
2857 dnp->dn_kind = dnkind;
2858 dnp->dn_ident = idp;
2859 dnp->dn_flags |= DT_NF_LVALUE | DT_NF_WRITABLE;
2861 dt_node_attr_assign(dnp, attr);
2863 } else if (scope != DTRACE_OBJ_EXEC) {
2864 xyerror(D_IDENT_UNDEF, "failed to resolve %s%s%s: %s\n",
2865 dnp->dn_string, mark, name,
2866 dtrace_errmsg(dtp, dtrace_errno(dtp)));
2867 } else {
2868 xyerror(D_IDENT_UNDEF, "failed to resolve %s: %s\n",
2869 dnp->dn_string, dtrace_errmsg(dtp, dtrace_errno(dtp)));
2873 static dt_node_t *
2874 dt_cook_ident(dt_node_t *dnp, uint_t idflags)
2876 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
2878 if (dnp->dn_op == DT_TOK_AGG)
2879 dt_xcook_ident(dnp, dtp->dt_aggs, DT_IDENT_AGG, B_FALSE);
2880 else
2881 dt_xcook_ident(dnp, dtp->dt_globals, DT_IDENT_SCALAR, B_FALSE);
2883 return (dt_node_cook(dnp, idflags));
2887 * Since operators [ and -> can instantiate new variables before we know
2888 * whether the reference is for a read or a write, we need to check read
2889 * references to determine if the identifier is currently dt_ident_unref().
2890 * If so, we report that this first access was to an undefined variable.
2892 static dt_node_t *
2893 dt_cook_var(dt_node_t *dnp, uint_t idflags)
2895 dt_ident_t *idp = dnp->dn_ident;
2897 if ((idflags & DT_IDFLG_REF) && dt_ident_unref(idp)) {
2898 dnerror(dnp, D_VAR_UNDEF,
2899 "%s%s has not yet been declared or assigned\n",
2900 (idp->di_flags & DT_IDFLG_LOCAL) ? "this->" :
2901 (idp->di_flags & DT_IDFLG_TLS) ? "self->" : "",
2902 idp->di_name);
2905 dt_node_attr_assign(dnp, dt_ident_cook(dnp, idp, &dnp->dn_args));
2906 return (dnp);
2909 /*ARGSUSED*/
2910 static dt_node_t *
2911 dt_cook_func(dt_node_t *dnp, uint_t idflags)
2913 dt_node_attr_assign(dnp,
2914 dt_ident_cook(dnp, dnp->dn_ident, &dnp->dn_args));
2916 return (dnp);
2919 static dt_node_t *
2920 dt_cook_op1(dt_node_t *dnp, uint_t idflags)
2922 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
2923 dt_node_t *cp = dnp->dn_child;
2925 char n[DT_TYPE_NAMELEN];
2926 dtrace_typeinfo_t dtt;
2927 dt_ident_t *idp;
2929 ctf_encoding_t e;
2930 ctf_arinfo_t r;
2931 ctf_id_t type, base;
2932 uint_t kind;
2934 if (dnp->dn_op == DT_TOK_PREINC || dnp->dn_op == DT_TOK_POSTINC ||
2935 dnp->dn_op == DT_TOK_PREDEC || dnp->dn_op == DT_TOK_POSTDEC)
2936 idflags = DT_IDFLG_REF | DT_IDFLG_MOD;
2937 else
2938 idflags = DT_IDFLG_REF;
2941 * We allow the unary ++ and -- operators to instantiate new scalar
2942 * variables if applied to an identifier; otherwise just cook as usual.
2944 if (cp->dn_kind == DT_NODE_IDENT && (idflags & DT_IDFLG_MOD))
2945 dt_xcook_ident(cp, dtp->dt_globals, DT_IDENT_SCALAR, B_TRUE);
2947 cp = dnp->dn_child = dt_node_cook(cp, 0); /* don't set idflags yet */
2949 if (cp->dn_kind == DT_NODE_VAR && dt_ident_unref(cp->dn_ident)) {
2950 if (dt_type_lookup("int64_t", &dtt) != 0)
2951 xyerror(D_TYPE_ERR, "failed to lookup int64_t\n");
2953 dt_ident_type_assign(cp->dn_ident, dtt.dtt_ctfp, dtt.dtt_type);
2954 dt_node_type_assign(cp, dtt.dtt_ctfp, dtt.dtt_type,
2955 dtt.dtt_flags & DTT_FL_USER ? B_TRUE : B_FALSE);
2958 if (cp->dn_kind == DT_NODE_VAR)
2959 cp->dn_ident->di_flags |= idflags;
2961 switch (dnp->dn_op) {
2962 case DT_TOK_DEREF:
2964 * If the deref operator is applied to a translated pointer,
2965 * we set our output type to the output of the translation.
2967 if ((idp = dt_node_resolve(cp, DT_IDENT_XLPTR)) != NULL) {
2968 dt_xlator_t *dxp = idp->di_data;
2970 dnp->dn_ident = &dxp->dx_souid;
2971 dt_node_type_assign(dnp,
2972 dnp->dn_ident->di_ctfp, dnp->dn_ident->di_type,
2973 cp->dn_flags & DT_NF_USERLAND);
2974 break;
2977 type = ctf_type_resolve(cp->dn_ctfp, cp->dn_type);
2978 kind = ctf_type_kind(cp->dn_ctfp, type);
2980 if (kind == CTF_K_ARRAY) {
2981 if (ctf_array_info(cp->dn_ctfp, type, &r) != 0) {
2982 dtp->dt_ctferr = ctf_errno(cp->dn_ctfp);
2983 longjmp(yypcb->pcb_jmpbuf, EDT_CTF);
2984 } else
2985 type = r.ctr_contents;
2986 } else if (kind == CTF_K_POINTER) {
2987 type = ctf_type_reference(cp->dn_ctfp, type);
2988 } else {
2989 xyerror(D_DEREF_NONPTR,
2990 "cannot dereference non-pointer type\n");
2993 dt_node_type_assign(dnp, cp->dn_ctfp, type,
2994 cp->dn_flags & DT_NF_USERLAND);
2995 base = ctf_type_resolve(cp->dn_ctfp, type);
2996 kind = ctf_type_kind(cp->dn_ctfp, base);
2998 if (kind == CTF_K_INTEGER && ctf_type_encoding(cp->dn_ctfp,
2999 base, &e) == 0 && IS_VOID(e)) {
3000 xyerror(D_DEREF_VOID,
3001 "cannot dereference pointer to void\n");
3004 if (kind == CTF_K_FUNCTION) {
3005 xyerror(D_DEREF_FUNC,
3006 "cannot dereference pointer to function\n");
3009 if (kind != CTF_K_ARRAY || dt_node_is_string(dnp))
3010 dnp->dn_flags |= DT_NF_LVALUE; /* see K&R[A7.4.3] */
3013 * If we propagated the l-value bit and the child operand was
3014 * a writable D variable or a binary operation of the form
3015 * a + b where a is writable, then propagate the writable bit.
3016 * This is necessary to permit assignments to scalar arrays,
3017 * which are converted to expressions of the form *(a + i).
3019 if ((cp->dn_flags & DT_NF_WRITABLE) ||
3020 (cp->dn_kind == DT_NODE_OP2 && cp->dn_op == DT_TOK_ADD &&
3021 (cp->dn_left->dn_flags & DT_NF_WRITABLE)))
3022 dnp->dn_flags |= DT_NF_WRITABLE;
3024 if ((cp->dn_flags & DT_NF_USERLAND) &&
3025 (kind == CTF_K_POINTER || (dnp->dn_flags & DT_NF_REF)))
3026 dnp->dn_flags |= DT_NF_USERLAND;
3027 break;
3029 case DT_TOK_IPOS:
3030 case DT_TOK_INEG:
3031 if (!dt_node_is_arith(cp)) {
3032 xyerror(D_OP_ARITH, "operator %s requires an operand "
3033 "of arithmetic type\n", opstr(dnp->dn_op));
3035 dt_node_type_propagate(cp, dnp); /* see K&R[A7.4.4-6] */
3036 break;
3038 case DT_TOK_BNEG:
3039 if (!dt_node_is_integer(cp)) {
3040 xyerror(D_OP_INT, "operator %s requires an operand of "
3041 "integral type\n", opstr(dnp->dn_op));
3043 dt_node_type_propagate(cp, dnp); /* see K&R[A7.4.4-6] */
3044 break;
3046 case DT_TOK_LNEG:
3047 if (!dt_node_is_scalar(cp)) {
3048 xyerror(D_OP_SCALAR, "operator %s requires an operand "
3049 "of scalar type\n", opstr(dnp->dn_op));
3051 dt_node_type_assign(dnp, DT_INT_CTFP(dtp), DT_INT_TYPE(dtp),
3052 B_FALSE);
3053 break;
3055 case DT_TOK_ADDROF:
3056 if (cp->dn_kind == DT_NODE_VAR || cp->dn_kind == DT_NODE_AGG) {
3057 xyerror(D_ADDROF_VAR,
3058 "cannot take address of dynamic variable\n");
3061 if (dt_node_is_dynamic(cp)) {
3062 xyerror(D_ADDROF_VAR,
3063 "cannot take address of dynamic object\n");
3066 if (!(cp->dn_flags & DT_NF_LVALUE)) {
3067 xyerror(D_ADDROF_LVAL, /* see K&R[A7.4.2] */
3068 "unacceptable operand for unary & operator\n");
3071 if (cp->dn_flags & DT_NF_BITFIELD) {
3072 xyerror(D_ADDROF_BITFIELD,
3073 "cannot take address of bit-field\n");
3076 dtt.dtt_object = NULL;
3077 dtt.dtt_ctfp = cp->dn_ctfp;
3078 dtt.dtt_type = cp->dn_type;
3080 if (dt_type_pointer(&dtt) == -1) {
3081 xyerror(D_TYPE_ERR, "cannot find type for \"&\": %s*\n",
3082 dt_node_type_name(cp, n, sizeof (n)));
3085 dt_node_type_assign(dnp, dtt.dtt_ctfp, dtt.dtt_type,
3086 cp->dn_flags & DT_NF_USERLAND);
3087 break;
3089 case DT_TOK_SIZEOF:
3090 if (cp->dn_flags & DT_NF_BITFIELD) {
3091 xyerror(D_SIZEOF_BITFIELD,
3092 "cannot apply sizeof to a bit-field\n");
3095 if (dt_node_sizeof(cp) == 0) {
3096 xyerror(D_SIZEOF_TYPE, "cannot apply sizeof to an "
3097 "operand of unknown size\n");
3100 dt_node_type_assign(dnp, dtp->dt_ddefs->dm_ctfp,
3101 ctf_lookup_by_name(dtp->dt_ddefs->dm_ctfp, "size_t"),
3102 B_FALSE);
3103 break;
3105 case DT_TOK_STRINGOF:
3106 if (!dt_node_is_scalar(cp) && !dt_node_is_pointer(cp) &&
3107 !dt_node_is_strcompat(cp)) {
3108 xyerror(D_STRINGOF_TYPE,
3109 "cannot apply stringof to a value of type %s\n",
3110 dt_node_type_name(cp, n, sizeof (n)));
3112 dt_node_type_assign(dnp, DT_STR_CTFP(dtp), DT_STR_TYPE(dtp),
3113 cp->dn_flags & DT_NF_USERLAND);
3114 break;
3116 case DT_TOK_PREINC:
3117 case DT_TOK_POSTINC:
3118 case DT_TOK_PREDEC:
3119 case DT_TOK_POSTDEC:
3120 if (dt_node_is_scalar(cp) == 0) {
3121 xyerror(D_OP_SCALAR, "operator %s requires operand of "
3122 "scalar type\n", opstr(dnp->dn_op));
3125 if (dt_node_is_vfptr(cp)) {
3126 xyerror(D_OP_VFPTR, "operator %s requires an operand "
3127 "of known size\n", opstr(dnp->dn_op));
3130 if (!(cp->dn_flags & DT_NF_LVALUE)) {
3131 xyerror(D_OP_LVAL, "operator %s requires modifiable "
3132 "lvalue as an operand\n", opstr(dnp->dn_op));
3135 if (!(cp->dn_flags & DT_NF_WRITABLE)) {
3136 xyerror(D_OP_WRITE, "operator %s can only be applied "
3137 "to a writable variable\n", opstr(dnp->dn_op));
3140 dt_node_type_propagate(cp, dnp); /* see K&R[A7.4.1] */
3141 break;
3143 default:
3144 xyerror(D_UNKNOWN, "invalid unary op %s\n", opstr(dnp->dn_op));
3147 dt_node_attr_assign(dnp, cp->dn_attr);
3148 return (dnp);
3151 static void
3152 dt_assign_common(dt_node_t *dnp)
3154 dt_node_t *lp = dnp->dn_left;
3155 dt_node_t *rp = dnp->dn_right;
3156 int op = dnp->dn_op;
3158 if (rp->dn_kind == DT_NODE_INT)
3159 dt_cast(lp, rp);
3161 if (!(lp->dn_flags & DT_NF_LVALUE)) {
3162 xyerror(D_OP_LVAL, "operator %s requires modifiable "
3163 "lvalue as an operand\n", opstr(op));
3164 /* see K&R[A7.17] */
3167 if (!(lp->dn_flags & DT_NF_WRITABLE)) {
3168 xyerror(D_OP_WRITE, "operator %s can only be applied "
3169 "to a writable variable\n", opstr(op));
3172 dt_node_type_propagate(lp, dnp); /* see K&R[A7.17] */
3173 dt_node_attr_assign(dnp, dt_attr_min(lp->dn_attr, rp->dn_attr));
3176 static dt_node_t *
3177 dt_cook_op2(dt_node_t *dnp, uint_t idflags)
3179 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
3180 dt_node_t *lp = dnp->dn_left;
3181 dt_node_t *rp = dnp->dn_right;
3182 int op = dnp->dn_op;
3184 ctf_membinfo_t m;
3185 ctf_file_t *ctfp;
3186 ctf_id_t type;
3187 int kind, val, uref;
3188 dt_ident_t *idp;
3190 char n1[DT_TYPE_NAMELEN];
3191 char n2[DT_TYPE_NAMELEN];
3194 * The expression E1[E2] is identical by definition to *((E1)+(E2)) so
3195 * we convert "[" to "+" and glue on "*" at the end (see K&R[A7.3.1])
3196 * unless the left-hand side is an untyped D scalar, associative array,
3197 * or aggregation. In these cases, we proceed to case DT_TOK_LBRAC and
3198 * handle associative array and aggregation references there.
3200 if (op == DT_TOK_LBRAC) {
3201 if (lp->dn_kind == DT_NODE_IDENT) {
3202 dt_idhash_t *dhp;
3203 uint_t idkind;
3205 if (lp->dn_op == DT_TOK_AGG) {
3206 dhp = dtp->dt_aggs;
3207 idp = dt_idhash_lookup(dhp, lp->dn_string + 1);
3208 idkind = DT_IDENT_AGG;
3209 } else {
3210 dhp = dtp->dt_globals;
3211 idp = dt_idstack_lookup(
3212 &yypcb->pcb_globals, lp->dn_string);
3213 idkind = DT_IDENT_ARRAY;
3216 if (idp == NULL || dt_ident_unref(idp))
3217 dt_xcook_ident(lp, dhp, idkind, B_TRUE);
3218 else
3219 dt_xcook_ident(lp, dhp, idp->di_kind, B_FALSE);
3220 } else {
3221 lp = dnp->dn_left = dt_node_cook(lp, 0);
3225 * Switch op to '+' for *(E1 + E2) array mode in these cases:
3226 * (a) lp is a DT_IDENT_ARRAY variable that has already been
3227 * referenced using [] notation (dn_args != NULL).
3228 * (b) lp is a non-ARRAY variable that has already been given
3229 * a type by assignment or declaration (!dt_ident_unref())
3230 * (c) lp is neither a variable nor an aggregation
3232 if (lp->dn_kind == DT_NODE_VAR) {
3233 if (lp->dn_ident->di_kind == DT_IDENT_ARRAY) {
3234 if (lp->dn_args != NULL)
3235 op = DT_TOK_ADD;
3236 } else if (!dt_ident_unref(lp->dn_ident)) {
3237 op = DT_TOK_ADD;
3239 } else if (lp->dn_kind != DT_NODE_AGG) {
3240 op = DT_TOK_ADD;
3244 switch (op) {
3245 case DT_TOK_BAND:
3246 case DT_TOK_XOR:
3247 case DT_TOK_BOR:
3248 lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF);
3249 rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
3251 if (!dt_node_is_integer(lp) || !dt_node_is_integer(rp)) {
3252 xyerror(D_OP_INT, "operator %s requires operands of "
3253 "integral type\n", opstr(op));
3256 dt_node_promote(lp, rp, dnp); /* see K&R[A7.11-13] */
3257 break;
3259 case DT_TOK_LSH:
3260 case DT_TOK_RSH:
3261 lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF);
3262 rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
3264 if (!dt_node_is_integer(lp) || !dt_node_is_integer(rp)) {
3265 xyerror(D_OP_INT, "operator %s requires operands of "
3266 "integral type\n", opstr(op));
3269 dt_node_type_propagate(lp, dnp); /* see K&R[A7.8] */
3270 dt_node_attr_assign(dnp, dt_attr_min(lp->dn_attr, rp->dn_attr));
3271 break;
3273 case DT_TOK_MOD:
3274 lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF);
3275 rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
3277 if (!dt_node_is_integer(lp) || !dt_node_is_integer(rp)) {
3278 xyerror(D_OP_INT, "operator %s requires operands of "
3279 "integral type\n", opstr(op));
3282 dt_node_promote(lp, rp, dnp); /* see K&R[A7.6] */
3283 break;
3285 case DT_TOK_MUL:
3286 case DT_TOK_DIV:
3287 lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF);
3288 rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
3290 if (!dt_node_is_arith(lp) || !dt_node_is_arith(rp)) {
3291 xyerror(D_OP_ARITH, "operator %s requires operands of "
3292 "arithmetic type\n", opstr(op));
3295 dt_node_promote(lp, rp, dnp); /* see K&R[A7.6] */
3296 break;
3298 case DT_TOK_LAND:
3299 case DT_TOK_LXOR:
3300 case DT_TOK_LOR:
3301 lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF);
3302 rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
3304 if (!dt_node_is_scalar(lp) || !dt_node_is_scalar(rp)) {
3305 xyerror(D_OP_SCALAR, "operator %s requires operands "
3306 "of scalar type\n", opstr(op));
3309 dt_node_type_assign(dnp, DT_INT_CTFP(dtp), DT_INT_TYPE(dtp),
3310 B_FALSE);
3311 dt_node_attr_assign(dnp, dt_attr_min(lp->dn_attr, rp->dn_attr));
3312 break;
3314 case DT_TOK_LT:
3315 case DT_TOK_LE:
3316 case DT_TOK_GT:
3317 case DT_TOK_GE:
3318 case DT_TOK_EQU:
3319 case DT_TOK_NEQ:
3321 * The D comparison operators provide the ability to transform
3322 * a right-hand identifier into a corresponding enum tag value
3323 * if the left-hand side is an enum type. To do this, we cook
3324 * the left-hand side, and then see if the right-hand side is
3325 * an unscoped identifier defined in the enum. If so, we
3326 * convert into an integer constant node with the tag's value.
3328 lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF);
3330 kind = ctf_type_kind(lp->dn_ctfp,
3331 ctf_type_resolve(lp->dn_ctfp, lp->dn_type));
3333 if (kind == CTF_K_ENUM && rp->dn_kind == DT_NODE_IDENT &&
3334 strchr(rp->dn_string, '`') == NULL && ctf_enum_value(
3335 lp->dn_ctfp, lp->dn_type, rp->dn_string, &val) == 0) {
3337 if ((idp = dt_idstack_lookup(&yypcb->pcb_globals,
3338 rp->dn_string)) != NULL) {
3339 xyerror(D_IDENT_AMBIG,
3340 "ambiguous use of operator %s: %s is "
3341 "both a %s enum tag and a global %s\n",
3342 opstr(op), rp->dn_string,
3343 dt_node_type_name(lp, n1, sizeof (n1)),
3344 dt_idkind_name(idp->di_kind));
3347 free(rp->dn_string);
3348 rp->dn_string = NULL;
3349 rp->dn_kind = DT_NODE_INT;
3350 rp->dn_flags |= DT_NF_COOKED;
3351 rp->dn_op = DT_TOK_INT;
3352 rp->dn_value = (intmax_t)val;
3354 dt_node_type_assign(rp, lp->dn_ctfp, lp->dn_type,
3355 B_FALSE);
3356 dt_node_attr_assign(rp, _dtrace_symattr);
3359 rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
3362 * The rules for type checking for the relational operators are
3363 * described in the ANSI-C spec (see K&R[A7.9-10]). We perform
3364 * the various tests in order from least to most expensive. We
3365 * also allow derived strings to be compared as a first-class
3366 * type (resulting in a strcmp(3C)-style comparison), and we
3367 * slightly relax the A7.9 rules to permit void pointer
3368 * comparisons as in A7.10. Our users won't be confused by
3369 * this since they understand pointers are just numbers, and
3370 * relaxing this constraint simplifies the implementation.
3372 if (ctf_type_compat(lp->dn_ctfp, lp->dn_type,
3373 rp->dn_ctfp, rp->dn_type))
3374 /*EMPTY*/;
3375 else if (dt_node_is_integer(lp) && dt_node_is_integer(rp))
3376 /*EMPTY*/;
3377 else if (dt_node_is_strcompat(lp) && dt_node_is_strcompat(rp) &&
3378 (dt_node_is_string(lp) || dt_node_is_string(rp)))
3379 /*EMPTY*/;
3380 else if (dt_node_is_ptrcompat(lp, rp, NULL, NULL) == 0) {
3381 xyerror(D_OP_INCOMPAT, "operands have "
3382 "incompatible types: \"%s\" %s \"%s\"\n",
3383 dt_node_type_name(lp, n1, sizeof (n1)), opstr(op),
3384 dt_node_type_name(rp, n2, sizeof (n2)));
3387 dt_node_type_assign(dnp, DT_INT_CTFP(dtp), DT_INT_TYPE(dtp),
3388 B_FALSE);
3389 dt_node_attr_assign(dnp, dt_attr_min(lp->dn_attr, rp->dn_attr));
3390 break;
3392 case DT_TOK_ADD:
3393 case DT_TOK_SUB: {
3395 * The rules for type checking for the additive operators are
3396 * described in the ANSI-C spec (see K&R[A7.7]). Pointers and
3397 * integers may be manipulated according to specific rules. In
3398 * these cases D permits strings to be treated as pointers.
3400 int lp_is_ptr, lp_is_int, rp_is_ptr, rp_is_int;
3402 lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF);
3403 rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
3405 lp_is_ptr = dt_node_is_string(lp) ||
3406 (dt_node_is_pointer(lp) && !dt_node_is_vfptr(lp));
3407 lp_is_int = dt_node_is_integer(lp);
3409 rp_is_ptr = dt_node_is_string(rp) ||
3410 (dt_node_is_pointer(rp) && !dt_node_is_vfptr(rp));
3411 rp_is_int = dt_node_is_integer(rp);
3413 if (lp_is_int && rp_is_int) {
3414 dt_type_promote(lp, rp, &ctfp, &type);
3415 uref = 0;
3416 } else if (lp_is_ptr && rp_is_int) {
3417 ctfp = lp->dn_ctfp;
3418 type = lp->dn_type;
3419 uref = lp->dn_flags & DT_NF_USERLAND;
3420 } else if (lp_is_int && rp_is_ptr && op == DT_TOK_ADD) {
3421 ctfp = rp->dn_ctfp;
3422 type = rp->dn_type;
3423 uref = rp->dn_flags & DT_NF_USERLAND;
3424 } else if (lp_is_ptr && rp_is_ptr && op == DT_TOK_SUB &&
3425 dt_node_is_ptrcompat(lp, rp, NULL, NULL)) {
3426 ctfp = dtp->dt_ddefs->dm_ctfp;
3427 type = ctf_lookup_by_name(ctfp, "ptrdiff_t");
3428 uref = 0;
3429 } else {
3430 xyerror(D_OP_INCOMPAT, "operands have incompatible "
3431 "types: \"%s\" %s \"%s\"\n",
3432 dt_node_type_name(lp, n1, sizeof (n1)), opstr(op),
3433 dt_node_type_name(rp, n2, sizeof (n2)));
3436 dt_node_type_assign(dnp, ctfp, type, B_FALSE);
3437 dt_node_attr_assign(dnp, dt_attr_min(lp->dn_attr, rp->dn_attr));
3439 if (uref)
3440 dnp->dn_flags |= DT_NF_USERLAND;
3441 break;
3444 case DT_TOK_OR_EQ:
3445 case DT_TOK_XOR_EQ:
3446 case DT_TOK_AND_EQ:
3447 case DT_TOK_LSH_EQ:
3448 case DT_TOK_RSH_EQ:
3449 case DT_TOK_MOD_EQ:
3450 if (lp->dn_kind == DT_NODE_IDENT) {
3451 dt_xcook_ident(lp, dtp->dt_globals,
3452 DT_IDENT_SCALAR, B_TRUE);
3455 lp = dnp->dn_left =
3456 dt_node_cook(lp, DT_IDFLG_REF | DT_IDFLG_MOD);
3458 rp = dnp->dn_right =
3459 dt_node_cook(rp, DT_IDFLG_REF | DT_IDFLG_MOD);
3461 if (!dt_node_is_integer(lp) || !dt_node_is_integer(rp)) {
3462 xyerror(D_OP_INT, "operator %s requires operands of "
3463 "integral type\n", opstr(op));
3465 goto asgn_common;
3467 case DT_TOK_MUL_EQ:
3468 case DT_TOK_DIV_EQ:
3469 if (lp->dn_kind == DT_NODE_IDENT) {
3470 dt_xcook_ident(lp, dtp->dt_globals,
3471 DT_IDENT_SCALAR, B_TRUE);
3474 lp = dnp->dn_left =
3475 dt_node_cook(lp, DT_IDFLG_REF | DT_IDFLG_MOD);
3477 rp = dnp->dn_right =
3478 dt_node_cook(rp, DT_IDFLG_REF | DT_IDFLG_MOD);
3480 if (!dt_node_is_arith(lp) || !dt_node_is_arith(rp)) {
3481 xyerror(D_OP_ARITH, "operator %s requires operands of "
3482 "arithmetic type\n", opstr(op));
3484 goto asgn_common;
3486 case DT_TOK_ASGN:
3488 * If the left-hand side is an identifier, attempt to resolve
3489 * it as either an aggregation or scalar variable. We pass
3490 * B_TRUE to dt_xcook_ident to indicate that a new variable can
3491 * be created if no matching variable exists in the namespace.
3493 if (lp->dn_kind == DT_NODE_IDENT) {
3494 if (lp->dn_op == DT_TOK_AGG) {
3495 dt_xcook_ident(lp, dtp->dt_aggs,
3496 DT_IDENT_AGG, B_TRUE);
3497 } else {
3498 dt_xcook_ident(lp, dtp->dt_globals,
3499 DT_IDENT_SCALAR, B_TRUE);
3503 lp = dnp->dn_left = dt_node_cook(lp, 0); /* don't set mod yet */
3504 rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
3507 * If the left-hand side is an aggregation, verify that we are
3508 * assigning it the result of an aggregating function. Once
3509 * we've done so, hide the func node in the aggregation and
3510 * return the aggregation itself up to the parse tree parent.
3511 * This transformation is legal since the assigned function
3512 * cannot change identity across disjoint cooking passes and
3513 * the argument list subtree is retained for later cooking.
3515 if (lp->dn_kind == DT_NODE_AGG) {
3516 const char *aname = lp->dn_ident->di_name;
3517 dt_ident_t *oid = lp->dn_ident->di_iarg;
3519 if (rp->dn_kind != DT_NODE_FUNC ||
3520 rp->dn_ident->di_kind != DT_IDENT_AGGFUNC) {
3521 xyerror(D_AGG_FUNC,
3522 "@%s must be assigned the result of "
3523 "an aggregating function\n", aname);
3526 if (oid != NULL && oid != rp->dn_ident) {
3527 xyerror(D_AGG_REDEF,
3528 "aggregation redefined: @%s\n\t "
3529 "current: @%s = %s( )\n\tprevious: @%s = "
3530 "%s( ) : line %d\n", aname, aname,
3531 rp->dn_ident->di_name, aname, oid->di_name,
3532 lp->dn_ident->di_lineno);
3533 } else if (oid == NULL)
3534 lp->dn_ident->di_iarg = rp->dn_ident;
3537 * Do not allow multiple aggregation assignments in a
3538 * single statement, e.g. (@a = count()) = count();
3539 * We produce a message as if the result of aggregating
3540 * function does not propagate DT_NF_LVALUE.
3542 if (lp->dn_aggfun != NULL) {
3543 xyerror(D_OP_LVAL, "operator = requires "
3544 "modifiable lvalue as an operand\n");
3547 lp->dn_aggfun = rp;
3548 lp = dt_node_cook(lp, DT_IDFLG_MOD);
3550 dnp->dn_left = dnp->dn_right = NULL;
3551 dt_node_free(dnp);
3553 return (lp);
3557 * If the right-hand side is a dynamic variable that is the
3558 * output of a translator, our result is the translated type.
3560 if ((idp = dt_node_resolve(rp, DT_IDENT_XLSOU)) != NULL) {
3561 ctfp = idp->di_ctfp;
3562 type = idp->di_type;
3563 uref = idp->di_flags & DT_IDFLG_USER;
3564 } else {
3565 ctfp = rp->dn_ctfp;
3566 type = rp->dn_type;
3567 uref = rp->dn_flags & DT_NF_USERLAND;
3571 * If the left-hand side of an assignment statement is a virgin
3572 * variable created by this compilation pass, reset the type of
3573 * this variable to the type of the right-hand side.
3575 if (lp->dn_kind == DT_NODE_VAR &&
3576 dt_ident_unref(lp->dn_ident)) {
3577 dt_node_type_assign(lp, ctfp, type, B_FALSE);
3578 dt_ident_type_assign(lp->dn_ident, ctfp, type);
3580 if (uref) {
3581 lp->dn_flags |= DT_NF_USERLAND;
3582 lp->dn_ident->di_flags |= DT_IDFLG_USER;
3586 if (lp->dn_kind == DT_NODE_VAR)
3587 lp->dn_ident->di_flags |= DT_IDFLG_MOD;
3590 * The rules for type checking for the assignment operators are
3591 * described in the ANSI-C spec (see K&R[A7.17]). We share
3592 * most of this code with the argument list checking code.
3594 if (!dt_node_is_string(lp)) {
3595 kind = ctf_type_kind(lp->dn_ctfp,
3596 ctf_type_resolve(lp->dn_ctfp, lp->dn_type));
3598 if (kind == CTF_K_ARRAY || kind == CTF_K_FUNCTION) {
3599 xyerror(D_OP_ARRFUN, "operator %s may not be "
3600 "applied to operand of type \"%s\"\n",
3601 opstr(op),
3602 dt_node_type_name(lp, n1, sizeof (n1)));
3606 if (idp != NULL && idp->di_kind == DT_IDENT_XLSOU &&
3607 ctf_type_compat(lp->dn_ctfp, lp->dn_type, ctfp, type))
3608 goto asgn_common;
3610 if (dt_node_is_argcompat(lp, rp))
3611 goto asgn_common;
3613 xyerror(D_OP_INCOMPAT,
3614 "operands have incompatible types: \"%s\" %s \"%s\"\n",
3615 dt_node_type_name(lp, n1, sizeof (n1)), opstr(op),
3616 dt_node_type_name(rp, n2, sizeof (n2)));
3617 /*NOTREACHED*/
3619 case DT_TOK_ADD_EQ:
3620 case DT_TOK_SUB_EQ:
3621 if (lp->dn_kind == DT_NODE_IDENT) {
3622 dt_xcook_ident(lp, dtp->dt_globals,
3623 DT_IDENT_SCALAR, B_TRUE);
3626 lp = dnp->dn_left =
3627 dt_node_cook(lp, DT_IDFLG_REF | DT_IDFLG_MOD);
3629 rp = dnp->dn_right =
3630 dt_node_cook(rp, DT_IDFLG_REF | DT_IDFLG_MOD);
3632 if (dt_node_is_string(lp) || dt_node_is_string(rp)) {
3633 xyerror(D_OP_INCOMPAT, "operands have "
3634 "incompatible types: \"%s\" %s \"%s\"\n",
3635 dt_node_type_name(lp, n1, sizeof (n1)), opstr(op),
3636 dt_node_type_name(rp, n2, sizeof (n2)));
3640 * The rules for type checking for the assignment operators are
3641 * described in the ANSI-C spec (see K&R[A7.17]). To these
3642 * rules we add that only writable D nodes can be modified.
3644 if (dt_node_is_integer(lp) == 0 ||
3645 dt_node_is_integer(rp) == 0) {
3646 if (!dt_node_is_pointer(lp) || dt_node_is_vfptr(lp)) {
3647 xyerror(D_OP_VFPTR,
3648 "operator %s requires left-hand scalar "
3649 "operand of known size\n", opstr(op));
3650 } else if (dt_node_is_integer(rp) == 0 &&
3651 dt_node_is_ptrcompat(lp, rp, NULL, NULL) == 0) {
3652 xyerror(D_OP_INCOMPAT, "operands have "
3653 "incompatible types: \"%s\" %s \"%s\"\n",
3654 dt_node_type_name(lp, n1, sizeof (n1)),
3655 opstr(op),
3656 dt_node_type_name(rp, n2, sizeof (n2)));
3659 asgn_common:
3660 dt_assign_common(dnp);
3661 break;
3663 case DT_TOK_PTR:
3665 * If the left-hand side of operator -> is one of the scoping
3666 * keywords, permit a local or thread variable to be created or
3667 * referenced.
3669 if (lp->dn_kind == DT_NODE_IDENT) {
3670 dt_idhash_t *dhp = NULL;
3672 if (strcmp(lp->dn_string, "self") == 0) {
3673 dhp = dtp->dt_tls;
3674 } else if (strcmp(lp->dn_string, "this") == 0) {
3675 dhp = yypcb->pcb_locals;
3677 if (dhp != NULL) {
3678 if (rp->dn_kind != DT_NODE_VAR) {
3679 dt_xcook_ident(rp, dhp,
3680 DT_IDENT_SCALAR, B_TRUE);
3683 if (idflags != 0)
3684 rp = dt_node_cook(rp, idflags);
3686 /* avoid freeing rp */
3687 dnp->dn_right = dnp->dn_left;
3688 dt_node_free(dnp);
3689 return (rp);
3692 /*FALLTHRU*/
3693 case DT_TOK_DOT:
3694 lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF);
3696 if (rp->dn_kind != DT_NODE_IDENT) {
3697 xyerror(D_OP_IDENT, "operator %s must be followed by "
3698 "an identifier\n", opstr(op));
3701 if ((idp = dt_node_resolve(lp, DT_IDENT_XLSOU)) != NULL ||
3702 (idp = dt_node_resolve(lp, DT_IDENT_XLPTR)) != NULL) {
3704 * If the left-hand side is a translated struct or ptr,
3705 * the type of the left is the translation output type.
3707 dt_xlator_t *dxp = idp->di_data;
3709 if (dt_xlator_member(dxp, rp->dn_string) == NULL) {
3710 xyerror(D_XLATE_NOCONV,
3711 "translator does not define conversion "
3712 "for member: %s\n", rp->dn_string);
3715 ctfp = idp->di_ctfp;
3716 type = ctf_type_resolve(ctfp, idp->di_type);
3717 uref = idp->di_flags & DT_IDFLG_USER;
3718 } else {
3719 ctfp = lp->dn_ctfp;
3720 type = ctf_type_resolve(ctfp, lp->dn_type);
3721 uref = lp->dn_flags & DT_NF_USERLAND;
3724 kind = ctf_type_kind(ctfp, type);
3726 if (op == DT_TOK_PTR) {
3727 if (kind != CTF_K_POINTER) {
3728 xyerror(D_OP_PTR, "operator %s must be "
3729 "applied to a pointer\n", opstr(op));
3731 type = ctf_type_reference(ctfp, type);
3732 type = ctf_type_resolve(ctfp, type);
3733 kind = ctf_type_kind(ctfp, type);
3737 * If we follow a reference to a forward declaration tag,
3738 * search the entire type space for the actual definition.
3740 dt_resolve_forward_decl(&ctfp, &type);
3741 kind = ctf_type_kind(ctfp, type);
3743 if (kind == CTF_K_FORWARD) {
3744 xyerror(D_OP_INCOMPLETE,
3745 "operator %s cannot be applied to a "
3746 "forward declaration: no %s definition "
3747 "is available\n", opstr(op),
3748 ctf_type_name(ctfp, type, n1, sizeof (n1)));
3749 } else if (kind != CTF_K_STRUCT && kind != CTF_K_UNION) {
3750 if (op == DT_TOK_PTR) {
3751 xyerror(D_OP_SOU, "operator -> cannot be "
3752 "applied to pointer to type \"%s\"; must "
3753 "be applied to a struct or union pointer\n",
3754 ctf_type_name(ctfp, type, n1, sizeof (n1)));
3755 } else {
3756 xyerror(D_OP_SOU, "operator %s cannot be "
3757 "applied to type \"%s\"; must be applied "
3758 "to a struct or union\n", opstr(op),
3759 ctf_type_name(ctfp, type, n1, sizeof (n1)));
3763 if (ctf_member_info(ctfp, type, rp->dn_string, &m) == CTF_ERR) {
3764 xyerror(D_TYPE_MEMBER,
3765 "%s is not a member of %s\n", rp->dn_string,
3766 ctf_type_name(ctfp, type, n1, sizeof (n1)));
3769 type = m.ctm_type;
3771 dt_resolve_forward_decl(&ctfp, &type);
3772 dt_node_type_assign(dnp, ctfp, type, B_FALSE);
3773 dt_node_attr_assign(dnp, lp->dn_attr);
3775 type = ctf_type_resolve(ctfp, type);
3776 kind = ctf_type_kind(ctfp, type);
3778 if (op == DT_TOK_PTR && (kind != CTF_K_ARRAY ||
3779 dt_node_is_string(dnp)))
3780 dnp->dn_flags |= DT_NF_LVALUE; /* see K&R[A7.3.3] */
3782 if (op == DT_TOK_DOT && (lp->dn_flags & DT_NF_LVALUE) &&
3783 (kind != CTF_K_ARRAY || dt_node_is_string(dnp)))
3784 dnp->dn_flags |= DT_NF_LVALUE; /* see K&R[A7.3.3] */
3786 if (lp->dn_flags & DT_NF_WRITABLE)
3787 dnp->dn_flags |= DT_NF_WRITABLE;
3789 if (uref && (kind == CTF_K_POINTER ||
3790 (dnp->dn_flags & DT_NF_REF)))
3791 dnp->dn_flags |= DT_NF_USERLAND;
3792 break;
3794 case DT_TOK_LBRAC: {
3796 * If op is DT_TOK_LBRAC, we know from the special-case code at
3797 * the top that lp is either a D variable or an aggregation.
3799 dt_node_t *lnp;
3802 * If the left-hand side is an aggregation, just set dn_aggtup
3803 * to the right-hand side and return the cooked aggregation.
3804 * This transformation is legal since we are just collapsing
3805 * nodes to simplify later processing, and the entire aggtup
3806 * parse subtree is retained for subsequent cooking passes.
3808 if (lp->dn_kind == DT_NODE_AGG) {
3809 if (lp->dn_aggtup != NULL) {
3810 xyerror(D_AGG_MDIM, "improper attempt to "
3811 "reference @%s as a multi-dimensional "
3812 "array\n", lp->dn_ident->di_name);
3815 lp->dn_aggtup = rp;
3816 lp = dt_node_cook(lp, 0);
3818 dnp->dn_left = dnp->dn_right = NULL;
3819 dt_node_free(dnp);
3821 return (lp);
3824 assert(lp->dn_kind == DT_NODE_VAR);
3825 idp = lp->dn_ident;
3828 * If the left-hand side is a non-global scalar that hasn't yet
3829 * been referenced or modified, it was just created by self->
3830 * or this-> and we can convert it from scalar to assoc array.
3832 if (idp->di_kind == DT_IDENT_SCALAR && dt_ident_unref(idp) &&
3833 (idp->di_flags & (DT_IDFLG_LOCAL | DT_IDFLG_TLS)) != 0) {
3835 if (idp->di_flags & DT_IDFLG_LOCAL) {
3836 xyerror(D_ARR_LOCAL,
3837 "local variables may not be used as "
3838 "associative arrays: %s\n", idp->di_name);
3841 dt_dprintf("morph variable %s (id %u) from scalar to "
3842 "array\n", idp->di_name, idp->di_id);
3844 dt_ident_morph(idp, DT_IDENT_ARRAY,
3845 &dt_idops_assc, NULL);
3848 if (idp->di_kind != DT_IDENT_ARRAY) {
3849 xyerror(D_IDENT_BADREF, "%s '%s' may not be referenced "
3850 "as %s\n", dt_idkind_name(idp->di_kind),
3851 idp->di_name, dt_idkind_name(DT_IDENT_ARRAY));
3855 * Now that we've confirmed our left-hand side is a DT_NODE_VAR
3856 * of idkind DT_IDENT_ARRAY, we need to splice the [ node from
3857 * the parse tree and leave a cooked DT_NODE_VAR in its place
3858 * where dn_args for the VAR node is the right-hand 'rp' tree,
3859 * as shown in the parse tree diagram below:
3861 * / /
3862 * [ OP2 "[" ]=dnp [ VAR ]=dnp
3863 * / \ => |
3864 * / \ +- dn_args -> [ ??? ]=rp
3865 * [ VAR ]=lp [ ??? ]=rp
3867 * Since the final dt_node_cook(dnp) can fail using longjmp we
3868 * must perform the transformations as a group first by over-
3869 * writing 'dnp' to become the VAR node, so that the parse tree
3870 * is guaranteed to be in a consistent state if the cook fails.
3872 assert(lp->dn_kind == DT_NODE_VAR);
3873 assert(lp->dn_args == NULL);
3875 lnp = dnp->dn_link;
3876 bcopy(lp, dnp, sizeof (dt_node_t));
3877 dnp->dn_link = lnp;
3879 dnp->dn_args = rp;
3880 dnp->dn_list = NULL;
3882 dt_node_free(lp);
3883 return (dt_node_cook(dnp, idflags));
3886 case DT_TOK_XLATE: {
3887 dt_xlator_t *dxp;
3889 assert(lp->dn_kind == DT_NODE_TYPE);
3890 rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
3891 dxp = dt_xlator_lookup(dtp, rp, lp, DT_XLATE_FUZZY);
3893 if (dxp == NULL) {
3894 xyerror(D_XLATE_NONE,
3895 "cannot translate from \"%s\" to \"%s\"\n",
3896 dt_node_type_name(rp, n1, sizeof (n1)),
3897 dt_node_type_name(lp, n2, sizeof (n2)));
3900 dnp->dn_ident = dt_xlator_ident(dxp, lp->dn_ctfp, lp->dn_type);
3901 dt_node_type_assign(dnp, DT_DYN_CTFP(dtp), DT_DYN_TYPE(dtp),
3902 B_FALSE);
3903 dt_node_attr_assign(dnp,
3904 dt_attr_min(rp->dn_attr, dnp->dn_ident->di_attr));
3905 break;
3908 case DT_TOK_LPAR: {
3909 ctf_id_t ltype, rtype;
3910 uint_t lkind, rkind;
3912 assert(lp->dn_kind == DT_NODE_TYPE);
3913 rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
3915 ltype = ctf_type_resolve(lp->dn_ctfp, lp->dn_type);
3916 lkind = ctf_type_kind(lp->dn_ctfp, ltype);
3918 rtype = ctf_type_resolve(rp->dn_ctfp, rp->dn_type);
3919 rkind = ctf_type_kind(rp->dn_ctfp, rtype);
3922 * The rules for casting are loosely explained in K&R[A7.5]
3923 * and K&R[A6]. Basically, we can cast to the same type or
3924 * same base type, between any kind of scalar values, from
3925 * arrays to pointers, and we can cast anything to void.
3926 * To these rules D adds casts from scalars to strings.
3928 if (ctf_type_compat(lp->dn_ctfp, lp->dn_type,
3929 rp->dn_ctfp, rp->dn_type))
3930 /*EMPTY*/;
3931 else if (dt_node_is_scalar(lp) &&
3932 (dt_node_is_scalar(rp) || rkind == CTF_K_FUNCTION))
3933 /*EMPTY*/;
3934 else if (dt_node_is_void(lp))
3935 /*EMPTY*/;
3936 else if (lkind == CTF_K_POINTER && dt_node_is_pointer(rp))
3937 /*EMPTY*/;
3938 else if (dt_node_is_string(lp) && (dt_node_is_scalar(rp) ||
3939 dt_node_is_pointer(rp) || dt_node_is_strcompat(rp)))
3940 /*EMPTY*/;
3941 else {
3942 xyerror(D_CAST_INVAL,
3943 "invalid cast expression: \"%s\" to \"%s\"\n",
3944 dt_node_type_name(rp, n1, sizeof (n1)),
3945 dt_node_type_name(lp, n2, sizeof (n2)));
3948 dt_node_type_propagate(lp, dnp); /* see K&R[A7.5] */
3949 dt_node_attr_assign(dnp, dt_attr_min(lp->dn_attr, rp->dn_attr));
3952 * If it's a pointer then should be able to (attempt to)
3953 * assign to it.
3955 if (lkind == CTF_K_POINTER)
3956 dnp->dn_flags |= DT_NF_WRITABLE;
3958 break;
3961 case DT_TOK_COMMA:
3962 lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF);
3963 rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
3965 if (dt_node_is_dynamic(lp) || dt_node_is_dynamic(rp)) {
3966 xyerror(D_OP_DYN, "operator %s operands "
3967 "cannot be of dynamic type\n", opstr(op));
3970 if (dt_node_is_actfunc(lp) || dt_node_is_actfunc(rp)) {
3971 xyerror(D_OP_ACT, "operator %s operands "
3972 "cannot be actions\n", opstr(op));
3975 dt_node_type_propagate(rp, dnp); /* see K&R[A7.18] */
3976 dt_node_attr_assign(dnp, dt_attr_min(lp->dn_attr, rp->dn_attr));
3977 break;
3979 default:
3980 xyerror(D_UNKNOWN, "invalid binary op %s\n", opstr(op));
3984 * Complete the conversion of E1[E2] to *((E1)+(E2)) that we started
3985 * at the top of our switch() above (see K&R[A7.3.1]). Since E2 is
3986 * parsed as an argument_expression_list by dt_grammar.y, we can
3987 * end up with a comma-separated list inside of a non-associative
3988 * array reference. We check for this and report an appropriate error.
3990 if (dnp->dn_op == DT_TOK_LBRAC && op == DT_TOK_ADD) {
3991 dt_node_t *pnp;
3993 if (rp->dn_list != NULL) {
3994 xyerror(D_ARR_BADREF,
3995 "cannot access %s as an associative array\n",
3996 dt_node_name(lp, n1, sizeof (n1)));
3999 dnp->dn_op = DT_TOK_ADD;
4000 pnp = dt_node_op1(DT_TOK_DEREF, dnp);
4003 * Cook callbacks are not typically permitted to allocate nodes.
4004 * When we do, we must insert them in the middle of an existing
4005 * allocation list rather than having them appended to the pcb
4006 * list because the sub-expression may be part of a definition.
4008 assert(yypcb->pcb_list == pnp);
4009 yypcb->pcb_list = pnp->dn_link;
4011 pnp->dn_link = dnp->dn_link;
4012 dnp->dn_link = pnp;
4014 return (dt_node_cook(pnp, DT_IDFLG_REF));
4017 return (dnp);
4020 /*ARGSUSED*/
4021 static dt_node_t *
4022 dt_cook_op3(dt_node_t *dnp, uint_t idflags)
4024 dt_node_t *lp, *rp;
4025 ctf_file_t *ctfp;
4026 ctf_id_t type;
4028 dnp->dn_expr = dt_node_cook(dnp->dn_expr, DT_IDFLG_REF);
4029 lp = dnp->dn_left = dt_node_cook(dnp->dn_left, DT_IDFLG_REF);
4030 rp = dnp->dn_right = dt_node_cook(dnp->dn_right, DT_IDFLG_REF);
4032 if (!dt_node_is_scalar(dnp->dn_expr)) {
4033 xyerror(D_OP_SCALAR,
4034 "operator ?: expression must be of scalar type\n");
4037 if (dt_node_is_dynamic(lp) || dt_node_is_dynamic(rp)) {
4038 xyerror(D_OP_DYN,
4039 "operator ?: operands cannot be of dynamic type\n");
4043 * The rules for type checking for the ternary operator are complex and
4044 * are described in the ANSI-C spec (see K&R[A7.16]). We implement
4045 * the various tests in order from least to most expensive.
4047 if (ctf_type_compat(lp->dn_ctfp, lp->dn_type,
4048 rp->dn_ctfp, rp->dn_type)) {
4049 ctfp = lp->dn_ctfp;
4050 type = lp->dn_type;
4051 } else if (dt_node_is_integer(lp) && dt_node_is_integer(rp)) {
4052 dt_type_promote(lp, rp, &ctfp, &type);
4053 } else if (dt_node_is_strcompat(lp) && dt_node_is_strcompat(rp) &&
4054 (dt_node_is_string(lp) || dt_node_is_string(rp))) {
4055 ctfp = DT_STR_CTFP(yypcb->pcb_hdl);
4056 type = DT_STR_TYPE(yypcb->pcb_hdl);
4057 } else if (dt_node_is_ptrcompat(lp, rp, &ctfp, &type) == 0) {
4058 xyerror(D_OP_INCOMPAT,
4059 "operator ?: operands must have compatible types\n");
4062 if (dt_node_is_actfunc(lp) || dt_node_is_actfunc(rp)) {
4063 xyerror(D_OP_ACT, "action cannot be "
4064 "used in a conditional context\n");
4067 dt_node_type_assign(dnp, ctfp, type, B_FALSE);
4068 dt_node_attr_assign(dnp, dt_attr_min(dnp->dn_expr->dn_attr,
4069 dt_attr_min(lp->dn_attr, rp->dn_attr)));
4071 return (dnp);
4074 static dt_node_t *
4075 dt_cook_statement(dt_node_t *dnp, uint_t idflags)
4077 dnp->dn_expr = dt_node_cook(dnp->dn_expr, idflags);
4078 dt_node_attr_assign(dnp, dnp->dn_expr->dn_attr);
4080 return (dnp);
4084 * If dn_aggfun is set, this node is a collapsed aggregation assignment (see
4085 * the special case code for DT_TOK_ASGN in dt_cook_op2() above), in which
4086 * case we cook both the tuple and the function call. If dn_aggfun is NULL,
4087 * this node is just a reference to the aggregation's type and attributes.
4089 /*ARGSUSED*/
4090 static dt_node_t *
4091 dt_cook_aggregation(dt_node_t *dnp, uint_t idflags)
4093 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
4095 if (dnp->dn_aggfun != NULL) {
4096 dnp->dn_aggfun = dt_node_cook(dnp->dn_aggfun, DT_IDFLG_REF);
4097 dt_node_attr_assign(dnp, dt_ident_cook(dnp,
4098 dnp->dn_ident, &dnp->dn_aggtup));
4099 } else {
4100 dt_node_type_assign(dnp, DT_DYN_CTFP(dtp), DT_DYN_TYPE(dtp),
4101 B_FALSE);
4102 dt_node_attr_assign(dnp, dnp->dn_ident->di_attr);
4105 return (dnp);
4109 * Since D permits new variable identifiers to be instantiated in any program
4110 * expression, we may need to cook a clause's predicate either before or after
4111 * the action list depending on the program code in question. Consider:
4113 * probe-description-list probe-description-list
4114 * /x++/ /x == 0/
4115 * { {
4116 * trace(x); trace(x++);
4117 * } }
4119 * In the left-hand example, the predicate uses operator ++ to instantiate 'x'
4120 * as a variable of type int64_t. The predicate must be cooked first because
4121 * otherwise the statement trace(x) refers to an unknown identifier. In the
4122 * right-hand example, the action list uses ++ to instantiate 'x'; the action
4123 * list must be cooked first because otherwise the predicate x == 0 refers to
4124 * an unknown identifier. In order to simplify programming, we support both.
4126 * When cooking a clause, we cook the action statements before the predicate by
4127 * default, since it seems more common to create or modify identifiers in the
4128 * action list. If cooking fails due to an unknown identifier, we attempt to
4129 * cook the predicate (i.e. do it first) and then go back and cook the actions.
4130 * If this, too, fails (or if we get an error other than D_IDENT_UNDEF) we give
4131 * up and report failure back to the user. There are five possible paths:
4133 * cook actions = OK, cook predicate = OK -> OK
4134 * cook actions = OK, cook predicate = ERR -> ERR
4135 * cook actions = ERR, cook predicate = ERR -> ERR
4136 * cook actions = ERR, cook predicate = OK, cook actions = OK -> OK
4137 * cook actions = ERR, cook predicate = OK, cook actions = ERR -> ERR
4139 * The programmer can still defeat our scheme by creating circular definition
4140 * dependencies between predicates and actions, as in this example clause:
4142 * probe-description-list
4143 * /x++ && y == 0/
4145 * trace(x + y++);
4148 * but it doesn't seem worth the complexity to handle such rare cases. The
4149 * user can simply use the D variable declaration syntax to work around them.
4151 static dt_node_t *
4152 dt_cook_clause(dt_node_t *dnp, uint_t idflags)
4154 volatile int err, tries;
4155 jmp_buf ojb;
4158 * Before assigning dn_ctxattr, temporarily assign the probe attribute
4159 * to 'dnp' itself to force an attribute check and minimum violation.
4161 dt_node_attr_assign(dnp, yypcb->pcb_pinfo.dtp_attr);
4162 dnp->dn_ctxattr = yypcb->pcb_pinfo.dtp_attr;
4164 bcopy(yypcb->pcb_jmpbuf, ojb, sizeof (jmp_buf));
4165 tries = 0;
4167 if (dnp->dn_pred != NULL && (err = setjmp(yypcb->pcb_jmpbuf)) != 0) {
4168 bcopy(ojb, yypcb->pcb_jmpbuf, sizeof (jmp_buf));
4169 if (tries++ != 0 || err != EDT_COMPILER || (
4170 yypcb->pcb_hdl->dt_errtag != dt_errtag(D_IDENT_UNDEF) &&
4171 yypcb->pcb_hdl->dt_errtag != dt_errtag(D_VAR_UNDEF)))
4172 longjmp(yypcb->pcb_jmpbuf, err);
4175 if (tries == 0) {
4176 yylabel("action list");
4178 dt_node_attr_assign(dnp,
4179 dt_node_list_cook(&dnp->dn_acts, idflags));
4181 bcopy(ojb, yypcb->pcb_jmpbuf, sizeof (jmp_buf));
4182 yylabel(NULL);
4185 if (dnp->dn_pred != NULL) {
4186 yylabel("predicate");
4188 dnp->dn_pred = dt_node_cook(dnp->dn_pred, idflags);
4189 dt_node_attr_assign(dnp,
4190 dt_attr_min(dnp->dn_attr, dnp->dn_pred->dn_attr));
4192 if (!dt_node_is_scalar(dnp->dn_pred)) {
4193 xyerror(D_PRED_SCALAR,
4194 "predicate result must be of scalar type\n");
4197 yylabel(NULL);
4200 if (tries != 0) {
4201 yylabel("action list");
4203 dt_node_attr_assign(dnp,
4204 dt_node_list_cook(&dnp->dn_acts, idflags));
4206 yylabel(NULL);
4209 return (dnp);
4212 /*ARGSUSED*/
4213 static dt_node_t *
4214 dt_cook_inline(dt_node_t *dnp, uint_t idflags)
4216 dt_idnode_t *inp = dnp->dn_ident->di_iarg;
4217 dt_ident_t *rdp;
4219 char n1[DT_TYPE_NAMELEN];
4220 char n2[DT_TYPE_NAMELEN];
4222 assert(dnp->dn_ident->di_flags & DT_IDFLG_INLINE);
4223 assert(inp->din_root->dn_flags & DT_NF_COOKED);
4226 * If we are inlining a translation, verify that the inline declaration
4227 * type exactly matches the type that is returned by the translation.
4228 * Otherwise just use dt_node_is_argcompat() to check the types.
4230 if ((rdp = dt_node_resolve(inp->din_root, DT_IDENT_XLSOU)) != NULL ||
4231 (rdp = dt_node_resolve(inp->din_root, DT_IDENT_XLPTR)) != NULL) {
4233 ctf_file_t *lctfp = dnp->dn_ctfp;
4234 ctf_id_t ltype = ctf_type_resolve(lctfp, dnp->dn_type);
4236 dt_xlator_t *dxp = rdp->di_data;
4237 ctf_file_t *rctfp = dxp->dx_dst_ctfp;
4238 ctf_id_t rtype = dxp->dx_dst_base;
4240 if (ctf_type_kind(lctfp, ltype) == CTF_K_POINTER) {
4241 ltype = ctf_type_reference(lctfp, ltype);
4242 ltype = ctf_type_resolve(lctfp, ltype);
4245 if (ctf_type_compat(lctfp, ltype, rctfp, rtype) == 0) {
4246 dnerror(dnp, D_OP_INCOMPAT,
4247 "inline %s definition uses incompatible types: "
4248 "\"%s\" = \"%s\"\n", dnp->dn_ident->di_name,
4249 dt_type_name(lctfp, ltype, n1, sizeof (n1)),
4250 dt_type_name(rctfp, rtype, n2, sizeof (n2)));
4253 } else if (dt_node_is_argcompat(dnp, inp->din_root) == 0) {
4254 dnerror(dnp, D_OP_INCOMPAT,
4255 "inline %s definition uses incompatible types: "
4256 "\"%s\" = \"%s\"\n", dnp->dn_ident->di_name,
4257 dt_node_type_name(dnp, n1, sizeof (n1)),
4258 dt_node_type_name(inp->din_root, n2, sizeof (n2)));
4261 return (dnp);
4264 static dt_node_t *
4265 dt_cook_member(dt_node_t *dnp, uint_t idflags)
4267 dnp->dn_membexpr = dt_node_cook(dnp->dn_membexpr, idflags);
4268 dt_node_attr_assign(dnp, dnp->dn_membexpr->dn_attr);
4269 return (dnp);
4272 /*ARGSUSED*/
4273 static dt_node_t *
4274 dt_cook_xlator(dt_node_t *dnp, uint_t idflags)
4276 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
4277 dt_xlator_t *dxp = dnp->dn_xlator;
4278 dt_node_t *mnp;
4280 char n1[DT_TYPE_NAMELEN];
4281 char n2[DT_TYPE_NAMELEN];
4283 dtrace_attribute_t attr = _dtrace_maxattr;
4284 ctf_membinfo_t ctm;
4285 ctf_id_t type;
4286 ctf_file_t *ctfp;
4289 * Before cooking each translator member, we push a reference to the
4290 * hash containing translator-local identifiers on to pcb_globals to
4291 * temporarily interpose these identifiers in front of other globals.
4293 dt_idstack_push(&yypcb->pcb_globals, dxp->dx_locals);
4295 for (mnp = dnp->dn_members; mnp != NULL; mnp = mnp->dn_list) {
4296 if (ctf_member_info(dxp->dx_dst_ctfp, dxp->dx_dst_type,
4297 mnp->dn_membname, &ctm) == CTF_ERR) {
4298 xyerror(D_XLATE_MEMB,
4299 "translator member %s is not a member of %s\n",
4300 mnp->dn_membname, ctf_type_name(dxp->dx_dst_ctfp,
4301 dxp->dx_dst_type, n1, sizeof (n1)));
4304 (void) dt_node_cook(mnp, DT_IDFLG_REF);
4305 ctfp = dxp->dx_dst_ctfp;
4306 type = ctm.ctm_type;
4310 * This probably doesn't need to be resolved, because it's of
4311 * the translator, but is done for completeness right now.
4313 dt_resolve_forward_decl(&ctfp, &type);
4314 dt_node_type_assign(mnp, ctfp, type,
4315 B_FALSE);
4316 attr = dt_attr_min(attr, mnp->dn_attr);
4318 if (dt_node_is_argcompat(mnp, mnp->dn_membexpr) == 0) {
4319 xyerror(D_XLATE_INCOMPAT,
4320 "translator member %s definition uses "
4321 "incompatible types: \"%s\" = \"%s\"\n",
4322 mnp->dn_membname,
4323 dt_node_type_name(mnp, n1, sizeof (n1)),
4324 dt_node_type_name(mnp->dn_membexpr,
4325 n2, sizeof (n2)));
4329 dt_idstack_pop(&yypcb->pcb_globals, dxp->dx_locals);
4331 dxp->dx_souid.di_attr = attr;
4332 dxp->dx_ptrid.di_attr = attr;
4334 dt_node_type_assign(dnp, DT_DYN_CTFP(dtp), DT_DYN_TYPE(dtp), B_FALSE);
4335 dt_node_attr_assign(dnp, _dtrace_defattr);
4337 return (dnp);
4340 static void
4341 dt_node_provider_cmp_argv(dt_provider_t *pvp, dt_node_t *pnp, const char *kind,
4342 uint_t old_argc, dt_node_t *old_argv, uint_t new_argc, dt_node_t *new_argv)
4344 dt_probe_t *prp = pnp->dn_ident->di_data;
4345 uint_t i;
4347 char n1[DT_TYPE_NAMELEN];
4348 char n2[DT_TYPE_NAMELEN];
4350 if (old_argc != new_argc) {
4351 dnerror(pnp, D_PROV_INCOMPAT,
4352 "probe %s:%s %s prototype mismatch:\n"
4353 "\t current: %u arg%s\n\tprevious: %u arg%s\n",
4354 pvp->pv_desc.dtvd_name, prp->pr_ident->di_name, kind,
4355 new_argc, new_argc != 1 ? "s" : "",
4356 old_argc, old_argc != 1 ? "s" : "");
4359 for (i = 0; i < old_argc; i++,
4360 old_argv = old_argv->dn_list, new_argv = new_argv->dn_list) {
4361 if (ctf_type_cmp(old_argv->dn_ctfp, old_argv->dn_type,
4362 new_argv->dn_ctfp, new_argv->dn_type) == 0)
4363 continue;
4365 dnerror(pnp, D_PROV_INCOMPAT,
4366 "probe %s:%s %s prototype argument #%u mismatch:\n"
4367 "\t current: %s\n\tprevious: %s\n",
4368 pvp->pv_desc.dtvd_name, prp->pr_ident->di_name, kind, i + 1,
4369 dt_node_type_name(new_argv, n1, sizeof (n1)),
4370 dt_node_type_name(old_argv, n2, sizeof (n2)));
4375 * Compare a new probe declaration with an existing probe definition (either
4376 * from a previous declaration or cached from the kernel). If the existing
4377 * definition and declaration both have an input and output parameter list,
4378 * compare both lists. Otherwise compare only the output parameter lists.
4380 static void
4381 dt_node_provider_cmp(dt_provider_t *pvp, dt_node_t *pnp,
4382 dt_probe_t *old, dt_probe_t *new)
4384 dt_node_provider_cmp_argv(pvp, pnp, "output",
4385 old->pr_xargc, old->pr_xargs, new->pr_xargc, new->pr_xargs);
4387 if (old->pr_nargs != old->pr_xargs && new->pr_nargs != new->pr_xargs) {
4388 dt_node_provider_cmp_argv(pvp, pnp, "input",
4389 old->pr_nargc, old->pr_nargs, new->pr_nargc, new->pr_nargs);
4392 if (old->pr_nargs == old->pr_xargs && new->pr_nargs != new->pr_xargs) {
4393 if (pvp->pv_flags & DT_PROVIDER_IMPL) {
4394 dnerror(pnp, D_PROV_INCOMPAT,
4395 "provider interface mismatch: %s\n"
4396 "\t current: probe %s:%s has an output prototype\n"
4397 "\tprevious: probe %s:%s has no output prototype\n",
4398 pvp->pv_desc.dtvd_name, pvp->pv_desc.dtvd_name,
4399 new->pr_ident->di_name, pvp->pv_desc.dtvd_name,
4400 old->pr_ident->di_name);
4403 if (old->pr_ident->di_gen == yypcb->pcb_hdl->dt_gen)
4404 old->pr_ident->di_flags |= DT_IDFLG_ORPHAN;
4406 dt_idhash_delete(pvp->pv_probes, old->pr_ident);
4407 dt_probe_declare(pvp, new);
4411 static void
4412 dt_cook_probe(dt_node_t *dnp, dt_provider_t *pvp)
4414 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
4415 dt_probe_t *prp = dnp->dn_ident->di_data;
4417 dt_xlator_t *dxp;
4418 uint_t i;
4420 char n1[DT_TYPE_NAMELEN];
4421 char n2[DT_TYPE_NAMELEN];
4423 if (prp->pr_nargs == prp->pr_xargs)
4424 return;
4426 for (i = 0; i < prp->pr_xargc; i++) {
4427 dt_node_t *xnp = prp->pr_xargv[i];
4428 dt_node_t *nnp = prp->pr_nargv[prp->pr_mapping[i]];
4430 if ((dxp = dt_xlator_lookup(dtp,
4431 nnp, xnp, DT_XLATE_FUZZY)) != NULL) {
4432 if (dt_provider_xref(dtp, pvp, dxp->dx_id) != 0)
4433 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
4434 continue;
4437 if (dt_node_is_argcompat(nnp, xnp))
4438 continue; /* no translator defined and none required */
4440 dnerror(dnp, D_PROV_PRXLATOR, "translator for %s:%s output "
4441 "argument #%u from %s to %s is not defined\n",
4442 pvp->pv_desc.dtvd_name, dnp->dn_ident->di_name, i + 1,
4443 dt_node_type_name(nnp, n1, sizeof (n1)),
4444 dt_node_type_name(xnp, n2, sizeof (n2)));
4448 /*ARGSUSED*/
4449 static dt_node_t *
4450 dt_cook_provider(dt_node_t *dnp, uint_t idflags)
4452 dt_provider_t *pvp = dnp->dn_provider;
4453 dt_node_t *pnp;
4456 * If we're declaring a provider for the first time and it is unknown
4457 * to dtrace(7D), insert the probe definitions into the provider's hash.
4458 * If we're redeclaring a known provider, verify the interface matches.
4460 for (pnp = dnp->dn_probes; pnp != NULL; pnp = pnp->dn_list) {
4461 const char *probename = pnp->dn_ident->di_name;
4462 dt_probe_t *prp = dt_probe_lookup(pvp, probename);
4464 assert(pnp->dn_kind == DT_NODE_PROBE);
4466 if (prp != NULL && dnp->dn_provred) {
4467 dt_node_provider_cmp(pvp, pnp,
4468 prp, pnp->dn_ident->di_data);
4469 } else if (prp == NULL && dnp->dn_provred) {
4470 dnerror(pnp, D_PROV_INCOMPAT,
4471 "provider interface mismatch: %s\n"
4472 "\t current: probe %s:%s defined\n"
4473 "\tprevious: probe %s:%s not defined\n",
4474 dnp->dn_provname, dnp->dn_provname,
4475 probename, dnp->dn_provname, probename);
4476 } else if (prp != NULL) {
4477 dnerror(pnp, D_PROV_PRDUP, "probe redeclared: %s:%s\n",
4478 dnp->dn_provname, probename);
4479 } else
4480 dt_probe_declare(pvp, pnp->dn_ident->di_data);
4482 dt_cook_probe(pnp, pvp);
4485 return (dnp);
4488 /*ARGSUSED*/
4489 static dt_node_t *
4490 dt_cook_none(dt_node_t *dnp, uint_t idflags)
4492 return (dnp);
4495 static dt_node_t *(*dt_cook_funcs[])(dt_node_t *, uint_t) = {
4496 dt_cook_none, /* DT_NODE_FREE */
4497 dt_cook_none, /* DT_NODE_INT */
4498 dt_cook_none, /* DT_NODE_STRING */
4499 dt_cook_ident, /* DT_NODE_IDENT */
4500 dt_cook_var, /* DT_NODE_VAR */
4501 dt_cook_none, /* DT_NODE_SYM */
4502 dt_cook_none, /* DT_NODE_TYPE */
4503 dt_cook_func, /* DT_NODE_FUNC */
4504 dt_cook_op1, /* DT_NODE_OP1 */
4505 dt_cook_op2, /* DT_NODE_OP2 */
4506 dt_cook_op3, /* DT_NODE_OP3 */
4507 dt_cook_statement, /* DT_NODE_DEXPR */
4508 dt_cook_statement, /* DT_NODE_DFUNC */
4509 dt_cook_aggregation, /* DT_NODE_AGG */
4510 dt_cook_none, /* DT_NODE_PDESC */
4511 dt_cook_clause, /* DT_NODE_CLAUSE */
4512 dt_cook_inline, /* DT_NODE_INLINE */
4513 dt_cook_member, /* DT_NODE_MEMBER */
4514 dt_cook_xlator, /* DT_NODE_XLATOR */
4515 dt_cook_none, /* DT_NODE_PROBE */
4516 dt_cook_provider, /* DT_NODE_PROVIDER */
4517 dt_cook_none, /* DT_NODE_PROG */
4518 dt_cook_none, /* DT_NODE_IF */
4522 * Recursively cook the parse tree starting at the specified node. The idflags
4523 * parameter is used to indicate the type of reference (r/w) and is applied to
4524 * the resulting identifier if it is a D variable or D aggregation.
4526 dt_node_t *
4527 dt_node_cook(dt_node_t *dnp, uint_t idflags)
4529 int oldlineno = yylineno;
4531 yylineno = dnp->dn_line;
4533 assert(dnp->dn_kind <
4534 sizeof (dt_cook_funcs) / sizeof (dt_cook_funcs[0]));
4535 dnp = dt_cook_funcs[dnp->dn_kind](dnp, idflags);
4536 dnp->dn_flags |= DT_NF_COOKED;
4538 if (dnp->dn_kind == DT_NODE_VAR || dnp->dn_kind == DT_NODE_AGG)
4539 dnp->dn_ident->di_flags |= idflags;
4541 yylineno = oldlineno;
4542 return (dnp);
4545 dtrace_attribute_t
4546 dt_node_list_cook(dt_node_t **pnp, uint_t idflags)
4548 dtrace_attribute_t attr = _dtrace_defattr;
4549 dt_node_t *dnp, *nnp;
4551 for (dnp = (pnp != NULL ? *pnp : NULL); dnp != NULL; dnp = nnp) {
4552 nnp = dnp->dn_list;
4553 dnp = *pnp = dt_node_cook(dnp, idflags);
4554 attr = dt_attr_min(attr, dnp->dn_attr);
4555 dnp->dn_list = nnp;
4556 pnp = &dnp->dn_list;
4559 return (attr);
4562 void
4563 dt_node_list_free(dt_node_t **pnp)
4565 dt_node_t *dnp, *nnp;
4567 for (dnp = (pnp != NULL ? *pnp : NULL); dnp != NULL; dnp = nnp) {
4568 nnp = dnp->dn_list;
4569 dt_node_free(dnp);
4572 if (pnp != NULL)
4573 *pnp = NULL;
4576 void
4577 dt_node_link_free(dt_node_t **pnp)
4579 dt_node_t *dnp, *nnp;
4581 for (dnp = (pnp != NULL ? *pnp : NULL); dnp != NULL; dnp = nnp) {
4582 nnp = dnp->dn_link;
4583 dt_node_free(dnp);
4586 for (dnp = (pnp != NULL ? *pnp : NULL); dnp != NULL; dnp = nnp) {
4587 nnp = dnp->dn_link;
4588 free(dnp);
4591 if (pnp != NULL)
4592 *pnp = NULL;
4595 dt_node_t *
4596 dt_node_link(dt_node_t *lp, dt_node_t *rp)
4598 dt_node_t *dnp;
4600 if (lp == NULL)
4601 return (rp);
4602 else if (rp == NULL)
4603 return (lp);
4605 for (dnp = lp; dnp->dn_list != NULL; dnp = dnp->dn_list)
4606 continue;
4608 dnp->dn_list = rp;
4609 return (lp);
4613 * Compute the DOF dtrace_diftype_t representation of a node's type. This is
4614 * called from a variety of places in the library so it cannot assume yypcb
4615 * is valid: any references to handle-specific data must be made through 'dtp'.
4617 void
4618 dt_node_diftype(dtrace_hdl_t *dtp, const dt_node_t *dnp, dtrace_diftype_t *tp)
4620 if (dnp->dn_ctfp == DT_STR_CTFP(dtp) &&
4621 dnp->dn_type == DT_STR_TYPE(dtp)) {
4622 tp->dtdt_kind = DIF_TYPE_STRING;
4623 tp->dtdt_ckind = CTF_K_UNKNOWN;
4624 } else {
4625 tp->dtdt_kind = DIF_TYPE_CTF;
4626 tp->dtdt_ckind = ctf_type_kind(dnp->dn_ctfp,
4627 ctf_type_resolve(dnp->dn_ctfp, dnp->dn_type));
4630 tp->dtdt_flags = (dnp->dn_flags & DT_NF_REF) ?
4631 (dnp->dn_flags & DT_NF_USERLAND) ? DIF_TF_BYUREF :
4632 DIF_TF_BYREF : 0;
4633 tp->dtdt_pad = 0;
4634 tp->dtdt_size = ctf_type_size(dnp->dn_ctfp, dnp->dn_type);
4638 * Output the parse tree as D. The "-xtree=8" argument will call this
4639 * function to print out the program after any syntactic sugar
4640 * transformations have been applied (e.g. to implement "if"). The
4641 * resulting output can be used to understand the transformations
4642 * applied by these features, or to run such a script on a system that
4643 * does not support these features
4645 * Note that the output does not express precisely the same program as
4646 * the input. In particular:
4647 * - Only the clauses are output. #pragma options, variable
4648 * declarations, etc. are excluded.
4649 * - Command argument substitution has already been done, so the output
4650 * will not contain e.g. $$1, but rather the substituted string.
4652 void
4653 dt_printd(dt_node_t *dnp, FILE *fp, int depth)
4655 dt_node_t *arg;
4657 switch (dnp->dn_kind) {
4658 case DT_NODE_INT:
4659 (void) fprintf(fp, "0x%llx", (u_longlong_t)dnp->dn_value);
4660 if (!(dnp->dn_flags & DT_NF_SIGNED))
4661 (void) fprintf(fp, "u");
4662 break;
4664 case DT_NODE_STRING: {
4665 char *escd = strchr2esc(dnp->dn_string, strlen(dnp->dn_string));
4666 (void) fprintf(fp, "\"%s\"", escd);
4667 free(escd);
4668 break;
4671 case DT_NODE_IDENT:
4672 (void) fprintf(fp, "%s", dnp->dn_string);
4673 break;
4675 case DT_NODE_VAR:
4676 (void) fprintf(fp, "%s%s",
4677 (dnp->dn_ident->di_flags & DT_IDFLG_LOCAL) ? "this->" :
4678 (dnp->dn_ident->di_flags & DT_IDFLG_TLS) ? "self->" : "",
4679 dnp->dn_ident->di_name);
4681 if (dnp->dn_args != NULL) {
4682 (void) fprintf(fp, "[");
4684 for (arg = dnp->dn_args; arg != NULL;
4685 arg = arg->dn_list) {
4686 dt_printd(arg, fp, 0);
4687 if (arg->dn_list != NULL)
4688 (void) fprintf(fp, ", ");
4691 (void) fprintf(fp, "]");
4693 break;
4695 case DT_NODE_SYM: {
4696 const dtrace_syminfo_t *dts = dnp->dn_ident->di_data;
4697 (void) fprintf(fp, "%s`%s", dts->dts_object, dts->dts_name);
4698 break;
4700 case DT_NODE_FUNC:
4701 (void) fprintf(fp, "%s(", dnp->dn_ident->di_name);
4703 for (arg = dnp->dn_args; arg != NULL; arg = arg->dn_list) {
4704 dt_printd(arg, fp, 0);
4705 if (arg->dn_list != NULL)
4706 (void) fprintf(fp, ", ");
4708 (void) fprintf(fp, ")");
4709 break;
4711 case DT_NODE_OP1:
4712 (void) fprintf(fp, "%s(", opstr(dnp->dn_op));
4713 dt_printd(dnp->dn_child, fp, 0);
4714 (void) fprintf(fp, ")");
4715 break;
4717 case DT_NODE_OP2:
4718 (void) fprintf(fp, "(");
4719 dt_printd(dnp->dn_left, fp, 0);
4720 if (dnp->dn_op == DT_TOK_LPAR) {
4721 (void) fprintf(fp, ")");
4722 dt_printd(dnp->dn_right, fp, 0);
4723 break;
4725 if (dnp->dn_op == DT_TOK_PTR || dnp->dn_op == DT_TOK_DOT ||
4726 dnp->dn_op == DT_TOK_LBRAC)
4727 (void) fprintf(fp, "%s", opstr(dnp->dn_op));
4728 else
4729 (void) fprintf(fp, " %s ", opstr(dnp->dn_op));
4730 dt_printd(dnp->dn_right, fp, 0);
4731 if (dnp->dn_op == DT_TOK_LBRAC) {
4732 dt_node_t *ln = dnp->dn_right;
4733 while (ln->dn_list != NULL) {
4734 (void) fprintf(fp, ", ");
4735 dt_printd(ln->dn_list, fp, depth);
4736 ln = ln->dn_list;
4738 (void) fprintf(fp, "]");
4740 (void) fprintf(fp, ")");
4741 break;
4743 case DT_NODE_OP3:
4744 (void) fprintf(fp, "(");
4745 dt_printd(dnp->dn_expr, fp, 0);
4746 (void) fprintf(fp, " ? ");
4747 dt_printd(dnp->dn_left, fp, 0);
4748 (void) fprintf(fp, " : ");
4749 dt_printd(dnp->dn_right, fp, 0);
4750 (void) fprintf(fp, ")");
4751 break;
4753 case DT_NODE_DEXPR:
4754 case DT_NODE_DFUNC:
4755 (void) fprintf(fp, "%*s", depth * 8, "");
4756 dt_printd(dnp->dn_expr, fp, depth + 1);
4757 (void) fprintf(fp, ";\n");
4758 break;
4760 case DT_NODE_PDESC:
4761 (void) fprintf(fp, "%s:%s:%s:%s",
4762 dnp->dn_desc->dtpd_provider, dnp->dn_desc->dtpd_mod,
4763 dnp->dn_desc->dtpd_func, dnp->dn_desc->dtpd_name);
4764 break;
4766 case DT_NODE_CLAUSE:
4767 for (arg = dnp->dn_pdescs; arg != NULL; arg = arg->dn_list) {
4768 dt_printd(arg, fp, 0);
4769 if (arg->dn_list != NULL)
4770 (void) fprintf(fp, ",");
4771 (void) fprintf(fp, "\n");
4774 if (dnp->dn_pred != NULL) {
4775 (void) fprintf(fp, "/");
4776 dt_printd(dnp->dn_pred, fp, 0);
4777 (void) fprintf(fp, "/\n");
4779 (void) fprintf(fp, "{\n");
4781 for (arg = dnp->dn_acts; arg != NULL; arg = arg->dn_list)
4782 dt_printd(arg, fp, depth + 1);
4783 (void) fprintf(fp, "}\n");
4784 (void) fprintf(fp, "\n");
4785 break;
4787 case DT_NODE_IF:
4788 (void) fprintf(fp, "%*sif (", depth * 8, "");
4789 dt_printd(dnp->dn_conditional, fp, 0);
4790 (void) fprintf(fp, ") {\n");
4792 for (arg = dnp->dn_body; arg != NULL; arg = arg->dn_list)
4793 dt_printd(arg, fp, depth + 1);
4794 if (dnp->dn_alternate_body == NULL) {
4795 (void) fprintf(fp, "%*s}\n", depth * 8, "");
4796 } else {
4797 (void) fprintf(fp, "%*s} else {\n", depth * 8, "");
4798 for (arg = dnp->dn_alternate_body; arg != NULL;
4799 arg = arg->dn_list)
4800 dt_printd(arg, fp, depth + 1);
4801 (void) fprintf(fp, "%*s}\n", depth * 8, "");
4804 break;
4806 default:
4807 (void) fprintf(fp, "/* bad node %p, kind %d */\n",
4808 (void *)dnp, dnp->dn_kind);
4812 void
4813 dt_node_printr(dt_node_t *dnp, FILE *fp, int depth)
4815 char n[DT_TYPE_NAMELEN], buf[BUFSIZ], a[8];
4816 const dtrace_syminfo_t *dts;
4817 const dt_idnode_t *inp;
4818 dt_node_t *arg;
4820 (void) fprintf(fp, "%*s", depth * 2, "");
4821 (void) dt_attr_str(dnp->dn_attr, a, sizeof (a));
4823 if (dnp->dn_ctfp != NULL && dnp->dn_type != CTF_ERR &&
4824 ctf_type_name(dnp->dn_ctfp, dnp->dn_type, n, sizeof (n)) != NULL) {
4825 (void) snprintf(buf, BUFSIZ, "type=<%s> attr=%s flags=", n, a);
4826 } else {
4827 (void) snprintf(buf, BUFSIZ, "type=<%ld> attr=%s flags=",
4828 dnp->dn_type, a);
4831 if (dnp->dn_flags != 0) {
4832 n[0] = '\0';
4833 if (dnp->dn_flags & DT_NF_SIGNED)
4834 (void) strcat(n, ",SIGN");
4835 if (dnp->dn_flags & DT_NF_COOKED)
4836 (void) strcat(n, ",COOK");
4837 if (dnp->dn_flags & DT_NF_REF)
4838 (void) strcat(n, ",REF");
4839 if (dnp->dn_flags & DT_NF_LVALUE)
4840 (void) strcat(n, ",LVAL");
4841 if (dnp->dn_flags & DT_NF_WRITABLE)
4842 (void) strcat(n, ",WRITE");
4843 if (dnp->dn_flags & DT_NF_BITFIELD)
4844 (void) strcat(n, ",BITF");
4845 if (dnp->dn_flags & DT_NF_USERLAND)
4846 (void) strcat(n, ",USER");
4847 (void) strcat(buf, n + 1);
4848 } else
4849 (void) strcat(buf, "0");
4851 switch (dnp->dn_kind) {
4852 case DT_NODE_FREE:
4853 (void) fprintf(fp, "FREE <node %p>\n", (void *)dnp);
4854 break;
4856 case DT_NODE_INT:
4857 (void) fprintf(fp, "INT 0x%llx (%s)\n",
4858 (u_longlong_t)dnp->dn_value, buf);
4859 break;
4861 case DT_NODE_STRING:
4862 (void) fprintf(fp, "STRING \"%s\" (%s)\n", dnp->dn_string, buf);
4863 break;
4865 case DT_NODE_IDENT:
4866 (void) fprintf(fp, "IDENT %s (%s)\n", dnp->dn_string, buf);
4867 break;
4869 case DT_NODE_VAR:
4870 (void) fprintf(fp, "VARIABLE %s%s (%s)\n",
4871 (dnp->dn_ident->di_flags & DT_IDFLG_LOCAL) ? "this->" :
4872 (dnp->dn_ident->di_flags & DT_IDFLG_TLS) ? "self->" : "",
4873 dnp->dn_ident->di_name, buf);
4875 if (dnp->dn_args != NULL)
4876 (void) fprintf(fp, "%*s[\n", depth * 2, "");
4878 for (arg = dnp->dn_args; arg != NULL; arg = arg->dn_list) {
4879 dt_node_printr(arg, fp, depth + 1);
4880 if (arg->dn_list != NULL)
4881 (void) fprintf(fp, "%*s,\n", depth * 2, "");
4884 if (dnp->dn_args != NULL)
4885 (void) fprintf(fp, "%*s]\n", depth * 2, "");
4886 break;
4888 case DT_NODE_SYM:
4889 dts = dnp->dn_ident->di_data;
4890 (void) fprintf(fp, "SYMBOL %s`%s (%s)\n",
4891 dts->dts_object, dts->dts_name, buf);
4892 break;
4894 case DT_NODE_TYPE:
4895 if (dnp->dn_string != NULL) {
4896 (void) fprintf(fp, "TYPE (%s) %s\n",
4897 buf, dnp->dn_string);
4898 } else
4899 (void) fprintf(fp, "TYPE (%s)\n", buf);
4900 break;
4902 case DT_NODE_FUNC:
4903 (void) fprintf(fp, "FUNC %s (%s)\n",
4904 dnp->dn_ident->di_name, buf);
4906 for (arg = dnp->dn_args; arg != NULL; arg = arg->dn_list) {
4907 dt_node_printr(arg, fp, depth + 1);
4908 if (arg->dn_list != NULL)
4909 (void) fprintf(fp, "%*s,\n", depth * 2, "");
4911 break;
4913 case DT_NODE_OP1:
4914 (void) fprintf(fp, "OP1 %s (%s)\n", opstr(dnp->dn_op), buf);
4915 dt_node_printr(dnp->dn_child, fp, depth + 1);
4916 break;
4918 case DT_NODE_OP2:
4919 (void) fprintf(fp, "OP2 %s (%s)\n", opstr(dnp->dn_op), buf);
4920 dt_node_printr(dnp->dn_left, fp, depth + 1);
4921 dt_node_printr(dnp->dn_right, fp, depth + 1);
4922 if (dnp->dn_op == DT_TOK_LBRAC) {
4923 dt_node_t *ln = dnp->dn_right;
4924 while (ln->dn_list != NULL) {
4925 dt_node_printr(ln->dn_list, fp, depth + 1);
4926 ln = ln->dn_list;
4929 break;
4931 case DT_NODE_OP3:
4932 (void) fprintf(fp, "OP3 (%s)\n", buf);
4933 dt_node_printr(dnp->dn_expr, fp, depth + 1);
4934 (void) fprintf(fp, "%*s?\n", depth * 2, "");
4935 dt_node_printr(dnp->dn_left, fp, depth + 1);
4936 (void) fprintf(fp, "%*s:\n", depth * 2, "");
4937 dt_node_printr(dnp->dn_right, fp, depth + 1);
4938 break;
4940 case DT_NODE_DEXPR:
4941 case DT_NODE_DFUNC:
4942 (void) fprintf(fp, "D EXPRESSION attr=%s\n", a);
4943 dt_node_printr(dnp->dn_expr, fp, depth + 1);
4944 break;
4946 case DT_NODE_AGG:
4947 (void) fprintf(fp, "AGGREGATE @%s attr=%s [\n",
4948 dnp->dn_ident->di_name, a);
4950 for (arg = dnp->dn_aggtup; arg != NULL; arg = arg->dn_list) {
4951 dt_node_printr(arg, fp, depth + 1);
4952 if (arg->dn_list != NULL)
4953 (void) fprintf(fp, "%*s,\n", depth * 2, "");
4956 if (dnp->dn_aggfun) {
4957 (void) fprintf(fp, "%*s] = ", depth * 2, "");
4958 dt_node_printr(dnp->dn_aggfun, fp, depth + 1);
4959 } else
4960 (void) fprintf(fp, "%*s]\n", depth * 2, "");
4962 if (dnp->dn_aggfun)
4963 (void) fprintf(fp, "%*s)\n", depth * 2, "");
4964 break;
4966 case DT_NODE_PDESC:
4967 (void) fprintf(fp, "PDESC %s:%s:%s:%s [%u]\n",
4968 dnp->dn_desc->dtpd_provider, dnp->dn_desc->dtpd_mod,
4969 dnp->dn_desc->dtpd_func, dnp->dn_desc->dtpd_name,
4970 dnp->dn_desc->dtpd_id);
4971 break;
4973 case DT_NODE_CLAUSE:
4974 (void) fprintf(fp, "CLAUSE attr=%s\n", a);
4976 for (arg = dnp->dn_pdescs; arg != NULL; arg = arg->dn_list)
4977 dt_node_printr(arg, fp, depth + 1);
4979 (void) fprintf(fp, "%*sCTXATTR %s\n", depth * 2, "",
4980 dt_attr_str(dnp->dn_ctxattr, a, sizeof (a)));
4982 if (dnp->dn_pred != NULL) {
4983 (void) fprintf(fp, "%*sPREDICATE /\n", depth * 2, "");
4984 dt_node_printr(dnp->dn_pred, fp, depth + 1);
4985 (void) fprintf(fp, "%*s/\n", depth * 2, "");
4988 for (arg = dnp->dn_acts; arg != NULL; arg = arg->dn_list)
4989 dt_node_printr(arg, fp, depth + 1);
4990 (void) fprintf(fp, "\n");
4991 break;
4993 case DT_NODE_INLINE:
4994 inp = dnp->dn_ident->di_iarg;
4996 (void) fprintf(fp, "INLINE %s (%s)\n",
4997 dnp->dn_ident->di_name, buf);
4998 dt_node_printr(inp->din_root, fp, depth + 1);
4999 break;
5001 case DT_NODE_MEMBER:
5002 (void) fprintf(fp, "MEMBER %s (%s)\n", dnp->dn_membname, buf);
5003 if (dnp->dn_membexpr)
5004 dt_node_printr(dnp->dn_membexpr, fp, depth + 1);
5005 break;
5007 case DT_NODE_XLATOR:
5008 (void) fprintf(fp, "XLATOR (%s)", buf);
5010 if (ctf_type_name(dnp->dn_xlator->dx_src_ctfp,
5011 dnp->dn_xlator->dx_src_type, n, sizeof (n)) != NULL)
5012 (void) fprintf(fp, " from <%s>", n);
5014 if (ctf_type_name(dnp->dn_xlator->dx_dst_ctfp,
5015 dnp->dn_xlator->dx_dst_type, n, sizeof (n)) != NULL)
5016 (void) fprintf(fp, " to <%s>", n);
5018 (void) fprintf(fp, "\n");
5020 for (arg = dnp->dn_members; arg != NULL; arg = arg->dn_list)
5021 dt_node_printr(arg, fp, depth + 1);
5022 break;
5024 case DT_NODE_PROBE:
5025 (void) fprintf(fp, "PROBE %s\n", dnp->dn_ident->di_name);
5026 break;
5028 case DT_NODE_PROVIDER:
5029 (void) fprintf(fp, "PROVIDER %s (%s)\n",
5030 dnp->dn_provname, dnp->dn_provred ? "redecl" : "decl");
5031 for (arg = dnp->dn_probes; arg != NULL; arg = arg->dn_list)
5032 dt_node_printr(arg, fp, depth + 1);
5033 break;
5035 case DT_NODE_PROG:
5036 (void) fprintf(fp, "PROGRAM attr=%s\n", a);
5037 for (arg = dnp->dn_list; arg != NULL; arg = arg->dn_list)
5038 dt_node_printr(arg, fp, depth + 1);
5039 break;
5041 case DT_NODE_IF:
5042 (void) fprintf(fp, "IF attr=%s CONDITION:\n", a);
5044 dt_node_printr(dnp->dn_conditional, fp, depth + 1);
5046 (void) fprintf(fp, "%*sIF BODY: \n", depth * 2, "");
5047 for (arg = dnp->dn_body; arg != NULL; arg = arg->dn_list)
5048 dt_node_printr(arg, fp, depth + 1);
5050 if (dnp->dn_alternate_body != NULL) {
5051 (void) fprintf(fp, "%*sIF ELSE: \n", depth * 2, "");
5052 for (arg = dnp->dn_alternate_body; arg != NULL;
5053 arg = arg->dn_list)
5054 dt_node_printr(arg, fp, depth + 1);
5057 break;
5059 default:
5060 (void) fprintf(fp, "<bad node %p, kind %d>\n",
5061 (void *)dnp, dnp->dn_kind);
5066 dt_node_root(dt_node_t *dnp)
5068 yypcb->pcb_root = dnp;
5069 return (0);
5072 /*PRINTFLIKE3*/
5073 void
5074 dnerror(const dt_node_t *dnp, dt_errtag_t tag, const char *format, ...)
5076 int oldlineno = yylineno;
5077 va_list ap;
5079 yylineno = dnp->dn_line;
5081 va_start(ap, format);
5082 xyvwarn(tag, format, ap);
5083 va_end(ap);
5085 yylineno = oldlineno;
5086 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
5089 /*PRINTFLIKE3*/
5090 void
5091 dnwarn(const dt_node_t *dnp, dt_errtag_t tag, const char *format, ...)
5093 int oldlineno = yylineno;
5094 va_list ap;
5096 yylineno = dnp->dn_line;
5098 va_start(ap, format);
5099 xyvwarn(tag, format, ap);
5100 va_end(ap);
5102 yylineno = oldlineno;
5105 /*PRINTFLIKE2*/
5106 void
5107 xyerror(dt_errtag_t tag, const char *format, ...)
5109 va_list ap;
5111 va_start(ap, format);
5112 xyvwarn(tag, format, ap);
5113 va_end(ap);
5115 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
5118 /*PRINTFLIKE2*/
5119 void
5120 xywarn(dt_errtag_t tag, const char *format, ...)
5122 va_list ap;
5124 va_start(ap, format);
5125 xyvwarn(tag, format, ap);
5126 va_end(ap);
5129 void
5130 xyvwarn(dt_errtag_t tag, const char *format, va_list ap)
5132 if (yypcb == NULL)
5133 return; /* compiler is not currently active: act as a no-op */
5135 dt_set_errmsg(yypcb->pcb_hdl, dt_errtag(tag), yypcb->pcb_region,
5136 yypcb->pcb_filetag, yypcb->pcb_fileptr ? yylineno : 0, format, ap);
5139 /*PRINTFLIKE1*/
5140 void
5141 yyerror(const char *format, ...)
5143 va_list ap;
5145 va_start(ap, format);
5146 yyvwarn(format, ap);
5147 va_end(ap);
5149 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
5152 /*PRINTFLIKE1*/
5153 void
5154 yywarn(const char *format, ...)
5156 va_list ap;
5158 va_start(ap, format);
5159 yyvwarn(format, ap);
5160 va_end(ap);
5163 void
5164 yyvwarn(const char *format, va_list ap)
5166 if (yypcb == NULL)
5167 return; /* compiler is not currently active: act as a no-op */
5169 dt_set_errmsg(yypcb->pcb_hdl, dt_errtag(D_SYNTAX), yypcb->pcb_region,
5170 yypcb->pcb_filetag, yypcb->pcb_fileptr ? yylineno : 0, format, ap);
5172 if (strchr(format, '\n') == NULL) {
5173 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
5174 size_t len = strlen(dtp->dt_errmsg);
5175 char *p, *s = dtp->dt_errmsg + len;
5176 size_t n = sizeof (dtp->dt_errmsg) - len;
5178 if (yytext[0] == '\0')
5179 (void) snprintf(s, n, " near end of input");
5180 else if (yytext[0] == '\n')
5181 (void) snprintf(s, n, " near end of line");
5182 else {
5183 if ((p = strchr(yytext, '\n')) != NULL)
5184 *p = '\0'; /* crop at newline */
5185 (void) snprintf(s, n, " near \"%s\"", yytext);
5190 void
5191 yylabel(const char *label)
5193 dt_dprintf("set label to <%s>\n", label ? label : "NULL");
5194 yypcb->pcb_region = label;
5198 yywrap(void)
5200 return (1); /* indicate that lex should return a zero token for EOF */