xref: /onnv-gate/usr/src/lib/libdtrace/common/dt_cc.c (revision 1017:f5a6f019be79)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
230Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate  * DTrace D Language Compiler
310Sstevel@tonic-gate  *
320Sstevel@tonic-gate  * The code in this source file implements the main engine for the D language
330Sstevel@tonic-gate  * compiler.  The driver routine for the compiler is dt_compile(), below.  The
340Sstevel@tonic-gate  * compiler operates on either stdio FILEs or in-memory strings as its input
350Sstevel@tonic-gate  * and can produce either dtrace_prog_t structures from a D program or a single
360Sstevel@tonic-gate  * dtrace_difo_t structure from a D expression.  Multiple entry points are
370Sstevel@tonic-gate  * provided as wrappers around dt_compile() for the various input/output pairs.
380Sstevel@tonic-gate  * The compiler itself is implemented across the following source files:
390Sstevel@tonic-gate  *
400Sstevel@tonic-gate  * dt_lex.l - lex scanner
410Sstevel@tonic-gate  * dt_grammar.y - yacc grammar
420Sstevel@tonic-gate  * dt_parser.c - parse tree creation and semantic checking
430Sstevel@tonic-gate  * dt_decl.c - declaration stack processing
440Sstevel@tonic-gate  * dt_xlator.c - D translator lookup and creation
450Sstevel@tonic-gate  * dt_ident.c - identifier and symbol table routines
460Sstevel@tonic-gate  * dt_pragma.c - #pragma processing and D pragmas
470Sstevel@tonic-gate  * dt_printf.c - D printf() and printa() argument checking and processing
480Sstevel@tonic-gate  * dt_cc.c - compiler driver and dtrace_prog_t construction
490Sstevel@tonic-gate  * dt_cg.c - DIF code generator
500Sstevel@tonic-gate  * dt_as.c - DIF assembler
510Sstevel@tonic-gate  * dt_dof.c - dtrace_prog_t -> DOF conversion
520Sstevel@tonic-gate  *
530Sstevel@tonic-gate  * Several other source files provide collections of utility routines used by
540Sstevel@tonic-gate  * these major files.  The compiler itself is implemented in multiple passes:
550Sstevel@tonic-gate  *
560Sstevel@tonic-gate  * (1) The input program is scanned and parsed by dt_lex.l and dt_grammar.y
570Sstevel@tonic-gate  *     and parse tree nodes are constructed using the routines in dt_parser.c.
580Sstevel@tonic-gate  *     This node construction pass is described further in dt_parser.c.
590Sstevel@tonic-gate  *
600Sstevel@tonic-gate  * (2) The parse tree is "cooked" by assigning each clause a context (see the
610Sstevel@tonic-gate  *     routine dt_setcontext(), below) based on its probe description and then
620Sstevel@tonic-gate  *     recursively descending the tree performing semantic checking.  The cook
630Sstevel@tonic-gate  *     routines are also implemented in dt_parser.c and described there.
640Sstevel@tonic-gate  *
650Sstevel@tonic-gate  * (3) For actions that are DIF expression statements, the DIF code generator
660Sstevel@tonic-gate  *     and assembler are invoked to create a finished DIFO for the statement.
670Sstevel@tonic-gate  *
680Sstevel@tonic-gate  * (4) The dtrace_prog_t data structures for the program clauses and actions
690Sstevel@tonic-gate  *     are built, containing pointers to any DIFOs created in step (3).
700Sstevel@tonic-gate  *
710Sstevel@tonic-gate  * (5) The caller invokes a routine in dt_dof.c to convert the finished program
720Sstevel@tonic-gate  *     into DOF format for use in anonymous tracing or enabling in the kernel.
730Sstevel@tonic-gate  *
740Sstevel@tonic-gate  * In the implementation, steps 2-4 are intertwined in that they are performed
750Sstevel@tonic-gate  * in order for each clause as part of a loop that executes over the clauses.
760Sstevel@tonic-gate  *
770Sstevel@tonic-gate  * The D compiler currently implements nearly no optimization.  The compiler
780Sstevel@tonic-gate  * implements integer constant folding as part of pass (1), and a set of very
790Sstevel@tonic-gate  * simple peephole optimizations as part of pass (3).  As with any C compiler,
800Sstevel@tonic-gate  * a large number of optimizations are possible on both the intermediate data
810Sstevel@tonic-gate  * structures and the generated DIF code.  These possibilities should be
820Sstevel@tonic-gate  * investigated in the context of whether they will have any substantive effect
830Sstevel@tonic-gate  * on the overall DTrace probe effect before they are undertaken.
840Sstevel@tonic-gate  */
850Sstevel@tonic-gate 
860Sstevel@tonic-gate #include <sys/types.h>
870Sstevel@tonic-gate #include <sys/wait.h>
880Sstevel@tonic-gate 
890Sstevel@tonic-gate #include <assert.h>
900Sstevel@tonic-gate #include <strings.h>
910Sstevel@tonic-gate #include <signal.h>
920Sstevel@tonic-gate #include <unistd.h>
930Sstevel@tonic-gate #include <stdlib.h>
940Sstevel@tonic-gate #include <stdio.h>
950Sstevel@tonic-gate #include <errno.h>
960Sstevel@tonic-gate #include <ucontext.h>
970Sstevel@tonic-gate #include <limits.h>
980Sstevel@tonic-gate #include <ctype.h>
990Sstevel@tonic-gate #include <dirent.h>
1000Sstevel@tonic-gate #include <dt_module.h>
101265Smws #include <dt_program.h>
1020Sstevel@tonic-gate #include <dt_provider.h>
1030Sstevel@tonic-gate #include <dt_printf.h>
1040Sstevel@tonic-gate #include <dt_pid.h>
1050Sstevel@tonic-gate #include <dt_grammar.h>
1060Sstevel@tonic-gate #include <dt_ident.h>
1070Sstevel@tonic-gate #include <dt_string.h>
1080Sstevel@tonic-gate #include <dt_impl.h>
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate static const dtrace_diftype_t dt_void_rtype = {
1110Sstevel@tonic-gate 	DIF_TYPE_CTF, CTF_K_INTEGER, 0, 0, 0
1120Sstevel@tonic-gate };
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate static const dtrace_diftype_t dt_int_rtype = {
1150Sstevel@tonic-gate 	DIF_TYPE_CTF, CTF_K_INTEGER, 0, 0, sizeof (uint64_t)
1160Sstevel@tonic-gate };
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate /*ARGSUSED*/
1190Sstevel@tonic-gate static int
1200Sstevel@tonic-gate dt_idreset(dt_idhash_t *dhp, dt_ident_t *idp, void *ignored)
1210Sstevel@tonic-gate {
1220Sstevel@tonic-gate 	idp->di_flags &= ~(DT_IDFLG_REF | DT_IDFLG_MOD |
1230Sstevel@tonic-gate 	    DT_IDFLG_DIFR | DT_IDFLG_DIFW);
1240Sstevel@tonic-gate 	return (0);
1250Sstevel@tonic-gate }
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate /*ARGSUSED*/
1280Sstevel@tonic-gate static int
1290Sstevel@tonic-gate dt_idpragma(dt_idhash_t *dhp, dt_ident_t *idp, void *ignored)
1300Sstevel@tonic-gate {
1310Sstevel@tonic-gate 	yylineno = idp->di_lineno;
1320Sstevel@tonic-gate 	xyerror(D_PRAGMA_UNUSED, "unused #pragma %s\n", (char *)idp->di_iarg);
1330Sstevel@tonic-gate 	return (0);
1340Sstevel@tonic-gate }
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate static dtrace_stmtdesc_t *
1370Sstevel@tonic-gate dt_stmt_create(dtrace_hdl_t *dtp, dtrace_ecbdesc_t *edp,
1380Sstevel@tonic-gate     dtrace_attribute_t descattr, dtrace_attribute_t stmtattr)
1390Sstevel@tonic-gate {
1400Sstevel@tonic-gate 	dtrace_stmtdesc_t *sdp = dtrace_stmt_create(dtp, edp);
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate 	if (sdp == NULL)
1430Sstevel@tonic-gate 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate 	assert(yypcb->pcb_stmt == NULL);
1460Sstevel@tonic-gate 	yypcb->pcb_stmt = sdp;
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate 	sdp->dtsd_descattr = descattr;
1490Sstevel@tonic-gate 	sdp->dtsd_stmtattr = stmtattr;
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate 	return (sdp);
1520Sstevel@tonic-gate }
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate static dtrace_actdesc_t *
1550Sstevel@tonic-gate dt_stmt_action(dtrace_hdl_t *dtp, dtrace_stmtdesc_t *sdp)
1560Sstevel@tonic-gate {
1570Sstevel@tonic-gate 	dtrace_actdesc_t *new;
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate 	if ((new = dtrace_stmt_action(dtp, sdp)) == NULL)
1600Sstevel@tonic-gate 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate 	return (new);
1630Sstevel@tonic-gate }
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate /*
1660Sstevel@tonic-gate  * Utility function to determine if a given action description is destructive.
1670Sstevel@tonic-gate  * The dtdo_destructive bit is set for us by the DIF assembler (see dt_as.c).
1680Sstevel@tonic-gate  */
1690Sstevel@tonic-gate static int
1700Sstevel@tonic-gate dt_action_destructive(const dtrace_actdesc_t *ap)
1710Sstevel@tonic-gate {
1720Sstevel@tonic-gate 	return (DTRACEACT_ISDESTRUCTIVE(ap->dtad_kind) || (ap->dtad_kind ==
1730Sstevel@tonic-gate 	    DTRACEACT_DIFEXPR && ap->dtad_difo->dtdo_destructive));
1740Sstevel@tonic-gate }
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate static void
1770Sstevel@tonic-gate dt_stmt_append(dtrace_stmtdesc_t *sdp, const dt_node_t *dnp)
1780Sstevel@tonic-gate {
1790Sstevel@tonic-gate 	dtrace_ecbdesc_t *edp = sdp->dtsd_ecbdesc;
1800Sstevel@tonic-gate 	dtrace_actdesc_t *ap, *tap;
1810Sstevel@tonic-gate 	int commit = 0;
1820Sstevel@tonic-gate 	int speculate = 0;
1830Sstevel@tonic-gate 	int datarec = 0;
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate 	/*
1860Sstevel@tonic-gate 	 * Make sure that the new statement jibes with the rest of the ECB.
1870Sstevel@tonic-gate 	 */
1880Sstevel@tonic-gate 	for (ap = edp->dted_action; ap != NULL; ap = ap->dtad_next) {
1890Sstevel@tonic-gate 		if (ap->dtad_kind == DTRACEACT_COMMIT) {
1900Sstevel@tonic-gate 			if (commit) {
1910Sstevel@tonic-gate 				dnerror(dnp, D_COMM_COMM, "commit( ) may "
1920Sstevel@tonic-gate 				    "not follow commit( )\n");
1930Sstevel@tonic-gate 			}
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate 			if (datarec) {
1960Sstevel@tonic-gate 				dnerror(dnp, D_COMM_DREC, "commit( ) may "
1970Sstevel@tonic-gate 				    "not follow data-recording action(s)\n");
1980Sstevel@tonic-gate 			}
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate 			for (tap = ap; tap != NULL; tap = tap->dtad_next) {
2010Sstevel@tonic-gate 				if (!DTRACEACT_ISAGG(tap->dtad_kind))
2020Sstevel@tonic-gate 					continue;
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate 				dnerror(dnp, D_AGG_COMM, "aggregating actions "
2050Sstevel@tonic-gate 				    "may not follow commit( )\n");
2060Sstevel@tonic-gate 			}
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate 			commit = 1;
2090Sstevel@tonic-gate 			continue;
2100Sstevel@tonic-gate 		}
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate 		if (ap->dtad_kind == DTRACEACT_SPECULATE) {
2130Sstevel@tonic-gate 			if (speculate) {
2140Sstevel@tonic-gate 				dnerror(dnp, D_SPEC_SPEC, "speculate( ) may "
2150Sstevel@tonic-gate 				    "not follow speculate( )\n");
2160Sstevel@tonic-gate 			}
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate 			if (commit) {
2190Sstevel@tonic-gate 				dnerror(dnp, D_SPEC_COMM, "speculate( ) may "
2200Sstevel@tonic-gate 				    "not follow commit( )\n");
2210Sstevel@tonic-gate 			}
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate 			if (datarec) {
2240Sstevel@tonic-gate 				dnerror(dnp, D_SPEC_DREC, "speculate( ) may "
2250Sstevel@tonic-gate 				    "not follow data-recording action(s)\n");
2260Sstevel@tonic-gate 			}
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate 			speculate = 1;
2290Sstevel@tonic-gate 			continue;
2300Sstevel@tonic-gate 		}
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate 		if (DTRACEACT_ISAGG(ap->dtad_kind)) {
2330Sstevel@tonic-gate 			if (speculate) {
2340Sstevel@tonic-gate 				dnerror(dnp, D_AGG_SPEC, "aggregating actions "
2350Sstevel@tonic-gate 				    "may not follow speculate( )\n");
2360Sstevel@tonic-gate 			}
2370Sstevel@tonic-gate 
2380Sstevel@tonic-gate 			datarec = 1;
2390Sstevel@tonic-gate 			continue;
2400Sstevel@tonic-gate 		}
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate 		if (speculate) {
2430Sstevel@tonic-gate 			if (dt_action_destructive(ap)) {
2440Sstevel@tonic-gate 				dnerror(dnp, D_ACT_SPEC, "destructive actions "
2450Sstevel@tonic-gate 				    "may not follow speculate( )\n");
2460Sstevel@tonic-gate 			}
2470Sstevel@tonic-gate 
2480Sstevel@tonic-gate 			if (ap->dtad_kind == DTRACEACT_EXIT) {
2490Sstevel@tonic-gate 				dnerror(dnp, D_EXIT_SPEC, "exit( ) may not "
2500Sstevel@tonic-gate 				    "follow speculate( )\n");
2510Sstevel@tonic-gate 			}
2520Sstevel@tonic-gate 		}
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate 		/*
2550Sstevel@tonic-gate 		 * Exclude all non data-recording actions.
2560Sstevel@tonic-gate 		 */
2570Sstevel@tonic-gate 		if (dt_action_destructive(ap) ||
2580Sstevel@tonic-gate 		    ap->dtad_kind == DTRACEACT_DISCARD)
2590Sstevel@tonic-gate 			continue;
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate 		if (ap->dtad_kind == DTRACEACT_DIFEXPR &&
2620Sstevel@tonic-gate 		    ap->dtad_difo->dtdo_rtype.dtdt_kind == DIF_TYPE_CTF &&
2630Sstevel@tonic-gate 		    ap->dtad_difo->dtdo_rtype.dtdt_size == 0)
2640Sstevel@tonic-gate 			continue;
2650Sstevel@tonic-gate 
2660Sstevel@tonic-gate 		if (commit) {
2670Sstevel@tonic-gate 			dnerror(dnp, D_DREC_COMM, "data-recording actions "
2680Sstevel@tonic-gate 			    "may not follow commit( )\n");
2690Sstevel@tonic-gate 		}
2700Sstevel@tonic-gate 
2710Sstevel@tonic-gate 		if (!speculate)
2720Sstevel@tonic-gate 			datarec = 1;
2730Sstevel@tonic-gate 	}
2740Sstevel@tonic-gate 
2750Sstevel@tonic-gate 	if (dtrace_stmt_add(yypcb->pcb_hdl, yypcb->pcb_prog, sdp) != 0)
2760Sstevel@tonic-gate 		longjmp(yypcb->pcb_jmpbuf, dtrace_errno(yypcb->pcb_hdl));
2770Sstevel@tonic-gate 
2780Sstevel@tonic-gate 	if (yypcb->pcb_stmt == sdp)
2790Sstevel@tonic-gate 		yypcb->pcb_stmt = NULL;
2800Sstevel@tonic-gate }
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate /*
2830Sstevel@tonic-gate  * For the first element of an aggregation tuple or for printa(), we create a
2840Sstevel@tonic-gate  * simple DIF program that simply returns the immediate value that is the ID
2850Sstevel@tonic-gate  * of the aggregation itself.  This could be optimized in the future by
2860Sstevel@tonic-gate  * creating a new in-kernel dtad_kind that just returns an integer.
2870Sstevel@tonic-gate  */
2880Sstevel@tonic-gate static void
2890Sstevel@tonic-gate dt_action_difconst(dtrace_actdesc_t *ap, uint_t id, dtrace_actkind_t kind)
2900Sstevel@tonic-gate {
291265Smws 	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
292265Smws 	dtrace_difo_t *dp = dt_zalloc(dtp, sizeof (dtrace_difo_t));
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate 	if (dp == NULL)
2950Sstevel@tonic-gate 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2960Sstevel@tonic-gate 
297265Smws 	dp->dtdo_buf = dt_alloc(dtp, sizeof (dif_instr_t) * 2);
298265Smws 	dp->dtdo_inttab = dt_alloc(dtp, sizeof (uint64_t));
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate 	if (dp->dtdo_buf == NULL || dp->dtdo_inttab == NULL) {
301265Smws 		dt_difo_free(dtp, dp);
3020Sstevel@tonic-gate 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
3030Sstevel@tonic-gate 	}
3040Sstevel@tonic-gate 
3050Sstevel@tonic-gate 	dp->dtdo_buf[0] = DIF_INSTR_SETX(0, 1); /* setx	DIF_INTEGER[0], %r1 */
3060Sstevel@tonic-gate 	dp->dtdo_buf[1] = DIF_INSTR_RET(1);	/* ret	%r1 */
3070Sstevel@tonic-gate 	dp->dtdo_len = 2;
3080Sstevel@tonic-gate 	dp->dtdo_inttab[0] = id;
3090Sstevel@tonic-gate 	dp->dtdo_intlen = 1;
3100Sstevel@tonic-gate 	dp->dtdo_rtype = dt_int_rtype;
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate 	ap->dtad_difo = dp;
3130Sstevel@tonic-gate 	ap->dtad_kind = kind;
3140Sstevel@tonic-gate }
3150Sstevel@tonic-gate 
3160Sstevel@tonic-gate static void
3170Sstevel@tonic-gate dt_action_clear(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
3180Sstevel@tonic-gate {
3190Sstevel@tonic-gate 	dt_ident_t *aid;
3200Sstevel@tonic-gate 	dtrace_actdesc_t *ap;
3210Sstevel@tonic-gate 	dt_node_t *anp;
3220Sstevel@tonic-gate 
3230Sstevel@tonic-gate 	char n[DT_TYPE_NAMELEN];
3240Sstevel@tonic-gate 	int argc = 0;
3250Sstevel@tonic-gate 
3260Sstevel@tonic-gate 	for (anp = dnp->dn_args; anp != NULL; anp = anp->dn_list)
3270Sstevel@tonic-gate 		argc++; /* count up arguments for error messages below */
3280Sstevel@tonic-gate 
3290Sstevel@tonic-gate 	if (argc != 1) {
3300Sstevel@tonic-gate 		dnerror(dnp, D_CLEAR_PROTO,
3310Sstevel@tonic-gate 		    "%s( ) prototype mismatch: %d args passed, 1 expected\n",
3320Sstevel@tonic-gate 		    dnp->dn_ident->di_name, argc);
3330Sstevel@tonic-gate 	}
3340Sstevel@tonic-gate 
3350Sstevel@tonic-gate 	anp = dnp->dn_args;
3360Sstevel@tonic-gate 	assert(anp != NULL);
3370Sstevel@tonic-gate 
3380Sstevel@tonic-gate 	if (anp->dn_kind != DT_NODE_AGG) {
3390Sstevel@tonic-gate 		dnerror(dnp, D_CLEAR_AGGARG,
3400Sstevel@tonic-gate 		    "%s( ) argument #1 is incompatible with prototype:\n"
3410Sstevel@tonic-gate 		    "\tprototype: aggregation\n\t argument: %s\n",
3420Sstevel@tonic-gate 		    dnp->dn_ident->di_name,
3430Sstevel@tonic-gate 		    dt_node_type_name(anp, n, sizeof (n)));
3440Sstevel@tonic-gate 	}
3450Sstevel@tonic-gate 
3460Sstevel@tonic-gate 	aid = anp->dn_ident;
3470Sstevel@tonic-gate 
3480Sstevel@tonic-gate 	if (aid->di_gen == dtp->dt_gen && !(aid->di_flags & DT_IDFLG_MOD)) {
3490Sstevel@tonic-gate 		dnerror(dnp, D_CLEAR_AGGBAD,
3500Sstevel@tonic-gate 		    "undefined aggregation: @%s\n", aid->di_name);
3510Sstevel@tonic-gate 	}
3520Sstevel@tonic-gate 
3530Sstevel@tonic-gate 	ap = dt_stmt_action(dtp, sdp);
3540Sstevel@tonic-gate 	dt_action_difconst(ap, anp->dn_ident->di_id, DTRACEACT_LIBACT);
3550Sstevel@tonic-gate 	ap->dtad_arg = DT_ACT_CLEAR;
3560Sstevel@tonic-gate }
3570Sstevel@tonic-gate 
3580Sstevel@tonic-gate static void
3590Sstevel@tonic-gate dt_action_normalize(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
3600Sstevel@tonic-gate {
3610Sstevel@tonic-gate 	dt_ident_t *aid;
3620Sstevel@tonic-gate 	dtrace_actdesc_t *ap;
3630Sstevel@tonic-gate 	dt_node_t *anp, *normal;
3640Sstevel@tonic-gate 	int denormal = (strcmp(dnp->dn_ident->di_name, "denormalize") == 0);
3650Sstevel@tonic-gate 
3660Sstevel@tonic-gate 	char n[DT_TYPE_NAMELEN];
3670Sstevel@tonic-gate 	int argc = 0;
3680Sstevel@tonic-gate 
3690Sstevel@tonic-gate 	for (anp = dnp->dn_args; anp != NULL; anp = anp->dn_list)
3700Sstevel@tonic-gate 		argc++; /* count up arguments for error messages below */
3710Sstevel@tonic-gate 
3720Sstevel@tonic-gate 	if ((denormal && argc != 1) || (!denormal && argc != 2)) {
3730Sstevel@tonic-gate 		dnerror(dnp, D_NORMALIZE_PROTO,
3740Sstevel@tonic-gate 		    "%s( ) prototype mismatch: %d args passed, %d expected\n",
3750Sstevel@tonic-gate 		    dnp->dn_ident->di_name, argc, denormal ? 1 : 2);
3760Sstevel@tonic-gate 	}
3770Sstevel@tonic-gate 
3780Sstevel@tonic-gate 	anp = dnp->dn_args;
3790Sstevel@tonic-gate 	assert(anp != NULL);
3800Sstevel@tonic-gate 
3810Sstevel@tonic-gate 	if (anp->dn_kind != DT_NODE_AGG) {
3820Sstevel@tonic-gate 		dnerror(dnp, D_NORMALIZE_AGGARG,
3830Sstevel@tonic-gate 		    "%s( ) argument #1 is incompatible with prototype:\n"
3840Sstevel@tonic-gate 		    "\tprototype: aggregation\n\t argument: %s\n",
3850Sstevel@tonic-gate 		    dnp->dn_ident->di_name,
3860Sstevel@tonic-gate 		    dt_node_type_name(anp, n, sizeof (n)));
3870Sstevel@tonic-gate 	}
3880Sstevel@tonic-gate 
3890Sstevel@tonic-gate 	if ((normal = anp->dn_list) != NULL && !dt_node_is_scalar(normal)) {
3900Sstevel@tonic-gate 		dnerror(dnp, D_NORMALIZE_SCALAR,
3910Sstevel@tonic-gate 		    "%s( ) argument #2 must be of scalar type\n",
3920Sstevel@tonic-gate 		    dnp->dn_ident->di_name);
3930Sstevel@tonic-gate 	}
3940Sstevel@tonic-gate 
3950Sstevel@tonic-gate 	aid = anp->dn_ident;
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate 	if (aid->di_gen == dtp->dt_gen && !(aid->di_flags & DT_IDFLG_MOD)) {
3980Sstevel@tonic-gate 		dnerror(dnp, D_NORMALIZE_AGGBAD,
3990Sstevel@tonic-gate 		    "undefined aggregation: @%s\n", aid->di_name);
4000Sstevel@tonic-gate 	}
4010Sstevel@tonic-gate 
4020Sstevel@tonic-gate 	ap = dt_stmt_action(dtp, sdp);
4030Sstevel@tonic-gate 	dt_action_difconst(ap, anp->dn_ident->di_id, DTRACEACT_LIBACT);
4040Sstevel@tonic-gate 
4050Sstevel@tonic-gate 	if (denormal) {
4060Sstevel@tonic-gate 		ap->dtad_arg = DT_ACT_DENORMALIZE;
4070Sstevel@tonic-gate 		return;
4080Sstevel@tonic-gate 	}
4090Sstevel@tonic-gate 
4100Sstevel@tonic-gate 	ap->dtad_arg = DT_ACT_NORMALIZE;
4110Sstevel@tonic-gate 
4120Sstevel@tonic-gate 	assert(normal != NULL);
4130Sstevel@tonic-gate 	ap = dt_stmt_action(dtp, sdp);
4140Sstevel@tonic-gate 	dt_cg(yypcb, normal);
4150Sstevel@tonic-gate 
4160Sstevel@tonic-gate 	ap->dtad_difo = dt_as(yypcb);
4170Sstevel@tonic-gate 	ap->dtad_kind = DTRACEACT_LIBACT;
4180Sstevel@tonic-gate 	ap->dtad_arg = DT_ACT_NORMALIZE;
4190Sstevel@tonic-gate }
4200Sstevel@tonic-gate 
4210Sstevel@tonic-gate static void
4220Sstevel@tonic-gate dt_action_trunc(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
4230Sstevel@tonic-gate {
4240Sstevel@tonic-gate 	dt_ident_t *aid;
4250Sstevel@tonic-gate 	dtrace_actdesc_t *ap;
4260Sstevel@tonic-gate 	dt_node_t *anp, *trunc;
4270Sstevel@tonic-gate 
4280Sstevel@tonic-gate 	char n[DT_TYPE_NAMELEN];
4290Sstevel@tonic-gate 	int argc = 0;
4300Sstevel@tonic-gate 
4310Sstevel@tonic-gate 	for (anp = dnp->dn_args; anp != NULL; anp = anp->dn_list)
4320Sstevel@tonic-gate 		argc++; /* count up arguments for error messages below */
4330Sstevel@tonic-gate 
4340Sstevel@tonic-gate 	if (argc > 2 || argc < 1) {
4350Sstevel@tonic-gate 		dnerror(dnp, D_TRUNC_PROTO,
4360Sstevel@tonic-gate 		    "%s( ) prototype mismatch: %d args passed, %s expected\n",
4370Sstevel@tonic-gate 		    dnp->dn_ident->di_name, argc,
4380Sstevel@tonic-gate 		    argc < 1 ? "at least 1" : "no more than 2");
4390Sstevel@tonic-gate 	}
4400Sstevel@tonic-gate 
4410Sstevel@tonic-gate 	anp = dnp->dn_args;
4420Sstevel@tonic-gate 	assert(anp != NULL);
4430Sstevel@tonic-gate 	trunc = anp->dn_list;
4440Sstevel@tonic-gate 
4450Sstevel@tonic-gate 	if (anp->dn_kind != DT_NODE_AGG) {
4460Sstevel@tonic-gate 		dnerror(dnp, D_TRUNC_AGGARG,
4470Sstevel@tonic-gate 		    "%s( ) argument #1 is incompatible with prototype:\n"
4480Sstevel@tonic-gate 		    "\tprototype: aggregation\n\t argument: %s\n",
4490Sstevel@tonic-gate 		    dnp->dn_ident->di_name,
4500Sstevel@tonic-gate 		    dt_node_type_name(anp, n, sizeof (n)));
4510Sstevel@tonic-gate 	}
4520Sstevel@tonic-gate 
4530Sstevel@tonic-gate 	if (argc == 2) {
4540Sstevel@tonic-gate 		assert(trunc != NULL);
4550Sstevel@tonic-gate 		if (!dt_node_is_scalar(trunc)) {
4560Sstevel@tonic-gate 			dnerror(dnp, D_TRUNC_SCALAR,
4570Sstevel@tonic-gate 			    "%s( ) argument #2 must be of scalar type\n",
4580Sstevel@tonic-gate 			    dnp->dn_ident->di_name);
4590Sstevel@tonic-gate 		}
4600Sstevel@tonic-gate 	}
4610Sstevel@tonic-gate 
4620Sstevel@tonic-gate 	aid = anp->dn_ident;
4630Sstevel@tonic-gate 
4640Sstevel@tonic-gate 	if (aid->di_gen == dtp->dt_gen && !(aid->di_flags & DT_IDFLG_MOD)) {
4650Sstevel@tonic-gate 		dnerror(dnp, D_TRUNC_AGGBAD,
4660Sstevel@tonic-gate 		    "undefined aggregation: @%s\n", aid->di_name);
4670Sstevel@tonic-gate 	}
4680Sstevel@tonic-gate 
4690Sstevel@tonic-gate 	ap = dt_stmt_action(dtp, sdp);
4700Sstevel@tonic-gate 	dt_action_difconst(ap, anp->dn_ident->di_id, DTRACEACT_LIBACT);
4710Sstevel@tonic-gate 	ap->dtad_arg = DT_ACT_TRUNC;
4720Sstevel@tonic-gate 
4730Sstevel@tonic-gate 	ap = dt_stmt_action(dtp, sdp);
4740Sstevel@tonic-gate 
4750Sstevel@tonic-gate 	if (argc == 1) {
4760Sstevel@tonic-gate 		dt_action_difconst(ap, 0, DTRACEACT_LIBACT);
4770Sstevel@tonic-gate 	} else {
4780Sstevel@tonic-gate 		assert(trunc != NULL);
4790Sstevel@tonic-gate 		dt_cg(yypcb, trunc);
4800Sstevel@tonic-gate 		ap->dtad_difo = dt_as(yypcb);
4810Sstevel@tonic-gate 		ap->dtad_kind = DTRACEACT_LIBACT;
4820Sstevel@tonic-gate 	}
4830Sstevel@tonic-gate 
4840Sstevel@tonic-gate 	ap->dtad_arg = DT_ACT_TRUNC;
4850Sstevel@tonic-gate }
4860Sstevel@tonic-gate 
4870Sstevel@tonic-gate static void
4880Sstevel@tonic-gate dt_action_printa(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
4890Sstevel@tonic-gate {
4900Sstevel@tonic-gate 	dt_ident_t *aid, *fid;
4910Sstevel@tonic-gate 	dtrace_actdesc_t *ap;
4920Sstevel@tonic-gate 	const char *format;
493*1017Sbmc 	dt_node_t *anp, *proto = NULL;
4940Sstevel@tonic-gate 
4950Sstevel@tonic-gate 	char n[DT_TYPE_NAMELEN];
4960Sstevel@tonic-gate 	int argc = 0, argr = 0;
4970Sstevel@tonic-gate 
4980Sstevel@tonic-gate 	for (anp = dnp->dn_args; anp != NULL; anp = anp->dn_list)
4990Sstevel@tonic-gate 		argc++; /* count up arguments for error messages below */
5000Sstevel@tonic-gate 
5010Sstevel@tonic-gate 	switch (dnp->dn_args->dn_kind) {
5020Sstevel@tonic-gate 	case DT_NODE_STRING:
5030Sstevel@tonic-gate 		format = dnp->dn_args->dn_string;
5040Sstevel@tonic-gate 		anp = dnp->dn_args->dn_list;
5050Sstevel@tonic-gate 		argr = 2;
5060Sstevel@tonic-gate 		break;
5070Sstevel@tonic-gate 	case DT_NODE_AGG:
5080Sstevel@tonic-gate 		format = NULL;
5090Sstevel@tonic-gate 		anp = dnp->dn_args;
5100Sstevel@tonic-gate 		argr = 1;
5110Sstevel@tonic-gate 		break;
5120Sstevel@tonic-gate 	default:
5130Sstevel@tonic-gate 		format = NULL;
5140Sstevel@tonic-gate 		anp = dnp->dn_args;
5150Sstevel@tonic-gate 		argr = 1;
5160Sstevel@tonic-gate 	}
5170Sstevel@tonic-gate 
518*1017Sbmc 	if (argc < argr) {
5190Sstevel@tonic-gate 		dnerror(dnp, D_PRINTA_PROTO,
5200Sstevel@tonic-gate 		    "%s( ) prototype mismatch: %d args passed, %d expected\n",
5210Sstevel@tonic-gate 		    dnp->dn_ident->di_name, argc, argr);
5220Sstevel@tonic-gate 	}
5230Sstevel@tonic-gate 
524*1017Sbmc 	assert(anp != NULL);
5250Sstevel@tonic-gate 
526*1017Sbmc 	while (anp != NULL) {
527*1017Sbmc 		if (anp->dn_kind != DT_NODE_AGG) {
528*1017Sbmc 			dnerror(dnp, D_PRINTA_AGGARG,
529*1017Sbmc 			    "%s( ) argument #%d is incompatible with "
530*1017Sbmc 			    "prototype:\n\tprototype: aggregation\n"
531*1017Sbmc 			    "\t argument: %s\n", dnp->dn_ident->di_name, argr,
532*1017Sbmc 			    dt_node_type_name(anp, n, sizeof (n)));
533*1017Sbmc 		}
534*1017Sbmc 
535*1017Sbmc 		aid = anp->dn_ident;
536*1017Sbmc 		fid = aid->di_iarg;
537*1017Sbmc 
538*1017Sbmc 		if (aid->di_gen == dtp->dt_gen &&
539*1017Sbmc 		    !(aid->di_flags & DT_IDFLG_MOD)) {
540*1017Sbmc 			dnerror(dnp, D_PRINTA_AGGBAD,
541*1017Sbmc 			    "undefined aggregation: @%s\n", aid->di_name);
542*1017Sbmc 		}
5430Sstevel@tonic-gate 
544*1017Sbmc 		/*
545*1017Sbmc 		 * If we have multiple aggregations, we must be sure that
546*1017Sbmc 		 * their key signatures match.
547*1017Sbmc 		 */
548*1017Sbmc 		if (proto != NULL) {
549*1017Sbmc 			dt_printa_validate(proto, anp);
550*1017Sbmc 		} else {
551*1017Sbmc 			proto = anp;
552*1017Sbmc 		}
5530Sstevel@tonic-gate 
554*1017Sbmc 		if (format != NULL) {
555*1017Sbmc 			yylineno = dnp->dn_line;
5560Sstevel@tonic-gate 
557*1017Sbmc 			sdp->dtsd_fmtdata =
558*1017Sbmc 			    dt_printf_create(yypcb->pcb_hdl, format);
559*1017Sbmc 			dt_printf_validate(sdp->dtsd_fmtdata,
560*1017Sbmc 			    DT_PRINTF_AGGREGATION, dnp->dn_ident, 1,
561*1017Sbmc 			    fid->di_id, ((dt_idsig_t *)aid->di_data)->dis_args);
562*1017Sbmc 			format = NULL;
563*1017Sbmc 		}
564*1017Sbmc 
565*1017Sbmc 		ap = dt_stmt_action(dtp, sdp);
566*1017Sbmc 		dt_action_difconst(ap, anp->dn_ident->di_id, DTRACEACT_PRINTA);
567*1017Sbmc 
568*1017Sbmc 		anp = anp->dn_list;
569*1017Sbmc 		argr++;
5700Sstevel@tonic-gate 	}
5710Sstevel@tonic-gate }
5720Sstevel@tonic-gate 
5730Sstevel@tonic-gate static void
5740Sstevel@tonic-gate dt_action_printflike(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp,
5750Sstevel@tonic-gate     dtrace_actkind_t kind)
5760Sstevel@tonic-gate {
5770Sstevel@tonic-gate 	dt_node_t *anp, *arg1;
5780Sstevel@tonic-gate 	dtrace_actdesc_t *ap = NULL;
5790Sstevel@tonic-gate 	char n[DT_TYPE_NAMELEN], *str;
5800Sstevel@tonic-gate 
5810Sstevel@tonic-gate 	assert(DTRACEACT_ISPRINTFLIKE(kind));
5820Sstevel@tonic-gate 
5830Sstevel@tonic-gate 	if (dnp->dn_args->dn_kind != DT_NODE_STRING) {
5840Sstevel@tonic-gate 		dnerror(dnp, D_PRINTF_ARG_FMT,
5850Sstevel@tonic-gate 		    "%s( ) argument #1 is incompatible with prototype:\n"
5860Sstevel@tonic-gate 		    "\tprototype: string constant\n\t argument: %s\n",
5870Sstevel@tonic-gate 		    dnp->dn_ident->di_name,
5880Sstevel@tonic-gate 		    dt_node_type_name(dnp->dn_args, n, sizeof (n)));
5890Sstevel@tonic-gate 	}
5900Sstevel@tonic-gate 
5910Sstevel@tonic-gate 	arg1 = dnp->dn_args->dn_list;
5920Sstevel@tonic-gate 	yylineno = dnp->dn_line;
5930Sstevel@tonic-gate 	str = dnp->dn_args->dn_string;
5940Sstevel@tonic-gate 
5950Sstevel@tonic-gate 
5960Sstevel@tonic-gate 	/*
5970Sstevel@tonic-gate 	 * If this is an freopen(), we use an empty string to denote that
5980Sstevel@tonic-gate 	 * stdout should be restored.  For other printf()-like actions, an
5990Sstevel@tonic-gate 	 * empty format string is illegal:  an empty format string would
6000Sstevel@tonic-gate 	 * result in malformed DOF, and the compiler thus flags an empty
6010Sstevel@tonic-gate 	 * format string as a compile-time error.  To avoid propagating the
6020Sstevel@tonic-gate 	 * freopen() special case throughout the system, we simply transpose
6030Sstevel@tonic-gate 	 * an empty string into a sentinel string (DT_FREOPEN_RESTORE) that
6040Sstevel@tonic-gate 	 * denotes that stdout should be restored.
6050Sstevel@tonic-gate 	 */
6060Sstevel@tonic-gate 	if (kind == DTRACEACT_FREOPEN) {
6070Sstevel@tonic-gate 		if (strcmp(str, DT_FREOPEN_RESTORE) == 0) {
6080Sstevel@tonic-gate 			/*
6090Sstevel@tonic-gate 			 * Our sentinel is always an invalid argument to
6100Sstevel@tonic-gate 			 * freopen(), but if it's been manually specified, we
6110Sstevel@tonic-gate 			 * must fail now instead of when the freopen() is
6120Sstevel@tonic-gate 			 * actually evaluated.
6130Sstevel@tonic-gate 			 */
6140Sstevel@tonic-gate 			dnerror(dnp, D_FREOPEN_INVALID,
6150Sstevel@tonic-gate 			    "%s( ) argument #1 cannot be \"%s\"\n",
6160Sstevel@tonic-gate 			    dnp->dn_ident->di_name, DT_FREOPEN_RESTORE);
6170Sstevel@tonic-gate 		}
6180Sstevel@tonic-gate 
6190Sstevel@tonic-gate 		if (str[0] == '\0')
6200Sstevel@tonic-gate 			str = DT_FREOPEN_RESTORE;
6210Sstevel@tonic-gate 	}
6220Sstevel@tonic-gate 
623265Smws 	sdp->dtsd_fmtdata = dt_printf_create(dtp, str);
6240Sstevel@tonic-gate 
6250Sstevel@tonic-gate 	dt_printf_validate(sdp->dtsd_fmtdata, DT_PRINTF_EXACTLEN,
6260Sstevel@tonic-gate 	    dnp->dn_ident, 1, DTRACEACT_AGGREGATION, arg1);
6270Sstevel@tonic-gate 
6280Sstevel@tonic-gate 	if (arg1 == NULL) {
6290Sstevel@tonic-gate 		dif_instr_t *dbuf;
6300Sstevel@tonic-gate 		dtrace_difo_t *dp;
6310Sstevel@tonic-gate 
632265Smws 		if ((dbuf = dt_alloc(dtp, sizeof (dif_instr_t))) == NULL ||
633265Smws 		    (dp = dt_zalloc(dtp, sizeof (dtrace_difo_t))) == NULL) {
634265Smws 			dt_free(dtp, dbuf);
6350Sstevel@tonic-gate 			longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
6360Sstevel@tonic-gate 		}
6370Sstevel@tonic-gate 
6380Sstevel@tonic-gate 		dbuf[0] = DIF_INSTR_RET(DIF_REG_R0); /* ret %r0 */
6390Sstevel@tonic-gate 
6400Sstevel@tonic-gate 		dp->dtdo_buf = dbuf;
6410Sstevel@tonic-gate 		dp->dtdo_len = 1;
6420Sstevel@tonic-gate 		dp->dtdo_rtype = dt_int_rtype;
6430Sstevel@tonic-gate 
6440Sstevel@tonic-gate 		ap = dt_stmt_action(dtp, sdp);
6450Sstevel@tonic-gate 		ap->dtad_difo = dp;
6460Sstevel@tonic-gate 		ap->dtad_kind = kind;
6470Sstevel@tonic-gate 		return;
6480Sstevel@tonic-gate 	}
6490Sstevel@tonic-gate 
6500Sstevel@tonic-gate 	for (anp = arg1; anp != NULL; anp = anp->dn_list) {
6510Sstevel@tonic-gate 		ap = dt_stmt_action(dtp, sdp);
6520Sstevel@tonic-gate 		dt_cg(yypcb, anp);
6530Sstevel@tonic-gate 		ap->dtad_difo = dt_as(yypcb);
6540Sstevel@tonic-gate 		ap->dtad_kind = kind;
6550Sstevel@tonic-gate 	}
6560Sstevel@tonic-gate }
6570Sstevel@tonic-gate 
6580Sstevel@tonic-gate static void
6590Sstevel@tonic-gate dt_action_trace(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
6600Sstevel@tonic-gate {
6610Sstevel@tonic-gate 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
6620Sstevel@tonic-gate 
6630Sstevel@tonic-gate 	if (dt_node_is_void(dnp->dn_args)) {
6640Sstevel@tonic-gate 		dnerror(dnp->dn_args, D_TRACE_VOID,
6650Sstevel@tonic-gate 		    "trace( ) may not be applied to a void expression\n");
6660Sstevel@tonic-gate 	}
6670Sstevel@tonic-gate 
6680Sstevel@tonic-gate 	if (dt_node_is_dynamic(dnp->dn_args)) {
6690Sstevel@tonic-gate 		dnerror(dnp->dn_args, D_TRACE_DYN,
6700Sstevel@tonic-gate 		    "trace( ) may not be applied to a dynamic expression\n");
6710Sstevel@tonic-gate 	}
6720Sstevel@tonic-gate 
6730Sstevel@tonic-gate 	dt_cg(yypcb, dnp->dn_args);
6740Sstevel@tonic-gate 	ap->dtad_difo = dt_as(yypcb);
6750Sstevel@tonic-gate 	ap->dtad_kind = DTRACEACT_DIFEXPR;
6760Sstevel@tonic-gate }
6770Sstevel@tonic-gate 
6780Sstevel@tonic-gate static void
6790Sstevel@tonic-gate dt_action_tracemem(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
6800Sstevel@tonic-gate {
6810Sstevel@tonic-gate 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
6820Sstevel@tonic-gate 
6830Sstevel@tonic-gate 	dt_node_t *addr = dnp->dn_args;
6840Sstevel@tonic-gate 	dt_node_t *size = dnp->dn_args->dn_list;
6850Sstevel@tonic-gate 
6860Sstevel@tonic-gate 	char n[DT_TYPE_NAMELEN];
6870Sstevel@tonic-gate 
6880Sstevel@tonic-gate 	if (dt_node_is_integer(addr) == 0 && dt_node_is_pointer(addr) == 0) {
6890Sstevel@tonic-gate 		dnerror(addr, D_TRACEMEM_ADDR,
6900Sstevel@tonic-gate 		    "tracemem( ) argument #1 is incompatible with "
6910Sstevel@tonic-gate 		    "prototype:\n\tprototype: pointer or integer\n"
6920Sstevel@tonic-gate 		    "\t argument: %s\n",
6930Sstevel@tonic-gate 		    dt_node_type_name(addr, n, sizeof (n)));
6940Sstevel@tonic-gate 	}
6950Sstevel@tonic-gate 
6960Sstevel@tonic-gate 	if (dt_node_is_posconst(size) == 0) {
6970Sstevel@tonic-gate 		dnerror(size, D_TRACEMEM_SIZE, "tracemem( ) argument #2 must "
6980Sstevel@tonic-gate 		    "be a non-zero positive integral constant expression\n");
6990Sstevel@tonic-gate 	}
7000Sstevel@tonic-gate 
7010Sstevel@tonic-gate 	dt_cg(yypcb, addr);
7020Sstevel@tonic-gate 	ap->dtad_difo = dt_as(yypcb);
7030Sstevel@tonic-gate 	ap->dtad_kind = DTRACEACT_DIFEXPR;
7040Sstevel@tonic-gate 
7050Sstevel@tonic-gate 	ap->dtad_difo->dtdo_rtype.dtdt_flags |= DIF_TF_BYREF;
7060Sstevel@tonic-gate 	ap->dtad_difo->dtdo_rtype.dtdt_size = size->dn_value;
7070Sstevel@tonic-gate }
7080Sstevel@tonic-gate 
7090Sstevel@tonic-gate static void
7100Sstevel@tonic-gate dt_action_stack_args(dtrace_hdl_t *dtp, dtrace_actdesc_t *ap, dt_node_t *arg0)
7110Sstevel@tonic-gate {
7120Sstevel@tonic-gate 	ap->dtad_kind = DTRACEACT_STACK;
7130Sstevel@tonic-gate 
7140Sstevel@tonic-gate 	if (dtp->dt_options[DTRACEOPT_STACKFRAMES] != DTRACEOPT_UNSET) {
7150Sstevel@tonic-gate 		ap->dtad_arg = dtp->dt_options[DTRACEOPT_STACKFRAMES];
7160Sstevel@tonic-gate 	} else {
7170Sstevel@tonic-gate 		ap->dtad_arg = 0;
7180Sstevel@tonic-gate 	}
7190Sstevel@tonic-gate 
7200Sstevel@tonic-gate 	if (arg0 != NULL) {
7210Sstevel@tonic-gate 		if (arg0->dn_list != NULL) {
7220Sstevel@tonic-gate 			dnerror(arg0, D_STACK_PROTO, "stack( ) prototype "
7230Sstevel@tonic-gate 			    "mismatch: too many arguments\n");
7240Sstevel@tonic-gate 		}
7250Sstevel@tonic-gate 
7260Sstevel@tonic-gate 		if (dt_node_is_posconst(arg0) == 0) {
7270Sstevel@tonic-gate 			dnerror(arg0, D_STACK_SIZE, "stack( ) size must be a "
7280Sstevel@tonic-gate 			    "non-zero positive integral constant expression\n");
7290Sstevel@tonic-gate 		}
7300Sstevel@tonic-gate 
7310Sstevel@tonic-gate 		ap->dtad_arg = arg0->dn_value;
7320Sstevel@tonic-gate 	}
7330Sstevel@tonic-gate }
7340Sstevel@tonic-gate 
7350Sstevel@tonic-gate static void
7360Sstevel@tonic-gate dt_action_stack(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
7370Sstevel@tonic-gate {
7380Sstevel@tonic-gate 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
7390Sstevel@tonic-gate 	dt_action_stack_args(dtp, ap, dnp->dn_args);
7400Sstevel@tonic-gate }
7410Sstevel@tonic-gate 
7420Sstevel@tonic-gate static void
7430Sstevel@tonic-gate dt_action_ustack_args(dtrace_hdl_t *dtp, dtrace_actdesc_t *ap, dt_node_t *dnp)
7440Sstevel@tonic-gate {
7450Sstevel@tonic-gate 	uint32_t nframes = 0;
7460Sstevel@tonic-gate 	uint32_t strsize = 0;	/* default string table size */
7470Sstevel@tonic-gate 	dt_node_t *arg0 = dnp->dn_args;
7480Sstevel@tonic-gate 	dt_node_t *arg1 = arg0 != NULL ? arg0->dn_list : NULL;
7490Sstevel@tonic-gate 
7500Sstevel@tonic-gate 	assert(dnp->dn_ident->di_id == DT_ACT_JSTACK ||
7510Sstevel@tonic-gate 	    dnp->dn_ident->di_id == DT_ACT_USTACK);
7520Sstevel@tonic-gate 
7530Sstevel@tonic-gate 	if (dnp->dn_ident->di_id == DT_ACT_JSTACK) {
7540Sstevel@tonic-gate 		if (dtp->dt_options[DTRACEOPT_JSTACKFRAMES] != DTRACEOPT_UNSET)
7550Sstevel@tonic-gate 			nframes = dtp->dt_options[DTRACEOPT_JSTACKFRAMES];
7560Sstevel@tonic-gate 
7570Sstevel@tonic-gate 		if (dtp->dt_options[DTRACEOPT_JSTACKSTRSIZE] != DTRACEOPT_UNSET)
7580Sstevel@tonic-gate 			strsize = dtp->dt_options[DTRACEOPT_JSTACKSTRSIZE];
7590Sstevel@tonic-gate 
7600Sstevel@tonic-gate 		ap->dtad_kind = DTRACEACT_JSTACK;
7610Sstevel@tonic-gate 	} else {
7620Sstevel@tonic-gate 		assert(dnp->dn_ident->di_id == DT_ACT_USTACK);
7630Sstevel@tonic-gate 
7640Sstevel@tonic-gate 		if (dtp->dt_options[DTRACEOPT_USTACKFRAMES] != DTRACEOPT_UNSET)
7650Sstevel@tonic-gate 			nframes = dtp->dt_options[DTRACEOPT_USTACKFRAMES];
7660Sstevel@tonic-gate 
7670Sstevel@tonic-gate 		ap->dtad_kind = DTRACEACT_USTACK;
7680Sstevel@tonic-gate 	}
7690Sstevel@tonic-gate 
7700Sstevel@tonic-gate 	if (arg0 != NULL) {
7710Sstevel@tonic-gate 		if (!dt_node_is_posconst(arg0)) {
7720Sstevel@tonic-gate 			dnerror(arg0, D_USTACK_FRAMES, "ustack( ) argument #1 "
7730Sstevel@tonic-gate 			    "must be a non-zero positive integer constant\n");
7740Sstevel@tonic-gate 		}
7750Sstevel@tonic-gate 		nframes = (uint32_t)arg0->dn_value;
7760Sstevel@tonic-gate 	}
7770Sstevel@tonic-gate 
7780Sstevel@tonic-gate 	if (arg1 != NULL) {
7790Sstevel@tonic-gate 		if (arg1->dn_kind != DT_NODE_INT ||
7800Sstevel@tonic-gate 		    ((arg1->dn_flags & DT_NF_SIGNED) &&
7810Sstevel@tonic-gate 		    (int64_t)arg1->dn_value < 0)) {
7820Sstevel@tonic-gate 			dnerror(arg1, D_USTACK_STRSIZE, "ustack( ) argument #2 "
7830Sstevel@tonic-gate 			    "must be a positive integer constant\n");
7840Sstevel@tonic-gate 		}
7850Sstevel@tonic-gate 
7860Sstevel@tonic-gate 		if (arg1->dn_list != NULL) {
7870Sstevel@tonic-gate 			dnerror(arg1, D_USTACK_PROTO, "ustack( ) prototype "
7880Sstevel@tonic-gate 			    "mismatch: too many arguments\n");
7890Sstevel@tonic-gate 		}
7900Sstevel@tonic-gate 
7910Sstevel@tonic-gate 		strsize = (uint32_t)arg1->dn_value;
7920Sstevel@tonic-gate 	}
7930Sstevel@tonic-gate 
7940Sstevel@tonic-gate 	ap->dtad_arg = DTRACE_USTACK_ARG(nframes, strsize);
7950Sstevel@tonic-gate }
7960Sstevel@tonic-gate 
7970Sstevel@tonic-gate static void
7980Sstevel@tonic-gate dt_action_ustack(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
7990Sstevel@tonic-gate {
8000Sstevel@tonic-gate 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
8010Sstevel@tonic-gate 	dt_action_ustack_args(dtp, ap, dnp);
8020Sstevel@tonic-gate }
8030Sstevel@tonic-gate 
804457Sbmc static void
805457Sbmc dt_action_setopt(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
806457Sbmc {
807457Sbmc 	dtrace_actdesc_t *ap;
808457Sbmc 	dt_node_t *arg0, *arg1;
809457Sbmc 
810457Sbmc 	/*
811457Sbmc 	 * The prototype guarantees that we are called with either one or
812457Sbmc 	 * two arguments, and that any arguments that are present are strings.
813457Sbmc 	 */
814457Sbmc 	arg0 = dnp->dn_args;
815457Sbmc 	arg1 = arg0->dn_list;
816457Sbmc 
817457Sbmc 	ap = dt_stmt_action(dtp, sdp);
818457Sbmc 	dt_cg(yypcb, arg0);
819457Sbmc 	ap->dtad_difo = dt_as(yypcb);
820457Sbmc 	ap->dtad_kind = DTRACEACT_LIBACT;
821457Sbmc 	ap->dtad_arg = DT_ACT_SETOPT;
822457Sbmc 
823457Sbmc 	ap = dt_stmt_action(dtp, sdp);
824457Sbmc 
825457Sbmc 	if (arg1 == NULL) {
826457Sbmc 		dt_action_difconst(ap, 0, DTRACEACT_LIBACT);
827457Sbmc 	} else {
828457Sbmc 		dt_cg(yypcb, arg1);
829457Sbmc 		ap->dtad_difo = dt_as(yypcb);
830457Sbmc 		ap->dtad_kind = DTRACEACT_LIBACT;
831457Sbmc 	}
832457Sbmc 
833457Sbmc 	ap->dtad_arg = DT_ACT_SETOPT;
834457Sbmc }
835457Sbmc 
836457Sbmc /*ARGSUSED*/
837457Sbmc static void
838457Sbmc dt_action_symmod_args(dtrace_hdl_t *dtp, dtrace_actdesc_t *ap,
839457Sbmc     dt_node_t *dnp, dtrace_actkind_t kind)
840457Sbmc {
841457Sbmc 	assert(kind == DTRACEACT_SYM || kind == DTRACEACT_MOD ||
842457Sbmc 	    kind == DTRACEACT_USYM || kind == DTRACEACT_UMOD ||
843457Sbmc 	    kind == DTRACEACT_UADDR);
844457Sbmc 
845457Sbmc 	dt_cg(yypcb, dnp);
846457Sbmc 	ap->dtad_difo = dt_as(yypcb);
847457Sbmc 	ap->dtad_kind = kind;
848457Sbmc 	ap->dtad_difo->dtdo_rtype.dtdt_size = sizeof (uint64_t);
849457Sbmc }
850457Sbmc 
851457Sbmc static void
852457Sbmc dt_action_symmod(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp,
853457Sbmc     dtrace_actkind_t kind)
854457Sbmc {
855457Sbmc 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
856457Sbmc 	dt_action_symmod_args(dtp, ap, dnp->dn_args, kind);
857457Sbmc }
858457Sbmc 
8590Sstevel@tonic-gate /*ARGSUSED*/
8600Sstevel@tonic-gate static void
8610Sstevel@tonic-gate dt_action_ftruncate(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
8620Sstevel@tonic-gate {
8630Sstevel@tonic-gate 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
8640Sstevel@tonic-gate 
8650Sstevel@tonic-gate 	/*
8660Sstevel@tonic-gate 	 * Library actions need a DIFO that serves as an argument.  As
8670Sstevel@tonic-gate 	 * ftruncate() doesn't take an argument, we generate the constant 0
8680Sstevel@tonic-gate 	 * in a DIFO; this constant will be ignored when the ftruncate() is
8690Sstevel@tonic-gate 	 * processed.
8700Sstevel@tonic-gate 	 */
8710Sstevel@tonic-gate 	dt_action_difconst(ap, 0, DTRACEACT_LIBACT);
8720Sstevel@tonic-gate 	ap->dtad_arg = DT_ACT_FTRUNCATE;
8730Sstevel@tonic-gate }
8740Sstevel@tonic-gate 
8750Sstevel@tonic-gate /*ARGSUSED*/
8760Sstevel@tonic-gate static void
8770Sstevel@tonic-gate dt_action_stop(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
8780Sstevel@tonic-gate {
8790Sstevel@tonic-gate 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
8800Sstevel@tonic-gate 
8810Sstevel@tonic-gate 	ap->dtad_kind = DTRACEACT_STOP;
8820Sstevel@tonic-gate 	ap->dtad_arg = 0;
8830Sstevel@tonic-gate }
8840Sstevel@tonic-gate 
8850Sstevel@tonic-gate /*ARGSUSED*/
8860Sstevel@tonic-gate static void
8870Sstevel@tonic-gate dt_action_breakpoint(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
8880Sstevel@tonic-gate {
8890Sstevel@tonic-gate 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
8900Sstevel@tonic-gate 
8910Sstevel@tonic-gate 	ap->dtad_kind = DTRACEACT_BREAKPOINT;
8920Sstevel@tonic-gate 	ap->dtad_arg = 0;
8930Sstevel@tonic-gate }
8940Sstevel@tonic-gate 
8950Sstevel@tonic-gate /*ARGSUSED*/
8960Sstevel@tonic-gate static void
8970Sstevel@tonic-gate dt_action_panic(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
8980Sstevel@tonic-gate {
8990Sstevel@tonic-gate 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
9000Sstevel@tonic-gate 
9010Sstevel@tonic-gate 	ap->dtad_kind = DTRACEACT_PANIC;
9020Sstevel@tonic-gate 	ap->dtad_arg = 0;
9030Sstevel@tonic-gate }
9040Sstevel@tonic-gate 
9050Sstevel@tonic-gate static void
9060Sstevel@tonic-gate dt_action_chill(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
9070Sstevel@tonic-gate {
9080Sstevel@tonic-gate 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
9090Sstevel@tonic-gate 
9100Sstevel@tonic-gate 	dt_cg(yypcb, dnp->dn_args);
9110Sstevel@tonic-gate 	ap->dtad_difo = dt_as(yypcb);
9120Sstevel@tonic-gate 	ap->dtad_kind = DTRACEACT_CHILL;
9130Sstevel@tonic-gate }
9140Sstevel@tonic-gate 
9150Sstevel@tonic-gate static void
9160Sstevel@tonic-gate dt_action_raise(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
9170Sstevel@tonic-gate {
9180Sstevel@tonic-gate 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
9190Sstevel@tonic-gate 
9200Sstevel@tonic-gate 	dt_cg(yypcb, dnp->dn_args);
9210Sstevel@tonic-gate 	ap->dtad_difo = dt_as(yypcb);
9220Sstevel@tonic-gate 	ap->dtad_kind = DTRACEACT_RAISE;
9230Sstevel@tonic-gate }
9240Sstevel@tonic-gate 
9250Sstevel@tonic-gate static void
9260Sstevel@tonic-gate dt_action_exit(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
9270Sstevel@tonic-gate {
9280Sstevel@tonic-gate 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
9290Sstevel@tonic-gate 
9300Sstevel@tonic-gate 	dt_cg(yypcb, dnp->dn_args);
9310Sstevel@tonic-gate 	ap->dtad_difo = dt_as(yypcb);
9320Sstevel@tonic-gate 	ap->dtad_kind = DTRACEACT_EXIT;
9330Sstevel@tonic-gate 	ap->dtad_difo->dtdo_rtype.dtdt_size = sizeof (int);
9340Sstevel@tonic-gate }
9350Sstevel@tonic-gate 
9360Sstevel@tonic-gate static void
9370Sstevel@tonic-gate dt_action_speculate(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
9380Sstevel@tonic-gate {
9390Sstevel@tonic-gate 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
9400Sstevel@tonic-gate 
9410Sstevel@tonic-gate 	dt_cg(yypcb, dnp->dn_args);
9420Sstevel@tonic-gate 	ap->dtad_difo = dt_as(yypcb);
9430Sstevel@tonic-gate 	ap->dtad_kind = DTRACEACT_SPECULATE;
9440Sstevel@tonic-gate }
9450Sstevel@tonic-gate 
9460Sstevel@tonic-gate static void
9470Sstevel@tonic-gate dt_action_commit(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
9480Sstevel@tonic-gate {
9490Sstevel@tonic-gate 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
9500Sstevel@tonic-gate 
9510Sstevel@tonic-gate 	dt_cg(yypcb, dnp->dn_args);
9520Sstevel@tonic-gate 	ap->dtad_difo = dt_as(yypcb);
9530Sstevel@tonic-gate 	ap->dtad_kind = DTRACEACT_COMMIT;
9540Sstevel@tonic-gate }
9550Sstevel@tonic-gate 
9560Sstevel@tonic-gate static void
9570Sstevel@tonic-gate dt_action_discard(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
9580Sstevel@tonic-gate {
9590Sstevel@tonic-gate 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
9600Sstevel@tonic-gate 
9610Sstevel@tonic-gate 	dt_cg(yypcb, dnp->dn_args);
9620Sstevel@tonic-gate 	ap->dtad_difo = dt_as(yypcb);
9630Sstevel@tonic-gate 	ap->dtad_kind = DTRACEACT_DISCARD;
9640Sstevel@tonic-gate }
9650Sstevel@tonic-gate 
9660Sstevel@tonic-gate static void
9670Sstevel@tonic-gate dt_compile_fun(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
9680Sstevel@tonic-gate {
9690Sstevel@tonic-gate 	switch (dnp->dn_expr->dn_ident->di_id) {
9700Sstevel@tonic-gate 	case DT_ACT_BREAKPOINT:
9710Sstevel@tonic-gate 		dt_action_breakpoint(dtp, dnp->dn_expr, sdp);
9720Sstevel@tonic-gate 		break;
9730Sstevel@tonic-gate 	case DT_ACT_CHILL:
9740Sstevel@tonic-gate 		dt_action_chill(dtp, dnp->dn_expr, sdp);
9750Sstevel@tonic-gate 		break;
9760Sstevel@tonic-gate 	case DT_ACT_CLEAR:
9770Sstevel@tonic-gate 		dt_action_clear(dtp, dnp->dn_expr, sdp);
9780Sstevel@tonic-gate 		break;
9790Sstevel@tonic-gate 	case DT_ACT_COMMIT:
9800Sstevel@tonic-gate 		dt_action_commit(dtp, dnp->dn_expr, sdp);
9810Sstevel@tonic-gate 		break;
9820Sstevel@tonic-gate 	case DT_ACT_DENORMALIZE:
9830Sstevel@tonic-gate 		dt_action_normalize(dtp, dnp->dn_expr, sdp);
9840Sstevel@tonic-gate 		break;
9850Sstevel@tonic-gate 	case DT_ACT_DISCARD:
9860Sstevel@tonic-gate 		dt_action_discard(dtp, dnp->dn_expr, sdp);
9870Sstevel@tonic-gate 		break;
9880Sstevel@tonic-gate 	case DT_ACT_EXIT:
9890Sstevel@tonic-gate 		dt_action_exit(dtp, dnp->dn_expr, sdp);
9900Sstevel@tonic-gate 		break;
9910Sstevel@tonic-gate 	case DT_ACT_FREOPEN:
9920Sstevel@tonic-gate 		dt_action_printflike(dtp, dnp->dn_expr, sdp, DTRACEACT_FREOPEN);
9930Sstevel@tonic-gate 		break;
9940Sstevel@tonic-gate 	case DT_ACT_FTRUNCATE:
9950Sstevel@tonic-gate 		dt_action_ftruncate(dtp, dnp->dn_expr, sdp);
9960Sstevel@tonic-gate 		break;
997457Sbmc 	case DT_ACT_MOD:
998457Sbmc 		dt_action_symmod(dtp, dnp->dn_expr, sdp, DTRACEACT_MOD);
999457Sbmc 		break;
10000Sstevel@tonic-gate 	case DT_ACT_NORMALIZE:
10010Sstevel@tonic-gate 		dt_action_normalize(dtp, dnp->dn_expr, sdp);
10020Sstevel@tonic-gate 		break;
10030Sstevel@tonic-gate 	case DT_ACT_PANIC:
10040Sstevel@tonic-gate 		dt_action_panic(dtp, dnp->dn_expr, sdp);
10050Sstevel@tonic-gate 		break;
10060Sstevel@tonic-gate 	case DT_ACT_PRINTA:
10070Sstevel@tonic-gate 		dt_action_printa(dtp, dnp->dn_expr, sdp);
10080Sstevel@tonic-gate 		break;
10090Sstevel@tonic-gate 	case DT_ACT_PRINTF:
10100Sstevel@tonic-gate 		dt_action_printflike(dtp, dnp->dn_expr, sdp, DTRACEACT_PRINTF);
10110Sstevel@tonic-gate 		break;
10120Sstevel@tonic-gate 	case DT_ACT_RAISE:
10130Sstevel@tonic-gate 		dt_action_raise(dtp, dnp->dn_expr, sdp);
10140Sstevel@tonic-gate 		break;
1015457Sbmc 	case DT_ACT_SETOPT:
1016457Sbmc 		dt_action_setopt(dtp, dnp->dn_expr, sdp);
1017457Sbmc 		break;
10180Sstevel@tonic-gate 	case DT_ACT_SPECULATE:
10190Sstevel@tonic-gate 		dt_action_speculate(dtp, dnp->dn_expr, sdp);
10200Sstevel@tonic-gate 		break;
10210Sstevel@tonic-gate 	case DT_ACT_STACK:
10220Sstevel@tonic-gate 		dt_action_stack(dtp, dnp->dn_expr, sdp);
10230Sstevel@tonic-gate 		break;
10240Sstevel@tonic-gate 	case DT_ACT_STOP:
10250Sstevel@tonic-gate 		dt_action_stop(dtp, dnp->dn_expr, sdp);
10260Sstevel@tonic-gate 		break;
1027457Sbmc 	case DT_ACT_SYM:
1028457Sbmc 		dt_action_symmod(dtp, dnp->dn_expr, sdp, DTRACEACT_SYM);
1029457Sbmc 		break;
10300Sstevel@tonic-gate 	case DT_ACT_SYSTEM:
10310Sstevel@tonic-gate 		dt_action_printflike(dtp, dnp->dn_expr, sdp, DTRACEACT_SYSTEM);
10320Sstevel@tonic-gate 		break;
10330Sstevel@tonic-gate 	case DT_ACT_TRACE:
10340Sstevel@tonic-gate 		dt_action_trace(dtp, dnp->dn_expr, sdp);
10350Sstevel@tonic-gate 		break;
10360Sstevel@tonic-gate 	case DT_ACT_TRACEMEM:
10370Sstevel@tonic-gate 		dt_action_tracemem(dtp, dnp->dn_expr, sdp);
10380Sstevel@tonic-gate 		break;
10390Sstevel@tonic-gate 	case DT_ACT_TRUNC:
10400Sstevel@tonic-gate 		dt_action_trunc(dtp, dnp->dn_expr, sdp);
10410Sstevel@tonic-gate 		break;
1042457Sbmc 	case DT_ACT_UADDR:
1043457Sbmc 		dt_action_symmod(dtp, dnp->dn_expr, sdp, DTRACEACT_UADDR);
1044457Sbmc 		break;
1045457Sbmc 	case DT_ACT_UMOD:
1046457Sbmc 		dt_action_symmod(dtp, dnp->dn_expr, sdp, DTRACEACT_UMOD);
1047457Sbmc 		break;
1048457Sbmc 	case DT_ACT_USYM:
1049457Sbmc 		dt_action_symmod(dtp, dnp->dn_expr, sdp, DTRACEACT_USYM);
1050457Sbmc 		break;
10510Sstevel@tonic-gate 	case DT_ACT_USTACK:
10520Sstevel@tonic-gate 	case DT_ACT_JSTACK:
10530Sstevel@tonic-gate 		dt_action_ustack(dtp, dnp->dn_expr, sdp);
10540Sstevel@tonic-gate 		break;
10550Sstevel@tonic-gate 	default:
10560Sstevel@tonic-gate 		dnerror(dnp->dn_expr, D_UNKNOWN, "tracing function %s( ) is "
10570Sstevel@tonic-gate 		    "not yet supported\n", dnp->dn_expr->dn_ident->di_name);
10580Sstevel@tonic-gate 	}
10590Sstevel@tonic-gate }
10600Sstevel@tonic-gate 
10610Sstevel@tonic-gate static void
10620Sstevel@tonic-gate dt_compile_exp(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
10630Sstevel@tonic-gate {
10640Sstevel@tonic-gate 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
10650Sstevel@tonic-gate 
10660Sstevel@tonic-gate 	dt_cg(yypcb, dnp->dn_expr);
10670Sstevel@tonic-gate 	ap->dtad_difo = dt_as(yypcb);
10680Sstevel@tonic-gate 	ap->dtad_difo->dtdo_rtype = dt_void_rtype;
10690Sstevel@tonic-gate 	ap->dtad_kind = DTRACEACT_DIFEXPR;
10700Sstevel@tonic-gate }
10710Sstevel@tonic-gate 
10720Sstevel@tonic-gate static void
10730Sstevel@tonic-gate dt_compile_agg(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
10740Sstevel@tonic-gate {
10750Sstevel@tonic-gate 	dt_ident_t *aid, *fid;
1076457Sbmc 	dt_node_t *anp, *incr = NULL;
10770Sstevel@tonic-gate 	dtrace_actdesc_t *ap;
1078457Sbmc 	uint_t n = 1, argmax;
1079457Sbmc 	uint64_t arg = 0;
10800Sstevel@tonic-gate 
10810Sstevel@tonic-gate 	/*
10820Sstevel@tonic-gate 	 * If the aggregation has no aggregating function applied to it, then
10830Sstevel@tonic-gate 	 * this statement has no effect.  Flag this as a programming error.
10840Sstevel@tonic-gate 	 */
10850Sstevel@tonic-gate 	if (dnp->dn_aggfun == NULL) {
10860Sstevel@tonic-gate 		dnerror(dnp, D_AGG_NULL, "expression has null effect: @%s\n",
10870Sstevel@tonic-gate 		    dnp->dn_ident->di_name);
10880Sstevel@tonic-gate 	}
10890Sstevel@tonic-gate 
10900Sstevel@tonic-gate 	aid = dnp->dn_ident;
10910Sstevel@tonic-gate 	fid = dnp->dn_aggfun->dn_ident;
10920Sstevel@tonic-gate 
10930Sstevel@tonic-gate 	if (dnp->dn_aggfun->dn_args != NULL &&
10940Sstevel@tonic-gate 	    dt_node_is_scalar(dnp->dn_aggfun->dn_args) == 0) {
10950Sstevel@tonic-gate 		dnerror(dnp->dn_aggfun, D_AGG_SCALAR, "%s( ) argument #1 must "
10960Sstevel@tonic-gate 		    "be of scalar type\n", fid->di_name);
10970Sstevel@tonic-gate 	}
10980Sstevel@tonic-gate 
10990Sstevel@tonic-gate 	/*
11000Sstevel@tonic-gate 	 * The ID of the aggregation itself is implicitly recorded as the first
11010Sstevel@tonic-gate 	 * member of each aggregation tuple so we can distinguish them later.
11020Sstevel@tonic-gate 	 */
11030Sstevel@tonic-gate 	ap = dt_stmt_action(dtp, sdp);
11040Sstevel@tonic-gate 	dt_action_difconst(ap, aid->di_id, DTRACEACT_DIFEXPR);
11050Sstevel@tonic-gate 
11060Sstevel@tonic-gate 	for (anp = dnp->dn_aggtup; anp != NULL; anp = anp->dn_list) {
11070Sstevel@tonic-gate 		ap = dt_stmt_action(dtp, sdp);
11080Sstevel@tonic-gate 		n++;
11090Sstevel@tonic-gate 
11100Sstevel@tonic-gate 		if (anp->dn_kind == DT_NODE_FUNC) {
11110Sstevel@tonic-gate 			if (anp->dn_ident->di_id == DT_ACT_STACK) {
11120Sstevel@tonic-gate 				dt_action_stack_args(dtp, ap, anp->dn_args);
11130Sstevel@tonic-gate 				continue;
11140Sstevel@tonic-gate 			}
11150Sstevel@tonic-gate 
11160Sstevel@tonic-gate 			if (anp->dn_ident->di_id == DT_ACT_USTACK ||
11170Sstevel@tonic-gate 			    anp->dn_ident->di_id == DT_ACT_JSTACK) {
11180Sstevel@tonic-gate 				dt_action_ustack_args(dtp, ap, anp);
11190Sstevel@tonic-gate 				continue;
11200Sstevel@tonic-gate 			}
1121457Sbmc 
1122457Sbmc 			switch (anp->dn_ident->di_id) {
1123457Sbmc 			case DT_ACT_UADDR:
1124457Sbmc 				dt_action_symmod_args(dtp, ap,
1125457Sbmc 				    anp->dn_args, DTRACEACT_UADDR);
1126457Sbmc 				continue;
1127457Sbmc 
1128457Sbmc 			case DT_ACT_USYM:
1129457Sbmc 				dt_action_symmod_args(dtp, ap,
1130457Sbmc 				    anp->dn_args, DTRACEACT_USYM);
1131457Sbmc 				continue;
1132457Sbmc 
1133457Sbmc 			case DT_ACT_UMOD:
1134457Sbmc 				dt_action_symmod_args(dtp, ap,
1135457Sbmc 				    anp->dn_args, DTRACEACT_UMOD);
1136457Sbmc 				continue;
1137457Sbmc 
1138457Sbmc 			case DT_ACT_SYM:
1139457Sbmc 				dt_action_symmod_args(dtp, ap,
1140457Sbmc 				    anp->dn_args, DTRACEACT_SYM);
1141457Sbmc 				continue;
1142457Sbmc 
1143457Sbmc 			case DT_ACT_MOD:
1144457Sbmc 				dt_action_symmod_args(dtp, ap,
1145457Sbmc 				    anp->dn_args, DTRACEACT_MOD);
1146457Sbmc 				continue;
1147457Sbmc 
1148457Sbmc 			default:
1149457Sbmc 				break;
1150457Sbmc 			}
11510Sstevel@tonic-gate 		}
11520Sstevel@tonic-gate 
11530Sstevel@tonic-gate 		dt_cg(yypcb, anp);
11540Sstevel@tonic-gate 		ap->dtad_difo = dt_as(yypcb);
11550Sstevel@tonic-gate 		ap->dtad_kind = DTRACEACT_DIFEXPR;
11560Sstevel@tonic-gate 	}
11570Sstevel@tonic-gate 
11580Sstevel@tonic-gate 	if (fid->di_id == DTRACEAGG_LQUANTIZE) {
11590Sstevel@tonic-gate 		/*
1160457Sbmc 		 * For linear quantization, we have between two and four
1161457Sbmc 		 * arguments in addition to the expression:
11620Sstevel@tonic-gate 		 *
11630Sstevel@tonic-gate 		 *    arg1 => Base value
11640Sstevel@tonic-gate 		 *    arg2 => Limit value
11650Sstevel@tonic-gate 		 *    arg3 => Quantization level step size (defaults to 1)
1166457Sbmc 		 *    arg4 => Quantization increment value (defaults to 1)
11670Sstevel@tonic-gate 		 */
11680Sstevel@tonic-gate 		dt_node_t *arg1 = dnp->dn_aggfun->dn_args->dn_list;
11690Sstevel@tonic-gate 		dt_node_t *arg2 = arg1->dn_list;
11700Sstevel@tonic-gate 		dt_node_t *arg3 = arg2->dn_list;
1171*1017Sbmc 		dt_idsig_t *isp;
1172*1017Sbmc 		uint64_t nlevels, step = 1, oarg;
11730Sstevel@tonic-gate 		int64_t baseval, limitval;
11740Sstevel@tonic-gate 
11750Sstevel@tonic-gate 		if (arg1->dn_kind != DT_NODE_INT) {
11760Sstevel@tonic-gate 			dnerror(arg1, D_LQUANT_BASETYPE, "lquantize( ) "
11770Sstevel@tonic-gate 			    "argument #1 must be an integer constant\n");
11780Sstevel@tonic-gate 		}
11790Sstevel@tonic-gate 
11800Sstevel@tonic-gate 		baseval = (int64_t)arg1->dn_value;
11810Sstevel@tonic-gate 
11820Sstevel@tonic-gate 		if (baseval < INT32_MIN || baseval > INT32_MAX) {
11830Sstevel@tonic-gate 			dnerror(arg1, D_LQUANT_BASEVAL, "lquantize( ) "
11840Sstevel@tonic-gate 			    "argument #1 must be a 32-bit quantity\n");
11850Sstevel@tonic-gate 		}
11860Sstevel@tonic-gate 
11870Sstevel@tonic-gate 		if (arg2->dn_kind != DT_NODE_INT) {
11880Sstevel@tonic-gate 			dnerror(arg2, D_LQUANT_LIMTYPE, "lquantize( ) "
11890Sstevel@tonic-gate 			    "argument #2 must be an integer constant\n");
11900Sstevel@tonic-gate 		}
11910Sstevel@tonic-gate 
11920Sstevel@tonic-gate 		limitval = (int64_t)arg2->dn_value;
11930Sstevel@tonic-gate 
11940Sstevel@tonic-gate 		if (limitval < INT32_MIN || limitval > INT32_MAX) {
11950Sstevel@tonic-gate 			dnerror(arg2, D_LQUANT_LIMVAL, "lquantize( ) "
11960Sstevel@tonic-gate 			    "argument #2 must be a 32-bit quantity\n");
11970Sstevel@tonic-gate 		}
11980Sstevel@tonic-gate 
11990Sstevel@tonic-gate 		if (limitval < baseval) {
12000Sstevel@tonic-gate 			dnerror(dnp, D_LQUANT_MISMATCH,
12010Sstevel@tonic-gate 			    "lquantize( ) base (argument #1) must be less "
12020Sstevel@tonic-gate 			    "than limit (argument #2)\n");
12030Sstevel@tonic-gate 		}
12040Sstevel@tonic-gate 
12050Sstevel@tonic-gate 		if (arg3 != NULL) {
12060Sstevel@tonic-gate 			if (!dt_node_is_posconst(arg3)) {
12070Sstevel@tonic-gate 				dnerror(arg3, D_LQUANT_STEPTYPE, "lquantize( ) "
12080Sstevel@tonic-gate 				    "argument #3 must be a non-zero positive "
12090Sstevel@tonic-gate 				    "integer constant\n");
12100Sstevel@tonic-gate 			}
12110Sstevel@tonic-gate 
12120Sstevel@tonic-gate 			if ((step = arg3->dn_value) > UINT16_MAX) {
12130Sstevel@tonic-gate 				dnerror(arg3, D_LQUANT_STEPVAL, "lquantize( ) "
12140Sstevel@tonic-gate 				    "argument #3 must be a 16-bit quantity\n");
12150Sstevel@tonic-gate 			}
12160Sstevel@tonic-gate 		}
12170Sstevel@tonic-gate 
12180Sstevel@tonic-gate 		nlevels = (limitval - baseval) / step;
12190Sstevel@tonic-gate 
12200Sstevel@tonic-gate 		if (nlevels == 0) {
12210Sstevel@tonic-gate 			dnerror(dnp, D_LQUANT_STEPLARGE,
12220Sstevel@tonic-gate 			    "lquantize( ) step (argument #3) too large: must "
12230Sstevel@tonic-gate 			    "have at least one quantization level\n");
12240Sstevel@tonic-gate 		}
12250Sstevel@tonic-gate 
12260Sstevel@tonic-gate 		if (nlevels > UINT16_MAX) {
12270Sstevel@tonic-gate 			dnerror(dnp, D_LQUANT_STEPSMALL, "lquantize( ) step "
12280Sstevel@tonic-gate 			    "(argument #3) too small: number of quantization "
12290Sstevel@tonic-gate 			    "levels must be a 16-bit quantity\n");
12300Sstevel@tonic-gate 		}
12310Sstevel@tonic-gate 
1232457Sbmc 		arg = (step << DTRACE_LQUANTIZE_STEPSHIFT) |
12330Sstevel@tonic-gate 		    (nlevels << DTRACE_LQUANTIZE_LEVELSHIFT) |
12340Sstevel@tonic-gate 		    ((baseval << DTRACE_LQUANTIZE_BASESHIFT) &
12350Sstevel@tonic-gate 		    DTRACE_LQUANTIZE_BASEMASK);
1236457Sbmc 
1237*1017Sbmc 		assert(arg != 0);
1238*1017Sbmc 
1239*1017Sbmc 		isp = (dt_idsig_t *)aid->di_data;
1240*1017Sbmc 
1241*1017Sbmc 		if (isp->dis_auxinfo == 0) {
1242*1017Sbmc 			/*
1243*1017Sbmc 			 * This is the first time we've seen an lquantize()
1244*1017Sbmc 			 * for this aggregation; we'll store our argument
1245*1017Sbmc 			 * as the auxiliary signature information.
1246*1017Sbmc 			 */
1247*1017Sbmc 			isp->dis_auxinfo = arg;
1248*1017Sbmc 		} else if ((oarg = isp->dis_auxinfo) != arg) {
1249*1017Sbmc 			/*
1250*1017Sbmc 			 * If we have seen this lquantize() before and the
1251*1017Sbmc 			 * argument doesn't match the original argument, pick
1252*1017Sbmc 			 * the original argument apart to concisely report the
1253*1017Sbmc 			 * mismatch.
1254*1017Sbmc 			 */
1255*1017Sbmc 			int obaseval = DTRACE_LQUANTIZE_BASE(oarg);
1256*1017Sbmc 			int onlevels = DTRACE_LQUANTIZE_LEVELS(oarg);
1257*1017Sbmc 			int ostep = DTRACE_LQUANTIZE_STEP(oarg);
1258*1017Sbmc 
1259*1017Sbmc 			if (obaseval != baseval) {
1260*1017Sbmc 				dnerror(dnp, D_LQUANT_MATCHBASE, "lquantize( ) "
1261*1017Sbmc 				    "base (argument #1) doesn't match previous "
1262*1017Sbmc 				    "declaration: expected %d, found %d\n",
1263*1017Sbmc 				    obaseval, (int)baseval);
1264*1017Sbmc 			}
1265*1017Sbmc 
1266*1017Sbmc 			if (onlevels * ostep != nlevels * step) {
1267*1017Sbmc 				dnerror(dnp, D_LQUANT_MATCHLIM, "lquantize( ) "
1268*1017Sbmc 				    "limit (argument #2) doesn't match previous"
1269*1017Sbmc 				    " declaration: expected %d, found %d\n",
1270*1017Sbmc 				    obaseval + onlevels * ostep,
1271*1017Sbmc 				    (int)baseval + (int)nlevels * (int)step);
1272*1017Sbmc 			}
1273*1017Sbmc 
1274*1017Sbmc 			if (ostep != step) {
1275*1017Sbmc 				dnerror(dnp, D_LQUANT_MATCHSTEP, "lquantize( ) "
1276*1017Sbmc 				    "step (argument #3) doesn't match previous "
1277*1017Sbmc 				    "declaration: expected %d, found %d\n",
1278*1017Sbmc 				    ostep, (int)step);
1279*1017Sbmc 			}
1280*1017Sbmc 
1281*1017Sbmc 			/*
1282*1017Sbmc 			 * We shouldn't be able to get here -- one of the
1283*1017Sbmc 			 * parameters must be mismatched if the arguments
1284*1017Sbmc 			 * didn't match.
1285*1017Sbmc 			 */
1286*1017Sbmc 			assert(0);
1287*1017Sbmc 		}
1288*1017Sbmc 
1289457Sbmc 		incr = arg3 != NULL ? arg3->dn_list : NULL;
1290457Sbmc 		argmax = 5;
1291457Sbmc 	}
1292457Sbmc 
1293457Sbmc 	if (fid->di_id == DTRACEAGG_QUANTIZE) {
1294457Sbmc 		incr = dnp->dn_aggfun->dn_args->dn_list;
1295457Sbmc 		argmax = 2;
1296457Sbmc 	}
1297457Sbmc 
1298457Sbmc 	if (incr != NULL) {
1299457Sbmc 		if (!dt_node_is_scalar(incr)) {
1300457Sbmc 			dnerror(dnp, D_PROTO_ARG, "%s( ) increment value "
1301457Sbmc 			    "(argument #%d) must be of scalar type\n",
1302457Sbmc 			    fid->di_name, argmax);
1303457Sbmc 		}
1304457Sbmc 
1305457Sbmc 		if ((anp = incr->dn_list) != NULL) {
1306457Sbmc 			int argc = argmax;
1307457Sbmc 
1308457Sbmc 			for (; anp != NULL; anp = anp->dn_list)
1309457Sbmc 				argc++;
1310457Sbmc 
1311457Sbmc 			dnerror(incr, D_PROTO_LEN, "%s( ) prototype "
1312457Sbmc 			    "mismatch: %d args passed, at most %d expected",
1313457Sbmc 			    fid->di_name, argc, argmax);
1314457Sbmc 		}
1315457Sbmc 
1316457Sbmc 		ap = dt_stmt_action(dtp, sdp);
1317457Sbmc 		n++;
1318457Sbmc 
1319457Sbmc 		dt_cg(yypcb, incr);
1320457Sbmc 		ap->dtad_difo = dt_as(yypcb);
1321457Sbmc 		ap->dtad_difo->dtdo_rtype = dt_void_rtype;
1322457Sbmc 		ap->dtad_kind = DTRACEACT_DIFEXPR;
1323457Sbmc 	}
1324457Sbmc 
1325457Sbmc 	assert(sdp->dtsd_aggdata == NULL);
1326457Sbmc 	sdp->dtsd_aggdata = aid;
1327457Sbmc 
1328457Sbmc 	ap = dt_stmt_action(dtp, sdp);
1329457Sbmc 	assert(fid->di_kind == DT_IDENT_AGGFUNC);
1330457Sbmc 	assert(DTRACEACT_ISAGG(fid->di_id));
1331457Sbmc 	ap->dtad_kind = fid->di_id;
1332457Sbmc 	ap->dtad_ntuple = n;
1333457Sbmc 	ap->dtad_arg = arg;
1334457Sbmc 
1335457Sbmc 	if (dnp->dn_aggfun->dn_args != NULL) {
1336457Sbmc 		dt_cg(yypcb, dnp->dn_aggfun->dn_args);
1337457Sbmc 		ap->dtad_difo = dt_as(yypcb);
13380Sstevel@tonic-gate 	}
13390Sstevel@tonic-gate }
13400Sstevel@tonic-gate 
13410Sstevel@tonic-gate static void
13420Sstevel@tonic-gate dt_compile_one_clause(dtrace_hdl_t *dtp, dt_node_t *cnp, dt_node_t *pnp)
13430Sstevel@tonic-gate {
13440Sstevel@tonic-gate 	dtrace_ecbdesc_t *edp;
13450Sstevel@tonic-gate 	dtrace_stmtdesc_t *sdp;
13460Sstevel@tonic-gate 	dt_node_t *dnp;
13470Sstevel@tonic-gate 
13480Sstevel@tonic-gate 	yylineno = pnp->dn_line;
13490Sstevel@tonic-gate 	dt_setcontext(dtp, pnp->dn_desc);
13500Sstevel@tonic-gate 	(void) dt_node_cook(cnp, DT_IDFLG_REF);
13510Sstevel@tonic-gate 
13520Sstevel@tonic-gate 	if (DT_TREEDUMP_PASS(dtp, 2))
13530Sstevel@tonic-gate 		dt_node_printr(cnp, stderr, 0);
13540Sstevel@tonic-gate 
1355265Smws 	if ((edp = dt_ecbdesc_create(dtp, pnp->dn_desc)) == NULL)
13560Sstevel@tonic-gate 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
13570Sstevel@tonic-gate 
13580Sstevel@tonic-gate 	assert(yypcb->pcb_ecbdesc == NULL);
13590Sstevel@tonic-gate 	yypcb->pcb_ecbdesc = edp;
13600Sstevel@tonic-gate 
13610Sstevel@tonic-gate 	if (cnp->dn_pred != NULL) {
13620Sstevel@tonic-gate 		dt_cg(yypcb, cnp->dn_pred);
13630Sstevel@tonic-gate 		edp->dted_pred.dtpdd_difo = dt_as(yypcb);
13640Sstevel@tonic-gate 	}
13650Sstevel@tonic-gate 
13660Sstevel@tonic-gate 	if (cnp->dn_acts == NULL) {
13670Sstevel@tonic-gate 		dt_stmt_append(dt_stmt_create(dtp, edp,
13680Sstevel@tonic-gate 		    cnp->dn_ctxattr, _dtrace_defattr), cnp);
13690Sstevel@tonic-gate 	}
13700Sstevel@tonic-gate 
13710Sstevel@tonic-gate 	for (dnp = cnp->dn_acts; dnp != NULL; dnp = dnp->dn_list) {
13720Sstevel@tonic-gate 		assert(yypcb->pcb_stmt == NULL);
13730Sstevel@tonic-gate 		sdp = dt_stmt_create(dtp, edp, cnp->dn_ctxattr, cnp->dn_attr);
13740Sstevel@tonic-gate 
13750Sstevel@tonic-gate 		switch (dnp->dn_kind) {
13760Sstevel@tonic-gate 		case DT_NODE_DEXPR:
13770Sstevel@tonic-gate 			if (dnp->dn_expr->dn_kind == DT_NODE_AGG)
13780Sstevel@tonic-gate 				dt_compile_agg(dtp, dnp->dn_expr, sdp);
13790Sstevel@tonic-gate 			else
13800Sstevel@tonic-gate 				dt_compile_exp(dtp, dnp, sdp);
13810Sstevel@tonic-gate 			break;
13820Sstevel@tonic-gate 		case DT_NODE_DFUNC:
13830Sstevel@tonic-gate 			dt_compile_fun(dtp, dnp, sdp);
13840Sstevel@tonic-gate 			break;
13850Sstevel@tonic-gate 		case DT_NODE_AGG:
13860Sstevel@tonic-gate 			dt_compile_agg(dtp, dnp, sdp);
13870Sstevel@tonic-gate 			break;
13880Sstevel@tonic-gate 		default:
13890Sstevel@tonic-gate 			dnerror(dnp, D_UNKNOWN, "internal error -- node kind "
13900Sstevel@tonic-gate 			    "%u is not a valid statement\n", dnp->dn_kind);
13910Sstevel@tonic-gate 		}
13920Sstevel@tonic-gate 
13930Sstevel@tonic-gate 		assert(yypcb->pcb_stmt == sdp);
13940Sstevel@tonic-gate 		dt_stmt_append(sdp, dnp);
13950Sstevel@tonic-gate 	}
13960Sstevel@tonic-gate 
13970Sstevel@tonic-gate 	assert(yypcb->pcb_ecbdesc == edp);
1398265Smws 	dt_ecbdesc_release(dtp, edp);
13990Sstevel@tonic-gate 	dt_endcontext(dtp);
14000Sstevel@tonic-gate 	yypcb->pcb_ecbdesc = NULL;
14010Sstevel@tonic-gate }
14020Sstevel@tonic-gate 
14030Sstevel@tonic-gate static void
14040Sstevel@tonic-gate dt_compile_clause(dtrace_hdl_t *dtp, dt_node_t *cnp)
14050Sstevel@tonic-gate {
14060Sstevel@tonic-gate 	dt_node_t *pnp;
14070Sstevel@tonic-gate 
14080Sstevel@tonic-gate 	for (pnp = cnp->dn_pdescs; pnp != NULL; pnp = pnp->dn_list)
14090Sstevel@tonic-gate 		dt_compile_one_clause(dtp, cnp, pnp);
14100Sstevel@tonic-gate }
14110Sstevel@tonic-gate 
1412265Smws static void
1413265Smws dt_compile_xlator(dt_node_t *dnp)
1414265Smws {
1415265Smws 	dt_xlator_t *dxp = dnp->dn_xlator;
1416265Smws 	dt_node_t *mnp;
1417265Smws 
1418265Smws 	for (mnp = dnp->dn_members; mnp != NULL; mnp = mnp->dn_list) {
1419265Smws 		assert(dxp->dx_membdif[mnp->dn_membid] == NULL);
1420265Smws 		dt_cg(yypcb, mnp);
1421265Smws 		dxp->dx_membdif[mnp->dn_membid] = dt_as(yypcb);
1422265Smws 	}
1423265Smws }
1424265Smws 
14250Sstevel@tonic-gate void
14260Sstevel@tonic-gate dt_setcontext(dtrace_hdl_t *dtp, dtrace_probedesc_t *pdp)
14270Sstevel@tonic-gate {
14280Sstevel@tonic-gate 	const dtrace_pattr_t *pap;
14290Sstevel@tonic-gate 	dt_probe_t *prp;
14300Sstevel@tonic-gate 	dt_ident_t *idp;
14310Sstevel@tonic-gate 	char attrstr[8];
14320Sstevel@tonic-gate 	int err;
14330Sstevel@tonic-gate 
14340Sstevel@tonic-gate 	/*
14350Sstevel@tonic-gate 	 * If the provider name ends with what could be interpreted as a
14360Sstevel@tonic-gate 	 * number, we assume that it's a pid and that we may need to
14370Sstevel@tonic-gate 	 * dynamically create those probes for that process.
14380Sstevel@tonic-gate 	 */
14390Sstevel@tonic-gate 	if (isdigit(pdp->dtpd_provider[strlen(pdp->dtpd_provider) - 1]))
14400Sstevel@tonic-gate 		dt_pid_create_probes(pdp, dtp);
14410Sstevel@tonic-gate 
14420Sstevel@tonic-gate 	/*
14430Sstevel@tonic-gate 	 * Call dt_probe_info() to get the probe arguments and attributes.  If
14440Sstevel@tonic-gate 	 * a representative probe is found, set 'pap' to the probe provider's
14450Sstevel@tonic-gate 	 * attributes.  Otherwise set 'pap' to default Unstable attributes.
14460Sstevel@tonic-gate 	 */
14470Sstevel@tonic-gate 	if ((prp = dt_probe_info(dtp, pdp, &yypcb->pcb_pinfo)) == NULL) {
14480Sstevel@tonic-gate 		pap = &_dtrace_prvdesc;
14490Sstevel@tonic-gate 		err = dtrace_errno(dtp);
14500Sstevel@tonic-gate 		bzero(&yypcb->pcb_pinfo, sizeof (dtrace_probeinfo_t));
14510Sstevel@tonic-gate 		yypcb->pcb_pinfo.dtp_attr = pap->dtpa_provider;
14520Sstevel@tonic-gate 		yypcb->pcb_pinfo.dtp_arga = pap->dtpa_args;
14530Sstevel@tonic-gate 	} else {
14540Sstevel@tonic-gate 		pap = &prp->pr_pvp->pv_desc.dtvd_attr;
14550Sstevel@tonic-gate 		err = 0;
14560Sstevel@tonic-gate 	}
14570Sstevel@tonic-gate 
14580Sstevel@tonic-gate 	if (err == EDT_NOPROBE && !(yypcb->pcb_cflags & DTRACE_C_ZDEFS)) {
14590Sstevel@tonic-gate 		xyerror(D_PDESC_ZERO, "probe description %s:%s:%s:%s does not "
14600Sstevel@tonic-gate 		    "match any probes\n", pdp->dtpd_provider, pdp->dtpd_mod,
14610Sstevel@tonic-gate 		    pdp->dtpd_func, pdp->dtpd_name);
14620Sstevel@tonic-gate 	}
14630Sstevel@tonic-gate 
14640Sstevel@tonic-gate 	if (err != EDT_NOPROBE && err != EDT_UNSTABLE && err != 0)
14650Sstevel@tonic-gate 		xyerror(D_PDESC_INVAL, "%s\n", dtrace_errmsg(dtp, err));
14660Sstevel@tonic-gate 
14670Sstevel@tonic-gate 	dt_dprintf("set context to %s:%s:%s:%s [%u] prp=%p attr=%s argc=%d\n",
14680Sstevel@tonic-gate 	    pdp->dtpd_provider, pdp->dtpd_mod, pdp->dtpd_func, pdp->dtpd_name,
14690Sstevel@tonic-gate 	    pdp->dtpd_id, (void *)prp, dt_attr_str(yypcb->pcb_pinfo.dtp_attr,
14700Sstevel@tonic-gate 	    attrstr, sizeof (attrstr)), yypcb->pcb_pinfo.dtp_argc);
14710Sstevel@tonic-gate 
14720Sstevel@tonic-gate 	/*
14730Sstevel@tonic-gate 	 * Reset the stability attributes of D global variables that vary
14740Sstevel@tonic-gate 	 * based on the attributes of the provider and context itself.
14750Sstevel@tonic-gate 	 */
14760Sstevel@tonic-gate 	if ((idp = dt_idhash_lookup(dtp->dt_globals, "probeprov")) != NULL)
14770Sstevel@tonic-gate 		idp->di_attr = pap->dtpa_provider;
14780Sstevel@tonic-gate 	if ((idp = dt_idhash_lookup(dtp->dt_globals, "probemod")) != NULL)
14790Sstevel@tonic-gate 		idp->di_attr = pap->dtpa_mod;
14800Sstevel@tonic-gate 	if ((idp = dt_idhash_lookup(dtp->dt_globals, "probefunc")) != NULL)
14810Sstevel@tonic-gate 		idp->di_attr = pap->dtpa_func;
14820Sstevel@tonic-gate 	if ((idp = dt_idhash_lookup(dtp->dt_globals, "probename")) != NULL)
14830Sstevel@tonic-gate 		idp->di_attr = pap->dtpa_name;
14840Sstevel@tonic-gate 	if ((idp = dt_idhash_lookup(dtp->dt_globals, "args")) != NULL)
14850Sstevel@tonic-gate 		idp->di_attr = pap->dtpa_args;
14860Sstevel@tonic-gate 
14870Sstevel@tonic-gate 	yypcb->pcb_pdesc = pdp;
14880Sstevel@tonic-gate 	yypcb->pcb_probe = prp;
14890Sstevel@tonic-gate }
14900Sstevel@tonic-gate 
14910Sstevel@tonic-gate /*
14920Sstevel@tonic-gate  * Reset context-dependent variables and state at the end of cooking a D probe
14930Sstevel@tonic-gate  * definition clause.  This ensures that external declarations between clauses
14940Sstevel@tonic-gate  * do not reference any stale context-dependent data from the previous clause.
14950Sstevel@tonic-gate  */
14960Sstevel@tonic-gate void
14970Sstevel@tonic-gate dt_endcontext(dtrace_hdl_t *dtp)
14980Sstevel@tonic-gate {
14990Sstevel@tonic-gate 	static const char *const cvars[] = {
15000Sstevel@tonic-gate 		"probeprov", "probemod", "probefunc", "probename", "args", NULL
15010Sstevel@tonic-gate 	};
15020Sstevel@tonic-gate 
15030Sstevel@tonic-gate 	dt_ident_t *idp;
15040Sstevel@tonic-gate 	int i;
15050Sstevel@tonic-gate 
15060Sstevel@tonic-gate 	for (i = 0; cvars[i] != NULL; i++) {
15070Sstevel@tonic-gate 		if ((idp = dt_idhash_lookup(dtp->dt_globals, cvars[i])) != NULL)
15080Sstevel@tonic-gate 			idp->di_attr = _dtrace_defattr;
15090Sstevel@tonic-gate 	}
15100Sstevel@tonic-gate 
15110Sstevel@tonic-gate 	yypcb->pcb_pdesc = NULL;
15120Sstevel@tonic-gate 	yypcb->pcb_probe = NULL;
15130Sstevel@tonic-gate }
15140Sstevel@tonic-gate 
15150Sstevel@tonic-gate static int
15160Sstevel@tonic-gate dt_reduceid(dt_idhash_t *dhp, dt_ident_t *idp, dtrace_hdl_t *dtp)
15170Sstevel@tonic-gate {
15180Sstevel@tonic-gate 	if (idp->di_vers != 0 && idp->di_vers > dtp->dt_vmax)
15190Sstevel@tonic-gate 		dt_idhash_delete(dhp, idp);
15200Sstevel@tonic-gate 
15210Sstevel@tonic-gate 	return (0);
15220Sstevel@tonic-gate }
15230Sstevel@tonic-gate 
15240Sstevel@tonic-gate /*
15250Sstevel@tonic-gate  * When dtrace_setopt() is called for "version", it calls dt_reduce() to remove
15260Sstevel@tonic-gate  * any identifiers or translators that have been previously defined as bound to
15270Sstevel@tonic-gate  * a version greater than the specified version.  Therefore, in our current
15280Sstevel@tonic-gate  * version implementation, establishing a binding is a one-way transformation.
15290Sstevel@tonic-gate  * In addition, no versioning is currently provided for types as our .d library
15300Sstevel@tonic-gate  * files do not define any types and we reserve prefixes DTRACE_ and dtrace_
15310Sstevel@tonic-gate  * for our exclusive use.  If required, type versioning will require more work.
15320Sstevel@tonic-gate  */
15330Sstevel@tonic-gate int
15340Sstevel@tonic-gate dt_reduce(dtrace_hdl_t *dtp, dt_version_t v)
15350Sstevel@tonic-gate {
15360Sstevel@tonic-gate 	char s[DT_VERSION_STRMAX];
15370Sstevel@tonic-gate 	dt_xlator_t *dxp, *nxp;
15380Sstevel@tonic-gate 
15390Sstevel@tonic-gate 	if (v > dtp->dt_vmax)
15400Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_VERSREDUCED));
15410Sstevel@tonic-gate 	else if (v == dtp->dt_vmax)
15420Sstevel@tonic-gate 		return (0); /* no reduction necessary */
15430Sstevel@tonic-gate 
15440Sstevel@tonic-gate 	dt_dprintf("reducing api version to %s\n",
15450Sstevel@tonic-gate 	    dt_version_num2str(v, s, sizeof (s)));
15460Sstevel@tonic-gate 
15470Sstevel@tonic-gate 	dtp->dt_vmax = v;
15480Sstevel@tonic-gate 
15490Sstevel@tonic-gate 	for (dxp = dt_list_next(&dtp->dt_xlators); dxp != NULL; dxp = nxp) {
15500Sstevel@tonic-gate 		nxp = dt_list_next(dxp);
15510Sstevel@tonic-gate 		if ((dxp->dx_souid.di_vers != 0 && dxp->dx_souid.di_vers > v) ||
15520Sstevel@tonic-gate 		    (dxp->dx_ptrid.di_vers != 0 && dxp->dx_ptrid.di_vers > v))
15530Sstevel@tonic-gate 			dt_list_delete(&dtp->dt_xlators, dxp);
15540Sstevel@tonic-gate 	}
15550Sstevel@tonic-gate 
15560Sstevel@tonic-gate 	(void) dt_idhash_iter(dtp->dt_macros, (dt_idhash_f *)dt_reduceid, dtp);
15570Sstevel@tonic-gate 	(void) dt_idhash_iter(dtp->dt_aggs, (dt_idhash_f *)dt_reduceid, dtp);
15580Sstevel@tonic-gate 	(void) dt_idhash_iter(dtp->dt_globals, (dt_idhash_f *)dt_reduceid, dtp);
15590Sstevel@tonic-gate 	(void) dt_idhash_iter(dtp->dt_tls, (dt_idhash_f *)dt_reduceid, dtp);
15600Sstevel@tonic-gate 
15610Sstevel@tonic-gate 	return (0);
15620Sstevel@tonic-gate }
15630Sstevel@tonic-gate 
15640Sstevel@tonic-gate /*
15650Sstevel@tonic-gate  * Fork and exec the cpp(1) preprocessor to run over the specified input file,
15660Sstevel@tonic-gate  * and return a FILE handle for the cpp output.  We use the /dev/fd filesystem
15670Sstevel@tonic-gate  * here to simplify the code by leveraging file descriptor inheritance.
15680Sstevel@tonic-gate  */
15690Sstevel@tonic-gate static FILE *
15700Sstevel@tonic-gate dt_preproc(dtrace_hdl_t *dtp, FILE *ifp)
15710Sstevel@tonic-gate {
15720Sstevel@tonic-gate 	int argc = dtp->dt_cpp_argc;
15730Sstevel@tonic-gate 	char **argv = malloc(sizeof (char *) * (argc + 5));
15740Sstevel@tonic-gate 	FILE *ofp = tmpfile();
15750Sstevel@tonic-gate 
15760Sstevel@tonic-gate 	char ipath[20], opath[20]; /* big enough for /dev/fd/ + INT_MAX + \0 */
15770Sstevel@tonic-gate 	char verdef[32]; /* big enough for -D__SUNW_D_VERSION=0x%08x + \0 */
15780Sstevel@tonic-gate 
15790Sstevel@tonic-gate 	struct sigaction act, oact;
15800Sstevel@tonic-gate 	sigset_t mask, omask;
15810Sstevel@tonic-gate 
15820Sstevel@tonic-gate 	int wstat, estat;
15830Sstevel@tonic-gate 	pid_t pid;
15840Sstevel@tonic-gate 	off64_t off;
15850Sstevel@tonic-gate 	int c;
15860Sstevel@tonic-gate 
15870Sstevel@tonic-gate 	if (argv == NULL || ofp == NULL) {
15880Sstevel@tonic-gate 		(void) dt_set_errno(dtp, errno);
15890Sstevel@tonic-gate 		goto err;
15900Sstevel@tonic-gate 	}
15910Sstevel@tonic-gate 
15920Sstevel@tonic-gate 	/*
15930Sstevel@tonic-gate 	 * If the input is a seekable file, see if it is an interpreter file.
15940Sstevel@tonic-gate 	 * If we see #!, seek past the first line because cpp will choke on it.
15950Sstevel@tonic-gate 	 * We start cpp just prior to the \n at the end of this line so that
15960Sstevel@tonic-gate 	 * it still sees the newline, ensuring that #line values are correct.
15970Sstevel@tonic-gate 	 */
15980Sstevel@tonic-gate 	if (isatty(fileno(ifp)) == 0 && (off = ftello64(ifp)) != -1) {
15990Sstevel@tonic-gate 		if ((c = fgetc(ifp)) == '#' && (c = fgetc(ifp)) == '!') {
16000Sstevel@tonic-gate 			for (off += 2; c != '\n'; off++) {
16010Sstevel@tonic-gate 				if ((c = fgetc(ifp)) == EOF)
16020Sstevel@tonic-gate 					break;
16030Sstevel@tonic-gate 			}
16040Sstevel@tonic-gate 			if (c == '\n')
16050Sstevel@tonic-gate 				off--; /* start cpp just prior to \n */
16060Sstevel@tonic-gate 		}
16070Sstevel@tonic-gate 		(void) fflush(ifp);
16080Sstevel@tonic-gate 		(void) fseeko64(ifp, off, SEEK_SET);
16090Sstevel@tonic-gate 	}
16100Sstevel@tonic-gate 
16110Sstevel@tonic-gate 	(void) snprintf(ipath, sizeof (ipath), "/dev/fd/%d", fileno(ifp));
16120Sstevel@tonic-gate 	(void) snprintf(opath, sizeof (opath), "/dev/fd/%d", fileno(ofp));
16130Sstevel@tonic-gate 
16140Sstevel@tonic-gate 	bcopy(dtp->dt_cpp_argv, argv, sizeof (char *) * argc);
16150Sstevel@tonic-gate 
16160Sstevel@tonic-gate 	(void) snprintf(verdef, sizeof (verdef),
16170Sstevel@tonic-gate 	    "-D__SUNW_D_VERSION=0x%08x", dtp->dt_vmax);
16180Sstevel@tonic-gate 	argv[argc++] = verdef;
16190Sstevel@tonic-gate 
16200Sstevel@tonic-gate 	switch (dtp->dt_stdcmode) {
16210Sstevel@tonic-gate 	case DT_STDC_XA:
16220Sstevel@tonic-gate 	case DT_STDC_XT:
16230Sstevel@tonic-gate 		argv[argc++] = "-D__STDC__=0";
16240Sstevel@tonic-gate 		break;
16250Sstevel@tonic-gate 	case DT_STDC_XC:
16260Sstevel@tonic-gate 		argv[argc++] = "-D__STDC__=1";
16270Sstevel@tonic-gate 		break;
16280Sstevel@tonic-gate 	}
16290Sstevel@tonic-gate 
16300Sstevel@tonic-gate 	argv[argc++] = ipath;
16310Sstevel@tonic-gate 	argv[argc++] = opath;
16320Sstevel@tonic-gate 	argv[argc] = NULL;
16330Sstevel@tonic-gate 
16340Sstevel@tonic-gate 	/*
16350Sstevel@tonic-gate 	 * libdtrace must be able to be embedded in other programs that may
16360Sstevel@tonic-gate 	 * include application-specific signal handlers.  Therefore, if we
16370Sstevel@tonic-gate 	 * need to fork to run cpp(1), we must avoid generating a SIGCHLD
16380Sstevel@tonic-gate 	 * that could confuse the containing application.  To do this,
16390Sstevel@tonic-gate 	 * we block SIGCHLD and reset its disposition to SIG_DFL.
16400Sstevel@tonic-gate 	 * We restore our signal state once we are done.
16410Sstevel@tonic-gate 	 */
16420Sstevel@tonic-gate 	(void) sigemptyset(&mask);
16430Sstevel@tonic-gate 	(void) sigaddset(&mask, SIGCHLD);
16440Sstevel@tonic-gate 	(void) sigprocmask(SIG_BLOCK, &mask, &omask);
16450Sstevel@tonic-gate 
16460Sstevel@tonic-gate 	bzero(&act, sizeof (act));
16470Sstevel@tonic-gate 	act.sa_handler = SIG_DFL;
16480Sstevel@tonic-gate 	(void) sigaction(SIGCHLD, &act, &oact);
16490Sstevel@tonic-gate 
16500Sstevel@tonic-gate 	if ((pid = fork1()) == -1) {
16510Sstevel@tonic-gate 		(void) sigaction(SIGCHLD, &oact, NULL);
16520Sstevel@tonic-gate 		(void) sigprocmask(SIG_SETMASK, &omask, NULL);
16530Sstevel@tonic-gate 		(void) dt_set_errno(dtp, EDT_CPPFORK);
16540Sstevel@tonic-gate 		goto err;
16550Sstevel@tonic-gate 	}
16560Sstevel@tonic-gate 
16570Sstevel@tonic-gate 	if (pid == 0) {
16580Sstevel@tonic-gate 		(void) execvp(dtp->dt_cpp_path, argv);
16590Sstevel@tonic-gate 		_exit(errno == ENOENT ? 127 : 126);
16600Sstevel@tonic-gate 	}
16610Sstevel@tonic-gate 
16620Sstevel@tonic-gate 	do {
16630Sstevel@tonic-gate 		dt_dprintf("waiting for %s (PID %d)\n", dtp->dt_cpp_path,
16640Sstevel@tonic-gate 		    (int)pid);
16650Sstevel@tonic-gate 	} while (waitpid(pid, &wstat, 0) == -1 && errno == EINTR);
16660Sstevel@tonic-gate 
16670Sstevel@tonic-gate 	(void) sigaction(SIGCHLD, &oact, NULL);
16680Sstevel@tonic-gate 	(void) sigprocmask(SIG_SETMASK, &omask, NULL);
16690Sstevel@tonic-gate 
16700Sstevel@tonic-gate 	dt_dprintf("%s returned exit status 0x%x\n", dtp->dt_cpp_path, wstat);
16710Sstevel@tonic-gate 	estat = WIFEXITED(wstat) ? WEXITSTATUS(wstat) : -1;
16720Sstevel@tonic-gate 
16730Sstevel@tonic-gate 	if (estat != 0) {
16740Sstevel@tonic-gate 		switch (estat) {
16750Sstevel@tonic-gate 		case 126:
16760Sstevel@tonic-gate 			(void) dt_set_errno(dtp, EDT_CPPEXEC);
16770Sstevel@tonic-gate 			break;
16780Sstevel@tonic-gate 		case 127:
16790Sstevel@tonic-gate 			(void) dt_set_errno(dtp, EDT_CPPENT);
16800Sstevel@tonic-gate 			break;
16810Sstevel@tonic-gate 		default:
16820Sstevel@tonic-gate 			(void) dt_set_errno(dtp, EDT_CPPERR);
16830Sstevel@tonic-gate 		}
16840Sstevel@tonic-gate 		goto err;
16850Sstevel@tonic-gate 	}
16860Sstevel@tonic-gate 
16870Sstevel@tonic-gate 	free(argv);
16880Sstevel@tonic-gate 	(void) fflush(ofp);
16890Sstevel@tonic-gate 	(void) fseek(ofp, 0, SEEK_SET);
16900Sstevel@tonic-gate 	return (ofp);
16910Sstevel@tonic-gate 
16920Sstevel@tonic-gate err:
16930Sstevel@tonic-gate 	free(argv);
16940Sstevel@tonic-gate 	(void) fclose(ofp);
16950Sstevel@tonic-gate 	return (NULL);
16960Sstevel@tonic-gate }
16970Sstevel@tonic-gate 
16980Sstevel@tonic-gate /*
16990Sstevel@tonic-gate  * Open all of the .d library files found in the specified directory and try to
17000Sstevel@tonic-gate  * compile each one in order to cache its inlines and translators, etc.  We
17010Sstevel@tonic-gate  * silently ignore any missing directories and other files found therein.
17020Sstevel@tonic-gate  * We only fail (and thereby fail dt_load_libs()) if we fail to compile a
17030Sstevel@tonic-gate  * library and the error is something other than #pragma D depends_on.
17040Sstevel@tonic-gate  * Dependency errors are silently ignored to permit a library directory to
17050Sstevel@tonic-gate  * contain libraries which may not be accessible depending on our privileges.
17060Sstevel@tonic-gate  *
17070Sstevel@tonic-gate  * Note that at present, no ordering is defined between library files found in
17080Sstevel@tonic-gate  * the same directory: if cross-library dependencies are eventually required,
17090Sstevel@tonic-gate  * we will need to extend the #pragma D depends_on directive with an additional
17100Sstevel@tonic-gate  * class for libraries, and this function will need to create a graph of the
17110Sstevel@tonic-gate  * various library pathnames and then perform a topological ordering using the
17120Sstevel@tonic-gate  * dependency information before we attempt to compile any of them.
17130Sstevel@tonic-gate  */
17140Sstevel@tonic-gate static int
17150Sstevel@tonic-gate dt_load_libs_dir(dtrace_hdl_t *dtp, const char *path)
17160Sstevel@tonic-gate {
1717871Scasper 	struct dirent *dp;
17180Sstevel@tonic-gate 	const char *p;
17190Sstevel@tonic-gate 	DIR *dirp;
17200Sstevel@tonic-gate 
17210Sstevel@tonic-gate 	char fname[PATH_MAX];
17220Sstevel@tonic-gate 	dtrace_prog_t *pgp;
17230Sstevel@tonic-gate 	FILE *fp;
17240Sstevel@tonic-gate 
17250Sstevel@tonic-gate 	if ((dirp = opendir(path)) == NULL) {
17260Sstevel@tonic-gate 		dt_dprintf("skipping lib dir %s: %s\n", path, strerror(errno));
17270Sstevel@tonic-gate 		return (0);
17280Sstevel@tonic-gate 	}
17290Sstevel@tonic-gate 
1730871Scasper 	while ((dp = readdir(dirp)) != NULL) {
17310Sstevel@tonic-gate 		if ((p = strrchr(dp->d_name, '.')) == NULL || strcmp(p, ".d"))
17320Sstevel@tonic-gate 			continue; /* skip any filename not ending in .d */
17330Sstevel@tonic-gate 
17340Sstevel@tonic-gate 		(void) snprintf(fname, sizeof (fname),
17350Sstevel@tonic-gate 		    "%s/%s", path, dp->d_name);
17360Sstevel@tonic-gate 
17370Sstevel@tonic-gate 		if ((fp = fopen(fname, "r")) == NULL) {
17380Sstevel@tonic-gate 			dt_dprintf("skipping library %s: %s\n",
17390Sstevel@tonic-gate 			    fname, strerror(errno));
17400Sstevel@tonic-gate 			continue;
17410Sstevel@tonic-gate 		}
17420Sstevel@tonic-gate 
17430Sstevel@tonic-gate 		dtp->dt_filetag = fname;
17440Sstevel@tonic-gate 		pgp = dtrace_program_fcompile(dtp, fp, DTRACE_C_EMPTY, 0, NULL);
17450Sstevel@tonic-gate 		(void) fclose(fp);
17460Sstevel@tonic-gate 		dtp->dt_filetag = NULL;
17470Sstevel@tonic-gate 
17480Sstevel@tonic-gate 		if (pgp == NULL && (dtp->dt_errno != EDT_COMPILER ||
17490Sstevel@tonic-gate 		    dtp->dt_errtag != dt_errtag(D_PRAGMA_DEPEND))) {
17500Sstevel@tonic-gate 			(void) closedir(dirp);
17510Sstevel@tonic-gate 			return (-1); /* preserve dt_errno */
17520Sstevel@tonic-gate 		}
17530Sstevel@tonic-gate 
17540Sstevel@tonic-gate 		if (pgp == NULL) {
17550Sstevel@tonic-gate 			dt_dprintf("skipping library: %s\n",
17560Sstevel@tonic-gate 			    dtrace_errmsg(dtp, dtrace_errno(dtp)));
17570Sstevel@tonic-gate 		} else
1758265Smws 			dt_program_destroy(dtp, pgp);
17590Sstevel@tonic-gate 	}
17600Sstevel@tonic-gate 
17610Sstevel@tonic-gate 	(void) closedir(dirp);
17620Sstevel@tonic-gate 	return (0);
17630Sstevel@tonic-gate }
17640Sstevel@tonic-gate 
17650Sstevel@tonic-gate /*
17660Sstevel@tonic-gate  * Load the contents of any appropriate DTrace .d library files.  These files
17670Sstevel@tonic-gate  * contain inlines and translators that will be cached by the compiler.  We
17680Sstevel@tonic-gate  * defer this activity until the first compile to permit libdtrace clients to
17690Sstevel@tonic-gate  * add their own library directories and so that we can properly report errors.
17700Sstevel@tonic-gate  */
17710Sstevel@tonic-gate static int
17720Sstevel@tonic-gate dt_load_libs(dtrace_hdl_t *dtp)
17730Sstevel@tonic-gate {
17740Sstevel@tonic-gate 	dt_dirpath_t *dirp;
17750Sstevel@tonic-gate 
17760Sstevel@tonic-gate 	if (dtp->dt_cflags & DTRACE_C_NOLIBS)
17770Sstevel@tonic-gate 		return (0); /* libraries already processed */
17780Sstevel@tonic-gate 
17790Sstevel@tonic-gate 	dtp->dt_cflags |= DTRACE_C_NOLIBS;
17800Sstevel@tonic-gate 
17810Sstevel@tonic-gate 	for (dirp = dt_list_next(&dtp->dt_lib_path);
17820Sstevel@tonic-gate 	    dirp != NULL; dirp = dt_list_next(dirp)) {
17830Sstevel@tonic-gate 		if (dt_load_libs_dir(dtp, dirp->dir_path) != 0) {
17840Sstevel@tonic-gate 			dtp->dt_cflags &= ~DTRACE_C_NOLIBS;
17850Sstevel@tonic-gate 			return (-1); /* errno is set for us */
17860Sstevel@tonic-gate 		}
17870Sstevel@tonic-gate 	}
17880Sstevel@tonic-gate 
17890Sstevel@tonic-gate 	return (0);
17900Sstevel@tonic-gate }
17910Sstevel@tonic-gate 
17920Sstevel@tonic-gate static void *
17930Sstevel@tonic-gate dt_compile(dtrace_hdl_t *dtp, int context, dtrace_probespec_t pspec, void *arg,
17940Sstevel@tonic-gate     uint_t cflags, int argc, char *const argv[], FILE *fp, const char *s)
17950Sstevel@tonic-gate {
17960Sstevel@tonic-gate 	dt_node_t *dnp;
17970Sstevel@tonic-gate 	dt_decl_t *ddp;
17980Sstevel@tonic-gate 	dt_pcb_t pcb;
17990Sstevel@tonic-gate 	void *rv;
18000Sstevel@tonic-gate 	int err;
18010Sstevel@tonic-gate 
18020Sstevel@tonic-gate 	if ((fp == NULL && s == NULL) || (cflags & ~DTRACE_C_MASK) != 0) {
18030Sstevel@tonic-gate 		(void) dt_set_errno(dtp, EINVAL);
18040Sstevel@tonic-gate 		return (NULL);
18050Sstevel@tonic-gate 	}
18060Sstevel@tonic-gate 
18070Sstevel@tonic-gate 	if (dt_list_next(&dtp->dt_lib_path) != NULL && dt_load_libs(dtp) != 0)
18080Sstevel@tonic-gate 		return (NULL); /* errno is set for us */
18090Sstevel@tonic-gate 
18100Sstevel@tonic-gate 	(void) ctf_discard(dtp->dt_cdefs->dm_ctfp);
18110Sstevel@tonic-gate 	(void) ctf_discard(dtp->dt_ddefs->dm_ctfp);
18120Sstevel@tonic-gate 
18130Sstevel@tonic-gate 	(void) dt_idhash_iter(dtp->dt_globals, dt_idreset, NULL);
18140Sstevel@tonic-gate 	(void) dt_idhash_iter(dtp->dt_tls, dt_idreset, NULL);
18150Sstevel@tonic-gate 
18160Sstevel@tonic-gate 	if (fp && (cflags & DTRACE_C_CPP) && (fp = dt_preproc(dtp, fp)) == NULL)
18170Sstevel@tonic-gate 		return (NULL); /* errno is set for us */
18180Sstevel@tonic-gate 
18190Sstevel@tonic-gate 	dt_pcb_push(dtp, &pcb);
18200Sstevel@tonic-gate 
18210Sstevel@tonic-gate 	pcb.pcb_fileptr = fp;
18220Sstevel@tonic-gate 	pcb.pcb_string = s;
18230Sstevel@tonic-gate 	pcb.pcb_strptr = s;
18240Sstevel@tonic-gate 	pcb.pcb_strlen = s ? strlen(s) : 0;
18250Sstevel@tonic-gate 	pcb.pcb_sargc = argc;
18260Sstevel@tonic-gate 	pcb.pcb_sargv = argv;
18270Sstevel@tonic-gate 	pcb.pcb_sflagv = argc ? calloc(argc, sizeof (ushort_t)) : NULL;
18280Sstevel@tonic-gate 	pcb.pcb_pspec = pspec;
18290Sstevel@tonic-gate 	pcb.pcb_cflags = dtp->dt_cflags | cflags;
18300Sstevel@tonic-gate 	pcb.pcb_amin = dtp->dt_amin;
18310Sstevel@tonic-gate 	pcb.pcb_yystate = -1;
18320Sstevel@tonic-gate 	pcb.pcb_context = context;
18330Sstevel@tonic-gate 	pcb.pcb_token = context;
18340Sstevel@tonic-gate 
18350Sstevel@tonic-gate 	if (context == DT_CTX_DPROG)
18360Sstevel@tonic-gate 		yybegin(YYS_CLAUSE);
18370Sstevel@tonic-gate 	else
18380Sstevel@tonic-gate 		yybegin(YYS_EXPR);
18390Sstevel@tonic-gate 
18400Sstevel@tonic-gate 	if ((err = setjmp(yypcb->pcb_jmpbuf)) != 0)
18410Sstevel@tonic-gate 		goto out;
18420Sstevel@tonic-gate 
18430Sstevel@tonic-gate 	if (yypcb->pcb_sargc != 0 && yypcb->pcb_sflagv == NULL)
18440Sstevel@tonic-gate 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
18450Sstevel@tonic-gate 
18460Sstevel@tonic-gate 	yypcb->pcb_idents = dt_idhash_create("ambiguous", NULL, 0, 0);
18470Sstevel@tonic-gate 	yypcb->pcb_locals = dt_idhash_create("clause local", NULL,
18480Sstevel@tonic-gate 	    DIF_VAR_OTHER_UBASE, DIF_VAR_OTHER_MAX);
18490Sstevel@tonic-gate 
18500Sstevel@tonic-gate 	if (yypcb->pcb_idents == NULL || yypcb->pcb_locals == NULL)
18510Sstevel@tonic-gate 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
18520Sstevel@tonic-gate 
18530Sstevel@tonic-gate 	/*
18540Sstevel@tonic-gate 	 * Invoke the parser to evaluate the D source code.  If any errors
18550Sstevel@tonic-gate 	 * occur during parsing, an error function will be called and we
18560Sstevel@tonic-gate 	 * will longjmp back to pcb_jmpbuf to abort.  If parsing succeeds,
18570Sstevel@tonic-gate 	 * we optionally display the parse tree if debugging is enabled.
18580Sstevel@tonic-gate 	 */
18590Sstevel@tonic-gate 	if (yyparse() != 0 || yypcb->pcb_root == NULL)
18600Sstevel@tonic-gate 		xyerror(D_EMPTY, "empty D program translation unit\n");
18610Sstevel@tonic-gate 
18620Sstevel@tonic-gate 	yybegin(YYS_DONE);
18630Sstevel@tonic-gate 
18640Sstevel@tonic-gate 	if (context != DT_CTX_DTYPE && DT_TREEDUMP_PASS(dtp, 1))
18650Sstevel@tonic-gate 		dt_node_printr(yypcb->pcb_root, stderr, 0);
18660Sstevel@tonic-gate 
18670Sstevel@tonic-gate 	if (yypcb->pcb_pragmas != NULL)
18680Sstevel@tonic-gate 		(void) dt_idhash_iter(yypcb->pcb_pragmas, dt_idpragma, NULL);
18690Sstevel@tonic-gate 
18700Sstevel@tonic-gate 	if (argc > 1 && !(yypcb->pcb_cflags & DTRACE_C_ARGREF) &&
18710Sstevel@tonic-gate 	    !(yypcb->pcb_sflagv[argc - 1] & DT_IDFLG_REF)) {
18720Sstevel@tonic-gate 		xyerror(D_MACRO_UNUSED, "extraneous argument '%s' ($%d is "
18730Sstevel@tonic-gate 		    "not referenced)\n", yypcb->pcb_sargv[argc - 1], argc - 1);
18740Sstevel@tonic-gate 	}
18750Sstevel@tonic-gate 
18760Sstevel@tonic-gate 	/*
18770Sstevel@tonic-gate 	 * If we have successfully created a parse tree for a D program, loop
18780Sstevel@tonic-gate 	 * over the clauses and actions and instantiate the corresponding
18790Sstevel@tonic-gate 	 * libdtrace program.  If we are parsing a D expression, then we
18800Sstevel@tonic-gate 	 * simply run the code generator and assembler on the resulting tree.
18810Sstevel@tonic-gate 	 */
18820Sstevel@tonic-gate 	switch (context) {
18830Sstevel@tonic-gate 	case DT_CTX_DPROG:
18840Sstevel@tonic-gate 		assert(yypcb->pcb_root->dn_kind == DT_NODE_PROG);
18850Sstevel@tonic-gate 
18860Sstevel@tonic-gate 		if ((dnp = yypcb->pcb_root->dn_list) == NULL &&
18870Sstevel@tonic-gate 		    !(yypcb->pcb_cflags & DTRACE_C_EMPTY))
18880Sstevel@tonic-gate 			xyerror(D_EMPTY, "empty D program translation unit\n");
18890Sstevel@tonic-gate 
1890265Smws 		if ((yypcb->pcb_prog = dt_program_create(dtp)) == NULL)
18910Sstevel@tonic-gate 			longjmp(yypcb->pcb_jmpbuf, dtrace_errno(dtp));
18920Sstevel@tonic-gate 
18930Sstevel@tonic-gate 		for (; dnp != NULL; dnp = dnp->dn_list) {
18940Sstevel@tonic-gate 			switch (dnp->dn_kind) {
18950Sstevel@tonic-gate 			case DT_NODE_CLAUSE:
18960Sstevel@tonic-gate 				dt_compile_clause(dtp, dnp);
18970Sstevel@tonic-gate 				break;
1898265Smws 			case DT_NODE_XLATOR:
1899265Smws 				if (dtp->dt_xlatemode == DT_XL_DYNAMIC)
1900265Smws 					dt_compile_xlator(dnp);
1901265Smws 				break;
19020Sstevel@tonic-gate 			case DT_NODE_PROVIDER:
19030Sstevel@tonic-gate 				(void) dt_node_cook(dnp, DT_IDFLG_REF);
19040Sstevel@tonic-gate 				break;
19050Sstevel@tonic-gate 			}
19060Sstevel@tonic-gate 		}
19070Sstevel@tonic-gate 
1908265Smws 		yypcb->pcb_prog->dp_xrefs = yypcb->pcb_asxrefs;
1909265Smws 		yypcb->pcb_prog->dp_xrefslen = yypcb->pcb_asxreflen;
1910265Smws 		yypcb->pcb_asxrefs = NULL;
1911265Smws 		yypcb->pcb_asxreflen = 0;
1912265Smws 
1913265Smws 		rv = yypcb->pcb_prog;
19140Sstevel@tonic-gate 		break;
19150Sstevel@tonic-gate 
19160Sstevel@tonic-gate 	case DT_CTX_DEXPR:
19170Sstevel@tonic-gate 		(void) dt_node_cook(yypcb->pcb_root, DT_IDFLG_REF);
19180Sstevel@tonic-gate 		dt_cg(yypcb, yypcb->pcb_root);
19190Sstevel@tonic-gate 		rv = dt_as(yypcb);
19200Sstevel@tonic-gate 		break;
19210Sstevel@tonic-gate 
19220Sstevel@tonic-gate 	case DT_CTX_DTYPE:
19230Sstevel@tonic-gate 		ddp = (dt_decl_t *)yypcb->pcb_root; /* root is really a decl */
19240Sstevel@tonic-gate 		err = dt_decl_type(ddp, arg);
19250Sstevel@tonic-gate 		dt_decl_free(ddp);
19260Sstevel@tonic-gate 
19270Sstevel@tonic-gate 		if (err != 0)
19280Sstevel@tonic-gate 			longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
19290Sstevel@tonic-gate 
19300Sstevel@tonic-gate 		rv = NULL;
19310Sstevel@tonic-gate 		break;
19320Sstevel@tonic-gate 	}
19330Sstevel@tonic-gate 
19340Sstevel@tonic-gate out:
19350Sstevel@tonic-gate 	if (context != DT_CTX_DTYPE && DT_TREEDUMP_PASS(dtp, 3))
19360Sstevel@tonic-gate 		dt_node_printr(yypcb->pcb_root, stderr, 0);
19370Sstevel@tonic-gate 
19380Sstevel@tonic-gate 	if (dtp->dt_cdefs_fd != -1 && (ftruncate64(dtp->dt_cdefs_fd, 0) == -1 ||
19390Sstevel@tonic-gate 	    lseek64(dtp->dt_cdefs_fd, 0, SEEK_SET) == -1 ||
19400Sstevel@tonic-gate 	    ctf_write(dtp->dt_cdefs->dm_ctfp, dtp->dt_cdefs_fd) == CTF_ERR))
19410Sstevel@tonic-gate 		dt_dprintf("failed to update CTF cache: %s\n", strerror(errno));
19420Sstevel@tonic-gate 
19430Sstevel@tonic-gate 	if (dtp->dt_ddefs_fd != -1 && (ftruncate64(dtp->dt_ddefs_fd, 0) == -1 ||
19440Sstevel@tonic-gate 	    lseek64(dtp->dt_ddefs_fd, 0, SEEK_SET) == -1 ||
19450Sstevel@tonic-gate 	    ctf_write(dtp->dt_ddefs->dm_ctfp, dtp->dt_ddefs_fd) == CTF_ERR))
19460Sstevel@tonic-gate 		dt_dprintf("failed to update CTF cache: %s\n", strerror(errno));
19470Sstevel@tonic-gate 
19480Sstevel@tonic-gate 	if (yypcb->pcb_fileptr && (cflags & DTRACE_C_CPP))
19490Sstevel@tonic-gate 		(void) fclose(yypcb->pcb_fileptr); /* close dt_preproc() file */
19500Sstevel@tonic-gate 
19510Sstevel@tonic-gate 	dt_pcb_pop(dtp, err);
19520Sstevel@tonic-gate 	(void) dt_set_errno(dtp, err);
19530Sstevel@tonic-gate 	return (err ? NULL : rv);
19540Sstevel@tonic-gate }
19550Sstevel@tonic-gate 
19560Sstevel@tonic-gate dtrace_prog_t *
19570Sstevel@tonic-gate dtrace_program_strcompile(dtrace_hdl_t *dtp, const char *s,
19580Sstevel@tonic-gate     dtrace_probespec_t spec, uint_t cflags, int argc, char *const argv[])
19590Sstevel@tonic-gate {
19600Sstevel@tonic-gate 	return (dt_compile(dtp, DT_CTX_DPROG,
19610Sstevel@tonic-gate 	    spec, NULL, cflags, argc, argv, NULL, s));
19620Sstevel@tonic-gate }
19630Sstevel@tonic-gate 
19640Sstevel@tonic-gate dtrace_prog_t *
19650Sstevel@tonic-gate dtrace_program_fcompile(dtrace_hdl_t *dtp, FILE *fp,
19660Sstevel@tonic-gate     uint_t cflags, int argc, char *const argv[])
19670Sstevel@tonic-gate {
19680Sstevel@tonic-gate 	return (dt_compile(dtp, DT_CTX_DPROG,
19690Sstevel@tonic-gate 	    DTRACE_PROBESPEC_NAME, NULL, cflags, argc, argv, fp, NULL));
19700Sstevel@tonic-gate }
19710Sstevel@tonic-gate 
19720Sstevel@tonic-gate int
19730Sstevel@tonic-gate dtrace_type_strcompile(dtrace_hdl_t *dtp, const char *s, dtrace_typeinfo_t *dtt)
19740Sstevel@tonic-gate {
19750Sstevel@tonic-gate 	(void) dt_compile(dtp, DT_CTX_DTYPE,
19760Sstevel@tonic-gate 	    DTRACE_PROBESPEC_NONE, dtt, 0, 0, NULL, NULL, s);
19770Sstevel@tonic-gate 	return (dtp->dt_errno ? -1 : 0);
19780Sstevel@tonic-gate }
19790Sstevel@tonic-gate 
19800Sstevel@tonic-gate int
19810Sstevel@tonic-gate dtrace_type_fcompile(dtrace_hdl_t *dtp, FILE *fp, dtrace_typeinfo_t *dtt)
19820Sstevel@tonic-gate {
19830Sstevel@tonic-gate 	(void) dt_compile(dtp, DT_CTX_DTYPE,
19840Sstevel@tonic-gate 	    DTRACE_PROBESPEC_NONE, dtt, 0, 0, NULL, fp, NULL);
19850Sstevel@tonic-gate 	return (dtp->dt_errno ? -1 : 0);
19860Sstevel@tonic-gate }
1987