14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync/*
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * CDDL HEADER START
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync *
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * The contents of this file are subject to the terms of the
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * Common Development and Distribution License, Version 1.0 only
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * (the "License"). You may not use this file except in compliance
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * with the License.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync *
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * or http://www.opensolaris.org/os/licensing.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * See the License for the specific language governing permissions
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * and limitations under the License.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync *
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * When distributing Covered Code, include this CDDL HEADER in each
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * If applicable, add the following below this CDDL HEADER, with the
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * fields enclosed by brackets "[]" replaced with your own identifying
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * information: Portions Copyright [yyyy] [name of copyright owner]
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync *
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * CDDL HEADER END
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync/*
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * Use is subject to license terms.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
15131f635944423d12704f2e6e51799511ac51ccvboxsync#ifndef VBOX
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#pragma ident "%Z%%M% %I% %E% SMI"
15131f635944423d12704f2e6e51799511ac51ccvboxsync#endif
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync/*
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * DTrace Parsing Control Block
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync *
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * A DTrace Parsing Control Block (PCB) contains all of the state that is used
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * by a single pass of the D compiler, other than the global variables used by
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * lex and yacc. The routines in this file are used to set up and tear down
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * PCBs, which are kept on a stack pointed to by the libdtrace global 'yypcb'.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * The main engine of the compiler, dt_compile(), is located in dt_cc.c and is
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * responsible for calling these routines to begin and end a compilation pass.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync *
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * Sun's lex/yacc are not MT-safe or re-entrant, but we permit limited nested
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * use of dt_compile() once the entire parse tree has been constructed but has
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * not yet executed the "cooking" pass (see dt_cc.c for more information). The
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * PCB design also makes it easier to debug (since all global state is kept in
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * one place) and could permit us to make the D compiler MT-safe or re-entrant
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * in the future by adding locks to libdtrace or switching to Flex and Bison.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
15131f635944423d12704f2e6e51799511ac51ccvboxsync#ifndef VBOX
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#include <strings.h>
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#include <stdlib.h>
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#include <assert.h>
15131f635944423d12704f2e6e51799511ac51ccvboxsync#endif
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#include <dt_impl.h>
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#include <dt_program.h>
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#include <dt_provider.h>
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#include <dt_pcb.h>
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync/*
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * Initialize the specified PCB by zeroing it and filling in a few default
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * members, and then pushing it on to the top of the PCB stack and setting
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * yypcb to point to it. Increment the current handle's generation count.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsyncvoid
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsyncdt_pcb_push(dtrace_hdl_t *dtp, dt_pcb_t *pcb)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync{
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync /*
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * Since lex/yacc are not re-entrant and we don't implement state save,
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * assert that if another PCB is active, it is from the same handle and
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * has completed execution of yyparse(). If the first assertion fires,
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * the caller is calling libdtrace without proper MT locking. If the
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * second assertion fires, dt_compile() is being called recursively
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * from an illegal location in libdtrace, or a dt_pcb_pop() is missing.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (yypcb != NULL) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync assert(yypcb->pcb_hdl == dtp);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync assert(yypcb->pcb_yystate == YYS_DONE);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync bzero(pcb, sizeof (dt_pcb_t));
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dt_scope_create(&pcb->pcb_dstack);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dt_idstack_push(&pcb->pcb_globals, dtp->dt_globals);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dt_irlist_create(&pcb->pcb_ir);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync pcb->pcb_hdl = dtp;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync pcb->pcb_prev = dtp->dt_pcb;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dtp->dt_pcb = pcb;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dtp->dt_gen++;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
2fc831009973ba4f02b68bfccfd69fa85d8f522bvboxsync#ifndef USING_FLEX /* In case flex starts work too early. Moved to dt_compile. */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync yyinit(pcb);
2fc831009973ba4f02b68bfccfd69fa85d8f522bvboxsync#endif
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync}
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsyncstatic int
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsyncdt_pcb_pop_ident(dt_idhash_t *dhp, dt_ident_t *idp, void *arg)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync{
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dtrace_hdl_t *dtp = arg;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (idp->di_gen == dtp->dt_gen)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dt_idhash_delete(dhp, idp);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync return (0);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync}
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync/*
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * Pop the topmost PCB from the PCB stack and destroy any data structures that
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * are associated with it. If 'err' is non-zero, destroy any intermediate
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * state that is left behind as part of a compilation that has failed.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsyncvoid
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsyncdt_pcb_pop(dtrace_hdl_t *dtp, int err)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync{
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dt_pcb_t *pcb = yypcb;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync uint_t i;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync assert(pcb != NULL);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync assert(pcb == dtp->dt_pcb);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync while (pcb->pcb_dstack.ds_next != NULL)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync (void) dt_scope_pop();
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dt_scope_destroy(&pcb->pcb_dstack);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dt_irlist_destroy(&pcb->pcb_ir);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dt_node_link_free(&pcb->pcb_list);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dt_node_link_free(&pcb->pcb_hold);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (err != 0) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dt_xlator_t *dxp, *nxp;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dt_provider_t *pvp, *nvp;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (pcb->pcb_prog != NULL)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dt_program_destroy(dtp, pcb->pcb_prog);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (pcb->pcb_stmt != NULL)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dtrace_stmt_destroy(dtp, pcb->pcb_stmt);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (pcb->pcb_ecbdesc != NULL)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dt_ecbdesc_release(dtp, pcb->pcb_ecbdesc);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync for (dxp = dt_list_next(&dtp->dt_xlators); dxp; dxp = nxp) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync nxp = dt_list_next(dxp);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (dxp->dx_gen == dtp->dt_gen)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dt_xlator_destroy(dtp, dxp);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync for (pvp = dt_list_next(&dtp->dt_provlist); pvp; pvp = nvp) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync nvp = dt_list_next(pvp);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (pvp->pv_gen == dtp->dt_gen)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dt_provider_destroy(dtp, pvp);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync (void) dt_idhash_iter(dtp->dt_aggs, dt_pcb_pop_ident, dtp);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dt_idhash_update(dtp->dt_aggs);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync (void) dt_idhash_iter(dtp->dt_globals, dt_pcb_pop_ident, dtp);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dt_idhash_update(dtp->dt_globals);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync (void) dt_idhash_iter(dtp->dt_tls, dt_pcb_pop_ident, dtp);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dt_idhash_update(dtp->dt_tls);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync (void) ctf_discard(dtp->dt_cdefs->dm_ctfp);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync (void) ctf_discard(dtp->dt_ddefs->dm_ctfp);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (pcb->pcb_pragmas != NULL)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dt_idhash_destroy(pcb->pcb_pragmas);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (pcb->pcb_locals != NULL)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dt_idhash_destroy(pcb->pcb_locals);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (pcb->pcb_idents != NULL)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dt_idhash_destroy(pcb->pcb_idents);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (pcb->pcb_inttab != NULL)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dt_inttab_destroy(pcb->pcb_inttab);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (pcb->pcb_strtab != NULL)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dt_strtab_destroy(pcb->pcb_strtab);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (pcb->pcb_regs != NULL)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dt_regset_destroy(pcb->pcb_regs);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync for (i = 0; i < pcb->pcb_asxreflen; i++)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dt_free(dtp, pcb->pcb_asxrefs[i]);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dt_free(dtp, pcb->pcb_asxrefs);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dt_difo_free(dtp, pcb->pcb_difo);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync free(pcb->pcb_filetag);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync free(pcb->pcb_sflagv);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dtp->dt_pcb = pcb->pcb_prev;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync bzero(pcb, sizeof (dt_pcb_t));
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync yyinit(dtp->dt_pcb);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync}