32x: drc: more wip, ARM untested
[picodrive.git] / cpu / drc / emit_x86.c
index e94e685..5a36faf 100644 (file)
@@ -1,22 +1,44 @@
+/*
+ * note about silly things like emith_eor_r_r_r_lsl:
+ * these are here because the compiler was designed
+ * for ARM as it's primary target.
+ */
 #include <stdarg.h>
 
 enum { xAX = 0, xCX, xDX, xBX, xSP, xBP, xSI, xDI };
 
 #define CONTEXT_REG xBP
 
+#define IOP_JO  0x70
+#define IOP_JNO 0x71
+#define IOP_JB  0x72
+#define IOP_JAE 0x73
 #define IOP_JE  0x74
 #define IOP_JNE 0x75
 #define IOP_JBE 0x76
 #define IOP_JA  0x77
 #define IOP_JS  0x78
 #define IOP_JNS 0x79
+#define IOP_JL  0x7c
+#define IOP_JGE 0x7d
 #define IOP_JLE 0x7e
+#define IOP_JG  0x7f
 
 // unified conditions (we just use rel8 jump instructions for x86)
 #define DCOND_EQ IOP_JE
 #define DCOND_NE IOP_JNE
 #define DCOND_MI IOP_JS      // MInus
 #define DCOND_PL IOP_JNS     // PLus or zero
+#define DCOND_HI IOP_JA      // higher (unsigned)
+#define DCOND_HS IOP_JAE     // higher || same (unsigned)
+#define DCOND_LO IOP_JB      // lower (unsigned)
+#define DCOND_LS IOP_JBE     // lower || same (unsigned)
+#define DCOND_GE IOP_JGE     // greater || equal (signed)
+#define DCOND_GT IOP_JG      // greater (signed)
+#define DCOND_LE IOP_JLE     // less || equal (signed)
+#define DCOND_LT IOP_JL      // less (signed)
+#define DCOND_VS IOP_JO      // oVerflow Set
+#define DCOND_VC IOP_JNO     // oVerflow Clear
 
 #define EMIT_PTR(ptr, val, type) \
        *(type *)(ptr) = val
@@ -47,6 +69,7 @@ enum { xAX = 0, xCX, xDX, xBX, xSP, xBP, xSI, xDI };
        EMIT_PTR(ptr, op, u8); \
        EMIT_PTR(ptr + 1, (tcache_ptr - (ptr+2)), u8)
 
+// _r_r
 #define emith_move_r_r(dst, src) \
        EMIT_OP_MODRM(0x8b, 3, dst, src)
 
@@ -56,11 +79,26 @@ enum { xAX = 0, xCX, xDX, xBX, xSP, xBP, xSI, xDI };
 #define emith_sub_r_r(d, s) \
        EMIT_OP_MODRM(0x29, 3, s, d)
 
+#define emith_adc_r_r(d, s) \
+       EMIT_OP_MODRM(0x11, 3, s, d)
+
+#define emith_sbc_r_r(d, s) \
+       EMIT_OP_MODRM(0x19, 3, s, d) /* SBB */
+
 #define emith_or_r_r(d, s) \
        EMIT_OP_MODRM(0x09, 3, s, d)
 
+#define emith_and_r_r(d, s) \
+       EMIT_OP_MODRM(0x21, 3, s, d)
+
 #define emith_eor_r_r(d, s) \
-       EMIT_OP_MODRM(0x31, 3, s, d)
+       EMIT_OP_MODRM(0x31, 3, s, d) /* XOR */
+
+#define emith_tst_r_r(d, s) \
+       EMIT_OP_MODRM(0x85, 3, s, d) /* TEST */
+
+#define emith_cmp_r_r(d, s) \
+       EMIT_OP_MODRM(0x39, 3, s, d)
 
 // fake teq - test equivalence - get_flags(d ^ s)
 #define emith_teq_r_r(d, s) { \
@@ -69,6 +107,27 @@ enum { xAX = 0, xCX, xDX, xBX, xSP, xBP, xSI, xDI };
        emith_pop(d); \
 }
 
