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