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
29 #include "../recomp.h"
30 #include "../recomph.h" //include for function prototypes
31 #include "../macros.h"
34 #include "../interupt.h"
35 #include "new_dynarec.h"
37 #include "../../memory/memory.h"
38 #include "../../main/rom.h"
42 #if NEW_DYNAREC == NEW_DYNAREC_X86
43 #include "assem_x86.h"
44 #elif NEW_DYNAREC == NEW_DYNAREC_ARM
45 #include "assem_arm.h"
47 #error Unsupported dynarec architecture
51 #define MAX_OUTPUT_BLOCK_SIZE 262144
52 #define CLOCK_DIVIDER count_per_op
58 signed char regmap_entry[HOST_REGS];
59 signed char regmap[HOST_REGS];
68 uint64_t constmap[HOST_REGS];
76 struct ll_entry *next;
81 static u_int pagelimit;
82 static char insn[MAXBLOCK][10];
83 static u_char itype[MAXBLOCK];
84 static u_char opcode[MAXBLOCK];
85 static u_char opcode2[MAXBLOCK];
86 static u_char bt[MAXBLOCK];
87 static u_char rs1[MAXBLOCK];
88 static u_char rs2[MAXBLOCK];
89 static u_char rt1[MAXBLOCK];
90 static u_char rt2[MAXBLOCK];
91 static u_char us1[MAXBLOCK];
92 static u_char us2[MAXBLOCK];
93 static u_char dep1[MAXBLOCK];
94 static u_char dep2[MAXBLOCK];
95 static u_char lt1[MAXBLOCK];
96 static int imm[MAXBLOCK];
97 static u_int ba[MAXBLOCK];
98 static char likely[MAXBLOCK];
99 static char is_ds[MAXBLOCK];
100 static char ooo[MAXBLOCK];
101 static uint64_t unneeded_reg[MAXBLOCK];
102 static uint64_t unneeded_reg_upper[MAXBLOCK];
103 static uint64_t branch_unneeded_reg[MAXBLOCK];
104 static uint64_t branch_unneeded_reg_upper[MAXBLOCK];
105 static uint64_t p32[MAXBLOCK];
106 static uint64_t pr32[MAXBLOCK];
107 static signed char regmap_pre[MAXBLOCK][HOST_REGS];
109 static signed char regmap[MAXBLOCK][HOST_REGS];
110 static signed char regmap_entry[MAXBLOCK][HOST_REGS];
112 static uint64_t constmap[MAXBLOCK][HOST_REGS];
113 static struct regstat regs[MAXBLOCK];
114 static struct regstat branch_regs[MAXBLOCK];
115 static signed char minimum_free_regs[MAXBLOCK];
116 static u_int needed_reg[MAXBLOCK];
117 static uint64_t requires_32bit[MAXBLOCK];
118 static u_int wont_dirty[MAXBLOCK];
119 static u_int will_dirty[MAXBLOCK];
120 static int ccadj[MAXBLOCK];
122 static u_int instr_addr[MAXBLOCK];
123 static u_int link_addr[MAXBLOCK][3];
124 static int linkcount;
125 static u_int stubs[MAXBLOCK*3][8];
126 static int stubcount;
127 static int literalcount;
128 static int is_delayslot;
129 static int cop1_usable;
131 struct ll_entry *jump_in[4096];
132 static struct ll_entry *jump_out[4096];
133 struct ll_entry *jump_dirty[4096];
134 u_int hash_table[65536][4] __attribute__((aligned(16)));
135 static char shadow[2097152] __attribute__((aligned(16)));
139 static u_int stop_after_jal;
140 extern u_char restore_candidate[512];
141 extern int cycle_count;
143 /* registers that may be allocated */
145 #define HIREG 32 // hi
146 #define LOREG 33 // lo
147 #define FSREG 34 // FPU status (FCSR)
148 #define CSREG 35 // Coprocessor status
149 #define CCREG 36 // Cycle count
150 #define INVCP 37 // Pointer to invalid_code
151 #define MMREG 38 // Pointer to memory_map
152 #define ROREG 39 // ram offset (if rdram!=0x80000000)
154 #define FTEMP 40 // FPU temporary register
155 #define PTEMP 41 // Prefetch temporary register
156 #define TLREG 42 // TLB mapping offset
157 #define RHASH 43 // Return address hash
158 #define RHTBL 44 // Return address hash table address
159 #define RTEMP 45 // JR/JALR address register
161 #define AGEN1 46 // Address generation temporary register
162 #define AGEN2 47 // Address generation temporary register
163 #define MGEN1 48 // Maptable address generation temporary register
164 #define MGEN2 49 // Maptable address generation temporary register
165 #define BTREG 50 // Branch target temporary register
167 /* instruction types */
168 #define NOP 0 // No operation
169 #define LOAD 1 // Load
170 #define STORE 2 // Store
171 #define LOADLR 3 // Unaligned load
172 #define STORELR 4 // Unaligned store
173 #define MOV 5 // Move
174 #define ALU 6 // Arithmetic/logic
175 #define MULTDIV 7 // Multiply/divide
176 #define SHIFT 8 // Shift by register
177 #define SHIFTIMM 9// Shift by immediate
178 #define IMM16 10 // 16-bit immediate
179 #define RJUMP 11 // Unconditional jump to register
180 #define UJUMP 12 // Unconditional jump
181 #define CJUMP 13 // Conditional branch (BEQ/BNE/BGTZ/BLEZ)
182 #define SJUMP 14 // Conditional branch (regimm format)
183 #define COP0 15 // Coprocessor 0
184 #define COP1 16 // Coprocessor 1
185 #define C1LS 17 // Coprocessor 1 load/store
186 #define FJUMP 18 // Conditional branch (floating point)
187 #define FLOAT 19 // Floating point unit
188 #define FCONV 20 // Convert integer to float
189 #define FCOMP 21 // Floating point compare (sets FSREG)
190 #define SYSCALL 22// SYSCALL
191 #define OTHER 23 // Other
192 #define SPAN 24 // Branch/delay slot spans 2 pages
193 #define NI 25 // Not implemented
202 #define LOADBU_STUB 7
203 #define LOADHU_STUB 8
204 #define STOREB_STUB 9
205 #define STOREH_STUB 10
206 #define STOREW_STUB 11
207 #define STORED_STUB 12
208 #define STORELR_STUB 13
209 #define INVCODE_STUB 14
216 /* bug-fix to implement __clear_cache (missing in Android; http://code.google.com/p/android/issues/detail?id=1803) */
217 void __clear_cache_bugfix(char* begin, char *end);
219 #define __clear_cache __clear_cache_bugfix
223 int new_recompile_block(int addr);
224 void *get_addr_ht(u_int vaddr);
225 static void remove_hash(int vaddr);
227 void dyna_linker_ds();
229 void verify_code_vm();
230 void verify_code_ds();
233 void fp_exception_ds();
236 #if NEW_DYNAREC == NEW_DYNAREC_ARM
237 static void invalidate_addr(u_int addr);
243 void read_nomem_new();
244 void read_nomemb_new();
245 void read_nomemh_new();
246 void read_nomemd_new();
247 void write_nomem_new();
248 void write_nomemb_new();
249 void write_nomemh_new();
250 void write_nomemd_new();
251 void write_rdram_new();
252 void write_rdramb_new();
253 void write_rdramh_new();
254 void write_rdramd_new();
255 extern u_int memory_map[1048576];
257 // Needed by assembler
258 static void wb_register(signed char r,signed char regmap[],uint64_t dirty,uint64_t is32);
259 static void wb_dirtys(signed char i_regmap[],uint64_t i_is32,uint64_t i_dirty);
260 static void wb_needed_dirtys(signed char i_regmap[],uint64_t i_is32,uint64_t i_dirty,int addr);
261 static void load_all_regs(signed char i_regmap[]);
262 static void load_needed_regs(signed char i_regmap[],signed char next_regmap[]);
263 static void load_regs_entry(int t);
264 static void load_all_consts(signed char regmap[],int is32,u_int dirty,int i);
266 static void add_stub(int type,int addr,int retaddr,int a,int b,int c,int d,int e);
267 static void add_to_linker(int addr,int target,int ext);
268 static int verify_dirty(void *addr);
270 //static int tracedebug=0;
272 //#define DEBUG_CYCLE_COUNT 1
274 // Uncomment these two lines to generate debug output:
275 #//define ASSEM_DEBUG 1
276 //#define INV_DEBUG 1
278 // Uncomment this line to output the number of NOTCOMPILED blocks as they occur:
279 //#define COUNT_NOTCOMPILEDS 1
281 #if defined (COUNT_NOTCOMPILEDS )
282 int notcompiledCount = 0;
284 static void nullf() {}
286 #if defined( ASSEM_DEBUG )
287 #define assem_debug(...) DebugMessage(M64MSG_VERBOSE, __VA_ARGS__)
289 #define assem_debug nullf
291 #if defined( INV_DEBUG )
292 #define inv_debug(...) DebugMessage(M64MSG_VERBOSE, __VA_ARGS__)
294 #define inv_debug nullf
297 #define log_message(...) DebugMessage(M64MSG_VERBOSE, __VA_ARGS__)
299 static void tlb_hacks()
302 if (strncmp((char *) ROM_HEADER.Name, "GOLDENEYE",9) == 0)
306 switch (ROM_HEADER.Country_code&0xFF)
318 // Unknown country code
322 u_int rom_addr=(u_int)rom;
324 // Since memory_map is 32-bit, on 64-bit systems the rom needs to be
325 // in the lower 4G of memory to use this hack. Copy it if necessary.
326 if((void *)rom>(void *)0xffffffff) {
327 munmap(ROM_COPY, 67108864);
328 if(mmap(ROM_COPY, 12582912,
329 PROT_READ | PROT_WRITE,
330 MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS,
331 -1, 0) <= 0) {DebugMessage(M64MSG_ERROR, "mmap() failed");}
332 memcpy(ROM_COPY,rom,12582912);
333 rom_addr=(u_int)ROM_COPY;
337 for(n=0x7F000;n<0x80000;n++) {
338 memory_map[n]=(((u_int)(rom_addr+addr-0x7F000000))>>2)|0x40000000;
344 // Get address from virtual address
345 // This is called from the recompiled JR/JALR instructions
346 void *get_addr(u_int vaddr)
348 u_int page=(vaddr^0x80000000)>>12;
350 if(page>262143&&tlb_LUT_r[vaddr>>12]) page=(tlb_LUT_r[vaddr>>12]^0x80000000)>>12;
351 if(page>2048) page=2048+(page&2047);
352 if(vpage>262143&&tlb_LUT_r[vaddr>>12]) vpage&=2047; // jump_dirty uses a hash of the virtual address instead
353 if(vpage>2048) vpage=2048+(vpage&2047);
354 struct ll_entry *head;
355 //DebugMessage(M64MSG_VERBOSE, "TRACE: count=%d next=%d (get_addr %x,page %d)",Count,next_interupt,vaddr,page);
358 if(head->vaddr==vaddr&&head->reg32==0) {
359 //DebugMessage(M64MSG_VERBOSE, "TRACE: count=%d next=%d (get_addr match %x: %x)",Count,next_interupt,vaddr,(int)head->addr);
360 u_int *ht_bin=hash_table[((vaddr>>16)^vaddr)&0xFFFF];
363 ht_bin[1]=(int)head->addr;
369 head=jump_dirty[vpage];
371 if(head->vaddr==vaddr&&head->reg32==0) {
372 //DebugMessage(M64MSG_VERBOSE, "TRACE: count=%d next=%d (get_addr match dirty %x: %x)",Count,next_interupt,vaddr,(int)head->addr);
373 // Don't restore blocks which are about to expire from the cache
374 if((((u_int)head->addr-(u_int)out)<<(32-TARGET_SIZE_2))>0x60000000+(MAX_OUTPUT_BLOCK_SIZE<<(32-TARGET_SIZE_2)))
375 if(verify_dirty(head->addr)) {
376 //DebugMessage(M64MSG_VERBOSE, "restore candidate: %x (%d) d=%d",vaddr,page,invalid_code[vaddr>>12]);
377 invalid_code[vaddr>>12]=0;
378 memory_map[vaddr>>12]|=0x40000000;
380 if(tlb_LUT_r[vaddr>>12]) {
381 invalid_code[tlb_LUT_r[vaddr>>12]>>12]=0;
382 memory_map[tlb_LUT_r[vaddr>>12]>>12]|=0x40000000;
384 restore_candidate[vpage>>3]|=1<<(vpage&7);
386 else restore_candidate[page>>3]|=1<<(page&7);
387 u_int *ht_bin=hash_table[((vaddr>>16)^vaddr)&0xFFFF];
388 if(ht_bin[0]==vaddr) {
389 ht_bin[1]=(int)head->addr; // Replace existing entry
395 ht_bin[1]=(int)head->addr;
403 //DebugMessage(M64MSG_VERBOSE, "TRACE: count=%d next=%d (get_addr no-match %x)",Count,next_interupt,vaddr);
404 int r=new_recompile_block(vaddr);
405 if(r==0) return get_addr(vaddr);
406 // Execute in unmapped page, generate pagefault execption
408 Cause=(vaddr<<31)|0x8;
409 EPC=(vaddr&1)?vaddr-5:vaddr;
411 Context=(Context&0xFF80000F)|((BadVAddr>>9)&0x007FFFF0);
412 EntryHi=BadVAddr&0xFFFFE000;
413 return get_addr_ht(0x80000000);
415 // Look up address in hash table first
416 void *get_addr_ht(u_int vaddr)
418 //DebugMessage(M64MSG_VERBOSE, "TRACE: count=%d next=%d (get_addr_ht %x)",Count,next_interupt,vaddr);
419 u_int *ht_bin=hash_table[((vaddr>>16)^vaddr)&0xFFFF];
420 if(ht_bin[0]==vaddr) return (void *)ht_bin[1];
421 if(ht_bin[2]==vaddr) return (void *)ht_bin[3];
422 return get_addr(vaddr);
425 void *get_addr_32(u_int vaddr,u_int flags)
427 //DebugMessage(M64MSG_VERBOSE, "TRACE: count=%d next=%d (get_addr_32 %x,flags %x)",Count,next_interupt,vaddr,flags);
428 u_int *ht_bin=hash_table[((vaddr>>16)^vaddr)&0xFFFF];
429 if(ht_bin[0]==vaddr) return (void *)ht_bin[1];
430 if(ht_bin[2]==vaddr) return (void *)ht_bin[3];
431 u_int page=(vaddr^0x80000000)>>12;
433 if(page>262143&&tlb_LUT_r[vaddr>>12]) page=(tlb_LUT_r[vaddr>>12]^0x80000000)>>12;
434 if(page>2048) page=2048+(page&2047);
435 if(vpage>262143&&tlb_LUT_r[vaddr>>12]) vpage&=2047; // jump_dirty uses a hash of the virtual address instead
436 if(vpage>2048) vpage=2048+(vpage&2047);
437 struct ll_entry *head;
440 if(head->vaddr==vaddr&&(head->reg32&flags)==0) {
441 //DebugMessage(M64MSG_VERBOSE, "TRACE: count=%d next=%d (get_addr_32 match %x: %x)",Count,next_interupt,vaddr,(int)head->addr);
443 u_int *ht_bin=hash_table[((vaddr>>16)^vaddr)&0xFFFF];
445 ht_bin[1]=(int)head->addr;
447 }else if(ht_bin[2]==-1) {
448 ht_bin[3]=(int)head->addr;
451 //ht_bin[3]=ht_bin[1];
452 //ht_bin[2]=ht_bin[0];
453 //ht_bin[1]=(int)head->addr;
460 head=jump_dirty[vpage];
462 if(head->vaddr==vaddr&&(head->reg32&flags)==0) {
463 //DebugMessage(M64MSG_VERBOSE, "TRACE: count=%d next=%d (get_addr_32 match dirty %x: %x)",Count,next_interupt,vaddr,(int)head->addr);
464 // Don't restore blocks which are about to expire from the cache
465 if((((u_int)head->addr-(u_int)out)<<(32-TARGET_SIZE_2))>0x60000000+(MAX_OUTPUT_BLOCK_SIZE<<(32-TARGET_SIZE_2)))
466 if(verify_dirty(head->addr)) {
467 //DebugMessage(M64MSG_VERBOSE, "restore candidate: %x (%d) d=%d",vaddr,page,invalid_code[vaddr>>12]);
468 invalid_code[vaddr>>12]=0;
469 memory_map[vaddr>>12]|=0x40000000;
471 if(tlb_LUT_r[vaddr>>12]) {
472 invalid_code[tlb_LUT_r[vaddr>>12]>>12]=0;
473 memory_map[tlb_LUT_r[vaddr>>12]>>12]|=0x40000000;
475 restore_candidate[vpage>>3]|=1<<(vpage&7);
477 else restore_candidate[page>>3]|=1<<(page&7);
479 u_int *ht_bin=hash_table[((vaddr>>16)^vaddr)&0xFFFF];
481 ht_bin[1]=(int)head->addr;
483 }else if(ht_bin[2]==-1) {
484 ht_bin[3]=(int)head->addr;
487 //ht_bin[3]=ht_bin[1];
488 //ht_bin[2]=ht_bin[0];
489 //ht_bin[1]=(int)head->addr;
497 //DebugMessage(M64MSG_VERBOSE, "TRACE: count=%d next=%d (get_addr_32 no-match %x,flags %x)",Count,next_interupt,vaddr,flags);
498 int r=new_recompile_block(vaddr);
499 if(r==0) return get_addr(vaddr);
500 // Execute in unmapped page, generate pagefault execption
502 Cause=(vaddr<<31)|0x8;
503 EPC=(vaddr&1)?vaddr-5:vaddr;
505 Context=(Context&0xFF80000F)|((BadVAddr>>9)&0x007FFFF0);
506 EntryHi=BadVAddr&0xFFFFE000;
507 return get_addr_ht(0x80000000);
510 static void clear_all_regs(signed char regmap[])
513 for (hr=0;hr<HOST_REGS;hr++) regmap[hr]=-1;
516 static signed char get_reg(signed char regmap[],int r)
519 for (hr=0;hr<HOST_REGS;hr++) if(hr!=EXCLUDE_REG&®map[hr]==r) return hr;
523 // Find a register that is available for two consecutive cycles
524 static signed char get_reg2(signed char regmap1[],signed char regmap2[],int r)
527 for (hr=0;hr<HOST_REGS;hr++) if(hr!=EXCLUDE_REG&®map1[hr]==r&®map2[hr]==r) return hr;
531 static int count_free_regs(signed char regmap[])
535 for(hr=0;hr<HOST_REGS;hr++)
537 if(hr!=EXCLUDE_REG) {
538 if(regmap[hr]<0) count++;
544 static void dirty_reg(struct regstat *cur,signed char reg)
548 for (hr=0;hr<HOST_REGS;hr++) {
549 if((cur->regmap[hr]&63)==reg) {
555 // If we dirty the lower half of a 64 bit register which is now being
556 // sign-extended, we need to dump the upper half.
557 // Note: Do this only after completion of the instruction, because
558 // some instructions may need to read the full 64-bit value even if
559 // overwriting it (eg SLTI, DSRA32).
560 static void flush_dirty_uppers(struct regstat *cur)
563 for (hr=0;hr<HOST_REGS;hr++) {
564 if((cur->dirty>>hr)&1) {
567 if((cur->is32>>(reg&63))&1) cur->regmap[hr]=-1;
572 static void set_const(struct regstat *cur,signed char reg,uint64_t value)
576 for (hr=0;hr<HOST_REGS;hr++) {
577 if(cur->regmap[hr]==reg) {
579 cur->constmap[hr]=value;
581 else if((cur->regmap[hr]^64)==reg) {
583 cur->constmap[hr]=value>>32;
588 static void clear_const(struct regstat *cur,signed char reg)
592 for (hr=0;hr<HOST_REGS;hr++) {
593 if((cur->regmap[hr]&63)==reg) {
594 cur->isconst&=~(1<<hr);
599 static int is_const(struct regstat *cur,signed char reg)
604 for (hr=0;hr<HOST_REGS;hr++) {
605 if((cur->regmap[hr]&63)==reg) {
606 return (cur->isconst>>hr)&1;
611 static uint64_t get_const(struct regstat *cur,signed char reg)
615 for (hr=0;hr<HOST_REGS;hr++) {
616 if(cur->regmap[hr]==reg) {
617 return cur->constmap[hr];
620 DebugMessage(M64MSG_ERROR, "Unknown constant in r%d",reg);
624 // Least soon needed registers
625 // Look at the next ten instructions and see which registers
626 // will be used. Try not to reallocate these.
627 static void lsn(u_char hsn[], int i, int *preferred_reg)
637 if(itype[i+j]==UJUMP||itype[i+j]==RJUMP||(source[i+j]>>16)==0x1000)
639 // Don't go past an unconditonal jump
646 if(rs1[i+j]) hsn[rs1[i+j]]=j;
647 if(rs2[i+j]) hsn[rs2[i+j]]=j;
648 if(rt1[i+j]) hsn[rt1[i+j]]=j;
649 if(rt2[i+j]) hsn[rt2[i+j]]=j;
650 if(itype[i+j]==STORE || itype[i+j]==STORELR) {
651 // Stores can allocate zero
655 // On some architectures stores need invc_ptr
656 #if defined(HOST_IMM8)
657 if(itype[i+j]==STORE || itype[i+j]==STORELR || (opcode[i+j]&0x3b)==0x39) {
661 if(i+j>=0&&(itype[i+j]==UJUMP||itype[i+j]==CJUMP||itype[i+j]==SJUMP||itype[i+j]==FJUMP))
669 if(ba[i+b]>=start && ba[i+b]<(start+slen*4))
671 // Follow first branch
672 int t=(ba[i+b]-start)>>2;
673 j=7-b;if(t+j>=slen) j=slen-t-1;
676 if(rs1[t+j]) if(hsn[rs1[t+j]]>j+b+2) hsn[rs1[t+j]]=j+b+2;
677 if(rs2[t+j]) if(hsn[rs2[t+j]]>j+b+2) hsn[rs2[t+j]]=j+b+2;
678 //if(rt1[t+j]) if(hsn[rt1[t+j]]>j+b+2) hsn[rt1[t+j]]=j+b+2;
679 //if(rt2[t+j]) if(hsn[rt2[t+j]]>j+b+2) hsn[rt2[t+j]]=j+b+2;
682 // TODO: preferred register based on backward branch
684 // Delay slot should preferably not overwrite branch conditions or cycle count
685 if(i>0&&(itype[i-1]==RJUMP||itype[i-1]==UJUMP||itype[i-1]==CJUMP||itype[i-1]==SJUMP||itype[i-1]==FJUMP)) {
686 if(rs1[i-1]) if(hsn[rs1[i-1]]>1) hsn[rs1[i-1]]=1;
687 if(rs2[i-1]) if(hsn[rs2[i-1]]>1) hsn[rs2[i-1]]=1;
693 // Coprocessor load/store needs FTEMP, even if not declared
697 // Load L/R also uses FTEMP as a temporary register
698 if(itype[i]==LOADLR) {
701 // Also 64-bit SDL/SDR
702 if(opcode[i]==0x2c||opcode[i]==0x2d) {
705 // Don't remove the TLB registers either
706 if(itype[i]==LOAD || itype[i]==LOADLR || itype[i]==STORE || itype[i]==STORELR || itype[i]==C1LS ) {
709 // Don't remove the miniht registers
710 if(itype[i]==UJUMP||itype[i]==RJUMP)
717 // We only want to allocate registers if we're going to use them again soon
718 static int needed_again(int r, int i)
724 if(i>0&&(itype[i-1]==UJUMP||itype[i-1]==RJUMP||(source[i-1]>>16)==0x1000))
726 if(ba[i-1]<start || ba[i-1]>start+slen*4-4)
727 return 0; // Don't need any registers if exiting the block
735 if(itype[i+j]==UJUMP||itype[i+j]==RJUMP||(source[i+j]>>16)==0x1000)
737 // Don't go past an unconditonal jump
741 if(itype[i+j]==SYSCALL||((source[i+j]&0xfc00003f)==0x0d))
748 if(rs1[i+j]==r) rn=j;
749 if(rs2[i+j]==r) rn=j;
750 if((unneeded_reg[i+j]>>r)&1) rn=10;
751 if(i+j>=0&&(itype[i+j]==UJUMP||itype[i+j]==CJUMP||itype[i+j]==SJUMP||itype[i+j]==FJUMP))
759 if(ba[i+b]>=start && ba[i+b]<(start+slen*4))
761 // Follow first branch
763 int t=(ba[i+b]-start)>>2;
764 j=7-b;if(t+j>=slen) j=slen-t-1;
767 if(!((unneeded_reg[t+j]>>r)&1)) {
768 if(rs1[t+j]==r) if(rn>j+b+2) rn=j+b+2;
769 if(rs2[t+j]==r) if(rn>j+b+2) rn=j+b+2;
779 // Try to match register allocations at the end of a loop with those
781 static int loop_reg(int i, int r, int hr)
790 if(itype[i+j]==UJUMP||itype[i+j]==RJUMP||(source[i+j]>>16)==0x1000)
792 // Don't go past an unconditonal jump
799 if(itype[i-1]==UJUMP||itype[i-1]==CJUMP||itype[i-1]==SJUMP||itype[i-1]==FJUMP)
804 if(r<64&&((unneeded_reg[i+k]>>r)&1)) return hr;
805 if(r>64&&((unneeded_reg_upper[i+k]>>r)&1)) return hr;
806 if(i+k>=0&&(itype[i+k]==UJUMP||itype[i+k]==CJUMP||itype[i+k]==SJUMP||itype[i+k]==FJUMP))
808 if(ba[i+k]>=start && ba[i+k]<(start+i*4))
810 int t=(ba[i+k]-start)>>2;
811 int reg=get_reg(regs[t].regmap_entry,r);
812 if(reg>=0) return reg;
813 //reg=get_reg(regs[t+1].regmap_entry,r);
814 //if(reg>=0) return reg;
822 // Allocate every register, preserving source/target regs
823 static void alloc_all(struct regstat *cur,int i)
827 for(hr=0;hr<HOST_REGS;hr++) {
828 if(hr!=EXCLUDE_REG) {
829 if(((cur->regmap[hr]&63)!=rs1[i])&&((cur->regmap[hr]&63)!=rs2[i])&&
830 ((cur->regmap[hr]&63)!=rt1[i])&&((cur->regmap[hr]&63)!=rt2[i]))
833 cur->dirty&=~(1<<hr);
836 if((cur->regmap[hr]&63)==0)
839 cur->dirty&=~(1<<hr);
846 static void div64(int64_t dividend,int64_t divisor)
848 if ((dividend) && (divisor)) {
855 //DebugMessage(M64MSG_VERBOSE, "TRACE: ddiv %8x%8x %8x%8x" ,(int)reg[HIREG],(int)(reg[HIREG]>>32)
856 // ,(int)reg[LOREG],(int)(reg[LOREG]>>32));
858 static void divu64(uint64_t dividend,uint64_t divisor)
860 if ((dividend) && (divisor)) {
867 //DebugMessage(M64MSG_VERBOSE, "TRACE: ddivu %8x%8x %8x%8x",(int)reg[HIREG],(int)(reg[HIREG]>>32)
868 // ,(int)reg[LOREG],(int)(reg[LOREG]>>32));
870 static void div32(int32_t dividend,int32_t divisor)
872 if ((dividend) && (divisor)) {
879 //DebugMessage(M64MSG_VERBOSE, "TRACE: ddiv %8x%8x %8x%8x" ,(int)reg[HIREG],(int)(reg[HIREG]>>32)
880 // ,(int)reg[LOREG],(int)(reg[LOREG]>>32));
882 static void divu32(uint32_t dividend,uint32_t divisor)
884 if ((dividend) && (divisor)) {
891 //DebugMessage(M64MSG_VERBOSE, "TRACE: ddivu %8x%8x %8x%8x",(int)reg[HIREG],(int)(reg[HIREG]>>32)
892 // ,(int)reg[LOREG],(int)(reg[LOREG]>>32));
895 static void mult64(int64_t m1,int64_t m2)
897 uint64_t op1, op2, op3, op4;
898 uint64_t result1, result2, result3, result4;
899 uint64_t temp1, temp2, temp3, temp4;
915 op1 = op2 & 0xFFFFFFFF;
916 op2 = (op2 >> 32) & 0xFFFFFFFF;
917 op3 = op4 & 0xFFFFFFFF;
918 op4 = (op4 >> 32) & 0xFFFFFFFF;
921 temp2 = (temp1 >> 32) + op1 * op4;
923 temp4 = (temp3 >> 32) + op2 * op4;
925 result1 = temp1 & 0xFFFFFFFF;
926 result2 = temp2 + (temp3 & 0xFFFFFFFF);
927 result3 = (result2 >> 32) + temp4;
928 result4 = (result3 >> 32);
930 lo = result1 | (result2 << 32);
931 hi = (result3 & 0xFFFFFFFF) | (result4 << 32);
940 #if NEW_DYNAREC == NEW_DYNAREC_ARM
941 static void multu64(uint64_t m1,uint64_t m2)
943 uint64_t op1, op2, op3, op4;
944 uint64_t result1, result2, result3, result4;
945 uint64_t temp1, temp2, temp3, temp4;
947 op1 = m1 & 0xFFFFFFFF;
948 op2 = (m1 >> 32) & 0xFFFFFFFF;
949 op3 = m2 & 0xFFFFFFFF;
950 op4 = (m2 >> 32) & 0xFFFFFFFF;
953 temp2 = (temp1 >> 32) + op1 * op4;
955 temp4 = (temp3 >> 32) + op2 * op4;
957 result1 = temp1 & 0xFFFFFFFF;
958 result2 = temp2 + (temp3 & 0xFFFFFFFF);
959 result3 = (result2 >> 32) + temp4;
960 result4 = (result3 >> 32);
962 lo = result1 | (result2 << 32);
963 hi = (result3 & 0xFFFFFFFF) | (result4 << 32);
965 //DebugMessage(M64MSG_VERBOSE, "TRACE: dmultu %8x%8x %8x%8x",(int)reg[HIREG],(int)(reg[HIREG]>>32)
966 // ,(int)reg[LOREG],(int)(reg[LOREG]>>32));
970 static uint64_t ldl_merge(uint64_t original,uint64_t loaded,u_int bits)
978 else original=loaded;
981 static uint64_t ldr_merge(uint64_t original,uint64_t loaded,u_int bits)
984 original>>=64-(bits^56);
985 original<<=64-(bits^56);
989 else original=loaded;
993 #if NEW_DYNAREC == NEW_DYNAREC_X86
994 #include "assem_x86.c"
995 #elif NEW_DYNAREC == NEW_DYNAREC_ARM
996 #include "assem_arm.c"
998 #error Unsupported dynarec architecture
1001 // Add virtual address mapping to linked list
1002 static void ll_add(struct ll_entry **head,int vaddr,void *addr)
1004 struct ll_entry *new_entry;
1005 new_entry=malloc(sizeof(struct ll_entry));
1006 assert(new_entry!=NULL);
1007 new_entry->vaddr=vaddr;
1009 new_entry->addr=addr;
1010 new_entry->next=*head;
1014 // Add virtual address mapping for 32-bit compiled block
1015 static void ll_add_32(struct ll_entry **head,int vaddr,u_int reg32,void *addr)
1017 struct ll_entry *new_entry;
1018 new_entry=malloc(sizeof(struct ll_entry));
1019 assert(new_entry!=NULL);
1020 new_entry->vaddr=vaddr;
1021 new_entry->reg32=reg32;
1022 new_entry->addr=addr;
1023 new_entry->next=*head;
1027 // Check if an address is already compiled
1028 // but don't return addresses which are about to expire from the cache
1029 static void *check_addr(u_int vaddr)
1031 u_int *ht_bin=hash_table[((vaddr>>16)^vaddr)&0xFFFF];
1032 if(ht_bin[0]==vaddr) {
1033 if(((ht_bin[1]-MAX_OUTPUT_BLOCK_SIZE-(u_int)out)<<(32-TARGET_SIZE_2))>0x60000000+(MAX_OUTPUT_BLOCK_SIZE<<(32-TARGET_SIZE_2)))
1034 if(isclean(ht_bin[1])) return (void *)ht_bin[1];
1036 if(ht_bin[2]==vaddr) {
1037 if(((ht_bin[3]-MAX_OUTPUT_BLOCK_SIZE-(u_int)out)<<(32-TARGET_SIZE_2))>0x60000000+(MAX_OUTPUT_BLOCK_SIZE<<(32-TARGET_SIZE_2)))
1038 if(isclean(ht_bin[3])) return (void *)ht_bin[3];
1040 u_int page=(vaddr^0x80000000)>>12;
1041 if(page>262143&&tlb_LUT_r[vaddr>>12]) page=(tlb_LUT_r[vaddr>>12]^0x80000000)>>12;
1042 if(page>2048) page=2048+(page&2047);
1043 struct ll_entry *head;
1046 if(head->vaddr==vaddr&&head->reg32==0) {
1047 if((((u_int)head->addr-(u_int)out)<<(32-TARGET_SIZE_2))>0x60000000+(MAX_OUTPUT_BLOCK_SIZE<<(32-TARGET_SIZE_2))) {
1048 // Update existing entry with current address
1049 if(ht_bin[0]==vaddr) {
1050 ht_bin[1]=(int)head->addr;
1053 if(ht_bin[2]==vaddr) {
1054 ht_bin[3]=(int)head->addr;
1057 // Insert into hash table with low priority.
1058 // Don't evict existing entries, as they are probably
1059 // addresses that are being accessed frequently.
1061 ht_bin[1]=(int)head->addr;
1063 }else if(ht_bin[2]==-1) {
1064 ht_bin[3]=(int)head->addr;
1075 static void remove_hash(int vaddr)
1077 //DebugMessage(M64MSG_VERBOSE, "remove hash: %x",vaddr);
1078 u_int *ht_bin=hash_table[(((vaddr)>>16)^vaddr)&0xFFFF];
1079 if(ht_bin[2]==vaddr) {
1080 ht_bin[2]=ht_bin[3]=-1;
1082 if(ht_bin[0]==vaddr) {
1083 ht_bin[0]=ht_bin[2];
1084 ht_bin[1]=ht_bin[3];
1085 ht_bin[2]=ht_bin[3]=-1;
1089 static void ll_remove_matching_addrs(struct ll_entry **head,int addr,int shift)
1091 struct ll_entry *next;
1093 if(((u_int)((*head)->addr)>>shift)==(addr>>shift) ||
1094 ((u_int)((*head)->addr-MAX_OUTPUT_BLOCK_SIZE)>>shift)==(addr>>shift))
1096 inv_debug("EXP: Remove pointer to %x (%x)\n",(int)(*head)->addr,(*head)->vaddr);
1097 remove_hash((*head)->vaddr);
1104 head=&((*head)->next);
1109 // Remove all entries from linked list
1110 static void ll_clear(struct ll_entry **head)
1112 struct ll_entry *cur;
1113 struct ll_entry *next;
1124 // Dereference the pointers and remove if it matches
1125 static void ll_kill_pointers(struct ll_entry *head,int addr,int shift)
1128 int ptr=get_pointer(head->addr);
1129 inv_debug("EXP: Lookup pointer to %x at %x (%x)\n",(int)ptr,(int)head->addr,head->vaddr);
1130 if(((ptr>>shift)==(addr>>shift)) ||
1131 (((ptr-MAX_OUTPUT_BLOCK_SIZE)>>shift)==(addr>>shift)))
1133 inv_debug("EXP: Kill pointer at %x (%x)\n",(int)head->addr,head->vaddr);
1134 u_int host_addr=(int)kill_pointer(head->addr);
1135 #if NEW_DYNAREC == NEW_DYNAREC_ARM
1136 needs_clear_cache[(host_addr-(u_int)base_addr)>>17]|=1<<(((host_addr-(u_int)base_addr)>>12)&31);
1143 // This is called when we write to a compiled block (see do_invstub)
1144 static void invalidate_page(u_int page)
1146 struct ll_entry *head;
1147 struct ll_entry *next;
1151 inv_debug("INVALIDATE: %x\n",head->vaddr);
1152 remove_hash(head->vaddr);
1157 head=jump_out[page];
1160 inv_debug("INVALIDATE: kill pointer to %x (%x)\n",head->vaddr,(int)head->addr);
1161 u_int host_addr=(int)kill_pointer(head->addr);
1162 #if NEW_DYNAREC == NEW_DYNAREC_ARM
1163 needs_clear_cache[(host_addr-(u_int)base_addr)>>17]|=1<<(((host_addr-(u_int)base_addr)>>12)&31);
1170 void invalidate_block(u_int block)
1173 page=vpage=block^0x80000;
1174 if(page>262143&&tlb_LUT_r[block]) page=(tlb_LUT_r[block]^0x80000000)>>12;
1175 if(page>2048) page=2048+(page&2047);
1176 if(vpage>262143&&tlb_LUT_r[block]) vpage&=2047; // jump_dirty uses a hash of the virtual address instead
1177 if(vpage>2048) vpage=2048+(vpage&2047);
1178 inv_debug("INVALIDATE: %x (%d)\n",block<<12,page);
1179 //inv_debug("invalid_code[block]=%d\n",invalid_code[block]);
1182 struct ll_entry *head;
1183 head=jump_dirty[vpage];
1184 //DebugMessage(M64MSG_VERBOSE, "page=%d vpage=%d",page,vpage);
1187 if(vpage>2047||(head->vaddr>>12)==block) { // Ignore vaddr hash collision
1188 get_bounds((int)head->addr,&start,&end);
1189 //DebugMessage(M64MSG_VERBOSE, "start: %x end: %x",start,end);
1190 if(page<2048&&start>=0x80000000&&end<0x80800000) {
1191 if(((start-(u_int)rdram)>>12)<=page&&((end-1-(u_int)rdram)>>12)>=page) {
1192 if((((start-(u_int)rdram)>>12)&2047)<first) first=((start-(u_int)rdram)>>12)&2047;
1193 if((((end-1-(u_int)rdram)>>12)&2047)>last) last=((end-1-(u_int)rdram)>>12)&2047;
1196 if(page<2048&&(signed int)start>=(signed int)0xC0000000&&(signed int)end>=(signed int)0xC0000000) {
1197 if(((start+memory_map[start>>12]-(u_int)rdram)>>12)<=page&&((end-1+memory_map[(end-1)>>12]-(u_int)rdram)>>12)>=page) {
1198 if((((start+memory_map[start>>12]-(u_int)rdram)>>12)&2047)<first) first=((start+memory_map[start>>12]-(u_int)rdram)>>12)&2047;
1199 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;
1205 //DebugMessage(M64MSG_VERBOSE, "first=%d last=%d",first,last);
1206 invalidate_page(page);
1207 assert(first+5>page); // NB: this assumes MAXBLOCK<=4096 (4 pages)
1208 assert(last<page+5);
1209 // Invalidate the adjacent pages if a block crosses a 4K boundary
1211 invalidate_page(first);
1214 for(first=page+1;first<last;first++) {
1215 invalidate_page(first);
1217 #if NEW_DYNAREC == NEW_DYNAREC_ARM
1221 // Don't trap writes
1222 invalid_code[block]=1;
1223 // If there is a valid TLB entry for this page, remove write protect
1224 if(tlb_LUT_w[block]) {
1225 assert(tlb_LUT_r[block]==tlb_LUT_w[block]);
1226 // CHECK: Is this right?
1227 memory_map[block]=((tlb_LUT_w[block]&0xFFFFF000)-(block<<12)+(unsigned int)rdram-0x80000000)>>2;
1228 u_int real_block=tlb_LUT_w[block]>>12;
1229 invalid_code[real_block]=1;
1230 if(real_block>=0x80000&&real_block<0x80800) memory_map[real_block]=((u_int)rdram-0x80000000)>>2;
1232 else if(block>=0x80000&&block<0x80800) memory_map[block]=((u_int)rdram-0x80000000)>>2;
1234 memset(mini_ht,-1,sizeof(mini_ht));
1238 #if NEW_DYNAREC == NEW_DYNAREC_ARM
1239 static void invalidate_addr(u_int addr)
1241 invalidate_block(addr>>12);
1245 // This is called when loading a save state.
1246 // Anything could have changed, so invalidate everything.
1247 void invalidate_all_pages()
1250 for(page=0;page<4096;page++)
1251 invalidate_page(page);
1252 for(page=0;page<1048576;page++)
1253 if(!invalid_code[page]) {
1254 restore_candidate[(page&2047)>>3]|=1<<(page&7);
1255 restore_candidate[((page&2047)>>3)+256]|=1<<(page&7);
1257 #if NEW_DYNAREC == NEW_DYNAREC_ARM
1258 __clear_cache((void *)base_addr,(void *)base_addr+(1<<TARGET_SIZE_2));
1259 //cacheflush((void *)base_addr,(void *)base_addr+(1<<TARGET_SIZE_2),0);
1262 memset(mini_ht,-1,sizeof(mini_ht));
1265 for(page=0;page<0x100000;page++) {
1266 if(tlb_LUT_r[page]) {
1267 memory_map[page]=((tlb_LUT_r[page]&0xFFFFF000)-(page<<12)+(unsigned int)rdram-0x80000000)>>2;
1268 if(!tlb_LUT_w[page]||!invalid_code[page])
1269 memory_map[page]|=0x40000000; // Write protect
1271 else memory_map[page]=-1;
1272 if(page==0x80000) page=0xC0000;
1277 // Add an entry to jump_out after making a link
1278 void add_link(u_int vaddr,void *src)
1280 u_int page=(vaddr^0x80000000)>>12;
1281 if(page>262143&&tlb_LUT_r[vaddr>>12]) page=(tlb_LUT_r[vaddr>>12]^0x80000000)>>12;
1282 if(page>4095) page=2048+(page&2047);
1283 inv_debug("add_link: %x -> %x (%d)\n",(int)src,vaddr,page);
1284 ll_add(jump_out+page,vaddr,src);
1285 //int ptr=get_pointer(src);
1286 //inv_debug("add_link: Pointer is to %x\n",(int)ptr);
1289 // If a code block was found to be unmodified (bit was set in
1290 // restore_candidate) and it remains unmodified (bit is clear
1291 // in invalid_code) then move the entries for that 4K page from
1292 // the dirty list to the clean list.
1293 void clean_blocks(u_int page)
1295 struct ll_entry *head;
1296 inv_debug("INV: clean_blocks page=%d\n",page);
1297 head=jump_dirty[page];
1299 if(!invalid_code[head->vaddr>>12]) {
1300 // Don't restore blocks which are about to expire from the cache
1301 if((((u_int)head->addr-(u_int)out)<<(32-TARGET_SIZE_2))>0x60000000+(MAX_OUTPUT_BLOCK_SIZE<<(32-TARGET_SIZE_2))) {
1303 if(verify_dirty(head->addr)) {
1304 //DebugMessage(M64MSG_VERBOSE, "Possibly Restore %x (%x)",head->vaddr, (int)head->addr);
1307 get_bounds((int)head->addr,&start,&end);
1308 if(start-(u_int)rdram<0x800000) {
1309 for(i=(start-(u_int)rdram+0x80000000)>>12;i<=(end-1-(u_int)rdram+0x80000000)>>12;i++) {
1310 inv|=invalid_code[i];
1313 if((signed int)head->vaddr>=(signed int)0xC0000000) {
1314 u_int addr = (head->vaddr+(memory_map[head->vaddr>>12]<<2));
1315 //DebugMessage(M64MSG_VERBOSE, "addr=%x start=%x end=%x",addr,start,end);
1316 if(addr<start||addr>=end) inv=1;
1318 else if((signed int)head->vaddr>=(signed int)0x80800000) {
1322 void * clean_addr=(void *)get_clean_addr((int)head->addr);
1323 if((((u_int)clean_addr-(u_int)out)<<(32-TARGET_SIZE_2))>0x60000000+(MAX_OUTPUT_BLOCK_SIZE<<(32-TARGET_SIZE_2))) {
1325 if(page<2048&&tlb_LUT_r[head->vaddr>>12]) ppage=(tlb_LUT_r[head->vaddr>>12]^0x80000000)>>12;
1326 inv_debug("INV: Restored %x (%x/%x)\n",head->vaddr, (int)head->addr, (int)clean_addr);
1327 //DebugMessage(M64MSG_VERBOSE, "page=%x, addr=%x",page,head->vaddr);
1328 //assert(head->vaddr>>12==(page|0x80000));
1329 ll_add_32(jump_in+ppage,head->vaddr,head->reg32,clean_addr);
1330 u_int *ht_bin=hash_table[((head->vaddr>>16)^head->vaddr)&0xFFFF];
1332 if(ht_bin[0]==head->vaddr) {
1333 ht_bin[1]=(int)clean_addr; // Replace existing entry
1335 if(ht_bin[2]==head->vaddr) {
1336 ht_bin[3]=(int)clean_addr; // Replace existing entry
1349 static void mov_alloc(struct regstat *current,int i)
1351 // Note: Don't need to actually alloc the source registers
1352 if((~current->is32>>rs1[i])&1) {
1353 //alloc_reg64(current,i,rs1[i]);
1354 alloc_reg64(current,i,rt1[i]);
1355 current->is32&=~(1LL<<rt1[i]);
1357 //alloc_reg(current,i,rs1[i]);
1358 alloc_reg(current,i,rt1[i]);
1359 current->is32|=(1LL<<rt1[i]);
1361 clear_const(current,rs1[i]);
1362 clear_const(current,rt1[i]);
1363 dirty_reg(current,rt1[i]);
1366 static void shiftimm_alloc(struct regstat *current,int i)
1368 clear_const(current,rs1[i]);
1369 clear_const(current,rt1[i]);
1370 if(opcode2[i]<=0x3) // SLL/SRL/SRA
1373 if(rs1[i]&&needed_again(rs1[i],i)) alloc_reg(current,i,rs1[i]);
1375 alloc_reg(current,i,rt1[i]);
1376 current->is32|=1LL<<rt1[i];
1377 dirty_reg(current,rt1[i]);
1380 if(opcode2[i]>=0x38&&opcode2[i]<=0x3b) // DSLL/DSRL/DSRA
1383 if(rs1[i]) alloc_reg64(current,i,rs1[i]);
1384 alloc_reg64(current,i,rt1[i]);
1385 current->is32&=~(1LL<<rt1[i]);
1386 dirty_reg(current,rt1[i]);
1389 if(opcode2[i]==0x3c) // DSLL32
1392 if(rs1[i]) alloc_reg(current,i,rs1[i]);
1393 alloc_reg64(current,i,rt1[i]);
1394 current->is32&=~(1LL<<rt1[i]);
1395 dirty_reg(current,rt1[i]);
1398 if(opcode2[i]==0x3e) // DSRL32
1401 alloc_reg64(current,i,rs1[i]);
1403 alloc_reg64(current,i,rt1[i]);
1404 current->is32&=~(1LL<<rt1[i]);
1406 alloc_reg(current,i,rt1[i]);
1407 current->is32|=1LL<<rt1[i];
1409 dirty_reg(current,rt1[i]);
1412 if(opcode2[i]==0x3f) // DSRA32
1415 alloc_reg64(current,i,rs1[i]);
1416 alloc_reg(current,i,rt1[i]);
1417 current->is32|=1LL<<rt1[i];
1418 dirty_reg(current,rt1[i]);
1423 static void shift_alloc(struct regstat *current,int i)
1426 if(opcode2[i]<=0x07) // SLLV/SRLV/SRAV
1428 if(rs1[i]) alloc_reg(current,i,rs1[i]);
1429 if(rs2[i]) alloc_reg(current,i,rs2[i]);
1430 alloc_reg(current,i,rt1[i]);
1431 if(rt1[i]==rs2[i]) {
1432 alloc_reg_temp(current,i,-1);
1433 minimum_free_regs[i]=1;
1435 current->is32|=1LL<<rt1[i];
1436 } else { // DSLLV/DSRLV/DSRAV
1437 if(rs1[i]) alloc_reg64(current,i,rs1[i]);
1438 if(rs2[i]) alloc_reg(current,i,rs2[i]);
1439 alloc_reg64(current,i,rt1[i]);
1440 current->is32&=~(1LL<<rt1[i]);
1441 if(opcode2[i]==0x16||opcode2[i]==0x17) // DSRLV and DSRAV need a temporary register
1443 alloc_reg_temp(current,i,-1);
1444 minimum_free_regs[i]=1;
1447 clear_const(current,rs1[i]);
1448 clear_const(current,rs2[i]);
1449 clear_const(current,rt1[i]);
1450 dirty_reg(current,rt1[i]);
1454 static void alu_alloc(struct regstat *current,int i)
1456 if(opcode2[i]>=0x20&&opcode2[i]<=0x23) { // ADD/ADDU/SUB/SUBU
1458 if(rs1[i]&&rs2[i]) {
1459 alloc_reg(current,i,rs1[i]);
1460 alloc_reg(current,i,rs2[i]);
1463 if(rs1[i]&&needed_again(rs1[i],i)) alloc_reg(current,i,rs1[i]);
1464 if(rs2[i]&&needed_again(rs2[i],i)) alloc_reg(current,i,rs2[i]);
1466 alloc_reg(current,i,rt1[i]);
1468 current->is32|=1LL<<rt1[i];
1470 if(opcode2[i]==0x2a||opcode2[i]==0x2b) { // SLT/SLTU
1472 if(!((current->is32>>rs1[i])&(current->is32>>rs2[i])&1))
1474 alloc_reg64(current,i,rs1[i]);
1475 alloc_reg64(current,i,rs2[i]);
1476 alloc_reg(current,i,rt1[i]);
1478 alloc_reg(current,i,rs1[i]);
1479 alloc_reg(current,i,rs2[i]);
1480 alloc_reg(current,i,rt1[i]);
1483 current->is32|=1LL<<rt1[i];
1485 if(opcode2[i]>=0x24&&opcode2[i]<=0x27) { // AND/OR/XOR/NOR
1487 if(rs1[i]&&rs2[i]) {
1488 alloc_reg(current,i,rs1[i]);
1489 alloc_reg(current,i,rs2[i]);
1493 if(rs1[i]&&needed_again(rs1[i],i)) alloc_reg(current,i,rs1[i]);
1494 if(rs2[i]&&needed_again(rs2[i],i)) alloc_reg(current,i,rs2[i]);
1496 alloc_reg(current,i,rt1[i]);
1497 if(!((current->is32>>rs1[i])&(current->is32>>rs2[i])&1))
1499 if(!((current->uu>>rt1[i])&1)) {
1500 alloc_reg64(current,i,rt1[i]);
1502 if(get_reg(current->regmap,rt1[i]|64)>=0) {
1503 if(rs1[i]&&rs2[i]) {
1504 alloc_reg64(current,i,rs1[i]);
1505 alloc_reg64(current,i,rs2[i]);
1509 // Is is really worth it to keep 64-bit values in registers?
1511 if(rs1[i]&&needed_again(rs1[i],i)) alloc_reg64(current,i,rs1[i]);
1512 if(rs2[i]&&needed_again(rs2[i],i)) alloc_reg64(current,i,rs2[i]);
1516 current->is32&=~(1LL<<rt1[i]);
1518 current->is32|=1LL<<rt1[i];
1522 if(opcode2[i]>=0x2c&&opcode2[i]<=0x2f) { // DADD/DADDU/DSUB/DSUBU
1524 if(rs1[i]&&rs2[i]) {
1525 if(!((current->uu>>rt1[i])&1)||get_reg(current->regmap,rt1[i]|64)>=0) {
1526 alloc_reg64(current,i,rs1[i]);
1527 alloc_reg64(current,i,rs2[i]);
1528 alloc_reg64(current,i,rt1[i]);
1530 alloc_reg(current,i,rs1[i]);
1531 alloc_reg(current,i,rs2[i]);
1532 alloc_reg(current,i,rt1[i]);
1536 alloc_reg(current,i,rt1[i]);
1537 if(!((current->uu>>rt1[i])&1)||get_reg(current->regmap,rt1[i]|64)>=0) {
1538 // DADD used as move, or zeroing
1539 // If we have a 64-bit source, then make the target 64 bits too
1540 if(rs1[i]&&!((current->is32>>rs1[i])&1)) {
1541 if(get_reg(current->regmap,rs1[i])>=0) alloc_reg64(current,i,rs1[i]);
1542 alloc_reg64(current,i,rt1[i]);
1543 } else if(rs2[i]&&!((current->is32>>rs2[i])&1)) {
1544 if(get_reg(current->regmap,rs2[i])>=0) alloc_reg64(current,i,rs2[i]);
1545 alloc_reg64(current,i,rt1[i]);
1547 if(opcode2[i]>=0x2e&&rs2[i]) {
1548 // DSUB used as negation - 64-bit result
1549 // If we have a 32-bit register, extend it to 64 bits
1550 if(get_reg(current->regmap,rs2[i])>=0) alloc_reg64(current,i,rs2[i]);
1551 alloc_reg64(current,i,rt1[i]);
1555 if(rs1[i]&&rs2[i]) {
1556 current->is32&=~(1LL<<rt1[i]);
1558 current->is32&=~(1LL<<rt1[i]);
1559 if((current->is32>>rs1[i])&1)
1560 current->is32|=1LL<<rt1[i];
1562 current->is32&=~(1LL<<rt1[i]);
1563 if((current->is32>>rs2[i])&1)
1564 current->is32|=1LL<<rt1[i];
1566 current->is32|=1LL<<rt1[i];
1570 clear_const(current,rs1[i]);
1571 clear_const(current,rs2[i]);
1572 clear_const(current,rt1[i]);
1573 dirty_reg(current,rt1[i]);
1576 static void imm16_alloc(struct regstat *current,int i)
1578 if(rs1[i]&&needed_again(rs1[i],i)) alloc_reg(current,i,rs1[i]);
1580 if(rt1[i]) alloc_reg(current,i,rt1[i]);
1581 if(opcode[i]==0x18||opcode[i]==0x19) { // DADDI/DADDIU
1582 current->is32&=~(1LL<<rt1[i]);
1583 if(!((current->uu>>rt1[i])&1)||get_reg(current->regmap,rt1[i]|64)>=0) {
1584 // TODO: Could preserve the 32-bit flag if the immediate is zero
1585 alloc_reg64(current,i,rt1[i]);
1586 alloc_reg64(current,i,rs1[i]);
1588 clear_const(current,rs1[i]);
1589 clear_const(current,rt1[i]);
1591 else if(opcode[i]==0x0a||opcode[i]==0x0b) { // SLTI/SLTIU
1592 if((~current->is32>>rs1[i])&1) alloc_reg64(current,i,rs1[i]);
1593 current->is32|=1LL<<rt1[i];
1594 clear_const(current,rs1[i]);
1595 clear_const(current,rt1[i]);
1597 else if(opcode[i]>=0x0c&&opcode[i]<=0x0e) { // ANDI/ORI/XORI
1598 if(((~current->is32>>rs1[i])&1)&&opcode[i]>0x0c) {
1599 if(rs1[i]!=rt1[i]) {
1600 if(needed_again(rs1[i],i)) alloc_reg64(current,i,rs1[i]);
1601 alloc_reg64(current,i,rt1[i]);
1602 current->is32&=~(1LL<<rt1[i]);
1605 else current->is32|=1LL<<rt1[i]; // ANDI clears upper bits
1606 if(is_const(current,rs1[i])) {
1607 int v=get_const(current,rs1[i]);
1608 if(opcode[i]==0x0c) set_const(current,rt1[i],v&imm[i]);
1609 if(opcode[i]==0x0d) set_const(current,rt1[i],v|imm[i]);
1610 if(opcode[i]==0x0e) set_const(current,rt1[i],v^imm[i]);
1612 else clear_const(current,rt1[i]);
1614 else if(opcode[i]==0x08||opcode[i]==0x09) { // ADDI/ADDIU
1615 if(is_const(current,rs1[i])) {
1616 int v=get_const(current,rs1[i]);
1617 set_const(current,rt1[i],v+imm[i]);
1619 else clear_const(current,rt1[i]);
1620 current->is32|=1LL<<rt1[i];
1623 set_const(current,rt1[i],((long long)((short)imm[i]))<<16); // LUI
1624 current->is32|=1LL<<rt1[i];
1626 dirty_reg(current,rt1[i]);
1629 static void load_alloc(struct regstat *current,int i)
1631 clear_const(current,rt1[i]);
1632 //if(rs1[i]!=rt1[i]&&needed_again(rs1[i],i)) clear_const(current,rs1[i]); // Does this help or hurt?
1633 if(!rs1[i]) current->u&=~1LL; // Allow allocating r0 if it's the source register
1634 if(needed_again(rs1[i],i)) alloc_reg(current,i,rs1[i]);
1635 if(rt1[i]&&!((current->u>>rt1[i])&1)) {
1636 alloc_reg(current,i,rt1[i]);
1637 assert(get_reg(current->regmap,rt1[i])>=0);
1638 if(opcode[i]==0x27||opcode[i]==0x37) // LWU/LD
1640 current->is32&=~(1LL<<rt1[i]);
1641 alloc_reg64(current,i,rt1[i]);
1643 else if(opcode[i]==0x1A||opcode[i]==0x1B) // LDL/LDR
1645 current->is32&=~(1LL<<rt1[i]);
1646 alloc_reg64(current,i,rt1[i]);
1647 alloc_all(current,i);
1648 alloc_reg64(current,i,FTEMP);
1649 minimum_free_regs[i]=HOST_REGS;
1651 else current->is32|=1LL<<rt1[i];
1652 dirty_reg(current,rt1[i]);
1653 // If using TLB, need a register for pointer to the mapping table
1654 if(using_tlb) alloc_reg(current,i,TLREG);
1655 // LWL/LWR need a temporary register for the old value
1656 if(opcode[i]==0x22||opcode[i]==0x26)
1658 alloc_reg(current,i,FTEMP);
1659 alloc_reg_temp(current,i,-1);
1660 minimum_free_regs[i]=1;
1665 // Load to r0 or unneeded register (dummy load)
1666 // but we still need a register to calculate the address
1667 if(opcode[i]==0x22||opcode[i]==0x26)
1669 alloc_reg(current,i,FTEMP); // LWL/LWR need another temporary
1671 // If using TLB, need a register for pointer to the mapping table
1672 if(using_tlb) alloc_reg(current,i,TLREG);
1673 alloc_reg_temp(current,i,-1);
1674 minimum_free_regs[i]=1;
1675 if(opcode[i]==0x1A||opcode[i]==0x1B) // LDL/LDR
1677 alloc_all(current,i);
1678 alloc_reg64(current,i,FTEMP);
1679 minimum_free_regs[i]=HOST_REGS;
1684 static void store_alloc(struct regstat *current,int i)
1686 clear_const(current,rs2[i]);
1687 if(!(rs2[i])) current->u&=~1LL; // Allow allocating r0 if necessary
1688 if(needed_again(rs1[i],i)) alloc_reg(current,i,rs1[i]);
1689 alloc_reg(current,i,rs2[i]);
1690 if(opcode[i]==0x2c||opcode[i]==0x2d||opcode[i]==0x3f) { // 64-bit SDL/SDR/SD
1691 alloc_reg64(current,i,rs2[i]);
1692 if(rs2[i]) alloc_reg(current,i,FTEMP);
1694 // If using TLB, need a register for pointer to the mapping table
1695 if(using_tlb) alloc_reg(current,i,TLREG);
1696 #if defined(HOST_IMM8)
1697 // On CPUs without 32-bit immediates we need a pointer to invalid_code
1698 else alloc_reg(current,i,INVCP);
1700 if(opcode[i]==0x2c||opcode[i]==0x2d) { // 64-bit SDL/SDR
1701 alloc_reg(current,i,FTEMP);
1703 // We need a temporary register for address generation
1704 alloc_reg_temp(current,i,-1);
1705 minimum_free_regs[i]=1;
1708 static void c1ls_alloc(struct regstat *current,int i)
1710 //clear_const(current,rs1[i]); // FIXME
1711 clear_const(current,rt1[i]);
1712 if(needed_again(rs1[i],i)) alloc_reg(current,i,rs1[i]);
1713 alloc_reg(current,i,CSREG); // Status
1714 alloc_reg(current,i,FTEMP);
1715 if(opcode[i]==0x35||opcode[i]==0x3d) { // 64-bit LDC1/SDC1
1716 alloc_reg64(current,i,FTEMP);
1718 // If using TLB, need a register for pointer to the mapping table
1719 if(using_tlb) alloc_reg(current,i,TLREG);
1720 #if defined(HOST_IMM8)
1721 // On CPUs without 32-bit immediates we need a pointer to invalid_code
1722 else if((opcode[i]&0x3b)==0x39) // SWC1/SDC1
1723 alloc_reg(current,i,INVCP);
1725 // We need a temporary register for address generation
1726 alloc_reg_temp(current,i,-1);
1727 minimum_free_regs[i]=1;
1730 #ifndef multdiv_alloc
1731 void multdiv_alloc(struct regstat *current,int i)
1738 // case 0x1D: DMULTU
1741 clear_const(current,rs1[i]);
1742 clear_const(current,rs2[i]);
1745 if((opcode2[i]&4)==0) // 32-bit
1747 current->u&=~(1LL<<HIREG);
1748 current->u&=~(1LL<<LOREG);
1749 alloc_reg(current,i,HIREG);
1750 alloc_reg(current,i,LOREG);
1751 alloc_reg(current,i,rs1[i]);
1752 alloc_reg(current,i,rs2[i]);
1753 current->is32|=1LL<<HIREG;
1754 current->is32|=1LL<<LOREG;
1755 dirty_reg(current,HIREG);
1756 dirty_reg(current,LOREG);
1760 current->u&=~(1LL<<HIREG);
1761 current->u&=~(1LL<<LOREG);
1762 current->uu&=~(1LL<<HIREG);
1763 current->uu&=~(1LL<<LOREG);
1764 alloc_reg64(current,i,HIREG);
1765 alloc_reg64(current,i,LOREG);
1766 //if(HOST_REGS>10) alloc_reg64(current,i,LOREG); //*SEB* Why commenting this line? uncommenting make SM64 freeze after title (before mario head and spinning stars)
1767 alloc_reg64(current,i,rs1[i]);
1768 alloc_reg64(current,i,rs2[i]);
1769 alloc_all(current,i);
1770 current->is32&=~(1LL<<HIREG);
1771 current->is32&=~(1LL<<LOREG);
1772 dirty_reg(current,HIREG);
1773 dirty_reg(current,LOREG);
1774 minimum_free_regs[i]=HOST_REGS;
1779 // Multiply by zero is zero.
1780 // MIPS does not have a divide by zero exception.
1781 // The result is undefined, we return zero.
1782 if((opcode2[i]&4)==0) // 32-bit
1784 alloc_reg(current,i,HIREG);
1785 alloc_reg(current,i,LOREG);
1786 current->is32|=1LL<<HIREG;
1787 current->is32|=1LL<<LOREG;
1789 alloc_reg64(current,i,HIREG);
1790 alloc_reg64(current,i,LOREG);
1791 current->is32&=~(1LL<<HIREG);
1792 current->is32&=~(1LL<<LOREG);
1794 dirty_reg(current,HIREG);
1795 dirty_reg(current,LOREG);
1800 static void cop0_alloc(struct regstat *current,int i)
1802 if(opcode2[i]==0) // MFC0
1805 clear_const(current,rt1[i]);
1806 alloc_all(current,i);
1807 alloc_reg(current,i,rt1[i]);
1808 current->is32|=1LL<<rt1[i];
1809 dirty_reg(current,rt1[i]);
1812 else if(opcode2[i]==4) // MTC0
1815 clear_const(current,rs1[i]);
1816 alloc_reg(current,i,rs1[i]);
1817 alloc_all(current,i);
1820 alloc_all(current,i); // FIXME: Keep r0
1822 alloc_reg(current,i,0);
1827 // TLBR/TLBWI/TLBWR/TLBP/ERET
1828 assert(opcode2[i]==0x10);
1829 alloc_all(current,i);
1831 minimum_free_regs[i]=HOST_REGS;
1834 static void cop1_alloc(struct regstat *current,int i)
1836 alloc_reg(current,i,CSREG); // Load status
1837 if(opcode2[i]<3) // MFC1/DMFC1/CFC1
1840 clear_const(current,rt1[i]);
1842 alloc_reg64(current,i,rt1[i]); // DMFC1
1843 current->is32&=~(1LL<<rt1[i]);
1845 alloc_reg(current,i,rt1[i]); // MFC1/CFC1
1846 current->is32|=1LL<<rt1[i];
1848 dirty_reg(current,rt1[i]);
1849 alloc_reg_temp(current,i,-1);
1851 else if(opcode2[i]>3) // MTC1/DMTC1/CTC1
1854 clear_const(current,rs1[i]);
1856 alloc_reg64(current,i,rs1[i]); // DMTC1
1858 alloc_reg(current,i,rs1[i]); // MTC1/CTC1
1859 alloc_reg_temp(current,i,-1);
1863 alloc_reg(current,i,0);
1864 alloc_reg_temp(current,i,-1);
1867 minimum_free_regs[i]=1;
1869 static void fconv_alloc(struct regstat *current,int i)
1871 alloc_reg(current,i,CSREG); // Load status
1872 alloc_reg_temp(current,i,-1);
1873 minimum_free_regs[i]=1;
1875 static void float_alloc(struct regstat *current,int i)
1877 alloc_reg(current,i,CSREG); // Load status
1878 alloc_reg_temp(current,i,-1);
1879 minimum_free_regs[i]=1;
1881 static void fcomp_alloc(struct regstat *current,int i)
1883 alloc_reg(current,i,CSREG); // Load status
1884 alloc_reg(current,i,FSREG); // Load flags
1885 dirty_reg(current,FSREG); // Flag will be modified
1886 alloc_reg_temp(current,i,-1);
1887 minimum_free_regs[i]=1;
1890 static void syscall_alloc(struct regstat *current,int i)
1892 alloc_cc(current,i);
1893 dirty_reg(current,CCREG);
1894 alloc_all(current,i);
1895 minimum_free_regs[i]=HOST_REGS;
1899 static void delayslot_alloc(struct regstat *current,int i)
1909 assem_debug("jump in the delay slot. this shouldn't happen.");//exit(1);
1910 DebugMessage(M64MSG_VERBOSE, "Disabled speculative precompilation");
1914 imm16_alloc(current,i);
1918 load_alloc(current,i);
1922 store_alloc(current,i);
1925 alu_alloc(current,i);
1928 shift_alloc(current,i);
1931 multdiv_alloc(current,i);
1934 shiftimm_alloc(current,i);
1937 mov_alloc(current,i);
1940 cop0_alloc(current,i);
1943 cop1_alloc(current,i);
1946 c1ls_alloc(current,i);
1949 fconv_alloc(current,i);
1952 float_alloc(current,i);
1955 fcomp_alloc(current,i);
1960 // Special case where a branch and delay slot span two pages in virtual memory
1961 static void pagespan_alloc(struct regstat *current,int i)
1964 current->wasconst=0;
1966 minimum_free_regs[i]=HOST_REGS;
1967 alloc_all(current,i);
1968 alloc_cc(current,i);
1969 dirty_reg(current,CCREG);
1970 if(opcode[i]==3) // JAL
1972 alloc_reg(current,i,31);
1973 dirty_reg(current,31);
1975 if(opcode[i]==0&&(opcode2[i]&0x3E)==8) // JR/JALR
1977 alloc_reg(current,i,rs1[i]);
1979 alloc_reg(current,i,rt1[i]);
1980 dirty_reg(current,rt1[i]);
1983 if((opcode[i]&0x2E)==4) // BEQ/BNE/BEQL/BNEL
1985 if(rs1[i]) alloc_reg(current,i,rs1[i]);
1986 if(rs2[i]) alloc_reg(current,i,rs2[i]);
1987 if(!((current->is32>>rs1[i])&(current->is32>>rs2[i])&1))
1989 if(rs1[i]) alloc_reg64(current,i,rs1[i]);
1990 if(rs2[i]) alloc_reg64(current,i,rs2[i]);
1994 if((opcode[i]&0x2E)==6) // BLEZ/BGTZ/BLEZL/BGTZL
1996 if(rs1[i]) alloc_reg(current,i,rs1[i]);
1997 if(!((current->is32>>rs1[i])&1))
1999 if(rs1[i]) alloc_reg64(current,i,rs1[i]);
2003 if(opcode[i]==0x11) // BC1
2005 alloc_reg(current,i,FSREG);
2006 alloc_reg(current,i,CSREG);
2011 static void add_stub(int type,int addr,int retaddr,int a,int b,int c,int d,int e)
2013 stubs[stubcount][0]=type;
2014 stubs[stubcount][1]=addr;
2015 stubs[stubcount][2]=retaddr;
2016 stubs[stubcount][3]=a;
2017 stubs[stubcount][4]=b;
2018 stubs[stubcount][5]=c;
2019 stubs[stubcount][6]=d;
2020 stubs[stubcount][7]=e;
2024 // Write out a single register
2025 static void wb_register(signed char r,signed char regmap[],uint64_t dirty,uint64_t is32)
2028 for(hr=0;hr<HOST_REGS;hr++) {
2029 if(hr!=EXCLUDE_REG) {
2030 if((regmap[hr]&63)==r) {
2033 emit_storereg(r,hr);
2034 if((is32>>regmap[hr])&1) {
2035 emit_sarimm(hr,31,hr);
2036 emit_storereg(r|64,hr);
2039 emit_storereg(r|64,hr);
2047 static int mchecksum()
2049 //if(!tracedebug) return 0;
2052 for(i=0;i<2097152;i++) {
2053 unsigned int temp=sum;
2056 sum^=((u_int *)rdram)[i];
2061 static int rchecksum()
2066 sum^=((u_int *)reg)[i];
2073 DebugMessage(M64MSG_VERBOSE, "TRACE: ");
2075 DebugMessage(M64MSG_VERBOSE, "r%d:%8x%8x ",i,((int *)(reg+i))[1],((int *)(reg+i))[0]);
2076 DebugMessage(M64MSG_VERBOSE, "TRACE: ");
2078 DebugMessage(M64MSG_VERBOSE, "f%d:%8x%8x ",i,((int*)reg_cop1_simple[i])[1],*((int*)reg_cop1_simple[i]));
2081 static void enabletrace()
2087 static void memdebug(int i)
2089 //DebugMessage(M64MSG_VERBOSE, "TRACE: count=%d next=%d (checksum %x) lo=%8x%8x",Count,next_interupt,mchecksum(),(int)(reg[LOREG]>>32),(int)reg[LOREG]);
2090 //DebugMessage(M64MSG_VERBOSE, "TRACE: count=%d next=%d (rchecksum %x)",Count,next_interupt,rchecksum());
2093 //if(Count>=-2084597794) {
2094 if((signed int)Count>=-2084597794&&(signed int)Count<0) {
2096 DebugMessage(M64MSG_VERBOSE, "TRACE: count=%d next=%d (checksum %x)",Count,next_interupt,mchecksum());
2097 //DebugMessage(M64MSG_VERBOSE, "TRACE: count=%d next=%d (checksum %x) Status=%x",Count,next_interupt,mchecksum(),Status);
2098 //DebugMessage(M64MSG_VERBOSE, "TRACE: count=%d next=%d (checksum %x) hi=%8x%8x",Count,next_interupt,mchecksum(),(int)(reg[HIREG]>>32),(int)reg[HIREG]);
2100 #if NEW_DYNAREC == NEW_DYNAREC_X86
2101 DebugMessage(M64MSG_VERBOSE, "TRACE: %x",(&i)[-1]);
2103 #if NEW_DYNAREC == NEW_DYNAREC_ARM
2105 DebugMessage(M64MSG_VERBOSE, "TRACE: %x ",(&j)[10]);
2106 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]);
2110 //DebugMessage(M64MSG_VERBOSE, "TRACE: %x",(&i)[-1]);
2115 static void tlb_debug(u_int cause, u_int addr, u_int iaddr)
2117 DebugMessage(M64MSG_VERBOSE, "TLB Exception: instruction=%x addr=%x cause=%x",iaddr, addr, cause);
2121 static void alu_assemble(int i,struct regstat *i_regs)
2123 if(opcode2[i]>=0x20&&opcode2[i]<=0x23) { // ADD/ADDU/SUB/SUBU
2125 signed char s1,s2,t;
2126 t=get_reg(i_regs->regmap,rt1[i]);
2128 s1=get_reg(i_regs->regmap,rs1[i]);
2129 s2=get_reg(i_regs->regmap,rs2[i]);
2130 if(rs1[i]&&rs2[i]) {
2133 if(opcode2[i]&2) emit_sub(s1,s2,t);
2134 else emit_add(s1,s2,t);
2137 if(s1>=0) emit_mov(s1,t);
2138 else emit_loadreg(rs1[i],t);
2142 if(opcode2[i]&2) emit_neg(s2,t);
2143 else emit_mov(s2,t);
2146 emit_loadreg(rs2[i],t);
2147 if(opcode2[i]&2) emit_neg(t,t);
2150 else emit_zeroreg(t);
2154 if(opcode2[i]>=0x2c&&opcode2[i]<=0x2f) { // DADD/DADDU/DSUB/DSUBU
2156 signed char s1l,s2l,s1h,s2h,tl,th;
2157 tl=get_reg(i_regs->regmap,rt1[i]);
2158 th=get_reg(i_regs->regmap,rt1[i]|64);
2160 s1l=get_reg(i_regs->regmap,rs1[i]);
2161 s2l=get_reg(i_regs->regmap,rs2[i]);
2162 s1h=get_reg(i_regs->regmap,rs1[i]|64);
2163 s2h=get_reg(i_regs->regmap,rs2[i]|64);
2164 if(rs1[i]&&rs2[i]) {
2167 if(opcode2[i]&2) emit_subs(s1l,s2l,tl);
2168 else emit_adds(s1l,s2l,tl);
2170 #ifdef INVERTED_CARRY
2171 if(opcode2[i]&2) {if(s1h!=th) emit_mov(s1h,th);emit_sbb(th,s2h);}
2173 if(opcode2[i]&2) emit_sbc(s1h,s2h,th);
2175 else emit_add(s1h,s2h,th);
2179 if(s1l>=0) emit_mov(s1l,tl);
2180 else emit_loadreg(rs1[i],tl);
2182 if(s1h>=0) emit_mov(s1h,th);
2183 else emit_loadreg(rs1[i]|64,th);
2188 if(opcode2[i]&2) emit_negs(s2l,tl);
2189 else emit_mov(s2l,tl);
2192 emit_loadreg(rs2[i],tl);
2193 if(opcode2[i]&2) emit_negs(tl,tl);
2196 #ifdef INVERTED_CARRY
2197 if(s2h>=0) emit_mov(s2h,th);
2198 else emit_loadreg(rs2[i]|64,th);
2200 emit_adcimm(-1,th); // x86 has inverted carry flag
2205 if(s2h>=0) emit_rscimm(s2h,0,th);
2207 emit_loadreg(rs2[i]|64,th);
2208 emit_rscimm(th,0,th);
2211 if(s2h>=0) emit_mov(s2h,th);
2212 else emit_loadreg(rs2[i]|64,th);
2219 if(th>=0) emit_zeroreg(th);
2224 if(opcode2[i]==0x2a||opcode2[i]==0x2b) { // SLT/SLTU
2226 signed char s1l,s1h,s2l,s2h,t;
2227 if(!((i_regs->was32>>rs1[i])&(i_regs->was32>>rs2[i])&1))
2229 t=get_reg(i_regs->regmap,rt1[i]);
2232 s1l=get_reg(i_regs->regmap,rs1[i]);
2233 s1h=get_reg(i_regs->regmap,rs1[i]|64);
2234 s2l=get_reg(i_regs->regmap,rs2[i]);
2235 s2h=get_reg(i_regs->regmap,rs2[i]|64);
2236 if(rs2[i]==0) // rx<r0
2239 if(opcode2[i]==0x2a) // SLT
2240 emit_shrimm(s1h,31,t);
2241 else // SLTU (unsigned can not be less than zero)
2244 else if(rs1[i]==0) // r0<rx
2247 if(opcode2[i]==0x2a) // SLT
2248 emit_set_gz64_32(s2h,s2l,t);
2249 else // SLTU (set if not zero)
2250 emit_set_nz64_32(s2h,s2l,t);
2253 assert(s1l>=0);assert(s1h>=0);
2254 assert(s2l>=0);assert(s2h>=0);
2255 if(opcode2[i]==0x2a) // SLT
2256 emit_set_if_less64_32(s1h,s1l,s2h,s2l,t);
2258 emit_set_if_carry64_32(s1h,s1l,s2h,s2l,t);
2262 t=get_reg(i_regs->regmap,rt1[i]);
2265 s1l=get_reg(i_regs->regmap,rs1[i]);
2266 s2l=get_reg(i_regs->regmap,rs2[i]);
2267 if(rs2[i]==0) // rx<r0
2270 if(opcode2[i]==0x2a) // SLT
2271 emit_shrimm(s1l,31,t);
2272 else // SLTU (unsigned can not be less than zero)
2275 else if(rs1[i]==0) // r0<rx
2278 if(opcode2[i]==0x2a) // SLT
2279 emit_set_gz32(s2l,t);
2280 else // SLTU (set if not zero)
2281 emit_set_nz32(s2l,t);
2284 assert(s1l>=0);assert(s2l>=0);
2285 if(opcode2[i]==0x2a) // SLT
2286 emit_set_if_less32(s1l,s2l,t);
2288 emit_set_if_carry32(s1l,s2l,t);
2294 if(opcode2[i]>=0x24&&opcode2[i]<=0x27) { // AND/OR/XOR/NOR
2296 signed char s1l,s1h,s2l,s2h,th,tl;
2297 tl=get_reg(i_regs->regmap,rt1[i]);
2298 th=get_reg(i_regs->regmap,rt1[i]|64);
2299 if(!((i_regs->was32>>rs1[i])&(i_regs->was32>>rs2[i])&1)&&th>=0)
2303 s1l=get_reg(i_regs->regmap,rs1[i]);
2304 s1h=get_reg(i_regs->regmap,rs1[i]|64);
2305 s2l=get_reg(i_regs->regmap,rs2[i]);
2306 s2h=get_reg(i_regs->regmap,rs2[i]|64);
2307 if(rs1[i]&&rs2[i]) {
2308 assert(s1l>=0);assert(s1h>=0);
2309 assert(s2l>=0);assert(s2h>=0);
2310 if(opcode2[i]==0x24) { // AND
2311 emit_and(s1l,s2l,tl);
2312 emit_and(s1h,s2h,th);
2314 if(opcode2[i]==0x25) { // OR
2315 emit_or(s1l,s2l,tl);
2316 emit_or(s1h,s2h,th);
2318 if(opcode2[i]==0x26) { // XOR
2319 emit_xor(s1l,s2l,tl);
2320 emit_xor(s1h,s2h,th);
2322 if(opcode2[i]==0x27) { // NOR
2323 emit_or(s1l,s2l,tl);
2324 emit_or(s1h,s2h,th);
2331 if(opcode2[i]==0x24) { // AND
2335 if(opcode2[i]==0x25||opcode2[i]==0x26) { // OR/XOR
2337 if(s1l>=0) emit_mov(s1l,tl);
2338 else emit_loadreg(rs1[i],tl);
2339 if(s1h>=0) emit_mov(s1h,th);
2340 else emit_loadreg(rs1[i]|64,th);
2344 if(s2l>=0) emit_mov(s2l,tl);
2345 else emit_loadreg(rs2[i],tl);
2346 if(s2h>=0) emit_mov(s2h,th);
2347 else emit_loadreg(rs2[i]|64,th);
2354 if(opcode2[i]==0x27) { // NOR
2356 if(s1l>=0) emit_not(s1l,tl);
2358 emit_loadreg(rs1[i],tl);
2361 if(s1h>=0) emit_not(s1h,th);
2363 emit_loadreg(rs1[i]|64,th);
2369 if(s2l>=0) emit_not(s2l,tl);
2371 emit_loadreg(rs2[i],tl);
2374 if(s2h>=0) emit_not(s2h,th);
2376 emit_loadreg(rs2[i]|64,th);
2392 s1l=get_reg(i_regs->regmap,rs1[i]);
2393 s2l=get_reg(i_regs->regmap,rs2[i]);
2394 if(rs1[i]&&rs2[i]) {
2397 if(opcode2[i]==0x24) { // AND
2398 emit_and(s1l,s2l,tl);
2400 if(opcode2[i]==0x25) { // OR
2401 emit_or(s1l,s2l,tl);
2403 if(opcode2[i]==0x26) { // XOR
2404 emit_xor(s1l,s2l,tl);
2406 if(opcode2[i]==0x27) { // NOR
2407 emit_or(s1l,s2l,tl);
2413 if(opcode2[i]==0x24) { // AND
2416 if(opcode2[i]==0x25||opcode2[i]==0x26) { // OR/XOR
2418 if(s1l>=0) emit_mov(s1l,tl);
2419 else emit_loadreg(rs1[i],tl); // CHECK: regmap_entry?
2423 if(s2l>=0) emit_mov(s2l,tl);
2424 else emit_loadreg(rs2[i],tl); // CHECK: regmap_entry?
2426 else emit_zeroreg(tl);
2428 if(opcode2[i]==0x27) { // NOR
2430 if(s1l>=0) emit_not(s1l,tl);
2432 emit_loadreg(rs1[i],tl);
2438 if(s2l>=0) emit_not(s2l,tl);
2440 emit_loadreg(rs2[i],tl);
2444 else emit_movimm(-1,tl);
2453 static void imm16_assemble(int i,struct regstat *i_regs)
2455 if (opcode[i]==0x0f) { // LUI
2458 t=get_reg(i_regs->regmap,rt1[i]);
2461 if(!((i_regs->isconst>>t)&1))
2462 emit_movimm(imm[i]<<16,t);
2466 if(opcode[i]==0x08||opcode[i]==0x09) { // ADDI/ADDIU
2469 t=get_reg(i_regs->regmap,rt1[i]);
2470 s=get_reg(i_regs->regmap,rs1[i]);
2475 if(!((i_regs->isconst>>t)&1)) {
2477 if(i_regs->regmap_entry[t]!=rs1[i]) emit_loadreg(rs1[i],t);
2478 emit_addimm(t,imm[i],t);
2480 if(!((i_regs->wasconst>>s)&1))
2481 emit_addimm(s,imm[i],t);
2483 emit_movimm(constmap[i][s]+imm[i],t);
2489 if(!((i_regs->isconst>>t)&1))
2490 emit_movimm(imm[i],t);
2495 if(opcode[i]==0x18||opcode[i]==0x19) { // DADDI/DADDIU
2497 signed char sh,sl,th,tl;
2498 th=get_reg(i_regs->regmap,rt1[i]|64);
2499 tl=get_reg(i_regs->regmap,rt1[i]);
2500 sh=get_reg(i_regs->regmap,rs1[i]|64);
2501 sl=get_reg(i_regs->regmap,rs1[i]);
2507 emit_addimm64_32(sh,sl,imm[i],th,tl);
2510 emit_addimm(sl,imm[i],tl);
2513 emit_movimm(imm[i],tl);
2514 if(th>=0) emit_movimm(((signed int)imm[i])>>31,th);
2519 else if(opcode[i]==0x0a||opcode[i]==0x0b) { // SLTI/SLTIU
2521 //assert(rs1[i]!=0); // r0 might be valid, but it's probably a bug
2522 signed char sh,sl,t;
2523 t=get_reg(i_regs->regmap,rt1[i]);
2524 sh=get_reg(i_regs->regmap,rs1[i]|64);
2525 sl=get_reg(i_regs->regmap,rs1[i]);
2529 if(sh<0) assert((i_regs->was32>>rs1[i])&1);
2530 if(sh<0||((i_regs->was32>>rs1[i])&1)) {
2531 if(opcode[i]==0x0a) { // SLTI
2533 if(i_regs->regmap_entry[t]!=rs1[i]) emit_loadreg(rs1[i],t);
2534 emit_slti32(t,imm[i],t);
2536 emit_slti32(sl,imm[i],t);
2541 if(i_regs->regmap_entry[t]!=rs1[i]) emit_loadreg(rs1[i],t);
2542 emit_sltiu32(t,imm[i],t);
2544 emit_sltiu32(sl,imm[i],t);
2549 if(opcode[i]==0x0a) // SLTI
2550 emit_slti64_32(sh,sl,imm[i],t);
2552 emit_sltiu64_32(sh,sl,imm[i],t);
2555 // SLTI(U) with r0 is just stupid,
2556 // nonetheless examples can be found
2557 if(opcode[i]==0x0a) // SLTI
2558 if(0<imm[i]) emit_movimm(1,t);
2559 else emit_zeroreg(t);
2562 if(imm[i]) emit_movimm(1,t);
2563 else emit_zeroreg(t);
2569 else if(opcode[i]>=0x0c&&opcode[i]<=0x0e) { // ANDI/ORI/XORI
2571 signed char sh,sl,th,tl;
2572 th=get_reg(i_regs->regmap,rt1[i]|64);
2573 tl=get_reg(i_regs->regmap,rt1[i]);
2574 sh=get_reg(i_regs->regmap,rs1[i]|64);
2575 sl=get_reg(i_regs->regmap,rs1[i]);
2576 if(tl>=0 && !((i_regs->isconst>>tl)&1)) {
2577 if(opcode[i]==0x0c) //ANDI
2581 if(i_regs->regmap_entry[tl]!=rs1[i]) emit_loadreg(rs1[i],tl);
2582 emit_andimm(tl,imm[i],tl);
2584 if(!((i_regs->wasconst>>sl)&1))
2585 emit_andimm(sl,imm[i],tl);
2587 emit_movimm(constmap[i][sl]&imm[i],tl);
2592 if(th>=0) emit_zeroreg(th);
2598 if(i_regs->regmap_entry[tl]!=rs1[i]) emit_loadreg(rs1[i],tl);
2602 emit_loadreg(rs1[i]|64,th);
2607 if(opcode[i]==0x0d) { //ORI
2609 emit_orimm(tl,imm[i],tl);
2611 if(!((i_regs->wasconst>>sl)&1))
2612 emit_orimm(sl,imm[i],tl);
2614 emit_movimm(constmap[i][sl]|imm[i],tl);
2617 if(opcode[i]==0x0e) { //XORI
2619 emit_xorimm(tl,imm[i],tl);
2621 if(!((i_regs->wasconst>>sl)&1))
2622 emit_xorimm(sl,imm[i],tl);
2624 emit_movimm(constmap[i][sl]^imm[i],tl);
2629 emit_movimm(imm[i],tl);
2630 if(th>=0) emit_zeroreg(th);
2638 static void shiftimm_assemble(int i,struct regstat *i_regs)
2640 if(opcode2[i]<=0x3) // SLL/SRL/SRA
2644 t=get_reg(i_regs->regmap,rt1[i]);
2645 s=get_reg(i_regs->regmap,rs1[i]);
2654 if(s<0&&i_regs->regmap_entry[t]!=rs1[i]) emit_loadreg(rs1[i],t);
2656 if(opcode2[i]==0) // SLL
2658 emit_shlimm(s<0?t:s,imm[i],t);
2660 if(opcode2[i]==2) // SRL
2662 emit_shrimm(s<0?t:s,imm[i],t);
2664 if(opcode2[i]==3) // SRA
2666 emit_sarimm(s<0?t:s,imm[i],t);
2670 if(s>=0 && s!=t) emit_mov(s,t);
2674 //emit_storereg(rt1[i],t); //DEBUG
2677 if(opcode2[i]>=0x38&&opcode2[i]<=0x3b) // DSLL/DSRL/DSRA
2680 signed char sh,sl,th,tl;
2681 th=get_reg(i_regs->regmap,rt1[i]|64);
2682 tl=get_reg(i_regs->regmap,rt1[i]);
2683 sh=get_reg(i_regs->regmap,rs1[i]|64);
2684 sl=get_reg(i_regs->regmap,rs1[i]);
2689 if(th>=0) emit_zeroreg(th);
2696 if(opcode2[i]==0x38) // DSLL
2698 if(th>=0) emit_shldimm(sh,sl,imm[i],th);
2699 emit_shlimm(sl,imm[i],tl);
2701 if(opcode2[i]==0x3a) // DSRL
2703 emit_shrdimm(sl,sh,imm[i],tl);
2704 if(th>=0) emit_shrimm(sh,imm[i],th);
2706 if(opcode2[i]==0x3b) // DSRA
2708 emit_shrdimm(sl,sh,imm[i],tl);
2709 if(th>=0) emit_sarimm(sh,imm[i],th);
2713 if(sl!=tl) emit_mov(sl,tl);
2714 if(th>=0&&sh!=th) emit_mov(sh,th);
2720 if(opcode2[i]==0x3c) // DSLL32
2723 signed char sl,tl,th;
2724 tl=get_reg(i_regs->regmap,rt1[i]);
2725 th=get_reg(i_regs->regmap,rt1[i]|64);
2726 sl=get_reg(i_regs->regmap,rs1[i]);
2735 emit_shlimm(th,imm[i]&31,th);
2740 if(opcode2[i]==0x3e) // DSRL32
2743 signed char sh,tl,th;
2744 tl=get_reg(i_regs->regmap,rt1[i]);
2745 th=get_reg(i_regs->regmap,rt1[i]|64);
2746 sh=get_reg(i_regs->regmap,rs1[i]|64);
2750 if(th>=0) emit_zeroreg(th);
2753 emit_shrimm(tl,imm[i]&31,tl);
2758 if(opcode2[i]==0x3f) // DSRA32
2762 tl=get_reg(i_regs->regmap,rt1[i]);
2763 sh=get_reg(i_regs->regmap,rs1[i]|64);
2769 emit_sarimm(tl,imm[i]&31,tl);
2776 #ifndef shift_assemble
2777 void shift_assemble(int i,struct regstat *i_regs)
2779 DebugMessage(M64MSG_ERROR, "Need shift_assemble for this architecture.");
2784 static void load_assemble(int i,struct regstat *i_regs)
2786 int s,th,tl,addr,map=-1,cache=-1;
2791 th=get_reg(i_regs->regmap,rt1[i]|64);
2792 tl=get_reg(i_regs->regmap,rt1[i]);
2793 s=get_reg(i_regs->regmap,rs1[i]);
2795 for(hr=0;hr<HOST_REGS;hr++) {
2796 if(i_regs->regmap[hr]>=0) reglist|=1<<hr;
2798 if(i_regs->regmap[HOST_CCREG]==CCREG) reglist&=~(1<<HOST_CCREG);
2800 c=(i_regs->wasconst>>s)&1;
2801 memtarget=((signed int)(constmap[i][s]+offset))<(signed int)0x80800000;
2802 if(using_tlb&&((signed int)(constmap[i][s]+offset))>=(signed int)0xC0000000) memtarget=1;
2804 if(tl<0) tl=get_reg(i_regs->regmap,-1);
2805 if(offset||s<0||c) addr=tl;
2807 //DebugMessage(M64MSG_VERBOSE, "load_assemble: c=%d",c);
2808 //if(c) DebugMessage(M64MSG_VERBOSE, "load_assemble: const=%x",(int)constmap[i][s]+offset);
2809 assert(tl>=0); // Even if the load is a NOP, we must check for pagefaults and I/O
2811 if(th>=0) reglist&=~(1<<th);
2815 map=get_reg(i_regs->regmap,ROREG);
2816 if(map<0) emit_loadreg(ROREG,map=HOST_TEMPREG);
2818 //#define R29_HACK 1
2820 // Strmnnrmn's speed hack
2821 if(rs1[i]!=29||start<0x80001000||start>=0x80800000)
2824 emit_cmpimm(addr,0x800000);
2826 #ifdef CORTEX_A8_BRANCH_PREDICTION_HACK
2827 // Hint to branch predictor that the branch is unlikely to be taken
2829 emit_jno_unlikely(0);
2837 if (opcode[i]==0x20||opcode[i]==0x24) x=3; // LB/LBU
2838 if (opcode[i]==0x21||opcode[i]==0x25) x=2; // LH/LHU
2839 map=get_reg(i_regs->regmap,TLREG);
2840 cache=get_reg(i_regs->regmap,MMREG);
2843 map=do_tlb_r(addr,tl,map,cache,x,-1,-1,c,constmap[i][s]+offset);
2844 do_tlb_r_branch(map,c,constmap[i][s]+offset,&jaddr);
2846 int dummy=(rt1[i]==0)||(tl!=get_reg(i_regs->regmap,rt1[i])); // ignore loads to r0 and unneeded reg
2847 if (opcode[i]==0x20) { // LB
2850 #ifdef HOST_IMM_ADDR32
2852 emit_movsbl_tlb((constmap[i][s]+offset)^3,map,tl);
2856 //emit_xorimm(addr,3,tl);
2857 //gen_tlb_addr_r(tl,map);
2858 //emit_movsbl_indexed((int)rdram-0x80000000,tl,tl);
2860 if(!c) emit_xorimm(addr,3,tl);
2861 else x=((constmap[i][s]+offset)^3)-(constmap[i][s]+offset);
2862 emit_movsbl_indexed_tlb(x,tl,map,tl);
2866 add_stub(LOADB_STUB,jaddr,(int)out,i,addr,(int)i_regs,ccadj[i],reglist);
2869 inline_readstub(LOADB_STUB,i,constmap[i][s]+offset,i_regs->regmap,rt1[i],ccadj[i],reglist);
2871 if (opcode[i]==0x21) { // LH
2874 #ifdef HOST_IMM_ADDR32
2876 emit_movswl_tlb((constmap[i][s]+offset)^2,map,tl);
2881 if(!c) emit_xorimm(addr,2,tl);
2882 else x=((constmap[i][s]+offset)^2)-(constmap[i][s]+offset);
2884 //emit_movswl_indexed_tlb(x,tl,map,tl);
2887 gen_tlb_addr_r(tl,map);
2888 emit_movswl_indexed(x,tl,tl);
2891 emit_movswl_indexed(x,tl,tl);
2893 emit_movswl_indexed((int)rdram-0x80000000+x,tl,tl);
2899 add_stub(LOADH_STUB,jaddr,(int)out,i,addr,(int)i_regs,ccadj[i],reglist);
2902 inline_readstub(LOADH_STUB,i,constmap[i][s]+offset,i_regs->regmap,rt1[i],ccadj[i],reglist);
2904 if (opcode[i]==0x23) { // LW
2907 //emit_readword_indexed((int)rdram-0x80000000,addr,tl);
2908 #ifdef HOST_IMM_ADDR32
2910 emit_readword_tlb(constmap[i][s]+offset,map,tl);
2913 emit_readword_indexed_tlb(0,addr,map,tl);
2916 add_stub(LOADW_STUB,jaddr,(int)out,i,addr,(int)i_regs,ccadj[i],reglist);
2919 inline_readstub(LOADW_STUB,i,constmap[i][s]+offset,i_regs->regmap,rt1[i],ccadj[i],reglist);
2921 if (opcode[i]==0x24) { // LBU
2924 #ifdef HOST_IMM_ADDR32
2926 emit_movzbl_tlb((constmap[i][s]+offset)^3,map,tl);
2930 //emit_xorimm(addr,3,tl);
2931 //gen_tlb_addr_r(tl,map);
2932 //emit_movzbl_indexed((int)rdram-0x80000000,tl,tl);
2934 if(!c) emit_xorimm(addr,3,tl);
2935 else x=((constmap[i][s]+offset)^3)-(constmap[i][s]+offset);
2936 emit_movzbl_indexed_tlb(x,tl,map,tl);
2940 add_stub(LOADBU_STUB,jaddr,(int)out,i,addr,(int)i_regs,ccadj[i],reglist);
2943 inline_readstub(LOADBU_STUB,i,constmap[i][s]+offset,i_regs->regmap,rt1[i],ccadj[i],reglist);
2945 if (opcode[i]==0x25) { // LHU
2948 #ifdef HOST_IMM_ADDR32
2950 emit_movzwl_tlb((constmap[i][s]+offset)^2,map,tl);
2955 if(!c) emit_xorimm(addr,2,tl);
2956 else x=((constmap[i][s]+offset)^2)-(constmap[i][s]+offset);
2958 //emit_movzwl_indexed_tlb(x,tl,map,tl);
2961 gen_tlb_addr_r(tl,map);
2962 emit_movzwl_indexed(x,tl,tl);
2965 emit_movzwl_indexed(x,tl,tl);
2967 emit_movzwl_indexed((int)rdram-0x80000000+x,tl,tl);
2973 add_stub(LOADHU_STUB,jaddr,(int)out,i,addr,(int)i_regs,ccadj[i],reglist);
2976 inline_readstub(LOADHU_STUB,i,constmap[i][s]+offset,i_regs->regmap,rt1[i],ccadj[i],reglist);
2978 if (opcode[i]==0x27) { // LWU
2982 //emit_readword_indexed((int)rdram-0x80000000,addr,tl);
2983 #ifdef HOST_IMM_ADDR32
2985 emit_readword_tlb(constmap[i][s]+offset,map,tl);
2988 emit_readword_indexed_tlb(0,addr,map,tl);
2991 add_stub(LOADW_STUB,jaddr,(int)out,i,addr,(int)i_regs,ccadj[i],reglist);
2994 inline_readstub(LOADW_STUB,i,constmap[i][s]+offset,i_regs->regmap,rt1[i],ccadj[i],reglist);
2998 if (opcode[i]==0x37) { // LD
3001 //gen_tlb_addr_r(tl,map);
3002 //if(th>=0) emit_readword_indexed((int)rdram-0x80000000,addr,th);
3003 //emit_readword_indexed((int)rdram-0x7FFFFFFC,addr,tl);
3004 #ifdef HOST_IMM_ADDR32
3006 emit_readdword_tlb(constmap[i][s]+offset,map,th,tl);
3009 emit_readdword_indexed_tlb(0,addr,map,th,tl);
3012 add_stub(LOADD_STUB,jaddr,(int)out,i,addr,(int)i_regs,ccadj[i],reglist);
3015 inline_readstub(LOADD_STUB,i,constmap[i][s]+offset,i_regs->regmap,rt1[i],ccadj[i],reglist);
3017 //emit_storereg(rt1[i],tl); // DEBUG
3018 //if(opcode[i]==0x23)
3019 //if(opcode[i]==0x24)
3020 //if(opcode[i]==0x23||opcode[i]==0x24)
3021 /*if(opcode[i]==0x21||opcode[i]==0x23||opcode[i]==0x24)
3025 emit_readword((int)&last_count,ECX);
3026 #if NEW_DYNAREC == NEW_DYNAREC_X86
3027 if(get_reg(i_regs->regmap,CCREG)<0)
3028 emit_loadreg(CCREG,HOST_CCREG);
3029 emit_add(HOST_CCREG,ECX,HOST_CCREG);
3030 emit_addimm(HOST_CCREG,2*ccadj[i],HOST_CCREG);
3031 emit_writeword(HOST_CCREG,(int)&Count);
3033 #if NEW_DYNAREC == NEW_DYNAREC_ARM
3034 if(get_reg(i_regs->regmap,CCREG)<0)
3035 emit_loadreg(CCREG,0);
3037 emit_mov(HOST_CCREG,0);
3039 emit_addimm(0,2*ccadj[i],0);
3040 emit_writeword(0,(int)&Count);
3042 emit_call((int)memdebug);
3044 restore_regs(0x100f);
3048 #ifndef loadlr_assemble
3049 static void loadlr_assemble(int i,struct regstat *i_regs)
3051 DebugMessage(M64MSG_ERROR, "Need loadlr_assemble for this architecture.");
3056 static void store_assemble(int i,struct regstat *i_regs)
3058 int s,th,tl,map=-1,cache=-1;
3061 int jaddr=0,jaddr2,type;
3063 int agr=AGEN1+(i&1);
3065 th=get_reg(i_regs->regmap,rs2[i]|64);
3066 tl=get_reg(i_regs->regmap,rs2[i]);
3067 s=get_reg(i_regs->regmap,rs1[i]);
3068 temp=get_reg(i_regs->regmap,agr);
3069 if(temp<0) temp=get_reg(i_regs->regmap,-1);
3072 c=(i_regs->wasconst>>s)&1;
3073 memtarget=((signed int)(constmap[i][s]+offset))<(signed int)0x80800000;
3074 if(using_tlb&&((signed int)(constmap[i][s]+offset))>=(signed int)0xC0000000) memtarget=1;
3078 for(hr=0;hr<HOST_REGS;hr++) {
3079 if(i_regs->regmap[hr]>=0) reglist|=1<<hr;
3081 if(i_regs->regmap[HOST_CCREG]==CCREG) reglist&=~(1<<HOST_CCREG);
3082 if(offset||s<0||c) addr=temp;
3086 map=get_reg(i_regs->regmap,ROREG);
3087 if(map<0) emit_loadreg(ROREG,map=HOST_TEMPREG);
3091 // Strmnnrmn's speed hack
3093 if(rs1[i]!=29||start<0x80001000||start>=0x80800000)
3095 emit_cmpimm(addr,0x800000);
3096 #ifdef DESTRUCTIVE_SHIFT
3097 if(s==addr) emit_mov(s,temp);
3100 if(rs1[i]!=29||start<0x80001000||start>=0x80800000)
3104 #ifdef CORTEX_A8_BRANCH_PREDICTION_HACK
3105 // Hint to branch predictor that the branch is unlikely to be taken
3107 emit_jno_unlikely(0);
3115 if (opcode[i]==0x28) x=3; // SB
3116 if (opcode[i]==0x29) x=2; // SH
3117 map=get_reg(i_regs->regmap,TLREG);
3118 cache=get_reg(i_regs->regmap,MMREG);
3121 map=do_tlb_w(addr,temp,map,cache,x,c,constmap[i][s]+offset);
3122 do_tlb_w_branch(map,c,constmap[i][s]+offset,&jaddr);
3125 if (opcode[i]==0x28) { // SB
3128 if(!c) emit_xorimm(addr,3,temp);
3129 else x=((constmap[i][s]+offset)^3)-(constmap[i][s]+offset);
3130 //gen_tlb_addr_w(temp,map);
3131 //emit_writebyte_indexed(tl,(int)rdram-0x80000000,temp);
3132 emit_writebyte_indexed_tlb(tl,x,temp,map,temp);
3136 if (opcode[i]==0x29) { // SH
3139 if(!c) emit_xorimm(addr,2,temp);
3140 else x=((constmap[i][s]+offset)^2)-(constmap[i][s]+offset);
3142 //emit_writehword_indexed_tlb(tl,x,temp,map,temp);
3145 gen_tlb_addr_w(temp,map);
3146 emit_writehword_indexed(tl,x,temp);
3148 emit_writehword_indexed(tl,(int)rdram-0x80000000+x,temp);
3152 if (opcode[i]==0x2B) { // SW
3154 //emit_writeword_indexed(tl,(int)rdram-0x80000000,addr);
3155 emit_writeword_indexed_tlb(tl,0,addr,map,temp);
3158 if (opcode[i]==0x3F) { // SD
3162 //emit_writeword_indexed(th,(int)rdram-0x80000000,addr);
3163 //emit_writeword_indexed(tl,(int)rdram-0x7FFFFFFC,addr);
3164 emit_writedword_indexed_tlb(th,tl,0,addr,map,temp);
3167 //emit_writeword_indexed(tl,(int)rdram-0x80000000,temp);
3168 //emit_writeword_indexed(tl,(int)rdram-0x7FFFFFFC,temp);
3169 emit_writedword_indexed_tlb(tl,tl,0,addr,map,temp);
3176 #ifdef DESTRUCTIVE_SHIFT
3177 // The x86 shift operation is 'destructive'; it overwrites the
3178 // source register, so we need to make a copy first and use that.
3181 #if defined(HOST_IMM8)
3182 int ir=get_reg(i_regs->regmap,INVCP);
3184 emit_cmpmem_indexedsr12_reg(ir,addr,1);
3186 emit_cmpmem_indexedsr12_imm((int)invalid_code,addr,1);
3188 #if defined(HAVE_CONDITIONAL_CALL) && !defined(DESTRUCTIVE_SHIFT)
3189 emit_callne(invalidate_addr_reg[addr]);
3193 add_stub(INVCODE_STUB,jaddr2,(int)out,reglist|(1<<HOST_CCREG),addr,0,0,0);
3198 add_stub(type,jaddr,(int)out,i,addr,(int)i_regs,ccadj[i],reglist);
3199 } else if(c&&!memtarget) {
3200 inline_writestub(type,i,constmap[i][s]+offset,i_regs->regmap,rs2[i],ccadj[i],reglist);
3202 //if(opcode[i]==0x2B || opcode[i]==0x3F)
3203 //if(opcode[i]==0x2B || opcode[i]==0x28)
3204 //if(opcode[i]==0x2B || opcode[i]==0x29)
3205 //if(opcode[i]==0x2B)
3207 // Uncomment for extra debug output:
3209 if(opcode[i]==0x2B || opcode[i]==0x28 || opcode[i]==0x29 || opcode[i]==0x3F)
3211 #if NEW_DYNAREC == NEW_DYNAREC_X86
3214 #if NEW_DYNAREC == NEW_DYNAREC_ARM
3217 emit_readword((int)&last_count,ECX);
3218 #if NEW_DYNAREC == NEW_DYNAREC_X86
3219 if(get_reg(i_regs->regmap,CCREG)<0)
3220 emit_loadreg(CCREG,HOST_CCREG);
3221 emit_add(HOST_CCREG,ECX,HOST_CCREG);
3222 emit_addimm(HOST_CCREG,2*ccadj[i],HOST_CCREG);
3223 emit_writeword(HOST_CCREG,(int)&Count);
3225 #if NEW_DYNAREC == NEW_DYNAREC_ARM
3226 if(get_reg(i_regs->regmap,CCREG)<0)
3227 emit_loadreg(CCREG,0);
3229 emit_mov(HOST_CCREG,0);
3231 emit_addimm(0,2*ccadj[i],0);
3232 emit_writeword(0,(int)&Count);
3234 emit_call((int)memdebug);
3235 #if NEW_DYNAREC == NEW_DYNAREC_X86
3238 #if NEW_DYNAREC == NEW_DYNAREC_ARM
3239 restore_regs(0x100f);
3245 static void storelr_assemble(int i,struct regstat *i_regs)
3252 int case1,case2,case3;
3253 int done0,done1,done2;
3255 int agr=AGEN1+(i&1);
3257 th=get_reg(i_regs->regmap,rs2[i]|64);
3258 tl=get_reg(i_regs->regmap,rs2[i]);
3259 s=get_reg(i_regs->regmap,rs1[i]);
3260 temp=get_reg(i_regs->regmap,agr);
3261 if(temp<0) temp=get_reg(i_regs->regmap,-1);
3264 c=(i_regs->isconst>>s)&1;
3265 memtarget=((signed int)(constmap[i][s]+offset))<(signed int)0x80800000;
3266 if(using_tlb&&((signed int)(constmap[i][s]+offset))>=(signed int)0xC0000000) memtarget=1;
3269 for(hr=0;hr<HOST_REGS;hr++) {
3270 if(i_regs->regmap[hr]>=0) reglist|=1<<hr;
3275 emit_cmpimm(s<0||offset?temp:s,0x800000);
3276 if(!offset&&s!=temp) emit_mov(s,temp);
3282 if(!memtarget||!rs1[i]) {
3288 int map=get_reg(i_regs->regmap,ROREG);
3289 if(map<0) emit_loadreg(ROREG,map=HOST_TEMPREG);
3290 gen_tlb_addr_w(temp,map);
3292 if((u_int)rdram!=0x80000000)
3293 emit_addimm_no_flags((u_int)rdram-(u_int)0x80000000,temp);
3296 int map=get_reg(i_regs->regmap,TLREG);
3297 int cache=get_reg(i_regs->regmap,MMREG);
3300 map=do_tlb_w(c||s<0||offset?temp:s,temp,map,cache,0,c,constmap[i][s]+offset);
3301 if(!c&&!offset&&s>=0) emit_mov(s,temp);
3302 do_tlb_w_branch(map,c,constmap[i][s]+offset,&jaddr);
3303 if(!jaddr&&!memtarget) {
3307 gen_tlb_addr_w(temp,map);
3310 if (opcode[i]==0x2C||opcode[i]==0x2D) { // SDL/SDR
3311 temp2=get_reg(i_regs->regmap,FTEMP);
3312 if(!rs2[i]) temp2=th=tl;
3315 emit_testimm(temp,2);
3318 emit_testimm(temp,1);
3322 if (opcode[i]==0x2A) { // SWL
3323 emit_writeword_indexed(tl,0,temp);
3325 if (opcode[i]==0x2E) { // SWR
3326 emit_writebyte_indexed(tl,3,temp);
3328 if (opcode[i]==0x2C) { // SDL
3329 emit_writeword_indexed(th,0,temp);
3330 if(rs2[i]) emit_mov(tl,temp2);
3332 if (opcode[i]==0x2D) { // SDR
3333 emit_writebyte_indexed(tl,3,temp);
3334 if(rs2[i]) emit_shldimm(th,tl,24,temp2);
3339 set_jump_target(case1,(int)out);
3340 if (opcode[i]==0x2A) { // SWL
3341 // Write 3 msb into three least significant bytes
3342 if(rs2[i]) emit_rorimm(tl,8,tl);
3343 emit_writehword_indexed(tl,-1,temp);
3344 if(rs2[i]) emit_rorimm(tl,16,tl);
3345 emit_writebyte_indexed(tl,1,temp);
3346 if(rs2[i]) emit_rorimm(tl,8,tl);
3348 if (opcode[i]==0x2E) { // SWR
3349 // Write two lsb into two most significant bytes
3350 emit_writehword_indexed(tl,1,temp);
3352 if (opcode[i]==0x2C) { // SDL
3353 if(rs2[i]) emit_shrdimm(tl,th,8,temp2);
3354 // Write 3 msb into three least significant bytes
3355 if(rs2[i]) emit_rorimm(th,8,th);
3356 emit_writehword_indexed(th,-1,temp);
3357 if(rs2[i]) emit_rorimm(th,16,th);
3358 emit_writebyte_indexed(th,1,temp);
3359 if(rs2[i]) emit_rorimm(th,8,th);
3361 if (opcode[i]==0x2D) { // SDR
3362 if(rs2[i]) emit_shldimm(th,tl,16,temp2);
3363 // Write two lsb into two most significant bytes
3364 emit_writehword_indexed(tl,1,temp);
3369 set_jump_target(case2,(int)out);
3370 emit_testimm(temp,1);
3373 if (opcode[i]==0x2A) { // SWL
3374 // Write two msb into two least significant bytes
3375 if(rs2[i]) emit_rorimm(tl,16,tl);
3376 emit_writehword_indexed(tl,-2,temp);
3377 if(rs2[i]) emit_rorimm(tl,16,tl);
3379 if (opcode[i]==0x2E) { // SWR
3380 // Write 3 lsb into three most significant bytes
3381 emit_writebyte_indexed(tl,-1,temp);
3382 if(rs2[i]) emit_rorimm(tl,8,tl);
3383 emit_writehword_indexed(tl,0,temp);
3384 if(rs2[i]) emit_rorimm(tl,24,tl);
3386 if (opcode[i]==0x2C) { // SDL
3387 if(rs2[i]) emit_shrdimm(tl,th,16,temp2);
3388 // Write two msb into two least significant bytes
3389 if(rs2[i]) emit_rorimm(th,16,th);
3390 emit_writehword_indexed(th,-2,temp);
3391 if(rs2[i]) emit_rorimm(th,16,th);
3393 if (opcode[i]==0x2D) { // SDR
3394 if(rs2[i]) emit_shldimm(th,tl,8,temp2);
3395 // Write 3 lsb into three most significant bytes
3396 emit_writebyte_indexed(tl,-1,temp);
3397 if(rs2[i]) emit_rorimm(tl,8,tl);
3398 emit_writehword_indexed(tl,0,temp);
3399 if(rs2[i]) emit_rorimm(tl,24,tl);
3404 set_jump_target(case3,(int)out);
3405 if (opcode[i]==0x2A) { // SWL
3406 // Write msb into least significant byte
3407 if(rs2[i]) emit_rorimm(tl,24,tl);
3408 emit_writebyte_indexed(tl,-3,temp);
3409 if(rs2[i]) emit_rorimm(tl,8,tl);
3411 if (opcode[i]==0x2E) { // SWR
3412 // Write entire word
3413 emit_writeword_indexed(tl,-3,temp);
3415 if (opcode[i]==0x2C) { // SDL
3416 if(rs2[i]) emit_shrdimm(tl,th,24,temp2);
3417 // Write msb into least significant byte
3418 if(rs2[i]) emit_rorimm(th,24,th);
3419 emit_writebyte_indexed(th,-3,temp);
3420 if(rs2[i]) emit_rorimm(th,8,th);
3422 if (opcode[i]==0x2D) { // SDR
3423 if(rs2[i]) emit_mov(th,temp2);
3424 // Write entire word
3425 emit_writeword_indexed(tl,-3,temp);
3427 set_jump_target(done0,(int)out);
3428 set_jump_target(done1,(int)out);
3429 set_jump_target(done2,(int)out);
3430 if (opcode[i]==0x2C) { // SDL
3431 emit_testimm(temp,4);
3434 emit_andimm(temp,~3,temp);
3435 emit_writeword_indexed(temp2,4,temp);
3436 set_jump_target(done0,(int)out);
3438 if (opcode[i]==0x2D) { // SDR
3439 emit_testimm(temp,4);
3442 emit_andimm(temp,~3,temp);
3443 emit_writeword_indexed(temp2,-4,temp);
3444 set_jump_target(done0,(int)out);
3447 add_stub(STORELR_STUB,jaddr,(int)out,0,(int)i_regs,rs2[i],ccadj[i],reglist);
3450 int map=get_reg(i_regs->regmap,ROREG);
3451 if(map<0) map=HOST_TEMPREG;
3452 gen_orig_addr_w(temp,map);
3454 emit_addimm_no_flags((u_int)0x80000000-(u_int)rdram,temp);
3456 #if defined(HOST_IMM8)
3457 int ir=get_reg(i_regs->regmap,INVCP);
3459 emit_cmpmem_indexedsr12_reg(ir,temp,1);
3461 emit_cmpmem_indexedsr12_imm((int)invalid_code,temp,1);
3463 #if defined(HAVE_CONDITIONAL_CALL) && !defined(DESTRUCTIVE_SHIFT)
3464 emit_callne(invalidate_addr_reg[temp]);
3468 add_stub(INVCODE_STUB,jaddr2,(int)out,reglist|(1<<HOST_CCREG),temp,0,0,0);
3473 //save_regs(0x100f);
3474 emit_readword((int)&last_count,ECX);
3475 if(get_reg(i_regs->regmap,CCREG)<0)
3476 emit_loadreg(CCREG,HOST_CCREG);
3477 emit_add(HOST_CCREG,ECX,HOST_CCREG);
3478 emit_addimm(HOST_CCREG,2*ccadj[i],HOST_CCREG);
3479 emit_writeword(HOST_CCREG,(int)&Count);
3480 emit_call((int)memdebug);
3482 //restore_regs(0x100f);
3486 static void c1ls_assemble(int i,struct regstat *i_regs)
3493 int jaddr,jaddr2=0,jaddr3,type;
3494 int agr=AGEN1+(i&1);
3496 th=get_reg(i_regs->regmap,FTEMP|64);
3497 tl=get_reg(i_regs->regmap,FTEMP);
3498 s=get_reg(i_regs->regmap,rs1[i]);
3499 temp=get_reg(i_regs->regmap,agr);
3500 if(temp<0) temp=get_reg(i_regs->regmap,-1);
3505 for(hr=0;hr<HOST_REGS;hr++) {
3506 if(i_regs->regmap[hr]>=0) reglist|=1<<hr;
3508 if(i_regs->regmap[HOST_CCREG]==CCREG) reglist&=~(1<<HOST_CCREG);
3509 if (opcode[i]==0x31||opcode[i]==0x35) // LWC1/LDC1
3511 // Loads use a temporary register which we need to save
3514 if (opcode[i]==0x39||opcode[i]==0x3D) // SWC1/SDC1
3518 //if(s<0) emit_loadreg(rs1[i],ar); //address_generation does this now
3519 //else c=(i_regs->wasconst>>s)&1;
3520 if(s>=0) c=(i_regs->wasconst>>s)&1;
3521 // Check cop1 unusable
3523 signed char rs=get_reg(i_regs->regmap,CSREG);
3525 emit_testimm(rs,0x20000000);
3528 add_stub(FP_STUB,jaddr,(int)out,i,rs,(int)i_regs,is_delayslot,0);
3531 if (opcode[i]==0x39) { // SWC1 (get float address)
3532 emit_readword((int)®_cop1_simple[(source[i]>>16)&0x1f],tl);
3534 if (opcode[i]==0x3D) { // SDC1 (get double address)
3535 emit_readword((int)®_cop1_double[(source[i]>>16)&0x1f],tl);
3537 // Generate address + offset
3540 if (!c||opcode[i]==0x39||opcode[i]==0x3D) // SWC1/SDC1
3542 map=get_reg(i_regs->regmap,ROREG);
3543 if(map<0) emit_loadreg(ROREG,map=HOST_TEMPREG);
3547 emit_cmpimm(offset||c||s<0?ar:s,0x800000);
3551 map=get_reg(i_regs->regmap,TLREG);
3552 int cache=get_reg(i_regs->regmap,MMREG);
3555 if (opcode[i]==0x31||opcode[i]==0x35) { // LWC1/LDC1
3556 map=do_tlb_r(offset||c||s<0?ar:s,ar,map,cache,0,-1,-1,c,constmap[i][s]+offset);
3558 if (opcode[i]==0x39||opcode[i]==0x3D) { // SWC1/SDC1
3559 map=do_tlb_w(offset||c||s<0?ar:s,ar,map,cache,0,c,constmap[i][s]+offset);
3562 if (opcode[i]==0x39) { // SWC1 (read float)
3563 emit_readword_indexed(0,tl,tl);
3565 if (opcode[i]==0x3D) { // SDC1 (read double)
3566 emit_readword_indexed(4,tl,th);
3567 emit_readword_indexed(0,tl,tl);
3569 if (opcode[i]==0x31) { // LWC1 (get target address)
3570 emit_readword((int)®_cop1_simple[(source[i]>>16)&0x1f],temp);
3572 if (opcode[i]==0x35) { // LDC1 (get target address)
3573 emit_readword((int)®_cop1_double[(source[i]>>16)&0x1f],temp);
3580 else if(((signed int)(constmap[i][s]+offset))>=(signed int)0x80800000) {
3582 emit_jmp(0); // inline_readstub/inline_writestub? Very rare case
3584 #ifdef DESTRUCTIVE_SHIFT
3585 if (opcode[i]==0x39||opcode[i]==0x3D) { // SWC1/SDC1
3586 if(!offset&&!c&&s>=0) emit_mov(s,ar);
3590 if (opcode[i]==0x31||opcode[i]==0x35) { // LWC1/LDC1
3591 do_tlb_r_branch(map,c,constmap[i][s]+offset,&jaddr2);
3593 if (opcode[i]==0x39||opcode[i]==0x3D) { // SWC1/SDC1
3594 do_tlb_w_branch(map,c,constmap[i][s]+offset,&jaddr2);
3597 if (opcode[i]==0x31) { // LWC1
3598 //if(s>=0&&!c&&!offset) emit_mov(s,tl);
3599 //gen_tlb_addr_r(ar,map);
3600 //emit_readword_indexed((int)rdram-0x80000000,tl,tl);
3601 #ifdef HOST_IMM_ADDR32
3602 if(c) emit_readword_tlb(constmap[i][s]+offset,map,tl);
3605 emit_readword_indexed_tlb(0,offset||c||s<0?tl:s,map,tl);
3608 if (opcode[i]==0x35) { // LDC1
3610 //if(s>=0&&!c&&!offset) emit_mov(s,tl);
3611 //gen_tlb_addr_r(ar,map);
3612 //emit_readword_indexed((int)rdram-0x80000000,tl,th);
3613 //emit_readword_indexed((int)rdram-0x7FFFFFFC,tl,tl);
3614 #ifdef HOST_IMM_ADDR32
3615 if(c) emit_readdword_tlb(constmap[i][s]+offset,map,th,tl);
3618 emit_readdword_indexed_tlb(0,offset||c||s<0?tl:s,map,th,tl);
3621 if (opcode[i]==0x39) { // SWC1
3622 //emit_writeword_indexed(tl,(int)rdram-0x80000000,temp);
3623 emit_writeword_indexed_tlb(tl,0,offset||c||s<0?temp:s,map,temp);
3626 if (opcode[i]==0x3D) { // SDC1
3628 //emit_writeword_indexed(th,(int)rdram-0x80000000,temp);
3629 //emit_writeword_indexed(tl,(int)rdram-0x7FFFFFFC,temp);
3630 emit_writedword_indexed_tlb(th,tl,0,offset||c||s<0?temp:s,map,temp);
3634 if (opcode[i]==0x39||opcode[i]==0x3D) { // SWC1/SDC1
3635 #ifndef DESTRUCTIVE_SHIFT
3636 temp=offset||c||s<0?ar:s;
3638 #if defined(HOST_IMM8)
3639 int ir=get_reg(i_regs->regmap,INVCP);
3641 emit_cmpmem_indexedsr12_reg(ir,temp,1);
3643 emit_cmpmem_indexedsr12_imm((int)invalid_code,temp,1);
3645 #if defined(HAVE_CONDITIONAL_CALL) && !defined(DESTRUCTIVE_SHIFT)
3646 emit_callne(invalidate_addr_reg[temp]);
3650 add_stub(INVCODE_STUB,jaddr3,(int)out,reglist|(1<<HOST_CCREG),temp,0,0,0);
3654 if(jaddr2) add_stub(type,jaddr2,(int)out,i,offset||c||s<0?ar:s,(int)i_regs,ccadj[i],reglist);
3655 if (opcode[i]==0x31) { // LWC1 (write float)
3656 emit_writeword_indexed(tl,0,temp);
3658 if (opcode[i]==0x35) { // LDC1 (write double)
3659 emit_writeword_indexed(th,4,temp);
3660 emit_writeword_indexed(tl,0,temp);
3662 //if(opcode[i]==0x39)
3663 /*if(opcode[i]==0x39||opcode[i]==0x31)
3666 emit_readword((int)&last_count,ECX);
3667 if(get_reg(i_regs->regmap,CCREG)<0)
3668 emit_loadreg(CCREG,HOST_CCREG);
3669 emit_add(HOST_CCREG,ECX,HOST_CCREG);
3670 emit_addimm(HOST_CCREG,2*ccadj[i],HOST_CCREG);
3671 emit_writeword(HOST_CCREG,(int)&Count);
3672 emit_call((int)memdebug);
3677 #ifndef multdiv_assemble
3678 void multdiv_assemble(int i,struct regstat *i_regs)
3680 DebugMessage(M64MSG_ERROR, "Need multdiv_assemble for this architecture.");
3685 static void mov_assemble(int i,struct regstat *i_regs)
3687 //if(opcode2[i]==0x10||opcode2[i]==0x12) { // MFHI/MFLO
3688 //if(opcode2[i]==0x11||opcode2[i]==0x13) { // MTHI/MTLO
3690 signed char sh,sl,th,tl;
3691 th=get_reg(i_regs->regmap,rt1[i]|64);
3692 tl=get_reg(i_regs->regmap,rt1[i]);
3695 sh=get_reg(i_regs->regmap,rs1[i]|64);
3696 sl=get_reg(i_regs->regmap,rs1[i]);
3697 if(sl>=0) emit_mov(sl,tl);
3698 else emit_loadreg(rs1[i],tl);
3700 if(sh>=0) emit_mov(sh,th);
3701 else emit_loadreg(rs1[i]|64,th);
3707 #ifndef fconv_assemble
3708 void fconv_assemble(int i,struct regstat *i_regs)
3710 DebugMessage(M64MSG_ERROR, "Need fconv_assemble for this architecture.");
3716 static void float_assemble(int i,struct regstat *i_regs)
3718 DebugMessage(M64MSG_ERROR, "Need float_assemble for this architecture.");
3723 static void syscall_assemble(int i,struct regstat *i_regs)
3725 signed char ccreg=get_reg(i_regs->regmap,CCREG);
3726 assert(ccreg==HOST_CCREG);
3727 assert(!is_delayslot);
3728 emit_movimm(start+i*4,EAX); // Get PC
3729 emit_addimm(HOST_CCREG,CLOCK_DIVIDER*ccadj[i],HOST_CCREG); // CHECK: is this right? There should probably be an extra cycle...
3730 emit_jmp((int)jump_syscall);
3733 static void ds_assemble(int i,struct regstat *i_regs)
3738 alu_assemble(i,i_regs);break;
3740 imm16_assemble(i,i_regs);break;
3742 shift_assemble(i,i_regs);break;
3744 shiftimm_assemble(i,i_regs);break;
3746 load_assemble(i,i_regs);break;
3748 loadlr_assemble(i,i_regs);break;
3750 store_assemble(i,i_regs);break;
3752 storelr_assemble(i,i_regs);break;
3754 cop0_assemble(i,i_regs);break;
3756 cop1_assemble(i,i_regs);break;
3758 c1ls_assemble(i,i_regs);break;
3760 fconv_assemble(i,i_regs);break;
3762 float_assemble(i,i_regs);break;
3764 fcomp_assemble(i,i_regs);break;
3766 multdiv_assemble(i,i_regs);break;
3768 mov_assemble(i,i_regs);break;
3776 DebugMessage(M64MSG_VERBOSE, "Jump in the delay slot. This is probably a bug.");
3781 // Is the branch target a valid internal jump?
3782 static int internal_branch(uint64_t i_is32,int addr)
3784 if(addr&1) return 0; // Indirect (register) jump
3785 if(addr>=start && addr<start+slen*4-4)
3787 int t=(addr-start)>>2;
3788 // Delay slots are not valid branch targets
3789 //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;
3790 // 64 -> 32 bit transition requires a recompile
3791 /*if(is32[t]&~unneeded_reg_upper[t]&~i_is32)
3793 if(requires_32bit[t]&~i_is32) DebugMessage(M64MSG_VERBOSE, "optimizable: no");
3794 else DebugMessage(M64MSG_VERBOSE, "optimizable: yes");
3796 //if(is32[t]&~unneeded_reg_upper[t]&~i_is32) return 0;
3797 if(requires_32bit[t]&~i_is32) return 0;
3803 #ifndef wb_invalidate
3804 static void wb_invalidate(signed char pre[],signed char entry[],uint64_t dirty,uint64_t is32,
3805 uint64_t u,uint64_t uu)
3808 for(hr=0;hr<HOST_REGS;hr++) {
3809 if(hr!=EXCLUDE_REG) {
3810 if(pre[hr]!=entry[hr]) {
3813 if(get_reg(entry,pre[hr])<0) {
3815 if(!((u>>pre[hr])&1)) {
3816 emit_storereg(pre[hr],hr);
3817 if( ((is32>>pre[hr])&1) && !((uu>>pre[hr])&1) ) {
3818 emit_sarimm(hr,31,hr);
3819 emit_storereg(pre[hr]|64,hr);
3823 if(!((uu>>(pre[hr]&63))&1) && !((is32>>(pre[hr]&63))&1)) {
3824 emit_storereg(pre[hr],hr);
3833 // Move from one register to another (no writeback)
3834 for(hr=0;hr<HOST_REGS;hr++) {
3835 if(hr!=EXCLUDE_REG) {
3836 if(pre[hr]!=entry[hr]) {
3837 if(pre[hr]>=0&&(pre[hr]&63)<TEMPREG) {
3839 if((nr=get_reg(entry,pre[hr]))>=0) {
3849 // Load the specified registers
3850 // This only loads the registers given as arguments because
3851 // we don't want to load things that will be overwritten
3852 static void load_regs(signed char entry[],signed char regmap[],int is32,int rs1,int rs2)
3856 for(hr=0;hr<HOST_REGS;hr++) {
3857 if(hr!=EXCLUDE_REG&®map[hr]>=0) {
3858 if(entry[hr]!=regmap[hr]) {
3859 if(regmap[hr]==rs1||regmap[hr]==rs2)
3866 emit_loadreg(regmap[hr],hr);
3873 for(hr=0;hr<HOST_REGS;hr++) {
3874 if(hr!=EXCLUDE_REG&®map[hr]>=0) {
3875 if(entry[hr]!=regmap[hr]) {
3876 if(regmap[hr]-64==rs1||regmap[hr]-64==rs2)
3878 assert(regmap[hr]!=64);
3879 if((is32>>(regmap[hr]&63))&1) {
3880 int lr=get_reg(regmap,regmap[hr]-64);
3882 emit_sarimm(lr,31,hr);
3884 emit_loadreg(regmap[hr],hr);
3888 emit_loadreg(regmap[hr],hr);
3896 // Load registers prior to the start of a loop
3897 // so that they are not loaded within the loop
3898 static void loop_preload(signed char pre[],signed char entry[])
3901 for(hr=0;hr<HOST_REGS;hr++) {
3902 if(hr!=EXCLUDE_REG) {
3903 if(pre[hr]!=entry[hr]) {
3905 if(get_reg(pre,entry[hr])<0) {
3906 assem_debug("loop preload:");
3907 //DebugMessage(M64MSG_VERBOSE, "loop preload: %d",hr);
3911 else if(entry[hr]<TEMPREG)
3913 emit_loadreg(entry[hr],hr);
3915 else if(entry[hr]-64<TEMPREG)
3917 emit_loadreg(entry[hr],hr);
3926 // Generate address for load/store instruction
3927 static void address_generation(int i,struct regstat *i_regs,signed char entry[])
3929 if(itype[i]==LOAD||itype[i]==LOADLR||itype[i]==STORE||itype[i]==STORELR||itype[i]==C1LS) {
3931 int agr=AGEN1+(i&1);
3932 int mgr=MGEN1+(i&1);
3933 if(itype[i]==LOAD) {
3934 ra=get_reg(i_regs->regmap,rt1[i]);
3935 if(ra<0) ra=get_reg(i_regs->regmap,-1);
3938 if(itype[i]==LOADLR) {
3939 ra=get_reg(i_regs->regmap,FTEMP);
3941 if(itype[i]==STORE||itype[i]==STORELR) {
3942 ra=get_reg(i_regs->regmap,agr);
3943 if(ra<0) ra=get_reg(i_regs->regmap,-1);
3945 if(itype[i]==C1LS) {
3946 if (opcode[i]==0x31||opcode[i]==0x35) // LWC1/LDC1
3947 ra=get_reg(i_regs->regmap,FTEMP);
3949 ra=get_reg(i_regs->regmap,agr);
3950 if(ra<0) ra=get_reg(i_regs->regmap,-1);
3953 int rs=get_reg(i_regs->regmap,rs1[i]);
3954 int rm=get_reg(i_regs->regmap,TLREG);
3957 int c=(i_regs->wasconst>>rs)&1;
3959 // Using r0 as a base address
3961 if(!entry||entry[rm]!=mgr) {
3962 generate_map_const(offset,rm);
3963 } // else did it in the previous cycle
3965 if(!entry||entry[ra]!=agr) {
3966 if (opcode[i]==0x22||opcode[i]==0x26) {
3967 emit_movimm(offset&0xFFFFFFFC,ra); // LWL/LWR
3968 }else if (opcode[i]==0x1a||opcode[i]==0x1b) {
3969 emit_movimm(offset&0xFFFFFFF8,ra); // LDL/LDR
3971 emit_movimm(offset,ra);
3973 } // else did it in the previous cycle
3976 if(!entry||entry[ra]!=rs1[i])
3977 emit_loadreg(rs1[i],ra);
3978 //if(!entry||entry[ra]!=rs1[i])
3979 // DebugMessage(M64MSG_VERBOSE, "poor load scheduling!");
3983 if(!entry||entry[rm]!=mgr) {
3984 if(itype[i]==STORE||itype[i]==STORELR||opcode[i]==0x39||opcode[i]==0x3D) {
3985 // Stores to memory go thru the mapper to detect self-modifying
3986 // code, loads don't.
3987 if((unsigned int)(constmap[i][rs]+offset)>=0xC0000000 ||
3988 (unsigned int)(constmap[i][rs]+offset)<0x80800000 )
3989 generate_map_const(constmap[i][rs]+offset,rm);
3991 if((signed int)(constmap[i][rs]+offset)>=(signed int)0xC0000000)
3992 generate_map_const(constmap[i][rs]+offset,rm);
3996 if(rs1[i]!=rt1[i]||itype[i]!=LOAD) {
3997 if(!entry||entry[ra]!=agr) {
3998 if (opcode[i]==0x22||opcode[i]==0x26) { // LWL/LWR
4000 if((signed int)constmap[i][rs]+offset<(signed int)0x80800000)
4001 emit_movimm(((constmap[i][rs]+offset)&0xFFFFFFFC)+(int)rdram-0x80000000,ra);
4004 emit_movimm((constmap[i][rs]+offset)&0xFFFFFFFC,ra);
4005 }else if (opcode[i]==0x1a||opcode[i]==0x1b) { // LDL/LDR
4007 if((signed int)constmap[i][rs]+offset<(signed int)0x80800000)
4008 emit_movimm(((constmap[i][rs]+offset)&0xFFFFFFF8)+(int)rdram-0x80000000,ra);
4011 emit_movimm((constmap[i][rs]+offset)&0xFFFFFFF8,ra);
4013 #ifdef HOST_IMM_ADDR32
4014 if((itype[i]!=LOAD&&opcode[i]!=0x31&&opcode[i]!=0x35) ||
4015 (using_tlb&&((signed int)constmap[i][rs]+offset)>=(signed int)0xC0000000))
4018 if((itype[i]==LOAD||opcode[i]==0x31||opcode[i]==0x35)&&(signed int)constmap[i][rs]+offset<(signed int)0x80800000)
4019 emit_movimm(constmap[i][rs]+offset+(int)rdram-0x80000000,ra);
4022 emit_movimm(constmap[i][rs]+offset,ra);
4024 } // else did it in the previous cycle
4025 } // else load_consts already did it
4027 if(offset&&!c&&rs1[i]) {
4029 emit_addimm(rs,offset,ra);
4031 emit_addimm(ra,offset,ra);
4036 // Preload constants for next instruction
4037 if(itype[i+1]==LOAD||itype[i+1]==LOADLR||itype[i+1]==STORE||itype[i+1]==STORELR||itype[i+1]==C1LS) {
4039 #ifndef HOST_IMM_ADDR32
4041 agr=MGEN1+((i+1)&1);
4042 ra=get_reg(i_regs->regmap,agr);
4044 int rs=get_reg(regs[i+1].regmap,rs1[i+1]);
4045 int offset=imm[i+1];
4046 int c=(regs[i+1].wasconst>>rs)&1;
4048 if(itype[i+1]==STORE||itype[i+1]==STORELR||opcode[i+1]==0x39||opcode[i+1]==0x3D) {
4049 // Stores to memory go thru the mapper to detect self-modifying
4050 // code, loads don't.
4051 if((unsigned int)(constmap[i+1][rs]+offset)>=0xC0000000 ||
4052 (unsigned int)(constmap[i+1][rs]+offset)<0x80800000 )
4053 generate_map_const(constmap[i+1][rs]+offset,ra);
4055 if((signed int)(constmap[i+1][rs]+offset)>=(signed int)0xC0000000)
4056 generate_map_const(constmap[i+1][rs]+offset,ra);
4059 /*else if(rs1[i]==0) {
4060 generate_map_const(offset,ra);
4065 agr=AGEN1+((i+1)&1);
4066 ra=get_reg(i_regs->regmap,agr);
4068 int rs=get_reg(regs[i+1].regmap,rs1[i+1]);
4069 int offset=imm[i+1];
4070 int c=(regs[i+1].wasconst>>rs)&1;
4071 if(c&&(rs1[i+1]!=rt1[i+1]||itype[i+1]!=LOAD)) {
4072 if (opcode[i+1]==0x22||opcode[i+1]==0x26) { // LWL/LWR
4074 if((signed int)constmap[i+1][rs]+offset<(signed int)0x80800000)
4075 emit_movimm(((constmap[i+1][rs]+offset)&0xFFFFFFFC)+(int)rdram-0x80000000,ra);
4078 emit_movimm((constmap[i+1][rs]+offset)&0xFFFFFFFC,ra);
4079 }else if (opcode[i+1]==0x1a||opcode[i+1]==0x1b) { // LDL/LDR
4081 if((signed int)constmap[i+1][rs]+offset<(signed int)0x80800000)
4082 emit_movimm(((constmap[i+1][rs]+offset)&0xFFFFFFF8)+(int)rdram-0x80000000,ra);
4085 emit_movimm((constmap[i+1][rs]+offset)&0xFFFFFFF8,ra);
4087 #ifdef HOST_IMM_ADDR32
4088 if((itype[i+1]!=LOAD&&opcode[i+1]!=0x31&&opcode[i+1]!=0x35) ||
4089 (using_tlb&&((signed int)constmap[i+1][rs]+offset)>=(signed int)0xC0000000))
4092 if((itype[i+1]==LOAD||opcode[i+1]==0x31||opcode[i+1]==0x35)&&(signed int)constmap[i+1][rs]+offset<(signed int)0x80800000)
4093 emit_movimm(constmap[i+1][rs]+offset+(int)rdram-0x80000000,ra);
4096 emit_movimm(constmap[i+1][rs]+offset,ra);
4099 else if(rs1[i+1]==0) {
4100 // Using r0 as a base address
4101 if (opcode[i+1]==0x22||opcode[i+1]==0x26) {
4102 emit_movimm(offset&0xFFFFFFFC,ra); // LWL/LWR
4103 }else if (opcode[i+1]==0x1a||opcode[i+1]==0x1b) {
4104 emit_movimm(offset&0xFFFFFFF8,ra); // LDL/LDR
4106 emit_movimm(offset,ra);
4113 static int get_final_value(int hr, int i, int *value)
4115 int reg=regs[i].regmap[hr];
4117 if(regs[i+1].regmap[hr]!=reg) break;
4118 if(!((regs[i+1].isconst>>hr)&1)) break;
4123 if(itype[i]==UJUMP||itype[i]==RJUMP||itype[i]==CJUMP||itype[i]==SJUMP) {
4124 *value=constmap[i][hr];
4128 if(itype[i+1]==UJUMP||itype[i+1]==RJUMP||itype[i+1]==CJUMP||itype[i+1]==SJUMP) {
4129 // Load in delay slot, out-of-order execution
4130 if(itype[i+2]==LOAD&&rs1[i+2]==reg&&rt1[i+2]==reg&&((regs[i+1].wasconst>>hr)&1))
4132 #ifdef HOST_IMM_ADDR32
4133 if(!using_tlb||((signed int)constmap[i][hr]+imm[i+2])<(signed int)0xC0000000) return 0;
4136 if((signed int)constmap[i][hr]+imm[i+2]<(signed int)0x80800000)
4137 *value=constmap[i][hr]+imm[i+2]+(int)rdram-0x80000000;
4140 // Precompute load address
4141 *value=constmap[i][hr]+imm[i+2];
4145 if(itype[i+1]==LOAD&&rs1[i+1]==reg&&rt1[i+1]==reg)
4147 #ifdef HOST_IMM_ADDR32
4148 if(!using_tlb||((signed int)constmap[i][hr]+imm[i+1])<(signed int)0xC0000000) return 0;
4151 if((signed int)constmap[i][hr]+imm[i+1]<(signed int)0x80800000)
4152 *value=constmap[i][hr]+imm[i+1]+(int)rdram-0x80000000;
4155 // Precompute load address
4156 *value=constmap[i][hr]+imm[i+1];
4157 //DebugMessage(M64MSG_VERBOSE, "c=%x imm=%x",(int)constmap[i][hr],imm[i+1]);
4162 *value=constmap[i][hr];
4163 //DebugMessage(M64MSG_VERBOSE, "c=%x",(int)constmap[i][hr]);
4164 if(i==slen-1) return 1;
4166 return !((unneeded_reg[i+1]>>reg)&1);
4168 return !((unneeded_reg_upper[i+1]>>reg)&1);
4172 // Load registers with known constants
4173 static void load_consts(signed char pre[],signed char regmap[],int is32,int i)
4177 for(hr=0;hr<HOST_REGS;hr++) {
4178 if(hr!=EXCLUDE_REG&®map[hr]>=0) {
4179 //if(entry[hr]!=regmap[hr]) {
4180 if(i==0||!((regs[i-1].isconst>>hr)&1)||pre[hr]!=regmap[hr]||bt[i]) {
4181 if(((regs[i].isconst>>hr)&1)&®map[hr]<64&®map[hr]>0) {
4183 if(get_final_value(hr,i,&value)) {
4188 emit_movimm(value,hr);
4196 for(hr=0;hr<HOST_REGS;hr++) {
4197 if(hr!=EXCLUDE_REG&®map[hr]>=0) {
4198 //if(entry[hr]!=regmap[hr]) {
4199 if(i==0||!((regs[i-1].isconst>>hr)&1)||pre[hr]!=regmap[hr]||bt[i]) {
4200 if(((regs[i].isconst>>hr)&1)&®map[hr]>64) {
4201 if((is32>>(regmap[hr]&63))&1) {
4202 int lr=get_reg(regmap,regmap[hr]-64);
4204 emit_sarimm(lr,31,hr);
4209 if(get_final_value(hr,i,&value)) {
4214 emit_movimm(value,hr);
4223 static void load_all_consts(signed char regmap[],int is32,u_int dirty,int i)
4227 for(hr=0;hr<HOST_REGS;hr++) {
4228 if(hr!=EXCLUDE_REG&®map[hr]>=0&&((dirty>>hr)&1)) {
4229 if(((regs[i].isconst>>hr)&1)&®map[hr]<64&®map[hr]>0) {
4230 int value=constmap[i][hr];
4235 emit_movimm(value,hr);
4241 for(hr=0;hr<HOST_REGS;hr++) {
4242 if(hr!=EXCLUDE_REG&®map[hr]>=0&&((dirty>>hr)&1)) {
4243 if(((regs[i].isconst>>hr)&1)&®map[hr]>64) {
4244 if((is32>>(regmap[hr]&63))&1) {
4245 int lr=get_reg(regmap,regmap[hr]-64);
4247 emit_sarimm(lr,31,hr);
4251 int value=constmap[i][hr];
4256 emit_movimm(value,hr);
4264 // Write out all dirty registers (except cycle count)
4265 static void wb_dirtys(signed char i_regmap[],uint64_t i_is32,uint64_t i_dirty)
4268 for(hr=0;hr<HOST_REGS;hr++) {
4269 if(hr!=EXCLUDE_REG) {
4270 if(i_regmap[hr]>0) {
4271 if(i_regmap[hr]!=CCREG) {
4272 if((i_dirty>>hr)&1) {
4273 if(i_regmap[hr]<64) {
4274 emit_storereg(i_regmap[hr],hr);
4275 if( ((i_is32>>i_regmap[hr])&1) ) {
4276 #ifdef DESTRUCTIVE_WRITEBACK
4277 emit_sarimm(hr,31,hr);
4278 emit_storereg(i_regmap[hr]|64,hr);
4280 emit_sarimm(hr,31,HOST_TEMPREG);
4281 emit_storereg(i_regmap[hr]|64,HOST_TEMPREG);
4285 if( !((i_is32>>(i_regmap[hr]&63))&1) ) {
4286 emit_storereg(i_regmap[hr],hr);
4295 // Write out dirty registers that we need to reload (pair with load_needed_regs)
4296 // This writes the registers not written by store_regs_bt
4297 static void wb_needed_dirtys(signed char i_regmap[],uint64_t i_is32,uint64_t i_dirty,int addr)
4300 int t=(addr-start)>>2;
4301 for(hr=0;hr<HOST_REGS;hr++) {
4302 if(hr!=EXCLUDE_REG) {
4303 if(i_regmap[hr]>0) {
4304 if(i_regmap[hr]!=CCREG) {
4305 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)) {
4306 if((i_dirty>>hr)&1) {
4307 if(i_regmap[hr]<64) {
4308 emit_storereg(i_regmap[hr],hr);
4309 if( ((i_is32>>i_regmap[hr])&1) ) {
4310 #ifdef DESTRUCTIVE_WRITEBACK
4311 emit_sarimm(hr,31,hr);
4312 emit_storereg(i_regmap[hr]|64,hr);
4314 emit_sarimm(hr,31,HOST_TEMPREG);
4315 emit_storereg(i_regmap[hr]|64,HOST_TEMPREG);
4319 if( !((i_is32>>(i_regmap[hr]&63))&1) ) {
4320 emit_storereg(i_regmap[hr],hr);
4331 // Load all registers (except cycle count)
4332 static void load_all_regs(signed char i_regmap[])
4335 for(hr=0;hr<HOST_REGS;hr++) {
4336 if(hr!=EXCLUDE_REG) {
4337 if(i_regmap[hr]==0) {
4341 if(i_regmap[hr]>0 && (i_regmap[hr]&63)<TEMPREG && i_regmap[hr]!=CCREG)
4343 emit_loadreg(i_regmap[hr],hr);
4349 // Load all current registers also needed by next instruction
4350 static void load_needed_regs(signed char i_regmap[],signed char next_regmap[])
4353 for(hr=0;hr<HOST_REGS;hr++) {
4354 if(hr!=EXCLUDE_REG) {
4355 if(get_reg(next_regmap,i_regmap[hr])>=0) {
4356 if(i_regmap[hr]==0) {
4360 if(i_regmap[hr]>0 && (i_regmap[hr]&63)<TEMPREG && i_regmap[hr]!=CCREG)
4362 emit_loadreg(i_regmap[hr],hr);
4369 // Load all regs, storing cycle count if necessary
4370 static void load_regs_entry(int t)
4373 if(is_ds[t]) emit_addimm(HOST_CCREG,CLOCK_DIVIDER,HOST_CCREG);
4374 else if(ccadj[t]) emit_addimm(HOST_CCREG,-ccadj[t]*CLOCK_DIVIDER,HOST_CCREG);
4375 if(regs[t].regmap_entry[HOST_CCREG]!=CCREG) {
4376 emit_storereg(CCREG,HOST_CCREG);
4379 for(hr=0;hr<HOST_REGS;hr++) {
4380 if(regs[t].regmap_entry[hr]>=0&®s[t].regmap_entry[hr]<TEMPREG) {
4381 if(regs[t].regmap_entry[hr]==0) {
4384 else if(regs[t].regmap_entry[hr]!=CCREG)
4386 emit_loadreg(regs[t].regmap_entry[hr],hr);
4391 for(hr=0;hr<HOST_REGS;hr++) {
4392 if(regs[t].regmap_entry[hr]>=64&®s[t].regmap_entry[hr]<TEMPREG+64) {
4393 assert(regs[t].regmap_entry[hr]!=64);
4394 if((regs[t].was32>>(regs[t].regmap_entry[hr]&63))&1) {
4395 int lr=get_reg(regs[t].regmap_entry,regs[t].regmap_entry[hr]-64);
4397 emit_loadreg(regs[t].regmap_entry[hr],hr);
4401 emit_sarimm(lr,31,hr);
4406 emit_loadreg(regs[t].regmap_entry[hr],hr);
4412 // Store dirty registers prior to branch
4413 static void store_regs_bt(signed char i_regmap[],uint64_t i_is32,uint64_t i_dirty,int addr)
4415 if(internal_branch(i_is32,addr))
4417 int t=(addr-start)>>2;
4419 for(hr=0;hr<HOST_REGS;hr++) {
4420 if(hr!=EXCLUDE_REG) {
4421 if(i_regmap[hr]>0 && i_regmap[hr]!=CCREG) {
4422 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)) {
4423 if((i_dirty>>hr)&1) {
4424 if(i_regmap[hr]<64) {
4425 if(!((unneeded_reg[t]>>i_regmap[hr])&1)) {
4426 emit_storereg(i_regmap[hr],hr);
4427 if( ((i_is32>>i_regmap[hr])&1) && !((unneeded_reg_upper[t]>>i_regmap[hr])&1) ) {
4428 #ifdef DESTRUCTIVE_WRITEBACK
4429 emit_sarimm(hr,31,hr);
4430 emit_storereg(i_regmap[hr]|64,hr);
4432 emit_sarimm(hr,31,HOST_TEMPREG);
4433 emit_storereg(i_regmap[hr]|64,HOST_TEMPREG);
4438 if( !((i_is32>>(i_regmap[hr]&63))&1) && !((unneeded_reg_upper[t]>>(i_regmap[hr]&63))&1) ) {
4439 emit_storereg(i_regmap[hr],hr);
4450 // Branch out of this block, write out all dirty regs
4451 wb_dirtys(i_regmap,i_is32,i_dirty);
4455 // Load all needed registers for branch target
4456 static void load_regs_bt(signed char i_regmap[],uint64_t i_is32,uint64_t i_dirty,int addr)
4458 //if(addr>=start && addr<(start+slen*4))
4459 if(internal_branch(i_is32,addr))
4461 int t=(addr-start)>>2;
4463 // Store the cycle count before loading something else
4464 if(i_regmap[HOST_CCREG]!=CCREG) {
4465 assert(i_regmap[HOST_CCREG]==-1);
4467 if(regs[t].regmap_entry[HOST_CCREG]!=CCREG) {
4468 emit_storereg(CCREG,HOST_CCREG);
4471 for(hr=0;hr<HOST_REGS;hr++) {
4472 if(hr!=EXCLUDE_REG&®s[t].regmap_entry[hr]>=0&®s[t].regmap_entry[hr]<TEMPREG) {
4473 #ifdef DESTRUCTIVE_WRITEBACK
4474 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)) {
4476 if(i_regmap[hr]!=regs[t].regmap_entry[hr] ) {
4478 if(regs[t].regmap_entry[hr]==0) {
4481 else if(regs[t].regmap_entry[hr]!=CCREG)
4483 emit_loadreg(regs[t].regmap_entry[hr],hr);
4489 for(hr=0;hr<HOST_REGS;hr++) {
4490 if(hr!=EXCLUDE_REG&®s[t].regmap_entry[hr]>=64&®s[t].regmap_entry[hr]<TEMPREG+64) {
4491 if(i_regmap[hr]!=regs[t].regmap_entry[hr]) {
4492 assert(regs[t].regmap_entry[hr]!=64);
4493 if((i_is32>>(regs[t].regmap_entry[hr]&63))&1) {
4494 int lr=get_reg(regs[t].regmap_entry,regs[t].regmap_entry[hr]-64);
4496 emit_loadreg(regs[t].regmap_entry[hr],hr);
4500 emit_sarimm(lr,31,hr);
4505 emit_loadreg(regs[t].regmap_entry[hr],hr);
4508 else if((i_is32>>(regs[t].regmap_entry[hr]&63))&1) {
4509 int lr=get_reg(regs[t].regmap_entry,regs[t].regmap_entry[hr]-64);
4511 emit_sarimm(lr,31,hr);
4518 static int match_bt(signed char i_regmap[],uint64_t i_is32,uint64_t i_dirty,int addr)
4520 if(addr>=start && addr<start+slen*4-4)
4522 int t=(addr-start)>>2;
4524 if(regs[t].regmap_entry[HOST_CCREG]!=CCREG) return 0;
4525 for(hr=0;hr<HOST_REGS;hr++)
4529 if(i_regmap[hr]!=regs[t].regmap_entry[hr])
4531 if(regs[t].regmap_entry[hr]>=0&&(regs[t].regmap_entry[hr]|64)<TEMPREG+64)
4538 if(i_regmap[hr]<TEMPREG)
4540 if(!((unneeded_reg[t]>>i_regmap[hr])&1))
4543 else if(i_regmap[hr]>=64&&i_regmap[hr]<TEMPREG+64)
4545 if(!((unneeded_reg_upper[t]>>(i_regmap[hr]&63))&1))
4550 else // Same register but is it 32-bit or dirty?
4553 if(!((regs[t].dirty>>hr)&1))
4557 if(!((unneeded_reg[t]>>i_regmap[hr])&1))
4559 //DebugMessage(M64MSG_VERBOSE, "%x: dirty no match",addr);
4564 if((((regs[t].was32^i_is32)&~unneeded_reg_upper[t])>>(i_regmap[hr]&63))&1)
4566 //DebugMessage(M64MSG_VERBOSE, "%x: is32 no match",addr);
4572 //if(is32[t]&~unneeded_reg_upper[t]&~i_is32) return 0;
4573 if(requires_32bit[t]&~i_is32) return 0;
4574 // Delay slots are not valid branch targets
4575 //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;
4576 // Delay slots require additional processing, so do not match
4577 if(is_ds[t]) return 0;
4582 for(hr=0;hr<HOST_REGS;hr++)
4588 if(hr!=HOST_CCREG||i_regmap[hr]!=CCREG)
4602 // Used when a branch jumps into the delay slot of another branch
4603 static void ds_assemble_entry(int i)
4605 int t=(ba[i]-start)>>2;
4606 if(!instr_addr[t]) instr_addr[t]=(u_int)out;
4607 assem_debug("Assemble delay slot at %x",ba[i]);
4609 if(regs[t].regmap_entry[HOST_CCREG]==CCREG&®s[t].regmap[HOST_CCREG]!=CCREG)
4610 wb_register(CCREG,regs[t].regmap_entry,regs[t].wasdirty,regs[t].was32);
4611 load_regs(regs[t].regmap_entry,regs[t].regmap,regs[t].was32,rs1[t],rs2[t]);
4612 address_generation(t,®s[t],regs[t].regmap_entry);
4613 if(itype[t]==LOAD||itype[t]==LOADLR||itype[t]==STORE||itype[t]==STORELR||itype[t]==C1LS)
4614 load_regs(regs[t].regmap_entry,regs[t].regmap,regs[t].was32,MMREG,ROREG);
4615 if(itype[t]==STORE||itype[t]==STORELR||(opcode[t]&0x3b)==0x39)
4616 load_regs(regs[t].regmap_entry,regs[t].regmap,regs[t].was32,INVCP,INVCP);
4621 alu_assemble(t,®s[t]);break;
4623 imm16_assemble(t,®s[t]);break;
4625 shift_assemble(t,®s[t]);break;
4627 shiftimm_assemble(t,®s[t]);break;
4629 load_assemble(t,®s[t]);break;
4631 loadlr_assemble(t,®s[t]);break;
4633 store_assemble(t,®s[t]);break;
4635 storelr_assemble(t,®s[t]);break;
4637 cop0_assemble(t,®s[t]);break;
4639 cop1_assemble(t,®s[t]);break;
4641 c1ls_assemble(t,®s[t]);break;
4643 fconv_assemble(t,®s[t]);break;
4645 float_assemble(t,®s[t]);break;
4647 fcomp_assemble(t,®s[t]);break;
4649 multdiv_assemble(t,®s[t]);break;
4651 mov_assemble(t,®s[t]);break;
4659 DebugMessage(M64MSG_VERBOSE, "Jump in the delay slot. This is probably a bug.");
4661 store_regs_bt(regs[t].regmap,regs[t].is32,regs[t].dirty,ba[i]+4);
4662 load_regs_bt(regs[t].regmap,regs[t].is32,regs[t].dirty,ba[i]+4);
4663 if(internal_branch(regs[t].is32,ba[i]+4))
4664 assem_debug("branch: internal");
4666 assem_debug("branch: external");
4667 assert(internal_branch(regs[t].is32,ba[i]+4));
4668 add_to_linker((int)out,ba[i]+4,internal_branch(regs[t].is32,ba[i]+4));
4672 static void do_cc(int i,signed char i_regmap[],int *adj,int addr,int taken,int invert)
4681 //if(ba[i]>=start && ba[i]<(start+slen*4))
4682 if(internal_branch(branch_regs[i].is32,ba[i]))
4684 int t=(ba[i]-start)>>2;
4685 if(is_ds[t]) *adj=-1; // Branch into delay slot adds an extra cycle
4693 if(taken==TAKEN && i==(ba[i]-start)>>2 && source[i+1]==0) {
4695 if(count&1) emit_addimm_and_set_flags(2*(count+2),HOST_CCREG);
4697 //emit_subfrommem(&idlecount,HOST_CCREG); // Count idle cycles
4698 emit_andimm(HOST_CCREG,3,HOST_CCREG);
4702 else if(*adj==0||invert) {
4703 emit_addimm_and_set_flags(CLOCK_DIVIDER*(count+2),HOST_CCREG);
4709 emit_cmpimm(HOST_CCREG,-CLOCK_DIVIDER*(count+2));
4713 add_stub(CC_STUB,jaddr,idle?idle:(int)out,(*adj==0||invert||idle)?0:(count+2),i,addr,taken,0);
4716 static void do_ccstub(int n)
4719 assem_debug("do_ccstub %x",start+stubs[n][4]*4);
4720 set_jump_target(stubs[n][1],(int)out);
4722 if(stubs[n][6]==NULLDS) {
4723 // Delay slot instruction is nullified ("likely" branch)
4724 wb_dirtys(regs[i].regmap,regs[i].is32,regs[i].dirty);
4726 else if(stubs[n][6]!=TAKEN) {
4727 wb_dirtys(branch_regs[i].regmap,branch_regs[i].is32,branch_regs[i].dirty);
4730 if(internal_branch(branch_regs[i].is32,ba[i]))
4731 wb_needed_dirtys(branch_regs[i].regmap,branch_regs[i].is32,branch_regs[i].dirty,ba[i]);
4735 // Save PC as return address
4736 emit_movimm(stubs[n][5],EAX);
4737 emit_writeword(EAX,(int)&pcaddr);
4741 // Return address depends on which way the branch goes
4742 if(itype[i]==CJUMP||itype[i]==SJUMP||itype[i]==FJUMP)
4744 int s1l=get_reg(branch_regs[i].regmap,rs1[i]);
4745 int s1h=get_reg(branch_regs[i].regmap,rs1[i]|64);
4746 int s2l=get_reg(branch_regs[i].regmap,rs2[i]);
4747 int s2h=get_reg(branch_regs[i].regmap,rs2[i]|64);
4757 if((branch_regs[i].is32>>rs1[i])&(branch_regs[i].is32>>rs2[i])&1) {
4761 #ifdef DESTRUCTIVE_WRITEBACK
4763 if((branch_regs[i].dirty>>s1l)&(branch_regs[i].is32>>rs1[i])&1)
4764 emit_loadreg(rs1[i],s1l);
4767 if((branch_regs[i].dirty>>s1l)&(branch_regs[i].is32>>rs2[i])&1)
4768 emit_loadreg(rs2[i],s1l);
4771 if((branch_regs[i].dirty>>s2l)&(branch_regs[i].is32>>rs2[i])&1)
4772 emit_loadreg(rs2[i],s2l);
4775 int addr,alt,ntaddr;
4778 if(hr!=EXCLUDE_REG && hr!=HOST_CCREG &&
4779 (branch_regs[i].regmap[hr]&63)!=rs1[i] &&
4780 (branch_regs[i].regmap[hr]&63)!=rs2[i] )
4788 if(hr!=EXCLUDE_REG && hr!=HOST_CCREG &&
4789 (branch_regs[i].regmap[hr]&63)!=rs1[i] &&
4790 (branch_regs[i].regmap[hr]&63)!=rs2[i] )
4796 if((opcode[i]&0x2E)==6) // BLEZ/BGTZ needs another register
4800 if(hr!=EXCLUDE_REG && hr!=HOST_CCREG &&
4801 (branch_regs[i].regmap[hr]&63)!=rs1[i] &&
4802 (branch_regs[i].regmap[hr]&63)!=rs2[i] )
4808 assert(hr<HOST_REGS);
4810 if((opcode[i]&0x2f)==4) // BEQ
4812 #ifdef HAVE_CMOV_IMM
4814 if(s2l>=0) emit_cmp(s1l,s2l);
4815 else emit_test(s1l,s1l);
4816 emit_cmov2imm_e_ne_compact(ba[i],start+i*4+8,addr);
4821 emit_mov2imm_compact(ba[i],addr,start+i*4+8,alt);
4823 if(s2h>=0) emit_cmp(s1h,s2h);
4824 else emit_test(s1h,s1h);
4825 emit_cmovne_reg(alt,addr);
4827 if(s2l>=0) emit_cmp(s1l,s2l);
4828 else emit_test(s1l,s1l);
4829 emit_cmovne_reg(alt,addr);
4832 if((opcode[i]&0x2f)==5) // BNE
4834 #ifdef HAVE_CMOV_IMM
4836 if(s2l>=0) emit_cmp(s1l,s2l);
4837 else emit_test(s1l,s1l);
4838 emit_cmov2imm_e_ne_compact(start+i*4+8,ba[i],addr);
4843 emit_mov2imm_compact(start+i*4+8,addr,ba[i],alt);
4845 if(s2h>=0) emit_cmp(s1h,s2h);
4846 else emit_test(s1h,s1h);
4847 emit_cmovne_reg(alt,addr);
4849 if(s2l>=0) emit_cmp(s1l,s2l);
4850 else emit_test(s1l,s1l);
4851 emit_cmovne_reg(alt,addr);
4854 if((opcode[i]&0x2f)==6) // BLEZ
4856 //emit_movimm(ba[i],alt);
4857 //emit_movimm(start+i*4+8,addr);
4858 emit_mov2imm_compact(ba[i],alt,start+i*4+8,addr);
4860 if(s1h>=0) emit_mov(addr,ntaddr);
4861 emit_cmovl_reg(alt,addr);
4864 emit_cmovne_reg(ntaddr,addr);
4865 emit_cmovs_reg(alt,addr);
4868 if((opcode[i]&0x2f)==7) // BGTZ
4870 //emit_movimm(ba[i],addr);
4871 //emit_movimm(start+i*4+8,ntaddr);
4872 emit_mov2imm_compact(ba[i],addr,start+i*4+8,ntaddr);
4874 if(s1h>=0) emit_mov(addr,alt);
4875 emit_cmovl_reg(ntaddr,addr);
4878 emit_cmovne_reg(alt,addr);
4879 emit_cmovs_reg(ntaddr,addr);
4882 if((opcode[i]==1)&&(opcode2[i]&0x2D)==0) // BLTZ
4884 //emit_movimm(ba[i],alt);
4885 //emit_movimm(start+i*4+8,addr);
4886 emit_mov2imm_compact(ba[i],alt,start+i*4+8,addr);
4887 if(s1h>=0) emit_test(s1h,s1h);
4888 else emit_test(s1l,s1l);
4889 emit_cmovs_reg(alt,addr);
4891 if((opcode[i]==1)&&(opcode2[i]&0x2D)==1) // BGEZ
4893 //emit_movimm(ba[i],addr);
4894 //emit_movimm(start+i*4+8,alt);
4895 emit_mov2imm_compact(ba[i],addr,start+i*4+8,alt);
4896 if(s1h>=0) emit_test(s1h,s1h);
4897 else emit_test(s1l,s1l);
4898 emit_cmovs_reg(alt,addr);
4900 if(opcode[i]==0x11 && opcode2[i]==0x08 ) {
4901 if(source[i]&0x10000) // BC1T
4903 //emit_movimm(ba[i],alt);
4904 //emit_movimm(start+i*4+8,addr);
4905 emit_mov2imm_compact(ba[i],alt,start+i*4+8,addr);
4906 emit_testimm(s1l,0x800000);
4907 emit_cmovne_reg(alt,addr);
4911 //emit_movimm(ba[i],addr);
4912 //emit_movimm(start+i*4+8,alt);
4913 emit_mov2imm_compact(ba[i],addr,start+i*4+8,alt);
4914 emit_testimm(s1l,0x800000);
4915 emit_cmovne_reg(alt,addr);
4918 emit_writeword(addr,(int)&pcaddr);
4923 int r=get_reg(branch_regs[i].regmap,rs1[i]);
4924 if(rs1[i]==rt1[i+1]||rs1[i]==rt2[i+1]) {
4925 r=get_reg(branch_regs[i].regmap,RTEMP);
4927 emit_writeword(r,(int)&pcaddr);
4929 else {DebugMessage(M64MSG_ERROR, "Unknown branch type in do_ccstub");exit(1);}
4931 // Update cycle count
4932 assert(branch_regs[i].regmap[HOST_CCREG]==CCREG||branch_regs[i].regmap[HOST_CCREG]==-1);
4933 if(stubs[n][3]) emit_addimm(HOST_CCREG,CLOCK_DIVIDER*stubs[n][3],HOST_CCREG);
4934 emit_call((int)cc_interrupt);
4935 if(stubs[n][3]) emit_addimm(HOST_CCREG,-CLOCK_DIVIDER*stubs[n][3],HOST_CCREG);
4936 if(stubs[n][6]==TAKEN) {
4937 if(internal_branch(branch_regs[i].is32,ba[i]))
4938 load_needed_regs(branch_regs[i].regmap,regs[(ba[i]-start)>>2].regmap_entry);
4939 else if(itype[i]==RJUMP) {
4940 if(get_reg(branch_regs[i].regmap,RTEMP)>=0)
4941 emit_readword((int)&pcaddr,get_reg(branch_regs[i].regmap,RTEMP));
4943 emit_loadreg(rs1[i],get_reg(branch_regs[i].regmap,rs1[i]));
4945 }else if(stubs[n][6]==NOTTAKEN) {
4946 if(i<slen-2) load_needed_regs(branch_regs[i].regmap,regmap_pre[i+2]);
4947 else load_all_regs(branch_regs[i].regmap);
4948 }else if(stubs[n][6]==NULLDS) {
4949 // Delay slot instruction is nullified ("likely" branch)
4950 if(i<slen-2) load_needed_regs(regs[i].regmap,regmap_pre[i+2]);
4951 else load_all_regs(regs[i].regmap);
4953 load_all_regs(branch_regs[i].regmap);
4955 emit_jmp(stubs[n][2]); // return address
4957 /* This works but uses a lot of memory...
4958 emit_readword((int)&last_count,ECX);
4959 emit_add(HOST_CCREG,ECX,EAX);
4960 emit_writeword(EAX,(int)&Count);
4961 emit_call((int)gen_interupt);
4962 emit_readword((int)&Count,HOST_CCREG);
4963 emit_readword((int)&next_interupt,EAX);
4964 emit_readword((int)&pending_exception,EBX);
4965 emit_writeword(EAX,(int)&last_count);
4966 emit_sub(HOST_CCREG,EAX,HOST_CCREG);
4968 int jne_instr=(int)out;
4970 if(stubs[n][3]) emit_addimm(HOST_CCREG,-2*stubs[n][3],HOST_CCREG);
4971 load_all_regs(branch_regs[i].regmap);
4972 emit_jmp(stubs[n][2]); // return address
4973 set_jump_target(jne_instr,(int)out);
4974 emit_readword((int)&pcaddr,EAX);
4975 // Call get_addr_ht instead of doing the hash table here.
4976 // This code is executed infrequently and takes up a lot of space
4977 // so smaller is better.
4978 emit_storereg(CCREG,HOST_CCREG);
4980 emit_call((int)get_addr_ht);
4981 emit_loadreg(CCREG,HOST_CCREG);
4982 emit_addimm(ESP,4,ESP);
4986 static void add_to_linker(int addr,int target,int ext)
4988 link_addr[linkcount][0]=addr;
4989 link_addr[linkcount][1]=target;
4990 link_addr[linkcount][2]=ext;
4994 static void ujump_assemble(int i,struct regstat *i_regs)
4997 signed char *i_regmap=i_regs->regmap;
4999 if(i==(ba[i]-start)>>2) assem_debug("idle loop");
5000 address_generation(i+1,i_regs,regs[i].regmap_entry);
5002 int temp=get_reg(branch_regs[i].regmap,PTEMP);
5003 if(rt1[i]==31&&temp>=0)
5005 int return_address=start+i*4+8;
5006 if(get_reg(branch_regs[i].regmap,31)>0)
5007 if(i_regmap[temp]==PTEMP) emit_movimm((int)hash_table[((return_address>>16)^return_address)&0xFFFF],temp);
5010 ds_assemble(i+1,i_regs);
5011 uint64_t bc_unneeded=branch_regs[i].u;
5012 uint64_t bc_unneeded_upper=branch_regs[i].uu;
5013 bc_unneeded|=1|(1LL<<rt1[i]);
5014 bc_unneeded_upper|=1|(1LL<<rt1[i]);
5015 wb_invalidate(regs[i].regmap,branch_regs[i].regmap,regs[i].dirty,regs[i].is32,
5016 bc_unneeded,bc_unneeded_upper);
5017 load_regs(regs[i].regmap,branch_regs[i].regmap,regs[i].was32,CCREG,CCREG);
5020 unsigned int return_address;
5021 assert(rt1[i+1]!=31);
5022 assert(rt2[i+1]!=31);
5023 rt=get_reg(branch_regs[i].regmap,31);
5024 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]);
5026 return_address=start+i*4+8;
5029 if(internal_branch(branch_regs[i].is32,return_address)) {
5031 if(temp==EXCLUDE_REG||temp>=HOST_REGS||
5032 branch_regs[i].regmap[temp]>=0)
5034 temp=get_reg(branch_regs[i].regmap,-1);
5037 if(temp<0) temp=HOST_TEMPREG;
5039 if(temp>=0) do_miniht_insert(return_address,rt,temp);
5040 else emit_movimm(return_address,rt);
5048 if(i_regmap[temp]!=PTEMP) emit_movimm((int)hash_table[((return_address>>16)^return_address)&0xFFFF],temp);
5051 emit_movimm(return_address,rt); // PC into link register
5053 emit_prefetch(hash_table[((return_address>>16)^return_address)&0xFFFF]);
5059 cc=get_reg(branch_regs[i].regmap,CCREG);
5060 assert(cc==HOST_CCREG);
5061 store_regs_bt(branch_regs[i].regmap,branch_regs[i].is32,branch_regs[i].dirty,ba[i]);
5063 if(rt1[i]==31&&temp>=0) emit_prefetchreg(temp);
5065 do_cc(i,branch_regs[i].regmap,&adj,ba[i],TAKEN,0);
5066 if(adj) emit_addimm(cc,CLOCK_DIVIDER*(ccadj[i]+2-adj),cc);
5067 load_regs_bt(branch_regs[i].regmap,branch_regs[i].is32,branch_regs[i].dirty,ba[i]);
5068 if(internal_branch(branch_regs[i].is32,ba[i]))
5069 assem_debug("branch: internal");
5071 assem_debug("branch: external");
5072 if(internal_branch(branch_regs[i].is32,ba[i])&&is_ds[(ba[i]-start)>>2]) {
5073 ds_assemble_entry(i);
5076 add_to_linker((int)out,ba[i],internal_branch(branch_regs[i].is32,ba[i]));
5081 static void rjump_assemble(int i,struct regstat *i_regs)
5084 signed char *i_regmap=i_regs->regmap;
5088 rs=get_reg(branch_regs[i].regmap,rs1[i]);
5090 if(rs1[i]==rt1[i+1]||rs1[i]==rt2[i+1]) {
5091 // Delay slot abuse, make a copy of the branch address register
5092 temp=get_reg(branch_regs[i].regmap,RTEMP);
5094 assert(regs[i].regmap[temp]==RTEMP);
5098 address_generation(i+1,i_regs,regs[i].regmap_entry);
5102 if((temp=get_reg(branch_regs[i].regmap,PTEMP))>=0) {
5103 int return_address=start+i*4+8;
5104 if(i_regmap[temp]==PTEMP) emit_movimm((int)hash_table[((return_address>>16)^return_address)&0xFFFF],temp);
5110 int rh=get_reg(regs[i].regmap,RHASH);
5111 if(rh>=0) do_preload_rhash(rh);
5114 ds_assemble(i+1,i_regs);
5115 uint64_t bc_unneeded=branch_regs[i].u;
5116 uint64_t bc_unneeded_upper=branch_regs[i].uu;
5117 bc_unneeded|=1|(1LL<<rt1[i]);
5118 bc_unneeded_upper|=1|(1LL<<rt1[i]);
5119 bc_unneeded&=~(1LL<<rs1[i]);
5120 wb_invalidate(regs[i].regmap,branch_regs[i].regmap,regs[i].dirty,regs[i].is32,
5121 bc_unneeded,bc_unneeded_upper);
5122 load_regs(regs[i].regmap,branch_regs[i].regmap,regs[i].was32,rs1[i],CCREG);
5124 int rt,return_address;
5125 assert(rt1[i+1]!=rt1[i]);
5126 assert(rt2[i+1]!=rt1[i]);
5127 rt=get_reg(branch_regs[i].regmap,rt1[i]);
5128 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]);
5130 return_address=start+i*4+8;
5134 if(i_regmap[temp]!=PTEMP) emit_movimm((int)hash_table[((return_address>>16)^return_address)&0xFFFF],temp);
5137 emit_movimm(return_address,rt); // PC into link register
5139 emit_prefetch(hash_table[((return_address>>16)^return_address)&0xFFFF]);
5142 cc=get_reg(branch_regs[i].regmap,CCREG);
5143 assert(cc==HOST_CCREG);
5145 int rh=get_reg(branch_regs[i].regmap,RHASH);
5146 int ht=get_reg(branch_regs[i].regmap,RHTBL);
5148 if(regs[i].regmap[rh]!=RHASH) do_preload_rhash(rh);
5149 do_preload_rhtbl(ht);
5153 store_regs_bt(branch_regs[i].regmap,branch_regs[i].is32,branch_regs[i].dirty,-1);
5154 #ifdef DESTRUCTIVE_WRITEBACK
5155 if((branch_regs[i].dirty>>rs)&(branch_regs[i].is32>>rs1[i])&1) {
5156 if(rs1[i]!=rt1[i+1]&&rs1[i]!=rt2[i+1]) {
5157 emit_loadreg(rs1[i],rs);
5162 if(rt1[i]==31&&temp>=0) emit_prefetchreg(temp);
5166 do_miniht_load(ht,rh);
5169 //do_cc(i,branch_regs[i].regmap,&adj,-1,TAKEN);
5170 //if(adj) emit_addimm(cc,2*(ccadj[i]+2-adj),cc); // ??? - Shouldn't happen
5172 emit_addimm_and_set_flags(CLOCK_DIVIDER*(ccadj[i]+2),HOST_CCREG);
5173 add_stub(CC_STUB,(int)out,jump_vaddr_reg[rs],0,i,-1,TAKEN,0);
5175 //load_regs_bt(branch_regs[i].regmap,branch_regs[i].is32,branch_regs[i].dirty,-1);
5178 do_miniht_jump(rs,rh,ht);
5183 //if(rs!=EAX) emit_mov(rs,EAX);
5184 //emit_jmp((int)jump_vaddr_eax);
5185 emit_jmp(jump_vaddr_reg[rs]);
5190 emit_shrimm(rs,16,rs);
5191 emit_xor(temp,rs,rs);
5192 emit_movzwl_reg(rs,rs);
5193 emit_shlimm(rs,4,rs);
5194 emit_cmpmem_indexed((int)hash_table,rs,temp);
5195 emit_jne((int)out+14);
5196 emit_readword_indexed((int)hash_table+4,rs,rs);
5198 emit_cmpmem_indexed((int)hash_table+8,rs,temp);
5199 emit_addimm_no_flags(8,rs);
5200 emit_jeq((int)out-17);
5201 // No hit on hash table, call compiler
5204 #ifdef DEBUG_CYCLE_COUNT
5205 emit_readword((int)&last_count,ECX);
5206 emit_add(HOST_CCREG,ECX,HOST_CCREG);
5207 emit_readword((int)&next_interupt,ECX);
5208 emit_writeword(HOST_CCREG,(int)&Count);
5209 emit_sub(HOST_CCREG,ECX,HOST_CCREG);
5210 emit_writeword(ECX,(int)&last_count);
5213 emit_storereg(CCREG,HOST_CCREG);
5214 emit_call((int)get_addr);
5215 emit_loadreg(CCREG,HOST_CCREG);
5216 emit_addimm(ESP,4,ESP);
5218 #ifdef CORTEX_A8_BRANCH_PREDICTION_HACK
5219 if(rt1[i]!=31&&i<slen-2&&(((u_int)out)&7)) emit_mov(13,13);
5223 static void cjump_assemble(int i,struct regstat *i_regs)
5225 signed char *i_regmap=i_regs->regmap;
5228 match=match_bt(branch_regs[i].regmap,branch_regs[i].is32,branch_regs[i].dirty,ba[i]);
5229 assem_debug("match=%d",match);
5230 int s1h,s1l,s2h,s2l;
5231 int prev_cop1_usable=cop1_usable;
5232 int unconditional=0,nop=0;
5235 int internal=internal_branch(branch_regs[i].is32,ba[i]);
5236 if(i==(ba[i]-start)>>2) assem_debug("idle loop");
5237 if(!match) invert=1;
5238 #ifdef CORTEX_A8_BRANCH_PREDICTION_HACK
5239 if(i>(ba[i]-start)>>2) invert=1;
5243 s1l=get_reg(branch_regs[i].regmap,rs1[i]);
5244 s1h=get_reg(branch_regs[i].regmap,rs1[i]|64);
5245 s2l=get_reg(branch_regs[i].regmap,rs2[i]);
5246 s2h=get_reg(branch_regs[i].regmap,rs2[i]|64);
5249 s1l=get_reg(i_regmap,rs1[i]);
5250 s1h=get_reg(i_regmap,rs1[i]|64);
5251 s2l=get_reg(i_regmap,rs2[i]);
5252 s2h=get_reg(i_regmap,rs2[i]|64);
5254 if(rs1[i]==0&&rs2[i]==0)
5256 if(opcode[i]&1) nop=1;
5257 else unconditional=1;
5258 //assert(opcode[i]!=5);
5259 //assert(opcode[i]!=7);
5260 //assert(opcode[i]!=0x15);
5261 //assert(opcode[i]!=0x17);
5267 only32=(regs[i].was32>>rs2[i])&1;
5272 only32=(regs[i].was32>>rs1[i])&1;
5275 only32=(regs[i].was32>>rs1[i])&(regs[i].was32>>rs2[i])&1;
5279 // Out of order execution (delay slot first)
5280 //DebugMessage(M64MSG_VERBOSE, "OOOE");
5281 address_generation(i+1,i_regs,regs[i].regmap_entry);
5282 ds_assemble(i+1,i_regs);
5284 uint64_t bc_unneeded=branch_regs[i].u;
5285 uint64_t bc_unneeded_upper=branch_regs[i].uu;
5286 bc_unneeded&=~((1LL<<rs1[i])|(1LL<<rs2[i]));
5287 bc_unneeded_upper&=~((1LL<<us1[i])|(1LL<<us2[i]));
5289 bc_unneeded_upper|=1;
5290 wb_invalidate(regs[i].regmap,branch_regs[i].regmap,regs[i].dirty,regs[i].is32,
5291 bc_unneeded,bc_unneeded_upper);
5292 load_regs(regs[i].regmap,branch_regs[i].regmap,regs[i].was32,rs1[i],rs2[i]);
5293 load_regs(regs[i].regmap,branch_regs[i].regmap,regs[i].was32,CCREG,CCREG);
5294 cc=get_reg(branch_regs[i].regmap,CCREG);
5295 assert(cc==HOST_CCREG);
5297 store_regs_bt(branch_regs[i].regmap,branch_regs[i].is32,branch_regs[i].dirty,ba[i]);
5298 //do_cc(i,branch_regs[i].regmap,&adj,unconditional?ba[i]:-1,unconditional);
5299 //assem_debug("cycle count (adj)");
5301 do_cc(i,branch_regs[i].regmap,&adj,ba[i],TAKEN,0);
5302 if(i!=(ba[i]-start)>>2 || source[i+1]!=0) {
5303 if(adj) emit_addimm(cc,CLOCK_DIVIDER*(ccadj[i]+2-adj),cc);
5304 load_regs_bt(branch_regs[i].regmap,branch_regs[i].is32,branch_regs[i].dirty,ba[i]);
5306 assem_debug("branch: internal");
5308 assem_debug("branch: external");
5309 if(internal&&is_ds[(ba[i]-start)>>2]) {
5310 ds_assemble_entry(i);
5313 add_to_linker((int)out,ba[i],internal);
5316 #ifdef CORTEX_A8_BRANCH_PREDICTION_HACK
5317 if(((u_int)out)&7) emit_addnop(0);
5322 emit_addimm_and_set_flags(CLOCK_DIVIDER*(ccadj[i]+2),cc);
5325 add_stub(CC_STUB,jaddr,(int)out,0,i,start+i*4+8,NOTTAKEN,0);
5328 int taken=0,nottaken=0,nottaken1=0;
5329 do_cc(i,branch_regs[i].regmap,&adj,-1,0,invert);
5330 if(adj&&!invert) emit_addimm(cc,CLOCK_DIVIDER*(ccadj[i]+2-adj),cc);
5334 if(opcode[i]==4) // BEQ
5336 if(s2h>=0) emit_cmp(s1h,s2h);
5337 else emit_test(s1h,s1h);
5341 if(opcode[i]==5) // BNE
5343 if(s2h>=0) emit_cmp(s1h,s2h);
5344 else emit_test(s1h,s1h);
5345 if(invert) taken=(int)out;
5346 else add_to_linker((int)out,ba[i],internal);
5349 if(opcode[i]==6) // BLEZ
5352 // emit_testimm(s1h,0);
5353 if(invert) taken=(int)out;
5354 else add_to_linker((int)out,ba[i],internal);
5359 if(opcode[i]==7) // BGTZ
5362 // emit_testimm(s1h,0);
5365 if(invert) taken=(int)out;
5366 else add_to_linker((int)out,ba[i],internal);
5371 //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]);
5373 if(opcode[i]==4) // BEQ
5375 if(s2l>=0) emit_cmp(s1l,s2l);
5376 else emit_test(s1l,s1l);
5381 add_to_linker((int)out,ba[i],internal);
5385 if(opcode[i]==5) // BNE
5387 if(s2l>=0) emit_cmp(s1l,s2l);
5388 else emit_test(s1l,s1l);
5393 add_to_linker((int)out,ba[i],internal);
5397 if(opcode[i]==6) // BLEZ
5404 add_to_linker((int)out,ba[i],internal);
5408 if(opcode[i]==7) // BGTZ
5415 add_to_linker((int)out,ba[i],internal);
5420 if(taken) set_jump_target(taken,(int)out);
5421 #ifdef CORTEX_A8_BRANCH_PREDICTION_HACK
5422 if(match&&(!internal||!is_ds[(ba[i]-start)>>2])) {
5424 emit_addimm(cc,-CLOCK_DIVIDER*adj,cc);
5425 add_to_linker((int)out,ba[i],internal);
5428 add_to_linker((int)out,ba[i],internal*2);
5434 if(adj) emit_addimm(cc,-CLOCK_DIVIDER*adj,cc);
5435 store_regs_bt(branch_regs[i].regmap,branch_regs[i].is32,branch_regs[i].dirty,ba[i]);
5436 load_regs_bt(branch_regs[i].regmap,branch_regs[i].is32,branch_regs[i].dirty,ba[i]);
5438 assem_debug("branch: internal");
5440 assem_debug("branch: external");
5441 if(internal&&is_ds[(ba[i]-start)>>2]) {
5442 ds_assemble_entry(i);
5445 add_to_linker((int)out,ba[i],internal);
5449 set_jump_target(nottaken,(int)out);
5452 if(nottaken1) set_jump_target(nottaken1,(int)out);
5454 if(!invert) emit_addimm(cc,CLOCK_DIVIDER*adj,cc);
5456 } // (!unconditional)
5460 // In-order execution (branch first)
5461 //if(likely[i]) DebugMessage(M64MSG_VERBOSE, "IOL");
5463 //DebugMessage(M64MSG_VERBOSE, "IOE");
5464 int taken=0,nottaken=0,nottaken1=0;
5465 if(!unconditional&&!nop) {
5469 if((opcode[i]&0x2f)==4) // BEQ
5471 if(s2h>=0) emit_cmp(s1h,s2h);
5472 else emit_test(s1h,s1h);
5476 if((opcode[i]&0x2f)==5) // BNE
5478 if(s2h>=0) emit_cmp(s1h,s2h);
5479 else emit_test(s1h,s1h);
5483 if((opcode[i]&0x2f)==6) // BLEZ
5491 if((opcode[i]&0x2f)==7) // BGTZ
5501 //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]);
5503 if((opcode[i]&0x2f)==4) // BEQ
5505 if(s2l>=0) emit_cmp(s1l,s2l);
5506 else emit_test(s1l,s1l);
5510 if((opcode[i]&0x2f)==5) // BNE
5512 if(s2l>=0) emit_cmp(s1l,s2l);
5513 else emit_test(s1l,s1l);
5517 if((opcode[i]&0x2f)==6) // BLEZ
5523 if((opcode[i]&0x2f)==7) // BGTZ
5529 } // if(!unconditional)
5531 uint64_t ds_unneeded=branch_regs[i].u;
5532 uint64_t ds_unneeded_upper=branch_regs[i].uu;
5533 ds_unneeded&=~((1LL<<rs1[i+1])|(1LL<<rs2[i+1]));
5534 ds_unneeded_upper&=~((1LL<<us1[i+1])|(1LL<<us2[i+1]));
5535 if((~ds_unneeded_upper>>rt1[i+1])&1) ds_unneeded_upper&=~((1LL<<dep1[i+1])|(1LL<<dep2[i+1]));
5537 ds_unneeded_upper|=1;
5540 if(taken) set_jump_target(taken,(int)out);
5542 wb_invalidate(regs[i].regmap,branch_regs[i].regmap,regs[i].dirty,regs[i].is32,
5543 ds_unneeded,ds_unneeded_upper);
5545 load_regs(regs[i].regmap,branch_regs[i].regmap,regs[i].was32,rs1[i+1],rs2[i+1]);
5546 address_generation(i+1,&branch_regs[i],0);
5547 load_regs(regs[i].regmap,branch_regs[i].regmap,regs[i].was32,CCREG,INVCP);
5548 ds_assemble(i+1,&branch_regs[i]);
5549 cc=get_reg(branch_regs[i].regmap,CCREG);
5551 emit_loadreg(CCREG,cc=HOST_CCREG);
5552 // CHECK: Is the following instruction (fall thru) allocated ok?
5554 assert(cc==HOST_CCREG);
5555 store_regs_bt(branch_regs[i].regmap,branch_regs[i].is32,branch_regs[i].dirty,ba[i]);
5556 do_cc(i,i_regmap,&adj,ba[i],TAKEN,0);
5557 assem_debug("cycle count (adj)");
5558 if(adj) emit_addimm(cc,CLOCK_DIVIDER*(ccadj[i]+2-adj),cc);
5559 load_regs_bt(branch_regs[i].regmap,branch_regs[i].is32,branch_regs[i].dirty,ba[i]);
5561 assem_debug("branch: internal");
5563 assem_debug("branch: external");
5564 if(internal&&is_ds[(ba[i]-start)>>2]) {
5565 ds_assemble_entry(i);
5568 add_to_linker((int)out,ba[i],internal);
5573 cop1_usable=prev_cop1_usable;
5574 if(!unconditional) {
5575 if(nottaken1) set_jump_target(nottaken1,(int)out);
5576 set_jump_target(nottaken,(int)out);
5579 wb_invalidate(regs[i].regmap,branch_regs[i].regmap,regs[i].dirty,regs[i].is32,
5580 ds_unneeded,ds_unneeded_upper);
5581 load_regs(regs[i].regmap,branch_regs[i].regmap,regs[i].was32,rs1[i+1],rs2[i+1]);
5582 address_generation(i+1,&branch_regs[i],0);
5583 load_regs(regs[i].regmap,branch_regs[i].regmap,regs[i].was32,CCREG,CCREG);
5584 ds_assemble(i+1,&branch_regs[i]);
5586 cc=get_reg(branch_regs[i].regmap,CCREG);
5587 if(cc==-1&&!likely[i]) {
5588 // Cycle count isn't in a register, temporarily load it then write it out
5589 emit_loadreg(CCREG,HOST_CCREG);
5590 emit_addimm_and_set_flags(CLOCK_DIVIDER*(ccadj[i]+2),HOST_CCREG);
5593 add_stub(CC_STUB,jaddr,(int)out,0,i,start+i*4+8,NOTTAKEN,0);
5594 emit_storereg(CCREG,HOST_CCREG);
5597 cc=get_reg(i_regmap,CCREG);
5598 assert(cc==HOST_CCREG);
5599 emit_addimm_and_set_flags(CLOCK_DIVIDER*(ccadj[i]+2),cc);
5602 add_stub(CC_STUB,jaddr,(int)out,0,i,start+i*4+8,likely[i]?NULLDS:NOTTAKEN,0);
5608 static void sjump_assemble(int i,struct regstat *i_regs)
5610 signed char *i_regmap=i_regs->regmap;
5613 match=match_bt(branch_regs[i].regmap,branch_regs[i].is32,branch_regs[i].dirty,ba[i]);
5614 assem_debug("smatch=%d",match);
5616 int prev_cop1_usable=cop1_usable;
5617 int unconditional=0,nevertaken=0;
5620 int internal=internal_branch(branch_regs[i].is32,ba[i]);
5621 if(i==(ba[i]-start)>>2) assem_debug("idle loop");
5622 if(!match) invert=1;
5623 #ifdef CORTEX_A8_BRANCH_PREDICTION_HACK
5624 if(i>(ba[i]-start)>>2) invert=1;
5627 //if(opcode2[i]>=0x10) return; // FIXME (BxxZAL)
5628 assert(opcode2[i]<0x10||rs1[i]==0); // FIXME (BxxZAL)
5631 s1l=get_reg(branch_regs[i].regmap,rs1[i]);
5632 s1h=get_reg(branch_regs[i].regmap,rs1[i]|64);
5635 s1l=get_reg(i_regmap,rs1[i]);
5636 s1h=get_reg(i_regmap,rs1[i]|64);
5640 if(opcode2[i]&1) unconditional=1;
5642 // These are never taken (r0 is never less than zero)
5643 //assert(opcode2[i]!=0);
5644 //assert(opcode2[i]!=2);
5645 //assert(opcode2[i]!=0x10);
5646 //assert(opcode2[i]!=0x12);
5649 only32=(regs[i].was32>>rs1[i])&1;
5653 // Out of order execution (delay slot first)
5654 //DebugMessage(M64MSG_VERBOSE, "OOOE");
5655 address_generation(i+1,i_regs,regs[i].regmap_entry);
5656 ds_assemble(i+1,i_regs);
5658 uint64_t bc_unneeded=branch_regs[i].u;
5659 uint64_t bc_unneeded_upper=branch_regs[i].uu;
5660 bc_unneeded&=~((1LL<<rs1[i])|(1LL<<rs2[i]));
5661 bc_unneeded_upper&=~((1LL<<us1[i])|(1LL<<us2[i]));
5663 bc_unneeded_upper|=1;
5664 wb_invalidate(regs[i].regmap,branch_regs[i].regmap,regs[i].dirty,regs[i].is32,
5665 bc_unneeded,bc_unneeded_upper);
5666 load_regs(regs[i].regmap,branch_regs[i].regmap,regs[i].was32,rs1[i],rs1[i]);
5667 load_regs(regs[i].regmap,branch_regs[i].regmap,regs[i].was32,CCREG,CCREG);
5669 int rt,return_address;
5670 assert(rt1[i+1]!=31);
5671 assert(rt2[i+1]!=31);
5672 rt=get_reg(branch_regs[i].regmap,31);
5673 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]);
5675 // Save the PC even if the branch is not taken
5676 return_address=start+i*4+8;
5677 emit_movimm(return_address,rt); // PC into link register
5679 if(!nevertaken) emit_prefetch(hash_table[((return_address>>16)^return_address)&0xFFFF]);
5683 cc=get_reg(branch_regs[i].regmap,CCREG);
5684 assert(cc==HOST_CCREG);
5686 store_regs_bt(branch_regs[i].regmap,branch_regs[i].is32,branch_regs[i].dirty,ba[i]);
5687 //do_cc(i,branch_regs[i].regmap,&adj,unconditional?ba[i]:-1,unconditional);
5688 assem_debug("cycle count (adj)");
5690 do_cc(i,branch_regs[i].regmap,&adj,ba[i],TAKEN,0);
5691 if(i!=(ba[i]-start)>>2 || source[i+1]!=0) {
5692 if(adj) emit_addimm(cc,CLOCK_DIVIDER*(ccadj[i]+2-adj),cc);
5693 load_regs_bt(branch_regs[i].regmap,branch_regs[i].is32,branch_regs[i].dirty,ba[i]);
5695 assem_debug("branch: internal");
5697 assem_debug("branch: external");
5698 if(internal&&is_ds[(ba[i]-start)>>2]) {
5699 ds_assemble_entry(i);
5702 add_to_linker((int)out,ba[i],internal);
5705 #ifdef CORTEX_A8_BRANCH_PREDICTION_HACK
5706 if(((u_int)out)&7) emit_addnop(0);
5710 else if(nevertaken) {
5711 emit_addimm_and_set_flags(CLOCK_DIVIDER*(ccadj[i]+2),cc);
5714 add_stub(CC_STUB,jaddr,(int)out,0,i,start+i*4+8,NOTTAKEN,0);
5718 do_cc(i,branch_regs[i].regmap,&adj,-1,0,invert);
5719 if(adj&&!invert) emit_addimm(cc,CLOCK_DIVIDER*(ccadj[i]+2-adj),cc);
5723 if(opcode2[i]==0) // BLTZ
5730 add_to_linker((int)out,ba[i],internal);
5734 if(opcode2[i]==1) // BGEZ
5741 add_to_linker((int)out,ba[i],internal);
5749 if(opcode2[i]==0) // BLTZ
5756 add_to_linker((int)out,ba[i],internal);
5760 if(opcode2[i]==1) // BGEZ
5767 add_to_linker((int)out,ba[i],internal);
5774 #ifdef CORTEX_A8_BRANCH_PREDICTION_HACK
5775 if(match&&(!internal||!is_ds[(ba[i]-start)>>2])) {
5777 emit_addimm(cc,-CLOCK_DIVIDER*adj,cc);
5778 add_to_linker((int)out,ba[i],internal);
5781 add_to_linker((int)out,ba[i],internal*2);
5787 if(adj) emit_addimm(cc,-CLOCK_DIVIDER*adj,cc);
5788 store_regs_bt(branch_regs[i].regmap,branch_regs[i].is32,branch_regs[i].dirty,ba[i]);
5789 load_regs_bt(branch_regs[i].regmap,branch_regs[i].is32,branch_regs[i].dirty,ba[i]);
5791 assem_debug("branch: internal");
5793 assem_debug("branch: external");
5794 if(internal&&is_ds[(ba[i]-start)>>2]) {
5795 ds_assemble_entry(i);
5798 add_to_linker((int)out,ba[i],internal);
5802 set_jump_target(nottaken,(int)out);
5806 if(!invert) emit_addimm(cc,CLOCK_DIVIDER*adj,cc);
5808 } // (!unconditional)
5812 // In-order execution (branch first)
5813 //DebugMessage(M64MSG_VERBOSE, "IOE");
5815 if(!unconditional) {
5816 //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]);
5820 if((opcode2[i]&0x1d)==0) // BLTZ/BLTZL
5826 if((opcode2[i]&0x1d)==1) // BGEZ/BGEZL
5836 if((opcode2[i]&0x1d)==0) // BLTZ/BLTZL
5842 if((opcode2[i]&0x1d)==1) // BGEZ/BGEZL
5849 } // if(!unconditional)
5851 uint64_t ds_unneeded=branch_regs[i].u;
5852 uint64_t ds_unneeded_upper=branch_regs[i].uu;
5853 ds_unneeded&=~((1LL<<rs1[i+1])|(1LL<<rs2[i+1]));
5854 ds_unneeded_upper&=~((1LL<<us1[i+1])|(1LL<<us2[i+1]));
5855 if((~ds_unneeded_upper>>rt1[i+1])&1) ds_unneeded_upper&=~((1LL<<dep1[i+1])|(1LL<<dep2[i+1]));
5857 ds_unneeded_upper|=1;
5860 //assem_debug("1:");
5861 wb_invalidate(regs[i].regmap,branch_regs[i].regmap,regs[i].dirty,regs[i].is32,
5862 ds_unneeded,ds_unneeded_upper);
5864 load_regs(regs[i].regmap,branch_regs[i].regmap,regs[i].was32,rs1[i+1],rs2[i+1]);
5865 address_generation(i+1,&branch_regs[i],0);
5866 load_regs(regs[i].regmap,branch_regs[i].regmap,regs[i].was32,CCREG,INVCP);
5867 ds_assemble(i+1,&branch_regs[i]);
5868 cc=get_reg(branch_regs[i].regmap,CCREG);
5870 emit_loadreg(CCREG,cc=HOST_CCREG);
5871 // CHECK: Is the following instruction (fall thru) allocated ok?
5873 assert(cc==HOST_CCREG);
5874 store_regs_bt(branch_regs[i].regmap,branch_regs[i].is32,branch_regs[i].dirty,ba[i]);
5875 do_cc(i,i_regmap,&adj,ba[i],TAKEN,0);
5876 assem_debug("cycle count (adj)");
5877 if(adj) emit_addimm(cc,CLOCK_DIVIDER*(ccadj[i]+2-adj),cc);
5878 load_regs_bt(branch_regs[i].regmap,branch_regs[i].is32,branch_regs[i].dirty,ba[i]);
5880 assem_debug("branch: internal");
5882 assem_debug("branch: external");
5883 if(internal&&is_ds[(ba[i]-start)>>2]) {
5884 ds_assemble_entry(i);
5887 add_to_linker((int)out,ba[i],internal);
5892 cop1_usable=prev_cop1_usable;
5893 if(!unconditional) {
5894 set_jump_target(nottaken,(int)out);
5897 wb_invalidate(regs[i].regmap,branch_regs[i].regmap,regs[i].dirty,regs[i].is32,
5898 ds_unneeded,ds_unneeded_upper);
5899 load_regs(regs[i].regmap,branch_regs[i].regmap,regs[i].was32,rs1[i+1],rs2[i+1]);
5900 address_generation(i+1,&branch_regs[i],0);
5901 load_regs(regs[i].regmap,branch_regs[i].regmap,regs[i].was32,CCREG,CCREG);
5902 ds_assemble(i+1,&branch_regs[i]);
5904 cc=get_reg(branch_regs[i].regmap,CCREG);
5905 if(cc==-1&&!likely[i]) {
5906 // Cycle count isn't in a register, temporarily load it then write it out
5907 emit_loadreg(CCREG,HOST_CCREG);
5908 emit_addimm_and_set_flags(CLOCK_DIVIDER*(ccadj[i]+2),HOST_CCREG);
5911 add_stub(CC_STUB,jaddr,(int)out,0,i,start+i*4+8,NOTTAKEN,0);
5912 emit_storereg(CCREG,HOST_CCREG);
5915 cc=get_reg(i_regmap,CCREG);
5916 assert(cc==HOST_CCREG);
5917 emit_addimm_and_set_flags(CLOCK_DIVIDER*(ccadj[i]+2),cc);
5920 add_stub(CC_STUB,jaddr,(int)out,0,i,start+i*4+8,likely[i]?NULLDS:NOTTAKEN,0);
5926 static void fjump_assemble(int i,struct regstat *i_regs)
5928 signed char *i_regmap=i_regs->regmap;
5931 match=match_bt(branch_regs[i].regmap,branch_regs[i].is32,branch_regs[i].dirty,ba[i]);
5932 assem_debug("fmatch=%d",match);
5936 int internal=internal_branch(branch_regs[i].is32,ba[i]);
5937 if(i==(ba[i]-start)>>2) assem_debug("idle loop");
5938 if(!match) invert=1;
5939 #ifdef CORTEX_A8_BRANCH_PREDICTION_HACK
5940 if(i>(ba[i]-start)>>2) invert=1;
5944 fs=get_reg(branch_regs[i].regmap,FSREG);
5945 address_generation(i+1,i_regs,regs[i].regmap_entry); // Is this okay?
5948 fs=get_reg(i_regmap,FSREG);
5951 // Check cop1 unusable
5953 cs=get_reg(i_regmap,CSREG);
5955 emit_testimm(cs,0x20000000);
5958 add_stub(FP_STUB,eaddr,(int)out,i,cs,(int)i_regs,0,0);
5963 // Out of order execution (delay slot first)
5964 //DebugMessage(M64MSG_VERBOSE, "OOOE");
5965 ds_assemble(i+1,i_regs);
5967 uint64_t bc_unneeded=branch_regs[i].u;
5968 uint64_t bc_unneeded_upper=branch_regs[i].uu;
5969 bc_unneeded&=~((1LL<<rs1[i])|(1LL<<rs2[i]));
5970 bc_unneeded_upper&=~((1LL<<us1[i])|(1LL<<us2[i]));
5972 bc_unneeded_upper|=1;
5973 wb_invalidate(regs[i].regmap,branch_regs[i].regmap,regs[i].dirty,regs[i].is32,
5974 bc_unneeded,bc_unneeded_upper);
5975 load_regs(regs[i].regmap,branch_regs[i].regmap,regs[i].was32,rs1[i],rs1[i]);
5976 load_regs(regs[i].regmap,branch_regs[i].regmap,regs[i].was32,CCREG,CCREG);
5977 cc=get_reg(branch_regs[i].regmap,CCREG);
5978 assert(cc==HOST_CCREG);
5979 do_cc(i,branch_regs[i].regmap,&adj,-1,0,invert);
5980 assem_debug("cycle count (adj)");
5983 if(adj&&!invert) emit_addimm(cc,CLOCK_DIVIDER*(ccadj[i]+2-adj),cc);
5986 emit_testimm(fs,0x800000);
5987 if(source[i]&0x10000) // BC1T
5993 add_to_linker((int)out,ba[i],internal);
6002 add_to_linker((int)out,ba[i],internal);
6010 if(adj) emit_addimm(cc,-CLOCK_DIVIDER*adj,cc);
6011 #ifdef CORTEX_A8_BRANCH_PREDICTION_HACK
6012 else if(match) emit_addnop(13);
6014 store_regs_bt(branch_regs[i].regmap,branch_regs[i].is32,branch_regs[i].dirty,ba[i]);
6015 load_regs_bt(branch_regs[i].regmap,branch_regs[i].is32,branch_regs[i].dirty,ba[i]);
6017 assem_debug("branch: internal");
6019 assem_debug("branch: external");
6020 if(internal&&is_ds[(ba[i]-start)>>2]) {
6021 ds_assemble_entry(i);
6024 add_to_linker((int)out,ba[i],internal);
6027 set_jump_target(nottaken,(int)out);
6031 if(!invert) emit_addimm(cc,CLOCK_DIVIDER*adj,cc);
6033 } // (!unconditional)
6037 // In-order execution (branch first)
6038 //DebugMessage(M64MSG_VERBOSE, "IOE");
6041 //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]);
6044 emit_testimm(fs,0x800000);
6045 if(source[i]&0x10000) // BC1T
6056 } // if(!unconditional)
6058 uint64_t ds_unneeded=branch_regs[i].u;
6059 uint64_t ds_unneeded_upper=branch_regs[i].uu;
6060 ds_unneeded&=~((1LL<<rs1[i+1])|(1LL<<rs2[i+1]));
6061 ds_unneeded_upper&=~((1LL<<us1[i+1])|(1LL<<us2[i+1]));
6062 if((~ds_unneeded_upper>>rt1[i+1])&1) ds_unneeded_upper&=~((1LL<<dep1[i+1])|(1LL<<dep2[i+1]));
6064 ds_unneeded_upper|=1;
6066 //assem_debug("1:");
6067 wb_invalidate(regs[i].regmap,branch_regs[i].regmap,regs[i].dirty,regs[i].is32,
6068 ds_unneeded,ds_unneeded_upper);
6070 load_regs(regs[i].regmap,branch_regs[i].regmap,regs[i].was32,rs1[i+1],rs2[i+1]);
6071 address_generation(i+1,&branch_regs[i],0);
6072 load_regs(regs[i].regmap,branch_regs[i].regmap,regs[i].was32,CCREG,INVCP);
6073 ds_assemble(i+1,&branch_regs[i]);
6074 cc=get_reg(branch_regs[i].regmap,CCREG);
6076 emit_loadreg(CCREG,cc=HOST_CCREG);
6077 // CHECK: Is the following instruction (fall thru) allocated ok?
6079 assert(cc==HOST_CCREG);
6080 store_regs_bt(branch_regs[i].regmap,branch_regs[i].is32,branch_regs[i].dirty,ba[i]);
6081 do_cc(i,i_regmap,&adj,ba[i],TAKEN,0);
6082 assem_debug("cycle count (adj)");
6083 if(adj) emit_addimm(cc,CLOCK_DIVIDER*(ccadj[i]+2-adj),cc);
6084 load_regs_bt(branch_regs[i].regmap,branch_regs[i].is32,branch_regs[i].dirty,ba[i]);
6086 assem_debug("branch: internal");
6088 assem_debug("branch: external");
6089 if(internal&&is_ds[(ba[i]-start)>>2]) {
6090 ds_assemble_entry(i);
6093 add_to_linker((int)out,ba[i],internal);
6098 if(1) { // <- FIXME (don't need this)
6099 set_jump_target(nottaken,(int)out);
6102 wb_invalidate(regs[i].regmap,branch_regs[i].regmap,regs[i].dirty,regs[i].is32,
6103 ds_unneeded,ds_unneeded_upper);
6104 load_regs(regs[i].regmap,branch_regs[i].regmap,regs[i].was32,rs1[i+1],rs2[i+1]);
6105 address_generation(i+1,&branch_regs[i],0);
6106 load_regs(regs[i].regmap,branch_regs[i].regmap,regs[i].was32,CCREG,CCREG);
6107 ds_assemble(i+1,&branch_regs[i]);
6109 cc=get_reg(branch_regs[i].regmap,CCREG);
6110 if(cc==-1&&!likely[i]) {
6111 // Cycle count isn't in a register, temporarily load it then write it out
6112 emit_loadreg(CCREG,HOST_CCREG);
6113 emit_addimm_and_set_flags(CLOCK_DIVIDER*(ccadj[i]+2),HOST_CCREG);
6116 add_stub(CC_STUB,jaddr,(int)out,0,i,start+i*4+8,NOTTAKEN,0);
6117 emit_storereg(CCREG,HOST_CCREG);
6120 cc=get_reg(i_regmap,CCREG);
6121 assert(cc==HOST_CCREG);
6122 emit_addimm_and_set_flags(CLOCK_DIVIDER*(ccadj[i]+2),cc);
6125 add_stub(CC_STUB,jaddr,(int)out,0,i,start+i*4+8,likely[i]?NULLDS:NOTTAKEN,0);
6131 static void pagespan_assemble(int i,struct regstat *i_regs)
6133 int s1l=get_reg(i_regs->regmap,rs1[i]);
6134 int s1h=get_reg(i_regs->regmap,rs1[i]|64);
6135 int s2l=get_reg(i_regs->regmap,rs2[i]);
6136 int s2h=get_reg(i_regs->regmap,rs2[i]|64);
6139 int unconditional=0;
6149 if((i_regs->is32>>rs1[i])&(i_regs->is32>>rs2[i])&1) {
6153 int addr,alt,ntaddr;
6154 if(i_regs->regmap[HOST_BTREG]<0) {addr=HOST_BTREG;}
6158 if(hr!=EXCLUDE_REG && hr!=HOST_CCREG &&
6159 (i_regs->regmap[hr]&63)!=rs1[i] &&
6160 (i_regs->regmap[hr]&63)!=rs2[i] )
6169 if(hr!=EXCLUDE_REG && hr!=HOST_CCREG && hr!=HOST_BTREG &&
6170 (i_regs->regmap[hr]&63)!=rs1[i] &&
6171 (i_regs->regmap[hr]&63)!=rs2[i] )
6177 if((opcode[i]&0x2E)==6) // BLEZ/BGTZ needs another register
6181 if(hr!=EXCLUDE_REG && hr!=HOST_CCREG && hr!=HOST_BTREG &&
6182 (i_regs->regmap[hr]&63)!=rs1[i] &&
6183 (i_regs->regmap[hr]&63)!=rs2[i] )
6190 assert(hr<HOST_REGS);
6191 if((opcode[i]&0x2e)==4||opcode[i]==0x11) { // BEQ/BNE/BEQL/BNEL/BC1
6192 load_regs(regs[i].regmap_entry,regs[i].regmap,regs[i].was32,CCREG,CCREG);
6194 emit_addimm(HOST_CCREG,CLOCK_DIVIDER*(ccadj[i]+2),HOST_CCREG);
6195 if(opcode[i]==2) // J
6199 if(opcode[i]==3) // JAL
6202 int rt=get_reg(i_regs->regmap,31);
6203 emit_movimm(start+i*4+8,rt);
6206 if(opcode[i]==0&&(opcode2[i]&0x3E)==8) // JR/JALR
6209 if(opcode2[i]==9) // JALR
6211 int rt=get_reg(i_regs->regmap,rt1[i]);
6212 emit_movimm(start+i*4+8,rt);
6215 if((opcode[i]&0x3f)==4) // BEQ
6222 #ifdef HAVE_CMOV_IMM
6224 if(s2l>=0) emit_cmp(s1l,s2l);
6225 else emit_test(s1l,s1l);
6226 emit_cmov2imm_e_ne_compact(ba[i],start+i*4+8,addr);
6232 emit_mov2imm_compact(ba[i],addr,start+i*4+8,alt);
6234 if(s2h>=0) emit_cmp(s1h,s2h);
6235 else emit_test(s1h,s1h);
6236 emit_cmovne_reg(alt,addr);
6238 if(s2l>=0) emit_cmp(s1l,s2l);
6239 else emit_test(s1l,s1l);
6240 emit_cmovne_reg(alt,addr);
6243 if((opcode[i]&0x3f)==5) // BNE
6245 #ifdef HAVE_CMOV_IMM
6247 if(s2l>=0) emit_cmp(s1l,s2l);
6248 else emit_test(s1l,s1l);
6249 emit_cmov2imm_e_ne_compact(start+i*4+8,ba[i],addr);
6255 emit_mov2imm_compact(start+i*4+8,addr,ba[i],alt);
6257 if(s2h>=0) emit_cmp(s1h,s2h);
6258 else emit_test(s1h,s1h);
6259 emit_cmovne_reg(alt,addr);
6261 if(s2l>=0) emit_cmp(s1l,s2l);
6262 else emit_test(s1l,s1l);
6263 emit_cmovne_reg(alt,addr);
6266 if((opcode[i]&0x3f)==0x14) // BEQL
6269 if(s2h>=0) emit_cmp(s1h,s2h);
6270 else emit_test(s1h,s1h);
6274 if(s2l>=0) emit_cmp(s1l,s2l);
6275 else emit_test(s1l,s1l);
6276 if(nottaken) set_jump_target(nottaken,(int)out);
6280 if((opcode[i]&0x3f)==0x15) // BNEL
6283 if(s2h>=0) emit_cmp(s1h,s2h);
6284 else emit_test(s1h,s1h);
6288 if(s2l>=0) emit_cmp(s1l,s2l);
6289 else emit_test(s1l,s1l);
6292 if(taken) set_jump_target(taken,(int)out);
6294 if((opcode[i]&0x3f)==6) // BLEZ
6296 emit_mov2imm_compact(ba[i],alt,start+i*4+8,addr);
6298 if(s1h>=0) emit_mov(addr,ntaddr);
6299 emit_cmovl_reg(alt,addr);
6302 emit_cmovne_reg(ntaddr,addr);
6303 emit_cmovs_reg(alt,addr);
6306 if((opcode[i]&0x3f)==7) // BGTZ
6308 emit_mov2imm_compact(ba[i],addr,start+i*4+8,ntaddr);
6310 if(s1h>=0) emit_mov(addr,alt);
6311 emit_cmovl_reg(ntaddr,addr);
6314 emit_cmovne_reg(alt,addr);
6315 emit_cmovs_reg(ntaddr,addr);
6318 if((opcode[i]&0x3f)==0x16) // BLEZL
6320 assert((opcode[i]&0x3f)!=0x16);
6322 if((opcode[i]&0x3f)==0x17) // BGTZL
6324 assert((opcode[i]&0x3f)!=0x17);
6326 assert(opcode[i]!=1); // BLTZ/BGEZ
6328 //FIXME: Check CSREG
6329 if(opcode[i]==0x11 && opcode2[i]==0x08 ) {
6330 if((source[i]&0x30000)==0) // BC1F
6332 emit_mov2imm_compact(ba[i],addr,start+i*4+8,alt);
6333 emit_testimm(s1l,0x800000);
6334 emit_cmovne_reg(alt,addr);
6336 if((source[i]&0x30000)==0x10000) // BC1T
6338 emit_mov2imm_compact(ba[i],alt,start+i*4+8,addr);
6339 emit_testimm(s1l,0x800000);
6340 emit_cmovne_reg(alt,addr);
6342 if((source[i]&0x30000)==0x20000) // BC1FL
6344 emit_testimm(s1l,0x800000);
6348 if((source[i]&0x30000)==0x30000) // BC1TL
6350 emit_testimm(s1l,0x800000);
6356 assert(i_regs->regmap[HOST_CCREG]==CCREG);
6357 wb_dirtys(regs[i].regmap,regs[i].is32,regs[i].dirty);
6358 if(likely[i]||unconditional)
6360 emit_movimm(ba[i],HOST_BTREG);
6362 else if(addr!=HOST_BTREG)
6364 emit_mov(addr,HOST_BTREG);
6366 void *branch_addr=out;
6368 int target_addr=start+i*4+5;
6370 void *compiled_target_addr=check_addr(target_addr);
6371 emit_extjump_ds((int)branch_addr,target_addr);
6372 if(compiled_target_addr) {
6373 set_jump_target((int)branch_addr,(int)compiled_target_addr);
6374 add_link(target_addr,stub);
6376 else set_jump_target((int)branch_addr,(int)stub);
6379 set_jump_target((int)nottaken,(int)out);
6380 wb_dirtys(regs[i].regmap,regs[i].is32,regs[i].dirty);
6381 void *branch_addr=out;
6383 int target_addr=start+i*4+8;
6385 void *compiled_target_addr=check_addr(target_addr);
6386 emit_extjump_ds((int)branch_addr,target_addr);
6387 if(compiled_target_addr) {
6388 set_jump_target((int)branch_addr,(int)compiled_target_addr);
6389 add_link(target_addr,stub);
6391 else set_jump_target((int)branch_addr,(int)stub);
6395 // Assemble the delay slot for the above
6396 static void pagespan_ds()
6398 assem_debug("initial delay slot:");
6399 u_int vaddr=start+1;
6400 u_int page=(0x80000000^vaddr)>>12;
6402 if(page>262143&&tlb_LUT_r[vaddr>>12]) page=(tlb_LUT_r[page^0x80000]^0x80000000)>>12;
6403 if(page>2048) page=2048+(page&2047);
6404 if(vpage>262143&&tlb_LUT_r[vaddr>>12]) vpage&=2047; // jump_dirty uses a hash of the virtual address instead
6405 if(vpage>2048) vpage=2048+(vpage&2047);
6406 ll_add(jump_dirty+vpage,vaddr,(void *)out);
6408 ll_add(jump_in+page,vaddr,(void *)out);
6409 assert(regs[0].regmap_entry[HOST_CCREG]==CCREG);
6410 if(regs[0].regmap[HOST_CCREG]!=CCREG)
6411 wb_register(CCREG,regs[0].regmap_entry,regs[0].wasdirty,regs[0].was32);
6412 if(regs[0].regmap[HOST_BTREG]!=BTREG)
6413 emit_writeword(HOST_BTREG,(int)&branch_target);
6414 load_regs(regs[0].regmap_entry,regs[0].regmap,regs[0].was32,rs1[0],rs2[0]);
6415 address_generation(0,®s[0],regs[0].regmap_entry);
6416 if(itype[0]==LOAD||itype[0]==LOADLR||itype[0]==STORE||itype[0]==STORELR||itype[0]==C1LS)
6417 load_regs(regs[0].regmap_entry,regs[0].regmap,regs[0].was32,MMREG,ROREG);
6418 if(itype[0]==STORE||itype[0]==STORELR||(opcode[0]&0x3b)==0x39)
6419 load_regs(regs[0].regmap_entry,regs[0].regmap,regs[0].was32,INVCP,INVCP);
6424 alu_assemble(0,®s[0]);break;
6426 imm16_assemble(0,®s[0]);break;
6428 shift_assemble(0,®s[0]);break;
6430 shiftimm_assemble(0,®s[0]);break;
6432 load_assemble(0,®s[0]);break;
6434 loadlr_assemble(0,®s[0]);break;
6436 store_assemble(0,®s[0]);break;
6438 storelr_assemble(0,®s[0]);break;
6440 cop0_assemble(0,®s[0]);break;
6442 cop1_assemble(0,®s[0]);break;
6444 c1ls_assemble(0,®s[0]);break;
6446 fconv_assemble(0,®s[0]);break;
6448 float_assemble(0,®s[0]);break;
6450 fcomp_assemble(0,®s[0]);break;
6452 multdiv_assemble(0,®s[0]);break;
6454 mov_assemble(0,®s[0]);break;
6462 DebugMessage(M64MSG_VERBOSE, "Jump in the delay slot. This is probably a bug.");
6464 int btaddr=get_reg(regs[0].regmap,BTREG);
6466 btaddr=get_reg(regs[0].regmap,-1);
6467 emit_readword((int)&branch_target,btaddr);
6469 assert(btaddr!=HOST_CCREG);
6470 if(regs[0].regmap[HOST_CCREG]!=CCREG) emit_loadreg(CCREG,HOST_CCREG);
6472 emit_movimm(start+4,HOST_TEMPREG);
6473 emit_cmp(btaddr,HOST_TEMPREG);
6475 emit_cmpimm(btaddr,start+4);
6477 int branch=(int)out;
6479 store_regs_bt(regs[0].regmap,regs[0].is32,regs[0].dirty,-1);
6480 emit_jmp(jump_vaddr_reg[btaddr]);
6481 set_jump_target(branch,(int)out);
6482 store_regs_bt(regs[0].regmap,regs[0].is32,regs[0].dirty,start+4);
6483 load_regs_bt(regs[0].regmap,regs[0].is32,regs[0].dirty,start+4);
6486 // Basic liveness analysis for MIPS registers
6487 static void unneeded_registers(int istart,int iend,int r)
6491 uint64_t temp_u,temp_uu;
6496 u=unneeded_reg[iend+1];
6497 uu=unneeded_reg_upper[iend+1];
6500 for (i=iend;i>=istart;i--)
6502 //DebugMessage(M64MSG_VERBOSE, "unneeded registers i=%d (%d,%d) r=%d",i,istart,iend,r);
6503 if(itype[i]==RJUMP||itype[i]==UJUMP||itype[i]==CJUMP||itype[i]==SJUMP||itype[i]==FJUMP)
6505 // If subroutine call, flag return address as a possible branch target
6506 if(rt1[i]==31 && i<slen-2) bt[i+2]=1;
6508 if(ba[i]<start || ba[i]>=(start+slen*4))
6510 // Branch out of this block, flush all regs
6514 if(itype[i]==UJUMP&&rt1[i]==31)
6516 uu=u=0x300C00F; // Discard at, v0-v1, t6-t9
6518 if(itype[i]==RJUMP&&rs1[i]==31)
6520 uu=u=0x300C0F3; // Discard at, a0-a3, t6-t9
6522 if(start>0x80000400&&start<0x80800000) {
6523 if(itype[i]==UJUMP&&rt1[i]==31)
6525 //uu=u=0x30300FF0FLL; // Discard at, v0-v1, t0-t9, lo, hi
6526 uu=u=0x300FF0F; // Discard at, v0-v1, t0-t9
6528 if(itype[i]==RJUMP&&rs1[i]==31)
6530 //uu=u=0x30300FFF3LL; // Discard at, a0-a3, t0-t9, lo, hi
6531 uu=u=0x300FFF3; // Discard at, a0-a3, t0-t9
6534 branch_unneeded_reg[i]=u;
6535 branch_unneeded_reg_upper[i]=uu;
6536 // Merge in delay slot
6537 tdep=(~uu>>rt1[i+1])&1;
6538 u|=(1LL<<rt1[i+1])|(1LL<<rt2[i+1]);
6539 uu|=(1LL<<rt1[i+1])|(1LL<<rt2[i+1]);
6540 u&=~((1LL<<rs1[i+1])|(1LL<<rs2[i+1]));
6541 uu&=~((1LL<<us1[i+1])|(1LL<<us2[i+1]));
6542 uu&=~((tdep<<dep1[i+1])|(tdep<<dep2[i+1]));
6544 // If branch is "likely" (and conditional)
6545 // then we skip the delay slot on the fall-thru path
6548 u&=unneeded_reg[i+2];
6549 uu&=unneeded_reg_upper[i+2];
6560 // Internal branch, flag target
6561 bt[(ba[i]-start)>>2]=1;
6562 if(ba[i]<=start+i*4) {
6564 if(itype[i]==RJUMP||itype[i]==UJUMP||(source[i]>>16)==0x1000)
6566 // Unconditional branch
6569 // Conditional branch (not taken case)
6570 temp_u=unneeded_reg[i+2];
6571 temp_uu=unneeded_reg_upper[i+2];
6573 // Merge in delay slot
6574 tdep=(~temp_uu>>rt1[i+1])&1;
6575 temp_u|=(1LL<<rt1[i+1])|(1LL<<rt2[i+1]);
6576 temp_uu|=(1LL<<rt1[i+1])|(1LL<<rt2[i+1]);
6577 temp_u&=~((1LL<<rs1[i+1])|(1LL<<rs2[i+1]));
6578 temp_uu&=~((1LL<<us1[i+1])|(1LL<<us2[i+1]));
6579 temp_uu&=~((tdep<<dep1[i+1])|(tdep<<dep2[i+1]));
6580 temp_u|=1;temp_uu|=1;
6581 // If branch is "likely" (and conditional)
6582 // then we skip the delay slot on the fall-thru path
6585 temp_u&=unneeded_reg[i+2];
6586 temp_uu&=unneeded_reg_upper[i+2];
6594 tdep=(~temp_uu>>rt1[i])&1;
6595 temp_u|=(1LL<<rt1[i])|(1LL<<rt2[i]);
6596 temp_uu|=(1LL<<rt1[i])|(1LL<<rt2[i]);
6597 temp_u&=~((1LL<<rs1[i])|(1LL<<rs2[i]));
6598 temp_uu&=~((1LL<<us1[i])|(1LL<<us2[i]));
6599 temp_uu&=~((tdep<<dep1[i])|(tdep<<dep2[i]));
6600 temp_u|=1;temp_uu|=1;
6601 unneeded_reg[i]=temp_u;
6602 unneeded_reg_upper[i]=temp_uu;
6603 // Only go three levels deep. This recursion can take an
6604 // excessive amount of time if there are a lot of nested loops.
6606 unneeded_registers((ba[i]-start)>>2,i-1,r+1);
6608 unneeded_reg[(ba[i]-start)>>2]=1;
6609 unneeded_reg_upper[(ba[i]-start)>>2]=1;
6612 if(itype[i]==RJUMP||itype[i]==UJUMP||(source[i]>>16)==0x1000)
6614 // Unconditional branch
6615 u=unneeded_reg[(ba[i]-start)>>2];
6616 uu=unneeded_reg_upper[(ba[i]-start)>>2];
6617 branch_unneeded_reg[i]=u;
6618 branch_unneeded_reg_upper[i]=uu;
6621 //branch_unneeded_reg[i]=u;
6622 //branch_unneeded_reg_upper[i]=uu;
6623 // Merge in delay slot
6624 tdep=(~uu>>rt1[i+1])&1;
6625 u|=(1LL<<rt1[i+1])|(1LL<<rt2[i+1]);
6626 uu|=(1LL<<rt1[i+1])|(1LL<<rt2[i+1]);
6627 u&=~((1LL<<rs1[i+1])|(1LL<<rs2[i+1]));
6628 uu&=~((1LL<<us1[i+1])|(1LL<<us2[i+1]));
6629 uu&=~((tdep<<dep1[i+1])|(tdep<<dep2[i+1]));
6632 // Conditional branch
6633 b=unneeded_reg[(ba[i]-start)>>2];
6634 bu=unneeded_reg_upper[(ba[i]-start)>>2];
6635 branch_unneeded_reg[i]=b;
6636 branch_unneeded_reg_upper[i]=bu;
6639 //branch_unneeded_reg[i]=b;
6640 //branch_unneeded_reg_upper[i]=bu;
6641 // Branch delay slot
6642 tdep=(~uu>>rt1[i+1])&1;
6643 b|=(1LL<<rt1[i+1])|(1LL<<rt2[i+1]);
6644 bu|=(1LL<<rt1[i+1])|(1LL<<rt2[i+1]);
6645 b&=~((1LL<<rs1[i+1])|(1LL<<rs2[i+1]));
6646 bu&=~((1LL<<us1[i+1])|(1LL<<us2[i+1]));
6647 bu&=~((tdep<<dep1[i+1])|(tdep<<dep2[i+1]));
6649 // If branch is "likely" then we skip the
6650 // delay slot on the fall-thru path
6655 u&=unneeded_reg[i+2];
6656 uu&=unneeded_reg_upper[i+2];
6667 branch_unneeded_reg[i]&=unneeded_reg[i+2];
6668 branch_unneeded_reg_upper[i]&=unneeded_reg_upper[i+2];
6669 //branch_unneeded_reg[i]=1;
6670 //branch_unneeded_reg_upper[i]=1;
6672 branch_unneeded_reg[i]=1;
6673 branch_unneeded_reg_upper[i]=1;
6679 else if(itype[i]==SYSCALL)
6681 // SYSCALL instruction (software interrupt)
6685 else if(itype[i]==COP0 && (source[i]&0x3f)==0x18)
6687 // ERET instruction (return from interrupt)
6692 tdep=(~uu>>rt1[i])&1;
6693 // Written registers are unneeded
6698 // Accessed registers are needed
6703 // Source-target dependencies
6704 uu&=~(tdep<<dep1[i]);
6705 uu&=~(tdep<<dep2[i]);
6706 // R0 is always unneeded
6710 unneeded_reg_upper[i]=uu;
6712 DebugMessage(M64MSG_VERBOSE, "ur (%d,%d) %x: ",istart,iend,start+i*4);
6713 DebugMessage(M64MSG_VERBOSE, "U:");
6715 for(r=1;r<=CCREG;r++) {
6716 if((unneeded_reg[i]>>r)&1) {
6717 if(r==HIREG) DebugMessage(M64MSG_VERBOSE, " HI");
6718 else if(r==LOREG) DebugMessage(M64MSG_VERBOSE, " LO");
6719 else DebugMessage(M64MSG_VERBOSE, " r%d",r);
6722 DebugMessage(M64MSG_VERBOSE, " UU:");
6723 for(r=1;r<=CCREG;r++) {
6724 if(((unneeded_reg_upper[i]&~unneeded_reg[i])>>r)&1) {
6725 if(r==HIREG) DebugMessage(M64MSG_VERBOSE, " HI");
6726 else if(r==LOREG) DebugMessage(M64MSG_VERBOSE, " LO");
6727 else DebugMessage(M64MSG_VERBOSE, " r%d",r);
6733 // Identify registers which are likely to contain 32-bit values
6734 // This is used to predict whether any branches will jump to a
6735 // location with 64-bit values in registers.
6736 static void provisional_32bit()
6740 uint64_t lastbranch=1;
6745 if(itype[i-1]==CJUMP||itype[i-1]==SJUMP||itype[i-1]==FJUMP) {
6746 if(i>1) is32=lastbranch;
6752 if(itype[i-2]==CJUMP||itype[i-2]==SJUMP||itype[i-2]==FJUMP) {
6754 if(i>2) is32=lastbranch;
6758 if((opcode[i-2]&0x2f)==0x05) // BNE/BNEL
6760 if(rs1[i-2]==0||rs2[i-2]==0)
6763 is32|=1LL<<rs1[i-2];
6766 is32|=1LL<<rs2[i-2];
6771 // If something jumps here with 64-bit values
6772 // then promote those registers to 64 bits
6775 uint64_t temp_is32=is32;
6778 if(ba[j]==start+i*4)
6779 //temp_is32&=branch_regs[j].is32;
6784 if(ba[j]==start+i*4)
6795 if(type==UJUMP||type==RJUMP||type==CJUMP||type==SJUMP||type==FJUMP) {
6796 // Branches don't write registers, consider the delay slot instead.
6807 if(opcode[i]==0x27||opcode[i]==0x37|| // LWU/LD
6808 opcode[i]==0x1A||opcode[i]==0x1B) // LDL/LDR
6817 if(op==0x1a||op==0x1b) is32&=~(1LL<<rt); // LDR/LDL
6818 if(op==0x22) is32|=1LL<<rt; // LWL
6821 if (op==0x08||op==0x09|| // ADDI/ADDIU
6822 op==0x0a||op==0x0b|| // SLTI/SLTIU
6828 if(op==0x18||op==0x19) { // DADDI/DADDIU
6831 // is32|=((is32>>s1)&1LL)<<rt;
6833 if(op==0x0d||op==0x0e) { // ORI/XORI
6834 uint64_t sr=((is32>>s1)&1LL);
6850 if(op2>=0x20&&op2<=0x23) { // ADD/ADDU/SUB/SUBU
6853 if(op2==0x2a||op2==0x2b) { // SLT/SLTU
6856 else if(op2>=0x24&&op2<=0x27) { // AND/OR/XOR/NOR
6857 uint64_t sr=((is32>>s1)&(is32>>s2)&1LL);
6861 else if(op2>=0x2c&&op2<=0x2d) { // DADD/DADDU
6866 uint64_t sr=((is32>>s1)&1LL);
6871 uint64_t sr=((is32>>s2)&1LL);
6879 else if(op2>=0x2e&&op2<=0x2f) { // DSUB/DSUBU
6884 uint64_t sr=((is32>>s1)&1LL);
6894 if (op2>=0x1c&&op2<=0x1f) { // DMULT/DMULTU/DDIV/DDIVU
6895 is32&=~((1LL<<HIREG)|(1LL<<LOREG));
6898 is32|=(1LL<<HIREG)|(1LL<<LOREG);
6903 uint64_t sr=((is32>>s1)&1LL);
6909 if(op2>=0x14&&op2<=0x17) is32&=~(1LL<<rt); // DSLLV/DSRLV/DSRAV
6910 else is32|=1LL<<rt; // SLLV/SRLV/SRAV
6914 // DSLL/DSRL/DSRA/DSLL32/DSRL32 but not DSRA32 have 64-bit result
6915 if(op2>=0x38&&op2<0x3f) is32&=~(1LL<<rt);
6918 if(op2==0) is32|=1LL<<rt; // MFC0
6921 if(op2==0) is32|=1LL<<rt; // MFC1
6922 if(op2==1) is32&=~(1LL<<rt); // DMFC1
6923 if(op2==2) is32|=1LL<<rt; // CFC1
6942 if(itype[i-1]==UJUMP||itype[i-1]==RJUMP||(source[i-1]>>16)==0x1000)
6944 if(rt1[i-1]==31) // JAL/JALR
6946 // Subroutine call will return here, don't alloc any registers
6951 // Internal branch will jump here, match registers to caller
6959 // Identify registers which may be assumed to contain 32-bit values
6960 // and where optimizations will rely on this.
6961 // This is used to determine whether backward branches can safely
6962 // jump to a location with 64-bit values in registers.
6963 static void provisional_r32()
6968 for (i=slen-1;i>=0;i--)
6971 if(itype[i]==RJUMP||itype[i]==UJUMP||itype[i]==CJUMP||itype[i]==SJUMP||itype[i]==FJUMP)
6973 if(ba[i]<start || ba[i]>=(start+slen*4))
6975 // Branch out of this block, don't need anything
6981 // Need whatever matches the target
6982 // (and doesn't get overwritten by the delay slot instruction)
6984 int t=(ba[i]-start)>>2;
6985 if(ba[i]>start+i*4) {
6987 //if(!(requires_32bit[t]&~regs[i].was32))
6988 // r32|=requires_32bit[t]&(~(1LL<<rt1[i+1]))&(~(1LL<<rt2[i+1]));
6989 if(!(pr32[t]&~regs[i].was32))
6990 r32|=pr32[t]&(~(1LL<<rt1[i+1]))&(~(1LL<<rt2[i+1]));
6993 if(!(regs[t].was32&~unneeded_reg_upper[t]&~regs[i].was32))
6994 r32|=regs[t].was32&~unneeded_reg_upper[t]&(~(1LL<<rt1[i+1]))&(~(1LL<<rt2[i+1]));
6997 // Conditional branch may need registers for following instructions
6998 if(itype[i]!=RJUMP&&itype[i]!=UJUMP&&(source[i]>>16)!=0x1000)
7001 //r32|=requires_32bit[i+2];
7004 // Mark this address as a branch target since it may be called
7005 // upon return from interrupt
7009 // Merge in delay slot
7011 // These are overwritten unless the branch is "likely"
7012 // and the delay slot is nullified if not taken
7013 r32&=~(1LL<<rt1[i+1]);
7014 r32&=~(1LL<<rt2[i+1]);
7016 // Assume these are needed (delay slot)
7019 if((regs[i].was32>>us1[i+1])&1) r32|=1LL<<us1[i+1];
7023 if((regs[i].was32>>us2[i+1])&1) r32|=1LL<<us2[i+1];
7025 if(dep1[i+1]&&!((unneeded_reg_upper[i]>>dep1[i+1])&1))
7027 if((regs[i].was32>>dep1[i+1])&1) r32|=1LL<<dep1[i+1];
7029 if(dep2[i+1]&&!((unneeded_reg_upper[i]>>dep2[i+1])&1))
7031 if((regs[i].was32>>dep2[i+1])&1) r32|=1LL<<dep2[i+1];
7034 else if(itype[i]==SYSCALL)
7036 // SYSCALL instruction (software interrupt)
7039 else if(itype[i]==COP0 && (source[i]&0x3f)==0x18)
7041 // ERET instruction (return from interrupt)
7045 r32&=~(1LL<<rt1[i]);
7046 r32&=~(1LL<<rt2[i]);
7049 if((regs[i].was32>>us1[i])&1) r32|=1LL<<us1[i];
7053 if((regs[i].was32>>us2[i])&1) r32|=1LL<<us2[i];
7055 if(dep1[i]&&!((unneeded_reg_upper[i]>>dep1[i])&1))
7057 if((regs[i].was32>>dep1[i])&1) r32|=1LL<<dep1[i];
7059 if(dep2[i]&&!((unneeded_reg_upper[i]>>dep2[i])&1))
7061 if((regs[i].was32>>dep2[i])&1) r32|=1LL<<dep2[i];
7063 //requires_32bit[i]=r32;
7066 // Dirty registers which are 32-bit, require 32-bit input
7067 // as they will be written as 32-bit values
7068 for(hr=0;hr<HOST_REGS;hr++)
7070 if(regs[i].regmap_entry[hr]>0&®s[i].regmap_entry[hr]<64) {
7071 if((regs[i].was32>>regs[i].regmap_entry[hr])&(regs[i].wasdirty>>hr)&1) {
7072 if(!((unneeded_reg_upper[i]>>regs[i].regmap_entry[hr])&1))
7073 pr32[i]|=1LL<<regs[i].regmap_entry[hr];
7074 //requires_32bit[i]|=1LL<<regs[i].regmap_entry[hr];
7081 // Write back dirty registers as soon as we will no longer modify them,
7082 // so that we don't end up with lots of writes at the branches.
7083 static void clean_registers(int istart,int iend,int wr)
7087 u_int will_dirty_i,will_dirty_next,temp_will_dirty;
7088 u_int wont_dirty_i,wont_dirty_next,temp_wont_dirty;
7090 will_dirty_i=will_dirty_next=0;
7091 wont_dirty_i=wont_dirty_next=0;
7093 will_dirty_i=will_dirty_next=will_dirty[iend+1];
7094 wont_dirty_i=wont_dirty_next=wont_dirty[iend+1];
7096 for (i=iend;i>=istart;i--)
7098 if(itype[i]==RJUMP||itype[i]==UJUMP||itype[i]==CJUMP||itype[i]==SJUMP||itype[i]==FJUMP)
7100 if(ba[i]<start || ba[i]>=(start+slen*4))
7102 // Branch out of this block, flush all regs
7103 if(itype[i]==RJUMP||itype[i]==UJUMP||(source[i]>>16)==0x1000)
7105 // Unconditional branch
7108 // Merge in delay slot (will dirty)
7109 for(r=0;r<HOST_REGS;r++) {
7110 if(r!=EXCLUDE_REG) {
7111 if((branch_regs[i].regmap[r]&63)==rt1[i]) will_dirty_i|=1<<r;
7112 if((branch_regs[i].regmap[r]&63)==rt2[i]) will_dirty_i|=1<<r;
7113 if((branch_regs[i].regmap[r]&63)==rt1[i+1]) will_dirty_i|=1<<r;
7114 if((branch_regs[i].regmap[r]&63)==rt2[i+1]) will_dirty_i|=1<<r;
7115 if((branch_regs[i].regmap[r]&63)>33) will_dirty_i&=~(1<<r);
7116 if(branch_regs[i].regmap[r]<=0) will_dirty_i&=~(1<<r);
7117 if(branch_regs[i].regmap[r]==CCREG) will_dirty_i|=1<<r;
7118 if((regs[i].regmap[r]&63)==rt1[i]) will_dirty_i|=1<<r;
7119 if((regs[i].regmap[r]&63)==rt2[i]) will_dirty_i|=1<<r;
7120 if((regs[i].regmap[r]&63)==rt1[i+1]) will_dirty_i|=1<<r;
7121 if((regs[i].regmap[r]&63)==rt2[i+1]) will_dirty_i|=1<<r;
7122 if((regs[i].regmap[r]&63)>33) will_dirty_i&=~(1<<r);
7123 if(regs[i].regmap[r]<=0) will_dirty_i&=~(1<<r);
7124 if(regs[i].regmap[r]==CCREG) will_dirty_i|=1<<r;
7130 // Conditional branch
7132 wont_dirty_i=wont_dirty_next;
7133 // Merge in delay slot (will dirty)
7134 for(r=0;r<HOST_REGS;r++) {
7135 if(r!=EXCLUDE_REG) {
7137 // Might not dirty if likely branch is not taken
7138 if((branch_regs[i].regmap[r]&63)==rt1[i]) will_dirty_i|=1<<r;
7139 if((branch_regs[i].regmap[r]&63)==rt2[i]) will_dirty_i|=1<<r;
7140 if((branch_regs[i].regmap[r]&63)==rt1[i+1]) will_dirty_i|=1<<r;
7141 if((branch_regs[i].regmap[r]&63)==rt2[i+1]) will_dirty_i|=1<<r;
7142 if((branch_regs[i].regmap[r]&63)>33) will_dirty_i&=~(1<<r);
7143 if(branch_regs[i].regmap[r]==0) will_dirty_i&=~(1<<r);
7144 if(branch_regs[i].regmap[r]==CCREG) will_dirty_i|=1<<r;
7145 //if((regs[i].regmap[r]&63)==rt1[i]) will_dirty_i|=1<<r;
7146 //if((regs[i].regmap[r]&63)==rt2[i]) will_dirty_i|=1<<r;
7147 if((regs[i].regmap[r]&63)==rt1[i+1]) will_dirty_i|=1<<r;
7148 if((regs[i].regmap[r]&63)==rt2[i+1]) will_dirty_i|=1<<r;
7149 if((regs[i].regmap[r]&63)>33) will_dirty_i&=~(1<<r);
7150 if(regs[i].regmap[r]<=0) will_dirty_i&=~(1<<r);
7151 if(regs[i].regmap[r]==CCREG) will_dirty_i|=1<<r;
7156 // Merge in delay slot (wont dirty)
7157 for(r=0;r<HOST_REGS;r++) {
7158 if(r!=EXCLUDE_REG) {
7159 if((regs[i].regmap[r]&63)==rt1[i]) wont_dirty_i|=1<<r;
7160 if((regs[i].regmap[r]&63)==rt2[i]) wont_dirty_i|=1<<r;
7161 if((regs[i].regmap[r]&63)==rt1[i+1]) wont_dirty_i|=1<<r;
7162 if((regs[i].regmap[r]&63)==rt2[i+1]) wont_dirty_i|=1<<r;
7163 if(regs[i].regmap[r]==CCREG) wont_dirty_i|=1<<r;
7164 if((branch_regs[i].regmap[r]&63)==rt1[i]) wont_dirty_i|=1<<r;
7165 if((branch_regs[i].regmap[r]&63)==rt2[i]) wont_dirty_i|=1<<r;
7166 if((branch_regs[i].regmap[r]&63)==rt1[i+1]) wont_dirty_i|=1<<r;
7167 if((branch_regs[i].regmap[r]&63)==rt2[i+1]) wont_dirty_i|=1<<r;
7168 if(branch_regs[i].regmap[r]==CCREG) wont_dirty_i|=1<<r;
7172 #ifndef DESTRUCTIVE_WRITEBACK
7173 branch_regs[i].dirty&=wont_dirty_i;
7175 branch_regs[i].dirty|=will_dirty_i;
7181 if(ba[i]<=start+i*4) {
7183 if(itype[i]==RJUMP||itype[i]==UJUMP||(source[i]>>16)==0x1000)
7185 // Unconditional branch
7188 // Merge in delay slot (will dirty)
7189 for(r=0;r<HOST_REGS;r++) {
7190 if(r!=EXCLUDE_REG) {
7191 if((branch_regs[i].regmap[r]&63)==rt1[i]) temp_will_dirty|=1<<r;
7192 if((branch_regs[i].regmap[r]&63)==rt2[i]) temp_will_dirty|=1<<r;
7193 if((branch_regs[i].regmap[r]&63)==rt1[i+1]) temp_will_dirty|=1<<r;
7194 if((branch_regs[i].regmap[r]&63)==rt2[i+1]) temp_will_dirty|=1<<r;
7195 if((branch_regs[i].regmap[r]&63)>33) temp_will_dirty&=~(1<<r);
7196 if(branch_regs[i].regmap[r]<=0) temp_will_dirty&=~(1<<r);
7197 if(branch_regs[i].regmap[r]==CCREG) temp_will_dirty|=1<<r;
7198 if((regs[i].regmap[r]&63)==rt1[i]) temp_will_dirty|=1<<r;
7199 if((regs[i].regmap[r]&63)==rt2[i]) temp_will_dirty|=1<<r;
7200 if((regs[i].regmap[r]&63)==rt1[i+1]) temp_will_dirty|=1<<r;
7201 if((regs[i].regmap[r]&63)==rt2[i+1]) temp_will_dirty|=1<<r;
7202 if((regs[i].regmap[r]&63)>33) temp_will_dirty&=~(1<<r);
7203 if(regs[i].regmap[r]<=0) temp_will_dirty&=~(1<<r);
7204 if(regs[i].regmap[r]==CCREG) temp_will_dirty|=1<<r;
7208 // Conditional branch (not taken case)
7209 temp_will_dirty=will_dirty_next;
7210 temp_wont_dirty=wont_dirty_next;
7211 // Merge in delay slot (will dirty)
7212 for(r=0;r<HOST_REGS;r++) {
7213 if(r!=EXCLUDE_REG) {
7215 // Will not dirty if likely branch is not taken
7216 if((branch_regs[i].regmap[r]&63)==rt1[i]) temp_will_dirty|=1<<r;
7217 if((branch_regs[i].regmap[r]&63)==rt2[i]) temp_will_dirty|=1<<r;
7218 if((branch_regs[i].regmap[r]&63)==rt1[i+1]) temp_will_dirty|=1<<r;
7219 if((branch_regs[i].regmap[r]&63)==rt2[i+1]) temp_will_dirty|=1<<r;
7220 if((branch_regs[i].regmap[r]&63)>33) temp_will_dirty&=~(1<<r);
7221 if(branch_regs[i].regmap[r]==0) temp_will_dirty&=~(1<<r);
7222 if(branch_regs[i].regmap[r]==CCREG) temp_will_dirty|=1<<r;
7223 //if((regs[i].regmap[r]&63)==rt1[i]) temp_will_dirty|=1<<r;
7224 //if((regs[i].regmap[r]&63)==rt2[i]) temp_will_dirty|=1<<r;
7225 if((regs[i].regmap[r]&63)==rt1[i+1]) temp_will_dirty|=1<<r;
7226 if((regs[i].regmap[r]&63)==rt2[i+1]) temp_will_dirty|=1<<r;
7227 if((regs[i].regmap[r]&63)>33) temp_will_dirty&=~(1<<r);
7228 if(regs[i].regmap[r]<=0) temp_will_dirty&=~(1<<r);
7229 if(regs[i].regmap[r]==CCREG) temp_will_dirty|=1<<r;
7234 // Merge in delay slot (wont dirty)
7235 for(r=0;r<HOST_REGS;r++) {
7236 if(r!=EXCLUDE_REG) {
7237 if((regs[i].regmap[r]&63)==rt1[i]) temp_wont_dirty|=1<<r;
7238 if((regs[i].regmap[r]&63)==rt2[i]) temp_wont_dirty|=1<<r;
7239 if((regs[i].regmap[r]&63)==rt1[i+1]) temp_wont_dirty|=1<<r;
7240 if((regs[i].regmap[r]&63)==rt2[i+1]) temp_wont_dirty|=1<<r;
7241 if(regs[i].regmap[r]==CCREG) temp_wont_dirty|=1<<r;
7242 if((branch_regs[i].regmap[r]&63)==rt1[i]) temp_wont_dirty|=1<<r;
7243 if((branch_regs[i].regmap[r]&63)==rt2[i]) temp_wont_dirty|=1<<r;
7244 if((branch_regs[i].regmap[r]&63)==rt1[i+1]) temp_wont_dirty|=1<<r;
7245 if((branch_regs[i].regmap[r]&63)==rt2[i+1]) temp_wont_dirty|=1<<r;
7246 if(branch_regs[i].regmap[r]==CCREG) temp_wont_dirty|=1<<r;
7249 // Deal with changed mappings
7251 for(r=0;r<HOST_REGS;r++) {
7252 if(r!=EXCLUDE_REG) {
7253 if(regs[i].regmap[r]!=regmap_pre[i][r]) {
7254 temp_will_dirty&=~(1<<r);
7255 temp_wont_dirty&=~(1<<r);
7256 if((regmap_pre[i][r]&63)>0 && (regmap_pre[i][r]&63)<34) {
7257 temp_will_dirty|=((unneeded_reg[i]>>(regmap_pre[i][r]&63))&1)<<r;
7258 temp_wont_dirty|=((unneeded_reg[i]>>(regmap_pre[i][r]&63))&1)<<r;
7260 temp_will_dirty|=1<<r;
7261 temp_wont_dirty|=1<<r;
7268 will_dirty[i]=temp_will_dirty;
7269 wont_dirty[i]=temp_wont_dirty;
7270 clean_registers((ba[i]-start)>>2,i-1,0);
7272 // Limit recursion. It can take an excessive amount
7273 // of time if there are a lot of nested loops.
7274 will_dirty[(ba[i]-start)>>2]=0;
7275 wont_dirty[(ba[i]-start)>>2]=-1;
7280 if(itype[i]==RJUMP||itype[i]==UJUMP||(source[i]>>16)==0x1000)
7282 // Unconditional branch
7285 //if(ba[i]>start+i*4) { // Disable recursion (for debugging)
7286 for(r=0;r<HOST_REGS;r++) {
7287 if(r!=EXCLUDE_REG) {
7288 if(branch_regs[i].regmap[r]==regs[(ba[i]-start)>>2].regmap_entry[r]) {
7289 will_dirty_i|=will_dirty[(ba[i]-start)>>2]&(1<<r);
7290 wont_dirty_i|=wont_dirty[(ba[i]-start)>>2]&(1<<r);
7292 if(branch_regs[i].regmap[r]>=0) {
7293 will_dirty_i|=((unneeded_reg[(ba[i]-start)>>2]>>(branch_regs[i].regmap[r]&63))&1)<<r;
7294 wont_dirty_i|=((unneeded_reg[(ba[i]-start)>>2]>>(branch_regs[i].regmap[r]&63))&1)<<r;
7299 // Merge in delay slot
7300 for(r=0;r<HOST_REGS;r++) {
7301 if(r!=EXCLUDE_REG) {
7302 if((branch_regs[i].regmap[r]&63)==rt1[i]) will_dirty_i|=1<<r;
7303 if((branch_regs[i].regmap[r]&63)==rt2[i]) will_dirty_i|=1<<r;
7304 if((branch_regs[i].regmap[r]&63)==rt1[i+1]) will_dirty_i|=1<<r;
7305 if((branch_regs[i].regmap[r]&63)==rt2[i+1]) will_dirty_i|=1<<r;
7306 if((branch_regs[i].regmap[r]&63)>33) will_dirty_i&=~(1<<r);
7307 if(branch_regs[i].regmap[r]<=0) will_dirty_i&=~(1<<r);
7308 if(branch_regs[i].regmap[r]==CCREG) will_dirty_i|=1<<r;
7309 if((regs[i].regmap[r]&63)==rt1[i]) will_dirty_i|=1<<r;
7310 if((regs[i].regmap[r]&63)==rt2[i]) will_dirty_i|=1<<r;
7311 if((regs[i].regmap[r]&63)==rt1[i+1]) will_dirty_i|=1<<r;
7312 if((regs[i].regmap[r]&63)==rt2[i+1]) will_dirty_i|=1<<r;
7313 if((regs[i].regmap[r]&63)>33) will_dirty_i&=~(1<<r);
7314 if(regs[i].regmap[r]<=0) will_dirty_i&=~(1<<r);
7315 if(regs[i].regmap[r]==CCREG) will_dirty_i|=1<<r;
7319 // Conditional branch
7320 will_dirty_i=will_dirty_next;
7321 wont_dirty_i=wont_dirty_next;
7322 //if(ba[i]>start+i*4) { // Disable recursion (for debugging)
7323 for(r=0;r<HOST_REGS;r++) {
7324 if(r!=EXCLUDE_REG) {
7325 signed char target_reg=branch_regs[i].regmap[r];
7326 if(target_reg==regs[(ba[i]-start)>>2].regmap_entry[r]) {
7327 will_dirty_i&=will_dirty[(ba[i]-start)>>2]&(1<<r);
7328 wont_dirty_i|=wont_dirty[(ba[i]-start)>>2]&(1<<r);
7330 else if(target_reg>=0) {
7331 will_dirty_i&=((unneeded_reg[(ba[i]-start)>>2]>>(target_reg&63))&1)<<r;
7332 wont_dirty_i|=((unneeded_reg[(ba[i]-start)>>2]>>(target_reg&63))&1)<<r;
7334 // Treat delay slot as part of branch too
7335 /*if(regs[i+1].regmap[r]==regs[(ba[i]-start)>>2].regmap_entry[r]) {
7336 will_dirty[i+1]&=will_dirty[(ba[i]-start)>>2]&(1<<r);
7337 wont_dirty[i+1]|=wont_dirty[(ba[i]-start)>>2]&(1<<r);
7341 will_dirty[i+1]&=~(1<<r);
7346 // Merge in delay slot
7347 for(r=0;r<HOST_REGS;r++) {
7348 if(r!=EXCLUDE_REG) {
7350 // Might not dirty if likely branch is not taken
7351 if((branch_regs[i].regmap[r]&63)==rt1[i]) will_dirty_i|=1<<r;
7352 if((branch_regs[i].regmap[r]&63)==rt2[i]) will_dirty_i|=1<<r;
7353 if((branch_regs[i].regmap[r]&63)==rt1[i+1]) will_dirty_i|=1<<r;
7354 if((branch_regs[i].regmap[r]&63)==rt2[i+1]) will_dirty_i|=1<<r;
7355 if((branch_regs[i].regmap[r]&63)>33) will_dirty_i&=~(1<<r);
7356 if(branch_regs[i].regmap[r]<=0) will_dirty_i&=~(1<<r);
7357 if(branch_regs[i].regmap[r]==CCREG) will_dirty_i|=1<<r;
7358 //if((regs[i].regmap[r]&63)==rt1[i]) will_dirty_i|=1<<r;
7359 //if((regs[i].regmap[r]&63)==rt2[i]) will_dirty_i|=1<<r;
7360 if((regs[i].regmap[r]&63)==rt1[i+1]) will_dirty_i|=1<<r;
7361 if((regs[i].regmap[r]&63)==rt2[i+1]) will_dirty_i|=1<<r;
7362 if((regs[i].regmap[r]&63)>33) will_dirty_i&=~(1<<r);
7363 if(regs[i].regmap[r]<=0) will_dirty_i&=~(1<<r);
7364 if(regs[i].regmap[r]==CCREG) will_dirty_i|=1<<r;
7369 // Merge in delay slot (won't dirty)
7370 for(r=0;r<HOST_REGS;r++) {
7371 if(r!=EXCLUDE_REG) {
7372 if((regs[i].regmap[r]&63)==rt1[i]) wont_dirty_i|=1<<r;
7373 if((regs[i].regmap[r]&63)==rt2[i]) wont_dirty_i|=1<<r;
7374 if((regs[i].regmap[r]&63)==rt1[i+1]) wont_dirty_i|=1<<r;
7375 if((regs[i].regmap[r]&63)==rt2[i+1]) wont_dirty_i|=1<<r;
7376 if(regs[i].regmap[r]==CCREG) wont_dirty_i|=1<<r;
7377 if((branch_regs[i].regmap[r]&63)==rt1[i]) wont_dirty_i|=1<<r;
7378 if((branch_regs[i].regmap[r]&63)==rt2[i]) wont_dirty_i|=1<<r;
7379 if((branch_regs[i].regmap[r]&63)==rt1[i+1]) wont_dirty_i|=1<<r;
7380 if((branch_regs[i].regmap[r]&63)==rt2[i+1]) wont_dirty_i|=1<<r;
7381 if(branch_regs[i].regmap[r]==CCREG) wont_dirty_i|=1<<r;
7385 #ifndef DESTRUCTIVE_WRITEBACK
7386 branch_regs[i].dirty&=wont_dirty_i;
7388 branch_regs[i].dirty|=will_dirty_i;
7393 else if(itype[i]==SYSCALL)
7395 // SYSCALL instruction (software interrupt)
7399 else if(itype[i]==COP0 && (source[i]&0x3f)==0x18)
7401 // ERET instruction (return from interrupt)
7405 will_dirty_next=will_dirty_i;
7406 wont_dirty_next=wont_dirty_i;
7407 for(r=0;r<HOST_REGS;r++) {
7408 if(r!=EXCLUDE_REG) {
7409 if((regs[i].regmap[r]&63)==rt1[i]) will_dirty_i|=1<<r;
7410 if((regs[i].regmap[r]&63)==rt2[i]) will_dirty_i|=1<<r;
7411 if((regs[i].regmap[r]&63)>33) will_dirty_i&=~(1<<r);
7412 if(regs[i].regmap[r]<=0) will_dirty_i&=~(1<<r);
7413 if(regs[i].regmap[r]==CCREG) will_dirty_i|=1<<r;
7414 if((regs[i].regmap[r]&63)==rt1[i]) wont_dirty_i|=1<<r;
7415 if((regs[i].regmap[r]&63)==rt2[i]) wont_dirty_i|=1<<r;
7416 if(regs[i].regmap[r]==CCREG) wont_dirty_i|=1<<r;
7418 if(itype[i]!=RJUMP&&itype[i]!=UJUMP&&itype[i]!=CJUMP&&itype[i]!=SJUMP&&itype[i]!=FJUMP)
7420 // Don't store a register immediately after writing it,
7421 // may prevent dual-issue.
7422 if((regs[i].regmap[r]&63)==rt1[i-1]) wont_dirty_i|=1<<r;
7423 if((regs[i].regmap[r]&63)==rt2[i-1]) wont_dirty_i|=1<<r;
7429 will_dirty[i]=will_dirty_i;
7430 wont_dirty[i]=wont_dirty_i;
7431 // Mark registers that won't be dirtied as not dirty
7433 /*DebugMessage(M64MSG_VERBOSE, "wr (%d,%d) %x will:",istart,iend,start+i*4);
7434 for(r=0;r<HOST_REGS;r++) {
7435 if((will_dirty_i>>r)&1) {
7436 DebugMessage(M64MSG_VERBOSE, " r%d",r);
7440 //if(i==istart||(itype[i-1]!=RJUMP&&itype[i-1]!=UJUMP&&itype[i-1]!=CJUMP&&itype[i-1]!=SJUMP&&itype[i-1]!=FJUMP)) {
7441 regs[i].dirty|=will_dirty_i;
7442 #ifndef DESTRUCTIVE_WRITEBACK
7443 regs[i].dirty&=wont_dirty_i;
7444 if(itype[i]==RJUMP||itype[i]==UJUMP||itype[i]==CJUMP||itype[i]==SJUMP||itype[i]==FJUMP)
7446 if(i<iend-1&&itype[i]!=RJUMP&&itype[i]!=UJUMP&&(source[i]>>16)!=0x1000) {
7447 for(r=0;r<HOST_REGS;r++) {
7448 if(r!=EXCLUDE_REG) {
7449 if(regs[i].regmap[r]==regmap_pre[i+2][r]) {
7450 regs[i+2].wasdirty&=wont_dirty_i|~(1<<r);
7451 }else {/*DebugMessage(M64MSG_VERBOSE, "i: %x (%d) mismatch(+2): %d",start+i*4,i,r); / *assert(!((wont_dirty_i>>r)&1));*/}
7459 for(r=0;r<HOST_REGS;r++) {
7460 if(r!=EXCLUDE_REG) {
7461 if(regs[i].regmap[r]==regmap_pre[i+1][r]) {
7462 regs[i+1].wasdirty&=wont_dirty_i|~(1<<r);
7463 }else {/*DebugMessage(M64MSG_VERBOSE, "i: %x (%d) mismatch(+1): %d",start+i*4,i,r);/ *assert(!((wont_dirty_i>>r)&1));*/}
7471 // Deal with changed mappings
7472 temp_will_dirty=will_dirty_i;
7473 temp_wont_dirty=wont_dirty_i;
7474 for(r=0;r<HOST_REGS;r++) {
7475 if(r!=EXCLUDE_REG) {
7477 if(regs[i].regmap[r]==regmap_pre[i][r]) {
7479 #ifndef DESTRUCTIVE_WRITEBACK
7480 regs[i].wasdirty&=wont_dirty_i|~(1<<r);
7482 regs[i].wasdirty|=will_dirty_i&(1<<r);
7485 else if((nr=get_reg(regs[i].regmap,regmap_pre[i][r]))>=0) {
7486 // Register moved to a different register
7487 will_dirty_i&=~(1<<r);
7488 wont_dirty_i&=~(1<<r);
7489 will_dirty_i|=((temp_will_dirty>>nr)&1)<<r;
7490 wont_dirty_i|=((temp_wont_dirty>>nr)&1)<<r;
7492 #ifndef DESTRUCTIVE_WRITEBACK
7493 regs[i].wasdirty&=wont_dirty_i|~(1<<r);
7495 regs[i].wasdirty|=will_dirty_i&(1<<r);
7499 will_dirty_i&=~(1<<r);
7500 wont_dirty_i&=~(1<<r);
7501 if((regmap_pre[i][r]&63)>0 && (regmap_pre[i][r]&63)<34) {
7502 will_dirty_i|=((unneeded_reg[i]>>(regmap_pre[i][r]&63))&1)<<r;
7503 wont_dirty_i|=((unneeded_reg[i]>>(regmap_pre[i][r]&63))&1)<<r;
7506 /*DebugMessage(M64MSG_VERBOSE, "i: %x (%d) mismatch: %d",start+i*4,i,r);/ *assert(!((will_dirty>>r)&1));*/
7516 static void disassemble_inst(int i)
7518 if (bt[i]) DebugMessage(M64MSG_VERBOSE, "*"); else DebugMessage(M64MSG_VERBOSE, " ");
7521 printf (" %x: %s %8x",start+i*4,insn[i],ba[i]);break;
7523 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;
7525 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;
7527 printf (" %x: %s %8x",start+i*4,insn[i],ba[i]);break;
7529 if ((opcode2[i]&1)&&rt1[i]!=31)
7530 printf (" %x: %s r%d,r%d",start+i*4,insn[i],rt1[i],rs1[i]);
7532 printf (" %x: %s r%d",start+i*4,insn[i],rs1[i]);
7535 printf (" %x: %s (pagespan) r%d,r%d,%8x",start+i*4,insn[i],rs1[i],rs2[i],ba[i]);break;
7537 if(opcode[i]==0xf) //LUI
7538 printf (" %x: %s r%d,%4x0000",start+i*4,insn[i],rt1[i],imm[i]&0xffff);
7540 printf (" %x: %s r%d,r%d,%d",start+i*4,insn[i],rt1[i],rs1[i],imm[i]);
7544 printf (" %x: %s r%d,r%d+%x",start+i*4,insn[i],rt1[i],rs1[i],imm[i]);
7548 printf (" %x: %s r%d,r%d+%x",start+i*4,insn[i],rs2[i],rs1[i],imm[i]);
7552 printf (" %x: %s r%d,r%d,r%d",start+i*4,insn[i],rt1[i],rs1[i],rs2[i]);
7555 printf (" %x: %s r%d,r%d",start+i*4,insn[i],rs1[i],rs2[i]);
7558 printf (" %x: %s r%d,r%d,%d",start+i*4,insn[i],rt1[i],rs1[i],imm[i]);
7561 if((opcode2[i]&0x1d)==0x10)
7562 printf (" %x: %s r%d",start+i*4,insn[i],rt1[i]);
7563 else if((opcode2[i]&0x1d)==0x11)
7564 printf (" %x: %s r%d",start+i*4,insn[i],rs1[i]);
7566 printf (" %x: %s",start+i*4,insn[i]);
7570 printf (" %x: %s r%d,cpr0[%d]",start+i*4,insn[i],rt1[i],(source[i]>>11)&0x1f); // MFC0
7571 else if(opcode2[i]==4)
7572 printf (" %x: %s r%d,cpr0[%d]",start+i*4,insn[i],rs1[i],(source[i]>>11)&0x1f); // MTC0
7573 else printf (" %x: %s",start+i*4,insn[i]);
7577 printf (" %x: %s r%d,cpr1[%d]",start+i*4,insn[i],rt1[i],(source[i]>>11)&0x1f); // MFC1
7578 else if(opcode2[i]>3)
7579 printf (" %x: %s r%d,cpr1[%d]",start+i*4,insn[i],rs1[i],(source[i]>>11)&0x1f); // MTC1
7580 else printf (" %x: %s",start+i*4,insn[i]);
7583 printf (" %x: %s cpr1[%d],r%d+%x",start+i*4,insn[i],(source[i]>>16)&0x1f,rs1[i],imm[i]);
7586 //printf (" %s %8x",insn[i],source[i]);
7587 printf (" %x: %s",start+i*4,insn[i]);
7592 void new_dynarec_init()
7594 DebugMessage(M64MSG_INFO, "Init new dynarec");
7596 #if NEW_DYNAREC == NEW_DYNAREC_ARM
7597 if ((base_addr = mmap ((u_char *)BASE_ADDR, 1<<TARGET_SIZE_2,
7598 PROT_READ | PROT_WRITE | PROT_EXEC,
7599 MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS,
7600 -1, 0)) <= 0) {DebugMessage(M64MSG_ERROR, "mmap() failed");}
7602 if ((base_addr = mmap (NULL, 1<<TARGET_SIZE_2,
7603 PROT_READ | PROT_WRITE | PROT_EXEC,
7604 MAP_PRIVATE | MAP_ANONYMOUS,
7605 -1, 0)) <= 0) {DebugMessage(M64MSG_ERROR, "mmap() failed");}
7607 out=(u_char *)base_addr;
7609 rdword=&readmem_dword;
7610 fake_pc.f.r.rs=(long long int *)&readmem_dword;
7611 fake_pc.f.r.rt=(long long int *)&readmem_dword;
7612 fake_pc.f.r.rd=(long long int *)&readmem_dword;
7614 for(n=0x80000;n<0x80800;n++)
7616 for(n=0;n<65536;n++)
7617 hash_table[n][0]=hash_table[n][2]=-1;
7618 memset(mini_ht,-1,sizeof(mini_ht));
7619 memset(restore_candidate,0,sizeof(restore_candidate));
7621 expirep=16384; // Expiry pointer, +2 blocks
7622 pending_exception=0;
7625 // Copy this into local area so we don't have to put it in every literal pool
7626 invc_ptr=invalid_code;
7631 for(n=0;n<524288;n++) // 0 .. 0x7FFFFFFF
7633 for(n=524288;n<526336;n++) // 0x80000000 .. 0x807FFFFF
7634 memory_map[n]=((u_int)rdram-0x80000000)>>2;
7635 for(n=526336;n<1048576;n++) // 0x80800000 .. 0xFFFFFFFF
7637 for(n=0;n<0x8000;n++) { // 0 .. 0x7FFFFFFF
7638 writemem[n] = write_nomem_new;
7639 writememb[n] = write_nomemb_new;
7640 writememh[n] = write_nomemh_new;
7641 writememd[n] = write_nomemd_new;
7642 readmem[n] = read_nomem_new;
7643 readmemb[n] = read_nomemb_new;
7644 readmemh[n] = read_nomemh_new;
7645 readmemd[n] = read_nomemd_new;
7647 for(n=0x8000;n<0x8080;n++) { // 0x80000000 .. 0x807FFFFF
7648 writemem[n] = write_rdram_new;
7649 writememb[n] = write_rdramb_new;
7650 writememh[n] = write_rdramh_new;
7651 writememd[n] = write_rdramd_new;
7653 for(n=0xC000;n<0x10000;n++) { // 0xC0000000 .. 0xFFFFFFFF
7654 writemem[n] = write_nomem_new;
7655 writememb[n] = write_nomemb_new;
7656 writememh[n] = write_nomemh_new;
7657 writememd[n] = write_nomemd_new;
7658 readmem[n] = read_nomem_new;
7659 readmemb[n] = read_nomemb_new;
7660 readmemh[n] = read_nomemh_new;
7661 readmemd[n] = read_nomemd_new;
7667 void new_dynarec_cleanup()
7670 if (munmap (base_addr, 1<<TARGET_SIZE_2) < 0) {DebugMessage(M64MSG_ERROR, "munmap() failed");}
7671 for(n=0;n<4096;n++) ll_clear(jump_in+n);
7672 for(n=0;n<4096;n++) ll_clear(jump_out+n);
7673 for(n=0;n<4096;n++) ll_clear(jump_dirty+n);
7675 if (munmap (ROM_COPY, 67108864) < 0) {DebugMessage(M64MSG_ERROR, "munmap() failed");}
7679 int new_recompile_block(int addr)
7682 if(addr==0x800cd050) {
7684 for(block=0x80000;block<0x80800;block++) invalidate_block(block);
7686 for(n=0;n<=2048;n++) ll_clear(jump_dirty+n);
7689 //if(Count==365117028) tracedebug=1;
7690 assem_debug("NOTCOMPILED: addr = %x -> %x", (int)addr, (int)out);
7691 #if defined (COUNT_NOTCOMPILEDS )
7693 log_message( "notcompiledCount=%i", notcompiledCount );
7695 //DebugMessage(M64MSG_VERBOSE, "NOTCOMPILED: addr = %x -> %x", (int)addr, (int)out);
7696 //DebugMessage(M64MSG_VERBOSE, "TRACE: count=%d next=%d (compile %x)",Count,next_interupt,addr);
7698 //DebugMessage(M64MSG_VERBOSE, "TRACE: count=%d next=%d (checksum %x)",Count,next_interupt,mchecksum());
7699 //DebugMessage(M64MSG_VERBOSE, "fpu mapping=%x enabled=%x",(Status & 0x04000000)>>26,(Status & 0x20000000)>>29);
7700 /*if(Count>=312978186) {
7704 start = (u_int)addr&~3;
7705 //assert(((u_int)addr&1)==0);
7706 if ((int)addr >= 0xa4000000 && (int)addr < 0xa4001000) {
7707 source = (u_int *)((u_int)SP_DMEM+start-0xa4000000);
7708 pagelimit = 0xa4001000;
7710 else if ((int)addr >= 0x80000000 && (int)addr < 0x80800000) {
7711 source = (u_int *)((u_int)rdram+start-0x80000000);
7712 pagelimit = 0x80800000;
7714 else if ((signed int)addr >= (signed int)0xC0000000) {
7715 //DebugMessage(M64MSG_VERBOSE, "addr=%x mm=%x",(u_int)addr,(memory_map[start>>12]<<2));
7716 //if(tlb_LUT_r[start>>12])
7717 //source = (u_int *)(((int)rdram)+(tlb_LUT_r[start>>12]&0xFFFFF000)+(((int)addr)&0xFFF)-0x80000000);
7718 if((signed int)memory_map[start>>12]>=0) {
7719 source = (u_int *)((u_int)(start+(memory_map[start>>12]<<2)));
7720 pagelimit=(start+4096)&0xFFFFF000;
7721 int map=memory_map[start>>12];
7724 //DebugMessage(M64MSG_VERBOSE, "start: %x next: %x",map,memory_map[pagelimit>>12]);
7725 if((map&0xBFFFFFFF)==(memory_map[pagelimit>>12]&0xBFFFFFFF)) pagelimit+=4096;
7727 assem_debug("pagelimit=%x",pagelimit);
7728 assem_debug("mapping=%x (%x)",memory_map[start>>12],(memory_map[start>>12]<<2)+start);
7731 assem_debug("Compile at unmapped memory address: %x ", (int)addr);
7732 //assem_debug("start: %x next: %x",memory_map[start>>12],memory_map[(start+4096)>>12]);
7733 return 1; // Caller will invoke exception handler
7735 //DebugMessage(M64MSG_VERBOSE, "source= %x",(int)source);
7738 //DebugMessage(M64MSG_VERBOSE, "Compile at bogus memory address: %x ", (int)addr);
7739 log_message("Compile at bogus memory address: %x", (int)addr);
7743 /* Pass 1: disassemble */
7744 /* Pass 2: register dependencies, branch targets */
7745 /* Pass 3: register allocation */
7746 /* Pass 4: branch dependencies */
7747 /* Pass 5: pre-alloc */
7748 /* Pass 6: optimize clean/dirty state */
7749 /* Pass 7: flag 32-bit registers */
7750 /* Pass 8: assembly */
7751 /* Pass 9: linker */
7752 /* Pass 10: garbage collection / free memory */
7756 unsigned int type,op,op2;
7758 //DebugMessage(M64MSG_VERBOSE, "addr = %x source = %x %x", addr,source,source[0]);
7760 /* Pass 1 disassembly */
7762 for(i=0;!done;i++) {
7763 bt[i]=0;likely[i]=0;ooo[i]=0;op2=0;
7764 minimum_free_regs[i]=0;
7765 opcode[i]=op=source[i]>>26;
7768 case 0x00: strcpy(insn[i],"special"); type=NI;
7772 case 0x00: strcpy(insn[i],"SLL"); type=SHIFTIMM; break;
7773 case 0x02: strcpy(insn[i],"SRL"); type=SHIFTIMM; break;
7774 case 0x03: strcpy(insn[i],"SRA"); type=SHIFTIMM; break;
7775 case 0x04: strcpy(insn[i],"SLLV"); type=SHIFT; break;
7776 case 0x06: strcpy(insn[i],"SRLV"); type=SHIFT; break;
7777 case 0x07: strcpy(insn[i],"SRAV"); type=SHIFT; break;
7778 case 0x08: strcpy(insn[i],"JR"); type=RJUMP; break;
7779 case 0x09: strcpy(insn[i],"JALR"); type=RJUMP; break;
7780 case 0x0C: strcpy(insn[i],"SYSCALL"); type=SYSCALL; break;
7781 case 0x0D: strcpy(insn[i],"BREAK"); type=OTHER; break;
7782 case 0x0F: strcpy(insn[i],"SYNC"); type=OTHER; break;
7783 case 0x10: strcpy(insn[i],"MFHI"); type=MOV; break;
7784 case 0x11: strcpy(insn[i],"MTHI"); type=MOV; break;
7785 case 0x12: strcpy(insn[i],"MFLO"); type=MOV; break;
7786 case 0x13: strcpy(insn[i],"MTLO"); type=MOV; break;
7787 case 0x14: strcpy(insn[i],"DSLLV"); type=SHIFT; break;
7788 case 0x16: strcpy(insn[i],"DSRLV"); type=SHIFT; break;
7789 case 0x17: strcpy(insn[i],"DSRAV"); type=SHIFT; break;
7790 case 0x18: strcpy(insn[i],"MULT"); type=MULTDIV; break;
7791 case 0x19: strcpy(insn[i],"MULTU"); type=MULTDIV; break;
7792 case 0x1A: strcpy(insn[i],"DIV"); type=MULTDIV; break;
7793 case 0x1B: strcpy(insn[i],"DIVU"); type=MULTDIV; break;
7794 case 0x1C: strcpy(insn[i],"DMULT"); type=MULTDIV; break;
7795 case 0x1D: strcpy(insn[i],"DMULTU"); type=MULTDIV; break;
7796 case 0x1E: strcpy(insn[i],"DDIV"); type=MULTDIV; break;
7797 case 0x1F: strcpy(insn[i],"DDIVU"); type=MULTDIV; break;
7798 case 0x20: strcpy(insn[i],"ADD"); type=ALU; break;
7799 case 0x21: strcpy(insn[i],"ADDU"); type=ALU; break;
7800 case 0x22: strcpy(insn[i],"SUB"); type=ALU; break;
7801 case 0x23: strcpy(insn[i],"SUBU"); type=ALU; break;
7802 case 0x24: strcpy(insn[i],"AND"); type=ALU; break;
7803 case 0x25: strcpy(insn[i],"OR"); type=ALU; break;
7804 case 0x26: strcpy(insn[i],"XOR"); type=ALU; break;
7805 case 0x27: strcpy(insn[i],"NOR"); type=ALU; break;
7806 case 0x2A: strcpy(insn[i],"SLT"); type=ALU; break;
7807 case 0x2B: strcpy(insn[i],"SLTU"); type=ALU; break;
7808 case 0x2C: strcpy(insn[i],"DADD"); type=ALU; break;
7809 case 0x2D: strcpy(insn[i],"DADDU"); type=ALU; break;
7810 case 0x2E: strcpy(insn[i],"DSUB"); type=ALU; break;
7811 case 0x2F: strcpy(insn[i],"DSUBU"); type=ALU; break;
7812 case 0x30: strcpy(insn[i],"TGE"); type=NI; break;
7813 case 0x31: strcpy(insn[i],"TGEU"); type=NI; break;
7814 case 0x32: strcpy(insn[i],"TLT"); type=NI; break;
7815 case 0x33: strcpy(insn[i],"TLTU"); type=NI; break;
7816 case 0x34: strcpy(insn[i],"TEQ"); type=NI; break;
7817 case 0x36: strcpy(insn[i],"TNE"); type=NI; break;
7818 case 0x38: strcpy(insn[i],"DSLL"); type=SHIFTIMM; break;
7819 case 0x3A: strcpy(insn[i],"DSRL"); type=SHIFTIMM; break;
7820 case 0x3B: strcpy(insn[i],"DSRA"); type=SHIFTIMM; break;
7821 case 0x3C: strcpy(insn[i],"DSLL32"); type=SHIFTIMM; break;
7822 case 0x3E: strcpy(insn[i],"DSRL32"); type=SHIFTIMM; break;
7823 case 0x3F: strcpy(insn[i],"DSRA32"); type=SHIFTIMM; break;
7826 case 0x01: strcpy(insn[i],"regimm"); type=NI;
7827 op2=(source[i]>>16)&0x1f;
7830 case 0x00: strcpy(insn[i],"BLTZ"); type=SJUMP; break;
7831 case 0x01: strcpy(insn[i],"BGEZ"); type=SJUMP; break;
7832 case 0x02: strcpy(insn[i],"BLTZL"); type=SJUMP; break;
7833 case 0x03: strcpy(insn[i],"BGEZL"); type=SJUMP; break;
7834 case 0x08: strcpy(insn[i],"TGEI"); type=NI; break;
7835 case 0x09: strcpy(insn[i],"TGEIU"); type=NI; break;
7836 case 0x0A: strcpy(insn[i],"TLTI"); type=NI; break;
7837 case 0x0B: strcpy(insn[i],"TLTIU"); type=NI; break;
7838 case 0x0C: strcpy(insn[i],"TEQI"); type=NI; break;
7839 case 0x0E: strcpy(insn[i],"TNEI"); type=NI; break;
7840 case 0x10: strcpy(insn[i],"BLTZAL"); type=SJUMP; break;
7841 case 0x11: strcpy(insn[i],"BGEZAL"); type=SJUMP; break;
7842 case 0x12: strcpy(insn[i],"BLTZALL"); type=SJUMP; break;
7843 case 0x13: strcpy(insn[i],"BGEZALL"); type=SJUMP; break;
7846 case 0x02: strcpy(insn[i],"J"); type=UJUMP; break;
7847 case 0x03: strcpy(insn[i],"JAL"); type=UJUMP; break;
7848 case 0x04: strcpy(insn[i],"BEQ"); type=CJUMP; break;
7849 case 0x05: strcpy(insn[i],"BNE"); type=CJUMP; break;
7850 case 0x06: strcpy(insn[i],"BLEZ"); type=CJUMP; break;
7851 case 0x07: strcpy(insn[i],"BGTZ"); type=CJUMP; break;
7852 case 0x08: strcpy(insn[i],"ADDI"); type=IMM16; break;
7853 case 0x09: strcpy(insn[i],"ADDIU"); type=IMM16; break;
7854 case 0x0A: strcpy(insn[i],"SLTI"); type=IMM16; break;
7855 case 0x0B: strcpy(insn[i],"SLTIU"); type=IMM16; break;
7856 case 0x0C: strcpy(insn[i],"ANDI"); type=IMM16; break;
7857 case 0x0D: strcpy(insn[i],"ORI"); type=IMM16; break;
7858 case 0x0E: strcpy(insn[i],"XORI"); type=IMM16; break;
7859 case 0x0F: strcpy(insn[i],"LUI"); type=IMM16; break;
7860 case 0x10: strcpy(insn[i],"cop0"); type=NI;
7861 op2=(source[i]>>21)&0x1f;
7864 case 0x00: strcpy(insn[i],"MFC0"); type=COP0; break;
7865 case 0x04: strcpy(insn[i],"MTC0"); type=COP0; break;
7866 case 0x10: strcpy(insn[i],"tlb"); type=NI;
7867 switch(source[i]&0x3f)
7869 case 0x01: strcpy(insn[i],"TLBR"); type=COP0; break;
7870 case 0x02: strcpy(insn[i],"TLBWI"); type=COP0; break;
7871 case 0x06: strcpy(insn[i],"TLBWR"); type=COP0; break;
7872 case 0x08: strcpy(insn[i],"TLBP"); type=COP0; break;
7873 case 0x18: strcpy(insn[i],"ERET"); type=COP0; break;
7877 case 0x11: strcpy(insn[i],"cop1"); type=NI;
7878 op2=(source[i]>>21)&0x1f;
7881 case 0x00: strcpy(insn[i],"MFC1"); type=COP1; break;
7882 case 0x01: strcpy(insn[i],"DMFC1"); type=COP1; break;
7883 case 0x02: strcpy(insn[i],"CFC1"); type=COP1; break;
7884 case 0x04: strcpy(insn[i],"MTC1"); type=COP1; break;
7885 case 0x05: strcpy(insn[i],"DMTC1"); type=COP1; break;
7886 case 0x06: strcpy(insn[i],"CTC1"); type=COP1; break;
7887 case 0x08: strcpy(insn[i],"BC1"); type=FJUMP;
7888 switch((source[i]>>16)&0x3)
7890 case 0x00: strcpy(insn[i],"BC1F"); break;
7891 case 0x01: strcpy(insn[i],"BC1T"); break;
7892 case 0x02: strcpy(insn[i],"BC1FL"); break;
7893 case 0x03: strcpy(insn[i],"BC1TL"); break;
7896 case 0x10: strcpy(insn[i],"C1.S"); type=NI;
7897 switch(source[i]&0x3f)
7899 case 0x00: strcpy(insn[i],"ADD.S"); type=FLOAT; break;
7900 case 0x01: strcpy(insn[i],"SUB.S"); type=FLOAT; break;
7901 case 0x02: strcpy(insn[i],"MUL.S"); type=FLOAT; break;
7902 case 0x03: strcpy(insn[i],"DIV.S"); type=FLOAT; break;
7903 case 0x04: strcpy(insn[i],"SQRT.S"); type=FLOAT; break;
7904 case 0x05: strcpy(insn[i],"ABS.S"); type=FLOAT; break;
7905 case 0x06: strcpy(insn[i],"MOV.S"); type=FLOAT; break;
7906 case 0x07: strcpy(insn[i],"NEG.S"); type=FLOAT; break;
7907 case 0x08: strcpy(insn[i],"ROUND.L.S"); type=FCONV; break;
7908 case 0x09: strcpy(insn[i],"TRUNC.L.S"); type=FCONV; break;
7909 case 0x0A: strcpy(insn[i],"CEIL.L.S"); type=FCONV; break;
7910 case 0x0B: strcpy(insn[i],"FLOOR.L.S"); type=FCONV; break;
7911 case 0x0C: strcpy(insn[i],"ROUND.W.S"); type=FCONV; break;
7912 case 0x0D: strcpy(insn[i],"TRUNC.W.S"); type=FCONV; break;
7913 case 0x0E: strcpy(insn[i],"CEIL.W.S"); type=FCONV; break;
7914 case 0x0F: strcpy(insn[i],"FLOOR.W.S"); type=FCONV; break;
7915 case 0x21: strcpy(insn[i],"CVT.D.S"); type=FCONV; break;
7916 case 0x24: strcpy(insn[i],"CVT.W.S"); type=FCONV; break;
7917 case 0x25: strcpy(insn[i],"CVT.L.S"); type=FCONV; break;
7918 case 0x30: strcpy(insn[i],"C.F.S"); type=FCOMP; break;
7919 case 0x31: strcpy(insn[i],"C.UN.S"); type=FCOMP; break;
7920 case 0x32: strcpy(insn[i],"C.EQ.S"); type=FCOMP; break;
7921 case 0x33: strcpy(insn[i],"C.UEQ.S"); type=FCOMP; break;
7922 case 0x34: strcpy(insn[i],"C.OLT.S"); type=FCOMP; break;
7923 case 0x35: strcpy(insn[i],"C.ULT.S"); type=FCOMP; break;
7924 case 0x36: strcpy(insn[i],"C.OLE.S"); type=FCOMP; break;
7925 case 0x37: strcpy(insn[i],"C.ULE.S"); type=FCOMP; break;
7926 case 0x38: strcpy(insn[i],"C.SF.S"); type=FCOMP; break;
7927 case 0x39: strcpy(insn[i],"C.NGLE.S"); type=FCOMP; break;
7928 case 0x3A: strcpy(insn[i],"C.SEQ.S"); type=FCOMP; break;
7929 case 0x3B: strcpy(insn[i],"C.NGL.S"); type=FCOMP; break;
7930 case 0x3C: strcpy(insn[i],"C.LT.S"); type=FCOMP; break;
7931 case 0x3D: strcpy(insn[i],"C.NGE.S"); type=FCOMP; break;
7932 case 0x3E: strcpy(insn[i],"C.LE.S"); type=FCOMP; break;
7933 case 0x3F: strcpy(insn[i],"C.NGT.S"); type=FCOMP; break;
7936 case 0x11: strcpy(insn[i],"C1.D"); type=NI;
7937 switch(source[i]&0x3f)
7939 case 0x00: strcpy(insn[i],"ADD.D"); type=FLOAT; break;
7940 case 0x01: strcpy(insn[i],"SUB.D"); type=FLOAT; break;
7941 case 0x02: strcpy(insn[i],"MUL.D"); type=FLOAT; break;
7942 case 0x03: strcpy(insn[i],"DIV.D"); type=FLOAT; break;
7943 case 0x04: strcpy(insn[i],"SQRT.D"); type=FLOAT; break;
7944 case 0x05: strcpy(insn[i],"ABS.D"); type=FLOAT; break;
7945 case 0x06: strcpy(insn[i],"MOV.D"); type=FLOAT; break;
7946 case 0x07: strcpy(insn[i],"NEG.D"); type=FLOAT; break;
7947 case 0x08: strcpy(insn[i],"ROUND.L.D"); type=FCONV; break;
7948 case 0x09: strcpy(insn[i],"TRUNC.L.D"); type=FCONV; break;
7949 case 0x0A: strcpy(insn[i],"CEIL.L.D"); type=FCONV; break;
7950 case 0x0B: strcpy(insn[i],"FLOOR.L.D"); type=FCONV; break;
7951 case 0x0C: strcpy(insn[i],"ROUND.W.D"); type=FCONV; break;
7952 case 0x0D: strcpy(insn[i],"TRUNC.W.D"); type=FCONV; break;
7953 case 0x0E: strcpy(insn[i],"CEIL.W.D"); type=FCONV; break;
7954 case 0x0F: strcpy(insn[i],"FLOOR.W.D"); type=FCONV; break;
7955 case 0x20: strcpy(insn[i],"CVT.S.D"); type=FCONV; break;
7956 case 0x24: strcpy(insn[i],"CVT.W.D"); type=FCONV; break;
7957 case 0x25: strcpy(insn[i],"CVT.L.D"); type=FCONV; break;
7958 case 0x30: strcpy(insn[i],"C.F.D"); type=FCOMP; break;
7959 case 0x31: strcpy(insn[i],"C.UN.D"); type=FCOMP; break;
7960 case 0x32: strcpy(insn[i],"C.EQ.D"); type=FCOMP; break;
7961 case 0x33: strcpy(insn[i],"C.UEQ.D"); type=FCOMP; break;
7962 case 0x34: strcpy(insn[i],"C.OLT.D"); type=FCOMP; break;
7963 case 0x35: strcpy(insn[i],"C.ULT.D"); type=FCOMP; break;
7964 case 0x36: strcpy(insn[i],"C.OLE.D"); type=FCOMP; break;
7965 case 0x37: strcpy(insn[i],"C.ULE.D"); type=FCOMP; break;
7966 case 0x38: strcpy(insn[i],"C.SF.D"); type=FCOMP; break;
7967 case 0x39: strcpy(insn[i],"C.NGLE.D"); type=FCOMP; break;
7968 case 0x3A: strcpy(insn[i],"C.SEQ.D"); type=FCOMP; break;
7969 case 0x3B: strcpy(insn[i],"C.NGL.D"); type=FCOMP; break;
7970 case 0x3C: strcpy(insn[i],"C.LT.D"); type=FCOMP; break;
7971 case 0x3D: strcpy(insn[i],"C.NGE.D"); type=FCOMP; break;
7972 case 0x3E: strcpy(insn[i],"C.LE.D"); type=FCOMP; break;
7973 case 0x3F: strcpy(insn[i],"C.NGT.D"); type=FCOMP; break;
7976 case 0x14: strcpy(insn[i],"C1.W"); type=NI;
7977 switch(source[i]&0x3f)
7979 case 0x20: strcpy(insn[i],"CVT.S.W"); type=FCONV; break;
7980 case 0x21: strcpy(insn[i],"CVT.D.W"); type=FCONV; break;
7983 case 0x15: strcpy(insn[i],"C1.L"); type=NI;
7984 switch(source[i]&0x3f)
7986 case 0x20: strcpy(insn[i],"CVT.S.L"); type=FCONV; break;
7987 case 0x21: strcpy(insn[i],"CVT.D.L"); type=FCONV; break;
7992 case 0x14: strcpy(insn[i],"BEQL"); type=CJUMP; break;
7993 case 0x15: strcpy(insn[i],"BNEL"); type=CJUMP; break;
7994 case 0x16: strcpy(insn[i],"BLEZL"); type=CJUMP; break;
7995 case 0x17: strcpy(insn[i],"BGTZL"); type=CJUMP; break;
7996 case 0x18: strcpy(insn[i],"DADDI"); type=IMM16; break;
7997 case 0x19: strcpy(insn[i],"DADDIU"); type=IMM16; break;
7998 case 0x1A: strcpy(insn[i],"LDL"); type=LOADLR; break;
7999 case 0x1B: strcpy(insn[i],"LDR"); type=LOADLR; break;
8000 case 0x20: strcpy(insn[i],"LB"); type=LOAD; break;
8001 case 0x21: strcpy(insn[i],"LH"); type=LOAD; break;
8002 case 0x22: strcpy(insn[i],"LWL"); type=LOADLR; break;
8003 case 0x23: strcpy(insn[i],"LW"); type=LOAD; break;
8004 case 0x24: strcpy(insn[i],"LBU"); type=LOAD; break;
8005 case 0x25: strcpy(insn[i],"LHU"); type=LOAD; break;
8006 case 0x26: strcpy(insn[i],"LWR"); type=LOADLR; break;
8007 case 0x27: strcpy(insn[i],"LWU"); type=LOAD; break;
8008 case 0x28: strcpy(insn[i],"SB"); type=STORE; break;
8009 case 0x29: strcpy(insn[i],"SH"); type=STORE; break;
8010 case 0x2A: strcpy(insn[i],"SWL"); type=STORELR; break;
8011 case 0x2B: strcpy(insn[i],"SW"); type=STORE; break;
8012 case 0x2C: strcpy(insn[i],"SDL"); type=STORELR; break;
8013 case 0x2D: strcpy(insn[i],"SDR"); type=STORELR; break;
8014 case 0x2E: strcpy(insn[i],"SWR"); type=STORELR; break;
8015 case 0x2F: strcpy(insn[i],"CACHE"); type=NOP; break;
8016 case 0x30: strcpy(insn[i],"LL"); type=NI; break;
8017 case 0x31: strcpy(insn[i],"LWC1"); type=C1LS; break;
8018 case 0x34: strcpy(insn[i],"LLD"); type=NI; break;
8019 case 0x35: strcpy(insn[i],"LDC1"); type=C1LS; break;
8020 case 0x37: strcpy(insn[i],"LD"); type=LOAD; break;
8021 case 0x38: strcpy(insn[i],"SC"); type=NI; break;
8022 case 0x39: strcpy(insn[i],"SWC1"); type=C1LS; break;
8023 case 0x3C: strcpy(insn[i],"SCD"); type=NI; break;
8024 case 0x3D: strcpy(insn[i],"SDC1"); type=C1LS; break;
8025 case 0x3F: strcpy(insn[i],"SD"); type=STORE; break;
8026 default: strcpy(insn[i],"???"); type=NI; break;
8030 /* Get registers/immediates */
8038 rs1[i]=(source[i]>>21)&0x1f;
8040 rt1[i]=(source[i]>>16)&0x1f;
8042 imm[i]=(short)source[i];
8046 rs1[i]=(source[i]>>21)&0x1f;
8047 rs2[i]=(source[i]>>16)&0x1f;
8050 imm[i]=(short)source[i];
8051 if(op==0x2c||op==0x2d||op==0x3f) us1[i]=rs2[i]; // 64-bit SDL/SDR/SD
8054 // LWL/LWR only load part of the register,
8055 // therefore the target register must be treated as a source too
8056 rs1[i]=(source[i]>>21)&0x1f;
8057 rs2[i]=(source[i]>>16)&0x1f;
8058 rt1[i]=(source[i]>>16)&0x1f;
8060 imm[i]=(short)source[i];
8061 if(op==0x1a||op==0x1b) us1[i]=rs2[i]; // LDR/LDL
8062 if(op==0x26) dep1[i]=rt1[i]; // LWR
8065 if (op==0x0f) rs1[i]=0; // LUI instruction has no source register
8066 else rs1[i]=(source[i]>>21)&0x1f;
8068 rt1[i]=(source[i]>>16)&0x1f;
8070 if(op>=0x0c&&op<=0x0e) { // ANDI/ORI/XORI
8071 imm[i]=(unsigned short)source[i];
8073 imm[i]=(short)source[i];
8075 if(op==0x18||op==0x19) us1[i]=rs1[i]; // DADDI/DADDIU
8076 if(op==0x0a||op==0x0b) us1[i]=rs1[i]; // SLTI/SLTIU
8077 if(op==0x0d||op==0x0e) dep1[i]=rs1[i]; // ORI/XORI
8084 // The JAL instruction writes to r31.
8091 rs1[i]=(source[i]>>21)&0x1f;
8095 // The JALR instruction writes to rd.
8097 rt1[i]=(source[i]>>11)&0x1f;
8102 rs1[i]=(source[i]>>21)&0x1f;
8103 rs2[i]=(source[i]>>16)&0x1f;
8106 if(op&2) { // BGTZ/BLEZ
8114 rs1[i]=(source[i]>>21)&0x1f;
8119 if(op2&0x10) { // BxxAL
8121 // NOTE: If the branch is not taken, r31 is still overwritten
8123 likely[i]=(op2&2)>>1;
8130 likely[i]=((source[i])>>17)&1;
8133 rs1[i]=(source[i]>>21)&0x1f; // source
8134 rs2[i]=(source[i]>>16)&0x1f; // subtract amount
8135 rt1[i]=(source[i]>>11)&0x1f; // destination
8137 if(op2==0x2a||op2==0x2b) { // SLT/SLTU
8138 us1[i]=rs1[i];us2[i]=rs2[i];
8140 else if(op2>=0x24&&op2<=0x27) { // AND/OR/XOR/NOR
8141 dep1[i]=rs1[i];dep2[i]=rs2[i];
8143 else if(op2>=0x2c&&op2<=0x2f) { // DADD/DSUB
8144 dep1[i]=rs1[i];dep2[i]=rs2[i];
8148 rs1[i]=(source[i]>>21)&0x1f; // source
8149 rs2[i]=(source[i]>>16)&0x1f; // divisor
8152 if (op2>=0x1c&&op2<=0x1f) { // DMULT/DMULTU/DDIV/DDIVU
8153 us1[i]=rs1[i];us2[i]=rs2[i];
8161 if(op2==0x10) rs1[i]=HIREG; // MFHI
8162 if(op2==0x11) rt1[i]=HIREG; // MTHI
8163 if(op2==0x12) rs1[i]=LOREG; // MFLO
8164 if(op2==0x13) rt1[i]=LOREG; // MTLO
8165 if((op2&0x1d)==0x10) rt1[i]=(source[i]>>11)&0x1f; // MFxx
8166 if((op2&0x1d)==0x11) rs1[i]=(source[i]>>21)&0x1f; // MTxx
8170 rs1[i]=(source[i]>>16)&0x1f; // target of shift
8171 rs2[i]=(source[i]>>21)&0x1f; // shift amount
8172 rt1[i]=(source[i]>>11)&0x1f; // destination
8174 // DSLLV/DSRLV/DSRAV are 64-bit
8175 if(op2>=0x14&&op2<=0x17) us1[i]=rs1[i];
8178 rs1[i]=(source[i]>>16)&0x1f;
8180 rt1[i]=(source[i]>>11)&0x1f;
8182 imm[i]=(source[i]>>6)&0x1f;
8183 // DSxx32 instructions
8184 if(op2>=0x3c) imm[i]|=0x20;
8185 // DSLL/DSRL/DSRA/DSRA32/DSRL32 but not DSLL32 require 64-bit source
8186 if(op2>=0x38&&op2!=0x3c) us1[i]=rs1[i];
8193 if(op2==0) rt1[i]=(source[i]>>16)&0x1F; // MFC0
8194 if(op2==4) rs1[i]=(source[i]>>16)&0x1F; // MTC0
8195 if(op2==4&&((source[i]>>11)&0x1f)==12) rt2[i]=CSREG; // Status
8196 if(op2==16) if((source[i]&0x3f)==0x18) rs2[i]=CCREG; // ERET
8203 if(op2<3) rt1[i]=(source[i]>>16)&0x1F; // MFC1/DMFC1/CFC1
8204 if(op2>3) rs1[i]=(source[i]>>16)&0x1F; // MTC1/DMTC1/CTC1
8205 if(op2==5) us1[i]=rs1[i]; // DMTC1
8209 rs1[i]=(source[i]>>21)&0x1F;
8213 imm[i]=(short)source[i];
8240 /* Calculate branch target addresses */
8242 ba[i]=((start+i*4+4)&0xF0000000)|(((unsigned int)source[i]<<6)>>4);
8243 else if(type==CJUMP&&rs1[i]==rs2[i]&&(op&1))
8244 ba[i]=start+i*4+8; // Ignore never taken branch
8245 else if(type==SJUMP&&rs1[i]==0&&!(op2&1))
8246 ba[i]=start+i*4+8; // Ignore never taken branch
8247 else if(type==CJUMP||type==SJUMP||type==FJUMP)
8248 ba[i]=start+i*4+4+((signed int)((unsigned int)source[i]<<16)>>14);
8250 /* Is this the end of the block? */
8251 if(i>0&&(itype[i-1]==UJUMP||itype[i-1]==RJUMP||(source[i-1]>>16)==0x1000)) {
8252 if(rt1[i-1]==0) { // Continue past subroutine call (JAL)
8254 // Does the block continue due to a branch?
8257 if(ba[j]==start+i*4) done=j=0; // Branch into delay slot
8258 if(ba[j]==start+i*4+4) done=j=0;
8259 if(ba[j]==start+i*4+8) done=j=0;
8263 if(stop_after_jal) done=1;
8265 if((source[i+1]&0xfc00003f)==0x0d) done=1;
8267 // Don't recompile stuff that's already compiled
8268 if(check_addr(start+i*4+4)) done=1;
8269 // Don't get too close to the limit
8270 if(i>MAXBLOCK/2) done=1;
8272 if(i>0&&itype[i-1]==SYSCALL&&stop_after_jal) done=1;
8273 assert(i<MAXBLOCK-1);
8274 if(start+i*4==pagelimit-4) done=1;
8275 assert(start+i*4<pagelimit);
8276 if (i==MAXBLOCK-1) done=1;
8277 // Stop if we're compiling junk
8278 if(itype[i]==NI&&opcode[i]==0x11) {
8279 done=stop_after_jal=1;
8280 DebugMessage(M64MSG_VERBOSE, "Disabled speculative precompilation");
8284 if(itype[i-1]==UJUMP||itype[i-1]==CJUMP||itype[i-1]==SJUMP||itype[i-1]==RJUMP||itype[i-1]==FJUMP) {
8285 if(start+i*4==pagelimit) {
8291 /* Pass 2 - Register dependencies and branch targets */
8293 unneeded_registers(0,slen-1,0);
8295 /* Pass 3 - Register allocation */
8297 struct regstat current; // Current register allocations/status
8300 current.u=unneeded_reg[0];
8301 current.uu=unneeded_reg_upper[0];
8302 clear_all_regs(current.regmap);
8303 alloc_reg(¤t,0,CCREG);
8304 dirty_reg(¤t,CCREG);
8311 provisional_32bit();
8314 // First instruction is delay slot
8319 unneeded_reg_upper[0]=1;
8320 current.regmap[HOST_BTREG]=BTREG;
8328 for(hr=0;hr<HOST_REGS;hr++)
8330 // Is this really necessary?
8331 if(current.regmap[hr]==0) current.regmap[hr]=-1;
8337 if((opcode[i-2]&0x2f)==0x05) // BNE/BNEL
8339 if(rs1[i-2]==0||rs2[i-2]==0)
8342 current.is32|=1LL<<rs1[i-2];
8343 int hr=get_reg(current.regmap,rs1[i-2]|64);
8344 if(hr>=0) current.regmap[hr]=-1;
8347 current.is32|=1LL<<rs2[i-2];
8348 int hr=get_reg(current.regmap,rs2[i-2]|64);
8349 if(hr>=0) current.regmap[hr]=-1;
8354 // If something jumps here with 64-bit values
8355 // then promote those registers to 64 bits
8358 uint64_t temp_is32=current.is32;
8361 if(ba[j]==start+i*4)
8362 temp_is32&=branch_regs[j].is32;
8366 if(ba[j]==start+i*4)
8370 if(temp_is32!=current.is32) {
8371 //DebugMessage(M64MSG_VERBOSE, "dumping 32-bit regs (%x)",start+i*4);
8372 #ifndef DESTRUCTIVE_WRITEBACK
8375 for(hr=0;hr<HOST_REGS;hr++)
8377 int r=current.regmap[hr];
8380 if((current.dirty>>hr)&((current.is32&~temp_is32)>>r)&1) {
8382 //DebugMessage(M64MSG_VERBOSE, "restore %d",r);
8386 current.is32=temp_is32;
8389 memcpy(regmap_pre[i],current.regmap,sizeof(current.regmap));
8390 regs[i].wasconst=current.isconst;
8391 regs[i].was32=current.is32;
8392 regs[i].wasdirty=current.dirty;
8393 #ifdef DESTRUCTIVE_WRITEBACK
8394 // To change a dirty register from 32 to 64 bits, we must write
8395 // it out during the previous cycle (for branches, 2 cycles)
8396 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)
8398 uint64_t temp_is32=current.is32;
8401 if(ba[j]==start+i*4+4)
8402 temp_is32&=branch_regs[j].is32;
8406 if(ba[j]==start+i*4+4)
8410 if(temp_is32!=current.is32) {
8411 //DebugMessage(M64MSG_VERBOSE, "pre-dumping 32-bit regs (%x)",start+i*4);
8412 for(hr=0;hr<HOST_REGS;hr++)
8414 int r=current.regmap[hr];
8417 if((current.dirty>>hr)&((current.is32&~temp_is32)>>(r&63))&1) {
8418 if(itype[i]!=UJUMP&&itype[i]!=CJUMP&&itype[i]!=SJUMP&&itype[i]!=RJUMP&&itype[i]!=FJUMP)
8420 if(rs1[i]!=(r&63)&&rs2[i]!=(r&63))
8422 //DebugMessage(M64MSG_VERBOSE, "dump %d/r%d",hr,r);
8423 current.regmap[hr]=-1;
8424 if(get_reg(current.regmap,r|64)>=0)
8425 current.regmap[get_reg(current.regmap,r|64)]=-1;
8433 else if(i<slen-2&&bt[i+2]&&(source[i-1]>>16)!=0x1000&&(itype[i]==CJUMP||itype[i]==SJUMP||itype[i]==FJUMP))
8435 uint64_t temp_is32=current.is32;
8438 if(ba[j]==start+i*4+8)
8439 temp_is32&=branch_regs[j].is32;
8443 if(ba[j]==start+i*4+8)
8447 if(temp_is32!=current.is32) {
8448 //DebugMessage(M64MSG_VERBOSE, "pre-dumping 32-bit regs (%x)",start+i*4);
8449 for(hr=0;hr<HOST_REGS;hr++)
8451 int r=current.regmap[hr];
8454 if((current.dirty>>hr)&((current.is32&~temp_is32)>>(r&63))&1) {
8455 if(rs1[i]!=(r&63)&&rs2[i]!=(r&63)&&rs1[i+1]!=(r&63)&&rs2[i+1]!=(r&63))
8457 //DebugMessage(M64MSG_VERBOSE, "dump %d/r%d",hr,r);
8458 current.regmap[hr]=-1;
8459 if(get_reg(current.regmap,r|64)>=0)
8460 current.regmap[get_reg(current.regmap,r|64)]=-1;
8468 if(itype[i]!=UJUMP&&itype[i]!=CJUMP&&itype[i]!=SJUMP&&itype[i]!=RJUMP&&itype[i]!=FJUMP) {
8470 current.u=unneeded_reg[i+1]&~((1LL<<rs1[i])|(1LL<<rs2[i]));
8471 current.uu=unneeded_reg_upper[i+1]&~((1LL<<us1[i])|(1LL<<us2[i]));
8472 if((~current.uu>>rt1[i])&1) current.uu&=~((1LL<<dep1[i])|(1LL<<dep2[i]));
8481 current.u=branch_unneeded_reg[i]&~((1LL<<rs1[i+1])|(1LL<<rs2[i+1]));
8482 current.uu=branch_unneeded_reg_upper[i]&~((1LL<<us1[i+1])|(1LL<<us2[i+1]));
8483 if((~current.uu>>rt1[i+1])&1) current.uu&=~((1LL<<dep1[i+1])|(1LL<<dep2[i+1]));
8484 current.u&=~((1LL<<rs1[i])|(1LL<<rs2[i]));
8485 current.uu&=~((1LL<<us1[i])|(1LL<<us2[i]));
8488 } else { DebugMessage(M64MSG_ERROR, "oops, branch at end of block with no delay slot");exit(1); }
8492 ds=0; // Skip delay slot, already allocated as part of branch
8493 // ...but we need to alloc it in case something jumps here
8495 current.u=branch_unneeded_reg[i-1]&unneeded_reg[i+1];
8496 current.uu=branch_unneeded_reg_upper[i-1]&unneeded_reg_upper[i+1];
8498 current.u=branch_unneeded_reg[i-1];
8499 current.uu=branch_unneeded_reg_upper[i-1];
8501 current.u&=~((1LL<<rs1[i])|(1LL<<rs2[i]));
8502 current.uu&=~((1LL<<us1[i])|(1LL<<us2[i]));
8503 if((~current.uu>>rt1[i])&1) current.uu&=~((1LL<<dep1[i])|(1LL<<dep2[i]));
8506 struct regstat temp;
8507 memcpy(&temp,¤t,sizeof(current));
8508 temp.wasdirty=temp.dirty;
8509 temp.was32=temp.is32;
8510 // TODO: Take into account unconditional branches, as below
8511 delayslot_alloc(&temp,i);
8512 memcpy(regs[i].regmap,temp.regmap,sizeof(temp.regmap));
8513 regs[i].wasdirty=temp.wasdirty;
8514 regs[i].was32=temp.was32;
8515 regs[i].dirty=temp.dirty;
8516 regs[i].is32=temp.is32;
8520 // Create entry (branch target) regmap
8521 for(hr=0;hr<HOST_REGS;hr++)
8523 int r=temp.regmap[hr];
8525 if(r!=regmap_pre[i][hr]) {
8526 regs[i].regmap_entry[hr]=-1;
8531 if((current.u>>r)&1) {
8532 regs[i].regmap_entry[hr]=-1;
8533 regs[i].regmap[hr]=-1;
8534 //Don't clear regs in the delay slot as the branch might need them
8535 //current.regmap[hr]=-1;
8537 regs[i].regmap_entry[hr]=r;
8540 if((current.uu>>(r&63))&1) {
8541 regs[i].regmap_entry[hr]=-1;
8542 regs[i].regmap[hr]=-1;
8543 //Don't clear regs in the delay slot as the branch might need them
8544 //current.regmap[hr]=-1;
8546 regs[i].regmap_entry[hr]=r;
8550 // First instruction expects CCREG to be allocated
8551 if(i==0&&hr==HOST_CCREG)
8552 regs[i].regmap_entry[hr]=CCREG;
8554 regs[i].regmap_entry[hr]=-1;
8558 else { // Not delay slot
8561 //current.isconst=0; // DEBUG
8562 //current.wasconst=0; // DEBUG
8563 //regs[i].wasconst=0; // DEBUG
8564 clear_const(¤t,rt1[i]);
8565 alloc_cc(¤t,i);
8566 dirty_reg(¤t,CCREG);
8568 alloc_reg(¤t,i,31);
8569 dirty_reg(¤t,31);
8570 assert(rs1[i+1]!=31&&rs2[i+1]!=31);
8572 alloc_reg(¤t,i,PTEMP);
8574 //current.is32|=1LL<<rt1[i];
8577 delayslot_alloc(¤t,i+1);
8578 //current.isconst=0; // DEBUG
8580 //DebugMessage(M64MSG_VERBOSE, "i=%d, isconst=%x",i,current.isconst);
8583 //current.isconst=0;
8584 //current.wasconst=0;
8585 //regs[i].wasconst=0;
8586 clear_const(¤t,rs1[i]);
8587 clear_const(¤t,rt1[i]);
8588 alloc_cc(¤t,i);
8589 dirty_reg(¤t,CCREG);
8590 if(rs1[i]!=rt1[i+1]&&rs1[i]!=rt2[i+1]) {
8591 alloc_reg(¤t,i,rs1[i]);
8593 alloc_reg(¤t,i,rt1[i]);
8594 dirty_reg(¤t,rt1[i]);
8595 assert(rs1[i+1]!=31&&rs2[i+1]!=31);
8597 alloc_reg(¤t,i,PTEMP);
8601 if(rs1[i]==31) { // JALR
8602 alloc_reg(¤t,i,RHASH);
8603 #ifndef HOST_IMM_ADDR32
8604 alloc_reg(¤t,i,RHTBL);
8608 delayslot_alloc(¤t,i+1);
8610 // The delay slot overwrites our source register,
8611 // allocate a temporary register to hold the old value.
8615 delayslot_alloc(¤t,i+1);
8617 alloc_reg(¤t,i,RTEMP);
8619 //current.isconst=0; // DEBUG
8624 //current.isconst=0;
8625 //current.wasconst=0;
8626 //regs[i].wasconst=0;
8627 clear_const(¤t,rs1[i]);
8628 clear_const(¤t,rs2[i]);
8629 if((opcode[i]&0x3E)==4) // BEQ/BNE
8631 alloc_cc(¤t,i);
8632 dirty_reg(¤t,CCREG);
8633 if(rs1[i]) alloc_reg(¤t,i,rs1[i]);
8634 if(rs2[i]) alloc_reg(¤t,i,rs2[i]);
8635 if(!((current.is32>>rs1[i])&(current.is32>>rs2[i])&1))
8637 if(rs1[i]) alloc_reg64(¤t,i,rs1[i]);
8638 if(rs2[i]) alloc_reg64(¤t,i,rs2[i]);
8640 if((rs1[i]&&(rs1[i]==rt1[i+1]||rs1[i]==rt2[i+1]))||
8641 (rs2[i]&&(rs2[i]==rt1[i+1]||rs2[i]==rt2[i+1]))) {
8642 // The delay slot overwrites one of our conditions.
8643 // Allocate the branch condition registers instead.
8647 if(rs1[i]) alloc_reg(¤t,i,rs1[i]);
8648 if(rs2[i]) alloc_reg(¤t,i,rs2[i]);
8649 if(!((current.is32>>rs1[i])&(current.is32>>rs2[i])&1))
8651 if(rs1[i]) alloc_reg64(¤t,i,rs1[i]);
8652 if(rs2[i]) alloc_reg64(¤t,i,rs2[i]);
8658 delayslot_alloc(¤t,i+1);
8662 if((opcode[i]&0x3E)==6) // BLEZ/BGTZ
8664 alloc_cc(¤t,i);
8665 dirty_reg(¤t,CCREG);
8666 alloc_reg(¤t,i,rs1[i]);
8667 if(!(current.is32>>rs1[i]&1))
8669 alloc_reg64(¤t,i,rs1[i]);
8671 if(rs1[i]&&(rs1[i]==rt1[i+1]||rs1[i]==rt2[i+1])) {
8672 // The delay slot overwrites one of our conditions.
8673 // Allocate the branch condition registers instead.
8677 if(rs1[i]) alloc_reg(¤t,i,rs1[i]);
8678 if(!((current.is32>>rs1[i])&1))
8680 if(rs1[i]) alloc_reg64(¤t,i,rs1[i]);
8686 delayslot_alloc(¤t,i+1);
8690 // Don't alloc the delay slot yet because we might not execute it
8691 if((opcode[i]&0x3E)==0x14) // BEQL/BNEL
8696 alloc_cc(¤t,i);
8697 dirty_reg(¤t,CCREG);
8698 alloc_reg(¤t,i,rs1[i]);
8699 alloc_reg(¤t,i,rs2[i]);
8700 if(!((current.is32>>rs1[i])&(current.is32>>rs2[i])&1))
8702 alloc_reg64(¤t,i,rs1[i]);
8703 alloc_reg64(¤t,i,rs2[i]);
8707 if((opcode[i]&0x3E)==0x16) // BLEZL/BGTZL
8712 alloc_cc(¤t,i);
8713 dirty_reg(¤t,CCREG);
8714 alloc_reg(¤t,i,rs1[i]);
8715 if(!(current.is32>>rs1[i]&1))
8717 alloc_reg64(¤t,i,rs1[i]);
8721 //current.isconst=0;
8724 //current.isconst=0;
8725 //current.wasconst=0;
8726 //regs[i].wasconst=0;
8727 clear_const(¤t,rs1[i]);
8728 clear_const(¤t,rt1[i]);
8729 //if((opcode2[i]&0x1E)==0x0) // BLTZ/BGEZ
8730 if((opcode2[i]&0x0E)==0x0) // BLTZ/BGEZ
8732 alloc_cc(¤t,i);
8733 dirty_reg(¤t,CCREG);
8734 alloc_reg(¤t,i,rs1[i]);
8735 if(!(current.is32>>rs1[i]&1))
8737 alloc_reg64(¤t,i,rs1[i]);
8739 if (rt1[i]==31) { // BLTZAL/BGEZAL
8740 alloc_reg(¤t,i,31);
8741 dirty_reg(¤t,31);
8742 assert(rs1[i+1]!=31&&rs2[i+1]!=31);
8743 //#ifdef REG_PREFETCH
8744 //alloc_reg(¤t,i,PTEMP);
8746 //current.is32|=1LL<<rt1[i];
8748 if(rs1[i]&&(rs1[i]==rt1[i+1]||rs1[i]==rt2[i+1])) {
8749 // The delay slot overwrites the branch condition.
8750 // Allocate the branch condition registers instead.
8754 if(rs1[i]) alloc_reg(¤t,i,rs1[i]);
8755 if(!((current.is32>>rs1[i])&1))
8757 if(rs1[i]) alloc_reg64(¤t,i,rs1[i]);
8763 delayslot_alloc(¤t,i+1);
8767 // Don't alloc the delay slot yet because we might not execute it
8768 if((opcode2[i]&0x1E)==0x2) // BLTZL/BGEZL
8773 alloc_cc(¤t,i);
8774 dirty_reg(¤t,CCREG);
8775 alloc_reg(¤t,i,rs1[i]);
8776 if(!(current.is32>>rs1[i]&1))
8778 alloc_reg64(¤t,i,rs1[i]);
8782 //current.isconst=0;
8788 if(likely[i]==0) // BC1F/BC1T
8790 // TODO: Theoretically we can run out of registers here on x86.
8791 // The delay slot can allocate up to six, and we need to check
8792 // CSREG before executing the delay slot. Possibly we can drop
8793 // the cycle count and then reload it after checking that the
8794 // FPU is in a usable state, or don't do out-of-order execution.
8795 alloc_cc(¤t,i);
8796 dirty_reg(¤t,CCREG);
8797 alloc_reg(¤t,i,FSREG);
8798 alloc_reg(¤t,i,CSREG);
8799 if(itype[i+1]==FCOMP) {
8800 // The delay slot overwrites the branch condition.
8801 // Allocate the branch condition registers instead.
8802 alloc_cc(¤t,i);
8803 dirty_reg(¤t,CCREG);
8804 alloc_reg(¤t,i,CSREG);
8805 alloc_reg(¤t,i,FSREG);
8809 delayslot_alloc(¤t,i+1);
8810 alloc_reg(¤t,i+1,CSREG);
8814 // Don't alloc the delay slot yet because we might not execute it
8815 if(likely[i]) // BC1FL/BC1TL
8817 alloc_cc(¤t,i);
8818 dirty_reg(¤t,CCREG);
8819 alloc_reg(¤t,i,CSREG);
8820 alloc_reg(¤t,i,FSREG);
8826 imm16_alloc(¤t,i);
8830 load_alloc(¤t,i);
8834 store_alloc(¤t,i);
8837 alu_alloc(¤t,i);
8840 shift_alloc(¤t,i);
8843 multdiv_alloc(¤t,i);
8846 shiftimm_alloc(¤t,i);
8849 mov_alloc(¤t,i);
8852 cop0_alloc(¤t,i);
8855 cop1_alloc(¤t,i);
8858 c1ls_alloc(¤t,i);
8861 fconv_alloc(¤t,i);
8864 float_alloc(¤t,i);
8867 fcomp_alloc(¤t,i);
8870 syscall_alloc(¤t,i);
8873 pagespan_alloc(¤t,i);
8877 // Drop the upper half of registers that have become 32-bit
8878 current.uu|=current.is32&((1LL<<rt1[i])|(1LL<<rt2[i]));
8879 if(itype[i]!=UJUMP&&itype[i]!=CJUMP&&itype[i]!=SJUMP&&itype[i]!=RJUMP&&itype[i]!=FJUMP) {
8880 current.uu&=~((1LL<<us1[i])|(1LL<<us2[i]));
8881 if((~current.uu>>rt1[i])&1) current.uu&=~((1LL<<dep1[i])|(1LL<<dep2[i]));
8884 current.uu|=current.is32&((1LL<<rt1[i+1])|(1LL<<rt2[i+1]));
8885 current.uu&=~((1LL<<us1[i+1])|(1LL<<us2[i+1]));
8886 if((~current.uu>>rt1[i+1])&1) current.uu&=~((1LL<<dep1[i+1])|(1LL<<dep2[i+1]));
8887 current.uu&=~((1LL<<us1[i])|(1LL<<us2[i]));
8891 // Create entry (branch target) regmap
8892 for(hr=0;hr<HOST_REGS;hr++)
8895 r=current.regmap[hr];
8897 if(r!=regmap_pre[i][hr]) {
8898 // TODO: delay slot (?)
8899 or=get_reg(regmap_pre[i],r); // Get old mapping for this register
8900 if(or<0||(r&63)>=TEMPREG){
8901 regs[i].regmap_entry[hr]=-1;
8905 // Just move it to a different register
8906 regs[i].regmap_entry[hr]=r;
8907 // If it was dirty before, it's still dirty
8908 if((regs[i].wasdirty>>or)&1) dirty_reg(¤t,r&63);
8915 regs[i].regmap_entry[hr]=0;
8919 if((current.u>>r)&1) {
8920 regs[i].regmap_entry[hr]=-1;
8921 //regs[i].regmap[hr]=-1;
8922 current.regmap[hr]=-1;
8924 regs[i].regmap_entry[hr]=r;
8927 if((current.uu>>(r&63))&1) {
8928 regs[i].regmap_entry[hr]=-1;
8929 //regs[i].regmap[hr]=-1;
8930 current.regmap[hr]=-1;
8932 regs[i].regmap_entry[hr]=r;
8936 // Branches expect CCREG to be allocated at the target
8937 if(regmap_pre[i][hr]==CCREG)
8938 regs[i].regmap_entry[hr]=CCREG;
8940 regs[i].regmap_entry[hr]=-1;
8943 memcpy(regs[i].regmap,current.regmap,sizeof(current.regmap));
8945 /* Branch post-alloc */
8948 current.was32=current.is32;
8949 current.wasdirty=current.dirty;
8950 switch(itype[i-1]) {
8952 memcpy(&branch_regs[i-1],¤t,sizeof(current));
8953 branch_regs[i-1].isconst=0;
8954 branch_regs[i-1].wasconst=0;
8955 branch_regs[i-1].u=branch_unneeded_reg[i-1]&~((1LL<<rs1[i-1])|(1LL<<rs2[i-1]));
8956 branch_regs[i-1].uu=branch_unneeded_reg_upper[i-1]&~((1LL<<us1[i-1])|(1LL<<us2[i-1]));
8957 alloc_cc(&branch_regs[i-1],i-1);
8958 dirty_reg(&branch_regs[i-1],CCREG);
8959 if(rt1[i-1]==31) { // JAL
8960 alloc_reg(&branch_regs[i-1],i-1,31);
8961 dirty_reg(&branch_regs[i-1],31);
8962 branch_regs[i-1].is32|=1LL<<31;
8964 memcpy(&branch_regs[i-1].regmap_entry,&branch_regs[i-1].regmap,sizeof(current.regmap));
8965 memcpy(constmap[i],constmap[i-1],sizeof(current.constmap));
8968 memcpy(&branch_regs[i-1],¤t,sizeof(current));
8969 branch_regs[i-1].isconst=0;
8970 branch_regs[i-1].wasconst=0;
8971 branch_regs[i-1].u=branch_unneeded_reg[i-1]&~((1LL<<rs1[i-1])|(1LL<<rs2[i-1]));
8972 branch_regs[i-1].uu=branch_unneeded_reg_upper[i-1]&~((1LL<<us1[i-1])|(1LL<<us2[i-1]));
8973 alloc_cc(&branch_regs[i-1],i-1);
8974 dirty_reg(&branch_regs[i-1],CCREG);
8975 alloc_reg(&branch_regs[i-1],i-1,rs1[i-1]);
8976 if(rt1[i-1]!=0) { // JALR
8977 alloc_reg(&branch_regs[i-1],i-1,rt1[i-1]);
8978 dirty_reg(&branch_regs[i-1],rt1[i-1]);
8979 branch_regs[i-1].is32|=1LL<<rt1[i-1];
8982 if(rs1[i-1]==31) { // JALR
8983 alloc_reg(&branch_regs[i-1],i-1,RHASH);
8984 #ifndef HOST_IMM_ADDR32
8985 alloc_reg(&branch_regs[i-1],i-1,RHTBL);
8989 memcpy(&branch_regs[i-1].regmap_entry,&branch_regs[i-1].regmap,sizeof(current.regmap));
8990 memcpy(constmap[i],constmap[i-1],sizeof(current.constmap));
8993 if((opcode[i-1]&0x3E)==4) // BEQ/BNE
8995 alloc_cc(¤t,i-1);
8996 dirty_reg(¤t,CCREG);
8997 if((rs1[i-1]&&(rs1[i-1]==rt1[i]||rs1[i-1]==rt2[i]))||
8998 (rs2[i-1]&&(rs2[i-1]==rt1[i]||rs2[i-1]==rt2[i]))) {
8999 // The delay slot overwrote one of our conditions
9000 // Delay slot goes after the test (in order)
9001 current.u=branch_unneeded_reg[i-1]&~((1LL<<rs1[i])|(1LL<<rs2[i]));
9002 current.uu=branch_unneeded_reg_upper[i-1]&~((1LL<<us1[i])|(1LL<<us2[i]));
9003 if((~current.uu>>rt1[i])&1) current.uu&=~((1LL<<dep1[i])|(1LL<<dep2[i]));
9006 delayslot_alloc(¤t,i);
9011 current.u=branch_unneeded_reg[i-1]&~((1LL<<rs1[i-1])|(1LL<<rs2[i-1]));
9012 current.uu=branch_unneeded_reg_upper[i-1]&~((1LL<<us1[i-1])|(1LL<<us2[i-1]));
9013 // Alloc the branch condition registers
9014 if(rs1[i-1]) alloc_reg(¤t,i-1,rs1[i-1]);
9015 if(rs2[i-1]) alloc_reg(¤t,i-1,rs2[i-1]);
9016 if(!((current.is32>>rs1[i-1])&(current.is32>>rs2[i-1])&1))
9018 if(rs1[i-1]) alloc_reg64(¤t,i-1,rs1[i-1]);
9019 if(rs2[i-1]) alloc_reg64(¤t,i-1,rs2[i-1]);
9022 memcpy(&branch_regs[i-1],¤t,sizeof(current));
9023 branch_regs[i-1].isconst=0;
9024 branch_regs[i-1].wasconst=0;
9025 memcpy(&branch_regs[i-1].regmap_entry,¤t.regmap,sizeof(current.regmap));
9026 memcpy(constmap[i],constmap[i-1],sizeof(current.constmap));
9029 if((opcode[i-1]&0x3E)==6) // BLEZ/BGTZ
9031 alloc_cc(¤t,i-1);
9032 dirty_reg(¤t,CCREG);
9033 if(rs1[i-1]==rt1[i]||rs1[i-1]==rt2[i]) {
9034 // The delay slot overwrote the branch condition
9035 // Delay slot goes after the test (in order)
9036 current.u=branch_unneeded_reg[i-1]&~((1LL<<rs1[i])|(1LL<<rs2[i]));
9037 current.uu=branch_unneeded_reg_upper[i-1]&~((1LL<<us1[i])|(1LL<<us2[i]));
9038 if((~current.uu>>rt1[i])&1) current.uu&=~((1LL<<dep1[i])|(1LL<<dep2[i]));
9041 delayslot_alloc(¤t,i);
9046 current.u=branch_unneeded_reg[i-1]&~(1LL<<rs1[i-1]);
9047 current.uu=branch_unneeded_reg_upper[i-1]&~(1LL<<us1[i-1]);
9048 // Alloc the branch condition register
9049 alloc_reg(¤t,i-1,rs1[i-1]);
9050 if(!(current.is32>>rs1[i-1]&1))
9052 alloc_reg64(¤t,i-1,rs1[i-1]);
9055 memcpy(&branch_regs[i-1],¤t,sizeof(current));
9056 branch_regs[i-1].isconst=0;
9057 branch_regs[i-1].wasconst=0;
9058 memcpy(&branch_regs[i-1].regmap_entry,¤t.regmap,sizeof(current.regmap));
9059 memcpy(constmap[i],constmap[i-1],sizeof(current.constmap));
9062 // Alloc the delay slot in case the branch is taken
9063 if((opcode[i-1]&0x3E)==0x14) // BEQL/BNEL
9065 memcpy(&branch_regs[i-1],¤t,sizeof(current));
9066 branch_regs[i-1].u=(branch_unneeded_reg[i-1]&~((1LL<<rs1[i])|(1LL<<rs2[i])|(1LL<<rt1[i])|(1LL<<rt2[i])))|1;
9067 branch_regs[i-1].uu=(branch_unneeded_reg_upper[i-1]&~((1LL<<us1[i])|(1LL<<us2[i])|(1LL<<rt1[i])|(1LL<<rt2[i])))|1;
9068 if((~branch_regs[i-1].uu>>rt1[i])&1) branch_regs[i-1].uu&=~((1LL<<dep1[i])|(1LL<<dep2[i]))|1;
9069 alloc_cc(&branch_regs[i-1],i);
9070 dirty_reg(&branch_regs[i-1],CCREG);
9071 delayslot_alloc(&branch_regs[i-1],i);
9072 branch_regs[i-1].isconst=0;
9073 alloc_reg(¤t,i,CCREG); // Not taken path
9074 dirty_reg(¤t,CCREG);
9075 memcpy(&branch_regs[i-1].regmap_entry,&branch_regs[i-1].regmap,sizeof(current.regmap));
9078 if((opcode[i-1]&0x3E)==0x16) // BLEZL/BGTZL
9080 memcpy(&branch_regs[i-1],¤t,sizeof(current));
9081 branch_regs[i-1].u=(branch_unneeded_reg[i-1]&~((1LL<<rs1[i])|(1LL<<rs2[i])|(1LL<<rt1[i])|(1LL<<rt2[i])))|1;
9082 branch_regs[i-1].uu=(branch_unneeded_reg_upper[i-1]&~((1LL<<us1[i])|(1LL<<us2[i])|(1LL<<rt1[i])|(1LL<<rt2[i])))|1;
9083 if((~branch_regs[i-1].uu>>rt1[i])&1) branch_regs[i-1].uu&=~((1LL<<dep1[i])|(1LL<<dep2[i]))|1;
9084 alloc_cc(&branch_regs[i-1],i);
9085 dirty_reg(&branch_regs[i-1],CCREG);
9086 delayslot_alloc(&branch_regs[i-1],i);
9087 branch_regs[i-1].isconst=0;
9088 alloc_reg(¤t,i,CCREG); // Not taken path
9089 dirty_reg(¤t,CCREG);
9090 memcpy(&branch_regs[i-1].regmap_entry,&branch_regs[i-1].regmap,sizeof(current.regmap));
9094 //if((opcode2[i-1]&0x1E)==0) // BLTZ/BGEZ
9095 if((opcode2[i-1]&0x0E)==0) // BLTZ/BGEZ
9097 alloc_cc(¤t,i-1);
9098 dirty_reg(¤t,CCREG);
9099 if(rs1[i-1]==rt1[i]||rs1[i-1]==rt2[i]) {
9100 // The delay slot overwrote the branch condition
9101 // Delay slot goes after the test (in order)
9102 current.u=branch_unneeded_reg[i-1]&~((1LL<<rs1[i])|(1LL<<rs2[i]));
9103 current.uu=branch_unneeded_reg_upper[i-1]&~((1LL<<us1[i])|(1LL<<us2[i]));
9104 if((~current.uu>>rt1[i])&1) current.uu&=~((1LL<<dep1[i])|(1LL<<dep2[i]));
9107 delayslot_alloc(¤t,i);
9112 current.u=branch_unneeded_reg[i-1]&~(1LL<<rs1[i-1]);
9113 current.uu=branch_unneeded_reg_upper[i-1]&~(1LL<<us1[i-1]);
9114 // Alloc the branch condition register
9115 alloc_reg(¤t,i-1,rs1[i-1]);
9116 if(!(current.is32>>rs1[i-1]&1))
9118 alloc_reg64(¤t,i-1,rs1[i-1]);
9121 memcpy(&branch_regs[i-1],¤t,sizeof(current));
9122 branch_regs[i-1].isconst=0;
9123 branch_regs[i-1].wasconst=0;
9124 memcpy(&branch_regs[i-1].regmap_entry,¤t.regmap,sizeof(current.regmap));
9125 memcpy(constmap[i],constmap[i-1],sizeof(current.constmap));
9128 // Alloc the delay slot in case the branch is taken
9129 if((opcode2[i-1]&0x1E)==2) // BLTZL/BGEZL
9131 memcpy(&branch_regs[i-1],¤t,sizeof(current));
9132 branch_regs[i-1].u=(branch_unneeded_reg[i-1]&~((1LL<<rs1[i])|(1LL<<rs2[i])|(1LL<<rt1[i])|(1LL<<rt2[i])))|1;
9133 branch_regs[i-1].uu=(branch_unneeded_reg_upper[i-1]&~((1LL<<us1[i])|(1LL<<us2[i])|(1LL<<rt1[i])|(1LL<<rt2[i])))|1;
9134 if((~branch_regs[i-1].uu>>rt1[i])&1) branch_regs[i-1].uu&=~((1LL<<dep1[i])|(1LL<<dep2[i]))|1;
9135 alloc_cc(&branch_regs[i-1],i);
9136 dirty_reg(&branch_regs[i-1],CCREG);
9137 delayslot_alloc(&branch_regs[i-1],i);
9138 branch_regs[i-1].isconst=0;
9139 alloc_reg(¤t,i,CCREG); // Not taken path
9140 dirty_reg(¤t,CCREG);
9141 memcpy(&branch_regs[i-1].regmap_entry,&branch_regs[i-1].regmap,sizeof(current.regmap));
9143 // FIXME: BLTZAL/BGEZAL
9144 if(opcode2[i-1]&0x10) { // BxxZAL
9145 alloc_reg(&branch_regs[i-1],i-1,31);
9146 dirty_reg(&branch_regs[i-1],31);
9147 branch_regs[i-1].is32|=1LL<<31;
9151 if(likely[i-1]==0) // BC1F/BC1T
9153 alloc_cc(¤t,i-1);
9154 dirty_reg(¤t,CCREG);
9155 if(itype[i]==FCOMP) {
9156 // The delay slot overwrote the branch condition
9157 // Delay slot goes after the test (in order)
9158 delayslot_alloc(¤t,i);
9163 current.u=branch_unneeded_reg[i-1]&~(1LL<<rs1[i-1]);
9164 current.uu=branch_unneeded_reg_upper[i-1]&~(1LL<<us1[i-1]);
9165 // Alloc the branch condition register
9166 alloc_reg(¤t,i-1,FSREG);
9168 memcpy(&branch_regs[i-1],¤t,sizeof(current));
9169 memcpy(&branch_regs[i-1].regmap_entry,¤t.regmap,sizeof(current.regmap));
9173 // Alloc the delay slot in case the branch is taken
9174 memcpy(&branch_regs[i-1],¤t,sizeof(current));
9175 branch_regs[i-1].u=(branch_unneeded_reg[i-1]&~((1LL<<rs1[i])|(1LL<<rs2[i])|(1LL<<rt1[i])|(1LL<<rt2[i])))|1;
9176 branch_regs[i-1].uu=(branch_unneeded_reg_upper[i-1]&~((1LL<<us1[i])|(1LL<<us2[i])|(1LL<<rt1[i])|(1LL<<rt2[i])))|1;
9177 if((~branch_regs[i-1].uu>>rt1[i])&1) branch_regs[i-1].uu&=~((1LL<<dep1[i])|(1LL<<dep2[i]))|1;
9178 alloc_cc(&branch_regs[i-1],i);
9179 dirty_reg(&branch_regs[i-1],CCREG);
9180 delayslot_alloc(&branch_regs[i-1],i);
9181 branch_regs[i-1].isconst=0;
9182 alloc_reg(¤t,i,CCREG); // Not taken path
9183 dirty_reg(¤t,CCREG);
9184 memcpy(&branch_regs[i-1].regmap_entry,&branch_regs[i-1].regmap,sizeof(current.regmap));
9189 if(itype[i-1]==UJUMP||itype[i-1]==RJUMP||(source[i-1]>>16)==0x1000)
9191 if(rt1[i-1]==31) // JAL/JALR
9193 // Subroutine call will return here, don't alloc any registers
9196 clear_all_regs(current.regmap);
9197 alloc_reg(¤t,i,CCREG);
9198 dirty_reg(¤t,CCREG);
9202 // Internal branch will jump here, match registers to caller
9203 current.is32=0x3FFFFFFFFLL;
9205 clear_all_regs(current.regmap);
9206 alloc_reg(¤t,i,CCREG);
9207 dirty_reg(¤t,CCREG);
9210 if(ba[j]==start+i*4+4) {
9211 memcpy(current.regmap,branch_regs[j].regmap,sizeof(current.regmap));
9212 current.is32=branch_regs[j].is32;
9213 current.dirty=branch_regs[j].dirty;
9218 if(ba[j]==start+i*4+4) {
9219 for(hr=0;hr<HOST_REGS;hr++) {
9220 if(current.regmap[hr]!=branch_regs[j].regmap[hr]) {
9221 current.regmap[hr]=-1;
9223 current.is32&=branch_regs[j].is32;
9224 current.dirty&=branch_regs[j].dirty;
9233 // Count cycles in between branches
9235 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))
9244 flush_dirty_uppers(¤t);
9246 regs[i].is32=current.is32;
9247 regs[i].dirty=current.dirty;
9248 regs[i].isconst=current.isconst;
9249 memcpy(constmap[i],current.constmap,sizeof(current.constmap));
9251 for(hr=0;hr<HOST_REGS;hr++) {
9252 if(hr!=EXCLUDE_REG&®s[i].regmap[hr]>=0) {
9253 if(regmap_pre[i][hr]!=regs[i].regmap[hr]) {
9254 regs[i].wasconst&=~(1<<hr);
9258 if(current.regmap[HOST_BTREG]==BTREG) current.regmap[HOST_BTREG]=-1;
9261 /* Pass 4 - Cull unused host registers */
9265 for (i=slen-1;i>=0;i--)
9268 if(itype[i]==RJUMP||itype[i]==UJUMP||itype[i]==CJUMP||itype[i]==SJUMP||itype[i]==FJUMP)
9270 if(ba[i]<start || ba[i]>=(start+slen*4))
9272 // Branch out of this block, don't need anything
9278 // Need whatever matches the target
9280 int t=(ba[i]-start)>>2;
9281 for(hr=0;hr<HOST_REGS;hr++)
9283 if(regs[i].regmap_entry[hr]>=0) {
9284 if(regs[i].regmap_entry[hr]==regs[t].regmap_entry[hr]) nr|=1<<hr;
9288 // Conditional branch may need registers for following instructions
9289 if(itype[i]!=RJUMP&&itype[i]!=UJUMP&&(source[i]>>16)!=0x1000)
9292 nr|=needed_reg[i+2];
9293 for(hr=0;hr<HOST_REGS;hr++)
9295 if(regmap_pre[i+2][hr]>=0&&get_reg(regs[i+2].regmap_entry,regmap_pre[i+2][hr])<0) nr&=~(1<<hr);
9296 //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]);
9300 // Don't need stuff which is overwritten
9301 if(regs[i].regmap[hr]!=regmap_pre[i][hr]) nr&=~(1<<hr);
9302 if(regs[i].regmap[hr]<0) nr&=~(1<<hr); //moved...
9303 // Merge in delay slot
9304 for(hr=0;hr<HOST_REGS;hr++)
9306 // Don't need stuff which is overwritten
9307 /* if(regs[i].regmap[hr]!=regmap_pre[i][hr]) nr&=~(1<<hr); //*SEB* Moved here
9308 if(regs[i].regmap[hr]<0) nr&=~(1<<hr);*/
9311 // These are overwritten unless the branch is "likely"
9312 // and the delay slot is nullified if not taken
9313 if(rt1[i+1]&&rt1[i+1]==(regs[i].regmap[hr]&63)) nr&=~(1<<hr);
9314 if(rt2[i+1]&&rt2[i+1]==(regs[i].regmap[hr]&63)) nr&=~(1<<hr);
9316 if(us1[i+1]==(regmap_pre[i][hr]&63)) nr|=1<<hr;
9317 if(us2[i+1]==(regmap_pre[i][hr]&63)) nr|=1<<hr;
9318 if(rs1[i+1]==regmap_pre[i][hr]) nr|=1<<hr;
9319 if(rs2[i+1]==regmap_pre[i][hr]) nr|=1<<hr;
9320 if(us1[i+1]==(regs[i].regmap_entry[hr]&63)) nr|=1<<hr;
9321 if(us2[i+1]==(regs[i].regmap_entry[hr]&63)) nr|=1<<hr;
9322 if(rs1[i+1]==regs[i].regmap_entry[hr]) nr|=1<<hr;
9323 if(rs2[i+1]==regs[i].regmap_entry[hr]) nr|=1<<hr;
9324 if(dep1[i+1]&&!((unneeded_reg_upper[i]>>dep1[i+1])&1)) {
9325 if(dep1[i+1]==(regmap_pre[i][hr]&63)) nr|=1<<hr;
9326 if(dep2[i+1]==(regmap_pre[i][hr]&63)) nr|=1<<hr;
9328 if(dep2[i+1]&&!((unneeded_reg_upper[i]>>dep2[i+1])&1)) {
9329 if(dep1[i+1]==(regs[i].regmap_entry[hr]&63)) nr|=1<<hr;
9330 if(dep2[i+1]==(regs[i].regmap_entry[hr]&63)) nr|=1<<hr;
9332 if(itype[i+1]==STORE || itype[i+1]==STORELR || (opcode[i+1]&0x3b)==0x39) {
9333 if(regmap_pre[i][hr]==INVCP) nr|=1<<hr;
9334 if(regs[i].regmap_entry[hr]==INVCP) nr|=1<<hr;
9338 else if(itype[i]==SYSCALL)
9340 // SYSCALL instruction (software interrupt)
9343 else if(itype[i]==COP0 && (source[i]&0x3f)==0x18)
9345 // ERET instruction (return from interrupt)
9351 for(hr=0;hr<HOST_REGS;hr++) {
9352 if(regmap_pre[i+1][hr]>=0&&get_reg(regs[i+1].regmap_entry,regmap_pre[i+1][hr])<0) nr&=~(1<<hr);
9353 if(regs[i].regmap[hr]!=regmap_pre[i+1][hr]) nr&=~(1<<hr);
9354 if(regs[i].regmap[hr]!=regmap_pre[i][hr]) nr&=~(1<<hr);
9355 if(regs[i].regmap[hr]<0) nr&=~(1<<hr);
9359 for(hr=0;hr<HOST_REGS;hr++)
9361 // Overwritten registers are not needed
9362 if(rt1[i]&&rt1[i]==(regs[i].regmap[hr]&63)) nr&=~(1<<hr);
9363 if(rt2[i]&&rt2[i]==(regs[i].regmap[hr]&63)) nr&=~(1<<hr);
9364 if(FTEMP==(regs[i].regmap[hr]&63)) nr&=~(1<<hr);
9365 // Source registers are needed
9366 if(us1[i]==(regmap_pre[i][hr]&63)) nr|=1<<hr;
9367 if(us2[i]==(regmap_pre[i][hr]&63)) nr|=1<<hr;
9368 if(rs1[i]==regmap_pre[i][hr]) nr|=1<<hr;
9369 if(rs2[i]==regmap_pre[i][hr]) nr|=1<<hr;
9370 if(us1[i]==(regs[i].regmap_entry[hr]&63)) nr|=1<<hr;
9371 if(us2[i]==(regs[i].regmap_entry[hr]&63)) nr|=1<<hr;
9372 if(rs1[i]==regs[i].regmap_entry[hr]) nr|=1<<hr;
9373 if(rs2[i]==regs[i].regmap_entry[hr]) nr|=1<<hr;
9374 if(dep1[i]&&!((unneeded_reg_upper[i]>>dep1[i])&1)) {
9375 if(dep1[i]==(regmap_pre[i][hr]&63)) nr|=1<<hr;
9376 if(dep1[i]==(regs[i].regmap_entry[hr]&63)) nr|=1<<hr;
9378 if(dep2[i]&&!((unneeded_reg_upper[i]>>dep2[i])&1)) {
9379 if(dep2[i]==(regmap_pre[i][hr]&63)) nr|=1<<hr;
9380 if(dep2[i]==(regs[i].regmap_entry[hr]&63)) nr|=1<<hr;
9382 if(itype[i]==STORE || itype[i]==STORELR || (opcode[i]&0x3b)==0x39) {
9383 if(regmap_pre[i][hr]==INVCP) nr|=1<<hr;
9384 if(regs[i].regmap_entry[hr]==INVCP) nr|=1<<hr;
9386 // Don't store a register immediately after writing it,
9387 // may prevent dual-issue.
9388 // But do so if this is a branch target, otherwise we
9389 // might have to load the register before the branch.
9390 if(i>0&&!bt[i]&&((regs[i].wasdirty>>hr)&1)) {
9391 if((regmap_pre[i][hr]>0&®map_pre[i][hr]<64&&!((unneeded_reg[i]>>regmap_pre[i][hr])&1)) ||
9392 (regmap_pre[i][hr]>64&&!((unneeded_reg_upper[i]>>(regmap_pre[i][hr]&63))&1)) ) {
9393 if(rt1[i-1]==(regmap_pre[i][hr]&63)) nr|=1<<hr;
9394 if(rt2[i-1]==(regmap_pre[i][hr]&63)) nr|=1<<hr;
9396 if((regs[i].regmap_entry[hr]>0&®s[i].regmap_entry[hr]<64&&!((unneeded_reg[i]>>regs[i].regmap_entry[hr])&1)) ||
9397 (regs[i].regmap_entry[hr]>64&&!((unneeded_reg_upper[i]>>(regs[i].regmap_entry[hr]&63))&1)) ) {
9398 if(rt1[i-1]==(regs[i].regmap_entry[hr]&63)) nr|=1<<hr;
9399 if(rt2[i-1]==(regs[i].regmap_entry[hr]&63)) nr|=1<<hr;
9403 // Cycle count is needed at branches. Assume it is needed at the target too.
9404 if(i==0||bt[i]||itype[i]==CJUMP||itype[i]==FJUMP||itype[i]==SPAN) {
9405 if(regmap_pre[i][HOST_CCREG]==CCREG) nr|=1<<HOST_CCREG;
9406 if(regs[i].regmap_entry[HOST_CCREG]==CCREG) nr|=1<<HOST_CCREG;
9411 // Deallocate unneeded registers
9412 for(hr=0;hr<HOST_REGS;hr++)
9415 if(regs[i].regmap_entry[hr]!=CCREG) regs[i].regmap_entry[hr]=-1;
9416 if((regs[i].regmap[hr]&63)!=rs1[i] && (regs[i].regmap[hr]&63)!=rs2[i] &&
9417 (regs[i].regmap[hr]&63)!=rt1[i] && (regs[i].regmap[hr]&63)!=rt2[i] &&
9418 (regs[i].regmap[hr]&63)!=PTEMP && (regs[i].regmap[hr]&63)!=CCREG)
9420 if(itype[i]!=RJUMP&&itype[i]!=UJUMP&&(source[i]>>16)!=0x1000)
9423 regs[i].regmap[hr]=-1;
9424 regs[i].isconst&=~(1<<hr);
9426 regmap_pre[i+2][hr]=-1;
9427 regs[i+2].wasconst&=~(1<<hr);
9432 if(itype[i]==RJUMP||itype[i]==UJUMP||itype[i]==CJUMP||itype[i]==SJUMP||itype[i]==FJUMP)
9434 int d1=0,d2=0,map=0,temp=0;
9435 if(get_reg(regs[i].regmap,rt1[i+1]|64)>=0||get_reg(branch_regs[i].regmap,rt1[i+1]|64)>=0)
9441 if(itype[i+1]==LOAD || itype[i+1]==LOADLR ||
9442 itype[i+1]==STORE || itype[i+1]==STORELR ||
9446 if(itype[i+1]==STORE || itype[i+1]==STORELR || (opcode[i+1]&0x3b)==0x39) {
9449 if(itype[i+1]==LOADLR || itype[i+1]==STORELR ||
9452 if((regs[i].regmap[hr]&63)!=rs1[i] && (regs[i].regmap[hr]&63)!=rs2[i] &&
9453 (regs[i].regmap[hr]&63)!=rt1[i] && (regs[i].regmap[hr]&63)!=rt2[i] &&
9454 (regs[i].regmap[hr]&63)!=rt1[i+1] && (regs[i].regmap[hr]&63)!=rt2[i+1] &&
9455 (regs[i].regmap[hr]^64)!=us1[i+1] && (regs[i].regmap[hr]^64)!=us2[i+1] &&
9456 (regs[i].regmap[hr]^64)!=d1 && (regs[i].regmap[hr]^64)!=d2 &&
9457 regs[i].regmap[hr]!=rs1[i+1] && regs[i].regmap[hr]!=rs2[i+1] &&
9458 (regs[i].regmap[hr]&63)!=temp && regs[i].regmap[hr]!=PTEMP &&
9459 regs[i].regmap[hr]!=RHASH && regs[i].regmap[hr]!=RHTBL &&
9460 regs[i].regmap[hr]!=RTEMP && regs[i].regmap[hr]!=CCREG &&
9461 regs[i].regmap[hr]!=map )
9463 regs[i].regmap[hr]=-1;
9464 regs[i].isconst&=~(1<<hr);
9465 if((branch_regs[i].regmap[hr]&63)!=rs1[i] && (branch_regs[i].regmap[hr]&63)!=rs2[i] &&
9466 (branch_regs[i].regmap[hr]&63)!=rt1[i] && (branch_regs[i].regmap[hr]&63)!=rt2[i] &&
9467 (branch_regs[i].regmap[hr]&63)!=rt1[i+1] && (branch_regs[i].regmap[hr]&63)!=rt2[i+1] &&
9468 (branch_regs[i].regmap[hr]^64)!=us1[i+1] && (branch_regs[i].regmap[hr]^64)!=us2[i+1] &&
9469 (branch_regs[i].regmap[hr]^64)!=d1 && (branch_regs[i].regmap[hr]^64)!=d2 &&
9470 branch_regs[i].regmap[hr]!=rs1[i+1] && branch_regs[i].regmap[hr]!=rs2[i+1] &&
9471 (branch_regs[i].regmap[hr]&63)!=temp && branch_regs[i].regmap[hr]!=PTEMP &&
9472 branch_regs[i].regmap[hr]!=RHASH && branch_regs[i].regmap[hr]!=RHTBL &&
9473 branch_regs[i].regmap[hr]!=RTEMP && branch_regs[i].regmap[hr]!=CCREG &&
9474 branch_regs[i].regmap[hr]!=map)
9476 branch_regs[i].regmap[hr]=-1;
9477 branch_regs[i].regmap_entry[hr]=-1;
9478 if(itype[i]!=RJUMP&&itype[i]!=UJUMP&&(source[i]>>16)!=0x1000)
9480 if(!likely[i]&&i<slen-2) {
9481 regmap_pre[i+2][hr]=-1;
9482 regs[i+2].wasconst&=~(1<<hr);
9493 int d1=0,d2=0,map=-1,temp=-1;
9494 if(get_reg(regs[i].regmap,rt1[i]|64)>=0)
9500 if(itype[i]==LOAD || itype[i]==LOADLR ||
9501 itype[i]==STORE || itype[i]==STORELR ||
9504 } else if(itype[i]==STORE || itype[i]==STORELR || (opcode[i]&0x3b)==0x39) {
9507 if(itype[i]==LOADLR || itype[i]==STORELR ||
9510 if((regs[i].regmap[hr]&63)!=rt1[i] && (regs[i].regmap[hr]&63)!=rt2[i] &&
9511 (regs[i].regmap[hr]^64)!=us1[i] && (regs[i].regmap[hr]^64)!=us2[i] &&
9512 (regs[i].regmap[hr]^64)!=d1 && (regs[i].regmap[hr]^64)!=d2 &&
9513 regs[i].regmap[hr]!=rs1[i] && regs[i].regmap[hr]!=rs2[i] &&
9514 (regs[i].regmap[hr]&63)!=temp && regs[i].regmap[hr]!=map &&
9515 (itype[i]!=SPAN||regs[i].regmap[hr]!=CCREG))
9517 if(i<slen-1&&!is_ds[i]) {
9518 if(regmap_pre[i+1][hr]!=-1 || regs[i].regmap[hr]!=-1)
9519 if(regmap_pre[i+1][hr]!=regs[i].regmap[hr])
9520 if(regs[i].regmap[hr]<64||!((regs[i].was32>>(regs[i].regmap[hr]&63))&1))
9522 DebugMessage(M64MSG_VERBOSE, "fail: %x (%d %d!=%d)",start+i*4,hr,regmap_pre[i+1][hr],regs[i].regmap[hr]);
9523 assert(regmap_pre[i+1][hr]==regs[i].regmap[hr]);
9525 regmap_pre[i+1][hr]=-1;
9526 if(regs[i+1].regmap_entry[hr]==CCREG) regs[i+1].regmap_entry[hr]=-1;
9527 regs[i+1].wasconst&=~(1<<hr);
9529 regs[i].regmap[hr]=-1;
9530 regs[i].isconst&=~(1<<hr);
9538 /* Pass 5 - Pre-allocate registers */
9540 // If a register is allocated during a loop, try to allocate it for the
9541 // entire loop, if possible. This avoids loading/storing registers
9542 // inside of the loop.
9544 signed char f_regmap[HOST_REGS];
9545 clear_all_regs(f_regmap);
9546 for(i=0;i<slen-1;i++)
9548 if(itype[i]==UJUMP||itype[i]==CJUMP||itype[i]==SJUMP||itype[i]==FJUMP)
9550 if(ba[i]>=start && ba[i]<(start+i*4))
9551 if(itype[i+1]==NOP||itype[i+1]==MOV||itype[i+1]==ALU
9552 ||itype[i+1]==SHIFTIMM||itype[i+1]==IMM16||itype[i+1]==LOAD
9553 ||itype[i+1]==STORE||itype[i+1]==STORELR||itype[i+1]==C1LS
9554 ||itype[i+1]==SHIFT||itype[i+1]==COP1||itype[i+1]==FLOAT
9555 ||itype[i+1]==FCOMP||itype[i+1]==FCONV)
9557 int t=(ba[i]-start)>>2;
9558 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
9559 if(t<2||(itype[t-2]!=UJUMP&&itype[t-2]!=RJUMP)||rt1[t-2]!=31) // call/ret assumes no registers allocated
9560 for(hr=0;hr<HOST_REGS;hr++)
9562 if(regs[i].regmap[hr]>64) {
9563 if(!((regs[i].dirty>>hr)&1))
9564 f_regmap[hr]=regs[i].regmap[hr];
9565 else f_regmap[hr]=-1;
9567 else if(regs[i].regmap[hr]>=0) {
9568 if(f_regmap[hr]!=regs[i].regmap[hr]) {
9569 // dealloc old register
9571 for(n=0;n<HOST_REGS;n++)
9573 if(f_regmap[n]==regs[i].regmap[hr]) {f_regmap[n]=-1;}
9575 // and alloc new one
9576 f_regmap[hr]=regs[i].regmap[hr];
9579 if(branch_regs[i].regmap[hr]>64) {
9580 if(!((branch_regs[i].dirty>>hr)&1))
9581 f_regmap[hr]=branch_regs[i].regmap[hr];
9582 else f_regmap[hr]=-1;
9584 else if(branch_regs[i].regmap[hr]>=0) {
9585 if(f_regmap[hr]!=branch_regs[i].regmap[hr]) {
9586 // dealloc old register
9588 for(n=0;n<HOST_REGS;n++)
9590 if(f_regmap[n]==branch_regs[i].regmap[hr]) {f_regmap[n]=-1;}
9592 // and alloc new one
9593 f_regmap[hr]=branch_regs[i].regmap[hr];
9597 if(count_free_regs(regs[i].regmap)<=minimum_free_regs[i+1])
9598 f_regmap[hr]=branch_regs[i].regmap[hr];
9600 if(count_free_regs(branch_regs[i].regmap)<=minimum_free_regs[i+1])
9601 f_regmap[hr]=branch_regs[i].regmap[hr];
9603 // Avoid dirty->clean transition
9604 #ifdef DESTRUCTIVE_WRITEBACK
9605 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;
9607 // This check is only strictly required in the DESTRUCTIVE_WRITEBACK
9608 // case above, however it's always a good idea. We can't hoist the
9609 // load if the register was already allocated, so there's no point
9610 // wasting time analyzing most of these cases. It only "succeeds"
9611 // when the mapping was different and the load can be replaced with
9612 // a mov, which is of negligible benefit. So such cases are
9614 if(f_regmap[hr]>0) {
9615 if(regs[t].regmap[hr]==f_regmap[hr]||(regs[t].regmap_entry[hr]<0&&get_reg(regmap_pre[t],f_regmap[hr])<0)) {
9619 //DebugMessage(M64MSG_VERBOSE, "Test %x -> %x, %x %d/%d",start+i*4,ba[i],start+j*4,hr,r);
9620 if(r<34&&((unneeded_reg[j]>>r)&1)) break;
9621 if(r>63&&((unneeded_reg_upper[j]>>(r&63))&1)) break;
9623 // NB This can exclude the case where the upper-half
9624 // register is lower numbered than the lower-half
9625 // register. Not sure if it's worth fixing...
9626 if(get_reg(regs[j].regmap,r&63)<0) break;
9627 if(get_reg(regs[j].regmap_entry,r&63)<0) break;
9628 if(regs[j].is32&(1LL<<(r&63))) break;
9630 if(regs[j].regmap[hr]==f_regmap[hr]&&(f_regmap[hr]&63)<TEMPREG) {
9631 //DebugMessage(M64MSG_VERBOSE, "Hit %x -> %x, %x %d/%d",start+i*4,ba[i],start+j*4,hr,r);
9633 if(regs[i].regmap[hr]==-1&&branch_regs[i].regmap[hr]==-1) {
9634 if(get_reg(regs[i+2].regmap,f_regmap[hr])>=0) break;
9636 if(get_reg(regs[i].regmap,r&63)<0) break;
9637 if(get_reg(branch_regs[i].regmap,r&63)<0) break;
9640 while(k>1&®s[k-1].regmap[hr]==-1) {
9641 if(count_free_regs(regs[k-1].regmap)<=minimum_free_regs[k-1]) {
9642 //DebugMessage(M64MSG_VERBOSE, "no free regs for store %x",start+(k-1)*4);
9645 if(get_reg(regs[k-1].regmap,f_regmap[hr])>=0) {
9646 //DebugMessage(M64MSG_VERBOSE, "no-match due to different register");
9649 if(itype[k-2]==UJUMP||itype[k-2]==RJUMP||itype[k-2]==CJUMP||itype[k-2]==SJUMP||itype[k-2]==FJUMP) {
9650 //DebugMessage(M64MSG_VERBOSE, "no-match due to branch");
9653 // call/ret fast path assumes no registers allocated
9654 if(k>2&&(itype[k-3]==UJUMP||itype[k-3]==RJUMP)&&rt1[k-3]==31) {
9658 // NB This can exclude the case where the upper-half
9659 // register is lower numbered than the lower-half
9660 // register. Not sure if it's worth fixing...
9661 if(get_reg(regs[k-1].regmap,r&63)<0) break;
9662 if(regs[k-1].is32&(1LL<<(r&63))) break;
9667 if((regs[k].is32&(1LL<<f_regmap[hr]))!=
9668 (regs[i+2].was32&(1LL<<f_regmap[hr]))) {
9669 //DebugMessage(M64MSG_VERBOSE, "bad match after branch");
9673 if(regs[k-1].regmap[hr]==f_regmap[hr]&®map_pre[k][hr]==f_regmap[hr]) {
9674 //DebugMessage(M64MSG_VERBOSE, "Extend r%d, %x ->",hr,start+k*4);
9676 regs[k].regmap_entry[hr]=f_regmap[hr];
9677 regs[k].regmap[hr]=f_regmap[hr];
9678 regmap_pre[k+1][hr]=f_regmap[hr];
9679 regs[k].wasdirty&=~(1<<hr);
9680 regs[k].dirty&=~(1<<hr);
9681 regs[k].wasdirty|=(1<<hr)®s[k-1].dirty;
9682 regs[k].dirty|=(1<<hr)®s[k].wasdirty;
9683 regs[k].wasconst&=~(1<<hr);
9684 regs[k].isconst&=~(1<<hr);
9689 //DebugMessage(M64MSG_VERBOSE, "Fail Extend r%d, %x ->",hr,start+k*4);
9692 assert(regs[i-1].regmap[hr]==f_regmap[hr]);
9693 if(regs[i-1].regmap[hr]==f_regmap[hr]&®map_pre[i][hr]==f_regmap[hr]) {
9694 //DebugMessage(M64MSG_VERBOSE, "OK fill %x (r%d)",start+i*4,hr);
9695 regs[i].regmap_entry[hr]=f_regmap[hr];
9696 regs[i].regmap[hr]=f_regmap[hr];
9697 regs[i].wasdirty&=~(1<<hr);
9698 regs[i].dirty&=~(1<<hr);
9699 regs[i].wasdirty|=(1<<hr)®s[i-1].dirty;
9700 regs[i].dirty|=(1<<hr)®s[i-1].dirty;
9701 regs[i].wasconst&=~(1<<hr);
9702 regs[i].isconst&=~(1<<hr);
9703 branch_regs[i].regmap_entry[hr]=f_regmap[hr];
9704 branch_regs[i].wasdirty&=~(1<<hr);
9705 branch_regs[i].wasdirty|=(1<<hr)®s[i].dirty;
9706 branch_regs[i].regmap[hr]=f_regmap[hr];
9707 branch_regs[i].dirty&=~(1<<hr);
9708 branch_regs[i].dirty|=(1<<hr)®s[i].dirty;
9709 branch_regs[i].wasconst&=~(1<<hr);
9710 branch_regs[i].isconst&=~(1<<hr);
9711 if(itype[i]!=RJUMP&&itype[i]!=UJUMP&&(source[i]>>16)!=0x1000) {
9712 regmap_pre[i+2][hr]=f_regmap[hr];
9713 regs[i+2].wasdirty&=~(1<<hr);
9714 regs[i+2].wasdirty|=(1<<hr)®s[i].dirty;
9715 assert((branch_regs[i].is32&(1LL<<f_regmap[hr]))==
9716 (regs[i+2].was32&(1LL<<f_regmap[hr])));
9721 // Alloc register clean at beginning of loop,
9722 // but may dirty it in pass 6
9723 regs[k].regmap_entry[hr]=f_regmap[hr];
9724 regs[k].regmap[hr]=f_regmap[hr];
9725 regs[k].dirty&=~(1<<hr);
9726 regs[k].wasconst&=~(1<<hr);
9727 regs[k].isconst&=~(1<<hr);
9728 if(itype[k]==UJUMP||itype[k]==RJUMP||itype[k]==CJUMP||itype[k]==SJUMP||itype[k]==FJUMP) {
9729 branch_regs[k].regmap_entry[hr]=f_regmap[hr];
9730 branch_regs[k].regmap[hr]=f_regmap[hr];
9731 branch_regs[k].dirty&=~(1<<hr);
9732 branch_regs[k].wasconst&=~(1<<hr);
9733 branch_regs[k].isconst&=~(1<<hr);
9734 if(itype[k]!=RJUMP&&itype[k]!=UJUMP&&(source[k]>>16)!=0x1000) {
9735 regmap_pre[k+2][hr]=f_regmap[hr];
9736 regs[k+2].wasdirty&=~(1<<hr);
9737 assert((branch_regs[k].is32&(1LL<<f_regmap[hr]))==
9738 (regs[k+2].was32&(1LL<<f_regmap[hr])));
9743 regmap_pre[k+1][hr]=f_regmap[hr];
9744 regs[k+1].wasdirty&=~(1<<hr);
9747 if(regs[j].regmap[hr]==f_regmap[hr])
9748 regs[j].regmap_entry[hr]=f_regmap[hr];
9752 if(regs[j].regmap[hr]>=0)
9754 if(get_reg(regs[j].regmap,f_regmap[hr])>=0) {
9755 //DebugMessage(M64MSG_VERBOSE, "no-match due to different register");
9758 if((regs[j+1].is32&(1LL<<f_regmap[hr]))!=(regs[j].is32&(1LL<<f_regmap[hr]))) {
9759 //DebugMessage(M64MSG_VERBOSE, "32/64 mismatch %x %d",start+j*4,hr);
9762 if(itype[j]==UJUMP||itype[j]==RJUMP||(source[j]>>16)==0x1000)
9764 // Stop on unconditional branch
9767 if(itype[j]==CJUMP||itype[j]==SJUMP||itype[j]==FJUMP)
9770 if(count_free_regs(regs[j].regmap)<=minimum_free_regs[j+1])
9773 if(count_free_regs(branch_regs[j].regmap)<=minimum_free_regs[j+1])
9776 if(get_reg(branch_regs[j].regmap,f_regmap[hr])>=0) {
9777 //DebugMessage(M64MSG_VERBOSE, "no-match due to different register (branch)");
9781 if(count_free_regs(regs[j].regmap)<=minimum_free_regs[j]) {
9782 //DebugMessage(M64MSG_VERBOSE, "No free regs for store %x",start+j*4);
9785 if(f_regmap[hr]>=64) {
9786 if(regs[j].is32&(1LL<<(f_regmap[hr]&63))) {
9791 if(get_reg(regs[j].regmap,f_regmap[hr]&63)<0) {
9802 // Non branch or undetermined branch target
9803 for(hr=0;hr<HOST_REGS;hr++)
9805 if(hr!=EXCLUDE_REG) {
9806 if(regs[i].regmap[hr]>64) {
9807 if(!((regs[i].dirty>>hr)&1))
9808 f_regmap[hr]=regs[i].regmap[hr];
9810 else if(regs[i].regmap[hr]>=0) {
9811 if(f_regmap[hr]!=regs[i].regmap[hr]) {
9812 // dealloc old register
9814 for(n=0;n<HOST_REGS;n++)
9816 if(f_regmap[n]==regs[i].regmap[hr]) {f_regmap[n]=-1;}
9818 // and alloc new one
9819 f_regmap[hr]=regs[i].regmap[hr];
9824 // Try to restore cycle count at branch targets
9826 for(j=i;j<slen-1;j++) {
9827 if(regs[j].regmap[HOST_CCREG]!=-1) break;
9828 if(count_free_regs(regs[j].regmap)<=minimum_free_regs[j]) {
9829 //DebugMessage(M64MSG_VERBOSE, "no free regs for store %x",start+j*4);
9833 if(regs[j].regmap[HOST_CCREG]==CCREG) {
9835 //DebugMessage(M64MSG_VERBOSE, "Extend CC, %x -> %x",start+k*4,start+j*4);
9837 regs[k].regmap_entry[HOST_CCREG]=CCREG;
9838 regs[k].regmap[HOST_CCREG]=CCREG;
9839 regmap_pre[k+1][HOST_CCREG]=CCREG;
9840 regs[k+1].wasdirty|=1<<HOST_CCREG;
9841 regs[k].dirty|=1<<HOST_CCREG;
9842 regs[k].wasconst&=~(1<<HOST_CCREG);
9843 regs[k].isconst&=~(1<<HOST_CCREG);
9846 regs[j].regmap_entry[HOST_CCREG]=CCREG;
9848 // Work backwards from the branch target
9849 if(j>i&&f_regmap[HOST_CCREG]==CCREG)
9851 //DebugMessage(M64MSG_VERBOSE, "Extend backwards");
9854 while(regs[k-1].regmap[HOST_CCREG]==-1) {
9855 if(count_free_regs(regs[k-1].regmap)<=minimum_free_regs[k-1]) {
9856 //DebugMessage(M64MSG_VERBOSE, "no free regs for store %x",start+(k-1)*4);
9861 if(regs[k-1].regmap[HOST_CCREG]==CCREG) {
9862 //DebugMessage(M64MSG_VERBOSE, "Extend CC, %x ->",start+k*4);
9864 regs[k].regmap_entry[HOST_CCREG]=CCREG;
9865 regs[k].regmap[HOST_CCREG]=CCREG;
9866 regmap_pre[k+1][HOST_CCREG]=CCREG;
9867 regs[k+1].wasdirty|=1<<HOST_CCREG;
9868 regs[k].dirty|=1<<HOST_CCREG;
9869 regs[k].wasconst&=~(1<<HOST_CCREG);
9870 regs[k].isconst&=~(1<<HOST_CCREG);
9875 //DebugMessage(M64MSG_VERBOSE, "Fail Extend CC, %x ->",start+k*4);
9879 if(itype[i]!=STORE&&itype[i]!=STORELR&&itype[i]!=C1LS&&itype[i]!=SHIFT&&
9880 itype[i]!=NOP&&itype[i]!=MOV&&itype[i]!=ALU&&itype[i]!=SHIFTIMM&&
9881 itype[i]!=IMM16&&itype[i]!=LOAD&&itype[i]!=COP1&&itype[i]!=FLOAT&&
9882 itype[i]!=FCONV&&itype[i]!=FCOMP)
9884 memcpy(f_regmap,regs[i].regmap,sizeof(f_regmap));
9889 // Cache memory offset or tlb map pointer if a register is available
9890 #ifndef HOST_IMM_ADDR32
9895 int earliest_available[HOST_REGS];
9896 int loop_start[HOST_REGS];
9897 int score[HOST_REGS];
9899 int reg=using_tlb?MMREG:ROREG;
9902 for(hr=0;hr<HOST_REGS;hr++) {
9903 score[hr]=0;earliest_available[hr]=0;
9904 loop_start[hr]=MAXBLOCK;
9906 for(i=0;i<slen-1;i++)
9908 // Can't do anything if no registers are available
9909 if(count_free_regs(regs[i].regmap)<=minimum_free_regs[i]) {
9910 for(hr=0;hr<HOST_REGS;hr++) {
9911 score[hr]=0;earliest_available[hr]=i+1;
9912 loop_start[hr]=MAXBLOCK;
9915 if(itype[i]==UJUMP||itype[i]==RJUMP||itype[i]==CJUMP||itype[i]==SJUMP||itype[i]==FJUMP) {
9917 if(count_free_regs(branch_regs[i].regmap)<=minimum_free_regs[i+1]) {
9918 for(hr=0;hr<HOST_REGS;hr++) {
9919 score[hr]=0;earliest_available[hr]=i+1;
9920 loop_start[hr]=MAXBLOCK;
9924 if(count_free_regs(regs[i].regmap)<=minimum_free_regs[i+1]) {
9925 for(hr=0;hr<HOST_REGS;hr++) {
9926 score[hr]=0;earliest_available[hr]=i+1;
9927 loop_start[hr]=MAXBLOCK;
9932 // Mark unavailable registers
9933 for(hr=0;hr<HOST_REGS;hr++) {
9934 if(regs[i].regmap[hr]>=0) {
9935 score[hr]=0;earliest_available[hr]=i+1;
9936 loop_start[hr]=MAXBLOCK;
9938 if(itype[i]==UJUMP||itype[i]==RJUMP||itype[i]==CJUMP||itype[i]==SJUMP||itype[i]==FJUMP) {
9939 if(branch_regs[i].regmap[hr]>=0) {
9940 score[hr]=0;earliest_available[hr]=i+2;
9941 loop_start[hr]=MAXBLOCK;
9945 // No register allocations after unconditional jumps
9946 if(itype[i]==UJUMP||itype[i]==RJUMP||(source[i]>>16)==0x1000)
9948 for(hr=0;hr<HOST_REGS;hr++) {
9949 score[hr]=0;earliest_available[hr]=i+2;
9950 loop_start[hr]=MAXBLOCK;
9952 i++; // Skip delay slot too
9953 //DebugMessage(M64MSG_VERBOSE, "skip delay slot: %x",start+i*4);
9957 if(itype[i]==LOAD||itype[i]==LOADLR||
9958 itype[i]==STORE||itype[i]==STORELR||itype[i]==C1LS) {
9959 for(hr=0;hr<HOST_REGS;hr++) {
9960 if(hr!=EXCLUDE_REG) {
9962 for(j=i;j<slen-1;j++) {
9963 if(regs[j].regmap[hr]>=0) break;
9964 if(itype[j]==UJUMP||itype[j]==RJUMP||itype[j]==CJUMP||itype[j]==SJUMP||itype[j]==FJUMP) {
9965 if(branch_regs[j].regmap[hr]>=0) break;
9967 if(count_free_regs(regs[j].regmap)<=minimum_free_regs[j+1]) break;
9969 if(count_free_regs(branch_regs[j].regmap)<=minimum_free_regs[j+1]) break;
9972 else if(count_free_regs(regs[j].regmap)<=minimum_free_regs[j]) break;
9973 if(itype[j]==UJUMP||itype[j]==RJUMP||itype[j]==CJUMP||itype[j]==SJUMP||itype[j]==FJUMP) {
9974 int t=(ba[j]-start)>>2;
9975 if(t<j&&t>=earliest_available[hr]) {
9976 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
9977 // Score a point for hoisting loop invariant
9978 if(t<loop_start[hr]) loop_start[hr]=t;
9979 //DebugMessage(M64MSG_VERBOSE, "set loop_start: i=%x j=%x (%x)",start+i*4,start+j*4,start+t*4);
9985 if(regs[t].regmap[hr]==reg) {
9986 // Score a point if the branch target matches this register
9991 if(itype[j+1]==LOAD||itype[j+1]==LOADLR||
9992 itype[j+1]==STORE||itype[j+1]==STORELR||itype[j+1]==C1LS) {
9997 if(itype[j]==UJUMP||itype[j]==RJUMP||(source[j]>>16)==0x1000)
9999 // Stop on unconditional branch
10003 if(itype[j]==LOAD||itype[j]==LOADLR||
10004 itype[j]==STORE||itype[j]==STORELR||itype[j]==C1LS) {
10011 // Find highest score and allocate that register
10013 for(hr=0;hr<HOST_REGS;hr++) {
10014 if(hr!=EXCLUDE_REG) {
10015 if(score[hr]>score[maxscore]) {
10017 //DebugMessage(M64MSG_VERBOSE, "highest score: %d %d (%x->%x)",score[hr],hr,start+i*4,start+end[hr]*4);
10021 if(score[maxscore]>1)
10023 if(i<loop_start[maxscore]) loop_start[maxscore]=i;
10024 for(j=loop_start[maxscore];j<slen&&j<=end[maxscore];j++) {
10025 //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]);}
10026 assert(regs[j].regmap[maxscore]<0);
10027 if(j>loop_start[maxscore]) regs[j].regmap_entry[maxscore]=reg;
10028 regs[j].regmap[maxscore]=reg;
10029 regs[j].dirty&=~(1<<maxscore);
10030 regs[j].wasconst&=~(1<<maxscore);
10031 regs[j].isconst&=~(1<<maxscore);
10032 if(itype[j]==UJUMP||itype[j]==RJUMP||itype[j]==CJUMP||itype[j]==SJUMP||itype[j]==FJUMP) {
10033 branch_regs[j].regmap[maxscore]=reg;
10034 branch_regs[j].wasdirty&=~(1<<maxscore);
10035 branch_regs[j].dirty&=~(1<<maxscore);
10036 branch_regs[j].wasconst&=~(1<<maxscore);
10037 branch_regs[j].isconst&=~(1<<maxscore);
10038 if(itype[j]!=RJUMP&&itype[j]!=UJUMP&&(source[j]>>16)!=0x1000) {
10039 regmap_pre[j+2][maxscore]=reg;
10040 regs[j+2].wasdirty&=~(1<<maxscore);
10042 // loop optimization (loop_preload)
10043 int t=(ba[j]-start)>>2;
10044 if(t==loop_start[maxscore]) {
10045 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
10046 regs[t].regmap_entry[maxscore]=reg;
10051 if(j<1||(itype[j-1]!=RJUMP&&itype[j-1]!=UJUMP&&itype[j-1]!=CJUMP&&itype[j-1]!=SJUMP&&itype[j-1]!=FJUMP)) {
10052 regmap_pre[j+1][maxscore]=reg;
10053 regs[j+1].wasdirty&=~(1<<maxscore);
10058 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
10059 for(hr=0;hr<HOST_REGS;hr++) {
10060 score[hr]=0;earliest_available[hr]=i+i;
10061 loop_start[hr]=MAXBLOCK;
10069 // This allocates registers (if possible) one instruction prior
10070 // to use, which can avoid a load-use penalty on certain CPUs.
10071 for(i=0;i<slen-1;i++)
10073 if(!i||(itype[i-1]!=UJUMP&&itype[i-1]!=CJUMP&&itype[i-1]!=SJUMP&&itype[i-1]!=RJUMP&&itype[i-1]!=FJUMP))
10077 if(itype[i]==ALU||itype[i]==MOV||itype[i]==LOAD||itype[i]==SHIFTIMM||itype[i]==IMM16||(itype[i]==COP1&&opcode2[i]<3))
10080 if((hr=get_reg(regs[i+1].regmap,rs1[i+1]))>=0)
10082 if(regs[i].regmap[hr]<0&®s[i+1].regmap_entry[hr]<0)
10084 regs[i].regmap[hr]=regs[i+1].regmap[hr];
10085 regmap_pre[i+1][hr]=regs[i+1].regmap[hr];
10086 regs[i+1].regmap_entry[hr]=regs[i+1].regmap[hr];
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);
10096 if((hr=get_reg(regs[i+1].regmap,rs2[i+1]))>=0)
10098 if(regs[i].regmap[hr]<0&®s[i+1].regmap_entry[hr]<0)
10100 regs[i].regmap[hr]=regs[i+1].regmap[hr];
10101 regmap_pre[i+1][hr]=regs[i+1].regmap[hr];
10102 regs[i+1].regmap_entry[hr]=regs[i+1].regmap[hr];
10103 regs[i].isconst&=~(1<<hr);
10104 regs[i].isconst|=regs[i+1].isconst&(1<<hr);
10105 constmap[i][hr]=constmap[i+1][hr];
10106 regs[i+1].wasdirty&=~(1<<hr);
10107 regs[i].dirty&=~(1<<hr);
10111 // Preload target address for load instruction (non-constant)
10112 if(itype[i+1]==LOAD&&rs1[i+1]&&get_reg(regs[i+1].regmap,rs1[i+1])<0) {
10113 if((hr=get_reg(regs[i+1].regmap,rt1[i+1]))>=0)
10115 if(regs[i].regmap[hr]<0&®s[i+1].regmap_entry[hr]<0)
10117 regs[i].regmap[hr]=rs1[i+1];
10118 regmap_pre[i+1][hr]=rs1[i+1];
10119 regs[i+1].regmap_entry[hr]=rs1[i+1];
10120 regs[i].isconst&=~(1<<hr);
10121 regs[i].isconst|=regs[i+1].isconst&(1<<hr);
10122 constmap[i][hr]=constmap[i+1][hr];
10123 regs[i+1].wasdirty&=~(1<<hr);
10124 regs[i].dirty&=~(1<<hr);
10128 // Load source into target register
10129 if(lt1[i+1]&&get_reg(regs[i+1].regmap,rs1[i+1])<0) {
10130 if((hr=get_reg(regs[i+1].regmap,rt1[i+1]))>=0)
10132 if(regs[i].regmap[hr]<0&®s[i+1].regmap_entry[hr]<0)
10134 regs[i].regmap[hr]=rs1[i+1];
10135 regmap_pre[i+1][hr]=rs1[i+1];
10136 regs[i+1].regmap_entry[hr]=rs1[i+1];
10137 regs[i].isconst&=~(1<<hr);
10138 regs[i].isconst|=regs[i+1].isconst&(1<<hr);
10139 constmap[i][hr]=constmap[i+1][hr];
10140 regs[i+1].wasdirty&=~(1<<hr);
10141 regs[i].dirty&=~(1<<hr);
10145 // Preload map address
10146 #ifndef HOST_IMM_ADDR32
10147 if(itype[i+1]==LOAD||itype[i+1]==LOADLR||itype[i+1]==STORE||itype[i+1]==STORELR||itype[i+1]==C1LS) {
10148 hr=get_reg(regs[i+1].regmap,TLREG);
10150 int sr=get_reg(regs[i+1].regmap,rs1[i+1]);
10151 if(sr>=0&&((regs[i+1].wasconst>>sr)&1)) {
10153 if(regs[i].regmap[hr]<0&®s[i+1].regmap_entry[hr]<0)
10155 regs[i].regmap[hr]=MGEN1+((i+1)&1);
10156 regmap_pre[i+1][hr]=MGEN1+((i+1)&1);
10157 regs[i+1].regmap_entry[hr]=MGEN1+((i+1)&1);
10158 regs[i].isconst&=~(1<<hr);
10159 regs[i].isconst|=regs[i+1].isconst&(1<<hr);
10160 constmap[i][hr]=constmap[i+1][hr];
10161 regs[i+1].wasdirty&=~(1<<hr);
10162 regs[i].dirty&=~(1<<hr);
10164 else if((nr=get_reg2(regs[i].regmap,regs[i+1].regmap,-1))>=0)
10166 // move it to another register
10167 regs[i+1].regmap[hr]=-1;
10168 regmap_pre[i+2][hr]=-1;
10169 regs[i+1].regmap[nr]=TLREG;
10170 regmap_pre[i+2][nr]=TLREG;
10171 regs[i].regmap[nr]=MGEN1+((i+1)&1);
10172 regmap_pre[i+1][nr]=MGEN1+((i+1)&1);
10173 regs[i+1].regmap_entry[nr]=MGEN1+((i+1)&1);
10174 regs[i].isconst&=~(1<<nr);
10175 regs[i+1].isconst&=~(1<<nr);
10176 regs[i].dirty&=~(1<<nr);
10177 regs[i+1].wasdirty&=~(1<<nr);
10178 regs[i+1].dirty&=~(1<<nr);
10179 regs[i+2].wasdirty&=~(1<<nr);
10185 // Address for store instruction (non-constant)
10186 if(itype[i+1]==STORE||itype[i+1]==STORELR||opcode[i+1]==0x39||opcode[i+1]==0x3D) { // SB/SH/SW/SD/SWC1/SDC1
10187 if(get_reg(regs[i+1].regmap,rs1[i+1])<0) {
10188 hr=get_reg2(regs[i].regmap,regs[i+1].regmap,-1);
10189 if(hr<0) hr=get_reg(regs[i+1].regmap,-1);
10190 else {regs[i+1].regmap[hr]=AGEN1+((i+1)&1);regs[i+1].isconst&=~(1<<hr);}
10192 if(regs[i].regmap[hr]<0&®s[i+1].regmap_entry[hr]<0)
10194 regs[i].regmap[hr]=rs1[i+1];
10195 regmap_pre[i+1][hr]=rs1[i+1];
10196 regs[i+1].regmap_entry[hr]=rs1[i+1];
10197 regs[i].isconst&=~(1<<hr);
10198 regs[i].isconst|=regs[i+1].isconst&(1<<hr);
10199 constmap[i][hr]=constmap[i+1][hr];
10200 regs[i+1].wasdirty&=~(1<<hr);
10201 regs[i].dirty&=~(1<<hr);
10205 if(itype[i+1]==LOADLR||opcode[i+1]==0x31||opcode[i+1]==0x35) { // LWC1/LDC1
10206 if(get_reg(regs[i+1].regmap,rs1[i+1])<0) {
10208 hr=get_reg(regs[i+1].regmap,FTEMP);
10210 if(regs[i].regmap[hr]<0&®s[i+1].regmap_entry[hr]<0)
10212 regs[i].regmap[hr]=rs1[i+1];
10213 regmap_pre[i+1][hr]=rs1[i+1];
10214 regs[i+1].regmap_entry[hr]=rs1[i+1];
10215 regs[i].isconst&=~(1<<hr);
10216 regs[i].isconst|=regs[i+1].isconst&(1<<hr);
10217 constmap[i][hr]=constmap[i+1][hr];
10218 regs[i+1].wasdirty&=~(1<<hr);
10219 regs[i].dirty&=~(1<<hr);
10221 else if((nr=get_reg2(regs[i].regmap,regs[i+1].regmap,-1))>=0)
10223 // move it to another register
10224 regs[i+1].regmap[hr]=-1;
10225 regmap_pre[i+2][hr]=-1;
10226 regs[i+1].regmap[nr]=FTEMP;
10227 regmap_pre[i+2][nr]=FTEMP;
10228 regs[i].regmap[nr]=rs1[i+1];
10229 regmap_pre[i+1][nr]=rs1[i+1];
10230 regs[i+1].regmap_entry[nr]=rs1[i+1];
10231 regs[i].isconst&=~(1<<nr);
10232 regs[i+1].isconst&=~(1<<nr);
10233 regs[i].dirty&=~(1<<nr);
10234 regs[i+1].wasdirty&=~(1<<nr);
10235 regs[i+1].dirty&=~(1<<nr);
10236 regs[i+2].wasdirty&=~(1<<nr);
10240 if(itype[i+1]==LOAD||itype[i+1]==LOADLR||itype[i+1]==STORE||itype[i+1]==STORELR/*||itype[i+1]==C1LS*/) {
10241 if(itype[i+1]==LOAD)
10242 hr=get_reg(regs[i+1].regmap,rt1[i+1]);
10243 if(itype[i+1]==LOADLR||opcode[i+1]==0x31||opcode[i+1]==0x35) // LWC1/LDC1
10244 hr=get_reg(regs[i+1].regmap,FTEMP);
10245 if(itype[i+1]==STORE||itype[i+1]==STORELR||opcode[i+1]==0x39||opcode[i+1]==0x3D) { // SWC1/SDC1
10246 hr=get_reg(regs[i+1].regmap,AGEN1+((i+1)&1));
10247 if(hr<0) hr=get_reg(regs[i+1].regmap,-1);
10249 if(hr>=0&®s[i].regmap[hr]<0) {
10250 int rs=get_reg(regs[i+1].regmap,rs1[i+1]);
10251 if(rs>=0&&((regs[i+1].wasconst>>rs)&1)) {
10252 regs[i].regmap[hr]=AGEN1+((i+1)&1);
10253 regmap_pre[i+1][hr]=AGEN1+((i+1)&1);
10254 regs[i+1].regmap_entry[hr]=AGEN1+((i+1)&1);
10255 regs[i].isconst&=~(1<<hr);
10256 regs[i+1].wasdirty&=~(1<<hr);
10257 regs[i].dirty&=~(1<<hr);
10266 /* Pass 6 - Optimize clean/dirty state */
10267 clean_registers(0,slen-1,1);
10269 /* Pass 7 - Identify 32-bit registers */
10275 for (i=slen-1;i>=0;i--)
10278 if(itype[i]==RJUMP||itype[i]==UJUMP||itype[i]==CJUMP||itype[i]==SJUMP||itype[i]==FJUMP)
10280 if(ba[i]<start || ba[i]>=(start+slen*4))
10282 // Branch out of this block, don't need anything
10288 // Need whatever matches the target
10289 // (and doesn't get overwritten by the delay slot instruction)
10291 int t=(ba[i]-start)>>2;
10292 if(ba[i]>start+i*4) {
10294 if(!(requires_32bit[t]&~regs[i].was32))
10295 r32|=requires_32bit[t]&(~(1LL<<rt1[i+1]))&(~(1LL<<rt2[i+1]));
10298 //if(!(regs[t].was32&~unneeded_reg_upper[t]&~regs[i].was32))
10299 // r32|=regs[t].was32&~unneeded_reg_upper[t]&(~(1LL<<rt1[i+1]))&(~(1LL<<rt2[i+1]));
10300 if(!(pr32[t]&~regs[i].was32))
10301 r32|=pr32[t]&(~(1LL<<rt1[i+1]))&(~(1LL<<rt2[i+1]));
10304 // Conditional branch may need registers for following instructions
10305 if(itype[i]!=RJUMP&&itype[i]!=UJUMP&&(source[i]>>16)!=0x1000)
10308 r32|=requires_32bit[i+2];
10309 r32&=regs[i].was32;
10310 // Mark this address as a branch target since it may be called
10311 // upon return from interrupt
10315 // Merge in delay slot
10317 // These are overwritten unless the branch is "likely"
10318 // and the delay slot is nullified if not taken
10319 r32&=~(1LL<<rt1[i+1]);
10320 r32&=~(1LL<<rt2[i+1]);
10322 // Assume these are needed (delay slot)
10325 if((regs[i].was32>>us1[i+1])&1) r32|=1LL<<us1[i+1];
10329 if((regs[i].was32>>us2[i+1])&1) r32|=1LL<<us2[i+1];
10331 if(dep1[i+1]&&!((unneeded_reg_upper[i]>>dep1[i+1])&1))
10333 if((regs[i].was32>>dep1[i+1])&1) r32|=1LL<<dep1[i+1];
10335 if(dep2[i+1]&&!((unneeded_reg_upper[i]>>dep2[i+1])&1))
10337 if((regs[i].was32>>dep2[i+1])&1) r32|=1LL<<dep2[i+1];
10340 else if(itype[i]==SYSCALL)
10342 // SYSCALL instruction (software interrupt)
10345 else if(itype[i]==COP0 && (source[i]&0x3f)==0x18)
10347 // ERET instruction (return from interrupt)
10351 r32&=~(1LL<<rt1[i]);
10352 r32&=~(1LL<<rt2[i]);
10355 if((regs[i].was32>>us1[i])&1) r32|=1LL<<us1[i];
10359 if((regs[i].was32>>us2[i])&1) r32|=1LL<<us2[i];
10361 if(dep1[i]&&!((unneeded_reg_upper[i]>>dep1[i])&1))
10363 if((regs[i].was32>>dep1[i])&1) r32|=1LL<<dep1[i];
10365 if(dep2[i]&&!((unneeded_reg_upper[i]>>dep2[i])&1))
10367 if((regs[i].was32>>dep2[i])&1) r32|=1LL<<dep2[i];
10369 requires_32bit[i]=r32;
10371 // Dirty registers which are 32-bit, require 32-bit input
10372 // as they will be written as 32-bit values
10373 for(hr=0;hr<HOST_REGS;hr++)
10375 if(regs[i].regmap_entry[hr]>0&®s[i].regmap_entry[hr]<64) {
10376 if((regs[i].was32>>regs[i].regmap_entry[hr])&(regs[i].wasdirty>>hr)&1) {
10377 if(!((unneeded_reg_upper[i]>>regs[i].regmap_entry[hr])&1))
10378 requires_32bit[i]|=1LL<<regs[i].regmap_entry[hr];
10382 //requires_32bit[i]=is32[i]&~unneeded_reg_upper[i]; // DEBUG
10385 if(itype[slen-1]==SPAN) {
10386 bt[slen-1]=1; // Mark as a branch target so instruction can restart after exception
10389 /* Debug/disassembly */
10390 // if((void*)assem_debug==(void*)printf)
10391 #if defined( ASSEM_DEBUG )
10392 for(i=0;i<slen;i++)
10394 DebugMessage(M64MSG_VERBOSE, "U:");
10396 for(r=1;r<=CCREG;r++) {
10397 if((unneeded_reg[i]>>r)&1) {
10398 if(r==HIREG) DebugMessage(M64MSG_VERBOSE, " HI");
10399 else if(r==LOREG) DebugMessage(M64MSG_VERBOSE, " LO");
10400 else DebugMessage(M64MSG_VERBOSE, " r%d",r);
10403 DebugMessage(M64MSG_VERBOSE, " UU:");
10404 for(r=1;r<=CCREG;r++) {
10405 if(((unneeded_reg_upper[i]&~unneeded_reg[i])>>r)&1) {
10406 if(r==HIREG) DebugMessage(M64MSG_VERBOSE, " HI");
10407 else if(r==LOREG) DebugMessage(M64MSG_VERBOSE, " LO");
10408 else DebugMessage(M64MSG_VERBOSE, " r%d",r);
10411 DebugMessage(M64MSG_VERBOSE, " 32:");
10412 for(r=0;r<=CCREG;r++) {
10413 //if(((is32[i]>>r)&(~unneeded_reg[i]>>r))&1) {
10414 if((regs[i].was32>>r)&1) {
10415 if(r==CCREG) DebugMessage(M64MSG_VERBOSE, " CC");
10416 else if(r==HIREG) DebugMessage(M64MSG_VERBOSE, " HI");
10417 else if(r==LOREG) DebugMessage(M64MSG_VERBOSE, " LO");
10418 else DebugMessage(M64MSG_VERBOSE, " r%d",r);
10421 #if NEW_DYNAREC == NEW_DYNAREC_X86
10422 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]);
10424 #if NEW_DYNAREC == NEW_DYNAREC_ARM
10425 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]);
10427 DebugMessage(M64MSG_VERBOSE, "needs: ");
10428 if(needed_reg[i]&1) DebugMessage(M64MSG_VERBOSE, "eax ");
10429 if((needed_reg[i]>>1)&1) DebugMessage(M64MSG_VERBOSE, "ecx ");
10430 if((needed_reg[i]>>2)&1) DebugMessage(M64MSG_VERBOSE, "edx ");
10431 if((needed_reg[i]>>3)&1) DebugMessage(M64MSG_VERBOSE, "ebx ");
10432 if((needed_reg[i]>>5)&1) DebugMessage(M64MSG_VERBOSE, "ebp ");
10433 if((needed_reg[i]>>6)&1) DebugMessage(M64MSG_VERBOSE, "esi ");
10434 if((needed_reg[i]>>7)&1) DebugMessage(M64MSG_VERBOSE, "edi ");
10435 DebugMessage(M64MSG_VERBOSE, "r:");
10436 for(r=0;r<=CCREG;r++) {
10437 //if(((requires_32bit[i]>>r)&(~unneeded_reg[i]>>r))&1) {
10438 if((requires_32bit[i]>>r)&1) {
10439 if(r==CCREG) DebugMessage(M64MSG_VERBOSE, " CC");
10440 else if(r==HIREG) DebugMessage(M64MSG_VERBOSE, " HI");
10441 else if(r==LOREG) DebugMessage(M64MSG_VERBOSE, " LO");
10442 else DebugMessage(M64MSG_VERBOSE, " r%d",r);
10445 /*DebugMessage(M64MSG_VERBOSE, "pr:");
10446 for(r=0;r<=CCREG;r++) {
10447 //if(((requires_32bit[i]>>r)&(~unneeded_reg[i]>>r))&1) {
10448 if((pr32[i]>>r)&1) {
10449 if(r==CCREG) DebugMessage(M64MSG_VERBOSE, " CC");
10450 else if(r==HIREG) DebugMessage(M64MSG_VERBOSE, " HI");
10451 else if(r==LOREG) DebugMessage(M64MSG_VERBOSE, " LO");
10452 else DebugMessage(M64MSG_VERBOSE, " r%d",r);
10455 if(pr32[i]!=requires_32bit[i]) DebugMessage(M64MSG_ERROR, " OOPS");*/
10456 #if NEW_DYNAREC == NEW_DYNAREC_X86
10457 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]);
10458 DebugMessage(M64MSG_VERBOSE, "dirty: ");
10459 if(regs[i].wasdirty&1) DebugMessage(M64MSG_VERBOSE, "eax ");
10460 if((regs[i].wasdirty>>1)&1) DebugMessage(M64MSG_VERBOSE, "ecx ");
10461 if((regs[i].wasdirty>>2)&1) DebugMessage(M64MSG_VERBOSE, "edx ");
10462 if((regs[i].wasdirty>>3)&1) DebugMessage(M64MSG_VERBOSE, "ebx ");
10463 if((regs[i].wasdirty>>5)&1) DebugMessage(M64MSG_VERBOSE, "ebp ");
10464 if((regs[i].wasdirty>>6)&1) DebugMessage(M64MSG_VERBOSE, "esi ");
10465 if((regs[i].wasdirty>>7)&1) DebugMessage(M64MSG_VERBOSE, "edi ");
10467 #if NEW_DYNAREC == NEW_DYNAREC_ARM
10468 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]);
10469 DebugMessage(M64MSG_VERBOSE, "dirty: ");
10470 if(regs[i].wasdirty&1) DebugMessage(M64MSG_VERBOSE, "r0 ");
10471 if((regs[i].wasdirty>>1)&1) DebugMessage(M64MSG_VERBOSE, "r1 ");
10472 if((regs[i].wasdirty>>2)&1) DebugMessage(M64MSG_VERBOSE, "r2 ");
10473 if((regs[i].wasdirty>>3)&1) DebugMessage(M64MSG_VERBOSE, "r3 ");
10474 if((regs[i].wasdirty>>4)&1) DebugMessage(M64MSG_VERBOSE, "r4 ");
10475 if((regs[i].wasdirty>>5)&1) DebugMessage(M64MSG_VERBOSE, "r5 ");
10476 if((regs[i].wasdirty>>6)&1) DebugMessage(M64MSG_VERBOSE, "r6 ");
10477 if((regs[i].wasdirty>>7)&1) DebugMessage(M64MSG_VERBOSE, "r7 ");
10478 if((regs[i].wasdirty>>8)&1) DebugMessage(M64MSG_VERBOSE, "r8 ");
10479 if((regs[i].wasdirty>>9)&1) DebugMessage(M64MSG_VERBOSE, "r9 ");
10480 if((regs[i].wasdirty>>10)&1) DebugMessage(M64MSG_VERBOSE, "r10 ");
10481 if((regs[i].wasdirty>>12)&1) DebugMessage(M64MSG_VERBOSE, "r12 ");
10483 disassemble_inst(i);
10484 //printf ("ccadj[%d] = %d",i,ccadj[i]);
10485 #if NEW_DYNAREC == NEW_DYNAREC_X86
10486 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]);
10487 if(regs[i].dirty&1) DebugMessage(M64MSG_VERBOSE, "eax ");
10488 if((regs[i].dirty>>1)&1) DebugMessage(M64MSG_VERBOSE, "ecx ");
10489 if((regs[i].dirty>>2)&1) DebugMessage(M64MSG_VERBOSE, "edx ");
10490 if((regs[i].dirty>>3)&1) DebugMessage(M64MSG_VERBOSE, "ebx ");
10491 if((regs[i].dirty>>5)&1) DebugMessage(M64MSG_VERBOSE, "ebp ");
10492 if((regs[i].dirty>>6)&1) DebugMessage(M64MSG_VERBOSE, "esi ");
10493 if((regs[i].dirty>>7)&1) DebugMessage(M64MSG_VERBOSE, "edi ");
10495 #if NEW_DYNAREC == NEW_DYNAREC_ARM
10496 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]);
10497 if(regs[i].dirty&1) DebugMessage(M64MSG_VERBOSE, "r0 ");
10498 if((regs[i].dirty>>1)&1) DebugMessage(M64MSG_VERBOSE, "r1 ");
10499 if((regs[i].dirty>>2)&1) DebugMessage(M64MSG_VERBOSE, "r2 ");
10500 if((regs[i].dirty>>3)&1) DebugMessage(M64MSG_VERBOSE, "r3 ");
10501 if((regs[i].dirty>>4)&1) DebugMessage(M64MSG_VERBOSE, "r4 ");
10502 if((regs[i].dirty>>5)&1) DebugMessage(M64MSG_VERBOSE, "r5 ");
10503 if((regs[i].dirty>>6)&1) DebugMessage(M64MSG_VERBOSE, "r6 ");
10504 if((regs[i].dirty>>7)&1) DebugMessage(M64MSG_VERBOSE, "r7 ");
10505 if((regs[i].dirty>>8)&1) DebugMessage(M64MSG_VERBOSE, "r8 ");
10506 if((regs[i].dirty>>9)&1) DebugMessage(M64MSG_VERBOSE, "r9 ");
10507 if((regs[i].dirty>>10)&1) DebugMessage(M64MSG_VERBOSE, "r10 ");
10508 if((regs[i].dirty>>12)&1) DebugMessage(M64MSG_VERBOSE, "r12 ");
10510 if(regs[i].isconst) {
10511 DebugMessage(M64MSG_VERBOSE, "constants: ");
10512 #if NEW_DYNAREC == NEW_DYNAREC_X86
10513 if(regs[i].isconst&1) DebugMessage(M64MSG_VERBOSE, "eax=%x ",(int)constmap[i][0]);
10514 if((regs[i].isconst>>1)&1) DebugMessage(M64MSG_VERBOSE, "ecx=%x ",(int)constmap[i][1]);
10515 if((regs[i].isconst>>2)&1) DebugMessage(M64MSG_VERBOSE, "edx=%x ",(int)constmap[i][2]);
10516 if((regs[i].isconst>>3)&1) DebugMessage(M64MSG_VERBOSE, "ebx=%x ",(int)constmap[i][3]);
10517 if((regs[i].isconst>>5)&1) DebugMessage(M64MSG_VERBOSE, "ebp=%x ",(int)constmap[i][5]);
10518 if((regs[i].isconst>>6)&1) DebugMessage(M64MSG_VERBOSE, "esi=%x ",(int)constmap[i][6]);
10519 if((regs[i].isconst>>7)&1) DebugMessage(M64MSG_VERBOSE, "edi=%x ",(int)constmap[i][7]);
10521 #if NEW_DYNAREC == NEW_DYNAREC_ARM
10522 if(regs[i].isconst&1) DebugMessage(M64MSG_VERBOSE, "r0=%x ",(int)constmap[i][0]);
10523 if((regs[i].isconst>>1)&1) DebugMessage(M64MSG_VERBOSE, "r1=%x ",(int)constmap[i][1]);
10524 if((regs[i].isconst>>2)&1) DebugMessage(M64MSG_VERBOSE, "r2=%x ",(int)constmap[i][2]);
10525 if((regs[i].isconst>>3)&1) DebugMessage(M64MSG_VERBOSE, "r3=%x ",(int)constmap[i][3]);
10526 if((regs[i].isconst>>4)&1) DebugMessage(M64MSG_VERBOSE, "r4=%x ",(int)constmap[i][4]);
10527 if((regs[i].isconst>>5)&1) DebugMessage(M64MSG_VERBOSE, "r5=%x ",(int)constmap[i][5]);
10528 if((regs[i].isconst>>6)&1) DebugMessage(M64MSG_VERBOSE, "r6=%x ",(int)constmap[i][6]);
10529 if((regs[i].isconst>>7)&1) DebugMessage(M64MSG_VERBOSE, "r7=%x ",(int)constmap[i][7]);
10530 if((regs[i].isconst>>8)&1) DebugMessage(M64MSG_VERBOSE, "r8=%x ",(int)constmap[i][8]);
10531 if((regs[i].isconst>>9)&1) DebugMessage(M64MSG_VERBOSE, "r9=%x ",(int)constmap[i][9]);
10532 if((regs[i].isconst>>10)&1) DebugMessage(M64MSG_VERBOSE, "r10=%x ",(int)constmap[i][10]);
10533 if((regs[i].isconst>>12)&1) DebugMessage(M64MSG_VERBOSE, "r12=%x ",(int)constmap[i][12]);
10536 DebugMessage(M64MSG_VERBOSE, " 32:");
10537 for(r=0;r<=CCREG;r++) {
10538 if((regs[i].is32>>r)&1) {
10539 if(r==CCREG) DebugMessage(M64MSG_VERBOSE, " CC");
10540 else if(r==HIREG) DebugMessage(M64MSG_VERBOSE, " HI");
10541 else if(r==LOREG) DebugMessage(M64MSG_VERBOSE, " LO");
10542 else DebugMessage(M64MSG_VERBOSE, " r%d",r);
10545 /*DebugMessage(M64MSG_VERBOSE, " p32:");
10546 for(r=0;r<=CCREG;r++) {
10547 if((p32[i]>>r)&1) {
10548 if(r==CCREG) DebugMessage(M64MSG_VERBOSE, " CC");
10549 else if(r==HIREG) DebugMessage(M64MSG_VERBOSE, " HI");
10550 else if(r==LOREG) DebugMessage(M64MSG_VERBOSE, " LO");
10551 else DebugMessage(M64MSG_VERBOSE, " r%d",r);
10554 if(p32[i]!=regs[i].is32) DebugMessage(M64MSG_VERBOSE, " NO MATCH");*/
10555 if(itype[i]==RJUMP||itype[i]==UJUMP||itype[i]==CJUMP||itype[i]==SJUMP||itype[i]==FJUMP) {
10556 #if NEW_DYNAREC == NEW_DYNAREC_X86
10557 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]);
10558 if(branch_regs[i].dirty&1) DebugMessage(M64MSG_VERBOSE, "eax ");
10559 if((branch_regs[i].dirty>>1)&1) DebugMessage(M64MSG_VERBOSE, "ecx ");
10560 if((branch_regs[i].dirty>>2)&1) DebugMessage(M64MSG_VERBOSE, "edx ");
10561 if((branch_regs[i].dirty>>3)&1) DebugMessage(M64MSG_VERBOSE, "ebx ");
10562 if((branch_regs[i].dirty>>5)&1) DebugMessage(M64MSG_VERBOSE, "ebp ");
10563 if((branch_regs[i].dirty>>6)&1) DebugMessage(M64MSG_VERBOSE, "esi ");
10564 if((branch_regs[i].dirty>>7)&1) DebugMessage(M64MSG_VERBOSE, "edi ");
10566 #if NEW_DYNAREC == NEW_DYNAREC_ARM
10567 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]);
10568 if(branch_regs[i].dirty&1) DebugMessage(M64MSG_VERBOSE, "r0 ");
10569 if((branch_regs[i].dirty>>1)&1) DebugMessage(M64MSG_VERBOSE, "r1 ");
10570 if((branch_regs[i].dirty>>2)&1) DebugMessage(M64MSG_VERBOSE, "r2 ");
10571 if((branch_regs[i].dirty>>3)&1) DebugMessage(M64MSG_VERBOSE, "r3 ");
10572 if((branch_regs[i].dirty>>4)&1) DebugMessage(M64MSG_VERBOSE, "r4 ");
10573 if((branch_regs[i].dirty>>5)&1) DebugMessage(M64MSG_VERBOSE, "r5 ");
10574 if((branch_regs[i].dirty>>6)&1) DebugMessage(M64MSG_VERBOSE, "r6 ");
10575 if((branch_regs[i].dirty>>7)&1) DebugMessage(M64MSG_VERBOSE, "r7 ");
10576 if((branch_regs[i].dirty>>8)&1) DebugMessage(M64MSG_VERBOSE, "r8 ");
10577 if((branch_regs[i].dirty>>9)&1) DebugMessage(M64MSG_VERBOSE, "r9 ");
10578 if((branch_regs[i].dirty>>10)&1) DebugMessage(M64MSG_VERBOSE, "r10 ");
10579 if((branch_regs[i].dirty>>12)&1) DebugMessage(M64MSG_VERBOSE, "r12 ");
10581 DebugMessage(M64MSG_VERBOSE, " 32:");
10582 for(r=0;r<=CCREG;r++) {
10583 if((branch_regs[i].is32>>r)&1) {
10584 if(r==CCREG) DebugMessage(M64MSG_VERBOSE, " CC");
10585 else if(r==HIREG) DebugMessage(M64MSG_VERBOSE, " HI");
10586 else if(r==LOREG) DebugMessage(M64MSG_VERBOSE, " LO");
10587 else DebugMessage(M64MSG_VERBOSE, " r%d",r);
10594 /* Pass 8 - Assembly */
10595 linkcount=0;stubcount=0;
10596 ds=0;is_delayslot=0;
10598 #ifndef DESTRUCTIVE_WRITEBACK
10599 uint64_t is32_pre=0;
10602 u_int beginning=(u_int)out;
10603 if((u_int)addr&1) {
10607 for(i=0;i<slen;i++)
10609 //if(ds) DebugMessage(M64MSG_VERBOSE, "ds: ");
10610 // if((void*)assem_debug==(void*)printf) disassemble_inst(i);
10611 #if defined( ASSEM_DEBUG )
10612 disassemble_inst(i);
10615 ds=0; // Skip delay slot
10616 if(bt[i]) assem_debug("OOPS - branch into delay slot");
10619 #ifndef DESTRUCTIVE_WRITEBACK
10620 if(i<2||(itype[i-2]!=UJUMP&&itype[i-2]!=RJUMP&&(source[i-2]>>16)!=0x1000))
10622 wb_sx(regmap_pre[i],regs[i].regmap_entry,regs[i].wasdirty,is32_pre,regs[i].was32,
10623 unneeded_reg[i],unneeded_reg_upper[i]);
10624 wb_valid(regmap_pre[i],regs[i].regmap_entry,dirty_pre,regs[i].wasdirty,is32_pre,
10625 unneeded_reg[i],unneeded_reg_upper[i]);
10627 is32_pre=regs[i].is32;
10628 dirty_pre=regs[i].dirty;
10631 if(i<2||(itype[i-2]!=UJUMP&&itype[i-2]!=RJUMP&&(source[i-2]>>16)!=0x1000))
10633 wb_invalidate(regmap_pre[i],regs[i].regmap_entry,regs[i].wasdirty,regs[i].was32,
10634 unneeded_reg[i],unneeded_reg_upper[i]);
10635 loop_preload(regmap_pre[i],regs[i].regmap_entry);
10637 // branch target entry point
10638 instr_addr[i]=(u_int)out;
10639 assem_debug("<->");
10641 if(regs[i].regmap_entry[HOST_CCREG]==CCREG&®s[i].regmap[HOST_CCREG]!=CCREG)
10642 wb_register(CCREG,regs[i].regmap_entry,regs[i].wasdirty,regs[i].was32);
10643 load_regs(regs[i].regmap_entry,regs[i].regmap,regs[i].was32,rs1[i],rs2[i]);
10644 address_generation(i,®s[i],regs[i].regmap_entry);
10645 load_consts(regmap_pre[i],regs[i].regmap,regs[i].was32,i);
10646 if(itype[i]==RJUMP||itype[i]==UJUMP||itype[i]==CJUMP||itype[i]==SJUMP||itype[i]==FJUMP)
10648 // Load the delay slot registers if necessary
10649 if(rs1[i+1]!=rs1[i]&&rs1[i+1]!=rs2[i])
10650 load_regs(regs[i].regmap_entry,regs[i].regmap,regs[i].was32,rs1[i+1],rs1[i+1]);
10651 if(rs2[i+1]!=rs1[i+1]&&rs2[i+1]!=rs1[i]&&rs2[i+1]!=rs2[i])
10652 load_regs(regs[i].regmap_entry,regs[i].regmap,regs[i].was32,rs2[i+1],rs2[i+1]);
10653 if(itype[i+1]==STORE||itype[i+1]==STORELR||(opcode[i+1]&0x3b)==0x39)
10654 load_regs(regs[i].regmap_entry,regs[i].regmap,regs[i].was32,INVCP,INVCP);
10658 // Preload registers for following instruction
10659 if(rs1[i+1]!=rs1[i]&&rs1[i+1]!=rs2[i])
10660 if(rs1[i+1]!=rt1[i]&&rs1[i+1]!=rt2[i])
10661 load_regs(regs[i].regmap_entry,regs[i].regmap,regs[i].was32,rs1[i+1],rs1[i+1]);
10662 if(rs2[i+1]!=rs1[i+1]&&rs2[i+1]!=rs1[i]&&rs2[i+1]!=rs2[i])
10663 if(rs2[i+1]!=rt1[i]&&rs2[i+1]!=rt2[i])
10664 load_regs(regs[i].regmap_entry,regs[i].regmap,regs[i].was32,rs2[i+1],rs2[i+1]);
10666 // TODO: if(is_ooo(i)) address_generation(i+1);
10667 if(itype[i]==CJUMP||itype[i]==FJUMP)
10668 load_regs(regs[i].regmap_entry,regs[i].regmap,regs[i].was32,CCREG,CCREG);
10669 if(itype[i]==LOAD||itype[i]==LOADLR||itype[i]==STORE||itype[i]==STORELR||itype[i]==C1LS)
10670 load_regs(regs[i].regmap_entry,regs[i].regmap,regs[i].was32,MMREG,ROREG);
10671 if(itype[i]==STORE||itype[i]==STORELR||(opcode[i]&0x3b)==0x39)
10672 load_regs(regs[i].regmap_entry,regs[i].regmap,regs[i].was32,INVCP,INVCP);
10673 if(bt[i]) cop1_usable=0;
10677 alu_assemble(i,®s[i]);break;
10679 imm16_assemble(i,®s[i]);break;
10681 shift_assemble(i,®s[i]);break;
10683 shiftimm_assemble(i,®s[i]);break;
10685 load_assemble(i,®s[i]);break;
10687 loadlr_assemble(i,®s[i]);break;
10689 store_assemble(i,®s[i]);break;
10691 storelr_assemble(i,®s[i]);break;
10693 cop0_assemble(i,®s[i]);break;
10695 cop1_assemble(i,®s[i]);break;
10697 c1ls_assemble(i,®s[i]);break;
10699 fconv_assemble(i,®s[i]);break;
10701 float_assemble(i,®s[i]);break;
10703 fcomp_assemble(i,®s[i]);break;
10705 multdiv_assemble(i,®s[i]);break;
10707 mov_assemble(i,®s[i]);break;
10709 syscall_assemble(i,®s[i]);break;
10711 ujump_assemble(i,®s[i]);ds=1;break;
10713 rjump_assemble(i,®s[i]);ds=1;break;
10715 cjump_assemble(i,®s[i]);ds=1;break;
10717 sjump_assemble(i,®s[i]);ds=1;break;
10719 fjump_assemble(i,®s[i]);ds=1;break;
10721 pagespan_assemble(i,®s[i]);break;
10723 if(itype[i]==UJUMP||itype[i]==RJUMP||(source[i]>>16)==0x1000)
10724 literal_pool(1024);
10726 literal_pool_jumpover(256);
10729 //assert(itype[i-2]==UJUMP||itype[i-2]==RJUMP||(source[i-2]>>16)==0x1000);
10730 // If the block did not end with an unconditional branch,
10731 // add a jump to the next instruction.
10733 if(itype[i-2]!=UJUMP&&itype[i-2]!=RJUMP&&(source[i-2]>>16)!=0x1000&&itype[i-1]!=SPAN) {
10734 assert(itype[i-1]!=UJUMP&&itype[i-1]!=CJUMP&&itype[i-1]!=SJUMP&&itype[i-1]!=RJUMP&&itype[i-1]!=FJUMP);
10736 if(itype[i-2]!=CJUMP&&itype[i-2]!=SJUMP&&itype[i-2]!=FJUMP) {
10737 store_regs_bt(regs[i-1].regmap,regs[i-1].is32,regs[i-1].dirty,start+i*4);
10738 if(regs[i-1].regmap[HOST_CCREG]!=CCREG)
10739 emit_loadreg(CCREG,HOST_CCREG);
10740 emit_addimm(HOST_CCREG,CLOCK_DIVIDER*(ccadj[i-1]+1),HOST_CCREG);
10742 else if(!likely[i-2])
10744 store_regs_bt(branch_regs[i-2].regmap,branch_regs[i-2].is32,branch_regs[i-2].dirty,start+i*4);
10745 assert(branch_regs[i-2].regmap[HOST_CCREG]==CCREG);
10749 store_regs_bt(regs[i-2].regmap,regs[i-2].is32,regs[i-2].dirty,start+i*4);
10750 assert(regs[i-2].regmap[HOST_CCREG]==CCREG);
10752 add_to_linker((int)out,start+i*4,0);
10759 assert(itype[i-1]!=UJUMP&&itype[i-1]!=CJUMP&&itype[i-1]!=SJUMP&&itype[i-1]!=RJUMP&&itype[i-1]!=FJUMP);
10760 store_regs_bt(regs[i-1].regmap,regs[i-1].is32,regs[i-1].dirty,start+i*4);
10761 if(regs[i-1].regmap[HOST_CCREG]!=CCREG)
10762 emit_loadreg(CCREG,HOST_CCREG);
10763 emit_addimm(HOST_CCREG,CLOCK_DIVIDER*(ccadj[i-1]+1),HOST_CCREG);
10764 add_to_linker((int)out,start+i*4,0);
10768 // TODO: delay slot stubs?
10770 for(i=0;i<stubcount;i++)
10772 switch(stubs[i][0])
10780 do_readstub(i);break;
10785 do_writestub(i);break;
10787 do_ccstub(i);break;
10789 do_invstub(i);break;
10791 do_cop1stub(i);break;
10793 do_unalignedwritestub(i);break;
10797 /* Pass 9 - Linker */
10798 for(i=0;i<linkcount;i++)
10800 assem_debug("%8x -> %8x",link_addr[i][0],link_addr[i][1]);
10802 if(!link_addr[i][2])
10805 void *addr=check_addr(link_addr[i][1]);
10806 emit_extjump(link_addr[i][0],link_addr[i][1]);
10808 set_jump_target(link_addr[i][0],(int)addr);
10809 add_link(link_addr[i][1],stub);
10811 else set_jump_target(link_addr[i][0],(int)stub);
10816 int target=(link_addr[i][1]-start)>>2;
10817 assert(target>=0&&target<slen);
10818 assert(instr_addr[target]);
10819 //#ifdef CORTEX_A8_BRANCH_PREDICTION_HACK
10820 //set_jump_target_fillslot(link_addr[i][0],instr_addr[target],link_addr[i][2]>>1);
10822 set_jump_target(link_addr[i][0],instr_addr[target]);
10826 // External Branch Targets (jump_in)
10827 if(copy+slen*4>(void *)shadow+sizeof(shadow)) copy=shadow;
10828 for(i=0;i<slen;i++)
10832 if(instr_addr[i]) // TODO - delay slots (=null)
10834 u_int vaddr=start+i*4;
10835 u_int page=(0x80000000^vaddr)>>12;
10837 if(page>262143&&tlb_LUT_r[vaddr>>12]) page=(tlb_LUT_r[page^0x80000]^0x80000000)>>12;
10838 if(page>2048) page=2048+(page&2047);
10839 if(vpage>262143&&tlb_LUT_r[vaddr>>12]) vpage&=2047; // jump_dirty uses a hash of the virtual address instead
10840 if(vpage>2048) vpage=2048+(vpage&2047);
10842 //if(!(is32[i]&(~unneeded_reg_upper[i])&~(1LL<<CCREG)))
10843 if(!requires_32bit[i])
10845 assem_debug("%8x (%d) <- %8x",instr_addr[i],i,start+i*4);
10846 assem_debug("jump_in: %x",start+i*4);
10847 ll_add(jump_dirty+vpage,vaddr,(void *)out);
10848 int entry_point=do_dirty_stub(i);
10849 ll_add(jump_in+page,vaddr,(void *)entry_point);
10850 // If there was an existing entry in the hash table,
10851 // replace it with the new address.
10852 // Don't add new entries. We'll insert the
10853 // ones that actually get used in check_addr().
10854 u_int *ht_bin=hash_table[((vaddr>>16)^vaddr)&0xFFFF];
10855 if(ht_bin[0]==vaddr) {
10856 ht_bin[1]=entry_point;
10858 if(ht_bin[2]==vaddr) {
10859 ht_bin[3]=entry_point;
10864 u_int r=requires_32bit[i]|!!(requires_32bit[i]>>32);
10865 assem_debug("%8x (%d) <- %8x",instr_addr[i],i,start+i*4);
10866 assem_debug("jump_in: %x (restricted - %x)",start+i*4,r);
10867 //int entry_point=(int)out;
10868 ////assem_debug("entry_point: %x",entry_point);
10869 //load_regs_entry(i);
10870 //if(entry_point==(int)out)
10871 // entry_point=instr_addr[i];
10873 // emit_jmp(instr_addr[i]);
10874 //ll_add_32(jump_in+page,vaddr,r,(void *)entry_point);
10875 ll_add_32(jump_dirty+vpage,vaddr,r,(void *)out);
10876 int entry_point=do_dirty_stub(i);
10877 ll_add_32(jump_in+page,vaddr,r,(void *)entry_point);
10882 // Write out the literal pool if necessary
10884 #ifdef CORTEX_A8_BRANCH_PREDICTION_HACK
10886 if(((u_int)out)&7) emit_addnop(13);
10888 assert((u_int)out-beginning<MAX_OUTPUT_BLOCK_SIZE);
10889 //DebugMessage(M64MSG_VERBOSE, "shadow buffer: %x-%x",(int)copy,(int)copy+slen*4);
10890 memcpy(copy,source,slen*4);
10893 #if NEW_DYNAREC == NEW_DYNAREC_ARM
10894 __clear_cache((void *)beginning,out);
10895 //cacheflush((void *)beginning,out,0);
10898 // If we're within 256K of the end of the buffer,
10899 // start over from the beginning. (Is 256K enough?)
10900 if(out > (u_char *)(base_addr+(1<<TARGET_SIZE_2)-MAX_OUTPUT_BLOCK_SIZE-JUMP_TABLE_SIZE))
10901 out=(u_char *)base_addr;
10903 // Trap writes to any of the pages we compiled
10904 for(i=start>>12;i<=(start+slen*4)>>12;i++) {
10906 memory_map[i]|=0x40000000;
10907 if((signed int)start>=(signed int)0xC0000000) {
10909 j=(((u_int)i<<12)+(memory_map[i]<<2)-(u_int)rdram+(u_int)0x80000000)>>12;
10911 memory_map[j]|=0x40000000;
10912 //DebugMessage(M64MSG_VERBOSE, "write protect physical page: %x (virtual %x)",j<<12,start);
10916 /* Pass 10 - Free memory by expiring oldest blocks */
10918 int end=((((intptr_t)out-(intptr_t)base_addr)>>(TARGET_SIZE_2-16))+16384)&65535;
10919 while(expirep!=end)
10921 int shift=TARGET_SIZE_2-3; // Divide into 8 blocks
10922 int base=(int)base_addr+((expirep>>13)<<shift); // Base address of this block
10923 inv_debug("EXP: Phase %d\n",expirep);
10924 switch((expirep>>11)&3)
10927 // Clear jump_in and jump_dirty
10928 ll_remove_matching_addrs(jump_in+(expirep&2047),base,shift);
10929 ll_remove_matching_addrs(jump_dirty+(expirep&2047),base,shift);
10930 ll_remove_matching_addrs(jump_in+2048+(expirep&2047),base,shift);
10931 ll_remove_matching_addrs(jump_dirty+2048+(expirep&2047),base,shift);
10935 ll_kill_pointers(jump_out[expirep&2047],base,shift);
10936 ll_kill_pointers(jump_out[(expirep&2047)+2048],base,shift);
10939 // Clear hash table
10940 for(i=0;i<32;i++) {
10941 u_int *ht_bin=hash_table[((expirep&2047)<<5)+i];
10942 if((ht_bin[3]>>shift)==(base>>shift) ||
10943 ((ht_bin[3]-MAX_OUTPUT_BLOCK_SIZE)>>shift)==(base>>shift)) {
10944 inv_debug("EXP: Remove hash %x -> %x\n",ht_bin[2],ht_bin[3]);
10945 ht_bin[2]=ht_bin[3]=-1;
10947 if((ht_bin[1]>>shift)==(base>>shift) ||
10948 ((ht_bin[1]-MAX_OUTPUT_BLOCK_SIZE)>>shift)==(base>>shift)) {
10949 inv_debug("EXP: Remove hash %x -> %x\n",ht_bin[0],ht_bin[1]);
10950 ht_bin[0]=ht_bin[2];
10951 ht_bin[1]=ht_bin[3];
10952 ht_bin[2]=ht_bin[3]=-1;
10958 #if NEW_DYNAREC == NEW_DYNAREC_ARM
10959 if((expirep&2047)==0)
10962 ll_remove_matching_addrs(jump_out+(expirep&2047),base,shift);
10963 ll_remove_matching_addrs(jump_out+2048+(expirep&2047),base,shift);
10966 expirep=(expirep+1)&65535;
10971 void TLBWI_new(void)
10974 /* Remove old entries */
10975 unsigned int old_start_even=tlb_e[Index&0x3F].start_even;
10976 unsigned int old_end_even=tlb_e[Index&0x3F].end_even;
10977 unsigned int old_start_odd=tlb_e[Index&0x3F].start_odd;
10978 unsigned int old_end_odd=tlb_e[Index&0x3F].end_odd;
10979 for (i=old_start_even>>12; i<=old_end_even>>12; i++)
10981 if(i<0x80000||i>0xBFFFF)
10983 invalidate_block(i);
10987 for (i=old_start_odd>>12; i<=old_end_odd>>12; i++)
10989 if(i<0x80000||i>0xBFFFF)
10991 invalidate_block(i);
10995 cached_interpreter_table.TLBWI();
10996 //DebugMessage(M64MSG_VERBOSE, "TLBWI: index=%d",Index);
10997 //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);
10998 //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);
10999 /* Combine tlb_LUT_r, tlb_LUT_w, and invalid_code into a single table
11000 for fast look up. */
11001 for (i=tlb_e[Index&0x3F].start_even>>12; i<=tlb_e[Index&0x3F].end_even>>12; i++)
11003 //DebugMessage(M64MSG_VERBOSE, "%x: r:%8x w:%8x",i,tlb_LUT_r[i],tlb_LUT_w[i]);
11004 if(i<0x80000||i>0xBFFFF)
11007 memory_map[i]=((tlb_LUT_r[i]&0xFFFFF000)-(i<<12)+(unsigned int)rdram-0x80000000)>>2;
11008 // FIXME: should make sure the physical page is invalid too
11009 if(!tlb_LUT_w[i]||!invalid_code[i]) {
11010 memory_map[i]|=0x40000000; // Write protect
11012 assert(tlb_LUT_r[i]==tlb_LUT_w[i]);
11014 if(!using_tlb) DebugMessage(M64MSG_VERBOSE, "Enabled TLB");
11015 // Tell the dynamic recompiler to generate tlb lookup code
11018 else memory_map[i]=-1;
11020 //DebugMessage(M64MSG_VERBOSE, "memory_map[%x]: %8x (+%8x)",i,memory_map[i],memory_map[i]<<2);
11022 for (i=tlb_e[Index&0x3F].start_odd>>12; i<=tlb_e[Index&0x3F].end_odd>>12; i++)
11024 //DebugMessage(M64MSG_VERBOSE, "%x: r:%8x w:%8x",i,tlb_LUT_r[i],tlb_LUT_w[i]);
11025 if(i<0x80000||i>0xBFFFF)
11028 memory_map[i]=((tlb_LUT_r[i]&0xFFFFF000)-(i<<12)+(unsigned int)rdram-0x80000000)>>2;
11029 // FIXME: should make sure the physical page is invalid too
11030 if(!tlb_LUT_w[i]||!invalid_code[i]) {
11031 memory_map[i]|=0x40000000; // Write protect
11033 assert(tlb_LUT_r[i]==tlb_LUT_w[i]);
11035 if(!using_tlb) DebugMessage(M64MSG_VERBOSE, "Enabled TLB");
11036 // Tell the dynamic recompiler to generate tlb lookup code
11039 else memory_map[i]=-1;
11041 //DebugMessage(M64MSG_VERBOSE, "memory_map[%x]: %8x (+%8x)",i,memory_map[i],memory_map[i]<<2);
11045 void TLBWR_new(void)
11048 Random = (Count/2 % (32 - Wired)) + Wired;
11049 /* Remove old entries */
11050 unsigned int old_start_even=tlb_e[Random&0x3F].start_even;
11051 unsigned int old_end_even=tlb_e[Random&0x3F].end_even;
11052 unsigned int old_start_odd=tlb_e[Random&0x3F].start_odd;
11053 unsigned int old_end_odd=tlb_e[Random&0x3F].end_odd;
11054 for (i=old_start_even>>12; i<=old_end_even>>12; i++)
11056 if(i<0x80000||i>0xBFFFF)
11058 invalidate_block(i);
11062 for (i=old_start_odd>>12; i<=old_end_odd>>12; i++)
11064 if(i<0x80000||i>0xBFFFF)
11066 invalidate_block(i);
11070 cached_interpreter_table.TLBWR();
11071 /* Combine tlb_LUT_r, tlb_LUT_w, and invalid_code into a single table
11072 for fast look up. */
11073 for (i=tlb_e[Random&0x3F].start_even>>12; i<=tlb_e[Random&0x3F].end_even>>12; i++)
11075 //DebugMessage(M64MSG_VERBOSE, "%x: r:%8x w:%8x",i,tlb_LUT_r[i],tlb_LUT_w[i]);
11076 if(i<0x80000||i>0xBFFFF)
11079 memory_map[i]=((tlb_LUT_r[i]&0xFFFFF000)-(i<<12)+(unsigned int)rdram-0x80000000)>>2;
11080 // FIXME: should make sure the physical page is invalid too
11081 if(!tlb_LUT_w[i]||!invalid_code[i]) {
11082 memory_map[i]|=0x40000000; // Write protect
11084 assert(tlb_LUT_r[i]==tlb_LUT_w[i]);
11086 if(!using_tlb) DebugMessage(M64MSG_VERBOSE, "Enabled TLB");
11087 // Tell the dynamic recompiler to generate tlb lookup code
11090 else memory_map[i]=-1;
11092 //DebugMessage(M64MSG_VERBOSE, "memory_map[%x]: %8x (+%8x)",i,memory_map[i],memory_map[i]<<2);
11094 for (i=tlb_e[Random&0x3F].start_odd>>12; i<=tlb_e[Random&0x3F].end_odd>>12; i++)
11096 //DebugMessage(M64MSG_VERBOSE, "%x: r:%8x w:%8x",i,tlb_LUT_r[i],tlb_LUT_w[i]);
11097 if(i<0x80000||i>0xBFFFF)
11100 memory_map[i]=((tlb_LUT_r[i]&0xFFFFF000)-(i<<12)+(unsigned int)rdram-0x80000000)>>2;
11101 // FIXME: should make sure the physical page is invalid too
11102 if(!tlb_LUT_w[i]||!invalid_code[i]) {
11103 memory_map[i]|=0x40000000; // Write protect
11105 assert(tlb_LUT_r[i]==tlb_LUT_w[i]);
11107 if(!using_tlb) DebugMessage(M64MSG_VERBOSE, "Enabled TLB");
11108 // Tell the dynamic recompiler to generate tlb lookup code
11111 else memory_map[i]=-1;
11113 //DebugMessage(M64MSG_VERBOSE, "memory_map[%x]: %8x (+%8x)",i,memory_map[i],memory_map[i]<<2);