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#include <strings.h>
2N/A#include <assert.h>
2N/A
2N/A#include <dt_xlator.h>
2N/A#include <dt_parser.h>
2N/A#include <dt_grammar.h>
2N/A#include <dt_module.h>
2N/A#include <dt_impl.h>
2N/A
2N/A/*
2N/A * Create a member node corresponding to one of the output members of a dynamic
2N/A * translator. We set the member's dn_membexpr to a DT_NODE_XLATOR node that
2N/A * has dn_op set to DT_TOK_XLATE and refers back to the translator itself. The
2N/A * code generator will then use this as the indicator for dynamic translation.
2N/A */
2N/A/*ARGSUSED*/
2N/Astatic int
2N/Adt_xlator_create_member(const char *name, ctf_id_t type, ulong_t off, void *arg)
2N/A{
2N/A dt_xlator_t *dxp = arg;
2N/A dtrace_hdl_t *dtp = dxp->dx_hdl;
2N/A dt_node_t *enp, *mnp;
2N/A
2N/A if ((enp = dt_node_xalloc(dtp, DT_NODE_XLATOR)) == NULL)
2N/A return (dt_set_errno(dtp, EDT_NOMEM));
2N/A
2N/A enp->dn_link = dxp->dx_nodes;
2N/A dxp->dx_nodes = enp;
2N/A
2N/A if ((mnp = dt_node_xalloc(dtp, DT_NODE_MEMBER)) == NULL)
2N/A return (dt_set_errno(dtp, EDT_NOMEM));
2N/A
2N/A mnp->dn_link = dxp->dx_nodes;
2N/A dxp->dx_nodes = mnp;
2N/A
2N/A /*
2N/A * For the member expression, we use a DT_NODE_XLATOR/TOK_XLATE whose
2N/A * xlator refers back to the translator and whose dn_xmember refers to
2N/A * the current member. These refs will be used by dt_cg.c and dt_as.c.
2N/A */
2N/A enp->dn_op = DT_TOK_XLATE;
2N/A enp->dn_xlator = dxp;
2N/A enp->dn_xmember = mnp;
2N/A dt_node_type_assign(enp, dxp->dx_dst_ctfp, type);
2N/A
2N/A /*
2N/A * For the member itself, we use a DT_NODE_MEMBER as usual with the
2N/A * appropriate name, output type, and member expression set to 'enp'.
2N/A */
2N/A if (dxp->dx_members != NULL) {
2N/A assert(enp->dn_link->dn_kind == DT_NODE_MEMBER);
2N/A enp->dn_link->dn_list = mnp;
2N/A } else
2N/A dxp->dx_members = mnp;
2N/A
2N/A mnp->dn_membname = strdup(name);
2N/A mnp->dn_membexpr = enp;
2N/A dt_node_type_assign(mnp, dxp->dx_dst_ctfp, type);
2N/A
2N/A if (mnp->dn_membname == NULL)
2N/A return (dt_set_errno(dtp, EDT_NOMEM));
2N/A
2N/A return (0);
2N/A}
2N/A
2N/Adt_xlator_t *
2N/Adt_xlator_create(dtrace_hdl_t *dtp,
2N/A const dtrace_typeinfo_t *src, const dtrace_typeinfo_t *dst,
2N/A const char *name, dt_node_t *members, dt_node_t *nodes)
2N/A{
2N/A dt_xlator_t *dxp = dt_zalloc(dtp, sizeof (dt_xlator_t));
2N/A dtrace_typeinfo_t ptr = *dst;
2N/A dt_xlator_t **map;
2N/A dt_node_t *dnp;
2N/A uint_t kind;
2N/A
2N/A if (dxp == NULL)
2N/A return (NULL);
2N/A
2N/A dxp->dx_hdl = dtp;
2N/A dxp->dx_id = dtp->dt_xlatorid++;
2N/A dxp->dx_gen = dtp->dt_gen;
2N/A dxp->dx_arg = -1;
2N/A
2N/A if ((map = dt_alloc(dtp, sizeof (void *) * (dxp->dx_id + 1))) == NULL) {
2N/A dt_free(dtp, dxp);
2N/A return (NULL);
2N/A }
2N/A
2N/A dt_list_append(&dtp->dt_xlators, dxp);
2N/A bcopy(dtp->dt_xlatormap, map, sizeof (void *) * dxp->dx_id);
2N/A dt_free(dtp, dtp->dt_xlatormap);
2N/A dtp->dt_xlatormap = map;
2N/A dtp->dt_xlatormap[dxp->dx_id] = dxp;
2N/A
2N/A if (dt_type_pointer(&ptr) == -1) {
2N/A ptr.dtt_ctfp = NULL;
2N/A ptr.dtt_type = CTF_ERR;
2N/A }
2N/A
2N/A dxp->dx_ident = dt_ident_create(name ? name : "T",
2N/A DT_IDENT_SCALAR, DT_IDFLG_REF | DT_IDFLG_ORPHAN, 0,
2N/A _dtrace_defattr, 0, &dt_idops_thaw, NULL, dtp->dt_gen);
2N/A
2N/A if (dxp->dx_ident == NULL)
2N/A goto err; /* no memory for identifier */
2N/A
2N/A dxp->dx_ident->di_ctfp = src->dtt_ctfp;
2N/A dxp->dx_ident->di_type = src->dtt_type;
2N/A
2N/A /*
2N/A * If an input parameter name is given, this is a static translator
2N/A * definition: create an idhash and identifier for the parameter.
2N/A */
2N/A if (name != NULL) {
2N/A dxp->dx_locals = dt_idhash_create("xlparams", NULL, 0, 0);
2N/A
2N/A if (dxp->dx_locals == NULL)
2N/A goto err; /* no memory for identifier hash */
2N/A
2N/A dt_idhash_xinsert(dxp->dx_locals, dxp->dx_ident);
2N/A }
2N/A
2N/A dxp->dx_souid.di_name = "translator";
2N/A dxp->dx_souid.di_kind = DT_IDENT_XLSOU;
2N/A dxp->dx_souid.di_flags = DT_IDFLG_REF;
2N/A dxp->dx_souid.di_id = dxp->dx_id;
2N/A dxp->dx_souid.di_attr = _dtrace_defattr;
2N/A dxp->dx_souid.di_ops = &dt_idops_thaw;
2N/A dxp->dx_souid.di_data = dxp;
2N/A dxp->dx_souid.di_ctfp = dst->dtt_ctfp;
2N/A dxp->dx_souid.di_type = dst->dtt_type;
2N/A dxp->dx_souid.di_gen = dtp->dt_gen;
2N/A
2N/A dxp->dx_ptrid.di_name = "translator";
2N/A dxp->dx_ptrid.di_kind = DT_IDENT_XLPTR;
2N/A dxp->dx_ptrid.di_flags = DT_IDFLG_REF;
2N/A dxp->dx_ptrid.di_id = dxp->dx_id;
2N/A dxp->dx_ptrid.di_attr = _dtrace_defattr;
2N/A dxp->dx_ptrid.di_ops = &dt_idops_thaw;
2N/A dxp->dx_ptrid.di_data = dxp;
2N/A dxp->dx_ptrid.di_ctfp = ptr.dtt_ctfp;
2N/A dxp->dx_ptrid.di_type = ptr.dtt_type;
2N/A dxp->dx_ptrid.di_gen = dtp->dt_gen;
2N/A
2N/A /*
2N/A * If a deferred pragma is pending on the keyword "translator", run all
2N/A * the deferred pragmas on dx_souid and then copy results to dx_ptrid.
2N/A * See the code in dt_pragma.c for details on deferred ident pragmas.
2N/A */
2N/A if (dtp->dt_globals->dh_defer != NULL && yypcb->pcb_pragmas != NULL &&
2N/A dt_idhash_lookup(yypcb->pcb_pragmas, "translator") != NULL) {
2N/A dtp->dt_globals->dh_defer(dtp->dt_globals, &dxp->dx_souid);
2N/A dxp->dx_ptrid.di_attr = dxp->dx_souid.di_attr;
2N/A dxp->dx_ptrid.di_vers = dxp->dx_souid.di_vers;
2N/A }
2N/A
2N/A dxp->dx_src_ctfp = src->dtt_ctfp;
2N/A dxp->dx_src_type = src->dtt_type;
2N/A dxp->dx_src_base = ctf_type_resolve(src->dtt_ctfp, src->dtt_type);
2N/A
2N/A dxp->dx_dst_ctfp = dst->dtt_ctfp;
2N/A dxp->dx_dst_type = dst->dtt_type;
2N/A dxp->dx_dst_base = ctf_type_resolve(dst->dtt_ctfp, dst->dtt_type);
2N/A
2N/A kind = ctf_type_kind(dst->dtt_ctfp, dxp->dx_dst_base);
2N/A assert(kind == CTF_K_STRUCT || kind == CTF_K_UNION);
2N/A
2N/A /*
2N/A * If no input parameter is given, we're making a dynamic translator:
2N/A * create member nodes for every member of the output type. Otherwise
2N/A * retain the member and allocation node lists presented by the parser.
2N/A */
2N/A if (name == NULL) {
2N/A if (ctf_member_iter(dxp->dx_dst_ctfp, dxp->dx_dst_base,
2N/A dt_xlator_create_member, dxp) != 0)
2N/A goto err;
2N/A } else {
2N/A dxp->dx_members = members;
2N/A dxp->dx_nodes = nodes;
2N/A }
2N/A
2N/A /*
2N/A * Assign member IDs to each member and allocate space for DIFOs
2N/A * if and when this translator is eventually compiled.
2N/A */
2N/A for (dnp = dxp->dx_members; dnp != NULL; dnp = dnp->dn_list) {
2N/A dnp->dn_membxlator = dxp;
2N/A dnp->dn_membid = dxp->dx_nmembers++;
2N/A }
2N/A
2N/A dxp->dx_membdif = dt_zalloc(dtp,
2N/A sizeof (dtrace_difo_t *) * dxp->dx_nmembers);
2N/A
2N/A if (dxp->dx_membdif == NULL) {
2N/A dxp->dx_nmembers = 0;
2N/A goto err;
2N/A }
2N/A
2N/A return (dxp);
2N/A
2N/Aerr:
2N/A dt_xlator_destroy(dtp, dxp);
2N/A return (NULL);
2N/A}
2N/A
2N/Avoid
2N/Adt_xlator_destroy(dtrace_hdl_t *dtp, dt_xlator_t *dxp)
2N/A{
2N/A uint_t i;
2N/A
2N/A dt_node_link_free(&dxp->dx_nodes);
2N/A
2N/A if (dxp->dx_locals != NULL)
2N/A dt_idhash_destroy(dxp->dx_locals);
2N/A else if (dxp->dx_ident != NULL)
2N/A dt_ident_destroy(dxp->dx_ident);
2N/A
2N/A for (i = 0; i < dxp->dx_nmembers; i++)
2N/A dt_difo_free(dtp, dxp->dx_membdif[i]);
2N/A
2N/A dt_free(dtp, dxp->dx_membdif);
2N/A dt_list_delete(&dtp->dt_xlators, dxp);
2N/A dt_free(dtp, dxp);
2N/A}
2N/A
2N/Adt_xlator_t *
2N/Adt_xlator_lookup(dtrace_hdl_t *dtp, dt_node_t *src, dt_node_t *dst, int flags)
2N/A{
2N/A ctf_file_t *src_ctfp = src->dn_ctfp;
2N/A ctf_id_t src_type = src->dn_type;
2N/A ctf_id_t src_base = ctf_type_resolve(src_ctfp, src_type);
2N/A
2N/A ctf_file_t *dst_ctfp = dst->dn_ctfp;
2N/A ctf_id_t dst_type = dst->dn_type;
2N/A ctf_id_t dst_base = ctf_type_resolve(dst_ctfp, dst_type);
2N/A uint_t dst_kind = ctf_type_kind(dst_ctfp, dst_base);
2N/A
2N/A int ptr = dst_kind == CTF_K_POINTER;
2N/A dtrace_typeinfo_t src_dtt, dst_dtt;
2N/A dt_node_t xn = { 0 };
2N/A dt_xlator_t *dxp = NULL;
2N/A
2N/A if (src_base == CTF_ERR || dst_base == CTF_ERR)
2N/A return (NULL); /* fail if these are unresolvable types */
2N/A
2N/A /*
2N/A * Translators are always defined using a struct or union type, so if
2N/A * we are attempting to translate to type "T *", we internally look
2N/A * for a translation to type "T" by following the pointer reference.
2N/A */
2N/A if (ptr) {
2N/A dst_type = ctf_type_reference(dst_ctfp, dst_type);
2N/A dst_base = ctf_type_resolve(dst_ctfp, dst_type);
2N/A dst_kind = ctf_type_kind(dst_ctfp, dst_base);
2N/A }
2N/A
2N/A if (dst_kind != CTF_K_UNION && dst_kind != CTF_K_STRUCT)
2N/A return (NULL); /* fail if the output isn't a struct or union */
2N/A
2N/A /*
2N/A * In order to find a matching translator, we iterate over the set of
2N/A * available translators in three passes. First, we look for a
2N/A * translation from the exact source type to the resolved destination.
2N/A * Second, we look for a translation from the resolved source type to
2N/A * the resolved destination. Third, we look for a translation from a
2N/A * compatible source type (using the same rules as parameter formals)
2N/A * to the resolved destination. If all passes fail, return NULL.
2N/A */
2N/A for (dxp = dt_list_next(&dtp->dt_xlators); dxp != NULL;
2N/A dxp = dt_list_next(dxp)) {
2N/A if (ctf_type_compat(dxp->dx_src_ctfp, dxp->dx_src_type,
2N/A src_ctfp, src_type) &&
2N/A ctf_type_compat(dxp->dx_dst_ctfp, dxp->dx_dst_base,
2N/A dst_ctfp, dst_base))
2N/A goto out;
2N/A }
2N/A
2N/A if (flags & DT_XLATE_EXACT)
2N/A goto out; /* skip remaining passes if exact match required */
2N/A
2N/A for (dxp = dt_list_next(&dtp->dt_xlators); dxp != NULL;
2N/A dxp = dt_list_next(dxp)) {
2N/A if (ctf_type_compat(dxp->dx_src_ctfp, dxp->dx_src_base,
2N/A src_ctfp, src_type) &&
2N/A ctf_type_compat(dxp->dx_dst_ctfp, dxp->dx_dst_base,
2N/A dst_ctfp, dst_base))
2N/A goto out;
2N/A }
2N/A
2N/A for (dxp = dt_list_next(&dtp->dt_xlators); dxp != NULL;
2N/A dxp = dt_list_next(dxp)) {
2N/A dt_node_type_assign(&xn, dxp->dx_src_ctfp, dxp->dx_src_type);
2N/A if (ctf_type_compat(dxp->dx_dst_ctfp, dxp->dx_dst_base,
2N/A dst_ctfp, dst_base) && dt_node_is_argcompat(src, &xn))
2N/A goto out;
2N/A }
2N/A
2N/Aout:
2N/A if (ptr && dxp != NULL && dxp->dx_ptrid.di_type == CTF_ERR)
2N/A return (NULL); /* no translation available to pointer type */
2N/A
2N/A if (dxp != NULL || !(flags & DT_XLATE_EXTERN) ||
2N/A dtp->dt_xlatemode == DT_XL_STATIC)
2N/A return (dxp); /* we succeeded or not allowed to extern */
2N/A
2N/A /*
2N/A * If we get here, then we didn't find an existing translator, but the
2N/A * caller and xlatemode permit us to create an extern to a dynamic one.
2N/A */
2N/A src_dtt.dtt_object = dt_module_lookup_by_ctf(dtp, src_ctfp)->dm_name;
2N/A src_dtt.dtt_ctfp = src_ctfp;
2N/A src_dtt.dtt_type = src_type;
2N/A
2N/A dst_dtt.dtt_object = dt_module_lookup_by_ctf(dtp, dst_ctfp)->dm_name;
2N/A dst_dtt.dtt_ctfp = dst_ctfp;
2N/A dst_dtt.dtt_type = dst_type;
2N/A
2N/A return (dt_xlator_create(dtp, &src_dtt, &dst_dtt, NULL, NULL, NULL));
2N/A}
2N/A
2N/Adt_xlator_t *
2N/Adt_xlator_lookup_id(dtrace_hdl_t *dtp, id_t id)
2N/A{
2N/A assert(id >= 0 && id < dtp->dt_xlatorid);
2N/A return (dtp->dt_xlatormap[id]);
2N/A}
2N/A
2N/Adt_ident_t *
2N/Adt_xlator_ident(dt_xlator_t *dxp, ctf_file_t *ctfp, ctf_id_t type)
2N/A{
2N/A if (ctf_type_kind(ctfp, ctf_type_resolve(ctfp, type)) == CTF_K_POINTER)
2N/A return (&dxp->dx_ptrid);
2N/A else
2N/A return (&dxp->dx_souid);
2N/A}
2N/A
2N/Adt_node_t *
2N/Adt_xlator_member(dt_xlator_t *dxp, const char *name)
2N/A{
2N/A dt_node_t *dnp;
2N/A
2N/A for (dnp = dxp->dx_members; dnp != NULL; dnp = dnp->dn_list) {
2N/A if (strcmp(dnp->dn_membname, name) == 0)
2N/A return (dnp);
2N/A }
2N/A
2N/A return (NULL);
2N/A}
2N/A
2N/Aint
2N/Adt_xlator_dynamic(const dt_xlator_t *dxp)
2N/A{
2N/A return (dxp->dx_locals == NULL);
2N/A}