xref: /freebsd-src/cddl/contrib/opensolaris/lib/libdtrace/common/dt_pcb.c (revision 6ff6d951ade3f3379932df7f878ef3ea272cfc59)
1*6ff6d951SJohn Birrell /*
2*6ff6d951SJohn Birrell  * CDDL HEADER START
3*6ff6d951SJohn Birrell  *
4*6ff6d951SJohn Birrell  * The contents of this file are subject to the terms of the
5*6ff6d951SJohn Birrell  * Common Development and Distribution License, Version 1.0 only
6*6ff6d951SJohn Birrell  * (the "License").  You may not use this file except in compliance
7*6ff6d951SJohn Birrell  * with the License.
8*6ff6d951SJohn Birrell  *
9*6ff6d951SJohn Birrell  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*6ff6d951SJohn Birrell  * or http://www.opensolaris.org/os/licensing.
11*6ff6d951SJohn Birrell  * See the License for the specific language governing permissions
12*6ff6d951SJohn Birrell  * and limitations under the License.
13*6ff6d951SJohn Birrell  *
14*6ff6d951SJohn Birrell  * When distributing Covered Code, include this CDDL HEADER in each
15*6ff6d951SJohn Birrell  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*6ff6d951SJohn Birrell  * If applicable, add the following below this CDDL HEADER, with the
17*6ff6d951SJohn Birrell  * fields enclosed by brackets "[]" replaced with your own identifying
18*6ff6d951SJohn Birrell  * information: Portions Copyright [yyyy] [name of copyright owner]
19*6ff6d951SJohn Birrell  *
20*6ff6d951SJohn Birrell  * CDDL HEADER END
21*6ff6d951SJohn Birrell  */
22*6ff6d951SJohn Birrell /*
23*6ff6d951SJohn Birrell  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*6ff6d951SJohn Birrell  * Use is subject to license terms.
25*6ff6d951SJohn Birrell  */
26*6ff6d951SJohn Birrell 
27*6ff6d951SJohn Birrell #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*6ff6d951SJohn Birrell 
29*6ff6d951SJohn Birrell /*
30*6ff6d951SJohn Birrell  * DTrace Parsing Control Block
31*6ff6d951SJohn Birrell  *
32*6ff6d951SJohn Birrell  * A DTrace Parsing Control Block (PCB) contains all of the state that is used
33*6ff6d951SJohn Birrell  * by a single pass of the D compiler, other than the global variables used by
34*6ff6d951SJohn Birrell  * lex and yacc.  The routines in this file are used to set up and tear down
35*6ff6d951SJohn Birrell  * PCBs, which are kept on a stack pointed to by the libdtrace global 'yypcb'.
36*6ff6d951SJohn Birrell  * The main engine of the compiler, dt_compile(), is located in dt_cc.c and is
37*6ff6d951SJohn Birrell  * responsible for calling these routines to begin and end a compilation pass.
38*6ff6d951SJohn Birrell  *
39*6ff6d951SJohn Birrell  * Sun's lex/yacc are not MT-safe or re-entrant, but we permit limited nested
40*6ff6d951SJohn Birrell  * use of dt_compile() once the entire parse tree has been constructed but has
41*6ff6d951SJohn Birrell  * not yet executed the "cooking" pass (see dt_cc.c for more information).  The
42*6ff6d951SJohn Birrell  * PCB design also makes it easier to debug (since all global state is kept in
43*6ff6d951SJohn Birrell  * one place) and could permit us to make the D compiler MT-safe or re-entrant
44*6ff6d951SJohn Birrell  * in the future by adding locks to libdtrace or switching to Flex and Bison.
45*6ff6d951SJohn Birrell  */
46*6ff6d951SJohn Birrell 
47*6ff6d951SJohn Birrell #include <strings.h>
48*6ff6d951SJohn Birrell #include <stdlib.h>
49*6ff6d951SJohn Birrell #include <assert.h>
50*6ff6d951SJohn Birrell 
51*6ff6d951SJohn Birrell #include <dt_impl.h>
52*6ff6d951SJohn Birrell #include <dt_program.h>
53*6ff6d951SJohn Birrell #include <dt_provider.h>
54*6ff6d951SJohn Birrell #include <dt_pcb.h>
55*6ff6d951SJohn Birrell 
56*6ff6d951SJohn Birrell /*
57*6ff6d951SJohn Birrell  * Initialize the specified PCB by zeroing it and filling in a few default
58*6ff6d951SJohn Birrell  * members, and then pushing it on to the top of the PCB stack and setting
59*6ff6d951SJohn Birrell  * yypcb to point to it.  Increment the current handle's generation count.
60*6ff6d951SJohn Birrell  */
61*6ff6d951SJohn Birrell void
dt_pcb_push(dtrace_hdl_t * dtp,dt_pcb_t * pcb)62*6ff6d951SJohn Birrell dt_pcb_push(dtrace_hdl_t *dtp, dt_pcb_t *pcb)
63*6ff6d951SJohn Birrell {
64*6ff6d951SJohn Birrell 	/*
65*6ff6d951SJohn Birrell 	 * Since lex/yacc are not re-entrant and we don't implement state save,
66*6ff6d951SJohn Birrell 	 * assert that if another PCB is active, it is from the same handle and
67*6ff6d951SJohn Birrell 	 * has completed execution of yyparse().  If the first assertion fires,
68*6ff6d951SJohn Birrell 	 * the caller is calling libdtrace without proper MT locking.  If the
69*6ff6d951SJohn Birrell 	 * second assertion fires, dt_compile() is being called recursively
70*6ff6d951SJohn Birrell 	 * from an illegal location in libdtrace, or a dt_pcb_pop() is missing.
71*6ff6d951SJohn Birrell 	 */
72*6ff6d951SJohn Birrell 	if (yypcb != NULL) {
73*6ff6d951SJohn Birrell 		assert(yypcb->pcb_hdl == dtp);
74*6ff6d951SJohn Birrell 		assert(yypcb->pcb_yystate == YYS_DONE);
75*6ff6d951SJohn Birrell 	}
76*6ff6d951SJohn Birrell 
77*6ff6d951SJohn Birrell 	bzero(pcb, sizeof (dt_pcb_t));
78*6ff6d951SJohn Birrell 
79*6ff6d951SJohn Birrell 	dt_scope_create(&pcb->pcb_dstack);
80*6ff6d951SJohn Birrell 	dt_idstack_push(&pcb->pcb_globals, dtp->dt_globals);
81*6ff6d951SJohn Birrell 	dt_irlist_create(&pcb->pcb_ir);
82*6ff6d951SJohn Birrell 
83*6ff6d951SJohn Birrell 	pcb->pcb_hdl = dtp;
84*6ff6d951SJohn Birrell 	pcb->pcb_prev = dtp->dt_pcb;
85*6ff6d951SJohn Birrell 
86*6ff6d951SJohn Birrell 	dtp->dt_pcb = pcb;
87*6ff6d951SJohn Birrell 	dtp->dt_gen++;
88*6ff6d951SJohn Birrell 
89*6ff6d951SJohn Birrell 	yyinit(pcb);
90*6ff6d951SJohn Birrell }
91*6ff6d951SJohn Birrell 
92*6ff6d951SJohn Birrell static int
dt_pcb_pop_ident(dt_idhash_t * dhp,dt_ident_t * idp,void * arg)93*6ff6d951SJohn Birrell dt_pcb_pop_ident(dt_idhash_t *dhp, dt_ident_t *idp, void *arg)
94*6ff6d951SJohn Birrell {
95*6ff6d951SJohn Birrell 	dtrace_hdl_t *dtp = arg;
96*6ff6d951SJohn Birrell 
97*6ff6d951SJohn Birrell 	if (idp->di_gen == dtp->dt_gen)
98*6ff6d951SJohn Birrell 		dt_idhash_delete(dhp, idp);
99*6ff6d951SJohn Birrell 
100*6ff6d951SJohn Birrell 	return (0);
101*6ff6d951SJohn Birrell }
102*6ff6d951SJohn Birrell 
103*6ff6d951SJohn Birrell /*
104*6ff6d951SJohn Birrell  * Pop the topmost PCB from the PCB stack and destroy any data structures that
105*6ff6d951SJohn Birrell  * are associated with it.  If 'err' is non-zero, destroy any intermediate
106*6ff6d951SJohn Birrell  * state that is left behind as part of a compilation that has failed.
107*6ff6d951SJohn Birrell  */
108*6ff6d951SJohn Birrell void
dt_pcb_pop(dtrace_hdl_t * dtp,int err)109*6ff6d951SJohn Birrell dt_pcb_pop(dtrace_hdl_t *dtp, int err)
110*6ff6d951SJohn Birrell {
111*6ff6d951SJohn Birrell 	dt_pcb_t *pcb = yypcb;
112*6ff6d951SJohn Birrell 	uint_t i;
113*6ff6d951SJohn Birrell 
114*6ff6d951SJohn Birrell 	assert(pcb != NULL);
115*6ff6d951SJohn Birrell 	assert(pcb == dtp->dt_pcb);
116*6ff6d951SJohn Birrell 
117*6ff6d951SJohn Birrell 	while (pcb->pcb_dstack.ds_next != NULL)
118*6ff6d951SJohn Birrell 		(void) dt_scope_pop();
119*6ff6d951SJohn Birrell 
120*6ff6d951SJohn Birrell 	dt_scope_destroy(&pcb->pcb_dstack);
121*6ff6d951SJohn Birrell 	dt_irlist_destroy(&pcb->pcb_ir);
122*6ff6d951SJohn Birrell 
123*6ff6d951SJohn Birrell 	dt_node_link_free(&pcb->pcb_list);
124*6ff6d951SJohn Birrell 	dt_node_link_free(&pcb->pcb_hold);
125*6ff6d951SJohn Birrell 
126*6ff6d951SJohn Birrell 	if (err != 0) {
127*6ff6d951SJohn Birrell 		dt_xlator_t *dxp, *nxp;
128*6ff6d951SJohn Birrell 		dt_provider_t *pvp, *nvp;
129*6ff6d951SJohn Birrell 
130*6ff6d951SJohn Birrell 		if (pcb->pcb_prog != NULL)
131*6ff6d951SJohn Birrell 			dt_program_destroy(dtp, pcb->pcb_prog);
132*6ff6d951SJohn Birrell 		if (pcb->pcb_stmt != NULL)
133*6ff6d951SJohn Birrell 			dtrace_stmt_destroy(dtp, pcb->pcb_stmt);
134*6ff6d951SJohn Birrell 		if (pcb->pcb_ecbdesc != NULL)
135*6ff6d951SJohn Birrell 			dt_ecbdesc_release(dtp, pcb->pcb_ecbdesc);
136*6ff6d951SJohn Birrell 
137*6ff6d951SJohn Birrell 		for (dxp = dt_list_next(&dtp->dt_xlators); dxp; dxp = nxp) {
138*6ff6d951SJohn Birrell 			nxp = dt_list_next(dxp);
139*6ff6d951SJohn Birrell 			if (dxp->dx_gen == dtp->dt_gen)
140*6ff6d951SJohn Birrell 				dt_xlator_destroy(dtp, dxp);
141*6ff6d951SJohn Birrell 		}
142*6ff6d951SJohn Birrell 
143*6ff6d951SJohn Birrell 		for (pvp = dt_list_next(&dtp->dt_provlist); pvp; pvp = nvp) {
144*6ff6d951SJohn Birrell 			nvp = dt_list_next(pvp);
145*6ff6d951SJohn Birrell 			if (pvp->pv_gen == dtp->dt_gen)
146*6ff6d951SJohn Birrell 				dt_provider_destroy(dtp, pvp);
147*6ff6d951SJohn Birrell 		}
148*6ff6d951SJohn Birrell 
149*6ff6d951SJohn Birrell 		(void) dt_idhash_iter(dtp->dt_aggs, dt_pcb_pop_ident, dtp);
150*6ff6d951SJohn Birrell 		dt_idhash_update(dtp->dt_aggs);
151*6ff6d951SJohn Birrell 
152*6ff6d951SJohn Birrell 		(void) dt_idhash_iter(dtp->dt_globals, dt_pcb_pop_ident, dtp);
153*6ff6d951SJohn Birrell 		dt_idhash_update(dtp->dt_globals);
154*6ff6d951SJohn Birrell 
155*6ff6d951SJohn Birrell 		(void) dt_idhash_iter(dtp->dt_tls, dt_pcb_pop_ident, dtp);
156*6ff6d951SJohn Birrell 		dt_idhash_update(dtp->dt_tls);
157*6ff6d951SJohn Birrell 
158*6ff6d951SJohn Birrell 		(void) ctf_discard(dtp->dt_cdefs->dm_ctfp);
159*6ff6d951SJohn Birrell 		(void) ctf_discard(dtp->dt_ddefs->dm_ctfp);
160*6ff6d951SJohn Birrell 	}
161*6ff6d951SJohn Birrell 
162*6ff6d951SJohn Birrell 	if (pcb->pcb_pragmas != NULL)
163*6ff6d951SJohn Birrell 		dt_idhash_destroy(pcb->pcb_pragmas);
164*6ff6d951SJohn Birrell 	if (pcb->pcb_locals != NULL)
165*6ff6d951SJohn Birrell 		dt_idhash_destroy(pcb->pcb_locals);
166*6ff6d951SJohn Birrell 	if (pcb->pcb_idents != NULL)
167*6ff6d951SJohn Birrell 		dt_idhash_destroy(pcb->pcb_idents);
168*6ff6d951SJohn Birrell 	if (pcb->pcb_inttab != NULL)
169*6ff6d951SJohn Birrell 		dt_inttab_destroy(pcb->pcb_inttab);
170*6ff6d951SJohn Birrell 	if (pcb->pcb_strtab != NULL)
171*6ff6d951SJohn Birrell 		dt_strtab_destroy(pcb->pcb_strtab);
172*6ff6d951SJohn Birrell 	if (pcb->pcb_regs != NULL)
173*6ff6d951SJohn Birrell 		dt_regset_destroy(pcb->pcb_regs);
174*6ff6d951SJohn Birrell 
175*6ff6d951SJohn Birrell 	for (i = 0; i < pcb->pcb_asxreflen; i++)
176*6ff6d951SJohn Birrell 		dt_free(dtp, pcb->pcb_asxrefs[i]);
177*6ff6d951SJohn Birrell 
178*6ff6d951SJohn Birrell 	dt_free(dtp, pcb->pcb_asxrefs);
179*6ff6d951SJohn Birrell 	dt_difo_free(dtp, pcb->pcb_difo);
180*6ff6d951SJohn Birrell 
181*6ff6d951SJohn Birrell 	free(pcb->pcb_filetag);
182*6ff6d951SJohn Birrell 	free(pcb->pcb_sflagv);
183*6ff6d951SJohn Birrell 
184*6ff6d951SJohn Birrell 	dtp->dt_pcb = pcb->pcb_prev;
185*6ff6d951SJohn Birrell 	bzero(pcb, sizeof (dt_pcb_t));
186*6ff6d951SJohn Birrell 	yyinit(dtp->dt_pcb);
187*6ff6d951SJohn Birrell }
188