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.
7 *
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.
12 *
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]
18 *
19 * CDDL HEADER END
20 */
21
22 /*
23 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2011, Joyent Inc. All rights reserved.
25 * Copyright (c) 2012 by Delphix. All rights reserved.
26 */
27
28 /*
29 * DTrace D Language Parser
30 *
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.
39 *
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.
45 *
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 ("<?>");
187 }
188 }
189
190 int
191 dt_type_lookup(const char *s, dtrace_typeinfo_t *tip)
192 {
193 static const char delimiters[] = " \t\n\r\v\f*`";
194 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
195 const char *p, *q, *end, *obj;
196
197 for (p = s, end = s + strlen(s); *p != '\0'; p = q) {
198 while (isspace(*p))
199 p++; /* skip leading whitespace prior to token */
200
201 if (p == end || (q = strpbrk(p + 1, delimiters)) == NULL)
202 break; /* empty string or single token remaining */
203
204 if (*q == '`') {
205 char *object = alloca((size_t)(q - p) + 1);
206 char *type = alloca((size_t)(end - s) + 1);
207
208 /*
209 * Copy from the start of the token (p) to the location
210 * backquote (q) to extract the nul-terminated object.
211 */
212 bcopy(p, object, (size_t)(q - p));
213 object[(size_t)(q - p)] = '\0';
214
215 /*
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.
219 */
220 bcopy(s, type, (size_t)(p - s));
221 bcopy(q + 1, type + (size_t)(p - s), strlen(q + 1) + 1);
222
223 if (strchr(q + 1, '`') != NULL)
224 return (dt_set_errno(dtp, EDT_BADSCOPE));
225
226 return (dtrace_lookup_by_type(dtp, object, type, tip));
227 }
228 }
229
230 if (yypcb->pcb_idepth != 0)
231 obj = DTRACE_OBJ_CDEFS;
232 else
233 obj = DTRACE_OBJ_EVERY;
234
235 return (dtrace_lookup_by_type(dtp, obj, s, tip));
236 }
237
238 /*
239 * When we parse type expressions or parse an expression with unary "&", we
240 * need to find a type that is a pointer to a previously known type.
241 * Unfortunately CTF is limited to a per-container view, so ctf_type_pointer()
242 * alone does not suffice for our needs. We provide a more intelligent wrapper
243 * for the compiler that attempts to compute a pointer to either the given type
244 * or its base (that is, we try both "foo_t *" and "struct foo *"), and also
245 * to potentially construct the required type on-the-fly.
246 */
247 int
248 dt_type_pointer(dtrace_typeinfo_t *tip)
249 {
250 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
251 ctf_file_t *ctfp = tip->dtt_ctfp;
252 ctf_id_t type = tip->dtt_type;
253 ctf_id_t base = ctf_type_resolve(ctfp, type);
254
255 dt_module_t *dmp;
256 ctf_id_t ptr;
257
258 if ((ptr = ctf_type_pointer(ctfp, type)) != CTF_ERR ||
259 (ptr = ctf_type_pointer(ctfp, base)) != CTF_ERR) {
260 tip->dtt_type = ptr;
261 return (0);
262 }
263
264 if (yypcb->pcb_idepth != 0)
265 dmp = dtp->dt_cdefs;
266 else
267 dmp = dtp->dt_ddefs;
268
269 if (ctfp != dmp->dm_ctfp && ctfp != ctf_parent_file(dmp->dm_ctfp) &&
270 (type = ctf_add_type(dmp->dm_ctfp, ctfp, type)) == CTF_ERR) {
271 dtp->dt_ctferr = ctf_errno(dmp->dm_ctfp);
272 return (dt_set_errno(dtp, EDT_CTF));
273 }
274
275 ptr = ctf_add_pointer(dmp->dm_ctfp, CTF_ADD_ROOT, type);
276
277 if (ptr == CTF_ERR || ctf_update(dmp->dm_ctfp) == CTF_ERR) {
278 dtp->dt_ctferr = ctf_errno(dmp->dm_ctfp);
279 return (dt_set_errno(dtp, EDT_CTF));
280 }
281
282 tip->dtt_object = dmp->dm_name;
283 tip->dtt_ctfp = dmp->dm_ctfp;
284 tip->dtt_type = ptr;
285
286 return (0);
287 }
288
289 const char *
290 dt_type_name(ctf_file_t *ctfp, ctf_id_t type, char *buf, size_t len)
291 {
292 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
293
294 if (ctfp == DT_FPTR_CTFP(dtp) && type == DT_FPTR_TYPE(dtp))
295 (void) snprintf(buf, len, "function pointer");
296 else if (ctfp == DT_FUNC_CTFP(dtp) && type == DT_FUNC_TYPE(dtp))
297 (void) snprintf(buf, len, "function");
298 else if (ctfp == DT_DYN_CTFP(dtp) && type == DT_DYN_TYPE(dtp))
299 (void) snprintf(buf, len, "dynamic variable");
300 else if (ctfp == NULL)
301 (void) snprintf(buf, len, "<none>");
302 else if (ctf_type_name(ctfp, type, buf, len) == NULL)
303 (void) snprintf(buf, len, "unknown");
304
368 goto return_ltype;
369 } else if (lrank > rrank) {
370 goto return_ltype;
371 } else
372 goto return_rtype;
373
374 return_ltype:
375 *ofp = lfp;
376 *otype = ltype;
377 return;
378
379 return_rtype:
380 *ofp = rfp;
381 *otype = rtype;
382 }
383
384 void
385 dt_node_promote(dt_node_t *lp, dt_node_t *rp, dt_node_t *dnp)
386 {
387 dt_type_promote(lp, rp, &dnp->dn_ctfp, &dnp->dn_type);
388 dt_node_type_assign(dnp, dnp->dn_ctfp, dnp->dn_type);
389 dt_node_attr_assign(dnp, dt_attr_min(lp->dn_attr, rp->dn_attr));
390 }
391
392 const char *
393 dt_node_name(const dt_node_t *dnp, char *buf, size_t len)
394 {
395 char n1[DT_TYPE_NAMELEN];
396 char n2[DT_TYPE_NAMELEN];
397
398 const char *prefix = "", *suffix = "";
399 const dtrace_syminfo_t *dts;
400 char *s;
401
402 switch (dnp->dn_kind) {
403 case DT_NODE_INT:
404 (void) snprintf(buf, len, "integer constant 0x%llx",
405 (u_longlong_t)dnp->dn_value);
406 break;
407 case DT_NODE_STRING:
408 s = strchr2esc(dnp->dn_string, strlen(dnp->dn_string));
637 }
638 }
639
640 void
641 dt_node_attr_assign(dt_node_t *dnp, dtrace_attribute_t attr)
642 {
643 if ((yypcb->pcb_cflags & DTRACE_C_EATTR) &&
644 (dt_attr_cmp(attr, yypcb->pcb_amin) < 0)) {
645 char a[DTRACE_ATTR2STR_MAX];
646 char s[BUFSIZ];
647
648 dnerror(dnp, D_ATTR_MIN, "attributes for %s (%s) are less than "
649 "predefined minimum\n", dt_node_name(dnp, s, sizeof (s)),
650 dtrace_attr2str(attr, a, sizeof (a)));
651 }
652
653 dnp->dn_attr = attr;
654 }
655
656 void
657 dt_node_type_assign(dt_node_t *dnp, ctf_file_t *fp, ctf_id_t type)
658 {
659 ctf_id_t base = ctf_type_resolve(fp, type);
660 uint_t kind = ctf_type_kind(fp, base);
661 ctf_encoding_t e;
662
663 dnp->dn_flags &=
664 ~(DT_NF_SIGNED | DT_NF_REF | DT_NF_BITFIELD | DT_NF_USERLAND);
665
666 if (kind == CTF_K_INTEGER && ctf_type_encoding(fp, base, &e) == 0) {
667 size_t size = e.cte_bits / NBBY;
668
669 if (size > 8 || (e.cte_bits % NBBY) != 0 || (size & (size - 1)))
670 dnp->dn_flags |= DT_NF_BITFIELD;
671
672 if (e.cte_format & CTF_INT_SIGNED)
673 dnp->dn_flags |= DT_NF_SIGNED;
674 }
675
676 if (kind == CTF_K_FLOAT && ctf_type_encoding(fp, base, &e) == 0) {
677 if (e.cte_bits / NBBY > sizeof (uint64_t))
678 dnp->dn_flags |= DT_NF_REF;
679 }
680
681 if (kind == CTF_K_STRUCT || kind == CTF_K_UNION ||
682 kind == CTF_K_FORWARD ||
683 kind == CTF_K_ARRAY || kind == CTF_K_FUNCTION)
684 dnp->dn_flags |= DT_NF_REF;
685 else if (yypcb != NULL && fp == DT_DYN_CTFP(yypcb->pcb_hdl) &&
686 type == DT_DYN_TYPE(yypcb->pcb_hdl))
687 dnp->dn_flags |= DT_NF_REF;
688
689 dnp->dn_flags |= DT_NF_COOKED;
690 dnp->dn_ctfp = fp;
691 dnp->dn_type = type;
692 }
693
694 void
695 dt_node_type_propagate(const dt_node_t *src, dt_node_t *dst)
696 {
697 assert(src->dn_flags & DT_NF_COOKED);
698 dst->dn_flags = src->dn_flags & ~DT_NF_LVALUE;
699 dst->dn_ctfp = src->dn_ctfp;
700 dst->dn_type = src->dn_type;
701 }
702
703 const char *
704 dt_node_type_name(const dt_node_t *dnp, char *buf, size_t len)
705 {
706 if (dt_node_is_dynamic(dnp) && dnp->dn_ident != NULL) {
707 (void) snprintf(buf, len, "%s",
708 dt_idkind_name(dt_ident_resolve(dnp->dn_ident)->di_kind));
709 return (buf);
710 }
711
712 if (dnp->dn_flags & DT_NF_USERLAND) {
713 size_t n = snprintf(buf, len, "userland ");
714 len = len > n ? len - n : 0;
715 (void) dt_type_name(dnp->dn_ctfp, dnp->dn_type, buf + n, len);
716 return (buf);
717 }
718
719 return (dt_type_name(dnp->dn_ctfp, dnp->dn_type, buf, len));
720 }
721
722 size_t
723 dt_node_type_size(const dt_node_t *dnp)
724 {
725 ctf_id_t base;
726
727 if (dnp->dn_kind == DT_NODE_STRING)
728 return (strlen(dnp->dn_string) + 1);
729
730 if (dt_node_is_dynamic(dnp) && dnp->dn_ident != NULL)
731 return (dt_ident_size(dnp->dn_ident));
732
733 base = ctf_type_resolve(dnp->dn_ctfp, dnp->dn_type);
734
735 if (ctf_type_kind(dnp->dn_ctfp, base) == CTF_K_FORWARD)
736 return (0);
737
738 return (ctf_type_size(dnp->dn_ctfp, dnp->dn_type));
739 }
740
741 /*
742 * Determine if the specified parse tree node references an identifier of the
743 * specified kind, and if so return a pointer to it; otherwise return NULL.
744 * This function resolves the identifier itself, following through any inlines.
745 */
746 dt_ident_t *
747 dt_node_resolve(const dt_node_t *dnp, uint_t idkind)
748 {
749 dt_ident_t *idp;
750
751 switch (dnp->dn_kind) {
752 case DT_NODE_VAR:
753 case DT_NODE_SYM:
754 case DT_NODE_FUNC:
755 case DT_NODE_AGG:
756 case DT_NODE_INLINE:
757 case DT_NODE_PROBE:
1204 int n = (yyintdecimal | (yyintsuffix[0] == 'u')) + 1;
1205 int i = 0;
1206
1207 const char *p;
1208 char c;
1209
1210 dnp->dn_op = DT_TOK_INT;
1211 dnp->dn_value = value;
1212
1213 for (p = yyintsuffix; (c = *p) != '\0'; p++) {
1214 if (c == 'U' || c == 'u')
1215 i += 1;
1216 else if (c == 'L' || c == 'l')
1217 i += 2;
1218 }
1219
1220 for (; i < sizeof (dtp->dt_ints) / sizeof (dtp->dt_ints[0]); i += n) {
1221 if (value <= dtp->dt_ints[i].did_limit) {
1222 dt_node_type_assign(dnp,
1223 dtp->dt_ints[i].did_ctfp,
1224 dtp->dt_ints[i].did_type);
1225
1226 /*
1227 * If a prefix character is present in macro text, add
1228 * in the corresponding operator node (see dt_lex.l).
1229 */
1230 switch (yyintprefix) {
1231 case '+':
1232 return (dt_node_op1(DT_TOK_IPOS, dnp));
1233 case '-':
1234 return (dt_node_op1(DT_TOK_INEG, dnp));
1235 default:
1236 return (dnp);
1237 }
1238 }
1239 }
1240
1241 xyerror(D_INT_OFLOW, "integer constant 0x%llx cannot be represented "
1242 "in any built-in integral type\n", (u_longlong_t)value);
1243 /*NOTREACHED*/
1244 return (NULL); /* keep gcc happy */
1245 }
1246
1247 dt_node_t *
1248 dt_node_string(char *string)
1249 {
1250 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
1251 dt_node_t *dnp;
1252
1253 if (string == NULL)
1254 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
1255
1256 dnp = dt_node_alloc(DT_NODE_STRING);
1257 dnp->dn_op = DT_TOK_STRING;
1258 dnp->dn_string = string;
1259 dt_node_type_assign(dnp, DT_STR_CTFP(dtp), DT_STR_TYPE(dtp));
1260
1261 return (dnp);
1262 }
1263
1264 dt_node_t *
1265 dt_node_ident(char *name)
1266 {
1267 dt_ident_t *idp;
1268 dt_node_t *dnp;
1269
1270 if (name == NULL)
1271 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
1272
1273 /*
1274 * If the identifier is an inlined integer constant, then create an INT
1275 * node that is a clone of the inline parse tree node and return that
1276 * immediately, allowing this inline to be used in parsing contexts
1277 * that require constant expressions (e.g. scalar array sizes).
1278 */
1279 if ((idp = dt_idstack_lookup(&yypcb->pcb_globals, name)) != NULL &&
1315 int err;
1316
1317 /*
1318 * If 'ddp' is NULL, we get a decl by popping the decl stack. This
1319 * form of dt_node_type() is used by parameter rules in dt_grammar.y.
1320 */
1321 if (ddp == NULL)
1322 ddp = dt_decl_pop_param(&name);
1323
1324 err = dt_decl_type(ddp, &dtt);
1325 dt_decl_free(ddp);
1326
1327 if (err != 0) {
1328 free(name);
1329 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
1330 }
1331
1332 dnp = dt_node_alloc(DT_NODE_TYPE);
1333 dnp->dn_op = DT_TOK_IDENT;
1334 dnp->dn_string = name;
1335 dt_node_type_assign(dnp, dtt.dtt_ctfp, dtt.dtt_type);
1336
1337 if (dtt.dtt_ctfp == dtp->dt_cdefs->dm_ctfp ||
1338 dtt.dtt_ctfp == dtp->dt_ddefs->dm_ctfp)
1339 dt_node_attr_assign(dnp, _dtrace_defattr);
1340 else
1341 dt_node_attr_assign(dnp, _dtrace_typattr);
1342
1343 return (dnp);
1344 }
1345
1346 /*
1347 * Create a type node corresponding to a varargs (...) parameter by just
1348 * assigning it type CTF_ERR. The decl processing code will handle this.
1349 */
1350 dt_node_t *
1351 dt_node_vatype(void)
1352 {
1353 dt_node_t *dnp = dt_node_alloc(DT_NODE_TYPE);
1354
1355 dnp->dn_op = DT_TOK_IDENT;
1559 * by a type rather than an integer, then it's an associative
1560 * array (assc). We then expect to match either DT_IDENT_ARRAY
1561 * for associative arrays or DT_IDENT_SCALAR for anything else.
1562 */
1563 assc = ddp->dd_kind == CTF_K_ARRAY &&
1564 ddp->dd_node->dn_kind == DT_NODE_TYPE;
1565
1566 idkind = assc ? DT_IDENT_ARRAY : DT_IDENT_SCALAR;
1567
1568 /*
1569 * Create a fake dt_node_t on the stack so we can determine the
1570 * type of any matching identifier by assigning to this node.
1571 * If the pre-existing ident has its di_type set, propagate
1572 * the type by hand so as not to trigger a prototype check for
1573 * arrays (yet); otherwise we use dt_ident_cook() on the ident
1574 * to ensure it is fully initialized before looking at it.
1575 */
1576 bzero(&idn, sizeof (dt_node_t));
1577
1578 if (idp != NULL && idp->di_type != CTF_ERR)
1579 dt_node_type_assign(&idn, idp->di_ctfp, idp->di_type);
1580 else if (idp != NULL)
1581 (void) dt_ident_cook(&idn, idp, NULL);
1582
1583 if (assc) {
1584 if (class == DT_DC_THIS) {
1585 xyerror(D_DECL_LOCASSC, "associative arrays "
1586 "may not be declared as local variables:"
1587 " %s\n", dsp->ds_ident);
1588 }
1589
1590 if (dt_decl_type(ddp->dd_next, &dtt) != 0)
1591 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
1592 }
1593
1594 if (idp != NULL && (idp->di_kind != idkind ||
1595 ctf_type_cmp(dtt.dtt_ctfp, dtt.dtt_type,
1596 idn.dn_ctfp, idn.dn_type) != 0)) {
1597 xyerror(D_DECL_IDRED, "identifier redeclared: %s\n"
1598 "\t current: %s %s\n\tprevious: %s %s\n",
1599 dsp->ds_ident, dt_idkind_name(idkind),
1768 err = dt_decl_type(ddp, &dtt);
1769 dt_decl_free(ddp);
1770
1771 if (err != 0)
1772 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
1773
1774 type = ctf_type_resolve(dtt.dtt_ctfp, dtt.dtt_type);
1775 kind = ctf_type_kind(dtt.dtt_ctfp, type);
1776
1777 if (kind != CTF_K_STRUCT && kind != CTF_K_UNION) {
1778 xyerror(D_OFFSETOF_TYPE,
1779 "offsetof operand must be a struct or union type\n");
1780 }
1781
1782 if (ctf_member_info(dtt.dtt_ctfp, type, name, &ctm) == CTF_ERR) {
1783 xyerror(D_UNKNOWN, "failed to determine offset of %s: %s\n",
1784 name, ctf_errmsg(ctf_errno(dtt.dtt_ctfp)));
1785 }
1786
1787 bzero(&dn, sizeof (dn));
1788 dt_node_type_assign(&dn, dtt.dtt_ctfp, ctm.ctm_type);
1789
1790 if (dn.dn_flags & DT_NF_BITFIELD) {
1791 xyerror(D_OFFSETOF_BITFIELD,
1792 "cannot take offset of a bit-field: %s\n", name);
1793 }
1794
1795 return (dt_node_int(ctm.ctm_offset / NBBY));
1796 }
1797
1798 dt_node_t *
1799 dt_node_op1(int op, dt_node_t *cp)
1800 {
1801 dt_node_t *dnp;
1802
1803 if (cp->dn_kind == DT_NODE_INT) {
1804 switch (op) {
1805 case DT_TOK_INEG:
1806 /*
1807 * If we're negating an unsigned integer, zero out any
1808 * extra top bits to truncate the value to the size of
1824 return (cp);
1825 }
1826 }
1827
1828 /*
1829 * If sizeof is applied to a type_name or string constant, we can
1830 * transform 'cp' into an integer constant in the node construction
1831 * pass so that it can then be used for arithmetic in this pass.
1832 */
1833 if (op == DT_TOK_SIZEOF &&
1834 (cp->dn_kind == DT_NODE_STRING || cp->dn_kind == DT_NODE_TYPE)) {
1835 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
1836 size_t size = dt_node_type_size(cp);
1837
1838 if (size == 0) {
1839 xyerror(D_SIZEOF_TYPE, "cannot apply sizeof to an "
1840 "operand of unknown size\n");
1841 }
1842
1843 dt_node_type_assign(cp, dtp->dt_ddefs->dm_ctfp,
1844 ctf_lookup_by_name(dtp->dt_ddefs->dm_ctfp, "size_t"));
1845
1846 cp->dn_kind = DT_NODE_INT;
1847 cp->dn_op = DT_TOK_INT;
1848 cp->dn_value = size;
1849
1850 return (cp);
1851 }
1852
1853 dnp = dt_node_alloc(DT_NODE_OP1);
1854 assert(op <= USHRT_MAX);
1855 dnp->dn_op = (ushort_t)op;
1856 dnp->dn_child = cp;
1857
1858 return (dnp);
1859 }
1860
1861 /*
1862 * If an integer constant is being cast to another integer type, we can
1863 * perform the cast as part of integer constant folding in this pass. We must
1864 * take action when the integer is being cast to a smaller type or if it is
1902 */
1903 if (rp->dn_kind == DT_NODE_INT && rp->dn_value == 0 &&
1904 (op == DT_TOK_MOD || op == DT_TOK_DIV ||
1905 op == DT_TOK_MOD_EQ || op == DT_TOK_DIV_EQ))
1906 xyerror(D_DIV_ZERO, "expression contains division by zero\n");
1907
1908 /*
1909 * If both children are immediate values, we can just perform inline
1910 * calculation and return a new immediate node with the result.
1911 */
1912 if (lp->dn_kind == DT_NODE_INT && rp->dn_kind == DT_NODE_INT) {
1913 uintmax_t l = lp->dn_value;
1914 uintmax_t r = rp->dn_value;
1915
1916 dnp = dt_node_int(0); /* allocate new integer node for result */
1917
1918 switch (op) {
1919 case DT_TOK_LOR:
1920 dnp->dn_value = l || r;
1921 dt_node_type_assign(dnp,
1922 DT_INT_CTFP(dtp), DT_INT_TYPE(dtp));
1923 break;
1924 case DT_TOK_LXOR:
1925 dnp->dn_value = (l != 0) ^ (r != 0);
1926 dt_node_type_assign(dnp,
1927 DT_INT_CTFP(dtp), DT_INT_TYPE(dtp));
1928 break;
1929 case DT_TOK_LAND:
1930 dnp->dn_value = l && r;
1931 dt_node_type_assign(dnp,
1932 DT_INT_CTFP(dtp), DT_INT_TYPE(dtp));
1933 break;
1934 case DT_TOK_BOR:
1935 dnp->dn_value = l | r;
1936 dt_node_promote(lp, rp, dnp);
1937 break;
1938 case DT_TOK_XOR:
1939 dnp->dn_value = l ^ r;
1940 dt_node_promote(lp, rp, dnp);
1941 break;
1942 case DT_TOK_BAND:
1943 dnp->dn_value = l & r;
1944 dt_node_promote(lp, rp, dnp);
1945 break;
1946 case DT_TOK_EQU:
1947 dnp->dn_value = l == r;
1948 dt_node_type_assign(dnp,
1949 DT_INT_CTFP(dtp), DT_INT_TYPE(dtp));
1950 break;
1951 case DT_TOK_NEQ:
1952 dnp->dn_value = l != r;
1953 dt_node_type_assign(dnp,
1954 DT_INT_CTFP(dtp), DT_INT_TYPE(dtp));
1955 break;
1956 case DT_TOK_LT:
1957 dt_node_promote(lp, rp, dnp);
1958 if (dnp->dn_flags & DT_NF_SIGNED)
1959 dnp->dn_value = (intmax_t)l < (intmax_t)r;
1960 else
1961 dnp->dn_value = l < r;
1962 dt_node_type_assign(dnp,
1963 DT_INT_CTFP(dtp), DT_INT_TYPE(dtp));
1964 break;
1965 case DT_TOK_LE:
1966 dt_node_promote(lp, rp, dnp);
1967 if (dnp->dn_flags & DT_NF_SIGNED)
1968 dnp->dn_value = (intmax_t)l <= (intmax_t)r;
1969 else
1970 dnp->dn_value = l <= r;
1971 dt_node_type_assign(dnp,
1972 DT_INT_CTFP(dtp), DT_INT_TYPE(dtp));
1973 break;
1974 case DT_TOK_GT:
1975 dt_node_promote(lp, rp, dnp);
1976 if (dnp->dn_flags & DT_NF_SIGNED)
1977 dnp->dn_value = (intmax_t)l > (intmax_t)r;
1978 else
1979 dnp->dn_value = l > r;
1980 dt_node_type_assign(dnp,
1981 DT_INT_CTFP(dtp), DT_INT_TYPE(dtp));
1982 break;
1983 case DT_TOK_GE:
1984 dt_node_promote(lp, rp, dnp);
1985 if (dnp->dn_flags & DT_NF_SIGNED)
1986 dnp->dn_value = (intmax_t)l >= (intmax_t)r;
1987 else
1988 dnp->dn_value = l >= r;
1989 dt_node_type_assign(dnp,
1990 DT_INT_CTFP(dtp), DT_INT_TYPE(dtp));
1991 break;
1992 case DT_TOK_LSH:
1993 dnp->dn_value = l << r;
1994 dt_node_type_propagate(lp, dnp);
1995 dt_node_attr_assign(rp,
1996 dt_attr_min(lp->dn_attr, rp->dn_attr));
1997 break;
1998 case DT_TOK_RSH:
1999 dnp->dn_value = l >> r;
2000 dt_node_type_propagate(lp, dnp);
2001 dt_node_attr_assign(rp,
2002 dt_attr_min(lp->dn_attr, rp->dn_attr));
2003 break;
2004 case DT_TOK_ADD:
2005 dnp->dn_value = l + r;
2006 dt_node_promote(lp, rp, dnp);
2007 break;
2008 case DT_TOK_SUB:
2009 dnp->dn_value = l - r;
2010 dt_node_promote(lp, rp, dnp);
2211 if (ddp->dd_node == NULL) {
2212 xyerror(D_DECL_ARRNULL, "inline declaration requires "
2213 "array tuple signature: %s\n", dsp->ds_ident);
2214 }
2215
2216 if (ddp->dd_node->dn_kind != DT_NODE_TYPE) {
2217 xyerror(D_DECL_ARRNULL, "inline declaration cannot be "
2218 "of scalar array type: %s\n", dsp->ds_ident);
2219 }
2220
2221 if (dt_decl_type(ddp->dd_next, &dtt) != 0)
2222 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
2223 }
2224
2225 /*
2226 * If the inline identifier is not defined, then create it with the
2227 * orphan flag set. We do not insert the identifier into dt_globals
2228 * until we have successfully cooked the right-hand expression, below.
2229 */
2230 dnp = dt_node_alloc(DT_NODE_INLINE);
2231 dt_node_type_assign(dnp, dtt.dtt_ctfp, dtt.dtt_type);
2232 dt_node_attr_assign(dnp, _dtrace_defattr);
2233
2234 if (dt_node_is_void(dnp)) {
2235 xyerror(D_DECL_VOIDOBJ,
2236 "cannot declare void inline: %s\n", dsp->ds_ident);
2237 }
2238
2239 if (ctf_type_kind(dnp->dn_ctfp, ctf_type_resolve(
2240 dnp->dn_ctfp, dnp->dn_type)) == CTF_K_FORWARD) {
2241 xyerror(D_DECL_INCOMPLETE,
2242 "incomplete struct/union/enum %s: %s\n",
2243 dt_node_type_name(dnp, n, sizeof (n)), dsp->ds_ident);
2244 }
2245
2246 if ((inp = malloc(sizeof (dt_idnode_t))) == NULL)
2247 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2248
2249 bzero(inp, sizeof (dt_idnode_t));
2250
2251 idp = dnp->dn_ident = dt_ident_create(dsp->ds_ident,
2366 dt_node_t *
2367 dt_node_member(dt_decl_t *ddp, char *name, dt_node_t *expr)
2368 {
2369 dtrace_typeinfo_t dtt;
2370 dt_node_t *dnp;
2371 int err;
2372
2373 if (ddp != NULL) {
2374 err = dt_decl_type(ddp, &dtt);
2375 dt_decl_free(ddp);
2376
2377 if (err != 0)
2378 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
2379 }
2380
2381 dnp = dt_node_alloc(DT_NODE_MEMBER);
2382 dnp->dn_membname = name;
2383 dnp->dn_membexpr = expr;
2384
2385 if (ddp != NULL)
2386 dt_node_type_assign(dnp, dtt.dtt_ctfp, dtt.dtt_type);
2387
2388 return (dnp);
2389 }
2390
2391 dt_node_t *
2392 dt_node_xlator(dt_decl_t *ddp, dt_decl_t *sdp, char *name, dt_node_t *members)
2393 {
2394 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
2395 dtrace_typeinfo_t src, dst;
2396 dt_node_t sn, dn;
2397 dt_xlator_t *dxp;
2398 dt_node_t *dnp;
2399 int edst, esrc;
2400 uint_t kind;
2401
2402 char n1[DT_TYPE_NAMELEN];
2403 char n2[DT_TYPE_NAMELEN];
2404
2405 edst = dt_decl_type(ddp, &dst);
2406 dt_decl_free(ddp);
2407
2408 esrc = dt_decl_type(sdp, &src);
2409 dt_decl_free(sdp);
2410
2411 if (edst != 0 || esrc != 0) {
2412 free(name);
2413 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
2414 }
2415
2416 bzero(&sn, sizeof (sn));
2417 dt_node_type_assign(&sn, src.dtt_ctfp, src.dtt_type);
2418
2419 bzero(&dn, sizeof (dn));
2420 dt_node_type_assign(&dn, dst.dtt_ctfp, dst.dtt_type);
2421
2422 if (dt_xlator_lookup(dtp, &sn, &dn, DT_XLATE_EXACT) != NULL) {
2423 xyerror(D_XLATE_REDECL,
2424 "translator from %s to %s has already been declared\n",
2425 dt_node_type_name(&sn, n1, sizeof (n1)),
2426 dt_node_type_name(&dn, n2, sizeof (n2)));
2427 }
2428
2429 kind = ctf_type_kind(dst.dtt_ctfp,
2430 ctf_type_resolve(dst.dtt_ctfp, dst.dtt_type));
2431
2432 if (kind == CTF_K_FORWARD) {
2433 xyerror(D_XLATE_SOU, "incomplete struct/union/enum %s\n",
2434 dt_type_name(dst.dtt_ctfp, dst.dtt_type, n1, sizeof (n1)));
2435 }
2436
2437 if (kind != CTF_K_STRUCT && kind != CTF_K_UNION) {
2438 xyerror(D_XLATE_SOU,
2439 "translator output type must be a struct or union\n");
2440 }
2646 * matches its type if this is a global lookup. In the TLS or
2647 * local case, we don't know how the ident will be used until
2648 * the time operator -> is seen; more parsing is needed.
2649 */
2650 if (idp->di_kind != idkind && dhp == dtp->dt_globals) {
2651 xyerror(D_IDENT_BADREF, "%s '%s' may not be referenced "
2652 "as %s\n", dt_idkind_name(idp->di_kind),
2653 idp->di_name, dt_idkind_name(idkind));
2654 }
2655
2656 /*
2657 * Arrays and aggregations are not cooked individually. They
2658 * have dynamic types and must be referenced using operator [].
2659 * This is handled explicitly by the code for DT_TOK_LBRAC.
2660 */
2661 if (idp->di_kind != DT_IDENT_ARRAY &&
2662 idp->di_kind != DT_IDENT_AGG)
2663 attr = dt_ident_cook(dnp, idp, NULL);
2664 else {
2665 dt_node_type_assign(dnp,
2666 DT_DYN_CTFP(dtp), DT_DYN_TYPE(dtp));
2667 attr = idp->di_attr;
2668 }
2669
2670 free(dnp->dn_string);
2671 dnp->dn_string = NULL;
2672 dnp->dn_kind = dnkind;
2673 dnp->dn_ident = idp;
2674 dnp->dn_flags |= DT_NF_LVALUE;
2675
2676 if (idp->di_flags & DT_IDFLG_WRITE)
2677 dnp->dn_flags |= DT_NF_WRITABLE;
2678
2679 dt_node_attr_assign(dnp, attr);
2680
2681 } else if (dhp == dtp->dt_globals && scope != DTRACE_OBJ_EXEC &&
2682 dtrace_lookup_by_name(dtp, scope, name, &sym, &dts) == 0) {
2683
2684 dt_module_t *mp = dt_module_lookup_by_name(dtp, dts.dts_object);
2685 int umod = (mp->dm_flags & DT_DM_KERNEL) == 0;
2686 static const char *const kunames[] = { "kernel", "user" };
2722 if (mp->dm_flags & DT_DM_PRIMARY)
2723 idp->di_flags |= DT_IDFLG_PRIM;
2724
2725 idp->di_next = dtp->dt_externs;
2726 dtp->dt_externs = idp;
2727
2728 if ((sip = malloc(sizeof (dtrace_syminfo_t))) == NULL)
2729 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2730
2731 bcopy(&dts, sip, sizeof (dtrace_syminfo_t));
2732 idp->di_data = sip;
2733 idp->di_ctfp = dtt.dtt_ctfp;
2734 idp->di_type = dtt.dtt_type;
2735
2736 free(dnp->dn_string);
2737 dnp->dn_string = NULL;
2738 dnp->dn_kind = DT_NODE_SYM;
2739 dnp->dn_ident = idp;
2740 dnp->dn_flags |= DT_NF_LVALUE;
2741
2742 dt_node_type_assign(dnp, dtt.dtt_ctfp, dtt.dtt_type);
2743 dt_node_attr_assign(dnp, _dtrace_symattr);
2744
2745 if (uref) {
2746 idp->di_flags |= DT_IDFLG_USER;
2747 dnp->dn_flags |= DT_NF_USERLAND;
2748 }
2749
2750 } else if (scope == DTRACE_OBJ_EXEC && create == B_TRUE) {
2751 uint_t flags = DT_IDFLG_WRITE;
2752 uint_t id;
2753
2754 if (dt_idhash_nextid(dhp, &id) == -1) {
2755 xyerror(D_ID_OFLOW, "cannot create %s: limit on number "
2756 "of %s variables exceeded\n", name, sname);
2757 }
2758
2759 if (dhp == yypcb->pcb_locals)
2760 flags |= DT_IDFLG_LOCAL;
2761 else if (dhp == dtp->dt_tls)
2762 flags |= DT_IDFLG_TLS;
2770 &dt_idops_assc, NULL, dtp->dt_gen);
2771 } else {
2772 idp = dt_idhash_insert(dhp, name,
2773 idkind, flags, id, _dtrace_defattr, 0,
2774 &dt_idops_thaw, NULL, dtp->dt_gen);
2775 }
2776
2777 if (idp == NULL)
2778 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2779
2780 /*
2781 * Arrays and aggregations are not cooked individually. They
2782 * have dynamic types and must be referenced using operator [].
2783 * This is handled explicitly by the code for DT_TOK_LBRAC.
2784 */
2785 if (idp->di_kind != DT_IDENT_ARRAY &&
2786 idp->di_kind != DT_IDENT_AGG)
2787 attr = dt_ident_cook(dnp, idp, NULL);
2788 else {
2789 dt_node_type_assign(dnp,
2790 DT_DYN_CTFP(dtp), DT_DYN_TYPE(dtp));
2791 attr = idp->di_attr;
2792 }
2793
2794 free(dnp->dn_string);
2795 dnp->dn_string = NULL;
2796 dnp->dn_kind = dnkind;
2797 dnp->dn_ident = idp;
2798 dnp->dn_flags |= DT_NF_LVALUE | DT_NF_WRITABLE;
2799
2800 dt_node_attr_assign(dnp, attr);
2801
2802 } else if (scope != DTRACE_OBJ_EXEC) {
2803 xyerror(D_IDENT_UNDEF, "failed to resolve %s%s%s: %s\n",
2804 dnp->dn_string, mark, name,
2805 dtrace_errmsg(dtp, dtrace_errno(dtp)));
2806 } else {
2807 xyerror(D_IDENT_UNDEF, "failed to resolve %s: %s\n",
2808 dnp->dn_string, dtrace_errmsg(dtp, dtrace_errno(dtp)));
2809 }
2810 }
2873 if (dnp->dn_op == DT_TOK_PREINC || dnp->dn_op == DT_TOK_POSTINC ||
2874 dnp->dn_op == DT_TOK_PREDEC || dnp->dn_op == DT_TOK_POSTDEC)
2875 idflags = DT_IDFLG_REF | DT_IDFLG_MOD;
2876 else
2877 idflags = DT_IDFLG_REF;
2878
2879 /*
2880 * We allow the unary ++ and -- operators to instantiate new scalar
2881 * variables if applied to an identifier; otherwise just cook as usual.
2882 */
2883 if (cp->dn_kind == DT_NODE_IDENT && (idflags & DT_IDFLG_MOD))
2884 dt_xcook_ident(cp, dtp->dt_globals, DT_IDENT_SCALAR, B_TRUE);
2885
2886 cp = dnp->dn_child = dt_node_cook(cp, 0); /* don't set idflags yet */
2887
2888 if (cp->dn_kind == DT_NODE_VAR && dt_ident_unref(cp->dn_ident)) {
2889 if (dt_type_lookup("int64_t", &dtt) != 0)
2890 xyerror(D_TYPE_ERR, "failed to lookup int64_t\n");
2891
2892 dt_ident_type_assign(cp->dn_ident, dtt.dtt_ctfp, dtt.dtt_type);
2893 dt_node_type_assign(cp, dtt.dtt_ctfp, dtt.dtt_type);
2894 }
2895
2896 if (cp->dn_kind == DT_NODE_VAR)
2897 cp->dn_ident->di_flags |= idflags;
2898
2899 switch (dnp->dn_op) {
2900 case DT_TOK_DEREF:
2901 /*
2902 * If the deref operator is applied to a translated pointer,
2903 * we set our output type to the output of the translation.
2904 */
2905 if ((idp = dt_node_resolve(cp, DT_IDENT_XLPTR)) != NULL) {
2906 dt_xlator_t *dxp = idp->di_data;
2907
2908 dnp->dn_ident = &dxp->dx_souid;
2909 dt_node_type_assign(dnp,
2910 dnp->dn_ident->di_ctfp, dnp->dn_ident->di_type);
2911 break;
2912 }
2913
2914 type = ctf_type_resolve(cp->dn_ctfp, cp->dn_type);
2915 kind = ctf_type_kind(cp->dn_ctfp, type);
2916
2917 if (kind == CTF_K_ARRAY) {
2918 if (ctf_array_info(cp->dn_ctfp, type, &r) != 0) {
2919 dtp->dt_ctferr = ctf_errno(cp->dn_ctfp);
2920 longjmp(yypcb->pcb_jmpbuf, EDT_CTF);
2921 } else
2922 type = r.ctr_contents;
2923 } else if (kind == CTF_K_POINTER) {
2924 type = ctf_type_reference(cp->dn_ctfp, type);
2925 } else {
2926 xyerror(D_DEREF_NONPTR,
2927 "cannot dereference non-pointer type\n");
2928 }
2929
2930 dt_node_type_assign(dnp, cp->dn_ctfp, type);
2931 base = ctf_type_resolve(cp->dn_ctfp, type);
2932 kind = ctf_type_kind(cp->dn_ctfp, base);
2933
2934 if (kind == CTF_K_INTEGER && ctf_type_encoding(cp->dn_ctfp,
2935 base, &e) == 0 && IS_VOID(e)) {
2936 xyerror(D_DEREF_VOID,
2937 "cannot dereference pointer to void\n");
2938 }
2939
2940 if (kind == CTF_K_FUNCTION) {
2941 xyerror(D_DEREF_FUNC,
2942 "cannot dereference pointer to function\n");
2943 }
2944
2945 if (kind != CTF_K_ARRAY || dt_node_is_string(dnp))
2946 dnp->dn_flags |= DT_NF_LVALUE; /* see K&R[A7.4.3] */
2947
2948 /*
2949 * If we propagated the l-value bit and the child operand was
2950 * a writable D variable or a binary operation of the form
2967 if (!dt_node_is_arith(cp)) {
2968 xyerror(D_OP_ARITH, "operator %s requires an operand "
2969 "of arithmetic type\n", opstr(dnp->dn_op));
2970 }
2971 dt_node_type_propagate(cp, dnp); /* see K&R[A7.4.4-6] */
2972 break;
2973
2974 case DT_TOK_BNEG:
2975 if (!dt_node_is_integer(cp)) {
2976 xyerror(D_OP_INT, "operator %s requires an operand of "
2977 "integral type\n", opstr(dnp->dn_op));
2978 }
2979 dt_node_type_propagate(cp, dnp); /* see K&R[A7.4.4-6] */
2980 break;
2981
2982 case DT_TOK_LNEG:
2983 if (!dt_node_is_scalar(cp)) {
2984 xyerror(D_OP_SCALAR, "operator %s requires an operand "
2985 "of scalar type\n", opstr(dnp->dn_op));
2986 }
2987 dt_node_type_assign(dnp, DT_INT_CTFP(dtp), DT_INT_TYPE(dtp));
2988 break;
2989
2990 case DT_TOK_ADDROF:
2991 if (cp->dn_kind == DT_NODE_VAR || cp->dn_kind == DT_NODE_AGG) {
2992 xyerror(D_ADDROF_VAR,
2993 "cannot take address of dynamic variable\n");
2994 }
2995
2996 if (dt_node_is_dynamic(cp)) {
2997 xyerror(D_ADDROF_VAR,
2998 "cannot take address of dynamic object\n");
2999 }
3000
3001 if (!(cp->dn_flags & DT_NF_LVALUE)) {
3002 xyerror(D_ADDROF_LVAL, /* see K&R[A7.4.2] */
3003 "unacceptable operand for unary & operator\n");
3004 }
3005
3006 if (cp->dn_flags & DT_NF_BITFIELD) {
3007 xyerror(D_ADDROF_BITFIELD,
3008 "cannot take address of bit-field\n");
3009 }
3010
3011 dtt.dtt_object = NULL;
3012 dtt.dtt_ctfp = cp->dn_ctfp;
3013 dtt.dtt_type = cp->dn_type;
3014
3015 if (dt_type_pointer(&dtt) == -1) {
3016 xyerror(D_TYPE_ERR, "cannot find type for \"&\": %s*\n",
3017 dt_node_type_name(cp, n, sizeof (n)));
3018 }
3019
3020 dt_node_type_assign(dnp, dtt.dtt_ctfp, dtt.dtt_type);
3021
3022 if (cp->dn_flags & DT_NF_USERLAND)
3023 dnp->dn_flags |= DT_NF_USERLAND;
3024 break;
3025
3026 case DT_TOK_SIZEOF:
3027 if (cp->dn_flags & DT_NF_BITFIELD) {
3028 xyerror(D_SIZEOF_BITFIELD,
3029 "cannot apply sizeof to a bit-field\n");
3030 }
3031
3032 if (dt_node_sizeof(cp) == 0) {
3033 xyerror(D_SIZEOF_TYPE, "cannot apply sizeof to an "
3034 "operand of unknown size\n");
3035 }
3036
3037 dt_node_type_assign(dnp, dtp->dt_ddefs->dm_ctfp,
3038 ctf_lookup_by_name(dtp->dt_ddefs->dm_ctfp, "size_t"));
3039 break;
3040
3041 case DT_TOK_STRINGOF:
3042 if (!dt_node_is_scalar(cp) && !dt_node_is_pointer(cp) &&
3043 !dt_node_is_strcompat(cp)) {
3044 xyerror(D_STRINGOF_TYPE,
3045 "cannot apply stringof to a value of type %s\n",
3046 dt_node_type_name(cp, n, sizeof (n)));
3047 }
3048 dt_node_type_assign(dnp, DT_STR_CTFP(dtp), DT_STR_TYPE(dtp));
3049 break;
3050
3051 case DT_TOK_PREINC:
3052 case DT_TOK_POSTINC:
3053 case DT_TOK_PREDEC:
3054 case DT_TOK_POSTDEC:
3055 if (dt_node_is_scalar(cp) == 0) {
3056 xyerror(D_OP_SCALAR, "operator %s requires operand of "
3057 "scalar type\n", opstr(dnp->dn_op));
3058 }
3059
3060 if (dt_node_is_vfptr(cp)) {
3061 xyerror(D_OP_VFPTR, "operator %s requires an operand "
3062 "of known size\n", opstr(dnp->dn_op));
3063 }
3064
3065 if (!(cp->dn_flags & DT_NF_LVALUE)) {
3066 xyerror(D_OP_LVAL, "operator %s requires modifiable "
3067 "lvalue as an operand\n", opstr(dnp->dn_op));
3068 }
3221
3222 if (!dt_node_is_arith(lp) || !dt_node_is_arith(rp)) {
3223 xyerror(D_OP_ARITH, "operator %s requires operands of "
3224 "arithmetic type\n", opstr(op));
3225 }
3226
3227 dt_node_promote(lp, rp, dnp); /* see K&R[A7.6] */
3228 break;
3229
3230 case DT_TOK_LAND:
3231 case DT_TOK_LXOR:
3232 case DT_TOK_LOR:
3233 lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF);
3234 rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
3235
3236 if (!dt_node_is_scalar(lp) || !dt_node_is_scalar(rp)) {
3237 xyerror(D_OP_SCALAR, "operator %s requires operands "
3238 "of scalar type\n", opstr(op));
3239 }
3240
3241 dt_node_type_assign(dnp, DT_INT_CTFP(dtp), DT_INT_TYPE(dtp));
3242 dt_node_attr_assign(dnp, dt_attr_min(lp->dn_attr, rp->dn_attr));
3243 break;
3244
3245 case DT_TOK_LT:
3246 case DT_TOK_LE:
3247 case DT_TOK_GT:
3248 case DT_TOK_GE:
3249 case DT_TOK_EQU:
3250 case DT_TOK_NEQ:
3251 /*
3252 * The D comparison operators provide the ability to transform
3253 * a right-hand identifier into a corresponding enum tag value
3254 * if the left-hand side is an enum type. To do this, we cook
3255 * the left-hand side, and then see if the right-hand side is
3256 * an unscoped identifier defined in the enum. If so, we
3257 * convert into an integer constant node with the tag's value.
3258 */
3259 lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF);
3260
3261 kind = ctf_type_kind(lp->dn_ctfp,
3265 strchr(rp->dn_string, '`') == NULL && ctf_enum_value(
3266 lp->dn_ctfp, lp->dn_type, rp->dn_string, &val) == 0) {
3267
3268 if ((idp = dt_idstack_lookup(&yypcb->pcb_globals,
3269 rp->dn_string)) != NULL) {
3270 xyerror(D_IDENT_AMBIG,
3271 "ambiguous use of operator %s: %s is "
3272 "both a %s enum tag and a global %s\n",
3273 opstr(op), rp->dn_string,
3274 dt_node_type_name(lp, n1, sizeof (n1)),
3275 dt_idkind_name(idp->di_kind));
3276 }
3277
3278 free(rp->dn_string);
3279 rp->dn_string = NULL;
3280 rp->dn_kind = DT_NODE_INT;
3281 rp->dn_flags |= DT_NF_COOKED;
3282 rp->dn_op = DT_TOK_INT;
3283 rp->dn_value = (intmax_t)val;
3284
3285 dt_node_type_assign(rp, lp->dn_ctfp, lp->dn_type);
3286 dt_node_attr_assign(rp, _dtrace_symattr);
3287 }
3288
3289 rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
3290
3291 /*
3292 * The rules for type checking for the relational operators are
3293 * described in the ANSI-C spec (see K&R[A7.9-10]). We perform
3294 * the various tests in order from least to most expensive. We
3295 * also allow derived strings to be compared as a first-class
3296 * type (resulting in a strcmp(3C)-style comparison), and we
3297 * slightly relax the A7.9 rules to permit void pointer
3298 * comparisons as in A7.10. Our users won't be confused by
3299 * this since they understand pointers are just numbers, and
3300 * relaxing this constraint simplifies the implementation.
3301 */
3302 if (ctf_type_compat(lp->dn_ctfp, lp->dn_type,
3303 rp->dn_ctfp, rp->dn_type))
3304 /*EMPTY*/;
3305 else if (dt_node_is_integer(lp) && dt_node_is_integer(rp))
3306 /*EMPTY*/;
3307 else if (dt_node_is_strcompat(lp) && dt_node_is_strcompat(rp) &&
3308 (dt_node_is_string(lp) || dt_node_is_string(rp)))
3309 /*EMPTY*/;
3310 else if (dt_node_is_ptrcompat(lp, rp, NULL, NULL) == 0) {
3311 xyerror(D_OP_INCOMPAT, "operands have "
3312 "incompatible types: \"%s\" %s \"%s\"\n",
3313 dt_node_type_name(lp, n1, sizeof (n1)), opstr(op),
3314 dt_node_type_name(rp, n2, sizeof (n2)));
3315 }
3316
3317 dt_node_type_assign(dnp, DT_INT_CTFP(dtp), DT_INT_TYPE(dtp));
3318 dt_node_attr_assign(dnp, dt_attr_min(lp->dn_attr, rp->dn_attr));
3319 break;
3320
3321 case DT_TOK_ADD:
3322 case DT_TOK_SUB: {
3323 /*
3324 * The rules for type checking for the additive operators are
3325 * described in the ANSI-C spec (see K&R[A7.7]). Pointers and
3326 * integers may be manipulated according to specific rules. In
3327 * these cases D permits strings to be treated as pointers.
3328 */
3329 int lp_is_ptr, lp_is_int, rp_is_ptr, rp_is_int;
3330
3331 lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF);
3332 rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
3333
3334 lp_is_ptr = dt_node_is_string(lp) ||
3335 (dt_node_is_pointer(lp) && !dt_node_is_vfptr(lp));
3336 lp_is_int = dt_node_is_integer(lp);
3337
3345 } else if (lp_is_ptr && rp_is_int) {
3346 ctfp = lp->dn_ctfp;
3347 type = lp->dn_type;
3348 uref = lp->dn_flags & DT_NF_USERLAND;
3349 } else if (lp_is_int && rp_is_ptr && op == DT_TOK_ADD) {
3350 ctfp = rp->dn_ctfp;
3351 type = rp->dn_type;
3352 uref = rp->dn_flags & DT_NF_USERLAND;
3353 } else if (lp_is_ptr && rp_is_ptr && op == DT_TOK_SUB &&
3354 dt_node_is_ptrcompat(lp, rp, NULL, NULL)) {
3355 ctfp = dtp->dt_ddefs->dm_ctfp;
3356 type = ctf_lookup_by_name(ctfp, "ptrdiff_t");
3357 uref = 0;
3358 } else {
3359 xyerror(D_OP_INCOMPAT, "operands have incompatible "
3360 "types: \"%s\" %s \"%s\"\n",
3361 dt_node_type_name(lp, n1, sizeof (n1)), opstr(op),
3362 dt_node_type_name(rp, n2, sizeof (n2)));
3363 }
3364
3365 dt_node_type_assign(dnp, ctfp, type);
3366 dt_node_attr_assign(dnp, dt_attr_min(lp->dn_attr, rp->dn_attr));
3367
3368 if (uref)
3369 dnp->dn_flags |= DT_NF_USERLAND;
3370 break;
3371 }
3372
3373 case DT_TOK_OR_EQ:
3374 case DT_TOK_XOR_EQ:
3375 case DT_TOK_AND_EQ:
3376 case DT_TOK_LSH_EQ:
3377 case DT_TOK_RSH_EQ:
3378 case DT_TOK_MOD_EQ:
3379 if (lp->dn_kind == DT_NODE_IDENT) {
3380 dt_xcook_ident(lp, dtp->dt_globals,
3381 DT_IDENT_SCALAR, B_TRUE);
3382 }
3383
3384 lp = dnp->dn_left =
3385 dt_node_cook(lp, DT_IDFLG_REF | DT_IDFLG_MOD);
3486 * If the right-hand side is a dynamic variable that is the
3487 * output of a translator, our result is the translated type.
3488 */
3489 if ((idp = dt_node_resolve(rp, DT_IDENT_XLSOU)) != NULL) {
3490 ctfp = idp->di_ctfp;
3491 type = idp->di_type;
3492 uref = idp->di_flags & DT_IDFLG_USER;
3493 } else {
3494 ctfp = rp->dn_ctfp;
3495 type = rp->dn_type;
3496 uref = rp->dn_flags & DT_NF_USERLAND;
3497 }
3498
3499 /*
3500 * If the left-hand side of an assignment statement is a virgin
3501 * variable created by this compilation pass, reset the type of
3502 * this variable to the type of the right-hand side.
3503 */
3504 if (lp->dn_kind == DT_NODE_VAR &&
3505 dt_ident_unref(lp->dn_ident)) {
3506 dt_node_type_assign(lp, ctfp, type);
3507 dt_ident_type_assign(lp->dn_ident, ctfp, type);
3508
3509 if (uref) {
3510 lp->dn_flags |= DT_NF_USERLAND;
3511 lp->dn_ident->di_flags |= DT_IDFLG_USER;
3512 }
3513 }
3514
3515 if (lp->dn_kind == DT_NODE_VAR)
3516 lp->dn_ident->di_flags |= DT_IDFLG_MOD;
3517
3518 /*
3519 * The rules for type checking for the assignment operators are
3520 * described in the ANSI-C spec (see K&R[A7.17]). We share
3521 * most of this code with the argument list checking code.
3522 */
3523 if (!dt_node_is_string(lp)) {
3524 kind = ctf_type_kind(lp->dn_ctfp,
3525 ctf_type_resolve(lp->dn_ctfp, lp->dn_type));
3526
3700 "applied to pointer to type \"%s\"; must "
3701 "be applied to a struct or union pointer\n",
3702 ctf_type_name(ctfp, type, n1, sizeof (n1)));
3703 } else {
3704 xyerror(D_OP_SOU, "operator %s cannot be "
3705 "applied to type \"%s\"; must be applied "
3706 "to a struct or union\n", opstr(op),
3707 ctf_type_name(ctfp, type, n1, sizeof (n1)));
3708 }
3709 }
3710
3711 if (ctf_member_info(ctfp, type, rp->dn_string, &m) == CTF_ERR) {
3712 xyerror(D_TYPE_MEMBER,
3713 "%s is not a member of %s\n", rp->dn_string,
3714 ctf_type_name(ctfp, type, n1, sizeof (n1)));
3715 }
3716
3717 type = ctf_type_resolve(ctfp, m.ctm_type);
3718 kind = ctf_type_kind(ctfp, type);
3719
3720 dt_node_type_assign(dnp, ctfp, m.ctm_type);
3721 dt_node_attr_assign(dnp, lp->dn_attr);
3722
3723 if (op == DT_TOK_PTR && (kind != CTF_K_ARRAY ||
3724 dt_node_is_string(dnp)))
3725 dnp->dn_flags |= DT_NF_LVALUE; /* see K&R[A7.3.3] */
3726
3727 if (op == DT_TOK_DOT && (lp->dn_flags & DT_NF_LVALUE) &&
3728 (kind != CTF_K_ARRAY || dt_node_is_string(dnp)))
3729 dnp->dn_flags |= DT_NF_LVALUE; /* see K&R[A7.3.3] */
3730
3731 if (lp->dn_flags & DT_NF_WRITABLE)
3732 dnp->dn_flags |= DT_NF_WRITABLE;
3733
3734 if (uref && (kind == CTF_K_POINTER ||
3735 (dnp->dn_flags & DT_NF_REF)))
3736 dnp->dn_flags |= DT_NF_USERLAND;
3737 break;
3738
3739 case DT_TOK_LBRAC: {
3740 /*
3826
3827 dt_node_free(lp);
3828 return (dt_node_cook(dnp, idflags));
3829 }
3830
3831 case DT_TOK_XLATE: {
3832 dt_xlator_t *dxp;
3833
3834 assert(lp->dn_kind == DT_NODE_TYPE);
3835 rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
3836 dxp = dt_xlator_lookup(dtp, rp, lp, DT_XLATE_FUZZY);
3837
3838 if (dxp == NULL) {
3839 xyerror(D_XLATE_NONE,
3840 "cannot translate from \"%s\" to \"%s\"\n",
3841 dt_node_type_name(rp, n1, sizeof (n1)),
3842 dt_node_type_name(lp, n2, sizeof (n2)));
3843 }
3844
3845 dnp->dn_ident = dt_xlator_ident(dxp, lp->dn_ctfp, lp->dn_type);
3846 dt_node_type_assign(dnp, DT_DYN_CTFP(dtp), DT_DYN_TYPE(dtp));
3847 dt_node_attr_assign(dnp,
3848 dt_attr_min(rp->dn_attr, dnp->dn_ident->di_attr));
3849 break;
3850 }
3851
3852 case DT_TOK_LPAR: {
3853 ctf_id_t ltype, rtype;
3854 uint_t lkind, rkind;
3855
3856 assert(lp->dn_kind == DT_NODE_TYPE);
3857 rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
3858
3859 ltype = ctf_type_resolve(lp->dn_ctfp, lp->dn_type);
3860 lkind = ctf_type_kind(lp->dn_ctfp, ltype);
3861
3862 rtype = ctf_type_resolve(rp->dn_ctfp, rp->dn_type);
3863 rkind = ctf_type_kind(rp->dn_ctfp, rtype);
3864
3865 /*
3866 * The rules for casting are loosely explained in K&R[A7.5]
3991 if (ctf_type_compat(lp->dn_ctfp, lp->dn_type,
3992 rp->dn_ctfp, rp->dn_type)) {
3993 ctfp = lp->dn_ctfp;
3994 type = lp->dn_type;
3995 } else if (dt_node_is_integer(lp) && dt_node_is_integer(rp)) {
3996 dt_type_promote(lp, rp, &ctfp, &type);
3997 } else if (dt_node_is_strcompat(lp) && dt_node_is_strcompat(rp) &&
3998 (dt_node_is_string(lp) || dt_node_is_string(rp))) {
3999 ctfp = DT_STR_CTFP(yypcb->pcb_hdl);
4000 type = DT_STR_TYPE(yypcb->pcb_hdl);
4001 } else if (dt_node_is_ptrcompat(lp, rp, &ctfp, &type) == 0) {
4002 xyerror(D_OP_INCOMPAT,
4003 "operator ?: operands must have compatible types\n");
4004 }
4005
4006 if (dt_node_is_actfunc(lp) || dt_node_is_actfunc(rp)) {
4007 xyerror(D_OP_ACT, "action cannot be "
4008 "used in a conditional context\n");
4009 }
4010
4011 dt_node_type_assign(dnp, ctfp, type);
4012 dt_node_attr_assign(dnp, dt_attr_min(dnp->dn_expr->dn_attr,
4013 dt_attr_min(lp->dn_attr, rp->dn_attr)));
4014
4015 return (dnp);
4016 }
4017
4018 static dt_node_t *
4019 dt_cook_statement(dt_node_t *dnp, uint_t idflags)
4020 {
4021 dnp->dn_expr = dt_node_cook(dnp->dn_expr, idflags);
4022 dt_node_attr_assign(dnp, dnp->dn_expr->dn_attr);
4023
4024 return (dnp);
4025 }
4026
4027 /*
4028 * If dn_aggfun is set, this node is a collapsed aggregation assignment (see
4029 * the special case code for DT_TOK_ASGN in dt_cook_op2() above), in which
4030 * case we cook both the tuple and the function call. If dn_aggfun is NULL,
4031 * this node is just a reference to the aggregation's type and attributes.
4032 */
4033 /*ARGSUSED*/
4034 static dt_node_t *
4035 dt_cook_aggregation(dt_node_t *dnp, uint_t idflags)
4036 {
4037 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
4038
4039 if (dnp->dn_aggfun != NULL) {
4040 dnp->dn_aggfun = dt_node_cook(dnp->dn_aggfun, DT_IDFLG_REF);
4041 dt_node_attr_assign(dnp, dt_ident_cook(dnp,
4042 dnp->dn_ident, &dnp->dn_aggtup));
4043 } else {
4044 dt_node_type_assign(dnp, DT_DYN_CTFP(dtp), DT_DYN_TYPE(dtp));
4045 dt_node_attr_assign(dnp, dnp->dn_ident->di_attr);
4046 }
4047
4048 return (dnp);
4049 }
4050
4051 /*
4052 * Since D permits new variable identifiers to be instantiated in any program
4053 * expression, we may need to cook a clause's predicate either before or after
4054 * the action list depending on the program code in question. Consider:
4055 *
4056 * probe-description-list probe-description-list
4057 * /x++/ /x == 0/
4058 * { {
4059 * trace(x); trace(x++);
4060 * } }
4061 *
4062 * In the left-hand example, the predicate uses operator ++ to instantiate 'x'
4063 * as a variable of type int64_t. The predicate must be cooked first because
4064 * otherwise the statement trace(x) refers to an unknown identifier. In the
4226 dtrace_attribute_t attr = _dtrace_maxattr;
4227 ctf_membinfo_t ctm;
4228
4229 /*
4230 * Before cooking each translator member, we push a reference to the
4231 * hash containing translator-local identifiers on to pcb_globals to
4232 * temporarily interpose these identifiers in front of other globals.
4233 */
4234 dt_idstack_push(&yypcb->pcb_globals, dxp->dx_locals);
4235
4236 for (mnp = dnp->dn_members; mnp != NULL; mnp = mnp->dn_list) {
4237 if (ctf_member_info(dxp->dx_dst_ctfp, dxp->dx_dst_type,
4238 mnp->dn_membname, &ctm) == CTF_ERR) {
4239 xyerror(D_XLATE_MEMB,
4240 "translator member %s is not a member of %s\n",
4241 mnp->dn_membname, ctf_type_name(dxp->dx_dst_ctfp,
4242 dxp->dx_dst_type, n1, sizeof (n1)));
4243 }
4244
4245 (void) dt_node_cook(mnp, DT_IDFLG_REF);
4246 dt_node_type_assign(mnp, dxp->dx_dst_ctfp, ctm.ctm_type);
4247 attr = dt_attr_min(attr, mnp->dn_attr);
4248
4249 if (dt_node_is_argcompat(mnp, mnp->dn_membexpr) == 0) {
4250 xyerror(D_XLATE_INCOMPAT,
4251 "translator member %s definition uses "
4252 "incompatible types: \"%s\" = \"%s\"\n",
4253 mnp->dn_membname,
4254 dt_node_type_name(mnp, n1, sizeof (n1)),
4255 dt_node_type_name(mnp->dn_membexpr,
4256 n2, sizeof (n2)));
4257 }
4258 }
4259
4260 dt_idstack_pop(&yypcb->pcb_globals, dxp->dx_locals);
4261
4262 dxp->dx_souid.di_attr = attr;
4263 dxp->dx_ptrid.di_attr = attr;
4264
4265 dt_node_type_assign(dnp, DT_DYN_CTFP(dtp), DT_DYN_TYPE(dtp));
4266 dt_node_attr_assign(dnp, _dtrace_defattr);
4267
4268 return (dnp);
4269 }
4270
4271 static void
4272 dt_node_provider_cmp_argv(dt_provider_t *pvp, dt_node_t *pnp, const char *kind,
4273 uint_t old_argc, dt_node_t *old_argv, uint_t new_argc, dt_node_t *new_argv)
4274 {
4275 dt_probe_t *prp = pnp->dn_ident->di_data;
4276 uint_t i;
4277
4278 char n1[DT_TYPE_NAMELEN];
4279 char n2[DT_TYPE_NAMELEN];
4280
4281 if (old_argc != new_argc) {
4282 dnerror(pnp, D_PROV_INCOMPAT,
4283 "probe %s:%s %s prototype mismatch:\n"
4284 "\t current: %u arg%s\n\tprevious: %u arg%s\n",
4285 pvp->pv_desc.dtvd_name, prp->pr_ident->di_name, kind,
4538 }
4539
4540 /*
4541 * Compute the DOF dtrace_diftype_t representation of a node's type. This is
4542 * called from a variety of places in the library so it cannot assume yypcb
4543 * is valid: any references to handle-specific data must be made through 'dtp'.
4544 */
4545 void
4546 dt_node_diftype(dtrace_hdl_t *dtp, const dt_node_t *dnp, dtrace_diftype_t *tp)
4547 {
4548 if (dnp->dn_ctfp == DT_STR_CTFP(dtp) &&
4549 dnp->dn_type == DT_STR_TYPE(dtp)) {
4550 tp->dtdt_kind = DIF_TYPE_STRING;
4551 tp->dtdt_ckind = CTF_K_UNKNOWN;
4552 } else {
4553 tp->dtdt_kind = DIF_TYPE_CTF;
4554 tp->dtdt_ckind = ctf_type_kind(dnp->dn_ctfp,
4555 ctf_type_resolve(dnp->dn_ctfp, dnp->dn_type));
4556 }
4557
4558 tp->dtdt_flags = (dnp->dn_flags & DT_NF_REF) ? DIF_TF_BYREF : 0;
4559 tp->dtdt_pad = 0;
4560 tp->dtdt_size = ctf_type_size(dnp->dn_ctfp, dnp->dn_type);
4561 }
4562
4563 void
4564 dt_node_printr(dt_node_t *dnp, FILE *fp, int depth)
4565 {
4566 char n[DT_TYPE_NAMELEN], buf[BUFSIZ], a[8];
4567 const dtrace_syminfo_t *dts;
4568 const dt_idnode_t *inp;
4569 dt_node_t *arg;
4570
4571 (void) fprintf(fp, "%*s", depth * 2, "");
4572 (void) dt_attr_str(dnp->dn_attr, a, sizeof (a));
4573
4574 if (dnp->dn_ctfp != NULL && dnp->dn_type != CTF_ERR &&
4575 ctf_type_name(dnp->dn_ctfp, dnp->dn_type, n, sizeof (n)) != NULL) {
4576 (void) snprintf(buf, BUFSIZ, "type=<%s> attr=%s flags=", n, a);
4577 } else {
4578 (void) snprintf(buf, BUFSIZ, "type=<%ld> attr=%s flags=",
|
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.
7 *
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.
12 *
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]
18 *
19 * CDDL HEADER END
20 */
21
22 /*
23 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2013, Joyent Inc. All rights reserved.
25 * Copyright (c) 2013 by Delphix. All rights reserved.
26 */
27
28 /*
29 * DTrace D Language Parser
30 *
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.
39 *
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.
45 *
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 ("<?>");
187 }
188 }
189
190 int
191 dt_type_lookup(const char *s, dtrace_typeinfo_t *tip)
192 {
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;
196
197 for (p = s, end = s + strlen(s); *p != '\0'; p = q) {
198 while (isspace(*p))
199 p++; /* skip leading whitespace prior to token */
200
201 if (p == end || (q = strpbrk(p + 1, delimiters)) == NULL)
202 break; /* empty string or single token remaining */
203
204 if (*q == '`') {
205 char *object = alloca((size_t)(q - p) + 1);
206 char *type = alloca((size_t)(end - s) + 1);
207
208 /*
209 * Copy from the start of the token (p) to the location
210 * backquote (q) to extract the nul-terminated object.
211 */
212 bcopy(p, object, (size_t)(q - p));
213 object[(size_t)(q - p)] = '\0';
214
215 /*
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.
219 */
220 bcopy(s, type, (size_t)(p - s));
221 bcopy(q + 1, type + (size_t)(p - s), strlen(q + 1) + 1);
222
223 /*
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]
230 */
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));
239 }
240
241 return (dtrace_lookup_by_type(dtp, object, type, tip));
242 }
243 }
244
245 if (yypcb->pcb_idepth != 0)
246 obj = DTRACE_OBJ_CDEFS;
247 else
248 obj = DTRACE_OBJ_EVERY;
249
250 return (dtrace_lookup_by_type(dtp, obj, s, tip));
251 }
252
253 /*
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.
261 */
262 int
263 dt_type_pointer(dtrace_typeinfo_t *tip)
264 {
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;
270
271 dt_module_t *dmp;
272 ctf_id_t ptr;
273
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);
278 }
279
280 if (yypcb->pcb_idepth != 0)
281 dmp = dtp->dt_cdefs;
282 else
283 dmp = dtp->dt_ddefs;
284
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));
289 }
290
291 ptr = ctf_add_pointer(dmp->dm_ctfp, CTF_ADD_ROOT, type);
292
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));
296 }
297
298 tip->dtt_object = dmp->dm_name;
299 tip->dtt_ctfp = dmp->dm_ctfp;
300 tip->dtt_type = ptr;
301 tip->dtt_flags = bflags;
302
303 return (0);
304 }
305
306 const char *
307 dt_type_name(ctf_file_t *ctfp, ctf_id_t type, char *buf, size_t len)
308 {
309 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
310
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");
321
385 goto return_ltype;
386 } else if (lrank > rrank) {
387 goto return_ltype;
388 } else
389 goto return_rtype;
390
391 return_ltype:
392 *ofp = lfp;
393 *otype = ltype;
394 return;
395
396 return_rtype:
397 *ofp = rfp;
398 *otype = rtype;
399 }
400
401 void
402 dt_node_promote(dt_node_t *lp, dt_node_t *rp, dt_node_t *dnp)
403 {
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));
407 }
408
409 const char *
410 dt_node_name(const dt_node_t *dnp, char *buf, size_t len)
411 {
412 char n1[DT_TYPE_NAMELEN];
413 char n2[DT_TYPE_NAMELEN];
414
415 const char *prefix = "", *suffix = "";
416 const dtrace_syminfo_t *dts;
417 char *s;
418
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));
654 }
655 }
656
657 void
658 dt_node_attr_assign(dt_node_t *dnp, dtrace_attribute_t attr)
659 {
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];
664
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)));
668 }
669
670 dnp->dn_attr = attr;
671 }
672
673 void
674 dt_node_type_assign(dt_node_t *dnp, ctf_file_t *fp, ctf_id_t type,
675 boolean_t user)
676 {
677 ctf_id_t base = ctf_type_resolve(fp, type);
678 uint_t kind = ctf_type_kind(fp, base);
679 ctf_encoding_t e;
680
681 dnp->dn_flags &=
682 ~(DT_NF_SIGNED | DT_NF_REF | DT_NF_BITFIELD | DT_NF_USERLAND);
683
684 if (kind == CTF_K_INTEGER && ctf_type_encoding(fp, base, &e) == 0) {
685 size_t size = e.cte_bits / NBBY;
686
687 if (size > 8 || (e.cte_bits % NBBY) != 0 || (size & (size - 1)))
688 dnp->dn_flags |= DT_NF_BITFIELD;
689
690 if (e.cte_format & CTF_INT_SIGNED)
691 dnp->dn_flags |= DT_NF_SIGNED;
692 }
693
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;
697 }
698
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;
706
707 if (user)
708 dnp->dn_flags |= DT_NF_USERLAND;
709
710 dnp->dn_flags |= DT_NF_COOKED;
711 dnp->dn_ctfp = fp;
712 dnp->dn_type = type;
713 }
714
715 void
716 dt_node_type_propagate(const dt_node_t *src, dt_node_t *dst)
717 {
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;
722 }
723
724 const char *
725 dt_node_type_name(const dt_node_t *dnp, char *buf, size_t len)
726 {
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);
731 }
732
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);
738 }
739
740 return (dt_type_name(dnp->dn_ctfp, dnp->dn_type, buf, len));
741 }
742
743 size_t
744 dt_node_type_size(const dt_node_t *dnp)
745 {
746 ctf_id_t base;
747 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
748
749 if (dnp->dn_kind == DT_NODE_STRING)
750 return (strlen(dnp->dn_string) + 1);
751
752 if (dt_node_is_dynamic(dnp) && dnp->dn_ident != NULL)
753 return (dt_ident_size(dnp->dn_ident));
754
755 base = ctf_type_resolve(dnp->dn_ctfp, dnp->dn_type);
756
757 if (ctf_type_kind(dnp->dn_ctfp, base) == CTF_K_FORWARD)
758 return (0);
759
760 /*
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.
767 */
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);
773
774 return (ctf_type_size(dnp->dn_ctfp, dnp->dn_type));
775 }
776
777 /*
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.
781 */
782 dt_ident_t *
783 dt_node_resolve(const dt_node_t *dnp, uint_t idkind)
784 {
785 dt_ident_t *idp;
786
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:
1240 int n = (yyintdecimal | (yyintsuffix[0] == 'u')) + 1;
1241 int i = 0;
1242
1243 const char *p;
1244 char c;
1245
1246 dnp->dn_op = DT_TOK_INT;
1247 dnp->dn_value = value;
1248
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;
1254 }
1255
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);
1261
1262 /*
1263 * If a prefix character is present in macro text, add
1264 * in the corresponding operator node (see dt_lex.l).
1265 */
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);
1273 }
1274 }
1275 }
1276
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 */
1281 }
1282
1283 dt_node_t *
1284 dt_node_string(char *string)
1285 {
1286 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
1287 dt_node_t *dnp;
1288
1289 if (string == NULL)
1290 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
1291
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);
1296
1297 return (dnp);
1298 }
1299
1300 dt_node_t *
1301 dt_node_ident(char *name)
1302 {
1303 dt_ident_t *idp;
1304 dt_node_t *dnp;
1305
1306 if (name == NULL)
1307 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
1308
1309 /*
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).
1314 */
1315 if ((idp = dt_idstack_lookup(&yypcb->pcb_globals, name)) != NULL &&
1351 int err;
1352
1353 /*
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.
1356 */
1357 if (ddp == NULL)
1358 ddp = dt_decl_pop_param(&name);
1359
1360 err = dt_decl_type(ddp, &dtt);
1361 dt_decl_free(ddp);
1362
1363 if (err != 0) {
1364 free(name);
1365 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
1366 }
1367
1368 dnp = dt_node_alloc(DT_NODE_TYPE);
1369 dnp->dn_op = DT_TOK_IDENT;
1370 dnp->dn_string = name;
1371
1372 dt_node_type_assign(dnp, dtt.dtt_ctfp, dtt.dtt_type, dtt.dtt_flags);
1373
1374 if (dtt.dtt_ctfp == dtp->dt_cdefs->dm_ctfp ||
1375 dtt.dtt_ctfp == dtp->dt_ddefs->dm_ctfp)
1376 dt_node_attr_assign(dnp, _dtrace_defattr);
1377 else
1378 dt_node_attr_assign(dnp, _dtrace_typattr);
1379
1380 return (dnp);
1381 }
1382
1383 /*
1384 * Create a type node corresponding to a varargs (...) parameter by just
1385 * assigning it type CTF_ERR. The decl processing code will handle this.
1386 */
1387 dt_node_t *
1388 dt_node_vatype(void)
1389 {
1390 dt_node_t *dnp = dt_node_alloc(DT_NODE_TYPE);
1391
1392 dnp->dn_op = DT_TOK_IDENT;
1596 * by a type rather than an integer, then it's an associative
1597 * array (assc). We then expect to match either DT_IDENT_ARRAY
1598 * for associative arrays or DT_IDENT_SCALAR for anything else.
1599 */
1600 assc = ddp->dd_kind == CTF_K_ARRAY &&
1601 ddp->dd_node->dn_kind == DT_NODE_TYPE;
1602
1603 idkind = assc ? DT_IDENT_ARRAY : DT_IDENT_SCALAR;
1604
1605 /*
1606 * Create a fake dt_node_t on the stack so we can determine the
1607 * type of any matching identifier by assigning to this node.
1608 * If the pre-existing ident has its di_type set, propagate
1609 * the type by hand so as not to trigger a prototype check for
1610 * arrays (yet); otherwise we use dt_ident_cook() on the ident
1611 * to ensure it is fully initialized before looking at it.
1612 */
1613 bzero(&idn, sizeof (dt_node_t));
1614
1615 if (idp != NULL && idp->di_type != CTF_ERR)
1616 dt_node_type_assign(&idn, idp->di_ctfp, idp->di_type,
1617 B_FALSE);
1618 else if (idp != NULL)
1619 (void) dt_ident_cook(&idn, idp, NULL);
1620
1621 if (assc) {
1622 if (class == DT_DC_THIS) {
1623 xyerror(D_DECL_LOCASSC, "associative arrays "
1624 "may not be declared as local variables:"
1625 " %s\n", dsp->ds_ident);
1626 }
1627
1628 if (dt_decl_type(ddp->dd_next, &dtt) != 0)
1629 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
1630 }
1631
1632 if (idp != NULL && (idp->di_kind != idkind ||
1633 ctf_type_cmp(dtt.dtt_ctfp, dtt.dtt_type,
1634 idn.dn_ctfp, idn.dn_type) != 0)) {
1635 xyerror(D_DECL_IDRED, "identifier redeclared: %s\n"
1636 "\t current: %s %s\n\tprevious: %s %s\n",
1637 dsp->ds_ident, dt_idkind_name(idkind),
1806 err = dt_decl_type(ddp, &dtt);
1807 dt_decl_free(ddp);
1808
1809 if (err != 0)
1810 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
1811
1812 type = ctf_type_resolve(dtt.dtt_ctfp, dtt.dtt_type);
1813 kind = ctf_type_kind(dtt.dtt_ctfp, type);
1814
1815 if (kind != CTF_K_STRUCT && kind != CTF_K_UNION) {
1816 xyerror(D_OFFSETOF_TYPE,
1817 "offsetof operand must be a struct or union type\n");
1818 }
1819
1820 if (ctf_member_info(dtt.dtt_ctfp, type, name, &ctm) == CTF_ERR) {
1821 xyerror(D_UNKNOWN, "failed to determine offset of %s: %s\n",
1822 name, ctf_errmsg(ctf_errno(dtt.dtt_ctfp)));
1823 }
1824
1825 bzero(&dn, sizeof (dn));
1826 dt_node_type_assign(&dn, dtt.dtt_ctfp, ctm.ctm_type, B_FALSE);
1827
1828 if (dn.dn_flags & DT_NF_BITFIELD) {
1829 xyerror(D_OFFSETOF_BITFIELD,
1830 "cannot take offset of a bit-field: %s\n", name);
1831 }
1832
1833 return (dt_node_int(ctm.ctm_offset / NBBY));
1834 }
1835
1836 dt_node_t *
1837 dt_node_op1(int op, dt_node_t *cp)
1838 {
1839 dt_node_t *dnp;
1840
1841 if (cp->dn_kind == DT_NODE_INT) {
1842 switch (op) {
1843 case DT_TOK_INEG:
1844 /*
1845 * If we're negating an unsigned integer, zero out any
1846 * extra top bits to truncate the value to the size of
1862 return (cp);
1863 }
1864 }
1865
1866 /*
1867 * If sizeof is applied to a type_name or string constant, we can
1868 * transform 'cp' into an integer constant in the node construction
1869 * pass so that it can then be used for arithmetic in this pass.
1870 */
1871 if (op == DT_TOK_SIZEOF &&
1872 (cp->dn_kind == DT_NODE_STRING || cp->dn_kind == DT_NODE_TYPE)) {
1873 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
1874 size_t size = dt_node_type_size(cp);
1875
1876 if (size == 0) {
1877 xyerror(D_SIZEOF_TYPE, "cannot apply sizeof to an "
1878 "operand of unknown size\n");
1879 }
1880
1881 dt_node_type_assign(cp, dtp->dt_ddefs->dm_ctfp,
1882 ctf_lookup_by_name(dtp->dt_ddefs->dm_ctfp, "size_t"),
1883 B_FALSE);
1884
1885 cp->dn_kind = DT_NODE_INT;
1886 cp->dn_op = DT_TOK_INT;
1887 cp->dn_value = size;
1888
1889 return (cp);
1890 }
1891
1892 dnp = dt_node_alloc(DT_NODE_OP1);
1893 assert(op <= USHRT_MAX);
1894 dnp->dn_op = (ushort_t)op;
1895 dnp->dn_child = cp;
1896
1897 return (dnp);
1898 }
1899
1900 /*
1901 * If an integer constant is being cast to another integer type, we can
1902 * perform the cast as part of integer constant folding in this pass. We must
1903 * take action when the integer is being cast to a smaller type or if it is
1941 */
1942 if (rp->dn_kind == DT_NODE_INT && rp->dn_value == 0 &&
1943 (op == DT_TOK_MOD || op == DT_TOK_DIV ||
1944 op == DT_TOK_MOD_EQ || op == DT_TOK_DIV_EQ))
1945 xyerror(D_DIV_ZERO, "expression contains division by zero\n");
1946
1947 /*
1948 * If both children are immediate values, we can just perform inline
1949 * calculation and return a new immediate node with the result.
1950 */
1951 if (lp->dn_kind == DT_NODE_INT && rp->dn_kind == DT_NODE_INT) {
1952 uintmax_t l = lp->dn_value;
1953 uintmax_t r = rp->dn_value;
1954
1955 dnp = dt_node_int(0); /* allocate new integer node for result */
1956
1957 switch (op) {
1958 case DT_TOK_LOR:
1959 dnp->dn_value = l || r;
1960 dt_node_type_assign(dnp,
1961 DT_INT_CTFP(dtp), DT_INT_TYPE(dtp), B_FALSE);
1962 break;
1963 case DT_TOK_LXOR:
1964 dnp->dn_value = (l != 0) ^ (r != 0);
1965 dt_node_type_assign(dnp,
1966 DT_INT_CTFP(dtp), DT_INT_TYPE(dtp), B_FALSE);
1967 break;
1968 case DT_TOK_LAND:
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_BOR:
1974 dnp->dn_value = l | r;
1975 dt_node_promote(lp, rp, dnp);
1976 break;
1977 case DT_TOK_XOR:
1978 dnp->dn_value = l ^ r;
1979 dt_node_promote(lp, rp, dnp);
1980 break;
1981 case DT_TOK_BAND:
1982 dnp->dn_value = l & r;
1983 dt_node_promote(lp, rp, dnp);
1984 break;
1985 case DT_TOK_EQU:
1986 dnp->dn_value = l == r;
1987 dt_node_type_assign(dnp,
1988 DT_INT_CTFP(dtp), DT_INT_TYPE(dtp), B_FALSE);
1989 break;
1990 case DT_TOK_NEQ:
1991 dnp->dn_value = l != r;
1992 dt_node_type_assign(dnp,
1993 DT_INT_CTFP(dtp), DT_INT_TYPE(dtp), B_FALSE);
1994 break;
1995 case DT_TOK_LT:
1996 dt_node_promote(lp, rp, dnp);
1997 if (dnp->dn_flags & DT_NF_SIGNED)
1998 dnp->dn_value = (intmax_t)l < (intmax_t)r;
1999 else
2000 dnp->dn_value = l < r;
2001 dt_node_type_assign(dnp,
2002 DT_INT_CTFP(dtp), DT_INT_TYPE(dtp), B_FALSE);
2003 break;
2004 case DT_TOK_LE:
2005 dt_node_promote(lp, rp, dnp);
2006 if (dnp->dn_flags & DT_NF_SIGNED)
2007 dnp->dn_value = (intmax_t)l <= (intmax_t)r;
2008 else
2009 dnp->dn_value = l <= r;
2010 dt_node_type_assign(dnp,
2011 DT_INT_CTFP(dtp), DT_INT_TYPE(dtp), B_FALSE);
2012 break;
2013 case DT_TOK_GT:
2014 dt_node_promote(lp, rp, dnp);
2015 if (dnp->dn_flags & DT_NF_SIGNED)
2016 dnp->dn_value = (intmax_t)l > (intmax_t)r;
2017 else
2018 dnp->dn_value = l > r;
2019 dt_node_type_assign(dnp,
2020 DT_INT_CTFP(dtp), DT_INT_TYPE(dtp), B_FALSE);
2021 break;
2022 case DT_TOK_GE:
2023 dt_node_promote(lp, rp, dnp);
2024 if (dnp->dn_flags & DT_NF_SIGNED)
2025 dnp->dn_value = (intmax_t)l >= (intmax_t)r;
2026 else
2027 dnp->dn_value = l >= r;
2028 dt_node_type_assign(dnp,
2029 DT_INT_CTFP(dtp), DT_INT_TYPE(dtp), B_FALSE);
2030 break;
2031 case DT_TOK_LSH:
2032 dnp->dn_value = l << r;
2033 dt_node_type_propagate(lp, dnp);
2034 dt_node_attr_assign(rp,
2035 dt_attr_min(lp->dn_attr, rp->dn_attr));
2036 break;
2037 case DT_TOK_RSH:
2038 dnp->dn_value = l >> r;
2039 dt_node_type_propagate(lp, dnp);
2040 dt_node_attr_assign(rp,
2041 dt_attr_min(lp->dn_attr, rp->dn_attr));
2042 break;
2043 case DT_TOK_ADD:
2044 dnp->dn_value = l + r;
2045 dt_node_promote(lp, rp, dnp);
2046 break;
2047 case DT_TOK_SUB:
2048 dnp->dn_value = l - r;
2049 dt_node_promote(lp, rp, dnp);
2250 if (ddp->dd_node == NULL) {
2251 xyerror(D_DECL_ARRNULL, "inline declaration requires "
2252 "array tuple signature: %s\n", dsp->ds_ident);
2253 }
2254
2255 if (ddp->dd_node->dn_kind != DT_NODE_TYPE) {
2256 xyerror(D_DECL_ARRNULL, "inline declaration cannot be "
2257 "of scalar array type: %s\n", dsp->ds_ident);
2258 }
2259
2260 if (dt_decl_type(ddp->dd_next, &dtt) != 0)
2261 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
2262 }
2263
2264 /*
2265 * If the inline identifier is not defined, then create it with the
2266 * orphan flag set. We do not insert the identifier into dt_globals
2267 * until we have successfully cooked the right-hand expression, below.
2268 */
2269 dnp = dt_node_alloc(DT_NODE_INLINE);
2270 dt_node_type_assign(dnp, dtt.dtt_ctfp, dtt.dtt_type, B_FALSE);
2271 dt_node_attr_assign(dnp, _dtrace_defattr);
2272
2273 if (dt_node_is_void(dnp)) {
2274 xyerror(D_DECL_VOIDOBJ,
2275 "cannot declare void inline: %s\n", dsp->ds_ident);
2276 }
2277
2278 if (ctf_type_kind(dnp->dn_ctfp, ctf_type_resolve(
2279 dnp->dn_ctfp, dnp->dn_type)) == CTF_K_FORWARD) {
2280 xyerror(D_DECL_INCOMPLETE,
2281 "incomplete struct/union/enum %s: %s\n",
2282 dt_node_type_name(dnp, n, sizeof (n)), dsp->ds_ident);
2283 }
2284
2285 if ((inp = malloc(sizeof (dt_idnode_t))) == NULL)
2286 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2287
2288 bzero(inp, sizeof (dt_idnode_t));
2289
2290 idp = dnp->dn_ident = dt_ident_create(dsp->ds_ident,
2405 dt_node_t *
2406 dt_node_member(dt_decl_t *ddp, char *name, dt_node_t *expr)
2407 {
2408 dtrace_typeinfo_t dtt;
2409 dt_node_t *dnp;
2410 int err;
2411
2412 if (ddp != NULL) {
2413 err = dt_decl_type(ddp, &dtt);
2414 dt_decl_free(ddp);
2415
2416 if (err != 0)
2417 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
2418 }
2419
2420 dnp = dt_node_alloc(DT_NODE_MEMBER);
2421 dnp->dn_membname = name;
2422 dnp->dn_membexpr = expr;
2423
2424 if (ddp != NULL)
2425 dt_node_type_assign(dnp, dtt.dtt_ctfp, dtt.dtt_type,
2426 dtt.dtt_flags);
2427
2428 return (dnp);
2429 }
2430
2431 dt_node_t *
2432 dt_node_xlator(dt_decl_t *ddp, dt_decl_t *sdp, char *name, dt_node_t *members)
2433 {
2434 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
2435 dtrace_typeinfo_t src, dst;
2436 dt_node_t sn, dn;
2437 dt_xlator_t *dxp;
2438 dt_node_t *dnp;
2439 int edst, esrc;
2440 uint_t kind;
2441
2442 char n1[DT_TYPE_NAMELEN];
2443 char n2[DT_TYPE_NAMELEN];
2444
2445 edst = dt_decl_type(ddp, &dst);
2446 dt_decl_free(ddp);
2447
2448 esrc = dt_decl_type(sdp, &src);
2449 dt_decl_free(sdp);
2450
2451 if (edst != 0 || esrc != 0) {
2452 free(name);
2453 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
2454 }
2455
2456 bzero(&sn, sizeof (sn));
2457 dt_node_type_assign(&sn, src.dtt_ctfp, src.dtt_type, B_FALSE);
2458
2459 bzero(&dn, sizeof (dn));
2460 dt_node_type_assign(&dn, dst.dtt_ctfp, dst.dtt_type, B_FALSE);
2461
2462 if (dt_xlator_lookup(dtp, &sn, &dn, DT_XLATE_EXACT) != NULL) {
2463 xyerror(D_XLATE_REDECL,
2464 "translator from %s to %s has already been declared\n",
2465 dt_node_type_name(&sn, n1, sizeof (n1)),
2466 dt_node_type_name(&dn, n2, sizeof (n2)));
2467 }
2468
2469 kind = ctf_type_kind(dst.dtt_ctfp,
2470 ctf_type_resolve(dst.dtt_ctfp, dst.dtt_type));
2471
2472 if (kind == CTF_K_FORWARD) {
2473 xyerror(D_XLATE_SOU, "incomplete struct/union/enum %s\n",
2474 dt_type_name(dst.dtt_ctfp, dst.dtt_type, n1, sizeof (n1)));
2475 }
2476
2477 if (kind != CTF_K_STRUCT && kind != CTF_K_UNION) {
2478 xyerror(D_XLATE_SOU,
2479 "translator output type must be a struct or union\n");
2480 }
2686 * matches its type if this is a global lookup. In the TLS or
2687 * local case, we don't know how the ident will be used until
2688 * the time operator -> is seen; more parsing is needed.
2689 */
2690 if (idp->di_kind != idkind && dhp == dtp->dt_globals) {
2691 xyerror(D_IDENT_BADREF, "%s '%s' may not be referenced "
2692 "as %s\n", dt_idkind_name(idp->di_kind),
2693 idp->di_name, dt_idkind_name(idkind));
2694 }
2695
2696 /*
2697 * Arrays and aggregations are not cooked individually. They
2698 * have dynamic types and must be referenced using operator [].
2699 * This is handled explicitly by the code for DT_TOK_LBRAC.
2700 */
2701 if (idp->di_kind != DT_IDENT_ARRAY &&
2702 idp->di_kind != DT_IDENT_AGG)
2703 attr = dt_ident_cook(dnp, idp, NULL);
2704 else {
2705 dt_node_type_assign(dnp,
2706 DT_DYN_CTFP(dtp), DT_DYN_TYPE(dtp), B_FALSE);
2707 attr = idp->di_attr;
2708 }
2709
2710 free(dnp->dn_string);
2711 dnp->dn_string = NULL;
2712 dnp->dn_kind = dnkind;
2713 dnp->dn_ident = idp;
2714 dnp->dn_flags |= DT_NF_LVALUE;
2715
2716 if (idp->di_flags & DT_IDFLG_WRITE)
2717 dnp->dn_flags |= DT_NF_WRITABLE;
2718
2719 dt_node_attr_assign(dnp, attr);
2720
2721 } else if (dhp == dtp->dt_globals && scope != DTRACE_OBJ_EXEC &&
2722 dtrace_lookup_by_name(dtp, scope, name, &sym, &dts) == 0) {
2723
2724 dt_module_t *mp = dt_module_lookup_by_name(dtp, dts.dts_object);
2725 int umod = (mp->dm_flags & DT_DM_KERNEL) == 0;
2726 static const char *const kunames[] = { "kernel", "user" };
2762 if (mp->dm_flags & DT_DM_PRIMARY)
2763 idp->di_flags |= DT_IDFLG_PRIM;
2764
2765 idp->di_next = dtp->dt_externs;
2766 dtp->dt_externs = idp;
2767
2768 if ((sip = malloc(sizeof (dtrace_syminfo_t))) == NULL)
2769 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2770
2771 bcopy(&dts, sip, sizeof (dtrace_syminfo_t));
2772 idp->di_data = sip;
2773 idp->di_ctfp = dtt.dtt_ctfp;
2774 idp->di_type = dtt.dtt_type;
2775
2776 free(dnp->dn_string);
2777 dnp->dn_string = NULL;
2778 dnp->dn_kind = DT_NODE_SYM;
2779 dnp->dn_ident = idp;
2780 dnp->dn_flags |= DT_NF_LVALUE;
2781
2782 dt_node_type_assign(dnp, dtt.dtt_ctfp, dtt.dtt_type,
2783 dtt.dtt_flags);
2784 dt_node_attr_assign(dnp, _dtrace_symattr);
2785
2786 if (uref) {
2787 idp->di_flags |= DT_IDFLG_USER;
2788 dnp->dn_flags |= DT_NF_USERLAND;
2789 }
2790
2791 } else if (scope == DTRACE_OBJ_EXEC && create == B_TRUE) {
2792 uint_t flags = DT_IDFLG_WRITE;
2793 uint_t id;
2794
2795 if (dt_idhash_nextid(dhp, &id) == -1) {
2796 xyerror(D_ID_OFLOW, "cannot create %s: limit on number "
2797 "of %s variables exceeded\n", name, sname);
2798 }
2799
2800 if (dhp == yypcb->pcb_locals)
2801 flags |= DT_IDFLG_LOCAL;
2802 else if (dhp == dtp->dt_tls)
2803 flags |= DT_IDFLG_TLS;
2811 &dt_idops_assc, NULL, dtp->dt_gen);
2812 } else {
2813 idp = dt_idhash_insert(dhp, name,
2814 idkind, flags, id, _dtrace_defattr, 0,
2815 &dt_idops_thaw, NULL, dtp->dt_gen);
2816 }
2817
2818 if (idp == NULL)
2819 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2820
2821 /*
2822 * Arrays and aggregations are not cooked individually. They
2823 * have dynamic types and must be referenced using operator [].
2824 * This is handled explicitly by the code for DT_TOK_LBRAC.
2825 */
2826 if (idp->di_kind != DT_IDENT_ARRAY &&
2827 idp->di_kind != DT_IDENT_AGG)
2828 attr = dt_ident_cook(dnp, idp, NULL);
2829 else {
2830 dt_node_type_assign(dnp,
2831 DT_DYN_CTFP(dtp), DT_DYN_TYPE(dtp), B_FALSE);
2832 attr = idp->di_attr;
2833 }
2834
2835 free(dnp->dn_string);
2836 dnp->dn_string = NULL;
2837 dnp->dn_kind = dnkind;
2838 dnp->dn_ident = idp;
2839 dnp->dn_flags |= DT_NF_LVALUE | DT_NF_WRITABLE;
2840
2841 dt_node_attr_assign(dnp, attr);
2842
2843 } else if (scope != DTRACE_OBJ_EXEC) {
2844 xyerror(D_IDENT_UNDEF, "failed to resolve %s%s%s: %s\n",
2845 dnp->dn_string, mark, name,
2846 dtrace_errmsg(dtp, dtrace_errno(dtp)));
2847 } else {
2848 xyerror(D_IDENT_UNDEF, "failed to resolve %s: %s\n",
2849 dnp->dn_string, dtrace_errmsg(dtp, dtrace_errno(dtp)));
2850 }
2851 }
2914 if (dnp->dn_op == DT_TOK_PREINC || dnp->dn_op == DT_TOK_POSTINC ||
2915 dnp->dn_op == DT_TOK_PREDEC || dnp->dn_op == DT_TOK_POSTDEC)
2916 idflags = DT_IDFLG_REF | DT_IDFLG_MOD;
2917 else
2918 idflags = DT_IDFLG_REF;
2919
2920 /*
2921 * We allow the unary ++ and -- operators to instantiate new scalar
2922 * variables if applied to an identifier; otherwise just cook as usual.
2923 */
2924 if (cp->dn_kind == DT_NODE_IDENT && (idflags & DT_IDFLG_MOD))
2925 dt_xcook_ident(cp, dtp->dt_globals, DT_IDENT_SCALAR, B_TRUE);
2926
2927 cp = dnp->dn_child = dt_node_cook(cp, 0); /* don't set idflags yet */
2928
2929 if (cp->dn_kind == DT_NODE_VAR && dt_ident_unref(cp->dn_ident)) {
2930 if (dt_type_lookup("int64_t", &dtt) != 0)
2931 xyerror(D_TYPE_ERR, "failed to lookup int64_t\n");
2932
2933 dt_ident_type_assign(cp->dn_ident, dtt.dtt_ctfp, dtt.dtt_type);
2934 dt_node_type_assign(cp, dtt.dtt_ctfp, dtt.dtt_type,
2935 dtt.dtt_flags);
2936 }
2937
2938 if (cp->dn_kind == DT_NODE_VAR)
2939 cp->dn_ident->di_flags |= idflags;
2940
2941 switch (dnp->dn_op) {
2942 case DT_TOK_DEREF:
2943 /*
2944 * If the deref operator is applied to a translated pointer,
2945 * we set our output type to the output of the translation.
2946 */
2947 if ((idp = dt_node_resolve(cp, DT_IDENT_XLPTR)) != NULL) {
2948 dt_xlator_t *dxp = idp->di_data;
2949
2950 dnp->dn_ident = &dxp->dx_souid;
2951 dt_node_type_assign(dnp,
2952 dnp->dn_ident->di_ctfp, dnp->dn_ident->di_type,
2953 cp->dn_flags & DT_NF_USERLAND);
2954 break;
2955 }
2956
2957 type = ctf_type_resolve(cp->dn_ctfp, cp->dn_type);
2958 kind = ctf_type_kind(cp->dn_ctfp, type);
2959
2960 if (kind == CTF_K_ARRAY) {
2961 if (ctf_array_info(cp->dn_ctfp, type, &r) != 0) {
2962 dtp->dt_ctferr = ctf_errno(cp->dn_ctfp);
2963 longjmp(yypcb->pcb_jmpbuf, EDT_CTF);
2964 } else
2965 type = r.ctr_contents;
2966 } else if (kind == CTF_K_POINTER) {
2967 type = ctf_type_reference(cp->dn_ctfp, type);
2968 } else {
2969 xyerror(D_DEREF_NONPTR,
2970 "cannot dereference non-pointer type\n");
2971 }
2972
2973 dt_node_type_assign(dnp, cp->dn_ctfp, type,
2974 cp->dn_flags & DT_NF_USERLAND);
2975 base = ctf_type_resolve(cp->dn_ctfp, type);
2976 kind = ctf_type_kind(cp->dn_ctfp, base);
2977
2978 if (kind == CTF_K_INTEGER && ctf_type_encoding(cp->dn_ctfp,
2979 base, &e) == 0 && IS_VOID(e)) {
2980 xyerror(D_DEREF_VOID,
2981 "cannot dereference pointer to void\n");
2982 }
2983
2984 if (kind == CTF_K_FUNCTION) {
2985 xyerror(D_DEREF_FUNC,
2986 "cannot dereference pointer to function\n");
2987 }
2988
2989 if (kind != CTF_K_ARRAY || dt_node_is_string(dnp))
2990 dnp->dn_flags |= DT_NF_LVALUE; /* see K&R[A7.4.3] */
2991
2992 /*
2993 * If we propagated the l-value bit and the child operand was
2994 * a writable D variable or a binary operation of the form
3011 if (!dt_node_is_arith(cp)) {
3012 xyerror(D_OP_ARITH, "operator %s requires an operand "
3013 "of arithmetic type\n", opstr(dnp->dn_op));
3014 }
3015 dt_node_type_propagate(cp, dnp); /* see K&R[A7.4.4-6] */
3016 break;
3017
3018 case DT_TOK_BNEG:
3019 if (!dt_node_is_integer(cp)) {
3020 xyerror(D_OP_INT, "operator %s requires an operand of "
3021 "integral type\n", opstr(dnp->dn_op));
3022 }
3023 dt_node_type_propagate(cp, dnp); /* see K&R[A7.4.4-6] */
3024 break;
3025
3026 case DT_TOK_LNEG:
3027 if (!dt_node_is_scalar(cp)) {
3028 xyerror(D_OP_SCALAR, "operator %s requires an operand "
3029 "of scalar type\n", opstr(dnp->dn_op));
3030 }
3031 dt_node_type_assign(dnp, DT_INT_CTFP(dtp), DT_INT_TYPE(dtp),
3032 B_FALSE);
3033 break;
3034
3035 case DT_TOK_ADDROF:
3036 if (cp->dn_kind == DT_NODE_VAR || cp->dn_kind == DT_NODE_AGG) {
3037 xyerror(D_ADDROF_VAR,
3038 "cannot take address of dynamic variable\n");
3039 }
3040
3041 if (dt_node_is_dynamic(cp)) {
3042 xyerror(D_ADDROF_VAR,
3043 "cannot take address of dynamic object\n");
3044 }
3045
3046 if (!(cp->dn_flags & DT_NF_LVALUE)) {
3047 xyerror(D_ADDROF_LVAL, /* see K&R[A7.4.2] */
3048 "unacceptable operand for unary & operator\n");
3049 }
3050
3051 if (cp->dn_flags & DT_NF_BITFIELD) {
3052 xyerror(D_ADDROF_BITFIELD,
3053 "cannot take address of bit-field\n");
3054 }
3055
3056 dtt.dtt_object = NULL;
3057 dtt.dtt_ctfp = cp->dn_ctfp;
3058 dtt.dtt_type = cp->dn_type;
3059
3060 if (dt_type_pointer(&dtt) == -1) {
3061 xyerror(D_TYPE_ERR, "cannot find type for \"&\": %s*\n",
3062 dt_node_type_name(cp, n, sizeof (n)));
3063 }
3064
3065 dt_node_type_assign(dnp, dtt.dtt_ctfp, dtt.dtt_type,
3066 cp->dn_flags & DT_NF_USERLAND);
3067 break;
3068
3069 case DT_TOK_SIZEOF:
3070 if (cp->dn_flags & DT_NF_BITFIELD) {
3071 xyerror(D_SIZEOF_BITFIELD,
3072 "cannot apply sizeof to a bit-field\n");
3073 }
3074
3075 if (dt_node_sizeof(cp) == 0) {
3076 xyerror(D_SIZEOF_TYPE, "cannot apply sizeof to an "
3077 "operand of unknown size\n");
3078 }
3079
3080 dt_node_type_assign(dnp, dtp->dt_ddefs->dm_ctfp,
3081 ctf_lookup_by_name(dtp->dt_ddefs->dm_ctfp, "size_t"),
3082 B_FALSE);
3083 break;
3084
3085 case DT_TOK_STRINGOF:
3086 if (!dt_node_is_scalar(cp) && !dt_node_is_pointer(cp) &&
3087 !dt_node_is_strcompat(cp)) {
3088 xyerror(D_STRINGOF_TYPE,
3089 "cannot apply stringof to a value of type %s\n",
3090 dt_node_type_name(cp, n, sizeof (n)));
3091 }
3092 dt_node_type_assign(dnp, DT_STR_CTFP(dtp), DT_STR_TYPE(dtp),
3093 cp->dn_flags & DT_NF_USERLAND);
3094 break;
3095
3096 case DT_TOK_PREINC:
3097 case DT_TOK_POSTINC:
3098 case DT_TOK_PREDEC:
3099 case DT_TOK_POSTDEC:
3100 if (dt_node_is_scalar(cp) == 0) {
3101 xyerror(D_OP_SCALAR, "operator %s requires operand of "
3102 "scalar type\n", opstr(dnp->dn_op));
3103 }
3104
3105 if (dt_node_is_vfptr(cp)) {
3106 xyerror(D_OP_VFPTR, "operator %s requires an operand "
3107 "of known size\n", opstr(dnp->dn_op));
3108 }
3109
3110 if (!(cp->dn_flags & DT_NF_LVALUE)) {
3111 xyerror(D_OP_LVAL, "operator %s requires modifiable "
3112 "lvalue as an operand\n", opstr(dnp->dn_op));
3113 }
3266
3267 if (!dt_node_is_arith(lp) || !dt_node_is_arith(rp)) {
3268 xyerror(D_OP_ARITH, "operator %s requires operands of "
3269 "arithmetic type\n", opstr(op));
3270 }
3271
3272 dt_node_promote(lp, rp, dnp); /* see K&R[A7.6] */
3273 break;
3274
3275 case DT_TOK_LAND:
3276 case DT_TOK_LXOR:
3277 case DT_TOK_LOR:
3278 lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF);
3279 rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
3280
3281 if (!dt_node_is_scalar(lp) || !dt_node_is_scalar(rp)) {
3282 xyerror(D_OP_SCALAR, "operator %s requires operands "
3283 "of scalar type\n", opstr(op));
3284 }
3285
3286 dt_node_type_assign(dnp, DT_INT_CTFP(dtp), DT_INT_TYPE(dtp),
3287 B_FALSE);
3288 dt_node_attr_assign(dnp, dt_attr_min(lp->dn_attr, rp->dn_attr));
3289 break;
3290
3291 case DT_TOK_LT:
3292 case DT_TOK_LE:
3293 case DT_TOK_GT:
3294 case DT_TOK_GE:
3295 case DT_TOK_EQU:
3296 case DT_TOK_NEQ:
3297 /*
3298 * The D comparison operators provide the ability to transform
3299 * a right-hand identifier into a corresponding enum tag value
3300 * if the left-hand side is an enum type. To do this, we cook
3301 * the left-hand side, and then see if the right-hand side is
3302 * an unscoped identifier defined in the enum. If so, we
3303 * convert into an integer constant node with the tag's value.
3304 */
3305 lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF);
3306
3307 kind = ctf_type_kind(lp->dn_ctfp,
3311 strchr(rp->dn_string, '`') == NULL && ctf_enum_value(
3312 lp->dn_ctfp, lp->dn_type, rp->dn_string, &val) == 0) {
3313
3314 if ((idp = dt_idstack_lookup(&yypcb->pcb_globals,
3315 rp->dn_string)) != NULL) {
3316 xyerror(D_IDENT_AMBIG,
3317 "ambiguous use of operator %s: %s is "
3318 "both a %s enum tag and a global %s\n",
3319 opstr(op), rp->dn_string,
3320 dt_node_type_name(lp, n1, sizeof (n1)),
3321 dt_idkind_name(idp->di_kind));
3322 }
3323
3324 free(rp->dn_string);
3325 rp->dn_string = NULL;
3326 rp->dn_kind = DT_NODE_INT;
3327 rp->dn_flags |= DT_NF_COOKED;
3328 rp->dn_op = DT_TOK_INT;
3329 rp->dn_value = (intmax_t)val;
3330
3331 dt_node_type_assign(rp, lp->dn_ctfp, lp->dn_type,
3332 B_FALSE);
3333 dt_node_attr_assign(rp, _dtrace_symattr);
3334 }
3335
3336 rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
3337
3338 /*
3339 * The rules for type checking for the relational operators are
3340 * described in the ANSI-C spec (see K&R[A7.9-10]). We perform
3341 * the various tests in order from least to most expensive. We
3342 * also allow derived strings to be compared as a first-class
3343 * type (resulting in a strcmp(3C)-style comparison), and we
3344 * slightly relax the A7.9 rules to permit void pointer
3345 * comparisons as in A7.10. Our users won't be confused by
3346 * this since they understand pointers are just numbers, and
3347 * relaxing this constraint simplifies the implementation.
3348 */
3349 if (ctf_type_compat(lp->dn_ctfp, lp->dn_type,
3350 rp->dn_ctfp, rp->dn_type))
3351 /*EMPTY*/;
3352 else if (dt_node_is_integer(lp) && dt_node_is_integer(rp))
3353 /*EMPTY*/;
3354 else if (dt_node_is_strcompat(lp) && dt_node_is_strcompat(rp) &&
3355 (dt_node_is_string(lp) || dt_node_is_string(rp)))
3356 /*EMPTY*/;
3357 else if (dt_node_is_ptrcompat(lp, rp, NULL, NULL) == 0) {
3358 xyerror(D_OP_INCOMPAT, "operands have "
3359 "incompatible types: \"%s\" %s \"%s\"\n",
3360 dt_node_type_name(lp, n1, sizeof (n1)), opstr(op),
3361 dt_node_type_name(rp, n2, sizeof (n2)));
3362 }
3363
3364 dt_node_type_assign(dnp, DT_INT_CTFP(dtp), DT_INT_TYPE(dtp),
3365 B_FALSE);
3366 dt_node_attr_assign(dnp, dt_attr_min(lp->dn_attr, rp->dn_attr));
3367 break;
3368
3369 case DT_TOK_ADD:
3370 case DT_TOK_SUB: {
3371 /*
3372 * The rules for type checking for the additive operators are
3373 * described in the ANSI-C spec (see K&R[A7.7]). Pointers and
3374 * integers may be manipulated according to specific rules. In
3375 * these cases D permits strings to be treated as pointers.
3376 */
3377 int lp_is_ptr, lp_is_int, rp_is_ptr, rp_is_int;
3378
3379 lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF);
3380 rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
3381
3382 lp_is_ptr = dt_node_is_string(lp) ||
3383 (dt_node_is_pointer(lp) && !dt_node_is_vfptr(lp));
3384 lp_is_int = dt_node_is_integer(lp);
3385
3393 } else if (lp_is_ptr && rp_is_int) {
3394 ctfp = lp->dn_ctfp;
3395 type = lp->dn_type;
3396 uref = lp->dn_flags & DT_NF_USERLAND;
3397 } else if (lp_is_int && rp_is_ptr && op == DT_TOK_ADD) {
3398 ctfp = rp->dn_ctfp;
3399 type = rp->dn_type;
3400 uref = rp->dn_flags & DT_NF_USERLAND;
3401 } else if (lp_is_ptr && rp_is_ptr && op == DT_TOK_SUB &&
3402 dt_node_is_ptrcompat(lp, rp, NULL, NULL)) {
3403 ctfp = dtp->dt_ddefs->dm_ctfp;
3404 type = ctf_lookup_by_name(ctfp, "ptrdiff_t");
3405 uref = 0;
3406 } else {
3407 xyerror(D_OP_INCOMPAT, "operands have incompatible "
3408 "types: \"%s\" %s \"%s\"\n",
3409 dt_node_type_name(lp, n1, sizeof (n1)), opstr(op),
3410 dt_node_type_name(rp, n2, sizeof (n2)));
3411 }
3412
3413 dt_node_type_assign(dnp, ctfp, type, B_FALSE);
3414 dt_node_attr_assign(dnp, dt_attr_min(lp->dn_attr, rp->dn_attr));
3415
3416 if (uref)
3417 dnp->dn_flags |= DT_NF_USERLAND;
3418 break;
3419 }
3420
3421 case DT_TOK_OR_EQ:
3422 case DT_TOK_XOR_EQ:
3423 case DT_TOK_AND_EQ:
3424 case DT_TOK_LSH_EQ:
3425 case DT_TOK_RSH_EQ:
3426 case DT_TOK_MOD_EQ:
3427 if (lp->dn_kind == DT_NODE_IDENT) {
3428 dt_xcook_ident(lp, dtp->dt_globals,
3429 DT_IDENT_SCALAR, B_TRUE);
3430 }
3431
3432 lp = dnp->dn_left =
3433 dt_node_cook(lp, DT_IDFLG_REF | DT_IDFLG_MOD);
3534 * If the right-hand side is a dynamic variable that is the
3535 * output of a translator, our result is the translated type.
3536 */
3537 if ((idp = dt_node_resolve(rp, DT_IDENT_XLSOU)) != NULL) {
3538 ctfp = idp->di_ctfp;
3539 type = idp->di_type;
3540 uref = idp->di_flags & DT_IDFLG_USER;
3541 } else {
3542 ctfp = rp->dn_ctfp;
3543 type = rp->dn_type;
3544 uref = rp->dn_flags & DT_NF_USERLAND;
3545 }
3546
3547 /*
3548 * If the left-hand side of an assignment statement is a virgin
3549 * variable created by this compilation pass, reset the type of
3550 * this variable to the type of the right-hand side.
3551 */
3552 if (lp->dn_kind == DT_NODE_VAR &&
3553 dt_ident_unref(lp->dn_ident)) {
3554 dt_node_type_assign(lp, ctfp, type, B_FALSE);
3555 dt_ident_type_assign(lp->dn_ident, ctfp, type);
3556
3557 if (uref) {
3558 lp->dn_flags |= DT_NF_USERLAND;
3559 lp->dn_ident->di_flags |= DT_IDFLG_USER;
3560 }
3561 }
3562
3563 if (lp->dn_kind == DT_NODE_VAR)
3564 lp->dn_ident->di_flags |= DT_IDFLG_MOD;
3565
3566 /*
3567 * The rules for type checking for the assignment operators are
3568 * described in the ANSI-C spec (see K&R[A7.17]). We share
3569 * most of this code with the argument list checking code.
3570 */
3571 if (!dt_node_is_string(lp)) {
3572 kind = ctf_type_kind(lp->dn_ctfp,
3573 ctf_type_resolve(lp->dn_ctfp, lp->dn_type));
3574
3748 "applied to pointer to type \"%s\"; must "
3749 "be applied to a struct or union pointer\n",
3750 ctf_type_name(ctfp, type, n1, sizeof (n1)));
3751 } else {
3752 xyerror(D_OP_SOU, "operator %s cannot be "
3753 "applied to type \"%s\"; must be applied "
3754 "to a struct or union\n", opstr(op),
3755 ctf_type_name(ctfp, type, n1, sizeof (n1)));
3756 }
3757 }
3758
3759 if (ctf_member_info(ctfp, type, rp->dn_string, &m) == CTF_ERR) {
3760 xyerror(D_TYPE_MEMBER,
3761 "%s is not a member of %s\n", rp->dn_string,
3762 ctf_type_name(ctfp, type, n1, sizeof (n1)));
3763 }
3764
3765 type = ctf_type_resolve(ctfp, m.ctm_type);
3766 kind = ctf_type_kind(ctfp, type);
3767
3768 dt_node_type_assign(dnp, ctfp, m.ctm_type, B_FALSE);
3769 dt_node_attr_assign(dnp, lp->dn_attr);
3770
3771 if (op == DT_TOK_PTR && (kind != CTF_K_ARRAY ||
3772 dt_node_is_string(dnp)))
3773 dnp->dn_flags |= DT_NF_LVALUE; /* see K&R[A7.3.3] */
3774
3775 if (op == DT_TOK_DOT && (lp->dn_flags & DT_NF_LVALUE) &&
3776 (kind != CTF_K_ARRAY || dt_node_is_string(dnp)))
3777 dnp->dn_flags |= DT_NF_LVALUE; /* see K&R[A7.3.3] */
3778
3779 if (lp->dn_flags & DT_NF_WRITABLE)
3780 dnp->dn_flags |= DT_NF_WRITABLE;
3781
3782 if (uref && (kind == CTF_K_POINTER ||
3783 (dnp->dn_flags & DT_NF_REF)))
3784 dnp->dn_flags |= DT_NF_USERLAND;
3785 break;
3786
3787 case DT_TOK_LBRAC: {
3788 /*
3874
3875 dt_node_free(lp);
3876 return (dt_node_cook(dnp, idflags));
3877 }
3878
3879 case DT_TOK_XLATE: {
3880 dt_xlator_t *dxp;
3881
3882 assert(lp->dn_kind == DT_NODE_TYPE);
3883 rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
3884 dxp = dt_xlator_lookup(dtp, rp, lp, DT_XLATE_FUZZY);
3885
3886 if (dxp == NULL) {
3887 xyerror(D_XLATE_NONE,
3888 "cannot translate from \"%s\" to \"%s\"\n",
3889 dt_node_type_name(rp, n1, sizeof (n1)),
3890 dt_node_type_name(lp, n2, sizeof (n2)));
3891 }
3892
3893 dnp->dn_ident = dt_xlator_ident(dxp, lp->dn_ctfp, lp->dn_type);
3894 dt_node_type_assign(dnp, DT_DYN_CTFP(dtp), DT_DYN_TYPE(dtp),
3895 B_FALSE);
3896 dt_node_attr_assign(dnp,
3897 dt_attr_min(rp->dn_attr, dnp->dn_ident->di_attr));
3898 break;
3899 }
3900
3901 case DT_TOK_LPAR: {
3902 ctf_id_t ltype, rtype;
3903 uint_t lkind, rkind;
3904
3905 assert(lp->dn_kind == DT_NODE_TYPE);
3906 rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
3907
3908 ltype = ctf_type_resolve(lp->dn_ctfp, lp->dn_type);
3909 lkind = ctf_type_kind(lp->dn_ctfp, ltype);
3910
3911 rtype = ctf_type_resolve(rp->dn_ctfp, rp->dn_type);
3912 rkind = ctf_type_kind(rp->dn_ctfp, rtype);
3913
3914 /*
3915 * The rules for casting are loosely explained in K&R[A7.5]
4040 if (ctf_type_compat(lp->dn_ctfp, lp->dn_type,
4041 rp->dn_ctfp, rp->dn_type)) {
4042 ctfp = lp->dn_ctfp;
4043 type = lp->dn_type;
4044 } else if (dt_node_is_integer(lp) && dt_node_is_integer(rp)) {
4045 dt_type_promote(lp, rp, &ctfp, &type);
4046 } else if (dt_node_is_strcompat(lp) && dt_node_is_strcompat(rp) &&
4047 (dt_node_is_string(lp) || dt_node_is_string(rp))) {
4048 ctfp = DT_STR_CTFP(yypcb->pcb_hdl);
4049 type = DT_STR_TYPE(yypcb->pcb_hdl);
4050 } else if (dt_node_is_ptrcompat(lp, rp, &ctfp, &type) == 0) {
4051 xyerror(D_OP_INCOMPAT,
4052 "operator ?: operands must have compatible types\n");
4053 }
4054
4055 if (dt_node_is_actfunc(lp) || dt_node_is_actfunc(rp)) {
4056 xyerror(D_OP_ACT, "action cannot be "
4057 "used in a conditional context\n");
4058 }
4059
4060 dt_node_type_assign(dnp, ctfp, type, B_FALSE);
4061 dt_node_attr_assign(dnp, dt_attr_min(dnp->dn_expr->dn_attr,
4062 dt_attr_min(lp->dn_attr, rp->dn_attr)));
4063
4064 return (dnp);
4065 }
4066
4067 static dt_node_t *
4068 dt_cook_statement(dt_node_t *dnp, uint_t idflags)
4069 {
4070 dnp->dn_expr = dt_node_cook(dnp->dn_expr, idflags);
4071 dt_node_attr_assign(dnp, dnp->dn_expr->dn_attr);
4072
4073 return (dnp);
4074 }
4075
4076 /*
4077 * If dn_aggfun is set, this node is a collapsed aggregation assignment (see
4078 * the special case code for DT_TOK_ASGN in dt_cook_op2() above), in which
4079 * case we cook both the tuple and the function call. If dn_aggfun is NULL,
4080 * this node is just a reference to the aggregation's type and attributes.
4081 */
4082 /*ARGSUSED*/
4083 static dt_node_t *
4084 dt_cook_aggregation(dt_node_t *dnp, uint_t idflags)
4085 {
4086 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
4087
4088 if (dnp->dn_aggfun != NULL) {
4089 dnp->dn_aggfun = dt_node_cook(dnp->dn_aggfun, DT_IDFLG_REF);
4090 dt_node_attr_assign(dnp, dt_ident_cook(dnp,
4091 dnp->dn_ident, &dnp->dn_aggtup));
4092 } else {
4093 dt_node_type_assign(dnp, DT_DYN_CTFP(dtp), DT_DYN_TYPE(dtp),
4094 B_FALSE);
4095 dt_node_attr_assign(dnp, dnp->dn_ident->di_attr);
4096 }
4097
4098 return (dnp);
4099 }
4100
4101 /*
4102 * Since D permits new variable identifiers to be instantiated in any program
4103 * expression, we may need to cook a clause's predicate either before or after
4104 * the action list depending on the program code in question. Consider:
4105 *
4106 * probe-description-list probe-description-list
4107 * /x++/ /x == 0/
4108 * { {
4109 * trace(x); trace(x++);
4110 * } }
4111 *
4112 * In the left-hand example, the predicate uses operator ++ to instantiate 'x'
4113 * as a variable of type int64_t. The predicate must be cooked first because
4114 * otherwise the statement trace(x) refers to an unknown identifier. In the
4276 dtrace_attribute_t attr = _dtrace_maxattr;
4277 ctf_membinfo_t ctm;
4278
4279 /*
4280 * Before cooking each translator member, we push a reference to the
4281 * hash containing translator-local identifiers on to pcb_globals to
4282 * temporarily interpose these identifiers in front of other globals.
4283 */
4284 dt_idstack_push(&yypcb->pcb_globals, dxp->dx_locals);
4285
4286 for (mnp = dnp->dn_members; mnp != NULL; mnp = mnp->dn_list) {
4287 if (ctf_member_info(dxp->dx_dst_ctfp, dxp->dx_dst_type,
4288 mnp->dn_membname, &ctm) == CTF_ERR) {
4289 xyerror(D_XLATE_MEMB,
4290 "translator member %s is not a member of %s\n",
4291 mnp->dn_membname, ctf_type_name(dxp->dx_dst_ctfp,
4292 dxp->dx_dst_type, n1, sizeof (n1)));
4293 }
4294
4295 (void) dt_node_cook(mnp, DT_IDFLG_REF);
4296 dt_node_type_assign(mnp, dxp->dx_dst_ctfp, ctm.ctm_type,
4297 B_FALSE);
4298 attr = dt_attr_min(attr, mnp->dn_attr);
4299
4300 if (dt_node_is_argcompat(mnp, mnp->dn_membexpr) == 0) {
4301 xyerror(D_XLATE_INCOMPAT,
4302 "translator member %s definition uses "
4303 "incompatible types: \"%s\" = \"%s\"\n",
4304 mnp->dn_membname,
4305 dt_node_type_name(mnp, n1, sizeof (n1)),
4306 dt_node_type_name(mnp->dn_membexpr,
4307 n2, sizeof (n2)));
4308 }
4309 }
4310
4311 dt_idstack_pop(&yypcb->pcb_globals, dxp->dx_locals);
4312
4313 dxp->dx_souid.di_attr = attr;
4314 dxp->dx_ptrid.di_attr = attr;
4315
4316 dt_node_type_assign(dnp, DT_DYN_CTFP(dtp), DT_DYN_TYPE(dtp), B_FALSE);
4317 dt_node_attr_assign(dnp, _dtrace_defattr);
4318
4319 return (dnp);
4320 }
4321
4322 static void
4323 dt_node_provider_cmp_argv(dt_provider_t *pvp, dt_node_t *pnp, const char *kind,
4324 uint_t old_argc, dt_node_t *old_argv, uint_t new_argc, dt_node_t *new_argv)
4325 {
4326 dt_probe_t *prp = pnp->dn_ident->di_data;
4327 uint_t i;
4328
4329 char n1[DT_TYPE_NAMELEN];
4330 char n2[DT_TYPE_NAMELEN];
4331
4332 if (old_argc != new_argc) {
4333 dnerror(pnp, D_PROV_INCOMPAT,
4334 "probe %s:%s %s prototype mismatch:\n"
4335 "\t current: %u arg%s\n\tprevious: %u arg%s\n",
4336 pvp->pv_desc.dtvd_name, prp->pr_ident->di_name, kind,
4589 }
4590
4591 /*
4592 * Compute the DOF dtrace_diftype_t representation of a node's type. This is
4593 * called from a variety of places in the library so it cannot assume yypcb
4594 * is valid: any references to handle-specific data must be made through 'dtp'.
4595 */
4596 void
4597 dt_node_diftype(dtrace_hdl_t *dtp, const dt_node_t *dnp, dtrace_diftype_t *tp)
4598 {
4599 if (dnp->dn_ctfp == DT_STR_CTFP(dtp) &&
4600 dnp->dn_type == DT_STR_TYPE(dtp)) {
4601 tp->dtdt_kind = DIF_TYPE_STRING;
4602 tp->dtdt_ckind = CTF_K_UNKNOWN;
4603 } else {
4604 tp->dtdt_kind = DIF_TYPE_CTF;
4605 tp->dtdt_ckind = ctf_type_kind(dnp->dn_ctfp,
4606 ctf_type_resolve(dnp->dn_ctfp, dnp->dn_type));
4607 }
4608
4609 tp->dtdt_flags = (dnp->dn_flags & DT_NF_REF) ?
4610 (dnp->dn_flags & DT_NF_USERLAND) ? DIF_TF_BYUREF :
4611 DIF_TF_BYREF : 0;
4612 tp->dtdt_pad = 0;
4613 tp->dtdt_size = ctf_type_size(dnp->dn_ctfp, dnp->dn_type);
4614 }
4615
4616 void
4617 dt_node_printr(dt_node_t *dnp, FILE *fp, int depth)
4618 {
4619 char n[DT_TYPE_NAMELEN], buf[BUFSIZ], a[8];
4620 const dtrace_syminfo_t *dts;
4621 const dt_idnode_t *inp;
4622 dt_node_t *arg;
4623
4624 (void) fprintf(fp, "%*s", depth * 2, "");
4625 (void) dt_attr_str(dnp->dn_attr, a, sizeof (a));
4626
4627 if (dnp->dn_ctfp != NULL && dnp->dn_type != CTF_ERR &&
4628 ctf_type_name(dnp->dn_ctfp, dnp->dn_type, n, sizeof (n)) != NULL) {
4629 (void) snprintf(buf, BUFSIZ, "type=<%s> attr=%s flags=", n, a);
4630 } else {
4631 (void) snprintf(buf, BUFSIZ, "type=<%ld> attr=%s flags=",
|