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>
33 #include <psp2/kernel/sysmem.h>
37 #include "new_dynarec_config.h"
38 #include "../psxhle.h"
39 #include "../psxinterpreter.h"
41 #include "emu_if.h" // emulator interface
43 #define noinline __attribute__((noinline,noclone))
45 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
48 #define min(a, b) ((b) < (a) ? (b) : (a))
51 #define max(a, b) ((b) > (a) ? (b) : (a))
58 #define assem_debug printf
60 #define assem_debug(...)
62 //#define inv_debug printf
63 #define inv_debug(...)
66 #include "assem_x86.h"
69 #include "assem_x64.h"
72 #include "assem_arm.h"
75 #include "assem_arm64.h"
78 #define RAM_SIZE 0x200000
80 #define MAX_OUTPUT_BLOCK_SIZE 262144
84 u_char translation_cache[1 << TARGET_SIZE_2];
87 struct tramp_insns ops[2048 / sizeof(struct tramp_insns)];
88 const void *f[2048 / sizeof(void *)];
92 #ifdef BASE_ADDR_DYNAMIC
93 static struct ndrc_mem *ndrc;
95 static struct ndrc_mem ndrc_ __attribute__((aligned(4096)));
96 static struct ndrc_mem *ndrc = &ndrc_;
119 signed char regmap_entry[HOST_REGS];
120 signed char regmap[HOST_REGS];
126 u_int loadedconst; // host regs that have constants loaded
127 u_int waswritten; // MIPS regs that were used as store base before
130 // note: asm depends on this layout
136 struct ll_entry *next;
164 static struct decoded_insn
182 struct ht_entry hash_table[65536] __attribute__((aligned(16)));
183 struct ll_entry *jump_in[4096] __attribute__((aligned(16)));
184 struct ll_entry *jump_dirty[4096];
186 static struct ll_entry *jump_out[4096];
188 static u_int *source;
189 static char insn[MAXBLOCK][10];
190 static uint64_t gte_rs[MAXBLOCK]; // gte: 32 data and 32 ctl regs
191 static uint64_t gte_rt[MAXBLOCK];
192 static uint64_t gte_unneeded[MAXBLOCK];
193 static u_int smrv[32]; // speculated MIPS register values
194 static u_int smrv_strong; // mask or regs that are likely to have correct values
195 static u_int smrv_weak; // same, but somewhat less likely
196 static u_int smrv_strong_next; // same, but after current insn executes
197 static u_int smrv_weak_next;
198 static int imm[MAXBLOCK];
199 static u_int ba[MAXBLOCK];
200 static uint64_t unneeded_reg[MAXBLOCK];
201 static uint64_t branch_unneeded_reg[MAXBLOCK];
202 static signed char regmap_pre[MAXBLOCK][HOST_REGS]; // pre-instruction i?
203 // contains 'real' consts at [i] insn, but may differ from what's actually
204 // loaded in host reg as 'final' value is always loaded, see get_final_value()
205 static uint32_t current_constmap[HOST_REGS];
206 static uint32_t constmap[MAXBLOCK][HOST_REGS];
207 static struct regstat regs[MAXBLOCK];
208 static struct regstat branch_regs[MAXBLOCK];
209 static signed char minimum_free_regs[MAXBLOCK];
210 static u_int needed_reg[MAXBLOCK];
211 static u_int wont_dirty[MAXBLOCK];
212 static u_int will_dirty[MAXBLOCK];
213 static int ccadj[MAXBLOCK];
215 static void *instr_addr[MAXBLOCK];
216 static struct link_entry link_addr[MAXBLOCK];
217 static int linkcount;
218 static struct code_stub stubs[MAXBLOCK*3];
219 static int stubcount;
220 static u_int literals[1024][2];
221 static int literalcount;
222 static int is_delayslot;
223 static char shadow[1048576] __attribute__((aligned(16)));
226 static u_int stop_after_jal;
228 static uintptr_t ram_offset;
230 static const uintptr_t ram_offset=0;
233 int new_dynarec_hacks;
234 int new_dynarec_hacks_pergame;
235 int new_dynarec_hacks_old;
236 int new_dynarec_did_compile;
238 #define HACK_ENABLED(x) ((new_dynarec_hacks | new_dynarec_hacks_pergame) & (x))
240 extern int cycle_count; // ... until end of the timeslice, counts -N -> 0
241 extern int last_count; // last absolute target, often = next_interupt
243 extern int pending_exception;
244 extern int branch_target;
245 extern uintptr_t mini_ht[32][2];
246 extern u_char restore_candidate[512];
248 /* registers that may be allocated */
250 #define LOREG 32 // lo
251 #define HIREG 33 // hi
252 //#define FSREG 34 // FPU status (FCSR)
253 #define CSREG 35 // Coprocessor status
254 #define CCREG 36 // Cycle count
255 #define INVCP 37 // Pointer to invalid_code
256 //#define MMREG 38 // Pointer to memory_map
257 //#define ROREG 39 // ram offset (if rdram!=0x80000000)
259 #define FTEMP 40 // FPU temporary register
260 #define PTEMP 41 // Prefetch temporary register
261 //#define TLREG 42 // TLB mapping offset
262 #define RHASH 43 // Return address hash
263 #define RHTBL 44 // Return address hash table address
264 #define RTEMP 45 // JR/JALR address register
266 #define AGEN1 46 // Address generation temporary register
267 //#define AGEN2 47 // Address generation temporary register
268 //#define MGEN1 48 // Maptable address generation temporary register
269 //#define MGEN2 49 // Maptable address generation temporary register
270 #define BTREG 50 // Branch target temporary register
272 /* instruction types */
273 #define NOP 0 // No operation
274 #define LOAD 1 // Load
275 #define STORE 2 // Store
276 #define LOADLR 3 // Unaligned load
277 #define STORELR 4 // Unaligned store
278 #define MOV 5 // Move
279 #define ALU 6 // Arithmetic/logic
280 #define MULTDIV 7 // Multiply/divide
281 #define SHIFT 8 // Shift by register
282 #define SHIFTIMM 9// Shift by immediate
283 #define IMM16 10 // 16-bit immediate
284 #define RJUMP 11 // Unconditional jump to register
285 #define UJUMP 12 // Unconditional jump
286 #define CJUMP 13 // Conditional branch (BEQ/BNE/BGTZ/BLEZ)
287 #define SJUMP 14 // Conditional branch (regimm format)
288 #define COP0 15 // Coprocessor 0
289 #define COP1 16 // Coprocessor 1
290 #define C1LS 17 // Coprocessor 1 load/store
291 //#define FJUMP 18 // Conditional branch (floating point)
292 //#define FLOAT 19 // Floating point unit
293 //#define FCONV 20 // Convert integer to float
294 //#define FCOMP 21 // Floating point compare (sets FSREG)
295 #define SYSCALL 22// SYSCALL
296 #define OTHER 23 // Other
297 #define SPAN 24 // Branch/delay slot spans 2 pages
298 #define NI 25 // Not implemented
299 #define HLECALL 26// PCSX fake opcodes for HLE
300 #define COP2 27 // Coprocessor 2 move
301 #define C2LS 28 // Coprocessor 2 load/store
302 #define C2OP 29 // Coprocessor 2 operation
303 #define INTCALL 30// Call interpreter to handle rare corner cases
310 #define DJT_1 (void *)1l // no function, just a label in assem_debug log
311 #define DJT_2 (void *)2l
314 int new_recompile_block(u_int addr);
315 void *get_addr_ht(u_int vaddr);
316 void invalidate_block(u_int block);
317 void invalidate_addr(u_int addr);
318 void remove_hash(int vaddr);
320 void dyna_linker_ds();
322 void verify_code_ds();
325 void fp_exception_ds();
326 void jump_to_new_pc();
327 void call_gteStall();
328 void new_dyna_leave();
330 // Needed by assembler
331 static void wb_register(signed char r,signed char regmap[],uint64_t dirty);
332 static void wb_dirtys(signed char i_regmap[],uint64_t i_dirty);
333 static void wb_needed_dirtys(signed char i_regmap[],uint64_t i_dirty,int addr);
334 static void load_all_regs(signed char i_regmap[]);
335 static void load_needed_regs(signed char i_regmap[],signed char next_regmap[]);
336 static void load_regs_entry(int t);
337 static void load_all_consts(signed char regmap[],u_int dirty,int i);
338 static u_int get_host_reglist(const signed char *regmap);
340 static int verify_dirty(const u_int *ptr);
341 static int get_final_value(int hr, int i, int *value);
342 static void add_stub(enum stub_type type, void *addr, void *retaddr,
343 u_int a, uintptr_t b, uintptr_t c, u_int d, u_int e);
344 static void add_stub_r(enum stub_type type, void *addr, void *retaddr,
345 int i, int addr_reg, const struct regstat *i_regs, int ccadj, u_int reglist);
346 static void add_to_linker(void *addr, u_int target, int ext);
347 static void *emit_fastpath_cmp_jump(int i,int addr,int *addr_reg_override);
348 static void *get_direct_memhandler(void *table, u_int addr,
349 enum stub_type type, uintptr_t *addr_host);
350 static void cop2_do_stall_check(u_int op, int i, const struct regstat *i_regs, u_int reglist);
351 static void pass_args(int a0, int a1);
352 static void emit_far_jump(const void *f);
353 static void emit_far_call(const void *f);
355 static void mprotect_w_x(void *start, void *end, int is_x)
359 // *Open* enables write on all memory that was
360 // allocated by sceKernelAllocMemBlockForVM()?
362 sceKernelCloseVMDomain();
364 sceKernelOpenVMDomain();
366 u_long mstart = (u_long)start & ~4095ul;
367 u_long mend = (u_long)end;
368 if (mprotect((void *)mstart, mend - mstart,
369 PROT_READ | (is_x ? PROT_EXEC : PROT_WRITE)) != 0)
370 SysPrintf("mprotect(%c) failed: %s\n", is_x ? 'x' : 'w', strerror(errno));
375 static void start_tcache_write(void *start, void *end)
377 mprotect_w_x(start, end, 0);
380 static void end_tcache_write(void *start, void *end)
382 #if defined(__arm__) || defined(__aarch64__)
383 size_t len = (char *)end - (char *)start;
384 #if defined(__BLACKBERRY_QNX__)
385 msync(start, len, MS_SYNC | MS_CACHE_ONLY | MS_INVALIDATE_ICACHE);
386 #elif defined(__MACH__)
387 sys_cache_control(kCacheFunctionPrepareForExecution, start, len);
389 sceKernelSyncVMDomain(sceBlock, start, len);
391 ctr_flush_invalidate_cache();
392 #elif defined(__aarch64__)
393 // as of 2021, __clear_cache() is still broken on arm64
394 // so here is a custom one :(
395 clear_cache_arm64(start, end);
397 __clear_cache(start, end);
402 mprotect_w_x(start, end, 1);
405 static void *start_block(void)
407 u_char *end = out + MAX_OUTPUT_BLOCK_SIZE;
408 if (end > ndrc->translation_cache + sizeof(ndrc->translation_cache))
409 end = ndrc->translation_cache + sizeof(ndrc->translation_cache);
410 start_tcache_write(out, end);
414 static void end_block(void *start)
416 end_tcache_write(start, out);
419 // also takes care of w^x mappings when patching code
420 static u_int needs_clear_cache[1<<(TARGET_SIZE_2-17)];
422 static void mark_clear_cache(void *target)
424 uintptr_t offset = (u_char *)target - ndrc->translation_cache;
425 u_int mask = 1u << ((offset >> 12) & 31);
426 if (!(needs_clear_cache[offset >> 17] & mask)) {
427 char *start = (char *)((uintptr_t)target & ~4095l);
428 start_tcache_write(start, start + 4095);
429 needs_clear_cache[offset >> 17] |= mask;
433 // Clearing the cache is rather slow on ARM Linux, so mark the areas
434 // that need to be cleared, and then only clear these areas once.
435 static void do_clear_cache(void)
438 for (i = 0; i < (1<<(TARGET_SIZE_2-17)); i++)
440 u_int bitmap = needs_clear_cache[i];
443 for (j = 0; j < 32; j++)
446 if (!(bitmap & (1<<j)))
449 start = ndrc->translation_cache + i*131072 + j*4096;
451 for (j++; j < 32; j++) {
452 if (!(bitmap & (1<<j)))
456 end_tcache_write(start, end);
458 needs_clear_cache[i] = 0;
462 //#define DEBUG_CYCLE_COUNT 1
464 #define NO_CYCLE_PENALTY_THR 12
466 int cycle_multiplier; // 100 for 1.0
467 int cycle_multiplier_override;
468 int cycle_multiplier_old;
470 static int CLOCK_ADJUST(int x)
472 int m = cycle_multiplier_override
473 ? cycle_multiplier_override : cycle_multiplier;
475 return (x * m + s * 50) / 100;
478 // is the op an unconditional jump?
479 static int is_ujump(int i)
481 return dops[i].itype == UJUMP || dops[i].itype == RJUMP
482 || (source[i] >> 16) == 0x1000; // beq r0, r0, offset // b offset
485 static int is_jump(int i)
487 return dops[i].itype == RJUMP || dops[i].itype == UJUMP || dops[i].itype == CJUMP || dops[i].itype == SJUMP;
490 static int ds_writes_rjump_rs(int i)
492 return dops[i].rs1 != 0 && (dops[i].rs1 == dops[i+1].rt1 || dops[i].rs1 == dops[i+1].rt2);
495 static u_int get_page(u_int vaddr)
497 u_int page=vaddr&~0xe0000000;
498 if (page < 0x1000000)
499 page &= ~0x0e00000; // RAM mirrors
501 if(page>2048) page=2048+(page&2047);
505 // no virtual mem in PCSX
506 static u_int get_vpage(u_int vaddr)
508 return get_page(vaddr);
511 static struct ht_entry *hash_table_get(u_int vaddr)
513 return &hash_table[((vaddr>>16)^vaddr)&0xFFFF];
516 static void hash_table_add(struct ht_entry *ht_bin, u_int vaddr, void *tcaddr)
518 ht_bin->vaddr[1] = ht_bin->vaddr[0];
519 ht_bin->tcaddr[1] = ht_bin->tcaddr[0];
520 ht_bin->vaddr[0] = vaddr;
521 ht_bin->tcaddr[0] = tcaddr;
524 // some messy ari64's code, seems to rely on unsigned 32bit overflow
525 static int doesnt_expire_soon(void *tcaddr)
527 u_int diff = (u_int)((u_char *)tcaddr - out) << (32-TARGET_SIZE_2);
528 return diff > (u_int)(0x60000000 + (MAX_OUTPUT_BLOCK_SIZE << (32-TARGET_SIZE_2)));
531 // Get address from virtual address
532 // This is called from the recompiled JR/JALR instructions
533 void noinline *get_addr(u_int vaddr)
535 u_int page=get_page(vaddr);
536 u_int vpage=get_vpage(vaddr);
537 struct ll_entry *head;
538 //printf("TRACE: count=%d next=%d (get_addr %x,page %d)\n",Count,next_interupt,vaddr,page);
541 if(head->vaddr==vaddr) {
542 //printf("TRACE: count=%d next=%d (get_addr match %x: %p)\n",Count,next_interupt,vaddr,head->addr);
543 hash_table_add(hash_table_get(vaddr), vaddr, head->addr);
548 head=jump_dirty[vpage];
550 if(head->vaddr==vaddr) {
551 //printf("TRACE: count=%d next=%d (get_addr match dirty %x: %p)\n",Count,next_interupt,vaddr,head->addr);
552 // Don't restore blocks which are about to expire from the cache
553 if (doesnt_expire_soon(head->addr))
554 if (verify_dirty(head->addr)) {
555 //printf("restore candidate: %x (%d) d=%d\n",vaddr,page,invalid_code[vaddr>>12]);
556 invalid_code[vaddr>>12]=0;
557 inv_code_start=inv_code_end=~0;
559 restore_candidate[vpage>>3]|=1<<(vpage&7);
561 else restore_candidate[page>>3]|=1<<(page&7);
562 struct ht_entry *ht_bin = hash_table_get(vaddr);
563 if (ht_bin->vaddr[0] == vaddr)
564 ht_bin->tcaddr[0] = head->addr; // Replace existing entry
566 hash_table_add(ht_bin, vaddr, head->addr);
573 //printf("TRACE: count=%d next=%d (get_addr no-match %x)\n",Count,next_interupt,vaddr);
574 int r=new_recompile_block(vaddr);
575 if(r==0) return get_addr(vaddr);
576 // Execute in unmapped page, generate pagefault execption
578 Cause=(vaddr<<31)|0x8;
579 EPC=(vaddr&1)?vaddr-5:vaddr;
581 Context=(Context&0xFF80000F)|((BadVAddr>>9)&0x007FFFF0);
582 EntryHi=BadVAddr&0xFFFFE000;
583 return get_addr_ht(0x80000000);
585 // Look up address in hash table first
586 void *get_addr_ht(u_int vaddr)
588 //printf("TRACE: count=%d next=%d (get_addr_ht %x)\n",Count,next_interupt,vaddr);
589 const struct ht_entry *ht_bin = hash_table_get(vaddr);
590 if (ht_bin->vaddr[0] == vaddr) return ht_bin->tcaddr[0];
591 if (ht_bin->vaddr[1] == vaddr) return ht_bin->tcaddr[1];
592 return get_addr(vaddr);
595 void clear_all_regs(signed char regmap[])
598 for (hr=0;hr<HOST_REGS;hr++) regmap[hr]=-1;
601 static signed char get_reg(const signed char regmap[],int r)
604 for (hr=0;hr<HOST_REGS;hr++) if(hr!=EXCLUDE_REG&®map[hr]==r) return hr;
608 // Find a register that is available for two consecutive cycles
609 static signed char get_reg2(signed char regmap1[], const signed char regmap2[], int r)
612 for (hr=0;hr<HOST_REGS;hr++) if(hr!=EXCLUDE_REG&®map1[hr]==r&®map2[hr]==r) return hr;
616 int count_free_regs(signed char regmap[])
620 for(hr=0;hr<HOST_REGS;hr++)
622 if(hr!=EXCLUDE_REG) {
623 if(regmap[hr]<0) count++;
629 void dirty_reg(struct regstat *cur,signed char reg)
633 for (hr=0;hr<HOST_REGS;hr++) {
634 if((cur->regmap[hr]&63)==reg) {
640 static void set_const(struct regstat *cur, signed char reg, uint32_t value)
644 for (hr=0;hr<HOST_REGS;hr++) {
645 if(cur->regmap[hr]==reg) {
647 current_constmap[hr]=value;
652 static void clear_const(struct regstat *cur, signed char reg)
656 for (hr=0;hr<HOST_REGS;hr++) {
657 if((cur->regmap[hr]&63)==reg) {
658 cur->isconst&=~(1<<hr);
663 static int is_const(struct regstat *cur, signed char reg)
668 for (hr=0;hr<HOST_REGS;hr++) {
669 if((cur->regmap[hr]&63)==reg) {
670 return (cur->isconst>>hr)&1;
676 static uint32_t get_const(struct regstat *cur, signed char reg)
680 for (hr=0;hr<HOST_REGS;hr++) {
681 if(cur->regmap[hr]==reg) {
682 return current_constmap[hr];
685 SysPrintf("Unknown constant in r%d\n",reg);
689 // Least soon needed registers
690 // Look at the next ten instructions and see which registers
691 // will be used. Try not to reallocate these.
692 void lsn(u_char hsn[], int i, int *preferred_reg)
704 // Don't go past an unconditonal jump
711 if(dops[i+j].rs1) hsn[dops[i+j].rs1]=j;
712 if(dops[i+j].rs2) hsn[dops[i+j].rs2]=j;
713 if(dops[i+j].rt1) hsn[dops[i+j].rt1]=j;
714 if(dops[i+j].rt2) hsn[dops[i+j].rt2]=j;
715 if(dops[i+j].itype==STORE || dops[i+j].itype==STORELR) {
716 // Stores can allocate zero
717 hsn[dops[i+j].rs1]=j;
718 hsn[dops[i+j].rs2]=j;
720 // On some architectures stores need invc_ptr
721 #if defined(HOST_IMM8)
722 if(dops[i+j].itype==STORE || dops[i+j].itype==STORELR || (dops[i+j].opcode&0x3b)==0x39 || (dops[i+j].opcode&0x3b)==0x3a) {
726 if(i+j>=0&&(dops[i+j].itype==UJUMP||dops[i+j].itype==CJUMP||dops[i+j].itype==SJUMP))
734 if(ba[i+b]>=start && ba[i+b]<(start+slen*4))
736 // Follow first branch
737 int t=(ba[i+b]-start)>>2;
738 j=7-b;if(t+j>=slen) j=slen-t-1;
741 if(dops[t+j].rs1) if(hsn[dops[t+j].rs1]>j+b+2) hsn[dops[t+j].rs1]=j+b+2;
742 if(dops[t+j].rs2) if(hsn[dops[t+j].rs2]>j+b+2) hsn[dops[t+j].rs2]=j+b+2;
743 //if(dops[t+j].rt1) if(hsn[dops[t+j].rt1]>j+b+2) hsn[dops[t+j].rt1]=j+b+2;
744 //if(dops[t+j].rt2) if(hsn[dops[t+j].rt2]>j+b+2) hsn[dops[t+j].rt2]=j+b+2;
747 // TODO: preferred register based on backward branch
749 // Delay slot should preferably not overwrite branch conditions or cycle count
750 if (i > 0 && is_jump(i-1)) {
751 if(dops[i-1].rs1) if(hsn[dops[i-1].rs1]>1) hsn[dops[i-1].rs1]=1;
752 if(dops[i-1].rs2) if(hsn[dops[i-1].rs2]>1) hsn[dops[i-1].rs2]=1;
758 // Coprocessor load/store needs FTEMP, even if not declared
759 if(dops[i].itype==C1LS||dops[i].itype==C2LS) {
762 // Load L/R also uses FTEMP as a temporary register
763 if(dops[i].itype==LOADLR) {
766 // Also SWL/SWR/SDL/SDR
767 if(dops[i].opcode==0x2a||dops[i].opcode==0x2e||dops[i].opcode==0x2c||dops[i].opcode==0x2d) {
770 // Don't remove the miniht registers
771 if(dops[i].itype==UJUMP||dops[i].itype==RJUMP)
778 // We only want to allocate registers if we're going to use them again soon
779 int needed_again(int r, int i)
785 if (i > 0 && is_ujump(i-1))
787 if(ba[i-1]<start || ba[i-1]>start+slen*4-4)
788 return 0; // Don't need any registers if exiting the block
798 // Don't go past an unconditonal jump
802 if(dops[i+j].itype==SYSCALL||dops[i+j].itype==HLECALL||dops[i+j].itype==INTCALL||((source[i+j]&0xfc00003f)==0x0d))
809 if(dops[i+j].rs1==r) rn=j;
810 if(dops[i+j].rs2==r) rn=j;
811 if((unneeded_reg[i+j]>>r)&1) rn=10;
812 if(i+j>=0&&(dops[i+j].itype==UJUMP||dops[i+j].itype==CJUMP||dops[i+j].itype==SJUMP))
822 // Try to match register allocations at the end of a loop with those
824 int loop_reg(int i, int r, int hr)
835 // Don't go past an unconditonal jump
842 if(dops[i-1].itype==UJUMP||dops[i-1].itype==CJUMP||dops[i-1].itype==SJUMP)
848 if((unneeded_reg[i+k]>>r)&1) return hr;
849 if(i+k>=0&&(dops[i+k].itype==UJUMP||dops[i+k].itype==CJUMP||dops[i+k].itype==SJUMP))
851 if(ba[i+k]>=start && ba[i+k]<(start+i*4))
853 int t=(ba[i+k]-start)>>2;
854 int reg=get_reg(regs[t].regmap_entry,r);
855 if(reg>=0) return reg;
856 //reg=get_reg(regs[t+1].regmap_entry,r);
857 //if(reg>=0) return reg;
865 // Allocate every register, preserving source/target regs
866 void alloc_all(struct regstat *cur,int i)
870 for(hr=0;hr<HOST_REGS;hr++) {
871 if(hr!=EXCLUDE_REG) {
872 if(((cur->regmap[hr]&63)!=dops[i].rs1)&&((cur->regmap[hr]&63)!=dops[i].rs2)&&
873 ((cur->regmap[hr]&63)!=dops[i].rt1)&&((cur->regmap[hr]&63)!=dops[i].rt2))
876 cur->dirty&=~(1<<hr);
879 if((cur->regmap[hr]&63)==0)
882 cur->dirty&=~(1<<hr);
889 static int host_tempreg_in_use;
891 static void host_tempreg_acquire(void)
893 assert(!host_tempreg_in_use);
894 host_tempreg_in_use = 1;
897 static void host_tempreg_release(void)
899 host_tempreg_in_use = 0;
902 static void host_tempreg_acquire(void) {}
903 static void host_tempreg_release(void) {}
907 extern void gen_interupt();
908 extern void do_insn_cmp();
909 #define FUNCNAME(f) { f, " " #f }
910 static const struct {
913 } function_names[] = {
914 FUNCNAME(cc_interrupt),
915 FUNCNAME(gen_interupt),
916 FUNCNAME(get_addr_ht),
918 FUNCNAME(jump_handler_read8),
919 FUNCNAME(jump_handler_read16),
920 FUNCNAME(jump_handler_read32),
921 FUNCNAME(jump_handler_write8),
922 FUNCNAME(jump_handler_write16),
923 FUNCNAME(jump_handler_write32),
924 FUNCNAME(invalidate_addr),
925 FUNCNAME(jump_to_new_pc),
926 FUNCNAME(call_gteStall),
927 FUNCNAME(new_dyna_leave),
929 FUNCNAME(pcsx_mtc0_ds),
931 FUNCNAME(do_insn_cmp),
934 FUNCNAME(verify_code),
938 static const char *func_name(const void *a)
941 for (i = 0; i < sizeof(function_names)/sizeof(function_names[0]); i++)
942 if (function_names[i].addr == a)
943 return function_names[i].name;
947 #define func_name(x) ""
951 #include "assem_x86.c"
954 #include "assem_x64.c"
957 #include "assem_arm.c"
960 #include "assem_arm64.c"
963 static void *get_trampoline(const void *f)
967 for (i = 0; i < ARRAY_SIZE(ndrc->tramp.f); i++) {
968 if (ndrc->tramp.f[i] == f || ndrc->tramp.f[i] == NULL)
971 if (i == ARRAY_SIZE(ndrc->tramp.f)) {
972 SysPrintf("trampoline table is full, last func %p\n", f);
975 if (ndrc->tramp.f[i] == NULL) {
976 start_tcache_write(&ndrc->tramp.f[i], &ndrc->tramp.f[i + 1]);
977 ndrc->tramp.f[i] = f;
978 end_tcache_write(&ndrc->tramp.f[i], &ndrc->tramp.f[i + 1]);
980 return &ndrc->tramp.ops[i];
983 static void emit_far_jump(const void *f)
985 if (can_jump_or_call(f)) {
990 f = get_trampoline(f);
994 static void emit_far_call(const void *f)
996 if (can_jump_or_call(f)) {
1001 f = get_trampoline(f);
1005 // Add virtual address mapping to linked list
1006 void ll_add(struct ll_entry **head,int vaddr,void *addr)
1008 struct ll_entry *new_entry;
1009 new_entry=malloc(sizeof(struct ll_entry));
1010 assert(new_entry!=NULL);
1011 new_entry->vaddr=vaddr;
1012 new_entry->reg_sv_flags=0;
1013 new_entry->addr=addr;
1014 new_entry->next=*head;
1018 void ll_add_flags(struct ll_entry **head,int vaddr,u_int reg_sv_flags,void *addr)
1020 ll_add(head,vaddr,addr);
1021 (*head)->reg_sv_flags=reg_sv_flags;
1024 // Check if an address is already compiled
1025 // but don't return addresses which are about to expire from the cache
1026 void *check_addr(u_int vaddr)
1028 struct ht_entry *ht_bin = hash_table_get(vaddr);
1030 for (i = 0; i < ARRAY_SIZE(ht_bin->vaddr); i++) {
1031 if (ht_bin->vaddr[i] == vaddr)
1032 if (doesnt_expire_soon((u_char *)ht_bin->tcaddr[i] - MAX_OUTPUT_BLOCK_SIZE))
1033 if (isclean(ht_bin->tcaddr[i]))
1034 return ht_bin->tcaddr[i];
1036 u_int page=get_page(vaddr);
1037 struct ll_entry *head;
1039 while (head != NULL) {
1040 if (head->vaddr == vaddr) {
1041 if (doesnt_expire_soon(head->addr)) {
1042 // Update existing entry with current address
1043 if (ht_bin->vaddr[0] == vaddr) {
1044 ht_bin->tcaddr[0] = head->addr;
1047 if (ht_bin->vaddr[1] == vaddr) {
1048 ht_bin->tcaddr[1] = head->addr;
1051 // Insert into hash table with low priority.
1052 // Don't evict existing entries, as they are probably
1053 // addresses that are being accessed frequently.
1054 if (ht_bin->vaddr[0] == -1) {
1055 ht_bin->vaddr[0] = vaddr;
1056 ht_bin->tcaddr[0] = head->addr;
1058 else if (ht_bin->vaddr[1] == -1) {
1059 ht_bin->vaddr[1] = vaddr;
1060 ht_bin->tcaddr[1] = head->addr;
1070 void remove_hash(int vaddr)
1072 //printf("remove hash: %x\n",vaddr);
1073 struct ht_entry *ht_bin = hash_table_get(vaddr);
1074 if (ht_bin->vaddr[1] == vaddr) {
1075 ht_bin->vaddr[1] = -1;
1076 ht_bin->tcaddr[1] = NULL;
1078 if (ht_bin->vaddr[0] == vaddr) {
1079 ht_bin->vaddr[0] = ht_bin->vaddr[1];
1080 ht_bin->tcaddr[0] = ht_bin->tcaddr[1];
1081 ht_bin->vaddr[1] = -1;
1082 ht_bin->tcaddr[1] = NULL;
1086 static void ll_remove_matching_addrs(struct ll_entry **head,
1087 uintptr_t base_offs_s, int shift)
1089 struct ll_entry *next;
1091 uintptr_t o1 = (u_char *)(*head)->addr - ndrc->translation_cache;
1092 uintptr_t o2 = o1 - MAX_OUTPUT_BLOCK_SIZE;
1093 if ((o1 >> shift) == base_offs_s || (o2 >> shift) == base_offs_s)
1095 inv_debug("EXP: Remove pointer to %p (%x)\n",(*head)->addr,(*head)->vaddr);
1096 remove_hash((*head)->vaddr);
1103 head=&((*head)->next);
1108 // Remove all entries from linked list
1109 void ll_clear(struct ll_entry **head)
1111 struct ll_entry *cur;
1112 struct ll_entry *next;
1123 // Dereference the pointers and remove if it matches
1124 static void ll_kill_pointers(struct ll_entry *head,
1125 uintptr_t base_offs_s, int shift)
1128 u_char *ptr = get_pointer(head->addr);
1129 uintptr_t o1 = ptr - ndrc->translation_cache;
1130 uintptr_t o2 = o1 - MAX_OUTPUT_BLOCK_SIZE;
1131 inv_debug("EXP: Lookup pointer to %p at %p (%x)\n",ptr,head->addr,head->vaddr);
1132 if ((o1 >> shift) == base_offs_s || (o2 >> shift) == base_offs_s)
1134 inv_debug("EXP: Kill pointer at %p (%x)\n",head->addr,head->vaddr);
1135 void *host_addr=find_extjump_insn(head->addr);
1136 mark_clear_cache(host_addr);
1137 set_jump_target(host_addr, head->addr);
1143 // This is called when we write to a compiled block (see do_invstub)
1144 static void invalidate_page(u_int page)
1146 struct ll_entry *head;
1147 struct ll_entry *next;
1151 inv_debug("INVALIDATE: %x\n",head->vaddr);
1152 remove_hash(head->vaddr);
1157 head=jump_out[page];
1160 inv_debug("INVALIDATE: kill pointer to %x (%p)\n",head->vaddr,head->addr);
1161 void *host_addr=find_extjump_insn(head->addr);
1162 mark_clear_cache(host_addr);
1163 set_jump_target(host_addr, head->addr); // point back to dyna_linker
1170 static void invalidate_block_range(u_int block, u_int first, u_int last)
1172 u_int page=get_page(block<<12);
1173 //printf("first=%d last=%d\n",first,last);
1174 invalidate_page(page);
1175 assert(first+5>page); // NB: this assumes MAXBLOCK<=4096 (4 pages)
1176 assert(last<page+5);
1177 // Invalidate the adjacent pages if a block crosses a 4K boundary
1179 invalidate_page(first);
1182 for(first=page+1;first<last;first++) {
1183 invalidate_page(first);
1187 // Don't trap writes
1188 invalid_code[block]=1;
1191 memset(mini_ht,-1,sizeof(mini_ht));
1195 void invalidate_block(u_int block)
1197 u_int page=get_page(block<<12);
1198 u_int vpage=get_vpage(block<<12);
1199 inv_debug("INVALIDATE: %x (%d)\n",block<<12,page);
1200 //inv_debug("invalid_code[block]=%d\n",invalid_code[block]);
1203 struct ll_entry *head;
1204 head=jump_dirty[vpage];
1205 //printf("page=%d vpage=%d\n",page,vpage);
1207 if(vpage>2047||(head->vaddr>>12)==block) { // Ignore vaddr hash collision
1208 u_char *start, *end;
1209 get_bounds(head->addr, &start, &end);
1210 //printf("start: %p end: %p\n", start, end);
1211 if (page < 2048 && start >= rdram && end < rdram+RAM_SIZE) {
1212 if (((start-rdram)>>12) <= page && ((end-1-rdram)>>12) >= page) {
1213 if ((((start-rdram)>>12)&2047) < first) first = ((start-rdram)>>12)&2047;
1214 if ((((end-1-rdram)>>12)&2047) > last) last = ((end-1-rdram)>>12)&2047;
1220 invalidate_block_range(block,first,last);
1223 void invalidate_addr(u_int addr)
1226 // this check is done by the caller
1227 //if (inv_code_start<=addr&&addr<=inv_code_end) { rhits++; return; }
1228 u_int page=get_vpage(addr);
1229 if(page<2048) { // RAM
1230 struct ll_entry *head;
1231 u_int addr_min=~0, addr_max=0;
1232 u_int mask=RAM_SIZE-1;
1233 u_int addr_main=0x80000000|(addr&mask);
1235 inv_code_start=addr_main&~0xfff;
1236 inv_code_end=addr_main|0xfff;
1239 // must check previous page too because of spans..
1241 inv_code_start-=0x1000;
1243 for(;pg1<=page;pg1++) {
1244 for(head=jump_dirty[pg1];head!=NULL;head=head->next) {
1245 u_char *start_h, *end_h;
1247 get_bounds(head->addr, &start_h, &end_h);
1248 start = (uintptr_t)start_h - ram_offset;
1249 end = (uintptr_t)end_h - ram_offset;
1250 if(start<=addr_main&&addr_main<end) {
1251 if(start<addr_min) addr_min=start;
1252 if(end>addr_max) addr_max=end;
1254 else if(addr_main<start) {
1255 if(start<inv_code_end)
1256 inv_code_end=start-1;
1259 if(end>inv_code_start)
1265 inv_debug("INV ADDR: %08x hit %08x-%08x\n", addr, addr_min, addr_max);
1266 inv_code_start=inv_code_end=~0;
1267 invalidate_block_range(addr>>12,(addr_min&mask)>>12,(addr_max&mask)>>12);
1271 inv_code_start=(addr&~mask)|(inv_code_start&mask);
1272 inv_code_end=(addr&~mask)|(inv_code_end&mask);
1273 inv_debug("INV ADDR: %08x miss, inv %08x-%08x, sk %d\n", addr, inv_code_start, inv_code_end, 0);
1277 invalidate_block(addr>>12);
1280 // This is called when loading a save state.
1281 // Anything could have changed, so invalidate everything.
1282 void invalidate_all_pages(void)
1285 for(page=0;page<4096;page++)
1286 invalidate_page(page);
1287 for(page=0;page<1048576;page++)
1288 if(!invalid_code[page]) {
1289 restore_candidate[(page&2047)>>3]|=1<<(page&7);
1290 restore_candidate[((page&2047)>>3)+256]|=1<<(page&7);
1293 memset(mini_ht,-1,sizeof(mini_ht));
1298 static void do_invstub(int n)
1301 u_int reglist=stubs[n].a;
1302 set_jump_target(stubs[n].addr, out);
1304 if(stubs[n].b!=0) emit_mov(stubs[n].b,0);
1305 emit_far_call(invalidate_addr);
1306 restore_regs(reglist);
1307 emit_jmp(stubs[n].retaddr); // return address
1310 // Add an entry to jump_out after making a link
1311 // src should point to code by emit_extjump2()
1312 void add_jump_out(u_int vaddr,void *src)
1314 u_int page=get_page(vaddr);
1315 inv_debug("add_jump_out: %p -> %x (%d)\n",src,vaddr,page);
1316 check_extjump2(src);
1317 ll_add(jump_out+page,vaddr,src);
1318 //inv_debug("add_jump_out: to %p\n",get_pointer(src));
1321 // If a code block was found to be unmodified (bit was set in
1322 // restore_candidate) and it remains unmodified (bit is clear
1323 // in invalid_code) then move the entries for that 4K page from
1324 // the dirty list to the clean list.
1325 void clean_blocks(u_int page)
1327 struct ll_entry *head;
1328 inv_debug("INV: clean_blocks page=%d\n",page);
1329 head=jump_dirty[page];
1331 if(!invalid_code[head->vaddr>>12]) {
1332 // Don't restore blocks which are about to expire from the cache
1333 if (doesnt_expire_soon(head->addr)) {
1334 if(verify_dirty(head->addr)) {
1335 u_char *start, *end;
1336 //printf("Possibly Restore %x (%p)\n",head->vaddr, head->addr);
1339 get_bounds(head->addr, &start, &end);
1340 if (start - rdram < RAM_SIZE) {
1341 for (i = (start-rdram+0x80000000)>>12; i <= (end-1-rdram+0x80000000)>>12; i++) {
1342 inv|=invalid_code[i];
1345 else if((signed int)head->vaddr>=(signed int)0x80000000+RAM_SIZE) {
1349 void *clean_addr = get_clean_addr(head->addr);
1350 if (doesnt_expire_soon(clean_addr)) {
1352 inv_debug("INV: Restored %x (%p/%p)\n",head->vaddr, head->addr, clean_addr);
1353 //printf("page=%x, addr=%x\n",page,head->vaddr);
1354 //assert(head->vaddr>>12==(page|0x80000));
1355 ll_add_flags(jump_in+ppage,head->vaddr,head->reg_sv_flags,clean_addr);
1356 struct ht_entry *ht_bin = hash_table_get(head->vaddr);
1357 if (ht_bin->vaddr[0] == head->vaddr)
1358 ht_bin->tcaddr[0] = clean_addr; // Replace existing entry
1359 if (ht_bin->vaddr[1] == head->vaddr)
1360 ht_bin->tcaddr[1] = clean_addr; // Replace existing entry
1370 /* Register allocation */
1372 // Note: registers are allocated clean (unmodified state)
1373 // if you intend to modify the register, you must call dirty_reg().
1374 static void alloc_reg(struct regstat *cur,int i,signed char reg)
1377 int preferred_reg = (reg&7);
1378 if(reg==CCREG) preferred_reg=HOST_CCREG;
1379 if(reg==PTEMP||reg==FTEMP) preferred_reg=12;
1381 // Don't allocate unused registers
1382 if((cur->u>>reg)&1) return;
1384 // see if it's already allocated
1385 for(hr=0;hr<HOST_REGS;hr++)
1387 if(cur->regmap[hr]==reg) return;
1390 // Keep the same mapping if the register was already allocated in a loop
1391 preferred_reg = loop_reg(i,reg,preferred_reg);
1393 // Try to allocate the preferred register
1394 if(cur->regmap[preferred_reg]==-1) {
1395 cur->regmap[preferred_reg]=reg;
1396 cur->dirty&=~(1<<preferred_reg);
1397 cur->isconst&=~(1<<preferred_reg);
1400 r=cur->regmap[preferred_reg];
1403 cur->regmap[preferred_reg]=reg;
1404 cur->dirty&=~(1<<preferred_reg);
1405 cur->isconst&=~(1<<preferred_reg);
1409 // Clear any unneeded registers
1410 // We try to keep the mapping consistent, if possible, because it
1411 // makes branches easier (especially loops). So we try to allocate
1412 // first (see above) before removing old mappings. If this is not
1413 // possible then go ahead and clear out the registers that are no
1415 for(hr=0;hr<HOST_REGS;hr++)
1420 if((cur->u>>r)&1) {cur->regmap[hr]=-1;break;}
1423 // Try to allocate any available register, but prefer
1424 // registers that have not been used recently.
1426 for(hr=0;hr<HOST_REGS;hr++) {
1427 if(hr!=EXCLUDE_REG&&cur->regmap[hr]==-1) {
1428 if(regs[i-1].regmap[hr]!=dops[i-1].rs1&®s[i-1].regmap[hr]!=dops[i-1].rs2&®s[i-1].regmap[hr]!=dops[i-1].rt1&®s[i-1].regmap[hr]!=dops[i-1].rt2) {
1429 cur->regmap[hr]=reg;
1430 cur->dirty&=~(1<<hr);
1431 cur->isconst&=~(1<<hr);
1437 // Try to allocate any available register
1438 for(hr=0;hr<HOST_REGS;hr++) {
1439 if(hr!=EXCLUDE_REG&&cur->regmap[hr]==-1) {
1440 cur->regmap[hr]=reg;
1441 cur->dirty&=~(1<<hr);
1442 cur->isconst&=~(1<<hr);
1447 // Ok, now we have to evict someone
1448 // Pick a register we hopefully won't need soon
1449 u_char hsn[MAXREG+1];
1450 memset(hsn,10,sizeof(hsn));
1452 lsn(hsn,i,&preferred_reg);
1453 //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]);
1454 //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]);
1456 // Don't evict the cycle count at entry points, otherwise the entry
1457 // stub will have to write it.
1458 if(dops[i].bt&&hsn[CCREG]>2) hsn[CCREG]=2;
1459 if(i>1&&hsn[CCREG]>2&&(dops[i-2].itype==RJUMP||dops[i-2].itype==UJUMP||dops[i-2].itype==CJUMP||dops[i-2].itype==SJUMP)) hsn[CCREG]=2;
1462 // Alloc preferred register if available
1463 if(hsn[r=cur->regmap[preferred_reg]&63]==j) {
1464 for(hr=0;hr<HOST_REGS;hr++) {
1465 // Evict both parts of a 64-bit register
1466 if((cur->regmap[hr]&63)==r) {
1468 cur->dirty&=~(1<<hr);
1469 cur->isconst&=~(1<<hr);
1472 cur->regmap[preferred_reg]=reg;
1475 for(r=1;r<=MAXREG;r++)
1477 if(hsn[r]==j&&r!=dops[i-1].rs1&&r!=dops[i-1].rs2&&r!=dops[i-1].rt1&&r!=dops[i-1].rt2) {
1478 for(hr=0;hr<HOST_REGS;hr++) {
1479 if(hr!=HOST_CCREG||j<hsn[CCREG]) {
1480 if(cur->regmap[hr]==r) {
1481 cur->regmap[hr]=reg;
1482 cur->dirty&=~(1<<hr);
1483 cur->isconst&=~(1<<hr);
1494 for(r=1;r<=MAXREG;r++)
1497 for(hr=0;hr<HOST_REGS;hr++) {
1498 if(cur->regmap[hr]==r) {
1499 cur->regmap[hr]=reg;
1500 cur->dirty&=~(1<<hr);
1501 cur->isconst&=~(1<<hr);
1508 SysPrintf("This shouldn't happen (alloc_reg)");abort();
1511 // Allocate a temporary register. This is done without regard to
1512 // dirty status or whether the register we request is on the unneeded list
1513 // Note: This will only allocate one register, even if called multiple times
1514 static void alloc_reg_temp(struct regstat *cur,int i,signed char reg)
1517 int preferred_reg = -1;
1519 // see if it's already allocated
1520 for(hr=0;hr<HOST_REGS;hr++)
1522 if(hr!=EXCLUDE_REG&&cur->regmap[hr]==reg) return;
1525 // Try to allocate any available register
1526 for(hr=HOST_REGS-1;hr>=0;hr--) {
1527 if(hr!=EXCLUDE_REG&&cur->regmap[hr]==-1) {
1528 cur->regmap[hr]=reg;
1529 cur->dirty&=~(1<<hr);
1530 cur->isconst&=~(1<<hr);
1535 // Find an unneeded register
1536 for(hr=HOST_REGS-1;hr>=0;hr--)
1542 if(i==0||((unneeded_reg[i-1]>>r)&1)) {
1543 cur->regmap[hr]=reg;
1544 cur->dirty&=~(1<<hr);
1545 cur->isconst&=~(1<<hr);
1552 // Ok, now we have to evict someone
1553 // Pick a register we hopefully won't need soon
1554 // TODO: we might want to follow unconditional jumps here
1555 // TODO: get rid of dupe code and make this into a function
1556 u_char hsn[MAXREG+1];
1557 memset(hsn,10,sizeof(hsn));
1559 lsn(hsn,i,&preferred_reg);
1560 //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]);
1562 // Don't evict the cycle count at entry points, otherwise the entry
1563 // stub will have to write it.
1564 if(dops[i].bt&&hsn[CCREG]>2) hsn[CCREG]=2;
1565 if(i>1&&hsn[CCREG]>2&&(dops[i-2].itype==RJUMP||dops[i-2].itype==UJUMP||dops[i-2].itype==CJUMP||dops[i-2].itype==SJUMP)) hsn[CCREG]=2;
1568 for(r=1;r<=MAXREG;r++)
1570 if(hsn[r]==j&&r!=dops[i-1].rs1&&r!=dops[i-1].rs2&&r!=dops[i-1].rt1&&r!=dops[i-1].rt2) {
1571 for(hr=0;hr<HOST_REGS;hr++) {
1572 if(hr!=HOST_CCREG||hsn[CCREG]>2) {
1573 if(cur->regmap[hr]==r) {
1574 cur->regmap[hr]=reg;
1575 cur->dirty&=~(1<<hr);
1576 cur->isconst&=~(1<<hr);
1587 for(r=1;r<=MAXREG;r++)
1590 for(hr=0;hr<HOST_REGS;hr++) {
1591 if(cur->regmap[hr]==r) {
1592 cur->regmap[hr]=reg;
1593 cur->dirty&=~(1<<hr);
1594 cur->isconst&=~(1<<hr);
1601 SysPrintf("This shouldn't happen");abort();
1604 static void mov_alloc(struct regstat *current,int i)
1606 if (dops[i].rs1 == HIREG || dops[i].rs1 == LOREG) {
1607 // logically this is needed but just won't work, no idea why
1608 //alloc_cc(current,i); // for stalls
1609 //dirty_reg(current,CCREG);
1612 // Note: Don't need to actually alloc the source registers
1613 //alloc_reg(current,i,dops[i].rs1);
1614 alloc_reg(current,i,dops[i].rt1);
1616 clear_const(current,dops[i].rs1);
1617 clear_const(current,dops[i].rt1);
1618 dirty_reg(current,dops[i].rt1);
1621 static void shiftimm_alloc(struct regstat *current,int i)
1623 if(dops[i].opcode2<=0x3) // SLL/SRL/SRA
1626 if(dops[i].rs1&&needed_again(dops[i].rs1,i)) alloc_reg(current,i,dops[i].rs1);
1627 else dops[i].lt1=dops[i].rs1;
1628 alloc_reg(current,i,dops[i].rt1);
1629 dirty_reg(current,dops[i].rt1);
1630 if(is_const(current,dops[i].rs1)) {
1631 int v=get_const(current,dops[i].rs1);
1632 if(dops[i].opcode2==0x00) set_const(current,dops[i].rt1,v<<imm[i]);
1633 if(dops[i].opcode2==0x02) set_const(current,dops[i].rt1,(u_int)v>>imm[i]);
1634 if(dops[i].opcode2==0x03) set_const(current,dops[i].rt1,v>>imm[i]);
1636 else clear_const(current,dops[i].rt1);
1641 clear_const(current,dops[i].rs1);
1642 clear_const(current,dops[i].rt1);
1645 if(dops[i].opcode2>=0x38&&dops[i].opcode2<=0x3b) // DSLL/DSRL/DSRA
1649 if(dops[i].opcode2==0x3c) // DSLL32
1653 if(dops[i].opcode2==0x3e) // DSRL32
1657 if(dops[i].opcode2==0x3f) // DSRA32
1663 static void shift_alloc(struct regstat *current,int i)
1666 if(dops[i].opcode2<=0x07) // SLLV/SRLV/SRAV
1668 if(dops[i].rs1) alloc_reg(current,i,dops[i].rs1);
1669 if(dops[i].rs2) alloc_reg(current,i,dops[i].rs2);
1670 alloc_reg(current,i,dops[i].rt1);
1671 if(dops[i].rt1==dops[i].rs2) {
1672 alloc_reg_temp(current,i,-1);
1673 minimum_free_regs[i]=1;
1675 } else { // DSLLV/DSRLV/DSRAV
1678 clear_const(current,dops[i].rs1);
1679 clear_const(current,dops[i].rs2);
1680 clear_const(current,dops[i].rt1);
1681 dirty_reg(current,dops[i].rt1);
1685 static void alu_alloc(struct regstat *current,int i)
1687 if(dops[i].opcode2>=0x20&&dops[i].opcode2<=0x23) { // ADD/ADDU/SUB/SUBU
1689 if(dops[i].rs1&&dops[i].rs2) {
1690 alloc_reg(current,i,dops[i].rs1);
1691 alloc_reg(current,i,dops[i].rs2);
1694 if(dops[i].rs1&&needed_again(dops[i].rs1,i)) alloc_reg(current,i,dops[i].rs1);
1695 if(dops[i].rs2&&needed_again(dops[i].rs2,i)) alloc_reg(current,i,dops[i].rs2);
1697 alloc_reg(current,i,dops[i].rt1);
1700 if(dops[i].opcode2==0x2a||dops[i].opcode2==0x2b) { // SLT/SLTU
1702 alloc_reg(current,i,dops[i].rs1);
1703 alloc_reg(current,i,dops[i].rs2);
1704 alloc_reg(current,i,dops[i].rt1);
1707 if(dops[i].opcode2>=0x24&&dops[i].opcode2<=0x27) { // AND/OR/XOR/NOR
1709 if(dops[i].rs1&&dops[i].rs2) {
1710 alloc_reg(current,i,dops[i].rs1);
1711 alloc_reg(current,i,dops[i].rs2);
1715 if(dops[i].rs1&&needed_again(dops[i].rs1,i)) alloc_reg(current,i,dops[i].rs1);
1716 if(dops[i].rs2&&needed_again(dops[i].rs2,i)) alloc_reg(current,i,dops[i].rs2);
1718 alloc_reg(current,i,dops[i].rt1);
1721 if(dops[i].opcode2>=0x2c&&dops[i].opcode2<=0x2f) { // DADD/DADDU/DSUB/DSUBU
1724 clear_const(current,dops[i].rs1);
1725 clear_const(current,dops[i].rs2);
1726 clear_const(current,dops[i].rt1);
1727 dirty_reg(current,dops[i].rt1);
1730 static void imm16_alloc(struct regstat *current,int i)
1732 if(dops[i].rs1&&needed_again(dops[i].rs1,i)) alloc_reg(current,i,dops[i].rs1);
1733 else dops[i].lt1=dops[i].rs1;
1734 if(dops[i].rt1) alloc_reg(current,i,dops[i].rt1);
1735 if(dops[i].opcode==0x18||dops[i].opcode==0x19) { // DADDI/DADDIU
1738 else if(dops[i].opcode==0x0a||dops[i].opcode==0x0b) { // SLTI/SLTIU
1739 clear_const(current,dops[i].rs1);
1740 clear_const(current,dops[i].rt1);
1742 else if(dops[i].opcode>=0x0c&&dops[i].opcode<=0x0e) { // ANDI/ORI/XORI
1743 if(is_const(current,dops[i].rs1)) {
1744 int v=get_const(current,dops[i].rs1);
1745 if(dops[i].opcode==0x0c) set_const(current,dops[i].rt1,v&imm[i]);
1746 if(dops[i].opcode==0x0d) set_const(current,dops[i].rt1,v|imm[i]);
1747 if(dops[i].opcode==0x0e) set_const(current,dops[i].rt1,v^imm[i]);
1749 else clear_const(current,dops[i].rt1);
1751 else if(dops[i].opcode==0x08||dops[i].opcode==0x09) { // ADDI/ADDIU
1752 if(is_const(current,dops[i].rs1)) {
1753 int v=get_const(current,dops[i].rs1);
1754 set_const(current,dops[i].rt1,v+imm[i]);
1756 else clear_const(current,dops[i].rt1);
1759 set_const(current,dops[i].rt1,imm[i]<<16); // LUI
1761 dirty_reg(current,dops[i].rt1);
1764 static void load_alloc(struct regstat *current,int i)
1766 clear_const(current,dops[i].rt1);
1767 //if(dops[i].rs1!=dops[i].rt1&&needed_again(dops[i].rs1,i)) clear_const(current,dops[i].rs1); // Does this help or hurt?
1768 if(!dops[i].rs1) current->u&=~1LL; // Allow allocating r0 if it's the source register
1769 if(needed_again(dops[i].rs1,i)) alloc_reg(current,i,dops[i].rs1);
1770 if(dops[i].rt1&&!((current->u>>dops[i].rt1)&1)) {
1771 alloc_reg(current,i,dops[i].rt1);
1772 assert(get_reg(current->regmap,dops[i].rt1)>=0);
1773 if(dops[i].opcode==0x27||dops[i].opcode==0x37) // LWU/LD
1777 else if(dops[i].opcode==0x1A||dops[i].opcode==0x1B) // LDL/LDR
1781 dirty_reg(current,dops[i].rt1);
1782 // LWL/LWR need a temporary register for the old value
1783 if(dops[i].opcode==0x22||dops[i].opcode==0x26)
1785 alloc_reg(current,i,FTEMP);
1786 alloc_reg_temp(current,i,-1);
1787 minimum_free_regs[i]=1;
1792 // Load to r0 or unneeded register (dummy load)
1793 // but we still need a register to calculate the address
1794 if(dops[i].opcode==0x22||dops[i].opcode==0x26)
1796 alloc_reg(current,i,FTEMP); // LWL/LWR need another temporary
1798 alloc_reg_temp(current,i,-1);
1799 minimum_free_regs[i]=1;
1800 if(dops[i].opcode==0x1A||dops[i].opcode==0x1B) // LDL/LDR
1807 void store_alloc(struct regstat *current,int i)
1809 clear_const(current,dops[i].rs2);
1810 if(!(dops[i].rs2)) current->u&=~1LL; // Allow allocating r0 if necessary
1811 if(needed_again(dops[i].rs1,i)) alloc_reg(current,i,dops[i].rs1);
1812 alloc_reg(current,i,dops[i].rs2);
1813 if(dops[i].opcode==0x2c||dops[i].opcode==0x2d||dops[i].opcode==0x3f) { // 64-bit SDL/SDR/SD
1816 #if defined(HOST_IMM8)
1817 // On CPUs without 32-bit immediates we need a pointer to invalid_code
1818 else alloc_reg(current,i,INVCP);
1820 if(dops[i].opcode==0x2a||dops[i].opcode==0x2e||dops[i].opcode==0x2c||dops[i].opcode==0x2d) { // SWL/SWL/SDL/SDR
1821 alloc_reg(current,i,FTEMP);
1823 // We need a temporary register for address generation
1824 alloc_reg_temp(current,i,-1);
1825 minimum_free_regs[i]=1;
1828 void c1ls_alloc(struct regstat *current,int i)
1830 //clear_const(current,dops[i].rs1); // FIXME
1831 clear_const(current,dops[i].rt1);
1832 if(needed_again(dops[i].rs1,i)) alloc_reg(current,i,dops[i].rs1);
1833 alloc_reg(current,i,CSREG); // Status
1834 alloc_reg(current,i,FTEMP);
1835 if(dops[i].opcode==0x35||dops[i].opcode==0x3d) { // 64-bit LDC1/SDC1
1838 #if defined(HOST_IMM8)
1839 // On CPUs without 32-bit immediates we need a pointer to invalid_code
1840 else if((dops[i].opcode&0x3b)==0x39) // SWC1/SDC1
1841 alloc_reg(current,i,INVCP);
1843 // We need a temporary register for address generation
1844 alloc_reg_temp(current,i,-1);
1847 void c2ls_alloc(struct regstat *current,int i)
1849 clear_const(current,dops[i].rt1);
1850 if(needed_again(dops[i].rs1,i)) alloc_reg(current,i,dops[i].rs1);
1851 alloc_reg(current,i,FTEMP);
1852 #if defined(HOST_IMM8)
1853 // On CPUs without 32-bit immediates we need a pointer to invalid_code
1854 if((dops[i].opcode&0x3b)==0x3a) // SWC2/SDC2
1855 alloc_reg(current,i,INVCP);
1857 // We need a temporary register for address generation
1858 alloc_reg_temp(current,i,-1);
1859 minimum_free_regs[i]=1;
1862 #ifndef multdiv_alloc
1863 void multdiv_alloc(struct regstat *current,int i)
1870 // case 0x1D: DMULTU
1873 clear_const(current,dops[i].rs1);
1874 clear_const(current,dops[i].rs2);
1875 alloc_cc(current,i); // for stalls
1876 if(dops[i].rs1&&dops[i].rs2)
1878 if((dops[i].opcode2&4)==0) // 32-bit
1880 current->u&=~(1LL<<HIREG);
1881 current->u&=~(1LL<<LOREG);
1882 alloc_reg(current,i,HIREG);
1883 alloc_reg(current,i,LOREG);
1884 alloc_reg(current,i,dops[i].rs1);
1885 alloc_reg(current,i,dops[i].rs2);
1886 dirty_reg(current,HIREG);
1887 dirty_reg(current,LOREG);
1896 // Multiply by zero is zero.
1897 // MIPS does not have a divide by zero exception.
1898 // The result is undefined, we return zero.
1899 alloc_reg(current,i,HIREG);
1900 alloc_reg(current,i,LOREG);
1901 dirty_reg(current,HIREG);
1902 dirty_reg(current,LOREG);
1907 void cop0_alloc(struct regstat *current,int i)
1909 if(dops[i].opcode2==0) // MFC0
1912 clear_const(current,dops[i].rt1);
1913 alloc_all(current,i);
1914 alloc_reg(current,i,dops[i].rt1);
1915 dirty_reg(current,dops[i].rt1);
1918 else if(dops[i].opcode2==4) // MTC0
1921 clear_const(current,dops[i].rs1);
1922 alloc_reg(current,i,dops[i].rs1);
1923 alloc_all(current,i);
1926 alloc_all(current,i); // FIXME: Keep r0
1928 alloc_reg(current,i,0);
1933 // TLBR/TLBWI/TLBWR/TLBP/ERET
1934 assert(dops[i].opcode2==0x10);
1935 alloc_all(current,i);
1937 minimum_free_regs[i]=HOST_REGS;
1940 static void cop2_alloc(struct regstat *current,int i)
1942 if (dops[i].opcode2 < 3) // MFC2/CFC2
1944 alloc_cc(current,i); // for stalls
1945 dirty_reg(current,CCREG);
1947 clear_const(current,dops[i].rt1);
1948 alloc_reg(current,i,dops[i].rt1);
1949 dirty_reg(current,dops[i].rt1);
1952 else if (dops[i].opcode2 > 3) // MTC2/CTC2
1955 clear_const(current,dops[i].rs1);
1956 alloc_reg(current,i,dops[i].rs1);
1960 alloc_reg(current,i,0);
1963 alloc_reg_temp(current,i,-1);
1964 minimum_free_regs[i]=1;
1967 void c2op_alloc(struct regstat *current,int i)
1969 alloc_cc(current,i); // for stalls
1970 dirty_reg(current,CCREG);
1971 alloc_reg_temp(current,i,-1);
1974 void syscall_alloc(struct regstat *current,int i)
1976 alloc_cc(current,i);
1977 dirty_reg(current,CCREG);
1978 alloc_all(current,i);
1979 minimum_free_regs[i]=HOST_REGS;
1983 void delayslot_alloc(struct regstat *current,int i)
1985 switch(dops[i].itype) {
1993 assem_debug("jump in the delay slot. this shouldn't happen.\n");//abort();
1994 SysPrintf("Disabled speculative precompilation\n");
1998 imm16_alloc(current,i);
2002 load_alloc(current,i);
2006 store_alloc(current,i);
2009 alu_alloc(current,i);
2012 shift_alloc(current,i);
2015 multdiv_alloc(current,i);
2018 shiftimm_alloc(current,i);
2021 mov_alloc(current,i);
2024 cop0_alloc(current,i);
2029 cop2_alloc(current,i);
2032 c1ls_alloc(current,i);
2035 c2ls_alloc(current,i);
2038 c2op_alloc(current,i);
2043 // Special case where a branch and delay slot span two pages in virtual memory
2044 static void pagespan_alloc(struct regstat *current,int i)
2047 current->wasconst=0;
2049 minimum_free_regs[i]=HOST_REGS;
2050 alloc_all(current,i);
2051 alloc_cc(current,i);
2052 dirty_reg(current,CCREG);
2053 if(dops[i].opcode==3) // JAL
2055 alloc_reg(current,i,31);
2056 dirty_reg(current,31);
2058 if(dops[i].opcode==0&&(dops[i].opcode2&0x3E)==8) // JR/JALR
2060 alloc_reg(current,i,dops[i].rs1);
2061 if (dops[i].rt1!=0) {
2062 alloc_reg(current,i,dops[i].rt1);
2063 dirty_reg(current,dops[i].rt1);
2066 if((dops[i].opcode&0x2E)==4) // BEQ/BNE/BEQL/BNEL
2068 if(dops[i].rs1) alloc_reg(current,i,dops[i].rs1);
2069 if(dops[i].rs2) alloc_reg(current,i,dops[i].rs2);
2072 if((dops[i].opcode&0x2E)==6) // BLEZ/BGTZ/BLEZL/BGTZL
2074 if(dops[i].rs1) alloc_reg(current,i,dops[i].rs1);
2079 static void add_stub(enum stub_type type, void *addr, void *retaddr,
2080 u_int a, uintptr_t b, uintptr_t c, u_int d, u_int e)
2082 assert(stubcount < ARRAY_SIZE(stubs));
2083 stubs[stubcount].type = type;
2084 stubs[stubcount].addr = addr;
2085 stubs[stubcount].retaddr = retaddr;
2086 stubs[stubcount].a = a;
2087 stubs[stubcount].b = b;
2088 stubs[stubcount].c = c;
2089 stubs[stubcount].d = d;
2090 stubs[stubcount].e = e;
2094 static void add_stub_r(enum stub_type type, void *addr, void *retaddr,
2095 int i, int addr_reg, const struct regstat *i_regs, int ccadj, u_int reglist)
2097 add_stub(type, addr, retaddr, i, addr_reg, (uintptr_t)i_regs, ccadj, reglist);
2100 // Write out a single register
2101 static void wb_register(signed char r,signed char regmap[],uint64_t dirty)
2104 for(hr=0;hr<HOST_REGS;hr++) {
2105 if(hr!=EXCLUDE_REG) {
2106 if((regmap[hr]&63)==r) {
2108 assert(regmap[hr]<64);
2109 emit_storereg(r,hr);
2116 static void wb_valid(signed char pre[],signed char entry[],u_int dirty_pre,u_int dirty,uint64_t u)
2118 //if(dirty_pre==dirty) return;
2120 for(hr=0;hr<HOST_REGS;hr++) {
2121 if(hr!=EXCLUDE_REG) {
2123 if(((~u)>>(reg&63))&1) {
2125 if(((dirty_pre&~dirty)>>hr)&1) {
2127 emit_storereg(reg,hr);
2140 static void pass_args(int a0, int a1)
2144 emit_mov(a0,2); emit_mov(a1,1); emit_mov(2,0);
2146 else if(a0!=0&&a1==0) {
2148 if (a0>=0) emit_mov(a0,0);
2151 if(a0>=0&&a0!=0) emit_mov(a0,0);
2152 if(a1>=0&&a1!=1) emit_mov(a1,1);
2156 static void alu_assemble(int i,struct regstat *i_regs)
2158 if(dops[i].opcode2>=0x20&&dops[i].opcode2<=0x23) { // ADD/ADDU/SUB/SUBU
2160 signed char s1,s2,t;
2161 t=get_reg(i_regs->regmap,dops[i].rt1);
2163 s1=get_reg(i_regs->regmap,dops[i].rs1);
2164 s2=get_reg(i_regs->regmap,dops[i].rs2);
2165 if(dops[i].rs1&&dops[i].rs2) {
2168 if(dops[i].opcode2&2) emit_sub(s1,s2,t);
2169 else emit_add(s1,s2,t);
2171 else if(dops[i].rs1) {
2172 if(s1>=0) emit_mov(s1,t);
2173 else emit_loadreg(dops[i].rs1,t);
2175 else if(dops[i].rs2) {
2177 if(dops[i].opcode2&2) emit_neg(s2,t);
2178 else emit_mov(s2,t);
2181 emit_loadreg(dops[i].rs2,t);
2182 if(dops[i].opcode2&2) emit_neg(t,t);
2185 else emit_zeroreg(t);
2189 if(dops[i].opcode2>=0x2c&&dops[i].opcode2<=0x2f) { // DADD/DADDU/DSUB/DSUBU
2192 if(dops[i].opcode2==0x2a||dops[i].opcode2==0x2b) { // SLT/SLTU
2194 signed char s1l,s2l,t;
2196 t=get_reg(i_regs->regmap,dops[i].rt1);
2199 s1l=get_reg(i_regs->regmap,dops[i].rs1);
2200 s2l=get_reg(i_regs->regmap,dops[i].rs2);
2201 if(dops[i].rs2==0) // rx<r0
2203 if(dops[i].opcode2==0x2a&&dops[i].rs1!=0) { // SLT
2205 emit_shrimm(s1l,31,t);
2207 else // SLTU (unsigned can not be less than zero, 0<0)
2210 else if(dops[i].rs1==0) // r0<rx
2213 if(dops[i].opcode2==0x2a) // SLT
2214 emit_set_gz32(s2l,t);
2215 else // SLTU (set if not zero)
2216 emit_set_nz32(s2l,t);
2219 assert(s1l>=0);assert(s2l>=0);
2220 if(dops[i].opcode2==0x2a) // SLT
2221 emit_set_if_less32(s1l,s2l,t);
2223 emit_set_if_carry32(s1l,s2l,t);
2229 if(dops[i].opcode2>=0x24&&dops[i].opcode2<=0x27) { // AND/OR/XOR/NOR
2231 signed char s1l,s2l,tl;
2232 tl=get_reg(i_regs->regmap,dops[i].rt1);
2235 s1l=get_reg(i_regs->regmap,dops[i].rs1);
2236 s2l=get_reg(i_regs->regmap,dops[i].rs2);
2237 if(dops[i].rs1&&dops[i].rs2) {
2240 if(dops[i].opcode2==0x24) { // AND
2241 emit_and(s1l,s2l,tl);
2243 if(dops[i].opcode2==0x25) { // OR
2244 emit_or(s1l,s2l,tl);
2246 if(dops[i].opcode2==0x26) { // XOR
2247 emit_xor(s1l,s2l,tl);
2249 if(dops[i].opcode2==0x27) { // NOR
2250 emit_or(s1l,s2l,tl);
2256 if(dops[i].opcode2==0x24) { // AND
2259 if(dops[i].opcode2==0x25||dops[i].opcode2==0x26) { // OR/XOR
2261 if(s1l>=0) emit_mov(s1l,tl);
2262 else emit_loadreg(dops[i].rs1,tl); // CHECK: regmap_entry?
2266 if(s2l>=0) emit_mov(s2l,tl);
2267 else emit_loadreg(dops[i].rs2,tl); // CHECK: regmap_entry?
2269 else emit_zeroreg(tl);
2271 if(dops[i].opcode2==0x27) { // NOR
2273 if(s1l>=0) emit_not(s1l,tl);
2275 emit_loadreg(dops[i].rs1,tl);
2281 if(s2l>=0) emit_not(s2l,tl);
2283 emit_loadreg(dops[i].rs2,tl);
2287 else emit_movimm(-1,tl);
2296 void imm16_assemble(int i,struct regstat *i_regs)
2298 if (dops[i].opcode==0x0f) { // LUI
2301 t=get_reg(i_regs->regmap,dops[i].rt1);
2304 if(!((i_regs->isconst>>t)&1))
2305 emit_movimm(imm[i]<<16,t);
2309 if(dops[i].opcode==0x08||dops[i].opcode==0x09) { // ADDI/ADDIU
2312 t=get_reg(i_regs->regmap,dops[i].rt1);
2313 s=get_reg(i_regs->regmap,dops[i].rs1);
2318 if(!((i_regs->isconst>>t)&1)) {
2320 if(i_regs->regmap_entry[t]!=dops[i].rs1) emit_loadreg(dops[i].rs1,t);
2321 emit_addimm(t,imm[i],t);
2323 if(!((i_regs->wasconst>>s)&1))
2324 emit_addimm(s,imm[i],t);
2326 emit_movimm(constmap[i][s]+imm[i],t);
2332 if(!((i_regs->isconst>>t)&1))
2333 emit_movimm(imm[i],t);
2338 if(dops[i].opcode==0x18||dops[i].opcode==0x19) { // DADDI/DADDIU
2341 tl=get_reg(i_regs->regmap,dops[i].rt1);
2342 sl=get_reg(i_regs->regmap,dops[i].rs1);
2346 emit_addimm(sl,imm[i],tl);
2348 emit_movimm(imm[i],tl);
2353 else if(dops[i].opcode==0x0a||dops[i].opcode==0x0b) { // SLTI/SLTIU
2355 //assert(dops[i].rs1!=0); // r0 might be valid, but it's probably a bug
2357 t=get_reg(i_regs->regmap,dops[i].rt1);
2358 sl=get_reg(i_regs->regmap,dops[i].rs1);
2362 if(dops[i].opcode==0x0a) { // SLTI
2364 if(i_regs->regmap_entry[t]!=dops[i].rs1) emit_loadreg(dops[i].rs1,t);
2365 emit_slti32(t,imm[i],t);
2367 emit_slti32(sl,imm[i],t);
2372 if(i_regs->regmap_entry[t]!=dops[i].rs1) emit_loadreg(dops[i].rs1,t);
2373 emit_sltiu32(t,imm[i],t);
2375 emit_sltiu32(sl,imm[i],t);
2379 // SLTI(U) with r0 is just stupid,
2380 // nonetheless examples can be found
2381 if(dops[i].opcode==0x0a) // SLTI
2382 if(0<imm[i]) emit_movimm(1,t);
2383 else emit_zeroreg(t);
2386 if(imm[i]) emit_movimm(1,t);
2387 else emit_zeroreg(t);
2393 else if(dops[i].opcode>=0x0c&&dops[i].opcode<=0x0e) { // ANDI/ORI/XORI
2396 tl=get_reg(i_regs->regmap,dops[i].rt1);
2397 sl=get_reg(i_regs->regmap,dops[i].rs1);
2398 if(tl>=0 && !((i_regs->isconst>>tl)&1)) {
2399 if(dops[i].opcode==0x0c) //ANDI
2403 if(i_regs->regmap_entry[tl]!=dops[i].rs1) emit_loadreg(dops[i].rs1,tl);
2404 emit_andimm(tl,imm[i],tl);
2406 if(!((i_regs->wasconst>>sl)&1))
2407 emit_andimm(sl,imm[i],tl);
2409 emit_movimm(constmap[i][sl]&imm[i],tl);
2419 if(i_regs->regmap_entry[tl]!=dops[i].rs1) emit_loadreg(dops[i].rs1,tl);
2421 if(dops[i].opcode==0x0d) { // ORI
2423 emit_orimm(tl,imm[i],tl);
2425 if(!((i_regs->wasconst>>sl)&1))
2426 emit_orimm(sl,imm[i],tl);
2428 emit_movimm(constmap[i][sl]|imm[i],tl);
2431 if(dops[i].opcode==0x0e) { // XORI
2433 emit_xorimm(tl,imm[i],tl);
2435 if(!((i_regs->wasconst>>sl)&1))
2436 emit_xorimm(sl,imm[i],tl);
2438 emit_movimm(constmap[i][sl]^imm[i],tl);
2443 emit_movimm(imm[i],tl);
2451 void shiftimm_assemble(int i,struct regstat *i_regs)
2453 if(dops[i].opcode2<=0x3) // SLL/SRL/SRA
2457 t=get_reg(i_regs->regmap,dops[i].rt1);
2458 s=get_reg(i_regs->regmap,dops[i].rs1);
2460 if(t>=0&&!((i_regs->isconst>>t)&1)){
2467 if(s<0&&i_regs->regmap_entry[t]!=dops[i].rs1) emit_loadreg(dops[i].rs1,t);
2469 if(dops[i].opcode2==0) // SLL
2471 emit_shlimm(s<0?t:s,imm[i],t);
2473 if(dops[i].opcode2==2) // SRL
2475 emit_shrimm(s<0?t:s,imm[i],t);
2477 if(dops[i].opcode2==3) // SRA
2479 emit_sarimm(s<0?t:s,imm[i],t);
2483 if(s>=0 && s!=t) emit_mov(s,t);
2487 //emit_storereg(dops[i].rt1,t); //DEBUG
2490 if(dops[i].opcode2>=0x38&&dops[i].opcode2<=0x3b) // DSLL/DSRL/DSRA
2494 if(dops[i].opcode2==0x3c) // DSLL32
2498 if(dops[i].opcode2==0x3e) // DSRL32
2502 if(dops[i].opcode2==0x3f) // DSRA32
2508 #ifndef shift_assemble
2509 static void shift_assemble(int i,struct regstat *i_regs)
2511 signed char s,t,shift;
2512 if (dops[i].rt1 == 0)
2514 assert(dops[i].opcode2<=0x07); // SLLV/SRLV/SRAV
2515 t = get_reg(i_regs->regmap, dops[i].rt1);
2516 s = get_reg(i_regs->regmap, dops[i].rs1);
2517 shift = get_reg(i_regs->regmap, dops[i].rs2);
2523 else if(dops[i].rs2==0) {
2525 if(s!=t) emit_mov(s,t);
2528 host_tempreg_acquire();
2529 emit_andimm(shift,31,HOST_TEMPREG);
2530 switch(dops[i].opcode2) {
2532 emit_shl(s,HOST_TEMPREG,t);
2535 emit_shr(s,HOST_TEMPREG,t);
2538 emit_sar(s,HOST_TEMPREG,t);
2543 host_tempreg_release();
2557 static int get_ptr_mem_type(u_int a)
2559 if(a < 0x00200000) {
2560 if(a<0x1000&&((start>>20)==0xbfc||(start>>24)==0xa0))
2561 // return wrong, must use memhandler for BIOS self-test to pass
2562 // 007 does similar stuff from a00 mirror, weird stuff
2566 if(0x1f800000 <= a && a < 0x1f801000)
2568 if(0x80200000 <= a && a < 0x80800000)
2570 if(0xa0000000 <= a && a < 0xa0200000)
2575 static void *emit_fastpath_cmp_jump(int i,int addr,int *addr_reg_override)
2580 if(((smrv_strong|smrv_weak)>>mr)&1) {
2581 type=get_ptr_mem_type(smrv[mr]);
2582 //printf("set %08x @%08x r%d %d\n", smrv[mr], start+i*4, mr, type);
2585 // use the mirror we are running on
2586 type=get_ptr_mem_type(start);
2587 //printf("set nospec @%08x r%d %d\n", start+i*4, mr, type);
2590 if(type==MTYPE_8020) { // RAM 80200000+ mirror
2591 host_tempreg_acquire();
2592 emit_andimm(addr,~0x00e00000,HOST_TEMPREG);
2593 addr=*addr_reg_override=HOST_TEMPREG;
2596 else if(type==MTYPE_0000) { // RAM 0 mirror
2597 host_tempreg_acquire();
2598 emit_orimm(addr,0x80000000,HOST_TEMPREG);
2599 addr=*addr_reg_override=HOST_TEMPREG;
2602 else if(type==MTYPE_A000) { // RAM A mirror
2603 host_tempreg_acquire();
2604 emit_andimm(addr,~0x20000000,HOST_TEMPREG);
2605 addr=*addr_reg_override=HOST_TEMPREG;
2608 else if(type==MTYPE_1F80) { // scratchpad
2609 if (psxH == (void *)0x1f800000) {
2610 host_tempreg_acquire();
2611 emit_xorimm(addr,0x1f800000,HOST_TEMPREG);
2612 emit_cmpimm(HOST_TEMPREG,0x1000);
2613 host_tempreg_release();
2618 // do the usual RAM check, jump will go to the right handler
2625 emit_cmpimm(addr,RAM_SIZE);
2627 #ifdef CORTEX_A8_BRANCH_PREDICTION_HACK
2628 // Hint to branch predictor that the branch is unlikely to be taken
2630 emit_jno_unlikely(0);
2635 host_tempreg_acquire();
2636 emit_addimm(addr,ram_offset,HOST_TEMPREG);
2637 addr=*addr_reg_override=HOST_TEMPREG;
2644 // return memhandler, or get directly accessable address and return 0
2645 static void *get_direct_memhandler(void *table, u_int addr,
2646 enum stub_type type, uintptr_t *addr_host)
2648 uintptr_t l1, l2 = 0;
2649 l1 = ((uintptr_t *)table)[addr>>12];
2650 if ((l1 & (1ul << (sizeof(l1)*8-1))) == 0) {
2651 uintptr_t v = l1 << 1;
2652 *addr_host = v + addr;
2657 if (type == LOADB_STUB || type == LOADBU_STUB || type == STOREB_STUB)
2658 l2 = ((uintptr_t *)l1)[0x1000/4 + 0x1000/2 + (addr&0xfff)];
2659 else if (type == LOADH_STUB || type == LOADHU_STUB || type == STOREH_STUB)
2660 l2=((uintptr_t *)l1)[0x1000/4 + (addr&0xfff)/2];
2662 l2=((uintptr_t *)l1)[(addr&0xfff)/4];
2663 if ((l2 & (1<<31)) == 0) {
2664 uintptr_t v = l2 << 1;
2665 *addr_host = v + (addr&0xfff);
2668 return (void *)(l2 << 1);
2672 static u_int get_host_reglist(const signed char *regmap)
2674 u_int reglist = 0, hr;
2675 for (hr = 0; hr < HOST_REGS; hr++) {
2676 if (hr != EXCLUDE_REG && regmap[hr] >= 0)
2682 static u_int reglist_exclude(u_int reglist, int r1, int r2)
2685 reglist &= ~(1u << r1);
2687 reglist &= ~(1u << r2);
2691 // find a temp caller-saved register not in reglist (so assumed to be free)
2692 static int reglist_find_free(u_int reglist)
2694 u_int free_regs = ~reglist & CALLER_SAVE_REGS;
2697 return __builtin_ctz(free_regs);
2700 static void load_assemble(int i, const struct regstat *i_regs)
2705 int memtarget=0,c=0;
2706 int fastio_reg_override=-1;
2707 u_int reglist=get_host_reglist(i_regs->regmap);
2708 tl=get_reg(i_regs->regmap,dops[i].rt1);
2709 s=get_reg(i_regs->regmap,dops[i].rs1);
2711 if(i_regs->regmap[HOST_CCREG]==CCREG) reglist&=~(1<<HOST_CCREG);
2713 c=(i_regs->wasconst>>s)&1;
2715 memtarget=((signed int)(constmap[i][s]+offset))<(signed int)0x80000000+RAM_SIZE;
2718 //printf("load_assemble: c=%d\n",c);
2719 //if(c) printf("load_assemble: const=%lx\n",(long)constmap[i][s]+offset);
2720 // FIXME: Even if the load is a NOP, we should check for pagefaults...
2721 if((tl<0&&(!c||(((u_int)constmap[i][s]+offset)>>16)==0x1f80))
2723 // could be FIFO, must perform the read
2725 assem_debug("(forced read)\n");
2726 tl=get_reg(i_regs->regmap,-1);
2729 if(offset||s<0||c) addr=tl;
2731 //if(tl<0) tl=get_reg(i_regs->regmap,-1);
2733 //printf("load_assemble: c=%d\n",c);
2734 //if(c) printf("load_assemble: const=%lx\n",(long)constmap[i][s]+offset);
2735 assert(tl>=0); // Even if the load is a NOP, we must check for pagefaults and I/O
2739 // Strmnnrmn's speed hack
2740 if(dops[i].rs1!=29||start<0x80001000||start>=0x80000000+RAM_SIZE)
2743 jaddr=emit_fastpath_cmp_jump(i,addr,&fastio_reg_override);
2746 else if(ram_offset&&memtarget) {
2747 host_tempreg_acquire();
2748 emit_addimm(addr,ram_offset,HOST_TEMPREG);
2749 fastio_reg_override=HOST_TEMPREG;
2751 int dummy=(dops[i].rt1==0)||(tl!=get_reg(i_regs->regmap,dops[i].rt1)); // ignore loads to r0 and unneeded reg
2752 if (dops[i].opcode==0x20) { // LB
2758 if(fastio_reg_override>=0) a=fastio_reg_override;
2760 emit_movsbl_indexed(x,a,tl);
2764 add_stub_r(LOADB_STUB,jaddr,out,i,addr,i_regs,ccadj[i],reglist);
2767 inline_readstub(LOADB_STUB,i,constmap[i][s]+offset,i_regs->regmap,dops[i].rt1,ccadj[i],reglist);
2769 if (dops[i].opcode==0x21) { // LH
2774 if(fastio_reg_override>=0) a=fastio_reg_override;
2775 emit_movswl_indexed(x,a,tl);
2778 add_stub_r(LOADH_STUB,jaddr,out,i,addr,i_regs,ccadj[i],reglist);
2781 inline_readstub(LOADH_STUB,i,constmap[i][s]+offset,i_regs->regmap,dops[i].rt1,ccadj[i],reglist);
2783 if (dops[i].opcode==0x23) { // LW
2787 if(fastio_reg_override>=0) a=fastio_reg_override;
2788 emit_readword_indexed(0,a,tl);
2791 add_stub_r(LOADW_STUB,jaddr,out,i,addr,i_regs,ccadj[i],reglist);
2794 inline_readstub(LOADW_STUB,i,constmap[i][s]+offset,i_regs->regmap,dops[i].rt1,ccadj[i],reglist);
2796 if (dops[i].opcode==0x24) { // LBU
2801 if(fastio_reg_override>=0) a=fastio_reg_override;
2803 emit_movzbl_indexed(x,a,tl);
2806 add_stub_r(LOADBU_STUB,jaddr,out,i,addr,i_regs,ccadj[i],reglist);
2809 inline_readstub(LOADBU_STUB,i,constmap[i][s]+offset,i_regs->regmap,dops[i].rt1,ccadj[i],reglist);
2811 if (dops[i].opcode==0x25) { // LHU
2816 if(fastio_reg_override>=0) a=fastio_reg_override;
2817 emit_movzwl_indexed(x,a,tl);
2820 add_stub_r(LOADHU_STUB,jaddr,out,i,addr,i_regs,ccadj[i],reglist);
2823 inline_readstub(LOADHU_STUB,i,constmap[i][s]+offset,i_regs->regmap,dops[i].rt1,ccadj[i],reglist);
2825 if (dops[i].opcode==0x27) { // LWU
2828 if (dops[i].opcode==0x37) { // LD
2832 if (fastio_reg_override == HOST_TEMPREG)
2833 host_tempreg_release();
2836 #ifndef loadlr_assemble
2837 static void loadlr_assemble(int i, const struct regstat *i_regs)
2839 int s,tl,temp,temp2,addr;
2842 int memtarget=0,c=0;
2843 int fastio_reg_override=-1;
2844 u_int reglist=get_host_reglist(i_regs->regmap);
2845 tl=get_reg(i_regs->regmap,dops[i].rt1);
2846 s=get_reg(i_regs->regmap,dops[i].rs1);
2847 temp=get_reg(i_regs->regmap,-1);
2848 temp2=get_reg(i_regs->regmap,FTEMP);
2849 addr=get_reg(i_regs->regmap,AGEN1+(i&1));
2853 if(offset||s<0||c) addr=temp2;
2856 c=(i_regs->wasconst>>s)&1;
2858 memtarget=((signed int)(constmap[i][s]+offset))<(signed int)0x80000000+RAM_SIZE;
2862 emit_shlimm(addr,3,temp);
2863 if (dops[i].opcode==0x22||dops[i].opcode==0x26) {
2864 emit_andimm(addr,0xFFFFFFFC,temp2); // LWL/LWR
2866 emit_andimm(addr,0xFFFFFFF8,temp2); // LDL/LDR
2868 jaddr=emit_fastpath_cmp_jump(i,temp2,&fastio_reg_override);
2871 if(ram_offset&&memtarget) {
2872 host_tempreg_acquire();
2873 emit_addimm(temp2,ram_offset,HOST_TEMPREG);
2874 fastio_reg_override=HOST_TEMPREG;
2876 if (dops[i].opcode==0x22||dops[i].opcode==0x26) {
2877 emit_movimm(((constmap[i][s]+offset)<<3)&24,temp); // LWL/LWR
2879 emit_movimm(((constmap[i][s]+offset)<<3)&56,temp); // LDL/LDR
2882 if (dops[i].opcode==0x22||dops[i].opcode==0x26) { // LWL/LWR
2885 if(fastio_reg_override>=0) a=fastio_reg_override;
2886 emit_readword_indexed(0,a,temp2);
2887 if(fastio_reg_override==HOST_TEMPREG) host_tempreg_release();
2888 if(jaddr) add_stub_r(LOADW_STUB,jaddr,out,i,temp2,i_regs,ccadj[i],reglist);
2891 inline_readstub(LOADW_STUB,i,(constmap[i][s]+offset)&0xFFFFFFFC,i_regs->regmap,FTEMP,ccadj[i],reglist);
2894 emit_andimm(temp,24,temp);
2895 if (dops[i].opcode==0x22) // LWL
2896 emit_xorimm(temp,24,temp);
2897 host_tempreg_acquire();
2898 emit_movimm(-1,HOST_TEMPREG);
2899 if (dops[i].opcode==0x26) {
2900 emit_shr(temp2,temp,temp2);
2901 emit_bic_lsr(tl,HOST_TEMPREG,temp,tl);
2903 emit_shl(temp2,temp,temp2);
2904 emit_bic_lsl(tl,HOST_TEMPREG,temp,tl);
2906 host_tempreg_release();
2907 emit_or(temp2,tl,tl);
2909 //emit_storereg(dops[i].rt1,tl); // DEBUG
2911 if (dops[i].opcode==0x1A||dops[i].opcode==0x1B) { // LDL/LDR
2917 void store_assemble(int i, const struct regstat *i_regs)
2923 enum stub_type type;
2924 int memtarget=0,c=0;
2925 int agr=AGEN1+(i&1);
2926 int fastio_reg_override=-1;
2927 u_int reglist=get_host_reglist(i_regs->regmap);
2928 tl=get_reg(i_regs->regmap,dops[i].rs2);
2929 s=get_reg(i_regs->regmap,dops[i].rs1);
2930 temp=get_reg(i_regs->regmap,agr);
2931 if(temp<0) temp=get_reg(i_regs->regmap,-1);
2934 c=(i_regs->wasconst>>s)&1;
2936 memtarget=((signed int)(constmap[i][s]+offset))<(signed int)0x80000000+RAM_SIZE;
2941 if(i_regs->regmap[HOST_CCREG]==CCREG) reglist&=~(1<<HOST_CCREG);
2942 if(offset||s<0||c) addr=temp;
2945 jaddr=emit_fastpath_cmp_jump(i,addr,&fastio_reg_override);
2947 else if(ram_offset&&memtarget) {
2948 host_tempreg_acquire();
2949 emit_addimm(addr,ram_offset,HOST_TEMPREG);
2950 fastio_reg_override=HOST_TEMPREG;
2953 if (dops[i].opcode==0x28) { // SB
2957 if(fastio_reg_override>=0) a=fastio_reg_override;
2958 emit_writebyte_indexed(tl,x,a);
2962 if (dops[i].opcode==0x29) { // SH
2966 if(fastio_reg_override>=0) a=fastio_reg_override;
2967 emit_writehword_indexed(tl,x,a);
2971 if (dops[i].opcode==0x2B) { // SW
2974 if(fastio_reg_override>=0) a=fastio_reg_override;
2975 emit_writeword_indexed(tl,0,a);
2979 if (dops[i].opcode==0x3F) { // SD
2983 if(fastio_reg_override==HOST_TEMPREG)
2984 host_tempreg_release();
2986 // PCSX store handlers don't check invcode again
2988 add_stub_r(type,jaddr,out,i,addr,i_regs,ccadj[i],reglist);
2991 if(!(i_regs->waswritten&(1<<dops[i].rs1)) && !HACK_ENABLED(NDHACK_NO_SMC_CHECK)) {
2993 #ifdef DESTRUCTIVE_SHIFT
2994 // The x86 shift operation is 'destructive'; it overwrites the
2995 // source register, so we need to make a copy first and use that.
2998 #if defined(HOST_IMM8)
2999 int ir=get_reg(i_regs->regmap,INVCP);
3001 emit_cmpmem_indexedsr12_reg(ir,addr,1);
3003 emit_cmpmem_indexedsr12_imm(invalid_code,addr,1);
3005 #if defined(HAVE_CONDITIONAL_CALL) && !defined(DESTRUCTIVE_SHIFT)
3006 emit_callne(invalidate_addr_reg[addr]);
3010 add_stub(INVCODE_STUB,jaddr2,out,reglist|(1<<HOST_CCREG),addr,0,0,0);
3014 u_int addr_val=constmap[i][s]+offset;
3016 add_stub_r(type,jaddr,out,i,addr,i_regs,ccadj[i],reglist);
3017 } else if(c&&!memtarget) {
3018 inline_writestub(type,i,addr_val,i_regs->regmap,dops[i].rs2,ccadj[i],reglist);
3020 // basic current block modification detection..
3021 // not looking back as that should be in mips cache already
3022 // (see Spyro2 title->attract mode)
3023 if(c&&start+i*4<addr_val&&addr_val<start+slen*4) {
3024 SysPrintf("write to %08x hits block %08x, pc=%08x\n",addr_val,start,start+i*4);
3025 assert(i_regs->regmap==regs[i].regmap); // not delay slot
3026 if(i_regs->regmap==regs[i].regmap) {
3027 load_all_consts(regs[i].regmap_entry,regs[i].wasdirty,i);
3028 wb_dirtys(regs[i].regmap_entry,regs[i].wasdirty);
3029 emit_movimm(start+i*4+4,0);
3030 emit_writeword(0,&pcaddr);
3031 emit_addimm(HOST_CCREG,2,HOST_CCREG);
3032 emit_far_call(get_addr_ht);
3038 static void storelr_assemble(int i, const struct regstat *i_regs)
3044 void *case1, *case2, *case3;
3045 void *done0, *done1, *done2;
3046 int memtarget=0,c=0;
3047 int agr=AGEN1+(i&1);
3048 u_int reglist=get_host_reglist(i_regs->regmap);
3049 tl=get_reg(i_regs->regmap,dops[i].rs2);
3050 s=get_reg(i_regs->regmap,dops[i].rs1);
3051 temp=get_reg(i_regs->regmap,agr);
3052 if(temp<0) temp=get_reg(i_regs->regmap,-1);
3055 c=(i_regs->isconst>>s)&1;
3057 memtarget=((signed int)(constmap[i][s]+offset))<(signed int)0x80000000+RAM_SIZE;
3063 emit_cmpimm(s<0||offset?temp:s,RAM_SIZE);
3064 if(!offset&&s!=temp) emit_mov(s,temp);
3070 if(!memtarget||!dops[i].rs1) {
3076 emit_addimm_no_flags(ram_offset,temp);
3078 if (dops[i].opcode==0x2C||dops[i].opcode==0x2D) { // SDL/SDR
3082 emit_xorimm(temp,3,temp);
3083 emit_testimm(temp,2);
3086 emit_testimm(temp,1);
3090 if (dops[i].opcode==0x2A) { // SWL
3091 emit_writeword_indexed(tl,0,temp);
3093 else if (dops[i].opcode==0x2E) { // SWR
3094 emit_writebyte_indexed(tl,3,temp);
3101 set_jump_target(case1, out);
3102 if (dops[i].opcode==0x2A) { // SWL
3103 // Write 3 msb into three least significant bytes
3104 if(dops[i].rs2) emit_rorimm(tl,8,tl);
3105 emit_writehword_indexed(tl,-1,temp);
3106 if(dops[i].rs2) emit_rorimm(tl,16,tl);
3107 emit_writebyte_indexed(tl,1,temp);
3108 if(dops[i].rs2) emit_rorimm(tl,8,tl);
3110 else if (dops[i].opcode==0x2E) { // SWR
3111 // Write two lsb into two most significant bytes
3112 emit_writehword_indexed(tl,1,temp);
3117 set_jump_target(case2, out);
3118 emit_testimm(temp,1);
3121 if (dops[i].opcode==0x2A) { // SWL
3122 // Write two msb into two least significant bytes
3123 if(dops[i].rs2) emit_rorimm(tl,16,tl);
3124 emit_writehword_indexed(tl,-2,temp);
3125 if(dops[i].rs2) emit_rorimm(tl,16,tl);
3127 else if (dops[i].opcode==0x2E) { // SWR
3128 // Write 3 lsb into three most significant bytes
3129 emit_writebyte_indexed(tl,-1,temp);
3130 if(dops[i].rs2) emit_rorimm(tl,8,tl);
3131 emit_writehword_indexed(tl,0,temp);
3132 if(dops[i].rs2) emit_rorimm(tl,24,tl);
3137 set_jump_target(case3, out);
3138 if (dops[i].opcode==0x2A) { // SWL
3139 // Write msb into least significant byte
3140 if(dops[i].rs2) emit_rorimm(tl,24,tl);
3141 emit_writebyte_indexed(tl,-3,temp);
3142 if(dops[i].rs2) emit_rorimm(tl,8,tl);
3144 else if (dops[i].opcode==0x2E) { // SWR
3145 // Write entire word
3146 emit_writeword_indexed(tl,-3,temp);
3148 set_jump_target(done0, out);
3149 set_jump_target(done1, out);
3150 set_jump_target(done2, out);
3152 add_stub_r(STORELR_STUB,jaddr,out,i,temp,i_regs,ccadj[i],reglist);
3153 if(!(i_regs->waswritten&(1<<dops[i].rs1)) && !HACK_ENABLED(NDHACK_NO_SMC_CHECK)) {
3154 emit_addimm_no_flags(-ram_offset,temp);
3155 #if defined(HOST_IMM8)
3156 int ir=get_reg(i_regs->regmap,INVCP);
3158 emit_cmpmem_indexedsr12_reg(ir,temp,1);
3160 emit_cmpmem_indexedsr12_imm(invalid_code,temp,1);
3162 #if defined(HAVE_CONDITIONAL_CALL) && !defined(DESTRUCTIVE_SHIFT)
3163 emit_callne(invalidate_addr_reg[temp]);
3167 add_stub(INVCODE_STUB,jaddr2,out,reglist|(1<<HOST_CCREG),temp,0,0,0);
3172 static void cop0_assemble(int i,struct regstat *i_regs)
3174 if(dops[i].opcode2==0) // MFC0
3176 signed char t=get_reg(i_regs->regmap,dops[i].rt1);
3177 u_int copr=(source[i]>>11)&0x1f;
3178 //assert(t>=0); // Why does this happen? OOT is weird
3179 if(t>=0&&dops[i].rt1!=0) {
3180 emit_readword(®_cop0[copr],t);
3183 else if(dops[i].opcode2==4) // MTC0
3185 signed char s=get_reg(i_regs->regmap,dops[i].rs1);
3186 char copr=(source[i]>>11)&0x1f;
3188 wb_register(dops[i].rs1,i_regs->regmap,i_regs->dirty);
3189 if(copr==9||copr==11||copr==12||copr==13) {
3190 emit_readword(&last_count,HOST_TEMPREG);
3191 emit_loadreg(CCREG,HOST_CCREG); // TODO: do proper reg alloc
3192 emit_add(HOST_CCREG,HOST_TEMPREG,HOST_CCREG);
3193 emit_addimm(HOST_CCREG,CLOCK_ADJUST(ccadj[i]),HOST_CCREG);
3194 emit_writeword(HOST_CCREG,&Count);
3196 // What a mess. The status register (12) can enable interrupts,
3197 // so needs a special case to handle a pending interrupt.
3198 // The interrupt must be taken immediately, because a subsequent
3199 // instruction might disable interrupts again.
3200 if(copr==12||copr==13) {
3202 // burn cycles to cause cc_interrupt, which will
3203 // reschedule next_interupt. Relies on CCREG from above.
3204 assem_debug("MTC0 DS %d\n", copr);
3205 emit_writeword(HOST_CCREG,&last_count);
3206 emit_movimm(0,HOST_CCREG);
3207 emit_storereg(CCREG,HOST_CCREG);
3208 emit_loadreg(dops[i].rs1,1);
3209 emit_movimm(copr,0);
3210 emit_far_call(pcsx_mtc0_ds);
3211 emit_loadreg(dops[i].rs1,s);
3214 emit_movimm(start+i*4+4,HOST_TEMPREG);
3215 emit_writeword(HOST_TEMPREG,&pcaddr);
3216 emit_movimm(0,HOST_TEMPREG);
3217 emit_writeword(HOST_TEMPREG,&pending_exception);
3220 emit_loadreg(dops[i].rs1,1);
3223 emit_movimm(copr,0);
3224 emit_far_call(pcsx_mtc0);
3225 if(copr==9||copr==11||copr==12||copr==13) {
3226 emit_readword(&Count,HOST_CCREG);
3227 emit_readword(&next_interupt,HOST_TEMPREG);
3228 emit_addimm(HOST_CCREG,-CLOCK_ADJUST(ccadj[i]),HOST_CCREG);
3229 emit_sub(HOST_CCREG,HOST_TEMPREG,HOST_CCREG);
3230 emit_writeword(HOST_TEMPREG,&last_count);
3231 emit_storereg(CCREG,HOST_CCREG);
3233 if(copr==12||copr==13) {
3234 assert(!is_delayslot);
3235 emit_readword(&pending_exception,14);
3239 emit_readword(&pcaddr, 0);
3240 emit_addimm(HOST_CCREG,2,HOST_CCREG);
3241 emit_far_call(get_addr_ht);
3243 set_jump_target(jaddr, out);
3245 emit_loadreg(dops[i].rs1,s);
3249 assert(dops[i].opcode2==0x10);
3250 //if((source[i]&0x3f)==0x10) // RFE
3252 emit_readword(&Status,0);
3253 emit_andimm(0,0x3c,1);
3254 emit_andimm(0,~0xf,0);
3255 emit_orrshr_imm(1,2,0);
3256 emit_writeword(0,&Status);
3261 static void cop1_unusable(int i,struct regstat *i_regs)
3263 // XXX: should just just do the exception instead
3268 add_stub_r(FP_STUB,jaddr,out,i,0,i_regs,is_delayslot,0);
3272 static void cop1_assemble(int i,struct regstat *i_regs)
3274 cop1_unusable(i, i_regs);
3277 static void c1ls_assemble(int i,struct regstat *i_regs)
3279 cop1_unusable(i, i_regs);
3283 static void do_cop1stub(int n)
3286 assem_debug("do_cop1stub %x\n",start+stubs[n].a*4);
3287 set_jump_target(stubs[n].addr, out);
3289 // int rs=stubs[n].b;
3290 struct regstat *i_regs=(struct regstat *)stubs[n].c;
3293 load_all_consts(regs[i].regmap_entry,regs[i].wasdirty,i);
3294 //if(i_regs!=®s[i]) printf("oops: regs[i]=%x i_regs=%x",(int)®s[i],(int)i_regs);
3296 //else {printf("fp exception in delay slot\n");}
3297 wb_dirtys(i_regs->regmap_entry,i_regs->wasdirty);
3298 if(regs[i].regmap_entry[HOST_CCREG]!=CCREG) emit_loadreg(CCREG,HOST_CCREG);
3299 emit_movimm(start+(i-ds)*4,EAX); // Get PC
3300 emit_addimm(HOST_CCREG,CLOCK_ADJUST(ccadj[i]),HOST_CCREG); // CHECK: is this right? There should probably be an extra cycle...
3301 emit_far_jump(ds?fp_exception_ds:fp_exception);
3304 static int cop2_is_stalling_op(int i, int *cycles)
3306 if (dops[i].opcode == 0x3a) { // SWC2
3310 if (dops[i].itype == COP2 && (dops[i].opcode2 == 0 || dops[i].opcode2 == 2)) { // MFC2/CFC2
3314 if (dops[i].itype == C2OP) {
3315 *cycles = gte_cycletab[source[i] & 0x3f];
3318 // ... what about MTC2/CTC2/LWC2?
3323 static void log_gte_stall(int stall, u_int cycle)
3325 if ((u_int)stall <= 44)
3326 printf("x stall %2d %u\n", stall, cycle + last_count);
3329 static void emit_log_gte_stall(int i, int stall, u_int reglist)
3333 emit_movimm(stall, 0);
3335 emit_mov(HOST_TEMPREG, 0);
3336 emit_addimm(HOST_CCREG, CLOCK_ADJUST(ccadj[i]), 1);
3337 emit_far_call(log_gte_stall);
3338 restore_regs(reglist);
3342 static void cop2_do_stall_check(u_int op, int i, const struct regstat *i_regs, u_int reglist)
3344 int j = i, other_gte_op_cycles = -1, stall = -MAXBLOCK, cycles_passed;
3345 int rtmp = reglist_find_free(reglist);
3347 if (HACK_ENABLED(NDHACK_NO_STALLS))
3349 if (get_reg(i_regs->regmap, CCREG) != HOST_CCREG) {
3350 // happens occasionally... cc evicted? Don't bother then
3351 //printf("no cc %08x\n", start + i*4);
3355 for (j = i - 1; j >= 0; j--) {
3356 //if (dops[j].is_ds) break;
3357 if (cop2_is_stalling_op(j, &other_gte_op_cycles) || dops[j].bt)
3362 cycles_passed = CLOCK_ADJUST(ccadj[i] - ccadj[j]);
3363 if (other_gte_op_cycles >= 0)
3364 stall = other_gte_op_cycles - cycles_passed;
3365 else if (cycles_passed >= 44)
3366 stall = 0; // can't stall
3367 if (stall == -MAXBLOCK && rtmp >= 0) {
3368 // unknown stall, do the expensive runtime check
3369 assem_debug("; cop2_do_stall_check\n");
3372 emit_movimm(gte_cycletab[op], 0);
3373 emit_addimm(HOST_CCREG, CLOCK_ADJUST(ccadj[i]), 1);
3374 emit_far_call(call_gteStall);
3375 restore_regs(reglist);
3377 host_tempreg_acquire();
3378 emit_readword(&psxRegs.gteBusyCycle, rtmp);
3379 emit_addimm(rtmp, -CLOCK_ADJUST(ccadj[i]), rtmp);
3380 emit_sub(rtmp, HOST_CCREG, HOST_TEMPREG);
3381 emit_cmpimm(HOST_TEMPREG, 44);
3382 emit_cmovb_reg(rtmp, HOST_CCREG);
3383 //emit_log_gte_stall(i, 0, reglist);
3384 host_tempreg_release();
3387 else if (stall > 0) {
3388 //emit_log_gte_stall(i, stall, reglist);
3389 emit_addimm(HOST_CCREG, stall, HOST_CCREG);
3392 // save gteBusyCycle, if needed
3393 if (gte_cycletab[op] == 0)
3395 other_gte_op_cycles = -1;
3396 for (j = i + 1; j < slen; j++) {
3397 if (cop2_is_stalling_op(j, &other_gte_op_cycles))
3401 if (j + 1 < slen && cop2_is_stalling_op(j + 1, &other_gte_op_cycles))
3406 if (other_gte_op_cycles >= 0)
3407 // will handle stall when assembling that op
3409 cycles_passed = CLOCK_ADJUST(ccadj[min(j, slen -1)] - ccadj[i]);
3410 if (cycles_passed >= 44)
3412 assem_debug("; save gteBusyCycle\n");
3413 host_tempreg_acquire();
3415 emit_readword(&last_count, HOST_TEMPREG);
3416 emit_add(HOST_TEMPREG, HOST_CCREG, HOST_TEMPREG);
3417 emit_addimm(HOST_TEMPREG, CLOCK_ADJUST(ccadj[i]), HOST_TEMPREG);
3418 emit_addimm(HOST_TEMPREG, gte_cycletab[op]), HOST_TEMPREG);
3419 emit_writeword(HOST_TEMPREG, &psxRegs.gteBusyCycle);
3421 emit_addimm(HOST_CCREG, CLOCK_ADJUST(ccadj[i]) + gte_cycletab[op], HOST_TEMPREG);
3422 emit_writeword(HOST_TEMPREG, &psxRegs.gteBusyCycle);
3424 host_tempreg_release();
3427 static int is_mflohi(int i)
3429 return (dops[i].itype == MOV && (dops[i].rs1 == HIREG || dops[i].rs1 == LOREG));
3432 static int check_multdiv(int i, int *cycles)
3434 if (dops[i].itype != MULTDIV)
3436 if (dops[i].opcode2 == 0x18 || dops[i].opcode2 == 0x19) // MULT(U)
3437 *cycles = 11; // approx from 7 11 14
3443 static void multdiv_prepare_stall(int i, const struct regstat *i_regs)
3445 int j, found = 0, c = 0;
3446 if (HACK_ENABLED(NDHACK_NO_STALLS))
3448 if (get_reg(i_regs->regmap, CCREG) != HOST_CCREG) {
3449 // happens occasionally... cc evicted? Don't bother then
3452 for (j = i + 1; j < slen; j++) {
3455 if ((found = is_mflohi(j)))
3459 if (j + 1 < slen && (found = is_mflohi(j + 1)))
3465 // handle all in multdiv_do_stall()
3467 check_multdiv(i, &c);
3469 assem_debug("; muldiv prepare stall %d\n", c);
3470 host_tempreg_acquire();
3471 emit_addimm(HOST_CCREG, CLOCK_ADJUST(ccadj[i]) + c, HOST_TEMPREG);
3472 emit_writeword(HOST_TEMPREG, &psxRegs.muldivBusyCycle);
3473 host_tempreg_release();
3476 static void multdiv_do_stall(int i, const struct regstat *i_regs)
3478 int j, known_cycles = 0;
3479 u_int reglist = get_host_reglist(i_regs->regmap);
3480 int rtmp = get_reg(i_regs->regmap, -1);
3482 rtmp = reglist_find_free(reglist);
3483 if (HACK_ENABLED(NDHACK_NO_STALLS))
3485 if (get_reg(i_regs->regmap, CCREG) != HOST_CCREG || rtmp < 0) {
3486 // happens occasionally... cc evicted? Don't bother then
3487 //printf("no cc/rtmp %08x\n", start + i*4);
3491 for (j = i - 1; j >= 0; j--) {
3492 if (dops[j].is_ds) break;
3493 if (check_multdiv(j, &known_cycles) || dops[j].bt)
3496 // already handled by this op
3501 if (known_cycles > 0) {
3502 known_cycles -= CLOCK_ADJUST(ccadj[i] - ccadj[j]);
3503 assem_debug("; muldiv stall resolved %d\n", known_cycles);
3504 if (known_cycles > 0)
3505 emit_addimm(HOST_CCREG, known_cycles, HOST_CCREG);
3508 assem_debug("; muldiv stall unresolved\n");
3509 host_tempreg_acquire();
3510 emit_readword(&psxRegs.muldivBusyCycle, rtmp);
3511 emit_addimm(rtmp, -CLOCK_ADJUST(ccadj[i]), rtmp);
3512 emit_sub(rtmp, HOST_CCREG, HOST_TEMPREG);
3513 emit_cmpimm(HOST_TEMPREG, 37);
3514 emit_cmovb_reg(rtmp, HOST_CCREG);
3515 //emit_log_gte_stall(i, 0, reglist);
3516 host_tempreg_release();
3519 static void cop2_get_dreg(u_int copr,signed char tl,signed char temp)
3529 emit_readword(®_cop2d[copr],tl);
3530 emit_signextend16(tl,tl);
3531 emit_writeword(tl,®_cop2d[copr]); // hmh
3538 emit_readword(®_cop2d[copr],tl);
3539 emit_andimm(tl,0xffff,tl);
3540 emit_writeword(tl,®_cop2d[copr]);
3543 emit_readword(®_cop2d[14],tl); // SXY2
3544 emit_writeword(tl,®_cop2d[copr]);
3548 c2op_mfc2_29_assemble(tl,temp);
3551 emit_readword(®_cop2d[copr],tl);
3556 static void cop2_put_dreg(u_int copr,signed char sl,signed char temp)
3560 emit_readword(®_cop2d[13],temp); // SXY1
3561 emit_writeword(sl,®_cop2d[copr]);
3562 emit_writeword(temp,®_cop2d[12]); // SXY0
3563 emit_readword(®_cop2d[14],temp); // SXY2
3564 emit_writeword(sl,®_cop2d[14]);
3565 emit_writeword(temp,®_cop2d[13]); // SXY1
3568 emit_andimm(sl,0x001f,temp);
3569 emit_shlimm(temp,7,temp);
3570 emit_writeword(temp,®_cop2d[9]);
3571 emit_andimm(sl,0x03e0,temp);
3572 emit_shlimm(temp,2,temp);
3573 emit_writeword(temp,®_cop2d[10]);
3574 emit_andimm(sl,0x7c00,temp);
3575 emit_shrimm(temp,3,temp);
3576 emit_writeword(temp,®_cop2d[11]);
3577 emit_writeword(sl,®_cop2d[28]);
3580 emit_xorsar_imm(sl,sl,31,temp);
3581 #if defined(HAVE_ARMV5) || defined(__aarch64__)
3582 emit_clz(temp,temp);
3584 emit_movs(temp,HOST_TEMPREG);
3585 emit_movimm(0,temp);
3586 emit_jeq((int)out+4*4);
3587 emit_addpl_imm(temp,1,temp);
3588 emit_lslpls_imm(HOST_TEMPREG,1,HOST_TEMPREG);
3589 emit_jns((int)out-2*4);
3591 emit_writeword(sl,®_cop2d[30]);
3592 emit_writeword(temp,®_cop2d[31]);
3597 emit_writeword(sl,®_cop2d[copr]);
3602 static void c2ls_assemble(int i, const struct regstat *i_regs)
3607 int memtarget=0,c=0;
3609 enum stub_type type;
3610 int agr=AGEN1+(i&1);
3611 int fastio_reg_override=-1;
3612 u_int reglist=get_host_reglist(i_regs->regmap);
3613 u_int copr=(source[i]>>16)&0x1f;
3614 s=get_reg(i_regs->regmap,dops[i].rs1);
3615 tl=get_reg(i_regs->regmap,FTEMP);
3617 assert(dops[i].rs1>0);
3620 if(i_regs->regmap[HOST_CCREG]==CCREG)
3621 reglist&=~(1<<HOST_CCREG);
3624 if (dops[i].opcode==0x3a) { // SWC2
3625 ar=get_reg(i_regs->regmap,agr);
3626 if(ar<0) ar=get_reg(i_regs->regmap,-1);
3631 if(s>=0) c=(i_regs->wasconst>>s)&1;
3632 memtarget=c&&(((signed int)(constmap[i][s]+offset))<(signed int)0x80000000+RAM_SIZE);
3633 if (!offset&&!c&&s>=0) ar=s;
3636 cop2_do_stall_check(0, i, i_regs, reglist);
3638 if (dops[i].opcode==0x3a) { // SWC2
3639 cop2_get_dreg(copr,tl,-1);
3647 emit_jmp(0); // inline_readstub/inline_writestub?
3651 jaddr2=emit_fastpath_cmp_jump(i,ar,&fastio_reg_override);
3653 else if(ram_offset&&memtarget) {
3654 host_tempreg_acquire();
3655 emit_addimm(ar,ram_offset,HOST_TEMPREG);
3656 fastio_reg_override=HOST_TEMPREG;
3658 if (dops[i].opcode==0x32) { // LWC2
3660 if(fastio_reg_override>=0) a=fastio_reg_override;
3661 emit_readword_indexed(0,a,tl);
3663 if (dops[i].opcode==0x3a) { // SWC2
3664 #ifdef DESTRUCTIVE_SHIFT
3665 if(!offset&&!c&&s>=0) emit_mov(s,ar);
3668 if(fastio_reg_override>=0) a=fastio_reg_override;
3669 emit_writeword_indexed(tl,0,a);
3672 if(fastio_reg_override==HOST_TEMPREG)
3673 host_tempreg_release();
3675 add_stub_r(type,jaddr2,out,i,ar,i_regs,ccadj[i],reglist);
3676 if(dops[i].opcode==0x3a) // SWC2
3677 if(!(i_regs->waswritten&(1<<dops[i].rs1)) && !HACK_ENABLED(NDHACK_NO_SMC_CHECK)) {
3678 #if defined(HOST_IMM8)
3679 int ir=get_reg(i_regs->regmap,INVCP);
3681 emit_cmpmem_indexedsr12_reg(ir,ar,1);
3683 emit_cmpmem_indexedsr12_imm(invalid_code,ar,1);
3685 #if defined(HAVE_CONDITIONAL_CALL) && !defined(DESTRUCTIVE_SHIFT)
3686 emit_callne(invalidate_addr_reg[ar]);
3690 add_stub(INVCODE_STUB,jaddr3,out,reglist|(1<<HOST_CCREG),ar,0,0,0);
3693 if (dops[i].opcode==0x32) { // LWC2
3694 host_tempreg_acquire();
3695 cop2_put_dreg(copr,tl,HOST_TEMPREG);
3696 host_tempreg_release();
3700 static void cop2_assemble(int i, const struct regstat *i_regs)
3702 u_int copr = (source[i]>>11) & 0x1f;
3703 signed char temp = get_reg(i_regs->regmap, -1);
3705 if (!HACK_ENABLED(NDHACK_NO_STALLS)) {
3706 u_int reglist = reglist_exclude(get_host_reglist(i_regs->regmap), temp, -1);
3707 if (dops[i].opcode2 == 0 || dops[i].opcode2 == 2) { // MFC2/CFC2
3708 signed char tl = get_reg(i_regs->regmap, dops[i].rt1);
3709 reglist = reglist_exclude(reglist, tl, -1);
3711 cop2_do_stall_check(0, i, i_regs, reglist);
3713 if (dops[i].opcode2==0) { // MFC2
3714 signed char tl=get_reg(i_regs->regmap,dops[i].rt1);
3715 if(tl>=0&&dops[i].rt1!=0)
3716 cop2_get_dreg(copr,tl,temp);
3718 else if (dops[i].opcode2==4) { // MTC2
3719 signed char sl=get_reg(i_regs->regmap,dops[i].rs1);
3720 cop2_put_dreg(copr,sl,temp);
3722 else if (dops[i].opcode2==2) // CFC2
3724 signed char tl=get_reg(i_regs->regmap,dops[i].rt1);
3725 if(tl>=0&&dops[i].rt1!=0)
3726 emit_readword(®_cop2c[copr],tl);
3728 else if (dops[i].opcode2==6) // CTC2
3730 signed char sl=get_reg(i_regs->regmap,dops[i].rs1);
3739 emit_signextend16(sl,temp);
3742 c2op_ctc2_31_assemble(sl,temp);
3748 emit_writeword(temp,®_cop2c[copr]);
3753 static void do_unalignedwritestub(int n)
3755 assem_debug("do_unalignedwritestub %x\n",start+stubs[n].a*4);
3757 set_jump_target(stubs[n].addr, out);
3760 struct regstat *i_regs=(struct regstat *)stubs[n].c;
3761 int addr=stubs[n].b;
3762 u_int reglist=stubs[n].e;
3763 signed char *i_regmap=i_regs->regmap;
3764 int temp2=get_reg(i_regmap,FTEMP);
3766 rt=get_reg(i_regmap,dops[i].rs2);
3769 assert(dops[i].opcode==0x2a||dops[i].opcode==0x2e); // SWL/SWR only implemented
3771 reglist&=~(1<<temp2);
3773 // don't bother with it and call write handler
3776 int cc=get_reg(i_regmap,CCREG);
3778 emit_loadreg(CCREG,2);
3779 emit_addimm(cc<0?2:cc,CLOCK_ADJUST((int)stubs[n].d+1),2);
3780 emit_far_call((dops[i].opcode==0x2a?jump_handle_swl:jump_handle_swr));
3781 emit_addimm(0,-CLOCK_ADJUST((int)stubs[n].d+1),cc<0?2:cc);
3783 emit_storereg(CCREG,2);
3784 restore_regs(reglist);
3785 emit_jmp(stubs[n].retaddr); // return address
3788 #ifndef multdiv_assemble
3789 void multdiv_assemble(int i,struct regstat *i_regs)
3791 printf("Need multdiv_assemble for this architecture.\n");
3796 static void mov_assemble(int i,struct regstat *i_regs)
3798 //if(dops[i].opcode2==0x10||dops[i].opcode2==0x12) { // MFHI/MFLO
3799 //if(dops[i].opcode2==0x11||dops[i].opcode2==0x13) { // MTHI/MTLO
3802 tl=get_reg(i_regs->regmap,dops[i].rt1);
3805 sl=get_reg(i_regs->regmap,dops[i].rs1);
3806 if(sl>=0) emit_mov(sl,tl);
3807 else emit_loadreg(dops[i].rs1,tl);
3810 if (dops[i].rs1 == HIREG || dops[i].rs1 == LOREG) // MFHI/MFLO
3811 multdiv_do_stall(i, i_regs);
3814 // call interpreter, exception handler, things that change pc/regs/cycles ...
3815 static void call_c_cpu_handler(int i, const struct regstat *i_regs, u_int pc, void *func)
3817 signed char ccreg=get_reg(i_regs->regmap,CCREG);
3818 assert(ccreg==HOST_CCREG);
3819 assert(!is_delayslot);
3822 emit_movimm(pc,3); // Get PC
3823 emit_readword(&last_count,2);
3824 emit_writeword(3,&psxRegs.pc);
3825 emit_addimm(HOST_CCREG,CLOCK_ADJUST(ccadj[i]),HOST_CCREG); // XXX
3826 emit_add(2,HOST_CCREG,2);
3827 emit_writeword(2,&psxRegs.cycle);
3828 emit_far_call(func);
3829 emit_far_jump(jump_to_new_pc);
3832 static void syscall_assemble(int i,struct regstat *i_regs)
3834 emit_movimm(0x20,0); // cause code
3835 emit_movimm(0,1); // not in delay slot
3836 call_c_cpu_handler(i,i_regs,start+i*4,psxException);
3839 static void hlecall_assemble(int i,struct regstat *i_regs)
3841 void *hlefunc = psxNULL;
3842 uint32_t hleCode = source[i] & 0x03ffffff;
3843 if (hleCode < ARRAY_SIZE(psxHLEt))
3844 hlefunc = psxHLEt[hleCode];
3846 call_c_cpu_handler(i,i_regs,start+i*4+4,hlefunc);
3849 static void intcall_assemble(int i,struct regstat *i_regs)
3851 call_c_cpu_handler(i,i_regs,start+i*4,execI);
3854 static void speculate_mov(int rs,int rt)
3857 smrv_strong_next|=1<<rt;
3862 static void speculate_mov_weak(int rs,int rt)
3865 smrv_weak_next|=1<<rt;
3870 static void speculate_register_values(int i)
3873 memcpy(smrv,psxRegs.GPR.r,sizeof(smrv));
3874 // gp,sp are likely to stay the same throughout the block
3875 smrv_strong_next=(1<<28)|(1<<29)|(1<<30);
3876 smrv_weak_next=~smrv_strong_next;
3877 //printf(" llr %08x\n", smrv[4]);
3879 smrv_strong=smrv_strong_next;
3880 smrv_weak=smrv_weak_next;
3881 switch(dops[i].itype) {
3883 if ((smrv_strong>>dops[i].rs1)&1) speculate_mov(dops[i].rs1,dops[i].rt1);
3884 else if((smrv_strong>>dops[i].rs2)&1) speculate_mov(dops[i].rs2,dops[i].rt1);
3885 else if((smrv_weak>>dops[i].rs1)&1) speculate_mov_weak(dops[i].rs1,dops[i].rt1);
3886 else if((smrv_weak>>dops[i].rs2)&1) speculate_mov_weak(dops[i].rs2,dops[i].rt1);
3888 smrv_strong_next&=~(1<<dops[i].rt1);
3889 smrv_weak_next&=~(1<<dops[i].rt1);
3893 smrv_strong_next&=~(1<<dops[i].rt1);
3894 smrv_weak_next&=~(1<<dops[i].rt1);
3897 if(dops[i].rt1&&is_const(®s[i],dops[i].rt1)) {
3898 int value,hr=get_reg(regs[i].regmap,dops[i].rt1);
3900 if(get_final_value(hr,i,&value))
3901 smrv[dops[i].rt1]=value;
3902 else smrv[dops[i].rt1]=constmap[i][hr];
3903 smrv_strong_next|=1<<dops[i].rt1;
3907 if ((smrv_strong>>dops[i].rs1)&1) speculate_mov(dops[i].rs1,dops[i].rt1);
3908 else if((smrv_weak>>dops[i].rs1)&1) speculate_mov_weak(dops[i].rs1,dops[i].rt1);
3912 if(start<0x2000&&(dops[i].rt1==26||(smrv[dops[i].rt1]>>24)==0xa0)) {
3913 // special case for BIOS
3914 smrv[dops[i].rt1]=0xa0000000;
3915 smrv_strong_next|=1<<dops[i].rt1;
3922 smrv_strong_next&=~(1<<dops[i].rt1);
3923 smrv_weak_next&=~(1<<dops[i].rt1);
3927 if(dops[i].opcode2==0||dops[i].opcode2==2) { // MFC/CFC
3928 smrv_strong_next&=~(1<<dops[i].rt1);
3929 smrv_weak_next&=~(1<<dops[i].rt1);
3933 if (dops[i].opcode==0x32) { // LWC2
3934 smrv_strong_next&=~(1<<dops[i].rt1);
3935 smrv_weak_next&=~(1<<dops[i].rt1);
3941 printf("x %08x %08x %d %d c %08x %08x\n",smrv[r],start+i*4,
3942 ((smrv_strong>>r)&1),(smrv_weak>>r)&1,regs[i].isconst,regs[i].wasconst);
3946 static void ds_assemble(int i,struct regstat *i_regs)
3948 speculate_register_values(i);
3950 switch(dops[i].itype) {
3952 alu_assemble(i,i_regs);break;
3954 imm16_assemble(i,i_regs);break;
3956 shift_assemble(i,i_regs);break;
3958 shiftimm_assemble(i,i_regs);break;
3960 load_assemble(i,i_regs);break;
3962 loadlr_assemble(i,i_regs);break;
3964 store_assemble(i,i_regs);break;
3966 storelr_assemble(i,i_regs);break;
3968 cop0_assemble(i,i_regs);break;
3970 cop1_assemble(i,i_regs);break;
3972 c1ls_assemble(i,i_regs);break;
3974 cop2_assemble(i,i_regs);break;
3976 c2ls_assemble(i,i_regs);break;
3978 c2op_assemble(i,i_regs);break;
3980 multdiv_assemble(i,i_regs);
3981 multdiv_prepare_stall(i,i_regs);
3984 mov_assemble(i,i_regs);break;
3993 SysPrintf("Jump in the delay slot. This is probably a bug.\n");
3998 // Is the branch target a valid internal jump?
3999 static int internal_branch(int addr)
4001 if(addr&1) return 0; // Indirect (register) jump
4002 if(addr>=start && addr<start+slen*4-4)
4009 static void wb_invalidate(signed char pre[],signed char entry[],uint64_t dirty,uint64_t u)
4012 for(hr=0;hr<HOST_REGS;hr++) {
4013 if(hr!=EXCLUDE_REG) {
4014 if(pre[hr]!=entry[hr]) {
4017 if(get_reg(entry,pre[hr])<0) {
4019 if(!((u>>pre[hr])&1))
4020 emit_storereg(pre[hr],hr);
4027 // Move from one register to another (no writeback)
4028 for(hr=0;hr<HOST_REGS;hr++) {
4029 if(hr!=EXCLUDE_REG) {
4030 if(pre[hr]!=entry[hr]) {
4031 if(pre[hr]>=0&&(pre[hr]&63)<TEMPREG) {
4033 if((nr=get_reg(entry,pre[hr]))>=0) {
4042 // Load the specified registers
4043 // This only loads the registers given as arguments because
4044 // we don't want to load things that will be overwritten
4045 static void load_regs(signed char entry[],signed char regmap[],int rs1,int rs2)
4049 for(hr=0;hr<HOST_REGS;hr++) {
4050 if(hr!=EXCLUDE_REG&®map[hr]>=0) {
4051 if(entry[hr]!=regmap[hr]) {
4052 if(regmap[hr]==rs1||regmap[hr]==rs2)
4059 emit_loadreg(regmap[hr],hr);
4067 // Load registers prior to the start of a loop
4068 // so that they are not loaded within the loop
4069 static void loop_preload(signed char pre[],signed char entry[])
4072 for(hr=0;hr<HOST_REGS;hr++) {
4073 if(hr!=EXCLUDE_REG) {
4074 if(pre[hr]!=entry[hr]) {
4076 if(get_reg(pre,entry[hr])<0) {
4077 assem_debug("loop preload:\n");
4078 //printf("loop preload: %d\n",hr);
4082 else if(entry[hr]<TEMPREG)
4084 emit_loadreg(entry[hr],hr);
4086 else if(entry[hr]-64<TEMPREG)
4088 emit_loadreg(entry[hr],hr);
4097 // Generate address for load/store instruction
4098 // goes to AGEN for writes, FTEMP for LOADLR and cop1/2 loads
4099 void address_generation(int i,struct regstat *i_regs,signed char entry[])
4101 if(dops[i].itype==LOAD||dops[i].itype==LOADLR||dops[i].itype==STORE||dops[i].itype==STORELR||dops[i].itype==C1LS||dops[i].itype==C2LS) {
4103 int agr=AGEN1+(i&1);
4104 if(dops[i].itype==LOAD) {
4105 ra=get_reg(i_regs->regmap,dops[i].rt1);
4106 if(ra<0) ra=get_reg(i_regs->regmap,-1);
4109 if(dops[i].itype==LOADLR) {
4110 ra=get_reg(i_regs->regmap,FTEMP);
4112 if(dops[i].itype==STORE||dops[i].itype==STORELR) {
4113 ra=get_reg(i_regs->regmap,agr);
4114 if(ra<0) ra=get_reg(i_regs->regmap,-1);
4116 if(dops[i].itype==C1LS||dops[i].itype==C2LS) {
4117 if ((dops[i].opcode&0x3b)==0x31||(dops[i].opcode&0x3b)==0x32) // LWC1/LDC1/LWC2/LDC2
4118 ra=get_reg(i_regs->regmap,FTEMP);
4119 else { // SWC1/SDC1/SWC2/SDC2
4120 ra=get_reg(i_regs->regmap,agr);
4121 if(ra<0) ra=get_reg(i_regs->regmap,-1);
4124 int rs=get_reg(i_regs->regmap,dops[i].rs1);
4127 int c=(i_regs->wasconst>>rs)&1;
4128 if(dops[i].rs1==0) {
4129 // Using r0 as a base address
4130 if(!entry||entry[ra]!=agr) {
4131 if (dops[i].opcode==0x22||dops[i].opcode==0x26) {
4132 emit_movimm(offset&0xFFFFFFFC,ra); // LWL/LWR
4133 }else if (dops[i].opcode==0x1a||dops[i].opcode==0x1b) {
4134 emit_movimm(offset&0xFFFFFFF8,ra); // LDL/LDR
4136 emit_movimm(offset,ra);
4138 } // else did it in the previous cycle
4141 if(!entry||entry[ra]!=dops[i].rs1)
4142 emit_loadreg(dops[i].rs1,ra);
4143 //if(!entry||entry[ra]!=dops[i].rs1)
4144 // printf("poor load scheduling!\n");
4147 if(dops[i].rs1!=dops[i].rt1||dops[i].itype!=LOAD) {
4148 if(!entry||entry[ra]!=agr) {
4149 if (dops[i].opcode==0x22||dops[i].opcode==0x26) {
4150 emit_movimm((constmap[i][rs]+offset)&0xFFFFFFFC,ra); // LWL/LWR
4151 }else if (dops[i].opcode==0x1a||dops[i].opcode==0x1b) {
4152 emit_movimm((constmap[i][rs]+offset)&0xFFFFFFF8,ra); // LDL/LDR
4154 emit_movimm(constmap[i][rs]+offset,ra);
4155 regs[i].loadedconst|=1<<ra;
4157 } // else did it in the previous cycle
4158 } // else load_consts already did it
4160 if(offset&&!c&&dops[i].rs1) {
4162 emit_addimm(rs,offset,ra);
4164 emit_addimm(ra,offset,ra);
4169 // Preload constants for next instruction
4170 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) {
4173 agr=AGEN1+((i+1)&1);
4174 ra=get_reg(i_regs->regmap,agr);
4176 int rs=get_reg(regs[i+1].regmap,dops[i+1].rs1);
4177 int offset=imm[i+1];
4178 int c=(regs[i+1].wasconst>>rs)&1;
4179 if(c&&(dops[i+1].rs1!=dops[i+1].rt1||dops[i+1].itype!=LOAD)) {
4180 if (dops[i+1].opcode==0x22||dops[i+1].opcode==0x26) {
4181 emit_movimm((constmap[i+1][rs]+offset)&0xFFFFFFFC,ra); // LWL/LWR
4182 }else if (dops[i+1].opcode==0x1a||dops[i+1].opcode==0x1b) {
4183 emit_movimm((constmap[i+1][rs]+offset)&0xFFFFFFF8,ra); // LDL/LDR
4185 emit_movimm(constmap[i+1][rs]+offset,ra);
4186 regs[i+1].loadedconst|=1<<ra;
4189 else if(dops[i+1].rs1==0) {
4190 // Using r0 as a base address
4191 if (dops[i+1].opcode==0x22||dops[i+1].opcode==0x26) {
4192 emit_movimm(offset&0xFFFFFFFC,ra); // LWL/LWR
4193 }else if (dops[i+1].opcode==0x1a||dops[i+1].opcode==0x1b) {
4194 emit_movimm(offset&0xFFFFFFF8,ra); // LDL/LDR
4196 emit_movimm(offset,ra);
4203 static int get_final_value(int hr, int i, int *value)
4205 int reg=regs[i].regmap[hr];
4207 if(regs[i+1].regmap[hr]!=reg) break;
4208 if(!((regs[i+1].isconst>>hr)&1)) break;
4209 if(dops[i+1].bt) break;
4213 if(dops[i].itype==UJUMP||dops[i].itype==RJUMP||dops[i].itype==CJUMP||dops[i].itype==SJUMP) {
4214 *value=constmap[i][hr];
4218 if(dops[i+1].itype==UJUMP||dops[i+1].itype==RJUMP||dops[i+1].itype==CJUMP||dops[i+1].itype==SJUMP) {
4219 // Load in delay slot, out-of-order execution
4220 if(dops[i+2].itype==LOAD&&dops[i+2].rs1==reg&&dops[i+2].rt1==reg&&((regs[i+1].wasconst>>hr)&1))
4222 // Precompute load address
4223 *value=constmap[i][hr]+imm[i+2];
4227 if(dops[i+1].itype==LOAD&&dops[i+1].rs1==reg&&dops[i+1].rt1==reg)
4229 // Precompute load address
4230 *value=constmap[i][hr]+imm[i+1];
4231 //printf("c=%x imm=%lx\n",(long)constmap[i][hr],imm[i+1]);
4236 *value=constmap[i][hr];
4237 //printf("c=%lx\n",(long)constmap[i][hr]);
4238 if(i==slen-1) return 1;
4240 return !((unneeded_reg[i+1]>>reg)&1);
4243 // Load registers with known constants
4244 static void load_consts(signed char pre[],signed char regmap[],int i)
4247 // propagate loaded constant flags
4248 if(i==0||dops[i].bt)
4249 regs[i].loadedconst=0;
4251 for(hr=0;hr<HOST_REGS;hr++) {
4252 if(hr!=EXCLUDE_REG&®map[hr]>=0&&((regs[i-1].isconst>>hr)&1)&&pre[hr]==regmap[hr]
4253 &®map[hr]==regs[i-1].regmap[hr]&&((regs[i-1].loadedconst>>hr)&1))
4255 regs[i].loadedconst|=1<<hr;
4260 for(hr=0;hr<HOST_REGS;hr++) {
4261 if(hr!=EXCLUDE_REG&®map[hr]>=0) {
4262 //if(entry[hr]!=regmap[hr]) {
4263 if(!((regs[i].loadedconst>>hr)&1)) {
4264 assert(regmap[hr]<64);
4265 if(((regs[i].isconst>>hr)&1)&®map[hr]>0) {
4266 int value,similar=0;
4267 if(get_final_value(hr,i,&value)) {
4268 // see if some other register has similar value
4269 for(hr2=0;hr2<HOST_REGS;hr2++) {
4270 if(hr2!=EXCLUDE_REG&&((regs[i].loadedconst>>hr2)&1)) {
4271 if(is_similar_value(value,constmap[i][hr2])) {
4279 if(get_final_value(hr2,i,&value2)) // is this needed?
4280 emit_movimm_from(value2,hr2,value,hr);
4282 emit_movimm(value,hr);
4288 emit_movimm(value,hr);
4291 regs[i].loadedconst|=1<<hr;
4298 void load_all_consts(signed char regmap[], u_int dirty, int i)
4302 for(hr=0;hr<HOST_REGS;hr++) {
4303 if(hr!=EXCLUDE_REG&®map[hr]>=0&&((dirty>>hr)&1)) {
4304 assert(regmap[hr] < 64);
4305 if(((regs[i].isconst>>hr)&1)&®map[hr]>0) {
4306 int value=constmap[i][hr];
4311 emit_movimm(value,hr);
4318 // Write out all dirty registers (except cycle count)
4319 static void wb_dirtys(signed char i_regmap[],uint64_t i_dirty)
4322 for(hr=0;hr<HOST_REGS;hr++) {
4323 if(hr!=EXCLUDE_REG) {
4324 if(i_regmap[hr]>0) {
4325 if(i_regmap[hr]!=CCREG) {
4326 if((i_dirty>>hr)&1) {
4327 assert(i_regmap[hr]<64);
4328 emit_storereg(i_regmap[hr],hr);
4336 // Write out dirty registers that we need to reload (pair with load_needed_regs)
4337 // This writes the registers not written by store_regs_bt
4338 void wb_needed_dirtys(signed char i_regmap[],uint64_t i_dirty,int addr)
4341 int t=(addr-start)>>2;
4342 for(hr=0;hr<HOST_REGS;hr++) {
4343 if(hr!=EXCLUDE_REG) {
4344 if(i_regmap[hr]>0) {
4345 if(i_regmap[hr]!=CCREG) {
4346 if(i_regmap[hr]==regs[t].regmap_entry[hr] && ((regs[t].dirty>>hr)&1)) {
4347 if((i_dirty>>hr)&1) {
4348 assert(i_regmap[hr]<64);
4349 emit_storereg(i_regmap[hr],hr);
4358 // Load all registers (except cycle count)
4359 void load_all_regs(signed char i_regmap[])
4362 for(hr=0;hr<HOST_REGS;hr++) {
4363 if(hr!=EXCLUDE_REG) {
4364 if(i_regmap[hr]==0) {
4368 if(i_regmap[hr]>0 && (i_regmap[hr]&63)<TEMPREG && i_regmap[hr]!=CCREG)
4370 emit_loadreg(i_regmap[hr],hr);
4376 // Load all current registers also needed by next instruction
4377 void load_needed_regs(signed char i_regmap[],signed char next_regmap[])
4380 for(hr=0;hr<HOST_REGS;hr++) {
4381 if(hr!=EXCLUDE_REG) {
4382 if(get_reg(next_regmap,i_regmap[hr])>=0) {
4383 if(i_regmap[hr]==0) {
4387 if(i_regmap[hr]>0 && (i_regmap[hr]&63)<TEMPREG && i_regmap[hr]!=CCREG)
4389 emit_loadreg(i_regmap[hr],hr);
4396 // Load all regs, storing cycle count if necessary
4397 void load_regs_entry(int t)
4400 if(dops[t].is_ds) emit_addimm(HOST_CCREG,CLOCK_ADJUST(1),HOST_CCREG);
4401 else if(ccadj[t]) emit_addimm(HOST_CCREG,-CLOCK_ADJUST(ccadj[t]),HOST_CCREG);
4402 if(regs[t].regmap_entry[HOST_CCREG]!=CCREG) {
4403 emit_storereg(CCREG,HOST_CCREG);
4406 for(hr=0;hr<HOST_REGS;hr++) {
4407 if(regs[t].regmap_entry[hr]>=0&®s[t].regmap_entry[hr]<TEMPREG) {
4408 if(regs[t].regmap_entry[hr]==0) {
4411 else if(regs[t].regmap_entry[hr]!=CCREG)
4413 emit_loadreg(regs[t].regmap_entry[hr],hr);
4419 // Store dirty registers prior to branch
4420 void store_regs_bt(signed char i_regmap[],uint64_t i_dirty,int addr)
4422 if(internal_branch(addr))
4424 int t=(addr-start)>>2;
4426 for(hr=0;hr<HOST_REGS;hr++) {
4427 if(hr!=EXCLUDE_REG) {
4428 if(i_regmap[hr]>0 && i_regmap[hr]!=CCREG) {
4429 if(i_regmap[hr]!=regs[t].regmap_entry[hr] || !((regs[t].dirty>>hr)&1)) {
4430 if((i_dirty>>hr)&1) {
4431 assert(i_regmap[hr]<64);
4432 if(!((unneeded_reg[t]>>i_regmap[hr])&1))
4433 emit_storereg(i_regmap[hr],hr);
4442 // Branch out of this block, write out all dirty regs
4443 wb_dirtys(i_regmap,i_dirty);
4447 // Load all needed registers for branch target
4448 static void load_regs_bt(signed char i_regmap[],uint64_t i_dirty,int addr)
4450 //if(addr>=start && addr<(start+slen*4))
4451 if(internal_branch(addr))
4453 int t=(addr-start)>>2;
4455 // Store the cycle count before loading something else
4456 if(i_regmap[HOST_CCREG]!=CCREG) {
4457 assert(i_regmap[HOST_CCREG]==-1);
4459 if(regs[t].regmap_entry[HOST_CCREG]!=CCREG) {
4460 emit_storereg(CCREG,HOST_CCREG);
4463 for(hr=0;hr<HOST_REGS;hr++) {
4464 if(hr!=EXCLUDE_REG&®s[t].regmap_entry[hr]>=0&®s[t].regmap_entry[hr]<TEMPREG) {
4465 if(i_regmap[hr]!=regs[t].regmap_entry[hr]) {
4466 if(regs[t].regmap_entry[hr]==0) {
4469 else if(regs[t].regmap_entry[hr]!=CCREG)
4471 emit_loadreg(regs[t].regmap_entry[hr],hr);
4479 static int match_bt(signed char i_regmap[],uint64_t i_dirty,int addr)
4481 if(addr>=start && addr<start+slen*4-4)
4483 int t=(addr-start)>>2;
4485 if(regs[t].regmap_entry[HOST_CCREG]!=CCREG) return 0;
4486 for(hr=0;hr<HOST_REGS;hr++)
4490 if(i_regmap[hr]!=regs[t].regmap_entry[hr])
4492 if(regs[t].regmap_entry[hr]>=0&&(regs[t].regmap_entry[hr]|64)<TEMPREG+64)
4499 if(i_regmap[hr]<TEMPREG)
4501 if(!((unneeded_reg[t]>>i_regmap[hr])&1))
4504 else if(i_regmap[hr]>=64&&i_regmap[hr]<TEMPREG+64)
4510 else // Same register but is it 32-bit or dirty?
4513 if(!((regs[t].dirty>>hr)&1))
4517 if(!((unneeded_reg[t]>>i_regmap[hr])&1))
4519 //printf("%x: dirty no match\n",addr);
4527 // Delay slots are not valid branch targets
4528 //if(t>0&&(dops[t-1].itype==RJUMP||dops[t-1].itype==UJUMP||dops[t-1].itype==CJUMP||dops[t-1].itype==SJUMP)) return 0;
4529 // Delay slots require additional processing, so do not match
4530 if(dops[t].is_ds) return 0;
4535 for(hr=0;hr<HOST_REGS;hr++)
4541 if(hr!=HOST_CCREG||i_regmap[hr]!=CCREG)
4556 static void drc_dbg_emit_do_cmp(int i)
4558 extern void do_insn_cmp();
4560 u_int hr, reglist = get_host_reglist(regs[i].regmap);
4562 assem_debug("//do_insn_cmp %08x\n", start+i*4);
4564 // write out changed consts to match the interpreter
4565 if (i > 0 && !dops[i].bt) {
4566 for (hr = 0; hr < HOST_REGS; hr++) {
4567 int reg = regs[i-1].regmap[hr];
4568 if (hr == EXCLUDE_REG || reg < 0)
4570 if (!((regs[i-1].isconst >> hr) & 1))
4572 if (i > 1 && reg == regs[i-2].regmap[hr] && constmap[i-1][hr] == constmap[i-2][hr])
4574 emit_movimm(constmap[i-1][hr],0);
4575 emit_storereg(reg, 0);
4578 emit_movimm(start+i*4,0);
4579 emit_writeword(0,&pcaddr);
4580 emit_far_call(do_insn_cmp);
4581 //emit_readword(&cycle,0);
4582 //emit_addimm(0,2,0);
4583 //emit_writeword(0,&cycle);
4585 restore_regs(reglist);
4586 assem_debug("\\\\do_insn_cmp\n");
4589 #define drc_dbg_emit_do_cmp(x)
4592 // Used when a branch jumps into the delay slot of another branch
4593 static void ds_assemble_entry(int i)
4595 int t=(ba[i]-start)>>2;
4597 instr_addr[t] = out;
4598 assem_debug("Assemble delay slot at %x\n",ba[i]);
4599 assem_debug("<->\n");
4600 drc_dbg_emit_do_cmp(t);
4601 if(regs[t].regmap_entry[HOST_CCREG]==CCREG&®s[t].regmap[HOST_CCREG]!=CCREG)
4602 wb_register(CCREG,regs[t].regmap_entry,regs[t].wasdirty);
4603 load_regs(regs[t].regmap_entry,regs[t].regmap,dops[t].rs1,dops[t].rs2);
4604 address_generation(t,®s[t],regs[t].regmap_entry);
4605 if(dops[t].itype==STORE||dops[t].itype==STORELR||(dops[t].opcode&0x3b)==0x39||(dops[t].opcode&0x3b)==0x3a)
4606 load_regs(regs[t].regmap_entry,regs[t].regmap,INVCP,INVCP);
4608 switch(dops[t].itype) {
4610 alu_assemble(t,®s[t]);break;
4612 imm16_assemble(t,®s[t]);break;
4614 shift_assemble(t,®s[t]);break;
4616 shiftimm_assemble(t,®s[t]);break;
4618 load_assemble(t,®s[t]);break;
4620 loadlr_assemble(t,®s[t]);break;
4622 store_assemble(t,®s[t]);break;
4624 storelr_assemble(t,®s[t]);break;
4626 cop0_assemble(t,®s[t]);break;
4628 cop1_assemble(t,®s[t]);break;
4630 c1ls_assemble(t,®s[t]);break;
4632 cop2_assemble(t,®s[t]);break;
4634 c2ls_assemble(t,®s[t]);break;
4636 c2op_assemble(t,®s[t]);break;
4638 multdiv_assemble(t,®s[t]);
4639 multdiv_prepare_stall(i,®s[t]);
4642 mov_assemble(t,®s[t]);break;
4651 SysPrintf("Jump in the delay slot. This is probably a bug.\n");
4653 store_regs_bt(regs[t].regmap,regs[t].dirty,ba[i]+4);
4654 load_regs_bt(regs[t].regmap,regs[t].dirty,ba[i]+4);
4655 if(internal_branch(ba[i]+4))
4656 assem_debug("branch: internal\n");
4658 assem_debug("branch: external\n");
4659 assert(internal_branch(ba[i]+4));
4660 add_to_linker(out,ba[i]+4,internal_branch(ba[i]+4));
4664 static void emit_extjump(void *addr, u_int target)
4666 emit_extjump2(addr, target, dyna_linker);
4669 static void emit_extjump_ds(void *addr, u_int target)
4671 emit_extjump2(addr, target, dyna_linker_ds);
4674 // Load 2 immediates optimizing for small code size
4675 static void emit_mov2imm_compact(int imm1,u_int rt1,int imm2,u_int rt2)
4677 emit_movimm(imm1,rt1);
4678 emit_movimm_from(imm1,rt1,imm2,rt2);
4681 void do_cc(int i,signed char i_regmap[],int *adj,int addr,int taken,int invert)
4687 if(dops[i].itype==RJUMP)
4691 //if(ba[i]>=start && ba[i]<(start+slen*4))
4692 if(internal_branch(ba[i]))
4695 if(dops[t].is_ds) *adj=-1; // Branch into delay slot adds an extra cycle
4703 if(taken==TAKEN && i==(ba[i]-start)>>2 && source[i+1]==0) {
4705 if(count&1) emit_addimm_and_set_flags(2*(count+2),HOST_CCREG);
4707 //emit_subfrommem(&idlecount,HOST_CCREG); // Count idle cycles
4708 emit_andimm(HOST_CCREG,3,HOST_CCREG);
4712 else if(*adj==0||invert) {
4713 int cycles=CLOCK_ADJUST(count+2);
4718 if(-NO_CYCLE_PENALTY_THR<rel&&rel<0)
4719 cycles=CLOCK_ADJUST(*adj)+count+2-*adj;
4722 emit_addimm_and_set_flags(cycles,HOST_CCREG);
4728 emit_cmpimm(HOST_CCREG,-CLOCK_ADJUST(count+2));
4732 add_stub(CC_STUB,jaddr,idle?idle:out,(*adj==0||invert||idle)?0:(count+2),i,addr,taken,0);
4735 static void do_ccstub(int n)
4738 assem_debug("do_ccstub %x\n",start+(u_int)stubs[n].b*4);
4739 set_jump_target(stubs[n].addr, out);
4741 if(stubs[n].d==NULLDS) {
4742 // Delay slot instruction is nullified ("likely" branch)
4743 wb_dirtys(regs[i].regmap,regs[i].dirty);
4745 else if(stubs[n].d!=TAKEN) {
4746 wb_dirtys(branch_regs[i].regmap,branch_regs[i].dirty);
4749 if(internal_branch(ba[i]))
4750 wb_needed_dirtys(branch_regs[i].regmap,branch_regs[i].dirty,ba[i]);
4754 // Save PC as return address
4755 emit_movimm(stubs[n].c,EAX);
4756 emit_writeword(EAX,&pcaddr);
4760 // Return address depends on which way the branch goes
4761 if(dops[i].itype==CJUMP||dops[i].itype==SJUMP)
4763 int s1l=get_reg(branch_regs[i].regmap,dops[i].rs1);
4764 int s2l=get_reg(branch_regs[i].regmap,dops[i].rs2);
4770 else if(dops[i].rs2==0)
4775 #ifdef DESTRUCTIVE_WRITEBACK
4777 if((branch_regs[i].dirty>>s1l)&&1)
4778 emit_loadreg(dops[i].rs1,s1l);
4781 if((branch_regs[i].dirty>>s1l)&1)
4782 emit_loadreg(dops[i].rs2,s1l);
4785 if((branch_regs[i].dirty>>s2l)&1)
4786 emit_loadreg(dops[i].rs2,s2l);
4789 int addr=-1,alt=-1,ntaddr=-1;
4792 if(hr!=EXCLUDE_REG && hr!=HOST_CCREG &&
4793 (branch_regs[i].regmap[hr]&63)!=dops[i].rs1 &&
4794 (branch_regs[i].regmap[hr]&63)!=dops[i].rs2 )
4802 if(hr!=EXCLUDE_REG && hr!=HOST_CCREG &&
4803 (branch_regs[i].regmap[hr]&63)!=dops[i].rs1 &&
4804 (branch_regs[i].regmap[hr]&63)!=dops[i].rs2 )
4810 if((dops[i].opcode&0x2E)==6) // BLEZ/BGTZ needs another register
4814 if(hr!=EXCLUDE_REG && hr!=HOST_CCREG &&
4815 (branch_regs[i].regmap[hr]&63)!=dops[i].rs1 &&
4816 (branch_regs[i].regmap[hr]&63)!=dops[i].rs2 )
4822 assert(hr<HOST_REGS);
4824 if((dops[i].opcode&0x2f)==4) // BEQ
4826 #ifdef HAVE_CMOV_IMM
4827 if(s2l>=0) emit_cmp(s1l,s2l);
4828 else emit_test(s1l,s1l);
4829 emit_cmov2imm_e_ne_compact(ba[i],start+i*4+8,addr);
4831 emit_mov2imm_compact(ba[i],addr,start+i*4+8,alt);
4832 if(s2l>=0) emit_cmp(s1l,s2l);
4833 else emit_test(s1l,s1l);
4834 emit_cmovne_reg(alt,addr);
4837 if((dops[i].opcode&0x2f)==5) // BNE
4839 #ifdef HAVE_CMOV_IMM
4840 if(s2l>=0) emit_cmp(s1l,s2l);
4841 else emit_test(s1l,s1l);
4842 emit_cmov2imm_e_ne_compact(start+i*4+8,ba[i],addr);
4844 emit_mov2imm_compact(start+i*4+8,addr,ba[i],alt);
4845 if(s2l>=0) emit_cmp(s1l,s2l);
4846 else emit_test(s1l,s1l);
4847 emit_cmovne_reg(alt,addr);
4850 if((dops[i].opcode&0x2f)==6) // BLEZ
4852 //emit_movimm(ba[i],alt);
4853 //emit_movimm(start+i*4+8,addr);
4854 emit_mov2imm_compact(ba[i],alt,start+i*4+8,addr);
4856 emit_cmovl_reg(alt,addr);
4858 if((dops[i].opcode&0x2f)==7) // BGTZ
4860 //emit_movimm(ba[i],addr);
4861 //emit_movimm(start+i*4+8,ntaddr);
4862 emit_mov2imm_compact(ba[i],addr,start+i*4+8,ntaddr);
4864 emit_cmovl_reg(ntaddr,addr);
4866 if((dops[i].opcode==1)&&(dops[i].opcode2&0x2D)==0) // BLTZ
4868 //emit_movimm(ba[i],alt);
4869 //emit_movimm(start+i*4+8,addr);
4870 emit_mov2imm_compact(ba[i],alt,start+i*4+8,addr);
4872 emit_cmovs_reg(alt,addr);
4874 if((dops[i].opcode==1)&&(dops[i].opcode2&0x2D)==1) // BGEZ
4876 //emit_movimm(ba[i],addr);
4877 //emit_movimm(start+i*4+8,alt);
4878 emit_mov2imm_compact(ba[i],addr,start+i*4+8,alt);
4880 emit_cmovs_reg(alt,addr);
4882 if(dops[i].opcode==0x11 && dops[i].opcode2==0x08 ) {
4883 if(source[i]&0x10000) // BC1T
4885 //emit_movimm(ba[i],alt);
4886 //emit_movimm(start+i*4+8,addr);
4887 emit_mov2imm_compact(ba[i],alt,start+i*4+8,addr);
4888 emit_testimm(s1l,0x800000);
4889 emit_cmovne_reg(alt,addr);
4893 //emit_movimm(ba[i],addr);
4894 //emit_movimm(start+i*4+8,alt);
4895 emit_mov2imm_compact(ba[i],addr,start+i*4+8,alt);
4896 emit_testimm(s1l,0x800000);
4897 emit_cmovne_reg(alt,addr);
4900 emit_writeword(addr,&pcaddr);
4903 if(dops[i].itype==RJUMP)
4905 int r=get_reg(branch_regs[i].regmap,dops[i].rs1);
4906 if (ds_writes_rjump_rs(i)) {
4907 r=get_reg(branch_regs[i].regmap,RTEMP);
4909 emit_writeword(r,&pcaddr);
4911 else {SysPrintf("Unknown branch type in do_ccstub\n");abort();}
4913 // Update cycle count
4914 assert(branch_regs[i].regmap[HOST_CCREG]==CCREG||branch_regs[i].regmap[HOST_CCREG]==-1);
4915 if(stubs[n].a) emit_addimm(HOST_CCREG,CLOCK_ADJUST((signed int)stubs[n].a),HOST_CCREG);
4916 emit_far_call(cc_interrupt);
4917 if(stubs[n].a) emit_addimm(HOST_CCREG,-CLOCK_ADJUST((signed int)stubs[n].a),HOST_CCREG);
4918 if(stubs[n].d==TAKEN) {
4919 if(internal_branch(ba[i]))
4920 load_needed_regs(branch_regs[i].regmap,regs[(ba[i]-start)>>2].regmap_entry);
4921 else if(dops[i].itype==RJUMP) {
4922 if(get_reg(branch_regs[i].regmap,RTEMP)>=0)
4923 emit_readword(&pcaddr,get_reg(branch_regs[i].regmap,RTEMP));
4925 emit_loadreg(dops[i].rs1,get_reg(branch_regs[i].regmap,dops[i].rs1));
4927 }else if(stubs[n].d==NOTTAKEN) {
4928 if(i<slen-2) load_needed_regs(branch_regs[i].regmap,regmap_pre[i+2]);
4929 else load_all_regs(branch_regs[i].regmap);
4930 }else if(stubs[n].d==NULLDS) {
4931 // Delay slot instruction is nullified ("likely" branch)
4932 if(i<slen-2) load_needed_regs(regs[i].regmap,regmap_pre[i+2]);
4933 else load_all_regs(regs[i].regmap);
4935 load_all_regs(branch_regs[i].regmap);
4937 if (stubs[n].retaddr)
4938 emit_jmp(stubs[n].retaddr);
4940 do_jump_vaddr(stubs[n].e);
4943 static void add_to_linker(void *addr, u_int target, int ext)
4945 assert(linkcount < ARRAY_SIZE(link_addr));
4946 link_addr[linkcount].addr = addr;
4947 link_addr[linkcount].target = target;
4948 link_addr[linkcount].ext = ext;
4952 static void ujump_assemble_write_ra(int i)
4955 unsigned int return_address;
4956 rt=get_reg(branch_regs[i].regmap,31);
4957 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]);
4959 return_address=start+i*4+8;
4962 if(internal_branch(return_address)&&dops[i+1].rt1!=31) {
4963 int temp=-1; // note: must be ds-safe
4967 if(temp>=0) do_miniht_insert(return_address,rt,temp);
4968 else emit_movimm(return_address,rt);
4976 if(i_regmap[temp]!=PTEMP) emit_movimm((uintptr_t)hash_table_get(return_address),temp);
4979 emit_movimm(return_address,rt); // PC into link register
4981 emit_prefetch(hash_table_get(return_address));
4987 static void ujump_assemble(int i,struct regstat *i_regs)
4990 if(i==(ba[i]-start)>>2) assem_debug("idle loop\n");
4991 address_generation(i+1,i_regs,regs[i].regmap_entry);
4993 int temp=get_reg(branch_regs[i].regmap,PTEMP);
4994 if(dops[i].rt1==31&&temp>=0)
4996 signed char *i_regmap=i_regs->regmap;
4997 int return_address=start+i*4+8;
4998 if(get_reg(branch_regs[i].regmap,31)>0)
4999 if(i_regmap[temp]==PTEMP) emit_movimm((uintptr_t)hash_table_get(return_address),temp);
5002 if(dops[i].rt1==31&&(dops[i].rt1==dops[i+1].rs1||dops[i].rt1==dops[i+1].rs2)) {
5003 ujump_assemble_write_ra(i); // writeback ra for DS
5006 ds_assemble(i+1,i_regs);
5007 uint64_t bc_unneeded=branch_regs[i].u;
5008 bc_unneeded|=1|(1LL<<dops[i].rt1);
5009 wb_invalidate(regs[i].regmap,branch_regs[i].regmap,regs[i].dirty,bc_unneeded);
5010 load_regs(regs[i].regmap,branch_regs[i].regmap,CCREG,CCREG);
5011 if(!ra_done&&dops[i].rt1==31)
5012 ujump_assemble_write_ra(i);
5014 cc=get_reg(branch_regs[i].regmap,CCREG);
5015 assert(cc==HOST_CCREG);
5016 store_regs_bt(branch_regs[i].regmap,branch_regs[i].dirty,ba[i]);
5018 if(dops[i].rt1==31&&temp>=0) emit_prefetchreg(temp);
5020 do_cc(i,branch_regs[i].regmap,&adj,ba[i],TAKEN,0);
5021 if(adj) emit_addimm(cc,CLOCK_ADJUST(ccadj[i]+2-adj),cc);
5022 load_regs_bt(branch_regs[i].regmap,branch_regs[i].dirty,ba[i]);
5023 if(internal_branch(ba[i]))
5024 assem_debug("branch: internal\n");
5026 assem_debug("branch: external\n");
5027 if (internal_branch(ba[i]) && dops[(ba[i]-start)>>2].is_ds) {
5028 ds_assemble_entry(i);
5031 add_to_linker(out,ba[i],internal_branch(ba[i]));
5036 static void rjump_assemble_write_ra(int i)
5038 int rt,return_address;
5039 assert(dops[i+1].rt1!=dops[i].rt1);
5040 assert(dops[i+1].rt2!=dops[i].rt1);
5041 rt=get_reg(branch_regs[i].regmap,dops[i].rt1);
5042 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]);
5044 return_address=start+i*4+8;
5048 if(i_regmap[temp]!=PTEMP) emit_movimm((uintptr_t)hash_table_get(return_address),temp);
5051 emit_movimm(return_address,rt); // PC into link register
5053 emit_prefetch(hash_table_get(return_address));
5057 static void rjump_assemble(int i,struct regstat *i_regs)
5062 rs=get_reg(branch_regs[i].regmap,dops[i].rs1);
5064 if (ds_writes_rjump_rs(i)) {
5065 // Delay slot abuse, make a copy of the branch address register
5066 temp=get_reg(branch_regs[i].regmap,RTEMP);
5068 assert(regs[i].regmap[temp]==RTEMP);
5072 address_generation(i+1,i_regs,regs[i].regmap_entry);
5076 if((temp=get_reg(branch_regs[i].regmap,PTEMP))>=0) {
5077 signed char *i_regmap=i_regs->regmap;
5078 int return_address=start+i*4+8;
5079 if(i_regmap[temp]==PTEMP) emit_movimm((uintptr_t)hash_table_get(return_address),temp);
5084 if(dops[i].rs1==31) {
5085 int rh=get_reg(regs[i].regmap,RHASH);
5086 if(rh>=0) do_preload_rhash(rh);
5089 if(dops[i].rt1!=0&&(dops[i].rt1==dops[i+1].rs1||dops[i].rt1==dops[i+1].rs2)) {
5090 rjump_assemble_write_ra(i);
5093 ds_assemble(i+1,i_regs);
5094 uint64_t bc_unneeded=branch_regs[i].u;
5095 bc_unneeded|=1|(1LL<<dops[i].rt1);
5096 bc_unneeded&=~(1LL<<dops[i].rs1);
5097 wb_invalidate(regs[i].regmap,branch_regs[i].regmap,regs[i].dirty,bc_unneeded);
5098 load_regs(regs[i].regmap,branch_regs[i].regmap,dops[i].rs1,CCREG);
5099 if(!ra_done&&dops[i].rt1!=0)
5100 rjump_assemble_write_ra(i);
5101 cc=get_reg(branch_regs[i].regmap,CCREG);
5102 assert(cc==HOST_CCREG);
5105 int rh=get_reg(branch_regs[i].regmap,RHASH);
5106 int ht=get_reg(branch_regs[i].regmap,RHTBL);
5107 if(dops[i].rs1==31) {
5108 if(regs[i].regmap[rh]!=RHASH) do_preload_rhash(rh);
5109 do_preload_rhtbl(ht);
5113 store_regs_bt(branch_regs[i].regmap,branch_regs[i].dirty,-1);
5114 #ifdef DESTRUCTIVE_WRITEBACK
5115 if((branch_regs[i].dirty>>rs)&1) {
5116 if(dops[i].rs1!=dops[i+1].rt1&&dops[i].rs1!=dops[i+1].rt2) {
5117 emit_loadreg(dops[i].rs1,rs);
5122 if(dops[i].rt1==31&&temp>=0) emit_prefetchreg(temp);
5125 if(dops[i].rs1==31) {
5126 do_miniht_load(ht,rh);
5129 //do_cc(i,branch_regs[i].regmap,&adj,-1,TAKEN);
5130 //if(adj) emit_addimm(cc,2*(ccadj[i]+2-adj),cc); // ??? - Shouldn't happen
5132 emit_addimm_and_set_flags(CLOCK_ADJUST(ccadj[i]+2),HOST_CCREG);
5133 add_stub(CC_STUB,out,NULL,0,i,-1,TAKEN,rs);
5134 if(dops[i+1].itype==COP0&&(source[i+1]&0x3f)==0x10)
5135 // special case for RFE
5139 //load_regs_bt(branch_regs[i].regmap,branch_regs[i].dirty,-1);
5141 if(dops[i].rs1==31) {
5142 do_miniht_jump(rs,rh,ht);
5149 #ifdef CORTEX_A8_BRANCH_PREDICTION_HACK
5150 if(dops[i].rt1!=31&&i<slen-2&&(((u_int)out)&7)) emit_mov(13,13);
5154 static void cjump_assemble(int i,struct regstat *i_regs)
5156 signed char *i_regmap=i_regs->regmap;
5159 match=match_bt(branch_regs[i].regmap,branch_regs[i].dirty,ba[i]);
5160 assem_debug("match=%d\n",match);
5162 int unconditional=0,nop=0;
5164 int internal=internal_branch(ba[i]);
5165 if(i==(ba[i]-start)>>2) assem_debug("idle loop\n");
5166 if(!match) invert=1;
5167 #ifdef CORTEX_A8_BRANCH_PREDICTION_HACK
5168 if(i>(ba[i]-start)>>2) invert=1;
5171 invert=1; // because of near cond. branches
5175 s1l=get_reg(branch_regs[i].regmap,dops[i].rs1);
5176 s2l=get_reg(branch_regs[i].regmap,dops[i].rs2);
5179 s1l=get_reg(i_regmap,dops[i].rs1);
5180 s2l=get_reg(i_regmap,dops[i].rs2);
5182 if(dops[i].rs1==0&&dops[i].rs2==0)
5184 if(dops[i].opcode&1) nop=1;
5185 else unconditional=1;
5186 //assert(dops[i].opcode!=5);
5187 //assert(dops[i].opcode!=7);
5188 //assert(dops[i].opcode!=0x15);
5189 //assert(dops[i].opcode!=0x17);
5191 else if(dops[i].rs1==0)
5196 else if(dops[i].rs2==0)
5202 // Out of order execution (delay slot first)
5204 address_generation(i+1,i_regs,regs[i].regmap_entry);
5205 ds_assemble(i+1,i_regs);
5207 uint64_t bc_unneeded=branch_regs[i].u;
5208 bc_unneeded&=~((1LL<<dops[i].rs1)|(1LL<<dops[i].rs2));
5210 wb_invalidate(regs[i].regmap,branch_regs[i].regmap,regs[i].dirty,bc_unneeded);
5211 load_regs(regs[i].regmap,branch_regs[i].regmap,dops[i].rs1,dops[i].rs2);
5212 load_regs(regs[i].regmap,branch_regs[i].regmap,CCREG,CCREG);
5213 cc=get_reg(branch_regs[i].regmap,CCREG);
5214 assert(cc==HOST_CCREG);
5216 store_regs_bt(branch_regs[i].regmap,branch_regs[i].dirty,ba[i]);
5217 //do_cc(i,branch_regs[i].regmap,&adj,unconditional?ba[i]:-1,unconditional);
5218 //assem_debug("cycle count (adj)\n");
5220 do_cc(i,branch_regs[i].regmap,&adj,ba[i],TAKEN,0);
5221 if(i!=(ba[i]-start)>>2 || source[i+1]!=0) {
5222 if(adj) emit_addimm(cc,CLOCK_ADJUST(ccadj[i]+2-adj),cc);
5223 load_regs_bt(branch_regs[i].regmap,branch_regs[i].dirty,ba[i]);
5225 assem_debug("branch: internal\n");
5227 assem_debug("branch: external\n");
5228 if (internal && dops[(ba[i]-start)>>2].is_ds) {
5229 ds_assemble_entry(i);
5232 add_to_linker(out,ba[i],internal);
5235 #ifdef CORTEX_A8_BRANCH_PREDICTION_HACK
5236 if(((u_int)out)&7) emit_addnop(0);
5241 emit_addimm_and_set_flags(CLOCK_ADJUST(ccadj[i]+2),cc);
5244 add_stub(CC_STUB,jaddr,out,0,i,start+i*4+8,NOTTAKEN,0);
5247 void *taken = NULL, *nottaken = NULL, *nottaken1 = NULL;
5248 do_cc(i,branch_regs[i].regmap,&adj,-1,0,invert);
5249 if(adj&&!invert) emit_addimm(cc,CLOCK_ADJUST(ccadj[i]+2-adj),cc);
5251 //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]);
5253 if(dops[i].opcode==4) // BEQ
5255 if(s2l>=0) emit_cmp(s1l,s2l);
5256 else emit_test(s1l,s1l);
5261 add_to_linker(out,ba[i],internal);
5265 if(dops[i].opcode==5) // BNE
5267 if(s2l>=0) emit_cmp(s1l,s2l);
5268 else emit_test(s1l,s1l);
5273 add_to_linker(out,ba[i],internal);
5277 if(dops[i].opcode==6) // BLEZ
5284 add_to_linker(out,ba[i],internal);
5288 if(dops[i].opcode==7) // BGTZ
5295 add_to_linker(out,ba[i],internal);
5300 if(taken) set_jump_target(taken, out);
5301 #ifdef CORTEX_A8_BRANCH_PREDICTION_HACK
5302 if (match && (!internal || !dops[(ba[i]-start)>>2].is_ds)) {
5304 emit_addimm(cc,-CLOCK_ADJUST(adj),cc);
5305 add_to_linker(out,ba[i],internal);
5308 add_to_linker(out,ba[i],internal*2);
5314 if(adj) emit_addimm(cc,-CLOCK_ADJUST(adj),cc);
5315 store_regs_bt(branch_regs[i].regmap,branch_regs[i].dirty,ba[i]);
5316 load_regs_bt(branch_regs[i].regmap,branch_regs[i].dirty,ba[i]);
5318 assem_debug("branch: internal\n");
5320 assem_debug("branch: external\n");
5321 if (internal && dops[(ba[i] - start) >> 2].is_ds) {
5322 ds_assemble_entry(i);
5325 add_to_linker(out,ba[i],internal);
5329 set_jump_target(nottaken, out);
5332 if(nottaken1) set_jump_target(nottaken1, out);
5334 if(!invert) emit_addimm(cc,CLOCK_ADJUST(adj),cc);
5336 } // (!unconditional)
5340 // In-order execution (branch first)
5341 //if(dops[i].likely) printf("IOL\n");
5344 void *taken = NULL, *nottaken = NULL, *nottaken1 = NULL;
5345 if(!unconditional&&!nop) {
5346 //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]);
5348 if((dops[i].opcode&0x2f)==4) // BEQ
5350 if(s2l>=0) emit_cmp(s1l,s2l);
5351 else emit_test(s1l,s1l);
5355 if((dops[i].opcode&0x2f)==5) // BNE
5357 if(s2l>=0) emit_cmp(s1l,s2l);
5358 else emit_test(s1l,s1l);
5362 if((dops[i].opcode&0x2f)==6) // BLEZ
5368 if((dops[i].opcode&0x2f)==7) // BGTZ
5374 } // if(!unconditional)
5376 uint64_t ds_unneeded=branch_regs[i].u;
5377 ds_unneeded&=~((1LL<<dops[i+1].rs1)|(1LL<<dops[i+1].rs2));
5381 if(taken) set_jump_target(taken, out);
5382 assem_debug("1:\n");
5383 wb_invalidate(regs[i].regmap,branch_regs[i].regmap,regs[i].dirty,ds_unneeded);
5385 load_regs(regs[i].regmap,branch_regs[i].regmap,dops[i+1].rs1,dops[i+1].rs2);
5386 address_generation(i+1,&branch_regs[i],0);
5387 load_regs(regs[i].regmap,branch_regs[i].regmap,CCREG,INVCP);
5388 ds_assemble(i+1,&branch_regs[i]);
5389 cc=get_reg(branch_regs[i].regmap,CCREG);
5391 emit_loadreg(CCREG,cc=HOST_CCREG);
5392 // CHECK: Is the following instruction (fall thru) allocated ok?
5394 assert(cc==HOST_CCREG);
5395 store_regs_bt(branch_regs[i].regmap,branch_regs[i].dirty,ba[i]);
5396 do_cc(i,i_regmap,&adj,ba[i],TAKEN,0);
5397 assem_debug("cycle count (adj)\n");
5398 if(adj) emit_addimm(cc,CLOCK_ADJUST(ccadj[i]+2-adj),cc);
5399 load_regs_bt(branch_regs[i].regmap,branch_regs[i].dirty,ba[i]);
5401 assem_debug("branch: internal\n");
5403 assem_debug("branch: external\n");
5404 if (internal && dops[(ba[i] - start) >> 2].is_ds) {
5405 ds_assemble_entry(i);
5408 add_to_linker(out,ba[i],internal);
5413 if(!unconditional) {
5414 if(nottaken1) set_jump_target(nottaken1, out);
5415 set_jump_target(nottaken, out);
5416 assem_debug("2:\n");
5417 if(!dops[i].likely) {
5418 wb_invalidate(regs[i].regmap,branch_regs[i].regmap,regs[i].dirty,ds_unneeded);
5419 load_regs(regs[i].regmap,branch_regs[i].regmap,dops[i+1].rs1,dops[i+1].rs2);
5420 address_generation(i+1,&branch_regs[i],0);
5421 load_regs(regs[i].regmap,branch_regs[i].regmap,CCREG,CCREG);
5422 ds_assemble(i+1,&branch_regs[i]);
5424 cc=get_reg(branch_regs[i].regmap,CCREG);
5425 if(cc==-1&&!dops[i].likely) {
5426 // Cycle count isn't in a register, temporarily load it then write it out
5427 emit_loadreg(CCREG,HOST_CCREG);
5428 emit_addimm_and_set_flags(CLOCK_ADJUST(ccadj[i]+2),HOST_CCREG);
5431 add_stub(CC_STUB,jaddr,out,0,i,start+i*4+8,NOTTAKEN,0);
5432 emit_storereg(CCREG,HOST_CCREG);
5435 cc=get_reg(i_regmap,CCREG);
5436 assert(cc==HOST_CCREG);
5437 emit_addimm_and_set_flags(CLOCK_ADJUST(ccadj[i]+2),cc);
5440 add_stub(CC_STUB,jaddr,out,0,i,start+i*4+8,dops[i].likely?NULLDS:NOTTAKEN,0);
5446 static void sjump_assemble(int i,struct regstat *i_regs)
5448 signed char *i_regmap=i_regs->regmap;
5451 match=match_bt(branch_regs[i].regmap,branch_regs[i].dirty,ba[i]);
5452 assem_debug("smatch=%d\n",match);
5454 int unconditional=0,nevertaken=0;
5456 int internal=internal_branch(ba[i]);
5457 if(i==(ba[i]-start)>>2) assem_debug("idle loop\n");
5458 if(!match) invert=1;
5459 #ifdef CORTEX_A8_BRANCH_PREDICTION_HACK
5460 if(i>(ba[i]-start)>>2) invert=1;
5463 invert=1; // because of near cond. branches
5466 //if(dops[i].opcode2>=0x10) return; // FIXME (BxxZAL)
5467 //assert(dops[i].opcode2<0x10||dops[i].rs1==0); // FIXME (BxxZAL)
5470 s1l=get_reg(branch_regs[i].regmap,dops[i].rs1);
5473 s1l=get_reg(i_regmap,dops[i].rs1);
5477 if(dops[i].opcode2&1) unconditional=1;
5479 // These are never taken (r0 is never less than zero)
5480 //assert(dops[i].opcode2!=0);
5481 //assert(dops[i].opcode2!=2);
5482 //assert(dops[i].opcode2!=0x10);
5483 //assert(dops[i].opcode2!=0x12);
5487 // Out of order execution (delay slot first)
5489 address_generation(i+1,i_regs,regs[i].regmap_entry);
5490 ds_assemble(i+1,i_regs);
5492 uint64_t bc_unneeded=branch_regs[i].u;
5493 bc_unneeded&=~((1LL<<dops[i].rs1)|(1LL<<dops[i].rs2));
5495 wb_invalidate(regs[i].regmap,branch_regs[i].regmap,regs[i].dirty,bc_unneeded);
5496 load_regs(regs[i].regmap,branch_regs[i].regmap,dops[i].rs1,dops[i].rs1);
5497 load_regs(regs[i].regmap,branch_regs[i].regmap,CCREG,CCREG);
5498 if(dops[i].rt1==31) {
5499 int rt,return_address;
5500 rt=get_reg(branch_regs[i].regmap,31);
5501 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]);
5503 // Save the PC even if the branch is not taken
5504 return_address=start+i*4+8;
5505 emit_movimm(return_address,rt); // PC into link register
5507 if(!nevertaken) emit_prefetch(hash_table_get(return_address));
5511 cc=get_reg(branch_regs[i].regmap,CCREG);
5512 assert(cc==HOST_CCREG);
5514 store_regs_bt(branch_regs[i].regmap,branch_regs[i].dirty,ba[i]);
5515 //do_cc(i,branch_regs[i].regmap,&adj,unconditional?ba[i]:-1,unconditional);
5516 assem_debug("cycle count (adj)\n");
5518 do_cc(i,branch_regs[i].regmap,&adj,ba[i],TAKEN,0);
5519 if(i!=(ba[i]-start)>>2 || source[i+1]!=0) {
5520 if(adj) emit_addimm(cc,CLOCK_ADJUST(ccadj[i]+2-adj),cc);
5521 load_regs_bt(branch_regs[i].regmap,branch_regs[i].dirty,ba[i]);
5523 assem_debug("branch: internal\n");
5525 assem_debug("branch: external\n");
5526 if (internal && dops[(ba[i] - start) >> 2].is_ds) {
5527 ds_assemble_entry(i);
5530 add_to_linker(out,ba[i],internal);
5533 #ifdef CORTEX_A8_BRANCH_PREDICTION_HACK
5534 if(((u_int)out)&7) emit_addnop(0);
5538 else if(nevertaken) {
5539 emit_addimm_and_set_flags(CLOCK_ADJUST(ccadj[i]+2),cc);
5542 add_stub(CC_STUB,jaddr,out,0,i,start+i*4+8,NOTTAKEN,0);
5545 void *nottaken = NULL;
5546 do_cc(i,branch_regs[i].regmap,&adj,-1,0,invert);
5547 if(adj&&!invert) emit_addimm(cc,CLOCK_ADJUST(ccadj[i]+2-adj),cc);
5550 if((dops[i].opcode2&0xf)==0) // BLTZ/BLTZAL
5557 add_to_linker(out,ba[i],internal);
5561 if((dops[i].opcode2&0xf)==1) // BGEZ/BLTZAL
5568 add_to_linker(out,ba[i],internal);
5575 #ifdef CORTEX_A8_BRANCH_PREDICTION_HACK
5576 if (match && (!internal || !dops[(ba[i] - start) >> 2].is_ds)) {
5578 emit_addimm(cc,-CLOCK_ADJUST(adj),cc);
5579 add_to_linker(out,ba[i],internal);
5582 add_to_linker(out,ba[i],internal*2);
5588 if(adj) emit_addimm(cc,-CLOCK_ADJUST(adj),cc);
5589 store_regs_bt(branch_regs[i].regmap,branch_regs[i].dirty,ba[i]);
5590 load_regs_bt(branch_regs[i].regmap,branch_regs[i].dirty,ba[i]);
5592 assem_debug("branch: internal\n");
5594 assem_debug("branch: external\n");
5595 if (internal && dops[(ba[i] - start) >> 2].is_ds) {
5596 ds_assemble_entry(i);
5599 add_to_linker(out,ba[i],internal);
5603 set_jump_target(nottaken, out);
5607 if(!invert) emit_addimm(cc,CLOCK_ADJUST(adj),cc);
5609 } // (!unconditional)
5613 // In-order execution (branch first)
5615 void *nottaken = NULL;
5616 if(dops[i].rt1==31) {
5617 int rt,return_address;
5618 rt=get_reg(branch_regs[i].regmap,31);
5620 // Save the PC even if the branch is not taken
5621 return_address=start+i*4+8;
5622 emit_movimm(return_address,rt); // PC into link register
5624 emit_prefetch(hash_table_get(return_address));
5628 if(!unconditional) {
5629 //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]);
5631 if((dops[i].opcode2&0x0d)==0) // BLTZ/BLTZL/BLTZAL/BLTZALL
5637 if((dops[i].opcode2&0x0d)==1) // BGEZ/BGEZL/BGEZAL/BGEZALL
5643 } // if(!unconditional)
5645 uint64_t ds_unneeded=branch_regs[i].u;
5646 ds_unneeded&=~((1LL<<dops[i+1].rs1)|(1LL<<dops[i+1].rs2));
5650 //assem_debug("1:\n");
5651 wb_invalidate(regs[i].regmap,branch_regs[i].regmap,regs[i].dirty,ds_unneeded);
5653 load_regs(regs[i].regmap,branch_regs[i].regmap,dops[i+1].rs1,dops[i+1].rs2);
5654 address_generation(i+1,&branch_regs[i],0);
5655 load_regs(regs[i].regmap,branch_regs[i].regmap,CCREG,INVCP);
5656 ds_assemble(i+1,&branch_regs[i]);
5657 cc=get_reg(branch_regs[i].regmap,CCREG);
5659 emit_loadreg(CCREG,cc=HOST_CCREG);
5660 // CHECK: Is the following instruction (fall thru) allocated ok?
5662 assert(cc==HOST_CCREG);
5663 store_regs_bt(branch_regs[i].regmap,branch_regs[i].dirty,ba[i]);
5664 do_cc(i,i_regmap,&adj,ba[i],TAKEN,0);
5665 assem_debug("cycle count (adj)\n");
5666 if(adj) emit_addimm(cc,CLOCK_ADJUST(ccadj[i]+2-adj),cc);
5667 load_regs_bt(branch_regs[i].regmap,branch_regs[i].dirty,ba[i]);
5669 assem_debug("branch: internal\n");
5671 assem_debug("branch: external\n");
5672 if (internal && dops[(ba[i] - start) >> 2].is_ds) {
5673 ds_assemble_entry(i);
5676 add_to_linker(out,ba[i],internal);
5681 if(!unconditional) {
5682 set_jump_target(nottaken, out);
5683 assem_debug("1:\n");
5684 if(!dops[i].likely) {
5685 wb_invalidate(regs[i].regmap,branch_regs[i].regmap,regs[i].dirty,ds_unneeded);
5686 load_regs(regs[i].regmap,branch_regs[i].regmap,dops[i+1].rs1,dops[i+1].rs2);
5687 address_generation(i+1,&branch_regs[i],0);
5688 load_regs(regs[i].regmap,branch_regs[i].regmap,CCREG,CCREG);
5689 ds_assemble(i+1,&branch_regs[i]);
5691 cc=get_reg(branch_regs[i].regmap,CCREG);
5692 if(cc==-1&&!dops[i].likely) {
5693 // Cycle count isn't in a register, temporarily load it then write it out
5694 emit_loadreg(CCREG,HOST_CCREG);
5695 emit_addimm_and_set_flags(CLOCK_ADJUST(ccadj[i]+2),HOST_CCREG);
5698 add_stub(CC_STUB,jaddr,out,0,i,start+i*4+8,NOTTAKEN,0);
5699 emit_storereg(CCREG,HOST_CCREG);
5702 cc=get_reg(i_regmap,CCREG);
5703 assert(cc==HOST_CCREG);
5704 emit_addimm_and_set_flags(CLOCK_ADJUST(ccadj[i]+2),cc);
5707 add_stub(CC_STUB,jaddr,out,0,i,start+i*4+8,dops[i].likely?NULLDS:NOTTAKEN,0);
5713 static void pagespan_assemble(int i,struct regstat *i_regs)
5715 int s1l=get_reg(i_regs->regmap,dops[i].rs1);
5716 int s2l=get_reg(i_regs->regmap,dops[i].rs2);
5718 void *nottaken = NULL;
5719 int unconditional=0;
5725 else if(dops[i].rs2==0)
5730 int addr=-1,alt=-1,ntaddr=-1;
5731 if(i_regs->regmap[HOST_BTREG]<0) {addr=HOST_BTREG;}
5735 if(hr!=EXCLUDE_REG && hr!=HOST_CCREG &&
5736 (i_regs->regmap[hr]&63)!=dops[i].rs1 &&
5737 (i_regs->regmap[hr]&63)!=dops[i].rs2 )
5746 if(hr!=EXCLUDE_REG && hr!=HOST_CCREG && hr!=HOST_BTREG &&
5747 (i_regs->regmap[hr]&63)!=dops[i].rs1 &&
5748 (i_regs->regmap[hr]&63)!=dops[i].rs2 )
5754 if((dops[i].opcode&0x2E)==6) // BLEZ/BGTZ needs another register
5758 if(hr!=EXCLUDE_REG && hr!=HOST_CCREG && hr!=HOST_BTREG &&
5759 (i_regs->regmap[hr]&63)!=dops[i].rs1 &&
5760 (i_regs->regmap[hr]&63)!=dops[i].rs2 )
5767 assert(hr<HOST_REGS);
5768 if((dops[i].opcode&0x2e)==4||dops[i].opcode==0x11) { // BEQ/BNE/BEQL/BNEL/BC1
5769 load_regs(regs[i].regmap_entry,regs[i].regmap,CCREG,CCREG);
5771 emit_addimm(HOST_CCREG,CLOCK_ADJUST(ccadj[i]+2),HOST_CCREG);
5772 if(dops[i].opcode==2) // J
5776 if(dops[i].opcode==3) // JAL
5779 int rt=get_reg(i_regs->regmap,31);
5780 emit_movimm(start+i*4+8,rt);
5783 if(dops[i].opcode==0&&(dops[i].opcode2&0x3E)==8) // JR/JALR
5786 if(dops[i].opcode2==9) // JALR
5788 int rt=get_reg(i_regs->regmap,dops[i].rt1);
5789 emit_movimm(start+i*4+8,rt);
5792 if((dops[i].opcode&0x3f)==4) // BEQ
5794 if(dops[i].rs1==dops[i].rs2)
5799 #ifdef HAVE_CMOV_IMM
5801 if(s2l>=0) emit_cmp(s1l,s2l);
5802 else emit_test(s1l,s1l);
5803 emit_cmov2imm_e_ne_compact(ba[i],start+i*4+8,addr);
5809 emit_mov2imm_compact(ba[i],addr,start+i*4+8,alt);
5810 if(s2l>=0) emit_cmp(s1l,s2l);
5811 else emit_test(s1l,s1l);
5812 emit_cmovne_reg(alt,addr);
5815 if((dops[i].opcode&0x3f)==5) // BNE
5817 #ifdef HAVE_CMOV_IMM
5818 if(s2l>=0) emit_cmp(s1l,s2l);
5819 else emit_test(s1l,s1l);
5820 emit_cmov2imm_e_ne_compact(start+i*4+8,ba[i],addr);
5823 emit_mov2imm_compact(start+i*4+8,addr,ba[i],alt);
5824 if(s2l>=0) emit_cmp(s1l,s2l);
5825 else emit_test(s1l,s1l);
5826 emit_cmovne_reg(alt,addr);
5829 if((dops[i].opcode&0x3f)==0x14) // BEQL
5831 if(s2l>=0) emit_cmp(s1l,s2l);
5832 else emit_test(s1l,s1l);
5833 if(nottaken) set_jump_target(nottaken, out);
5837 if((dops[i].opcode&0x3f)==0x15) // BNEL
5839 if(s2l>=0) emit_cmp(s1l,s2l);
5840 else emit_test(s1l,s1l);
5843 if(taken) set_jump_target(taken, out);
5845 if((dops[i].opcode&0x3f)==6) // BLEZ
5847 emit_mov2imm_compact(ba[i],alt,start+i*4+8,addr);
5849 emit_cmovl_reg(alt,addr);
5851 if((dops[i].opcode&0x3f)==7) // BGTZ
5853 emit_mov2imm_compact(ba[i],addr,start+i*4+8,ntaddr);
5855 emit_cmovl_reg(ntaddr,addr);
5857 if((dops[i].opcode&0x3f)==0x16) // BLEZL
5859 assert((dops[i].opcode&0x3f)!=0x16);
5861 if((dops[i].opcode&0x3f)==0x17) // BGTZL
5863 assert((dops[i].opcode&0x3f)!=0x17);
5865 assert(dops[i].opcode!=1); // BLTZ/BGEZ
5867 //FIXME: Check CSREG
5868 if(dops[i].opcode==0x11 && dops[i].opcode2==0x08 ) {
5869 if((source[i]&0x30000)==0) // BC1F
5871 emit_mov2imm_compact(ba[i],addr,start+i*4+8,alt);
5872 emit_testimm(s1l,0x800000);
5873 emit_cmovne_reg(alt,addr);
5875 if((source[i]&0x30000)==0x10000) // BC1T
5877 emit_mov2imm_compact(ba[i],alt,start+i*4+8,addr);
5878 emit_testimm(s1l,0x800000);
5879 emit_cmovne_reg(alt,addr);
5881 if((source[i]&0x30000)==0x20000) // BC1FL
5883 emit_testimm(s1l,0x800000);
5887 if((source[i]&0x30000)==0x30000) // BC1TL
5889 emit_testimm(s1l,0x800000);
5895 assert(i_regs->regmap[HOST_CCREG]==CCREG);
5896 wb_dirtys(regs[i].regmap,regs[i].dirty);
5897 if(dops[i].likely||unconditional)
5899 emit_movimm(ba[i],HOST_BTREG);
5901 else if(addr!=HOST_BTREG)
5903 emit_mov(addr,HOST_BTREG);
5905 void *branch_addr=out;
5907 int target_addr=start+i*4+5;
5909 void *compiled_target_addr=check_addr(target_addr);
5910 emit_extjump_ds(branch_addr, target_addr);
5911 if(compiled_target_addr) {
5912 set_jump_target(branch_addr, compiled_target_addr);
5913 add_jump_out(target_addr,stub);
5915 else set_jump_target(branch_addr, stub);
5916 if(dops[i].likely) {
5918 set_jump_target(nottaken, out);
5919 wb_dirtys(regs[i].regmap,regs[i].dirty);
5920 void *branch_addr=out;
5922 int target_addr=start+i*4+8;
5924 void *compiled_target_addr=check_addr(target_addr);
5925 emit_extjump_ds(branch_addr, target_addr);
5926 if(compiled_target_addr) {
5927 set_jump_target(branch_addr, compiled_target_addr);
5928 add_jump_out(target_addr,stub);
5930 else set_jump_target(branch_addr, stub);
5934 // Assemble the delay slot for the above
5935 static void pagespan_ds()
5937 assem_debug("initial delay slot:\n");
5938 u_int vaddr=start+1;
5939 u_int page=get_page(vaddr);
5940 u_int vpage=get_vpage(vaddr);
5941 ll_add(jump_dirty+vpage,vaddr,(void *)out);
5942 do_dirty_stub_ds(slen*4);
5943 ll_add(jump_in+page,vaddr,(void *)out);
5944 assert(regs[0].regmap_entry[HOST_CCREG]==CCREG);
5945 if(regs[0].regmap[HOST_CCREG]!=CCREG)
5946 wb_register(CCREG,regs[0].regmap_entry,regs[0].wasdirty);
5947 if(regs[0].regmap[HOST_BTREG]!=BTREG)
5948 emit_writeword(HOST_BTREG,&branch_target);
5949 load_regs(regs[0].regmap_entry,regs[0].regmap,dops[0].rs1,dops[0].rs2);
5950 address_generation(0,®s[0],regs[0].regmap_entry);
5951 if(dops[0].itype==STORE||dops[0].itype==STORELR||(dops[0].opcode&0x3b)==0x39||(dops[0].opcode&0x3b)==0x3a)
5952 load_regs(regs[0].regmap_entry,regs[0].regmap,INVCP,INVCP);
5954 switch(dops[0].itype) {
5956 alu_assemble(0,®s[0]);break;
5958 imm16_assemble(0,®s[0]);break;
5960 shift_assemble(0,®s[0]);break;
5962 shiftimm_assemble(0,®s[0]);break;
5964 load_assemble(0,®s[0]);break;
5966 loadlr_assemble(0,®s[0]);break;
5968 store_assemble(0,®s[0]);break;
5970 storelr_assemble(0,®s[0]);break;
5972 cop0_assemble(0,®s[0]);break;
5974 cop1_assemble(0,®s[0]);break;
5976 c1ls_assemble(0,®s[0]);break;
5978 cop2_assemble(0,®s[0]);break;
5980 c2ls_assemble(0,®s[0]);break;
5982 c2op_assemble(0,®s[0]);break;
5984 multdiv_assemble(0,®s[0]);
5985 multdiv_prepare_stall(0,®s[0]);
5988 mov_assemble(0,®s[0]);break;
5997 SysPrintf("Jump in the delay slot. This is probably a bug.\n");
5999 int btaddr=get_reg(regs[0].regmap,BTREG);
6001 btaddr=get_reg(regs[0].regmap,-1);
6002 emit_readword(&branch_target,btaddr);
6004 assert(btaddr!=HOST_CCREG);
6005 if(regs[0].regmap[HOST_CCREG]!=CCREG) emit_loadreg(CCREG,HOST_CCREG);
6007 host_tempreg_acquire();
6008 emit_movimm(start+4,HOST_TEMPREG);
6009 emit_cmp(btaddr,HOST_TEMPREG);
6010 host_tempreg_release();
6012 emit_cmpimm(btaddr,start+4);
6016 store_regs_bt(regs[0].regmap,regs[0].dirty,-1);
6017 do_jump_vaddr(btaddr);
6018 set_jump_target(branch, out);
6019 store_regs_bt(regs[0].regmap,regs[0].dirty,start+4);
6020 load_regs_bt(regs[0].regmap,regs[0].dirty,start+4);
6023 // Basic liveness analysis for MIPS registers
6024 void unneeded_registers(int istart,int iend,int r)
6027 uint64_t u,gte_u,b,gte_b;
6028 uint64_t temp_u,temp_gte_u=0;
6029 uint64_t gte_u_unknown=0;
6030 if (HACK_ENABLED(NDHACK_GTE_UNNEEDED))
6034 gte_u=gte_u_unknown;
6036 //u=unneeded_reg[iend+1];
6038 gte_u=gte_unneeded[iend+1];
6041 for (i=iend;i>=istart;i--)
6043 //printf("unneeded registers i=%d (%d,%d) r=%d\n",i,istart,iend,r);
6044 if(dops[i].itype==RJUMP||dops[i].itype==UJUMP||dops[i].itype==CJUMP||dops[i].itype==SJUMP)
6046 // If subroutine call, flag return address as a possible branch target
6047 if(dops[i].rt1==31 && i<slen-2) dops[i+2].bt=1;
6049 if(ba[i]<start || ba[i]>=(start+slen*4))
6051 // Branch out of this block, flush all regs
6053 gte_u=gte_u_unknown;
6054 branch_unneeded_reg[i]=u;
6055 // Merge in delay slot
6056 u|=(1LL<<dops[i+1].rt1)|(1LL<<dops[i+1].rt2);
6057 u&=~((1LL<<dops[i+1].rs1)|(1LL<<dops[i+1].rs2));
6060 gte_u&=~gte_rs[i+1];
6061 // If branch is "likely" (and conditional)
6062 // then we skip the delay slot on the fall-thru path
6063 if(dops[i].likely) {
6065 u&=unneeded_reg[i+2];
6066 gte_u&=gte_unneeded[i+2];
6071 gte_u=gte_u_unknown;
6077 // Internal branch, flag target
6078 dops[(ba[i]-start)>>2].bt=1;
6079 if(ba[i]<=start+i*4) {
6083 // Unconditional branch
6087 // Conditional branch (not taken case)
6088 temp_u=unneeded_reg[i+2];
6089 temp_gte_u&=gte_unneeded[i+2];
6091 // Merge in delay slot
6092 temp_u|=(1LL<<dops[i+1].rt1)|(1LL<<dops[i+1].rt2);
6093 temp_u&=~((1LL<<dops[i+1].rs1)|(1LL<<dops[i+1].rs2));
6095 temp_gte_u|=gte_rt[i+1];
6096 temp_gte_u&=~gte_rs[i+1];
6097 // If branch is "likely" (and conditional)
6098 // then we skip the delay slot on the fall-thru path
6099 if(dops[i].likely) {
6101 temp_u&=unneeded_reg[i+2];
6102 temp_gte_u&=gte_unneeded[i+2];
6107 temp_gte_u=gte_u_unknown;
6110 temp_u|=(1LL<<dops[i].rt1)|(1LL<<dops[i].rt2);
6111 temp_u&=~((1LL<<dops[i].rs1)|(1LL<<dops[i].rs2));
6113 temp_gte_u|=gte_rt[i];
6114 temp_gte_u&=~gte_rs[i];
6115 unneeded_reg[i]=temp_u;
6116 gte_unneeded[i]=temp_gte_u;
6117 // Only go three levels deep. This recursion can take an
6118 // excessive amount of time if there are a lot of nested loops.
6120 unneeded_registers((ba[i]-start)>>2,i-1,r+1);
6122 unneeded_reg[(ba[i]-start)>>2]=1;
6123 gte_unneeded[(ba[i]-start)>>2]=gte_u_unknown;
6128 // Unconditional branch
6129 u=unneeded_reg[(ba[i]-start)>>2];
6130 gte_u=gte_unneeded[(ba[i]-start)>>2];
6131 branch_unneeded_reg[i]=u;
6132 // Merge in delay slot
6133 u|=(1LL<<dops[i+1].rt1)|(1LL<<dops[i+1].rt2);
6134 u&=~((1LL<<dops[i+1].rs1)|(1LL<<dops[i+1].rs2));
6137 gte_u&=~gte_rs[i+1];
6139 // Conditional branch
6140 b=unneeded_reg[(ba[i]-start)>>2];
6141 gte_b=gte_unneeded[(ba[i]-start)>>2];
6142 branch_unneeded_reg[i]=b;
6143 // Branch delay slot
6144 b|=(1LL<<dops[i+1].rt1)|(1LL<<dops[i+1].rt2);
6145 b&=~((1LL<<dops[i+1].rs1)|(1LL<<dops[i+1].rs2));
6148 gte_b&=~gte_rs[i+1];
6149 // If branch is "likely" then we skip the
6150 // delay slot on the fall-thru path
6151 if(dops[i].likely) {
6155 u&=unneeded_reg[i+2];
6156 gte_u&=gte_unneeded[i+2];
6163 branch_unneeded_reg[i]&=unneeded_reg[i+2];
6165 branch_unneeded_reg[i]=1;
6171 else if(dops[i].itype==SYSCALL||dops[i].itype==HLECALL||dops[i].itype==INTCALL)
6173 // SYSCALL instruction (software interrupt)
6176 else if(dops[i].itype==COP0 && (source[i]&0x3f)==0x18)
6178 // ERET instruction (return from interrupt)
6182 // Written registers are unneeded
6183 u|=1LL<<dops[i].rt1;
6184 u|=1LL<<dops[i].rt2;
6186 // Accessed registers are needed
6187 u&=~(1LL<<dops[i].rs1);
6188 u&=~(1LL<<dops[i].rs2);
6190 if(gte_rs[i]&&dops[i].rt1&&(unneeded_reg[i+1]&(1ll<<dops[i].rt1)))
6191 gte_u|=gte_rs[i]>e_unneeded[i+1]; // MFC2/CFC2 to dead register, unneeded
6192 // Source-target dependencies
6193 // R0 is always unneeded
6197 gte_unneeded[i]=gte_u;
6199 printf("ur (%d,%d) %x: ",istart,iend,start+i*4);
6202 for(r=1;r<=CCREG;r++) {
6203 if((unneeded_reg[i]>>r)&1) {
6204 if(r==HIREG) printf(" HI");
6205 else if(r==LOREG) printf(" LO");
6206 else printf(" r%d",r);
6214 // Write back dirty registers as soon as we will no longer modify them,
6215 // so that we don't end up with lots of writes at the branches.
6216 void clean_registers(int istart,int iend,int wr)
6220 u_int will_dirty_i,will_dirty_next,temp_will_dirty;
6221 u_int wont_dirty_i,wont_dirty_next,temp_wont_dirty;
6223 will_dirty_i=will_dirty_next=0;
6224 wont_dirty_i=wont_dirty_next=0;
6226 will_dirty_i=will_dirty_next=will_dirty[iend+1];
6227 wont_dirty_i=wont_dirty_next=wont_dirty[iend+1];
6229 for (i=iend;i>=istart;i--)
6231 if(dops[i].itype==RJUMP||dops[i].itype==UJUMP||dops[i].itype==CJUMP||dops[i].itype==SJUMP)
6233 if(ba[i]<start || ba[i]>=(start+slen*4))
6235 // Branch out of this block, flush all regs
6238 // Unconditional branch
6241 // Merge in delay slot (will dirty)
6242 for(r=0;r<HOST_REGS;r++) {
6243 if(r!=EXCLUDE_REG) {
6244 if((branch_regs[i].regmap[r]&63)==dops[i].rt1) will_dirty_i|=1<<r;
6245 if((branch_regs[i].regmap[r]&63)==dops[i].rt2) will_dirty_i|=1<<r;
6246 if((branch_regs[i].regmap[r]&63)==dops[i+1].rt1) will_dirty_i|=1<<r;
6247 if((branch_regs[i].regmap[r]&63)==dops[i+1].rt2) will_dirty_i|=1<<r;
6248 if((branch_regs[i].regmap[r]&63)>33) will_dirty_i&=~(1<<r);
6249 if(branch_regs[i].regmap[r]<=0) will_dirty_i&=~(1<<r);
6250 if(branch_regs[i].regmap[r]==CCREG) will_dirty_i|=1<<r;
6251 if((regs[i].regmap[r]&63)==dops[i].rt1) will_dirty_i|=1<<r;
6252 if((regs[i].regmap[r]&63)==dops[i].rt2) will_dirty_i|=1<<r;
6253 if((regs[i].regmap[r]&63)==dops[i+1].rt1) will_dirty_i|=1<<r;
6254 if((regs[i].regmap[r]&63)==dops[i+1].rt2) will_dirty_i|=1<<r;
6255 if((regs[i].regmap[r]&63)>33) will_dirty_i&=~(1<<r);
6256 if(regs[i].regmap[r]<=0) will_dirty_i&=~(1<<r);
6257 if(regs[i].regmap[r]==CCREG) will_dirty_i|=1<<r;
6263 // Conditional branch
6265 wont_dirty_i=wont_dirty_next;
6266 // Merge in delay slot (will dirty)
6267 for(r=0;r<HOST_REGS;r++) {
6268 if(r!=EXCLUDE_REG) {
6269 if(!dops[i].likely) {
6270 // Might not dirty if likely branch is not taken
6271 if((branch_regs[i].regmap[r]&63)==dops[i].rt1) will_dirty_i|=1<<r;
6272 if((branch_regs[i].regmap[r]&63)==dops[i].rt2) will_dirty_i|=1<<r;
6273 if((branch_regs[i].regmap[r]&63)==dops[i+1].rt1) will_dirty_i|=1<<r;
6274 if((branch_regs[i].regmap[r]&63)==dops[i+1].rt2) will_dirty_i|=1<<r;
6275 if((branch_regs[i].regmap[r]&63)>33) will_dirty_i&=~(1<<r);
6276 if(branch_regs[i].regmap[r]==0) will_dirty_i&=~(1<<r);
6277 if(branch_regs[i].regmap[r]==CCREG) will_dirty_i|=1<<r;
6278 //if((regs[i].regmap[r]&63)==dops[i].rt1) will_dirty_i|=1<<r;
6279 //if((regs[i].regmap[r]&63)==dops[i].rt2) will_dirty_i|=1<<r;
6280 if((regs[i].regmap[r]&63)==dops[i+1].rt1) will_dirty_i|=1<<r;
6281 if((regs[i].regmap[r]&63)==dops[i+1].rt2) will_dirty_i|=1<<r;
6282 if((regs[i].regmap[r]&63)>33) will_dirty_i&=~(1<<r);
6283 if(regs[i].regmap[r]<=0) will_dirty_i&=~(1<<r);
6284 if(regs[i].regmap[r]==CCREG) will_dirty_i|=1<<r;
6289 // Merge in delay slot (wont dirty)
6290 for(r=0;r<HOST_REGS;r++) {
6291 if(r!=EXCLUDE_REG) {
6292 if((regs[i].regmap[r]&63)==dops[i].rt1) wont_dirty_i|=1<<r;
6293 if((regs[i].regmap[r]&63)==dops[i].rt2) wont_dirty_i|=1<<r;
6294 if((regs[i].regmap[r]&63)==dops[i+1].rt1) wont_dirty_i|=1<<r;
6295 if((regs[i].regmap[r]&63)==dops[i+1].rt2) wont_dirty_i|=1<<r;
6296 if(regs[i].regmap[r]==CCREG) wont_dirty_i|=1<<r;
6297 if((branch_regs[i].regmap[r]&63)==dops[i].rt1) wont_dirty_i|=1<<r;
6298 if((branch_regs[i].regmap[r]&63)==dops[i].rt2) wont_dirty_i|=1<<r;
6299 if((branch_regs[i].regmap[r]&63)==dops[i+1].rt1) wont_dirty_i|=1<<r;
6300 if((branch_regs[i].regmap[r]&63)==dops[i+1].rt2) wont_dirty_i|=1<<r;
6301 if(branch_regs[i].regmap[r]==CCREG) wont_dirty_i|=1<<r;
6305 #ifndef DESTRUCTIVE_WRITEBACK
6306 branch_regs[i].dirty&=wont_dirty_i;
6308 branch_regs[i].dirty|=will_dirty_i;
6314 if(ba[i]<=start+i*4) {
6318 // Unconditional branch
6321 // Merge in delay slot (will dirty)
6322 for(r=0;r<HOST_REGS;r++) {
6323 if(r!=EXCLUDE_REG) {
6324 if((branch_regs[i].regmap[r]&63)==dops[i].rt1) temp_will_dirty|=1<<r;
6325 if((branch_regs[i].regmap[r]&63)==dops[i].rt2) temp_will_dirty|=1<<r;
6326 if((branch_regs[i].regmap[r]&63)==dops[i+1].rt1) temp_will_dirty|=1<<r;
6327 if((branch_regs[i].regmap[r]&63)==dops[i+1].rt2) temp_will_dirty|=1<<r;
6328 if((branch_regs[i].regmap[r]&63)>33) temp_will_dirty&=~(1<<r);
6329 if(branch_regs[i].regmap[r]<=0) temp_will_dirty&=~(1<<r);
6330 if(branch_regs[i].regmap[r]==CCREG) temp_will_dirty|=1<<r;
6331 if((regs[i].regmap[r]&63)==dops[i].rt1) temp_will_dirty|=1<<r;
6332 if((regs[i].regmap[r]&63)==dops[i].rt2) temp_will_dirty|=1<<r;
6333 if((regs[i].regmap[r]&63)==dops[i+1].rt1) temp_will_dirty|=1<<r;
6334 if((regs[i].regmap[r]&63)==dops[i+1].rt2) temp_will_dirty|=1<<r;
6335 if((regs[i].regmap[r]&63)>33) temp_will_dirty&=~(1<<r);
6336 if(regs[i].regmap[r]<=0) temp_will_dirty&=~(1<<r);
6337 if(regs[i].regmap[r]==CCREG) temp_will_dirty|=1<<r;
6341 // Conditional branch (not taken case)
6342 temp_will_dirty=will_dirty_next;
6343 temp_wont_dirty=wont_dirty_next;
6344 // Merge in delay slot (will dirty)
6345 for(r=0;r<HOST_REGS;r++) {
6346 if(r!=EXCLUDE_REG) {
6347 if(!dops[i].likely) {
6348 // Will not dirty if likely branch is not taken
6349 if((branch_regs[i].regmap[r]&63)==dops[i].rt1) temp_will_dirty|=1<<r;
6350 if((branch_regs[i].regmap[r]&63)==dops[i].rt2) temp_will_dirty|=1<<r;
6351 if((branch_regs[i].regmap[r]&63)==dops[i+1].rt1) temp_will_dirty|=1<<r;
6352 if((branch_regs[i].regmap[r]&63)==dops[i+1].rt2) temp_will_dirty|=1<<r;
6353 if((branch_regs[i].regmap[r]&63)>33) temp_will_dirty&=~(1<<r);
6354 if(branch_regs[i].regmap[r]==0) temp_will_dirty&=~(1<<r);
6355 if(branch_regs[i].regmap[r]==CCREG) temp_will_dirty|=1<<r;
6356 //if((regs[i].regmap[r]&63)==dops[i].rt1) temp_will_dirty|=1<<r;
6357 //if((regs[i].regmap[r]&63)==dops[i].rt2) temp_will_dirty|=1<<r;
6358 if((regs[i].regmap[r]&63)==dops[i+1].rt1) temp_will_dirty|=1<<r;
6359 if((regs[i].regmap[r]&63)==dops[i+1].rt2) temp_will_dirty|=1<<r;
6360 if((regs[i].regmap[r]&63)>33) temp_will_dirty&=~(1<<r);
6361 if(regs[i].regmap[r]<=0) temp_will_dirty&=~(1<<r);
6362 if(regs[i].regmap[r]==CCREG) temp_will_dirty|=1<<r;
6367 // Merge in delay slot (wont dirty)
6368 for(r=0;r<HOST_REGS;r++) {
6369 if(r!=EXCLUDE_REG) {
6370 if((regs[i].regmap[r]&63)==dops[i].rt1) temp_wont_dirty|=1<<r;
6371 if((regs[i].regmap[r]&63)==dops[i].rt2) temp_wont_dirty|=1<<r;
6372 if((regs[i].regmap[r]&63)==dops[i+1].rt1) temp_wont_dirty|=1<<r;
6373 if((regs[i].regmap[r]&63)==dops[i+1].rt2) temp_wont_dirty|=1<<r;
6374 if(regs[i].regmap[r]==CCREG) temp_wont_dirty|=1<<r;
6375 if((branch_regs[i].regmap[r]&63)==dops[i].rt1) temp_wont_dirty|=1<<r;
6376 if((branch_regs[i].regmap[r]&63)==dops[i].rt2) temp_wont_dirty|=1<<r;
6377 if((branch_regs[i].regmap[r]&63)==dops[i+1].rt1) temp_wont_dirty|=1<<r;
6378 if((branch_regs[i].regmap[r]&63)==dops[i+1].rt2) temp_wont_dirty|=1<<r;
6379 if(branch_regs[i].regmap[r]==CCREG) temp_wont_dirty|=1<<r;
6382 // Deal with changed mappings
6384 for(r=0;r<HOST_REGS;r++) {
6385 if(r!=EXCLUDE_REG) {
6386 if(regs[i].regmap[r]!=regmap_pre[i][r]) {
6387 temp_will_dirty&=~(1<<r);
6388 temp_wont_dirty&=~(1<<r);
6389 if((regmap_pre[i][r]&63)>0 && (regmap_pre[i][r]&63)<34) {
6390 temp_will_dirty|=((unneeded_reg[i]>>(regmap_pre[i][r]&63))&1)<<r;
6391 temp_wont_dirty|=((unneeded_reg[i]>>(regmap_pre[i][r]&63))&1)<<r;
6393 temp_will_dirty|=1<<r;
6394 temp_wont_dirty|=1<<r;
6401 will_dirty[i]=temp_will_dirty;
6402 wont_dirty[i]=temp_wont_dirty;
6403 clean_registers((ba[i]-start)>>2,i-1,0);
6405 // Limit recursion. It can take an excessive amount
6406 // of time if there are a lot of nested loops.
6407 will_dirty[(ba[i]-start)>>2]=0;
6408 wont_dirty[(ba[i]-start)>>2]=-1;
6415 // Unconditional branch
6418 //if(ba[i]>start+i*4) { // Disable recursion (for debugging)
6419 for(r=0;r<HOST_REGS;r++) {
6420 if(r!=EXCLUDE_REG) {
6421 if(branch_regs[i].regmap[r]==regs[(ba[i]-start)>>2].regmap_entry[r]) {
6422 will_dirty_i|=will_dirty[(ba[i]-start)>>2]&(1<<r);
6423 wont_dirty_i|=wont_dirty[(ba[i]-start)>>2]&(1<<r);
6425 if(branch_regs[i].regmap[r]>=0) {
6426 will_dirty_i|=((unneeded_reg[(ba[i]-start)>>2]>>(branch_regs[i].regmap[r]&63))&1)<<r;
6427 wont_dirty_i|=((unneeded_reg[(ba[i]-start)>>2]>>(branch_regs[i].regmap[r]&63))&1)<<r;
6432 // Merge in delay slot
6433 for(r=0;r<HOST_REGS;r++) {
6434 if(r!=EXCLUDE_REG) {
6435 if((branch_regs[i].regmap[r]&63)==dops[i].rt1) will_dirty_i|=1<<r;
6436 if((branch_regs[i].regmap[r]&63)==dops[i].rt2) will_dirty_i|=1<<r;
6437 if((branch_regs[i].regmap[r]&63)==dops[i+1].rt1) will_dirty_i|=1<<r;
6438 if((branch_regs[i].regmap[r]&63)==dops[i+1].rt2) will_dirty_i|=1<<r;
6439 if((branch_regs[i].regmap[r]&63)>33) will_dirty_i&=~(1<<r);
6440 if(branch_regs[i].regmap[r]<=0) will_dirty_i&=~(1<<r);
6441 if(branch_regs[i].regmap[r]==CCREG) will_dirty_i|=1<<r;
6442 if((regs[i].regmap[r]&63)==dops[i].rt1) will_dirty_i|=1<<r;
6443 if((regs[i].regmap[r]&63)==dops[i].rt2) will_dirty_i|=1<<r;
6444 if((regs[i].regmap[r]&63)==dops[i+1].rt1) will_dirty_i|=1<<r;
6445 if((regs[i].regmap[r]&63)==dops[i+1].rt2) will_dirty_i|=1<<r;
6446 if((regs[i].regmap[r]&63)>33) will_dirty_i&=~(1<<r);
6447 if(regs[i].regmap[r]<=0) will_dirty_i&=~(1<<r);
6448 if(regs[i].regmap[r]==CCREG) will_dirty_i|=1<<r;
6452 // Conditional branch
6453 will_dirty_i=will_dirty_next;
6454 wont_dirty_i=wont_dirty_next;
6455 //if(ba[i]>start+i*4) { // Disable recursion (for debugging)
6456 for(r=0;r<HOST_REGS;r++) {
6457 if(r!=EXCLUDE_REG) {
6458 signed char target_reg=branch_regs[i].regmap[r];
6459 if(target_reg==regs[(ba[i]-start)>>2].regmap_entry[r]) {
6460 will_dirty_i&=will_dirty[(ba[i]-start)>>2]&(1<<r);
6461 wont_dirty_i|=wont_dirty[(ba[i]-start)>>2]&(1<<r);
6463 else if(target_reg>=0) {
6464 will_dirty_i&=((unneeded_reg[(ba[i]-start)>>2]>>(target_reg&63))&1)<<r;
6465 wont_dirty_i|=((unneeded_reg[(ba[i]-start)>>2]>>(target_reg&63))&1)<<r;
6470 // Merge in delay slot
6471 for(r=0;r<HOST_REGS;r++) {
6472 if(r!=EXCLUDE_REG) {
6473 if(!dops[i].likely) {
6474 // Might not dirty if likely branch is not taken
6475 if((branch_regs[i].regmap[r]&63)==dops[i].rt1) will_dirty_i|=1<<r;
6476 if((branch_regs[i].regmap[r]&63)==dops[i].rt2) will_dirty_i|=1<<r;
6477 if((branch_regs[i].regmap[r]&63)==dops[i+1].rt1) will_dirty_i|=1<<r;
6478 if((branch_regs[i].regmap[r]&63)==dops[i+1].rt2) will_dirty_i|=1<<r;
6479 if((branch_regs[i].regmap[r]&63)>33) will_dirty_i&=~(1<<r);
6480 if(branch_regs[i].regmap[r]<=0) will_dirty_i&=~(1<<r);
6481 if(branch_regs[i].regmap[r]==CCREG) will_dirty_i|=1<<r;
6482 //if((regs[i].regmap[r]&63)==dops[i].rt1) will_dirty_i|=1<<r;
6483 //if((regs[i].regmap[r]&63)==dops[i].rt2) will_dirty_i|=1<<r;
6484 if((regs[i].regmap[r]&63)==dops[i+1].rt1) will_dirty_i|=1<<r;
6485 if((regs[i].regmap[r]&63)==dops[i+1].rt2) will_dirty_i|=1<<r;
6486 if((regs[i].regmap[r]&63)>33) will_dirty_i&=~(1<<r);
6487 if(regs[i].regmap[r]<=0) will_dirty_i&=~(1<<r);
6488 if(regs[i].regmap[r]==CCREG) will_dirty_i|=1<<r;
6493 // Merge in delay slot (won't dirty)
6494 for(r=0;r<HOST_REGS;r++) {
6495 if(r!=EXCLUDE_REG) {
6496 if((regs[i].regmap[r]&63)==dops[i].rt1) wont_dirty_i|=1<<r;
6497 if((regs[i].regmap[r]&63)==dops[i].rt2) wont_dirty_i|=1<<r;
6498 if((regs[i].regmap[r]&63)==dops[i+1].rt1) wont_dirty_i|=1<<r;
6499 if((regs[i].regmap[r]&63)==dops[i+1].rt2) wont_dirty_i|=1<<r;
6500 if(regs[i].regmap[r]==CCREG) wont_dirty_i|=1<<r;
6501 if((branch_regs[i].regmap[r]&63)==dops[i].rt1) wont_dirty_i|=1<<r;
6502 if((branch_regs[i].regmap[r]&63)==dops[i].rt2) wont_dirty_i|=1<<r;
6503 if((branch_regs[i].regmap[r]&63)==dops[i+1].rt1) wont_dirty_i|=1<<r;
6504 if((branch_regs[i].regmap[r]&63)==dops[i+1].rt2) wont_dirty_i|=1<<r;
6505 if(branch_regs[i].regmap[r]==CCREG) wont_dirty_i|=1<<r;
6509 #ifndef DESTRUCTIVE_WRITEBACK
6510 branch_regs[i].dirty&=wont_dirty_i;
6512 branch_regs[i].dirty|=will_dirty_i;
6517 else if(dops[i].itype==SYSCALL||dops[i].itype==HLECALL||dops[i].itype==INTCALL)
6519 // SYSCALL instruction (software interrupt)
6523 else if(dops[i].itype==COP0 && (source[i]&0x3f)==0x18)
6525 // ERET instruction (return from interrupt)
6529 will_dirty_next=will_dirty_i;
6530 wont_dirty_next=wont_dirty_i;
6531 for(r=0;r<HOST_REGS;r++) {
6532 if(r!=EXCLUDE_REG) {
6533 if((regs[i].regmap[r]&63)==dops[i].rt1) will_dirty_i|=1<<r;
6534 if((regs[i].regmap[r]&63)==dops[i].rt2) will_dirty_i|=1<<r;
6535 if((regs[i].regmap[r]&63)>33) will_dirty_i&=~(1<<r);
6536 if(regs[i].regmap[r]<=0) will_dirty_i&=~(1<<r);
6537 if(regs[i].regmap[r]==CCREG) will_dirty_i|=1<<r;
6538 if((regs[i].regmap[r]&63)==dops[i].rt1) wont_dirty_i|=1<<r;
6539 if((regs[i].regmap[r]&63)==dops[i].rt2) wont_dirty_i|=1<<r;
6540 if(regs[i].regmap[r]==CCREG) wont_dirty_i|=1<<r;
6542 if(dops[i].itype!=RJUMP&&dops[i].itype!=UJUMP&&dops[i].itype!=CJUMP&&dops[i].itype!=SJUMP)
6544 // Don't store a register immediately after writing it,
6545 // may prevent dual-issue.
6546 if((regs[i].regmap[r]&63)==dops[i-1].rt1) wont_dirty_i|=1<<r;
6547 if((regs[i].regmap[r]&63)==dops[i-1].rt2) wont_dirty_i|=1<<r;
6553 will_dirty[i]=will_dirty_i;
6554 wont_dirty[i]=wont_dirty_i;
6555 // Mark registers that won't be dirtied as not dirty
6557 regs[i].dirty|=will_dirty_i;
6558 #ifndef DESTRUCTIVE_WRITEBACK
6559 regs[i].dirty&=wont_dirty_i;
6560 if(dops[i].itype==RJUMP||dops[i].itype==UJUMP||dops[i].itype==CJUMP||dops[i].itype==SJUMP)
6562 if (i < iend-1 && !is_ujump(i)) {
6563 for(r=0;r<HOST_REGS;r++) {
6564 if(r!=EXCLUDE_REG) {
6565 if(regs[i].regmap[r]==regmap_pre[i+2][r]) {
6566 regs[i+2].wasdirty&=wont_dirty_i|~(1<<r);
6567 }else {/*printf("i: %x (%d) mismatch(+2): %d\n",start+i*4,i,r);assert(!((wont_dirty_i>>r)&1));*/}
6575 for(r=0;r<HOST_REGS;r++) {
6576 if(r!=EXCLUDE_REG) {
6577 if(regs[i].regmap[r]==regmap_pre[i+1][r]) {
6578 regs[i+1].wasdirty&=wont_dirty_i|~(1<<r);
6579 }else {/*printf("i: %x (%d) mismatch(+1): %d\n",start+i*4,i,r);assert(!((wont_dirty_i>>r)&1));*/}
6587 // Deal with changed mappings
6588 temp_will_dirty=will_dirty_i;
6589 temp_wont_dirty=wont_dirty_i;
6590 for(r=0;r<HOST_REGS;r++) {
6591 if(r!=EXCLUDE_REG) {
6593 if(regs[i].regmap[r]==regmap_pre[i][r]) {
6595 #ifndef DESTRUCTIVE_WRITEBACK
6596 regs[i].wasdirty&=wont_dirty_i|~(1<<r);
6598 regs[i].wasdirty|=will_dirty_i&(1<<r);
6601 else if(regmap_pre[i][r]>=0&&(nr=get_reg(regs[i].regmap,regmap_pre[i][r]))>=0) {
6602 // Register moved to a different register
6603 will_dirty_i&=~(1<<r);
6604 wont_dirty_i&=~(1<<r);
6605 will_dirty_i|=((temp_will_dirty>>nr)&1)<<r;
6606 wont_dirty_i|=((temp_wont_dirty>>nr)&1)<<r;
6608 #ifndef DESTRUCTIVE_WRITEBACK
6609 regs[i].wasdirty&=wont_dirty_i|~(1<<r);
6611 regs[i].wasdirty|=will_dirty_i&(1<<r);
6615 will_dirty_i&=~(1<<r);
6616 wont_dirty_i&=~(1<<r);
6617 if((regmap_pre[i][r]&63)>0 && (regmap_pre[i][r]&63)<34) {
6618 will_dirty_i|=((unneeded_reg[i]>>(regmap_pre[i][r]&63))&1)<<r;
6619 wont_dirty_i|=((unneeded_reg[i]>>(regmap_pre[i][r]&63))&1)<<r;
6622 /*printf("i: %x (%d) mismatch: %d\n",start+i*4,i,r);assert(!((will_dirty>>r)&1));*/
6632 void disassemble_inst(int i)
6634 if (dops[i].bt) printf("*"); else printf(" ");
6635 switch(dops[i].itype) {
6637 printf (" %x: %s %8x\n",start+i*4,insn[i],ba[i]);break;
6639 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;
6641 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;
6643 if (dops[i].opcode==0x9&&dops[i].rt1!=31)
6644 printf (" %x: %s r%d,r%d\n",start+i*4,insn[i],dops[i].rt1,dops[i].rs1);
6646 printf (" %x: %s r%d\n",start+i*4,insn[i],dops[i].rs1);
6649 printf (" %x: %s (pagespan) r%d,r%d,%8x\n",start+i*4,insn[i],dops[i].rs1,dops[i].rs2,ba[i]);break;
6651 if(dops[i].opcode==0xf) //LUI
6652 printf (" %x: %s r%d,%4x0000\n",start+i*4,insn[i],dops[i].rt1,imm[i]&0xffff);
6654 printf (" %x: %s r%d,r%d,%d\n",start+i*4,insn[i],dops[i].rt1,dops[i].rs1,imm[i]);
6658 printf (" %x: %s r%d,r%d+%x\n",start+i*4,insn[i],dops[i].rt1,dops[i].rs1,imm[i]);
6662 printf (" %x: %s r%d,r%d+%x\n",start+i*4,insn[i],dops[i].rs2,dops[i].rs1,imm[i]);
6666 printf (" %x: %s r%d,r%d,r%d\n",start+i*4,insn[i],dops[i].rt1,dops[i].rs1,dops[i].rs2);
6669 printf (" %x: %s r%d,r%d\n",start+i*4,insn[i],dops[i].rs1,dops[i].rs2);
6672 printf (" %x: %s r%d,r%d,%d\n",start+i*4,insn[i],dops[i].rt1,dops[i].rs1,imm[i]);
6675 if((dops[i].opcode2&0x1d)==0x10)
6676 printf (" %x: %s r%d\n",start+i*4,insn[i],dops[i].rt1);
6677 else if((dops[i].opcode2&0x1d)==0x11)
6678 printf (" %x: %s r%d\n",start+i*4,insn[i],dops[i].rs1);
6680 printf (" %x: %s\n",start+i*4,insn[i]);
6683 if(dops[i].opcode2==0)
6684 printf (" %x: %s r%d,cpr0[%d]\n",start+i*4,insn[i],dops[i].rt1,(source[i]>>11)&0x1f); // MFC0
6685 else if(dops[i].opcode2==4)
6686 printf (" %x: %s r%d,cpr0[%d]\n",start+i*4,insn[i],dops[i].rs1,(source[i]>>11)&0x1f); // MTC0
6687 else printf (" %x: %s\n",start+i*4,insn[i]);
6690 if(dops[i].opcode2<3)
6691 printf (" %x: %s r%d,cpr1[%d]\n",start+i*4,insn[i],dops[i].rt1,(source[i]>>11)&0x1f); // MFC1
6692 else if(dops[i].opcode2>3)
6693 printf (" %x: %s r%d,cpr1[%d]\n",start+i*4,insn[i],dops[i].rs1,(source[i]>>11)&0x1f); // MTC1
6694 else printf (" %x: %s\n",start+i*4,insn[i]);
6697 if(dops[i].opcode2<3)
6698 printf (" %x: %s r%d,cpr2[%d]\n",start+i*4,insn[i],dops[i].rt1,(source[i]>>11)&0x1f); // MFC2
6699 else if(dops[i].opcode2>3)
6700 printf (" %x: %s r%d,cpr2[%d]\n",start+i*4,insn[i],dops[i].rs1,(source[i]>>11)&0x1f); // MTC2
6701 else printf (" %x: %s\n",start+i*4,insn[i]);
6704 printf (" %x: %s cpr1[%d],r%d+%x\n",start+i*4,insn[i],(source[i]>>16)&0x1f,dops[i].rs1,imm[i]);
6707 printf (" %x: %s cpr2[%d],r%d+%x\n",start+i*4,insn[i],(source[i]>>16)&0x1f,dops[i].rs1,imm[i]);
6710 printf (" %x: %s (INTCALL)\n",start+i*4,insn[i]);
6713 //printf (" %s %8x\n",insn[i],source[i]);
6714 printf (" %x: %s\n",start+i*4,insn[i]);
6718 static void disassemble_inst(int i) {}
6721 #define DRC_TEST_VAL 0x74657374
6723 static void new_dynarec_test(void)
6725 int (*testfunc)(void);
6730 // check structure linkage
6731 if ((u_char *)rcnts - (u_char *)&psxRegs != sizeof(psxRegs))
6733 SysPrintf("linkage_arm* miscompilation/breakage detected.\n");
6736 SysPrintf("testing if we can run recompiled code...\n");
6737 ((volatile u_int *)out)[0]++; // make cache dirty
6739 for (i = 0; i < ARRAY_SIZE(ret); i++) {
6740 out = ndrc->translation_cache;
6741 beginning = start_block();
6742 emit_movimm(DRC_TEST_VAL + i, 0); // test
6745 end_block(beginning);
6746 testfunc = beginning;
6747 ret[i] = testfunc();
6750 if (ret[0] == DRC_TEST_VAL && ret[1] == DRC_TEST_VAL + 1)
6751 SysPrintf("test passed.\n");
6753 SysPrintf("test failed, will likely crash soon (r=%08x %08x)\n", ret[0], ret[1]);
6754 out = ndrc->translation_cache;
6757 // clear the state completely, instead of just marking
6758 // things invalid like invalidate_all_pages() does
6759 void new_dynarec_clear_full(void)
6762 out = ndrc->translation_cache;
6763 memset(invalid_code,1,sizeof(invalid_code));
6764 memset(hash_table,0xff,sizeof(hash_table));
6765 memset(mini_ht,-1,sizeof(mini_ht));
6766 memset(restore_candidate,0,sizeof(restore_candidate));
6767 memset(shadow,0,sizeof(shadow));
6769 expirep=16384; // Expiry pointer, +2 blocks
6770 pending_exception=0;
6773 inv_code_start=inv_code_end=~0;
6775 for(n=0;n<4096;n++) ll_clear(jump_in+n);
6776 for(n=0;n<4096;n++) ll_clear(jump_out+n);
6777 for(n=0;n<4096;n++) ll_clear(jump_dirty+n);
6779 cycle_multiplier_old = cycle_multiplier;
6780 new_dynarec_hacks_old = new_dynarec_hacks;
6783 void new_dynarec_init(void)
6785 SysPrintf("Init new dynarec\n");
6787 #ifdef BASE_ADDR_DYNAMIC
6789 sceBlock = sceKernelAllocMemBlockForVM("code", 1 << TARGET_SIZE_2);
6791 SysPrintf("sceKernelAllocMemBlockForVM failed\n");
6792 int ret = sceKernelGetMemBlockBase(sceBlock, (void **)&ndrc);
6794 SysPrintf("sceKernelGetMemBlockBase failed\n");
6796 uintptr_t desired_addr = 0;
6799 desired_addr = ((uintptr_t)&_end + 0xffffff) & ~0xffffffl;
6801 ndrc = mmap((void *)desired_addr, sizeof(*ndrc),
6802 PROT_READ | PROT_WRITE | PROT_EXEC,
6803 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
6804 if (ndrc == MAP_FAILED) {
6805 SysPrintf("mmap() failed: %s\n", strerror(errno));
6810 #ifndef NO_WRITE_EXEC
6811 // not all systems allow execute in data segment by default
6812 if (mprotect(ndrc, sizeof(ndrc->translation_cache) + sizeof(ndrc->tramp.ops),
6813 PROT_READ | PROT_WRITE | PROT_EXEC) != 0)
6814 SysPrintf("mprotect() failed: %s\n", strerror(errno));
6817 out = ndrc->translation_cache;
6818 cycle_multiplier=200;
6819 new_dynarec_clear_full();
6821 // Copy this into local area so we don't have to put it in every literal pool
6822 invc_ptr=invalid_code;
6827 ram_offset=(uintptr_t)rdram-0x80000000;
6830 SysPrintf("warning: RAM is not directly mapped, performance will suffer\n");
6833 void new_dynarec_cleanup(void)
6836 #ifdef BASE_ADDR_DYNAMIC
6838 sceKernelFreeMemBlock(sceBlock);
6841 if (munmap(ndrc, sizeof(*ndrc)) < 0)
6842 SysPrintf("munmap() failed\n");
6845 for(n=0;n<4096;n++) ll_clear(jump_in+n);
6846 for(n=0;n<4096;n++) ll_clear(jump_out+n);
6847 for(n=0;n<4096;n++) ll_clear(jump_dirty+n);
6849 if (munmap (ROM_COPY, 67108864) < 0) {SysPrintf("munmap() failed\n");}
6853 static u_int *get_source_start(u_int addr, u_int *limit)
6855 if (!HACK_ENABLED(NDHACK_OVERRIDE_CYCLE_M))
6856 cycle_multiplier_override = 0;
6858 if (addr < 0x00200000 ||
6859 (0xa0000000 <= addr && addr < 0xa0200000))
6861 // used for BIOS calls mostly?
6862 *limit = (addr&0xa0000000)|0x00200000;
6863 return (u_int *)(rdram + (addr&0x1fffff));
6865 else if (!Config.HLE && (
6866 /* (0x9fc00000 <= addr && addr < 0x9fc80000) ||*/
6867 (0xbfc00000 <= addr && addr < 0xbfc80000)))
6869 // BIOS. The multiplier should be much higher as it's uncached 8bit mem,
6870 // but timings in PCSX are too tied to the interpreter's BIAS
6871 if (!HACK_ENABLED(NDHACK_OVERRIDE_CYCLE_M))
6872 cycle_multiplier_override = 200;
6874 *limit = (addr & 0xfff00000) | 0x80000;
6875 return (u_int *)((u_char *)psxR + (addr&0x7ffff));
6877 else if (addr >= 0x80000000 && addr < 0x80000000+RAM_SIZE) {
6878 *limit = (addr & 0x80600000) + 0x00200000;
6879 return (u_int *)(rdram + (addr&0x1fffff));
6884 static u_int scan_for_ret(u_int addr)
6889 mem = get_source_start(addr, &limit);
6893 if (limit > addr + 0x1000)
6894 limit = addr + 0x1000;
6895 for (; addr < limit; addr += 4, mem++) {
6896 if (*mem == 0x03e00008) // jr $ra
6902 struct savestate_block {
6907 static int addr_cmp(const void *p1_, const void *p2_)
6909 const struct savestate_block *p1 = p1_, *p2 = p2_;
6910 return p1->addr - p2->addr;
6913 int new_dynarec_save_blocks(void *save, int size)
6915 struct savestate_block *blocks = save;
6916 int maxcount = size / sizeof(blocks[0]);
6917 struct savestate_block tmp_blocks[1024];
6918 struct ll_entry *head;
6919 int p, s, d, o, bcnt;
6923 for (p = 0; p < ARRAY_SIZE(jump_in); p++) {
6925 for (head = jump_in[p]; head != NULL; head = head->next) {
6926 tmp_blocks[bcnt].addr = head->vaddr;
6927 tmp_blocks[bcnt].regflags = head->reg_sv_flags;
6932 qsort(tmp_blocks, bcnt, sizeof(tmp_blocks[0]), addr_cmp);
6934 addr = tmp_blocks[0].addr;
6935 for (s = d = 0; s < bcnt; s++) {
6936 if (tmp_blocks[s].addr < addr)
6938 if (d == 0 || tmp_blocks[d-1].addr != tmp_blocks[s].addr)
6939 tmp_blocks[d++] = tmp_blocks[s];
6940 addr = scan_for_ret(tmp_blocks[s].addr);
6943 if (o + d > maxcount)
6945 memcpy(&blocks[o], tmp_blocks, d * sizeof(blocks[0]));
6949 return o * sizeof(blocks[0]);
6952 void new_dynarec_load_blocks(const void *save, int size)
6954 const struct savestate_block *blocks = save;
6955 int count = size / sizeof(blocks[0]);
6956 u_int regs_save[32];
6960 get_addr(psxRegs.pc);
6962 // change GPRs for speculation to at least partially work..
6963 memcpy(regs_save, &psxRegs.GPR, sizeof(regs_save));
6964 for (i = 1; i < 32; i++)
6965 psxRegs.GPR.r[i] = 0x80000000;
6967 for (b = 0; b < count; b++) {
6968 for (f = blocks[b].regflags, i = 0; f; f >>= 1, i++) {
6970 psxRegs.GPR.r[i] = 0x1f800000;
6973 get_addr(blocks[b].addr);
6975 for (f = blocks[b].regflags, i = 0; f; f >>= 1, i++) {
6977 psxRegs.GPR.r[i] = 0x80000000;
6981 memcpy(&psxRegs.GPR, regs_save, sizeof(regs_save));
6984 int new_recompile_block(u_int addr)
6986 u_int pagelimit = 0;
6987 u_int state_rflags = 0;
6990 assem_debug("NOTCOMPILED: addr = %x -> %p\n", addr, out);
6991 //printf("TRACE: count=%d next=%d (compile %x)\n",Count,next_interupt,addr);
6993 //printf("fpu mapping=%x enabled=%x\n",(Status & 0x04000000)>>26,(Status & 0x20000000)>>29);
6995 // this is just for speculation
6996 for (i = 1; i < 32; i++) {
6997 if ((psxRegs.GPR.r[i] & 0xffff0000) == 0x1f800000)
6998 state_rflags |= 1 << i;
7001 start = (u_int)addr&~3;
7002 //assert(((u_int)addr&1)==0); // start-in-delay-slot flag
7003 new_dynarec_did_compile=1;
7004 if (Config.HLE && start == 0x80001000) // hlecall
7006 // XXX: is this enough? Maybe check hleSoftCall?
7007 void *beginning=start_block();
7008 u_int page=get_page(start);
7010 invalid_code[start>>12]=0;
7011 emit_movimm(start,0);
7012 emit_writeword(0,&pcaddr);
7013 emit_far_jump(new_dyna_leave);
7015 end_block(beginning);
7016 ll_add_flags(jump_in+page,start,state_rflags,(void *)beginning);
7020 source = get_source_start(start, &pagelimit);
7021 if (source == NULL) {
7022 SysPrintf("Compile at bogus memory address: %08x\n", addr);
7026 /* Pass 1: disassemble */
7027 /* Pass 2: register dependencies, branch targets */
7028 /* Pass 3: register allocation */
7029 /* Pass 4: branch dependencies */
7030 /* Pass 5: pre-alloc */
7031 /* Pass 6: optimize clean/dirty state */
7032 /* Pass 7: flag 32-bit registers */
7033 /* Pass 8: assembly */
7034 /* Pass 9: linker */
7035 /* Pass 10: garbage collection / free memory */
7039 unsigned int type,op,op2;
7041 //printf("addr = %x source = %x %x\n", addr,source,source[0]);
7043 /* Pass 1 disassembly */
7045 for(i=0;!done;i++) {
7050 minimum_free_regs[i]=0;
7051 dops[i].opcode=op=source[i]>>26;
7054 case 0x00: strcpy(insn[i],"special"); type=NI;
7058 case 0x00: strcpy(insn[i],"SLL"); type=SHIFTIMM; break;
7059 case 0x02: strcpy(insn[i],"SRL"); type=SHIFTIMM; break;
7060 case 0x03: strcpy(insn[i],"SRA"); type=SHIFTIMM; break;
7061 case 0x04: strcpy(insn[i],"SLLV"); type=SHIFT; break;
7062 case 0x06: strcpy(insn[i],"SRLV"); type=SHIFT; break;
7063 case 0x07: strcpy(insn[i],"SRAV"); type=SHIFT; break;
7064 case 0x08: strcpy(insn[i],"JR"); type=RJUMP; break;
7065 case 0x09: strcpy(insn[i],"JALR"); type=RJUMP; break;
7066 case 0x0C: strcpy(insn[i],"SYSCALL"); type=SYSCALL; break;
7067 case 0x0D: strcpy(insn[i],"BREAK"); type=OTHER; break;
7068 case 0x0F: strcpy(insn[i],"SYNC"); type=OTHER; break;
7069 case 0x10: strcpy(insn[i],"MFHI"); type=MOV; break;
7070 case 0x11: strcpy(insn[i],"MTHI"); type=MOV; break;
7071 case 0x12: strcpy(insn[i],"MFLO"); type=MOV; break;
7072 case 0x13: strcpy(insn[i],"MTLO"); type=MOV; break;
7073 case 0x18: strcpy(insn[i],"MULT"); type=MULTDIV; break;
7074 case 0x19: strcpy(insn[i],"MULTU"); type=MULTDIV; break;
7075 case 0x1A: strcpy(insn[i],"DIV"); type=MULTDIV; break;
7076 case 0x1B: strcpy(insn[i],"DIVU"); type=MULTDIV; break;
7077 case 0x20: strcpy(insn[i],"ADD"); type=ALU; break;
7078 case 0x21: strcpy(insn[i],"ADDU"); type=ALU; break;
7079 case 0x22: strcpy(insn[i],"SUB"); type=ALU; break;
7080 case 0x23: strcpy(insn[i],"SUBU"); type=ALU; break;
7081 case 0x24: strcpy(insn[i],"AND"); type=ALU; break;
7082 case 0x25: strcpy(insn[i],"OR"); type=ALU; break;
7083 case 0x26: strcpy(insn[i],"XOR"); type=ALU; break;
7084 case 0x27: strcpy(insn[i],"NOR"); type=ALU; break;
7085 case 0x2A: strcpy(insn[i],"SLT"); type=ALU; break;
7086 case 0x2B: strcpy(insn[i],"SLTU"); type=ALU; break;
7087 case 0x30: strcpy(insn[i],"TGE"); type=NI; break;
7088 case 0x31: strcpy(insn[i],"TGEU"); type=NI; break;
7089 case 0x32: strcpy(insn[i],"TLT"); type=NI; break;
7090 case 0x33: strcpy(insn[i],"TLTU"); type=NI; break;
7091 case 0x34: strcpy(insn[i],"TEQ"); type=NI; break;
7092 case 0x36: strcpy(insn[i],"TNE"); type=NI; break;
7094 case 0x14: strcpy(insn[i],"DSLLV"); type=SHIFT; break;
7095 case 0x16: strcpy(insn[i],"DSRLV"); type=SHIFT; break;
7096 case 0x17: strcpy(insn[i],"DSRAV"); type=SHIFT; break;
7097 case 0x1C: strcpy(insn[i],"DMULT"); type=MULTDIV; break;
7098 case 0x1D: strcpy(insn[i],"DMULTU"); type=MULTDIV; break;
7099 case 0x1E: strcpy(insn[i],"DDIV"); type=MULTDIV; break;
7100 case 0x1F: strcpy(insn[i],"DDIVU"); type=MULTDIV; break;
7101 case 0x2C: strcpy(insn[i],"DADD"); type=ALU; break;
7102 case 0x2D: strcpy(insn[i],"DADDU"); type=ALU; break;
7103 case 0x2E: strcpy(insn[i],"DSUB"); type=ALU; break;
7104 case 0x2F: strcpy(insn[i],"DSUBU"); type=ALU; break;
7105 case 0x38: strcpy(insn[i],"DSLL"); type=SHIFTIMM; break;
7106 case 0x3A: strcpy(insn[i],"DSRL"); type=SHIFTIMM; break;
7107 case 0x3B: strcpy(insn[i],"DSRA"); type=SHIFTIMM; break;
7108 case 0x3C: strcpy(insn[i],"DSLL32"); type=SHIFTIMM; break;
7109 case 0x3E: strcpy(insn[i],"DSRL32"); type=SHIFTIMM; break;
7110 case 0x3F: strcpy(insn[i],"DSRA32"); type=SHIFTIMM; break;
7114 case 0x01: strcpy(insn[i],"regimm"); type=NI;
7115 op2=(source[i]>>16)&0x1f;
7118 case 0x00: strcpy(insn[i],"BLTZ"); type=SJUMP; break;
7119 case 0x01: strcpy(insn[i],"BGEZ"); type=SJUMP; break;
7120 //case 0x02: strcpy(insn[i],"BLTZL"); type=SJUMP; break;
7121 //case 0x03: strcpy(insn[i],"BGEZL"); type=SJUMP; break;
7122 //case 0x08: strcpy(insn[i],"TGEI"); type=NI; break;
7123 //case 0x09: strcpy(insn[i],"TGEIU"); type=NI; break;
7124 //case 0x0A: strcpy(insn[i],"TLTI"); type=NI; break;
7125 //case 0x0B: strcpy(insn[i],"TLTIU"); type=NI; break;
7126 //case 0x0C: strcpy(insn[i],"TEQI"); type=NI; break;
7127 //case 0x0E: strcpy(insn[i],"TNEI"); type=NI; break;
7128 case 0x10: strcpy(insn[i],"BLTZAL"); type=SJUMP; break;
7129 case 0x11: strcpy(insn[i],"BGEZAL"); type=SJUMP; break;
7130 //case 0x12: strcpy(insn[i],"BLTZALL"); type=SJUMP; break;
7131 //case 0x13: strcpy(insn[i],"BGEZALL"); type=SJUMP; break;
7134 case 0x02: strcpy(insn[i],"J"); type=UJUMP; break;
7135 case 0x03: strcpy(insn[i],"JAL"); type=UJUMP; break;
7136 case 0x04: strcpy(insn[i],"BEQ"); type=CJUMP; break;
7137 case 0x05: strcpy(insn[i],"BNE"); type=CJUMP; break;
7138 case 0x06: strcpy(insn[i],"BLEZ"); type=CJUMP; break;
7139 case 0x07: strcpy(insn[i],"BGTZ"); type=CJUMP; break;
7140 case 0x08: strcpy(insn[i],"ADDI"); type=IMM16; break;
7141 case 0x09: strcpy(insn[i],"ADDIU"); type=IMM16; break;
7142 case 0x0A: strcpy(insn[i],"SLTI"); type=IMM16; break;
7143 case 0x0B: strcpy(insn[i],"SLTIU"); type=IMM16; break;
7144 case 0x0C: strcpy(insn[i],"ANDI"); type=IMM16; break;
7145 case 0x0D: strcpy(insn[i],"ORI"); type=IMM16; break;
7146 case 0x0E: strcpy(insn[i],"XORI"); type=IMM16; break;
7147 case 0x0F: strcpy(insn[i],"LUI"); type=IMM16; break;
7148 case 0x10: strcpy(insn[i],"cop0"); type=NI;
7149 op2=(source[i]>>21)&0x1f;
7152 case 0x00: strcpy(insn[i],"MFC0"); type=COP0; break;
7153 case 0x02: strcpy(insn[i],"CFC0"); type=COP0; break;
7154 case 0x04: strcpy(insn[i],"MTC0"); type=COP0; break;
7155 case 0x06: strcpy(insn[i],"CTC0"); type=COP0; break;
7156 case 0x10: strcpy(insn[i],"RFE"); type=COP0; break;
7159 case 0x11: strcpy(insn[i],"cop1"); type=COP1;
7160 op2=(source[i]>>21)&0x1f;
7163 case 0x14: strcpy(insn[i],"BEQL"); type=CJUMP; break;
7164 case 0x15: strcpy(insn[i],"BNEL"); type=CJUMP; break;
7165 case 0x16: strcpy(insn[i],"BLEZL"); type=CJUMP; break;
7166 case 0x17: strcpy(insn[i],"BGTZL"); type=CJUMP; break;
7167 case 0x18: strcpy(insn[i],"DADDI"); type=IMM16; break;
7168 case 0x19: strcpy(insn[i],"DADDIU"); type=IMM16; break;
7169 case 0x1A: strcpy(insn[i],"LDL"); type=LOADLR; break;
7170 case 0x1B: strcpy(insn[i],"LDR"); type=LOADLR; break;
7172 case 0x20: strcpy(insn[i],"LB"); type=LOAD; break;
7173 case 0x21: strcpy(insn[i],"LH"); type=LOAD; break;
7174 case 0x22: strcpy(insn[i],"LWL"); type=LOADLR; break;
7175 case 0x23: strcpy(insn[i],"LW"); type=LOAD; break;
7176 case 0x24: strcpy(insn[i],"LBU"); type=LOAD; break;
7177 case 0x25: strcpy(insn[i],"LHU"); type=LOAD; break;
7178 case 0x26: strcpy(insn[i],"LWR"); type=LOADLR; break;
7180 case 0x27: strcpy(insn[i],"LWU"); type=LOAD; break;
7182 case 0x28: strcpy(insn[i],"SB"); type=STORE; break;
7183 case 0x29: strcpy(insn[i],"SH"); type=STORE; break;
7184 case 0x2A: strcpy(insn[i],"SWL"); type=STORELR; break;
7185 case 0x2B: strcpy(insn[i],"SW"); type=STORE; break;
7187 case 0x2C: strcpy(insn[i],"SDL"); type=STORELR; break;
7188 case 0x2D: strcpy(insn[i],"SDR"); type=STORELR; break;
7190 case 0x2E: strcpy(insn[i],"SWR"); type=STORELR; break;
7191 case 0x2F: strcpy(insn[i],"CACHE"); type=NOP; break;
7192 case 0x30: strcpy(insn[i],"LL"); type=NI; break;
7193 case 0x31: strcpy(insn[i],"LWC1"); type=C1LS; break;
7195 case 0x34: strcpy(insn[i],"LLD"); type=NI; break;
7196 case 0x35: strcpy(insn[i],"LDC1"); type=C1LS; break;
7197 case 0x37: strcpy(insn[i],"LD"); type=LOAD; break;
7199 case 0x38: strcpy(insn[i],"SC"); type=NI; break;
7200 case 0x39: strcpy(insn[i],"SWC1"); type=C1LS; break;
7202 case 0x3C: strcpy(insn[i],"SCD"); type=NI; break;
7203 case 0x3D: strcpy(insn[i],"SDC1"); type=C1LS; break;
7204 case 0x3F: strcpy(insn[i],"SD"); type=STORE; break;
7206 case 0x12: strcpy(insn[i],"COP2"); type=NI;
7207 op2=(source[i]>>21)&0x1f;
7209 if (source[i]&0x3f) { // use this hack to support old savestates with patched gte insns
7210 if (gte_handlers[source[i]&0x3f]!=NULL) {
7211 if (gte_regnames[source[i]&0x3f]!=NULL)
7212 strcpy(insn[i],gte_regnames[source[i]&0x3f]);
7214 snprintf(insn[i], sizeof(insn[i]), "COP2 %x", source[i]&0x3f);
7220 case 0x00: strcpy(insn[i],"MFC2"); type=COP2; break;
7221 case 0x02: strcpy(insn[i],"CFC2"); type=COP2; break;
7222 case 0x04: strcpy(insn[i],"MTC2"); type=COP2; break;
7223 case 0x06: strcpy(insn[i],"CTC2"); type=COP2; break;
7226 case 0x32: strcpy(insn[i],"LWC2"); type=C2LS; break;
7227 case 0x3A: strcpy(insn[i],"SWC2"); type=C2LS; break;
7228 case 0x3B: strcpy(insn[i],"HLECALL"); type=HLECALL; break;
7229 default: strcpy(insn[i],"???"); type=NI;
7230 SysPrintf("NI %08x @%08x (%08x)\n", source[i], addr + i*4, addr);
7234 dops[i].opcode2=op2;
7235 /* Get registers/immediates */
7237 gte_rs[i]=gte_rt[i]=0;
7240 dops[i].rs1=(source[i]>>21)&0x1f;
7242 dops[i].rt1=(source[i]>>16)&0x1f;
7244 imm[i]=(short)source[i];
7248 dops[i].rs1=(source[i]>>21)&0x1f;
7249 dops[i].rs2=(source[i]>>16)&0x1f;
7252 imm[i]=(short)source[i];
7255 // LWL/LWR only load part of the register,
7256 // therefore the target register must be treated as a source too
7257 dops[i].rs1=(source[i]>>21)&0x1f;
7258 dops[i].rs2=(source[i]>>16)&0x1f;
7259 dops[i].rt1=(source[i]>>16)&0x1f;
7261 imm[i]=(short)source[i];
7264 if (op==0x0f) dops[i].rs1=0; // LUI instruction has no source register
7265 else dops[i].rs1=(source[i]>>21)&0x1f;
7267 dops[i].rt1=(source[i]>>16)&0x1f;
7269 if(op>=0x0c&&op<=0x0e) { // ANDI/ORI/XORI
7270 imm[i]=(unsigned short)source[i];
7272 imm[i]=(short)source[i];
7280 // The JAL instruction writes to r31.
7287 dops[i].rs1=(source[i]>>21)&0x1f;
7291 // The JALR instruction writes to rd.
7293 dops[i].rt1=(source[i]>>11)&0x1f;
7298 dops[i].rs1=(source[i]>>21)&0x1f;
7299 dops[i].rs2=(source[i]>>16)&0x1f;
7302 if(op&2) { // BGTZ/BLEZ
7305 dops[i].likely=(op>>4)?1:0;
7308 dops[i].rs1=(source[i]>>21)&0x1f;
7312 if(op2&0x10) { // BxxAL
7314 // NOTE: If the branch is not taken, r31 is still overwritten
7316 dops[i].likely=(op2&2)?1:0;
7319 dops[i].rs1=(source[i]>>21)&0x1f; // source
7320 dops[i].rs2=(source[i]>>16)&0x1f; // subtract amount
7321 dops[i].rt1=(source[i]>>11)&0x1f; // destination
7325 dops[i].rs1=(source[i]>>21)&0x1f; // source
7326 dops[i].rs2=(source[i]>>16)&0x1f; // divisor
7335 if(op2==0x10) dops[i].rs1=HIREG; // MFHI
7336 if(op2==0x11) dops[i].rt1=HIREG; // MTHI
7337 if(op2==0x12) dops[i].rs1=LOREG; // MFLO
7338 if(op2==0x13) dops[i].rt1=LOREG; // MTLO
7339 if((op2&0x1d)==0x10) dops[i].rt1=(source[i]>>11)&0x1f; // MFxx
7340 if((op2&0x1d)==0x11) dops[i].rs1=(source[i]>>21)&0x1f; // MTxx
7343 dops[i].rs1=(source[i]>>16)&0x1f; // target of shift
7344 dops[i].rs2=(source[i]>>21)&0x1f; // shift amount
7345 dops[i].rt1=(source[i]>>11)&0x1f; // destination
7349 dops[i].rs1=(source[i]>>16)&0x1f;
7351 dops[i].rt1=(source[i]>>11)&0x1f;
7353 imm[i]=(source[i]>>6)&0x1f;
7354 // DSxx32 instructions
7355 if(op2>=0x3c) imm[i]|=0x20;
7362 if(op2==0||op2==2) dops[i].rt1=(source[i]>>16)&0x1F; // MFC0/CFC0
7363 if(op2==4||op2==6) dops[i].rs1=(source[i]>>16)&0x1F; // MTC0/CTC0
7364 if(op2==4&&((source[i]>>11)&0x1f)==12) dops[i].rt2=CSREG; // Status
7365 if(op2==16) if((source[i]&0x3f)==0x18) dops[i].rs2=CCREG; // ERET
7372 if(op2<3) dops[i].rt1=(source[i]>>16)&0x1F; // MFC1/DMFC1/CFC1
7373 if(op2>3) dops[i].rs1=(source[i]>>16)&0x1F; // MTC1/DMTC1/CTC1
7381 if(op2<3) dops[i].rt1=(source[i]>>16)&0x1F; // MFC2/CFC2
7382 if(op2>3) dops[i].rs1=(source[i]>>16)&0x1F; // MTC2/CTC2
7384 int gr=(source[i]>>11)&0x1F;
7387 case 0x00: gte_rs[i]=1ll<<gr; break; // MFC2
7388 case 0x04: gte_rt[i]=1ll<<gr; break; // MTC2
7389 case 0x02: gte_rs[i]=1ll<<(gr+32); break; // CFC2
7390 case 0x06: gte_rt[i]=1ll<<(gr+32); break; // CTC2
7394 dops[i].rs1=(source[i]>>21)&0x1F;
7398 imm[i]=(short)source[i];
7401 dops[i].rs1=(source[i]>>21)&0x1F;
7405 imm[i]=(short)source[i];
7406 if(op==0x32) gte_rt[i]=1ll<<((source[i]>>16)&0x1F); // LWC2
7407 else gte_rs[i]=1ll<<((source[i]>>16)&0x1F); // SWC2
7414 gte_rs[i]=gte_reg_reads[source[i]&0x3f];
7415 gte_rt[i]=gte_reg_writes[source[i]&0x3f];
7416 gte_rt[i]|=1ll<<63; // every op changes flags
7417 if((source[i]&0x3f)==GTE_MVMVA) {
7418 int v = (source[i] >> 15) & 3;
7419 gte_rs[i]&=~0xe3fll;
7420 if(v==3) gte_rs[i]|=0xe00ll;
7421 else gte_rs[i]|=3ll<<(v*2);
7438 /* Calculate branch target addresses */
7440 ba[i]=((start+i*4+4)&0xF0000000)|(((unsigned int)source[i]<<6)>>4);
7441 else if(type==CJUMP&&dops[i].rs1==dops[i].rs2&&(op&1))
7442 ba[i]=start+i*4+8; // Ignore never taken branch
7443 else if(type==SJUMP&&dops[i].rs1==0&&!(op2&1))
7444 ba[i]=start+i*4+8; // Ignore never taken branch
7445 else if(type==CJUMP||type==SJUMP)
7446 ba[i]=start+i*4+4+((signed int)((unsigned int)source[i]<<16)>>14);
7449 /* simplify always (not)taken branches */
7450 if (type == CJUMP && dops[i].rs1 == dops[i].rs2) {
7451 dops[i].rs1 = dops[i].rs2 = 0;
7453 dops[i].itype = type = UJUMP;
7454 dops[i].rs2 = CCREG;
7457 else if (type == SJUMP && dops[i].rs1 == 0 && (op2 & 1))
7458 dops[i].itype = type = UJUMP;
7460 /* messy cases to just pass over to the interpreter */
7461 if (i > 0 && is_jump(i-1)) {
7463 // branch in delay slot?
7465 // don't handle first branch and call interpreter if it's hit
7466 SysPrintf("branch in delay slot @%08x (%08x)\n", addr + i*4, addr);
7469 // basic load delay detection
7470 else if((type==LOAD||type==LOADLR||type==COP0||type==COP2||type==C2LS)&&dops[i].rt1!=0) {
7471 int t=(ba[i-1]-start)/4;
7472 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) {
7473 // jump target wants DS result - potential load delay effect
7474 SysPrintf("load delay @%08x (%08x)\n", addr + i*4, addr);
7476 dops[t+1].bt=1; // expected return from interpreter
7478 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&&
7479 !(i>=3&&is_jump(i-3))) {
7480 // v0 overwrite like this is a sign of trouble, bail out
7481 SysPrintf("v0 overwrite @%08x (%08x)\n", addr + i*4, addr);
7486 dops[i-1].rs1=CCREG;
7487 dops[i-1].rs2=dops[i-1].rt1=dops[i-1].rt2=0;
7489 dops[i-1].itype=INTCALL;
7491 i--; // don't compile the DS
7495 /* Is this the end of the block? */
7496 if (i > 0 && is_ujump(i-1)) {
7497 if(dops[i-1].rt1==0) { // Continue past subroutine call (JAL)
7501 if(stop_after_jal) done=1;
7503 if((source[i+1]&0xfc00003f)==0x0d) done=1;
7505 // Don't recompile stuff that's already compiled
7506 if(check_addr(start+i*4+4)) done=1;
7507 // Don't get too close to the limit
7508 if(i>MAXBLOCK/2) done=1;
7510 if(dops[i].itype==SYSCALL&&stop_after_jal) done=1;
7511 if(dops[i].itype==HLECALL||dops[i].itype==INTCALL) done=2;
7513 // Does the block continue due to a branch?
7516 if(ba[j]==start+i*4) done=j=0; // Branch into delay slot
7517 if(ba[j]==start+i*4+4) done=j=0;
7518 if(ba[j]==start+i*4+8) done=j=0;
7521 //assert(i<MAXBLOCK-1);
7522 if(start+i*4==pagelimit-4) done=1;
7523 assert(start+i*4<pagelimit);
7524 if (i==MAXBLOCK-1) done=1;
7525 // Stop if we're compiling junk
7526 if(dops[i].itype==NI&&dops[i].opcode==0x11) {
7527 done=stop_after_jal=1;
7528 SysPrintf("Disabled speculative precompilation\n");
7532 if(dops[i-1].itype==UJUMP||dops[i-1].itype==CJUMP||dops[i-1].itype==SJUMP||dops[i-1].itype==RJUMP) {
7533 if(start+i*4==pagelimit) {
7534 dops[i-1].itype=SPAN;
7539 /* Pass 2 - Register dependencies and branch targets */
7541 unneeded_registers(0,slen-1,0);
7543 /* Pass 3 - Register allocation */
7545 struct regstat current; // Current register allocations/status
7547 current.u=unneeded_reg[0];
7548 clear_all_regs(current.regmap);
7549 alloc_reg(¤t,0,CCREG);
7550 dirty_reg(¤t,CCREG);
7553 current.waswritten=0;
7559 // First instruction is delay slot
7564 current.regmap[HOST_BTREG]=BTREG;
7572 for(hr=0;hr<HOST_REGS;hr++)
7574 // Is this really necessary?
7575 if(current.regmap[hr]==0) current.regmap[hr]=-1;
7578 current.waswritten=0;
7581 memcpy(regmap_pre[i],current.regmap,sizeof(current.regmap));
7582 regs[i].wasconst=current.isconst;
7583 regs[i].wasdirty=current.dirty;
7584 regs[i].loadedconst=0;
7585 if(dops[i].itype!=UJUMP&&dops[i].itype!=CJUMP&&dops[i].itype!=SJUMP&&dops[i].itype!=RJUMP) {
7587 current.u=unneeded_reg[i+1]&~((1LL<<dops[i].rs1)|(1LL<<dops[i].rs2));
7594 current.u=branch_unneeded_reg[i]&~((1LL<<dops[i+1].rs1)|(1LL<<dops[i+1].rs2));
7595 current.u&=~((1LL<<dops[i].rs1)|(1LL<<dops[i].rs2));
7597 } else { SysPrintf("oops, branch at end of block with no delay slot\n");abort(); }
7601 ds=0; // Skip delay slot, already allocated as part of branch
7602 // ...but we need to alloc it in case something jumps here
7604 current.u=branch_unneeded_reg[i-1]&unneeded_reg[i+1];
7606 current.u=branch_unneeded_reg[i-1];
7608 current.u&=~((1LL<<dops[i].rs1)|(1LL<<dops[i].rs2));
7610 struct regstat temp;
7611 memcpy(&temp,¤t,sizeof(current));
7612 temp.wasdirty=temp.dirty;
7613 // TODO: Take into account unconditional branches, as below
7614 delayslot_alloc(&temp,i);
7615 memcpy(regs[i].regmap,temp.regmap,sizeof(temp.regmap));
7616 regs[i].wasdirty=temp.wasdirty;
7617 regs[i].dirty=temp.dirty;
7621 // Create entry (branch target) regmap
7622 for(hr=0;hr<HOST_REGS;hr++)
7624 int r=temp.regmap[hr];
7626 if(r!=regmap_pre[i][hr]) {
7627 regs[i].regmap_entry[hr]=-1;
7632 if((current.u>>r)&1) {
7633 regs[i].regmap_entry[hr]=-1;
7634 regs[i].regmap[hr]=-1;
7635 //Don't clear regs in the delay slot as the branch might need them
7636 //current.regmap[hr]=-1;
7638 regs[i].regmap_entry[hr]=r;
7641 // First instruction expects CCREG to be allocated
7642 if(i==0&&hr==HOST_CCREG)
7643 regs[i].regmap_entry[hr]=CCREG;
7645 regs[i].regmap_entry[hr]=-1;
7649 else { // Not delay slot
7650 switch(dops[i].itype) {
7652 //current.isconst=0; // DEBUG
7653 //current.wasconst=0; // DEBUG
7654 //regs[i].wasconst=0; // DEBUG
7655 clear_const(¤t,dops[i].rt1);
7656 alloc_cc(¤t,i);
7657 dirty_reg(¤t,CCREG);
7658 if (dops[i].rt1==31) {
7659 alloc_reg(¤t,i,31);
7660 dirty_reg(¤t,31);
7661 //assert(dops[i+1].rs1!=31&&dops[i+1].rs2!=31);
7662 //assert(dops[i+1].rt1!=dops[i].rt1);
7664 alloc_reg(¤t,i,PTEMP);
7668 delayslot_alloc(¤t,i+1);
7669 //current.isconst=0; // DEBUG
7671 //printf("i=%d, isconst=%x\n",i,current.isconst);
7674 //current.isconst=0;
7675 //current.wasconst=0;
7676 //regs[i].wasconst=0;
7677 clear_const(¤t,dops[i].rs1);
7678 clear_const(¤t,dops[i].rt1);
7679 alloc_cc(¤t,i);
7680 dirty_reg(¤t,CCREG);
7681 if (!ds_writes_rjump_rs(i)) {
7682 alloc_reg(¤t,i,dops[i].rs1);
7683 if (dops[i].rt1!=0) {
7684 alloc_reg(¤t,i,dops[i].rt1);
7685 dirty_reg(¤t,dops[i].rt1);
7686 assert(dops[i+1].rs1!=dops[i].rt1&&dops[i+1].rs2!=dops[i].rt1);
7687 assert(dops[i+1].rt1!=dops[i].rt1);
7689 alloc_reg(¤t,i,PTEMP);
7693 if(dops[i].rs1==31) { // JALR
7694 alloc_reg(¤t,i,RHASH);
7695 alloc_reg(¤t,i,RHTBL);
7698 delayslot_alloc(¤t,i+1);
7700 // The delay slot overwrites our source register,
7701 // allocate a temporary register to hold the old value.
7705 delayslot_alloc(¤t,i+1);
7707 alloc_reg(¤t,i,RTEMP);
7709 //current.isconst=0; // DEBUG
7714 //current.isconst=0;
7715 //current.wasconst=0;
7716 //regs[i].wasconst=0;
7717 clear_const(¤t,dops[i].rs1);
7718 clear_const(¤t,dops[i].rs2);
7719 if((dops[i].opcode&0x3E)==4) // BEQ/BNE
7721 alloc_cc(¤t,i);
7722 dirty_reg(¤t,CCREG);
7723 if(dops[i].rs1) alloc_reg(¤t,i,dops[i].rs1);
7724 if(dops[i].rs2) alloc_reg(¤t,i,dops[i].rs2);
7725 if((dops[i].rs1&&(dops[i].rs1==dops[i+1].rt1||dops[i].rs1==dops[i+1].rt2))||
7726 (dops[i].rs2&&(dops[i].rs2==dops[i+1].rt1||dops[i].rs2==dops[i+1].rt2))) {
7727 // The delay slot overwrites one of our conditions.
7728 // Allocate the branch condition registers instead.
7732 if(dops[i].rs1) alloc_reg(¤t,i,dops[i].rs1);
7733 if(dops[i].rs2) alloc_reg(¤t,i,dops[i].rs2);
7738 delayslot_alloc(¤t,i+1);
7742 if((dops[i].opcode&0x3E)==6) // BLEZ/BGTZ
7744 alloc_cc(¤t,i);
7745 dirty_reg(¤t,CCREG);
7746 alloc_reg(¤t,i,dops[i].rs1);
7747 if(dops[i].rs1&&(dops[i].rs1==dops[i+1].rt1||dops[i].rs1==dops[i+1].rt2)) {
7748 // The delay slot overwrites one of our conditions.
7749 // Allocate the branch condition registers instead.
7753 if(dops[i].rs1) alloc_reg(¤t,i,dops[i].rs1);
7758 delayslot_alloc(¤t,i+1);
7762 // Don't alloc the delay slot yet because we might not execute it
7763 if((dops[i].opcode&0x3E)==0x14) // BEQL/BNEL
7768 alloc_cc(¤t,i);
7769 dirty_reg(¤t,CCREG);
7770 alloc_reg(¤t,i,dops[i].rs1);
7771 alloc_reg(¤t,i,dops[i].rs2);
7774 if((dops[i].opcode&0x3E)==0x16) // BLEZL/BGTZL
7779 alloc_cc(¤t,i);
7780 dirty_reg(¤t,CCREG);
7781 alloc_reg(¤t,i,dops[i].rs1);
7784 //current.isconst=0;
7787 //current.isconst=0;
7788 //current.wasconst=0;
7789 //regs[i].wasconst=0;
7790 clear_const(¤t,dops[i].rs1);
7791 clear_const(¤t,dops[i].rt1);
7792 //if((dops[i].opcode2&0x1E)==0x0) // BLTZ/BGEZ
7793 if((dops[i].opcode2&0x0E)==0x0) // BLTZ/BGEZ
7795 alloc_cc(¤t,i);
7796 dirty_reg(¤t,CCREG);
7797 alloc_reg(¤t,i,dops[i].rs1);
7798 if (dops[i].rt1==31) { // BLTZAL/BGEZAL
7799 alloc_reg(¤t,i,31);
7800 dirty_reg(¤t,31);
7801 //#ifdef REG_PREFETCH
7802 //alloc_reg(¤t,i,PTEMP);
7805 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.
7806 ||(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
7807 // Allocate the branch condition registers instead.
7811 if(dops[i].rs1) alloc_reg(¤t,i,dops[i].rs1);
7816 delayslot_alloc(¤t,i+1);
7820 // Don't alloc the delay slot yet because we might not execute it
7821 if((dops[i].opcode2&0x1E)==0x2) // BLTZL/BGEZL
7826 alloc_cc(¤t,i);
7827 dirty_reg(¤t,CCREG);
7828 alloc_reg(¤t,i,dops[i].rs1);
7831 //current.isconst=0;
7834 imm16_alloc(¤t,i);
7838 load_alloc(¤t,i);
7842 store_alloc(¤t,i);
7845 alu_alloc(¤t,i);
7848 shift_alloc(¤t,i);
7851 multdiv_alloc(¤t,i);
7854 shiftimm_alloc(¤t,i);
7857 mov_alloc(¤t,i);
7860 cop0_alloc(¤t,i);
7865 cop2_alloc(¤t,i);
7868 c1ls_alloc(¤t,i);
7871 c2ls_alloc(¤t,i);
7874 c2op_alloc(¤t,i);
7879 syscall_alloc(¤t,i);
7882 pagespan_alloc(¤t,i);
7886 // Create entry (branch target) regmap
7887 for(hr=0;hr<HOST_REGS;hr++)
7890 r=current.regmap[hr];
7892 if(r!=regmap_pre[i][hr]) {
7893 // TODO: delay slot (?)
7894 or=get_reg(regmap_pre[i],r); // Get old mapping for this register
7895 if(or<0||(r&63)>=TEMPREG){
7896 regs[i].regmap_entry[hr]=-1;
7900 // Just move it to a different register
7901 regs[i].regmap_entry[hr]=r;
7902 // If it was dirty before, it's still dirty
7903 if((regs[i].wasdirty>>or)&1) dirty_reg(¤t,r&63);
7910 regs[i].regmap_entry[hr]=0;
7915 if((current.u>>r)&1) {
7916 regs[i].regmap_entry[hr]=-1;
7917 //regs[i].regmap[hr]=-1;
7918 current.regmap[hr]=-1;
7920 regs[i].regmap_entry[hr]=r;
7924 // Branches expect CCREG to be allocated at the target
7925 if(regmap_pre[i][hr]==CCREG)
7926 regs[i].regmap_entry[hr]=CCREG;
7928 regs[i].regmap_entry[hr]=-1;
7931 memcpy(regs[i].regmap,current.regmap,sizeof(current.regmap));
7934 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)
7935 current.waswritten|=1<<dops[i-1].rs1;
7936 current.waswritten&=~(1<<dops[i].rt1);
7937 current.waswritten&=~(1<<dops[i].rt2);
7938 if((dops[i].itype==STORE||dops[i].itype==STORELR||(dops[i].itype==C2LS&&dops[i].opcode==0x3a))&&(u_int)imm[i]>=0x800)
7939 current.waswritten&=~(1<<dops[i].rs1);
7941 /* Branch post-alloc */
7944 current.wasdirty=current.dirty;
7945 switch(dops[i-1].itype) {
7947 memcpy(&branch_regs[i-1],¤t,sizeof(current));
7948 branch_regs[i-1].isconst=0;
7949 branch_regs[i-1].wasconst=0;
7950 branch_regs[i-1].u=branch_unneeded_reg[i-1]&~((1LL<<dops[i-1].rs1)|(1LL<<dops[i-1].rs2));
7951 alloc_cc(&branch_regs[i-1],i-1);
7952 dirty_reg(&branch_regs[i-1],CCREG);
7953 if(dops[i-1].rt1==31) { // JAL
7954 alloc_reg(&branch_regs[i-1],i-1,31);
7955 dirty_reg(&branch_regs[i-1],31);
7957 memcpy(&branch_regs[i-1].regmap_entry,&branch_regs[i-1].regmap,sizeof(current.regmap));
7958 memcpy(constmap[i],constmap[i-1],sizeof(constmap[i]));
7961 memcpy(&branch_regs[i-1],¤t,sizeof(current));
7962 branch_regs[i-1].isconst=0;
7963 branch_regs[i-1].wasconst=0;
7964 branch_regs[i-1].u=branch_unneeded_reg[i-1]&~((1LL<<dops[i-1].rs1)|(1LL<<dops[i-1].rs2));
7965 alloc_cc(&branch_regs[i-1],i-1);
7966 dirty_reg(&branch_regs[i-1],CCREG);
7967 alloc_reg(&branch_regs[i-1],i-1,dops[i-1].rs1);
7968 if(dops[i-1].rt1!=0) { // JALR
7969 alloc_reg(&branch_regs[i-1],i-1,dops[i-1].rt1);
7970 dirty_reg(&branch_regs[i-1],dops[i-1].rt1);
7973 if(dops[i-1].rs1==31) { // JALR
7974 alloc_reg(&branch_regs[i-1],i-1,RHASH);
7975 alloc_reg(&branch_regs[i-1],i-1,RHTBL);
7978 memcpy(&branch_regs[i-1].regmap_entry,&branch_regs[i-1].regmap,sizeof(current.regmap));
7979 memcpy(constmap[i],constmap[i-1],sizeof(constmap[i]));
7982 if((dops[i-1].opcode&0x3E)==4) // BEQ/BNE
7984 alloc_cc(¤t,i-1);
7985 dirty_reg(¤t,CCREG);
7986 if((dops[i-1].rs1&&(dops[i-1].rs1==dops[i].rt1||dops[i-1].rs1==dops[i].rt2))||
7987 (dops[i-1].rs2&&(dops[i-1].rs2==dops[i].rt1||dops[i-1].rs2==dops[i].rt2))) {
7988 // The delay slot overwrote one of our conditions
7989 // Delay slot goes after the test (in order)
7990 current.u=branch_unneeded_reg[i-1]&~((1LL<<dops[i].rs1)|(1LL<<dops[i].rs2));
7992 delayslot_alloc(¤t,i);
7997 current.u=branch_unneeded_reg[i-1]&~((1LL<<dops[i-1].rs1)|(1LL<<dops[i-1].rs2));
7998 // Alloc the branch condition registers
7999 if(dops[i-1].rs1) alloc_reg(¤t,i-1,dops[i-1].rs1);
8000 if(dops[i-1].rs2) alloc_reg(¤t,i-1,dops[i-1].rs2);
8002 memcpy(&branch_regs[i-1],¤t,sizeof(current));
8003 branch_regs[i-1].isconst=0;
8004 branch_regs[i-1].wasconst=0;
8005 memcpy(&branch_regs[i-1].regmap_entry,¤t.regmap,sizeof(current.regmap));
8006 memcpy(constmap[i],constmap[i-1],sizeof(constmap[i]));
8009 if((dops[i-1].opcode&0x3E)==6) // BLEZ/BGTZ
8011 alloc_cc(¤t,i-1);
8012 dirty_reg(¤t,CCREG);
8013 if(dops[i-1].rs1==dops[i].rt1||dops[i-1].rs1==dops[i].rt2) {
8014 // The delay slot overwrote the branch condition
8015 // Delay slot goes after the test (in order)
8016 current.u=branch_unneeded_reg[i-1]&~((1LL<<dops[i].rs1)|(1LL<<dops[i].rs2));
8018 delayslot_alloc(¤t,i);
8023 current.u=branch_unneeded_reg[i-1]&~(1LL<<dops[i-1].rs1);
8024 // Alloc the branch condition register
8025 alloc_reg(¤t,i-1,dops[i-1].rs1);
8027 memcpy(&branch_regs[i-1],¤t,sizeof(current));
8028 branch_regs[i-1].isconst=0;
8029 branch_regs[i-1].wasconst=0;
8030 memcpy(&branch_regs[i-1].regmap_entry,¤t.regmap,sizeof(current.regmap));
8031 memcpy(constmap[i],constmap[i-1],sizeof(constmap[i]));
8034 // Alloc the delay slot in case the branch is taken
8035 if((dops[i-1].opcode&0x3E)==0x14) // BEQL/BNEL
8037 memcpy(&branch_regs[i-1],¤t,sizeof(current));
8038 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;
8039 alloc_cc(&branch_regs[i-1],i);
8040 dirty_reg(&branch_regs[i-1],CCREG);
8041 delayslot_alloc(&branch_regs[i-1],i);
8042 branch_regs[i-1].isconst=0;
8043 alloc_reg(¤t,i,CCREG); // Not taken path
8044 dirty_reg(¤t,CCREG);
8045 memcpy(&branch_regs[i-1].regmap_entry,&branch_regs[i-1].regmap,sizeof(current.regmap));
8048 if((dops[i-1].opcode&0x3E)==0x16) // BLEZL/BGTZL
8050 memcpy(&branch_regs[i-1],¤t,sizeof(current));
8051 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;
8052 alloc_cc(&branch_regs[i-1],i);
8053 dirty_reg(&branch_regs[i-1],CCREG);
8054 delayslot_alloc(&branch_regs[i-1],i);
8055 branch_regs[i-1].isconst=0;
8056 alloc_reg(¤t,i,CCREG); // Not taken path
8057 dirty_reg(¤t,CCREG);
8058 memcpy(&branch_regs[i-1].regmap_entry,&branch_regs[i-1].regmap,sizeof(current.regmap));
8062 //if((dops[i-1].opcode2&0x1E)==0) // BLTZ/BGEZ
8063 if((dops[i-1].opcode2&0x0E)==0) // BLTZ/BGEZ
8065 alloc_cc(¤t,i-1);
8066 dirty_reg(¤t,CCREG);
8067 if(dops[i-1].rs1==dops[i].rt1||dops[i-1].rs1==dops[i].rt2) {
8068 // The delay slot overwrote the branch condition
8069 // Delay slot goes after the test (in order)
8070 current.u=branch_unneeded_reg[i-1]&~((1LL<<dops[i].rs1)|(1LL<<dops[i].rs2));
8072 delayslot_alloc(¤t,i);
8077 current.u=branch_unneeded_reg[i-1]&~(1LL<<dops[i-1].rs1);
8078 // Alloc the branch condition register
8079 alloc_reg(¤t,i-1,dops[i-1].rs1);
8081 memcpy(&branch_regs[i-1],¤t,sizeof(current));
8082 branch_regs[i-1].isconst=0;
8083 branch_regs[i-1].wasconst=0;
8084 memcpy(&branch_regs[i-1].regmap_entry,¤t.regmap,sizeof(current.regmap));
8085 memcpy(constmap[i],constmap[i-1],sizeof(constmap[i]));
8088 // Alloc the delay slot in case the branch is taken
8089 if((dops[i-1].opcode2&0x1E)==2) // BLTZL/BGEZL
8091 memcpy(&branch_regs[i-1],¤t,sizeof(current));
8092 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;
8093 alloc_cc(&branch_regs[i-1],i);
8094 dirty_reg(&branch_regs[i-1],CCREG);
8095 delayslot_alloc(&branch_regs[i-1],i);
8096 branch_regs[i-1].isconst=0;
8097 alloc_reg(¤t,i,CCREG); // Not taken path
8098 dirty_reg(¤t,CCREG);
8099 memcpy(&branch_regs[i-1].regmap_entry,&branch_regs[i-1].regmap,sizeof(current.regmap));
8101 // FIXME: BLTZAL/BGEZAL
8102 if(dops[i-1].opcode2&0x10) { // BxxZAL
8103 alloc_reg(&branch_regs[i-1],i-1,31);
8104 dirty_reg(&branch_regs[i-1],31);
8111 if(dops[i-1].rt1==31) // JAL/JALR
8113 // Subroutine call will return here, don't alloc any registers
8115 clear_all_regs(current.regmap);
8116 alloc_reg(¤t,i,CCREG);
8117 dirty_reg(¤t,CCREG);
8121 // Internal branch will jump here, match registers to caller
8123 clear_all_regs(current.regmap);
8124 alloc_reg(¤t,i,CCREG);
8125 dirty_reg(¤t,CCREG);
8128 if(ba[j]==start+i*4+4) {
8129 memcpy(current.regmap,branch_regs[j].regmap,sizeof(current.regmap));
8130 current.dirty=branch_regs[j].dirty;
8135 if(ba[j]==start+i*4+4) {
8136 for(hr=0;hr<HOST_REGS;hr++) {
8137 if(current.regmap[hr]!=branch_regs[j].regmap[hr]) {
8138 current.regmap[hr]=-1;
8140 current.dirty&=branch_regs[j].dirty;
8149 // Count cycles in between branches
8151 if(i>0&&(dops[i-1].itype==RJUMP||dops[i-1].itype==UJUMP||dops[i-1].itype==CJUMP||dops[i-1].itype==SJUMP||dops[i].itype==SYSCALL||dops[i].itype==HLECALL))
8155 #if !defined(DRC_DBG)
8156 else if(dops[i].itype==C2OP&>e_cycletab[source[i]&0x3f]>2)
8158 // this should really be removed since the real stalls have been implemented,
8159 // but doing so causes sizeable perf regression against the older version
8160 u_int gtec = gte_cycletab[source[i] & 0x3f];
8161 cc += HACK_ENABLED(NDHACK_NO_STALLS) ? gtec/2 : 2;
8163 else if(i>1&&dops[i].itype==STORE&&dops[i-1].itype==STORE&&dops[i-2].itype==STORE&&!dops[i].bt)
8167 else if(dops[i].itype==C2LS)
8169 // same as with C2OP
8170 cc += HACK_ENABLED(NDHACK_NO_STALLS) ? 4 : 2;
8178 if(!dops[i].is_ds) {
8179 regs[i].dirty=current.dirty;
8180 regs[i].isconst=current.isconst;
8181 memcpy(constmap[i],current_constmap,sizeof(constmap[i]));
8183 for(hr=0;hr<HOST_REGS;hr++) {
8184 if(hr!=EXCLUDE_REG&®s[i].regmap[hr]>=0) {
8185 if(regmap_pre[i][hr]!=regs[i].regmap[hr]) {
8186 regs[i].wasconst&=~(1<<hr);
8190 if(current.regmap[HOST_BTREG]==BTREG) current.regmap[HOST_BTREG]=-1;
8191 regs[i].waswritten=current.waswritten;
8194 /* Pass 4 - Cull unused host registers */
8198 for (i=slen-1;i>=0;i--)
8201 if(dops[i].itype==RJUMP||dops[i].itype==UJUMP||dops[i].itype==CJUMP||dops[i].itype==SJUMP)
8203 if(ba[i]<start || ba[i]>=(start+slen*4))
8205 // Branch out of this block, don't need anything
8211 // Need whatever matches the target
8213 int t=(ba[i]-start)>>2;
8214 for(hr=0;hr<HOST_REGS;hr++)
8216 if(regs[i].regmap_entry[hr]>=0) {
8217 if(regs[i].regmap_entry[hr]==regs[t].regmap_entry[hr]) nr|=1<<hr;
8221 // Conditional branch may need registers for following instructions
8225 nr|=needed_reg[i+2];
8226 for(hr=0;hr<HOST_REGS;hr++)
8228 if(regmap_pre[i+2][hr]>=0&&get_reg(regs[i+2].regmap_entry,regmap_pre[i+2][hr])<0) nr&=~(1<<hr);
8229 //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]);
8233 // Don't need stuff which is overwritten
8234 //if(regs[i].regmap[hr]!=regmap_pre[i][hr]) nr&=~(1<<hr);
8235 //if(regs[i].regmap[hr]<0) nr&=~(1<<hr);
8236 // Merge in delay slot
8237 for(hr=0;hr<HOST_REGS;hr++)
8239 if(!dops[i].likely) {
8240 // These are overwritten unless the branch is "likely"
8241 // and the delay slot is nullified if not taken
8242 if(dops[i+1].rt1&&dops[i+1].rt1==(regs[i].regmap[hr]&63)) nr&=~(1<<hr);
8243 if(dops[i+1].rt2&&dops[i+1].rt2==(regs[i].regmap[hr]&63)) nr&=~(1<<hr);
8245 if(dops[i+1].rs1==regmap_pre[i][hr]) nr|=1<<hr;
8246 if(dops[i+1].rs2==regmap_pre[i][hr]) nr|=1<<hr;
8247 if(dops[i+1].rs1==regs[i].regmap_entry[hr]) nr|=1<<hr;
8248 if(dops[i+1].rs2==regs[i].regmap_entry[hr]) nr|=1<<hr;
8249 if(dops[i+1].itype==STORE || dops[i+1].itype==STORELR || (dops[i+1].opcode&0x3b)==0x39 || (dops[i+1].opcode&0x3b)==0x3a) {
8250 if(regmap_pre[i][hr]==INVCP) nr|=1<<hr;
8251 if(regs[i].regmap_entry[hr]==INVCP) nr|=1<<hr;
8255 else if(dops[i].itype==SYSCALL||dops[i].itype==HLECALL||dops[i].itype==INTCALL)
8257 // SYSCALL instruction (software interrupt)
8260 else if(dops[i].itype==COP0 && (source[i]&0x3f)==0x18)
8262 // ERET instruction (return from interrupt)
8268 for(hr=0;hr<HOST_REGS;hr++) {
8269 if(regmap_pre[i+1][hr]>=0&&get_reg(regs[i+1].regmap_entry,regmap_pre[i+1][hr])<0) nr&=~(1<<hr);
8270 if(regs[i].regmap[hr]!=regmap_pre[i+1][hr]) nr&=~(1<<hr);
8271 if(regs[i].regmap[hr]!=regmap_pre[i][hr]) nr&=~(1<<hr);
8272 if(regs[i].regmap[hr]<0) nr&=~(1<<hr);
8276 for(hr=0;hr<HOST_REGS;hr++)
8278 // Overwritten registers are not needed
8279 if(dops[i].rt1&&dops[i].rt1==(regs[i].regmap[hr]&63)) nr&=~(1<<hr);
8280 if(dops[i].rt2&&dops[i].rt2==(regs[i].regmap[hr]&63)) nr&=~(1<<hr);
8281 if(FTEMP==(regs[i].regmap[hr]&63)) nr&=~(1<<hr);
8282 // Source registers are needed
8283 if(dops[i].rs1==regmap_pre[i][hr]) nr|=1<<hr;
8284 if(dops[i].rs2==regmap_pre[i][hr]) nr|=1<<hr;
8285 if(dops[i].rs1==regs[i].regmap_entry[hr]) nr|=1<<hr;
8286 if(dops[i].rs2==regs[i].regmap_entry[hr]) nr|=1<<hr;
8287 if(dops[i].itype==STORE || dops[i].itype==STORELR || (dops[i].opcode&0x3b)==0x39 || (dops[i].opcode&0x3b)==0x3a) {
8288 if(regmap_pre[i][hr]==INVCP) nr|=1<<hr;
8289 if(regs[i].regmap_entry[hr]==INVCP) nr|=1<<hr;
8291 // Don't store a register immediately after writing it,
8292 // may prevent dual-issue.
8293 // But do so if this is a branch target, otherwise we
8294 // might have to load the register before the branch.
8295 if(i>0&&!dops[i].bt&&((regs[i].wasdirty>>hr)&1)) {
8296 if((regmap_pre[i][hr]>0&&!((unneeded_reg[i]>>regmap_pre[i][hr])&1))) {
8297 if(dops[i-1].rt1==(regmap_pre[i][hr]&63)) nr|=1<<hr;
8298 if(dops[i-1].rt2==(regmap_pre[i][hr]&63)) nr|=1<<hr;
8300 if((regs[i].regmap_entry[hr]>0&&!((unneeded_reg[i]>>regs[i].regmap_entry[hr])&1))) {
8301 if(dops[i-1].rt1==(regs[i].regmap_entry[hr]&63)) nr|=1<<hr;
8302 if(dops[i-1].rt2==(regs[i].regmap_entry[hr]&63)) nr|=1<<hr;
8306 // Cycle count is needed at branches. Assume it is needed at the target too.
8307 if(i==0||dops[i].bt||dops[i].itype==CJUMP||dops[i].itype==SPAN) {
8308 if(regmap_pre[i][HOST_CCREG]==CCREG) nr|=1<<HOST_CCREG;
8309 if(regs[i].regmap_entry[HOST_CCREG]==CCREG) nr|=1<<HOST_CCREG;
8314 // Deallocate unneeded registers
8315 for(hr=0;hr<HOST_REGS;hr++)
8318 if(regs[i].regmap_entry[hr]!=CCREG) regs[i].regmap_entry[hr]=-1;
8319 if((regs[i].regmap[hr]&63)!=dops[i].rs1 && (regs[i].regmap[hr]&63)!=dops[i].rs2 &&
8320 (regs[i].regmap[hr]&63)!=dops[i].rt1 && (regs[i].regmap[hr]&63)!=dops[i].rt2 &&
8321 (regs[i].regmap[hr]&63)!=PTEMP && (regs[i].regmap[hr]&63)!=CCREG)
8325 if(dops[i].likely) {
8326 regs[i].regmap[hr]=-1;
8327 regs[i].isconst&=~(1<<hr);
8329 regmap_pre[i+2][hr]=-1;
8330 regs[i+2].wasconst&=~(1<<hr);
8335 if(dops[i].itype==RJUMP||dops[i].itype==UJUMP||dops[i].itype==CJUMP||dops[i].itype==SJUMP)
8338 if(dops[i+1].itype==STORE || dops[i+1].itype==STORELR ||
8339 (dops[i+1].opcode&0x3b)==0x39 || (dops[i+1].opcode&0x3b)==0x3a) { // SWC1/SDC1 || SWC2/SDC2
8342 if(dops[i+1].itype==LOADLR || dops[i+1].itype==STORELR ||
8343 dops[i+1].itype==C1LS || dops[i+1].itype==C2LS)
8345 if((regs[i].regmap[hr]&63)!=dops[i].rs1 && (regs[i].regmap[hr]&63)!=dops[i].rs2 &&
8346 (regs[i].regmap[hr]&63)!=dops[i].rt1 && (regs[i].regmap[hr]&63)!=dops[i].rt2 &&
8347 (regs[i].regmap[hr]&63)!=dops[i+1].rt1 && (regs[i].regmap[hr]&63)!=dops[i+1].rt2 &&
8348 regs[i].regmap[hr]!=dops[i+1].rs1 && regs[i].regmap[hr]!=dops[i+1].rs2 &&
8349 (regs[i].regmap[hr]&63)!=temp && regs[i].regmap[hr]!=PTEMP &&
8350 regs[i].regmap[hr]!=RHASH && regs[i].regmap[hr]!=RHTBL &&
8351 regs[i].regmap[hr]!=RTEMP && regs[i].regmap[hr]!=CCREG &&
8352 regs[i].regmap[hr]!=map )
8354 regs[i].regmap[hr]=-1;
8355 regs[i].isconst&=~(1<<hr);
8356 if((branch_regs[i].regmap[hr]&63)!=dops[i].rs1 && (branch_regs[i].regmap[hr]&63)!=dops[i].rs2 &&
8357 (branch_regs[i].regmap[hr]&63)!=dops[i].rt1 && (branch_regs[i].regmap[hr]&63)!=dops[i].rt2 &&
8358 (branch_regs[i].regmap[hr]&63)!=dops[i+1].rt1 && (branch_regs[i].regmap[hr]&63)!=dops[i+1].rt2 &&
8359 branch_regs[i].regmap[hr]!=dops[i+1].rs1 && branch_regs[i].regmap[hr]!=dops[i+1].rs2 &&
8360 (branch_regs[i].regmap[hr]&63)!=temp && branch_regs[i].regmap[hr]!=PTEMP &&
8361 branch_regs[i].regmap[hr]!=RHASH && branch_regs[i].regmap[hr]!=RHTBL &&
8362 branch_regs[i].regmap[hr]!=RTEMP && branch_regs[i].regmap[hr]!=CCREG &&
8363 branch_regs[i].regmap[hr]!=map)
8365 branch_regs[i].regmap[hr]=-1;
8366 branch_regs[i].regmap_entry[hr]=-1;
8369 if(!dops[i].likely&&i<slen-2) {
8370 regmap_pre[i+2][hr]=-1;
8371 regs[i+2].wasconst&=~(1<<hr);
8383 if(dops[i].itype==STORE || dops[i].itype==STORELR ||
8384 (dops[i].opcode&0x3b)==0x39 || (dops[i].opcode&0x3b)==0x3a) { // SWC1/SDC1 || SWC2/SDC2
8387 if(dops[i].itype==LOADLR || dops[i].itype==STORELR ||
8388 dops[i].itype==C1LS || dops[i].itype==C2LS)
8390 if((regs[i].regmap[hr]&63)!=dops[i].rt1 && (regs[i].regmap[hr]&63)!=dops[i].rt2 &&
8391 regs[i].regmap[hr]!=dops[i].rs1 && regs[i].regmap[hr]!=dops[i].rs2 &&
8392 (regs[i].regmap[hr]&63)!=temp && regs[i].regmap[hr]!=map &&
8393 (dops[i].itype!=SPAN||regs[i].regmap[hr]!=CCREG))
8395 if(i<slen-1&&!dops[i].is_ds) {
8396 assert(regs[i].regmap[hr]<64);
8397 if(regmap_pre[i+1][hr]!=-1 || regs[i].regmap[hr]>0)
8398 if(regmap_pre[i+1][hr]!=regs[i].regmap[hr])
8400 SysPrintf("fail: %x (%d %d!=%d)\n",start+i*4,hr,regmap_pre[i+1][hr],regs[i].regmap[hr]);
8401 assert(regmap_pre[i+1][hr]==regs[i].regmap[hr]);
8403 regmap_pre[i+1][hr]=-1;
8404 if(regs[i+1].regmap_entry[hr]==CCREG) regs[i+1].regmap_entry[hr]=-1;
8405 regs[i+1].wasconst&=~(1<<hr);
8407 regs[i].regmap[hr]=-1;
8408 regs[i].isconst&=~(1<<hr);
8416 /* Pass 5 - Pre-allocate registers */
8418 // If a register is allocated during a loop, try to allocate it for the
8419 // entire loop, if possible. This avoids loading/storing registers
8420 // inside of the loop.
8422 signed char f_regmap[HOST_REGS];
8423 clear_all_regs(f_regmap);
8424 for(i=0;i<slen-1;i++)
8426 if(dops[i].itype==UJUMP||dops[i].itype==CJUMP||dops[i].itype==SJUMP)
8428 if(ba[i]>=start && ba[i]<(start+i*4))
8429 if(dops[i+1].itype==NOP||dops[i+1].itype==MOV||dops[i+1].itype==ALU
8430 ||dops[i+1].itype==SHIFTIMM||dops[i+1].itype==IMM16||dops[i+1].itype==LOAD
8431 ||dops[i+1].itype==STORE||dops[i+1].itype==STORELR||dops[i+1].itype==C1LS
8432 ||dops[i+1].itype==SHIFT||dops[i+1].itype==COP1
8433 ||dops[i+1].itype==COP2||dops[i+1].itype==C2LS||dops[i+1].itype==C2OP)
8435 int t=(ba[i]-start)>>2;
8436 if(t>0&&(dops[t-1].itype!=UJUMP&&dops[t-1].itype!=RJUMP&&dops[t-1].itype!=CJUMP&&dops[t-1].itype!=SJUMP)) // loop_preload can't handle jumps into delay slots
8437 if(t<2||(dops[t-2].itype!=UJUMP&&dops[t-2].itype!=RJUMP)||dops[t-2].rt1!=31) // call/ret assumes no registers allocated
8438 for(hr=0;hr<HOST_REGS;hr++)
8440 if(regs[i].regmap[hr]>=0) {
8441 if(f_regmap[hr]!=regs[i].regmap[hr]) {
8442 // dealloc old register
8444 for(n=0;n<HOST_REGS;n++)
8446 if(f_regmap[n]==regs[i].regmap[hr]) {f_regmap[n]=-1;}
8448 // and alloc new one
8449 f_regmap[hr]=regs[i].regmap[hr];
8452 if(branch_regs[i].regmap[hr]>=0) {
8453 if(f_regmap[hr]!=branch_regs[i].regmap[hr]) {
8454 // dealloc old register
8456 for(n=0;n<HOST_REGS;n++)
8458 if(f_regmap[n]==branch_regs[i].regmap[hr]) {f_regmap[n]=-1;}
8460 // and alloc new one
8461 f_regmap[hr]=branch_regs[i].regmap[hr];
8465 if(count_free_regs(regs[i].regmap)<=minimum_free_regs[i+1])
8466 f_regmap[hr]=branch_regs[i].regmap[hr];
8468 if(count_free_regs(branch_regs[i].regmap)<=minimum_free_regs[i+1])
8469 f_regmap[hr]=branch_regs[i].regmap[hr];
8471 // Avoid dirty->clean transition
8472 #ifdef DESTRUCTIVE_WRITEBACK
8473 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;
8475 // This check is only strictly required in the DESTRUCTIVE_WRITEBACK
8476 // case above, however it's always a good idea. We can't hoist the
8477 // load if the register was already allocated, so there's no point
8478 // wasting time analyzing most of these cases. It only "succeeds"
8479 // when the mapping was different and the load can be replaced with
8480 // a mov, which is of negligible benefit. So such cases are
8482 if(f_regmap[hr]>0) {
8483 if(regs[t].regmap[hr]==f_regmap[hr]||(regs[t].regmap_entry[hr]<0&&get_reg(regmap_pre[t],f_regmap[hr])<0)) {
8487 //printf("Test %x -> %x, %x %d/%d\n",start+i*4,ba[i],start+j*4,hr,r);
8488 if(r<34&&((unneeded_reg[j]>>r)&1)) break;
8490 if(regs[j].regmap[hr]==f_regmap[hr]&&(f_regmap[hr]&63)<TEMPREG) {
8491 //printf("Hit %x -> %x, %x %d/%d\n",start+i*4,ba[i],start+j*4,hr,r);
8493 if(regs[i].regmap[hr]==-1&&branch_regs[i].regmap[hr]==-1) {
8494 if(get_reg(regs[i+2].regmap,f_regmap[hr])>=0) break;
8496 if(get_reg(regs[i].regmap,r&63)<0) break;
8497 if(get_reg(branch_regs[i].regmap,r&63)<0) break;
8500 while(k>1&®s[k-1].regmap[hr]==-1) {
8501 if(count_free_regs(regs[k-1].regmap)<=minimum_free_regs[k-1]) {
8502 //printf("no free regs for store %x\n",start+(k-1)*4);
8505 if(get_reg(regs[k-1].regmap,f_regmap[hr])>=0) {
8506 //printf("no-match due to different register\n");
8509 if(dops[k-2].itype==UJUMP||dops[k-2].itype==RJUMP||dops[k-2].itype==CJUMP||dops[k-2].itype==SJUMP) {
8510 //printf("no-match due to branch\n");
8513 // call/ret fast path assumes no registers allocated
8514 if(k>2&&(dops[k-3].itype==UJUMP||dops[k-3].itype==RJUMP)&&dops[k-3].rt1==31) {
8520 if(regs[k-1].regmap[hr]==f_regmap[hr]&®map_pre[k][hr]==f_regmap[hr]) {
8521 //printf("Extend r%d, %x ->\n",hr,start+k*4);
8523 regs[k].regmap_entry[hr]=f_regmap[hr];
8524 regs[k].regmap[hr]=f_regmap[hr];
8525 regmap_pre[k+1][hr]=f_regmap[hr];
8526 regs[k].wasdirty&=~(1<<hr);
8527 regs[k].dirty&=~(1<<hr);
8528 regs[k].wasdirty|=(1<<hr)®s[k-1].dirty;
8529 regs[k].dirty|=(1<<hr)®s[k].wasdirty;
8530 regs[k].wasconst&=~(1<<hr);
8531 regs[k].isconst&=~(1<<hr);
8536 //printf("Fail Extend r%d, %x ->\n",hr,start+k*4);
8539 assert(regs[i-1].regmap[hr]==f_regmap[hr]);
8540 if(regs[i-1].regmap[hr]==f_regmap[hr]&®map_pre[i][hr]==f_regmap[hr]) {
8541 //printf("OK fill %x (r%d)\n",start+i*4,hr);
8542 regs[i].regmap_entry[hr]=f_regmap[hr];
8543 regs[i].regmap[hr]=f_regmap[hr];
8544 regs[i].wasdirty&=~(1<<hr);
8545 regs[i].dirty&=~(1<<hr);
8546 regs[i].wasdirty|=(1<<hr)®s[i-1].dirty;
8547 regs[i].dirty|=(1<<hr)®s[i-1].dirty;
8548 regs[i].wasconst&=~(1<<hr);
8549 regs[i].isconst&=~(1<<hr);
8550 branch_regs[i].regmap_entry[hr]=f_regmap[hr];
8551 branch_regs[i].wasdirty&=~(1<<hr);
8552 branch_regs[i].wasdirty|=(1<<hr)®s[i].dirty;
8553 branch_regs[i].regmap[hr]=f_regmap[hr];
8554 branch_regs[i].dirty&=~(1<<hr);
8555 branch_regs[i].dirty|=(1<<hr)®s[i].dirty;
8556 branch_regs[i].wasconst&=~(1<<hr);
8557 branch_regs[i].isconst&=~(1<<hr);
8559 regmap_pre[i+2][hr]=f_regmap[hr];
8560 regs[i+2].wasdirty&=~(1<<hr);
8561 regs[i+2].wasdirty|=(1<<hr)®s[i].dirty;
8566 // Alloc register clean at beginning of loop,
8567 // but may dirty it in pass 6
8568 regs[k].regmap_entry[hr]=f_regmap[hr];
8569 regs[k].regmap[hr]=f_regmap[hr];
8570 regs[k].dirty&=~(1<<hr);
8571 regs[k].wasconst&=~(1<<hr);
8572 regs[k].isconst&=~(1<<hr);
8573 if(dops[k].itype==UJUMP||dops[k].itype==RJUMP||dops[k].itype==CJUMP||dops[k].itype==SJUMP) {
8574 branch_regs[k].regmap_entry[hr]=f_regmap[hr];
8575 branch_regs[k].regmap[hr]=f_regmap[hr];
8576 branch_regs[k].dirty&=~(1<<hr);
8577 branch_regs[k].wasconst&=~(1<<hr);
8578 branch_regs[k].isconst&=~(1<<hr);
8580 regmap_pre[k+2][hr]=f_regmap[hr];
8581 regs[k+2].wasdirty&=~(1<<hr);
8586 regmap_pre[k+1][hr]=f_regmap[hr];
8587 regs[k+1].wasdirty&=~(1<<hr);
8590 if(regs[j].regmap[hr]==f_regmap[hr])
8591 regs[j].regmap_entry[hr]=f_regmap[hr];
8595 if(regs[j].regmap[hr]>=0)
8597 if(get_reg(regs[j].regmap,f_regmap[hr])>=0) {
8598 //printf("no-match due to different register\n");
8603 // Stop on unconditional branch
8606 if(dops[j].itype==CJUMP||dops[j].itype==SJUMP)
8609 if(count_free_regs(regs[j].regmap)<=minimum_free_regs[j+1])
8612 if(count_free_regs(branch_regs[j].regmap)<=minimum_free_regs[j+1])
8615 if(get_reg(branch_regs[j].regmap,f_regmap[hr])>=0) {
8616 //printf("no-match due to different register (branch)\n");
8620 if(count_free_regs(regs[j].regmap)<=minimum_free_regs[j]) {
8621 //printf("No free regs for store %x\n",start+j*4);
8624 assert(f_regmap[hr]<64);
8631 // Non branch or undetermined branch target
8632 for(hr=0;hr<HOST_REGS;hr++)
8634 if(hr!=EXCLUDE_REG) {
8635 if(regs[i].regmap[hr]>=0) {
8636 if(f_regmap[hr]!=regs[i].regmap[hr]) {
8637 // dealloc old register
8639 for(n=0;n<HOST_REGS;n++)
8641 if(f_regmap[n]==regs[i].regmap[hr]) {f_regmap[n]=-1;}
8643 // and alloc new one
8644 f_regmap[hr]=regs[i].regmap[hr];
8649 // Try to restore cycle count at branch targets
8651 for(j=i;j<slen-1;j++) {
8652 if(regs[j].regmap[HOST_CCREG]!=-1) break;
8653 if(count_free_regs(regs[j].regmap)<=minimum_free_regs[j]) {
8654 //printf("no free regs for store %x\n",start+j*4);
8658 if(regs[j].regmap[HOST_CCREG]==CCREG) {
8660 //printf("Extend CC, %x -> %x\n",start+k*4,start+j*4);
8662 regs[k].regmap_entry[HOST_CCREG]=CCREG;
8663 regs[k].regmap[HOST_CCREG]=CCREG;
8664 regmap_pre[k+1][HOST_CCREG]=CCREG;
8665 regs[k+1].wasdirty|=1<<HOST_CCREG;
8666 regs[k].dirty|=1<<HOST_CCREG;
8667 regs[k].wasconst&=~(1<<HOST_CCREG);
8668 regs[k].isconst&=~(1<<HOST_CCREG);
8671 regs[j].regmap_entry[HOST_CCREG]=CCREG;
8673 // Work backwards from the branch target
8674 if(j>i&&f_regmap[HOST_CCREG]==CCREG)
8676 //printf("Extend backwards\n");
8679 while(regs[k-1].regmap[HOST_CCREG]==-1) {
8680 if(count_free_regs(regs[k-1].regmap)<=minimum_free_regs[k-1]) {
8681 //printf("no free regs for store %x\n",start+(k-1)*4);
8686 if(regs[k-1].regmap[HOST_CCREG]==CCREG) {
8687 //printf("Extend CC, %x ->\n",start+k*4);
8689 regs[k].regmap_entry[HOST_CCREG]=CCREG;
8690 regs[k].regmap[HOST_CCREG]=CCREG;
8691 regmap_pre[k+1][HOST_CCREG]=CCREG;
8692 regs[k+1].wasdirty|=1<<HOST_CCREG;
8693 regs[k].dirty|=1<<HOST_CCREG;
8694 regs[k].wasconst&=~(1<<HOST_CCREG);
8695 regs[k].isconst&=~(1<<HOST_CCREG);
8700 //printf("Fail Extend CC, %x ->\n",start+k*4);
8704 if(dops[i].itype!=STORE&&dops[i].itype!=STORELR&&dops[i].itype!=C1LS&&dops[i].itype!=SHIFT&&
8705 dops[i].itype!=NOP&&dops[i].itype!=MOV&&dops[i].itype!=ALU&&dops[i].itype!=SHIFTIMM&&
8706 dops[i].itype!=IMM16&&dops[i].itype!=LOAD&&dops[i].itype!=COP1)
8708 memcpy(f_regmap,regs[i].regmap,sizeof(f_regmap));
8713 // This allocates registers (if possible) one instruction prior
8714 // to use, which can avoid a load-use penalty on certain CPUs.
8715 for(i=0;i<slen-1;i++)
8717 if(!i||(dops[i-1].itype!=UJUMP&&dops[i-1].itype!=CJUMP&&dops[i-1].itype!=SJUMP&&dops[i-1].itype!=RJUMP))
8721 if(dops[i].itype==ALU||dops[i].itype==MOV||dops[i].itype==LOAD||dops[i].itype==SHIFTIMM||dops[i].itype==IMM16
8722 ||((dops[i].itype==COP1||dops[i].itype==COP2)&&dops[i].opcode2<3))
8725 if((hr=get_reg(regs[i+1].regmap,dops[i+1].rs1))>=0)
8727 if(regs[i].regmap[hr]<0&®s[i+1].regmap_entry[hr]<0)
8729 regs[i].regmap[hr]=regs[i+1].regmap[hr];
8730 regmap_pre[i+1][hr]=regs[i+1].regmap[hr];
8731 regs[i+1].regmap_entry[hr]=regs[i+1].regmap[hr];
8732 regs[i].isconst&=~(1<<hr);
8733 regs[i].isconst|=regs[i+1].isconst&(1<<hr);
8734 constmap[i][hr]=constmap[i+1][hr];
8735 regs[i+1].wasdirty&=~(1<<hr);
8736 regs[i].dirty&=~(1<<hr);
8741 if((hr=get_reg(regs[i+1].regmap,dops[i+1].rs2))>=0)
8743 if(regs[i].regmap[hr]<0&®s[i+1].regmap_entry[hr]<0)
8745 regs[i].regmap[hr]=regs[i+1].regmap[hr];
8746 regmap_pre[i+1][hr]=regs[i+1].regmap[hr];
8747 regs[i+1].regmap_entry[hr]=regs[i+1].regmap[hr];
8748 regs[i].isconst&=~(1<<hr);
8749 regs[i].isconst|=regs[i+1].isconst&(1<<hr);
8750 constmap[i][hr]=constmap[i+1][hr];
8751 regs[i+1].wasdirty&=~(1<<hr);
8752 regs[i].dirty&=~(1<<hr);
8756 // Preload target address for load instruction (non-constant)
8757 if(dops[i+1].itype==LOAD&&dops[i+1].rs1&&get_reg(regs[i+1].regmap,dops[i+1].rs1)<0) {
8758 if((hr=get_reg(regs[i+1].regmap,dops[i+1].rt1))>=0)
8760 if(regs[i].regmap[hr]<0&®s[i+1].regmap_entry[hr]<0)
8762 regs[i].regmap[hr]=dops[i+1].rs1;
8763 regmap_pre[i+1][hr]=dops[i+1].rs1;
8764 regs[i+1].regmap_entry[hr]=dops[i+1].rs1;
8765 regs[i].isconst&=~(1<<hr);
8766 regs[i].isconst|=regs[i+1].isconst&(1<<hr);
8767 constmap[i][hr]=constmap[i+1][hr];
8768 regs[i+1].wasdirty&=~(1<<hr);
8769 regs[i].dirty&=~(1<<hr);
8773 // Load source into target register
8774 if(dops[i+1].lt1&&get_reg(regs[i+1].regmap,dops[i+1].rs1)<0) {
8775 if((hr=get_reg(regs[i+1].regmap,dops[i+1].rt1))>=0)
8777 if(regs[i].regmap[hr]<0&®s[i+1].regmap_entry[hr]<0)
8779 regs[i].regmap[hr]=dops[i+1].rs1;
8780 regmap_pre[i+1][hr]=dops[i+1].rs1;
8781 regs[i+1].regmap_entry[hr]=dops[i+1].rs1;
8782 regs[i].isconst&=~(1<<hr);
8783 regs[i].isconst|=regs[i+1].isconst&(1<<hr);
8784 constmap[i][hr]=constmap[i+1][hr];
8785 regs[i+1].wasdirty&=~(1<<hr);
8786 regs[i].dirty&=~(1<<hr);
8790 // Address for store instruction (non-constant)
8791 if(dops[i+1].itype==STORE||dops[i+1].itype==STORELR
8792 ||(dops[i+1].opcode&0x3b)==0x39||(dops[i+1].opcode&0x3b)==0x3a) { // SB/SH/SW/SD/SWC1/SDC1/SWC2/SDC2
8793 if(get_reg(regs[i+1].regmap,dops[i+1].rs1)<0) {
8794 hr=get_reg2(regs[i].regmap,regs[i+1].regmap,-1);
8795 if(hr<0) hr=get_reg(regs[i+1].regmap,-1);
8796 else {regs[i+1].regmap[hr]=AGEN1+((i+1)&1);regs[i+1].isconst&=~(1<<hr);}
8798 if(regs[i].regmap[hr]<0&®s[i+1].regmap_entry[hr]<0)
8800 regs[i].regmap[hr]=dops[i+1].rs1;
8801 regmap_pre[i+1][hr]=dops[i+1].rs1;
8802 regs[i+1].regmap_entry[hr]=dops[i+1].rs1;
8803 regs[i].isconst&=~(1<<hr);
8804 regs[i].isconst|=regs[i+1].isconst&(1<<hr);
8805 constmap[i][hr]=constmap[i+1][hr];
8806 regs[i+1].wasdirty&=~(1<<hr);
8807 regs[i].dirty&=~(1<<hr);
8811 if(dops[i+1].itype==LOADLR||(dops[i+1].opcode&0x3b)==0x31||(dops[i+1].opcode&0x3b)==0x32) { // LWC1/LDC1, LWC2/LDC2
8812 if(get_reg(regs[i+1].regmap,dops[i+1].rs1)<0) {
8814 hr=get_reg(regs[i+1].regmap,FTEMP);
8816 if(regs[i].regmap[hr]<0&®s[i+1].regmap_entry[hr]<0)
8818 regs[i].regmap[hr]=dops[i+1].rs1;
8819 regmap_pre[i+1][hr]=dops[i+1].rs1;
8820 regs[i+1].regmap_entry[hr]=dops[i+1].rs1;
8821 regs[i].isconst&=~(1<<hr);
8822 regs[i].isconst|=regs[i+1].isconst&(1<<hr);
8823 constmap[i][hr]=constmap[i+1][hr];
8824 regs[i+1].wasdirty&=~(1<<hr);
8825 regs[i].dirty&=~(1<<hr);
8827 else if((nr=get_reg2(regs[i].regmap,regs[i+1].regmap,-1))>=0)
8829 // move it to another register
8830 regs[i+1].regmap[hr]=-1;
8831 regmap_pre[i+2][hr]=-1;
8832 regs[i+1].regmap[nr]=FTEMP;
8833 regmap_pre[i+2][nr]=FTEMP;
8834 regs[i].regmap[nr]=dops[i+1].rs1;
8835 regmap_pre[i+1][nr]=dops[i+1].rs1;
8836 regs[i+1].regmap_entry[nr]=dops[i+1].rs1;
8837 regs[i].isconst&=~(1<<nr);
8838 regs[i+1].isconst&=~(1<<nr);
8839 regs[i].dirty&=~(1<<nr);
8840 regs[i+1].wasdirty&=~(1<<nr);
8841 regs[i+1].dirty&=~(1<<nr);
8842 regs[i+2].wasdirty&=~(1<<nr);
8846 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*/) {
8847 if(dops[i+1].itype==LOAD)
8848 hr=get_reg(regs[i+1].regmap,dops[i+1].rt1);
8849 if(dops[i+1].itype==LOADLR||(dops[i+1].opcode&0x3b)==0x31||(dops[i+1].opcode&0x3b)==0x32) // LWC1/LDC1, LWC2/LDC2
8850 hr=get_reg(regs[i+1].regmap,FTEMP);
8851 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
8852 hr=get_reg(regs[i+1].regmap,AGEN1+((i+1)&1));
8853 if(hr<0) hr=get_reg(regs[i+1].regmap,-1);
8855 if(hr>=0&®s[i].regmap[hr]<0) {
8856 int rs=get_reg(regs[i+1].regmap,dops[i+1].rs1);
8857 if(rs>=0&&((regs[i+1].wasconst>>rs)&1)) {
8858 regs[i].regmap[hr]=AGEN1+((i+1)&1);
8859 regmap_pre[i+1][hr]=AGEN1+((i+1)&1);
8860 regs[i+1].regmap_entry[hr]=AGEN1+((i+1)&1);
8861 regs[i].isconst&=~(1<<hr);
8862 regs[i+1].wasdirty&=~(1<<hr);
8863 regs[i].dirty&=~(1<<hr);
8872 /* Pass 6 - Optimize clean/dirty state */
8873 clean_registers(0,slen-1,1);
8875 /* Pass 7 - Identify 32-bit registers */
8876 for (i=slen-1;i>=0;i--)
8878 if(dops[i].itype==CJUMP||dops[i].itype==SJUMP)
8880 // Conditional branch
8881 if((source[i]>>16)!=0x1000&&i<slen-2) {
8882 // Mark this address as a branch target since it may be called
8883 // upon return from interrupt
8889 if(dops[slen-1].itype==SPAN) {
8890 dops[slen-1].bt=1; // Mark as a branch target so instruction can restart after exception
8894 /* Debug/disassembly */
8899 for(r=1;r<=CCREG;r++) {
8900 if((unneeded_reg[i]>>r)&1) {
8901 if(r==HIREG) printf(" HI");
8902 else if(r==LOREG) printf(" LO");
8903 else printf(" r%d",r);
8907 #if defined(__i386__) || defined(__x86_64__)
8908 printf("pre: eax=%d ecx=%d edx=%d ebx=%d ebp=%d esi=%d edi=%d\n",regmap_pre[i][0],regmap_pre[i][1],regmap_pre[i][2],regmap_pre[i][3],regmap_pre[i][5],regmap_pre[i][6],regmap_pre[i][7]);
8911 printf("pre: r0=%d r1=%d r2=%d r3=%d r4=%d r5=%d r6=%d r7=%d r8=%d r9=%d r10=%d r12=%d\n",regmap_pre[i][0],regmap_pre[i][1],regmap_pre[i][2],regmap_pre[i][3],regmap_pre[i][4],regmap_pre[i][5],regmap_pre[i][6],regmap_pre[i][7],regmap_pre[i][8],regmap_pre[i][9],regmap_pre[i][10],regmap_pre[i][12]);
8913 #if defined(__i386__) || defined(__x86_64__)
8915 if(needed_reg[i]&1) printf("eax ");
8916 if((needed_reg[i]>>1)&1) printf("ecx ");
8917 if((needed_reg[i]>>2)&1) printf("edx ");
8918 if((needed_reg[i]>>3)&1) printf("ebx ");
8919 if((needed_reg[i]>>5)&1) printf("ebp ");
8920 if((needed_reg[i]>>6)&1) printf("esi ");
8921 if((needed_reg[i]>>7)&1) printf("edi ");
8923 printf("entry: eax=%d ecx=%d edx=%d ebx=%d ebp=%d esi=%d edi=%d\n",regs[i].regmap_entry[0],regs[i].regmap_entry[1],regs[i].regmap_entry[2],regs[i].regmap_entry[3],regs[i].regmap_entry[5],regs[i].regmap_entry[6],regs[i].regmap_entry[7]);
8925 if(regs[i].wasdirty&1) printf("eax ");
8926 if((regs[i].wasdirty>>1)&1) printf("ecx ");
8927 if((regs[i].wasdirty>>2)&1) printf("edx ");
8928 if((regs[i].wasdirty>>3)&1) printf("ebx ");
8929 if((regs[i].wasdirty>>5)&1) printf("ebp ");
8930 if((regs[i].wasdirty>>6)&1) printf("esi ");
8931 if((regs[i].wasdirty>>7)&1) printf("edi ");
8934 printf("entry: r0=%d r1=%d r2=%d r3=%d r4=%d r5=%d r6=%d r7=%d r8=%d r9=%d r10=%d r12=%d\n",regs[i].regmap_entry[0],regs[i].regmap_entry[1],regs[i].regmap_entry[2],regs[i].regmap_entry[3],regs[i].regmap_entry[4],regs[i].regmap_entry[5],regs[i].regmap_entry[6],regs[i].regmap_entry[7],regs[i].regmap_entry[8],regs[i].regmap_entry[9],regs[i].regmap_entry[10],regs[i].regmap_entry[12]);
8936 if(regs[i].wasdirty&1) printf("r0 ");
8937 if((regs[i].wasdirty>>1)&1) printf("r1 ");
8938 if((regs[i].wasdirty>>2)&1) printf("r2 ");
8939 if((regs[i].wasdirty>>3)&1) printf("r3 ");
8940 if((regs[i].wasdirty>>4)&1) printf("r4 ");
8941 if((regs[i].wasdirty>>5)&1) printf("r5 ");
8942 if((regs[i].wasdirty>>6)&1) printf("r6 ");
8943 if((regs[i].wasdirty>>7)&1) printf("r7 ");
8944 if((regs[i].wasdirty>>8)&1) printf("r8 ");
8945 if((regs[i].wasdirty>>9)&1) printf("r9 ");
8946 if((regs[i].wasdirty>>10)&1) printf("r10 ");
8947 if((regs[i].wasdirty>>12)&1) printf("r12 ");
8950 disassemble_inst(i);
8951 //printf ("ccadj[%d] = %d\n",i,ccadj[i]);
8952 #if defined(__i386__) || defined(__x86_64__)
8953 printf("eax=%d ecx=%d edx=%d ebx=%d ebp=%d esi=%d edi=%d dirty: ",regs[i].regmap[0],regs[i].regmap[1],regs[i].regmap[2],regs[i].regmap[3],regs[i].regmap[5],regs[i].regmap[6],regs[i].regmap[7]);
8954 if(regs[i].dirty&1) printf("eax ");
8955 if((regs[i].dirty>>1)&1) printf("ecx ");
8956 if((regs[i].dirty>>2)&1) printf("edx ");
8957 if((regs[i].dirty>>3)&1) printf("ebx ");
8958 if((regs[i].dirty>>5)&1) printf("ebp ");
8959 if((regs[i].dirty>>6)&1) printf("esi ");
8960 if((regs[i].dirty>>7)&1) printf("edi ");
8963 printf("r0=%d r1=%d r2=%d r3=%d r4=%d r5=%d r6=%d r7=%d r8=%d r9=%d r10=%d r12=%d dirty: ",regs[i].regmap[0],regs[i].regmap[1],regs[i].regmap[2],regs[i].regmap[3],regs[i].regmap[4],regs[i].regmap[5],regs[i].regmap[6],regs[i].regmap[7],regs[i].regmap[8],regs[i].regmap[9],regs[i].regmap[10],regs[i].regmap[12]);
8964 if(regs[i].dirty&1) printf("r0 ");
8965 if((regs[i].dirty>>1)&1) printf("r1 ");
8966 if((regs[i].dirty>>2)&1) printf("r2 ");
8967 if((regs[i].dirty>>3)&1) printf("r3 ");
8968 if((regs[i].dirty>>4)&1) printf("r4 ");
8969 if((regs[i].dirty>>5)&1) printf("r5 ");
8970 if((regs[i].dirty>>6)&1) printf("r6 ");
8971 if((regs[i].dirty>>7)&1) printf("r7 ");
8972 if((regs[i].dirty>>8)&1) printf("r8 ");
8973 if((regs[i].dirty>>9)&1) printf("r9 ");
8974 if((regs[i].dirty>>10)&1) printf("r10 ");
8975 if((regs[i].dirty>>12)&1) printf("r12 ");
8978 if(regs[i].isconst) {
8979 printf("constants: ");
8980 #if defined(__i386__) || defined(__x86_64__)
8981 if(regs[i].isconst&1) printf("eax=%x ",(u_int)constmap[i][0]);
8982 if((regs[i].isconst>>1)&1) printf("ecx=%x ",(u_int)constmap[i][1]);
8983 if((regs[i].isconst>>2)&1) printf("edx=%x ",(u_int)constmap[i][2]);
8984 if((regs[i].isconst>>3)&1) printf("ebx=%x ",(u_int)constmap[i][3]);
8985 if((regs[i].isconst>>5)&1) printf("ebp=%x ",(u_int)constmap[i][5]);
8986 if((regs[i].isconst>>6)&1) printf("esi=%x ",(u_int)constmap[i][6]);
8987 if((regs[i].isconst>>7)&1) printf("edi=%x ",(u_int)constmap[i][7]);
8989 #if defined(__arm__) || defined(__aarch64__)
8991 for (r = 0; r < ARRAY_SIZE(constmap[i]); r++)
8992 if ((regs[i].isconst >> r) & 1)
8993 printf(" r%d=%x", r, (u_int)constmap[i][r]);
8997 if(dops[i].itype==RJUMP||dops[i].itype==UJUMP||dops[i].itype==CJUMP||dops[i].itype==SJUMP) {
8998 #if defined(__i386__) || defined(__x86_64__)
8999 printf("branch(%d): eax=%d ecx=%d edx=%d ebx=%d ebp=%d esi=%d edi=%d dirty: ",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]);
9000 if(branch_regs[i].dirty&1) printf("eax ");
9001 if((branch_regs[i].dirty>>1)&1) printf("ecx ");
9002 if((branch_regs[i].dirty>>2)&1) printf("edx ");
9003 if((branch_regs[i].dirty>>3)&1) printf("ebx ");
9004 if((branch_regs[i].dirty>>5)&1) printf("ebp ");
9005 if((branch_regs[i].dirty>>6)&1) printf("esi ");
9006 if((branch_regs[i].dirty>>7)&1) printf("edi ");
9009 printf("branch(%d): r0=%d r1=%d r2=%d r3=%d r4=%d r5=%d r6=%d r7=%d r8=%d r9=%d r10=%d r12=%d dirty: ",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[4],branch_regs[i].regmap[5],branch_regs[i].regmap[6],branch_regs[i].regmap[7],branch_regs[i].regmap[8],branch_regs[i].regmap[9],branch_regs[i].regmap[10],branch_regs[i].regmap[12]);
9010 if(branch_regs[i].dirty&1) printf("r0 ");
9011 if((branch_regs[i].dirty>>1)&1) printf("r1 ");
9012 if((branch_regs[i].dirty>>2)&1) printf("r2 ");
9013 if((branch_regs[i].dirty>>3)&1) printf("r3 ");
9014 if((branch_regs[i].dirty>>4)&1) printf("r4 ");
9015 if((branch_regs[i].dirty>>5)&1) printf("r5 ");
9016 if((branch_regs[i].dirty>>6)&1) printf("r6 ");
9017 if((branch_regs[i].dirty>>7)&1) printf("r7 ");
9018 if((branch_regs[i].dirty>>8)&1) printf("r8 ");
9019 if((branch_regs[i].dirty>>9)&1) printf("r9 ");
9020 if((branch_regs[i].dirty>>10)&1) printf("r10 ");
9021 if((branch_regs[i].dirty>>12)&1) printf("r12 ");
9027 /* Pass 8 - Assembly */
9028 linkcount=0;stubcount=0;
9029 ds=0;is_delayslot=0;
9031 void *beginning=start_block();
9036 void *instr_addr0_override = NULL;
9038 if (start == 0x80030000) {
9039 // nasty hack for the fastbios thing
9040 // override block entry to this code
9041 instr_addr0_override = out;
9042 emit_movimm(start,0);
9043 // abuse io address var as a flag that we
9044 // have already returned here once
9045 emit_readword(&address,1);
9046 emit_writeword(0,&pcaddr);
9047 emit_writeword(0,&address);
9050 emit_jeq(out + 4*2);
9051 emit_far_jump(new_dyna_leave);
9053 emit_jne(new_dyna_leave);
9058 //if(ds) printf("ds: ");
9059 disassemble_inst(i);
9061 ds=0; // Skip delay slot
9062 if(dops[i].bt) assem_debug("OOPS - branch into delay slot\n");
9063 instr_addr[i] = NULL;
9065 speculate_register_values(i);
9066 #ifndef DESTRUCTIVE_WRITEBACK
9067 if (i < 2 || !is_ujump(i-2))
9069 wb_valid(regmap_pre[i],regs[i].regmap_entry,dirty_pre,regs[i].wasdirty,unneeded_reg[i]);
9071 if((dops[i].itype==CJUMP||dops[i].itype==SJUMP)&&!dops[i].likely) {
9072 dirty_pre=branch_regs[i].dirty;
9074 dirty_pre=regs[i].dirty;
9078 if (i < 2 || !is_ujump(i-2))
9080 wb_invalidate(regmap_pre[i],regs[i].regmap_entry,regs[i].wasdirty,unneeded_reg[i]);
9081 loop_preload(regmap_pre[i],regs[i].regmap_entry);
9083 // branch target entry point
9084 instr_addr[i] = out;
9085 assem_debug("<->\n");
9086 drc_dbg_emit_do_cmp(i);
9089 if(regs[i].regmap_entry[HOST_CCREG]==CCREG&®s[i].regmap[HOST_CCREG]!=CCREG)
9090 wb_register(CCREG,regs[i].regmap_entry,regs[i].wasdirty);
9091 load_regs(regs[i].regmap_entry,regs[i].regmap,dops[i].rs1,dops[i].rs2);
9092 address_generation(i,®s[i],regs[i].regmap_entry);
9093 load_consts(regmap_pre[i],regs[i].regmap,i);
9094 if(dops[i].itype==RJUMP||dops[i].itype==UJUMP||dops[i].itype==CJUMP||dops[i].itype==SJUMP)
9096 // Load the delay slot registers if necessary
9097 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))
9098 load_regs(regs[i].regmap_entry,regs[i].regmap,dops[i+1].rs1,dops[i+1].rs1);
9099 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))
9100 load_regs(regs[i].regmap_entry,regs[i].regmap,dops[i+1].rs2,dops[i+1].rs2);
9101 if(dops[i+1].itype==STORE||dops[i+1].itype==STORELR||(dops[i+1].opcode&0x3b)==0x39||(dops[i+1].opcode&0x3b)==0x3a)
9102 load_regs(regs[i].regmap_entry,regs[i].regmap,INVCP,INVCP);
9106 // Preload registers for following instruction
9107 if(dops[i+1].rs1!=dops[i].rs1&&dops[i+1].rs1!=dops[i].rs2)
9108 if(dops[i+1].rs1!=dops[i].rt1&&dops[i+1].rs1!=dops[i].rt2)
9109 load_regs(regs[i].regmap_entry,regs[i].regmap,dops[i+1].rs1,dops[i+1].rs1);
9110 if(dops[i+1].rs2!=dops[i+1].rs1&&dops[i+1].rs2!=dops[i].rs1&&dops[i+1].rs2!=dops[i].rs2)
9111 if(dops[i+1].rs2!=dops[i].rt1&&dops[i+1].rs2!=dops[i].rt2)
9112 load_regs(regs[i].regmap_entry,regs[i].regmap,dops[i+1].rs2,dops[i+1].rs2);
9114 // TODO: if(is_ooo(i)) address_generation(i+1);
9115 if(dops[i].itype==CJUMP)
9116 load_regs(regs[i].regmap_entry,regs[i].regmap,CCREG,CCREG);
9117 if(dops[i].itype==STORE||dops[i].itype==STORELR||(dops[i].opcode&0x3b)==0x39||(dops[i].opcode&0x3b)==0x3a)
9118 load_regs(regs[i].regmap_entry,regs[i].regmap,INVCP,INVCP);
9120 switch(dops[i].itype) {
9122 alu_assemble(i,®s[i]);break;
9124 imm16_assemble(i,®s[i]);break;
9126 shift_assemble(i,®s[i]);break;
9128 shiftimm_assemble(i,®s[i]);break;
9130 load_assemble(i,®s[i]);break;
9132 loadlr_assemble(i,®s[i]);break;
9134 store_assemble(i,®s[i]);break;
9136 storelr_assemble(i,®s[i]);break;
9138 cop0_assemble(i,®s[i]);break;
9140 cop1_assemble(i,®s[i]);break;
9142 c1ls_assemble(i,®s[i]);break;
9144 cop2_assemble(i,®s[i]);break;
9146 c2ls_assemble(i,®s[i]);break;
9148 c2op_assemble(i,®s[i]);break;
9150 multdiv_assemble(i,®s[i]);
9151 multdiv_prepare_stall(i,®s[i]);
9154 mov_assemble(i,®s[i]);break;
9156 syscall_assemble(i,®s[i]);break;
9158 hlecall_assemble(i,®s[i]);break;
9160 intcall_assemble(i,®s[i]);break;
9162 ujump_assemble(i,®s[i]);ds=1;break;
9164 rjump_assemble(i,®s[i]);ds=1;break;
9166 cjump_assemble(i,®s[i]);ds=1;break;
9168 sjump_assemble(i,®s[i]);ds=1;break;
9170 pagespan_assemble(i,®s[i]);break;
9175 literal_pool_jumpover(256);
9180 if (slen > 0 && dops[slen-1].itype == INTCALL) {
9181 // no ending needed for this block since INTCALL never returns
9183 // If the block did not end with an unconditional branch,
9184 // add a jump to the next instruction.
9186 if(!is_ujump(i-2)&&dops[i-1].itype!=SPAN) {
9187 assert(dops[i-1].itype!=UJUMP&&dops[i-1].itype!=CJUMP&&dops[i-1].itype!=SJUMP&&dops[i-1].itype!=RJUMP);
9189 if(dops[i-2].itype!=CJUMP&&dops[i-2].itype!=SJUMP) {
9190 store_regs_bt(regs[i-1].regmap,regs[i-1].dirty,start+i*4);
9191 if(regs[i-1].regmap[HOST_CCREG]!=CCREG)
9192 emit_loadreg(CCREG,HOST_CCREG);
9193 emit_addimm(HOST_CCREG,CLOCK_ADJUST(ccadj[i-1]+1),HOST_CCREG);
9195 else if(!dops[i-2].likely)
9197 store_regs_bt(branch_regs[i-2].regmap,branch_regs[i-2].dirty,start+i*4);
9198 assert(branch_regs[i-2].regmap[HOST_CCREG]==CCREG);
9202 store_regs_bt(regs[i-2].regmap,regs[i-2].dirty,start+i*4);
9203 assert(regs[i-2].regmap[HOST_CCREG]==CCREG);
9205 add_to_linker(out,start+i*4,0);
9212 assert(dops[i-1].itype!=UJUMP&&dops[i-1].itype!=CJUMP&&dops[i-1].itype!=SJUMP&&dops[i-1].itype!=RJUMP);
9213 store_regs_bt(regs[i-1].regmap,regs[i-1].dirty,start+i*4);
9214 if(regs[i-1].regmap[HOST_CCREG]!=CCREG)
9215 emit_loadreg(CCREG,HOST_CCREG);
9216 emit_addimm(HOST_CCREG,CLOCK_ADJUST(ccadj[i-1]+1),HOST_CCREG);
9217 add_to_linker(out,start+i*4,0);
9221 // TODO: delay slot stubs?
9223 for(i=0;i<stubcount;i++)
9225 switch(stubs[i].type)
9233 do_readstub(i);break;
9238 do_writestub(i);break;
9242 do_invstub(i);break;
9244 do_cop1stub(i);break;
9246 do_unalignedwritestub(i);break;
9250 if (instr_addr0_override)
9251 instr_addr[0] = instr_addr0_override;
9253 /* Pass 9 - Linker */
9254 for(i=0;i<linkcount;i++)
9256 assem_debug("%p -> %8x\n",link_addr[i].addr,link_addr[i].target);
9258 if (!link_addr[i].ext)
9261 void *addr = check_addr(link_addr[i].target);
9262 emit_extjump(link_addr[i].addr, link_addr[i].target);
9264 set_jump_target(link_addr[i].addr, addr);
9265 add_jump_out(link_addr[i].target,stub);
9268 set_jump_target(link_addr[i].addr, stub);
9273 int target=(link_addr[i].target-start)>>2;
9274 assert(target>=0&&target<slen);
9275 assert(instr_addr[target]);
9276 //#ifdef CORTEX_A8_BRANCH_PREDICTION_HACK
9277 //set_jump_target_fillslot(link_addr[i].addr,instr_addr[target],link_addr[i].ext>>1);
9279 set_jump_target(link_addr[i].addr, instr_addr[target]);
9284 u_int source_len = slen*4;
9285 if (dops[slen-1].itype == INTCALL && source_len > 4)
9286 // no need to treat the last instruction as compiled
9287 // as interpreter fully handles it
9290 if ((u_char *)copy + source_len > (u_char *)shadow + sizeof(shadow))
9293 // External Branch Targets (jump_in)
9296 if(dops[i].bt||i==0)
9298 if(instr_addr[i]) // TODO - delay slots (=null)
9300 u_int vaddr=start+i*4;
9301 u_int page=get_page(vaddr);
9302 u_int vpage=get_vpage(vaddr);
9305 assem_debug("%p (%d) <- %8x\n",instr_addr[i],i,start+i*4);
9306 assem_debug("jump_in: %x\n",start+i*4);
9307 ll_add(jump_dirty+vpage,vaddr,out);
9308 void *entry_point = do_dirty_stub(i, source_len);
9309 ll_add_flags(jump_in+page,vaddr,state_rflags,entry_point);
9310 // If there was an existing entry in the hash table,
9311 // replace it with the new address.
9312 // Don't add new entries. We'll insert the
9313 // ones that actually get used in check_addr().
9314 struct ht_entry *ht_bin = hash_table_get(vaddr);
9315 if (ht_bin->vaddr[0] == vaddr)
9316 ht_bin->tcaddr[0] = entry_point;
9317 if (ht_bin->vaddr[1] == vaddr)
9318 ht_bin->tcaddr[1] = entry_point;
9323 // Write out the literal pool if necessary
9325 #ifdef CORTEX_A8_BRANCH_PREDICTION_HACK
9327 if(((u_int)out)&7) emit_addnop(13);
9329 assert(out - (u_char *)beginning < MAX_OUTPUT_BLOCK_SIZE);
9330 //printf("shadow buffer: %p-%p\n",copy,(u_char *)copy+slen*4);
9331 memcpy(copy, source, source_len);
9334 end_block(beginning);
9336 // If we're within 256K of the end of the buffer,
9337 // start over from the beginning. (Is 256K enough?)
9338 if (out > ndrc->translation_cache + sizeof(ndrc->translation_cache) - MAX_OUTPUT_BLOCK_SIZE)
9339 out = ndrc->translation_cache;
9341 // Trap writes to any of the pages we compiled
9342 for(i=start>>12;i<=(start+slen*4)>>12;i++) {
9345 inv_code_start=inv_code_end=~0;
9347 // for PCSX we need to mark all mirrors too
9348 if(get_page(start)<(RAM_SIZE>>12))
9349 for(i=start>>12;i<=(start+slen*4)>>12;i++)
9350 invalid_code[((u_int)0x00000000>>12)|(i&0x1ff)]=
9351 invalid_code[((u_int)0x80000000>>12)|(i&0x1ff)]=
9352 invalid_code[((u_int)0xa0000000>>12)|(i&0x1ff)]=0;
9354 /* Pass 10 - Free memory by expiring oldest blocks */
9356 int end=(((out-ndrc->translation_cache)>>(TARGET_SIZE_2-16))+16384)&65535;
9359 int shift=TARGET_SIZE_2-3; // Divide into 8 blocks
9360 uintptr_t base_offs = ((uintptr_t)(expirep >> 13) << shift); // Base offset of this block
9361 uintptr_t base_offs_s = base_offs >> shift;
9362 inv_debug("EXP: Phase %d\n",expirep);
9363 switch((expirep>>11)&3)
9366 // Clear jump_in and jump_dirty
9367 ll_remove_matching_addrs(jump_in+(expirep&2047),base_offs_s,shift);
9368 ll_remove_matching_addrs(jump_dirty+(expirep&2047),base_offs_s,shift);
9369 ll_remove_matching_addrs(jump_in+2048+(expirep&2047),base_offs_s,shift);
9370 ll_remove_matching_addrs(jump_dirty+2048+(expirep&2047),base_offs_s,shift);
9374 ll_kill_pointers(jump_out[expirep&2047],base_offs_s,shift);
9375 ll_kill_pointers(jump_out[(expirep&2047)+2048],base_offs_s,shift);
9380 struct ht_entry *ht_bin = &hash_table[((expirep&2047)<<5)+i];
9381 uintptr_t o1 = (u_char *)ht_bin->tcaddr[1] - ndrc->translation_cache;
9382 uintptr_t o2 = o1 - MAX_OUTPUT_BLOCK_SIZE;
9383 if ((o1 >> shift) == base_offs_s || (o2 >> shift) == base_offs_s) {
9384 inv_debug("EXP: Remove hash %x -> %p\n",ht_bin->vaddr[1],ht_bin->tcaddr[1]);
9385 ht_bin->vaddr[1] = -1;
9386 ht_bin->tcaddr[1] = NULL;
9388 o1 = (u_char *)ht_bin->tcaddr[0] - ndrc->translation_cache;
9389 o2 = o1 - MAX_OUTPUT_BLOCK_SIZE;
9390 if ((o1 >> shift) == base_offs_s || (o2 >> shift) == base_offs_s) {
9391 inv_debug("EXP: Remove hash %x -> %p\n",ht_bin->vaddr[0],ht_bin->tcaddr[0]);
9392 ht_bin->vaddr[0] = ht_bin->vaddr[1];
9393 ht_bin->tcaddr[0] = ht_bin->tcaddr[1];
9394 ht_bin->vaddr[1] = -1;
9395 ht_bin->tcaddr[1] = NULL;
9401 if((expirep&2047)==0)
9403 ll_remove_matching_addrs(jump_out+(expirep&2047),base_offs_s,shift);
9404 ll_remove_matching_addrs(jump_out+2048+(expirep&2047),base_offs_s,shift);
9407 expirep=(expirep+1)&65535;
9412 // vim:shiftwidth=2:expandtab