xref: /netbsd-src/external/cddl/osnet/dist/lib/libdtrace/common/dt_cg.c (revision e89934bbf778a6d6d6894877c4da59d0c7835b0f)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 
23 /*
24  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 
28 /*
29  * Copyright (c) 2012 by Delphix. All rights reserved.
30  */
31 
32 #include <sys/types.h>
33 #include <sys/sysmacros.h>
34 #include <sys/isa_defs.h>
35 
36 #include <strings.h>
37 #include <stdlib.h>
38 #include <setjmp.h>
39 #include <assert.h>
40 #include <errno.h>
41 
42 #include <dt_impl.h>
43 #include <dt_grammar.h>
44 #include <dt_parser.h>
45 #include <dt_provider.h>
46 
47 static void dt_cg_node(dt_node_t *, dt_irlist_t *, dt_regset_t *);
48 
49 static dt_irnode_t *
50 dt_cg_node_alloc(uint_t label, dif_instr_t instr)
51 {
52 	dt_irnode_t *dip = malloc(sizeof (dt_irnode_t));
53 
54 	if (dip == NULL)
55 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
56 
57 	dip->di_label = label;
58 	dip->di_instr = instr;
59 	dip->di_extern = NULL;
60 	dip->di_next = NULL;
61 
62 	return (dip);
63 }
64 
65 /*
66  * Code generator wrapper function for ctf_member_info.  If we are given a
67  * reference to a forward declaration tag, search the entire type space for
68  * the actual definition and then call ctf_member_info on the result.
69  */
70 static ctf_file_t *
71 dt_cg_membinfo(ctf_file_t *fp, ctf_id_t type, const char *s, ctf_membinfo_t *mp)
72 {
73 	while (ctf_type_kind(fp, type) == CTF_K_FORWARD) {
74 		char n[DT_TYPE_NAMELEN];
75 		dtrace_typeinfo_t dtt;
76 
77 		if (ctf_type_name(fp, type, n, sizeof (n)) == NULL ||
78 		    dt_type_lookup(n, &dtt) == -1 || (
79 		    dtt.dtt_ctfp == fp && dtt.dtt_type == type))
80 			break; /* unable to improve our position */
81 
82 		fp = dtt.dtt_ctfp;
83 		type = ctf_type_resolve(fp, dtt.dtt_type);
84 	}
85 
86 	if (ctf_member_info(fp, type, s, mp) == CTF_ERR)
87 		return (NULL); /* ctf_errno is set for us */
88 
89 	return (fp);
90 }
91 
92 static void
93 dt_cg_xsetx(dt_irlist_t *dlp, dt_ident_t *idp, uint_t lbl, int reg, uint64_t x)
94 {
95 	int flag = idp != NULL ? DT_INT_PRIVATE : DT_INT_SHARED;
96 	int intoff = dt_inttab_insert(yypcb->pcb_inttab, x, flag);
97 	dif_instr_t instr = DIF_INSTR_SETX((uint_t)intoff, reg);
98 
99 	if (intoff == -1)
100 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
101 
102 	if (intoff > DIF_INTOFF_MAX)
103 		longjmp(yypcb->pcb_jmpbuf, EDT_INT2BIG);
104 
105 	dt_irlist_append(dlp, dt_cg_node_alloc(lbl, instr));
106 
107 	if (idp != NULL)
108 		dlp->dl_last->di_extern = idp;
109 }
110 
111 static void
112 dt_cg_setx(dt_irlist_t *dlp, int reg, uint64_t x)
113 {
114 	dt_cg_xsetx(dlp, NULL, DT_LBL_NONE, reg, x);
115 }
116 
117 /*
118  * When loading bit-fields, we want to convert a byte count in the range
119  * 1-8 to the closest power of 2 (e.g. 3->4, 5->8, etc).  The clp2() function
120  * is a clever implementation from "Hacker's Delight" by Henry Warren, Jr.
121  */
122 static size_t
123 clp2(size_t x)
124 {
125 	x--;
126 
127 	x |= (x >> 1);
128 	x |= (x >> 2);
129 	x |= (x >> 4);
130 	x |= (x >> 8);
131 	x |= (x >> 16);
132 
133 	return (x + 1);
134 }
135 
136 /*
137  * Lookup the correct load opcode to use for the specified node and CTF type.
138  * We determine the size and convert it to a 3-bit index.  Our lookup table
139  * is constructed to use a 5-bit index, consisting of the 3-bit size 0-7, a
140  * bit for the sign, and a bit for userland address.  For example, a 4-byte
141  * signed load from userland would be at the following table index:
142  * user=1 sign=1 size=4 => binary index 11011 = decimal index 27
143  */
144 static uint_t
145 dt_cg_load(dt_node_t *dnp, ctf_file_t *ctfp, ctf_id_t type)
146 {
147 	static const uint_t ops[] = {
148 		DIF_OP_LDUB,	DIF_OP_LDUH,	0,	DIF_OP_LDUW,
149 		0,		0,		0,	DIF_OP_LDX,
150 		DIF_OP_LDSB,	DIF_OP_LDSH,	0,	DIF_OP_LDSW,
151 		0,		0,		0,	DIF_OP_LDX,
152 		DIF_OP_ULDUB,	DIF_OP_ULDUH,	0,	DIF_OP_ULDUW,
153 		0,		0,		0,	DIF_OP_ULDX,
154 		DIF_OP_ULDSB,	DIF_OP_ULDSH,	0,	DIF_OP_ULDSW,
155 		0,		0,		0,	DIF_OP_ULDX,
156 	};
157 
158 	ctf_encoding_t e;
159 	ssize_t size;
160 
161 	/*
162 	 * If we're loading a bit-field, the size of our load is found by
163 	 * rounding cte_bits up to a byte boundary and then finding the
164 	 * nearest power of two to this value (see clp2(), above).
165 	 */
166 	if ((dnp->dn_flags & DT_NF_BITFIELD) &&
167 	    ctf_type_encoding(ctfp, type, &e) != CTF_ERR)
168 		size = clp2(P2ROUNDUP(e.cte_bits, NBBY) / NBBY);
169 	else
170 		size = ctf_type_size(ctfp, type);
171 
172 	if (size < 1 || size > 8 || (size & (size - 1)) != 0) {
173 		xyerror(D_UNKNOWN, "internal error -- cg cannot load "
174 		    "size %ld when passed by value\n", (long)size);
175 	}
176 
177 	size--; /* convert size to 3-bit index */
178 
179 	if (dnp->dn_flags & DT_NF_SIGNED)
180 		size |= 0x08;
181 	if (dnp->dn_flags & DT_NF_USERLAND)
182 		size |= 0x10;
183 
184 	return (ops[size]);
185 }
186 
187 static void
188 dt_cg_ptrsize(dt_node_t *dnp, dt_irlist_t *dlp, dt_regset_t *drp,
189     uint_t op, int dreg)
190 {
191 	ctf_file_t *ctfp = dnp->dn_ctfp;
192 	ctf_arinfo_t r;
193 	dif_instr_t instr;
194 	ctf_id_t type;
195 	uint_t kind;
196 	ssize_t size;
197 	int sreg;
198 
199 	type = ctf_type_resolve(ctfp, dnp->dn_type);
200 	kind = ctf_type_kind(ctfp, type);
201 	assert(kind == CTF_K_POINTER || kind == CTF_K_ARRAY);
202 
203 	if (kind == CTF_K_ARRAY) {
204 		if (ctf_array_info(ctfp, type, &r) != 0) {
205 			yypcb->pcb_hdl->dt_ctferr = ctf_errno(ctfp);
206 			longjmp(yypcb->pcb_jmpbuf, EDT_CTF);
207 		}
208 		type = r.ctr_contents;
209 	} else
210 		type = ctf_type_reference(ctfp, type);
211 
212 	if ((size = ctf_type_size(ctfp, type)) == 1)
213 		return; /* multiply or divide by one can be omitted */
214 
215 	sreg = dt_regset_alloc(drp);
216 	dt_cg_setx(dlp, sreg, size);
217 	instr = DIF_INSTR_FMT(op, dreg, sreg, dreg);
218 	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
219 	dt_regset_free(drp, sreg);
220 }
221 
222 /*
223  * If the result of a "." or "->" operation is a bit-field, we use this routine
224  * to generate an epilogue to the load instruction that extracts the value.  In
225  * the diagrams below the "ld??" is the load instruction that is generated to
226  * load the containing word that is generating prior to calling this function.
227  *
228  * Epilogue for unsigned fields:	Epilogue for signed fields:
229  *
230  * ldu?	[r1], r1			lds? [r1], r1
231  * setx	USHIFT, r2			setx 64 - SSHIFT, r2
232  * srl	r1, r2, r1			sll  r1, r2, r1
233  * setx	(1 << bits) - 1, r2		setx 64 - bits, r2
234  * and	r1, r2, r1			sra  r1, r2, r1
235  *
236  * The *SHIFT constants above changes value depending on the endian-ness of our
237  * target architecture.  Refer to the comments below for more details.
238  */
239 static void
240 dt_cg_field_get(dt_node_t *dnp, dt_irlist_t *dlp, dt_regset_t *drp,
241     ctf_file_t *fp, const ctf_membinfo_t *mp)
242 {
243 	ctf_encoding_t e;
244 	dif_instr_t instr;
245 	uint64_t shift;
246 	int r1, r2;
247 
248 	if (ctf_type_encoding(fp, mp->ctm_type, &e) != 0 || e.cte_bits > 64) {
249 		xyerror(D_UNKNOWN, "cg: bad field: off %lu type <%ld> "
250 		    "bits %u\n", mp->ctm_offset, mp->ctm_type, e.cte_bits);
251 	}
252 
253 	assert(dnp->dn_op == DT_TOK_PTR || dnp->dn_op == DT_TOK_DOT);
254 	r1 = dnp->dn_left->dn_reg;
255 	r2 = dt_regset_alloc(drp);
256 
257 	/*
258 	 * On little-endian architectures, ctm_offset counts from the right so
259 	 * ctm_offset % NBBY itself is the amount we want to shift right to
260 	 * move the value bits to the little end of the register to mask them.
261 	 * On big-endian architectures, ctm_offset counts from the left so we
262 	 * must subtract (ctm_offset % NBBY + cte_bits) from the size in bits
263 	 * we used for the load.  The size of our load in turn is found by
264 	 * rounding cte_bits up to a byte boundary and then finding the
265 	 * nearest power of two to this value (see clp2(), above).  These
266 	 * properties are used to compute shift as USHIFT or SSHIFT, below.
267 	 */
268 	if (dnp->dn_flags & DT_NF_SIGNED) {
269 #if BYTE_ORDER == _BIG_ENDIAN
270 		shift = clp2(P2ROUNDUP(e.cte_bits, NBBY) / NBBY) * NBBY -
271 		    mp->ctm_offset % NBBY;
272 #else
273 		shift = mp->ctm_offset % NBBY + e.cte_bits;
274 #endif
275 		dt_cg_setx(dlp, r2, 64 - shift);
276 		instr = DIF_INSTR_FMT(DIF_OP_SLL, r1, r2, r1);
277 		dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
278 
279 		dt_cg_setx(dlp, r2, 64 - e.cte_bits);
280 		instr = DIF_INSTR_FMT(DIF_OP_SRA, r1, r2, r1);
281 		dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
282 	} else {
283 #if BYTE_ORDER == _BIG_ENDIAN
284 		shift = clp2(P2ROUNDUP(e.cte_bits, NBBY) / NBBY) * NBBY -
285 		    (mp->ctm_offset % NBBY + e.cte_bits);
286 #else
287 		shift = mp->ctm_offset % NBBY;
288 #endif
289 		dt_cg_setx(dlp, r2, shift);
290 		instr = DIF_INSTR_FMT(DIF_OP_SRL, r1, r2, r1);
291 		dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
292 
293 		dt_cg_setx(dlp, r2, (1ULL << e.cte_bits) - 1);
294 		instr = DIF_INSTR_FMT(DIF_OP_AND, r1, r2, r1);
295 		dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
296 	}
297 
298 	dt_regset_free(drp, r2);
299 }
300 
301 /*
302  * If the destination of a store operation is a bit-field, we use this routine
303  * to generate a prologue to the store instruction that loads the surrounding
304  * bits, clears the destination field, and ORs in the new value of the field.
305  * In the diagram below the "st?" is the store instruction that is generated to
306  * store the containing word that is generating after calling this function.
307  *
308  * ld	[dst->dn_reg], r1
309  * setx	~(((1 << cte_bits) - 1) << (ctm_offset % NBBY)), r2
310  * and	r1, r2, r1
311  *
312  * setx	(1 << cte_bits) - 1, r2
313  * and	src->dn_reg, r2, r2
314  * setx ctm_offset % NBBY, r3
315  * sll	r2, r3, r2
316  *
317  * or	r1, r2, r1
318  * st?	r1, [dst->dn_reg]
319  *
320  * This routine allocates a new register to hold the value to be stored and
321  * returns it.  The caller is responsible for freeing this register later.
322  */
323 static int
324 dt_cg_field_set(dt_node_t *src, dt_irlist_t *dlp,
325     dt_regset_t *drp, dt_node_t *dst)
326 {
327 	uint64_t cmask, fmask, shift;
328 	dif_instr_t instr;
329 	int r1, r2, r3;
330 
331 	ctf_membinfo_t m;
332 	ctf_encoding_t e;
333 	ctf_file_t *fp, *ofp;
334 	ctf_id_t type;
335 
336 	assert(dst->dn_op == DT_TOK_PTR || dst->dn_op == DT_TOK_DOT);
337 	assert(dst->dn_right->dn_kind == DT_NODE_IDENT);
338 
339 	fp = dst->dn_left->dn_ctfp;
340 	type = ctf_type_resolve(fp, dst->dn_left->dn_type);
341 
342 	if (dst->dn_op == DT_TOK_PTR) {
343 		type = ctf_type_reference(fp, type);
344 		type = ctf_type_resolve(fp, type);
345 	}
346 
347 	if ((fp = dt_cg_membinfo(ofp = fp, type,
348 	    dst->dn_right->dn_string, &m)) == NULL) {
349 		yypcb->pcb_hdl->dt_ctferr = ctf_errno(ofp);
350 		longjmp(yypcb->pcb_jmpbuf, EDT_CTF);
351 	}
352 
353 	if (ctf_type_encoding(fp, m.ctm_type, &e) != 0 || e.cte_bits > 64) {
354 		xyerror(D_UNKNOWN, "cg: bad field: off %lu type <%ld> "
355 		    "bits %u\n", m.ctm_offset, m.ctm_type, e.cte_bits);
356 	}
357 
358 	r1 = dt_regset_alloc(drp);
359 	r2 = dt_regset_alloc(drp);
360 	r3 = dt_regset_alloc(drp);
361 
362 	/*
363 	 * Compute shifts and masks.  We need to compute "shift" as the amount
364 	 * we need to shift left to position our field in the containing word.
365 	 * Refer to the comments in dt_cg_field_get(), above, for more info.
366 	 * We then compute fmask as the mask that truncates the value in the
367 	 * input register to width cte_bits, and cmask as the mask used to
368 	 * pass through the containing bits and zero the field bits.
369 	 */
370 #if BYTE_ORDER == _BIG_ENDIAN
371 	shift = clp2(P2ROUNDUP(e.cte_bits, NBBY) / NBBY) * NBBY -
372 	    (m.ctm_offset % NBBY + e.cte_bits);
373 #else
374 	shift = m.ctm_offset % NBBY;
375 #endif
376 	fmask = (1ULL << e.cte_bits) - 1;
377 	cmask = ~(fmask << shift);
378 
379 	instr = DIF_INSTR_LOAD(
380 	    dt_cg_load(dst, fp, m.ctm_type), dst->dn_reg, r1);
381 	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
382 
383 	dt_cg_setx(dlp, r2, cmask);
384 	instr = DIF_INSTR_FMT(DIF_OP_AND, r1, r2, r1);
385 	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
386 
387 	dt_cg_setx(dlp, r2, fmask);
388 	instr = DIF_INSTR_FMT(DIF_OP_AND, src->dn_reg, r2, r2);
389 	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
390 
391 	dt_cg_setx(dlp, r3, shift);
392 	instr = DIF_INSTR_FMT(DIF_OP_SLL, r2, r3, r2);
393 	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
394 
395 	instr = DIF_INSTR_FMT(DIF_OP_OR, r1, r2, r1);
396 	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
397 
398 	dt_regset_free(drp, r3);
399 	dt_regset_free(drp, r2);
400 
401 	return (r1);
402 }
403 
404 static void
405 dt_cg_store(dt_node_t *src, dt_irlist_t *dlp, dt_regset_t *drp, dt_node_t *dst)
406 {
407 	ctf_encoding_t e;
408 	dif_instr_t instr;
409 	size_t size;
410 	int reg;
411 
412 	/*
413 	 * If we're loading a bit-field, the size of our store is found by
414 	 * rounding dst's cte_bits up to a byte boundary and then finding the
415 	 * nearest power of two to this value (see clp2(), above).
416 	 */
417 	if ((dst->dn_flags & DT_NF_BITFIELD) &&
418 	    ctf_type_encoding(dst->dn_ctfp, dst->dn_type, &e) != CTF_ERR)
419 		size = clp2(P2ROUNDUP(e.cte_bits, NBBY) / NBBY);
420 	else
421 		size = dt_node_type_size(src);
422 
423 	if (src->dn_flags & DT_NF_REF) {
424 		reg = dt_regset_alloc(drp);
425 		dt_cg_setx(dlp, reg, size);
426 		instr = DIF_INSTR_COPYS(src->dn_reg, reg, dst->dn_reg);
427 		dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
428 		dt_regset_free(drp, reg);
429 	} else {
430 		if (dst->dn_flags & DT_NF_BITFIELD)
431 			reg = dt_cg_field_set(src, dlp, drp, dst);
432 		else
433 			reg = src->dn_reg;
434 
435 		switch (size) {
436 		case 1:
437 			instr = DIF_INSTR_STORE(DIF_OP_STB, reg, dst->dn_reg);
438 			break;
439 		case 2:
440 			instr = DIF_INSTR_STORE(DIF_OP_STH, reg, dst->dn_reg);
441 			break;
442 		case 4:
443 			instr = DIF_INSTR_STORE(DIF_OP_STW, reg, dst->dn_reg);
444 			break;
445 		case 8:
446 			instr = DIF_INSTR_STORE(DIF_OP_STX, reg, dst->dn_reg);
447 			break;
448 		default:
449 			instr = 0;
450 			xyerror(D_UNKNOWN, "internal error -- cg cannot store "
451 			    "size %lu when passed by value\n", (ulong_t)size);
452 		}
453 		dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
454 
455 		if (dst->dn_flags & DT_NF_BITFIELD)
456 			dt_regset_free(drp, reg);
457 	}
458 }
459 
460 /*
461  * Generate code for a typecast or for argument promotion from the type of the
462  * actual to the type of the formal.  We need to generate code for casts when
463  * a scalar type is being narrowed or changing signed-ness.  We first shift the
464  * desired bits high (losing excess bits if narrowing) and then shift them down
465  * using logical shift (unsigned result) or arithmetic shift (signed result).
466  */
467 static void
468 dt_cg_typecast(const dt_node_t *src, const dt_node_t *dst,
469     dt_irlist_t *dlp, dt_regset_t *drp)
470 {
471 	size_t srcsize = dt_node_type_size(src);
472 	size_t dstsize = dt_node_type_size(dst);
473 
474 	dif_instr_t instr;
475 	int rg;
476 
477 	if (!dt_node_is_scalar(dst))
478 		return; /* not a scalar */
479 	if (dstsize == srcsize &&
480 	    ((src->dn_flags ^ dst->dn_flags) & DT_NF_SIGNED) != 0)
481 		return; /* not narrowing or changing signed-ness */
482 	if (dstsize > srcsize && (src->dn_flags & DT_NF_SIGNED) == 0)
483 		return; /* nothing to do in this case */
484 
485 	rg = dt_regset_alloc(drp);
486 
487 	if (dstsize > srcsize) {
488 		int n = sizeof (uint64_t) * NBBY - srcsize * NBBY;
489 		int s = (dstsize - srcsize) * NBBY;
490 
491 		dt_cg_setx(dlp, rg, n);
492 
493 		instr = DIF_INSTR_FMT(DIF_OP_SLL, src->dn_reg, rg, dst->dn_reg);
494 		dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
495 
496 		if ((dst->dn_flags & DT_NF_SIGNED) || n == s) {
497 			instr = DIF_INSTR_FMT(DIF_OP_SRA,
498 			    dst->dn_reg, rg, dst->dn_reg);
499 			dt_irlist_append(dlp,
500 			    dt_cg_node_alloc(DT_LBL_NONE, instr));
501 		} else {
502 			dt_cg_setx(dlp, rg, s);
503 			instr = DIF_INSTR_FMT(DIF_OP_SRA,
504 			    dst->dn_reg, rg, dst->dn_reg);
505 			dt_irlist_append(dlp,
506 			    dt_cg_node_alloc(DT_LBL_NONE, instr));
507 			dt_cg_setx(dlp, rg, n - s);
508 			instr = DIF_INSTR_FMT(DIF_OP_SRL,
509 			    dst->dn_reg, rg, dst->dn_reg);
510 			dt_irlist_append(dlp,
511 			    dt_cg_node_alloc(DT_LBL_NONE, instr));
512 		}
513 	} else if (dstsize != sizeof (uint64_t)) {
514 		int n = sizeof (uint64_t) * NBBY - dstsize * NBBY;
515 
516 		dt_cg_setx(dlp, rg, n);
517 
518 		instr = DIF_INSTR_FMT(DIF_OP_SLL, src->dn_reg, rg, dst->dn_reg);
519 		dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
520 
521 		instr = DIF_INSTR_FMT((dst->dn_flags & DT_NF_SIGNED) ?
522 		    DIF_OP_SRA : DIF_OP_SRL, dst->dn_reg, rg, dst->dn_reg);
523 		dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
524 	}
525 
526 	dt_regset_free(drp, rg);
527 }
528 
529 /*
530  * Generate code to push the specified argument list on to the tuple stack.
531  * We use this routine for handling subroutine calls and associative arrays.
532  * We must first generate code for all subexpressions before loading the stack
533  * because any subexpression could itself require the use of the tuple stack.
534  * This holds a number of registers equal to the number of arguments, but this
535  * is not a huge problem because the number of arguments can't exceed the
536  * number of tuple register stack elements anyway.  At most one extra register
537  * is required (either by dt_cg_typecast() or for dtdt_size, below).  This
538  * implies that a DIF implementation should offer a number of general purpose
539  * registers at least one greater than the number of tuple registers.
540  */
541 static void
542 dt_cg_arglist(dt_ident_t *idp, dt_node_t *args,
543     dt_irlist_t *dlp, dt_regset_t *drp)
544 {
545 	const dt_idsig_t *isp = idp->di_data;
546 	dt_node_t *dnp;
547 	int i = 0;
548 
549 	for (dnp = args; dnp != NULL; dnp = dnp->dn_list)
550 		dt_cg_node(dnp, dlp, drp);
551 
552 	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, DIF_INSTR_FLUSHTS));
553 
554 	for (dnp = args; dnp != NULL; dnp = dnp->dn_list, i++) {
555 		dtrace_diftype_t t;
556 		dif_instr_t instr;
557 		uint_t op;
558 		int reg;
559 
560 		dt_node_diftype(yypcb->pcb_hdl, dnp, &t);
561 
562 		isp->dis_args[i].dn_reg = dnp->dn_reg; /* re-use register */
563 		dt_cg_typecast(dnp, &isp->dis_args[i], dlp, drp);
564 		isp->dis_args[i].dn_reg = -1;
565 
566 		if (t.dtdt_flags & DIF_TF_BYREF) {
567 			op = DIF_OP_PUSHTR;
568 			if (t.dtdt_size != 0) {
569 				reg = dt_regset_alloc(drp);
570 				dt_cg_setx(dlp, reg, t.dtdt_size);
571 			} else {
572 				reg = DIF_REG_R0;
573 			}
574 		} else {
575 			op = DIF_OP_PUSHTV;
576 			reg = DIF_REG_R0;
577 		}
578 
579 		instr = DIF_INSTR_PUSHTS(op, t.dtdt_kind, reg, dnp->dn_reg);
580 		dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
581 		dt_regset_free(drp, dnp->dn_reg);
582 
583 		if (reg != DIF_REG_R0)
584 			dt_regset_free(drp, reg);
585 	}
586 
587 	if (i > yypcb->pcb_hdl->dt_conf.dtc_diftupregs)
588 		longjmp(yypcb->pcb_jmpbuf, EDT_NOTUPREG);
589 }
590 
591 static void
592 dt_cg_arithmetic_op(dt_node_t *dnp, dt_irlist_t *dlp,
593     dt_regset_t *drp, uint_t op)
594 {
595 	int is_ptr_op = (dnp->dn_op == DT_TOK_ADD || dnp->dn_op == DT_TOK_SUB ||
596 	    dnp->dn_op == DT_TOK_ADD_EQ || dnp->dn_op == DT_TOK_SUB_EQ);
597 
598 	int lp_is_ptr = dt_node_is_pointer(dnp->dn_left);
599 	int rp_is_ptr = dt_node_is_pointer(dnp->dn_right);
600 
601 	dif_instr_t instr;
602 
603 	if (lp_is_ptr && rp_is_ptr) {
604 		assert(dnp->dn_op == DT_TOK_SUB);
605 		is_ptr_op = 0;
606 	}
607 
608 	dt_cg_node(dnp->dn_left, dlp, drp);
609 	if (is_ptr_op && rp_is_ptr)
610 		dt_cg_ptrsize(dnp, dlp, drp, DIF_OP_MUL, dnp->dn_left->dn_reg);
611 
612 	dt_cg_node(dnp->dn_right, dlp, drp);
613 	if (is_ptr_op && lp_is_ptr)
614 		dt_cg_ptrsize(dnp, dlp, drp, DIF_OP_MUL, dnp->dn_right->dn_reg);
615 
616 	instr = DIF_INSTR_FMT(op, dnp->dn_left->dn_reg,
617 	    dnp->dn_right->dn_reg, dnp->dn_left->dn_reg);
618 
619 	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
620 	dt_regset_free(drp, dnp->dn_right->dn_reg);
621 	dnp->dn_reg = dnp->dn_left->dn_reg;
622 
623 	if (lp_is_ptr && rp_is_ptr)
624 		dt_cg_ptrsize(dnp->dn_right,
625 		    dlp, drp, DIF_OP_UDIV, dnp->dn_reg);
626 }
627 
628 static uint_t
629 dt_cg_stvar(const dt_ident_t *idp)
630 {
631 	static const uint_t aops[] = { DIF_OP_STGAA, DIF_OP_STTAA, DIF_OP_NOP };
632 	static const uint_t sops[] = { DIF_OP_STGS, DIF_OP_STTS, DIF_OP_STLS };
633 
634 	uint_t i = (((idp->di_flags & DT_IDFLG_LOCAL) != 0) << 1) |
635 	    ((idp->di_flags & DT_IDFLG_TLS) != 0);
636 
637 	return (idp->di_kind == DT_IDENT_ARRAY ? aops[i] : sops[i]);
638 }
639 
640 static void
641 dt_cg_prearith_op(dt_node_t *dnp, dt_irlist_t *dlp, dt_regset_t *drp, uint_t op)
642 {
643 	ctf_file_t *ctfp = dnp->dn_ctfp;
644 	dif_instr_t instr;
645 	ctf_id_t type;
646 	ssize_t size = 1;
647 	int reg;
648 
649 	if (dt_node_is_pointer(dnp)) {
650 		type = ctf_type_resolve(ctfp, dnp->dn_type);
651 		assert(ctf_type_kind(ctfp, type) == CTF_K_POINTER);
652 		size = ctf_type_size(ctfp, ctf_type_reference(ctfp, type));
653 	}
654 
655 	dt_cg_node(dnp->dn_child, dlp, drp);
656 	dnp->dn_reg = dnp->dn_child->dn_reg;
657 
658 	reg = dt_regset_alloc(drp);
659 	dt_cg_setx(dlp, reg, size);
660 
661 	instr = DIF_INSTR_FMT(op, dnp->dn_reg, reg, dnp->dn_reg);
662 	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
663 	dt_regset_free(drp, reg);
664 
665 	/*
666 	 * If we are modifying a variable, generate an stv instruction from
667 	 * the variable specified by the identifier.  If we are storing to a
668 	 * memory address, generate code again for the left-hand side using
669 	 * DT_NF_REF to get the address, and then generate a store to it.
670 	 * In both paths, we store the value in dnp->dn_reg (the new value).
671 	 */
672 	if (dnp->dn_child->dn_kind == DT_NODE_VAR) {
673 		dt_ident_t *idp = dt_ident_resolve(dnp->dn_child->dn_ident);
674 
675 		idp->di_flags |= DT_IDFLG_DIFW;
676 		instr = DIF_INSTR_STV(dt_cg_stvar(idp),
677 		    idp->di_id, dnp->dn_reg);
678 		dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
679 	} else {
680 		uint_t rbit = dnp->dn_child->dn_flags & DT_NF_REF;
681 
682 		assert(dnp->dn_child->dn_flags & DT_NF_WRITABLE);
683 		assert(dnp->dn_child->dn_flags & DT_NF_LVALUE);
684 
685 		dnp->dn_child->dn_flags |= DT_NF_REF; /* force pass-by-ref */
686 		dt_cg_node(dnp->dn_child, dlp, drp);
687 
688 		dt_cg_store(dnp, dlp, drp, dnp->dn_child);
689 		dt_regset_free(drp, dnp->dn_child->dn_reg);
690 
691 		dnp->dn_left->dn_flags &= ~DT_NF_REF;
692 		dnp->dn_left->dn_flags |= rbit;
693 	}
694 }
695 
696 static void
697 dt_cg_postarith_op(dt_node_t *dnp, dt_irlist_t *dlp,
698     dt_regset_t *drp, uint_t op)
699 {
700 	ctf_file_t *ctfp = dnp->dn_ctfp;
701 	dif_instr_t instr;
702 	ctf_id_t type;
703 	ssize_t size = 1;
704 	int nreg;
705 
706 	if (dt_node_is_pointer(dnp)) {
707 		type = ctf_type_resolve(ctfp, dnp->dn_type);
708 		assert(ctf_type_kind(ctfp, type) == CTF_K_POINTER);
709 		size = ctf_type_size(ctfp, ctf_type_reference(ctfp, type));
710 	}
711 
712 	dt_cg_node(dnp->dn_child, dlp, drp);
713 	dnp->dn_reg = dnp->dn_child->dn_reg;
714 
715 	nreg = dt_regset_alloc(drp);
716 	dt_cg_setx(dlp, nreg, size);
717 	instr = DIF_INSTR_FMT(op, dnp->dn_reg, nreg, nreg);
718 	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
719 
720 	/*
721 	 * If we are modifying a variable, generate an stv instruction from
722 	 * the variable specified by the identifier.  If we are storing to a
723 	 * memory address, generate code again for the left-hand side using
724 	 * DT_NF_REF to get the address, and then generate a store to it.
725 	 * In both paths, we store the value from 'nreg' (the new value).
726 	 */
727 	if (dnp->dn_child->dn_kind == DT_NODE_VAR) {
728 		dt_ident_t *idp = dt_ident_resolve(dnp->dn_child->dn_ident);
729 
730 		idp->di_flags |= DT_IDFLG_DIFW;
731 		instr = DIF_INSTR_STV(dt_cg_stvar(idp), idp->di_id, nreg);
732 		dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
733 	} else {
734 		uint_t rbit = dnp->dn_child->dn_flags & DT_NF_REF;
735 		int oreg = dnp->dn_reg;
736 
737 		assert(dnp->dn_child->dn_flags & DT_NF_WRITABLE);
738 		assert(dnp->dn_child->dn_flags & DT_NF_LVALUE);
739 
740 		dnp->dn_child->dn_flags |= DT_NF_REF; /* force pass-by-ref */
741 		dt_cg_node(dnp->dn_child, dlp, drp);
742 
743 		dnp->dn_reg = nreg;
744 		dt_cg_store(dnp, dlp, drp, dnp->dn_child);
745 		dnp->dn_reg = oreg;
746 
747 		dt_regset_free(drp, dnp->dn_child->dn_reg);
748 		dnp->dn_left->dn_flags &= ~DT_NF_REF;
749 		dnp->dn_left->dn_flags |= rbit;
750 	}
751 
752 	dt_regset_free(drp, nreg);
753 }
754 
755 /*
756  * Determine if we should perform signed or unsigned comparison for an OP2.
757  * If both operands are of arithmetic type, perform the usual arithmetic
758  * conversions to determine the common real type for comparison [ISOC 6.5.8.3].
759  */
760 static int
761 dt_cg_compare_signed(dt_node_t *dnp)
762 {
763 	dt_node_t dn;
764 
765 	if (dt_node_is_string(dnp->dn_left) ||
766 	    dt_node_is_string(dnp->dn_right))
767 		return (1); /* strings always compare signed */
768 	else if (!dt_node_is_arith(dnp->dn_left) ||
769 	    !dt_node_is_arith(dnp->dn_right))
770 		return (0); /* non-arithmetic types always compare unsigned */
771 
772 	bzero(&dn, sizeof (dn));
773 	dt_node_promote(dnp->dn_left, dnp->dn_right, &dn);
774 	return (dn.dn_flags & DT_NF_SIGNED);
775 }
776 
777 static void
778 dt_cg_compare_op(dt_node_t *dnp, dt_irlist_t *dlp, dt_regset_t *drp, uint_t op)
779 {
780 	uint_t lbl_true = dt_irlist_label(dlp);
781 	uint_t lbl_post = dt_irlist_label(dlp);
782 
783 	dif_instr_t instr;
784 	uint_t opc;
785 
786 	dt_cg_node(dnp->dn_left, dlp, drp);
787 	dt_cg_node(dnp->dn_right, dlp, drp);
788 
789 	if (dt_node_is_string(dnp->dn_left) || dt_node_is_string(dnp->dn_right))
790 		opc = DIF_OP_SCMP;
791 	else
792 		opc = DIF_OP_CMP;
793 
794 	instr = DIF_INSTR_CMP(opc, dnp->dn_left->dn_reg, dnp->dn_right->dn_reg);
795 	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
796 	dt_regset_free(drp, dnp->dn_right->dn_reg);
797 	dnp->dn_reg = dnp->dn_left->dn_reg;
798 
799 	instr = DIF_INSTR_BRANCH(op, lbl_true);
800 	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
801 
802 	instr = DIF_INSTR_MOV(DIF_REG_R0, dnp->dn_reg);
803 	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
804 
805 	instr = DIF_INSTR_BRANCH(DIF_OP_BA, lbl_post);
806 	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
807 
808 	dt_cg_xsetx(dlp, NULL, lbl_true, dnp->dn_reg, 1);
809 	dt_irlist_append(dlp, dt_cg_node_alloc(lbl_post, DIF_INSTR_NOP));
810 }
811 
812 /*
813  * Code generation for the ternary op requires some trickery with the assembler
814  * in order to conserve registers.  We generate code for dn_expr and dn_left
815  * and free their registers so they do not have be consumed across codegen for
816  * dn_right.  We insert a dummy MOV at the end of dn_left into the destination
817  * register, which is not yet known because we haven't done dn_right yet, and
818  * save the pointer to this instruction node.  We then generate code for
819  * dn_right and use its register as our output.  Finally, we reach back and
820  * patch the instruction for dn_left to move its output into this register.
821  */
822 static void
823 dt_cg_ternary_op(dt_node_t *dnp, dt_irlist_t *dlp, dt_regset_t *drp)
824 {
825 	uint_t lbl_false = dt_irlist_label(dlp);
826 	uint_t lbl_post = dt_irlist_label(dlp);
827 
828 	dif_instr_t instr;
829 	dt_irnode_t *dip;
830 
831 	dt_cg_node(dnp->dn_expr, dlp, drp);
832 	instr = DIF_INSTR_TST(dnp->dn_expr->dn_reg);
833 	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
834 	dt_regset_free(drp, dnp->dn_expr->dn_reg);
835 
836 	instr = DIF_INSTR_BRANCH(DIF_OP_BE, lbl_false);
837 	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
838 
839 	dt_cg_node(dnp->dn_left, dlp, drp);
840 	instr = DIF_INSTR_MOV(dnp->dn_left->dn_reg, DIF_REG_R0);
841 	dip = dt_cg_node_alloc(DT_LBL_NONE, instr); /* save dip for below */
842 	dt_irlist_append(dlp, dip);
843 	dt_regset_free(drp, dnp->dn_left->dn_reg);
844 
845 	instr = DIF_INSTR_BRANCH(DIF_OP_BA, lbl_post);
846 	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
847 
848 	dt_irlist_append(dlp, dt_cg_node_alloc(lbl_false, DIF_INSTR_NOP));
849 	dt_cg_node(dnp->dn_right, dlp, drp);
850 	dnp->dn_reg = dnp->dn_right->dn_reg;
851 
852 	/*
853 	 * Now that dn_reg is assigned, reach back and patch the correct MOV
854 	 * instruction into the tail of dn_left.  We know dn_reg was unused
855 	 * at that point because otherwise dn_right couldn't have allocated it.
856 	 */
857 	dip->di_instr = DIF_INSTR_MOV(dnp->dn_left->dn_reg, dnp->dn_reg);
858 	dt_irlist_append(dlp, dt_cg_node_alloc(lbl_post, DIF_INSTR_NOP));
859 }
860 
861 static void
862 dt_cg_logical_and(dt_node_t *dnp, dt_irlist_t *dlp, dt_regset_t *drp)
863 {
864 	uint_t lbl_false = dt_irlist_label(dlp);
865 	uint_t lbl_post = dt_irlist_label(dlp);
866 
867 	dif_instr_t instr;
868 
869 	dt_cg_node(dnp->dn_left, dlp, drp);
870 	instr = DIF_INSTR_TST(dnp->dn_left->dn_reg);
871 	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
872 	dt_regset_free(drp, dnp->dn_left->dn_reg);
873 
874 	instr = DIF_INSTR_BRANCH(DIF_OP_BE, lbl_false);
875 	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
876 
877 	dt_cg_node(dnp->dn_right, dlp, drp);
878 	instr = DIF_INSTR_TST(dnp->dn_right->dn_reg);
879 	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
880 	dnp->dn_reg = dnp->dn_right->dn_reg;
881 
882 	instr = DIF_INSTR_BRANCH(DIF_OP_BE, lbl_false);
883 	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
884 
885 	dt_cg_setx(dlp, dnp->dn_reg, 1);
886 
887 	instr = DIF_INSTR_BRANCH(DIF_OP_BA, lbl_post);
888 	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
889 
890 	instr = DIF_INSTR_MOV(DIF_REG_R0, dnp->dn_reg);
891 	dt_irlist_append(dlp, dt_cg_node_alloc(lbl_false, instr));
892 
893 	dt_irlist_append(dlp, dt_cg_node_alloc(lbl_post, DIF_INSTR_NOP));
894 }
895 
896 static void
897 dt_cg_logical_xor(dt_node_t *dnp, dt_irlist_t *dlp, dt_regset_t *drp)
898 {
899 	uint_t lbl_next = dt_irlist_label(dlp);
900 	uint_t lbl_tail = dt_irlist_label(dlp);
901 
902 	dif_instr_t instr;
903 
904 	dt_cg_node(dnp->dn_left, dlp, drp);
905 	instr = DIF_INSTR_TST(dnp->dn_left->dn_reg);
906 	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
907 
908 	instr = DIF_INSTR_BRANCH(DIF_OP_BE, lbl_next);
909 	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
910 	dt_cg_setx(dlp, dnp->dn_left->dn_reg, 1);
911 
912 	dt_irlist_append(dlp, dt_cg_node_alloc(lbl_next, DIF_INSTR_NOP));
913 	dt_cg_node(dnp->dn_right, dlp, drp);
914 
915 	instr = DIF_INSTR_TST(dnp->dn_right->dn_reg);
916 	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
917 
918 	instr = DIF_INSTR_BRANCH(DIF_OP_BE, lbl_tail);
919 	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
920 	dt_cg_setx(dlp, dnp->dn_right->dn_reg, 1);
921 
922 	instr = DIF_INSTR_FMT(DIF_OP_XOR, dnp->dn_left->dn_reg,
923 	    dnp->dn_right->dn_reg, dnp->dn_left->dn_reg);
924 
925 	dt_irlist_append(dlp, dt_cg_node_alloc(lbl_tail, instr));
926 
927 	dt_regset_free(drp, dnp->dn_right->dn_reg);
928 	dnp->dn_reg = dnp->dn_left->dn_reg;
929 }
930 
931 static void
932 dt_cg_logical_or(dt_node_t *dnp, dt_irlist_t *dlp, dt_regset_t *drp)
933 {
934 	uint_t lbl_true = dt_irlist_label(dlp);
935 	uint_t lbl_false = dt_irlist_label(dlp);
936 	uint_t lbl_post = dt_irlist_label(dlp);
937 
938 	dif_instr_t instr;
939 
940 	dt_cg_node(dnp->dn_left, dlp, drp);
941 	instr = DIF_INSTR_TST(dnp->dn_left->dn_reg);
942 	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
943 	dt_regset_free(drp, dnp->dn_left->dn_reg);
944 
945 	instr = DIF_INSTR_BRANCH(DIF_OP_BNE, lbl_true);
946 	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
947 
948 	dt_cg_node(dnp->dn_right, dlp, drp);
949 	instr = DIF_INSTR_TST(dnp->dn_right->dn_reg);
950 	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
951 	dnp->dn_reg = dnp->dn_right->dn_reg;
952 
953 	instr = DIF_INSTR_BRANCH(DIF_OP_BE, lbl_false);
954 	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
955 
956 	dt_cg_xsetx(dlp, NULL, lbl_true, dnp->dn_reg, 1);
957 
958 	instr = DIF_INSTR_BRANCH(DIF_OP_BA, lbl_post);
959 	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
960 
961 	instr = DIF_INSTR_MOV(DIF_REG_R0, dnp->dn_reg);
962 	dt_irlist_append(dlp, dt_cg_node_alloc(lbl_false, instr));
963 
964 	dt_irlist_append(dlp, dt_cg_node_alloc(lbl_post, DIF_INSTR_NOP));
965 }
966 
967 static void
968 dt_cg_logical_neg(dt_node_t *dnp, dt_irlist_t *dlp, dt_regset_t *drp)
969 {
970 	uint_t lbl_zero = dt_irlist_label(dlp);
971 	uint_t lbl_post = dt_irlist_label(dlp);
972 
973 	dif_instr_t instr;
974 
975 	dt_cg_node(dnp->dn_child, dlp, drp);
976 	dnp->dn_reg = dnp->dn_child->dn_reg;
977 
978 	instr = DIF_INSTR_TST(dnp->dn_reg);
979 	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
980 
981 	instr = DIF_INSTR_BRANCH(DIF_OP_BE, lbl_zero);
982 	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
983 
984 	instr = DIF_INSTR_MOV(DIF_REG_R0, dnp->dn_reg);
985 	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
986 
987 	instr = DIF_INSTR_BRANCH(DIF_OP_BA, lbl_post);
988 	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
989 
990 	dt_cg_xsetx(dlp, NULL, lbl_zero, dnp->dn_reg, 1);
991 	dt_irlist_append(dlp, dt_cg_node_alloc(lbl_post, DIF_INSTR_NOP));
992 }
993 
994 static void
995 dt_cg_asgn_op(dt_node_t *dnp, dt_irlist_t *dlp, dt_regset_t *drp)
996 {
997 	dif_instr_t instr;
998 	dt_ident_t *idp;
999 
1000 	/*
1001 	 * If we are performing a structure assignment of a translated type,
1002 	 * we must instantiate all members and create a snapshot of the object
1003 	 * in scratch space.  We allocs a chunk of memory, generate code for
1004 	 * each member, and then set dnp->dn_reg to the scratch object address.
1005 	 */
1006 	if ((idp = dt_node_resolve(dnp->dn_right, DT_IDENT_XLSOU)) != NULL) {
1007 		ctf_membinfo_t ctm;
1008 		dt_xlator_t *dxp = idp->di_data;
1009 		dt_node_t *mnp, dn, mn;
1010 		int r1, r2;
1011 
1012 		/*
1013 		 * Create two fake dt_node_t's representing operator "." and a
1014 		 * right-hand identifier child node.  These will be repeatedly
1015 		 * modified according to each instantiated member so that we
1016 		 * can pass them to dt_cg_store() and effect a member store.
1017 		 */
1018 		bzero(&dn, sizeof (dt_node_t));
1019 		dn.dn_kind = DT_NODE_OP2;
1020 		dn.dn_op = DT_TOK_DOT;
1021 		dn.dn_left = dnp;
1022 		dn.dn_right = &mn;
1023 
1024 		bzero(&mn, sizeof (dt_node_t));
1025 		mn.dn_kind = DT_NODE_IDENT;
1026 		mn.dn_op = DT_TOK_IDENT;
1027 
1028 		/*
1029 		 * Allocate a register for our scratch data pointer.  First we
1030 		 * set it to the size of our data structure, and then replace
1031 		 * it with the result of an allocs of the specified size.
1032 		 */
1033 		r1 = dt_regset_alloc(drp);
1034 		dt_cg_setx(dlp, r1,
1035 		    ctf_type_size(dxp->dx_dst_ctfp, dxp->dx_dst_base));
1036 
1037 		instr = DIF_INSTR_ALLOCS(r1, r1);
1038 		dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
1039 
1040 		/*
1041 		 * When dt_cg_asgn_op() is called, we have already generated
1042 		 * code for dnp->dn_right, which is the translator input.  We
1043 		 * now associate this register with the translator's input
1044 		 * identifier so it can be referenced during our member loop.
1045 		 */
1046 		dxp->dx_ident->di_flags |= DT_IDFLG_CGREG;
1047 		dxp->dx_ident->di_id = dnp->dn_right->dn_reg;
1048 
1049 		for (mnp = dxp->dx_members; mnp != NULL; mnp = mnp->dn_list) {
1050 			/*
1051 			 * Generate code for the translator member expression,
1052 			 * and then cast the result to the member type.
1053 			 */
1054 			dt_cg_node(mnp->dn_membexpr, dlp, drp);
1055 			mnp->dn_reg = mnp->dn_membexpr->dn_reg;
1056 			dt_cg_typecast(mnp->dn_membexpr, mnp, dlp, drp);
1057 
1058 			/*
1059 			 * Ask CTF for the offset of the member so we can store
1060 			 * to the appropriate offset.  This call has already
1061 			 * been done once by the parser, so it should succeed.
1062 			 */
1063 			if (ctf_member_info(dxp->dx_dst_ctfp, dxp->dx_dst_base,
1064 			    mnp->dn_membname, &ctm) == CTF_ERR) {
1065 				yypcb->pcb_hdl->dt_ctferr =
1066 				    ctf_errno(dxp->dx_dst_ctfp);
1067 				longjmp(yypcb->pcb_jmpbuf, EDT_CTF);
1068 			}
1069 
1070 			/*
1071 			 * If the destination member is at offset 0, store the
1072 			 * result directly to r1 (the scratch buffer address).
1073 			 * Otherwise allocate another temporary for the offset
1074 			 * and add r1 to it before storing the result.
1075 			 */
1076 			if (ctm.ctm_offset != 0) {
1077 				r2 = dt_regset_alloc(drp);
1078 
1079 				/*
1080 				 * Add the member offset rounded down to the
1081 				 * nearest byte.  If the offset was not aligned
1082 				 * on a byte boundary, this member is a bit-
1083 				 * field and dt_cg_store() will handle masking.
1084 				 */
1085 				dt_cg_setx(dlp, r2, ctm.ctm_offset / NBBY);
1086 				instr = DIF_INSTR_FMT(DIF_OP_ADD, r1, r2, r2);
1087 				dt_irlist_append(dlp,
1088 				    dt_cg_node_alloc(DT_LBL_NONE, instr));
1089 
1090 				dt_node_type_propagate(mnp, &dn);
1091 				dn.dn_right->dn_string = mnp->dn_membname;
1092 				dn.dn_reg = r2;
1093 
1094 				dt_cg_store(mnp, dlp, drp, &dn);
1095 				dt_regset_free(drp, r2);
1096 
1097 			} else {
1098 				dt_node_type_propagate(mnp, &dn);
1099 				dn.dn_right->dn_string = mnp->dn_membname;
1100 				dn.dn_reg = r1;
1101 
1102 				dt_cg_store(mnp, dlp, drp, &dn);
1103 			}
1104 
1105 			dt_regset_free(drp, mnp->dn_reg);
1106 		}
1107 
1108 		dxp->dx_ident->di_flags &= ~DT_IDFLG_CGREG;
1109 		dxp->dx_ident->di_id = 0;
1110 
1111 		if (dnp->dn_right->dn_reg != -1)
1112 			dt_regset_free(drp, dnp->dn_right->dn_reg);
1113 
1114 		assert(dnp->dn_reg == dnp->dn_right->dn_reg);
1115 		dnp->dn_reg = r1;
1116 	}
1117 
1118 	/*
1119 	 * If we are storing to a variable, generate an stv instruction from
1120 	 * the variable specified by the identifier.  If we are storing to a
1121 	 * memory address, generate code again for the left-hand side using
1122 	 * DT_NF_REF to get the address, and then generate a store to it.
1123 	 * In both paths, we assume dnp->dn_reg already has the new value.
1124 	 */
1125 	if (dnp->dn_left->dn_kind == DT_NODE_VAR) {
1126 		idp = dt_ident_resolve(dnp->dn_left->dn_ident);
1127 
1128 		if (idp->di_kind == DT_IDENT_ARRAY)
1129 			dt_cg_arglist(idp, dnp->dn_left->dn_args, dlp, drp);
1130 
1131 		idp->di_flags |= DT_IDFLG_DIFW;
1132 		instr = DIF_INSTR_STV(dt_cg_stvar(idp),
1133 		    idp->di_id, dnp->dn_reg);
1134 		dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
1135 	} else {
1136 		uint_t rbit = dnp->dn_left->dn_flags & DT_NF_REF;
1137 
1138 		assert(dnp->dn_left->dn_flags & DT_NF_WRITABLE);
1139 		assert(dnp->dn_left->dn_flags & DT_NF_LVALUE);
1140 
1141 		dnp->dn_left->dn_flags |= DT_NF_REF; /* force pass-by-ref */
1142 
1143 		dt_cg_node(dnp->dn_left, dlp, drp);
1144 		dt_cg_store(dnp, dlp, drp, dnp->dn_left);
1145 		dt_regset_free(drp, dnp->dn_left->dn_reg);
1146 
1147 		dnp->dn_left->dn_flags &= ~DT_NF_REF;
1148 		dnp->dn_left->dn_flags |= rbit;
1149 	}
1150 }
1151 
1152 static void
1153 dt_cg_assoc_op(dt_node_t *dnp, dt_irlist_t *dlp, dt_regset_t *drp)
1154 {
1155 	dif_instr_t instr;
1156 	uint_t op;
1157 
1158 	assert(dnp->dn_kind == DT_NODE_VAR);
1159 	assert(!(dnp->dn_ident->di_flags & DT_IDFLG_LOCAL));
1160 	assert(dnp->dn_args != NULL);
1161 
1162 	dt_cg_arglist(dnp->dn_ident, dnp->dn_args, dlp, drp);
1163 
1164 	dnp->dn_reg = dt_regset_alloc(drp);
1165 
1166 	if (dnp->dn_ident->di_flags & DT_IDFLG_TLS)
1167 		op = DIF_OP_LDTAA;
1168 	else
1169 		op = DIF_OP_LDGAA;
1170 
1171 	dnp->dn_ident->di_flags |= DT_IDFLG_DIFR;
1172 	instr = DIF_INSTR_LDV(op, dnp->dn_ident->di_id, dnp->dn_reg);
1173 	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
1174 
1175 	/*
1176 	 * If the associative array is a pass-by-reference type, then we are
1177 	 * loading its value as a pointer to either load or store through it.
1178 	 * The array element in question may not have been faulted in yet, in
1179 	 * which case DIF_OP_LD*AA will return zero.  We append an epilogue
1180 	 * of instructions similar to the following:
1181 	 *
1182 	 *	  ld?aa	 id, %r1	! base ld?aa instruction above
1183 	 *	  tst	 %r1		! start of epilogue
1184 	 *   +--- bne	 label
1185 	 *   |    setx	 size, %r1
1186 	 *   |    allocs %r1, %r1
1187 	 *   |    st?aa	 id, %r1
1188 	 *   |    ld?aa	 id, %r1
1189 	 *   v
1190 	 * label: < rest of code >
1191 	 *
1192 	 * The idea is that we allocs a zero-filled chunk of scratch space and
1193 	 * do a DIF_OP_ST*AA to fault in and initialize the array element, and
1194 	 * then reload it to get the faulted-in address of the new variable
1195 	 * storage.  This isn't cheap, but pass-by-ref associative array values
1196 	 * are (thus far) uncommon and the allocs cost only occurs once.  If
1197 	 * this path becomes important to DTrace users, we can improve things
1198 	 * by adding a new DIF opcode to fault in associative array elements.
1199 	 */
1200 	if (dnp->dn_flags & DT_NF_REF) {
1201 		uint_t stvop = op == DIF_OP_LDTAA ? DIF_OP_STTAA : DIF_OP_STGAA;
1202 		uint_t label = dt_irlist_label(dlp);
1203 
1204 		instr = DIF_INSTR_TST(dnp->dn_reg);
1205 		dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
1206 
1207 		instr = DIF_INSTR_BRANCH(DIF_OP_BNE, label);
1208 		dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
1209 
1210 		dt_cg_setx(dlp, dnp->dn_reg, dt_node_type_size(dnp));
1211 		instr = DIF_INSTR_ALLOCS(dnp->dn_reg, dnp->dn_reg);
1212 		dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
1213 
1214 		dnp->dn_ident->di_flags |= DT_IDFLG_DIFW;
1215 		instr = DIF_INSTR_STV(stvop, dnp->dn_ident->di_id, dnp->dn_reg);
1216 		dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
1217 
1218 		instr = DIF_INSTR_LDV(op, dnp->dn_ident->di_id, dnp->dn_reg);
1219 		dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
1220 
1221 		dt_irlist_append(dlp, dt_cg_node_alloc(label, DIF_INSTR_NOP));
1222 	}
1223 }
1224 
1225 static void
1226 dt_cg_array_op(dt_node_t *dnp, dt_irlist_t *dlp, dt_regset_t *drp)
1227 {
1228 	dt_probe_t *prp = yypcb->pcb_probe;
1229 	uintmax_t saved = dnp->dn_args->dn_value;
1230 	dt_ident_t *idp = dnp->dn_ident;
1231 
1232 	dif_instr_t instr;
1233 	uint_t op;
1234 	size_t size;
1235 	int reg, n;
1236 
1237 	assert(dnp->dn_kind == DT_NODE_VAR);
1238 	assert(!(idp->di_flags & DT_IDFLG_LOCAL));
1239 
1240 	assert(dnp->dn_args->dn_kind == DT_NODE_INT);
1241 	assert(dnp->dn_args->dn_list == NULL);
1242 
1243 	/*
1244 	 * If this is a reference in the args[] array, temporarily modify the
1245 	 * array index according to the static argument mapping (if any),
1246 	 * unless the argument reference is provided by a dynamic translator.
1247 	 * If we're using a dynamic translator for args[], then just set dn_reg
1248 	 * to an invalid reg and return: DIF_OP_XLARG will fetch the arg later.
1249 	 */
1250 	if (idp->di_id == DIF_VAR_ARGS) {
1251 		if ((idp->di_kind == DT_IDENT_XLPTR ||
1252 		    idp->di_kind == DT_IDENT_XLSOU) &&
1253 		    dt_xlator_dynamic(idp->di_data)) {
1254 			dnp->dn_reg = -1;
1255 			return;
1256 		}
1257 		dnp->dn_args->dn_value = prp->pr_mapping[saved];
1258 	}
1259 
1260 	dt_cg_node(dnp->dn_args, dlp, drp);
1261 	dnp->dn_args->dn_value = saved;
1262 
1263 	dnp->dn_reg = dnp->dn_args->dn_reg;
1264 
1265 	if (idp->di_flags & DT_IDFLG_TLS)
1266 		op = DIF_OP_LDTA;
1267 	else
1268 		op = DIF_OP_LDGA;
1269 
1270 	idp->di_flags |= DT_IDFLG_DIFR;
1271 
1272 	instr = DIF_INSTR_LDA(op, idp->di_id,
1273 	    dnp->dn_args->dn_reg, dnp->dn_reg);
1274 
1275 	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
1276 
1277 	/*
1278 	 * If this is a reference to the args[] array, we need to take the
1279 	 * additional step of explicitly eliminating any bits larger than the
1280 	 * type size: the DIF interpreter in the kernel will always give us
1281 	 * the raw (64-bit) argument value, and any bits larger than the type
1282 	 * size may be junk.  As a practical matter, this arises only on 64-bit
1283 	 * architectures and only when the argument index is larger than the
1284 	 * number of arguments passed directly to DTrace: if a 8-, 16- or
1285 	 * 32-bit argument must be retrieved from the stack, it is possible
1286 	 * (and it some cases, likely) that the upper bits will be garbage.
1287 	 */
1288 	if (idp->di_id != DIF_VAR_ARGS || !dt_node_is_scalar(dnp))
1289 		return;
1290 
1291 	if ((size = dt_node_type_size(dnp)) == sizeof (uint64_t))
1292 		return;
1293 
1294 	reg = dt_regset_alloc(drp);
1295 	assert(size < sizeof (uint64_t));
1296 	n = sizeof (uint64_t) * NBBY - size * NBBY;
1297 
1298 	dt_cg_setx(dlp, reg, n);
1299 
1300 	instr = DIF_INSTR_FMT(DIF_OP_SLL, dnp->dn_reg, reg, dnp->dn_reg);
1301 	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
1302 
1303 	instr = DIF_INSTR_FMT((dnp->dn_flags & DT_NF_SIGNED) ?
1304 	    DIF_OP_SRA : DIF_OP_SRL, dnp->dn_reg, reg, dnp->dn_reg);
1305 
1306 	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
1307 	dt_regset_free(drp, reg);
1308 }
1309 
1310 /*
1311  * Generate code for an inlined variable reference.  Inlines can be used to
1312  * define either scalar or associative array substitutions.  For scalars, we
1313  * simply generate code for the parse tree saved in the identifier's din_root,
1314  * and then cast the resulting expression to the inline's declaration type.
1315  * For arrays, we take the input parameter subtrees from dnp->dn_args and
1316  * temporarily store them in the din_root of each din_argv[i] identifier,
1317  * which are themselves inlines and were set up for us by the parser.  The
1318  * result is that any reference to the inlined parameter inside the top-level
1319  * din_root will turn into a recursive call to dt_cg_inline() for a scalar
1320  * inline whose din_root will refer to the subtree pointed to by the argument.
1321  */
1322 static void
1323 dt_cg_inline(dt_node_t *dnp, dt_irlist_t *dlp, dt_regset_t *drp)
1324 {
1325 	dt_ident_t *idp = dnp->dn_ident;
1326 	dt_idnode_t *inp = idp->di_iarg;
1327 
1328 	dt_idnode_t *pinp;
1329 	dt_node_t *pnp;
1330 	int i;
1331 
1332 	assert(idp->di_flags & DT_IDFLG_INLINE);
1333 	assert(idp->di_ops == &dt_idops_inline);
1334 
1335 	if (idp->di_kind == DT_IDENT_ARRAY) {
1336 		for (i = 0, pnp = dnp->dn_args;
1337 		    pnp != NULL; pnp = pnp->dn_list, i++) {
1338 			if (inp->din_argv[i] != NULL) {
1339 				pinp = inp->din_argv[i]->di_iarg;
1340 				pinp->din_root = pnp;
1341 			}
1342 		}
1343 	}
1344 
1345 	dt_cg_node(inp->din_root, dlp, drp);
1346 	dnp->dn_reg = inp->din_root->dn_reg;
1347 	dt_cg_typecast(inp->din_root, dnp, dlp, drp);
1348 
1349 	if (idp->di_kind == DT_IDENT_ARRAY) {
1350 		for (i = 0; i < inp->din_argc; i++) {
1351 			pinp = inp->din_argv[i]->di_iarg;
1352 			pinp->din_root = NULL;
1353 		}
1354 	}
1355 }
1356 
1357 static void
1358 dt_cg_func_typeref(dtrace_hdl_t *dtp, dt_node_t *dnp)
1359 {
1360 	dtrace_typeinfo_t dtt;
1361 	dt_node_t *addr = dnp->dn_args;
1362 	dt_node_t *nelm = addr->dn_list;
1363 	dt_node_t *strp = nelm->dn_list;
1364 	dt_node_t *typs = strp->dn_list;
1365 	char buf[DT_TYPE_NAMELEN];
1366 	char *p;
1367 
1368 	ctf_type_name(addr->dn_ctfp, addr->dn_type, buf, sizeof (buf));
1369 
1370 	/*
1371 	 * XXX Hack alert! XXX
1372 	 * The prototype has two dummy args that we munge to represent
1373 	 * the type string and the type size.
1374 	 *
1375 	 * Yes, I hear your grumble, but it works for now. We'll come
1376 	 * up with a more elegant implementation later. :-)
1377 	 */
1378 	free(strp->dn_string);
1379 
1380 	if ((p = strchr(buf, '*')) != NULL)
1381 		*p = '\0';
1382 
1383 	strp->dn_string = strdup(buf);
1384 
1385 	if (dtrace_lookup_by_type(dtp,  DTRACE_OBJ_EVERY, buf, &dtt) < 0)
1386 		return;
1387 
1388 	typs->dn_value = ctf_type_size(dtt.dtt_ctfp, dtt.dtt_type);
1389 }
1390 
1391 typedef struct dt_xlmemb {
1392 	dt_ident_t *dtxl_idp;		/* translated ident */
1393 	dt_irlist_t *dtxl_dlp;		/* instruction list */
1394 	dt_regset_t *dtxl_drp;		/* register set */
1395 	int dtxl_sreg;			/* location of the translation input */
1396 	int dtxl_dreg;			/* location of our allocated buffer */
1397 } dt_xlmemb_t;
1398 
1399 /*ARGSUSED*/
1400 static int
1401 dt_cg_xlate_member(const char *name, ctf_id_t type, ulong_t off, void *arg)
1402 {
1403 	dt_xlmemb_t *dx = arg;
1404 	dt_ident_t *idp = dx->dtxl_idp;
1405 	dt_irlist_t *dlp = dx->dtxl_dlp;
1406 	dt_regset_t *drp = dx->dtxl_drp;
1407 
1408 	dt_node_t *mnp;
1409 	dt_xlator_t *dxp;
1410 
1411 	int reg, treg;
1412 	uint32_t instr;
1413 	size_t size;
1414 
1415 	/* Generate code for the translation. */
1416 	dxp = idp->di_data;
1417 	mnp = dt_xlator_member(dxp, name);
1418 
1419 	/* If there's no translator for the given member, skip it. */
1420 	if (mnp == NULL)
1421 		return (0);
1422 
1423 	dxp->dx_ident->di_flags |= DT_IDFLG_CGREG;
1424 	dxp->dx_ident->di_id = dx->dtxl_sreg;
1425 
1426 	dt_cg_node(mnp->dn_membexpr, dlp, drp);
1427 
1428 	dxp->dx_ident->di_flags &= ~DT_IDFLG_CGREG;
1429 	dxp->dx_ident->di_id = 0;
1430 
1431 	treg = mnp->dn_membexpr->dn_reg;
1432 
1433 	/* Compute the offset into our buffer and store the result there. */
1434 	reg = dt_regset_alloc(drp);
1435 
1436 	dt_cg_setx(dlp, reg, off / NBBY);
1437 	instr = DIF_INSTR_FMT(DIF_OP_ADD, dx->dtxl_dreg, reg, reg);
1438 	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
1439 
1440 	size = ctf_type_size(mnp->dn_membexpr->dn_ctfp,
1441 	    mnp->dn_membexpr->dn_type);
1442 	if (dt_node_is_scalar(mnp->dn_membexpr)) {
1443 		/*
1444 		 * Copying scalars is simple.
1445 		 */
1446 		switch (size) {
1447 		case 1:
1448 			instr = DIF_INSTR_STORE(DIF_OP_STB, treg, reg);
1449 			break;
1450 		case 2:
1451 			instr = DIF_INSTR_STORE(DIF_OP_STH, treg, reg);
1452 			break;
1453 		case 4:
1454 			instr = DIF_INSTR_STORE(DIF_OP_STW, treg, reg);
1455 			break;
1456 		case 8:
1457 			instr = DIF_INSTR_STORE(DIF_OP_STX, treg, reg);
1458 			break;
1459 		default:
1460 			xyerror(D_UNKNOWN, "internal error -- unexpected "
1461 			    "size: %lu\n", (ulong_t)size);
1462 		}
1463 
1464 		dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
1465 
1466 	} else if (dt_node_is_string(mnp->dn_membexpr)) {
1467 		int szreg;
1468 
1469 		/*
1470 		 * Use the copys instruction for strings.
1471 		 */
1472 		szreg = dt_regset_alloc(drp);
1473 		dt_cg_setx(dlp, szreg, size);
1474 		instr = DIF_INSTR_COPYS(treg, szreg, reg);
1475 		dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
1476 		dt_regset_free(drp, szreg);
1477 	} else {
1478 		int szreg;
1479 
1480 		/*
1481 		 * If it's anything else then we'll just bcopy it.
1482 		 */
1483 		szreg = dt_regset_alloc(drp);
1484 		dt_cg_setx(dlp, szreg, size);
1485 		dt_irlist_append(dlp,
1486 		    dt_cg_node_alloc(DT_LBL_NONE, DIF_INSTR_FLUSHTS));
1487 		instr = DIF_INSTR_PUSHTS(DIF_OP_PUSHTV, DIF_TYPE_CTF,
1488 		    DIF_REG_R0, treg);
1489 		dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
1490 		instr = DIF_INSTR_PUSHTS(DIF_OP_PUSHTV, DIF_TYPE_CTF,
1491 		    DIF_REG_R0, reg);
1492 		dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
1493 		instr = DIF_INSTR_PUSHTS(DIF_OP_PUSHTV, DIF_TYPE_CTF,
1494 		    DIF_REG_R0, szreg);
1495 		dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
1496 		instr = DIF_INSTR_CALL(DIF_SUBR_BCOPY, szreg);
1497 		dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
1498 		dt_regset_free(drp, szreg);
1499 	}
1500 
1501 	dt_regset_free(drp, reg);
1502 	dt_regset_free(drp, treg);
1503 
1504 	return (0);
1505 }
1506 
1507 /*
1508  * If we're expanding a translated type, we create an appropriately sized
1509  * buffer with alloca() and then translate each member into it.
1510  */
1511 static int
1512 dt_cg_xlate_expand(dt_node_t *dnp, dt_ident_t *idp, dt_irlist_t *dlp,
1513     dt_regset_t *drp)
1514 {
1515 	dt_xlmemb_t dlm;
1516 	uint32_t instr;
1517 	int dreg;
1518 	size_t size;
1519 
1520 	dreg = dt_regset_alloc(drp);
1521 	size = ctf_type_size(dnp->dn_ident->di_ctfp, dnp->dn_ident->di_type);
1522 
1523 	/* Call alloca() to create the buffer. */
1524 	dt_cg_setx(dlp, dreg, size);
1525 
1526 	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, DIF_INSTR_FLUSHTS));
1527 
1528 	instr = DIF_INSTR_PUSHTS(DIF_OP_PUSHTV, DIF_TYPE_CTF, DIF_REG_R0, dreg);
1529 	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
1530 
1531 	instr = DIF_INSTR_CALL(DIF_SUBR_ALLOCA, dreg);
1532 	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
1533 
1534 	/* Generate the translation for each member. */
1535 	dlm.dtxl_idp = idp;
1536 	dlm.dtxl_dlp = dlp;
1537 	dlm.dtxl_drp = drp;
1538 	dlm.dtxl_sreg = dnp->dn_reg;
1539 	dlm.dtxl_dreg = dreg;
1540 	(void) ctf_member_iter(dnp->dn_ident->di_ctfp,
1541 	    dnp->dn_ident->di_type, dt_cg_xlate_member,
1542 	    &dlm);
1543 
1544 	return (dreg);
1545 }
1546 
1547 static void
1548 dt_cg_node(dt_node_t *dnp, dt_irlist_t *dlp, dt_regset_t *drp)
1549 {
1550 	ctf_file_t *ctfp = dnp->dn_ctfp;
1551 	ctf_file_t *octfp;
1552 	ctf_membinfo_t m;
1553 	ctf_id_t type;
1554 
1555 	dif_instr_t instr;
1556 	dt_ident_t *idp;
1557 	ssize_t stroff;
1558 	uint_t op;
1559 
1560 	switch (dnp->dn_op) {
1561 	case DT_TOK_COMMA:
1562 		dt_cg_node(dnp->dn_left, dlp, drp);
1563 		dt_regset_free(drp, dnp->dn_left->dn_reg);
1564 		dt_cg_node(dnp->dn_right, dlp, drp);
1565 		dnp->dn_reg = dnp->dn_right->dn_reg;
1566 		break;
1567 
1568 	case DT_TOK_ASGN:
1569 		dt_cg_node(dnp->dn_right, dlp, drp);
1570 		dnp->dn_reg = dnp->dn_right->dn_reg;
1571 		dt_cg_asgn_op(dnp, dlp, drp);
1572 		break;
1573 
1574 	case DT_TOK_ADD_EQ:
1575 		dt_cg_arithmetic_op(dnp, dlp, drp, DIF_OP_ADD);
1576 		dt_cg_asgn_op(dnp, dlp, drp);
1577 		break;
1578 
1579 	case DT_TOK_SUB_EQ:
1580 		dt_cg_arithmetic_op(dnp, dlp, drp, DIF_OP_SUB);
1581 		dt_cg_asgn_op(dnp, dlp, drp);
1582 		break;
1583 
1584 	case DT_TOK_MUL_EQ:
1585 		dt_cg_arithmetic_op(dnp, dlp, drp, DIF_OP_MUL);
1586 		dt_cg_asgn_op(dnp, dlp, drp);
1587 		break;
1588 
1589 	case DT_TOK_DIV_EQ:
1590 		dt_cg_arithmetic_op(dnp, dlp, drp,
1591 		    (dnp->dn_flags & DT_NF_SIGNED) ? DIF_OP_SDIV : DIF_OP_UDIV);
1592 		dt_cg_asgn_op(dnp, dlp, drp);
1593 		break;
1594 
1595 	case DT_TOK_MOD_EQ:
1596 		dt_cg_arithmetic_op(dnp, dlp, drp,
1597 		    (dnp->dn_flags & DT_NF_SIGNED) ? DIF_OP_SREM : DIF_OP_UREM);
1598 		dt_cg_asgn_op(dnp, dlp, drp);
1599 		break;
1600 
1601 	case DT_TOK_AND_EQ:
1602 		dt_cg_arithmetic_op(dnp, dlp, drp, DIF_OP_AND);
1603 		dt_cg_asgn_op(dnp, dlp, drp);
1604 		break;
1605 
1606 	case DT_TOK_XOR_EQ:
1607 		dt_cg_arithmetic_op(dnp, dlp, drp, DIF_OP_XOR);
1608 		dt_cg_asgn_op(dnp, dlp, drp);
1609 		break;
1610 
1611 	case DT_TOK_OR_EQ:
1612 		dt_cg_arithmetic_op(dnp, dlp, drp, DIF_OP_OR);
1613 		dt_cg_asgn_op(dnp, dlp, drp);
1614 		break;
1615 
1616 	case DT_TOK_LSH_EQ:
1617 		dt_cg_arithmetic_op(dnp, dlp, drp, DIF_OP_SLL);
1618 		dt_cg_asgn_op(dnp, dlp, drp);
1619 		break;
1620 
1621 	case DT_TOK_RSH_EQ:
1622 		dt_cg_arithmetic_op(dnp, dlp, drp,
1623 		    (dnp->dn_flags & DT_NF_SIGNED) ? DIF_OP_SRA : DIF_OP_SRL);
1624 		dt_cg_asgn_op(dnp, dlp, drp);
1625 		break;
1626 
1627 	case DT_TOK_QUESTION:
1628 		dt_cg_ternary_op(dnp, dlp, drp);
1629 		break;
1630 
1631 	case DT_TOK_LOR:
1632 		dt_cg_logical_or(dnp, dlp, drp);
1633 		break;
1634 
1635 	case DT_TOK_LXOR:
1636 		dt_cg_logical_xor(dnp, dlp, drp);
1637 		break;
1638 
1639 	case DT_TOK_LAND:
1640 		dt_cg_logical_and(dnp, dlp, drp);
1641 		break;
1642 
1643 	case DT_TOK_BOR:
1644 		dt_cg_arithmetic_op(dnp, dlp, drp, DIF_OP_OR);
1645 		break;
1646 
1647 	case DT_TOK_XOR:
1648 		dt_cg_arithmetic_op(dnp, dlp, drp, DIF_OP_XOR);
1649 		break;
1650 
1651 	case DT_TOK_BAND:
1652 		dt_cg_arithmetic_op(dnp, dlp, drp, DIF_OP_AND);
1653 		break;
1654 
1655 	case DT_TOK_EQU:
1656 		dt_cg_compare_op(dnp, dlp, drp, DIF_OP_BE);
1657 		break;
1658 
1659 	case DT_TOK_NEQ:
1660 		dt_cg_compare_op(dnp, dlp, drp, DIF_OP_BNE);
1661 		break;
1662 
1663 	case DT_TOK_LT:
1664 		dt_cg_compare_op(dnp, dlp, drp,
1665 		    dt_cg_compare_signed(dnp) ? DIF_OP_BL : DIF_OP_BLU);
1666 		break;
1667 
1668 	case DT_TOK_LE:
1669 		dt_cg_compare_op(dnp, dlp, drp,
1670 		    dt_cg_compare_signed(dnp) ? DIF_OP_BLE : DIF_OP_BLEU);
1671 		break;
1672 
1673 	case DT_TOK_GT:
1674 		dt_cg_compare_op(dnp, dlp, drp,
1675 		    dt_cg_compare_signed(dnp) ? DIF_OP_BG : DIF_OP_BGU);
1676 		break;
1677 
1678 	case DT_TOK_GE:
1679 		dt_cg_compare_op(dnp, dlp, drp,
1680 		    dt_cg_compare_signed(dnp) ? DIF_OP_BGE : DIF_OP_BGEU);
1681 		break;
1682 
1683 	case DT_TOK_LSH:
1684 		dt_cg_arithmetic_op(dnp, dlp, drp, DIF_OP_SLL);
1685 		break;
1686 
1687 	case DT_TOK_RSH:
1688 		dt_cg_arithmetic_op(dnp, dlp, drp,
1689 		    (dnp->dn_flags & DT_NF_SIGNED) ? DIF_OP_SRA : DIF_OP_SRL);
1690 		break;
1691 
1692 	case DT_TOK_ADD:
1693 		dt_cg_arithmetic_op(dnp, dlp, drp, DIF_OP_ADD);
1694 		break;
1695 
1696 	case DT_TOK_SUB:
1697 		dt_cg_arithmetic_op(dnp, dlp, drp, DIF_OP_SUB);
1698 		break;
1699 
1700 	case DT_TOK_MUL:
1701 		dt_cg_arithmetic_op(dnp, dlp, drp, DIF_OP_MUL);
1702 		break;
1703 
1704 	case DT_TOK_DIV:
1705 		dt_cg_arithmetic_op(dnp, dlp, drp,
1706 		    (dnp->dn_flags & DT_NF_SIGNED) ? DIF_OP_SDIV : DIF_OP_UDIV);
1707 		break;
1708 
1709 	case DT_TOK_MOD:
1710 		dt_cg_arithmetic_op(dnp, dlp, drp,
1711 		    (dnp->dn_flags & DT_NF_SIGNED) ? DIF_OP_SREM : DIF_OP_UREM);
1712 		break;
1713 
1714 	case DT_TOK_LNEG:
1715 		dt_cg_logical_neg(dnp, dlp, drp);
1716 		break;
1717 
1718 	case DT_TOK_BNEG:
1719 		dt_cg_node(dnp->dn_child, dlp, drp);
1720 		dnp->dn_reg = dnp->dn_child->dn_reg;
1721 		instr = DIF_INSTR_NOT(dnp->dn_reg, dnp->dn_reg);
1722 		dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
1723 		break;
1724 
1725 	case DT_TOK_PREINC:
1726 		dt_cg_prearith_op(dnp, dlp, drp, DIF_OP_ADD);
1727 		break;
1728 
1729 	case DT_TOK_POSTINC:
1730 		dt_cg_postarith_op(dnp, dlp, drp, DIF_OP_ADD);
1731 		break;
1732 
1733 	case DT_TOK_PREDEC:
1734 		dt_cg_prearith_op(dnp, dlp, drp, DIF_OP_SUB);
1735 		break;
1736 
1737 	case DT_TOK_POSTDEC:
1738 		dt_cg_postarith_op(dnp, dlp, drp, DIF_OP_SUB);
1739 		break;
1740 
1741 	case DT_TOK_IPOS:
1742 		dt_cg_node(dnp->dn_child, dlp, drp);
1743 		dnp->dn_reg = dnp->dn_child->dn_reg;
1744 		break;
1745 
1746 	case DT_TOK_INEG:
1747 		dt_cg_node(dnp->dn_child, dlp, drp);
1748 		dnp->dn_reg = dnp->dn_child->dn_reg;
1749 
1750 		instr = DIF_INSTR_FMT(DIF_OP_SUB, DIF_REG_R0,
1751 		    dnp->dn_reg, dnp->dn_reg);
1752 
1753 		dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
1754 		break;
1755 
1756 	case DT_TOK_DEREF:
1757 		dt_cg_node(dnp->dn_child, dlp, drp);
1758 		dnp->dn_reg = dnp->dn_child->dn_reg;
1759 
1760 		if (dt_node_is_dynamic(dnp->dn_child)) {
1761 			int reg;
1762 			idp = dt_node_resolve(dnp->dn_child, DT_IDENT_XLPTR);
1763 			assert(idp != NULL);
1764 			reg = dt_cg_xlate_expand(dnp, idp, dlp, drp);
1765 
1766 			dt_regset_free(drp, dnp->dn_child->dn_reg);
1767 			dnp->dn_reg = reg;
1768 
1769 		} else if (!(dnp->dn_flags & DT_NF_REF)) {
1770 			uint_t ubit = dnp->dn_flags & DT_NF_USERLAND;
1771 
1772 			/*
1773 			 * Save and restore DT_NF_USERLAND across dt_cg_load():
1774 			 * we need the sign bit from dnp and the user bit from
1775 			 * dnp->dn_child in order to get the proper opcode.
1776 			 */
1777 			dnp->dn_flags |=
1778 			    (dnp->dn_child->dn_flags & DT_NF_USERLAND);
1779 
1780 			instr = DIF_INSTR_LOAD(dt_cg_load(dnp, ctfp,
1781 			    dnp->dn_type), dnp->dn_reg, dnp->dn_reg);
1782 
1783 			dnp->dn_flags &= ~DT_NF_USERLAND;
1784 			dnp->dn_flags |= ubit;
1785 
1786 			dt_irlist_append(dlp,
1787 			    dt_cg_node_alloc(DT_LBL_NONE, instr));
1788 		}
1789 		break;
1790 
1791 	case DT_TOK_ADDROF: {
1792 		uint_t rbit = dnp->dn_child->dn_flags & DT_NF_REF;
1793 
1794 		dnp->dn_child->dn_flags |= DT_NF_REF; /* force pass-by-ref */
1795 		dt_cg_node(dnp->dn_child, dlp, drp);
1796 		dnp->dn_reg = dnp->dn_child->dn_reg;
1797 
1798 		dnp->dn_child->dn_flags &= ~DT_NF_REF;
1799 		dnp->dn_child->dn_flags |= rbit;
1800 		break;
1801 	}
1802 
1803 	case DT_TOK_SIZEOF: {
1804 		size_t size = dt_node_sizeof(dnp->dn_child);
1805 		dnp->dn_reg = dt_regset_alloc(drp);
1806 		assert(size != 0);
1807 		dt_cg_setx(dlp, dnp->dn_reg, size);
1808 		break;
1809 	}
1810 
1811 	case DT_TOK_STRINGOF:
1812 		dt_cg_node(dnp->dn_child, dlp, drp);
1813 		dnp->dn_reg = dnp->dn_child->dn_reg;
1814 		break;
1815 
1816 	case DT_TOK_XLATE:
1817 		/*
1818 		 * An xlate operator appears in either an XLATOR, indicating a
1819 		 * reference to a dynamic translator, or an OP2, indicating
1820 		 * use of the xlate operator in the user's program.  For the
1821 		 * dynamic case, generate an xlate opcode with a reference to
1822 		 * the corresponding member, pre-computed for us in dn_members.
1823 		 */
1824 		if (dnp->dn_kind == DT_NODE_XLATOR) {
1825 			dt_xlator_t *dxp = dnp->dn_xlator;
1826 
1827 			assert(dxp->dx_ident->di_flags & DT_IDFLG_CGREG);
1828 			assert(dxp->dx_ident->di_id != 0);
1829 
1830 			dnp->dn_reg = dt_regset_alloc(drp);
1831 
1832 			if (dxp->dx_arg == -1) {
1833 				instr = DIF_INSTR_MOV(
1834 				    dxp->dx_ident->di_id, dnp->dn_reg);
1835 				dt_irlist_append(dlp,
1836 				    dt_cg_node_alloc(DT_LBL_NONE, instr));
1837 				op = DIF_OP_XLATE;
1838 			} else
1839 				op = DIF_OP_XLARG;
1840 
1841 			instr = DIF_INSTR_XLATE(op, 0, dnp->dn_reg);
1842 			dt_irlist_append(dlp,
1843 			    dt_cg_node_alloc(DT_LBL_NONE, instr));
1844 
1845 			dlp->dl_last->di_extern = dnp->dn_xmember;
1846 			break;
1847 		}
1848 
1849 		assert(dnp->dn_kind == DT_NODE_OP2);
1850 		dt_cg_node(dnp->dn_right, dlp, drp);
1851 		dnp->dn_reg = dnp->dn_right->dn_reg;
1852 		break;
1853 
1854 	case DT_TOK_LPAR:
1855 		dt_cg_node(dnp->dn_right, dlp, drp);
1856 		dnp->dn_reg = dnp->dn_right->dn_reg;
1857 		dt_cg_typecast(dnp->dn_right, dnp, dlp, drp);
1858 		break;
1859 
1860 	case DT_TOK_PTR:
1861 	case DT_TOK_DOT:
1862 		assert(dnp->dn_right->dn_kind == DT_NODE_IDENT);
1863 		dt_cg_node(dnp->dn_left, dlp, drp);
1864 
1865 		/*
1866 		 * If the left-hand side of PTR or DOT is a dynamic variable,
1867 		 * we expect it to be the output of a D translator.   In this
1868 		 * case, we look up the parse tree corresponding to the member
1869 		 * that is being accessed and run the code generator over it.
1870 		 * We then cast the result as if by the assignment operator.
1871 		 */
1872 		if ((idp = dt_node_resolve(
1873 		    dnp->dn_left, DT_IDENT_XLSOU)) != NULL ||
1874 		    (idp = dt_node_resolve(
1875 		    dnp->dn_left, DT_IDENT_XLPTR)) != NULL) {
1876 
1877 			dt_xlator_t *dxp;
1878 			dt_node_t *mnp;
1879 
1880 			dxp = idp->di_data;
1881 			mnp = dt_xlator_member(dxp, dnp->dn_right->dn_string);
1882 			assert(mnp != NULL);
1883 
1884 			dxp->dx_ident->di_flags |= DT_IDFLG_CGREG;
1885 			dxp->dx_ident->di_id = dnp->dn_left->dn_reg;
1886 
1887 			dt_cg_node(mnp->dn_membexpr, dlp, drp);
1888 			dnp->dn_reg = mnp->dn_membexpr->dn_reg;
1889 			dt_cg_typecast(mnp->dn_membexpr, dnp, dlp, drp);
1890 
1891 			dxp->dx_ident->di_flags &= ~DT_IDFLG_CGREG;
1892 			dxp->dx_ident->di_id = 0;
1893 
1894 			if (dnp->dn_left->dn_reg != -1)
1895 				dt_regset_free(drp, dnp->dn_left->dn_reg);
1896 			break;
1897 		}
1898 
1899 		ctfp = dnp->dn_left->dn_ctfp;
1900 		type = ctf_type_resolve(ctfp, dnp->dn_left->dn_type);
1901 
1902 		if (dnp->dn_op == DT_TOK_PTR) {
1903 			type = ctf_type_reference(ctfp, type);
1904 			type = ctf_type_resolve(ctfp, type);
1905 		}
1906 
1907 		if ((ctfp = dt_cg_membinfo(octfp = ctfp, type,
1908 		    dnp->dn_right->dn_string, &m)) == NULL) {
1909 			yypcb->pcb_hdl->dt_ctferr = ctf_errno(octfp);
1910 			longjmp(yypcb->pcb_jmpbuf, EDT_CTF);
1911 		}
1912 
1913 		if (m.ctm_offset != 0) {
1914 			int reg;
1915 
1916 			reg = dt_regset_alloc(drp);
1917 
1918 			/*
1919 			 * If the offset is not aligned on a byte boundary, it
1920 			 * is a bit-field member and we will extract the value
1921 			 * bits below after we generate the appropriate load.
1922 			 */
1923 			dt_cg_setx(dlp, reg, m.ctm_offset / NBBY);
1924 
1925 			instr = DIF_INSTR_FMT(DIF_OP_ADD,
1926 			    dnp->dn_left->dn_reg, reg, dnp->dn_left->dn_reg);
1927 
1928 			dt_irlist_append(dlp,
1929 			    dt_cg_node_alloc(DT_LBL_NONE, instr));
1930 			dt_regset_free(drp, reg);
1931 		}
1932 
1933 		if (!(dnp->dn_flags & DT_NF_REF)) {
1934 			uint_t ubit = dnp->dn_flags & DT_NF_USERLAND;
1935 
1936 			/*
1937 			 * Save and restore DT_NF_USERLAND across dt_cg_load():
1938 			 * we need the sign bit from dnp and the user bit from
1939 			 * dnp->dn_left in order to get the proper opcode.
1940 			 */
1941 			dnp->dn_flags |=
1942 			    (dnp->dn_left->dn_flags & DT_NF_USERLAND);
1943 
1944 			instr = DIF_INSTR_LOAD(dt_cg_load(dnp,
1945 			    ctfp, m.ctm_type), dnp->dn_left->dn_reg,
1946 			    dnp->dn_left->dn_reg);
1947 
1948 			dnp->dn_flags &= ~DT_NF_USERLAND;
1949 			dnp->dn_flags |= ubit;
1950 
1951 			dt_irlist_append(dlp,
1952 			    dt_cg_node_alloc(DT_LBL_NONE, instr));
1953 
1954 			if (dnp->dn_flags & DT_NF_BITFIELD)
1955 				dt_cg_field_get(dnp, dlp, drp, ctfp, &m);
1956 		}
1957 
1958 		dnp->dn_reg = dnp->dn_left->dn_reg;
1959 		break;
1960 
1961 	case DT_TOK_STRING:
1962 		dnp->dn_reg = dt_regset_alloc(drp);
1963 
1964 		assert(dnp->dn_kind == DT_NODE_STRING);
1965 		stroff = dt_strtab_insert(yypcb->pcb_strtab, dnp->dn_string);
1966 
1967 		if (stroff == -1L)
1968 			longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
1969 		if (stroff > DIF_STROFF_MAX)
1970 			longjmp(yypcb->pcb_jmpbuf, EDT_STR2BIG);
1971 
1972 		instr = DIF_INSTR_SETS((ulong_t)stroff, dnp->dn_reg);
1973 		dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
1974 		break;
1975 
1976 	case DT_TOK_IDENT:
1977 		/*
1978 		 * If the specified identifier is a variable on which we have
1979 		 * set the code generator register flag, then this variable
1980 		 * has already had code generated for it and saved in di_id.
1981 		 * Allocate a new register and copy the existing value to it.
1982 		 */
1983 		if (dnp->dn_kind == DT_NODE_VAR &&
1984 		    (dnp->dn_ident->di_flags & DT_IDFLG_CGREG)) {
1985 			dnp->dn_reg = dt_regset_alloc(drp);
1986 			instr = DIF_INSTR_MOV(dnp->dn_ident->di_id,
1987 			    dnp->dn_reg);
1988 			dt_irlist_append(dlp,
1989 			    dt_cg_node_alloc(DT_LBL_NONE, instr));
1990 			break;
1991 		}
1992 
1993 		/*
1994 		 * Identifiers can represent function calls, variable refs, or
1995 		 * symbols.  First we check for inlined variables, and handle
1996 		 * them by generating code for the inline parse tree.
1997 		 */
1998 		if (dnp->dn_kind == DT_NODE_VAR &&
1999 		    (dnp->dn_ident->di_flags & DT_IDFLG_INLINE)) {
2000 			dt_cg_inline(dnp, dlp, drp);
2001 			break;
2002 		}
2003 
2004 		switch (dnp->dn_kind) {
2005 		case DT_NODE_FUNC: {
2006 			dtrace_hdl_t *dtp = yypcb->pcb_hdl;
2007 
2008 			if ((idp = dnp->dn_ident)->di_kind != DT_IDENT_FUNC) {
2009 				dnerror(dnp, D_CG_EXPR, "%s %s( ) may not be "
2010 				    "called from a D expression (D program "
2011 				    "context required)\n",
2012 				    dt_idkind_name(idp->di_kind), idp->di_name);
2013 			}
2014 
2015 			switch (idp->di_id) {
2016 			case DIF_SUBR_TYPEREF:
2017 				dt_cg_func_typeref(dtp, dnp);
2018 				break;
2019 
2020 			default:
2021 				break;
2022 			}
2023 
2024 			dt_cg_arglist(dnp->dn_ident, dnp->dn_args, dlp, drp);
2025 
2026 			dnp->dn_reg = dt_regset_alloc(drp);
2027 			instr = DIF_INSTR_CALL(dnp->dn_ident->di_id,
2028 			    dnp->dn_reg);
2029 
2030 			dt_irlist_append(dlp,
2031 			    dt_cg_node_alloc(DT_LBL_NONE, instr));
2032 
2033 			break;
2034 		}
2035 
2036 		case DT_NODE_VAR:
2037 			if (dnp->dn_ident->di_kind == DT_IDENT_XLSOU ||
2038 			    dnp->dn_ident->di_kind == DT_IDENT_XLPTR) {
2039 				/*
2040 				 * This can only happen if we have translated
2041 				 * args[].  See dt_idcook_args() for details.
2042 				 */
2043 				assert(dnp->dn_ident->di_id == DIF_VAR_ARGS);
2044 				dt_cg_array_op(dnp, dlp, drp);
2045 				break;
2046 			}
2047 
2048 			if (dnp->dn_ident->di_kind == DT_IDENT_ARRAY) {
2049 				if (dnp->dn_ident->di_id > DIF_VAR_ARRAY_MAX)
2050 					dt_cg_assoc_op(dnp, dlp, drp);
2051 				else
2052 					dt_cg_array_op(dnp, dlp, drp);
2053 				break;
2054 			}
2055 
2056 			dnp->dn_reg = dt_regset_alloc(drp);
2057 
2058 			if (dnp->dn_ident->di_flags & DT_IDFLG_LOCAL)
2059 				op = DIF_OP_LDLS;
2060 			else if (dnp->dn_ident->di_flags & DT_IDFLG_TLS)
2061 				op = DIF_OP_LDTS;
2062 			else
2063 				op = DIF_OP_LDGS;
2064 
2065 			dnp->dn_ident->di_flags |= DT_IDFLG_DIFR;
2066 
2067 			instr = DIF_INSTR_LDV(op,
2068 			    dnp->dn_ident->di_id, dnp->dn_reg);
2069 
2070 			dt_irlist_append(dlp,
2071 			    dt_cg_node_alloc(DT_LBL_NONE, instr));
2072 			break;
2073 
2074 		case DT_NODE_SYM: {
2075 			dtrace_hdl_t *dtp = yypcb->pcb_hdl;
2076 			dtrace_syminfo_t *sip = dnp->dn_ident->di_data;
2077 			GElf_Sym sym;
2078 
2079 			if (dtrace_lookup_by_name(dtp,
2080 			    sip->dts_object, sip->dts_name, &sym, NULL) == -1) {
2081 				xyerror(D_UNKNOWN, "cg failed for symbol %s`%s:"
2082 				    " %s\n", sip->dts_object, sip->dts_name,
2083 				    dtrace_errmsg(dtp, dtrace_errno(dtp)));
2084 			}
2085 
2086 			dnp->dn_reg = dt_regset_alloc(drp);
2087 			dt_cg_xsetx(dlp, dnp->dn_ident,
2088 			    DT_LBL_NONE, dnp->dn_reg, sym.st_value);
2089 
2090 			if (!(dnp->dn_flags & DT_NF_REF)) {
2091 				instr = DIF_INSTR_LOAD(dt_cg_load(dnp, ctfp,
2092 				    dnp->dn_type), dnp->dn_reg, dnp->dn_reg);
2093 				dt_irlist_append(dlp,
2094 				    dt_cg_node_alloc(DT_LBL_NONE, instr));
2095 			}
2096 			break;
2097 		}
2098 
2099 		default:
2100 			xyerror(D_UNKNOWN, "internal error -- node type %u is "
2101 			    "not valid for an identifier\n", dnp->dn_kind);
2102 		}
2103 		break;
2104 
2105 	case DT_TOK_INT:
2106 		dnp->dn_reg = dt_regset_alloc(drp);
2107 		dt_cg_setx(dlp, dnp->dn_reg, dnp->dn_value);
2108 		break;
2109 
2110 	default:
2111 		xyerror(D_UNKNOWN, "internal error -- token type %u is not a "
2112 		    "valid D compilation token\n", dnp->dn_op);
2113 	}
2114 }
2115 
2116 void
2117 dt_cg(dt_pcb_t *pcb, dt_node_t *dnp)
2118 {
2119 	dif_instr_t instr;
2120 	dt_xlator_t *dxp = NULL;	// XXX: gcc
2121 	dt_ident_t *idp;
2122 
2123 	if (pcb->pcb_regs == NULL && (pcb->pcb_regs =
2124 	    dt_regset_create(pcb->pcb_hdl->dt_conf.dtc_difintregs)) == NULL)
2125 		longjmp(pcb->pcb_jmpbuf, EDT_NOMEM);
2126 
2127 	dt_regset_reset(pcb->pcb_regs);
2128 	(void) dt_regset_alloc(pcb->pcb_regs); /* allocate %r0 */
2129 
2130 	if (pcb->pcb_inttab != NULL)
2131 		dt_inttab_destroy(pcb->pcb_inttab);
2132 
2133 	if ((pcb->pcb_inttab = dt_inttab_create(yypcb->pcb_hdl)) == NULL)
2134 		longjmp(pcb->pcb_jmpbuf, EDT_NOMEM);
2135 
2136 	if (pcb->pcb_strtab != NULL)
2137 		dt_strtab_destroy(pcb->pcb_strtab);
2138 
2139 	if ((pcb->pcb_strtab = dt_strtab_create(BUFSIZ)) == NULL)
2140 		longjmp(pcb->pcb_jmpbuf, EDT_NOMEM);
2141 
2142 	dt_irlist_destroy(&pcb->pcb_ir);
2143 	dt_irlist_create(&pcb->pcb_ir);
2144 
2145 	assert(pcb->pcb_dret == NULL);
2146 	pcb->pcb_dret = dnp;
2147 
2148 	if (dt_node_resolve(dnp, DT_IDENT_XLPTR) != NULL) {
2149 		dnerror(dnp, D_CG_DYN, "expression cannot evaluate to result "
2150 		    "of a translated pointer\n");
2151 	}
2152 
2153 	/*
2154 	 * If we're generating code for a translator body, assign the input
2155 	 * parameter to the first available register (i.e. caller passes %r1).
2156 	 */
2157 	if (dnp->dn_kind == DT_NODE_MEMBER) {
2158 		dxp = dnp->dn_membxlator;
2159 		dnp = dnp->dn_membexpr;
2160 
2161 		dxp->dx_ident->di_flags |= DT_IDFLG_CGREG;
2162 		dxp->dx_ident->di_id = dt_regset_alloc(pcb->pcb_regs);
2163 	}
2164 
2165 	dt_cg_node(dnp, &pcb->pcb_ir, pcb->pcb_regs);
2166 
2167 	if ((idp = dt_node_resolve(dnp, DT_IDENT_XLSOU)) != NULL) {
2168 		int reg = dt_cg_xlate_expand(dnp, idp,
2169 		    &pcb->pcb_ir, pcb->pcb_regs);
2170 		dt_regset_free(pcb->pcb_regs, dnp->dn_reg);
2171 		dnp->dn_reg = reg;
2172 	}
2173 
2174 	instr = DIF_INSTR_RET(dnp->dn_reg);
2175 	dt_regset_free(pcb->pcb_regs, dnp->dn_reg);
2176 	dt_irlist_append(&pcb->pcb_ir, dt_cg_node_alloc(DT_LBL_NONE, instr));
2177 
2178 	if (dnp->dn_kind == DT_NODE_MEMBER) {
2179 		dt_regset_free(pcb->pcb_regs, dxp->dx_ident->di_id);
2180 		dxp->dx_ident->di_id = 0;
2181 		dxp->dx_ident->di_flags &= ~DT_IDFLG_CGREG;
2182 	}
2183 
2184 	dt_regset_free(pcb->pcb_regs, 0);
2185 	dt_regset_assert_free(pcb->pcb_regs);
2186 }
2187