xref: /illumos-gate/usr/src/lib/libdtrace/common/dt_cc.c (revision 4de2612967d06c4fdbf524a62556a1e8118a006f)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * DTrace D Language Compiler
31  *
32  * The code in this source file implements the main engine for the D language
33  * compiler.  The driver routine for the compiler is dt_compile(), below.  The
34  * compiler operates on either stdio FILEs or in-memory strings as its input
35  * and can produce either dtrace_prog_t structures from a D program or a single
36  * dtrace_difo_t structure from a D expression.  Multiple entry points are
37  * provided as wrappers around dt_compile() for the various input/output pairs.
38  * The compiler itself is implemented across the following source files:
39  *
40  * dt_lex.l - lex scanner
41  * dt_grammar.y - yacc grammar
42  * dt_parser.c - parse tree creation and semantic checking
43  * dt_decl.c - declaration stack processing
44  * dt_xlator.c - D translator lookup and creation
45  * dt_ident.c - identifier and symbol table routines
46  * dt_pragma.c - #pragma processing and D pragmas
47  * dt_printf.c - D printf() and printa() argument checking and processing
48  * dt_cc.c - compiler driver and dtrace_prog_t construction
49  * dt_cg.c - DIF code generator
50  * dt_as.c - DIF assembler
51  * dt_dof.c - dtrace_prog_t -> DOF conversion
52  *
53  * Several other source files provide collections of utility routines used by
54  * these major files.  The compiler itself is implemented in multiple passes:
55  *
56  * (1) The input program is scanned and parsed by dt_lex.l and dt_grammar.y
57  *     and parse tree nodes are constructed using the routines in dt_parser.c.
58  *     This node construction pass is described further in dt_parser.c.
59  *
60  * (2) The parse tree is "cooked" by assigning each clause a context (see the
61  *     routine dt_setcontext(), below) based on its probe description and then
62  *     recursively descending the tree performing semantic checking.  The cook
63  *     routines are also implemented in dt_parser.c and described there.
64  *
65  * (3) For actions that are DIF expression statements, the DIF code generator
66  *     and assembler are invoked to create a finished DIFO for the statement.
67  *
68  * (4) The dtrace_prog_t data structures for the program clauses and actions
69  *     are built, containing pointers to any DIFOs created in step (3).
70  *
71  * (5) The caller invokes a routine in dt_dof.c to convert the finished program
72  *     into DOF format for use in anonymous tracing or enabling in the kernel.
73  *
74  * In the implementation, steps 2-4 are intertwined in that they are performed
75  * in order for each clause as part of a loop that executes over the clauses.
76  *
77  * The D compiler currently implements nearly no optimization.  The compiler
78  * implements integer constant folding as part of pass (1), and a set of very
79  * simple peephole optimizations as part of pass (3).  As with any C compiler,
80  * a large number of optimizations are possible on both the intermediate data
81  * structures and the generated DIF code.  These possibilities should be
82  * investigated in the context of whether they will have any substantive effect
83  * on the overall DTrace probe effect before they are undertaken.
84  */
85 
86 #include <sys/types.h>
87 #include <sys/wait.h>
88 
89 #include <assert.h>
90 #include <strings.h>
91 #include <signal.h>
92 #include <unistd.h>
93 #include <stdlib.h>
94 #include <stdio.h>
95 #include <errno.h>
96 #include <ucontext.h>
97 #include <limits.h>
98 #include <ctype.h>
99 #include <dirent.h>
100 #include <dt_module.h>
101 #include <dt_program.h>
102 #include <dt_provider.h>
103 #include <dt_printf.h>
104 #include <dt_pid.h>
105 #include <dt_grammar.h>
106 #include <dt_ident.h>
107 #include <dt_string.h>
108 #include <dt_impl.h>
109 
110 static const dtrace_diftype_t dt_void_rtype = {
111 	DIF_TYPE_CTF, CTF_K_INTEGER, 0, 0, 0
112 };
113 
114 static const dtrace_diftype_t dt_int_rtype = {
115 	DIF_TYPE_CTF, CTF_K_INTEGER, 0, 0, sizeof (uint64_t)
116 };
117 
118 /*ARGSUSED*/
119 static int
120 dt_idreset(dt_idhash_t *dhp, dt_ident_t *idp, void *ignored)
121 {
122 	idp->di_flags &= ~(DT_IDFLG_REF | DT_IDFLG_MOD |
123 	    DT_IDFLG_DIFR | DT_IDFLG_DIFW);
124 	return (0);
125 }
126 
127 /*ARGSUSED*/
128 static int
129 dt_idpragma(dt_idhash_t *dhp, dt_ident_t *idp, void *ignored)
130 {
131 	yylineno = idp->di_lineno;
132 	xyerror(D_PRAGMA_UNUSED, "unused #pragma %s\n", (char *)idp->di_iarg);
133 	return (0);
134 }
135 
136 static dtrace_stmtdesc_t *
137 dt_stmt_create(dtrace_hdl_t *dtp, dtrace_ecbdesc_t *edp,
138     dtrace_attribute_t descattr, dtrace_attribute_t stmtattr)
139 {
140 	dtrace_stmtdesc_t *sdp = dtrace_stmt_create(dtp, edp);
141 
142 	if (sdp == NULL)
143 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
144 
145 	assert(yypcb->pcb_stmt == NULL);
146 	yypcb->pcb_stmt = sdp;
147 
148 	sdp->dtsd_descattr = descattr;
149 	sdp->dtsd_stmtattr = stmtattr;
150 
151 	return (sdp);
152 }
153 
154 static dtrace_actdesc_t *
155 dt_stmt_action(dtrace_hdl_t *dtp, dtrace_stmtdesc_t *sdp)
156 {
157 	dtrace_actdesc_t *new;
158 
159 	if ((new = dtrace_stmt_action(dtp, sdp)) == NULL)
160 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
161 
162 	return (new);
163 }
164 
165 /*
166  * Utility function to determine if a given action description is destructive.
167  * The dtdo_destructive bit is set for us by the DIF assembler (see dt_as.c).
168  */
169 static int
170 dt_action_destructive(const dtrace_actdesc_t *ap)
171 {
172 	return (DTRACEACT_ISDESTRUCTIVE(ap->dtad_kind) || (ap->dtad_kind ==
173 	    DTRACEACT_DIFEXPR && ap->dtad_difo->dtdo_destructive));
174 }
175 
176 static void
177 dt_stmt_append(dtrace_stmtdesc_t *sdp, const dt_node_t *dnp)
178 {
179 	dtrace_ecbdesc_t *edp = sdp->dtsd_ecbdesc;
180 	dtrace_actdesc_t *ap, *tap;
181 	int commit = 0;
182 	int speculate = 0;
183 	int datarec = 0;
184 
185 	/*
186 	 * Make sure that the new statement jibes with the rest of the ECB.
187 	 */
188 	for (ap = edp->dted_action; ap != NULL; ap = ap->dtad_next) {
189 		if (ap->dtad_kind == DTRACEACT_COMMIT) {
190 			if (commit) {
191 				dnerror(dnp, D_COMM_COMM, "commit( ) may "
192 				    "not follow commit( )\n");
193 			}
194 
195 			if (datarec) {
196 				dnerror(dnp, D_COMM_DREC, "commit( ) may "
197 				    "not follow data-recording action(s)\n");
198 			}
199 
200 			for (tap = ap; tap != NULL; tap = tap->dtad_next) {
201 				if (!DTRACEACT_ISAGG(tap->dtad_kind))
202 					continue;
203 
204 				dnerror(dnp, D_AGG_COMM, "aggregating actions "
205 				    "may not follow commit( )\n");
206 			}
207 
208 			commit = 1;
209 			continue;
210 		}
211 
212 		if (ap->dtad_kind == DTRACEACT_SPECULATE) {
213 			if (speculate) {
214 				dnerror(dnp, D_SPEC_SPEC, "speculate( ) may "
215 				    "not follow speculate( )\n");
216 			}
217 
218 			if (commit) {
219 				dnerror(dnp, D_SPEC_COMM, "speculate( ) may "
220 				    "not follow commit( )\n");
221 			}
222 
223 			if (datarec) {
224 				dnerror(dnp, D_SPEC_DREC, "speculate( ) may "
225 				    "not follow data-recording action(s)\n");
226 			}
227 
228 			speculate = 1;
229 			continue;
230 		}
231 
232 		if (DTRACEACT_ISAGG(ap->dtad_kind)) {
233 			if (speculate) {
234 				dnerror(dnp, D_AGG_SPEC, "aggregating actions "
235 				    "may not follow speculate( )\n");
236 			}
237 
238 			datarec = 1;
239 			continue;
240 		}
241 
242 		if (speculate) {
243 			if (dt_action_destructive(ap)) {
244 				dnerror(dnp, D_ACT_SPEC, "destructive actions "
245 				    "may not follow speculate( )\n");
246 			}
247 
248 			if (ap->dtad_kind == DTRACEACT_EXIT) {
249 				dnerror(dnp, D_EXIT_SPEC, "exit( ) may not "
250 				    "follow speculate( )\n");
251 			}
252 		}
253 
254 		/*
255 		 * Exclude all non data-recording actions.
256 		 */
257 		if (dt_action_destructive(ap) ||
258 		    ap->dtad_kind == DTRACEACT_DISCARD)
259 			continue;
260 
261 		if (ap->dtad_kind == DTRACEACT_DIFEXPR &&
262 		    ap->dtad_difo->dtdo_rtype.dtdt_kind == DIF_TYPE_CTF &&
263 		    ap->dtad_difo->dtdo_rtype.dtdt_size == 0)
264 			continue;
265 
266 		if (commit) {
267 			dnerror(dnp, D_DREC_COMM, "data-recording actions "
268 			    "may not follow commit( )\n");
269 		}
270 
271 		if (!speculate)
272 			datarec = 1;
273 	}
274 
275 	if (dtrace_stmt_add(yypcb->pcb_hdl, yypcb->pcb_prog, sdp) != 0)
276 		longjmp(yypcb->pcb_jmpbuf, dtrace_errno(yypcb->pcb_hdl));
277 
278 	if (yypcb->pcb_stmt == sdp)
279 		yypcb->pcb_stmt = NULL;
280 }
281 
282 /*
283  * For the first element of an aggregation tuple or for printa(), we create a
284  * simple DIF program that simply returns the immediate value that is the ID
285  * of the aggregation itself.  This could be optimized in the future by
286  * creating a new in-kernel dtad_kind that just returns an integer.
287  */
288 static void
289 dt_action_difconst(dtrace_actdesc_t *ap, uint_t id, dtrace_actkind_t kind)
290 {
291 	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
292 	dtrace_difo_t *dp = dt_zalloc(dtp, sizeof (dtrace_difo_t));
293 
294 	if (dp == NULL)
295 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
296 
297 	dp->dtdo_buf = dt_alloc(dtp, sizeof (dif_instr_t) * 2);
298 	dp->dtdo_inttab = dt_alloc(dtp, sizeof (uint64_t));
299 
300 	if (dp->dtdo_buf == NULL || dp->dtdo_inttab == NULL) {
301 		dt_difo_free(dtp, dp);
302 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
303 	}
304 
305 	dp->dtdo_buf[0] = DIF_INSTR_SETX(0, 1); /* setx	DIF_INTEGER[0], %r1 */
306 	dp->dtdo_buf[1] = DIF_INSTR_RET(1);	/* ret	%r1 */
307 	dp->dtdo_len = 2;
308 	dp->dtdo_inttab[0] = id;
309 	dp->dtdo_intlen = 1;
310 	dp->dtdo_rtype = dt_int_rtype;
311 
312 	ap->dtad_difo = dp;
313 	ap->dtad_kind = kind;
314 }
315 
316 static void
317 dt_action_clear(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
318 {
319 	dt_ident_t *aid;
320 	dtrace_actdesc_t *ap;
321 	dt_node_t *anp;
322 
323 	char n[DT_TYPE_NAMELEN];
324 	int argc = 0;
325 
326 	for (anp = dnp->dn_args; anp != NULL; anp = anp->dn_list)
327 		argc++; /* count up arguments for error messages below */
328 
329 	if (argc != 1) {
330 		dnerror(dnp, D_CLEAR_PROTO,
331 		    "%s( ) prototype mismatch: %d args passed, 1 expected\n",
332 		    dnp->dn_ident->di_name, argc);
333 	}
334 
335 	anp = dnp->dn_args;
336 	assert(anp != NULL);
337 
338 	if (anp->dn_kind != DT_NODE_AGG) {
339 		dnerror(dnp, D_CLEAR_AGGARG,
340 		    "%s( ) argument #1 is incompatible with prototype:\n"
341 		    "\tprototype: aggregation\n\t argument: %s\n",
342 		    dnp->dn_ident->di_name,
343 		    dt_node_type_name(anp, n, sizeof (n)));
344 	}
345 
346 	aid = anp->dn_ident;
347 
348 	if (aid->di_gen == dtp->dt_gen && !(aid->di_flags & DT_IDFLG_MOD)) {
349 		dnerror(dnp, D_CLEAR_AGGBAD,
350 		    "undefined aggregation: @%s\n", aid->di_name);
351 	}
352 
353 	ap = dt_stmt_action(dtp, sdp);
354 	dt_action_difconst(ap, anp->dn_ident->di_id, DTRACEACT_LIBACT);
355 	ap->dtad_arg = DT_ACT_CLEAR;
356 }
357 
358 static void
359 dt_action_normalize(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
360 {
361 	dt_ident_t *aid;
362 	dtrace_actdesc_t *ap;
363 	dt_node_t *anp, *normal;
364 	int denormal = (strcmp(dnp->dn_ident->di_name, "denormalize") == 0);
365 
366 	char n[DT_TYPE_NAMELEN];
367 	int argc = 0;
368 
369 	for (anp = dnp->dn_args; anp != NULL; anp = anp->dn_list)
370 		argc++; /* count up arguments for error messages below */
371 
372 	if ((denormal && argc != 1) || (!denormal && argc != 2)) {
373 		dnerror(dnp, D_NORMALIZE_PROTO,
374 		    "%s( ) prototype mismatch: %d args passed, %d expected\n",
375 		    dnp->dn_ident->di_name, argc, denormal ? 1 : 2);
376 	}
377 
378 	anp = dnp->dn_args;
379 	assert(anp != NULL);
380 
381 	if (anp->dn_kind != DT_NODE_AGG) {
382 		dnerror(dnp, D_NORMALIZE_AGGARG,
383 		    "%s( ) argument #1 is incompatible with prototype:\n"
384 		    "\tprototype: aggregation\n\t argument: %s\n",
385 		    dnp->dn_ident->di_name,
386 		    dt_node_type_name(anp, n, sizeof (n)));
387 	}
388 
389 	if ((normal = anp->dn_list) != NULL && !dt_node_is_scalar(normal)) {
390 		dnerror(dnp, D_NORMALIZE_SCALAR,
391 		    "%s( ) argument #2 must be of scalar type\n",
392 		    dnp->dn_ident->di_name);
393 	}
394 
395 	aid = anp->dn_ident;
396 
397 	if (aid->di_gen == dtp->dt_gen && !(aid->di_flags & DT_IDFLG_MOD)) {
398 		dnerror(dnp, D_NORMALIZE_AGGBAD,
399 		    "undefined aggregation: @%s\n", aid->di_name);
400 	}
401 
402 	ap = dt_stmt_action(dtp, sdp);
403 	dt_action_difconst(ap, anp->dn_ident->di_id, DTRACEACT_LIBACT);
404 
405 	if (denormal) {
406 		ap->dtad_arg = DT_ACT_DENORMALIZE;
407 		return;
408 	}
409 
410 	ap->dtad_arg = DT_ACT_NORMALIZE;
411 
412 	assert(normal != NULL);
413 	ap = dt_stmt_action(dtp, sdp);
414 	dt_cg(yypcb, normal);
415 
416 	ap->dtad_difo = dt_as(yypcb);
417 	ap->dtad_kind = DTRACEACT_LIBACT;
418 	ap->dtad_arg = DT_ACT_NORMALIZE;
419 }
420 
421 static void
422 dt_action_trunc(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
423 {
424 	dt_ident_t *aid;
425 	dtrace_actdesc_t *ap;
426 	dt_node_t *anp, *trunc;
427 
428 	char n[DT_TYPE_NAMELEN];
429 	int argc = 0;
430 
431 	for (anp = dnp->dn_args; anp != NULL; anp = anp->dn_list)
432 		argc++; /* count up arguments for error messages below */
433 
434 	if (argc > 2 || argc < 1) {
435 		dnerror(dnp, D_TRUNC_PROTO,
436 		    "%s( ) prototype mismatch: %d args passed, %s expected\n",
437 		    dnp->dn_ident->di_name, argc,
438 		    argc < 1 ? "at least 1" : "no more than 2");
439 	}
440 
441 	anp = dnp->dn_args;
442 	assert(anp != NULL);
443 	trunc = anp->dn_list;
444 
445 	if (anp->dn_kind != DT_NODE_AGG) {
446 		dnerror(dnp, D_TRUNC_AGGARG,
447 		    "%s( ) argument #1 is incompatible with prototype:\n"
448 		    "\tprototype: aggregation\n\t argument: %s\n",
449 		    dnp->dn_ident->di_name,
450 		    dt_node_type_name(anp, n, sizeof (n)));
451 	}
452 
453 	if (argc == 2) {
454 		assert(trunc != NULL);
455 		if (!dt_node_is_scalar(trunc)) {
456 			dnerror(dnp, D_TRUNC_SCALAR,
457 			    "%s( ) argument #2 must be of scalar type\n",
458 			    dnp->dn_ident->di_name);
459 		}
460 	}
461 
462 	aid = anp->dn_ident;
463 
464 	if (aid->di_gen == dtp->dt_gen && !(aid->di_flags & DT_IDFLG_MOD)) {
465 		dnerror(dnp, D_TRUNC_AGGBAD,
466 		    "undefined aggregation: @%s\n", aid->di_name);
467 	}
468 
469 	ap = dt_stmt_action(dtp, sdp);
470 	dt_action_difconst(ap, anp->dn_ident->di_id, DTRACEACT_LIBACT);
471 	ap->dtad_arg = DT_ACT_TRUNC;
472 
473 	ap = dt_stmt_action(dtp, sdp);
474 
475 	if (argc == 1) {
476 		dt_action_difconst(ap, 0, DTRACEACT_LIBACT);
477 	} else {
478 		assert(trunc != NULL);
479 		dt_cg(yypcb, trunc);
480 		ap->dtad_difo = dt_as(yypcb);
481 		ap->dtad_kind = DTRACEACT_LIBACT;
482 	}
483 
484 	ap->dtad_arg = DT_ACT_TRUNC;
485 }
486 
487 static void
488 dt_action_printa(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
489 {
490 	dt_ident_t *aid, *fid;
491 	dtrace_actdesc_t *ap;
492 	const char *format;
493 	dt_node_t *anp;
494 
495 	char n[DT_TYPE_NAMELEN];
496 	int argc = 0, argr = 0;
497 
498 	for (anp = dnp->dn_args; anp != NULL; anp = anp->dn_list)
499 		argc++; /* count up arguments for error messages below */
500 
501 	switch (dnp->dn_args->dn_kind) {
502 	case DT_NODE_STRING:
503 		format = dnp->dn_args->dn_string;
504 		anp = dnp->dn_args->dn_list;
505 		argr = 2;
506 		break;
507 	case DT_NODE_AGG:
508 		format = NULL;
509 		anp = dnp->dn_args;
510 		argr = 1;
511 		break;
512 	default:
513 		format = NULL;
514 		anp = dnp->dn_args;
515 		argr = 1;
516 	}
517 
518 	if (argc != argr) {
519 		dnerror(dnp, D_PRINTA_PROTO,
520 		    "%s( ) prototype mismatch: %d args passed, %d expected\n",
521 		    dnp->dn_ident->di_name, argc, argr);
522 	}
523 
524 	if (anp->dn_kind != DT_NODE_AGG) {
525 		dnerror(dnp, D_PRINTA_AGGARG,
526 		    "%s( ) argument #%d is incompatible with prototype:\n"
527 		    "\tprototype: aggregation\n\t argument: %s\n",
528 		    dnp->dn_ident->di_name, argr,
529 		    dt_node_type_name(anp, n, sizeof (n)));
530 	}
531 
532 	aid = anp->dn_ident;
533 	fid = aid->di_iarg;
534 
535 	if (aid->di_gen == dtp->dt_gen && !(aid->di_flags & DT_IDFLG_MOD)) {
536 		dnerror(dnp, D_PRINTA_AGGBAD,
537 		    "undefined aggregation: @%s\n", aid->di_name);
538 	}
539 
540 	if (format != NULL) {
541 		yylineno = dnp->dn_line;
542 
543 		sdp->dtsd_fmtdata = dt_printf_create(yypcb->pcb_hdl, format);
544 		dt_printf_validate(sdp->dtsd_fmtdata,
545 		    DT_PRINTF_AGGREGATION, dnp->dn_ident, 1,
546 		    fid->di_id, ((dt_idsig_t *)aid->di_data)->dis_args);
547 	}
548 
549 	ap = dt_stmt_action(dtp, sdp);
550 	dt_action_difconst(ap, anp->dn_ident->di_id, DTRACEACT_PRINTA);
551 }
552 
553 static void
554 dt_action_printflike(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp,
555     dtrace_actkind_t kind)
556 {
557 	dt_node_t *anp, *arg1;
558 	dtrace_actdesc_t *ap = NULL;
559 	char n[DT_TYPE_NAMELEN], *str;
560 
561 	assert(DTRACEACT_ISPRINTFLIKE(kind));
562 
563 	if (dnp->dn_args->dn_kind != DT_NODE_STRING) {
564 		dnerror(dnp, D_PRINTF_ARG_FMT,
565 		    "%s( ) argument #1 is incompatible with prototype:\n"
566 		    "\tprototype: string constant\n\t argument: %s\n",
567 		    dnp->dn_ident->di_name,
568 		    dt_node_type_name(dnp->dn_args, n, sizeof (n)));
569 	}
570 
571 	arg1 = dnp->dn_args->dn_list;
572 	yylineno = dnp->dn_line;
573 	str = dnp->dn_args->dn_string;
574 
575 
576 	/*
577 	 * If this is an freopen(), we use an empty string to denote that
578 	 * stdout should be restored.  For other printf()-like actions, an
579 	 * empty format string is illegal:  an empty format string would
580 	 * result in malformed DOF, and the compiler thus flags an empty
581 	 * format string as a compile-time error.  To avoid propagating the
582 	 * freopen() special case throughout the system, we simply transpose
583 	 * an empty string into a sentinel string (DT_FREOPEN_RESTORE) that
584 	 * denotes that stdout should be restored.
585 	 */
586 	if (kind == DTRACEACT_FREOPEN) {
587 		if (strcmp(str, DT_FREOPEN_RESTORE) == 0) {
588 			/*
589 			 * Our sentinel is always an invalid argument to
590 			 * freopen(), but if it's been manually specified, we
591 			 * must fail now instead of when the freopen() is
592 			 * actually evaluated.
593 			 */
594 			dnerror(dnp, D_FREOPEN_INVALID,
595 			    "%s( ) argument #1 cannot be \"%s\"\n",
596 			    dnp->dn_ident->di_name, DT_FREOPEN_RESTORE);
597 		}
598 
599 		if (str[0] == '\0')
600 			str = DT_FREOPEN_RESTORE;
601 	}
602 
603 	sdp->dtsd_fmtdata = dt_printf_create(dtp, str);
604 
605 	dt_printf_validate(sdp->dtsd_fmtdata, DT_PRINTF_EXACTLEN,
606 	    dnp->dn_ident, 1, DTRACEACT_AGGREGATION, arg1);
607 
608 	if (arg1 == NULL) {
609 		dif_instr_t *dbuf;
610 		dtrace_difo_t *dp;
611 
612 		if ((dbuf = dt_alloc(dtp, sizeof (dif_instr_t))) == NULL ||
613 		    (dp = dt_zalloc(dtp, sizeof (dtrace_difo_t))) == NULL) {
614 			dt_free(dtp, dbuf);
615 			longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
616 		}
617 
618 		dbuf[0] = DIF_INSTR_RET(DIF_REG_R0); /* ret %r0 */
619 
620 		dp->dtdo_buf = dbuf;
621 		dp->dtdo_len = 1;
622 		dp->dtdo_rtype = dt_int_rtype;
623 
624 		ap = dt_stmt_action(dtp, sdp);
625 		ap->dtad_difo = dp;
626 		ap->dtad_kind = kind;
627 		return;
628 	}
629 
630 	for (anp = arg1; anp != NULL; anp = anp->dn_list) {
631 		ap = dt_stmt_action(dtp, sdp);
632 		dt_cg(yypcb, anp);
633 		ap->dtad_difo = dt_as(yypcb);
634 		ap->dtad_kind = kind;
635 	}
636 }
637 
638 static void
639 dt_action_trace(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
640 {
641 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
642 
643 	if (dt_node_is_void(dnp->dn_args)) {
644 		dnerror(dnp->dn_args, D_TRACE_VOID,
645 		    "trace( ) may not be applied to a void expression\n");
646 	}
647 
648 	if (dt_node_is_dynamic(dnp->dn_args)) {
649 		dnerror(dnp->dn_args, D_TRACE_DYN,
650 		    "trace( ) may not be applied to a dynamic expression\n");
651 	}
652 
653 	dt_cg(yypcb, dnp->dn_args);
654 	ap->dtad_difo = dt_as(yypcb);
655 	ap->dtad_kind = DTRACEACT_DIFEXPR;
656 }
657 
658 static void
659 dt_action_tracemem(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
660 {
661 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
662 
663 	dt_node_t *addr = dnp->dn_args;
664 	dt_node_t *size = dnp->dn_args->dn_list;
665 
666 	char n[DT_TYPE_NAMELEN];
667 
668 	if (dt_node_is_integer(addr) == 0 && dt_node_is_pointer(addr) == 0) {
669 		dnerror(addr, D_TRACEMEM_ADDR,
670 		    "tracemem( ) argument #1 is incompatible with "
671 		    "prototype:\n\tprototype: pointer or integer\n"
672 		    "\t argument: %s\n",
673 		    dt_node_type_name(addr, n, sizeof (n)));
674 	}
675 
676 	if (dt_node_is_posconst(size) == 0) {
677 		dnerror(size, D_TRACEMEM_SIZE, "tracemem( ) argument #2 must "
678 		    "be a non-zero positive integral constant expression\n");
679 	}
680 
681 	dt_cg(yypcb, addr);
682 	ap->dtad_difo = dt_as(yypcb);
683 	ap->dtad_kind = DTRACEACT_DIFEXPR;
684 
685 	ap->dtad_difo->dtdo_rtype.dtdt_flags |= DIF_TF_BYREF;
686 	ap->dtad_difo->dtdo_rtype.dtdt_size = size->dn_value;
687 }
688 
689 static void
690 dt_action_stack_args(dtrace_hdl_t *dtp, dtrace_actdesc_t *ap, dt_node_t *arg0)
691 {
692 	ap->dtad_kind = DTRACEACT_STACK;
693 
694 	if (dtp->dt_options[DTRACEOPT_STACKFRAMES] != DTRACEOPT_UNSET) {
695 		ap->dtad_arg = dtp->dt_options[DTRACEOPT_STACKFRAMES];
696 	} else {
697 		ap->dtad_arg = 0;
698 	}
699 
700 	if (arg0 != NULL) {
701 		if (arg0->dn_list != NULL) {
702 			dnerror(arg0, D_STACK_PROTO, "stack( ) prototype "
703 			    "mismatch: too many arguments\n");
704 		}
705 
706 		if (dt_node_is_posconst(arg0) == 0) {
707 			dnerror(arg0, D_STACK_SIZE, "stack( ) size must be a "
708 			    "non-zero positive integral constant expression\n");
709 		}
710 
711 		ap->dtad_arg = arg0->dn_value;
712 	}
713 }
714 
715 static void
716 dt_action_stack(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
717 {
718 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
719 	dt_action_stack_args(dtp, ap, dnp->dn_args);
720 }
721 
722 static void
723 dt_action_ustack_args(dtrace_hdl_t *dtp, dtrace_actdesc_t *ap, dt_node_t *dnp)
724 {
725 	uint32_t nframes = 0;
726 	uint32_t strsize = 0;	/* default string table size */
727 	dt_node_t *arg0 = dnp->dn_args;
728 	dt_node_t *arg1 = arg0 != NULL ? arg0->dn_list : NULL;
729 
730 	assert(dnp->dn_ident->di_id == DT_ACT_JSTACK ||
731 	    dnp->dn_ident->di_id == DT_ACT_USTACK);
732 
733 	if (dnp->dn_ident->di_id == DT_ACT_JSTACK) {
734 		if (dtp->dt_options[DTRACEOPT_JSTACKFRAMES] != DTRACEOPT_UNSET)
735 			nframes = dtp->dt_options[DTRACEOPT_JSTACKFRAMES];
736 
737 		if (dtp->dt_options[DTRACEOPT_JSTACKSTRSIZE] != DTRACEOPT_UNSET)
738 			strsize = dtp->dt_options[DTRACEOPT_JSTACKSTRSIZE];
739 
740 		ap->dtad_kind = DTRACEACT_JSTACK;
741 	} else {
742 		assert(dnp->dn_ident->di_id == DT_ACT_USTACK);
743 
744 		if (dtp->dt_options[DTRACEOPT_USTACKFRAMES] != DTRACEOPT_UNSET)
745 			nframes = dtp->dt_options[DTRACEOPT_USTACKFRAMES];
746 
747 		ap->dtad_kind = DTRACEACT_USTACK;
748 	}
749 
750 	if (arg0 != NULL) {
751 		if (!dt_node_is_posconst(arg0)) {
752 			dnerror(arg0, D_USTACK_FRAMES, "ustack( ) argument #1 "
753 			    "must be a non-zero positive integer constant\n");
754 		}
755 		nframes = (uint32_t)arg0->dn_value;
756 	}
757 
758 	if (arg1 != NULL) {
759 		if (arg1->dn_kind != DT_NODE_INT ||
760 		    ((arg1->dn_flags & DT_NF_SIGNED) &&
761 		    (int64_t)arg1->dn_value < 0)) {
762 			dnerror(arg1, D_USTACK_STRSIZE, "ustack( ) argument #2 "
763 			    "must be a positive integer constant\n");
764 		}
765 
766 		if (arg1->dn_list != NULL) {
767 			dnerror(arg1, D_USTACK_PROTO, "ustack( ) prototype "
768 			    "mismatch: too many arguments\n");
769 		}
770 
771 		strsize = (uint32_t)arg1->dn_value;
772 	}
773 
774 	ap->dtad_arg = DTRACE_USTACK_ARG(nframes, strsize);
775 }
776 
777 static void
778 dt_action_ustack(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
779 {
780 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
781 	dt_action_ustack_args(dtp, ap, dnp);
782 }
783 
784 static void
785 dt_action_setopt(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
786 {
787 	dtrace_actdesc_t *ap;
788 	dt_node_t *arg0, *arg1;
789 
790 	/*
791 	 * The prototype guarantees that we are called with either one or
792 	 * two arguments, and that any arguments that are present are strings.
793 	 */
794 	arg0 = dnp->dn_args;
795 	arg1 = arg0->dn_list;
796 
797 	ap = dt_stmt_action(dtp, sdp);
798 	dt_cg(yypcb, arg0);
799 	ap->dtad_difo = dt_as(yypcb);
800 	ap->dtad_kind = DTRACEACT_LIBACT;
801 	ap->dtad_arg = DT_ACT_SETOPT;
802 
803 	ap = dt_stmt_action(dtp, sdp);
804 
805 	if (arg1 == NULL) {
806 		dt_action_difconst(ap, 0, DTRACEACT_LIBACT);
807 	} else {
808 		dt_cg(yypcb, arg1);
809 		ap->dtad_difo = dt_as(yypcb);
810 		ap->dtad_kind = DTRACEACT_LIBACT;
811 	}
812 
813 	ap->dtad_arg = DT_ACT_SETOPT;
814 }
815 
816 /*ARGSUSED*/
817 static void
818 dt_action_symmod_args(dtrace_hdl_t *dtp, dtrace_actdesc_t *ap,
819     dt_node_t *dnp, dtrace_actkind_t kind)
820 {
821 	assert(kind == DTRACEACT_SYM || kind == DTRACEACT_MOD ||
822 	    kind == DTRACEACT_USYM || kind == DTRACEACT_UMOD ||
823 	    kind == DTRACEACT_UADDR);
824 
825 	dt_cg(yypcb, dnp);
826 	ap->dtad_difo = dt_as(yypcb);
827 	ap->dtad_kind = kind;
828 	ap->dtad_difo->dtdo_rtype.dtdt_size = sizeof (uint64_t);
829 }
830 
831 static void
832 dt_action_symmod(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp,
833     dtrace_actkind_t kind)
834 {
835 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
836 	dt_action_symmod_args(dtp, ap, dnp->dn_args, kind);
837 }
838 
839 /*ARGSUSED*/
840 static void
841 dt_action_ftruncate(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
842 {
843 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
844 
845 	/*
846 	 * Library actions need a DIFO that serves as an argument.  As
847 	 * ftruncate() doesn't take an argument, we generate the constant 0
848 	 * in a DIFO; this constant will be ignored when the ftruncate() is
849 	 * processed.
850 	 */
851 	dt_action_difconst(ap, 0, DTRACEACT_LIBACT);
852 	ap->dtad_arg = DT_ACT_FTRUNCATE;
853 }
854 
855 /*ARGSUSED*/
856 static void
857 dt_action_stop(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
858 {
859 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
860 
861 	ap->dtad_kind = DTRACEACT_STOP;
862 	ap->dtad_arg = 0;
863 }
864 
865 /*ARGSUSED*/
866 static void
867 dt_action_breakpoint(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
868 {
869 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
870 
871 	ap->dtad_kind = DTRACEACT_BREAKPOINT;
872 	ap->dtad_arg = 0;
873 }
874 
875 /*ARGSUSED*/
876 static void
877 dt_action_panic(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
878 {
879 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
880 
881 	ap->dtad_kind = DTRACEACT_PANIC;
882 	ap->dtad_arg = 0;
883 }
884 
885 static void
886 dt_action_chill(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
887 {
888 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
889 
890 	dt_cg(yypcb, dnp->dn_args);
891 	ap->dtad_difo = dt_as(yypcb);
892 	ap->dtad_kind = DTRACEACT_CHILL;
893 }
894 
895 static void
896 dt_action_raise(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
897 {
898 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
899 
900 	dt_cg(yypcb, dnp->dn_args);
901 	ap->dtad_difo = dt_as(yypcb);
902 	ap->dtad_kind = DTRACEACT_RAISE;
903 }
904 
905 static void
906 dt_action_exit(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
907 {
908 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
909 
910 	dt_cg(yypcb, dnp->dn_args);
911 	ap->dtad_difo = dt_as(yypcb);
912 	ap->dtad_kind = DTRACEACT_EXIT;
913 	ap->dtad_difo->dtdo_rtype.dtdt_size = sizeof (int);
914 }
915 
916 static void
917 dt_action_speculate(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
918 {
919 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
920 
921 	dt_cg(yypcb, dnp->dn_args);
922 	ap->dtad_difo = dt_as(yypcb);
923 	ap->dtad_kind = DTRACEACT_SPECULATE;
924 }
925 
926 static void
927 dt_action_commit(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
928 {
929 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
930 
931 	dt_cg(yypcb, dnp->dn_args);
932 	ap->dtad_difo = dt_as(yypcb);
933 	ap->dtad_kind = DTRACEACT_COMMIT;
934 }
935 
936 static void
937 dt_action_discard(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
938 {
939 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
940 
941 	dt_cg(yypcb, dnp->dn_args);
942 	ap->dtad_difo = dt_as(yypcb);
943 	ap->dtad_kind = DTRACEACT_DISCARD;
944 }
945 
946 static void
947 dt_compile_fun(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
948 {
949 	switch (dnp->dn_expr->dn_ident->di_id) {
950 	case DT_ACT_BREAKPOINT:
951 		dt_action_breakpoint(dtp, dnp->dn_expr, sdp);
952 		break;
953 	case DT_ACT_CHILL:
954 		dt_action_chill(dtp, dnp->dn_expr, sdp);
955 		break;
956 	case DT_ACT_CLEAR:
957 		dt_action_clear(dtp, dnp->dn_expr, sdp);
958 		break;
959 	case DT_ACT_COMMIT:
960 		dt_action_commit(dtp, dnp->dn_expr, sdp);
961 		break;
962 	case DT_ACT_DENORMALIZE:
963 		dt_action_normalize(dtp, dnp->dn_expr, sdp);
964 		break;
965 	case DT_ACT_DISCARD:
966 		dt_action_discard(dtp, dnp->dn_expr, sdp);
967 		break;
968 	case DT_ACT_EXIT:
969 		dt_action_exit(dtp, dnp->dn_expr, sdp);
970 		break;
971 	case DT_ACT_FREOPEN:
972 		dt_action_printflike(dtp, dnp->dn_expr, sdp, DTRACEACT_FREOPEN);
973 		break;
974 	case DT_ACT_FTRUNCATE:
975 		dt_action_ftruncate(dtp, dnp->dn_expr, sdp);
976 		break;
977 	case DT_ACT_MOD:
978 		dt_action_symmod(dtp, dnp->dn_expr, sdp, DTRACEACT_MOD);
979 		break;
980 	case DT_ACT_NORMALIZE:
981 		dt_action_normalize(dtp, dnp->dn_expr, sdp);
982 		break;
983 	case DT_ACT_PANIC:
984 		dt_action_panic(dtp, dnp->dn_expr, sdp);
985 		break;
986 	case DT_ACT_PRINTA:
987 		dt_action_printa(dtp, dnp->dn_expr, sdp);
988 		break;
989 	case DT_ACT_PRINTF:
990 		dt_action_printflike(dtp, dnp->dn_expr, sdp, DTRACEACT_PRINTF);
991 		break;
992 	case DT_ACT_RAISE:
993 		dt_action_raise(dtp, dnp->dn_expr, sdp);
994 		break;
995 	case DT_ACT_SETOPT:
996 		dt_action_setopt(dtp, dnp->dn_expr, sdp);
997 		break;
998 	case DT_ACT_SPECULATE:
999 		dt_action_speculate(dtp, dnp->dn_expr, sdp);
1000 		break;
1001 	case DT_ACT_STACK:
1002 		dt_action_stack(dtp, dnp->dn_expr, sdp);
1003 		break;
1004 	case DT_ACT_STOP:
1005 		dt_action_stop(dtp, dnp->dn_expr, sdp);
1006 		break;
1007 	case DT_ACT_SYM:
1008 		dt_action_symmod(dtp, dnp->dn_expr, sdp, DTRACEACT_SYM);
1009 		break;
1010 	case DT_ACT_SYSTEM:
1011 		dt_action_printflike(dtp, dnp->dn_expr, sdp, DTRACEACT_SYSTEM);
1012 		break;
1013 	case DT_ACT_TRACE:
1014 		dt_action_trace(dtp, dnp->dn_expr, sdp);
1015 		break;
1016 	case DT_ACT_TRACEMEM:
1017 		dt_action_tracemem(dtp, dnp->dn_expr, sdp);
1018 		break;
1019 	case DT_ACT_TRUNC:
1020 		dt_action_trunc(dtp, dnp->dn_expr, sdp);
1021 		break;
1022 	case DT_ACT_UADDR:
1023 		dt_action_symmod(dtp, dnp->dn_expr, sdp, DTRACEACT_UADDR);
1024 		break;
1025 	case DT_ACT_UMOD:
1026 		dt_action_symmod(dtp, dnp->dn_expr, sdp, DTRACEACT_UMOD);
1027 		break;
1028 	case DT_ACT_USYM:
1029 		dt_action_symmod(dtp, dnp->dn_expr, sdp, DTRACEACT_USYM);
1030 		break;
1031 	case DT_ACT_USTACK:
1032 	case DT_ACT_JSTACK:
1033 		dt_action_ustack(dtp, dnp->dn_expr, sdp);
1034 		break;
1035 	default:
1036 		dnerror(dnp->dn_expr, D_UNKNOWN, "tracing function %s( ) is "
1037 		    "not yet supported\n", dnp->dn_expr->dn_ident->di_name);
1038 	}
1039 }
1040 
1041 static void
1042 dt_compile_exp(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
1043 {
1044 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
1045 
1046 	dt_cg(yypcb, dnp->dn_expr);
1047 	ap->dtad_difo = dt_as(yypcb);
1048 	ap->dtad_difo->dtdo_rtype = dt_void_rtype;
1049 	ap->dtad_kind = DTRACEACT_DIFEXPR;
1050 }
1051 
1052 static void
1053 dt_compile_agg(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
1054 {
1055 	dt_ident_t *aid, *fid;
1056 	dt_node_t *anp, *incr = NULL;
1057 	dtrace_actdesc_t *ap;
1058 	uint_t n = 1, argmax;
1059 	uint64_t arg = 0;
1060 
1061 	/*
1062 	 * If the aggregation has no aggregating function applied to it, then
1063 	 * this statement has no effect.  Flag this as a programming error.
1064 	 */
1065 	if (dnp->dn_aggfun == NULL) {
1066 		dnerror(dnp, D_AGG_NULL, "expression has null effect: @%s\n",
1067 		    dnp->dn_ident->di_name);
1068 	}
1069 
1070 	aid = dnp->dn_ident;
1071 	fid = dnp->dn_aggfun->dn_ident;
1072 
1073 	if (dnp->dn_aggfun->dn_args != NULL &&
1074 	    dt_node_is_scalar(dnp->dn_aggfun->dn_args) == 0) {
1075 		dnerror(dnp->dn_aggfun, D_AGG_SCALAR, "%s( ) argument #1 must "
1076 		    "be of scalar type\n", fid->di_name);
1077 	}
1078 
1079 	/*
1080 	 * The ID of the aggregation itself is implicitly recorded as the first
1081 	 * member of each aggregation tuple so we can distinguish them later.
1082 	 */
1083 	ap = dt_stmt_action(dtp, sdp);
1084 	dt_action_difconst(ap, aid->di_id, DTRACEACT_DIFEXPR);
1085 
1086 	for (anp = dnp->dn_aggtup; anp != NULL; anp = anp->dn_list) {
1087 		ap = dt_stmt_action(dtp, sdp);
1088 		n++;
1089 
1090 		if (anp->dn_kind == DT_NODE_FUNC) {
1091 			if (anp->dn_ident->di_id == DT_ACT_STACK) {
1092 				dt_action_stack_args(dtp, ap, anp->dn_args);
1093 				continue;
1094 			}
1095 
1096 			if (anp->dn_ident->di_id == DT_ACT_USTACK ||
1097 			    anp->dn_ident->di_id == DT_ACT_JSTACK) {
1098 				dt_action_ustack_args(dtp, ap, anp);
1099 				continue;
1100 			}
1101 
1102 			switch (anp->dn_ident->di_id) {
1103 			case DT_ACT_UADDR:
1104 				dt_action_symmod_args(dtp, ap,
1105 				    anp->dn_args, DTRACEACT_UADDR);
1106 				continue;
1107 
1108 			case DT_ACT_USYM:
1109 				dt_action_symmod_args(dtp, ap,
1110 				    anp->dn_args, DTRACEACT_USYM);
1111 				continue;
1112 
1113 			case DT_ACT_UMOD:
1114 				dt_action_symmod_args(dtp, ap,
1115 				    anp->dn_args, DTRACEACT_UMOD);
1116 				continue;
1117 
1118 			case DT_ACT_SYM:
1119 				dt_action_symmod_args(dtp, ap,
1120 				    anp->dn_args, DTRACEACT_SYM);
1121 				continue;
1122 
1123 			case DT_ACT_MOD:
1124 				dt_action_symmod_args(dtp, ap,
1125 				    anp->dn_args, DTRACEACT_MOD);
1126 				continue;
1127 
1128 			default:
1129 				break;
1130 			}
1131 		}
1132 
1133 		dt_cg(yypcb, anp);
1134 		ap->dtad_difo = dt_as(yypcb);
1135 		ap->dtad_kind = DTRACEACT_DIFEXPR;
1136 	}
1137 
1138 	if (fid->di_id == DTRACEAGG_LQUANTIZE) {
1139 		/*
1140 		 * For linear quantization, we have between two and four
1141 		 * arguments in addition to the expression:
1142 		 *
1143 		 *    arg1 => Base value
1144 		 *    arg2 => Limit value
1145 		 *    arg3 => Quantization level step size (defaults to 1)
1146 		 *    arg4 => Quantization increment value (defaults to 1)
1147 		 */
1148 		dt_node_t *arg1 = dnp->dn_aggfun->dn_args->dn_list;
1149 		dt_node_t *arg2 = arg1->dn_list;
1150 		dt_node_t *arg3 = arg2->dn_list;
1151 		uint64_t nlevels, step = 1;
1152 		int64_t baseval, limitval;
1153 
1154 		if (arg1->dn_kind != DT_NODE_INT) {
1155 			dnerror(arg1, D_LQUANT_BASETYPE, "lquantize( ) "
1156 			    "argument #1 must be an integer constant\n");
1157 		}
1158 
1159 		baseval = (int64_t)arg1->dn_value;
1160 
1161 		if (baseval < INT32_MIN || baseval > INT32_MAX) {
1162 			dnerror(arg1, D_LQUANT_BASEVAL, "lquantize( ) "
1163 			    "argument #1 must be a 32-bit quantity\n");
1164 		}
1165 
1166 		if (arg2->dn_kind != DT_NODE_INT) {
1167 			dnerror(arg2, D_LQUANT_LIMTYPE, "lquantize( ) "
1168 			    "argument #2 must be an integer constant\n");
1169 		}
1170 
1171 		limitval = (int64_t)arg2->dn_value;
1172 
1173 		if (limitval < INT32_MIN || limitval > INT32_MAX) {
1174 			dnerror(arg2, D_LQUANT_LIMVAL, "lquantize( ) "
1175 			    "argument #2 must be a 32-bit quantity\n");
1176 		}
1177 
1178 		if (limitval < baseval) {
1179 			dnerror(dnp, D_LQUANT_MISMATCH,
1180 			    "lquantize( ) base (argument #1) must be less "
1181 			    "than limit (argument #2)\n");
1182 		}
1183 
1184 		if (arg3 != NULL) {
1185 			if (!dt_node_is_posconst(arg3)) {
1186 				dnerror(arg3, D_LQUANT_STEPTYPE, "lquantize( ) "
1187 				    "argument #3 must be a non-zero positive "
1188 				    "integer constant\n");
1189 			}
1190 
1191 			if ((step = arg3->dn_value) > UINT16_MAX) {
1192 				dnerror(arg3, D_LQUANT_STEPVAL, "lquantize( ) "
1193 				    "argument #3 must be a 16-bit quantity\n");
1194 			}
1195 		}
1196 
1197 		nlevels = (limitval - baseval) / step;
1198 
1199 		if (nlevels == 0) {
1200 			dnerror(dnp, D_LQUANT_STEPLARGE,
1201 			    "lquantize( ) step (argument #3) too large: must "
1202 			    "have at least one quantization level\n");
1203 		}
1204 
1205 		if (nlevels > UINT16_MAX) {
1206 			dnerror(dnp, D_LQUANT_STEPSMALL, "lquantize( ) step "
1207 			    "(argument #3) too small: number of quantization "
1208 			    "levels must be a 16-bit quantity\n");
1209 		}
1210 
1211 		arg = (step << DTRACE_LQUANTIZE_STEPSHIFT) |
1212 		    (nlevels << DTRACE_LQUANTIZE_LEVELSHIFT) |
1213 		    ((baseval << DTRACE_LQUANTIZE_BASESHIFT) &
1214 		    DTRACE_LQUANTIZE_BASEMASK);
1215 
1216 		incr = arg3 != NULL ? arg3->dn_list : NULL;
1217 		argmax = 5;
1218 	}
1219 
1220 	if (fid->di_id == DTRACEAGG_QUANTIZE) {
1221 		incr = dnp->dn_aggfun->dn_args->dn_list;
1222 		argmax = 2;
1223 	}
1224 
1225 	if (incr != NULL) {
1226 		if (!dt_node_is_scalar(incr)) {
1227 			dnerror(dnp, D_PROTO_ARG, "%s( ) increment value "
1228 			    "(argument #%d) must be of scalar type\n",
1229 			    fid->di_name, argmax);
1230 		}
1231 
1232 		if ((anp = incr->dn_list) != NULL) {
1233 			int argc = argmax;
1234 
1235 			for (; anp != NULL; anp = anp->dn_list)
1236 				argc++;
1237 
1238 			dnerror(incr, D_PROTO_LEN, "%s( ) prototype "
1239 			    "mismatch: %d args passed, at most %d expected",
1240 			    fid->di_name, argc, argmax);
1241 		}
1242 
1243 		ap = dt_stmt_action(dtp, sdp);
1244 		n++;
1245 
1246 		dt_cg(yypcb, incr);
1247 		ap->dtad_difo = dt_as(yypcb);
1248 		ap->dtad_difo->dtdo_rtype = dt_void_rtype;
1249 		ap->dtad_kind = DTRACEACT_DIFEXPR;
1250 	}
1251 
1252 	assert(sdp->dtsd_aggdata == NULL);
1253 	sdp->dtsd_aggdata = aid;
1254 
1255 	ap = dt_stmt_action(dtp, sdp);
1256 	assert(fid->di_kind == DT_IDENT_AGGFUNC);
1257 	assert(DTRACEACT_ISAGG(fid->di_id));
1258 	ap->dtad_kind = fid->di_id;
1259 	ap->dtad_ntuple = n;
1260 	ap->dtad_arg = arg;
1261 
1262 	if (dnp->dn_aggfun->dn_args != NULL) {
1263 		dt_cg(yypcb, dnp->dn_aggfun->dn_args);
1264 		ap->dtad_difo = dt_as(yypcb);
1265 	}
1266 }
1267 
1268 static void
1269 dt_compile_one_clause(dtrace_hdl_t *dtp, dt_node_t *cnp, dt_node_t *pnp)
1270 {
1271 	dtrace_ecbdesc_t *edp;
1272 	dtrace_stmtdesc_t *sdp;
1273 	dt_node_t *dnp;
1274 
1275 	yylineno = pnp->dn_line;
1276 	dt_setcontext(dtp, pnp->dn_desc);
1277 	(void) dt_node_cook(cnp, DT_IDFLG_REF);
1278 
1279 	if (DT_TREEDUMP_PASS(dtp, 2))
1280 		dt_node_printr(cnp, stderr, 0);
1281 
1282 	if ((edp = dt_ecbdesc_create(dtp, pnp->dn_desc)) == NULL)
1283 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
1284 
1285 	assert(yypcb->pcb_ecbdesc == NULL);
1286 	yypcb->pcb_ecbdesc = edp;
1287 
1288 	if (cnp->dn_pred != NULL) {
1289 		dt_cg(yypcb, cnp->dn_pred);
1290 		edp->dted_pred.dtpdd_difo = dt_as(yypcb);
1291 	}
1292 
1293 	if (cnp->dn_acts == NULL) {
1294 		dt_stmt_append(dt_stmt_create(dtp, edp,
1295 		    cnp->dn_ctxattr, _dtrace_defattr), cnp);
1296 	}
1297 
1298 	for (dnp = cnp->dn_acts; dnp != NULL; dnp = dnp->dn_list) {
1299 		assert(yypcb->pcb_stmt == NULL);
1300 		sdp = dt_stmt_create(dtp, edp, cnp->dn_ctxattr, cnp->dn_attr);
1301 
1302 		switch (dnp->dn_kind) {
1303 		case DT_NODE_DEXPR:
1304 			if (dnp->dn_expr->dn_kind == DT_NODE_AGG)
1305 				dt_compile_agg(dtp, dnp->dn_expr, sdp);
1306 			else
1307 				dt_compile_exp(dtp, dnp, sdp);
1308 			break;
1309 		case DT_NODE_DFUNC:
1310 			dt_compile_fun(dtp, dnp, sdp);
1311 			break;
1312 		case DT_NODE_AGG:
1313 			dt_compile_agg(dtp, dnp, sdp);
1314 			break;
1315 		default:
1316 			dnerror(dnp, D_UNKNOWN, "internal error -- node kind "
1317 			    "%u is not a valid statement\n", dnp->dn_kind);
1318 		}
1319 
1320 		assert(yypcb->pcb_stmt == sdp);
1321 		dt_stmt_append(sdp, dnp);
1322 	}
1323 
1324 	assert(yypcb->pcb_ecbdesc == edp);
1325 	dt_ecbdesc_release(dtp, edp);
1326 	dt_endcontext(dtp);
1327 	yypcb->pcb_ecbdesc = NULL;
1328 }
1329 
1330 static void
1331 dt_compile_clause(dtrace_hdl_t *dtp, dt_node_t *cnp)
1332 {
1333 	dt_node_t *pnp;
1334 
1335 	for (pnp = cnp->dn_pdescs; pnp != NULL; pnp = pnp->dn_list)
1336 		dt_compile_one_clause(dtp, cnp, pnp);
1337 }
1338 
1339 static void
1340 dt_compile_xlator(dt_node_t *dnp)
1341 {
1342 	dt_xlator_t *dxp = dnp->dn_xlator;
1343 	dt_node_t *mnp;
1344 
1345 	for (mnp = dnp->dn_members; mnp != NULL; mnp = mnp->dn_list) {
1346 		assert(dxp->dx_membdif[mnp->dn_membid] == NULL);
1347 		dt_cg(yypcb, mnp);
1348 		dxp->dx_membdif[mnp->dn_membid] = dt_as(yypcb);
1349 	}
1350 }
1351 
1352 void
1353 dt_setcontext(dtrace_hdl_t *dtp, dtrace_probedesc_t *pdp)
1354 {
1355 	const dtrace_pattr_t *pap;
1356 	dt_probe_t *prp;
1357 	dt_ident_t *idp;
1358 	char attrstr[8];
1359 	int err;
1360 
1361 	/*
1362 	 * If the provider name ends with what could be interpreted as a
1363 	 * number, we assume that it's a pid and that we may need to
1364 	 * dynamically create those probes for that process.
1365 	 */
1366 	if (isdigit(pdp->dtpd_provider[strlen(pdp->dtpd_provider) - 1]))
1367 		dt_pid_create_probes(pdp, dtp);
1368 
1369 	/*
1370 	 * Call dt_probe_info() to get the probe arguments and attributes.  If
1371 	 * a representative probe is found, set 'pap' to the probe provider's
1372 	 * attributes.  Otherwise set 'pap' to default Unstable attributes.
1373 	 */
1374 	if ((prp = dt_probe_info(dtp, pdp, &yypcb->pcb_pinfo)) == NULL) {
1375 		pap = &_dtrace_prvdesc;
1376 		err = dtrace_errno(dtp);
1377 		bzero(&yypcb->pcb_pinfo, sizeof (dtrace_probeinfo_t));
1378 		yypcb->pcb_pinfo.dtp_attr = pap->dtpa_provider;
1379 		yypcb->pcb_pinfo.dtp_arga = pap->dtpa_args;
1380 	} else {
1381 		pap = &prp->pr_pvp->pv_desc.dtvd_attr;
1382 		err = 0;
1383 	}
1384 
1385 	if (err == EDT_NOPROBE && !(yypcb->pcb_cflags & DTRACE_C_ZDEFS)) {
1386 		xyerror(D_PDESC_ZERO, "probe description %s:%s:%s:%s does not "
1387 		    "match any probes\n", pdp->dtpd_provider, pdp->dtpd_mod,
1388 		    pdp->dtpd_func, pdp->dtpd_name);
1389 	}
1390 
1391 	if (err != EDT_NOPROBE && err != EDT_UNSTABLE && err != 0)
1392 		xyerror(D_PDESC_INVAL, "%s\n", dtrace_errmsg(dtp, err));
1393 
1394 	dt_dprintf("set context to %s:%s:%s:%s [%u] prp=%p attr=%s argc=%d\n",
1395 	    pdp->dtpd_provider, pdp->dtpd_mod, pdp->dtpd_func, pdp->dtpd_name,
1396 	    pdp->dtpd_id, (void *)prp, dt_attr_str(yypcb->pcb_pinfo.dtp_attr,
1397 	    attrstr, sizeof (attrstr)), yypcb->pcb_pinfo.dtp_argc);
1398 
1399 	/*
1400 	 * Reset the stability attributes of D global variables that vary
1401 	 * based on the attributes of the provider and context itself.
1402 	 */
1403 	if ((idp = dt_idhash_lookup(dtp->dt_globals, "probeprov")) != NULL)
1404 		idp->di_attr = pap->dtpa_provider;
1405 	if ((idp = dt_idhash_lookup(dtp->dt_globals, "probemod")) != NULL)
1406 		idp->di_attr = pap->dtpa_mod;
1407 	if ((idp = dt_idhash_lookup(dtp->dt_globals, "probefunc")) != NULL)
1408 		idp->di_attr = pap->dtpa_func;
1409 	if ((idp = dt_idhash_lookup(dtp->dt_globals, "probename")) != NULL)
1410 		idp->di_attr = pap->dtpa_name;
1411 	if ((idp = dt_idhash_lookup(dtp->dt_globals, "args")) != NULL)
1412 		idp->di_attr = pap->dtpa_args;
1413 
1414 	yypcb->pcb_pdesc = pdp;
1415 	yypcb->pcb_probe = prp;
1416 }
1417 
1418 /*
1419  * Reset context-dependent variables and state at the end of cooking a D probe
1420  * definition clause.  This ensures that external declarations between clauses
1421  * do not reference any stale context-dependent data from the previous clause.
1422  */
1423 void
1424 dt_endcontext(dtrace_hdl_t *dtp)
1425 {
1426 	static const char *const cvars[] = {
1427 		"probeprov", "probemod", "probefunc", "probename", "args", NULL
1428 	};
1429 
1430 	dt_ident_t *idp;
1431 	int i;
1432 
1433 	for (i = 0; cvars[i] != NULL; i++) {
1434 		if ((idp = dt_idhash_lookup(dtp->dt_globals, cvars[i])) != NULL)
1435 			idp->di_attr = _dtrace_defattr;
1436 	}
1437 
1438 	yypcb->pcb_pdesc = NULL;
1439 	yypcb->pcb_probe = NULL;
1440 }
1441 
1442 static int
1443 dt_reduceid(dt_idhash_t *dhp, dt_ident_t *idp, dtrace_hdl_t *dtp)
1444 {
1445 	if (idp->di_vers != 0 && idp->di_vers > dtp->dt_vmax)
1446 		dt_idhash_delete(dhp, idp);
1447 
1448 	return (0);
1449 }
1450 
1451 /*
1452  * When dtrace_setopt() is called for "version", it calls dt_reduce() to remove
1453  * any identifiers or translators that have been previously defined as bound to
1454  * a version greater than the specified version.  Therefore, in our current
1455  * version implementation, establishing a binding is a one-way transformation.
1456  * In addition, no versioning is currently provided for types as our .d library
1457  * files do not define any types and we reserve prefixes DTRACE_ and dtrace_
1458  * for our exclusive use.  If required, type versioning will require more work.
1459  */
1460 int
1461 dt_reduce(dtrace_hdl_t *dtp, dt_version_t v)
1462 {
1463 	char s[DT_VERSION_STRMAX];
1464 	dt_xlator_t *dxp, *nxp;
1465 
1466 	if (v > dtp->dt_vmax)
1467 		return (dt_set_errno(dtp, EDT_VERSREDUCED));
1468 	else if (v == dtp->dt_vmax)
1469 		return (0); /* no reduction necessary */
1470 
1471 	dt_dprintf("reducing api version to %s\n",
1472 	    dt_version_num2str(v, s, sizeof (s)));
1473 
1474 	dtp->dt_vmax = v;
1475 
1476 	for (dxp = dt_list_next(&dtp->dt_xlators); dxp != NULL; dxp = nxp) {
1477 		nxp = dt_list_next(dxp);
1478 		if ((dxp->dx_souid.di_vers != 0 && dxp->dx_souid.di_vers > v) ||
1479 		    (dxp->dx_ptrid.di_vers != 0 && dxp->dx_ptrid.di_vers > v))
1480 			dt_list_delete(&dtp->dt_xlators, dxp);
1481 	}
1482 
1483 	(void) dt_idhash_iter(dtp->dt_macros, (dt_idhash_f *)dt_reduceid, dtp);
1484 	(void) dt_idhash_iter(dtp->dt_aggs, (dt_idhash_f *)dt_reduceid, dtp);
1485 	(void) dt_idhash_iter(dtp->dt_globals, (dt_idhash_f *)dt_reduceid, dtp);
1486 	(void) dt_idhash_iter(dtp->dt_tls, (dt_idhash_f *)dt_reduceid, dtp);
1487 
1488 	return (0);
1489 }
1490 
1491 /*
1492  * Fork and exec the cpp(1) preprocessor to run over the specified input file,
1493  * and return a FILE handle for the cpp output.  We use the /dev/fd filesystem
1494  * here to simplify the code by leveraging file descriptor inheritance.
1495  */
1496 static FILE *
1497 dt_preproc(dtrace_hdl_t *dtp, FILE *ifp)
1498 {
1499 	int argc = dtp->dt_cpp_argc;
1500 	char **argv = malloc(sizeof (char *) * (argc + 5));
1501 	FILE *ofp = tmpfile();
1502 
1503 	char ipath[20], opath[20]; /* big enough for /dev/fd/ + INT_MAX + \0 */
1504 	char verdef[32]; /* big enough for -D__SUNW_D_VERSION=0x%08x + \0 */
1505 
1506 	struct sigaction act, oact;
1507 	sigset_t mask, omask;
1508 
1509 	int wstat, estat;
1510 	pid_t pid;
1511 	off64_t off;
1512 	int c;
1513 
1514 	if (argv == NULL || ofp == NULL) {
1515 		(void) dt_set_errno(dtp, errno);
1516 		goto err;
1517 	}
1518 
1519 	/*
1520 	 * If the input is a seekable file, see if it is an interpreter file.
1521 	 * If we see #!, seek past the first line because cpp will choke on it.
1522 	 * We start cpp just prior to the \n at the end of this line so that
1523 	 * it still sees the newline, ensuring that #line values are correct.
1524 	 */
1525 	if (isatty(fileno(ifp)) == 0 && (off = ftello64(ifp)) != -1) {
1526 		if ((c = fgetc(ifp)) == '#' && (c = fgetc(ifp)) == '!') {
1527 			for (off += 2; c != '\n'; off++) {
1528 				if ((c = fgetc(ifp)) == EOF)
1529 					break;
1530 			}
1531 			if (c == '\n')
1532 				off--; /* start cpp just prior to \n */
1533 		}
1534 		(void) fflush(ifp);
1535 		(void) fseeko64(ifp, off, SEEK_SET);
1536 	}
1537 
1538 	(void) snprintf(ipath, sizeof (ipath), "/dev/fd/%d", fileno(ifp));
1539 	(void) snprintf(opath, sizeof (opath), "/dev/fd/%d", fileno(ofp));
1540 
1541 	bcopy(dtp->dt_cpp_argv, argv, sizeof (char *) * argc);
1542 
1543 	(void) snprintf(verdef, sizeof (verdef),
1544 	    "-D__SUNW_D_VERSION=0x%08x", dtp->dt_vmax);
1545 	argv[argc++] = verdef;
1546 
1547 	switch (dtp->dt_stdcmode) {
1548 	case DT_STDC_XA:
1549 	case DT_STDC_XT:
1550 		argv[argc++] = "-D__STDC__=0";
1551 		break;
1552 	case DT_STDC_XC:
1553 		argv[argc++] = "-D__STDC__=1";
1554 		break;
1555 	}
1556 
1557 	argv[argc++] = ipath;
1558 	argv[argc++] = opath;
1559 	argv[argc] = NULL;
1560 
1561 	/*
1562 	 * libdtrace must be able to be embedded in other programs that may
1563 	 * include application-specific signal handlers.  Therefore, if we
1564 	 * need to fork to run cpp(1), we must avoid generating a SIGCHLD
1565 	 * that could confuse the containing application.  To do this,
1566 	 * we block SIGCHLD and reset its disposition to SIG_DFL.
1567 	 * We restore our signal state once we are done.
1568 	 */
1569 	(void) sigemptyset(&mask);
1570 	(void) sigaddset(&mask, SIGCHLD);
1571 	(void) sigprocmask(SIG_BLOCK, &mask, &omask);
1572 
1573 	bzero(&act, sizeof (act));
1574 	act.sa_handler = SIG_DFL;
1575 	(void) sigaction(SIGCHLD, &act, &oact);
1576 
1577 	if ((pid = fork1()) == -1) {
1578 		(void) sigaction(SIGCHLD, &oact, NULL);
1579 		(void) sigprocmask(SIG_SETMASK, &omask, NULL);
1580 		(void) dt_set_errno(dtp, EDT_CPPFORK);
1581 		goto err;
1582 	}
1583 
1584 	if (pid == 0) {
1585 		(void) execvp(dtp->dt_cpp_path, argv);
1586 		_exit(errno == ENOENT ? 127 : 126);
1587 	}
1588 
1589 	do {
1590 		dt_dprintf("waiting for %s (PID %d)\n", dtp->dt_cpp_path,
1591 		    (int)pid);
1592 	} while (waitpid(pid, &wstat, 0) == -1 && errno == EINTR);
1593 
1594 	(void) sigaction(SIGCHLD, &oact, NULL);
1595 	(void) sigprocmask(SIG_SETMASK, &omask, NULL);
1596 
1597 	dt_dprintf("%s returned exit status 0x%x\n", dtp->dt_cpp_path, wstat);
1598 	estat = WIFEXITED(wstat) ? WEXITSTATUS(wstat) : -1;
1599 
1600 	if (estat != 0) {
1601 		switch (estat) {
1602 		case 126:
1603 			(void) dt_set_errno(dtp, EDT_CPPEXEC);
1604 			break;
1605 		case 127:
1606 			(void) dt_set_errno(dtp, EDT_CPPENT);
1607 			break;
1608 		default:
1609 			(void) dt_set_errno(dtp, EDT_CPPERR);
1610 		}
1611 		goto err;
1612 	}
1613 
1614 	free(argv);
1615 	(void) fflush(ofp);
1616 	(void) fseek(ofp, 0, SEEK_SET);
1617 	return (ofp);
1618 
1619 err:
1620 	free(argv);
1621 	(void) fclose(ofp);
1622 	return (NULL);
1623 }
1624 
1625 /*
1626  * Open all of the .d library files found in the specified directory and try to
1627  * compile each one in order to cache its inlines and translators, etc.  We
1628  * silently ignore any missing directories and other files found therein.
1629  * We only fail (and thereby fail dt_load_libs()) if we fail to compile a
1630  * library and the error is something other than #pragma D depends_on.
1631  * Dependency errors are silently ignored to permit a library directory to
1632  * contain libraries which may not be accessible depending on our privileges.
1633  *
1634  * Note that at present, no ordering is defined between library files found in
1635  * the same directory: if cross-library dependencies are eventually required,
1636  * we will need to extend the #pragma D depends_on directive with an additional
1637  * class for libraries, and this function will need to create a graph of the
1638  * various library pathnames and then perform a topological ordering using the
1639  * dependency information before we attempt to compile any of them.
1640  */
1641 static int
1642 dt_load_libs_dir(dtrace_hdl_t *dtp, const char *path)
1643 {
1644 	struct dirent *dp;
1645 	const char *p;
1646 	DIR *dirp;
1647 
1648 	char fname[PATH_MAX];
1649 	dtrace_prog_t *pgp;
1650 	FILE *fp;
1651 
1652 	if ((dirp = opendir(path)) == NULL) {
1653 		dt_dprintf("skipping lib dir %s: %s\n", path, strerror(errno));
1654 		return (0);
1655 	}
1656 
1657 	while ((dp = readdir(dirp)) != NULL) {
1658 		if ((p = strrchr(dp->d_name, '.')) == NULL || strcmp(p, ".d"))
1659 			continue; /* skip any filename not ending in .d */
1660 
1661 		(void) snprintf(fname, sizeof (fname),
1662 		    "%s/%s", path, dp->d_name);
1663 
1664 		if ((fp = fopen(fname, "r")) == NULL) {
1665 			dt_dprintf("skipping library %s: %s\n",
1666 			    fname, strerror(errno));
1667 			continue;
1668 		}
1669 
1670 		dtp->dt_filetag = fname;
1671 		pgp = dtrace_program_fcompile(dtp, fp, DTRACE_C_EMPTY, 0, NULL);
1672 		(void) fclose(fp);
1673 		dtp->dt_filetag = NULL;
1674 
1675 		if (pgp == NULL && (dtp->dt_errno != EDT_COMPILER ||
1676 		    dtp->dt_errtag != dt_errtag(D_PRAGMA_DEPEND))) {
1677 			(void) closedir(dirp);
1678 			return (-1); /* preserve dt_errno */
1679 		}
1680 
1681 		if (pgp == NULL) {
1682 			dt_dprintf("skipping library: %s\n",
1683 			    dtrace_errmsg(dtp, dtrace_errno(dtp)));
1684 		} else
1685 			dt_program_destroy(dtp, pgp);
1686 	}
1687 
1688 	(void) closedir(dirp);
1689 	return (0);
1690 }
1691 
1692 /*
1693  * Load the contents of any appropriate DTrace .d library files.  These files
1694  * contain inlines and translators that will be cached by the compiler.  We
1695  * defer this activity until the first compile to permit libdtrace clients to
1696  * add their own library directories and so that we can properly report errors.
1697  */
1698 static int
1699 dt_load_libs(dtrace_hdl_t *dtp)
1700 {
1701 	dt_dirpath_t *dirp;
1702 
1703 	if (dtp->dt_cflags & DTRACE_C_NOLIBS)
1704 		return (0); /* libraries already processed */
1705 
1706 	dtp->dt_cflags |= DTRACE_C_NOLIBS;
1707 
1708 	for (dirp = dt_list_next(&dtp->dt_lib_path);
1709 	    dirp != NULL; dirp = dt_list_next(dirp)) {
1710 		if (dt_load_libs_dir(dtp, dirp->dir_path) != 0) {
1711 			dtp->dt_cflags &= ~DTRACE_C_NOLIBS;
1712 			return (-1); /* errno is set for us */
1713 		}
1714 	}
1715 
1716 	return (0);
1717 }
1718 
1719 static void *
1720 dt_compile(dtrace_hdl_t *dtp, int context, dtrace_probespec_t pspec, void *arg,
1721     uint_t cflags, int argc, char *const argv[], FILE *fp, const char *s)
1722 {
1723 	dt_node_t *dnp;
1724 	dt_decl_t *ddp;
1725 	dt_pcb_t pcb;
1726 	void *rv;
1727 	int err;
1728 
1729 	if ((fp == NULL && s == NULL) || (cflags & ~DTRACE_C_MASK) != 0) {
1730 		(void) dt_set_errno(dtp, EINVAL);
1731 		return (NULL);
1732 	}
1733 
1734 	if (dt_list_next(&dtp->dt_lib_path) != NULL && dt_load_libs(dtp) != 0)
1735 		return (NULL); /* errno is set for us */
1736 
1737 	(void) ctf_discard(dtp->dt_cdefs->dm_ctfp);
1738 	(void) ctf_discard(dtp->dt_ddefs->dm_ctfp);
1739 
1740 	(void) dt_idhash_iter(dtp->dt_globals, dt_idreset, NULL);
1741 	(void) dt_idhash_iter(dtp->dt_tls, dt_idreset, NULL);
1742 
1743 	if (fp && (cflags & DTRACE_C_CPP) && (fp = dt_preproc(dtp, fp)) == NULL)
1744 		return (NULL); /* errno is set for us */
1745 
1746 	dt_pcb_push(dtp, &pcb);
1747 
1748 	pcb.pcb_fileptr = fp;
1749 	pcb.pcb_string = s;
1750 	pcb.pcb_strptr = s;
1751 	pcb.pcb_strlen = s ? strlen(s) : 0;
1752 	pcb.pcb_sargc = argc;
1753 	pcb.pcb_sargv = argv;
1754 	pcb.pcb_sflagv = argc ? calloc(argc, sizeof (ushort_t)) : NULL;
1755 	pcb.pcb_pspec = pspec;
1756 	pcb.pcb_cflags = dtp->dt_cflags | cflags;
1757 	pcb.pcb_amin = dtp->dt_amin;
1758 	pcb.pcb_yystate = -1;
1759 	pcb.pcb_context = context;
1760 	pcb.pcb_token = context;
1761 
1762 	if (context == DT_CTX_DPROG)
1763 		yybegin(YYS_CLAUSE);
1764 	else
1765 		yybegin(YYS_EXPR);
1766 
1767 	if ((err = setjmp(yypcb->pcb_jmpbuf)) != 0)
1768 		goto out;
1769 
1770 	if (yypcb->pcb_sargc != 0 && yypcb->pcb_sflagv == NULL)
1771 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
1772 
1773 	yypcb->pcb_idents = dt_idhash_create("ambiguous", NULL, 0, 0);
1774 	yypcb->pcb_locals = dt_idhash_create("clause local", NULL,
1775 	    DIF_VAR_OTHER_UBASE, DIF_VAR_OTHER_MAX);
1776 
1777 	if (yypcb->pcb_idents == NULL || yypcb->pcb_locals == NULL)
1778 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
1779 
1780 	/*
1781 	 * Invoke the parser to evaluate the D source code.  If any errors
1782 	 * occur during parsing, an error function will be called and we
1783 	 * will longjmp back to pcb_jmpbuf to abort.  If parsing succeeds,
1784 	 * we optionally display the parse tree if debugging is enabled.
1785 	 */
1786 	if (yyparse() != 0 || yypcb->pcb_root == NULL)
1787 		xyerror(D_EMPTY, "empty D program translation unit\n");
1788 
1789 	yybegin(YYS_DONE);
1790 
1791 	if (context != DT_CTX_DTYPE && DT_TREEDUMP_PASS(dtp, 1))
1792 		dt_node_printr(yypcb->pcb_root, stderr, 0);
1793 
1794 	if (yypcb->pcb_pragmas != NULL)
1795 		(void) dt_idhash_iter(yypcb->pcb_pragmas, dt_idpragma, NULL);
1796 
1797 	if (argc > 1 && !(yypcb->pcb_cflags & DTRACE_C_ARGREF) &&
1798 	    !(yypcb->pcb_sflagv[argc - 1] & DT_IDFLG_REF)) {
1799 		xyerror(D_MACRO_UNUSED, "extraneous argument '%s' ($%d is "
1800 		    "not referenced)\n", yypcb->pcb_sargv[argc - 1], argc - 1);
1801 	}
1802 
1803 	/*
1804 	 * If we have successfully created a parse tree for a D program, loop
1805 	 * over the clauses and actions and instantiate the corresponding
1806 	 * libdtrace program.  If we are parsing a D expression, then we
1807 	 * simply run the code generator and assembler on the resulting tree.
1808 	 */
1809 	switch (context) {
1810 	case DT_CTX_DPROG:
1811 		assert(yypcb->pcb_root->dn_kind == DT_NODE_PROG);
1812 
1813 		if ((dnp = yypcb->pcb_root->dn_list) == NULL &&
1814 		    !(yypcb->pcb_cflags & DTRACE_C_EMPTY))
1815 			xyerror(D_EMPTY, "empty D program translation unit\n");
1816 
1817 		if ((yypcb->pcb_prog = dt_program_create(dtp)) == NULL)
1818 			longjmp(yypcb->pcb_jmpbuf, dtrace_errno(dtp));
1819 
1820 		for (; dnp != NULL; dnp = dnp->dn_list) {
1821 			switch (dnp->dn_kind) {
1822 			case DT_NODE_CLAUSE:
1823 				dt_compile_clause(dtp, dnp);
1824 				break;
1825 			case DT_NODE_XLATOR:
1826 				if (dtp->dt_xlatemode == DT_XL_DYNAMIC)
1827 					dt_compile_xlator(dnp);
1828 				break;
1829 			case DT_NODE_PROVIDER:
1830 				(void) dt_node_cook(dnp, DT_IDFLG_REF);
1831 				break;
1832 			}
1833 		}
1834 
1835 		yypcb->pcb_prog->dp_xrefs = yypcb->pcb_asxrefs;
1836 		yypcb->pcb_prog->dp_xrefslen = yypcb->pcb_asxreflen;
1837 		yypcb->pcb_asxrefs = NULL;
1838 		yypcb->pcb_asxreflen = 0;
1839 
1840 		rv = yypcb->pcb_prog;
1841 		break;
1842 
1843 	case DT_CTX_DEXPR:
1844 		(void) dt_node_cook(yypcb->pcb_root, DT_IDFLG_REF);
1845 		dt_cg(yypcb, yypcb->pcb_root);
1846 		rv = dt_as(yypcb);
1847 		break;
1848 
1849 	case DT_CTX_DTYPE:
1850 		ddp = (dt_decl_t *)yypcb->pcb_root; /* root is really a decl */
1851 		err = dt_decl_type(ddp, arg);
1852 		dt_decl_free(ddp);
1853 
1854 		if (err != 0)
1855 			longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
1856 
1857 		rv = NULL;
1858 		break;
1859 	}
1860 
1861 out:
1862 	if (context != DT_CTX_DTYPE && DT_TREEDUMP_PASS(dtp, 3))
1863 		dt_node_printr(yypcb->pcb_root, stderr, 0);
1864 
1865 	if (dtp->dt_cdefs_fd != -1 && (ftruncate64(dtp->dt_cdefs_fd, 0) == -1 ||
1866 	    lseek64(dtp->dt_cdefs_fd, 0, SEEK_SET) == -1 ||
1867 	    ctf_write(dtp->dt_cdefs->dm_ctfp, dtp->dt_cdefs_fd) == CTF_ERR))
1868 		dt_dprintf("failed to update CTF cache: %s\n", strerror(errno));
1869 
1870 	if (dtp->dt_ddefs_fd != -1 && (ftruncate64(dtp->dt_ddefs_fd, 0) == -1 ||
1871 	    lseek64(dtp->dt_ddefs_fd, 0, SEEK_SET) == -1 ||
1872 	    ctf_write(dtp->dt_ddefs->dm_ctfp, dtp->dt_ddefs_fd) == CTF_ERR))
1873 		dt_dprintf("failed to update CTF cache: %s\n", strerror(errno));
1874 
1875 	if (yypcb->pcb_fileptr && (cflags & DTRACE_C_CPP))
1876 		(void) fclose(yypcb->pcb_fileptr); /* close dt_preproc() file */
1877 
1878 	dt_pcb_pop(dtp, err);
1879 	(void) dt_set_errno(dtp, err);
1880 	return (err ? NULL : rv);
1881 }
1882 
1883 dtrace_prog_t *
1884 dtrace_program_strcompile(dtrace_hdl_t *dtp, const char *s,
1885     dtrace_probespec_t spec, uint_t cflags, int argc, char *const argv[])
1886 {
1887 	return (dt_compile(dtp, DT_CTX_DPROG,
1888 	    spec, NULL, cflags, argc, argv, NULL, s));
1889 }
1890 
1891 dtrace_prog_t *
1892 dtrace_program_fcompile(dtrace_hdl_t *dtp, FILE *fp,
1893     uint_t cflags, int argc, char *const argv[])
1894 {
1895 	return (dt_compile(dtp, DT_CTX_DPROG,
1896 	    DTRACE_PROBESPEC_NAME, NULL, cflags, argc, argv, fp, NULL));
1897 }
1898 
1899 int
1900 dtrace_type_strcompile(dtrace_hdl_t *dtp, const char *s, dtrace_typeinfo_t *dtt)
1901 {
1902 	(void) dt_compile(dtp, DT_CTX_DTYPE,
1903 	    DTRACE_PROBESPEC_NONE, dtt, 0, 0, NULL, NULL, s);
1904 	return (dtp->dt_errno ? -1 : 0);
1905 }
1906 
1907 int
1908 dtrace_type_fcompile(dtrace_hdl_t *dtp, FILE *fp, dtrace_typeinfo_t *dtt)
1909 {
1910 	(void) dt_compile(dtp, DT_CTX_DTYPE,
1911 	    DTRACE_PROBESPEC_NONE, dtt, 0, 0, NULL, fp, NULL);
1912 	return (dtp->dt_errno ? -1 : 0);
1913 }
1914