+// _r_r_r
+#define emith_eor_r_r_r(d, s1, s2) { \
+       if (d != s1) \
+               emith_move_r_r(d, s1); \
+       emith_eor_r_r(d, s2); \
+}
+
+#define emith_or_r_r_r_lsl(d, s1, s2, lslimm) { \
+       if (d != s2 && d != s1) { \
+               emith_lsl(d, s2, lslimm); \
+               emith_or_r_r(d, s1); \
+       } else { \
+               if (d != s1) \
+                       emith_move_r_r(d, s1); \
+               emith_push(s2); \
+               emith_lsl(s2, s2, lslimm); \
+               emith_or_r_r(d, s2); \
+               emith_pop(s2); \
+       } \
+}
+
 // _r_imm
 #define emith_move_r_imm(r, imm) { \
        EMIT_OP(0xb8 + (r)); \
@@ -80,7 +139,7 @@ enum { xAX = 0, xCX, xDX, xBX, xSP, xBP, xSI, xDI };
        EMIT(imm, u32); \
 }
 
-// 2 - adc, 3 - sbb, 6 - xor, 7 - cmp
+// 2 - adc, 3 - sbb, 6 - xor
 #define emith_add_r_imm(r, imm) \
        emith_arith_r_imm(0, r, imm)
 
@@ -93,6 +152,9 @@ enum { xAX = 0, xCX, xDX, xBX, xSP, xBP, xSI, xDI };
 #define emith_sub_r_imm(r, imm) \
        emith_arith_r_imm(5, r, imm)
 
+#define emith_cmp_r_imm(r, imm) \
+       emith_arith_r_imm(7, r, imm)
+
 #define emith_tst_r_imm(r, imm) { \
        EMIT_OP_MODRM(0xf7, 3, 0, r); \
        EMIT(imm, u32); \
@@ -105,17 +167,22 @@ enum { xAX = 0, xCX, xDX, xBX, xSP, xBP, xSI, xDI };
 // fake conditionals (using SJMP instead)
 #define emith_add_r_imm_c(cond, r, imm) { \
        (void)(cond); \
-       emith_arith_r_imm(0, r, imm); \
+       emith_add_r_imm(r, imm); \
 }
 
 #define emith_or_r_imm_c(cond, r, imm) { \
        (void)(cond); \
-       emith_arith_r_imm(1, r, imm); \
+       emith_or_r_imm(r, imm); \
 }
 
 #define emith_sub_r_imm_c(cond, r, imm) { \
        (void)(cond); \
-       emith_arith_r_imm(5, r, imm); \
+       emith_sub_r_imm(r, imm); \
+}
+
+#define emith_bic_r_imm_c(cond, r, imm) { \
+       (void)(cond); \
+       emith_bic_r_imm(r, imm); \
 }
 
 // shift
@@ -126,11 +193,26 @@ enum { xAX = 0, xCX, xDX, xBX, xSP, xBP, xSI, xDI };
        EMIT(cnt, u8); \
 }
 
+#define emith_lsl(d, s, cnt) \
+       emith_shift(4, d, s, cnt)
+
+#define emith_lsr(d, s, cnt) \
+       emith_shift(5, d, s, cnt)
+
 #define emith_asr(d, s, cnt) \
        emith_shift(7, d, s, cnt)
 
-#define emith_lsl(d, s, cnt) \
-       emith_shift(4, d, s, cnt)
+#define emith_rol(d, s, cnt) \
+       emith_shift(0, d, s, cnt)
+
+#define emith_ror(d, s, cnt) \
+       emith_shift(1, d, s, cnt)
+
+#define emith_rolc(r) \
+       EMIT_OP_MODRM(0xd1, 3, 2, r)
+
+#define emith_rorc(r) \
+       EMIT_OP_MODRM(0xd1, 3, 3, r)
 
 // misc
 #define emith_push(r) \
@@ -155,11 +237,24 @@ enum { xAX = 0, xCX, xDX, xBX, xSP, xBP, xSI, xDI };
        emith_asr(d, d, 32 - (bits)); \
 }
 
