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 */
22250812Smarkj
23178479Sjb/*
24178479Sjb * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
25178479Sjb * Use is subject to license terms.
26178479Sjb */
27178479Sjb
28250812Smarkj/*
29250812Smarkj * Copyright (c) 2012 by Delphix. All rights reserved.
30250812Smarkj */
31178479Sjb
32178479Sjb#include <sys/types.h>
33178479Sjb#include <sys/sysmacros.h>
34178479Sjb#include <sys/isa_defs.h>
35178479Sjb
36178479Sjb#include <strings.h>
37178479Sjb#include <stdlib.h>
38178479Sjb#include <setjmp.h>
39178479Sjb#include <assert.h>
40178479Sjb#include <errno.h>
41178479Sjb
42178479Sjb#include <dt_impl.h>
43178479Sjb#include <dt_grammar.h>
44178479Sjb#include <dt_parser.h>
45178479Sjb#include <dt_provider.h>
46178479Sjb
47178479Sjbstatic void dt_cg_node(dt_node_t *, dt_irlist_t *, dt_regset_t *);
48178479Sjb
49178479Sjbstatic dt_irnode_t *
50178479Sjbdt_cg_node_alloc(uint_t label, dif_instr_t instr)
51178479Sjb{
52178479Sjb	dt_irnode_t *dip = malloc(sizeof (dt_irnode_t));
53178479Sjb
54178479Sjb	if (dip == NULL)
55178479Sjb		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
56178479Sjb
57178479Sjb	dip->di_label = label;
58178479Sjb	dip->di_instr = instr;
59178479Sjb	dip->di_extern = NULL;
60178479Sjb	dip->di_next = NULL;
61178479Sjb
62178479Sjb	return (dip);
63178479Sjb}
64178479Sjb
65178479Sjb/*
66178479Sjb * Code generator wrapper function for ctf_member_info.  If we are given a
67178479Sjb * reference to a forward declaration tag, search the entire type space for
68178479Sjb * the actual definition and then call ctf_member_info on the result.
69178479Sjb */
70178479Sjbstatic ctf_file_t *
71178479Sjbdt_cg_membinfo(ctf_file_t *fp, ctf_id_t type, const char *s, ctf_membinfo_t *mp)
72178479Sjb{
73178479Sjb	while (ctf_type_kind(fp, type) == CTF_K_FORWARD) {
74178479Sjb		char n[DT_TYPE_NAMELEN];
75178479Sjb		dtrace_typeinfo_t dtt;
76178479Sjb
77178479Sjb		if (ctf_type_name(fp, type, n, sizeof (n)) == NULL ||
78178479Sjb		    dt_type_lookup(n, &dtt) == -1 || (
79178479Sjb		    dtt.dtt_ctfp == fp && dtt.dtt_type == type))
80178479Sjb			break; /* unable to improve our position */
81178479Sjb
82178479Sjb		fp = dtt.dtt_ctfp;
83178479Sjb		type = ctf_type_resolve(fp, dtt.dtt_type);
84178479Sjb	}
85178479Sjb
86178479Sjb	if (ctf_member_info(fp, type, s, mp) == CTF_ERR)
87178479Sjb		return (NULL); /* ctf_errno is set for us */
88178479Sjb
89178479Sjb	return (fp);
90178479Sjb}
91178479Sjb
92178479Sjbstatic void
93178479Sjbdt_cg_xsetx(dt_irlist_t *dlp, dt_ident_t *idp, uint_t lbl, int reg, uint64_t x)
94178479Sjb{
95178479Sjb	int flag = idp != NULL ? DT_INT_PRIVATE : DT_INT_SHARED;
96178479Sjb	int intoff = dt_inttab_insert(yypcb->pcb_inttab, x, flag);
97178479Sjb	dif_instr_t instr = DIF_INSTR_SETX((uint_t)intoff, reg);
98178479Sjb
99178479Sjb	if (intoff == -1)
100178479Sjb		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
101178479Sjb
102178479Sjb	if (intoff > DIF_INTOFF_MAX)
103178479Sjb		longjmp(yypcb->pcb_jmpbuf, EDT_INT2BIG);
104178479Sjb
105178479Sjb	dt_irlist_append(dlp, dt_cg_node_alloc(lbl, instr));
106178479Sjb
107178479Sjb	if (idp != NULL)
108178479Sjb		dlp->dl_last->di_extern = idp;
109178479Sjb}
110178479Sjb
111178479Sjbstatic void
112178479Sjbdt_cg_setx(dt_irlist_t *dlp, int reg, uint64_t x)
113178479Sjb{
114178479Sjb	dt_cg_xsetx(dlp, NULL, DT_LBL_NONE, reg, x);
115178479Sjb}
116178479Sjb
117178479Sjb/*
118178479Sjb * When loading bit-fields, we want to convert a byte count in the range
119178479Sjb * 1-8 to the closest power of 2 (e.g. 3->4, 5->8, etc).  The clp2() function
120178479Sjb * is a clever implementation from "Hacker's Delight" by Henry Warren, Jr.
121178479Sjb */
122178479Sjbstatic size_t
123178479Sjbclp2(size_t x)
124178479Sjb{
125178479Sjb	x--;
126178479Sjb
127178479Sjb	x |= (x >> 1);
128178479Sjb	x |= (x >> 2);
129178479Sjb	x |= (x >> 4);
130178479Sjb	x |= (x >> 8);
131178479Sjb	x |= (x >> 16);
132178479Sjb
133178479Sjb	return (x + 1);
134178479Sjb}
135178479Sjb
136178479Sjb/*
137178479Sjb * Lookup the correct load opcode to use for the specified node and CTF type.
138178479Sjb * We determine the size and convert it to a 3-bit index.  Our lookup table
139178479Sjb * is constructed to use a 5-bit index, consisting of the 3-bit size 0-7, a
140178479Sjb * bit for the sign, and a bit for userland address.  For example, a 4-byte
141178479Sjb * signed load from userland would be at the following table index:
142178479Sjb * user=1 sign=1 size=4 => binary index 11011 = decimal index 27
143178479Sjb */
144178479Sjbstatic uint_t
145178479Sjbdt_cg_load(dt_node_t *dnp, ctf_file_t *ctfp, ctf_id_t type)
146178479Sjb{
147178479Sjb	static const uint_t ops[] = {
148178479Sjb		DIF_OP_LDUB,	DIF_OP_LDUH,	0,	DIF_OP_LDUW,
149178479Sjb		0,		0,		0,	DIF_OP_LDX,
150178479Sjb		DIF_OP_LDSB,	DIF_OP_LDSH,	0,	DIF_OP_LDSW,
151178479Sjb		0,		0,		0,	DIF_OP_LDX,
152178479Sjb		DIF_OP_ULDUB,	DIF_OP_ULDUH,	0,	DIF_OP_ULDUW,
153178479Sjb		0,		0,		0,	DIF_OP_ULDX,
154178479Sjb		DIF_OP_ULDSB,	DIF_OP_ULDSH,	0,	DIF_OP_ULDSW,
155178479Sjb		0,		0,		0,	DIF_OP_ULDX,
156178479Sjb	};
157178479Sjb
158178479Sjb	ctf_encoding_t e;
159178479Sjb	ssize_t size;
160178479Sjb
161178479Sjb	/*
162178479Sjb	 * If we're loading a bit-field, the size of our load is found by
163178479Sjb	 * rounding cte_bits up to a byte boundary and then finding the
164178479Sjb	 * nearest power of two to this value (see clp2(), above).
165178479Sjb	 */
166178479Sjb	if ((dnp->dn_flags & DT_NF_BITFIELD) &&
167178479Sjb	    ctf_type_encoding(ctfp, type, &e) != CTF_ERR)
168178479Sjb		size = clp2(P2ROUNDUP(e.cte_bits, NBBY) / NBBY);
169178479Sjb	else
170178479Sjb		size = ctf_type_size(ctfp, type);
171178479Sjb
172178479Sjb	if (size < 1 || size > 8 || (size & (size - 1)) != 0) {
173178479Sjb		xyerror(D_UNKNOWN, "internal error -- cg cannot load "
174178479Sjb		    "size %ld when passed by value\n", (long)size);
175178479Sjb	}
176178479Sjb
177178479Sjb	size--; /* convert size to 3-bit index */
178178479Sjb
179178479Sjb	if (dnp->dn_flags & DT_NF_SIGNED)
180178479Sjb		size |= 0x08;
181178479Sjb	if (dnp->dn_flags & DT_NF_USERLAND)
182178479Sjb		size |= 0x10;
183178479Sjb
184178479Sjb	return (ops[size]);
185178479Sjb}
186178479Sjb
187178479Sjbstatic void
188178479Sjbdt_cg_ptrsize(dt_node_t *dnp, dt_irlist_t *dlp, dt_regset_t *drp,
189178479Sjb    uint_t op, int dreg)
190178479Sjb{
191178479Sjb	ctf_file_t *ctfp = dnp->dn_ctfp;
192178479Sjb	ctf_arinfo_t r;
193178479Sjb	dif_instr_t instr;
194178479Sjb	ctf_id_t type;
195178479Sjb	uint_t kind;
196178479Sjb	ssize_t size;
197178479Sjb	int sreg;
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
215250812Smarkj	sreg = dt_regset_alloc(drp);
216178479Sjb	dt_cg_setx(dlp, sreg, size);
217178479Sjb	instr = DIF_INSTR_FMT(op, dreg, sreg, dreg);
218178479Sjb	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
219178479Sjb	dt_regset_free(drp, sreg);
220178479Sjb}
221178479Sjb
222178479Sjb/*
223178479Sjb * If the result of a "." or "->" operation is a bit-field, we use this routine
224178479Sjb * to generate an epilogue to the load instruction that extracts the value.  In
225178479Sjb * the diagrams below the "ld??" is the load instruction that is generated to
226178479Sjb * load the containing word that is generating prior to calling this function.
227178479Sjb *
228178479Sjb * Epilogue for unsigned fields:	Epilogue for signed fields:
229178479Sjb *
230178479Sjb * ldu?	[r1], r1			lds? [r1], r1
231178479Sjb * setx	USHIFT, r2			setx 64 - SSHIFT, r2
232178479Sjb * srl	r1, r2, r1			sll  r1, r2, r1
233178479Sjb * setx	(1 << bits) - 1, r2		setx 64 - bits, r2
234178479Sjb * and	r1, r2, r1			sra  r1, r2, r1
235178479Sjb *
236178479Sjb * The *SHIFT constants above changes value depending on the endian-ness of our
237178479Sjb * target architecture.  Refer to the comments below for more details.
238178479Sjb */
239178479Sjbstatic void
240178479Sjbdt_cg_field_get(dt_node_t *dnp, dt_irlist_t *dlp, dt_regset_t *drp,
241178479Sjb    ctf_file_t *fp, const ctf_membinfo_t *mp)
242178479Sjb{
243178479Sjb	ctf_encoding_t e;
244178479Sjb	dif_instr_t instr;
245178479Sjb	uint64_t shift;
246178479Sjb	int r1, r2;
247178479Sjb
248178479Sjb	if (ctf_type_encoding(fp, mp->ctm_type, &e) != 0 || e.cte_bits > 64) {
249178479Sjb		xyerror(D_UNKNOWN, "cg: bad field: off %lu type <%ld> "
250178479Sjb		    "bits %u\n", mp->ctm_offset, mp->ctm_type, e.cte_bits);
251178479Sjb	}
252178479Sjb
253178479Sjb	assert(dnp->dn_op == DT_TOK_PTR || dnp->dn_op == DT_TOK_DOT);
254178479Sjb	r1 = dnp->dn_left->dn_reg;
255250812Smarkj	r2 = dt_regset_alloc(drp);
256178479Sjb
257178479Sjb	/*
258178479Sjb	 * On little-endian architectures, ctm_offset counts from the right so
259178479Sjb	 * ctm_offset % NBBY itself is the amount we want to shift right to
260178479Sjb	 * move the value bits to the little end of the register to mask them.
261178479Sjb	 * On big-endian architectures, ctm_offset counts from the left so we
262178479Sjb	 * must subtract (ctm_offset % NBBY + cte_bits) from the size in bits
263178479Sjb	 * we used for the load.  The size of our load in turn is found by
264178479Sjb	 * rounding cte_bits up to a byte boundary and then finding the
265178479Sjb	 * nearest power of two to this value (see clp2(), above).  These
266178479Sjb	 * properties are used to compute shift as USHIFT or SSHIFT, below.
267178479Sjb	 */
268178479Sjb	if (dnp->dn_flags & DT_NF_SIGNED) {
269178576Sjb#if BYTE_ORDER == _BIG_ENDIAN
270178479Sjb		shift = clp2(P2ROUNDUP(e.cte_bits, NBBY) / NBBY) * NBBY -
271178479Sjb		    mp->ctm_offset % NBBY;
272178479Sjb#else
273178479Sjb		shift = mp->ctm_offset % NBBY + e.cte_bits;
274178479Sjb#endif
275178479Sjb		dt_cg_setx(dlp, r2, 64 - shift);
276178479Sjb		instr = DIF_INSTR_FMT(DIF_OP_SLL, r1, r2, r1);
277178479Sjb		dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
278178479Sjb
279178479Sjb		dt_cg_setx(dlp, r2, 64 - e.cte_bits);
280178479Sjb		instr = DIF_INSTR_FMT(DIF_OP_SRA, r1, r2, r1);
281178479Sjb		dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
282178479Sjb	} else {
283178576Sjb#if BYTE_ORDER == _BIG_ENDIAN
284178479Sjb		shift = clp2(P2ROUNDUP(e.cte_bits, NBBY) / NBBY) * NBBY -
285178479Sjb		    (mp->ctm_offset % NBBY + e.cte_bits);
286178479Sjb#else
287178479Sjb		shift = mp->ctm_offset % NBBY;
288178479Sjb#endif
289178479Sjb		dt_cg_setx(dlp, r2, shift);
290178479Sjb		instr = DIF_INSTR_FMT(DIF_OP_SRL, r1, r2, r1);
291178479Sjb		dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
292178479Sjb
293178479Sjb		dt_cg_setx(dlp, r2, (1ULL << e.cte_bits) - 1);
294178479Sjb		instr = DIF_INSTR_FMT(DIF_OP_AND, r1, r2, r1);
295178479Sjb		dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
296178479Sjb	}
297178479Sjb
298178479Sjb	dt_regset_free(drp, r2);
299178479Sjb}
300178479Sjb
301178479Sjb/*
302178479Sjb * If the destination of a store operation is a bit-field, we use this routine
303178479Sjb * to generate a prologue to the store instruction that loads the surrounding
304178479Sjb * bits, clears the destination field, and ORs in the new value of the field.
305178479Sjb * In the diagram below the "st?" is the store instruction that is generated to
306178479Sjb * store the containing word that is generating after calling this function.
307178479Sjb *
308178479Sjb * ld	[dst->dn_reg], r1
309178479Sjb * setx	~(((1 << cte_bits) - 1) << (ctm_offset % NBBY)), r2
310178479Sjb * and	r1, r2, r1
311178479Sjb *
312178479Sjb * setx	(1 << cte_bits) - 1, r2
313178479Sjb * and	src->dn_reg, r2, r2
314178479Sjb * setx ctm_offset % NBBY, r3
315178479Sjb * sll	r2, r3, r2
316178479Sjb *
317178479Sjb * or	r1, r2, r1
318178479Sjb * st?	r1, [dst->dn_reg]
319178479Sjb *
320178479Sjb * This routine allocates a new register to hold the value to be stored and
321178479Sjb * returns it.  The caller is responsible for freeing this register later.
322178479Sjb */
323178479Sjbstatic int
324178479Sjbdt_cg_field_set(dt_node_t *src, dt_irlist_t *dlp,
325178479Sjb    dt_regset_t *drp, dt_node_t *dst)
326178479Sjb{
327178479Sjb	uint64_t cmask, fmask, shift;
328178479Sjb	dif_instr_t instr;
329178479Sjb	int r1, r2, r3;
330178479Sjb
331178479Sjb	ctf_membinfo_t m;
332178479Sjb	ctf_encoding_t e;
333178479Sjb	ctf_file_t *fp, *ofp;
334178479Sjb	ctf_id_t type;
335178479Sjb
336178479Sjb	assert(dst->dn_op == DT_TOK_PTR || dst->dn_op == DT_TOK_DOT);
337178479Sjb	assert(dst->dn_right->dn_kind == DT_NODE_IDENT);
338178479Sjb
339178479Sjb	fp = dst->dn_left->dn_ctfp;
340178479Sjb	type = ctf_type_resolve(fp, dst->dn_left->dn_type);
341178479Sjb
342178479Sjb	if (dst->dn_op == DT_TOK_PTR) {
343178479Sjb		type = ctf_type_reference(fp, type);
344178479Sjb		type = ctf_type_resolve(fp, type);
345178479Sjb	}
346178479Sjb
347178479Sjb	if ((fp = dt_cg_membinfo(ofp = fp, type,
348178479Sjb	    dst->dn_right->dn_string, &m)) == NULL) {
349178479Sjb		yypcb->pcb_hdl->dt_ctferr = ctf_errno(ofp);
350178479Sjb		longjmp(yypcb->pcb_jmpbuf, EDT_CTF);
351178479Sjb	}
352178479Sjb
353178479Sjb	if (ctf_type_encoding(fp, m.ctm_type, &e) != 0 || e.cte_bits > 64) {
354178479Sjb		xyerror(D_UNKNOWN, "cg: bad field: off %lu type <%ld> "
355178479Sjb		    "bits %u\n", m.ctm_offset, m.ctm_type, e.cte_bits);
356178479Sjb	}
357178479Sjb
358250812Smarkj	r1 = dt_regset_alloc(drp);
359250812Smarkj	r2 = dt_regset_alloc(drp);
360250812Smarkj	r3 = dt_regset_alloc(drp);
361178479Sjb
362178479Sjb	/*
363178479Sjb	 * Compute shifts and masks.  We need to compute "shift" as the amount
364178479Sjb	 * we need to shift left to position our field in the containing word.
365178479Sjb	 * Refer to the comments in dt_cg_field_get(), above, for more info.
366178479Sjb	 * We then compute fmask as the mask that truncates the value in the
367178479Sjb	 * input register to width cte_bits, and cmask as the mask used to
368178479Sjb	 * pass through the containing bits and zero the field bits.
369178479Sjb	 */
370178576Sjb#if BYTE_ORDER == _BIG_ENDIAN
371178479Sjb	shift = clp2(P2ROUNDUP(e.cte_bits, NBBY) / NBBY) * NBBY -
372178479Sjb	    (m.ctm_offset % NBBY + e.cte_bits);
373178479Sjb#else
374178479Sjb	shift = m.ctm_offset % NBBY;
375178479Sjb#endif
376178479Sjb	fmask = (1ULL << e.cte_bits) - 1;
377178479Sjb	cmask = ~(fmask << shift);
378178479Sjb
379178479Sjb	instr = DIF_INSTR_LOAD(
380178479Sjb	    dt_cg_load(dst, fp, m.ctm_type), dst->dn_reg, r1);
381178479Sjb	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
382178479Sjb
383178479Sjb	dt_cg_setx(dlp, r2, cmask);
384178479Sjb	instr = DIF_INSTR_FMT(DIF_OP_AND, r1, r2, r1);
385178479Sjb	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
386178479Sjb
387178479Sjb	dt_cg_setx(dlp, r2, fmask);
388178479Sjb	instr = DIF_INSTR_FMT(DIF_OP_AND, src->dn_reg, r2, r2);
389178479Sjb	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
390178479Sjb
391178479Sjb	dt_cg_setx(dlp, r3, shift);
392178479Sjb	instr = DIF_INSTR_FMT(DIF_OP_SLL, r2, r3, r2);
393178479Sjb	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
394178479Sjb
395178479Sjb	instr = DIF_INSTR_FMT(DIF_OP_OR, r1, r2, r1);
396178479Sjb	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
397178479Sjb
398178479Sjb	dt_regset_free(drp, r3);
399178479Sjb	dt_regset_free(drp, r2);
400178479Sjb
401178479Sjb	return (r1);
402178479Sjb}
403178479Sjb
404178479Sjbstatic void
405178479Sjbdt_cg_store(dt_node_t *src, dt_irlist_t *dlp, dt_regset_t *drp, dt_node_t *dst)
406178479Sjb{
407178479Sjb	ctf_encoding_t e;
408178479Sjb	dif_instr_t instr;
409178479Sjb	size_t size;
410178479Sjb	int reg;
411178479Sjb
412178479Sjb	/*
413178479Sjb	 * If we're loading a bit-field, the size of our store is found by
414178479Sjb	 * rounding dst's cte_bits up to a byte boundary and then finding the
415178479Sjb	 * nearest power of two to this value (see clp2(), above).
416178479Sjb	 */
417178479Sjb	if ((dst->dn_flags & DT_NF_BITFIELD) &&
418178479Sjb	    ctf_type_encoding(dst->dn_ctfp, dst->dn_type, &e) != CTF_ERR)
419178479Sjb		size = clp2(P2ROUNDUP(e.cte_bits, NBBY) / NBBY);
420178479Sjb	else
421178479Sjb		size = dt_node_type_size(src);
422178479Sjb
423178479Sjb	if (src->dn_flags & DT_NF_REF) {
424250812Smarkj		reg = dt_regset_alloc(drp);
425178479Sjb		dt_cg_setx(dlp, reg, size);
426178479Sjb		instr = DIF_INSTR_COPYS(src->dn_reg, reg, dst->dn_reg);
427178479Sjb		dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
428178479Sjb		dt_regset_free(drp, reg);
429178479Sjb	} else {
430178479Sjb		if (dst->dn_flags & DT_NF_BITFIELD)
431178479Sjb			reg = dt_cg_field_set(src, dlp, drp, dst);
432178479Sjb		else
433178479Sjb			reg = src->dn_reg;
434178479Sjb
435178479Sjb		switch (size) {
436178479Sjb		case 1:
437178479Sjb			instr = DIF_INSTR_STORE(DIF_OP_STB, reg, dst->dn_reg);
438178479Sjb			break;
439178479Sjb		case 2:
440178479Sjb			instr = DIF_INSTR_STORE(DIF_OP_STH, reg, dst->dn_reg);
441178479Sjb			break;
442178479Sjb		case 4:
443178479Sjb			instr = DIF_INSTR_STORE(DIF_OP_STW, reg, dst->dn_reg);
444178479Sjb			break;
445178479Sjb		case 8:
446178479Sjb			instr = DIF_INSTR_STORE(DIF_OP_STX, reg, dst->dn_reg);
447178479Sjb			break;
448178479Sjb		default:
449178479Sjb			xyerror(D_UNKNOWN, "internal error -- cg cannot store "
450178479Sjb			    "size %lu when passed by value\n", (ulong_t)size);
451178479Sjb		}
452178479Sjb		dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
453178479Sjb
454178479Sjb		if (dst->dn_flags & DT_NF_BITFIELD)
455178479Sjb			dt_regset_free(drp, reg);
456178479Sjb	}
457178479Sjb}
458178479Sjb
459178479Sjb/*
460178479Sjb * Generate code for a typecast or for argument promotion from the type of the
461178479Sjb * actual to the type of the formal.  We need to generate code for casts when
462178479Sjb * a scalar type is being narrowed or changing signed-ness.  We first shift the
463178479Sjb * desired bits high (losing excess bits if narrowing) and then shift them down
464178479Sjb * using logical shift (unsigned result) or arithmetic shift (signed result).
465178479Sjb */
466178479Sjbstatic void
467178479Sjbdt_cg_typecast(const dt_node_t *src, const dt_node_t *dst,
468178479Sjb    dt_irlist_t *dlp, dt_regset_t *drp)
469178479Sjb{
470178479Sjb	size_t srcsize = dt_node_type_size(src);
471178479Sjb	size_t dstsize = dt_node_type_size(dst);
472178479Sjb
473178479Sjb	dif_instr_t instr;
474250812Smarkj	int rg;
475178479Sjb
476250812Smarkj	if (!dt_node_is_scalar(dst))
477250812Smarkj		return; /* not a scalar */
478250812Smarkj	if (dstsize == srcsize &&
479250812Smarkj	    ((src->dn_flags ^ dst->dn_flags) & DT_NF_SIGNED) != 0)
480250812Smarkj		return; /* not narrowing or changing signed-ness */
481250812Smarkj	if (dstsize > srcsize && (src->dn_flags & DT_NF_SIGNED) == 0)
482250812Smarkj		return; /* nothing to do in this case */
483178479Sjb
484250812Smarkj	rg = dt_regset_alloc(drp);
485178479Sjb
486250812Smarkj	if (dstsize > srcsize) {
487250812Smarkj		int n = sizeof (uint64_t) * NBBY - srcsize * NBBY;
488250812Smarkj		int s = (dstsize - srcsize) * NBBY;
489178479Sjb
490250812Smarkj		dt_cg_setx(dlp, rg, n);
491250812Smarkj
492250812Smarkj		instr = DIF_INSTR_FMT(DIF_OP_SLL, src->dn_reg, rg, dst->dn_reg);
493178479Sjb		dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
494178479Sjb
495250812Smarkj		if ((dst->dn_flags & DT_NF_SIGNED) || n == s) {
496250812Smarkj			instr = DIF_INSTR_FMT(DIF_OP_SRA,
497250812Smarkj			    dst->dn_reg, rg, dst->dn_reg);
498250812Smarkj			dt_irlist_append(dlp,
499250812Smarkj			    dt_cg_node_alloc(DT_LBL_NONE, instr));
500250812Smarkj		} else {
501250812Smarkj			dt_cg_setx(dlp, rg, s);
502250812Smarkj			instr = DIF_INSTR_FMT(DIF_OP_SRA,
503250812Smarkj			    dst->dn_reg, rg, dst->dn_reg);
504250812Smarkj			dt_irlist_append(dlp,
505250812Smarkj			    dt_cg_node_alloc(DT_LBL_NONE, instr));
506250812Smarkj			dt_cg_setx(dlp, rg, n - s);
507250812Smarkj			instr = DIF_INSTR_FMT(DIF_OP_SRL,
508250812Smarkj			    dst->dn_reg, rg, dst->dn_reg);
509250812Smarkj			dt_irlist_append(dlp,
510250812Smarkj			    dt_cg_node_alloc(DT_LBL_NONE, instr));
511250812Smarkj		}
512250812Smarkj	} else if (dstsize != sizeof (uint64_t)) {
513250812Smarkj		int n = sizeof (uint64_t) * NBBY - dstsize * NBBY;
514178479Sjb
515250812Smarkj		dt_cg_setx(dlp, rg, n);
516250812Smarkj
517250812Smarkj		instr = DIF_INSTR_FMT(DIF_OP_SLL, src->dn_reg, rg, dst->dn_reg);
518178479Sjb		dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
519250812Smarkj
520250812Smarkj		instr = DIF_INSTR_FMT((dst->dn_flags & DT_NF_SIGNED) ?
521250812Smarkj		    DIF_OP_SRA : DIF_OP_SRL, dst->dn_reg, rg, dst->dn_reg);
522250812Smarkj		dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
523178479Sjb	}
524250812Smarkj
525250812Smarkj	dt_regset_free(drp, rg);
526178479Sjb}
527178479Sjb
528178479Sjb/*
529178479Sjb * Generate code to push the specified argument list on to the tuple stack.
530178479Sjb * We use this routine for handling subroutine calls and associative arrays.
531178479Sjb * We must first generate code for all subexpressions before loading the stack
532178479Sjb * because any subexpression could itself require the use of the tuple stack.
533178479Sjb * This holds a number of registers equal to the number of arguments, but this
534178479Sjb * is not a huge problem because the number of arguments can't exceed the
535178479Sjb * number of tuple register stack elements anyway.  At most one extra register
536178479Sjb * is required (either by dt_cg_typecast() or for dtdt_size, below).  This
537178479Sjb * implies that a DIF implementation should offer a number of general purpose
538178479Sjb * registers at least one greater than the number of tuple registers.
539178479Sjb */
540178479Sjbstatic void
541178479Sjbdt_cg_arglist(dt_ident_t *idp, dt_node_t *args,
542178479Sjb    dt_irlist_t *dlp, dt_regset_t *drp)
543178479Sjb{
544178479Sjb	const dt_idsig_t *isp = idp->di_data;
545178479Sjb	dt_node_t *dnp;
546178479Sjb	int i = 0;
547178479Sjb
548178479Sjb	for (dnp = args; dnp != NULL; dnp = dnp->dn_list)
549178479Sjb		dt_cg_node(dnp, dlp, drp);
550178479Sjb
551250812Smarkj	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, DIF_INSTR_FLUSHTS));
552178479Sjb
553178479Sjb	for (dnp = args; dnp != NULL; dnp = dnp->dn_list, i++) {
554178479Sjb		dtrace_diftype_t t;
555178479Sjb		dif_instr_t instr;
556178479Sjb		uint_t op;
557178479Sjb		int reg;
558178479Sjb
559178479Sjb		dt_node_diftype(yypcb->pcb_hdl, dnp, &t);
560178479Sjb
561178479Sjb		isp->dis_args[i].dn_reg = dnp->dn_reg; /* re-use register */
562178479Sjb		dt_cg_typecast(dnp, &isp->dis_args[i], dlp, drp);
563178479Sjb		isp->dis_args[i].dn_reg = -1;
564178479Sjb
565250812Smarkj		if (t.dtdt_flags & DIF_TF_BYREF) {
566178479Sjb			op = DIF_OP_PUSHTR;
567250812Smarkj			if (t.dtdt_size != 0) {
568250812Smarkj				reg = dt_regset_alloc(drp);
569250812Smarkj				dt_cg_setx(dlp, reg, t.dtdt_size);
570250812Smarkj			} else {
571250812Smarkj				reg = DIF_REG_R0;
572250812Smarkj			}
573250812Smarkj		} else {
574178479Sjb			op = DIF_OP_PUSHTV;
575178479Sjb			reg = DIF_REG_R0;
576250812Smarkj		}
577178479Sjb
578178479Sjb		instr = DIF_INSTR_PUSHTS(op, t.dtdt_kind, reg, dnp->dn_reg);
579178479Sjb		dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
580178479Sjb		dt_regset_free(drp, dnp->dn_reg);
581178479Sjb
582178479Sjb		if (reg != DIF_REG_R0)
583178479Sjb			dt_regset_free(drp, reg);
584178479Sjb	}
585178479Sjb
586178479Sjb	if (i > yypcb->pcb_hdl->dt_conf.dtc_diftupregs)
587178479Sjb		longjmp(yypcb->pcb_jmpbuf, EDT_NOTUPREG);
588178479Sjb}
589178479Sjb
590178479Sjbstatic void
591178479Sjbdt_cg_arithmetic_op(dt_node_t *dnp, dt_irlist_t *dlp,
592178479Sjb    dt_regset_t *drp, uint_t op)
593178479Sjb{
594178479Sjb	int is_ptr_op = (dnp->dn_op == DT_TOK_ADD || dnp->dn_op == DT_TOK_SUB ||
595178479Sjb	    dnp->dn_op == DT_TOK_ADD_EQ || dnp->dn_op == DT_TOK_SUB_EQ);
596178479Sjb
597178479Sjb	int lp_is_ptr = dt_node_is_pointer(dnp->dn_left);
598178479Sjb	int rp_is_ptr = dt_node_is_pointer(dnp->dn_right);
599178479Sjb
600178479Sjb	dif_instr_t instr;
601178479Sjb
602178479Sjb	if (lp_is_ptr && rp_is_ptr) {
603178479Sjb		assert(dnp->dn_op == DT_TOK_SUB);
604178479Sjb		is_ptr_op = 0;
605178479Sjb	}
606178479Sjb
607178479Sjb	dt_cg_node(dnp->dn_left, dlp, drp);
608178479Sjb	if (is_ptr_op && rp_is_ptr)
609178479Sjb		dt_cg_ptrsize(dnp, dlp, drp, DIF_OP_MUL, dnp->dn_left->dn_reg);
610178479Sjb
611178479Sjb	dt_cg_node(dnp->dn_right, dlp, drp);
612178479Sjb	if (is_ptr_op && lp_is_ptr)
613178479Sjb		dt_cg_ptrsize(dnp, dlp, drp, DIF_OP_MUL, dnp->dn_right->dn_reg);
614178479Sjb
615178479Sjb	instr = DIF_INSTR_FMT(op, dnp->dn_left->dn_reg,
616178479Sjb	    dnp->dn_right->dn_reg, dnp->dn_left->dn_reg);
617178479Sjb
618178479Sjb	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
619178479Sjb	dt_regset_free(drp, dnp->dn_right->dn_reg);
620178479Sjb	dnp->dn_reg = dnp->dn_left->dn_reg;
621178479Sjb
622178479Sjb	if (lp_is_ptr && rp_is_ptr)
623178479Sjb		dt_cg_ptrsize(dnp->dn_right,
624178479Sjb		    dlp, drp, DIF_OP_UDIV, dnp->dn_reg);
625178479Sjb}
626178479Sjb
627178479Sjbstatic uint_t
628178479Sjbdt_cg_stvar(const dt_ident_t *idp)
629178479Sjb{
630178479Sjb	static const uint_t aops[] = { DIF_OP_STGAA, DIF_OP_STTAA, DIF_OP_NOP };
631178479Sjb	static const uint_t sops[] = { DIF_OP_STGS, DIF_OP_STTS, DIF_OP_STLS };
632178479Sjb
633178479Sjb	uint_t i = (((idp->di_flags & DT_IDFLG_LOCAL) != 0) << 1) |
634178479Sjb	    ((idp->di_flags & DT_IDFLG_TLS) != 0);
635178479Sjb
636178479Sjb	return (idp->di_kind == DT_IDENT_ARRAY ? aops[i] : sops[i]);
637178479Sjb}
638178479Sjb
639178479Sjbstatic void
640178479Sjbdt_cg_prearith_op(dt_node_t *dnp, dt_irlist_t *dlp, dt_regset_t *drp, uint_t op)
641178479Sjb{
642178479Sjb	ctf_file_t *ctfp = dnp->dn_ctfp;
643178479Sjb	dif_instr_t instr;
644178479Sjb	ctf_id_t type;
645178479Sjb	ssize_t size = 1;
646178479Sjb	int reg;
647178479Sjb
648178479Sjb	if (dt_node_is_pointer(dnp)) {
649178479Sjb		type = ctf_type_resolve(ctfp, dnp->dn_type);
650178479Sjb		assert(ctf_type_kind(ctfp, type) == CTF_K_POINTER);
651178479Sjb		size = ctf_type_size(ctfp, ctf_type_reference(ctfp, type));
652178479Sjb	}
653178479Sjb
654178479Sjb	dt_cg_node(dnp->dn_child, dlp, drp);
655178479Sjb	dnp->dn_reg = dnp->dn_child->dn_reg;
656178479Sjb
657250812Smarkj	reg = dt_regset_alloc(drp);
658178479Sjb	dt_cg_setx(dlp, reg, size);
659178479Sjb
660178479Sjb	instr = DIF_INSTR_FMT(op, dnp->dn_reg, reg, dnp->dn_reg);
661178479Sjb	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
662178479Sjb	dt_regset_free(drp, reg);
663178479Sjb
664178479Sjb	/*
665178479Sjb	 * If we are modifying a variable, generate an stv instruction from
666178479Sjb	 * the variable specified by the identifier.  If we are storing to a
667178479Sjb	 * memory address, generate code again for the left-hand side using
668178479Sjb	 * DT_NF_REF to get the address, and then generate a store to it.
669178479Sjb	 * In both paths, we store the value in dnp->dn_reg (the new value).
670178479Sjb	 */
671178479Sjb	if (dnp->dn_child->dn_kind == DT_NODE_VAR) {
672178479Sjb		dt_ident_t *idp = dt_ident_resolve(dnp->dn_child->dn_ident);
673178479Sjb
674178479Sjb		idp->di_flags |= DT_IDFLG_DIFW;
675178479Sjb		instr = DIF_INSTR_STV(dt_cg_stvar(idp),
676178479Sjb		    idp->di_id, dnp->dn_reg);
677178479Sjb		dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
678178479Sjb	} else {
679178479Sjb		uint_t rbit = dnp->dn_child->dn_flags & DT_NF_REF;
680178479Sjb
681178479Sjb		assert(dnp->dn_child->dn_flags & DT_NF_WRITABLE);
682178479Sjb		assert(dnp->dn_child->dn_flags & DT_NF_LVALUE);
683178479Sjb
684178479Sjb		dnp->dn_child->dn_flags |= DT_NF_REF; /* force pass-by-ref */
685178479Sjb		dt_cg_node(dnp->dn_child, dlp, drp);
686178479Sjb
687178479Sjb		dt_cg_store(dnp, dlp, drp, dnp->dn_child);
688178479Sjb		dt_regset_free(drp, dnp->dn_child->dn_reg);
689178479Sjb
690178479Sjb		dnp->dn_left->dn_flags &= ~DT_NF_REF;
691178479Sjb		dnp->dn_left->dn_flags |= rbit;
692178479Sjb	}
693178479Sjb}
694178479Sjb
695178479Sjbstatic void
696178479Sjbdt_cg_postarith_op(dt_node_t *dnp, dt_irlist_t *dlp,
697178479Sjb    dt_regset_t *drp, uint_t op)
698178479Sjb{
699178479Sjb	ctf_file_t *ctfp = dnp->dn_ctfp;
700178479Sjb	dif_instr_t instr;
701178479Sjb	ctf_id_t type;
702178479Sjb	ssize_t size = 1;
703178479Sjb	int nreg;
704178479Sjb
705178479Sjb	if (dt_node_is_pointer(dnp)) {
706178479Sjb		type = ctf_type_resolve(ctfp, dnp->dn_type);
707178479Sjb		assert(ctf_type_kind(ctfp, type) == CTF_K_POINTER);
708178479Sjb		size = ctf_type_size(ctfp, ctf_type_reference(ctfp, type));
709178479Sjb	}
710178479Sjb
711178479Sjb	dt_cg_node(dnp->dn_child, dlp, drp);
712178479Sjb	dnp->dn_reg = dnp->dn_child->dn_reg;
713178479Sjb
714250812Smarkj	nreg = dt_regset_alloc(drp);
715178479Sjb	dt_cg_setx(dlp, nreg, size);
716178479Sjb	instr = DIF_INSTR_FMT(op, dnp->dn_reg, nreg, nreg);
717178479Sjb	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
718178479Sjb
719178479Sjb	/*
720178479Sjb	 * If we are modifying a variable, generate an stv instruction from
721178479Sjb	 * the variable specified by the identifier.  If we are storing to a
722178479Sjb	 * memory address, generate code again for the left-hand side using
723178479Sjb	 * DT_NF_REF to get the address, and then generate a store to it.
724178479Sjb	 * In both paths, we store the value from 'nreg' (the new value).
725178479Sjb	 */
726178479Sjb	if (dnp->dn_child->dn_kind == DT_NODE_VAR) {
727178479Sjb		dt_ident_t *idp = dt_ident_resolve(dnp->dn_child->dn_ident);
728178479Sjb
729178479Sjb		idp->di_flags |= DT_IDFLG_DIFW;
730178479Sjb		instr = DIF_INSTR_STV(dt_cg_stvar(idp), idp->di_id, nreg);
731178479Sjb		dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
732178479Sjb	} else {
733178479Sjb		uint_t rbit = dnp->dn_child->dn_flags & DT_NF_REF;
734178479Sjb		int oreg = dnp->dn_reg;
735178479Sjb
736178479Sjb		assert(dnp->dn_child->dn_flags & DT_NF_WRITABLE);
737178479Sjb		assert(dnp->dn_child->dn_flags & DT_NF_LVALUE);
738178479Sjb
739178479Sjb		dnp->dn_child->dn_flags |= DT_NF_REF; /* force pass-by-ref */
740178479Sjb		dt_cg_node(dnp->dn_child, dlp, drp);
741178479Sjb
742178479Sjb		dnp->dn_reg = nreg;
743178479Sjb		dt_cg_store(dnp, dlp, drp, dnp->dn_child);
744178479Sjb		dnp->dn_reg = oreg;
745178479Sjb
746178479Sjb		dt_regset_free(drp, dnp->dn_child->dn_reg);
747178479Sjb		dnp->dn_left->dn_flags &= ~DT_NF_REF;
748178479Sjb		dnp->dn_left->dn_flags |= rbit;
749178479Sjb	}
750178479Sjb
751178479Sjb	dt_regset_free(drp, nreg);
752178479Sjb}
753178479Sjb
754178479Sjb/*
755178479Sjb * Determine if we should perform signed or unsigned comparison for an OP2.
756178479Sjb * If both operands are of arithmetic type, perform the usual arithmetic
757178479Sjb * conversions to determine the common real type for comparison [ISOC 6.5.8.3].
758178479Sjb */
759178479Sjbstatic int
760178479Sjbdt_cg_compare_signed(dt_node_t *dnp)
761178479Sjb{
762178479Sjb	dt_node_t dn;
763178479Sjb
764178479Sjb	if (dt_node_is_string(dnp->dn_left) ||
765178479Sjb	    dt_node_is_string(dnp->dn_right))
766178479Sjb		return (1); /* strings always compare signed */
767178479Sjb	else if (!dt_node_is_arith(dnp->dn_left) ||
768178479Sjb	    !dt_node_is_arith(dnp->dn_right))
769178479Sjb		return (0); /* non-arithmetic types always compare unsigned */
770178479Sjb
771178479Sjb	bzero(&dn, sizeof (dn));
772178479Sjb	dt_node_promote(dnp->dn_left, dnp->dn_right, &dn);
773178479Sjb	return (dn.dn_flags & DT_NF_SIGNED);
774178479Sjb}
775178479Sjb
776178479Sjbstatic void
777178479Sjbdt_cg_compare_op(dt_node_t *dnp, dt_irlist_t *dlp, dt_regset_t *drp, uint_t op)
778178479Sjb{
779178479Sjb	uint_t lbl_true = dt_irlist_label(dlp);
780178479Sjb	uint_t lbl_post = dt_irlist_label(dlp);
781178479Sjb
782178479Sjb	dif_instr_t instr;
783178479Sjb	uint_t opc;
784178479Sjb
785178479Sjb	dt_cg_node(dnp->dn_left, dlp, drp);
786178479Sjb	dt_cg_node(dnp->dn_right, dlp, drp);
787178479Sjb
788178479Sjb	if (dt_node_is_string(dnp->dn_left) || dt_node_is_string(dnp->dn_right))
789178479Sjb		opc = DIF_OP_SCMP;
790178479Sjb	else
791178479Sjb		opc = DIF_OP_CMP;
792178479Sjb
793178479Sjb	instr = DIF_INSTR_CMP(opc, dnp->dn_left->dn_reg, dnp->dn_right->dn_reg);
794178479Sjb	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
795178479Sjb	dt_regset_free(drp, dnp->dn_right->dn_reg);
796178479Sjb	dnp->dn_reg = dnp->dn_left->dn_reg;
797178479Sjb
798178479Sjb	instr = DIF_INSTR_BRANCH(op, lbl_true);
799178479Sjb	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
800178479Sjb
801178479Sjb	instr = DIF_INSTR_MOV(DIF_REG_R0, dnp->dn_reg);
802178479Sjb	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
803178479Sjb
804178479Sjb	instr = DIF_INSTR_BRANCH(DIF_OP_BA, lbl_post);
805178479Sjb	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
806178479Sjb
807178479Sjb	dt_cg_xsetx(dlp, NULL, lbl_true, dnp->dn_reg, 1);
808178479Sjb	dt_irlist_append(dlp, dt_cg_node_alloc(lbl_post, DIF_INSTR_NOP));
809178479Sjb}
810178479Sjb
811178479Sjb/*
812178479Sjb * Code generation for the ternary op requires some trickery with the assembler
813178479Sjb * in order to conserve registers.  We generate code for dn_expr and dn_left
814178479Sjb * and free their registers so they do not have be consumed across codegen for
815178479Sjb * dn_right.  We insert a dummy MOV at the end of dn_left into the destination
816178479Sjb * register, which is not yet known because we haven't done dn_right yet, and
817178479Sjb * save the pointer to this instruction node.  We then generate code for
818178479Sjb * dn_right and use its register as our output.  Finally, we reach back and
819178479Sjb * patch the instruction for dn_left to move its output into this register.
820178479Sjb */
821178479Sjbstatic void
822178479Sjbdt_cg_ternary_op(dt_node_t *dnp, dt_irlist_t *dlp, dt_regset_t *drp)
823178479Sjb{
824178479Sjb	uint_t lbl_false = dt_irlist_label(dlp);
825178479Sjb	uint_t lbl_post = dt_irlist_label(dlp);
826178479Sjb
827178479Sjb	dif_instr_t instr;
828178479Sjb	dt_irnode_t *dip;
829178479Sjb
830178479Sjb	dt_cg_node(dnp->dn_expr, dlp, drp);
831178479Sjb	instr = DIF_INSTR_TST(dnp->dn_expr->dn_reg);
832178479Sjb	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
833178479Sjb	dt_regset_free(drp, dnp->dn_expr->dn_reg);
834178479Sjb
835178479Sjb	instr = DIF_INSTR_BRANCH(DIF_OP_BE, lbl_false);
836178479Sjb	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
837178479Sjb
838178479Sjb	dt_cg_node(dnp->dn_left, dlp, drp);
839178479Sjb	instr = DIF_INSTR_MOV(dnp->dn_left->dn_reg, DIF_REG_R0);
840178479Sjb	dip = dt_cg_node_alloc(DT_LBL_NONE, instr); /* save dip for below */
841178479Sjb	dt_irlist_append(dlp, dip);
842178479Sjb	dt_regset_free(drp, dnp->dn_left->dn_reg);
843178479Sjb
844178479Sjb	instr = DIF_INSTR_BRANCH(DIF_OP_BA, lbl_post);
845178479Sjb	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
846178479Sjb
847178479Sjb	dt_irlist_append(dlp, dt_cg_node_alloc(lbl_false, DIF_INSTR_NOP));
848178479Sjb	dt_cg_node(dnp->dn_right, dlp, drp);
849178479Sjb	dnp->dn_reg = dnp->dn_right->dn_reg;
850178479Sjb
851178479Sjb	/*
852178479Sjb	 * Now that dn_reg is assigned, reach back and patch the correct MOV
853178479Sjb	 * instruction into the tail of dn_left.  We know dn_reg was unused
854178479Sjb	 * at that point because otherwise dn_right couldn't have allocated it.
855178479Sjb	 */
856178479Sjb	dip->di_instr = DIF_INSTR_MOV(dnp->dn_left->dn_reg, dnp->dn_reg);
857178479Sjb	dt_irlist_append(dlp, dt_cg_node_alloc(lbl_post, DIF_INSTR_NOP));
858178479Sjb}
859178479Sjb
860178479Sjbstatic void
861178479Sjbdt_cg_logical_and(dt_node_t *dnp, dt_irlist_t *dlp, dt_regset_t *drp)
862178479Sjb{
863178479Sjb	uint_t lbl_false = dt_irlist_label(dlp);
864178479Sjb	uint_t lbl_post = dt_irlist_label(dlp);
865178479Sjb
866178479Sjb	dif_instr_t instr;
867178479Sjb
868178479Sjb	dt_cg_node(dnp->dn_left, dlp, drp);
869178479Sjb	instr = DIF_INSTR_TST(dnp->dn_left->dn_reg);
870178479Sjb	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
871178479Sjb	dt_regset_free(drp, dnp->dn_left->dn_reg);
872178479Sjb
873178479Sjb	instr = DIF_INSTR_BRANCH(DIF_OP_BE, lbl_false);
874178479Sjb	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
875178479Sjb
876178479Sjb	dt_cg_node(dnp->dn_right, dlp, drp);
877178479Sjb	instr = DIF_INSTR_TST(dnp->dn_right->dn_reg);
878178479Sjb	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
879178479Sjb	dnp->dn_reg = dnp->dn_right->dn_reg;
880178479Sjb
881178479Sjb	instr = DIF_INSTR_BRANCH(DIF_OP_BE, lbl_false);
882178479Sjb	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
883178479Sjb
884178479Sjb	dt_cg_setx(dlp, dnp->dn_reg, 1);
885178479Sjb
886178479Sjb	instr = DIF_INSTR_BRANCH(DIF_OP_BA, lbl_post);
887178479Sjb	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
888178479Sjb
889178479Sjb	instr = DIF_INSTR_MOV(DIF_REG_R0, dnp->dn_reg);
890178479Sjb	dt_irlist_append(dlp, dt_cg_node_alloc(lbl_false, instr));
891178479Sjb
892178479Sjb	dt_irlist_append(dlp, dt_cg_node_alloc(lbl_post, DIF_INSTR_NOP));
893178479Sjb}
894178479Sjb
895178479Sjbstatic void
896178479Sjbdt_cg_logical_xor(dt_node_t *dnp, dt_irlist_t *dlp, dt_regset_t *drp)
897178479Sjb{
898178479Sjb	uint_t lbl_next = dt_irlist_label(dlp);
899178479Sjb	uint_t lbl_tail = dt_irlist_label(dlp);
900178479Sjb
901178479Sjb	dif_instr_t instr;
902178479Sjb
903178479Sjb	dt_cg_node(dnp->dn_left, dlp, drp);
904178479Sjb	instr = DIF_INSTR_TST(dnp->dn_left->dn_reg);
905178479Sjb	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
906178479Sjb
907178479Sjb	instr = DIF_INSTR_BRANCH(DIF_OP_BE, lbl_next);
908178479Sjb	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
909178479Sjb	dt_cg_setx(dlp, dnp->dn_left->dn_reg, 1);
910178479Sjb
911178479Sjb	dt_irlist_append(dlp, dt_cg_node_alloc(lbl_next, DIF_INSTR_NOP));
912178479Sjb	dt_cg_node(dnp->dn_right, dlp, drp);
913178479Sjb
914178479Sjb	instr = DIF_INSTR_TST(dnp->dn_right->dn_reg);
915178479Sjb	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
916178479Sjb
917178479Sjb	instr = DIF_INSTR_BRANCH(DIF_OP_BE, lbl_tail);
918178479Sjb	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
919178479Sjb	dt_cg_setx(dlp, dnp->dn_right->dn_reg, 1);
920178479Sjb
921178479Sjb	instr = DIF_INSTR_FMT(DIF_OP_XOR, dnp->dn_left->dn_reg,
922178479Sjb	    dnp->dn_right->dn_reg, dnp->dn_left->dn_reg);
923178479Sjb
924178479Sjb	dt_irlist_append(dlp, dt_cg_node_alloc(lbl_tail, instr));
925178479Sjb
926178479Sjb	dt_regset_free(drp, dnp->dn_right->dn_reg);
927178479Sjb	dnp->dn_reg = dnp->dn_left->dn_reg;
928178479Sjb}
929178479Sjb
930178479Sjbstatic void
931178479Sjbdt_cg_logical_or(dt_node_t *dnp, dt_irlist_t *dlp, dt_regset_t *drp)
932178479Sjb{
933178479Sjb	uint_t lbl_true = dt_irlist_label(dlp);
934178479Sjb	uint_t lbl_false = dt_irlist_label(dlp);
935178479Sjb	uint_t lbl_post = dt_irlist_label(dlp);
936178479Sjb
937178479Sjb	dif_instr_t instr;
938178479Sjb
939178479Sjb	dt_cg_node(dnp->dn_left, dlp, drp);
940178479Sjb	instr = DIF_INSTR_TST(dnp->dn_left->dn_reg);
941178479Sjb	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
942178479Sjb	dt_regset_free(drp, dnp->dn_left->dn_reg);
943178479Sjb
944178479Sjb	instr = DIF_INSTR_BRANCH(DIF_OP_BNE, lbl_true);
945178479Sjb	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
946178479Sjb
947178479Sjb	dt_cg_node(dnp->dn_right, dlp, drp);
948178479Sjb	instr = DIF_INSTR_TST(dnp->dn_right->dn_reg);
949178479Sjb	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
950178479Sjb	dnp->dn_reg = dnp->dn_right->dn_reg;
951178479Sjb
952178479Sjb	instr = DIF_INSTR_BRANCH(DIF_OP_BE, lbl_false);
953178479Sjb	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
954178479Sjb
955178479Sjb	dt_cg_xsetx(dlp, NULL, lbl_true, dnp->dn_reg, 1);
956178479Sjb
957178479Sjb	instr = DIF_INSTR_BRANCH(DIF_OP_BA, lbl_post);
958178479Sjb	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
959178479Sjb
960178479Sjb	instr = DIF_INSTR_MOV(DIF_REG_R0, dnp->dn_reg);
961178479Sjb	dt_irlist_append(dlp, dt_cg_node_alloc(lbl_false, instr));
962178479Sjb
963178479Sjb	dt_irlist_append(dlp, dt_cg_node_alloc(lbl_post, DIF_INSTR_NOP));
964178479Sjb}
965178479Sjb
966178479Sjbstatic void
967178479Sjbdt_cg_logical_neg(dt_node_t *dnp, dt_irlist_t *dlp, dt_regset_t *drp)
968178479Sjb{
969178479Sjb	uint_t lbl_zero = dt_irlist_label(dlp);
970178479Sjb	uint_t lbl_post = dt_irlist_label(dlp);
971178479Sjb
972178479Sjb	dif_instr_t instr;
973178479Sjb
974178479Sjb	dt_cg_node(dnp->dn_child, dlp, drp);
975178479Sjb	dnp->dn_reg = dnp->dn_child->dn_reg;
976178479Sjb
977178479Sjb	instr = DIF_INSTR_TST(dnp->dn_reg);
978178479Sjb	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
979178479Sjb
980178479Sjb	instr = DIF_INSTR_BRANCH(DIF_OP_BE, lbl_zero);
981178479Sjb	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
982178479Sjb
983178479Sjb	instr = DIF_INSTR_MOV(DIF_REG_R0, dnp->dn_reg);
984178479Sjb	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
985178479Sjb
986178479Sjb	instr = DIF_INSTR_BRANCH(DIF_OP_BA, lbl_post);
987178479Sjb	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
988178479Sjb
989178479Sjb	dt_cg_xsetx(dlp, NULL, lbl_zero, dnp->dn_reg, 1);
990178479Sjb	dt_irlist_append(dlp, dt_cg_node_alloc(lbl_post, DIF_INSTR_NOP));
991178479Sjb}
992178479Sjb
993178479Sjbstatic void
994178479Sjbdt_cg_asgn_op(dt_node_t *dnp, dt_irlist_t *dlp, dt_regset_t *drp)
995178479Sjb{
996178479Sjb	dif_instr_t instr;
997178479Sjb	dt_ident_t *idp;
998178479Sjb
999178479Sjb	/*
1000178479Sjb	 * If we are performing a structure assignment of a translated type,
1001178479Sjb	 * we must instantiate all members and create a snapshot of the object
1002178479Sjb	 * in scratch space.  We allocs a chunk of memory, generate code for
1003178479Sjb	 * each member, and then set dnp->dn_reg to the scratch object address.
1004178479Sjb	 */
1005178479Sjb	if ((idp = dt_node_resolve(dnp->dn_right, DT_IDENT_XLSOU)) != NULL) {
1006178479Sjb		ctf_membinfo_t ctm;
1007178479Sjb		dt_xlator_t *dxp = idp->di_data;
1008178479Sjb		dt_node_t *mnp, dn, mn;
1009178479Sjb		int r1, r2;
1010178479Sjb
1011178479Sjb		/*
1012178479Sjb		 * Create two fake dt_node_t's representing operator "." and a
1013178479Sjb		 * right-hand identifier child node.  These will be repeatedly
1014178479Sjb		 * modified according to each instantiated member so that we
1015178479Sjb		 * can pass them to dt_cg_store() and effect a member store.
1016178479Sjb		 */
1017178479Sjb		bzero(&dn, sizeof (dt_node_t));
1018178479Sjb		dn.dn_kind = DT_NODE_OP2;
1019178479Sjb		dn.dn_op = DT_TOK_DOT;
1020178479Sjb		dn.dn_left = dnp;
1021178479Sjb		dn.dn_right = &mn;
1022178479Sjb
1023178479Sjb		bzero(&mn, sizeof (dt_node_t));
1024178479Sjb		mn.dn_kind = DT_NODE_IDENT;
1025178479Sjb		mn.dn_op = DT_TOK_IDENT;
1026178479Sjb
1027178479Sjb		/*
1028178479Sjb		 * Allocate a register for our scratch data pointer.  First we
1029178479Sjb		 * set it to the size of our data structure, and then replace
1030178479Sjb		 * it with the result of an allocs of the specified size.
1031178479Sjb		 */
1032250812Smarkj		r1 = dt_regset_alloc(drp);
1033178479Sjb		dt_cg_setx(dlp, r1,
1034178479Sjb		    ctf_type_size(dxp->dx_dst_ctfp, dxp->dx_dst_base));
1035178479Sjb
1036178479Sjb		instr = DIF_INSTR_ALLOCS(r1, r1);
1037178479Sjb		dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
1038178479Sjb
1039178479Sjb		/*
1040178479Sjb		 * When dt_cg_asgn_op() is called, we have already generated
1041178479Sjb		 * code for dnp->dn_right, which is the translator input.  We
1042178479Sjb		 * now associate this register with the translator's input
1043178479Sjb		 * identifier so it can be referenced during our member loop.
1044178479Sjb		 */
1045178479Sjb		dxp->dx_ident->di_flags |= DT_IDFLG_CGREG;
1046178479Sjb		dxp->dx_ident->di_id = dnp->dn_right->dn_reg;
1047178479Sjb
1048178479Sjb		for (mnp = dxp->dx_members; mnp != NULL; mnp = mnp->dn_list) {
1049178479Sjb			/*
1050178479Sjb			 * Generate code for the translator member expression,
1051178479Sjb			 * and then cast the result to the member type.
1052178479Sjb			 */
1053178479Sjb			dt_cg_node(mnp->dn_membexpr, dlp, drp);
1054178479Sjb			mnp->dn_reg = mnp->dn_membexpr->dn_reg;
1055178479Sjb			dt_cg_typecast(mnp->dn_membexpr, mnp, dlp, drp);
1056178479Sjb
1057178479Sjb			/*
1058178479Sjb			 * Ask CTF for the offset of the member so we can store
1059178479Sjb			 * to the appropriate offset.  This call has already
1060178479Sjb			 * been done once by the parser, so it should succeed.
1061178479Sjb			 */
1062178479Sjb			if (ctf_member_info(dxp->dx_dst_ctfp, dxp->dx_dst_base,
1063178479Sjb			    mnp->dn_membname, &ctm) == CTF_ERR) {
1064178479Sjb				yypcb->pcb_hdl->dt_ctferr =
1065178479Sjb				    ctf_errno(dxp->dx_dst_ctfp);
1066178479Sjb				longjmp(yypcb->pcb_jmpbuf, EDT_CTF);
1067178479Sjb			}
1068178479Sjb
1069178479Sjb			/*
1070178479Sjb			 * If the destination member is at offset 0, store the
1071178479Sjb			 * result directly to r1 (the scratch buffer address).
1072178479Sjb			 * Otherwise allocate another temporary for the offset
1073178479Sjb			 * and add r1 to it before storing the result.
1074178479Sjb			 */
1075178479Sjb			if (ctm.ctm_offset != 0) {
1076250812Smarkj				r2 = dt_regset_alloc(drp);
1077178479Sjb
1078178479Sjb				/*
1079178479Sjb				 * Add the member offset rounded down to the
1080178479Sjb				 * nearest byte.  If the offset was not aligned
1081178479Sjb				 * on a byte boundary, this member is a bit-
1082178479Sjb				 * field and dt_cg_store() will handle masking.
1083178479Sjb				 */
1084178479Sjb				dt_cg_setx(dlp, r2, ctm.ctm_offset / NBBY);
1085178479Sjb				instr = DIF_INSTR_FMT(DIF_OP_ADD, r1, r2, r2);
1086178479Sjb				dt_irlist_append(dlp,
1087178479Sjb				    dt_cg_node_alloc(DT_LBL_NONE, instr));
1088178479Sjb
1089178479Sjb				dt_node_type_propagate(mnp, &dn);
1090178479Sjb				dn.dn_right->dn_string = mnp->dn_membname;
1091178479Sjb				dn.dn_reg = r2;
1092178479Sjb
1093178479Sjb				dt_cg_store(mnp, dlp, drp, &dn);
1094178479Sjb				dt_regset_free(drp, r2);
1095178479Sjb
1096178479Sjb			} else {
1097178479Sjb				dt_node_type_propagate(mnp, &dn);
1098178479Sjb				dn.dn_right->dn_string = mnp->dn_membname;
1099178479Sjb				dn.dn_reg = r1;
1100178479Sjb
1101178479Sjb				dt_cg_store(mnp, dlp, drp, &dn);
1102178479Sjb			}
1103178479Sjb
1104178479Sjb			dt_regset_free(drp, mnp->dn_reg);
1105178479Sjb		}
1106178479Sjb
1107178479Sjb		dxp->dx_ident->di_flags &= ~DT_IDFLG_CGREG;
1108178479Sjb		dxp->dx_ident->di_id = 0;
1109178479Sjb
1110178479Sjb		if (dnp->dn_right->dn_reg != -1)
1111178479Sjb			dt_regset_free(drp, dnp->dn_right->dn_reg);
1112178479Sjb
1113178479Sjb		assert(dnp->dn_reg == dnp->dn_right->dn_reg);
1114178479Sjb		dnp->dn_reg = r1;
1115178479Sjb	}
1116178479Sjb
1117178479Sjb	/*
1118178479Sjb	 * If we are storing to a variable, generate an stv instruction from
1119178479Sjb	 * the variable specified by the identifier.  If we are storing to a
1120178479Sjb	 * memory address, generate code again for the left-hand side using
1121178479Sjb	 * DT_NF_REF to get the address, and then generate a store to it.
1122178479Sjb	 * In both paths, we assume dnp->dn_reg already has the new value.
1123178479Sjb	 */
1124178479Sjb	if (dnp->dn_left->dn_kind == DT_NODE_VAR) {
1125178479Sjb		idp = dt_ident_resolve(dnp->dn_left->dn_ident);
1126178479Sjb
1127178479Sjb		if (idp->di_kind == DT_IDENT_ARRAY)
1128178479Sjb			dt_cg_arglist(idp, dnp->dn_left->dn_args, dlp, drp);
1129178479Sjb
1130178479Sjb		idp->di_flags |= DT_IDFLG_DIFW;
1131178479Sjb		instr = DIF_INSTR_STV(dt_cg_stvar(idp),
1132178479Sjb		    idp->di_id, dnp->dn_reg);
1133178479Sjb		dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
1134178479Sjb	} else {
1135178479Sjb		uint_t rbit = dnp->dn_left->dn_flags & DT_NF_REF;
1136178479Sjb
1137178479Sjb		assert(dnp->dn_left->dn_flags & DT_NF_WRITABLE);
1138178479Sjb		assert(dnp->dn_left->dn_flags & DT_NF_LVALUE);
1139178479Sjb
1140178479Sjb		dnp->dn_left->dn_flags |= DT_NF_REF; /* force pass-by-ref */
1141178479Sjb
1142178479Sjb		dt_cg_node(dnp->dn_left, dlp, drp);
1143178479Sjb		dt_cg_store(dnp, dlp, drp, dnp->dn_left);
1144178479Sjb		dt_regset_free(drp, dnp->dn_left->dn_reg);
1145178479Sjb
1146178479Sjb		dnp->dn_left->dn_flags &= ~DT_NF_REF;
1147178479Sjb		dnp->dn_left->dn_flags |= rbit;
1148178479Sjb	}
1149178479Sjb}
1150178479Sjb
1151178479Sjbstatic void
1152178479Sjbdt_cg_assoc_op(dt_node_t *dnp, dt_irlist_t *dlp, dt_regset_t *drp)
1153178479Sjb{
1154178479Sjb	dif_instr_t instr;
1155178479Sjb	uint_t op;
1156178479Sjb
1157178479Sjb	assert(dnp->dn_kind == DT_NODE_VAR);
1158178479Sjb	assert(!(dnp->dn_ident->di_flags & DT_IDFLG_LOCAL));
1159178479Sjb	assert(dnp->dn_args != NULL);
1160178479Sjb
1161178479Sjb	dt_cg_arglist(dnp->dn_ident, dnp->dn_args, dlp, drp);
1162178479Sjb
1163250812Smarkj	dnp->dn_reg = dt_regset_alloc(drp);
1164178479Sjb
1165178479Sjb	if (dnp->dn_ident->di_flags & DT_IDFLG_TLS)
1166178479Sjb		op = DIF_OP_LDTAA;
1167178479Sjb	else
1168178479Sjb		op = DIF_OP_LDGAA;
1169178479Sjb
1170178479Sjb	dnp->dn_ident->di_flags |= DT_IDFLG_DIFR;
1171178479Sjb	instr = DIF_INSTR_LDV(op, dnp->dn_ident->di_id, dnp->dn_reg);
1172178479Sjb	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
1173178479Sjb
1174178479Sjb	/*
1175178479Sjb	 * If the associative array is a pass-by-reference type, then we are
1176178479Sjb	 * loading its value as a pointer to either load or store through it.
1177178479Sjb	 * The array element in question may not have been faulted in yet, in
1178178479Sjb	 * which case DIF_OP_LD*AA will return zero.  We append an epilogue
1179178479Sjb	 * of instructions similar to the following:
1180178479Sjb	 *
1181178479Sjb	 *	  ld?aa	 id, %r1	! base ld?aa instruction above
1182178479Sjb	 *	  tst	 %r1		! start of epilogue
1183178479Sjb	 *   +--- bne	 label
1184178479Sjb	 *   |    setx	 size, %r1
1185178479Sjb	 *   |    allocs %r1, %r1
1186178479Sjb	 *   |    st?aa	 id, %r1
1187178479Sjb	 *   |    ld?aa	 id, %r1
1188178479Sjb	 *   v
1189178479Sjb	 * label: < rest of code >
1190178479Sjb	 *
1191178479Sjb	 * The idea is that we allocs a zero-filled chunk of scratch space and
1192178479Sjb	 * do a DIF_OP_ST*AA to fault in and initialize the array element, and
1193178479Sjb	 * then reload it to get the faulted-in address of the new variable
1194178479Sjb	 * storage.  This isn't cheap, but pass-by-ref associative array values
1195178479Sjb	 * are (thus far) uncommon and the allocs cost only occurs once.  If
1196178479Sjb	 * this path becomes important to DTrace users, we can improve things
1197178479Sjb	 * by adding a new DIF opcode to fault in associative array elements.
1198178479Sjb	 */
1199178479Sjb	if (dnp->dn_flags & DT_NF_REF) {
1200178479Sjb		uint_t stvop = op == DIF_OP_LDTAA ? DIF_OP_STTAA : DIF_OP_STGAA;
1201178479Sjb		uint_t label = dt_irlist_label(dlp);
1202178479Sjb
1203178479Sjb		instr = DIF_INSTR_TST(dnp->dn_reg);
1204178479Sjb		dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
1205178479Sjb
1206178479Sjb		instr = DIF_INSTR_BRANCH(DIF_OP_BNE, label);
1207178479Sjb		dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
1208178479Sjb
1209178479Sjb		dt_cg_setx(dlp, dnp->dn_reg, dt_node_type_size(dnp));
1210178479Sjb		instr = DIF_INSTR_ALLOCS(dnp->dn_reg, dnp->dn_reg);
1211178479Sjb		dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
1212178479Sjb
1213178479Sjb		dnp->dn_ident->di_flags |= DT_IDFLG_DIFW;
1214178479Sjb		instr = DIF_INSTR_STV(stvop, dnp->dn_ident->di_id, dnp->dn_reg);
1215178479Sjb		dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
1216178479Sjb
1217178479Sjb		instr = DIF_INSTR_LDV(op, dnp->dn_ident->di_id, dnp->dn_reg);
1218178479Sjb		dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
1219178479Sjb
1220178479Sjb		dt_irlist_append(dlp, dt_cg_node_alloc(label, DIF_INSTR_NOP));
1221178479Sjb	}
1222178479Sjb}
1223178479Sjb
1224178479Sjbstatic void
1225178479Sjbdt_cg_array_op(dt_node_t *dnp, dt_irlist_t *dlp, dt_regset_t *drp)
1226178479Sjb{
1227178479Sjb	dt_probe_t *prp = yypcb->pcb_probe;
1228178479Sjb	uintmax_t saved = dnp->dn_args->dn_value;
1229178479Sjb	dt_ident_t *idp = dnp->dn_ident;
1230178479Sjb
1231178479Sjb	dif_instr_t instr;
1232178479Sjb	uint_t op;
1233178479Sjb	size_t size;
1234178479Sjb	int reg, n;
1235178479Sjb
1236178479Sjb	assert(dnp->dn_kind == DT_NODE_VAR);
1237178479Sjb	assert(!(idp->di_flags & DT_IDFLG_LOCAL));
1238178479Sjb
1239178479Sjb	assert(dnp->dn_args->dn_kind == DT_NODE_INT);
1240178479Sjb	assert(dnp->dn_args->dn_list == NULL);
1241178479Sjb
1242178479Sjb	/*
1243178479Sjb	 * If this is a reference in the args[] array, temporarily modify the
1244178479Sjb	 * array index according to the static argument mapping (if any),
1245178479Sjb	 * unless the argument reference is provided by a dynamic translator.
1246178479Sjb	 * If we're using a dynamic translator for args[], then just set dn_reg
1247178479Sjb	 * to an invalid reg and return: DIF_OP_XLARG will fetch the arg later.
1248178479Sjb	 */
1249178479Sjb	if (idp->di_id == DIF_VAR_ARGS) {
1250178479Sjb		if ((idp->di_kind == DT_IDENT_XLPTR ||
1251178479Sjb		    idp->di_kind == DT_IDENT_XLSOU) &&
1252178479Sjb		    dt_xlator_dynamic(idp->di_data)) {
1253178479Sjb			dnp->dn_reg = -1;
1254178479Sjb			return;
1255178479Sjb		}
1256178479Sjb		dnp->dn_args->dn_value = prp->pr_mapping[saved];
1257178479Sjb	}
1258178479Sjb
1259178479Sjb	dt_cg_node(dnp->dn_args, dlp, drp);
1260178479Sjb	dnp->dn_args->dn_value = saved;
1261178479Sjb
1262178479Sjb	dnp->dn_reg = dnp->dn_args->dn_reg;
1263178479Sjb
1264178479Sjb	if (idp->di_flags & DT_IDFLG_TLS)
1265178479Sjb		op = DIF_OP_LDTA;
1266178479Sjb	else
1267178479Sjb		op = DIF_OP_LDGA;
1268178479Sjb
1269178479Sjb	idp->di_flags |= DT_IDFLG_DIFR;
1270178479Sjb
1271178479Sjb	instr = DIF_INSTR_LDA(op, idp->di_id,
1272178479Sjb	    dnp->dn_args->dn_reg, dnp->dn_reg);
1273178479Sjb
1274178479Sjb	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
1275178479Sjb
1276178479Sjb	/*
1277178479Sjb	 * If this is a reference to the args[] array, we need to take the
1278178479Sjb	 * additional step of explicitly eliminating any bits larger than the
1279178479Sjb	 * type size: the DIF interpreter in the kernel will always give us
1280178479Sjb	 * the raw (64-bit) argument value, and any bits larger than the type
1281178479Sjb	 * size may be junk.  As a practical matter, this arises only on 64-bit
1282178479Sjb	 * architectures and only when the argument index is larger than the
1283178479Sjb	 * number of arguments passed directly to DTrace: if a 8-, 16- or
1284178479Sjb	 * 32-bit argument must be retrieved from the stack, it is possible
1285178479Sjb	 * (and it some cases, likely) that the upper bits will be garbage.
1286178479Sjb	 */
1287178479Sjb	if (idp->di_id != DIF_VAR_ARGS || !dt_node_is_scalar(dnp))
1288178479Sjb		return;
1289178479Sjb
1290178479Sjb	if ((size = dt_node_type_size(dnp)) == sizeof (uint64_t))
1291178479Sjb		return;
1292178479Sjb
1293250812Smarkj	reg = dt_regset_alloc(drp);
1294178479Sjb	assert(size < sizeof (uint64_t));
1295178479Sjb	n = sizeof (uint64_t) * NBBY - size * NBBY;
1296178479Sjb
1297178479Sjb	dt_cg_setx(dlp, reg, n);
1298178479Sjb
1299178479Sjb	instr = DIF_INSTR_FMT(DIF_OP_SLL, dnp->dn_reg, reg, dnp->dn_reg);
1300178479Sjb	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
1301178479Sjb
1302178479Sjb	instr = DIF_INSTR_FMT((dnp->dn_flags & DT_NF_SIGNED) ?
1303178479Sjb	    DIF_OP_SRA : DIF_OP_SRL, dnp->dn_reg, reg, dnp->dn_reg);
1304178479Sjb
1305178479Sjb	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
1306178479Sjb	dt_regset_free(drp, reg);
1307178479Sjb}
1308178479Sjb
1309178479Sjb/*
1310178479Sjb * Generate code for an inlined variable reference.  Inlines can be used to
1311178479Sjb * define either scalar or associative array substitutions.  For scalars, we
1312178479Sjb * simply generate code for the parse tree saved in the identifier's din_root,
1313178479Sjb * and then cast the resulting expression to the inline's declaration type.
1314178479Sjb * For arrays, we take the input parameter subtrees from dnp->dn_args and
1315178479Sjb * temporarily store them in the din_root of each din_argv[i] identifier,
1316178479Sjb * which are themselves inlines and were set up for us by the parser.  The
1317178479Sjb * result is that any reference to the inlined parameter inside the top-level
1318178479Sjb * din_root will turn into a recursive call to dt_cg_inline() for a scalar
1319178479Sjb * inline whose din_root will refer to the subtree pointed to by the argument.
1320178479Sjb */
1321178479Sjbstatic void
1322178479Sjbdt_cg_inline(dt_node_t *dnp, dt_irlist_t *dlp, dt_regset_t *drp)
1323178479Sjb{
1324178479Sjb	dt_ident_t *idp = dnp->dn_ident;
1325178479Sjb	dt_idnode_t *inp = idp->di_iarg;
1326178479Sjb
1327178479Sjb	dt_idnode_t *pinp;
1328178479Sjb	dt_node_t *pnp;
1329178479Sjb	int i;
1330178479Sjb
1331178479Sjb	assert(idp->di_flags & DT_IDFLG_INLINE);
1332178479Sjb	assert(idp->di_ops == &dt_idops_inline);
1333178479Sjb
1334178479Sjb	if (idp->di_kind == DT_IDENT_ARRAY) {
1335178479Sjb		for (i = 0, pnp = dnp->dn_args;
1336178479Sjb		    pnp != NULL; pnp = pnp->dn_list, i++) {
1337178479Sjb			if (inp->din_argv[i] != NULL) {
1338178479Sjb				pinp = inp->din_argv[i]->di_iarg;
1339178479Sjb				pinp->din_root = pnp;
1340178479Sjb			}
1341178479Sjb		}
1342178479Sjb	}
1343178479Sjb
1344178479Sjb	dt_cg_node(inp->din_root, dlp, drp);
1345178479Sjb	dnp->dn_reg = inp->din_root->dn_reg;
1346178479Sjb	dt_cg_typecast(inp->din_root, dnp, dlp, drp);
1347178479Sjb
1348178479Sjb	if (idp->di_kind == DT_IDENT_ARRAY) {
1349178479Sjb		for (i = 0; i < inp->din_argc; i++) {
1350178479Sjb			pinp = inp->din_argv[i]->di_iarg;
1351178479Sjb			pinp->din_root = NULL;
1352178479Sjb		}
1353178479Sjb	}
1354178479Sjb}
1355178479Sjb
1356178479Sjbstatic void
1357178576Sjbdt_cg_func_typeref(dtrace_hdl_t *dtp, dt_node_t *dnp)
1358178576Sjb{
1359178576Sjb	dtrace_typeinfo_t dtt;
1360178576Sjb	dt_node_t *addr = dnp->dn_args;
1361178576Sjb	dt_node_t *nelm = addr->dn_list;
1362178576Sjb	dt_node_t *strp = nelm->dn_list;
1363178576Sjb	dt_node_t *typs = strp->dn_list;
1364178576Sjb	char buf[DT_TYPE_NAMELEN];
1365178576Sjb	char *p;
1366178576Sjb
1367178576Sjb	ctf_type_name(addr->dn_ctfp, addr->dn_type, buf, sizeof (buf));
1368178576Sjb
1369178576Sjb	/*
1370178576Sjb	 * XXX Hack alert! XXX
1371178576Sjb	 * The prototype has two dummy args that we munge to represent
1372178576Sjb	 * the type string and the type size.
1373178576Sjb	 *
1374178576Sjb	 * Yes, I hear your grumble, but it works for now. We'll come
1375178576Sjb	 * up with a more elegant implementation later. :-)
1376178576Sjb	 */
1377178576Sjb	free(strp->dn_string);
1378178576Sjb
1379178576Sjb	if ((p = strchr(buf, '*')) != NULL)
1380178576Sjb		*p = '\0';
1381178576Sjb
1382178576Sjb	strp->dn_string = strdup(buf);
1383178576Sjb
1384178576Sjb	if (dtrace_lookup_by_type(dtp,  DTRACE_OBJ_EVERY, buf, &dtt) < 0)
1385178576Sjb		return;
1386178576Sjb
1387178576Sjb	typs->dn_value = ctf_type_size(dtt.dtt_ctfp, dtt.dtt_type);
1388178576Sjb}
1389178576Sjb
1390253725Spfgtypedef struct dt_xlmemb {
1391253725Spfg	dt_ident_t *dtxl_idp;		/* translated ident */
1392253725Spfg	dt_irlist_t *dtxl_dlp;		/* instruction list */
1393253725Spfg	dt_regset_t *dtxl_drp;		/* register set */
1394253725Spfg	int dtxl_sreg;			/* location of the translation input */
1395253725Spfg	int dtxl_dreg;			/* location of our allocated buffer */
1396253725Spfg} dt_xlmemb_t;
1397253725Spfg
1398253725Spfg/*ARGSUSED*/
1399253725Spfgstatic int
1400253725Spfgdt_cg_xlate_member(const char *name, ctf_id_t type, ulong_t off, void *arg)
1401253725Spfg{
1402253725Spfg	dt_xlmemb_t *dx = arg;
1403253725Spfg	dt_ident_t *idp = dx->dtxl_idp;
1404253725Spfg	dt_irlist_t *dlp = dx->dtxl_dlp;
1405253725Spfg	dt_regset_t *drp = dx->dtxl_drp;
1406253725Spfg
1407253725Spfg	dt_node_t *mnp;
1408253725Spfg	dt_xlator_t *dxp;
1409253725Spfg
1410253725Spfg	int reg, treg;
1411253725Spfg	uint32_t instr;
1412253725Spfg	size_t size;
1413253725Spfg
1414253725Spfg	/* Generate code for the translation. */
1415253725Spfg	dxp = idp->di_data;
1416253725Spfg	mnp = dt_xlator_member(dxp, name);
1417253725Spfg
1418253725Spfg	/* If there's no translator for the given member, skip it. */
1419253725Spfg	if (mnp == NULL)
1420253725Spfg		return (0);
1421253725Spfg
1422253725Spfg	dxp->dx_ident->di_flags |= DT_IDFLG_CGREG;
1423253725Spfg	dxp->dx_ident->di_id = dx->dtxl_sreg;
1424253725Spfg
1425253725Spfg	dt_cg_node(mnp->dn_membexpr, dlp, drp);
1426253725Spfg
1427253725Spfg	dxp->dx_ident->di_flags &= ~DT_IDFLG_CGREG;
1428253725Spfg	dxp->dx_ident->di_id = 0;
1429253725Spfg
1430253725Spfg	treg = mnp->dn_membexpr->dn_reg;
1431253725Spfg
1432253725Spfg	/* Compute the offset into our buffer and store the result there. */
1433253725Spfg	reg = dt_regset_alloc(drp);
1434253725Spfg
1435253725Spfg	dt_cg_setx(dlp, reg, off / NBBY);
1436253725Spfg	instr = DIF_INSTR_FMT(DIF_OP_ADD, dx->dtxl_dreg, reg, reg);
1437253725Spfg	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
1438253725Spfg
1439253725Spfg	size = ctf_type_size(mnp->dn_membexpr->dn_ctfp,
1440253725Spfg	    mnp->dn_membexpr->dn_type);
1441253725Spfg	if (dt_node_is_scalar(mnp->dn_membexpr)) {
1442253725Spfg		/*
1443253725Spfg		 * Copying scalars is simple.
1444253725Spfg		 */
1445253725Spfg		switch (size) {
1446253725Spfg		case 1:
1447253725Spfg			instr = DIF_INSTR_STORE(DIF_OP_STB, treg, reg);
1448253725Spfg			break;
1449253725Spfg		case 2:
1450253725Spfg			instr = DIF_INSTR_STORE(DIF_OP_STH, treg, reg);
1451253725Spfg			break;
1452253725Spfg		case 4:
1453253725Spfg			instr = DIF_INSTR_STORE(DIF_OP_STW, treg, reg);
1454253725Spfg			break;
1455253725Spfg		case 8:
1456253725Spfg			instr = DIF_INSTR_STORE(DIF_OP_STX, treg, reg);
1457253725Spfg			break;
1458253725Spfg		default:
1459253725Spfg			xyerror(D_UNKNOWN, "internal error -- unexpected "
1460253725Spfg			    "size: %lu\n", (ulong_t)size);
1461253725Spfg		}
1462253725Spfg
1463253725Spfg		dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
1464253725Spfg
1465253725Spfg	} else if (dt_node_is_string(mnp->dn_membexpr)) {
1466253725Spfg		int szreg;
1467253725Spfg
1468253725Spfg		/*
1469253725Spfg		 * Use the copys instruction for strings.
1470253725Spfg		 */
1471253725Spfg		szreg = dt_regset_alloc(drp);
1472253725Spfg		dt_cg_setx(dlp, szreg, size);
1473253725Spfg		instr = DIF_INSTR_COPYS(treg, szreg, reg);
1474253725Spfg		dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
1475253725Spfg		dt_regset_free(drp, szreg);
1476253725Spfg	} else {
1477253725Spfg		int szreg;
1478253725Spfg
1479253725Spfg		/*
1480253725Spfg		 * If it's anything else then we'll just bcopy it.
1481253725Spfg		 */
1482253725Spfg		szreg = dt_regset_alloc(drp);
1483253725Spfg		dt_cg_setx(dlp, szreg, size);
1484253725Spfg		dt_irlist_append(dlp,
1485253725Spfg		    dt_cg_node_alloc(DT_LBL_NONE, DIF_INSTR_FLUSHTS));
1486253725Spfg		instr = DIF_INSTR_PUSHTS(DIF_OP_PUSHTV, DIF_TYPE_CTF,
1487253725Spfg		    DIF_REG_R0, treg);
1488253725Spfg		dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
1489253725Spfg		instr = DIF_INSTR_PUSHTS(DIF_OP_PUSHTV, DIF_TYPE_CTF,
1490253725Spfg		    DIF_REG_R0, reg);
1491253725Spfg		dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
1492253725Spfg		instr = DIF_INSTR_PUSHTS(DIF_OP_PUSHTV, DIF_TYPE_CTF,
1493253725Spfg		    DIF_REG_R0, szreg);
1494253725Spfg		dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
1495253725Spfg		instr = DIF_INSTR_CALL(DIF_SUBR_BCOPY, szreg);
1496253725Spfg		dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
1497253725Spfg		dt_regset_free(drp, szreg);
1498253725Spfg	}
1499253725Spfg
1500253725Spfg	dt_regset_free(drp, reg);
1501253725Spfg	dt_regset_free(drp, treg);
1502253725Spfg
1503253725Spfg	return (0);
1504253725Spfg}
1505253725Spfg
1506253725Spfg/*
1507253725Spfg * If we're expanding a translated type, we create an appropriately sized
1508253725Spfg * buffer with alloca() and then translate each member into it.
1509253725Spfg */
1510253725Spfgstatic int
1511253725Spfgdt_cg_xlate_expand(dt_node_t *dnp, dt_ident_t *idp, dt_irlist_t *dlp,
1512253725Spfg    dt_regset_t *drp)
1513253725Spfg{
1514253725Spfg	dt_xlmemb_t dlm;
1515253725Spfg	uint32_t instr;
1516253725Spfg	int dreg;
1517253725Spfg	size_t size;
1518253725Spfg
1519253725Spfg	dreg = dt_regset_alloc(drp);
1520253725Spfg	size = ctf_type_size(dnp->dn_ident->di_ctfp, dnp->dn_ident->di_type);
1521253725Spfg
1522253725Spfg	/* Call alloca() to create the buffer. */
1523253725Spfg	dt_cg_setx(dlp, dreg, size);
1524253725Spfg
1525253725Spfg	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, DIF_INSTR_FLUSHTS));
1526253725Spfg
1527253725Spfg	instr = DIF_INSTR_PUSHTS(DIF_OP_PUSHTV, DIF_TYPE_CTF, DIF_REG_R0, dreg);
1528253725Spfg	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
1529253725Spfg
1530253725Spfg	instr = DIF_INSTR_CALL(DIF_SUBR_ALLOCA, dreg);
1531253725Spfg	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
1532253725Spfg
1533253725Spfg	/* Generate the translation for each member. */
1534253725Spfg	dlm.dtxl_idp = idp;
1535253725Spfg	dlm.dtxl_dlp = dlp;
1536253725Spfg	dlm.dtxl_drp = drp;
1537253725Spfg	dlm.dtxl_sreg = dnp->dn_reg;
1538253725Spfg	dlm.dtxl_dreg = dreg;
1539253725Spfg	(void) ctf_member_iter(dnp->dn_ident->di_ctfp,
1540253725Spfg	    dnp->dn_ident->di_type, dt_cg_xlate_member,
1541253725Spfg	    &dlm);
1542253725Spfg
1543253725Spfg	return (dreg);
1544253725Spfg}
1545253725Spfg
1546178576Sjbstatic void
1547178479Sjbdt_cg_node(dt_node_t *dnp, dt_irlist_t *dlp, dt_regset_t *drp)
1548178479Sjb{
1549178479Sjb	ctf_file_t *ctfp = dnp->dn_ctfp;
1550178479Sjb	ctf_file_t *octfp;
1551178479Sjb	ctf_membinfo_t m;
1552178479Sjb	ctf_id_t type;
1553178479Sjb
1554178479Sjb	dif_instr_t instr;
1555178479Sjb	dt_ident_t *idp;
1556178479Sjb	ssize_t stroff;
1557178479Sjb	uint_t op;
1558178479Sjb
1559178479Sjb	switch (dnp->dn_op) {
1560178479Sjb	case DT_TOK_COMMA:
1561178479Sjb		dt_cg_node(dnp->dn_left, dlp, drp);
1562178479Sjb		dt_regset_free(drp, dnp->dn_left->dn_reg);
1563178479Sjb		dt_cg_node(dnp->dn_right, dlp, drp);
1564178479Sjb		dnp->dn_reg = dnp->dn_right->dn_reg;
1565178479Sjb		break;
1566178479Sjb
1567178479Sjb	case DT_TOK_ASGN:
1568178479Sjb		dt_cg_node(dnp->dn_right, dlp, drp);
1569178479Sjb		dnp->dn_reg = dnp->dn_right->dn_reg;
1570178479Sjb		dt_cg_asgn_op(dnp, dlp, drp);
1571178479Sjb		break;
1572178479Sjb
1573178479Sjb	case DT_TOK_ADD_EQ:
1574178479Sjb		dt_cg_arithmetic_op(dnp, dlp, drp, DIF_OP_ADD);
1575178479Sjb		dt_cg_asgn_op(dnp, dlp, drp);
1576178479Sjb		break;
1577178479Sjb
1578178479Sjb	case DT_TOK_SUB_EQ:
1579178479Sjb		dt_cg_arithmetic_op(dnp, dlp, drp, DIF_OP_SUB);
1580178479Sjb		dt_cg_asgn_op(dnp, dlp, drp);
1581178479Sjb		break;
1582178479Sjb
1583178479Sjb	case DT_TOK_MUL_EQ:
1584178479Sjb		dt_cg_arithmetic_op(dnp, dlp, drp, DIF_OP_MUL);
1585178479Sjb		dt_cg_asgn_op(dnp, dlp, drp);
1586178479Sjb		break;
1587178479Sjb
1588178479Sjb	case DT_TOK_DIV_EQ:
1589178479Sjb		dt_cg_arithmetic_op(dnp, dlp, drp,
1590178479Sjb		    (dnp->dn_flags & DT_NF_SIGNED) ? DIF_OP_SDIV : DIF_OP_UDIV);
1591178479Sjb		dt_cg_asgn_op(dnp, dlp, drp);
1592178479Sjb		break;
1593178479Sjb
1594178479Sjb	case DT_TOK_MOD_EQ:
1595178479Sjb		dt_cg_arithmetic_op(dnp, dlp, drp,
1596178479Sjb		    (dnp->dn_flags & DT_NF_SIGNED) ? DIF_OP_SREM : DIF_OP_UREM);
1597178479Sjb		dt_cg_asgn_op(dnp, dlp, drp);
1598178479Sjb		break;
1599178479Sjb
1600178479Sjb	case DT_TOK_AND_EQ:
1601178479Sjb		dt_cg_arithmetic_op(dnp, dlp, drp, DIF_OP_AND);
1602178479Sjb		dt_cg_asgn_op(dnp, dlp, drp);
1603178479Sjb		break;
1604178479Sjb
1605178479Sjb	case DT_TOK_XOR_EQ:
1606178479Sjb		dt_cg_arithmetic_op(dnp, dlp, drp, DIF_OP_XOR);
1607178479Sjb		dt_cg_asgn_op(dnp, dlp, drp);
1608178479Sjb		break;
1609178479Sjb
1610178479Sjb	case DT_TOK_OR_EQ:
1611178479Sjb		dt_cg_arithmetic_op(dnp, dlp, drp, DIF_OP_OR);
1612178479Sjb		dt_cg_asgn_op(dnp, dlp, drp);
1613178479Sjb		break;
1614178479Sjb
1615178479Sjb	case DT_TOK_LSH_EQ:
1616178479Sjb		dt_cg_arithmetic_op(dnp, dlp, drp, DIF_OP_SLL);
1617178479Sjb		dt_cg_asgn_op(dnp, dlp, drp);
1618178479Sjb		break;
1619178479Sjb
1620178479Sjb	case DT_TOK_RSH_EQ:
1621178479Sjb		dt_cg_arithmetic_op(dnp, dlp, drp,
1622178479Sjb		    (dnp->dn_flags & DT_NF_SIGNED) ? DIF_OP_SRA : DIF_OP_SRL);
1623178479Sjb		dt_cg_asgn_op(dnp, dlp, drp);
1624178479Sjb		break;
1625178479Sjb
1626178479Sjb	case DT_TOK_QUESTION:
1627178479Sjb		dt_cg_ternary_op(dnp, dlp, drp);
1628178479Sjb		break;
1629178479Sjb
1630178479Sjb	case DT_TOK_LOR:
1631178479Sjb		dt_cg_logical_or(dnp, dlp, drp);
1632178479Sjb		break;
1633178479Sjb
1634178479Sjb	case DT_TOK_LXOR:
1635178479Sjb		dt_cg_logical_xor(dnp, dlp, drp);
1636178479Sjb		break;
1637178479Sjb
1638178479Sjb	case DT_TOK_LAND:
1639178479Sjb		dt_cg_logical_and(dnp, dlp, drp);
1640178479Sjb		break;
1641178479Sjb
1642178479Sjb	case DT_TOK_BOR:
1643178479Sjb		dt_cg_arithmetic_op(dnp, dlp, drp, DIF_OP_OR);
1644178479Sjb		break;
1645178479Sjb
1646178479Sjb	case DT_TOK_XOR:
1647178479Sjb		dt_cg_arithmetic_op(dnp, dlp, drp, DIF_OP_XOR);
1648178479Sjb		break;
1649178479Sjb
1650178479Sjb	case DT_TOK_BAND:
1651178479Sjb		dt_cg_arithmetic_op(dnp, dlp, drp, DIF_OP_AND);
1652178479Sjb		break;
1653178479Sjb
1654178479Sjb	case DT_TOK_EQU:
1655178479Sjb		dt_cg_compare_op(dnp, dlp, drp, DIF_OP_BE);
1656178479Sjb		break;
1657178479Sjb
1658178479Sjb	case DT_TOK_NEQ:
1659178479Sjb		dt_cg_compare_op(dnp, dlp, drp, DIF_OP_BNE);
1660178479Sjb		break;
1661178479Sjb
1662178479Sjb	case DT_TOK_LT:
1663178479Sjb		dt_cg_compare_op(dnp, dlp, drp,
1664178479Sjb		    dt_cg_compare_signed(dnp) ? DIF_OP_BL : DIF_OP_BLU);
1665178479Sjb		break;
1666178479Sjb
1667178479Sjb	case DT_TOK_LE:
1668178479Sjb		dt_cg_compare_op(dnp, dlp, drp,
1669178479Sjb		    dt_cg_compare_signed(dnp) ? DIF_OP_BLE : DIF_OP_BLEU);
1670178479Sjb		break;
1671178479Sjb
1672178479Sjb	case DT_TOK_GT:
1673178479Sjb		dt_cg_compare_op(dnp, dlp, drp,
1674178479Sjb		    dt_cg_compare_signed(dnp) ? DIF_OP_BG : DIF_OP_BGU);
1675178479Sjb		break;
1676178479Sjb
1677178479Sjb	case DT_TOK_GE:
1678178479Sjb		dt_cg_compare_op(dnp, dlp, drp,
1679178479Sjb		    dt_cg_compare_signed(dnp) ? DIF_OP_BGE : DIF_OP_BGEU);
1680178479Sjb		break;
1681178479Sjb
1682178479Sjb	case DT_TOK_LSH:
1683178479Sjb		dt_cg_arithmetic_op(dnp, dlp, drp, DIF_OP_SLL);
1684178479Sjb		break;
1685178479Sjb
1686178479Sjb	case DT_TOK_RSH:
1687178479Sjb		dt_cg_arithmetic_op(dnp, dlp, drp,
1688178479Sjb		    (dnp->dn_flags & DT_NF_SIGNED) ? DIF_OP_SRA : DIF_OP_SRL);
1689178479Sjb		break;
1690178479Sjb
1691178479Sjb	case DT_TOK_ADD:
1692178479Sjb		dt_cg_arithmetic_op(dnp, dlp, drp, DIF_OP_ADD);
1693178479Sjb		break;
1694178479Sjb
1695178479Sjb	case DT_TOK_SUB:
1696178479Sjb		dt_cg_arithmetic_op(dnp, dlp, drp, DIF_OP_SUB);
1697178479Sjb		break;
1698178479Sjb
1699178479Sjb	case DT_TOK_MUL:
1700178479Sjb		dt_cg_arithmetic_op(dnp, dlp, drp, DIF_OP_MUL);
1701178479Sjb		break;
1702178479Sjb
1703178479Sjb	case DT_TOK_DIV:
1704178479Sjb		dt_cg_arithmetic_op(dnp, dlp, drp,
1705178479Sjb		    (dnp->dn_flags & DT_NF_SIGNED) ? DIF_OP_SDIV : DIF_OP_UDIV);
1706178479Sjb		break;
1707178479Sjb
1708178479Sjb	case DT_TOK_MOD:
1709178479Sjb		dt_cg_arithmetic_op(dnp, dlp, drp,
1710178479Sjb		    (dnp->dn_flags & DT_NF_SIGNED) ? DIF_OP_SREM : DIF_OP_UREM);
1711178479Sjb		break;
1712178479Sjb
1713178479Sjb	case DT_TOK_LNEG:
1714178479Sjb		dt_cg_logical_neg(dnp, dlp, drp);
1715178479Sjb		break;
1716178479Sjb
1717178479Sjb	case DT_TOK_BNEG:
1718178479Sjb		dt_cg_node(dnp->dn_child, dlp, drp);
1719178479Sjb		dnp->dn_reg = dnp->dn_child->dn_reg;
1720178479Sjb		instr = DIF_INSTR_NOT(dnp->dn_reg, dnp->dn_reg);
1721178479Sjb		dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
1722178479Sjb		break;
1723178479Sjb
1724178479Sjb	case DT_TOK_PREINC:
1725178479Sjb		dt_cg_prearith_op(dnp, dlp, drp, DIF_OP_ADD);
1726178479Sjb		break;
1727178479Sjb
1728178479Sjb	case DT_TOK_POSTINC:
1729178479Sjb		dt_cg_postarith_op(dnp, dlp, drp, DIF_OP_ADD);
1730178479Sjb		break;
1731178479Sjb
1732178479Sjb	case DT_TOK_PREDEC:
1733178479Sjb		dt_cg_prearith_op(dnp, dlp, drp, DIF_OP_SUB);
1734178479Sjb		break;
1735178479Sjb
1736178479Sjb	case DT_TOK_POSTDEC:
1737178479Sjb		dt_cg_postarith_op(dnp, dlp, drp, DIF_OP_SUB);
1738178479Sjb		break;
1739178479Sjb
1740178479Sjb	case DT_TOK_IPOS:
1741178479Sjb		dt_cg_node(dnp->dn_child, dlp, drp);
1742178479Sjb		dnp->dn_reg = dnp->dn_child->dn_reg;
1743178479Sjb		break;
1744178479Sjb
1745178479Sjb	case DT_TOK_INEG:
1746178479Sjb		dt_cg_node(dnp->dn_child, dlp, drp);
1747178479Sjb		dnp->dn_reg = dnp->dn_child->dn_reg;
1748178479Sjb
1749178479Sjb		instr = DIF_INSTR_FMT(DIF_OP_SUB, DIF_REG_R0,
1750178479Sjb		    dnp->dn_reg, dnp->dn_reg);
1751178479Sjb
1752178479Sjb		dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
1753178479Sjb		break;
1754178479Sjb
1755178479Sjb	case DT_TOK_DEREF:
1756178479Sjb		dt_cg_node(dnp->dn_child, dlp, drp);
1757178479Sjb		dnp->dn_reg = dnp->dn_child->dn_reg;
1758178479Sjb
1759253725Spfg		if (dt_node_is_dynamic(dnp->dn_child)) {
1760253725Spfg			int reg;
1761253725Spfg			idp = dt_node_resolve(dnp->dn_child, DT_IDENT_XLPTR);
1762253725Spfg			assert(idp != NULL);
1763253725Spfg			reg = dt_cg_xlate_expand(dnp, idp, dlp, drp);
1764253725Spfg
1765253725Spfg			dt_regset_free(drp, dnp->dn_child->dn_reg);
1766253725Spfg			dnp->dn_reg = reg;
1767253725Spfg
1768253725Spfg		} else if (!(dnp->dn_flags & DT_NF_REF)) {
1769178479Sjb			uint_t ubit = dnp->dn_flags & DT_NF_USERLAND;
1770178479Sjb
1771178479Sjb			/*
1772178479Sjb			 * Save and restore DT_NF_USERLAND across dt_cg_load():
1773178479Sjb			 * we need the sign bit from dnp and the user bit from
1774178479Sjb			 * dnp->dn_child in order to get the proper opcode.
1775178479Sjb			 */
1776178479Sjb			dnp->dn_flags |=
1777178479Sjb			    (dnp->dn_child->dn_flags & DT_NF_USERLAND);
1778178479Sjb
1779178479Sjb			instr = DIF_INSTR_LOAD(dt_cg_load(dnp, ctfp,
1780178479Sjb			    dnp->dn_type), dnp->dn_reg, dnp->dn_reg);
1781178479Sjb
1782178479Sjb			dnp->dn_flags &= ~DT_NF_USERLAND;
1783178479Sjb			dnp->dn_flags |= ubit;
1784178479Sjb
1785178479Sjb			dt_irlist_append(dlp,
1786178479Sjb			    dt_cg_node_alloc(DT_LBL_NONE, instr));
1787178479Sjb		}
1788178479Sjb		break;
1789178479Sjb
1790178479Sjb	case DT_TOK_ADDROF: {
1791178479Sjb		uint_t rbit = dnp->dn_child->dn_flags & DT_NF_REF;
1792178479Sjb
1793178479Sjb		dnp->dn_child->dn_flags |= DT_NF_REF; /* force pass-by-ref */
1794178479Sjb		dt_cg_node(dnp->dn_child, dlp, drp);
1795178479Sjb		dnp->dn_reg = dnp->dn_child->dn_reg;
1796178479Sjb
1797178479Sjb		dnp->dn_child->dn_flags &= ~DT_NF_REF;
1798178479Sjb		dnp->dn_child->dn_flags |= rbit;
1799178479Sjb		break;
1800178479Sjb	}
1801178479Sjb
1802178479Sjb	case DT_TOK_SIZEOF: {
1803178479Sjb		size_t size = dt_node_sizeof(dnp->dn_child);
1804250812Smarkj		dnp->dn_reg = dt_regset_alloc(drp);
1805178479Sjb		assert(size != 0);
1806178479Sjb		dt_cg_setx(dlp, dnp->dn_reg, size);
1807178479Sjb		break;
1808178479Sjb	}
1809178479Sjb
1810178479Sjb	case DT_TOK_STRINGOF:
1811178479Sjb		dt_cg_node(dnp->dn_child, dlp, drp);
1812178479Sjb		dnp->dn_reg = dnp->dn_child->dn_reg;
1813178479Sjb		break;
1814178479Sjb
1815178479Sjb	case DT_TOK_XLATE:
1816178479Sjb		/*
1817178479Sjb		 * An xlate operator appears in either an XLATOR, indicating a
1818178479Sjb		 * reference to a dynamic translator, or an OP2, indicating
1819178479Sjb		 * use of the xlate operator in the user's program.  For the
1820178479Sjb		 * dynamic case, generate an xlate opcode with a reference to
1821178479Sjb		 * the corresponding member, pre-computed for us in dn_members.
1822178479Sjb		 */
1823178479Sjb		if (dnp->dn_kind == DT_NODE_XLATOR) {
1824178479Sjb			dt_xlator_t *dxp = dnp->dn_xlator;
1825178479Sjb
1826178479Sjb			assert(dxp->dx_ident->di_flags & DT_IDFLG_CGREG);
1827178479Sjb			assert(dxp->dx_ident->di_id != 0);
1828178479Sjb
1829250812Smarkj			dnp->dn_reg = dt_regset_alloc(drp);
1830178479Sjb
1831178479Sjb			if (dxp->dx_arg == -1) {
1832178479Sjb				instr = DIF_INSTR_MOV(
1833178479Sjb				    dxp->dx_ident->di_id, dnp->dn_reg);
1834178479Sjb				dt_irlist_append(dlp,
1835178479Sjb				    dt_cg_node_alloc(DT_LBL_NONE, instr));
1836178479Sjb				op = DIF_OP_XLATE;
1837178479Sjb			} else
1838178479Sjb				op = DIF_OP_XLARG;
1839178479Sjb
1840178479Sjb			instr = DIF_INSTR_XLATE(op, 0, dnp->dn_reg);
1841178479Sjb			dt_irlist_append(dlp,
1842178479Sjb			    dt_cg_node_alloc(DT_LBL_NONE, instr));
1843178479Sjb
1844178479Sjb			dlp->dl_last->di_extern = dnp->dn_xmember;
1845178479Sjb			break;
1846178479Sjb		}
1847178479Sjb
1848178479Sjb		assert(dnp->dn_kind == DT_NODE_OP2);
1849178479Sjb		dt_cg_node(dnp->dn_right, dlp, drp);
1850178479Sjb		dnp->dn_reg = dnp->dn_right->dn_reg;
1851178479Sjb		break;
1852178479Sjb
1853178479Sjb	case DT_TOK_LPAR:
1854178479Sjb		dt_cg_node(dnp->dn_right, dlp, drp);
1855178479Sjb		dnp->dn_reg = dnp->dn_right->dn_reg;
1856178479Sjb		dt_cg_typecast(dnp->dn_right, dnp, dlp, drp);
1857178479Sjb		break;
1858178479Sjb
1859178479Sjb	case DT_TOK_PTR:
1860178479Sjb	case DT_TOK_DOT:
1861178479Sjb		assert(dnp->dn_right->dn_kind == DT_NODE_IDENT);
1862178479Sjb		dt_cg_node(dnp->dn_left, dlp, drp);
1863178479Sjb
1864178479Sjb		/*
1865178479Sjb		 * If the left-hand side of PTR or DOT is a dynamic variable,
1866178479Sjb		 * we expect it to be the output of a D translator.   In this
1867178479Sjb		 * case, we look up the parse tree corresponding to the member
1868178479Sjb		 * that is being accessed and run the code generator over it.
1869178479Sjb		 * We then cast the result as if by the assignment operator.
1870178479Sjb		 */
1871178479Sjb		if ((idp = dt_node_resolve(
1872178479Sjb		    dnp->dn_left, DT_IDENT_XLSOU)) != NULL ||
1873178479Sjb		    (idp = dt_node_resolve(
1874178479Sjb		    dnp->dn_left, DT_IDENT_XLPTR)) != NULL) {
1875178479Sjb
1876178479Sjb			dt_xlator_t *dxp;
1877178479Sjb			dt_node_t *mnp;
1878178479Sjb
1879178479Sjb			dxp = idp->di_data;
1880178479Sjb			mnp = dt_xlator_member(dxp, dnp->dn_right->dn_string);
1881178479Sjb			assert(mnp != NULL);
1882178479Sjb
1883178479Sjb			dxp->dx_ident->di_flags |= DT_IDFLG_CGREG;
1884178479Sjb			dxp->dx_ident->di_id = dnp->dn_left->dn_reg;
1885178479Sjb
1886178479Sjb			dt_cg_node(mnp->dn_membexpr, dlp, drp);
1887178479Sjb			dnp->dn_reg = mnp->dn_membexpr->dn_reg;
1888178479Sjb			dt_cg_typecast(mnp->dn_membexpr, dnp, dlp, drp);
1889178479Sjb
1890178479Sjb			dxp->dx_ident->di_flags &= ~DT_IDFLG_CGREG;
1891178479Sjb			dxp->dx_ident->di_id = 0;
1892178479Sjb
1893178479Sjb			if (dnp->dn_left->dn_reg != -1)
1894178479Sjb				dt_regset_free(drp, dnp->dn_left->dn_reg);
1895178479Sjb			break;
1896178479Sjb		}
1897178479Sjb
1898178479Sjb		ctfp = dnp->dn_left->dn_ctfp;
1899178479Sjb		type = ctf_type_resolve(ctfp, dnp->dn_left->dn_type);
1900178479Sjb
1901178479Sjb		if (dnp->dn_op == DT_TOK_PTR) {
1902178479Sjb			type = ctf_type_reference(ctfp, type);
1903178479Sjb			type = ctf_type_resolve(ctfp, type);
1904178479Sjb		}
1905178479Sjb
1906178479Sjb		if ((ctfp = dt_cg_membinfo(octfp = ctfp, type,
1907178479Sjb		    dnp->dn_right->dn_string, &m)) == NULL) {
1908178479Sjb			yypcb->pcb_hdl->dt_ctferr = ctf_errno(octfp);
1909178479Sjb			longjmp(yypcb->pcb_jmpbuf, EDT_CTF);
1910178479Sjb		}
1911178479Sjb
1912178479Sjb		if (m.ctm_offset != 0) {
1913250812Smarkj			int reg;
1914178479Sjb
1915250812Smarkj			reg = dt_regset_alloc(drp);
1916250812Smarkj
1917178479Sjb			/*
1918178479Sjb			 * If the offset is not aligned on a byte boundary, it
1919178479Sjb			 * is a bit-field member and we will extract the value
1920178479Sjb			 * bits below after we generate the appropriate load.
1921178479Sjb			 */
1922178479Sjb			dt_cg_setx(dlp, reg, m.ctm_offset / NBBY);
1923178479Sjb
1924178479Sjb			instr = DIF_INSTR_FMT(DIF_OP_ADD,
1925178479Sjb			    dnp->dn_left->dn_reg, reg, dnp->dn_left->dn_reg);
1926178479Sjb
1927178479Sjb			dt_irlist_append(dlp,
1928178479Sjb			    dt_cg_node_alloc(DT_LBL_NONE, instr));
1929178479Sjb			dt_regset_free(drp, reg);
1930178479Sjb		}
1931178479Sjb
1932178479Sjb		if (!(dnp->dn_flags & DT_NF_REF)) {
1933178479Sjb			uint_t ubit = dnp->dn_flags & DT_NF_USERLAND;
1934178479Sjb
1935178479Sjb			/*
1936178479Sjb			 * Save and restore DT_NF_USERLAND across dt_cg_load():
1937178479Sjb			 * we need the sign bit from dnp and the user bit from
1938178479Sjb			 * dnp->dn_left in order to get the proper opcode.
1939178479Sjb			 */
1940178479Sjb			dnp->dn_flags |=
1941178479Sjb			    (dnp->dn_left->dn_flags & DT_NF_USERLAND);
1942178479Sjb
1943178479Sjb			instr = DIF_INSTR_LOAD(dt_cg_load(dnp,
1944178479Sjb			    ctfp, m.ctm_type), dnp->dn_left->dn_reg,
1945178479Sjb			    dnp->dn_left->dn_reg);
1946178479Sjb
1947178479Sjb			dnp->dn_flags &= ~DT_NF_USERLAND;
1948178479Sjb			dnp->dn_flags |= ubit;
1949178479Sjb
1950178479Sjb			dt_irlist_append(dlp,
1951178479Sjb			    dt_cg_node_alloc(DT_LBL_NONE, instr));
1952178479Sjb
1953178479Sjb			if (dnp->dn_flags & DT_NF_BITFIELD)
1954178479Sjb				dt_cg_field_get(dnp, dlp, drp, ctfp, &m);
1955178479Sjb		}
1956178479Sjb
1957178479Sjb		dnp->dn_reg = dnp->dn_left->dn_reg;
1958178479Sjb		break;
1959178479Sjb
1960178479Sjb	case DT_TOK_STRING:
1961250812Smarkj		dnp->dn_reg = dt_regset_alloc(drp);
1962178479Sjb
1963178479Sjb		assert(dnp->dn_kind == DT_NODE_STRING);
1964178479Sjb		stroff = dt_strtab_insert(yypcb->pcb_strtab, dnp->dn_string);
1965178479Sjb
1966178479Sjb		if (stroff == -1L)
1967178479Sjb			longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
1968178479Sjb		if (stroff > DIF_STROFF_MAX)
1969178479Sjb			longjmp(yypcb->pcb_jmpbuf, EDT_STR2BIG);
1970178479Sjb
1971178479Sjb		instr = DIF_INSTR_SETS((ulong_t)stroff, dnp->dn_reg);
1972178479Sjb		dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
1973178479Sjb		break;
1974178479Sjb
1975178479Sjb	case DT_TOK_IDENT:
1976178479Sjb		/*
1977178479Sjb		 * If the specified identifier is a variable on which we have
1978178479Sjb		 * set the code generator register flag, then this variable
1979178479Sjb		 * has already had code generated for it and saved in di_id.
1980178479Sjb		 * Allocate a new register and copy the existing value to it.
1981178479Sjb		 */
1982178479Sjb		if (dnp->dn_kind == DT_NODE_VAR &&
1983178479Sjb		    (dnp->dn_ident->di_flags & DT_IDFLG_CGREG)) {
1984250812Smarkj			dnp->dn_reg = dt_regset_alloc(drp);
1985178479Sjb			instr = DIF_INSTR_MOV(dnp->dn_ident->di_id,
1986178479Sjb			    dnp->dn_reg);
1987178479Sjb			dt_irlist_append(dlp,
1988178479Sjb			    dt_cg_node_alloc(DT_LBL_NONE, instr));
1989178479Sjb			break;
1990178479Sjb		}
1991178479Sjb
1992178479Sjb		/*
1993178479Sjb		 * Identifiers can represent function calls, variable refs, or
1994178479Sjb		 * symbols.  First we check for inlined variables, and handle
1995178479Sjb		 * them by generating code for the inline parse tree.
1996178479Sjb		 */
1997178479Sjb		if (dnp->dn_kind == DT_NODE_VAR &&
1998178479Sjb		    (dnp->dn_ident->di_flags & DT_IDFLG_INLINE)) {
1999178479Sjb			dt_cg_inline(dnp, dlp, drp);
2000178479Sjb			break;
2001178479Sjb		}
2002178479Sjb
2003178479Sjb		switch (dnp->dn_kind) {
2004178576Sjb		case DT_NODE_FUNC: {
2005178576Sjb			dtrace_hdl_t *dtp = yypcb->pcb_hdl;
2006178576Sjb
2007178479Sjb			if ((idp = dnp->dn_ident)->di_kind != DT_IDENT_FUNC) {
2008178479Sjb				dnerror(dnp, D_CG_EXPR, "%s %s( ) may not be "
2009178479Sjb				    "called from a D expression (D program "
2010178479Sjb				    "context required)\n",
2011178479Sjb				    dt_idkind_name(idp->di_kind), idp->di_name);
2012178479Sjb			}
2013178479Sjb
2014178576Sjb			switch (idp->di_id) {
2015178576Sjb			case DIF_SUBR_TYPEREF:
2016178576Sjb				dt_cg_func_typeref(dtp, dnp);
2017178576Sjb				break;
2018178576Sjb
2019178576Sjb			default:
2020178576Sjb				break;
2021178576Sjb			}
2022178576Sjb
2023178479Sjb			dt_cg_arglist(dnp->dn_ident, dnp->dn_args, dlp, drp);
2024178479Sjb
2025250812Smarkj			dnp->dn_reg = dt_regset_alloc(drp);
2026250812Smarkj			instr = DIF_INSTR_CALL(dnp->dn_ident->di_id,
2027250812Smarkj			    dnp->dn_reg);
2028178479Sjb
2029178479Sjb			dt_irlist_append(dlp,
2030178479Sjb			    dt_cg_node_alloc(DT_LBL_NONE, instr));
2031178479Sjb
2032178479Sjb			break;
2033178576Sjb		}
2034178479Sjb
2035178479Sjb		case DT_NODE_VAR:
2036178479Sjb			if (dnp->dn_ident->di_kind == DT_IDENT_XLSOU ||
2037178479Sjb			    dnp->dn_ident->di_kind == DT_IDENT_XLPTR) {
2038178479Sjb				/*
2039178479Sjb				 * This can only happen if we have translated
2040178479Sjb				 * args[].  See dt_idcook_args() for details.
2041178479Sjb				 */
2042178479Sjb				assert(dnp->dn_ident->di_id == DIF_VAR_ARGS);
2043178479Sjb				dt_cg_array_op(dnp, dlp, drp);
2044178479Sjb				break;
2045178479Sjb			}
2046178479Sjb
2047178479Sjb			if (dnp->dn_ident->di_kind == DT_IDENT_ARRAY) {
2048178479Sjb				if (dnp->dn_ident->di_id > DIF_VAR_ARRAY_MAX)
2049178479Sjb					dt_cg_assoc_op(dnp, dlp, drp);
2050178479Sjb				else
2051178479Sjb					dt_cg_array_op(dnp, dlp, drp);
2052178479Sjb				break;
2053178479Sjb			}
2054178479Sjb
2055250812Smarkj			dnp->dn_reg = dt_regset_alloc(drp);
2056178479Sjb
2057178479Sjb			if (dnp->dn_ident->di_flags & DT_IDFLG_LOCAL)
2058178479Sjb				op = DIF_OP_LDLS;
2059178479Sjb			else if (dnp->dn_ident->di_flags & DT_IDFLG_TLS)
2060178479Sjb				op = DIF_OP_LDTS;
2061178479Sjb			else
2062178479Sjb				op = DIF_OP_LDGS;
2063178479Sjb
2064178479Sjb			dnp->dn_ident->di_flags |= DT_IDFLG_DIFR;
2065178479Sjb
2066178479Sjb			instr = DIF_INSTR_LDV(op,
2067178479Sjb			    dnp->dn_ident->di_id, dnp->dn_reg);
2068178479Sjb
2069178479Sjb			dt_irlist_append(dlp,
2070178479Sjb			    dt_cg_node_alloc(DT_LBL_NONE, instr));
2071178479Sjb			break;
2072178479Sjb
2073178479Sjb		case DT_NODE_SYM: {
2074178479Sjb			dtrace_hdl_t *dtp = yypcb->pcb_hdl;
2075178479Sjb			dtrace_syminfo_t *sip = dnp->dn_ident->di_data;
2076178479Sjb			GElf_Sym sym;
2077178479Sjb
2078178479Sjb			if (dtrace_lookup_by_name(dtp,
2079178479Sjb			    sip->dts_object, sip->dts_name, &sym, NULL) == -1) {
2080178479Sjb				xyerror(D_UNKNOWN, "cg failed for symbol %s`%s:"
2081178479Sjb				    " %s\n", sip->dts_object, sip->dts_name,
2082178479Sjb				    dtrace_errmsg(dtp, dtrace_errno(dtp)));
2083178479Sjb			}
2084178479Sjb
2085250812Smarkj			dnp->dn_reg = dt_regset_alloc(drp);
2086178479Sjb			dt_cg_xsetx(dlp, dnp->dn_ident,
2087178479Sjb			    DT_LBL_NONE, dnp->dn_reg, sym.st_value);
2088178479Sjb
2089178479Sjb			if (!(dnp->dn_flags & DT_NF_REF)) {
2090178479Sjb				instr = DIF_INSTR_LOAD(dt_cg_load(dnp, ctfp,
2091178479Sjb				    dnp->dn_type), dnp->dn_reg, dnp->dn_reg);
2092178479Sjb				dt_irlist_append(dlp,
2093178479Sjb				    dt_cg_node_alloc(DT_LBL_NONE, instr));
2094178479Sjb			}
2095178479Sjb			break;
2096178479Sjb		}
2097178479Sjb
2098178479Sjb		default:
2099178479Sjb			xyerror(D_UNKNOWN, "internal error -- node type %u is "
2100178479Sjb			    "not valid for an identifier\n", dnp->dn_kind);
2101178479Sjb		}
2102178479Sjb		break;
2103178479Sjb
2104178479Sjb	case DT_TOK_INT:
2105250812Smarkj		dnp->dn_reg = dt_regset_alloc(drp);
2106178479Sjb		dt_cg_setx(dlp, dnp->dn_reg, dnp->dn_value);
2107178479Sjb		break;
2108178479Sjb
2109178479Sjb	default:
2110178479Sjb		xyerror(D_UNKNOWN, "internal error -- token type %u is not a "
2111178479Sjb		    "valid D compilation token\n", dnp->dn_op);
2112178479Sjb	}
2113178479Sjb}
2114178479Sjb
2115178479Sjbvoid
2116178479Sjbdt_cg(dt_pcb_t *pcb, dt_node_t *dnp)
2117178479Sjb{
2118178479Sjb	dif_instr_t instr;
2119178479Sjb	dt_xlator_t *dxp;
2120250812Smarkj	dt_ident_t *idp;
2121178479Sjb
2122178479Sjb	if (pcb->pcb_regs == NULL && (pcb->pcb_regs =
2123178479Sjb	    dt_regset_create(pcb->pcb_hdl->dt_conf.dtc_difintregs)) == NULL)
2124178479Sjb		longjmp(pcb->pcb_jmpbuf, EDT_NOMEM);
2125178479Sjb
2126178479Sjb	dt_regset_reset(pcb->pcb_regs);
2127178479Sjb	(void) dt_regset_alloc(pcb->pcb_regs); /* allocate %r0 */
2128178479Sjb
2129178479Sjb	if (pcb->pcb_inttab != NULL)
2130178479Sjb		dt_inttab_destroy(pcb->pcb_inttab);
2131178479Sjb
2132178479Sjb	if ((pcb->pcb_inttab = dt_inttab_create(yypcb->pcb_hdl)) == NULL)
2133178479Sjb		longjmp(pcb->pcb_jmpbuf, EDT_NOMEM);
2134178479Sjb
2135178479Sjb	if (pcb->pcb_strtab != NULL)
2136178479Sjb		dt_strtab_destroy(pcb->pcb_strtab);
2137178479Sjb
2138178479Sjb	if ((pcb->pcb_strtab = dt_strtab_create(BUFSIZ)) == NULL)
2139178479Sjb		longjmp(pcb->pcb_jmpbuf, EDT_NOMEM);
2140178479Sjb
2141178479Sjb	dt_irlist_destroy(&pcb->pcb_ir);
2142178479Sjb	dt_irlist_create(&pcb->pcb_ir);
2143178479Sjb
2144178479Sjb	assert(pcb->pcb_dret == NULL);
2145178479Sjb	pcb->pcb_dret = dnp;
2146178479Sjb
2147250812Smarkj	if (dt_node_resolve(dnp, DT_IDENT_XLPTR) != NULL) {
2148178479Sjb		dnerror(dnp, D_CG_DYN, "expression cannot evaluate to result "
2149250812Smarkj		    "of a translated pointer\n");
2150178479Sjb	}
2151178479Sjb
2152178479Sjb	/*
2153178479Sjb	 * If we're generating code for a translator body, assign the input
2154178479Sjb	 * parameter to the first available register (i.e. caller passes %r1).
2155178479Sjb	 */
2156178479Sjb	if (dnp->dn_kind == DT_NODE_MEMBER) {
2157178479Sjb		dxp = dnp->dn_membxlator;
2158178479Sjb		dnp = dnp->dn_membexpr;
2159178479Sjb
2160178479Sjb		dxp->dx_ident->di_flags |= DT_IDFLG_CGREG;
2161178479Sjb		dxp->dx_ident->di_id = dt_regset_alloc(pcb->pcb_regs);
2162178479Sjb	}
2163178479Sjb
2164178479Sjb	dt_cg_node(dnp, &pcb->pcb_ir, pcb->pcb_regs);
2165250812Smarkj
2166253725Spfg	if ((idp = dt_node_resolve(dnp, DT_IDENT_XLSOU)) != NULL) {
2167253725Spfg		int reg = dt_cg_xlate_expand(dnp, idp,
2168253725Spfg		    &pcb->pcb_ir, pcb->pcb_regs);
2169253725Spfg		dt_regset_free(pcb->pcb_regs, dnp->dn_reg);
2170253725Spfg		dnp->dn_reg = reg;
2171253725Spfg	}
2172253725Spfg
2173178479Sjb	instr = DIF_INSTR_RET(dnp->dn_reg);
2174178479Sjb	dt_regset_free(pcb->pcb_regs, dnp->dn_reg);
2175178479Sjb	dt_irlist_append(&pcb->pcb_ir, dt_cg_node_alloc(DT_LBL_NONE, instr));
2176178479Sjb
2177178479Sjb	if (dnp->dn_kind == DT_NODE_MEMBER) {
2178178479Sjb		dt_regset_free(pcb->pcb_regs, dxp->dx_ident->di_id);
2179178479Sjb		dxp->dx_ident->di_id = 0;
2180178479Sjb		dxp->dx_ident->di_flags &= ~DT_IDFLG_CGREG;
2181178479Sjb	}
2182250812Smarkj
2183250812Smarkj	dt_regset_free(pcb->pcb_regs, 0);
2184250812Smarkj	dt_regset_assert_free(pcb->pcb_regs);
2185178479Sjb}
2186