diff options
Diffstat (limited to 'src/arch/sparc')
-rw-r--r-- | src/arch/sparc/.gitignore | 3 | ||||
-rw-r--r-- | src/arch/sparc/Makefile.am | 7 | ||||
-rw-r--r-- | src/arch/sparc/sparc-codegen.h | 955 | ||||
-rw-r--r-- | src/arch/sparc/test.c | 123 | ||||
-rw-r--r-- | src/arch/sparc/tramp.c | 1080 |
5 files changed, 2168 insertions, 0 deletions
diff --git a/src/arch/sparc/.gitignore b/src/arch/sparc/.gitignore new file mode 100644 index 0000000..dc1ebd2 --- /dev/null +++ b/src/arch/sparc/.gitignore @@ -0,0 +1,3 @@ +/Makefile +/Makefile.in +/.deps diff --git a/src/arch/sparc/Makefile.am b/src/arch/sparc/Makefile.am new file mode 100644 index 0000000..a888904 --- /dev/null +++ b/src/arch/sparc/Makefile.am @@ -0,0 +1,7 @@ + +AM_CPPFLAGS = $(GLIB_CFLAGS) -I$(top_srcdir) + +noinst_LTLIBRARIES = libmonoarch-sparc.la + +libmonoarch_sparc_la_SOURCES = tramp.c sparc-codegen.h + diff --git a/src/arch/sparc/sparc-codegen.h b/src/arch/sparc/sparc-codegen.h new file mode 100644 index 0000000..eb421bb --- /dev/null +++ b/src/arch/sparc/sparc-codegen.h @@ -0,0 +1,955 @@ +#ifndef __SPARC_CODEGEN_H__ +#define __SPARC_CODEGEN_H__ + +#if SIZEOF_VOID_P == 8 +#define SPARCV9 1 +#else +#endif + +typedef enum { + sparc_r0 = 0, + sparc_r1 = 1, + sparc_r2 = 2, + sparc_r3 = 3, + sparc_r4 = 4, + sparc_r5 = 5, + sparc_r6 = 6, + sparc_r7 = 7, + sparc_r8 = 8, + sparc_r9 = 9, + sparc_r10 = 10, + sparc_r11 = 11, + sparc_r12 = 12, + sparc_r13 = 13, + sparc_r14 = 14, + sparc_r15 = 15, + sparc_r16 = 16, + sparc_r17 = 17, + sparc_r18 = 18, + sparc_r19 = 19, + sparc_r20 = 20, + sparc_r21 = 21, + sparc_r22 = 22, + sparc_r23 = 23, + sparc_r24 = 24, + sparc_r25 = 25, + sparc_r26 = 26, + sparc_r27 = 27, + sparc_r28 = 28, + sparc_r29 = 29, + sparc_r30 = 30, + sparc_r31 = 31, + /* aliases */ + /* global registers */ + sparc_g0 = 0, sparc_zero = 0, + sparc_g1 = 1, + sparc_g2 = 2, + sparc_g3 = 3, + sparc_g4 = 4, + sparc_g5 = 5, + sparc_g6 = 6, + sparc_g7 = 7, + /* out registers */ + sparc_o0 = 8, + sparc_o1 = 9, + sparc_o2 = 10, + sparc_o3 = 11, + sparc_o4 = 12, + sparc_o5 = 13, + sparc_o6 = 14, sparc_sp = 14, + sparc_o7 = 15, sparc_callsite = 15, + /* local registers */ + sparc_l0 = 16, + sparc_l1 = 17, + sparc_l2 = 18, + sparc_l3 = 19, + sparc_l4 = 20, + sparc_l5 = 21, + sparc_l6 = 22, + sparc_l7 = 23, + /* in registers */ + sparc_i0 = 24, + sparc_i1 = 25, + sparc_i2 = 26, + sparc_i3 = 27, + sparc_i4 = 28, + sparc_i5 = 29, + sparc_i6 = 30, sparc_fp = 30, + sparc_i7 = 31, + sparc_nreg = 32, + /* floating point registers */ + sparc_f0 = 0, + sparc_f1 = 1, + sparc_f2 = 2, + sparc_f3 = 3, + sparc_f4 = 4, + sparc_f5 = 5, + sparc_f6 = 6, + sparc_f7 = 7, + sparc_f8 = 8, + sparc_f9 = 9, + sparc_f10 = 10, + sparc_f11 = 11, + sparc_f12 = 12, + sparc_f13 = 13, + sparc_f14 = 14, + sparc_f15 = 15, + sparc_f16 = 16, + sparc_f17 = 17, + sparc_f18 = 18, + sparc_f19 = 19, + sparc_f20 = 20, + sparc_f21 = 21, + sparc_f22 = 22, + sparc_f23 = 23, + sparc_f24 = 24, + sparc_f25 = 25, + sparc_f26 = 26, + sparc_f27 = 27, + sparc_f28 = 28, + sparc_f29 = 29, + sparc_f30 = 30, + sparc_f31 = 31, +} SparcRegister; + +typedef enum { + sparc_bn = 0, sparc_bnever = 0, + sparc_be = 1, + sparc_ble = 2, + sparc_bl = 3, + sparc_bleu = 4, + sparc_bcs = 5, sparc_blu = 5, + sparc_bneg = 6, + sparc_bvs = 7, sparc_boverflow = 7, + sparc_ba = 8, sparc_balways = 8, + sparc_bne = 9, + sparc_bg = 10, + sparc_bge = 11, + sparc_bgu = 12, + sparc_bcc = 13, sparc_beu = 13, + sparc_bpos = 14, + sparc_bvc = 15 +} SparcCond; + +typedef enum { + /* with fcmp */ + sparc_feq = 0, + sparc_fl = 1, + sparc_fg = 2, + sparc_unordered = 3, + /* branch ops */ + sparc_fba = 8, + sparc_fbn = 0, + sparc_fbu = 7, + sparc_fbg = 6, + sparc_fbug = 5, + sparc_fbl = 4, + sparc_fbul = 3, + sparc_fblg = 2, + sparc_fbne = 1, + sparc_fbe = 9, + sparc_fbue = 10, + sparc_fbge = 11, + sparc_fbuge = 12, + sparc_fble = 13, + sparc_fbule = 14, + sparc_fbo = 15 +} SparcFCond; + +typedef enum { + sparc_icc = 4, + sparc_xcc = 6, + sparc_fcc0 = 0, + sparc_fcc1 = 1, + sparc_fcc2 = 2, + sparc_fcc3 = 3 +} SparcCC; + +typedef enum { + sparc_icc_short = 0, + sparc_xcc_short = 2 +} SparcCCShort; + +typedef enum { + /* fop1 format */ + sparc_fitos_val = 196, + sparc_fitod_val = 200, + sparc_fitoq_val = 204, + sparc_fxtos_val = 132, + sparc_fxtod_val = 136, + sparc_fxtoq_val = 140, + sparc_fstoi_val = 209, + sparc_fdtoi_val = 210, + sparc_fqtoi_val = 211, + sparc_fstod_val = 201, + sparc_fstoq_val = 205, + sparc_fdtos_val = 198, + sparc_fdtoq_val = 206, + sparc_fqtos_val = 199, + sparc_fqtod_val = 203, + sparc_fmovs_val = 1, + sparc_fmovd_val = 2, + sparc_fnegs_val = 5, + sparc_fnegd_val = 6, + sparc_fabss_val = 9, + sparc_fabsd_val = 10, + sparc_fsqrts_val = 41, + sparc_fsqrtd_val = 42, + sparc_fsqrtq_val = 43, + sparc_fadds_val = 65, + sparc_faddd_val = 66, + sparc_faddq_val = 67, + sparc_fsubs_val = 69, + sparc_fsubd_val = 70, + sparc_fsubq_val = 71, + sparc_fmuls_val = 73, + sparc_fmuld_val = 74, + sparc_fmulq_val = 75, + sparc_fsmuld_val = 105, + sparc_fdmulq_val = 111, + sparc_fdivs_val = 77, + sparc_fdivd_val = 78, + sparc_fdivq_val = 79, + /* fop2 format */ + sparc_fcmps_val = 81, + sparc_fcmpd_val = 82, + sparc_fcmpq_val = 83, + sparc_fcmpes_val = 85, + sparc_fcmped_val = 86, + sparc_fcmpeq_val = 87 +} SparcFOp; + +typedef enum { + sparc_membar_load_load = 0x1, + sparc_membar_store_load = 0x2, + sparc_membar_load_store = 0x4, + sparc_membar_store_store = 0x8, + + sparc_membar_lookaside = 0x10, + sparc_membar_memissue = 0x20, + sparc_membar_sync = 0x40, + + sparc_membar_all = 0x4f +} SparcMembarFlags; + +typedef struct { + unsigned int op : 2; /* always 1 */ + unsigned int disp : 30; +} sparc_format1; + +typedef struct { + unsigned int op : 2; /* always 0 */ + unsigned int rd : 5; + unsigned int op2 : 3; + unsigned int disp : 22; +} sparc_format2a; + +typedef struct { + unsigned int op : 2; /* always 0 */ + unsigned int a : 1; + unsigned int cond : 4; + unsigned int op2 : 3; + unsigned int disp : 22; +} sparc_format2b; + +typedef struct { + unsigned int op : 2; /* always 0 */ + unsigned int a : 1; + unsigned int cond : 4; + unsigned int op2 : 3; + unsigned int cc01 : 2; + unsigned int p : 1; + unsigned int d19 : 19; +} sparc_format2c; + +typedef struct { + unsigned int op : 2; /* always 0 */ + unsigned int a : 1; + unsigned int res : 1; + unsigned int rcond: 3; + unsigned int op2 : 3; + unsigned int d16hi: 2; + unsigned int p : 1; + unsigned int rs1 : 5; + unsigned int d16lo: 14; +} sparc_format2d; + +typedef struct { + unsigned int op : 2; /* 2 or 3 */ + unsigned int rd : 5; + unsigned int op3 : 6; + unsigned int rs1 : 5; + unsigned int i : 1; + unsigned int asi : 8; + unsigned int rs2 : 5; +} sparc_format3a; + +typedef struct { + unsigned int op : 2; /* 2 or 3 */ + unsigned int rd : 5; + unsigned int op3 : 6; + unsigned int rs1 : 5; + unsigned int i : 1; + unsigned int x : 1; + unsigned int asi : 7; + unsigned int rs2 : 5; +} sparc_format3ax; + +typedef struct { + unsigned int op : 2; /* 2 or 3 */ + unsigned int rd : 5; + unsigned int op3 : 6; + unsigned int rs1 : 5; + unsigned int i : 1; + unsigned int imm : 13; +} sparc_format3b; + +typedef struct { + unsigned int op : 2; /* 2 or 3 */ + unsigned int rd : 5; + unsigned int op3 : 6; + unsigned int rs1 : 5; + unsigned int i : 1; + unsigned int x : 1; + unsigned int imm : 12; +} sparc_format3bx; + +typedef struct { + unsigned int op : 2; /* 2 or 3 */ + unsigned int rd : 5; + unsigned int op3 : 6; + unsigned int rs1 : 5; + unsigned int opf : 9; + unsigned int rs2 : 5; +} sparc_format3c; + +typedef struct { + unsigned int op : 2; + unsigned int rd : 5; + unsigned int op3 : 6; + unsigned int rs1 : 5; + unsigned int i : 1; + unsigned int cc01 : 2; + unsigned int res : 6; + unsigned int rs2 : 5; +} sparc_format4a; + +typedef struct { + unsigned int op : 2; + unsigned int rd : 5; + unsigned int op3 : 6; + unsigned int rs1 : 5; + unsigned int i : 1; + unsigned int cc01 : 2; + unsigned int simm : 11; +} sparc_format4b; + +typedef struct { + unsigned int op : 2; + unsigned int rd : 5; + unsigned int op3 : 6; + unsigned int cc2 : 1; + unsigned int cond : 4; + unsigned int i : 1; + unsigned int cc01 : 2; + unsigned int res : 6; + unsigned int rs2 : 5; +} sparc_format4c; + +typedef struct { + unsigned int op : 2; + unsigned int rd : 5; + unsigned int op3 : 6; + unsigned int cc2 : 1; + unsigned int cond : 4; + unsigned int i : 1; + unsigned int cc01 : 2; + unsigned int simm : 11; +} sparc_format4d; + +/* for use in logical ops, use 0 to not set flags */ +#define sparc_cc 16 + +#define sparc_is_imm13(val) ((glong)val >= (glong)-(1<<12) && (glong)val <= (glong)((1<<12)-1)) +#define sparc_is_imm22(val) ((glong)val >= (glong)-(1<<21) && (glong)val <= (glong)((1<<21)-1)) +#define sparc_is_imm16(val) ((glong)val >= (glong)-(1<<15) && (glong)val <= (glong)((1<<15)-1)) +#define sparc_is_imm19(val) ((glong)val >= (glong)-(1<<18) && (glong)val <= (glong)((1<<18)-1)) +#define sparc_is_imm30(val) ((glong)val >= (glong)-(1<<29) && (glong)val <= (glong)((1<<29)-1)) + +/* disassembly */ +#define sparc_inst_op(inst) ((inst) >> 30) +#define sparc_inst_op2(inst) (((inst) >> 22) & 0x7) +#define sparc_inst_rd(inst) (((inst) >> 25) & 0x1f) +#define sparc_inst_op3(inst) (((inst) >> 19) & 0x3f) +#define sparc_inst_i(inst) (((inst) >> 13) & 0x1) +#define sparc_inst_rs1(inst) (((inst) >> 14) & 0x1f) +#define sparc_inst_rs2(inst) (((inst) >> 0) & 0x1f) +#define sparc_inst_imm(inst) (((inst) >> 13) & 0x1) +#define sparc_inst_imm13(inst) (((inst) >> 0) & 0x1fff) + +#define sparc_encode_call(ins,addr) \ + do { \ + sparc_format1 *__f = (sparc_format1*)(ins); \ + __f->op = 1; \ + __f->disp = ((unsigned int)(addr) >> 2); \ + (ins) = (unsigned int*)__f + 1; \ + } while (0) + +#define sparc_encode_format2a(ins,val,oper,dest) \ + do { \ + sparc_format2a *__f = (sparc_format2a*)(ins); \ + __f->op = 0; \ + __f->rd = (dest); \ + __f->op2 = (oper); \ + __f->disp = (val) & 0x3fffff; \ + (ins) = (unsigned int*)__f + 1; \ + } while (0) + +#define sparc_encode_format2b(ins,aval,bcond,oper,disp22) \ + do { \ + sparc_format2b *__f = (sparc_format2b*)(ins); \ + __f->op = 0; \ + __f->a = (aval); \ + __f->cond = (bcond); \ + __f->op2 = (oper); \ + __f->disp = (disp22); \ + (ins) = (unsigned int*)__f + 1; \ + } while (0) + +#define sparc_encode_format2c(ins,aval,bcond,oper,xcc,predict,disp19) \ + do { \ + sparc_format2c *__f = (sparc_format2c*)(ins); \ + __f->op = 0; \ + __f->a = (aval); \ + __f->cond = (bcond); \ + __f->op2 = (oper); \ + __f->cc01 = (xcc); \ + __f->p = (predict); \ + __f->d19 = (disp19); \ + (ins) = (unsigned int*)__f + 1; \ + } while (0) + +#define sparc_encode_format2d(ins,aval,bcond,oper,predict,r1,disp16) \ + do { \ + sparc_format2d *__f = (sparc_format2d*)(ins); \ + __f->op = 0; \ + __f->a = (aval); \ + __f->res = 0; \ + __f->rcond = (bcond); \ + __f->op2 = (oper); \ + __f->d16hi = ((disp16) >> 14); \ + __f->p = (predict); \ + __f->rs1 = (r1); \ + __f->d16lo = ((disp16) & 0x3fff); \ + (ins) = (unsigned int*)__f + 1; \ + } while (0) + +#define sparc_encode_format3a(ins,opval,asival,r1,r2,oper,dest) \ + do { \ + sparc_format3a *__f = (sparc_format3a*)(ins); \ + __f->op = (opval); \ + __f->asi = (asival); \ + __f->i = 0; \ + __f->rd = (dest); \ + __f->rs1 = (r1); \ + __f->rs2 = (r2); \ + __f->op3 = (oper); \ + (ins) = (unsigned int*)__f + 1; \ + } while (0) + +#define sparc_encode_format3ax(ins,opval,asival,r1,r2,oper,dest) \ + do { \ + sparc_format3ax *__f = (sparc_format3ax*)(ins); \ + __f->op = (opval); \ + __f->asi = (asival); \ + __f->i = 0; \ + __f->x = 1; \ + __f->rd = (dest); \ + __f->rs1 = (r1); \ + __f->rs2 = (r2); \ + __f->op3 = (oper); \ + (ins) = (unsigned int*)__f + 1; \ + } while (0) + +#define sparc_encode_format3b(ins,opval,r1,val,oper,dest) \ + do { \ + sparc_format3b *__f = (sparc_format3b*)(ins); \ + __f->op = (opval); \ + __f->imm = (val); \ + __f->i = 1; \ + __f->rd = (dest); \ + __f->rs1 = (r1); \ + __f->op3 = (oper); \ + (ins) = (unsigned int*)__f + 1; \ + } while (0) + +#define sparc_encode_format3bx(ins,opval,r1,val,oper,dest) \ + do { \ + sparc_format3bx *__f = (sparc_format3bx*)(ins); \ + __f->op = (opval); \ + __f->imm = (val); \ + __f->i = 1; \ + __f->x = 1; \ + __f->rd = (dest); \ + __f->rs1 = (r1); \ + __f->op3 = (oper); \ + (ins) = (unsigned int*)__f + 1; \ + } while (0) + +#define sparc_encode_format3c(ins,opval,opfval,r1,oper,r2,dest) \ + do { \ + sparc_format3c *__f = (sparc_format3c*)(ins); \ + __f->op = (opval); \ + __f->opf = (opfval); \ + __f->rd = (dest); \ + __f->rs1 = (r1); \ + __f->rs2 = (r2); \ + __f->op3 = (oper); \ + (ins) = (unsigned int*)__f + 1; \ + } while (0) + +#define sparc_encode_format4a(ins,opval,oper,cc,r1,r2,dest) \ + do { \ + sparc_format4a *__f = (sparc_format4a*)(ins); \ + __f->op = (opval); \ + __f->rd = (dest); \ + __f->op3 = (oper); \ + __f->rs1 = (r1); \ + __f->i = 0; \ + __f->cc01= (cc) & 0x3; \ + __f->res = 0; \ + __f->rs2 = (r2); \ + (ins) = (unsigned int*)__f + 1; \ + } while (0) + +#define sparc_encode_format4b(ins,opval,oper,cc,r1,imm,dest) \ + do { \ + sparc_format4b *__f = (sparc_format4b*)(ins); \ + __f->op = (opval); \ + __f->rd = (dest); \ + __f->op3 = (oper); \ + __f->rs1 = (r1); \ + __f->i = 1; \ + __f->cc01= (cc) & 0x3; \ + __f->simm = (imm); \ + (ins) = (unsigned int*)__f + 1; \ + } while (0) + +#define sparc_encode_format4c(ins,opval,oper,cc,bcond,r2,dest) \ + do { \ + sparc_format4c *__f = (sparc_format4c*)(ins); \ + __f->op = (opval); \ + __f->rd = (dest); \ + __f->op3 = (oper); \ + __f->cc2 = ((xcc) >> 2) & 0x1; \ + __f->cond = bcond; \ + __f->i = 0; \ + __f->cc01= (xcc) & 0x3; \ + __f->res = 0; \ + __f->rs2 = (r2); \ + (ins) = (unsigned int*)__f + 1; \ + } while (0) + +#define sparc_encode_format4d(ins,opval,oper,xcc,bcond,imm,dest) \ + do { \ + sparc_format4d *__f = (sparc_format4d*)(ins); \ + __f->op = (opval); \ + __f->rd = (dest); \ + __f->op3 = (oper); \ + __f->cc2 = ((xcc) >> 2) & 0x1; \ + __f->cond = bcond; \ + __f->i = 1; \ + __f->cc01= (xcc) & 0x3; \ + __f->simm = (imm); \ + (ins) = (unsigned int*)__f + 1; \ + } while (0) + +/* is it useful to provide a non-default value? */ +#define sparc_asi 0x0 + +/* load */ +#define sparc_ldsb(ins,base,disp,dest) sparc_encode_format3a((ins),3,sparc_asi,(base),(disp),9,(dest)) +#define sparc_ldsb_imm(ins,base,disp,dest) sparc_encode_format3b((ins),3,(base),(disp),9,(dest)) + +#define sparc_ldsh(ins,base,disp,dest) sparc_encode_format3a((ins),3,sparc_asi,(base),(disp),10,(dest)) +#define sparc_ldsh_imm(ins,base,disp,dest) sparc_encode_format3b((ins),3,(base),(disp),10,(dest)) + +#define sparc_ldub(ins,base,disp,dest) sparc_encode_format3a((ins),3,sparc_asi,(base),(disp),1,(dest)) +#define sparc_ldub_imm(ins,base,disp,dest) sparc_encode_format3b((ins),3,(base),(disp),1,(dest)) + +#define sparc_lduh(ins,base,disp,dest) sparc_encode_format3a((ins),3,sparc_asi,(base),(disp),2,(dest)) +#define sparc_lduh_imm(ins,base,disp,dest) sparc_encode_format3b((ins),3,(base),(disp),2,(dest)) + +#define sparc_ld(ins,base,disp,dest) sparc_encode_format3a((ins),3,sparc_asi,(base),(disp),0,(dest)) +#define sparc_ld_imm(ins,base,disp,dest) sparc_encode_format3b((ins),3,(base),(disp),0,(dest)) + +/* Sparc V9 */ +#define sparc_ldx(ins,base,disp,dest) sparc_encode_format3a((ins),3,sparc_asi,(base),(disp),11,(dest)) +#define sparc_ldx_imm(ins,base,disp,dest) sparc_encode_format3b((ins),3,(base),(disp),11,(dest)) + +#define sparc_ldsw(ins,base,disp,dest) sparc_encode_format3a((ins),3,sparc_asi,(base),(disp),8,(dest)) +#define sparc_ldsw_imm(ins,base,disp,dest) sparc_encode_format3b((ins),3,(base),(disp),8,(dest)) + +#define sparc_ldd(ins,base,disp,dest) sparc_encode_format3a((ins),3,sparc_asi,(base),(disp),3,(dest)) +#define sparc_ldd_imm(ins,base,disp,dest) sparc_encode_format3b((ins),3,(base),(disp),3,(dest)) + +#define sparc_ldf(ins,base,disp,dest) sparc_encode_format3a((ins),3,0,(base),(disp),32,(dest)) +#define sparc_ldf_imm(ins,base,disp,dest) sparc_encode_format3b((ins),3,(base),(disp),32,(dest)) + +#define sparc_lddf(ins,base,disp,dest) sparc_encode_format3a((ins),3,0,(base),(disp),35,(dest)) +#define sparc_lddf_imm(ins,base,disp,dest) sparc_encode_format3b((ins),3,(base),(disp),35,(dest)) + +/* store */ +#define sparc_stb(ins,src,base,disp) sparc_encode_format3a((ins),3,sparc_asi,(base),(disp),5,(src)) +#define sparc_stb_imm(ins,src,base,disp) sparc_encode_format3b((ins),3,(base),(disp),5,(src)) + +#define sparc_sth(ins,src,base,disp) sparc_encode_format3a((ins),3,sparc_asi,(base),(disp),6,(src)) +#define sparc_sth_imm(ins,src,base,disp) sparc_encode_format3b((ins),3,(base),(disp),6,(src)) + +#define sparc_st(ins,src,base,disp) sparc_encode_format3a((ins),3,sparc_asi,(base),(disp),4,(src)) +#define sparc_st_imm(ins,src,base,disp) sparc_encode_format3b((ins),3,(base),(disp),4,(src)) + +/* Sparc V9 */ +#define sparc_stx(ins,src,base,disp) sparc_encode_format3a((ins),3,sparc_asi,(base),(disp),14,(src)) +#define sparc_stx_imm(ins,src,base,disp) sparc_encode_format3b((ins),3,(base),(disp),14,(src)) + +#define sparc_std(ins,src,base,disp) sparc_encode_format3a((ins),3,sparc_asi,(base),(disp),7,(src)) +#define sparc_std_imm(ins,src,base,disp) sparc_encode_format3b((ins),3,(base),(disp),7,(src)) + +#define sparc_stf(ins,src,base,disp) sparc_encode_format3a((ins),3,sparc_asi,(base),(disp),36,(src)) +#define sparc_stf_imm(ins,src,base,disp) sparc_encode_format3b((ins),3,(base),(disp),36,(src)) + +#define sparc_stdf(ins,src,base,disp) sparc_encode_format3a((ins),3,sparc_asi,(base),(disp),39,(src)) +#define sparc_stdf_imm(ins,src,base,disp) sparc_encode_format3b((ins),3,(base),(disp),39,(src)) + +/* swap */ +#define sparc_ldstub(ins,base,disp,dest) sparc_encode_format3a((ins),3,sparc_asi,(base),(disp),13,(dest)) +#define sparc_ldstub_imm(ins,base,disp,dest) sparc_encode_format3b((ins),3,(base),(disp),13,(dest)) + +#define sparc_swap(ins,base,disp,dest) sparc_encode_format3a((ins),3,sparc_asi,(base),(disp),15,(dest)) +#define sparc_swap_imm(ins,base,disp,dest) sparc_encode_format3b((ins),3,(base),(disp),15,(dest)) + +/* misc */ +/* note: with sethi val is the full 32 bit value (think of it as %hi(val)) */ +#define sparc_sethi(ins,val,dest) sparc_encode_format2a((ins),((val)>>10),4,(dest)) + +#define sparc_nop(ins) sparc_sethi((ins),0,sparc_zero) + +#define sparc_save(ins,src,disp,dest) sparc_encode_format3a((ins),2,0,(src),(disp),60,(dest)) +#define sparc_save_imm(ins,src,disp,dest) sparc_encode_format3b((ins),2,(src),(disp),60,(dest)) + +#define sparc_restore(ins,src,disp,dest) sparc_encode_format3a((ins),2,0,(src),(disp),61,(dest)) +#define sparc_restore_imm(ins,src,disp,dest) sparc_encode_format3b((ins),2,(src),(disp),61,(dest)) + +#define sparc_rett(ins,src,disp) sparc_encode_format3a((ins),2,0,(src),(disp),0x39,0) +#define sparc_rett_imm(ins,src,disp) sparc_encode_format3b((ins),2,(src),(disp),0x39,0) + +#define sparc_jmpl(ins,base,disp,dest) sparc_encode_format3a((ins),2,0,(base),(disp),56,(dest)) +#define sparc_jmpl_imm(ins,base,disp,dest) sparc_encode_format3b((ins),2,(base),(disp),56,(dest)) + +#define sparc_call_simple(ins,disp) sparc_encode_call((ins),((unsigned int)(disp))) + +#define sparc_rdy(ins,dest) sparc_encode_format3a((ins),2,0,0,0,40,(dest)) + +#define sparc_wry(ins,base,disp) sparc_encode_format3a((ins),2,0,(base),(disp),48,0) +#define sparc_wry_imm(ins,base,disp) sparc_encode_format3b((ins),2,(base),(disp),48,0) + +/* stbar, unimp, flush */ +#define sparc_stbar(ins) sparc_encode_format3a((ins),2,0,15,0,40,0) +#define sparc_unimp(ins,val) sparc_encode_format2b((ins),0,0,0,(val)) + +#define sparc_flush(ins,base,disp) sparc_encode_format3a((ins),2,0,(base),(disp),59,0) +#define sparc_flush_imm(ins,base,disp) sparc_encode_format3b((ins),2,(base),(disp),59,0) + +#define sparc_flushw(ins) sparc_encode_format3a((ins),2,0,0,0,43,0) + +#define sparc_membar(ins,flags) sparc_encode_format3b ((ins), 2, 0xf, (flags), 0x28, 0) + +/* trap */ + +#define sparc_ta(ins,tt) sparc_encode_format3b((ins),2,0,(tt),58,0x8) + +/* alu fop */ +/* provide wrappers for: fitos, fitod, fstoi, fdtoi, fstod, fdtos, fmov, fneg, fabs */ + +#define sparc_fop(ins,r1,op,r2,dest) sparc_encode_format3c((ins),2,(op),(r1),52,(r2),(dest)) +#define sparc_fcmp(ins,r1,op,r2) sparc_encode_format3c((ins),2,(op),(r1),53,(r2),0) + +/* format 1 fops */ +#define sparc_fadds(ins, r1, r2, dest) sparc_fop( ins, r1, sparc_fadds_val, r2, dest ) +#define sparc_faddd(ins, r1, r2, dest) sparc_fop( ins, r1, sparc_faddd_val, r2, dest ) +#define sparc_faddq(ins, r1, r2, dest) sparc_fop( ins, r1, sparc_faddq_val, r2, dest ) + +#define sparc_fsubs(ins, r1, r2, dest) sparc_fop( ins, r1, sparc_fsubs_val, r2, dest ) +#define sparc_fsubd(ins, r1, r2, dest) sparc_fop( ins, r1, sparc_fsubd_val, r2, dest ) +#define sparc_fsubq(ins, r1, r2, dest) sparc_fop( ins, r1, sparc_fsubq_val, r2, dest ) + +#define sparc_fmuls( ins, r1, r2, dest ) sparc_fop( ins, r1, sparc_fmuls_val, r2, dest ) +#define sparc_fmuld( ins, r1, r2, dest ) sparc_fop( ins, r1, sparc_fmuld_val, r2, dest ) +#define sparc_fmulq( ins, r1, r2, dest ) sparc_fop( ins, r1, sparc_fmulq_val, r2, dest ) + +#define sparc_fsmuld( ins, r1, r2, dest ) sparc_fop( ins, r1, sparc_fsmuld_val, r2, dest ) +#define sparc_fdmulq( ins, r1, r2, dest ) sparc_fop( ins, r1, sparc_fdmulq_val, r2, dest ) + +#define sparc_fdivs( ins, r1, r2, dest ) sparc_fop( ins, r1, sparc_fdivs_val, r2, dest ) +#define sparc_fdivd( ins, r1, r2, dest ) sparc_fop( ins, r1, sparc_fdivd_val, r2, dest ) +#define sparc_fdivq( ins, r1, r2, dest ) sparc_fop( ins, r1, sparc_fdivq_val, r2, dest ) + +#define sparc_fitos( ins, r2, dest ) sparc_fop( ins, 0, sparc_fitos_val, r2, dest ) +#define sparc_fitod( ins, r2, dest ) sparc_fop( ins, 0, sparc_fitod_val, r2, dest ) +#define sparc_fitoq( ins, r2, dest ) sparc_fop( ins, 0, sparc_fitoq_val, r2, dest ) + +#define sparc_fxtos( ins, r2, dest) sparc_fop( ins, 0, sparc_fxtos_val, r2, dest ) +#define sparc_fxtod( ins, r2, dest) sparc_fop( ins, 0, sparc_fxtod_val, r2, dest ) +#define sparc_fxtoq( ins, r2, dest) sparc_fop( ins, 0, sparc_fxtoq_val, r2, dest ) + +#define sparc_fstoi( ins, r2, dest ) sparc_fop( ins, 0, sparc_fstoi_val, r2, dest ) +#define sparc_fdtoi( ins, r2, dest ) sparc_fop( ins, 0, sparc_fdtoi_val, r2, dest ) +#define sparc_fqtoi( ins, r2, dest ) sparc_fop( ins, 0, sparc_fqtoi_val, r2, dest ) + +#define sparc_fstod( ins, r2, dest ) sparc_fop( ins, 0, sparc_fstod_val, r2, dest ) +#define sparc_fstoq( ins, r2, dest ) sparc_fop( ins, 0, sparc_fstoq_val, r2, dest ) + +#define sparc_fdtos( ins, r2, dest ) sparc_fop( ins, 0, sparc_fdtos_val, r2, dest ) +#define sparc_fdtoq( ins, r2, dest ) sparc_fop( ins, 0, sparc_fdtoq_val, r2, dest ) + +#define sparc_fqtos( ins, r2, dest ) sparc_fop( ins, 0, sparc_fqtos_val, r2, dest ) +#define sparc_fqtod( ins, r2, dest ) sparc_fop( ins, 0, sparc_fqtod_val, r2, dest ) + +#define sparc_fmovs( ins, r2, dest ) sparc_fop( ins, 0, sparc_fmovs_val, r2, dest ) +#define sparc_fnegs( ins, r2, dest ) sparc_fop( ins, 0, sparc_fnegs_val, r2, dest ) +#define sparc_fabss( ins, r2, dest ) sparc_fop( ins, 0, sparc_fabss_val, r2, dest ) + +#define sparc_fmovd( ins, r2, dest) sparc_fop (ins, 0, sparc_fmovd_val, r2, dest); +#define sparc_fnegd( ins, r2, dest) sparc_fop (ins, 0, sparc_fnegd_val, r2, dest); +#define sparc_fabsd( ins, r2, dest) sparc_fop (ins, 0, sparc_fabsd_val, r2, dest); + +#define sparc_fsqrts( ins, r2, dest ) sparc_fop( ins, 0, sparc_fsqrts_val, r2, dest ) +#define sparc_fsqrtd( ins, r2, dest ) sparc_fop( ins, 0, sparc_fsqrtd_val, r2, dest ) +#define sparc_fsqrtq( ins, r2, dest ) sparc_fop( ins, 0, sparc_fsqrtq_val, r2, dest ) + +/* format 2 fops */ + +#define sparc_fcmps( ins, r1, r2 ) sparc_fcmp( ins, r1, sparc_fcmps_val, r2 ) +#define sparc_fcmpd( ins, r1, r2 ) sparc_fcmp( ins, r1, sparc_fcmpd_val, r2 ) +#define sparc_fcmpq( ins, r1, r2 ) sparc_fcmp( ins, r1, sparc_fcmpq_val, r2 ) +#define sparc_fcmpes( ins, r1, r2 ) sparc_fcmpes( ins, r1, sparc_fcmpes_val, r2 ) +#define sparc_fcmped( ins, r1, r2 ) sparc_fcmped( ins, r1, sparc_fcmped_val, r2 ) +#define sparc_fcmpeq( ins, r1, r2 ) sparc_fcmpeq( ins, r1, sparc_fcmpeq_val, r2 ) + +/* logical */ + +/* FIXME: condense this using macros */ +/* FIXME: the setcc stuff is wrong in lots of places */ + +#define sparc_logic(ins,op,setcc,r1,r2,dest) sparc_encode_format3a((ins),2,0,(r1),(r2),((setcc) ? 0x10 : 0) | (op), (dest)) +#define sparc_logic_imm(ins,op,setcc,r1,imm,dest) sparc_encode_format3b((ins),2,(r1),(imm),((setcc) ? 0x10 : 0) | (op), (dest)) + +#define sparc_and(ins,setcc,r1,r2,dest) sparc_logic(ins,1,setcc,r1,r2,dest) +#define sparc_and_imm(ins,setcc,r1,imm,dest) sparc_logic_imm(ins,1,setcc,r1,imm,dest) + +#define sparc_andn(ins,setcc,r1,r2,dest) sparc_encode_format3a((ins),2,0,(r1),(r2),(setcc)|5,(dest)) +#define sparc_andn_imm(ins,setcc,r1,imm,dest) sparc_encode_format3b((ins),2,(r1),(imm),(setcc)|5,(dest)) + +#define sparc_or(ins,setcc,r1,r2,dest) sparc_encode_format3a((ins),2,0,(r1),(r2),(setcc)|2,(dest)) +#define sparc_or_imm(ins,setcc,r1,imm,dest) sparc_encode_format3b((ins),2,(r1),(imm),(setcc)|2,(dest)) + +#define sparc_orn(ins,setcc,r1,r2,dest) sparc_encode_format3a((ins),2,0,(r1),(r2),(setcc)|6,(dest)) +#define sparc_orn_imm(ins,setcc,r1,imm,dest) sparc_encode_format3b((ins),2,(r1),(imm),(setcc)|6,(dest)) + +#define sparc_xor(ins,setcc,r1,r2,dest) sparc_encode_format3a((ins),2,0,(r1),(r2),(setcc)|3,(dest)) +#define sparc_xor_imm(ins,setcc,r1,imm,dest) sparc_encode_format3b((ins),2,(r1),(imm), (setcc)|3,(dest)) + +#define sparc_xnor(ins,setcc,r1,r2,dest) sparc_encode_format3a((ins),2,0,(r1),(r2),(setcc)|7,(dest)) +#define sparc_xnor_imm(ins,setcc,r1,imm,dest) sparc_encode_format3b((ins),2,(r1),(imm),(setcc)|7,(dest)) + +/* shift */ +#define sparc_sll(ins,src,disp,dest) sparc_encode_format3a((ins),2,0,(src),(disp),37,(dest)) +#define sparc_sll_imm(ins,src,disp,dest) sparc_encode_format3b((ins),2,(src),(disp),37,(dest)) + +/* Sparc V9 */ +#define sparc_sllx(ins,src,disp,dest) sparc_encode_format3ax((ins),2,0,(src),(disp),37,(dest)) +#define sparc_sllx_imm(ins,src,disp,dest) sparc_encode_format3bx((ins),2,(src),(disp),37,(dest)) + +#define sparc_srl(ins,src,disp,dest) sparc_encode_format3a((ins),2,0,(src),(disp),38,(dest)) +#define sparc_srl_imm(ins,src,disp,dest) sparc_encode_format3b((ins),2,(src),(disp),38,(dest)) + +/* Sparc V9 */ +#define sparc_srlx(ins,src,disp,dest) sparc_encode_format3ax((ins),2,0,(src),(disp),38,(dest)) +#define sparc_srlx_imm(ins,src,disp,dest) sparc_encode_format3bx((ins),2,(src),(disp),38,(dest)) + +#define sparc_sra(ins,src,disp,dest) sparc_encode_format3a((ins),2,0,(src),(disp),39,(dest)) +#define sparc_sra_imm(ins,src,disp,dest) sparc_encode_format3b((ins),2,(src),(disp),39,(dest)) + +/* Sparc V9 */ +#define sparc_srax(ins,src,disp,dest) sparc_encode_format3ax((ins),2,0,(src),(disp),39,(dest)) +#define sparc_srax_imm(ins,src,disp,dest) sparc_encode_format3bx((ins),2,(src),(disp),39,(dest)) + +/* alu */ + +#define sparc_alu_reg(ins,op,setcc,r1,r2,dest) sparc_encode_format3a((ins),2,0,(r1),(r2),op|((setcc) ? 0x10 : 0),(dest)) +#define sparc_alu_imm(ins,op,setcc,r1,imm,dest) sparc_encode_format3b((ins),2,(r1),(imm),op|((setcc) ? 0x10 : 0),(dest)) + +#define sparc_add(ins,setcc,r1,r2,dest) sparc_alu_reg((ins),0,(setcc),(r1),(r2),(dest)) +#define sparc_add_imm(ins,setcc,r1,imm,dest) sparc_alu_imm((ins),0,(setcc),(r1),(imm),(dest)) + +#define sparc_addx(ins,setcc,r1,r2,dest) sparc_alu_reg((ins),0x8,(setcc),(r1),(r2),(dest)) +#define sparc_addx_imm(ins,setcc,r1,imm,dest) sparc_alu_imm((ins),0x8,(setcc),(r1),(imm),(dest)) + +#define sparc_sub(ins,setcc,r1,r2,dest) sparc_alu_reg((ins),0x4,(setcc),(r1),(r2),(dest)) +#define sparc_sub_imm(ins,setcc,r1,imm,dest) sparc_alu_imm((ins),0x4,(setcc),(r1),(imm),(dest)) + +#define sparc_subx(ins,setcc,r1,r2,dest) sparc_alu_reg((ins),0xc,(setcc),(r1),(r2),(dest)) +#define sparc_subx_imm(ins,setcc,r1,imm,dest) sparc_alu_imm((ins),0xc,(setcc),(r1),(imm),(dest)) + +#define sparc_muls(ins,r1,r2,dest) sparc_encode_format3a((ins),2,0,(r1),(r2),36,(dest)) +#define sparc_muls_imm(ins,r1,imm,dest) sparc_encode_format3b((ins),2,(r1),(imm),36,(dest)) + +#define sparc_umul(ins,setcc,r1,r2,dest) sparc_alu_reg((ins),0xa,(setcc),(r1),(r2),(dest)) +#define sparc_umul_imm(ins,setcc,r1,imm,dest) sparc_alu_imm((ins),0xa,(setcc),(r1),(imm),(dest)) + +#define sparc_smul(ins,setcc,r1,r2,dest) sparc_alu_reg((ins),0xb,(setcc),(r1),(r2),(dest)) +#define sparc_smul_imm(ins,setcc,r1,imm,dest) sparc_alu_imm((ins),0xb,(setcc),(r1),(imm),(dest)) + +#define sparc_udiv(ins,setcc,r1,r2,dest) sparc_alu_reg((ins),0xe,(setcc),(r1),(r2),(dest)) +#define sparc_udiv_imm(ins,setcc,r1,imm,dest) sparc_alu_imm((ins),0xe,(setcc),(r1),(imm),(dest)) + +#define sparc_sdiv(ins,setcc,r1,r2,dest) sparc_alu_reg((ins),0xf,(setcc),(r1),(r2),(dest)) +#define sparc_sdiv_imm(ins,setcc,r1,imm,dest) sparc_alu_imm((ins),0xf,(setcc),(r1),(imm),(dest)) + + +/* branch */ +#define sparc_branch(ins,aval,condval,displ) sparc_encode_format2b((ins),(aval),(condval),2,(displ)) +/* FIXME: float condition codes are different: unify. */ +#define sparc_fbranch(ins,aval,condval,displ) sparc_encode_format2b((ins),(aval),(condval),6,(displ)) +#define sparc_branchp(ins,aval,condval,xcc,predict,displ) sparc_encode_format2c((ins),(aval),(condval),0x1,(xcc),(predict),(displ)) + +#define sparc_brz(ins,aval,predict,rs1,disp) sparc_encode_format2d((ins), (aval),0x1,0x3,(predict),(rs1),(disp)) +#define sparc_brlez(ins,aval,predict,rs1,disp) sparc_encode_format2d((ins), (aval),0x2,0x3,(predict),(rs1),(disp)) +#define sparc_brlz(ins,aval,predict,rs1,disp) sparc_encode_format2d((ins), (aval),0x3,0x3,(predict),(rs1),(disp)) +#define sparc_brnz(ins,aval,predict,rs1,disp) sparc_encode_format2d((ins), (aval),0x5,0x3,(predict),(rs1),(disp)) +#define sparc_brgz(ins,aval,predict,rs1,disp) sparc_encode_format2d((ins), (aval),0x6,0x3,(predict),(rs1),(disp)) +#define sparc_brgez(ins,aval,predict,rs1,disp) sparc_encode_format2d((ins), (aval),0x7,0x3,(predict),(rs1),(disp)) + +/* conditional moves */ +#define sparc_movcc(ins,cc,condval,r1,dest) sparc_encode_format4c((ins), 0x2, 0x2c, cc, condval, r1, dest) + +#define sparc_movcc_imm(ins,cc,condval,imm,dest) sparc_encode_format4d((ins), 0x2, 0x2c, cc, condval, imm, dest) + +/* synthetic instructions */ +#define sparc_cmp(ins,r1,r2) sparc_sub((ins),sparc_cc,(r1),(r2),sparc_g0) +#define sparc_cmp_imm(ins,r1,imm) sparc_sub_imm((ins),sparc_cc,(r1),(imm),sparc_g0) +#define sparc_jmp(ins,base,disp) sparc_jmpl((ins),(base),(disp),sparc_g0) +#define sparc_jmp_imm(ins,base,disp) sparc_jmpl_imm((ins),(base),(disp),sparc_g0) +#define sparc_call(ins,base,disp) sparc_jmpl((ins),(base),(disp),sparc_o7) +#define sparc_call_imm(ins,base,disp) sparc_jmpl_imm((ins),(base),(disp),sparc_o7) + +#define sparc_test(ins,reg) sparc_or ((ins),sparc_cc,sparc_g0,(reg),sparc_g0) + +#define sparc_ret(ins) sparc_jmpl_imm((ins),sparc_i7,8,sparc_g0) +#define sparc_retl(ins) sparc_jmpl_imm((ins),sparc_o7,8,sparc_g0) +#define sparc_restore_simple(ins) sparc_restore((ins),sparc_g0,sparc_g0,sparc_g0) +#define sparc_rett_simple(ins) sparc_rett_imm((ins),sparc_i7,8) + +#define sparc_set32(ins,val,reg) \ + do { \ + if ((val) == 0) \ + sparc_clr_reg((ins),(reg)); \ + else if (((guint32)(val) & 0x3ff) == 0) \ + sparc_sethi((ins),(guint32)(val),(reg)); \ + else if (((gint32)(val) >= -4096) && ((gint32)(val) <= 4095)) \ + sparc_or_imm((ins),FALSE,sparc_g0,(gint32)(val),(reg)); \ + else { \ + sparc_sethi((ins),(guint32)(val),(reg)); \ + sparc_or_imm((ins),FALSE,(reg),(guint32)(val)&0x3ff,(reg)); \ + } \ + } while (0) + +#ifdef SPARCV9 +#define SPARC_SET_MAX_SIZE (6 * 4) +#else +#define SPARC_SET_MAX_SIZE (2 * 4) +#endif + +#if SPARCV9 +#define sparc_set(ins,ptr,reg) \ + do { \ + g_assert ((reg) != sparc_g1); \ + gint64 val = (gint64)ptr; \ + guint32 top_word = (val) >> 32; \ + guint32 bottom_word = (val) & 0xffffffff; \ + if (val == 0) \ + sparc_clr_reg ((ins), reg); \ + else if ((val >= -4096) && ((val) <= 4095)) \ + sparc_or_imm((ins),FALSE,sparc_g0,bottom_word,(reg)); \ + else if ((val >= 0) && (val <= 4294967295L)) { \ + sparc_sethi((ins),bottom_word,(reg)); \ + if (bottom_word & 0x3ff) \ + sparc_or_imm((ins),FALSE,(reg),bottom_word&0x3ff,(reg)); \ + } \ + else if ((val >= 0) && (val <= (1L << 44) - 1)) { \ + sparc_sethi ((ins), (val >> 12), (reg)); \ + sparc_or_imm ((ins), FALSE, (reg), (val >> 12) & 0x3ff, (reg)); \ + sparc_sllx_imm ((ins),(reg), 12, (reg)); \ + sparc_or_imm ((ins), FALSE, (reg), (val) & 0xfff, (reg)); \ + } \ + else if (top_word == 0xffffffff) { \ + sparc_xnor ((ins), FALSE, sparc_g0, sparc_g0, sparc_g1); \ + sparc_sethi((ins),bottom_word,(reg)); \ + sparc_sllx_imm((ins),sparc_g1,32,sparc_g1); \ + sparc_or_imm((ins),FALSE,(reg),bottom_word&0x3ff,(reg)); \ + sparc_or((ins),FALSE,(reg),sparc_g1,(reg)); \ + } \ + else { \ + sparc_sethi((ins),top_word,sparc_g1); \ + sparc_sethi((ins),bottom_word,(reg)); \ + sparc_or_imm((ins),FALSE,sparc_g1,top_word&0x3ff,sparc_g1); \ + sparc_or_imm((ins),FALSE,(reg),bottom_word&0x3ff,(reg)); \ + sparc_sllx_imm((ins),sparc_g1,32,sparc_g1); \ + sparc_or((ins),FALSE,(reg),sparc_g1,(reg)); \ + } \ + } while (0) +#else +#define sparc_set(ins,val,reg) \ + do { \ + if ((val) == 0) \ + sparc_clr_reg((ins),(reg)); \ + else if (((guint32)(val) & 0x3ff) == 0) \ + sparc_sethi((ins),(guint32)(val),(reg)); \ + else if (((gint32)(val) >= -4096) && ((gint32)(val) <= 4095)) \ + sparc_or_imm((ins),FALSE,sparc_g0,(gint32)(val),(reg)); \ + else { \ + sparc_sethi((ins),(guint32)(val),(reg)); \ + sparc_or_imm((ins),FALSE,(reg),(guint32)(val)&0x3ff,(reg)); \ + } \ + } while (0) +#endif + +#define sparc_set_ptr(ins,val,reg) sparc_set(ins,val,reg) + +#ifdef SPARCV9 +#define sparc_set_template(ins,reg) sparc_set (ins,0x7fffffff7fffffff, reg) +#else +#define sparc_set_template(ins,reg) sparc_set (ins,0x7fffffff, reg) +#endif + +#define sparc_not(ins,reg) sparc_xnor((ins),FALSE,(reg),sparc_g0,(reg)) +#define sparc_neg(ins,reg) sparc_sub((ins),FALSE,sparc_g0,(reg),(reg)) +#define sparc_clr_reg(ins,reg) sparc_or((ins),FALSE,sparc_g0,sparc_g0,(reg)) + +#define sparc_mov_reg_reg(ins,src,dest) sparc_or((ins),FALSE,sparc_g0,(src),(dest)) + +#ifdef SPARCV9 +#define sparc_sti_imm sparc_stx_imm +#define sparc_ldi_imm sparc_ldx_imm +#define sparc_sti sparc_stx +#define sparc_ldi sparc_ldx +#else +#define sparc_sti_imm sparc_st_imm +#define sparc_ldi_imm sparc_ld_imm +#define sparc_sti sparc_st +#define sparc_ldi sparc_ld +#endif + +#endif /* __SPARC_CODEGEN_H__ */ + diff --git a/src/arch/sparc/test.c b/src/arch/sparc/test.c new file mode 100644 index 0000000..0d4ad18 --- /dev/null +++ b/src/arch/sparc/test.c @@ -0,0 +1,123 @@ +#include <glib.h> +#include "sparc-codegen.h" + +/* don't run the resulting program, it will destroy your computer, + * just objdump -d it to inspect we generated the correct assembler. + */ + +int +main () +{ + guint32 *p; + guint32 code_buffer [500]; + guint32 local_size = 0, stack_size = 0, code_size = 6; + guint32 arg_pos, simpletype; + unsigned char *ins; + int i, stringp, cur_out_reg, size; + + p = code_buffer; + + printf (".text\n.align 4\n.globl main\n.type main,@function\nmain:\n"); + + /* + * Standard function prolog. + */ + sparc_save_imm (p, sparc_sp, -112-stack_size, sparc_sp); + cur_out_reg = sparc_o0; + arg_pos = 0; + + if (1) { + sparc_mov_reg_reg (p, sparc_i2, cur_out_reg); + ++cur_out_reg; + } + + sparc_ld_imm (p, sparc_i3, arg_pos, cur_out_reg); + ++cur_out_reg; + sparc_ld_imm (p, sparc_i3, arg_pos+4, cur_out_reg); + ++cur_out_reg; + /* + * Insert call to function + */ + sparc_jmpl (p, sparc_i0, 0, sparc_callsite); + sparc_nop (p); + + sparc_jmpl_imm (p, sparc_i7, 8, sparc_zero); + sparc_restore (p, sparc_zero, sparc_zero, sparc_zero); + + sparc_ldsb (p, sparc_i3, sparc_l0, sparc_o5); + sparc_ldsb_imm (p, sparc_i3, 2, sparc_o5); + + sparc_ldsh (p, sparc_i3, sparc_l0, sparc_o5); + sparc_ldsh_imm (p, sparc_i3, 2, sparc_o5); + + sparc_ldub (p, sparc_i3, sparc_l0, sparc_o5); + sparc_ldub_imm (p, sparc_i3, 2, sparc_o5); + + sparc_lduh (p, sparc_i3, sparc_l0, sparc_o5); + sparc_lduh_imm (p, sparc_i3, 2, sparc_o5); + + sparc_ldf (p, sparc_i3, sparc_l0, sparc_o5); + sparc_ldf_imm (p, sparc_i3, 2, sparc_o5); + + sparc_stb (p, sparc_i3, sparc_l0, sparc_l2); + sparc_stb_imm (p, sparc_i3, sparc_o5, 2); + + sparc_sethi (p, 0xff000000, sparc_o2); + sparc_rdy (p, sparc_l0); + sparc_wry (p, sparc_l0, sparc_l1); + sparc_wry_imm (p, sparc_l0, 16); + sparc_stbar (p); + sparc_unimp (p, 24); + sparc_flush (p, sparc_l4, 0); + + sparc_and (p, sparc_cc, sparc_l0, sparc_l1, sparc_o1); + sparc_and_imm (p, FALSE, sparc_l0, 0xff, sparc_o1); + sparc_andn (p, sparc_cc, sparc_l0, sparc_l1, sparc_o1); + sparc_or (p, sparc_cc, sparc_l0, sparc_l1, sparc_o1); + sparc_orn (p, sparc_cc, sparc_l0, sparc_l1, sparc_o1); + sparc_xor (p, sparc_cc, sparc_l0, sparc_l1, sparc_o1); + sparc_xnor (p, sparc_cc, sparc_l0, sparc_l1, sparc_o1); + + sparc_sll (p, sparc_l0, sparc_l1, sparc_o1); + sparc_sll_imm (p, sparc_l0, 2, sparc_o1); + sparc_srl (p, sparc_l0, sparc_l1, sparc_o1); + sparc_srl_imm (p, sparc_l0, 2, sparc_o1); + sparc_sra (p, sparc_l0, sparc_l1, sparc_o1); + sparc_sra_imm (p, sparc_l0, 2, sparc_o1); + + sparc_add (p, sparc_cc, sparc_l0, sparc_l1, sparc_o1); + sparc_add_imm (p, FALSE, sparc_l0, 0xff, sparc_o1); + sparc_addx (p, sparc_cc, sparc_l0, sparc_l1, sparc_o1); + sparc_sub (p, sparc_cc, sparc_l0, sparc_l1, sparc_o1); + sparc_subx (p, sparc_cc, sparc_l0, sparc_l1, sparc_o1); + + sparc_muls (p, sparc_l0, sparc_l1, sparc_o1); + sparc_umul (p, sparc_cc, sparc_l0, sparc_l1, sparc_o1); + sparc_smul (p, sparc_cc, sparc_l0, sparc_l1, sparc_o1); + sparc_udiv (p, sparc_cc, sparc_l0, sparc_l1, sparc_o1); + sparc_sdiv (p, sparc_cc, sparc_l0, sparc_l1, sparc_o1); + + sparc_branch (p, FALSE, sparc_bne, -12); + sparc_ret (p); + sparc_retl (p); + sparc_test (p, sparc_l4); + sparc_cmp (p, sparc_l4, sparc_l6); + sparc_cmp_imm (p, sparc_l4, 4); + sparc_restore_simple (p); + + sparc_set (p, 0xff000000, sparc_l7); + sparc_set (p, 1, sparc_l7); + sparc_set (p, 0xff0000ff, sparc_l7); + + sparc_not (p, sparc_g2); + sparc_neg (p, sparc_g3); + sparc_clr_reg (p, sparc_g4); + + + size = (p-code_buffer)*4; + ins = (gchar*)code_buffer; + for (i = 0; i < size; ++i) + printf (".byte %d\n", (unsigned int) ins [i]); + return 0; +} + diff --git a/src/arch/sparc/tramp.c b/src/arch/sparc/tramp.c new file mode 100644 index 0000000..19c0a78 --- /dev/null +++ b/src/arch/sparc/tramp.c @@ -0,0 +1,1080 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ +/* + * Create trampolines to invoke arbitrary functions. + * + * Copyright (C) Ximian Inc. + * + * Authors: Paolo Molaro (lupus@ximian.com) + * Jeffrey Stedfast <fejj@ximian.com> + * Mark Crichton <crichton@gimp.org> + * + */ + +#include "config.h" +#include <stdlib.h> +#include <string.h> +#include "sparc-codegen.h" +#include "mono/metadata/class.h" +#include "mono/metadata/tabledefs.h" +#include "mono/interpreter/interp.h" +#include "mono/metadata/appdomain.h" +#include "mono/metadata/debug-helpers.h" +#include "mono/metadata/marshal.h" + + +#define ARG_SIZE sizeof (stackval) +#define PROLOG_INS 1 +#define CALL_INS 3 /* Max 3. 1 for the jmpl and 1 for the nop and 1 for the possible unimp */ +#define EPILOG_INS 2 +#define FLOAT_REGS 32 +#define OUT_REGS 6 +#define LOCAL_REGS 8 +#define SLOT_SIZE sizeof(gpointer) +#if SPARCV9 +#define MINIMAL_STACK_SIZE 22 +#define BIAS 2047 +#define FRAME_ALIGN 16 +#else +#define MINIMAL_STACK_SIZE 23 +#define BIAS 0 +#define FRAME_ALIGN 8 +#endif + +#define NOT_IMPL(x) g_error("FIXME: %s", x); +/*#define DEBUG(a) a*/ +#define DEBUG(a) + +/* Some assembly... */ +#ifdef __GNUC__ +#define flushi(addr) __asm__ __volatile__ ("flush %0"::"r"(addr):"memory") +#else +static void flushi(void *addr) +{ + asm("flush %i0"); +} +#endif + +static char* +sig_to_name (MonoMethodSignature *sig, const char *prefix) +{ + int i; + char *result; + GString *res = g_string_new (""); + char *p; + + if (prefix) { + g_string_append (res, prefix); + g_string_append_c (res, '_'); + } + + mono_type_get_desc (res, sig->ret, TRUE); + + for (i = 0; i < sig->param_count; ++i) { + g_string_append_c (res, '_'); + mono_type_get_desc (res, sig->params [i], TRUE); + } + result = res->str; + p = result; + /* remove chars Sun's asssembler doesn't like */ + while (*p != '\0') { + if (*p == '.' || *p == '/') + *p = '_'; + else if (*p == '&') + *p = '$'; + else if (*p == '[' || *p == ']') + *p = 'X'; + p++; + } + g_string_free (res, FALSE); + return result; +} + +static void +sparc_disassemble_code (guint32 *code_buffer, guint32 *p, const char *id) +{ + guchar *cp; + FILE *ofd; + + if (!(ofd = fopen ("/tmp/test.s", "w"))) + g_assert_not_reached(); + + fprintf (ofd, "%s:\n", id); + + for (cp = (guchar *)code_buffer; cp < (guchar *)p; cp++) + fprintf (ofd, ".byte %d\n", *cp); + + fclose (ofd); + +#ifdef __GNUC__ + system ("as /tmp/test.s -o /tmp/test.o;objdump -d /tmp/test.o"); +#else + /* this assumes we are using Sun tools as we aren't GCC */ +#if SPARCV9 + system ("as -xarch=v9 /tmp/test.s -o /tmp/test.o;dis /tmp/test.o"); +#else + system ("as /tmp/test.s -o /tmp/test.o;dis /tmp/test.o"); +#endif +#endif +} + + +static void +add_general (guint *gr, guint *stack_size, guint *code_size, gboolean simple) +{ + if (simple) { + if (*gr >= OUT_REGS) { + *stack_size += SLOT_SIZE; + *code_size += 12; + } else { + *code_size += 4; + } + } else { + if (*gr >= OUT_REGS - 1) { + *stack_size += 8 + (*stack_size % 8); /* ???64 */ + *code_size += 16; + } else { + *code_size += 16; + } + (*gr)++; + } + (*gr)++; +} + +static void +calculate_sizes (MonoMethodSignature *sig, guint *stack_size, guint *code_size, + gboolean string_ctor, gboolean *use_memcpy) +{ + guint i, fr, gr; + guint32 simpletype; + + fr = gr = 0; + *stack_size = MINIMAL_STACK_SIZE * SLOT_SIZE; + *code_size = (PROLOG_INS + CALL_INS + EPILOG_INS) * 4; + + /* function arguments */ + if (sig->hasthis) + add_general (&gr, stack_size, code_size, TRUE); + + for (i = 0; i < sig->param_count; i++) { + if (sig->params[i]->byref) { + add_general (&gr, stack_size, code_size, TRUE); + continue; + } + simpletype = sig->params[i]->type; + enum_calc_size: + switch (simpletype) { + case MONO_TYPE_R4: +#if SPARCV9 + (*code_size) += 4; /* for the fdtos */ +#else + (*code_size) += 12; + (*stack_size) += 4; +#endif + case MONO_TYPE_BOOLEAN: + case MONO_TYPE_CHAR: + case MONO_TYPE_I1: + case MONO_TYPE_U1: + case MONO_TYPE_I2: + case MONO_TYPE_U2: + case MONO_TYPE_I4: + case MONO_TYPE_U4: + case MONO_TYPE_I: + case MONO_TYPE_U: + case MONO_TYPE_PTR: + case MONO_TYPE_CLASS: + case MONO_TYPE_OBJECT: + case MONO_TYPE_STRING: + case MONO_TYPE_SZARRAY: + add_general (&gr, stack_size, code_size, TRUE); + break; + case MONO_TYPE_VALUETYPE: { + gint size; + guint32 align; + if (sig->params[i]->data.klass->enumtype) { + simpletype = sig->params[i]->data.klass->enum_basetype->type; + goto enum_calc_size; + } + size = mono_class_native_size (sig->params[i]->data.klass, &align); +#if SPARCV9 + if (size != 4) { +#else + if (1) { +#endif + DEBUG(fprintf(stderr, "copy %d byte struct on stack\n", size)); + *use_memcpy = TRUE; + *code_size += 8*4; + + *stack_size = (*stack_size + (align - 1)) & (~(align -1)); + *stack_size += (size + 3) & (~3); + if (gr > OUT_REGS) { + *code_size += 4; + *stack_size += 4; + } + } else { + add_general (&gr, stack_size, code_size, TRUE); +#if SPARCV9 + *code_size += 8; +#else + *code_size += 4; +#endif + } + break; + } + case MONO_TYPE_I8: + case MONO_TYPE_R8: + add_general (&gr, stack_size, code_size, FALSE); + break; + default: + g_error ("Can't trampoline 0x%x", sig->params[i]->type); + } + } + + /* function return value */ + if (sig->ret->byref || string_ctor) { + *code_size += 8; + } else { + simpletype = sig->ret->type; + enum_retvalue: + switch (simpletype) { + case MONO_TYPE_BOOLEAN: + case MONO_TYPE_I1: + case MONO_TYPE_U1: + case MONO_TYPE_I2: + case MONO_TYPE_U2: + case MONO_TYPE_CHAR: + case MONO_TYPE_I4: + case MONO_TYPE_U4: + case MONO_TYPE_I: + case MONO_TYPE_U: + case MONO_TYPE_CLASS: + case MONO_TYPE_OBJECT: + case MONO_TYPE_PTR: + case MONO_TYPE_STRING: + case MONO_TYPE_R4: + case MONO_TYPE_R8: + case MONO_TYPE_SZARRAY: + case MONO_TYPE_ARRAY: + *code_size += 8; + break; + case MONO_TYPE_I8: + *code_size += 12; + break; + case MONO_TYPE_VALUETYPE: { + gint size; + if (sig->ret->data.klass->enumtype) { + simpletype = sig->ret->data.klass->enum_basetype->type; + goto enum_retvalue; + } + size = mono_class_native_size (sig->ret->data.klass, NULL); +#if SPARCV9 + if (size <= 32) + *code_size += 8 + (size + 7) / 2; + else + *code_size += 8; +#else + *code_size += 8; +#endif + break; + } + case MONO_TYPE_VOID: + break; + default: + g_error ("Can't handle as return value 0x%x", sig->ret->type); + } + } + + if (*use_memcpy) { + *stack_size += 8; + *code_size += 24; + if (sig->hasthis) { + *stack_size += SLOT_SIZE; + *code_size += 4; + } + } + + *stack_size = (*stack_size + (FRAME_ALIGN - 1)) & (~(FRAME_ALIGN -1)); +} + +static inline guint32 * +emit_epilog (guint32 *p, MonoMethodSignature *sig, guint stack_size) +{ + int ret_offset = 8; + + /* + * Standard epilog. + * 8 may be 12 when returning structures (to skip unimp opcode). + */ +#if !SPARCV9 + if (sig != NULL && !sig->ret->byref && sig->ret->type == MONO_TYPE_VALUETYPE && !sig->ret->data.klass->enumtype) + ret_offset = 12; +#endif + sparc_jmpl_imm (p, sparc_i7, ret_offset, sparc_zero); + sparc_restore (p, sparc_zero, sparc_zero, sparc_zero); + + return p; +} + +static inline guint32 * +emit_prolog (guint32 *p, MonoMethodSignature *sig, guint stack_size) +{ + /* yes kids, it is this simple! */ + sparc_save_imm (p, sparc_sp, -stack_size, sparc_sp); + return p; +} + +#if SPARCV9 +#define sparc_st_ptr(a,b,c,d) sparc_stx(a,b,c,d) +#define sparc_st_imm_ptr(a,b,c,d) sparc_stx_imm(a,b,c,d) +#define sparc_ld_ptr(a,b,c,d) sparc_ldx(a,b,c,d) +#define sparc_ld_imm_ptr(a,b,c,d) sparc_ldx_imm(a,b,c,d) +#else +#define sparc_st_ptr(a,b,c,d) sparc_st(a,b,c,d) +#define sparc_st_imm_ptr(a,b,c,d) sparc_st_imm(a,b,c,d) +#define sparc_ld_ptr(a,b,c,d) sparc_ld(a,b,c,d) +#define sparc_ld_imm_ptr(a,b,c,d) sparc_ld_imm(a,b,c,d) +#endif + +/* synonyms for when values are really widened scalar values */ +#define sparc_st_imm_word sparc_st_imm_ptr + +#define ARG_BASE sparc_i3 /* pointer to args in i3 */ +#define SAVE_PTR_IN_GENERIC_REGISTER \ + if (gr < OUT_REGS) { \ + sparc_ld_imm_ptr (p, ARG_BASE, i*ARG_SIZE, sparc_o0 + gr); \ + gr++; \ + } else { \ + sparc_ld_imm_ptr (p, ARG_BASE, i*ARG_SIZE, sparc_l0); \ + sparc_st_imm_ptr (p, sparc_l0, sparc_sp, stack_par_pos); \ + stack_par_pos += SLOT_SIZE; \ + } + +#if SPARCV9 +/* This is a half hearted attempt at coping with structs by value - the + actual convention is complicated when floats & doubles are involved as + you end up with fields in different registers on/off the stack. + It will take more time to get right... */ +static guint32 * +v9_struct_arg(guint32 *p, int arg_index, MonoClass *klass, int size, guint *p_gr) +{ + MonoMarshalType *info = mono_marshal_load_type_info (klass); + int off = 0; + int index = 0; + guint gr = *p_gr; + sparc_ld_imm_ptr (p, ARG_BASE, arg_index*ARG_SIZE, sparc_l0); + if (size > 8) { + if (info->fields [index].field->type->type == MONO_TYPE_R8) { + sparc_lddf_imm (p, sparc_l0, 0, sparc_f0 + 2 * gr); + index++; + } + else { + sparc_ldx_imm (p, sparc_l0, 0, sparc_o0 + gr); + index++; /* FIXME could be multiple fields in one register */ + } + gr++; + size -= 8; + off = 8; + } + if (size > 0) { + if (info->fields [index].field->type->type == MONO_TYPE_R8) { + sparc_lddf_imm (p, sparc_l0, off, sparc_f0 + 2 * gr); + index++; + } + else { + /* will load extra garbage off end of short structs ... */ + sparc_ldx_imm (p, sparc_l0, off, sparc_o0 + gr); + } + gr++; + } + *p_gr = gr; + return p; +} +#endif + +static inline guint32* +emit_save_parameters (guint32 *p, MonoMethodSignature *sig, guint stack_size, + gboolean use_memcpy) +{ + guint i, fr, gr, stack_par_pos, struct_pos, cur_struct_pos; + guint32 simpletype; + + fr = gr = 0; + stack_par_pos = MINIMAL_STACK_SIZE * SLOT_SIZE + BIAS; + + if (sig->hasthis) { + if (use_memcpy) { + /* we don't need to save a thing. */ + } else + sparc_mov_reg_reg (p, sparc_i2, sparc_o0); + gr ++; + } + + if (use_memcpy) { + cur_struct_pos = struct_pos = stack_par_pos; + for (i = 0; i < sig->param_count; i++) { + if (sig->params[i]->byref) + continue; + if (sig->params[i]->type == MONO_TYPE_VALUETYPE && + !sig->params[i]->data.klass->enumtype) { + gint size; + guint32 align; + + size = mono_class_native_size (sig->params[i]->data.klass, &align); +#if SPARCV9 + if (size != 4) { +#else + if (1) { +#endif + /* Add alignment */ + stack_par_pos = (stack_par_pos + (align - 1)) & (~(align - 1)); + /* need to call memcpy here */ + sparc_add_imm (p, 0, sparc_sp, stack_par_pos, sparc_o0); + sparc_ld_imm_ptr (p, sparc_i3, i*16, sparc_o1); + sparc_set (p, (guint32)size, sparc_o2); + sparc_set_ptr (p, (void *)memmove, sparc_l0); + sparc_jmpl_imm (p, sparc_l0, 0, sparc_callsite); + sparc_nop (p); + stack_par_pos += (size + (SLOT_SIZE - 1)) & (~(SLOT_SIZE - 1)); + } + } + } + } + + if (sig->ret->type == MONO_TYPE_VALUETYPE && !sig->ret->byref) { + MonoClass *klass = sig->ret->data.klass; + if (!klass->enumtype) { + gint size = mono_class_native_size (klass, NULL); + + DEBUG(fprintf(stderr, "retval value type size: %d\n", size)); +#if SPARCV9 + if (size > 32) { +#else + { +#endif + /* pass on buffer in interp.c to called function */ + sparc_ld_imm_ptr (p, sparc_i1, 0, sparc_l0); + sparc_st_imm_ptr (p, sparc_l0, sparc_sp, 64); + } + } + } + + DEBUG(fprintf(stderr, "%s\n", sig_to_name(sig, FALSE))); + + for (i = 0; i < sig->param_count; i++) { + if (sig->params[i]->byref) { + SAVE_PTR_IN_GENERIC_REGISTER; + continue; + } + simpletype = sig->params[i]->type; + enum_calc_size: + switch (simpletype) { + case MONO_TYPE_BOOLEAN: + case MONO_TYPE_I1: + case MONO_TYPE_U1: + case MONO_TYPE_I2: + case MONO_TYPE_U2: + case MONO_TYPE_CHAR: + case MONO_TYPE_I4: + case MONO_TYPE_U4: + if (gr < OUT_REGS) { + sparc_ld_imm (p, ARG_BASE, i*ARG_SIZE, sparc_o0 + gr); + gr++; + } else { + sparc_ld_imm (p, ARG_BASE, i*ARG_SIZE, sparc_l0); + sparc_st_imm_word (p, sparc_l0, sparc_sp, stack_par_pos); + stack_par_pos += SLOT_SIZE; + } + break; + + case MONO_TYPE_R4: +#if SPARCV9 + sparc_lddf_imm (p, ARG_BASE, i*ARG_SIZE, sparc_f30); /* fix using this fixed reg */ + sparc_fdtos(p, sparc_f30, sparc_f0 + 2 * gr + 1); + gr++; + break; +#else + /* Convert from double to single */ + sparc_lddf_imm (p, ARG_BASE, i*ARG_SIZE, sparc_f0); + sparc_fdtos (p, sparc_f0, sparc_f0); + + /* + * FIXME: Is there an easier way to do an + * freg->ireg move ? + */ + sparc_stf_imm (p, sparc_f0, sparc_sp, stack_par_pos); + + if (gr < OUT_REGS) { + sparc_ld_imm (p, sparc_sp, stack_par_pos, sparc_o0 + gr); + gr++; + } else { + sparc_ldf_imm (p, sparc_sp, stack_par_pos, sparc_f0); + sparc_stf_imm (p, sparc_f0, sparc_sp, stack_par_pos); + stack_par_pos += SLOT_SIZE; + } + break; +#endif + + case MONO_TYPE_I: + case MONO_TYPE_U: + case MONO_TYPE_PTR: + case MONO_TYPE_CLASS: + case MONO_TYPE_OBJECT: + case MONO_TYPE_STRING: + case MONO_TYPE_SZARRAY: + SAVE_PTR_IN_GENERIC_REGISTER; + break; + case MONO_TYPE_VALUETYPE: { + gint size; + guint32 align; + MonoClass *klass = sig->params[i]->data.klass; + if (klass->enumtype) { + simpletype = klass->enum_basetype->type; + goto enum_calc_size; + } + size = mono_class_native_size (klass, &align); +#if SPARCV9 + if (size <= 16) { + if (gr < OUT_REGS) { + p = v9_struct_arg(p, i, klass, size, &gr); + } else { + sparc_ld_imm_ptr (p, ARG_BASE, i*ARG_SIZE, sparc_l0); + sparc_ld_imm (p, sparc_l0, 0, sparc_l0); + sparc_st_imm_word (p, sparc_l0, sparc_sp, stack_par_pos); + stack_par_pos += SLOT_SIZE; + } + break; + } +#else + /* + * FIXME: The 32bit ABI docs do not mention that small + * structures are passed in registers. + */ + + /* + if (size == 4) { + if (gr < OUT_REGS) { + sparc_ld_imm_ptr (p, ARG_BASE, i*ARG_SIZE, sparc_l0); + sparc_ld_imm (p, sparc_l0, 0, sparc_o0 + gr); + gr++; + } else { + sparc_ld_imm_ptr (p, ARG_BASE, i*ARG_SIZE, sparc_l0); + sparc_ld_imm (p, sparc_l0, 0, sparc_l0); + sparc_st_imm_word (p, sparc_l0, sparc_sp, stack_par_pos); + stack_par_pos += SLOT_SIZE; + } + break; + } + */ +#endif + + cur_struct_pos = (cur_struct_pos + (align - 1)) & (~(align - 1)); + if (gr < OUT_REGS) { + sparc_add_imm (p, 0, sparc_sp, + cur_struct_pos, sparc_o0 + gr); + gr ++; + } else { + sparc_ld_imm_ptr (p, sparc_sp, + cur_struct_pos, + sparc_l1); + sparc_st_imm_ptr (p, sparc_l1, + sparc_sp, + stack_par_pos); + } + cur_struct_pos += (size + (SLOT_SIZE - 1)) & (~(SLOT_SIZE - 1)); + break; + } + +#if SPARCV9 + case MONO_TYPE_I8: + if (gr < OUT_REGS) { + sparc_ldx_imm (p, ARG_BASE, i*ARG_SIZE, sparc_o0 + gr); + gr++; + } else { + sparc_ldx_imm (p, ARG_BASE, i*ARG_SIZE, sparc_l0); + sparc_stx_imm (p, sparc_l0, sparc_sp, stack_par_pos); + stack_par_pos += SLOT_SIZE; + } + break; + case MONO_TYPE_R8: + sparc_lddf_imm (p, ARG_BASE, i*ARG_SIZE, sparc_f0 + 2 * i); + break; +#else + case MONO_TYPE_I8: + case MONO_TYPE_R8: + if (gr < (OUT_REGS - 1)) { + sparc_ld_imm (p, ARG_BASE, i*ARG_SIZE, sparc_o0 + gr); + gr ++; + + sparc_ld_imm (p, ARG_BASE, + (i*ARG_SIZE) + 4, + sparc_o0 + gr); + gr ++; + } else if (gr == (OUT_REGS - 1)) { + /* Split register/stack */ + sparc_ld_imm (p, ARG_BASE, i*ARG_SIZE, sparc_o0 + gr); + gr ++; + + sparc_ld_imm (p, ARG_BASE, (i*ARG_SIZE) + 4, sparc_l0); + sparc_st_imm (p, sparc_l0, sparc_sp, stack_par_pos); + stack_par_pos += SLOT_SIZE; + } else { + sparc_ld_imm (p, ARG_BASE, i*ARG_SIZE, sparc_l0); + sparc_st_imm (p, sparc_l0, sparc_sp, stack_par_pos); + stack_par_pos += SLOT_SIZE; + + sparc_ld_imm (p, ARG_BASE, (i*ARG_SIZE) + 4, sparc_l0); + sparc_st_imm (p, sparc_l0, sparc_sp, stack_par_pos); + stack_par_pos += SLOT_SIZE; + } + break; +#endif + default: + g_error ("Can't trampoline 0x%x", sig->params[i]->type); + } + } + + g_assert ((stack_par_pos - BIAS) <= stack_size); + + return p; +} + +static inline guint32 * +alloc_code_memory (guint code_size) +{ + guint32 *p; + + p = g_malloc(code_size); + + return p; +} + +static inline guint32 * +emit_call_and_store_retval (guint32 *p, MonoMethodSignature *sig, + guint stack_size, gboolean string_ctor) +{ + guint32 simpletype; + + /* call "callme" */ + sparc_jmpl_imm (p, sparc_i0, 0, sparc_callsite); + sparc_nop (p); +#if !SPARCV9 + if (sig->ret->type == MONO_TYPE_VALUETYPE && !sig->ret->data.klass->enumtype) { + int size = mono_class_native_size (sig->ret->data.klass, NULL); + sparc_unimp (p, size & 4095); + } +#endif + + /* get return value */ + if (sig->ret->byref || string_ctor) { + sparc_st_ptr (p, sparc_o0, sparc_i1, 0); + } else { + simpletype = sig->ret->type; + enum_retval: + switch (simpletype) { + case MONO_TYPE_BOOLEAN: + case MONO_TYPE_I1: + case MONO_TYPE_U1: + sparc_stb (p, sparc_o0, sparc_i1, 0); + break; + case MONO_TYPE_CHAR: + case MONO_TYPE_I2: + case MONO_TYPE_U2: + sparc_sth (p, sparc_o0, sparc_i1, 0); + break; + case MONO_TYPE_I4: + case MONO_TYPE_U4: + sparc_st (p, sparc_o0, sparc_i1, 0); + break; + case MONO_TYPE_I: + case MONO_TYPE_U: + case MONO_TYPE_CLASS: + case MONO_TYPE_OBJECT: + case MONO_TYPE_SZARRAY: + case MONO_TYPE_ARRAY: + case MONO_TYPE_STRING: + case MONO_TYPE_PTR: + sparc_st_ptr (p, sparc_o0, sparc_i1, 0); + break; + case MONO_TYPE_R4: + sparc_stf (p, sparc_f0, sparc_i1, 0); + break; + case MONO_TYPE_R8: + sparc_stdf (p, sparc_f0, sparc_i1, 0); + break; + case MONO_TYPE_I8: +#if SPARCV9 + sparc_stx (p, sparc_o0, sparc_i1, 0); +#else + sparc_std (p, sparc_o0, sparc_i1, 0); +#endif + break; + case MONO_TYPE_VALUETYPE: { + gint size; + if (sig->ret->data.klass->enumtype) { + simpletype = sig->ret->data.klass->enum_basetype->type; + goto enum_retval; + } +#if SPARCV9 + size = mono_class_native_size (sig->ret->data.klass, NULL); + if (size <= 32) { + int n_regs = size / 8; + int j; + sparc_ldx_imm (p, sparc_i1, 0, sparc_i1); + /* wrong if there are floating values in the struct... */ + for (j = 0; j < n_regs; j++) { + sparc_stx_imm (p, sparc_o0 + j, sparc_i1, j * 8); + } + size -= n_regs * 8; + if (size > 0) { + int last_reg = sparc_o0 + n_regs; + /* get value right aligned in register */ + sparc_srlx_imm(p, last_reg, 64 - 8 * size, last_reg); + if ((size & 1) != 0) { + sparc_stb_imm (p, last_reg, sparc_i1, n_regs * 8 + size - 1); + size--; + if (size > 0) + sparc_srlx_imm(p, last_reg, 8, last_reg); + } + if ((size & 2) != 0) { + sparc_sth_imm (p, last_reg, sparc_i1, n_regs * 8 + size - 2); + size -= 2; + if (size > 0) + sparc_srlx_imm(p, last_reg, 16, last_reg); + } + if ((size & 4) != 0) + sparc_st_imm (p, last_reg, sparc_i1, n_regs * 8); + } + } +#endif + } + case MONO_TYPE_VOID: + break; + default: + g_error ("Can't handle as return value 0x%x", sig->ret->type); + } + } + return p; +} + +MonoPIFunc +mono_arch_create_trampoline (MonoMethodSignature *sig, gboolean string_ctor) +{ + guint32 *p, *code_buffer; + guint stack_size, code_size, i; + gboolean use_memcpy = FALSE; + static GHashTable *cache = NULL; + MonoPIFunc res; + + if (!cache) + cache = g_hash_table_new ((GHashFunc)mono_signature_hash, + (GCompareFunc)mono_metadata_signature_equal); + + if ((res = (MonoPIFunc)g_hash_table_lookup(cache, sig))) + return res; + + calculate_sizes (sig, &stack_size, &code_size, + string_ctor, &use_memcpy); + + p = code_buffer = alloc_code_memory (code_size); + p = emit_prolog (p, sig, stack_size); + p = emit_save_parameters (p, sig, stack_size, use_memcpy); + p = emit_call_and_store_retval (p, sig, stack_size, string_ctor); + /* we don't return structs here so pass in NULL as signature */ + p = emit_epilog (p, NULL, stack_size); + + g_assert(p <= code_buffer + (code_size / 4)); + + DEBUG(sparc_disassemble_code (code_buffer, p, sig_to_name(sig, NULL))); + + /* So here's the deal... + * UltraSPARC will flush a whole cache line at a time + * BUT, older SPARCs won't. + * So, be compatable and flush dwords at a time... + */ + + for (i = 0; i < ((p - code_buffer)/2); i++) + flushi((code_buffer + (i*8))); + + g_hash_table_insert(cache, sig, code_buffer); + + return (MonoPIFunc)code_buffer; +} + +#define MINV_POS (MINIMAL_STACK_SIZE * SLOT_SIZE + BIAS) + +void * +mono_arch_create_method_pointer (MonoMethod *method) +{ + MonoMethodSignature *sig; + MonoJitInfo *ji; + guint stack_size, code_size, stackval_arg_pos, local_pos; + guint i, local_start, reg_param = 0, stack_param, cpos, vt_cur; + guint32 align = 0; + guint32 *p, *code_buffer; + gint *vtbuf; + gint32 simpletype; + + code_size = 1024; /* these should be calculated... */ + stack_size = 1024; + stack_param = 0; + + sig = method->signature; + + p = code_buffer = g_malloc (code_size); + + DEBUG(fprintf(stderr, "Delegate [start emiting] %s\n", method->name)); + DEBUG(fprintf(stderr, "%s\n", sig_to_name(sig, FALSE))); + + p = emit_prolog (p, sig, stack_size); + + /* fill MonoInvocation */ + sparc_st_imm_ptr (p, sparc_g0, sparc_sp, + (MINV_POS + G_STRUCT_OFFSET (MonoInvocation, ex))); + sparc_st_imm_ptr (p, sparc_g0, sparc_sp, + (MINV_POS + G_STRUCT_OFFSET (MonoInvocation, ex_handler))); + sparc_st_imm_ptr (p, sparc_g0, sparc_sp, + (MINV_POS + G_STRUCT_OFFSET (MonoInvocation, parent))); + + sparc_set_ptr (p, (void *)method, sparc_l0); + sparc_st_imm_ptr (p, sparc_l0, sparc_sp, + (MINV_POS + G_STRUCT_OFFSET (MonoInvocation, method))); + + stackval_arg_pos = MINV_POS + sizeof (MonoInvocation); + local_start = local_pos = stackval_arg_pos + (sig->param_count + 1) * sizeof (stackval); + + if (sig->hasthis) { + sparc_st_imm_ptr (p, sparc_i0, sparc_sp, + (MINV_POS + G_STRUCT_OFFSET (MonoInvocation, obj))); + reg_param = 1; + } + + if (sig->param_count) { + gint save_count = MIN (OUT_REGS, sig->param_count + sig->hasthis); + for (i = reg_param; i < save_count; i++) { + sparc_st_imm_ptr (p, sparc_i0 + i, sparc_sp, local_pos); + local_pos += SLOT_SIZE; + } + } + + /* prepare space for valuetypes */ + vt_cur = local_pos; + vtbuf = alloca (sizeof(int)*sig->param_count); + cpos = 0; + for (i = 0; i < sig->param_count; i++) { + MonoType *type = sig->params [i]; + vtbuf [i] = -1; + if (!sig->params[i]->byref && type->type == MONO_TYPE_VALUETYPE) { + MonoClass *klass = type->data.klass; + gint size; + + if (klass->enumtype) + continue; + size = mono_class_native_size (klass, &align); + cpos += align - 1; + cpos &= ~(align - 1); + vtbuf [i] = cpos; + cpos += size; + } + } + cpos += SLOT_SIZE - 1; + cpos &= ~(SLOT_SIZE - 1); + + local_pos += cpos; + + /* set MonoInvocation::stack_args */ + sparc_add_imm (p, 0, sparc_sp, stackval_arg_pos, sparc_l0); + sparc_st_imm_ptr (p, sparc_l0, sparc_sp, + (MINV_POS + G_STRUCT_OFFSET (MonoInvocation, stack_args))); + + /* add stackval arguments */ + for (i=0; i < sig->param_count; i++) { + int stack_offset; + int type; + if (reg_param < OUT_REGS) { + stack_offset = local_start + i * SLOT_SIZE; + reg_param++; + } else { + stack_offset = stack_size + 8 + stack_param; + stack_param++; + } + + if (!sig->params[i]->byref) { + type = sig->params[i]->type; + enum_arg: + switch (type) { + case MONO_TYPE_I8: + case MONO_TYPE_U8: + case MONO_TYPE_I: + case MONO_TYPE_U: + case MONO_TYPE_STRING: + case MONO_TYPE_OBJECT: + case MONO_TYPE_CLASS: + case MONO_TYPE_SZARRAY: + case MONO_TYPE_PTR: + case MONO_TYPE_R8: + break; + case MONO_TYPE_I4: + case MONO_TYPE_U4: + stack_offset += SLOT_SIZE - 4; + break; + case MONO_TYPE_CHAR: + case MONO_TYPE_I2: + case MONO_TYPE_U2: + stack_offset += SLOT_SIZE - 2; + break; + case MONO_TYPE_I1: + case MONO_TYPE_U1: + case MONO_TYPE_BOOLEAN: + stack_offset += SLOT_SIZE - 1; + break; + case MONO_TYPE_VALUETYPE: + if (sig->params[i]->data.klass->enumtype) { + type = sig->params[i]->data.klass->enum_basetype->type; + goto enum_arg; + } + g_assert(vtbuf[i] >= 0); + break; + default: + g_error ("can not cope with delegate arg type %d", type); + } + } + + sparc_add_imm (p, 0, sparc_sp, stack_offset, sparc_o2); + + if (vtbuf[i] >= 0) { + sparc_add_imm (p, 0, sparc_sp, vt_cur, sparc_o1); + sparc_st_imm_ptr (p, sparc_o1, sparc_sp, stackval_arg_pos); + sparc_add_imm (p, 0, sparc_sp, stackval_arg_pos, + sparc_o1); + sparc_ld_imm_ptr (p, sparc_o2, 0, sparc_o2); + vt_cur += vtbuf[i]; + } else { + sparc_add_imm (p, 0, sparc_sp, stackval_arg_pos, + sparc_o1); + } + + sparc_set_ptr (p, (void *)sig->params[i], sparc_o0); + sparc_set (p, (guint32)sig->pinvoke, sparc_o3); + + /* YOU make the CALL! */ + sparc_set_ptr (p, (void *)stackval_from_data, sparc_l0); + sparc_jmpl_imm (p, sparc_l0, 0, sparc_callsite); + sparc_nop (p); + stackval_arg_pos += sizeof(stackval); + } + + /* return value storage */ + /* Align to dword */ + stackval_arg_pos = (stackval_arg_pos + (8 - 1)) & (~(8 -1)); + if (sig->param_count) { + sparc_add_imm (p, 0, sparc_sp, stackval_arg_pos, sparc_l0); + } + if (!sig->ret->byref && sig->ret->type == MONO_TYPE_VALUETYPE && !sig->ret->data.klass->enumtype) { +#if !SPARCV9 + /* pass on callers buffer */ + sparc_ld_imm_ptr (p, sparc_fp, 64, sparc_l1); + sparc_st_imm_ptr (p, sparc_l1, sparc_l0, 0); +#else + sparc_add_imm (p, 0, sparc_l0, sizeof(stackval), sparc_l1); + sparc_st_imm_ptr (p, sparc_l1, sparc_l0, 0); +#endif + } + + sparc_st_imm_ptr (p, sparc_l0, sparc_sp, + (MINV_POS + G_STRUCT_OFFSET (MonoInvocation, retval))); + + /* call ves_exec_method */ + sparc_add_imm (p, 0, sparc_sp, MINV_POS, sparc_o0); + sparc_set_ptr (p, (void *)ves_exec_method, sparc_l0); + sparc_jmpl_imm (p, sparc_l0, 0, sparc_callsite); + sparc_nop (p); + + /* move retval from stackval to proper place (r3/r4/...) */ + if (sig->ret->byref) { + sparc_ld_imm_ptr (p, sparc_sp, stackval_arg_pos, sparc_i0 ); + } else { + enum_retvalue: + switch (sig->ret->type) { + case MONO_TYPE_VOID: + break; + case MONO_TYPE_BOOLEAN: + case MONO_TYPE_I1: + case MONO_TYPE_U1: + case MONO_TYPE_I2: + case MONO_TYPE_U2: + case MONO_TYPE_I4: + case MONO_TYPE_U4: + sparc_ld_imm (p, sparc_sp, stackval_arg_pos, sparc_i0); + break; + case MONO_TYPE_I: + case MONO_TYPE_U: + case MONO_TYPE_OBJECT: + case MONO_TYPE_STRING: + case MONO_TYPE_CLASS: + sparc_ld_imm_ptr (p, sparc_sp, stackval_arg_pos, sparc_i0); + break; + case MONO_TYPE_I8: + case MONO_TYPE_U8: +#if SPARCV9 + sparc_ldx_imm (p, sparc_sp, stackval_arg_pos, sparc_i0); +#else + sparc_ld_imm (p, sparc_sp, stackval_arg_pos, sparc_i0); + sparc_ld_imm (p, sparc_sp, stackval_arg_pos + 4, sparc_i1); +#endif + break; + case MONO_TYPE_R4: + sparc_lddf_imm (p, sparc_sp, stackval_arg_pos, sparc_f0); + sparc_fdtos(p, sparc_f0, sparc_f0); + break; + case MONO_TYPE_R8: + sparc_lddf_imm (p, sparc_sp, stackval_arg_pos, sparc_f0); + break; + case MONO_TYPE_VALUETYPE: { + gint size; + gint reg = sparc_i0; + if (sig->ret->data.klass->enumtype) { + simpletype = sig->ret->data.klass->enum_basetype->type; + goto enum_retvalue; + } +#if SPARCV9 + size = mono_class_native_size (sig->ret->data.klass, NULL); + sparc_ldx_imm (p, sparc_sp, stackval_arg_pos, sparc_l0); + if (size <= 16) { + gint off = 0; + if (size >= 8) { + sparc_ldx_imm (p, sparc_l0, 0, reg); + size -= 8; + off += 8; + reg++; + } + if (size > 0) + sparc_ldx_imm (p, sparc_l0, off, reg); + } else + NOT_IMPL("value type as ret val from delegate"); +#endif + break; + } + default: + g_error ("Type 0x%x not handled yet in thunk creation", + sig->ret->type); + break; + } + } + + p = emit_epilog (p, sig, stack_size); + + for (i = 0; i < ((p - code_buffer)/2); i++) + flushi((code_buffer + (i*8))); + + ji = g_new0 (MonoJitInfo, 1); + ji->method = method; + ji->code_size = p - code_buffer; + ji->code_start = code_buffer; + + mono_jit_info_table_add (mono_get_root_domain (), ji); + + DEBUG(sparc_disassemble_code (code_buffer, p, method->name)); + + DEBUG(fprintf(stderr, "Delegate [end emiting] %s\n", method->name)); + + return ji->code_start; +} |