dt_cg.c revision 178479
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 <sys/types.h>
30178479Sjb#include <sys/sysmacros.h>
31178479Sjb#include <sys/isa_defs.h>
32178479Sjb
33178479Sjb#include <strings.h>
34178479Sjb#include <stdlib.h>
35178479Sjb#include <setjmp.h>
36178479Sjb#include <assert.h>
37178479Sjb#include <errno.h>
38178479Sjb
39178479Sjb#include <dt_impl.h>
40178479Sjb#include <dt_grammar.h>
41178479Sjb#include <dt_parser.h>
42178479Sjb#include <dt_provider.h>
43178479Sjb
44178479Sjbstatic void dt_cg_node(dt_node_t *, dt_irlist_t *, dt_regset_t *);
45178479Sjb
46178479Sjbstatic dt_irnode_t *
47178479Sjbdt_cg_node_alloc(uint_t label, dif_instr_t instr)
48178479Sjb{
49178479Sjb	dt_irnode_t *dip = malloc(sizeof (dt_irnode_t));
50178479Sjb
51178479Sjb	if (dip == NULL)
52178479Sjb		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
53178479Sjb
54178479Sjb	dip->di_label = label;
55178479Sjb	dip->di_instr = instr;
56178479Sjb	dip->di_extern = NULL;
57178479Sjb	dip->di_next = NULL;
58178479Sjb
59178479Sjb	return (dip);
60178479Sjb}
61178479Sjb
62178479Sjb/*
63178479Sjb * Code generator wrapper function for ctf_member_info.  If we are given a
64178479Sjb * reference to a forward declaration tag, search the entire type space for
65178479Sjb * the actual definition and then call ctf_member_info on the result.
66178479Sjb */
67178479Sjbstatic ctf_file_t *
68178479Sjbdt_cg_membinfo(ctf_file_t *fp, ctf_id_t type, const char *s, ctf_membinfo_t *mp)
69178479Sjb{
70178479Sjb	while (ctf_type_kind(fp, type) == CTF_K_FORWARD) {
71178479Sjb		char n[DT_TYPE_NAMELEN];
72178479Sjb		dtrace_typeinfo_t dtt;
73178479Sjb
74178479Sjb		if (ctf_type_name(fp, type, n, sizeof (n)) == NULL ||
75178479Sjb		    dt_type_lookup(n, &dtt) == -1 || (
76178479Sjb		    dtt.dtt_ctfp == fp && dtt.dtt_type == type))
77178479Sjb			break; /* unable to improve our position */
78178479Sjb
79178479Sjb		fp = dtt.dtt_ctfp;
80178479Sjb		type = ctf_type_resolve(fp, dtt.dtt_type);
81178479Sjb	}
82178479Sjb
83178479Sjb	if (ctf_member_info(fp, type, s, mp) == CTF_ERR)
84178479Sjb		return (NULL); /* ctf_errno is set for us */
85178479Sjb
86178479Sjb	return (fp);
87178479Sjb}
88178479Sjb
89178479Sjbstatic void
90178479Sjbdt_cg_xsetx(dt_irlist_t *dlp, dt_ident_t *idp, uint_t lbl, int reg, uint64_t x)
91178479Sjb{
92178479Sjb	int flag = idp != NULL ? DT_INT_PRIVATE : DT_INT_SHARED;
93178479Sjb	int intoff = dt_inttab_insert(yypcb->pcb_inttab, x, flag);
94178479Sjb	dif_instr_t instr = DIF_INSTR_SETX((uint_t)intoff, reg);
95178479Sjb
96178479Sjb	if (intoff == -1)
97178479Sjb		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
98178479Sjb
99178479Sjb	if (intoff > DIF_INTOFF_MAX)
100178479Sjb		longjmp(yypcb->pcb_jmpbuf, EDT_INT2BIG);
101178479Sjb
102178479Sjb	dt_irlist_append(dlp, dt_cg_node_alloc(lbl, instr));
103178479Sjb
104178479Sjb	if (idp != NULL)
105178479Sjb		dlp->dl_last->di_extern = idp;
106178479Sjb}
107178479Sjb
108178479Sjbstatic void
109178479Sjbdt_cg_setx(dt_irlist_t *dlp, int reg, uint64_t x)
110178479Sjb{
111178479Sjb	dt_cg_xsetx(dlp, NULL, DT_LBL_NONE, reg, x);
112178479Sjb}
113178479Sjb
114178479Sjb/*
115178479Sjb * When loading bit-fields, we want to convert a byte count in the range
116178479Sjb * 1-8 to the closest power of 2 (e.g. 3->4, 5->8, etc).  The clp2() function
117178479Sjb * is a clever implementation from "Hacker's Delight" by Henry Warren, Jr.
118178479Sjb */
119178479Sjbstatic size_t
120178479Sjbclp2(size_t x)
121178479Sjb{
122178479Sjb	x--;
123178479Sjb
124178479Sjb	x |= (x >> 1);
125178479Sjb	x |= (x >> 2);
126178479Sjb	x |= (x >> 4);
127178479Sjb	x |= (x >> 8);
128178479Sjb	x |= (x >> 16);
129178479Sjb
130178479Sjb	return (x + 1);
131178479Sjb}
132178479Sjb
133178479Sjb/*
134178479Sjb * Lookup the correct load opcode to use for the specified node and CTF type.
135178479Sjb * We determine the size and convert it to a 3-bit index.  Our lookup table
136178479Sjb * is constructed to use a 5-bit index, consisting of the 3-bit size 0-7, a
137178479Sjb * bit for the sign, and a bit for userland address.  For example, a 4-byte
138178479Sjb * signed load from userland would be at the following table index:
139178479Sjb * user=1 sign=1 size=4 => binary index 11011 = decimal index 27
140178479Sjb */
141178479Sjbstatic uint_t
142178479Sjbdt_cg_load(dt_node_t *dnp, ctf_file_t *ctfp, ctf_id_t type)
143178479Sjb{
144178479Sjb	static const uint_t ops[] = {
145178479Sjb		DIF_OP_LDUB,	DIF_OP_LDUH,	0,	DIF_OP_LDUW,
146178479Sjb		0,		0,		0,	DIF_OP_LDX,
147178479Sjb		DIF_OP_LDSB,	DIF_OP_LDSH,	0,	DIF_OP_LDSW,
148178479Sjb		0,		0,		0,	DIF_OP_LDX,
149178479Sjb		DIF_OP_ULDUB,	DIF_OP_ULDUH,	0,	DIF_OP_ULDUW,
150178479Sjb		0,		0,		0,	DIF_OP_ULDX,
151178479Sjb		DIF_OP_ULDSB,	DIF_OP_ULDSH,	0,	DIF_OP_ULDSW,
152178479Sjb		0,		0,		0,	DIF_OP_ULDX,
153178479Sjb	};
154178479Sjb
155178479Sjb	ctf_encoding_t e;
156178479Sjb	ssize_t size;
157178479Sjb
158178479Sjb	/*
159178479Sjb	 * If we're loading a bit-field, the size of our load is found by
160178479Sjb	 * rounding cte_bits up to a byte boundary and then finding the
161178479Sjb	 * nearest power of two to this value (see clp2(), above).
162178479Sjb	 */
163178479Sjb	if ((dnp->dn_flags & DT_NF_BITFIELD) &&
164178479Sjb	    ctf_type_encoding(ctfp, type, &e) != CTF_ERR)
165178479Sjb		size = clp2(P2ROUNDUP(e.cte_bits, NBBY) / NBBY);
166178479Sjb	else
167178479Sjb		size = ctf_type_size(ctfp, type);
168178479Sjb
169178479Sjb	if (size < 1 || size > 8 || (size & (size - 1)) != 0) {
170178479Sjb		xyerror(D_UNKNOWN, "internal error -- cg cannot load "
171178479Sjb		    "size %ld when passed by value\n", (long)size);
172178479Sjb	}
173178479Sjb
174178479Sjb	size--; /* convert size to 3-bit index */
175178479Sjb
176178479Sjb	if (dnp->dn_flags & DT_NF_SIGNED)
177178479Sjb		size |= 0x08;
178178479Sjb	if (dnp->dn_flags & DT_NF_USERLAND)
179178479Sjb		size |= 0x10;
180178479Sjb
181178479Sjb	return (ops[size]);
182178479Sjb}
183178479Sjb
184178479Sjbstatic void
185178479Sjbdt_cg_ptrsize(dt_node_t *dnp, dt_irlist_t *dlp, dt_regset_t *drp,
186178479Sjb    uint_t op, int dreg)
187178479Sjb{
188178479Sjb	ctf_file_t *ctfp = dnp->dn_ctfp;
189178479Sjb	ctf_arinfo_t r;
190178479Sjb	dif_instr_t instr;
191178479Sjb	ctf_id_t type;
192178479Sjb	uint_t kind;
193178479Sjb	ssize_t size;
194178479Sjb	int sreg;
195178479Sjb
196178479Sjb	if ((sreg = dt_regset_alloc(drp)) == -1)
197178479Sjb		longjmp(yypcb->pcb_jmpbuf, EDT_NOREG);
198178479Sjb
199178479Sjb	type = ctf_type_resolve(ctfp, dnp->dn_type);
200178479Sjb	kind = ctf_type_kind(ctfp, type);
201178479Sjb	assert(kind == CTF_K_POINTER || kind == CTF_K_ARRAY);
202178479Sjb
203178479Sjb	if (kind == CTF_K_ARRAY) {
204178479Sjb		if (ctf_array_info(ctfp, type, &r) != 0) {
205178479Sjb			yypcb->pcb_hdl->dt_ctferr = ctf_errno(ctfp);
206178479Sjb			longjmp(yypcb->pcb_jmpbuf, EDT_CTF);
207178479Sjb		}
208178479Sjb		type = r.ctr_contents;
209178479Sjb	} else
210178479Sjb		type = ctf_type_reference(ctfp, type);
211178479Sjb
212178479Sjb	if ((size = ctf_type_size(ctfp, type)) == 1)
213178479Sjb		return; /* multiply or divide by one can be omitted */
214178479Sjb
215178479Sjb	dt_cg_setx(dlp, sreg, size);
216178479Sjb	instr = DIF_INSTR_FMT(op, dreg, sreg, dreg);
217178479Sjb	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
218178479Sjb	dt_regset_free(drp, sreg);
219178479Sjb}
220178479Sjb
221178479Sjb/*
222178479Sjb * If the result of a "." or "->" operation is a bit-field, we use this routine
223178479Sjb * to generate an epilogue to the load instruction that extracts the value.  In
224178479Sjb * the diagrams below the "ld??" is the load instruction that is generated to
225178479Sjb * load the containing word that is generating prior to calling this function.
226178479Sjb *
227178479Sjb * Epilogue for unsigned fields:	Epilogue for signed fields:
228178479Sjb *
229178479Sjb * ldu?	[r1], r1			lds? [r1], r1
230178479Sjb * setx	USHIFT, r2			setx 64 - SSHIFT, r2
231178479Sjb * srl	r1, r2, r1			sll  r1, r2, r1
232178479Sjb * setx	(1 << bits) - 1, r2		setx 64 - bits, r2
233178479Sjb * and	r1, r2, r1			sra  r1, r2, r1
234178479Sjb *
235178479Sjb * The *SHIFT constants above changes value depending on the endian-ness of our
236178479Sjb * target architecture.  Refer to the comments below for more details.
237178479Sjb */
238178479Sjbstatic void
239178479Sjbdt_cg_field_get(dt_node_t *dnp, dt_irlist_t *dlp, dt_regset_t *drp,
240178479Sjb    ctf_file_t *fp, const ctf_membinfo_t *mp)
241178479Sjb{
242178479Sjb	ctf_encoding_t e;
243178479Sjb	dif_instr_t instr;
244178479Sjb	uint64_t shift;
245178479Sjb	int r1, r2;
246178479Sjb
247178479Sjb	if (ctf_type_encoding(fp, mp->ctm_type, &e) != 0 || e.cte_bits > 64) {
248178479Sjb		xyerror(D_UNKNOWN, "cg: bad field: off %lu type <%ld> "
249178479Sjb		    "bits %u\n", mp->ctm_offset, mp->ctm_type, e.cte_bits);
250178479Sjb	}
251178479Sjb
252178479Sjb	assert(dnp->dn_op == DT_TOK_PTR || dnp->dn_op == DT_TOK_DOT);
253178479Sjb	r1 = dnp->dn_left->dn_reg;
254178479Sjb
255178479Sjb	if ((r2 = dt_regset_alloc(drp)) == -1)
256178479Sjb		longjmp(yypcb->pcb_jmpbuf, EDT_NOREG);
257178479Sjb
258178479Sjb	/*
259178479Sjb	 * On little-endian architectures, ctm_offset counts from the right so
260178479Sjb	 * ctm_offset % NBBY itself is the amount we want to shift right to
261178479Sjb	 * move the value bits to the little end of the register to mask them.
262178479Sjb	 * On big-endian architectures, ctm_offset counts from the left so we
263178479Sjb	 * must subtract (ctm_offset % NBBY + cte_bits) from the size in bits
264178479Sjb	 * we used for the load.  The size of our load in turn is found by
265178479Sjb	 * rounding cte_bits up to a byte boundary and then finding the
266178479Sjb	 * nearest power of two to this value (see clp2(), above).  These
267178479Sjb	 * properties are used to compute shift as USHIFT or SSHIFT, below.
268178479Sjb	 */
269178479Sjb	if (dnp->dn_flags & DT_NF_SIGNED) {
270178479Sjb#if BYTE_ORDER == _BIG_ENDIAN
271178479Sjb		shift = clp2(P2ROUNDUP(e.cte_bits, NBBY) / NBBY) * NBBY -
272178479Sjb		    mp->ctm_offset % NBBY;
273178479Sjb#else
274178479Sjb		shift = mp->ctm_offset % NBBY + e.cte_bits;
275178479Sjb#endif
276178479Sjb		dt_cg_setx(dlp, r2, 64 - shift);
277178479Sjb		instr = DIF_INSTR_FMT(DIF_OP_SLL, r1, r2, r1);
278178479Sjb		dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
279178479Sjb
280178479Sjb		dt_cg_setx(dlp, r2, 64 - e.cte_bits);
281178479Sjb		instr = DIF_INSTR_FMT(DIF_OP_SRA, r1, r2, r1);
282178479Sjb		dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
283178479Sjb	} else {
284178479Sjb#if BYTE_ORDER == _BIG_ENDIAN
285178479Sjb		shift = clp2(P2ROUNDUP(e.cte_bits, NBBY) / NBBY) * NBBY -
286178479Sjb		    (mp->ctm_offset % NBBY + e.cte_bits);
287178479Sjb#else
288178479Sjb		shift = mp->ctm_offset % NBBY;
289178479Sjb#endif
290178479Sjb		dt_cg_setx(dlp, r2, shift);
291178479Sjb		instr = DIF_INSTR_FMT(DIF_OP_SRL, r1, r2, r1);
292178479Sjb		dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
293178479Sjb
294178479Sjb		dt_cg_setx(dlp, r2, (1ULL << e.cte_bits) - 1);
295178479Sjb		instr = DIF_INSTR_FMT(DIF_OP_AND, r1, r2, r1);
296178479Sjb		dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
297178479Sjb	}
298178479Sjb
299178479Sjb	dt_regset_free(drp, r2);
300178479Sjb}
301178479Sjb
302178479Sjb/*
303178479Sjb * If the destination of a store operation is a bit-field, we use this routine
304178479Sjb * to generate a prologue to the store instruction that loads the surrounding
305178479Sjb * bits, clears the destination field, and ORs in the new value of the field.
306178479Sjb * In the diagram below the "st?" is the store instruction that is generated to
307178479Sjb * store the containing word that is generating after calling this function.
308178479Sjb *
309178479Sjb * ld	[dst->dn_reg], r1
310178479Sjb * setx	~(((1 << cte_bits) - 1) << (ctm_offset % NBBY)), r2
311178479Sjb * and	r1, r2, r1
312178479Sjb *
313178479Sjb * setx	(1 << cte_bits) - 1, r2
314178479Sjb * and	src->dn_reg, r2, r2
315178479Sjb * setx ctm_offset % NBBY, r3
316178479Sjb * sll	r2, r3, r2
317178479Sjb *
318178479Sjb * or	r1, r2, r1
319178479Sjb * st?	r1, [dst->dn_reg]
320178479Sjb *
321178479Sjb * This routine allocates a new register to hold the value to be stored and
322178479Sjb * returns it.  The caller is responsible for freeing this register later.
323178479Sjb */
324178479Sjbstatic int
325178479Sjbdt_cg_field_set(dt_node_t *src, dt_irlist_t *dlp,
326178479Sjb    dt_regset_t *drp, dt_node_t *dst)
327178479Sjb{
328178479Sjb	uint64_t cmask, fmask, shift;
329178479Sjb	dif_instr_t instr;
330178479Sjb	int r1, r2, r3;
331178479Sjb
332178479Sjb	ctf_membinfo_t m;
333178479Sjb	ctf_encoding_t e;
334178479Sjb	ctf_file_t *fp, *ofp;
335178479Sjb	ctf_id_t type;
336178479Sjb
337178479Sjb	assert(dst->dn_op == DT_TOK_PTR || dst->dn_op == DT_TOK_DOT);
338178479Sjb	assert(dst->dn_right->dn_kind == DT_NODE_IDENT);
339178479Sjb
340178479Sjb	fp = dst->dn_left->dn_ctfp;
341178479Sjb	type = ctf_type_resolve(fp, dst->dn_left->dn_type);
342178479Sjb
343178479Sjb	if (dst->dn_op == DT_TOK_PTR) {
344178479Sjb		type = ctf_type_reference(fp, type);
345178479Sjb		type = ctf_type_resolve(fp, type);
346178479Sjb	}
347178479Sjb
348178479Sjb	if ((fp = dt_cg_membinfo(ofp = fp, type,
349178479Sjb	    dst->dn_right->dn_string, &m)) == NULL) {
350178479Sjb		yypcb->pcb_hdl->dt_ctferr = ctf_errno(ofp);
351178479Sjb		longjmp(yypcb->pcb_jmpbuf, EDT_CTF);
352178479Sjb	}
353178479Sjb
354178479Sjb	if (ctf_type_encoding(fp, m.ctm_type, &e) != 0 || e.cte_bits > 64) {
355178479Sjb		xyerror(D_UNKNOWN, "cg: bad field: off %lu type <%ld> "
356178479Sjb		    "bits %u\n", m.ctm_offset, m.ctm_type, e.cte_bits);
357178479Sjb	}
358178479Sjb
359178479Sjb	if ((r1 = dt_regset_alloc(drp)) == -1 ||
360178479Sjb	    (r2 = dt_regset_alloc(drp)) == -1 ||
361178479Sjb	    (r3 = dt_regset_alloc(drp)) == -1)
362178479Sjb		longjmp(yypcb->pcb_jmpbuf, EDT_NOREG);
363178479Sjb
364178479Sjb	/*
365178479Sjb	 * Compute shifts and masks.  We need to compute "shift" as the amount
366178479Sjb	 * we need to shift left to position our field in the containing word.
367178479Sjb	 * Refer to the comments in dt_cg_field_get(), above, for more info.
368178479Sjb	 * We then compute fmask as the mask that truncates the value in the
369178479Sjb	 * input register to width cte_bits, and cmask as the mask used to
370178479Sjb	 * pass through the containing bits and zero the field bits.
371178479Sjb	 */
372178479Sjb#if BYTE_ORDER == _BIG_ENDIAN
373178479Sjb	shift = clp2(P2ROUNDUP(e.cte_bits, NBBY) / NBBY) * NBBY -
374178479Sjb	    (m.ctm_offset % NBBY + e.cte_bits);
375178479Sjb#else
376178479Sjb	shift = m.ctm_offset % NBBY;
377178479Sjb#endif
378178479Sjb	fmask = (1ULL << e.cte_bits) - 1;
379178479Sjb	cmask = ~(fmask << shift);
380178479Sjb
381178479Sjb	instr = DIF_INSTR_LOAD(
382178479Sjb	    dt_cg_load(dst, fp, m.ctm_type), dst->dn_reg, r1);
383178479Sjb	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
384178479Sjb
385178479Sjb	dt_cg_setx(dlp, r2, cmask);
386178479Sjb	instr = DIF_INSTR_FMT(DIF_OP_AND, r1, r2, r1);
387178479Sjb	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
388178479Sjb
389178479Sjb	dt_cg_setx(dlp, r2, fmask);
390178479Sjb	instr = DIF_INSTR_FMT(DIF_OP_AND, src->dn_reg, r2, r2);
391178479Sjb	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
392178479Sjb
393178479Sjb	dt_cg_setx(dlp, r3, shift);
394178479Sjb	instr = DIF_INSTR_FMT(DIF_OP_SLL, r2, r3, r2);
395178479Sjb	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
396178479Sjb
397178479Sjb	instr = DIF_INSTR_FMT(DIF_OP_OR, r1, r2, r1);
398178479Sjb	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
399178479Sjb
400178479Sjb	dt_regset_free(drp, r3);
401178479Sjb	dt_regset_free(drp, r2);
402178479Sjb
403178479Sjb	return (r1);
404178479Sjb}
405178479Sjb
406178479Sjbstatic void
407178479Sjbdt_cg_store(dt_node_t *src, dt_irlist_t *dlp, dt_regset_t *drp, dt_node_t *dst)
408178479Sjb{
409178479Sjb	ctf_encoding_t e;
410178479Sjb	dif_instr_t instr;
411178479Sjb	size_t size;
412178479Sjb	int reg;
413178479Sjb
414178479Sjb	/*
415178479Sjb	 * If we're loading a bit-field, the size of our store is found by
416178479Sjb	 * rounding dst's cte_bits up to a byte boundary and then finding the
417178479Sjb	 * nearest power of two to this value (see clp2(), above).
418178479Sjb	 */
419178479Sjb	if ((dst->dn_flags & DT_NF_BITFIELD) &&
420178479Sjb	    ctf_type_encoding(dst->dn_ctfp, dst->dn_type, &e) != CTF_ERR)
421178479Sjb		size = clp2(P2ROUNDUP(e.cte_bits, NBBY) / NBBY);
422178479Sjb	else
423178479Sjb		size = dt_node_type_size(src);
424178479Sjb
425178479Sjb	if (src->dn_flags & DT_NF_REF) {
426178479Sjb		if ((reg = dt_regset_alloc(drp)) == -1)
427178479Sjb			longjmp(yypcb->pcb_jmpbuf, EDT_NOREG);
428178479Sjb		dt_cg_setx(dlp, reg, size);
429178479Sjb		instr = DIF_INSTR_COPYS(src->dn_reg, reg, dst->dn_reg);
430178479Sjb		dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
431178479Sjb		dt_regset_free(drp, reg);
432178479Sjb	} else {
433178479Sjb		if (dst->dn_flags & DT_NF_BITFIELD)
434178479Sjb			reg = dt_cg_field_set(src, dlp, drp, dst);
435178479Sjb		else
436178479Sjb			reg = src->dn_reg;
437178479Sjb
438178479Sjb		switch (size) {
439178479Sjb		case 1:
440178479Sjb			instr = DIF_INSTR_STORE(DIF_OP_STB, reg, dst->dn_reg);
441178479Sjb			break;
442178479Sjb		case 2:
443178479Sjb			instr = DIF_INSTR_STORE(DIF_OP_STH, reg, dst->dn_reg);
444178479Sjb			break;
445178479Sjb		case 4:
446178479Sjb			instr = DIF_INSTR_STORE(DIF_OP_STW, reg, dst->dn_reg);
447178479Sjb			break;
448178479Sjb		case 8:
449178479Sjb			instr = DIF_INSTR_STORE(DIF_OP_STX, reg, dst->dn_reg);
450178479Sjb			break;
451178479Sjb		default:
452178479Sjb			xyerror(D_UNKNOWN, "internal error -- cg cannot store "
453178479Sjb			    "size %lu when passed by value\n", (ulong_t)size);
454178479Sjb		}
455178479Sjb		dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
456178479Sjb
457178479Sjb		if (dst->dn_flags & DT_NF_BITFIELD)
458178479Sjb			dt_regset_free(drp, reg);
459178479Sjb	}
460178479Sjb}
461178479Sjb
462178479Sjb/*
463178479Sjb * Generate code for a typecast or for argument promotion from the type of the
464178479Sjb * actual to the type of the formal.  We need to generate code for casts when
465178479Sjb * a scalar type is being narrowed or changing signed-ness.  We first shift the
466178479Sjb * desired bits high (losing excess bits if narrowing) and then shift them down
467178479Sjb * using logical shift (unsigned result) or arithmetic shift (signed result).
468178479Sjb */
469178479Sjbstatic void
470178479Sjbdt_cg_typecast(const dt_node_t *src, const dt_node_t *dst,
471178479Sjb    dt_irlist_t *dlp, dt_regset_t *drp)
472178479Sjb{
473178479Sjb	size_t srcsize = dt_node_type_size(src);
474178479Sjb	size_t dstsize = dt_node_type_size(dst);
475178479Sjb
476178479Sjb	dif_instr_t instr;
477178479Sjb	int reg, n;
478178479Sjb
479178479Sjb	if (dt_node_is_scalar(dst) && (dstsize < srcsize ||
480178479Sjb	    (src->dn_flags & DT_NF_SIGNED) ^ (dst->dn_flags & DT_NF_SIGNED))) {
481178479Sjb		if ((reg = dt_regset_alloc(drp)) == -1)
482178479Sjb			longjmp(yypcb->pcb_jmpbuf, EDT_NOREG);
483178479Sjb
484178479Sjb		if (dstsize < srcsize)
485178479Sjb			n = sizeof (uint64_t) * NBBY - dstsize * NBBY;
486178479Sjb		else
487178479Sjb			n = sizeof (uint64_t) * NBBY - srcsize * NBBY;
488178479Sjb
489178479Sjb		dt_cg_setx(dlp, reg, n);
490178479Sjb
491178479Sjb		instr = DIF_INSTR_FMT(DIF_OP_SLL,
492178479Sjb		    src->dn_reg, reg, dst->dn_reg);
493178479Sjb		dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
494178479Sjb
495178479Sjb		instr = DIF_INSTR_FMT((dst->dn_flags & DT_NF_SIGNED) ?
496178479Sjb		    DIF_OP_SRA : DIF_OP_SRL, dst->dn_reg, reg, dst->dn_reg);
497178479Sjb
498178479Sjb		dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
499178479Sjb		dt_regset_free(drp, reg);
500178479Sjb	}
501178479Sjb}
502178479Sjb
503178479Sjb/*
504178479Sjb * Generate code to push the specified argument list on to the tuple stack.
505178479Sjb * We use this routine for handling subroutine calls and associative arrays.
506178479Sjb * We must first generate code for all subexpressions before loading the stack
507178479Sjb * because any subexpression could itself require the use of the tuple stack.
508178479Sjb * This holds a number of registers equal to the number of arguments, but this
509178479Sjb * is not a huge problem because the number of arguments can't exceed the
510178479Sjb * number of tuple register stack elements anyway.  At most one extra register
511178479Sjb * is required (either by dt_cg_typecast() or for dtdt_size, below).  This
512178479Sjb * implies that a DIF implementation should offer a number of general purpose
513178479Sjb * registers at least one greater than the number of tuple registers.
514178479Sjb */
515178479Sjbstatic void
516178479Sjbdt_cg_arglist(dt_ident_t *idp, dt_node_t *args,
517178479Sjb    dt_irlist_t *dlp, dt_regset_t *drp)
518178479Sjb{
519178479Sjb	const dt_idsig_t *isp = idp->di_data;
520178479Sjb	dt_node_t *dnp;
521178479Sjb	int i = 0;
522178479Sjb
523178479Sjb	for (dnp = args; dnp != NULL; dnp = dnp->dn_list)
524178479Sjb		dt_cg_node(dnp, dlp, drp);
525178479Sjb
526178479Sjb	dt_irlist_append(dlp,
527178479Sjb	    dt_cg_node_alloc(DT_LBL_NONE, DIF_INSTR_FLUSHTS));
528178479Sjb
529178479Sjb	for (dnp = args; dnp != NULL; dnp = dnp->dn_list, i++) {
530178479Sjb		dtrace_diftype_t t;
531178479Sjb		dif_instr_t instr;
532178479Sjb		uint_t op;
533178479Sjb		int reg;
534178479Sjb
535178479Sjb		dt_node_diftype(yypcb->pcb_hdl, dnp, &t);
536178479Sjb
537178479Sjb		isp->dis_args[i].dn_reg = dnp->dn_reg; /* re-use register */
538178479Sjb		dt_cg_typecast(dnp, &isp->dis_args[i], dlp, drp);
539178479Sjb		isp->dis_args[i].dn_reg = -1;
540178479Sjb
541178479Sjb		if (t.dtdt_flags & DIF_TF_BYREF)
542178479Sjb			op = DIF_OP_PUSHTR;
543178479Sjb		else
544178479Sjb			op = DIF_OP_PUSHTV;
545178479Sjb
546178479Sjb		if (t.dtdt_size != 0) {
547178479Sjb			if ((reg = dt_regset_alloc(drp)) == -1)
548178479Sjb				longjmp(yypcb->pcb_jmpbuf, EDT_NOREG);
549178479Sjb			dt_cg_setx(dlp, reg, t.dtdt_size);
550178479Sjb		} else
551178479Sjb			reg = DIF_REG_R0;
552178479Sjb
553178479Sjb		instr = DIF_INSTR_PUSHTS(op, t.dtdt_kind, reg, dnp->dn_reg);
554178479Sjb		dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
555178479Sjb		dt_regset_free(drp, dnp->dn_reg);
556178479Sjb
557178479Sjb		if (reg != DIF_REG_R0)
558178479Sjb			dt_regset_free(drp, reg);
559178479Sjb	}
560178479Sjb
561178479Sjb	if (i > yypcb->pcb_hdl->dt_conf.dtc_diftupregs)
562178479Sjb		longjmp(yypcb->pcb_jmpbuf, EDT_NOTUPREG);
563178479Sjb}
564178479Sjb
565178479Sjbstatic void
566178479Sjbdt_cg_arithmetic_op(dt_node_t *dnp, dt_irlist_t *dlp,
567178479Sjb    dt_regset_t *drp, uint_t op)
568178479Sjb{
569178479Sjb	int is_ptr_op = (dnp->dn_op == DT_TOK_ADD || dnp->dn_op == DT_TOK_SUB ||
570178479Sjb	    dnp->dn_op == DT_TOK_ADD_EQ || dnp->dn_op == DT_TOK_SUB_EQ);
571178479Sjb
572178479Sjb	int lp_is_ptr = dt_node_is_pointer(dnp->dn_left);
573178479Sjb	int rp_is_ptr = dt_node_is_pointer(dnp->dn_right);
574178479Sjb
575178479Sjb	dif_instr_t instr;
576178479Sjb
577178479Sjb	if (lp_is_ptr && rp_is_ptr) {
578178479Sjb		assert(dnp->dn_op == DT_TOK_SUB);
579178479Sjb		is_ptr_op = 0;
580178479Sjb	}
581178479Sjb
582178479Sjb	dt_cg_node(dnp->dn_left, dlp, drp);
583178479Sjb	if (is_ptr_op && rp_is_ptr)
584178479Sjb		dt_cg_ptrsize(dnp, dlp, drp, DIF_OP_MUL, dnp->dn_left->dn_reg);
585178479Sjb
586178479Sjb	dt_cg_node(dnp->dn_right, dlp, drp);
587178479Sjb	if (is_ptr_op && lp_is_ptr)
588178479Sjb		dt_cg_ptrsize(dnp, dlp, drp, DIF_OP_MUL, dnp->dn_right->dn_reg);
589178479Sjb
590178479Sjb	instr = DIF_INSTR_FMT(op, dnp->dn_left->dn_reg,
591178479Sjb	    dnp->dn_right->dn_reg, dnp->dn_left->dn_reg);
592178479Sjb
593178479Sjb	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
594178479Sjb	dt_regset_free(drp, dnp->dn_right->dn_reg);
595178479Sjb	dnp->dn_reg = dnp->dn_left->dn_reg;
596178479Sjb
597178479Sjb	if (lp_is_ptr && rp_is_ptr)
598178479Sjb		dt_cg_ptrsize(dnp->dn_right,
599178479Sjb		    dlp, drp, DIF_OP_UDIV, dnp->dn_reg);
600178479Sjb}
601178479Sjb
602178479Sjbstatic uint_t
603178479Sjbdt_cg_stvar(const dt_ident_t *idp)
604178479Sjb{
605178479Sjb	static const uint_t aops[] = { DIF_OP_STGAA, DIF_OP_STTAA, DIF_OP_NOP };
606178479Sjb	static const uint_t sops[] = { DIF_OP_STGS, DIF_OP_STTS, DIF_OP_STLS };
607178479Sjb
608178479Sjb	uint_t i = (((idp->di_flags & DT_IDFLG_LOCAL) != 0) << 1) |
609178479Sjb	    ((idp->di_flags & DT_IDFLG_TLS) != 0);
610178479Sjb
611178479Sjb	return (idp->di_kind == DT_IDENT_ARRAY ? aops[i] : sops[i]);
612178479Sjb}
613178479Sjb
614178479Sjbstatic void
615178479Sjbdt_cg_prearith_op(dt_node_t *dnp, dt_irlist_t *dlp, dt_regset_t *drp, uint_t op)
616178479Sjb{
617178479Sjb	ctf_file_t *ctfp = dnp->dn_ctfp;
618178479Sjb	dif_instr_t instr;
619178479Sjb	ctf_id_t type;
620178479Sjb	ssize_t size = 1;
621178479Sjb	int reg;
622178479Sjb
623178479Sjb	if (dt_node_is_pointer(dnp)) {
624178479Sjb		type = ctf_type_resolve(ctfp, dnp->dn_type);
625178479Sjb		assert(ctf_type_kind(ctfp, type) == CTF_K_POINTER);
626178479Sjb		size = ctf_type_size(ctfp, ctf_type_reference(ctfp, type));
627178479Sjb	}
628178479Sjb
629178479Sjb	dt_cg_node(dnp->dn_child, dlp, drp);
630178479Sjb	dnp->dn_reg = dnp->dn_child->dn_reg;
631178479Sjb
632178479Sjb	if ((reg = dt_regset_alloc(drp)) == -1)
633178479Sjb		longjmp(yypcb->pcb_jmpbuf, EDT_NOREG);
634178479Sjb
635178479Sjb	dt_cg_setx(dlp, reg, size);
636178479Sjb
637178479Sjb	instr = DIF_INSTR_FMT(op, dnp->dn_reg, reg, dnp->dn_reg);
638178479Sjb	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
639178479Sjb	dt_regset_free(drp, reg);
640178479Sjb
641178479Sjb	/*
642178479Sjb	 * If we are modifying a variable, generate an stv instruction from
643178479Sjb	 * the variable specified by the identifier.  If we are storing to a
644178479Sjb	 * memory address, generate code again for the left-hand side using
645178479Sjb	 * DT_NF_REF to get the address, and then generate a store to it.
646178479Sjb	 * In both paths, we store the value in dnp->dn_reg (the new value).
647178479Sjb	 */
648178479Sjb	if (dnp->dn_child->dn_kind == DT_NODE_VAR) {
649178479Sjb		dt_ident_t *idp = dt_ident_resolve(dnp->dn_child->dn_ident);
650178479Sjb
651178479Sjb		idp->di_flags |= DT_IDFLG_DIFW;
652178479Sjb		instr = DIF_INSTR_STV(dt_cg_stvar(idp),
653178479Sjb		    idp->di_id, dnp->dn_reg);
654178479Sjb		dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
655178479Sjb	} else {
656178479Sjb		uint_t rbit = dnp->dn_child->dn_flags & DT_NF_REF;
657178479Sjb
658178479Sjb		assert(dnp->dn_child->dn_flags & DT_NF_WRITABLE);
659178479Sjb		assert(dnp->dn_child->dn_flags & DT_NF_LVALUE);
660178479Sjb
661178479Sjb		dnp->dn_child->dn_flags |= DT_NF_REF; /* force pass-by-ref */
662178479Sjb		dt_cg_node(dnp->dn_child, dlp, drp);
663178479Sjb
664178479Sjb		dt_cg_store(dnp, dlp, drp, dnp->dn_child);
665178479Sjb		dt_regset_free(drp, dnp->dn_child->dn_reg);
666178479Sjb
667178479Sjb		dnp->dn_left->dn_flags &= ~DT_NF_REF;
668178479Sjb		dnp->dn_left->dn_flags |= rbit;
669178479Sjb	}
670178479Sjb}
671178479Sjb
672178479Sjbstatic void
673178479Sjbdt_cg_postarith_op(dt_node_t *dnp, dt_irlist_t *dlp,
674178479Sjb    dt_regset_t *drp, uint_t op)
675178479Sjb{
676178479Sjb	ctf_file_t *ctfp = dnp->dn_ctfp;
677178479Sjb	dif_instr_t instr;
678178479Sjb	ctf_id_t type;
679178479Sjb	ssize_t size = 1;
680178479Sjb	int nreg;
681178479Sjb
682178479Sjb	if (dt_node_is_pointer(dnp)) {
683178479Sjb		type = ctf_type_resolve(ctfp, dnp->dn_type);
684178479Sjb		assert(ctf_type_kind(ctfp, type) == CTF_K_POINTER);
685178479Sjb		size = ctf_type_size(ctfp, ctf_type_reference(ctfp, type));
686178479Sjb	}
687178479Sjb
688178479Sjb	dt_cg_node(dnp->dn_child, dlp, drp);
689178479Sjb	dnp->dn_reg = dnp->dn_child->dn_reg;
690178479Sjb
691178479Sjb	if ((nreg = dt_regset_alloc(drp)) == -1)
692178479Sjb		longjmp(yypcb->pcb_jmpbuf, EDT_NOREG);
693178479Sjb
694178479Sjb	dt_cg_setx(dlp, nreg, size);
695178479Sjb	instr = DIF_INSTR_FMT(op, dnp->dn_reg, nreg, nreg);
696178479Sjb	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
697178479Sjb
698178479Sjb	/*
699178479Sjb	 * If we are modifying a variable, generate an stv instruction from
700178479Sjb	 * the variable specified by the identifier.  If we are storing to a
701178479Sjb	 * memory address, generate code again for the left-hand side using
702178479Sjb	 * DT_NF_REF to get the address, and then generate a store to it.
703178479Sjb	 * In both paths, we store the value from 'nreg' (the new value).
704178479Sjb	 */
705178479Sjb	if (dnp->dn_child->dn_kind == DT_NODE_VAR) {
706178479Sjb		dt_ident_t *idp = dt_ident_resolve(dnp->dn_child->dn_ident);
707178479Sjb
708178479Sjb		idp->di_flags |= DT_IDFLG_DIFW;
709178479Sjb		instr = DIF_INSTR_STV(dt_cg_stvar(idp), idp->di_id, nreg);
710178479Sjb		dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
711178479Sjb	} else {
712178479Sjb		uint_t rbit = dnp->dn_child->dn_flags & DT_NF_REF;
713178479Sjb		int oreg = dnp->dn_reg;
714178479Sjb
715178479Sjb		assert(dnp->dn_child->dn_flags & DT_NF_WRITABLE);
716178479Sjb		assert(dnp->dn_child->dn_flags & DT_NF_LVALUE);
717178479Sjb
718178479Sjb		dnp->dn_child->dn_flags |= DT_NF_REF; /* force pass-by-ref */
719178479Sjb		dt_cg_node(dnp->dn_child, dlp, drp);
720178479Sjb
721178479Sjb		dnp->dn_reg = nreg;
722178479Sjb		dt_cg_store(dnp, dlp, drp, dnp->dn_child);
723178479Sjb		dnp->dn_reg = oreg;
724178479Sjb
725178479Sjb		dt_regset_free(drp, dnp->dn_child->dn_reg);
726178479Sjb		dnp->dn_left->dn_flags &= ~DT_NF_REF;
727178479Sjb		dnp->dn_left->dn_flags |= rbit;
728178479Sjb	}
729178479Sjb
730178479Sjb	dt_regset_free(drp, nreg);
731178479Sjb}
732178479Sjb
733178479Sjb/*
734178479Sjb * Determine if we should perform signed or unsigned comparison for an OP2.
735178479Sjb * If both operands are of arithmetic type, perform the usual arithmetic
736178479Sjb * conversions to determine the common real type for comparison [ISOC 6.5.8.3].
737178479Sjb */
738178479Sjbstatic int
739178479Sjbdt_cg_compare_signed(dt_node_t *dnp)
740178479Sjb{
741178479Sjb	dt_node_t dn;
742178479Sjb
743178479Sjb	if (dt_node_is_string(dnp->dn_left) ||
744178479Sjb	    dt_node_is_string(dnp->dn_right))
745178479Sjb		return (1); /* strings always compare signed */
746178479Sjb	else if (!dt_node_is_arith(dnp->dn_left) ||
747178479Sjb	    !dt_node_is_arith(dnp->dn_right))
748178479Sjb		return (0); /* non-arithmetic types always compare unsigned */
749178479Sjb
750178479Sjb	bzero(&dn, sizeof (dn));
751178479Sjb	dt_node_promote(dnp->dn_left, dnp->dn_right, &dn);
752178479Sjb	return (dn.dn_flags & DT_NF_SIGNED);
753178479Sjb}
754178479Sjb
755178479Sjbstatic void
756178479Sjbdt_cg_compare_op(dt_node_t *dnp, dt_irlist_t *dlp, dt_regset_t *drp, uint_t op)
757178479Sjb{
758178479Sjb	uint_t lbl_true = dt_irlist_label(dlp);
759178479Sjb	uint_t lbl_post = dt_irlist_label(dlp);
760178479Sjb
761178479Sjb	dif_instr_t instr;
762178479Sjb	uint_t opc;
763178479Sjb
764178479Sjb	dt_cg_node(dnp->dn_left, dlp, drp);
765178479Sjb	dt_cg_node(dnp->dn_right, dlp, drp);
766178479Sjb
767178479Sjb	if (dt_node_is_string(dnp->dn_left) || dt_node_is_string(dnp->dn_right))
768178479Sjb		opc = DIF_OP_SCMP;
769178479Sjb	else
770178479Sjb		opc = DIF_OP_CMP;
771178479Sjb
772178479Sjb	instr = DIF_INSTR_CMP(opc, dnp->dn_left->dn_reg, dnp->dn_right->dn_reg);
773178479Sjb	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
774178479Sjb	dt_regset_free(drp, dnp->dn_right->dn_reg);
775178479Sjb	dnp->dn_reg = dnp->dn_left->dn_reg;
776178479Sjb
777178479Sjb	instr = DIF_INSTR_BRANCH(op, lbl_true);
778178479Sjb	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
779178479Sjb
780178479Sjb	instr = DIF_INSTR_MOV(DIF_REG_R0, dnp->dn_reg);
781178479Sjb	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
782178479Sjb
783178479Sjb	instr = DIF_INSTR_BRANCH(DIF_OP_BA, lbl_post);
784178479Sjb	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
785178479Sjb
786178479Sjb	dt_cg_xsetx(dlp, NULL, lbl_true, dnp->dn_reg, 1);
787178479Sjb	dt_irlist_append(dlp, dt_cg_node_alloc(lbl_post, DIF_INSTR_NOP));
788178479Sjb}
789178479Sjb
790178479Sjb/*
791178479Sjb * Code generation for the ternary op requires some trickery with the assembler
792178479Sjb * in order to conserve registers.  We generate code for dn_expr and dn_left
793178479Sjb * and free their registers so they do not have be consumed across codegen for
794178479Sjb * dn_right.  We insert a dummy MOV at the end of dn_left into the destination
795178479Sjb * register, which is not yet known because we haven't done dn_right yet, and
796178479Sjb * save the pointer to this instruction node.  We then generate code for
797178479Sjb * dn_right and use its register as our output.  Finally, we reach back and
798178479Sjb * patch the instruction for dn_left to move its output into this register.
799178479Sjb */
800178479Sjbstatic void
801178479Sjbdt_cg_ternary_op(dt_node_t *dnp, dt_irlist_t *dlp, dt_regset_t *drp)
802178479Sjb{
803178479Sjb	uint_t lbl_false = dt_irlist_label(dlp);
804178479Sjb	uint_t lbl_post = dt_irlist_label(dlp);
805178479Sjb
806178479Sjb	dif_instr_t instr;
807178479Sjb	dt_irnode_t *dip;
808178479Sjb
809178479Sjb	dt_cg_node(dnp->dn_expr, dlp, drp);
810178479Sjb	instr = DIF_INSTR_TST(dnp->dn_expr->dn_reg);
811178479Sjb	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
812178479Sjb	dt_regset_free(drp, dnp->dn_expr->dn_reg);
813178479Sjb
814178479Sjb	instr = DIF_INSTR_BRANCH(DIF_OP_BE, lbl_false);
815178479Sjb	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
816178479Sjb
817178479Sjb	dt_cg_node(dnp->dn_left, dlp, drp);
818178479Sjb	instr = DIF_INSTR_MOV(dnp->dn_left->dn_reg, DIF_REG_R0);
819178479Sjb	dip = dt_cg_node_alloc(DT_LBL_NONE, instr); /* save dip for below */
820178479Sjb	dt_irlist_append(dlp, dip);
821178479Sjb	dt_regset_free(drp, dnp->dn_left->dn_reg);
822178479Sjb
823178479Sjb	instr = DIF_INSTR_BRANCH(DIF_OP_BA, lbl_post);
824178479Sjb	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
825178479Sjb
826178479Sjb	dt_irlist_append(dlp, dt_cg_node_alloc(lbl_false, DIF_INSTR_NOP));
827178479Sjb	dt_cg_node(dnp->dn_right, dlp, drp);
828178479Sjb	dnp->dn_reg = dnp->dn_right->dn_reg;
829178479Sjb
830178479Sjb	/*
831178479Sjb	 * Now that dn_reg is assigned, reach back and patch the correct MOV
832178479Sjb	 * instruction into the tail of dn_left.  We know dn_reg was unused
833178479Sjb	 * at that point because otherwise dn_right couldn't have allocated it.
834178479Sjb	 */
835178479Sjb	dip->di_instr = DIF_INSTR_MOV(dnp->dn_left->dn_reg, dnp->dn_reg);
836178479Sjb	dt_irlist_append(dlp, dt_cg_node_alloc(lbl_post, DIF_INSTR_NOP));
837178479Sjb}
838178479Sjb
839178479Sjbstatic void
840178479Sjbdt_cg_logical_and(dt_node_t *dnp, dt_irlist_t *dlp, dt_regset_t *drp)
841178479Sjb{
842178479Sjb	uint_t lbl_false = dt_irlist_label(dlp);
843178479Sjb	uint_t lbl_post = dt_irlist_label(dlp);
844178479Sjb
845178479Sjb	dif_instr_t instr;
846178479Sjb
847178479Sjb	dt_cg_node(dnp->dn_left, dlp, drp);
848178479Sjb	instr = DIF_INSTR_TST(dnp->dn_left->dn_reg);
849178479Sjb	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
850178479Sjb	dt_regset_free(drp, dnp->dn_left->dn_reg);
851178479Sjb
852178479Sjb	instr = DIF_INSTR_BRANCH(DIF_OP_BE, lbl_false);
853178479Sjb	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
854178479Sjb
855178479Sjb	dt_cg_node(dnp->dn_right, dlp, drp);
856178479Sjb	instr = DIF_INSTR_TST(dnp->dn_right->dn_reg);
857178479Sjb	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
858178479Sjb	dnp->dn_reg = dnp->dn_right->dn_reg;
859178479Sjb
860178479Sjb	instr = DIF_INSTR_BRANCH(DIF_OP_BE, lbl_false);
861178479Sjb	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
862178479Sjb
863178479Sjb	dt_cg_setx(dlp, dnp->dn_reg, 1);
864178479Sjb
865178479Sjb	instr = DIF_INSTR_BRANCH(DIF_OP_BA, lbl_post);
866178479Sjb	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
867178479Sjb
868178479Sjb	instr = DIF_INSTR_MOV(DIF_REG_R0, dnp->dn_reg);
869178479Sjb	dt_irlist_append(dlp, dt_cg_node_alloc(lbl_false, instr));
870178479Sjb
871178479Sjb	dt_irlist_append(dlp, dt_cg_node_alloc(lbl_post, DIF_INSTR_NOP));
872178479Sjb}
873178479Sjb
874178479Sjbstatic void
875178479Sjbdt_cg_logical_xor(dt_node_t *dnp, dt_irlist_t *dlp, dt_regset_t *drp)
876178479Sjb{
877178479Sjb	uint_t lbl_next = dt_irlist_label(dlp);
878178479Sjb	uint_t lbl_tail = dt_irlist_label(dlp);
879178479Sjb
880178479Sjb	dif_instr_t instr;
881178479Sjb
882178479Sjb	dt_cg_node(dnp->dn_left, dlp, drp);
883178479Sjb	instr = DIF_INSTR_TST(dnp->dn_left->dn_reg);
884178479Sjb	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
885178479Sjb
886178479Sjb	instr = DIF_INSTR_BRANCH(DIF_OP_BE, lbl_next);
887178479Sjb	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
888178479Sjb	dt_cg_setx(dlp, dnp->dn_left->dn_reg, 1);
889178479Sjb
890178479Sjb	dt_irlist_append(dlp, dt_cg_node_alloc(lbl_next, DIF_INSTR_NOP));
891178479Sjb	dt_cg_node(dnp->dn_right, dlp, drp);
892178479Sjb
893178479Sjb	instr = DIF_INSTR_TST(dnp->dn_right->dn_reg);
894178479Sjb	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
895178479Sjb
896178479Sjb	instr = DIF_INSTR_BRANCH(DIF_OP_BE, lbl_tail);
897178479Sjb	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
898178479Sjb	dt_cg_setx(dlp, dnp->dn_right->dn_reg, 1);
899178479Sjb
900178479Sjb	instr = DIF_INSTR_FMT(DIF_OP_XOR, dnp->dn_left->dn_reg,
901178479Sjb	    dnp->dn_right->dn_reg, dnp->dn_left->dn_reg);
902178479Sjb
903178479Sjb	dt_irlist_append(dlp, dt_cg_node_alloc(lbl_tail, instr));
904178479Sjb
905178479Sjb	dt_regset_free(drp, dnp->dn_right->dn_reg);
906178479Sjb	dnp->dn_reg = dnp->dn_left->dn_reg;
907178479Sjb}
908178479Sjb
909178479Sjbstatic void
910178479Sjbdt_cg_logical_or(dt_node_t *dnp, dt_irlist_t *dlp, dt_regset_t *drp)
911178479Sjb{
912178479Sjb	uint_t lbl_true = dt_irlist_label(dlp);
913178479Sjb	uint_t lbl_false = dt_irlist_label(dlp);
914178479Sjb	uint_t lbl_post = dt_irlist_label(dlp);
915178479Sjb
916178479Sjb	dif_instr_t instr;
917178479Sjb
918178479Sjb	dt_cg_node(dnp->dn_left, dlp, drp);
919178479Sjb	instr = DIF_INSTR_TST(dnp->dn_left->dn_reg);
920178479Sjb	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
921178479Sjb	dt_regset_free(drp, dnp->dn_left->dn_reg);
922178479Sjb
923178479Sjb	instr = DIF_INSTR_BRANCH(DIF_OP_BNE, lbl_true);
924178479Sjb	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
925178479Sjb
926178479Sjb	dt_cg_node(dnp->dn_right, dlp, drp);
927178479Sjb	instr = DIF_INSTR_TST(dnp->dn_right->dn_reg);
928178479Sjb	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
929178479Sjb	dnp->dn_reg = dnp->dn_right->dn_reg;
930178479Sjb
931178479Sjb	instr = DIF_INSTR_BRANCH(DIF_OP_BE, lbl_false);
932178479Sjb	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
933178479Sjb
934178479Sjb	dt_cg_xsetx(dlp, NULL, lbl_true, dnp->dn_reg, 1);
935178479Sjb
936178479Sjb	instr = DIF_INSTR_BRANCH(DIF_OP_BA, lbl_post);
937178479Sjb	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
938178479Sjb
939178479Sjb	instr = DIF_INSTR_MOV(DIF_REG_R0, dnp->dn_reg);
940178479Sjb	dt_irlist_append(dlp, dt_cg_node_alloc(lbl_false, instr));
941178479Sjb
942178479Sjb	dt_irlist_append(dlp, dt_cg_node_alloc(lbl_post, DIF_INSTR_NOP));
943178479Sjb}
944178479Sjb
945178479Sjbstatic void
946178479Sjbdt_cg_logical_neg(dt_node_t *dnp, dt_irlist_t *dlp, dt_regset_t *drp)
947178479Sjb{
948178479Sjb	uint_t lbl_zero = dt_irlist_label(dlp);
949178479Sjb	uint_t lbl_post = dt_irlist_label(dlp);
950178479Sjb
951178479Sjb	dif_instr_t instr;
952178479Sjb
953178479Sjb	dt_cg_node(dnp->dn_child, dlp, drp);
954178479Sjb	dnp->dn_reg = dnp->dn_child->dn_reg;
955178479Sjb
956178479Sjb	instr = DIF_INSTR_TST(dnp->dn_reg);
957178479Sjb	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
958178479Sjb
959178479Sjb	instr = DIF_INSTR_BRANCH(DIF_OP_BE, lbl_zero);
960178479Sjb	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
961178479Sjb
962178479Sjb	instr = DIF_INSTR_MOV(DIF_REG_R0, dnp->dn_reg);
963178479Sjb	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
964178479Sjb
965178479Sjb	instr = DIF_INSTR_BRANCH(DIF_OP_BA, lbl_post);
966178479Sjb	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
967178479Sjb
968178479Sjb	dt_cg_xsetx(dlp, NULL, lbl_zero, dnp->dn_reg, 1);
969178479Sjb	dt_irlist_append(dlp, dt_cg_node_alloc(lbl_post, DIF_INSTR_NOP));
970178479Sjb}
971178479Sjb
972178479Sjbstatic void
973178479Sjbdt_cg_asgn_op(dt_node_t *dnp, dt_irlist_t *dlp, dt_regset_t *drp)
974178479Sjb{
975178479Sjb	dif_instr_t instr;
976178479Sjb	dt_ident_t *idp;
977178479Sjb
978178479Sjb	/*
979178479Sjb	 * If we are performing a structure assignment of a translated type,
980178479Sjb	 * we must instantiate all members and create a snapshot of the object
981178479Sjb	 * in scratch space.  We allocs a chunk of memory, generate code for
982178479Sjb	 * each member, and then set dnp->dn_reg to the scratch object address.
983178479Sjb	 */
984178479Sjb	if ((idp = dt_node_resolve(dnp->dn_right, DT_IDENT_XLSOU)) != NULL) {
985178479Sjb		ctf_membinfo_t ctm;
986178479Sjb		dt_xlator_t *dxp = idp->di_data;
987178479Sjb		dt_node_t *mnp, dn, mn;
988178479Sjb		int r1, r2;
989178479Sjb
990178479Sjb		/*
991178479Sjb		 * Create two fake dt_node_t's representing operator "." and a
992178479Sjb		 * right-hand identifier child node.  These will be repeatedly
993178479Sjb		 * modified according to each instantiated member so that we
994178479Sjb		 * can pass them to dt_cg_store() and effect a member store.
995178479Sjb		 */
996178479Sjb		bzero(&dn, sizeof (dt_node_t));
997178479Sjb		dn.dn_kind = DT_NODE_OP2;
998178479Sjb		dn.dn_op = DT_TOK_DOT;
999178479Sjb		dn.dn_left = dnp;
1000178479Sjb		dn.dn_right = &mn;
1001178479Sjb
1002178479Sjb		bzero(&mn, sizeof (dt_node_t));
1003178479Sjb		mn.dn_kind = DT_NODE_IDENT;
1004178479Sjb		mn.dn_op = DT_TOK_IDENT;
1005178479Sjb
1006178479Sjb		/*
1007178479Sjb		 * Allocate a register for our scratch data pointer.  First we
1008178479Sjb		 * set it to the size of our data structure, and then replace
1009178479Sjb		 * it with the result of an allocs of the specified size.
1010178479Sjb		 */
1011178479Sjb		if ((r1 = dt_regset_alloc(drp)) == -1)
1012178479Sjb			longjmp(yypcb->pcb_jmpbuf, EDT_NOREG);
1013178479Sjb
1014178479Sjb		dt_cg_setx(dlp, r1,
1015178479Sjb		    ctf_type_size(dxp->dx_dst_ctfp, dxp->dx_dst_base));
1016178479Sjb
1017178479Sjb		instr = DIF_INSTR_ALLOCS(r1, r1);
1018178479Sjb		dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
1019178479Sjb
1020178479Sjb		/*
1021178479Sjb		 * When dt_cg_asgn_op() is called, we have already generated
1022178479Sjb		 * code for dnp->dn_right, which is the translator input.  We
1023178479Sjb		 * now associate this register with the translator's input
1024178479Sjb		 * identifier so it can be referenced during our member loop.
1025178479Sjb		 */
1026178479Sjb		dxp->dx_ident->di_flags |= DT_IDFLG_CGREG;
1027178479Sjb		dxp->dx_ident->di_id = dnp->dn_right->dn_reg;
1028178479Sjb
1029178479Sjb		for (mnp = dxp->dx_members; mnp != NULL; mnp = mnp->dn_list) {
1030178479Sjb			/*
1031178479Sjb			 * Generate code for the translator member expression,
1032178479Sjb			 * and then cast the result to the member type.
1033178479Sjb			 */
1034178479Sjb			dt_cg_node(mnp->dn_membexpr, dlp, drp);
1035178479Sjb			mnp->dn_reg = mnp->dn_membexpr->dn_reg;
1036178479Sjb			dt_cg_typecast(mnp->dn_membexpr, mnp, dlp, drp);
1037178479Sjb
1038178479Sjb			/*
1039178479Sjb			 * Ask CTF for the offset of the member so we can store
1040178479Sjb			 * to the appropriate offset.  This call has already
1041178479Sjb			 * been done once by the parser, so it should succeed.
1042178479Sjb			 */
1043178479Sjb			if (ctf_member_info(dxp->dx_dst_ctfp, dxp->dx_dst_base,
1044178479Sjb			    mnp->dn_membname, &ctm) == CTF_ERR) {
1045178479Sjb				yypcb->pcb_hdl->dt_ctferr =
1046178479Sjb				    ctf_errno(dxp->dx_dst_ctfp);
1047178479Sjb				longjmp(yypcb->pcb_jmpbuf, EDT_CTF);
1048178479Sjb			}
1049178479Sjb
1050178479Sjb			/*
1051178479Sjb			 * If the destination member is at offset 0, store the
1052178479Sjb			 * result directly to r1 (the scratch buffer address).
1053178479Sjb			 * Otherwise allocate another temporary for the offset
1054178479Sjb			 * and add r1 to it before storing the result.
1055178479Sjb			 */
1056178479Sjb			if (ctm.ctm_offset != 0) {
1057178479Sjb				if ((r2 = dt_regset_alloc(drp)) == -1)
1058178479Sjb					longjmp(yypcb->pcb_jmpbuf, EDT_NOREG);
1059178479Sjb
1060178479Sjb				/*
1061178479Sjb				 * Add the member offset rounded down to the
1062178479Sjb				 * nearest byte.  If the offset was not aligned
1063178479Sjb				 * on a byte boundary, this member is a bit-
1064178479Sjb				 * field and dt_cg_store() will handle masking.
1065178479Sjb				 */
1066178479Sjb				dt_cg_setx(dlp, r2, ctm.ctm_offset / NBBY);
1067178479Sjb				instr = DIF_INSTR_FMT(DIF_OP_ADD, r1, r2, r2);
1068178479Sjb				dt_irlist_append(dlp,
1069178479Sjb				    dt_cg_node_alloc(DT_LBL_NONE, instr));
1070178479Sjb
1071178479Sjb				dt_node_type_propagate(mnp, &dn);
1072178479Sjb				dn.dn_right->dn_string = mnp->dn_membname;
1073178479Sjb				dn.dn_reg = r2;
1074178479Sjb
1075178479Sjb				dt_cg_store(mnp, dlp, drp, &dn);
1076178479Sjb				dt_regset_free(drp, r2);
1077178479Sjb
1078178479Sjb			} else {
1079178479Sjb				dt_node_type_propagate(mnp, &dn);
1080178479Sjb				dn.dn_right->dn_string = mnp->dn_membname;
1081178479Sjb				dn.dn_reg = r1;
1082178479Sjb
1083178479Sjb				dt_cg_store(mnp, dlp, drp, &dn);
1084178479Sjb			}
1085178479Sjb
1086178479Sjb			dt_regset_free(drp, mnp->dn_reg);
1087178479Sjb		}
1088178479Sjb
1089178479Sjb		dxp->dx_ident->di_flags &= ~DT_IDFLG_CGREG;
1090178479Sjb		dxp->dx_ident->di_id = 0;
1091178479Sjb
1092178479Sjb		if (dnp->dn_right->dn_reg != -1)
1093178479Sjb			dt_regset_free(drp, dnp->dn_right->dn_reg);
1094178479Sjb
1095178479Sjb		assert(dnp->dn_reg == dnp->dn_right->dn_reg);
1096178479Sjb		dnp->dn_reg = r1;
1097178479Sjb	}
1098178479Sjb
1099178479Sjb	/*
1100178479Sjb	 * If we are storing to a variable, generate an stv instruction from
1101178479Sjb	 * the variable specified by the identifier.  If we are storing to a
1102178479Sjb	 * memory address, generate code again for the left-hand side using
1103178479Sjb	 * DT_NF_REF to get the address, and then generate a store to it.
1104178479Sjb	 * In both paths, we assume dnp->dn_reg already has the new value.
1105178479Sjb	 */
1106178479Sjb	if (dnp->dn_left->dn_kind == DT_NODE_VAR) {
1107178479Sjb		idp = dt_ident_resolve(dnp->dn_left->dn_ident);
1108178479Sjb
1109178479Sjb		if (idp->di_kind == DT_IDENT_ARRAY)
1110178479Sjb			dt_cg_arglist(idp, dnp->dn_left->dn_args, dlp, drp);
1111178479Sjb
1112178479Sjb		idp->di_flags |= DT_IDFLG_DIFW;
1113178479Sjb		instr = DIF_INSTR_STV(dt_cg_stvar(idp),
1114178479Sjb		    idp->di_id, dnp->dn_reg);
1115178479Sjb		dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
1116178479Sjb	} else {
1117178479Sjb		uint_t rbit = dnp->dn_left->dn_flags & DT_NF_REF;
1118178479Sjb
1119178479Sjb		assert(dnp->dn_left->dn_flags & DT_NF_WRITABLE);
1120178479Sjb		assert(dnp->dn_left->dn_flags & DT_NF_LVALUE);
1121178479Sjb
1122178479Sjb		dnp->dn_left->dn_flags |= DT_NF_REF; /* force pass-by-ref */
1123178479Sjb
1124178479Sjb		dt_cg_node(dnp->dn_left, dlp, drp);
1125178479Sjb		dt_cg_store(dnp, dlp, drp, dnp->dn_left);
1126178479Sjb		dt_regset_free(drp, dnp->dn_left->dn_reg);
1127178479Sjb
1128178479Sjb		dnp->dn_left->dn_flags &= ~DT_NF_REF;
1129178479Sjb		dnp->dn_left->dn_flags |= rbit;
1130178479Sjb	}
1131178479Sjb}
1132178479Sjb
1133178479Sjbstatic void
1134178479Sjbdt_cg_assoc_op(dt_node_t *dnp, dt_irlist_t *dlp, dt_regset_t *drp)
1135178479Sjb{
1136178479Sjb	dif_instr_t instr;
1137178479Sjb	uint_t op;
1138178479Sjb
1139178479Sjb	assert(dnp->dn_kind == DT_NODE_VAR);
1140178479Sjb	assert(!(dnp->dn_ident->di_flags & DT_IDFLG_LOCAL));
1141178479Sjb	assert(dnp->dn_args != NULL);
1142178479Sjb
1143178479Sjb	dt_cg_arglist(dnp->dn_ident, dnp->dn_args, dlp, drp);
1144178479Sjb
1145178479Sjb	if ((dnp->dn_reg = dt_regset_alloc(drp)) == -1)
1146178479Sjb		longjmp(yypcb->pcb_jmpbuf, EDT_NOREG);
1147178479Sjb
1148178479Sjb	if (dnp->dn_ident->di_flags & DT_IDFLG_TLS)
1149178479Sjb		op = DIF_OP_LDTAA;
1150178479Sjb	else
1151178479Sjb		op = DIF_OP_LDGAA;
1152178479Sjb
1153178479Sjb	dnp->dn_ident->di_flags |= DT_IDFLG_DIFR;
1154178479Sjb	instr = DIF_INSTR_LDV(op, dnp->dn_ident->di_id, dnp->dn_reg);
1155178479Sjb	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
1156178479Sjb
1157178479Sjb	/*
1158178479Sjb	 * If the associative array is a pass-by-reference type, then we are
1159178479Sjb	 * loading its value as a pointer to either load or store through it.
1160178479Sjb	 * The array element in question may not have been faulted in yet, in
1161178479Sjb	 * which case DIF_OP_LD*AA will return zero.  We append an epilogue
1162178479Sjb	 * of instructions similar to the following:
1163178479Sjb	 *
1164178479Sjb	 *	  ld?aa	 id, %r1	! base ld?aa instruction above
1165178479Sjb	 *	  tst	 %r1		! start of epilogue
1166178479Sjb	 *   +--- bne	 label
1167178479Sjb	 *   |    setx	 size, %r1
1168178479Sjb	 *   |    allocs %r1, %r1
1169178479Sjb	 *   |    st?aa	 id, %r1
1170178479Sjb	 *   |    ld?aa	 id, %r1
1171178479Sjb	 *   v
1172178479Sjb	 * label: < rest of code >
1173178479Sjb	 *
1174178479Sjb	 * The idea is that we allocs a zero-filled chunk of scratch space and
1175178479Sjb	 * do a DIF_OP_ST*AA to fault in and initialize the array element, and
1176178479Sjb	 * then reload it to get the faulted-in address of the new variable
1177178479Sjb	 * storage.  This isn't cheap, but pass-by-ref associative array values
1178178479Sjb	 * are (thus far) uncommon and the allocs cost only occurs once.  If
1179178479Sjb	 * this path becomes important to DTrace users, we can improve things
1180178479Sjb	 * by adding a new DIF opcode to fault in associative array elements.
1181178479Sjb	 */
1182178479Sjb	if (dnp->dn_flags & DT_NF_REF) {
1183178479Sjb		uint_t stvop = op == DIF_OP_LDTAA ? DIF_OP_STTAA : DIF_OP_STGAA;
1184178479Sjb		uint_t label = dt_irlist_label(dlp);
1185178479Sjb
1186178479Sjb		instr = DIF_INSTR_TST(dnp->dn_reg);
1187178479Sjb		dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
1188178479Sjb
1189178479Sjb		instr = DIF_INSTR_BRANCH(DIF_OP_BNE, label);
1190178479Sjb		dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
1191178479Sjb
1192178479Sjb		dt_cg_setx(dlp, dnp->dn_reg, dt_node_type_size(dnp));
1193178479Sjb		instr = DIF_INSTR_ALLOCS(dnp->dn_reg, dnp->dn_reg);
1194178479Sjb		dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
1195178479Sjb
1196178479Sjb		dnp->dn_ident->di_flags |= DT_IDFLG_DIFW;
1197178479Sjb		instr = DIF_INSTR_STV(stvop, dnp->dn_ident->di_id, dnp->dn_reg);
1198178479Sjb		dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
1199178479Sjb
1200178479Sjb		instr = DIF_INSTR_LDV(op, dnp->dn_ident->di_id, dnp->dn_reg);
1201178479Sjb		dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
1202178479Sjb
1203178479Sjb		dt_irlist_append(dlp, dt_cg_node_alloc(label, DIF_INSTR_NOP));
1204178479Sjb	}
1205178479Sjb}
1206178479Sjb
1207178479Sjbstatic void
1208178479Sjbdt_cg_array_op(dt_node_t *dnp, dt_irlist_t *dlp, dt_regset_t *drp)
1209178479Sjb{
1210178479Sjb	dt_probe_t *prp = yypcb->pcb_probe;
1211178479Sjb	uintmax_t saved = dnp->dn_args->dn_value;
1212178479Sjb	dt_ident_t *idp = dnp->dn_ident;
1213178479Sjb
1214178479Sjb	dif_instr_t instr;
1215178479Sjb	uint_t op;
1216178479Sjb	size_t size;
1217178479Sjb	int reg, n;
1218178479Sjb
1219178479Sjb	assert(dnp->dn_kind == DT_NODE_VAR);
1220178479Sjb	assert(!(idp->di_flags & DT_IDFLG_LOCAL));
1221178479Sjb
1222178479Sjb	assert(dnp->dn_args->dn_kind == DT_NODE_INT);
1223178479Sjb	assert(dnp->dn_args->dn_list == NULL);
1224178479Sjb
1225178479Sjb	/*
1226178479Sjb	 * If this is a reference in the args[] array, temporarily modify the
1227178479Sjb	 * array index according to the static argument mapping (if any),
1228178479Sjb	 * unless the argument reference is provided by a dynamic translator.
1229178479Sjb	 * If we're using a dynamic translator for args[], then just set dn_reg
1230178479Sjb	 * to an invalid reg and return: DIF_OP_XLARG will fetch the arg later.
1231178479Sjb	 */
1232178479Sjb	if (idp->di_id == DIF_VAR_ARGS) {
1233178479Sjb		if ((idp->di_kind == DT_IDENT_XLPTR ||
1234178479Sjb		    idp->di_kind == DT_IDENT_XLSOU) &&
1235178479Sjb		    dt_xlator_dynamic(idp->di_data)) {
1236178479Sjb			dnp->dn_reg = -1;
1237178479Sjb			return;
1238178479Sjb		}
1239178479Sjb		dnp->dn_args->dn_value = prp->pr_mapping[saved];
1240178479Sjb	}
1241178479Sjb
1242178479Sjb	dt_cg_node(dnp->dn_args, dlp, drp);
1243178479Sjb	dnp->dn_args->dn_value = saved;
1244178479Sjb
1245178479Sjb	dnp->dn_reg = dnp->dn_args->dn_reg;
1246178479Sjb
1247178479Sjb	if (idp->di_flags & DT_IDFLG_TLS)
1248178479Sjb		op = DIF_OP_LDTA;
1249178479Sjb	else
1250178479Sjb		op = DIF_OP_LDGA;
1251178479Sjb
1252178479Sjb	idp->di_flags |= DT_IDFLG_DIFR;
1253178479Sjb
1254178479Sjb	instr = DIF_INSTR_LDA(op, idp->di_id,
1255178479Sjb	    dnp->dn_args->dn_reg, dnp->dn_reg);
1256178479Sjb
1257178479Sjb	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
1258178479Sjb
1259178479Sjb	/*
1260178479Sjb	 * If this is a reference to the args[] array, we need to take the
1261178479Sjb	 * additional step of explicitly eliminating any bits larger than the
1262178479Sjb	 * type size: the DIF interpreter in the kernel will always give us
1263178479Sjb	 * the raw (64-bit) argument value, and any bits larger than the type
1264178479Sjb	 * size may be junk.  As a practical matter, this arises only on 64-bit
1265178479Sjb	 * architectures and only when the argument index is larger than the
1266178479Sjb	 * number of arguments passed directly to DTrace: if a 8-, 16- or
1267178479Sjb	 * 32-bit argument must be retrieved from the stack, it is possible
1268178479Sjb	 * (and it some cases, likely) that the upper bits will be garbage.
1269178479Sjb	 */
1270178479Sjb	if (idp->di_id != DIF_VAR_ARGS || !dt_node_is_scalar(dnp))
1271178479Sjb		return;
1272178479Sjb
1273178479Sjb	if ((size = dt_node_type_size(dnp)) == sizeof (uint64_t))
1274178479Sjb		return;
1275178479Sjb
1276178479Sjb	if ((reg = dt_regset_alloc(drp)) == -1)
1277178479Sjb		longjmp(yypcb->pcb_jmpbuf, EDT_NOREG);
1278178479Sjb
1279178479Sjb	assert(size < sizeof (uint64_t));
1280178479Sjb	n = sizeof (uint64_t) * NBBY - size * NBBY;
1281178479Sjb
1282178479Sjb	dt_cg_setx(dlp, reg, n);
1283178479Sjb
1284178479Sjb	instr = DIF_INSTR_FMT(DIF_OP_SLL, dnp->dn_reg, reg, dnp->dn_reg);
1285178479Sjb	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
1286178479Sjb
1287178479Sjb	instr = DIF_INSTR_FMT((dnp->dn_flags & DT_NF_SIGNED) ?
1288178479Sjb	    DIF_OP_SRA : DIF_OP_SRL, dnp->dn_reg, reg, dnp->dn_reg);
1289178479Sjb
1290178479Sjb	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
1291178479Sjb	dt_regset_free(drp, reg);
1292178479Sjb}
1293178479Sjb
1294178479Sjb/*
1295178479Sjb * Generate code for an inlined variable reference.  Inlines can be used to
1296178479Sjb * define either scalar or associative array substitutions.  For scalars, we
1297178479Sjb * simply generate code for the parse tree saved in the identifier's din_root,
1298178479Sjb * and then cast the resulting expression to the inline's declaration type.
1299178479Sjb * For arrays, we take the input parameter subtrees from dnp->dn_args and
1300178479Sjb * temporarily store them in the din_root of each din_argv[i] identifier,
1301178479Sjb * which are themselves inlines and were set up for us by the parser.  The
1302178479Sjb * result is that any reference to the inlined parameter inside the top-level
1303178479Sjb * din_root will turn into a recursive call to dt_cg_inline() for a scalar
1304178479Sjb * inline whose din_root will refer to the subtree pointed to by the argument.
1305178479Sjb */
1306178479Sjbstatic void
1307178479Sjbdt_cg_inline(dt_node_t *dnp, dt_irlist_t *dlp, dt_regset_t *drp)
1308178479Sjb{
1309178479Sjb	dt_ident_t *idp = dnp->dn_ident;
1310178479Sjb	dt_idnode_t *inp = idp->di_iarg;
1311178479Sjb
1312178479Sjb	dt_idnode_t *pinp;
1313178479Sjb	dt_node_t *pnp;
1314178479Sjb	int i;
1315178479Sjb
1316178479Sjb	assert(idp->di_flags & DT_IDFLG_INLINE);
1317178479Sjb	assert(idp->di_ops == &dt_idops_inline);
1318178479Sjb
1319178479Sjb	if (idp->di_kind == DT_IDENT_ARRAY) {
1320178479Sjb		for (i = 0, pnp = dnp->dn_args;
1321178479Sjb		    pnp != NULL; pnp = pnp->dn_list, i++) {
1322178479Sjb			if (inp->din_argv[i] != NULL) {
1323178479Sjb				pinp = inp->din_argv[i]->di_iarg;
1324178479Sjb				pinp->din_root = pnp;
1325178479Sjb			}
1326178479Sjb		}
1327178479Sjb	}
1328178479Sjb
1329178479Sjb	dt_cg_node(inp->din_root, dlp, drp);
1330178479Sjb	dnp->dn_reg = inp->din_root->dn_reg;
1331178479Sjb	dt_cg_typecast(inp->din_root, dnp, dlp, drp);
1332178479Sjb
1333178479Sjb	if (idp->di_kind == DT_IDENT_ARRAY) {
1334178479Sjb		for (i = 0; i < inp->din_argc; i++) {
1335178479Sjb			pinp = inp->din_argv[i]->di_iarg;
1336178479Sjb			pinp->din_root = NULL;
1337178479Sjb		}
1338178479Sjb	}
1339178479Sjb}
1340178479Sjb
1341178479Sjbstatic void
1342178479Sjbdt_cg_func_typeref(dtrace_hdl_t *dtp, dt_node_t *dnp)
1343178479Sjb{
1344178479Sjb	dtrace_typeinfo_t dtt;
1345178479Sjb	dt_node_t *addr = dnp->dn_args;
1346178479Sjb	dt_node_t *nelm = addr->dn_list;
1347178479Sjb	dt_node_t *strp = nelm->dn_list;
1348178479Sjb	dt_node_t *typs = strp->dn_list;
1349178479Sjb	char buf[DT_TYPE_NAMELEN];
1350178479Sjb	char *p;
1351178479Sjb
1352178479Sjb	ctf_type_name(addr->dn_ctfp, addr->dn_type, buf, sizeof (buf));
1353178479Sjb
1354178479Sjb	/*
1355178479Sjb	 * XXX Hack alert! XXX
1356178479Sjb	 * The prototype has two dummy args that we munge to represent
1357178479Sjb	 * the type string and the type size.
1358178479Sjb	 *
1359178479Sjb	 * Yes, I hear your grumble, but it works for now. We'll come
1360178479Sjb	 * up with a more elegant implementation later. :-)
1361178479Sjb	 */
1362178479Sjb	free(strp->dn_string);
1363178479Sjb
1364178479Sjb	if ((p = strchr(buf, '*')) != NULL)
1365178479Sjb		*p = '\0';
1366178479Sjb
1367178479Sjb	strp->dn_string = strdup(buf);
1368178479Sjb
1369178479Sjb	if (dtrace_lookup_by_type(dtp,  DTRACE_OBJ_EVERY, buf, &dtt) < 0)
1370178479Sjb		return;
1371178479Sjb
1372178479Sjb	typs->dn_value = ctf_type_size(dtt.dtt_ctfp, dtt.dtt_type);
1373178479Sjb}
1374178479Sjb
1375178479Sjbstatic void
1376178479Sjbdt_cg_node(dt_node_t *dnp, dt_irlist_t *dlp, dt_regset_t *drp)
1377178479Sjb{
1378178479Sjb	ctf_file_t *ctfp = dnp->dn_ctfp;
1379178479Sjb	ctf_file_t *octfp;
1380178479Sjb	ctf_membinfo_t m;
1381178479Sjb	ctf_id_t type;
1382178479Sjb
1383178479Sjb	dif_instr_t instr;
1384178479Sjb	dt_ident_t *idp;
1385178479Sjb	ssize_t stroff;
1386178479Sjb	uint_t op;
1387178479Sjb	int reg;
1388178479Sjb
1389178479Sjb	switch (dnp->dn_op) {
1390178479Sjb	case DT_TOK_COMMA:
1391178479Sjb		dt_cg_node(dnp->dn_left, dlp, drp);
1392178479Sjb		dt_regset_free(drp, dnp->dn_left->dn_reg);
1393178479Sjb		dt_cg_node(dnp->dn_right, dlp, drp);
1394178479Sjb		dnp->dn_reg = dnp->dn_right->dn_reg;
1395178479Sjb		break;
1396178479Sjb
1397178479Sjb	case DT_TOK_ASGN:
1398178479Sjb		dt_cg_node(dnp->dn_right, dlp, drp);
1399178479Sjb		dnp->dn_reg = dnp->dn_right->dn_reg;
1400178479Sjb		dt_cg_asgn_op(dnp, dlp, drp);
1401178479Sjb		break;
1402178479Sjb
1403178479Sjb	case DT_TOK_ADD_EQ:
1404178479Sjb		dt_cg_arithmetic_op(dnp, dlp, drp, DIF_OP_ADD);
1405178479Sjb		dt_cg_asgn_op(dnp, dlp, drp);
1406178479Sjb		break;
1407178479Sjb
1408178479Sjb	case DT_TOK_SUB_EQ:
1409178479Sjb		dt_cg_arithmetic_op(dnp, dlp, drp, DIF_OP_SUB);
1410178479Sjb		dt_cg_asgn_op(dnp, dlp, drp);
1411178479Sjb		break;
1412178479Sjb
1413178479Sjb	case DT_TOK_MUL_EQ:
1414178479Sjb		dt_cg_arithmetic_op(dnp, dlp, drp, DIF_OP_MUL);
1415178479Sjb		dt_cg_asgn_op(dnp, dlp, drp);
1416178479Sjb		break;
1417178479Sjb
1418178479Sjb	case DT_TOK_DIV_EQ:
1419178479Sjb		dt_cg_arithmetic_op(dnp, dlp, drp,
1420178479Sjb		    (dnp->dn_flags & DT_NF_SIGNED) ? DIF_OP_SDIV : DIF_OP_UDIV);
1421178479Sjb		dt_cg_asgn_op(dnp, dlp, drp);
1422178479Sjb		break;
1423178479Sjb
1424178479Sjb	case DT_TOK_MOD_EQ:
1425178479Sjb		dt_cg_arithmetic_op(dnp, dlp, drp,
1426178479Sjb		    (dnp->dn_flags & DT_NF_SIGNED) ? DIF_OP_SREM : DIF_OP_UREM);
1427178479Sjb		dt_cg_asgn_op(dnp, dlp, drp);
1428178479Sjb		break;
1429178479Sjb
1430178479Sjb	case DT_TOK_AND_EQ:
1431178479Sjb		dt_cg_arithmetic_op(dnp, dlp, drp, DIF_OP_AND);
1432178479Sjb		dt_cg_asgn_op(dnp, dlp, drp);
1433178479Sjb		break;
1434178479Sjb
1435178479Sjb	case DT_TOK_XOR_EQ:
1436178479Sjb		dt_cg_arithmetic_op(dnp, dlp, drp, DIF_OP_XOR);
1437178479Sjb		dt_cg_asgn_op(dnp, dlp, drp);
1438178479Sjb		break;
1439178479Sjb
1440178479Sjb	case DT_TOK_OR_EQ:
1441178479Sjb		dt_cg_arithmetic_op(dnp, dlp, drp, DIF_OP_OR);
1442178479Sjb		dt_cg_asgn_op(dnp, dlp, drp);
1443178479Sjb		break;
1444178479Sjb
1445178479Sjb	case DT_TOK_LSH_EQ:
1446178479Sjb		dt_cg_arithmetic_op(dnp, dlp, drp, DIF_OP_SLL);
1447178479Sjb		dt_cg_asgn_op(dnp, dlp, drp);
1448178479Sjb		break;
1449178479Sjb
1450178479Sjb	case DT_TOK_RSH_EQ:
1451178479Sjb		dt_cg_arithmetic_op(dnp, dlp, drp,
1452178479Sjb		    (dnp->dn_flags & DT_NF_SIGNED) ? DIF_OP_SRA : DIF_OP_SRL);
1453178479Sjb		dt_cg_asgn_op(dnp, dlp, drp);
1454178479Sjb		break;
1455178479Sjb
1456178479Sjb	case DT_TOK_QUESTION:
1457178479Sjb		dt_cg_ternary_op(dnp, dlp, drp);
1458178479Sjb		break;
1459178479Sjb
1460178479Sjb	case DT_TOK_LOR:
1461178479Sjb		dt_cg_logical_or(dnp, dlp, drp);
1462178479Sjb		break;
1463178479Sjb
1464178479Sjb	case DT_TOK_LXOR:
1465178479Sjb		dt_cg_logical_xor(dnp, dlp, drp);
1466178479Sjb		break;
1467178479Sjb
1468178479Sjb	case DT_TOK_LAND:
1469178479Sjb		dt_cg_logical_and(dnp, dlp, drp);
1470178479Sjb		break;
1471178479Sjb
1472178479Sjb	case DT_TOK_BOR:
1473178479Sjb		dt_cg_arithmetic_op(dnp, dlp, drp, DIF_OP_OR);
1474178479Sjb		break;
1475178479Sjb
1476178479Sjb	case DT_TOK_XOR:
1477178479Sjb		dt_cg_arithmetic_op(dnp, dlp, drp, DIF_OP_XOR);
1478178479Sjb		break;
1479178479Sjb
1480178479Sjb	case DT_TOK_BAND:
1481178479Sjb		dt_cg_arithmetic_op(dnp, dlp, drp, DIF_OP_AND);
1482178479Sjb		break;
1483178479Sjb
1484178479Sjb	case DT_TOK_EQU:
1485178479Sjb		dt_cg_compare_op(dnp, dlp, drp, DIF_OP_BE);
1486178479Sjb		break;
1487178479Sjb
1488178479Sjb	case DT_TOK_NEQ:
1489178479Sjb		dt_cg_compare_op(dnp, dlp, drp, DIF_OP_BNE);
1490178479Sjb		break;
1491178479Sjb
1492178479Sjb	case DT_TOK_LT:
1493178479Sjb		dt_cg_compare_op(dnp, dlp, drp,
1494178479Sjb		    dt_cg_compare_signed(dnp) ? DIF_OP_BL : DIF_OP_BLU);
1495178479Sjb		break;
1496178479Sjb
1497178479Sjb	case DT_TOK_LE:
1498178479Sjb		dt_cg_compare_op(dnp, dlp, drp,
1499178479Sjb		    dt_cg_compare_signed(dnp) ? DIF_OP_BLE : DIF_OP_BLEU);
1500178479Sjb		break;
1501178479Sjb
1502178479Sjb	case DT_TOK_GT:
1503178479Sjb		dt_cg_compare_op(dnp, dlp, drp,
1504178479Sjb		    dt_cg_compare_signed(dnp) ? DIF_OP_BG : DIF_OP_BGU);
1505178479Sjb		break;
1506178479Sjb
1507178479Sjb	case DT_TOK_GE:
1508178479Sjb		dt_cg_compare_op(dnp, dlp, drp,
1509178479Sjb		    dt_cg_compare_signed(dnp) ? DIF_OP_BGE : DIF_OP_BGEU);
1510178479Sjb		break;
1511178479Sjb
1512178479Sjb	case DT_TOK_LSH:
1513178479Sjb		dt_cg_arithmetic_op(dnp, dlp, drp, DIF_OP_SLL);
1514178479Sjb		break;
1515178479Sjb
1516178479Sjb	case DT_TOK_RSH:
1517178479Sjb		dt_cg_arithmetic_op(dnp, dlp, drp,
1518178479Sjb		    (dnp->dn_flags & DT_NF_SIGNED) ? DIF_OP_SRA : DIF_OP_SRL);
1519178479Sjb		break;
1520178479Sjb
1521178479Sjb	case DT_TOK_ADD:
1522178479Sjb		dt_cg_arithmetic_op(dnp, dlp, drp, DIF_OP_ADD);
1523178479Sjb		break;
1524178479Sjb
1525178479Sjb	case DT_TOK_SUB:
1526178479Sjb		dt_cg_arithmetic_op(dnp, dlp, drp, DIF_OP_SUB);
1527178479Sjb		break;
1528178479Sjb
1529178479Sjb	case DT_TOK_MUL:
1530178479Sjb		dt_cg_arithmetic_op(dnp, dlp, drp, DIF_OP_MUL);
1531178479Sjb		break;
1532178479Sjb
1533178479Sjb	case DT_TOK_DIV:
1534178479Sjb		dt_cg_arithmetic_op(dnp, dlp, drp,
1535178479Sjb		    (dnp->dn_flags & DT_NF_SIGNED) ? DIF_OP_SDIV : DIF_OP_UDIV);
1536178479Sjb		break;
1537178479Sjb
1538178479Sjb	case DT_TOK_MOD:
1539178479Sjb		dt_cg_arithmetic_op(dnp, dlp, drp,
1540178479Sjb		    (dnp->dn_flags & DT_NF_SIGNED) ? DIF_OP_SREM : DIF_OP_UREM);
1541178479Sjb		break;
1542178479Sjb
1543178479Sjb	case DT_TOK_LNEG:
1544178479Sjb		dt_cg_logical_neg(dnp, dlp, drp);
1545178479Sjb		break;
1546178479Sjb
1547178479Sjb	case DT_TOK_BNEG:
1548178479Sjb		dt_cg_node(dnp->dn_child, dlp, drp);
1549178479Sjb		dnp->dn_reg = dnp->dn_child->dn_reg;
1550178479Sjb		instr = DIF_INSTR_NOT(dnp->dn_reg, dnp->dn_reg);
1551178479Sjb		dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
1552178479Sjb		break;
1553178479Sjb
1554178479Sjb	case DT_TOK_PREINC:
1555178479Sjb		dt_cg_prearith_op(dnp, dlp, drp, DIF_OP_ADD);
1556178479Sjb		break;
1557178479Sjb
1558178479Sjb	case DT_TOK_POSTINC:
1559178479Sjb		dt_cg_postarith_op(dnp, dlp, drp, DIF_OP_ADD);
1560178479Sjb		break;
1561178479Sjb
1562178479Sjb	case DT_TOK_PREDEC:
1563178479Sjb		dt_cg_prearith_op(dnp, dlp, drp, DIF_OP_SUB);
1564178479Sjb		break;
1565178479Sjb
1566178479Sjb	case DT_TOK_POSTDEC:
1567178479Sjb		dt_cg_postarith_op(dnp, dlp, drp, DIF_OP_SUB);
1568178479Sjb		break;
1569178479Sjb
1570178479Sjb	case DT_TOK_IPOS:
1571178479Sjb		dt_cg_node(dnp->dn_child, dlp, drp);
1572178479Sjb		dnp->dn_reg = dnp->dn_child->dn_reg;
1573178479Sjb		break;
1574178479Sjb
1575178479Sjb	case DT_TOK_INEG:
1576178479Sjb		dt_cg_node(dnp->dn_child, dlp, drp);
1577178479Sjb		dnp->dn_reg = dnp->dn_child->dn_reg;
1578178479Sjb
1579178479Sjb		instr = DIF_INSTR_FMT(DIF_OP_SUB, DIF_REG_R0,
1580178479Sjb		    dnp->dn_reg, dnp->dn_reg);
1581178479Sjb
1582178479Sjb		dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
1583178479Sjb		break;
1584178479Sjb
1585178479Sjb	case DT_TOK_DEREF:
1586178479Sjb		dt_cg_node(dnp->dn_child, dlp, drp);
1587178479Sjb		dnp->dn_reg = dnp->dn_child->dn_reg;
1588178479Sjb
1589178479Sjb		if (!(dnp->dn_flags & DT_NF_REF)) {
1590178479Sjb			uint_t ubit = dnp->dn_flags & DT_NF_USERLAND;
1591178479Sjb
1592178479Sjb			/*
1593178479Sjb			 * Save and restore DT_NF_USERLAND across dt_cg_load():
1594178479Sjb			 * we need the sign bit from dnp and the user bit from
1595178479Sjb			 * dnp->dn_child in order to get the proper opcode.
1596178479Sjb			 */
1597178479Sjb			dnp->dn_flags |=
1598178479Sjb			    (dnp->dn_child->dn_flags & DT_NF_USERLAND);
1599178479Sjb
1600178479Sjb			instr = DIF_INSTR_LOAD(dt_cg_load(dnp, ctfp,
1601178479Sjb			    dnp->dn_type), dnp->dn_reg, dnp->dn_reg);
1602178479Sjb
1603178479Sjb			dnp->dn_flags &= ~DT_NF_USERLAND;
1604178479Sjb			dnp->dn_flags |= ubit;
1605178479Sjb
1606178479Sjb			dt_irlist_append(dlp,
1607178479Sjb			    dt_cg_node_alloc(DT_LBL_NONE, instr));
1608178479Sjb		}
1609178479Sjb		break;
1610178479Sjb
1611178479Sjb	case DT_TOK_ADDROF: {
1612178479Sjb		uint_t rbit = dnp->dn_child->dn_flags & DT_NF_REF;
1613178479Sjb
1614178479Sjb		dnp->dn_child->dn_flags |= DT_NF_REF; /* force pass-by-ref */
1615178479Sjb		dt_cg_node(dnp->dn_child, dlp, drp);
1616178479Sjb		dnp->dn_reg = dnp->dn_child->dn_reg;
1617178479Sjb
1618178479Sjb		dnp->dn_child->dn_flags &= ~DT_NF_REF;
1619178479Sjb		dnp->dn_child->dn_flags |= rbit;
1620178479Sjb		break;
1621178479Sjb	}
1622178479Sjb
1623178479Sjb	case DT_TOK_SIZEOF: {
1624178479Sjb		size_t size = dt_node_sizeof(dnp->dn_child);
1625178479Sjb
1626178479Sjb		if ((dnp->dn_reg = dt_regset_alloc(drp)) == -1)
1627178479Sjb			longjmp(yypcb->pcb_jmpbuf, EDT_NOREG);
1628178479Sjb
1629178479Sjb		assert(size != 0);
1630178479Sjb		dt_cg_setx(dlp, dnp->dn_reg, size);
1631178479Sjb		break;
1632178479Sjb	}
1633178479Sjb
1634178479Sjb	case DT_TOK_STRINGOF:
1635178479Sjb		dt_cg_node(dnp->dn_child, dlp, drp);
1636178479Sjb		dnp->dn_reg = dnp->dn_child->dn_reg;
1637178479Sjb		break;
1638178479Sjb
1639178479Sjb	case DT_TOK_XLATE:
1640178479Sjb		/*
1641178479Sjb		 * An xlate operator appears in either an XLATOR, indicating a
1642178479Sjb		 * reference to a dynamic translator, or an OP2, indicating
1643178479Sjb		 * use of the xlate operator in the user's program.  For the
1644178479Sjb		 * dynamic case, generate an xlate opcode with a reference to
1645178479Sjb		 * the corresponding member, pre-computed for us in dn_members.
1646178479Sjb		 */
1647178479Sjb		if (dnp->dn_kind == DT_NODE_XLATOR) {
1648178479Sjb			dt_xlator_t *dxp = dnp->dn_xlator;
1649178479Sjb
1650178479Sjb			assert(dxp->dx_ident->di_flags & DT_IDFLG_CGREG);
1651178479Sjb			assert(dxp->dx_ident->di_id != 0);
1652178479Sjb
1653178479Sjb			if ((dnp->dn_reg = dt_regset_alloc(drp)) == -1)
1654178479Sjb				longjmp(yypcb->pcb_jmpbuf, EDT_NOREG);
1655178479Sjb
1656178479Sjb			if (dxp->dx_arg == -1) {
1657178479Sjb				instr = DIF_INSTR_MOV(
1658178479Sjb				    dxp->dx_ident->di_id, dnp->dn_reg);
1659178479Sjb				dt_irlist_append(dlp,
1660178479Sjb				    dt_cg_node_alloc(DT_LBL_NONE, instr));
1661178479Sjb				op = DIF_OP_XLATE;
1662178479Sjb			} else
1663178479Sjb				op = DIF_OP_XLARG;
1664178479Sjb
1665178479Sjb			instr = DIF_INSTR_XLATE(op, 0, dnp->dn_reg);
1666178479Sjb			dt_irlist_append(dlp,
1667178479Sjb			    dt_cg_node_alloc(DT_LBL_NONE, instr));
1668178479Sjb
1669178479Sjb			dlp->dl_last->di_extern = dnp->dn_xmember;
1670178479Sjb			break;
1671178479Sjb		}
1672178479Sjb
1673178479Sjb		assert(dnp->dn_kind == DT_NODE_OP2);
1674178479Sjb		dt_cg_node(dnp->dn_right, dlp, drp);
1675178479Sjb		dnp->dn_reg = dnp->dn_right->dn_reg;
1676178479Sjb		break;
1677178479Sjb
1678178479Sjb	case DT_TOK_LPAR:
1679178479Sjb		dt_cg_node(dnp->dn_right, dlp, drp);
1680178479Sjb		dnp->dn_reg = dnp->dn_right->dn_reg;
1681178479Sjb		dt_cg_typecast(dnp->dn_right, dnp, dlp, drp);
1682178479Sjb		break;
1683178479Sjb
1684178479Sjb	case DT_TOK_PTR:
1685178479Sjb	case DT_TOK_DOT:
1686178479Sjb		assert(dnp->dn_right->dn_kind == DT_NODE_IDENT);
1687178479Sjb		dt_cg_node(dnp->dn_left, dlp, drp);
1688178479Sjb
1689178479Sjb		/*
1690178479Sjb		 * If the left-hand side of PTR or DOT is a dynamic variable,
1691178479Sjb		 * we expect it to be the output of a D translator.   In this
1692178479Sjb		 * case, we look up the parse tree corresponding to the member
1693178479Sjb		 * that is being accessed and run the code generator over it.
1694178479Sjb		 * We then cast the result as if by the assignment operator.
1695178479Sjb		 */
1696178479Sjb		if ((idp = dt_node_resolve(
1697178479Sjb		    dnp->dn_left, DT_IDENT_XLSOU)) != NULL ||
1698178479Sjb		    (idp = dt_node_resolve(
1699178479Sjb		    dnp->dn_left, DT_IDENT_XLPTR)) != NULL) {
1700178479Sjb
1701178479Sjb			dt_xlator_t *dxp;
1702178479Sjb			dt_node_t *mnp;
1703178479Sjb
1704178479Sjb			dxp = idp->di_data;
1705178479Sjb			mnp = dt_xlator_member(dxp, dnp->dn_right->dn_string);
1706178479Sjb			assert(mnp != NULL);
1707178479Sjb
1708178479Sjb			dxp->dx_ident->di_flags |= DT_IDFLG_CGREG;
1709178479Sjb			dxp->dx_ident->di_id = dnp->dn_left->dn_reg;
1710178479Sjb
1711178479Sjb			dt_cg_node(mnp->dn_membexpr, dlp, drp);
1712178479Sjb			dnp->dn_reg = mnp->dn_membexpr->dn_reg;
1713178479Sjb			dt_cg_typecast(mnp->dn_membexpr, dnp, dlp, drp);
1714178479Sjb
1715178479Sjb			dxp->dx_ident->di_flags &= ~DT_IDFLG_CGREG;
1716178479Sjb			dxp->dx_ident->di_id = 0;
1717178479Sjb
1718178479Sjb			if (dnp->dn_left->dn_reg != -1)
1719178479Sjb				dt_regset_free(drp, dnp->dn_left->dn_reg);
1720178479Sjb			break;
1721178479Sjb		}
1722178479Sjb
1723178479Sjb		ctfp = dnp->dn_left->dn_ctfp;
1724178479Sjb		type = ctf_type_resolve(ctfp, dnp->dn_left->dn_type);
1725178479Sjb
1726178479Sjb		if (dnp->dn_op == DT_TOK_PTR) {
1727178479Sjb			type = ctf_type_reference(ctfp, type);
1728178479Sjb			type = ctf_type_resolve(ctfp, type);
1729178479Sjb		}
1730178479Sjb
1731178479Sjb		if ((ctfp = dt_cg_membinfo(octfp = ctfp, type,
1732178479Sjb		    dnp->dn_right->dn_string, &m)) == NULL) {
1733178479Sjb			yypcb->pcb_hdl->dt_ctferr = ctf_errno(octfp);
1734178479Sjb			longjmp(yypcb->pcb_jmpbuf, EDT_CTF);
1735178479Sjb		}
1736178479Sjb
1737178479Sjb		if (m.ctm_offset != 0) {
1738178479Sjb			if ((reg = dt_regset_alloc(drp)) == -1)
1739178479Sjb				longjmp(yypcb->pcb_jmpbuf, EDT_NOREG);
1740178479Sjb
1741178479Sjb			/*
1742178479Sjb			 * If the offset is not aligned on a byte boundary, it
1743178479Sjb			 * is a bit-field member and we will extract the value
1744178479Sjb			 * bits below after we generate the appropriate load.
1745178479Sjb			 */
1746178479Sjb			dt_cg_setx(dlp, reg, m.ctm_offset / NBBY);
1747178479Sjb
1748178479Sjb			instr = DIF_INSTR_FMT(DIF_OP_ADD,
1749178479Sjb			    dnp->dn_left->dn_reg, reg, dnp->dn_left->dn_reg);
1750178479Sjb
1751178479Sjb			dt_irlist_append(dlp,
1752178479Sjb			    dt_cg_node_alloc(DT_LBL_NONE, instr));
1753178479Sjb			dt_regset_free(drp, reg);
1754178479Sjb		}
1755178479Sjb
1756178479Sjb		if (!(dnp->dn_flags & DT_NF_REF)) {
1757178479Sjb			uint_t ubit = dnp->dn_flags & DT_NF_USERLAND;
1758178479Sjb
1759178479Sjb			/*
1760178479Sjb			 * Save and restore DT_NF_USERLAND across dt_cg_load():
1761178479Sjb			 * we need the sign bit from dnp and the user bit from
1762178479Sjb			 * dnp->dn_left in order to get the proper opcode.
1763178479Sjb			 */
1764178479Sjb			dnp->dn_flags |=
1765178479Sjb			    (dnp->dn_left->dn_flags & DT_NF_USERLAND);
1766178479Sjb
1767178479Sjb			instr = DIF_INSTR_LOAD(dt_cg_load(dnp,
1768178479Sjb			    ctfp, m.ctm_type), dnp->dn_left->dn_reg,
1769178479Sjb			    dnp->dn_left->dn_reg);
1770178479Sjb
1771178479Sjb			dnp->dn_flags &= ~DT_NF_USERLAND;
1772178479Sjb			dnp->dn_flags |= ubit;
1773178479Sjb
1774178479Sjb			dt_irlist_append(dlp,
1775178479Sjb			    dt_cg_node_alloc(DT_LBL_NONE, instr));
1776178479Sjb
1777178479Sjb			if (dnp->dn_flags & DT_NF_BITFIELD)
1778178479Sjb				dt_cg_field_get(dnp, dlp, drp, ctfp, &m);
1779178479Sjb		}
1780178479Sjb
1781178479Sjb		dnp->dn_reg = dnp->dn_left->dn_reg;
1782178479Sjb		break;
1783178479Sjb
1784178479Sjb	case DT_TOK_STRING:
1785178479Sjb		if ((dnp->dn_reg = dt_regset_alloc(drp)) == -1)
1786178479Sjb			longjmp(yypcb->pcb_jmpbuf, EDT_NOREG);
1787178479Sjb
1788178479Sjb		assert(dnp->dn_kind == DT_NODE_STRING);
1789178479Sjb		stroff = dt_strtab_insert(yypcb->pcb_strtab, dnp->dn_string);
1790178479Sjb
1791178479Sjb		if (stroff == -1L)
1792178479Sjb			longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
1793178479Sjb		if (stroff > DIF_STROFF_MAX)
1794178479Sjb			longjmp(yypcb->pcb_jmpbuf, EDT_STR2BIG);
1795178479Sjb
1796178479Sjb		instr = DIF_INSTR_SETS((ulong_t)stroff, dnp->dn_reg);
1797178479Sjb		dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
1798178479Sjb		break;
1799178479Sjb
1800178479Sjb	case DT_TOK_IDENT:
1801178479Sjb		/*
1802178479Sjb		 * If the specified identifier is a variable on which we have
1803178479Sjb		 * set the code generator register flag, then this variable
1804178479Sjb		 * has already had code generated for it and saved in di_id.
1805178479Sjb		 * Allocate a new register and copy the existing value to it.
1806178479Sjb		 */
1807178479Sjb		if (dnp->dn_kind == DT_NODE_VAR &&
1808178479Sjb		    (dnp->dn_ident->di_flags & DT_IDFLG_CGREG)) {
1809178479Sjb			if ((dnp->dn_reg = dt_regset_alloc(drp)) == -1)
1810178479Sjb				longjmp(yypcb->pcb_jmpbuf, EDT_NOREG);
1811178479Sjb			instr = DIF_INSTR_MOV(dnp->dn_ident->di_id,
1812178479Sjb			    dnp->dn_reg);
1813178479Sjb			dt_irlist_append(dlp,
1814178479Sjb			    dt_cg_node_alloc(DT_LBL_NONE, instr));
1815178479Sjb			break;
1816178479Sjb		}
1817178479Sjb
1818178479Sjb		/*
1819178479Sjb		 * Identifiers can represent function calls, variable refs, or
1820178479Sjb		 * symbols.  First we check for inlined variables, and handle
1821178479Sjb		 * them by generating code for the inline parse tree.
1822178479Sjb		 */
1823178479Sjb		if (dnp->dn_kind == DT_NODE_VAR &&
1824178479Sjb		    (dnp->dn_ident->di_flags & DT_IDFLG_INLINE)) {
1825178479Sjb			dt_cg_inline(dnp, dlp, drp);
1826178479Sjb			break;
1827178479Sjb		}
1828178479Sjb
1829178479Sjb		switch (dnp->dn_kind) {
1830178479Sjb		case DT_NODE_FUNC: {
1831178479Sjb			dtrace_hdl_t *dtp = yypcb->pcb_hdl;
1832178479Sjb
1833178479Sjb			if ((idp = dnp->dn_ident)->di_kind != DT_IDENT_FUNC) {
1834178479Sjb				dnerror(dnp, D_CG_EXPR, "%s %s( ) may not be "
1835178479Sjb				    "called from a D expression (D program "
1836178479Sjb				    "context required)\n",
1837178479Sjb				    dt_idkind_name(idp->di_kind), idp->di_name);
1838178479Sjb			}
1839178479Sjb
1840178479Sjb			switch (idp->di_id) {
1841178479Sjb			case DIF_SUBR_TYPEREF:
1842178479Sjb				dt_cg_func_typeref(dtp, dnp);
1843178479Sjb				break;
1844178479Sjb
1845178479Sjb			default:
1846178479Sjb				break;
1847178479Sjb			}
1848178479Sjb
1849178479Sjb			dt_cg_arglist(dnp->dn_ident, dnp->dn_args, dlp, drp);
1850178479Sjb
1851178479Sjb			if ((dnp->dn_reg = dt_regset_alloc(drp)) == -1)
1852178479Sjb				longjmp(yypcb->pcb_jmpbuf, EDT_NOREG);
1853178479Sjb
1854178479Sjb			instr = DIF_INSTR_CALL(
1855178479Sjb			    dnp->dn_ident->di_id, dnp->dn_reg);
1856178479Sjb
1857178479Sjb			dt_irlist_append(dlp,
1858178479Sjb			    dt_cg_node_alloc(DT_LBL_NONE, instr));
1859178479Sjb
1860178479Sjb			break;
1861178479Sjb		}
1862178479Sjb
1863178479Sjb		case DT_NODE_VAR:
1864178479Sjb			if (dnp->dn_ident->di_kind == DT_IDENT_XLSOU ||
1865178479Sjb			    dnp->dn_ident->di_kind == DT_IDENT_XLPTR) {
1866178479Sjb				/*
1867178479Sjb				 * This can only happen if we have translated
1868178479Sjb				 * args[].  See dt_idcook_args() for details.
1869178479Sjb				 */
1870178479Sjb				assert(dnp->dn_ident->di_id == DIF_VAR_ARGS);
1871178479Sjb				dt_cg_array_op(dnp, dlp, drp);
1872178479Sjb				break;
1873178479Sjb			}
1874178479Sjb
1875178479Sjb			if (dnp->dn_ident->di_kind == DT_IDENT_ARRAY) {
1876178479Sjb				if (dnp->dn_ident->di_id > DIF_VAR_ARRAY_MAX)
1877178479Sjb					dt_cg_assoc_op(dnp, dlp, drp);
1878178479Sjb				else
1879178479Sjb					dt_cg_array_op(dnp, dlp, drp);
1880178479Sjb				break;
1881178479Sjb			}
1882178479Sjb
1883178479Sjb			if ((dnp->dn_reg = dt_regset_alloc(drp)) == -1)
1884178479Sjb				longjmp(yypcb->pcb_jmpbuf, EDT_NOREG);
1885178479Sjb
1886178479Sjb			if (dnp->dn_ident->di_flags & DT_IDFLG_LOCAL)
1887178479Sjb				op = DIF_OP_LDLS;
1888178479Sjb			else if (dnp->dn_ident->di_flags & DT_IDFLG_TLS)
1889178479Sjb				op = DIF_OP_LDTS;
1890178479Sjb			else
1891178479Sjb				op = DIF_OP_LDGS;
1892178479Sjb
1893178479Sjb			dnp->dn_ident->di_flags |= DT_IDFLG_DIFR;
1894178479Sjb
1895178479Sjb			instr = DIF_INSTR_LDV(op,
1896178479Sjb			    dnp->dn_ident->di_id, dnp->dn_reg);
1897178479Sjb
1898178479Sjb			dt_irlist_append(dlp,
1899178479Sjb			    dt_cg_node_alloc(DT_LBL_NONE, instr));
1900178479Sjb			break;
1901178479Sjb
1902178479Sjb		case DT_NODE_SYM: {
1903178479Sjb			dtrace_hdl_t *dtp = yypcb->pcb_hdl;
1904178479Sjb			dtrace_syminfo_t *sip = dnp->dn_ident->di_data;
1905178479Sjb			GElf_Sym sym;
1906178479Sjb
1907178479Sjb			if (dtrace_lookup_by_name(dtp,
1908178479Sjb			    sip->dts_object, sip->dts_name, &sym, NULL) == -1) {
1909178479Sjb				xyerror(D_UNKNOWN, "cg failed for symbol %s`%s:"
1910178479Sjb				    " %s\n", sip->dts_object, sip->dts_name,
1911178479Sjb				    dtrace_errmsg(dtp, dtrace_errno(dtp)));
1912178479Sjb			}
1913178479Sjb
1914178479Sjb			if ((dnp->dn_reg = dt_regset_alloc(drp)) == -1)
1915178479Sjb				longjmp(yypcb->pcb_jmpbuf, EDT_NOREG);
1916178479Sjb
1917178479Sjb			dt_cg_xsetx(dlp, dnp->dn_ident,
1918178479Sjb			    DT_LBL_NONE, dnp->dn_reg, sym.st_value);
1919178479Sjb
1920178479Sjb			if (!(dnp->dn_flags & DT_NF_REF)) {
1921178479Sjb				instr = DIF_INSTR_LOAD(dt_cg_load(dnp, ctfp,
1922178479Sjb				    dnp->dn_type), dnp->dn_reg, dnp->dn_reg);
1923178479Sjb				dt_irlist_append(dlp,
1924178479Sjb				    dt_cg_node_alloc(DT_LBL_NONE, instr));
1925178479Sjb			}
1926178479Sjb			break;
1927178479Sjb		}
1928178479Sjb
1929178479Sjb		default:
1930178479Sjb			xyerror(D_UNKNOWN, "internal error -- node type %u is "
1931178479Sjb			    "not valid for an identifier\n", dnp->dn_kind);
1932178479Sjb		}
1933178479Sjb		break;
1934178479Sjb
1935178479Sjb	case DT_TOK_INT:
1936178479Sjb		if ((dnp->dn_reg = dt_regset_alloc(drp)) == -1)
1937178479Sjb			longjmp(yypcb->pcb_jmpbuf, EDT_NOREG);
1938178479Sjb
1939178479Sjb		dt_cg_setx(dlp, dnp->dn_reg, dnp->dn_value);
1940178479Sjb		break;
1941178479Sjb
1942178479Sjb	default:
1943178479Sjb		xyerror(D_UNKNOWN, "internal error -- token type %u is not a "
1944178479Sjb		    "valid D compilation token\n", dnp->dn_op);
1945178479Sjb	}
1946178479Sjb}
1947178479Sjb
1948178479Sjbvoid
1949178479Sjbdt_cg(dt_pcb_t *pcb, dt_node_t *dnp)
1950178479Sjb{
1951178479Sjb	dif_instr_t instr;
1952178479Sjb	dt_xlator_t *dxp;
1953178479Sjb
1954178479Sjb	if (pcb->pcb_regs == NULL && (pcb->pcb_regs =
1955178479Sjb	    dt_regset_create(pcb->pcb_hdl->dt_conf.dtc_difintregs)) == NULL)
1956178479Sjb		longjmp(pcb->pcb_jmpbuf, EDT_NOMEM);
1957178479Sjb
1958178479Sjb	dt_regset_reset(pcb->pcb_regs);
1959178479Sjb	(void) dt_regset_alloc(pcb->pcb_regs); /* allocate %r0 */
1960178479Sjb
1961178479Sjb	if (pcb->pcb_inttab != NULL)
1962178479Sjb		dt_inttab_destroy(pcb->pcb_inttab);
1963178479Sjb
1964178479Sjb	if ((pcb->pcb_inttab = dt_inttab_create(yypcb->pcb_hdl)) == NULL)
1965178479Sjb		longjmp(pcb->pcb_jmpbuf, EDT_NOMEM);
1966178479Sjb
1967178479Sjb	if (pcb->pcb_strtab != NULL)
1968178479Sjb		dt_strtab_destroy(pcb->pcb_strtab);
1969178479Sjb
1970178479Sjb	if ((pcb->pcb_strtab = dt_strtab_create(BUFSIZ)) == NULL)
1971178479Sjb		longjmp(pcb->pcb_jmpbuf, EDT_NOMEM);
1972178479Sjb
1973178479Sjb	dt_irlist_destroy(&pcb->pcb_ir);
1974178479Sjb	dt_irlist_create(&pcb->pcb_ir);
1975178479Sjb
1976178479Sjb	assert(pcb->pcb_dret == NULL);
1977178479Sjb	pcb->pcb_dret = dnp;
1978178479Sjb
1979178479Sjb	if (dt_node_is_dynamic(dnp)) {
1980178479Sjb		dnerror(dnp, D_CG_DYN, "expression cannot evaluate to result "
1981178479Sjb		    "of dynamic type\n");
1982178479Sjb	}
1983178479Sjb
1984178479Sjb	/*
1985178479Sjb	 * If we're generating code for a translator body, assign the input
1986178479Sjb	 * parameter to the first available register (i.e. caller passes %r1).
1987178479Sjb	 */
1988178479Sjb	if (dnp->dn_kind == DT_NODE_MEMBER) {
1989178479Sjb		dxp = dnp->dn_membxlator;
1990178479Sjb		dnp = dnp->dn_membexpr;
1991178479Sjb
1992178479Sjb		dxp->dx_ident->di_flags |= DT_IDFLG_CGREG;
1993178479Sjb		dxp->dx_ident->di_id = dt_regset_alloc(pcb->pcb_regs);
1994178479Sjb	}
1995178479Sjb
1996178479Sjb	dt_cg_node(dnp, &pcb->pcb_ir, pcb->pcb_regs);
1997178479Sjb	instr = DIF_INSTR_RET(dnp->dn_reg);
1998178479Sjb	dt_regset_free(pcb->pcb_regs, dnp->dn_reg);
1999178479Sjb	dt_irlist_append(&pcb->pcb_ir, dt_cg_node_alloc(DT_LBL_NONE, instr));
2000178479Sjb
2001178479Sjb	if (dnp->dn_kind == DT_NODE_MEMBER) {
2002178479Sjb		dt_regset_free(pcb->pcb_regs, dxp->dx_ident->di_id);
2003178479Sjb		dxp->dx_ident->di_id = 0;
2004178479Sjb		dxp->dx_ident->di_flags &= ~DT_IDFLG_CGREG;
2005178479Sjb	}
2006178479Sjb}
2007