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 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
25 #include <stdint.h> //include for uint64_t
28 #include "../recomp.h"
29 #include "../recomph.h" //include for function prototypes
30 #include "../macros.h"
33 #include "../interupt.h"
34 #include "new_dynarec.h"
36 #include "../../memory/memory.h"
37 #include "../../main/rom.h"
41 #if NEW_DYNAREC == NEW_DYNAREC_X86
42 #include "assem_x86.h"
43 #elif NEW_DYNAREC == NEW_DYNAREC_ARM
44 #include "assem_arm.h"
46 #error Unsupported dynarec architecture
50 #define MAX_OUTPUT_BLOCK_SIZE 262144
51 #define CLOCK_DIVIDER 2
57 signed char regmap_entry[HOST_REGS];
58 signed char regmap[HOST_REGS];
67 uint64_t constmap[HOST_REGS];
75 struct ll_entry *next;
80 static u_int pagelimit;
81 static char insn[MAXBLOCK][10];
82 static u_char itype[MAXBLOCK];
83 static u_char opcode[MAXBLOCK];
84 static u_char opcode2[MAXBLOCK];
85 static u_char bt[MAXBLOCK];
86 static u_char rs1[MAXBLOCK];
87 static u_char rs2[MAXBLOCK];
88 static u_char rt1[MAXBLOCK];
89 static u_char rt2[MAXBLOCK];
90 static u_char us1[MAXBLOCK];
91 static u_char us2[MAXBLOCK];
92 static u_char dep1[MAXBLOCK];
93 static u_char dep2[MAXBLOCK];
94 static u_char lt1[MAXBLOCK];
95 static int imm[MAXBLOCK];
96 static u_int ba[MAXBLOCK];
97 static char likely[MAXBLOCK];
98 static char is_ds[MAXBLOCK];
99 static char ooo[MAXBLOCK];
100 static uint64_t unneeded_reg[MAXBLOCK];
101 static uint64_t unneeded_reg_upper[MAXBLOCK];
102 static uint64_t branch_unneeded_reg[MAXBLOCK];
103 static uint64_t branch_unneeded_reg_upper[MAXBLOCK];
104 static uint64_t p32[MAXBLOCK];
105 static uint64_t pr32[MAXBLOCK];
106 static signed char regmap_pre[MAXBLOCK][HOST_REGS];
108 static signed char regmap[MAXBLOCK][HOST_REGS];
109 static signed char regmap_entry[MAXBLOCK][HOST_REGS];
111 static uint64_t constmap[MAXBLOCK][HOST_REGS];
112 static struct regstat regs[MAXBLOCK];
113 static struct regstat branch_regs[MAXBLOCK];
114 static signed char minimum_free_regs[MAXBLOCK];
115 static u_int needed_reg[MAXBLOCK];
116 static uint64_t requires_32bit[MAXBLOCK];
117 static u_int wont_dirty[MAXBLOCK];
118 static u_int will_dirty[MAXBLOCK];
119 static int ccadj[MAXBLOCK];
121 static u_int instr_addr[MAXBLOCK];
122 static u_int link_addr[MAXBLOCK][3];
123 static int linkcount;
124 static u_int stubs[MAXBLOCK*3][8];
125 static int stubcount;
126 static int literalcount;
127 static int is_delayslot;
128 static int cop1_usable;
130 struct ll_entry *jump_in[4096];
131 static struct ll_entry *jump_out[4096];
132 struct ll_entry *jump_dirty[4096];
133 u_int hash_table[65536][4] __attribute__((aligned(16)));
134 static char shadow[2097152] __attribute__((aligned(16)));
138 static u_int stop_after_jal;
139 extern u_char restore_candidate[512];
140 extern int cycle_count;
142 /* registers that may be allocated */
144 #define HIREG 32 // hi
145 #define LOREG 33 // lo
146 #define FSREG 34 // FPU status (FCSR)
147 #define CSREG 35 // Coprocessor status
148 #define CCREG 36 // Cycle count
149 #define INVCP 37 // Pointer to invalid_code
150 #define MMREG 38 // Pointer to memory_map
151 #define ROREG 39 // ram offset (if rdram!=0x80000000)
153 #define FTEMP 40 // FPU temporary register
154 #define PTEMP 41 // Prefetch temporary register
155 #define TLREG 42 // TLB mapping offset
156 #define RHASH 43 // Return address hash
157 #define RHTBL 44 // Return address hash table address
158 #define RTEMP 45 // JR/JALR address register
160 #define AGEN1 46 // Address generation temporary register
161 #define AGEN2 47 // Address generation temporary register
162 #define MGEN1 48 // Maptable address generation temporary register
163 #define MGEN2 49 // Maptable address generation temporary register
164 #define BTREG 50 // Branch target temporary register
166 /* instruction types */
167 #define NOP 0 // No operation
168 #define LOAD 1 // Load
169 #define STORE 2 // Store
170 #define LOADLR 3 // Unaligned load
171 #define STORELR 4 // Unaligned store
172 #define MOV 5 // Move
173 #define ALU 6 // Arithmetic/logic
174 #define MULTDIV 7 // Multiply/divide
175 #define SHIFT 8 // Shift by register
176 #define SHIFTIMM 9// Shift by immediate
177 #define IMM16 10 // 16-bit immediate
178 #define RJUMP 11 // Unconditional jump to register
179 #define UJUMP 12 // Unconditional jump
180 #define CJUMP 13 // Conditional branch (BEQ/BNE/BGTZ/BLEZ)
181 #define SJUMP 14 // Conditional branch (regimm format)
182 #define COP0 15 // Coprocessor 0
183 #define COP1 16 // Coprocessor 1
184 #define C1LS 17 // Coprocessor 1 load/store
185 #define FJUMP 18 // Conditional branch (floating point)
186 #define FLOAT 19 // Floating point unit
187 #define FCONV 20 // Convert integer to float
188 #define FCOMP 21 // Floating point compare (sets FSREG)
189 #define SYSCALL 22// SYSCALL
190 #define OTHER 23 // Other
191 #define SPAN 24 // Branch/delay slot spans 2 pages
192 #define NI 25 // Not implemented
201 #define LOADBU_STUB 7
202 #define LOADHU_STUB 8
203 #define STOREB_STUB 9
204 #define STOREH_STUB 10
205 #define STOREW_STUB 11
206 #define STORED_STUB 12
207 #define STORELR_STUB 13
208 #define INVCODE_STUB 14
215 /* bug-fix to implement __clear_cache (missing in Android; http://code.google.com/p/android/issues/detail?id=1803) */
216 void __clear_cache_bugfix(char* begin, char *end);
218 #define __clear_cache __clear_cache_bugfix
222 int new_recompile_block(int addr);
223 void *get_addr_ht(u_int vaddr);
224 static void remove_hash(int vaddr);
226 void dyna_linker_ds();
228 void verify_code_vm();
229 void verify_code_ds();
232 void fp_exception_ds();
235 #if NEW_DYNAREC == NEW_DYNAREC_ARM
236 static void invalidate_addr(u_int addr);
242 void read_nomem_new();
243 void read_nomemb_new();
244 void read_nomemh_new();
245 void read_nomemd_new();
246 void write_nomem_new();
247 void write_nomemb_new();
248 void write_nomemh_new();
249 void write_nomemd_new();
250 void write_rdram_new();
251 void write_rdramb_new();
252 void write_rdramh_new();
253 void write_rdramd_new();
254 extern u_int memory_map[1048576];
256 // Needed by assembler
257 static void wb_register(signed char r,signed char regmap[],uint64_t dirty,uint64_t is32);
258 static void wb_dirtys(signed char i_regmap[],uint64_t i_is32,uint64_t i_dirty);
259 static void wb_needed_dirtys(signed char i_regmap[],uint64_t i_is32,uint64_t i_dirty,int addr);
260 static void load_all_regs(signed char i_regmap[]);
261 static void load_needed_regs(signed char i_regmap[],signed char next_regmap[]);
262 static void load_regs_entry(int t);
263 static void load_all_consts(signed char regmap[],int is32,u_int dirty,int i);
265 static void add_stub(int type,int addr,int retaddr,int a,int b,int c,int d,int e);
266 static void add_to_linker(int addr,int target,int ext);
267 static int verify_dirty(void *addr);
269 //static int tracedebug=0;
271 //#define DEBUG_CYCLE_COUNT 1
273 // Uncomment these two lines to generate debug output:
274 //#define ASSEM_DEBUG 1
275 //#define INV_DEBUG 1
277 // Uncomment this line to output the number of NOTCOMPILED blocks as they occur:
278 //#define COUNT_NOTCOMPILEDS 1
280 #if defined (COUNT_NOTCOMPILEDS )
281 int notcompiledCount = 0;
283 static void nullf() {}
285 #if defined( ASSEM_DEBUG )
286 #define assem_debug(...) DebugMessage(M64MSG_VERBOSE, __VA_ARGS__)
288 #define assem_debug nullf
290 #if defined( INV_DEBUG )
291 #define inv_debug(...) DebugMessage(M64MSG_VERBOSE, __VA_ARGS__)
293 #define inv_debug nullf
296 #define log_message(...) DebugMessage(M64MSG_VERBOSE, __VA_ARGS__)
298 static void tlb_hacks()
301 if (strncmp((char *) ROM_HEADER.Name, "GOLDENEYE",9) == 0)
305 switch (ROM_HEADER.Country_code&0xFF)
317 // Unknown country code
321 u_int rom_addr=(u_int)rom;
323 // Since memory_map is 32-bit, on 64-bit systems the rom needs to be
324 // in the lower 4G of memory to use this hack. Copy it if necessary.
325 if((void *)rom>(void *)0xffffffff) {
326 munmap(ROM_COPY, 67108864);
327 if(mmap(ROM_COPY, 12582912,
328 PROT_READ | PROT_WRITE,
329 MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS,
330 -1, 0) <= 0) {DebugMessage(M64MSG_ERROR, "mmap() failed");}
331 memcpy(ROM_COPY,rom,12582912);
332 rom_addr=(u_int)ROM_COPY;
336 for(n=0x7F000;n<0x80000;n++) {
337 memory_map[n]=(((u_int)(rom_addr+addr-0x7F000000))>>2)|0x40000000;
343 // Get address from virtual address
344 // This is called from the recompiled JR/JALR instructions
345 void *get_addr(u_int vaddr)
347 u_int page=(vaddr^0x80000000)>>12;
349 if(page>262143&&tlb_LUT_r[vaddr>>12]) page=(tlb_LUT_r[vaddr>>12]^0x80000000)>>12;
350 if(page>2048) page=2048+(page&2047);
351 if(vpage>262143&&tlb_LUT_r[vaddr>>12]) vpage&=2047; // jump_dirty uses a hash of the virtual address instead
352 if(vpage>2048) vpage=2048+(vpage&2047);
353 struct ll_entry *head;
354 //DebugMessage(M64MSG_VERBOSE, "TRACE: count=%d next=%d (get_addr %x,page %d)",Count,next_interupt,vaddr,page);
357 if(head->vaddr==vaddr&&head->reg32==0) {
358 //DebugMessage(M64MSG_VERBOSE, "TRACE: count=%d next=%d (get_addr match %x: %x)",Count,next_interupt,vaddr,(int)head->addr);
359 u_int *ht_bin=hash_table[((vaddr>>16)^vaddr)&0xFFFF];
362 ht_bin[1]=(int)head->addr;
368 head=jump_dirty[vpage];
370 if(head->vaddr==vaddr&&head->reg32==0) {
371 //DebugMessage(M64MSG_VERBOSE, "TRACE: count=%d next=%d (get_addr match dirty %x: %x)",Count,next_interupt,vaddr,(int)head->addr);
372 // Don't restore blocks which are about to expire from the cache
373 if((((u_int)head->addr-(u_int)out)<<(32-TARGET_SIZE_2))>0x60000000+(MAX_OUTPUT_BLOCK_SIZE<<(32-TARGET_SIZE_2)))
374 if(verify_dirty(head->addr)) {
375 //DebugMessage(M64MSG_VERBOSE, "restore candidate: %x (%d) d=%d",vaddr,page,invalid_code[vaddr>>12]);
376 invalid_code[vaddr>>12]=0;
377 memory_map[vaddr>>12]|=0x40000000;
379 if(tlb_LUT_r[vaddr>>12]) {
380 invalid_code[tlb_LUT_r[vaddr>>12]>>12]=0;
381 memory_map[tlb_LUT_r[vaddr>>12]>>12]|=0x40000000;
383 restore_candidate[vpage>>3]|=1<<(vpage&7);
385 else restore_candidate[page>>3]|=1<<(page&7);
386 u_int *ht_bin=hash_table[((vaddr>>16)^vaddr)&0xFFFF];
387 if(ht_bin[0]==vaddr) {
388 ht_bin[1]=(int)head->addr; // Replace existing entry
394 ht_bin[1]=(int)head->addr;
402 //DebugMessage(M64MSG_VERBOSE, "TRACE: count=%d next=%d (get_addr no-match %x)",Count,next_interupt,vaddr);
403 int r=new_recompile_block(vaddr);
404 if(r==0) return get_addr(vaddr);
405 // Execute in unmapped page, generate pagefault execption
407 Cause=(vaddr<<31)|0x8;
408 EPC=(vaddr&1)?vaddr-5:vaddr;
410 Context=(Context&0xFF80000F)|((BadVAddr>>9)&0x007FFFF0);
411 EntryHi=BadVAddr&0xFFFFE000;
412 return get_addr_ht(0x80000000);
414 // Look up address in hash table first
415 void *get_addr_ht(u_int vaddr)
417 //DebugMessage(M64MSG_VERBOSE, "TRACE: count=%d next=%d (get_addr_ht %x)",Count,next_interupt,vaddr);
418 u_int *ht_bin=hash_table[((vaddr>>16)^vaddr)&0xFFFF];
419 if(ht_bin[0]==vaddr) return (void *)ht_bin[1];
420 if(ht_bin[2]==vaddr) return (void *)ht_bin[3];
421 return get_addr(vaddr);
424 void *get_addr_32(u_int vaddr,u_int flags)
426 //DebugMessage(M64MSG_VERBOSE, "TRACE: count=%d next=%d (get_addr_32 %x,flags %x)",Count,next_interupt,vaddr,flags);
427 u_int *ht_bin=hash_table[((vaddr>>16)^vaddr)&0xFFFF];
428 if(ht_bin[0]==vaddr) return (void *)ht_bin[1];
429 if(ht_bin[2]==vaddr) return (void *)ht_bin[3];
430 u_int page=(vaddr^0x80000000)>>12;
432 if(page>262143&&tlb_LUT_r[vaddr>>12]) page=(tlb_LUT_r[vaddr>>12]^0x80000000)>>12;
433 if(page>2048) page=2048+(page&2047);
434 if(vpage>262143&&tlb_LUT_r[vaddr>>12]) vpage&=2047; // jump_dirty uses a hash of the virtual address instead
435 if(vpage>2048) vpage=2048+(vpage&2047);
436 struct ll_entry *head;
439 if(head->vaddr==vaddr&&(head->reg32&flags)==0) {
440 //DebugMessage(M64MSG_VERBOSE, "TRACE: count=%d next=%d (get_addr_32 match %x: %x)",Count,next_interupt,vaddr,(int)head->addr);
442 u_int *ht_bin=hash_table[((vaddr>>16)^vaddr)&0xFFFF];
444 ht_bin[1]=(int)head->addr;
446 }else if(ht_bin[2]==-1) {
447 ht_bin[3]=(int)head->addr;
450 //ht_bin[3]=ht_bin[1];
451 //ht_bin[2]=ht_bin[0];
452 //ht_bin[1]=(int)head->addr;
459 head=jump_dirty[vpage];
461 if(head->vaddr==vaddr&&(head->reg32&flags)==0) {
462 //DebugMessage(M64MSG_VERBOSE, "TRACE: count=%d next=%d (get_addr_32 match dirty %x: %x)",Count,next_interupt,vaddr,(int)head->addr);
463 // Don't restore blocks which are about to expire from the cache
464 if((((u_int)head->addr-(u_int)out)<<(32-TARGET_SIZE_2))>0x60000000+(MAX_OUTPUT_BLOCK_SIZE<<(32-TARGET_SIZE_2)))
465 if(verify_dirty(head->addr)) {
466 //DebugMessage(M64MSG_VERBOSE, "restore candidate: %x (%d) d=%d",vaddr,page,invalid_code[vaddr>>12]);
467 invalid_code[vaddr>>12]=0;
468 memory_map[vaddr>>12]|=0x40000000;
470 if(tlb_LUT_r[vaddr>>12]) {
471 invalid_code[tlb_LUT_r[vaddr>>12]>>12]=0;
472 memory_map[tlb_LUT_r[vaddr>>12]>>12]|=0x40000000;
474 restore_candidate[vpage>>3]|=1<<(vpage&7);
476 else restore_candidate[page>>3]|=1<<(page&7);
478 u_int *ht_bin=hash_table[((vaddr>>16)^vaddr)&0xFFFF];
480 ht_bin[1]=(int)head->addr;
482 }else if(ht_bin[2]==-1) {
483 ht_bin[3]=(int)head->addr;
486 //ht_bin[3]=ht_bin[1];
487 //ht_bin[2]=ht_bin[0];
488 //ht_bin[1]=(int)head->addr;
496 //DebugMessage(M64MSG_VERBOSE, "TRACE: count=%d next=%d (get_addr_32 no-match %x,flags %x)",Count,next_interupt,vaddr,flags);
497 int r=new_recompile_block(vaddr);
498 if(r==0) return get_addr(vaddr);
499 // Execute in unmapped page, generate pagefault execption
501 Cause=(vaddr<<31)|0x8;
502 EPC=(vaddr&1)?vaddr-5:vaddr;
504 Context=(Context&0xFF80000F)|((BadVAddr>>9)&0x007FFFF0);
505 EntryHi=BadVAddr&0xFFFFE000;
506 return get_addr_ht(0x80000000);
509 static void clear_all_regs(signed char regmap[])
512 for (hr=0;hr<HOST_REGS;hr++) regmap[hr]=-1;
515 static signed char get_reg(signed char regmap[],int r)
518 for (hr=0;hr<HOST_REGS;hr++) if(hr!=EXCLUDE_REG&®map[hr]==r) return hr;
522 // Find a register that is available for two consecutive cycles
523 static signed char get_reg2(signed char regmap1[],signed char regmap2[],int r)
526 for (hr=0;hr<HOST_REGS;hr++) if(hr!=EXCLUDE_REG&®map1[hr]==r&®map2[hr]==r) return hr;
530 static int count_free_regs(signed char regmap[])
534 for(hr=0;hr<HOST_REGS;hr++)
536 if(hr!=EXCLUDE_REG) {
537 if(regmap[hr]<0) count++;
543 static void dirty_reg(struct regstat *cur,signed char reg)
547 for (hr=0;hr<HOST_REGS;hr++) {
548 if((cur->regmap[hr]&63)==reg) {
554 // If we dirty the lower half of a 64 bit register which is now being
555 // sign-extended, we need to dump the upper half.
556 // Note: Do this only after completion of the instruction, because
557 // some instructions may need to read the full 64-bit value even if
558 // overwriting it (eg SLTI, DSRA32).
559 static void flush_dirty_uppers(struct regstat *cur)
562 for (hr=0;hr<HOST_REGS;hr++) {
563 if((cur->dirty>>hr)&1) {
566 if((cur->is32>>(reg&63))&1) cur->regmap[hr]=-1;
571 static void set_const(struct regstat *cur,signed char reg,uint64_t value)
575 for (hr=0;hr<HOST_REGS;hr++) {
576 if(cur->regmap[hr]==reg) {
578 cur->constmap[hr]=value;
580 else if((cur->regmap[hr]^64)==reg) {
582 cur->constmap[hr]=value>>32;
587 static void clear_const(struct regstat *cur,signed char reg)
591 for (hr=0;hr<HOST_REGS;hr++) {
592 if((cur->regmap[hr]&63)==reg) {
593 cur->isconst&=~(1<<hr);
598 static int is_const(struct regstat *cur,signed char reg)
603 for (hr=0;hr<HOST_REGS;hr++) {
604 if((cur->regmap[hr]&63)==reg) {
605 return (cur->isconst>>hr)&1;
610 static uint64_t get_const(struct regstat *cur,signed char reg)
614 for (hr=0;hr<HOST_REGS;hr++) {
615 if(cur->regmap[hr]==reg) {
616 return cur->constmap[hr];
619 DebugMessage(M64MSG_ERROR, "Unknown constant in r%d",reg);
623 // Least soon needed registers
624 // Look at the next ten instructions and see which registers
625 // will be used. Try not to reallocate these.
626 static void lsn(u_char hsn[], int i, int *preferred_reg)
636 if(itype[i+j]==UJUMP||itype[i+j]==RJUMP||(source[i+j]>>16)==0x1000)
638 // Don't go past an unconditonal jump
645 if(rs1[i+j]) hsn[rs1[i+j]]=j;
646 if(rs2[i+j]) hsn[rs2[i+j]]=j;
647 if(rt1[i+j]) hsn[rt1[i+j]]=j;
648 if(rt2[i+j]) hsn[rt2[i+j]]=j;
649 if(itype[i+j]==STORE || itype[i+j]==STORELR) {
650 // Stores can allocate zero
654 // On some architectures stores need invc_ptr
655 #if defined(HOST_IMM8)
656 if(itype[i+j]==STORE || itype[i+j]==STORELR || (opcode[i+j]&0x3b)==0x39) {
660 if(i+j>=0&&(itype[i+j]==UJUMP||itype[i+j]==CJUMP||itype[i+j]==SJUMP||itype[i+j]==FJUMP))
668 if(ba[i+b]>=start && ba[i+b]<(start+slen*4))
670 // Follow first branch
671 int t=(ba[i+b]-start)>>2;
672 j=7-b;if(t+j>=slen) j=slen-t-1;
675 if(rs1[t+j]) if(hsn[rs1[t+j]]>j+b+2) hsn[rs1[t+j]]=j+b+2;
676 if(rs2[t+j]) if(hsn[rs2[t+j]]>j+b+2) hsn[rs2[t+j]]=j+b+2;
677 //if(rt1[t+j]) if(hsn[rt1[t+j]]>j+b+2) hsn[rt1[t+j]]=j+b+2;
678 //if(rt2[t+j]) if(hsn[rt2[t+j]]>j+b+2) hsn[rt2[t+j]]=j+b+2;
681 // TODO: preferred register based on backward branch
683 // Delay slot should preferably not overwrite branch conditions or cycle count
684 if(i>0&&(itype[i-1]==RJUMP||itype[i-1]==UJUMP||itype[i-1]==CJUMP||itype[i-1]==SJUMP||itype[i-1]==FJUMP)) {
685 if(rs1[i-1]) if(hsn[rs1[i-1]]>1) hsn[rs1[i-1]]=1;
686 if(rs2[i-1]) if(hsn[rs2[i-1]]>1) hsn[rs2[i-1]]=1;
692 // Coprocessor load/store needs FTEMP, even if not declared
696 // Load L/R also uses FTEMP as a temporary register
697 if(itype[i]==LOADLR) {
700 // Also 64-bit SDL/SDR
701 if(opcode[i]==0x2c||opcode[i]==0x2d) {
704 // Don't remove the TLB registers either
705 if(itype[i]==LOAD || itype[i]==LOADLR || itype[i]==STORE || itype[i]==STORELR || itype[i]==C1LS ) {
708 // Don't remove the miniht registers
709 if(itype[i]==UJUMP||itype[i]==RJUMP)
716 // We only want to allocate registers if we're going to use them again soon
717 static int needed_again(int r, int i)
723 if(i>0&&(itype[i-1]==UJUMP||itype[i-1]==RJUMP||(source[i-1]>>16)==0x1000))
725 if(ba[i-1]<start || ba[i-1]>start+slen*4-4)
726 return 0; // Don't need any registers if exiting the block
734 if(itype[i+j]==UJUMP||itype[i+j]==RJUMP||(source[i+j]>>16)==0x1000)
736 // Don't go past an unconditonal jump
740 if(itype[i+j]==SYSCALL||((source[i+j]&0xfc00003f)==0x0d))
747 if(rs1[i+j]==r) rn=j;
748 if(rs2[i+j]==r) rn=j;
749 if((unneeded_reg[i+j]>>r)&1) rn=10;
750 if(i+j>=0&&(itype[i+j]==UJUMP||itype[i+j]==CJUMP||itype[i+j]==SJUMP||itype[i+j]==FJUMP))
758 if(ba[i+b]>=start && ba[i+b]<(start+slen*4))
760 // Follow first branch
762 int t=(ba[i+b]-start)>>2;
763 j=7-b;if(t+j>=slen) j=slen-t-1;
766 if(!((unneeded_reg[t+j]>>r)&1)) {
767 if(rs1[t+j]==r) if(rn>j+b+2) rn=j+b+2;
768 if(rs2[t+j]==r) if(rn>j+b+2) rn=j+b+2;
778 // Try to match register allocations at the end of a loop with those
780 static int loop_reg(int i, int r, int hr)
789 if(itype[i+j]==UJUMP||itype[i+j]==RJUMP||(source[i+j]>>16)==0x1000)
791 // Don't go past an unconditonal jump
798 if(itype[i-1]==UJUMP||itype[i-1]==CJUMP||itype[i-1]==SJUMP||itype[i-1]==FJUMP)
803 if(r<64&&((unneeded_reg[i+k]>>r)&1)) return hr;
804 if(r>64&&((unneeded_reg_upper[i+k]>>r)&1)) return hr;
805 if(i+k>=0&&(itype[i+k]==UJUMP||itype[i+k]==CJUMP||itype[i+k]==SJUMP||itype[i+k]==FJUMP))
807 if(ba[i+k]>=start && ba[i+k]<(start+i*4))
809 int t=(ba[i+k]-start)>>2;
810 int reg=get_reg(regs[t].regmap_entry,r);
811 if(reg>=0) return reg;
812 //reg=get_reg(regs[t+1].regmap_entry,r);
813 //if(reg>=0) return reg;
821 // Allocate every register, preserving source/target regs
822 static void alloc_all(struct regstat *cur,int i)
826 for(hr=0;hr<HOST_REGS;hr++) {
827 if(hr!=EXCLUDE_REG) {
828 if(((cur->regmap[hr]&63)!=rs1[i])&&((cur->regmap[hr]&63)!=rs2[i])&&
829 ((cur->regmap[hr]&63)!=rt1[i])&&((cur->regmap[hr]&63)!=rt2[i]))
832 cur->dirty&=~(1<<hr);
835 if((cur->regmap[hr]&63)==0)
838 cur->dirty&=~(1<<hr);
845 static void div64(int64_t dividend,int64_t divisor)
849 //DebugMessage(M64MSG_VERBOSE, "TRACE: ddiv %8x%8x %8x%8x" ,(int)reg[HIREG],(int)(reg[HIREG]>>32)
850 // ,(int)reg[LOREG],(int)(reg[LOREG]>>32));
852 static void divu64(uint64_t dividend,uint64_t divisor)
856 //DebugMessage(M64MSG_VERBOSE, "TRACE: ddivu %8x%8x %8x%8x",(int)reg[HIREG],(int)(reg[HIREG]>>32)
857 // ,(int)reg[LOREG],(int)(reg[LOREG]>>32));
860 static void mult64(uint64_t m1,uint64_t m2)
862 unsigned long long int op1, op2, op3, op4;
863 unsigned long long int result1, result2, result3, result4;
864 unsigned long long int temp1, temp2, temp3, temp4;
880 op1 = op2 & 0xFFFFFFFF;
881 op2 = (op2 >> 32) & 0xFFFFFFFF;
882 op3 = op4 & 0xFFFFFFFF;
883 op4 = (op4 >> 32) & 0xFFFFFFFF;
886 temp2 = (temp1 >> 32) + op1 * op4;
888 temp4 = (temp3 >> 32) + op2 * op4;
890 result1 = temp1 & 0xFFFFFFFF;
891 result2 = temp2 + (temp3 & 0xFFFFFFFF);
892 result3 = (result2 >> 32) + temp4;
893 result4 = (result3 >> 32);
895 lo = result1 | (result2 << 32);
896 hi = (result3 & 0xFFFFFFFF) | (result4 << 32);
905 #if NEW_DYNAREC == NEW_DYNAREC_ARM
906 static void multu64(uint64_t m1,uint64_t m2)
908 unsigned long long int op1, op2, op3, op4;
909 unsigned long long int result1, result2, result3, result4;
910 unsigned long long int temp1, temp2, temp3, temp4;
912 op1 = m1 & 0xFFFFFFFF;
913 op2 = (m1 >> 32) & 0xFFFFFFFF;
914 op3 = m2 & 0xFFFFFFFF;
915 op4 = (m2 >> 32) & 0xFFFFFFFF;
918 temp2 = (temp1 >> 32) + op1 * op4;
920 temp4 = (temp3 >> 32) + op2 * op4;
922 result1 = temp1 & 0xFFFFFFFF;
923 result2 = temp2 + (temp3 & 0xFFFFFFFF);
924 result3 = (result2 >> 32) + temp4;
925 result4 = (result3 >> 32);
927 lo = result1 | (result2 << 32);
928 hi = (result3 & 0xFFFFFFFF) | (result4 << 32);
930 //DebugMessage(M64MSG_VERBOSE, "TRACE: dmultu %8x%8x %8x%8x",(int)reg[HIREG],(int)(reg[HIREG]>>32)
931 // ,(int)reg[LOREG],(int)(reg[LOREG]>>32));
935 static uint64_t ldl_merge(uint64_t original,uint64_t loaded,u_int bits)
943 else original=loaded;
946 static uint64_t ldr_merge(uint64_t original,uint64_t loaded,u_int bits)
949 original>>=64-(bits^56);
950 original<<=64-(bits^56);
954 else original=loaded;
958 #if NEW_DYNAREC == NEW_DYNAREC_X86
959 #include "assem_x86.c"
960 #elif NEW_DYNAREC == NEW_DYNAREC_ARM
961 #include "assem_arm.c"
963 #error Unsupported dynarec architecture
966 // Add virtual address mapping to linked list
967 static void ll_add(struct ll_entry **head,int vaddr,void *addr)
969 struct ll_entry *new_entry;
970 new_entry=malloc(sizeof(struct ll_entry));
971 assert(new_entry!=NULL);
972 new_entry->vaddr=vaddr;
974 new_entry->addr=addr;
975 new_entry->next=*head;
979 // Add virtual address mapping for 32-bit compiled block
980 static void ll_add_32(struct ll_entry **head,int vaddr,u_int reg32,void *addr)
982 struct ll_entry *new_entry;
983 new_entry=malloc(sizeof(struct ll_entry));
984 assert(new_entry!=NULL);
985 new_entry->vaddr=vaddr;
986 new_entry->reg32=reg32;
987 new_entry->addr=addr;
988 new_entry->next=*head;
992 // Check if an address is already compiled
993 // but don't return addresses which are about to expire from the cache
994 static void *check_addr(u_int vaddr)
996 u_int *ht_bin=hash_table[((vaddr>>16)^vaddr)&0xFFFF];
997 if(ht_bin[0]==vaddr) {
998 if(((ht_bin[1]-MAX_OUTPUT_BLOCK_SIZE-(u_int)out)<<(32-TARGET_SIZE_2))>0x60000000+(MAX_OUTPUT_BLOCK_SIZE<<(32-TARGET_SIZE_2)))
999 if(isclean(ht_bin[1])) return (void *)ht_bin[1];
1001 if(ht_bin[2]==vaddr) {
1002 if(((ht_bin[3]-MAX_OUTPUT_BLOCK_SIZE-(u_int)out)<<(32-TARGET_SIZE_2))>0x60000000+(MAX_OUTPUT_BLOCK_SIZE<<(32-TARGET_SIZE_2)))
1003 if(isclean(ht_bin[3])) return (void *)ht_bin[3];
1005 u_int page=(vaddr^0x80000000)>>12;
1006 if(page>262143&&tlb_LUT_r[vaddr>>12]) page=(tlb_LUT_r[vaddr>>12]^0x80000000)>>12;
1007 if(page>2048) page=2048+(page&2047);
1008 struct ll_entry *head;
1011 if(head->vaddr==vaddr&&head->reg32==0) {
1012 if((((u_int)head->addr-(u_int)out)<<(32-TARGET_SIZE_2))>0x60000000+(MAX_OUTPUT_BLOCK_SIZE<<(32-TARGET_SIZE_2))) {
1013 // Update existing entry with current address
1014 if(ht_bin[0]==vaddr) {
1015 ht_bin[1]=(int)head->addr;
1018 if(ht_bin[2]==vaddr) {
1019 ht_bin[3]=(int)head->addr;
1022 // Insert into hash table with low priority.
1023 // Don't evict existing entries, as they are probably
1024 // addresses that are being accessed frequently.
1026 ht_bin[1]=(int)head->addr;
1028 }else if(ht_bin[2]==-1) {
1029 ht_bin[3]=(int)head->addr;
1040 static void remove_hash(int vaddr)
1042 //DebugMessage(M64MSG_VERBOSE, "remove hash: %x",vaddr);
1043 u_int *ht_bin=hash_table[(((vaddr)>>16)^vaddr)&0xFFFF];
1044 if(ht_bin[2]==vaddr) {
1045 ht_bin[2]=ht_bin[3]=-1;
1047 if(ht_bin[0]==vaddr) {
1048 ht_bin[0]=ht_bin[2];
1049 ht_bin[1]=ht_bin[3];
1050 ht_bin[2]=ht_bin[3]=-1;
1054 static void ll_remove_matching_addrs(struct ll_entry **head,int addr,int shift)
1056 struct ll_entry *next;
1058 if(((u_int)((*head)->addr)>>shift)==(addr>>shift) ||
1059 ((u_int)((*head)->addr-MAX_OUTPUT_BLOCK_SIZE)>>shift)==(addr>>shift))
1061 inv_debug("EXP: Remove pointer to %x (%x)\n",(int)(*head)->addr,(*head)->vaddr);
1062 remove_hash((*head)->vaddr);
1069 head=&((*head)->next);
1074 // Remove all entries from linked list
1075 static void ll_clear(struct ll_entry **head)
1077 struct ll_entry *cur;
1078 struct ll_entry *next;
1089 // Dereference the pointers and remove if it matches
1090 static void ll_kill_pointers(struct ll_entry *head,int addr,int shift)
1093 int ptr=get_pointer(head->addr);
1094 inv_debug("EXP: Lookup pointer to %x at %x (%x)\n",(int)ptr,(int)head->addr,head->vaddr);
1095 if(((ptr>>shift)==(addr>>shift)) ||
1096 (((ptr-MAX_OUTPUT_BLOCK_SIZE)>>shift)==(addr>>shift)))
1098 inv_debug("EXP: Kill pointer at %x (%x)\n",(int)head->addr,head->vaddr);
1099 u_int host_addr=(int)kill_pointer(head->addr);
1100 #if NEW_DYNAREC == NEW_DYNAREC_ARM
1101 needs_clear_cache[(host_addr-(u_int)base_addr)>>17]|=1<<(((host_addr-(u_int)base_addr)>>12)&31);
1108 // This is called when we write to a compiled block (see do_invstub)
1109 static void invalidate_page(u_int page)
1111 struct ll_entry *head;
1112 struct ll_entry *next;
1116 inv_debug("INVALIDATE: %x\n",head->vaddr);
1117 remove_hash(head->vaddr);
1122 head=jump_out[page];
1125 inv_debug("INVALIDATE: kill pointer to %x (%x)\n",head->vaddr,(int)head->addr);
1126 u_int host_addr=(int)kill_pointer(head->addr);
1127 #if NEW_DYNAREC == NEW_DYNAREC_ARM
1128 needs_clear_cache[(host_addr-(u_int)base_addr)>>17]|=1<<(((host_addr-(u_int)base_addr)>>12)&31);
1135 void invalidate_block(u_int block)
1138 page=vpage=block^0x80000;
1139 if(page>262143&&tlb_LUT_r[block]) page=(tlb_LUT_r[block]^0x80000000)>>12;
1140 if(page>2048) page=2048+(page&2047);
1141 if(vpage>262143&&tlb_LUT_r[block]) vpage&=2047; // jump_dirty uses a hash of the virtual address instead
1142 if(vpage>2048) vpage=2048+(vpage&2047);
1143 inv_debug("INVALIDATE: %x (%d)\n",block<<12,page);
1144 //inv_debug("invalid_code[block]=%d\n",invalid_code[block]);
1147 struct ll_entry *head;
1148 head=jump_dirty[vpage];
1149 //DebugMessage(M64MSG_VERBOSE, "page=%d vpage=%d",page,vpage);
1152 if(vpage>2047||(head->vaddr>>12)==block) { // Ignore vaddr hash collision
1153 get_bounds((int)head->addr,&start,&end);
1154 //DebugMessage(M64MSG_VERBOSE, "start: %x end: %x",start,end);
1155 if(page<2048&&start>=0x80000000&&end<0x80800000) {
1156 if(((start-(u_int)rdram)>>12)<=page&&((end-1-(u_int)rdram)>>12)>=page) {
1157 if((((start-(u_int)rdram)>>12)&2047)<first) first=((start-(u_int)rdram)>>12)&2047;
1158 if((((end-1-(u_int)rdram)>>12)&2047)>last) last=((end-1-(u_int)rdram)>>12)&2047;
1161 if(page<2048&&(signed int)start>=(signed int)0xC0000000&&(signed int)end>=(signed int)0xC0000000) {
1162 if(((start+memory_map[start>>12]-(u_int)rdram)>>12)<=page&&((end-1+memory_map[(end-1)>>12]-(u_int)rdram)>>12)>=page) {
1163 if((((start+memory_map[start>>12]-(u_int)rdram)>>12)&2047)<first) first=((start+memory_map[start>>12]-(u_int)rdram)>>12)&2047;
1164 if((((end-1+memory_map[(end-1)>>12]-(u_int)rdram)>>12)&2047)>last) last=((end-1+memory_map[(end-1)>>12]-(u_int)rdram)>>12)&2047;
1170 //DebugMessage(M64MSG_VERBOSE, "first=%d last=%d",first,last);
1171 invalidate_page(page);
1172 assert(first+5>page); // NB: this assumes MAXBLOCK<=4096 (4 pages)
1173 assert(last<page+5);
1174 // Invalidate the adjacent pages if a block crosses a 4K boundary
1176 invalidate_page(first);
1179 for(first=page+1;first<last;first++) {
1180 invalidate_page(first);
1182 #if NEW_DYNAREC == NEW_DYNAREC_ARM
1186 // Don't trap writes
1187 invalid_code[block]=1;
1188 // If there is a valid TLB entry for this page, remove write protect
1189 if(tlb_LUT_w[block]) {
1190 assert(tlb_LUT_r[block]==tlb_LUT_w[block]);
1191 // CHECK: Is this right?
1192 memory_map[block]=((tlb_LUT_w[block]&0xFFFFF000)-(block<<12)+(unsigned int)rdram-0x80000000)>>2;
1193 u_int real_block=tlb_LUT_w[block]>>12;
1194 invalid_code[real_block]=1;
1195 if(real_block>=0x80000&&real_block<0x80800) memory_map[real_block]=((u_int)rdram-0x80000000)>>2;
1197 else if(block>=0x80000&&block<0x80800) memory_map[block]=((u_int)rdram-0x80000000)>>2;
1199 memset(mini_ht,-1,sizeof(mini_ht));
1203 #if NEW_DYNAREC == NEW_DYNAREC_ARM
1204 static void invalidate_addr(u_int addr)
1206 invalidate_block(addr>>12);
1210 // This is called when loading a save state.
1211 // Anything could have changed, so invalidate everything.
1212 void invalidate_all_pages()
1215 for(page=0;page<4096;page++)
1216 invalidate_page(page);
1217 for(page=0;page<1048576;page++)
1218 if(!invalid_code[page]) {
1219 restore_candidate[(page&2047)>>3]|=1<<(page&7);
1220 restore_candidate[((page&2047)>>3)+256]|=1<<(page&7);
1222 #if NEW_DYNAREC == NEW_DYNAREC_ARM
1223 __clear_cache((void *)base_addr,(void *)base_addr+(1<<TARGET_SIZE_2));
1224 //cacheflush((void *)base_addr,(void *)base_addr+(1<<TARGET_SIZE_2),0);
1227 memset(mini_ht,-1,sizeof(mini_ht));
1230 for(page=0;page<0x100000;page++) {
1231 if(tlb_LUT_r[page]) {
1232 memory_map[page]=((tlb_LUT_r[page]&0xFFFFF000)-(page<<12)+(unsigned int)rdram-0x80000000)>>2;
1233 if(!tlb_LUT_w[page]||!invalid_code[page])
1234 memory_map[page]|=0x40000000; // Write protect
1236 else memory_map[page]=-1;
1237 if(page==0x80000) page=0xC0000;
1242 // Add an entry to jump_out after making a link
1243 void add_link(u_int vaddr,void *src)
1245 u_int page=(vaddr^0x80000000)>>12;
1246 if(page>262143&&tlb_LUT_r[vaddr>>12]) page=(tlb_LUT_r[vaddr>>12]^0x80000000)>>12;
1247 if(page>4095) page=2048+(page&2047);
1248 inv_debug("add_link: %x -> %x (%d)\n",(int)src,vaddr,page);
1249 ll_add(jump_out+page,vaddr,src);
1250 //int ptr=get_pointer(src);
1251 //inv_debug("add_link: Pointer is to %x\n",(int)ptr);
1254 // If a code block was found to be unmodified (bit was set in
1255 // restore_candidate) and it remains unmodified (bit is clear
1256 // in invalid_code) then move the entries for that 4K page from
1257 // the dirty list to the clean list.
1258 void clean_blocks(u_int page)
1260 struct ll_entry *head;
1261 inv_debug("INV: clean_blocks page=%d\n",page);
1262 head=jump_dirty[page];
1264 if(!invalid_code[head->vaddr>>12]) {
1265 // Don't restore blocks which are about to expire from the cache
1266 if((((u_int)head->addr-(u_int)out)<<(32-TARGET_SIZE_2))>0x60000000+(MAX_OUTPUT_BLOCK_SIZE<<(32-TARGET_SIZE_2))) {
1268 if(verify_dirty(head->addr)) {
1269 //DebugMessage(M64MSG_VERBOSE, "Possibly Restore %x (%x)",head->vaddr, (int)head->addr);
1272 get_bounds((int)head->addr,&start,&end);
1273 if(start-(u_int)rdram<0x800000) {
1274 for(i=(start-(u_int)rdram+0x80000000)>>12;i<=(end-1-(u_int)rdram+0x80000000)>>12;i++) {
1275 inv|=invalid_code[i];
1278 if((signed int)head->vaddr>=(signed int)0xC0000000) {
1279 u_int addr = (head->vaddr+(memory_map[head->vaddr>>12]<<2));
1280 //DebugMessage(M64MSG_VERBOSE, "addr=%x start=%x end=%x",addr,start,end);
1281 if(addr<start||addr>=end) inv=1;
1283 else if((signed int)head->vaddr>=(signed int)0x80800000) {
1287 void * clean_addr=(void *)get_clean_addr((int)head->addr);
1288 if((((u_int)clean_addr-(u_int)out)<<(32-TARGET_SIZE_2))>0x60000000+(MAX_OUTPUT_BLOCK_SIZE<<(32-TARGET_SIZE_2))) {
1290 if(page<2048&&tlb_LUT_r[head->vaddr>>12]) ppage=(tlb_LUT_r[head->vaddr>>12]^0x80000000)>>12;
1291 inv_debug("INV: Restored %x (%x/%x)\n",head->vaddr, (int)head->addr, (int)clean_addr);
1292 //DebugMessage(M64MSG_VERBOSE, "page=%x, addr=%x",page,head->vaddr);
1293 //assert(head->vaddr>>12==(page|0x80000));
1294 ll_add_32(jump_in+ppage,head->vaddr,head->reg32,clean_addr);
1295 u_int *ht_bin=hash_table[((head->vaddr>>16)^head->vaddr)&0xFFFF];
1297 if(ht_bin[0]==head->vaddr) {
1298 ht_bin[1]=(int)clean_addr; // Replace existing entry
1300 if(ht_bin[2]==head->vaddr) {
1301 ht_bin[3]=(int)clean_addr; // Replace existing entry
1314 static void mov_alloc(struct regstat *current,int i)
1316 // Note: Don't need to actually alloc the source registers
1317 if((~current->is32>>rs1[i])&1) {
1318 //alloc_reg64(current,i,rs1[i]);
1319 alloc_reg64(current,i,rt1[i]);
1320 current->is32&=~(1LL<<rt1[i]);
1322 //alloc_reg(current,i,rs1[i]);
1323 alloc_reg(current,i,rt1[i]);
1324 current->is32|=(1LL<<rt1[i]);
1326 clear_const(current,rs1[i]);
1327 clear_const(current,rt1[i]);
1328 dirty_reg(current,rt1[i]);
1331 static void shiftimm_alloc(struct regstat *current,int i)
1333 clear_const(current,rs1[i]);
1334 clear_const(current,rt1[i]);
1335 if(opcode2[i]<=0x3) // SLL/SRL/SRA
1338 if(rs1[i]&&needed_again(rs1[i],i)) alloc_reg(current,i,rs1[i]);
1340 alloc_reg(current,i,rt1[i]);
1341 current->is32|=1LL<<rt1[i];
1342 dirty_reg(current,rt1[i]);
1345 if(opcode2[i]>=0x38&&opcode2[i]<=0x3b) // DSLL/DSRL/DSRA
1348 if(rs1[i]) alloc_reg64(current,i,rs1[i]);
1349 alloc_reg64(current,i,rt1[i]);
1350 current->is32&=~(1LL<<rt1[i]);
1351 dirty_reg(current,rt1[i]);
1354 if(opcode2[i]==0x3c) // DSLL32
1357 if(rs1[i]) alloc_reg(current,i,rs1[i]);
1358 alloc_reg64(current,i,rt1[i]);
1359 current->is32&=~(1LL<<rt1[i]);
1360 dirty_reg(current,rt1[i]);
1363 if(opcode2[i]==0x3e) // DSRL32
1366 alloc_reg64(current,i,rs1[i]);
1368 alloc_reg64(current,i,rt1[i]);
1369 current->is32&=~(1LL<<rt1[i]);
1371 alloc_reg(current,i,rt1[i]);
1372 current->is32|=1LL<<rt1[i];
1374 dirty_reg(current,rt1[i]);
1377 if(opcode2[i]==0x3f) // DSRA32
1380 alloc_reg64(current,i,rs1[i]);
1381 alloc_reg(current,i,rt1[i]);
1382 current->is32|=1LL<<rt1[i];
1383 dirty_reg(current,rt1[i]);
1388 static void shift_alloc(struct regstat *current,int i)
1391 if(opcode2[i]<=0x07) // SLLV/SRLV/SRAV
1393 if(rs1[i]) alloc_reg(current,i,rs1[i]);
1394 if(rs2[i]) alloc_reg(current,i,rs2[i]);
1395 alloc_reg(current,i,rt1[i]);
1396 if(rt1[i]==rs2[i]) {
1397 alloc_reg_temp(current,i,-1);
1398 minimum_free_regs[i]=1;
1400 current->is32|=1LL<<rt1[i];
1401 } else { // DSLLV/DSRLV/DSRAV
1402 if(rs1[i]) alloc_reg64(current,i,rs1[i]);
1403 if(rs2[i]) alloc_reg(current,i,rs2[i]);
1404 alloc_reg64(current,i,rt1[i]);
1405 current->is32&=~(1LL<<rt1[i]);
1406 if(opcode2[i]==0x16||opcode2[i]==0x17) // DSRLV and DSRAV need a temporary register
1408 alloc_reg_temp(current,i,-1);
1409 minimum_free_regs[i]=1;
1412 clear_const(current,rs1[i]);
1413 clear_const(current,rs2[i]);
1414 clear_const(current,rt1[i]);
1415 dirty_reg(current,rt1[i]);
1419 static void alu_alloc(struct regstat *current,int i)
1421 if(opcode2[i]>=0x20&&opcode2[i]<=0x23) { // ADD/ADDU/SUB/SUBU
1423 if(rs1[i]&&rs2[i]) {
1424 alloc_reg(current,i,rs1[i]);
1425 alloc_reg(current,i,rs2[i]);
1428 if(rs1[i]&&needed_again(rs1[i],i)) alloc_reg(current,i,rs1[i]);
1429 if(rs2[i]&&needed_again(rs2[i],i)) alloc_reg(current,i,rs2[i]);
1431 alloc_reg(current,i,rt1[i]);
1433 current->is32|=1LL<<rt1[i];
1435 if(opcode2[i]==0x2a||opcode2[i]==0x2b) { // SLT/SLTU
1437 if(!((current->is32>>rs1[i])&(current->is32>>rs2[i])&1))
1439 alloc_reg64(current,i,rs1[i]);
1440 alloc_reg64(current,i,rs2[i]);
1441 alloc_reg(current,i,rt1[i]);
1443 alloc_reg(current,i,rs1[i]);
1444 alloc_reg(current,i,rs2[i]);
1445 alloc_reg(current,i,rt1[i]);
1448 current->is32|=1LL<<rt1[i];
1450 if(opcode2[i]>=0x24&&opcode2[i]<=0x27) { // AND/OR/XOR/NOR
1452 if(rs1[i]&&rs2[i]) {
1453 alloc_reg(current,i,rs1[i]);
1454 alloc_reg(current,i,rs2[i]);
1458 if(rs1[i]&&needed_again(rs1[i],i)) alloc_reg(current,i,rs1[i]);
1459 if(rs2[i]&&needed_again(rs2[i],i)) alloc_reg(current,i,rs2[i]);
1461 alloc_reg(current,i,rt1[i]);
1462 if(!((current->is32>>rs1[i])&(current->is32>>rs2[i])&1))
1464 if(!((current->uu>>rt1[i])&1)) {
1465 alloc_reg64(current,i,rt1[i]);
1467 if(get_reg(current->regmap,rt1[i]|64)>=0) {
1468 if(rs1[i]&&rs2[i]) {
1469 alloc_reg64(current,i,rs1[i]);
1470 alloc_reg64(current,i,rs2[i]);
1474 // Is is really worth it to keep 64-bit values in registers?
1476 if(rs1[i]&&needed_again(rs1[i],i)) alloc_reg64(current,i,rs1[i]);
1477 if(rs2[i]&&needed_again(rs2[i],i)) alloc_reg64(current,i,rs2[i]);
1481 current->is32&=~(1LL<<rt1[i]);
1483 current->is32|=1LL<<rt1[i];
1487 if(opcode2[i]>=0x2c&&opcode2[i]<=0x2f) { // DADD/DADDU/DSUB/DSUBU
1489 if(rs1[i]&&rs2[i]) {
1490 if(!((current->uu>>rt1[i])&1)||get_reg(current->regmap,rt1[i]|64)>=0) {
1491 alloc_reg64(current,i,rs1[i]);
1492 alloc_reg64(current,i,rs2[i]);
1493 alloc_reg64(current,i,rt1[i]);
1495 alloc_reg(current,i,rs1[i]);
1496 alloc_reg(current,i,rs2[i]);
1497 alloc_reg(current,i,rt1[i]);
1501 alloc_reg(current,i,rt1[i]);
1502 if(!((current->uu>>rt1[i])&1)||get_reg(current->regmap,rt1[i]|64)>=0) {
1503 // DADD used as move, or zeroing
1504 // If we have a 64-bit source, then make the target 64 bits too
1505 if(rs1[i]&&!((current->is32>>rs1[i])&1)) {
1506 if(get_reg(current->regmap,rs1[i])>=0) alloc_reg64(current,i,rs1[i]);
1507 alloc_reg64(current,i,rt1[i]);
1508 } else if(rs2[i]&&!((current->is32>>rs2[i])&1)) {
1509 if(get_reg(current->regmap,rs2[i])>=0) alloc_reg64(current,i,rs2[i]);
1510 alloc_reg64(current,i,rt1[i]);
1512 if(opcode2[i]>=0x2e&&rs2[i]) {
1513 // DSUB used as negation - 64-bit result
1514 // If we have a 32-bit register, extend it to 64 bits
1515 if(get_reg(current->regmap,rs2[i])>=0) alloc_reg64(current,i,rs2[i]);
1516 alloc_reg64(current,i,rt1[i]);
1520 if(rs1[i]&&rs2[i]) {
1521 current->is32&=~(1LL<<rt1[i]);
1523 current->is32&=~(1LL<<rt1[i]);
1524 if((current->is32>>rs1[i])&1)
1525 current->is32|=1LL<<rt1[i];
1527 current->is32&=~(1LL<<rt1[i]);
1528 if((current->is32>>rs2[i])&1)
1529 current->is32|=1LL<<rt1[i];
1531 current->is32|=1LL<<rt1[i];
1535 clear_const(current,rs1[i]);
1536 clear_const(current,rs2[i]);
1537 clear_const(current,rt1[i]);
1538 dirty_reg(current,rt1[i]);
1541 static void imm16_alloc(struct regstat *current,int i)
1543 if(rs1[i]&&needed_again(rs1[i],i)) alloc_reg(current,i,rs1[i]);
1545 if(rt1[i]) alloc_reg(current,i,rt1[i]);
1546 if(opcode[i]==0x18||opcode[i]==0x19) { // DADDI/DADDIU
1547 current->is32&=~(1LL<<rt1[i]);
1548 if(!((current->uu>>rt1[i])&1)||get_reg(current->regmap,rt1[i]|64)>=0) {
1549 // TODO: Could preserve the 32-bit flag if the immediate is zero
1550 alloc_reg64(current,i,rt1[i]);
1551 alloc_reg64(current,i,rs1[i]);
1553 clear_const(current,rs1[i]);
1554 clear_const(current,rt1[i]);
1556 else if(opcode[i]==0x0a||opcode[i]==0x0b) { // SLTI/SLTIU
1557 if((~current->is32>>rs1[i])&1) alloc_reg64(current,i,rs1[i]);
1558 current->is32|=1LL<<rt1[i];
1559 clear_const(current,rs1[i]);
1560 clear_const(current,rt1[i]);
1562 else if(opcode[i]>=0x0c&&opcode[i]<=0x0e) { // ANDI/ORI/XORI
1563 if(((~current->is32>>rs1[i])&1)&&opcode[i]>0x0c) {
1564 if(rs1[i]!=rt1[i]) {
1565 if(needed_again(rs1[i],i)) alloc_reg64(current,i,rs1[i]);
1566 alloc_reg64(current,i,rt1[i]);
1567 current->is32&=~(1LL<<rt1[i]);
1570 else current->is32|=1LL<<rt1[i]; // ANDI clears upper bits
1571 if(is_const(current,rs1[i])) {
1572 int v=get_const(current,rs1[i]);
1573 if(opcode[i]==0x0c) set_const(current,rt1[i],v&imm[i]);
1574 if(opcode[i]==0x0d) set_const(current,rt1[i],v|imm[i]);
1575 if(opcode[i]==0x0e) set_const(current,rt1[i],v^imm[i]);
1577 else clear_const(current,rt1[i]);
1579 else if(opcode[i]==0x08||opcode[i]==0x09) { // ADDI/ADDIU
1580 if(is_const(current,rs1[i])) {
1581 int v=get_const(current,rs1[i]);
1582 set_const(current,rt1[i],v+imm[i]);
1584 else clear_const(current,rt1[i]);
1585 current->is32|=1LL<<rt1[i];
1588 set_const(current,rt1[i],((long long)((short)imm[i]))<<16); // LUI
1589 current->is32|=1LL<<rt1[i];
1591 dirty_reg(current,rt1[i]);
1594 static void load_alloc(struct regstat *current,int i)
1596 clear_const(current,rt1[i]);
1597 //if(rs1[i]!=rt1[i]&&needed_again(rs1[i],i)) clear_const(current,rs1[i]); // Does this help or hurt?
1598 if(!rs1[i]) current->u&=~1LL; // Allow allocating r0 if it's the source register
1599 if(needed_again(rs1[i],i)) alloc_reg(current,i,rs1[i]);
1600 if(rt1[i]&&!((current->u>>rt1[i])&1)) {
1601 alloc_reg(current,i,rt1[i]);
1602 assert(get_reg(current->regmap,rt1[i])>=0);
1603 if(opcode[i]==0x27||opcode[i]==0x37) // LWU/LD
1605 current->is32&=~(1LL<<rt1[i]);
1606 alloc_reg64(current,i,rt1[i]);
1608 else if(opcode[i]==0x1A||opcode[i]==0x1B) // LDL/LDR
1610 current->is32&=~(1LL<<rt1[i]);
1611 alloc_reg64(current,i,rt1[i]);
1612 alloc_all(current,i);
1613 alloc_reg64(current,i,FTEMP);
1614 minimum_free_regs[i]=HOST_REGS;
1616 else current->is32|=1LL<<rt1[i];
1617 dirty_reg(current,rt1[i]);
1618 // If using TLB, need a register for pointer to the mapping table
1619 if(using_tlb) alloc_reg(current,i,TLREG);
1620 // LWL/LWR need a temporary register for the old value
1621 if(opcode[i]==0x22||opcode[i]==0x26)
1623 alloc_reg(current,i,FTEMP);
1624 alloc_reg_temp(current,i,-1);
1625 minimum_free_regs[i]=1;
1630 // Load to r0 or unneeded register (dummy load)
1631 // but we still need a register to calculate the address
1632 if(opcode[i]==0x22||opcode[i]==0x26)
1634 alloc_reg(current,i,FTEMP); // LWL/LWR need another temporary
1636 // If using TLB, need a register for pointer to the mapping table
1637 if(using_tlb) alloc_reg(current,i,TLREG);
1638 alloc_reg_temp(current,i,-1);
1639 minimum_free_regs[i]=1;
1640 if(opcode[i]==0x1A||opcode[i]==0x1B) // LDL/LDR
1642 alloc_all(current,i);
1643 alloc_reg64(current,i,FTEMP);
1644 minimum_free_regs[i]=HOST_REGS;
1649 static void store_alloc(struct regstat *current,int i)
1651 clear_const(current,rs2[i]);
1652 if(!(rs2[i])) current->u&=~1LL; // Allow allocating r0 if necessary
1653 if(needed_again(rs1[i],i)) alloc_reg(current,i,rs1[i]);
1654 alloc_reg(current,i,rs2[i]);
1655 if(opcode[i]==0x2c||opcode[i]==0x2d||opcode[i]==0x3f) { // 64-bit SDL/SDR/SD
1656 alloc_reg64(current,i,rs2[i]);
1657 if(rs2[i]) alloc_reg(current,i,FTEMP);
1659 // If using TLB, need a register for pointer to the mapping table
1660 if(using_tlb) alloc_reg(current,i,TLREG);
1661 #if defined(HOST_IMM8)
1662 // On CPUs without 32-bit immediates we need a pointer to invalid_code
1663 else alloc_reg(current,i,INVCP);
1665 if(opcode[i]==0x2c||opcode[i]==0x2d) { // 64-bit SDL/SDR
1666 alloc_reg(current,i,FTEMP);
1668 // We need a temporary register for address generation
1669 alloc_reg_temp(current,i,-1);
1670 minimum_free_regs[i]=1;
1673 static void c1ls_alloc(struct regstat *current,int i)
1675 //clear_const(current,rs1[i]); // FIXME
1676 clear_const(current,rt1[i]);
1677 if(needed_again(rs1[i],i)) alloc_reg(current,i,rs1[i]);
1678 alloc_reg(current,i,CSREG); // Status
1679 alloc_reg(current,i,FTEMP);
1680 if(opcode[i]==0x35||opcode[i]==0x3d) { // 64-bit LDC1/SDC1
1681 alloc_reg64(current,i,FTEMP);
1683 // If using TLB, need a register for pointer to the mapping table
1684 if(using_tlb) alloc_reg(current,i,TLREG);
1685 #if defined(HOST_IMM8)
1686 // On CPUs without 32-bit immediates we need a pointer to invalid_code
1687 else if((opcode[i]&0x3b)==0x39) // SWC1/SDC1
1688 alloc_reg(current,i,INVCP);
1690 // We need a temporary register for address generation
1691 alloc_reg_temp(current,i,-1);
1692 minimum_free_regs[i]=1;
1695 #ifndef multdiv_alloc
1696 void multdiv_alloc(struct regstat *current,int i)
1703 // case 0x1D: DMULTU
1706 clear_const(current,rs1[i]);
1707 clear_const(current,rs2[i]);
1710 if((opcode2[i]&4)==0) // 32-bit
1712 current->u&=~(1LL<<HIREG);
1713 current->u&=~(1LL<<LOREG);
1714 alloc_reg(current,i,HIREG);
1715 alloc_reg(current,i,LOREG);
1716 alloc_reg(current,i,rs1[i]);
1717 alloc_reg(current,i,rs2[i]);
1718 current->is32|=1LL<<HIREG;
1719 current->is32|=1LL<<LOREG;
1720 dirty_reg(current,HIREG);
1721 dirty_reg(current,LOREG);
1725 current->u&=~(1LL<<HIREG);
1726 current->u&=~(1LL<<LOREG);
1727 current->uu&=~(1LL<<HIREG);
1728 current->uu&=~(1LL<<LOREG);
1729 alloc_reg64(current,i,HIREG);
1730 //if(HOST_REGS>10) alloc_reg64(current,i,LOREG);
1731 alloc_reg64(current,i,rs1[i]);
1732 alloc_reg64(current,i,rs2[i]);
1733 alloc_all(current,i);
1734 current->is32&=~(1LL<<HIREG);
1735 current->is32&=~(1LL<<LOREG);
1736 dirty_reg(current,HIREG);
1737 dirty_reg(current,LOREG);
1738 minimum_free_regs[i]=HOST_REGS;
1743 // Multiply by zero is zero.
1744 // MIPS does not have a divide by zero exception.
1745 // The result is undefined, we return zero.
1746 alloc_reg(current,i,HIREG);
1747 alloc_reg(current,i,LOREG);
1748 current->is32|=1LL<<HIREG;
1749 current->is32|=1LL<<LOREG;
1750 dirty_reg(current,HIREG);
1751 dirty_reg(current,LOREG);
1756 static void cop0_alloc(struct regstat *current,int i)
1758 if(opcode2[i]==0) // MFC0
1761 clear_const(current,rt1[i]);
1762 alloc_all(current,i);
1763 alloc_reg(current,i,rt1[i]);
1764 current->is32|=1LL<<rt1[i];
1765 dirty_reg(current,rt1[i]);
1768 else if(opcode2[i]==4) // MTC0
1771 clear_const(current,rs1[i]);
1772 alloc_reg(current,i,rs1[i]);
1773 alloc_all(current,i);
1776 alloc_all(current,i); // FIXME: Keep r0
1778 alloc_reg(current,i,0);
1783 // TLBR/TLBWI/TLBWR/TLBP/ERET
1784 assert(opcode2[i]==0x10);
1785 alloc_all(current,i);
1787 minimum_free_regs[i]=HOST_REGS;
1790 static void cop1_alloc(struct regstat *current,int i)
1792 alloc_reg(current,i,CSREG); // Load status
1793 if(opcode2[i]<3) // MFC1/DMFC1/CFC1
1796 clear_const(current,rt1[i]);
1798 alloc_reg64(current,i,rt1[i]); // DMFC1
1799 current->is32&=~(1LL<<rt1[i]);
1801 alloc_reg(current,i,rt1[i]); // MFC1/CFC1
1802 current->is32|=1LL<<rt1[i];
1804 dirty_reg(current,rt1[i]);
1805 alloc_reg_temp(current,i,-1);
1807 else if(opcode2[i]>3) // MTC1/DMTC1/CTC1
1810 clear_const(current,rs1[i]);
1812 alloc_reg64(current,i,rs1[i]); // DMTC1
1814 alloc_reg(current,i,rs1[i]); // MTC1/CTC1
1815 alloc_reg_temp(current,i,-1);
1819 alloc_reg(current,i,0);
1820 alloc_reg_temp(current,i,-1);
1823 minimum_free_regs[i]=1;
1825 static void fconv_alloc(struct regstat *current,int i)
1827 alloc_reg(current,i,CSREG); // Load status
1828 alloc_reg_temp(current,i,-1);
1829 minimum_free_regs[i]=1;
1831 static void float_alloc(struct regstat *current,int i)
1833 alloc_reg(current,i,CSREG); // Load status
1834 alloc_reg_temp(current,i,-1);
1835 minimum_free_regs[i]=1;
1837 static void fcomp_alloc(struct regstat *current,int i)
1839 alloc_reg(current,i,CSREG); // Load status
1840 alloc_reg(current,i,FSREG); // Load flags
1841 dirty_reg(current,FSREG); // Flag will be modified
1842 alloc_reg_temp(current,i,-1);
1843 minimum_free_regs[i]=1;
1846 static void syscall_alloc(struct regstat *current,int i)
1848 alloc_cc(current,i);
1849 dirty_reg(current,CCREG);
1850 alloc_all(current,i);
1851 minimum_free_regs[i]=HOST_REGS;
1855 static void delayslot_alloc(struct regstat *current,int i)
1865 assem_debug("jump in the delay slot. this shouldn't happen.");//exit(1);
1866 DebugMessage(M64MSG_VERBOSE, "Disabled speculative precompilation");
1870 imm16_alloc(current,i);
1874 load_alloc(current,i);
1878 store_alloc(current,i);
1881 alu_alloc(current,i);
1884 shift_alloc(current,i);
1887 multdiv_alloc(current,i);
1890 shiftimm_alloc(current,i);
1893 mov_alloc(current,i);
1896 cop0_alloc(current,i);
1899 cop1_alloc(current,i);
1902 c1ls_alloc(current,i);
1905 fconv_alloc(current,i);
1908 float_alloc(current,i);
1911 fcomp_alloc(current,i);
1916 // Special case where a branch and delay slot span two pages in virtual memory
1917 static void pagespan_alloc(struct regstat *current,int i)
1920 current->wasconst=0;
1922 minimum_free_regs[i]=HOST_REGS;
1923 alloc_all(current,i);
1924 alloc_cc(current,i);
1925 dirty_reg(current,CCREG);
1926 if(opcode[i]==3) // JAL
1928 alloc_reg(current,i,31);
1929 dirty_reg(current,31);
1931 if(opcode[i]==0&&(opcode2[i]&0x3E)==8) // JR/JALR
1933 alloc_reg(current,i,rs1[i]);
1935 alloc_reg(current,i,rt1[i]);
1936 dirty_reg(current,rt1[i]);
1939 if((opcode[i]&0x2E)==4) // BEQ/BNE/BEQL/BNEL
1941 if(rs1[i]) alloc_reg(current,i,rs1[i]);
1942 if(rs2[i]) alloc_reg(current,i,rs2[i]);
1943 if(!((current->is32>>rs1[i])&(current->is32>>rs2[i])&1))
1945 if(rs1[i]) alloc_reg64(current,i,rs1[i]);
1946 if(rs2[i]) alloc_reg64(current,i,rs2[i]);
1950 if((opcode[i]&0x2E)==6) // BLEZ/BGTZ/BLEZL/BGTZL
1952 if(rs1[i]) alloc_reg(current,i,rs1[i]);
1953 if(!((current->is32>>rs1[i])&1))
1955 if(rs1[i]) alloc_reg64(current,i,rs1[i]);
1959 if(opcode[i]==0x11) // BC1
1961 alloc_reg(current,i,FSREG);
1962 alloc_reg(current,i,CSREG);
1967 static void add_stub(int type,int addr,int retaddr,int a,int b,int c,int d,int e)
1969 stubs[stubcount][0]=type;
1970 stubs[stubcount][1]=addr;
1971 stubs[stubcount][2]=retaddr;
1972 stubs[stubcount][3]=a;
1973 stubs[stubcount][4]=b;
1974 stubs[stubcount][5]=c;
1975 stubs[stubcount][6]=d;
1976 stubs[stubcount][7]=e;
1980 // Write out a single register
1981 static void wb_register(signed char r,signed char regmap[],uint64_t dirty,uint64_t is32)
1984 for(hr=0;hr<HOST_REGS;hr++) {
1985 if(hr!=EXCLUDE_REG) {
1986 if((regmap[hr]&63)==r) {
1989 emit_storereg(r,hr);
1990 if((is32>>regmap[hr])&1) {
1991 emit_sarimm(hr,31,hr);
1992 emit_storereg(r|64,hr);
1995 emit_storereg(r|64,hr);
2003 static int mchecksum()
2005 //if(!tracedebug) return 0;
2008 for(i=0;i<2097152;i++) {
2009 unsigned int temp=sum;
2012 sum^=((u_int *)rdram)[i];
2017 static int rchecksum()
2022 sum^=((u_int *)reg)[i];
2029 DebugMessage(M64MSG_VERBOSE, "TRACE: ");
2031 DebugMessage(M64MSG_VERBOSE, "r%d:%8x%8x ",i,((int *)(reg+i))[1],((int *)(reg+i))[0]);
2032 DebugMessage(M64MSG_VERBOSE, "TRACE: ");
2034 DebugMessage(M64MSG_VERBOSE, "f%d:%8x%8x ",i,((int*)reg_cop1_simple[i])[1],*((int*)reg_cop1_simple[i]));
2037 static void enabletrace()
2043 static void memdebug(int i)
2045 //DebugMessage(M64MSG_VERBOSE, "TRACE: count=%d next=%d (checksum %x) lo=%8x%8x",Count,next_interupt,mchecksum(),(int)(reg[LOREG]>>32),(int)reg[LOREG]);
2046 //DebugMessage(M64MSG_VERBOSE, "TRACE: count=%d next=%d (rchecksum %x)",Count,next_interupt,rchecksum());
2049 //if(Count>=-2084597794) {
2050 if((signed int)Count>=-2084597794&&(signed int)Count<0) {
2052 DebugMessage(M64MSG_VERBOSE, "TRACE: count=%d next=%d (checksum %x)",Count,next_interupt,mchecksum());
2053 //DebugMessage(M64MSG_VERBOSE, "TRACE: count=%d next=%d (checksum %x) Status=%x",Count,next_interupt,mchecksum(),Status);
2054 //DebugMessage(M64MSG_VERBOSE, "TRACE: count=%d next=%d (checksum %x) hi=%8x%8x",Count,next_interupt,mchecksum(),(int)(reg[HIREG]>>32),(int)reg[HIREG]);
2056 #if NEW_DYNAREC == NEW_DYNAREC_X86
2057 DebugMessage(M64MSG_VERBOSE, "TRACE: %x",(&i)[-1]);
2059 #if NEW_DYNAREC == NEW_DYNAREC_ARM
2061 DebugMessage(M64MSG_VERBOSE, "TRACE: %x ",(&j)[10]);
2062 DebugMessage(M64MSG_VERBOSE, "TRACE: %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x",(&j)[1],(&j)[2],(&j)[3],(&j)[4],(&j)[5],(&j)[6],(&j)[7],(&j)[8],(&j)[9],(&j)[10],(&j)[11],(&j)[12],(&j)[13],(&j)[14],(&j)[15],(&j)[16],(&j)[17],(&j)[18],(&j)[19],(&j)[20]);
2066 //DebugMessage(M64MSG_VERBOSE, "TRACE: %x",(&i)[-1]);
2071 static void tlb_debug(u_int cause, u_int addr, u_int iaddr)
2073 DebugMessage(M64MSG_VERBOSE, "TLB Exception: instruction=%x addr=%x cause=%x",iaddr, addr, cause);
2077 static void alu_assemble(int i,struct regstat *i_regs)
2079 if(opcode2[i]>=0x20&&opcode2[i]<=0x23) { // ADD/ADDU/SUB/SUBU
2081 signed char s1,s2,t;
2082 t=get_reg(i_regs->regmap,rt1[i]);
2084 s1=get_reg(i_regs->regmap,rs1[i]);
2085 s2=get_reg(i_regs->regmap,rs2[i]);
2086 if(rs1[i]&&rs2[i]) {
2089 if(opcode2[i]&2) emit_sub(s1,s2,t);
2090 else emit_add(s1,s2,t);
2093 if(s1>=0) emit_mov(s1,t);
2094 else emit_loadreg(rs1[i],t);
2098 if(opcode2[i]&2) emit_neg(s2,t);
2099 else emit_mov(s2,t);
2102 emit_loadreg(rs2[i],t);
2103 if(opcode2[i]&2) emit_neg(t,t);
2106 else emit_zeroreg(t);
2110 if(opcode2[i]>=0x2c&&opcode2[i]<=0x2f) { // DADD/DADDU/DSUB/DSUBU
2112 signed char s1l,s2l,s1h,s2h,tl,th;
2113 tl=get_reg(i_regs->regmap,rt1[i]);
2114 th=get_reg(i_regs->regmap,rt1[i]|64);
2116 s1l=get_reg(i_regs->regmap,rs1[i]);
2117 s2l=get_reg(i_regs->regmap,rs2[i]);
2118 s1h=get_reg(i_regs->regmap,rs1[i]|64);
2119 s2h=get_reg(i_regs->regmap,rs2[i]|64);
2120 if(rs1[i]&&rs2[i]) {
2123 if(opcode2[i]&2) emit_subs(s1l,s2l,tl);
2124 else emit_adds(s1l,s2l,tl);
2126 #ifdef INVERTED_CARRY
2127 if(opcode2[i]&2) {if(s1h!=th) emit_mov(s1h,th);emit_sbb(th,s2h);}
2129 if(opcode2[i]&2) emit_sbc(s1h,s2h,th);
2131 else emit_add(s1h,s2h,th);
2135 if(s1l>=0) emit_mov(s1l,tl);
2136 else emit_loadreg(rs1[i],tl);
2138 if(s1h>=0) emit_mov(s1h,th);
2139 else emit_loadreg(rs1[i]|64,th);
2144 if(opcode2[i]&2) emit_negs(s2l,tl);
2145 else emit_mov(s2l,tl);
2148 emit_loadreg(rs2[i],tl);
2149 if(opcode2[i]&2) emit_negs(tl,tl);
2152 #ifdef INVERTED_CARRY
2153 if(s2h>=0) emit_mov(s2h,th);
2154 else emit_loadreg(rs2[i]|64,th);
2156 emit_adcimm(-1,th); // x86 has inverted carry flag
2161 if(s2h>=0) emit_rscimm(s2h,0,th);
2163 emit_loadreg(rs2[i]|64,th);
2164 emit_rscimm(th,0,th);
2167 if(s2h>=0) emit_mov(s2h,th);
2168 else emit_loadreg(rs2[i]|64,th);
2175 if(th>=0) emit_zeroreg(th);
2180 if(opcode2[i]==0x2a||opcode2[i]==0x2b) { // SLT/SLTU
2182 signed char s1l,s1h,s2l,s2h,t;
2183 if(!((i_regs->was32>>rs1[i])&(i_regs->was32>>rs2[i])&1))
2185 t=get_reg(i_regs->regmap,rt1[i]);
2188 s1l=get_reg(i_regs->regmap,rs1[i]);
2189 s1h=get_reg(i_regs->regmap,rs1[i]|64);
2190 s2l=get_reg(i_regs->regmap,rs2[i]);
2191 s2h=get_reg(i_regs->regmap,rs2[i]|64);
2192 if(rs2[i]==0) // rx<r0
2195 if(opcode2[i]==0x2a) // SLT
2196 emit_shrimm(s1h,31,t);
2197 else // SLTU (unsigned can not be less than zero)
2200 else if(rs1[i]==0) // r0<rx
2203 if(opcode2[i]==0x2a) // SLT
2204 emit_set_gz64_32(s2h,s2l,t);
2205 else // SLTU (set if not zero)
2206 emit_set_nz64_32(s2h,s2l,t);
2209 assert(s1l>=0);assert(s1h>=0);
2210 assert(s2l>=0);assert(s2h>=0);
2211 if(opcode2[i]==0x2a) // SLT
2212 emit_set_if_less64_32(s1h,s1l,s2h,s2l,t);
2214 emit_set_if_carry64_32(s1h,s1l,s2h,s2l,t);
2218 t=get_reg(i_regs->regmap,rt1[i]);
2221 s1l=get_reg(i_regs->regmap,rs1[i]);
2222 s2l=get_reg(i_regs->regmap,rs2[i]);
2223 if(rs2[i]==0) // rx<r0
2226 if(opcode2[i]==0x2a) // SLT
2227 emit_shrimm(s1l,31,t);
2228 else // SLTU (unsigned can not be less than zero)
2231 else if(rs1[i]==0) // r0<rx
2234 if(opcode2[i]==0x2a) // SLT
2235 emit_set_gz32(s2l,t);
2236 else // SLTU (set if not zero)
2237 emit_set_nz32(s2l,t);
2240 assert(s1l>=0);assert(s2l>=0);
2241 if(opcode2[i]==0x2a) // SLT
2242 emit_set_if_less32(s1l,s2l,t);
2244 emit_set_if_carry32(s1l,s2l,t);
2250 if(opcode2[i]>=0x24&&opcode2[i]<=0x27) { // AND/OR/XOR/NOR
2252 signed char s1l,s1h,s2l,s2h,th,tl;
2253 tl=get_reg(i_regs->regmap,rt1[i]);
2254 th=get_reg(i_regs->regmap,rt1[i]|64);
2255 if(!((i_regs->was32>>rs1[i])&(i_regs->was32>>rs2[i])&1)&&th>=0)
2259 s1l=get_reg(i_regs->regmap,rs1[i]);
2260 s1h=get_reg(i_regs->regmap,rs1[i]|64);
2261 s2l=get_reg(i_regs->regmap,rs2[i]);
2262 s2h=get_reg(i_regs->regmap,rs2[i]|64);
2263 if(rs1[i]&&rs2[i]) {
2264 assert(s1l>=0);assert(s1h>=0);
2265 assert(s2l>=0);assert(s2h>=0);
2266 if(opcode2[i]==0x24) { // AND
2267 emit_and(s1l,s2l,tl);
2268 emit_and(s1h,s2h,th);
2270 if(opcode2[i]==0x25) { // OR
2271 emit_or(s1l,s2l,tl);
2272 emit_or(s1h,s2h,th);
2274 if(opcode2[i]==0x26) { // XOR
2275 emit_xor(s1l,s2l,tl);
2276 emit_xor(s1h,s2h,th);
2278 if(opcode2[i]==0x27) { // NOR
2279 emit_or(s1l,s2l,tl);
2280 emit_or(s1h,s2h,th);
2287 if(opcode2[i]==0x24) { // AND
2291 if(opcode2[i]==0x25||opcode2[i]==0x26) { // OR/XOR
2293 if(s1l>=0) emit_mov(s1l,tl);
2294 else emit_loadreg(rs1[i],tl);
2295 if(s1h>=0) emit_mov(s1h,th);
2296 else emit_loadreg(rs1[i]|64,th);
2300 if(s2l>=0) emit_mov(s2l,tl);
2301 else emit_loadreg(rs2[i],tl);
2302 if(s2h>=0) emit_mov(s2h,th);
2303 else emit_loadreg(rs2[i]|64,th);
2310 if(opcode2[i]==0x27) { // NOR
2312 if(s1l>=0) emit_not(s1l,tl);
2314 emit_loadreg(rs1[i],tl);
2317 if(s1h>=0) emit_not(s1h,th);
2319 emit_loadreg(rs1[i]|64,th);
2325 if(s2l>=0) emit_not(s2l,tl);
2327 emit_loadreg(rs2[i],tl);
2330 if(s2h>=0) emit_not(s2h,th);
2332 emit_loadreg(rs2[i]|64,th);
2348 s1l=get_reg(i_regs->regmap,rs1[i]);
2349 s2l=get_reg(i_regs->regmap,rs2[i]);
2350 if(rs1[i]&&rs2[i]) {
2353 if(opcode2[i]==0x24) { // AND
2354 emit_and(s1l,s2l,tl);
2356 if(opcode2[i]==0x25) { // OR
2357 emit_or(s1l,s2l,tl);
2359 if(opcode2[i]==0x26) { // XOR
2360 emit_xor(s1l,s2l,tl);
2362 if(opcode2[i]==0x27) { // NOR
2363 emit_or(s1l,s2l,tl);
2369 if(opcode2[i]==0x24) { // AND
2372 if(opcode2[i]==0x25||opcode2[i]==0x26) { // OR/XOR
2374 if(s1l>=0) emit_mov(s1l,tl);
2375 else emit_loadreg(rs1[i],tl); // CHECK: regmap_entry?
2379 if(s2l>=0) emit_mov(s2l,tl);
2380 else emit_loadreg(rs2[i],tl); // CHECK: regmap_entry?
2382 else emit_zeroreg(tl);
2384 if(opcode2[i]==0x27) { // NOR
2386 if(s1l>=0) emit_not(s1l,tl);
2388 emit_loadreg(rs1[i],tl);
2394 if(s2l>=0) emit_not(s2l,tl);
2396 emit_loadreg(rs2[i],tl);
2400 else emit_movimm(-1,tl);
2409 static void imm16_assemble(int i,struct regstat *i_regs)
2411 if (opcode[i]==0x0f) { // LUI
2414 t=get_reg(i_regs->regmap,rt1[i]);
2417 if(!((i_regs->isconst>>t)&1))
2418 emit_movimm(imm[i]<<16,t);
2422 if(opcode[i]==0x08||opcode[i]==0x09) { // ADDI/ADDIU
2425 t=get_reg(i_regs->regmap,rt1[i]);
2426 s=get_reg(i_regs->regmap,rs1[i]);
2431 if(!((i_regs->isconst>>t)&1)) {
2433 if(i_regs->regmap_entry[t]!=rs1[i]) emit_loadreg(rs1[i],t);
2434 emit_addimm(t,imm[i],t);
2436 if(!((i_regs->wasconst>>s)&1))
2437 emit_addimm(s,imm[i],t);
2439 emit_movimm(constmap[i][s]+imm[i],t);
2445 if(!((i_regs->isconst>>t)&1))
2446 emit_movimm(imm[i],t);
2451 if(opcode[i]==0x18||opcode[i]==0x19) { // DADDI/DADDIU
2453 signed char sh,sl,th,tl;
2454 th=get_reg(i_regs->regmap,rt1[i]|64);
2455 tl=get_reg(i_regs->regmap,rt1[i]);
2456 sh=get_reg(i_regs->regmap,rs1[i]|64);
2457 sl=get_reg(i_regs->regmap,rs1[i]);
2463 emit_addimm64_32(sh,sl,imm[i],th,tl);
2466 emit_addimm(sl,imm[i],tl);
2469 emit_movimm(imm[i],tl);
2470 if(th>=0) emit_movimm(((signed int)imm[i])>>31,th);
2475 else if(opcode[i]==0x0a||opcode[i]==0x0b) { // SLTI/SLTIU
2477 //assert(rs1[i]!=0); // r0 might be valid, but it's probably a bug
2478 signed char sh,sl,t;
2479 t=get_reg(i_regs->regmap,rt1[i]);
2480 sh=get_reg(i_regs->regmap,rs1[i]|64);
2481 sl=get_reg(i_regs->regmap,rs1[i]);
2485 if(sh<0) assert((i_regs->was32>>rs1[i])&1);
2486 if(sh<0||((i_regs->was32>>rs1[i])&1)) {
2487 if(opcode[i]==0x0a) { // SLTI
2489 if(i_regs->regmap_entry[t]!=rs1[i]) emit_loadreg(rs1[i],t);
2490 emit_slti32(t,imm[i],t);
2492 emit_slti32(sl,imm[i],t);
2497 if(i_regs->regmap_entry[t]!=rs1[i]) emit_loadreg(rs1[i],t);
2498 emit_sltiu32(t,imm[i],t);
2500 emit_sltiu32(sl,imm[i],t);
2505 if(opcode[i]==0x0a) // SLTI
2506 emit_slti64_32(sh,sl,imm[i],t);
2508 emit_sltiu64_32(sh,sl,imm[i],t);
2511 // SLTI(U) with r0 is just stupid,
2512 // nonetheless examples can be found
2513 if(opcode[i]==0x0a) // SLTI
2514 if(0<imm[i]) emit_movimm(1,t);
2515 else emit_zeroreg(t);
2518 if(imm[i]) emit_movimm(1,t);
2519 else emit_zeroreg(t);
2525 else if(opcode[i]>=0x0c&&opcode[i]<=0x0e) { // ANDI/ORI/XORI
2527 signed char sh,sl,th,tl;
2528 th=get_reg(i_regs->regmap,rt1[i]|64);
2529 tl=get_reg(i_regs->regmap,rt1[i]);
2530 sh=get_reg(i_regs->regmap,rs1[i]|64);
2531 sl=get_reg(i_regs->regmap,rs1[i]);
2532 if(tl>=0 && !((i_regs->isconst>>tl)&1)) {
2533 if(opcode[i]==0x0c) //ANDI
2537 if(i_regs->regmap_entry[tl]!=rs1[i]) emit_loadreg(rs1[i],tl);
2538 emit_andimm(tl,imm[i],tl);
2540 if(!((i_regs->wasconst>>sl)&1))
2541 emit_andimm(sl,imm[i],tl);
2543 emit_movimm(constmap[i][sl]&imm[i],tl);
2548 if(th>=0) emit_zeroreg(th);
2554 if(i_regs->regmap_entry[tl]!=rs1[i]) emit_loadreg(rs1[i],tl);
2558 emit_loadreg(rs1[i]|64,th);
2563 if(opcode[i]==0x0d) { //ORI
2565 emit_orimm(tl,imm[i],tl);
2567 if(!((i_regs->wasconst>>sl)&1))
2568 emit_orimm(sl,imm[i],tl);
2570 emit_movimm(constmap[i][sl]|imm[i],tl);
2573 if(opcode[i]==0x0e) { //XORI
2575 emit_xorimm(tl,imm[i],tl);
2577 if(!((i_regs->wasconst>>sl)&1))
2578 emit_xorimm(sl,imm[i],tl);
2580 emit_movimm(constmap[i][sl]^imm[i],tl);
2585 emit_movimm(imm[i],tl);
2586 if(th>=0) emit_zeroreg(th);
2594 static void shiftimm_assemble(int i,struct regstat *i_regs)
2596 if(opcode2[i]<=0x3) // SLL/SRL/SRA
2600 t=get_reg(i_regs->regmap,rt1[i]);
2601 s=get_reg(i_regs->regmap,rs1[i]);
2610 if(s<0&&i_regs->regmap_entry[t]!=rs1[i]) emit_loadreg(rs1[i],t);
2612 if(opcode2[i]==0) // SLL
2614 emit_shlimm(s<0?t:s,imm[i],t);
2616 if(opcode2[i]==2) // SRL
2618 emit_shrimm(s<0?t:s,imm[i],t);
2620 if(opcode2[i]==3) // SRA
2622 emit_sarimm(s<0?t:s,imm[i],t);
2626 if(s>=0 && s!=t) emit_mov(s,t);
2630 //emit_storereg(rt1[i],t); //DEBUG
2633 if(opcode2[i]>=0x38&&opcode2[i]<=0x3b) // DSLL/DSRL/DSRA
2636 signed char sh,sl,th,tl;
2637 th=get_reg(i_regs->regmap,rt1[i]|64);
2638 tl=get_reg(i_regs->regmap,rt1[i]);
2639 sh=get_reg(i_regs->regmap,rs1[i]|64);
2640 sl=get_reg(i_regs->regmap,rs1[i]);
2645 if(th>=0) emit_zeroreg(th);
2652 if(opcode2[i]==0x38) // DSLL
2654 if(th>=0) emit_shldimm(sh,sl,imm[i],th);
2655 emit_shlimm(sl,imm[i],tl);
2657 if(opcode2[i]==0x3a) // DSRL
2659 emit_shrdimm(sl,sh,imm[i],tl);
2660 if(th>=0) emit_shrimm(sh,imm[i],th);
2662 if(opcode2[i]==0x3b) // DSRA
2664 emit_shrdimm(sl,sh,imm[i],tl);
2665 if(th>=0) emit_sarimm(sh,imm[i],th);
2669 if(sl!=tl) emit_mov(sl,tl);
2670 if(th>=0&&sh!=th) emit_mov(sh,th);
2676 if(opcode2[i]==0x3c) // DSLL32
2679 signed char sl,tl,th;
2680 tl=get_reg(i_regs->regmap,rt1[i]);
2681 th=get_reg(i_regs->regmap,rt1[i]|64);
2682 sl=get_reg(i_regs->regmap,rs1[i]);
2691 emit_shlimm(th,imm[i]&31,th);
2696 if(opcode2[i]==0x3e) // DSRL32
2699 signed char sh,tl,th;
2700 tl=get_reg(i_regs->regmap,rt1[i]);
2701 th=get_reg(i_regs->regmap,rt1[i]|64);
2702 sh=get_reg(i_regs->regmap,rs1[i]|64);
2706 if(th>=0) emit_zeroreg(th);
2709 emit_shrimm(tl,imm[i]&31,tl);
2714 if(opcode2[i]==0x3f) // DSRA32
2718 tl=get_reg(i_regs->regmap,rt1[i]);
2719 sh=get_reg(i_regs->regmap,rs1[i]|64);
2725 emit_sarimm(tl,imm[i]&31,tl);
2732 #ifndef shift_assemble
2733 void shift_assemble(int i,struct regstat *i_regs)
2735 DebugMessage(M64MSG_ERROR, "Need shift_assemble for this architecture.");
2740 static void load_assemble(int i,struct regstat *i_regs)
2742 int s,th,tl,addr,map=-1,cache=-1;
2747 th=get_reg(i_regs->regmap,rt1[i]|64);
2748 tl=get_reg(i_regs->regmap,rt1[i]);
2749 s=get_reg(i_regs->regmap,rs1[i]);
2751 for(hr=0;hr<HOST_REGS;hr++) {
2752 if(i_regs->regmap[hr]>=0) reglist|=1<<hr;
2754 if(i_regs->regmap[HOST_CCREG]==CCREG) reglist&=~(1<<HOST_CCREG);
2756 c=(i_regs->wasconst>>s)&1;
2757 memtarget=((signed int)(constmap[i][s]+offset))<(signed int)0x80800000;
2758 if(using_tlb&&((signed int)(constmap[i][s]+offset))>=(signed int)0xC0000000) memtarget=1;
2760 if(tl<0) tl=get_reg(i_regs->regmap,-1);
2761 if(offset||s<0||c) addr=tl;
2763 //DebugMessage(M64MSG_VERBOSE, "load_assemble: c=%d",c);
2764 //if(c) DebugMessage(M64MSG_VERBOSE, "load_assemble: const=%x",(int)constmap[i][s]+offset);
2765 assert(tl>=0); // Even if the load is a NOP, we must check for pagefaults and I/O
2767 if(th>=0) reglist&=~(1<<th);
2771 map=get_reg(i_regs->regmap,ROREG);
2772 if(map<0) emit_loadreg(ROREG,map=HOST_TEMPREG);
2774 //#define R29_HACK 1
2776 // Strmnnrmn's speed hack
2777 if(rs1[i]!=29||start<0x80001000||start>=0x80800000)
2780 emit_cmpimm(addr,0x800000);
2782 #ifdef CORTEX_A8_BRANCH_PREDICTION_HACK
2783 // Hint to branch predictor that the branch is unlikely to be taken
2785 emit_jno_unlikely(0);
2793 if (opcode[i]==0x20||opcode[i]==0x24) x=3; // LB/LBU
2794 if (opcode[i]==0x21||opcode[i]==0x25) x=2; // LH/LHU
2795 map=get_reg(i_regs->regmap,TLREG);
2796 cache=get_reg(i_regs->regmap,MMREG);
2799 map=do_tlb_r(addr,tl,map,cache,x,-1,-1,c,constmap[i][s]+offset);
2800 do_tlb_r_branch(map,c,constmap[i][s]+offset,&jaddr);
2802 int dummy=(rt1[i]==0)||(tl!=get_reg(i_regs->regmap,rt1[i])); // ignore loads to r0 and unneeded reg
2803 if (opcode[i]==0x20) { // LB
2806 #ifdef HOST_IMM_ADDR32
2808 emit_movsbl_tlb((constmap[i][s]+offset)^3,map,tl);
2812 //emit_xorimm(addr,3,tl);
2813 //gen_tlb_addr_r(tl,map);
2814 //emit_movsbl_indexed((int)rdram-0x80000000,tl,tl);
2816 if(!c) emit_xorimm(addr,3,tl);
2817 else x=((constmap[i][s]+offset)^3)-(constmap[i][s]+offset);
2818 emit_movsbl_indexed_tlb(x,tl,map,tl);
2822 add_stub(LOADB_STUB,jaddr,(int)out,i,addr,(int)i_regs,ccadj[i],reglist);
2825 inline_readstub(LOADB_STUB,i,constmap[i][s]+offset,i_regs->regmap,rt1[i],ccadj[i],reglist);
2827 if (opcode[i]==0x21) { // LH
2830 #ifdef HOST_IMM_ADDR32
2832 emit_movswl_tlb((constmap[i][s]+offset)^2,map,tl);
2837 if(!c) emit_xorimm(addr,2,tl);
2838 else x=((constmap[i][s]+offset)^2)-(constmap[i][s]+offset);
2840 //emit_movswl_indexed_tlb(x,tl,map,tl);
2843 gen_tlb_addr_r(tl,map);
2844 emit_movswl_indexed(x,tl,tl);
2847 emit_movswl_indexed(x,tl,tl);
2849 emit_movswl_indexed((int)rdram-0x80000000+x,tl,tl);
2855 add_stub(LOADH_STUB,jaddr,(int)out,i,addr,(int)i_regs,ccadj[i],reglist);
2858 inline_readstub(LOADH_STUB,i,constmap[i][s]+offset,i_regs->regmap,rt1[i],ccadj[i],reglist);
2860 if (opcode[i]==0x23) { // LW
2863 //emit_readword_indexed((int)rdram-0x80000000,addr,tl);
2864 #ifdef HOST_IMM_ADDR32
2866 emit_readword_tlb(constmap[i][s]+offset,map,tl);
2869 emit_readword_indexed_tlb(0,addr,map,tl);
2872 add_stub(LOADW_STUB,jaddr,(int)out,i,addr,(int)i_regs,ccadj[i],reglist);
2875 inline_readstub(LOADW_STUB,i,constmap[i][s]+offset,i_regs->regmap,rt1[i],ccadj[i],reglist);
2877 if (opcode[i]==0x24) { // LBU
2880 #ifdef HOST_IMM_ADDR32
2882 emit_movzbl_tlb((constmap[i][s]+offset)^3,map,tl);
2886 //emit_xorimm(addr,3,tl);
2887 //gen_tlb_addr_r(tl,map);
2888 //emit_movzbl_indexed((int)rdram-0x80000000,tl,tl);
2890 if(!c) emit_xorimm(addr,3,tl);
2891 else x=((constmap[i][s]+offset)^3)-(constmap[i][s]+offset);
2892 emit_movzbl_indexed_tlb(x,tl,map,tl);
2896 add_stub(LOADBU_STUB,jaddr,(int)out,i,addr,(int)i_regs,ccadj[i],reglist);
2899 inline_readstub(LOADBU_STUB,i,constmap[i][s]+offset,i_regs->regmap,rt1[i],ccadj[i],reglist);
2901 if (opcode[i]==0x25) { // LHU
2904 #ifdef HOST_IMM_ADDR32
2906 emit_movzwl_tlb((constmap[i][s]+offset)^2,map,tl);
2911 if(!c) emit_xorimm(addr,2,tl);
2912 else x=((constmap[i][s]+offset)^2)-(constmap[i][s]+offset);
2914 //emit_movzwl_indexed_tlb(x,tl,map,tl);
2917 gen_tlb_addr_r(tl,map);
2918 emit_movzwl_indexed(x,tl,tl);
2921 emit_movzwl_indexed(x,tl,tl);
2923 emit_movzwl_indexed((int)rdram-0x80000000+x,tl,tl);
2929 add_stub(LOADHU_STUB,jaddr,(int)out,i,addr,(int)i_regs,ccadj[i],reglist);
2932 inline_readstub(LOADHU_STUB,i,constmap[i][s]+offset,i_regs->regmap,rt1[i],ccadj[i],reglist);
2934 if (opcode[i]==0x27) { // LWU
2938 //emit_readword_indexed((int)rdram-0x80000000,addr,tl);
2939 #ifdef HOST_IMM_ADDR32
2941 emit_readword_tlb(constmap[i][s]+offset,map,tl);
2944 emit_readword_indexed_tlb(0,addr,map,tl);
2947 add_stub(LOADW_STUB,jaddr,(int)out,i,addr,(int)i_regs,ccadj[i],reglist);
2950 inline_readstub(LOADW_STUB,i,constmap[i][s]+offset,i_regs->regmap,rt1[i],ccadj[i],reglist);
2954 if (opcode[i]==0x37) { // LD
2957 //gen_tlb_addr_r(tl,map);
2958 //if(th>=0) emit_readword_indexed((int)rdram-0x80000000,addr,th);
2959 //emit_readword_indexed((int)rdram-0x7FFFFFFC,addr,tl);
2960 #ifdef HOST_IMM_ADDR32
2962 emit_readdword_tlb(constmap[i][s]+offset,map,th,tl);
2965 emit_readdword_indexed_tlb(0,addr,map,th,tl);
2968 add_stub(LOADD_STUB,jaddr,(int)out,i,addr,(int)i_regs,ccadj[i],reglist);
2971 inline_readstub(LOADD_STUB,i,constmap[i][s]+offset,i_regs->regmap,rt1[i],ccadj[i],reglist);
2973 //emit_storereg(rt1[i],tl); // DEBUG
2974 //if(opcode[i]==0x23)
2975 //if(opcode[i]==0x24)
2976 //if(opcode[i]==0x23||opcode[i]==0x24)
2977 /*if(opcode[i]==0x21||opcode[i]==0x23||opcode[i]==0x24)
2981 emit_readword((int)&last_count,ECX);
2982 #if NEW_DYNAREC == NEW_DYNAREC_X86
2983 if(get_reg(i_regs->regmap,CCREG)<0)
2984 emit_loadreg(CCREG,HOST_CCREG);
2985 emit_add(HOST_CCREG,ECX,HOST_CCREG);
2986 emit_addimm(HOST_CCREG,2*ccadj[i],HOST_CCREG);
2987 emit_writeword(HOST_CCREG,(int)&Count);
2989 #if NEW_DYNAREC == NEW_DYNAREC_ARM
2990 if(get_reg(i_regs->regmap,CCREG)<0)
2991 emit_loadreg(CCREG,0);
2993 emit_mov(HOST_CCREG,0);
2995 emit_addimm(0,2*ccadj[i],0);
2996 emit_writeword(0,(int)&Count);
2998 emit_call((int)memdebug);
3000 restore_regs(0x100f);
3004 #ifndef loadlr_assemble
3005 static void loadlr_assemble(int i,struct regstat *i_regs)
3007 DebugMessage(M64MSG_ERROR, "Need loadlr_assemble for this architecture.");
3012 static void store_assemble(int i,struct regstat *i_regs)
3014 int s,th,tl,map=-1,cache=-1;
3017 int jaddr=0,jaddr2,type;
3019 int agr=AGEN1+(i&1);
3021 th=get_reg(i_regs->regmap,rs2[i]|64);
3022 tl=get_reg(i_regs->regmap,rs2[i]);
3023 s=get_reg(i_regs->regmap,rs1[i]);
3024 temp=get_reg(i_regs->regmap,agr);
3025 if(temp<0) temp=get_reg(i_regs->regmap,-1);
3028 c=(i_regs->wasconst>>s)&1;
3029 memtarget=((signed int)(constmap[i][s]+offset))<(signed int)0x80800000;
3030 if(using_tlb&&((signed int)(constmap[i][s]+offset))>=(signed int)0xC0000000) memtarget=1;
3034 for(hr=0;hr<HOST_REGS;hr++) {
3035 if(i_regs->regmap[hr]>=0) reglist|=1<<hr;
3037 if(i_regs->regmap[HOST_CCREG]==CCREG) reglist&=~(1<<HOST_CCREG);
3038 if(offset||s<0||c) addr=temp;
3042 map=get_reg(i_regs->regmap,ROREG);
3043 if(map<0) emit_loadreg(ROREG,map=HOST_TEMPREG);
3047 // Strmnnrmn's speed hack
3049 if(rs1[i]!=29||start<0x80001000||start>=0x80800000)
3051 emit_cmpimm(addr,0x800000);
3052 #ifdef DESTRUCTIVE_SHIFT
3053 if(s==addr) emit_mov(s,temp);
3056 if(rs1[i]!=29||start<0x80001000||start>=0x80800000)
3060 #ifdef CORTEX_A8_BRANCH_PREDICTION_HACK
3061 // Hint to branch predictor that the branch is unlikely to be taken
3063 emit_jno_unlikely(0);
3071 if (opcode[i]==0x28) x=3; // SB
3072 if (opcode[i]==0x29) x=2; // SH
3073 map=get_reg(i_regs->regmap,TLREG);
3074 cache=get_reg(i_regs->regmap,MMREG);
3077 map=do_tlb_w(addr,temp,map,cache,x,c,constmap[i][s]+offset);
3078 do_tlb_w_branch(map,c,constmap[i][s]+offset,&jaddr);
3081 if (opcode[i]==0x28) { // SB
3084 if(!c) emit_xorimm(addr,3,temp);
3085 else x=((constmap[i][s]+offset)^3)-(constmap[i][s]+offset);
3086 //gen_tlb_addr_w(temp,map);
3087 //emit_writebyte_indexed(tl,(int)rdram-0x80000000,temp);
3088 emit_writebyte_indexed_tlb(tl,x,temp,map,temp);
3092 if (opcode[i]==0x29) { // SH
3095 if(!c) emit_xorimm(addr,2,temp);
3096 else x=((constmap[i][s]+offset)^2)-(constmap[i][s]+offset);
3098 //emit_writehword_indexed_tlb(tl,x,temp,map,temp);
3101 gen_tlb_addr_w(temp,map);
3102 emit_writehword_indexed(tl,x,temp);
3104 emit_writehword_indexed(tl,(int)rdram-0x80000000+x,temp);
3108 if (opcode[i]==0x2B) { // SW
3110 //emit_writeword_indexed(tl,(int)rdram-0x80000000,addr);
3111 emit_writeword_indexed_tlb(tl,0,addr,map,temp);
3114 if (opcode[i]==0x3F) { // SD
3118 //emit_writeword_indexed(th,(int)rdram-0x80000000,addr);
3119 //emit_writeword_indexed(tl,(int)rdram-0x7FFFFFFC,addr);
3120 emit_writedword_indexed_tlb(th,tl,0,addr,map,temp);
3123 //emit_writeword_indexed(tl,(int)rdram-0x80000000,temp);
3124 //emit_writeword_indexed(tl,(int)rdram-0x7FFFFFFC,temp);
3125 emit_writedword_indexed_tlb(tl,tl,0,addr,map,temp);
3132 #ifdef DESTRUCTIVE_SHIFT
3133 // The x86 shift operation is 'destructive'; it overwrites the
3134 // source register, so we need to make a copy first and use that.
3137 #if defined(HOST_IMM8)
3138 int ir=get_reg(i_regs->regmap,INVCP);
3140 emit_cmpmem_indexedsr12_reg(ir,addr,1);
3142 emit_cmpmem_indexedsr12_imm((int)invalid_code,addr,1);
3144 #if defined(HAVE_CONDITIONAL_CALL) && !defined(DESTRUCTIVE_SHIFT)
3145 emit_callne(invalidate_addr_reg[addr]);
3149 add_stub(INVCODE_STUB,jaddr2,(int)out,reglist|(1<<HOST_CCREG),addr,0,0,0);
3154 add_stub(type,jaddr,(int)out,i,addr,(int)i_regs,ccadj[i],reglist);
3155 } else if(c&&!memtarget) {
3156 inline_writestub(type,i,constmap[i][s]+offset,i_regs->regmap,rs2[i],ccadj[i],reglist);
3158 //if(opcode[i]==0x2B || opcode[i]==0x3F)
3159 //if(opcode[i]==0x2B || opcode[i]==0x28)
3160 //if(opcode[i]==0x2B || opcode[i]==0x29)
3161 //if(opcode[i]==0x2B)
3163 // Uncomment for extra debug output:
3165 if(opcode[i]==0x2B || opcode[i]==0x28 || opcode[i]==0x29 || opcode[i]==0x3F)
3167 #if NEW_DYNAREC == NEW_DYNAREC_X86
3170 #if NEW_DYNAREC == NEW_DYNAREC_ARM
3173 emit_readword((int)&last_count,ECX);
3174 #if NEW_DYNAREC == NEW_DYNAREC_X86
3175 if(get_reg(i_regs->regmap,CCREG)<0)
3176 emit_loadreg(CCREG,HOST_CCREG);
3177 emit_add(HOST_CCREG,ECX,HOST_CCREG);
3178 emit_addimm(HOST_CCREG,2*ccadj[i],HOST_CCREG);
3179 emit_writeword(HOST_CCREG,(int)&Count);
3181 #if NEW_DYNAREC == NEW_DYNAREC_ARM
3182 if(get_reg(i_regs->regmap,CCREG)<0)
3183 emit_loadreg(CCREG,0);
3185 emit_mov(HOST_CCREG,0);
3187 emit_addimm(0,2*ccadj[i],0);
3188 emit_writeword(0,(int)&Count);
3190 emit_call((int)memdebug);
3191 #if NEW_DYNAREC == NEW_DYNAREC_X86
3194 #if NEW_DYNAREC == NEW_DYNAREC_ARM
3195 restore_regs(0x100f);
3201 static void storelr_assemble(int i,struct regstat *i_regs)
3208 int case1,case2,case3;
3209 int done0,done1,done2;
3211 int agr=AGEN1+(i&1);
3213 th=get_reg(i_regs->regmap,rs2[i]|64);
3214 tl=get_reg(i_regs->regmap,rs2[i]);
3215 s=get_reg(i_regs->regmap,rs1[i]);
3216 temp=get_reg(i_regs->regmap,agr);
3217 if(temp<0) temp=get_reg(i_regs->regmap,-1);
3220 c=(i_regs->isconst>>s)&1;
3221 memtarget=((signed int)(constmap[i][s]+offset))<(signed int)0x80800000;
3222 if(using_tlb&&((signed int)(constmap[i][s]+offset))>=(signed int)0xC0000000) memtarget=1;
3225 for(hr=0;hr<HOST_REGS;hr++) {
3226 if(i_regs->regmap[hr]>=0) reglist|=1<<hr;
3231 emit_cmpimm(s<0||offset?temp:s,0x800000);
3232 if(!offset&&s!=temp) emit_mov(s,temp);
3238 if(!memtarget||!rs1[i]) {
3244 int map=get_reg(i_regs->regmap,ROREG);
3245 if(map<0) emit_loadreg(ROREG,map=HOST_TEMPREG);
3246 gen_tlb_addr_w(temp,map);
3248 if((u_int)rdram!=0x80000000)
3249 emit_addimm_no_flags((u_int)rdram-(u_int)0x80000000,temp);
3252 int map=get_reg(i_regs->regmap,TLREG);
3253 int cache=get_reg(i_regs->regmap,MMREG);
3256 map=do_tlb_w(c||s<0||offset?temp:s,temp,map,cache,0,c,constmap[i][s]+offset);
3257 if(!c&&!offset&&s>=0) emit_mov(s,temp);
3258 do_tlb_w_branch(map,c,constmap[i][s]+offset,&jaddr);
3259 if(!jaddr&&!memtarget) {
3263 gen_tlb_addr_w(temp,map);
3266 if (opcode[i]==0x2C||opcode[i]==0x2D) { // SDL/SDR
3267 temp2=get_reg(i_regs->regmap,FTEMP);
3268 if(!rs2[i]) temp2=th=tl;
3271 emit_testimm(temp,2);
3274 emit_testimm(temp,1);
3278 if (opcode[i]==0x2A) { // SWL
3279 emit_writeword_indexed(tl,0,temp);
3281 if (opcode[i]==0x2E) { // SWR
3282 emit_writebyte_indexed(tl,3,temp);
3284 if (opcode[i]==0x2C) { // SDL
3285 emit_writeword_indexed(th,0,temp);
3286 if(rs2[i]) emit_mov(tl,temp2);
3288 if (opcode[i]==0x2D) { // SDR
3289 emit_writebyte_indexed(tl,3,temp);
3290 if(rs2[i]) emit_shldimm(th,tl,24,temp2);
3295 set_jump_target(case1,(int)out);
3296 if (opcode[i]==0x2A) { // SWL
3297 // Write 3 msb into three least significant bytes
3298 if(rs2[i]) emit_rorimm(tl,8,tl);
3299 emit_writehword_indexed(tl,-1,temp);
3300 if(rs2[i]) emit_rorimm(tl,16,tl);
3301 emit_writebyte_indexed(tl,1,temp);
3302 if(rs2[i]) emit_rorimm(tl,8,tl);
3304 if (opcode[i]==0x2E) { // SWR
3305 // Write two lsb into two most significant bytes
3306 emit_writehword_indexed(tl,1,temp);
3308 if (opcode[i]==0x2C) { // SDL
3309 if(rs2[i]) emit_shrdimm(tl,th,8,temp2);
3310 // Write 3 msb into three least significant bytes
3311 if(rs2[i]) emit_rorimm(th,8,th);
3312 emit_writehword_indexed(th,-1,temp);
3313 if(rs2[i]) emit_rorimm(th,16,th);
3314 emit_writebyte_indexed(th,1,temp);
3315 if(rs2[i]) emit_rorimm(th,8,th);
3317 if (opcode[i]==0x2D) { // SDR
3318 if(rs2[i]) emit_shldimm(th,tl,16,temp2);
3319 // Write two lsb into two most significant bytes
3320 emit_writehword_indexed(tl,1,temp);
3325 set_jump_target(case2,(int)out);
3326 emit_testimm(temp,1);
3329 if (opcode[i]==0x2A) { // SWL
3330 // Write two msb into two least significant bytes
3331 if(rs2[i]) emit_rorimm(tl,16,tl);
3332 emit_writehword_indexed(tl,-2,temp);
3333 if(rs2[i]) emit_rorimm(tl,16,tl);
3335 if (opcode[i]==0x2E) { // SWR
3336 // Write 3 lsb into three most significant bytes
3337 emit_writebyte_indexed(tl,-1,temp);
3338 if(rs2[i]) emit_rorimm(tl,8,tl);
3339 emit_writehword_indexed(tl,0,temp);
3340 if(rs2[i]) emit_rorimm(tl,24,tl);
3342 if (opcode[i]==0x2C) { // SDL
3343 if(rs2[i]) emit_shrdimm(tl,th,16,temp2);
3344 // Write two msb into two least significant bytes
3345 if(rs2[i]) emit_rorimm(th,16,th);
3346 emit_writehword_indexed(th,-2,temp);
3347 if(rs2[i]) emit_rorimm(th,16,th);
3349 if (opcode[i]==0x2D) { // SDR
3350 if(rs2[i]) emit_shldimm(th,tl,8,temp2);
3351 // Write 3 lsb into three most significant bytes
3352 emit_writebyte_indexed(tl,-1,temp);
3353 if(rs2[i]) emit_rorimm(tl,8,tl);
3354 emit_writehword_indexed(tl,0,temp);
3355 if(rs2[i]) emit_rorimm(tl,24,tl);
3360 set_jump_target(case3,(int)out);
3361 if (opcode[i]==0x2A) { // SWL
3362 // Write msb into least significant byte
3363 if(rs2[i]) emit_rorimm(tl,24,tl);
3364 emit_writebyte_indexed(tl,-3,temp);
3365 if(rs2[i]) emit_rorimm(tl,8,tl);
3367 if (opcode[i]==0x2E) { // SWR
3368 // Write entire word
3369 emit_writeword_indexed(tl,-3,temp);
3371 if (opcode[i]==0x2C) { // SDL
3372 if(rs2[i]) emit_shrdimm(tl,th,24,temp2);
3373 // Write msb into least significant byte
3374 if(rs2[i]) emit_rorimm(th,24,th);
3375 emit_writebyte_indexed(th,-3,temp);
3376 if(rs2[i]) emit_rorimm(th,8,th);
3378 if (opcode[i]==0x2D) { // SDR
3379 if(rs2[i]) emit_mov(th,temp2);
3380 // Write entire word
3381 emit_writeword_indexed(tl,-3,temp);
3383 set_jump_target(done0,(int)out);
3384 set_jump_target(done1,(int)out);
3385 set_jump_target(done2,(int)out);
3386 if (opcode[i]==0x2C) { // SDL
3387 emit_testimm(temp,4);
3390 emit_andimm(temp,~3,temp);
3391 emit_writeword_indexed(temp2,4,temp);
3392 set_jump_target(done0,(int)out);
3394 if (opcode[i]==0x2D) { // SDR
3395 emit_testimm(temp,4);
3398 emit_andimm(temp,~3,temp);
3399 emit_writeword_indexed(temp2,-4,temp);
3400 set_jump_target(done0,(int)out);
3403 add_stub(STORELR_STUB,jaddr,(int)out,0,(int)i_regs,rs2[i],ccadj[i],reglist);
3406 int map=get_reg(i_regs->regmap,ROREG);
3407 if(map<0) map=HOST_TEMPREG;
3408 gen_orig_addr_w(temp,map);
3410 emit_addimm_no_flags((u_int)0x80000000-(u_int)rdram,temp);
3412 #if defined(HOST_IMM8)
3413 int ir=get_reg(i_regs->regmap,INVCP);
3415 emit_cmpmem_indexedsr12_reg(ir,temp,1);
3417 emit_cmpmem_indexedsr12_imm((int)invalid_code,temp,1);
3419 #if defined(HAVE_CONDITIONAL_CALL) && !defined(DESTRUCTIVE_SHIFT)
3420 emit_callne(invalidate_addr_reg[temp]);
3424 add_stub(INVCODE_STUB,jaddr2,(int)out,reglist|(1<<HOST_CCREG),temp,0,0,0);
3429 //save_regs(0x100f);
3430 emit_readword((int)&last_count,ECX);
3431 if(get_reg(i_regs->regmap,CCREG)<0)
3432 emit_loadreg(CCREG,HOST_CCREG);
3433 emit_add(HOST_CCREG,ECX,HOST_CCREG);
3434 emit_addimm(HOST_CCREG,2*ccadj[i],HOST_CCREG);
3435 emit_writeword(HOST_CCREG,(int)&Count);
3436 emit_call((int)memdebug);
3438 //restore_regs(0x100f);
3442 static void c1ls_assemble(int i,struct regstat *i_regs)
3449 int jaddr,jaddr2=0,jaddr3,type;
3450 int agr=AGEN1+(i&1);
3452 th=get_reg(i_regs->regmap,FTEMP|64);
3453 tl=get_reg(i_regs->regmap,FTEMP);
3454 s=get_reg(i_regs->regmap,rs1[i]);
3455 temp=get_reg(i_regs->regmap,agr);
3456 if(temp<0) temp=get_reg(i_regs->regmap,-1);
3461 for(hr=0;hr<HOST_REGS;hr++) {
3462 if(i_regs->regmap[hr]>=0) reglist|=1<<hr;
3464 if(i_regs->regmap[HOST_CCREG]==CCREG) reglist&=~(1<<HOST_CCREG);
3465 if (opcode[i]==0x31||opcode[i]==0x35) // LWC1/LDC1
3467 // Loads use a temporary register which we need to save
3470 if (opcode[i]==0x39||opcode[i]==0x3D) // SWC1/SDC1
3474 //if(s<0) emit_loadreg(rs1[i],ar); //address_generation does this now
3475 //else c=(i_regs->wasconst>>s)&1;
3476 if(s>=0) c=(i_regs->wasconst>>s)&1;
3477 // Check cop1 unusable
3479 signed char rs=get_reg(i_regs->regmap,CSREG);
3481 emit_testimm(rs,0x20000000);
3484 add_stub(FP_STUB,jaddr,(int)out,i,rs,(int)i_regs,is_delayslot,0);
3487 if (opcode[i]==0x39) { // SWC1 (get float address)
3488 emit_readword((int)®_cop1_simple[(source[i]>>16)&0x1f],tl);
3490 if (opcode[i]==0x3D) { // SDC1 (get double address)
3491 emit_readword((int)®_cop1_double[(source[i]>>16)&0x1f],tl);
3493 // Generate address + offset
3496 if (!c||opcode[i]==0x39||opcode[i]==0x3D) // SWC1/SDC1
3498 map=get_reg(i_regs->regmap,ROREG);
3499 if(map<0) emit_loadreg(ROREG,map=HOST_TEMPREG);
3503 emit_cmpimm(offset||c||s<0?ar:s,0x800000);
3507 map=get_reg(i_regs->regmap,TLREG);
3508 int cache=get_reg(i_regs->regmap,MMREG);
3511 if (opcode[i]==0x31||opcode[i]==0x35) { // LWC1/LDC1
3512 map=do_tlb_r(offset||c||s<0?ar:s,ar,map,cache,0,-1,-1,c,constmap[i][s]+offset);
3514 if (opcode[i]==0x39||opcode[i]==0x3D) { // SWC1/SDC1
3515 map=do_tlb_w(offset||c||s<0?ar:s,ar,map,cache,0,c,constmap[i][s]+offset);
3518 if (opcode[i]==0x39) { // SWC1 (read float)
3519 emit_readword_indexed(0,tl,tl);
3521 if (opcode[i]==0x3D) { // SDC1 (read double)
3522 emit_readword_indexed(4,tl,th);
3523 emit_readword_indexed(0,tl,tl);
3525 if (opcode[i]==0x31) { // LWC1 (get target address)
3526 emit_readword((int)®_cop1_simple[(source[i]>>16)&0x1f],temp);
3528 if (opcode[i]==0x35) { // LDC1 (get target address)
3529 emit_readword((int)®_cop1_double[(source[i]>>16)&0x1f],temp);
3536 else if(((signed int)(constmap[i][s]+offset))>=(signed int)0x80800000) {
3538 emit_jmp(0); // inline_readstub/inline_writestub? Very rare case
3540 #ifdef DESTRUCTIVE_SHIFT
3541 if (opcode[i]==0x39||opcode[i]==0x3D) { // SWC1/SDC1
3542 if(!offset&&!c&&s>=0) emit_mov(s,ar);
3546 if (opcode[i]==0x31||opcode[i]==0x35) { // LWC1/LDC1
3547 do_tlb_r_branch(map,c,constmap[i][s]+offset,&jaddr2);
3549 if (opcode[i]==0x39||opcode[i]==0x3D) { // SWC1/SDC1
3550 do_tlb_w_branch(map,c,constmap[i][s]+offset,&jaddr2);
3553 if (opcode[i]==0x31) { // LWC1
3554 //if(s>=0&&!c&&!offset) emit_mov(s,tl);
3555 //gen_tlb_addr_r(ar,map);
3556 //emit_readword_indexed((int)rdram-0x80000000,tl,tl);
3557 #ifdef HOST_IMM_ADDR32
3558 if(c) emit_readword_tlb(constmap[i][s]+offset,map,tl);
3561 emit_readword_indexed_tlb(0,offset||c||s<0?tl:s,map,tl);
3564 if (opcode[i]==0x35) { // LDC1
3566 //if(s>=0&&!c&&!offset) emit_mov(s,tl);
3567 //gen_tlb_addr_r(ar,map);
3568 //emit_readword_indexed((int)rdram-0x80000000,tl,th);
3569 //emit_readword_indexed((int)rdram-0x7FFFFFFC,tl,tl);
3570 #ifdef HOST_IMM_ADDR32
3571 if(c) emit_readdword_tlb(constmap[i][s]+offset,map,th,tl);
3574 emit_readdword_indexed_tlb(0,offset||c||s<0?tl:s,map,th,tl);
3577 if (opcode[i]==0x39) { // SWC1
3578 //emit_writeword_indexed(tl,(int)rdram-0x80000000,temp);
3579 emit_writeword_indexed_tlb(tl,0,offset||c||s<0?temp:s,map,temp);
3582 if (opcode[i]==0x3D) { // SDC1
3584 //emit_writeword_indexed(th,(int)rdram-0x80000000,temp);
3585 //emit_writeword_indexed(tl,(int)rdram-0x7FFFFFFC,temp);
3586 emit_writedword_indexed_tlb(th,tl,0,offset||c||s<0?temp:s,map,temp);
3590 if (opcode[i]==0x39||opcode[i]==0x3D) { // SWC1/SDC1
3591 #ifndef DESTRUCTIVE_SHIFT
3592 temp=offset||c||s<0?ar:s;
3594 #if defined(HOST_IMM8)
3595 int ir=get_reg(i_regs->regmap,INVCP);
3597 emit_cmpmem_indexedsr12_reg(ir,temp,1);
3599 emit_cmpmem_indexedsr12_imm((int)invalid_code,temp,1);
3601 #if defined(HAVE_CONDITIONAL_CALL) && !defined(DESTRUCTIVE_SHIFT)
3602 emit_callne(invalidate_addr_reg[temp]);
3606 add_stub(INVCODE_STUB,jaddr3,(int)out,reglist|(1<<HOST_CCREG),temp,0,0,0);
3610 if(jaddr2) add_stub(type,jaddr2,(int)out,i,offset||c||s<0?ar:s,(int)i_regs,ccadj[i],reglist);
3611 if (opcode[i]==0x31) { // LWC1 (write float)
3612 emit_writeword_indexed(tl,0,temp);
3614 if (opcode[i]==0x35) { // LDC1 (write double)
3615 emit_writeword_indexed(th,4,temp);
3616 emit_writeword_indexed(tl,0,temp);
3618 //if(opcode[i]==0x39)
3619 /*if(opcode[i]==0x39||opcode[i]==0x31)
3622 emit_readword((int)&last_count,ECX);
3623 if(get_reg(i_regs->regmap,CCREG)<0)
3624 emit_loadreg(CCREG,HOST_CCREG);
3625 emit_add(HOST_CCREG,ECX,HOST_CCREG);
3626 emit_addimm(HOST_CCREG,2*ccadj[i],HOST_CCREG);
3627 emit_writeword(HOST_CCREG,(int)&Count);
3628 emit_call((int)memdebug);
3633 #ifndef multdiv_assemble
3634 void multdiv_assemble(int i,struct regstat *i_regs)
3636 DebugMessage(M64MSG_ERROR, "Need multdiv_assemble for this architecture.");
3641 static void mov_assemble(int i,struct regstat *i_regs)
3643 //if(opcode2[i]==0x10||opcode2[i]==0x12) { // MFHI/MFLO
3644 //if(opcode2[i]==0x11||opcode2[i]==0x13) { // MTHI/MTLO
3646 signed char sh,sl,th,tl;
3647 th=get_reg(i_regs->regmap,rt1[i]|64);
3648 tl=get_reg(i_regs->regmap,rt1[i]);
3651 sh=get_reg(i_regs->regmap,rs1[i]|64);
3652 sl=get_reg(i_regs->regmap,rs1[i]);
3653 if(sl>=0) emit_mov(sl,tl);
3654 else emit_loadreg(rs1[i],tl);
3656 if(sh>=0) emit_mov(sh,th);
3657 else emit_loadreg(rs1[i]|64,th);
3663 #ifndef fconv_assemble
3664 void fconv_assemble(int i,struct regstat *i_regs)
3666 DebugMessage(M64MSG_ERROR, "Need fconv_assemble for this architecture.");
3672 static void float_assemble(int i,struct regstat *i_regs)
3674 DebugMessage(M64MSG_ERROR, "Need float_assemble for this architecture.");
3679 static void syscall_assemble(int i,struct regstat *i_regs)
3681 signed char ccreg=get_reg(i_regs->regmap,CCREG);
3682 assert(ccreg==HOST_CCREG);
3683 assert(!is_delayslot);
3684 emit_movimm(start+i*4,EAX); // Get PC
3685 emit_addimm(HOST_CCREG,CLOCK_DIVIDER*ccadj[i],HOST_CCREG); // CHECK: is this right? There should probably be an extra cycle...
3686 emit_jmp((int)jump_syscall);
3689 static void ds_assemble(int i,struct regstat *i_regs)
3694 alu_assemble(i,i_regs);break;
3696 imm16_assemble(i,i_regs);break;
3698 shift_assemble(i,i_regs);break;
3700 shiftimm_assemble(i,i_regs);break;
3702 load_assemble(i,i_regs);break;
3704 loadlr_assemble(i,i_regs);break;
3706 store_assemble(i,i_regs);break;
3708 storelr_assemble(i,i_regs);break;
3710 cop0_assemble(i,i_regs);break;
3712 cop1_assemble(i,i_regs);break;
3714 c1ls_assemble(i,i_regs);break;
3716 fconv_assemble(i,i_regs);break;
3718 float_assemble(i,i_regs);break;
3720 fcomp_assemble(i,i_regs);break;
3722 multdiv_assemble(i,i_regs);break;
3724 mov_assemble(i,i_regs);break;
3732 DebugMessage(M64MSG_VERBOSE, "Jump in the delay slot. This is probably a bug.");
3737 // Is the branch target a valid internal jump?
3738 static int internal_branch(uint64_t i_is32,int addr)
3740 if(addr&1) return 0; // Indirect (register) jump
3741 if(addr>=start && addr<start+slen*4-4)
3743 int t=(addr-start)>>2;
3744 // Delay slots are not valid branch targets
3745 //if(t>0&&(itype[t-1]==RJUMP||itype[t-1]==UJUMP||itype[t-1]==CJUMP||itype[t-1]==SJUMP||itype[t-1]==FJUMP)) return 0;
3746 // 64 -> 32 bit transition requires a recompile
3747 /*if(is32[t]&~unneeded_reg_upper[t]&~i_is32)
3749 if(requires_32bit[t]&~i_is32) DebugMessage(M64MSG_VERBOSE, "optimizable: no");
3750 else DebugMessage(M64MSG_VERBOSE, "optimizable: yes");
3752 //if(is32[t]&~unneeded_reg_upper[t]&~i_is32) return 0;
3753 if(requires_32bit[t]&~i_is32) return 0;
3759 #ifndef wb_invalidate
3760 static void wb_invalidate(signed char pre[],signed char entry[],uint64_t dirty,uint64_t is32,
3761 uint64_t u,uint64_t uu)
3764 for(hr=0;hr<HOST_REGS;hr++) {
3765 if(hr!=EXCLUDE_REG) {
3766 if(pre[hr]!=entry[hr]) {
3769 if(get_reg(entry,pre[hr])<0) {
3771 if(!((u>>pre[hr])&1)) {
3772 emit_storereg(pre[hr],hr);
3773 if( ((is32>>pre[hr])&1) && !((uu>>pre[hr])&1) ) {
3774 emit_sarimm(hr,31,hr);
3775 emit_storereg(pre[hr]|64,hr);
3779 if(!((uu>>(pre[hr]&63))&1) && !((is32>>(pre[hr]&63))&1)) {
3780 emit_storereg(pre[hr],hr);
3789 // Move from one register to another (no writeback)
3790 for(hr=0;hr<HOST_REGS;hr++) {
3791 if(hr!=EXCLUDE_REG) {
3792 if(pre[hr]!=entry[hr]) {
3793 if(pre[hr]>=0&&(pre[hr]&63)<TEMPREG) {
3795 if((nr=get_reg(entry,pre[hr]))>=0) {
3805 // Load the specified registers
3806 // This only loads the registers given as arguments because
3807 // we don't want to load things that will be overwritten
3808 static void load_regs(signed char entry[],signed char regmap[],int is32,int rs1,int rs2)
3812 for(hr=0;hr<HOST_REGS;hr++) {
3813 if(hr!=EXCLUDE_REG&®map[hr]>=0) {
3814 if(entry[hr]!=regmap[hr]) {
3815 if(regmap[hr]==rs1||regmap[hr]==rs2)
3822 emit_loadreg(regmap[hr],hr);
3829 for(hr=0;hr<HOST_REGS;hr++) {
3830 if(hr!=EXCLUDE_REG&®map[hr]>=0) {
3831 if(entry[hr]!=regmap[hr]) {
3832 if(regmap[hr]-64==rs1||regmap[hr]-64==rs2)
3834 assert(regmap[hr]!=64);
3835 if((is32>>(regmap[hr]&63))&1) {
3836 int lr=get_reg(regmap,regmap[hr]-64);
3838 emit_sarimm(lr,31,hr);
3840 emit_loadreg(regmap[hr],hr);
3844 emit_loadreg(regmap[hr],hr);
3852 // Load registers prior to the start of a loop
3853 // so that they are not loaded within the loop
3854 static void loop_preload(signed char pre[],signed char entry[])
3857 for(hr=0;hr<HOST_REGS;hr++) {
3858 if(hr!=EXCLUDE_REG) {
3859 if(pre[hr]!=entry[hr]) {
3861 if(get_reg(pre,entry[hr])<0) {
3862 assem_debug("loop preload:");
3863 //DebugMessage(M64MSG_VERBOSE, "loop preload: %d",hr);
3867 else if(entry[hr]<TEMPREG)
3869 emit_loadreg(entry[hr],hr);
3871 else if(entry[hr]-64<TEMPREG)
3873 emit_loadreg(entry[hr],hr);
3882 // Generate address for load/store instruction
3883 static void address_generation(int i,struct regstat *i_regs,signed char entry[])
3885 if(itype[i]==LOAD||itype[i]==LOADLR||itype[i]==STORE||itype[i]==STORELR||itype[i]==C1LS) {
3887 int agr=AGEN1+(i&1);
3888 int mgr=MGEN1+(i&1);
3889 if(itype[i]==LOAD) {
3890 ra=get_reg(i_regs->regmap,rt1[i]);
3891 if(ra<0) ra=get_reg(i_regs->regmap,-1);
3894 if(itype[i]==LOADLR) {
3895 ra=get_reg(i_regs->regmap,FTEMP);
3897 if(itype[i]==STORE||itype[i]==STORELR) {
3898 ra=get_reg(i_regs->regmap,agr);
3899 if(ra<0) ra=get_reg(i_regs->regmap,-1);
3901 if(itype[i]==C1LS) {
3902 if (opcode[i]==0x31||opcode[i]==0x35) // LWC1/LDC1
3903 ra=get_reg(i_regs->regmap,FTEMP);
3905 ra=get_reg(i_regs->regmap,agr);
3906 if(ra<0) ra=get_reg(i_regs->regmap,-1);
3909 int rs=get_reg(i_regs->regmap,rs1[i]);
3910 int rm=get_reg(i_regs->regmap,TLREG);
3913 int c=(i_regs->wasconst>>rs)&1;
3915 // Using r0 as a base address
3917 if(!entry||entry[rm]!=mgr) {
3918 generate_map_const(offset,rm);
3919 } // else did it in the previous cycle
3921 if(!entry||entry[ra]!=agr) {
3922 if (opcode[i]==0x22||opcode[i]==0x26) {
3923 emit_movimm(offset&0xFFFFFFFC,ra); // LWL/LWR
3924 }else if (opcode[i]==0x1a||opcode[i]==0x1b) {
3925 emit_movimm(offset&0xFFFFFFF8,ra); // LDL/LDR
3927 emit_movimm(offset,ra);
3929 } // else did it in the previous cycle
3932 if(!entry||entry[ra]!=rs1[i])
3933 emit_loadreg(rs1[i],ra);
3934 //if(!entry||entry[ra]!=rs1[i])
3935 // DebugMessage(M64MSG_VERBOSE, "poor load scheduling!");
3939 if(!entry||entry[rm]!=mgr) {
3940 if(itype[i]==STORE||itype[i]==STORELR||opcode[i]==0x39||opcode[i]==0x3D) {
3941 // Stores to memory go thru the mapper to detect self-modifying
3942 // code, loads don't.
3943 if((unsigned int)(constmap[i][rs]+offset)>=0xC0000000 ||
3944 (unsigned int)(constmap[i][rs]+offset)<0x80800000 )
3945 generate_map_const(constmap[i][rs]+offset,rm);
3947 if((signed int)(constmap[i][rs]+offset)>=(signed int)0xC0000000)
3948 generate_map_const(constmap[i][rs]+offset,rm);
3952 if(rs1[i]!=rt1[i]||itype[i]!=LOAD) {
3953 if(!entry||entry[ra]!=agr) {
3954 if (opcode[i]==0x22||opcode[i]==0x26) { // LWL/LWR
3956 if((signed int)constmap[i][rs]+offset<(signed int)0x80800000)
3957 emit_movimm(((constmap[i][rs]+offset)&0xFFFFFFFC)+(int)rdram-0x80000000,ra);
3960 emit_movimm((constmap[i][rs]+offset)&0xFFFFFFFC,ra);
3961 }else if (opcode[i]==0x1a||opcode[i]==0x1b) { // LDL/LDR
3963 if((signed int)constmap[i][rs]+offset<(signed int)0x80800000)
3964 emit_movimm(((constmap[i][rs]+offset)&0xFFFFFFF8)+(int)rdram-0x80000000,ra);
3967 emit_movimm((constmap[i][rs]+offset)&0xFFFFFFF8,ra);
3969 #ifdef HOST_IMM_ADDR32
3970 if((itype[i]!=LOAD&&opcode[i]!=0x31&&opcode[i]!=0x35) ||
3971 (using_tlb&&((signed int)constmap[i][rs]+offset)>=(signed int)0xC0000000))
3974 if((itype[i]==LOAD||opcode[i]==0x31||opcode[i]==0x35)&&(signed int)constmap[i][rs]+offset<(signed int)0x80800000)
3975 emit_movimm(constmap[i][rs]+offset+(int)rdram-0x80000000,ra);
3978 emit_movimm(constmap[i][rs]+offset,ra);
3980 } // else did it in the previous cycle
3981 } // else load_consts already did it
3983 if(offset&&!c&&rs1[i]) {
3985 emit_addimm(rs,offset,ra);
3987 emit_addimm(ra,offset,ra);
3992 // Preload constants for next instruction
3993 if(itype[i+1]==LOAD||itype[i+1]==LOADLR||itype[i+1]==STORE||itype[i+1]==STORELR||itype[i+1]==C1LS) {
3995 #ifndef HOST_IMM_ADDR32
3997 agr=MGEN1+((i+1)&1);
3998 ra=get_reg(i_regs->regmap,agr);
4000 int rs=get_reg(regs[i+1].regmap,rs1[i+1]);
4001 int offset=imm[i+1];
4002 int c=(regs[i+1].wasconst>>rs)&1;
4004 if(itype[i+1]==STORE||itype[i+1]==STORELR||opcode[i+1]==0x39||opcode[i+1]==0x3D) {
4005 // Stores to memory go thru the mapper to detect self-modifying
4006 // code, loads don't.
4007 if((unsigned int)(constmap[i+1][rs]+offset)>=0xC0000000 ||
4008 (unsigned int)(constmap[i+1][rs]+offset)<0x80800000 )
4009 generate_map_const(constmap[i+1][rs]+offset,ra);
4011 if((signed int)(constmap[i+1][rs]+offset)>=(signed int)0xC0000000)
4012 generate_map_const(constmap[i+1][rs]+offset,ra);
4015 /*else if(rs1[i]==0) {
4016 generate_map_const(offset,ra);
4021 agr=AGEN1+((i+1)&1);
4022 ra=get_reg(i_regs->regmap,agr);
4024 int rs=get_reg(regs[i+1].regmap,rs1[i+1]);
4025 int offset=imm[i+1];
4026 int c=(regs[i+1].wasconst>>rs)&1;
4027 if(c&&(rs1[i+1]!=rt1[i+1]||itype[i+1]!=LOAD)) {
4028 if (opcode[i+1]==0x22||opcode[i+1]==0x26) { // LWL/LWR
4030 if((signed int)constmap[i+1][rs]+offset<(signed int)0x80800000)
4031 emit_movimm(((constmap[i+1][rs]+offset)&0xFFFFFFFC)+(int)rdram-0x80000000,ra);
4034 emit_movimm((constmap[i+1][rs]+offset)&0xFFFFFFFC,ra);
4035 }else if (opcode[i+1]==0x1a||opcode[i+1]==0x1b) { // LDL/LDR
4037 if((signed int)constmap[i+1][rs]+offset<(signed int)0x80800000)
4038 emit_movimm(((constmap[i+1][rs]+offset)&0xFFFFFFF8)+(int)rdram-0x80000000,ra);
4041 emit_movimm((constmap[i+1][rs]+offset)&0xFFFFFFF8,ra);
4043 #ifdef HOST_IMM_ADDR32
4044 if((itype[i+1]!=LOAD&&opcode[i+1]!=0x31&&opcode[i+1]!=0x35) ||
4045 (using_tlb&&((signed int)constmap[i+1][rs]+offset)>=(signed int)0xC0000000))
4048 if((itype[i+1]==LOAD||opcode[i+1]==0x31||opcode[i+1]==0x35)&&(signed int)constmap[i+1][rs]+offset<(signed int)0x80800000)
4049 emit_movimm(constmap[i+1][rs]+offset+(int)rdram-0x80000000,ra);
4052 emit_movimm(constmap[i+1][rs]+offset,ra);
4055 else if(rs1[i+1]==0) {
4056 // Using r0 as a base address
4057 if (opcode[i+1]==0x22||opcode[i+1]==0x26) {
4058 emit_movimm(offset&0xFFFFFFFC,ra); // LWL/LWR
4059 }else if (opcode[i+1]==0x1a||opcode[i+1]==0x1b) {
4060 emit_movimm(offset&0xFFFFFFF8,ra); // LDL/LDR
4062 emit_movimm(offset,ra);
4069 static int get_final_value(int hr, int i, int *value)
4071 int reg=regs[i].regmap[hr];
4073 if(regs[i+1].regmap[hr]!=reg) break;
4074 if(!((regs[i+1].isconst>>hr)&1)) break;
4079 if(itype[i]==UJUMP||itype[i]==RJUMP||itype[i]==CJUMP||itype[i]==SJUMP) {
4080 *value=constmap[i][hr];
4084 if(itype[i+1]==UJUMP||itype[i+1]==RJUMP||itype[i+1]==CJUMP||itype[i+1]==SJUMP) {
4085 // Load in delay slot, out-of-order execution
4086 if(itype[i+2]==LOAD&&rs1[i+2]==reg&&rt1[i+2]==reg&&((regs[i+1].wasconst>>hr)&1))
4088 #ifdef HOST_IMM_ADDR32
4089 if(!using_tlb||((signed int)constmap[i][hr]+imm[i+2])<(signed int)0xC0000000) return 0;
4092 if((signed int)constmap[i][hr]+imm[i+2]<(signed int)0x80800000)
4093 *value=constmap[i][hr]+imm[i+2]+(int)rdram-0x80000000;
4096 // Precompute load address
4097 *value=constmap[i][hr]+imm[i+2];
4101 if(itype[i+1]==LOAD&&rs1[i+1]==reg&&rt1[i+1]==reg)
4103 #ifdef HOST_IMM_ADDR32
4104 if(!using_tlb||((signed int)constmap[i][hr]+imm[i+1])<(signed int)0xC0000000) return 0;
4107 if((signed int)constmap[i][hr]+imm[i+1]<(signed int)0x80800000)
4108 *value=constmap[i][hr]+imm[i+1]+(int)rdram-0x80000000;
4111 // Precompute load address
4112 *value=constmap[i][hr]+imm[i+1];
4113 //DebugMessage(M64MSG_VERBOSE, "c=%x imm=%x",(int)constmap[i][hr],imm[i+1]);
4118 *value=constmap[i][hr];
4119 //DebugMessage(M64MSG_VERBOSE, "c=%x",(int)constmap[i][hr]);
4120 if(i==slen-1) return 1;
4122 return !((unneeded_reg[i+1]>>reg)&1);
4124 return !((unneeded_reg_upper[i+1]>>reg)&1);
4128 // Load registers with known constants
4129 static void load_consts(signed char pre[],signed char regmap[],int is32,int i)
4133 for(hr=0;hr<HOST_REGS;hr++) {
4134 if(hr!=EXCLUDE_REG&®map[hr]>=0) {
4135 //if(entry[hr]!=regmap[hr]) {
4136 if(i==0||!((regs[i-1].isconst>>hr)&1)||pre[hr]!=regmap[hr]||bt[i]) {
4137 if(((regs[i].isconst>>hr)&1)&®map[hr]<64&®map[hr]>0) {
4139 if(get_final_value(hr,i,&value)) {
4144 emit_movimm(value,hr);
4152 for(hr=0;hr<HOST_REGS;hr++) {
4153 if(hr!=EXCLUDE_REG&®map[hr]>=0) {
4154 //if(entry[hr]!=regmap[hr]) {
4155 if(i==0||!((regs[i-1].isconst>>hr)&1)||pre[hr]!=regmap[hr]||bt[i]) {
4156 if(((regs[i].isconst>>hr)&1)&®map[hr]>64) {
4157 if((is32>>(regmap[hr]&63))&1) {
4158 int lr=get_reg(regmap,regmap[hr]-64);
4160 emit_sarimm(lr,31,hr);
4165 if(get_final_value(hr,i,&value)) {
4170 emit_movimm(value,hr);
4179 static void load_all_consts(signed char regmap[],int is32,u_int dirty,int i)
4183 for(hr=0;hr<HOST_REGS;hr++) {
4184 if(hr!=EXCLUDE_REG&®map[hr]>=0&&((dirty>>hr)&1)) {
4185 if(((regs[i].isconst>>hr)&1)&®map[hr]<64&®map[hr]>0) {
4186 int value=constmap[i][hr];
4191 emit_movimm(value,hr);
4197 for(hr=0;hr<HOST_REGS;hr++) {
4198 if(hr!=EXCLUDE_REG&®map[hr]>=0&&((dirty>>hr)&1)) {
4199 if(((regs[i].isconst>>hr)&1)&®map[hr]>64) {
4200 if((is32>>(regmap[hr]&63))&1) {
4201 int lr=get_reg(regmap,regmap[hr]-64);
4203 emit_sarimm(lr,31,hr);
4207 int value=constmap[i][hr];
4212 emit_movimm(value,hr);
4220 // Write out all dirty registers (except cycle count)
4221 static void wb_dirtys(signed char i_regmap[],uint64_t i_is32,uint64_t i_dirty)
4224 for(hr=0;hr<HOST_REGS;hr++) {
4225 if(hr!=EXCLUDE_REG) {
4226 if(i_regmap[hr]>0) {
4227 if(i_regmap[hr]!=CCREG) {
4228 if((i_dirty>>hr)&1) {
4229 if(i_regmap[hr]<64) {
4230 emit_storereg(i_regmap[hr],hr);
4231 if( ((i_is32>>i_regmap[hr])&1) ) {
4232 #ifdef DESTRUCTIVE_WRITEBACK
4233 emit_sarimm(hr,31,hr);
4234 emit_storereg(i_regmap[hr]|64,hr);
4236 emit_sarimm(hr,31,HOST_TEMPREG);
4237 emit_storereg(i_regmap[hr]|64,HOST_TEMPREG);
4241 if( !((i_is32>>(i_regmap[hr]&63))&1) ) {
4242 emit_storereg(i_regmap[hr],hr);
4251 // Write out dirty registers that we need to reload (pair with load_needed_regs)
4252 // This writes the registers not written by store_regs_bt
4253 static void wb_needed_dirtys(signed char i_regmap[],uint64_t i_is32,uint64_t i_dirty,int addr)
4256 int t=(addr-start)>>2;
4257 for(hr=0;hr<HOST_REGS;hr++) {
4258 if(hr!=EXCLUDE_REG) {
4259 if(i_regmap[hr]>0) {
4260 if(i_regmap[hr]!=CCREG) {
4261 if(i_regmap[hr]==regs[t].regmap_entry[hr] && ((regs[t].dirty>>hr)&1) && !(((i_is32&~regs[t].was32&~unneeded_reg_upper[t])>>(i_regmap[hr]&63))&1)) {
4262 if((i_dirty>>hr)&1) {
4263 if(i_regmap[hr]<64) {
4264 emit_storereg(i_regmap[hr],hr);
4265 if( ((i_is32>>i_regmap[hr])&1) ) {
4266 #ifdef DESTRUCTIVE_WRITEBACK
4267 emit_sarimm(hr,31,hr);
4268 emit_storereg(i_regmap[hr]|64,hr);
4270 emit_sarimm(hr,31,HOST_TEMPREG);
4271 emit_storereg(i_regmap[hr]|64,HOST_TEMPREG);
4275 if( !((i_is32>>(i_regmap[hr]&63))&1) ) {
4276 emit_storereg(i_regmap[hr],hr);
4287 // Load all registers (except cycle count)
4288 static void load_all_regs(signed char i_regmap[])
4291 for(hr=0;hr<HOST_REGS;hr++) {
4292 if(hr!=EXCLUDE_REG) {
4293 if(i_regmap[hr]==0) {
4297 if(i_regmap[hr]>0 && (i_regmap[hr]&63)<TEMPREG && i_regmap[hr]!=CCREG)
4299 emit_loadreg(i_regmap[hr],hr);
4305 // Load all current registers also needed by next instruction
4306 static void load_needed_regs(signed char i_regmap[],signed char next_regmap[])
4309 for(hr=0;hr<HOST_REGS;hr++) {
4310 if(hr!=EXCLUDE_REG) {
4311 if(get_reg(next_regmap,i_regmap[hr])>=0) {
4312 if(i_regmap[hr]==0) {
4316 if(i_regmap[hr]>0 && (i_regmap[hr]&63)<TEMPREG && i_regmap[hr]!=CCREG)
4318 emit_loadreg(i_regmap[hr],hr);
4325 // Load all regs, storing cycle count if necessary
4326 static void load_regs_entry(int t)
4329 if(is_ds[t]) emit_addimm(HOST_CCREG,CLOCK_DIVIDER,HOST_CCREG);
4330 else if(ccadj[t]) emit_addimm(HOST_CCREG,-ccadj[t]*CLOCK_DIVIDER,HOST_CCREG);
4331 if(regs[t].regmap_entry[HOST_CCREG]!=CCREG) {
4332 emit_storereg(CCREG,HOST_CCREG);
4335 for(hr=0;hr<HOST_REGS;hr++) {
4336 if(regs[t].regmap_entry[hr]>=0&®s[t].regmap_entry[hr]<TEMPREG) {
4337 if(regs[t].regmap_entry[hr]==0) {
4340 else if(regs[t].regmap_entry[hr]!=CCREG)
4342 emit_loadreg(regs[t].regmap_entry[hr],hr);
4347 for(hr=0;hr<HOST_REGS;hr++) {
4348 if(regs[t].regmap_entry[hr]>=64&®s[t].regmap_entry[hr]<TEMPREG+64) {
4349 assert(regs[t].regmap_entry[hr]!=64);
4350 if((regs[t].was32>>(regs[t].regmap_entry[hr]&63))&1) {
4351 int lr=get_reg(regs[t].regmap_entry,regs[t].regmap_entry[hr]-64);
4353 emit_loadreg(regs[t].regmap_entry[hr],hr);
4357 emit_sarimm(lr,31,hr);
4362 emit_loadreg(regs[t].regmap_entry[hr],hr);
4368 // Store dirty registers prior to branch
4369 static void store_regs_bt(signed char i_regmap[],uint64_t i_is32,uint64_t i_dirty,int addr)
4371 if(internal_branch(i_is32,addr))
4373 int t=(addr-start)>>2;
4375 for(hr=0;hr<HOST_REGS;hr++) {
4376 if(hr!=EXCLUDE_REG) {
4377 if(i_regmap[hr]>0 && i_regmap[hr]!=CCREG) {
4378 if(i_regmap[hr]!=regs[t].regmap_entry[hr] || !((regs[t].dirty>>hr)&1) || (((i_is32&~regs[t].was32&~unneeded_reg_upper[t])>>(i_regmap[hr]&63))&1)) {
4379 if((i_dirty>>hr)&1) {
4380 if(i_regmap[hr]<64) {
4381 if(!((unneeded_reg[t]>>i_regmap[hr])&1)) {
4382 emit_storereg(i_regmap[hr],hr);
4383 if( ((i_is32>>i_regmap[hr])&1) && !((unneeded_reg_upper[t]>>i_regmap[hr])&1) ) {
4384 #ifdef DESTRUCTIVE_WRITEBACK
4385 emit_sarimm(hr,31,hr);
4386 emit_storereg(i_regmap[hr]|64,hr);
4388 emit_sarimm(hr,31,HOST_TEMPREG);
4389 emit_storereg(i_regmap[hr]|64,HOST_TEMPREG);
4394 if( !((i_is32>>(i_regmap[hr]&63))&1) && !((unneeded_reg_upper[t]>>(i_regmap[hr]&63))&1) ) {
4395 emit_storereg(i_regmap[hr],hr);
4406 // Branch out of this block, write out all dirty regs
4407 wb_dirtys(i_regmap,i_is32,i_dirty);
4411 // Load all needed registers for branch target
4412 static void load_regs_bt(signed char i_regmap[],uint64_t i_is32,uint64_t i_dirty,int addr)
4414 //if(addr>=start && addr<(start+slen*4))
4415 if(internal_branch(i_is32,addr))
4417 int t=(addr-start)>>2;
4419 // Store the cycle count before loading something else
4420 if(i_regmap[HOST_CCREG]!=CCREG) {
4421 assert(i_regmap[HOST_CCREG]==-1);
4423 if(regs[t].regmap_entry[HOST_CCREG]!=CCREG) {
4424 emit_storereg(CCREG,HOST_CCREG);
4427 for(hr=0;hr<HOST_REGS;hr++) {
4428 if(hr!=EXCLUDE_REG&®s[t].regmap_entry[hr]>=0&®s[t].regmap_entry[hr]<TEMPREG) {
4429 #ifdef DESTRUCTIVE_WRITEBACK
4430 if(i_regmap[hr]!=regs[t].regmap_entry[hr] || ( !((regs[t].dirty>>hr)&1) && ((i_dirty>>hr)&1) && (((i_is32&~unneeded_reg_upper[t])>>i_regmap[hr])&1) ) || (((i_is32&~regs[t].was32&~unneeded_reg_upper[t])>>(i_regmap[hr]&63))&1)) {
4432 if(i_regmap[hr]!=regs[t].regmap_entry[hr] ) {
4434 if(regs[t].regmap_entry[hr]==0) {
4437 else if(regs[t].regmap_entry[hr]!=CCREG)
4439 emit_loadreg(regs[t].regmap_entry[hr],hr);
4445 for(hr=0;hr<HOST_REGS;hr++) {
4446 if(hr!=EXCLUDE_REG&®s[t].regmap_entry[hr]>=64&®s[t].regmap_entry[hr]<TEMPREG+64) {
4447 if(i_regmap[hr]!=regs[t].regmap_entry[hr]) {
4448 assert(regs[t].regmap_entry[hr]!=64);
4449 if((i_is32>>(regs[t].regmap_entry[hr]&63))&1) {
4450 int lr=get_reg(regs[t].regmap_entry,regs[t].regmap_entry[hr]-64);
4452 emit_loadreg(regs[t].regmap_entry[hr],hr);
4456 emit_sarimm(lr,31,hr);
4461 emit_loadreg(regs[t].regmap_entry[hr],hr);
4464 else if((i_is32>>(regs[t].regmap_entry[hr]&63))&1) {
4465 int lr=get_reg(regs[t].regmap_entry,regs[t].regmap_entry[hr]-64);
4467 emit_sarimm(lr,31,hr);
4474 static int match_bt(signed char i_regmap[],uint64_t i_is32,uint64_t i_dirty,int addr)
4476 if(addr>=start && addr<start+slen*4-4)
4478 int t=(addr-start)>>2;
4480 if(regs[t].regmap_entry[HOST_CCREG]!=CCREG) return 0;
4481 for(hr=0;hr<HOST_REGS;hr++)
4485 if(i_regmap[hr]!=regs[t].regmap_entry[hr])
4487 if(regs[t].regmap_entry[hr]>=0&&(regs[t].regmap_entry[hr]|64)<TEMPREG+64)
4494 if(i_regmap[hr]<TEMPREG)
4496 if(!((unneeded_reg[t]>>i_regmap[hr])&1))
4499 else if(i_regmap[hr]>=64&&i_regmap[hr]<TEMPREG+64)
4501 if(!((unneeded_reg_upper[t]>>(i_regmap[hr]&63))&1))
4506 else // Same register but is it 32-bit or dirty?
4509 if(!((regs[t].dirty>>hr)&1))
4513 if(!((unneeded_reg[t]>>i_regmap[hr])&1))
4515 //DebugMessage(M64MSG_VERBOSE, "%x: dirty no match",addr);
4520 if((((regs[t].was32^i_is32)&~unneeded_reg_upper[t])>>(i_regmap[hr]&63))&1)
4522 //DebugMessage(M64MSG_VERBOSE, "%x: is32 no match",addr);
4528 //if(is32[t]&~unneeded_reg_upper[t]&~i_is32) return 0;
4529 if(requires_32bit[t]&~i_is32) return 0;
4530 // Delay slots are not valid branch targets
4531 //if(t>0&&(itype[t-1]==RJUMP||itype[t-1]==UJUMP||itype[t-1]==CJUMP||itype[t-1]==SJUMP||itype[t-1]==FJUMP)) return 0;
4532 // Delay slots require additional processing, so do not match
4533 if(is_ds[t]) return 0;
4538 for(hr=0;hr<HOST_REGS;hr++)
4544 if(hr!=HOST_CCREG||i_regmap[hr]!=CCREG)
4558 // Used when a branch jumps into the delay slot of another branch
4559 static void ds_assemble_entry(int i)
4561 int t=(ba[i]-start)>>2;
4562 if(!instr_addr[t]) instr_addr[t]=(u_int)out;
4563 assem_debug("Assemble delay slot at %x",ba[i]);
4565 if(regs[t].regmap_entry[HOST_CCREG]==CCREG&®s[t].regmap[HOST_CCREG]!=CCREG)
4566 wb_register(CCREG,regs[t].regmap_entry,regs[t].wasdirty,regs[t].was32);
4567 load_regs(regs[t].regmap_entry,regs[t].regmap,regs[t].was32,rs1[t],rs2[t]);
4568 address_generation(t,®s[t],regs[t].regmap_entry);
4569 if(itype[t]==LOAD||itype[t]==LOADLR||itype[t]==STORE||itype[t]==STORELR||itype[t]==C1LS)
4570 load_regs(regs[t].regmap_entry,regs[t].regmap,regs[t].was32,MMREG,ROREG);
4571 if(itype[t]==STORE||itype[t]==STORELR||(opcode[t]&0x3b)==0x39)
4572 load_regs(regs[t].regmap_entry,regs[t].regmap,regs[t].was32,INVCP,INVCP);
4577 alu_assemble(t,®s[t]);break;
4579 imm16_assemble(t,®s[t]);break;
4581 shift_assemble(t,®s[t]);break;
4583 shiftimm_assemble(t,®s[t]);break;
4585 load_assemble(t,®s[t]);break;
4587 loadlr_assemble(t,®s[t]);break;
4589 store_assemble(t,®s[t]);break;
4591 storelr_assemble(t,®s[t]);break;
4593 cop0_assemble(t,®s[t]);break;
4595 cop1_assemble(t,®s[t]);break;
4597 c1ls_assemble(t,®s[t]);break;
4599 fconv_assemble(t,®s[t]);break;
4601 float_assemble(t,®s[t]);break;
4603 fcomp_assemble(t,®s[t]);break;
4605 multdiv_assemble(t,®s[t]);break;
4607 mov_assemble(t,®s[t]);break;
4615 DebugMessage(M64MSG_VERBOSE, "Jump in the delay slot. This is probably a bug.");
4617 store_regs_bt(regs[t].regmap,regs[t].is32,regs[t].dirty,ba[i]+4);
4618 load_regs_bt(regs[t].regmap,regs[t].is32,regs[t].dirty,ba[i]+4);
4619 if(internal_branch(regs[t].is32,ba[i]+4))
4620 assem_debug("branch: internal");
4622 assem_debug("branch: external");
4623 assert(internal_branch(regs[t].is32,ba[i]+4));
4624 add_to_linker((int)out,ba[i]+4,internal_branch(regs[t].is32,ba[i]+4));
4628 static void do_cc(int i,signed char i_regmap[],int *adj,int addr,int taken,int invert)
4637 //if(ba[i]>=start && ba[i]<(start+slen*4))
4638 if(internal_branch(branch_regs[i].is32,ba[i]))
4640 int t=(ba[i]-start)>>2;
4641 if(is_ds[t]) *adj=-1; // Branch into delay slot adds an extra cycle
4649 if(taken==TAKEN && i==(ba[i]-start)>>2 && source[i+1]==0) {
4651 if(count&1) emit_addimm_and_set_flags(2*(count+2),HOST_CCREG);
4653 //emit_subfrommem(&idlecount,HOST_CCREG); // Count idle cycles
4654 emit_andimm(HOST_CCREG,3,HOST_CCREG);
4658 else if(*adj==0||invert) {
4659 emit_addimm_and_set_flags(CLOCK_DIVIDER*(count+2),HOST_CCREG);
4665 emit_cmpimm(HOST_CCREG,-CLOCK_DIVIDER*(count+2));
4669 add_stub(CC_STUB,jaddr,idle?idle:(int)out,(*adj==0||invert||idle)?0:(count+2),i,addr,taken,0);
4672 static void do_ccstub(int n)
4675 assem_debug("do_ccstub %x",start+stubs[n][4]*4);
4676 set_jump_target(stubs[n][1],(int)out);
4678 if(stubs[n][6]==NULLDS) {
4679 // Delay slot instruction is nullified ("likely" branch)
4680 wb_dirtys(regs[i].regmap,regs[i].is32,regs[i].dirty);
4682 else if(stubs[n][6]!=TAKEN) {
4683 wb_dirtys(branch_regs[i].regmap,branch_regs[i].is32,branch_regs[i].dirty);
4686 if(internal_branch(branch_regs[i].is32,ba[i]))
4687 wb_needed_dirtys(branch_regs[i].regmap,branch_regs[i].is32,branch_regs[i].dirty,ba[i]);
4691 // Save PC as return address
4692 emit_movimm(stubs[n][5],EAX);
4693 emit_writeword(EAX,(int)&pcaddr);
4697 // Return address depends on which way the branch goes
4698 if(itype[i]==CJUMP||itype[i]==SJUMP||itype[i]==FJUMP)
4700 int s1l=get_reg(branch_regs[i].regmap,rs1[i]);
4701 int s1h=get_reg(branch_regs[i].regmap,rs1[i]|64);
4702 int s2l=get_reg(branch_regs[i].regmap,rs2[i]);
4703 int s2h=get_reg(branch_regs[i].regmap,rs2[i]|64);
4713 if((branch_regs[i].is32>>rs1[i])&(branch_regs[i].is32>>rs2[i])&1) {
4717 #ifdef DESTRUCTIVE_WRITEBACK
4719 if((branch_regs[i].dirty>>s1l)&(branch_regs[i].is32>>rs1[i])&1)
4720 emit_loadreg(rs1[i],s1l);
4723 if((branch_regs[i].dirty>>s1l)&(branch_regs[i].is32>>rs2[i])&1)
4724 emit_loadreg(rs2[i],s1l);
4727 if((branch_regs[i].dirty>>s2l)&(branch_regs[i].is32>>rs2[i])&1)
4728 emit_loadreg(rs2[i],s2l);
4731 int addr,alt,ntaddr;
4734 if(hr!=EXCLUDE_REG && hr!=HOST_CCREG &&
4735 (branch_regs[i].regmap[hr]&63)!=rs1[i] &&
4736 (branch_regs[i].regmap[hr]&63)!=rs2[i] )
4744 if(hr!=EXCLUDE_REG && hr!=HOST_CCREG &&
4745 (branch_regs[i].regmap[hr]&63)!=rs1[i] &&
4746 (branch_regs[i].regmap[hr]&63)!=rs2[i] )
4752 if((opcode[i]&0x2E)==6) // BLEZ/BGTZ needs another register
4756 if(hr!=EXCLUDE_REG && hr!=HOST_CCREG &&
4757 (branch_regs[i].regmap[hr]&63)!=rs1[i] &&
4758 (branch_regs[i].regmap[hr]&63)!=rs2[i] )
4764 assert(hr<HOST_REGS);
4766 if((opcode[i]&0x2f)==4) // BEQ
4768 #ifdef HAVE_CMOV_IMM
4770 if(s2l>=0) emit_cmp(s1l,s2l);
4771 else emit_test(s1l,s1l);
4772 emit_cmov2imm_e_ne_compact(ba[i],start+i*4+8,addr);
4777 emit_mov2imm_compact(ba[i],addr,start+i*4+8,alt);
4779 if(s2h>=0) emit_cmp(s1h,s2h);
4780 else emit_test(s1h,s1h);
4781 emit_cmovne_reg(alt,addr);
4783 if(s2l>=0) emit_cmp(s1l,s2l);
4784 else emit_test(s1l,s1l);
4785 emit_cmovne_reg(alt,addr);
4788 if((opcode[i]&0x2f)==5) // BNE
4790 #ifdef HAVE_CMOV_IMM
4792 if(s2l>=0) emit_cmp(s1l,s2l);
4793 else emit_test(s1l,s1l);
4794 emit_cmov2imm_e_ne_compact(start+i*4+8,ba[i],addr);
4799 emit_mov2imm_compact(start+i*4+8,addr,ba[i],alt);
4801 if(s2h>=0) emit_cmp(s1h,s2h);
4802 else emit_test(s1h,s1h);
4803 emit_cmovne_reg(alt,addr);
4805 if(s2l>=0) emit_cmp(s1l,s2l);
4806 else emit_test(s1l,s1l);
4807 emit_cmovne_reg(alt,addr);
4810 if((opcode[i]&0x2f)==6) // BLEZ
4812 //emit_movimm(ba[i],alt);
4813 //emit_movimm(start+i*4+8,addr);
4814 emit_mov2imm_compact(ba[i],alt,start+i*4+8,addr);
4816 if(s1h>=0) emit_mov(addr,ntaddr);
4817 emit_cmovl_reg(alt,addr);
4820 emit_cmovne_reg(ntaddr,addr);
4821 emit_cmovs_reg(alt,addr);
4824 if((opcode[i]&0x2f)==7) // BGTZ
4826 //emit_movimm(ba[i],addr);
4827 //emit_movimm(start+i*4+8,ntaddr);
4828 emit_mov2imm_compact(ba[i],addr,start+i*4+8,ntaddr);
4830 if(s1h>=0) emit_mov(addr,alt);
4831 emit_cmovl_reg(ntaddr,addr);
4834 emit_cmovne_reg(alt,addr);
4835 emit_cmovs_reg(ntaddr,addr);
4838 if((opcode[i]==1)&&(opcode2[i]&0x2D)==0) // BLTZ
4840 //emit_movimm(ba[i],alt);
4841 //emit_movimm(start+i*4+8,addr);
4842 emit_mov2imm_compact(ba[i],alt,start+i*4+8,addr);
4843 if(s1h>=0) emit_test(s1h,s1h);
4844 else emit_test(s1l,s1l);
4845 emit_cmovs_reg(alt,addr);
4847 if((opcode[i]==1)&&(opcode2[i]&0x2D)==1) // BGEZ
4849 //emit_movimm(ba[i],addr);
4850 //emit_movimm(start+i*4+8,alt);
4851 emit_mov2imm_compact(ba[i],addr,start+i*4+8,alt);
4852 if(s1h>=0) emit_test(s1h,s1h);
4853 else emit_test(s1l,s1l);
4854 emit_cmovs_reg(alt,addr);
4856 if(opcode[i]==0x11 && opcode2[i]==0x08 ) {
4857 if(source[i]&0x10000) // BC1T
4859 //emit_movimm(ba[i],alt);
4860 //emit_movimm(start+i*4+8,addr);
4861 emit_mov2imm_compact(ba[i],alt,start+i*4+8,addr);
4862 emit_testimm(s1l,0x800000);
4863 emit_cmovne_reg(alt,addr);
4867 //emit_movimm(ba[i],addr);
4868 //emit_movimm(start+i*4+8,alt);
4869 emit_mov2imm_compact(ba[i],addr,start+i*4+8,alt);
4870 emit_testimm(s1l,0x800000);
4871 emit_cmovne_reg(alt,addr);
4874 emit_writeword(addr,(int)&pcaddr);
4879 int r=get_reg(branch_regs[i].regmap,rs1[i]);
4880 if(rs1[i]==rt1[i+1]||rs1[i]==rt2[i+1]) {
4881 r=get_reg(branch_regs[i].regmap,RTEMP);
4883 emit_writeword(r,(int)&pcaddr);
4885 else {DebugMessage(M64MSG_ERROR, "Unknown branch type in do_ccstub");exit(1);}
4887 // Update cycle count
4888 assert(branch_regs[i].regmap[HOST_CCREG]==CCREG||branch_regs[i].regmap[HOST_CCREG]==-1);
4889 if(stubs[n][3]) emit_addimm(HOST_CCREG,CLOCK_DIVIDER*stubs[n][3],HOST_CCREG);
4890 emit_call((int)cc_interrupt);
4891 if(stubs[n][3]) emit_addimm(HOST_CCREG,-CLOCK_DIVIDER*stubs[n][3],HOST_CCREG);
4892 if(stubs[n][6]==TAKEN) {
4893 if(internal_branch(branch_regs[i].is32,ba[i]))
4894 load_needed_regs(branch_regs[i].regmap,regs[(ba[i]-start)>>2].regmap_entry);
4895 else if(itype[i]==RJUMP) {
4896 if(get_reg(branch_regs[i].regmap,RTEMP)>=0)
4897 emit_readword((int)&pcaddr,get_reg(branch_regs[i].regmap,RTEMP));
4899 emit_loadreg(rs1[i],get_reg(branch_regs[i].regmap,rs1[i]));
4901 }else if(stubs[n][6]==NOTTAKEN) {
4902 if(i<slen-2) load_needed_regs(branch_regs[i].regmap,regmap_pre[i+2]);
4903 else load_all_regs(branch_regs[i].regmap);
4904 }else if(stubs[n][6]==NULLDS) {
4905 // Delay slot instruction is nullified ("likely" branch)
4906 if(i<slen-2) load_needed_regs(regs[i].regmap,regmap_pre[i+2]);
4907 else load_all_regs(regs[i].regmap);
4909 load_all_regs(branch_regs[i].regmap);
4911 emit_jmp(stubs[n][2]); // return address
4913 /* This works but uses a lot of memory...
4914 emit_readword((int)&last_count,ECX);
4915 emit_add(HOST_CCREG,ECX,EAX);
4916 emit_writeword(EAX,(int)&Count);
4917 emit_call((int)gen_interupt);
4918 emit_readword((int)&Count,HOST_CCREG);
4919 emit_readword((int)&next_interupt,EAX);
4920 emit_readword((int)&pending_exception,EBX);
4921 emit_writeword(EAX,(int)&last_count);
4922 emit_sub(HOST_CCREG,EAX,HOST_CCREG);
4924 int jne_instr=(int)out;
4926 if(stubs[n][3]) emit_addimm(HOST_CCREG,-2*stubs[n][3],HOST_CCREG);
4927 load_all_regs(branch_regs[i].regmap);
4928 emit_jmp(stubs[n][2]); // return address
4929 set_jump_target(jne_instr,(int)out);
4930 emit_readword((int)&pcaddr,EAX);
4931 // Call get_addr_ht instead of doing the hash table here.
4932 // This code is executed infrequently and takes up a lot of space
4933 // so smaller is better.
4934 emit_storereg(CCREG,HOST_CCREG);
4936 emit_call((int)get_addr_ht);
4937 emit_loadreg(CCREG,HOST_CCREG);
4938 emit_addimm(ESP,4,ESP);
4942 static void add_to_linker(int addr,int target,int ext)
4944 link_addr[linkcount][0]=addr;
4945 link_addr[linkcount][1]=target;
4946 link_addr[linkcount][2]=ext;
4950 static void ujump_assemble(int i,struct regstat *i_regs)
4953 signed char *i_regmap=i_regs->regmap;
4955 if(i==(ba[i]-start)>>2) assem_debug("idle loop");
4956 address_generation(i+1,i_regs,regs[i].regmap_entry);
4958 int temp=get_reg(branch_regs[i].regmap,PTEMP);
4959 if(rt1[i]==31&&temp>=0)
4961 int return_address=start+i*4+8;
4962 if(get_reg(branch_regs[i].regmap,31)>0)
4963 if(i_regmap[temp]==PTEMP) emit_movimm((int)hash_table[((return_address>>16)^return_address)&0xFFFF],temp);
4966 ds_assemble(i+1,i_regs);
4967 uint64_t bc_unneeded=branch_regs[i].u;
4968 uint64_t bc_unneeded_upper=branch_regs[i].uu;
4969 bc_unneeded|=1|(1LL<<rt1[i]);
4970 bc_unneeded_upper|=1|(1LL<<rt1[i]);
4971 wb_invalidate(regs[i].regmap,branch_regs[i].regmap,regs[i].dirty,regs[i].is32,
4972 bc_unneeded,bc_unneeded_upper);
4973 load_regs(regs[i].regmap,branch_regs[i].regmap,regs[i].was32,CCREG,CCREG);
4976 unsigned int return_address;
4977 assert(rt1[i+1]!=31);
4978 assert(rt2[i+1]!=31);
4979 rt=get_reg(branch_regs[i].regmap,31);
4980 assem_debug("branch(%d): eax=%d ecx=%d edx=%d ebx=%d ebp=%d esi=%d edi=%d",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]);
4982 return_address=start+i*4+8;
4985 if(internal_branch(branch_regs[i].is32,return_address)) {
4987 if(temp==EXCLUDE_REG||temp>=HOST_REGS||
4988 branch_regs[i].regmap[temp]>=0)
4990 temp=get_reg(branch_regs[i].regmap,-1);
4993 if(temp<0) temp=HOST_TEMPREG;
4995 if(temp>=0) do_miniht_insert(return_address,rt,temp);
4996 else emit_movimm(return_address,rt);
5004 if(i_regmap[temp]!=PTEMP) emit_movimm((int)hash_table[((return_address>>16)^return_address)&0xFFFF],temp);
5007 emit_movimm(return_address,rt); // PC into link register
5009 emit_prefetch(hash_table[((return_address>>16)^return_address)&0xFFFF]);
5015 cc=get_reg(branch_regs[i].regmap,CCREG);
5016 assert(cc==HOST_CCREG);
5017 store_regs_bt(branch_regs[i].regmap,branch_regs[i].is32,branch_regs[i].dirty,ba[i]);
5019 if(rt1[i]==31&&temp>=0) emit_prefetchreg(temp);
5021 do_cc(i,branch_regs[i].regmap,&adj,ba[i],TAKEN,0);
5022 if(adj) emit_addimm(cc,CLOCK_DIVIDER*(ccadj[i]+2-adj),cc);
5023 load_regs_bt(branch_regs[i].regmap,branch_regs[i].is32,branch_regs[i].dirty,ba[i]);
5024 if(internal_branch(branch_regs[i].is32,ba[i]))
5025 assem_debug("branch: internal");
5027 assem_debug("branch: external");
5028 if(internal_branch(branch_regs[i].is32,ba[i])&&is_ds[(ba[i]-start)>>2]) {
5029 ds_assemble_entry(i);
5032 add_to_linker((int)out,ba[i],internal_branch(branch_regs[i].is32,ba[i]));
5037 static void rjump_assemble(int i,struct regstat *i_regs)
5040 signed char *i_regmap=i_regs->regmap;
5044 rs=get_reg(branch_regs[i].regmap,rs1[i]);
5046 if(rs1[i]==rt1[i+1]||rs1[i]==rt2[i+1]) {
5047 // Delay slot abuse, make a copy of the branch address register
5048 temp=get_reg(branch_regs[i].regmap,RTEMP);
5050 assert(regs[i].regmap[temp]==RTEMP);
5054 address_generation(i+1,i_regs,regs[i].regmap_entry);
5058 if((temp=get_reg(branch_regs[i].regmap,PTEMP))>=0) {
5059 int return_address=start+i*4+8;
5060 if(i_regmap[temp]==PTEMP) emit_movimm((int)hash_table[((return_address>>16)^return_address)&0xFFFF],temp);
5066 int rh=get_reg(regs[i].regmap,RHASH);
5067 if(rh>=0) do_preload_rhash(rh);
5070 ds_assemble(i+1,i_regs);
5071 uint64_t bc_unneeded=branch_regs[i].u;
5072 uint64_t bc_unneeded_upper=branch_regs[i].uu;
5073 bc_unneeded|=1|(1LL<<rt1[i]);
5074 bc_unneeded_upper|=1|(1LL<<rt1[i]);
5075 bc_unneeded&=~(1LL<<rs1[i]);
5076 wb_invalidate(regs[i].regmap,branch_regs[i].regmap,regs[i].dirty,regs[i].is32,
5077 bc_unneeded,bc_unneeded_upper);
5078 load_regs(regs[i].regmap,branch_regs[i].regmap,regs[i].was32,rs1[i],CCREG);
5080 int rt,return_address;
5081 assert(rt1[i+1]!=rt1[i]);
5082 assert(rt2[i+1]!=rt1[i]);
5083 rt=get_reg(branch_regs[i].regmap,rt1[i]);
5084 assem_debug("branch(%d): eax=%d ecx=%d edx=%d ebx=%d ebp=%d esi=%d edi=%d",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]);
5086 return_address=start+i*4+8;
5090 if(i_regmap[temp]!=PTEMP) emit_movimm((int)hash_table[((return_address>>16)^return_address)&0xFFFF],temp);
5093 emit_movimm(return_address,rt); // PC into link register
5095 emit_prefetch(hash_table[((return_address>>16)^return_address)&0xFFFF]);
5098 cc=get_reg(branch_regs[i].regmap,CCREG);
5099 assert(cc==HOST_CCREG);
5101 int rh=get_reg(branch_regs[i].regmap,RHASH);
5102 int ht=get_reg(branch_regs[i].regmap,RHTBL);
5104 if(regs[i].regmap[rh]!=RHASH) do_preload_rhash(rh);
5105 do_preload_rhtbl(ht);
5109 store_regs_bt(branch_regs[i].regmap,branch_regs[i].is32,branch_regs[i].dirty,-1);
5110 #ifdef DESTRUCTIVE_WRITEBACK
5111 if((branch_regs[i].dirty>>rs)&(branch_regs[i].is32>>rs1[i])&1) {
5112 if(rs1[i]!=rt1[i+1]&&rs1[i]!=rt2[i+1]) {
5113 emit_loadreg(rs1[i],rs);
5118 if(rt1[i]==31&&temp>=0) emit_prefetchreg(temp);
5122 do_miniht_load(ht,rh);
5125 //do_cc(i,branch_regs[i].regmap,&adj,-1,TAKEN);
5126 //if(adj) emit_addimm(cc,2*(ccadj[i]+2-adj),cc); // ??? - Shouldn't happen
5128 emit_addimm_and_set_flags(CLOCK_DIVIDER*(ccadj[i]+2),HOST_CCREG);
5129 add_stub(CC_STUB,(int)out,jump_vaddr_reg[rs],0,i,-1,TAKEN,0);
5131 //load_regs_bt(branch_regs[i].regmap,branch_regs[i].is32,branch_regs[i].dirty,-1);
5134 do_miniht_jump(rs,rh,ht);
5139 //if(rs!=EAX) emit_mov(rs,EAX);
5140 //emit_jmp((int)jump_vaddr_eax);
5141 emit_jmp(jump_vaddr_reg[rs]);
5146 emit_shrimm(rs,16,rs);
5147 emit_xor(temp,rs,rs);
5148 emit_movzwl_reg(rs,rs);
5149 emit_shlimm(rs,4,rs);
5150 emit_cmpmem_indexed((int)hash_table,rs,temp);
5151 emit_jne((int)out+14);
5152 emit_readword_indexed((int)hash_table+4,rs,rs);
5154 emit_cmpmem_indexed((int)hash_table+8,rs,temp);
5155 emit_addimm_no_flags(8,rs);
5156 emit_jeq((int)out-17);
5157 // No hit on hash table, call compiler
5160 #ifdef DEBUG_CYCLE_COUNT
5161 emit_readword((int)&last_count,ECX);
5162 emit_add(HOST_CCREG,ECX,HOST_CCREG);
5163 emit_readword((int)&next_interupt,ECX);
5164 emit_writeword(HOST_CCREG,(int)&Count);
5165 emit_sub(HOST_CCREG,ECX,HOST_CCREG);
5166 emit_writeword(ECX,(int)&last_count);
5169 emit_storereg(CCREG,HOST_CCREG);
5170 emit_call((int)get_addr);
5171 emit_loadreg(CCREG,HOST_CCREG);
5172 emit_addimm(ESP,4,ESP);
5174 #ifdef CORTEX_A8_BRANCH_PREDICTION_HACK
5175 if(rt1[i]!=31&&i<slen-2&&(((u_int)out)&7)) emit_mov(13,13);
5179 static void cjump_assemble(int i,struct regstat *i_regs)
5181 signed char *i_regmap=i_regs->regmap;
5184 match=match_bt(branch_regs[i].regmap,branch_regs[i].is32,branch_regs[i].dirty,ba[i]);
5185 assem_debug("match=%d",match);
5186 int s1h,s1l,s2h,s2l;
5187 int prev_cop1_usable=cop1_usable;
5188 int unconditional=0,nop=0;
5191 int internal=internal_branch(branch_regs[i].is32,ba[i]);
5192 if(i==(ba[i]-start)>>2) assem_debug("idle loop");
5193 if(!match) invert=1;
5194 #ifdef CORTEX_A8_BRANCH_PREDICTION_HACK
5195 if(i>(ba[i]-start)>>2) invert=1;
5199 s1l=get_reg(branch_regs[i].regmap,rs1[i]);
5200 s1h=get_reg(branch_regs[i].regmap,rs1[i]|64);
5201 s2l=get_reg(branch_regs[i].regmap,rs2[i]);
5202 s2h=get_reg(branch_regs[i].regmap,rs2[i]|64);
5205 s1l=get_reg(i_regmap,rs1[i]);
5206 s1h=get_reg(i_regmap,rs1[i]|64);
5207 s2l=get_reg(i_regmap,rs2[i]);
5208 s2h=get_reg(i_regmap,rs2[i]|64);
5210 if(rs1[i]==0&&rs2[i]==0)
5212 if(opcode[i]&1) nop=1;
5213 else unconditional=1;
5214 //assert(opcode[i]!=5);
5215 //assert(opcode[i]!=7);
5216 //assert(opcode[i]!=0x15);
5217 //assert(opcode[i]!=0x17);
5223 only32=(regs[i].was32>>rs2[i])&1;
5228 only32=(regs[i].was32>>rs1[i])&1;
5231 only32=(regs[i].was32>>rs1[i])&(regs[i].was32>>rs2[i])&1;
5235 // Out of order execution (delay slot first)
5236 //DebugMessage(M64MSG_VERBOSE, "OOOE");
5237 address_generation(i+1,i_regs,regs[i].regmap_entry);
5238 ds_assemble(i+1,i_regs);
5240 uint64_t bc_unneeded=branch_regs[i].u;
5241 uint64_t bc_unneeded_upper=branch_regs[i].uu;
5242 bc_unneeded&=~((1LL<<rs1[i])|(1LL<<rs2[i]));
5243 bc_unneeded_upper&=~((1LL<<us1[i])|(1LL<<us2[i]));
5245 bc_unneeded_upper|=1;
5246 wb_invalidate(regs[i].regmap,branch_regs[i].regmap,regs[i].dirty,regs[i].is32,
5247 bc_unneeded,bc_unneeded_upper);
5248 load_regs(regs[i].regmap,branch_regs[i].regmap,regs[i].was32,rs1[i],rs2[i]);
5249 load_regs(regs[i].regmap,branch_regs[i].regmap,regs[i].was32,CCREG,CCREG);
5250 cc=get_reg(branch_regs[i].regmap,CCREG);
5251 assert(cc==HOST_CCREG);
5253 store_regs_bt(branch_regs[i].regmap,branch_regs[i].is32,branch_regs[i].dirty,ba[i]);
5254 //do_cc(i,branch_regs[i].regmap,&adj,unconditional?ba[i]:-1,unconditional);
5255 //assem_debug("cycle count (adj)");
5257 do_cc(i,branch_regs[i].regmap,&adj,ba[i],TAKEN,0);
5258 if(i!=(ba[i]-start)>>2 || source[i+1]!=0) {
5259 if(adj) emit_addimm(cc,CLOCK_DIVIDER*(ccadj[i]+2-adj),cc);
5260 load_regs_bt(branch_regs[i].regmap,branch_regs[i].is32,branch_regs[i].dirty,ba[i]);
5262 assem_debug("branch: internal");
5264 assem_debug("branch: external");
5265 if(internal&&is_ds[(ba[i]-start)>>2]) {
5266 ds_assemble_entry(i);
5269 add_to_linker((int)out,ba[i],internal);
5272 #ifdef CORTEX_A8_BRANCH_PREDICTION_HACK
5273 if(((u_int)out)&7) emit_addnop(0);
5278 emit_addimm_and_set_flags(CLOCK_DIVIDER*(ccadj[i]+2),cc);
5281 add_stub(CC_STUB,jaddr,(int)out,0,i,start+i*4+8,NOTTAKEN,0);
5284 int taken=0,nottaken=0,nottaken1=0;
5285 do_cc(i,branch_regs[i].regmap,&adj,-1,0,invert);
5286 if(adj&&!invert) emit_addimm(cc,CLOCK_DIVIDER*(ccadj[i]+2-adj),cc);
5290 if(opcode[i]==4) // BEQ
5292 if(s2h>=0) emit_cmp(s1h,s2h);
5293 else emit_test(s1h,s1h);
5297 if(opcode[i]==5) // BNE
5299 if(s2h>=0) emit_cmp(s1h,s2h);
5300 else emit_test(s1h,s1h);
5301 if(invert) taken=(int)out;
5302 else add_to_linker((int)out,ba[i],internal);
5305 if(opcode[i]==6) // BLEZ
5308 if(invert) taken=(int)out;
5309 else add_to_linker((int)out,ba[i],internal);
5314 if(opcode[i]==7) // BGTZ
5319 if(invert) taken=(int)out;
5320 else add_to_linker((int)out,ba[i],internal);
5325 //DebugMessage(M64MSG_VERBOSE, "branch(%d): eax=%d ecx=%d edx=%d ebx=%d ebp=%d esi=%d edi=%d",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]);
5327 if(opcode[i]==4) // BEQ
5329 if(s2l>=0) emit_cmp(s1l,s2l);
5330 else emit_test(s1l,s1l);
5335 add_to_linker((int)out,ba[i],internal);
5339 if(opcode[i]==5) // BNE
5341 if(s2l>=0) emit_cmp(s1l,s2l);
5342 else emit_test(s1l,s1l);
5347 add_to_linker((int)out,ba[i],internal);
5351 if(opcode[i]==6) // BLEZ
5358 add_to_linker((int)out,ba[i],internal);
5362 if(opcode[i]==7) // BGTZ
5369 add_to_linker((int)out,ba[i],internal);
5374 if(taken) set_jump_target(taken,(int)out);
5375 #ifdef CORTEX_A8_BRANCH_PREDICTION_HACK
5376 if(match&&(!internal||!is_ds[(ba[i]-start)>>2])) {
5378 emit_addimm(cc,-CLOCK_DIVIDER*adj,cc);
5379 add_to_linker((int)out,ba[i],internal);
5382 add_to_linker((int)out,ba[i],internal*2);
5388 if(adj) emit_addimm(cc,-CLOCK_DIVIDER*adj,cc);
5389 store_regs_bt(branch_regs[i].regmap,branch_regs[i].is32,branch_regs[i].dirty,ba[i]);
5390 load_regs_bt(branch_regs[i].regmap,branch_regs[i].is32,branch_regs[i].dirty,ba[i]);
5392 assem_debug("branch: internal");
5394 assem_debug("branch: external");
5395 if(internal&&is_ds[(ba[i]-start)>>2]) {
5396 ds_assemble_entry(i);
5399 add_to_linker((int)out,ba[i],internal);
5403 set_jump_target(nottaken,(int)out);
5406 if(nottaken1) set_jump_target(nottaken1,(int)out);
5408 if(!invert) emit_addimm(cc,CLOCK_DIVIDER*adj,cc);
5410 } // (!unconditional)
5414 // In-order execution (branch first)
5415 //if(likely[i]) DebugMessage(M64MSG_VERBOSE, "IOL");
5417 //DebugMessage(M64MSG_VERBOSE, "IOE");
5418 int taken=0,nottaken=0,nottaken1=0;
5419 if(!unconditional&&!nop) {
5423 if((opcode[i]&0x2f)==4) // BEQ
5425 if(s2h>=0) emit_cmp(s1h,s2h);
5426 else emit_test(s1h,s1h);
5430 if((opcode[i]&0x2f)==5) // BNE
5432 if(s2h>=0) emit_cmp(s1h,s2h);
5433 else emit_test(s1h,s1h);
5437 if((opcode[i]&0x2f)==6) // BLEZ
5445 if((opcode[i]&0x2f)==7) // BGTZ
5455 //DebugMessage(M64MSG_VERBOSE, "branch(%d): eax=%d ecx=%d edx=%d ebx=%d ebp=%d esi=%d edi=%d",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]);
5457 if((opcode[i]&0x2f)==4) // BEQ
5459 if(s2l>=0) emit_cmp(s1l,s2l);
5460 else emit_test(s1l,s1l);
5464 if((opcode[i]&0x2f)==5) // BNE
5466 if(s2l>=0) emit_cmp(s1l,s2l);
5467 else emit_test(s1l,s1l);
5471 if((opcode[i]&0x2f)==6) // BLEZ
5477 if((opcode[i]&0x2f)==7) // BGTZ
5483 } // if(!unconditional)
5485 uint64_t ds_unneeded=branch_regs[i].u;
5486 uint64_t ds_unneeded_upper=branch_regs[i].uu;
5487 ds_unneeded&=~((1LL<<rs1[i+1])|(1LL<<rs2[i+1]));
5488 ds_unneeded_upper&=~((1LL<<us1[i+1])|(1LL<<us2[i+1]));
5489 if((~ds_unneeded_upper>>rt1[i+1])&1) ds_unneeded_upper&=~((1LL<<dep1[i+1])|(1LL<<dep2[i+1]));
5491 ds_unneeded_upper|=1;
5494 if(taken) set_jump_target(taken,(int)out);
5496 wb_invalidate(regs[i].regmap,branch_regs[i].regmap,regs[i].dirty,regs[i].is32,
5497 ds_unneeded,ds_unneeded_upper);
5499 load_regs(regs[i].regmap,branch_regs[i].regmap,regs[i].was32,rs1[i+1],rs2[i+1]);
5500 address_generation(i+1,&branch_regs[i],0);
5501 load_regs(regs[i].regmap,branch_regs[i].regmap,regs[i].was32,CCREG,INVCP);
5502 ds_assemble(i+1,&branch_regs[i]);
5503 cc=get_reg(branch_regs[i].regmap,CCREG);
5505 emit_loadreg(CCREG,cc=HOST_CCREG);
5506 // CHECK: Is the following instruction (fall thru) allocated ok?
5508 assert(cc==HOST_CCREG);
5509 store_regs_bt(branch_regs[i].regmap,branch_regs[i].is32,branch_regs[i].dirty,ba[i]);
5510 do_cc(i,i_regmap,&adj,ba[i],TAKEN,0);
5511 assem_debug("cycle count (adj)");
5512 if(adj) emit_addimm(cc,CLOCK_DIVIDER*(ccadj[i]+2-adj),cc);
5513 load_regs_bt(branch_regs[i].regmap,branch_regs[i].is32,branch_regs[i].dirty,ba[i]);
5515 assem_debug("branch: internal");
5517 assem_debug("branch: external");
5518 if(internal&&is_ds[(ba[i]-start)>>2]) {
5519 ds_assemble_entry(i);
5522 add_to_linker((int)out,ba[i],internal);
5527 cop1_usable=prev_cop1_usable;
5528 if(!unconditional) {
5529 if(nottaken1) set_jump_target(nottaken1,(int)out);
5530 set_jump_target(nottaken,(int)out);
5533 wb_invalidate(regs[i].regmap,branch_regs[i].regmap,regs[i].dirty,regs[i].is32,
5534 ds_unneeded,ds_unneeded_upper);
5535 load_regs(regs[i].regmap,branch_regs[i].regmap,regs[i].was32,rs1[i+1],rs2[i+1]);
5536 address_generation(i+1,&branch_regs[i],0);
5537 load_regs(regs[i].regmap,branch_regs[i].regmap,regs[i].was32,CCREG,CCREG);
5538 ds_assemble(i+1,&branch_regs[i]);
5540 cc=get_reg(branch_regs[i].regmap,CCREG);
5541 if(cc==-1&&!likely[i]) {
5542 // Cycle count isn't in a register, temporarily load it then write it out
5543 emit_loadreg(CCREG,HOST_CCREG);
5544 emit_addimm_and_set_flags(CLOCK_DIVIDER*(ccadj[i]+2),HOST_CCREG);
5547 add_stub(CC_STUB,jaddr,(int)out,0,i,start+i*4+8,NOTTAKEN,0);
5548 emit_storereg(CCREG,HOST_CCREG);
5551 cc=get_reg(i_regmap,CCREG);
5552 assert(cc==HOST_CCREG);
5553 emit_addimm_and_set_flags(CLOCK_DIVIDER*(ccadj[i]+2),cc);
5556 add_stub(CC_STUB,jaddr,(int)out,0,i,start+i*4+8,likely[i]?NULLDS:NOTTAKEN,0);
5562 static void sjump_assemble(int i,struct regstat *i_regs)
5564 signed char *i_regmap=i_regs->regmap;
5567 match=match_bt(branch_regs[i].regmap,branch_regs[i].is32,branch_regs[i].dirty,ba[i]);
5568 assem_debug("smatch=%d",match);
5570 int prev_cop1_usable=cop1_usable;
5571 int unconditional=0,nevertaken=0;
5574 int internal=internal_branch(branch_regs[i].is32,ba[i]);
5575 if(i==(ba[i]-start)>>2) assem_debug("idle loop");
5576 if(!match) invert=1;
5577 #ifdef CORTEX_A8_BRANCH_PREDICTION_HACK
5578 if(i>(ba[i]-start)>>2) invert=1;
5581 //if(opcode2[i]>=0x10) return; // FIXME (BxxZAL)
5582 assert(opcode2[i]<0x10||rs1[i]==0); // FIXME (BxxZAL)
5585 s1l=get_reg(branch_regs[i].regmap,rs1[i]);
5586 s1h=get_reg(branch_regs[i].regmap,rs1[i]|64);
5589 s1l=get_reg(i_regmap,rs1[i]);
5590 s1h=get_reg(i_regmap,rs1[i]|64);
5594 if(opcode2[i]&1) unconditional=1;
5596 // These are never taken (r0 is never less than zero)
5597 //assert(opcode2[i]!=0);
5598 //assert(opcode2[i]!=2);
5599 //assert(opcode2[i]!=0x10);
5600 //assert(opcode2[i]!=0x12);
5603 only32=(regs[i].was32>>rs1[i])&1;
5607 // Out of order execution (delay slot first)
5608 //DebugMessage(M64MSG_VERBOSE, "OOOE");
5609 address_generation(i+1,i_regs,regs[i].regmap_entry);
5610 ds_assemble(i+1,i_regs);
5612 uint64_t bc_unneeded=branch_regs[i].u;
5613 uint64_t bc_unneeded_upper=branch_regs[i].uu;
5614 bc_unneeded&=~((1LL<<rs1[i])|(1LL<<rs2[i]));
5615 bc_unneeded_upper&=~((1LL<<us1[i])|(1LL<<us2[i]));
5617 bc_unneeded_upper|=1;
5618 wb_invalidate(regs[i].regmap,branch_regs[i].regmap,regs[i].dirty,regs[i].is32,
5619 bc_unneeded,bc_unneeded_upper);
5620 load_regs(regs[i].regmap,branch_regs[i].regmap,regs[i].was32,rs1[i],rs1[i]);
5621 load_regs(regs[i].regmap,branch_regs[i].regmap,regs[i].was32,CCREG,CCREG);
5623 int rt,return_address;
5624 assert(rt1[i+1]!=31);
5625 assert(rt2[i+1]!=31);
5626 rt=get_reg(branch_regs[i].regmap,31);
5627 assem_debug("branch(%d): eax=%d ecx=%d edx=%d ebx=%d ebp=%d esi=%d edi=%d",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]);
5629 // Save the PC even if the branch is not taken
5630 return_address=start+i*4+8;
5631 emit_movimm(return_address,rt); // PC into link register
5633 if(!nevertaken) emit_prefetch(hash_table[((return_address>>16)^return_address)&0xFFFF]);
5637 cc=get_reg(branch_regs[i].regmap,CCREG);
5638 assert(cc==HOST_CCREG);
5640 store_regs_bt(branch_regs[i].regmap,branch_regs[i].is32,branch_regs[i].dirty,ba[i]);
5641 //do_cc(i,branch_regs[i].regmap,&adj,unconditional?ba[i]:-1,unconditional);
5642 assem_debug("cycle count (adj)");
5644 do_cc(i,branch_regs[i].regmap,&adj,ba[i],TAKEN,0);
5645 if(i!=(ba[i]-start)>>2 || source[i+1]!=0) {
5646 if(adj) emit_addimm(cc,CLOCK_DIVIDER*(ccadj[i]+2-adj),cc);
5647 load_regs_bt(branch_regs[i].regmap,branch_regs[i].is32,branch_regs[i].dirty,ba[i]);
5649 assem_debug("branch: internal");
5651 assem_debug("branch: external");
5652 if(internal&&is_ds[(ba[i]-start)>>2]) {
5653 ds_assemble_entry(i);
5656 add_to_linker((int)out,ba[i],internal);
5659 #ifdef CORTEX_A8_BRANCH_PREDICTION_HACK
5660 if(((u_int)out)&7) emit_addnop(0);
5664 else if(nevertaken) {
5665 emit_addimm_and_set_flags(CLOCK_DIVIDER*(ccadj[i]+2),cc);
5668 add_stub(CC_STUB,jaddr,(int)out,0,i,start+i*4+8,NOTTAKEN,0);
5672 do_cc(i,branch_regs[i].regmap,&adj,-1,0,invert);
5673 if(adj&&!invert) emit_addimm(cc,CLOCK_DIVIDER*(ccadj[i]+2-adj),cc);
5677 if(opcode2[i]==0) // BLTZ
5684 add_to_linker((int)out,ba[i],internal);
5688 if(opcode2[i]==1) // BGEZ
5695 add_to_linker((int)out,ba[i],internal);
5703 if(opcode2[i]==0) // BLTZ
5710 add_to_linker((int)out,ba[i],internal);
5714 if(opcode2[i]==1) // BGEZ
5721 add_to_linker((int)out,ba[i],internal);
5728 #ifdef CORTEX_A8_BRANCH_PREDICTION_HACK
5729 if(match&&(!internal||!is_ds[(ba[i]-start)>>2])) {
5731 emit_addimm(cc,-CLOCK_DIVIDER*adj,cc);
5732 add_to_linker((int)out,ba[i],internal);
5735 add_to_linker((int)out,ba[i],internal*2);
5741 if(adj) emit_addimm(cc,-CLOCK_DIVIDER*adj,cc);
5742 store_regs_bt(branch_regs[i].regmap,branch_regs[i].is32,branch_regs[i].dirty,ba[i]);
5743 load_regs_bt(branch_regs[i].regmap,branch_regs[i].is32,branch_regs[i].dirty,ba[i]);
5745 assem_debug("branch: internal");
5747 assem_debug("branch: external");
5748 if(internal&&is_ds[(ba[i]-start)>>2]) {
5749 ds_assemble_entry(i);
5752 add_to_linker((int)out,ba[i],internal);
5756 set_jump_target(nottaken,(int)out);
5760 if(!invert) emit_addimm(cc,CLOCK_DIVIDER*adj,cc);
5762 } // (!unconditional)
5766 // In-order execution (branch first)
5767 //DebugMessage(M64MSG_VERBOSE, "IOE");
5769 if(!unconditional) {
5770 //DebugMessage(M64MSG_VERBOSE, "branch(%d): eax=%d ecx=%d edx=%d ebx=%d ebp=%d esi=%d edi=%d",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]);
5774 if((opcode2[i]&0x1d)==0) // BLTZ/BLTZL
5780 if((opcode2[i]&0x1d)==1) // BGEZ/BGEZL
5790 if((opcode2[i]&0x1d)==0) // BLTZ/BLTZL
5796 if((opcode2[i]&0x1d)==1) // BGEZ/BGEZL
5803 } // if(!unconditional)
5805 uint64_t ds_unneeded=branch_regs[i].u;
5806 uint64_t ds_unneeded_upper=branch_regs[i].uu;
5807 ds_unneeded&=~((1LL<<rs1[i+1])|(1LL<<rs2[i+1]));
5808 ds_unneeded_upper&=~((1LL<<us1[i+1])|(1LL<<us2[i+1]));
5809 if((~ds_unneeded_upper>>rt1[i+1])&1) ds_unneeded_upper&=~((1LL<<dep1[i+1])|(1LL<<dep2[i+1]));
5811 ds_unneeded_upper|=1;
5814 //assem_debug("1:");
5815 wb_invalidate(regs[i].regmap,branch_regs[i].regmap,regs[i].dirty,regs[i].is32,
5816 ds_unneeded,ds_unneeded_upper);
5818 load_regs(regs[i].regmap,branch_regs[i].regmap,regs[i].was32,rs1[i+1],rs2[i+1]);
5819 address_generation(i+1,&branch_regs[i],0);
5820 load_regs(regs[i].regmap,branch_regs[i].regmap,regs[i].was32,CCREG,INVCP);
5821 ds_assemble(i+1,&branch_regs[i]);
5822 cc=get_reg(branch_regs[i].regmap,CCREG);
5824 emit_loadreg(CCREG,cc=HOST_CCREG);
5825 // CHECK: Is the following instruction (fall thru) allocated ok?
5827 assert(cc==HOST_CCREG);
5828 store_regs_bt(branch_regs[i].regmap,branch_regs[i].is32,branch_regs[i].dirty,ba[i]);
5829 do_cc(i,i_regmap,&adj,ba[i],TAKEN,0);
5830 assem_debug("cycle count (adj)");
5831 if(adj) emit_addimm(cc,CLOCK_DIVIDER*(ccadj[i]+2-adj),cc);
5832 load_regs_bt(branch_regs[i].regmap,branch_regs[i].is32,branch_regs[i].dirty,ba[i]);
5834 assem_debug("branch: internal");
5836 assem_debug("branch: external");
5837 if(internal&&is_ds[(ba[i]-start)>>2]) {
5838 ds_assemble_entry(i);
5841 add_to_linker((int)out,ba[i],internal);
5846 cop1_usable=prev_cop1_usable;
5847 if(!unconditional) {
5848 set_jump_target(nottaken,(int)out);
5851 wb_invalidate(regs[i].regmap,branch_regs[i].regmap,regs[i].dirty,regs[i].is32,
5852 ds_unneeded,ds_unneeded_upper);
5853 load_regs(regs[i].regmap,branch_regs[i].regmap,regs[i].was32,rs1[i+1],rs2[i+1]);
5854 address_generation(i+1,&branch_regs[i],0);
5855 load_regs(regs[i].regmap,branch_regs[i].regmap,regs[i].was32,CCREG,CCREG);
5856 ds_assemble(i+1,&branch_regs[i]);
5858 cc=get_reg(branch_regs[i].regmap,CCREG);
5859 if(cc==-1&&!likely[i]) {
5860 // Cycle count isn't in a register, temporarily load it then write it out
5861 emit_loadreg(CCREG,HOST_CCREG);
5862 emit_addimm_and_set_flags(CLOCK_DIVIDER*(ccadj[i]+2),HOST_CCREG);
5865 add_stub(CC_STUB,jaddr,(int)out,0,i,start+i*4+8,NOTTAKEN,0);
5866 emit_storereg(CCREG,HOST_CCREG);
5869 cc=get_reg(i_regmap,CCREG);
5870 assert(cc==HOST_CCREG);
5871 emit_addimm_and_set_flags(CLOCK_DIVIDER*(ccadj[i]+2),cc);
5874 add_stub(CC_STUB,jaddr,(int)out,0,i,start+i*4+8,likely[i]?NULLDS:NOTTAKEN,0);
5880 static void fjump_assemble(int i,struct regstat *i_regs)
5882 signed char *i_regmap=i_regs->regmap;
5885 match=match_bt(branch_regs[i].regmap,branch_regs[i].is32,branch_regs[i].dirty,ba[i]);
5886 assem_debug("fmatch=%d",match);
5890 int internal=internal_branch(branch_regs[i].is32,ba[i]);
5891 if(i==(ba[i]-start)>>2) assem_debug("idle loop");
5892 if(!match) invert=1;
5893 #ifdef CORTEX_A8_BRANCH_PREDICTION_HACK
5894 if(i>(ba[i]-start)>>2) invert=1;
5898 fs=get_reg(branch_regs[i].regmap,FSREG);
5899 address_generation(i+1,i_regs,regs[i].regmap_entry); // Is this okay?
5902 fs=get_reg(i_regmap,FSREG);
5905 // Check cop1 unusable
5907 cs=get_reg(i_regmap,CSREG);
5909 emit_testimm(cs,0x20000000);
5912 add_stub(FP_STUB,eaddr,(int)out,i,cs,(int)i_regs,0,0);
5917 // Out of order execution (delay slot first)
5918 //DebugMessage(M64MSG_VERBOSE, "OOOE");
5919 ds_assemble(i+1,i_regs);
5921 uint64_t bc_unneeded=branch_regs[i].u;
5922 uint64_t bc_unneeded_upper=branch_regs[i].uu;
5923 bc_unneeded&=~((1LL<<rs1[i])|(1LL<<rs2[i]));
5924 bc_unneeded_upper&=~((1LL<<us1[i])|(1LL<<us2[i]));
5926 bc_unneeded_upper|=1;
5927 wb_invalidate(regs[i].regmap,branch_regs[i].regmap,regs[i].dirty,regs[i].is32,
5928 bc_unneeded,bc_unneeded_upper);
5929 load_regs(regs[i].regmap,branch_regs[i].regmap,regs[i].was32,rs1[i],rs1[i]);
5930 load_regs(regs[i].regmap,branch_regs[i].regmap,regs[i].was32,CCREG,CCREG);
5931 cc=get_reg(branch_regs[i].regmap,CCREG);
5932 assert(cc==HOST_CCREG);
5933 do_cc(i,branch_regs[i].regmap,&adj,-1,0,invert);
5934 assem_debug("cycle count (adj)");
5937 if(adj&&!invert) emit_addimm(cc,CLOCK_DIVIDER*(ccadj[i]+2-adj),cc);
5940 emit_testimm(fs,0x800000);
5941 if(source[i]&0x10000) // BC1T
5947 add_to_linker((int)out,ba[i],internal);
5956 add_to_linker((int)out,ba[i],internal);
5964 if(adj) emit_addimm(cc,-CLOCK_DIVIDER*adj,cc);
5965 #ifdef CORTEX_A8_BRANCH_PREDICTION_HACK
5966 else if(match) emit_addnop(13);
5968 store_regs_bt(branch_regs[i].regmap,branch_regs[i].is32,branch_regs[i].dirty,ba[i]);
5969 load_regs_bt(branch_regs[i].regmap,branch_regs[i].is32,branch_regs[i].dirty,ba[i]);
5971 assem_debug("branch: internal");
5973 assem_debug("branch: external");
5974 if(internal&&is_ds[(ba[i]-start)>>2]) {
5975 ds_assemble_entry(i);
5978 add_to_linker((int)out,ba[i],internal);
5981 set_jump_target(nottaken,(int)out);
5985 if(!invert) emit_addimm(cc,CLOCK_DIVIDER*adj,cc);
5987 } // (!unconditional)
5991 // In-order execution (branch first)
5992 //DebugMessage(M64MSG_VERBOSE, "IOE");
5995 //DebugMessage(M64MSG_VERBOSE, "branch(%d): eax=%d ecx=%d edx=%d ebx=%d ebp=%d esi=%d edi=%d",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]);
5998 emit_testimm(fs,0x800000);
5999 if(source[i]&0x10000) // BC1T
6010 } // if(!unconditional)
6012 uint64_t ds_unneeded=branch_regs[i].u;
6013 uint64_t ds_unneeded_upper=branch_regs[i].uu;
6014 ds_unneeded&=~((1LL<<rs1[i+1])|(1LL<<rs2[i+1]));
6015 ds_unneeded_upper&=~((1LL<<us1[i+1])|(1LL<<us2[i+1]));
6016 if((~ds_unneeded_upper>>rt1[i+1])&1) ds_unneeded_upper&=~((1LL<<dep1[i+1])|(1LL<<dep2[i+1]));
6018 ds_unneeded_upper|=1;
6020 //assem_debug("1:");
6021 wb_invalidate(regs[i].regmap,branch_regs[i].regmap,regs[i].dirty,regs[i].is32,
6022 ds_unneeded,ds_unneeded_upper);
6024 load_regs(regs[i].regmap,branch_regs[i].regmap,regs[i].was32,rs1[i+1],rs2[i+1]);
6025 address_generation(i+1,&branch_regs[i],0);
6026 load_regs(regs[i].regmap,branch_regs[i].regmap,regs[i].was32,CCREG,INVCP);
6027 ds_assemble(i+1,&branch_regs[i]);
6028 cc=get_reg(branch_regs[i].regmap,CCREG);
6030 emit_loadreg(CCREG,cc=HOST_CCREG);
6031 // CHECK: Is the following instruction (fall thru) allocated ok?
6033 assert(cc==HOST_CCREG);
6034 store_regs_bt(branch_regs[i].regmap,branch_regs[i].is32,branch_regs[i].dirty,ba[i]);
6035 do_cc(i,i_regmap,&adj,ba[i],TAKEN,0);
6036 assem_debug("cycle count (adj)");
6037 if(adj) emit_addimm(cc,CLOCK_DIVIDER*(ccadj[i]+2-adj),cc);
6038 load_regs_bt(branch_regs[i].regmap,branch_regs[i].is32,branch_regs[i].dirty,ba[i]);
6040 assem_debug("branch: internal");
6042 assem_debug("branch: external");
6043 if(internal&&is_ds[(ba[i]-start)>>2]) {
6044 ds_assemble_entry(i);
6047 add_to_linker((int)out,ba[i],internal);
6052 if(1) { // <- FIXME (don't need this)
6053 set_jump_target(nottaken,(int)out);
6056 wb_invalidate(regs[i].regmap,branch_regs[i].regmap,regs[i].dirty,regs[i].is32,
6057 ds_unneeded,ds_unneeded_upper);
6058 load_regs(regs[i].regmap,branch_regs[i].regmap,regs[i].was32,rs1[i+1],rs2[i+1]);
6059 address_generation(i+1,&branch_regs[i],0);
6060 load_regs(regs[i].regmap,branch_regs[i].regmap,regs[i].was32,CCREG,CCREG);
6061 ds_assemble(i+1,&branch_regs[i]);
6063 cc=get_reg(branch_regs[i].regmap,CCREG);
6064 if(cc==-1&&!likely[i]) {
6065 // Cycle count isn't in a register, temporarily load it then write it out
6066 emit_loadreg(CCREG,HOST_CCREG);
6067 emit_addimm_and_set_flags(CLOCK_DIVIDER*(ccadj[i]+2),HOST_CCREG);
6070 add_stub(CC_STUB,jaddr,(int)out,0,i,start+i*4+8,NOTTAKEN,0);
6071 emit_storereg(CCREG,HOST_CCREG);
6074 cc=get_reg(i_regmap,CCREG);
6075 assert(cc==HOST_CCREG);
6076 emit_addimm_and_set_flags(CLOCK_DIVIDER*(ccadj[i]+2),cc);
6079 add_stub(CC_STUB,jaddr,(int)out,0,i,start+i*4+8,likely[i]?NULLDS:NOTTAKEN,0);
6085 static void pagespan_assemble(int i,struct regstat *i_regs)
6087 int s1l=get_reg(i_regs->regmap,rs1[i]);
6088 int s1h=get_reg(i_regs->regmap,rs1[i]|64);
6089 int s2l=get_reg(i_regs->regmap,rs2[i]);
6090 int s2h=get_reg(i_regs->regmap,rs2[i]|64);
6093 int unconditional=0;
6103 if((i_regs->is32>>rs1[i])&(i_regs->is32>>rs2[i])&1) {
6107 int addr,alt,ntaddr;
6108 if(i_regs->regmap[HOST_BTREG]<0) {addr=HOST_BTREG;}
6112 if(hr!=EXCLUDE_REG && hr!=HOST_CCREG &&
6113 (i_regs->regmap[hr]&63)!=rs1[i] &&
6114 (i_regs->regmap[hr]&63)!=rs2[i] )
6123 if(hr!=EXCLUDE_REG && hr!=HOST_CCREG && hr!=HOST_BTREG &&
6124 (i_regs->regmap[hr]&63)!=rs1[i] &&
6125 (i_regs->regmap[hr]&63)!=rs2[i] )
6131 if((opcode[i]&0x2E)==6) // BLEZ/BGTZ needs another register
6135 if(hr!=EXCLUDE_REG && hr!=HOST_CCREG && hr!=HOST_BTREG &&
6136 (i_regs->regmap[hr]&63)!=rs1[i] &&
6137 (i_regs->regmap[hr]&63)!=rs2[i] )
6144 assert(hr<HOST_REGS);
6145 if((opcode[i]&0x2e)==4||opcode[i]==0x11) { // BEQ/BNE/BEQL/BNEL/BC1
6146 load_regs(regs[i].regmap_entry,regs[i].regmap,regs[i].was32,CCREG,CCREG);
6148 emit_addimm(HOST_CCREG,CLOCK_DIVIDER*(ccadj[i]+2),HOST_CCREG);
6149 if(opcode[i]==2) // J
6153 if(opcode[i]==3) // JAL
6156 int rt=get_reg(i_regs->regmap,31);
6157 emit_movimm(start+i*4+8,rt);
6160 if(opcode[i]==0&&(opcode2[i]&0x3E)==8) // JR/JALR
6163 if(opcode2[i]==9) // JALR
6165 int rt=get_reg(i_regs->regmap,rt1[i]);
6166 emit_movimm(start+i*4+8,rt);
6169 if((opcode[i]&0x3f)==4) // BEQ
6176 #ifdef HAVE_CMOV_IMM
6178 if(s2l>=0) emit_cmp(s1l,s2l);
6179 else emit_test(s1l,s1l);
6180 emit_cmov2imm_e_ne_compact(ba[i],start+i*4+8,addr);
6186 emit_mov2imm_compact(ba[i],addr,start+i*4+8,alt);
6188 if(s2h>=0) emit_cmp(s1h,s2h);
6189 else emit_test(s1h,s1h);
6190 emit_cmovne_reg(alt,addr);
6192 if(s2l>=0) emit_cmp(s1l,s2l);
6193 else emit_test(s1l,s1l);
6194 emit_cmovne_reg(alt,addr);
6197 if((opcode[i]&0x3f)==5) // BNE
6199 #ifdef HAVE_CMOV_IMM
6201 if(s2l>=0) emit_cmp(s1l,s2l);
6202 else emit_test(s1l,s1l);
6203 emit_cmov2imm_e_ne_compact(start+i*4+8,ba[i],addr);
6209 emit_mov2imm_compact(start+i*4+8,addr,ba[i],alt);
6211 if(s2h>=0) emit_cmp(s1h,s2h);
6212 else emit_test(s1h,s1h);
6213 emit_cmovne_reg(alt,addr);
6215 if(s2l>=0) emit_cmp(s1l,s2l);
6216 else emit_test(s1l,s1l);
6217 emit_cmovne_reg(alt,addr);
6220 if((opcode[i]&0x3f)==0x14) // BEQL
6223 if(s2h>=0) emit_cmp(s1h,s2h);
6224 else emit_test(s1h,s1h);
6228 if(s2l>=0) emit_cmp(s1l,s2l);
6229 else emit_test(s1l,s1l);
6230 if(nottaken) set_jump_target(nottaken,(int)out);
6234 if((opcode[i]&0x3f)==0x15) // BNEL
6237 if(s2h>=0) emit_cmp(s1h,s2h);
6238 else emit_test(s1h,s1h);
6242 if(s2l>=0) emit_cmp(s1l,s2l);
6243 else emit_test(s1l,s1l);
6246 if(taken) set_jump_target(taken,(int)out);
6248 if((opcode[i]&0x3f)==6) // BLEZ
6250 emit_mov2imm_compact(ba[i],alt,start+i*4+8,addr);
6252 if(s1h>=0) emit_mov(addr,ntaddr);
6253 emit_cmovl_reg(alt,addr);
6256 emit_cmovne_reg(ntaddr,addr);
6257 emit_cmovs_reg(alt,addr);
6260 if((opcode[i]&0x3f)==7) // BGTZ
6262 emit_mov2imm_compact(ba[i],addr,start+i*4+8,ntaddr);
6264 if(s1h>=0) emit_mov(addr,alt);
6265 emit_cmovl_reg(ntaddr,addr);
6268 emit_cmovne_reg(alt,addr);
6269 emit_cmovs_reg(ntaddr,addr);
6272 if((opcode[i]&0x3f)==0x16) // BLEZL
6274 assert((opcode[i]&0x3f)!=0x16);
6276 if((opcode[i]&0x3f)==0x17) // BGTZL
6278 assert((opcode[i]&0x3f)!=0x17);
6280 assert(opcode[i]!=1); // BLTZ/BGEZ
6282 //FIXME: Check CSREG
6283 if(opcode[i]==0x11 && opcode2[i]==0x08 ) {
6284 if((source[i]&0x30000)==0) // BC1F
6286 emit_mov2imm_compact(ba[i],addr,start+i*4+8,alt);
6287 emit_testimm(s1l,0x800000);
6288 emit_cmovne_reg(alt,addr);
6290 if((source[i]&0x30000)==0x10000) // BC1T
6292 emit_mov2imm_compact(ba[i],alt,start+i*4+8,addr);
6293 emit_testimm(s1l,0x800000);
6294 emit_cmovne_reg(alt,addr);
6296 if((source[i]&0x30000)==0x20000) // BC1FL
6298 emit_testimm(s1l,0x800000);
6302 if((source[i]&0x30000)==0x30000) // BC1TL
6304 emit_testimm(s1l,0x800000);
6310 assert(i_regs->regmap[HOST_CCREG]==CCREG);
6311 wb_dirtys(regs[i].regmap,regs[i].is32,regs[i].dirty);
6312 if(likely[i]||unconditional)
6314 emit_movimm(ba[i],HOST_BTREG);
6316 else if(addr!=HOST_BTREG)
6318 emit_mov(addr,HOST_BTREG);
6320 void *branch_addr=out;
6322 int target_addr=start+i*4+5;
6324 void *compiled_target_addr=check_addr(target_addr);
6325 emit_extjump_ds((int)branch_addr,target_addr);
6326 if(compiled_target_addr) {
6327 set_jump_target((int)branch_addr,(int)compiled_target_addr);
6328 add_link(target_addr,stub);
6330 else set_jump_target((int)branch_addr,(int)stub);
6333 set_jump_target((int)nottaken,(int)out);
6334 wb_dirtys(regs[i].regmap,regs[i].is32,regs[i].dirty);
6335 void *branch_addr=out;
6337 int target_addr=start+i*4+8;
6339 void *compiled_target_addr=check_addr(target_addr);
6340 emit_extjump_ds((int)branch_addr,target_addr);
6341 if(compiled_target_addr) {
6342 set_jump_target((int)branch_addr,(int)compiled_target_addr);
6343 add_link(target_addr,stub);
6345 else set_jump_target((int)branch_addr,(int)stub);
6349 // Assemble the delay slot for the above
6350 static void pagespan_ds()
6352 assem_debug("initial delay slot:");
6353 u_int vaddr=start+1;
6354 u_int page=(0x80000000^vaddr)>>12;
6356 if(page>262143&&tlb_LUT_r[vaddr>>12]) page=(tlb_LUT_r[page^0x80000]^0x80000000)>>12;
6357 if(page>2048) page=2048+(page&2047);
6358 if(vpage>262143&&tlb_LUT_r[vaddr>>12]) vpage&=2047; // jump_dirty uses a hash of the virtual address instead
6359 if(vpage>2048) vpage=2048+(vpage&2047);
6360 ll_add(jump_dirty+vpage,vaddr,(void *)out);
6362 ll_add(jump_in+page,vaddr,(void *)out);
6363 assert(regs[0].regmap_entry[HOST_CCREG]==CCREG);
6364 if(regs[0].regmap[HOST_CCREG]!=CCREG)
6365 wb_register(CCREG,regs[0].regmap_entry,regs[0].wasdirty,regs[0].was32);
6366 if(regs[0].regmap[HOST_BTREG]!=BTREG)
6367 emit_writeword(HOST_BTREG,(int)&branch_target);
6368 load_regs(regs[0].regmap_entry,regs[0].regmap,regs[0].was32,rs1[0],rs2[0]);
6369 address_generation(0,®s[0],regs[0].regmap_entry);
6370 if(itype[0]==LOAD||itype[0]==LOADLR||itype[0]==STORE||itype[0]==STORELR||itype[0]==C1LS)
6371 load_regs(regs[0].regmap_entry,regs[0].regmap,regs[0].was32,MMREG,ROREG);
6372 if(itype[0]==STORE||itype[0]==STORELR||(opcode[0]&0x3b)==0x39)
6373 load_regs(regs[0].regmap_entry,regs[0].regmap,regs[0].was32,INVCP,INVCP);
6378 alu_assemble(0,®s[0]);break;
6380 imm16_assemble(0,®s[0]);break;
6382 shift_assemble(0,®s[0]);break;
6384 shiftimm_assemble(0,®s[0]);break;
6386 load_assemble(0,®s[0]);break;
6388 loadlr_assemble(0,®s[0]);break;
6390 store_assemble(0,®s[0]);break;
6392 storelr_assemble(0,®s[0]);break;
6394 cop0_assemble(0,®s[0]);break;
6396 cop1_assemble(0,®s[0]);break;
6398 c1ls_assemble(0,®s[0]);break;
6400 fconv_assemble(0,®s[0]);break;
6402 float_assemble(0,®s[0]);break;
6404 fcomp_assemble(0,®s[0]);break;
6406 multdiv_assemble(0,®s[0]);break;
6408 mov_assemble(0,®s[0]);break;
6416 DebugMessage(M64MSG_VERBOSE, "Jump in the delay slot. This is probably a bug.");
6418 int btaddr=get_reg(regs[0].regmap,BTREG);
6420 btaddr=get_reg(regs[0].regmap,-1);
6421 emit_readword((int)&branch_target,btaddr);
6423 assert(btaddr!=HOST_CCREG);
6424 if(regs[0].regmap[HOST_CCREG]!=CCREG) emit_loadreg(CCREG,HOST_CCREG);
6426 emit_movimm(start+4,HOST_TEMPREG);
6427 emit_cmp(btaddr,HOST_TEMPREG);
6429 emit_cmpimm(btaddr,start+4);
6431 int branch=(int)out;
6433 store_regs_bt(regs[0].regmap,regs[0].is32,regs[0].dirty,-1);
6434 emit_jmp(jump_vaddr_reg[btaddr]);
6435 set_jump_target(branch,(int)out);
6436 store_regs_bt(regs[0].regmap,regs[0].is32,regs[0].dirty,start+4);
6437 load_regs_bt(regs[0].regmap,regs[0].is32,regs[0].dirty,start+4);
6440 // Basic liveness analysis for MIPS registers
6441 static void unneeded_registers(int istart,int iend,int r)
6445 uint64_t temp_u,temp_uu;
6450 u=unneeded_reg[iend+1];
6451 uu=unneeded_reg_upper[iend+1];
6454 for (i=iend;i>=istart;i--)
6456 //DebugMessage(M64MSG_VERBOSE, "unneeded registers i=%d (%d,%d) r=%d",i,istart,iend,r);
6457 if(itype[i]==RJUMP||itype[i]==UJUMP||itype[i]==CJUMP||itype[i]==SJUMP||itype[i]==FJUMP)
6459 // If subroutine call, flag return address as a possible branch target
6460 if(rt1[i]==31 && i<slen-2) bt[i+2]=1;
6462 if(ba[i]<start || ba[i]>=(start+slen*4))
6464 // Branch out of this block, flush all regs
6468 if(itype[i]==UJUMP&&rt1[i]==31)
6470 uu=u=0x300C00F; // Discard at, v0-v1, t6-t9
6472 if(itype[i]==RJUMP&&rs1[i]==31)
6474 uu=u=0x300C0F3; // Discard at, a0-a3, t6-t9
6476 if(start>0x80000400&&start<0x80800000) {
6477 if(itype[i]==UJUMP&&rt1[i]==31)
6479 //uu=u=0x30300FF0FLL; // Discard at, v0-v1, t0-t9, lo, hi
6480 uu=u=0x300FF0F; // Discard at, v0-v1, t0-t9
6482 if(itype[i]==RJUMP&&rs1[i]==31)
6484 //uu=u=0x30300FFF3LL; // Discard at, a0-a3, t0-t9, lo, hi
6485 uu=u=0x300FFF3; // Discard at, a0-a3, t0-t9
6488 branch_unneeded_reg[i]=u;
6489 branch_unneeded_reg_upper[i]=uu;
6490 // Merge in delay slot
6491 tdep=(~uu>>rt1[i+1])&1;
6492 u|=(1LL<<rt1[i+1])|(1LL<<rt2[i+1]);
6493 uu|=(1LL<<rt1[i+1])|(1LL<<rt2[i+1]);
6494 u&=~((1LL<<rs1[i+1])|(1LL<<rs2[i+1]));
6495 uu&=~((1LL<<us1[i+1])|(1LL<<us2[i+1]));
6496 uu&=~((tdep<<dep1[i+1])|(tdep<<dep2[i+1]));
6498 // If branch is "likely" (and conditional)
6499 // then we skip the delay slot on the fall-thru path
6502 u&=unneeded_reg[i+2];
6503 uu&=unneeded_reg_upper[i+2];
6514 // Internal branch, flag target
6515 bt[(ba[i]-start)>>2]=1;
6516 if(ba[i]<=start+i*4) {
6518 if(itype[i]==RJUMP||itype[i]==UJUMP||(source[i]>>16)==0x1000)
6520 // Unconditional branch
6523 // Conditional branch (not taken case)
6524 temp_u=unneeded_reg[i+2];
6525 temp_uu=unneeded_reg_upper[i+2];
6527 // Merge in delay slot
6528 tdep=(~temp_uu>>rt1[i+1])&1;
6529 temp_u|=(1LL<<rt1[i+1])|(1LL<<rt2[i+1]);
6530 temp_uu|=(1LL<<rt1[i+1])|(1LL<<rt2[i+1]);
6531 temp_u&=~((1LL<<rs1[i+1])|(1LL<<rs2[i+1]));
6532 temp_uu&=~((1LL<<us1[i+1])|(1LL<<us2[i+1]));
6533 temp_uu&=~((tdep<<dep1[i+1])|(tdep<<dep2[i+1]));
6534 temp_u|=1;temp_uu|=1;
6535 // If branch is "likely" (and conditional)
6536 // then we skip the delay slot on the fall-thru path
6539 temp_u&=unneeded_reg[i+2];
6540 temp_uu&=unneeded_reg_upper[i+2];
6548 tdep=(~temp_uu>>rt1[i])&1;
6549 temp_u|=(1LL<<rt1[i])|(1LL<<rt2[i]);
6550 temp_uu|=(1LL<<rt1[i])|(1LL<<rt2[i]);
6551 temp_u&=~((1LL<<rs1[i])|(1LL<<rs2[i]));
6552 temp_uu&=~((1LL<<us1[i])|(1LL<<us2[i]));
6553 temp_uu&=~((tdep<<dep1[i])|(tdep<<dep2[i]));
6554 temp_u|=1;temp_uu|=1;
6555 unneeded_reg[i]=temp_u;
6556 unneeded_reg_upper[i]=temp_uu;
6557 // Only go three levels deep. This recursion can take an
6558 // excessive amount of time if there are a lot of nested loops.
6560 unneeded_registers((ba[i]-start)>>2,i-1,r+1);
6562 unneeded_reg[(ba[i]-start)>>2]=1;
6563 unneeded_reg_upper[(ba[i]-start)>>2]=1;
6566 if(itype[i]==RJUMP||itype[i]==UJUMP||(source[i]>>16)==0x1000)
6568 // Unconditional branch
6569 u=unneeded_reg[(ba[i]-start)>>2];
6570 uu=unneeded_reg_upper[(ba[i]-start)>>2];
6571 branch_unneeded_reg[i]=u;
6572 branch_unneeded_reg_upper[i]=uu;
6575 //branch_unneeded_reg[i]=u;
6576 //branch_unneeded_reg_upper[i]=uu;
6577 // Merge in delay slot
6578 tdep=(~uu>>rt1[i+1])&1;
6579 u|=(1LL<<rt1[i+1])|(1LL<<rt2[i+1]);
6580 uu|=(1LL<<rt1[i+1])|(1LL<<rt2[i+1]);
6581 u&=~((1LL<<rs1[i+1])|(1LL<<rs2[i+1]));
6582 uu&=~((1LL<<us1[i+1])|(1LL<<us2[i+1]));
6583 uu&=~((tdep<<dep1[i+1])|(tdep<<dep2[i+1]));
6586 // Conditional branch
6587 b=unneeded_reg[(ba[i]-start)>>2];
6588 bu=unneeded_reg_upper[(ba[i]-start)>>2];
6589 branch_unneeded_reg[i]=b;
6590 branch_unneeded_reg_upper[i]=bu;
6593 //branch_unneeded_reg[i]=b;
6594 //branch_unneeded_reg_upper[i]=bu;
6595 // Branch delay slot
6596 tdep=(~uu>>rt1[i+1])&1;
6597 b|=(1LL<<rt1[i+1])|(1LL<<rt2[i+1]);
6598 bu|=(1LL<<rt1[i+1])|(1LL<<rt2[i+1]);
6599 b&=~((1LL<<rs1[i+1])|(1LL<<rs2[i+1]));
6600 bu&=~((1LL<<us1[i+1])|(1LL<<us2[i+1]));
6601 bu&=~((tdep<<dep1[i+1])|(tdep<<dep2[i+1]));
6603 // If branch is "likely" then we skip the
6604 // delay slot on the fall-thru path
6609 u&=unneeded_reg[i+2];
6610 uu&=unneeded_reg_upper[i+2];
6621 branch_unneeded_reg[i]&=unneeded_reg[i+2];
6622 branch_unneeded_reg_upper[i]&=unneeded_reg_upper[i+2];
6623 //branch_unneeded_reg[i]=1;
6624 //branch_unneeded_reg_upper[i]=1;
6626 branch_unneeded_reg[i]=1;
6627 branch_unneeded_reg_upper[i]=1;
6633 else if(itype[i]==SYSCALL)
6635 // SYSCALL instruction (software interrupt)
6639 else if(itype[i]==COP0 && (source[i]&0x3f)==0x18)
6641 // ERET instruction (return from interrupt)
6646 tdep=(~uu>>rt1[i])&1;
6647 // Written registers are unneeded
6652 // Accessed registers are needed
6657 // Source-target dependencies
6658 uu&=~(tdep<<dep1[i]);
6659 uu&=~(tdep<<dep2[i]);
6660 // R0 is always unneeded
6664 unneeded_reg_upper[i]=uu;
6666 DebugMessage(M64MSG_VERBOSE, "ur (%d,%d) %x: ",istart,iend,start+i*4);
6667 DebugMessage(M64MSG_VERBOSE, "U:");
6669 for(r=1;r<=CCREG;r++) {
6670 if((unneeded_reg[i]>>r)&1) {
6671 if(r==HIREG) DebugMessage(M64MSG_VERBOSE, " HI");
6672 else if(r==LOREG) DebugMessage(M64MSG_VERBOSE, " LO");
6673 else DebugMessage(M64MSG_VERBOSE, " r%d",r);
6676 DebugMessage(M64MSG_VERBOSE, " UU:");
6677 for(r=1;r<=CCREG;r++) {
6678 if(((unneeded_reg_upper[i]&~unneeded_reg[i])>>r)&1) {
6679 if(r==HIREG) DebugMessage(M64MSG_VERBOSE, " HI");
6680 else if(r==LOREG) DebugMessage(M64MSG_VERBOSE, " LO");
6681 else DebugMessage(M64MSG_VERBOSE, " r%d",r);
6687 // Identify registers which are likely to contain 32-bit values
6688 // This is used to predict whether any branches will jump to a
6689 // location with 64-bit values in registers.
6690 static void provisional_32bit()
6694 uint64_t lastbranch=1;
6699 if(itype[i-1]==CJUMP||itype[i-1]==SJUMP||itype[i-1]==FJUMP) {
6700 if(i>1) is32=lastbranch;
6706 if(itype[i-2]==CJUMP||itype[i-2]==SJUMP||itype[i-2]==FJUMP) {
6708 if(i>2) is32=lastbranch;
6712 if((opcode[i-2]&0x2f)==0x05) // BNE/BNEL
6714 if(rs1[i-2]==0||rs2[i-2]==0)
6717 is32|=1LL<<rs1[i-2];
6720 is32|=1LL<<rs2[i-2];
6725 // If something jumps here with 64-bit values
6726 // then promote those registers to 64 bits
6729 uint64_t temp_is32=is32;
6732 if(ba[j]==start+i*4)
6733 //temp_is32&=branch_regs[j].is32;
6738 if(ba[j]==start+i*4)
6749 if(type==UJUMP||type==RJUMP||type==CJUMP||type==SJUMP||type==FJUMP) {
6750 // Branches don't write registers, consider the delay slot instead.
6761 if(opcode[i]==0x27||opcode[i]==0x37|| // LWU/LD
6762 opcode[i]==0x1A||opcode[i]==0x1B) // LDL/LDR
6771 if(op==0x1a||op==0x1b) is32&=~(1LL<<rt); // LDR/LDL
6772 if(op==0x22) is32|=1LL<<rt; // LWL
6775 if (op==0x08||op==0x09|| // ADDI/ADDIU
6776 op==0x0a||op==0x0b|| // SLTI/SLTIU
6782 if(op==0x18||op==0x19) { // DADDI/DADDIU
6785 // is32|=((is32>>s1)&1LL)<<rt;
6787 if(op==0x0d||op==0x0e) { // ORI/XORI
6788 uint64_t sr=((is32>>s1)&1LL);
6804 if(op2>=0x20&&op2<=0x23) { // ADD/ADDU/SUB/SUBU
6807 if(op2==0x2a||op2==0x2b) { // SLT/SLTU
6810 else if(op2>=0x24&&op2<=0x27) { // AND/OR/XOR/NOR
6811 uint64_t sr=((is32>>s1)&(is32>>s2)&1LL);
6815 else if(op2>=0x2c&&op2<=0x2d) { // DADD/DADDU
6820 uint64_t sr=((is32>>s1)&1LL);
6825 uint64_t sr=((is32>>s2)&1LL);
6833 else if(op2>=0x2e&&op2<=0x2f) { // DSUB/DSUBU
6838 uint64_t sr=((is32>>s1)&1LL);
6848 if (op2>=0x1c&&op2<=0x1f) { // DMULT/DMULTU/DDIV/DDIVU
6849 is32&=~((1LL<<HIREG)|(1LL<<LOREG));
6852 is32|=(1LL<<HIREG)|(1LL<<LOREG);
6857 uint64_t sr=((is32>>s1)&1LL);
6863 if(op2>=0x14&&op2<=0x17) is32&=~(1LL<<rt); // DSLLV/DSRLV/DSRAV
6864 else is32|=1LL<<rt; // SLLV/SRLV/SRAV
6868 // DSLL/DSRL/DSRA/DSLL32/DSRL32 but not DSRA32 have 64-bit result
6869 if(op2>=0x38&&op2<0x3f) is32&=~(1LL<<rt);
6872 if(op2==0) is32|=1LL<<rt; // MFC0
6875 if(op2==0) is32|=1LL<<rt; // MFC1
6876 if(op2==1) is32&=~(1LL<<rt); // DMFC1
6877 if(op2==2) is32|=1LL<<rt; // CFC1
6896 if(itype[i-1]==UJUMP||itype[i-1]==RJUMP||(source[i-1]>>16)==0x1000)
6898 if(rt1[i-1]==31) // JAL/JALR
6900 // Subroutine call will return here, don't alloc any registers
6905 // Internal branch will jump here, match registers to caller
6913 // Identify registers which may be assumed to contain 32-bit values
6914 // and where optimizations will rely on this.
6915 // This is used to determine whether backward branches can safely
6916 // jump to a location with 64-bit values in registers.
6917 static void provisional_r32()
6922 for (i=slen-1;i>=0;i--)
6925 if(itype[i]==RJUMP||itype[i]==UJUMP||itype[i]==CJUMP||itype[i]==SJUMP||itype[i]==FJUMP)
6927 if(ba[i]<start || ba[i]>=(start+slen*4))
6929 // Branch out of this block, don't need anything
6935 // Need whatever matches the target
6936 // (and doesn't get overwritten by the delay slot instruction)
6938 int t=(ba[i]-start)>>2;
6939 if(ba[i]>start+i*4) {
6941 //if(!(requires_32bit[t]&~regs[i].was32))
6942 // r32|=requires_32bit[t]&(~(1LL<<rt1[i+1]))&(~(1LL<<rt2[i+1]));
6943 if(!(pr32[t]&~regs[i].was32))
6944 r32|=pr32[t]&(~(1LL<<rt1[i+1]))&(~(1LL<<rt2[i+1]));
6947 if(!(regs[t].was32&~unneeded_reg_upper[t]&~regs[i].was32))
6948 r32|=regs[t].was32&~unneeded_reg_upper[t]&(~(1LL<<rt1[i+1]))&(~(1LL<<rt2[i+1]));
6951 // Conditional branch may need registers for following instructions
6952 if(itype[i]!=RJUMP&&itype[i]!=UJUMP&&(source[i]>>16)!=0x1000)
6955 //r32|=requires_32bit[i+2];
6958 // Mark this address as a branch target since it may be called
6959 // upon return from interrupt
6963 // Merge in delay slot
6965 // These are overwritten unless the branch is "likely"
6966 // and the delay slot is nullified if not taken
6967 r32&=~(1LL<<rt1[i+1]);
6968 r32&=~(1LL<<rt2[i+1]);
6970 // Assume these are needed (delay slot)
6973 if((regs[i].was32>>us1[i+1])&1) r32|=1LL<<us1[i+1];
6977 if((regs[i].was32>>us2[i+1])&1) r32|=1LL<<us2[i+1];
6979 if(dep1[i+1]&&!((unneeded_reg_upper[i]>>dep1[i+1])&1))
6981 if((regs[i].was32>>dep1[i+1])&1) r32|=1LL<<dep1[i+1];
6983 if(dep2[i+1]&&!((unneeded_reg_upper[i]>>dep2[i+1])&1))
6985 if((regs[i].was32>>dep2[i+1])&1) r32|=1LL<<dep2[i+1];
6988 else if(itype[i]==SYSCALL)
6990 // SYSCALL instruction (software interrupt)
6993 else if(itype[i]==COP0 && (source[i]&0x3f)==0x18)
6995 // ERET instruction (return from interrupt)
6999 r32&=~(1LL<<rt1[i]);
7000 r32&=~(1LL<<rt2[i]);
7003 if((regs[i].was32>>us1[i])&1) r32|=1LL<<us1[i];
7007 if((regs[i].was32>>us2[i])&1) r32|=1LL<<us2[i];
7009 if(dep1[i]&&!((unneeded_reg_upper[i]>>dep1[i])&1))
7011 if((regs[i].was32>>dep1[i])&1) r32|=1LL<<dep1[i];
7013 if(dep2[i]&&!((unneeded_reg_upper[i]>>dep2[i])&1))
7015 if((regs[i].was32>>dep2[i])&1) r32|=1LL<<dep2[i];
7017 //requires_32bit[i]=r32;
7020 // Dirty registers which are 32-bit, require 32-bit input
7021 // as they will be written as 32-bit values
7022 for(hr=0;hr<HOST_REGS;hr++)
7024 if(regs[i].regmap_entry[hr]>0&®s[i].regmap_entry[hr]<64) {
7025 if((regs[i].was32>>regs[i].regmap_entry[hr])&(regs[i].wasdirty>>hr)&1) {
7026 if(!((unneeded_reg_upper[i]>>regs[i].regmap_entry[hr])&1))
7027 pr32[i]|=1LL<<regs[i].regmap_entry[hr];
7028 //requires_32bit[i]|=1LL<<regs[i].regmap_entry[hr];
7035 // Write back dirty registers as soon as we will no longer modify them,
7036 // so that we don't end up with lots of writes at the branches.
7037 static void clean_registers(int istart,int iend,int wr)
7041 u_int will_dirty_i,will_dirty_next,temp_will_dirty;
7042 u_int wont_dirty_i,wont_dirty_next,temp_wont_dirty;
7044 will_dirty_i=will_dirty_next=0;
7045 wont_dirty_i=wont_dirty_next=0;
7047 will_dirty_i=will_dirty_next=will_dirty[iend+1];
7048 wont_dirty_i=wont_dirty_next=wont_dirty[iend+1];
7050 for (i=iend;i>=istart;i--)
7052 if(itype[i]==RJUMP||itype[i]==UJUMP||itype[i]==CJUMP||itype[i]==SJUMP||itype[i]==FJUMP)
7054 if(ba[i]<start || ba[i]>=(start+slen*4))
7056 // Branch out of this block, flush all regs
7057 if(itype[i]==RJUMP||itype[i]==UJUMP||(source[i]>>16)==0x1000)
7059 // Unconditional branch
7062 // Merge in delay slot (will dirty)
7063 for(r=0;r<HOST_REGS;r++) {
7064 if(r!=EXCLUDE_REG) {
7065 if((branch_regs[i].regmap[r]&63)==rt1[i]) will_dirty_i|=1<<r;
7066 if((branch_regs[i].regmap[r]&63)==rt2[i]) will_dirty_i|=1<<r;
7067 if((branch_regs[i].regmap[r]&63)==rt1[i+1]) will_dirty_i|=1<<r;
7068 if((branch_regs[i].regmap[r]&63)==rt2[i+1]) will_dirty_i|=1<<r;
7069 if((branch_regs[i].regmap[r]&63)>33) will_dirty_i&=~(1<<r);
7070 if(branch_regs[i].regmap[r]<=0) will_dirty_i&=~(1<<r);
7071 if(branch_regs[i].regmap[r]==CCREG) will_dirty_i|=1<<r;
7072 if((regs[i].regmap[r]&63)==rt1[i]) will_dirty_i|=1<<r;
7073 if((regs[i].regmap[r]&63)==rt2[i]) will_dirty_i|=1<<r;
7074 if((regs[i].regmap[r]&63)==rt1[i+1]) will_dirty_i|=1<<r;
7075 if((regs[i].regmap[r]&63)==rt2[i+1]) will_dirty_i|=1<<r;
7076 if((regs[i].regmap[r]&63)>33) will_dirty_i&=~(1<<r);
7077 if(regs[i].regmap[r]<=0) will_dirty_i&=~(1<<r);
7078 if(regs[i].regmap[r]==CCREG) will_dirty_i|=1<<r;
7084 // Conditional branch
7086 wont_dirty_i=wont_dirty_next;
7087 // Merge in delay slot (will dirty)
7088 for(r=0;r<HOST_REGS;r++) {
7089 if(r!=EXCLUDE_REG) {
7091 // Might not dirty if likely branch is not taken
7092 if((branch_regs[i].regmap[r]&63)==rt1[i]) will_dirty_i|=1<<r;
7093 if((branch_regs[i].regmap[r]&63)==rt2[i]) will_dirty_i|=1<<r;
7094 if((branch_regs[i].regmap[r]&63)==rt1[i+1]) will_dirty_i|=1<<r;
7095 if((branch_regs[i].regmap[r]&63)==rt2[i+1]) will_dirty_i|=1<<r;
7096 if((branch_regs[i].regmap[r]&63)>33) will_dirty_i&=~(1<<r);
7097 if(branch_regs[i].regmap[r]==0) will_dirty_i&=~(1<<r);
7098 if(branch_regs[i].regmap[r]==CCREG) will_dirty_i|=1<<r;
7099 //if((regs[i].regmap[r]&63)==rt1[i]) will_dirty_i|=1<<r;
7100 //if((regs[i].regmap[r]&63)==rt2[i]) will_dirty_i|=1<<r;
7101 if((regs[i].regmap[r]&63)==rt1[i+1]) will_dirty_i|=1<<r;
7102 if((regs[i].regmap[r]&63)==rt2[i+1]) will_dirty_i|=1<<r;
7103 if((regs[i].regmap[r]&63)>33) will_dirty_i&=~(1<<r);
7104 if(regs[i].regmap[r]<=0) will_dirty_i&=~(1<<r);
7105 if(regs[i].regmap[r]==CCREG) will_dirty_i|=1<<r;
7110 // Merge in delay slot (wont dirty)
7111 for(r=0;r<HOST_REGS;r++) {
7112 if(r!=EXCLUDE_REG) {
7113 if((regs[i].regmap[r]&63)==rt1[i]) wont_dirty_i|=1<<r;
7114 if((regs[i].regmap[r]&63)==rt2[i]) wont_dirty_i|=1<<r;
7115 if((regs[i].regmap[r]&63)==rt1[i+1]) wont_dirty_i|=1<<r;
7116 if((regs[i].regmap[r]&63)==rt2[i+1]) wont_dirty_i|=1<<r;
7117 if(regs[i].regmap[r]==CCREG) wont_dirty_i|=1<<r;
7118 if((branch_regs[i].regmap[r]&63)==rt1[i]) wont_dirty_i|=1<<r;
7119 if((branch_regs[i].regmap[r]&63)==rt2[i]) wont_dirty_i|=1<<r;
7120 if((branch_regs[i].regmap[r]&63)==rt1[i+1]) wont_dirty_i|=1<<r;
7121 if((branch_regs[i].regmap[r]&63)==rt2[i+1]) wont_dirty_i|=1<<r;
7122 if(branch_regs[i].regmap[r]==CCREG) wont_dirty_i|=1<<r;
7126 #ifndef DESTRUCTIVE_WRITEBACK
7127 branch_regs[i].dirty&=wont_dirty_i;
7129 branch_regs[i].dirty|=will_dirty_i;
7135 if(ba[i]<=start+i*4) {
7137 if(itype[i]==RJUMP||itype[i]==UJUMP||(source[i]>>16)==0x1000)
7139 // Unconditional branch
7142 // Merge in delay slot (will dirty)
7143 for(r=0;r<HOST_REGS;r++) {
7144 if(r!=EXCLUDE_REG) {
7145 if((branch_regs[i].regmap[r]&63)==rt1[i]) temp_will_dirty|=1<<r;
7146 if((branch_regs[i].regmap[r]&63)==rt2[i]) temp_will_dirty|=1<<r;
7147 if((branch_regs[i].regmap[r]&63)==rt1[i+1]) temp_will_dirty|=1<<r;
7148 if((branch_regs[i].regmap[r]&63)==rt2[i+1]) temp_will_dirty|=1<<r;
7149 if((branch_regs[i].regmap[r]&63)>33) temp_will_dirty&=~(1<<r);
7150 if(branch_regs[i].regmap[r]<=0) temp_will_dirty&=~(1<<r);
7151 if(branch_regs[i].regmap[r]==CCREG) temp_will_dirty|=1<<r;
7152 if((regs[i].regmap[r]&63)==rt1[i]) temp_will_dirty|=1<<r;
7153 if((regs[i].regmap[r]&63)==rt2[i]) temp_will_dirty|=1<<r;
7154 if((regs[i].regmap[r]&63)==rt1[i+1]) temp_will_dirty|=1<<r;
7155 if((regs[i].regmap[r]&63)==rt2[i+1]) temp_will_dirty|=1<<r;
7156 if((regs[i].regmap[r]&63)>33) temp_will_dirty&=~(1<<r);
7157 if(regs[i].regmap[r]<=0) temp_will_dirty&=~(1<<r);
7158 if(regs[i].regmap[r]==CCREG) temp_will_dirty|=1<<r;
7162 // Conditional branch (not taken case)
7163 temp_will_dirty=will_dirty_next;
7164 temp_wont_dirty=wont_dirty_next;
7165 // Merge in delay slot (will dirty)
7166 for(r=0;r<HOST_REGS;r++) {
7167 if(r!=EXCLUDE_REG) {
7169 // Will not dirty if likely branch is not taken
7170 if((branch_regs[i].regmap[r]&63)==rt1[i]) temp_will_dirty|=1<<r;
7171 if((branch_regs[i].regmap[r]&63)==rt2[i]) temp_will_dirty|=1<<r;
7172 if((branch_regs[i].regmap[r]&63)==rt1[i+1]) temp_will_dirty|=1<<r;
7173 if((branch_regs[i].regmap[r]&63)==rt2[i+1]) temp_will_dirty|=1<<r;
7174 if((branch_regs[i].regmap[r]&63)>33) temp_will_dirty&=~(1<<r);
7175 if(branch_regs[i].regmap[r]==0) temp_will_dirty&=~(1<<r);
7176 if(branch_regs[i].regmap[r]==CCREG) temp_will_dirty|=1<<r;
7177 //if((regs[i].regmap[r]&63)==rt1[i]) temp_will_dirty|=1<<r;
7178 //if((regs[i].regmap[r]&63)==rt2[i]) temp_will_dirty|=1<<r;
7179 if((regs[i].regmap[r]&63)==rt1[i+1]) temp_will_dirty|=1<<r;
7180 if((regs[i].regmap[r]&63)==rt2[i+1]) temp_will_dirty|=1<<r;
7181 if((regs[i].regmap[r]&63)>33) temp_will_dirty&=~(1<<r);
7182 if(regs[i].regmap[r]<=0) temp_will_dirty&=~(1<<r);
7183 if(regs[i].regmap[r]==CCREG) temp_will_dirty|=1<<r;
7188 // Merge in delay slot (wont dirty)
7189 for(r=0;r<HOST_REGS;r++) {
7190 if(r!=EXCLUDE_REG) {
7191 if((regs[i].regmap[r]&63)==rt1[i]) temp_wont_dirty|=1<<r;
7192 if((regs[i].regmap[r]&63)==rt2[i]) temp_wont_dirty|=1<<r;
7193 if((regs[i].regmap[r]&63)==rt1[i+1]) temp_wont_dirty|=1<<r;
7194 if((regs[i].regmap[r]&63)==rt2[i+1]) temp_wont_dirty|=1<<r;
7195 if(regs[i].regmap[r]==CCREG) temp_wont_dirty|=1<<r;
7196 if((branch_regs[i].regmap[r]&63)==rt1[i]) temp_wont_dirty|=1<<r;
7197 if((branch_regs[i].regmap[r]&63)==rt2[i]) temp_wont_dirty|=1<<r;
7198 if((branch_regs[i].regmap[r]&63)==rt1[i+1]) temp_wont_dirty|=1<<r;
7199 if((branch_regs[i].regmap[r]&63)==rt2[i+1]) temp_wont_dirty|=1<<r;
7200 if(branch_regs[i].regmap[r]==CCREG) temp_wont_dirty|=1<<r;
7203 // Deal with changed mappings
7205 for(r=0;r<HOST_REGS;r++) {
7206 if(r!=EXCLUDE_REG) {
7207 if(regs[i].regmap[r]!=regmap_pre[i][r]) {
7208 temp_will_dirty&=~(1<<r);
7209 temp_wont_dirty&=~(1<<r);
7210 if((regmap_pre[i][r]&63)>0 && (regmap_pre[i][r]&63)<34) {
7211 temp_will_dirty|=((unneeded_reg[i]>>(regmap_pre[i][r]&63))&1)<<r;
7212 temp_wont_dirty|=((unneeded_reg[i]>>(regmap_pre[i][r]&63))&1)<<r;
7214 temp_will_dirty|=1<<r;
7215 temp_wont_dirty|=1<<r;
7222 will_dirty[i]=temp_will_dirty;
7223 wont_dirty[i]=temp_wont_dirty;
7224 clean_registers((ba[i]-start)>>2,i-1,0);
7226 // Limit recursion. It can take an excessive amount
7227 // of time if there are a lot of nested loops.
7228 will_dirty[(ba[i]-start)>>2]=0;
7229 wont_dirty[(ba[i]-start)>>2]=-1;
7234 if(itype[i]==RJUMP||itype[i]==UJUMP||(source[i]>>16)==0x1000)
7236 // Unconditional branch
7239 //if(ba[i]>start+i*4) { // Disable recursion (for debugging)
7240 for(r=0;r<HOST_REGS;r++) {
7241 if(r!=EXCLUDE_REG) {
7242 if(branch_regs[i].regmap[r]==regs[(ba[i]-start)>>2].regmap_entry[r]) {
7243 will_dirty_i|=will_dirty[(ba[i]-start)>>2]&(1<<r);
7244 wont_dirty_i|=wont_dirty[(ba[i]-start)>>2]&(1<<r);
7246 if(branch_regs[i].regmap[r]>=0) {
7247 will_dirty_i|=((unneeded_reg[(ba[i]-start)>>2]>>(branch_regs[i].regmap[r]&63))&1)<<r;
7248 wont_dirty_i|=((unneeded_reg[(ba[i]-start)>>2]>>(branch_regs[i].regmap[r]&63))&1)<<r;
7253 // Merge in delay slot
7254 for(r=0;r<HOST_REGS;r++) {
7255 if(r!=EXCLUDE_REG) {
7256 if((branch_regs[i].regmap[r]&63)==rt1[i]) will_dirty_i|=1<<r;
7257 if((branch_regs[i].regmap[r]&63)==rt2[i]) will_dirty_i|=1<<r;
7258 if((branch_regs[i].regmap[r]&63)==rt1[i+1]) will_dirty_i|=1<<r;
7259 if((branch_regs[i].regmap[r]&63)==rt2[i+1]) will_dirty_i|=1<<r;
7260 if((branch_regs[i].regmap[r]&63)>33) will_dirty_i&=~(1<<r);
7261 if(branch_regs[i].regmap[r]<=0) will_dirty_i&=~(1<<r);
7262 if(branch_regs[i].regmap[r]==CCREG) will_dirty_i|=1<<r;
7263 if((regs[i].regmap[r]&63)==rt1[i]) will_dirty_i|=1<<r;
7264 if((regs[i].regmap[r]&63)==rt2[i]) will_dirty_i|=1<<r;
7265 if((regs[i].regmap[r]&63)==rt1[i+1]) will_dirty_i|=1<<r;
7266 if((regs[i].regmap[r]&63)==rt2[i+1]) will_dirty_i|=1<<r;
7267 if((regs[i].regmap[r]&63)>33) will_dirty_i&=~(1<<r);
7268 if(regs[i].regmap[r]<=0) will_dirty_i&=~(1<<r);
7269 if(regs[i].regmap[r]==CCREG) will_dirty_i|=1<<r;
7273 // Conditional branch
7274 will_dirty_i=will_dirty_next;
7275 wont_dirty_i=wont_dirty_next;
7276 //if(ba[i]>start+i*4) { // Disable recursion (for debugging)
7277 for(r=0;r<HOST_REGS;r++) {
7278 if(r!=EXCLUDE_REG) {
7279 signed char target_reg=branch_regs[i].regmap[r];
7280 if(target_reg==regs[(ba[i]-start)>>2].regmap_entry[r]) {
7281 will_dirty_i&=will_dirty[(ba[i]-start)>>2]&(1<<r);
7282 wont_dirty_i|=wont_dirty[(ba[i]-start)>>2]&(1<<r);
7284 else if(target_reg>=0) {
7285 will_dirty_i&=((unneeded_reg[(ba[i]-start)>>2]>>(target_reg&63))&1)<<r;
7286 wont_dirty_i|=((unneeded_reg[(ba[i]-start)>>2]>>(target_reg&63))&1)<<r;
7288 // Treat delay slot as part of branch too
7289 /*if(regs[i+1].regmap[r]==regs[(ba[i]-start)>>2].regmap_entry[r]) {
7290 will_dirty[i+1]&=will_dirty[(ba[i]-start)>>2]&(1<<r);
7291 wont_dirty[i+1]|=wont_dirty[(ba[i]-start)>>2]&(1<<r);
7295 will_dirty[i+1]&=~(1<<r);
7300 // Merge in delay slot
7301 for(r=0;r<HOST_REGS;r++) {
7302 if(r!=EXCLUDE_REG) {
7304 // Might not dirty if likely branch is not taken
7305 if((branch_regs[i].regmap[r]&63)==rt1[i]) will_dirty_i|=1<<r;
7306 if((branch_regs[i].regmap[r]&63)==rt2[i]) will_dirty_i|=1<<r;
7307 if((branch_regs[i].regmap[r]&63)==rt1[i+1]) will_dirty_i|=1<<r;
7308 if((branch_regs[i].regmap[r]&63)==rt2[i+1]) will_dirty_i|=1<<r;
7309 if((branch_regs[i].regmap[r]&63)>33) will_dirty_i&=~(1<<r);
7310 if(branch_regs[i].regmap[r]<=0) will_dirty_i&=~(1<<r);
7311 if(branch_regs[i].regmap[r]==CCREG) will_dirty_i|=1<<r;
7312 //if((regs[i].regmap[r]&63)==rt1[i]) will_dirty_i|=1<<r;
7313 //if((regs[i].regmap[r]&63)==rt2[i]) will_dirty_i|=1<<r;
7314 if((regs[i].regmap[r]&63)==rt1[i+1]) will_dirty_i|=1<<r;
7315 if((regs[i].regmap[r]&63)==rt2[i+1]) will_dirty_i|=1<<r;
7316 if((regs[i].regmap[r]&63)>33) will_dirty_i&=~(1<<r);
7317 if(regs[i].regmap[r]<=0) will_dirty_i&=~(1<<r);
7318 if(regs[i].regmap[r]==CCREG) will_dirty_i|=1<<r;
7323 // Merge in delay slot (won't dirty)
7324 for(r=0;r<HOST_REGS;r++) {
7325 if(r!=EXCLUDE_REG) {
7326 if((regs[i].regmap[r]&63)==rt1[i]) wont_dirty_i|=1<<r;
7327 if((regs[i].regmap[r]&63)==rt2[i]) wont_dirty_i|=1<<r;
7328 if((regs[i].regmap[r]&63)==rt1[i+1]) wont_dirty_i|=1<<r;
7329 if((regs[i].regmap[r]&63)==rt2[i+1]) wont_dirty_i|=1<<r;
7330 if(regs[i].regmap[r]==CCREG) wont_dirty_i|=1<<r;
7331 if((branch_regs[i].regmap[r]&63)==rt1[i]) wont_dirty_i|=1<<r;
7332 if((branch_regs[i].regmap[r]&63)==rt2[i]) wont_dirty_i|=1<<r;
7333 if((branch_regs[i].regmap[r]&63)==rt1[i+1]) wont_dirty_i|=1<<r;
7334 if((branch_regs[i].regmap[r]&63)==rt2[i+1]) wont_dirty_i|=1<<r;
7335 if(branch_regs[i].regmap[r]==CCREG) wont_dirty_i|=1<<r;
7339 #ifndef DESTRUCTIVE_WRITEBACK
7340 branch_regs[i].dirty&=wont_dirty_i;
7342 branch_regs[i].dirty|=will_dirty_i;
7347 else if(itype[i]==SYSCALL)
7349 // SYSCALL instruction (software interrupt)
7353 else if(itype[i]==COP0 && (source[i]&0x3f)==0x18)
7355 // ERET instruction (return from interrupt)
7359 will_dirty_next=will_dirty_i;
7360 wont_dirty_next=wont_dirty_i;
7361 for(r=0;r<HOST_REGS;r++) {
7362 if(r!=EXCLUDE_REG) {
7363 if((regs[i].regmap[r]&63)==rt1[i]) will_dirty_i|=1<<r;
7364 if((regs[i].regmap[r]&63)==rt2[i]) will_dirty_i|=1<<r;
7365 if((regs[i].regmap[r]&63)>33) will_dirty_i&=~(1<<r);
7366 if(regs[i].regmap[r]<=0) will_dirty_i&=~(1<<r);
7367 if(regs[i].regmap[r]==CCREG) will_dirty_i|=1<<r;
7368 if((regs[i].regmap[r]&63)==rt1[i]) wont_dirty_i|=1<<r;
7369 if((regs[i].regmap[r]&63)==rt2[i]) wont_dirty_i|=1<<r;
7370 if(regs[i].regmap[r]==CCREG) wont_dirty_i|=1<<r;
7372 if(itype[i]!=RJUMP&&itype[i]!=UJUMP&&itype[i]!=CJUMP&&itype[i]!=SJUMP&&itype[i]!=FJUMP)
7374 // Don't store a register immediately after writing it,
7375 // may prevent dual-issue.
7376 if((regs[i].regmap[r]&63)==rt1[i-1]) wont_dirty_i|=1<<r;
7377 if((regs[i].regmap[r]&63)==rt2[i-1]) wont_dirty_i|=1<<r;
7383 will_dirty[i]=will_dirty_i;
7384 wont_dirty[i]=wont_dirty_i;
7385 // Mark registers that won't be dirtied as not dirty
7387 /*DebugMessage(M64MSG_VERBOSE, "wr (%d,%d) %x will:",istart,iend,start+i*4);
7388 for(r=0;r<HOST_REGS;r++) {
7389 if((will_dirty_i>>r)&1) {
7390 DebugMessage(M64MSG_VERBOSE, " r%d",r);
7394 //if(i==istart||(itype[i-1]!=RJUMP&&itype[i-1]!=UJUMP&&itype[i-1]!=CJUMP&&itype[i-1]!=SJUMP&&itype[i-1]!=FJUMP)) {
7395 regs[i].dirty|=will_dirty_i;
7396 #ifndef DESTRUCTIVE_WRITEBACK
7397 regs[i].dirty&=wont_dirty_i;
7398 if(itype[i]==RJUMP||itype[i]==UJUMP||itype[i]==CJUMP||itype[i]==SJUMP||itype[i]==FJUMP)
7400 if(i<iend-1&&itype[i]!=RJUMP&&itype[i]!=UJUMP&&(source[i]>>16)!=0x1000) {
7401 for(r=0;r<HOST_REGS;r++) {
7402 if(r!=EXCLUDE_REG) {
7403 if(regs[i].regmap[r]==regmap_pre[i+2][r]) {
7404 regs[i+2].wasdirty&=wont_dirty_i|~(1<<r);
7405 }else {/*DebugMessage(M64MSG_VERBOSE, "i: %x (%d) mismatch(+2): %d",start+i*4,i,r); / *assert(!((wont_dirty_i>>r)&1));*/}
7413 for(r=0;r<HOST_REGS;r++) {
7414 if(r!=EXCLUDE_REG) {
7415 if(regs[i].regmap[r]==regmap_pre[i+1][r]) {
7416 regs[i+1].wasdirty&=wont_dirty_i|~(1<<r);
7417 }else {/*DebugMessage(M64MSG_VERBOSE, "i: %x (%d) mismatch(+1): %d",start+i*4,i,r);/ *assert(!((wont_dirty_i>>r)&1));*/}
7425 // Deal with changed mappings
7426 temp_will_dirty=will_dirty_i;
7427 temp_wont_dirty=wont_dirty_i;
7428 for(r=0;r<HOST_REGS;r++) {
7429 if(r!=EXCLUDE_REG) {
7431 if(regs[i].regmap[r]==regmap_pre[i][r]) {
7433 #ifndef DESTRUCTIVE_WRITEBACK
7434 regs[i].wasdirty&=wont_dirty_i|~(1<<r);
7436 regs[i].wasdirty|=will_dirty_i&(1<<r);
7439 else if((nr=get_reg(regs[i].regmap,regmap_pre[i][r]))>=0) {
7440 // Register moved to a different register
7441 will_dirty_i&=~(1<<r);
7442 wont_dirty_i&=~(1<<r);
7443 will_dirty_i|=((temp_will_dirty>>nr)&1)<<r;
7444 wont_dirty_i|=((temp_wont_dirty>>nr)&1)<<r;
7446 #ifndef DESTRUCTIVE_WRITEBACK
7447 regs[i].wasdirty&=wont_dirty_i|~(1<<r);
7449 regs[i].wasdirty|=will_dirty_i&(1<<r);
7453 will_dirty_i&=~(1<<r);
7454 wont_dirty_i&=~(1<<r);
7455 if((regmap_pre[i][r]&63)>0 && (regmap_pre[i][r]&63)<34) {
7456 will_dirty_i|=((unneeded_reg[i]>>(regmap_pre[i][r]&63))&1)<<r;
7457 wont_dirty_i|=((unneeded_reg[i]>>(regmap_pre[i][r]&63))&1)<<r;
7460 /*DebugMessage(M64MSG_VERBOSE, "i: %x (%d) mismatch: %d",start+i*4,i,r);/ *assert(!((will_dirty>>r)&1));*/
7470 static void disassemble_inst(int i)
7472 if (bt[i]) DebugMessage(M64MSG_VERBOSE, "*"); else DebugMessage(M64MSG_VERBOSE, " ");
7475 printf (" %x: %s %8x",start+i*4,insn[i],ba[i]);break;
7477 printf (" %x: %s r%d,r%d,%8x",start+i*4,insn[i],rs1[i],rs2[i],i?start+i*4+4+((signed int)((unsigned int)source[i]<<16)>>14):*ba);break;
7479 printf (" %x: %s r%d,%8x",start+i*4,insn[i],rs1[i],start+i*4+4+((signed int)((unsigned int)source[i]<<16)>>14));break;
7481 printf (" %x: %s %8x",start+i*4,insn[i],ba[i]);break;
7483 if ((opcode2[i]&1)&&rt1[i]!=31)
7484 printf (" %x: %s r%d,r%d",start+i*4,insn[i],rt1[i],rs1[i]);
7486 printf (" %x: %s r%d",start+i*4,insn[i],rs1[i]);
7489 printf (" %x: %s (pagespan) r%d,r%d,%8x",start+i*4,insn[i],rs1[i],rs2[i],ba[i]);break;
7491 if(opcode[i]==0xf) //LUI
7492 printf (" %x: %s r%d,%4x0000",start+i*4,insn[i],rt1[i],imm[i]&0xffff);
7494 printf (" %x: %s r%d,r%d,%d",start+i*4,insn[i],rt1[i],rs1[i],imm[i]);
7498 printf (" %x: %s r%d,r%d+%x",start+i*4,insn[i],rt1[i],rs1[i],imm[i]);
7502 printf (" %x: %s r%d,r%d+%x",start+i*4,insn[i],rs2[i],rs1[i],imm[i]);
7506 printf (" %x: %s r%d,r%d,r%d",start+i*4,insn[i],rt1[i],rs1[i],rs2[i]);
7509 printf (" %x: %s r%d,r%d",start+i*4,insn[i],rs1[i],rs2[i]);
7512 printf (" %x: %s r%d,r%d,%d",start+i*4,insn[i],rt1[i],rs1[i],imm[i]);
7515 if((opcode2[i]&0x1d)==0x10)
7516 printf (" %x: %s r%d",start+i*4,insn[i],rt1[i]);
7517 else if((opcode2[i]&0x1d)==0x11)
7518 printf (" %x: %s r%d",start+i*4,insn[i],rs1[i]);
7520 printf (" %x: %s",start+i*4,insn[i]);
7524 printf (" %x: %s r%d,cpr0[%d]",start+i*4,insn[i],rt1[i],(source[i]>>11)&0x1f); // MFC0
7525 else if(opcode2[i]==4)
7526 printf (" %x: %s r%d,cpr0[%d]",start+i*4,insn[i],rs1[i],(source[i]>>11)&0x1f); // MTC0
7527 else printf (" %x: %s",start+i*4,insn[i]);
7531 printf (" %x: %s r%d,cpr1[%d]",start+i*4,insn[i],rt1[i],(source[i]>>11)&0x1f); // MFC1
7532 else if(opcode2[i]>3)
7533 printf (" %x: %s r%d,cpr1[%d]",start+i*4,insn[i],rs1[i],(source[i]>>11)&0x1f); // MTC1
7534 else printf (" %x: %s",start+i*4,insn[i]);
7537 printf (" %x: %s cpr1[%d],r%d+%x",start+i*4,insn[i],(source[i]>>16)&0x1f,rs1[i],imm[i]);
7540 //printf (" %s %8x",insn[i],source[i]);
7541 printf (" %x: %s",start+i*4,insn[i]);
7546 void new_dynarec_init()
7548 DebugMessage(M64MSG_INFO, "Init new dynarec");
7550 #if NEW_DYNAREC == NEW_DYNAREC_ARM
7551 if ((base_addr = mmap ((u_char *)BASE_ADDR, 1<<TARGET_SIZE_2,
7552 PROT_READ | PROT_WRITE | PROT_EXEC,
7553 MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS,
7554 -1, 0)) <= 0) {DebugMessage(M64MSG_ERROR, "mmap() failed");}
7556 if ((base_addr = mmap (NULL, 1<<TARGET_SIZE_2,
7557 PROT_READ | PROT_WRITE | PROT_EXEC,
7558 MAP_PRIVATE | MAP_ANONYMOUS,
7559 -1, 0)) <= 0) {DebugMessage(M64MSG_ERROR, "mmap() failed");}
7561 out=(u_char *)base_addr;
7563 rdword=&readmem_dword;
7564 fake_pc.f.r.rs=(long long int *)&readmem_dword;
7565 fake_pc.f.r.rt=(long long int *)&readmem_dword;
7566 fake_pc.f.r.rd=(long long int *)&readmem_dword;
7568 for(n=0x80000;n<0x80800;n++)
7570 for(n=0;n<65536;n++)
7571 hash_table[n][0]=hash_table[n][2]=-1;
7572 memset(mini_ht,-1,sizeof(mini_ht));
7573 memset(restore_candidate,0,sizeof(restore_candidate));
7575 expirep=16384; // Expiry pointer, +2 blocks
7576 pending_exception=0;
7579 // Copy this into local area so we don't have to put it in every literal pool
7580 invc_ptr=invalid_code;
7585 for(n=0;n<524288;n++) // 0 .. 0x7FFFFFFF
7587 for(n=524288;n<526336;n++) // 0x80000000 .. 0x807FFFFF
7588 memory_map[n]=((u_int)rdram-0x80000000)>>2;
7589 for(n=526336;n<1048576;n++) // 0x80800000 .. 0xFFFFFFFF
7591 for(n=0;n<0x8000;n++) { // 0 .. 0x7FFFFFFF
7592 writemem[n] = write_nomem_new;
7593 writememb[n] = write_nomemb_new;
7594 writememh[n] = write_nomemh_new;
7595 writememd[n] = write_nomemd_new;
7596 readmem[n] = read_nomem_new;
7597 readmemb[n] = read_nomemb_new;
7598 readmemh[n] = read_nomemh_new;
7599 readmemd[n] = read_nomemd_new;
7601 for(n=0x8000;n<0x8080;n++) { // 0x80000000 .. 0x807FFFFF
7602 writemem[n] = write_rdram_new;
7603 writememb[n] = write_rdramb_new;
7604 writememh[n] = write_rdramh_new;
7605 writememd[n] = write_rdramd_new;
7607 for(n=0xC000;n<0x10000;n++) { // 0xC0000000 .. 0xFFFFFFFF
7608 writemem[n] = write_nomem_new;
7609 writememb[n] = write_nomemb_new;
7610 writememh[n] = write_nomemh_new;
7611 writememd[n] = write_nomemd_new;
7612 readmem[n] = read_nomem_new;
7613 readmemb[n] = read_nomemb_new;
7614 readmemh[n] = read_nomemh_new;
7615 readmemd[n] = read_nomemd_new;
7621 void new_dynarec_cleanup()
7624 if (munmap (base_addr, 1<<TARGET_SIZE_2) < 0) {DebugMessage(M64MSG_ERROR, "munmap() failed");}
7625 for(n=0;n<4096;n++) ll_clear(jump_in+n);
7626 for(n=0;n<4096;n++) ll_clear(jump_out+n);
7627 for(n=0;n<4096;n++) ll_clear(jump_dirty+n);
7629 if (munmap (ROM_COPY, 67108864) < 0) {DebugMessage(M64MSG_ERROR, "munmap() failed");}
7633 int new_recompile_block(int addr)
7636 if(addr==0x800cd050) {
7638 for(block=0x80000;block<0x80800;block++) invalidate_block(block);
7640 for(n=0;n<=2048;n++) ll_clear(jump_dirty+n);
7643 //if(Count==365117028) tracedebug=1;
7644 assem_debug("NOTCOMPILED: addr = %x -> %x", (int)addr, (int)out);
7645 #if defined (COUNT_NOTCOMPILEDS )
7647 log_message( "notcompiledCount=%i", notcompiledCount );
7649 //DebugMessage(M64MSG_VERBOSE, "NOTCOMPILED: addr = %x -> %x", (int)addr, (int)out);
7650 //DebugMessage(M64MSG_VERBOSE, "TRACE: count=%d next=%d (compile %x)",Count,next_interupt,addr);
7652 //DebugMessage(M64MSG_VERBOSE, "TRACE: count=%d next=%d (checksum %x)",Count,next_interupt,mchecksum());
7653 //DebugMessage(M64MSG_VERBOSE, "fpu mapping=%x enabled=%x",(Status & 0x04000000)>>26,(Status & 0x20000000)>>29);
7654 /*if(Count>=312978186) {
7658 start = (u_int)addr&~3;
7659 //assert(((u_int)addr&1)==0);
7660 if ((int)addr >= 0xa4000000 && (int)addr < 0xa4001000) {
7661 source = (u_int *)((u_int)SP_DMEM+start-0xa4000000);
7662 pagelimit = 0xa4001000;
7664 else if ((int)addr >= 0x80000000 && (int)addr < 0x80800000) {
7665 source = (u_int *)((u_int)rdram+start-0x80000000);
7666 pagelimit = 0x80800000;
7668 else if ((signed int)addr >= (signed int)0xC0000000) {
7669 //DebugMessage(M64MSG_VERBOSE, "addr=%x mm=%x",(u_int)addr,(memory_map[start>>12]<<2));
7670 //if(tlb_LUT_r[start>>12])
7671 //source = (u_int *)(((int)rdram)+(tlb_LUT_r[start>>12]&0xFFFFF000)+(((int)addr)&0xFFF)-0x80000000);
7672 if((signed int)memory_map[start>>12]>=0) {
7673 source = (u_int *)((u_int)(start+(memory_map[start>>12]<<2)));
7674 pagelimit=(start+4096)&0xFFFFF000;
7675 int map=memory_map[start>>12];
7678 //DebugMessage(M64MSG_VERBOSE, "start: %x next: %x",map,memory_map[pagelimit>>12]);
7679 if((map&0xBFFFFFFF)==(memory_map[pagelimit>>12]&0xBFFFFFFF)) pagelimit+=4096;
7681 assem_debug("pagelimit=%x",pagelimit);
7682 assem_debug("mapping=%x (%x)",memory_map[start>>12],(memory_map[start>>12]<<2)+start);
7685 assem_debug("Compile at unmapped memory address: %x ", (int)addr);
7686 //assem_debug("start: %x next: %x",memory_map[start>>12],memory_map[(start+4096)>>12]);
7687 return 1; // Caller will invoke exception handler
7689 //DebugMessage(M64MSG_VERBOSE, "source= %x",(int)source);
7692 //DebugMessage(M64MSG_VERBOSE, "Compile at bogus memory address: %x ", (int)addr);
7693 log_message("Compile at bogus memory address: %x", (int)addr);
7697 /* Pass 1: disassemble */
7698 /* Pass 2: register dependencies, branch targets */
7699 /* Pass 3: register allocation */
7700 /* Pass 4: branch dependencies */
7701 /* Pass 5: pre-alloc */
7702 /* Pass 6: optimize clean/dirty state */
7703 /* Pass 7: flag 32-bit registers */
7704 /* Pass 8: assembly */
7705 /* Pass 9: linker */
7706 /* Pass 10: garbage collection / free memory */
7710 unsigned int type,op,op2;
7712 //DebugMessage(M64MSG_VERBOSE, "addr = %x source = %x %x", addr,source,source[0]);
7714 /* Pass 1 disassembly */
7716 for(i=0;!done;i++) {
7717 bt[i]=0;likely[i]=0;ooo[i]=0;op2=0;
7718 minimum_free_regs[i]=0;
7719 opcode[i]=op=source[i]>>26;
7722 case 0x00: strcpy(insn[i],"special"); type=NI;
7726 case 0x00: strcpy(insn[i],"SLL"); type=SHIFTIMM; break;
7727 case 0x02: strcpy(insn[i],"SRL"); type=SHIFTIMM; break;
7728 case 0x03: strcpy(insn[i],"SRA"); type=SHIFTIMM; break;
7729 case 0x04: strcpy(insn[i],"SLLV"); type=SHIFT; break;
7730 case 0x06: strcpy(insn[i],"SRLV"); type=SHIFT; break;
7731 case 0x07: strcpy(insn[i],"SRAV"); type=SHIFT; break;
7732 case 0x08: strcpy(insn[i],"JR"); type=RJUMP; break;
7733 case 0x09: strcpy(insn[i],"JALR"); type=RJUMP; break;
7734 case 0x0C: strcpy(insn[i],"SYSCALL"); type=SYSCALL; break;
7735 case 0x0D: strcpy(insn[i],"BREAK"); type=OTHER; break;
7736 case 0x0F: strcpy(insn[i],"SYNC"); type=OTHER; break;
7737 case 0x10: strcpy(insn[i],"MFHI"); type=MOV; break;
7738 case 0x11: strcpy(insn[i],"MTHI"); type=MOV; break;
7739 case 0x12: strcpy(insn[i],"MFLO"); type=MOV; break;
7740 case 0x13: strcpy(insn[i],"MTLO"); type=MOV; break;
7741 case 0x14: strcpy(insn[i],"DSLLV"); type=SHIFT; break;
7742 case 0x16: strcpy(insn[i],"DSRLV"); type=SHIFT; break;
7743 case 0x17: strcpy(insn[i],"DSRAV"); type=SHIFT; break;
7744 case 0x18: strcpy(insn[i],"MULT"); type=MULTDIV; break;
7745 case 0x19: strcpy(insn[i],"MULTU"); type=MULTDIV; break;
7746 case 0x1A: strcpy(insn[i],"DIV"); type=MULTDIV; break;
7747 case 0x1B: strcpy(insn[i],"DIVU"); type=MULTDIV; break;
7748 case 0x1C: strcpy(insn[i],"DMULT"); type=MULTDIV; break;
7749 case 0x1D: strcpy(insn[i],"DMULTU"); type=MULTDIV; break;
7750 case 0x1E: strcpy(insn[i],"DDIV"); type=MULTDIV; break;
7751 case 0x1F: strcpy(insn[i],"DDIVU"); type=MULTDIV; break;
7752 case 0x20: strcpy(insn[i],"ADD"); type=ALU; break;
7753 case 0x21: strcpy(insn[i],"ADDU"); type=ALU; break;
7754 case 0x22: strcpy(insn[i],"SUB"); type=ALU; break;
7755 case 0x23: strcpy(insn[i],"SUBU"); type=ALU; break;
7756 case 0x24: strcpy(insn[i],"AND"); type=ALU; break;
7757 case 0x25: strcpy(insn[i],"OR"); type=ALU; break;
7758 case 0x26: strcpy(insn[i],"XOR"); type=ALU; break;
7759 case 0x27: strcpy(insn[i],"NOR"); type=ALU; break;
7760 case 0x2A: strcpy(insn[i],"SLT"); type=ALU; break;
7761 case 0x2B: strcpy(insn[i],"SLTU"); type=ALU; break;
7762 case 0x2C: strcpy(insn[i],"DADD"); type=ALU; break;
7763 case 0x2D: strcpy(insn[i],"DADDU"); type=ALU; break;
7764 case 0x2E: strcpy(insn[i],"DSUB"); type=ALU; break;
7765 case 0x2F: strcpy(insn[i],"DSUBU"); type=ALU; break;
7766 case 0x30: strcpy(insn[i],"TGE"); type=NI; break;
7767 case 0x31: strcpy(insn[i],"TGEU"); type=NI; break;
7768 case 0x32: strcpy(insn[i],"TLT"); type=NI; break;
7769 case 0x33: strcpy(insn[i],"TLTU"); type=NI; break;
7770 case 0x34: strcpy(insn[i],"TEQ"); type=NI; break;
7771 case 0x36: strcpy(insn[i],"TNE"); type=NI; break;
7772 case 0x38: strcpy(insn[i],"DSLL"); type=SHIFTIMM; break;
7773 case 0x3A: strcpy(insn[i],"DSRL"); type=SHIFTIMM; break;
7774 case 0x3B: strcpy(insn[i],"DSRA"); type=SHIFTIMM; break;
7775 case 0x3C: strcpy(insn[i],"DSLL32"); type=SHIFTIMM; break;
7776 case 0x3E: strcpy(insn[i],"DSRL32"); type=SHIFTIMM; break;
7777 case 0x3F: strcpy(insn[i],"DSRA32"); type=SHIFTIMM; break;
7780 case 0x01: strcpy(insn[i],"regimm"); type=NI;
7781 op2=(source[i]>>16)&0x1f;
7784 case 0x00: strcpy(insn[i],"BLTZ"); type=SJUMP; break;
7785 case 0x01: strcpy(insn[i],"BGEZ"); type=SJUMP; break;
7786 case 0x02: strcpy(insn[i],"BLTZL"); type=SJUMP; break;
7787 case 0x03: strcpy(insn[i],"BGEZL"); type=SJUMP; break;
7788 case 0x08: strcpy(insn[i],"TGEI"); type=NI; break;
7789 case 0x09: strcpy(insn[i],"TGEIU"); type=NI; break;
7790 case 0x0A: strcpy(insn[i],"TLTI"); type=NI; break;
7791 case 0x0B: strcpy(insn[i],"TLTIU"); type=NI; break;
7792 case 0x0C: strcpy(insn[i],"TEQI"); type=NI; break;
7793 case 0x0E: strcpy(insn[i],"TNEI"); type=NI; break;
7794 case 0x10: strcpy(insn[i],"BLTZAL"); type=SJUMP; break;
7795 case 0x11: strcpy(insn[i],"BGEZAL"); type=SJUMP; break;
7796 case 0x12: strcpy(insn[i],"BLTZALL"); type=SJUMP; break;
7797 case 0x13: strcpy(insn[i],"BGEZALL"); type=SJUMP; break;
7800 case 0x02: strcpy(insn[i],"J"); type=UJUMP; break;
7801 case 0x03: strcpy(insn[i],"JAL"); type=UJUMP; break;
7802 case 0x04: strcpy(insn[i],"BEQ"); type=CJUMP; break;
7803 case 0x05: strcpy(insn[i],"BNE"); type=CJUMP; break;
7804 case 0x06: strcpy(insn[i],"BLEZ"); type=CJUMP; break;
7805 case 0x07: strcpy(insn[i],"BGTZ"); type=CJUMP; break;
7806 case 0x08: strcpy(insn[i],"ADDI"); type=IMM16; break;
7807 case 0x09: strcpy(insn[i],"ADDIU"); type=IMM16; break;
7808 case 0x0A: strcpy(insn[i],"SLTI"); type=IMM16; break;
7809 case 0x0B: strcpy(insn[i],"SLTIU"); type=IMM16; break;
7810 case 0x0C: strcpy(insn[i],"ANDI"); type=IMM16; break;
7811 case 0x0D: strcpy(insn[i],"ORI"); type=IMM16; break;
7812 case 0x0E: strcpy(insn[i],"XORI"); type=IMM16; break;
7813 case 0x0F: strcpy(insn[i],"LUI"); type=IMM16; break;
7814 case 0x10: strcpy(insn[i],"cop0"); type=NI;
7815 op2=(source[i]>>21)&0x1f;
7818 case 0x00: strcpy(insn[i],"MFC0"); type=COP0; break;
7819 case 0x04: strcpy(insn[i],"MTC0"); type=COP0; break;
7820 case 0x10: strcpy(insn[i],"tlb"); type=NI;
7821 switch(source[i]&0x3f)
7823 case 0x01: strcpy(insn[i],"TLBR"); type=COP0; break;
7824 case 0x02: strcpy(insn[i],"TLBWI"); type=COP0; break;
7825 case 0x06: strcpy(insn[i],"TLBWR"); type=COP0; break;
7826 case 0x08: strcpy(insn[i],"TLBP"); type=COP0; break;
7827 case 0x18: strcpy(insn[i],"ERET"); type=COP0; break;
7831 case 0x11: strcpy(insn[i],"cop1"); type=NI;
7832 op2=(source[i]>>21)&0x1f;
7835 case 0x00: strcpy(insn[i],"MFC1"); type=COP1; break;
7836 case 0x01: strcpy(insn[i],"DMFC1"); type=COP1; break;
7837 case 0x02: strcpy(insn[i],"CFC1"); type=COP1; break;
7838 case 0x04: strcpy(insn[i],"MTC1"); type=COP1; break;
7839 case 0x05: strcpy(insn[i],"DMTC1"); type=COP1; break;
7840 case 0x06: strcpy(insn[i],"CTC1"); type=COP1; break;
7841 case 0x08: strcpy(insn[i],"BC1"); type=FJUMP;
7842 switch((source[i]>>16)&0x3)
7844 case 0x00: strcpy(insn[i],"BC1F"); break;
7845 case 0x01: strcpy(insn[i],"BC1T"); break;
7846 case 0x02: strcpy(insn[i],"BC1FL"); break;
7847 case 0x03: strcpy(insn[i],"BC1TL"); break;
7850 case 0x10: strcpy(insn[i],"C1.S"); type=NI;
7851 switch(source[i]&0x3f)
7853 case 0x00: strcpy(insn[i],"ADD.S"); type=FLOAT; break;
7854 case 0x01: strcpy(insn[i],"SUB.S"); type=FLOAT; break;
7855 case 0x02: strcpy(insn[i],"MUL.S"); type=FLOAT; break;
7856 case 0x03: strcpy(insn[i],"DIV.S"); type=FLOAT; break;
7857 case 0x04: strcpy(insn[i],"SQRT.S"); type=FLOAT; break;
7858 case 0x05: strcpy(insn[i],"ABS.S"); type=FLOAT; break;
7859 case 0x06: strcpy(insn[i],"MOV.S"); type=FLOAT; break;
7860 case 0x07: strcpy(insn[i],"NEG.S"); type=FLOAT; break;
7861 case 0x08: strcpy(insn[i],"ROUND.L.S"); type=FCONV; break;
7862 case 0x09: strcpy(insn[i],"TRUNC.L.S"); type=FCONV; break;
7863 case 0x0A: strcpy(insn[i],"CEIL.L.S"); type=FCONV; break;
7864 case 0x0B: strcpy(insn[i],"FLOOR.L.S"); type=FCONV; break;
7865 case 0x0C: strcpy(insn[i],"ROUND.W.S"); type=FCONV; break;
7866 case 0x0D: strcpy(insn[i],"TRUNC.W.S"); type=FCONV; break;
7867 case 0x0E: strcpy(insn[i],"CEIL.W.S"); type=FCONV; break;
7868 case 0x0F: strcpy(insn[i],"FLOOR.W.S"); type=FCONV; break;
7869 case 0x21: strcpy(insn[i],"CVT.D.S"); type=FCONV; break;
7870 case 0x24: strcpy(insn[i],"CVT.W.S"); type=FCONV; break;
7871 case 0x25: strcpy(insn[i],"CVT.L.S"); type=FCONV; break;
7872 case 0x30: strcpy(insn[i],"C.F.S"); type=FCOMP; break;
7873 case 0x31: strcpy(insn[i],"C.UN.S"); type=FCOMP; break;
7874 case 0x32: strcpy(insn[i],"C.EQ.S"); type=FCOMP; break;
7875 case 0x33: strcpy(insn[i],"C.UEQ.S"); type=FCOMP; break;
7876 case 0x34: strcpy(insn[i],"C.OLT.S"); type=FCOMP; break;
7877 case 0x35: strcpy(insn[i],"C.ULT.S"); type=FCOMP; break;
7878 case 0x36: strcpy(insn[i],"C.OLE.S"); type=FCOMP; break;
7879 case 0x37: strcpy(insn[i],"C.ULE.S"); type=FCOMP; break;
7880 case 0x38: strcpy(insn[i],"C.SF.S"); type=FCOMP; break;
7881 case 0x39: strcpy(insn[i],"C.NGLE.S"); type=FCOMP; break;
7882 case 0x3A: strcpy(insn[i],"C.SEQ.S"); type=FCOMP; break;
7883 case 0x3B: strcpy(insn[i],"C.NGL.S"); type=FCOMP; break;
7884 case 0x3C: strcpy(insn[i],"C.LT.S"); type=FCOMP; break;
7885 case 0x3D: strcpy(insn[i],"C.NGE.S"); type=FCOMP; break;
7886 case 0x3E: strcpy(insn[i],"C.LE.S"); type=FCOMP; break;
7887 case 0x3F: strcpy(insn[i],"C.NGT.S"); type=FCOMP; break;
7890 case 0x11: strcpy(insn[i],"C1.D"); type=NI;
7891 switch(source[i]&0x3f)
7893 case 0x00: strcpy(insn[i],"ADD.D"); type=FLOAT; break;
7894 case 0x01: strcpy(insn[i],"SUB.D"); type=FLOAT; break;
7895 case 0x02: strcpy(insn[i],"MUL.D"); type=FLOAT; break;
7896 case 0x03: strcpy(insn[i],"DIV.D"); type=FLOAT; break;
7897 case 0x04: strcpy(insn[i],"SQRT.D"); type=FLOAT; break;
7898 case 0x05: strcpy(insn[i],"ABS.D"); type=FLOAT; break;
7899 case 0x06: strcpy(insn[i],"MOV.D"); type=FLOAT; break;
7900 case 0x07: strcpy(insn[i],"NEG.D"); type=FLOAT; break;
7901 case 0x08: strcpy(insn[i],"ROUND.L.D"); type=FCONV; break;
7902 case 0x09: strcpy(insn[i],"TRUNC.L.D"); type=FCONV; break;
7903 case 0x0A: strcpy(insn[i],"CEIL.L.D"); type=FCONV; break;
7904 case 0x0B: strcpy(insn[i],"FLOOR.L.D"); type=FCONV; break;
7905 case 0x0C: strcpy(insn[i],"ROUND.W.D"); type=FCONV; break;
7906 case 0x0D: strcpy(insn[i],"TRUNC.W.D"); type=FCONV; break;
7907 case 0x0E: strcpy(insn[i],"CEIL.W.D"); type=FCONV; break;
7908 case 0x0F: strcpy(insn[i],"FLOOR.W.D"); type=FCONV; break;
7909 case 0x20: strcpy(insn[i],"CVT.S.D"); type=FCONV; break;
7910 case 0x24: strcpy(insn[i],"CVT.W.D"); type=FCONV; break;
7911 case 0x25: strcpy(insn[i],"CVT.L.D"); type=FCONV; break;
7912 case 0x30: strcpy(insn[i],"C.F.D"); type=FCOMP; break;
7913 case 0x31: strcpy(insn[i],"C.UN.D"); type=FCOMP; break;
7914 case 0x32: strcpy(insn[i],"C.EQ.D"); type=FCOMP; break;
7915 case 0x33: strcpy(insn[i],"C.UEQ.D"); type=FCOMP; break;
7916 case 0x34: strcpy(insn[i],"C.OLT.D"); type=FCOMP; break;
7917 case 0x35: strcpy(insn[i],"C.ULT.D"); type=FCOMP; break;
7918 case 0x36: strcpy(insn[i],"C.OLE.D"); type=FCOMP; break;
7919 case 0x37: strcpy(insn[i],"C.ULE.D"); type=FCOMP; break;
7920 case 0x38: strcpy(insn[i],"C.SF.D"); type=FCOMP; break;
7921 case 0x39: strcpy(insn[i],"C.NGLE.D"); type=FCOMP; break;
7922 case 0x3A: strcpy(insn[i],"C.SEQ.D"); type=FCOMP; break;
7923 case 0x3B: strcpy(insn[i],"C.NGL.D"); type=FCOMP; break;
7924 case 0x3C: strcpy(insn[i],"C.LT.D"); type=FCOMP; break;
7925 case 0x3D: strcpy(insn[i],"C.NGE.D"); type=FCOMP; break;
7926 case 0x3E: strcpy(insn[i],"C.LE.D"); type=FCOMP; break;
7927 case 0x3F: strcpy(insn[i],"C.NGT.D"); type=FCOMP; break;
7930 case 0x14: strcpy(insn[i],"C1.W"); type=NI;
7931 switch(source[i]&0x3f)
7933 case 0x20: strcpy(insn[i],"CVT.S.W"); type=FCONV; break;
7934 case 0x21: strcpy(insn[i],"CVT.D.W"); type=FCONV; break;
7937 case 0x15: strcpy(insn[i],"C1.L"); type=NI;
7938 switch(source[i]&0x3f)
7940 case 0x20: strcpy(insn[i],"CVT.S.L"); type=FCONV; break;
7941 case 0x21: strcpy(insn[i],"CVT.D.L"); type=FCONV; break;
7946 case 0x14: strcpy(insn[i],"BEQL"); type=CJUMP; break;
7947 case 0x15: strcpy(insn[i],"BNEL"); type=CJUMP; break;
7948 case 0x16: strcpy(insn[i],"BLEZL"); type=CJUMP; break;
7949 case 0x17: strcpy(insn[i],"BGTZL"); type=CJUMP; break;
7950 case 0x18: strcpy(insn[i],"DADDI"); type=IMM16; break;
7951 case 0x19: strcpy(insn[i],"DADDIU"); type=IMM16; break;
7952 case 0x1A: strcpy(insn[i],"LDL"); type=LOADLR; break;
7953 case 0x1B: strcpy(insn[i],"LDR"); type=LOADLR; break;
7954 case 0x20: strcpy(insn[i],"LB"); type=LOAD; break;
7955 case 0x21: strcpy(insn[i],"LH"); type=LOAD; break;
7956 case 0x22: strcpy(insn[i],"LWL"); type=LOADLR; break;
7957 case 0x23: strcpy(insn[i],"LW"); type=LOAD; break;
7958 case 0x24: strcpy(insn[i],"LBU"); type=LOAD; break;
7959 case 0x25: strcpy(insn[i],"LHU"); type=LOAD; break;
7960 case 0x26: strcpy(insn[i],"LWR"); type=LOADLR; break;
7961 case 0x27: strcpy(insn[i],"LWU"); type=LOAD; break;
7962 case 0x28: strcpy(insn[i],"SB"); type=STORE; break;
7963 case 0x29: strcpy(insn[i],"SH"); type=STORE; break;
7964 case 0x2A: strcpy(insn[i],"SWL"); type=STORELR; break;
7965 case 0x2B: strcpy(insn[i],"SW"); type=STORE; break;
7966 case 0x2C: strcpy(insn[i],"SDL"); type=STORELR; break;
7967 case 0x2D: strcpy(insn[i],"SDR"); type=STORELR; break;
7968 case 0x2E: strcpy(insn[i],"SWR"); type=STORELR; break;
7969 case 0x2F: strcpy(insn[i],"CACHE"); type=NOP; break;
7970 case 0x30: strcpy(insn[i],"LL"); type=NI; break;
7971 case 0x31: strcpy(insn[i],"LWC1"); type=C1LS; break;
7972 case 0x34: strcpy(insn[i],"LLD"); type=NI; break;
7973 case 0x35: strcpy(insn[i],"LDC1"); type=C1LS; break;
7974 case 0x37: strcpy(insn[i],"LD"); type=LOAD; break;
7975 case 0x38: strcpy(insn[i],"SC"); type=NI; break;
7976 case 0x39: strcpy(insn[i],"SWC1"); type=C1LS; break;
7977 case 0x3C: strcpy(insn[i],"SCD"); type=NI; break;
7978 case 0x3D: strcpy(insn[i],"SDC1"); type=C1LS; break;
7979 case 0x3F: strcpy(insn[i],"SD"); type=STORE; break;
7980 default: strcpy(insn[i],"???"); type=NI; break;
7984 /* Get registers/immediates */
7992 rs1[i]=(source[i]>>21)&0x1f;
7994 rt1[i]=(source[i]>>16)&0x1f;
7996 imm[i]=(short)source[i];
8000 rs1[i]=(source[i]>>21)&0x1f;
8001 rs2[i]=(source[i]>>16)&0x1f;
8004 imm[i]=(short)source[i];
8005 if(op==0x2c||op==0x2d||op==0x3f) us1[i]=rs2[i]; // 64-bit SDL/SDR/SD
8008 // LWL/LWR only load part of the register,
8009 // therefore the target register must be treated as a source too
8010 rs1[i]=(source[i]>>21)&0x1f;
8011 rs2[i]=(source[i]>>16)&0x1f;
8012 rt1[i]=(source[i]>>16)&0x1f;
8014 imm[i]=(short)source[i];
8015 if(op==0x1a||op==0x1b) us1[i]=rs2[i]; // LDR/LDL
8016 if(op==0x26) dep1[i]=rt1[i]; // LWR
8019 if (op==0x0f) rs1[i]=0; // LUI instruction has no source register
8020 else rs1[i]=(source[i]>>21)&0x1f;
8022 rt1[i]=(source[i]>>16)&0x1f;
8024 if(op>=0x0c&&op<=0x0e) { // ANDI/ORI/XORI
8025 imm[i]=(unsigned short)source[i];
8027 imm[i]=(short)source[i];
8029 if(op==0x18||op==0x19) us1[i]=rs1[i]; // DADDI/DADDIU
8030 if(op==0x0a||op==0x0b) us1[i]=rs1[i]; // SLTI/SLTIU
8031 if(op==0x0d||op==0x0e) dep1[i]=rs1[i]; // ORI/XORI
8038 // The JAL instruction writes to r31.
8045 rs1[i]=(source[i]>>21)&0x1f;
8049 // The JALR instruction writes to rd.
8051 rt1[i]=(source[i]>>11)&0x1f;
8056 rs1[i]=(source[i]>>21)&0x1f;
8057 rs2[i]=(source[i]>>16)&0x1f;
8060 if(op&2) { // BGTZ/BLEZ
8068 rs1[i]=(source[i]>>21)&0x1f;
8073 if(op2&0x10) { // BxxAL
8075 // NOTE: If the branch is not taken, r31 is still overwritten
8077 likely[i]=(op2&2)>>1;
8084 likely[i]=((source[i])>>17)&1;
8087 rs1[i]=(source[i]>>21)&0x1f; // source
8088 rs2[i]=(source[i]>>16)&0x1f; // subtract amount
8089 rt1[i]=(source[i]>>11)&0x1f; // destination
8091 if(op2==0x2a||op2==0x2b) { // SLT/SLTU
8092 us1[i]=rs1[i];us2[i]=rs2[i];
8094 else if(op2>=0x24&&op2<=0x27) { // AND/OR/XOR/NOR
8095 dep1[i]=rs1[i];dep2[i]=rs2[i];
8097 else if(op2>=0x2c&&op2<=0x2f) { // DADD/DSUB
8098 dep1[i]=rs1[i];dep2[i]=rs2[i];
8102 rs1[i]=(source[i]>>21)&0x1f; // source
8103 rs2[i]=(source[i]>>16)&0x1f; // divisor
8106 if (op2>=0x1c&&op2<=0x1f) { // DMULT/DMULTU/DDIV/DDIVU
8107 us1[i]=rs1[i];us2[i]=rs2[i];
8115 if(op2==0x10) rs1[i]=HIREG; // MFHI
8116 if(op2==0x11) rt1[i]=HIREG; // MTHI
8117 if(op2==0x12) rs1[i]=LOREG; // MFLO
8118 if(op2==0x13) rt1[i]=LOREG; // MTLO
8119 if((op2&0x1d)==0x10) rt1[i]=(source[i]>>11)&0x1f; // MFxx
8120 if((op2&0x1d)==0x11) rs1[i]=(source[i]>>21)&0x1f; // MTxx
8124 rs1[i]=(source[i]>>16)&0x1f; // target of shift
8125 rs2[i]=(source[i]>>21)&0x1f; // shift amount
8126 rt1[i]=(source[i]>>11)&0x1f; // destination
8128 // DSLLV/DSRLV/DSRAV are 64-bit
8129 if(op2>=0x14&&op2<=0x17) us1[i]=rs1[i];
8132 rs1[i]=(source[i]>>16)&0x1f;
8134 rt1[i]=(source[i]>>11)&0x1f;
8136 imm[i]=(source[i]>>6)&0x1f;
8137 // DSxx32 instructions
8138 if(op2>=0x3c) imm[i]|=0x20;
8139 // DSLL/DSRL/DSRA/DSRA32/DSRL32 but not DSLL32 require 64-bit source
8140 if(op2>=0x38&&op2!=0x3c) us1[i]=rs1[i];
8147 if(op2==0) rt1[i]=(source[i]>>16)&0x1F; // MFC0
8148 if(op2==4) rs1[i]=(source[i]>>16)&0x1F; // MTC0
8149 if(op2==4&&((source[i]>>11)&0x1f)==12) rt2[i]=CSREG; // Status
8150 if(op2==16) if((source[i]&0x3f)==0x18) rs2[i]=CCREG; // ERET
8157 if(op2<3) rt1[i]=(source[i]>>16)&0x1F; // MFC1/DMFC1/CFC1
8158 if(op2>3) rs1[i]=(source[i]>>16)&0x1F; // MTC1/DMTC1/CTC1
8159 if(op2==5) us1[i]=rs1[i]; // DMTC1
8163 rs1[i]=(source[i]>>21)&0x1F;
8167 imm[i]=(short)source[i];
8194 /* Calculate branch target addresses */
8196 ba[i]=((start+i*4+4)&0xF0000000)|(((unsigned int)source[i]<<6)>>4);
8197 else if(type==CJUMP&&rs1[i]==rs2[i]&&(op&1))
8198 ba[i]=start+i*4+8; // Ignore never taken branch
8199 else if(type==SJUMP&&rs1[i]==0&&!(op2&1))
8200 ba[i]=start+i*4+8; // Ignore never taken branch
8201 else if(type==CJUMP||type==SJUMP||type==FJUMP)
8202 ba[i]=start+i*4+4+((signed int)((unsigned int)source[i]<<16)>>14);
8204 /* Is this the end of the block? */
8205 if(i>0&&(itype[i-1]==UJUMP||itype[i-1]==RJUMP||(source[i-1]>>16)==0x1000)) {
8206 if(rt1[i-1]==0) { // Continue past subroutine call (JAL)
8208 // Does the block continue due to a branch?
8211 if(ba[j]==start+i*4) done=j=0; // Branch into delay slot
8212 if(ba[j]==start+i*4+4) done=j=0;
8213 if(ba[j]==start+i*4+8) done=j=0;
8217 if(stop_after_jal) done=1;
8219 if((source[i+1]&0xfc00003f)==0x0d) done=1;
8221 // Don't recompile stuff that's already compiled
8222 if(check_addr(start+i*4+4)) done=1;
8223 // Don't get too close to the limit
8224 if(i>MAXBLOCK/2) done=1;
8226 if(i>0&&itype[i-1]==SYSCALL&&stop_after_jal) done=1;
8227 assert(i<MAXBLOCK-1);
8228 if(start+i*4==pagelimit-4) done=1;
8229 assert(start+i*4<pagelimit);
8230 if (i==MAXBLOCK-1) done=1;
8231 // Stop if we're compiling junk
8232 if(itype[i]==NI&&opcode[i]==0x11) {
8233 done=stop_after_jal=1;
8234 DebugMessage(M64MSG_VERBOSE, "Disabled speculative precompilation");
8238 if(itype[i-1]==UJUMP||itype[i-1]==CJUMP||itype[i-1]==SJUMP||itype[i-1]==RJUMP||itype[i-1]==FJUMP) {
8239 if(start+i*4==pagelimit) {
8245 /* Pass 2 - Register dependencies and branch targets */
8247 unneeded_registers(0,slen-1,0);
8249 /* Pass 3 - Register allocation */
8251 struct regstat current; // Current register allocations/status
8254 current.u=unneeded_reg[0];
8255 current.uu=unneeded_reg_upper[0];
8256 clear_all_regs(current.regmap);
8257 alloc_reg(¤t,0,CCREG);
8258 dirty_reg(¤t,CCREG);
8265 provisional_32bit();
8268 // First instruction is delay slot
8273 unneeded_reg_upper[0]=1;
8274 current.regmap[HOST_BTREG]=BTREG;
8282 for(hr=0;hr<HOST_REGS;hr++)
8284 // Is this really necessary?
8285 if(current.regmap[hr]==0) current.regmap[hr]=-1;
8291 if((opcode[i-2]&0x2f)==0x05) // BNE/BNEL
8293 if(rs1[i-2]==0||rs2[i-2]==0)
8296 current.is32|=1LL<<rs1[i-2];
8297 int hr=get_reg(current.regmap,rs1[i-2]|64);
8298 if(hr>=0) current.regmap[hr]=-1;
8301 current.is32|=1LL<<rs2[i-2];
8302 int hr=get_reg(current.regmap,rs2[i-2]|64);
8303 if(hr>=0) current.regmap[hr]=-1;
8308 // If something jumps here with 64-bit values
8309 // then promote those registers to 64 bits
8312 uint64_t temp_is32=current.is32;
8315 if(ba[j]==start+i*4)
8316 temp_is32&=branch_regs[j].is32;
8320 if(ba[j]==start+i*4)
8324 if(temp_is32!=current.is32) {
8325 //DebugMessage(M64MSG_VERBOSE, "dumping 32-bit regs (%x)",start+i*4);
8326 #ifndef DESTRUCTIVE_WRITEBACK
8329 for(hr=0;hr<HOST_REGS;hr++)
8331 int r=current.regmap[hr];
8334 if((current.dirty>>hr)&((current.is32&~temp_is32)>>r)&1) {
8336 //DebugMessage(M64MSG_VERBOSE, "restore %d",r);
8340 current.is32=temp_is32;
8343 memcpy(regmap_pre[i],current.regmap,sizeof(current.regmap));
8344 regs[i].wasconst=current.isconst;
8345 regs[i].was32=current.is32;
8346 regs[i].wasdirty=current.dirty;
8347 #ifdef DESTRUCTIVE_WRITEBACK
8348 // To change a dirty register from 32 to 64 bits, we must write
8349 // it out during the previous cycle (for branches, 2 cycles)
8350 if(i<slen-1&&bt[i+1]&&itype[i-1]!=UJUMP&&itype[i-1]!=CJUMP&&itype[i-1]!=SJUMP&&itype[i-1]!=RJUMP&&itype[i-1]!=FJUMP)
8352 uint64_t temp_is32=current.is32;
8355 if(ba[j]==start+i*4+4)
8356 temp_is32&=branch_regs[j].is32;
8360 if(ba[j]==start+i*4+4)
8364 if(temp_is32!=current.is32) {
8365 //DebugMessage(M64MSG_VERBOSE, "pre-dumping 32-bit regs (%x)",start+i*4);
8366 for(hr=0;hr<HOST_REGS;hr++)
8368 int r=current.regmap[hr];
8371 if((current.dirty>>hr)&((current.is32&~temp_is32)>>(r&63))&1) {
8372 if(itype[i]!=UJUMP&&itype[i]!=CJUMP&&itype[i]!=SJUMP&&itype[i]!=RJUMP&&itype[i]!=FJUMP)
8374 if(rs1[i]!=(r&63)&&rs2[i]!=(r&63))
8376 //DebugMessage(M64MSG_VERBOSE, "dump %d/r%d",hr,r);
8377 current.regmap[hr]=-1;
8378 if(get_reg(current.regmap,r|64)>=0)
8379 current.regmap[get_reg(current.regmap,r|64)]=-1;
8387 else if(i<slen-2&&bt[i+2]&&(source[i-1]>>16)!=0x1000&&(itype[i]==CJUMP||itype[i]==SJUMP||itype[i]==FJUMP))
8389 uint64_t temp_is32=current.is32;
8392 if(ba[j]==start+i*4+8)
8393 temp_is32&=branch_regs[j].is32;
8397 if(ba[j]==start+i*4+8)
8401 if(temp_is32!=current.is32) {
8402 //DebugMessage(M64MSG_VERBOSE, "pre-dumping 32-bit regs (%x)",start+i*4);
8403 for(hr=0;hr<HOST_REGS;hr++)
8405 int r=current.regmap[hr];
8408 if((current.dirty>>hr)&((current.is32&~temp_is32)>>(r&63))&1) {
8409 if(rs1[i]!=(r&63)&&rs2[i]!=(r&63)&&rs1[i+1]!=(r&63)&&rs2[i+1]!=(r&63))
8411 //DebugMessage(M64MSG_VERBOSE, "dump %d/r%d",hr,r);
8412 current.regmap[hr]=-1;
8413 if(get_reg(current.regmap,r|64)>=0)
8414 current.regmap[get_reg(current.regmap,r|64)]=-1;
8422 if(itype[i]!=UJUMP&&itype[i]!=CJUMP&&itype[i]!=SJUMP&&itype[i]!=RJUMP&&itype[i]!=FJUMP) {
8424 current.u=unneeded_reg[i+1]&~((1LL<<rs1[i])|(1LL<<rs2[i]));
8425 current.uu=unneeded_reg_upper[i+1]&~((1LL<<us1[i])|(1LL<<us2[i]));
8426 if((~current.uu>>rt1[i])&1) current.uu&=~((1LL<<dep1[i])|(1LL<<dep2[i]));
8435 current.u=branch_unneeded_reg[i]&~((1LL<<rs1[i+1])|(1LL<<rs2[i+1]));
8436 current.uu=branch_unneeded_reg_upper[i]&~((1LL<<us1[i+1])|(1LL<<us2[i+1]));
8437 if((~current.uu>>rt1[i+1])&1) current.uu&=~((1LL<<dep1[i+1])|(1LL<<dep2[i+1]));
8438 current.u&=~((1LL<<rs1[i])|(1LL<<rs2[i]));
8439 current.uu&=~((1LL<<us1[i])|(1LL<<us2[i]));
8442 } else { DebugMessage(M64MSG_ERROR, "oops, branch at end of block with no delay slot");exit(1); }
8446 ds=0; // Skip delay slot, already allocated as part of branch
8447 // ...but we need to alloc it in case something jumps here
8449 current.u=branch_unneeded_reg[i-1]&unneeded_reg[i+1];
8450 current.uu=branch_unneeded_reg_upper[i-1]&unneeded_reg_upper[i+1];
8452 current.u=branch_unneeded_reg[i-1];
8453 current.uu=branch_unneeded_reg_upper[i-1];
8455 current.u&=~((1LL<<rs1[i])|(1LL<<rs2[i]));
8456 current.uu&=~((1LL<<us1[i])|(1LL<<us2[i]));
8457 if((~current.uu>>rt1[i])&1) current.uu&=~((1LL<<dep1[i])|(1LL<<dep2[i]));
8460 struct regstat temp;
8461 memcpy(&temp,¤t,sizeof(current));
8462 temp.wasdirty=temp.dirty;
8463 temp.was32=temp.is32;
8464 // TODO: Take into account unconditional branches, as below
8465 delayslot_alloc(&temp,i);
8466 memcpy(regs[i].regmap,temp.regmap,sizeof(temp.regmap));
8467 regs[i].wasdirty=temp.wasdirty;
8468 regs[i].was32=temp.was32;
8469 regs[i].dirty=temp.dirty;
8470 regs[i].is32=temp.is32;
8474 // Create entry (branch target) regmap
8475 for(hr=0;hr<HOST_REGS;hr++)
8477 int r=temp.regmap[hr];
8479 if(r!=regmap_pre[i][hr]) {
8480 regs[i].regmap_entry[hr]=-1;
8485 if((current.u>>r)&1) {
8486 regs[i].regmap_entry[hr]=-1;
8487 regs[i].regmap[hr]=-1;
8488 //Don't clear regs in the delay slot as the branch might need them
8489 //current.regmap[hr]=-1;
8491 regs[i].regmap_entry[hr]=r;
8494 if((current.uu>>(r&63))&1) {
8495 regs[i].regmap_entry[hr]=-1;
8496 regs[i].regmap[hr]=-1;
8497 //Don't clear regs in the delay slot as the branch might need them
8498 //current.regmap[hr]=-1;
8500 regs[i].regmap_entry[hr]=r;
8504 // First instruction expects CCREG to be allocated
8505 if(i==0&&hr==HOST_CCREG)
8506 regs[i].regmap_entry[hr]=CCREG;
8508 regs[i].regmap_entry[hr]=-1;
8512 else { // Not delay slot
8515 //current.isconst=0; // DEBUG
8516 //current.wasconst=0; // DEBUG
8517 //regs[i].wasconst=0; // DEBUG
8518 clear_const(¤t,rt1[i]);
8519 alloc_cc(¤t,i);
8520 dirty_reg(¤t,CCREG);
8522 alloc_reg(¤t,i,31);
8523 dirty_reg(¤t,31);
8524 assert(rs1[i+1]!=31&&rs2[i+1]!=31);
8526 alloc_reg(¤t,i,PTEMP);
8528 //current.is32|=1LL<<rt1[i];
8531 delayslot_alloc(¤t,i+1);
8532 //current.isconst=0; // DEBUG
8534 //DebugMessage(M64MSG_VERBOSE, "i=%d, isconst=%x",i,current.isconst);
8537 //current.isconst=0;
8538 //current.wasconst=0;
8539 //regs[i].wasconst=0;
8540 clear_const(¤t,rs1[i]);
8541 clear_const(¤t,rt1[i]);
8542 alloc_cc(¤t,i);
8543 dirty_reg(¤t,CCREG);
8544 if(rs1[i]!=rt1[i+1]&&rs1[i]!=rt2[i+1]) {
8545 alloc_reg(¤t,i,rs1[i]);
8547 alloc_reg(¤t,i,rt1[i]);
8548 dirty_reg(¤t,rt1[i]);
8549 assert(rs1[i+1]!=31&&rs2[i+1]!=31);
8551 alloc_reg(¤t,i,PTEMP);
8555 if(rs1[i]==31) { // JALR
8556 alloc_reg(¤t,i,RHASH);
8557 #ifndef HOST_IMM_ADDR32
8558 alloc_reg(¤t,i,RHTBL);
8562 delayslot_alloc(¤t,i+1);
8564 // The delay slot overwrites our source register,
8565 // allocate a temporary register to hold the old value.
8569 delayslot_alloc(¤t,i+1);
8571 alloc_reg(¤t,i,RTEMP);
8573 //current.isconst=0; // DEBUG
8578 //current.isconst=0;
8579 //current.wasconst=0;
8580 //regs[i].wasconst=0;
8581 clear_const(¤t,rs1[i]);
8582 clear_const(¤t,rs2[i]);
8583 if((opcode[i]&0x3E)==4) // BEQ/BNE
8585 alloc_cc(¤t,i);
8586 dirty_reg(¤t,CCREG);
8587 if(rs1[i]) alloc_reg(¤t,i,rs1[i]);
8588 if(rs2[i]) alloc_reg(¤t,i,rs2[i]);
8589 if(!((current.is32>>rs1[i])&(current.is32>>rs2[i])&1))
8591 if(rs1[i]) alloc_reg64(¤t,i,rs1[i]);
8592 if(rs2[i]) alloc_reg64(¤t,i,rs2[i]);
8594 if((rs1[i]&&(rs1[i]==rt1[i+1]||rs1[i]==rt2[i+1]))||
8595 (rs2[i]&&(rs2[i]==rt1[i+1]||rs2[i]==rt2[i+1]))) {
8596 // The delay slot overwrites one of our conditions.
8597 // Allocate the branch condition registers instead.
8601 if(rs1[i]) alloc_reg(¤t,i,rs1[i]);
8602 if(rs2[i]) alloc_reg(¤t,i,rs2[i]);
8603 if(!((current.is32>>rs1[i])&(current.is32>>rs2[i])&1))
8605 if(rs1[i]) alloc_reg64(¤t,i,rs1[i]);
8606 if(rs2[i]) alloc_reg64(¤t,i,rs2[i]);
8612 delayslot_alloc(¤t,i+1);
8616 if((opcode[i]&0x3E)==6) // BLEZ/BGTZ
8618 alloc_cc(¤t,i);
8619 dirty_reg(¤t,CCREG);
8620 alloc_reg(¤t,i,rs1[i]);
8621 if(!(current.is32>>rs1[i]&1))
8623 alloc_reg64(¤t,i,rs1[i]);
8625 if(rs1[i]&&(rs1[i]==rt1[i+1]||rs1[i]==rt2[i+1])) {
8626 // The delay slot overwrites one of our conditions.
8627 // Allocate the branch condition registers instead.
8631 if(rs1[i]) alloc_reg(¤t,i,rs1[i]);
8632 if(!((current.is32>>rs1[i])&1))
8634 if(rs1[i]) alloc_reg64(¤t,i,rs1[i]);
8640 delayslot_alloc(¤t,i+1);
8644 // Don't alloc the delay slot yet because we might not execute it
8645 if((opcode[i]&0x3E)==0x14) // BEQL/BNEL
8650 alloc_cc(¤t,i);
8651 dirty_reg(¤t,CCREG);
8652 alloc_reg(¤t,i,rs1[i]);
8653 alloc_reg(¤t,i,rs2[i]);
8654 if(!((current.is32>>rs1[i])&(current.is32>>rs2[i])&1))
8656 alloc_reg64(¤t,i,rs1[i]);
8657 alloc_reg64(¤t,i,rs2[i]);
8661 if((opcode[i]&0x3E)==0x16) // BLEZL/BGTZL
8666 alloc_cc(¤t,i);
8667 dirty_reg(¤t,CCREG);
8668 alloc_reg(¤t,i,rs1[i]);
8669 if(!(current.is32>>rs1[i]&1))
8671 alloc_reg64(¤t,i,rs1[i]);
8675 //current.isconst=0;
8678 //current.isconst=0;
8679 //current.wasconst=0;
8680 //regs[i].wasconst=0;
8681 clear_const(¤t,rs1[i]);
8682 clear_const(¤t,rt1[i]);
8683 //if((opcode2[i]&0x1E)==0x0) // BLTZ/BGEZ
8684 if((opcode2[i]&0x0E)==0x0) // BLTZ/BGEZ
8686 alloc_cc(¤t,i);
8687 dirty_reg(¤t,CCREG);
8688 alloc_reg(¤t,i,rs1[i]);
8689 if(!(current.is32>>rs1[i]&1))
8691 alloc_reg64(¤t,i,rs1[i]);
8693 if (rt1[i]==31) { // BLTZAL/BGEZAL
8694 alloc_reg(¤t,i,31);
8695 dirty_reg(¤t,31);
8696 assert(rs1[i+1]!=31&&rs2[i+1]!=31);
8697 //#ifdef REG_PREFETCH
8698 //alloc_reg(¤t,i,PTEMP);
8700 //current.is32|=1LL<<rt1[i];
8702 if(rs1[i]&&(rs1[i]==rt1[i+1]||rs1[i]==rt2[i+1])) {
8703 // The delay slot overwrites the branch condition.
8704 // Allocate the branch condition registers instead.
8708 if(rs1[i]) alloc_reg(¤t,i,rs1[i]);
8709 if(!((current.is32>>rs1[i])&1))
8711 if(rs1[i]) alloc_reg64(¤t,i,rs1[i]);
8717 delayslot_alloc(¤t,i+1);
8721 // Don't alloc the delay slot yet because we might not execute it
8722 if((opcode2[i]&0x1E)==0x2) // BLTZL/BGEZL
8727 alloc_cc(¤t,i);
8728 dirty_reg(¤t,CCREG);
8729 alloc_reg(¤t,i,rs1[i]);
8730 if(!(current.is32>>rs1[i]&1))
8732 alloc_reg64(¤t,i,rs1[i]);
8736 //current.isconst=0;
8742 if(likely[i]==0) // BC1F/BC1T
8744 // TODO: Theoretically we can run out of registers here on x86.
8745 // The delay slot can allocate up to six, and we need to check
8746 // CSREG before executing the delay slot. Possibly we can drop
8747 // the cycle count and then reload it after checking that the
8748 // FPU is in a usable state, or don't do out-of-order execution.
8749 alloc_cc(¤t,i);
8750 dirty_reg(¤t,CCREG);
8751 alloc_reg(¤t,i,FSREG);
8752 alloc_reg(¤t,i,CSREG);
8753 if(itype[i+1]==FCOMP) {
8754 // The delay slot overwrites the branch condition.
8755 // Allocate the branch condition registers instead.
8756 alloc_cc(¤t,i);
8757 dirty_reg(¤t,CCREG);
8758 alloc_reg(¤t,i,CSREG);
8759 alloc_reg(¤t,i,FSREG);
8763 delayslot_alloc(¤t,i+1);
8764 alloc_reg(¤t,i+1,CSREG);
8768 // Don't alloc the delay slot yet because we might not execute it
8769 if(likely[i]) // BC1FL/BC1TL
8771 alloc_cc(¤t,i);
8772 dirty_reg(¤t,CCREG);
8773 alloc_reg(¤t,i,CSREG);
8774 alloc_reg(¤t,i,FSREG);
8780 imm16_alloc(¤t,i);
8784 load_alloc(¤t,i);
8788 store_alloc(¤t,i);
8791 alu_alloc(¤t,i);
8794 shift_alloc(¤t,i);
8797 multdiv_alloc(¤t,i);
8800 shiftimm_alloc(¤t,i);
8803 mov_alloc(¤t,i);
8806 cop0_alloc(¤t,i);
8809 cop1_alloc(¤t,i);
8812 c1ls_alloc(¤t,i);
8815 fconv_alloc(¤t,i);
8818 float_alloc(¤t,i);
8821 fcomp_alloc(¤t,i);
8824 syscall_alloc(¤t,i);
8827 pagespan_alloc(¤t,i);
8831 // Drop the upper half of registers that have become 32-bit
8832 current.uu|=current.is32&((1LL<<rt1[i])|(1LL<<rt2[i]));
8833 if(itype[i]!=UJUMP&&itype[i]!=CJUMP&&itype[i]!=SJUMP&&itype[i]!=RJUMP&&itype[i]!=FJUMP) {
8834 current.uu&=~((1LL<<us1[i])|(1LL<<us2[i]));
8835 if((~current.uu>>rt1[i])&1) current.uu&=~((1LL<<dep1[i])|(1LL<<dep2[i]));
8838 current.uu|=current.is32&((1LL<<rt1[i+1])|(1LL<<rt2[i+1]));
8839 current.uu&=~((1LL<<us1[i+1])|(1LL<<us2[i+1]));
8840 if((~current.uu>>rt1[i+1])&1) current.uu&=~((1LL<<dep1[i+1])|(1LL<<dep2[i+1]));
8841 current.uu&=~((1LL<<us1[i])|(1LL<<us2[i]));
8845 // Create entry (branch target) regmap
8846 for(hr=0;hr<HOST_REGS;hr++)
8849 r=current.regmap[hr];
8851 if(r!=regmap_pre[i][hr]) {
8852 // TODO: delay slot (?)
8853 or=get_reg(regmap_pre[i],r); // Get old mapping for this register
8854 if(or<0||(r&63)>=TEMPREG){
8855 regs[i].regmap_entry[hr]=-1;
8859 // Just move it to a different register
8860 regs[i].regmap_entry[hr]=r;
8861 // If it was dirty before, it's still dirty
8862 if((regs[i].wasdirty>>or)&1) dirty_reg(¤t,r&63);
8869 regs[i].regmap_entry[hr]=0;
8873 if((current.u>>r)&1) {
8874 regs[i].regmap_entry[hr]=-1;
8875 //regs[i].regmap[hr]=-1;
8876 current.regmap[hr]=-1;
8878 regs[i].regmap_entry[hr]=r;
8881 if((current.uu>>(r&63))&1) {
8882 regs[i].regmap_entry[hr]=-1;
8883 //regs[i].regmap[hr]=-1;
8884 current.regmap[hr]=-1;
8886 regs[i].regmap_entry[hr]=r;
8890 // Branches expect CCREG to be allocated at the target
8891 if(regmap_pre[i][hr]==CCREG)
8892 regs[i].regmap_entry[hr]=CCREG;
8894 regs[i].regmap_entry[hr]=-1;
8897 memcpy(regs[i].regmap,current.regmap,sizeof(current.regmap));
8899 /* Branch post-alloc */
8902 current.was32=current.is32;
8903 current.wasdirty=current.dirty;
8904 switch(itype[i-1]) {
8906 memcpy(&branch_regs[i-1],¤t,sizeof(current));
8907 branch_regs[i-1].isconst=0;
8908 branch_regs[i-1].wasconst=0;
8909 branch_regs[i-1].u=branch_unneeded_reg[i-1]&~((1LL<<rs1[i-1])|(1LL<<rs2[i-1]));
8910 branch_regs[i-1].uu=branch_unneeded_reg_upper[i-1]&~((1LL<<us1[i-1])|(1LL<<us2[i-1]));
8911 alloc_cc(&branch_regs[i-1],i-1);
8912 dirty_reg(&branch_regs[i-1],CCREG);
8913 if(rt1[i-1]==31) { // JAL
8914 alloc_reg(&branch_regs[i-1],i-1,31);
8915 dirty_reg(&branch_regs[i-1],31);
8916 branch_regs[i-1].is32|=1LL<<31;
8918 memcpy(&branch_regs[i-1].regmap_entry,&branch_regs[i-1].regmap,sizeof(current.regmap));
8919 memcpy(constmap[i],constmap[i-1],sizeof(current.constmap));
8922 memcpy(&branch_regs[i-1],¤t,sizeof(current));
8923 branch_regs[i-1].isconst=0;
8924 branch_regs[i-1].wasconst=0;
8925 branch_regs[i-1].u=branch_unneeded_reg[i-1]&~((1LL<<rs1[i-1])|(1LL<<rs2[i-1]));
8926 branch_regs[i-1].uu=branch_unneeded_reg_upper[i-1]&~((1LL<<us1[i-1])|(1LL<<us2[i-1]));
8927 alloc_cc(&branch_regs[i-1],i-1);
8928 dirty_reg(&branch_regs[i-1],CCREG);
8929 alloc_reg(&branch_regs[i-1],i-1,rs1[i-1]);
8930 if(rt1[i-1]!=0) { // JALR
8931 alloc_reg(&branch_regs[i-1],i-1,rt1[i-1]);
8932 dirty_reg(&branch_regs[i-1],rt1[i-1]);
8933 branch_regs[i-1].is32|=1LL<<rt1[i-1];
8936 if(rs1[i-1]==31) { // JALR
8937 alloc_reg(&branch_regs[i-1],i-1,RHASH);
8938 #ifndef HOST_IMM_ADDR32
8939 alloc_reg(&branch_regs[i-1],i-1,RHTBL);
8943 memcpy(&branch_regs[i-1].regmap_entry,&branch_regs[i-1].regmap,sizeof(current.regmap));
8944 memcpy(constmap[i],constmap[i-1],sizeof(current.constmap));
8947 if((opcode[i-1]&0x3E)==4) // BEQ/BNE
8949 alloc_cc(¤t,i-1);
8950 dirty_reg(¤t,CCREG);
8951 if((rs1[i-1]&&(rs1[i-1]==rt1[i]||rs1[i-1]==rt2[i]))||
8952 (rs2[i-1]&&(rs2[i-1]==rt1[i]||rs2[i-1]==rt2[i]))) {
8953 // The delay slot overwrote one of our conditions
8954 // Delay slot goes after the test (in order)
8955 current.u=branch_unneeded_reg[i-1]&~((1LL<<rs1[i])|(1LL<<rs2[i]));
8956 current.uu=branch_unneeded_reg_upper[i-1]&~((1LL<<us1[i])|(1LL<<us2[i]));
8957 if((~current.uu>>rt1[i])&1) current.uu&=~((1LL<<dep1[i])|(1LL<<dep2[i]));
8960 delayslot_alloc(¤t,i);
8965 current.u=branch_unneeded_reg[i-1]&~((1LL<<rs1[i-1])|(1LL<<rs2[i-1]));
8966 current.uu=branch_unneeded_reg_upper[i-1]&~((1LL<<us1[i-1])|(1LL<<us2[i-1]));
8967 // Alloc the branch condition registers
8968 if(rs1[i-1]) alloc_reg(¤t,i-1,rs1[i-1]);
8969 if(rs2[i-1]) alloc_reg(¤t,i-1,rs2[i-1]);
8970 if(!((current.is32>>rs1[i-1])&(current.is32>>rs2[i-1])&1))
8972 if(rs1[i-1]) alloc_reg64(¤t,i-1,rs1[i-1]);
8973 if(rs2[i-1]) alloc_reg64(¤t,i-1,rs2[i-1]);
8976 memcpy(&branch_regs[i-1],¤t,sizeof(current));
8977 branch_regs[i-1].isconst=0;
8978 branch_regs[i-1].wasconst=0;
8979 memcpy(&branch_regs[i-1].regmap_entry,¤t.regmap,sizeof(current.regmap));
8980 memcpy(constmap[i],constmap[i-1],sizeof(current.constmap));
8983 if((opcode[i-1]&0x3E)==6) // BLEZ/BGTZ
8985 alloc_cc(¤t,i-1);
8986 dirty_reg(¤t,CCREG);
8987 if(rs1[i-1]==rt1[i]||rs1[i-1]==rt2[i]) {
8988 // The delay slot overwrote the branch condition
8989 // Delay slot goes after the test (in order)
8990 current.u=branch_unneeded_reg[i-1]&~((1LL<<rs1[i])|(1LL<<rs2[i]));
8991 current.uu=branch_unneeded_reg_upper[i-1]&~((1LL<<us1[i])|(1LL<<us2[i]));
8992 if((~current.uu>>rt1[i])&1) current.uu&=~((1LL<<dep1[i])|(1LL<<dep2[i]));
8995 delayslot_alloc(¤t,i);
9000 current.u=branch_unneeded_reg[i-1]&~(1LL<<rs1[i-1]);
9001 current.uu=branch_unneeded_reg_upper[i-1]&~(1LL<<us1[i-1]);
9002 // Alloc the branch condition register
9003 alloc_reg(¤t,i-1,rs1[i-1]);
9004 if(!(current.is32>>rs1[i-1]&1))
9006 alloc_reg64(¤t,i-1,rs1[i-1]);
9009 memcpy(&branch_regs[i-1],¤t,sizeof(current));
9010 branch_regs[i-1].isconst=0;
9011 branch_regs[i-1].wasconst=0;
9012 memcpy(&branch_regs[i-1].regmap_entry,¤t.regmap,sizeof(current.regmap));
9013 memcpy(constmap[i],constmap[i-1],sizeof(current.constmap));
9016 // Alloc the delay slot in case the branch is taken
9017 if((opcode[i-1]&0x3E)==0x14) // BEQL/BNEL
9019 memcpy(&branch_regs[i-1],¤t,sizeof(current));
9020 branch_regs[i-1].u=(branch_unneeded_reg[i-1]&~((1LL<<rs1[i])|(1LL<<rs2[i])|(1LL<<rt1[i])|(1LL<<rt2[i])))|1;
9021 branch_regs[i-1].uu=(branch_unneeded_reg_upper[i-1]&~((1LL<<us1[i])|(1LL<<us2[i])|(1LL<<rt1[i])|(1LL<<rt2[i])))|1;
9022 if((~branch_regs[i-1].uu>>rt1[i])&1) branch_regs[i-1].uu&=~((1LL<<dep1[i])|(1LL<<dep2[i]))|1;
9023 alloc_cc(&branch_regs[i-1],i);
9024 dirty_reg(&branch_regs[i-1],CCREG);
9025 delayslot_alloc(&branch_regs[i-1],i);
9026 branch_regs[i-1].isconst=0;
9027 alloc_reg(¤t,i,CCREG); // Not taken path
9028 dirty_reg(¤t,CCREG);
9029 memcpy(&branch_regs[i-1].regmap_entry,&branch_regs[i-1].regmap,sizeof(current.regmap));
9032 if((opcode[i-1]&0x3E)==0x16) // BLEZL/BGTZL
9034 memcpy(&branch_regs[i-1],¤t,sizeof(current));
9035 branch_regs[i-1].u=(branch_unneeded_reg[i-1]&~((1LL<<rs1[i])|(1LL<<rs2[i])|(1LL<<rt1[i])|(1LL<<rt2[i])))|1;
9036 branch_regs[i-1].uu=(branch_unneeded_reg_upper[i-1]&~((1LL<<us1[i])|(1LL<<us2[i])|(1LL<<rt1[i])|(1LL<<rt2[i])))|1;
9037 if((~branch_regs[i-1].uu>>rt1[i])&1) branch_regs[i-1].uu&=~((1LL<<dep1[i])|(1LL<<dep2[i]))|1;
9038 alloc_cc(&branch_regs[i-1],i);
9039 dirty_reg(&branch_regs[i-1],CCREG);
9040 delayslot_alloc(&branch_regs[i-1],i);
9041 branch_regs[i-1].isconst=0;
9042 alloc_reg(¤t,i,CCREG); // Not taken path
9043 dirty_reg(¤t,CCREG);
9044 memcpy(&branch_regs[i-1].regmap_entry,&branch_regs[i-1].regmap,sizeof(current.regmap));
9048 //if((opcode2[i-1]&0x1E)==0) // BLTZ/BGEZ
9049 if((opcode2[i-1]&0x0E)==0) // BLTZ/BGEZ
9051 alloc_cc(¤t,i-1);
9052 dirty_reg(¤t,CCREG);
9053 if(rs1[i-1]==rt1[i]||rs1[i-1]==rt2[i]) {
9054 // The delay slot overwrote the branch condition
9055 // Delay slot goes after the test (in order)
9056 current.u=branch_unneeded_reg[i-1]&~((1LL<<rs1[i])|(1LL<<rs2[i]));
9057 current.uu=branch_unneeded_reg_upper[i-1]&~((1LL<<us1[i])|(1LL<<us2[i]));
9058 if((~current.uu>>rt1[i])&1) current.uu&=~((1LL<<dep1[i])|(1LL<<dep2[i]));
9061 delayslot_alloc(¤t,i);
9066 current.u=branch_unneeded_reg[i-1]&~(1LL<<rs1[i-1]);
9067 current.uu=branch_unneeded_reg_upper[i-1]&~(1LL<<us1[i-1]);
9068 // Alloc the branch condition register
9069 alloc_reg(¤t,i-1,rs1[i-1]);
9070 if(!(current.is32>>rs1[i-1]&1))
9072 alloc_reg64(¤t,i-1,rs1[i-1]);
9075 memcpy(&branch_regs[i-1],¤t,sizeof(current));
9076 branch_regs[i-1].isconst=0;
9077 branch_regs[i-1].wasconst=0;
9078 memcpy(&branch_regs[i-1].regmap_entry,¤t.regmap,sizeof(current.regmap));
9079 memcpy(constmap[i],constmap[i-1],sizeof(current.constmap));
9082 // Alloc the delay slot in case the branch is taken
9083 if((opcode2[i-1]&0x1E)==2) // BLTZL/BGEZL
9085 memcpy(&branch_regs[i-1],¤t,sizeof(current));
9086 branch_regs[i-1].u=(branch_unneeded_reg[i-1]&~((1LL<<rs1[i])|(1LL<<rs2[i])|(1LL<<rt1[i])|(1LL<<rt2[i])))|1;
9087 branch_regs[i-1].uu=(branch_unneeded_reg_upper[i-1]&~((1LL<<us1[i])|(1LL<<us2[i])|(1LL<<rt1[i])|(1LL<<rt2[i])))|1;
9088 if((~branch_regs[i-1].uu>>rt1[i])&1) branch_regs[i-1].uu&=~((1LL<<dep1[i])|(1LL<<dep2[i]))|1;
9089 alloc_cc(&branch_regs[i-1],i);
9090 dirty_reg(&branch_regs[i-1],CCREG);
9091 delayslot_alloc(&branch_regs[i-1],i);
9092 branch_regs[i-1].isconst=0;
9093 alloc_reg(¤t,i,CCREG); // Not taken path
9094 dirty_reg(¤t,CCREG);
9095 memcpy(&branch_regs[i-1].regmap_entry,&branch_regs[i-1].regmap,sizeof(current.regmap));
9097 // FIXME: BLTZAL/BGEZAL
9098 if(opcode2[i-1]&0x10) { // BxxZAL
9099 alloc_reg(&branch_regs[i-1],i-1,31);
9100 dirty_reg(&branch_regs[i-1],31);
9101 branch_regs[i-1].is32|=1LL<<31;
9105 if(likely[i-1]==0) // BC1F/BC1T
9107 alloc_cc(¤t,i-1);
9108 dirty_reg(¤t,CCREG);
9109 if(itype[i]==FCOMP) {
9110 // The delay slot overwrote the branch condition
9111 // Delay slot goes after the test (in order)
9112 delayslot_alloc(¤t,i);
9117 current.u=branch_unneeded_reg[i-1]&~(1LL<<rs1[i-1]);
9118 current.uu=branch_unneeded_reg_upper[i-1]&~(1LL<<us1[i-1]);
9119 // Alloc the branch condition register
9120 alloc_reg(¤t,i-1,FSREG);
9122 memcpy(&branch_regs[i-1],¤t,sizeof(current));
9123 memcpy(&branch_regs[i-1].regmap_entry,¤t.regmap,sizeof(current.regmap));
9127 // Alloc the delay slot in case the branch is taken
9128 memcpy(&branch_regs[i-1],¤t,sizeof(current));
9129 branch_regs[i-1].u=(branch_unneeded_reg[i-1]&~((1LL<<rs1[i])|(1LL<<rs2[i])|(1LL<<rt1[i])|(1LL<<rt2[i])))|1;
9130 branch_regs[i-1].uu=(branch_unneeded_reg_upper[i-1]&~((1LL<<us1[i])|(1LL<<us2[i])|(1LL<<rt1[i])|(1LL<<rt2[i])))|1;
9131 if((~branch_regs[i-1].uu>>rt1[i])&1) branch_regs[i-1].uu&=~((1LL<<dep1[i])|(1LL<<dep2[i]))|1;
9132 alloc_cc(&branch_regs[i-1],i);
9133 dirty_reg(&branch_regs[i-1],CCREG);
9134 delayslot_alloc(&branch_regs[i-1],i);
9135 branch_regs[i-1].isconst=0;
9136 alloc_reg(¤t,i,CCREG); // Not taken path
9137 dirty_reg(¤t,CCREG);
9138 memcpy(&branch_regs[i-1].regmap_entry,&branch_regs[i-1].regmap,sizeof(current.regmap));
9143 if(itype[i-1]==UJUMP||itype[i-1]==RJUMP||(source[i-1]>>16)==0x1000)
9145 if(rt1[i-1]==31) // JAL/JALR
9147 // Subroutine call will return here, don't alloc any registers
9150 clear_all_regs(current.regmap);
9151 alloc_reg(¤t,i,CCREG);
9152 dirty_reg(¤t,CCREG);
9156 // Internal branch will jump here, match registers to caller
9157 current.is32=0x3FFFFFFFFLL;
9159 clear_all_regs(current.regmap);
9160 alloc_reg(¤t,i,CCREG);
9161 dirty_reg(¤t,CCREG);
9164 if(ba[j]==start+i*4+4) {
9165 memcpy(current.regmap,branch_regs[j].regmap,sizeof(current.regmap));
9166 current.is32=branch_regs[j].is32;
9167 current.dirty=branch_regs[j].dirty;
9172 if(ba[j]==start+i*4+4) {
9173 for(hr=0;hr<HOST_REGS;hr++) {
9174 if(current.regmap[hr]!=branch_regs[j].regmap[hr]) {
9175 current.regmap[hr]=-1;
9177 current.is32&=branch_regs[j].is32;
9178 current.dirty&=branch_regs[j].dirty;
9187 // Count cycles in between branches
9189 if(i>0&&(itype[i-1]==RJUMP||itype[i-1]==UJUMP||itype[i-1]==CJUMP||itype[i-1]==SJUMP||itype[i-1]==FJUMP||itype[i]==SYSCALL))
9198 flush_dirty_uppers(¤t);
9200 regs[i].is32=current.is32;
9201 regs[i].dirty=current.dirty;
9202 regs[i].isconst=current.isconst;
9203 memcpy(constmap[i],current.constmap,sizeof(current.constmap));
9205 for(hr=0;hr<HOST_REGS;hr++) {
9206 if(hr!=EXCLUDE_REG&®s[i].regmap[hr]>=0) {
9207 if(regmap_pre[i][hr]!=regs[i].regmap[hr]) {
9208 regs[i].wasconst&=~(1<<hr);
9212 if(current.regmap[HOST_BTREG]==BTREG) current.regmap[HOST_BTREG]=-1;
9215 /* Pass 4 - Cull unused host registers */
9219 for (i=slen-1;i>=0;i--)
9222 if(itype[i]==RJUMP||itype[i]==UJUMP||itype[i]==CJUMP||itype[i]==SJUMP||itype[i]==FJUMP)
9224 if(ba[i]<start || ba[i]>=(start+slen*4))
9226 // Branch out of this block, don't need anything
9232 // Need whatever matches the target
9234 int t=(ba[i]-start)>>2;
9235 for(hr=0;hr<HOST_REGS;hr++)
9237 if(regs[i].regmap_entry[hr]>=0) {
9238 if(regs[i].regmap_entry[hr]==regs[t].regmap_entry[hr]) nr|=1<<hr;
9242 // Conditional branch may need registers for following instructions
9243 if(itype[i]!=RJUMP&&itype[i]!=UJUMP&&(source[i]>>16)!=0x1000)
9246 nr|=needed_reg[i+2];
9247 for(hr=0;hr<HOST_REGS;hr++)
9249 if(regmap_pre[i+2][hr]>=0&&get_reg(regs[i+2].regmap_entry,regmap_pre[i+2][hr])<0) nr&=~(1<<hr);
9250 //if((regmap_entry[i+2][hr])>=0) if(!((nr>>hr)&1)) DebugMessage(M64MSG_VERBOSE, "%x-bogus(%d=%d)",start+i*4,hr,regmap_entry[i+2][hr]);
9254 // Don't need stuff which is overwritten
9255 if(regs[i].regmap[hr]!=regmap_pre[i][hr]) nr&=~(1<<hr);
9256 if(regs[i].regmap[hr]<0) nr&=~(1<<hr);
9257 // Merge in delay slot
9258 for(hr=0;hr<HOST_REGS;hr++)
9261 // These are overwritten unless the branch is "likely"
9262 // and the delay slot is nullified if not taken
9263 if(rt1[i+1]&&rt1[i+1]==(regs[i].regmap[hr]&63)) nr&=~(1<<hr);
9264 if(rt2[i+1]&&rt2[i+1]==(regs[i].regmap[hr]&63)) nr&=~(1<<hr);
9266 if(us1[i+1]==(regmap_pre[i][hr]&63)) nr|=1<<hr;
9267 if(us2[i+1]==(regmap_pre[i][hr]&63)) nr|=1<<hr;
9268 if(rs1[i+1]==regmap_pre[i][hr]) nr|=1<<hr;
9269 if(rs2[i+1]==regmap_pre[i][hr]) nr|=1<<hr;
9270 if(us1[i+1]==(regs[i].regmap_entry[hr]&63)) nr|=1<<hr;
9271 if(us2[i+1]==(regs[i].regmap_entry[hr]&63)) nr|=1<<hr;
9272 if(rs1[i+1]==regs[i].regmap_entry[hr]) nr|=1<<hr;
9273 if(rs2[i+1]==regs[i].regmap_entry[hr]) nr|=1<<hr;
9274 if(dep1[i+1]&&!((unneeded_reg_upper[i]>>dep1[i+1])&1)) {
9275 if(dep1[i+1]==(regmap_pre[i][hr]&63)) nr|=1<<hr;
9276 if(dep2[i+1]==(regmap_pre[i][hr]&63)) nr|=1<<hr;
9278 if(dep2[i+1]&&!((unneeded_reg_upper[i]>>dep2[i+1])&1)) {
9279 if(dep1[i+1]==(regs[i].regmap_entry[hr]&63)) nr|=1<<hr;
9280 if(dep2[i+1]==(regs[i].regmap_entry[hr]&63)) nr|=1<<hr;
9282 if(itype[i+1]==STORE || itype[i+1]==STORELR || (opcode[i+1]&0x3b)==0x39) {
9283 if(regmap_pre[i][hr]==INVCP) nr|=1<<hr;
9284 if(regs[i].regmap_entry[hr]==INVCP) nr|=1<<hr;
9288 else if(itype[i]==SYSCALL)
9290 // SYSCALL instruction (software interrupt)
9293 else if(itype[i]==COP0 && (source[i]&0x3f)==0x18)
9295 // ERET instruction (return from interrupt)
9301 for(hr=0;hr<HOST_REGS;hr++) {
9302 if(regmap_pre[i+1][hr]>=0&&get_reg(regs[i+1].regmap_entry,regmap_pre[i+1][hr])<0) nr&=~(1<<hr);
9303 if(regs[i].regmap[hr]!=regmap_pre[i+1][hr]) nr&=~(1<<hr);
9304 if(regs[i].regmap[hr]!=regmap_pre[i][hr]) nr&=~(1<<hr);
9305 if(regs[i].regmap[hr]<0) nr&=~(1<<hr);
9309 for(hr=0;hr<HOST_REGS;hr++)
9311 // Overwritten registers are not needed
9312 if(rt1[i]&&rt1[i]==(regs[i].regmap[hr]&63)) nr&=~(1<<hr);
9313 if(rt2[i]&&rt2[i]==(regs[i].regmap[hr]&63)) nr&=~(1<<hr);
9314 if(FTEMP==(regs[i].regmap[hr]&63)) nr&=~(1<<hr);
9315 // Source registers are needed
9316 if(us1[i]==(regmap_pre[i][hr]&63)) nr|=1<<hr;
9317 if(us2[i]==(regmap_pre[i][hr]&63)) nr|=1<<hr;
9318 if(rs1[i]==regmap_pre[i][hr]) nr|=1<<hr;
9319 if(rs2[i]==regmap_pre[i][hr]) nr|=1<<hr;
9320 if(us1[i]==(regs[i].regmap_entry[hr]&63)) nr|=1<<hr;
9321 if(us2[i]==(regs[i].regmap_entry[hr]&63)) nr|=1<<hr;
9322 if(rs1[i]==regs[i].regmap_entry[hr]) nr|=1<<hr;
9323 if(rs2[i]==regs[i].regmap_entry[hr]) nr|=1<<hr;
9324 if(dep1[i]&&!((unneeded_reg_upper[i]>>dep1[i])&1)) {
9325 if(dep1[i]==(regmap_pre[i][hr]&63)) nr|=1<<hr;
9326 if(dep1[i]==(regs[i].regmap_entry[hr]&63)) nr|=1<<hr;
9328 if(dep2[i]&&!((unneeded_reg_upper[i]>>dep2[i])&1)) {
9329 if(dep2[i]==(regmap_pre[i][hr]&63)) nr|=1<<hr;
9330 if(dep2[i]==(regs[i].regmap_entry[hr]&63)) nr|=1<<hr;
9332 if(itype[i]==STORE || itype[i]==STORELR || (opcode[i]&0x3b)==0x39) {
9333 if(regmap_pre[i][hr]==INVCP) nr|=1<<hr;
9334 if(regs[i].regmap_entry[hr]==INVCP) nr|=1<<hr;
9336 // Don't store a register immediately after writing it,
9337 // may prevent dual-issue.
9338 // But do so if this is a branch target, otherwise we
9339 // might have to load the register before the branch.
9340 if(i>0&&!bt[i]&&((regs[i].wasdirty>>hr)&1)) {
9341 if((regmap_pre[i][hr]>0&®map_pre[i][hr]<64&&!((unneeded_reg[i]>>regmap_pre[i][hr])&1)) ||
9342 (regmap_pre[i][hr]>64&&!((unneeded_reg_upper[i]>>(regmap_pre[i][hr]&63))&1)) ) {
9343 if(rt1[i-1]==(regmap_pre[i][hr]&63)) nr|=1<<hr;
9344 if(rt2[i-1]==(regmap_pre[i][hr]&63)) nr|=1<<hr;
9346 if((regs[i].regmap_entry[hr]>0&®s[i].regmap_entry[hr]<64&&!((unneeded_reg[i]>>regs[i].regmap_entry[hr])&1)) ||
9347 (regs[i].regmap_entry[hr]>64&&!((unneeded_reg_upper[i]>>(regs[i].regmap_entry[hr]&63))&1)) ) {
9348 if(rt1[i-1]==(regs[i].regmap_entry[hr]&63)) nr|=1<<hr;
9349 if(rt2[i-1]==(regs[i].regmap_entry[hr]&63)) nr|=1<<hr;
9353 // Cycle count is needed at branches. Assume it is needed at the target too.
9354 if(i==0||bt[i]||itype[i]==CJUMP||itype[i]==FJUMP||itype[i]==SPAN) {
9355 if(regmap_pre[i][HOST_CCREG]==CCREG) nr|=1<<HOST_CCREG;
9356 if(regs[i].regmap_entry[HOST_CCREG]==CCREG) nr|=1<<HOST_CCREG;
9361 // Deallocate unneeded registers
9362 for(hr=0;hr<HOST_REGS;hr++)
9365 if(regs[i].regmap_entry[hr]!=CCREG) regs[i].regmap_entry[hr]=-1;
9366 if((regs[i].regmap[hr]&63)!=rs1[i] && (regs[i].regmap[hr]&63)!=rs2[i] &&
9367 (regs[i].regmap[hr]&63)!=rt1[i] && (regs[i].regmap[hr]&63)!=rt2[i] &&
9368 (regs[i].regmap[hr]&63)!=PTEMP && (regs[i].regmap[hr]&63)!=CCREG)
9370 if(itype[i]!=RJUMP&&itype[i]!=UJUMP&&(source[i]>>16)!=0x1000)
9373 regs[i].regmap[hr]=-1;
9374 regs[i].isconst&=~(1<<hr);
9376 regmap_pre[i+2][hr]=-1;
9377 regs[i+2].wasconst&=~(1<<hr);
9382 if(itype[i]==RJUMP||itype[i]==UJUMP||itype[i]==CJUMP||itype[i]==SJUMP||itype[i]==FJUMP)
9384 int d1=0,d2=0,map=0,temp=0;
9385 if(get_reg(regs[i].regmap,rt1[i+1]|64)>=0||get_reg(branch_regs[i].regmap,rt1[i+1]|64)>=0)
9391 if(itype[i+1]==LOAD || itype[i+1]==LOADLR ||
9392 itype[i+1]==STORE || itype[i+1]==STORELR ||
9396 if(itype[i+1]==STORE || itype[i+1]==STORELR || (opcode[i+1]&0x3b)==0x39) {
9399 if(itype[i+1]==LOADLR || itype[i+1]==STORELR ||
9402 if((regs[i].regmap[hr]&63)!=rs1[i] && (regs[i].regmap[hr]&63)!=rs2[i] &&
9403 (regs[i].regmap[hr]&63)!=rt1[i] && (regs[i].regmap[hr]&63)!=rt2[i] &&
9404 (regs[i].regmap[hr]&63)!=rt1[i+1] && (regs[i].regmap[hr]&63)!=rt2[i+1] &&
9405 (regs[i].regmap[hr]^64)!=us1[i+1] && (regs[i].regmap[hr]^64)!=us2[i+1] &&
9406 (regs[i].regmap[hr]^64)!=d1 && (regs[i].regmap[hr]^64)!=d2 &&
9407 regs[i].regmap[hr]!=rs1[i+1] && regs[i].regmap[hr]!=rs2[i+1] &&
9408 (regs[i].regmap[hr]&63)!=temp && regs[i].regmap[hr]!=PTEMP &&
9409 regs[i].regmap[hr]!=RHASH && regs[i].regmap[hr]!=RHTBL &&
9410 regs[i].regmap[hr]!=RTEMP && regs[i].regmap[hr]!=CCREG &&
9411 regs[i].regmap[hr]!=map )
9413 regs[i].regmap[hr]=-1;
9414 regs[i].isconst&=~(1<<hr);
9415 if((branch_regs[i].regmap[hr]&63)!=rs1[i] && (branch_regs[i].regmap[hr]&63)!=rs2[i] &&
9416 (branch_regs[i].regmap[hr]&63)!=rt1[i] && (branch_regs[i].regmap[hr]&63)!=rt2[i] &&
9417 (branch_regs[i].regmap[hr]&63)!=rt1[i+1] && (branch_regs[i].regmap[hr]&63)!=rt2[i+1] &&
9418 (branch_regs[i].regmap[hr]^64)!=us1[i+1] && (branch_regs[i].regmap[hr]^64)!=us2[i+1] &&
9419 (branch_regs[i].regmap[hr]^64)!=d1 && (branch_regs[i].regmap[hr]^64)!=d2 &&
9420 branch_regs[i].regmap[hr]!=rs1[i+1] && branch_regs[i].regmap[hr]!=rs2[i+1] &&
9421 (branch_regs[i].regmap[hr]&63)!=temp && branch_regs[i].regmap[hr]!=PTEMP &&
9422 branch_regs[i].regmap[hr]!=RHASH && branch_regs[i].regmap[hr]!=RHTBL &&
9423 branch_regs[i].regmap[hr]!=RTEMP && branch_regs[i].regmap[hr]!=CCREG &&
9424 branch_regs[i].regmap[hr]!=map)
9426 branch_regs[i].regmap[hr]=-1;
9427 branch_regs[i].regmap_entry[hr]=-1;
9428 if(itype[i]!=RJUMP&&itype[i]!=UJUMP&&(source[i]>>16)!=0x1000)
9430 if(!likely[i]&&i<slen-2) {
9431 regmap_pre[i+2][hr]=-1;
9432 regs[i+2].wasconst&=~(1<<hr);
9443 int d1=0,d2=0,map=-1,temp=-1;
9444 if(get_reg(regs[i].regmap,rt1[i]|64)>=0)
9450 if(itype[i]==LOAD || itype[i]==LOADLR ||
9451 itype[i]==STORE || itype[i]==STORELR ||
9454 } else if(itype[i]==STORE || itype[i]==STORELR || (opcode[i]&0x3b)==0x39) {
9457 if(itype[i]==LOADLR || itype[i]==STORELR ||
9460 if((regs[i].regmap[hr]&63)!=rt1[i] && (regs[i].regmap[hr]&63)!=rt2[i] &&
9461 (regs[i].regmap[hr]^64)!=us1[i] && (regs[i].regmap[hr]^64)!=us2[i] &&
9462 (regs[i].regmap[hr]^64)!=d1 && (regs[i].regmap[hr]^64)!=d2 &&
9463 regs[i].regmap[hr]!=rs1[i] && regs[i].regmap[hr]!=rs2[i] &&
9464 (regs[i].regmap[hr]&63)!=temp && regs[i].regmap[hr]!=map &&
9465 (itype[i]!=SPAN||regs[i].regmap[hr]!=CCREG))
9467 if(i<slen-1&&!is_ds[i]) {
9468 if(regmap_pre[i+1][hr]!=-1 || regs[i].regmap[hr]!=-1)
9469 if(regmap_pre[i+1][hr]!=regs[i].regmap[hr])
9470 if(regs[i].regmap[hr]<64||!((regs[i].was32>>(regs[i].regmap[hr]&63))&1))
9472 DebugMessage(M64MSG_VERBOSE, "fail: %x (%d %d!=%d)",start+i*4,hr,regmap_pre[i+1][hr],regs[i].regmap[hr]);
9473 assert(regmap_pre[i+1][hr]==regs[i].regmap[hr]);
9475 regmap_pre[i+1][hr]=-1;
9476 if(regs[i+1].regmap_entry[hr]==CCREG) regs[i+1].regmap_entry[hr]=-1;
9477 regs[i+1].wasconst&=~(1<<hr);
9479 regs[i].regmap[hr]=-1;
9480 regs[i].isconst&=~(1<<hr);
9488 /* Pass 5 - Pre-allocate registers */
9490 // If a register is allocated during a loop, try to allocate it for the
9491 // entire loop, if possible. This avoids loading/storing registers
9492 // inside of the loop.
9494 signed char f_regmap[HOST_REGS];
9495 clear_all_regs(f_regmap);
9496 for(i=0;i<slen-1;i++)
9498 if(itype[i]==UJUMP||itype[i]==CJUMP||itype[i]==SJUMP||itype[i]==FJUMP)
9500 if(ba[i]>=start && ba[i]<(start+i*4))
9501 if(itype[i+1]==NOP||itype[i+1]==MOV||itype[i+1]==ALU
9502 ||itype[i+1]==SHIFTIMM||itype[i+1]==IMM16||itype[i+1]==LOAD
9503 ||itype[i+1]==STORE||itype[i+1]==STORELR||itype[i+1]==C1LS
9504 ||itype[i+1]==SHIFT||itype[i+1]==COP1||itype[i+1]==FLOAT
9505 ||itype[i+1]==FCOMP||itype[i+1]==FCONV)
9507 int t=(ba[i]-start)>>2;
9508 if(t>0&&(itype[t-1]!=UJUMP&&itype[t-1]!=RJUMP&&itype[t-1]!=CJUMP&&itype[t-1]!=SJUMP&&itype[t-1]!=FJUMP)) // loop_preload can't handle jumps into delay slots
9509 if(t<2||(itype[t-2]!=UJUMP&&itype[t-2]!=RJUMP)||rt1[t-2]!=31) // call/ret assumes no registers allocated
9510 for(hr=0;hr<HOST_REGS;hr++)
9512 if(regs[i].regmap[hr]>64) {
9513 if(!((regs[i].dirty>>hr)&1))
9514 f_regmap[hr]=regs[i].regmap[hr];
9515 else f_regmap[hr]=-1;
9517 else if(regs[i].regmap[hr]>=0) {
9518 if(f_regmap[hr]!=regs[i].regmap[hr]) {
9519 // dealloc old register
9521 for(n=0;n<HOST_REGS;n++)
9523 if(f_regmap[n]==regs[i].regmap[hr]) {f_regmap[n]=-1;}
9525 // and alloc new one
9526 f_regmap[hr]=regs[i].regmap[hr];
9529 if(branch_regs[i].regmap[hr]>64) {
9530 if(!((branch_regs[i].dirty>>hr)&1))
9531 f_regmap[hr]=branch_regs[i].regmap[hr];
9532 else f_regmap[hr]=-1;
9534 else if(branch_regs[i].regmap[hr]>=0) {
9535 if(f_regmap[hr]!=branch_regs[i].regmap[hr]) {
9536 // dealloc old register
9538 for(n=0;n<HOST_REGS;n++)
9540 if(f_regmap[n]==branch_regs[i].regmap[hr]) {f_regmap[n]=-1;}
9542 // and alloc new one
9543 f_regmap[hr]=branch_regs[i].regmap[hr];
9547 if(count_free_regs(regs[i].regmap)<=minimum_free_regs[i+1])
9548 f_regmap[hr]=branch_regs[i].regmap[hr];
9550 if(count_free_regs(branch_regs[i].regmap)<=minimum_free_regs[i+1])
9551 f_regmap[hr]=branch_regs[i].regmap[hr];
9553 // Avoid dirty->clean transition
9554 #ifdef DESTRUCTIVE_WRITEBACK
9555 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;
9557 // This check is only strictly required in the DESTRUCTIVE_WRITEBACK
9558 // case above, however it's always a good idea. We can't hoist the
9559 // load if the register was already allocated, so there's no point
9560 // wasting time analyzing most of these cases. It only "succeeds"
9561 // when the mapping was different and the load can be replaced with
9562 // a mov, which is of negligible benefit. So such cases are
9564 if(f_regmap[hr]>0) {
9565 if(regs[t].regmap[hr]==f_regmap[hr]||(regs[t].regmap_entry[hr]<0&&get_reg(regmap_pre[t],f_regmap[hr])<0)) {
9569 //DebugMessage(M64MSG_VERBOSE, "Test %x -> %x, %x %d/%d",start+i*4,ba[i],start+j*4,hr,r);
9570 if(r<34&&((unneeded_reg[j]>>r)&1)) break;
9571 if(r>63&&((unneeded_reg_upper[j]>>(r&63))&1)) break;
9573 // NB This can exclude the case where the upper-half
9574 // register is lower numbered than the lower-half
9575 // register. Not sure if it's worth fixing...
9576 if(get_reg(regs[j].regmap,r&63)<0) break;
9577 if(get_reg(regs[j].regmap_entry,r&63)<0) break;
9578 if(regs[j].is32&(1LL<<(r&63))) break;
9580 if(regs[j].regmap[hr]==f_regmap[hr]&&(f_regmap[hr]&63)<TEMPREG) {
9581 //DebugMessage(M64MSG_VERBOSE, "Hit %x -> %x, %x %d/%d",start+i*4,ba[i],start+j*4,hr,r);
9583 if(regs[i].regmap[hr]==-1&&branch_regs[i].regmap[hr]==-1) {
9584 if(get_reg(regs[i+2].regmap,f_regmap[hr])>=0) break;
9586 if(get_reg(regs[i].regmap,r&63)<0) break;
9587 if(get_reg(branch_regs[i].regmap,r&63)<0) break;
9590 while(k>1&®s[k-1].regmap[hr]==-1) {
9591 if(count_free_regs(regs[k-1].regmap)<=minimum_free_regs[k-1]) {
9592 //DebugMessage(M64MSG_VERBOSE, "no free regs for store %x",start+(k-1)*4);
9595 if(get_reg(regs[k-1].regmap,f_regmap[hr])>=0) {
9596 //DebugMessage(M64MSG_VERBOSE, "no-match due to different register");
9599 if(itype[k-2]==UJUMP||itype[k-2]==RJUMP||itype[k-2]==CJUMP||itype[k-2]==SJUMP||itype[k-2]==FJUMP) {
9600 //DebugMessage(M64MSG_VERBOSE, "no-match due to branch");
9603 // call/ret fast path assumes no registers allocated
9604 if(k>2&&(itype[k-3]==UJUMP||itype[k-3]==RJUMP)&&rt1[k-3]==31) {
9608 // NB This can exclude the case where the upper-half
9609 // register is lower numbered than the lower-half
9610 // register. Not sure if it's worth fixing...
9611 if(get_reg(regs[k-1].regmap,r&63)<0) break;
9612 if(regs[k-1].is32&(1LL<<(r&63))) break;
9617 if((regs[k].is32&(1LL<<f_regmap[hr]))!=
9618 (regs[i+2].was32&(1LL<<f_regmap[hr]))) {
9619 //DebugMessage(M64MSG_VERBOSE, "bad match after branch");
9623 if(regs[k-1].regmap[hr]==f_regmap[hr]&®map_pre[k][hr]==f_regmap[hr]) {
9624 //DebugMessage(M64MSG_VERBOSE, "Extend r%d, %x ->",hr,start+k*4);
9626 regs[k].regmap_entry[hr]=f_regmap[hr];
9627 regs[k].regmap[hr]=f_regmap[hr];
9628 regmap_pre[k+1][hr]=f_regmap[hr];
9629 regs[k].wasdirty&=~(1<<hr);
9630 regs[k].dirty&=~(1<<hr);
9631 regs[k].wasdirty|=(1<<hr)®s[k-1].dirty;
9632 regs[k].dirty|=(1<<hr)®s[k].wasdirty;
9633 regs[k].wasconst&=~(1<<hr);
9634 regs[k].isconst&=~(1<<hr);
9639 //DebugMessage(M64MSG_VERBOSE, "Fail Extend r%d, %x ->",hr,start+k*4);
9642 assert(regs[i-1].regmap[hr]==f_regmap[hr]);
9643 if(regs[i-1].regmap[hr]==f_regmap[hr]&®map_pre[i][hr]==f_regmap[hr]) {
9644 //DebugMessage(M64MSG_VERBOSE, "OK fill %x (r%d)",start+i*4,hr);
9645 regs[i].regmap_entry[hr]=f_regmap[hr];
9646 regs[i].regmap[hr]=f_regmap[hr];
9647 regs[i].wasdirty&=~(1<<hr);
9648 regs[i].dirty&=~(1<<hr);
9649 regs[i].wasdirty|=(1<<hr)®s[i-1].dirty;
9650 regs[i].dirty|=(1<<hr)®s[i-1].dirty;
9651 regs[i].wasconst&=~(1<<hr);
9652 regs[i].isconst&=~(1<<hr);
9653 branch_regs[i].regmap_entry[hr]=f_regmap[hr];
9654 branch_regs[i].wasdirty&=~(1<<hr);
9655 branch_regs[i].wasdirty|=(1<<hr)®s[i].dirty;
9656 branch_regs[i].regmap[hr]=f_regmap[hr];
9657 branch_regs[i].dirty&=~(1<<hr);
9658 branch_regs[i].dirty|=(1<<hr)®s[i].dirty;
9659 branch_regs[i].wasconst&=~(1<<hr);
9660 branch_regs[i].isconst&=~(1<<hr);
9661 if(itype[i]!=RJUMP&&itype[i]!=UJUMP&&(source[i]>>16)!=0x1000) {
9662 regmap_pre[i+2][hr]=f_regmap[hr];
9663 regs[i+2].wasdirty&=~(1<<hr);
9664 regs[i+2].wasdirty|=(1<<hr)®s[i].dirty;
9665 assert((branch_regs[i].is32&(1LL<<f_regmap[hr]))==
9666 (regs[i+2].was32&(1LL<<f_regmap[hr])));
9671 // Alloc register clean at beginning of loop,
9672 // but may dirty it in pass 6
9673 regs[k].regmap_entry[hr]=f_regmap[hr];
9674 regs[k].regmap[hr]=f_regmap[hr];
9675 regs[k].dirty&=~(1<<hr);
9676 regs[k].wasconst&=~(1<<hr);
9677 regs[k].isconst&=~(1<<hr);
9678 if(itype[k]==UJUMP||itype[k]==RJUMP||itype[k]==CJUMP||itype[k]==SJUMP||itype[k]==FJUMP) {
9679 branch_regs[k].regmap_entry[hr]=f_regmap[hr];
9680 branch_regs[k].regmap[hr]=f_regmap[hr];
9681 branch_regs[k].dirty&=~(1<<hr);
9682 branch_regs[k].wasconst&=~(1<<hr);
9683 branch_regs[k].isconst&=~(1<<hr);
9684 if(itype[k]!=RJUMP&&itype[k]!=UJUMP&&(source[k]>>16)!=0x1000) {
9685 regmap_pre[k+2][hr]=f_regmap[hr];
9686 regs[k+2].wasdirty&=~(1<<hr);
9687 assert((branch_regs[k].is32&(1LL<<f_regmap[hr]))==
9688 (regs[k+2].was32&(1LL<<f_regmap[hr])));
9693 regmap_pre[k+1][hr]=f_regmap[hr];
9694 regs[k+1].wasdirty&=~(1<<hr);
9697 if(regs[j].regmap[hr]==f_regmap[hr])
9698 regs[j].regmap_entry[hr]=f_regmap[hr];
9702 if(regs[j].regmap[hr]>=0)
9704 if(get_reg(regs[j].regmap,f_regmap[hr])>=0) {
9705 //DebugMessage(M64MSG_VERBOSE, "no-match due to different register");
9708 if((regs[j+1].is32&(1LL<<f_regmap[hr]))!=(regs[j].is32&(1LL<<f_regmap[hr]))) {
9709 //DebugMessage(M64MSG_VERBOSE, "32/64 mismatch %x %d",start+j*4,hr);
9712 if(itype[j]==UJUMP||itype[j]==RJUMP||(source[j]>>16)==0x1000)
9714 // Stop on unconditional branch
9717 if(itype[j]==CJUMP||itype[j]==SJUMP||itype[j]==FJUMP)
9720 if(count_free_regs(regs[j].regmap)<=minimum_free_regs[j+1])
9723 if(count_free_regs(branch_regs[j].regmap)<=minimum_free_regs[j+1])
9726 if(get_reg(branch_regs[j].regmap,f_regmap[hr])>=0) {
9727 //DebugMessage(M64MSG_VERBOSE, "no-match due to different register (branch)");
9731 if(count_free_regs(regs[j].regmap)<=minimum_free_regs[j]) {
9732 //DebugMessage(M64MSG_VERBOSE, "No free regs for store %x",start+j*4);
9735 if(f_regmap[hr]>=64) {
9736 if(regs[j].is32&(1LL<<(f_regmap[hr]&63))) {
9741 if(get_reg(regs[j].regmap,f_regmap[hr]&63)<0) {
9752 // Non branch or undetermined branch target
9753 for(hr=0;hr<HOST_REGS;hr++)
9755 if(hr!=EXCLUDE_REG) {
9756 if(regs[i].regmap[hr]>64) {
9757 if(!((regs[i].dirty>>hr)&1))
9758 f_regmap[hr]=regs[i].regmap[hr];
9760 else if(regs[i].regmap[hr]>=0) {
9761 if(f_regmap[hr]!=regs[i].regmap[hr]) {
9762 // dealloc old register
9764 for(n=0;n<HOST_REGS;n++)
9766 if(f_regmap[n]==regs[i].regmap[hr]) {f_regmap[n]=-1;}
9768 // and alloc new one
9769 f_regmap[hr]=regs[i].regmap[hr];
9774 // Try to restore cycle count at branch targets
9776 for(j=i;j<slen-1;j++) {
9777 if(regs[j].regmap[HOST_CCREG]!=-1) break;
9778 if(count_free_regs(regs[j].regmap)<=minimum_free_regs[j]) {
9779 //DebugMessage(M64MSG_VERBOSE, "no free regs for store %x",start+j*4);
9783 if(regs[j].regmap[HOST_CCREG]==CCREG) {
9785 //DebugMessage(M64MSG_VERBOSE, "Extend CC, %x -> %x",start+k*4,start+j*4);
9787 regs[k].regmap_entry[HOST_CCREG]=CCREG;
9788 regs[k].regmap[HOST_CCREG]=CCREG;
9789 regmap_pre[k+1][HOST_CCREG]=CCREG;
9790 regs[k+1].wasdirty|=1<<HOST_CCREG;
9791 regs[k].dirty|=1<<HOST_CCREG;
9792 regs[k].wasconst&=~(1<<HOST_CCREG);
9793 regs[k].isconst&=~(1<<HOST_CCREG);
9796 regs[j].regmap_entry[HOST_CCREG]=CCREG;
9798 // Work backwards from the branch target
9799 if(j>i&&f_regmap[HOST_CCREG]==CCREG)
9801 //DebugMessage(M64MSG_VERBOSE, "Extend backwards");
9804 while(regs[k-1].regmap[HOST_CCREG]==-1) {
9805 if(count_free_regs(regs[k-1].regmap)<=minimum_free_regs[k-1]) {
9806 //DebugMessage(M64MSG_VERBOSE, "no free regs for store %x",start+(k-1)*4);
9811 if(regs[k-1].regmap[HOST_CCREG]==CCREG) {
9812 //DebugMessage(M64MSG_VERBOSE, "Extend CC, %x ->",start+k*4);
9814 regs[k].regmap_entry[HOST_CCREG]=CCREG;
9815 regs[k].regmap[HOST_CCREG]=CCREG;
9816 regmap_pre[k+1][HOST_CCREG]=CCREG;
9817 regs[k+1].wasdirty|=1<<HOST_CCREG;
9818 regs[k].dirty|=1<<HOST_CCREG;
9819 regs[k].wasconst&=~(1<<HOST_CCREG);
9820 regs[k].isconst&=~(1<<HOST_CCREG);
9825 //DebugMessage(M64MSG_VERBOSE, "Fail Extend CC, %x ->",start+k*4);
9829 if(itype[i]!=STORE&&itype[i]!=STORELR&&itype[i]!=C1LS&&itype[i]!=SHIFT&&
9830 itype[i]!=NOP&&itype[i]!=MOV&&itype[i]!=ALU&&itype[i]!=SHIFTIMM&&
9831 itype[i]!=IMM16&&itype[i]!=LOAD&&itype[i]!=COP1&&itype[i]!=FLOAT&&
9832 itype[i]!=FCONV&&itype[i]!=FCOMP)
9834 memcpy(f_regmap,regs[i].regmap,sizeof(f_regmap));
9839 // Cache memory offset or tlb map pointer if a register is available
9840 #ifndef HOST_IMM_ADDR32
9845 int earliest_available[HOST_REGS];
9846 int loop_start[HOST_REGS];
9847 int score[HOST_REGS];
9849 int reg=using_tlb?MMREG:ROREG;
9852 for(hr=0;hr<HOST_REGS;hr++) {
9853 score[hr]=0;earliest_available[hr]=0;
9854 loop_start[hr]=MAXBLOCK;
9856 for(i=0;i<slen-1;i++)
9858 // Can't do anything if no registers are available
9859 if(count_free_regs(regs[i].regmap)<=minimum_free_regs[i]) {
9860 for(hr=0;hr<HOST_REGS;hr++) {
9861 score[hr]=0;earliest_available[hr]=i+1;
9862 loop_start[hr]=MAXBLOCK;
9865 if(itype[i]==UJUMP||itype[i]==RJUMP||itype[i]==CJUMP||itype[i]==SJUMP||itype[i]==FJUMP) {
9867 if(count_free_regs(branch_regs[i].regmap)<=minimum_free_regs[i+1]) {
9868 for(hr=0;hr<HOST_REGS;hr++) {
9869 score[hr]=0;earliest_available[hr]=i+1;
9870 loop_start[hr]=MAXBLOCK;
9874 if(count_free_regs(regs[i].regmap)<=minimum_free_regs[i+1]) {
9875 for(hr=0;hr<HOST_REGS;hr++) {
9876 score[hr]=0;earliest_available[hr]=i+1;
9877 loop_start[hr]=MAXBLOCK;
9882 // Mark unavailable registers
9883 for(hr=0;hr<HOST_REGS;hr++) {
9884 if(regs[i].regmap[hr]>=0) {
9885 score[hr]=0;earliest_available[hr]=i+1;
9886 loop_start[hr]=MAXBLOCK;
9888 if(itype[i]==UJUMP||itype[i]==RJUMP||itype[i]==CJUMP||itype[i]==SJUMP||itype[i]==FJUMP) {
9889 if(branch_regs[i].regmap[hr]>=0) {
9890 score[hr]=0;earliest_available[hr]=i+2;
9891 loop_start[hr]=MAXBLOCK;
9895 // No register allocations after unconditional jumps
9896 if(itype[i]==UJUMP||itype[i]==RJUMP||(source[i]>>16)==0x1000)
9898 for(hr=0;hr<HOST_REGS;hr++) {
9899 score[hr]=0;earliest_available[hr]=i+2;
9900 loop_start[hr]=MAXBLOCK;
9902 i++; // Skip delay slot too
9903 //DebugMessage(M64MSG_VERBOSE, "skip delay slot: %x",start+i*4);
9907 if(itype[i]==LOAD||itype[i]==LOADLR||
9908 itype[i]==STORE||itype[i]==STORELR||itype[i]==C1LS) {
9909 for(hr=0;hr<HOST_REGS;hr++) {
9910 if(hr!=EXCLUDE_REG) {
9912 for(j=i;j<slen-1;j++) {
9913 if(regs[j].regmap[hr]>=0) break;
9914 if(itype[j]==UJUMP||itype[j]==RJUMP||itype[j]==CJUMP||itype[j]==SJUMP||itype[j]==FJUMP) {
9915 if(branch_regs[j].regmap[hr]>=0) break;
9917 if(count_free_regs(regs[j].regmap)<=minimum_free_regs[j+1]) break;
9919 if(count_free_regs(branch_regs[j].regmap)<=minimum_free_regs[j+1]) break;
9922 else if(count_free_regs(regs[j].regmap)<=minimum_free_regs[j]) break;
9923 if(itype[j]==UJUMP||itype[j]==RJUMP||itype[j]==CJUMP||itype[j]==SJUMP||itype[j]==FJUMP) {
9924 int t=(ba[j]-start)>>2;
9925 if(t<j&&t>=earliest_available[hr]) {
9926 if(t==1||(t>1&&itype[t-2]!=UJUMP&&itype[t-2]!=RJUMP)||(t>1&&rt1[t-2]!=31)) { // call/ret assumes no registers allocated
9927 // Score a point for hoisting loop invariant
9928 if(t<loop_start[hr]) loop_start[hr]=t;
9929 //DebugMessage(M64MSG_VERBOSE, "set loop_start: i=%x j=%x (%x)",start+i*4,start+j*4,start+t*4);
9935 if(regs[t].regmap[hr]==reg) {
9936 // Score a point if the branch target matches this register
9941 if(itype[j+1]==LOAD||itype[j+1]==LOADLR||
9942 itype[j+1]==STORE||itype[j+1]==STORELR||itype[j+1]==C1LS) {
9947 if(itype[j]==UJUMP||itype[j]==RJUMP||(source[j]>>16)==0x1000)
9949 // Stop on unconditional branch
9953 if(itype[j]==LOAD||itype[j]==LOADLR||
9954 itype[j]==STORE||itype[j]==STORELR||itype[j]==C1LS) {
9961 // Find highest score and allocate that register
9963 for(hr=0;hr<HOST_REGS;hr++) {
9964 if(hr!=EXCLUDE_REG) {
9965 if(score[hr]>score[maxscore]) {
9967 //DebugMessage(M64MSG_VERBOSE, "highest score: %d %d (%x->%x)",score[hr],hr,start+i*4,start+end[hr]*4);
9971 if(score[maxscore]>1)
9973 if(i<loop_start[maxscore]) loop_start[maxscore]=i;
9974 for(j=loop_start[maxscore];j<slen&&j<=end[maxscore];j++) {
9975 //if(regs[j].regmap[maxscore]>=0) {DebugMessage(M64MSG_ERROR, "oops: %x %x was %d=%d",loop_start[maxscore]*4+start,j*4+start,maxscore,regs[j].regmap[maxscore]);}
9976 assert(regs[j].regmap[maxscore]<0);
9977 if(j>loop_start[maxscore]) regs[j].regmap_entry[maxscore]=reg;
9978 regs[j].regmap[maxscore]=reg;
9979 regs[j].dirty&=~(1<<maxscore);
9980 regs[j].wasconst&=~(1<<maxscore);
9981 regs[j].isconst&=~(1<<maxscore);
9982 if(itype[j]==UJUMP||itype[j]==RJUMP||itype[j]==CJUMP||itype[j]==SJUMP||itype[j]==FJUMP) {
9983 branch_regs[j].regmap[maxscore]=reg;
9984 branch_regs[j].wasdirty&=~(1<<maxscore);
9985 branch_regs[j].dirty&=~(1<<maxscore);
9986 branch_regs[j].wasconst&=~(1<<maxscore);
9987 branch_regs[j].isconst&=~(1<<maxscore);
9988 if(itype[j]!=RJUMP&&itype[j]!=UJUMP&&(source[j]>>16)!=0x1000) {
9989 regmap_pre[j+2][maxscore]=reg;
9990 regs[j+2].wasdirty&=~(1<<maxscore);
9992 // loop optimization (loop_preload)
9993 int t=(ba[j]-start)>>2;
9994 if(t==loop_start[maxscore]) {
9995 if(t==1||(t>1&&itype[t-2]!=UJUMP&&itype[t-2]!=RJUMP)||(t>1&&rt1[t-2]!=31)) // call/ret assumes no registers allocated
9996 regs[t].regmap_entry[maxscore]=reg;
10001 if(j<1||(itype[j-1]!=RJUMP&&itype[j-1]!=UJUMP&&itype[j-1]!=CJUMP&&itype[j-1]!=SJUMP&&itype[j-1]!=FJUMP)) {
10002 regmap_pre[j+1][maxscore]=reg;
10003 regs[j+1].wasdirty&=~(1<<maxscore);
10008 if(itype[j-1]==RJUMP||itype[j-1]==UJUMP||itype[j-1]==CJUMP||itype[j-1]==SJUMP||itype[j-1]==FJUMP) i++; // skip delay slot
10009 for(hr=0;hr<HOST_REGS;hr++) {
10010 score[hr]=0;earliest_available[hr]=i+i;
10011 loop_start[hr]=MAXBLOCK;
10019 // This allocates registers (if possible) one instruction prior
10020 // to use, which can avoid a load-use penalty on certain CPUs.
10021 for(i=0;i<slen-1;i++)
10023 if(!i||(itype[i-1]!=UJUMP&&itype[i-1]!=CJUMP&&itype[i-1]!=SJUMP&&itype[i-1]!=RJUMP&&itype[i-1]!=FJUMP))
10027 if(itype[i]==ALU||itype[i]==MOV||itype[i]==LOAD||itype[i]==SHIFTIMM||itype[i]==IMM16||(itype[i]==COP1&&opcode2[i]<3))
10030 if((hr=get_reg(regs[i+1].regmap,rs1[i+1]))>=0)
10032 if(regs[i].regmap[hr]<0&®s[i+1].regmap_entry[hr]<0)
10034 regs[i].regmap[hr]=regs[i+1].regmap[hr];
10035 regmap_pre[i+1][hr]=regs[i+1].regmap[hr];
10036 regs[i+1].regmap_entry[hr]=regs[i+1].regmap[hr];
10037 regs[i].isconst&=~(1<<hr);
10038 regs[i].isconst|=regs[i+1].isconst&(1<<hr);
10039 constmap[i][hr]=constmap[i+1][hr];
10040 regs[i+1].wasdirty&=~(1<<hr);
10041 regs[i].dirty&=~(1<<hr);
10046 if((hr=get_reg(regs[i+1].regmap,rs2[i+1]))>=0)
10048 if(regs[i].regmap[hr]<0&®s[i+1].regmap_entry[hr]<0)
10050 regs[i].regmap[hr]=regs[i+1].regmap[hr];
10051 regmap_pre[i+1][hr]=regs[i+1].regmap[hr];
10052 regs[i+1].regmap_entry[hr]=regs[i+1].regmap[hr];
10053 regs[i].isconst&=~(1<<hr);
10054 regs[i].isconst|=regs[i+1].isconst&(1<<hr);
10055 constmap[i][hr]=constmap[i+1][hr];
10056 regs[i+1].wasdirty&=~(1<<hr);
10057 regs[i].dirty&=~(1<<hr);
10061 // Preload target address for load instruction (non-constant)
10062 if(itype[i+1]==LOAD&&rs1[i+1]&&get_reg(regs[i+1].regmap,rs1[i+1])<0) {
10063 if((hr=get_reg(regs[i+1].regmap,rt1[i+1]))>=0)
10065 if(regs[i].regmap[hr]<0&®s[i+1].regmap_entry[hr]<0)
10067 regs[i].regmap[hr]=rs1[i+1];
10068 regmap_pre[i+1][hr]=rs1[i+1];
10069 regs[i+1].regmap_entry[hr]=rs1[i+1];
10070 regs[i].isconst&=~(1<<hr);
10071 regs[i].isconst|=regs[i+1].isconst&(1<<hr);
10072 constmap[i][hr]=constmap[i+1][hr];
10073 regs[i+1].wasdirty&=~(1<<hr);
10074 regs[i].dirty&=~(1<<hr);
10078 // Load source into target register
10079 if(lt1[i+1]&&get_reg(regs[i+1].regmap,rs1[i+1])<0) {
10080 if((hr=get_reg(regs[i+1].regmap,rt1[i+1]))>=0)
10082 if(regs[i].regmap[hr]<0&®s[i+1].regmap_entry[hr]<0)
10084 regs[i].regmap[hr]=rs1[i+1];
10085 regmap_pre[i+1][hr]=rs1[i+1];
10086 regs[i+1].regmap_entry[hr]=rs1[i+1];
10087 regs[i].isconst&=~(1<<hr);
10088 regs[i].isconst|=regs[i+1].isconst&(1<<hr);
10089 constmap[i][hr]=constmap[i+1][hr];
10090 regs[i+1].wasdirty&=~(1<<hr);
10091 regs[i].dirty&=~(1<<hr);
10095 // Preload map address
10096 #ifndef HOST_IMM_ADDR32
10097 if(itype[i+1]==LOAD||itype[i+1]==LOADLR||itype[i+1]==STORE||itype[i+1]==STORELR||itype[i+1]==C1LS) {
10098 hr=get_reg(regs[i+1].regmap,TLREG);
10100 int sr=get_reg(regs[i+1].regmap,rs1[i+1]);
10101 if(sr>=0&&((regs[i+1].wasconst>>sr)&1)) {
10103 if(regs[i].regmap[hr]<0&®s[i+1].regmap_entry[hr]<0)
10105 regs[i].regmap[hr]=MGEN1+((i+1)&1);
10106 regmap_pre[i+1][hr]=MGEN1+((i+1)&1);
10107 regs[i+1].regmap_entry[hr]=MGEN1+((i+1)&1);
10108 regs[i].isconst&=~(1<<hr);
10109 regs[i].isconst|=regs[i+1].isconst&(1<<hr);
10110 constmap[i][hr]=constmap[i+1][hr];
10111 regs[i+1].wasdirty&=~(1<<hr);
10112 regs[i].dirty&=~(1<<hr);
10114 else if((nr=get_reg2(regs[i].regmap,regs[i+1].regmap,-1))>=0)
10116 // move it to another register
10117 regs[i+1].regmap[hr]=-1;
10118 regmap_pre[i+2][hr]=-1;
10119 regs[i+1].regmap[nr]=TLREG;
10120 regmap_pre[i+2][nr]=TLREG;
10121 regs[i].regmap[nr]=MGEN1+((i+1)&1);
10122 regmap_pre[i+1][nr]=MGEN1+((i+1)&1);
10123 regs[i+1].regmap_entry[nr]=MGEN1+((i+1)&1);
10124 regs[i].isconst&=~(1<<nr);
10125 regs[i+1].isconst&=~(1<<nr);
10126 regs[i].dirty&=~(1<<nr);
10127 regs[i+1].wasdirty&=~(1<<nr);
10128 regs[i+1].dirty&=~(1<<nr);
10129 regs[i+2].wasdirty&=~(1<<nr);
10135 // Address for store instruction (non-constant)
10136 if(itype[i+1]==STORE||itype[i+1]==STORELR||opcode[i+1]==0x39||opcode[i+1]==0x3D) { // SB/SH/SW/SD/SWC1/SDC1
10137 if(get_reg(regs[i+1].regmap,rs1[i+1])<0) {
10138 hr=get_reg2(regs[i].regmap,regs[i+1].regmap,-1);
10139 if(hr<0) hr=get_reg(regs[i+1].regmap,-1);
10140 else {regs[i+1].regmap[hr]=AGEN1+((i+1)&1);regs[i+1].isconst&=~(1<<hr);}
10142 if(regs[i].regmap[hr]<0&®s[i+1].regmap_entry[hr]<0)
10144 regs[i].regmap[hr]=rs1[i+1];
10145 regmap_pre[i+1][hr]=rs1[i+1];
10146 regs[i+1].regmap_entry[hr]=rs1[i+1];
10147 regs[i].isconst&=~(1<<hr);
10148 regs[i].isconst|=regs[i+1].isconst&(1<<hr);
10149 constmap[i][hr]=constmap[i+1][hr];
10150 regs[i+1].wasdirty&=~(1<<hr);
10151 regs[i].dirty&=~(1<<hr);
10155 if(itype[i+1]==LOADLR||opcode[i+1]==0x31||opcode[i+1]==0x35) { // LWC1/LDC1
10156 if(get_reg(regs[i+1].regmap,rs1[i+1])<0) {
10158 hr=get_reg(regs[i+1].regmap,FTEMP);
10160 if(regs[i].regmap[hr]<0&®s[i+1].regmap_entry[hr]<0)
10162 regs[i].regmap[hr]=rs1[i+1];
10163 regmap_pre[i+1][hr]=rs1[i+1];
10164 regs[i+1].regmap_entry[hr]=rs1[i+1];
10165 regs[i].isconst&=~(1<<hr);
10166 regs[i].isconst|=regs[i+1].isconst&(1<<hr);
10167 constmap[i][hr]=constmap[i+1][hr];
10168 regs[i+1].wasdirty&=~(1<<hr);
10169 regs[i].dirty&=~(1<<hr);
10171 else if((nr=get_reg2(regs[i].regmap,regs[i+1].regmap,-1))>=0)
10173 // move it to another register
10174 regs[i+1].regmap[hr]=-1;
10175 regmap_pre[i+2][hr]=-1;
10176 regs[i+1].regmap[nr]=FTEMP;
10177 regmap_pre[i+2][nr]=FTEMP;
10178 regs[i].regmap[nr]=rs1[i+1];
10179 regmap_pre[i+1][nr]=rs1[i+1];
10180 regs[i+1].regmap_entry[nr]=rs1[i+1];
10181 regs[i].isconst&=~(1<<nr);
10182 regs[i+1].isconst&=~(1<<nr);
10183 regs[i].dirty&=~(1<<nr);
10184 regs[i+1].wasdirty&=~(1<<nr);
10185 regs[i+1].dirty&=~(1<<nr);
10186 regs[i+2].wasdirty&=~(1<<nr);
10190 if(itype[i+1]==LOAD||itype[i+1]==LOADLR||itype[i+1]==STORE||itype[i+1]==STORELR/*||itype[i+1]==C1LS*/) {
10191 if(itype[i+1]==LOAD)
10192 hr=get_reg(regs[i+1].regmap,rt1[i+1]);
10193 if(itype[i+1]==LOADLR||opcode[i+1]==0x31||opcode[i+1]==0x35) // LWC1/LDC1
10194 hr=get_reg(regs[i+1].regmap,FTEMP);
10195 if(itype[i+1]==STORE||itype[i+1]==STORELR||opcode[i+1]==0x39||opcode[i+1]==0x3D) { // SWC1/SDC1
10196 hr=get_reg(regs[i+1].regmap,AGEN1+((i+1)&1));
10197 if(hr<0) hr=get_reg(regs[i+1].regmap,-1);
10199 if(hr>=0&®s[i].regmap[hr]<0) {
10200 int rs=get_reg(regs[i+1].regmap,rs1[i+1]);
10201 if(rs>=0&&((regs[i+1].wasconst>>rs)&1)) {
10202 regs[i].regmap[hr]=AGEN1+((i+1)&1);
10203 regmap_pre[i+1][hr]=AGEN1+((i+1)&1);
10204 regs[i+1].regmap_entry[hr]=AGEN1+((i+1)&1);
10205 regs[i].isconst&=~(1<<hr);
10206 regs[i+1].wasdirty&=~(1<<hr);
10207 regs[i].dirty&=~(1<<hr);
10216 /* Pass 6 - Optimize clean/dirty state */
10217 clean_registers(0,slen-1,1);
10219 /* Pass 7 - Identify 32-bit registers */
10225 for (i=slen-1;i>=0;i--)
10228 if(itype[i]==RJUMP||itype[i]==UJUMP||itype[i]==CJUMP||itype[i]==SJUMP||itype[i]==FJUMP)
10230 if(ba[i]<start || ba[i]>=(start+slen*4))
10232 // Branch out of this block, don't need anything
10238 // Need whatever matches the target
10239 // (and doesn't get overwritten by the delay slot instruction)
10241 int t=(ba[i]-start)>>2;
10242 if(ba[i]>start+i*4) {
10244 if(!(requires_32bit[t]&~regs[i].was32))
10245 r32|=requires_32bit[t]&(~(1LL<<rt1[i+1]))&(~(1LL<<rt2[i+1]));
10248 //if(!(regs[t].was32&~unneeded_reg_upper[t]&~regs[i].was32))
10249 // r32|=regs[t].was32&~unneeded_reg_upper[t]&(~(1LL<<rt1[i+1]))&(~(1LL<<rt2[i+1]));
10250 if(!(pr32[t]&~regs[i].was32))
10251 r32|=pr32[t]&(~(1LL<<rt1[i+1]))&(~(1LL<<rt2[i+1]));
10254 // Conditional branch may need registers for following instructions
10255 if(itype[i]!=RJUMP&&itype[i]!=UJUMP&&(source[i]>>16)!=0x1000)
10258 r32|=requires_32bit[i+2];
10259 r32&=regs[i].was32;
10260 // Mark this address as a branch target since it may be called
10261 // upon return from interrupt
10265 // Merge in delay slot
10267 // These are overwritten unless the branch is "likely"
10268 // and the delay slot is nullified if not taken
10269 r32&=~(1LL<<rt1[i+1]);
10270 r32&=~(1LL<<rt2[i+1]);
10272 // Assume these are needed (delay slot)
10275 if((regs[i].was32>>us1[i+1])&1) r32|=1LL<<us1[i+1];
10279 if((regs[i].was32>>us2[i+1])&1) r32|=1LL<<us2[i+1];
10281 if(dep1[i+1]&&!((unneeded_reg_upper[i]>>dep1[i+1])&1))
10283 if((regs[i].was32>>dep1[i+1])&1) r32|=1LL<<dep1[i+1];
10285 if(dep2[i+1]&&!((unneeded_reg_upper[i]>>dep2[i+1])&1))
10287 if((regs[i].was32>>dep2[i+1])&1) r32|=1LL<<dep2[i+1];
10290 else if(itype[i]==SYSCALL)
10292 // SYSCALL instruction (software interrupt)
10295 else if(itype[i]==COP0 && (source[i]&0x3f)==0x18)
10297 // ERET instruction (return from interrupt)
10301 r32&=~(1LL<<rt1[i]);
10302 r32&=~(1LL<<rt2[i]);
10305 if((regs[i].was32>>us1[i])&1) r32|=1LL<<us1[i];
10309 if((regs[i].was32>>us2[i])&1) r32|=1LL<<us2[i];
10311 if(dep1[i]&&!((unneeded_reg_upper[i]>>dep1[i])&1))
10313 if((regs[i].was32>>dep1[i])&1) r32|=1LL<<dep1[i];
10315 if(dep2[i]&&!((unneeded_reg_upper[i]>>dep2[i])&1))
10317 if((regs[i].was32>>dep2[i])&1) r32|=1LL<<dep2[i];
10319 requires_32bit[i]=r32;
10321 // Dirty registers which are 32-bit, require 32-bit input
10322 // as they will be written as 32-bit values
10323 for(hr=0;hr<HOST_REGS;hr++)
10325 if(regs[i].regmap_entry[hr]>0&®s[i].regmap_entry[hr]<64) {
10326 if((regs[i].was32>>regs[i].regmap_entry[hr])&(regs[i].wasdirty>>hr)&1) {
10327 if(!((unneeded_reg_upper[i]>>regs[i].regmap_entry[hr])&1))
10328 requires_32bit[i]|=1LL<<regs[i].regmap_entry[hr];
10332 //requires_32bit[i]=is32[i]&~unneeded_reg_upper[i]; // DEBUG
10335 if(itype[slen-1]==SPAN) {
10336 bt[slen-1]=1; // Mark as a branch target so instruction can restart after exception
10339 /* Debug/disassembly */
10340 // if((void*)assem_debug==(void*)printf)
10341 #if defined( ASSEM_DEBUG )
10342 for(i=0;i<slen;i++)
10344 DebugMessage(M64MSG_VERBOSE, "U:");
10346 for(r=1;r<=CCREG;r++) {
10347 if((unneeded_reg[i]>>r)&1) {
10348 if(r==HIREG) DebugMessage(M64MSG_VERBOSE, " HI");
10349 else if(r==LOREG) DebugMessage(M64MSG_VERBOSE, " LO");
10350 else DebugMessage(M64MSG_VERBOSE, " r%d",r);
10353 DebugMessage(M64MSG_VERBOSE, " UU:");
10354 for(r=1;r<=CCREG;r++) {
10355 if(((unneeded_reg_upper[i]&~unneeded_reg[i])>>r)&1) {
10356 if(r==HIREG) DebugMessage(M64MSG_VERBOSE, " HI");
10357 else if(r==LOREG) DebugMessage(M64MSG_VERBOSE, " LO");
10358 else DebugMessage(M64MSG_VERBOSE, " r%d",r);
10361 DebugMessage(M64MSG_VERBOSE, " 32:");
10362 for(r=0;r<=CCREG;r++) {
10363 //if(((is32[i]>>r)&(~unneeded_reg[i]>>r))&1) {
10364 if((regs[i].was32>>r)&1) {
10365 if(r==CCREG) DebugMessage(M64MSG_VERBOSE, " CC");
10366 else if(r==HIREG) DebugMessage(M64MSG_VERBOSE, " HI");
10367 else if(r==LOREG) DebugMessage(M64MSG_VERBOSE, " LO");
10368 else DebugMessage(M64MSG_VERBOSE, " r%d",r);
10371 #if NEW_DYNAREC == NEW_DYNAREC_X86
10372 DebugMessage(M64MSG_VERBOSE, "pre: eax=%d ecx=%d edx=%d ebx=%d ebp=%d esi=%d edi=%d",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]);
10374 #if NEW_DYNAREC == NEW_DYNAREC_ARM
10375 DebugMessage(M64MSG_VERBOSE, "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",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]);
10377 DebugMessage(M64MSG_VERBOSE, "needs: ");
10378 if(needed_reg[i]&1) DebugMessage(M64MSG_VERBOSE, "eax ");
10379 if((needed_reg[i]>>1)&1) DebugMessage(M64MSG_VERBOSE, "ecx ");
10380 if((needed_reg[i]>>2)&1) DebugMessage(M64MSG_VERBOSE, "edx ");
10381 if((needed_reg[i]>>3)&1) DebugMessage(M64MSG_VERBOSE, "ebx ");
10382 if((needed_reg[i]>>5)&1) DebugMessage(M64MSG_VERBOSE, "ebp ");
10383 if((needed_reg[i]>>6)&1) DebugMessage(M64MSG_VERBOSE, "esi ");
10384 if((needed_reg[i]>>7)&1) DebugMessage(M64MSG_VERBOSE, "edi ");
10385 DebugMessage(M64MSG_VERBOSE, "r:");
10386 for(r=0;r<=CCREG;r++) {
10387 //if(((requires_32bit[i]>>r)&(~unneeded_reg[i]>>r))&1) {
10388 if((requires_32bit[i]>>r)&1) {
10389 if(r==CCREG) DebugMessage(M64MSG_VERBOSE, " CC");
10390 else if(r==HIREG) DebugMessage(M64MSG_VERBOSE, " HI");
10391 else if(r==LOREG) DebugMessage(M64MSG_VERBOSE, " LO");
10392 else DebugMessage(M64MSG_VERBOSE, " r%d",r);
10395 /*DebugMessage(M64MSG_VERBOSE, "pr:");
10396 for(r=0;r<=CCREG;r++) {
10397 //if(((requires_32bit[i]>>r)&(~unneeded_reg[i]>>r))&1) {
10398 if((pr32[i]>>r)&1) {
10399 if(r==CCREG) DebugMessage(M64MSG_VERBOSE, " CC");
10400 else if(r==HIREG) DebugMessage(M64MSG_VERBOSE, " HI");
10401 else if(r==LOREG) DebugMessage(M64MSG_VERBOSE, " LO");
10402 else DebugMessage(M64MSG_VERBOSE, " r%d",r);
10405 if(pr32[i]!=requires_32bit[i]) DebugMessage(M64MSG_ERROR, " OOPS");*/
10406 #if NEW_DYNAREC == NEW_DYNAREC_X86
10407 DebugMessage(M64MSG_VERBOSE, "entry: eax=%d ecx=%d edx=%d ebx=%d ebp=%d esi=%d edi=%d",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]);
10408 DebugMessage(M64MSG_VERBOSE, "dirty: ");
10409 if(regs[i].wasdirty&1) DebugMessage(M64MSG_VERBOSE, "eax ");
10410 if((regs[i].wasdirty>>1)&1) DebugMessage(M64MSG_VERBOSE, "ecx ");
10411 if((regs[i].wasdirty>>2)&1) DebugMessage(M64MSG_VERBOSE, "edx ");
10412 if((regs[i].wasdirty>>3)&1) DebugMessage(M64MSG_VERBOSE, "ebx ");
10413 if((regs[i].wasdirty>>5)&1) DebugMessage(M64MSG_VERBOSE, "ebp ");
10414 if((regs[i].wasdirty>>6)&1) DebugMessage(M64MSG_VERBOSE, "esi ");
10415 if((regs[i].wasdirty>>7)&1) DebugMessage(M64MSG_VERBOSE, "edi ");
10417 #if NEW_DYNAREC == NEW_DYNAREC_ARM
10418 DebugMessage(M64MSG_VERBOSE, "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",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]);
10419 DebugMessage(M64MSG_VERBOSE, "dirty: ");
10420 if(regs[i].wasdirty&1) DebugMessage(M64MSG_VERBOSE, "r0 ");
10421 if((regs[i].wasdirty>>1)&1) DebugMessage(M64MSG_VERBOSE, "r1 ");
10422 if((regs[i].wasdirty>>2)&1) DebugMessage(M64MSG_VERBOSE, "r2 ");
10423 if((regs[i].wasdirty>>3)&1) DebugMessage(M64MSG_VERBOSE, "r3 ");
10424 if((regs[i].wasdirty>>4)&1) DebugMessage(M64MSG_VERBOSE, "r4 ");
10425 if((regs[i].wasdirty>>5)&1) DebugMessage(M64MSG_VERBOSE, "r5 ");
10426 if((regs[i].wasdirty>>6)&1) DebugMessage(M64MSG_VERBOSE, "r6 ");
10427 if((regs[i].wasdirty>>7)&1) DebugMessage(M64MSG_VERBOSE, "r7 ");
10428 if((regs[i].wasdirty>>8)&1) DebugMessage(M64MSG_VERBOSE, "r8 ");
10429 if((regs[i].wasdirty>>9)&1) DebugMessage(M64MSG_VERBOSE, "r9 ");
10430 if((regs[i].wasdirty>>10)&1) DebugMessage(M64MSG_VERBOSE, "r10 ");
10431 if((regs[i].wasdirty>>12)&1) DebugMessage(M64MSG_VERBOSE, "r12 ");
10433 disassemble_inst(i);
10434 //printf ("ccadj[%d] = %d",i,ccadj[i]);
10435 #if NEW_DYNAREC == NEW_DYNAREC_X86
10436 DebugMessage(M64MSG_VERBOSE, "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]);
10437 if(regs[i].dirty&1) DebugMessage(M64MSG_VERBOSE, "eax ");
10438 if((regs[i].dirty>>1)&1) DebugMessage(M64MSG_VERBOSE, "ecx ");
10439 if((regs[i].dirty>>2)&1) DebugMessage(M64MSG_VERBOSE, "edx ");
10440 if((regs[i].dirty>>3)&1) DebugMessage(M64MSG_VERBOSE, "ebx ");
10441 if((regs[i].dirty>>5)&1) DebugMessage(M64MSG_VERBOSE, "ebp ");
10442 if((regs[i].dirty>>6)&1) DebugMessage(M64MSG_VERBOSE, "esi ");
10443 if((regs[i].dirty>>7)&1) DebugMessage(M64MSG_VERBOSE, "edi ");
10445 #if NEW_DYNAREC == NEW_DYNAREC_ARM
10446 DebugMessage(M64MSG_VERBOSE, "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]);
10447 if(regs[i].dirty&1) DebugMessage(M64MSG_VERBOSE, "r0 ");
10448 if((regs[i].dirty>>1)&1) DebugMessage(M64MSG_VERBOSE, "r1 ");
10449 if((regs[i].dirty>>2)&1) DebugMessage(M64MSG_VERBOSE, "r2 ");
10450 if((regs[i].dirty>>3)&1) DebugMessage(M64MSG_VERBOSE, "r3 ");
10451 if((regs[i].dirty>>4)&1) DebugMessage(M64MSG_VERBOSE, "r4 ");
10452 if((regs[i].dirty>>5)&1) DebugMessage(M64MSG_VERBOSE, "r5 ");
10453 if((regs[i].dirty>>6)&1) DebugMessage(M64MSG_VERBOSE, "r6 ");
10454 if((regs[i].dirty>>7)&1) DebugMessage(M64MSG_VERBOSE, "r7 ");
10455 if((regs[i].dirty>>8)&1) DebugMessage(M64MSG_VERBOSE, "r8 ");
10456 if((regs[i].dirty>>9)&1) DebugMessage(M64MSG_VERBOSE, "r9 ");
10457 if((regs[i].dirty>>10)&1) DebugMessage(M64MSG_VERBOSE, "r10 ");
10458 if((regs[i].dirty>>12)&1) DebugMessage(M64MSG_VERBOSE, "r12 ");
10460 if(regs[i].isconst) {
10461 DebugMessage(M64MSG_VERBOSE, "constants: ");
10462 #if NEW_DYNAREC == NEW_DYNAREC_X86
10463 if(regs[i].isconst&1) DebugMessage(M64MSG_VERBOSE, "eax=%x ",(int)constmap[i][0]);
10464 if((regs[i].isconst>>1)&1) DebugMessage(M64MSG_VERBOSE, "ecx=%x ",(int)constmap[i][1]);
10465 if((regs[i].isconst>>2)&1) DebugMessage(M64MSG_VERBOSE, "edx=%x ",(int)constmap[i][2]);
10466 if((regs[i].isconst>>3)&1) DebugMessage(M64MSG_VERBOSE, "ebx=%x ",(int)constmap[i][3]);
10467 if((regs[i].isconst>>5)&1) DebugMessage(M64MSG_VERBOSE, "ebp=%x ",(int)constmap[i][5]);
10468 if((regs[i].isconst>>6)&1) DebugMessage(M64MSG_VERBOSE, "esi=%x ",(int)constmap[i][6]);
10469 if((regs[i].isconst>>7)&1) DebugMessage(M64MSG_VERBOSE, "edi=%x ",(int)constmap[i][7]);
10471 #if NEW_DYNAREC == NEW_DYNAREC_ARM
10472 if(regs[i].isconst&1) DebugMessage(M64MSG_VERBOSE, "r0=%x ",(int)constmap[i][0]);
10473 if((regs[i].isconst>>1)&1) DebugMessage(M64MSG_VERBOSE, "r1=%x ",(int)constmap[i][1]);
10474 if((regs[i].isconst>>2)&1) DebugMessage(M64MSG_VERBOSE, "r2=%x ",(int)constmap[i][2]);
10475 if((regs[i].isconst>>3)&1) DebugMessage(M64MSG_VERBOSE, "r3=%x ",(int)constmap[i][3]);
10476 if((regs[i].isconst>>4)&1) DebugMessage(M64MSG_VERBOSE, "r4=%x ",(int)constmap[i][4]);
10477 if((regs[i].isconst>>5)&1) DebugMessage(M64MSG_VERBOSE, "r5=%x ",(int)constmap[i][5]);
10478 if((regs[i].isconst>>6)&1) DebugMessage(M64MSG_VERBOSE, "r6=%x ",(int)constmap[i][6]);
10479 if((regs[i].isconst>>7)&1) DebugMessage(M64MSG_VERBOSE, "r7=%x ",(int)constmap[i][7]);
10480 if((regs[i].isconst>>8)&1) DebugMessage(M64MSG_VERBOSE, "r8=%x ",(int)constmap[i][8]);
10481 if((regs[i].isconst>>9)&1) DebugMessage(M64MSG_VERBOSE, "r9=%x ",(int)constmap[i][9]);
10482 if((regs[i].isconst>>10)&1) DebugMessage(M64MSG_VERBOSE, "r10=%x ",(int)constmap[i][10]);
10483 if((regs[i].isconst>>12)&1) DebugMessage(M64MSG_VERBOSE, "r12=%x ",(int)constmap[i][12]);
10486 DebugMessage(M64MSG_VERBOSE, " 32:");
10487 for(r=0;r<=CCREG;r++) {
10488 if((regs[i].is32>>r)&1) {
10489 if(r==CCREG) DebugMessage(M64MSG_VERBOSE, " CC");
10490 else if(r==HIREG) DebugMessage(M64MSG_VERBOSE, " HI");
10491 else if(r==LOREG) DebugMessage(M64MSG_VERBOSE, " LO");
10492 else DebugMessage(M64MSG_VERBOSE, " r%d",r);
10495 /*DebugMessage(M64MSG_VERBOSE, " p32:");
10496 for(r=0;r<=CCREG;r++) {
10497 if((p32[i]>>r)&1) {
10498 if(r==CCREG) DebugMessage(M64MSG_VERBOSE, " CC");
10499 else if(r==HIREG) DebugMessage(M64MSG_VERBOSE, " HI");
10500 else if(r==LOREG) DebugMessage(M64MSG_VERBOSE, " LO");
10501 else DebugMessage(M64MSG_VERBOSE, " r%d",r);
10504 if(p32[i]!=regs[i].is32) DebugMessage(M64MSG_VERBOSE, " NO MATCH");*/
10505 if(itype[i]==RJUMP||itype[i]==UJUMP||itype[i]==CJUMP||itype[i]==SJUMP||itype[i]==FJUMP) {
10506 #if NEW_DYNAREC == NEW_DYNAREC_X86
10507 DebugMessage(M64MSG_VERBOSE, "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]);
10508 if(branch_regs[i].dirty&1) DebugMessage(M64MSG_VERBOSE, "eax ");
10509 if((branch_regs[i].dirty>>1)&1) DebugMessage(M64MSG_VERBOSE, "ecx ");
10510 if((branch_regs[i].dirty>>2)&1) DebugMessage(M64MSG_VERBOSE, "edx ");
10511 if((branch_regs[i].dirty>>3)&1) DebugMessage(M64MSG_VERBOSE, "ebx ");
10512 if((branch_regs[i].dirty>>5)&1) DebugMessage(M64MSG_VERBOSE, "ebp ");
10513 if((branch_regs[i].dirty>>6)&1) DebugMessage(M64MSG_VERBOSE, "esi ");
10514 if((branch_regs[i].dirty>>7)&1) DebugMessage(M64MSG_VERBOSE, "edi ");
10516 #if NEW_DYNAREC == NEW_DYNAREC_ARM
10517 DebugMessage(M64MSG_VERBOSE, "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]);
10518 if(branch_regs[i].dirty&1) DebugMessage(M64MSG_VERBOSE, "r0 ");
10519 if((branch_regs[i].dirty>>1)&1) DebugMessage(M64MSG_VERBOSE, "r1 ");
10520 if((branch_regs[i].dirty>>2)&1) DebugMessage(M64MSG_VERBOSE, "r2 ");
10521 if((branch_regs[i].dirty>>3)&1) DebugMessage(M64MSG_VERBOSE, "r3 ");
10522 if((branch_regs[i].dirty>>4)&1) DebugMessage(M64MSG_VERBOSE, "r4 ");
10523 if((branch_regs[i].dirty>>5)&1) DebugMessage(M64MSG_VERBOSE, "r5 ");
10524 if((branch_regs[i].dirty>>6)&1) DebugMessage(M64MSG_VERBOSE, "r6 ");
10525 if((branch_regs[i].dirty>>7)&1) DebugMessage(M64MSG_VERBOSE, "r7 ");
10526 if((branch_regs[i].dirty>>8)&1) DebugMessage(M64MSG_VERBOSE, "r8 ");
10527 if((branch_regs[i].dirty>>9)&1) DebugMessage(M64MSG_VERBOSE, "r9 ");
10528 if((branch_regs[i].dirty>>10)&1) DebugMessage(M64MSG_VERBOSE, "r10 ");
10529 if((branch_regs[i].dirty>>12)&1) DebugMessage(M64MSG_VERBOSE, "r12 ");
10531 DebugMessage(M64MSG_VERBOSE, " 32:");
10532 for(r=0;r<=CCREG;r++) {
10533 if((branch_regs[i].is32>>r)&1) {
10534 if(r==CCREG) DebugMessage(M64MSG_VERBOSE, " CC");
10535 else if(r==HIREG) DebugMessage(M64MSG_VERBOSE, " HI");
10536 else if(r==LOREG) DebugMessage(M64MSG_VERBOSE, " LO");
10537 else DebugMessage(M64MSG_VERBOSE, " r%d",r);
10544 /* Pass 8 - Assembly */
10545 linkcount=0;stubcount=0;
10546 ds=0;is_delayslot=0;
10548 #ifndef DESTRUCTIVE_WRITEBACK
10549 uint64_t is32_pre=0;
10552 u_int beginning=(u_int)out;
10553 if((u_int)addr&1) {
10557 for(i=0;i<slen;i++)
10559 //if(ds) DebugMessage(M64MSG_VERBOSE, "ds: ");
10560 // if((void*)assem_debug==(void*)printf) disassemble_inst(i);
10561 #if defined( ASSEM_DEBUG )
10562 disassemble_inst(i);
10565 ds=0; // Skip delay slot
10566 if(bt[i]) assem_debug("OOPS - branch into delay slot");
10569 #ifndef DESTRUCTIVE_WRITEBACK
10570 if(i<2||(itype[i-2]!=UJUMP&&itype[i-2]!=RJUMP&&(source[i-2]>>16)!=0x1000))
10572 wb_sx(regmap_pre[i],regs[i].regmap_entry,regs[i].wasdirty,is32_pre,regs[i].was32,
10573 unneeded_reg[i],unneeded_reg_upper[i]);
10574 wb_valid(regmap_pre[i],regs[i].regmap_entry,dirty_pre,regs[i].wasdirty,is32_pre,
10575 unneeded_reg[i],unneeded_reg_upper[i]);
10577 is32_pre=regs[i].is32;
10578 dirty_pre=regs[i].dirty;
10581 if(i<2||(itype[i-2]!=UJUMP&&itype[i-2]!=RJUMP&&(source[i-2]>>16)!=0x1000))
10583 wb_invalidate(regmap_pre[i],regs[i].regmap_entry,regs[i].wasdirty,regs[i].was32,
10584 unneeded_reg[i],unneeded_reg_upper[i]);
10585 loop_preload(regmap_pre[i],regs[i].regmap_entry);
10587 // branch target entry point
10588 instr_addr[i]=(u_int)out;
10589 assem_debug("<->");
10591 if(regs[i].regmap_entry[HOST_CCREG]==CCREG&®s[i].regmap[HOST_CCREG]!=CCREG)
10592 wb_register(CCREG,regs[i].regmap_entry,regs[i].wasdirty,regs[i].was32);
10593 load_regs(regs[i].regmap_entry,regs[i].regmap,regs[i].was32,rs1[i],rs2[i]);
10594 address_generation(i,®s[i],regs[i].regmap_entry);
10595 load_consts(regmap_pre[i],regs[i].regmap,regs[i].was32,i);
10596 if(itype[i]==RJUMP||itype[i]==UJUMP||itype[i]==CJUMP||itype[i]==SJUMP||itype[i]==FJUMP)
10598 // Load the delay slot registers if necessary
10599 if(rs1[i+1]!=rs1[i]&&rs1[i+1]!=rs2[i])
10600 load_regs(regs[i].regmap_entry,regs[i].regmap,regs[i].was32,rs1[i+1],rs1[i+1]);
10601 if(rs2[i+1]!=rs1[i+1]&&rs2[i+1]!=rs1[i]&&rs2[i+1]!=rs2[i])
10602 load_regs(regs[i].regmap_entry,regs[i].regmap,regs[i].was32,rs2[i+1],rs2[i+1]);
10603 if(itype[i+1]==STORE||itype[i+1]==STORELR||(opcode[i+1]&0x3b)==0x39)
10604 load_regs(regs[i].regmap_entry,regs[i].regmap,regs[i].was32,INVCP,INVCP);
10608 // Preload registers for following instruction
10609 if(rs1[i+1]!=rs1[i]&&rs1[i+1]!=rs2[i])
10610 if(rs1[i+1]!=rt1[i]&&rs1[i+1]!=rt2[i])
10611 load_regs(regs[i].regmap_entry,regs[i].regmap,regs[i].was32,rs1[i+1],rs1[i+1]);
10612 if(rs2[i+1]!=rs1[i+1]&&rs2[i+1]!=rs1[i]&&rs2[i+1]!=rs2[i])
10613 if(rs2[i+1]!=rt1[i]&&rs2[i+1]!=rt2[i])
10614 load_regs(regs[i].regmap_entry,regs[i].regmap,regs[i].was32,rs2[i+1],rs2[i+1]);
10616 // TODO: if(is_ooo(i)) address_generation(i+1);
10617 if(itype[i]==CJUMP||itype[i]==FJUMP)
10618 load_regs(regs[i].regmap_entry,regs[i].regmap,regs[i].was32,CCREG,CCREG);
10619 if(itype[i]==LOAD||itype[i]==LOADLR||itype[i]==STORE||itype[i]==STORELR||itype[i]==C1LS)
10620 load_regs(regs[i].regmap_entry,regs[i].regmap,regs[i].was32,MMREG,ROREG);
10621 if(itype[i]==STORE||itype[i]==STORELR||(opcode[i]&0x3b)==0x39)
10622 load_regs(regs[i].regmap_entry,regs[i].regmap,regs[i].was32,INVCP,INVCP);
10623 if(bt[i]) cop1_usable=0;
10627 alu_assemble(i,®s[i]);break;
10629 imm16_assemble(i,®s[i]);break;
10631 shift_assemble(i,®s[i]);break;
10633 shiftimm_assemble(i,®s[i]);break;
10635 load_assemble(i,®s[i]);break;
10637 loadlr_assemble(i,®s[i]);break;
10639 store_assemble(i,®s[i]);break;
10641 storelr_assemble(i,®s[i]);break;
10643 cop0_assemble(i,®s[i]);break;
10645 cop1_assemble(i,®s[i]);break;
10647 c1ls_assemble(i,®s[i]);break;
10649 fconv_assemble(i,®s[i]);break;
10651 float_assemble(i,®s[i]);break;
10653 fcomp_assemble(i,®s[i]);break;
10655 multdiv_assemble(i,®s[i]);break;
10657 mov_assemble(i,®s[i]);break;
10659 syscall_assemble(i,®s[i]);break;
10661 ujump_assemble(i,®s[i]);ds=1;break;
10663 rjump_assemble(i,®s[i]);ds=1;break;
10665 cjump_assemble(i,®s[i]);ds=1;break;
10667 sjump_assemble(i,®s[i]);ds=1;break;
10669 fjump_assemble(i,®s[i]);ds=1;break;
10671 pagespan_assemble(i,®s[i]);break;
10673 if(itype[i]==UJUMP||itype[i]==RJUMP||(source[i]>>16)==0x1000)
10674 literal_pool(1024);
10676 literal_pool_jumpover(256);
10679 //assert(itype[i-2]==UJUMP||itype[i-2]==RJUMP||(source[i-2]>>16)==0x1000);
10680 // If the block did not end with an unconditional branch,
10681 // add a jump to the next instruction.
10683 if(itype[i-2]!=UJUMP&&itype[i-2]!=RJUMP&&(source[i-2]>>16)!=0x1000&&itype[i-1]!=SPAN) {
10684 assert(itype[i-1]!=UJUMP&&itype[i-1]!=CJUMP&&itype[i-1]!=SJUMP&&itype[i-1]!=RJUMP&&itype[i-1]!=FJUMP);
10686 if(itype[i-2]!=CJUMP&&itype[i-2]!=SJUMP&&itype[i-2]!=FJUMP) {
10687 store_regs_bt(regs[i-1].regmap,regs[i-1].is32,regs[i-1].dirty,start+i*4);
10688 if(regs[i-1].regmap[HOST_CCREG]!=CCREG)
10689 emit_loadreg(CCREG,HOST_CCREG);
10690 emit_addimm(HOST_CCREG,CLOCK_DIVIDER*(ccadj[i-1]+1),HOST_CCREG);
10692 else if(!likely[i-2])
10694 store_regs_bt(branch_regs[i-2].regmap,branch_regs[i-2].is32,branch_regs[i-2].dirty,start+i*4);
10695 assert(branch_regs[i-2].regmap[HOST_CCREG]==CCREG);
10699 store_regs_bt(regs[i-2].regmap,regs[i-2].is32,regs[i-2].dirty,start+i*4);
10700 assert(regs[i-2].regmap[HOST_CCREG]==CCREG);
10702 add_to_linker((int)out,start+i*4,0);
10709 assert(itype[i-1]!=UJUMP&&itype[i-1]!=CJUMP&&itype[i-1]!=SJUMP&&itype[i-1]!=RJUMP&&itype[i-1]!=FJUMP);
10710 store_regs_bt(regs[i-1].regmap,regs[i-1].is32,regs[i-1].dirty,start+i*4);
10711 if(regs[i-1].regmap[HOST_CCREG]!=CCREG)
10712 emit_loadreg(CCREG,HOST_CCREG);
10713 emit_addimm(HOST_CCREG,CLOCK_DIVIDER*(ccadj[i-1]+1),HOST_CCREG);
10714 add_to_linker((int)out,start+i*4,0);
10718 // TODO: delay slot stubs?
10720 for(i=0;i<stubcount;i++)
10722 switch(stubs[i][0])
10730 do_readstub(i);break;
10735 do_writestub(i);break;
10737 do_ccstub(i);break;
10739 do_invstub(i);break;
10741 do_cop1stub(i);break;
10743 do_unalignedwritestub(i);break;
10747 /* Pass 9 - Linker */
10748 for(i=0;i<linkcount;i++)
10750 assem_debug("%8x -> %8x",link_addr[i][0],link_addr[i][1]);
10752 if(!link_addr[i][2])
10755 void *addr=check_addr(link_addr[i][1]);
10756 emit_extjump(link_addr[i][0],link_addr[i][1]);
10758 set_jump_target(link_addr[i][0],(int)addr);
10759 add_link(link_addr[i][1],stub);
10761 else set_jump_target(link_addr[i][0],(int)stub);
10766 int target=(link_addr[i][1]-start)>>2;
10767 assert(target>=0&&target<slen);
10768 assert(instr_addr[target]);
10769 //#ifdef CORTEX_A8_BRANCH_PREDICTION_HACK
10770 //set_jump_target_fillslot(link_addr[i][0],instr_addr[target],link_addr[i][2]>>1);
10772 set_jump_target(link_addr[i][0],instr_addr[target]);
10776 // External Branch Targets (jump_in)
10777 if(copy+slen*4>(void *)shadow+sizeof(shadow)) copy=shadow;
10778 for(i=0;i<slen;i++)
10782 if(instr_addr[i]) // TODO - delay slots (=null)
10784 u_int vaddr=start+i*4;
10785 u_int page=(0x80000000^vaddr)>>12;
10787 if(page>262143&&tlb_LUT_r[vaddr>>12]) page=(tlb_LUT_r[page^0x80000]^0x80000000)>>12;
10788 if(page>2048) page=2048+(page&2047);
10789 if(vpage>262143&&tlb_LUT_r[vaddr>>12]) vpage&=2047; // jump_dirty uses a hash of the virtual address instead
10790 if(vpage>2048) vpage=2048+(vpage&2047);
10792 //if(!(is32[i]&(~unneeded_reg_upper[i])&~(1LL<<CCREG)))
10793 if(!requires_32bit[i])
10795 assem_debug("%8x (%d) <- %8x",instr_addr[i],i,start+i*4);
10796 assem_debug("jump_in: %x",start+i*4);
10797 ll_add(jump_dirty+vpage,vaddr,(void *)out);
10798 int entry_point=do_dirty_stub(i);
10799 ll_add(jump_in+page,vaddr,(void *)entry_point);
10800 // If there was an existing entry in the hash table,
10801 // replace it with the new address.
10802 // Don't add new entries. We'll insert the
10803 // ones that actually get used in check_addr().
10804 u_int *ht_bin=hash_table[((vaddr>>16)^vaddr)&0xFFFF];
10805 if(ht_bin[0]==vaddr) {
10806 ht_bin[1]=entry_point;
10808 if(ht_bin[2]==vaddr) {
10809 ht_bin[3]=entry_point;
10814 u_int r=requires_32bit[i]|!!(requires_32bit[i]>>32);
10815 assem_debug("%8x (%d) <- %8x",instr_addr[i],i,start+i*4);
10816 assem_debug("jump_in: %x (restricted - %x)",start+i*4,r);
10817 //int entry_point=(int)out;
10818 ////assem_debug("entry_point: %x",entry_point);
10819 //load_regs_entry(i);
10820 //if(entry_point==(int)out)
10821 // entry_point=instr_addr[i];
10823 // emit_jmp(instr_addr[i]);
10824 //ll_add_32(jump_in+page,vaddr,r,(void *)entry_point);
10825 ll_add_32(jump_dirty+vpage,vaddr,r,(void *)out);
10826 int entry_point=do_dirty_stub(i);
10827 ll_add_32(jump_in+page,vaddr,r,(void *)entry_point);
10832 // Write out the literal pool if necessary
10834 #ifdef CORTEX_A8_BRANCH_PREDICTION_HACK
10836 if(((u_int)out)&7) emit_addnop(13);
10838 assert((u_int)out-beginning<MAX_OUTPUT_BLOCK_SIZE);
10839 //DebugMessage(M64MSG_VERBOSE, "shadow buffer: %x-%x",(int)copy,(int)copy+slen*4);
10840 memcpy(copy,source,slen*4);
10843 #if NEW_DYNAREC == NEW_DYNAREC_ARM
10844 __clear_cache((void *)beginning,out);
10845 //cacheflush((void *)beginning,out,0);
10848 // If we're within 256K of the end of the buffer,
10849 // start over from the beginning. (Is 256K enough?)
10850 if(out > (u_char *)(base_addr+(1<<TARGET_SIZE_2)-MAX_OUTPUT_BLOCK_SIZE-JUMP_TABLE_SIZE))
10851 out=(u_char *)base_addr;
10853 // Trap writes to any of the pages we compiled
10854 for(i=start>>12;i<=(start+slen*4)>>12;i++) {
10856 memory_map[i]|=0x40000000;
10857 if((signed int)start>=(signed int)0xC0000000) {
10859 j=(((u_int)i<<12)+(memory_map[i]<<2)-(u_int)rdram+(u_int)0x80000000)>>12;
10861 memory_map[j]|=0x40000000;
10862 //DebugMessage(M64MSG_VERBOSE, "write protect physical page: %x (virtual %x)",j<<12,start);
10866 /* Pass 10 - Free memory by expiring oldest blocks */
10868 int end=((((intptr_t)out-(intptr_t)base_addr)>>(TARGET_SIZE_2-16))+16384)&65535;
10869 while(expirep!=end)
10871 int shift=TARGET_SIZE_2-3; // Divide into 8 blocks
10872 int base=(int)base_addr+((expirep>>13)<<shift); // Base address of this block
10873 inv_debug("EXP: Phase %d\n",expirep);
10874 switch((expirep>>11)&3)
10877 // Clear jump_in and jump_dirty
10878 ll_remove_matching_addrs(jump_in+(expirep&2047),base,shift);
10879 ll_remove_matching_addrs(jump_dirty+(expirep&2047),base,shift);
10880 ll_remove_matching_addrs(jump_in+2048+(expirep&2047),base,shift);
10881 ll_remove_matching_addrs(jump_dirty+2048+(expirep&2047),base,shift);
10885 ll_kill_pointers(jump_out[expirep&2047],base,shift);
10886 ll_kill_pointers(jump_out[(expirep&2047)+2048],base,shift);
10889 // Clear hash table
10890 for(i=0;i<32;i++) {
10891 u_int *ht_bin=hash_table[((expirep&2047)<<5)+i];
10892 if((ht_bin[3]>>shift)==(base>>shift) ||
10893 ((ht_bin[3]-MAX_OUTPUT_BLOCK_SIZE)>>shift)==(base>>shift)) {
10894 inv_debug("EXP: Remove hash %x -> %x\n",ht_bin[2],ht_bin[3]);
10895 ht_bin[2]=ht_bin[3]=-1;
10897 if((ht_bin[1]>>shift)==(base>>shift) ||
10898 ((ht_bin[1]-MAX_OUTPUT_BLOCK_SIZE)>>shift)==(base>>shift)) {
10899 inv_debug("EXP: Remove hash %x -> %x\n",ht_bin[0],ht_bin[1]);
10900 ht_bin[0]=ht_bin[2];
10901 ht_bin[1]=ht_bin[3];
10902 ht_bin[2]=ht_bin[3]=-1;
10908 #if NEW_DYNAREC == NEW_DYNAREC_ARM
10909 if((expirep&2047)==0)
10912 ll_remove_matching_addrs(jump_out+(expirep&2047),base,shift);
10913 ll_remove_matching_addrs(jump_out+2048+(expirep&2047),base,shift);
10916 expirep=(expirep+1)&65535;
10921 void TLBWI_new(void)
10924 /* Remove old entries */
10925 unsigned int old_start_even=tlb_e[Index&0x3F].start_even;
10926 unsigned int old_end_even=tlb_e[Index&0x3F].end_even;
10927 unsigned int old_start_odd=tlb_e[Index&0x3F].start_odd;
10928 unsigned int old_end_odd=tlb_e[Index&0x3F].end_odd;
10929 for (i=old_start_even>>12; i<=old_end_even>>12; i++)
10931 if(i<0x80000||i>0xBFFFF)
10933 invalidate_block(i);
10937 for (i=old_start_odd>>12; i<=old_end_odd>>12; i++)
10939 if(i<0x80000||i>0xBFFFF)
10941 invalidate_block(i);
10945 cached_interpreter_table.TLBWI();
10946 //DebugMessage(M64MSG_VERBOSE, "TLBWI: index=%d",Index);
10947 //DebugMessage(M64MSG_VERBOSE, "TLBWI: start_even=%x end_even=%x phys_even=%x v=%d d=%d",tlb_e[Index&0x3F].start_even,tlb_e[Index&0x3F].end_even,tlb_e[Index&0x3F].phys_even,tlb_e[Index&0x3F].v_even,tlb_e[Index&0x3F].d_even);
10948 //DebugMessage(M64MSG_VERBOSE, "TLBWI: start_odd=%x end_odd=%x phys_odd=%x v=%d d=%d",tlb_e[Index&0x3F].start_odd,tlb_e[Index&0x3F].end_odd,tlb_e[Index&0x3F].phys_odd,tlb_e[Index&0x3F].v_odd,tlb_e[Index&0x3F].d_odd);
10949 /* Combine tlb_LUT_r, tlb_LUT_w, and invalid_code into a single table
10950 for fast look up. */
10951 for (i=tlb_e[Index&0x3F].start_even>>12; i<=tlb_e[Index&0x3F].end_even>>12; i++)
10953 //DebugMessage(M64MSG_VERBOSE, "%x: r:%8x w:%8x",i,tlb_LUT_r[i],tlb_LUT_w[i]);
10954 if(i<0x80000||i>0xBFFFF)
10957 memory_map[i]=((tlb_LUT_r[i]&0xFFFFF000)-(i<<12)+(unsigned int)rdram-0x80000000)>>2;
10958 // FIXME: should make sure the physical page is invalid too
10959 if(!tlb_LUT_w[i]||!invalid_code[i]) {
10960 memory_map[i]|=0x40000000; // Write protect
10962 assert(tlb_LUT_r[i]==tlb_LUT_w[i]);
10964 if(!using_tlb) DebugMessage(M64MSG_VERBOSE, "Enabled TLB");
10965 // Tell the dynamic recompiler to generate tlb lookup code
10968 else memory_map[i]=-1;
10970 //DebugMessage(M64MSG_VERBOSE, "memory_map[%x]: %8x (+%8x)",i,memory_map[i],memory_map[i]<<2);
10972 for (i=tlb_e[Index&0x3F].start_odd>>12; i<=tlb_e[Index&0x3F].end_odd>>12; i++)
10974 //DebugMessage(M64MSG_VERBOSE, "%x: r:%8x w:%8x",i,tlb_LUT_r[i],tlb_LUT_w[i]);
10975 if(i<0x80000||i>0xBFFFF)
10978 memory_map[i]=((tlb_LUT_r[i]&0xFFFFF000)-(i<<12)+(unsigned int)rdram-0x80000000)>>2;
10979 // FIXME: should make sure the physical page is invalid too
10980 if(!tlb_LUT_w[i]||!invalid_code[i]) {
10981 memory_map[i]|=0x40000000; // Write protect
10983 assert(tlb_LUT_r[i]==tlb_LUT_w[i]);
10985 if(!using_tlb) DebugMessage(M64MSG_VERBOSE, "Enabled TLB");
10986 // Tell the dynamic recompiler to generate tlb lookup code
10989 else memory_map[i]=-1;
10991 //DebugMessage(M64MSG_VERBOSE, "memory_map[%x]: %8x (+%8x)",i,memory_map[i],memory_map[i]<<2);
10995 void TLBWR_new(void)
10998 Random = (Count/2 % (32 - Wired)) + Wired;
10999 /* Remove old entries */
11000 unsigned int old_start_even=tlb_e[Random&0x3F].start_even;
11001 unsigned int old_end_even=tlb_e[Random&0x3F].end_even;
11002 unsigned int old_start_odd=tlb_e[Random&0x3F].start_odd;
11003 unsigned int old_end_odd=tlb_e[Random&0x3F].end_odd;
11004 for (i=old_start_even>>12; i<=old_end_even>>12; i++)
11006 if(i<0x80000||i>0xBFFFF)
11008 invalidate_block(i);
11012 for (i=old_start_odd>>12; i<=old_end_odd>>12; i++)
11014 if(i<0x80000||i>0xBFFFF)
11016 invalidate_block(i);
11020 cached_interpreter_table.TLBWR();
11021 /* Combine tlb_LUT_r, tlb_LUT_w, and invalid_code into a single table
11022 for fast look up. */
11023 for (i=tlb_e[Random&0x3F].start_even>>12; i<=tlb_e[Random&0x3F].end_even>>12; i++)
11025 //DebugMessage(M64MSG_VERBOSE, "%x: r:%8x w:%8x",i,tlb_LUT_r[i],tlb_LUT_w[i]);
11026 if(i<0x80000||i>0xBFFFF)
11029 memory_map[i]=((tlb_LUT_r[i]&0xFFFFF000)-(i<<12)+(unsigned int)rdram-0x80000000)>>2;
11030 // FIXME: should make sure the physical page is invalid too
11031 if(!tlb_LUT_w[i]||!invalid_code[i]) {
11032 memory_map[i]|=0x40000000; // Write protect
11034 assert(tlb_LUT_r[i]==tlb_LUT_w[i]);
11036 if(!using_tlb) DebugMessage(M64MSG_VERBOSE, "Enabled TLB");
11037 // Tell the dynamic recompiler to generate tlb lookup code
11040 else memory_map[i]=-1;
11042 //DebugMessage(M64MSG_VERBOSE, "memory_map[%x]: %8x (+%8x)",i,memory_map[i],memory_map[i]<<2);
11044 for (i=tlb_e[Random&0x3F].start_odd>>12; i<=tlb_e[Random&0x3F].end_odd>>12; i++)
11046 //DebugMessage(M64MSG_VERBOSE, "%x: r:%8x w:%8x",i,tlb_LUT_r[i],tlb_LUT_w[i]);
11047 if(i<0x80000||i>0xBFFFF)
11050 memory_map[i]=((tlb_LUT_r[i]&0xFFFFF000)-(i<<12)+(unsigned int)rdram-0x80000000)>>2;
11051 // FIXME: should make sure the physical page is invalid too
11052 if(!tlb_LUT_w[i]||!invalid_code[i]) {
11053 memory_map[i]|=0x40000000; // Write protect
11055 assert(tlb_LUT_r[i]==tlb_LUT_w[i]);
11057 if(!using_tlb) DebugMessage(M64MSG_VERBOSE, "Enabled TLB");
11058 // Tell the dynamic recompiler to generate tlb lookup code
11061 else memory_map[i]=-1;
11063 //DebugMessage(M64MSG_VERBOSE, "memory_map[%x]: %8x (+%8x)",i,memory_map[i],memory_map[i]<<2);