+// put bit0 of r0 to carry
+#define emith_set_carry(r0) { \
+       emith_tst_r_imm(r0, 1); /* clears C */ \
+       EMITH_SJMP_START(DCOND_EQ); \
+       EMIT_OP(0xf9); /* STC */ \
+       EMITH_SJMP_END(DCOND_EQ); \
+}
+
+// put bit0 of r0 to carry (for subtraction)
+#define emith_set_carry_sub emith_set_carry
+
 // XXX: stupid mess
-#define emith_mul(d, s1, s2) { \
+#define emith_mul_(op, dlo, dhi, s1, s2) { \
        int rmr; \
-       if (d != xAX) \
+       if (dlo != xAX && dhi != xAX) \
                emith_push(xAX); \
+       if (dlo != xDX && dhi != xDX) \
+               emith_push(xDX); \
        if ((s1) == xAX) \
                rmr = s2; \
        else if ((s2) == xAX) \
@@ -168,18 +263,43 @@ enum { xAX = 0, xCX, xDX, xBX, xSP, xBP, xSI, xDI };
                emith_move_r_r(xAX, s1); \
                rmr = s2; \
        } \
-       emith_push(xDX); \
-       EMIT_OP_MODRM(0xf7, 3, 4, rmr); /* MUL rmr */ \
-       emith_pop(xDX); \
-       if (d != xAX) { \
-               emith_move_r_r(d, xAX); \
+       EMIT_OP_MODRM(0xf7, 3, op, rmr); /* xMUL rmr */ \
+       /* XXX: using push/pop for the case of edx->eax; eax->edx */ \
+       if (dhi != xDX && dhi != -1) \
+               emith_push(xDX); \
+       if (dlo != xAX) \
+               emith_move_r_r(dlo, xAX); \
+       if (dhi != xDX && dhi != -1) \
+               emith_pop(dhi); \
+       if (dlo != xDX && dhi != xDX) \
+               emith_pop(xDX); \
+       if (dlo != xAX && dhi != xAX) \
                emith_pop(xAX); \
-       } \
 }
 
+#define emith_mul_u64(dlo, dhi, s1, s2) \
+       emith_mul_(4, dlo, dhi, s1, s2) /* MUL */
+
+#define emith_mul_s64(dlo, dhi, s1, s2) \
+       emith_mul_(5, dlo, dhi, s1, s2) /* IMUL */
+
+#define emith_mul(d, s1, s2) \
+       emith_mul_(4, d, -1, s1, s2)
+
 // "flag" instructions are the same
 #define emith_subf_r_imm emith_sub_r_imm
+#define emith_addf_r_r   emith_add_r_r
 #define emith_subf_r_r   emith_sub_r_r
+#define emith_adcf_r_r   emith_adc_r_r
+#define emith_sbcf_r_r   emith_sbc_r_r
+
+#define emith_lslf  emith_lsl
+#define emith_lsrf  emith_lsr
+#define emith_asrf  emith_asr
+#define emith_rolf  emith_rol
+#define emith_rorf  emith_ror
+#define emith_rolcf emith_rolc
+#define emith_rorcf emith_rorc
 
 // XXX: offs is 8bit only
 #define emith_ctx_read(r, offs) { \
@@ -265,3 +385,22 @@ enum { xAX = 0, xCX, xDX, xBX, xSP, xBP, xSI, xDI };
        rcache_free_tmp(tmp);                            \
 }
 
+#define emith_write_sr(srcr) { \
+       int tmp = rcache_get_tmp(); \
+       int srr = rcache_get_reg(SHR_SR, RC_GR_RMW); \
+       emith_clear_msb(tmp, srcr, 20); \
+       emith_bic_r_imm(srr, 0xfff); \
+       emith_or_r_r(srr, tmp); \
+       rcache_free_tmp(tmp); \
+}
+
+#define emith_carry_to_t(srr, is_sub) { \
+       int tmp = rcache_get_tmp(); \
+       EMIT_OP(0x0f); \
+       EMIT(0x92, u8); \
+       EMIT_MODRM(3, 0, tmp); /* SETC */ \
+       emith_bic_r_imm(srr, 1); \
+       EMIT_OP_MODRM(0x08, 3, tmp, srr); /* OR srrl, tmpl */ \
+       rcache_free_tmp(tmp); \
+}
+