17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5351346dbSahl  * Common Development and Distribution License (the "License").
6351346dbSahl  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21351346dbSahl 
227c478bd9Sstevel@tonic-gate /*
2323a1cceaSRoger A. Faulkner  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24a386cc11SRobert Mustacchi  * Copyright (c) 2013 by Delphix. All rights reserved.
25a386cc11SRobert Mustacchi  * Copyright (c) 2013 Joyent, Inc. All rights reserved.
26*6eeafb34SRobert Mustacchi  * Copyright 2022 Oxide Computer Company
277c478bd9Sstevel@tonic-gate  */
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
307c478bd9Sstevel@tonic-gate #include <strings.h>
317c478bd9Sstevel@tonic-gate #include <stdlib.h>
327c478bd9Sstevel@tonic-gate #include <alloca.h>
337c478bd9Sstevel@tonic-gate #include <assert.h>
347c478bd9Sstevel@tonic-gate #include <errno.h>
357c478bd9Sstevel@tonic-gate #include <ctype.h>
367c478bd9Sstevel@tonic-gate #include <sys/procfs_isa.h>
377c478bd9Sstevel@tonic-gate #include <limits.h>
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate #include <dt_ident.h>
407c478bd9Sstevel@tonic-gate #include <dt_parser.h>
417c478bd9Sstevel@tonic-gate #include <dt_provider.h>
427c478bd9Sstevel@tonic-gate #include <dt_strtab.h>
437c478bd9Sstevel@tonic-gate #include <dt_impl.h>
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate /*
467c478bd9Sstevel@tonic-gate  * Common code for cooking an identifier that uses a typed signature list (we
477c478bd9Sstevel@tonic-gate  * use this for associative arrays and functions).  If the argument list is
487c478bd9Sstevel@tonic-gate  * of the same length and types, then return the return type.  Otherwise
497c478bd9Sstevel@tonic-gate  * print an appropriate compiler error message and abort the compile.
507c478bd9Sstevel@tonic-gate  */
517c478bd9Sstevel@tonic-gate static void
dt_idcook_sign(dt_node_t * dnp,dt_ident_t * idp,int argc,dt_node_t * args,const char * prefix,const char * suffix)527c478bd9Sstevel@tonic-gate dt_idcook_sign(dt_node_t *dnp, dt_ident_t *idp,
537c478bd9Sstevel@tonic-gate     int argc, dt_node_t *args, const char *prefix, const char *suffix)
547c478bd9Sstevel@tonic-gate {
557c478bd9Sstevel@tonic-gate 	dt_idsig_t *isp = idp->di_data;
5630ef842dSbmc 	int i, compat, mismatch, arglimit, iskey;
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate 	char n1[DT_TYPE_NAMELEN];
597c478bd9Sstevel@tonic-gate 	char n2[DT_TYPE_NAMELEN];
607c478bd9Sstevel@tonic-gate 
6130ef842dSbmc 	iskey = idp->di_kind == DT_IDENT_ARRAY || idp->di_kind == DT_IDENT_AGG;
6230ef842dSbmc 
637c478bd9Sstevel@tonic-gate 	if (isp->dis_varargs >= 0) {
647c478bd9Sstevel@tonic-gate 		mismatch = argc < isp->dis_varargs;
657c478bd9Sstevel@tonic-gate 		arglimit = isp->dis_varargs;
667c478bd9Sstevel@tonic-gate 	} else if (isp->dis_optargs >= 0) {
677c478bd9Sstevel@tonic-gate 		mismatch = (argc < isp->dis_optargs || argc > isp->dis_argc);
687c478bd9Sstevel@tonic-gate 		arglimit = argc;
697c478bd9Sstevel@tonic-gate 	} else {
707c478bd9Sstevel@tonic-gate 		mismatch = argc != isp->dis_argc;
717c478bd9Sstevel@tonic-gate 		arglimit = isp->dis_argc;
727c478bd9Sstevel@tonic-gate 	}
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate 	if (mismatch) {
7530ef842dSbmc 		xyerror(D_PROTO_LEN, "%s%s%s prototype mismatch: %d %s%s"
767c478bd9Sstevel@tonic-gate 		    "passed, %s%d expected\n", prefix, idp->di_name, suffix,
7730ef842dSbmc 		    argc, iskey ? "key" : "arg", argc == 1 ? " " : "s ",
787c478bd9Sstevel@tonic-gate 		    isp->dis_optargs >= 0 ? "at least " : "",
797c478bd9Sstevel@tonic-gate 		    isp->dis_optargs >= 0 ? isp->dis_optargs : arglimit);
807c478bd9Sstevel@tonic-gate 	}
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate 	for (i = 0; i < arglimit; i++, args = args->dn_list) {
837c478bd9Sstevel@tonic-gate 		if (isp->dis_args[i].dn_ctfp != NULL)
847c478bd9Sstevel@tonic-gate 			compat = dt_node_is_argcompat(&isp->dis_args[i], args);
857c478bd9Sstevel@tonic-gate 		else
867c478bd9Sstevel@tonic-gate 			compat = 1; /* "@" matches any type */
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate 		if (!compat) {
897c478bd9Sstevel@tonic-gate 			xyerror(D_PROTO_ARG,
9030ef842dSbmc 			    "%s%s%s %s #%d is incompatible with "
9130ef842dSbmc 			    "prototype:\n\tprototype: %s\n\t%9s: %s\n",
9230ef842dSbmc 			    prefix, idp->di_name, suffix,
9330ef842dSbmc 			    iskey ? "key" : "argument", i + 1,
947c478bd9Sstevel@tonic-gate 			    dt_node_type_name(&isp->dis_args[i], n1,
957c478bd9Sstevel@tonic-gate 			    sizeof (n1)),
9630ef842dSbmc 			    iskey ? "key" : "argument",
977c478bd9Sstevel@tonic-gate 			    dt_node_type_name(args, n2, sizeof (n2)));
987c478bd9Sstevel@tonic-gate 		}
997c478bd9Sstevel@tonic-gate 	}
1007c478bd9Sstevel@tonic-gate 
101a386cc11SRobert Mustacchi 	dt_node_type_assign(dnp, idp->di_ctfp, idp->di_type, B_FALSE);
1027c478bd9Sstevel@tonic-gate }
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate /*
1057c478bd9Sstevel@tonic-gate  * Cook an associative array identifier.  If this is the first time we are
1067c478bd9Sstevel@tonic-gate  * cooking this array, create its signature based on the argument list.
1077c478bd9Sstevel@tonic-gate  * Otherwise validate the argument list against the existing signature.
1087c478bd9Sstevel@tonic-gate  */
1097c478bd9Sstevel@tonic-gate static void
dt_idcook_assc(dt_node_t * dnp,dt_ident_t * idp,int argc,dt_node_t * args)1107c478bd9Sstevel@tonic-gate dt_idcook_assc(dt_node_t *dnp, dt_ident_t *idp, int argc, dt_node_t *args)
1117c478bd9Sstevel@tonic-gate {
1127c478bd9Sstevel@tonic-gate 	if (idp->di_data == NULL) {
1137c478bd9Sstevel@tonic-gate 		dt_idsig_t *isp = idp->di_data = malloc(sizeof (dt_idsig_t));
1147c478bd9Sstevel@tonic-gate 		char n[DT_TYPE_NAMELEN];
1157c478bd9Sstevel@tonic-gate 		int i;
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 		if (isp == NULL)
1187c478bd9Sstevel@tonic-gate 			longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate 		isp->dis_varargs = -1;
1217c478bd9Sstevel@tonic-gate 		isp->dis_optargs = -1;
1227c478bd9Sstevel@tonic-gate 		isp->dis_argc = argc;
1237c478bd9Sstevel@tonic-gate 		isp->dis_args = NULL;
12430ef842dSbmc 		isp->dis_auxinfo = 0;
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate 		if (argc != 0 && (isp->dis_args = calloc(argc,
1277c478bd9Sstevel@tonic-gate 		    sizeof (dt_node_t))) == NULL) {
1287c478bd9Sstevel@tonic-gate 			idp->di_data = NULL;
1297c478bd9Sstevel@tonic-gate 			free(isp);
1307c478bd9Sstevel@tonic-gate 			longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
1317c478bd9Sstevel@tonic-gate 		}
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate 		/*
1347c478bd9Sstevel@tonic-gate 		 * If this identifier has not been explicitly declared earlier,
1357c478bd9Sstevel@tonic-gate 		 * set the identifier's base type to be our special type <DYN>.
1367c478bd9Sstevel@tonic-gate 		 * If this ident is an aggregation, it will remain as is.  If
1377c478bd9Sstevel@tonic-gate 		 * this ident is an associative array, it will be reassigned
1387c478bd9Sstevel@tonic-gate 		 * based on the result type of the first assignment statement.
1397c478bd9Sstevel@tonic-gate 		 */
1407c478bd9Sstevel@tonic-gate 		if (!(idp->di_flags & DT_IDFLG_DECL)) {
1417c478bd9Sstevel@tonic-gate 			idp->di_ctfp = DT_DYN_CTFP(yypcb->pcb_hdl);
1427c478bd9Sstevel@tonic-gate 			idp->di_type = DT_DYN_TYPE(yypcb->pcb_hdl);
1437c478bd9Sstevel@tonic-gate 		}
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate 		for (i = 0; i < argc; i++, args = args->dn_list) {
1467c478bd9Sstevel@tonic-gate 			if (dt_node_is_dynamic(args) || dt_node_is_void(args)) {
1477c478bd9Sstevel@tonic-gate 				xyerror(D_KEY_TYPE, "%s expression may not be "
1487c478bd9Sstevel@tonic-gate 				    "used as %s index: key #%d\n",
1497c478bd9Sstevel@tonic-gate 				    dt_node_type_name(args, n, sizeof (n)),
1507c478bd9Sstevel@tonic-gate 				    dt_idkind_name(idp->di_kind), i + 1);
1517c478bd9Sstevel@tonic-gate 			}
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 			dt_node_type_propagate(args, &isp->dis_args[i]);
1547c478bd9Sstevel@tonic-gate 			isp->dis_args[i].dn_list = &isp->dis_args[i + 1];
1557c478bd9Sstevel@tonic-gate 		}
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate 		if (argc != 0)
1587c478bd9Sstevel@tonic-gate 			isp->dis_args[argc - 1].dn_list = NULL;
1597c478bd9Sstevel@tonic-gate 
160a386cc11SRobert Mustacchi 		dt_node_type_assign(dnp, idp->di_ctfp, idp->di_type, B_FALSE);
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate 	} else {
1637c478bd9Sstevel@tonic-gate 		dt_idcook_sign(dnp, idp, argc, args,
1647c478bd9Sstevel@tonic-gate 		    idp->di_kind == DT_IDENT_AGG ? "@" : "", "[ ]");
1657c478bd9Sstevel@tonic-gate 	}
1667c478bd9Sstevel@tonic-gate }
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate /*
1697c478bd9Sstevel@tonic-gate  * Cook a function call.  If this is the first time we are cooking this
1707c478bd9Sstevel@tonic-gate  * identifier, create its type signature based on predefined prototype stored
1717c478bd9Sstevel@tonic-gate  * in di_iarg.  We then validate the argument list against this signature.
1727c478bd9Sstevel@tonic-gate  */
1737c478bd9Sstevel@tonic-gate static void
dt_idcook_func(dt_node_t * dnp,dt_ident_t * idp,int argc,dt_node_t * args)1747c478bd9Sstevel@tonic-gate dt_idcook_func(dt_node_t *dnp, dt_ident_t *idp, int argc, dt_node_t *args)
1757c478bd9Sstevel@tonic-gate {
1767c478bd9Sstevel@tonic-gate 	if (idp->di_data == NULL) {
1777c478bd9Sstevel@tonic-gate 		dtrace_hdl_t *dtp = yypcb->pcb_hdl;
1787c478bd9Sstevel@tonic-gate 		dtrace_typeinfo_t dtt;
1797c478bd9Sstevel@tonic-gate 		dt_idsig_t *isp;
1807c478bd9Sstevel@tonic-gate 		char *s, *p1, *p2;
1817c478bd9Sstevel@tonic-gate 		int i = 0;
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 		assert(idp->di_iarg != NULL);
18423a1cceaSRoger A. Faulkner 		s = strdupa(idp->di_iarg);
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 		if ((p2 = strrchr(s, ')')) != NULL)
1877c478bd9Sstevel@tonic-gate 			*p2 = '\0'; /* mark end of parameter list string */
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 		if ((p1 = strchr(s, '(')) != NULL)
1907c478bd9Sstevel@tonic-gate 			*p1++ = '\0'; /* mark end of return type string */
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 		if (p1 == NULL || p2 == NULL) {
1937c478bd9Sstevel@tonic-gate 			xyerror(D_UNKNOWN, "internal error: malformed entry "
1947c478bd9Sstevel@tonic-gate 			    "for built-in function %s\n", idp->di_name);
1957c478bd9Sstevel@tonic-gate 		}
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 		for (p2 = p1; *p2 != '\0'; p2++) {
1987c478bd9Sstevel@tonic-gate 			if (!isspace(*p2)) {
1997c478bd9Sstevel@tonic-gate 				i++;
2007c478bd9Sstevel@tonic-gate 				break;
2017c478bd9Sstevel@tonic-gate 			}
2027c478bd9Sstevel@tonic-gate 		}
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 		for (p2 = strchr(p2, ','); p2++ != NULL; i++)
2057c478bd9Sstevel@tonic-gate 			p2 = strchr(p2, ',');
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 		/*
2087c478bd9Sstevel@tonic-gate 		 * We first allocate a new ident signature structure with the
2097c478bd9Sstevel@tonic-gate 		 * appropriate number of argument entries, and then look up
2107c478bd9Sstevel@tonic-gate 		 * the return type and store its CTF data in di_ctfp/type.
2117c478bd9Sstevel@tonic-gate 		 */
2127c478bd9Sstevel@tonic-gate 		if ((isp = idp->di_data = malloc(sizeof (dt_idsig_t))) == NULL)
2137c478bd9Sstevel@tonic-gate 			longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 		isp->dis_varargs = -1;
2167c478bd9Sstevel@tonic-gate 		isp->dis_optargs = -1;
2177c478bd9Sstevel@tonic-gate 		isp->dis_argc = i;
2187c478bd9Sstevel@tonic-gate 		isp->dis_args = NULL;
21930ef842dSbmc 		isp->dis_auxinfo = 0;
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate 		if (i != 0 && (isp->dis_args = calloc(i,
2227c478bd9Sstevel@tonic-gate 		    sizeof (dt_node_t))) == NULL) {
2237c478bd9Sstevel@tonic-gate 			idp->di_data = NULL;
2247c478bd9Sstevel@tonic-gate 			free(isp);
2257c478bd9Sstevel@tonic-gate 			longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2267c478bd9Sstevel@tonic-gate 		}
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate 		if (dt_type_lookup(s, &dtt) == -1) {
2297c478bd9Sstevel@tonic-gate 			xyerror(D_UNKNOWN, "failed to resolve type of %s (%s):"
2307c478bd9Sstevel@tonic-gate 			    " %s\n", idp->di_name, s,
2317c478bd9Sstevel@tonic-gate 			    dtrace_errmsg(dtp, dtrace_errno(dtp)));
2327c478bd9Sstevel@tonic-gate 		}
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 		if (idp->di_kind == DT_IDENT_AGGFUNC) {
2357c478bd9Sstevel@tonic-gate 			idp->di_ctfp = DT_DYN_CTFP(dtp);
2367c478bd9Sstevel@tonic-gate 			idp->di_type = DT_DYN_TYPE(dtp);
2377c478bd9Sstevel@tonic-gate 		} else {
2387c478bd9Sstevel@tonic-gate 			idp->di_ctfp = dtt.dtt_ctfp;
2397c478bd9Sstevel@tonic-gate 			idp->di_type = dtt.dtt_type;
2407c478bd9Sstevel@tonic-gate 		}
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate 		/*
2437c478bd9Sstevel@tonic-gate 		 * For each comma-delimited parameter in the prototype string,
2447c478bd9Sstevel@tonic-gate 		 * we look up the corresponding type and store its CTF data in
2457c478bd9Sstevel@tonic-gate 		 * the corresponding location in dis_args[].  We also recognize
2467c478bd9Sstevel@tonic-gate 		 * the special type string "@" to indicate that the specified
2477c478bd9Sstevel@tonic-gate 		 * parameter may be a D expression of *any* type (represented
2487c478bd9Sstevel@tonic-gate 		 * as a dis_args[] element with ctfp = NULL, type == CTF_ERR).
2497c478bd9Sstevel@tonic-gate 		 * If a varargs "..." is present, we record the argument index
2507c478bd9Sstevel@tonic-gate 		 * in dis_varargs for the benefit of dt_idcook_sign(), above.
2517c478bd9Sstevel@tonic-gate 		 * If the type of an argument is enclosed in square brackets
2527c478bd9Sstevel@tonic-gate 		 * (e.g. "[int]"), the argument is considered optional:  the
2537c478bd9Sstevel@tonic-gate 		 * argument may be absent, but if it is present, it must be of
2547c478bd9Sstevel@tonic-gate 		 * the specified type.  Note that varargs may not optional,
2557c478bd9Sstevel@tonic-gate 		 * optional arguments may not follow varargs, and non-optional
2567c478bd9Sstevel@tonic-gate 		 * arguments may not follow optional arguments.
2577c478bd9Sstevel@tonic-gate 		 */
2587c478bd9Sstevel@tonic-gate 		for (i = 0; i < isp->dis_argc; i++, p1 = p2) {
2597c478bd9Sstevel@tonic-gate 			while (isspace(*p1))
2607c478bd9Sstevel@tonic-gate 				p1++; /* skip leading whitespace */
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate 			if ((p2 = strchr(p1, ',')) == NULL)
2637c478bd9Sstevel@tonic-gate 				p2 = p1 + strlen(p1);
2647c478bd9Sstevel@tonic-gate 			else
2657c478bd9Sstevel@tonic-gate 				*p2++ = '\0';
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 			if (strcmp(p1, "@") == 0 || strcmp(p1, "...") == 0) {
2687c478bd9Sstevel@tonic-gate 				isp->dis_args[i].dn_ctfp = NULL;
2697c478bd9Sstevel@tonic-gate 				isp->dis_args[i].dn_type = CTF_ERR;
270*6eeafb34SRobert Mustacchi 				isp->dis_args[i].dn_bitoff = 0;
2717c478bd9Sstevel@tonic-gate 				if (*p1 == '.')
2727c478bd9Sstevel@tonic-gate 					isp->dis_varargs = i;
2737c478bd9Sstevel@tonic-gate 				continue;
2747c478bd9Sstevel@tonic-gate 			}
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate 			if (*p1 == '[' && p1[strlen(p1) - 1] == ']') {
2777c478bd9Sstevel@tonic-gate 				if (isp->dis_varargs != -1) {
2787c478bd9Sstevel@tonic-gate 					xyerror(D_UNKNOWN, "optional arg#%d "
2797c478bd9Sstevel@tonic-gate 					    "may not follow variable arg#%d\n",
2807c478bd9Sstevel@tonic-gate 					    i + 1, isp->dis_varargs + 1);
2817c478bd9Sstevel@tonic-gate 				}
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate 				if (isp->dis_optargs == -1)
2847c478bd9Sstevel@tonic-gate 					isp->dis_optargs = i;
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate 				p1[strlen(p1) - 1] = '\0';
2877c478bd9Sstevel@tonic-gate 				p1++;
2887c478bd9Sstevel@tonic-gate 			} else if (isp->dis_optargs != -1) {
2897c478bd9Sstevel@tonic-gate 				xyerror(D_UNKNOWN, "required arg#%d may not "
2907c478bd9Sstevel@tonic-gate 				    "follow optional arg#%d\n", i + 1,
2917c478bd9Sstevel@tonic-gate 				    isp->dis_optargs + 1);
2927c478bd9Sstevel@tonic-gate 			}
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate 			if (dt_type_lookup(p1, &dtt) == -1) {
2957c478bd9Sstevel@tonic-gate 				xyerror(D_UNKNOWN, "failed to resolve type of "
2967c478bd9Sstevel@tonic-gate 				    "%s arg#%d (%s): %s\n", idp->di_name, i + 1,
2977c478bd9Sstevel@tonic-gate 				    p1, dtrace_errmsg(dtp, dtrace_errno(dtp)));
2987c478bd9Sstevel@tonic-gate 			}
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate 			dt_node_type_assign(&isp->dis_args[i],
301a386cc11SRobert Mustacchi 			    dtt.dtt_ctfp, dtt.dtt_type, B_FALSE);
3027c478bd9Sstevel@tonic-gate 		}
3037c478bd9Sstevel@tonic-gate 	}
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate 	dt_idcook_sign(dnp, idp, argc, args, "", "( )");
3067c478bd9Sstevel@tonic-gate }
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate /*
3097c478bd9Sstevel@tonic-gate  * Cook a reference to the dynamically typed args[] array.  We verify that the
3107c478bd9Sstevel@tonic-gate  * reference is using a single integer constant, and then construct a new ident
3117c478bd9Sstevel@tonic-gate  * representing the appropriate type or translation specifically for this node.
3127c478bd9Sstevel@tonic-gate  */
3137c478bd9Sstevel@tonic-gate static void
dt_idcook_args(dt_node_t * dnp,dt_ident_t * idp,int argc,dt_node_t * ap)3147c478bd9Sstevel@tonic-gate dt_idcook_args(dt_node_t *dnp, dt_ident_t *idp, int argc, dt_node_t *ap)
3157c478bd9Sstevel@tonic-gate {
3167c478bd9Sstevel@tonic-gate 	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
3177c478bd9Sstevel@tonic-gate 	dt_probe_t *prp = yypcb->pcb_probe;
3187c478bd9Sstevel@tonic-gate 
3191a7c1b72Smws 	dt_node_t tag, *nnp, *xnp;
3207c478bd9Sstevel@tonic-gate 	dt_xlator_t *dxp;
3217c478bd9Sstevel@tonic-gate 	dt_ident_t *xidp;
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate 	char n1[DT_TYPE_NAMELEN];
3247c478bd9Sstevel@tonic-gate 	char n2[DT_TYPE_NAMELEN];
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 	if (argc != 1) {
3277c478bd9Sstevel@tonic-gate 		xyerror(D_PROTO_LEN, "%s[ ] prototype mismatch: %d arg%s"
3287c478bd9Sstevel@tonic-gate 		    "passed, 1 expected\n", idp->di_name, argc,
3297c478bd9Sstevel@tonic-gate 		    argc == 1 ? " " : "s ");
3307c478bd9Sstevel@tonic-gate 	}
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate 	if (ap->dn_kind != DT_NODE_INT) {
3337c478bd9Sstevel@tonic-gate 		xyerror(D_PROTO_ARG, "%s[ ] argument #1 is incompatible with "
3347c478bd9Sstevel@tonic-gate 		    "prototype:\n\tprototype: %s\n\t argument: %s\n",
3357c478bd9Sstevel@tonic-gate 		    idp->di_name, "integer constant",
3367c478bd9Sstevel@tonic-gate 		    dt_type_name(ap->dn_ctfp, ap->dn_type, n1, sizeof (n1)));
3377c478bd9Sstevel@tonic-gate 	}
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate 	if (yypcb->pcb_pdesc == NULL) {
3407c478bd9Sstevel@tonic-gate 		xyerror(D_ARGS_NONE, "%s[ ] may not be referenced outside "
3417c478bd9Sstevel@tonic-gate 		    "of a probe clause\n", idp->di_name);
3427c478bd9Sstevel@tonic-gate 	}
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate 	if (prp == NULL) {
3457c478bd9Sstevel@tonic-gate 		xyerror(D_ARGS_MULTI,
3467c478bd9Sstevel@tonic-gate 		    "%s[ ] may not be referenced because probe description %s "
3477c478bd9Sstevel@tonic-gate 		    "matches an unstable set of probes\n", idp->di_name,
3487c478bd9Sstevel@tonic-gate 		    dtrace_desc2str(yypcb->pcb_pdesc, n1, sizeof (n1)));
3497c478bd9Sstevel@tonic-gate 	}
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 	if (ap->dn_value >= prp->pr_argc) {
3527c478bd9Sstevel@tonic-gate 		xyerror(D_ARGS_IDX, "index %lld is out of range for %s %s[ ]\n",
3537c478bd9Sstevel@tonic-gate 		    (longlong_t)ap->dn_value, dtrace_desc2str(yypcb->pcb_pdesc,
3547c478bd9Sstevel@tonic-gate 		    n1, sizeof (n1)), idp->di_name);
3557c478bd9Sstevel@tonic-gate 	}
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate 	/*
3581a7c1b72Smws 	 * Look up the native and translated argument types for the probe.
3597c478bd9Sstevel@tonic-gate 	 * If no translation is needed, these will be the same underlying node.
3607c478bd9Sstevel@tonic-gate 	 * If translation is needed, look up the appropriate translator.  Once
3617c478bd9Sstevel@tonic-gate 	 * we have the appropriate node, create a new dt_ident_t for this node,
3627c478bd9Sstevel@tonic-gate 	 * assign it the appropriate attributes, and set the type of 'dnp'.
3637c478bd9Sstevel@tonic-gate 	 */
3647c478bd9Sstevel@tonic-gate 	xnp = prp->pr_xargv[ap->dn_value];
3657c478bd9Sstevel@tonic-gate 	nnp = prp->pr_nargv[prp->pr_mapping[ap->dn_value]];
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate 	if (xnp->dn_type == CTF_ERR) {
3687c478bd9Sstevel@tonic-gate 		xyerror(D_ARGS_TYPE, "failed to resolve translated type for "
3697c478bd9Sstevel@tonic-gate 		    "%s[%lld]\n", idp->di_name, (longlong_t)ap->dn_value);
3707c478bd9Sstevel@tonic-gate 	}
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate 	if (nnp->dn_type == CTF_ERR) {
3737c478bd9Sstevel@tonic-gate 		xyerror(D_ARGS_TYPE, "failed to resolve native type for "
3747c478bd9Sstevel@tonic-gate 		    "%s[%lld]\n", idp->di_name, (longlong_t)ap->dn_value);
3757c478bd9Sstevel@tonic-gate 	}
3767c478bd9Sstevel@tonic-gate 
3771a7c1b72Smws 	if (dtp->dt_xlatemode == DT_XL_STATIC && (
3781a7c1b72Smws 	    nnp == xnp || dt_node_is_argcompat(nnp, xnp))) {
3797c478bd9Sstevel@tonic-gate 		dnp->dn_ident = dt_ident_create(idp->di_name, idp->di_kind,
3807c478bd9Sstevel@tonic-gate 		    idp->di_flags | DT_IDFLG_ORPHAN, idp->di_id, idp->di_attr,
3811a7c1b72Smws 		    idp->di_vers, idp->di_ops, idp->di_iarg, idp->di_gen);
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate 		if (dnp->dn_ident == NULL)
3847c478bd9Sstevel@tonic-gate 			longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate 		dt_node_type_assign(dnp,
3877c478bd9Sstevel@tonic-gate 		    prp->pr_argv[ap->dn_value].dtt_ctfp,
388a386cc11SRobert Mustacchi 		    prp->pr_argv[ap->dn_value].dtt_type,
389a386cc11SRobert Mustacchi 		    prp->pr_argv[ap->dn_value].dtt_flags & DTT_FL_USER ?
390a386cc11SRobert Mustacchi 		    B_TRUE : B_FALSE);
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate 	} else if ((dxp = dt_xlator_lookup(dtp,
3931a7c1b72Smws 	    nnp, xnp, DT_XLATE_FUZZY)) != NULL || (
3941a7c1b72Smws 	    dxp = dt_xlator_lookup(dtp, dt_probe_tag(prp, ap->dn_value, &tag),
3951a7c1b72Smws 	    xnp, DT_XLATE_EXACT | DT_XLATE_EXTERN)) != NULL) {
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate 		xidp = dt_xlator_ident(dxp, xnp->dn_ctfp, xnp->dn_type);
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate 		dnp->dn_ident = dt_ident_create(idp->di_name, xidp->di_kind,
4007c478bd9Sstevel@tonic-gate 		    xidp->di_flags | DT_IDFLG_ORPHAN, idp->di_id, idp->di_attr,
4011a7c1b72Smws 		    idp->di_vers, idp->di_ops, idp->di_iarg, idp->di_gen);
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate 		if (dnp->dn_ident == NULL)
4047c478bd9Sstevel@tonic-gate 			longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
4057c478bd9Sstevel@tonic-gate 
4061a7c1b72Smws 		if (dt_xlator_dynamic(dxp))
4071a7c1b72Smws 			dxp->dx_arg = (int)ap->dn_value;
4081a7c1b72Smws 
4097c478bd9Sstevel@tonic-gate 		/*
4107c478bd9Sstevel@tonic-gate 		 * Propagate relevant members from the translator's internal
4117c478bd9Sstevel@tonic-gate 		 * dt_ident_t.  This code must be kept in sync with the state
4127c478bd9Sstevel@tonic-gate 		 * that is initialized for idents in dt_xlator_create().
4137c478bd9Sstevel@tonic-gate 		 */
4147c478bd9Sstevel@tonic-gate 		dnp->dn_ident->di_data = xidp->di_data;
4157c478bd9Sstevel@tonic-gate 		dnp->dn_ident->di_ctfp = xidp->di_ctfp;
4167c478bd9Sstevel@tonic-gate 		dnp->dn_ident->di_type = xidp->di_type;
4177c478bd9Sstevel@tonic-gate 
418a386cc11SRobert Mustacchi 		dt_node_type_assign(dnp, DT_DYN_CTFP(dtp), DT_DYN_TYPE(dtp),
419a386cc11SRobert Mustacchi 		    B_FALSE);
4207c478bd9Sstevel@tonic-gate 
4217c478bd9Sstevel@tonic-gate 	} else {
4227c478bd9Sstevel@tonic-gate 		xyerror(D_ARGS_XLATOR, "translator for %s[%lld] from %s to %s "
4237c478bd9Sstevel@tonic-gate 		    "is not defined\n", idp->di_name, (longlong_t)ap->dn_value,
4247c478bd9Sstevel@tonic-gate 		    dt_node_type_name(nnp, n1, sizeof (n1)),
4257c478bd9Sstevel@tonic-gate 		    dt_node_type_name(xnp, n2, sizeof (n2)));
4267c478bd9Sstevel@tonic-gate 	}
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate 	assert(dnp->dn_ident->di_flags & DT_IDFLG_ORPHAN);
4297c478bd9Sstevel@tonic-gate 	assert(dnp->dn_ident->di_id == idp->di_id);
4307c478bd9Sstevel@tonic-gate }
4317c478bd9Sstevel@tonic-gate 
4327c478bd9Sstevel@tonic-gate static void
dt_idcook_regs(dt_node_t * dnp,dt_ident_t * idp,int argc,dt_node_t * ap)4337c478bd9Sstevel@tonic-gate dt_idcook_regs(dt_node_t *dnp, dt_ident_t *idp, int argc, dt_node_t *ap)
4347c478bd9Sstevel@tonic-gate {
4357c478bd9Sstevel@tonic-gate 	dtrace_typeinfo_t dtt;
4367c478bd9Sstevel@tonic-gate 	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
4377c478bd9Sstevel@tonic-gate 	char n[DT_TYPE_NAMELEN];
4387c478bd9Sstevel@tonic-gate 
4397c478bd9Sstevel@tonic-gate 	if (argc != 1) {
4407c478bd9Sstevel@tonic-gate 		xyerror(D_PROTO_LEN, "%s[ ] prototype mismatch: %d arg%s"
4417c478bd9Sstevel@tonic-gate 		    "passed, 1 expected\n", idp->di_name,
4427c478bd9Sstevel@tonic-gate 		    argc, argc == 1 ? " " : "s ");
4437c478bd9Sstevel@tonic-gate 	}
4447c478bd9Sstevel@tonic-gate 
4457c478bd9Sstevel@tonic-gate 	if (ap->dn_kind != DT_NODE_INT) {
4467c478bd9Sstevel@tonic-gate 		xyerror(D_PROTO_ARG, "%s[ ] argument #1 is incompatible with "
4477c478bd9Sstevel@tonic-gate 		    "prototype:\n\tprototype: %s\n\t argument: %s\n",
4487c478bd9Sstevel@tonic-gate 		    idp->di_name, "integer constant",
4497c478bd9Sstevel@tonic-gate 		    dt_type_name(ap->dn_ctfp, ap->dn_type, n, sizeof (n)));
4507c478bd9Sstevel@tonic-gate 	}
4517c478bd9Sstevel@tonic-gate 
4527c478bd9Sstevel@tonic-gate 	if ((ap->dn_flags & DT_NF_SIGNED) && (int64_t)ap->dn_value < 0) {
4537c478bd9Sstevel@tonic-gate 		xyerror(D_REGS_IDX, "index %lld is out of range for array %s\n",
4547c478bd9Sstevel@tonic-gate 		    (longlong_t)ap->dn_value, idp->di_name);
4557c478bd9Sstevel@tonic-gate 	}
4567c478bd9Sstevel@tonic-gate 
4577c478bd9Sstevel@tonic-gate 	if (dt_type_lookup("uint64_t", &dtt) == -1) {
4587c478bd9Sstevel@tonic-gate 		xyerror(D_UNKNOWN, "failed to resolve type of %s: %s\n",
4597c478bd9Sstevel@tonic-gate 		    idp->di_name, dtrace_errmsg(dtp, dtrace_errno(dtp)));
4607c478bd9Sstevel@tonic-gate 	}
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate 	idp->di_ctfp = dtt.dtt_ctfp;
4637c478bd9Sstevel@tonic-gate 	idp->di_type = dtt.dtt_type;
4647c478bd9Sstevel@tonic-gate 
465a386cc11SRobert Mustacchi 	dt_node_type_assign(dnp, idp->di_ctfp, idp->di_type, B_FALSE);
4667c478bd9Sstevel@tonic-gate }
4677c478bd9Sstevel@tonic-gate 
4687c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4697c478bd9Sstevel@tonic-gate static void
dt_idcook_type(dt_node_t * dnp,dt_ident_t * idp,int argc,dt_node_t * args)4707c478bd9Sstevel@tonic-gate dt_idcook_type(dt_node_t *dnp, dt_ident_t *idp, int argc, dt_node_t *args)
4717c478bd9Sstevel@tonic-gate {
4727c478bd9Sstevel@tonic-gate 	if (idp->di_type == CTF_ERR) {
4737c478bd9Sstevel@tonic-gate 		dtrace_hdl_t *dtp = yypcb->pcb_hdl;
4747c478bd9Sstevel@tonic-gate 		dtrace_typeinfo_t dtt;
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate 		if (dt_type_lookup(idp->di_iarg, &dtt) == -1) {
4777c478bd9Sstevel@tonic-gate 			xyerror(D_UNKNOWN,
4787c478bd9Sstevel@tonic-gate 			    "failed to resolve type %s for identifier %s: %s\n",
4797c478bd9Sstevel@tonic-gate 			    (const char *)idp->di_iarg, idp->di_name,
4807c478bd9Sstevel@tonic-gate 			    dtrace_errmsg(dtp, dtrace_errno(dtp)));
4817c478bd9Sstevel@tonic-gate 		}
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate 		idp->di_ctfp = dtt.dtt_ctfp;
4847c478bd9Sstevel@tonic-gate 		idp->di_type = dtt.dtt_type;
4857c478bd9Sstevel@tonic-gate 	}
4867c478bd9Sstevel@tonic-gate 
487a386cc11SRobert Mustacchi 	dt_node_type_assign(dnp, idp->di_ctfp, idp->di_type, B_FALSE);
4887c478bd9Sstevel@tonic-gate }
4897c478bd9Sstevel@tonic-gate 
4907c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4917c478bd9Sstevel@tonic-gate static void
dt_idcook_thaw(dt_node_t * dnp,dt_ident_t * idp,int argc,dt_node_t * args)4927c478bd9Sstevel@tonic-gate dt_idcook_thaw(dt_node_t *dnp, dt_ident_t *idp, int argc, dt_node_t *args)
4937c478bd9Sstevel@tonic-gate {
4947c478bd9Sstevel@tonic-gate 	if (idp->di_ctfp != NULL && idp->di_type != CTF_ERR)
495a386cc11SRobert Mustacchi 		dt_node_type_assign(dnp, idp->di_ctfp, idp->di_type, B_FALSE);
4967c478bd9Sstevel@tonic-gate }
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate static void
dt_idcook_inline(dt_node_t * dnp,dt_ident_t * idp,int argc,dt_node_t * args)4997c478bd9Sstevel@tonic-gate dt_idcook_inline(dt_node_t *dnp, dt_ident_t *idp, int argc, dt_node_t *args)
5007c478bd9Sstevel@tonic-gate {
5017c478bd9Sstevel@tonic-gate 	if (idp->di_kind == DT_IDENT_ARRAY)
5027c478bd9Sstevel@tonic-gate 		dt_idcook_assc(dnp, idp, argc, args);
5037c478bd9Sstevel@tonic-gate 	else
5047c478bd9Sstevel@tonic-gate 		dt_idcook_thaw(dnp, idp, argc, args);
5057c478bd9Sstevel@tonic-gate }
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate static void
dt_iddtor_sign(dt_ident_t * idp)5087c478bd9Sstevel@tonic-gate dt_iddtor_sign(dt_ident_t *idp)
5097c478bd9Sstevel@tonic-gate {
5107c478bd9Sstevel@tonic-gate 	if (idp->di_data != NULL)
5117c478bd9Sstevel@tonic-gate 		free(((dt_idsig_t *)idp->di_data)->dis_args);
5127c478bd9Sstevel@tonic-gate 	free(idp->di_data);
5137c478bd9Sstevel@tonic-gate }
5147c478bd9Sstevel@tonic-gate 
5157c478bd9Sstevel@tonic-gate static void
dt_iddtor_free(dt_ident_t * idp)5167c478bd9Sstevel@tonic-gate dt_iddtor_free(dt_ident_t *idp)
5177c478bd9Sstevel@tonic-gate {
5187c478bd9Sstevel@tonic-gate 	free(idp->di_data);
5197c478bd9Sstevel@tonic-gate }
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate static void
dt_iddtor_inline(dt_ident_t * idp)5227c478bd9Sstevel@tonic-gate dt_iddtor_inline(dt_ident_t *idp)
5237c478bd9Sstevel@tonic-gate {
5247c478bd9Sstevel@tonic-gate 	dt_idnode_t *inp = idp->di_iarg;
5257c478bd9Sstevel@tonic-gate 
5267c478bd9Sstevel@tonic-gate 	if (inp != NULL) {
5277c478bd9Sstevel@tonic-gate 		dt_node_link_free(&inp->din_list);
5287c478bd9Sstevel@tonic-gate 
5297c478bd9Sstevel@tonic-gate 		if (inp->din_hash != NULL)
5307c478bd9Sstevel@tonic-gate 			dt_idhash_destroy(inp->din_hash);
5317c478bd9Sstevel@tonic-gate 
5327c478bd9Sstevel@tonic-gate 		free(inp->din_argv);
5337c478bd9Sstevel@tonic-gate 		free(inp);
5347c478bd9Sstevel@tonic-gate 	}
5357c478bd9Sstevel@tonic-gate 
5367c478bd9Sstevel@tonic-gate 	if (idp->di_kind == DT_IDENT_ARRAY)
5377c478bd9Sstevel@tonic-gate 		dt_iddtor_sign(idp);
5387c478bd9Sstevel@tonic-gate 	else
5397c478bd9Sstevel@tonic-gate 		dt_iddtor_free(idp);
5407c478bd9Sstevel@tonic-gate }
5417c478bd9Sstevel@tonic-gate 
5427c478bd9Sstevel@tonic-gate /*ARGSUSED*/
5437c478bd9Sstevel@tonic-gate static void
dt_iddtor_none(dt_ident_t * idp)5447c478bd9Sstevel@tonic-gate dt_iddtor_none(dt_ident_t *idp)
5457c478bd9Sstevel@tonic-gate {
5467c478bd9Sstevel@tonic-gate 	/* do nothing */
5477c478bd9Sstevel@tonic-gate }
5487c478bd9Sstevel@tonic-gate 
5497c478bd9Sstevel@tonic-gate static void
dt_iddtor_probe(dt_ident_t * idp)5507c478bd9Sstevel@tonic-gate dt_iddtor_probe(dt_ident_t *idp)
5517c478bd9Sstevel@tonic-gate {
5527c478bd9Sstevel@tonic-gate 	if (idp->di_data != NULL)
5537c478bd9Sstevel@tonic-gate 		dt_probe_destroy(idp->di_data);
5547c478bd9Sstevel@tonic-gate }
5557c478bd9Sstevel@tonic-gate 
5567c478bd9Sstevel@tonic-gate static size_t
dt_idsize_type(dt_ident_t * idp)5577c478bd9Sstevel@tonic-gate dt_idsize_type(dt_ident_t *idp)
5587c478bd9Sstevel@tonic-gate {
5597c478bd9Sstevel@tonic-gate 	return (ctf_type_size(idp->di_ctfp, idp->di_type));
5607c478bd9Sstevel@tonic-gate }
5617c478bd9Sstevel@tonic-gate 
5627c478bd9Sstevel@tonic-gate /*ARGSUSED*/
5637c478bd9Sstevel@tonic-gate static size_t
dt_idsize_none(dt_ident_t * idp)5647c478bd9Sstevel@tonic-gate dt_idsize_none(dt_ident_t *idp)
5657c478bd9Sstevel@tonic-gate {
5667c478bd9Sstevel@tonic-gate 	return (0);
5677c478bd9Sstevel@tonic-gate }
5687c478bd9Sstevel@tonic-gate 
5697c478bd9Sstevel@tonic-gate const dt_idops_t dt_idops_assc = {
5707c478bd9Sstevel@tonic-gate 	dt_idcook_assc,
5717c478bd9Sstevel@tonic-gate 	dt_iddtor_sign,
5727c478bd9Sstevel@tonic-gate 	dt_idsize_none,
5737c478bd9Sstevel@tonic-gate };
5747c478bd9Sstevel@tonic-gate 
5757c478bd9Sstevel@tonic-gate const dt_idops_t dt_idops_func = {
5767c478bd9Sstevel@tonic-gate 	dt_idcook_func,
5777c478bd9Sstevel@tonic-gate 	dt_iddtor_sign,
5787c478bd9Sstevel@tonic-gate 	dt_idsize_none,
5797c478bd9Sstevel@tonic-gate };
5807c478bd9Sstevel@tonic-gate 
5817c478bd9Sstevel@tonic-gate const dt_idops_t dt_idops_args = {
5827c478bd9Sstevel@tonic-gate 	dt_idcook_args,
5837c478bd9Sstevel@tonic-gate 	dt_iddtor_none,
5847c478bd9Sstevel@tonic-gate 	dt_idsize_none,
5857c478bd9Sstevel@tonic-gate };
5867c478bd9Sstevel@tonic-gate 
5877c478bd9Sstevel@tonic-gate const dt_idops_t dt_idops_regs = {
5887c478bd9Sstevel@tonic-gate 	dt_idcook_regs,
5897c478bd9Sstevel@tonic-gate 	dt_iddtor_free,
5907c478bd9Sstevel@tonic-gate 	dt_idsize_none,
5917c478bd9Sstevel@tonic-gate };
5927c478bd9Sstevel@tonic-gate 
5937c478bd9Sstevel@tonic-gate const dt_idops_t dt_idops_type = {
5947c478bd9Sstevel@tonic-gate 	dt_idcook_type,
5957c478bd9Sstevel@tonic-gate 	dt_iddtor_free,
5967c478bd9Sstevel@tonic-gate 	dt_idsize_type,
5977c478bd9Sstevel@tonic-gate };
5987c478bd9Sstevel@tonic-gate 
5997c478bd9Sstevel@tonic-gate const dt_idops_t dt_idops_thaw = {
6007c478bd9Sstevel@tonic-gate 	dt_idcook_thaw,
6017c478bd9Sstevel@tonic-gate 	dt_iddtor_free,
6027c478bd9Sstevel@tonic-gate 	dt_idsize_type,
6037c478bd9Sstevel@tonic-gate };
6047c478bd9Sstevel@tonic-gate 
6057c478bd9Sstevel@tonic-gate const dt_idops_t dt_idops_inline = {
6067c478bd9Sstevel@tonic-gate 	dt_idcook_inline,
6077c478bd9Sstevel@tonic-gate 	dt_iddtor_inline,
6087c478bd9Sstevel@tonic-gate 	dt_idsize_type,
6097c478bd9Sstevel@tonic-gate };
6107c478bd9Sstevel@tonic-gate 
6117c478bd9Sstevel@tonic-gate const dt_idops_t dt_idops_probe = {
6127c478bd9Sstevel@tonic-gate 	dt_idcook_thaw,
6137c478bd9Sstevel@tonic-gate 	dt_iddtor_probe,
6147c478bd9Sstevel@tonic-gate 	dt_idsize_none,
6157c478bd9Sstevel@tonic-gate };
6167c478bd9Sstevel@tonic-gate 
6177c478bd9Sstevel@tonic-gate static void
dt_idhash_populate(dt_idhash_t * dhp)6187c478bd9Sstevel@tonic-gate dt_idhash_populate(dt_idhash_t *dhp)
6197c478bd9Sstevel@tonic-gate {
6207c478bd9Sstevel@tonic-gate 	const dt_ident_t *idp = dhp->dh_tmpl;
6217c478bd9Sstevel@tonic-gate 
6227c478bd9Sstevel@tonic-gate 	dhp->dh_tmpl = NULL; /* clear dh_tmpl first to avoid recursion */
6237c478bd9Sstevel@tonic-gate 	dt_dprintf("populating %s idhash from %p\n", dhp->dh_name, (void *)idp);
6247c478bd9Sstevel@tonic-gate 
6257c478bd9Sstevel@tonic-gate 	for (; idp->di_name != NULL; idp++) {
6267c478bd9Sstevel@tonic-gate 		if (dt_idhash_insert(dhp, idp->di_name,
6277c478bd9Sstevel@tonic-gate 		    idp->di_kind, idp->di_flags, idp->di_id, idp->di_attr,
6287c478bd9Sstevel@tonic-gate 		    idp->di_vers, idp->di_ops ? idp->di_ops : &dt_idops_thaw,
6297c478bd9Sstevel@tonic-gate 		    idp->di_iarg, 0) == NULL)
6307c478bd9Sstevel@tonic-gate 			longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
6317c478bd9Sstevel@tonic-gate 	}
6327c478bd9Sstevel@tonic-gate }
6337c478bd9Sstevel@tonic-gate 
6347c478bd9Sstevel@tonic-gate dt_idhash_t *
dt_idhash_create(const char * name,const dt_ident_t * tmpl,uint_t min,uint_t max)6357c478bd9Sstevel@tonic-gate dt_idhash_create(const char *name, const dt_ident_t *tmpl,
6367c478bd9Sstevel@tonic-gate     uint_t min, uint_t max)
6377c478bd9Sstevel@tonic-gate {
6387c478bd9Sstevel@tonic-gate 	dt_idhash_t *dhp;
6397c478bd9Sstevel@tonic-gate 	size_t size;
6407c478bd9Sstevel@tonic-gate 
6417c478bd9Sstevel@tonic-gate 	assert(min <= max);
6427c478bd9Sstevel@tonic-gate 
6437c478bd9Sstevel@tonic-gate 	size = sizeof (dt_idhash_t) +
6447c478bd9Sstevel@tonic-gate 	    sizeof (dt_ident_t *) * (_dtrace_strbuckets - 1);
6457c478bd9Sstevel@tonic-gate 
6467c478bd9Sstevel@tonic-gate 	if ((dhp = malloc(size)) == NULL)
6477c478bd9Sstevel@tonic-gate 		return (NULL);
6487c478bd9Sstevel@tonic-gate 
6497c478bd9Sstevel@tonic-gate 	bzero(dhp, size);
6507c478bd9Sstevel@tonic-gate 	dhp->dh_name = name;
6517c478bd9Sstevel@tonic-gate 	dhp->dh_tmpl = tmpl;
6527c478bd9Sstevel@tonic-gate 	dhp->dh_nextid = min;
6537c478bd9Sstevel@tonic-gate 	dhp->dh_minid = min;
6547c478bd9Sstevel@tonic-gate 	dhp->dh_maxid = max;
6557c478bd9Sstevel@tonic-gate 	dhp->dh_hashsz = _dtrace_strbuckets;
6567c478bd9Sstevel@tonic-gate 
6577c478bd9Sstevel@tonic-gate 	return (dhp);
6587c478bd9Sstevel@tonic-gate }
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate /*
6617c478bd9Sstevel@tonic-gate  * Destroy an entire identifier hash.  This must be done using two passes with
6627c478bd9Sstevel@tonic-gate  * an inlined version of dt_ident_destroy() to avoid referencing freed memory.
6637c478bd9Sstevel@tonic-gate  * In the first pass di_dtor() is called for all identifiers; then the second
6647c478bd9Sstevel@tonic-gate  * pass frees the actual dt_ident_t's.  These must be done separately because
6657c478bd9Sstevel@tonic-gate  * a di_dtor() may operate on data structures which contain references to other
6667c478bd9Sstevel@tonic-gate  * identifiers inside of this hash itself (e.g. a global inline definition
6677c478bd9Sstevel@tonic-gate  * which contains a parse tree that refers to another global variable).
6687c478bd9Sstevel@tonic-gate  */
6697c478bd9Sstevel@tonic-gate void
dt_idhash_destroy(dt_idhash_t * dhp)6707c478bd9Sstevel@tonic-gate dt_idhash_destroy(dt_idhash_t *dhp)
6717c478bd9Sstevel@tonic-gate {
6727c478bd9Sstevel@tonic-gate 	dt_ident_t *idp, *next;
6737c478bd9Sstevel@tonic-gate 	ulong_t i;
6747c478bd9Sstevel@tonic-gate 
6757c478bd9Sstevel@tonic-gate 	for (i = 0; i < dhp->dh_hashsz; i++) {
6767c478bd9Sstevel@tonic-gate 		for (idp = dhp->dh_hash[i]; idp != NULL; idp = next) {
6777c478bd9Sstevel@tonic-gate 			next = idp->di_next;
6787c478bd9Sstevel@tonic-gate 			idp->di_ops->di_dtor(idp);
6797c478bd9Sstevel@tonic-gate 		}
6807c478bd9Sstevel@tonic-gate 	}
6817c478bd9Sstevel@tonic-gate 
6827c478bd9Sstevel@tonic-gate 	for (i = 0; i < dhp->dh_hashsz; i++) {
6837c478bd9Sstevel@tonic-gate 		for (idp = dhp->dh_hash[i]; idp != NULL; idp = next) {
6847c478bd9Sstevel@tonic-gate 			next = idp->di_next;
6857c478bd9Sstevel@tonic-gate 			free(idp->di_name);
6867c478bd9Sstevel@tonic-gate 			free(idp);
6877c478bd9Sstevel@tonic-gate 		}
6887c478bd9Sstevel@tonic-gate 	}
6897c478bd9Sstevel@tonic-gate 
6907c478bd9Sstevel@tonic-gate 	free(dhp);
6917c478bd9Sstevel@tonic-gate }
6927c478bd9Sstevel@tonic-gate 
6937c478bd9Sstevel@tonic-gate void
dt_idhash_update(dt_idhash_t * dhp)6947c478bd9Sstevel@tonic-gate dt_idhash_update(dt_idhash_t *dhp)
6957c478bd9Sstevel@tonic-gate {
6967c478bd9Sstevel@tonic-gate 	uint_t nextid = dhp->dh_minid;
6977c478bd9Sstevel@tonic-gate 	dt_ident_t *idp;
6987c478bd9Sstevel@tonic-gate 	ulong_t i;
6997c478bd9Sstevel@tonic-gate 
7007c478bd9Sstevel@tonic-gate 	for (i = 0; i < dhp->dh_hashsz; i++) {
7017c478bd9Sstevel@tonic-gate 		for (idp = dhp->dh_hash[i]; idp != NULL; idp = idp->di_next) {
702351346dbSahl 			/*
703351346dbSahl 			 * Right now we're hard coding which types need to be
704351346dbSahl 			 * reset, but ideally this would be done dynamically.
705351346dbSahl 			 */
7067c478bd9Sstevel@tonic-gate 			if (idp->di_kind == DT_IDENT_ARRAY ||
707351346dbSahl 			    idp->di_kind == DT_IDENT_SCALAR ||
708351346dbSahl 			    idp->di_kind == DT_IDENT_AGG)
7097c478bd9Sstevel@tonic-gate 				nextid = MAX(nextid, idp->di_id + 1);
7107c478bd9Sstevel@tonic-gate 		}
7117c478bd9Sstevel@tonic-gate 	}
7127c478bd9Sstevel@tonic-gate 
7137c478bd9Sstevel@tonic-gate 	dhp->dh_nextid = nextid;
7147c478bd9Sstevel@tonic-gate }
7157c478bd9Sstevel@tonic-gate 
7167c478bd9Sstevel@tonic-gate dt_ident_t *
dt_idhash_lookup(dt_idhash_t * dhp,const char * name)7177c478bd9Sstevel@tonic-gate dt_idhash_lookup(dt_idhash_t *dhp, const char *name)
7187c478bd9Sstevel@tonic-gate {
7197c478bd9Sstevel@tonic-gate 	size_t len;
7207c478bd9Sstevel@tonic-gate 	ulong_t h = dt_strtab_hash(name, &len) % dhp->dh_hashsz;
7217c478bd9Sstevel@tonic-gate 	dt_ident_t *idp;
7227c478bd9Sstevel@tonic-gate 
7237c478bd9Sstevel@tonic-gate 	if (dhp->dh_tmpl != NULL)
7247c478bd9Sstevel@tonic-gate 		dt_idhash_populate(dhp); /* fill hash w/ initial population */
7257c478bd9Sstevel@tonic-gate 
7267c478bd9Sstevel@tonic-gate 	for (idp = dhp->dh_hash[h]; idp != NULL; idp = idp->di_next) {
7277c478bd9Sstevel@tonic-gate 		if (strcmp(idp->di_name, name) == 0)
7287c478bd9Sstevel@tonic-gate 			return (idp);
7297c478bd9Sstevel@tonic-gate 	}
7307c478bd9Sstevel@tonic-gate 
7317c478bd9Sstevel@tonic-gate 	return (NULL);
7327c478bd9Sstevel@tonic-gate }
7337c478bd9Sstevel@tonic-gate 
7347c478bd9Sstevel@tonic-gate int
dt_idhash_nextid(dt_idhash_t * dhp,uint_t * p)7357c478bd9Sstevel@tonic-gate dt_idhash_nextid(dt_idhash_t *dhp, uint_t *p)
7367c478bd9Sstevel@tonic-gate {
7377c478bd9Sstevel@tonic-gate 	if (dhp->dh_nextid >= dhp->dh_maxid)
7387c478bd9Sstevel@tonic-gate 		return (-1); /* no more id's are free to allocate */
7397c478bd9Sstevel@tonic-gate 
7407c478bd9Sstevel@tonic-gate 	*p = dhp->dh_nextid++;
7417c478bd9Sstevel@tonic-gate 	return (0);
7427c478bd9Sstevel@tonic-gate }
7437c478bd9Sstevel@tonic-gate 
7447c478bd9Sstevel@tonic-gate ulong_t
dt_idhash_size(const dt_idhash_t * dhp)7457c478bd9Sstevel@tonic-gate dt_idhash_size(const dt_idhash_t *dhp)
7467c478bd9Sstevel@tonic-gate {
7477c478bd9Sstevel@tonic-gate 	return (dhp->dh_nelems);
7487c478bd9Sstevel@tonic-gate }
7497c478bd9Sstevel@tonic-gate 
7507c478bd9Sstevel@tonic-gate const char *
dt_idhash_name(const dt_idhash_t * dhp)7517c478bd9Sstevel@tonic-gate dt_idhash_name(const dt_idhash_t *dhp)
7527c478bd9Sstevel@tonic-gate {
7537c478bd9Sstevel@tonic-gate 	return (dhp->dh_name);
7547c478bd9Sstevel@tonic-gate }
7557c478bd9Sstevel@tonic-gate 
7567c478bd9Sstevel@tonic-gate dt_ident_t *
dt_idhash_insert(dt_idhash_t * dhp,const char * name,ushort_t kind,ushort_t flags,uint_t id,dtrace_attribute_t attr,uint_t vers,const dt_idops_t * ops,void * iarg,ulong_t gen)7577c478bd9Sstevel@tonic-gate dt_idhash_insert(dt_idhash_t *dhp, const char *name, ushort_t kind,
7587c478bd9Sstevel@tonic-gate     ushort_t flags, uint_t id, dtrace_attribute_t attr, uint_t vers,
7597c478bd9Sstevel@tonic-gate     const dt_idops_t *ops, void *iarg, ulong_t gen)
7607c478bd9Sstevel@tonic-gate {
7617c478bd9Sstevel@tonic-gate 	dt_ident_t *idp;
7627c478bd9Sstevel@tonic-gate 	ulong_t h;
7637c478bd9Sstevel@tonic-gate 
7647c478bd9Sstevel@tonic-gate 	if (dhp->dh_tmpl != NULL)
7657c478bd9Sstevel@tonic-gate 		dt_idhash_populate(dhp); /* fill hash w/ initial population */
7667c478bd9Sstevel@tonic-gate 
7677c478bd9Sstevel@tonic-gate 	idp = dt_ident_create(name, kind, flags, id,
7687c478bd9Sstevel@tonic-gate 	    attr, vers, ops, iarg, gen);
7697c478bd9Sstevel@tonic-gate 
7707c478bd9Sstevel@tonic-gate 	if (idp == NULL)
7717c478bd9Sstevel@tonic-gate 		return (NULL);
7727c478bd9Sstevel@tonic-gate 
7737c478bd9Sstevel@tonic-gate 	h = dt_strtab_hash(name, NULL) % dhp->dh_hashsz;
7747c478bd9Sstevel@tonic-gate 	idp->di_next = dhp->dh_hash[h];
7757c478bd9Sstevel@tonic-gate 
7767c478bd9Sstevel@tonic-gate 	dhp->dh_hash[h] = idp;
7777c478bd9Sstevel@tonic-gate 	dhp->dh_nelems++;
7787c478bd9Sstevel@tonic-gate 
7797c478bd9Sstevel@tonic-gate 	if (dhp->dh_defer != NULL)
7807c478bd9Sstevel@tonic-gate 		dhp->dh_defer(dhp, idp);
7817c478bd9Sstevel@tonic-gate 
7827c478bd9Sstevel@tonic-gate 	return (idp);
7837c478bd9Sstevel@tonic-gate }
7847c478bd9Sstevel@tonic-gate 
7857c478bd9Sstevel@tonic-gate void
dt_idhash_xinsert(dt_idhash_t * dhp,dt_ident_t * idp)7867c478bd9Sstevel@tonic-gate dt_idhash_xinsert(dt_idhash_t *dhp, dt_ident_t *idp)
7877c478bd9Sstevel@tonic-gate {
7887c478bd9Sstevel@tonic-gate 	ulong_t h;
7897c478bd9Sstevel@tonic-gate 
7907c478bd9Sstevel@tonic-gate 	if (dhp->dh_tmpl != NULL)
7917c478bd9Sstevel@tonic-gate 		dt_idhash_populate(dhp); /* fill hash w/ initial population */
7927c478bd9Sstevel@tonic-gate 
7937c478bd9Sstevel@tonic-gate 	h = dt_strtab_hash(idp->di_name, NULL) % dhp->dh_hashsz;
7947c478bd9Sstevel@tonic-gate 	idp->di_next = dhp->dh_hash[h];
7957c478bd9Sstevel@tonic-gate 	idp->di_flags &= ~DT_IDFLG_ORPHAN;
7967c478bd9Sstevel@tonic-gate 
7977c478bd9Sstevel@tonic-gate 	dhp->dh_hash[h] = idp;
7987c478bd9Sstevel@tonic-gate 	dhp->dh_nelems++;
7997c478bd9Sstevel@tonic-gate 
8007c478bd9Sstevel@tonic-gate 	if (dhp->dh_defer != NULL)
8017c478bd9Sstevel@tonic-gate 		dhp->dh_defer(dhp, idp);
8027c478bd9Sstevel@tonic-gate }
8037c478bd9Sstevel@tonic-gate 
8047c478bd9Sstevel@tonic-gate void
dt_idhash_delete(dt_idhash_t * dhp,dt_ident_t * key)8057c478bd9Sstevel@tonic-gate dt_idhash_delete(dt_idhash_t *dhp, dt_ident_t *key)
8067c478bd9Sstevel@tonic-gate {
8077c478bd9Sstevel@tonic-gate 	size_t len;
8087c478bd9Sstevel@tonic-gate 	ulong_t h = dt_strtab_hash(key->di_name, &len) % dhp->dh_hashsz;
8097c478bd9Sstevel@tonic-gate 	dt_ident_t **pp = &dhp->dh_hash[h];
8107c478bd9Sstevel@tonic-gate 	dt_ident_t *idp;
8117c478bd9Sstevel@tonic-gate 
8127c478bd9Sstevel@tonic-gate 	for (idp = dhp->dh_hash[h]; idp != NULL; idp = idp->di_next) {
8137c478bd9Sstevel@tonic-gate 		if (idp == key)
8147c478bd9Sstevel@tonic-gate 			break;
8157c478bd9Sstevel@tonic-gate 		else
8167c478bd9Sstevel@tonic-gate 			pp = &idp->di_next;
8177c478bd9Sstevel@tonic-gate 	}
8187c478bd9Sstevel@tonic-gate 
8197c478bd9Sstevel@tonic-gate 	assert(idp == key);
8207c478bd9Sstevel@tonic-gate 	*pp = idp->di_next;
8217c478bd9Sstevel@tonic-gate 
8227c478bd9Sstevel@tonic-gate 	assert(dhp->dh_nelems != 0);
8237c478bd9Sstevel@tonic-gate 	dhp->dh_nelems--;
8247c478bd9Sstevel@tonic-gate 
8251a7c1b72Smws 	if (!(idp->di_flags & DT_IDFLG_ORPHAN))
8261a7c1b72Smws 		dt_ident_destroy(idp);
8277c478bd9Sstevel@tonic-gate }
8287c478bd9Sstevel@tonic-gate 
8297c478bd9Sstevel@tonic-gate static int
dt_idhash_comp(const void * lp,const void * rp)8307c478bd9Sstevel@tonic-gate dt_idhash_comp(const void *lp, const void *rp)
8317c478bd9Sstevel@tonic-gate {
8327c478bd9Sstevel@tonic-gate 	const dt_ident_t *lhs = *((const dt_ident_t **)lp);
8337c478bd9Sstevel@tonic-gate 	const dt_ident_t *rhs = *((const dt_ident_t **)rp);
8347c478bd9Sstevel@tonic-gate 
8357c478bd9Sstevel@tonic-gate 	if (lhs->di_id != rhs->di_id)
8367c478bd9Sstevel@tonic-gate 		return ((int)(lhs->di_id - rhs->di_id));
8377c478bd9Sstevel@tonic-gate 	else
8387c478bd9Sstevel@tonic-gate 		return (strcmp(lhs->di_name, rhs->di_name));
8397c478bd9Sstevel@tonic-gate }
8407c478bd9Sstevel@tonic-gate 
8417c478bd9Sstevel@tonic-gate int
dt_idhash_iter(dt_idhash_t * dhp,dt_idhash_f * func,void * data)8427c478bd9Sstevel@tonic-gate dt_idhash_iter(dt_idhash_t *dhp, dt_idhash_f *func, void *data)
8437c478bd9Sstevel@tonic-gate {
8447c478bd9Sstevel@tonic-gate 	dt_ident_t **ids;
8457c478bd9Sstevel@tonic-gate 	dt_ident_t *idp;
8469e8164f5Sahl 	ulong_t i, j, n;
8477c478bd9Sstevel@tonic-gate 	int rv;
8487c478bd9Sstevel@tonic-gate 
8497c478bd9Sstevel@tonic-gate 	if (dhp->dh_tmpl != NULL)
8507c478bd9Sstevel@tonic-gate 		dt_idhash_populate(dhp); /* fill hash w/ initial population */
8517c478bd9Sstevel@tonic-gate 
8529e8164f5Sahl 	n = dhp->dh_nelems;
8539e8164f5Sahl 	ids = alloca(sizeof (dt_ident_t *) * n);
8547c478bd9Sstevel@tonic-gate 
8557c478bd9Sstevel@tonic-gate 	for (i = 0, j = 0; i < dhp->dh_hashsz; i++) {
8567c478bd9Sstevel@tonic-gate 		for (idp = dhp->dh_hash[i]; idp != NULL; idp = idp->di_next)
8577c478bd9Sstevel@tonic-gate 			ids[j++] = idp;
8587c478bd9Sstevel@tonic-gate 	}
8597c478bd9Sstevel@tonic-gate 
8607c478bd9Sstevel@tonic-gate 	qsort(ids, dhp->dh_nelems, sizeof (dt_ident_t *), dt_idhash_comp);
8617c478bd9Sstevel@tonic-gate 
8629e8164f5Sahl 	for (i = 0; i < n; i++) {
8637c478bd9Sstevel@tonic-gate 		if ((rv = func(dhp, ids[i], data)) != 0)
8647c478bd9Sstevel@tonic-gate 			return (rv);
8657c478bd9Sstevel@tonic-gate 	}
8667c478bd9Sstevel@tonic-gate 
8677c478bd9Sstevel@tonic-gate 	return (0);
8687c478bd9Sstevel@tonic-gate }
8697c478bd9Sstevel@tonic-gate 
8707c478bd9Sstevel@tonic-gate dt_ident_t *
dt_idstack_lookup(dt_idstack_t * sp,const char * name)8717c478bd9Sstevel@tonic-gate dt_idstack_lookup(dt_idstack_t *sp, const char *name)
8727c478bd9Sstevel@tonic-gate {
8737c478bd9Sstevel@tonic-gate 	dt_idhash_t *dhp;
8747c478bd9Sstevel@tonic-gate 	dt_ident_t *idp;
8757c478bd9Sstevel@tonic-gate 
8767c478bd9Sstevel@tonic-gate 	for (dhp = dt_list_prev(&sp->dids_list);
8777c478bd9Sstevel@tonic-gate 	    dhp != NULL; dhp = dt_list_prev(dhp)) {
8787c478bd9Sstevel@tonic-gate 		if ((idp = dt_idhash_lookup(dhp, name)) != NULL)
8797c478bd9Sstevel@tonic-gate 			return (idp);
8807c478bd9Sstevel@tonic-gate 	}
8817c478bd9Sstevel@tonic-gate 
8827c478bd9Sstevel@tonic-gate 	return (NULL);
8837c478bd9Sstevel@tonic-gate }
8847c478bd9Sstevel@tonic-gate 
8857c478bd9Sstevel@tonic-gate void
dt_idstack_push(dt_idstack_t * sp,dt_idhash_t * dhp)8867c478bd9Sstevel@tonic-gate dt_idstack_push(dt_idstack_t *sp, dt_idhash_t *dhp)
8877c478bd9Sstevel@tonic-gate {
8887c478bd9Sstevel@tonic-gate 	dt_list_append(&sp->dids_list, dhp);
8897c478bd9Sstevel@tonic-gate }
8907c478bd9Sstevel@tonic-gate 
8917c478bd9Sstevel@tonic-gate void
dt_idstack_pop(dt_idstack_t * sp,dt_idhash_t * dhp)8927c478bd9Sstevel@tonic-gate dt_idstack_pop(dt_idstack_t *sp, dt_idhash_t *dhp)
8937c478bd9Sstevel@tonic-gate {
8947c478bd9Sstevel@tonic-gate 	assert(dt_list_prev(&sp->dids_list) == dhp);
8957c478bd9Sstevel@tonic-gate 	dt_list_delete(&sp->dids_list, dhp);
8967c478bd9Sstevel@tonic-gate }
8977c478bd9Sstevel@tonic-gate 
8987c478bd9Sstevel@tonic-gate dt_ident_t *
dt_ident_create(const char * name,ushort_t kind,ushort_t flags,uint_t id,dtrace_attribute_t attr,uint_t vers,const dt_idops_t * ops,void * iarg,ulong_t gen)8997c478bd9Sstevel@tonic-gate dt_ident_create(const char *name, ushort_t kind, ushort_t flags, uint_t id,
9007c478bd9Sstevel@tonic-gate     dtrace_attribute_t attr, uint_t vers,
9017c478bd9Sstevel@tonic-gate     const dt_idops_t *ops, void *iarg, ulong_t gen)
9027c478bd9Sstevel@tonic-gate {
9037c478bd9Sstevel@tonic-gate 	dt_ident_t *idp;
9047c478bd9Sstevel@tonic-gate 	char *s = NULL;
9057c478bd9Sstevel@tonic-gate 
9067c478bd9Sstevel@tonic-gate 	if ((name != NULL && (s = strdup(name)) == NULL) ||
9077c478bd9Sstevel@tonic-gate 	    (idp = malloc(sizeof (dt_ident_t))) == NULL) {
9087c478bd9Sstevel@tonic-gate 		free(s);
9097c478bd9Sstevel@tonic-gate 		return (NULL);
9107c478bd9Sstevel@tonic-gate 	}
9117c478bd9Sstevel@tonic-gate 
9127c478bd9Sstevel@tonic-gate 	idp->di_name = s;
9137c478bd9Sstevel@tonic-gate 	idp->di_kind = kind;
9147c478bd9Sstevel@tonic-gate 	idp->di_flags = flags;
9157c478bd9Sstevel@tonic-gate 	idp->di_id = id;
9167c478bd9Sstevel@tonic-gate 	idp->di_attr = attr;
9177c478bd9Sstevel@tonic-gate 	idp->di_vers = vers;
9187c478bd9Sstevel@tonic-gate 	idp->di_ops = ops;
9197c478bd9Sstevel@tonic-gate 	idp->di_iarg = iarg;
9207c478bd9Sstevel@tonic-gate 	idp->di_data = NULL;
9217c478bd9Sstevel@tonic-gate 	idp->di_ctfp = NULL;
9227c478bd9Sstevel@tonic-gate 	idp->di_type = CTF_ERR;
9237c478bd9Sstevel@tonic-gate 	idp->di_next = NULL;
9247c478bd9Sstevel@tonic-gate 	idp->di_gen = gen;
9257c478bd9Sstevel@tonic-gate 	idp->di_lineno = yylineno;
9267c478bd9Sstevel@tonic-gate 
9277c478bd9Sstevel@tonic-gate 	return (idp);
9287c478bd9Sstevel@tonic-gate }
9297c478bd9Sstevel@tonic-gate 
9307c478bd9Sstevel@tonic-gate /*
9317c478bd9Sstevel@tonic-gate  * Destroy an individual identifier.  This code must be kept in sync with the
9327c478bd9Sstevel@tonic-gate  * dt_idhash_destroy() function below, which separates out the call to di_dtor.
9337c478bd9Sstevel@tonic-gate  */
9347c478bd9Sstevel@tonic-gate void
dt_ident_destroy(dt_ident_t * idp)9357c478bd9Sstevel@tonic-gate dt_ident_destroy(dt_ident_t *idp)
9367c478bd9Sstevel@tonic-gate {
9377c478bd9Sstevel@tonic-gate 	idp->di_ops->di_dtor(idp);
9387c478bd9Sstevel@tonic-gate 	free(idp->di_name);
9397c478bd9Sstevel@tonic-gate 	free(idp);
9407c478bd9Sstevel@tonic-gate }
9417c478bd9Sstevel@tonic-gate 
9427c478bd9Sstevel@tonic-gate void
dt_ident_morph(dt_ident_t * idp,ushort_t kind,const dt_idops_t * ops,void * iarg)9437c478bd9Sstevel@tonic-gate dt_ident_morph(dt_ident_t *idp, ushort_t kind,
9447c478bd9Sstevel@tonic-gate     const dt_idops_t *ops, void *iarg)
9457c478bd9Sstevel@tonic-gate {
9467c478bd9Sstevel@tonic-gate 	idp->di_ops->di_dtor(idp);
9477c478bd9Sstevel@tonic-gate 	idp->di_kind = kind;
9487c478bd9Sstevel@tonic-gate 	idp->di_ops = ops;
9497c478bd9Sstevel@tonic-gate 	idp->di_iarg = iarg;
9507c478bd9Sstevel@tonic-gate 	idp->di_data = NULL;
9517c478bd9Sstevel@tonic-gate }
9527c478bd9Sstevel@tonic-gate 
9537c478bd9Sstevel@tonic-gate dtrace_attribute_t
dt_ident_cook(dt_node_t * dnp,dt_ident_t * idp,dt_node_t ** pargp)9547c478bd9Sstevel@tonic-gate dt_ident_cook(dt_node_t *dnp, dt_ident_t *idp, dt_node_t **pargp)
9557c478bd9Sstevel@tonic-gate {
9567c478bd9Sstevel@tonic-gate 	dtrace_attribute_t attr;
9577c478bd9Sstevel@tonic-gate 	dt_node_t *args, *argp;
9587c478bd9Sstevel@tonic-gate 	int argc = 0;
9597c478bd9Sstevel@tonic-gate 
9607c478bd9Sstevel@tonic-gate 	attr = dt_node_list_cook(pargp, DT_IDFLG_REF);
9617c478bd9Sstevel@tonic-gate 	args = pargp ? *pargp : NULL;
9627c478bd9Sstevel@tonic-gate 
9637c478bd9Sstevel@tonic-gate 	for (argp = args; argp != NULL; argp = argp->dn_list)
9647c478bd9Sstevel@tonic-gate 		argc++;
9657c478bd9Sstevel@tonic-gate 
9667c478bd9Sstevel@tonic-gate 	idp->di_ops->di_cook(dnp, idp, argc, args);
9677c478bd9Sstevel@tonic-gate 
9687c478bd9Sstevel@tonic-gate 	if (idp->di_flags & DT_IDFLG_USER)
9697c478bd9Sstevel@tonic-gate 		dnp->dn_flags |= DT_NF_USERLAND;
9707c478bd9Sstevel@tonic-gate 
9717c478bd9Sstevel@tonic-gate 	return (dt_attr_min(attr, idp->di_attr));
9727c478bd9Sstevel@tonic-gate }
9737c478bd9Sstevel@tonic-gate 
9747c478bd9Sstevel@tonic-gate void
dt_ident_type_assign(dt_ident_t * idp,ctf_file_t * fp,ctf_id_t type)9757c478bd9Sstevel@tonic-gate dt_ident_type_assign(dt_ident_t *idp, ctf_file_t *fp, ctf_id_t type)
9767c478bd9Sstevel@tonic-gate {
9777c478bd9Sstevel@tonic-gate 	idp->di_ctfp = fp;
9787c478bd9Sstevel@tonic-gate 	idp->di_type = type;
9797c478bd9Sstevel@tonic-gate }
9807c478bd9Sstevel@tonic-gate 
9817c478bd9Sstevel@tonic-gate dt_ident_t *
dt_ident_resolve(dt_ident_t * idp)9827c478bd9Sstevel@tonic-gate dt_ident_resolve(dt_ident_t *idp)
9837c478bd9Sstevel@tonic-gate {
9847c478bd9Sstevel@tonic-gate 	while (idp->di_flags & DT_IDFLG_INLINE) {
9857c478bd9Sstevel@tonic-gate 		const dt_node_t *dnp = ((dt_idnode_t *)idp->di_iarg)->din_root;
9867c478bd9Sstevel@tonic-gate 
9871a7c1b72Smws 		if (dnp == NULL)
9881a7c1b72Smws 			break; /* can't resolve any further yet */
9891a7c1b72Smws 
9907c478bd9Sstevel@tonic-gate 		switch (dnp->dn_kind) {
9917c478bd9Sstevel@tonic-gate 		case DT_NODE_VAR:
9927c478bd9Sstevel@tonic-gate 		case DT_NODE_SYM:
9937c478bd9Sstevel@tonic-gate 		case DT_NODE_FUNC:
9947c478bd9Sstevel@tonic-gate 		case DT_NODE_AGG:
9957c478bd9Sstevel@tonic-gate 		case DT_NODE_INLINE:
9967c478bd9Sstevel@tonic-gate 		case DT_NODE_PROBE:
9977c478bd9Sstevel@tonic-gate 			idp = dnp->dn_ident;
9987c478bd9Sstevel@tonic-gate 			continue;
9997c478bd9Sstevel@tonic-gate 		}
10007c478bd9Sstevel@tonic-gate 
10017c478bd9Sstevel@tonic-gate 		if (dt_node_is_dynamic(dnp))
10027c478bd9Sstevel@tonic-gate 			idp = dnp->dn_ident;
10037c478bd9Sstevel@tonic-gate 		else
10047c478bd9Sstevel@tonic-gate 			break;
10057c478bd9Sstevel@tonic-gate 	}
10067c478bd9Sstevel@tonic-gate 
10077c478bd9Sstevel@tonic-gate 	return (idp);
10087c478bd9Sstevel@tonic-gate }
10097c478bd9Sstevel@tonic-gate 
10107c478bd9Sstevel@tonic-gate size_t
dt_ident_size(dt_ident_t * idp)10117c478bd9Sstevel@tonic-gate dt_ident_size(dt_ident_t *idp)
10127c478bd9Sstevel@tonic-gate {
10137c478bd9Sstevel@tonic-gate 	idp = dt_ident_resolve(idp);
10147c478bd9Sstevel@tonic-gate 	return (idp->di_ops->di_size(idp));
10157c478bd9Sstevel@tonic-gate }
10167c478bd9Sstevel@tonic-gate 
10177c478bd9Sstevel@tonic-gate int
dt_ident_unref(const dt_ident_t * idp)10187c478bd9Sstevel@tonic-gate dt_ident_unref(const dt_ident_t *idp)
10197c478bd9Sstevel@tonic-gate {
10207c478bd9Sstevel@tonic-gate 	return (idp->di_gen == yypcb->pcb_hdl->dt_gen &&
10217c478bd9Sstevel@tonic-gate 	    (idp->di_flags & (DT_IDFLG_REF|DT_IDFLG_MOD|DT_IDFLG_DECL)) == 0);
10227c478bd9Sstevel@tonic-gate }
10237c478bd9Sstevel@tonic-gate 
10247c478bd9Sstevel@tonic-gate const char *
dt_idkind_name(uint_t kind)10257c478bd9Sstevel@tonic-gate dt_idkind_name(uint_t kind)
10267c478bd9Sstevel@tonic-gate {
10277c478bd9Sstevel@tonic-gate 	switch (kind) {
10287c478bd9Sstevel@tonic-gate 	case DT_IDENT_ARRAY:	return ("associative array");
10297c478bd9Sstevel@tonic-gate 	case DT_IDENT_SCALAR:	return ("scalar");
10307c478bd9Sstevel@tonic-gate 	case DT_IDENT_PTR:	return ("pointer");
10317c478bd9Sstevel@tonic-gate 	case DT_IDENT_FUNC:	return ("function");
10327c478bd9Sstevel@tonic-gate 	case DT_IDENT_AGG:	return ("aggregation");
10337c478bd9Sstevel@tonic-gate 	case DT_IDENT_AGGFUNC:	return ("aggregating function");
10347c478bd9Sstevel@tonic-gate 	case DT_IDENT_ACTFUNC:	return ("tracing function");
10357c478bd9Sstevel@tonic-gate 	case DT_IDENT_XLSOU:	return ("translated data");
10367c478bd9Sstevel@tonic-gate 	case DT_IDENT_XLPTR:	return ("pointer to translated data");
10377c478bd9Sstevel@tonic-gate 	case DT_IDENT_SYMBOL:	return ("external symbol reference");
10387c478bd9Sstevel@tonic-gate 	case DT_IDENT_ENUM:	return ("enumerator");
10397c478bd9Sstevel@tonic-gate 	case DT_IDENT_PRAGAT:	return ("#pragma attributes");
10407c478bd9Sstevel@tonic-gate 	case DT_IDENT_PRAGBN:	return ("#pragma binding");
10417c478bd9Sstevel@tonic-gate 	case DT_IDENT_PROBE:	return ("probe definition");
10427c478bd9Sstevel@tonic-gate 	default:		return ("<?>");
10437c478bd9Sstevel@tonic-gate 	}
10447c478bd9Sstevel@tonic-gate }
1045