32x: drc: inline dispatcher and irq handling; do write-caused irqs
[picodrive.git] / cpu / drc / emit_x86.c
1 /*
2  * note:
3  *  temp registers must be eax-edx due to use of SETcc.
4  * note about silly things like emith_eor_r_r_r:
5  *  these are here because the compiler was designed
6  *  for ARM as it's primary target.
7  */
8 #include <stdarg.h>
9
10 enum { xAX = 0, xCX, xDX, xBX, xSP, xBP, xSI, xDI };
11
12 #define CONTEXT_REG xBP
13
14 #define ICOND_JO  0x00
15 #define ICOND_JNO 0x01
16 #define ICOND_JB  0x02
17 #define ICOND_JAE 0x03
18 #define ICOND_JE  0x04
19 #define ICOND_JNE 0x05
20 #define ICOND_JBE 0x06
21 #define ICOND_JA  0x07
22 #define ICOND_JS  0x08
23 #define ICOND_JNS 0x09
24 #define ICOND_JL  0x0c
25 #define ICOND_JGE 0x0d
26 #define ICOND_JLE 0x0e
27 #define ICOND_JG  0x0f
28
29 #define IOP_JMP   0xeb
30
31 // unified conditions (we just use rel8 jump instructions for x86)
32 #define DCOND_EQ ICOND_JE
33 #define DCOND_NE ICOND_JNE
34 #define DCOND_MI ICOND_JS      // MInus
35 #define DCOND_PL ICOND_JNS     // PLus or zero
36 #define DCOND_HI ICOND_JA      // higher (unsigned)
37 #define DCOND_HS ICOND_JAE     // higher || same (unsigned)
38 #define DCOND_LO ICOND_JB      // lower (unsigned)
39 #define DCOND_LS ICOND_JBE     // lower || same (unsigned)
40 #define DCOND_GE ICOND_JGE     // greater || equal (signed)
41 #define DCOND_GT ICOND_JG      // greater (signed)
42 #define DCOND_LE ICOND_JLE     // less || equal (signed)
43 #define DCOND_LT ICOND_JL      // less (signed)
44 #define DCOND_VS ICOND_JO      // oVerflow Set
45 #define DCOND_VC ICOND_JNO     // oVerflow Clear
46
47 #define EMIT_PTR(ptr, val, type) \
48         *(type *)(ptr) = val
49
50 #define EMIT(val, type) { \
51         EMIT_PTR(tcache_ptr, val, type); \
52         tcache_ptr += sizeof(type); \
53 }
54
55 #define EMIT_OP(op) { \
56         COUNT_OP; \
57         EMIT(op, u8); \
58 }
59
60 #define EMIT_MODRM(mod,r,rm) \
61         EMIT(((mod)<<6) | ((r)<<3) | (rm), u8)
62
63 #define EMIT_SIB(scale,index,base) \
64         EMIT(((scale)<<6) | ((index)<<3) | (base), u8)
65
66 #define EMIT_OP_MODRM(op,mod,r,rm) { \
67         EMIT_OP(op); \
68         EMIT_MODRM(mod, r, rm); \
69 }
70
71 #define JMP8_POS(ptr) \
72         ptr = tcache_ptr; \
73         tcache_ptr += 2
74
75 #define JMP8_EMIT(op, ptr) \
76         EMIT_PTR(ptr, 0x70|(op), u8); \
77         EMIT_PTR(ptr + 1, (tcache_ptr - (ptr+2)), u8)
78
79 #define JMP8_EMIT_NC(ptr) \
80         EMIT_PTR(ptr, IOP_JMP, u8); \
81         EMIT_PTR(ptr + 1, (tcache_ptr - (ptr+2)), u8)
82
83 // _r_r
84 #define emith_move_r_r(dst, src) \
85         EMIT_OP_MODRM(0x8b, 3, dst, src)
86
87 #define emith_add_r_r(d, s) \
88         EMIT_OP_MODRM(0x01, 3, s, d)
89
90 #define emith_sub_r_r(d, s) \
91         EMIT_OP_MODRM(0x29, 3, s, d)
92
93 #define emith_adc_r_r(d, s) \
94         EMIT_OP_MODRM(0x11, 3, s, d)
95
96 #define emith_sbc_r_r(d, s) \
97         EMIT_OP_MODRM(0x19, 3, s, d) /* SBB */
98
99 #define emith_or_r_r(d, s) \
100         EMIT_OP_MODRM(0x09, 3, s, d)
101
102 #define emith_and_r_r(d, s) \
103         EMIT_OP_MODRM(0x21, 3, s, d)
104
105 #define emith_eor_r_r(d, s) \
106         EMIT_OP_MODRM(0x31, 3, s, d) /* XOR */
107
108 #define emith_tst_r_r(d, s) \
109         EMIT_OP_MODRM(0x85, 3, s, d) /* TEST */
110
111 #define emith_cmp_r_r(d, s) \
112         EMIT_OP_MODRM(0x39, 3, s, d)
113
114 // fake teq - test equivalence - get_flags(d ^ s)
115 #define emith_teq_r_r(d, s) { \
116         emith_push(d); \
117         emith_eor_r_r(d, s); \
118         emith_pop(d); \
119 }
120
121 #define emith_mvn_r_r(d, s) { \
122         if (d != s) \
123                 emith_move_r_r(d, s); \
124         EMIT_OP_MODRM(0xf7, 3, 2, d); /* NOT d */ \
125 }
126
127 #define emith_negc_r_r(d, s) { \
128         int tmp_ = rcache_get_tmp(); \
129         emith_move_r_imm(tmp_, 0); \
130         emith_sbc_r_r(tmp_, s); \
131         emith_move_r_r(d, tmp_); \
132         rcache_free_tmp(tmp_); \
133 }
134
135 #define emith_neg_r_r(d, s) { \
136         if (d != s) \
137                 emith_move_r_r(d, s); \
138         EMIT_OP_MODRM(0xf7, 3, 3, d); /* NEG d */ \
139 }
140
141 // _r_r_r
142 #define emith_eor_r_r_r(d, s1, s2) { \
143         if (d == s1) { \
144                 emith_eor_r_r(d, s2); \
145         } else if (d == s2) { \
146                 emith_eor_r_r(d, s1); \
147         } else { \
148                 emith_move_r_r(d, s1); \
149                 emith_eor_r_r(d, s2); \
150         } \
151 }
152
153 // _r_r_shift
154 #define emith_or_r_r_lsl(d, s, lslimm) { \
155         int tmp_ = rcache_get_tmp(); \
156         emith_lsl(tmp_, s, lslimm); \
157         emith_or_r_r(d, tmp_); \
158         rcache_free_tmp(tmp_); \
159 }
160
161 // d != s
162 #define emith_eor_r_r_lsr(d, s, lsrimm) { \
163         emith_push(s); \
164         emith_lsr(s, s, lsrimm); \
165         emith_eor_r_r(d, s); \
166         emith_pop(s); \
167 }
168
169 // _r_imm
170 #define emith_move_r_imm(r, imm) { \
171         EMIT_OP(0xb8 + (r)); \
172         EMIT(imm, u32); \
173 }
174
175 #define emith_move_r_imm_s8(r, imm) \
176         emith_move_r_imm(r, (u32)(signed int)(signed char)(imm))
177
178 #define emith_arith_r_imm(op, r, imm) do { \
179         EMIT_OP_MODRM(0x81, 3, op, r); \
180         EMIT(imm, u32); \
181 } while (0)
182
183 // 2 - adc, 3 - sbb
184 #define emith_add_r_imm(r, imm) \
185         emith_arith_r_imm(0, r, imm)
186
187 #define emith_or_r_imm(r, imm) \
188         emith_arith_r_imm(1, r, imm)
189
190 #define emith_and_r_imm(r, imm) \
191         emith_arith_r_imm(4, r, imm)
192
193 #define emith_sub_r_imm(r, imm) \
194         emith_arith_r_imm(5, r, imm)
195
196 #define emith_eor_r_imm(r, imm) \
197         emith_arith_r_imm(6, r, imm)
198
199 #define emith_cmp_r_imm(r, imm) \
200         emith_arith_r_imm(7, r, imm)
201
202 #define emith_tst_r_imm(r, imm) do { \
203         EMIT_OP_MODRM(0xf7, 3, 0, r); \
204         EMIT(imm, u32); \
205 } while (0)
206
207 // fake
208 #define emith_bic_r_imm(r, imm) \
209         emith_arith_r_imm(4, r, ~(imm))
210
211 // fake conditionals (using SJMP instead)
212 #define emith_move_r_imm_c(cond, r, imm) { \
213         (void)(cond); \
214         emith_move_r_imm(r, imm); \
215 }
216
217 #define emith_add_r_imm_c(cond, r, imm) { \
218         (void)(cond); \
219         emith_add_r_imm(r, imm); \
220 }
221
222 #define emith_or_r_imm_c(cond, r, imm) { \
223         (void)(cond); \
224         emith_or_r_imm(r, imm); \
225 }
226
227 #define emith_eor_r_imm_c(cond, r, imm) { \
228         (void)(cond); \
229         emith_eor_r_imm(r, imm); \
230 }
231
232 #define emith_sub_r_imm_c(cond, r, imm) { \
233         (void)(cond); \
234         emith_sub_r_imm(r, imm); \
235 }
236
237 #define emith_bic_r_imm_c(cond, r, imm) { \
238         (void)(cond); \
239         emith_bic_r_imm(r, imm); \
240 }
241
242 #define emith_jump_reg_c(cond, r) emith_jump_reg(r)
243 #define emith_jump_ctx_c(cond, offs) emith_jump_ctx(offs)
244 #define emith_ret_c(cond) emith_ret()
245
246 // _r_r_imm
247 #define emith_add_r_r_imm(d, s, imm) { \
248         if (d != s) \
249                 emith_move_r_r(d, s); \
250         emith_add_r_imm(d, imm); \
251 }
252
253 #define emith_and_r_r_imm(d, s, imm) { \
254         if (d != s) \
255                 emith_move_r_r(d, s); \
256         emith_and_r_imm(d, imm); \
257 }
258
259 // shift
260 #define emith_shift(op, d, s, cnt) { \
261         if (d != s) \
262                 emith_move_r_r(d, s); \
263         EMIT_OP_MODRM(0xc1, 3, op, d); \
264         EMIT(cnt, u8); \
265 }
266
267 #define emith_lsl(d, s, cnt) \
268         emith_shift(4, d, s, cnt)
269
270 #define emith_lsr(d, s, cnt) \
271         emith_shift(5, d, s, cnt)
272
273 #define emith_asr(d, s, cnt) \
274         emith_shift(7, d, s, cnt)
275
276 #define emith_rol(d, s, cnt) \
277         emith_shift(0, d, s, cnt)
278
279 #define emith_ror(d, s, cnt) \
280         emith_shift(1, d, s, cnt)
281
282 #define emith_rolc(r) \
283         EMIT_OP_MODRM(0xd1, 3, 2, r)
284
285 #define emith_rorc(r) \
286         EMIT_OP_MODRM(0xd1, 3, 3, r)
287
288 // misc
289 #define emith_push(r) \
290         EMIT_OP(0x50 + (r))
291
292 #define emith_push_imm(imm) { \
293         EMIT_OP(0x68); \
294         EMIT(imm, u32); \
295 }
296
297 #define emith_pop(r) \
298         EMIT_OP(0x58 + (r))
299
300 #define emith_neg_r(r) \
301         EMIT_OP_MODRM(0xf7, 3, 3, r)
302
303 #define emith_clear_msb(d, s, count) { \
304         u32 t = (u32)-1; \
305         t >>= count; \
306         if (d != s) \
307                 emith_move_r_r(d, s); \
308         emith_and_r_imm(d, t); \
309 }
310
311 #define emith_clear_msb_c(cond, d, s, count) { \
312         (void)(cond); \
313         emith_clear_msb(d, s, count); \
314 }
315
316 #define emith_sext(d, s, bits) { \
317         emith_lsl(d, s, 32 - (bits)); \
318         emith_asr(d, d, 32 - (bits)); \
319 }
320
321 #define emith_setc(r) { \
322         EMIT_OP(0x0f); \
323         EMIT_OP_MODRM(0x92, 3, 0, r); /* SETC r */ \
324 }
325
326 // XXX: stupid mess
327 #define emith_mul_(op, dlo, dhi, s1, s2) { \
328         int rmr; \
329         if (dlo != xAX && dhi != xAX) \
330                 emith_push(xAX); \
331         if (dlo != xDX && dhi != xDX) \
332                 emith_push(xDX); \
333         if ((s1) == xAX) \
334                 rmr = s2; \
335         else if ((s2) == xAX) \
336                 rmr = s1; \
337         else { \
338                 emith_move_r_r(xAX, s1); \
339                 rmr = s2; \
340         } \
341         EMIT_OP_MODRM(0xf7, 3, op, rmr); /* xMUL rmr */ \
342         /* XXX: using push/pop for the case of edx->eax; eax->edx */ \
343         if (dhi != xDX && dhi != -1) \
344                 emith_push(xDX); \
345         if (dlo != xAX) \
346                 emith_move_r_r(dlo, xAX); \
347         if (dhi != xDX && dhi != -1) \
348                 emith_pop(dhi); \
349         if (dlo != xDX && dhi != xDX) \
350                 emith_pop(xDX); \
351         if (dlo != xAX && dhi != xAX) \
352                 emith_pop(xAX); \
353 }
354
355 #define emith_mul_u64(dlo, dhi, s1, s2) \
356         emith_mul_(4, dlo, dhi, s1, s2) /* MUL */
357
358 #define emith_mul_s64(dlo, dhi, s1, s2) \
359         emith_mul_(5, dlo, dhi, s1, s2) /* IMUL */
360
361 #define emith_mul(d, s1, s2) \
362         emith_mul_(4, d, -1, s1, s2)
363
364 // (dlo,dhi) += signed(s1) * signed(s2)
365 #define emith_mula_s64(dlo, dhi, s1, s2) { \
366         emith_push(dhi); \
367         emith_push(dlo); \
368         emith_mul_(5, dlo, dhi, s1, s2); \
369         EMIT_OP_MODRM(0x03, 0, dlo, 4); \
370         EMIT_SIB(0, 4, 4); /* add dlo, [esp] */ \
371         EMIT_OP_MODRM(0x13, 1, dhi, 4); \
372         EMIT_SIB(0, 4, 4); \
373         EMIT(4, u8); /* adc dhi, [esp+4] */ \
374         emith_add_r_imm(xSP, 4*2); \
375 }
376
377 // "flag" instructions are the same
378 #define emith_subf_r_imm emith_sub_r_imm
379 #define emith_addf_r_r   emith_add_r_r
380 #define emith_subf_r_r   emith_sub_r_r
381 #define emith_adcf_r_r   emith_adc_r_r
382 #define emith_sbcf_r_r   emith_sbc_r_r
383 #define emith_eorf_r_r   emith_eor_r_r
384 #define emith_negcf_r_r  emith_negc_r_r
385
386 #define emith_lslf  emith_lsl
387 #define emith_lsrf  emith_lsr
388 #define emith_asrf  emith_asr
389 #define emith_rolf  emith_rol
390 #define emith_rorf  emith_ror
391 #define emith_rolcf emith_rolc
392 #define emith_rorcf emith_rorc
393
394 #define emith_ctx_op(op, r, offs) do { \
395         /* mov r <-> [ebp+#offs] */ \
396         if ((offs) >= 0x80) { \
397                 EMIT_OP_MODRM(op, 2, r, xBP); \
398                 EMIT(offs, u32); \
399         } else { \
400                 EMIT_OP_MODRM(op, 1, r, xBP); \
401                 EMIT(offs, u8); \
402         } \
403 } while (0)
404
405 #define emith_ctx_read(r, offs) \
406         emith_ctx_op(0x8b, r, offs)
407
408 #define emith_ctx_write(r, offs) \
409         emith_ctx_op(0x89, r, offs)
410
411 #define emith_ctx_read_multiple(r, offs, cnt, tmpr) do { \
412         int r_ = r, offs_ = offs, cnt_ = cnt;     \
413         for (; cnt_ > 0; r_++, offs_ += 4, cnt_--) \
414                 emith_ctx_read(r_, offs_);        \
415 } while (0)
416
417 #define emith_ctx_write_multiple(r, offs, cnt, tmpr) do { \
418         int r_ = r, offs_ = offs, cnt_ = cnt;     \
419         for (; cnt_ > 0; r_++, offs_ += 4, cnt_--) \
420                 emith_ctx_write(r_, offs_);       \
421 } while (0)
422
423 // assumes EBX is free
424 #define emith_ret_to_ctx(offs) { \
425         emith_pop(xBX); \
426         emith_ctx_write(xBX, offs); \
427 }
428
429 #define emith_jump(ptr) { \
430         u32 disp = (u32)(ptr) - ((u32)tcache_ptr + 5); \
431         EMIT_OP(0xe9); \
432         EMIT(disp, u32); \
433 }
434
435 #define emith_jump_cond(cond, ptr) { \
436         u32 disp = (u32)(ptr) - ((u32)tcache_ptr + 6); \
437         EMIT(0x0f, u8); \
438         EMIT_OP(0x80 | (cond)); \
439         EMIT(disp, u32); \
440 }
441
442 #define emith_jump_patchable(cond) \
443         emith_jump_cond(cond, 0)
444
445 #define emith_jump_patch(ptr, target) do { \
446         u32 disp = (u32)(target) - ((u32)(ptr) + 6); \
447         EMIT_PTR((u8 *)(ptr) + 2, disp, u32); \
448 } while (0)
449
450 #define emith_call(ptr) { \
451         u32 disp = (u32)(ptr) - ((u32)tcache_ptr + 5); \
452         EMIT_OP(0xe8); \
453         EMIT(disp, u32); \
454 }
455
456 #define emith_call_cond(cond, ptr) \
457         emith_call(ptr)
458
459 #define emith_call_reg(r) \
460         EMIT_OP_MODRM(0xff, 3, 2, r)
461
462 #define emith_call_ctx(offs) { \
463         EMIT_OP_MODRM(0xff, 2, 2, xBP); \
464         EMIT(offs, u32); \
465 }
466
467 #define emith_ret() \
468         EMIT_OP(0xc3)
469
470 #define emith_jump_reg(r) \
471         EMIT_OP_MODRM(0xff, 3, 4, r)
472
473 #define emith_jump_ctx(offs) { \
474         EMIT_OP_MODRM(0xff, 2, 4, xBP); \
475         EMIT(offs, u32); \
476 }
477
478 #define EMITH_JMP_START(cond) { \
479         u8 *cond_ptr; \
480         JMP8_POS(cond_ptr)
481
482 #define EMITH_JMP_END(cond) \
483         JMP8_EMIT(cond, cond_ptr); \
484 }
485
486 // "simple" jump (no more then a few insns)
487 #define EMITH_SJMP_START EMITH_JMP_START
488 #define EMITH_SJMP_END EMITH_JMP_END
489
490 #define host_arg2reg(rd, arg) \
491         switch (arg) { \
492         case 0: rd = xAX; break; \
493         case 1: rd = xDX; break; \
494         case 2: rd = xCX; break; \
495         }
496
497 #define emith_pass_arg_r(arg, reg) { \
498         int rd = 7; \
499         host_arg2reg(rd, arg); \
500         emith_move_r_r(rd, reg); \
501 }
502
503 #define emith_pass_arg_imm(arg, imm) { \
504         int rd = 7; \
505         host_arg2reg(rd, arg); \
506         emith_move_r_imm(rd, imm); \
507 }
508
509 /* SH2 drc specific */
510 #define emith_sh2_drc_entry() { \
511         emith_push(xBX);        \
512         emith_push(xBP);        \
513         emith_push(xSI);        \
514         emith_push(xDI);        \
515 }
516
517 #define emith_sh2_drc_exit() {  \
518         emith_pop(xDI);         \
519         emith_pop(xSI);         \
520         emith_pop(xBP);         \
521         emith_pop(xBX);         \
522         emith_ret();            \
523 }
524
525 // assumes EBX is free temporary
526 #define emith_sh2_wcall(a, tab, ret_ptr) { \
527         int arg2_; \
528         host_arg2reg(arg2_, 2); \
529         emith_lsr(xBX, a, SH2_WRITE_SHIFT); \
530         EMIT_OP_MODRM(0x8b, 0, xBX, 4); \
531         EMIT_SIB(2, xBX, tab); /* mov ebx, [tab + ebx * 4] */ \
532         emith_ctx_read(arg2_, offsetof(SH2, is_slave)); \
533         emith_push_imm((long)(ret_ptr)); \
534         emith_jump_reg(xBX); \
535 }
536
537 #define emith_sh2_dtbf_loop() { \
538         u8 *jmp0; /* negative cycles check */            \
539         u8 *jmp1; /* unsinged overflow check */          \
540         int cr, rn;                                      \
541         int tmp_ = rcache_get_tmp();                     \
542         cr = rcache_get_reg(SHR_SR, RC_GR_RMW);          \
543         rn = rcache_get_reg((op >> 8) & 0x0f, RC_GR_RMW);\
544         emith_sub_r_imm(rn, 1);                          \
545         emith_sub_r_imm(cr, (cycles+1) << 12);           \
546         cycles = 0;                                      \
547         emith_asr(tmp_, cr, 2+12);                       \
548         JMP8_POS(jmp0); /* no negative cycles */         \
549         emith_move_r_imm(tmp_, 0);                       \
550         JMP8_EMIT(ICOND_JNS, jmp0);                      \
551         emith_and_r_imm(cr, 0xffe);                      \
552         emith_subf_r_r(rn, tmp_);                        \
553         JMP8_POS(jmp1); /* no overflow */                \
554         emith_neg_r(rn); /* count left */                \
555         emith_lsl(rn, rn, 2+12);                         \
556         emith_or_r_r(cr, rn);                            \
557         emith_or_r_imm(cr, 1);                           \
558         emith_move_r_imm(rn, 0);                         \
559         JMP8_EMIT(ICOND_JA, jmp1);                       \
560         rcache_free_tmp(tmp_);                           \
561 }
562
563 #define emith_write_sr(sr, srcr) { \
564         int tmp_ = rcache_get_tmp(); \
565         emith_clear_msb(tmp_, srcr, 22); \
566         emith_bic_r_imm(sr, 0x3ff); \
567         emith_or_r_r(sr, tmp_); \
568         rcache_free_tmp(tmp_); \
569 }
570
571 #define emith_tpop_carry(sr, is_sub) \
572         emith_lsr(sr, sr, 1)
573
574 #define emith_tpush_carry(sr, is_sub) \
575         emith_adc_r_r(sr, sr)
576
577 /*
578  * if Q
579  *   t = carry(Rn += Rm)
580  * else
581  *   t = carry(Rn -= Rm)
582  * T ^= t
583  */
584 #define emith_sh2_div1_step(rn, rm, sr) {         \
585         u8 *jmp0, *jmp1;                          \
586         int tmp_ = rcache_get_tmp();              \
587         emith_eor_r_r(tmp_, tmp_);                \
588         emith_tst_r_imm(sr, Q);  /* if (Q ^ M) */ \
589         JMP8_POS(jmp0);          /* je do_sub */  \
590         emith_add_r_r(rn, rm);                    \
591         JMP8_POS(jmp1);          /* jmp done */   \
592         JMP8_EMIT(ICOND_JE, jmp0); /* do_sub: */  \
593         emith_sub_r_r(rn, rm);                    \
594         JMP8_EMIT_NC(jmp1);      /* done: */      \
595         emith_setc(tmp_);                         \
596         EMIT_OP_MODRM(0x31, 3, tmp_, sr); /* T = Q1 ^ Q2 */ \
597         rcache_free_tmp(tmp_);                    \
598 }
599