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