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