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