1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 * Mupen64plus - new_dynarec.c *
3 * Copyright (C) 2009-2011 Ari64 *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
22 #include <stdint.h> //include for uint64_t
27 #include <libkern/OSCacheControl.h>
30 #include <3ds_utils.h>
37 #include "new_dynarec_config.h"
38 #include "../psxhle.h"
39 #include "../psxinterpreter.h"
41 #include "emu_if.h" // emulator interface
42 #include "arm_features.h"
44 #define unused __attribute__((unused))
46 #define noinline __attribute__((noinline))
48 #define noinline __attribute__((noinline,noclone))
51 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
54 #define min(a, b) ((b) < (a) ? (b) : (a))
57 #define max(a, b) ((b) > (a) ? (b) : (a))
66 #define assem_debug printf
68 #define assem_debug(...)
70 //#define inv_debug printf
71 #define inv_debug(...)
74 #include "assem_x86.h"
77 #include "assem_x64.h"
80 #include "assem_arm.h"
83 #include "assem_arm64.h"
86 #define RAM_SIZE 0x200000
88 #define MAX_OUTPUT_BLOCK_SIZE 262144
89 #define EXPIRITY_OFFSET (MAX_OUTPUT_BLOCK_SIZE * 2)
90 #define PAGE_COUNT 1024
92 #if defined(HAVE_CONDITIONAL_CALL) && !defined(DESTRUCTIVE_SHIFT)
93 #define INVALIDATE_USE_COND_CALL
97 // apparently Vita has a 16MB limit, so either we cut tc in half,
98 // or use this hack (it's a hack because tc size was designed to be power-of-2)
99 #define TC_REDUCE_BYTES 4096
101 #define TC_REDUCE_BYTES 0
106 struct tramp_insns ops[2048 / sizeof(struct tramp_insns)];
107 const void *f[2048 / sizeof(void *)];
112 u_char translation_cache[(1 << TARGET_SIZE_2) - TC_REDUCE_BYTES];
113 struct ndrc_tramp tramp;
116 #ifdef BASE_ADDR_DYNAMIC
117 static struct ndrc_mem *ndrc;
119 static struct ndrc_mem ndrc_ __attribute__((aligned(4096)));
120 static struct ndrc_mem *ndrc = &ndrc_;
122 #ifdef TC_WRITE_OFFSET
124 # include <sys/types.h>
125 # include <sys/stat.h>
129 static long ndrc_write_ofs;
130 #define NDRC_WRITE_OFFSET(x) (void *)((char *)(x) + ndrc_write_ofs)
132 #define NDRC_WRITE_OFFSET(x) (x)
153 // regmap_pre[i] - regs before [i] insn starts; dirty things here that
154 // don't match .regmap will be written back
155 // [i].regmap_entry - regs that must be set up if someone jumps here
156 // [i].regmap - regs [i] insn will read/(over)write
157 // branch_regs[i].* - same as above but for branches, takes delay slot into account
160 signed char regmap_entry[HOST_REGS];
161 signed char regmap[HOST_REGS];
165 u_int wasconst; // before; for example 'lw r2, (r2)' wasconst is true
166 u_int isconst; // ... but isconst is false when r2 is known
167 u_int loadedconst; // host regs that have constants loaded
168 //u_int waswritten; // MIPS regs that were used as store base before
198 struct block_info *next;
201 u_int start; // vaddr of the block start
202 u_int len; // of the whole block source
207 u_char inv_near_misses;
225 static struct decoded_insn
245 static struct ht_entry hash_table[65536];
246 static struct block_info *blocks[PAGE_COUNT];
247 static struct jump_info *jumps[PAGE_COUNT];
249 static u_int *source;
250 static uint64_t gte_rs[MAXBLOCK]; // gte: 32 data and 32 ctl regs
251 static uint64_t gte_rt[MAXBLOCK];
252 static uint64_t gte_unneeded[MAXBLOCK];
253 static u_int smrv[32]; // speculated MIPS register values
254 static u_int smrv_strong; // mask or regs that are likely to have correct values
255 static u_int smrv_weak; // same, but somewhat less likely
256 static u_int smrv_strong_next; // same, but after current insn executes
257 static u_int smrv_weak_next;
258 static int imm[MAXBLOCK];
259 static u_int ba[MAXBLOCK];
260 static uint64_t unneeded_reg[MAXBLOCK];
261 static uint64_t branch_unneeded_reg[MAXBLOCK];
262 // see 'struct regstat' for a description
263 static signed char regmap_pre[MAXBLOCK][HOST_REGS];
264 // contains 'real' consts at [i] insn, but may differ from what's actually
265 // loaded in host reg as 'final' value is always loaded, see get_final_value()
266 static uint32_t current_constmap[HOST_REGS];
267 static uint32_t constmap[MAXBLOCK][HOST_REGS];
268 static struct regstat regs[MAXBLOCK];
269 static struct regstat branch_regs[MAXBLOCK];
270 static signed char minimum_free_regs[MAXBLOCK];
271 static int ccadj[MAXBLOCK];
273 static void *instr_addr[MAXBLOCK];
274 static struct link_entry link_addr[MAXBLOCK];
275 static int linkcount;
276 static struct code_stub stubs[MAXBLOCK*3];
277 static int stubcount;
278 static u_int literals[1024][2];
279 static int literalcount;
280 static int is_delayslot;
281 static char shadow[1048576] __attribute__((aligned(16)));
283 static u_int expirep;
284 static u_int stop_after_jal;
285 static u_int f1_hack;
287 static int stat_bc_direct;
288 static int stat_bc_pre;
289 static int stat_bc_restore;
290 static int stat_ht_lookups;
291 static int stat_jump_in_lookups;
292 static int stat_restore_tries;
293 static int stat_restore_compares;
294 static int stat_inv_addr_calls;
295 static int stat_inv_hits;
296 static int stat_blocks;
297 static int stat_links;
298 #define stat_inc(s) s++
299 #define stat_dec(s) s--
300 #define stat_clear(s) s = 0
304 #define stat_clear(s)
307 int new_dynarec_hacks;
308 int new_dynarec_hacks_pergame;
309 int new_dynarec_hacks_old;
310 int new_dynarec_did_compile;
312 #define HACK_ENABLED(x) ((new_dynarec_hacks | new_dynarec_hacks_pergame) & (x))
314 extern int cycle_count; // ... until end of the timeslice, counts -N -> 0
315 extern int last_count; // last absolute target, often = next_interupt
317 extern int pending_exception;
318 extern int branch_target;
319 extern uintptr_t ram_offset;
320 extern uintptr_t mini_ht[32][2];
322 /* registers that may be allocated */
324 #define LOREG 32 // lo
325 #define HIREG 33 // hi
326 //#define FSREG 34 // FPU status (FCSR)
327 #define CSREG 35 // Coprocessor status
328 #define CCREG 36 // Cycle count
329 #define INVCP 37 // Pointer to invalid_code
330 //#define MMREG 38 // Pointer to memory_map
331 #define ROREG 39 // ram offset (if rdram!=0x80000000)
333 #define FTEMP 40 // FPU temporary register
334 #define PTEMP 41 // Prefetch temporary register
335 //#define TLREG 42 // TLB mapping offset
336 #define RHASH 43 // Return address hash
337 #define RHTBL 44 // Return address hash table address
338 #define RTEMP 45 // JR/JALR address register
340 #define AGEN1 46 // Address generation temporary register
341 //#define AGEN2 47 // Address generation temporary register
342 //#define MGEN1 48 // Maptable address generation temporary register
343 //#define MGEN2 49 // Maptable address generation temporary register
344 #define BTREG 50 // Branch target temporary register
346 /* instruction types */
347 #define NOP 0 // No operation
348 #define LOAD 1 // Load
349 #define STORE 2 // Store
350 #define LOADLR 3 // Unaligned load
351 #define STORELR 4 // Unaligned store
352 #define MOV 5 // Move
353 #define ALU 6 // Arithmetic/logic
354 #define MULTDIV 7 // Multiply/divide
355 #define SHIFT 8 // Shift by register
356 #define SHIFTIMM 9// Shift by immediate
357 #define IMM16 10 // 16-bit immediate
358 #define RJUMP 11 // Unconditional jump to register
359 #define UJUMP 12 // Unconditional jump
360 #define CJUMP 13 // Conditional branch (BEQ/BNE/BGTZ/BLEZ)
361 #define SJUMP 14 // Conditional branch (regimm format)
362 #define COP0 15 // Coprocessor 0
363 #define COP1 16 // Coprocessor 1
364 #define C1LS 17 // Coprocessor 1 load/store
365 //#define FJUMP 18 // Conditional branch (floating point)
366 //#define FLOAT 19 // Floating point unit
367 //#define FCONV 20 // Convert integer to float
368 //#define FCOMP 21 // Floating point compare (sets FSREG)
369 #define SYSCALL 22// SYSCALL,BREAK
370 #define OTHER 23 // Other
371 //#define SPAN 24 // Branch/delay slot spans 2 pages
372 #define NI 25 // Not implemented
373 #define HLECALL 26// PCSX fake opcodes for HLE
374 #define COP2 27 // Coprocessor 2 move
375 #define C2LS 28 // Coprocessor 2 load/store
376 #define C2OP 29 // Coprocessor 2 operation
377 #define INTCALL 30// Call interpreter to handle rare corner cases
384 #define DJT_1 (void *)1l // no function, just a label in assem_debug log
385 #define DJT_2 (void *)2l
391 void fp_exception_ds();
392 void jump_syscall (u_int u0, u_int u1, u_int pc);
393 void jump_syscall_ds(u_int u0, u_int u1, u_int pc);
394 void jump_break (u_int u0, u_int u1, u_int pc);
395 void jump_break_ds(u_int u0, u_int u1, u_int pc);
396 void jump_to_new_pc();
397 void call_gteStall();
398 void new_dyna_leave();
400 void *ndrc_get_addr_ht_param(u_int vaddr, int can_compile);
401 void *ndrc_get_addr_ht(u_int vaddr);
402 void ndrc_add_jump_out(u_int vaddr, void *src);
403 void ndrc_write_invalidate_one(u_int addr);
404 static void ndrc_write_invalidate_many(u_int addr, u_int end);
406 static int new_recompile_block(u_int addr);
407 static void invalidate_block(struct block_info *block);
409 // Needed by assembler
410 static void wb_register(signed char r, const signed char regmap[], uint64_t dirty);
411 static void wb_dirtys(const signed char i_regmap[], uint64_t i_dirty);
412 static void wb_needed_dirtys(const signed char i_regmap[], uint64_t i_dirty, int addr);
413 static void load_all_regs(const signed char i_regmap[]);
414 static void load_needed_regs(const signed char i_regmap[], const signed char next_regmap[]);
415 static void load_regs_entry(int t);
416 static void load_all_consts(const signed char regmap[], u_int dirty, int i);
417 static u_int get_host_reglist(const signed char *regmap);
419 static int get_final_value(int hr, int i, int *value);
420 static void add_stub(enum stub_type type, void *addr, void *retaddr,
421 u_int a, uintptr_t b, uintptr_t c, u_int d, u_int e);
422 static void add_stub_r(enum stub_type type, void *addr, void *retaddr,
423 int i, int addr_reg, const struct regstat *i_regs, int ccadj, u_int reglist);
424 static void add_to_linker(void *addr, u_int target, int ext);
425 static void *emit_fastpath_cmp_jump(int i, const struct regstat *i_regs,
426 int addr, int *offset_reg, int *addr_reg_override);
427 static void *get_direct_memhandler(void *table, u_int addr,
428 enum stub_type type, uintptr_t *addr_host);
429 static void cop2_do_stall_check(u_int op, int i, const struct regstat *i_regs, u_int reglist);
430 static void pass_args(int a0, int a1);
431 static void emit_far_jump(const void *f);
432 static void emit_far_call(const void *f);
435 #include <psp2/kernel/sysmem.h>
437 // note: this interacts with RetroArch's Vita bootstrap code: bootstrap/vita/sbrk.c
438 extern int getVMBlock();
439 int _newlib_vm_size_user = sizeof(*ndrc);
442 static void mprotect_w_x(void *start, void *end, int is_x)
446 // *Open* enables write on all memory that was
447 // allocated by sceKernelAllocMemBlockForVM()?
449 sceKernelCloseVMDomain();
451 sceKernelOpenVMDomain();
452 #elif defined(HAVE_LIBNX)
454 // check to avoid the full flush in jitTransitionToExecutable()
455 if (g_jit.type != JitType_CodeMemory) {
457 rc = jitTransitionToExecutable(&g_jit);
459 rc = jitTransitionToWritable(&g_jit);
461 ;//SysPrintf("jitTransition %d %08x\n", is_x, rc);
463 #elif defined(TC_WRITE_OFFSET)
464 // separated rx and rw areas are always available
466 u_long mstart = (u_long)start & ~4095ul;
467 u_long mend = (u_long)end;
468 if (mprotect((void *)mstart, mend - mstart,
469 PROT_READ | (is_x ? PROT_EXEC : PROT_WRITE)) != 0)
470 SysPrintf("mprotect(%c) failed: %s\n", is_x ? 'x' : 'w', strerror(errno));
475 static void start_tcache_write(void *start, void *end)
477 mprotect_w_x(start, end, 0);
480 static void end_tcache_write(void *start, void *end)
482 #if defined(__arm__) || defined(__aarch64__)
483 size_t len = (char *)end - (char *)start;
484 #if defined(__BLACKBERRY_QNX__)
485 msync(start, len, MS_SYNC | MS_CACHE_ONLY | MS_INVALIDATE_ICACHE);
486 #elif defined(__MACH__)
487 sys_cache_control(kCacheFunctionPrepareForExecution, start, len);
489 sceKernelSyncVMDomain(sceBlock, start, len);
491 ctr_flush_invalidate_cache();
492 #elif defined(HAVE_LIBNX)
493 if (g_jit.type == JitType_CodeMemory) {
494 armDCacheClean(start, len);
495 armICacheInvalidate((char *)start - ndrc_write_ofs, len);
496 // as of v4.2.1 libnx lacks isb
497 __asm__ volatile("isb" ::: "memory");
499 #elif defined(__aarch64__)
500 // as of 2021, __clear_cache() is still broken on arm64
501 // so here is a custom one :(
502 clear_cache_arm64(start, end);
504 __clear_cache(start, end);
509 mprotect_w_x(start, end, 1);
512 static void *start_block(void)
514 u_char *end = out + MAX_OUTPUT_BLOCK_SIZE;
515 if (end > ndrc->translation_cache + sizeof(ndrc->translation_cache))
516 end = ndrc->translation_cache + sizeof(ndrc->translation_cache);
517 start_tcache_write(NDRC_WRITE_OFFSET(out), NDRC_WRITE_OFFSET(end));
521 static void end_block(void *start)
523 end_tcache_write(NDRC_WRITE_OFFSET(start), NDRC_WRITE_OFFSET(out));
526 #ifdef NDRC_CACHE_FLUSH_ALL
528 static int needs_clear_cache;
530 static void mark_clear_cache(void *target)
532 if (!needs_clear_cache) {
533 start_tcache_write(NDRC_WRITE_OFFSET(ndrc), NDRC_WRITE_OFFSET(ndrc + 1));
534 needs_clear_cache = 1;
538 static void do_clear_cache(void)
540 if (needs_clear_cache) {
541 end_tcache_write(NDRC_WRITE_OFFSET(ndrc), NDRC_WRITE_OFFSET(ndrc + 1));
542 needs_clear_cache = 0;
548 // also takes care of w^x mappings when patching code
549 static u_int needs_clear_cache[1<<(TARGET_SIZE_2-17)];
551 static void mark_clear_cache(void *target)
553 uintptr_t offset = (u_char *)target - ndrc->translation_cache;
554 u_int mask = 1u << ((offset >> 12) & 31);
555 if (!(needs_clear_cache[offset >> 17] & mask)) {
556 char *start = (char *)NDRC_WRITE_OFFSET((uintptr_t)target & ~4095l);
557 start_tcache_write(start, start + 4095);
558 needs_clear_cache[offset >> 17] |= mask;
562 // Clearing the cache is rather slow on ARM Linux, so mark the areas
563 // that need to be cleared, and then only clear these areas once.
564 static void do_clear_cache(void)
567 for (i = 0; i < (1<<(TARGET_SIZE_2-17)); i++)
569 u_int bitmap = needs_clear_cache[i];
572 for (j = 0; j < 32; j++)
575 if (!(bitmap & (1u << j)))
578 start = ndrc->translation_cache + i*131072 + j*4096;
580 for (j++; j < 32; j++) {
581 if (!(bitmap & (1u << j)))
585 end_tcache_write(NDRC_WRITE_OFFSET(start), NDRC_WRITE_OFFSET(end));
587 needs_clear_cache[i] = 0;
591 #endif // NDRC_CACHE_FLUSH_ALL
593 #define NO_CYCLE_PENALTY_THR 12
595 int cycle_multiplier_old;
596 static int cycle_multiplier_active;
598 static int CLOCK_ADJUST(int x)
600 int m = cycle_multiplier_active;
601 int s = (x >> 31) | 1;
602 return (x * m + s * 50) / 100;
605 static int ds_writes_rjump_rs(int i)
607 return dops[i].rs1 != 0 && (dops[i].rs1 == dops[i+1].rt1 || dops[i].rs1 == dops[i+1].rt2);
610 // psx addr mirror masking (for invalidation)
611 static u_int pmmask(u_int vaddr)
613 vaddr &= ~0xe0000000;
614 if (vaddr < 0x01000000)
615 vaddr &= ~0x00e00000; // RAM mirrors
619 static u_int get_page(u_int vaddr)
621 u_int page = pmmask(vaddr) >> 12;
622 if (page >= PAGE_COUNT / 2)
623 page = PAGE_COUNT / 2 + (page & (PAGE_COUNT / 2 - 1));
627 // get a page for looking for a block that has vaddr
628 // (needed because the block may start in previous page)
629 static u_int get_page_prev(u_int vaddr)
631 assert(MAXBLOCK <= (1 << 12));
632 u_int page = get_page(vaddr);
638 static struct ht_entry *hash_table_get(u_int vaddr)
640 return &hash_table[((vaddr>>16)^vaddr)&0xFFFF];
643 static void hash_table_add(u_int vaddr, void *tcaddr)
645 struct ht_entry *ht_bin = hash_table_get(vaddr);
647 ht_bin->vaddr[1] = ht_bin->vaddr[0];
648 ht_bin->tcaddr[1] = ht_bin->tcaddr[0];
649 ht_bin->vaddr[0] = vaddr;
650 ht_bin->tcaddr[0] = tcaddr;
653 static void hash_table_remove(int vaddr)
655 //printf("remove hash: %x\n",vaddr);
656 struct ht_entry *ht_bin = hash_table_get(vaddr);
657 if (ht_bin->vaddr[1] == vaddr) {
658 ht_bin->vaddr[1] = -1;
659 ht_bin->tcaddr[1] = NULL;
661 if (ht_bin->vaddr[0] == vaddr) {
662 ht_bin->vaddr[0] = ht_bin->vaddr[1];
663 ht_bin->tcaddr[0] = ht_bin->tcaddr[1];
664 ht_bin->vaddr[1] = -1;
665 ht_bin->tcaddr[1] = NULL;
669 static void mark_invalid_code(u_int vaddr, u_int len, char invalid)
671 u_int vaddr_m = vaddr & 0x1fffffff;
673 for (i = vaddr_m & ~0xfff; i < vaddr_m + len; i += 0x1000) {
674 // ram mirrors, but should not hurt bios
675 for (j = 0; j < 0x800000; j += 0x200000) {
676 invalid_code[(i|j) >> 12] =
677 invalid_code[(i|j|0x80000000u) >> 12] =
678 invalid_code[(i|j|0xa0000000u) >> 12] = invalid;
681 if (!invalid && vaddr + len > inv_code_start && vaddr <= inv_code_end)
682 inv_code_start = inv_code_end = ~0;
685 static int doesnt_expire_soon(u_char *tcaddr)
687 u_int diff = (u_int)(tcaddr - out) & ((1u << TARGET_SIZE_2) - 1u);
688 return diff > EXPIRITY_OFFSET + MAX_OUTPUT_BLOCK_SIZE;
691 static unused void check_for_block_changes(u_int start, u_int end)
693 u_int start_page = get_page_prev(start);
694 u_int end_page = get_page(end - 1);
697 for (page = start_page; page <= end_page; page++) {
698 struct block_info *block;
699 for (block = blocks[page]; block != NULL; block = block->next) {
702 if (memcmp(block->source, block->copy, block->len)) {
703 printf("bad block %08x-%08x %016llx %016llx @%08x\n",
704 block->start, block->start + block->len,
705 *(long long *)block->source, *(long long *)block->copy, psxRegs.pc);
713 static void *try_restore_block(u_int vaddr, u_int start_page, u_int end_page)
715 void *found_clean = NULL;
718 stat_inc(stat_restore_tries);
719 for (page = start_page; page <= end_page; page++) {
720 struct block_info *block;
721 for (block = blocks[page]; block != NULL; block = block->next) {
722 if (vaddr < block->start)
724 if (!block->is_dirty || vaddr >= block->start + block->len)
726 for (i = 0; i < block->jump_in_cnt; i++)
727 if (block->jump_in[i].vaddr == vaddr)
729 if (i == block->jump_in_cnt)
731 assert(block->source && block->copy);
732 stat_inc(stat_restore_compares);
733 if (memcmp(block->source, block->copy, block->len))
736 block->is_dirty = block->inv_near_misses = 0;
737 found_clean = block->jump_in[i].addr;
738 hash_table_add(vaddr, found_clean);
739 mark_invalid_code(block->start, block->len, 0);
740 stat_inc(stat_bc_restore);
741 inv_debug("INV: restored %08x %p (%d)\n", vaddr, found_clean, block->jump_in_cnt);
748 // Get address from virtual address
749 // This is called from the recompiled JR/JALR instructions
750 static void noinline *get_addr(u_int vaddr, int can_compile)
752 u_int start_page = get_page_prev(vaddr);
753 u_int i, page, end_page = get_page(vaddr);
754 void *found_clean = NULL;
756 stat_inc(stat_jump_in_lookups);
757 for (page = start_page; page <= end_page; page++) {
758 const struct block_info *block;
759 for (block = blocks[page]; block != NULL; block = block->next) {
760 if (vaddr < block->start)
762 if (block->is_dirty || vaddr >= block->start + block->len)
764 for (i = 0; i < block->jump_in_cnt; i++)
765 if (block->jump_in[i].vaddr == vaddr)
767 if (i == block->jump_in_cnt)
769 found_clean = block->jump_in[i].addr;
770 hash_table_add(vaddr, found_clean);
774 found_clean = try_restore_block(vaddr, start_page, end_page);
781 int r = new_recompile_block(vaddr);
783 return ndrc_get_addr_ht(vaddr);
785 // generate an address error
787 Cause=(vaddr<<31)|(4<<2);
788 EPC=(vaddr&1)?vaddr-5:vaddr;
790 return ndrc_get_addr_ht(0x80000080);
793 // Look up address in hash table first
794 void *ndrc_get_addr_ht_param(u_int vaddr, int can_compile)
796 //check_for_block_changes(vaddr, vaddr + MAXBLOCK);
797 const struct ht_entry *ht_bin = hash_table_get(vaddr);
798 stat_inc(stat_ht_lookups);
799 if (ht_bin->vaddr[0] == vaddr) return ht_bin->tcaddr[0];
800 if (ht_bin->vaddr[1] == vaddr) return ht_bin->tcaddr[1];
801 return get_addr(vaddr, can_compile);
804 void *ndrc_get_addr_ht(u_int vaddr)
806 return ndrc_get_addr_ht_param(vaddr, 1);
809 static void clear_all_regs(signed char regmap[])
811 memset(regmap, -1, sizeof(regmap[0]) * HOST_REGS);
814 // get_reg: get allocated host reg from mips reg
815 // returns -1 if no such mips reg was allocated
816 #if defined(__arm__) && defined(HAVE_ARMV6) && HOST_REGS == 13 && EXCLUDE_REG == 11
818 extern signed char get_reg(const signed char regmap[], signed char r);
822 static signed char get_reg(const signed char regmap[], signed char r)
825 for (hr = 0; hr < HOST_REGS; hr++) {
826 if (hr == EXCLUDE_REG)
836 // get reg as mask bit (1 << hr)
837 static u_int get_regm(const signed char regmap[], signed char r)
839 return (1u << (get_reg(regmap, r) & 31)) & ~(1u << 31);
842 static signed char get_reg_temp(const signed char regmap[])
845 for (hr = 0; hr < HOST_REGS; hr++) {
846 if (hr == EXCLUDE_REG)
848 if (regmap[hr] == (signed char)-1)
854 // Find a register that is available for two consecutive cycles
855 static signed char get_reg2(signed char regmap1[], const signed char regmap2[], int r)
858 for (hr=0;hr<HOST_REGS;hr++) if(hr!=EXCLUDE_REG&®map1[hr]==r&®map2[hr]==r) return hr;
862 // reverse reg map: mips -> host
863 #define RRMAP_SIZE 64
864 static void make_rregs(const signed char regmap[], signed char rrmap[RRMAP_SIZE],
865 u_int *regs_can_change)
867 u_int r, hr, hr_can_change = 0;
868 memset(rrmap, -1, RRMAP_SIZE);
869 for (hr = 0; hr < HOST_REGS; )
872 rrmap[r & (RRMAP_SIZE - 1)] = hr;
873 // only add mips $1-$31+$lo, others shifted out
874 hr_can_change |= (uint64_t)1 << (hr + ((r - 1) & 32));
876 if (hr == EXCLUDE_REG)
879 hr_can_change |= 1u << (rrmap[33] & 31);
880 hr_can_change |= 1u << (rrmap[CCREG] & 31);
881 hr_can_change &= ~(1u << 31);
882 *regs_can_change = hr_can_change;
885 // same as get_reg, but takes rrmap
886 static signed char get_rreg(signed char rrmap[RRMAP_SIZE], signed char r)
888 assert(0 <= r && r < RRMAP_SIZE);
892 static int count_free_regs(const signed char regmap[])
896 for(hr=0;hr<HOST_REGS;hr++)
898 if(hr!=EXCLUDE_REG) {
899 if(regmap[hr]<0) count++;
905 static void dirty_reg(struct regstat *cur, signed char reg)
909 hr = get_reg(cur->regmap, reg);
914 static void set_const(struct regstat *cur, signed char reg, uint32_t value)
918 hr = get_reg(cur->regmap, reg);
920 cur->isconst |= 1<<hr;
921 current_constmap[hr] = value;
925 static void clear_const(struct regstat *cur, signed char reg)
929 hr = get_reg(cur->regmap, reg);
931 cur->isconst &= ~(1<<hr);
934 static int is_const(const struct regstat *cur, signed char reg)
937 if (reg < 0) return 0;
939 hr = get_reg(cur->regmap, reg);
941 return (cur->isconst>>hr)&1;
945 static uint32_t get_const(const struct regstat *cur, signed char reg)
949 hr = get_reg(cur->regmap, reg);
951 return current_constmap[hr];
953 SysPrintf("Unknown constant in r%d\n", reg);
957 // Least soon needed registers
958 // Look at the next ten instructions and see which registers
959 // will be used. Try not to reallocate these.
960 static void lsn(u_char hsn[], int i, int *preferred_reg)
970 if (dops[i+j].is_ujump)
972 // Don't go past an unconditonal jump
979 if(dops[i+j].rs1) hsn[dops[i+j].rs1]=j;
980 if(dops[i+j].rs2) hsn[dops[i+j].rs2]=j;
981 if(dops[i+j].rt1) hsn[dops[i+j].rt1]=j;
982 if(dops[i+j].rt2) hsn[dops[i+j].rt2]=j;
983 if(dops[i+j].itype==STORE || dops[i+j].itype==STORELR) {
984 // Stores can allocate zero
985 hsn[dops[i+j].rs1]=j;
986 hsn[dops[i+j].rs2]=j;
988 if (ram_offset && (dops[i+j].is_load || dops[i+j].is_store))
990 // On some architectures stores need invc_ptr
991 #if defined(HOST_IMM8)
992 if (dops[i+j].is_store)
995 if(i+j>=0&&(dops[i+j].itype==UJUMP||dops[i+j].itype==CJUMP||dops[i+j].itype==SJUMP))
1003 if(ba[i+b]>=start && ba[i+b]<(start+slen*4))
1005 // Follow first branch
1006 int t=(ba[i+b]-start)>>2;
1007 j=7-b;if(t+j>=slen) j=slen-t-1;
1010 if(dops[t+j].rs1) if(hsn[dops[t+j].rs1]>j+b+2) hsn[dops[t+j].rs1]=j+b+2;
1011 if(dops[t+j].rs2) if(hsn[dops[t+j].rs2]>j+b+2) hsn[dops[t+j].rs2]=j+b+2;
1012 //if(dops[t+j].rt1) if(hsn[dops[t+j].rt1]>j+b+2) hsn[dops[t+j].rt1]=j+b+2;
1013 //if(dops[t+j].rt2) if(hsn[dops[t+j].rt2]>j+b+2) hsn[dops[t+j].rt2]=j+b+2;
1016 // TODO: preferred register based on backward branch
1018 // Delay slot should preferably not overwrite branch conditions or cycle count
1019 if (i > 0 && dops[i-1].is_jump) {
1020 if(dops[i-1].rs1) if(hsn[dops[i-1].rs1]>1) hsn[dops[i-1].rs1]=1;
1021 if(dops[i-1].rs2) if(hsn[dops[i-1].rs2]>1) hsn[dops[i-1].rs2]=1;
1023 // ...or hash tables
1027 // Coprocessor load/store needs FTEMP, even if not declared
1028 if(dops[i].itype==C2LS) {
1031 // Load L/R also uses FTEMP as a temporary register
1032 if(dops[i].itype==LOADLR) {
1035 // Also SWL/SWR/SDL/SDR
1036 if(dops[i].opcode==0x2a||dops[i].opcode==0x2e||dops[i].opcode==0x2c||dops[i].opcode==0x2d) {
1039 // Don't remove the miniht registers
1040 if(dops[i].itype==UJUMP||dops[i].itype==RJUMP)
1047 // We only want to allocate registers if we're going to use them again soon
1048 static int needed_again(int r, int i)
1054 if (i > 0 && dops[i-1].is_ujump)
1056 if(ba[i-1]<start || ba[i-1]>start+slen*4-4)
1057 return 0; // Don't need any registers if exiting the block
1065 if (dops[i+j].is_ujump)
1067 // Don't go past an unconditonal jump
1071 if(dops[i+j].itype==SYSCALL||dops[i+j].itype==HLECALL||dops[i+j].itype==INTCALL||((source[i+j]&0xfc00003f)==0x0d))
1078 if(dops[i+j].rs1==r) rn=j;
1079 if(dops[i+j].rs2==r) rn=j;
1080 if((unneeded_reg[i+j]>>r)&1) rn=10;
1081 if(i+j>=0&&(dops[i+j].itype==UJUMP||dops[i+j].itype==CJUMP||dops[i+j].itype==SJUMP))
1091 // Try to match register allocations at the end of a loop with those
1093 static int loop_reg(int i, int r, int hr)
1102 if (dops[i+j].is_ujump)
1104 // Don't go past an unconditonal jump
1111 if(dops[i-1].itype==UJUMP||dops[i-1].itype==CJUMP||dops[i-1].itype==SJUMP)
1117 if((unneeded_reg[i+k]>>r)&1) return hr;
1118 if(i+k>=0&&(dops[i+k].itype==UJUMP||dops[i+k].itype==CJUMP||dops[i+k].itype==SJUMP))
1120 if(ba[i+k]>=start && ba[i+k]<(start+i*4))
1122 int t=(ba[i+k]-start)>>2;
1123 int reg=get_reg(regs[t].regmap_entry,r);
1124 if(reg>=0) return reg;
1125 //reg=get_reg(regs[t+1].regmap_entry,r);
1126 //if(reg>=0) return reg;
1134 // Allocate every register, preserving source/target regs
1135 static void alloc_all(struct regstat *cur,int i)
1139 for(hr=0;hr<HOST_REGS;hr++) {
1140 if(hr!=EXCLUDE_REG) {
1141 if((cur->regmap[hr]!=dops[i].rs1)&&(cur->regmap[hr]!=dops[i].rs2)&&
1142 (cur->regmap[hr]!=dops[i].rt1)&&(cur->regmap[hr]!=dops[i].rt2))
1145 cur->dirty&=~(1<<hr);
1148 if(cur->regmap[hr]==0)
1151 cur->dirty&=~(1<<hr);
1158 static int host_tempreg_in_use;
1160 static void host_tempreg_acquire(void)
1162 assert(!host_tempreg_in_use);
1163 host_tempreg_in_use = 1;
1166 static void host_tempreg_release(void)
1168 host_tempreg_in_use = 0;
1171 static void host_tempreg_acquire(void) {}
1172 static void host_tempreg_release(void) {}
1176 extern void gen_interupt();
1177 extern void do_insn_cmp();
1178 #define FUNCNAME(f) { f, " " #f }
1179 static const struct {
1182 } function_names[] = {
1183 FUNCNAME(cc_interrupt),
1184 FUNCNAME(gen_interupt),
1185 FUNCNAME(ndrc_get_addr_ht),
1186 FUNCNAME(jump_handler_read8),
1187 FUNCNAME(jump_handler_read16),
1188 FUNCNAME(jump_handler_read32),
1189 FUNCNAME(jump_handler_write8),
1190 FUNCNAME(jump_handler_write16),
1191 FUNCNAME(jump_handler_write32),
1192 FUNCNAME(ndrc_write_invalidate_one),
1193 FUNCNAME(ndrc_write_invalidate_many),
1194 FUNCNAME(jump_to_new_pc),
1195 FUNCNAME(jump_break),
1196 FUNCNAME(jump_break_ds),
1197 FUNCNAME(jump_syscall),
1198 FUNCNAME(jump_syscall_ds),
1199 FUNCNAME(call_gteStall),
1200 FUNCNAME(new_dyna_leave),
1201 FUNCNAME(pcsx_mtc0),
1202 FUNCNAME(pcsx_mtc0_ds),
1204 FUNCNAME(do_insn_cmp),
1208 static const char *func_name(const void *a)
1211 for (i = 0; i < sizeof(function_names)/sizeof(function_names[0]); i++)
1212 if (function_names[i].addr == a)
1213 return function_names[i].name;
1217 #define func_name(x) ""
1221 #include "assem_x86.c"
1224 #include "assem_x64.c"
1227 #include "assem_arm.c"
1230 #include "assem_arm64.c"
1233 static void *get_trampoline(const void *f)
1235 struct ndrc_tramp *tramp = NDRC_WRITE_OFFSET(&ndrc->tramp);
1238 for (i = 0; i < ARRAY_SIZE(tramp->f); i++) {
1239 if (tramp->f[i] == f || tramp->f[i] == NULL)
1242 if (i == ARRAY_SIZE(tramp->f)) {
1243 SysPrintf("trampoline table is full, last func %p\n", f);
1246 if (tramp->f[i] == NULL) {
1247 start_tcache_write(&tramp->f[i], &tramp->f[i + 1]);
1249 end_tcache_write(&tramp->f[i], &tramp->f[i + 1]);
1251 // invalidate the RX mirror (unsure if necessary, but just in case...)
1252 armDCacheFlush(&ndrc->tramp.f[i], sizeof(ndrc->tramp.f[i]));
1255 return &ndrc->tramp.ops[i];
1258 static void emit_far_jump(const void *f)
1260 if (can_jump_or_call(f)) {
1265 f = get_trampoline(f);
1269 static void emit_far_call(const void *f)
1271 if (can_jump_or_call(f)) {
1276 f = get_trampoline(f);
1280 // Check if an address is already compiled
1281 // but don't return addresses which are about to expire from the cache
1282 static void *check_addr(u_int vaddr)
1284 struct ht_entry *ht_bin = hash_table_get(vaddr);
1286 for (i = 0; i < ARRAY_SIZE(ht_bin->vaddr); i++) {
1287 if (ht_bin->vaddr[i] == vaddr)
1288 if (doesnt_expire_soon(ht_bin->tcaddr[i]))
1289 return ht_bin->tcaddr[i];
1292 // refactor to get_addr_nocompile?
1293 u_int start_page = get_page_prev(vaddr);
1294 u_int page, end_page = get_page(vaddr);
1296 stat_inc(stat_jump_in_lookups);
1297 for (page = start_page; page <= end_page; page++) {
1298 const struct block_info *block;
1299 for (block = blocks[page]; block != NULL; block = block->next) {
1300 if (vaddr < block->start)
1302 if (block->is_dirty || vaddr >= block->start + block->len)
1304 if (!doesnt_expire_soon(ndrc->translation_cache + block->tc_offs))
1306 for (i = 0; i < block->jump_in_cnt; i++)
1307 if (block->jump_in[i].vaddr == vaddr)
1309 if (i == block->jump_in_cnt)
1312 // Update existing entry with current address
1313 void *addr = block->jump_in[i].addr;
1314 if (ht_bin->vaddr[0] == vaddr) {
1315 ht_bin->tcaddr[0] = addr;
1318 if (ht_bin->vaddr[1] == vaddr) {
1319 ht_bin->tcaddr[1] = addr;
1322 // Insert into hash table with low priority.
1323 // Don't evict existing entries, as they are probably
1324 // addresses that are being accessed frequently.
1325 if (ht_bin->vaddr[0] == -1) {
1326 ht_bin->vaddr[0] = vaddr;
1327 ht_bin->tcaddr[0] = addr;
1329 else if (ht_bin->vaddr[1] == -1) {
1330 ht_bin->vaddr[1] = vaddr;
1331 ht_bin->tcaddr[1] = addr;
1339 static void blocks_clear(struct block_info **head)
1341 struct block_info *cur, *next;
1343 if ((cur = *head)) {
1353 static int blocks_remove_matching_addrs(struct block_info **head,
1354 u_int base_offs, int shift)
1356 struct block_info *next;
1359 if ((((*head)->tc_offs ^ base_offs) >> shift) == 0) {
1360 inv_debug("EXP: rm block %08x (tc_offs %x)\n", (*head)->start, (*head)->tc_offs);
1361 invalidate_block(*head);
1362 next = (*head)->next;
1365 stat_dec(stat_blocks);
1370 head = &((*head)->next);
1376 // This is called when we write to a compiled block (see do_invstub)
1377 static void unlink_jumps_vaddr_range(u_int start, u_int end)
1379 u_int page, start_page = get_page(start), end_page = get_page(end - 1);
1382 for (page = start_page; page <= end_page; page++) {
1383 struct jump_info *ji = jumps[page];
1386 for (i = 0; i < ji->count; ) {
1387 if (ji->e[i].target_vaddr < start || ji->e[i].target_vaddr >= end) {
1392 inv_debug("INV: rm link to %08x (tc_offs %zx)\n", ji->e[i].target_vaddr,
1393 (u_char *)ji->e[i].stub - ndrc->translation_cache);
1394 void *host_addr = find_extjump_insn(ji->e[i].stub);
1395 mark_clear_cache(host_addr);
1396 set_jump_target(host_addr, ji->e[i].stub); // point back to dyna_linker stub
1398 stat_dec(stat_links);
1400 if (i < ji->count) {
1401 ji->e[i] = ji->e[ji->count];
1409 static void unlink_jumps_tc_range(struct jump_info *ji, u_int base_offs, int shift)
1414 for (i = 0; i < ji->count; ) {
1415 u_int tc_offs = (u_char *)ji->e[i].stub - ndrc->translation_cache;
1416 if (((tc_offs ^ base_offs) >> shift) != 0) {
1421 inv_debug("EXP: rm link to %08x (tc_offs %x)\n", ji->e[i].target_vaddr, tc_offs);
1422 stat_dec(stat_links);
1424 if (i < ji->count) {
1425 ji->e[i] = ji->e[ji->count];
1432 static void invalidate_block(struct block_info *block)
1436 block->is_dirty = 1;
1437 unlink_jumps_vaddr_range(block->start, block->start + block->len);
1438 for (i = 0; i < block->jump_in_cnt; i++)
1439 hash_table_remove(block->jump_in[i].vaddr);
1442 static int invalidate_range(u_int start, u_int end,
1443 u32 *inv_start_ret, u32 *inv_end_ret)
1445 struct block_info *last_block = NULL;
1446 u_int start_page = get_page_prev(start);
1447 u_int end_page = get_page(end - 1);
1448 u_int start_m = pmmask(start);
1449 u_int end_m = pmmask(end - 1);
1450 u_int inv_start, inv_end;
1451 u_int blk_start_m, blk_end_m;
1455 // additional area without code (to supplement invalid_code[]), [start, end)
1456 // avoids excessive ndrc_write_invalidate*() calls
1457 inv_start = start_m & ~0xfff;
1458 inv_end = end_m | 0xfff;
1460 for (page = start_page; page <= end_page; page++) {
1461 struct block_info *block;
1462 for (block = blocks[page]; block != NULL; block = block->next) {
1463 if (block->is_dirty)
1466 blk_end_m = pmmask(block->start + block->len);
1467 if (blk_end_m <= start_m) {
1468 inv_start = max(inv_start, blk_end_m);
1471 blk_start_m = pmmask(block->start);
1472 if (end_m <= blk_start_m) {
1473 inv_end = min(inv_end, blk_start_m - 1);
1476 if (!block->source) // "hack" block - leave it alone
1480 invalidate_block(block);
1481 stat_inc(stat_inv_hits);
1485 if (!hit && last_block && last_block->source) {
1486 // could be some leftover unused block, uselessly trapping writes
1487 last_block->inv_near_misses++;
1488 if (last_block->inv_near_misses > 128) {
1489 invalidate_block(last_block);
1490 stat_inc(stat_inv_hits);
1497 memset(mini_ht, -1, sizeof(mini_ht));
1501 if (inv_start <= (start_m & ~0xfff) && inv_end >= (start_m | 0xfff))
1502 // the whole page is empty now
1503 mark_invalid_code(start, 1, 1);
1505 if (inv_start_ret) *inv_start_ret = inv_start | (start & 0xe0000000);
1506 if (inv_end_ret) *inv_end_ret = inv_end | (end & 0xe0000000);
1510 void new_dynarec_invalidate_range(unsigned int start, unsigned int end)
1512 invalidate_range(start, end, NULL, NULL);
1515 static void ndrc_write_invalidate_many(u_int start, u_int end)
1517 // this check is done by the caller
1518 //if (inv_code_start<=addr&&addr<=inv_code_end) { rhits++; return; }
1519 int ret = invalidate_range(start, end, &inv_code_start, &inv_code_end);
1521 int invc = invalid_code[start >> 12];
1522 u_int len = end - start;
1524 printf("INV ADDR: %08x/%02x hit %d blocks\n", start, len, ret);
1526 printf("INV ADDR: %08x/%02x miss, inv %08x-%08x invc %d->%d\n", start, len,
1527 inv_code_start, inv_code_end, invc, invalid_code[start >> 12]);
1528 check_for_block_changes(start, end);
1530 stat_inc(stat_inv_addr_calls);
1534 void ndrc_write_invalidate_one(u_int addr)
1536 ndrc_write_invalidate_many(addr, addr + 4);
1539 // This is called when loading a save state.
1540 // Anything could have changed, so invalidate everything.
1541 void new_dynarec_invalidate_all_pages(void)
1543 struct block_info *block;
1545 for (page = 0; page < ARRAY_SIZE(blocks); page++) {
1546 for (block = blocks[page]; block != NULL; block = block->next) {
1547 if (block->is_dirty)
1549 if (!block->source) // hack block?
1551 invalidate_block(block);
1556 memset(mini_ht, -1, sizeof(mini_ht));
1561 // Add an entry to jump_out after making a link
1562 // src should point to code by emit_extjump()
1563 void ndrc_add_jump_out(u_int vaddr, void *src)
1565 inv_debug("ndrc_add_jump_out: %p -> %x\n", src, vaddr);
1566 u_int page = get_page(vaddr);
1567 struct jump_info *ji;
1569 stat_inc(stat_links);
1570 check_extjump2(src);
1573 ji = malloc(sizeof(*ji) + sizeof(ji->e[0]) * 16);
1577 else if (ji->count >= ji->alloc) {
1579 ji = realloc(ji, sizeof(*ji) + sizeof(ji->e[0]) * ji->alloc);
1582 ji->e[ji->count].target_vaddr = vaddr;
1583 ji->e[ji->count].stub = src;
1587 /* Register allocation */
1589 // Note: registers are allocated clean (unmodified state)
1590 // if you intend to modify the register, you must call dirty_reg().
1591 static void alloc_reg(struct regstat *cur,int i,signed char reg)
1594 int preferred_reg = PREFERRED_REG_FIRST
1595 + reg % (PREFERRED_REG_LAST - PREFERRED_REG_FIRST + 1);
1596 if (reg == CCREG) preferred_reg = HOST_CCREG;
1597 if (reg == PTEMP || reg == FTEMP) preferred_reg = 12;
1598 assert(PREFERRED_REG_FIRST != EXCLUDE_REG && EXCLUDE_REG != HOST_REGS);
1601 // Don't allocate unused registers
1602 if((cur->u>>reg)&1) return;
1604 // see if it's already allocated
1605 if (get_reg(cur->regmap, reg) >= 0)
1608 // Keep the same mapping if the register was already allocated in a loop
1609 preferred_reg = loop_reg(i,reg,preferred_reg);
1611 // Try to allocate the preferred register
1612 if(cur->regmap[preferred_reg]==-1) {
1613 cur->regmap[preferred_reg]=reg;
1614 cur->dirty&=~(1<<preferred_reg);
1615 cur->isconst&=~(1<<preferred_reg);
1618 r=cur->regmap[preferred_reg];
1621 cur->regmap[preferred_reg]=reg;
1622 cur->dirty&=~(1<<preferred_reg);
1623 cur->isconst&=~(1<<preferred_reg);
1627 // Clear any unneeded registers
1628 // We try to keep the mapping consistent, if possible, because it
1629 // makes branches easier (especially loops). So we try to allocate
1630 // first (see above) before removing old mappings. If this is not
1631 // possible then go ahead and clear out the registers that are no
1633 for(hr=0;hr<HOST_REGS;hr++)
1638 if((cur->u>>r)&1) {cur->regmap[hr]=-1;break;}
1642 // Try to allocate any available register, but prefer
1643 // registers that have not been used recently.
1645 for (hr = PREFERRED_REG_FIRST; ; ) {
1646 if (cur->regmap[hr] < 0) {
1647 int oldreg = regs[i-1].regmap[hr];
1648 if (oldreg < 0 || (oldreg != dops[i-1].rs1 && oldreg != dops[i-1].rs2
1649 && oldreg != dops[i-1].rt1 && oldreg != dops[i-1].rt2))
1651 cur->regmap[hr]=reg;
1652 cur->dirty&=~(1<<hr);
1653 cur->isconst&=~(1<<hr);
1658 if (hr == EXCLUDE_REG)
1660 if (hr == HOST_REGS)
1662 if (hr == PREFERRED_REG_FIRST)
1667 // Try to allocate any available register
1668 for (hr = PREFERRED_REG_FIRST; ; ) {
1669 if (cur->regmap[hr] < 0) {
1670 cur->regmap[hr]=reg;
1671 cur->dirty&=~(1<<hr);
1672 cur->isconst&=~(1<<hr);
1676 if (hr == EXCLUDE_REG)
1678 if (hr == HOST_REGS)
1680 if (hr == PREFERRED_REG_FIRST)
1684 // Ok, now we have to evict someone
1685 // Pick a register we hopefully won't need soon
1686 u_char hsn[MAXREG+1];
1687 memset(hsn,10,sizeof(hsn));
1689 lsn(hsn,i,&preferred_reg);
1690 //printf("eax=%d ecx=%d edx=%d ebx=%d ebp=%d esi=%d edi=%d\n",cur->regmap[0],cur->regmap[1],cur->regmap[2],cur->regmap[3],cur->regmap[5],cur->regmap[6],cur->regmap[7]);
1691 //printf("hsn(%x): %d %d %d %d %d %d %d\n",start+i*4,hsn[cur->regmap[0]&63],hsn[cur->regmap[1]&63],hsn[cur->regmap[2]&63],hsn[cur->regmap[3]&63],hsn[cur->regmap[5]&63],hsn[cur->regmap[6]&63],hsn[cur->regmap[7]&63]);
1693 // Don't evict the cycle count at entry points, otherwise the entry
1694 // stub will have to write it.
1695 if(dops[i].bt&&hsn[CCREG]>2) hsn[CCREG]=2;
1696 if (i>1 && hsn[CCREG] > 2 && dops[i-2].is_jump) hsn[CCREG]=2;
1699 // Alloc preferred register if available
1700 if(hsn[r=cur->regmap[preferred_reg]&63]==j) {
1701 for(hr=0;hr<HOST_REGS;hr++) {
1702 // Evict both parts of a 64-bit register
1703 if(cur->regmap[hr]==r) {
1705 cur->dirty&=~(1<<hr);
1706 cur->isconst&=~(1<<hr);
1709 cur->regmap[preferred_reg]=reg;
1712 for(r=1;r<=MAXREG;r++)
1714 if(hsn[r]==j&&r!=dops[i-1].rs1&&r!=dops[i-1].rs2&&r!=dops[i-1].rt1&&r!=dops[i-1].rt2) {
1715 for(hr=0;hr<HOST_REGS;hr++) {
1716 if(hr!=HOST_CCREG||j<hsn[CCREG]) {
1717 if(cur->regmap[hr]==r) {
1718 cur->regmap[hr]=reg;
1719 cur->dirty&=~(1<<hr);
1720 cur->isconst&=~(1<<hr);
1731 for(r=1;r<=MAXREG;r++)
1734 for(hr=0;hr<HOST_REGS;hr++) {
1735 if(cur->regmap[hr]==r) {
1736 cur->regmap[hr]=reg;
1737 cur->dirty&=~(1<<hr);
1738 cur->isconst&=~(1<<hr);
1745 SysPrintf("This shouldn't happen (alloc_reg)");abort();
1748 // Allocate a temporary register. This is done without regard to
1749 // dirty status or whether the register we request is on the unneeded list
1750 // Note: This will only allocate one register, even if called multiple times
1751 static void alloc_reg_temp(struct regstat *cur,int i,signed char reg)
1754 int preferred_reg = -1;
1756 // see if it's already allocated
1757 for(hr=0;hr<HOST_REGS;hr++)
1759 if(hr!=EXCLUDE_REG&&cur->regmap[hr]==reg) return;
1762 // Try to allocate any available register
1763 for(hr=HOST_REGS-1;hr>=0;hr--) {
1764 if(hr!=EXCLUDE_REG&&cur->regmap[hr]==-1) {
1765 cur->regmap[hr]=reg;
1766 cur->dirty&=~(1<<hr);
1767 cur->isconst&=~(1<<hr);
1772 // Find an unneeded register
1773 for(hr=HOST_REGS-1;hr>=0;hr--)
1779 if(i==0||((unneeded_reg[i-1]>>r)&1)) {
1780 cur->regmap[hr]=reg;
1781 cur->dirty&=~(1<<hr);
1782 cur->isconst&=~(1<<hr);
1789 // Ok, now we have to evict someone
1790 // Pick a register we hopefully won't need soon
1791 // TODO: we might want to follow unconditional jumps here
1792 // TODO: get rid of dupe code and make this into a function
1793 u_char hsn[MAXREG+1];
1794 memset(hsn,10,sizeof(hsn));
1796 lsn(hsn,i,&preferred_reg);
1797 //printf("hsn: %d %d %d %d %d %d %d\n",hsn[cur->regmap[0]&63],hsn[cur->regmap[1]&63],hsn[cur->regmap[2]&63],hsn[cur->regmap[3]&63],hsn[cur->regmap[5]&63],hsn[cur->regmap[6]&63],hsn[cur->regmap[7]&63]);
1799 // Don't evict the cycle count at entry points, otherwise the entry
1800 // stub will have to write it.
1801 if(dops[i].bt&&hsn[CCREG]>2) hsn[CCREG]=2;
1802 if (i>1 && hsn[CCREG] > 2 && dops[i-2].is_jump) hsn[CCREG]=2;
1805 for(r=1;r<=MAXREG;r++)
1807 if(hsn[r]==j&&r!=dops[i-1].rs1&&r!=dops[i-1].rs2&&r!=dops[i-1].rt1&&r!=dops[i-1].rt2) {
1808 for(hr=0;hr<HOST_REGS;hr++) {
1809 if(hr!=HOST_CCREG||hsn[CCREG]>2) {
1810 if(cur->regmap[hr]==r) {
1811 cur->regmap[hr]=reg;
1812 cur->dirty&=~(1<<hr);
1813 cur->isconst&=~(1<<hr);
1824 for(r=1;r<=MAXREG;r++)
1827 for(hr=0;hr<HOST_REGS;hr++) {
1828 if(cur->regmap[hr]==r) {
1829 cur->regmap[hr]=reg;
1830 cur->dirty&=~(1<<hr);
1831 cur->isconst&=~(1<<hr);
1838 SysPrintf("This shouldn't happen");abort();
1841 static void mov_alloc(struct regstat *current,int i)
1843 if (dops[i].rs1 == HIREG || dops[i].rs1 == LOREG) {
1844 alloc_cc(current,i); // for stalls
1845 dirty_reg(current,CCREG);
1848 // Note: Don't need to actually alloc the source registers
1849 //alloc_reg(current,i,dops[i].rs1);
1850 alloc_reg(current,i,dops[i].rt1);
1852 clear_const(current,dops[i].rs1);
1853 clear_const(current,dops[i].rt1);
1854 dirty_reg(current,dops[i].rt1);
1857 static void shiftimm_alloc(struct regstat *current,int i)
1859 if(dops[i].opcode2<=0x3) // SLL/SRL/SRA
1862 if(dops[i].rs1&&needed_again(dops[i].rs1,i)) alloc_reg(current,i,dops[i].rs1);
1863 else dops[i].use_lt1=!!dops[i].rs1;
1864 alloc_reg(current,i,dops[i].rt1);
1865 dirty_reg(current,dops[i].rt1);
1866 if(is_const(current,dops[i].rs1)) {
1867 int v=get_const(current,dops[i].rs1);
1868 if(dops[i].opcode2==0x00) set_const(current,dops[i].rt1,v<<imm[i]);
1869 if(dops[i].opcode2==0x02) set_const(current,dops[i].rt1,(u_int)v>>imm[i]);
1870 if(dops[i].opcode2==0x03) set_const(current,dops[i].rt1,v>>imm[i]);
1872 else clear_const(current,dops[i].rt1);
1877 clear_const(current,dops[i].rs1);
1878 clear_const(current,dops[i].rt1);
1881 if(dops[i].opcode2>=0x38&&dops[i].opcode2<=0x3b) // DSLL/DSRL/DSRA
1885 if(dops[i].opcode2==0x3c) // DSLL32
1889 if(dops[i].opcode2==0x3e) // DSRL32
1893 if(dops[i].opcode2==0x3f) // DSRA32
1899 static void shift_alloc(struct regstat *current,int i)
1902 if(dops[i].opcode2<=0x07) // SLLV/SRLV/SRAV
1904 if(dops[i].rs1) alloc_reg(current,i,dops[i].rs1);
1905 if(dops[i].rs2) alloc_reg(current,i,dops[i].rs2);
1906 alloc_reg(current,i,dops[i].rt1);
1907 if(dops[i].rt1==dops[i].rs2) {
1908 alloc_reg_temp(current,i,-1);
1909 minimum_free_regs[i]=1;
1911 } else { // DSLLV/DSRLV/DSRAV
1914 clear_const(current,dops[i].rs1);
1915 clear_const(current,dops[i].rs2);
1916 clear_const(current,dops[i].rt1);
1917 dirty_reg(current,dops[i].rt1);
1921 static void alu_alloc(struct regstat *current,int i)
1923 if(dops[i].opcode2>=0x20&&dops[i].opcode2<=0x23) { // ADD/ADDU/SUB/SUBU
1925 if(dops[i].rs1&&dops[i].rs2) {
1926 alloc_reg(current,i,dops[i].rs1);
1927 alloc_reg(current,i,dops[i].rs2);
1930 if(dops[i].rs1&&needed_again(dops[i].rs1,i)) alloc_reg(current,i,dops[i].rs1);
1931 if(dops[i].rs2&&needed_again(dops[i].rs2,i)) alloc_reg(current,i,dops[i].rs2);
1933 alloc_reg(current,i,dops[i].rt1);
1936 if(dops[i].opcode2==0x2a||dops[i].opcode2==0x2b) { // SLT/SLTU
1938 alloc_reg(current,i,dops[i].rs1);
1939 alloc_reg(current,i,dops[i].rs2);
1940 alloc_reg(current,i,dops[i].rt1);
1943 if(dops[i].opcode2>=0x24&&dops[i].opcode2<=0x27) { // AND/OR/XOR/NOR
1945 if(dops[i].rs1&&dops[i].rs2) {
1946 alloc_reg(current,i,dops[i].rs1);
1947 alloc_reg(current,i,dops[i].rs2);
1951 if(dops[i].rs1&&needed_again(dops[i].rs1,i)) alloc_reg(current,i,dops[i].rs1);
1952 if(dops[i].rs2&&needed_again(dops[i].rs2,i)) alloc_reg(current,i,dops[i].rs2);
1954 alloc_reg(current,i,dops[i].rt1);
1957 if(dops[i].opcode2>=0x2c&&dops[i].opcode2<=0x2f) { // DADD/DADDU/DSUB/DSUBU
1960 clear_const(current,dops[i].rs1);
1961 clear_const(current,dops[i].rs2);
1962 clear_const(current,dops[i].rt1);
1963 dirty_reg(current,dops[i].rt1);
1966 static void imm16_alloc(struct regstat *current,int i)
1968 if(dops[i].rs1&&needed_again(dops[i].rs1,i)) alloc_reg(current,i,dops[i].rs1);
1969 else dops[i].use_lt1=!!dops[i].rs1;
1970 if(dops[i].rt1) alloc_reg(current,i,dops[i].rt1);
1971 if(dops[i].opcode==0x18||dops[i].opcode==0x19) { // DADDI/DADDIU
1974 else if(dops[i].opcode==0x0a||dops[i].opcode==0x0b) { // SLTI/SLTIU
1975 clear_const(current,dops[i].rs1);
1976 clear_const(current,dops[i].rt1);
1978 else if(dops[i].opcode>=0x0c&&dops[i].opcode<=0x0e) { // ANDI/ORI/XORI
1979 if(is_const(current,dops[i].rs1)) {
1980 int v=get_const(current,dops[i].rs1);
1981 if(dops[i].opcode==0x0c) set_const(current,dops[i].rt1,v&imm[i]);
1982 if(dops[i].opcode==0x0d) set_const(current,dops[i].rt1,v|imm[i]);
1983 if(dops[i].opcode==0x0e) set_const(current,dops[i].rt1,v^imm[i]);
1985 else clear_const(current,dops[i].rt1);
1987 else if(dops[i].opcode==0x08||dops[i].opcode==0x09) { // ADDI/ADDIU
1988 if(is_const(current,dops[i].rs1)) {
1989 int v=get_const(current,dops[i].rs1);
1990 set_const(current,dops[i].rt1,v+imm[i]);
1992 else clear_const(current,dops[i].rt1);
1995 set_const(current,dops[i].rt1,imm[i]<<16); // LUI
1997 dirty_reg(current,dops[i].rt1);
2000 static void load_alloc(struct regstat *current,int i)
2002 clear_const(current,dops[i].rt1);
2003 //if(dops[i].rs1!=dops[i].rt1&&needed_again(dops[i].rs1,i)) clear_const(current,dops[i].rs1); // Does this help or hurt?
2004 if(!dops[i].rs1) current->u&=~1LL; // Allow allocating r0 if it's the source register
2005 if (needed_again(dops[i].rs1, i))
2006 alloc_reg(current, i, dops[i].rs1);
2008 alloc_reg(current, i, ROREG);
2009 if(dops[i].rt1&&!((current->u>>dops[i].rt1)&1)) {
2010 alloc_reg(current,i,dops[i].rt1);
2011 assert(get_reg(current->regmap,dops[i].rt1)>=0);
2012 if(dops[i].opcode==0x27||dops[i].opcode==0x37) // LWU/LD
2016 else if(dops[i].opcode==0x1A||dops[i].opcode==0x1B) // LDL/LDR
2020 dirty_reg(current,dops[i].rt1);
2021 // LWL/LWR need a temporary register for the old value
2022 if(dops[i].opcode==0x22||dops[i].opcode==0x26)
2024 alloc_reg(current,i,FTEMP);
2025 alloc_reg_temp(current,i,-1);
2026 minimum_free_regs[i]=1;
2031 // Load to r0 or unneeded register (dummy load)
2032 // but we still need a register to calculate the address
2033 if(dops[i].opcode==0x22||dops[i].opcode==0x26)
2035 alloc_reg(current,i,FTEMP); // LWL/LWR need another temporary
2037 alloc_reg_temp(current,i,-1);
2038 minimum_free_regs[i]=1;
2039 if(dops[i].opcode==0x1A||dops[i].opcode==0x1B) // LDL/LDR
2046 static void store_alloc(struct regstat *current,int i)
2048 clear_const(current,dops[i].rs2);
2049 if(!(dops[i].rs2)) current->u&=~1LL; // Allow allocating r0 if necessary
2050 if(needed_again(dops[i].rs1,i)) alloc_reg(current,i,dops[i].rs1);
2051 alloc_reg(current,i,dops[i].rs2);
2052 if(dops[i].opcode==0x2c||dops[i].opcode==0x2d||dops[i].opcode==0x3f) { // 64-bit SDL/SDR/SD
2056 alloc_reg(current, i, ROREG);
2057 #if defined(HOST_IMM8)
2058 // On CPUs without 32-bit immediates we need a pointer to invalid_code
2059 alloc_reg(current, i, INVCP);
2061 if(dops[i].opcode==0x2a||dops[i].opcode==0x2e||dops[i].opcode==0x2c||dops[i].opcode==0x2d) { // SWL/SWL/SDL/SDR
2062 alloc_reg(current,i,FTEMP);
2064 // We need a temporary register for address generation
2065 alloc_reg_temp(current,i,-1);
2066 minimum_free_regs[i]=1;
2069 static void c1ls_alloc(struct regstat *current,int i)
2071 clear_const(current,dops[i].rt1);
2072 alloc_reg(current,i,CSREG); // Status
2075 static void c2ls_alloc(struct regstat *current,int i)
2077 clear_const(current,dops[i].rt1);
2078 if(needed_again(dops[i].rs1,i)) alloc_reg(current,i,dops[i].rs1);
2079 alloc_reg(current,i,FTEMP);
2081 alloc_reg(current, i, ROREG);
2082 #if defined(HOST_IMM8)
2083 // On CPUs without 32-bit immediates we need a pointer to invalid_code
2084 if (dops[i].opcode == 0x3a) // SWC2
2085 alloc_reg(current,i,INVCP);
2087 // We need a temporary register for address generation
2088 alloc_reg_temp(current,i,-1);
2089 minimum_free_regs[i]=1;
2092 #ifndef multdiv_alloc
2093 static void multdiv_alloc(struct regstat *current,int i)
2100 // case 0x1D: DMULTU
2103 clear_const(current,dops[i].rs1);
2104 clear_const(current,dops[i].rs2);
2105 alloc_cc(current,i); // for stalls
2106 if(dops[i].rs1&&dops[i].rs2)
2108 if((dops[i].opcode2&4)==0) // 32-bit
2110 current->u&=~(1LL<<HIREG);
2111 current->u&=~(1LL<<LOREG);
2112 alloc_reg(current,i,HIREG);
2113 alloc_reg(current,i,LOREG);
2114 alloc_reg(current,i,dops[i].rs1);
2115 alloc_reg(current,i,dops[i].rs2);
2116 dirty_reg(current,HIREG);
2117 dirty_reg(current,LOREG);
2126 // Multiply by zero is zero.
2127 // MIPS does not have a divide by zero exception.
2128 // The result is undefined, we return zero.
2129 alloc_reg(current,i,HIREG);
2130 alloc_reg(current,i,LOREG);
2131 dirty_reg(current,HIREG);
2132 dirty_reg(current,LOREG);
2137 static void cop0_alloc(struct regstat *current,int i)
2139 if(dops[i].opcode2==0) // MFC0
2142 clear_const(current,dops[i].rt1);
2143 alloc_all(current,i);
2144 alloc_reg(current,i,dops[i].rt1);
2145 dirty_reg(current,dops[i].rt1);
2148 else if(dops[i].opcode2==4) // MTC0
2151 clear_const(current,dops[i].rs1);
2152 alloc_reg(current,i,dops[i].rs1);
2153 alloc_all(current,i);
2156 alloc_all(current,i); // FIXME: Keep r0
2158 alloc_reg(current,i,0);
2164 assert(dops[i].opcode2==0x10);
2165 alloc_all(current,i);
2167 minimum_free_regs[i]=HOST_REGS;
2170 static void cop2_alloc(struct regstat *current,int i)
2172 if (dops[i].opcode2 < 3) // MFC2/CFC2
2174 alloc_cc(current,i); // for stalls
2175 dirty_reg(current,CCREG);
2177 clear_const(current,dops[i].rt1);
2178 alloc_reg(current,i,dops[i].rt1);
2179 dirty_reg(current,dops[i].rt1);
2182 else if (dops[i].opcode2 > 3) // MTC2/CTC2
2185 clear_const(current,dops[i].rs1);
2186 alloc_reg(current,i,dops[i].rs1);
2190 alloc_reg(current,i,0);
2193 alloc_reg_temp(current,i,-1);
2194 minimum_free_regs[i]=1;
2197 static void c2op_alloc(struct regstat *current,int i)
2199 alloc_cc(current,i); // for stalls
2200 dirty_reg(current,CCREG);
2201 alloc_reg_temp(current,i,-1);
2204 static void syscall_alloc(struct regstat *current,int i)
2206 alloc_cc(current,i);
2207 dirty_reg(current,CCREG);
2208 alloc_all(current,i);
2209 minimum_free_regs[i]=HOST_REGS;
2213 static void delayslot_alloc(struct regstat *current,int i)
2215 switch(dops[i].itype) {
2223 imm16_alloc(current,i);
2227 load_alloc(current,i);
2231 store_alloc(current,i);
2234 alu_alloc(current,i);
2237 shift_alloc(current,i);
2240 multdiv_alloc(current,i);
2243 shiftimm_alloc(current,i);
2246 mov_alloc(current,i);
2249 cop0_alloc(current,i);
2254 cop2_alloc(current,i);
2257 c1ls_alloc(current,i);
2260 c2ls_alloc(current,i);
2263 c2op_alloc(current,i);
2268 static void add_stub(enum stub_type type, void *addr, void *retaddr,
2269 u_int a, uintptr_t b, uintptr_t c, u_int d, u_int e)
2271 assert(stubcount < ARRAY_SIZE(stubs));
2272 stubs[stubcount].type = type;
2273 stubs[stubcount].addr = addr;
2274 stubs[stubcount].retaddr = retaddr;
2275 stubs[stubcount].a = a;
2276 stubs[stubcount].b = b;
2277 stubs[stubcount].c = c;
2278 stubs[stubcount].d = d;
2279 stubs[stubcount].e = e;
2283 static void add_stub_r(enum stub_type type, void *addr, void *retaddr,
2284 int i, int addr_reg, const struct regstat *i_regs, int ccadj, u_int reglist)
2286 add_stub(type, addr, retaddr, i, addr_reg, (uintptr_t)i_regs, ccadj, reglist);
2289 // Write out a single register
2290 static void wb_register(signed char r, const signed char regmap[], uint64_t dirty)
2293 for(hr=0;hr<HOST_REGS;hr++) {
2294 if(hr!=EXCLUDE_REG) {
2297 assert(regmap[hr]<64);
2298 emit_storereg(r,hr);
2305 static void wb_valid(signed char pre[],signed char entry[],u_int dirty_pre,u_int dirty,uint64_t u)
2307 //if(dirty_pre==dirty) return;
2309 for (hr = 0; hr < HOST_REGS; hr++) {
2311 if (r < 1 || r > 33 || ((u >> r) & 1))
2313 if (((dirty_pre & ~dirty) >> hr) & 1)
2314 emit_storereg(r, hr);
2319 static void pass_args(int a0, int a1)
2323 emit_mov(a0,2); emit_mov(a1,1); emit_mov(2,0);
2325 else if(a0!=0&&a1==0) {
2327 if (a0>=0) emit_mov(a0,0);
2330 if(a0>=0&&a0!=0) emit_mov(a0,0);
2331 if(a1>=0&&a1!=1) emit_mov(a1,1);
2335 static void alu_assemble(int i, const struct regstat *i_regs)
2337 if(dops[i].opcode2>=0x20&&dops[i].opcode2<=0x23) { // ADD/ADDU/SUB/SUBU
2339 signed char s1,s2,t;
2340 t=get_reg(i_regs->regmap,dops[i].rt1);
2342 s1=get_reg(i_regs->regmap,dops[i].rs1);
2343 s2=get_reg(i_regs->regmap,dops[i].rs2);
2344 if(dops[i].rs1&&dops[i].rs2) {
2347 if(dops[i].opcode2&2) emit_sub(s1,s2,t);
2348 else emit_add(s1,s2,t);
2350 else if(dops[i].rs1) {
2351 if(s1>=0) emit_mov(s1,t);
2352 else emit_loadreg(dops[i].rs1,t);
2354 else if(dops[i].rs2) {
2356 if(dops[i].opcode2&2) emit_neg(s2,t);
2357 else emit_mov(s2,t);
2360 emit_loadreg(dops[i].rs2,t);
2361 if(dops[i].opcode2&2) emit_neg(t,t);
2364 else emit_zeroreg(t);
2368 if(dops[i].opcode2>=0x2c&&dops[i].opcode2<=0x2f) { // DADD/DADDU/DSUB/DSUBU
2371 if(dops[i].opcode2==0x2a||dops[i].opcode2==0x2b) { // SLT/SLTU
2373 signed char s1l,s2l,t;
2375 t=get_reg(i_regs->regmap,dops[i].rt1);
2378 s1l=get_reg(i_regs->regmap,dops[i].rs1);
2379 s2l=get_reg(i_regs->regmap,dops[i].rs2);
2380 if(dops[i].rs2==0) // rx<r0
2382 if(dops[i].opcode2==0x2a&&dops[i].rs1!=0) { // SLT
2384 emit_shrimm(s1l,31,t);
2386 else // SLTU (unsigned can not be less than zero, 0<0)
2389 else if(dops[i].rs1==0) // r0<rx
2392 if(dops[i].opcode2==0x2a) // SLT
2393 emit_set_gz32(s2l,t);
2394 else // SLTU (set if not zero)
2395 emit_set_nz32(s2l,t);
2398 assert(s1l>=0);assert(s2l>=0);
2399 if(dops[i].opcode2==0x2a) // SLT
2400 emit_set_if_less32(s1l,s2l,t);
2402 emit_set_if_carry32(s1l,s2l,t);
2408 if(dops[i].opcode2>=0x24&&dops[i].opcode2<=0x27) { // AND/OR/XOR/NOR
2410 signed char s1l,s2l,tl;
2411 tl=get_reg(i_regs->regmap,dops[i].rt1);
2414 s1l=get_reg(i_regs->regmap,dops[i].rs1);
2415 s2l=get_reg(i_regs->regmap,dops[i].rs2);
2416 if(dops[i].rs1&&dops[i].rs2) {
2419 if(dops[i].opcode2==0x24) { // AND
2420 emit_and(s1l,s2l,tl);
2422 if(dops[i].opcode2==0x25) { // OR
2423 emit_or(s1l,s2l,tl);
2425 if(dops[i].opcode2==0x26) { // XOR
2426 emit_xor(s1l,s2l,tl);
2428 if(dops[i].opcode2==0x27) { // NOR
2429 emit_or(s1l,s2l,tl);
2435 if(dops[i].opcode2==0x24) { // AND
2438 if(dops[i].opcode2==0x25||dops[i].opcode2==0x26) { // OR/XOR
2440 if(s1l>=0) emit_mov(s1l,tl);
2441 else emit_loadreg(dops[i].rs1,tl); // CHECK: regmap_entry?
2445 if(s2l>=0) emit_mov(s2l,tl);
2446 else emit_loadreg(dops[i].rs2,tl); // CHECK: regmap_entry?
2448 else emit_zeroreg(tl);
2450 if(dops[i].opcode2==0x27) { // NOR
2452 if(s1l>=0) emit_not(s1l,tl);
2454 emit_loadreg(dops[i].rs1,tl);
2460 if(s2l>=0) emit_not(s2l,tl);
2462 emit_loadreg(dops[i].rs2,tl);
2466 else emit_movimm(-1,tl);
2475 static void imm16_assemble(int i, const struct regstat *i_regs)
2477 if (dops[i].opcode==0x0f) { // LUI
2480 t=get_reg(i_regs->regmap,dops[i].rt1);
2483 if(!((i_regs->isconst>>t)&1))
2484 emit_movimm(imm[i]<<16,t);
2488 if(dops[i].opcode==0x08||dops[i].opcode==0x09) { // ADDI/ADDIU
2491 t=get_reg(i_regs->regmap,dops[i].rt1);
2492 s=get_reg(i_regs->regmap,dops[i].rs1);
2497 if(!((i_regs->isconst>>t)&1)) {
2499 if(i_regs->regmap_entry[t]!=dops[i].rs1) emit_loadreg(dops[i].rs1,t);
2500 emit_addimm(t,imm[i],t);
2502 if(!((i_regs->wasconst>>s)&1))
2503 emit_addimm(s,imm[i],t);
2505 emit_movimm(constmap[i][s]+imm[i],t);
2511 if(!((i_regs->isconst>>t)&1))
2512 emit_movimm(imm[i],t);
2517 if(dops[i].opcode==0x18||dops[i].opcode==0x19) { // DADDI/DADDIU
2520 tl=get_reg(i_regs->regmap,dops[i].rt1);
2521 sl=get_reg(i_regs->regmap,dops[i].rs1);
2525 emit_addimm(sl,imm[i],tl);
2527 emit_movimm(imm[i],tl);
2532 else if(dops[i].opcode==0x0a||dops[i].opcode==0x0b) { // SLTI/SLTIU
2534 //assert(dops[i].rs1!=0); // r0 might be valid, but it's probably a bug
2536 t=get_reg(i_regs->regmap,dops[i].rt1);
2537 sl=get_reg(i_regs->regmap,dops[i].rs1);
2541 if(dops[i].opcode==0x0a) { // SLTI
2543 if(i_regs->regmap_entry[t]!=dops[i].rs1) emit_loadreg(dops[i].rs1,t);
2544 emit_slti32(t,imm[i],t);
2546 emit_slti32(sl,imm[i],t);
2551 if(i_regs->regmap_entry[t]!=dops[i].rs1) emit_loadreg(dops[i].rs1,t);
2552 emit_sltiu32(t,imm[i],t);
2554 emit_sltiu32(sl,imm[i],t);
2558 // SLTI(U) with r0 is just stupid,
2559 // nonetheless examples can be found
2560 if(dops[i].opcode==0x0a) // SLTI
2561 if(0<imm[i]) emit_movimm(1,t);
2562 else emit_zeroreg(t);
2565 if(imm[i]) emit_movimm(1,t);
2566 else emit_zeroreg(t);
2572 else if(dops[i].opcode>=0x0c&&dops[i].opcode<=0x0e) { // ANDI/ORI/XORI
2575 tl=get_reg(i_regs->regmap,dops[i].rt1);
2576 sl=get_reg(i_regs->regmap,dops[i].rs1);
2577 if(tl>=0 && !((i_regs->isconst>>tl)&1)) {
2578 if(dops[i].opcode==0x0c) //ANDI
2582 if(i_regs->regmap_entry[tl]!=dops[i].rs1) emit_loadreg(dops[i].rs1,tl);
2583 emit_andimm(tl,imm[i],tl);
2585 if(!((i_regs->wasconst>>sl)&1))
2586 emit_andimm(sl,imm[i],tl);
2588 emit_movimm(constmap[i][sl]&imm[i],tl);
2598 if(i_regs->regmap_entry[tl]!=dops[i].rs1) emit_loadreg(dops[i].rs1,tl);
2600 if(dops[i].opcode==0x0d) { // ORI
2602 emit_orimm(tl,imm[i],tl);
2604 if(!((i_regs->wasconst>>sl)&1))
2605 emit_orimm(sl,imm[i],tl);
2607 emit_movimm(constmap[i][sl]|imm[i],tl);
2610 if(dops[i].opcode==0x0e) { // XORI
2612 emit_xorimm(tl,imm[i],tl);
2614 if(!((i_regs->wasconst>>sl)&1))
2615 emit_xorimm(sl,imm[i],tl);
2617 emit_movimm(constmap[i][sl]^imm[i],tl);
2622 emit_movimm(imm[i],tl);
2630 static void shiftimm_assemble(int i, const struct regstat *i_regs)
2632 if(dops[i].opcode2<=0x3) // SLL/SRL/SRA
2636 t=get_reg(i_regs->regmap,dops[i].rt1);
2637 s=get_reg(i_regs->regmap,dops[i].rs1);
2639 if(t>=0&&!((i_regs->isconst>>t)&1)){
2646 if(s<0&&i_regs->regmap_entry[t]!=dops[i].rs1) emit_loadreg(dops[i].rs1,t);
2648 if(dops[i].opcode2==0) // SLL
2650 emit_shlimm(s<0?t:s,imm[i],t);
2652 if(dops[i].opcode2==2) // SRL
2654 emit_shrimm(s<0?t:s,imm[i],t);
2656 if(dops[i].opcode2==3) // SRA
2658 emit_sarimm(s<0?t:s,imm[i],t);
2662 if(s>=0 && s!=t) emit_mov(s,t);
2666 //emit_storereg(dops[i].rt1,t); //DEBUG
2669 if(dops[i].opcode2>=0x38&&dops[i].opcode2<=0x3b) // DSLL/DSRL/DSRA
2673 if(dops[i].opcode2==0x3c) // DSLL32
2677 if(dops[i].opcode2==0x3e) // DSRL32
2681 if(dops[i].opcode2==0x3f) // DSRA32
2687 #ifndef shift_assemble
2688 static void shift_assemble(int i, const struct regstat *i_regs)
2690 signed char s,t,shift;
2691 if (dops[i].rt1 == 0)
2693 assert(dops[i].opcode2<=0x07); // SLLV/SRLV/SRAV
2694 t = get_reg(i_regs->regmap, dops[i].rt1);
2695 s = get_reg(i_regs->regmap, dops[i].rs1);
2696 shift = get_reg(i_regs->regmap, dops[i].rs2);
2702 else if(dops[i].rs2==0) {
2704 if(s!=t) emit_mov(s,t);
2707 host_tempreg_acquire();
2708 emit_andimm(shift,31,HOST_TEMPREG);
2709 switch(dops[i].opcode2) {
2711 emit_shl(s,HOST_TEMPREG,t);
2714 emit_shr(s,HOST_TEMPREG,t);
2717 emit_sar(s,HOST_TEMPREG,t);
2722 host_tempreg_release();
2736 static int get_ptr_mem_type(u_int a)
2738 if(a < 0x00200000) {
2739 if(a<0x1000&&((start>>20)==0xbfc||(start>>24)==0xa0))
2740 // return wrong, must use memhandler for BIOS self-test to pass
2741 // 007 does similar stuff from a00 mirror, weird stuff
2745 if(0x1f800000 <= a && a < 0x1f801000)
2747 if(0x80200000 <= a && a < 0x80800000)
2749 if(0xa0000000 <= a && a < 0xa0200000)
2754 static int get_ro_reg(const struct regstat *i_regs, int host_tempreg_free)
2756 int r = get_reg(i_regs->regmap, ROREG);
2757 if (r < 0 && host_tempreg_free) {
2758 host_tempreg_acquire();
2759 emit_loadreg(ROREG, r = HOST_TEMPREG);
2766 static void *emit_fastpath_cmp_jump(int i, const struct regstat *i_regs,
2767 int addr, int *offset_reg, int *addr_reg_override)
2771 int mr = dops[i].rs1;
2773 if(((smrv_strong|smrv_weak)>>mr)&1) {
2774 type=get_ptr_mem_type(smrv[mr]);
2775 //printf("set %08x @%08x r%d %d\n", smrv[mr], start+i*4, mr, type);
2778 // use the mirror we are running on
2779 type=get_ptr_mem_type(start);
2780 //printf("set nospec @%08x r%d %d\n", start+i*4, mr, type);
2783 if(type==MTYPE_8020) { // RAM 80200000+ mirror
2784 host_tempreg_acquire();
2785 emit_andimm(addr,~0x00e00000,HOST_TEMPREG);
2786 addr=*addr_reg_override=HOST_TEMPREG;
2789 else if(type==MTYPE_0000) { // RAM 0 mirror
2790 host_tempreg_acquire();
2791 emit_orimm(addr,0x80000000,HOST_TEMPREG);
2792 addr=*addr_reg_override=HOST_TEMPREG;
2795 else if(type==MTYPE_A000) { // RAM A mirror
2796 host_tempreg_acquire();
2797 emit_andimm(addr,~0x20000000,HOST_TEMPREG);
2798 addr=*addr_reg_override=HOST_TEMPREG;
2801 else if(type==MTYPE_1F80) { // scratchpad
2802 if (psxH == (void *)0x1f800000) {
2803 host_tempreg_acquire();
2804 emit_xorimm(addr,0x1f800000,HOST_TEMPREG);
2805 emit_cmpimm(HOST_TEMPREG,0x1000);
2806 host_tempreg_release();
2811 // do the usual RAM check, jump will go to the right handler
2816 if (type == 0) // need ram check
2818 emit_cmpimm(addr,RAM_SIZE);
2820 #ifdef CORTEX_A8_BRANCH_PREDICTION_HACK
2821 // Hint to branch predictor that the branch is unlikely to be taken
2822 if (dops[i].rs1 >= 28)
2823 emit_jno_unlikely(0);
2827 if (ram_offset != 0)
2828 *offset_reg = get_ro_reg(i_regs, 0);
2834 // return memhandler, or get directly accessable address and return 0
2835 static void *get_direct_memhandler(void *table, u_int addr,
2836 enum stub_type type, uintptr_t *addr_host)
2838 uintptr_t msb = 1ull << (sizeof(uintptr_t)*8 - 1);
2839 uintptr_t l1, l2 = 0;
2840 l1 = ((uintptr_t *)table)[addr>>12];
2842 uintptr_t v = l1 << 1;
2843 *addr_host = v + addr;
2848 if (type == LOADB_STUB || type == LOADBU_STUB || type == STOREB_STUB)
2849 l2 = ((uintptr_t *)l1)[0x1000/4 + 0x1000/2 + (addr&0xfff)];
2850 else if (type == LOADH_STUB || type == LOADHU_STUB || type == STOREH_STUB)
2851 l2 = ((uintptr_t *)l1)[0x1000/4 + (addr&0xfff)/2];
2853 l2 = ((uintptr_t *)l1)[(addr&0xfff)/4];
2855 uintptr_t v = l2 << 1;
2856 *addr_host = v + (addr&0xfff);
2859 return (void *)(l2 << 1);
2863 static u_int get_host_reglist(const signed char *regmap)
2865 u_int reglist = 0, hr;
2866 for (hr = 0; hr < HOST_REGS; hr++) {
2867 if (hr != EXCLUDE_REG && regmap[hr] >= 0)
2873 static u_int reglist_exclude(u_int reglist, int r1, int r2)
2876 reglist &= ~(1u << r1);
2878 reglist &= ~(1u << r2);
2882 // find a temp caller-saved register not in reglist (so assumed to be free)
2883 static int reglist_find_free(u_int reglist)
2885 u_int free_regs = ~reglist & CALLER_SAVE_REGS;
2888 return __builtin_ctz(free_regs);
2891 static void do_load_word(int a, int rt, int offset_reg)
2893 if (offset_reg >= 0)
2894 emit_ldr_dualindexed(offset_reg, a, rt);
2896 emit_readword_indexed(0, a, rt);
2899 static void do_store_word(int a, int ofs, int rt, int offset_reg, int preseve_a)
2901 if (offset_reg < 0) {
2902 emit_writeword_indexed(rt, ofs, a);
2906 emit_addimm(a, ofs, a);
2907 emit_str_dualindexed(offset_reg, a, rt);
2908 if (ofs != 0 && preseve_a)
2909 emit_addimm(a, -ofs, a);
2912 static void do_store_hword(int a, int ofs, int rt, int offset_reg, int preseve_a)
2914 if (offset_reg < 0) {
2915 emit_writehword_indexed(rt, ofs, a);
2919 emit_addimm(a, ofs, a);
2920 emit_strh_dualindexed(offset_reg, a, rt);
2921 if (ofs != 0 && preseve_a)
2922 emit_addimm(a, -ofs, a);
2925 static void do_store_byte(int a, int rt, int offset_reg)
2927 if (offset_reg >= 0)
2928 emit_strb_dualindexed(offset_reg, a, rt);
2930 emit_writebyte_indexed(rt, 0, a);
2933 static void load_assemble(int i, const struct regstat *i_regs, int ccadj_)
2938 int memtarget=0,c=0;
2939 int offset_reg = -1;
2940 int fastio_reg_override = -1;
2941 u_int reglist=get_host_reglist(i_regs->regmap);
2942 tl=get_reg(i_regs->regmap,dops[i].rt1);
2943 s=get_reg(i_regs->regmap,dops[i].rs1);
2945 if(i_regs->regmap[HOST_CCREG]==CCREG) reglist&=~(1<<HOST_CCREG);
2947 c=(i_regs->wasconst>>s)&1;
2949 memtarget=((signed int)(constmap[i][s]+offset))<(signed int)0x80000000+RAM_SIZE;
2952 //printf("load_assemble: c=%d\n",c);
2953 //if(c) printf("load_assemble: const=%lx\n",(long)constmap[i][s]+offset);
2954 // FIXME: Even if the load is a NOP, we should check for pagefaults...
2955 if((tl<0&&(!c||(((u_int)constmap[i][s]+offset)>>16)==0x1f80))
2957 // could be FIFO, must perform the read
2959 assem_debug("(forced read)\n");
2960 tl=get_reg_temp(i_regs->regmap);
2963 if(offset||s<0||c) addr=tl;
2965 //if(tl<0) tl=get_reg_temp(i_regs->regmap);
2967 //printf("load_assemble: c=%d\n",c);
2968 //if(c) printf("load_assemble: const=%lx\n",(long)constmap[i][s]+offset);
2969 assert(tl>=0); // Even if the load is a NOP, we must check for pagefaults and I/O
2973 // Strmnnrmn's speed hack
2974 if(dops[i].rs1!=29||start<0x80001000||start>=0x80000000+RAM_SIZE)
2977 jaddr = emit_fastpath_cmp_jump(i, i_regs, addr,
2978 &offset_reg, &fastio_reg_override);
2981 else if (ram_offset && memtarget) {
2982 offset_reg = get_ro_reg(i_regs, 0);
2984 int dummy=(dops[i].rt1==0)||(tl!=get_reg(i_regs->regmap,dops[i].rt1)); // ignore loads to r0 and unneeded reg
2985 switch (dops[i].opcode) {
2991 if (fastio_reg_override >= 0)
2992 a = fastio_reg_override;
2994 if (offset_reg >= 0)
2995 emit_ldrsb_dualindexed(offset_reg, a, tl);
2997 emit_movsbl_indexed(0, a, tl);
3000 add_stub_r(LOADB_STUB,jaddr,out,i,addr,i_regs,ccadj_,reglist);
3003 inline_readstub(LOADB_STUB,i,constmap[i][s]+offset,i_regs->regmap,dops[i].rt1,ccadj_,reglist);
3010 if (fastio_reg_override >= 0)
3011 a = fastio_reg_override;
3012 if (offset_reg >= 0)
3013 emit_ldrsh_dualindexed(offset_reg, a, tl);
3015 emit_movswl_indexed(0, a, tl);
3018 add_stub_r(LOADH_STUB,jaddr,out,i,addr,i_regs,ccadj_,reglist);
3021 inline_readstub(LOADH_STUB,i,constmap[i][s]+offset,i_regs->regmap,dops[i].rt1,ccadj_,reglist);
3027 if (fastio_reg_override >= 0)
3028 a = fastio_reg_override;
3029 do_load_word(a, tl, offset_reg);
3032 add_stub_r(LOADW_STUB,jaddr,out,i,addr,i_regs,ccadj_,reglist);
3035 inline_readstub(LOADW_STUB,i,constmap[i][s]+offset,i_regs->regmap,dops[i].rt1,ccadj_,reglist);
3042 if (fastio_reg_override >= 0)
3043 a = fastio_reg_override;
3045 if (offset_reg >= 0)
3046 emit_ldrb_dualindexed(offset_reg, a, tl);
3048 emit_movzbl_indexed(0, a, tl);
3051 add_stub_r(LOADBU_STUB,jaddr,out,i,addr,i_regs,ccadj_,reglist);
3054 inline_readstub(LOADBU_STUB,i,constmap[i][s]+offset,i_regs->regmap,dops[i].rt1,ccadj_,reglist);
3061 if (fastio_reg_override >= 0)
3062 a = fastio_reg_override;
3063 if (offset_reg >= 0)
3064 emit_ldrh_dualindexed(offset_reg, a, tl);
3066 emit_movzwl_indexed(0, a, tl);
3069 add_stub_r(LOADHU_STUB,jaddr,out,i,addr,i_regs,ccadj_,reglist);
3072 inline_readstub(LOADHU_STUB,i,constmap[i][s]+offset,i_regs->regmap,dops[i].rt1,ccadj_,reglist);
3080 if (fastio_reg_override == HOST_TEMPREG || offset_reg == HOST_TEMPREG)
3081 host_tempreg_release();
3084 #ifndef loadlr_assemble
3085 static void loadlr_assemble(int i, const struct regstat *i_regs, int ccadj_)
3087 int s,tl,temp,temp2,addr;
3090 int memtarget=0,c=0;
3091 int offset_reg = -1;
3092 int fastio_reg_override = -1;
3093 u_int reglist=get_host_reglist(i_regs->regmap);
3094 tl=get_reg(i_regs->regmap,dops[i].rt1);
3095 s=get_reg(i_regs->regmap,dops[i].rs1);
3096 temp=get_reg_temp(i_regs->regmap);
3097 temp2=get_reg(i_regs->regmap,FTEMP);
3098 addr=get_reg(i_regs->regmap,AGEN1+(i&1));
3102 if(offset||s<0||c) addr=temp2;
3105 c=(i_regs->wasconst>>s)&1;
3107 memtarget=((signed int)(constmap[i][s]+offset))<(signed int)0x80000000+RAM_SIZE;
3111 emit_shlimm(addr,3,temp);
3112 if (dops[i].opcode==0x22||dops[i].opcode==0x26) {
3113 emit_andimm(addr,0xFFFFFFFC,temp2); // LWL/LWR
3115 emit_andimm(addr,0xFFFFFFF8,temp2); // LDL/LDR
3117 jaddr = emit_fastpath_cmp_jump(i, i_regs, temp2,
3118 &offset_reg, &fastio_reg_override);
3121 if (ram_offset && memtarget) {
3122 offset_reg = get_ro_reg(i_regs, 0);
3124 if (dops[i].opcode==0x22||dops[i].opcode==0x26) {
3125 emit_movimm(((constmap[i][s]+offset)<<3)&24,temp); // LWL/LWR
3127 emit_movimm(((constmap[i][s]+offset)<<3)&56,temp); // LDL/LDR
3130 if (dops[i].opcode==0x22||dops[i].opcode==0x26) { // LWL/LWR
3133 if (fastio_reg_override >= 0)
3134 a = fastio_reg_override;
3135 do_load_word(a, temp2, offset_reg);
3136 if (fastio_reg_override == HOST_TEMPREG || offset_reg == HOST_TEMPREG)
3137 host_tempreg_release();
3138 if(jaddr) add_stub_r(LOADW_STUB,jaddr,out,i,temp2,i_regs,ccadj_,reglist);
3141 inline_readstub(LOADW_STUB,i,(constmap[i][s]+offset)&0xFFFFFFFC,i_regs->regmap,FTEMP,ccadj_,reglist);
3144 emit_andimm(temp,24,temp);
3145 if (dops[i].opcode==0x22) // LWL
3146 emit_xorimm(temp,24,temp);
3147 host_tempreg_acquire();
3148 emit_movimm(-1,HOST_TEMPREG);
3149 if (dops[i].opcode==0x26) {
3150 emit_shr(temp2,temp,temp2);
3151 emit_bic_lsr(tl,HOST_TEMPREG,temp,tl);
3153 emit_shl(temp2,temp,temp2);
3154 emit_bic_lsl(tl,HOST_TEMPREG,temp,tl);
3156 host_tempreg_release();
3157 emit_or(temp2,tl,tl);
3159 //emit_storereg(dops[i].rt1,tl); // DEBUG
3161 if (dops[i].opcode==0x1A||dops[i].opcode==0x1B) { // LDL/LDR
3167 static void do_invstub(int n)
3170 assem_debug("do_invstub\n");
3171 u_int reglist = stubs[n].a;
3172 u_int addrr = stubs[n].b;
3173 int ofs_start = stubs[n].c;
3174 int ofs_end = stubs[n].d;
3175 int len = ofs_end - ofs_start;
3178 set_jump_target(stubs[n].addr, out);
3180 if (addrr != 0 || ofs_start != 0)
3181 emit_addimm(addrr, ofs_start, 0);
3182 emit_readword(&inv_code_start, 2);
3183 emit_readword(&inv_code_end, 3);
3185 emit_addimm(0, len + 4, (rightr = 1));
3187 emit_cmpcs(3, rightr);
3190 void *func = (len != 0)
3191 ? (void *)ndrc_write_invalidate_many
3192 : (void *)ndrc_write_invalidate_one;
3193 emit_far_call(func);
3194 set_jump_target(jaddr, out);
3195 restore_regs(reglist);
3196 emit_jmp(stubs[n].retaddr);
3199 static void do_store_smc_check(int i, const struct regstat *i_regs, u_int reglist, int addr)
3201 if (HACK_ENABLED(NDHACK_NO_SMC_CHECK))
3203 // this can't be used any more since we started to check exact
3204 // block boundaries in invalidate_range()
3205 //if (i_regs->waswritten & (1<<dops[i].rs1))
3207 // (naively) assume nobody will run code from stack
3208 if (dops[i].rs1 == 29)
3211 int j, imm_maxdiff = 32, imm_min = imm[i], imm_max = imm[i], count = 1;
3212 if (i < slen - 1 && dops[i+1].is_store && dops[i+1].rs1 == dops[i].rs1
3213 && abs(imm[i+1] - imm[i]) <= imm_maxdiff)
3215 for (j = i - 1; j >= 0; j--) {
3216 if (!dops[j].is_store || dops[j].rs1 != dops[i].rs1
3217 || abs(imm[j] - imm[j+1]) > imm_maxdiff)
3220 if (imm_min > imm[j])
3222 if (imm_max < imm[j])
3225 #if defined(HOST_IMM8)
3226 int ir = get_reg(i_regs->regmap, INVCP);
3228 host_tempreg_acquire();
3229 emit_ldrb_indexedsr12_reg(ir, addr, HOST_TEMPREG);
3231 emit_cmpmem_indexedsr12_imm(invalid_code, addr, 1);
3234 #ifdef INVALIDATE_USE_COND_CALL
3236 emit_cmpimm(HOST_TEMPREG, 1);
3237 emit_callne(invalidate_addr_reg[addr]);
3238 host_tempreg_release();
3242 void *jaddr = emit_cbz(HOST_TEMPREG, 0);
3243 host_tempreg_release();
3246 add_stub(INVCODE_STUB, jaddr, out, reglist|(1<<HOST_CCREG),
3247 addr, imm_min, imm_max, 0);
3250 static void store_assemble(int i, const struct regstat *i_regs, int ccadj_)
3256 enum stub_type type=0;
3257 int memtarget=0,c=0;
3258 int agr=AGEN1+(i&1);
3259 int offset_reg = -1;
3260 int fastio_reg_override = -1;
3261 u_int reglist=get_host_reglist(i_regs->regmap);
3262 tl=get_reg(i_regs->regmap,dops[i].rs2);
3263 s=get_reg(i_regs->regmap,dops[i].rs1);
3264 temp=get_reg(i_regs->regmap,agr);
3265 if(temp<0) temp=get_reg_temp(i_regs->regmap);
3268 c=(i_regs->wasconst>>s)&1;
3270 memtarget=((signed int)(constmap[i][s]+offset))<(signed int)0x80000000+RAM_SIZE;
3275 if(i_regs->regmap[HOST_CCREG]==CCREG) reglist&=~(1<<HOST_CCREG);
3276 if(offset||s<0||c) addr=temp;
3279 jaddr = emit_fastpath_cmp_jump(i, i_regs, addr,
3280 &offset_reg, &fastio_reg_override);
3282 else if (ram_offset && memtarget) {
3283 offset_reg = get_ro_reg(i_regs, 0);
3286 switch (dops[i].opcode) {
3291 if (fastio_reg_override >= 0)
3292 a = fastio_reg_override;
3293 do_store_byte(a, tl, offset_reg);
3301 if (fastio_reg_override >= 0)
3302 a = fastio_reg_override;
3303 do_store_hword(a, 0, tl, offset_reg, 1);
3310 if (fastio_reg_override >= 0)
3311 a = fastio_reg_override;
3312 do_store_word(a, 0, tl, offset_reg, 1);
3320 if (fastio_reg_override == HOST_TEMPREG || offset_reg == HOST_TEMPREG)
3321 host_tempreg_release();
3323 // PCSX store handlers don't check invcode again
3325 add_stub_r(type,jaddr,out,i,addr,i_regs,ccadj_,reglist);
3330 #ifdef DESTRUCTIVE_SHIFT
3331 // The x86 shift operation is 'destructive'; it overwrites the
3332 // source register, so we need to make a copy first and use that.
3335 do_store_smc_check(i, i_regs, reglist, addr);
3338 u_int addr_val=constmap[i][s]+offset;
3340 add_stub_r(type,jaddr,out,i,addr,i_regs,ccadj_,reglist);
3341 } else if(c&&!memtarget) {
3342 inline_writestub(type,i,addr_val,i_regs->regmap,dops[i].rs2,ccadj_,reglist);
3344 // basic current block modification detection..
3345 // not looking back as that should be in mips cache already
3346 // (see Spyro2 title->attract mode)
3347 if(c&&start+i*4<addr_val&&addr_val<start+slen*4) {
3348 SysPrintf("write to %08x hits block %08x, pc=%08x\n",addr_val,start,start+i*4);
3349 assert(i_regs->regmap==regs[i].regmap); // not delay slot
3350 if(i_regs->regmap==regs[i].regmap) {
3351 load_all_consts(regs[i].regmap_entry,regs[i].wasdirty,i);
3352 wb_dirtys(regs[i].regmap_entry,regs[i].wasdirty);
3353 emit_movimm(start+i*4+4,0);
3354 emit_writeword(0,&pcaddr);
3355 emit_addimm(HOST_CCREG,2,HOST_CCREG);
3356 emit_far_call(ndrc_get_addr_ht);
3362 static void storelr_assemble(int i, const struct regstat *i_regs, int ccadj_)
3368 void *case1, *case23, *case3;
3369 void *done0, *done1, *done2;
3370 int memtarget=0,c=0;
3371 int agr=AGEN1+(i&1);
3372 int offset_reg = -1;
3373 u_int reglist=get_host_reglist(i_regs->regmap);
3374 tl=get_reg(i_regs->regmap,dops[i].rs2);
3375 s=get_reg(i_regs->regmap,dops[i].rs1);
3376 temp=get_reg(i_regs->regmap,agr);
3377 if(temp<0) temp=get_reg_temp(i_regs->regmap);
3380 c=(i_regs->isconst>>s)&1;
3382 memtarget=((signed int)(constmap[i][s]+offset))<(signed int)0x80000000+RAM_SIZE;
3388 emit_cmpimm(s<0||offset?temp:s,RAM_SIZE);
3389 if(!offset&&s!=temp) emit_mov(s,temp);
3395 if(!memtarget||!dops[i].rs1) {
3401 offset_reg = get_ro_reg(i_regs, 0);
3403 if (dops[i].opcode==0x2C||dops[i].opcode==0x2D) { // SDL/SDR
3407 emit_testimm(temp,2);
3410 emit_testimm(temp,1);
3414 if (dops[i].opcode == 0x2A) { // SWL
3415 // Write msb into least significant byte
3416 if (dops[i].rs2) emit_rorimm(tl, 24, tl);
3417 do_store_byte(temp, tl, offset_reg);
3418 if (dops[i].rs2) emit_rorimm(tl, 8, tl);
3420 else if (dops[i].opcode == 0x2E) { // SWR
3421 // Write entire word
3422 do_store_word(temp, 0, tl, offset_reg, 1);
3427 set_jump_target(case1, out);
3428 if (dops[i].opcode == 0x2A) { // SWL
3429 // Write two msb into two least significant bytes
3430 if (dops[i].rs2) emit_rorimm(tl, 16, tl);
3431 do_store_hword(temp, -1, tl, offset_reg, 0);
3432 if (dops[i].rs2) emit_rorimm(tl, 16, tl);
3434 else if (dops[i].opcode == 0x2E) { // SWR
3435 // Write 3 lsb into three most significant bytes
3436 do_store_byte(temp, tl, offset_reg);
3437 if (dops[i].rs2) emit_rorimm(tl, 8, tl);
3438 do_store_hword(temp, 1, tl, offset_reg, 0);
3439 if (dops[i].rs2) emit_rorimm(tl, 24, tl);
3444 set_jump_target(case23, out);
3445 emit_testimm(temp,1);
3449 if (dops[i].opcode==0x2A) { // SWL
3450 // Write 3 msb into three least significant bytes
3451 if (dops[i].rs2) emit_rorimm(tl, 8, tl);
3452 do_store_hword(temp, -2, tl, offset_reg, 1);
3453 if (dops[i].rs2) emit_rorimm(tl, 16, tl);
3454 do_store_byte(temp, tl, offset_reg);
3455 if (dops[i].rs2) emit_rorimm(tl, 8, tl);
3457 else if (dops[i].opcode == 0x2E) { // SWR
3458 // Write two lsb into two most significant bytes
3459 do_store_hword(temp, 0, tl, offset_reg, 1);
3464 set_jump_target(case3, out);
3465 if (dops[i].opcode == 0x2A) { // SWL
3466 do_store_word(temp, -3, tl, offset_reg, 0);
3468 else if (dops[i].opcode == 0x2E) { // SWR
3469 do_store_byte(temp, tl, offset_reg);
3471 set_jump_target(done0, out);
3472 set_jump_target(done1, out);
3473 set_jump_target(done2, out);
3474 if (offset_reg == HOST_TEMPREG)
3475 host_tempreg_release();
3477 add_stub_r(STORELR_STUB,jaddr,out,i,temp,i_regs,ccadj_,reglist);
3478 do_store_smc_check(i, i_regs, reglist, temp);
3481 static void cop0_assemble(int i, const struct regstat *i_regs, int ccadj_)
3483 if(dops[i].opcode2==0) // MFC0
3485 signed char t=get_reg(i_regs->regmap,dops[i].rt1);
3486 u_int copr=(source[i]>>11)&0x1f;
3487 //assert(t>=0); // Why does this happen? OOT is weird
3488 if(t>=0&&dops[i].rt1!=0) {
3489 emit_readword(®_cop0[copr],t);
3492 else if(dops[i].opcode2==4) // MTC0
3494 signed char s=get_reg(i_regs->regmap,dops[i].rs1);
3495 char copr=(source[i]>>11)&0x1f;
3497 wb_register(dops[i].rs1,i_regs->regmap,i_regs->dirty);
3498 if(copr==9||copr==11||copr==12||copr==13) {
3499 emit_readword(&last_count,HOST_TEMPREG);
3500 emit_loadreg(CCREG,HOST_CCREG); // TODO: do proper reg alloc
3501 emit_add(HOST_CCREG,HOST_TEMPREG,HOST_CCREG);
3502 emit_addimm(HOST_CCREG,ccadj_,HOST_CCREG);
3503 emit_writeword(HOST_CCREG,&Count);
3505 // What a mess. The status register (12) can enable interrupts,
3506 // so needs a special case to handle a pending interrupt.
3507 // The interrupt must be taken immediately, because a subsequent
3508 // instruction might disable interrupts again.
3509 if(copr==12||copr==13) {
3511 // burn cycles to cause cc_interrupt, which will
3512 // reschedule next_interupt. Relies on CCREG from above.
3513 assem_debug("MTC0 DS %d\n", copr);
3514 emit_writeword(HOST_CCREG,&last_count);
3515 emit_movimm(0,HOST_CCREG);
3516 emit_storereg(CCREG,HOST_CCREG);
3517 emit_loadreg(dops[i].rs1,1);
3518 emit_movimm(copr,0);
3519 emit_far_call(pcsx_mtc0_ds);
3520 emit_loadreg(dops[i].rs1,s);
3523 emit_movimm(start+i*4+4,HOST_TEMPREG);
3524 emit_writeword(HOST_TEMPREG,&pcaddr);
3525 emit_movimm(0,HOST_TEMPREG);
3526 emit_writeword(HOST_TEMPREG,&pending_exception);
3529 emit_loadreg(dops[i].rs1,1);
3532 emit_movimm(copr,0);
3533 emit_far_call(pcsx_mtc0);
3534 if(copr==9||copr==11||copr==12||copr==13) {
3535 emit_readword(&Count,HOST_CCREG);
3536 emit_readword(&next_interupt,HOST_TEMPREG);
3537 emit_addimm(HOST_CCREG,-ccadj_,HOST_CCREG);
3538 emit_sub(HOST_CCREG,HOST_TEMPREG,HOST_CCREG);
3539 emit_writeword(HOST_TEMPREG,&last_count);
3540 emit_storereg(CCREG,HOST_CCREG);
3542 if(copr==12||copr==13) {
3543 assert(!is_delayslot);
3544 emit_readword(&pending_exception,HOST_TEMPREG);
3545 emit_test(HOST_TEMPREG,HOST_TEMPREG);
3548 emit_readword(&pcaddr, 0);
3549 emit_addimm(HOST_CCREG,2,HOST_CCREG);
3550 emit_far_call(ndrc_get_addr_ht);
3552 set_jump_target(jaddr, out);
3554 emit_loadreg(dops[i].rs1,s);
3558 assert(dops[i].opcode2==0x10);
3559 //if((source[i]&0x3f)==0x10) // RFE
3561 emit_readword(&Status,0);
3562 emit_andimm(0,0x3c,1);
3563 emit_andimm(0,~0xf,0);
3564 emit_orrshr_imm(1,2,0);
3565 emit_writeword(0,&Status);
3570 static void cop1_unusable(int i, const struct regstat *i_regs)
3572 // XXX: should just just do the exception instead
3577 add_stub_r(FP_STUB,jaddr,out,i,0,i_regs,is_delayslot,0);
3581 static void cop1_assemble(int i, const struct regstat *i_regs)
3583 cop1_unusable(i, i_regs);
3586 static void c1ls_assemble(int i, const struct regstat *i_regs)
3588 cop1_unusable(i, i_regs);
3592 static void do_cop1stub(int n)
3595 assem_debug("do_cop1stub %x\n",start+stubs[n].a*4);
3596 set_jump_target(stubs[n].addr, out);
3598 // int rs=stubs[n].b;
3599 struct regstat *i_regs=(struct regstat *)stubs[n].c;
3602 load_all_consts(regs[i].regmap_entry,regs[i].wasdirty,i);
3603 //if(i_regs!=®s[i]) printf("oops: regs[i]=%x i_regs=%x",(int)®s[i],(int)i_regs);
3605 //else {printf("fp exception in delay slot\n");}
3606 wb_dirtys(i_regs->regmap_entry,i_regs->wasdirty);
3607 if(regs[i].regmap_entry[HOST_CCREG]!=CCREG) emit_loadreg(CCREG,HOST_CCREG);
3608 emit_movimm(start+(i-ds)*4,EAX); // Get PC
3609 emit_addimm(HOST_CCREG,ccadj[i],HOST_CCREG); // CHECK: is this right? There should probably be an extra cycle...
3610 emit_far_jump(ds?fp_exception_ds:fp_exception);
3613 static int cop2_is_stalling_op(int i, int *cycles)
3615 if (dops[i].opcode == 0x3a) { // SWC2
3619 if (dops[i].itype == COP2 && (dops[i].opcode2 == 0 || dops[i].opcode2 == 2)) { // MFC2/CFC2
3623 if (dops[i].itype == C2OP) {
3624 *cycles = gte_cycletab[source[i] & 0x3f];
3627 // ... what about MTC2/CTC2/LWC2?
3632 static void log_gte_stall(int stall, u_int cycle)
3634 if ((u_int)stall <= 44)
3635 printf("x stall %2d %u\n", stall, cycle + last_count);
3638 static void emit_log_gte_stall(int i, int stall, u_int reglist)
3642 emit_movimm(stall, 0);
3644 emit_mov(HOST_TEMPREG, 0);
3645 emit_addimm(HOST_CCREG, ccadj[i], 1);
3646 emit_far_call(log_gte_stall);
3647 restore_regs(reglist);
3651 static void cop2_do_stall_check(u_int op, int i, const struct regstat *i_regs, u_int reglist)
3653 int j = i, other_gte_op_cycles = -1, stall = -MAXBLOCK, cycles_passed;
3654 int rtmp = reglist_find_free(reglist);
3656 if (HACK_ENABLED(NDHACK_NO_STALLS))
3658 if (get_reg(i_regs->regmap, CCREG) != HOST_CCREG) {
3659 // happens occasionally... cc evicted? Don't bother then
3660 //printf("no cc %08x\n", start + i*4);
3664 for (j = i - 1; j >= 0; j--) {
3665 //if (dops[j].is_ds) break;
3666 if (cop2_is_stalling_op(j, &other_gte_op_cycles) || dops[j].bt)
3668 if (j > 0 && ccadj[j - 1] > ccadj[j])
3673 cycles_passed = ccadj[i] - ccadj[j];
3674 if (other_gte_op_cycles >= 0)
3675 stall = other_gte_op_cycles - cycles_passed;
3676 else if (cycles_passed >= 44)
3677 stall = 0; // can't stall
3678 if (stall == -MAXBLOCK && rtmp >= 0) {
3679 // unknown stall, do the expensive runtime check
3680 assem_debug("; cop2_do_stall_check\n");
3683 emit_movimm(gte_cycletab[op], 0);
3684 emit_addimm(HOST_CCREG, ccadj[i], 1);
3685 emit_far_call(call_gteStall);
3686 restore_regs(reglist);
3688 host_tempreg_acquire();
3689 emit_readword(&psxRegs.gteBusyCycle, rtmp);
3690 emit_addimm(rtmp, -ccadj[i], rtmp);
3691 emit_sub(rtmp, HOST_CCREG, HOST_TEMPREG);
3692 emit_cmpimm(HOST_TEMPREG, 44);
3693 emit_cmovb_reg(rtmp, HOST_CCREG);
3694 //emit_log_gte_stall(i, 0, reglist);
3695 host_tempreg_release();
3698 else if (stall > 0) {
3699 //emit_log_gte_stall(i, stall, reglist);
3700 emit_addimm(HOST_CCREG, stall, HOST_CCREG);
3703 // save gteBusyCycle, if needed
3704 if (gte_cycletab[op] == 0)
3706 other_gte_op_cycles = -1;
3707 for (j = i + 1; j < slen; j++) {
3708 if (cop2_is_stalling_op(j, &other_gte_op_cycles))
3710 if (dops[j].is_jump) {
3712 if (j + 1 < slen && cop2_is_stalling_op(j + 1, &other_gte_op_cycles))
3717 if (other_gte_op_cycles >= 0)
3718 // will handle stall when assembling that op
3720 cycles_passed = ccadj[min(j, slen -1)] - ccadj[i];
3721 if (cycles_passed >= 44)
3723 assem_debug("; save gteBusyCycle\n");
3724 host_tempreg_acquire();
3726 emit_readword(&last_count, HOST_TEMPREG);
3727 emit_add(HOST_TEMPREG, HOST_CCREG, HOST_TEMPREG);
3728 emit_addimm(HOST_TEMPREG, ccadj[i], HOST_TEMPREG);
3729 emit_addimm(HOST_TEMPREG, gte_cycletab[op]), HOST_TEMPREG);
3730 emit_writeword(HOST_TEMPREG, &psxRegs.gteBusyCycle);
3732 emit_addimm(HOST_CCREG, ccadj[i] + gte_cycletab[op], HOST_TEMPREG);
3733 emit_writeword(HOST_TEMPREG, &psxRegs.gteBusyCycle);
3735 host_tempreg_release();
3738 static int is_mflohi(int i)
3740 return (dops[i].itype == MOV && (dops[i].rs1 == HIREG || dops[i].rs1 == LOREG));
3743 static int check_multdiv(int i, int *cycles)
3745 if (dops[i].itype != MULTDIV)
3747 if (dops[i].opcode2 == 0x18 || dops[i].opcode2 == 0x19) // MULT(U)
3748 *cycles = 11; // approx from 7 11 14
3754 static void multdiv_prepare_stall(int i, const struct regstat *i_regs, int ccadj_)
3756 int j, found = 0, c = 0;
3757 if (HACK_ENABLED(NDHACK_NO_STALLS))
3759 if (get_reg(i_regs->regmap, CCREG) != HOST_CCREG) {
3760 // happens occasionally... cc evicted? Don't bother then
3763 for (j = i + 1; j < slen; j++) {
3766 if ((found = is_mflohi(j)))
3768 if (dops[j].is_jump) {
3770 if (j + 1 < slen && (found = is_mflohi(j + 1)))
3776 // handle all in multdiv_do_stall()
3778 check_multdiv(i, &c);
3780 assem_debug("; muldiv prepare stall %d\n", c);
3781 host_tempreg_acquire();
3782 emit_addimm(HOST_CCREG, ccadj_ + c, HOST_TEMPREG);
3783 emit_writeword(HOST_TEMPREG, &psxRegs.muldivBusyCycle);
3784 host_tempreg_release();
3787 static void multdiv_do_stall(int i, const struct regstat *i_regs)
3789 int j, known_cycles = 0;
3790 u_int reglist = get_host_reglist(i_regs->regmap);
3791 int rtmp = get_reg_temp(i_regs->regmap);
3793 rtmp = reglist_find_free(reglist);
3794 if (HACK_ENABLED(NDHACK_NO_STALLS))
3796 if (get_reg(i_regs->regmap, CCREG) != HOST_CCREG || rtmp < 0) {
3797 // happens occasionally... cc evicted? Don't bother then
3798 //printf("no cc/rtmp %08x\n", start + i*4);
3802 for (j = i - 1; j >= 0; j--) {
3803 if (dops[j].is_ds) break;
3804 if (check_multdiv(j, &known_cycles))
3807 // already handled by this op
3809 if (dops[j].bt || (j > 0 && ccadj[j - 1] > ccadj[j]))
3814 if (known_cycles > 0) {
3815 known_cycles -= ccadj[i] - ccadj[j];
3816 assem_debug("; muldiv stall resolved %d\n", known_cycles);
3817 if (known_cycles > 0)
3818 emit_addimm(HOST_CCREG, known_cycles, HOST_CCREG);
3821 assem_debug("; muldiv stall unresolved\n");
3822 host_tempreg_acquire();
3823 emit_readword(&psxRegs.muldivBusyCycle, rtmp);
3824 emit_addimm(rtmp, -ccadj[i], rtmp);
3825 emit_sub(rtmp, HOST_CCREG, HOST_TEMPREG);
3826 emit_cmpimm(HOST_TEMPREG, 37);
3827 emit_cmovb_reg(rtmp, HOST_CCREG);
3828 //emit_log_gte_stall(i, 0, reglist);
3829 host_tempreg_release();
3832 static void cop2_get_dreg(u_int copr,signed char tl,signed char temp)
3842 emit_readword(®_cop2d[copr],tl);
3843 emit_signextend16(tl,tl);
3844 emit_writeword(tl,®_cop2d[copr]); // hmh
3851 emit_readword(®_cop2d[copr],tl);
3852 emit_andimm(tl,0xffff,tl);
3853 emit_writeword(tl,®_cop2d[copr]);
3856 emit_readword(®_cop2d[14],tl); // SXY2
3857 emit_writeword(tl,®_cop2d[copr]);
3861 c2op_mfc2_29_assemble(tl,temp);
3864 emit_readword(®_cop2d[copr],tl);
3869 static void cop2_put_dreg(u_int copr,signed char sl,signed char temp)
3873 emit_readword(®_cop2d[13],temp); // SXY1
3874 emit_writeword(sl,®_cop2d[copr]);
3875 emit_writeword(temp,®_cop2d[12]); // SXY0
3876 emit_readword(®_cop2d[14],temp); // SXY2
3877 emit_writeword(sl,®_cop2d[14]);
3878 emit_writeword(temp,®_cop2d[13]); // SXY1
3881 emit_andimm(sl,0x001f,temp);
3882 emit_shlimm(temp,7,temp);
3883 emit_writeword(temp,®_cop2d[9]);
3884 emit_andimm(sl,0x03e0,temp);
3885 emit_shlimm(temp,2,temp);
3886 emit_writeword(temp,®_cop2d[10]);
3887 emit_andimm(sl,0x7c00,temp);
3888 emit_shrimm(temp,3,temp);
3889 emit_writeword(temp,®_cop2d[11]);
3890 emit_writeword(sl,®_cop2d[28]);
3893 emit_xorsar_imm(sl,sl,31,temp);
3894 #if defined(HAVE_ARMV5) || defined(__aarch64__)
3895 emit_clz(temp,temp);
3897 emit_movs(temp,HOST_TEMPREG);
3898 emit_movimm(0,temp);
3899 emit_jeq((int)out+4*4);
3900 emit_addpl_imm(temp,1,temp);
3901 emit_lslpls_imm(HOST_TEMPREG,1,HOST_TEMPREG);
3902 emit_jns((int)out-2*4);
3904 emit_writeword(sl,®_cop2d[30]);
3905 emit_writeword(temp,®_cop2d[31]);
3910 emit_writeword(sl,®_cop2d[copr]);
3915 static void c2ls_assemble(int i, const struct regstat *i_regs, int ccadj_)
3920 int memtarget=0,c=0;
3922 enum stub_type type;
3923 int agr=AGEN1+(i&1);
3924 int offset_reg = -1;
3925 int fastio_reg_override = -1;
3926 u_int reglist=get_host_reglist(i_regs->regmap);
3927 u_int copr=(source[i]>>16)&0x1f;
3928 s=get_reg(i_regs->regmap,dops[i].rs1);
3929 tl=get_reg(i_regs->regmap,FTEMP);
3931 assert(dops[i].rs1>0);
3934 if(i_regs->regmap[HOST_CCREG]==CCREG)
3935 reglist&=~(1<<HOST_CCREG);
3938 if (dops[i].opcode==0x3a) { // SWC2
3939 ar=get_reg(i_regs->regmap,agr);
3940 if(ar<0) ar=get_reg_temp(i_regs->regmap);
3945 if(s>=0) c=(i_regs->wasconst>>s)&1;
3946 memtarget=c&&(((signed int)(constmap[i][s]+offset))<(signed int)0x80000000+RAM_SIZE);
3947 if (!offset&&!c&&s>=0) ar=s;
3950 cop2_do_stall_check(0, i, i_regs, reglist);
3952 if (dops[i].opcode==0x3a) { // SWC2
3953 cop2_get_dreg(copr,tl,-1);
3961 emit_jmp(0); // inline_readstub/inline_writestub?
3965 jaddr2 = emit_fastpath_cmp_jump(i, i_regs, ar,
3966 &offset_reg, &fastio_reg_override);
3968 else if (ram_offset && memtarget) {
3969 offset_reg = get_ro_reg(i_regs, 0);
3971 switch (dops[i].opcode) {
3972 case 0x32: { // LWC2
3974 if (fastio_reg_override >= 0)
3975 a = fastio_reg_override;
3976 do_load_word(a, tl, offset_reg);
3979 case 0x3a: { // SWC2
3980 #ifdef DESTRUCTIVE_SHIFT
3981 if(!offset&&!c&&s>=0) emit_mov(s,ar);
3984 if (fastio_reg_override >= 0)
3985 a = fastio_reg_override;
3986 do_store_word(a, 0, tl, offset_reg, 1);
3993 if (fastio_reg_override == HOST_TEMPREG || offset_reg == HOST_TEMPREG)
3994 host_tempreg_release();
3996 add_stub_r(type,jaddr2,out,i,ar,i_regs,ccadj_,reglist);
3997 if(dops[i].opcode==0x3a) // SWC2
3998 do_store_smc_check(i, i_regs, reglist, ar);
3999 if (dops[i].opcode==0x32) { // LWC2
4000 host_tempreg_acquire();
4001 cop2_put_dreg(copr,tl,HOST_TEMPREG);
4002 host_tempreg_release();
4006 static void cop2_assemble(int i, const struct regstat *i_regs)
4008 u_int copr = (source[i]>>11) & 0x1f;
4009 signed char temp = get_reg_temp(i_regs->regmap);
4011 if (!HACK_ENABLED(NDHACK_NO_STALLS)) {
4012 u_int reglist = reglist_exclude(get_host_reglist(i_regs->regmap), temp, -1);
4013 if (dops[i].opcode2 == 0 || dops[i].opcode2 == 2) { // MFC2/CFC2
4014 signed char tl = get_reg(i_regs->regmap, dops[i].rt1);
4015 reglist = reglist_exclude(reglist, tl, -1);
4017 cop2_do_stall_check(0, i, i_regs, reglist);
4019 if (dops[i].opcode2==0) { // MFC2
4020 signed char tl=get_reg(i_regs->regmap,dops[i].rt1);
4021 if(tl>=0&&dops[i].rt1!=0)
4022 cop2_get_dreg(copr,tl,temp);
4024 else if (dops[i].opcode2==4) { // MTC2
4025 signed char sl=get_reg(i_regs->regmap,dops[i].rs1);
4026 cop2_put_dreg(copr,sl,temp);
4028 else if (dops[i].opcode2==2) // CFC2
4030 signed char tl=get_reg(i_regs->regmap,dops[i].rt1);
4031 if(tl>=0&&dops[i].rt1!=0)
4032 emit_readword(®_cop2c[copr],tl);
4034 else if (dops[i].opcode2==6) // CTC2
4036 signed char sl=get_reg(i_regs->regmap,dops[i].rs1);
4045 emit_signextend16(sl,temp);
4048 c2op_ctc2_31_assemble(sl,temp);
4054 emit_writeword(temp,®_cop2c[copr]);
4059 static void do_unalignedwritestub(int n)
4061 assem_debug("do_unalignedwritestub %x\n",start+stubs[n].a*4);
4063 set_jump_target(stubs[n].addr, out);
4066 struct regstat *i_regs=(struct regstat *)stubs[n].c;
4067 int addr=stubs[n].b;
4068 u_int reglist=stubs[n].e;
4069 signed char *i_regmap=i_regs->regmap;
4070 int temp2=get_reg(i_regmap,FTEMP);
4072 rt=get_reg(i_regmap,dops[i].rs2);
4075 assert(dops[i].opcode==0x2a||dops[i].opcode==0x2e); // SWL/SWR only implemented
4077 reglist&=~(1<<temp2);
4079 // don't bother with it and call write handler
4082 int cc=get_reg(i_regmap,CCREG);
4084 emit_loadreg(CCREG,2);
4085 emit_addimm(cc<0?2:cc,(int)stubs[n].d+1,2);
4086 emit_far_call((dops[i].opcode==0x2a?jump_handle_swl:jump_handle_swr));
4087 emit_addimm(0,-((int)stubs[n].d+1),cc<0?2:cc);
4089 emit_storereg(CCREG,2);
4090 restore_regs(reglist);
4091 emit_jmp(stubs[n].retaddr); // return address
4094 #ifndef multdiv_assemble
4095 void multdiv_assemble(int i,struct regstat *i_regs)
4097 printf("Need multdiv_assemble for this architecture.\n");
4102 static void mov_assemble(int i, const struct regstat *i_regs)
4104 //if(dops[i].opcode2==0x10||dops[i].opcode2==0x12) { // MFHI/MFLO
4105 //if(dops[i].opcode2==0x11||dops[i].opcode2==0x13) { // MTHI/MTLO
4108 tl=get_reg(i_regs->regmap,dops[i].rt1);
4111 sl=get_reg(i_regs->regmap,dops[i].rs1);
4112 if(sl>=0) emit_mov(sl,tl);
4113 else emit_loadreg(dops[i].rs1,tl);
4116 if (dops[i].rs1 == HIREG || dops[i].rs1 == LOREG) // MFHI/MFLO
4117 multdiv_do_stall(i, i_regs);
4120 // call interpreter, exception handler, things that change pc/regs/cycles ...
4121 static void call_c_cpu_handler(int i, const struct regstat *i_regs, int ccadj_, u_int pc, void *func)
4123 signed char ccreg=get_reg(i_regs->regmap,CCREG);
4124 assert(ccreg==HOST_CCREG);
4125 assert(!is_delayslot);
4128 emit_movimm(pc,3); // Get PC
4129 emit_readword(&last_count,2);
4130 emit_writeword(3,&psxRegs.pc);
4131 emit_addimm(HOST_CCREG,ccadj_,HOST_CCREG);
4132 emit_add(2,HOST_CCREG,2);
4133 emit_writeword(2,&psxRegs.cycle);
4134 emit_far_call(func);
4135 emit_far_jump(jump_to_new_pc);
4138 static void syscall_assemble(int i, const struct regstat *i_regs, int ccadj_)
4140 // 'break' tends to be littered around to catch things like
4141 // division by 0 and is almost never executed, so don't emit much code here
4142 void *func = (dops[i].opcode2 == 0x0C)
4143 ? (is_delayslot ? jump_syscall_ds : jump_syscall)
4144 : (is_delayslot ? jump_break_ds : jump_break);
4145 assert(get_reg(i_regs->regmap, CCREG) == HOST_CCREG);
4146 emit_movimm(start + i*4, 2); // pc
4147 emit_addimm(HOST_CCREG, ccadj_ + CLOCK_ADJUST(1), HOST_CCREG);
4148 emit_far_jump(func);
4151 static void hlecall_assemble(int i, const struct regstat *i_regs, int ccadj_)
4153 void *hlefunc = gteNULL;
4154 uint32_t hleCode = source[i] & 0x03ffffff;
4155 if (hleCode < ARRAY_SIZE(psxHLEt))
4156 hlefunc = psxHLEt[hleCode];
4158 call_c_cpu_handler(i, i_regs, ccadj_, start + i*4+4, hlefunc);
4161 static void intcall_assemble(int i, const struct regstat *i_regs, int ccadj_)
4163 call_c_cpu_handler(i, i_regs, ccadj_, start + i*4, execI);
4166 static void speculate_mov(int rs,int rt)
4169 smrv_strong_next|=1<<rt;
4174 static void speculate_mov_weak(int rs,int rt)
4177 smrv_weak_next|=1<<rt;
4182 static void speculate_register_values(int i)
4185 memcpy(smrv,psxRegs.GPR.r,sizeof(smrv));
4186 // gp,sp are likely to stay the same throughout the block
4187 smrv_strong_next=(1<<28)|(1<<29)|(1<<30);
4188 smrv_weak_next=~smrv_strong_next;
4189 //printf(" llr %08x\n", smrv[4]);
4191 smrv_strong=smrv_strong_next;
4192 smrv_weak=smrv_weak_next;
4193 switch(dops[i].itype) {
4195 if ((smrv_strong>>dops[i].rs1)&1) speculate_mov(dops[i].rs1,dops[i].rt1);
4196 else if((smrv_strong>>dops[i].rs2)&1) speculate_mov(dops[i].rs2,dops[i].rt1);
4197 else if((smrv_weak>>dops[i].rs1)&1) speculate_mov_weak(dops[i].rs1,dops[i].rt1);
4198 else if((smrv_weak>>dops[i].rs2)&1) speculate_mov_weak(dops[i].rs2,dops[i].rt1);
4200 smrv_strong_next&=~(1<<dops[i].rt1);
4201 smrv_weak_next&=~(1<<dops[i].rt1);
4205 smrv_strong_next&=~(1<<dops[i].rt1);
4206 smrv_weak_next&=~(1<<dops[i].rt1);
4209 if(dops[i].rt1&&is_const(®s[i],dops[i].rt1)) {
4210 int value,hr=get_reg(regs[i].regmap,dops[i].rt1);
4212 if(get_final_value(hr,i,&value))
4213 smrv[dops[i].rt1]=value;
4214 else smrv[dops[i].rt1]=constmap[i][hr];
4215 smrv_strong_next|=1<<dops[i].rt1;
4219 if ((smrv_strong>>dops[i].rs1)&1) speculate_mov(dops[i].rs1,dops[i].rt1);
4220 else if((smrv_weak>>dops[i].rs1)&1) speculate_mov_weak(dops[i].rs1,dops[i].rt1);
4224 if(start<0x2000&&(dops[i].rt1==26||(smrv[dops[i].rt1]>>24)==0xa0)) {
4225 // special case for BIOS
4226 smrv[dops[i].rt1]=0xa0000000;
4227 smrv_strong_next|=1<<dops[i].rt1;
4234 smrv_strong_next&=~(1<<dops[i].rt1);
4235 smrv_weak_next&=~(1<<dops[i].rt1);
4239 if(dops[i].opcode2==0||dops[i].opcode2==2) { // MFC/CFC
4240 smrv_strong_next&=~(1<<dops[i].rt1);
4241 smrv_weak_next&=~(1<<dops[i].rt1);
4245 if (dops[i].opcode==0x32) { // LWC2
4246 smrv_strong_next&=~(1<<dops[i].rt1);
4247 smrv_weak_next&=~(1<<dops[i].rt1);
4253 printf("x %08x %08x %d %d c %08x %08x\n",smrv[r],start+i*4,
4254 ((smrv_strong>>r)&1),(smrv_weak>>r)&1,regs[i].isconst,regs[i].wasconst);
4258 static void ujump_assemble(int i, const struct regstat *i_regs);
4259 static void rjump_assemble(int i, const struct regstat *i_regs);
4260 static void cjump_assemble(int i, const struct regstat *i_regs);
4261 static void sjump_assemble(int i, const struct regstat *i_regs);
4263 static int assemble(int i, const struct regstat *i_regs, int ccadj_)
4266 switch (dops[i].itype) {
4268 alu_assemble(i, i_regs);
4271 imm16_assemble(i, i_regs);
4274 shift_assemble(i, i_regs);
4277 shiftimm_assemble(i, i_regs);
4280 load_assemble(i, i_regs, ccadj_);
4283 loadlr_assemble(i, i_regs, ccadj_);
4286 store_assemble(i, i_regs, ccadj_);
4289 storelr_assemble(i, i_regs, ccadj_);
4292 cop0_assemble(i, i_regs, ccadj_);
4295 cop1_assemble(i, i_regs);
4298 c1ls_assemble(i, i_regs);
4301 cop2_assemble(i, i_regs);
4304 c2ls_assemble(i, i_regs, ccadj_);
4307 c2op_assemble(i, i_regs);
4310 multdiv_assemble(i, i_regs);
4311 multdiv_prepare_stall(i, i_regs, ccadj_);
4314 mov_assemble(i, i_regs);
4317 syscall_assemble(i, i_regs, ccadj_);
4320 hlecall_assemble(i, i_regs, ccadj_);
4323 intcall_assemble(i, i_regs, ccadj_);
4326 ujump_assemble(i, i_regs);
4330 rjump_assemble(i, i_regs);
4334 cjump_assemble(i, i_regs);
4338 sjump_assemble(i, i_regs);
4344 // not handled, just skip
4352 static void ds_assemble(int i, const struct regstat *i_regs)
4354 speculate_register_values(i);
4356 switch (dops[i].itype) {
4364 SysPrintf("Jump in the delay slot. This is probably a bug.\n");
4367 assemble(i, i_regs, ccadj[i]);
4372 // Is the branch target a valid internal jump?
4373 static int internal_branch(int addr)
4375 if(addr&1) return 0; // Indirect (register) jump
4376 if(addr>=start && addr<start+slen*4-4)
4383 static void wb_invalidate(signed char pre[],signed char entry[],uint64_t dirty,uint64_t u)
4386 for(hr=0;hr<HOST_REGS;hr++) {
4387 if(hr!=EXCLUDE_REG) {
4388 if(pre[hr]!=entry[hr]) {
4391 if(get_reg(entry,pre[hr])<0) {
4393 if(!((u>>pre[hr])&1))
4394 emit_storereg(pre[hr],hr);
4401 // Move from one register to another (no writeback)
4402 for(hr=0;hr<HOST_REGS;hr++) {
4403 if(hr!=EXCLUDE_REG) {
4404 if(pre[hr]!=entry[hr]) {
4405 if(pre[hr]>=0&&pre[hr]<TEMPREG) {
4407 if((nr=get_reg(entry,pre[hr]))>=0) {
4416 // Load the specified registers
4417 // This only loads the registers given as arguments because
4418 // we don't want to load things that will be overwritten
4419 static inline void load_reg(signed char entry[], signed char regmap[], int rs)
4421 int hr = get_reg(regmap, rs);
4422 if (hr >= 0 && entry[hr] != regmap[hr])
4423 emit_loadreg(regmap[hr], hr);
4426 static void load_regs(signed char entry[], signed char regmap[], int rs1, int rs2)
4428 load_reg(entry, regmap, rs1);
4430 load_reg(entry, regmap, rs2);
4433 // Load registers prior to the start of a loop
4434 // so that they are not loaded within the loop
4435 static void loop_preload(signed char pre[],signed char entry[])
4438 for (hr = 0; hr < HOST_REGS; hr++) {
4440 if (r >= 0 && pre[hr] != r && get_reg(pre, r) < 0) {
4441 assem_debug("loop preload:\n");
4443 emit_loadreg(r, hr);
4448 // Generate address for load/store instruction
4449 // goes to AGEN for writes, FTEMP for LOADLR and cop1/2 loads
4450 static void address_generation(int i, const struct regstat *i_regs, signed char entry[])
4452 if (dops[i].is_load || dops[i].is_store) {
4454 int agr=AGEN1+(i&1);
4455 if(dops[i].itype==LOAD) {
4456 ra=get_reg(i_regs->regmap,dops[i].rt1);
4457 if(ra<0) ra=get_reg_temp(i_regs->regmap);
4460 if(dops[i].itype==LOADLR) {
4461 ra=get_reg(i_regs->regmap,FTEMP);
4463 if(dops[i].itype==STORE||dops[i].itype==STORELR) {
4464 ra=get_reg(i_regs->regmap,agr);
4465 if(ra<0) ra=get_reg_temp(i_regs->regmap);
4467 if(dops[i].itype==C2LS) {
4468 if ((dops[i].opcode&0x3b)==0x31||(dops[i].opcode&0x3b)==0x32) // LWC1/LDC1/LWC2/LDC2
4469 ra=get_reg(i_regs->regmap,FTEMP);
4470 else { // SWC1/SDC1/SWC2/SDC2
4471 ra=get_reg(i_regs->regmap,agr);
4472 if(ra<0) ra=get_reg_temp(i_regs->regmap);
4475 int rs=get_reg(i_regs->regmap,dops[i].rs1);
4478 int c=(i_regs->wasconst>>rs)&1;
4479 if(dops[i].rs1==0) {
4480 // Using r0 as a base address
4481 if(!entry||entry[ra]!=agr) {
4482 if (dops[i].opcode==0x22||dops[i].opcode==0x26) {
4483 emit_movimm(offset&0xFFFFFFFC,ra); // LWL/LWR
4484 }else if (dops[i].opcode==0x1a||dops[i].opcode==0x1b) {
4485 emit_movimm(offset&0xFFFFFFF8,ra); // LDL/LDR
4487 emit_movimm(offset,ra);
4489 } // else did it in the previous cycle
4492 if(!entry||entry[ra]!=dops[i].rs1)
4493 emit_loadreg(dops[i].rs1,ra);
4494 //if(!entry||entry[ra]!=dops[i].rs1)
4495 // printf("poor load scheduling!\n");
4498 if(dops[i].rs1!=dops[i].rt1||dops[i].itype!=LOAD) {
4499 if(!entry||entry[ra]!=agr) {
4500 if (dops[i].opcode==0x22||dops[i].opcode==0x26) {
4501 emit_movimm((constmap[i][rs]+offset)&0xFFFFFFFC,ra); // LWL/LWR
4502 }else if (dops[i].opcode==0x1a||dops[i].opcode==0x1b) {
4503 emit_movimm((constmap[i][rs]+offset)&0xFFFFFFF8,ra); // LDL/LDR
4505 emit_movimm(constmap[i][rs]+offset,ra);
4506 regs[i].loadedconst|=1<<ra;
4508 } // else did it in the previous cycle
4509 } // else load_consts already did it
4511 if(offset&&!c&&dops[i].rs1) {
4513 emit_addimm(rs,offset,ra);
4515 emit_addimm(ra,offset,ra);
4520 // Preload constants for next instruction
4521 if (dops[i+1].is_load || dops[i+1].is_store) {
4524 agr=AGEN1+((i+1)&1);
4525 ra=get_reg(i_regs->regmap,agr);
4527 int rs=get_reg(regs[i+1].regmap,dops[i+1].rs1);
4528 int offset=imm[i+1];
4529 int c=(regs[i+1].wasconst>>rs)&1;
4530 if(c&&(dops[i+1].rs1!=dops[i+1].rt1||dops[i+1].itype!=LOAD)) {
4531 if (dops[i+1].opcode==0x22||dops[i+1].opcode==0x26) {
4532 emit_movimm((constmap[i+1][rs]+offset)&0xFFFFFFFC,ra); // LWL/LWR
4533 }else if (dops[i+1].opcode==0x1a||dops[i+1].opcode==0x1b) {
4534 emit_movimm((constmap[i+1][rs]+offset)&0xFFFFFFF8,ra); // LDL/LDR
4536 emit_movimm(constmap[i+1][rs]+offset,ra);
4537 regs[i+1].loadedconst|=1<<ra;
4540 else if(dops[i+1].rs1==0) {
4541 // Using r0 as a base address
4542 if (dops[i+1].opcode==0x22||dops[i+1].opcode==0x26) {
4543 emit_movimm(offset&0xFFFFFFFC,ra); // LWL/LWR
4544 }else if (dops[i+1].opcode==0x1a||dops[i+1].opcode==0x1b) {
4545 emit_movimm(offset&0xFFFFFFF8,ra); // LDL/LDR
4547 emit_movimm(offset,ra);
4554 static int get_final_value(int hr, int i, int *value)
4556 int reg=regs[i].regmap[hr];
4558 if(regs[i+1].regmap[hr]!=reg) break;
4559 if(!((regs[i+1].isconst>>hr)&1)) break;
4560 if(dops[i+1].bt) break;
4564 if (dops[i].is_jump) {
4565 *value=constmap[i][hr];
4569 if (dops[i+1].is_jump) {
4570 // Load in delay slot, out-of-order execution
4571 if(dops[i+2].itype==LOAD&&dops[i+2].rs1==reg&&dops[i+2].rt1==reg&&((regs[i+1].wasconst>>hr)&1))
4573 // Precompute load address
4574 *value=constmap[i][hr]+imm[i+2];
4578 if(dops[i+1].itype==LOAD&&dops[i+1].rs1==reg&&dops[i+1].rt1==reg)
4580 // Precompute load address
4581 *value=constmap[i][hr]+imm[i+1];
4582 //printf("c=%x imm=%lx\n",(long)constmap[i][hr],imm[i+1]);
4587 *value=constmap[i][hr];
4588 //printf("c=%lx\n",(long)constmap[i][hr]);
4589 if(i==slen-1) return 1;
4591 return !((unneeded_reg[i+1]>>reg)&1);
4594 // Load registers with known constants
4595 static void load_consts(signed char pre[],signed char regmap[],int i)
4598 // propagate loaded constant flags
4599 if(i==0||dops[i].bt)
4600 regs[i].loadedconst=0;
4602 for(hr=0;hr<HOST_REGS;hr++) {
4603 if(hr!=EXCLUDE_REG&®map[hr]>=0&&((regs[i-1].isconst>>hr)&1)&&pre[hr]==regmap[hr]
4604 &®map[hr]==regs[i-1].regmap[hr]&&((regs[i-1].loadedconst>>hr)&1))
4606 regs[i].loadedconst|=1<<hr;
4611 for(hr=0;hr<HOST_REGS;hr++) {
4612 if(hr!=EXCLUDE_REG&®map[hr]>=0) {
4613 //if(entry[hr]!=regmap[hr]) {
4614 if(!((regs[i].loadedconst>>hr)&1)) {
4615 assert(regmap[hr]<64);
4616 if(((regs[i].isconst>>hr)&1)&®map[hr]>0) {
4617 int value,similar=0;
4618 if(get_final_value(hr,i,&value)) {
4619 // see if some other register has similar value
4620 for(hr2=0;hr2<HOST_REGS;hr2++) {
4621 if(hr2!=EXCLUDE_REG&&((regs[i].loadedconst>>hr2)&1)) {
4622 if(is_similar_value(value,constmap[i][hr2])) {
4630 if(get_final_value(hr2,i,&value2)) // is this needed?
4631 emit_movimm_from(value2,hr2,value,hr);
4633 emit_movimm(value,hr);
4639 emit_movimm(value,hr);
4642 regs[i].loadedconst|=1<<hr;
4649 static void load_all_consts(const signed char regmap[], u_int dirty, int i)
4653 for(hr=0;hr<HOST_REGS;hr++) {
4654 if(hr!=EXCLUDE_REG&®map[hr]>=0&&((dirty>>hr)&1)) {
4655 assert(regmap[hr] < 64);
4656 if(((regs[i].isconst>>hr)&1)&®map[hr]>0) {
4657 int value=constmap[i][hr];
4662 emit_movimm(value,hr);
4669 // Write out all dirty registers (except cycle count)
4670 static void wb_dirtys(const signed char i_regmap[], uint64_t i_dirty)
4673 for(hr=0;hr<HOST_REGS;hr++) {
4674 if(hr!=EXCLUDE_REG) {
4675 if(i_regmap[hr]>0) {
4676 if(i_regmap[hr]!=CCREG) {
4677 if((i_dirty>>hr)&1) {
4678 assert(i_regmap[hr]<64);
4679 emit_storereg(i_regmap[hr],hr);
4687 // Write out dirty registers that we need to reload (pair with load_needed_regs)
4688 // This writes the registers not written by store_regs_bt
4689 static void wb_needed_dirtys(const signed char i_regmap[], uint64_t i_dirty, int addr)
4692 int t=(addr-start)>>2;
4693 for(hr=0;hr<HOST_REGS;hr++) {
4694 if(hr!=EXCLUDE_REG) {
4695 if(i_regmap[hr]>0) {
4696 if(i_regmap[hr]!=CCREG) {
4697 if(i_regmap[hr]==regs[t].regmap_entry[hr] && ((regs[t].dirty>>hr)&1)) {
4698 if((i_dirty>>hr)&1) {
4699 assert(i_regmap[hr]<64);
4700 emit_storereg(i_regmap[hr],hr);
4709 // Load all registers (except cycle count)
4710 static void load_all_regs(const signed char i_regmap[])
4713 for(hr=0;hr<HOST_REGS;hr++) {
4714 if(hr!=EXCLUDE_REG) {
4715 if(i_regmap[hr]==0) {
4719 if(i_regmap[hr]>0 && i_regmap[hr]<TEMPREG && i_regmap[hr]!=CCREG)
4721 emit_loadreg(i_regmap[hr],hr);
4727 // Load all current registers also needed by next instruction
4728 static void load_needed_regs(const signed char i_regmap[], const signed char next_regmap[])
4731 for(hr=0;hr<HOST_REGS;hr++) {
4732 if(hr!=EXCLUDE_REG) {
4733 if(get_reg(next_regmap,i_regmap[hr])>=0) {
4734 if(i_regmap[hr]==0) {
4738 if(i_regmap[hr]>0 && i_regmap[hr]<TEMPREG && i_regmap[hr]!=CCREG)
4740 emit_loadreg(i_regmap[hr],hr);
4747 // Load all regs, storing cycle count if necessary
4748 static void load_regs_entry(int t)
4751 if(dops[t].is_ds) emit_addimm(HOST_CCREG,CLOCK_ADJUST(1),HOST_CCREG);
4752 else if(ccadj[t]) emit_addimm(HOST_CCREG,-ccadj[t],HOST_CCREG);
4753 if(regs[t].regmap_entry[HOST_CCREG]!=CCREG) {
4754 emit_storereg(CCREG,HOST_CCREG);
4757 for(hr=0;hr<HOST_REGS;hr++) {
4758 if(regs[t].regmap_entry[hr]>=0&®s[t].regmap_entry[hr]<TEMPREG) {
4759 if(regs[t].regmap_entry[hr]==0) {
4762 else if(regs[t].regmap_entry[hr]!=CCREG)
4764 emit_loadreg(regs[t].regmap_entry[hr],hr);
4770 // Store dirty registers prior to branch
4771 static void store_regs_bt(signed char i_regmap[],uint64_t i_dirty,int addr)
4773 if(internal_branch(addr))
4775 int t=(addr-start)>>2;
4777 for(hr=0;hr<HOST_REGS;hr++) {
4778 if(hr!=EXCLUDE_REG) {
4779 if(i_regmap[hr]>0 && i_regmap[hr]!=CCREG) {
4780 if(i_regmap[hr]!=regs[t].regmap_entry[hr] || !((regs[t].dirty>>hr)&1)) {
4781 if((i_dirty>>hr)&1) {
4782 assert(i_regmap[hr]<64);
4783 if(!((unneeded_reg[t]>>i_regmap[hr])&1))
4784 emit_storereg(i_regmap[hr],hr);
4793 // Branch out of this block, write out all dirty regs
4794 wb_dirtys(i_regmap,i_dirty);
4798 // Load all needed registers for branch target
4799 static void load_regs_bt(signed char i_regmap[],uint64_t i_dirty,int addr)
4801 //if(addr>=start && addr<(start+slen*4))
4802 if(internal_branch(addr))
4804 int t=(addr-start)>>2;
4806 // Store the cycle count before loading something else
4807 if(i_regmap[HOST_CCREG]!=CCREG) {
4808 assert(i_regmap[HOST_CCREG]==-1);
4810 if(regs[t].regmap_entry[HOST_CCREG]!=CCREG) {
4811 emit_storereg(CCREG,HOST_CCREG);
4814 for(hr=0;hr<HOST_REGS;hr++) {
4815 if(hr!=EXCLUDE_REG&®s[t].regmap_entry[hr]>=0&®s[t].regmap_entry[hr]<TEMPREG) {
4816 if(i_regmap[hr]!=regs[t].regmap_entry[hr]) {
4817 if(regs[t].regmap_entry[hr]==0) {
4820 else if(regs[t].regmap_entry[hr]!=CCREG)
4822 emit_loadreg(regs[t].regmap_entry[hr],hr);
4830 static int match_bt(signed char i_regmap[],uint64_t i_dirty,int addr)
4832 if(addr>=start && addr<start+slen*4-4)
4834 int t=(addr-start)>>2;
4836 if(regs[t].regmap_entry[HOST_CCREG]!=CCREG) return 0;
4837 for(hr=0;hr<HOST_REGS;hr++)
4841 if(i_regmap[hr]!=regs[t].regmap_entry[hr])
4843 if(regs[t].regmap_entry[hr]>=0&&(regs[t].regmap_entry[hr]|64)<TEMPREG+64)
4850 if(i_regmap[hr]<TEMPREG)
4852 if(!((unneeded_reg[t]>>i_regmap[hr])&1))
4855 else if(i_regmap[hr]>=64&&i_regmap[hr]<TEMPREG+64)
4861 else // Same register but is it 32-bit or dirty?
4864 if(!((regs[t].dirty>>hr)&1))
4868 if(!((unneeded_reg[t]>>i_regmap[hr])&1))
4870 //printf("%x: dirty no match\n",addr);
4878 // Delay slots are not valid branch targets
4879 //if(t>0&&(dops[t-1].is_jump) return 0;
4880 // Delay slots require additional processing, so do not match
4881 if(dops[t].is_ds) return 0;
4886 for(hr=0;hr<HOST_REGS;hr++)
4892 if(hr!=HOST_CCREG||i_regmap[hr]!=CCREG)
4907 static void drc_dbg_emit_do_cmp(int i, int ccadj_)
4909 extern void do_insn_cmp();
4911 u_int hr, reglist = get_host_reglist(regs[i].regmap);
4913 assem_debug("//do_insn_cmp %08x\n", start+i*4);
4915 // write out changed consts to match the interpreter
4916 if (i > 0 && !dops[i].bt) {
4917 for (hr = 0; hr < HOST_REGS; hr++) {
4918 int reg = regs[i].regmap_entry[hr]; // regs[i-1].regmap[hr];
4919 if (hr == EXCLUDE_REG || reg < 0)
4921 if (!((regs[i-1].isconst >> hr) & 1))
4923 if (i > 1 && reg == regs[i-2].regmap[hr] && constmap[i-1][hr] == constmap[i-2][hr])
4925 emit_movimm(constmap[i-1][hr],0);
4926 emit_storereg(reg, 0);
4929 emit_movimm(start+i*4,0);
4930 emit_writeword(0,&pcaddr);
4931 int cc = get_reg(regs[i].regmap_entry, CCREG);
4933 emit_loadreg(CCREG, cc = 0);
4934 emit_addimm(cc, ccadj_, 0);
4935 emit_writeword(0, &psxRegs.cycle);
4936 emit_far_call(do_insn_cmp);
4937 //emit_readword(&cycle,0);
4938 //emit_addimm(0,2,0);
4939 //emit_writeword(0,&cycle);
4941 restore_regs(reglist);
4942 assem_debug("\\\\do_insn_cmp\n");
4945 #define drc_dbg_emit_do_cmp(x,y)
4948 // Used when a branch jumps into the delay slot of another branch
4949 static void ds_assemble_entry(int i)
4951 int t = (ba[i] - start) >> 2;
4952 int ccadj_ = -CLOCK_ADJUST(1);
4954 instr_addr[t] = out;
4955 assem_debug("Assemble delay slot at %x\n",ba[i]);
4956 assem_debug("<->\n");
4957 drc_dbg_emit_do_cmp(t, ccadj_);
4958 if(regs[t].regmap_entry[HOST_CCREG]==CCREG&®s[t].regmap[HOST_CCREG]!=CCREG)
4959 wb_register(CCREG,regs[t].regmap_entry,regs[t].wasdirty);
4960 load_regs(regs[t].regmap_entry,regs[t].regmap,dops[t].rs1,dops[t].rs2);
4961 address_generation(t,®s[t],regs[t].regmap_entry);
4962 if (ram_offset && (dops[t].is_load || dops[t].is_store))
4963 load_reg(regs[t].regmap_entry,regs[t].regmap,ROREG);
4964 if (dops[t].is_store)
4965 load_reg(regs[t].regmap_entry,regs[t].regmap,INVCP);
4967 switch (dops[t].itype) {
4975 SysPrintf("Jump in the delay slot. This is probably a bug.\n");
4978 assemble(t, ®s[t], ccadj_);
4980 store_regs_bt(regs[t].regmap,regs[t].dirty,ba[i]+4);
4981 load_regs_bt(regs[t].regmap,regs[t].dirty,ba[i]+4);
4982 if(internal_branch(ba[i]+4))
4983 assem_debug("branch: internal\n");
4985 assem_debug("branch: external\n");
4986 assert(internal_branch(ba[i]+4));
4987 add_to_linker(out,ba[i]+4,internal_branch(ba[i]+4));
4991 // Load 2 immediates optimizing for small code size
4992 static void emit_mov2imm_compact(int imm1,u_int rt1,int imm2,u_int rt2)
4994 emit_movimm(imm1,rt1);
4995 emit_movimm_from(imm1,rt1,imm2,rt2);
4998 static void do_cc(int i, const signed char i_regmap[], int *adj,
4999 int addr, int taken, int invert)
5001 int count, count_plus2;
5005 if(dops[i].itype==RJUMP)
5009 //if(ba[i]>=start && ba[i]<(start+slen*4))
5010 if(internal_branch(ba[i]))
5013 if(dops[t].is_ds) *adj=-CLOCK_ADJUST(1); // Branch into delay slot adds an extra cycle
5021 count_plus2 = count + CLOCK_ADJUST(2);
5022 if(taken==TAKEN && i==(ba[i]-start)>>2 && source[i+1]==0) {
5024 if(count&1) emit_addimm_and_set_flags(2*(count+2),HOST_CCREG);
5026 //emit_subfrommem(&idlecount,HOST_CCREG); // Count idle cycles
5027 emit_andimm(HOST_CCREG,3,HOST_CCREG);
5031 else if(*adj==0||invert) {
5032 int cycles = count_plus2;
5037 if(-NO_CYCLE_PENALTY_THR<rel&&rel<0)
5038 cycles=*adj+count+2-*adj;
5041 emit_addimm_and_set_flags(cycles, HOST_CCREG);
5047 emit_cmpimm(HOST_CCREG, -count_plus2);
5051 add_stub(CC_STUB,jaddr,idle?idle:out,(*adj==0||invert||idle)?0:count_plus2,i,addr,taken,0);
5054 static void do_ccstub(int n)
5057 assem_debug("do_ccstub %x\n",start+(u_int)stubs[n].b*4);
5058 set_jump_target(stubs[n].addr, out);
5060 if(stubs[n].d==NULLDS) {
5061 // Delay slot instruction is nullified ("likely" branch)
5062 wb_dirtys(regs[i].regmap,regs[i].dirty);
5064 else if(stubs[n].d!=TAKEN) {
5065 wb_dirtys(branch_regs[i].regmap,branch_regs[i].dirty);
5068 if(internal_branch(ba[i]))
5069 wb_needed_dirtys(branch_regs[i].regmap,branch_regs[i].dirty,ba[i]);
5073 // Save PC as return address
5074 emit_movimm(stubs[n].c,EAX);
5075 emit_writeword(EAX,&pcaddr);
5079 // Return address depends on which way the branch goes
5080 if(dops[i].itype==CJUMP||dops[i].itype==SJUMP)
5082 int s1l=get_reg(branch_regs[i].regmap,dops[i].rs1);
5083 int s2l=get_reg(branch_regs[i].regmap,dops[i].rs2);
5089 else if(dops[i].rs2==0)
5094 #ifdef DESTRUCTIVE_WRITEBACK
5096 if((branch_regs[i].dirty>>s1l)&&1)
5097 emit_loadreg(dops[i].rs1,s1l);
5100 if((branch_regs[i].dirty>>s1l)&1)
5101 emit_loadreg(dops[i].rs2,s1l);
5104 if((branch_regs[i].dirty>>s2l)&1)
5105 emit_loadreg(dops[i].rs2,s2l);
5108 int addr=-1,alt=-1,ntaddr=-1;
5111 if(hr!=EXCLUDE_REG && hr!=HOST_CCREG &&
5112 branch_regs[i].regmap[hr]!=dops[i].rs1 &&
5113 branch_regs[i].regmap[hr]!=dops[i].rs2 )
5121 if(hr!=EXCLUDE_REG && hr!=HOST_CCREG &&
5122 branch_regs[i].regmap[hr]!=dops[i].rs1 &&
5123 branch_regs[i].regmap[hr]!=dops[i].rs2 )
5129 if((dops[i].opcode&0x2E)==6) // BLEZ/BGTZ needs another register
5133 if(hr!=EXCLUDE_REG && hr!=HOST_CCREG &&
5134 branch_regs[i].regmap[hr]!=dops[i].rs1 &&
5135 branch_regs[i].regmap[hr]!=dops[i].rs2 )
5141 assert(hr<HOST_REGS);
5143 if((dops[i].opcode&0x2f)==4) // BEQ
5145 #ifdef HAVE_CMOV_IMM
5146 if(s2l>=0) emit_cmp(s1l,s2l);
5147 else emit_test(s1l,s1l);
5148 emit_cmov2imm_e_ne_compact(ba[i],start+i*4+8,addr);
5150 emit_mov2imm_compact(ba[i],addr,start+i*4+8,alt);
5151 if(s2l>=0) emit_cmp(s1l,s2l);
5152 else emit_test(s1l,s1l);
5153 emit_cmovne_reg(alt,addr);
5156 if((dops[i].opcode&0x2f)==5) // BNE
5158 #ifdef HAVE_CMOV_IMM
5159 if(s2l>=0) emit_cmp(s1l,s2l);
5160 else emit_test(s1l,s1l);
5161 emit_cmov2imm_e_ne_compact(start+i*4+8,ba[i],addr);
5163 emit_mov2imm_compact(start+i*4+8,addr,ba[i],alt);
5164 if(s2l>=0) emit_cmp(s1l,s2l);
5165 else emit_test(s1l,s1l);
5166 emit_cmovne_reg(alt,addr);
5169 if((dops[i].opcode&0x2f)==6) // BLEZ
5171 //emit_movimm(ba[i],alt);
5172 //emit_movimm(start+i*4+8,addr);
5173 emit_mov2imm_compact(ba[i],alt,start+i*4+8,addr);
5175 emit_cmovl_reg(alt,addr);
5177 if((dops[i].opcode&0x2f)==7) // BGTZ
5179 //emit_movimm(ba[i],addr);
5180 //emit_movimm(start+i*4+8,ntaddr);
5181 emit_mov2imm_compact(ba[i],addr,start+i*4+8,ntaddr);
5183 emit_cmovl_reg(ntaddr,addr);
5185 if((dops[i].opcode==1)&&(dops[i].opcode2&0x2D)==0) // BLTZ
5187 //emit_movimm(ba[i],alt);
5188 //emit_movimm(start+i*4+8,addr);
5189 emit_mov2imm_compact(ba[i],alt,start+i*4+8,addr);
5191 emit_cmovs_reg(alt,addr);
5193 if((dops[i].opcode==1)&&(dops[i].opcode2&0x2D)==1) // BGEZ
5195 //emit_movimm(ba[i],addr);
5196 //emit_movimm(start+i*4+8,alt);
5197 emit_mov2imm_compact(ba[i],addr,start+i*4+8,alt);
5199 emit_cmovs_reg(alt,addr);
5201 if(dops[i].opcode==0x11 && dops[i].opcode2==0x08 ) {
5202 if(source[i]&0x10000) // BC1T
5204 //emit_movimm(ba[i],alt);
5205 //emit_movimm(start+i*4+8,addr);
5206 emit_mov2imm_compact(ba[i],alt,start+i*4+8,addr);
5207 emit_testimm(s1l,0x800000);
5208 emit_cmovne_reg(alt,addr);
5212 //emit_movimm(ba[i],addr);
5213 //emit_movimm(start+i*4+8,alt);
5214 emit_mov2imm_compact(ba[i],addr,start+i*4+8,alt);
5215 emit_testimm(s1l,0x800000);
5216 emit_cmovne_reg(alt,addr);
5219 emit_writeword(addr,&pcaddr);
5222 if(dops[i].itype==RJUMP)
5224 int r=get_reg(branch_regs[i].regmap,dops[i].rs1);
5225 if (ds_writes_rjump_rs(i)) {
5226 r=get_reg(branch_regs[i].regmap,RTEMP);
5228 emit_writeword(r,&pcaddr);
5230 else {SysPrintf("Unknown branch type in do_ccstub\n");abort();}
5232 // Update cycle count
5233 assert(branch_regs[i].regmap[HOST_CCREG]==CCREG||branch_regs[i].regmap[HOST_CCREG]==-1);
5234 if(stubs[n].a) emit_addimm(HOST_CCREG,(int)stubs[n].a,HOST_CCREG);
5235 emit_far_call(cc_interrupt);
5236 if(stubs[n].a) emit_addimm(HOST_CCREG,-(int)stubs[n].a,HOST_CCREG);
5237 if(stubs[n].d==TAKEN) {
5238 if(internal_branch(ba[i]))
5239 load_needed_regs(branch_regs[i].regmap,regs[(ba[i]-start)>>2].regmap_entry);
5240 else if(dops[i].itype==RJUMP) {
5241 if(get_reg(branch_regs[i].regmap,RTEMP)>=0)
5242 emit_readword(&pcaddr,get_reg(branch_regs[i].regmap,RTEMP));
5244 emit_loadreg(dops[i].rs1,get_reg(branch_regs[i].regmap,dops[i].rs1));
5246 }else if(stubs[n].d==NOTTAKEN) {
5247 if(i<slen-2) load_needed_regs(branch_regs[i].regmap,regmap_pre[i+2]);
5248 else load_all_regs(branch_regs[i].regmap);
5249 }else if(stubs[n].d==NULLDS) {
5250 // Delay slot instruction is nullified ("likely" branch)
5251 if(i<slen-2) load_needed_regs(regs[i].regmap,regmap_pre[i+2]);
5252 else load_all_regs(regs[i].regmap);
5254 load_all_regs(branch_regs[i].regmap);
5256 if (stubs[n].retaddr)
5257 emit_jmp(stubs[n].retaddr);
5259 do_jump_vaddr(stubs[n].e);
5262 static void add_to_linker(void *addr, u_int target, int is_internal)
5264 assert(linkcount < ARRAY_SIZE(link_addr));
5265 link_addr[linkcount].addr = addr;
5266 link_addr[linkcount].target = target;
5267 link_addr[linkcount].internal = is_internal;
5271 static void ujump_assemble_write_ra(int i)
5274 unsigned int return_address;
5275 rt=get_reg(branch_regs[i].regmap,31);
5276 assem_debug("branch(%d): eax=%d ecx=%d edx=%d ebx=%d ebp=%d esi=%d edi=%d\n",i,branch_regs[i].regmap[0],branch_regs[i].regmap[1],branch_regs[i].regmap[2],branch_regs[i].regmap[3],branch_regs[i].regmap[5],branch_regs[i].regmap[6],branch_regs[i].regmap[7]);
5278 return_address=start+i*4+8;
5281 if(internal_branch(return_address)&&dops[i+1].rt1!=31) {
5282 int temp=-1; // note: must be ds-safe
5286 if(temp>=0) do_miniht_insert(return_address,rt,temp);
5287 else emit_movimm(return_address,rt);
5295 if(i_regmap[temp]!=PTEMP) emit_movimm((uintptr_t)hash_table_get(return_address),temp);
5298 emit_movimm(return_address,rt); // PC into link register
5300 emit_prefetch(hash_table_get(return_address));
5306 static void ujump_assemble(int i, const struct regstat *i_regs)
5309 if(i==(ba[i]-start)>>2) assem_debug("idle loop\n");
5310 address_generation(i+1,i_regs,regs[i].regmap_entry);
5312 int temp=get_reg(branch_regs[i].regmap,PTEMP);
5313 if(dops[i].rt1==31&&temp>=0)
5315 signed char *i_regmap=i_regs->regmap;
5316 int return_address=start+i*4+8;
5317 if(get_reg(branch_regs[i].regmap,31)>0)
5318 if(i_regmap[temp]==PTEMP) emit_movimm((uintptr_t)hash_table_get(return_address),temp);
5321 if(dops[i].rt1==31&&(dops[i].rt1==dops[i+1].rs1||dops[i].rt1==dops[i+1].rs2)) {
5322 ujump_assemble_write_ra(i); // writeback ra for DS
5325 ds_assemble(i+1,i_regs);
5326 uint64_t bc_unneeded=branch_regs[i].u;
5327 bc_unneeded|=1|(1LL<<dops[i].rt1);
5328 wb_invalidate(regs[i].regmap,branch_regs[i].regmap,regs[i].dirty,bc_unneeded);
5329 load_reg(regs[i].regmap,branch_regs[i].regmap,CCREG);
5330 if(!ra_done&&dops[i].rt1==31)
5331 ujump_assemble_write_ra(i);
5333 cc=get_reg(branch_regs[i].regmap,CCREG);
5334 assert(cc==HOST_CCREG);
5335 store_regs_bt(branch_regs[i].regmap,branch_regs[i].dirty,ba[i]);
5337 if(dops[i].rt1==31&&temp>=0) emit_prefetchreg(temp);
5339 do_cc(i,branch_regs[i].regmap,&adj,ba[i],TAKEN,0);
5340 if(adj) emit_addimm(cc, ccadj[i] + CLOCK_ADJUST(2) - adj, cc);
5341 load_regs_bt(branch_regs[i].regmap,branch_regs[i].dirty,ba[i]);
5342 if(internal_branch(ba[i]))
5343 assem_debug("branch: internal\n");
5345 assem_debug("branch: external\n");
5346 if (internal_branch(ba[i]) && dops[(ba[i]-start)>>2].is_ds) {
5347 ds_assemble_entry(i);
5350 add_to_linker(out,ba[i],internal_branch(ba[i]));
5355 static void rjump_assemble_write_ra(int i)
5357 int rt,return_address;
5358 assert(dops[i+1].rt1!=dops[i].rt1);
5359 assert(dops[i+1].rt2!=dops[i].rt1);
5360 rt=get_reg(branch_regs[i].regmap,dops[i].rt1);
5361 assem_debug("branch(%d): eax=%d ecx=%d edx=%d ebx=%d ebp=%d esi=%d edi=%d\n",i,branch_regs[i].regmap[0],branch_regs[i].regmap[1],branch_regs[i].regmap[2],branch_regs[i].regmap[3],branch_regs[i].regmap[5],branch_regs[i].regmap[6],branch_regs[i].regmap[7]);
5363 return_address=start+i*4+8;
5367 if(i_regmap[temp]!=PTEMP) emit_movimm((uintptr_t)hash_table_get(return_address),temp);
5370 emit_movimm(return_address,rt); // PC into link register
5372 emit_prefetch(hash_table_get(return_address));
5376 static void rjump_assemble(int i, const struct regstat *i_regs)
5381 rs=get_reg(branch_regs[i].regmap,dops[i].rs1);
5383 if (ds_writes_rjump_rs(i)) {
5384 // Delay slot abuse, make a copy of the branch address register
5385 temp=get_reg(branch_regs[i].regmap,RTEMP);
5387 assert(regs[i].regmap[temp]==RTEMP);
5391 address_generation(i+1,i_regs,regs[i].regmap_entry);
5395 if((temp=get_reg(branch_regs[i].regmap,PTEMP))>=0) {
5396 signed char *i_regmap=i_regs->regmap;
5397 int return_address=start+i*4+8;
5398 if(i_regmap[temp]==PTEMP) emit_movimm((uintptr_t)hash_table_get(return_address),temp);
5403 if(dops[i].rs1==31) {
5404 int rh=get_reg(regs[i].regmap,RHASH);
5405 if(rh>=0) do_preload_rhash(rh);
5408 if(dops[i].rt1!=0&&(dops[i].rt1==dops[i+1].rs1||dops[i].rt1==dops[i+1].rs2)) {
5409 rjump_assemble_write_ra(i);
5412 ds_assemble(i+1,i_regs);
5413 uint64_t bc_unneeded=branch_regs[i].u;
5414 bc_unneeded|=1|(1LL<<dops[i].rt1);
5415 bc_unneeded&=~(1LL<<dops[i].rs1);
5416 wb_invalidate(regs[i].regmap,branch_regs[i].regmap,regs[i].dirty,bc_unneeded);
5417 load_regs(regs[i].regmap,branch_regs[i].regmap,dops[i].rs1,CCREG);
5418 if(!ra_done&&dops[i].rt1!=0)
5419 rjump_assemble_write_ra(i);
5420 cc=get_reg(branch_regs[i].regmap,CCREG);
5421 assert(cc==HOST_CCREG);
5424 int rh=get_reg(branch_regs[i].regmap,RHASH);
5425 int ht=get_reg(branch_regs[i].regmap,RHTBL);
5426 if(dops[i].rs1==31) {
5427 if(regs[i].regmap[rh]!=RHASH) do_preload_rhash(rh);
5428 do_preload_rhtbl(ht);
5432 store_regs_bt(branch_regs[i].regmap,branch_regs[i].dirty,-1);
5433 #ifdef DESTRUCTIVE_WRITEBACK
5434 if((branch_regs[i].dirty>>rs)&1) {
5435 if(dops[i].rs1!=dops[i+1].rt1&&dops[i].rs1!=dops[i+1].rt2) {
5436 emit_loadreg(dops[i].rs1,rs);
5441 if(dops[i].rt1==31&&temp>=0) emit_prefetchreg(temp);
5444 if(dops[i].rs1==31) {
5445 do_miniht_load(ht,rh);
5448 //do_cc(i,branch_regs[i].regmap,&adj,-1,TAKEN);
5449 //if(adj) emit_addimm(cc,2*(ccadj[i]+2-adj),cc); // ??? - Shouldn't happen
5451 emit_addimm_and_set_flags(ccadj[i] + CLOCK_ADJUST(2), HOST_CCREG);
5452 add_stub(CC_STUB,out,NULL,0,i,-1,TAKEN,rs);
5453 if(dops[i+1].itype==COP0 && dops[i+1].opcode2==0x10)
5454 // special case for RFE
5458 //load_regs_bt(branch_regs[i].regmap,branch_regs[i].dirty,-1);
5460 if(dops[i].rs1==31) {
5461 do_miniht_jump(rs,rh,ht);
5468 #ifdef CORTEX_A8_BRANCH_PREDICTION_HACK
5469 if(dops[i].rt1!=31&&i<slen-2&&(((u_int)out)&7)) emit_mov(13,13);
5473 static void cjump_assemble(int i, const struct regstat *i_regs)
5475 const signed char *i_regmap = i_regs->regmap;
5478 match=match_bt(branch_regs[i].regmap,branch_regs[i].dirty,ba[i]);
5479 assem_debug("match=%d\n",match);
5481 int unconditional=0,nop=0;
5483 int internal=internal_branch(ba[i]);
5484 if(i==(ba[i]-start)>>2) assem_debug("idle loop\n");
5485 if(!match) invert=1;
5486 #ifdef CORTEX_A8_BRANCH_PREDICTION_HACK
5487 if(i>(ba[i]-start)>>2) invert=1;
5490 invert=1; // because of near cond. branches
5494 s1l=get_reg(branch_regs[i].regmap,dops[i].rs1);
5495 s2l=get_reg(branch_regs[i].regmap,dops[i].rs2);
5498 s1l=get_reg(i_regmap,dops[i].rs1);
5499 s2l=get_reg(i_regmap,dops[i].rs2);
5501 if(dops[i].rs1==0&&dops[i].rs2==0)
5503 if(dops[i].opcode&1) nop=1;
5504 else unconditional=1;
5505 //assert(dops[i].opcode!=5);
5506 //assert(dops[i].opcode!=7);
5507 //assert(dops[i].opcode!=0x15);
5508 //assert(dops[i].opcode!=0x17);
5510 else if(dops[i].rs1==0)
5515 else if(dops[i].rs2==0)
5521 // Out of order execution (delay slot first)
5523 address_generation(i+1,i_regs,regs[i].regmap_entry);
5524 ds_assemble(i+1,i_regs);
5526 uint64_t bc_unneeded=branch_regs[i].u;
5527 bc_unneeded&=~((1LL<<dops[i].rs1)|(1LL<<dops[i].rs2));
5529 wb_invalidate(regs[i].regmap,branch_regs[i].regmap,regs[i].dirty,bc_unneeded);
5530 load_regs(regs[i].regmap,branch_regs[i].regmap,dops[i].rs1,dops[i].rs2);
5531 load_reg(regs[i].regmap,branch_regs[i].regmap,CCREG);
5532 cc=get_reg(branch_regs[i].regmap,CCREG);
5533 assert(cc==HOST_CCREG);
5535 store_regs_bt(branch_regs[i].regmap,branch_regs[i].dirty,ba[i]);
5536 //do_cc(i,branch_regs[i].regmap,&adj,unconditional?ba[i]:-1,unconditional);
5537 //assem_debug("cycle count (adj)\n");
5539 do_cc(i,branch_regs[i].regmap,&adj,ba[i],TAKEN,0);
5540 if(i!=(ba[i]-start)>>2 || source[i+1]!=0) {
5541 if(adj) emit_addimm(cc, ccadj[i] + CLOCK_ADJUST(2) - adj, cc);
5542 load_regs_bt(branch_regs[i].regmap,branch_regs[i].dirty,ba[i]);
5544 assem_debug("branch: internal\n");
5546 assem_debug("branch: external\n");
5547 if (internal && dops[(ba[i]-start)>>2].is_ds) {
5548 ds_assemble_entry(i);
5551 add_to_linker(out,ba[i],internal);
5554 #ifdef CORTEX_A8_BRANCH_PREDICTION_HACK
5555 if(((u_int)out)&7) emit_addnop(0);
5560 emit_addimm_and_set_flags(ccadj[i] + CLOCK_ADJUST(2), cc);
5563 add_stub(CC_STUB,jaddr,out,0,i,start+i*4+8,NOTTAKEN,0);
5566 void *taken = NULL, *nottaken = NULL, *nottaken1 = NULL;
5567 do_cc(i,branch_regs[i].regmap,&adj,-1,0,invert);
5568 if(adj&&!invert) emit_addimm(cc, ccadj[i] + CLOCK_ADJUST(2) - adj, cc);
5570 //printf("branch(%d): eax=%d ecx=%d edx=%d ebx=%d ebp=%d esi=%d edi=%d\n",i,branch_regs[i].regmap[0],branch_regs[i].regmap[1],branch_regs[i].regmap[2],branch_regs[i].regmap[3],branch_regs[i].regmap[5],branch_regs[i].regmap[6],branch_regs[i].regmap[7]);
5572 if(dops[i].opcode==4) // BEQ
5574 if(s2l>=0) emit_cmp(s1l,s2l);
5575 else emit_test(s1l,s1l);
5580 add_to_linker(out,ba[i],internal);
5584 if(dops[i].opcode==5) // BNE
5586 if(s2l>=0) emit_cmp(s1l,s2l);
5587 else emit_test(s1l,s1l);
5592 add_to_linker(out,ba[i],internal);
5596 if(dops[i].opcode==6) // BLEZ
5603 add_to_linker(out,ba[i],internal);
5607 if(dops[i].opcode==7) // BGTZ
5614 add_to_linker(out,ba[i],internal);
5619 if(taken) set_jump_target(taken, out);
5620 #ifdef CORTEX_A8_BRANCH_PREDICTION_HACK
5621 if (match && (!internal || !dops[(ba[i]-start)>>2].is_ds)) {
5623 emit_addimm(cc,-adj,cc);
5624 add_to_linker(out,ba[i],internal);
5627 add_to_linker(out,ba[i],internal*2);
5633 if(adj) emit_addimm(cc,-adj,cc);
5634 store_regs_bt(branch_regs[i].regmap,branch_regs[i].dirty,ba[i]);
5635 load_regs_bt(branch_regs[i].regmap,branch_regs[i].dirty,ba[i]);
5637 assem_debug("branch: internal\n");
5639 assem_debug("branch: external\n");
5640 if (internal && dops[(ba[i] - start) >> 2].is_ds) {
5641 ds_assemble_entry(i);
5644 add_to_linker(out,ba[i],internal);
5648 set_jump_target(nottaken, out);
5651 if(nottaken1) set_jump_target(nottaken1, out);
5653 if(!invert) emit_addimm(cc,adj,cc);
5655 } // (!unconditional)
5659 // In-order execution (branch first)
5660 void *taken = NULL, *nottaken = NULL, *nottaken1 = NULL;
5661 if(!unconditional&&!nop) {
5662 //printf("branch(%d): eax=%d ecx=%d edx=%d ebx=%d ebp=%d esi=%d edi=%d\n",i,branch_regs[i].regmap[0],branch_regs[i].regmap[1],branch_regs[i].regmap[2],branch_regs[i].regmap[3],branch_regs[i].regmap[5],branch_regs[i].regmap[6],branch_regs[i].regmap[7]);
5664 if((dops[i].opcode&0x2f)==4) // BEQ
5666 if(s2l>=0) emit_cmp(s1l,s2l);
5667 else emit_test(s1l,s1l);
5671 if((dops[i].opcode&0x2f)==5) // BNE
5673 if(s2l>=0) emit_cmp(s1l,s2l);
5674 else emit_test(s1l,s1l);
5678 if((dops[i].opcode&0x2f)==6) // BLEZ
5684 if((dops[i].opcode&0x2f)==7) // BGTZ
5690 } // if(!unconditional)
5692 uint64_t ds_unneeded=branch_regs[i].u;
5693 ds_unneeded&=~((1LL<<dops[i+1].rs1)|(1LL<<dops[i+1].rs2));
5697 if(taken) set_jump_target(taken, out);
5698 assem_debug("1:\n");
5699 wb_invalidate(regs[i].regmap,branch_regs[i].regmap,regs[i].dirty,ds_unneeded);
5701 load_regs(regs[i].regmap,branch_regs[i].regmap,dops[i+1].rs1,dops[i+1].rs2);
5702 address_generation(i+1,&branch_regs[i],0);
5704 load_reg(regs[i].regmap,branch_regs[i].regmap,ROREG);
5705 load_regs(regs[i].regmap,branch_regs[i].regmap,CCREG,INVCP);
5706 ds_assemble(i+1,&branch_regs[i]);
5707 cc=get_reg(branch_regs[i].regmap,CCREG);
5709 emit_loadreg(CCREG,cc=HOST_CCREG);
5710 // CHECK: Is the following instruction (fall thru) allocated ok?
5712 assert(cc==HOST_CCREG);
5713 store_regs_bt(branch_regs[i].regmap,branch_regs[i].dirty,ba[i]);
5714 do_cc(i,i_regmap,&adj,ba[i],TAKEN,0);
5715 assem_debug("cycle count (adj)\n");
5716 if(adj) emit_addimm(cc, ccadj[i] + CLOCK_ADJUST(2) - adj, cc);
5717 load_regs_bt(branch_regs[i].regmap,branch_regs[i].dirty,ba[i]);
5719 assem_debug("branch: internal\n");
5721 assem_debug("branch: external\n");
5722 if (internal && dops[(ba[i] - start) >> 2].is_ds) {
5723 ds_assemble_entry(i);
5726 add_to_linker(out,ba[i],internal);
5731 if(!unconditional) {
5732 if(nottaken1) set_jump_target(nottaken1, out);
5733 set_jump_target(nottaken, out);
5734 assem_debug("2:\n");
5735 wb_invalidate(regs[i].regmap,branch_regs[i].regmap,regs[i].dirty,ds_unneeded);
5737 load_regs(regs[i].regmap,branch_regs[i].regmap,dops[i+1].rs1,dops[i+1].rs2);
5738 address_generation(i+1,&branch_regs[i],0);
5740 load_reg(regs[i].regmap,branch_regs[i].regmap,ROREG);
5741 load_regs(regs[i].regmap,branch_regs[i].regmap,CCREG,INVCP);
5742 ds_assemble(i+1,&branch_regs[i]);
5743 cc=get_reg(branch_regs[i].regmap,CCREG);
5745 // Cycle count isn't in a register, temporarily load it then write it out
5746 emit_loadreg(CCREG,HOST_CCREG);
5747 emit_addimm_and_set_flags(ccadj[i] + CLOCK_ADJUST(2), HOST_CCREG);
5750 add_stub(CC_STUB,jaddr,out,0,i,start+i*4+8,NOTTAKEN,0);
5751 emit_storereg(CCREG,HOST_CCREG);
5754 cc=get_reg(i_regmap,CCREG);
5755 assert(cc==HOST_CCREG);
5756 emit_addimm_and_set_flags(ccadj[i] + CLOCK_ADJUST(2), cc);
5759 add_stub(CC_STUB,jaddr,out,0,i,start+i*4+8,NOTTAKEN,0);
5765 static void sjump_assemble(int i, const struct regstat *i_regs)
5767 const signed char *i_regmap = i_regs->regmap;
5770 match=match_bt(branch_regs[i].regmap,branch_regs[i].dirty,ba[i]);
5771 assem_debug("smatch=%d ooo=%d\n", match, dops[i].ooo);
5773 int unconditional=0,nevertaken=0;
5775 int internal=internal_branch(ba[i]);
5776 if(i==(ba[i]-start)>>2) assem_debug("idle loop\n");
5777 if(!match) invert=1;
5778 #ifdef CORTEX_A8_BRANCH_PREDICTION_HACK
5779 if(i>(ba[i]-start)>>2) invert=1;
5782 invert=1; // because of near cond. branches
5785 //if(dops[i].opcode2>=0x10) return; // FIXME (BxxZAL)
5786 //assert(dops[i].opcode2<0x10||dops[i].rs1==0); // FIXME (BxxZAL)
5789 s1l=get_reg(branch_regs[i].regmap,dops[i].rs1);
5792 s1l=get_reg(i_regmap,dops[i].rs1);
5796 if(dops[i].opcode2&1) unconditional=1;
5798 // These are never taken (r0 is never less than zero)
5799 //assert(dops[i].opcode2!=0);
5800 //assert(dops[i].opcode2!=2);
5801 //assert(dops[i].opcode2!=0x10);
5802 //assert(dops[i].opcode2!=0x12);
5806 // Out of order execution (delay slot first)
5808 address_generation(i+1,i_regs,regs[i].regmap_entry);
5809 ds_assemble(i+1,i_regs);
5811 uint64_t bc_unneeded=branch_regs[i].u;
5812 bc_unneeded&=~((1LL<<dops[i].rs1)|(1LL<<dops[i].rs2));
5814 wb_invalidate(regs[i].regmap,branch_regs[i].regmap,regs[i].dirty,bc_unneeded);
5815 load_regs(regs[i].regmap,branch_regs[i].regmap,dops[i].rs1,dops[i].rs1);
5816 load_reg(regs[i].regmap,branch_regs[i].regmap,CCREG);
5817 if(dops[i].rt1==31) {
5818 int rt,return_address;
5819 rt=get_reg(branch_regs[i].regmap,31);
5820 assem_debug("branch(%d): eax=%d ecx=%d edx=%d ebx=%d ebp=%d esi=%d edi=%d\n",i,branch_regs[i].regmap[0],branch_regs[i].regmap[1],branch_regs[i].regmap[2],branch_regs[i].regmap[3],branch_regs[i].regmap[5],branch_regs[i].regmap[6],branch_regs[i].regmap[7]);
5822 // Save the PC even if the branch is not taken
5823 return_address=start+i*4+8;
5824 emit_movimm(return_address,rt); // PC into link register
5826 if(!nevertaken) emit_prefetch(hash_table_get(return_address));
5830 cc=get_reg(branch_regs[i].regmap,CCREG);
5831 assert(cc==HOST_CCREG);
5833 store_regs_bt(branch_regs[i].regmap,branch_regs[i].dirty,ba[i]);
5834 //do_cc(i,branch_regs[i].regmap,&adj,unconditional?ba[i]:-1,unconditional);
5835 assem_debug("cycle count (adj)\n");
5837 do_cc(i,branch_regs[i].regmap,&adj,ba[i],TAKEN,0);
5838 if(i!=(ba[i]-start)>>2 || source[i+1]!=0) {
5839 if(adj) emit_addimm(cc, ccadj[i] + CLOCK_ADJUST(2) - adj, cc);
5840 load_regs_bt(branch_regs[i].regmap,branch_regs[i].dirty,ba[i]);
5842 assem_debug("branch: internal\n");
5844 assem_debug("branch: external\n");
5845 if (internal && dops[(ba[i] - start) >> 2].is_ds) {
5846 ds_assemble_entry(i);
5849 add_to_linker(out,ba[i],internal);
5852 #ifdef CORTEX_A8_BRANCH_PREDICTION_HACK
5853 if(((u_int)out)&7) emit_addnop(0);
5857 else if(nevertaken) {
5858 emit_addimm_and_set_flags(ccadj[i] + CLOCK_ADJUST(2), cc);
5861 add_stub(CC_STUB,jaddr,out,0,i,start+i*4+8,NOTTAKEN,0);
5864 void *nottaken = NULL;
5865 do_cc(i,branch_regs[i].regmap,&adj,-1,0,invert);
5866 if(adj&&!invert) emit_addimm(cc, ccadj[i] + CLOCK_ADJUST(2) - adj, cc);
5869 if((dops[i].opcode2&0xf)==0) // BLTZ/BLTZAL
5876 add_to_linker(out,ba[i],internal);
5880 if((dops[i].opcode2&0xf)==1) // BGEZ/BLTZAL
5887 add_to_linker(out,ba[i],internal);
5894 #ifdef CORTEX_A8_BRANCH_PREDICTION_HACK
5895 if (match && (!internal || !dops[(ba[i] - start) >> 2].is_ds)) {
5897 emit_addimm(cc,-adj,cc);
5898 add_to_linker(out,ba[i],internal);
5901 add_to_linker(out,ba[i],internal*2);
5907 if(adj) emit_addimm(cc,-adj,cc);
5908 store_regs_bt(branch_regs[i].regmap,branch_regs[i].dirty,ba[i]);
5909 load_regs_bt(branch_regs[i].regmap,branch_regs[i].dirty,ba[i]);
5911 assem_debug("branch: internal\n");
5913 assem_debug("branch: external\n");
5914 if (internal && dops[(ba[i] - start) >> 2].is_ds) {
5915 ds_assemble_entry(i);
5918 add_to_linker(out,ba[i],internal);
5922 set_jump_target(nottaken, out);
5926 if(!invert) emit_addimm(cc,adj,cc);
5928 } // (!unconditional)
5932 // In-order execution (branch first)
5934 void *nottaken = NULL;
5935 if(dops[i].rt1==31) {
5936 int rt,return_address;
5937 rt=get_reg(branch_regs[i].regmap,31);
5939 // Save the PC even if the branch is not taken
5940 return_address=start+i*4+8;
5941 emit_movimm(return_address,rt); // PC into link register
5943 emit_prefetch(hash_table_get(return_address));
5947 if(!unconditional) {
5948 //printf("branch(%d): eax=%d ecx=%d edx=%d ebx=%d ebp=%d esi=%d edi=%d\n",i,branch_regs[i].regmap[0],branch_regs[i].regmap[1],branch_regs[i].regmap[2],branch_regs[i].regmap[3],branch_regs[i].regmap[5],branch_regs[i].regmap[6],branch_regs[i].regmap[7]);
5950 if((dops[i].opcode2&0x0d)==0) // BLTZ/BLTZL/BLTZAL/BLTZALL
5956 if((dops[i].opcode2&0x0d)==1) // BGEZ/BGEZL/BGEZAL/BGEZALL
5962 } // if(!unconditional)
5964 uint64_t ds_unneeded=branch_regs[i].u;
5965 ds_unneeded&=~((1LL<<dops[i+1].rs1)|(1LL<<dops[i+1].rs2));
5969 //assem_debug("1:\n");
5970 wb_invalidate(regs[i].regmap,branch_regs[i].regmap,regs[i].dirty,ds_unneeded);
5972 load_regs(regs[i].regmap,branch_regs[i].regmap,dops[i+1].rs1,dops[i+1].rs2);
5973 address_generation(i+1,&branch_regs[i],0);
5975 load_reg(regs[i].regmap,branch_regs[i].regmap,ROREG);
5976 load_regs(regs[i].regmap,branch_regs[i].regmap,CCREG,INVCP);
5977 ds_assemble(i+1,&branch_regs[i]);
5978 cc=get_reg(branch_regs[i].regmap,CCREG);
5980 emit_loadreg(CCREG,cc=HOST_CCREG);
5981 // CHECK: Is the following instruction (fall thru) allocated ok?
5983 assert(cc==HOST_CCREG);
5984 store_regs_bt(branch_regs[i].regmap,branch_regs[i].dirty,ba[i]);
5985 do_cc(i,i_regmap,&adj,ba[i],TAKEN,0);
5986 assem_debug("cycle count (adj)\n");
5987 if(adj) emit_addimm(cc, ccadj[i] + CLOCK_ADJUST(2) - adj, cc);
5988 load_regs_bt(branch_regs[i].regmap,branch_regs[i].dirty,ba[i]);
5990 assem_debug("branch: internal\n");
5992 assem_debug("branch: external\n");
5993 if (internal && dops[(ba[i] - start) >> 2].is_ds) {
5994 ds_assemble_entry(i);
5997 add_to_linker(out,ba[i],internal);
6002 if(!unconditional) {
6003 set_jump_target(nottaken, out);
6004 assem_debug("1:\n");
6005 wb_invalidate(regs[i].regmap,branch_regs[i].regmap,regs[i].dirty,ds_unneeded);
6006 load_regs(regs[i].regmap,branch_regs[i].regmap,dops[i+1].rs1,dops[i+1].rs2);
6007 address_generation(i+1,&branch_regs[i],0);
6009 load_reg(regs[i].regmap,branch_regs[i].regmap,ROREG);
6010 load_regs(regs[i].regmap,branch_regs[i].regmap,CCREG,INVCP);
6011 ds_assemble(i+1,&branch_regs[i]);
6012 cc=get_reg(branch_regs[i].regmap,CCREG);
6014 // Cycle count isn't in a register, temporarily load it then write it out
6015 emit_loadreg(CCREG,HOST_CCREG);
6016 emit_addimm_and_set_flags(ccadj[i] + CLOCK_ADJUST(2), HOST_CCREG);
6019 add_stub(CC_STUB,jaddr,out,0,i,start+i*4+8,NOTTAKEN,0);
6020 emit_storereg(CCREG,HOST_CCREG);
6023 cc=get_reg(i_regmap,CCREG);
6024 assert(cc==HOST_CCREG);
6025 emit_addimm_and_set_flags(ccadj[i] + CLOCK_ADJUST(2), cc);
6028 add_stub(CC_STUB,jaddr,out,0,i,start+i*4+8,NOTTAKEN,0);
6034 static void check_regmap(signed char *regmap)
6038 for (i = 0; i < HOST_REGS; i++) {
6041 for (j = i + 1; j < HOST_REGS; j++)
6042 assert(regmap[i] != regmap[j]);
6048 #include <inttypes.h>
6049 static char insn[MAXBLOCK][10];
6051 #define set_mnemonic(i_, n_) \
6052 strcpy(insn[i_], n_)
6054 void print_regmap(const char *name, const signed char *regmap)
6058 fputs(name, stdout);
6059 for (i = 0; i < HOST_REGS; i++) {
6062 l = snprintf(buf, sizeof(buf), "$%d", regmap[i]);
6066 printf(" r%d=%s", i, buf);
6068 fputs("\n", stdout);
6072 void disassemble_inst(int i)
6074 if (dops[i].bt) printf("*"); else printf(" ");
6075 switch(dops[i].itype) {
6077 printf (" %x: %s %8x\n",start+i*4,insn[i],ba[i]);break;
6079 printf (" %x: %s r%d,r%d,%8x\n",start+i*4,insn[i],dops[i].rs1,dops[i].rs2,i?start+i*4+4+((signed int)((unsigned int)source[i]<<16)>>14):*ba);break;
6081 printf (" %x: %s r%d,%8x\n",start+i*4,insn[i],dops[i].rs1,start+i*4+4+((signed int)((unsigned int)source[i]<<16)>>14));break;
6083 if (dops[i].opcode==0x9&&dops[i].rt1!=31)
6084 printf (" %x: %s r%d,r%d\n",start+i*4,insn[i],dops[i].rt1,dops[i].rs1);
6086 printf (" %x: %s r%d\n",start+i*4,insn[i],dops[i].rs1);
6089 if(dops[i].opcode==0xf) //LUI
6090 printf (" %x: %s r%d,%4x0000\n",start+i*4,insn[i],dops[i].rt1,imm[i]&0xffff);
6092 printf (" %x: %s r%d,r%d,%d\n",start+i*4,insn[i],dops[i].rt1,dops[i].rs1,imm[i]);
6096 printf (" %x: %s r%d,r%d+%x\n",start+i*4,insn[i],dops[i].rt1,dops[i].rs1,imm[i]);
6100 printf (" %x: %s r%d,r%d+%x\n",start+i*4,insn[i],dops[i].rs2,dops[i].rs1,imm[i]);
6104 printf (" %x: %s r%d,r%d,r%d\n",start+i*4,insn[i],dops[i].rt1,dops[i].rs1,dops[i].rs2);
6107 printf (" %x: %s r%d,r%d\n",start+i*4,insn[i],dops[i].rs1,dops[i].rs2);
6110 printf (" %x: %s r%d,r%d,%d\n",start+i*4,insn[i],dops[i].rt1,dops[i].rs1,imm[i]);
6113 if((dops[i].opcode2&0x1d)==0x10)
6114 printf (" %x: %s r%d\n",start+i*4,insn[i],dops[i].rt1);
6115 else if((dops[i].opcode2&0x1d)==0x11)
6116 printf (" %x: %s r%d\n",start+i*4,insn[i],dops[i].rs1);
6118 printf (" %x: %s\n",start+i*4,insn[i]);
6121 if(dops[i].opcode2==0)
6122 printf (" %x: %s r%d,cpr0[%d]\n",start+i*4,insn[i],dops[i].rt1,(source[i]>>11)&0x1f); // MFC0
6123 else if(dops[i].opcode2==4)
6124 printf (" %x: %s r%d,cpr0[%d]\n",start+i*4,insn[i],dops[i].rs1,(source[i]>>11)&0x1f); // MTC0
6125 else printf (" %x: %s\n",start+i*4,insn[i]);
6128 if(dops[i].opcode2<3)
6129 printf (" %x: %s r%d,cpr1[%d]\n",start+i*4,insn[i],dops[i].rt1,(source[i]>>11)&0x1f); // MFC1
6130 else if(dops[i].opcode2>3)
6131 printf (" %x: %s r%d,cpr1[%d]\n",start+i*4,insn[i],dops[i].rs1,(source[i]>>11)&0x1f); // MTC1
6132 else printf (" %x: %s\n",start+i*4,insn[i]);
6135 if(dops[i].opcode2<3)
6136 printf (" %x: %s r%d,cpr2[%d]\n",start+i*4,insn[i],dops[i].rt1,(source[i]>>11)&0x1f); // MFC2
6137 else if(dops[i].opcode2>3)
6138 printf (" %x: %s r%d,cpr2[%d]\n",start+i*4,insn[i],dops[i].rs1,(source[i]>>11)&0x1f); // MTC2
6139 else printf (" %x: %s\n",start+i*4,insn[i]);
6142 printf (" %x: %s cpr1[%d],r%d+%x\n",start+i*4,insn[i],(source[i]>>16)&0x1f,dops[i].rs1,imm[i]);
6145 printf (" %x: %s cpr2[%d],r%d+%x\n",start+i*4,insn[i],(source[i]>>16)&0x1f,dops[i].rs1,imm[i]);
6148 printf (" %x: %s (INTCALL)\n",start+i*4,insn[i]);
6151 //printf (" %s %8x\n",insn[i],source[i]);
6152 printf (" %x: %s\n",start+i*4,insn[i]);
6155 printf("D: %"PRIu64" WD: %"PRIu64" U: %"PRIu64"\n",
6156 regs[i].dirty, regs[i].wasdirty, unneeded_reg[i]);
6157 print_regmap("pre: ", regmap_pre[i]);
6158 print_regmap("entry: ", regs[i].regmap_entry);
6159 print_regmap("map: ", regs[i].regmap);
6160 if (dops[i].is_jump) {
6161 print_regmap("bentry:", branch_regs[i].regmap_entry);
6162 print_regmap("bmap: ", branch_regs[i].regmap);
6166 #define set_mnemonic(i_, n_)
6167 static void disassemble_inst(int i) {}
6170 #define DRC_TEST_VAL 0x74657374
6172 static noinline void new_dynarec_test(void)
6174 int (*testfunc)(void);
6179 // check structure linkage
6180 if ((u_char *)rcnts - (u_char *)&psxRegs != sizeof(psxRegs))
6182 SysPrintf("linkage_arm* miscompilation/breakage detected.\n");
6185 SysPrintf("(%p) testing if we can run recompiled code @%p...\n",
6186 new_dynarec_test, out);
6187 ((volatile u_int *)NDRC_WRITE_OFFSET(out))[0]++; // make the cache dirty
6189 for (i = 0; i < ARRAY_SIZE(ret); i++) {
6190 out = ndrc->translation_cache;
6191 beginning = start_block();
6192 emit_movimm(DRC_TEST_VAL + i, 0); // test
6195 end_block(beginning);
6196 testfunc = beginning;
6197 ret[i] = testfunc();
6200 if (ret[0] == DRC_TEST_VAL && ret[1] == DRC_TEST_VAL + 1)
6201 SysPrintf("test passed.\n");
6203 SysPrintf("test failed, will likely crash soon (r=%08x %08x)\n", ret[0], ret[1]);
6204 out = ndrc->translation_cache;
6207 // clear the state completely, instead of just marking
6208 // things invalid like invalidate_all_pages() does
6209 void new_dynarec_clear_full(void)
6212 out = ndrc->translation_cache;
6213 memset(invalid_code,1,sizeof(invalid_code));
6214 memset(hash_table,0xff,sizeof(hash_table));
6215 memset(mini_ht,-1,sizeof(mini_ht));
6216 memset(shadow,0,sizeof(shadow));
6218 expirep = EXPIRITY_OFFSET;
6219 pending_exception=0;
6222 inv_code_start=inv_code_end=~0;
6225 for (n = 0; n < ARRAY_SIZE(blocks); n++)
6226 blocks_clear(&blocks[n]);
6227 for (n = 0; n < ARRAY_SIZE(jumps); n++) {
6231 stat_clear(stat_blocks);
6232 stat_clear(stat_links);
6234 cycle_multiplier_old = Config.cycle_multiplier;
6235 new_dynarec_hacks_old = new_dynarec_hacks;
6238 void new_dynarec_init(void)
6240 SysPrintf("Init new dynarec, ndrc size %x\n", (int)sizeof(*ndrc));
6245 #ifdef BASE_ADDR_DYNAMIC
6247 sceBlock = getVMBlock(); //sceKernelAllocMemBlockForVM("code", sizeof(*ndrc));
6249 SysPrintf("sceKernelAllocMemBlockForVM failed: %x\n", sceBlock);
6250 int ret = sceKernelGetMemBlockBase(sceBlock, (void **)&ndrc);
6252 SysPrintf("sceKernelGetMemBlockBase failed: %x\n", ret);
6253 sceKernelOpenVMDomain();
6254 sceClibPrintf("translation_cache = 0x%08lx\n ", (long)ndrc->translation_cache);
6255 #elif defined(_MSC_VER)
6256 ndrc = VirtualAlloc(NULL, sizeof(*ndrc), MEM_COMMIT | MEM_RESERVE,
6257 PAGE_EXECUTE_READWRITE);
6258 #elif defined(HAVE_LIBNX)
6259 Result rc = jitCreate(&g_jit, sizeof(*ndrc));
6261 SysPrintf("jitCreate failed: %08x\n", rc);
6262 SysPrintf("jitCreate: RX: %p RW: %p type: %d\n", g_jit.rx_addr, g_jit.rw_addr, g_jit.type);
6263 jitTransitionToWritable(&g_jit);
6264 ndrc = g_jit.rx_addr;
6265 ndrc_write_ofs = (char *)g_jit.rw_addr - (char *)ndrc;
6266 memset(NDRC_WRITE_OFFSET(&ndrc->tramp), 0, sizeof(ndrc->tramp));
6268 uintptr_t desired_addr = 0;
6269 int prot = PROT_READ | PROT_WRITE | PROT_EXEC;
6270 int flags = MAP_PRIVATE | MAP_ANONYMOUS;
6274 desired_addr = ((uintptr_t)&_end + 0xffffff) & ~0xffffffl;
6276 #ifdef TC_WRITE_OFFSET
6277 // mostly for testing
6278 fd = open("/dev/shm/pcsxr", O_CREAT | O_RDWR, 0600);
6279 ftruncate(fd, sizeof(*ndrc));
6280 void *mw = mmap(NULL, sizeof(*ndrc), PROT_READ | PROT_WRITE,
6281 (flags = MAP_SHARED), fd, 0);
6282 assert(mw != MAP_FAILED);
6283 prot = PROT_READ | PROT_EXEC;
6285 ndrc = mmap((void *)desired_addr, sizeof(*ndrc), prot, flags, fd, 0);
6286 if (ndrc == MAP_FAILED) {
6287 SysPrintf("mmap() failed: %s\n", strerror(errno));
6290 #ifdef TC_WRITE_OFFSET
6291 ndrc_write_ofs = (char *)mw - (char *)ndrc;
6295 #ifndef NO_WRITE_EXEC
6296 // not all systems allow execute in data segment by default
6297 // size must be 4K aligned for 3DS?
6298 if (mprotect(ndrc, sizeof(*ndrc),
6299 PROT_READ | PROT_WRITE | PROT_EXEC) != 0)
6300 SysPrintf("mprotect() failed: %s\n", strerror(errno));
6303 out = ndrc->translation_cache;
6304 new_dynarec_clear_full();
6306 // Copy this into local area so we don't have to put it in every literal pool
6307 invc_ptr=invalid_code;
6311 ram_offset=(uintptr_t)rdram-0x80000000;
6313 SysPrintf("warning: RAM is not directly mapped, performance will suffer\n");
6314 SysPrintf("Mapped (RAM/scrp/ROM/LUTs/TC):\n");
6315 SysPrintf("%p/%p/%p/%p/%p\n", psxM, psxH, psxR, mem_rtab, out);
6318 void new_dynarec_cleanup(void)
6321 #ifdef BASE_ADDR_DYNAMIC
6323 // sceBlock is managed by retroarch's bootstrap code
6324 //sceKernelFreeMemBlock(sceBlock);
6326 #elif defined(HAVE_LIBNX)
6330 if (munmap(ndrc, sizeof(*ndrc)) < 0)
6331 SysPrintf("munmap() failed\n");
6335 for (n = 0; n < ARRAY_SIZE(blocks); n++)
6336 blocks_clear(&blocks[n]);
6337 for (n = 0; n < ARRAY_SIZE(jumps); n++) {
6341 stat_clear(stat_blocks);
6342 stat_clear(stat_links);
6343 new_dynarec_print_stats();
6346 static u_int *get_source_start(u_int addr, u_int *limit)
6348 if (addr < 0x00200000 ||
6349 (0xa0000000 <= addr && addr < 0xa0200000))
6351 // used for BIOS calls mostly?
6352 *limit = (addr&0xa0000000)|0x00200000;
6353 return (u_int *)(rdram + (addr&0x1fffff));
6355 else if (!Config.HLE && (
6356 /* (0x9fc00000 <= addr && addr < 0x9fc80000) ||*/
6357 (0xbfc00000 <= addr && addr < 0xbfc80000)))
6359 // BIOS. The multiplier should be much higher as it's uncached 8bit mem,
6360 // but timings in PCSX are too tied to the interpreter's 2-per-insn assumption
6361 if (!HACK_ENABLED(NDHACK_OVERRIDE_CYCLE_M))
6362 cycle_multiplier_active = 200;
6364 *limit = (addr & 0xfff00000) | 0x80000;
6365 return (u_int *)((u_char *)psxR + (addr&0x7ffff));
6367 else if (addr >= 0x80000000 && addr < 0x80000000+RAM_SIZE) {
6368 *limit = (addr & 0x80600000) + 0x00200000;
6369 return (u_int *)(rdram + (addr&0x1fffff));
6374 static u_int scan_for_ret(u_int addr)
6379 mem = get_source_start(addr, &limit);
6383 if (limit > addr + 0x1000)
6384 limit = addr + 0x1000;
6385 for (; addr < limit; addr += 4, mem++) {
6386 if (*mem == 0x03e00008) // jr $ra
6392 struct savestate_block {
6397 static int addr_cmp(const void *p1_, const void *p2_)
6399 const struct savestate_block *p1 = p1_, *p2 = p2_;
6400 return p1->addr - p2->addr;
6403 int new_dynarec_save_blocks(void *save, int size)
6405 struct savestate_block *sblocks = save;
6406 int maxcount = size / sizeof(sblocks[0]);
6407 struct savestate_block tmp_blocks[1024];
6408 struct block_info *block;
6409 int p, s, d, o, bcnt;
6413 for (p = 0; p < ARRAY_SIZE(blocks); p++) {
6415 for (block = blocks[p]; block != NULL; block = block->next) {
6416 if (block->is_dirty)
6418 tmp_blocks[bcnt].addr = block->start;
6419 tmp_blocks[bcnt].regflags = block->reg_sv_flags;
6424 qsort(tmp_blocks, bcnt, sizeof(tmp_blocks[0]), addr_cmp);
6426 addr = tmp_blocks[0].addr;
6427 for (s = d = 0; s < bcnt; s++) {
6428 if (tmp_blocks[s].addr < addr)
6430 if (d == 0 || tmp_blocks[d-1].addr != tmp_blocks[s].addr)
6431 tmp_blocks[d++] = tmp_blocks[s];
6432 addr = scan_for_ret(tmp_blocks[s].addr);
6435 if (o + d > maxcount)
6437 memcpy(&sblocks[o], tmp_blocks, d * sizeof(sblocks[0]));
6441 return o * sizeof(sblocks[0]);
6444 void new_dynarec_load_blocks(const void *save, int size)
6446 const struct savestate_block *sblocks = save;
6447 int count = size / sizeof(sblocks[0]);
6448 struct block_info *block;
6449 u_int regs_save[32];
6454 // restore clean blocks, if any
6455 for (page = 0, b = i = 0; page < ARRAY_SIZE(blocks); page++) {
6456 for (block = blocks[page]; block != NULL; block = block->next, b++) {
6457 if (!block->is_dirty)
6459 assert(block->source && block->copy);
6460 if (memcmp(block->source, block->copy, block->len))
6463 // see try_restore_block
6464 block->is_dirty = 0;
6465 mark_invalid_code(block->start, block->len, 0);
6469 inv_debug("load_blocks: %d/%d clean blocks\n", i, b);
6471 // change GPRs for speculation to at least partially work..
6472 memcpy(regs_save, &psxRegs.GPR, sizeof(regs_save));
6473 for (i = 1; i < 32; i++)
6474 psxRegs.GPR.r[i] = 0x80000000;
6476 for (b = 0; b < count; b++) {
6477 for (f = sblocks[b].regflags, i = 0; f; f >>= 1, i++) {
6479 psxRegs.GPR.r[i] = 0x1f800000;
6482 ndrc_get_addr_ht(sblocks[b].addr);
6484 for (f = sblocks[b].regflags, i = 0; f; f >>= 1, i++) {
6486 psxRegs.GPR.r[i] = 0x80000000;
6490 memcpy(&psxRegs.GPR, regs_save, sizeof(regs_save));
6493 void new_dynarec_print_stats(void)
6496 printf("cc %3d,%3d,%3d lu%6d,%3d,%3d c%3d inv%3d,%3d tc_offs %zu b %u,%u\n",
6497 stat_bc_pre, stat_bc_direct, stat_bc_restore,
6498 stat_ht_lookups, stat_jump_in_lookups, stat_restore_tries,
6499 stat_restore_compares, stat_inv_addr_calls, stat_inv_hits,
6500 out - ndrc->translation_cache, stat_blocks, stat_links);
6501 stat_bc_direct = stat_bc_pre = stat_bc_restore =
6502 stat_ht_lookups = stat_jump_in_lookups = stat_restore_tries =
6503 stat_restore_compares = stat_inv_addr_calls = stat_inv_hits = 0;
6507 static int apply_hacks(void)
6510 if (HACK_ENABLED(NDHACK_NO_COMPAT_HACKS))
6512 /* special hack(s) */
6513 for (i = 0; i < slen - 4; i++)
6515 // lui a4, 0xf200; jal <rcnt_read>; addu a0, 2; slti v0, 28224
6516 if (source[i] == 0x3c04f200 && dops[i+1].itype == UJUMP
6517 && source[i+2] == 0x34840002 && dops[i+3].opcode == 0x0a
6518 && imm[i+3] == 0x6e40 && dops[i+3].rs1 == 2)
6520 SysPrintf("PE2 hack @%08x\n", start + (i+3)*4);
6521 dops[i + 3].itype = NOP;
6525 if (i > 10 && source[i-1] == 0 && source[i-2] == 0x03e00008
6526 && source[i-4] == 0x8fbf0018 && source[i-6] == 0x00c0f809
6527 && dops[i-7].itype == STORE)
6530 if (dops[i].itype == IMM16)
6532 // swl r2, 15(r6); swr r2, 12(r6); sw r6, *; jalr r6
6533 if (dops[i].itype == STORELR && dops[i].rs1 == 6
6534 && dops[i-1].itype == STORELR && dops[i-1].rs1 == 6)
6536 SysPrintf("F1 hack from %08x, old dst %08x\n", start, hack_addr);
6544 static noinline void pass1_disassemble(u_int pagelimit)
6546 int i, j, done = 0, ni_count = 0;
6547 unsigned int type,op,op2;
6549 for (i = 0; !done; i++)
6551 memset(&dops[i], 0, sizeof(dops[i]));
6553 minimum_free_regs[i]=0;
6554 dops[i].opcode=op=source[i]>>26;
6557 case 0x00: set_mnemonic(i, "special"); type=NI;
6561 case 0x00: set_mnemonic(i, "SLL"); type=SHIFTIMM; break;
6562 case 0x02: set_mnemonic(i, "SRL"); type=SHIFTIMM; break;
6563 case 0x03: set_mnemonic(i, "SRA"); type=SHIFTIMM; break;
6564 case 0x04: set_mnemonic(i, "SLLV"); type=SHIFT; break;
6565 case 0x06: set_mnemonic(i, "SRLV"); type=SHIFT; break;
6566 case 0x07: set_mnemonic(i, "SRAV"); type=SHIFT; break;
6567 case 0x08: set_mnemonic(i, "JR"); type=RJUMP; break;
6568 case 0x09: set_mnemonic(i, "JALR"); type=RJUMP; break;
6569 case 0x0C: set_mnemonic(i, "SYSCALL"); type=SYSCALL; break;
6570 case 0x0D: set_mnemonic(i, "BREAK"); type=SYSCALL; break;
6571 case 0x0F: set_mnemonic(i, "SYNC"); type=OTHER; break;
6572 case 0x10: set_mnemonic(i, "MFHI"); type=MOV; break;
6573 case 0x11: set_mnemonic(i, "MTHI"); type=MOV; break;
6574 case 0x12: set_mnemonic(i, "MFLO"); type=MOV; break;
6575 case 0x13: set_mnemonic(i, "MTLO"); type=MOV; break;
6576 case 0x18: set_mnemonic(i, "MULT"); type=MULTDIV; break;
6577 case 0x19: set_mnemonic(i, "MULTU"); type=MULTDIV; break;
6578 case 0x1A: set_mnemonic(i, "DIV"); type=MULTDIV; break;
6579 case 0x1B: set_mnemonic(i, "DIVU"); type=MULTDIV; break;
6580 case 0x20: set_mnemonic(i, "ADD"); type=ALU; break;
6581 case 0x21: set_mnemonic(i, "ADDU"); type=ALU; break;
6582 case 0x22: set_mnemonic(i, "SUB"); type=ALU; break;
6583 case 0x23: set_mnemonic(i, "SUBU"); type=ALU; break;
6584 case 0x24: set_mnemonic(i, "AND"); type=ALU; break;
6585 case 0x25: set_mnemonic(i, "OR"); type=ALU; break;
6586 case 0x26: set_mnemonic(i, "XOR"); type=ALU; break;
6587 case 0x27: set_mnemonic(i, "NOR"); type=ALU; break;
6588 case 0x2A: set_mnemonic(i, "SLT"); type=ALU; break;
6589 case 0x2B: set_mnemonic(i, "SLTU"); type=ALU; break;
6590 case 0x30: set_mnemonic(i, "TGE"); type=NI; break;
6591 case 0x31: set_mnemonic(i, "TGEU"); type=NI; break;
6592 case 0x32: set_mnemonic(i, "TLT"); type=NI; break;
6593 case 0x33: set_mnemonic(i, "TLTU"); type=NI; break;
6594 case 0x34: set_mnemonic(i, "TEQ"); type=NI; break;
6595 case 0x36: set_mnemonic(i, "TNE"); type=NI; break;
6597 case 0x14: set_mnemonic(i, "DSLLV"); type=SHIFT; break;
6598 case 0x16: set_mnemonic(i, "DSRLV"); type=SHIFT; break;
6599 case 0x17: set_mnemonic(i, "DSRAV"); type=SHIFT; break;
6600 case 0x1C: set_mnemonic(i, "DMULT"); type=MULTDIV; break;
6601 case 0x1D: set_mnemonic(i, "DMULTU"); type=MULTDIV; break;
6602 case 0x1E: set_mnemonic(i, "DDIV"); type=MULTDIV; break;
6603 case 0x1F: set_mnemonic(i, "DDIVU"); type=MULTDIV; break;
6604 case 0x2C: set_mnemonic(i, "DADD"); type=ALU; break;
6605 case 0x2D: set_mnemonic(i, "DADDU"); type=ALU; break;
6606 case 0x2E: set_mnemonic(i, "DSUB"); type=ALU; break;
6607 case 0x2F: set_mnemonic(i, "DSUBU"); type=ALU; break;
6608 case 0x38: set_mnemonic(i, "DSLL"); type=SHIFTIMM; break;
6609 case 0x3A: set_mnemonic(i, "DSRL"); type=SHIFTIMM; break;
6610 case 0x3B: set_mnemonic(i, "DSRA"); type=SHIFTIMM; break;
6611 case 0x3C: set_mnemonic(i, "DSLL32"); type=SHIFTIMM; break;
6612 case 0x3E: set_mnemonic(i, "DSRL32"); type=SHIFTIMM; break;
6613 case 0x3F: set_mnemonic(i, "DSRA32"); type=SHIFTIMM; break;
6617 case 0x01: set_mnemonic(i, "regimm"); type=NI;
6618 op2=(source[i]>>16)&0x1f;
6621 case 0x00: set_mnemonic(i, "BLTZ"); type=SJUMP; break;
6622 case 0x01: set_mnemonic(i, "BGEZ"); type=SJUMP; break;
6623 //case 0x02: set_mnemonic(i, "BLTZL"); type=SJUMP; break;
6624 //case 0x03: set_mnemonic(i, "BGEZL"); type=SJUMP; break;
6625 //case 0x08: set_mnemonic(i, "TGEI"); type=NI; break;
6626 //case 0x09: set_mnemonic(i, "TGEIU"); type=NI; break;
6627 //case 0x0A: set_mnemonic(i, "TLTI"); type=NI; break;
6628 //case 0x0B: set_mnemonic(i, "TLTIU"); type=NI; break;
6629 //case 0x0C: set_mnemonic(i, "TEQI"); type=NI; break;
6630 //case 0x0E: set_mnemonic(i, "TNEI"); type=NI; break;
6631 case 0x10: set_mnemonic(i, "BLTZAL"); type=SJUMP; break;
6632 case 0x11: set_mnemonic(i, "BGEZAL"); type=SJUMP; break;
6633 //case 0x12: set_mnemonic(i, "BLTZALL"); type=SJUMP; break;
6634 //case 0x13: set_mnemonic(i, "BGEZALL"); type=SJUMP; break;
6637 case 0x02: set_mnemonic(i, "J"); type=UJUMP; break;
6638 case 0x03: set_mnemonic(i, "JAL"); type=UJUMP; break;
6639 case 0x04: set_mnemonic(i, "BEQ"); type=CJUMP; break;
6640 case 0x05: set_mnemonic(i, "BNE"); type=CJUMP; break;
6641 case 0x06: set_mnemonic(i, "BLEZ"); type=CJUMP; break;
6642 case 0x07: set_mnemonic(i, "BGTZ"); type=CJUMP; break;
6643 case 0x08: set_mnemonic(i, "ADDI"); type=IMM16; break;
6644 case 0x09: set_mnemonic(i, "ADDIU"); type=IMM16; break;
6645 case 0x0A: set_mnemonic(i, "SLTI"); type=IMM16; break;
6646 case 0x0B: set_mnemonic(i, "SLTIU"); type=IMM16; break;
6647 case 0x0C: set_mnemonic(i, "ANDI"); type=IMM16; break;
6648 case 0x0D: set_mnemonic(i, "ORI"); type=IMM16; break;
6649 case 0x0E: set_mnemonic(i, "XORI"); type=IMM16; break;
6650 case 0x0F: set_mnemonic(i, "LUI"); type=IMM16; break;
6651 case 0x10: set_mnemonic(i, "cop0"); type=NI;
6652 op2=(source[i]>>21)&0x1f;
6655 case 0x00: set_mnemonic(i, "MFC0"); type=COP0; break;
6656 case 0x02: set_mnemonic(i, "CFC0"); type=COP0; break;
6657 case 0x04: set_mnemonic(i, "MTC0"); type=COP0; break;
6658 case 0x06: set_mnemonic(i, "CTC0"); type=COP0; break;
6659 case 0x10: set_mnemonic(i, "RFE"); type=COP0; break;
6662 case 0x11: set_mnemonic(i, "cop1"); type=COP1;
6663 op2=(source[i]>>21)&0x1f;
6666 case 0x14: set_mnemonic(i, "BEQL"); type=CJUMP; break;
6667 case 0x15: set_mnemonic(i, "BNEL"); type=CJUMP; break;
6668 case 0x16: set_mnemonic(i, "BLEZL"); type=CJUMP; break;
6669 case 0x17: set_mnemonic(i, "BGTZL"); type=CJUMP; break;
6670 case 0x18: set_mnemonic(i, "DADDI"); type=IMM16; break;
6671 case 0x19: set_mnemonic(i, "DADDIU"); type=IMM16; break;
6672 case 0x1A: set_mnemonic(i, "LDL"); type=LOADLR; break;
6673 case 0x1B: set_mnemonic(i, "LDR"); type=LOADLR; break;
6675 case 0x20: set_mnemonic(i, "LB"); type=LOAD; break;
6676 case 0x21: set_mnemonic(i, "LH"); type=LOAD; break;
6677 case 0x22: set_mnemonic(i, "LWL"); type=LOADLR; break;
6678 case 0x23: set_mnemonic(i, "LW"); type=LOAD; break;
6679 case 0x24: set_mnemonic(i, "LBU"); type=LOAD; break;
6680 case 0x25: set_mnemonic(i, "LHU"); type=LOAD; break;
6681 case 0x26: set_mnemonic(i, "LWR"); type=LOADLR; break;
6683 case 0x27: set_mnemonic(i, "LWU"); type=LOAD; break;
6685 case 0x28: set_mnemonic(i, "SB"); type=STORE; break;
6686 case 0x29: set_mnemonic(i, "SH"); type=STORE; break;
6687 case 0x2A: set_mnemonic(i, "SWL"); type=STORELR; break;
6688 case 0x2B: set_mnemonic(i, "SW"); type=STORE; break;
6690 case 0x2C: set_mnemonic(i, "SDL"); type=STORELR; break;
6691 case 0x2D: set_mnemonic(i, "SDR"); type=STORELR; break;
6693 case 0x2E: set_mnemonic(i, "SWR"); type=STORELR; break;
6694 case 0x2F: set_mnemonic(i, "CACHE"); type=NOP; break;
6695 case 0x30: set_mnemonic(i, "LL"); type=NI; break;
6696 case 0x31: set_mnemonic(i, "LWC1"); type=C1LS; break;
6698 case 0x34: set_mnemonic(i, "LLD"); type=NI; break;
6699 case 0x35: set_mnemonic(i, "LDC1"); type=C1LS; break;
6700 case 0x37: set_mnemonic(i, "LD"); type=LOAD; break;
6702 case 0x38: set_mnemonic(i, "SC"); type=NI; break;
6703 case 0x39: set_mnemonic(i, "SWC1"); type=C1LS; break;
6705 case 0x3C: set_mnemonic(i, "SCD"); type=NI; break;
6706 case 0x3D: set_mnemonic(i, "SDC1"); type=C1LS; break;
6707 case 0x3F: set_mnemonic(i, "SD"); type=STORE; break;
6709 case 0x12: set_mnemonic(i, "COP2"); type=NI;
6710 op2=(source[i]>>21)&0x1f;
6712 if (source[i]&0x3f) { // use this hack to support old savestates with patched gte insns
6713 if (gte_handlers[source[i]&0x3f]!=NULL) {
6715 if (gte_regnames[source[i]&0x3f]!=NULL)
6716 strcpy(insn[i],gte_regnames[source[i]&0x3f]);
6718 snprintf(insn[i], sizeof(insn[i]), "COP2 %x", source[i]&0x3f);
6725 case 0x00: set_mnemonic(i, "MFC2"); type=COP2; break;
6726 case 0x02: set_mnemonic(i, "CFC2"); type=COP2; break;
6727 case 0x04: set_mnemonic(i, "MTC2"); type=COP2; break;
6728 case 0x06: set_mnemonic(i, "CTC2"); type=COP2; break;
6731 case 0x32: set_mnemonic(i, "LWC2"); type=C2LS; break;
6732 case 0x3A: set_mnemonic(i, "SWC2"); type=C2LS; break;
6733 case 0x3B: set_mnemonic(i, "HLECALL"); type=HLECALL; break;
6734 default: set_mnemonic(i, "???"); type=NI;
6735 SysPrintf("NI %08x @%08x (%08x)\n", source[i], start + i*4, start);
6739 dops[i].opcode2=op2;
6740 /* Get registers/immediates */
6742 gte_rs[i]=gte_rt[i]=0;
6745 dops[i].rs1=(source[i]>>21)&0x1f;
6747 dops[i].rt1=(source[i]>>16)&0x1f;
6749 imm[i]=(short)source[i];
6753 dops[i].rs1=(source[i]>>21)&0x1f;
6754 dops[i].rs2=(source[i]>>16)&0x1f;
6757 imm[i]=(short)source[i];
6760 // LWL/LWR only load part of the register,
6761 // therefore the target register must be treated as a source too
6762 dops[i].rs1=(source[i]>>21)&0x1f;
6763 dops[i].rs2=(source[i]>>16)&0x1f;
6764 dops[i].rt1=(source[i]>>16)&0x1f;
6766 imm[i]=(short)source[i];
6769 if (op==0x0f) dops[i].rs1=0; // LUI instruction has no source register
6770 else dops[i].rs1=(source[i]>>21)&0x1f;
6772 dops[i].rt1=(source[i]>>16)&0x1f;
6774 if(op>=0x0c&&op<=0x0e) { // ANDI/ORI/XORI
6775 imm[i]=(unsigned short)source[i];
6777 imm[i]=(short)source[i];
6785 // The JAL instruction writes to r31.
6792 dops[i].rs1=(source[i]>>21)&0x1f;
6796 // The JALR instruction writes to rd.
6798 dops[i].rt1=(source[i]>>11)&0x1f;
6803 dops[i].rs1=(source[i]>>21)&0x1f;
6804 dops[i].rs2=(source[i]>>16)&0x1f;
6807 if(op&2) { // BGTZ/BLEZ
6812 dops[i].rs1=(source[i]>>21)&0x1f;
6816 if(op2&0x10) { // BxxAL
6818 // NOTE: If the branch is not taken, r31 is still overwritten
6822 dops[i].rs1=(source[i]>>21)&0x1f; // source
6823 dops[i].rs2=(source[i]>>16)&0x1f; // subtract amount
6824 dops[i].rt1=(source[i]>>11)&0x1f; // destination
6828 dops[i].rs1=(source[i]>>21)&0x1f; // source
6829 dops[i].rs2=(source[i]>>16)&0x1f; // divisor
6838 if(op2==0x10) dops[i].rs1=HIREG; // MFHI
6839 if(op2==0x11) dops[i].rt1=HIREG; // MTHI
6840 if(op2==0x12) dops[i].rs1=LOREG; // MFLO
6841 if(op2==0x13) dops[i].rt1=LOREG; // MTLO
6842 if((op2&0x1d)==0x10) dops[i].rt1=(source[i]>>11)&0x1f; // MFxx
6843 if((op2&0x1d)==0x11) dops[i].rs1=(source[i]>>21)&0x1f; // MTxx
6846 dops[i].rs1=(source[i]>>16)&0x1f; // target of shift
6847 dops[i].rs2=(source[i]>>21)&0x1f; // shift amount
6848 dops[i].rt1=(source[i]>>11)&0x1f; // destination
6852 dops[i].rs1=(source[i]>>16)&0x1f;
6854 dops[i].rt1=(source[i]>>11)&0x1f;
6856 imm[i]=(source[i]>>6)&0x1f;
6857 // DSxx32 instructions
6858 if(op2>=0x3c) imm[i]|=0x20;
6865 if(op2==0||op2==2) dops[i].rt1=(source[i]>>16)&0x1F; // MFC0/CFC0
6866 if(op2==4||op2==6) dops[i].rs1=(source[i]>>16)&0x1F; // MTC0/CTC0
6867 if(op2==4&&((source[i]>>11)&0x1f)==12) dops[i].rt2=CSREG; // Status
6868 if(op2==16) if((source[i]&0x3f)==0x18) dops[i].rs2=CCREG; // ERET
6875 if(op2<3) dops[i].rt1=(source[i]>>16)&0x1F; // MFC1/DMFC1/CFC1
6876 if(op2>3) dops[i].rs1=(source[i]>>16)&0x1F; // MTC1/DMTC1/CTC1
6884 if(op2<3) dops[i].rt1=(source[i]>>16)&0x1F; // MFC2/CFC2
6885 if(op2>3) dops[i].rs1=(source[i]>>16)&0x1F; // MTC2/CTC2
6887 int gr=(source[i]>>11)&0x1F;
6890 case 0x00: gte_rs[i]=1ll<<gr; break; // MFC2
6891 case 0x04: gte_rt[i]=1ll<<gr; break; // MTC2
6892 case 0x02: gte_rs[i]=1ll<<(gr+32); break; // CFC2
6893 case 0x06: gte_rt[i]=1ll<<(gr+32); break; // CTC2
6897 dops[i].rs1=(source[i]>>21)&0x1F;
6901 imm[i]=(short)source[i];
6904 dops[i].rs1=(source[i]>>21)&0x1F;
6908 imm[i]=(short)source[i];
6909 if(op==0x32) gte_rt[i]=1ll<<((source[i]>>16)&0x1F); // LWC2
6910 else gte_rs[i]=1ll<<((source[i]>>16)&0x1F); // SWC2
6917 gte_rs[i]=gte_reg_reads[source[i]&0x3f];
6918 gte_rt[i]=gte_reg_writes[source[i]&0x3f];
6919 gte_rt[i]|=1ll<<63; // every op changes flags
6920 if((source[i]&0x3f)==GTE_MVMVA) {
6921 int v = (source[i] >> 15) & 3;
6922 gte_rs[i]&=~0xe3fll;
6923 if(v==3) gte_rs[i]|=0xe00ll;
6924 else gte_rs[i]|=3ll<<(v*2);
6941 /* Calculate branch target addresses */
6943 ba[i]=((start+i*4+4)&0xF0000000)|(((unsigned int)source[i]<<6)>>4);
6944 else if(type==CJUMP&&dops[i].rs1==dops[i].rs2&&(op&1))
6945 ba[i]=start+i*4+8; // Ignore never taken branch
6946 else if(type==SJUMP&&dops[i].rs1==0&&!(op2&1))
6947 ba[i]=start+i*4+8; // Ignore never taken branch
6948 else if(type==CJUMP||type==SJUMP)
6949 ba[i]=start+i*4+4+((signed int)((unsigned int)source[i]<<16)>>14);
6952 /* simplify always (not)taken branches */
6953 if (type == CJUMP && dops[i].rs1 == dops[i].rs2) {
6954 dops[i].rs1 = dops[i].rs2 = 0;
6956 dops[i].itype = type = UJUMP;
6957 dops[i].rs2 = CCREG;
6960 else if (type == SJUMP && dops[i].rs1 == 0 && (op2 & 1))
6961 dops[i].itype = type = UJUMP;
6963 dops[i].is_jump = (dops[i].itype == RJUMP || dops[i].itype == UJUMP || dops[i].itype == CJUMP || dops[i].itype == SJUMP);
6964 dops[i].is_ujump = (dops[i].itype == RJUMP || dops[i].itype == UJUMP); // || (source[i] >> 16) == 0x1000 // beq r0,r0
6965 dops[i].is_load = (dops[i].itype == LOAD || dops[i].itype == LOADLR || op == 0x32); // LWC2
6966 dops[i].is_store = (dops[i].itype == STORE || dops[i].itype == STORELR || op == 0x3a); // SWC2
6968 /* messy cases to just pass over to the interpreter */
6969 if (i > 0 && dops[i-1].is_jump) {
6971 // branch in delay slot?
6972 if (dops[i].is_jump) {
6973 // don't handle first branch and call interpreter if it's hit
6974 SysPrintf("branch in delay slot @%08x (%08x)\n", start + i*4, start);
6977 // basic load delay detection
6978 else if((type==LOAD||type==LOADLR||type==COP0||type==COP2||type==C2LS)&&dops[i].rt1!=0) {
6979 int t=(ba[i-1]-start)/4;
6980 if(0 <= t && t < i &&(dops[i].rt1==dops[t].rs1||dops[i].rt1==dops[t].rs2)&&dops[t].itype!=CJUMP&&dops[t].itype!=SJUMP) {
6981 // jump target wants DS result - potential load delay effect
6982 SysPrintf("load delay @%08x (%08x)\n", start + i*4, start);
6984 dops[t+1].bt=1; // expected return from interpreter
6986 else if(i>=2&&dops[i-2].rt1==2&&dops[i].rt1==2&&dops[i].rs1!=2&&dops[i].rs2!=2&&dops[i-1].rs1!=2&&dops[i-1].rs2!=2&&
6987 !(i>=3&&dops[i-3].is_jump)) {
6988 // v0 overwrite like this is a sign of trouble, bail out
6989 SysPrintf("v0 overwrite @%08x (%08x)\n", start + i*4, start);
6994 memset(&dops[i-1], 0, sizeof(dops[i-1]));
6995 dops[i-1].itype = INTCALL;
6996 dops[i-1].rs1 = CCREG;
6999 i--; // don't compile the DS
7003 /* Is this the end of the block? */
7004 if (i > 0 && dops[i-1].is_ujump) {
7005 if (dops[i-1].rt1 == 0) { // not jal
7006 int found_bbranch = 0, t = (ba[i-1] - start) / 4;
7007 if ((u_int)(t - i) < 64 && start + (t+64)*4 < pagelimit) {
7008 // scan for a branch back to i+1
7009 for (j = t; j < t + 64; j++) {
7010 int tmpop = source[j] >> 26;
7011 if (tmpop == 1 || ((tmpop & ~3) == 4)) {
7012 int t2 = j + 1 + (int)(signed short)source[j];
7014 //printf("blk expand %08x<-%08x\n", start + (i+1)*4, start + j*4);
7025 if(stop_after_jal) done=1;
7027 if((source[i+1]&0xfc00003f)==0x0d) done=1;
7029 // Don't recompile stuff that's already compiled
7030 if(check_addr(start+i*4+4)) done=1;
7031 // Don't get too close to the limit
7032 if(i>MAXBLOCK/2) done=1;
7034 if (dops[i].itype == SYSCALL || dops[i].itype == HLECALL || dops[i].itype == INTCALL)
7035 done = stop_after_jal ? 1 : 2;
7037 // Does the block continue due to a branch?
7040 if(ba[j]==start+i*4) done=j=0; // Branch into delay slot
7041 if(ba[j]==start+i*4+4) done=j=0;
7042 if(ba[j]==start+i*4+8) done=j=0;
7045 //assert(i<MAXBLOCK-1);
7046 if(start+i*4==pagelimit-4) done=1;
7047 assert(start+i*4<pagelimit);
7048 if (i==MAXBLOCK-1) done=1;
7049 // Stop if we're compiling junk
7050 if(dops[i].itype == NI && (++ni_count > 8 || dops[i].opcode == 0x11)) {
7051 done=stop_after_jal=1;
7052 SysPrintf("Disabled speculative precompilation\n");
7055 while (i > 0 && dops[i-1].is_jump)
7058 assert(!dops[i-1].is_jump);
7062 // Basic liveness analysis for MIPS registers
7063 static noinline void pass2_unneeded_regs(int istart,int iend,int r)
7066 uint64_t u,gte_u,b,gte_b;
7067 uint64_t temp_u,temp_gte_u=0;
7068 uint64_t gte_u_unknown=0;
7069 if (HACK_ENABLED(NDHACK_GTE_UNNEEDED))
7073 gte_u=gte_u_unknown;
7075 //u=unneeded_reg[iend+1];
7077 gte_u=gte_unneeded[iend+1];
7080 for (i=iend;i>=istart;i--)
7082 //printf("unneeded registers i=%d (%d,%d) r=%d\n",i,istart,iend,r);
7085 // If subroutine call, flag return address as a possible branch target
7086 if(dops[i].rt1==31 && i<slen-2) dops[i+2].bt=1;
7088 if(ba[i]<start || ba[i]>=(start+slen*4))
7090 // Branch out of this block, flush all regs
7092 gte_u=gte_u_unknown;
7093 branch_unneeded_reg[i]=u;
7094 // Merge in delay slot
7095 u|=(1LL<<dops[i+1].rt1)|(1LL<<dops[i+1].rt2);
7096 u&=~((1LL<<dops[i+1].rs1)|(1LL<<dops[i+1].rs2));
7099 gte_u&=~gte_rs[i+1];
7103 // Internal branch, flag target
7104 dops[(ba[i]-start)>>2].bt=1;
7105 if(ba[i]<=start+i*4) {
7107 if(dops[i].is_ujump)
7109 // Unconditional branch
7113 // Conditional branch (not taken case)
7114 temp_u=unneeded_reg[i+2];
7115 temp_gte_u&=gte_unneeded[i+2];
7117 // Merge in delay slot
7118 temp_u|=(1LL<<dops[i+1].rt1)|(1LL<<dops[i+1].rt2);
7119 temp_u&=~((1LL<<dops[i+1].rs1)|(1LL<<dops[i+1].rs2));
7121 temp_gte_u|=gte_rt[i+1];
7122 temp_gte_u&=~gte_rs[i+1];
7123 temp_u|=(1LL<<dops[i].rt1)|(1LL<<dops[i].rt2);
7124 temp_u&=~((1LL<<dops[i].rs1)|(1LL<<dops[i].rs2));
7126 temp_gte_u|=gte_rt[i];
7127 temp_gte_u&=~gte_rs[i];
7128 unneeded_reg[i]=temp_u;
7129 gte_unneeded[i]=temp_gte_u;
7130 // Only go three levels deep. This recursion can take an
7131 // excessive amount of time if there are a lot of nested loops.
7133 pass2_unneeded_regs((ba[i]-start)>>2,i-1,r+1);
7135 unneeded_reg[(ba[i]-start)>>2]=1;
7136 gte_unneeded[(ba[i]-start)>>2]=gte_u_unknown;
7139 if (dops[i].is_ujump)
7141 // Unconditional branch
7142 u=unneeded_reg[(ba[i]-start)>>2];
7143 gte_u=gte_unneeded[(ba[i]-start)>>2];
7144 branch_unneeded_reg[i]=u;
7145 // Merge in delay slot
7146 u|=(1LL<<dops[i+1].rt1)|(1LL<<dops[i+1].rt2);
7147 u&=~((1LL<<dops[i+1].rs1)|(1LL<<dops[i+1].rs2));
7150 gte_u&=~gte_rs[i+1];
7152 // Conditional branch
7153 b=unneeded_reg[(ba[i]-start)>>2];
7154 gte_b=gte_unneeded[(ba[i]-start)>>2];
7155 branch_unneeded_reg[i]=b;
7156 // Branch delay slot
7157 b|=(1LL<<dops[i+1].rt1)|(1LL<<dops[i+1].rt2);
7158 b&=~((1LL<<dops[i+1].rs1)|(1LL<<dops[i+1].rs2));
7161 gte_b&=~gte_rs[i+1];
7165 branch_unneeded_reg[i]&=unneeded_reg[i+2];
7167 branch_unneeded_reg[i]=1;
7173 else if(dops[i].itype==SYSCALL||dops[i].itype==HLECALL||dops[i].itype==INTCALL)
7175 // SYSCALL instruction (software interrupt)
7178 else if(dops[i].itype==COP0 && dops[i].opcode2==0x10)
7184 // Written registers are unneeded
7185 u|=1LL<<dops[i].rt1;
7186 u|=1LL<<dops[i].rt2;
7188 // Accessed registers are needed
7189 u&=~(1LL<<dops[i].rs1);
7190 u&=~(1LL<<dops[i].rs2);
7192 if(gte_rs[i]&&dops[i].rt1&&(unneeded_reg[i+1]&(1ll<<dops[i].rt1)))
7193 gte_u|=gte_rs[i]>e_unneeded[i+1]; // MFC2/CFC2 to dead register, unneeded
7194 // Source-target dependencies
7195 // R0 is always unneeded
7199 gte_unneeded[i]=gte_u;
7201 printf("ur (%d,%d) %x: ",istart,iend,start+i*4);
7204 for(r=1;r<=CCREG;r++) {
7205 if((unneeded_reg[i]>>r)&1) {
7206 if(r==HIREG) printf(" HI");
7207 else if(r==LOREG) printf(" LO");
7208 else printf(" r%d",r);
7216 static noinline void pass3_register_alloc(u_int addr)
7218 struct regstat current; // Current register allocations/status
7219 clear_all_regs(current.regmap_entry);
7220 clear_all_regs(current.regmap);
7221 current.wasdirty = current.dirty = 0;
7222 current.u = unneeded_reg[0];
7223 alloc_reg(¤t, 0, CCREG);
7224 dirty_reg(¤t, CCREG);
7225 current.wasconst = 0;
7226 current.isconst = 0;
7227 current.loadedconst = 0;
7228 //current.waswritten = 0;
7235 // First instruction is delay slot
7240 current.regmap[HOST_BTREG]=BTREG;
7247 for(hr=0;hr<HOST_REGS;hr++)
7249 // Is this really necessary?
7250 if(current.regmap[hr]==0) current.regmap[hr]=-1;
7253 //current.waswritten=0;
7256 memcpy(regmap_pre[i],current.regmap,sizeof(current.regmap));
7257 regs[i].wasconst=current.isconst;
7258 regs[i].wasdirty=current.dirty;
7262 regs[i].loadedconst=0;
7263 if (!dops[i].is_jump) {
7265 current.u=unneeded_reg[i+1]&~((1LL<<dops[i].rs1)|(1LL<<dops[i].rs2));
7272 current.u=branch_unneeded_reg[i]&~((1LL<<dops[i+1].rs1)|(1LL<<dops[i+1].rs2));
7273 current.u&=~((1LL<<dops[i].rs1)|(1LL<<dops[i].rs2));
7276 SysPrintf("oops, branch at end of block with no delay slot @%08x\n", start + i*4);
7282 ds=0; // Skip delay slot, already allocated as part of branch
7283 // ...but we need to alloc it in case something jumps here
7285 current.u=branch_unneeded_reg[i-1]&unneeded_reg[i+1];
7287 current.u=branch_unneeded_reg[i-1];
7289 current.u&=~((1LL<<dops[i].rs1)|(1LL<<dops[i].rs2));
7291 struct regstat temp;
7292 memcpy(&temp,¤t,sizeof(current));
7293 temp.wasdirty=temp.dirty;
7294 // TODO: Take into account unconditional branches, as below
7295 delayslot_alloc(&temp,i);
7296 memcpy(regs[i].regmap,temp.regmap,sizeof(temp.regmap));
7297 regs[i].wasdirty=temp.wasdirty;
7298 regs[i].dirty=temp.dirty;
7302 // Create entry (branch target) regmap
7303 for(hr=0;hr<HOST_REGS;hr++)
7305 int r=temp.regmap[hr];
7307 if(r!=regmap_pre[i][hr]) {
7308 regs[i].regmap_entry[hr]=-1;
7313 if((current.u>>r)&1) {
7314 regs[i].regmap_entry[hr]=-1;
7315 regs[i].regmap[hr]=-1;
7316 //Don't clear regs in the delay slot as the branch might need them
7317 //current.regmap[hr]=-1;
7319 regs[i].regmap_entry[hr]=r;
7322 // First instruction expects CCREG to be allocated
7323 if(i==0&&hr==HOST_CCREG)
7324 regs[i].regmap_entry[hr]=CCREG;
7326 regs[i].regmap_entry[hr]=-1;
7330 else { // Not delay slot
7331 switch(dops[i].itype) {
7333 //current.isconst=0; // DEBUG
7334 //current.wasconst=0; // DEBUG
7335 //regs[i].wasconst=0; // DEBUG
7336 clear_const(¤t,dops[i].rt1);
7337 alloc_cc(¤t,i);
7338 dirty_reg(¤t,CCREG);
7339 if (dops[i].rt1==31) {
7340 alloc_reg(¤t,i,31);
7341 dirty_reg(¤t,31);
7342 //assert(dops[i+1].rs1!=31&&dops[i+1].rs2!=31);
7343 //assert(dops[i+1].rt1!=dops[i].rt1);
7345 alloc_reg(¤t,i,PTEMP);
7349 delayslot_alloc(¤t,i+1);
7350 //current.isconst=0; // DEBUG
7352 //printf("i=%d, isconst=%x\n",i,current.isconst);
7355 //current.isconst=0;
7356 //current.wasconst=0;
7357 //regs[i].wasconst=0;
7358 clear_const(¤t,dops[i].rs1);
7359 clear_const(¤t,dops[i].rt1);
7360 alloc_cc(¤t,i);
7361 dirty_reg(¤t,CCREG);
7362 if (!ds_writes_rjump_rs(i)) {
7363 alloc_reg(¤t,i,dops[i].rs1);
7364 if (dops[i].rt1!=0) {
7365 alloc_reg(¤t,i,dops[i].rt1);
7366 dirty_reg(¤t,dops[i].rt1);
7367 assert(dops[i+1].rs1!=dops[i].rt1&&dops[i+1].rs2!=dops[i].rt1);
7368 assert(dops[i+1].rt1!=dops[i].rt1);
7370 alloc_reg(¤t,i,PTEMP);
7374 if(dops[i].rs1==31) { // JALR
7375 alloc_reg(¤t,i,RHASH);
7376 alloc_reg(¤t,i,RHTBL);
7379 delayslot_alloc(¤t,i+1);
7381 // The delay slot overwrites our source register,
7382 // allocate a temporary register to hold the old value.
7386 delayslot_alloc(¤t,i+1);
7388 alloc_reg(¤t,i,RTEMP);
7390 //current.isconst=0; // DEBUG
7395 //current.isconst=0;
7396 //current.wasconst=0;
7397 //regs[i].wasconst=0;
7398 clear_const(¤t,dops[i].rs1);
7399 clear_const(¤t,dops[i].rs2);
7400 if((dops[i].opcode&0x3E)==4) // BEQ/BNE
7402 alloc_cc(¤t,i);
7403 dirty_reg(¤t,CCREG);
7404 if(dops[i].rs1) alloc_reg(¤t,i,dops[i].rs1);
7405 if(dops[i].rs2) alloc_reg(¤t,i,dops[i].rs2);
7406 if((dops[i].rs1&&(dops[i].rs1==dops[i+1].rt1||dops[i].rs1==dops[i+1].rt2))||
7407 (dops[i].rs2&&(dops[i].rs2==dops[i+1].rt1||dops[i].rs2==dops[i+1].rt2))) {
7408 // The delay slot overwrites one of our conditions.
7409 // Allocate the branch condition registers instead.
7413 if(dops[i].rs1) alloc_reg(¤t,i,dops[i].rs1);
7414 if(dops[i].rs2) alloc_reg(¤t,i,dops[i].rs2);
7419 delayslot_alloc(¤t,i+1);
7423 if((dops[i].opcode&0x3E)==6) // BLEZ/BGTZ
7425 alloc_cc(¤t,i);
7426 dirty_reg(¤t,CCREG);
7427 alloc_reg(¤t,i,dops[i].rs1);
7428 if(dops[i].rs1&&(dops[i].rs1==dops[i+1].rt1||dops[i].rs1==dops[i+1].rt2)) {
7429 // The delay slot overwrites one of our conditions.
7430 // Allocate the branch condition registers instead.
7434 if(dops[i].rs1) alloc_reg(¤t,i,dops[i].rs1);
7439 delayslot_alloc(¤t,i+1);
7443 // Don't alloc the delay slot yet because we might not execute it
7444 if((dops[i].opcode&0x3E)==0x14) // BEQL/BNEL
7449 alloc_cc(¤t,i);
7450 dirty_reg(¤t,CCREG);
7451 alloc_reg(¤t,i,dops[i].rs1);
7452 alloc_reg(¤t,i,dops[i].rs2);
7455 if((dops[i].opcode&0x3E)==0x16) // BLEZL/BGTZL
7460 alloc_cc(¤t,i);
7461 dirty_reg(¤t,CCREG);
7462 alloc_reg(¤t,i,dops[i].rs1);
7465 //current.isconst=0;
7468 //current.isconst=0;
7469 //current.wasconst=0;
7470 //regs[i].wasconst=0;
7471 clear_const(¤t,dops[i].rs1);
7472 clear_const(¤t,dops[i].rt1);
7473 //if((dops[i].opcode2&0x1E)==0x0) // BLTZ/BGEZ
7474 if((dops[i].opcode2&0x0E)==0x0) // BLTZ/BGEZ
7476 alloc_cc(¤t,i);
7477 dirty_reg(¤t,CCREG);
7478 alloc_reg(¤t,i,dops[i].rs1);
7479 if (dops[i].rt1==31) { // BLTZAL/BGEZAL
7480 alloc_reg(¤t,i,31);
7481 dirty_reg(¤t,31);
7482 //#ifdef REG_PREFETCH
7483 //alloc_reg(¤t,i,PTEMP);
7486 if((dops[i].rs1&&(dops[i].rs1==dops[i+1].rt1||dops[i].rs1==dops[i+1].rt2)) // The delay slot overwrites the branch condition.
7487 ||(dops[i].rt1==31&&(dops[i+1].rs1==31||dops[i+1].rs2==31||dops[i+1].rt1==31||dops[i+1].rt2==31))) { // DS touches $ra
7488 // Allocate the branch condition registers instead.
7492 if(dops[i].rs1) alloc_reg(¤t,i,dops[i].rs1);
7497 delayslot_alloc(¤t,i+1);
7501 // Don't alloc the delay slot yet because we might not execute it
7502 if((dops[i].opcode2&0x1E)==0x2) // BLTZL/BGEZL
7507 alloc_cc(¤t,i);
7508 dirty_reg(¤t,CCREG);
7509 alloc_reg(¤t,i,dops[i].rs1);
7512 //current.isconst=0;
7515 imm16_alloc(¤t,i);
7519 load_alloc(¤t,i);
7523 store_alloc(¤t,i);
7526 alu_alloc(¤t,i);
7529 shift_alloc(¤t,i);
7532 multdiv_alloc(¤t,i);
7535 shiftimm_alloc(¤t,i);
7538 mov_alloc(¤t,i);
7541 cop0_alloc(¤t,i);
7546 cop2_alloc(¤t,i);
7549 c1ls_alloc(¤t,i);
7552 c2ls_alloc(¤t,i);
7555 c2op_alloc(¤t,i);
7560 syscall_alloc(¤t,i);
7564 // Create entry (branch target) regmap
7565 for(hr=0;hr<HOST_REGS;hr++)
7568 r=current.regmap[hr];
7570 if(r!=regmap_pre[i][hr]) {
7571 // TODO: delay slot (?)
7572 or=get_reg(regmap_pre[i],r); // Get old mapping for this register
7573 if(or<0||r>=TEMPREG){
7574 regs[i].regmap_entry[hr]=-1;
7578 // Just move it to a different register
7579 regs[i].regmap_entry[hr]=r;
7580 // If it was dirty before, it's still dirty
7581 if((regs[i].wasdirty>>or)&1) dirty_reg(¤t,r);
7588 regs[i].regmap_entry[hr]=0;
7593 if((current.u>>r)&1) {
7594 regs[i].regmap_entry[hr]=-1;
7595 //regs[i].regmap[hr]=-1;
7596 current.regmap[hr]=-1;
7598 regs[i].regmap_entry[hr]=r;
7602 // Branches expect CCREG to be allocated at the target
7603 if(regmap_pre[i][hr]==CCREG)
7604 regs[i].regmap_entry[hr]=CCREG;
7606 regs[i].regmap_entry[hr]=-1;
7609 memcpy(regs[i].regmap,current.regmap,sizeof(current.regmap));
7612 #if 0 // see do_store_smc_check()
7613 if(i>0&&(dops[i-1].itype==STORE||dops[i-1].itype==STORELR||(dops[i-1].itype==C2LS&&dops[i-1].opcode==0x3a))&&(u_int)imm[i-1]<0x800)
7614 current.waswritten|=1<<dops[i-1].rs1;
7615 current.waswritten&=~(1<<dops[i].rt1);
7616 current.waswritten&=~(1<<dops[i].rt2);
7617 if((dops[i].itype==STORE||dops[i].itype==STORELR||(dops[i].itype==C2LS&&dops[i].opcode==0x3a))&&(u_int)imm[i]>=0x800)
7618 current.waswritten&=~(1<<dops[i].rs1);
7621 /* Branch post-alloc */
7624 current.wasdirty=current.dirty;
7625 switch(dops[i-1].itype) {
7627 memcpy(&branch_regs[i-1],¤t,sizeof(current));
7628 branch_regs[i-1].isconst=0;
7629 branch_regs[i-1].wasconst=0;
7630 branch_regs[i-1].u=branch_unneeded_reg[i-1]&~((1LL<<dops[i-1].rs1)|(1LL<<dops[i-1].rs2));
7631 alloc_cc(&branch_regs[i-1],i-1);
7632 dirty_reg(&branch_regs[i-1],CCREG);
7633 if(dops[i-1].rt1==31) { // JAL
7634 alloc_reg(&branch_regs[i-1],i-1,31);
7635 dirty_reg(&branch_regs[i-1],31);
7637 memcpy(&branch_regs[i-1].regmap_entry,&branch_regs[i-1].regmap,sizeof(current.regmap));
7638 memcpy(constmap[i],constmap[i-1],sizeof(constmap[i]));
7641 memcpy(&branch_regs[i-1],¤t,sizeof(current));
7642 branch_regs[i-1].isconst=0;
7643 branch_regs[i-1].wasconst=0;
7644 branch_regs[i-1].u=branch_unneeded_reg[i-1]&~((1LL<<dops[i-1].rs1)|(1LL<<dops[i-1].rs2));
7645 alloc_cc(&branch_regs[i-1],i-1);
7646 dirty_reg(&branch_regs[i-1],CCREG);
7647 alloc_reg(&branch_regs[i-1],i-1,dops[i-1].rs1);
7648 if(dops[i-1].rt1!=0) { // JALR
7649 alloc_reg(&branch_regs[i-1],i-1,dops[i-1].rt1);
7650 dirty_reg(&branch_regs[i-1],dops[i-1].rt1);
7653 if(dops[i-1].rs1==31) { // JALR
7654 alloc_reg(&branch_regs[i-1],i-1,RHASH);
7655 alloc_reg(&branch_regs[i-1],i-1,RHTBL);
7658 memcpy(&branch_regs[i-1].regmap_entry,&branch_regs[i-1].regmap,sizeof(current.regmap));
7659 memcpy(constmap[i],constmap[i-1],sizeof(constmap[i]));
7662 if((dops[i-1].opcode&0x3E)==4) // BEQ/BNE
7664 alloc_cc(¤t,i-1);
7665 dirty_reg(¤t,CCREG);
7666 if((dops[i-1].rs1&&(dops[i-1].rs1==dops[i].rt1||dops[i-1].rs1==dops[i].rt2))||
7667 (dops[i-1].rs2&&(dops[i-1].rs2==dops[i].rt1||dops[i-1].rs2==dops[i].rt2))) {
7668 // The delay slot overwrote one of our conditions
7669 // Delay slot goes after the test (in order)
7670 current.u=branch_unneeded_reg[i-1]&~((1LL<<dops[i].rs1)|(1LL<<dops[i].rs2));
7672 delayslot_alloc(¤t,i);
7677 current.u=branch_unneeded_reg[i-1]&~((1LL<<dops[i-1].rs1)|(1LL<<dops[i-1].rs2));
7678 // Alloc the branch condition registers
7679 if(dops[i-1].rs1) alloc_reg(¤t,i-1,dops[i-1].rs1);
7680 if(dops[i-1].rs2) alloc_reg(¤t,i-1,dops[i-1].rs2);
7682 memcpy(&branch_regs[i-1],¤t,sizeof(current));
7683 branch_regs[i-1].isconst=0;
7684 branch_regs[i-1].wasconst=0;
7685 memcpy(&branch_regs[i-1].regmap_entry,¤t.regmap,sizeof(current.regmap));
7686 memcpy(constmap[i],constmap[i-1],sizeof(constmap[i]));
7689 if((dops[i-1].opcode&0x3E)==6) // BLEZ/BGTZ
7691 alloc_cc(¤t,i-1);
7692 dirty_reg(¤t,CCREG);
7693 if(dops[i-1].rs1==dops[i].rt1||dops[i-1].rs1==dops[i].rt2) {
7694 // The delay slot overwrote the branch condition
7695 // Delay slot goes after the test (in order)
7696 current.u=branch_unneeded_reg[i-1]&~((1LL<<dops[i].rs1)|(1LL<<dops[i].rs2));
7698 delayslot_alloc(¤t,i);
7703 current.u=branch_unneeded_reg[i-1]&~(1LL<<dops[i-1].rs1);
7704 // Alloc the branch condition register
7705 alloc_reg(¤t,i-1,dops[i-1].rs1);
7707 memcpy(&branch_regs[i-1],¤t,sizeof(current));
7708 branch_regs[i-1].isconst=0;
7709 branch_regs[i-1].wasconst=0;
7710 memcpy(&branch_regs[i-1].regmap_entry,¤t.regmap,sizeof(current.regmap));
7711 memcpy(constmap[i],constmap[i-1],sizeof(constmap[i]));
7714 // Alloc the delay slot in case the branch is taken
7715 if((dops[i-1].opcode&0x3E)==0x14) // BEQL/BNEL
7717 memcpy(&branch_regs[i-1],¤t,sizeof(current));
7718 branch_regs[i-1].u=(branch_unneeded_reg[i-1]&~((1LL<<dops[i].rs1)|(1LL<<dops[i].rs2)|(1LL<<dops[i].rt1)|(1LL<<dops[i].rt2)))|1;
7719 alloc_cc(&branch_regs[i-1],i);
7720 dirty_reg(&branch_regs[i-1],CCREG);
7721 delayslot_alloc(&branch_regs[i-1],i);
7722 branch_regs[i-1].isconst=0;
7723 alloc_reg(¤t,i,CCREG); // Not taken path
7724 dirty_reg(¤t,CCREG);
7725 memcpy(&branch_regs[i-1].regmap_entry,&branch_regs[i-1].regmap,sizeof(current.regmap));
7728 if((dops[i-1].opcode&0x3E)==0x16) // BLEZL/BGTZL
7730 memcpy(&branch_regs[i-1],¤t,sizeof(current));
7731 branch_regs[i-1].u=(branch_unneeded_reg[i-1]&~((1LL<<dops[i].rs1)|(1LL<<dops[i].rs2)|(1LL<<dops[i].rt1)|(1LL<<dops[i].rt2)))|1;
7732 alloc_cc(&branch_regs[i-1],i);
7733 dirty_reg(&branch_regs[i-1],CCREG);
7734 delayslot_alloc(&branch_regs[i-1],i);
7735 branch_regs[i-1].isconst=0;
7736 alloc_reg(¤t,i,CCREG); // Not taken path
7737 dirty_reg(¤t,CCREG);
7738 memcpy(&branch_regs[i-1].regmap_entry,&branch_regs[i-1].regmap,sizeof(current.regmap));
7742 //if((dops[i-1].opcode2&0x1E)==0) // BLTZ/BGEZ
7743 if((dops[i-1].opcode2&0x0E)==0) // BLTZ/BGEZ
7745 alloc_cc(¤t,i-1);
7746 dirty_reg(¤t,CCREG);
7747 if(dops[i-1].rs1==dops[i].rt1||dops[i-1].rs1==dops[i].rt2) {
7748 // The delay slot overwrote the branch condition
7749 // Delay slot goes after the test (in order)
7750 current.u=branch_unneeded_reg[i-1]&~((1LL<<dops[i].rs1)|(1LL<<dops[i].rs2));
7752 delayslot_alloc(¤t,i);
7757 current.u=branch_unneeded_reg[i-1]&~(1LL<<dops[i-1].rs1);
7758 // Alloc the branch condition register
7759 alloc_reg(¤t,i-1,dops[i-1].rs1);
7761 memcpy(&branch_regs[i-1],¤t,sizeof(current));
7762 branch_regs[i-1].isconst=0;
7763 branch_regs[i-1].wasconst=0;
7764 memcpy(&branch_regs[i-1].regmap_entry,¤t.regmap,sizeof(current.regmap));
7765 memcpy(constmap[i],constmap[i-1],sizeof(constmap[i]));
7768 // Alloc the delay slot in case the branch is taken
7769 if((dops[i-1].opcode2&0x1E)==2) // BLTZL/BGEZL
7771 memcpy(&branch_regs[i-1],¤t,sizeof(current));
7772 branch_regs[i-1].u=(branch_unneeded_reg[i-1]&~((1LL<<dops[i].rs1)|(1LL<<dops[i].rs2)|(1LL<<dops[i].rt1)|(1LL<<dops[i].rt2)))|1;
7773 alloc_cc(&branch_regs[i-1],i);
7774 dirty_reg(&branch_regs[i-1],CCREG);
7775 delayslot_alloc(&branch_regs[i-1],i);
7776 branch_regs[i-1].isconst=0;
7777 alloc_reg(¤t,i,CCREG); // Not taken path
7778 dirty_reg(¤t,CCREG);
7779 memcpy(&branch_regs[i-1].regmap_entry,&branch_regs[i-1].regmap,sizeof(current.regmap));
7781 // FIXME: BLTZAL/BGEZAL
7782 if(dops[i-1].opcode2&0x10) { // BxxZAL
7783 alloc_reg(&branch_regs[i-1],i-1,31);
7784 dirty_reg(&branch_regs[i-1],31);
7789 if (dops[i-1].is_ujump)
7791 if(dops[i-1].rt1==31) // JAL/JALR
7793 // Subroutine call will return here, don't alloc any registers
7795 clear_all_regs(current.regmap);
7796 alloc_reg(¤t,i,CCREG);
7797 dirty_reg(¤t,CCREG);
7801 // Internal branch will jump here, match registers to caller
7803 clear_all_regs(current.regmap);
7804 alloc_reg(¤t,i,CCREG);
7805 dirty_reg(¤t,CCREG);
7808 if(ba[j]==start+i*4+4) {
7809 memcpy(current.regmap,branch_regs[j].regmap,sizeof(current.regmap));
7810 current.dirty=branch_regs[j].dirty;
7815 if(ba[j]==start+i*4+4) {
7816 for(hr=0;hr<HOST_REGS;hr++) {
7817 if(current.regmap[hr]!=branch_regs[j].regmap[hr]) {
7818 current.regmap[hr]=-1;
7820 current.dirty&=branch_regs[j].dirty;
7829 // Count cycles in between branches
7830 ccadj[i] = CLOCK_ADJUST(cc);
7831 if (i > 0 && (dops[i-1].is_jump || dops[i].itype == SYSCALL || dops[i].itype == HLECALL))
7835 #if !defined(DRC_DBG)
7836 else if(dops[i].itype==C2OP&>e_cycletab[source[i]&0x3f]>2)
7838 // this should really be removed since the real stalls have been implemented,
7839 // but doing so causes sizeable perf regression against the older version
7840 u_int gtec = gte_cycletab[source[i] & 0x3f];
7841 cc += HACK_ENABLED(NDHACK_NO_STALLS) ? gtec/2 : 2;
7843 else if(i>1&&dops[i].itype==STORE&&dops[i-1].itype==STORE&&dops[i-2].itype==STORE&&!dops[i].bt)
7847 else if(dops[i].itype==C2LS)
7849 // same as with C2OP
7850 cc += HACK_ENABLED(NDHACK_NO_STALLS) ? 4 : 2;
7858 if(!dops[i].is_ds) {
7859 regs[i].dirty=current.dirty;
7860 regs[i].isconst=current.isconst;
7861 memcpy(constmap[i],current_constmap,sizeof(constmap[i]));
7863 for(hr=0;hr<HOST_REGS;hr++) {
7864 if(hr!=EXCLUDE_REG&®s[i].regmap[hr]>=0) {
7865 if(regmap_pre[i][hr]!=regs[i].regmap[hr]) {
7866 regs[i].wasconst&=~(1<<hr);
7870 if(current.regmap[HOST_BTREG]==BTREG) current.regmap[HOST_BTREG]=-1;
7871 //regs[i].waswritten=current.waswritten;
7875 static noinline void pass4_cull_unused_regs(void)
7877 u_int last_needed_regs[4] = {0,0,0,0};
7881 for (i=slen-1;i>=0;i--)
7884 __builtin_prefetch(regs[i-2].regmap);
7887 if(ba[i]<start || ba[i]>=(start+slen*4))
7889 // Branch out of this block, don't need anything
7895 // Need whatever matches the target
7897 int t=(ba[i]-start)>>2;
7898 for(hr=0;hr<HOST_REGS;hr++)
7900 if(regs[i].regmap_entry[hr]>=0) {
7901 if(regs[i].regmap_entry[hr]==regs[t].regmap_entry[hr]) nr|=1<<hr;
7905 // Conditional branch may need registers for following instructions
7906 if (!dops[i].is_ujump)
7909 nr |= last_needed_regs[(i+2) & 3];
7910 for(hr=0;hr<HOST_REGS;hr++)
7912 if(regmap_pre[i+2][hr]>=0&&get_reg(regs[i+2].regmap_entry,regmap_pre[i+2][hr])<0) nr&=~(1<<hr);
7913 //if((regmap_entry[i+2][hr])>=0) if(!((nr>>hr)&1)) printf("%x-bogus(%d=%d)\n",start+i*4,hr,regmap_entry[i+2][hr]);
7917 // Don't need stuff which is overwritten
7918 //if(regs[i].regmap[hr]!=regmap_pre[i][hr]) nr&=~(1<<hr);
7919 //if(regs[i].regmap[hr]<0) nr&=~(1<<hr);
7920 // Merge in delay slot
7921 if (dops[i+1].rt1) nr &= ~get_regm(regs[i].regmap, dops[i+1].rt1);
7922 if (dops[i+1].rt2) nr &= ~get_regm(regs[i].regmap, dops[i+1].rt2);
7923 nr |= get_regm(regmap_pre[i], dops[i+1].rs1);
7924 nr |= get_regm(regmap_pre[i], dops[i+1].rs2);
7925 nr |= get_regm(regs[i].regmap_entry, dops[i+1].rs1);
7926 nr |= get_regm(regs[i].regmap_entry, dops[i+1].rs2);
7927 if (ram_offset && (dops[i+1].is_load || dops[i+1].is_store)) {
7928 nr |= get_regm(regmap_pre[i], ROREG);
7929 nr |= get_regm(regs[i].regmap_entry, ROREG);
7931 if (dops[i+1].is_store) {
7932 nr |= get_regm(regmap_pre[i], INVCP);
7933 nr |= get_regm(regs[i].regmap_entry, INVCP);
7936 else if(dops[i].itype==SYSCALL||dops[i].itype==HLECALL||dops[i].itype==INTCALL)
7938 // SYSCALL instruction (software interrupt)
7941 else if(dops[i].itype==COP0 && (source[i]&0x3f)==0x18)
7943 // ERET instruction (return from interrupt)
7949 for(hr=0;hr<HOST_REGS;hr++) {
7950 if(regmap_pre[i+1][hr]>=0&&get_reg(regs[i+1].regmap_entry,regmap_pre[i+1][hr])<0) nr&=~(1<<hr);
7951 if(regs[i].regmap[hr]!=regmap_pre[i+1][hr]) nr&=~(1<<hr);
7952 if(regs[i].regmap[hr]!=regmap_pre[i][hr]) nr&=~(1<<hr);
7953 if(regs[i].regmap[hr]<0) nr&=~(1<<hr);
7957 // Overwritten registers are not needed
7958 if (dops[i].rt1) nr &= ~get_regm(regs[i].regmap, dops[i].rt1);
7959 if (dops[i].rt2) nr &= ~get_regm(regs[i].regmap, dops[i].rt2);
7960 nr &= ~get_regm(regs[i].regmap, FTEMP);
7961 // Source registers are needed
7962 nr |= get_regm(regmap_pre[i], dops[i].rs1);
7963 nr |= get_regm(regmap_pre[i], dops[i].rs2);
7964 nr |= get_regm(regs[i].regmap_entry, dops[i].rs1);
7965 nr |= get_regm(regs[i].regmap_entry, dops[i].rs2);
7966 if (ram_offset && (dops[i].is_load || dops[i].is_store)) {
7967 nr |= get_regm(regmap_pre[i], ROREG);
7968 nr |= get_regm(regs[i].regmap_entry, ROREG);
7970 if (dops[i].is_store) {
7971 nr |= get_regm(regmap_pre[i], INVCP);
7972 nr |= get_regm(regs[i].regmap_entry, INVCP);
7975 if (i > 0 && !dops[i].bt && regs[i].wasdirty)
7976 for(hr=0;hr<HOST_REGS;hr++)
7978 // Don't store a register immediately after writing it,
7979 // may prevent dual-issue.
7980 // But do so if this is a branch target, otherwise we
7981 // might have to load the register before the branch.
7982 if((regs[i].wasdirty>>hr)&1) {
7983 if((regmap_pre[i][hr]>0&&!((unneeded_reg[i]>>regmap_pre[i][hr])&1))) {
7984 if(dops[i-1].rt1==regmap_pre[i][hr]) nr|=1<<hr;
7985 if(dops[i-1].rt2==regmap_pre[i][hr]) nr|=1<<hr;
7987 if((regs[i].regmap_entry[hr]>0&&!((unneeded_reg[i]>>regs[i].regmap_entry[hr])&1))) {
7988 if(dops[i-1].rt1==regs[i].regmap_entry[hr]) nr|=1<<hr;
7989 if(dops[i-1].rt2==regs[i].regmap_entry[hr]) nr|=1<<hr;
7993 // Cycle count is needed at branches. Assume it is needed at the target too.
7994 if(i==0||dops[i].bt||dops[i].itype==CJUMP) {
7995 if(regmap_pre[i][HOST_CCREG]==CCREG) nr|=1<<HOST_CCREG;
7996 if(regs[i].regmap_entry[HOST_CCREG]==CCREG) nr|=1<<HOST_CCREG;
7999 last_needed_regs[i & 3] = nr;
8001 // Deallocate unneeded registers
8002 for(hr=0;hr<HOST_REGS;hr++)
8005 if(regs[i].regmap_entry[hr]!=CCREG) regs[i].regmap_entry[hr]=-1;
8008 int map1 = 0, map2 = 0, temp = 0; // or -1 ??
8009 if (dops[i+1].is_load || dops[i+1].is_store)
8011 if (dops[i+1].is_store)
8013 if(dops[i+1].itype==LOADLR || dops[i+1].itype==STORELR || dops[i+1].itype==C2LS)
8015 if(regs[i].regmap[hr]!=dops[i].rs1 && regs[i].regmap[hr]!=dops[i].rs2 &&
8016 regs[i].regmap[hr]!=dops[i].rt1 && regs[i].regmap[hr]!=dops[i].rt2 &&
8017 regs[i].regmap[hr]!=dops[i+1].rt1 && regs[i].regmap[hr]!=dops[i+1].rt2 &&
8018 regs[i].regmap[hr]!=dops[i+1].rs1 && regs[i].regmap[hr]!=dops[i+1].rs2 &&
8019 regs[i].regmap[hr]!=temp && regs[i].regmap[hr]!=PTEMP &&
8020 regs[i].regmap[hr]!=RHASH && regs[i].regmap[hr]!=RHTBL &&
8021 regs[i].regmap[hr]!=RTEMP && regs[i].regmap[hr]!=CCREG &&
8022 regs[i].regmap[hr]!=map1 && regs[i].regmap[hr]!=map2)
8024 regs[i].regmap[hr]=-1;
8025 regs[i].isconst&=~(1<<hr);
8026 regs[i].dirty&=~(1<<hr);
8027 regs[i+1].wasdirty&=~(1<<hr);
8028 if(branch_regs[i].regmap[hr]!=dops[i].rs1 && branch_regs[i].regmap[hr]!=dops[i].rs2 &&
8029 branch_regs[i].regmap[hr]!=dops[i].rt1 && branch_regs[i].regmap[hr]!=dops[i].rt2 &&
8030 branch_regs[i].regmap[hr]!=dops[i+1].rt1 && branch_regs[i].regmap[hr]!=dops[i+1].rt2 &&
8031 branch_regs[i].regmap[hr]!=dops[i+1].rs1 && branch_regs[i].regmap[hr]!=dops[i+1].rs2 &&
8032 branch_regs[i].regmap[hr]!=temp && branch_regs[i].regmap[hr]!=PTEMP &&
8033 branch_regs[i].regmap[hr]!=RHASH && branch_regs[i].regmap[hr]!=RHTBL &&
8034 branch_regs[i].regmap[hr]!=RTEMP && branch_regs[i].regmap[hr]!=CCREG &&
8035 branch_regs[i].regmap[hr]!=map1 && branch_regs[i].regmap[hr]!=map2)
8037 branch_regs[i].regmap[hr]=-1;
8038 branch_regs[i].regmap_entry[hr]=-1;
8039 if (!dops[i].is_ujump)
8042 regmap_pre[i+2][hr]=-1;
8043 regs[i+2].wasconst&=~(1<<hr);
8054 int map1 = -1, map2 = -1, temp=-1;
8055 if (dops[i].is_load || dops[i].is_store)
8057 if (dops[i].is_store)
8059 if (dops[i].itype==LOADLR || dops[i].itype==STORELR || dops[i].itype==C2LS)
8061 if(regs[i].regmap[hr]!=dops[i].rt1 && regs[i].regmap[hr]!=dops[i].rt2 &&
8062 regs[i].regmap[hr]!=dops[i].rs1 && regs[i].regmap[hr]!=dops[i].rs2 &&
8063 regs[i].regmap[hr]!=temp && regs[i].regmap[hr]!=map1 && regs[i].regmap[hr]!=map2 &&
8064 //(dops[i].itype!=SPAN||regs[i].regmap[hr]!=CCREG)
8065 regs[i].regmap[hr] != CCREG)
8067 if(i<slen-1&&!dops[i].is_ds) {
8068 assert(regs[i].regmap[hr]<64);
8069 if(regmap_pre[i+1][hr]!=-1 || regs[i].regmap[hr]>0)
8070 if(regmap_pre[i+1][hr]!=regs[i].regmap[hr])
8072 SysPrintf("fail: %x (%d %d!=%d)\n",start+i*4,hr,regmap_pre[i+1][hr],regs[i].regmap[hr]);
8073 assert(regmap_pre[i+1][hr]==regs[i].regmap[hr]);
8075 regmap_pre[i+1][hr]=-1;
8076 if(regs[i+1].regmap_entry[hr]==CCREG) regs[i+1].regmap_entry[hr]=-1;
8077 regs[i+1].wasconst&=~(1<<hr);
8079 regs[i].regmap[hr]=-1;
8080 regs[i].isconst&=~(1<<hr);
8081 regs[i].dirty&=~(1<<hr);
8082 regs[i+1].wasdirty&=~(1<<hr);
8091 // If a register is allocated during a loop, try to allocate it for the
8092 // entire loop, if possible. This avoids loading/storing registers
8093 // inside of the loop.
8094 static noinline void pass5a_preallocate1(void)
8097 signed char f_regmap[HOST_REGS];
8098 clear_all_regs(f_regmap);
8099 for(i=0;i<slen-1;i++)
8101 if(dops[i].itype==UJUMP||dops[i].itype==CJUMP||dops[i].itype==SJUMP)
8103 if(ba[i]>=start && ba[i]<(start+i*4))
8104 if(dops[i+1].itype==NOP||dops[i+1].itype==MOV||dops[i+1].itype==ALU
8105 ||dops[i+1].itype==SHIFTIMM||dops[i+1].itype==IMM16||dops[i+1].itype==LOAD
8106 ||dops[i+1].itype==STORE||dops[i+1].itype==STORELR||dops[i+1].itype==C1LS
8107 ||dops[i+1].itype==SHIFT||dops[i+1].itype==COP1
8108 ||dops[i+1].itype==COP2||dops[i+1].itype==C2LS||dops[i+1].itype==C2OP)
8110 int t=(ba[i]-start)>>2;
8111 if(t > 0 && !dops[t-1].is_jump) // loop_preload can't handle jumps into delay slots
8112 if(t<2||(dops[t-2].itype!=UJUMP&&dops[t-2].itype!=RJUMP)||dops[t-2].rt1!=31) // call/ret assumes no registers allocated
8113 for(hr=0;hr<HOST_REGS;hr++)
8115 if(regs[i].regmap[hr]>=0) {
8116 if(f_regmap[hr]!=regs[i].regmap[hr]) {
8117 // dealloc old register
8119 for(n=0;n<HOST_REGS;n++)
8121 if(f_regmap[n]==regs[i].regmap[hr]) {f_regmap[n]=-1;}
8123 // and alloc new one
8124 f_regmap[hr]=regs[i].regmap[hr];
8127 if(branch_regs[i].regmap[hr]>=0) {
8128 if(f_regmap[hr]!=branch_regs[i].regmap[hr]) {
8129 // dealloc old register
8131 for(n=0;n<HOST_REGS;n++)
8133 if(f_regmap[n]==branch_regs[i].regmap[hr]) {f_regmap[n]=-1;}
8135 // and alloc new one
8136 f_regmap[hr]=branch_regs[i].regmap[hr];
8140 if(count_free_regs(regs[i].regmap)<=minimum_free_regs[i+1])
8141 f_regmap[hr]=branch_regs[i].regmap[hr];
8143 if(count_free_regs(branch_regs[i].regmap)<=minimum_free_regs[i+1])
8144 f_regmap[hr]=branch_regs[i].regmap[hr];
8146 // Avoid dirty->clean transition
8147 #ifdef DESTRUCTIVE_WRITEBACK
8148 if(t>0) if(get_reg(regmap_pre[t],f_regmap[hr])>=0) if((regs[t].wasdirty>>get_reg(regmap_pre[t],f_regmap[hr]))&1) f_regmap[hr]=-1;
8150 // This check is only strictly required in the DESTRUCTIVE_WRITEBACK
8151 // case above, however it's always a good idea. We can't hoist the
8152 // load if the register was already allocated, so there's no point
8153 // wasting time analyzing most of these cases. It only "succeeds"
8154 // when the mapping was different and the load can be replaced with
8155 // a mov, which is of negligible benefit. So such cases are
8157 if(f_regmap[hr]>0) {
8158 if(regs[t].regmap[hr]==f_regmap[hr]||(regs[t].regmap_entry[hr]<0&&get_reg(regmap_pre[t],f_regmap[hr])<0)) {
8162 //printf("Test %x -> %x, %x %d/%d\n",start+i*4,ba[i],start+j*4,hr,r);
8163 if(r<34&&((unneeded_reg[j]>>r)&1)) break;
8165 if(regs[j].regmap[hr]==f_regmap[hr]&&f_regmap[hr]<TEMPREG) {
8166 //printf("Hit %x -> %x, %x %d/%d\n",start+i*4,ba[i],start+j*4,hr,r);
8168 if(regs[i].regmap[hr]==-1&&branch_regs[i].regmap[hr]==-1) {
8169 if(get_reg(regs[i].regmap,f_regmap[hr])>=0) break;
8170 if(get_reg(regs[i+2].regmap,f_regmap[hr])>=0) break;
8172 while(k>1&®s[k-1].regmap[hr]==-1) {
8173 if(count_free_regs(regs[k-1].regmap)<=minimum_free_regs[k-1]) {
8174 //printf("no free regs for store %x\n",start+(k-1)*4);
8177 if(get_reg(regs[k-1].regmap,f_regmap[hr])>=0) {
8178 //printf("no-match due to different register\n");
8181 if (dops[k-2].is_jump) {
8182 //printf("no-match due to branch\n");
8185 // call/ret fast path assumes no registers allocated
8186 if(k>2&&(dops[k-3].itype==UJUMP||dops[k-3].itype==RJUMP)&&dops[k-3].rt1==31) {
8191 if(regs[k-1].regmap[hr]==f_regmap[hr]&®map_pre[k][hr]==f_regmap[hr]) {
8192 //printf("Extend r%d, %x ->\n",hr,start+k*4);
8194 regs[k].regmap_entry[hr]=f_regmap[hr];
8195 regs[k].regmap[hr]=f_regmap[hr];
8196 regmap_pre[k+1][hr]=f_regmap[hr];
8197 regs[k].wasdirty&=~(1<<hr);
8198 regs[k].dirty&=~(1<<hr);
8199 regs[k].wasdirty|=(1<<hr)®s[k-1].dirty;
8200 regs[k].dirty|=(1<<hr)®s[k].wasdirty;
8201 regs[k].wasconst&=~(1<<hr);
8202 regs[k].isconst&=~(1<<hr);
8207 //printf("Fail Extend r%d, %x ->\n",hr,start+k*4);
8210 assert(regs[i-1].regmap[hr]==f_regmap[hr]);
8211 if(regs[i-1].regmap[hr]==f_regmap[hr]&®map_pre[i][hr]==f_regmap[hr]) {
8212 //printf("OK fill %x (r%d)\n",start+i*4,hr);
8213 regs[i].regmap_entry[hr]=f_regmap[hr];
8214 regs[i].regmap[hr]=f_regmap[hr];
8215 regs[i].wasdirty&=~(1<<hr);
8216 regs[i].dirty&=~(1<<hr);
8217 regs[i].wasdirty|=(1<<hr)®s[i-1].dirty;
8218 regs[i].dirty|=(1<<hr)®s[i-1].dirty;
8219 regs[i].wasconst&=~(1<<hr);
8220 regs[i].isconst&=~(1<<hr);
8221 branch_regs[i].regmap_entry[hr]=f_regmap[hr];
8222 branch_regs[i].wasdirty&=~(1<<hr);
8223 branch_regs[i].wasdirty|=(1<<hr)®s[i].dirty;
8224 branch_regs[i].regmap[hr]=f_regmap[hr];
8225 branch_regs[i].dirty&=~(1<<hr);
8226 branch_regs[i].dirty|=(1<<hr)®s[i].dirty;
8227 branch_regs[i].wasconst&=~(1<<hr);
8228 branch_regs[i].isconst&=~(1<<hr);
8229 if (!dops[i].is_ujump) {
8230 regmap_pre[i+2][hr]=f_regmap[hr];
8231 regs[i+2].wasdirty&=~(1<<hr);
8232 regs[i+2].wasdirty|=(1<<hr)®s[i].dirty;
8237 // Alloc register clean at beginning of loop,
8238 // but may dirty it in pass 6
8239 regs[k].regmap_entry[hr]=f_regmap[hr];
8240 regs[k].regmap[hr]=f_regmap[hr];
8241 regs[k].dirty&=~(1<<hr);
8242 regs[k].wasconst&=~(1<<hr);
8243 regs[k].isconst&=~(1<<hr);
8244 if (dops[k].is_jump) {
8245 branch_regs[k].regmap_entry[hr]=f_regmap[hr];
8246 branch_regs[k].regmap[hr]=f_regmap[hr];
8247 branch_regs[k].dirty&=~(1<<hr);
8248 branch_regs[k].wasconst&=~(1<<hr);
8249 branch_regs[k].isconst&=~(1<<hr);
8250 if (!dops[k].is_ujump) {
8251 regmap_pre[k+2][hr]=f_regmap[hr];
8252 regs[k+2].wasdirty&=~(1<<hr);
8257 regmap_pre[k+1][hr]=f_regmap[hr];
8258 regs[k+1].wasdirty&=~(1<<hr);
8261 if(regs[j].regmap[hr]==f_regmap[hr])
8262 regs[j].regmap_entry[hr]=f_regmap[hr];
8266 if(regs[j].regmap[hr]>=0)
8268 if(get_reg(regs[j].regmap,f_regmap[hr])>=0) {
8269 //printf("no-match due to different register\n");
8272 if (dops[j].is_ujump)
8274 // Stop on unconditional branch
8277 if(dops[j].itype==CJUMP||dops[j].itype==SJUMP)
8280 if(count_free_regs(regs[j].regmap)<=minimum_free_regs[j+1])
8283 if(count_free_regs(branch_regs[j].regmap)<=minimum_free_regs[j+1])
8286 if(get_reg(branch_regs[j].regmap,f_regmap[hr])>=0) {
8287 //printf("no-match due to different register (branch)\n");
8291 if(count_free_regs(regs[j].regmap)<=minimum_free_regs[j]) {
8292 //printf("No free regs for store %x\n",start+j*4);
8295 assert(f_regmap[hr]<64);
8302 // Non branch or undetermined branch target
8303 for(hr=0;hr<HOST_REGS;hr++)
8305 if(hr!=EXCLUDE_REG) {
8306 if(regs[i].regmap[hr]>=0) {
8307 if(f_regmap[hr]!=regs[i].regmap[hr]) {
8308 // dealloc old register
8310 for(n=0;n<HOST_REGS;n++)
8312 if(f_regmap[n]==regs[i].regmap[hr]) {f_regmap[n]=-1;}
8314 // and alloc new one
8315 f_regmap[hr]=regs[i].regmap[hr];
8320 // Try to restore cycle count at branch targets
8322 for(j=i;j<slen-1;j++) {
8323 if(regs[j].regmap[HOST_CCREG]!=-1) break;
8324 if(count_free_regs(regs[j].regmap)<=minimum_free_regs[j]) {
8325 //printf("no free regs for store %x\n",start+j*4);
8329 if(regs[j].regmap[HOST_CCREG]==CCREG) {
8331 //printf("Extend CC, %x -> %x\n",start+k*4,start+j*4);
8333 regs[k].regmap_entry[HOST_CCREG]=CCREG;
8334 regs[k].regmap[HOST_CCREG]=CCREG;
8335 regmap_pre[k+1][HOST_CCREG]=CCREG;
8336 regs[k+1].wasdirty|=1<<HOST_CCREG;
8337 regs[k].dirty|=1<<HOST_CCREG;
8338 regs[k].wasconst&=~(1<<HOST_CCREG);
8339 regs[k].isconst&=~(1<<HOST_CCREG);
8342 regs[j].regmap_entry[HOST_CCREG]=CCREG;
8344 // Work backwards from the branch target
8345 if(j>i&&f_regmap[HOST_CCREG]==CCREG)
8347 //printf("Extend backwards\n");
8350 while(regs[k-1].regmap[HOST_CCREG]==-1) {
8351 if(count_free_regs(regs[k-1].regmap)<=minimum_free_regs[k-1]) {
8352 //printf("no free regs for store %x\n",start+(k-1)*4);
8357 if(regs[k-1].regmap[HOST_CCREG]==CCREG) {
8358 //printf("Extend CC, %x ->\n",start+k*4);
8360 regs[k].regmap_entry[HOST_CCREG]=CCREG;
8361 regs[k].regmap[HOST_CCREG]=CCREG;
8362 regmap_pre[k+1][HOST_CCREG]=CCREG;
8363 regs[k+1].wasdirty|=1<<HOST_CCREG;
8364 regs[k].dirty|=1<<HOST_CCREG;
8365 regs[k].wasconst&=~(1<<HOST_CCREG);
8366 regs[k].isconst&=~(1<<HOST_CCREG);
8371 //printf("Fail Extend CC, %x ->\n",start+k*4);
8375 if(dops[i].itype!=STORE&&dops[i].itype!=STORELR&&dops[i].itype!=C1LS&&dops[i].itype!=SHIFT&&
8376 dops[i].itype!=NOP&&dops[i].itype!=MOV&&dops[i].itype!=ALU&&dops[i].itype!=SHIFTIMM&&
8377 dops[i].itype!=IMM16&&dops[i].itype!=LOAD&&dops[i].itype!=COP1)
8379 memcpy(f_regmap,regs[i].regmap,sizeof(f_regmap));
8385 // This allocates registers (if possible) one instruction prior
8386 // to use, which can avoid a load-use penalty on certain CPUs.
8387 static noinline void pass5b_preallocate2(void)
8390 for(i=0;i<slen-1;i++)
8392 if (!i || !dops[i-1].is_jump)
8396 if(dops[i].itype==ALU||dops[i].itype==MOV||dops[i].itype==LOAD||dops[i].itype==SHIFTIMM||dops[i].itype==IMM16
8397 ||((dops[i].itype==COP1||dops[i].itype==COP2)&&dops[i].opcode2<3))
8400 if((hr=get_reg(regs[i+1].regmap,dops[i+1].rs1))>=0)
8402 if(regs[i].regmap[hr]<0&®s[i+1].regmap_entry[hr]<0)
8404 regs[i].regmap[hr]=regs[i+1].regmap[hr];
8405 regmap_pre[i+1][hr]=regs[i+1].regmap[hr];
8406 regs[i+1].regmap_entry[hr]=regs[i+1].regmap[hr];
8407 regs[i].isconst&=~(1<<hr);
8408 regs[i].isconst|=regs[i+1].isconst&(1<<hr);
8409 constmap[i][hr]=constmap[i+1][hr];
8410 regs[i+1].wasdirty&=~(1<<hr);
8411 regs[i].dirty&=~(1<<hr);
8416 if((hr=get_reg(regs[i+1].regmap,dops[i+1].rs2))>=0)
8418 if(regs[i].regmap[hr]<0&®s[i+1].regmap_entry[hr]<0)
8420 regs[i].regmap[hr]=regs[i+1].regmap[hr];
8421 regmap_pre[i+1][hr]=regs[i+1].regmap[hr];
8422 regs[i+1].regmap_entry[hr]=regs[i+1].regmap[hr];
8423 regs[i].isconst&=~(1<<hr);
8424 regs[i].isconst|=regs[i+1].isconst&(1<<hr);
8425 constmap[i][hr]=constmap[i+1][hr];
8426 regs[i+1].wasdirty&=~(1<<hr);
8427 regs[i].dirty&=~(1<<hr);
8431 // Preload target address for load instruction (non-constant)
8432 if(dops[i+1].itype==LOAD&&dops[i+1].rs1&&get_reg(regs[i+1].regmap,dops[i+1].rs1)<0) {
8433 if((hr=get_reg(regs[i+1].regmap,dops[i+1].rt1))>=0)
8435 if(regs[i].regmap[hr]<0&®s[i+1].regmap_entry[hr]<0)
8437 regs[i].regmap[hr]=dops[i+1].rs1;
8438 regmap_pre[i+1][hr]=dops[i+1].rs1;
8439 regs[i+1].regmap_entry[hr]=dops[i+1].rs1;
8440 regs[i].isconst&=~(1<<hr);
8441 regs[i].isconst|=regs[i+1].isconst&(1<<hr);
8442 constmap[i][hr]=constmap[i+1][hr];
8443 regs[i+1].wasdirty&=~(1<<hr);
8444 regs[i].dirty&=~(1<<hr);
8448 // Load source into target register
8449 if(dops[i+1].use_lt1&&get_reg(regs[i+1].regmap,dops[i+1].rs1)<0) {
8450 if((hr=get_reg(regs[i+1].regmap,dops[i+1].rt1))>=0)
8452 if(regs[i].regmap[hr]<0&®s[i+1].regmap_entry[hr]<0)
8454 regs[i].regmap[hr]=dops[i+1].rs1;
8455 regmap_pre[i+1][hr]=dops[i+1].rs1;
8456 regs[i+1].regmap_entry[hr]=dops[i+1].rs1;
8457 regs[i].isconst&=~(1<<hr);
8458 regs[i].isconst|=regs[i+1].isconst&(1<<hr);
8459 constmap[i][hr]=constmap[i+1][hr];
8460 regs[i+1].wasdirty&=~(1<<hr);
8461 regs[i].dirty&=~(1<<hr);
8465 // Address for store instruction (non-constant)
8466 if(dops[i+1].itype==STORE||dops[i+1].itype==STORELR
8467 ||(dops[i+1].opcode&0x3b)==0x39||(dops[i+1].opcode&0x3b)==0x3a) { // SB/SH/SW/SD/SWC1/SDC1/SWC2/SDC2
8468 if(get_reg(regs[i+1].regmap,dops[i+1].rs1)<0) {
8469 hr=get_reg2(regs[i].regmap,regs[i+1].regmap,-1);
8470 if(hr<0) hr=get_reg_temp(regs[i+1].regmap);
8472 regs[i+1].regmap[hr]=AGEN1+((i+1)&1);
8473 regs[i+1].isconst&=~(1<<hr);
8476 if(regs[i].regmap[hr]<0&®s[i+1].regmap_entry[hr]<0)
8478 regs[i].regmap[hr]=dops[i+1].rs1;
8479 regmap_pre[i+1][hr]=dops[i+1].rs1;
8480 regs[i+1].regmap_entry[hr]=dops[i+1].rs1;
8481 regs[i].isconst&=~(1<<hr);
8482 regs[i].isconst|=regs[i+1].isconst&(1<<hr);
8483 constmap[i][hr]=constmap[i+1][hr];
8484 regs[i+1].wasdirty&=~(1<<hr);
8485 regs[i].dirty&=~(1<<hr);
8489 if(dops[i+1].itype==LOADLR||(dops[i+1].opcode&0x3b)==0x31||(dops[i+1].opcode&0x3b)==0x32) { // LWC1/LDC1, LWC2/LDC2
8490 if(get_reg(regs[i+1].regmap,dops[i+1].rs1)<0) {
8492 hr=get_reg(regs[i+1].regmap,FTEMP);
8494 if(regs[i].regmap[hr]<0&®s[i+1].regmap_entry[hr]<0)
8496 regs[i].regmap[hr]=dops[i+1].rs1;
8497 regmap_pre[i+1][hr]=dops[i+1].rs1;
8498 regs[i+1].regmap_entry[hr]=dops[i+1].rs1;
8499 regs[i].isconst&=~(1<<hr);
8500 regs[i].isconst|=regs[i+1].isconst&(1<<hr);
8501 constmap[i][hr]=constmap[i+1][hr];
8502 regs[i+1].wasdirty&=~(1<<hr);
8503 regs[i].dirty&=~(1<<hr);
8505 else if((nr=get_reg2(regs[i].regmap,regs[i+1].regmap,-1))>=0)
8507 // move it to another register
8508 regs[i+1].regmap[hr]=-1;
8509 regmap_pre[i+2][hr]=-1;
8510 regs[i+1].regmap[nr]=FTEMP;
8511 regmap_pre[i+2][nr]=FTEMP;
8512 regs[i].regmap[nr]=dops[i+1].rs1;
8513 regmap_pre[i+1][nr]=dops[i+1].rs1;
8514 regs[i+1].regmap_entry[nr]=dops[i+1].rs1;
8515 regs[i].isconst&=~(1<<nr);
8516 regs[i+1].isconst&=~(1<<nr);
8517 regs[i].dirty&=~(1<<nr);
8518 regs[i+1].wasdirty&=~(1<<nr);
8519 regs[i+1].dirty&=~(1<<nr);
8520 regs[i+2].wasdirty&=~(1<<nr);
8524 if(dops[i+1].itype==LOAD||dops[i+1].itype==LOADLR||dops[i+1].itype==STORE||dops[i+1].itype==STORELR/*||dops[i+1].itype==C1LS||||dops[i+1].itype==C2LS*/) {
8526 if(dops[i+1].itype==LOAD)
8527 hr=get_reg(regs[i+1].regmap,dops[i+1].rt1);
8528 if(dops[i+1].itype==LOADLR||(dops[i+1].opcode&0x3b)==0x31||(dops[i+1].opcode&0x3b)==0x32) // LWC1/LDC1, LWC2/LDC2
8529 hr=get_reg(regs[i+1].regmap,FTEMP);
8530 if(dops[i+1].itype==STORE||dops[i+1].itype==STORELR||(dops[i+1].opcode&0x3b)==0x39||(dops[i+1].opcode&0x3b)==0x3a) { // SWC1/SDC1/SWC2/SDC2
8531 hr=get_reg(regs[i+1].regmap,AGEN1+((i+1)&1));
8532 if(hr<0) hr=get_reg_temp(regs[i+1].regmap);
8534 if(hr>=0&®s[i].regmap[hr]<0) {
8535 int rs=get_reg(regs[i+1].regmap,dops[i+1].rs1);
8536 if(rs>=0&&((regs[i+1].wasconst>>rs)&1)) {
8537 regs[i].regmap[hr]=AGEN1+((i+1)&1);
8538 regmap_pre[i+1][hr]=AGEN1+((i+1)&1);
8539 regs[i+1].regmap_entry[hr]=AGEN1+((i+1)&1);
8540 regs[i].isconst&=~(1<<hr);
8541 regs[i+1].wasdirty&=~(1<<hr);
8542 regs[i].dirty&=~(1<<hr);
8552 // Write back dirty registers as soon as we will no longer modify them,
8553 // so that we don't end up with lots of writes at the branches.
8554 static noinline void pass6_clean_registers(int istart, int iend, int wr)
8556 static u_int wont_dirty[MAXBLOCK];
8557 static u_int will_dirty[MAXBLOCK];
8560 u_int will_dirty_i,will_dirty_next,temp_will_dirty;
8561 u_int wont_dirty_i,wont_dirty_next,temp_wont_dirty;
8563 will_dirty_i=will_dirty_next=0;
8564 wont_dirty_i=wont_dirty_next=0;
8566 will_dirty_i=will_dirty_next=will_dirty[iend+1];
8567 wont_dirty_i=wont_dirty_next=wont_dirty[iend+1];
8569 for (i=iend;i>=istart;i--)
8571 signed char rregmap_i[RRMAP_SIZE];
8572 u_int hr_candirty = 0;
8573 assert(HOST_REGS < 32);
8574 make_rregs(regs[i].regmap, rregmap_i, &hr_candirty);
8575 __builtin_prefetch(regs[i-1].regmap);
8578 signed char branch_rregmap_i[RRMAP_SIZE];
8579 u_int branch_hr_candirty = 0;
8580 make_rregs(branch_regs[i].regmap, branch_rregmap_i, &branch_hr_candirty);
8581 if(ba[i]<start || ba[i]>=(start+slen*4))
8583 // Branch out of this block, flush all regs
8585 will_dirty_i |= 1u << (get_rreg(branch_rregmap_i, dops[i].rt1) & 31);
8586 will_dirty_i |= 1u << (get_rreg(branch_rregmap_i, dops[i].rt2) & 31);
8587 will_dirty_i |= 1u << (get_rreg(branch_rregmap_i, dops[i+1].rt1) & 31);
8588 will_dirty_i |= 1u << (get_rreg(branch_rregmap_i, dops[i+1].rt2) & 31);
8589 will_dirty_i |= 1u << (get_rreg(branch_rregmap_i, CCREG) & 31);
8590 will_dirty_i &= branch_hr_candirty;
8591 if (dops[i].is_ujump)
8593 // Unconditional branch
8595 // Merge in delay slot (will dirty)
8596 will_dirty_i |= 1u << (get_rreg(rregmap_i, dops[i].rt1) & 31);
8597 will_dirty_i |= 1u << (get_rreg(rregmap_i, dops[i].rt2) & 31);
8598 will_dirty_i |= 1u << (get_rreg(rregmap_i, dops[i+1].rt1) & 31);
8599 will_dirty_i |= 1u << (get_rreg(rregmap_i, dops[i+1].rt2) & 31);
8600 will_dirty_i |= 1u << (get_rreg(rregmap_i, CCREG) & 31);
8601 will_dirty_i &= hr_candirty;
8605 // Conditional branch
8606 wont_dirty_i = wont_dirty_next;
8607 // Merge in delay slot (will dirty)
8608 // (the original code had no explanation why these 2 are commented out)
8609 //will_dirty_i |= 1u << (get_rreg(rregmap_i, dops[i].rt1) & 31);
8610 //will_dirty_i |= 1u << (get_rreg(rregmap_i, dops[i].rt2) & 31);
8611 will_dirty_i |= 1u << (get_rreg(rregmap_i, dops[i+1].rt1) & 31);
8612 will_dirty_i |= 1u << (get_rreg(rregmap_i, dops[i+1].rt2) & 31);
8613 will_dirty_i |= 1u << (get_rreg(rregmap_i, CCREG) & 31);
8614 will_dirty_i &= hr_candirty;
8616 // Merge in delay slot (wont dirty)
8617 wont_dirty_i |= 1u << (get_rreg(rregmap_i, dops[i].rt1) & 31);
8618 wont_dirty_i |= 1u << (get_rreg(rregmap_i, dops[i].rt2) & 31);
8619 wont_dirty_i |= 1u << (get_rreg(rregmap_i, dops[i+1].rt1) & 31);
8620 wont_dirty_i |= 1u << (get_rreg(rregmap_i, dops[i+1].rt2) & 31);
8621 wont_dirty_i |= 1u << (get_rreg(rregmap_i, CCREG) & 31);
8622 wont_dirty_i |= 1u << (get_rreg(branch_rregmap_i, dops[i].rt1) & 31);
8623 wont_dirty_i |= 1u << (get_rreg(branch_rregmap_i, dops[i].rt2) & 31);
8624 wont_dirty_i |= 1u << (get_rreg(branch_rregmap_i, dops[i+1].rt1) & 31);
8625 wont_dirty_i |= 1u << (get_rreg(branch_rregmap_i, dops[i+1].rt2) & 31);
8626 wont_dirty_i |= 1u << (get_rreg(branch_rregmap_i, CCREG) & 31);
8627 wont_dirty_i &= ~(1u << 31);
8629 #ifndef DESTRUCTIVE_WRITEBACK
8630 branch_regs[i].dirty&=wont_dirty_i;
8632 branch_regs[i].dirty|=will_dirty_i;
8638 if(ba[i]<=start+i*4) {
8640 if (dops[i].is_ujump)
8642 // Unconditional branch
8645 // Merge in delay slot (will dirty)
8646 temp_will_dirty |= 1u << (get_rreg(branch_rregmap_i, dops[i].rt1) & 31);
8647 temp_will_dirty |= 1u << (get_rreg(branch_rregmap_i, dops[i].rt2) & 31);
8648 temp_will_dirty |= 1u << (get_rreg(branch_rregmap_i, dops[i+1].rt1) & 31);
8649 temp_will_dirty |= 1u << (get_rreg(branch_rregmap_i, dops[i+1].rt2) & 31);
8650 temp_will_dirty |= 1u << (get_rreg(branch_rregmap_i, CCREG) & 31);
8651 temp_will_dirty &= branch_hr_candirty;
8652 temp_will_dirty |= 1u << (get_rreg(rregmap_i, dops[i].rt1) & 31);
8653 temp_will_dirty |= 1u << (get_rreg(rregmap_i, dops[i].rt2) & 31);
8654 temp_will_dirty |= 1u << (get_rreg(rregmap_i, dops[i+1].rt1) & 31);
8655 temp_will_dirty |= 1u << (get_rreg(rregmap_i, dops[i+1].rt2) & 31);
8656 temp_will_dirty |= 1u << (get_rreg(rregmap_i, CCREG) & 31);
8657 temp_will_dirty &= hr_candirty;
8659 // Conditional branch (not taken case)
8660 temp_will_dirty=will_dirty_next;
8661 temp_wont_dirty=wont_dirty_next;
8662 // Merge in delay slot (will dirty)
8663 temp_will_dirty |= 1u << (get_rreg(branch_rregmap_i, dops[i].rt1) & 31);
8664 temp_will_dirty |= 1u << (get_rreg(branch_rregmap_i, dops[i].rt2) & 31);
8665 temp_will_dirty |= 1u << (get_rreg(branch_rregmap_i, dops[i+1].rt1) & 31);
8666 temp_will_dirty |= 1u << (get_rreg(branch_rregmap_i, dops[i+1].rt2) & 31);
8667 temp_will_dirty |= 1u << (get_rreg(branch_rregmap_i, CCREG) & 31);
8668 temp_will_dirty &= branch_hr_candirty;
8669 //temp_will_dirty |= 1u << (get_rreg(rregmap_i, dops[i].rt1) & 31);
8670 //temp_will_dirty |= 1u << (get_rreg(rregmap_i, dops[i].rt2) & 31);
8671 temp_will_dirty |= 1u << (get_rreg(rregmap_i, dops[i+1].rt1) & 31);
8672 temp_will_dirty |= 1u << (get_rreg(rregmap_i, dops[i+1].rt2) & 31);
8673 temp_will_dirty |= 1u << (get_rreg(rregmap_i, CCREG) & 31);
8674 temp_will_dirty &= hr_candirty;
8676 // Merge in delay slot (wont dirty)
8677 temp_wont_dirty |= 1u << (get_rreg(rregmap_i, dops[i].rt1) & 31);
8678 temp_wont_dirty |= 1u << (get_rreg(rregmap_i, dops[i].rt2) & 31);
8679 temp_wont_dirty |= 1u << (get_rreg(rregmap_i, dops[i+1].rt1) & 31);
8680 temp_wont_dirty |= 1u << (get_rreg(rregmap_i, dops[i+1].rt2) & 31);
8681 temp_wont_dirty |= 1u << (get_rreg(rregmap_i, CCREG) & 31);
8682 temp_wont_dirty |= 1u << (get_rreg(branch_rregmap_i, dops[i].rt1) & 31);
8683 temp_wont_dirty |= 1u << (get_rreg(branch_rregmap_i, dops[i].rt2) & 31);
8684 temp_wont_dirty |= 1u << (get_rreg(branch_rregmap_i, dops[i+1].rt1) & 31);
8685 temp_wont_dirty |= 1u << (get_rreg(branch_rregmap_i, dops[i+1].rt2) & 31);
8686 temp_wont_dirty |= 1u << (get_rreg(branch_rregmap_i, CCREG) & 31);
8687 temp_wont_dirty &= ~(1u << 31);
8688 // Deal with changed mappings
8690 for(r=0;r<HOST_REGS;r++) {
8691 if(r!=EXCLUDE_REG) {
8692 if(regs[i].regmap[r]!=regmap_pre[i][r]) {
8693 temp_will_dirty&=~(1<<r);
8694 temp_wont_dirty&=~(1<<r);
8695 if(regmap_pre[i][r]>0 && regmap_pre[i][r]<34) {
8696 temp_will_dirty|=((unneeded_reg[i]>>regmap_pre[i][r])&1)<<r;
8697 temp_wont_dirty|=((unneeded_reg[i]>>regmap_pre[i][r])&1)<<r;
8699 temp_will_dirty|=1<<r;
8700 temp_wont_dirty|=1<<r;
8707 will_dirty[i]=temp_will_dirty;
8708 wont_dirty[i]=temp_wont_dirty;
8709 pass6_clean_registers((ba[i]-start)>>2,i-1,0);
8711 // Limit recursion. It can take an excessive amount
8712 // of time if there are a lot of nested loops.
8713 will_dirty[(ba[i]-start)>>2]=0;
8714 wont_dirty[(ba[i]-start)>>2]=-1;
8719 if (dops[i].is_ujump)
8721 // Unconditional branch
8724 //if(ba[i]>start+i*4) { // Disable recursion (for debugging)
8725 for(r=0;r<HOST_REGS;r++) {
8726 if(r!=EXCLUDE_REG) {
8727 if(branch_regs[i].regmap[r]==regs[(ba[i]-start)>>2].regmap_entry[r]) {
8728 will_dirty_i|=will_dirty[(ba[i]-start)>>2]&(1<<r);
8729 wont_dirty_i|=wont_dirty[(ba[i]-start)>>2]&(1<<r);
8731 if(branch_regs[i].regmap[r]>=0) {
8732 will_dirty_i|=((unneeded_reg[(ba[i]-start)>>2]>>branch_regs[i].regmap[r])&1)<<r;
8733 wont_dirty_i|=((unneeded_reg[(ba[i]-start)>>2]>>branch_regs[i].regmap[r])&1)<<r;
8738 // Merge in delay slot
8739 will_dirty_i |= 1u << (get_rreg(branch_rregmap_i, dops[i].rt1) & 31);
8740 will_dirty_i |= 1u << (get_rreg(branch_rregmap_i, dops[i].rt2) & 31);
8741 will_dirty_i |= 1u << (get_rreg(branch_rregmap_i, dops[i+1].rt1) & 31);
8742 will_dirty_i |= 1u << (get_rreg(branch_rregmap_i, dops[i+1].rt2) & 31);
8743 will_dirty_i |= 1u << (get_rreg(branch_rregmap_i, CCREG) & 31);
8744 will_dirty_i &= branch_hr_candirty;
8745 will_dirty_i |= 1u << (get_rreg(rregmap_i, dops[i].rt1) & 31);
8746 will_dirty_i |= 1u << (get_rreg(rregmap_i, dops[i].rt2) & 31);
8747 will_dirty_i |= 1u << (get_rreg(rregmap_i, dops[i+1].rt1) & 31);
8748 will_dirty_i |= 1u << (get_rreg(rregmap_i, dops[i+1].rt2) & 31);
8749 will_dirty_i |= 1u << (get_rreg(rregmap_i, CCREG) & 31);
8750 will_dirty_i &= hr_candirty;
8752 // Conditional branch
8753 will_dirty_i=will_dirty_next;
8754 wont_dirty_i=wont_dirty_next;
8755 //if(ba[i]>start+i*4) // Disable recursion (for debugging)
8756 for(r=0;r<HOST_REGS;r++) {
8757 if(r!=EXCLUDE_REG) {
8758 signed char target_reg=branch_regs[i].regmap[r];
8759 if(target_reg==regs[(ba[i]-start)>>2].regmap_entry[r]) {
8760 will_dirty_i&=will_dirty[(ba[i]-start)>>2]&(1<<r);
8761 wont_dirty_i|=wont_dirty[(ba[i]-start)>>2]&(1<<r);
8763 else if(target_reg>=0) {
8764 will_dirty_i&=((unneeded_reg[(ba[i]-start)>>2]>>target_reg)&1)<<r;
8765 wont_dirty_i|=((unneeded_reg[(ba[i]-start)>>2]>>target_reg)&1)<<r;
8769 // Merge in delay slot
8770 will_dirty_i |= 1u << (get_rreg(branch_rregmap_i, dops[i].rt1) & 31);
8771 will_dirty_i |= 1u << (get_rreg(branch_rregmap_i, dops[i].rt2) & 31);
8772 will_dirty_i |= 1u << (get_rreg(branch_rregmap_i, dops[i+1].rt1) & 31);
8773 will_dirty_i |= 1u << (get_rreg(branch_rregmap_i, dops[i+1].rt2) & 31);
8774 will_dirty_i |= 1u << (get_rreg(branch_rregmap_i, CCREG) & 31);
8775 will_dirty_i &= branch_hr_candirty;
8776 //will_dirty_i |= 1u << (get_rreg(rregmap_i, dops[i].rt1) & 31);
8777 //will_dirty_i |= 1u << (get_rreg(rregmap_i, dops[i].rt2) & 31);
8778 will_dirty_i |= 1u << (get_rreg(rregmap_i, dops[i+1].rt1) & 31);
8779 will_dirty_i |= 1u << (get_rreg(rregmap_i, dops[i+1].rt2) & 31);
8780 will_dirty_i |= 1u << (get_rreg(rregmap_i, CCREG) & 31);
8781 will_dirty_i &= hr_candirty;
8783 // Merge in delay slot (won't dirty)
8784 wont_dirty_i |= 1u << (get_rreg(rregmap_i, dops[i].rt1) & 31);
8785 wont_dirty_i |= 1u << (get_rreg(rregmap_i, dops[i].rt2) & 31);
8786 wont_dirty_i |= 1u << (get_rreg(rregmap_i, dops[i+1].rt1) & 31);
8787 wont_dirty_i |= 1u << (get_rreg(rregmap_i, dops[i+1].rt2) & 31);
8788 wont_dirty_i |= 1u << (get_rreg(rregmap_i, CCREG) & 31);
8789 wont_dirty_i |= 1u << (get_rreg(branch_rregmap_i, dops[i].rt1) & 31);
8790 wont_dirty_i |= 1u << (get_rreg(branch_rregmap_i, dops[i].rt2) & 31);
8791 wont_dirty_i |= 1u << (get_rreg(branch_rregmap_i, dops[i+1].rt1) & 31);
8792 wont_dirty_i |= 1u << (get_rreg(branch_rregmap_i, dops[i+1].rt2) & 31);
8793 wont_dirty_i |= 1u << (get_rreg(branch_rregmap_i, CCREG) & 31);
8794 wont_dirty_i &= ~(1u << 31);
8796 #ifndef DESTRUCTIVE_WRITEBACK
8797 branch_regs[i].dirty&=wont_dirty_i;
8799 branch_regs[i].dirty|=will_dirty_i;
8804 else if(dops[i].itype==SYSCALL||dops[i].itype==HLECALL||dops[i].itype==INTCALL)
8806 // SYSCALL instruction (software interrupt)
8810 else if(dops[i].itype==COP0 && (source[i]&0x3f)==0x18)
8812 // ERET instruction (return from interrupt)
8816 will_dirty_next=will_dirty_i;
8817 wont_dirty_next=wont_dirty_i;
8818 will_dirty_i |= 1u << (get_rreg(rregmap_i, dops[i].rt1) & 31);
8819 will_dirty_i |= 1u << (get_rreg(rregmap_i, dops[i].rt2) & 31);
8820 will_dirty_i |= 1u << (get_rreg(rregmap_i, CCREG) & 31);
8821 will_dirty_i &= hr_candirty;
8822 wont_dirty_i |= 1u << (get_rreg(rregmap_i, dops[i].rt1) & 31);
8823 wont_dirty_i |= 1u << (get_rreg(rregmap_i, dops[i].rt2) & 31);
8824 wont_dirty_i |= 1u << (get_rreg(rregmap_i, CCREG) & 31);
8825 wont_dirty_i &= ~(1u << 31);
8826 if (i > istart && !dops[i].is_jump) {
8827 // Don't store a register immediately after writing it,
8828 // may prevent dual-issue.
8829 wont_dirty_i |= 1u << (get_rreg(rregmap_i, dops[i-1].rt1) & 31);
8830 wont_dirty_i |= 1u << (get_rreg(rregmap_i, dops[i-1].rt2) & 31);
8833 will_dirty[i]=will_dirty_i;
8834 wont_dirty[i]=wont_dirty_i;
8835 // Mark registers that won't be dirtied as not dirty
8837 regs[i].dirty|=will_dirty_i;
8838 #ifndef DESTRUCTIVE_WRITEBACK
8839 regs[i].dirty&=wont_dirty_i;
8842 if (i < iend-1 && !dops[i].is_ujump) {
8843 for(r=0;r<HOST_REGS;r++) {
8844 if(r!=EXCLUDE_REG) {
8845 if(regs[i].regmap[r]==regmap_pre[i+2][r]) {
8846 regs[i+2].wasdirty&=wont_dirty_i|~(1<<r);
8847 }else {/*printf("i: %x (%d) mismatch(+2): %d\n",start+i*4,i,r);assert(!((wont_dirty_i>>r)&1));*/}
8855 for(r=0;r<HOST_REGS;r++) {
8856 if(r!=EXCLUDE_REG) {
8857 if(regs[i].regmap[r]==regmap_pre[i+1][r]) {
8858 regs[i+1].wasdirty&=wont_dirty_i|~(1<<r);
8859 }else {/*printf("i: %x (%d) mismatch(+1): %d\n",start+i*4,i,r);assert(!((wont_dirty_i>>r)&1));*/}
8866 // Deal with changed mappings
8867 temp_will_dirty=will_dirty_i;
8868 temp_wont_dirty=wont_dirty_i;
8869 for(r=0;r<HOST_REGS;r++) {
8870 if(r!=EXCLUDE_REG) {
8872 if(regs[i].regmap[r]==regmap_pre[i][r]) {
8874 #ifndef DESTRUCTIVE_WRITEBACK
8875 regs[i].wasdirty&=wont_dirty_i|~(1<<r);
8877 regs[i].wasdirty|=will_dirty_i&(1<<r);
8880 else if(regmap_pre[i][r]>=0&&(nr=get_rreg(rregmap_i,regmap_pre[i][r]))>=0) {
8881 // Register moved to a different register
8882 will_dirty_i&=~(1<<r);
8883 wont_dirty_i&=~(1<<r);
8884 will_dirty_i|=((temp_will_dirty>>nr)&1)<<r;
8885 wont_dirty_i|=((temp_wont_dirty>>nr)&1)<<r;
8887 #ifndef DESTRUCTIVE_WRITEBACK
8888 regs[i].wasdirty&=wont_dirty_i|~(1<<r);
8890 regs[i].wasdirty|=will_dirty_i&(1<<r);
8894 will_dirty_i&=~(1<<r);
8895 wont_dirty_i&=~(1<<r);
8896 if(regmap_pre[i][r]>0 && regmap_pre[i][r]<34) {
8897 will_dirty_i|=((unneeded_reg[i]>>regmap_pre[i][r])&1)<<r;
8898 wont_dirty_i|=((unneeded_reg[i]>>regmap_pre[i][r])&1)<<r;
8901 /*printf("i: %x (%d) mismatch: %d\n",start+i*4,i,r);assert(!((will_dirty>>r)&1));*/
8909 static noinline void pass10_expire_blocks(void)
8911 u_int step = MAX_OUTPUT_BLOCK_SIZE / PAGE_COUNT / 2;
8912 // not sizeof(ndrc->translation_cache) due to vita hack
8913 u_int step_mask = ((1u << TARGET_SIZE_2) - 1u) & ~(step - 1u);
8914 u_int end = (out - ndrc->translation_cache + EXPIRITY_OFFSET) & step_mask;
8915 u_int base_shift = __builtin_ctz(MAX_OUTPUT_BLOCK_SIZE);
8918 for (; expirep != end; expirep = ((expirep + step) & step_mask))
8920 u_int base_offs = expirep & ~(MAX_OUTPUT_BLOCK_SIZE - 1);
8921 u_int block_i = expirep / step & (PAGE_COUNT - 1);
8922 u_int phase = (expirep >> (base_shift - 1)) & 1u;
8923 if (!(expirep & (MAX_OUTPUT_BLOCK_SIZE / 2 - 1))) {
8924 inv_debug("EXP: base_offs %x/%lx phase %u\n", base_offs,
8925 (long)(out - ndrc->translation_cache), phase);
8929 hit = blocks_remove_matching_addrs(&blocks[block_i], base_offs, base_shift);
8933 memset(mini_ht, -1, sizeof(mini_ht));
8938 unlink_jumps_tc_range(jumps[block_i], base_offs, base_shift);
8942 static struct block_info *new_block_info(u_int start, u_int len,
8943 const void *source, const void *copy, u_char *beginning, u_short jump_in_count)
8945 struct block_info **b_pptr;
8946 struct block_info *block;
8947 u_int page = get_page(start);
8949 block = malloc(sizeof(*block) + jump_in_count * sizeof(block->jump_in[0]));
8951 assert(jump_in_count > 0);
8952 block->source = source;
8954 block->start = start;
8956 block->reg_sv_flags = 0;
8957 block->tc_offs = beginning - ndrc->translation_cache;
8958 //block->tc_len = out - beginning;
8959 block->is_dirty = 0;
8960 block->inv_near_misses = 0;
8961 block->jump_in_cnt = jump_in_count;
8963 // insert sorted by start mirror-unmasked vaddr
8964 for (b_pptr = &blocks[page]; ; b_pptr = &((*b_pptr)->next)) {
8965 if (*b_pptr == NULL || (*b_pptr)->start >= start) {
8966 block->next = *b_pptr;
8971 stat_inc(stat_blocks);
8975 static int new_recompile_block(u_int addr)
8977 u_int pagelimit = 0;
8978 u_int state_rflags = 0;
8981 assem_debug("NOTCOMPILED: addr = %x -> %p\n", addr, out);
8983 // this is just for speculation
8984 for (i = 1; i < 32; i++) {
8985 if ((psxRegs.GPR.r[i] & 0xffff0000) == 0x1f800000)
8986 state_rflags |= 1 << i;
8989 assert(!(addr & 3));
8991 new_dynarec_did_compile=1;
8992 if (Config.HLE && start == 0x80001000) // hlecall
8994 // XXX: is this enough? Maybe check hleSoftCall?
8995 void *beginning = start_block();
8997 emit_movimm(start,0);
8998 emit_writeword(0,&pcaddr);
8999 emit_far_jump(new_dyna_leave);
9001 end_block(beginning);
9002 struct block_info *block = new_block_info(start, 4, NULL, NULL, beginning, 1);
9003 block->jump_in[0].vaddr = start;
9004 block->jump_in[0].addr = beginning;
9007 else if (f1_hack && hack_addr == 0) {
9008 void *beginning = start_block();
9009 emit_movimm(start, 0);
9010 emit_writeword(0, &hack_addr);
9011 emit_readword(&psxRegs.GPR.n.sp, 0);
9012 emit_readptr(&mem_rtab, 1);
9013 emit_shrimm(0, 12, 2);
9014 emit_readptr_dualindexedx_ptrlen(1, 2, 1);
9015 emit_addimm(0, 0x18, 0);
9016 emit_adds_ptr(1, 1, 1);
9017 emit_ldr_dualindexed(1, 0, 0);
9018 emit_writeword(0, &psxRegs.GPR.r[26]); // lw k0, 0x18(sp)
9019 emit_far_call(ndrc_get_addr_ht);
9020 emit_jmpreg(0); // jr k0
9022 end_block(beginning);
9024 struct block_info *block = new_block_info(start, 4, NULL, NULL, beginning, 1);
9025 block->jump_in[0].vaddr = start;
9026 block->jump_in[0].addr = beginning;
9027 SysPrintf("F1 hack to %08x\n", start);
9031 cycle_multiplier_active = Config.cycle_multiplier_override && Config.cycle_multiplier == CYCLE_MULT_DEFAULT
9032 ? Config.cycle_multiplier_override : Config.cycle_multiplier;
9034 source = get_source_start(start, &pagelimit);
9035 if (source == NULL) {
9036 if (addr != hack_addr) {
9037 SysPrintf("Compile at bogus memory address: %08x\n", addr);
9044 /* Pass 1: disassemble */
9045 /* Pass 2: register dependencies, branch targets */
9046 /* Pass 3: register allocation */
9047 /* Pass 4: branch dependencies */
9048 /* Pass 5: pre-alloc */
9049 /* Pass 6: optimize clean/dirty state */
9050 /* Pass 7: flag 32-bit registers */
9051 /* Pass 8: assembly */
9052 /* Pass 9: linker */
9053 /* Pass 10: garbage collection / free memory */
9055 /* Pass 1 disassembly */
9057 pass1_disassemble(pagelimit);
9059 int clear_hack_addr = apply_hacks();
9061 /* Pass 2 - Register dependencies and branch targets */
9063 pass2_unneeded_regs(0,slen-1,0);
9065 /* Pass 3 - Register allocation */
9067 pass3_register_alloc(addr);
9069 /* Pass 4 - Cull unused host registers */
9071 pass4_cull_unused_regs();
9073 /* Pass 5 - Pre-allocate registers */
9075 pass5a_preallocate1();
9076 pass5b_preallocate2();
9078 /* Pass 6 - Optimize clean/dirty state */
9079 pass6_clean_registers(0, slen-1, 1);
9081 /* Pass 7 - Identify 32-bit registers */
9082 for (i=slen-1;i>=0;i--)
9084 if(dops[i].itype==CJUMP||dops[i].itype==SJUMP)
9086 // Conditional branch
9087 if((source[i]>>16)!=0x1000&&i<slen-2) {
9088 // Mark this address as a branch target since it may be called
9089 // upon return from interrupt
9095 /* Pass 8 - Assembly */
9096 linkcount=0;stubcount=0;
9099 void *beginning=start_block();
9100 void *instr_addr0_override = NULL;
9103 if (start == 0x80030000) {
9104 // nasty hack for the fastbios thing
9105 // override block entry to this code
9106 instr_addr0_override = out;
9107 emit_movimm(start,0);
9108 // abuse io address var as a flag that we
9109 // have already returned here once
9110 emit_readword(&address,1);
9111 emit_writeword(0,&pcaddr);
9112 emit_writeword(0,&address);
9115 emit_jeq(out + 4*2);
9116 emit_far_jump(new_dyna_leave);
9118 emit_jne(new_dyna_leave);
9123 __builtin_prefetch(regs[i+1].regmap);
9124 check_regmap(regmap_pre[i]);
9125 check_regmap(regs[i].regmap_entry);
9126 check_regmap(regs[i].regmap);
9127 //if(ds) printf("ds: ");
9128 disassemble_inst(i);
9130 ds=0; // Skip delay slot
9131 if(dops[i].bt) assem_debug("OOPS - branch into delay slot\n");
9132 instr_addr[i] = NULL;
9134 speculate_register_values(i);
9135 #ifndef DESTRUCTIVE_WRITEBACK
9136 if (i < 2 || !dops[i-2].is_ujump)
9138 wb_valid(regmap_pre[i],regs[i].regmap_entry,dirty_pre,regs[i].wasdirty,unneeded_reg[i]);
9140 if((dops[i].itype==CJUMP||dops[i].itype==SJUMP)) {
9141 dirty_pre=branch_regs[i].dirty;
9143 dirty_pre=regs[i].dirty;
9147 if (i < 2 || !dops[i-2].is_ujump)
9149 wb_invalidate(regmap_pre[i],regs[i].regmap_entry,regs[i].wasdirty,unneeded_reg[i]);
9150 loop_preload(regmap_pre[i],regs[i].regmap_entry);
9152 // branch target entry point
9153 instr_addr[i] = out;
9154 assem_debug("<->\n");
9155 drc_dbg_emit_do_cmp(i, ccadj[i]);
9156 if (clear_hack_addr) {
9158 emit_writeword(0, &hack_addr);
9159 clear_hack_addr = 0;
9163 if(regs[i].regmap_entry[HOST_CCREG]==CCREG&®s[i].regmap[HOST_CCREG]!=CCREG)
9164 wb_register(CCREG,regs[i].regmap_entry,regs[i].wasdirty);
9165 load_regs(regs[i].regmap_entry,regs[i].regmap,dops[i].rs1,dops[i].rs2);
9166 address_generation(i,®s[i],regs[i].regmap_entry);
9167 load_consts(regmap_pre[i],regs[i].regmap,i);
9170 // Load the delay slot registers if necessary
9171 if(dops[i+1].rs1!=dops[i].rs1&&dops[i+1].rs1!=dops[i].rs2&&(dops[i+1].rs1!=dops[i].rt1||dops[i].rt1==0))
9172 load_regs(regs[i].regmap_entry,regs[i].regmap,dops[i+1].rs1,dops[i+1].rs1);
9173 if(dops[i+1].rs2!=dops[i+1].rs1&&dops[i+1].rs2!=dops[i].rs1&&dops[i+1].rs2!=dops[i].rs2&&(dops[i+1].rs2!=dops[i].rt1||dops[i].rt1==0))
9174 load_regs(regs[i].regmap_entry,regs[i].regmap,dops[i+1].rs2,dops[i+1].rs2);
9175 if (ram_offset && (dops[i+1].is_load || dops[i+1].is_store))
9176 load_reg(regs[i].regmap_entry,regs[i].regmap,ROREG);
9177 if (dops[i+1].is_store)
9178 load_reg(regs[i].regmap_entry,regs[i].regmap,INVCP);
9182 // Preload registers for following instruction
9183 if(dops[i+1].rs1!=dops[i].rs1&&dops[i+1].rs1!=dops[i].rs2)
9184 if(dops[i+1].rs1!=dops[i].rt1&&dops[i+1].rs1!=dops[i].rt2)
9185 load_regs(regs[i].regmap_entry,regs[i].regmap,dops[i+1].rs1,dops[i+1].rs1);
9186 if(dops[i+1].rs2!=dops[i+1].rs1&&dops[i+1].rs2!=dops[i].rs1&&dops[i+1].rs2!=dops[i].rs2)
9187 if(dops[i+1].rs2!=dops[i].rt1&&dops[i+1].rs2!=dops[i].rt2)
9188 load_regs(regs[i].regmap_entry,regs[i].regmap,dops[i+1].rs2,dops[i+1].rs2);
9190 // TODO: if(is_ooo(i)) address_generation(i+1);
9191 if (!dops[i].is_jump || dops[i].itype == CJUMP)
9192 load_reg(regs[i].regmap_entry,regs[i].regmap,CCREG);
9193 if (ram_offset && (dops[i].is_load || dops[i].is_store))
9194 load_reg(regs[i].regmap_entry,regs[i].regmap,ROREG);
9195 if (dops[i].is_store)
9196 load_reg(regs[i].regmap_entry,regs[i].regmap,INVCP);
9198 ds = assemble(i, ®s[i], ccadj[i]);
9200 if (dops[i].is_ujump)
9203 literal_pool_jumpover(256);
9208 if (slen > 0 && dops[slen-1].itype == INTCALL) {
9209 // no ending needed for this block since INTCALL never returns
9211 // If the block did not end with an unconditional branch,
9212 // add a jump to the next instruction.
9214 if (!dops[i-2].is_ujump) {
9215 assert(!dops[i-1].is_jump);
9217 if(dops[i-2].itype!=CJUMP&&dops[i-2].itype!=SJUMP) {
9218 store_regs_bt(regs[i-1].regmap,regs[i-1].dirty,start+i*4);
9219 if(regs[i-1].regmap[HOST_CCREG]!=CCREG)
9220 emit_loadreg(CCREG,HOST_CCREG);
9221 emit_addimm(HOST_CCREG, ccadj[i-1] + CLOCK_ADJUST(1), HOST_CCREG);
9225 store_regs_bt(branch_regs[i-2].regmap,branch_regs[i-2].dirty,start+i*4);
9226 assert(branch_regs[i-2].regmap[HOST_CCREG]==CCREG);
9228 add_to_linker(out,start+i*4,0);
9235 assert(!dops[i-1].is_jump);
9236 store_regs_bt(regs[i-1].regmap,regs[i-1].dirty,start+i*4);
9237 if(regs[i-1].regmap[HOST_CCREG]!=CCREG)
9238 emit_loadreg(CCREG,HOST_CCREG);
9239 emit_addimm(HOST_CCREG, ccadj[i-1] + CLOCK_ADJUST(1), HOST_CCREG);
9240 add_to_linker(out,start+i*4,0);
9244 // TODO: delay slot stubs?
9246 for(i=0;i<stubcount;i++)
9248 switch(stubs[i].type)
9256 do_readstub(i);break;
9261 do_writestub(i);break;
9265 do_invstub(i);break;
9267 do_cop1stub(i);break;
9269 do_unalignedwritestub(i);break;
9273 if (instr_addr0_override)
9274 instr_addr[0] = instr_addr0_override;
9277 /* check for improper expiration */
9278 for (i = 0; i < ARRAY_SIZE(jumps); i++) {
9282 for (j = 0; j < jumps[i]->count; j++)
9283 assert(jumps[i]->e[j].stub < beginning || (u_char *)jumps[i]->e[j].stub > out);
9287 /* Pass 9 - Linker */
9288 for(i=0;i<linkcount;i++)
9290 assem_debug("%p -> %8x\n",link_addr[i].addr,link_addr[i].target);
9292 if (!link_addr[i].internal)
9295 void *addr = check_addr(link_addr[i].target);
9296 emit_extjump(link_addr[i].addr, link_addr[i].target);
9298 set_jump_target(link_addr[i].addr, addr);
9299 ndrc_add_jump_out(link_addr[i].target,stub);
9302 set_jump_target(link_addr[i].addr, stub);
9307 int target=(link_addr[i].target-start)>>2;
9308 assert(target>=0&&target<slen);
9309 assert(instr_addr[target]);
9310 //#ifdef CORTEX_A8_BRANCH_PREDICTION_HACK
9311 //set_jump_target_fillslot(link_addr[i].addr,instr_addr[target],link_addr[i].ext>>1);
9313 set_jump_target(link_addr[i].addr, instr_addr[target]);
9318 u_int source_len = slen*4;
9319 if (dops[slen-1].itype == INTCALL && source_len > 4)
9320 // no need to treat the last instruction as compiled
9321 // as interpreter fully handles it
9324 if ((u_char *)copy + source_len > (u_char *)shadow + sizeof(shadow))
9327 // External Branch Targets (jump_in)
9328 int jump_in_count = 1;
9329 assert(instr_addr[0]);
9330 for (i = 1; i < slen; i++)
9332 if (dops[i].bt && instr_addr[i])
9336 struct block_info *block =
9337 new_block_info(start, slen * 4, source, copy, beginning, jump_in_count);
9338 block->reg_sv_flags = state_rflags;
9341 for (i = 0; i < slen; i++)
9343 if ((i == 0 || dops[i].bt) && instr_addr[i])
9345 assem_debug("%p (%d) <- %8x\n", instr_addr[i], i, start + i*4);
9346 u_int vaddr = start + i*4;
9352 entry = instr_addr[i];
9354 emit_jmp(instr_addr[i]);
9356 block->jump_in[jump_in_i].vaddr = vaddr;
9357 block->jump_in[jump_in_i].addr = entry;
9361 assert(jump_in_i == jump_in_count);
9362 hash_table_add(block->jump_in[0].vaddr, block->jump_in[0].addr);
9363 // Write out the literal pool if necessary
9365 #ifdef CORTEX_A8_BRANCH_PREDICTION_HACK
9367 if(((u_int)out)&7) emit_addnop(13);
9369 assert(out - (u_char *)beginning < MAX_OUTPUT_BLOCK_SIZE);
9370 //printf("shadow buffer: %p-%p\n",copy,(u_char *)copy+slen*4);
9371 memcpy(copy, source, source_len);
9374 end_block(beginning);
9376 // If we're within 256K of the end of the buffer,
9377 // start over from the beginning. (Is 256K enough?)
9378 if (out > ndrc->translation_cache + sizeof(ndrc->translation_cache) - MAX_OUTPUT_BLOCK_SIZE)
9379 out = ndrc->translation_cache;
9381 // Trap writes to any of the pages we compiled
9382 mark_invalid_code(start, slen*4, 0);
9384 /* Pass 10 - Free memory by expiring oldest blocks */
9386 pass10_expire_blocks();
9391 stat_inc(stat_bc_direct);
9395 // vim:shiftwidth=2:expandtab