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