1178479Sjb/*
2178479Sjb * CDDL HEADER START
3178479Sjb *
4178479Sjb * The contents of this file are subject to the terms of the
5178479Sjb * Common Development and Distribution License, Version 1.0 only
6178479Sjb * (the "License").  You may not use this file except in compliance
7178479Sjb * with the License.
8178479Sjb *
9178479Sjb * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10178479Sjb * or http://www.opensolaris.org/os/licensing.
11178479Sjb * See the License for the specific language governing permissions
12178479Sjb * and limitations under the License.
13178479Sjb *
14178479Sjb * When distributing Covered Code, include this CDDL HEADER in each
15178479Sjb * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16178479Sjb * If applicable, add the following below this CDDL HEADER, with the
17178479Sjb * fields enclosed by brackets "[]" replaced with your own identifying
18178479Sjb * information: Portions Copyright [yyyy] [name of copyright owner]
19178479Sjb *
20178479Sjb * CDDL HEADER END
21178479Sjb */
22178479Sjb/*
23178479Sjb * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24178479Sjb * Use is subject to license terms.
25178479Sjb */
26178479Sjb
27178479Sjb#pragma ident	"%Z%%M%	%I%	%E% SMI"
28178479Sjb
29178479Sjb#include <strings.h>
30178479Sjb#include <assert.h>
31178479Sjb
32178479Sjb#include <dt_xlator.h>
33178479Sjb#include <dt_parser.h>
34178479Sjb#include <dt_grammar.h>
35178479Sjb#include <dt_module.h>
36178479Sjb#include <dt_impl.h>
37178479Sjb
38178479Sjb/*
39178479Sjb * Create a member node corresponding to one of the output members of a dynamic
40178479Sjb * translator.  We set the member's dn_membexpr to a DT_NODE_XLATOR node that
41178479Sjb * has dn_op set to DT_TOK_XLATE and refers back to the translator itself.  The
42178479Sjb * code generator will then use this as the indicator for dynamic translation.
43178479Sjb */
44178479Sjb/*ARGSUSED*/
45178479Sjbstatic int
46178479Sjbdt_xlator_create_member(const char *name, ctf_id_t type, ulong_t off, void *arg)
47178479Sjb{
48178479Sjb	dt_xlator_t *dxp = arg;
49178479Sjb	dtrace_hdl_t *dtp = dxp->dx_hdl;
50178479Sjb	dt_node_t *enp, *mnp;
51178479Sjb
52178479Sjb	if ((enp = dt_node_xalloc(dtp, DT_NODE_XLATOR)) == NULL)
53178479Sjb		return (dt_set_errno(dtp, EDT_NOMEM));
54178479Sjb
55178479Sjb	enp->dn_link = dxp->dx_nodes;
56178479Sjb	dxp->dx_nodes = enp;
57178479Sjb
58178479Sjb	if ((mnp = dt_node_xalloc(dtp, DT_NODE_MEMBER)) == NULL)
59178479Sjb		return (dt_set_errno(dtp, EDT_NOMEM));
60178479Sjb
61178479Sjb	mnp->dn_link = dxp->dx_nodes;
62178479Sjb	dxp->dx_nodes = mnp;
63178479Sjb
64178479Sjb	/*
65178479Sjb	 * For the member expression, we use a DT_NODE_XLATOR/TOK_XLATE whose
66178479Sjb	 * xlator refers back to the translator and whose dn_xmember refers to
67178479Sjb	 * the current member.  These refs will be used by dt_cg.c and dt_as.c.
68178479Sjb	 */
69178479Sjb	enp->dn_op = DT_TOK_XLATE;
70178479Sjb	enp->dn_xlator = dxp;
71178479Sjb	enp->dn_xmember = mnp;
72178479Sjb	dt_node_type_assign(enp, dxp->dx_dst_ctfp, type);
73178479Sjb
74178479Sjb	/*
75178479Sjb	 * For the member itself, we use a DT_NODE_MEMBER as usual with the
76178479Sjb	 * appropriate name, output type, and member expression set to 'enp'.
77178479Sjb	 */
78178479Sjb	if (dxp->dx_members != NULL) {
79178479Sjb		assert(enp->dn_link->dn_kind == DT_NODE_MEMBER);
80178479Sjb		enp->dn_link->dn_list = mnp;
81178479Sjb	} else
82178479Sjb		dxp->dx_members = mnp;
83178479Sjb
84178479Sjb	mnp->dn_membname = strdup(name);
85178479Sjb	mnp->dn_membexpr = enp;
86178479Sjb	dt_node_type_assign(mnp, dxp->dx_dst_ctfp, type);
87178479Sjb
88178479Sjb	if (mnp->dn_membname == NULL)
89178479Sjb		return (dt_set_errno(dtp, EDT_NOMEM));
90178479Sjb
91178479Sjb	return (0);
92178479Sjb}
93178479Sjb
94178479Sjbdt_xlator_t *
95178479Sjbdt_xlator_create(dtrace_hdl_t *dtp,
96178479Sjb    const dtrace_typeinfo_t *src, const dtrace_typeinfo_t *dst,
97178479Sjb    const char *name, dt_node_t *members, dt_node_t *nodes)
98178479Sjb{
99178479Sjb	dt_xlator_t *dxp = dt_zalloc(dtp, sizeof (dt_xlator_t));
100178479Sjb	dtrace_typeinfo_t ptr = *dst;
101178479Sjb	dt_xlator_t **map;
102178479Sjb	dt_node_t *dnp;
103178479Sjb	uint_t kind;
104178479Sjb
105178479Sjb	if (dxp == NULL)
106178479Sjb		return (NULL);
107178479Sjb
108178479Sjb	dxp->dx_hdl = dtp;
109178479Sjb	dxp->dx_id = dtp->dt_xlatorid++;
110178479Sjb	dxp->dx_gen = dtp->dt_gen;
111178479Sjb	dxp->dx_arg = -1;
112178479Sjb
113178479Sjb	if ((map = dt_alloc(dtp, sizeof (void *) * (dxp->dx_id + 1))) == NULL) {
114178479Sjb		dt_free(dtp, dxp);
115178479Sjb		return (NULL);
116178479Sjb	}
117178479Sjb
118178479Sjb	dt_list_append(&dtp->dt_xlators, dxp);
119178479Sjb	bcopy(dtp->dt_xlatormap, map, sizeof (void *) * dxp->dx_id);
120178479Sjb	dt_free(dtp, dtp->dt_xlatormap);
121178479Sjb	dtp->dt_xlatormap = map;
122178479Sjb	dtp->dt_xlatormap[dxp->dx_id] = dxp;
123178479Sjb
124178479Sjb	if (dt_type_pointer(&ptr) == -1) {
125178479Sjb		ptr.dtt_ctfp = NULL;
126178479Sjb		ptr.dtt_type = CTF_ERR;
127178479Sjb	}
128178479Sjb
129178479Sjb	dxp->dx_ident = dt_ident_create(name ? name : "T",
130178479Sjb	    DT_IDENT_SCALAR, DT_IDFLG_REF | DT_IDFLG_ORPHAN, 0,
131178479Sjb	    _dtrace_defattr, 0, &dt_idops_thaw, NULL, dtp->dt_gen);
132178479Sjb
133178479Sjb	if (dxp->dx_ident == NULL)
134178479Sjb		goto err; /* no memory for identifier */
135178479Sjb
136178479Sjb	dxp->dx_ident->di_ctfp = src->dtt_ctfp;
137178479Sjb	dxp->dx_ident->di_type = src->dtt_type;
138178479Sjb
139178479Sjb	/*
140178479Sjb	 * If an input parameter name is given, this is a static translator
141178479Sjb	 * definition: create an idhash and identifier for the parameter.
142178479Sjb	 */
143178479Sjb	if (name != NULL) {
144178479Sjb		dxp->dx_locals = dt_idhash_create("xlparams", NULL, 0, 0);
145178479Sjb
146178479Sjb		if (dxp->dx_locals == NULL)
147178479Sjb			goto err; /* no memory for identifier hash */
148178479Sjb
149178479Sjb		dt_idhash_xinsert(dxp->dx_locals, dxp->dx_ident);
150178479Sjb	}
151178479Sjb
152178479Sjb	dxp->dx_souid.di_name = "translator";
153178479Sjb	dxp->dx_souid.di_kind = DT_IDENT_XLSOU;
154178479Sjb	dxp->dx_souid.di_flags = DT_IDFLG_REF;
155178479Sjb	dxp->dx_souid.di_id = dxp->dx_id;
156178479Sjb	dxp->dx_souid.di_attr = _dtrace_defattr;
157178479Sjb	dxp->dx_souid.di_ops = &dt_idops_thaw;
158178479Sjb	dxp->dx_souid.di_data = dxp;
159178479Sjb	dxp->dx_souid.di_ctfp = dst->dtt_ctfp;
160178479Sjb	dxp->dx_souid.di_type = dst->dtt_type;
161178479Sjb	dxp->dx_souid.di_gen = dtp->dt_gen;
162178479Sjb
163178479Sjb	dxp->dx_ptrid.di_name = "translator";
164178479Sjb	dxp->dx_ptrid.di_kind = DT_IDENT_XLPTR;
165178479Sjb	dxp->dx_ptrid.di_flags = DT_IDFLG_REF;
166178479Sjb	dxp->dx_ptrid.di_id = dxp->dx_id;
167178479Sjb	dxp->dx_ptrid.di_attr = _dtrace_defattr;
168178479Sjb	dxp->dx_ptrid.di_ops = &dt_idops_thaw;
169178479Sjb	dxp->dx_ptrid.di_data = dxp;
170178479Sjb	dxp->dx_ptrid.di_ctfp = ptr.dtt_ctfp;
171178479Sjb	dxp->dx_ptrid.di_type = ptr.dtt_type;
172178479Sjb	dxp->dx_ptrid.di_gen = dtp->dt_gen;
173178479Sjb
174178479Sjb	/*
175178479Sjb	 * If a deferred pragma is pending on the keyword "translator", run all
176178479Sjb	 * the deferred pragmas on dx_souid and then copy results to dx_ptrid.
177178479Sjb	 * See the code in dt_pragma.c for details on deferred ident pragmas.
178178479Sjb	 */
179178479Sjb	if (dtp->dt_globals->dh_defer != NULL && yypcb->pcb_pragmas != NULL &&
180178479Sjb	    dt_idhash_lookup(yypcb->pcb_pragmas, "translator") != NULL) {
181178479Sjb		dtp->dt_globals->dh_defer(dtp->dt_globals, &dxp->dx_souid);
182178479Sjb		dxp->dx_ptrid.di_attr = dxp->dx_souid.di_attr;
183178479Sjb		dxp->dx_ptrid.di_vers = dxp->dx_souid.di_vers;
184178479Sjb	}
185178479Sjb
186178479Sjb	dxp->dx_src_ctfp = src->dtt_ctfp;
187178479Sjb	dxp->dx_src_type = src->dtt_type;
188178479Sjb	dxp->dx_src_base = ctf_type_resolve(src->dtt_ctfp, src->dtt_type);
189178479Sjb
190178479Sjb	dxp->dx_dst_ctfp = dst->dtt_ctfp;
191178479Sjb	dxp->dx_dst_type = dst->dtt_type;
192178479Sjb	dxp->dx_dst_base = ctf_type_resolve(dst->dtt_ctfp, dst->dtt_type);
193178479Sjb
194178479Sjb	kind = ctf_type_kind(dst->dtt_ctfp, dxp->dx_dst_base);
195178479Sjb	assert(kind == CTF_K_STRUCT || kind == CTF_K_UNION);
196178479Sjb
197178479Sjb	/*
198178479Sjb	 * If no input parameter is given, we're making a dynamic translator:
199178479Sjb	 * create member nodes for every member of the output type.  Otherwise
200178479Sjb	 * retain the member and allocation node lists presented by the parser.
201178479Sjb	 */
202178479Sjb	if (name == NULL) {
203178479Sjb		if (ctf_member_iter(dxp->dx_dst_ctfp, dxp->dx_dst_base,
204178479Sjb		    dt_xlator_create_member, dxp) != 0)
205178479Sjb			goto err;
206178479Sjb	} else {
207178479Sjb		dxp->dx_members = members;
208178479Sjb		dxp->dx_nodes = nodes;
209178479Sjb	}
210178479Sjb
211178479Sjb	/*
212178479Sjb	 * Assign member IDs to each member and allocate space for DIFOs
213178479Sjb	 * if and when this translator is eventually compiled.
214178479Sjb	 */
215178479Sjb	for (dnp = dxp->dx_members; dnp != NULL; dnp = dnp->dn_list) {
216178479Sjb		dnp->dn_membxlator = dxp;
217178479Sjb		dnp->dn_membid = dxp->dx_nmembers++;
218178479Sjb	}
219178479Sjb
220178479Sjb	dxp->dx_membdif = dt_zalloc(dtp,
221178479Sjb	    sizeof (dtrace_difo_t *) * dxp->dx_nmembers);
222178479Sjb
223178479Sjb	if (dxp->dx_membdif == NULL) {
224178479Sjb		dxp->dx_nmembers = 0;
225178479Sjb		goto err;
226178479Sjb	}
227178479Sjb
228178479Sjb	return (dxp);
229178479Sjb
230178479Sjberr:
231178479Sjb	dt_xlator_destroy(dtp, dxp);
232178479Sjb	return (NULL);
233178479Sjb}
234178479Sjb
235178479Sjbvoid
236178479Sjbdt_xlator_destroy(dtrace_hdl_t *dtp, dt_xlator_t *dxp)
237178479Sjb{
238178479Sjb	uint_t i;
239178479Sjb
240178479Sjb	dt_node_link_free(&dxp->dx_nodes);
241178479Sjb
242178479Sjb	if (dxp->dx_locals != NULL)
243178479Sjb		dt_idhash_destroy(dxp->dx_locals);
244178479Sjb	else if (dxp->dx_ident != NULL)
245178479Sjb		dt_ident_destroy(dxp->dx_ident);
246178479Sjb
247178479Sjb	for (i = 0; i < dxp->dx_nmembers; i++)
248178479Sjb		dt_difo_free(dtp, dxp->dx_membdif[i]);
249178479Sjb
250178479Sjb	dt_free(dtp, dxp->dx_membdif);
251178479Sjb	dt_list_delete(&dtp->dt_xlators, dxp);
252178479Sjb	dt_free(dtp, dxp);
253178479Sjb}
254178479Sjb
255178479Sjbdt_xlator_t *
256178479Sjbdt_xlator_lookup(dtrace_hdl_t *dtp, dt_node_t *src, dt_node_t *dst, int flags)
257178479Sjb{
258178479Sjb	ctf_file_t *src_ctfp = src->dn_ctfp;
259178479Sjb	ctf_id_t src_type = src->dn_type;
260178479Sjb	ctf_id_t src_base = ctf_type_resolve(src_ctfp, src_type);
261178479Sjb
262178479Sjb	ctf_file_t *dst_ctfp = dst->dn_ctfp;
263178479Sjb	ctf_id_t dst_type = dst->dn_type;
264178479Sjb	ctf_id_t dst_base = ctf_type_resolve(dst_ctfp, dst_type);
265178479Sjb	uint_t dst_kind = ctf_type_kind(dst_ctfp, dst_base);
266178479Sjb
267178479Sjb	int ptr = dst_kind == CTF_K_POINTER;
268178479Sjb	dtrace_typeinfo_t src_dtt, dst_dtt;
269178479Sjb	dt_node_t xn = { 0 };
270178479Sjb	dt_xlator_t *dxp = NULL;
271178479Sjb
272178479Sjb	if (src_base == CTF_ERR || dst_base == CTF_ERR)
273178479Sjb		return (NULL); /* fail if these are unresolvable types */
274178479Sjb
275178479Sjb	/*
276178479Sjb	 * Translators are always defined using a struct or union type, so if
277178479Sjb	 * we are attempting to translate to type "T *", we internally look
278178479Sjb	 * for a translation to type "T" by following the pointer reference.
279178479Sjb	 */
280178479Sjb	if (ptr) {
281178479Sjb		dst_type = ctf_type_reference(dst_ctfp, dst_type);
282178479Sjb		dst_base = ctf_type_resolve(dst_ctfp, dst_type);
283178479Sjb		dst_kind = ctf_type_kind(dst_ctfp, dst_base);
284178479Sjb	}
285178479Sjb
286178479Sjb	if (dst_kind != CTF_K_UNION && dst_kind != CTF_K_STRUCT)
287178479Sjb		return (NULL); /* fail if the output isn't a struct or union */
288178479Sjb
289178479Sjb	/*
290178479Sjb	 * In order to find a matching translator, we iterate over the set of
291178479Sjb	 * available translators in three passes.  First, we look for a
292178479Sjb	 * translation from the exact source type to the resolved destination.
293178479Sjb	 * Second, we look for a translation from the resolved source type to
294178479Sjb	 * the resolved destination.  Third, we look for a translation from a
295178479Sjb	 * compatible source type (using the same rules as parameter formals)
296178479Sjb	 * to the resolved destination.  If all passes fail, return NULL.
297178479Sjb	 */
298178479Sjb	for (dxp = dt_list_next(&dtp->dt_xlators); dxp != NULL;
299178479Sjb	    dxp = dt_list_next(dxp)) {
300178479Sjb		if (ctf_type_compat(dxp->dx_src_ctfp, dxp->dx_src_type,
301178479Sjb		    src_ctfp, src_type) &&
302178479Sjb		    ctf_type_compat(dxp->dx_dst_ctfp, dxp->dx_dst_base,
303178479Sjb		    dst_ctfp, dst_base))
304178479Sjb			goto out;
305178479Sjb	}
306178479Sjb
307178479Sjb	if (flags & DT_XLATE_EXACT)
308178479Sjb		goto out; /* skip remaining passes if exact match required */
309178479Sjb
310178479Sjb	for (dxp = dt_list_next(&dtp->dt_xlators); dxp != NULL;
311178479Sjb	    dxp = dt_list_next(dxp)) {
312178479Sjb		if (ctf_type_compat(dxp->dx_src_ctfp, dxp->dx_src_base,
313178479Sjb		    src_ctfp, src_type) &&
314178479Sjb		    ctf_type_compat(dxp->dx_dst_ctfp, dxp->dx_dst_base,
315178479Sjb		    dst_ctfp, dst_base))
316178479Sjb			goto out;
317178479Sjb	}
318178479Sjb
319178479Sjb	for (dxp = dt_list_next(&dtp->dt_xlators); dxp != NULL;
320178479Sjb	    dxp = dt_list_next(dxp)) {
321178479Sjb		dt_node_type_assign(&xn, dxp->dx_src_ctfp, dxp->dx_src_type);
322178479Sjb		if (ctf_type_compat(dxp->dx_dst_ctfp, dxp->dx_dst_base,
323178479Sjb		    dst_ctfp, dst_base) && dt_node_is_argcompat(src, &xn))
324178479Sjb			goto out;
325178479Sjb	}
326178479Sjb
327178479Sjbout:
328178479Sjb	if (ptr && dxp != NULL && dxp->dx_ptrid.di_type == CTF_ERR)
329178479Sjb		return (NULL);	/* no translation available to pointer type */
330178479Sjb
331178479Sjb	if (dxp != NULL || !(flags & DT_XLATE_EXTERN) ||
332178479Sjb	    dtp->dt_xlatemode == DT_XL_STATIC)
333178479Sjb		return (dxp);	/* we succeeded or not allowed to extern */
334178479Sjb
335178479Sjb	/*
336178479Sjb	 * If we get here, then we didn't find an existing translator, but the
337178479Sjb	 * caller and xlatemode permit us to create an extern to a dynamic one.
338178479Sjb	 */
339178479Sjb	src_dtt.dtt_object = dt_module_lookup_by_ctf(dtp, src_ctfp)->dm_name;
340178479Sjb	src_dtt.dtt_ctfp = src_ctfp;
341178479Sjb	src_dtt.dtt_type = src_type;
342178479Sjb
343178479Sjb	dst_dtt.dtt_object = dt_module_lookup_by_ctf(dtp, dst_ctfp)->dm_name;
344178479Sjb	dst_dtt.dtt_ctfp = dst_ctfp;
345178479Sjb	dst_dtt.dtt_type = dst_type;
346178479Sjb
347178479Sjb	return (dt_xlator_create(dtp, &src_dtt, &dst_dtt, NULL, NULL, NULL));
348178479Sjb}
349178479Sjb
350178479Sjbdt_xlator_t *
351178479Sjbdt_xlator_lookup_id(dtrace_hdl_t *dtp, id_t id)
352178479Sjb{
353178479Sjb	assert(id >= 0 && id < dtp->dt_xlatorid);
354178479Sjb	return (dtp->dt_xlatormap[id]);
355178479Sjb}
356178479Sjb
357178479Sjbdt_ident_t *
358178479Sjbdt_xlator_ident(dt_xlator_t *dxp, ctf_file_t *ctfp, ctf_id_t type)
359178479Sjb{
360178479Sjb	if (ctf_type_kind(ctfp, ctf_type_resolve(ctfp, type)) == CTF_K_POINTER)
361178479Sjb		return (&dxp->dx_ptrid);
362178479Sjb	else
363178479Sjb		return (&dxp->dx_souid);
364178479Sjb}
365178479Sjb
366178479Sjbdt_node_t *
367178479Sjbdt_xlator_member(dt_xlator_t *dxp, const char *name)
368178479Sjb{
369178479Sjb	dt_node_t *dnp;
370178479Sjb
371178479Sjb	for (dnp = dxp->dx_members; dnp != NULL; dnp = dnp->dn_list) {
372178479Sjb		if (strcmp(dnp->dn_membname, name) == 0)
373178479Sjb			return (dnp);
374178479Sjb	}
375178479Sjb
376178479Sjb	return (NULL);
377178479Sjb}
378178479Sjb
379178479Sjbint
380178479Sjbdt_xlator_dynamic(const dt_xlator_t *dxp)
381178479Sjb{
382178479Sjb	return (dxp->dx_locals == NULL);
383178479Sjb}
384