2 * Basic macros to emit x86 instructions and some utils
3 * Copyright (C) 2008,2009,2010 notaz
5 * This work is licensed under the terms of MAME license.
6 * See COPYING file in the top-level directory.
9 * temp registers must be eax-edx due to use of SETcc and r/w 8/16.
10 * note about silly things like emith_eor_r_r_r:
11 * these are here because the compiler was designed
12 * for ARM as it's primary target.
16 enum { xAX = 0, xCX, xDX, xBX, xSP, xBP, xSI, xDI };
18 #define CONTEXT_REG xBP
21 #define ICOND_JNO 0x01
23 #define ICOND_JAE 0x03
25 #define ICOND_JNE 0x05
26 #define ICOND_JBE 0x06
29 #define ICOND_JNS 0x09
31 #define ICOND_JGE 0x0d
32 #define ICOND_JLE 0x0e
37 // unified conditions (we just use rel8 jump instructions for x86)
38 #define DCOND_EQ ICOND_JE
39 #define DCOND_NE ICOND_JNE
40 #define DCOND_MI ICOND_JS // MInus
41 #define DCOND_PL ICOND_JNS // PLus or zero
42 #define DCOND_HI ICOND_JA // higher (unsigned)
43 #define DCOND_HS ICOND_JAE // higher || same (unsigned)
44 #define DCOND_LO ICOND_JB // lower (unsigned)
45 #define DCOND_LS ICOND_JBE // lower || same (unsigned)
46 #define DCOND_GE ICOND_JGE // greater || equal (signed)
47 #define DCOND_GT ICOND_JG // greater (signed)
48 #define DCOND_LE ICOND_JLE // less || equal (signed)
49 #define DCOND_LT ICOND_JL // less (signed)
50 #define DCOND_VS ICOND_JO // oVerflow Set
51 #define DCOND_VC ICOND_JNO // oVerflow Clear
53 #define EMIT_PTR(ptr, val, type) \
56 #define EMIT(val, type) { \
57 EMIT_PTR(tcache_ptr, val, type); \
58 tcache_ptr += sizeof(type); \
61 #define EMIT_OP(op) { \
66 #define EMIT_MODRM(mod,r,rm) \
67 EMIT(((mod)<<6) | ((r)<<3) | (rm), u8)
69 #define EMIT_SIB(scale,index,base) \
70 EMIT(((scale)<<6) | ((index)<<3) | (base), u8)
72 #define EMIT_OP_MODRM(op,mod,r,rm) do { \
74 EMIT_MODRM(mod, r, rm); \
77 #define JMP8_POS(ptr) \
81 #define JMP8_EMIT(op, ptr) \
82 EMIT_PTR(ptr, 0x70|(op), u8); \
83 EMIT_PTR(ptr + 1, (tcache_ptr - (ptr+2)), u8)
85 #define JMP8_EMIT_NC(ptr) \
86 EMIT_PTR(ptr, IOP_JMP, u8); \
87 EMIT_PTR(ptr + 1, (tcache_ptr - (ptr+2)), u8)
90 #define emith_move_r_r(dst, src) \
91 EMIT_OP_MODRM(0x8b, 3, dst, src)
93 #define emith_add_r_r(d, s) \
94 EMIT_OP_MODRM(0x01, 3, s, d)
96 #define emith_sub_r_r(d, s) \
97 EMIT_OP_MODRM(0x29, 3, s, d)
99 #define emith_adc_r_r(d, s) \
100 EMIT_OP_MODRM(0x11, 3, s, d)
102 #define emith_sbc_r_r(d, s) \
103 EMIT_OP_MODRM(0x19, 3, s, d) /* SBB */
105 #define emith_or_r_r(d, s) \
106 EMIT_OP_MODRM(0x09, 3, s, d)
108 #define emith_and_r_r(d, s) \
109 EMIT_OP_MODRM(0x21, 3, s, d)
111 #define emith_eor_r_r(d, s) \
112 EMIT_OP_MODRM(0x31, 3, s, d) /* XOR */
114 #define emith_tst_r_r(d, s) \
115 EMIT_OP_MODRM(0x85, 3, s, d) /* TEST */
117 #define emith_cmp_r_r(d, s) \
118 EMIT_OP_MODRM(0x39, 3, s, d)
120 // fake teq - test equivalence - get_flags(d ^ s)
121 #define emith_teq_r_r(d, s) { \
123 emith_eor_r_r(d, s); \
127 #define emith_mvn_r_r(d, s) { \
129 emith_move_r_r(d, s); \
130 EMIT_OP_MODRM(0xf7, 3, 2, d); /* NOT d */ \
133 #define emith_negc_r_r(d, s) { \
134 int tmp_ = rcache_get_tmp(); \
135 emith_move_r_imm(tmp_, 0); \
136 emith_sbc_r_r(tmp_, s); \
137 emith_move_r_r(d, tmp_); \
138 rcache_free_tmp(tmp_); \
141 #define emith_neg_r_r(d, s) { \
143 emith_move_r_r(d, s); \
144 EMIT_OP_MODRM(0xf7, 3, 3, d); /* NEG d */ \
148 #define emith_eor_r_r_r(d, s1, s2) { \
150 emith_eor_r_r(d, s2); \
151 } else if (d == s2) { \
152 emith_eor_r_r(d, s1); \
154 emith_move_r_r(d, s1); \
155 emith_eor_r_r(d, s2); \
160 #define emith_or_r_r_lsl(d, s, lslimm) { \
161 int tmp_ = rcache_get_tmp(); \
162 emith_lsl(tmp_, s, lslimm); \
163 emith_or_r_r(d, tmp_); \
164 rcache_free_tmp(tmp_); \
168 #define emith_eor_r_r_lsr(d, s, lsrimm) { \
170 emith_lsr(s, s, lsrimm); \
171 emith_eor_r_r(d, s); \
176 #define emith_move_r_imm(r, imm) { \
177 EMIT_OP(0xb8 + (r)); \
181 #define emith_move_r_imm_s8(r, imm) \
182 emith_move_r_imm(r, (u32)(signed int)(signed char)(imm))
184 #define emith_arith_r_imm(op, r, imm) do { \
185 EMIT_OP_MODRM(0x81, 3, op, r); \
189 #define emith_add_r_imm(r, imm) \
190 emith_arith_r_imm(0, r, imm)
192 #define emith_or_r_imm(r, imm) \
193 emith_arith_r_imm(1, r, imm)
195 #define emith_adc_r_imm(r, imm) \
196 emith_arith_r_imm(2, r, imm)
198 #define emith_sbc_r_imm(r, imm) \
199 emith_arith_r_imm(3, r, imm) // sbb
201 #define emith_and_r_imm(r, imm) \
202 emith_arith_r_imm(4, r, imm)
204 #define emith_sub_r_imm(r, imm) \
205 emith_arith_r_imm(5, r, imm)
207 #define emith_eor_r_imm(r, imm) \
208 emith_arith_r_imm(6, r, imm)
210 #define emith_cmp_r_imm(r, imm) \
211 emith_arith_r_imm(7, r, imm)
213 #define emith_tst_r_imm(r, imm) do { \
214 EMIT_OP_MODRM(0xf7, 3, 0, r); \
219 #define emith_bic_r_imm(r, imm) \
220 emith_arith_r_imm(4, r, ~(imm))
222 // fake conditionals (using SJMP instead)
223 #define emith_move_r_imm_c(cond, r, imm) { \
225 emith_move_r_imm(r, imm); \
228 #define emith_add_r_imm_c(cond, r, imm) { \
230 emith_add_r_imm(r, imm); \
233 #define emith_sub_r_imm_c(cond, r, imm) { \
235 emith_sub_r_imm(r, imm); \
238 #define emith_or_r_imm_c(cond, r, imm) \
239 emith_or_r_imm(r, imm)
240 #define emith_eor_r_imm_c(cond, r, imm) \
241 emith_eor_r_imm(r, imm)
242 #define emith_bic_r_imm_c(cond, r, imm) \
243 emith_bic_r_imm(r, imm)
244 #define emith_ror_c(cond, d, s, cnt) \
247 #define emith_read_r_r_offs_c(cond, r, rs, offs) \
248 emith_read_r_r_offs(r, rs, offs)
249 #define emith_write_r_r_offs_c(cond, r, rs, offs) \
250 emith_write_r_r_offs(r, rs, offs)
251 #define emith_read8_r_r_offs_c(cond, r, rs, offs) \
252 emith_read8_r_r_offs(r, rs, offs)
253 #define emith_write8_r_r_offs_c(cond, r, rs, offs) \
254 emith_write8_r_r_offs(r, rs, offs)
255 #define emith_read16_r_r_offs_c(cond, r, rs, offs) \
256 emith_read16_r_r_offs(r, rs, offs)
257 #define emith_write16_r_r_offs_c(cond, r, rs, offs) \
258 emith_write16_r_r_offs(r, rs, offs)
259 #define emith_jump_reg_c(cond, r) \
261 #define emith_jump_ctx_c(cond, offs) \
263 #define emith_ret_c(cond) \
267 #define emith_add_r_r_imm(d, s, imm) { \
269 emith_move_r_r(d, s); \
270 emith_add_r_imm(d, imm); \
273 #define emith_and_r_r_imm(d, s, imm) { \
275 emith_move_r_r(d, s); \
276 emith_and_r_imm(d, imm); \
280 #define emith_shift(op, d, s, cnt) { \
282 emith_move_r_r(d, s); \
283 EMIT_OP_MODRM(0xc1, 3, op, d); \
287 #define emith_lsl(d, s, cnt) \
288 emith_shift(4, d, s, cnt)
290 #define emith_lsr(d, s, cnt) \
291 emith_shift(5, d, s, cnt)
293 #define emith_asr(d, s, cnt) \
294 emith_shift(7, d, s, cnt)
296 #define emith_rol(d, s, cnt) \
297 emith_shift(0, d, s, cnt)
299 #define emith_ror(d, s, cnt) \
300 emith_shift(1, d, s, cnt)
302 #define emith_rolc(r) \
303 EMIT_OP_MODRM(0xd1, 3, 2, r)
305 #define emith_rorc(r) \
306 EMIT_OP_MODRM(0xd1, 3, 3, r)
309 #define emith_push(r) \
312 #define emith_push_imm(imm) { \
317 #define emith_pop(r) \
320 #define emith_neg_r(r) \
321 EMIT_OP_MODRM(0xf7, 3, 3, r)
323 #define emith_clear_msb(d, s, count) { \
327 emith_move_r_r(d, s); \
328 emith_and_r_imm(d, t); \
331 #define emith_clear_msb_c(cond, d, s, count) { \
333 emith_clear_msb(d, s, count); \
336 #define emith_sext(d, s, bits) { \
337 emith_lsl(d, s, 32 - (bits)); \
338 emith_asr(d, d, 32 - (bits)); \
341 #define emith_setc(r) { \
343 EMIT_OP_MODRM(0x92, 3, 0, r); /* SETC r */ \
347 #define emith_mul_(op, dlo, dhi, s1, s2) { \
349 if (dlo != xAX && dhi != xAX) \
351 if (dlo != xDX && dhi != xDX) \
355 else if ((s2) == xAX) \
358 emith_move_r_r(xAX, s1); \
361 EMIT_OP_MODRM(0xf7, 3, op, rmr); /* xMUL rmr */ \
362 /* XXX: using push/pop for the case of edx->eax; eax->edx */ \
363 if (dhi != xDX && dhi != -1) \
366 emith_move_r_r(dlo, xAX); \
367 if (dhi != xDX && dhi != -1) \
369 if (dlo != xDX && dhi != xDX) \
371 if (dlo != xAX && dhi != xAX) \
375 #define emith_mul_u64(dlo, dhi, s1, s2) \
376 emith_mul_(4, dlo, dhi, s1, s2) /* MUL */
378 #define emith_mul_s64(dlo, dhi, s1, s2) \
379 emith_mul_(5, dlo, dhi, s1, s2) /* IMUL */
381 #define emith_mul(d, s1, s2) \
382 emith_mul_(4, d, -1, s1, s2)
384 // (dlo,dhi) += signed(s1) * signed(s2)
385 #define emith_mula_s64(dlo, dhi, s1, s2) { \
388 emith_mul_(5, dlo, dhi, s1, s2); \
389 EMIT_OP_MODRM(0x03, 0, dlo, 4); \
390 EMIT_SIB(0, 4, 4); /* add dlo, [esp] */ \
391 EMIT_OP_MODRM(0x13, 1, dhi, 4); \
393 EMIT(4, u8); /* adc dhi, [esp+4] */ \
394 emith_add_r_imm(xSP, 4*2); \
397 // "flag" instructions are the same
398 #define emith_subf_r_imm emith_sub_r_imm
399 #define emith_addf_r_r emith_add_r_r
400 #define emith_subf_r_r emith_sub_r_r
401 #define emith_adcf_r_r emith_adc_r_r
402 #define emith_sbcf_r_r emith_sbc_r_r
403 #define emith_eorf_r_r emith_eor_r_r
404 #define emith_negcf_r_r emith_negc_r_r
406 #define emith_lslf emith_lsl
407 #define emith_lsrf emith_lsr
408 #define emith_asrf emith_asr
409 #define emith_rolf emith_rol
410 #define emith_rorf emith_ror
411 #define emith_rolcf emith_rolc
412 #define emith_rorcf emith_rorc
414 #define emith_deref_op(op, r, rs, offs) do { \
415 /* mov r <-> [ebp+#offs] */ \
416 if ((offs) >= 0x80) { \
417 EMIT_OP_MODRM(op, 2, r, rs); \
420 EMIT_OP_MODRM(op, 1, r, rs); \
425 #define is_abcdx(r) (xAX <= (r) && (r) <= xDX)
427 #define emith_read_r_r_offs(r, rs, offs) \
428 emith_deref_op(0x8b, r, rs, offs)
430 #define emith_write_r_r_offs(r, rs, offs) \
431 emith_deref_op(0x89, r, rs, offs)
433 // note: don't use prefixes on this
434 #define emith_read8_r_r_offs(r, rs, offs) do { \
437 r_ = rcache_get_tmp(); \
438 emith_deref_op(0x8a, r_, rs, offs); \
440 emith_move_r_r(r, r_); \
441 rcache_free_tmp(r_); \
445 #define emith_write8_r_r_offs(r, rs, offs) do {\
447 if (!is_abcdx(r)) { \
448 r_ = rcache_get_tmp(); \
449 emith_move_r_r(r_, r); \
451 emith_deref_op(0x88, r_, rs, offs); \
453 rcache_free_tmp(r_); \
456 #define emith_read16_r_r_offs(r, rs, offs) { \
457 EMIT(0x66, u8); /* operand override */ \
458 emith_read_r_r_offs(r, rs, offs); \
461 #define emith_write16_r_r_offs(r, rs, offs) { \
463 emith_write_r_r_offs(r, rs, offs); \
466 #define emith_ctx_read(r, offs) \
467 emith_read_r_r_offs(r, CONTEXT_REG, offs)
469 #define emith_ctx_write(r, offs) \
470 emith_write_r_r_offs(r, CONTEXT_REG, offs)
472 #define emith_ctx_read_multiple(r, offs, cnt, tmpr) do { \
473 int r_ = r, offs_ = offs, cnt_ = cnt; \
474 for (; cnt_ > 0; r_++, offs_ += 4, cnt_--) \
475 emith_ctx_read(r_, offs_); \
478 #define emith_ctx_write_multiple(r, offs, cnt, tmpr) do { \
479 int r_ = r, offs_ = offs, cnt_ = cnt; \
480 for (; cnt_ > 0; r_++, offs_ += 4, cnt_--) \
481 emith_ctx_write(r_, offs_); \
484 // assumes EBX is free
485 #define emith_ret_to_ctx(offs) { \
487 emith_ctx_write(xBX, offs); \
490 #define emith_jump(ptr) { \
491 u32 disp = (u32)(ptr) - ((u32)tcache_ptr + 5); \
496 #define emith_jump_patchable(target) \
499 #define emith_jump_cond(cond, ptr) { \
500 u32 disp = (u32)(ptr) - ((u32)tcache_ptr + 6); \
502 EMIT_OP(0x80 | (cond)); \
506 #define emith_jump_cond_patchable(cond, target) \
507 emith_jump_cond(cond, target)
509 #define emith_jump_patch(ptr, target) do { \
510 u32 disp_ = (u32)(target) - ((u32)(ptr) + 4); \
511 u32 offs_ = (*(u8 *)(ptr) == 0x0f) ? 2 : 1; \
512 EMIT_PTR((u8 *)(ptr) + offs_, disp_ - offs_, u32); \
515 #define emith_jump_at(ptr, target) { \
516 u32 disp_ = (u32)(target) - ((u32)(ptr) + 5); \
517 EMIT_PTR(ptr, 0xe9, u8); \
518 EMIT_PTR((u8 *)(ptr) + 1, disp_, u32); \
521 #define emith_call(ptr) { \
522 u32 disp = (u32)(ptr) - ((u32)tcache_ptr + 5); \
527 #define emith_call_cond(cond, ptr) \
530 #define emith_call_reg(r) \
531 EMIT_OP_MODRM(0xff, 3, 2, r)
533 #define emith_call_ctx(offs) { \
534 EMIT_OP_MODRM(0xff, 2, 2, CONTEXT_REG); \
538 #define emith_ret() \
541 #define emith_jump_reg(r) \
542 EMIT_OP_MODRM(0xff, 3, 4, r)
544 #define emith_jump_ctx(offs) { \
545 EMIT_OP_MODRM(0xff, 2, 4, CONTEXT_REG); \
549 #define emith_push_ret()
551 #define emith_pop_and_ret() \
554 #define EMITH_JMP_START(cond) { \
558 #define EMITH_JMP_END(cond) \
559 JMP8_EMIT(cond, cond_ptr); \
562 #define EMITH_JMP3_START(cond) { \
563 u8 *cond_ptr, *else_ptr; \
566 #define EMITH_JMP3_MID(cond) \
567 JMP8_POS(else_ptr); \
568 JMP8_EMIT(cond, cond_ptr);
570 #define EMITH_JMP3_END() \
571 JMP8_EMIT_NC(else_ptr); \
574 // "simple" jump (no more then a few insns)
575 // ARM will use conditional instructions here
576 #define EMITH_SJMP_START EMITH_JMP_START
577 #define EMITH_SJMP_END EMITH_JMP_END
579 #define EMITH_SJMP3_START EMITH_JMP3_START
580 #define EMITH_SJMP3_MID EMITH_JMP3_MID
581 #define EMITH_SJMP3_END EMITH_JMP3_END
583 #define emith_pass_arg_r(arg, reg) { \
585 host_arg2reg(rd, arg); \
586 emith_move_r_r(rd, reg); \
589 #define emith_pass_arg_imm(arg, imm) { \
591 host_arg2reg(rd, arg); \
592 emith_move_r_imm(rd, imm); \
595 #define host_instructions_updated(base, end)
597 #define host_arg2reg(rd, arg) \
599 case 0: rd = xAX; break; \
600 case 1: rd = xDX; break; \
601 case 2: rd = xCX; break; \
604 /* SH2 drc specific */
605 #define emith_sh2_drc_entry() { \
612 #define emith_sh2_drc_exit() { \
620 // assumes EBX is free temporary
621 #define emith_sh2_wcall(a, tab, ret_ptr) { \
623 host_arg2reg(arg2_, 2); \
624 emith_lsr(xBX, a, SH2_WRITE_SHIFT); \
625 EMIT_OP_MODRM(0x8b, 0, xBX, 4); \
626 EMIT_SIB(2, xBX, tab); /* mov ebx, [tab + ebx * 4] */ \
627 emith_ctx_read(arg2_, offsetof(SH2, is_slave)); \
628 emith_push_imm((long)(ret_ptr)); \
629 emith_jump_reg(xBX); \
632 #define emith_sh2_dtbf_loop() { \
633 u8 *jmp0; /* negative cycles check */ \
634 u8 *jmp1; /* unsinged overflow check */ \
636 int tmp_ = rcache_get_tmp(); \
637 cr = rcache_get_reg(SHR_SR, RC_GR_RMW); \
638 rn = rcache_get_reg((op >> 8) & 0x0f, RC_GR_RMW);\
639 emith_sub_r_imm(rn, 1); \
640 emith_sub_r_imm(cr, (cycles+1) << 12); \
642 emith_asr(tmp_, cr, 2+12); \
643 JMP8_POS(jmp0); /* no negative cycles */ \
644 emith_move_r_imm(tmp_, 0); \
645 JMP8_EMIT(ICOND_JNS, jmp0); \
646 emith_and_r_imm(cr, 0xffe); \
647 emith_subf_r_r(rn, tmp_); \
648 JMP8_POS(jmp1); /* no overflow */ \
649 emith_neg_r(rn); /* count left */ \
650 emith_lsl(rn, rn, 2+12); \
651 emith_or_r_r(cr, rn); \
652 emith_or_r_imm(cr, 1); \
653 emith_move_r_imm(rn, 0); \
654 JMP8_EMIT(ICOND_JA, jmp1); \
655 rcache_free_tmp(tmp_); \
658 #define emith_write_sr(sr, srcr) { \
659 int tmp_ = rcache_get_tmp(); \
660 emith_clear_msb(tmp_, srcr, 22); \
661 emith_bic_r_imm(sr, 0x3ff); \
662 emith_or_r_r(sr, tmp_); \
663 rcache_free_tmp(tmp_); \
666 #define emith_tpop_carry(sr, is_sub) \
669 #define emith_tpush_carry(sr, is_sub) \
670 emith_adc_r_r(sr, sr)
674 * t = carry(Rn += Rm)
676 * t = carry(Rn -= Rm)
679 #define emith_sh2_div1_step(rn, rm, sr) { \
681 int tmp_ = rcache_get_tmp(); \
682 emith_eor_r_r(tmp_, tmp_); \
683 emith_tst_r_imm(sr, Q); /* if (Q ^ M) */ \
684 JMP8_POS(jmp0); /* je do_sub */ \
685 emith_add_r_r(rn, rm); \
686 JMP8_POS(jmp1); /* jmp done */ \
687 JMP8_EMIT(ICOND_JE, jmp0); /* do_sub: */ \
688 emith_sub_r_r(rn, rm); \
689 JMP8_EMIT_NC(jmp1); /* done: */ \
691 EMIT_OP_MODRM(0x31, 3, tmp_, sr); /* T = Q1 ^ Q2 */ \
692 rcache_free_tmp(tmp_); \