10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
70Sstevel@tonic-gate * with the License.
80Sstevel@tonic-gate *
90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate * See the License for the specific language governing permissions
120Sstevel@tonic-gate * and limitations under the License.
130Sstevel@tonic-gate *
140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate *
200Sstevel@tonic-gate * CDDL HEADER END
210Sstevel@tonic-gate */
220Sstevel@tonic-gate /*
230Sstevel@tonic-gate * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
240Sstevel@tonic-gate * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate
270Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
280Sstevel@tonic-gate
290Sstevel@tonic-gate #include <sys/types.h>
300Sstevel@tonic-gate #include <sys/sysmacros.h>
310Sstevel@tonic-gate #include <sys/isa_defs.h>
320Sstevel@tonic-gate
330Sstevel@tonic-gate #include <strings.h>
340Sstevel@tonic-gate #include <stdlib.h>
350Sstevel@tonic-gate #include <setjmp.h>
360Sstevel@tonic-gate #include <assert.h>
370Sstevel@tonic-gate #include <errno.h>
380Sstevel@tonic-gate
390Sstevel@tonic-gate #include <dt_impl.h>
400Sstevel@tonic-gate #include <dt_grammar.h>
410Sstevel@tonic-gate #include <dt_parser.h>
420Sstevel@tonic-gate #include <dt_provider.h>
430Sstevel@tonic-gate
440Sstevel@tonic-gate static void dt_cg_node(dt_node_t *, dt_irlist_t *, dt_regset_t *);
450Sstevel@tonic-gate
460Sstevel@tonic-gate static dt_irnode_t *
dt_cg_node_alloc(uint_t label,dif_instr_t instr)470Sstevel@tonic-gate dt_cg_node_alloc(uint_t label, dif_instr_t instr)
480Sstevel@tonic-gate {
490Sstevel@tonic-gate dt_irnode_t *dip = malloc(sizeof (dt_irnode_t));
500Sstevel@tonic-gate
510Sstevel@tonic-gate if (dip == NULL)
520Sstevel@tonic-gate longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
530Sstevel@tonic-gate
540Sstevel@tonic-gate dip->di_label = label;
550Sstevel@tonic-gate dip->di_instr = instr;
56*265Smws dip->di_extern = NULL;
570Sstevel@tonic-gate dip->di_next = NULL;
580Sstevel@tonic-gate
590Sstevel@tonic-gate return (dip);
600Sstevel@tonic-gate }
610Sstevel@tonic-gate
620Sstevel@tonic-gate /*
630Sstevel@tonic-gate * Code generator wrapper function for ctf_member_info. If we are given a
640Sstevel@tonic-gate * reference to a forward declaration tag, search the entire type space for
650Sstevel@tonic-gate * the actual definition and then call ctf_member_info on the result.
660Sstevel@tonic-gate */
670Sstevel@tonic-gate static ctf_file_t *
dt_cg_membinfo(ctf_file_t * fp,ctf_id_t type,const char * s,ctf_membinfo_t * mp)680Sstevel@tonic-gate dt_cg_membinfo(ctf_file_t *fp, ctf_id_t type, const char *s, ctf_membinfo_t *mp)
690Sstevel@tonic-gate {
700Sstevel@tonic-gate while (ctf_type_kind(fp, type) == CTF_K_FORWARD) {
710Sstevel@tonic-gate char n[DT_TYPE_NAMELEN];
720Sstevel@tonic-gate dtrace_typeinfo_t dtt;
730Sstevel@tonic-gate
740Sstevel@tonic-gate if (ctf_type_name(fp, type, n, sizeof (n)) == NULL ||
750Sstevel@tonic-gate dt_type_lookup(n, &dtt) == -1 || (
760Sstevel@tonic-gate dtt.dtt_ctfp == fp && dtt.dtt_type == type))
770Sstevel@tonic-gate break; /* unable to improve our position */
780Sstevel@tonic-gate
790Sstevel@tonic-gate fp = dtt.dtt_ctfp;
800Sstevel@tonic-gate type = ctf_type_resolve(fp, dtt.dtt_type);
810Sstevel@tonic-gate }
820Sstevel@tonic-gate
830Sstevel@tonic-gate if (ctf_member_info(fp, type, s, mp) == CTF_ERR)
840Sstevel@tonic-gate return (NULL); /* ctf_errno is set for us */
850Sstevel@tonic-gate
860Sstevel@tonic-gate return (fp);
870Sstevel@tonic-gate }
880Sstevel@tonic-gate
890Sstevel@tonic-gate static void
dt_cg_xsetx(dt_irlist_t * dlp,dt_ident_t * idp,uint_t lbl,int reg,uint64_t x)900Sstevel@tonic-gate dt_cg_xsetx(dt_irlist_t *dlp, dt_ident_t *idp, uint_t lbl, int reg, uint64_t x)
910Sstevel@tonic-gate {
920Sstevel@tonic-gate int flag = idp != NULL ? DT_INT_PRIVATE : DT_INT_SHARED;
930Sstevel@tonic-gate int intoff = dt_inttab_insert(yypcb->pcb_inttab, x, flag);
940Sstevel@tonic-gate dif_instr_t instr = DIF_INSTR_SETX((uint_t)intoff, reg);
950Sstevel@tonic-gate
960Sstevel@tonic-gate if (intoff == -1)
970Sstevel@tonic-gate longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
980Sstevel@tonic-gate
990Sstevel@tonic-gate if (intoff > DIF_INTOFF_MAX)
1000Sstevel@tonic-gate longjmp(yypcb->pcb_jmpbuf, EDT_INT2BIG);
1010Sstevel@tonic-gate
1020Sstevel@tonic-gate dt_irlist_append(dlp, dt_cg_node_alloc(lbl, instr));
1030Sstevel@tonic-gate
1040Sstevel@tonic-gate if (idp != NULL)
105*265Smws dlp->dl_last->di_extern = idp;
1060Sstevel@tonic-gate }
1070Sstevel@tonic-gate
1080Sstevel@tonic-gate static void
dt_cg_setx(dt_irlist_t * dlp,int reg,uint64_t x)1090Sstevel@tonic-gate dt_cg_setx(dt_irlist_t *dlp, int reg, uint64_t x)
1100Sstevel@tonic-gate {
1110Sstevel@tonic-gate dt_cg_xsetx(dlp, NULL, DT_LBL_NONE, reg, x);
1120Sstevel@tonic-gate }
1130Sstevel@tonic-gate
1140Sstevel@tonic-gate /*
1150Sstevel@tonic-gate * When loading bit-fields, we want to convert a byte count in the range
1160Sstevel@tonic-gate * 1-8 to the closest power of 2 (e.g. 3->4, 5->8, etc). The clp2() function
1170Sstevel@tonic-gate * is a clever implementation from "Hacker's Delight" by Henry Warren, Jr.
1180Sstevel@tonic-gate */
1190Sstevel@tonic-gate static size_t
clp2(size_t x)1200Sstevel@tonic-gate clp2(size_t x)
1210Sstevel@tonic-gate {
1220Sstevel@tonic-gate x--;
1230Sstevel@tonic-gate
1240Sstevel@tonic-gate x |= (x >> 1);
1250Sstevel@tonic-gate x |= (x >> 2);
1260Sstevel@tonic-gate x |= (x >> 4);
1270Sstevel@tonic-gate x |= (x >> 8);
1280Sstevel@tonic-gate x |= (x >> 16);
1290Sstevel@tonic-gate
1300Sstevel@tonic-gate return (x + 1);
1310Sstevel@tonic-gate }
1320Sstevel@tonic-gate
1330Sstevel@tonic-gate /*
1340Sstevel@tonic-gate * Lookup the correct load opcode to use for the specified node and CTF type.
1350Sstevel@tonic-gate * We determine the size and convert it to a 3-bit index. Our lookup table
1360Sstevel@tonic-gate * is constructed to use a 5-bit index, consisting of the 3-bit size 0-7, a
1370Sstevel@tonic-gate * bit for the sign, and a bit for userland address. For example, a 4-byte
1380Sstevel@tonic-gate * signed load from userland would be at the following table index:
1390Sstevel@tonic-gate * user=1 sign=1 size=4 => binary index 11011 = decimal index 27
1400Sstevel@tonic-gate */
1410Sstevel@tonic-gate static uint_t
dt_cg_load(dt_node_t * dnp,ctf_file_t * ctfp,ctf_id_t type)1420Sstevel@tonic-gate dt_cg_load(dt_node_t *dnp, ctf_file_t *ctfp, ctf_id_t type)
1430Sstevel@tonic-gate {
1440Sstevel@tonic-gate static const uint_t ops[] = {
1450Sstevel@tonic-gate DIF_OP_LDUB, DIF_OP_LDUH, 0, DIF_OP_LDUW,
1460Sstevel@tonic-gate 0, 0, 0, DIF_OP_LDX,
1470Sstevel@tonic-gate DIF_OP_LDSB, DIF_OP_LDSH, 0, DIF_OP_LDSW,
1480Sstevel@tonic-gate 0, 0, 0, DIF_OP_LDX,
1490Sstevel@tonic-gate DIF_OP_ULDUB, DIF_OP_ULDUH, 0, DIF_OP_ULDUW,
1500Sstevel@tonic-gate 0, 0, 0, DIF_OP_ULDX,
1510Sstevel@tonic-gate DIF_OP_ULDSB, DIF_OP_ULDSH, 0, DIF_OP_ULDSW,
1520Sstevel@tonic-gate 0, 0, 0, DIF_OP_ULDX,
1530Sstevel@tonic-gate };
1540Sstevel@tonic-gate
1550Sstevel@tonic-gate ctf_encoding_t e;
1560Sstevel@tonic-gate ssize_t size;
1570Sstevel@tonic-gate
1580Sstevel@tonic-gate /*
1590Sstevel@tonic-gate * If we're loading a bit-field, the size of our load is found by
1600Sstevel@tonic-gate * rounding cte_bits up to a byte boundary and then finding the
1610Sstevel@tonic-gate * nearest power of two to this value (see clp2(), above).
1620Sstevel@tonic-gate */
1630Sstevel@tonic-gate if ((dnp->dn_flags & DT_NF_BITFIELD) &&
1640Sstevel@tonic-gate ctf_type_encoding(ctfp, type, &e) != CTF_ERR)
1650Sstevel@tonic-gate size = clp2(P2ROUNDUP(e.cte_bits, NBBY) / NBBY);
1660Sstevel@tonic-gate else
1670Sstevel@tonic-gate size = ctf_type_size(ctfp, type);
1680Sstevel@tonic-gate
1690Sstevel@tonic-gate if (size < 1 || size > 8 || (size & (size - 1)) != 0) {
1700Sstevel@tonic-gate xyerror(D_UNKNOWN, "internal error -- cg cannot load "
1710Sstevel@tonic-gate "size %ld when passed by value\n", (long)size);
1720Sstevel@tonic-gate }
1730Sstevel@tonic-gate
1740Sstevel@tonic-gate size--; /* convert size to 3-bit index */
1750Sstevel@tonic-gate
1760Sstevel@tonic-gate if (dnp->dn_flags & DT_NF_SIGNED)
1770Sstevel@tonic-gate size |= 0x08;
1780Sstevel@tonic-gate if (dnp->dn_flags & DT_NF_USERLAND)
1790Sstevel@tonic-gate size |= 0x10;
1800Sstevel@tonic-gate
1810Sstevel@tonic-gate return (ops[size]);
1820Sstevel@tonic-gate }
1830Sstevel@tonic-gate
1840Sstevel@tonic-gate static void
dt_cg_ptrsize(dt_node_t * dnp,dt_irlist_t * dlp,dt_regset_t * drp,uint_t op,int dreg)1850Sstevel@tonic-gate dt_cg_ptrsize(dt_node_t *dnp, dt_irlist_t *dlp, dt_regset_t *drp,
1860Sstevel@tonic-gate uint_t op, int dreg)
1870Sstevel@tonic-gate {
1880Sstevel@tonic-gate ctf_file_t *ctfp = dnp->dn_ctfp;
1890Sstevel@tonic-gate ctf_arinfo_t r;
1900Sstevel@tonic-gate dif_instr_t instr;
1910Sstevel@tonic-gate ctf_id_t type;
1920Sstevel@tonic-gate uint_t kind;
1930Sstevel@tonic-gate ssize_t size;
1940Sstevel@tonic-gate int sreg;
1950Sstevel@tonic-gate
1960Sstevel@tonic-gate if ((sreg = dt_regset_alloc(drp)) == -1)
1970Sstevel@tonic-gate longjmp(yypcb->pcb_jmpbuf, EDT_NOREG);
1980Sstevel@tonic-gate
1990Sstevel@tonic-gate type = ctf_type_resolve(ctfp, dnp->dn_type);
2000Sstevel@tonic-gate kind = ctf_type_kind(ctfp, type);
2010Sstevel@tonic-gate assert(kind == CTF_K_POINTER || kind == CTF_K_ARRAY);
2020Sstevel@tonic-gate
2030Sstevel@tonic-gate if (kind == CTF_K_ARRAY) {
2040Sstevel@tonic-gate if (ctf_array_info(ctfp, type, &r) != 0) {
2050Sstevel@tonic-gate yypcb->pcb_hdl->dt_ctferr = ctf_errno(ctfp);
2060Sstevel@tonic-gate longjmp(yypcb->pcb_jmpbuf, EDT_CTF);
2070Sstevel@tonic-gate }
2080Sstevel@tonic-gate type = r.ctr_contents;
2090Sstevel@tonic-gate } else
2100Sstevel@tonic-gate type = ctf_type_reference(ctfp, type);
2110Sstevel@tonic-gate
2120Sstevel@tonic-gate if ((size = ctf_type_size(ctfp, type)) == 1)
2130Sstevel@tonic-gate return; /* multiply or divide by one can be omitted */
2140Sstevel@tonic-gate
2150Sstevel@tonic-gate dt_cg_setx(dlp, sreg, size);
2160Sstevel@tonic-gate instr = DIF_INSTR_FMT(op, dreg, sreg, dreg);
2170Sstevel@tonic-gate dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
2180Sstevel@tonic-gate dt_regset_free(drp, sreg);
2190Sstevel@tonic-gate }
2200Sstevel@tonic-gate
2210Sstevel@tonic-gate /*
2220Sstevel@tonic-gate * If the result of a "." or "->" operation is a bit-field, we use this routine
2230Sstevel@tonic-gate * to generate an epilogue to the load instruction that extracts the value. In
2240Sstevel@tonic-gate * the diagrams below the "ld??" is the load instruction that is generated to
2250Sstevel@tonic-gate * load the containing word that is generating prior to calling this function.
2260Sstevel@tonic-gate *
2270Sstevel@tonic-gate * Epilogue for unsigned fields: Epilogue for signed fields:
2280Sstevel@tonic-gate *
2290Sstevel@tonic-gate * ldu? [r1], r1 lds? [r1], r1
2300Sstevel@tonic-gate * setx USHIFT, r2 setx 64 - SSHIFT, r2
2310Sstevel@tonic-gate * srl r1, r2, r1 sll r1, r2, r1
2320Sstevel@tonic-gate * setx (1 << bits) - 1, r2 setx 64 - bits, r2
2330Sstevel@tonic-gate * and r1, r2, r1 sra r1, r2, r1
2340Sstevel@tonic-gate *
2350Sstevel@tonic-gate * The *SHIFT constants above changes value depending on the endian-ness of our
2360Sstevel@tonic-gate * target architecture. Refer to the comments below for more details.
2370Sstevel@tonic-gate */
2380Sstevel@tonic-gate static void
dt_cg_field_get(dt_node_t * dnp,dt_irlist_t * dlp,dt_regset_t * drp,ctf_file_t * fp,const ctf_membinfo_t * mp)2390Sstevel@tonic-gate dt_cg_field_get(dt_node_t *dnp, dt_irlist_t *dlp, dt_regset_t *drp,
2400Sstevel@tonic-gate ctf_file_t *fp, const ctf_membinfo_t *mp)
2410Sstevel@tonic-gate {
2420Sstevel@tonic-gate ctf_encoding_t e;
2430Sstevel@tonic-gate dif_instr_t instr;
2440Sstevel@tonic-gate uint64_t shift;
2450Sstevel@tonic-gate int r1, r2;
2460Sstevel@tonic-gate
2470Sstevel@tonic-gate if (ctf_type_encoding(fp, mp->ctm_type, &e) != 0 || e.cte_bits > 64) {
2480Sstevel@tonic-gate xyerror(D_UNKNOWN, "cg: bad field: off %lu type <%ld> "
2490Sstevel@tonic-gate "bits %u\n", mp->ctm_offset, mp->ctm_type, e.cte_bits);
2500Sstevel@tonic-gate }
2510Sstevel@tonic-gate
2520Sstevel@tonic-gate assert(dnp->dn_op == DT_TOK_PTR || dnp->dn_op == DT_TOK_DOT);
2530Sstevel@tonic-gate r1 = dnp->dn_left->dn_reg;
2540Sstevel@tonic-gate
2550Sstevel@tonic-gate if ((r2 = dt_regset_alloc(drp)) == -1)
2560Sstevel@tonic-gate longjmp(yypcb->pcb_jmpbuf, EDT_NOREG);
2570Sstevel@tonic-gate
2580Sstevel@tonic-gate /*
2590Sstevel@tonic-gate * On little-endian architectures, ctm_offset counts from the right so
2600Sstevel@tonic-gate * ctm_offset % NBBY itself is the amount we want to shift right to
2610Sstevel@tonic-gate * move the value bits to the little end of the register to mask them.
2620Sstevel@tonic-gate * On big-endian architectures, ctm_offset counts from the left so we
2630Sstevel@tonic-gate * must subtract (ctm_offset % NBBY + cte_bits) from the size in bits
2640Sstevel@tonic-gate * we used for the load. The size of our load in turn is found by
2650Sstevel@tonic-gate * rounding cte_bits up to a byte boundary and then finding the
2660Sstevel@tonic-gate * nearest power of two to this value (see clp2(), above). These
2670Sstevel@tonic-gate * properties are used to compute shift as USHIFT or SSHIFT, below.
2680Sstevel@tonic-gate */
2690Sstevel@tonic-gate if (dnp->dn_flags & DT_NF_SIGNED) {
2700Sstevel@tonic-gate #ifdef _BIG_ENDIAN
2710Sstevel@tonic-gate shift = clp2(P2ROUNDUP(e.cte_bits, NBBY) / NBBY) * NBBY -
2720Sstevel@tonic-gate mp->ctm_offset % NBBY;
2730Sstevel@tonic-gate #else
2740Sstevel@tonic-gate shift = mp->ctm_offset % NBBY + e.cte_bits;
2750Sstevel@tonic-gate #endif
2760Sstevel@tonic-gate dt_cg_setx(dlp, r2, 64 - shift);
2770Sstevel@tonic-gate instr = DIF_INSTR_FMT(DIF_OP_SLL, r1, r2, r1);
2780Sstevel@tonic-gate dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
2790Sstevel@tonic-gate
2800Sstevel@tonic-gate dt_cg_setx(dlp, r2, 64 - e.cte_bits);
2810Sstevel@tonic-gate instr = DIF_INSTR_FMT(DIF_OP_SRA, r1, r2, r1);
2820Sstevel@tonic-gate dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
2830Sstevel@tonic-gate } else {
2840Sstevel@tonic-gate #ifdef _BIG_ENDIAN
2850Sstevel@tonic-gate shift = clp2(P2ROUNDUP(e.cte_bits, NBBY) / NBBY) * NBBY -
2860Sstevel@tonic-gate (mp->ctm_offset % NBBY + e.cte_bits);
2870Sstevel@tonic-gate #else
2880Sstevel@tonic-gate shift = mp->ctm_offset % NBBY;
2890Sstevel@tonic-gate #endif
2900Sstevel@tonic-gate dt_cg_setx(dlp, r2, shift);
2910Sstevel@tonic-gate instr = DIF_INSTR_FMT(DIF_OP_SRL, r1, r2, r1);
2920Sstevel@tonic-gate dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
2930Sstevel@tonic-gate
2940Sstevel@tonic-gate dt_cg_setx(dlp, r2, (1ULL << e.cte_bits) - 1);
2950Sstevel@tonic-gate instr = DIF_INSTR_FMT(DIF_OP_AND, r1, r2, r1);
2960Sstevel@tonic-gate dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
2970Sstevel@tonic-gate }
2980Sstevel@tonic-gate
2990Sstevel@tonic-gate dt_regset_free(drp, r2);
3000Sstevel@tonic-gate }
3010Sstevel@tonic-gate
3020Sstevel@tonic-gate /*
3030Sstevel@tonic-gate * If the destination of a store operation is a bit-field, we use this routine
3040Sstevel@tonic-gate * to generate a prologue to the store instruction that loads the surrounding
3050Sstevel@tonic-gate * bits, clears the destination field, and ORs in the new value of the field.
3060Sstevel@tonic-gate * In the diagram below the "st?" is the store instruction that is generated to
3070Sstevel@tonic-gate * store the containing word that is generating after calling this function.
3080Sstevel@tonic-gate *
3090Sstevel@tonic-gate * ld [dst->dn_reg], r1
3100Sstevel@tonic-gate * setx ~(((1 << cte_bits) - 1) << (ctm_offset % NBBY)), r2
3110Sstevel@tonic-gate * and r1, r2, r1
3120Sstevel@tonic-gate *
3130Sstevel@tonic-gate * setx (1 << cte_bits) - 1, r2
3140Sstevel@tonic-gate * and src->dn_reg, r2, r2
3150Sstevel@tonic-gate * setx ctm_offset % NBBY, r3
3160Sstevel@tonic-gate * sll r2, r3, r2
3170Sstevel@tonic-gate *
3180Sstevel@tonic-gate * or r1, r2, r1
3190Sstevel@tonic-gate * st? r1, [dst->dn_reg]
3200Sstevel@tonic-gate *
3210Sstevel@tonic-gate * This routine allocates a new register to hold the value to be stored and
3220Sstevel@tonic-gate * returns it. The caller is responsible for freeing this register later.
3230Sstevel@tonic-gate */
3240Sstevel@tonic-gate static int
dt_cg_field_set(dt_node_t * src,dt_irlist_t * dlp,dt_regset_t * drp,dt_node_t * dst)3250Sstevel@tonic-gate dt_cg_field_set(dt_node_t *src, dt_irlist_t *dlp,
3260Sstevel@tonic-gate dt_regset_t *drp, dt_node_t *dst)
3270Sstevel@tonic-gate {
3280Sstevel@tonic-gate uint64_t cmask, fmask, shift;
3290Sstevel@tonic-gate dif_instr_t instr;
3300Sstevel@tonic-gate int r1, r2, r3;
3310Sstevel@tonic-gate
3320Sstevel@tonic-gate ctf_membinfo_t m;
3330Sstevel@tonic-gate ctf_encoding_t e;
3340Sstevel@tonic-gate ctf_file_t *fp, *ofp;
3350Sstevel@tonic-gate ctf_id_t type;
3360Sstevel@tonic-gate
3370Sstevel@tonic-gate assert(dst->dn_op == DT_TOK_PTR || dst->dn_op == DT_TOK_DOT);
3380Sstevel@tonic-gate assert(dst->dn_right->dn_kind == DT_NODE_IDENT);
3390Sstevel@tonic-gate
3400Sstevel@tonic-gate fp = dst->dn_left->dn_ctfp;
3410Sstevel@tonic-gate type = ctf_type_resolve(fp, dst->dn_left->dn_type);
3420Sstevel@tonic-gate
3430Sstevel@tonic-gate if (dst->dn_op == DT_TOK_PTR) {
3440Sstevel@tonic-gate type = ctf_type_reference(fp, type);
3450Sstevel@tonic-gate type = ctf_type_resolve(fp, type);
3460Sstevel@tonic-gate }
3470Sstevel@tonic-gate
3480Sstevel@tonic-gate if ((fp = dt_cg_membinfo(ofp = fp, type,
3490Sstevel@tonic-gate dst->dn_right->dn_string, &m)) == NULL) {
3500Sstevel@tonic-gate yypcb->pcb_hdl->dt_ctferr = ctf_errno(ofp);
3510Sstevel@tonic-gate longjmp(yypcb->pcb_jmpbuf, EDT_CTF);
3520Sstevel@tonic-gate }
3530Sstevel@tonic-gate
3540Sstevel@tonic-gate if (ctf_type_encoding(fp, m.ctm_type, &e) != 0 || e.cte_bits > 64) {
3550Sstevel@tonic-gate xyerror(D_UNKNOWN, "cg: bad field: off %lu type <%ld> "
3560Sstevel@tonic-gate "bits %u\n", m.ctm_offset, m.ctm_type, e.cte_bits);
3570Sstevel@tonic-gate }
3580Sstevel@tonic-gate
3590Sstevel@tonic-gate if ((r1 = dt_regset_alloc(drp)) == -1 ||
3600Sstevel@tonic-gate (r2 = dt_regset_alloc(drp)) == -1 ||
3610Sstevel@tonic-gate (r3 = dt_regset_alloc(drp)) == -1)
3620Sstevel@tonic-gate longjmp(yypcb->pcb_jmpbuf, EDT_NOREG);
3630Sstevel@tonic-gate
3640Sstevel@tonic-gate /*
3650Sstevel@tonic-gate * Compute shifts and masks. We need to compute "shift" as the amount
3660Sstevel@tonic-gate * we need to shift left to position our field in the containing word.
3670Sstevel@tonic-gate * Refer to the comments in dt_cg_field_get(), above, for more info.
3680Sstevel@tonic-gate * We then compute fmask as the mask that truncates the value in the
3690Sstevel@tonic-gate * input register to width cte_bits, and cmask as the mask used to
3700Sstevel@tonic-gate * pass through the containing bits and zero the field bits.
3710Sstevel@tonic-gate */
3720Sstevel@tonic-gate #ifdef _BIG_ENDIAN
3730Sstevel@tonic-gate shift = clp2(P2ROUNDUP(e.cte_bits, NBBY) / NBBY) * NBBY -
3740Sstevel@tonic-gate (m.ctm_offset % NBBY + e.cte_bits);
3750Sstevel@tonic-gate #else
3760Sstevel@tonic-gate shift = m.ctm_offset % NBBY;
3770Sstevel@tonic-gate #endif
3780Sstevel@tonic-gate fmask = (1ULL << e.cte_bits) - 1;
3790Sstevel@tonic-gate cmask = ~(fmask << shift);
3800Sstevel@tonic-gate
3810Sstevel@tonic-gate instr = DIF_INSTR_LOAD(
3820Sstevel@tonic-gate dt_cg_load(dst, fp, m.ctm_type), dst->dn_reg, r1);
3830Sstevel@tonic-gate dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
3840Sstevel@tonic-gate
3850Sstevel@tonic-gate dt_cg_setx(dlp, r2, cmask);
3860Sstevel@tonic-gate instr = DIF_INSTR_FMT(DIF_OP_AND, r1, r2, r1);
3870Sstevel@tonic-gate dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
3880Sstevel@tonic-gate
3890Sstevel@tonic-gate dt_cg_setx(dlp, r2, fmask);
3900Sstevel@tonic-gate instr = DIF_INSTR_FMT(DIF_OP_AND, src->dn_reg, r2, r2);
3910Sstevel@tonic-gate dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
3920Sstevel@tonic-gate
3930Sstevel@tonic-gate dt_cg_setx(dlp, r3, shift);
3940Sstevel@tonic-gate instr = DIF_INSTR_FMT(DIF_OP_SLL, r2, r3, r2);
3950Sstevel@tonic-gate dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
3960Sstevel@tonic-gate
3970Sstevel@tonic-gate instr = DIF_INSTR_FMT(DIF_OP_OR, r1, r2, r1);
3980Sstevel@tonic-gate dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
3990Sstevel@tonic-gate
4000Sstevel@tonic-gate dt_regset_free(drp, r3);
4010Sstevel@tonic-gate dt_regset_free(drp, r2);
4020Sstevel@tonic-gate
4030Sstevel@tonic-gate return (r1);
4040Sstevel@tonic-gate }
4050Sstevel@tonic-gate
4060Sstevel@tonic-gate static void
dt_cg_store(dt_node_t * src,dt_irlist_t * dlp,dt_regset_t * drp,dt_node_t * dst)4070Sstevel@tonic-gate dt_cg_store(dt_node_t *src, dt_irlist_t *dlp, dt_regset_t *drp, dt_node_t *dst)
4080Sstevel@tonic-gate {
4090Sstevel@tonic-gate ctf_encoding_t e;
4100Sstevel@tonic-gate dif_instr_t instr;
4110Sstevel@tonic-gate size_t size;
4120Sstevel@tonic-gate int reg;
4130Sstevel@tonic-gate
4140Sstevel@tonic-gate /*
4150Sstevel@tonic-gate * If we're loading a bit-field, the size of our store is found by
4160Sstevel@tonic-gate * rounding dst's cte_bits up to a byte boundary and then finding the
4170Sstevel@tonic-gate * nearest power of two to this value (see clp2(), above).
4180Sstevel@tonic-gate */
4190Sstevel@tonic-gate if ((dst->dn_flags & DT_NF_BITFIELD) &&
4200Sstevel@tonic-gate ctf_type_encoding(dst->dn_ctfp, dst->dn_type, &e) != CTF_ERR)
4210Sstevel@tonic-gate size = clp2(P2ROUNDUP(e.cte_bits, NBBY) / NBBY);
4220Sstevel@tonic-gate else
4230Sstevel@tonic-gate size = dt_node_type_size(src);
4240Sstevel@tonic-gate
4250Sstevel@tonic-gate if (src->dn_flags & DT_NF_REF) {
4260Sstevel@tonic-gate if ((reg = dt_regset_alloc(drp)) == -1)
4270Sstevel@tonic-gate longjmp(yypcb->pcb_jmpbuf, EDT_NOREG);
4280Sstevel@tonic-gate dt_cg_setx(dlp, reg, size);
4290Sstevel@tonic-gate instr = DIF_INSTR_COPYS(src->dn_reg, reg, dst->dn_reg);
4300Sstevel@tonic-gate dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
4310Sstevel@tonic-gate dt_regset_free(drp, reg);
4320Sstevel@tonic-gate } else {
4330Sstevel@tonic-gate if (dst->dn_flags & DT_NF_BITFIELD)
4340Sstevel@tonic-gate reg = dt_cg_field_set(src, dlp, drp, dst);
4350Sstevel@tonic-gate else
4360Sstevel@tonic-gate reg = src->dn_reg;
4370Sstevel@tonic-gate
4380Sstevel@tonic-gate switch (size) {
4390Sstevel@tonic-gate case 1:
4400Sstevel@tonic-gate instr = DIF_INSTR_STORE(DIF_OP_STB, reg, dst->dn_reg);
4410Sstevel@tonic-gate break;
4420Sstevel@tonic-gate case 2:
4430Sstevel@tonic-gate instr = DIF_INSTR_STORE(DIF_OP_STH, reg, dst->dn_reg);
4440Sstevel@tonic-gate break;
4450Sstevel@tonic-gate case 4:
4460Sstevel@tonic-gate instr = DIF_INSTR_STORE(DIF_OP_STW, reg, dst->dn_reg);
4470Sstevel@tonic-gate break;
4480Sstevel@tonic-gate case 8:
4490Sstevel@tonic-gate instr = DIF_INSTR_STORE(DIF_OP_STX, reg, dst->dn_reg);
4500Sstevel@tonic-gate break;
4510Sstevel@tonic-gate default:
4520Sstevel@tonic-gate xyerror(D_UNKNOWN, "internal error -- cg cannot store "
4530Sstevel@tonic-gate "size %lu when passed by value\n", (ulong_t)size);
4540Sstevel@tonic-gate }
4550Sstevel@tonic-gate dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
4560Sstevel@tonic-gate
4570Sstevel@tonic-gate if (dst->dn_flags & DT_NF_BITFIELD)
4580Sstevel@tonic-gate dt_regset_free(drp, reg);
4590Sstevel@tonic-gate }
4600Sstevel@tonic-gate }
4610Sstevel@tonic-gate
4620Sstevel@tonic-gate /*
4630Sstevel@tonic-gate * Generate code for a typecast or for argument promotion from the type of the
4640Sstevel@tonic-gate * actual to the type of the formal. We need to generate code for casts when
4650Sstevel@tonic-gate * a scalar type is being narrowed or changing signed-ness. We first shift the
4660Sstevel@tonic-gate * desired bits high (losing excess bits if narrowing) and then shift them down
4670Sstevel@tonic-gate * using logical shift (unsigned result) or arithmetic shift (signed result).
4680Sstevel@tonic-gate */
4690Sstevel@tonic-gate static void
dt_cg_typecast(const dt_node_t * src,const dt_node_t * dst,dt_irlist_t * dlp,dt_regset_t * drp)4700Sstevel@tonic-gate dt_cg_typecast(const dt_node_t *src, const dt_node_t *dst,
4710Sstevel@tonic-gate dt_irlist_t *dlp, dt_regset_t *drp)
4720Sstevel@tonic-gate {
4730Sstevel@tonic-gate size_t srcsize = dt_node_type_size(src);
4740Sstevel@tonic-gate size_t dstsize = dt_node_type_size(dst);
4750Sstevel@tonic-gate
4760Sstevel@tonic-gate dif_instr_t instr;
4770Sstevel@tonic-gate int reg, n;
4780Sstevel@tonic-gate
4790Sstevel@tonic-gate if (dt_node_is_scalar(dst) && (dstsize < srcsize ||
4800Sstevel@tonic-gate (src->dn_flags & DT_NF_SIGNED) ^ (dst->dn_flags & DT_NF_SIGNED))) {
4810Sstevel@tonic-gate if ((reg = dt_regset_alloc(drp)) == -1)
4820Sstevel@tonic-gate longjmp(yypcb->pcb_jmpbuf, EDT_NOREG);
4830Sstevel@tonic-gate
4840Sstevel@tonic-gate if (dstsize < srcsize)
4850Sstevel@tonic-gate n = sizeof (uint64_t) * NBBY - dstsize * NBBY;
4860Sstevel@tonic-gate else
4870Sstevel@tonic-gate n = sizeof (uint64_t) * NBBY - srcsize * NBBY;
4880Sstevel@tonic-gate
4890Sstevel@tonic-gate dt_cg_setx(dlp, reg, n);
4900Sstevel@tonic-gate
4910Sstevel@tonic-gate instr = DIF_INSTR_FMT(DIF_OP_SLL,
4920Sstevel@tonic-gate src->dn_reg, reg, dst->dn_reg);
4930Sstevel@tonic-gate dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
4940Sstevel@tonic-gate
4950Sstevel@tonic-gate instr = DIF_INSTR_FMT((dst->dn_flags & DT_NF_SIGNED) ?
4960Sstevel@tonic-gate DIF_OP_SRA : DIF_OP_SRL, dst->dn_reg, reg, dst->dn_reg);
4970Sstevel@tonic-gate
4980Sstevel@tonic-gate dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
4990Sstevel@tonic-gate dt_regset_free(drp, reg);
5000Sstevel@tonic-gate }
5010Sstevel@tonic-gate }
5020Sstevel@tonic-gate
5030Sstevel@tonic-gate /*
5040Sstevel@tonic-gate * Generate code to push the specified argument list on to the tuple stack.
5050Sstevel@tonic-gate * We use this routine for handling subroutine calls and associative arrays.
5060Sstevel@tonic-gate * We must first generate code for all subexpressions before loading the stack
5070Sstevel@tonic-gate * because any subexpression could itself require the use of the tuple stack.
5080Sstevel@tonic-gate * This holds a number of registers equal to the number of arguments, but this
5090Sstevel@tonic-gate * is not a huge problem because the number of arguments can't exceed the
5100Sstevel@tonic-gate * number of tuple register stack elements anyway. At most one extra register
5110Sstevel@tonic-gate * is required (either by dt_cg_typecast() or for dtdt_size, below). This
5120Sstevel@tonic-gate * implies that a DIF implementation should offer a number of general purpose
5130Sstevel@tonic-gate * registers at least one greater than the number of tuple registers.
5140Sstevel@tonic-gate */
5150Sstevel@tonic-gate static void
dt_cg_arglist(dt_ident_t * idp,dt_node_t * args,dt_irlist_t * dlp,dt_regset_t * drp)5160Sstevel@tonic-gate dt_cg_arglist(dt_ident_t *idp, dt_node_t *args,
5170Sstevel@tonic-gate dt_irlist_t *dlp, dt_regset_t *drp)
5180Sstevel@tonic-gate {
5190Sstevel@tonic-gate const dt_idsig_t *isp = idp->di_data;
5200Sstevel@tonic-gate dt_node_t *dnp;
5210Sstevel@tonic-gate int i = 0;
5220Sstevel@tonic-gate
5230Sstevel@tonic-gate for (dnp = args; dnp != NULL; dnp = dnp->dn_list)
5240Sstevel@tonic-gate dt_cg_node(dnp, dlp, drp);
5250Sstevel@tonic-gate
5260Sstevel@tonic-gate dt_irlist_append(dlp,
5270Sstevel@tonic-gate dt_cg_node_alloc(DT_LBL_NONE, DIF_INSTR_FLUSHTS));
5280Sstevel@tonic-gate
5290Sstevel@tonic-gate for (dnp = args; dnp != NULL; dnp = dnp->dn_list, i++) {
5300Sstevel@tonic-gate dtrace_diftype_t t;
5310Sstevel@tonic-gate dif_instr_t instr;
5320Sstevel@tonic-gate uint_t op;
5330Sstevel@tonic-gate int reg;
5340Sstevel@tonic-gate
535*265Smws dt_node_diftype(yypcb->pcb_hdl, dnp, &t);
5360Sstevel@tonic-gate
5370Sstevel@tonic-gate isp->dis_args[i].dn_reg = dnp->dn_reg; /* re-use register */
5380Sstevel@tonic-gate dt_cg_typecast(dnp, &isp->dis_args[i], dlp, drp);
5390Sstevel@tonic-gate isp->dis_args[i].dn_reg = -1;
5400Sstevel@tonic-gate
5410Sstevel@tonic-gate if (t.dtdt_flags & DIF_TF_BYREF)
5420Sstevel@tonic-gate op = DIF_OP_PUSHTR;
5430Sstevel@tonic-gate else
5440Sstevel@tonic-gate op = DIF_OP_PUSHTV;
5450Sstevel@tonic-gate
5460Sstevel@tonic-gate if (t.dtdt_size != 0) {
5470Sstevel@tonic-gate if ((reg = dt_regset_alloc(drp)) == -1)
5480Sstevel@tonic-gate longjmp(yypcb->pcb_jmpbuf, EDT_NOREG);
5490Sstevel@tonic-gate dt_cg_setx(dlp, reg, t.dtdt_size);
5500Sstevel@tonic-gate } else
5510Sstevel@tonic-gate reg = DIF_REG_R0;
5520Sstevel@tonic-gate
5530Sstevel@tonic-gate instr = DIF_INSTR_PUSHTS(op, t.dtdt_kind, reg, dnp->dn_reg);
5540Sstevel@tonic-gate dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
5550Sstevel@tonic-gate dt_regset_free(drp, dnp->dn_reg);
5560Sstevel@tonic-gate
5570Sstevel@tonic-gate if (reg != DIF_REG_R0)
5580Sstevel@tonic-gate dt_regset_free(drp, reg);
5590Sstevel@tonic-gate }
5600Sstevel@tonic-gate
5610Sstevel@tonic-gate if (i > yypcb->pcb_hdl->dt_conf.dtc_diftupregs)
5620Sstevel@tonic-gate longjmp(yypcb->pcb_jmpbuf, EDT_NOTUPREG);
5630Sstevel@tonic-gate }
5640Sstevel@tonic-gate
5650Sstevel@tonic-gate static void
dt_cg_arithmetic_op(dt_node_t * dnp,dt_irlist_t * dlp,dt_regset_t * drp,uint_t op)5660Sstevel@tonic-gate dt_cg_arithmetic_op(dt_node_t *dnp, dt_irlist_t *dlp,
5670Sstevel@tonic-gate dt_regset_t *drp, uint_t op)
5680Sstevel@tonic-gate {
5690Sstevel@tonic-gate int is_ptr_op = (dnp->dn_op == DT_TOK_ADD || dnp->dn_op == DT_TOK_SUB ||
5700Sstevel@tonic-gate dnp->dn_op == DT_TOK_ADD_EQ || dnp->dn_op == DT_TOK_SUB_EQ);
5710Sstevel@tonic-gate
5720Sstevel@tonic-gate int lp_is_ptr = dt_node_is_pointer(dnp->dn_left);
5730Sstevel@tonic-gate int rp_is_ptr = dt_node_is_pointer(dnp->dn_right);
5740Sstevel@tonic-gate
5750Sstevel@tonic-gate dif_instr_t instr;
5760Sstevel@tonic-gate
5770Sstevel@tonic-gate if (lp_is_ptr && rp_is_ptr) {
5780Sstevel@tonic-gate assert(dnp->dn_op == DT_TOK_SUB);
5790Sstevel@tonic-gate is_ptr_op = 0;
5800Sstevel@tonic-gate }
5810Sstevel@tonic-gate
5820Sstevel@tonic-gate dt_cg_node(dnp->dn_left, dlp, drp);
5830Sstevel@tonic-gate if (is_ptr_op && rp_is_ptr)
5840Sstevel@tonic-gate dt_cg_ptrsize(dnp, dlp, drp, DIF_OP_MUL, dnp->dn_left->dn_reg);
5850Sstevel@tonic-gate
5860Sstevel@tonic-gate dt_cg_node(dnp->dn_right, dlp, drp);
5870Sstevel@tonic-gate if (is_ptr_op && lp_is_ptr)
5880Sstevel@tonic-gate dt_cg_ptrsize(dnp, dlp, drp, DIF_OP_MUL, dnp->dn_right->dn_reg);
5890Sstevel@tonic-gate
5900Sstevel@tonic-gate instr = DIF_INSTR_FMT(op, dnp->dn_left->dn_reg,
5910Sstevel@tonic-gate dnp->dn_right->dn_reg, dnp->dn_left->dn_reg);
5920Sstevel@tonic-gate
5930Sstevel@tonic-gate dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
5940Sstevel@tonic-gate dt_regset_free(drp, dnp->dn_right->dn_reg);
5950Sstevel@tonic-gate dnp->dn_reg = dnp->dn_left->dn_reg;
5960Sstevel@tonic-gate
5970Sstevel@tonic-gate if (lp_is_ptr && rp_is_ptr)
5980Sstevel@tonic-gate dt_cg_ptrsize(dnp->dn_right,
5990Sstevel@tonic-gate dlp, drp, DIF_OP_UDIV, dnp->dn_reg);
6000Sstevel@tonic-gate }
6010Sstevel@tonic-gate
6020Sstevel@tonic-gate static uint_t
dt_cg_stvar(const dt_ident_t * idp)6030Sstevel@tonic-gate dt_cg_stvar(const dt_ident_t *idp)
6040Sstevel@tonic-gate {
6050Sstevel@tonic-gate static const uint_t aops[] = { DIF_OP_STGAA, DIF_OP_STTAA, DIF_OP_NOP };
6060Sstevel@tonic-gate static const uint_t sops[] = { DIF_OP_STGS, DIF_OP_STTS, DIF_OP_STLS };
6070Sstevel@tonic-gate
6080Sstevel@tonic-gate uint_t i = (((idp->di_flags & DT_IDFLG_LOCAL) != 0) << 1) |
6090Sstevel@tonic-gate ((idp->di_flags & DT_IDFLG_TLS) != 0);
6100Sstevel@tonic-gate
6110Sstevel@tonic-gate return (idp->di_kind == DT_IDENT_ARRAY ? aops[i] : sops[i]);
6120Sstevel@tonic-gate }
6130Sstevel@tonic-gate
6140Sstevel@tonic-gate static void
dt_cg_prearith_op(dt_node_t * dnp,dt_irlist_t * dlp,dt_regset_t * drp,uint_t op)6150Sstevel@tonic-gate dt_cg_prearith_op(dt_node_t *dnp, dt_irlist_t *dlp, dt_regset_t *drp, uint_t op)
6160Sstevel@tonic-gate {
6170Sstevel@tonic-gate ctf_file_t *ctfp = dnp->dn_ctfp;
6180Sstevel@tonic-gate dif_instr_t instr;
6190Sstevel@tonic-gate ctf_id_t type;
6200Sstevel@tonic-gate ssize_t size = 1;
6210Sstevel@tonic-gate int reg;
6220Sstevel@tonic-gate
6230Sstevel@tonic-gate if (dt_node_is_pointer(dnp)) {
6240Sstevel@tonic-gate type = ctf_type_resolve(ctfp, dnp->dn_type);
6250Sstevel@tonic-gate assert(ctf_type_kind(ctfp, type) == CTF_K_POINTER);
6260Sstevel@tonic-gate size = ctf_type_size(ctfp, ctf_type_reference(ctfp, type));
6270Sstevel@tonic-gate }
6280Sstevel@tonic-gate
6290Sstevel@tonic-gate dt_cg_node(dnp->dn_child, dlp, drp);
6300Sstevel@tonic-gate dnp->dn_reg = dnp->dn_child->dn_reg;
6310Sstevel@tonic-gate
6320Sstevel@tonic-gate if ((reg = dt_regset_alloc(drp)) == -1)
6330Sstevel@tonic-gate longjmp(yypcb->pcb_jmpbuf, EDT_NOREG);
6340Sstevel@tonic-gate
6350Sstevel@tonic-gate dt_cg_setx(dlp, reg, size);
6360Sstevel@tonic-gate
6370Sstevel@tonic-gate instr = DIF_INSTR_FMT(op, dnp->dn_reg, reg, dnp->dn_reg);
6380Sstevel@tonic-gate dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
6390Sstevel@tonic-gate dt_regset_free(drp, reg);
6400Sstevel@tonic-gate
6410Sstevel@tonic-gate /*
6420Sstevel@tonic-gate * If we are modifying a variable, generate an stv instruction from
6430Sstevel@tonic-gate * the variable specified by the identifier. If we are storing to a
6440Sstevel@tonic-gate * memory address, generate code again for the left-hand side using
6450Sstevel@tonic-gate * DT_NF_REF to get the address, and then generate a store to it.
6460Sstevel@tonic-gate * In both paths, we store the value in dnp->dn_reg (the new value).
6470Sstevel@tonic-gate */
6480Sstevel@tonic-gate if (dnp->dn_child->dn_kind == DT_NODE_VAR) {
6490Sstevel@tonic-gate dt_ident_t *idp = dt_ident_resolve(dnp->dn_child->dn_ident);
6500Sstevel@tonic-gate
6510Sstevel@tonic-gate idp->di_flags |= DT_IDFLG_DIFW;
6520Sstevel@tonic-gate instr = DIF_INSTR_STV(dt_cg_stvar(idp),
6530Sstevel@tonic-gate idp->di_id, dnp->dn_reg);
6540Sstevel@tonic-gate dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
6550Sstevel@tonic-gate } else {
6560Sstevel@tonic-gate uint_t rbit = dnp->dn_child->dn_flags & DT_NF_REF;
6570Sstevel@tonic-gate
6580Sstevel@tonic-gate assert(dnp->dn_child->dn_flags & DT_NF_WRITABLE);
6590Sstevel@tonic-gate assert(dnp->dn_child->dn_flags & DT_NF_LVALUE);
6600Sstevel@tonic-gate
6610Sstevel@tonic-gate dnp->dn_child->dn_flags |= DT_NF_REF; /* force pass-by-ref */
6620Sstevel@tonic-gate dt_cg_node(dnp->dn_child, dlp, drp);
6630Sstevel@tonic-gate
6640Sstevel@tonic-gate dt_cg_store(dnp, dlp, drp, dnp->dn_child);
6650Sstevel@tonic-gate dt_regset_free(drp, dnp->dn_child->dn_reg);
6660Sstevel@tonic-gate
6670Sstevel@tonic-gate dnp->dn_left->dn_flags &= ~DT_NF_REF;
6680Sstevel@tonic-gate dnp->dn_left->dn_flags |= rbit;
6690Sstevel@tonic-gate }
6700Sstevel@tonic-gate }
6710Sstevel@tonic-gate
6720Sstevel@tonic-gate static void
dt_cg_postarith_op(dt_node_t * dnp,dt_irlist_t * dlp,dt_regset_t * drp,uint_t op)6730Sstevel@tonic-gate dt_cg_postarith_op(dt_node_t *dnp, dt_irlist_t *dlp,
6740Sstevel@tonic-gate dt_regset_t *drp, uint_t op)
6750Sstevel@tonic-gate {
6760Sstevel@tonic-gate ctf_file_t *ctfp = dnp->dn_ctfp;
6770Sstevel@tonic-gate dif_instr_t instr;
6780Sstevel@tonic-gate ctf_id_t type;
6790Sstevel@tonic-gate ssize_t size = 1;
6800Sstevel@tonic-gate int nreg;
6810Sstevel@tonic-gate
6820Sstevel@tonic-gate if (dt_node_is_pointer(dnp)) {
6830Sstevel@tonic-gate type = ctf_type_resolve(ctfp, dnp->dn_type);
6840Sstevel@tonic-gate assert(ctf_type_kind(ctfp, type) == CTF_K_POINTER);
6850Sstevel@tonic-gate size = ctf_type_size(ctfp, ctf_type_reference(ctfp, type));
6860Sstevel@tonic-gate }
6870Sstevel@tonic-gate
6880Sstevel@tonic-gate dt_cg_node(dnp->dn_child, dlp, drp);
6890Sstevel@tonic-gate dnp->dn_reg = dnp->dn_child->dn_reg;
6900Sstevel@tonic-gate
6910Sstevel@tonic-gate if ((nreg = dt_regset_alloc(drp)) == -1)
6920Sstevel@tonic-gate longjmp(yypcb->pcb_jmpbuf, EDT_NOREG);
6930Sstevel@tonic-gate
6940Sstevel@tonic-gate dt_cg_setx(dlp, nreg, size);
6950Sstevel@tonic-gate instr = DIF_INSTR_FMT(op, dnp->dn_reg, nreg, nreg);
6960Sstevel@tonic-gate dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
6970Sstevel@tonic-gate
6980Sstevel@tonic-gate /*
6990Sstevel@tonic-gate * If we are modifying a variable, generate an stv instruction from
7000Sstevel@tonic-gate * the variable specified by the identifier. If we are storing to a
7010Sstevel@tonic-gate * memory address, generate code again for the left-hand side using
7020Sstevel@tonic-gate * DT_NF_REF to get the address, and then generate a store to it.
7030Sstevel@tonic-gate * In both paths, we store the value from 'nreg' (the new value).
7040Sstevel@tonic-gate */
7050Sstevel@tonic-gate if (dnp->dn_child->dn_kind == DT_NODE_VAR) {
7060Sstevel@tonic-gate dt_ident_t *idp = dt_ident_resolve(dnp->dn_child->dn_ident);
7070Sstevel@tonic-gate
7080Sstevel@tonic-gate idp->di_flags |= DT_IDFLG_DIFW;
7090Sstevel@tonic-gate instr = DIF_INSTR_STV(dt_cg_stvar(idp), idp->di_id, nreg);
7100Sstevel@tonic-gate dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
7110Sstevel@tonic-gate } else {
7120Sstevel@tonic-gate uint_t rbit = dnp->dn_child->dn_flags & DT_NF_REF;
7130Sstevel@tonic-gate int oreg = dnp->dn_reg;
7140Sstevel@tonic-gate
7150Sstevel@tonic-gate assert(dnp->dn_child->dn_flags & DT_NF_WRITABLE);
7160Sstevel@tonic-gate assert(dnp->dn_child->dn_flags & DT_NF_LVALUE);
7170Sstevel@tonic-gate
7180Sstevel@tonic-gate dnp->dn_child->dn_flags |= DT_NF_REF; /* force pass-by-ref */
7190Sstevel@tonic-gate dt_cg_node(dnp->dn_child, dlp, drp);
7200Sstevel@tonic-gate
7210Sstevel@tonic-gate dnp->dn_reg = nreg;
7220Sstevel@tonic-gate dt_cg_store(dnp, dlp, drp, dnp->dn_child);
7230Sstevel@tonic-gate dnp->dn_reg = oreg;
7240Sstevel@tonic-gate
7250Sstevel@tonic-gate dt_regset_free(drp, dnp->dn_child->dn_reg);
7260Sstevel@tonic-gate dnp->dn_left->dn_flags &= ~DT_NF_REF;
7270Sstevel@tonic-gate dnp->dn_left->dn_flags |= rbit;
7280Sstevel@tonic-gate }
7290Sstevel@tonic-gate
7300Sstevel@tonic-gate dt_regset_free(drp, nreg);
7310Sstevel@tonic-gate }
7320Sstevel@tonic-gate
7330Sstevel@tonic-gate /*
7340Sstevel@tonic-gate * Determine if we should perform signed or unsigned comparison for an OP2.
7350Sstevel@tonic-gate * If both operands are of arithmetic type, perform the usual arithmetic
7360Sstevel@tonic-gate * conversions to determine the common real type for comparison [ISOC 6.5.8.3].
7370Sstevel@tonic-gate */
7380Sstevel@tonic-gate static int
dt_cg_compare_signed(dt_node_t * dnp)7390Sstevel@tonic-gate dt_cg_compare_signed(dt_node_t *dnp)
7400Sstevel@tonic-gate {
7410Sstevel@tonic-gate dt_node_t dn;
7420Sstevel@tonic-gate
7430Sstevel@tonic-gate if (dt_node_is_string(dnp->dn_left) ||
7440Sstevel@tonic-gate dt_node_is_string(dnp->dn_right))
7450Sstevel@tonic-gate return (1); /* strings always compare signed */
7460Sstevel@tonic-gate else if (!dt_node_is_arith(dnp->dn_left) ||
7470Sstevel@tonic-gate !dt_node_is_arith(dnp->dn_right))
7480Sstevel@tonic-gate return (0); /* non-arithmetic types always compare unsigned */
7490Sstevel@tonic-gate
7500Sstevel@tonic-gate bzero(&dn, sizeof (dn));
7510Sstevel@tonic-gate dt_node_promote(dnp->dn_left, dnp->dn_right, &dn);
7520Sstevel@tonic-gate return (dn.dn_flags & DT_NF_SIGNED);
7530Sstevel@tonic-gate }
7540Sstevel@tonic-gate
7550Sstevel@tonic-gate static void
dt_cg_compare_op(dt_node_t * dnp,dt_irlist_t * dlp,dt_regset_t * drp,uint_t op)7560Sstevel@tonic-gate dt_cg_compare_op(dt_node_t *dnp, dt_irlist_t *dlp, dt_regset_t *drp, uint_t op)
7570Sstevel@tonic-gate {
7580Sstevel@tonic-gate uint_t lbl_true = dt_irlist_label(dlp);
7590Sstevel@tonic-gate uint_t lbl_post = dt_irlist_label(dlp);
7600Sstevel@tonic-gate
7610Sstevel@tonic-gate dif_instr_t instr;
7620Sstevel@tonic-gate uint_t opc;
7630Sstevel@tonic-gate
7640Sstevel@tonic-gate dt_cg_node(dnp->dn_left, dlp, drp);
7650Sstevel@tonic-gate dt_cg_node(dnp->dn_right, dlp, drp);
7660Sstevel@tonic-gate
7670Sstevel@tonic-gate if (dt_node_is_string(dnp->dn_left) || dt_node_is_string(dnp->dn_right))
7680Sstevel@tonic-gate opc = DIF_OP_SCMP;
7690Sstevel@tonic-gate else
7700Sstevel@tonic-gate opc = DIF_OP_CMP;
7710Sstevel@tonic-gate
7720Sstevel@tonic-gate instr = DIF_INSTR_CMP(opc, dnp->dn_left->dn_reg, dnp->dn_right->dn_reg);
7730Sstevel@tonic-gate dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
7740Sstevel@tonic-gate dt_regset_free(drp, dnp->dn_right->dn_reg);
7750Sstevel@tonic-gate dnp->dn_reg = dnp->dn_left->dn_reg;
7760Sstevel@tonic-gate
7770Sstevel@tonic-gate instr = DIF_INSTR_BRANCH(op, lbl_true);
7780Sstevel@tonic-gate dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
7790Sstevel@tonic-gate
7800Sstevel@tonic-gate instr = DIF_INSTR_MOV(DIF_REG_R0, dnp->dn_reg);
7810Sstevel@tonic-gate dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
7820Sstevel@tonic-gate
7830Sstevel@tonic-gate instr = DIF_INSTR_BRANCH(DIF_OP_BA, lbl_post);
7840Sstevel@tonic-gate dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
7850Sstevel@tonic-gate
7860Sstevel@tonic-gate dt_cg_xsetx(dlp, NULL, lbl_true, dnp->dn_reg, 1);
7870Sstevel@tonic-gate dt_irlist_append(dlp, dt_cg_node_alloc(lbl_post, DIF_INSTR_NOP));
7880Sstevel@tonic-gate }
7890Sstevel@tonic-gate
7900Sstevel@tonic-gate /*
7910Sstevel@tonic-gate * Code generation for the ternary op requires some trickery with the assembler
7920Sstevel@tonic-gate * in order to conserve registers. We generate code for dn_expr and dn_left
7930Sstevel@tonic-gate * and free their registers so they do not have be consumed across codegen for
7940Sstevel@tonic-gate * dn_right. We insert a dummy MOV at the end of dn_left into the destination
7950Sstevel@tonic-gate * register, which is not yet known because we haven't done dn_right yet, and
7960Sstevel@tonic-gate * save the pointer to this instruction node. We then generate code for
7970Sstevel@tonic-gate * dn_right and use its register as our output. Finally, we reach back and
7980Sstevel@tonic-gate * patch the instruction for dn_left to move its output into this register.
7990Sstevel@tonic-gate */
8000Sstevel@tonic-gate static void
dt_cg_ternary_op(dt_node_t * dnp,dt_irlist_t * dlp,dt_regset_t * drp)8010Sstevel@tonic-gate dt_cg_ternary_op(dt_node_t *dnp, dt_irlist_t *dlp, dt_regset_t *drp)
8020Sstevel@tonic-gate {
8030Sstevel@tonic-gate uint_t lbl_false = dt_irlist_label(dlp);
8040Sstevel@tonic-gate uint_t lbl_post = dt_irlist_label(dlp);
8050Sstevel@tonic-gate
8060Sstevel@tonic-gate dif_instr_t instr;
8070Sstevel@tonic-gate dt_irnode_t *dip;
8080Sstevel@tonic-gate
8090Sstevel@tonic-gate dt_cg_node(dnp->dn_expr, dlp, drp);
8100Sstevel@tonic-gate instr = DIF_INSTR_TST(dnp->dn_expr->dn_reg);
8110Sstevel@tonic-gate dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
8120Sstevel@tonic-gate dt_regset_free(drp, dnp->dn_expr->dn_reg);
8130Sstevel@tonic-gate
8140Sstevel@tonic-gate instr = DIF_INSTR_BRANCH(DIF_OP_BE, lbl_false);
8150Sstevel@tonic-gate dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
8160Sstevel@tonic-gate
8170Sstevel@tonic-gate dt_cg_node(dnp->dn_left, dlp, drp);
8180Sstevel@tonic-gate instr = DIF_INSTR_MOV(dnp->dn_left->dn_reg, DIF_REG_R0);
8190Sstevel@tonic-gate dip = dt_cg_node_alloc(DT_LBL_NONE, instr); /* save dip for below */
8200Sstevel@tonic-gate dt_irlist_append(dlp, dip);
8210Sstevel@tonic-gate dt_regset_free(drp, dnp->dn_left->dn_reg);
8220Sstevel@tonic-gate
8230Sstevel@tonic-gate instr = DIF_INSTR_BRANCH(DIF_OP_BA, lbl_post);
8240Sstevel@tonic-gate dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
8250Sstevel@tonic-gate
8260Sstevel@tonic-gate dt_irlist_append(dlp, dt_cg_node_alloc(lbl_false, DIF_INSTR_NOP));
8270Sstevel@tonic-gate dt_cg_node(dnp->dn_right, dlp, drp);
8280Sstevel@tonic-gate dnp->dn_reg = dnp->dn_right->dn_reg;
8290Sstevel@tonic-gate
8300Sstevel@tonic-gate /*
8310Sstevel@tonic-gate * Now that dn_reg is assigned, reach back and patch the correct MOV
8320Sstevel@tonic-gate * instruction into the tail of dn_left. We know dn_reg was unused
8330Sstevel@tonic-gate * at that point because otherwise dn_right couldn't have allocated it.
8340Sstevel@tonic-gate */
8350Sstevel@tonic-gate dip->di_instr = DIF_INSTR_MOV(dnp->dn_left->dn_reg, dnp->dn_reg);
8360Sstevel@tonic-gate dt_irlist_append(dlp, dt_cg_node_alloc(lbl_post, DIF_INSTR_NOP));
8370Sstevel@tonic-gate }
8380Sstevel@tonic-gate
8390Sstevel@tonic-gate static void
dt_cg_logical_and(dt_node_t * dnp,dt_irlist_t * dlp,dt_regset_t * drp)8400Sstevel@tonic-gate dt_cg_logical_and(dt_node_t *dnp, dt_irlist_t *dlp, dt_regset_t *drp)
8410Sstevel@tonic-gate {
8420Sstevel@tonic-gate uint_t lbl_false = dt_irlist_label(dlp);
8430Sstevel@tonic-gate uint_t lbl_post = dt_irlist_label(dlp);
8440Sstevel@tonic-gate
8450Sstevel@tonic-gate dif_instr_t instr;
8460Sstevel@tonic-gate
8470Sstevel@tonic-gate dt_cg_node(dnp->dn_left, dlp, drp);
8480Sstevel@tonic-gate instr = DIF_INSTR_TST(dnp->dn_left->dn_reg);
8490Sstevel@tonic-gate dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
8500Sstevel@tonic-gate dt_regset_free(drp, dnp->dn_left->dn_reg);
8510Sstevel@tonic-gate
8520Sstevel@tonic-gate instr = DIF_INSTR_BRANCH(DIF_OP_BE, lbl_false);
8530Sstevel@tonic-gate dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
8540Sstevel@tonic-gate
8550Sstevel@tonic-gate dt_cg_node(dnp->dn_right, dlp, drp);
8560Sstevel@tonic-gate instr = DIF_INSTR_TST(dnp->dn_right->dn_reg);
8570Sstevel@tonic-gate dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
8580Sstevel@tonic-gate dnp->dn_reg = dnp->dn_right->dn_reg;
8590Sstevel@tonic-gate
8600Sstevel@tonic-gate instr = DIF_INSTR_BRANCH(DIF_OP_BE, lbl_false);
8610Sstevel@tonic-gate dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
8620Sstevel@tonic-gate
8630Sstevel@tonic-gate dt_cg_setx(dlp, dnp->dn_reg, 1);
8640Sstevel@tonic-gate
8650Sstevel@tonic-gate instr = DIF_INSTR_BRANCH(DIF_OP_BA, lbl_post);
8660Sstevel@tonic-gate dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
8670Sstevel@tonic-gate
8680Sstevel@tonic-gate instr = DIF_INSTR_MOV(DIF_REG_R0, dnp->dn_reg);
8690Sstevel@tonic-gate dt_irlist_append(dlp, dt_cg_node_alloc(lbl_false, instr));
8700Sstevel@tonic-gate
8710Sstevel@tonic-gate dt_irlist_append(dlp, dt_cg_node_alloc(lbl_post, DIF_INSTR_NOP));
8720Sstevel@tonic-gate }
8730Sstevel@tonic-gate
8740Sstevel@tonic-gate static void
dt_cg_logical_xor(dt_node_t * dnp,dt_irlist_t * dlp,dt_regset_t * drp)8750Sstevel@tonic-gate dt_cg_logical_xor(dt_node_t *dnp, dt_irlist_t *dlp, dt_regset_t *drp)
8760Sstevel@tonic-gate {
8770Sstevel@tonic-gate uint_t lbl_next = dt_irlist_label(dlp);
8780Sstevel@tonic-gate uint_t lbl_tail = dt_irlist_label(dlp);
8790Sstevel@tonic-gate
8800Sstevel@tonic-gate dif_instr_t instr;
8810Sstevel@tonic-gate
8820Sstevel@tonic-gate dt_cg_node(dnp->dn_left, dlp, drp);
8830Sstevel@tonic-gate instr = DIF_INSTR_TST(dnp->dn_left->dn_reg);
8840Sstevel@tonic-gate dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
8850Sstevel@tonic-gate
8860Sstevel@tonic-gate instr = DIF_INSTR_BRANCH(DIF_OP_BE, lbl_next);
8870Sstevel@tonic-gate dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
8880Sstevel@tonic-gate dt_cg_setx(dlp, dnp->dn_left->dn_reg, 1);
8890Sstevel@tonic-gate
8900Sstevel@tonic-gate dt_irlist_append(dlp, dt_cg_node_alloc(lbl_next, DIF_INSTR_NOP));
8910Sstevel@tonic-gate dt_cg_node(dnp->dn_right, dlp, drp);
8920Sstevel@tonic-gate
8930Sstevel@tonic-gate instr = DIF_INSTR_TST(dnp->dn_right->dn_reg);
8940Sstevel@tonic-gate dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
8950Sstevel@tonic-gate
8960Sstevel@tonic-gate instr = DIF_INSTR_BRANCH(DIF_OP_BE, lbl_tail);
8970Sstevel@tonic-gate dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
8980Sstevel@tonic-gate dt_cg_setx(dlp, dnp->dn_right->dn_reg, 1);
8990Sstevel@tonic-gate
9000Sstevel@tonic-gate instr = DIF_INSTR_FMT(DIF_OP_XOR, dnp->dn_left->dn_reg,
9010Sstevel@tonic-gate dnp->dn_right->dn_reg, dnp->dn_left->dn_reg);
9020Sstevel@tonic-gate
9030Sstevel@tonic-gate dt_irlist_append(dlp, dt_cg_node_alloc(lbl_tail, instr));
9040Sstevel@tonic-gate
9050Sstevel@tonic-gate dt_regset_free(drp, dnp->dn_right->dn_reg);
9060Sstevel@tonic-gate dnp->dn_reg = dnp->dn_left->dn_reg;
9070Sstevel@tonic-gate }
9080Sstevel@tonic-gate
9090Sstevel@tonic-gate static void
dt_cg_logical_or(dt_node_t * dnp,dt_irlist_t * dlp,dt_regset_t * drp)9100Sstevel@tonic-gate dt_cg_logical_or(dt_node_t *dnp, dt_irlist_t *dlp, dt_regset_t *drp)
9110Sstevel@tonic-gate {
9120Sstevel@tonic-gate uint_t lbl_true = dt_irlist_label(dlp);
9130Sstevel@tonic-gate uint_t lbl_false = dt_irlist_label(dlp);
9140Sstevel@tonic-gate uint_t lbl_post = dt_irlist_label(dlp);
9150Sstevel@tonic-gate
9160Sstevel@tonic-gate dif_instr_t instr;
9170Sstevel@tonic-gate
9180Sstevel@tonic-gate dt_cg_node(dnp->dn_left, dlp, drp);
9190Sstevel@tonic-gate instr = DIF_INSTR_TST(dnp->dn_left->dn_reg);
9200Sstevel@tonic-gate dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
9210Sstevel@tonic-gate dt_regset_free(drp, dnp->dn_left->dn_reg);
9220Sstevel@tonic-gate
9230Sstevel@tonic-gate instr = DIF_INSTR_BRANCH(DIF_OP_BNE, lbl_true);
9240Sstevel@tonic-gate dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
9250Sstevel@tonic-gate
9260Sstevel@tonic-gate dt_cg_node(dnp->dn_right, dlp, drp);
9270Sstevel@tonic-gate instr = DIF_INSTR_TST(dnp->dn_right->dn_reg);
9280Sstevel@tonic-gate dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
9290Sstevel@tonic-gate dnp->dn_reg = dnp->dn_right->dn_reg;
9300Sstevel@tonic-gate
9310Sstevel@tonic-gate instr = DIF_INSTR_BRANCH(DIF_OP_BE, lbl_false);
9320Sstevel@tonic-gate dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
9330Sstevel@tonic-gate
9340Sstevel@tonic-gate dt_cg_xsetx(dlp, NULL, lbl_true, dnp->dn_reg, 1);
9350Sstevel@tonic-gate
9360Sstevel@tonic-gate instr = DIF_INSTR_BRANCH(DIF_OP_BA, lbl_post);
9370Sstevel@tonic-gate dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
9380Sstevel@tonic-gate
9390Sstevel@tonic-gate instr = DIF_INSTR_MOV(DIF_REG_R0, dnp->dn_reg);
9400Sstevel@tonic-gate dt_irlist_append(dlp, dt_cg_node_alloc(lbl_false, instr));
9410Sstevel@tonic-gate
9420Sstevel@tonic-gate dt_irlist_append(dlp, dt_cg_node_alloc(lbl_post, DIF_INSTR_NOP));
9430Sstevel@tonic-gate }
9440Sstevel@tonic-gate
9450Sstevel@tonic-gate static void
dt_cg_logical_neg(dt_node_t * dnp,dt_irlist_t * dlp,dt_regset_t * drp)9460Sstevel@tonic-gate dt_cg_logical_neg(dt_node_t *dnp, dt_irlist_t *dlp, dt_regset_t *drp)
9470Sstevel@tonic-gate {
9480Sstevel@tonic-gate uint_t lbl_zero = dt_irlist_label(dlp);
9490Sstevel@tonic-gate uint_t lbl_post = dt_irlist_label(dlp);
9500Sstevel@tonic-gate
9510Sstevel@tonic-gate dif_instr_t instr;
9520Sstevel@tonic-gate
9530Sstevel@tonic-gate dt_cg_node(dnp->dn_child, dlp, drp);
9540Sstevel@tonic-gate dnp->dn_reg = dnp->dn_child->dn_reg;
9550Sstevel@tonic-gate
9560Sstevel@tonic-gate instr = DIF_INSTR_TST(dnp->dn_reg);
9570Sstevel@tonic-gate dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
9580Sstevel@tonic-gate
9590Sstevel@tonic-gate instr = DIF_INSTR_BRANCH(DIF_OP_BE, lbl_zero);
9600Sstevel@tonic-gate dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
9610Sstevel@tonic-gate
9620Sstevel@tonic-gate instr = DIF_INSTR_MOV(DIF_REG_R0, dnp->dn_reg);
9630Sstevel@tonic-gate dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
9640Sstevel@tonic-gate
9650Sstevel@tonic-gate instr = DIF_INSTR_BRANCH(DIF_OP_BA, lbl_post);
9660Sstevel@tonic-gate dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
9670Sstevel@tonic-gate
9680Sstevel@tonic-gate dt_cg_xsetx(dlp, NULL, lbl_zero, dnp->dn_reg, 1);
9690Sstevel@tonic-gate dt_irlist_append(dlp, dt_cg_node_alloc(lbl_post, DIF_INSTR_NOP));
9700Sstevel@tonic-gate }
9710Sstevel@tonic-gate
9720Sstevel@tonic-gate static void
dt_cg_asgn_op(dt_node_t * dnp,dt_irlist_t * dlp,dt_regset_t * drp)9730Sstevel@tonic-gate dt_cg_asgn_op(dt_node_t *dnp, dt_irlist_t *dlp, dt_regset_t *drp)
9740Sstevel@tonic-gate {
9750Sstevel@tonic-gate dif_instr_t instr;
9760Sstevel@tonic-gate dt_ident_t *idp;
9770Sstevel@tonic-gate
9780Sstevel@tonic-gate /*
9790Sstevel@tonic-gate * If we are performing a structure assignment of a translated type,
9800Sstevel@tonic-gate * we must instantiate all members and create a snapshot of the object
9810Sstevel@tonic-gate * in scratch space. We allocs a chunk of memory, generate code for
9820Sstevel@tonic-gate * each member, and then set dnp->dn_reg to the scratch object address.
9830Sstevel@tonic-gate */
9840Sstevel@tonic-gate if ((idp = dt_node_resolve(dnp->dn_right, DT_IDENT_XLSOU)) != NULL) {
9850Sstevel@tonic-gate ctf_membinfo_t ctm;
9860Sstevel@tonic-gate dt_xlator_t *dxp = idp->di_data;
9870Sstevel@tonic-gate dt_node_t *mnp, dn, mn;
9880Sstevel@tonic-gate int r1, r2;
9890Sstevel@tonic-gate
9900Sstevel@tonic-gate /*
9910Sstevel@tonic-gate * Create two fake dt_node_t's representing operator "." and a
9920Sstevel@tonic-gate * right-hand identifier child node. These will be repeatedly
9930Sstevel@tonic-gate * modified according to each instantiated member so that we
9940Sstevel@tonic-gate * can pass them to dt_cg_store() and effect a member store.
9950Sstevel@tonic-gate */
9960Sstevel@tonic-gate bzero(&dn, sizeof (dt_node_t));
9970Sstevel@tonic-gate dn.dn_kind = DT_NODE_OP2;
9980Sstevel@tonic-gate dn.dn_op = DT_TOK_DOT;
9990Sstevel@tonic-gate dn.dn_left = dnp;
10000Sstevel@tonic-gate dn.dn_right = &mn;
10010Sstevel@tonic-gate
10020Sstevel@tonic-gate bzero(&mn, sizeof (dt_node_t));
10030Sstevel@tonic-gate mn.dn_kind = DT_NODE_IDENT;
10040Sstevel@tonic-gate mn.dn_op = DT_TOK_IDENT;
10050Sstevel@tonic-gate
10060Sstevel@tonic-gate /*
10070Sstevel@tonic-gate * Allocate a register for our scratch data pointer. First we
10080Sstevel@tonic-gate * set it to the size of our data structure, and then replace
10090Sstevel@tonic-gate * it with the result of an allocs of the specified size.
10100Sstevel@tonic-gate */
10110Sstevel@tonic-gate if ((r1 = dt_regset_alloc(drp)) == -1)
10120Sstevel@tonic-gate longjmp(yypcb->pcb_jmpbuf, EDT_NOREG);
10130Sstevel@tonic-gate
10140Sstevel@tonic-gate dt_cg_setx(dlp, r1,
10150Sstevel@tonic-gate ctf_type_size(dxp->dx_dst_ctfp, dxp->dx_dst_base));
10160Sstevel@tonic-gate
10170Sstevel@tonic-gate instr = DIF_INSTR_ALLOCS(r1, r1);
10180Sstevel@tonic-gate dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
10190Sstevel@tonic-gate
10200Sstevel@tonic-gate /*
10210Sstevel@tonic-gate * When dt_cg_asgn_op() is called, we have already generated
10220Sstevel@tonic-gate * code for dnp->dn_right, which is the translator input. We
10230Sstevel@tonic-gate * now associate this register with the translator's input
10240Sstevel@tonic-gate * identifier so it can be referenced during our member loop.
10250Sstevel@tonic-gate */
10260Sstevel@tonic-gate dxp->dx_ident->di_flags |= DT_IDFLG_CGREG;
10270Sstevel@tonic-gate dxp->dx_ident->di_id = dnp->dn_right->dn_reg;
10280Sstevel@tonic-gate
10290Sstevel@tonic-gate for (mnp = dxp->dx_members; mnp != NULL; mnp = mnp->dn_list) {
10300Sstevel@tonic-gate /*
10310Sstevel@tonic-gate * Generate code for the translator member expression,
10320Sstevel@tonic-gate * and then cast the result to the member type.
10330Sstevel@tonic-gate */
10340Sstevel@tonic-gate dt_cg_node(mnp->dn_membexpr, dlp, drp);
10350Sstevel@tonic-gate mnp->dn_reg = mnp->dn_membexpr->dn_reg;
10360Sstevel@tonic-gate dt_cg_typecast(mnp->dn_membexpr, mnp, dlp, drp);
10370Sstevel@tonic-gate
10380Sstevel@tonic-gate /*
10390Sstevel@tonic-gate * Ask CTF for the offset of the member so we can store
10400Sstevel@tonic-gate * to the appropriate offset. This call has already
10410Sstevel@tonic-gate * been done once by the parser, so it should succeed.
10420Sstevel@tonic-gate */
10430Sstevel@tonic-gate if (ctf_member_info(dxp->dx_dst_ctfp, dxp->dx_dst_base,
10440Sstevel@tonic-gate mnp->dn_membname, &ctm) == CTF_ERR) {
10450Sstevel@tonic-gate yypcb->pcb_hdl->dt_ctferr =
10460Sstevel@tonic-gate ctf_errno(dxp->dx_dst_ctfp);
10470Sstevel@tonic-gate longjmp(yypcb->pcb_jmpbuf, EDT_CTF);
10480Sstevel@tonic-gate }
10490Sstevel@tonic-gate
10500Sstevel@tonic-gate /*
10510Sstevel@tonic-gate * If the destination member is at offset 0, store the
10520Sstevel@tonic-gate * result directly to r1 (the scratch buffer address).
10530Sstevel@tonic-gate * Otherwise allocate another temporary for the offset
10540Sstevel@tonic-gate * and add r1 to it before storing the result.
10550Sstevel@tonic-gate */
10560Sstevel@tonic-gate if (ctm.ctm_offset != 0) {
10570Sstevel@tonic-gate if ((r2 = dt_regset_alloc(drp)) == -1)
10580Sstevel@tonic-gate longjmp(yypcb->pcb_jmpbuf, EDT_NOREG);
10590Sstevel@tonic-gate
10600Sstevel@tonic-gate /*
10610Sstevel@tonic-gate * Add the member offset rounded down to the
10620Sstevel@tonic-gate * nearest byte. If the offset was not aligned
10630Sstevel@tonic-gate * on a byte boundary, this member is a bit-
10640Sstevel@tonic-gate * field and dt_cg_store() will handle masking.
10650Sstevel@tonic-gate */
10660Sstevel@tonic-gate dt_cg_setx(dlp, r2, ctm.ctm_offset / NBBY);
10670Sstevel@tonic-gate instr = DIF_INSTR_FMT(DIF_OP_ADD, r1, r2, r2);
10680Sstevel@tonic-gate dt_irlist_append(dlp,
10690Sstevel@tonic-gate dt_cg_node_alloc(DT_LBL_NONE, instr));
10700Sstevel@tonic-gate
10710Sstevel@tonic-gate dt_node_type_propagate(mnp, &dn);
10720Sstevel@tonic-gate dn.dn_right->dn_string = mnp->dn_membname;
10730Sstevel@tonic-gate dn.dn_reg = r2;
10740Sstevel@tonic-gate
10750Sstevel@tonic-gate dt_cg_store(mnp, dlp, drp, &dn);
10760Sstevel@tonic-gate dt_regset_free(drp, r2);
10770Sstevel@tonic-gate
10780Sstevel@tonic-gate } else {
10790Sstevel@tonic-gate dt_node_type_propagate(mnp, &dn);
10800Sstevel@tonic-gate dn.dn_right->dn_string = mnp->dn_membname;
10810Sstevel@tonic-gate dn.dn_reg = r1;
10820Sstevel@tonic-gate
10830Sstevel@tonic-gate dt_cg_store(mnp, dlp, drp, &dn);
10840Sstevel@tonic-gate }
10850Sstevel@tonic-gate
10860Sstevel@tonic-gate dt_regset_free(drp, mnp->dn_reg);
10870Sstevel@tonic-gate }
10880Sstevel@tonic-gate
10890Sstevel@tonic-gate dxp->dx_ident->di_flags &= ~DT_IDFLG_CGREG;
10900Sstevel@tonic-gate dxp->dx_ident->di_id = 0;
10910Sstevel@tonic-gate
1092*265Smws if (dnp->dn_right->dn_reg != -1)
1093*265Smws dt_regset_free(drp, dnp->dn_right->dn_reg);
1094*265Smws
10950Sstevel@tonic-gate assert(dnp->dn_reg == dnp->dn_right->dn_reg);
10960Sstevel@tonic-gate dnp->dn_reg = r1;
10970Sstevel@tonic-gate }
10980Sstevel@tonic-gate
10990Sstevel@tonic-gate /*
11000Sstevel@tonic-gate * If we are storing to a variable, generate an stv instruction from
11010Sstevel@tonic-gate * the variable specified by the identifier. If we are storing to a
11020Sstevel@tonic-gate * memory address, generate code again for the left-hand side using
11030Sstevel@tonic-gate * DT_NF_REF to get the address, and then generate a store to it.
11040Sstevel@tonic-gate * In both paths, we assume dnp->dn_reg already has the new value.
11050Sstevel@tonic-gate */
11060Sstevel@tonic-gate if (dnp->dn_left->dn_kind == DT_NODE_VAR) {
11070Sstevel@tonic-gate idp = dt_ident_resolve(dnp->dn_left->dn_ident);
11080Sstevel@tonic-gate
11090Sstevel@tonic-gate if (idp->di_kind == DT_IDENT_ARRAY)
11100Sstevel@tonic-gate dt_cg_arglist(idp, dnp->dn_left->dn_args, dlp, drp);
11110Sstevel@tonic-gate
11120Sstevel@tonic-gate idp->di_flags |= DT_IDFLG_DIFW;
11130Sstevel@tonic-gate instr = DIF_INSTR_STV(dt_cg_stvar(idp),
11140Sstevel@tonic-gate idp->di_id, dnp->dn_reg);
11150Sstevel@tonic-gate dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
11160Sstevel@tonic-gate } else {
11170Sstevel@tonic-gate uint_t rbit = dnp->dn_left->dn_flags & DT_NF_REF;
11180Sstevel@tonic-gate
11190Sstevel@tonic-gate assert(dnp->dn_left->dn_flags & DT_NF_WRITABLE);
11200Sstevel@tonic-gate assert(dnp->dn_left->dn_flags & DT_NF_LVALUE);
11210Sstevel@tonic-gate
11220Sstevel@tonic-gate dnp->dn_left->dn_flags |= DT_NF_REF; /* force pass-by-ref */
11230Sstevel@tonic-gate
11240Sstevel@tonic-gate dt_cg_node(dnp->dn_left, dlp, drp);
11250Sstevel@tonic-gate dt_cg_store(dnp, dlp, drp, dnp->dn_left);
11260Sstevel@tonic-gate dt_regset_free(drp, dnp->dn_left->dn_reg);
11270Sstevel@tonic-gate
11280Sstevel@tonic-gate dnp->dn_left->dn_flags &= ~DT_NF_REF;
11290Sstevel@tonic-gate dnp->dn_left->dn_flags |= rbit;
11300Sstevel@tonic-gate }
11310Sstevel@tonic-gate }
11320Sstevel@tonic-gate
11330Sstevel@tonic-gate static void
dt_cg_assoc_op(dt_node_t * dnp,dt_irlist_t * dlp,dt_regset_t * drp)11340Sstevel@tonic-gate dt_cg_assoc_op(dt_node_t *dnp, dt_irlist_t *dlp, dt_regset_t *drp)
11350Sstevel@tonic-gate {
11360Sstevel@tonic-gate dif_instr_t instr;
11370Sstevel@tonic-gate uint_t op;
11380Sstevel@tonic-gate
11390Sstevel@tonic-gate assert(dnp->dn_kind == DT_NODE_VAR);
11400Sstevel@tonic-gate assert(!(dnp->dn_ident->di_flags & DT_IDFLG_LOCAL));
11410Sstevel@tonic-gate assert(dnp->dn_args != NULL);
11420Sstevel@tonic-gate
11430Sstevel@tonic-gate dt_cg_arglist(dnp->dn_ident, dnp->dn_args, dlp, drp);
11440Sstevel@tonic-gate
11450Sstevel@tonic-gate if ((dnp->dn_reg = dt_regset_alloc(drp)) == -1)
11460Sstevel@tonic-gate longjmp(yypcb->pcb_jmpbuf, EDT_NOREG);
11470Sstevel@tonic-gate
11480Sstevel@tonic-gate if (dnp->dn_ident->di_flags & DT_IDFLG_TLS)
11490Sstevel@tonic-gate op = DIF_OP_LDTAA;
11500Sstevel@tonic-gate else
11510Sstevel@tonic-gate op = DIF_OP_LDGAA;
11520Sstevel@tonic-gate
11530Sstevel@tonic-gate dnp->dn_ident->di_flags |= DT_IDFLG_DIFR;
11540Sstevel@tonic-gate instr = DIF_INSTR_LDV(op, dnp->dn_ident->di_id, dnp->dn_reg);
11550Sstevel@tonic-gate dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
11560Sstevel@tonic-gate
11570Sstevel@tonic-gate /*
11580Sstevel@tonic-gate * If the associative array is a pass-by-reference type, then we are
11590Sstevel@tonic-gate * loading its value as a pointer to either load or store through it.
11600Sstevel@tonic-gate * The array element in question may not have been faulted in yet, in
11610Sstevel@tonic-gate * which case DIF_OP_LD*AA will return zero. We append an epilogue
11620Sstevel@tonic-gate * of instructions similar to the following:
11630Sstevel@tonic-gate *
11640Sstevel@tonic-gate * ld?aa id, %r1 ! base ld?aa instruction above
11650Sstevel@tonic-gate * tst %r1 ! start of epilogue
11660Sstevel@tonic-gate * +--- bne label
11670Sstevel@tonic-gate * | setx size, %r1
11680Sstevel@tonic-gate * | allocs %r1, %r1
11690Sstevel@tonic-gate * | st?aa id, %r1
11700Sstevel@tonic-gate * | ld?aa id, %r1
11710Sstevel@tonic-gate * v
11720Sstevel@tonic-gate * label: < rest of code >
11730Sstevel@tonic-gate *
11740Sstevel@tonic-gate * The idea is that we allocs a zero-filled chunk of scratch space and
11750Sstevel@tonic-gate * do a DIF_OP_ST*AA to fault in and initialize the array element, and
11760Sstevel@tonic-gate * then reload it to get the faulted-in address of the new variable
11770Sstevel@tonic-gate * storage. This isn't cheap, but pass-by-ref associative array values
11780Sstevel@tonic-gate * are (thus far) uncommon and the allocs cost only occurs once. If
11790Sstevel@tonic-gate * this path becomes important to DTrace users, we can improve things
11800Sstevel@tonic-gate * by adding a new DIF opcode to fault in associative array elements.
11810Sstevel@tonic-gate */
11820Sstevel@tonic-gate if (dnp->dn_flags & DT_NF_REF) {
11830Sstevel@tonic-gate uint_t stvop = op == DIF_OP_LDTAA ? DIF_OP_STTAA : DIF_OP_STGAA;
11840Sstevel@tonic-gate uint_t label = dt_irlist_label(dlp);
11850Sstevel@tonic-gate
11860Sstevel@tonic-gate instr = DIF_INSTR_TST(dnp->dn_reg);
11870Sstevel@tonic-gate dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
11880Sstevel@tonic-gate
11890Sstevel@tonic-gate instr = DIF_INSTR_BRANCH(DIF_OP_BNE, label);
11900Sstevel@tonic-gate dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
11910Sstevel@tonic-gate
11920Sstevel@tonic-gate dt_cg_setx(dlp, dnp->dn_reg, dt_node_type_size(dnp));
11930Sstevel@tonic-gate instr = DIF_INSTR_ALLOCS(dnp->dn_reg, dnp->dn_reg);
11940Sstevel@tonic-gate dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
11950Sstevel@tonic-gate
11960Sstevel@tonic-gate dnp->dn_ident->di_flags |= DT_IDFLG_DIFW;
11970Sstevel@tonic-gate instr = DIF_INSTR_STV(stvop, dnp->dn_ident->di_id, dnp->dn_reg);
11980Sstevel@tonic-gate dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
11990Sstevel@tonic-gate
12000Sstevel@tonic-gate instr = DIF_INSTR_LDV(op, dnp->dn_ident->di_id, dnp->dn_reg);
12010Sstevel@tonic-gate dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
12020Sstevel@tonic-gate
12030Sstevel@tonic-gate dt_irlist_append(dlp, dt_cg_node_alloc(label, DIF_INSTR_NOP));
12040Sstevel@tonic-gate }
12050Sstevel@tonic-gate }
12060Sstevel@tonic-gate
12070Sstevel@tonic-gate static void
dt_cg_array_op(dt_node_t * dnp,dt_irlist_t * dlp,dt_regset_t * drp)12080Sstevel@tonic-gate dt_cg_array_op(dt_node_t *dnp, dt_irlist_t *dlp, dt_regset_t *drp)
12090Sstevel@tonic-gate {
12100Sstevel@tonic-gate dt_probe_t *prp = yypcb->pcb_probe;
12110Sstevel@tonic-gate uintmax_t saved = dnp->dn_args->dn_value;
1212*265Smws dt_ident_t *idp = dnp->dn_ident;
12130Sstevel@tonic-gate
12140Sstevel@tonic-gate dif_instr_t instr;
12150Sstevel@tonic-gate uint_t op;
12160Sstevel@tonic-gate size_t size;
12170Sstevel@tonic-gate int reg, n;
12180Sstevel@tonic-gate
12190Sstevel@tonic-gate assert(dnp->dn_kind == DT_NODE_VAR);
1220*265Smws assert(!(idp->di_flags & DT_IDFLG_LOCAL));
12210Sstevel@tonic-gate
12220Sstevel@tonic-gate assert(dnp->dn_args->dn_kind == DT_NODE_INT);
12230Sstevel@tonic-gate assert(dnp->dn_args->dn_list == NULL);
12240Sstevel@tonic-gate
12250Sstevel@tonic-gate /*
12260Sstevel@tonic-gate * If this is a reference in the args[] array, temporarily modify the
1227*265Smws * array index according to the static argument mapping (if any),
1228*265Smws * unless the argument reference is provided by a dynamic translator.
1229*265Smws * If we're using a dynamic translator for args[], then just set dn_reg
1230*265Smws * to an invalid reg and return: DIF_OP_XLARG will fetch the arg later.
12310Sstevel@tonic-gate */
1232*265Smws if (idp->di_id == DIF_VAR_ARGS) {
1233*265Smws if ((idp->di_kind == DT_IDENT_XLPTR ||
1234*265Smws idp->di_kind == DT_IDENT_XLSOU) &&
1235*265Smws dt_xlator_dynamic(idp->di_data)) {
1236*265Smws dnp->dn_reg = -1;
1237*265Smws return;
1238*265Smws }
12390Sstevel@tonic-gate dnp->dn_args->dn_value = prp->pr_mapping[saved];
1240*265Smws }
12410Sstevel@tonic-gate
12420Sstevel@tonic-gate dt_cg_node(dnp->dn_args, dlp, drp);
12430Sstevel@tonic-gate dnp->dn_args->dn_value = saved;
12440Sstevel@tonic-gate
12450Sstevel@tonic-gate dnp->dn_reg = dnp->dn_args->dn_reg;
12460Sstevel@tonic-gate
1247*265Smws if (idp->di_flags & DT_IDFLG_TLS)
12480Sstevel@tonic-gate op = DIF_OP_LDTA;
12490Sstevel@tonic-gate else
12500Sstevel@tonic-gate op = DIF_OP_LDGA;
12510Sstevel@tonic-gate
1252*265Smws idp->di_flags |= DT_IDFLG_DIFR;
12530Sstevel@tonic-gate
1254*265Smws instr = DIF_INSTR_LDA(op, idp->di_id,
12550Sstevel@tonic-gate dnp->dn_args->dn_reg, dnp->dn_reg);
12560Sstevel@tonic-gate
12570Sstevel@tonic-gate dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
12580Sstevel@tonic-gate
12590Sstevel@tonic-gate /*
12600Sstevel@tonic-gate * If this is a reference to the args[] array, we need to take the
12610Sstevel@tonic-gate * additional step of explicitly eliminating any bits larger than the
12620Sstevel@tonic-gate * type size: the DIF interpreter in the kernel will always give us
12630Sstevel@tonic-gate * the raw (64-bit) argument value, and any bits larger than the type
12640Sstevel@tonic-gate * size may be junk. As a practical matter, this arises only on 64-bit
12650Sstevel@tonic-gate * architectures and only when the argument index is larger than the
12660Sstevel@tonic-gate * number of arguments passed directly to DTrace: if a 8-, 16- or
12670Sstevel@tonic-gate * 32-bit argument must be retrieved from the stack, it is possible
12680Sstevel@tonic-gate * (and it some cases, likely) that the upper bits will be garbage.
12690Sstevel@tonic-gate */
1270*265Smws if (idp->di_id != DIF_VAR_ARGS || !dt_node_is_scalar(dnp))
12710Sstevel@tonic-gate return;
12720Sstevel@tonic-gate
12730Sstevel@tonic-gate if ((size = dt_node_type_size(dnp)) == sizeof (uint64_t))
12740Sstevel@tonic-gate return;
12750Sstevel@tonic-gate
12760Sstevel@tonic-gate if ((reg = dt_regset_alloc(drp)) == -1)
12770Sstevel@tonic-gate longjmp(yypcb->pcb_jmpbuf, EDT_NOREG);
12780Sstevel@tonic-gate
12790Sstevel@tonic-gate assert(size < sizeof (uint64_t));
12800Sstevel@tonic-gate n = sizeof (uint64_t) * NBBY - size * NBBY;
12810Sstevel@tonic-gate
12820Sstevel@tonic-gate dt_cg_setx(dlp, reg, n);
12830Sstevel@tonic-gate
12840Sstevel@tonic-gate instr = DIF_INSTR_FMT(DIF_OP_SLL, dnp->dn_reg, reg, dnp->dn_reg);
12850Sstevel@tonic-gate dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
12860Sstevel@tonic-gate
12870Sstevel@tonic-gate instr = DIF_INSTR_FMT((dnp->dn_flags & DT_NF_SIGNED) ?
12880Sstevel@tonic-gate DIF_OP_SRA : DIF_OP_SRL, dnp->dn_reg, reg, dnp->dn_reg);
12890Sstevel@tonic-gate
12900Sstevel@tonic-gate dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
12910Sstevel@tonic-gate dt_regset_free(drp, reg);
12920Sstevel@tonic-gate }
12930Sstevel@tonic-gate
12940Sstevel@tonic-gate /*
12950Sstevel@tonic-gate * Generate code for an inlined variable reference. Inlines can be used to
12960Sstevel@tonic-gate * define either scalar or associative array substitutions. For scalars, we
12970Sstevel@tonic-gate * simply generate code for the parse tree saved in the identifier's din_root,
12980Sstevel@tonic-gate * and then cast the resulting expression to the inline's declaration type.
12990Sstevel@tonic-gate * For arrays, we take the input parameter subtrees from dnp->dn_args and
13000Sstevel@tonic-gate * temporarily store them in the din_root of each din_argv[i] identifier,
13010Sstevel@tonic-gate * which are themselves inlines and were set up for us by the parser. The
13020Sstevel@tonic-gate * result is that any reference to the inlined parameter inside the top-level
13030Sstevel@tonic-gate * din_root will turn into a recursive call to dt_cg_inline() for a scalar
13040Sstevel@tonic-gate * inline whose din_root will refer to the subtree pointed to by the argument.
13050Sstevel@tonic-gate */
13060Sstevel@tonic-gate static void
dt_cg_inline(dt_node_t * dnp,dt_irlist_t * dlp,dt_regset_t * drp)13070Sstevel@tonic-gate dt_cg_inline(dt_node_t *dnp, dt_irlist_t *dlp, dt_regset_t *drp)
13080Sstevel@tonic-gate {
13090Sstevel@tonic-gate dt_ident_t *idp = dnp->dn_ident;
13100Sstevel@tonic-gate dt_idnode_t *inp = idp->di_iarg;
13110Sstevel@tonic-gate
13120Sstevel@tonic-gate dt_idnode_t *pinp;
13130Sstevel@tonic-gate dt_node_t *pnp;
13140Sstevel@tonic-gate int i;
13150Sstevel@tonic-gate
13160Sstevel@tonic-gate assert(idp->di_flags & DT_IDFLG_INLINE);
13170Sstevel@tonic-gate assert(idp->di_ops == &dt_idops_inline);
13180Sstevel@tonic-gate
13190Sstevel@tonic-gate if (idp->di_kind == DT_IDENT_ARRAY) {
13200Sstevel@tonic-gate for (i = 0, pnp = dnp->dn_args;
13210Sstevel@tonic-gate pnp != NULL; pnp = pnp->dn_list, i++) {
13220Sstevel@tonic-gate if (inp->din_argv[i] != NULL) {
13230Sstevel@tonic-gate pinp = inp->din_argv[i]->di_iarg;
13240Sstevel@tonic-gate pinp->din_root = pnp;
13250Sstevel@tonic-gate }
13260Sstevel@tonic-gate }
13270Sstevel@tonic-gate }
13280Sstevel@tonic-gate
13290Sstevel@tonic-gate dt_cg_node(inp->din_root, dlp, drp);
13300Sstevel@tonic-gate dnp->dn_reg = inp->din_root->dn_reg;
13310Sstevel@tonic-gate dt_cg_typecast(inp->din_root, dnp, dlp, drp);
13320Sstevel@tonic-gate
13330Sstevel@tonic-gate if (idp->di_kind == DT_IDENT_ARRAY) {
13340Sstevel@tonic-gate for (i = 0; i < inp->din_argc; i++) {
13350Sstevel@tonic-gate pinp = inp->din_argv[i]->di_iarg;
13360Sstevel@tonic-gate pinp->din_root = NULL;
13370Sstevel@tonic-gate }
13380Sstevel@tonic-gate }
13390Sstevel@tonic-gate }
13400Sstevel@tonic-gate
13410Sstevel@tonic-gate static void
dt_cg_node(dt_node_t * dnp,dt_irlist_t * dlp,dt_regset_t * drp)13420Sstevel@tonic-gate dt_cg_node(dt_node_t *dnp, dt_irlist_t *dlp, dt_regset_t *drp)
13430Sstevel@tonic-gate {
13440Sstevel@tonic-gate ctf_file_t *ctfp = dnp->dn_ctfp;
13450Sstevel@tonic-gate ctf_file_t *octfp;
13460Sstevel@tonic-gate ctf_membinfo_t m;
13470Sstevel@tonic-gate ctf_id_t type;
13480Sstevel@tonic-gate
13490Sstevel@tonic-gate dif_instr_t instr;
13500Sstevel@tonic-gate dt_ident_t *idp;
13510Sstevel@tonic-gate ssize_t stroff;
13520Sstevel@tonic-gate uint_t op;
13530Sstevel@tonic-gate int reg;
13540Sstevel@tonic-gate
13550Sstevel@tonic-gate switch (dnp->dn_op) {
13560Sstevel@tonic-gate case DT_TOK_COMMA:
13570Sstevel@tonic-gate dt_cg_node(dnp->dn_left, dlp, drp);
13580Sstevel@tonic-gate dt_regset_free(drp, dnp->dn_left->dn_reg);
13590Sstevel@tonic-gate dt_cg_node(dnp->dn_right, dlp, drp);
13600Sstevel@tonic-gate dnp->dn_reg = dnp->dn_right->dn_reg;
13610Sstevel@tonic-gate break;
13620Sstevel@tonic-gate
13630Sstevel@tonic-gate case DT_TOK_ASGN:
13640Sstevel@tonic-gate dt_cg_node(dnp->dn_right, dlp, drp);
13650Sstevel@tonic-gate dnp->dn_reg = dnp->dn_right->dn_reg;
13660Sstevel@tonic-gate dt_cg_asgn_op(dnp, dlp, drp);
13670Sstevel@tonic-gate break;
13680Sstevel@tonic-gate
13690Sstevel@tonic-gate case DT_TOK_ADD_EQ:
13700Sstevel@tonic-gate dt_cg_arithmetic_op(dnp, dlp, drp, DIF_OP_ADD);
13710Sstevel@tonic-gate dt_cg_asgn_op(dnp, dlp, drp);
13720Sstevel@tonic-gate break;
13730Sstevel@tonic-gate
13740Sstevel@tonic-gate case DT_TOK_SUB_EQ:
13750Sstevel@tonic-gate dt_cg_arithmetic_op(dnp, dlp, drp, DIF_OP_SUB);
13760Sstevel@tonic-gate dt_cg_asgn_op(dnp, dlp, drp);
13770Sstevel@tonic-gate break;
13780Sstevel@tonic-gate
13790Sstevel@tonic-gate case DT_TOK_MUL_EQ:
13800Sstevel@tonic-gate dt_cg_arithmetic_op(dnp, dlp, drp, DIF_OP_MUL);
13810Sstevel@tonic-gate dt_cg_asgn_op(dnp, dlp, drp);
13820Sstevel@tonic-gate break;
13830Sstevel@tonic-gate
13840Sstevel@tonic-gate case DT_TOK_DIV_EQ:
13850Sstevel@tonic-gate dt_cg_arithmetic_op(dnp, dlp, drp,
13860Sstevel@tonic-gate (dnp->dn_flags & DT_NF_SIGNED) ? DIF_OP_SDIV : DIF_OP_UDIV);
13870Sstevel@tonic-gate dt_cg_asgn_op(dnp, dlp, drp);
13880Sstevel@tonic-gate break;
13890Sstevel@tonic-gate
13900Sstevel@tonic-gate case DT_TOK_MOD_EQ:
13910Sstevel@tonic-gate dt_cg_arithmetic_op(dnp, dlp, drp,
13920Sstevel@tonic-gate (dnp->dn_flags & DT_NF_SIGNED) ? DIF_OP_SREM : DIF_OP_UREM);
13930Sstevel@tonic-gate dt_cg_asgn_op(dnp, dlp, drp);
13940Sstevel@tonic-gate break;
13950Sstevel@tonic-gate
13960Sstevel@tonic-gate case DT_TOK_AND_EQ:
13970Sstevel@tonic-gate dt_cg_arithmetic_op(dnp, dlp, drp, DIF_OP_AND);
13980Sstevel@tonic-gate dt_cg_asgn_op(dnp, dlp, drp);
13990Sstevel@tonic-gate break;
14000Sstevel@tonic-gate
14010Sstevel@tonic-gate case DT_TOK_XOR_EQ:
14020Sstevel@tonic-gate dt_cg_arithmetic_op(dnp, dlp, drp, DIF_OP_XOR);
14030Sstevel@tonic-gate dt_cg_asgn_op(dnp, dlp, drp);
14040Sstevel@tonic-gate break;
14050Sstevel@tonic-gate
14060Sstevel@tonic-gate case DT_TOK_OR_EQ:
14070Sstevel@tonic-gate dt_cg_arithmetic_op(dnp, dlp, drp, DIF_OP_OR);
14080Sstevel@tonic-gate dt_cg_asgn_op(dnp, dlp, drp);
14090Sstevel@tonic-gate break;
14100Sstevel@tonic-gate
14110Sstevel@tonic-gate case DT_TOK_LSH_EQ:
14120Sstevel@tonic-gate dt_cg_arithmetic_op(dnp, dlp, drp, DIF_OP_SLL);
14130Sstevel@tonic-gate dt_cg_asgn_op(dnp, dlp, drp);
14140Sstevel@tonic-gate break;
14150Sstevel@tonic-gate
14160Sstevel@tonic-gate case DT_TOK_RSH_EQ:
14170Sstevel@tonic-gate dt_cg_arithmetic_op(dnp, dlp, drp,
14180Sstevel@tonic-gate (dnp->dn_flags & DT_NF_SIGNED) ? DIF_OP_SRA : DIF_OP_SRL);
14190Sstevel@tonic-gate dt_cg_asgn_op(dnp, dlp, drp);
14200Sstevel@tonic-gate break;
14210Sstevel@tonic-gate
14220Sstevel@tonic-gate case DT_TOK_QUESTION:
14230Sstevel@tonic-gate dt_cg_ternary_op(dnp, dlp, drp);
14240Sstevel@tonic-gate break;
14250Sstevel@tonic-gate
14260Sstevel@tonic-gate case DT_TOK_LOR:
14270Sstevel@tonic-gate dt_cg_logical_or(dnp, dlp, drp);
14280Sstevel@tonic-gate break;
14290Sstevel@tonic-gate
14300Sstevel@tonic-gate case DT_TOK_LXOR:
14310Sstevel@tonic-gate dt_cg_logical_xor(dnp, dlp, drp);
14320Sstevel@tonic-gate break;
14330Sstevel@tonic-gate
14340Sstevel@tonic-gate case DT_TOK_LAND:
14350Sstevel@tonic-gate dt_cg_logical_and(dnp, dlp, drp);
14360Sstevel@tonic-gate break;
14370Sstevel@tonic-gate
14380Sstevel@tonic-gate case DT_TOK_BOR:
14390Sstevel@tonic-gate dt_cg_arithmetic_op(dnp, dlp, drp, DIF_OP_OR);
14400Sstevel@tonic-gate break;
14410Sstevel@tonic-gate
14420Sstevel@tonic-gate case DT_TOK_XOR:
14430Sstevel@tonic-gate dt_cg_arithmetic_op(dnp, dlp, drp, DIF_OP_XOR);
14440Sstevel@tonic-gate break;
14450Sstevel@tonic-gate
14460Sstevel@tonic-gate case DT_TOK_BAND:
14470Sstevel@tonic-gate dt_cg_arithmetic_op(dnp, dlp, drp, DIF_OP_AND);
14480Sstevel@tonic-gate break;
14490Sstevel@tonic-gate
14500Sstevel@tonic-gate case DT_TOK_EQU:
14510Sstevel@tonic-gate dt_cg_compare_op(dnp, dlp, drp, DIF_OP_BE);
14520Sstevel@tonic-gate break;
14530Sstevel@tonic-gate
14540Sstevel@tonic-gate case DT_TOK_NEQ:
14550Sstevel@tonic-gate dt_cg_compare_op(dnp, dlp, drp, DIF_OP_BNE);
14560Sstevel@tonic-gate break;
14570Sstevel@tonic-gate
14580Sstevel@tonic-gate case DT_TOK_LT:
14590Sstevel@tonic-gate dt_cg_compare_op(dnp, dlp, drp,
14600Sstevel@tonic-gate dt_cg_compare_signed(dnp) ? DIF_OP_BL : DIF_OP_BLU);
14610Sstevel@tonic-gate break;
14620Sstevel@tonic-gate
14630Sstevel@tonic-gate case DT_TOK_LE:
14640Sstevel@tonic-gate dt_cg_compare_op(dnp, dlp, drp,
14650Sstevel@tonic-gate dt_cg_compare_signed(dnp) ? DIF_OP_BLE : DIF_OP_BLEU);
14660Sstevel@tonic-gate break;
14670Sstevel@tonic-gate
14680Sstevel@tonic-gate case DT_TOK_GT:
14690Sstevel@tonic-gate dt_cg_compare_op(dnp, dlp, drp,
14700Sstevel@tonic-gate dt_cg_compare_signed(dnp) ? DIF_OP_BG : DIF_OP_BGU);
14710Sstevel@tonic-gate break;
14720Sstevel@tonic-gate
14730Sstevel@tonic-gate case DT_TOK_GE:
14740Sstevel@tonic-gate dt_cg_compare_op(dnp, dlp, drp,
14750Sstevel@tonic-gate dt_cg_compare_signed(dnp) ? DIF_OP_BGE : DIF_OP_BGEU);
14760Sstevel@tonic-gate break;
14770Sstevel@tonic-gate
14780Sstevel@tonic-gate case DT_TOK_LSH:
14790Sstevel@tonic-gate dt_cg_arithmetic_op(dnp, dlp, drp, DIF_OP_SLL);
14800Sstevel@tonic-gate break;
14810Sstevel@tonic-gate
14820Sstevel@tonic-gate case DT_TOK_RSH:
14830Sstevel@tonic-gate dt_cg_arithmetic_op(dnp, dlp, drp,
14840Sstevel@tonic-gate (dnp->dn_flags & DT_NF_SIGNED) ? DIF_OP_SRA : DIF_OP_SRL);
14850Sstevel@tonic-gate break;
14860Sstevel@tonic-gate
14870Sstevel@tonic-gate case DT_TOK_ADD:
14880Sstevel@tonic-gate dt_cg_arithmetic_op(dnp, dlp, drp, DIF_OP_ADD);
14890Sstevel@tonic-gate break;
14900Sstevel@tonic-gate
14910Sstevel@tonic-gate case DT_TOK_SUB:
14920Sstevel@tonic-gate dt_cg_arithmetic_op(dnp, dlp, drp, DIF_OP_SUB);
14930Sstevel@tonic-gate break;
14940Sstevel@tonic-gate
14950Sstevel@tonic-gate case DT_TOK_MUL:
14960Sstevel@tonic-gate dt_cg_arithmetic_op(dnp, dlp, drp, DIF_OP_MUL);
14970Sstevel@tonic-gate break;
14980Sstevel@tonic-gate
14990Sstevel@tonic-gate case DT_TOK_DIV:
15000Sstevel@tonic-gate dt_cg_arithmetic_op(dnp, dlp, drp,
15010Sstevel@tonic-gate (dnp->dn_flags & DT_NF_SIGNED) ? DIF_OP_SDIV : DIF_OP_UDIV);
15020Sstevel@tonic-gate break;
15030Sstevel@tonic-gate
15040Sstevel@tonic-gate case DT_TOK_MOD:
15050Sstevel@tonic-gate dt_cg_arithmetic_op(dnp, dlp, drp,
15060Sstevel@tonic-gate (dnp->dn_flags & DT_NF_SIGNED) ? DIF_OP_SREM : DIF_OP_UREM);
15070Sstevel@tonic-gate break;
15080Sstevel@tonic-gate
15090Sstevel@tonic-gate case DT_TOK_LNEG:
15100Sstevel@tonic-gate dt_cg_logical_neg(dnp, dlp, drp);
15110Sstevel@tonic-gate break;
15120Sstevel@tonic-gate
15130Sstevel@tonic-gate case DT_TOK_BNEG:
15140Sstevel@tonic-gate dt_cg_node(dnp->dn_child, dlp, drp);
15150Sstevel@tonic-gate dnp->dn_reg = dnp->dn_child->dn_reg;
15160Sstevel@tonic-gate instr = DIF_INSTR_NOT(dnp->dn_reg, dnp->dn_reg);
15170Sstevel@tonic-gate dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
15180Sstevel@tonic-gate break;
15190Sstevel@tonic-gate
15200Sstevel@tonic-gate case DT_TOK_PREINC:
15210Sstevel@tonic-gate dt_cg_prearith_op(dnp, dlp, drp, DIF_OP_ADD);
15220Sstevel@tonic-gate break;
15230Sstevel@tonic-gate
15240Sstevel@tonic-gate case DT_TOK_POSTINC:
15250Sstevel@tonic-gate dt_cg_postarith_op(dnp, dlp, drp, DIF_OP_ADD);
15260Sstevel@tonic-gate break;
15270Sstevel@tonic-gate
15280Sstevel@tonic-gate case DT_TOK_PREDEC:
15290Sstevel@tonic-gate dt_cg_prearith_op(dnp, dlp, drp, DIF_OP_SUB);
15300Sstevel@tonic-gate break;
15310Sstevel@tonic-gate
15320Sstevel@tonic-gate case DT_TOK_POSTDEC:
15330Sstevel@tonic-gate dt_cg_postarith_op(dnp, dlp, drp, DIF_OP_SUB);
15340Sstevel@tonic-gate break;
15350Sstevel@tonic-gate
15360Sstevel@tonic-gate case DT_TOK_IPOS:
15370Sstevel@tonic-gate dt_cg_node(dnp->dn_child, dlp, drp);
15380Sstevel@tonic-gate dnp->dn_reg = dnp->dn_child->dn_reg;
15390Sstevel@tonic-gate break;
15400Sstevel@tonic-gate
15410Sstevel@tonic-gate case DT_TOK_INEG:
15420Sstevel@tonic-gate dt_cg_node(dnp->dn_child, dlp, drp);
15430Sstevel@tonic-gate dnp->dn_reg = dnp->dn_child->dn_reg;
15440Sstevel@tonic-gate
15450Sstevel@tonic-gate instr = DIF_INSTR_FMT(DIF_OP_SUB, DIF_REG_R0,
15460Sstevel@tonic-gate dnp->dn_reg, dnp->dn_reg);
15470Sstevel@tonic-gate
15480Sstevel@tonic-gate dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
15490Sstevel@tonic-gate break;
15500Sstevel@tonic-gate
15510Sstevel@tonic-gate case DT_TOK_DEREF:
15520Sstevel@tonic-gate dt_cg_node(dnp->dn_child, dlp, drp);
15530Sstevel@tonic-gate dnp->dn_reg = dnp->dn_child->dn_reg;
15540Sstevel@tonic-gate
15550Sstevel@tonic-gate if (!(dnp->dn_flags & DT_NF_REF)) {
15560Sstevel@tonic-gate uint_t ubit = dnp->dn_flags & DT_NF_USERLAND;
15570Sstevel@tonic-gate
15580Sstevel@tonic-gate /*
15590Sstevel@tonic-gate * Save and restore DT_NF_USERLAND across dt_cg_load():
15600Sstevel@tonic-gate * we need the sign bit from dnp and the user bit from
15610Sstevel@tonic-gate * dnp->dn_child in order to get the proper opcode.
15620Sstevel@tonic-gate */
15630Sstevel@tonic-gate dnp->dn_flags |=
15640Sstevel@tonic-gate (dnp->dn_child->dn_flags & DT_NF_USERLAND);
15650Sstevel@tonic-gate
15660Sstevel@tonic-gate instr = DIF_INSTR_LOAD(dt_cg_load(dnp, ctfp,
15670Sstevel@tonic-gate dnp->dn_type), dnp->dn_reg, dnp->dn_reg);
15680Sstevel@tonic-gate
15690Sstevel@tonic-gate dnp->dn_flags &= ~DT_NF_USERLAND;
15700Sstevel@tonic-gate dnp->dn_flags |= ubit;
15710Sstevel@tonic-gate
15720Sstevel@tonic-gate dt_irlist_append(dlp,
15730Sstevel@tonic-gate dt_cg_node_alloc(DT_LBL_NONE, instr));
15740Sstevel@tonic-gate }
15750Sstevel@tonic-gate break;
15760Sstevel@tonic-gate
15770Sstevel@tonic-gate case DT_TOK_ADDROF: {
15780Sstevel@tonic-gate uint_t rbit = dnp->dn_child->dn_flags & DT_NF_REF;
15790Sstevel@tonic-gate
15800Sstevel@tonic-gate dnp->dn_child->dn_flags |= DT_NF_REF; /* force pass-by-ref */
15810Sstevel@tonic-gate dt_cg_node(dnp->dn_child, dlp, drp);
15820Sstevel@tonic-gate dnp->dn_reg = dnp->dn_child->dn_reg;
15830Sstevel@tonic-gate
15840Sstevel@tonic-gate dnp->dn_child->dn_flags &= ~DT_NF_REF;
15850Sstevel@tonic-gate dnp->dn_child->dn_flags |= rbit;
15860Sstevel@tonic-gate break;
15870Sstevel@tonic-gate }
15880Sstevel@tonic-gate
15890Sstevel@tonic-gate case DT_TOK_SIZEOF: {
15900Sstevel@tonic-gate size_t size = dt_node_sizeof(dnp->dn_child);
15910Sstevel@tonic-gate
15920Sstevel@tonic-gate if ((dnp->dn_reg = dt_regset_alloc(drp)) == -1)
15930Sstevel@tonic-gate longjmp(yypcb->pcb_jmpbuf, EDT_NOREG);
15940Sstevel@tonic-gate
15950Sstevel@tonic-gate assert(size != 0);
15960Sstevel@tonic-gate dt_cg_setx(dlp, dnp->dn_reg, size);
15970Sstevel@tonic-gate break;
15980Sstevel@tonic-gate }
15990Sstevel@tonic-gate
16000Sstevel@tonic-gate case DT_TOK_STRINGOF:
16010Sstevel@tonic-gate dt_cg_node(dnp->dn_child, dlp, drp);
16020Sstevel@tonic-gate dnp->dn_reg = dnp->dn_child->dn_reg;
16030Sstevel@tonic-gate break;
16040Sstevel@tonic-gate
16050Sstevel@tonic-gate case DT_TOK_XLATE:
1606*265Smws /*
1607*265Smws * An xlate operator appears in either an XLATOR, indicating a
1608*265Smws * reference to a dynamic translator, or an OP2, indicating
1609*265Smws * use of the xlate operator in the user's program. For the
1610*265Smws * dynamic case, generate an xlate opcode with a reference to
1611*265Smws * the corresponding member, pre-computed for us in dn_members.
1612*265Smws */
1613*265Smws if (dnp->dn_kind == DT_NODE_XLATOR) {
1614*265Smws dt_xlator_t *dxp = dnp->dn_xlator;
1615*265Smws
1616*265Smws assert(dxp->dx_ident->di_flags & DT_IDFLG_CGREG);
1617*265Smws assert(dxp->dx_ident->di_id != 0);
1618*265Smws
1619*265Smws if ((dnp->dn_reg = dt_regset_alloc(drp)) == -1)
1620*265Smws longjmp(yypcb->pcb_jmpbuf, EDT_NOREG);
1621*265Smws
1622*265Smws if (dxp->dx_arg == -1) {
1623*265Smws instr = DIF_INSTR_MOV(
1624*265Smws dxp->dx_ident->di_id, dnp->dn_reg);
1625*265Smws dt_irlist_append(dlp,
1626*265Smws dt_cg_node_alloc(DT_LBL_NONE, instr));
1627*265Smws op = DIF_OP_XLATE;
1628*265Smws } else
1629*265Smws op = DIF_OP_XLARG;
1630*265Smws
1631*265Smws instr = DIF_INSTR_XLATE(op, 0, dnp->dn_reg);
1632*265Smws dt_irlist_append(dlp,
1633*265Smws dt_cg_node_alloc(DT_LBL_NONE, instr));
1634*265Smws
1635*265Smws dlp->dl_last->di_extern = dnp->dn_xmember;
1636*265Smws break;
1637*265Smws }
1638*265Smws
1639*265Smws assert(dnp->dn_kind == DT_NODE_OP2);
16400Sstevel@tonic-gate dt_cg_node(dnp->dn_right, dlp, drp);
16410Sstevel@tonic-gate dnp->dn_reg = dnp->dn_right->dn_reg;
16420Sstevel@tonic-gate break;
16430Sstevel@tonic-gate
16440Sstevel@tonic-gate case DT_TOK_LPAR:
16450Sstevel@tonic-gate dt_cg_node(dnp->dn_right, dlp, drp);
16460Sstevel@tonic-gate dnp->dn_reg = dnp->dn_right->dn_reg;
16470Sstevel@tonic-gate dt_cg_typecast(dnp->dn_right, dnp, dlp, drp);
16480Sstevel@tonic-gate break;
16490Sstevel@tonic-gate
16500Sstevel@tonic-gate case DT_TOK_PTR:
16510Sstevel@tonic-gate case DT_TOK_DOT:
16520Sstevel@tonic-gate assert(dnp->dn_right->dn_kind == DT_NODE_IDENT);
16530Sstevel@tonic-gate dt_cg_node(dnp->dn_left, dlp, drp);
16540Sstevel@tonic-gate
16550Sstevel@tonic-gate /*
16560Sstevel@tonic-gate * If the left-hand side of PTR or DOT is a dynamic variable,
16570Sstevel@tonic-gate * we expect it to be the output of a D translator. In this
16580Sstevel@tonic-gate * case, we look up the parse tree corresponding to the member
16590Sstevel@tonic-gate * that is being accessed and run the code generator over it.
16600Sstevel@tonic-gate * We then cast the result as if by the assignment operator.
16610Sstevel@tonic-gate */
16620Sstevel@tonic-gate if ((idp = dt_node_resolve(
16630Sstevel@tonic-gate dnp->dn_left, DT_IDENT_XLSOU)) != NULL ||
16640Sstevel@tonic-gate (idp = dt_node_resolve(
16650Sstevel@tonic-gate dnp->dn_left, DT_IDENT_XLPTR)) != NULL) {
16660Sstevel@tonic-gate
16670Sstevel@tonic-gate dt_xlator_t *dxp;
16680Sstevel@tonic-gate dt_node_t *mnp;
16690Sstevel@tonic-gate
16700Sstevel@tonic-gate dxp = idp->di_data;
16710Sstevel@tonic-gate mnp = dt_xlator_member(dxp, dnp->dn_right->dn_string);
16720Sstevel@tonic-gate assert(mnp != NULL);
16730Sstevel@tonic-gate
16740Sstevel@tonic-gate dxp->dx_ident->di_flags |= DT_IDFLG_CGREG;
16750Sstevel@tonic-gate dxp->dx_ident->di_id = dnp->dn_left->dn_reg;
16760Sstevel@tonic-gate
16770Sstevel@tonic-gate dt_cg_node(mnp->dn_membexpr, dlp, drp);
16780Sstevel@tonic-gate dnp->dn_reg = mnp->dn_membexpr->dn_reg;
16790Sstevel@tonic-gate dt_cg_typecast(mnp->dn_membexpr, dnp, dlp, drp);
16800Sstevel@tonic-gate
16810Sstevel@tonic-gate dxp->dx_ident->di_flags &= ~DT_IDFLG_CGREG;
16820Sstevel@tonic-gate dxp->dx_ident->di_id = 0;
16830Sstevel@tonic-gate
1684*265Smws if (dnp->dn_left->dn_reg != -1)
1685*265Smws dt_regset_free(drp, dnp->dn_left->dn_reg);
16860Sstevel@tonic-gate break;
16870Sstevel@tonic-gate }
16880Sstevel@tonic-gate
16890Sstevel@tonic-gate ctfp = dnp->dn_left->dn_ctfp;
16900Sstevel@tonic-gate type = ctf_type_resolve(ctfp, dnp->dn_left->dn_type);
16910Sstevel@tonic-gate
16920Sstevel@tonic-gate if (dnp->dn_op == DT_TOK_PTR) {
16930Sstevel@tonic-gate type = ctf_type_reference(ctfp, type);
16940Sstevel@tonic-gate type = ctf_type_resolve(ctfp, type);
16950Sstevel@tonic-gate }
16960Sstevel@tonic-gate
16970Sstevel@tonic-gate if ((ctfp = dt_cg_membinfo(octfp = ctfp, type,
16980Sstevel@tonic-gate dnp->dn_right->dn_string, &m)) == NULL) {
16990Sstevel@tonic-gate yypcb->pcb_hdl->dt_ctferr = ctf_errno(octfp);
17000Sstevel@tonic-gate longjmp(yypcb->pcb_jmpbuf, EDT_CTF);
17010Sstevel@tonic-gate }
17020Sstevel@tonic-gate
17030Sstevel@tonic-gate if (m.ctm_offset != 0) {
17040Sstevel@tonic-gate if ((reg = dt_regset_alloc(drp)) == -1)
17050Sstevel@tonic-gate longjmp(yypcb->pcb_jmpbuf, EDT_NOREG);
17060Sstevel@tonic-gate
17070Sstevel@tonic-gate /*
17080Sstevel@tonic-gate * If the offset is not aligned on a byte boundary, it
17090Sstevel@tonic-gate * is a bit-field member and we will extract the value
17100Sstevel@tonic-gate * bits below after we generate the appropriate load.
17110Sstevel@tonic-gate */
17120Sstevel@tonic-gate dt_cg_setx(dlp, reg, m.ctm_offset / NBBY);
17130Sstevel@tonic-gate
17140Sstevel@tonic-gate instr = DIF_INSTR_FMT(DIF_OP_ADD,
17150Sstevel@tonic-gate dnp->dn_left->dn_reg, reg, dnp->dn_left->dn_reg);
17160Sstevel@tonic-gate
17170Sstevel@tonic-gate dt_irlist_append(dlp,
17180Sstevel@tonic-gate dt_cg_node_alloc(DT_LBL_NONE, instr));
17190Sstevel@tonic-gate dt_regset_free(drp, reg);
17200Sstevel@tonic-gate }
17210Sstevel@tonic-gate
17220Sstevel@tonic-gate if (!(dnp->dn_flags & DT_NF_REF)) {
17230Sstevel@tonic-gate uint_t ubit = dnp->dn_flags & DT_NF_USERLAND;
17240Sstevel@tonic-gate
17250Sstevel@tonic-gate /*
17260Sstevel@tonic-gate * Save and restore DT_NF_USERLAND across dt_cg_load():
17270Sstevel@tonic-gate * we need the sign bit from dnp and the user bit from
17280Sstevel@tonic-gate * dnp->dn_left in order to get the proper opcode.
17290Sstevel@tonic-gate */
17300Sstevel@tonic-gate dnp->dn_flags |=
17310Sstevel@tonic-gate (dnp->dn_left->dn_flags & DT_NF_USERLAND);
17320Sstevel@tonic-gate
17330Sstevel@tonic-gate instr = DIF_INSTR_LOAD(dt_cg_load(dnp,
17340Sstevel@tonic-gate ctfp, m.ctm_type), dnp->dn_left->dn_reg,
17350Sstevel@tonic-gate dnp->dn_left->dn_reg);
17360Sstevel@tonic-gate
17370Sstevel@tonic-gate dnp->dn_flags &= ~DT_NF_USERLAND;
17380Sstevel@tonic-gate dnp->dn_flags |= ubit;
17390Sstevel@tonic-gate
17400Sstevel@tonic-gate dt_irlist_append(dlp,
17410Sstevel@tonic-gate dt_cg_node_alloc(DT_LBL_NONE, instr));
17420Sstevel@tonic-gate
17430Sstevel@tonic-gate if (dnp->dn_flags & DT_NF_BITFIELD)
17440Sstevel@tonic-gate dt_cg_field_get(dnp, dlp, drp, ctfp, &m);
17450Sstevel@tonic-gate }
17460Sstevel@tonic-gate
17470Sstevel@tonic-gate dnp->dn_reg = dnp->dn_left->dn_reg;
17480Sstevel@tonic-gate break;
17490Sstevel@tonic-gate
17500Sstevel@tonic-gate case DT_TOK_STRING:
17510Sstevel@tonic-gate if ((dnp->dn_reg = dt_regset_alloc(drp)) == -1)
17520Sstevel@tonic-gate longjmp(yypcb->pcb_jmpbuf, EDT_NOREG);
17530Sstevel@tonic-gate
17540Sstevel@tonic-gate assert(dnp->dn_kind == DT_NODE_STRING);
17550Sstevel@tonic-gate stroff = dt_strtab_insert(yypcb->pcb_strtab, dnp->dn_string);
17560Sstevel@tonic-gate
17570Sstevel@tonic-gate if (stroff == -1L)
17580Sstevel@tonic-gate longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
17590Sstevel@tonic-gate if (stroff > DIF_STROFF_MAX)
17600Sstevel@tonic-gate longjmp(yypcb->pcb_jmpbuf, EDT_STR2BIG);
17610Sstevel@tonic-gate
17620Sstevel@tonic-gate instr = DIF_INSTR_SETS((ulong_t)stroff, dnp->dn_reg);
17630Sstevel@tonic-gate dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
17640Sstevel@tonic-gate break;
17650Sstevel@tonic-gate
17660Sstevel@tonic-gate case DT_TOK_IDENT:
17670Sstevel@tonic-gate /*
17680Sstevel@tonic-gate * If the specified identifier is a variable on which we have
17690Sstevel@tonic-gate * set the code generator register flag, then this variable
17700Sstevel@tonic-gate * has already had code generated for it and saved in di_id.
17710Sstevel@tonic-gate * Allocate a new register and copy the existing value to it.
17720Sstevel@tonic-gate */
17730Sstevel@tonic-gate if (dnp->dn_kind == DT_NODE_VAR &&
17740Sstevel@tonic-gate (dnp->dn_ident->di_flags & DT_IDFLG_CGREG)) {
17750Sstevel@tonic-gate if ((dnp->dn_reg = dt_regset_alloc(drp)) == -1)
17760Sstevel@tonic-gate longjmp(yypcb->pcb_jmpbuf, EDT_NOREG);
17770Sstevel@tonic-gate instr = DIF_INSTR_MOV(dnp->dn_ident->di_id,
17780Sstevel@tonic-gate dnp->dn_reg);
17790Sstevel@tonic-gate dt_irlist_append(dlp,
17800Sstevel@tonic-gate dt_cg_node_alloc(DT_LBL_NONE, instr));
17810Sstevel@tonic-gate break;
17820Sstevel@tonic-gate }
17830Sstevel@tonic-gate
17840Sstevel@tonic-gate /*
17850Sstevel@tonic-gate * Identifiers can represent function calls, variable refs, or
17860Sstevel@tonic-gate * symbols. First we check for inlined variables, and handle
17870Sstevel@tonic-gate * them by generating code for the inline parse tree.
17880Sstevel@tonic-gate */
17890Sstevel@tonic-gate if (dnp->dn_kind == DT_NODE_VAR &&
17900Sstevel@tonic-gate (dnp->dn_ident->di_flags & DT_IDFLG_INLINE)) {
17910Sstevel@tonic-gate dt_cg_inline(dnp, dlp, drp);
17920Sstevel@tonic-gate break;
17930Sstevel@tonic-gate }
17940Sstevel@tonic-gate
17950Sstevel@tonic-gate switch (dnp->dn_kind) {
17960Sstevel@tonic-gate case DT_NODE_FUNC:
17970Sstevel@tonic-gate if ((idp = dnp->dn_ident)->di_kind != DT_IDENT_FUNC) {
17980Sstevel@tonic-gate dnerror(dnp, D_CG_EXPR, "%s %s( ) may not be "
17990Sstevel@tonic-gate "called from a D expression (D program "
18000Sstevel@tonic-gate "context required)\n",
18010Sstevel@tonic-gate dt_idkind_name(idp->di_kind), idp->di_name);
18020Sstevel@tonic-gate }
18030Sstevel@tonic-gate
18040Sstevel@tonic-gate dt_cg_arglist(dnp->dn_ident, dnp->dn_args, dlp, drp);
18050Sstevel@tonic-gate
18060Sstevel@tonic-gate if ((dnp->dn_reg = dt_regset_alloc(drp)) == -1)
18070Sstevel@tonic-gate longjmp(yypcb->pcb_jmpbuf, EDT_NOREG);
18080Sstevel@tonic-gate
18090Sstevel@tonic-gate instr = DIF_INSTR_CALL(
18100Sstevel@tonic-gate dnp->dn_ident->di_id, dnp->dn_reg);
18110Sstevel@tonic-gate
18120Sstevel@tonic-gate dt_irlist_append(dlp,
18130Sstevel@tonic-gate dt_cg_node_alloc(DT_LBL_NONE, instr));
18140Sstevel@tonic-gate
18150Sstevel@tonic-gate break;
18160Sstevel@tonic-gate
18170Sstevel@tonic-gate case DT_NODE_VAR:
18180Sstevel@tonic-gate if (dnp->dn_ident->di_kind == DT_IDENT_XLSOU ||
18190Sstevel@tonic-gate dnp->dn_ident->di_kind == DT_IDENT_XLPTR) {
18200Sstevel@tonic-gate /*
18210Sstevel@tonic-gate * This can only happen if we have translated
18220Sstevel@tonic-gate * args[]. See dt_idcook_args() for details.
18230Sstevel@tonic-gate */
18240Sstevel@tonic-gate assert(dnp->dn_ident->di_id == DIF_VAR_ARGS);
18250Sstevel@tonic-gate dt_cg_array_op(dnp, dlp, drp);
18260Sstevel@tonic-gate break;
18270Sstevel@tonic-gate }
18280Sstevel@tonic-gate
18290Sstevel@tonic-gate if (dnp->dn_ident->di_kind == DT_IDENT_ARRAY) {
18300Sstevel@tonic-gate if (dnp->dn_ident->di_id > DIF_VAR_ARRAY_MAX)
18310Sstevel@tonic-gate dt_cg_assoc_op(dnp, dlp, drp);
18320Sstevel@tonic-gate else
18330Sstevel@tonic-gate dt_cg_array_op(dnp, dlp, drp);
18340Sstevel@tonic-gate break;
18350Sstevel@tonic-gate }
18360Sstevel@tonic-gate
18370Sstevel@tonic-gate if ((dnp->dn_reg = dt_regset_alloc(drp)) == -1)
18380Sstevel@tonic-gate longjmp(yypcb->pcb_jmpbuf, EDT_NOREG);
18390Sstevel@tonic-gate
18400Sstevel@tonic-gate if (dnp->dn_ident->di_flags & DT_IDFLG_LOCAL)
18410Sstevel@tonic-gate op = DIF_OP_LDLS;
18420Sstevel@tonic-gate else if (dnp->dn_ident->di_flags & DT_IDFLG_TLS)
18430Sstevel@tonic-gate op = DIF_OP_LDTS;
18440Sstevel@tonic-gate else
18450Sstevel@tonic-gate op = DIF_OP_LDGS;
18460Sstevel@tonic-gate
18470Sstevel@tonic-gate dnp->dn_ident->di_flags |= DT_IDFLG_DIFR;
18480Sstevel@tonic-gate
18490Sstevel@tonic-gate instr = DIF_INSTR_LDV(op,
18500Sstevel@tonic-gate dnp->dn_ident->di_id, dnp->dn_reg);
18510Sstevel@tonic-gate
18520Sstevel@tonic-gate dt_irlist_append(dlp,
18530Sstevel@tonic-gate dt_cg_node_alloc(DT_LBL_NONE, instr));
18540Sstevel@tonic-gate break;
18550Sstevel@tonic-gate
18560Sstevel@tonic-gate case DT_NODE_SYM: {
18570Sstevel@tonic-gate dtrace_hdl_t *dtp = yypcb->pcb_hdl;
18580Sstevel@tonic-gate dtrace_syminfo_t *sip = dnp->dn_ident->di_data;
18590Sstevel@tonic-gate GElf_Sym sym;
18600Sstevel@tonic-gate
18610Sstevel@tonic-gate if (dtrace_lookup_by_name(dtp,
18620Sstevel@tonic-gate sip->dts_object, sip->dts_name, &sym, NULL) == -1) {
18630Sstevel@tonic-gate xyerror(D_UNKNOWN, "cg failed for symbol %s`%s:"
18640Sstevel@tonic-gate " %s\n", sip->dts_object, sip->dts_name,
18650Sstevel@tonic-gate dtrace_errmsg(dtp, dtrace_errno(dtp)));
18660Sstevel@tonic-gate }
18670Sstevel@tonic-gate
18680Sstevel@tonic-gate if ((dnp->dn_reg = dt_regset_alloc(drp)) == -1)
18690Sstevel@tonic-gate longjmp(yypcb->pcb_jmpbuf, EDT_NOREG);
18700Sstevel@tonic-gate
18710Sstevel@tonic-gate dt_cg_xsetx(dlp, dnp->dn_ident,
18720Sstevel@tonic-gate DT_LBL_NONE, dnp->dn_reg, sym.st_value);
18730Sstevel@tonic-gate
18740Sstevel@tonic-gate if (!(dnp->dn_flags & DT_NF_REF)) {
18750Sstevel@tonic-gate instr = DIF_INSTR_LOAD(dt_cg_load(dnp, ctfp,
18760Sstevel@tonic-gate dnp->dn_type), dnp->dn_reg, dnp->dn_reg);
18770Sstevel@tonic-gate dt_irlist_append(dlp,
18780Sstevel@tonic-gate dt_cg_node_alloc(DT_LBL_NONE, instr));
18790Sstevel@tonic-gate }
18800Sstevel@tonic-gate break;
18810Sstevel@tonic-gate }
18820Sstevel@tonic-gate
18830Sstevel@tonic-gate default:
18840Sstevel@tonic-gate xyerror(D_UNKNOWN, "internal error -- node type %u is "
18850Sstevel@tonic-gate "not valid for an identifier\n", dnp->dn_kind);
18860Sstevel@tonic-gate }
18870Sstevel@tonic-gate break;
18880Sstevel@tonic-gate
18890Sstevel@tonic-gate case DT_TOK_INT:
18900Sstevel@tonic-gate if ((dnp->dn_reg = dt_regset_alloc(drp)) == -1)
18910Sstevel@tonic-gate longjmp(yypcb->pcb_jmpbuf, EDT_NOREG);
18920Sstevel@tonic-gate
18930Sstevel@tonic-gate dt_cg_setx(dlp, dnp->dn_reg, dnp->dn_value);
18940Sstevel@tonic-gate break;
18950Sstevel@tonic-gate
18960Sstevel@tonic-gate default:
18970Sstevel@tonic-gate xyerror(D_UNKNOWN, "internal error -- token type %u is not a "
18980Sstevel@tonic-gate "valid D compilation token\n", dnp->dn_op);
18990Sstevel@tonic-gate }
19000Sstevel@tonic-gate }
19010Sstevel@tonic-gate
19020Sstevel@tonic-gate void
dt_cg(dt_pcb_t * pcb,dt_node_t * dnp)19030Sstevel@tonic-gate dt_cg(dt_pcb_t *pcb, dt_node_t *dnp)
19040Sstevel@tonic-gate {
19050Sstevel@tonic-gate dif_instr_t instr;
1906*265Smws dt_xlator_t *dxp;
19070Sstevel@tonic-gate
19080Sstevel@tonic-gate if (pcb->pcb_regs == NULL && (pcb->pcb_regs =
19090Sstevel@tonic-gate dt_regset_create(pcb->pcb_hdl->dt_conf.dtc_difintregs)) == NULL)
19100Sstevel@tonic-gate longjmp(pcb->pcb_jmpbuf, EDT_NOMEM);
19110Sstevel@tonic-gate
19120Sstevel@tonic-gate dt_regset_reset(pcb->pcb_regs);
19130Sstevel@tonic-gate (void) dt_regset_alloc(pcb->pcb_regs); /* allocate %r0 */
19140Sstevel@tonic-gate
19150Sstevel@tonic-gate if (pcb->pcb_inttab != NULL)
19160Sstevel@tonic-gate dt_inttab_destroy(pcb->pcb_inttab);
19170Sstevel@tonic-gate
19180Sstevel@tonic-gate if ((pcb->pcb_inttab = dt_inttab_create(yypcb->pcb_hdl)) == NULL)
19190Sstevel@tonic-gate longjmp(pcb->pcb_jmpbuf, EDT_NOMEM);
19200Sstevel@tonic-gate
19210Sstevel@tonic-gate if (pcb->pcb_strtab != NULL)
19220Sstevel@tonic-gate dt_strtab_destroy(pcb->pcb_strtab);
19230Sstevel@tonic-gate
19240Sstevel@tonic-gate if ((pcb->pcb_strtab = dt_strtab_create(BUFSIZ)) == NULL)
19250Sstevel@tonic-gate longjmp(pcb->pcb_jmpbuf, EDT_NOMEM);
19260Sstevel@tonic-gate
19270Sstevel@tonic-gate dt_irlist_destroy(&pcb->pcb_ir);
19280Sstevel@tonic-gate dt_irlist_create(&pcb->pcb_ir);
19290Sstevel@tonic-gate
19300Sstevel@tonic-gate assert(pcb->pcb_dret == NULL);
19310Sstevel@tonic-gate pcb->pcb_dret = dnp;
19320Sstevel@tonic-gate
19330Sstevel@tonic-gate if (dt_node_is_dynamic(dnp)) {
19340Sstevel@tonic-gate dnerror(dnp, D_CG_DYN, "expression cannot evaluate to result "
19350Sstevel@tonic-gate "of dynamic type\n");
19360Sstevel@tonic-gate }
19370Sstevel@tonic-gate
1938*265Smws /*
1939*265Smws * If we're generating code for a translator body, assign the input
1940*265Smws * parameter to the first available register (i.e. caller passes %r1).
1941*265Smws */
1942*265Smws if (dnp->dn_kind == DT_NODE_MEMBER) {
1943*265Smws dxp = dnp->dn_membxlator;
1944*265Smws dnp = dnp->dn_membexpr;
1945*265Smws
1946*265Smws dxp->dx_ident->di_flags |= DT_IDFLG_CGREG;
1947*265Smws dxp->dx_ident->di_id = dt_regset_alloc(pcb->pcb_regs);
1948*265Smws }
1949*265Smws
19500Sstevel@tonic-gate dt_cg_node(dnp, &pcb->pcb_ir, pcb->pcb_regs);
19510Sstevel@tonic-gate instr = DIF_INSTR_RET(dnp->dn_reg);
19520Sstevel@tonic-gate dt_regset_free(pcb->pcb_regs, dnp->dn_reg);
19530Sstevel@tonic-gate dt_irlist_append(&pcb->pcb_ir, dt_cg_node_alloc(DT_LBL_NONE, instr));
1954*265Smws
1955*265Smws if (dnp->dn_kind == DT_NODE_MEMBER) {
1956*265Smws dt_regset_free(pcb->pcb_regs, dxp->dx_ident->di_id);
1957*265Smws dxp->dx_ident->di_id = 0;
1958*265Smws dxp->dx_ident->di_flags &= ~DT_IDFLG_CGREG;
1959*265Smws }
19600Sstevel@tonic-gate }
1961