1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 * Mupen64plus - new_dynarec.c *
3 * Copyright (C) 2009-2011 Ari64 *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
22 #include <stdint.h> //include for uint64_t
26 #include "emu_if.h" //emulator interface
29 //#define assem_debug printf
30 //#define inv_debug printf
31 #define assem_debug(...)
32 #define inv_debug(...)
35 #include "assem_x86.h"
38 #include "assem_x64.h"
41 #include "assem_arm.h"
45 #define MAX_OUTPUT_BLOCK_SIZE 262144
46 #define CLOCK_DIVIDER 2
50 signed char regmap_entry[HOST_REGS];
51 signed char regmap[HOST_REGS];
60 uint64_t constmap[HOST_REGS];
68 struct ll_entry *next;
74 char insn[MAXBLOCK][10];
75 u_char itype[MAXBLOCK];
76 u_char opcode[MAXBLOCK];
77 u_char opcode2[MAXBLOCK];
85 u_char dep1[MAXBLOCK];
86 u_char dep2[MAXBLOCK];
88 static uint64_t gte_rs[MAXBLOCK]; // gte: 32 data and 32 ctl regs
89 static uint64_t gte_rt[MAXBLOCK];
90 static uint64_t gte_unneeded[MAXBLOCK];
91 static int gte_reads_flags; // gte flag read encountered
92 static u_int smrv[32]; // speculated MIPS register values
93 static u_int smrv_strong; // mask or regs that are likely to have correct values
94 static u_int smrv_weak; // same, but somewhat less likely
95 static u_int smrv_strong_next; // same, but after current insn executes
96 static u_int smrv_weak_next;
99 char likely[MAXBLOCK];
100 char is_ds[MAXBLOCK];
102 uint64_t unneeded_reg[MAXBLOCK];
103 uint64_t unneeded_reg_upper[MAXBLOCK];
104 uint64_t branch_unneeded_reg[MAXBLOCK];
105 uint64_t branch_unneeded_reg_upper[MAXBLOCK];
106 uint64_t p32[MAXBLOCK];
107 uint64_t pr32[MAXBLOCK];
108 signed char regmap_pre[MAXBLOCK][HOST_REGS];
109 signed char regmap[MAXBLOCK][HOST_REGS];
110 signed char regmap_entry[MAXBLOCK][HOST_REGS];
111 uint64_t constmap[MAXBLOCK][HOST_REGS];
112 struct regstat regs[MAXBLOCK];
113 struct regstat branch_regs[MAXBLOCK];
114 signed char minimum_free_regs[MAXBLOCK];
115 u_int needed_reg[MAXBLOCK];
116 uint64_t requires_32bit[MAXBLOCK];
117 u_int wont_dirty[MAXBLOCK];
118 u_int will_dirty[MAXBLOCK];
121 u_int instr_addr[MAXBLOCK];
122 u_int link_addr[MAXBLOCK][3];
124 u_int stubs[MAXBLOCK*3][8];
126 u_int literals[1024][2];
131 struct ll_entry *jump_in[4096];
132 struct ll_entry *jump_out[4096];
133 struct ll_entry *jump_dirty[4096];
134 u_int hash_table[65536][4] __attribute__((aligned(16)));
135 char shadow[1048576] __attribute__((aligned(16)));
141 static const u_int using_tlb=0;
143 int new_dynarec_did_compile;
144 u_int stop_after_jal;
145 extern u_char restore_candidate[512];
146 extern int cycle_count;
148 /* registers that may be allocated */
150 #define HIREG 32 // hi
151 #define LOREG 33 // lo
152 #define FSREG 34 // FPU status (FCSR)
153 #define CSREG 35 // Coprocessor status
154 #define CCREG 36 // Cycle count
155 #define INVCP 37 // Pointer to invalid_code
156 #define MMREG 38 // Pointer to memory_map
157 #define ROREG 39 // ram offset (if rdram!=0x80000000)
159 #define FTEMP 40 // FPU temporary register
160 #define PTEMP 41 // Prefetch temporary register
161 #define TLREG 42 // TLB mapping offset
162 #define RHASH 43 // Return address hash
163 #define RHTBL 44 // Return address hash table address
164 #define RTEMP 45 // JR/JALR address register
166 #define AGEN1 46 // Address generation temporary register
167 #define AGEN2 47 // Address generation temporary register
168 #define MGEN1 48 // Maptable address generation temporary register
169 #define MGEN2 49 // Maptable address generation temporary register
170 #define BTREG 50 // Branch target temporary register
172 /* instruction types */
173 #define NOP 0 // No operation
174 #define LOAD 1 // Load
175 #define STORE 2 // Store
176 #define LOADLR 3 // Unaligned load
177 #define STORELR 4 // Unaligned store
178 #define MOV 5 // Move
179 #define ALU 6 // Arithmetic/logic
180 #define MULTDIV 7 // Multiply/divide
181 #define SHIFT 8 // Shift by register
182 #define SHIFTIMM 9// Shift by immediate
183 #define IMM16 10 // 16-bit immediate
184 #define RJUMP 11 // Unconditional jump to register
185 #define UJUMP 12 // Unconditional jump
186 #define CJUMP 13 // Conditional branch (BEQ/BNE/BGTZ/BLEZ)
187 #define SJUMP 14 // Conditional branch (regimm format)
188 #define COP0 15 // Coprocessor 0
189 #define COP1 16 // Coprocessor 1
190 #define C1LS 17 // Coprocessor 1 load/store
191 #define FJUMP 18 // Conditional branch (floating point)
192 #define FLOAT 19 // Floating point unit
193 #define FCONV 20 // Convert integer to float
194 #define FCOMP 21 // Floating point compare (sets FSREG)
195 #define SYSCALL 22// SYSCALL
196 #define OTHER 23 // Other
197 #define SPAN 24 // Branch/delay slot spans 2 pages
198 #define NI 25 // Not implemented
199 #define HLECALL 26// PCSX fake opcodes for HLE
200 #define COP2 27 // Coprocessor 2 move
201 #define C2LS 28 // Coprocessor 2 load/store
202 #define C2OP 29 // Coprocessor 2 operation
203 #define INTCALL 30// Call interpreter to handle rare corner cases
212 #define LOADBU_STUB 7
213 #define LOADHU_STUB 8
214 #define STOREB_STUB 9
215 #define STOREH_STUB 10
216 #define STOREW_STUB 11
217 #define STORED_STUB 12
218 #define STORELR_STUB 13
219 #define INVCODE_STUB 14
227 int new_recompile_block(int addr);
228 void *get_addr_ht(u_int vaddr);
229 void invalidate_block(u_int block);
230 void invalidate_addr(u_int addr);
231 void remove_hash(int vaddr);
234 void dyna_linker_ds();
236 void verify_code_vm();
237 void verify_code_ds();
240 void fp_exception_ds();
242 void jump_syscall_hle();
246 void new_dyna_leave();
251 void read_nomem_new();
252 void read_nomemb_new();
253 void read_nomemh_new();
254 void read_nomemd_new();
255 void write_nomem_new();
256 void write_nomemb_new();
257 void write_nomemh_new();
258 void write_nomemd_new();
259 void write_rdram_new();
260 void write_rdramb_new();
261 void write_rdramh_new();
262 void write_rdramd_new();
263 extern u_int memory_map[1048576];
265 // Needed by assembler
266 void wb_register(signed char r,signed char regmap[],uint64_t dirty,uint64_t is32);
267 void wb_dirtys(signed char i_regmap[],uint64_t i_is32,uint64_t i_dirty);
268 void wb_needed_dirtys(signed char i_regmap[],uint64_t i_is32,uint64_t i_dirty,int addr);
269 void load_all_regs(signed char i_regmap[]);
270 void load_needed_regs(signed char i_regmap[],signed char next_regmap[]);
271 void load_regs_entry(int t);
272 void load_all_consts(signed char regmap[],int is32,u_int dirty,int i);
276 //#define DEBUG_CYCLE_COUNT 1
278 static void tlb_hacks()
282 if (strncmp((char *) ROM_HEADER->nom, "GOLDENEYE",9) == 0)
286 switch (ROM_HEADER->Country_code&0xFF)
298 // Unknown country code
302 u_int rom_addr=(u_int)rom;
304 // Since memory_map is 32-bit, on 64-bit systems the rom needs to be
305 // in the lower 4G of memory to use this hack. Copy it if necessary.
306 if((void *)rom>(void *)0xffffffff) {
307 munmap(ROM_COPY, 67108864);
308 if(mmap(ROM_COPY, 12582912,
309 PROT_READ | PROT_WRITE,
310 MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS,
311 -1, 0) <= 0) {printf("mmap() failed\n");}
312 memcpy(ROM_COPY,rom,12582912);
313 rom_addr=(u_int)ROM_COPY;
317 for(n=0x7F000;n<0x80000;n++) {
318 memory_map[n]=(((u_int)(rom_addr+addr-0x7F000000))>>2)|0x40000000;
325 static u_int get_page(u_int vaddr)
328 u_int page=(vaddr^0x80000000)>>12;
330 u_int page=vaddr&~0xe0000000;
331 if (page < 0x1000000)
332 page &= ~0x0e00000; // RAM mirrors
336 if(page>262143&&tlb_LUT_r[vaddr>>12]) page=(tlb_LUT_r[vaddr>>12]^0x80000000)>>12;
338 if(page>2048) page=2048+(page&2047);
342 static u_int get_vpage(u_int vaddr)
344 u_int vpage=(vaddr^0x80000000)>>12;
346 if(vpage>262143&&tlb_LUT_r[vaddr>>12]) vpage&=2047; // jump_dirty uses a hash of the virtual address instead
348 if(vpage>2048) vpage=2048+(vpage&2047);
352 // Get address from virtual address
353 // This is called from the recompiled JR/JALR instructions
354 void *get_addr(u_int vaddr)
356 u_int page=get_page(vaddr);
357 u_int vpage=get_vpage(vaddr);
358 struct ll_entry *head;
359 //printf("TRACE: count=%d next=%d (get_addr %x,page %d)\n",Count,next_interupt,vaddr,page);
362 if(head->vaddr==vaddr&&head->reg32==0) {
363 //printf("TRACE: count=%d next=%d (get_addr match %x: %x)\n",Count,next_interupt,vaddr,(int)head->addr);
364 int *ht_bin=hash_table[((vaddr>>16)^vaddr)&0xFFFF];
367 ht_bin[1]=(int)head->addr;
373 head=jump_dirty[vpage];
375 if(head->vaddr==vaddr&&head->reg32==0) {
376 //printf("TRACE: count=%d next=%d (get_addr match dirty %x: %x)\n",Count,next_interupt,vaddr,(int)head->addr);
377 // Don't restore blocks which are about to expire from the cache
378 if((((u_int)head->addr-(u_int)out)<<(32-TARGET_SIZE_2))>0x60000000+(MAX_OUTPUT_BLOCK_SIZE<<(32-TARGET_SIZE_2)))
379 if(verify_dirty(head->addr)) {
380 //printf("restore candidate: %x (%d) d=%d\n",vaddr,page,invalid_code[vaddr>>12]);
381 invalid_code[vaddr>>12]=0;
382 inv_code_start=inv_code_end=~0;
384 memory_map[vaddr>>12]|=0x40000000;
388 if(tlb_LUT_r[vaddr>>12]) {
389 invalid_code[tlb_LUT_r[vaddr>>12]>>12]=0;
390 memory_map[tlb_LUT_r[vaddr>>12]>>12]|=0x40000000;
393 restore_candidate[vpage>>3]|=1<<(vpage&7);
395 else restore_candidate[page>>3]|=1<<(page&7);
396 int *ht_bin=hash_table[((vaddr>>16)^vaddr)&0xFFFF];
397 if(ht_bin[0]==vaddr) {
398 ht_bin[1]=(int)head->addr; // Replace existing entry
404 ht_bin[1]=(int)head->addr;
412 //printf("TRACE: count=%d next=%d (get_addr no-match %x)\n",Count,next_interupt,vaddr);
413 int r=new_recompile_block(vaddr);
414 if(r==0) return get_addr(vaddr);
415 // Execute in unmapped page, generate pagefault execption
417 Cause=(vaddr<<31)|0x8;
418 EPC=(vaddr&1)?vaddr-5:vaddr;
420 Context=(Context&0xFF80000F)|((BadVAddr>>9)&0x007FFFF0);
421 EntryHi=BadVAddr&0xFFFFE000;
422 return get_addr_ht(0x80000000);
424 // Look up address in hash table first
425 void *get_addr_ht(u_int vaddr)
427 //printf("TRACE: count=%d next=%d (get_addr_ht %x)\n",Count,next_interupt,vaddr);
428 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 return get_addr(vaddr);
434 void *get_addr_32(u_int vaddr,u_int flags)
437 return get_addr(vaddr);
439 //printf("TRACE: count=%d next=%d (get_addr_32 %x,flags %x)\n",Count,next_interupt,vaddr,flags);
440 int *ht_bin=hash_table[((vaddr>>16)^vaddr)&0xFFFF];
441 if(ht_bin[0]==vaddr) return (void *)ht_bin[1];
442 if(ht_bin[2]==vaddr) return (void *)ht_bin[3];
443 u_int page=get_page(vaddr);
444 u_int vpage=get_vpage(vaddr);
445 struct ll_entry *head;
448 if(head->vaddr==vaddr&&(head->reg32&flags)==0) {
449 //printf("TRACE: count=%d next=%d (get_addr_32 match %x: %x)\n",Count,next_interupt,vaddr,(int)head->addr);
451 int *ht_bin=hash_table[((vaddr>>16)^vaddr)&0xFFFF];
453 ht_bin[1]=(int)head->addr;
455 }else if(ht_bin[2]==-1) {
456 ht_bin[3]=(int)head->addr;
459 //ht_bin[3]=ht_bin[1];
460 //ht_bin[2]=ht_bin[0];
461 //ht_bin[1]=(int)head->addr;
468 head=jump_dirty[vpage];
470 if(head->vaddr==vaddr&&(head->reg32&flags)==0) {
471 //printf("TRACE: count=%d next=%d (get_addr_32 match dirty %x: %x)\n",Count,next_interupt,vaddr,(int)head->addr);
472 // Don't restore blocks which are about to expire from the cache
473 if((((u_int)head->addr-(u_int)out)<<(32-TARGET_SIZE_2))>0x60000000+(MAX_OUTPUT_BLOCK_SIZE<<(32-TARGET_SIZE_2)))
474 if(verify_dirty(head->addr)) {
475 //printf("restore candidate: %x (%d) d=%d\n",vaddr,page,invalid_code[vaddr>>12]);
476 invalid_code[vaddr>>12]=0;
477 inv_code_start=inv_code_end=~0;
478 memory_map[vaddr>>12]|=0x40000000;
481 if(tlb_LUT_r[vaddr>>12]) {
482 invalid_code[tlb_LUT_r[vaddr>>12]>>12]=0;
483 memory_map[tlb_LUT_r[vaddr>>12]>>12]|=0x40000000;
486 restore_candidate[vpage>>3]|=1<<(vpage&7);
488 else restore_candidate[page>>3]|=1<<(page&7);
490 int *ht_bin=hash_table[((vaddr>>16)^vaddr)&0xFFFF];
492 ht_bin[1]=(int)head->addr;
494 }else if(ht_bin[2]==-1) {
495 ht_bin[3]=(int)head->addr;
498 //ht_bin[3]=ht_bin[1];
499 //ht_bin[2]=ht_bin[0];
500 //ht_bin[1]=(int)head->addr;
508 //printf("TRACE: count=%d next=%d (get_addr_32 no-match %x,flags %x)\n",Count,next_interupt,vaddr,flags);
509 int r=new_recompile_block(vaddr);
510 if(r==0) return get_addr(vaddr);
511 // Execute in unmapped page, generate pagefault execption
513 Cause=(vaddr<<31)|0x8;
514 EPC=(vaddr&1)?vaddr-5:vaddr;
516 Context=(Context&0xFF80000F)|((BadVAddr>>9)&0x007FFFF0);
517 EntryHi=BadVAddr&0xFFFFE000;
518 return get_addr_ht(0x80000000);
522 void clear_all_regs(signed char regmap[])
525 for (hr=0;hr<HOST_REGS;hr++) regmap[hr]=-1;
528 signed char get_reg(signed char regmap[],int r)
531 for (hr=0;hr<HOST_REGS;hr++) if(hr!=EXCLUDE_REG&®map[hr]==r) return hr;
535 // Find a register that is available for two consecutive cycles
536 signed char get_reg2(signed char regmap1[],signed char regmap2[],int r)
539 for (hr=0;hr<HOST_REGS;hr++) if(hr!=EXCLUDE_REG&®map1[hr]==r&®map2[hr]==r) return hr;
543 int count_free_regs(signed char regmap[])
547 for(hr=0;hr<HOST_REGS;hr++)
549 if(hr!=EXCLUDE_REG) {
550 if(regmap[hr]<0) count++;
556 void dirty_reg(struct regstat *cur,signed char reg)
560 for (hr=0;hr<HOST_REGS;hr++) {
561 if((cur->regmap[hr]&63)==reg) {
567 // If we dirty the lower half of a 64 bit register which is now being
568 // sign-extended, we need to dump the upper half.
569 // Note: Do this only after completion of the instruction, because
570 // some instructions may need to read the full 64-bit value even if
571 // overwriting it (eg SLTI, DSRA32).
572 static void flush_dirty_uppers(struct regstat *cur)
575 for (hr=0;hr<HOST_REGS;hr++) {
576 if((cur->dirty>>hr)&1) {
579 if((cur->is32>>(reg&63))&1) cur->regmap[hr]=-1;
584 void set_const(struct regstat *cur,signed char reg,uint64_t value)
588 for (hr=0;hr<HOST_REGS;hr++) {
589 if(cur->regmap[hr]==reg) {
591 cur->constmap[hr]=value;
593 else if((cur->regmap[hr]^64)==reg) {
595 cur->constmap[hr]=value>>32;
600 void clear_const(struct regstat *cur,signed char reg)
604 for (hr=0;hr<HOST_REGS;hr++) {
605 if((cur->regmap[hr]&63)==reg) {
606 cur->isconst&=~(1<<hr);
611 int is_const(struct regstat *cur,signed char reg)
616 for (hr=0;hr<HOST_REGS;hr++) {
617 if((cur->regmap[hr]&63)==reg) {
618 return (cur->isconst>>hr)&1;
623 uint64_t get_const(struct regstat *cur,signed char reg)
627 for (hr=0;hr<HOST_REGS;hr++) {
628 if(cur->regmap[hr]==reg) {
629 return cur->constmap[hr];
632 printf("Unknown constant in r%d\n",reg);
636 // Least soon needed registers
637 // Look at the next ten instructions and see which registers
638 // will be used. Try not to reallocate these.
639 void lsn(u_char hsn[], int i, int *preferred_reg)
649 if(itype[i+j]==UJUMP||itype[i+j]==RJUMP||(source[i+j]>>16)==0x1000)
651 // Don't go past an unconditonal jump
658 if(rs1[i+j]) hsn[rs1[i+j]]=j;
659 if(rs2[i+j]) hsn[rs2[i+j]]=j;
660 if(rt1[i+j]) hsn[rt1[i+j]]=j;
661 if(rt2[i+j]) hsn[rt2[i+j]]=j;
662 if(itype[i+j]==STORE || itype[i+j]==STORELR) {
663 // Stores can allocate zero
667 // On some architectures stores need invc_ptr
668 #if defined(HOST_IMM8)
669 if(itype[i+j]==STORE || itype[i+j]==STORELR || (opcode[i+j]&0x3b)==0x39 || (opcode[i+j]&0x3b)==0x3a) {
673 if(i+j>=0&&(itype[i+j]==UJUMP||itype[i+j]==CJUMP||itype[i+j]==SJUMP||itype[i+j]==FJUMP))
681 if(ba[i+b]>=start && ba[i+b]<(start+slen*4))
683 // Follow first branch
684 int t=(ba[i+b]-start)>>2;
685 j=7-b;if(t+j>=slen) j=slen-t-1;
688 if(rs1[t+j]) if(hsn[rs1[t+j]]>j+b+2) hsn[rs1[t+j]]=j+b+2;
689 if(rs2[t+j]) if(hsn[rs2[t+j]]>j+b+2) hsn[rs2[t+j]]=j+b+2;
690 //if(rt1[t+j]) if(hsn[rt1[t+j]]>j+b+2) hsn[rt1[t+j]]=j+b+2;
691 //if(rt2[t+j]) if(hsn[rt2[t+j]]>j+b+2) hsn[rt2[t+j]]=j+b+2;
694 // TODO: preferred register based on backward branch
696 // Delay slot should preferably not overwrite branch conditions or cycle count
697 if(i>0&&(itype[i-1]==RJUMP||itype[i-1]==UJUMP||itype[i-1]==CJUMP||itype[i-1]==SJUMP||itype[i-1]==FJUMP)) {
698 if(rs1[i-1]) if(hsn[rs1[i-1]]>1) hsn[rs1[i-1]]=1;
699 if(rs2[i-1]) if(hsn[rs2[i-1]]>1) hsn[rs2[i-1]]=1;
705 // Coprocessor load/store needs FTEMP, even if not declared
706 if(itype[i]==C1LS||itype[i]==C2LS) {
709 // Load L/R also uses FTEMP as a temporary register
710 if(itype[i]==LOADLR) {
713 // Also SWL/SWR/SDL/SDR
714 if(opcode[i]==0x2a||opcode[i]==0x2e||opcode[i]==0x2c||opcode[i]==0x2d) {
717 // Don't remove the TLB registers either
718 if(itype[i]==LOAD || itype[i]==LOADLR || itype[i]==STORE || itype[i]==STORELR || itype[i]==C1LS || itype[i]==C2LS) {
721 // Don't remove the miniht registers
722 if(itype[i]==UJUMP||itype[i]==RJUMP)
729 // We only want to allocate registers if we're going to use them again soon
730 int needed_again(int r, int i)
736 if(i>0&&(itype[i-1]==UJUMP||itype[i-1]==RJUMP||(source[i-1]>>16)==0x1000))
738 if(ba[i-1]<start || ba[i-1]>start+slen*4-4)
739 return 0; // Don't need any registers if exiting the block
747 if(itype[i+j]==UJUMP||itype[i+j]==RJUMP||(source[i+j]>>16)==0x1000)
749 // Don't go past an unconditonal jump
753 if(itype[i+j]==SYSCALL||itype[i+j]==HLECALL||itype[i+j]==INTCALL||((source[i+j]&0xfc00003f)==0x0d))
760 if(rs1[i+j]==r) rn=j;
761 if(rs2[i+j]==r) rn=j;
762 if((unneeded_reg[i+j]>>r)&1) rn=10;
763 if(i+j>=0&&(itype[i+j]==UJUMP||itype[i+j]==CJUMP||itype[i+j]==SJUMP||itype[i+j]==FJUMP))
771 if(ba[i+b]>=start && ba[i+b]<(start+slen*4))
773 // Follow first branch
775 int t=(ba[i+b]-start)>>2;
776 j=7-b;if(t+j>=slen) j=slen-t-1;
779 if(!((unneeded_reg[t+j]>>r)&1)) {
780 if(rs1[t+j]==r) if(rn>j+b+2) rn=j+b+2;
781 if(rs2[t+j]==r) if(rn>j+b+2) rn=j+b+2;
791 // Try to match register allocations at the end of a loop with those
793 int loop_reg(int i, int r, int hr)
802 if(itype[i+j]==UJUMP||itype[i+j]==RJUMP||(source[i+j]>>16)==0x1000)
804 // Don't go past an unconditonal jump
811 if(itype[i-1]==UJUMP||itype[i-1]==CJUMP||itype[i-1]==SJUMP||itype[i-1]==FJUMP)
816 if(r<64&&((unneeded_reg[i+k]>>r)&1)) return hr;
817 if(r>64&&((unneeded_reg_upper[i+k]>>r)&1)) return hr;
818 if(i+k>=0&&(itype[i+k]==UJUMP||itype[i+k]==CJUMP||itype[i+k]==SJUMP||itype[i+k]==FJUMP))
820 if(ba[i+k]>=start && ba[i+k]<(start+i*4))
822 int t=(ba[i+k]-start)>>2;
823 int reg=get_reg(regs[t].regmap_entry,r);
824 if(reg>=0) return reg;
825 //reg=get_reg(regs[t+1].regmap_entry,r);
826 //if(reg>=0) return reg;
834 // Allocate every register, preserving source/target regs
835 void alloc_all(struct regstat *cur,int i)
839 for(hr=0;hr<HOST_REGS;hr++) {
840 if(hr!=EXCLUDE_REG) {
841 if(((cur->regmap[hr]&63)!=rs1[i])&&((cur->regmap[hr]&63)!=rs2[i])&&
842 ((cur->regmap[hr]&63)!=rt1[i])&&((cur->regmap[hr]&63)!=rt2[i]))
845 cur->dirty&=~(1<<hr);
848 if((cur->regmap[hr]&63)==0)
851 cur->dirty&=~(1<<hr);
858 void div64(int64_t dividend,int64_t divisor)
862 //printf("TRACE: ddiv %8x%8x %8x%8x\n" ,(int)reg[HIREG],(int)(reg[HIREG]>>32)
863 // ,(int)reg[LOREG],(int)(reg[LOREG]>>32));
865 void divu64(uint64_t dividend,uint64_t divisor)
869 //printf("TRACE: ddivu %8x%8x %8x%8x\n",(int)reg[HIREG],(int)(reg[HIREG]>>32)
870 // ,(int)reg[LOREG],(int)(reg[LOREG]>>32));
873 void mult64(uint64_t m1,uint64_t m2)
875 unsigned long long int op1, op2, op3, op4;
876 unsigned long long int result1, result2, result3, result4;
877 unsigned long long int temp1, temp2, temp3, temp4;
893 op1 = op2 & 0xFFFFFFFF;
894 op2 = (op2 >> 32) & 0xFFFFFFFF;
895 op3 = op4 & 0xFFFFFFFF;
896 op4 = (op4 >> 32) & 0xFFFFFFFF;
899 temp2 = (temp1 >> 32) + op1 * op4;
901 temp4 = (temp3 >> 32) + op2 * op4;
903 result1 = temp1 & 0xFFFFFFFF;
904 result2 = temp2 + (temp3 & 0xFFFFFFFF);
905 result3 = (result2 >> 32) + temp4;
906 result4 = (result3 >> 32);
908 lo = result1 | (result2 << 32);
909 hi = (result3 & 0xFFFFFFFF) | (result4 << 32);
918 void multu64(uint64_t m1,uint64_t m2)
920 unsigned long long int op1, op2, op3, op4;
921 unsigned long long int result1, result2, result3, result4;
922 unsigned long long int temp1, temp2, temp3, temp4;
924 op1 = m1 & 0xFFFFFFFF;
925 op2 = (m1 >> 32) & 0xFFFFFFFF;
926 op3 = m2 & 0xFFFFFFFF;
927 op4 = (m2 >> 32) & 0xFFFFFFFF;
930 temp2 = (temp1 >> 32) + op1 * op4;
932 temp4 = (temp3 >> 32) + op2 * op4;
934 result1 = temp1 & 0xFFFFFFFF;
935 result2 = temp2 + (temp3 & 0xFFFFFFFF);
936 result3 = (result2 >> 32) + temp4;
937 result4 = (result3 >> 32);
939 lo = result1 | (result2 << 32);
940 hi = (result3 & 0xFFFFFFFF) | (result4 << 32);
942 //printf("TRACE: dmultu %8x%8x %8x%8x\n",(int)reg[HIREG],(int)(reg[HIREG]>>32)
943 // ,(int)reg[LOREG],(int)(reg[LOREG]>>32));
946 uint64_t ldl_merge(uint64_t original,uint64_t loaded,u_int bits)
954 else original=loaded;
957 uint64_t ldr_merge(uint64_t original,uint64_t loaded,u_int bits)
960 original>>=64-(bits^56);
961 original<<=64-(bits^56);
965 else original=loaded;
971 #include "assem_x86.c"
974 #include "assem_x64.c"
977 #include "assem_arm.c"
980 // Add virtual address mapping to linked list
981 void ll_add(struct ll_entry **head,int vaddr,void *addr)
983 struct ll_entry *new_entry;
984 new_entry=malloc(sizeof(struct ll_entry));
985 assert(new_entry!=NULL);
986 new_entry->vaddr=vaddr;
988 new_entry->addr=addr;
989 new_entry->next=*head;
993 // Add virtual address mapping for 32-bit compiled block
994 void ll_add_32(struct ll_entry **head,int vaddr,u_int reg32,void *addr)
996 ll_add(head,vaddr,addr);
998 (*head)->reg32=reg32;
1002 // Check if an address is already compiled
1003 // but don't return addresses which are about to expire from the cache
1004 void *check_addr(u_int vaddr)
1006 u_int *ht_bin=hash_table[((vaddr>>16)^vaddr)&0xFFFF];
1007 if(ht_bin[0]==vaddr) {
1008 if(((ht_bin[1]-MAX_OUTPUT_BLOCK_SIZE-(u_int)out)<<(32-TARGET_SIZE_2))>0x60000000+(MAX_OUTPUT_BLOCK_SIZE<<(32-TARGET_SIZE_2)))
1009 if(isclean(ht_bin[1])) return (void *)ht_bin[1];
1011 if(ht_bin[2]==vaddr) {
1012 if(((ht_bin[3]-MAX_OUTPUT_BLOCK_SIZE-(u_int)out)<<(32-TARGET_SIZE_2))>0x60000000+(MAX_OUTPUT_BLOCK_SIZE<<(32-TARGET_SIZE_2)))
1013 if(isclean(ht_bin[3])) return (void *)ht_bin[3];
1015 u_int page=get_page(vaddr);
1016 struct ll_entry *head;
1019 if(head->vaddr==vaddr&&head->reg32==0) {
1020 if((((u_int)head->addr-(u_int)out)<<(32-TARGET_SIZE_2))>0x60000000+(MAX_OUTPUT_BLOCK_SIZE<<(32-TARGET_SIZE_2))) {
1021 // Update existing entry with current address
1022 if(ht_bin[0]==vaddr) {
1023 ht_bin[1]=(int)head->addr;
1026 if(ht_bin[2]==vaddr) {
1027 ht_bin[3]=(int)head->addr;
1030 // Insert into hash table with low priority.
1031 // Don't evict existing entries, as they are probably
1032 // addresses that are being accessed frequently.
1034 ht_bin[1]=(int)head->addr;
1036 }else if(ht_bin[2]==-1) {
1037 ht_bin[3]=(int)head->addr;
1048 void remove_hash(int vaddr)
1050 //printf("remove hash: %x\n",vaddr);
1051 int *ht_bin=hash_table[(((vaddr)>>16)^vaddr)&0xFFFF];
1052 if(ht_bin[2]==vaddr) {
1053 ht_bin[2]=ht_bin[3]=-1;
1055 if(ht_bin[0]==vaddr) {
1056 ht_bin[0]=ht_bin[2];
1057 ht_bin[1]=ht_bin[3];
1058 ht_bin[2]=ht_bin[3]=-1;
1062 void ll_remove_matching_addrs(struct ll_entry **head,int addr,int shift)
1064 struct ll_entry *next;
1066 if(((u_int)((*head)->addr)>>shift)==(addr>>shift) ||
1067 ((u_int)((*head)->addr-MAX_OUTPUT_BLOCK_SIZE)>>shift)==(addr>>shift))
1069 inv_debug("EXP: Remove pointer to %x (%x)\n",(int)(*head)->addr,(*head)->vaddr);
1070 remove_hash((*head)->vaddr);
1077 head=&((*head)->next);
1082 // Remove all entries from linked list
1083 void ll_clear(struct ll_entry **head)
1085 struct ll_entry *cur;
1086 struct ll_entry *next;
1097 // Dereference the pointers and remove if it matches
1098 void ll_kill_pointers(struct ll_entry *head,int addr,int shift)
1101 int ptr=get_pointer(head->addr);
1102 inv_debug("EXP: Lookup pointer to %x at %x (%x)\n",(int)ptr,(int)head->addr,head->vaddr);
1103 if(((ptr>>shift)==(addr>>shift)) ||
1104 (((ptr-MAX_OUTPUT_BLOCK_SIZE)>>shift)==(addr>>shift)))
1106 inv_debug("EXP: Kill pointer at %x (%x)\n",(int)head->addr,head->vaddr);
1107 u_int host_addr=(u_int)kill_pointer(head->addr);
1109 needs_clear_cache[(host_addr-(u_int)BASE_ADDR)>>17]|=1<<(((host_addr-(u_int)BASE_ADDR)>>12)&31);
1116 // This is called when we write to a compiled block (see do_invstub)
1117 void invalidate_page(u_int page)
1119 struct ll_entry *head;
1120 struct ll_entry *next;
1124 inv_debug("INVALIDATE: %x\n",head->vaddr);
1125 remove_hash(head->vaddr);
1130 head=jump_out[page];
1133 inv_debug("INVALIDATE: kill pointer to %x (%x)\n",head->vaddr,(int)head->addr);
1134 u_int host_addr=(u_int)kill_pointer(head->addr);
1136 needs_clear_cache[(host_addr-(u_int)BASE_ADDR)>>17]|=1<<(((host_addr-(u_int)BASE_ADDR)>>12)&31);
1144 static void invalidate_block_range(u_int block, u_int first, u_int last)
1146 u_int page=get_page(block<<12);
1147 //printf("first=%d last=%d\n",first,last);
1148 invalidate_page(page);
1149 assert(first+5>page); // NB: this assumes MAXBLOCK<=4096 (4 pages)
1150 assert(last<page+5);
1151 // Invalidate the adjacent pages if a block crosses a 4K boundary
1153 invalidate_page(first);
1156 for(first=page+1;first<last;first++) {
1157 invalidate_page(first);
1163 // Don't trap writes
1164 invalid_code[block]=1;
1166 // If there is a valid TLB entry for this page, remove write protect
1167 if(tlb_LUT_w[block]) {
1168 assert(tlb_LUT_r[block]==tlb_LUT_w[block]);
1169 // CHECK: Is this right?
1170 memory_map[block]=((tlb_LUT_w[block]&0xFFFFF000)-(block<<12)+(unsigned int)rdram-0x80000000)>>2;
1171 u_int real_block=tlb_LUT_w[block]>>12;
1172 invalid_code[real_block]=1;
1173 if(real_block>=0x80000&&real_block<0x80800) memory_map[real_block]=((u_int)rdram-0x80000000)>>2;
1175 else if(block>=0x80000&&block<0x80800) memory_map[block]=((u_int)rdram-0x80000000)>>2;
1179 memset(mini_ht,-1,sizeof(mini_ht));
1183 void invalidate_block(u_int block)
1185 u_int page=get_page(block<<12);
1186 u_int vpage=get_vpage(block<<12);
1187 inv_debug("INVALIDATE: %x (%d)\n",block<<12,page);
1188 //inv_debug("invalid_code[block]=%d\n",invalid_code[block]);
1191 struct ll_entry *head;
1192 head=jump_dirty[vpage];
1193 //printf("page=%d vpage=%d\n",page,vpage);
1196 if(vpage>2047||(head->vaddr>>12)==block) { // Ignore vaddr hash collision
1197 get_bounds((int)head->addr,&start,&end);
1198 //printf("start: %x end: %x\n",start,end);
1199 if(page<2048&&start>=0x80000000&&end<0x80000000+RAM_SIZE) {
1200 if(((start-(u_int)rdram)>>12)<=page&&((end-1-(u_int)rdram)>>12)>=page) {
1201 if((((start-(u_int)rdram)>>12)&2047)<first) first=((start-(u_int)rdram)>>12)&2047;
1202 if((((end-1-(u_int)rdram)>>12)&2047)>last) last=((end-1-(u_int)rdram)>>12)&2047;
1206 if(page<2048&&(signed int)start>=(signed int)0xC0000000&&(signed int)end>=(signed int)0xC0000000) {
1207 if(((start+memory_map[start>>12]-(u_int)rdram)>>12)<=page&&((end-1+memory_map[(end-1)>>12]-(u_int)rdram)>>12)>=page) {
1208 if((((start+memory_map[start>>12]-(u_int)rdram)>>12)&2047)<first) first=((start+memory_map[start>>12]-(u_int)rdram)>>12)&2047;
1209 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;
1216 invalidate_block_range(block,first,last);
1219 void invalidate_addr(u_int addr)
1223 // this check is done by the caller
1224 //if (inv_code_start<=addr&&addr<=inv_code_end) { rhits++; return; }
1225 u_int page=get_page(addr);
1226 if(page<2048) { // RAM
1227 struct ll_entry *head;
1228 u_int addr_min=~0, addr_max=0;
1229 int mask=RAM_SIZE-1;
1231 inv_code_start=addr&~0xfff;
1232 inv_code_end=addr|0xfff;
1235 // must check previous page too because of spans..
1237 inv_code_start-=0x1000;
1239 for(;pg1<=page;pg1++) {
1240 for(head=jump_dirty[pg1];head!=NULL;head=head->next) {
1242 get_bounds((int)head->addr,&start,&end);
1243 if((start&mask)<=(addr&mask)&&(addr&mask)<(end&mask)) {
1244 if(start<addr_min) addr_min=start;
1245 if(end>addr_max) addr_max=end;
1247 else if(addr<start) {
1248 if(start<inv_code_end)
1249 inv_code_end=start-1;
1252 if(end>inv_code_start)
1258 inv_debug("INV ADDR: %08x hit %08x-%08x\n", addr, addr_min, addr_max);
1259 inv_code_start=inv_code_end=~0;
1260 invalidate_block_range(addr>>12,(addr_min&mask)>>12,(addr_max&mask)>>12);
1264 inv_debug("INV ADDR: %08x miss, inv %08x-%08x, sk %d\n", addr, inv_code_start, inv_code_end, 0);//rhits);
1267 if(page!=0) // FIXME: don't know what's up with page 0 (Klonoa)
1271 invalidate_block(addr>>12);
1274 // This is called when loading a save state.
1275 // Anything could have changed, so invalidate everything.
1276 void invalidate_all_pages()
1279 for(page=0;page<4096;page++)
1280 invalidate_page(page);
1281 for(page=0;page<1048576;page++)
1282 if(!invalid_code[page]) {
1283 restore_candidate[(page&2047)>>3]|=1<<(page&7);
1284 restore_candidate[((page&2047)>>3)+256]|=1<<(page&7);
1287 __clear_cache((void *)BASE_ADDR,(void *)BASE_ADDR+(1<<TARGET_SIZE_2));
1290 memset(mini_ht,-1,sizeof(mini_ht));
1294 for(page=0;page<0x100000;page++) {
1295 if(tlb_LUT_r[page]) {
1296 memory_map[page]=((tlb_LUT_r[page]&0xFFFFF000)-(page<<12)+(unsigned int)rdram-0x80000000)>>2;
1297 if(!tlb_LUT_w[page]||!invalid_code[page])
1298 memory_map[page]|=0x40000000; // Write protect
1300 else memory_map[page]=-1;
1301 if(page==0x80000) page=0xC0000;
1307 // Add an entry to jump_out after making a link
1308 void add_link(u_int vaddr,void *src)
1310 u_int page=get_page(vaddr);
1311 inv_debug("add_link: %x -> %x (%d)\n",(int)src,vaddr,page);
1312 int *ptr=(int *)(src+4);
1313 assert((*ptr&0x0fff0000)==0x059f0000);
1314 ll_add(jump_out+page,vaddr,src);
1315 //int ptr=get_pointer(src);
1316 //inv_debug("add_link: Pointer is to %x\n",(int)ptr);
1319 // If a code block was found to be unmodified (bit was set in
1320 // restore_candidate) and it remains unmodified (bit is clear
1321 // in invalid_code) then move the entries for that 4K page from
1322 // the dirty list to the clean list.
1323 void clean_blocks(u_int page)
1325 struct ll_entry *head;
1326 inv_debug("INV: clean_blocks page=%d\n",page);
1327 head=jump_dirty[page];
1329 if(!invalid_code[head->vaddr>>12]) {
1330 // Don't restore blocks which are about to expire from the cache
1331 if((((u_int)head->addr-(u_int)out)<<(32-TARGET_SIZE_2))>0x60000000+(MAX_OUTPUT_BLOCK_SIZE<<(32-TARGET_SIZE_2))) {
1333 if(verify_dirty((int)head->addr)) {
1334 //printf("Possibly Restore %x (%x)\n",head->vaddr, (int)head->addr);
1337 get_bounds((int)head->addr,&start,&end);
1338 if(start-(u_int)rdram<RAM_SIZE) {
1339 for(i=(start-(u_int)rdram+0x80000000)>>12;i<=(end-1-(u_int)rdram+0x80000000)>>12;i++) {
1340 inv|=invalid_code[i];
1344 if((signed int)head->vaddr>=(signed int)0xC0000000) {
1345 u_int addr = (head->vaddr+(memory_map[head->vaddr>>12]<<2));
1346 //printf("addr=%x start=%x end=%x\n",addr,start,end);
1347 if(addr<start||addr>=end) inv=1;
1350 else if((signed int)head->vaddr>=(signed int)0x80000000+RAM_SIZE) {
1354 void * clean_addr=(void *)get_clean_addr((int)head->addr);
1355 if((((u_int)clean_addr-(u_int)out)<<(32-TARGET_SIZE_2))>0x60000000+(MAX_OUTPUT_BLOCK_SIZE<<(32-TARGET_SIZE_2))) {
1358 if(page<2048&&tlb_LUT_r[head->vaddr>>12]) ppage=(tlb_LUT_r[head->vaddr>>12]^0x80000000)>>12;
1360 inv_debug("INV: Restored %x (%x/%x)\n",head->vaddr, (int)head->addr, (int)clean_addr);
1361 //printf("page=%x, addr=%x\n",page,head->vaddr);
1362 //assert(head->vaddr>>12==(page|0x80000));
1363 ll_add_32(jump_in+ppage,head->vaddr,head->reg32,clean_addr);
1364 int *ht_bin=hash_table[((head->vaddr>>16)^head->vaddr)&0xFFFF];
1366 if(ht_bin[0]==head->vaddr) {
1367 ht_bin[1]=(int)clean_addr; // Replace existing entry
1369 if(ht_bin[2]==head->vaddr) {
1370 ht_bin[3]=(int)clean_addr; // Replace existing entry
1383 void mov_alloc(struct regstat *current,int i)
1385 // Note: Don't need to actually alloc the source registers
1386 if((~current->is32>>rs1[i])&1) {
1387 //alloc_reg64(current,i,rs1[i]);
1388 alloc_reg64(current,i,rt1[i]);
1389 current->is32&=~(1LL<<rt1[i]);
1391 //alloc_reg(current,i,rs1[i]);
1392 alloc_reg(current,i,rt1[i]);
1393 current->is32|=(1LL<<rt1[i]);
1395 clear_const(current,rs1[i]);
1396 clear_const(current,rt1[i]);
1397 dirty_reg(current,rt1[i]);
1400 void shiftimm_alloc(struct regstat *current,int i)
1402 if(opcode2[i]<=0x3) // SLL/SRL/SRA
1405 if(rs1[i]&&needed_again(rs1[i],i)) alloc_reg(current,i,rs1[i]);
1407 alloc_reg(current,i,rt1[i]);
1408 current->is32|=1LL<<rt1[i];
1409 dirty_reg(current,rt1[i]);
1410 if(is_const(current,rs1[i])) {
1411 int v=get_const(current,rs1[i]);
1412 if(opcode2[i]==0x00) set_const(current,rt1[i],v<<imm[i]);
1413 if(opcode2[i]==0x02) set_const(current,rt1[i],(u_int)v>>imm[i]);
1414 if(opcode2[i]==0x03) set_const(current,rt1[i],v>>imm[i]);
1416 else clear_const(current,rt1[i]);
1421 clear_const(current,rs1[i]);
1422 clear_const(current,rt1[i]);
1425 if(opcode2[i]>=0x38&&opcode2[i]<=0x3b) // DSLL/DSRL/DSRA
1428 if(rs1[i]) alloc_reg64(current,i,rs1[i]);
1429 alloc_reg64(current,i,rt1[i]);
1430 current->is32&=~(1LL<<rt1[i]);
1431 dirty_reg(current,rt1[i]);
1434 if(opcode2[i]==0x3c) // DSLL32
1437 if(rs1[i]) alloc_reg(current,i,rs1[i]);
1438 alloc_reg64(current,i,rt1[i]);
1439 current->is32&=~(1LL<<rt1[i]);
1440 dirty_reg(current,rt1[i]);
1443 if(opcode2[i]==0x3e) // DSRL32
1446 alloc_reg64(current,i,rs1[i]);
1448 alloc_reg64(current,i,rt1[i]);
1449 current->is32&=~(1LL<<rt1[i]);
1451 alloc_reg(current,i,rt1[i]);
1452 current->is32|=1LL<<rt1[i];
1454 dirty_reg(current,rt1[i]);
1457 if(opcode2[i]==0x3f) // DSRA32
1460 alloc_reg64(current,i,rs1[i]);
1461 alloc_reg(current,i,rt1[i]);
1462 current->is32|=1LL<<rt1[i];
1463 dirty_reg(current,rt1[i]);
1468 void shift_alloc(struct regstat *current,int i)
1471 if(opcode2[i]<=0x07) // SLLV/SRLV/SRAV
1473 if(rs1[i]) alloc_reg(current,i,rs1[i]);
1474 if(rs2[i]) alloc_reg(current,i,rs2[i]);
1475 alloc_reg(current,i,rt1[i]);
1476 if(rt1[i]==rs2[i]) {
1477 alloc_reg_temp(current,i,-1);
1478 minimum_free_regs[i]=1;
1480 current->is32|=1LL<<rt1[i];
1481 } else { // DSLLV/DSRLV/DSRAV
1482 if(rs1[i]) alloc_reg64(current,i,rs1[i]);
1483 if(rs2[i]) alloc_reg(current,i,rs2[i]);
1484 alloc_reg64(current,i,rt1[i]);
1485 current->is32&=~(1LL<<rt1[i]);
1486 if(opcode2[i]==0x16||opcode2[i]==0x17) // DSRLV and DSRAV need a temporary register
1488 alloc_reg_temp(current,i,-1);
1489 minimum_free_regs[i]=1;
1492 clear_const(current,rs1[i]);
1493 clear_const(current,rs2[i]);
1494 clear_const(current,rt1[i]);
1495 dirty_reg(current,rt1[i]);
1499 void alu_alloc(struct regstat *current,int i)
1501 if(opcode2[i]>=0x20&&opcode2[i]<=0x23) { // ADD/ADDU/SUB/SUBU
1503 if(rs1[i]&&rs2[i]) {
1504 alloc_reg(current,i,rs1[i]);
1505 alloc_reg(current,i,rs2[i]);
1508 if(rs1[i]&&needed_again(rs1[i],i)) alloc_reg(current,i,rs1[i]);
1509 if(rs2[i]&&needed_again(rs2[i],i)) alloc_reg(current,i,rs2[i]);
1511 alloc_reg(current,i,rt1[i]);
1513 current->is32|=1LL<<rt1[i];
1515 if(opcode2[i]==0x2a||opcode2[i]==0x2b) { // SLT/SLTU
1517 if(!((current->is32>>rs1[i])&(current->is32>>rs2[i])&1))
1519 alloc_reg64(current,i,rs1[i]);
1520 alloc_reg64(current,i,rs2[i]);
1521 alloc_reg(current,i,rt1[i]);
1523 alloc_reg(current,i,rs1[i]);
1524 alloc_reg(current,i,rs2[i]);
1525 alloc_reg(current,i,rt1[i]);
1528 current->is32|=1LL<<rt1[i];
1530 if(opcode2[i]>=0x24&&opcode2[i]<=0x27) { // AND/OR/XOR/NOR
1532 if(rs1[i]&&rs2[i]) {
1533 alloc_reg(current,i,rs1[i]);
1534 alloc_reg(current,i,rs2[i]);
1538 if(rs1[i]&&needed_again(rs1[i],i)) alloc_reg(current,i,rs1[i]);
1539 if(rs2[i]&&needed_again(rs2[i],i)) alloc_reg(current,i,rs2[i]);
1541 alloc_reg(current,i,rt1[i]);
1542 if(!((current->is32>>rs1[i])&(current->is32>>rs2[i])&1))
1544 if(!((current->uu>>rt1[i])&1)) {
1545 alloc_reg64(current,i,rt1[i]);
1547 if(get_reg(current->regmap,rt1[i]|64)>=0) {
1548 if(rs1[i]&&rs2[i]) {
1549 alloc_reg64(current,i,rs1[i]);
1550 alloc_reg64(current,i,rs2[i]);
1554 // Is is really worth it to keep 64-bit values in registers?
1556 if(rs1[i]&&needed_again(rs1[i],i)) alloc_reg64(current,i,rs1[i]);
1557 if(rs2[i]&&needed_again(rs2[i],i)) alloc_reg64(current,i,rs2[i]);
1561 current->is32&=~(1LL<<rt1[i]);
1563 current->is32|=1LL<<rt1[i];
1567 if(opcode2[i]>=0x2c&&opcode2[i]<=0x2f) { // DADD/DADDU/DSUB/DSUBU
1569 if(rs1[i]&&rs2[i]) {
1570 if(!((current->uu>>rt1[i])&1)||get_reg(current->regmap,rt1[i]|64)>=0) {
1571 alloc_reg64(current,i,rs1[i]);
1572 alloc_reg64(current,i,rs2[i]);
1573 alloc_reg64(current,i,rt1[i]);
1575 alloc_reg(current,i,rs1[i]);
1576 alloc_reg(current,i,rs2[i]);
1577 alloc_reg(current,i,rt1[i]);
1581 alloc_reg(current,i,rt1[i]);
1582 if(!((current->uu>>rt1[i])&1)||get_reg(current->regmap,rt1[i]|64)>=0) {
1583 // DADD used as move, or zeroing
1584 // If we have a 64-bit source, then make the target 64 bits too
1585 if(rs1[i]&&!((current->is32>>rs1[i])&1)) {
1586 if(get_reg(current->regmap,rs1[i])>=0) alloc_reg64(current,i,rs1[i]);
1587 alloc_reg64(current,i,rt1[i]);
1588 } else if(rs2[i]&&!((current->is32>>rs2[i])&1)) {
1589 if(get_reg(current->regmap,rs2[i])>=0) alloc_reg64(current,i,rs2[i]);
1590 alloc_reg64(current,i,rt1[i]);
1592 if(opcode2[i]>=0x2e&&rs2[i]) {
1593 // DSUB used as negation - 64-bit result
1594 // If we have a 32-bit register, extend it to 64 bits
1595 if(get_reg(current->regmap,rs2[i])>=0) alloc_reg64(current,i,rs2[i]);
1596 alloc_reg64(current,i,rt1[i]);
1600 if(rs1[i]&&rs2[i]) {
1601 current->is32&=~(1LL<<rt1[i]);
1603 current->is32&=~(1LL<<rt1[i]);
1604 if((current->is32>>rs1[i])&1)
1605 current->is32|=1LL<<rt1[i];
1607 current->is32&=~(1LL<<rt1[i]);
1608 if((current->is32>>rs2[i])&1)
1609 current->is32|=1LL<<rt1[i];
1611 current->is32|=1LL<<rt1[i];
1615 clear_const(current,rs1[i]);
1616 clear_const(current,rs2[i]);
1617 clear_const(current,rt1[i]);
1618 dirty_reg(current,rt1[i]);
1621 void imm16_alloc(struct regstat *current,int i)
1623 if(rs1[i]&&needed_again(rs1[i],i)) alloc_reg(current,i,rs1[i]);
1625 if(rt1[i]) alloc_reg(current,i,rt1[i]);
1626 if(opcode[i]==0x18||opcode[i]==0x19) { // DADDI/DADDIU
1627 current->is32&=~(1LL<<rt1[i]);
1628 if(!((current->uu>>rt1[i])&1)||get_reg(current->regmap,rt1[i]|64)>=0) {
1629 // TODO: Could preserve the 32-bit flag if the immediate is zero
1630 alloc_reg64(current,i,rt1[i]);
1631 alloc_reg64(current,i,rs1[i]);
1633 clear_const(current,rs1[i]);
1634 clear_const(current,rt1[i]);
1636 else if(opcode[i]==0x0a||opcode[i]==0x0b) { // SLTI/SLTIU
1637 if((~current->is32>>rs1[i])&1) alloc_reg64(current,i,rs1[i]);
1638 current->is32|=1LL<<rt1[i];
1639 clear_const(current,rs1[i]);
1640 clear_const(current,rt1[i]);
1642 else if(opcode[i]>=0x0c&&opcode[i]<=0x0e) { // ANDI/ORI/XORI
1643 if(((~current->is32>>rs1[i])&1)&&opcode[i]>0x0c) {
1644 if(rs1[i]!=rt1[i]) {
1645 if(needed_again(rs1[i],i)) alloc_reg64(current,i,rs1[i]);
1646 alloc_reg64(current,i,rt1[i]);
1647 current->is32&=~(1LL<<rt1[i]);
1650 else current->is32|=1LL<<rt1[i]; // ANDI clears upper bits
1651 if(is_const(current,rs1[i])) {
1652 int v=get_const(current,rs1[i]);
1653 if(opcode[i]==0x0c) set_const(current,rt1[i],v&imm[i]);
1654 if(opcode[i]==0x0d) set_const(current,rt1[i],v|imm[i]);
1655 if(opcode[i]==0x0e) set_const(current,rt1[i],v^imm[i]);
1657 else clear_const(current,rt1[i]);
1659 else if(opcode[i]==0x08||opcode[i]==0x09) { // ADDI/ADDIU
1660 if(is_const(current,rs1[i])) {
1661 int v=get_const(current,rs1[i]);
1662 set_const(current,rt1[i],v+imm[i]);
1664 else clear_const(current,rt1[i]);
1665 current->is32|=1LL<<rt1[i];
1668 set_const(current,rt1[i],((long long)((short)imm[i]))<<16); // LUI
1669 current->is32|=1LL<<rt1[i];
1671 dirty_reg(current,rt1[i]);
1674 void load_alloc(struct regstat *current,int i)
1676 clear_const(current,rt1[i]);
1677 //if(rs1[i]!=rt1[i]&&needed_again(rs1[i],i)) clear_const(current,rs1[i]); // Does this help or hurt?
1678 if(!rs1[i]) current->u&=~1LL; // Allow allocating r0 if it's the source register
1679 if(needed_again(rs1[i],i)) alloc_reg(current,i,rs1[i]);
1680 if(rt1[i]&&!((current->u>>rt1[i])&1)) {
1681 alloc_reg(current,i,rt1[i]);
1682 assert(get_reg(current->regmap,rt1[i])>=0);
1683 if(opcode[i]==0x27||opcode[i]==0x37) // LWU/LD
1685 current->is32&=~(1LL<<rt1[i]);
1686 alloc_reg64(current,i,rt1[i]);
1688 else if(opcode[i]==0x1A||opcode[i]==0x1B) // LDL/LDR
1690 current->is32&=~(1LL<<rt1[i]);
1691 alloc_reg64(current,i,rt1[i]);
1692 alloc_all(current,i);
1693 alloc_reg64(current,i,FTEMP);
1694 minimum_free_regs[i]=HOST_REGS;
1696 else current->is32|=1LL<<rt1[i];
1697 dirty_reg(current,rt1[i]);
1698 // If using TLB, need a register for pointer to the mapping table
1699 if(using_tlb) alloc_reg(current,i,TLREG);
1700 // LWL/LWR need a temporary register for the old value
1701 if(opcode[i]==0x22||opcode[i]==0x26)
1703 alloc_reg(current,i,FTEMP);
1704 alloc_reg_temp(current,i,-1);
1705 minimum_free_regs[i]=1;
1710 // Load to r0 or unneeded register (dummy load)
1711 // but we still need a register to calculate the address
1712 if(opcode[i]==0x22||opcode[i]==0x26)
1714 alloc_reg(current,i,FTEMP); // LWL/LWR need another temporary
1716 // If using TLB, need a register for pointer to the mapping table
1717 if(using_tlb) alloc_reg(current,i,TLREG);
1718 alloc_reg_temp(current,i,-1);
1719 minimum_free_regs[i]=1;
1720 if(opcode[i]==0x1A||opcode[i]==0x1B) // LDL/LDR
1722 alloc_all(current,i);
1723 alloc_reg64(current,i,FTEMP);
1724 minimum_free_regs[i]=HOST_REGS;
1729 void store_alloc(struct regstat *current,int i)
1731 clear_const(current,rs2[i]);
1732 if(!(rs2[i])) current->u&=~1LL; // Allow allocating r0 if necessary
1733 if(needed_again(rs1[i],i)) alloc_reg(current,i,rs1[i]);
1734 alloc_reg(current,i,rs2[i]);
1735 if(opcode[i]==0x2c||opcode[i]==0x2d||opcode[i]==0x3f) { // 64-bit SDL/SDR/SD
1736 alloc_reg64(current,i,rs2[i]);
1737 if(rs2[i]) alloc_reg(current,i,FTEMP);
1739 // If using TLB, need a register for pointer to the mapping table
1740 if(using_tlb) alloc_reg(current,i,TLREG);
1741 #if defined(HOST_IMM8)
1742 // On CPUs without 32-bit immediates we need a pointer to invalid_code
1743 else alloc_reg(current,i,INVCP);
1745 if(opcode[i]==0x2a||opcode[i]==0x2e||opcode[i]==0x2c||opcode[i]==0x2d) { // SWL/SWL/SDL/SDR
1746 alloc_reg(current,i,FTEMP);
1748 // We need a temporary register for address generation
1749 alloc_reg_temp(current,i,-1);
1750 minimum_free_regs[i]=1;
1753 void c1ls_alloc(struct regstat *current,int i)
1755 //clear_const(current,rs1[i]); // FIXME
1756 clear_const(current,rt1[i]);
1757 if(needed_again(rs1[i],i)) alloc_reg(current,i,rs1[i]);
1758 alloc_reg(current,i,CSREG); // Status
1759 alloc_reg(current,i,FTEMP);
1760 if(opcode[i]==0x35||opcode[i]==0x3d) { // 64-bit LDC1/SDC1
1761 alloc_reg64(current,i,FTEMP);
1763 // If using TLB, need a register for pointer to the mapping table
1764 if(using_tlb) alloc_reg(current,i,TLREG);
1765 #if defined(HOST_IMM8)
1766 // On CPUs without 32-bit immediates we need a pointer to invalid_code
1767 else if((opcode[i]&0x3b)==0x39) // SWC1/SDC1
1768 alloc_reg(current,i,INVCP);
1770 // We need a temporary register for address generation
1771 alloc_reg_temp(current,i,-1);
1774 void c2ls_alloc(struct regstat *current,int i)
1776 clear_const(current,rt1[i]);
1777 if(needed_again(rs1[i],i)) alloc_reg(current,i,rs1[i]);
1778 alloc_reg(current,i,FTEMP);
1779 // If using TLB, need a register for pointer to the mapping table
1780 if(using_tlb) alloc_reg(current,i,TLREG);
1781 #if defined(HOST_IMM8)
1782 // On CPUs without 32-bit immediates we need a pointer to invalid_code
1783 else if((opcode[i]&0x3b)==0x3a) // SWC2/SDC2
1784 alloc_reg(current,i,INVCP);
1786 // We need a temporary register for address generation
1787 alloc_reg_temp(current,i,-1);
1788 minimum_free_regs[i]=1;
1791 #ifndef multdiv_alloc
1792 void multdiv_alloc(struct regstat *current,int i)
1799 // case 0x1D: DMULTU
1802 clear_const(current,rs1[i]);
1803 clear_const(current,rs2[i]);
1806 if((opcode2[i]&4)==0) // 32-bit
1808 current->u&=~(1LL<<HIREG);
1809 current->u&=~(1LL<<LOREG);
1810 alloc_reg(current,i,HIREG);
1811 alloc_reg(current,i,LOREG);
1812 alloc_reg(current,i,rs1[i]);
1813 alloc_reg(current,i,rs2[i]);
1814 current->is32|=1LL<<HIREG;
1815 current->is32|=1LL<<LOREG;
1816 dirty_reg(current,HIREG);
1817 dirty_reg(current,LOREG);
1821 current->u&=~(1LL<<HIREG);
1822 current->u&=~(1LL<<LOREG);
1823 current->uu&=~(1LL<<HIREG);
1824 current->uu&=~(1LL<<LOREG);
1825 alloc_reg64(current,i,HIREG);
1826 //if(HOST_REGS>10) alloc_reg64(current,i,LOREG);
1827 alloc_reg64(current,i,rs1[i]);
1828 alloc_reg64(current,i,rs2[i]);
1829 alloc_all(current,i);
1830 current->is32&=~(1LL<<HIREG);
1831 current->is32&=~(1LL<<LOREG);
1832 dirty_reg(current,HIREG);
1833 dirty_reg(current,LOREG);
1834 minimum_free_regs[i]=HOST_REGS;
1839 // Multiply by zero is zero.
1840 // MIPS does not have a divide by zero exception.
1841 // The result is undefined, we return zero.
1842 alloc_reg(current,i,HIREG);
1843 alloc_reg(current,i,LOREG);
1844 current->is32|=1LL<<HIREG;
1845 current->is32|=1LL<<LOREG;
1846 dirty_reg(current,HIREG);
1847 dirty_reg(current,LOREG);
1852 void cop0_alloc(struct regstat *current,int i)
1854 if(opcode2[i]==0) // MFC0
1857 clear_const(current,rt1[i]);
1858 alloc_all(current,i);
1859 alloc_reg(current,i,rt1[i]);
1860 current->is32|=1LL<<rt1[i];
1861 dirty_reg(current,rt1[i]);
1864 else if(opcode2[i]==4) // MTC0
1867 clear_const(current,rs1[i]);
1868 alloc_reg(current,i,rs1[i]);
1869 alloc_all(current,i);
1872 alloc_all(current,i); // FIXME: Keep r0
1874 alloc_reg(current,i,0);
1879 // TLBR/TLBWI/TLBWR/TLBP/ERET
1880 assert(opcode2[i]==0x10);
1881 alloc_all(current,i);
1883 minimum_free_regs[i]=HOST_REGS;
1886 void cop1_alloc(struct regstat *current,int i)
1888 alloc_reg(current,i,CSREG); // Load status
1889 if(opcode2[i]<3) // MFC1/DMFC1/CFC1
1892 clear_const(current,rt1[i]);
1894 alloc_reg64(current,i,rt1[i]); // DMFC1
1895 current->is32&=~(1LL<<rt1[i]);
1897 alloc_reg(current,i,rt1[i]); // MFC1/CFC1
1898 current->is32|=1LL<<rt1[i];
1900 dirty_reg(current,rt1[i]);
1902 alloc_reg_temp(current,i,-1);
1904 else if(opcode2[i]>3) // MTC1/DMTC1/CTC1
1907 clear_const(current,rs1[i]);
1909 alloc_reg64(current,i,rs1[i]); // DMTC1
1911 alloc_reg(current,i,rs1[i]); // MTC1/CTC1
1912 alloc_reg_temp(current,i,-1);
1916 alloc_reg(current,i,0);
1917 alloc_reg_temp(current,i,-1);
1920 minimum_free_regs[i]=1;
1922 void fconv_alloc(struct regstat *current,int i)
1924 alloc_reg(current,i,CSREG); // Load status
1925 alloc_reg_temp(current,i,-1);
1926 minimum_free_regs[i]=1;
1928 void float_alloc(struct regstat *current,int i)
1930 alloc_reg(current,i,CSREG); // Load status
1931 alloc_reg_temp(current,i,-1);
1932 minimum_free_regs[i]=1;
1934 void c2op_alloc(struct regstat *current,int i)
1936 alloc_reg_temp(current,i,-1);
1938 void fcomp_alloc(struct regstat *current,int i)
1940 alloc_reg(current,i,CSREG); // Load status
1941 alloc_reg(current,i,FSREG); // Load flags
1942 dirty_reg(current,FSREG); // Flag will be modified
1943 alloc_reg_temp(current,i,-1);
1944 minimum_free_regs[i]=1;
1947 void syscall_alloc(struct regstat *current,int i)
1949 alloc_cc(current,i);
1950 dirty_reg(current,CCREG);
1951 alloc_all(current,i);
1952 minimum_free_regs[i]=HOST_REGS;
1956 void delayslot_alloc(struct regstat *current,int i)
1967 assem_debug("jump in the delay slot. this shouldn't happen.\n");//exit(1);
1968 printf("Disabled speculative precompilation\n");
1972 imm16_alloc(current,i);
1976 load_alloc(current,i);
1980 store_alloc(current,i);
1983 alu_alloc(current,i);
1986 shift_alloc(current,i);
1989 multdiv_alloc(current,i);
1992 shiftimm_alloc(current,i);
1995 mov_alloc(current,i);
1998 cop0_alloc(current,i);
2002 cop1_alloc(current,i);
2005 c1ls_alloc(current,i);
2008 c2ls_alloc(current,i);
2011 fconv_alloc(current,i);
2014 float_alloc(current,i);
2017 fcomp_alloc(current,i);
2020 c2op_alloc(current,i);
2025 // Special case where a branch and delay slot span two pages in virtual memory
2026 static void pagespan_alloc(struct regstat *current,int i)
2029 current->wasconst=0;
2031 minimum_free_regs[i]=HOST_REGS;
2032 alloc_all(current,i);
2033 alloc_cc(current,i);
2034 dirty_reg(current,CCREG);
2035 if(opcode[i]==3) // JAL
2037 alloc_reg(current,i,31);
2038 dirty_reg(current,31);
2040 if(opcode[i]==0&&(opcode2[i]&0x3E)==8) // JR/JALR
2042 alloc_reg(current,i,rs1[i]);
2044 alloc_reg(current,i,rt1[i]);
2045 dirty_reg(current,rt1[i]);
2048 if((opcode[i]&0x2E)==4) // BEQ/BNE/BEQL/BNEL
2050 if(rs1[i]) alloc_reg(current,i,rs1[i]);
2051 if(rs2[i]) alloc_reg(current,i,rs2[i]);
2052 if(!((current->is32>>rs1[i])&(current->is32>>rs2[i])&1))
2054 if(rs1[i]) alloc_reg64(current,i,rs1[i]);
2055 if(rs2[i]) alloc_reg64(current,i,rs2[i]);
2059 if((opcode[i]&0x2E)==6) // BLEZ/BGTZ/BLEZL/BGTZL
2061 if(rs1[i]) alloc_reg(current,i,rs1[i]);
2062 if(!((current->is32>>rs1[i])&1))
2064 if(rs1[i]) alloc_reg64(current,i,rs1[i]);
2068 if(opcode[i]==0x11) // BC1
2070 alloc_reg(current,i,FSREG);
2071 alloc_reg(current,i,CSREG);
2076 add_stub(int type,int addr,int retaddr,int a,int b,int c,int d,int e)
2078 stubs[stubcount][0]=type;
2079 stubs[stubcount][1]=addr;
2080 stubs[stubcount][2]=retaddr;
2081 stubs[stubcount][3]=a;
2082 stubs[stubcount][4]=b;
2083 stubs[stubcount][5]=c;
2084 stubs[stubcount][6]=d;
2085 stubs[stubcount][7]=e;
2089 // Write out a single register
2090 void wb_register(signed char r,signed char regmap[],uint64_t dirty,uint64_t is32)
2093 for(hr=0;hr<HOST_REGS;hr++) {
2094 if(hr!=EXCLUDE_REG) {
2095 if((regmap[hr]&63)==r) {
2098 emit_storereg(r,hr);
2100 if((is32>>regmap[hr])&1) {
2101 emit_sarimm(hr,31,hr);
2102 emit_storereg(r|64,hr);
2106 emit_storereg(r|64,hr);
2116 //if(!tracedebug) return 0;
2119 for(i=0;i<2097152;i++) {
2120 unsigned int temp=sum;
2123 sum^=((u_int *)rdram)[i];
2132 sum^=((u_int *)reg)[i];
2140 printf("r%d:%8x%8x ",i,((int *)(reg+i))[1],((int *)(reg+i))[0]);
2142 #ifndef DISABLE_COP1
2145 printf("f%d:%8x%8x ",i,((int*)reg_cop1_simple[i])[1],*((int*)reg_cop1_simple[i]));
2155 void memdebug(int i)
2157 //printf("TRACE: count=%d next=%d (checksum %x) lo=%8x%8x\n",Count,next_interupt,mchecksum(),(int)(reg[LOREG]>>32),(int)reg[LOREG]);
2158 //printf("TRACE: count=%d next=%d (rchecksum %x)\n",Count,next_interupt,rchecksum());
2161 //if(Count>=-2084597794) {
2162 if((signed int)Count>=-2084597794&&(signed int)Count<0) {
2164 printf("TRACE: count=%d next=%d (checksum %x)\n",Count,next_interupt,mchecksum());
2165 //printf("TRACE: count=%d next=%d (checksum %x) Status=%x\n",Count,next_interupt,mchecksum(),Status);
2166 //printf("TRACE: count=%d next=%d (checksum %x) hi=%8x%8x\n",Count,next_interupt,mchecksum(),(int)(reg[HIREG]>>32),(int)reg[HIREG]);
2169 printf("TRACE: %x\n",(&i)[-1]);
2173 printf("TRACE: %x \n",(&j)[10]);
2174 printf("TRACE: %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x\n",(&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]);
2178 //printf("TRACE: %x\n",(&i)[-1]);
2181 void tlb_debug(u_int cause, u_int addr, u_int iaddr)
2183 printf("TLB Exception: instruction=%x addr=%x cause=%x\n",iaddr, addr, cause);
2186 void alu_assemble(int i,struct regstat *i_regs)
2188 if(opcode2[i]>=0x20&&opcode2[i]<=0x23) { // ADD/ADDU/SUB/SUBU
2190 signed char s1,s2,t;
2191 t=get_reg(i_regs->regmap,rt1[i]);
2193 s1=get_reg(i_regs->regmap,rs1[i]);
2194 s2=get_reg(i_regs->regmap,rs2[i]);
2195 if(rs1[i]&&rs2[i]) {
2198 if(opcode2[i]&2) emit_sub(s1,s2,t);
2199 else emit_add(s1,s2,t);
2202 if(s1>=0) emit_mov(s1,t);
2203 else emit_loadreg(rs1[i],t);
2207 if(opcode2[i]&2) emit_neg(s2,t);
2208 else emit_mov(s2,t);
2211 emit_loadreg(rs2[i],t);
2212 if(opcode2[i]&2) emit_neg(t,t);
2215 else emit_zeroreg(t);
2219 if(opcode2[i]>=0x2c&&opcode2[i]<=0x2f) { // DADD/DADDU/DSUB/DSUBU
2221 signed char s1l,s2l,s1h,s2h,tl,th;
2222 tl=get_reg(i_regs->regmap,rt1[i]);
2223 th=get_reg(i_regs->regmap,rt1[i]|64);
2225 s1l=get_reg(i_regs->regmap,rs1[i]);
2226 s2l=get_reg(i_regs->regmap,rs2[i]);
2227 s1h=get_reg(i_regs->regmap,rs1[i]|64);
2228 s2h=get_reg(i_regs->regmap,rs2[i]|64);
2229 if(rs1[i]&&rs2[i]) {
2232 if(opcode2[i]&2) emit_subs(s1l,s2l,tl);
2233 else emit_adds(s1l,s2l,tl);
2235 #ifdef INVERTED_CARRY
2236 if(opcode2[i]&2) {if(s1h!=th) emit_mov(s1h,th);emit_sbb(th,s2h);}
2238 if(opcode2[i]&2) emit_sbc(s1h,s2h,th);
2240 else emit_add(s1h,s2h,th);
2244 if(s1l>=0) emit_mov(s1l,tl);
2245 else emit_loadreg(rs1[i],tl);
2247 if(s1h>=0) emit_mov(s1h,th);
2248 else emit_loadreg(rs1[i]|64,th);
2253 if(opcode2[i]&2) emit_negs(s2l,tl);
2254 else emit_mov(s2l,tl);
2257 emit_loadreg(rs2[i],tl);
2258 if(opcode2[i]&2) emit_negs(tl,tl);
2261 #ifdef INVERTED_CARRY
2262 if(s2h>=0) emit_mov(s2h,th);
2263 else emit_loadreg(rs2[i]|64,th);
2265 emit_adcimm(-1,th); // x86 has inverted carry flag
2270 if(s2h>=0) emit_rscimm(s2h,0,th);
2272 emit_loadreg(rs2[i]|64,th);
2273 emit_rscimm(th,0,th);
2276 if(s2h>=0) emit_mov(s2h,th);
2277 else emit_loadreg(rs2[i]|64,th);
2284 if(th>=0) emit_zeroreg(th);
2289 if(opcode2[i]==0x2a||opcode2[i]==0x2b) { // SLT/SLTU
2291 signed char s1l,s1h,s2l,s2h,t;
2292 if(!((i_regs->was32>>rs1[i])&(i_regs->was32>>rs2[i])&1))
2294 t=get_reg(i_regs->regmap,rt1[i]);
2297 s1l=get_reg(i_regs->regmap,rs1[i]);
2298 s1h=get_reg(i_regs->regmap,rs1[i]|64);
2299 s2l=get_reg(i_regs->regmap,rs2[i]);
2300 s2h=get_reg(i_regs->regmap,rs2[i]|64);
2301 if(rs2[i]==0) // rx<r0
2304 if(opcode2[i]==0x2a) // SLT
2305 emit_shrimm(s1h,31,t);
2306 else // SLTU (unsigned can not be less than zero)
2309 else if(rs1[i]==0) // r0<rx
2312 if(opcode2[i]==0x2a) // SLT
2313 emit_set_gz64_32(s2h,s2l,t);
2314 else // SLTU (set if not zero)
2315 emit_set_nz64_32(s2h,s2l,t);
2318 assert(s1l>=0);assert(s1h>=0);
2319 assert(s2l>=0);assert(s2h>=0);
2320 if(opcode2[i]==0x2a) // SLT
2321 emit_set_if_less64_32(s1h,s1l,s2h,s2l,t);
2323 emit_set_if_carry64_32(s1h,s1l,s2h,s2l,t);
2327 t=get_reg(i_regs->regmap,rt1[i]);
2330 s1l=get_reg(i_regs->regmap,rs1[i]);
2331 s2l=get_reg(i_regs->regmap,rs2[i]);
2332 if(rs2[i]==0) // rx<r0
2335 if(opcode2[i]==0x2a) // SLT
2336 emit_shrimm(s1l,31,t);
2337 else // SLTU (unsigned can not be less than zero)
2340 else if(rs1[i]==0) // r0<rx
2343 if(opcode2[i]==0x2a) // SLT
2344 emit_set_gz32(s2l,t);
2345 else // SLTU (set if not zero)
2346 emit_set_nz32(s2l,t);
2349 assert(s1l>=0);assert(s2l>=0);
2350 if(opcode2[i]==0x2a) // SLT
2351 emit_set_if_less32(s1l,s2l,t);
2353 emit_set_if_carry32(s1l,s2l,t);
2359 if(opcode2[i]>=0x24&&opcode2[i]<=0x27) { // AND/OR/XOR/NOR
2361 signed char s1l,s1h,s2l,s2h,th,tl;
2362 tl=get_reg(i_regs->regmap,rt1[i]);
2363 th=get_reg(i_regs->regmap,rt1[i]|64);
2364 if(!((i_regs->was32>>rs1[i])&(i_regs->was32>>rs2[i])&1)&&th>=0)
2368 s1l=get_reg(i_regs->regmap,rs1[i]);
2369 s1h=get_reg(i_regs->regmap,rs1[i]|64);
2370 s2l=get_reg(i_regs->regmap,rs2[i]);
2371 s2h=get_reg(i_regs->regmap,rs2[i]|64);
2372 if(rs1[i]&&rs2[i]) {
2373 assert(s1l>=0);assert(s1h>=0);
2374 assert(s2l>=0);assert(s2h>=0);
2375 if(opcode2[i]==0x24) { // AND
2376 emit_and(s1l,s2l,tl);
2377 emit_and(s1h,s2h,th);
2379 if(opcode2[i]==0x25) { // OR
2380 emit_or(s1l,s2l,tl);
2381 emit_or(s1h,s2h,th);
2383 if(opcode2[i]==0x26) { // XOR
2384 emit_xor(s1l,s2l,tl);
2385 emit_xor(s1h,s2h,th);
2387 if(opcode2[i]==0x27) { // NOR
2388 emit_or(s1l,s2l,tl);
2389 emit_or(s1h,s2h,th);
2396 if(opcode2[i]==0x24) { // AND
2400 if(opcode2[i]==0x25||opcode2[i]==0x26) { // OR/XOR
2402 if(s1l>=0) emit_mov(s1l,tl);
2403 else emit_loadreg(rs1[i],tl);
2404 if(s1h>=0) emit_mov(s1h,th);
2405 else emit_loadreg(rs1[i]|64,th);
2409 if(s2l>=0) emit_mov(s2l,tl);
2410 else emit_loadreg(rs2[i],tl);
2411 if(s2h>=0) emit_mov(s2h,th);
2412 else emit_loadreg(rs2[i]|64,th);
2419 if(opcode2[i]==0x27) { // NOR
2421 if(s1l>=0) emit_not(s1l,tl);
2423 emit_loadreg(rs1[i],tl);
2426 if(s1h>=0) emit_not(s1h,th);
2428 emit_loadreg(rs1[i]|64,th);
2434 if(s2l>=0) emit_not(s2l,tl);
2436 emit_loadreg(rs2[i],tl);
2439 if(s2h>=0) emit_not(s2h,th);
2441 emit_loadreg(rs2[i]|64,th);
2457 s1l=get_reg(i_regs->regmap,rs1[i]);
2458 s2l=get_reg(i_regs->regmap,rs2[i]);
2459 if(rs1[i]&&rs2[i]) {
2462 if(opcode2[i]==0x24) { // AND
2463 emit_and(s1l,s2l,tl);
2465 if(opcode2[i]==0x25) { // OR
2466 emit_or(s1l,s2l,tl);
2468 if(opcode2[i]==0x26) { // XOR
2469 emit_xor(s1l,s2l,tl);
2471 if(opcode2[i]==0x27) { // NOR
2472 emit_or(s1l,s2l,tl);
2478 if(opcode2[i]==0x24) { // AND
2481 if(opcode2[i]==0x25||opcode2[i]==0x26) { // OR/XOR
2483 if(s1l>=0) emit_mov(s1l,tl);
2484 else emit_loadreg(rs1[i],tl); // CHECK: regmap_entry?
2488 if(s2l>=0) emit_mov(s2l,tl);
2489 else emit_loadreg(rs2[i],tl); // CHECK: regmap_entry?
2491 else emit_zeroreg(tl);
2493 if(opcode2[i]==0x27) { // NOR
2495 if(s1l>=0) emit_not(s1l,tl);
2497 emit_loadreg(rs1[i],tl);
2503 if(s2l>=0) emit_not(s2l,tl);
2505 emit_loadreg(rs2[i],tl);
2509 else emit_movimm(-1,tl);
2518 void imm16_assemble(int i,struct regstat *i_regs)
2520 if (opcode[i]==0x0f) { // LUI
2523 t=get_reg(i_regs->regmap,rt1[i]);
2526 if(!((i_regs->isconst>>t)&1))
2527 emit_movimm(imm[i]<<16,t);
2531 if(opcode[i]==0x08||opcode[i]==0x09) { // ADDI/ADDIU
2534 t=get_reg(i_regs->regmap,rt1[i]);
2535 s=get_reg(i_regs->regmap,rs1[i]);
2540 if(!((i_regs->isconst>>t)&1)) {
2542 if(i_regs->regmap_entry[t]!=rs1[i]) emit_loadreg(rs1[i],t);
2543 emit_addimm(t,imm[i],t);
2545 if(!((i_regs->wasconst>>s)&1))
2546 emit_addimm(s,imm[i],t);
2548 emit_movimm(constmap[i][s]+imm[i],t);
2554 if(!((i_regs->isconst>>t)&1))
2555 emit_movimm(imm[i],t);
2560 if(opcode[i]==0x18||opcode[i]==0x19) { // DADDI/DADDIU
2562 signed char sh,sl,th,tl;
2563 th=get_reg(i_regs->regmap,rt1[i]|64);
2564 tl=get_reg(i_regs->regmap,rt1[i]);
2565 sh=get_reg(i_regs->regmap,rs1[i]|64);
2566 sl=get_reg(i_regs->regmap,rs1[i]);
2572 emit_addimm64_32(sh,sl,imm[i],th,tl);
2575 emit_addimm(sl,imm[i],tl);
2578 emit_movimm(imm[i],tl);
2579 if(th>=0) emit_movimm(((signed int)imm[i])>>31,th);
2584 else if(opcode[i]==0x0a||opcode[i]==0x0b) { // SLTI/SLTIU
2586 //assert(rs1[i]!=0); // r0 might be valid, but it's probably a bug
2587 signed char sh,sl,t;
2588 t=get_reg(i_regs->regmap,rt1[i]);
2589 sh=get_reg(i_regs->regmap,rs1[i]|64);
2590 sl=get_reg(i_regs->regmap,rs1[i]);
2594 if(sh<0) assert((i_regs->was32>>rs1[i])&1);
2595 if(sh<0||((i_regs->was32>>rs1[i])&1)) {
2596 if(opcode[i]==0x0a) { // SLTI
2598 if(i_regs->regmap_entry[t]!=rs1[i]) emit_loadreg(rs1[i],t);
2599 emit_slti32(t,imm[i],t);
2601 emit_slti32(sl,imm[i],t);
2606 if(i_regs->regmap_entry[t]!=rs1[i]) emit_loadreg(rs1[i],t);
2607 emit_sltiu32(t,imm[i],t);
2609 emit_sltiu32(sl,imm[i],t);
2614 if(opcode[i]==0x0a) // SLTI
2615 emit_slti64_32(sh,sl,imm[i],t);
2617 emit_sltiu64_32(sh,sl,imm[i],t);
2620 // SLTI(U) with r0 is just stupid,
2621 // nonetheless examples can be found
2622 if(opcode[i]==0x0a) // SLTI
2623 if(0<imm[i]) emit_movimm(1,t);
2624 else emit_zeroreg(t);
2627 if(imm[i]) emit_movimm(1,t);
2628 else emit_zeroreg(t);
2634 else if(opcode[i]>=0x0c&&opcode[i]<=0x0e) { // ANDI/ORI/XORI
2636 signed char sh,sl,th,tl;
2637 th=get_reg(i_regs->regmap,rt1[i]|64);
2638 tl=get_reg(i_regs->regmap,rt1[i]);
2639 sh=get_reg(i_regs->regmap,rs1[i]|64);
2640 sl=get_reg(i_regs->regmap,rs1[i]);
2641 if(tl>=0 && !((i_regs->isconst>>tl)&1)) {
2642 if(opcode[i]==0x0c) //ANDI
2646 if(i_regs->regmap_entry[tl]!=rs1[i]) emit_loadreg(rs1[i],tl);
2647 emit_andimm(tl,imm[i],tl);
2649 if(!((i_regs->wasconst>>sl)&1))
2650 emit_andimm(sl,imm[i],tl);
2652 emit_movimm(constmap[i][sl]&imm[i],tl);
2657 if(th>=0) emit_zeroreg(th);
2663 if(i_regs->regmap_entry[tl]!=rs1[i]) emit_loadreg(rs1[i],tl);
2667 emit_loadreg(rs1[i]|64,th);
2672 if(opcode[i]==0x0d) //ORI
2674 emit_orimm(tl,imm[i],tl);
2676 if(!((i_regs->wasconst>>sl)&1))
2677 emit_orimm(sl,imm[i],tl);
2679 emit_movimm(constmap[i][sl]|imm[i],tl);
2681 if(opcode[i]==0x0e) //XORI
2683 emit_xorimm(tl,imm[i],tl);
2685 if(!((i_regs->wasconst>>sl)&1))
2686 emit_xorimm(sl,imm[i],tl);
2688 emit_movimm(constmap[i][sl]^imm[i],tl);
2692 emit_movimm(imm[i],tl);
2693 if(th>=0) emit_zeroreg(th);
2701 void shiftimm_assemble(int i,struct regstat *i_regs)
2703 if(opcode2[i]<=0x3) // SLL/SRL/SRA
2707 t=get_reg(i_regs->regmap,rt1[i]);
2708 s=get_reg(i_regs->regmap,rs1[i]);
2710 if(t>=0&&!((i_regs->isconst>>t)&1)){
2717 if(s<0&&i_regs->regmap_entry[t]!=rs1[i]) emit_loadreg(rs1[i],t);
2719 if(opcode2[i]==0) // SLL
2721 emit_shlimm(s<0?t:s,imm[i],t);
2723 if(opcode2[i]==2) // SRL
2725 emit_shrimm(s<0?t:s,imm[i],t);
2727 if(opcode2[i]==3) // SRA
2729 emit_sarimm(s<0?t:s,imm[i],t);
2733 if(s>=0 && s!=t) emit_mov(s,t);
2737 //emit_storereg(rt1[i],t); //DEBUG
2740 if(opcode2[i]>=0x38&&opcode2[i]<=0x3b) // DSLL/DSRL/DSRA
2743 signed char sh,sl,th,tl;
2744 th=get_reg(i_regs->regmap,rt1[i]|64);
2745 tl=get_reg(i_regs->regmap,rt1[i]);
2746 sh=get_reg(i_regs->regmap,rs1[i]|64);
2747 sl=get_reg(i_regs->regmap,rs1[i]);
2752 if(th>=0) emit_zeroreg(th);
2759 if(opcode2[i]==0x38) // DSLL
2761 if(th>=0) emit_shldimm(sh,sl,imm[i],th);
2762 emit_shlimm(sl,imm[i],tl);
2764 if(opcode2[i]==0x3a) // DSRL
2766 emit_shrdimm(sl,sh,imm[i],tl);
2767 if(th>=0) emit_shrimm(sh,imm[i],th);
2769 if(opcode2[i]==0x3b) // DSRA
2771 emit_shrdimm(sl,sh,imm[i],tl);
2772 if(th>=0) emit_sarimm(sh,imm[i],th);
2776 if(sl!=tl) emit_mov(sl,tl);
2777 if(th>=0&&sh!=th) emit_mov(sh,th);
2783 if(opcode2[i]==0x3c) // DSLL32
2786 signed char sl,tl,th;
2787 tl=get_reg(i_regs->regmap,rt1[i]);
2788 th=get_reg(i_regs->regmap,rt1[i]|64);
2789 sl=get_reg(i_regs->regmap,rs1[i]);
2798 emit_shlimm(th,imm[i]&31,th);
2803 if(opcode2[i]==0x3e) // DSRL32
2806 signed char sh,tl,th;
2807 tl=get_reg(i_regs->regmap,rt1[i]);
2808 th=get_reg(i_regs->regmap,rt1[i]|64);
2809 sh=get_reg(i_regs->regmap,rs1[i]|64);
2813 if(th>=0) emit_zeroreg(th);
2816 emit_shrimm(tl,imm[i]&31,tl);
2821 if(opcode2[i]==0x3f) // DSRA32
2825 tl=get_reg(i_regs->regmap,rt1[i]);
2826 sh=get_reg(i_regs->regmap,rs1[i]|64);
2832 emit_sarimm(tl,imm[i]&31,tl);
2839 #ifndef shift_assemble
2840 void shift_assemble(int i,struct regstat *i_regs)
2842 printf("Need shift_assemble for this architecture.\n");
2847 void load_assemble(int i,struct regstat *i_regs)
2849 int s,th,tl,addr,map=-1;
2852 int memtarget=0,c=0;
2853 int fastload_reg_override=0;
2855 th=get_reg(i_regs->regmap,rt1[i]|64);
2856 tl=get_reg(i_regs->regmap,rt1[i]);
2857 s=get_reg(i_regs->regmap,rs1[i]);
2859 for(hr=0;hr<HOST_REGS;hr++) {
2860 if(i_regs->regmap[hr]>=0) reglist|=1<<hr;
2862 if(i_regs->regmap[HOST_CCREG]==CCREG) reglist&=~(1<<HOST_CCREG);
2864 c=(i_regs->wasconst>>s)&1;
2866 memtarget=((signed int)(constmap[i][s]+offset))<(signed int)0x80000000+RAM_SIZE;
2867 if(using_tlb&&((signed int)(constmap[i][s]+offset))>=(signed int)0xC0000000) memtarget=1;
2870 //printf("load_assemble: c=%d\n",c);
2871 //if(c) printf("load_assemble: const=%x\n",(int)constmap[i][s]+offset);
2872 // FIXME: Even if the load is a NOP, we should check for pagefaults...
2874 if(tl<0&&(!c||(((u_int)constmap[i][s]+offset)>>16)==0x1f80)
2876 // could be FIFO, must perform the read
2878 assem_debug("(forced read)\n");
2879 tl=get_reg(i_regs->regmap,-1);
2883 if(offset||s<0||c) addr=tl;
2885 //if(tl<0) tl=get_reg(i_regs->regmap,-1);
2887 //printf("load_assemble: c=%d\n",c);
2888 //if(c) printf("load_assemble: const=%x\n",(int)constmap[i][s]+offset);
2889 assert(tl>=0); // Even if the load is a NOP, we must check for pagefaults and I/O
2891 if(th>=0) reglist&=~(1<<th);
2895 map=get_reg(i_regs->regmap,ROREG);
2896 if(map<0) emit_loadreg(ROREG,map=HOST_TEMPREG);
2898 //#define R29_HACK 1
2900 // Strmnnrmn's speed hack
2901 if(rs1[i]!=29||start<0x80001000||start>=0x80000000+RAM_SIZE)
2904 jaddr=emit_fastpath_cmp_jump(i,addr,&fastload_reg_override);
2909 if (opcode[i]==0x20||opcode[i]==0x24) x=3; // LB/LBU
2910 if (opcode[i]==0x21||opcode[i]==0x25) x=2; // LH/LHU
2911 map=get_reg(i_regs->regmap,TLREG);
2914 map=do_tlb_r(addr,tl,map,x,-1,-1,c,constmap[i][s]+offset);
2915 do_tlb_r_branch(map,c,constmap[i][s]+offset,&jaddr);
2917 int dummy=(rt1[i]==0)||(tl!=get_reg(i_regs->regmap,rt1[i])); // ignore loads to r0 and unneeded reg
2918 if (opcode[i]==0x20) { // LB
2921 #ifdef HOST_IMM_ADDR32
2923 emit_movsbl_tlb((constmap[i][s]+offset)^3,map,tl);
2927 //emit_xorimm(addr,3,tl);
2928 //gen_tlb_addr_r(tl,map);
2929 //emit_movsbl_indexed((int)rdram-0x80000000,tl,tl);
2931 #ifdef BIG_ENDIAN_MIPS
2932 if(!c) emit_xorimm(addr,3,tl);
2933 else x=((constmap[i][s]+offset)^3)-(constmap[i][s]+offset);
2937 if(fastload_reg_override) a=fastload_reg_override;
2939 emit_movsbl_indexed_tlb(x,a,map,tl);
2943 add_stub(LOADB_STUB,jaddr,(int)out,i,addr,(int)i_regs,ccadj[i],reglist);
2946 inline_readstub(LOADB_STUB,i,constmap[i][s]+offset,i_regs->regmap,rt1[i],ccadj[i],reglist);
2948 if (opcode[i]==0x21) { // LH
2951 #ifdef HOST_IMM_ADDR32
2953 emit_movswl_tlb((constmap[i][s]+offset)^2,map,tl);
2958 #ifdef BIG_ENDIAN_MIPS
2959 if(!c) emit_xorimm(addr,2,tl);
2960 else x=((constmap[i][s]+offset)^2)-(constmap[i][s]+offset);
2964 if(fastload_reg_override) a=fastload_reg_override;
2966 //emit_movswl_indexed_tlb(x,tl,map,tl);
2969 gen_tlb_addr_r(a,map);
2970 emit_movswl_indexed(x,a,tl);
2973 emit_movswl_indexed(x,a,tl);
2975 emit_movswl_indexed((int)rdram-0x80000000+x,a,tl);
2981 add_stub(LOADH_STUB,jaddr,(int)out,i,addr,(int)i_regs,ccadj[i],reglist);
2984 inline_readstub(LOADH_STUB,i,constmap[i][s]+offset,i_regs->regmap,rt1[i],ccadj[i],reglist);
2986 if (opcode[i]==0x23) { // LW
2990 if(fastload_reg_override) a=fastload_reg_override;
2991 //emit_readword_indexed((int)rdram-0x80000000,addr,tl);
2992 #ifdef HOST_IMM_ADDR32
2994 emit_readword_tlb(constmap[i][s]+offset,map,tl);
2997 emit_readword_indexed_tlb(0,a,map,tl);
3000 add_stub(LOADW_STUB,jaddr,(int)out,i,addr,(int)i_regs,ccadj[i],reglist);
3003 inline_readstub(LOADW_STUB,i,constmap[i][s]+offset,i_regs->regmap,rt1[i],ccadj[i],reglist);
3005 if (opcode[i]==0x24) { // LBU
3008 #ifdef HOST_IMM_ADDR32
3010 emit_movzbl_tlb((constmap[i][s]+offset)^3,map,tl);
3014 //emit_xorimm(addr,3,tl);
3015 //gen_tlb_addr_r(tl,map);
3016 //emit_movzbl_indexed((int)rdram-0x80000000,tl,tl);
3018 #ifdef BIG_ENDIAN_MIPS
3019 if(!c) emit_xorimm(addr,3,tl);
3020 else x=((constmap[i][s]+offset)^3)-(constmap[i][s]+offset);
3024 if(fastload_reg_override) a=fastload_reg_override;
3026 emit_movzbl_indexed_tlb(x,a,map,tl);
3030 add_stub(LOADBU_STUB,jaddr,(int)out,i,addr,(int)i_regs,ccadj[i],reglist);
3033 inline_readstub(LOADBU_STUB,i,constmap[i][s]+offset,i_regs->regmap,rt1[i],ccadj[i],reglist);
3035 if (opcode[i]==0x25) { // LHU
3038 #ifdef HOST_IMM_ADDR32
3040 emit_movzwl_tlb((constmap[i][s]+offset)^2,map,tl);
3045 #ifdef BIG_ENDIAN_MIPS
3046 if(!c) emit_xorimm(addr,2,tl);
3047 else x=((constmap[i][s]+offset)^2)-(constmap[i][s]+offset);
3051 if(fastload_reg_override) a=fastload_reg_override;
3053 //emit_movzwl_indexed_tlb(x,tl,map,tl);
3056 gen_tlb_addr_r(a,map);
3057 emit_movzwl_indexed(x,a,tl);
3060 emit_movzwl_indexed(x,a,tl);
3062 emit_movzwl_indexed((int)rdram-0x80000000+x,a,tl);
3068 add_stub(LOADHU_STUB,jaddr,(int)out,i,addr,(int)i_regs,ccadj[i],reglist);
3071 inline_readstub(LOADHU_STUB,i,constmap[i][s]+offset,i_regs->regmap,rt1[i],ccadj[i],reglist);
3073 if (opcode[i]==0x27) { // LWU
3078 if(fastload_reg_override) a=fastload_reg_override;
3079 //emit_readword_indexed((int)rdram-0x80000000,addr,tl);
3080 #ifdef HOST_IMM_ADDR32
3082 emit_readword_tlb(constmap[i][s]+offset,map,tl);
3085 emit_readword_indexed_tlb(0,a,map,tl);
3088 add_stub(LOADW_STUB,jaddr,(int)out,i,addr,(int)i_regs,ccadj[i],reglist);
3091 inline_readstub(LOADW_STUB,i,constmap[i][s]+offset,i_regs->regmap,rt1[i],ccadj[i],reglist);
3095 if (opcode[i]==0x37) { // LD
3099 if(fastload_reg_override) a=fastload_reg_override;
3100 //gen_tlb_addr_r(tl,map);
3101 //if(th>=0) emit_readword_indexed((int)rdram-0x80000000,addr,th);
3102 //emit_readword_indexed((int)rdram-0x7FFFFFFC,addr,tl);
3103 #ifdef HOST_IMM_ADDR32
3105 emit_readdword_tlb(constmap[i][s]+offset,map,th,tl);
3108 emit_readdword_indexed_tlb(0,a,map,th,tl);
3111 add_stub(LOADD_STUB,jaddr,(int)out,i,addr,(int)i_regs,ccadj[i],reglist);
3114 inline_readstub(LOADD_STUB,i,constmap[i][s]+offset,i_regs->regmap,rt1[i],ccadj[i],reglist);
3117 //emit_storereg(rt1[i],tl); // DEBUG
3118 //if(opcode[i]==0x23)
3119 //if(opcode[i]==0x24)
3120 //if(opcode[i]==0x23||opcode[i]==0x24)
3121 /*if(opcode[i]==0x21||opcode[i]==0x23||opcode[i]==0x24)
3125 emit_readword((int)&last_count,ECX);
3127 if(get_reg(i_regs->regmap,CCREG)<0)
3128 emit_loadreg(CCREG,HOST_CCREG);
3129 emit_add(HOST_CCREG,ECX,HOST_CCREG);
3130 emit_addimm(HOST_CCREG,2*ccadj[i],HOST_CCREG);
3131 emit_writeword(HOST_CCREG,(int)&Count);
3134 if(get_reg(i_regs->regmap,CCREG)<0)
3135 emit_loadreg(CCREG,0);
3137 emit_mov(HOST_CCREG,0);
3139 emit_addimm(0,2*ccadj[i],0);
3140 emit_writeword(0,(int)&Count);
3142 emit_call((int)memdebug);
3144 restore_regs(0x100f);
3148 #ifndef loadlr_assemble
3149 void loadlr_assemble(int i,struct regstat *i_regs)
3151 printf("Need loadlr_assemble for this architecture.\n");
3156 void store_assemble(int i,struct regstat *i_regs)
3161 int jaddr=0,jaddr2,type;
3162 int memtarget=0,c=0;
3163 int agr=AGEN1+(i&1);
3164 int faststore_reg_override=0;
3166 th=get_reg(i_regs->regmap,rs2[i]|64);
3167 tl=get_reg(i_regs->regmap,rs2[i]);
3168 s=get_reg(i_regs->regmap,rs1[i]);
3169 temp=get_reg(i_regs->regmap,agr);
3170 if(temp<0) temp=get_reg(i_regs->regmap,-1);
3173 c=(i_regs->wasconst>>s)&1;
3175 memtarget=((signed int)(constmap[i][s]+offset))<(signed int)0x80000000+RAM_SIZE;
3176 if(using_tlb&&((signed int)(constmap[i][s]+offset))>=(signed int)0xC0000000) memtarget=1;
3181 for(hr=0;hr<HOST_REGS;hr++) {
3182 if(i_regs->regmap[hr]>=0) reglist|=1<<hr;
3184 if(i_regs->regmap[HOST_CCREG]==CCREG) reglist&=~(1<<HOST_CCREG);
3185 if(offset||s<0||c) addr=temp;
3191 // Strmnnrmn's speed hack
3192 if(rs1[i]!=29||start<0x80001000||start>=0x80000000+RAM_SIZE)
3194 emit_cmpimm(addr,RAM_SIZE);
3195 #ifdef DESTRUCTIVE_SHIFT
3196 if(s==addr) emit_mov(s,temp);
3200 if(rs1[i]!=29||start<0x80001000||start>=0x80000000+RAM_SIZE)
3204 #ifdef CORTEX_A8_BRANCH_PREDICTION_HACK
3205 // Hint to branch predictor that the branch is unlikely to be taken
3207 emit_jno_unlikely(0);
3213 jaddr=emit_fastpath_cmp_jump(i,addr,&faststore_reg_override);
3218 if (opcode[i]==0x28) x=3; // SB
3219 if (opcode[i]==0x29) x=2; // SH
3220 map=get_reg(i_regs->regmap,TLREG);
3223 map=do_tlb_w(addr,temp,map,x,c,constmap[i][s]+offset);
3224 do_tlb_w_branch(map,c,constmap[i][s]+offset,&jaddr);
3227 if (opcode[i]==0x28) { // SB
3230 #ifdef BIG_ENDIAN_MIPS
3231 if(!c) emit_xorimm(addr,3,temp);
3232 else x=((constmap[i][s]+offset)^3)-(constmap[i][s]+offset);
3236 if(faststore_reg_override) a=faststore_reg_override;
3237 //gen_tlb_addr_w(temp,map);
3238 //emit_writebyte_indexed(tl,(int)rdram-0x80000000,temp);
3239 emit_writebyte_indexed_tlb(tl,x,a,map,a);
3243 if (opcode[i]==0x29) { // SH
3246 #ifdef BIG_ENDIAN_MIPS
3247 if(!c) emit_xorimm(addr,2,temp);
3248 else x=((constmap[i][s]+offset)^2)-(constmap[i][s]+offset);
3252 if(faststore_reg_override) a=faststore_reg_override;
3254 //emit_writehword_indexed_tlb(tl,x,temp,map,temp);
3257 gen_tlb_addr_w(a,map);
3258 emit_writehword_indexed(tl,x,a);
3260 emit_writehword_indexed(tl,(int)rdram-0x80000000+x,a);
3264 if (opcode[i]==0x2B) { // SW
3267 if(faststore_reg_override) a=faststore_reg_override;
3268 //emit_writeword_indexed(tl,(int)rdram-0x80000000,addr);
3269 emit_writeword_indexed_tlb(tl,0,a,map,temp);
3273 if (opcode[i]==0x3F) { // SD
3276 if(faststore_reg_override) a=faststore_reg_override;
3279 //emit_writeword_indexed(th,(int)rdram-0x80000000,addr);
3280 //emit_writeword_indexed(tl,(int)rdram-0x7FFFFFFC,addr);
3281 emit_writedword_indexed_tlb(th,tl,0,a,map,temp);
3284 //emit_writeword_indexed(tl,(int)rdram-0x80000000,temp);
3285 //emit_writeword_indexed(tl,(int)rdram-0x7FFFFFFC,temp);
3286 emit_writedword_indexed_tlb(tl,tl,0,a,map,temp);
3293 // PCSX store handlers don't check invcode again
3295 add_stub(type,jaddr,(int)out,i,addr,(int)i_regs,ccadj[i],reglist);
3301 #ifdef DESTRUCTIVE_SHIFT
3302 // The x86 shift operation is 'destructive'; it overwrites the
3303 // source register, so we need to make a copy first and use that.
3306 #if defined(HOST_IMM8)
3307 int ir=get_reg(i_regs->regmap,INVCP);
3309 emit_cmpmem_indexedsr12_reg(ir,addr,1);
3311 emit_cmpmem_indexedsr12_imm((int)invalid_code,addr,1);
3313 #if defined(HAVE_CONDITIONAL_CALL) && !defined(DESTRUCTIVE_SHIFT)
3314 emit_callne(invalidate_addr_reg[addr]);
3318 add_stub(INVCODE_STUB,jaddr2,(int)out,reglist|(1<<HOST_CCREG),addr,0,0,0);
3323 add_stub(type,jaddr,(int)out,i,addr,(int)i_regs,ccadj[i],reglist);
3324 } else if(c&&!memtarget) {
3325 inline_writestub(type,i,constmap[i][s]+offset,i_regs->regmap,rs2[i],ccadj[i],reglist);
3327 //if(opcode[i]==0x2B || opcode[i]==0x3F)
3328 //if(opcode[i]==0x2B || opcode[i]==0x28)
3329 //if(opcode[i]==0x2B || opcode[i]==0x29)
3330 //if(opcode[i]==0x2B)
3331 /*if(opcode[i]==0x2B || opcode[i]==0x28 || opcode[i]==0x29 || opcode[i]==0x3F)
3339 emit_readword((int)&last_count,ECX);
3341 if(get_reg(i_regs->regmap,CCREG)<0)
3342 emit_loadreg(CCREG,HOST_CCREG);
3343 emit_add(HOST_CCREG,ECX,HOST_CCREG);
3344 emit_addimm(HOST_CCREG,2*ccadj[i],HOST_CCREG);
3345 emit_writeword(HOST_CCREG,(int)&Count);
3348 if(get_reg(i_regs->regmap,CCREG)<0)
3349 emit_loadreg(CCREG,0);
3351 emit_mov(HOST_CCREG,0);
3353 emit_addimm(0,2*ccadj[i],0);
3354 emit_writeword(0,(int)&Count);
3356 emit_call((int)memdebug);
3361 restore_regs(0x100f);
3366 void storelr_assemble(int i,struct regstat *i_regs)
3373 int case1,case2,case3;
3374 int done0,done1,done2;
3375 int memtarget=0,c=0;
3376 int agr=AGEN1+(i&1);
3378 th=get_reg(i_regs->regmap,rs2[i]|64);
3379 tl=get_reg(i_regs->regmap,rs2[i]);
3380 s=get_reg(i_regs->regmap,rs1[i]);
3381 temp=get_reg(i_regs->regmap,agr);
3382 if(temp<0) temp=get_reg(i_regs->regmap,-1);
3385 c=(i_regs->isconst>>s)&1;
3387 memtarget=((signed int)(constmap[i][s]+offset))<(signed int)0x80000000+RAM_SIZE;
3388 if(using_tlb&&((signed int)(constmap[i][s]+offset))>=(signed int)0xC0000000) memtarget=1;
3392 for(hr=0;hr<HOST_REGS;hr++) {
3393 if(i_regs->regmap[hr]>=0) reglist|=1<<hr;
3398 emit_cmpimm(s<0||offset?temp:s,RAM_SIZE);
3399 if(!offset&&s!=temp) emit_mov(s,temp);
3405 if(!memtarget||!rs1[i]) {
3411 int map=get_reg(i_regs->regmap,ROREG);
3412 if(map<0) emit_loadreg(ROREG,map=HOST_TEMPREG);
3413 gen_tlb_addr_w(temp,map);
3415 if((u_int)rdram!=0x80000000)
3416 emit_addimm_no_flags((u_int)rdram-(u_int)0x80000000,temp);
3419 int map=get_reg(i_regs->regmap,TLREG);
3422 map=do_tlb_w(c||s<0||offset?temp:s,temp,map,0,c,constmap[i][s]+offset);
3423 if(!c&&!offset&&s>=0) emit_mov(s,temp);
3424 do_tlb_w_branch(map,c,constmap[i][s]+offset,&jaddr);
3425 if(!jaddr&&!memtarget) {
3429 gen_tlb_addr_w(temp,map);
3432 if (opcode[i]==0x2C||opcode[i]==0x2D) { // SDL/SDR
3433 temp2=get_reg(i_regs->regmap,FTEMP);
3434 if(!rs2[i]) temp2=th=tl;
3437 #ifndef BIG_ENDIAN_MIPS
3438 emit_xorimm(temp,3,temp);
3440 emit_testimm(temp,2);
3443 emit_testimm(temp,1);
3447 if (opcode[i]==0x2A) { // SWL
3448 emit_writeword_indexed(tl,0,temp);
3450 if (opcode[i]==0x2E) { // SWR
3451 emit_writebyte_indexed(tl,3,temp);
3453 if (opcode[i]==0x2C) { // SDL
3454 emit_writeword_indexed(th,0,temp);
3455 if(rs2[i]) emit_mov(tl,temp2);
3457 if (opcode[i]==0x2D) { // SDR
3458 emit_writebyte_indexed(tl,3,temp);
3459 if(rs2[i]) emit_shldimm(th,tl,24,temp2);
3464 set_jump_target(case1,(int)out);
3465 if (opcode[i]==0x2A) { // SWL
3466 // Write 3 msb into three least significant bytes
3467 if(rs2[i]) emit_rorimm(tl,8,tl);
3468 emit_writehword_indexed(tl,-1,temp);
3469 if(rs2[i]) emit_rorimm(tl,16,tl);
3470 emit_writebyte_indexed(tl,1,temp);
3471 if(rs2[i]) emit_rorimm(tl,8,tl);
3473 if (opcode[i]==0x2E) { // SWR
3474 // Write two lsb into two most significant bytes
3475 emit_writehword_indexed(tl,1,temp);
3477 if (opcode[i]==0x2C) { // SDL
3478 if(rs2[i]) emit_shrdimm(tl,th,8,temp2);
3479 // Write 3 msb into three least significant bytes
3480 if(rs2[i]) emit_rorimm(th,8,th);
3481 emit_writehword_indexed(th,-1,temp);
3482 if(rs2[i]) emit_rorimm(th,16,th);
3483 emit_writebyte_indexed(th,1,temp);
3484 if(rs2[i]) emit_rorimm(th,8,th);
3486 if (opcode[i]==0x2D) { // SDR
3487 if(rs2[i]) emit_shldimm(th,tl,16,temp2);
3488 // Write two lsb into two most significant bytes
3489 emit_writehword_indexed(tl,1,temp);
3494 set_jump_target(case2,(int)out);
3495 emit_testimm(temp,1);
3498 if (opcode[i]==0x2A) { // SWL
3499 // Write two msb into two least significant bytes
3500 if(rs2[i]) emit_rorimm(tl,16,tl);
3501 emit_writehword_indexed(tl,-2,temp);
3502 if(rs2[i]) emit_rorimm(tl,16,tl);
3504 if (opcode[i]==0x2E) { // SWR
3505 // Write 3 lsb into three most significant bytes
3506 emit_writebyte_indexed(tl,-1,temp);
3507 if(rs2[i]) emit_rorimm(tl,8,tl);
3508 emit_writehword_indexed(tl,0,temp);
3509 if(rs2[i]) emit_rorimm(tl,24,tl);
3511 if (opcode[i]==0x2C) { // SDL
3512 if(rs2[i]) emit_shrdimm(tl,th,16,temp2);
3513 // Write two msb into two least significant bytes
3514 if(rs2[i]) emit_rorimm(th,16,th);
3515 emit_writehword_indexed(th,-2,temp);
3516 if(rs2[i]) emit_rorimm(th,16,th);
3518 if (opcode[i]==0x2D) { // SDR
3519 if(rs2[i]) emit_shldimm(th,tl,8,temp2);
3520 // Write 3 lsb into three most significant bytes
3521 emit_writebyte_indexed(tl,-1,temp);
3522 if(rs2[i]) emit_rorimm(tl,8,tl);
3523 emit_writehword_indexed(tl,0,temp);
3524 if(rs2[i]) emit_rorimm(tl,24,tl);
3529 set_jump_target(case3,(int)out);
3530 if (opcode[i]==0x2A) { // SWL
3531 // Write msb into least significant byte
3532 if(rs2[i]) emit_rorimm(tl,24,tl);
3533 emit_writebyte_indexed(tl,-3,temp);
3534 if(rs2[i]) emit_rorimm(tl,8,tl);
3536 if (opcode[i]==0x2E) { // SWR
3537 // Write entire word
3538 emit_writeword_indexed(tl,-3,temp);
3540 if (opcode[i]==0x2C) { // SDL
3541 if(rs2[i]) emit_shrdimm(tl,th,24,temp2);
3542 // Write msb into least significant byte
3543 if(rs2[i]) emit_rorimm(th,24,th);
3544 emit_writebyte_indexed(th,-3,temp);
3545 if(rs2[i]) emit_rorimm(th,8,th);
3547 if (opcode[i]==0x2D) { // SDR
3548 if(rs2[i]) emit_mov(th,temp2);
3549 // Write entire word
3550 emit_writeword_indexed(tl,-3,temp);
3552 set_jump_target(done0,(int)out);
3553 set_jump_target(done1,(int)out);
3554 set_jump_target(done2,(int)out);
3555 if (opcode[i]==0x2C) { // SDL
3556 emit_testimm(temp,4);
3559 emit_andimm(temp,~3,temp);
3560 emit_writeword_indexed(temp2,4,temp);
3561 set_jump_target(done0,(int)out);
3563 if (opcode[i]==0x2D) { // SDR
3564 emit_testimm(temp,4);
3567 emit_andimm(temp,~3,temp);
3568 emit_writeword_indexed(temp2,-4,temp);
3569 set_jump_target(done0,(int)out);
3572 add_stub(STORELR_STUB,jaddr,(int)out,i,(int)i_regs,temp,ccadj[i],reglist);
3575 int map=get_reg(i_regs->regmap,ROREG);
3576 if(map<0) map=HOST_TEMPREG;
3577 gen_orig_addr_w(temp,map);
3579 emit_addimm_no_flags((u_int)0x80000000-(u_int)rdram,temp);
3581 #if defined(HOST_IMM8)
3582 int ir=get_reg(i_regs->regmap,INVCP);
3584 emit_cmpmem_indexedsr12_reg(ir,temp,1);
3586 emit_cmpmem_indexedsr12_imm((int)invalid_code,temp,1);
3588 #if defined(HAVE_CONDITIONAL_CALL) && !defined(DESTRUCTIVE_SHIFT)
3589 emit_callne(invalidate_addr_reg[temp]);
3593 add_stub(INVCODE_STUB,jaddr2,(int)out,reglist|(1<<HOST_CCREG),temp,0,0,0);
3598 //save_regs(0x100f);
3599 emit_readword((int)&last_count,ECX);
3600 if(get_reg(i_regs->regmap,CCREG)<0)
3601 emit_loadreg(CCREG,HOST_CCREG);
3602 emit_add(HOST_CCREG,ECX,HOST_CCREG);
3603 emit_addimm(HOST_CCREG,2*ccadj[i],HOST_CCREG);
3604 emit_writeword(HOST_CCREG,(int)&Count);
3605 emit_call((int)memdebug);
3607 //restore_regs(0x100f);
3611 void c1ls_assemble(int i,struct regstat *i_regs)
3613 #ifndef DISABLE_COP1
3619 int jaddr,jaddr2=0,jaddr3,type;
3620 int agr=AGEN1+(i&1);
3622 th=get_reg(i_regs->regmap,FTEMP|64);
3623 tl=get_reg(i_regs->regmap,FTEMP);
3624 s=get_reg(i_regs->regmap,rs1[i]);
3625 temp=get_reg(i_regs->regmap,agr);
3626 if(temp<0) temp=get_reg(i_regs->regmap,-1);
3631 for(hr=0;hr<HOST_REGS;hr++) {
3632 if(i_regs->regmap[hr]>=0) reglist|=1<<hr;
3634 if(i_regs->regmap[HOST_CCREG]==CCREG) reglist&=~(1<<HOST_CCREG);
3635 if (opcode[i]==0x31||opcode[i]==0x35) // LWC1/LDC1
3637 // Loads use a temporary register which we need to save
3640 if (opcode[i]==0x39||opcode[i]==0x3D) // SWC1/SDC1
3644 //if(s<0) emit_loadreg(rs1[i],ar); //address_generation does this now
3645 //else c=(i_regs->wasconst>>s)&1;
3646 if(s>=0) c=(i_regs->wasconst>>s)&1;
3647 // Check cop1 unusable
3649 signed char rs=get_reg(i_regs->regmap,CSREG);
3651 emit_testimm(rs,0x20000000);
3654 add_stub(FP_STUB,jaddr,(int)out,i,rs,(int)i_regs,is_delayslot,0);
3657 if (opcode[i]==0x39) { // SWC1 (get float address)
3658 emit_readword((int)®_cop1_simple[(source[i]>>16)&0x1f],tl);
3660 if (opcode[i]==0x3D) { // SDC1 (get double address)
3661 emit_readword((int)®_cop1_double[(source[i]>>16)&0x1f],tl);
3663 // Generate address + offset
3666 emit_cmpimm(offset||c||s<0?ar:s,RAM_SIZE);
3670 map=get_reg(i_regs->regmap,TLREG);
3673 if (opcode[i]==0x31||opcode[i]==0x35) { // LWC1/LDC1
3674 map=do_tlb_r(offset||c||s<0?ar:s,ar,map,0,-1,-1,c,constmap[i][s]+offset);
3676 if (opcode[i]==0x39||opcode[i]==0x3D) { // SWC1/SDC1
3677 map=do_tlb_w(offset||c||s<0?ar:s,ar,map,0,c,constmap[i][s]+offset);
3680 if (opcode[i]==0x39) { // SWC1 (read float)
3681 emit_readword_indexed(0,tl,tl);
3683 if (opcode[i]==0x3D) { // SDC1 (read double)
3684 emit_readword_indexed(4,tl,th);
3685 emit_readword_indexed(0,tl,tl);
3687 if (opcode[i]==0x31) { // LWC1 (get target address)
3688 emit_readword((int)®_cop1_simple[(source[i]>>16)&0x1f],temp);
3690 if (opcode[i]==0x35) { // LDC1 (get target address)
3691 emit_readword((int)®_cop1_double[(source[i]>>16)&0x1f],temp);
3698 else if(((signed int)(constmap[i][s]+offset))>=(signed int)0x80000000+RAM_SIZE) {
3700 emit_jmp(0); // inline_readstub/inline_writestub? Very rare case
3702 #ifdef DESTRUCTIVE_SHIFT
3703 if (opcode[i]==0x39||opcode[i]==0x3D) { // SWC1/SDC1
3704 if(!offset&&!c&&s>=0) emit_mov(s,ar);
3708 if (opcode[i]==0x31||opcode[i]==0x35) { // LWC1/LDC1
3709 do_tlb_r_branch(map,c,constmap[i][s]+offset,&jaddr2);
3711 if (opcode[i]==0x39||opcode[i]==0x3D) { // SWC1/SDC1
3712 do_tlb_w_branch(map,c,constmap[i][s]+offset,&jaddr2);
3715 if (opcode[i]==0x31) { // LWC1
3716 //if(s>=0&&!c&&!offset) emit_mov(s,tl);
3717 //gen_tlb_addr_r(ar,map);
3718 //emit_readword_indexed((int)rdram-0x80000000,tl,tl);
3719 #ifdef HOST_IMM_ADDR32
3720 if(c) emit_readword_tlb(constmap[i][s]+offset,map,tl);
3723 emit_readword_indexed_tlb(0,offset||c||s<0?tl:s,map,tl);
3726 if (opcode[i]==0x35) { // LDC1
3728 //if(s>=0&&!c&&!offset) emit_mov(s,tl);
3729 //gen_tlb_addr_r(ar,map);
3730 //emit_readword_indexed((int)rdram-0x80000000,tl,th);
3731 //emit_readword_indexed((int)rdram-0x7FFFFFFC,tl,tl);
3732 #ifdef HOST_IMM_ADDR32
3733 if(c) emit_readdword_tlb(constmap[i][s]+offset,map,th,tl);
3736 emit_readdword_indexed_tlb(0,offset||c||s<0?tl:s,map,th,tl);
3739 if (opcode[i]==0x39) { // SWC1
3740 //emit_writeword_indexed(tl,(int)rdram-0x80000000,temp);
3741 emit_writeword_indexed_tlb(tl,0,offset||c||s<0?temp:s,map,temp);
3744 if (opcode[i]==0x3D) { // SDC1
3746 //emit_writeword_indexed(th,(int)rdram-0x80000000,temp);
3747 //emit_writeword_indexed(tl,(int)rdram-0x7FFFFFFC,temp);
3748 emit_writedword_indexed_tlb(th,tl,0,offset||c||s<0?temp:s,map,temp);
3752 if (opcode[i]==0x39||opcode[i]==0x3D) { // SWC1/SDC1
3753 #ifndef DESTRUCTIVE_SHIFT
3754 temp=offset||c||s<0?ar:s;
3756 #if defined(HOST_IMM8)
3757 int ir=get_reg(i_regs->regmap,INVCP);
3759 emit_cmpmem_indexedsr12_reg(ir,temp,1);
3761 emit_cmpmem_indexedsr12_imm((int)invalid_code,temp,1);
3763 #if defined(HAVE_CONDITIONAL_CALL) && !defined(DESTRUCTIVE_SHIFT)
3764 emit_callne(invalidate_addr_reg[temp]);
3768 add_stub(INVCODE_STUB,jaddr3,(int)out,reglist|(1<<HOST_CCREG),temp,0,0,0);
3772 if(jaddr2) add_stub(type,jaddr2,(int)out,i,offset||c||s<0?ar:s,(int)i_regs,ccadj[i],reglist);
3773 if (opcode[i]==0x31) { // LWC1 (write float)
3774 emit_writeword_indexed(tl,0,temp);
3776 if (opcode[i]==0x35) { // LDC1 (write double)
3777 emit_writeword_indexed(th,4,temp);
3778 emit_writeword_indexed(tl,0,temp);
3780 //if(opcode[i]==0x39)
3781 /*if(opcode[i]==0x39||opcode[i]==0x31)
3784 emit_readword((int)&last_count,ECX);
3785 if(get_reg(i_regs->regmap,CCREG)<0)
3786 emit_loadreg(CCREG,HOST_CCREG);
3787 emit_add(HOST_CCREG,ECX,HOST_CCREG);
3788 emit_addimm(HOST_CCREG,2*ccadj[i],HOST_CCREG);
3789 emit_writeword(HOST_CCREG,(int)&Count);
3790 emit_call((int)memdebug);
3794 cop1_unusable(i, i_regs);
3798 void c2ls_assemble(int i,struct regstat *i_regs)
3803 int memtarget=0,c=0;
3804 int jaddr2=0,jaddr3,type;
3805 int agr=AGEN1+(i&1);
3806 int fastio_reg_override=0;
3808 u_int copr=(source[i]>>16)&0x1f;
3809 s=get_reg(i_regs->regmap,rs1[i]);
3810 tl=get_reg(i_regs->regmap,FTEMP);
3816 for(hr=0;hr<HOST_REGS;hr++) {
3817 if(i_regs->regmap[hr]>=0) reglist|=1<<hr;
3819 if(i_regs->regmap[HOST_CCREG]==CCREG)
3820 reglist&=~(1<<HOST_CCREG);
3823 if (opcode[i]==0x3a) { // SWC2
3824 ar=get_reg(i_regs->regmap,agr);
3825 if(ar<0) ar=get_reg(i_regs->regmap,-1);
3830 if(s>=0) c=(i_regs->wasconst>>s)&1;
3831 memtarget=c&&(((signed int)(constmap[i][s]+offset))<(signed int)0x80000000+RAM_SIZE);
3832 if (!offset&&!c&&s>=0) ar=s;
3835 if (opcode[i]==0x3a) { // SWC2
3836 cop2_get_dreg(copr,tl,HOST_TEMPREG);
3844 emit_jmp(0); // inline_readstub/inline_writestub?
3848 jaddr2=emit_fastpath_cmp_jump(i,ar,&fastio_reg_override);
3850 if (opcode[i]==0x32) { // LWC2
3851 #ifdef HOST_IMM_ADDR32
3852 if(c) emit_readword_tlb(constmap[i][s]+offset,-1,tl);
3856 if(fastio_reg_override) a=fastio_reg_override;
3857 emit_readword_indexed(0,a,tl);
3859 if (opcode[i]==0x3a) { // SWC2
3860 #ifdef DESTRUCTIVE_SHIFT
3861 if(!offset&&!c&&s>=0) emit_mov(s,ar);
3864 if(fastio_reg_override) a=fastio_reg_override;
3865 emit_writeword_indexed(tl,0,a);
3869 add_stub(type,jaddr2,(int)out,i,ar,(int)i_regs,ccadj[i],reglist);
3870 if (opcode[i]==0x3a) { // SWC2
3871 #if defined(HOST_IMM8)
3872 int ir=get_reg(i_regs->regmap,INVCP);
3874 emit_cmpmem_indexedsr12_reg(ir,ar,1);
3876 emit_cmpmem_indexedsr12_imm((int)invalid_code,ar,1);
3878 #if defined(HAVE_CONDITIONAL_CALL) && !defined(DESTRUCTIVE_SHIFT)
3879 emit_callne(invalidate_addr_reg[ar]);
3883 add_stub(INVCODE_STUB,jaddr3,(int)out,reglist|(1<<HOST_CCREG),ar,0,0,0);
3886 if (opcode[i]==0x32) { // LWC2
3887 cop2_put_dreg(copr,tl,HOST_TEMPREG);
3891 #ifndef multdiv_assemble
3892 void multdiv_assemble(int i,struct regstat *i_regs)
3894 printf("Need multdiv_assemble for this architecture.\n");
3899 void mov_assemble(int i,struct regstat *i_regs)
3901 //if(opcode2[i]==0x10||opcode2[i]==0x12) { // MFHI/MFLO
3902 //if(opcode2[i]==0x11||opcode2[i]==0x13) { // MTHI/MTLO
3904 signed char sh,sl,th,tl;
3905 th=get_reg(i_regs->regmap,rt1[i]|64);
3906 tl=get_reg(i_regs->regmap,rt1[i]);
3909 sh=get_reg(i_regs->regmap,rs1[i]|64);
3910 sl=get_reg(i_regs->regmap,rs1[i]);
3911 if(sl>=0) emit_mov(sl,tl);
3912 else emit_loadreg(rs1[i],tl);
3914 if(sh>=0) emit_mov(sh,th);
3915 else emit_loadreg(rs1[i]|64,th);
3921 #ifndef fconv_assemble
3922 void fconv_assemble(int i,struct regstat *i_regs)
3924 printf("Need fconv_assemble for this architecture.\n");
3930 void float_assemble(int i,struct regstat *i_regs)
3932 printf("Need float_assemble for this architecture.\n");
3937 void syscall_assemble(int i,struct regstat *i_regs)
3939 signed char ccreg=get_reg(i_regs->regmap,CCREG);
3940 assert(ccreg==HOST_CCREG);
3941 assert(!is_delayslot);
3942 emit_movimm(start+i*4,EAX); // Get PC
3943 emit_addimm(HOST_CCREG,CLOCK_DIVIDER*ccadj[i],HOST_CCREG); // CHECK: is this right? There should probably be an extra cycle...
3944 emit_jmp((int)jump_syscall_hle); // XXX
3947 void hlecall_assemble(int i,struct regstat *i_regs)
3949 signed char ccreg=get_reg(i_regs->regmap,CCREG);
3950 assert(ccreg==HOST_CCREG);
3951 assert(!is_delayslot);
3952 emit_movimm(start+i*4+4,0); // Get PC
3953 emit_movimm((int)psxHLEt[source[i]&7],1);
3954 emit_addimm(HOST_CCREG,CLOCK_DIVIDER*ccadj[i],HOST_CCREG); // XXX
3955 emit_jmp((int)jump_hlecall);
3958 void intcall_assemble(int i,struct regstat *i_regs)
3960 signed char ccreg=get_reg(i_regs->regmap,CCREG);
3961 assert(ccreg==HOST_CCREG);
3962 assert(!is_delayslot);
3963 emit_movimm(start+i*4,0); // Get PC
3964 emit_addimm(HOST_CCREG,CLOCK_DIVIDER*ccadj[i],HOST_CCREG);
3965 emit_jmp((int)jump_intcall);
3968 void ds_assemble(int i,struct regstat *i_regs)
3970 speculate_register_values(i);
3974 alu_assemble(i,i_regs);break;
3976 imm16_assemble(i,i_regs);break;
3978 shift_assemble(i,i_regs);break;
3980 shiftimm_assemble(i,i_regs);break;
3982 load_assemble(i,i_regs);break;
3984 loadlr_assemble(i,i_regs);break;
3986 store_assemble(i,i_regs);break;
3988 storelr_assemble(i,i_regs);break;
3990 cop0_assemble(i,i_regs);break;
3992 cop1_assemble(i,i_regs);break;
3994 c1ls_assemble(i,i_regs);break;
3996 cop2_assemble(i,i_regs);break;
3998 c2ls_assemble(i,i_regs);break;
4000 c2op_assemble(i,i_regs);break;
4002 fconv_assemble(i,i_regs);break;
4004 float_assemble(i,i_regs);break;
4006 fcomp_assemble(i,i_regs);break;
4008 multdiv_assemble(i,i_regs);break;
4010 mov_assemble(i,i_regs);break;
4020 printf("Jump in the delay slot. This is probably a bug.\n");
4025 // Is the branch target a valid internal jump?
4026 int internal_branch(uint64_t i_is32,int addr)
4028 if(addr&1) return 0; // Indirect (register) jump
4029 if(addr>=start && addr<start+slen*4-4)
4031 int t=(addr-start)>>2;
4032 // Delay slots are not valid branch targets
4033 //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;
4034 // 64 -> 32 bit transition requires a recompile
4035 /*if(is32[t]&~unneeded_reg_upper[t]&~i_is32)
4037 if(requires_32bit[t]&~i_is32) printf("optimizable: no\n");
4038 else printf("optimizable: yes\n");
4040 //if(is32[t]&~unneeded_reg_upper[t]&~i_is32) return 0;
4042 if(requires_32bit[t]&~i_is32) return 0;
4050 #ifndef wb_invalidate
4051 void wb_invalidate(signed char pre[],signed char entry[],uint64_t dirty,uint64_t is32,
4052 uint64_t u,uint64_t uu)
4055 for(hr=0;hr<HOST_REGS;hr++) {
4056 if(hr!=EXCLUDE_REG) {
4057 if(pre[hr]!=entry[hr]) {
4060 if(get_reg(entry,pre[hr])<0) {
4062 if(!((u>>pre[hr])&1)) {
4063 emit_storereg(pre[hr],hr);
4064 if( ((is32>>pre[hr])&1) && !((uu>>pre[hr])&1) ) {
4065 emit_sarimm(hr,31,hr);
4066 emit_storereg(pre[hr]|64,hr);
4070 if(!((uu>>(pre[hr]&63))&1) && !((is32>>(pre[hr]&63))&1)) {
4071 emit_storereg(pre[hr],hr);
4080 // Move from one register to another (no writeback)
4081 for(hr=0;hr<HOST_REGS;hr++) {
4082 if(hr!=EXCLUDE_REG) {
4083 if(pre[hr]!=entry[hr]) {
4084 if(pre[hr]>=0&&(pre[hr]&63)<TEMPREG) {
4086 if((nr=get_reg(entry,pre[hr]))>=0) {
4096 // Load the specified registers
4097 // This only loads the registers given as arguments because
4098 // we don't want to load things that will be overwritten
4099 void load_regs(signed char entry[],signed char regmap[],int is32,int rs1,int rs2)
4103 for(hr=0;hr<HOST_REGS;hr++) {
4104 if(hr!=EXCLUDE_REG&®map[hr]>=0) {
4105 if(entry[hr]!=regmap[hr]) {
4106 if(regmap[hr]==rs1||regmap[hr]==rs2)
4113 emit_loadreg(regmap[hr],hr);
4120 for(hr=0;hr<HOST_REGS;hr++) {
4121 if(hr!=EXCLUDE_REG&®map[hr]>=0) {
4122 if(entry[hr]!=regmap[hr]) {
4123 if(regmap[hr]-64==rs1||regmap[hr]-64==rs2)
4125 assert(regmap[hr]!=64);
4126 if((is32>>(regmap[hr]&63))&1) {
4127 int lr=get_reg(regmap,regmap[hr]-64);
4129 emit_sarimm(lr,31,hr);
4131 emit_loadreg(regmap[hr],hr);
4135 emit_loadreg(regmap[hr],hr);
4143 // Load registers prior to the start of a loop
4144 // so that they are not loaded within the loop
4145 static void loop_preload(signed char pre[],signed char entry[])
4148 for(hr=0;hr<HOST_REGS;hr++) {
4149 if(hr!=EXCLUDE_REG) {
4150 if(pre[hr]!=entry[hr]) {
4152 if(get_reg(pre,entry[hr])<0) {
4153 assem_debug("loop preload:\n");
4154 //printf("loop preload: %d\n",hr);
4158 else if(entry[hr]<TEMPREG)
4160 emit_loadreg(entry[hr],hr);
4162 else if(entry[hr]-64<TEMPREG)
4164 emit_loadreg(entry[hr],hr);
4173 // Generate address for load/store instruction
4174 // goes to AGEN for writes, FTEMP for LOADLR and cop1/2 loads
4175 void address_generation(int i,struct regstat *i_regs,signed char entry[])
4177 if(itype[i]==LOAD||itype[i]==LOADLR||itype[i]==STORE||itype[i]==STORELR||itype[i]==C1LS||itype[i]==C2LS) {
4179 int agr=AGEN1+(i&1);
4180 int mgr=MGEN1+(i&1);
4181 if(itype[i]==LOAD) {
4182 ra=get_reg(i_regs->regmap,rt1[i]);
4183 if(ra<0) ra=get_reg(i_regs->regmap,-1);
4186 if(itype[i]==LOADLR) {
4187 ra=get_reg(i_regs->regmap,FTEMP);
4189 if(itype[i]==STORE||itype[i]==STORELR) {
4190 ra=get_reg(i_regs->regmap,agr);
4191 if(ra<0) ra=get_reg(i_regs->regmap,-1);
4193 if(itype[i]==C1LS||itype[i]==C2LS) {
4194 if ((opcode[i]&0x3b)==0x31||(opcode[i]&0x3b)==0x32) // LWC1/LDC1/LWC2/LDC2
4195 ra=get_reg(i_regs->regmap,FTEMP);
4196 else { // SWC1/SDC1/SWC2/SDC2
4197 ra=get_reg(i_regs->regmap,agr);
4198 if(ra<0) ra=get_reg(i_regs->regmap,-1);
4201 int rs=get_reg(i_regs->regmap,rs1[i]);
4202 int rm=get_reg(i_regs->regmap,TLREG);
4205 int c=(i_regs->wasconst>>rs)&1;
4207 // Using r0 as a base address
4209 if(!entry||entry[rm]!=mgr) {
4210 generate_map_const(offset,rm);
4211 } // else did it in the previous cycle
4213 if(!entry||entry[ra]!=agr) {
4214 if (opcode[i]==0x22||opcode[i]==0x26) {
4215 emit_movimm(offset&0xFFFFFFFC,ra); // LWL/LWR
4216 }else if (opcode[i]==0x1a||opcode[i]==0x1b) {
4217 emit_movimm(offset&0xFFFFFFF8,ra); // LDL/LDR
4219 emit_movimm(offset,ra);
4221 } // else did it in the previous cycle
4224 if(!entry||entry[ra]!=rs1[i])
4225 emit_loadreg(rs1[i],ra);
4226 //if(!entry||entry[ra]!=rs1[i])
4227 // printf("poor load scheduling!\n");
4232 if(!entry||entry[rm]!=mgr) {
4233 if(itype[i]==STORE||itype[i]==STORELR||(opcode[i]&0x3b)==0x39||(opcode[i]&0x3b)==0x3a) {
4234 // Stores to memory go thru the mapper to detect self-modifying
4235 // code, loads don't.
4236 if((unsigned int)(constmap[i][rs]+offset)>=0xC0000000 ||
4237 (unsigned int)(constmap[i][rs]+offset)<0x80000000+RAM_SIZE )
4238 generate_map_const(constmap[i][rs]+offset,rm);
4240 if((signed int)(constmap[i][rs]+offset)>=(signed int)0xC0000000)
4241 generate_map_const(constmap[i][rs]+offset,rm);
4246 if(rs1[i]!=rt1[i]||itype[i]!=LOAD) {
4247 if(!entry||entry[ra]!=agr) {
4248 if (opcode[i]==0x22||opcode[i]==0x26) {
4249 emit_movimm((constmap[i][rs]+offset)&0xFFFFFFFC,ra); // LWL/LWR
4250 }else if (opcode[i]==0x1a||opcode[i]==0x1b) {
4251 emit_movimm((constmap[i][rs]+offset)&0xFFFFFFF8,ra); // LDL/LDR
4253 #ifdef HOST_IMM_ADDR32
4254 if((itype[i]!=LOAD&&(opcode[i]&0x3b)!=0x31&&(opcode[i]&0x3b)!=0x32) || // LWC1/LDC1/LWC2/LDC2
4255 (using_tlb&&((signed int)constmap[i][rs]+offset)>=(signed int)0xC0000000))
4257 emit_movimm(constmap[i][rs]+offset,ra);
4259 } // else did it in the previous cycle
4260 } // else load_consts already did it
4262 if(offset&&!c&&rs1[i]) {
4264 emit_addimm(rs,offset,ra);
4266 emit_addimm(ra,offset,ra);
4271 // Preload constants for next instruction
4272 if(itype[i+1]==LOAD||itype[i+1]==LOADLR||itype[i+1]==STORE||itype[i+1]==STORELR||itype[i+1]==C1LS||itype[i+1]==C2LS) {
4274 #if !defined(HOST_IMM_ADDR32) && !defined(DISABLE_TLB)
4276 agr=MGEN1+((i+1)&1);
4277 ra=get_reg(i_regs->regmap,agr);
4279 int rs=get_reg(regs[i+1].regmap,rs1[i+1]);
4280 int offset=imm[i+1];
4281 int c=(regs[i+1].wasconst>>rs)&1;
4283 if(itype[i+1]==STORE||itype[i+1]==STORELR
4284 ||(opcode[i+1]&0x3b)==0x39||(opcode[i+1]&0x3b)==0x3a) { // SWC1/SDC1, SWC2/SDC2
4285 // Stores to memory go thru the mapper to detect self-modifying
4286 // code, loads don't.
4287 if((unsigned int)(constmap[i+1][rs]+offset)>=0xC0000000 ||
4288 (unsigned int)(constmap[i+1][rs]+offset)<0x80000000+RAM_SIZE )
4289 generate_map_const(constmap[i+1][rs]+offset,ra);
4291 if((signed int)(constmap[i+1][rs]+offset)>=(signed int)0xC0000000)
4292 generate_map_const(constmap[i+1][rs]+offset,ra);
4295 /*else if(rs1[i]==0) {
4296 generate_map_const(offset,ra);
4301 agr=AGEN1+((i+1)&1);
4302 ra=get_reg(i_regs->regmap,agr);
4304 int rs=get_reg(regs[i+1].regmap,rs1[i+1]);
4305 int offset=imm[i+1];
4306 int c=(regs[i+1].wasconst>>rs)&1;
4307 if(c&&(rs1[i+1]!=rt1[i+1]||itype[i+1]!=LOAD)) {
4308 if (opcode[i+1]==0x22||opcode[i+1]==0x26) {
4309 emit_movimm((constmap[i+1][rs]+offset)&0xFFFFFFFC,ra); // LWL/LWR
4310 }else if (opcode[i+1]==0x1a||opcode[i+1]==0x1b) {
4311 emit_movimm((constmap[i+1][rs]+offset)&0xFFFFFFF8,ra); // LDL/LDR
4313 #ifdef HOST_IMM_ADDR32
4314 if((itype[i+1]!=LOAD&&(opcode[i+1]&0x3b)!=0x31&&(opcode[i+1]&0x3b)!=0x32) || // LWC1/LDC1/LWC2/LDC2
4315 (using_tlb&&((signed int)constmap[i+1][rs]+offset)>=(signed int)0xC0000000))
4317 emit_movimm(constmap[i+1][rs]+offset,ra);
4320 else if(rs1[i+1]==0) {
4321 // Using r0 as a base address
4322 if (opcode[i+1]==0x22||opcode[i+1]==0x26) {
4323 emit_movimm(offset&0xFFFFFFFC,ra); // LWL/LWR
4324 }else if (opcode[i+1]==0x1a||opcode[i+1]==0x1b) {
4325 emit_movimm(offset&0xFFFFFFF8,ra); // LDL/LDR
4327 emit_movimm(offset,ra);
4334 int get_final_value(int hr, int i, int *value)
4336 int reg=regs[i].regmap[hr];
4338 if(regs[i+1].regmap[hr]!=reg) break;
4339 if(!((regs[i+1].isconst>>hr)&1)) break;
4344 if(itype[i]==UJUMP||itype[i]==RJUMP||itype[i]==CJUMP||itype[i]==SJUMP) {
4345 *value=constmap[i][hr];
4349 if(itype[i+1]==UJUMP||itype[i+1]==RJUMP||itype[i+1]==CJUMP||itype[i+1]==SJUMP) {
4350 // Load in delay slot, out-of-order execution
4351 if(itype[i+2]==LOAD&&rs1[i+2]==reg&&rt1[i+2]==reg&&((regs[i+1].wasconst>>hr)&1))
4353 #ifdef HOST_IMM_ADDR32
4354 if(!using_tlb||((signed int)constmap[i][hr]+imm[i+2])<(signed int)0xC0000000) return 0;
4356 // Precompute load address
4357 *value=constmap[i][hr]+imm[i+2];
4361 if(itype[i+1]==LOAD&&rs1[i+1]==reg&&rt1[i+1]==reg)
4363 #ifdef HOST_IMM_ADDR32
4364 if(!using_tlb||((signed int)constmap[i][hr]+imm[i+1])<(signed int)0xC0000000) return 0;
4366 // Precompute load address
4367 *value=constmap[i][hr]+imm[i+1];
4368 //printf("c=%x imm=%x\n",(int)constmap[i][hr],imm[i+1]);
4373 *value=constmap[i][hr];
4374 //printf("c=%x\n",(int)constmap[i][hr]);
4375 if(i==slen-1) return 1;
4377 return !((unneeded_reg[i+1]>>reg)&1);
4379 return !((unneeded_reg_upper[i+1]>>reg)&1);
4383 // Load registers with known constants
4384 void load_consts(signed char pre[],signed char regmap[],int is32,int i)
4388 for(hr=0;hr<HOST_REGS;hr++) {
4389 if(hr!=EXCLUDE_REG&®map[hr]>=0) {
4390 //if(entry[hr]!=regmap[hr]) {
4391 if(i==0||!((regs[i-1].isconst>>hr)&1)||pre[hr]!=regmap[hr]||bt[i]) {
4392 if(((regs[i].isconst>>hr)&1)&®map[hr]<64&®map[hr]>0) {
4394 if(get_final_value(hr,i,&value)) {
4399 emit_movimm(value,hr);
4407 for(hr=0;hr<HOST_REGS;hr++) {
4408 if(hr!=EXCLUDE_REG&®map[hr]>=0) {
4409 //if(entry[hr]!=regmap[hr]) {
4410 if(i==0||!((regs[i-1].isconst>>hr)&1)||pre[hr]!=regmap[hr]||bt[i]) {
4411 if(((regs[i].isconst>>hr)&1)&®map[hr]>64) {
4412 if((is32>>(regmap[hr]&63))&1) {
4413 int lr=get_reg(regmap,regmap[hr]-64);
4415 emit_sarimm(lr,31,hr);
4420 if(get_final_value(hr,i,&value)) {
4425 emit_movimm(value,hr);
4434 void load_all_consts(signed char regmap[],int is32,u_int dirty,int i)
4438 for(hr=0;hr<HOST_REGS;hr++) {
4439 if(hr!=EXCLUDE_REG&®map[hr]>=0&&((dirty>>hr)&1)) {
4440 if(((regs[i].isconst>>hr)&1)&®map[hr]<64&®map[hr]>0) {
4441 int value=constmap[i][hr];
4446 emit_movimm(value,hr);
4452 for(hr=0;hr<HOST_REGS;hr++) {
4453 if(hr!=EXCLUDE_REG&®map[hr]>=0&&((dirty>>hr)&1)) {
4454 if(((regs[i].isconst>>hr)&1)&®map[hr]>64) {
4455 if((is32>>(regmap[hr]&63))&1) {
4456 int lr=get_reg(regmap,regmap[hr]-64);
4458 emit_sarimm(lr,31,hr);
4462 int value=constmap[i][hr];
4467 emit_movimm(value,hr);
4475 // Write out all dirty registers (except cycle count)
4476 void wb_dirtys(signed char i_regmap[],uint64_t i_is32,uint64_t i_dirty)
4479 for(hr=0;hr<HOST_REGS;hr++) {
4480 if(hr!=EXCLUDE_REG) {
4481 if(i_regmap[hr]>0) {
4482 if(i_regmap[hr]!=CCREG) {
4483 if((i_dirty>>hr)&1) {
4484 if(i_regmap[hr]<64) {
4485 emit_storereg(i_regmap[hr],hr);
4487 if( ((i_is32>>i_regmap[hr])&1) ) {
4488 #ifdef DESTRUCTIVE_WRITEBACK
4489 emit_sarimm(hr,31,hr);
4490 emit_storereg(i_regmap[hr]|64,hr);
4492 emit_sarimm(hr,31,HOST_TEMPREG);
4493 emit_storereg(i_regmap[hr]|64,HOST_TEMPREG);
4498 if( !((i_is32>>(i_regmap[hr]&63))&1) ) {
4499 emit_storereg(i_regmap[hr],hr);
4508 // Write out dirty registers that we need to reload (pair with load_needed_regs)
4509 // This writes the registers not written by store_regs_bt
4510 void wb_needed_dirtys(signed char i_regmap[],uint64_t i_is32,uint64_t i_dirty,int addr)
4513 int t=(addr-start)>>2;
4514 for(hr=0;hr<HOST_REGS;hr++) {
4515 if(hr!=EXCLUDE_REG) {
4516 if(i_regmap[hr]>0) {
4517 if(i_regmap[hr]!=CCREG) {
4518 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)) {
4519 if((i_dirty>>hr)&1) {
4520 if(i_regmap[hr]<64) {
4521 emit_storereg(i_regmap[hr],hr);
4523 if( ((i_is32>>i_regmap[hr])&1) ) {
4524 #ifdef DESTRUCTIVE_WRITEBACK
4525 emit_sarimm(hr,31,hr);
4526 emit_storereg(i_regmap[hr]|64,hr);
4528 emit_sarimm(hr,31,HOST_TEMPREG);
4529 emit_storereg(i_regmap[hr]|64,HOST_TEMPREG);
4534 if( !((i_is32>>(i_regmap[hr]&63))&1) ) {
4535 emit_storereg(i_regmap[hr],hr);
4546 // Load all registers (except cycle count)
4547 void load_all_regs(signed char i_regmap[])
4550 for(hr=0;hr<HOST_REGS;hr++) {
4551 if(hr!=EXCLUDE_REG) {
4552 if(i_regmap[hr]==0) {
4556 if(i_regmap[hr]>0 && (i_regmap[hr]&63)<TEMPREG && i_regmap[hr]!=CCREG)
4558 emit_loadreg(i_regmap[hr],hr);
4564 // Load all current registers also needed by next instruction
4565 void load_needed_regs(signed char i_regmap[],signed char next_regmap[])
4568 for(hr=0;hr<HOST_REGS;hr++) {
4569 if(hr!=EXCLUDE_REG) {
4570 if(get_reg(next_regmap,i_regmap[hr])>=0) {
4571 if(i_regmap[hr]==0) {
4575 if(i_regmap[hr]>0 && (i_regmap[hr]&63)<TEMPREG && i_regmap[hr]!=CCREG)
4577 emit_loadreg(i_regmap[hr],hr);
4584 // Load all regs, storing cycle count if necessary
4585 void load_regs_entry(int t)
4588 if(is_ds[t]) emit_addimm(HOST_CCREG,CLOCK_DIVIDER,HOST_CCREG);
4589 else if(ccadj[t]) emit_addimm(HOST_CCREG,-ccadj[t]*CLOCK_DIVIDER,HOST_CCREG);
4590 if(regs[t].regmap_entry[HOST_CCREG]!=CCREG) {
4591 emit_storereg(CCREG,HOST_CCREG);
4594 for(hr=0;hr<HOST_REGS;hr++) {
4595 if(regs[t].regmap_entry[hr]>=0&®s[t].regmap_entry[hr]<TEMPREG) {
4596 if(regs[t].regmap_entry[hr]==0) {
4599 else if(regs[t].regmap_entry[hr]!=CCREG)
4601 emit_loadreg(regs[t].regmap_entry[hr],hr);
4606 for(hr=0;hr<HOST_REGS;hr++) {
4607 if(regs[t].regmap_entry[hr]>=64&®s[t].regmap_entry[hr]<TEMPREG+64) {
4608 assert(regs[t].regmap_entry[hr]!=64);
4609 if((regs[t].was32>>(regs[t].regmap_entry[hr]&63))&1) {
4610 int lr=get_reg(regs[t].regmap_entry,regs[t].regmap_entry[hr]-64);
4612 emit_loadreg(regs[t].regmap_entry[hr],hr);
4616 emit_sarimm(lr,31,hr);
4621 emit_loadreg(regs[t].regmap_entry[hr],hr);
4627 // Store dirty registers prior to branch
4628 void store_regs_bt(signed char i_regmap[],uint64_t i_is32,uint64_t i_dirty,int addr)
4630 if(internal_branch(i_is32,addr))
4632 int t=(addr-start)>>2;
4634 for(hr=0;hr<HOST_REGS;hr++) {
4635 if(hr!=EXCLUDE_REG) {
4636 if(i_regmap[hr]>0 && i_regmap[hr]!=CCREG) {
4637 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)) {
4638 if((i_dirty>>hr)&1) {
4639 if(i_regmap[hr]<64) {
4640 if(!((unneeded_reg[t]>>i_regmap[hr])&1)) {
4641 emit_storereg(i_regmap[hr],hr);
4642 if( ((i_is32>>i_regmap[hr])&1) && !((unneeded_reg_upper[t]>>i_regmap[hr])&1) ) {
4643 #ifdef DESTRUCTIVE_WRITEBACK
4644 emit_sarimm(hr,31,hr);
4645 emit_storereg(i_regmap[hr]|64,hr);
4647 emit_sarimm(hr,31,HOST_TEMPREG);
4648 emit_storereg(i_regmap[hr]|64,HOST_TEMPREG);
4653 if( !((i_is32>>(i_regmap[hr]&63))&1) && !((unneeded_reg_upper[t]>>(i_regmap[hr]&63))&1) ) {
4654 emit_storereg(i_regmap[hr],hr);
4665 // Branch out of this block, write out all dirty regs
4666 wb_dirtys(i_regmap,i_is32,i_dirty);
4670 // Load all needed registers for branch target
4671 void load_regs_bt(signed char i_regmap[],uint64_t i_is32,uint64_t i_dirty,int addr)
4673 //if(addr>=start && addr<(start+slen*4))
4674 if(internal_branch(i_is32,addr))
4676 int t=(addr-start)>>2;
4678 // Store the cycle count before loading something else
4679 if(i_regmap[HOST_CCREG]!=CCREG) {
4680 assert(i_regmap[HOST_CCREG]==-1);
4682 if(regs[t].regmap_entry[HOST_CCREG]!=CCREG) {
4683 emit_storereg(CCREG,HOST_CCREG);
4686 for(hr=0;hr<HOST_REGS;hr++) {
4687 if(hr!=EXCLUDE_REG&®s[t].regmap_entry[hr]>=0&®s[t].regmap_entry[hr]<TEMPREG) {
4688 #ifdef DESTRUCTIVE_WRITEBACK
4689 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)) {
4691 if(i_regmap[hr]!=regs[t].regmap_entry[hr] ) {
4693 if(regs[t].regmap_entry[hr]==0) {
4696 else if(regs[t].regmap_entry[hr]!=CCREG)
4698 emit_loadreg(regs[t].regmap_entry[hr],hr);
4704 for(hr=0;hr<HOST_REGS;hr++) {
4705 if(hr!=EXCLUDE_REG&®s[t].regmap_entry[hr]>=64&®s[t].regmap_entry[hr]<TEMPREG+64) {
4706 if(i_regmap[hr]!=regs[t].regmap_entry[hr]) {
4707 assert(regs[t].regmap_entry[hr]!=64);
4708 if((i_is32>>(regs[t].regmap_entry[hr]&63))&1) {
4709 int lr=get_reg(regs[t].regmap_entry,regs[t].regmap_entry[hr]-64);
4711 emit_loadreg(regs[t].regmap_entry[hr],hr);
4715 emit_sarimm(lr,31,hr);
4720 emit_loadreg(regs[t].regmap_entry[hr],hr);
4723 else if((i_is32>>(regs[t].regmap_entry[hr]&63))&1) {
4724 int lr=get_reg(regs[t].regmap_entry,regs[t].regmap_entry[hr]-64);
4726 emit_sarimm(lr,31,hr);
4733 int match_bt(signed char i_regmap[],uint64_t i_is32,uint64_t i_dirty,int addr)
4735 if(addr>=start && addr<start+slen*4-4)
4737 int t=(addr-start)>>2;
4739 if(regs[t].regmap_entry[HOST_CCREG]!=CCREG) return 0;
4740 for(hr=0;hr<HOST_REGS;hr++)
4744 if(i_regmap[hr]!=regs[t].regmap_entry[hr])
4746 if(regs[t].regmap_entry[hr]>=0&&(regs[t].regmap_entry[hr]|64)<TEMPREG+64)
4753 if(i_regmap[hr]<TEMPREG)
4755 if(!((unneeded_reg[t]>>i_regmap[hr])&1))
4758 else if(i_regmap[hr]>=64&&i_regmap[hr]<TEMPREG+64)
4760 if(!((unneeded_reg_upper[t]>>(i_regmap[hr]&63))&1))
4765 else // Same register but is it 32-bit or dirty?
4768 if(!((regs[t].dirty>>hr)&1))
4772 if(!((unneeded_reg[t]>>i_regmap[hr])&1))
4774 //printf("%x: dirty no match\n",addr);
4779 if((((regs[t].was32^i_is32)&~unneeded_reg_upper[t])>>(i_regmap[hr]&63))&1)
4781 //printf("%x: is32 no match\n",addr);
4787 //if(is32[t]&~unneeded_reg_upper[t]&~i_is32) return 0;
4789 if(requires_32bit[t]&~i_is32) return 0;
4791 // Delay slots are not valid branch targets
4792 //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;
4793 // Delay slots require additional processing, so do not match
4794 if(is_ds[t]) return 0;
4799 for(hr=0;hr<HOST_REGS;hr++)
4805 if(hr!=HOST_CCREG||i_regmap[hr]!=CCREG)
4819 // Used when a branch jumps into the delay slot of another branch
4820 void ds_assemble_entry(int i)
4822 int t=(ba[i]-start)>>2;
4823 if(!instr_addr[t]) instr_addr[t]=(u_int)out;
4824 assem_debug("Assemble delay slot at %x\n",ba[i]);
4825 assem_debug("<->\n");
4826 if(regs[t].regmap_entry[HOST_CCREG]==CCREG&®s[t].regmap[HOST_CCREG]!=CCREG)
4827 wb_register(CCREG,regs[t].regmap_entry,regs[t].wasdirty,regs[t].was32);
4828 load_regs(regs[t].regmap_entry,regs[t].regmap,regs[t].was32,rs1[t],rs2[t]);
4829 address_generation(t,®s[t],regs[t].regmap_entry);
4830 if(itype[t]==STORE||itype[t]==STORELR||(opcode[t]&0x3b)==0x39||(opcode[t]&0x3b)==0x3a)
4831 load_regs(regs[t].regmap_entry,regs[t].regmap,regs[t].was32,INVCP,INVCP);
4836 alu_assemble(t,®s[t]);break;
4838 imm16_assemble(t,®s[t]);break;
4840 shift_assemble(t,®s[t]);break;
4842 shiftimm_assemble(t,®s[t]);break;
4844 load_assemble(t,®s[t]);break;
4846 loadlr_assemble(t,®s[t]);break;
4848 store_assemble(t,®s[t]);break;
4850 storelr_assemble(t,®s[t]);break;
4852 cop0_assemble(t,®s[t]);break;
4854 cop1_assemble(t,®s[t]);break;
4856 c1ls_assemble(t,®s[t]);break;
4858 cop2_assemble(t,®s[t]);break;
4860 c2ls_assemble(t,®s[t]);break;
4862 c2op_assemble(t,®s[t]);break;
4864 fconv_assemble(t,®s[t]);break;
4866 float_assemble(t,®s[t]);break;
4868 fcomp_assemble(t,®s[t]);break;
4870 multdiv_assemble(t,®s[t]);break;
4872 mov_assemble(t,®s[t]);break;
4882 printf("Jump in the delay slot. This is probably a bug.\n");
4884 store_regs_bt(regs[t].regmap,regs[t].is32,regs[t].dirty,ba[i]+4);
4885 load_regs_bt(regs[t].regmap,regs[t].is32,regs[t].dirty,ba[i]+4);
4886 if(internal_branch(regs[t].is32,ba[i]+4))
4887 assem_debug("branch: internal\n");
4889 assem_debug("branch: external\n");
4890 assert(internal_branch(regs[t].is32,ba[i]+4));
4891 add_to_linker((int)out,ba[i]+4,internal_branch(regs[t].is32,ba[i]+4));
4895 void do_cc(int i,signed char i_regmap[],int *adj,int addr,int taken,int invert)
4904 //if(ba[i]>=start && ba[i]<(start+slen*4))
4905 if(internal_branch(branch_regs[i].is32,ba[i]))
4907 int t=(ba[i]-start)>>2;
4908 if(is_ds[t]) *adj=-1; // Branch into delay slot adds an extra cycle
4916 if(taken==TAKEN && i==(ba[i]-start)>>2 && source[i+1]==0) {
4918 if(count&1) emit_addimm_and_set_flags(2*(count+2),HOST_CCREG);
4920 //emit_subfrommem(&idlecount,HOST_CCREG); // Count idle cycles
4921 emit_andimm(HOST_CCREG,3,HOST_CCREG);
4925 else if(*adj==0||invert) {
4926 emit_addimm_and_set_flags(CLOCK_DIVIDER*(count+2),HOST_CCREG);
4932 emit_cmpimm(HOST_CCREG,-CLOCK_DIVIDER*(count+2));
4936 add_stub(CC_STUB,jaddr,idle?idle:(int)out,(*adj==0||invert||idle)?0:(count+2),i,addr,taken,0);
4939 void do_ccstub(int n)
4942 assem_debug("do_ccstub %x\n",start+stubs[n][4]*4);
4943 set_jump_target(stubs[n][1],(int)out);
4945 if(stubs[n][6]==NULLDS) {
4946 // Delay slot instruction is nullified ("likely" branch)
4947 wb_dirtys(regs[i].regmap,regs[i].is32,regs[i].dirty);
4949 else if(stubs[n][6]!=TAKEN) {
4950 wb_dirtys(branch_regs[i].regmap,branch_regs[i].is32,branch_regs[i].dirty);
4953 if(internal_branch(branch_regs[i].is32,ba[i]))
4954 wb_needed_dirtys(branch_regs[i].regmap,branch_regs[i].is32,branch_regs[i].dirty,ba[i]);
4958 // Save PC as return address
4959 emit_movimm(stubs[n][5],EAX);
4960 emit_writeword(EAX,(int)&pcaddr);
4964 // Return address depends on which way the branch goes
4965 if(itype[i]==CJUMP||itype[i]==SJUMP||itype[i]==FJUMP)
4967 int s1l=get_reg(branch_regs[i].regmap,rs1[i]);
4968 int s1h=get_reg(branch_regs[i].regmap,rs1[i]|64);
4969 int s2l=get_reg(branch_regs[i].regmap,rs2[i]);
4970 int s2h=get_reg(branch_regs[i].regmap,rs2[i]|64);
4980 if((branch_regs[i].is32>>rs1[i])&(branch_regs[i].is32>>rs2[i])&1) {
4984 #ifdef DESTRUCTIVE_WRITEBACK
4986 if((branch_regs[i].dirty>>s1l)&(branch_regs[i].is32>>rs1[i])&1)
4987 emit_loadreg(rs1[i],s1l);
4990 if((branch_regs[i].dirty>>s1l)&(branch_regs[i].is32>>rs2[i])&1)
4991 emit_loadreg(rs2[i],s1l);
4994 if((branch_regs[i].dirty>>s2l)&(branch_regs[i].is32>>rs2[i])&1)
4995 emit_loadreg(rs2[i],s2l);
4998 int addr=-1,alt=-1,ntaddr=-1;
5001 if(hr!=EXCLUDE_REG && hr!=HOST_CCREG &&
5002 (branch_regs[i].regmap[hr]&63)!=rs1[i] &&
5003 (branch_regs[i].regmap[hr]&63)!=rs2[i] )
5011 if(hr!=EXCLUDE_REG && hr!=HOST_CCREG &&
5012 (branch_regs[i].regmap[hr]&63)!=rs1[i] &&
5013 (branch_regs[i].regmap[hr]&63)!=rs2[i] )
5019 if((opcode[i]&0x2E)==6) // BLEZ/BGTZ needs another register
5023 if(hr!=EXCLUDE_REG && hr!=HOST_CCREG &&
5024 (branch_regs[i].regmap[hr]&63)!=rs1[i] &&
5025 (branch_regs[i].regmap[hr]&63)!=rs2[i] )
5031 assert(hr<HOST_REGS);
5033 if((opcode[i]&0x2f)==4) // BEQ
5035 #ifdef HAVE_CMOV_IMM
5037 if(s2l>=0) emit_cmp(s1l,s2l);
5038 else emit_test(s1l,s1l);
5039 emit_cmov2imm_e_ne_compact(ba[i],start+i*4+8,addr);
5044 emit_mov2imm_compact(ba[i],addr,start+i*4+8,alt);
5046 if(s2h>=0) emit_cmp(s1h,s2h);
5047 else emit_test(s1h,s1h);
5048 emit_cmovne_reg(alt,addr);
5050 if(s2l>=0) emit_cmp(s1l,s2l);
5051 else emit_test(s1l,s1l);
5052 emit_cmovne_reg(alt,addr);
5055 if((opcode[i]&0x2f)==5) // BNE
5057 #ifdef HAVE_CMOV_IMM
5059 if(s2l>=0) emit_cmp(s1l,s2l);
5060 else emit_test(s1l,s1l);
5061 emit_cmov2imm_e_ne_compact(start+i*4+8,ba[i],addr);
5066 emit_mov2imm_compact(start+i*4+8,addr,ba[i],alt);
5068 if(s2h>=0) emit_cmp(s1h,s2h);
5069 else emit_test(s1h,s1h);
5070 emit_cmovne_reg(alt,addr);
5072 if(s2l>=0) emit_cmp(s1l,s2l);
5073 else emit_test(s1l,s1l);
5074 emit_cmovne_reg(alt,addr);
5077 if((opcode[i]&0x2f)==6) // BLEZ
5079 //emit_movimm(ba[i],alt);
5080 //emit_movimm(start+i*4+8,addr);
5081 emit_mov2imm_compact(ba[i],alt,start+i*4+8,addr);
5083 if(s1h>=0) emit_mov(addr,ntaddr);
5084 emit_cmovl_reg(alt,addr);
5087 emit_cmovne_reg(ntaddr,addr);
5088 emit_cmovs_reg(alt,addr);
5091 if((opcode[i]&0x2f)==7) // BGTZ
5093 //emit_movimm(ba[i],addr);
5094 //emit_movimm(start+i*4+8,ntaddr);
5095 emit_mov2imm_compact(ba[i],addr,start+i*4+8,ntaddr);
5097 if(s1h>=0) emit_mov(addr,alt);
5098 emit_cmovl_reg(ntaddr,addr);
5101 emit_cmovne_reg(alt,addr);
5102 emit_cmovs_reg(ntaddr,addr);
5105 if((opcode[i]==1)&&(opcode2[i]&0x2D)==0) // BLTZ
5107 //emit_movimm(ba[i],alt);
5108 //emit_movimm(start+i*4+8,addr);
5109 emit_mov2imm_compact(ba[i],alt,start+i*4+8,addr);
5110 if(s1h>=0) emit_test(s1h,s1h);
5111 else emit_test(s1l,s1l);
5112 emit_cmovs_reg(alt,addr);
5114 if((opcode[i]==1)&&(opcode2[i]&0x2D)==1) // BGEZ
5116 //emit_movimm(ba[i],addr);
5117 //emit_movimm(start+i*4+8,alt);
5118 emit_mov2imm_compact(ba[i],addr,start+i*4+8,alt);
5119 if(s1h>=0) emit_test(s1h,s1h);
5120 else emit_test(s1l,s1l);
5121 emit_cmovs_reg(alt,addr);
5123 if(opcode[i]==0x11 && opcode2[i]==0x08 ) {
5124 if(source[i]&0x10000) // BC1T
5126 //emit_movimm(ba[i],alt);
5127 //emit_movimm(start+i*4+8,addr);
5128 emit_mov2imm_compact(ba[i],alt,start+i*4+8,addr);
5129 emit_testimm(s1l,0x800000);
5130 emit_cmovne_reg(alt,addr);
5134 //emit_movimm(ba[i],addr);
5135 //emit_movimm(start+i*4+8,alt);
5136 emit_mov2imm_compact(ba[i],addr,start+i*4+8,alt);
5137 emit_testimm(s1l,0x800000);
5138 emit_cmovne_reg(alt,addr);
5141 emit_writeword(addr,(int)&pcaddr);
5146 int r=get_reg(branch_regs[i].regmap,rs1[i]);
5147 if(rs1[i]==rt1[i+1]||rs1[i]==rt2[i+1]) {
5148 r=get_reg(branch_regs[i].regmap,RTEMP);
5150 emit_writeword(r,(int)&pcaddr);
5152 else {printf("Unknown branch type in do_ccstub\n");exit(1);}
5154 // Update cycle count
5155 assert(branch_regs[i].regmap[HOST_CCREG]==CCREG||branch_regs[i].regmap[HOST_CCREG]==-1);
5156 if(stubs[n][3]) emit_addimm(HOST_CCREG,CLOCK_DIVIDER*stubs[n][3],HOST_CCREG);
5157 emit_call((int)cc_interrupt);
5158 if(stubs[n][3]) emit_addimm(HOST_CCREG,-CLOCK_DIVIDER*stubs[n][3],HOST_CCREG);
5159 if(stubs[n][6]==TAKEN) {
5160 if(internal_branch(branch_regs[i].is32,ba[i]))
5161 load_needed_regs(branch_regs[i].regmap,regs[(ba[i]-start)>>2].regmap_entry);
5162 else if(itype[i]==RJUMP) {
5163 if(get_reg(branch_regs[i].regmap,RTEMP)>=0)
5164 emit_readword((int)&pcaddr,get_reg(branch_regs[i].regmap,RTEMP));
5166 emit_loadreg(rs1[i],get_reg(branch_regs[i].regmap,rs1[i]));
5168 }else if(stubs[n][6]==NOTTAKEN) {
5169 if(i<slen-2) load_needed_regs(branch_regs[i].regmap,regmap_pre[i+2]);
5170 else load_all_regs(branch_regs[i].regmap);
5171 }else if(stubs[n][6]==NULLDS) {
5172 // Delay slot instruction is nullified ("likely" branch)
5173 if(i<slen-2) load_needed_regs(regs[i].regmap,regmap_pre[i+2]);
5174 else load_all_regs(regs[i].regmap);
5176 load_all_regs(branch_regs[i].regmap);
5178 emit_jmp(stubs[n][2]); // return address
5180 /* This works but uses a lot of memory...
5181 emit_readword((int)&last_count,ECX);
5182 emit_add(HOST_CCREG,ECX,EAX);
5183 emit_writeword(EAX,(int)&Count);
5184 emit_call((int)gen_interupt);
5185 emit_readword((int)&Count,HOST_CCREG);
5186 emit_readword((int)&next_interupt,EAX);
5187 emit_readword((int)&pending_exception,EBX);
5188 emit_writeword(EAX,(int)&last_count);
5189 emit_sub(HOST_CCREG,EAX,HOST_CCREG);
5191 int jne_instr=(int)out;
5193 if(stubs[n][3]) emit_addimm(HOST_CCREG,-2*stubs[n][3],HOST_CCREG);
5194 load_all_regs(branch_regs[i].regmap);
5195 emit_jmp(stubs[n][2]); // return address
5196 set_jump_target(jne_instr,(int)out);
5197 emit_readword((int)&pcaddr,EAX);
5198 // Call get_addr_ht instead of doing the hash table here.
5199 // This code is executed infrequently and takes up a lot of space
5200 // so smaller is better.
5201 emit_storereg(CCREG,HOST_CCREG);
5203 emit_call((int)get_addr_ht);
5204 emit_loadreg(CCREG,HOST_CCREG);
5205 emit_addimm(ESP,4,ESP);
5209 add_to_linker(int addr,int target,int ext)
5211 link_addr[linkcount][0]=addr;
5212 link_addr[linkcount][1]=target;
5213 link_addr[linkcount][2]=ext;
5217 static void ujump_assemble_write_ra(int i)
5220 unsigned int return_address;
5221 rt=get_reg(branch_regs[i].regmap,31);
5222 assem_debug("branch(%d): eax=%d ecx=%d edx=%d ebx=%d ebp=%d esi=%d edi=%d\n",i,branch_regs[i].regmap[0],branch_regs[i].regmap[1],branch_regs[i].regmap[2],branch_regs[i].regmap[3],branch_regs[i].regmap[5],branch_regs[i].regmap[6],branch_regs[i].regmap[7]);
5224 return_address=start+i*4+8;
5227 if(internal_branch(branch_regs[i].is32,return_address)&&rt1[i+1]!=31) {
5228 int temp=-1; // note: must be ds-safe
5232 if(temp>=0) do_miniht_insert(return_address,rt,temp);
5233 else emit_movimm(return_address,rt);
5241 if(i_regmap[temp]!=PTEMP) emit_movimm((int)hash_table[((return_address>>16)^return_address)&0xFFFF],temp);
5244 emit_movimm(return_address,rt); // PC into link register
5246 emit_prefetch(hash_table[((return_address>>16)^return_address)&0xFFFF]);
5252 void ujump_assemble(int i,struct regstat *i_regs)
5254 signed char *i_regmap=i_regs->regmap;
5256 if(i==(ba[i]-start)>>2) assem_debug("idle loop\n");
5257 address_generation(i+1,i_regs,regs[i].regmap_entry);
5259 int temp=get_reg(branch_regs[i].regmap,PTEMP);
5260 if(rt1[i]==31&&temp>=0)
5262 int return_address=start+i*4+8;
5263 if(get_reg(branch_regs[i].regmap,31)>0)
5264 if(i_regmap[temp]==PTEMP) emit_movimm((int)hash_table[((return_address>>16)^return_address)&0xFFFF],temp);
5267 if(rt1[i]==31&&(rt1[i]==rs1[i+1]||rt1[i]==rs2[i+1])) {
5268 ujump_assemble_write_ra(i); // writeback ra for DS
5271 ds_assemble(i+1,i_regs);
5272 uint64_t bc_unneeded=branch_regs[i].u;
5273 uint64_t bc_unneeded_upper=branch_regs[i].uu;
5274 bc_unneeded|=1|(1LL<<rt1[i]);
5275 bc_unneeded_upper|=1|(1LL<<rt1[i]);
5276 wb_invalidate(regs[i].regmap,branch_regs[i].regmap,regs[i].dirty,regs[i].is32,
5277 bc_unneeded,bc_unneeded_upper);
5278 load_regs(regs[i].regmap,branch_regs[i].regmap,regs[i].was32,CCREG,CCREG);
5279 if(!ra_done&&rt1[i]==31)
5280 ujump_assemble_write_ra(i);
5282 cc=get_reg(branch_regs[i].regmap,CCREG);
5283 assert(cc==HOST_CCREG);
5284 store_regs_bt(branch_regs[i].regmap,branch_regs[i].is32,branch_regs[i].dirty,ba[i]);
5286 if(rt1[i]==31&&temp>=0) emit_prefetchreg(temp);
5288 do_cc(i,branch_regs[i].regmap,&adj,ba[i],TAKEN,0);
5289 if(adj) emit_addimm(cc,CLOCK_DIVIDER*(ccadj[i]+2-adj),cc);
5290 load_regs_bt(branch_regs[i].regmap,branch_regs[i].is32,branch_regs[i].dirty,ba[i]);
5291 if(internal_branch(branch_regs[i].is32,ba[i]))
5292 assem_debug("branch: internal\n");
5294 assem_debug("branch: external\n");
5295 if(internal_branch(branch_regs[i].is32,ba[i])&&is_ds[(ba[i]-start)>>2]) {
5296 ds_assemble_entry(i);
5299 add_to_linker((int)out,ba[i],internal_branch(branch_regs[i].is32,ba[i]));
5304 static void rjump_assemble_write_ra(int i)
5306 int rt,return_address;
5307 assert(rt1[i+1]!=rt1[i]);
5308 assert(rt2[i+1]!=rt1[i]);
5309 rt=get_reg(branch_regs[i].regmap,rt1[i]);
5310 assem_debug("branch(%d): eax=%d ecx=%d edx=%d ebx=%d ebp=%d esi=%d edi=%d\n",i,branch_regs[i].regmap[0],branch_regs[i].regmap[1],branch_regs[i].regmap[2],branch_regs[i].regmap[3],branch_regs[i].regmap[5],branch_regs[i].regmap[6],branch_regs[i].regmap[7]);
5312 return_address=start+i*4+8;
5316 if(i_regmap[temp]!=PTEMP) emit_movimm((int)hash_table[((return_address>>16)^return_address)&0xFFFF],temp);
5319 emit_movimm(return_address,rt); // PC into link register
5321 emit_prefetch(hash_table[((return_address>>16)^return_address)&0xFFFF]);
5325 void rjump_assemble(int i,struct regstat *i_regs)
5327 signed char *i_regmap=i_regs->regmap;
5331 rs=get_reg(branch_regs[i].regmap,rs1[i]);
5333 if(rs1[i]==rt1[i+1]||rs1[i]==rt2[i+1]) {
5334 // Delay slot abuse, make a copy of the branch address register
5335 temp=get_reg(branch_regs[i].regmap,RTEMP);
5337 assert(regs[i].regmap[temp]==RTEMP);
5341 address_generation(i+1,i_regs,regs[i].regmap_entry);
5345 if((temp=get_reg(branch_regs[i].regmap,PTEMP))>=0) {
5346 int return_address=start+i*4+8;
5347 if(i_regmap[temp]==PTEMP) emit_movimm((int)hash_table[((return_address>>16)^return_address)&0xFFFF],temp);
5353 int rh=get_reg(regs[i].regmap,RHASH);
5354 if(rh>=0) do_preload_rhash(rh);
5357 if(rt1[i]!=0&&(rt1[i]==rs1[i+1]||rt1[i]==rs2[i+1])) {
5358 rjump_assemble_write_ra(i);
5361 ds_assemble(i+1,i_regs);
5362 uint64_t bc_unneeded=branch_regs[i].u;
5363 uint64_t bc_unneeded_upper=branch_regs[i].uu;
5364 bc_unneeded|=1|(1LL<<rt1[i]);
5365 bc_unneeded_upper|=1|(1LL<<rt1[i]);
5366 bc_unneeded&=~(1LL<<rs1[i]);
5367 wb_invalidate(regs[i].regmap,branch_regs[i].regmap,regs[i].dirty,regs[i].is32,
5368 bc_unneeded,bc_unneeded_upper);
5369 load_regs(regs[i].regmap,branch_regs[i].regmap,regs[i].was32,rs1[i],CCREG);
5370 if(!ra_done&&rt1[i]!=0)
5371 rjump_assemble_write_ra(i);
5372 cc=get_reg(branch_regs[i].regmap,CCREG);
5373 assert(cc==HOST_CCREG);
5375 int rh=get_reg(branch_regs[i].regmap,RHASH);
5376 int ht=get_reg(branch_regs[i].regmap,RHTBL);
5378 if(regs[i].regmap[rh]!=RHASH) do_preload_rhash(rh);
5379 do_preload_rhtbl(ht);
5383 store_regs_bt(branch_regs[i].regmap,branch_regs[i].is32,branch_regs[i].dirty,-1);
5384 #ifdef DESTRUCTIVE_WRITEBACK
5385 if((branch_regs[i].dirty>>rs)&(branch_regs[i].is32>>rs1[i])&1) {
5386 if(rs1[i]!=rt1[i+1]&&rs1[i]!=rt2[i+1]) {
5387 emit_loadreg(rs1[i],rs);
5392 if(rt1[i]==31&&temp>=0) emit_prefetchreg(temp);
5396 do_miniht_load(ht,rh);
5399 //do_cc(i,branch_regs[i].regmap,&adj,-1,TAKEN);
5400 //if(adj) emit_addimm(cc,2*(ccadj[i]+2-adj),cc); // ??? - Shouldn't happen
5402 emit_addimm_and_set_flags(CLOCK_DIVIDER*(ccadj[i]+2),HOST_CCREG);
5403 add_stub(CC_STUB,(int)out,jump_vaddr_reg[rs],0,i,-1,TAKEN,0);
5405 //load_regs_bt(branch_regs[i].regmap,branch_regs[i].is32,branch_regs[i].dirty,-1);
5408 do_miniht_jump(rs,rh,ht);
5413 //if(rs!=EAX) emit_mov(rs,EAX);
5414 //emit_jmp((int)jump_vaddr_eax);
5415 emit_jmp(jump_vaddr_reg[rs]);
5420 emit_shrimm(rs,16,rs);
5421 emit_xor(temp,rs,rs);
5422 emit_movzwl_reg(rs,rs);
5423 emit_shlimm(rs,4,rs);
5424 emit_cmpmem_indexed((int)hash_table,rs,temp);
5425 emit_jne((int)out+14);
5426 emit_readword_indexed((int)hash_table+4,rs,rs);
5428 emit_cmpmem_indexed((int)hash_table+8,rs,temp);
5429 emit_addimm_no_flags(8,rs);
5430 emit_jeq((int)out-17);
5431 // No hit on hash table, call compiler
5434 #ifdef DEBUG_CYCLE_COUNT
5435 emit_readword((int)&last_count,ECX);
5436 emit_add(HOST_CCREG,ECX,HOST_CCREG);
5437 emit_readword((int)&next_interupt,ECX);
5438 emit_writeword(HOST_CCREG,(int)&Count);
5439 emit_sub(HOST_CCREG,ECX,HOST_CCREG);
5440 emit_writeword(ECX,(int)&last_count);
5443 emit_storereg(CCREG,HOST_CCREG);
5444 emit_call((int)get_addr);
5445 emit_loadreg(CCREG,HOST_CCREG);
5446 emit_addimm(ESP,4,ESP);
5448 #ifdef CORTEX_A8_BRANCH_PREDICTION_HACK
5449 if(rt1[i]!=31&&i<slen-2&&(((u_int)out)&7)) emit_mov(13,13);
5453 void cjump_assemble(int i,struct regstat *i_regs)
5455 signed char *i_regmap=i_regs->regmap;
5458 match=match_bt(branch_regs[i].regmap,branch_regs[i].is32,branch_regs[i].dirty,ba[i]);
5459 assem_debug("match=%d\n",match);
5460 int s1h,s1l,s2h,s2l;
5461 int prev_cop1_usable=cop1_usable;
5462 int unconditional=0,nop=0;
5465 int internal=internal_branch(branch_regs[i].is32,ba[i]);
5466 if(i==(ba[i]-start)>>2) assem_debug("idle loop\n");
5467 if(!match) invert=1;
5468 #ifdef CORTEX_A8_BRANCH_PREDICTION_HACK
5469 if(i>(ba[i]-start)>>2) invert=1;
5473 s1l=get_reg(branch_regs[i].regmap,rs1[i]);
5474 s1h=get_reg(branch_regs[i].regmap,rs1[i]|64);
5475 s2l=get_reg(branch_regs[i].regmap,rs2[i]);
5476 s2h=get_reg(branch_regs[i].regmap,rs2[i]|64);
5479 s1l=get_reg(i_regmap,rs1[i]);
5480 s1h=get_reg(i_regmap,rs1[i]|64);
5481 s2l=get_reg(i_regmap,rs2[i]);
5482 s2h=get_reg(i_regmap,rs2[i]|64);
5484 if(rs1[i]==0&&rs2[i]==0)
5486 if(opcode[i]&1) nop=1;
5487 else unconditional=1;
5488 //assert(opcode[i]!=5);
5489 //assert(opcode[i]!=7);
5490 //assert(opcode[i]!=0x15);
5491 //assert(opcode[i]!=0x17);
5497 only32=(regs[i].was32>>rs2[i])&1;
5502 only32=(regs[i].was32>>rs1[i])&1;
5505 only32=(regs[i].was32>>rs1[i])&(regs[i].was32>>rs2[i])&1;
5509 // Out of order execution (delay slot first)
5511 address_generation(i+1,i_regs,regs[i].regmap_entry);
5512 ds_assemble(i+1,i_regs);
5514 uint64_t bc_unneeded=branch_regs[i].u;
5515 uint64_t bc_unneeded_upper=branch_regs[i].uu;
5516 bc_unneeded&=~((1LL<<rs1[i])|(1LL<<rs2[i]));
5517 bc_unneeded_upper&=~((1LL<<us1[i])|(1LL<<us2[i]));
5519 bc_unneeded_upper|=1;
5520 wb_invalidate(regs[i].regmap,branch_regs[i].regmap,regs[i].dirty,regs[i].is32,
5521 bc_unneeded,bc_unneeded_upper);
5522 load_regs(regs[i].regmap,branch_regs[i].regmap,regs[i].was32,rs1[i],rs2[i]);
5523 load_regs(regs[i].regmap,branch_regs[i].regmap,regs[i].was32,CCREG,CCREG);
5524 cc=get_reg(branch_regs[i].regmap,CCREG);
5525 assert(cc==HOST_CCREG);
5527 store_regs_bt(branch_regs[i].regmap,branch_regs[i].is32,branch_regs[i].dirty,ba[i]);
5528 //do_cc(i,branch_regs[i].regmap,&adj,unconditional?ba[i]:-1,unconditional);
5529 //assem_debug("cycle count (adj)\n");
5531 do_cc(i,branch_regs[i].regmap,&adj,ba[i],TAKEN,0);
5532 if(i!=(ba[i]-start)>>2 || source[i+1]!=0) {
5533 if(adj) emit_addimm(cc,CLOCK_DIVIDER*(ccadj[i]+2-adj),cc);
5534 load_regs_bt(branch_regs[i].regmap,branch_regs[i].is32,branch_regs[i].dirty,ba[i]);
5536 assem_debug("branch: internal\n");
5538 assem_debug("branch: external\n");
5539 if(internal&&is_ds[(ba[i]-start)>>2]) {
5540 ds_assemble_entry(i);
5543 add_to_linker((int)out,ba[i],internal);
5546 #ifdef CORTEX_A8_BRANCH_PREDICTION_HACK
5547 if(((u_int)out)&7) emit_addnop(0);
5552 emit_addimm_and_set_flags(CLOCK_DIVIDER*(ccadj[i]+2),cc);
5555 add_stub(CC_STUB,jaddr,(int)out,0,i,start+i*4+8,NOTTAKEN,0);
5558 int taken=0,nottaken=0,nottaken1=0;
5559 do_cc(i,branch_regs[i].regmap,&adj,-1,0,invert);
5560 if(adj&&!invert) emit_addimm(cc,CLOCK_DIVIDER*(ccadj[i]+2-adj),cc);
5564 if(opcode[i]==4) // BEQ
5566 if(s2h>=0) emit_cmp(s1h,s2h);
5567 else emit_test(s1h,s1h);
5571 if(opcode[i]==5) // BNE
5573 if(s2h>=0) emit_cmp(s1h,s2h);
5574 else emit_test(s1h,s1h);
5575 if(invert) taken=(int)out;
5576 else add_to_linker((int)out,ba[i],internal);
5579 if(opcode[i]==6) // BLEZ
5582 if(invert) taken=(int)out;
5583 else add_to_linker((int)out,ba[i],internal);
5588 if(opcode[i]==7) // BGTZ
5593 if(invert) taken=(int)out;
5594 else add_to_linker((int)out,ba[i],internal);
5599 //printf("branch(%d): eax=%d ecx=%d edx=%d ebx=%d ebp=%d esi=%d edi=%d\n",i,branch_regs[i].regmap[0],branch_regs[i].regmap[1],branch_regs[i].regmap[2],branch_regs[i].regmap[3],branch_regs[i].regmap[5],branch_regs[i].regmap[6],branch_regs[i].regmap[7]);
5601 if(opcode[i]==4) // BEQ
5603 if(s2l>=0) emit_cmp(s1l,s2l);
5604 else emit_test(s1l,s1l);
5609 add_to_linker((int)out,ba[i],internal);
5613 if(opcode[i]==5) // BNE
5615 if(s2l>=0) emit_cmp(s1l,s2l);
5616 else emit_test(s1l,s1l);
5621 add_to_linker((int)out,ba[i],internal);
5625 if(opcode[i]==6) // BLEZ
5632 add_to_linker((int)out,ba[i],internal);
5636 if(opcode[i]==7) // BGTZ
5643 add_to_linker((int)out,ba[i],internal);
5648 if(taken) set_jump_target(taken,(int)out);
5649 #ifdef CORTEX_A8_BRANCH_PREDICTION_HACK
5650 if(match&&(!internal||!is_ds[(ba[i]-start)>>2])) {
5652 emit_addimm(cc,-CLOCK_DIVIDER*adj,cc);
5653 add_to_linker((int)out,ba[i],internal);
5656 add_to_linker((int)out,ba[i],internal*2);
5662 if(adj) emit_addimm(cc,-CLOCK_DIVIDER*adj,cc);
5663 store_regs_bt(branch_regs[i].regmap,branch_regs[i].is32,branch_regs[i].dirty,ba[i]);
5664 load_regs_bt(branch_regs[i].regmap,branch_regs[i].is32,branch_regs[i].dirty,ba[i]);
5666 assem_debug("branch: internal\n");
5668 assem_debug("branch: external\n");
5669 if(internal&&is_ds[(ba[i]-start)>>2]) {
5670 ds_assemble_entry(i);
5673 add_to_linker((int)out,ba[i],internal);
5677 set_jump_target(nottaken,(int)out);
5680 if(nottaken1) set_jump_target(nottaken1,(int)out);
5682 if(!invert) emit_addimm(cc,CLOCK_DIVIDER*adj,cc);
5684 } // (!unconditional)
5688 // In-order execution (branch first)
5689 //if(likely[i]) printf("IOL\n");
5692 int taken=0,nottaken=0,nottaken1=0;
5693 if(!unconditional&&!nop) {
5697 if((opcode[i]&0x2f)==4) // BEQ
5699 if(s2h>=0) emit_cmp(s1h,s2h);
5700 else emit_test(s1h,s1h);
5704 if((opcode[i]&0x2f)==5) // BNE
5706 if(s2h>=0) emit_cmp(s1h,s2h);
5707 else emit_test(s1h,s1h);
5711 if((opcode[i]&0x2f)==6) // BLEZ
5719 if((opcode[i]&0x2f)==7) // BGTZ
5729 //printf("branch(%d): eax=%d ecx=%d edx=%d ebx=%d ebp=%d esi=%d edi=%d\n",i,branch_regs[i].regmap[0],branch_regs[i].regmap[1],branch_regs[i].regmap[2],branch_regs[i].regmap[3],branch_regs[i].regmap[5],branch_regs[i].regmap[6],branch_regs[i].regmap[7]);
5731 if((opcode[i]&0x2f)==4) // BEQ
5733 if(s2l>=0) emit_cmp(s1l,s2l);
5734 else emit_test(s1l,s1l);
5738 if((opcode[i]&0x2f)==5) // BNE
5740 if(s2l>=0) emit_cmp(s1l,s2l);
5741 else emit_test(s1l,s1l);
5745 if((opcode[i]&0x2f)==6) // BLEZ
5751 if((opcode[i]&0x2f)==7) // BGTZ
5757 } // if(!unconditional)
5759 uint64_t ds_unneeded=branch_regs[i].u;
5760 uint64_t ds_unneeded_upper=branch_regs[i].uu;
5761 ds_unneeded&=~((1LL<<rs1[i+1])|(1LL<<rs2[i+1]));
5762 ds_unneeded_upper&=~((1LL<<us1[i+1])|(1LL<<us2[i+1]));
5763 if((~ds_unneeded_upper>>rt1[i+1])&1) ds_unneeded_upper&=~((1LL<<dep1[i+1])|(1LL<<dep2[i+1]));
5765 ds_unneeded_upper|=1;
5768 if(taken) set_jump_target(taken,(int)out);
5769 assem_debug("1:\n");
5770 wb_invalidate(regs[i].regmap,branch_regs[i].regmap,regs[i].dirty,regs[i].is32,
5771 ds_unneeded,ds_unneeded_upper);
5773 load_regs(regs[i].regmap,branch_regs[i].regmap,regs[i].was32,rs1[i+1],rs2[i+1]);
5774 address_generation(i+1,&branch_regs[i],0);
5775 load_regs(regs[i].regmap,branch_regs[i].regmap,regs[i].was32,CCREG,INVCP);
5776 ds_assemble(i+1,&branch_regs[i]);
5777 cc=get_reg(branch_regs[i].regmap,CCREG);
5779 emit_loadreg(CCREG,cc=HOST_CCREG);
5780 // CHECK: Is the following instruction (fall thru) allocated ok?
5782 assert(cc==HOST_CCREG);
5783 store_regs_bt(branch_regs[i].regmap,branch_regs[i].is32,branch_regs[i].dirty,ba[i]);
5784 do_cc(i,i_regmap,&adj,ba[i],TAKEN,0);
5785 assem_debug("cycle count (adj)\n");
5786 if(adj) emit_addimm(cc,CLOCK_DIVIDER*(ccadj[i]+2-adj),cc);
5787 load_regs_bt(branch_regs[i].regmap,branch_regs[i].is32,branch_regs[i].dirty,ba[i]);
5789 assem_debug("branch: internal\n");
5791 assem_debug("branch: external\n");
5792 if(internal&&is_ds[(ba[i]-start)>>2]) {
5793 ds_assemble_entry(i);
5796 add_to_linker((int)out,ba[i],internal);
5801 cop1_usable=prev_cop1_usable;
5802 if(!unconditional) {
5803 if(nottaken1) set_jump_target(nottaken1,(int)out);
5804 set_jump_target(nottaken,(int)out);
5805 assem_debug("2:\n");
5807 wb_invalidate(regs[i].regmap,branch_regs[i].regmap,regs[i].dirty,regs[i].is32,
5808 ds_unneeded,ds_unneeded_upper);
5809 load_regs(regs[i].regmap,branch_regs[i].regmap,regs[i].was32,rs1[i+1],rs2[i+1]);
5810 address_generation(i+1,&branch_regs[i],0);
5811 load_regs(regs[i].regmap,branch_regs[i].regmap,regs[i].was32,CCREG,CCREG);
5812 ds_assemble(i+1,&branch_regs[i]);
5814 cc=get_reg(branch_regs[i].regmap,CCREG);
5815 if(cc==-1&&!likely[i]) {
5816 // Cycle count isn't in a register, temporarily load it then write it out
5817 emit_loadreg(CCREG,HOST_CCREG);
5818 emit_addimm_and_set_flags(CLOCK_DIVIDER*(ccadj[i]+2),HOST_CCREG);
5821 add_stub(CC_STUB,jaddr,(int)out,0,i,start+i*4+8,NOTTAKEN,0);
5822 emit_storereg(CCREG,HOST_CCREG);
5825 cc=get_reg(i_regmap,CCREG);
5826 assert(cc==HOST_CCREG);
5827 emit_addimm_and_set_flags(CLOCK_DIVIDER*(ccadj[i]+2),cc);
5830 add_stub(CC_STUB,jaddr,(int)out,0,i,start+i*4+8,likely[i]?NULLDS:NOTTAKEN,0);
5836 void sjump_assemble(int i,struct regstat *i_regs)
5838 signed char *i_regmap=i_regs->regmap;
5841 match=match_bt(branch_regs[i].regmap,branch_regs[i].is32,branch_regs[i].dirty,ba[i]);
5842 assem_debug("smatch=%d\n",match);
5844 int prev_cop1_usable=cop1_usable;
5845 int unconditional=0,nevertaken=0;
5848 int internal=internal_branch(branch_regs[i].is32,ba[i]);
5849 if(i==(ba[i]-start)>>2) assem_debug("idle loop\n");
5850 if(!match) invert=1;
5851 #ifdef CORTEX_A8_BRANCH_PREDICTION_HACK
5852 if(i>(ba[i]-start)>>2) invert=1;
5855 //if(opcode2[i]>=0x10) return; // FIXME (BxxZAL)
5856 //assert(opcode2[i]<0x10||rs1[i]==0); // FIXME (BxxZAL)
5859 s1l=get_reg(branch_regs[i].regmap,rs1[i]);
5860 s1h=get_reg(branch_regs[i].regmap,rs1[i]|64);
5863 s1l=get_reg(i_regmap,rs1[i]);
5864 s1h=get_reg(i_regmap,rs1[i]|64);
5868 if(opcode2[i]&1) unconditional=1;
5870 // These are never taken (r0 is never less than zero)
5871 //assert(opcode2[i]!=0);
5872 //assert(opcode2[i]!=2);
5873 //assert(opcode2[i]!=0x10);
5874 //assert(opcode2[i]!=0x12);
5877 only32=(regs[i].was32>>rs1[i])&1;
5881 // Out of order execution (delay slot first)
5883 address_generation(i+1,i_regs,regs[i].regmap_entry);
5884 ds_assemble(i+1,i_regs);
5886 uint64_t bc_unneeded=branch_regs[i].u;
5887 uint64_t bc_unneeded_upper=branch_regs[i].uu;
5888 bc_unneeded&=~((1LL<<rs1[i])|(1LL<<rs2[i]));
5889 bc_unneeded_upper&=~((1LL<<us1[i])|(1LL<<us2[i]));
5891 bc_unneeded_upper|=1;
5892 wb_invalidate(regs[i].regmap,branch_regs[i].regmap,regs[i].dirty,regs[i].is32,
5893 bc_unneeded,bc_unneeded_upper);
5894 load_regs(regs[i].regmap,branch_regs[i].regmap,regs[i].was32,rs1[i],rs1[i]);
5895 load_regs(regs[i].regmap,branch_regs[i].regmap,regs[i].was32,CCREG,CCREG);
5897 int rt,return_address;
5898 rt=get_reg(branch_regs[i].regmap,31);
5899 assem_debug("branch(%d): eax=%d ecx=%d edx=%d ebx=%d ebp=%d esi=%d edi=%d\n",i,branch_regs[i].regmap[0],branch_regs[i].regmap[1],branch_regs[i].regmap[2],branch_regs[i].regmap[3],branch_regs[i].regmap[5],branch_regs[i].regmap[6],branch_regs[i].regmap[7]);
5901 // Save the PC even if the branch is not taken
5902 return_address=start+i*4+8;
5903 emit_movimm(return_address,rt); // PC into link register
5905 if(!nevertaken) emit_prefetch(hash_table[((return_address>>16)^return_address)&0xFFFF]);
5909 cc=get_reg(branch_regs[i].regmap,CCREG);
5910 assert(cc==HOST_CCREG);
5912 store_regs_bt(branch_regs[i].regmap,branch_regs[i].is32,branch_regs[i].dirty,ba[i]);
5913 //do_cc(i,branch_regs[i].regmap,&adj,unconditional?ba[i]:-1,unconditional);
5914 assem_debug("cycle count (adj)\n");
5916 do_cc(i,branch_regs[i].regmap,&adj,ba[i],TAKEN,0);
5917 if(i!=(ba[i]-start)>>2 || source[i+1]!=0) {
5918 if(adj) emit_addimm(cc,CLOCK_DIVIDER*(ccadj[i]+2-adj),cc);
5919 load_regs_bt(branch_regs[i].regmap,branch_regs[i].is32,branch_regs[i].dirty,ba[i]);
5921 assem_debug("branch: internal\n");
5923 assem_debug("branch: external\n");
5924 if(internal&&is_ds[(ba[i]-start)>>2]) {
5925 ds_assemble_entry(i);
5928 add_to_linker((int)out,ba[i],internal);
5931 #ifdef CORTEX_A8_BRANCH_PREDICTION_HACK
5932 if(((u_int)out)&7) emit_addnop(0);
5936 else if(nevertaken) {
5937 emit_addimm_and_set_flags(CLOCK_DIVIDER*(ccadj[i]+2),cc);
5940 add_stub(CC_STUB,jaddr,(int)out,0,i,start+i*4+8,NOTTAKEN,0);
5944 do_cc(i,branch_regs[i].regmap,&adj,-1,0,invert);
5945 if(adj&&!invert) emit_addimm(cc,CLOCK_DIVIDER*(ccadj[i]+2-adj),cc);
5949 if((opcode2[i]&0xf)==0) // BLTZ/BLTZAL
5956 add_to_linker((int)out,ba[i],internal);
5960 if((opcode2[i]&0xf)==1) // BGEZ/BLTZAL
5967 add_to_linker((int)out,ba[i],internal);
5975 if((opcode2[i]&0xf)==0) // BLTZ/BLTZAL
5982 add_to_linker((int)out,ba[i],internal);
5986 if((opcode2[i]&0xf)==1) // BGEZ/BLTZAL
5993 add_to_linker((int)out,ba[i],internal);
6000 #ifdef CORTEX_A8_BRANCH_PREDICTION_HACK
6001 if(match&&(!internal||!is_ds[(ba[i]-start)>>2])) {
6003 emit_addimm(cc,-CLOCK_DIVIDER*adj,cc);
6004 add_to_linker((int)out,ba[i],internal);
6007 add_to_linker((int)out,ba[i],internal*2);
6013 if(adj) emit_addimm(cc,-CLOCK_DIVIDER*adj,cc);
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\n");
6019 assem_debug("branch: external\n");
6020 if(internal&&is_ds[(ba[i]-start)>>2]) {
6021 ds_assemble_entry(i);
6024 add_to_linker((int)out,ba[i],internal);
6028 set_jump_target(nottaken,(int)out);
6032 if(!invert) emit_addimm(cc,CLOCK_DIVIDER*adj,cc);
6034 } // (!unconditional)
6038 // In-order execution (branch first)
6042 int rt,return_address;
6043 rt=get_reg(branch_regs[i].regmap,31);
6045 // Save the PC even if the branch is not taken
6046 return_address=start+i*4+8;
6047 emit_movimm(return_address,rt); // PC into link register
6049 emit_prefetch(hash_table[((return_address>>16)^return_address)&0xFFFF]);
6053 if(!unconditional) {
6054 //printf("branch(%d): eax=%d ecx=%d edx=%d ebx=%d ebp=%d esi=%d edi=%d\n",i,branch_regs[i].regmap[0],branch_regs[i].regmap[1],branch_regs[i].regmap[2],branch_regs[i].regmap[3],branch_regs[i].regmap[5],branch_regs[i].regmap[6],branch_regs[i].regmap[7]);
6058 if((opcode2[i]&0x0d)==0) // BLTZ/BLTZL/BLTZAL/BLTZALL
6064 if((opcode2[i]&0x0d)==1) // BGEZ/BGEZL/BGEZAL/BGEZALL
6074 if((opcode2[i]&0x0d)==0) // BLTZ/BLTZL/BLTZAL/BLTZALL
6080 if((opcode2[i]&0x0d)==1) // BGEZ/BGEZL/BGEZAL/BGEZALL
6087 } // if(!unconditional)
6089 uint64_t ds_unneeded=branch_regs[i].u;
6090 uint64_t ds_unneeded_upper=branch_regs[i].uu;
6091 ds_unneeded&=~((1LL<<rs1[i+1])|(1LL<<rs2[i+1]));
6092 ds_unneeded_upper&=~((1LL<<us1[i+1])|(1LL<<us2[i+1]));
6093 if((~ds_unneeded_upper>>rt1[i+1])&1) ds_unneeded_upper&=~((1LL<<dep1[i+1])|(1LL<<dep2[i+1]));
6095 ds_unneeded_upper|=1;
6098 //assem_debug("1:\n");
6099 wb_invalidate(regs[i].regmap,branch_regs[i].regmap,regs[i].dirty,regs[i].is32,
6100 ds_unneeded,ds_unneeded_upper);
6102 load_regs(regs[i].regmap,branch_regs[i].regmap,regs[i].was32,rs1[i+1],rs2[i+1]);
6103 address_generation(i+1,&branch_regs[i],0);
6104 load_regs(regs[i].regmap,branch_regs[i].regmap,regs[i].was32,CCREG,INVCP);
6105 ds_assemble(i+1,&branch_regs[i]);
6106 cc=get_reg(branch_regs[i].regmap,CCREG);
6108 emit_loadreg(CCREG,cc=HOST_CCREG);
6109 // CHECK: Is the following instruction (fall thru) allocated ok?
6111 assert(cc==HOST_CCREG);
6112 store_regs_bt(branch_regs[i].regmap,branch_regs[i].is32,branch_regs[i].dirty,ba[i]);
6113 do_cc(i,i_regmap,&adj,ba[i],TAKEN,0);
6114 assem_debug("cycle count (adj)\n");
6115 if(adj) emit_addimm(cc,CLOCK_DIVIDER*(ccadj[i]+2-adj),cc);
6116 load_regs_bt(branch_regs[i].regmap,branch_regs[i].is32,branch_regs[i].dirty,ba[i]);
6118 assem_debug("branch: internal\n");
6120 assem_debug("branch: external\n");
6121 if(internal&&is_ds[(ba[i]-start)>>2]) {
6122 ds_assemble_entry(i);
6125 add_to_linker((int)out,ba[i],internal);
6130 cop1_usable=prev_cop1_usable;
6131 if(!unconditional) {
6132 set_jump_target(nottaken,(int)out);
6133 assem_debug("1:\n");
6135 wb_invalidate(regs[i].regmap,branch_regs[i].regmap,regs[i].dirty,regs[i].is32,
6136 ds_unneeded,ds_unneeded_upper);
6137 load_regs(regs[i].regmap,branch_regs[i].regmap,regs[i].was32,rs1[i+1],rs2[i+1]);
6138 address_generation(i+1,&branch_regs[i],0);
6139 load_regs(regs[i].regmap,branch_regs[i].regmap,regs[i].was32,CCREG,CCREG);
6140 ds_assemble(i+1,&branch_regs[i]);
6142 cc=get_reg(branch_regs[i].regmap,CCREG);
6143 if(cc==-1&&!likely[i]) {
6144 // Cycle count isn't in a register, temporarily load it then write it out
6145 emit_loadreg(CCREG,HOST_CCREG);
6146 emit_addimm_and_set_flags(CLOCK_DIVIDER*(ccadj[i]+2),HOST_CCREG);
6149 add_stub(CC_STUB,jaddr,(int)out,0,i,start+i*4+8,NOTTAKEN,0);
6150 emit_storereg(CCREG,HOST_CCREG);
6153 cc=get_reg(i_regmap,CCREG);
6154 assert(cc==HOST_CCREG);
6155 emit_addimm_and_set_flags(CLOCK_DIVIDER*(ccadj[i]+2),cc);
6158 add_stub(CC_STUB,jaddr,(int)out,0,i,start+i*4+8,likely[i]?NULLDS:NOTTAKEN,0);
6164 void fjump_assemble(int i,struct regstat *i_regs)
6166 signed char *i_regmap=i_regs->regmap;
6169 match=match_bt(branch_regs[i].regmap,branch_regs[i].is32,branch_regs[i].dirty,ba[i]);
6170 assem_debug("fmatch=%d\n",match);
6174 int internal=internal_branch(branch_regs[i].is32,ba[i]);
6175 if(i==(ba[i]-start)>>2) assem_debug("idle loop\n");
6176 if(!match) invert=1;
6177 #ifdef CORTEX_A8_BRANCH_PREDICTION_HACK
6178 if(i>(ba[i]-start)>>2) invert=1;
6182 fs=get_reg(branch_regs[i].regmap,FSREG);
6183 address_generation(i+1,i_regs,regs[i].regmap_entry); // Is this okay?
6186 fs=get_reg(i_regmap,FSREG);
6189 // Check cop1 unusable
6191 cs=get_reg(i_regmap,CSREG);
6193 emit_testimm(cs,0x20000000);
6196 add_stub(FP_STUB,eaddr,(int)out,i,cs,(int)i_regs,0,0);
6201 // Out of order execution (delay slot first)
6203 ds_assemble(i+1,i_regs);
6205 uint64_t bc_unneeded=branch_regs[i].u;
6206 uint64_t bc_unneeded_upper=branch_regs[i].uu;
6207 bc_unneeded&=~((1LL<<rs1[i])|(1LL<<rs2[i]));
6208 bc_unneeded_upper&=~((1LL<<us1[i])|(1LL<<us2[i]));
6210 bc_unneeded_upper|=1;
6211 wb_invalidate(regs[i].regmap,branch_regs[i].regmap,regs[i].dirty,regs[i].is32,
6212 bc_unneeded,bc_unneeded_upper);
6213 load_regs(regs[i].regmap,branch_regs[i].regmap,regs[i].was32,rs1[i],rs1[i]);
6214 load_regs(regs[i].regmap,branch_regs[i].regmap,regs[i].was32,CCREG,CCREG);
6215 cc=get_reg(branch_regs[i].regmap,CCREG);
6216 assert(cc==HOST_CCREG);
6217 do_cc(i,branch_regs[i].regmap,&adj,-1,0,invert);
6218 assem_debug("cycle count (adj)\n");
6221 if(adj&&!invert) emit_addimm(cc,CLOCK_DIVIDER*(ccadj[i]+2-adj),cc);
6224 emit_testimm(fs,0x800000);
6225 if(source[i]&0x10000) // BC1T
6231 add_to_linker((int)out,ba[i],internal);
6240 add_to_linker((int)out,ba[i],internal);
6248 if(adj) emit_addimm(cc,-CLOCK_DIVIDER*adj,cc);
6249 #ifdef CORTEX_A8_BRANCH_PREDICTION_HACK
6250 else if(match) emit_addnop(13);
6252 store_regs_bt(branch_regs[i].regmap,branch_regs[i].is32,branch_regs[i].dirty,ba[i]);
6253 load_regs_bt(branch_regs[i].regmap,branch_regs[i].is32,branch_regs[i].dirty,ba[i]);
6255 assem_debug("branch: internal\n");
6257 assem_debug("branch: external\n");
6258 if(internal&&is_ds[(ba[i]-start)>>2]) {
6259 ds_assemble_entry(i);
6262 add_to_linker((int)out,ba[i],internal);
6265 set_jump_target(nottaken,(int)out);
6269 if(!invert) emit_addimm(cc,CLOCK_DIVIDER*adj,cc);
6271 } // (!unconditional)
6275 // In-order execution (branch first)
6279 //printf("branch(%d): eax=%d ecx=%d edx=%d ebx=%d ebp=%d esi=%d edi=%d\n",i,branch_regs[i].regmap[0],branch_regs[i].regmap[1],branch_regs[i].regmap[2],branch_regs[i].regmap[3],branch_regs[i].regmap[5],branch_regs[i].regmap[6],branch_regs[i].regmap[7]);
6282 emit_testimm(fs,0x800000);
6283 if(source[i]&0x10000) // BC1T
6294 } // if(!unconditional)
6296 uint64_t ds_unneeded=branch_regs[i].u;
6297 uint64_t ds_unneeded_upper=branch_regs[i].uu;
6298 ds_unneeded&=~((1LL<<rs1[i+1])|(1LL<<rs2[i+1]));
6299 ds_unneeded_upper&=~((1LL<<us1[i+1])|(1LL<<us2[i+1]));
6300 if((~ds_unneeded_upper>>rt1[i+1])&1) ds_unneeded_upper&=~((1LL<<dep1[i+1])|(1LL<<dep2[i+1]));
6302 ds_unneeded_upper|=1;
6304 //assem_debug("1:\n");
6305 wb_invalidate(regs[i].regmap,branch_regs[i].regmap,regs[i].dirty,regs[i].is32,
6306 ds_unneeded,ds_unneeded_upper);
6308 load_regs(regs[i].regmap,branch_regs[i].regmap,regs[i].was32,rs1[i+1],rs2[i+1]);
6309 address_generation(i+1,&branch_regs[i],0);
6310 load_regs(regs[i].regmap,branch_regs[i].regmap,regs[i].was32,CCREG,INVCP);
6311 ds_assemble(i+1,&branch_regs[i]);
6312 cc=get_reg(branch_regs[i].regmap,CCREG);
6314 emit_loadreg(CCREG,cc=HOST_CCREG);
6315 // CHECK: Is the following instruction (fall thru) allocated ok?
6317 assert(cc==HOST_CCREG);
6318 store_regs_bt(branch_regs[i].regmap,branch_regs[i].is32,branch_regs[i].dirty,ba[i]);
6319 do_cc(i,i_regmap,&adj,ba[i],TAKEN,0);
6320 assem_debug("cycle count (adj)\n");
6321 if(adj) emit_addimm(cc,CLOCK_DIVIDER*(ccadj[i]+2-adj),cc);
6322 load_regs_bt(branch_regs[i].regmap,branch_regs[i].is32,branch_regs[i].dirty,ba[i]);
6324 assem_debug("branch: internal\n");
6326 assem_debug("branch: external\n");
6327 if(internal&&is_ds[(ba[i]-start)>>2]) {
6328 ds_assemble_entry(i);
6331 add_to_linker((int)out,ba[i],internal);
6336 if(1) { // <- FIXME (don't need this)
6337 set_jump_target(nottaken,(int)out);
6338 assem_debug("1:\n");
6340 wb_invalidate(regs[i].regmap,branch_regs[i].regmap,regs[i].dirty,regs[i].is32,
6341 ds_unneeded,ds_unneeded_upper);
6342 load_regs(regs[i].regmap,branch_regs[i].regmap,regs[i].was32,rs1[i+1],rs2[i+1]);
6343 address_generation(i+1,&branch_regs[i],0);
6344 load_regs(regs[i].regmap,branch_regs[i].regmap,regs[i].was32,CCREG,CCREG);
6345 ds_assemble(i+1,&branch_regs[i]);
6347 cc=get_reg(branch_regs[i].regmap,CCREG);
6348 if(cc==-1&&!likely[i]) {
6349 // Cycle count isn't in a register, temporarily load it then write it out
6350 emit_loadreg(CCREG,HOST_CCREG);
6351 emit_addimm_and_set_flags(CLOCK_DIVIDER*(ccadj[i]+2),HOST_CCREG);
6354 add_stub(CC_STUB,jaddr,(int)out,0,i,start+i*4+8,NOTTAKEN,0);
6355 emit_storereg(CCREG,HOST_CCREG);
6358 cc=get_reg(i_regmap,CCREG);
6359 assert(cc==HOST_CCREG);
6360 emit_addimm_and_set_flags(CLOCK_DIVIDER*(ccadj[i]+2),cc);
6363 add_stub(CC_STUB,jaddr,(int)out,0,i,start+i*4+8,likely[i]?NULLDS:NOTTAKEN,0);
6369 static void pagespan_assemble(int i,struct regstat *i_regs)
6371 int s1l=get_reg(i_regs->regmap,rs1[i]);
6372 int s1h=get_reg(i_regs->regmap,rs1[i]|64);
6373 int s2l=get_reg(i_regs->regmap,rs2[i]);
6374 int s2h=get_reg(i_regs->regmap,rs2[i]|64);
6375 void *nt_branch=NULL;
6378 int unconditional=0;
6388 if((i_regs->is32>>rs1[i])&(i_regs->is32>>rs2[i])&1) {
6392 int addr,alt,ntaddr;
6393 if(i_regs->regmap[HOST_BTREG]<0) {addr=HOST_BTREG;}
6397 if(hr!=EXCLUDE_REG && hr!=HOST_CCREG &&
6398 (i_regs->regmap[hr]&63)!=rs1[i] &&
6399 (i_regs->regmap[hr]&63)!=rs2[i] )
6408 if(hr!=EXCLUDE_REG && hr!=HOST_CCREG && hr!=HOST_BTREG &&
6409 (i_regs->regmap[hr]&63)!=rs1[i] &&
6410 (i_regs->regmap[hr]&63)!=rs2[i] )
6416 if((opcode[i]&0x2E)==6) // BLEZ/BGTZ needs another register
6420 if(hr!=EXCLUDE_REG && hr!=HOST_CCREG && hr!=HOST_BTREG &&
6421 (i_regs->regmap[hr]&63)!=rs1[i] &&
6422 (i_regs->regmap[hr]&63)!=rs2[i] )
6429 assert(hr<HOST_REGS);
6430 if((opcode[i]&0x2e)==4||opcode[i]==0x11) { // BEQ/BNE/BEQL/BNEL/BC1
6431 load_regs(regs[i].regmap_entry,regs[i].regmap,regs[i].was32,CCREG,CCREG);
6433 emit_addimm(HOST_CCREG,CLOCK_DIVIDER*(ccadj[i]+2),HOST_CCREG);
6434 if(opcode[i]==2) // J
6438 if(opcode[i]==3) // JAL
6441 int rt=get_reg(i_regs->regmap,31);
6442 emit_movimm(start+i*4+8,rt);
6445 if(opcode[i]==0&&(opcode2[i]&0x3E)==8) // JR/JALR
6448 if(opcode2[i]==9) // JALR
6450 int rt=get_reg(i_regs->regmap,rt1[i]);
6451 emit_movimm(start+i*4+8,rt);
6454 if((opcode[i]&0x3f)==4) // BEQ
6461 #ifdef HAVE_CMOV_IMM
6463 if(s2l>=0) emit_cmp(s1l,s2l);
6464 else emit_test(s1l,s1l);
6465 emit_cmov2imm_e_ne_compact(ba[i],start+i*4+8,addr);
6471 emit_mov2imm_compact(ba[i],addr,start+i*4+8,alt);
6473 if(s2h>=0) emit_cmp(s1h,s2h);
6474 else emit_test(s1h,s1h);
6475 emit_cmovne_reg(alt,addr);
6477 if(s2l>=0) emit_cmp(s1l,s2l);
6478 else emit_test(s1l,s1l);
6479 emit_cmovne_reg(alt,addr);
6482 if((opcode[i]&0x3f)==5) // BNE
6484 #ifdef HAVE_CMOV_IMM
6486 if(s2l>=0) emit_cmp(s1l,s2l);
6487 else emit_test(s1l,s1l);
6488 emit_cmov2imm_e_ne_compact(start+i*4+8,ba[i],addr);
6494 emit_mov2imm_compact(start+i*4+8,addr,ba[i],alt);
6496 if(s2h>=0) emit_cmp(s1h,s2h);
6497 else emit_test(s1h,s1h);
6498 emit_cmovne_reg(alt,addr);
6500 if(s2l>=0) emit_cmp(s1l,s2l);
6501 else emit_test(s1l,s1l);
6502 emit_cmovne_reg(alt,addr);
6505 if((opcode[i]&0x3f)==0x14) // BEQL
6508 if(s2h>=0) emit_cmp(s1h,s2h);
6509 else emit_test(s1h,s1h);
6513 if(s2l>=0) emit_cmp(s1l,s2l);
6514 else emit_test(s1l,s1l);
6515 if(nottaken) set_jump_target(nottaken,(int)out);
6519 if((opcode[i]&0x3f)==0x15) // BNEL
6522 if(s2h>=0) emit_cmp(s1h,s2h);
6523 else emit_test(s1h,s1h);
6527 if(s2l>=0) emit_cmp(s1l,s2l);
6528 else emit_test(s1l,s1l);
6531 if(taken) set_jump_target(taken,(int)out);
6533 if((opcode[i]&0x3f)==6) // BLEZ
6535 emit_mov2imm_compact(ba[i],alt,start+i*4+8,addr);
6537 if(s1h>=0) emit_mov(addr,ntaddr);
6538 emit_cmovl_reg(alt,addr);
6541 emit_cmovne_reg(ntaddr,addr);
6542 emit_cmovs_reg(alt,addr);
6545 if((opcode[i]&0x3f)==7) // BGTZ
6547 emit_mov2imm_compact(ba[i],addr,start+i*4+8,ntaddr);
6549 if(s1h>=0) emit_mov(addr,alt);
6550 emit_cmovl_reg(ntaddr,addr);
6553 emit_cmovne_reg(alt,addr);
6554 emit_cmovs_reg(ntaddr,addr);
6557 if((opcode[i]&0x3f)==0x16) // BLEZL
6559 assert((opcode[i]&0x3f)!=0x16);
6561 if((opcode[i]&0x3f)==0x17) // BGTZL
6563 assert((opcode[i]&0x3f)!=0x17);
6565 assert(opcode[i]!=1); // BLTZ/BGEZ
6567 //FIXME: Check CSREG
6568 if(opcode[i]==0x11 && opcode2[i]==0x08 ) {
6569 if((source[i]&0x30000)==0) // BC1F
6571 emit_mov2imm_compact(ba[i],addr,start+i*4+8,alt);
6572 emit_testimm(s1l,0x800000);
6573 emit_cmovne_reg(alt,addr);
6575 if((source[i]&0x30000)==0x10000) // BC1T
6577 emit_mov2imm_compact(ba[i],alt,start+i*4+8,addr);
6578 emit_testimm(s1l,0x800000);
6579 emit_cmovne_reg(alt,addr);
6581 if((source[i]&0x30000)==0x20000) // BC1FL
6583 emit_testimm(s1l,0x800000);
6587 if((source[i]&0x30000)==0x30000) // BC1TL
6589 emit_testimm(s1l,0x800000);
6595 assert(i_regs->regmap[HOST_CCREG]==CCREG);
6596 wb_dirtys(regs[i].regmap,regs[i].is32,regs[i].dirty);
6597 if(likely[i]||unconditional)
6599 emit_movimm(ba[i],HOST_BTREG);
6601 else if(addr!=HOST_BTREG)
6603 emit_mov(addr,HOST_BTREG);
6605 void *branch_addr=out;
6607 int target_addr=start+i*4+5;
6609 void *compiled_target_addr=check_addr(target_addr);
6610 emit_extjump_ds((int)branch_addr,target_addr);
6611 if(compiled_target_addr) {
6612 set_jump_target((int)branch_addr,(int)compiled_target_addr);
6613 add_link(target_addr,stub);
6615 else set_jump_target((int)branch_addr,(int)stub);
6618 set_jump_target((int)nottaken,(int)out);
6619 wb_dirtys(regs[i].regmap,regs[i].is32,regs[i].dirty);
6620 void *branch_addr=out;
6622 int target_addr=start+i*4+8;
6624 void *compiled_target_addr=check_addr(target_addr);
6625 emit_extjump_ds((int)branch_addr,target_addr);
6626 if(compiled_target_addr) {
6627 set_jump_target((int)branch_addr,(int)compiled_target_addr);
6628 add_link(target_addr,stub);
6630 else set_jump_target((int)branch_addr,(int)stub);
6634 // Assemble the delay slot for the above
6635 static void pagespan_ds()
6637 assem_debug("initial delay slot:\n");
6638 u_int vaddr=start+1;
6639 u_int page=get_page(vaddr);
6640 u_int vpage=get_vpage(vaddr);
6641 ll_add(jump_dirty+vpage,vaddr,(void *)out);
6643 ll_add(jump_in+page,vaddr,(void *)out);
6644 assert(regs[0].regmap_entry[HOST_CCREG]==CCREG);
6645 if(regs[0].regmap[HOST_CCREG]!=CCREG)
6646 wb_register(CCREG,regs[0].regmap_entry,regs[0].wasdirty,regs[0].was32);
6647 if(regs[0].regmap[HOST_BTREG]!=BTREG)
6648 emit_writeword(HOST_BTREG,(int)&branch_target);
6649 load_regs(regs[0].regmap_entry,regs[0].regmap,regs[0].was32,rs1[0],rs2[0]);
6650 address_generation(0,®s[0],regs[0].regmap_entry);
6651 if(itype[0]==STORE||itype[0]==STORELR||(opcode[0]&0x3b)==0x39||(opcode[0]&0x3b)==0x3a)
6652 load_regs(regs[0].regmap_entry,regs[0].regmap,regs[0].was32,INVCP,INVCP);
6657 alu_assemble(0,®s[0]);break;
6659 imm16_assemble(0,®s[0]);break;
6661 shift_assemble(0,®s[0]);break;
6663 shiftimm_assemble(0,®s[0]);break;
6665 load_assemble(0,®s[0]);break;
6667 loadlr_assemble(0,®s[0]);break;
6669 store_assemble(0,®s[0]);break;
6671 storelr_assemble(0,®s[0]);break;
6673 cop0_assemble(0,®s[0]);break;
6675 cop1_assemble(0,®s[0]);break;
6677 c1ls_assemble(0,®s[0]);break;
6679 cop2_assemble(0,®s[0]);break;
6681 c2ls_assemble(0,®s[0]);break;
6683 c2op_assemble(0,®s[0]);break;
6685 fconv_assemble(0,®s[0]);break;
6687 float_assemble(0,®s[0]);break;
6689 fcomp_assemble(0,®s[0]);break;
6691 multdiv_assemble(0,®s[0]);break;
6693 mov_assemble(0,®s[0]);break;
6703 printf("Jump in the delay slot. This is probably a bug.\n");
6705 int btaddr=get_reg(regs[0].regmap,BTREG);
6707 btaddr=get_reg(regs[0].regmap,-1);
6708 emit_readword((int)&branch_target,btaddr);
6710 assert(btaddr!=HOST_CCREG);
6711 if(regs[0].regmap[HOST_CCREG]!=CCREG) emit_loadreg(CCREG,HOST_CCREG);
6713 emit_movimm(start+4,HOST_TEMPREG);
6714 emit_cmp(btaddr,HOST_TEMPREG);
6716 emit_cmpimm(btaddr,start+4);
6718 int branch=(int)out;
6720 store_regs_bt(regs[0].regmap,regs[0].is32,regs[0].dirty,-1);
6721 emit_jmp(jump_vaddr_reg[btaddr]);
6722 set_jump_target(branch,(int)out);
6723 store_regs_bt(regs[0].regmap,regs[0].is32,regs[0].dirty,start+4);
6724 load_regs_bt(regs[0].regmap,regs[0].is32,regs[0].dirty,start+4);
6727 // Basic liveness analysis for MIPS registers
6728 void unneeded_registers(int istart,int iend,int r)
6731 uint64_t u,uu,gte_u,b,bu,gte_bu;
6732 uint64_t temp_u,temp_uu,temp_gte_u;
6737 u=unneeded_reg[iend+1];
6738 uu=unneeded_reg_upper[iend+1];
6743 for (i=iend;i>=istart;i--)
6745 //printf("unneeded registers i=%d (%d,%d) r=%d\n",i,istart,iend,r);
6746 if(itype[i]==RJUMP||itype[i]==UJUMP||itype[i]==CJUMP||itype[i]==SJUMP||itype[i]==FJUMP)
6748 // If subroutine call, flag return address as a possible branch target
6749 if(rt1[i]==31 && i<slen-2) bt[i+2]=1;
6751 if(ba[i]<start || ba[i]>=(start+slen*4))
6753 // Branch out of this block, flush all regs
6758 if(itype[i]==UJUMP&&rt1[i]==31)
6760 uu=u=0x300C00F; // Discard at, v0-v1, t6-t9
6762 if(itype[i]==RJUMP&&rs1[i]==31)
6764 uu=u=0x300C0F3; // Discard at, a0-a3, t6-t9
6766 if(start>0x80000400&&start<0x80000000+RAM_SIZE) {
6767 if(itype[i]==UJUMP&&rt1[i]==31)
6769 //uu=u=0x30300FF0FLL; // Discard at, v0-v1, t0-t9, lo, hi
6770 uu=u=0x300FF0F; // Discard at, v0-v1, t0-t9
6772 if(itype[i]==RJUMP&&rs1[i]==31)
6774 //uu=u=0x30300FFF3LL; // Discard at, a0-a3, t0-t9, lo, hi
6775 uu=u=0x300FFF3; // Discard at, a0-a3, t0-t9
6778 branch_unneeded_reg[i]=u;
6779 branch_unneeded_reg_upper[i]=uu;
6780 // Merge in delay slot
6781 tdep=(~uu>>rt1[i+1])&1;
6782 u|=(1LL<<rt1[i+1])|(1LL<<rt2[i+1]);
6783 uu|=(1LL<<rt1[i+1])|(1LL<<rt2[i+1]);
6784 u&=~((1LL<<rs1[i+1])|(1LL<<rs2[i+1]));
6785 uu&=~((1LL<<us1[i+1])|(1LL<<us2[i+1]));
6786 uu&=~((tdep<<dep1[i+1])|(tdep<<dep2[i+1]));
6789 gte_u&=~gte_rs[i+1];
6790 // If branch is "likely" (and conditional)
6791 // then we skip the delay slot on the fall-thru path
6794 u&=unneeded_reg[i+2];
6795 uu&=unneeded_reg_upper[i+2];
6796 gte_u&=gte_unneeded[i+2];
6808 // Internal branch, flag target
6809 bt[(ba[i]-start)>>2]=1;
6810 if(ba[i]<=start+i*4) {
6812 if(itype[i]==RJUMP||itype[i]==UJUMP||(source[i]>>16)==0x1000)
6814 // Unconditional branch
6818 // Conditional branch (not taken case)
6819 temp_u=unneeded_reg[i+2];
6820 temp_uu=unneeded_reg_upper[i+2];
6821 temp_gte_u&=gte_unneeded[i+2];
6823 // Merge in delay slot
6824 tdep=(~temp_uu>>rt1[i+1])&1;
6825 temp_u|=(1LL<<rt1[i+1])|(1LL<<rt2[i+1]);
6826 temp_uu|=(1LL<<rt1[i+1])|(1LL<<rt2[i+1]);
6827 temp_u&=~((1LL<<rs1[i+1])|(1LL<<rs2[i+1]));
6828 temp_uu&=~((1LL<<us1[i+1])|(1LL<<us2[i+1]));
6829 temp_uu&=~((tdep<<dep1[i+1])|(tdep<<dep2[i+1]));
6830 temp_u|=1;temp_uu|=1;
6831 temp_gte_u|=gte_rt[i+1];
6832 temp_gte_u&=~gte_rs[i+1];
6833 // If branch is "likely" (and conditional)
6834 // then we skip the delay slot on the fall-thru path
6837 temp_u&=unneeded_reg[i+2];
6838 temp_uu&=unneeded_reg_upper[i+2];
6839 temp_gte_u&=gte_unneeded[i+2];
6848 tdep=(~temp_uu>>rt1[i])&1;
6849 temp_u|=(1LL<<rt1[i])|(1LL<<rt2[i]);
6850 temp_uu|=(1LL<<rt1[i])|(1LL<<rt2[i]);
6851 temp_u&=~((1LL<<rs1[i])|(1LL<<rs2[i]));
6852 temp_uu&=~((1LL<<us1[i])|(1LL<<us2[i]));
6853 temp_uu&=~((tdep<<dep1[i])|(tdep<<dep2[i]));
6854 temp_u|=1;temp_uu|=1;
6855 temp_gte_u|=gte_rt[i];
6856 temp_gte_u&=~gte_rs[i];
6857 unneeded_reg[i]=temp_u;
6858 unneeded_reg_upper[i]=temp_uu;
6859 gte_unneeded[i]=temp_gte_u;
6860 // Only go three levels deep. This recursion can take an
6861 // excessive amount of time if there are a lot of nested loops.
6863 unneeded_registers((ba[i]-start)>>2,i-1,r+1);
6865 unneeded_reg[(ba[i]-start)>>2]=1;
6866 unneeded_reg_upper[(ba[i]-start)>>2]=1;
6867 gte_unneeded[(ba[i]-start)>>2]=0;
6870 if(itype[i]==RJUMP||itype[i]==UJUMP||(source[i]>>16)==0x1000)
6872 // Unconditional branch
6873 u=unneeded_reg[(ba[i]-start)>>2];
6874 uu=unneeded_reg_upper[(ba[i]-start)>>2];
6875 gte_u=gte_unneeded[(ba[i]-start)>>2];
6876 branch_unneeded_reg[i]=u;
6877 branch_unneeded_reg_upper[i]=uu;
6880 //branch_unneeded_reg[i]=u;
6881 //branch_unneeded_reg_upper[i]=uu;
6882 // Merge in delay slot
6883 tdep=(~uu>>rt1[i+1])&1;
6884 u|=(1LL<<rt1[i+1])|(1LL<<rt2[i+1]);
6885 uu|=(1LL<<rt1[i+1])|(1LL<<rt2[i+1]);
6886 u&=~((1LL<<rs1[i+1])|(1LL<<rs2[i+1]));
6887 uu&=~((1LL<<us1[i+1])|(1LL<<us2[i+1]));
6888 uu&=~((tdep<<dep1[i+1])|(tdep<<dep2[i+1]));
6891 gte_u&=~gte_rs[i+1];
6893 // Conditional branch
6894 b=unneeded_reg[(ba[i]-start)>>2];
6895 bu=unneeded_reg_upper[(ba[i]-start)>>2];
6896 gte_bu=gte_unneeded[(ba[i]-start)>>2];
6897 branch_unneeded_reg[i]=b;
6898 branch_unneeded_reg_upper[i]=bu;
6901 //branch_unneeded_reg[i]=b;
6902 //branch_unneeded_reg_upper[i]=bu;
6903 // Branch delay slot
6904 tdep=(~uu>>rt1[i+1])&1;
6905 b|=(1LL<<rt1[i+1])|(1LL<<rt2[i+1]);
6906 bu|=(1LL<<rt1[i+1])|(1LL<<rt2[i+1]);
6907 b&=~((1LL<<rs1[i+1])|(1LL<<rs2[i+1]));
6908 bu&=~((1LL<<us1[i+1])|(1LL<<us2[i+1]));
6909 bu&=~((tdep<<dep1[i+1])|(tdep<<dep2[i+1]));
6911 gte_bu|=gte_rt[i+1];
6912 gte_bu&=~gte_rs[i+1];
6913 // If branch is "likely" then we skip the
6914 // delay slot on the fall-thru path
6920 u&=unneeded_reg[i+2];
6921 uu&=unneeded_reg_upper[i+2];
6922 gte_u&=gte_unneeded[i+2];
6934 branch_unneeded_reg[i]&=unneeded_reg[i+2];
6935 branch_unneeded_reg_upper[i]&=unneeded_reg_upper[i+2];
6936 //branch_unneeded_reg[i]=1;
6937 //branch_unneeded_reg_upper[i]=1;
6939 branch_unneeded_reg[i]=1;
6940 branch_unneeded_reg_upper[i]=1;
6946 else if(itype[i]==SYSCALL||itype[i]==HLECALL||itype[i]==INTCALL)
6948 // SYSCALL instruction (software interrupt)
6952 else if(itype[i]==COP0 && (source[i]&0x3f)==0x18)
6954 // ERET instruction (return from interrupt)
6959 tdep=(~uu>>rt1[i])&1;
6960 // Written registers are unneeded
6966 // Accessed registers are needed
6972 // Source-target dependencies
6973 uu&=~(tdep<<dep1[i]);
6974 uu&=~(tdep<<dep2[i]);
6975 // R0 is always unneeded
6979 unneeded_reg_upper[i]=uu;
6980 gte_unneeded[i]=gte_u;
6982 printf("ur (%d,%d) %x: ",istart,iend,start+i*4);
6985 for(r=1;r<=CCREG;r++) {
6986 if((unneeded_reg[i]>>r)&1) {
6987 if(r==HIREG) printf(" HI");
6988 else if(r==LOREG) printf(" LO");
6989 else printf(" r%d",r);
6993 for(r=1;r<=CCREG;r++) {
6994 if(((unneeded_reg_upper[i]&~unneeded_reg[i])>>r)&1) {
6995 if(r==HIREG) printf(" HI");
6996 else if(r==LOREG) printf(" LO");
6997 else printf(" r%d",r);
7003 for (i=iend;i>=istart;i--)
7005 unneeded_reg_upper[i]=branch_unneeded_reg_upper[i]=-1LL;
7010 // Identify registers which are likely to contain 32-bit values
7011 // This is used to predict whether any branches will jump to a
7012 // location with 64-bit values in registers.
7013 static void provisional_32bit()
7017 uint64_t lastbranch=1;
7022 if(itype[i-1]==CJUMP||itype[i-1]==SJUMP||itype[i-1]==FJUMP) {
7023 if(i>1) is32=lastbranch;
7029 if(itype[i-2]==CJUMP||itype[i-2]==SJUMP||itype[i-2]==FJUMP) {
7031 if(i>2) is32=lastbranch;
7035 if((opcode[i-2]&0x2f)==0x05) // BNE/BNEL
7037 if(rs1[i-2]==0||rs2[i-2]==0)
7040 is32|=1LL<<rs1[i-2];
7043 is32|=1LL<<rs2[i-2];
7048 // If something jumps here with 64-bit values
7049 // then promote those registers to 64 bits
7052 uint64_t temp_is32=is32;
7055 if(ba[j]==start+i*4)
7056 //temp_is32&=branch_regs[j].is32;
7061 if(ba[j]==start+i*4)
7072 if(type==UJUMP||type==RJUMP||type==CJUMP||type==SJUMP||type==FJUMP) {
7073 // Branches don't write registers, consider the delay slot instead.
7084 if(opcode[i]==0x27||opcode[i]==0x37|| // LWU/LD
7085 opcode[i]==0x1A||opcode[i]==0x1B) // LDL/LDR
7094 if(op==0x1a||op==0x1b) is32&=~(1LL<<rt); // LDR/LDL
7095 if(op==0x22) is32|=1LL<<rt; // LWL
7098 if (op==0x08||op==0x09|| // ADDI/ADDIU
7099 op==0x0a||op==0x0b|| // SLTI/SLTIU
7105 if(op==0x18||op==0x19) { // DADDI/DADDIU
7108 // is32|=((is32>>s1)&1LL)<<rt;
7110 if(op==0x0d||op==0x0e) { // ORI/XORI
7111 uint64_t sr=((is32>>s1)&1LL);
7127 if(op2>=0x20&&op2<=0x23) { // ADD/ADDU/SUB/SUBU
7130 if(op2==0x2a||op2==0x2b) { // SLT/SLTU
7133 else if(op2>=0x24&&op2<=0x27) { // AND/OR/XOR/NOR
7134 uint64_t sr=((is32>>s1)&(is32>>s2)&1LL);
7138 else if(op2>=0x2c&&op2<=0x2d) { // DADD/DADDU
7143 uint64_t sr=((is32>>s1)&1LL);
7148 uint64_t sr=((is32>>s2)&1LL);
7156 else if(op2>=0x2e&&op2<=0x2f) { // DSUB/DSUBU
7161 uint64_t sr=((is32>>s1)&1LL);
7171 if (op2>=0x1c&&op2<=0x1f) { // DMULT/DMULTU/DDIV/DDIVU
7172 is32&=~((1LL<<HIREG)|(1LL<<LOREG));
7175 is32|=(1LL<<HIREG)|(1LL<<LOREG);
7180 uint64_t sr=((is32>>s1)&1LL);
7186 if(op2>=0x14&&op2<=0x17) is32&=~(1LL<<rt); // DSLLV/DSRLV/DSRAV
7187 else is32|=1LL<<rt; // SLLV/SRLV/SRAV
7191 // DSLL/DSRL/DSRA/DSLL32/DSRL32 but not DSRA32 have 64-bit result
7192 if(op2>=0x38&&op2<0x3f) is32&=~(1LL<<rt);
7195 if(op2==0) is32|=1LL<<rt; // MFC0
7199 if(op2==0) is32|=1LL<<rt; // MFC1
7200 if(op2==1) is32&=~(1LL<<rt); // DMFC1
7201 if(op2==2) is32|=1LL<<rt; // CFC1
7223 if(itype[i-1]==UJUMP||itype[i-1]==RJUMP||(source[i-1]>>16)==0x1000)
7225 if(rt1[i-1]==31) // JAL/JALR
7227 // Subroutine call will return here, don't alloc any registers
7232 // Internal branch will jump here, match registers to caller
7240 // Identify registers which may be assumed to contain 32-bit values
7241 // and where optimizations will rely on this.
7242 // This is used to determine whether backward branches can safely
7243 // jump to a location with 64-bit values in registers.
7244 static void provisional_r32()
7249 for (i=slen-1;i>=0;i--)
7252 if(itype[i]==RJUMP||itype[i]==UJUMP||itype[i]==CJUMP||itype[i]==SJUMP||itype[i]==FJUMP)
7254 if(ba[i]<start || ba[i]>=(start+slen*4))
7256 // Branch out of this block, don't need anything
7262 // Need whatever matches the target
7263 // (and doesn't get overwritten by the delay slot instruction)
7265 int t=(ba[i]-start)>>2;
7266 if(ba[i]>start+i*4) {
7268 //if(!(requires_32bit[t]&~regs[i].was32))
7269 // r32|=requires_32bit[t]&(~(1LL<<rt1[i+1]))&(~(1LL<<rt2[i+1]));
7270 if(!(pr32[t]&~regs[i].was32))
7271 r32|=pr32[t]&(~(1LL<<rt1[i+1]))&(~(1LL<<rt2[i+1]));
7274 if(!(regs[t].was32&~unneeded_reg_upper[t]&~regs[i].was32))
7275 r32|=regs[t].was32&~unneeded_reg_upper[t]&(~(1LL<<rt1[i+1]))&(~(1LL<<rt2[i+1]));
7278 // Conditional branch may need registers for following instructions
7279 if(itype[i]!=RJUMP&&itype[i]!=UJUMP&&(source[i]>>16)!=0x1000)
7282 //r32|=requires_32bit[i+2];
7285 // Mark this address as a branch target since it may be called
7286 // upon return from interrupt
7290 // Merge in delay slot
7292 // These are overwritten unless the branch is "likely"
7293 // and the delay slot is nullified if not taken
7294 r32&=~(1LL<<rt1[i+1]);
7295 r32&=~(1LL<<rt2[i+1]);
7297 // Assume these are needed (delay slot)
7300 if((regs[i].was32>>us1[i+1])&1) r32|=1LL<<us1[i+1];
7304 if((regs[i].was32>>us2[i+1])&1) r32|=1LL<<us2[i+1];
7306 if(dep1[i+1]&&!((unneeded_reg_upper[i]>>dep1[i+1])&1))
7308 if((regs[i].was32>>dep1[i+1])&1) r32|=1LL<<dep1[i+1];
7310 if(dep2[i+1]&&!((unneeded_reg_upper[i]>>dep2[i+1])&1))
7312 if((regs[i].was32>>dep2[i+1])&1) r32|=1LL<<dep2[i+1];
7315 else if(itype[i]==SYSCALL||itype[i]==HLECALL||itype[i]==INTCALL)
7317 // SYSCALL instruction (software interrupt)
7320 else if(itype[i]==COP0 && (source[i]&0x3f)==0x18)
7322 // ERET instruction (return from interrupt)
7326 r32&=~(1LL<<rt1[i]);
7327 r32&=~(1LL<<rt2[i]);
7330 if((regs[i].was32>>us1[i])&1) r32|=1LL<<us1[i];
7334 if((regs[i].was32>>us2[i])&1) r32|=1LL<<us2[i];
7336 if(dep1[i]&&!((unneeded_reg_upper[i]>>dep1[i])&1))
7338 if((regs[i].was32>>dep1[i])&1) r32|=1LL<<dep1[i];
7340 if(dep2[i]&&!((unneeded_reg_upper[i]>>dep2[i])&1))
7342 if((regs[i].was32>>dep2[i])&1) r32|=1LL<<dep2[i];
7344 //requires_32bit[i]=r32;
7347 // Dirty registers which are 32-bit, require 32-bit input
7348 // as they will be written as 32-bit values
7349 for(hr=0;hr<HOST_REGS;hr++)
7351 if(regs[i].regmap_entry[hr]>0&®s[i].regmap_entry[hr]<64) {
7352 if((regs[i].was32>>regs[i].regmap_entry[hr])&(regs[i].wasdirty>>hr)&1) {
7353 if(!((unneeded_reg_upper[i]>>regs[i].regmap_entry[hr])&1))
7354 pr32[i]|=1LL<<regs[i].regmap_entry[hr];
7355 //requires_32bit[i]|=1LL<<regs[i].regmap_entry[hr];
7362 // Write back dirty registers as soon as we will no longer modify them,
7363 // so that we don't end up with lots of writes at the branches.
7364 void clean_registers(int istart,int iend,int wr)
7368 u_int will_dirty_i,will_dirty_next,temp_will_dirty;
7369 u_int wont_dirty_i,wont_dirty_next,temp_wont_dirty;
7371 will_dirty_i=will_dirty_next=0;
7372 wont_dirty_i=wont_dirty_next=0;
7374 will_dirty_i=will_dirty_next=will_dirty[iend+1];
7375 wont_dirty_i=wont_dirty_next=wont_dirty[iend+1];
7377 for (i=iend;i>=istart;i--)
7379 if(itype[i]==RJUMP||itype[i]==UJUMP||itype[i]==CJUMP||itype[i]==SJUMP||itype[i]==FJUMP)
7381 if(ba[i]<start || ba[i]>=(start+slen*4))
7383 // Branch out of this block, flush all regs
7384 if(itype[i]==RJUMP||itype[i]==UJUMP||(source[i]>>16)==0x1000)
7386 // Unconditional branch
7389 // Merge in delay slot (will dirty)
7390 for(r=0;r<HOST_REGS;r++) {
7391 if(r!=EXCLUDE_REG) {
7392 if((branch_regs[i].regmap[r]&63)==rt1[i]) will_dirty_i|=1<<r;
7393 if((branch_regs[i].regmap[r]&63)==rt2[i]) will_dirty_i|=1<<r;
7394 if((branch_regs[i].regmap[r]&63)==rt1[i+1]) will_dirty_i|=1<<r;
7395 if((branch_regs[i].regmap[r]&63)==rt2[i+1]) will_dirty_i|=1<<r;
7396 if((branch_regs[i].regmap[r]&63)>33) will_dirty_i&=~(1<<r);
7397 if(branch_regs[i].regmap[r]<=0) will_dirty_i&=~(1<<r);
7398 if(branch_regs[i].regmap[r]==CCREG) will_dirty_i|=1<<r;
7399 if((regs[i].regmap[r]&63)==rt1[i]) will_dirty_i|=1<<r;
7400 if((regs[i].regmap[r]&63)==rt2[i]) will_dirty_i|=1<<r;
7401 if((regs[i].regmap[r]&63)==rt1[i+1]) will_dirty_i|=1<<r;
7402 if((regs[i].regmap[r]&63)==rt2[i+1]) will_dirty_i|=1<<r;
7403 if((regs[i].regmap[r]&63)>33) will_dirty_i&=~(1<<r);
7404 if(regs[i].regmap[r]<=0) will_dirty_i&=~(1<<r);
7405 if(regs[i].regmap[r]==CCREG) will_dirty_i|=1<<r;
7411 // Conditional branch
7413 wont_dirty_i=wont_dirty_next;
7414 // Merge in delay slot (will dirty)
7415 for(r=0;r<HOST_REGS;r++) {
7416 if(r!=EXCLUDE_REG) {
7418 // Might not dirty if likely branch is not taken
7419 if((branch_regs[i].regmap[r]&63)==rt1[i]) will_dirty_i|=1<<r;
7420 if((branch_regs[i].regmap[r]&63)==rt2[i]) will_dirty_i|=1<<r;
7421 if((branch_regs[i].regmap[r]&63)==rt1[i+1]) will_dirty_i|=1<<r;
7422 if((branch_regs[i].regmap[r]&63)==rt2[i+1]) will_dirty_i|=1<<r;
7423 if((branch_regs[i].regmap[r]&63)>33) will_dirty_i&=~(1<<r);
7424 if(branch_regs[i].regmap[r]==0) will_dirty_i&=~(1<<r);
7425 if(branch_regs[i].regmap[r]==CCREG) will_dirty_i|=1<<r;
7426 //if((regs[i].regmap[r]&63)==rt1[i]) will_dirty_i|=1<<r;
7427 //if((regs[i].regmap[r]&63)==rt2[i]) will_dirty_i|=1<<r;
7428 if((regs[i].regmap[r]&63)==rt1[i+1]) will_dirty_i|=1<<r;
7429 if((regs[i].regmap[r]&63)==rt2[i+1]) will_dirty_i|=1<<r;
7430 if((regs[i].regmap[r]&63)>33) will_dirty_i&=~(1<<r);
7431 if(regs[i].regmap[r]<=0) will_dirty_i&=~(1<<r);
7432 if(regs[i].regmap[r]==CCREG) will_dirty_i|=1<<r;
7437 // Merge in delay slot (wont dirty)
7438 for(r=0;r<HOST_REGS;r++) {
7439 if(r!=EXCLUDE_REG) {
7440 if((regs[i].regmap[r]&63)==rt1[i]) wont_dirty_i|=1<<r;
7441 if((regs[i].regmap[r]&63)==rt2[i]) wont_dirty_i|=1<<r;
7442 if((regs[i].regmap[r]&63)==rt1[i+1]) wont_dirty_i|=1<<r;
7443 if((regs[i].regmap[r]&63)==rt2[i+1]) wont_dirty_i|=1<<r;
7444 if(regs[i].regmap[r]==CCREG) wont_dirty_i|=1<<r;
7445 if((branch_regs[i].regmap[r]&63)==rt1[i]) wont_dirty_i|=1<<r;
7446 if((branch_regs[i].regmap[r]&63)==rt2[i]) wont_dirty_i|=1<<r;
7447 if((branch_regs[i].regmap[r]&63)==rt1[i+1]) wont_dirty_i|=1<<r;
7448 if((branch_regs[i].regmap[r]&63)==rt2[i+1]) wont_dirty_i|=1<<r;
7449 if(branch_regs[i].regmap[r]==CCREG) wont_dirty_i|=1<<r;
7453 #ifndef DESTRUCTIVE_WRITEBACK
7454 branch_regs[i].dirty&=wont_dirty_i;
7456 branch_regs[i].dirty|=will_dirty_i;
7462 if(ba[i]<=start+i*4) {
7464 if(itype[i]==RJUMP||itype[i]==UJUMP||(source[i]>>16)==0x1000)
7466 // Unconditional branch
7469 // Merge in delay slot (will dirty)
7470 for(r=0;r<HOST_REGS;r++) {
7471 if(r!=EXCLUDE_REG) {
7472 if((branch_regs[i].regmap[r]&63)==rt1[i]) temp_will_dirty|=1<<r;
7473 if((branch_regs[i].regmap[r]&63)==rt2[i]) temp_will_dirty|=1<<r;
7474 if((branch_regs[i].regmap[r]&63)==rt1[i+1]) temp_will_dirty|=1<<r;
7475 if((branch_regs[i].regmap[r]&63)==rt2[i+1]) temp_will_dirty|=1<<r;
7476 if((branch_regs[i].regmap[r]&63)>33) temp_will_dirty&=~(1<<r);
7477 if(branch_regs[i].regmap[r]<=0) temp_will_dirty&=~(1<<r);
7478 if(branch_regs[i].regmap[r]==CCREG) temp_will_dirty|=1<<r;
7479 if((regs[i].regmap[r]&63)==rt1[i]) temp_will_dirty|=1<<r;
7480 if((regs[i].regmap[r]&63)==rt2[i]) temp_will_dirty|=1<<r;
7481 if((regs[i].regmap[r]&63)==rt1[i+1]) temp_will_dirty|=1<<r;
7482 if((regs[i].regmap[r]&63)==rt2[i+1]) temp_will_dirty|=1<<r;
7483 if((regs[i].regmap[r]&63)>33) temp_will_dirty&=~(1<<r);
7484 if(regs[i].regmap[r]<=0) temp_will_dirty&=~(1<<r);
7485 if(regs[i].regmap[r]==CCREG) temp_will_dirty|=1<<r;
7489 // Conditional branch (not taken case)
7490 temp_will_dirty=will_dirty_next;
7491 temp_wont_dirty=wont_dirty_next;
7492 // Merge in delay slot (will dirty)
7493 for(r=0;r<HOST_REGS;r++) {
7494 if(r!=EXCLUDE_REG) {
7496 // Will not dirty if likely branch is not taken
7497 if((branch_regs[i].regmap[r]&63)==rt1[i]) temp_will_dirty|=1<<r;
7498 if((branch_regs[i].regmap[r]&63)==rt2[i]) temp_will_dirty|=1<<r;
7499 if((branch_regs[i].regmap[r]&63)==rt1[i+1]) temp_will_dirty|=1<<r;
7500 if((branch_regs[i].regmap[r]&63)==rt2[i+1]) temp_will_dirty|=1<<r;
7501 if((branch_regs[i].regmap[r]&63)>33) temp_will_dirty&=~(1<<r);
7502 if(branch_regs[i].regmap[r]==0) temp_will_dirty&=~(1<<r);
7503 if(branch_regs[i].regmap[r]==CCREG) temp_will_dirty|=1<<r;
7504 //if((regs[i].regmap[r]&63)==rt1[i]) temp_will_dirty|=1<<r;
7505 //if((regs[i].regmap[r]&63)==rt2[i]) temp_will_dirty|=1<<r;
7506 if((regs[i].regmap[r]&63)==rt1[i+1]) temp_will_dirty|=1<<r;
7507 if((regs[i].regmap[r]&63)==rt2[i+1]) temp_will_dirty|=1<<r;
7508 if((regs[i].regmap[r]&63)>33) temp_will_dirty&=~(1<<r);
7509 if(regs[i].regmap[r]<=0) temp_will_dirty&=~(1<<r);
7510 if(regs[i].regmap[r]==CCREG) temp_will_dirty|=1<<r;
7515 // Merge in delay slot (wont dirty)
7516 for(r=0;r<HOST_REGS;r++) {
7517 if(r!=EXCLUDE_REG) {
7518 if((regs[i].regmap[r]&63)==rt1[i]) temp_wont_dirty|=1<<r;
7519 if((regs[i].regmap[r]&63)==rt2[i]) temp_wont_dirty|=1<<r;
7520 if((regs[i].regmap[r]&63)==rt1[i+1]) temp_wont_dirty|=1<<r;
7521 if((regs[i].regmap[r]&63)==rt2[i+1]) temp_wont_dirty|=1<<r;
7522 if(regs[i].regmap[r]==CCREG) temp_wont_dirty|=1<<r;
7523 if((branch_regs[i].regmap[r]&63)==rt1[i]) temp_wont_dirty|=1<<r;
7524 if((branch_regs[i].regmap[r]&63)==rt2[i]) temp_wont_dirty|=1<<r;
7525 if((branch_regs[i].regmap[r]&63)==rt1[i+1]) temp_wont_dirty|=1<<r;
7526 if((branch_regs[i].regmap[r]&63)==rt2[i+1]) temp_wont_dirty|=1<<r;
7527 if(branch_regs[i].regmap[r]==CCREG) temp_wont_dirty|=1<<r;
7530 // Deal with changed mappings
7532 for(r=0;r<HOST_REGS;r++) {
7533 if(r!=EXCLUDE_REG) {
7534 if(regs[i].regmap[r]!=regmap_pre[i][r]) {
7535 temp_will_dirty&=~(1<<r);
7536 temp_wont_dirty&=~(1<<r);
7537 if((regmap_pre[i][r]&63)>0 && (regmap_pre[i][r]&63)<34) {
7538 temp_will_dirty|=((unneeded_reg[i]>>(regmap_pre[i][r]&63))&1)<<r;
7539 temp_wont_dirty|=((unneeded_reg[i]>>(regmap_pre[i][r]&63))&1)<<r;
7541 temp_will_dirty|=1<<r;
7542 temp_wont_dirty|=1<<r;
7549 will_dirty[i]=temp_will_dirty;
7550 wont_dirty[i]=temp_wont_dirty;
7551 clean_registers((ba[i]-start)>>2,i-1,0);
7553 // Limit recursion. It can take an excessive amount
7554 // of time if there are a lot of nested loops.
7555 will_dirty[(ba[i]-start)>>2]=0;
7556 wont_dirty[(ba[i]-start)>>2]=-1;
7561 if(itype[i]==RJUMP||itype[i]==UJUMP||(source[i]>>16)==0x1000)
7563 // Unconditional branch
7566 //if(ba[i]>start+i*4) { // Disable recursion (for debugging)
7567 for(r=0;r<HOST_REGS;r++) {
7568 if(r!=EXCLUDE_REG) {
7569 if(branch_regs[i].regmap[r]==regs[(ba[i]-start)>>2].regmap_entry[r]) {
7570 will_dirty_i|=will_dirty[(ba[i]-start)>>2]&(1<<r);
7571 wont_dirty_i|=wont_dirty[(ba[i]-start)>>2]&(1<<r);
7573 if(branch_regs[i].regmap[r]>=0) {
7574 will_dirty_i|=((unneeded_reg[(ba[i]-start)>>2]>>(branch_regs[i].regmap[r]&63))&1)<<r;
7575 wont_dirty_i|=((unneeded_reg[(ba[i]-start)>>2]>>(branch_regs[i].regmap[r]&63))&1)<<r;
7580 // Merge in delay slot
7581 for(r=0;r<HOST_REGS;r++) {
7582 if(r!=EXCLUDE_REG) {
7583 if((branch_regs[i].regmap[r]&63)==rt1[i]) will_dirty_i|=1<<r;
7584 if((branch_regs[i].regmap[r]&63)==rt2[i]) will_dirty_i|=1<<r;
7585 if((branch_regs[i].regmap[r]&63)==rt1[i+1]) will_dirty_i|=1<<r;
7586 if((branch_regs[i].regmap[r]&63)==rt2[i+1]) will_dirty_i|=1<<r;
7587 if((branch_regs[i].regmap[r]&63)>33) will_dirty_i&=~(1<<r);
7588 if(branch_regs[i].regmap[r]<=0) will_dirty_i&=~(1<<r);
7589 if(branch_regs[i].regmap[r]==CCREG) will_dirty_i|=1<<r;
7590 if((regs[i].regmap[r]&63)==rt1[i]) will_dirty_i|=1<<r;
7591 if((regs[i].regmap[r]&63)==rt2[i]) will_dirty_i|=1<<r;
7592 if((regs[i].regmap[r]&63)==rt1[i+1]) will_dirty_i|=1<<r;
7593 if((regs[i].regmap[r]&63)==rt2[i+1]) will_dirty_i|=1<<r;
7594 if((regs[i].regmap[r]&63)>33) will_dirty_i&=~(1<<r);
7595 if(regs[i].regmap[r]<=0) will_dirty_i&=~(1<<r);
7596 if(regs[i].regmap[r]==CCREG) will_dirty_i|=1<<r;
7600 // Conditional branch
7601 will_dirty_i=will_dirty_next;
7602 wont_dirty_i=wont_dirty_next;
7603 //if(ba[i]>start+i*4) { // Disable recursion (for debugging)
7604 for(r=0;r<HOST_REGS;r++) {
7605 if(r!=EXCLUDE_REG) {
7606 signed char target_reg=branch_regs[i].regmap[r];
7607 if(target_reg==regs[(ba[i]-start)>>2].regmap_entry[r]) {
7608 will_dirty_i&=will_dirty[(ba[i]-start)>>2]&(1<<r);
7609 wont_dirty_i|=wont_dirty[(ba[i]-start)>>2]&(1<<r);
7611 else if(target_reg>=0) {
7612 will_dirty_i&=((unneeded_reg[(ba[i]-start)>>2]>>(target_reg&63))&1)<<r;
7613 wont_dirty_i|=((unneeded_reg[(ba[i]-start)>>2]>>(target_reg&63))&1)<<r;
7615 // Treat delay slot as part of branch too
7616 /*if(regs[i+1].regmap[r]==regs[(ba[i]-start)>>2].regmap_entry[r]) {
7617 will_dirty[i+1]&=will_dirty[(ba[i]-start)>>2]&(1<<r);
7618 wont_dirty[i+1]|=wont_dirty[(ba[i]-start)>>2]&(1<<r);
7622 will_dirty[i+1]&=~(1<<r);
7627 // Merge in delay slot
7628 for(r=0;r<HOST_REGS;r++) {
7629 if(r!=EXCLUDE_REG) {
7631 // Might not dirty if likely branch is not taken
7632 if((branch_regs[i].regmap[r]&63)==rt1[i]) will_dirty_i|=1<<r;
7633 if((branch_regs[i].regmap[r]&63)==rt2[i]) will_dirty_i|=1<<r;
7634 if((branch_regs[i].regmap[r]&63)==rt1[i+1]) will_dirty_i|=1<<r;
7635 if((branch_regs[i].regmap[r]&63)==rt2[i+1]) will_dirty_i|=1<<r;
7636 if((branch_regs[i].regmap[r]&63)>33) will_dirty_i&=~(1<<r);
7637 if(branch_regs[i].regmap[r]<=0) will_dirty_i&=~(1<<r);
7638 if(branch_regs[i].regmap[r]==CCREG) will_dirty_i|=1<<r;
7639 //if((regs[i].regmap[r]&63)==rt1[i]) will_dirty_i|=1<<r;
7640 //if((regs[i].regmap[r]&63)==rt2[i]) will_dirty_i|=1<<r;
7641 if((regs[i].regmap[r]&63)==rt1[i+1]) will_dirty_i|=1<<r;
7642 if((regs[i].regmap[r]&63)==rt2[i+1]) will_dirty_i|=1<<r;
7643 if((regs[i].regmap[r]&63)>33) will_dirty_i&=~(1<<r);
7644 if(regs[i].regmap[r]<=0) will_dirty_i&=~(1<<r);
7645 if(regs[i].regmap[r]==CCREG) will_dirty_i|=1<<r;
7650 // Merge in delay slot (won't dirty)
7651 for(r=0;r<HOST_REGS;r++) {
7652 if(r!=EXCLUDE_REG) {
7653 if((regs[i].regmap[r]&63)==rt1[i]) wont_dirty_i|=1<<r;
7654 if((regs[i].regmap[r]&63)==rt2[i]) wont_dirty_i|=1<<r;
7655 if((regs[i].regmap[r]&63)==rt1[i+1]) wont_dirty_i|=1<<r;
7656 if((regs[i].regmap[r]&63)==rt2[i+1]) wont_dirty_i|=1<<r;
7657 if(regs[i].regmap[r]==CCREG) wont_dirty_i|=1<<r;
7658 if((branch_regs[i].regmap[r]&63)==rt1[i]) wont_dirty_i|=1<<r;
7659 if((branch_regs[i].regmap[r]&63)==rt2[i]) wont_dirty_i|=1<<r;
7660 if((branch_regs[i].regmap[r]&63)==rt1[i+1]) wont_dirty_i|=1<<r;
7661 if((branch_regs[i].regmap[r]&63)==rt2[i+1]) wont_dirty_i|=1<<r;
7662 if(branch_regs[i].regmap[r]==CCREG) wont_dirty_i|=1<<r;
7666 #ifndef DESTRUCTIVE_WRITEBACK
7667 branch_regs[i].dirty&=wont_dirty_i;
7669 branch_regs[i].dirty|=will_dirty_i;
7674 else if(itype[i]==SYSCALL||itype[i]==HLECALL||itype[i]==INTCALL)
7676 // SYSCALL instruction (software interrupt)
7680 else if(itype[i]==COP0 && (source[i]&0x3f)==0x18)
7682 // ERET instruction (return from interrupt)
7686 will_dirty_next=will_dirty_i;
7687 wont_dirty_next=wont_dirty_i;
7688 for(r=0;r<HOST_REGS;r++) {
7689 if(r!=EXCLUDE_REG) {
7690 if((regs[i].regmap[r]&63)==rt1[i]) will_dirty_i|=1<<r;
7691 if((regs[i].regmap[r]&63)==rt2[i]) will_dirty_i|=1<<r;
7692 if((regs[i].regmap[r]&63)>33) will_dirty_i&=~(1<<r);
7693 if(regs[i].regmap[r]<=0) will_dirty_i&=~(1<<r);
7694 if(regs[i].regmap[r]==CCREG) will_dirty_i|=1<<r;
7695 if((regs[i].regmap[r]&63)==rt1[i]) wont_dirty_i|=1<<r;
7696 if((regs[i].regmap[r]&63)==rt2[i]) wont_dirty_i|=1<<r;
7697 if(regs[i].regmap[r]==CCREG) wont_dirty_i|=1<<r;
7699 if(itype[i]!=RJUMP&&itype[i]!=UJUMP&&itype[i]!=CJUMP&&itype[i]!=SJUMP&&itype[i]!=FJUMP)
7701 // Don't store a register immediately after writing it,
7702 // may prevent dual-issue.
7703 if((regs[i].regmap[r]&63)==rt1[i-1]) wont_dirty_i|=1<<r;
7704 if((regs[i].regmap[r]&63)==rt2[i-1]) wont_dirty_i|=1<<r;
7710 will_dirty[i]=will_dirty_i;
7711 wont_dirty[i]=wont_dirty_i;
7712 // Mark registers that won't be dirtied as not dirty
7714 /*printf("wr (%d,%d) %x will:",istart,iend,start+i*4);
7715 for(r=0;r<HOST_REGS;r++) {
7716 if((will_dirty_i>>r)&1) {
7722 //if(i==istart||(itype[i-1]!=RJUMP&&itype[i-1]!=UJUMP&&itype[i-1]!=CJUMP&&itype[i-1]!=SJUMP&&itype[i-1]!=FJUMP)) {
7723 regs[i].dirty|=will_dirty_i;
7724 #ifndef DESTRUCTIVE_WRITEBACK
7725 regs[i].dirty&=wont_dirty_i;
7726 if(itype[i]==RJUMP||itype[i]==UJUMP||itype[i]==CJUMP||itype[i]==SJUMP||itype[i]==FJUMP)
7728 if(i<iend-1&&itype[i]!=RJUMP&&itype[i]!=UJUMP&&(source[i]>>16)!=0x1000) {
7729 for(r=0;r<HOST_REGS;r++) {
7730 if(r!=EXCLUDE_REG) {
7731 if(regs[i].regmap[r]==regmap_pre[i+2][r]) {
7732 regs[i+2].wasdirty&=wont_dirty_i|~(1<<r);
7733 }else {/*printf("i: %x (%d) mismatch(+2): %d\n",start+i*4,i,r);/*assert(!((wont_dirty_i>>r)&1));*/}
7741 for(r=0;r<HOST_REGS;r++) {
7742 if(r!=EXCLUDE_REG) {
7743 if(regs[i].regmap[r]==regmap_pre[i+1][r]) {
7744 regs[i+1].wasdirty&=wont_dirty_i|~(1<<r);
7745 }else {/*printf("i: %x (%d) mismatch(+1): %d\n",start+i*4,i,r);/*assert(!((wont_dirty_i>>r)&1));*/}
7753 // Deal with changed mappings
7754 temp_will_dirty=will_dirty_i;
7755 temp_wont_dirty=wont_dirty_i;
7756 for(r=0;r<HOST_REGS;r++) {
7757 if(r!=EXCLUDE_REG) {
7759 if(regs[i].regmap[r]==regmap_pre[i][r]) {
7761 #ifndef DESTRUCTIVE_WRITEBACK
7762 regs[i].wasdirty&=wont_dirty_i|~(1<<r);
7764 regs[i].wasdirty|=will_dirty_i&(1<<r);
7767 else if(regmap_pre[i][r]>=0&&(nr=get_reg(regs[i].regmap,regmap_pre[i][r]))>=0) {
7768 // Register moved to a different register
7769 will_dirty_i&=~(1<<r);
7770 wont_dirty_i&=~(1<<r);
7771 will_dirty_i|=((temp_will_dirty>>nr)&1)<<r;
7772 wont_dirty_i|=((temp_wont_dirty>>nr)&1)<<r;
7774 #ifndef DESTRUCTIVE_WRITEBACK
7775 regs[i].wasdirty&=wont_dirty_i|~(1<<r);
7777 regs[i].wasdirty|=will_dirty_i&(1<<r);
7781 will_dirty_i&=~(1<<r);
7782 wont_dirty_i&=~(1<<r);
7783 if((regmap_pre[i][r]&63)>0 && (regmap_pre[i][r]&63)<34) {
7784 will_dirty_i|=((unneeded_reg[i]>>(regmap_pre[i][r]&63))&1)<<r;
7785 wont_dirty_i|=((unneeded_reg[i]>>(regmap_pre[i][r]&63))&1)<<r;
7788 /*printf("i: %x (%d) mismatch: %d\n",start+i*4,i,r);/*assert(!((will_dirty>>r)&1));*/
7798 void disassemble_inst(int i)
7800 if (bt[i]) printf("*"); else printf(" ");
7803 printf (" %x: %s %8x\n",start+i*4,insn[i],ba[i]);break;
7805 printf (" %x: %s r%d,r%d,%8x\n",start+i*4,insn[i],rs1[i],rs2[i],i?start+i*4+4+((signed int)((unsigned int)source[i]<<16)>>14):*ba);break;
7807 printf (" %x: %s r%d,%8x\n",start+i*4,insn[i],rs1[i],start+i*4+4+((signed int)((unsigned int)source[i]<<16)>>14));break;
7809 printf (" %x: %s %8x\n",start+i*4,insn[i],ba[i]);break;
7811 if (opcode[i]==0x9&&rt1[i]!=31)
7812 printf (" %x: %s r%d,r%d\n",start+i*4,insn[i],rt1[i],rs1[i]);
7814 printf (" %x: %s r%d\n",start+i*4,insn[i],rs1[i]);
7817 printf (" %x: %s (pagespan) r%d,r%d,%8x\n",start+i*4,insn[i],rs1[i],rs2[i],ba[i]);break;
7819 if(opcode[i]==0xf) //LUI
7820 printf (" %x: %s r%d,%4x0000\n",start+i*4,insn[i],rt1[i],imm[i]&0xffff);
7822 printf (" %x: %s r%d,r%d,%d\n",start+i*4,insn[i],rt1[i],rs1[i],imm[i]);
7826 printf (" %x: %s r%d,r%d+%x\n",start+i*4,insn[i],rt1[i],rs1[i],imm[i]);
7830 printf (" %x: %s r%d,r%d+%x\n",start+i*4,insn[i],rs2[i],rs1[i],imm[i]);
7834 printf (" %x: %s r%d,r%d,r%d\n",start+i*4,insn[i],rt1[i],rs1[i],rs2[i]);
7837 printf (" %x: %s r%d,r%d\n",start+i*4,insn[i],rs1[i],rs2[i]);
7840 printf (" %x: %s r%d,r%d,%d\n",start+i*4,insn[i],rt1[i],rs1[i],imm[i]);
7843 if((opcode2[i]&0x1d)==0x10)
7844 printf (" %x: %s r%d\n",start+i*4,insn[i],rt1[i]);
7845 else if((opcode2[i]&0x1d)==0x11)
7846 printf (" %x: %s r%d\n",start+i*4,insn[i],rs1[i]);
7848 printf (" %x: %s\n",start+i*4,insn[i]);
7852 printf (" %x: %s r%d,cpr0[%d]\n",start+i*4,insn[i],rt1[i],(source[i]>>11)&0x1f); // MFC0
7853 else if(opcode2[i]==4)
7854 printf (" %x: %s r%d,cpr0[%d]\n",start+i*4,insn[i],rs1[i],(source[i]>>11)&0x1f); // MTC0
7855 else printf (" %x: %s\n",start+i*4,insn[i]);
7859 printf (" %x: %s r%d,cpr1[%d]\n",start+i*4,insn[i],rt1[i],(source[i]>>11)&0x1f); // MFC1
7860 else if(opcode2[i]>3)
7861 printf (" %x: %s r%d,cpr1[%d]\n",start+i*4,insn[i],rs1[i],(source[i]>>11)&0x1f); // MTC1
7862 else printf (" %x: %s\n",start+i*4,insn[i]);
7866 printf (" %x: %s r%d,cpr2[%d]\n",start+i*4,insn[i],rt1[i],(source[i]>>11)&0x1f); // MFC2
7867 else if(opcode2[i]>3)
7868 printf (" %x: %s r%d,cpr2[%d]\n",start+i*4,insn[i],rs1[i],(source[i]>>11)&0x1f); // MTC2
7869 else printf (" %x: %s\n",start+i*4,insn[i]);
7872 printf (" %x: %s cpr1[%d],r%d+%x\n",start+i*4,insn[i],(source[i]>>16)&0x1f,rs1[i],imm[i]);
7875 printf (" %x: %s cpr2[%d],r%d+%x\n",start+i*4,insn[i],(source[i]>>16)&0x1f,rs1[i],imm[i]);
7878 printf (" %x: %s (INTCALL)\n",start+i*4,insn[i]);
7881 //printf (" %s %8x\n",insn[i],source[i]);
7882 printf (" %x: %s\n",start+i*4,insn[i]);
7886 static void disassemble_inst(int i) {}
7889 // clear the state completely, instead of just marking
7890 // things invalid like invalidate_all_pages() does
7891 void new_dynarec_clear_full()
7894 out=(u_char *)BASE_ADDR;
7895 memset(invalid_code,1,sizeof(invalid_code));
7896 memset(hash_table,0xff,sizeof(hash_table));
7897 memset(mini_ht,-1,sizeof(mini_ht));
7898 memset(restore_candidate,0,sizeof(restore_candidate));
7899 memset(shadow,0,sizeof(shadow));
7901 expirep=16384; // Expiry pointer, +2 blocks
7902 pending_exception=0;
7905 inv_code_start=inv_code_end=~0;
7910 for(n=0;n<524288;n++) // 0 .. 0x7FFFFFFF
7912 for(n=524288;n<526336;n++) // 0x80000000 .. 0x807FFFFF
7913 memory_map[n]=((u_int)rdram-0x80000000)>>2;
7914 for(n=526336;n<1048576;n++) // 0x80800000 .. 0xFFFFFFFF
7917 for(n=0;n<4096;n++) ll_clear(jump_in+n);
7918 for(n=0;n<4096;n++) ll_clear(jump_out+n);
7919 for(n=0;n<4096;n++) ll_clear(jump_dirty+n);
7922 void new_dynarec_init()
7924 printf("Init new dynarec\n");
7925 out=(u_char *)BASE_ADDR;
7926 if (mmap (out, 1<<TARGET_SIZE_2,
7927 PROT_READ | PROT_WRITE | PROT_EXEC,
7928 MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS,
7929 -1, 0) <= 0) {printf("mmap() failed\n");}
7931 rdword=&readmem_dword;
7932 fake_pc.f.r.rs=&readmem_dword;
7933 fake_pc.f.r.rt=&readmem_dword;
7934 fake_pc.f.r.rd=&readmem_dword;
7937 new_dynarec_clear_full();
7939 // Copy this into local area so we don't have to put it in every literal pool
7940 invc_ptr=invalid_code;
7943 for(n=0;n<0x8000;n++) { // 0 .. 0x7FFFFFFF
7944 writemem[n] = write_nomem_new;
7945 writememb[n] = write_nomemb_new;
7946 writememh[n] = write_nomemh_new;
7948 writememd[n] = write_nomemd_new;
7950 readmem[n] = read_nomem_new;
7951 readmemb[n] = read_nomemb_new;
7952 readmemh[n] = read_nomemh_new;
7954 readmemd[n] = read_nomemd_new;
7957 for(n=0x8000;n<0x8080;n++) { // 0x80000000 .. 0x807FFFFF
7958 writemem[n] = write_rdram_new;
7959 writememb[n] = write_rdramb_new;
7960 writememh[n] = write_rdramh_new;
7962 writememd[n] = write_rdramd_new;
7965 for(n=0xC000;n<0x10000;n++) { // 0xC0000000 .. 0xFFFFFFFF
7966 writemem[n] = write_nomem_new;
7967 writememb[n] = write_nomemb_new;
7968 writememh[n] = write_nomemh_new;
7970 writememd[n] = write_nomemd_new;
7972 readmem[n] = read_nomem_new;
7973 readmemb[n] = read_nomemb_new;
7974 readmemh[n] = read_nomemh_new;
7976 readmemd[n] = read_nomemd_new;
7984 void new_dynarec_cleanup()
7987 if (munmap ((void *)BASE_ADDR, 1<<TARGET_SIZE_2) < 0) {printf("munmap() failed\n");}
7988 for(n=0;n<4096;n++) ll_clear(jump_in+n);
7989 for(n=0;n<4096;n++) ll_clear(jump_out+n);
7990 for(n=0;n<4096;n++) ll_clear(jump_dirty+n);
7992 if (munmap (ROM_COPY, 67108864) < 0) {printf("munmap() failed\n");}
7996 int new_recompile_block(int addr)
7999 if(addr==0x800cd050) {
8001 for(block=0x80000;block<0x80800;block++) invalidate_block(block);
8003 for(n=0;n<=2048;n++) ll_clear(jump_dirty+n);
8006 //if(Count==365117028) tracedebug=1;
8007 assem_debug("NOTCOMPILED: addr = %x -> %x\n", (int)addr, (int)out);
8008 //printf("NOTCOMPILED: addr = %x -> %x\n", (int)addr, (int)out);
8009 //printf("TRACE: count=%d next=%d (compile %x)\n",Count,next_interupt,addr);
8011 //printf("TRACE: count=%d next=%d (checksum %x)\n",Count,next_interupt,mchecksum());
8012 //printf("fpu mapping=%x enabled=%x\n",(Status & 0x04000000)>>26,(Status & 0x20000000)>>29);
8013 /*if(Count>=312978186) {
8017 start = (u_int)addr&~3;
8018 //assert(((u_int)addr&1)==0);
8019 new_dynarec_did_compile=1;
8021 if (Config.HLE && start == 0x80001000) // hlecall
8023 // XXX: is this enough? Maybe check hleSoftCall?
8024 u_int beginning=(u_int)out;
8025 u_int page=get_page(start);
8026 invalid_code[start>>12]=0;
8027 emit_movimm(start,0);
8028 emit_writeword(0,(int)&pcaddr);
8029 emit_jmp((int)new_dyna_leave);
8032 __clear_cache((void *)beginning,out);
8034 ll_add(jump_in+page,start,(void *)beginning);
8037 else if ((u_int)addr < 0x00200000 ||
8038 (0xa0000000 <= addr && addr < 0xa0200000)) {
8039 // used for BIOS calls mostly?
8040 source = (u_int *)((u_int)rdram+(start&0x1fffff));
8041 pagelimit = (addr&0xa0000000)|0x00200000;
8043 else if (!Config.HLE && (
8044 /* (0x9fc00000 <= addr && addr < 0x9fc80000) ||*/
8045 (0xbfc00000 <= addr && addr < 0xbfc80000))) {
8047 source = (u_int *)((u_int)psxR+(start&0x7ffff));
8048 pagelimit = (addr&0xfff00000)|0x80000;
8053 if ((int)addr >= 0xa4000000 && (int)addr < 0xa4001000) {
8054 source = (u_int *)((u_int)SP_DMEM+start-0xa4000000);
8055 pagelimit = 0xa4001000;
8059 if ((int)addr >= 0x80000000 && (int)addr < 0x80000000+RAM_SIZE) {
8060 source = (u_int *)((u_int)rdram+start-0x80000000);
8061 pagelimit = 0x80000000+RAM_SIZE;
8064 else if ((signed int)addr >= (signed int)0xC0000000) {
8065 //printf("addr=%x mm=%x\n",(u_int)addr,(memory_map[start>>12]<<2));
8066 //if(tlb_LUT_r[start>>12])
8067 //source = (u_int *)(((int)rdram)+(tlb_LUT_r[start>>12]&0xFFFFF000)+(((int)addr)&0xFFF)-0x80000000);
8068 if((signed int)memory_map[start>>12]>=0) {
8069 source = (u_int *)((u_int)(start+(memory_map[start>>12]<<2)));
8070 pagelimit=(start+4096)&0xFFFFF000;
8071 int map=memory_map[start>>12];
8074 //printf("start: %x next: %x\n",map,memory_map[pagelimit>>12]);
8075 if((map&0xBFFFFFFF)==(memory_map[pagelimit>>12]&0xBFFFFFFF)) pagelimit+=4096;
8077 assem_debug("pagelimit=%x\n",pagelimit);
8078 assem_debug("mapping=%x (%x)\n",memory_map[start>>12],(memory_map[start>>12]<<2)+start);
8081 assem_debug("Compile at unmapped memory address: %x \n", (int)addr);
8082 //assem_debug("start: %x next: %x\n",memory_map[start>>12],memory_map[(start+4096)>>12]);
8083 return -1; // Caller will invoke exception handler
8085 //printf("source= %x\n",(int)source);
8089 printf("Compile at bogus memory address: %x \n", (int)addr);
8093 /* Pass 1: disassemble */
8094 /* Pass 2: register dependencies, branch targets */
8095 /* Pass 3: register allocation */
8096 /* Pass 4: branch dependencies */
8097 /* Pass 5: pre-alloc */
8098 /* Pass 6: optimize clean/dirty state */
8099 /* Pass 7: flag 32-bit registers */
8100 /* Pass 8: assembly */
8101 /* Pass 9: linker */
8102 /* Pass 10: garbage collection / free memory */
8106 unsigned int type,op,op2;
8108 //printf("addr = %x source = %x %x\n", addr,source,source[0]);
8110 /* Pass 1 disassembly */
8112 for(i=0;!done;i++) {
8113 bt[i]=0;likely[i]=0;ooo[i]=0;op2=0;
8114 minimum_free_regs[i]=0;
8115 opcode[i]=op=source[i]>>26;
8118 case 0x00: strcpy(insn[i],"special"); type=NI;
8122 case 0x00: strcpy(insn[i],"SLL"); type=SHIFTIMM; break;
8123 case 0x02: strcpy(insn[i],"SRL"); type=SHIFTIMM; break;
8124 case 0x03: strcpy(insn[i],"SRA"); type=SHIFTIMM; break;
8125 case 0x04: strcpy(insn[i],"SLLV"); type=SHIFT; break;
8126 case 0x06: strcpy(insn[i],"SRLV"); type=SHIFT; break;
8127 case 0x07: strcpy(insn[i],"SRAV"); type=SHIFT; break;
8128 case 0x08: strcpy(insn[i],"JR"); type=RJUMP; break;
8129 case 0x09: strcpy(insn[i],"JALR"); type=RJUMP; break;
8130 case 0x0C: strcpy(insn[i],"SYSCALL"); type=SYSCALL; break;
8131 case 0x0D: strcpy(insn[i],"BREAK"); type=OTHER; break;
8132 case 0x0F: strcpy(insn[i],"SYNC"); type=OTHER; break;
8133 case 0x10: strcpy(insn[i],"MFHI"); type=MOV; break;
8134 case 0x11: strcpy(insn[i],"MTHI"); type=MOV; break;
8135 case 0x12: strcpy(insn[i],"MFLO"); type=MOV; break;
8136 case 0x13: strcpy(insn[i],"MTLO"); type=MOV; break;
8137 case 0x18: strcpy(insn[i],"MULT"); type=MULTDIV; break;
8138 case 0x19: strcpy(insn[i],"MULTU"); type=MULTDIV; break;
8139 case 0x1A: strcpy(insn[i],"DIV"); type=MULTDIV; break;
8140 case 0x1B: strcpy(insn[i],"DIVU"); type=MULTDIV; break;
8141 case 0x20: strcpy(insn[i],"ADD"); type=ALU; break;
8142 case 0x21: strcpy(insn[i],"ADDU"); type=ALU; break;
8143 case 0x22: strcpy(insn[i],"SUB"); type=ALU; break;
8144 case 0x23: strcpy(insn[i],"SUBU"); type=ALU; break;
8145 case 0x24: strcpy(insn[i],"AND"); type=ALU; break;
8146 case 0x25: strcpy(insn[i],"OR"); type=ALU; break;
8147 case 0x26: strcpy(insn[i],"XOR"); type=ALU; break;
8148 case 0x27: strcpy(insn[i],"NOR"); type=ALU; break;
8149 case 0x2A: strcpy(insn[i],"SLT"); type=ALU; break;
8150 case 0x2B: strcpy(insn[i],"SLTU"); type=ALU; break;
8151 case 0x30: strcpy(insn[i],"TGE"); type=NI; break;
8152 case 0x31: strcpy(insn[i],"TGEU"); type=NI; break;
8153 case 0x32: strcpy(insn[i],"TLT"); type=NI; break;
8154 case 0x33: strcpy(insn[i],"TLTU"); type=NI; break;
8155 case 0x34: strcpy(insn[i],"TEQ"); type=NI; break;
8156 case 0x36: strcpy(insn[i],"TNE"); type=NI; break;
8158 case 0x14: strcpy(insn[i],"DSLLV"); type=SHIFT; break;
8159 case 0x16: strcpy(insn[i],"DSRLV"); type=SHIFT; break;
8160 case 0x17: strcpy(insn[i],"DSRAV"); type=SHIFT; break;
8161 case 0x1C: strcpy(insn[i],"DMULT"); type=MULTDIV; break;
8162 case 0x1D: strcpy(insn[i],"DMULTU"); type=MULTDIV; break;
8163 case 0x1E: strcpy(insn[i],"DDIV"); type=MULTDIV; break;
8164 case 0x1F: strcpy(insn[i],"DDIVU"); type=MULTDIV; break;
8165 case 0x2C: strcpy(insn[i],"DADD"); type=ALU; break;
8166 case 0x2D: strcpy(insn[i],"DADDU"); type=ALU; break;
8167 case 0x2E: strcpy(insn[i],"DSUB"); type=ALU; break;
8168 case 0x2F: strcpy(insn[i],"DSUBU"); type=ALU; break;
8169 case 0x38: strcpy(insn[i],"DSLL"); type=SHIFTIMM; break;
8170 case 0x3A: strcpy(insn[i],"DSRL"); type=SHIFTIMM; break;
8171 case 0x3B: strcpy(insn[i],"DSRA"); type=SHIFTIMM; break;
8172 case 0x3C: strcpy(insn[i],"DSLL32"); type=SHIFTIMM; break;
8173 case 0x3E: strcpy(insn[i],"DSRL32"); type=SHIFTIMM; break;
8174 case 0x3F: strcpy(insn[i],"DSRA32"); type=SHIFTIMM; break;
8178 case 0x01: strcpy(insn[i],"regimm"); type=NI;
8179 op2=(source[i]>>16)&0x1f;
8182 case 0x00: strcpy(insn[i],"BLTZ"); type=SJUMP; break;
8183 case 0x01: strcpy(insn[i],"BGEZ"); type=SJUMP; break;
8184 case 0x02: strcpy(insn[i],"BLTZL"); type=SJUMP; break;
8185 case 0x03: strcpy(insn[i],"BGEZL"); type=SJUMP; break;
8186 case 0x08: strcpy(insn[i],"TGEI"); type=NI; break;
8187 case 0x09: strcpy(insn[i],"TGEIU"); type=NI; break;
8188 case 0x0A: strcpy(insn[i],"TLTI"); type=NI; break;
8189 case 0x0B: strcpy(insn[i],"TLTIU"); type=NI; break;
8190 case 0x0C: strcpy(insn[i],"TEQI"); type=NI; break;
8191 case 0x0E: strcpy(insn[i],"TNEI"); type=NI; break;
8192 case 0x10: strcpy(insn[i],"BLTZAL"); type=SJUMP; break;
8193 case 0x11: strcpy(insn[i],"BGEZAL"); type=SJUMP; break;
8194 case 0x12: strcpy(insn[i],"BLTZALL"); type=SJUMP; break;
8195 case 0x13: strcpy(insn[i],"BGEZALL"); type=SJUMP; break;
8198 case 0x02: strcpy(insn[i],"J"); type=UJUMP; break;
8199 case 0x03: strcpy(insn[i],"JAL"); type=UJUMP; break;
8200 case 0x04: strcpy(insn[i],"BEQ"); type=CJUMP; break;
8201 case 0x05: strcpy(insn[i],"BNE"); type=CJUMP; break;
8202 case 0x06: strcpy(insn[i],"BLEZ"); type=CJUMP; break;
8203 case 0x07: strcpy(insn[i],"BGTZ"); type=CJUMP; break;
8204 case 0x08: strcpy(insn[i],"ADDI"); type=IMM16; break;
8205 case 0x09: strcpy(insn[i],"ADDIU"); type=IMM16; break;
8206 case 0x0A: strcpy(insn[i],"SLTI"); type=IMM16; break;
8207 case 0x0B: strcpy(insn[i],"SLTIU"); type=IMM16; break;
8208 case 0x0C: strcpy(insn[i],"ANDI"); type=IMM16; break;
8209 case 0x0D: strcpy(insn[i],"ORI"); type=IMM16; break;
8210 case 0x0E: strcpy(insn[i],"XORI"); type=IMM16; break;
8211 case 0x0F: strcpy(insn[i],"LUI"); type=IMM16; break;
8212 case 0x10: strcpy(insn[i],"cop0"); type=NI;
8213 op2=(source[i]>>21)&0x1f;
8216 case 0x00: strcpy(insn[i],"MFC0"); type=COP0; break;
8217 case 0x04: strcpy(insn[i],"MTC0"); type=COP0; break;
8218 case 0x10: strcpy(insn[i],"tlb"); type=NI;
8219 switch(source[i]&0x3f)
8221 case 0x01: strcpy(insn[i],"TLBR"); type=COP0; break;
8222 case 0x02: strcpy(insn[i],"TLBWI"); type=COP0; break;
8223 case 0x06: strcpy(insn[i],"TLBWR"); type=COP0; break;
8224 case 0x08: strcpy(insn[i],"TLBP"); type=COP0; break;
8226 case 0x10: strcpy(insn[i],"RFE"); type=COP0; break;
8228 case 0x18: strcpy(insn[i],"ERET"); type=COP0; break;
8233 case 0x11: strcpy(insn[i],"cop1"); type=NI;
8234 op2=(source[i]>>21)&0x1f;
8237 case 0x00: strcpy(insn[i],"MFC1"); type=COP1; break;
8238 case 0x01: strcpy(insn[i],"DMFC1"); type=COP1; break;
8239 case 0x02: strcpy(insn[i],"CFC1"); type=COP1; break;
8240 case 0x04: strcpy(insn[i],"MTC1"); type=COP1; break;
8241 case 0x05: strcpy(insn[i],"DMTC1"); type=COP1; break;
8242 case 0x06: strcpy(insn[i],"CTC1"); type=COP1; break;
8243 case 0x08: strcpy(insn[i],"BC1"); type=FJUMP;
8244 switch((source[i]>>16)&0x3)
8246 case 0x00: strcpy(insn[i],"BC1F"); break;
8247 case 0x01: strcpy(insn[i],"BC1T"); break;
8248 case 0x02: strcpy(insn[i],"BC1FL"); break;
8249 case 0x03: strcpy(insn[i],"BC1TL"); break;
8252 case 0x10: strcpy(insn[i],"C1.S"); type=NI;
8253 switch(source[i]&0x3f)
8255 case 0x00: strcpy(insn[i],"ADD.S"); type=FLOAT; break;
8256 case 0x01: strcpy(insn[i],"SUB.S"); type=FLOAT; break;
8257 case 0x02: strcpy(insn[i],"MUL.S"); type=FLOAT; break;
8258 case 0x03: strcpy(insn[i],"DIV.S"); type=FLOAT; break;
8259 case 0x04: strcpy(insn[i],"SQRT.S"); type=FLOAT; break;
8260 case 0x05: strcpy(insn[i],"ABS.S"); type=FLOAT; break;
8261 case 0x06: strcpy(insn[i],"MOV.S"); type=FLOAT; break;
8262 case 0x07: strcpy(insn[i],"NEG.S"); type=FLOAT; break;
8263 case 0x08: strcpy(insn[i],"ROUND.L.S"); type=FCONV; break;
8264 case 0x09: strcpy(insn[i],"TRUNC.L.S"); type=FCONV; break;
8265 case 0x0A: strcpy(insn[i],"CEIL.L.S"); type=FCONV; break;
8266 case 0x0B: strcpy(insn[i],"FLOOR.L.S"); type=FCONV; break;
8267 case 0x0C: strcpy(insn[i],"ROUND.W.S"); type=FCONV; break;
8268 case 0x0D: strcpy(insn[i],"TRUNC.W.S"); type=FCONV; break;
8269 case 0x0E: strcpy(insn[i],"CEIL.W.S"); type=FCONV; break;
8270 case 0x0F: strcpy(insn[i],"FLOOR.W.S"); type=FCONV; break;
8271 case 0x21: strcpy(insn[i],"CVT.D.S"); type=FCONV; break;
8272 case 0x24: strcpy(insn[i],"CVT.W.S"); type=FCONV; break;
8273 case 0x25: strcpy(insn[i],"CVT.L.S"); type=FCONV; break;
8274 case 0x30: strcpy(insn[i],"C.F.S"); type=FCOMP; break;
8275 case 0x31: strcpy(insn[i],"C.UN.S"); type=FCOMP; break;
8276 case 0x32: strcpy(insn[i],"C.EQ.S"); type=FCOMP; break;
8277 case 0x33: strcpy(insn[i],"C.UEQ.S"); type=FCOMP; break;
8278 case 0x34: strcpy(insn[i],"C.OLT.S"); type=FCOMP; break;
8279 case 0x35: strcpy(insn[i],"C.ULT.S"); type=FCOMP; break;
8280 case 0x36: strcpy(insn[i],"C.OLE.S"); type=FCOMP; break;
8281 case 0x37: strcpy(insn[i],"C.ULE.S"); type=FCOMP; break;
8282 case 0x38: strcpy(insn[i],"C.SF.S"); type=FCOMP; break;
8283 case 0x39: strcpy(insn[i],"C.NGLE.S"); type=FCOMP; break;
8284 case 0x3A: strcpy(insn[i],"C.SEQ.S"); type=FCOMP; break;
8285 case 0x3B: strcpy(insn[i],"C.NGL.S"); type=FCOMP; break;
8286 case 0x3C: strcpy(insn[i],"C.LT.S"); type=FCOMP; break;
8287 case 0x3D: strcpy(insn[i],"C.NGE.S"); type=FCOMP; break;
8288 case 0x3E: strcpy(insn[i],"C.LE.S"); type=FCOMP; break;
8289 case 0x3F: strcpy(insn[i],"C.NGT.S"); type=FCOMP; break;
8292 case 0x11: strcpy(insn[i],"C1.D"); type=NI;
8293 switch(source[i]&0x3f)
8295 case 0x00: strcpy(insn[i],"ADD.D"); type=FLOAT; break;
8296 case 0x01: strcpy(insn[i],"SUB.D"); type=FLOAT; break;
8297 case 0x02: strcpy(insn[i],"MUL.D"); type=FLOAT; break;
8298 case 0x03: strcpy(insn[i],"DIV.D"); type=FLOAT; break;
8299 case 0x04: strcpy(insn[i],"SQRT.D"); type=FLOAT; break;
8300 case 0x05: strcpy(insn[i],"ABS.D"); type=FLOAT; break;
8301 case 0x06: strcpy(insn[i],"MOV.D"); type=FLOAT; break;
8302 case 0x07: strcpy(insn[i],"NEG.D"); type=FLOAT; break;
8303 case 0x08: strcpy(insn[i],"ROUND.L.D"); type=FCONV; break;
8304 case 0x09: strcpy(insn[i],"TRUNC.L.D"); type=FCONV; break;
8305 case 0x0A: strcpy(insn[i],"CEIL.L.D"); type=FCONV; break;
8306 case 0x0B: strcpy(insn[i],"FLOOR.L.D"); type=FCONV; break;
8307 case 0x0C: strcpy(insn[i],"ROUND.W.D"); type=FCONV; break;
8308 case 0x0D: strcpy(insn[i],"TRUNC.W.D"); type=FCONV; break;
8309 case 0x0E: strcpy(insn[i],"CEIL.W.D"); type=FCONV; break;
8310 case 0x0F: strcpy(insn[i],"FLOOR.W.D"); type=FCONV; break;
8311 case 0x20: strcpy(insn[i],"CVT.S.D"); type=FCONV; break;
8312 case 0x24: strcpy(insn[i],"CVT.W.D"); type=FCONV; break;
8313 case 0x25: strcpy(insn[i],"CVT.L.D"); type=FCONV; break;
8314 case 0x30: strcpy(insn[i],"C.F.D"); type=FCOMP; break;
8315 case 0x31: strcpy(insn[i],"C.UN.D"); type=FCOMP; break;
8316 case 0x32: strcpy(insn[i],"C.EQ.D"); type=FCOMP; break;
8317 case 0x33: strcpy(insn[i],"C.UEQ.D"); type=FCOMP; break;
8318 case 0x34: strcpy(insn[i],"C.OLT.D"); type=FCOMP; break;
8319 case 0x35: strcpy(insn[i],"C.ULT.D"); type=FCOMP; break;
8320 case 0x36: strcpy(insn[i],"C.OLE.D"); type=FCOMP; break;
8321 case 0x37: strcpy(insn[i],"C.ULE.D"); type=FCOMP; break;
8322 case 0x38: strcpy(insn[i],"C.SF.D"); type=FCOMP; break;
8323 case 0x39: strcpy(insn[i],"C.NGLE.D"); type=FCOMP; break;
8324 case 0x3A: strcpy(insn[i],"C.SEQ.D"); type=FCOMP; break;
8325 case 0x3B: strcpy(insn[i],"C.NGL.D"); type=FCOMP; break;
8326 case 0x3C: strcpy(insn[i],"C.LT.D"); type=FCOMP; break;
8327 case 0x3D: strcpy(insn[i],"C.NGE.D"); type=FCOMP; break;
8328 case 0x3E: strcpy(insn[i],"C.LE.D"); type=FCOMP; break;
8329 case 0x3F: strcpy(insn[i],"C.NGT.D"); type=FCOMP; break;
8332 case 0x14: strcpy(insn[i],"C1.W"); type=NI;
8333 switch(source[i]&0x3f)
8335 case 0x20: strcpy(insn[i],"CVT.S.W"); type=FCONV; break;
8336 case 0x21: strcpy(insn[i],"CVT.D.W"); type=FCONV; break;
8339 case 0x15: strcpy(insn[i],"C1.L"); type=NI;
8340 switch(source[i]&0x3f)
8342 case 0x20: strcpy(insn[i],"CVT.S.L"); type=FCONV; break;
8343 case 0x21: strcpy(insn[i],"CVT.D.L"); type=FCONV; break;
8349 case 0x14: strcpy(insn[i],"BEQL"); type=CJUMP; break;
8350 case 0x15: strcpy(insn[i],"BNEL"); type=CJUMP; break;
8351 case 0x16: strcpy(insn[i],"BLEZL"); type=CJUMP; break;
8352 case 0x17: strcpy(insn[i],"BGTZL"); type=CJUMP; break;
8353 case 0x18: strcpy(insn[i],"DADDI"); type=IMM16; break;
8354 case 0x19: strcpy(insn[i],"DADDIU"); type=IMM16; break;
8355 case 0x1A: strcpy(insn[i],"LDL"); type=LOADLR; break;
8356 case 0x1B: strcpy(insn[i],"LDR"); type=LOADLR; break;
8358 case 0x20: strcpy(insn[i],"LB"); type=LOAD; break;
8359 case 0x21: strcpy(insn[i],"LH"); type=LOAD; break;
8360 case 0x22: strcpy(insn[i],"LWL"); type=LOADLR; break;
8361 case 0x23: strcpy(insn[i],"LW"); type=LOAD; break;
8362 case 0x24: strcpy(insn[i],"LBU"); type=LOAD; break;
8363 case 0x25: strcpy(insn[i],"LHU"); type=LOAD; break;
8364 case 0x26: strcpy(insn[i],"LWR"); type=LOADLR; break;
8366 case 0x27: strcpy(insn[i],"LWU"); type=LOAD; break;
8368 case 0x28: strcpy(insn[i],"SB"); type=STORE; break;
8369 case 0x29: strcpy(insn[i],"SH"); type=STORE; break;
8370 case 0x2A: strcpy(insn[i],"SWL"); type=STORELR; break;
8371 case 0x2B: strcpy(insn[i],"SW"); type=STORE; break;
8373 case 0x2C: strcpy(insn[i],"SDL"); type=STORELR; break;
8374 case 0x2D: strcpy(insn[i],"SDR"); type=STORELR; break;
8376 case 0x2E: strcpy(insn[i],"SWR"); type=STORELR; break;
8377 case 0x2F: strcpy(insn[i],"CACHE"); type=NOP; break;
8378 case 0x30: strcpy(insn[i],"LL"); type=NI; break;
8379 case 0x31: strcpy(insn[i],"LWC1"); type=C1LS; break;
8381 case 0x34: strcpy(insn[i],"LLD"); type=NI; break;
8382 case 0x35: strcpy(insn[i],"LDC1"); type=C1LS; break;
8383 case 0x37: strcpy(insn[i],"LD"); type=LOAD; break;
8385 case 0x38: strcpy(insn[i],"SC"); type=NI; break;
8386 case 0x39: strcpy(insn[i],"SWC1"); type=C1LS; break;
8388 case 0x3C: strcpy(insn[i],"SCD"); type=NI; break;
8389 case 0x3D: strcpy(insn[i],"SDC1"); type=C1LS; break;
8390 case 0x3F: strcpy(insn[i],"SD"); type=STORE; break;
8393 case 0x12: strcpy(insn[i],"COP2"); type=NI;
8394 op2=(source[i]>>21)&0x1f;
8396 if (source[i]&0x3f) { // use this hack to support old savestates with patched gte insns
8397 if (gte_handlers[source[i]&0x3f]!=NULL) {
8398 if (gte_regnames[source[i]&0x3f]!=NULL)
8399 strcpy(insn[i],gte_regnames[source[i]&0x3f]);
8401 snprintf(insn[i], sizeof(insn[i]), "COP2 %x", source[i]&0x3f);
8407 case 0x00: strcpy(insn[i],"MFC2"); type=COP2; break;
8408 case 0x02: strcpy(insn[i],"CFC2"); type=COP2; break;
8409 case 0x04: strcpy(insn[i],"MTC2"); type=COP2; break;
8410 case 0x06: strcpy(insn[i],"CTC2"); type=COP2; break;
8413 case 0x32: strcpy(insn[i],"LWC2"); type=C2LS; break;
8414 case 0x3A: strcpy(insn[i],"SWC2"); type=C2LS; break;
8415 case 0x3B: strcpy(insn[i],"HLECALL"); type=HLECALL; break;
8417 default: strcpy(insn[i],"???"); type=NI;
8418 printf("NI %08x @%08x (%08x)\n", source[i], addr + i*4, addr);
8423 /* Get registers/immediates */
8429 gte_rs[i]=gte_rt[i]=0;
8432 rs1[i]=(source[i]>>21)&0x1f;
8434 rt1[i]=(source[i]>>16)&0x1f;
8436 imm[i]=(short)source[i];
8440 rs1[i]=(source[i]>>21)&0x1f;
8441 rs2[i]=(source[i]>>16)&0x1f;
8444 imm[i]=(short)source[i];
8445 if(op==0x2c||op==0x2d||op==0x3f) us1[i]=rs2[i]; // 64-bit SDL/SDR/SD
8448 // LWL/LWR only load part of the register,
8449 // therefore the target register must be treated as a source too
8450 rs1[i]=(source[i]>>21)&0x1f;
8451 rs2[i]=(source[i]>>16)&0x1f;
8452 rt1[i]=(source[i]>>16)&0x1f;
8454 imm[i]=(short)source[i];
8455 if(op==0x1a||op==0x1b) us1[i]=rs2[i]; // LDR/LDL
8456 if(op==0x26) dep1[i]=rt1[i]; // LWR
8459 if (op==0x0f) rs1[i]=0; // LUI instruction has no source register
8460 else rs1[i]=(source[i]>>21)&0x1f;
8462 rt1[i]=(source[i]>>16)&0x1f;
8464 if(op>=0x0c&&op<=0x0e) { // ANDI/ORI/XORI
8465 imm[i]=(unsigned short)source[i];
8467 imm[i]=(short)source[i];
8469 if(op==0x18||op==0x19) us1[i]=rs1[i]; // DADDI/DADDIU
8470 if(op==0x0a||op==0x0b) us1[i]=rs1[i]; // SLTI/SLTIU
8471 if(op==0x0d||op==0x0e) dep1[i]=rs1[i]; // ORI/XORI
8478 // The JAL instruction writes to r31.
8485 rs1[i]=(source[i]>>21)&0x1f;
8489 // The JALR instruction writes to rd.
8491 rt1[i]=(source[i]>>11)&0x1f;
8496 rs1[i]=(source[i]>>21)&0x1f;
8497 rs2[i]=(source[i]>>16)&0x1f;
8500 if(op&2) { // BGTZ/BLEZ
8508 rs1[i]=(source[i]>>21)&0x1f;
8513 if(op2&0x10) { // BxxAL
8515 // NOTE: If the branch is not taken, r31 is still overwritten
8517 likely[i]=(op2&2)>>1;
8524 likely[i]=((source[i])>>17)&1;
8527 rs1[i]=(source[i]>>21)&0x1f; // source
8528 rs2[i]=(source[i]>>16)&0x1f; // subtract amount
8529 rt1[i]=(source[i]>>11)&0x1f; // destination
8531 if(op2==0x2a||op2==0x2b) { // SLT/SLTU
8532 us1[i]=rs1[i];us2[i]=rs2[i];
8534 else if(op2>=0x24&&op2<=0x27) { // AND/OR/XOR/NOR
8535 dep1[i]=rs1[i];dep2[i]=rs2[i];
8537 else if(op2>=0x2c&&op2<=0x2f) { // DADD/DSUB
8538 dep1[i]=rs1[i];dep2[i]=rs2[i];
8542 rs1[i]=(source[i]>>21)&0x1f; // source
8543 rs2[i]=(source[i]>>16)&0x1f; // divisor
8546 if (op2>=0x1c&&op2<=0x1f) { // DMULT/DMULTU/DDIV/DDIVU
8547 us1[i]=rs1[i];us2[i]=rs2[i];
8555 if(op2==0x10) rs1[i]=HIREG; // MFHI
8556 if(op2==0x11) rt1[i]=HIREG; // MTHI
8557 if(op2==0x12) rs1[i]=LOREG; // MFLO
8558 if(op2==0x13) rt1[i]=LOREG; // MTLO
8559 if((op2&0x1d)==0x10) rt1[i]=(source[i]>>11)&0x1f; // MFxx
8560 if((op2&0x1d)==0x11) rs1[i]=(source[i]>>21)&0x1f; // MTxx
8564 rs1[i]=(source[i]>>16)&0x1f; // target of shift
8565 rs2[i]=(source[i]>>21)&0x1f; // shift amount
8566 rt1[i]=(source[i]>>11)&0x1f; // destination
8568 // DSLLV/DSRLV/DSRAV are 64-bit
8569 if(op2>=0x14&&op2<=0x17) us1[i]=rs1[i];
8572 rs1[i]=(source[i]>>16)&0x1f;
8574 rt1[i]=(source[i]>>11)&0x1f;
8576 imm[i]=(source[i]>>6)&0x1f;
8577 // DSxx32 instructions
8578 if(op2>=0x3c) imm[i]|=0x20;
8579 // DSLL/DSRL/DSRA/DSRA32/DSRL32 but not DSLL32 require 64-bit source
8580 if(op2>=0x38&&op2!=0x3c) us1[i]=rs1[i];
8587 if(op2==0) rt1[i]=(source[i]>>16)&0x1F; // MFC0
8588 if(op2==4) rs1[i]=(source[i]>>16)&0x1F; // MTC0
8589 if(op2==4&&((source[i]>>11)&0x1f)==12) rt2[i]=CSREG; // Status
8590 if(op2==16) if((source[i]&0x3f)==0x18) rs2[i]=CCREG; // ERET
8597 if(op2<3) rt1[i]=(source[i]>>16)&0x1F; // MFC1/DMFC1/CFC1
8598 if(op2>3) rs1[i]=(source[i]>>16)&0x1F; // MTC1/DMTC1/CTC1
8599 if(op2==5) us1[i]=rs1[i]; // DMTC1
8607 if(op2<3) rt1[i]=(source[i]>>16)&0x1F; // MFC2/CFC2
8608 if(op2>3) rs1[i]=(source[i]>>16)&0x1F; // MTC2/CTC2
8610 int gr=(source[i]>>11)&0x1F;
8613 case 0x00: gte_rs[i]=1ll<<gr; break; // MFC2
8614 case 0x04: gte_rt[i]=1ll<<gr; break; // MTC2
8615 case 0x02: gte_rs[i]=1ll<<(gr+32); // CFC2
8616 if(gr==31&&!gte_reads_flags) {
8617 assem_debug("gte flag read encountered @%08x\n",addr + i*4);
8621 case 0x06: gte_rt[i]=1ll<<(gr+32); break; // CTC2
8625 rs1[i]=(source[i]>>21)&0x1F;
8629 imm[i]=(short)source[i];
8632 rs1[i]=(source[i]>>21)&0x1F;
8636 imm[i]=(short)source[i];
8637 if(op==0x32) gte_rt[i]=1ll<<((source[i]>>16)&0x1F); // LWC2
8638 else gte_rs[i]=1ll<<((source[i]>>16)&0x1F); // SWC2
8645 gte_rt[i]=1ll<<63; // every op changes flags
8646 // TODO: other regs?
8675 /* Calculate branch target addresses */
8677 ba[i]=((start+i*4+4)&0xF0000000)|(((unsigned int)source[i]<<6)>>4);
8678 else if(type==CJUMP&&rs1[i]==rs2[i]&&(op&1))
8679 ba[i]=start+i*4+8; // Ignore never taken branch
8680 else if(type==SJUMP&&rs1[i]==0&&!(op2&1))
8681 ba[i]=start+i*4+8; // Ignore never taken branch
8682 else if(type==CJUMP||type==SJUMP||type==FJUMP)
8683 ba[i]=start+i*4+4+((signed int)((unsigned int)source[i]<<16)>>14);
8686 if(i>0&&(itype[i-1]==RJUMP||itype[i-1]==UJUMP||itype[i-1]==CJUMP||itype[i-1]==SJUMP||itype[i-1]==FJUMP)) {
8688 // branch in delay slot?
8689 if(type==RJUMP||type==UJUMP||type==CJUMP||type==SJUMP||type==FJUMP) {
8690 // don't handle first branch and call interpreter if it's hit
8691 printf("branch in delay slot @%08x (%08x)\n", addr + i*4, addr);
8694 // basic load delay detection
8695 else if((type==LOAD||type==LOADLR||type==COP0||type==COP2||type==C2LS)&&rt1[i]!=0) {
8696 int t=(ba[i-1]-start)/4;
8697 if(0 <= t && t < i &&(rt1[i]==rs1[t]||rt1[i]==rs2[t])&&itype[t]!=CJUMP&&itype[t]!=SJUMP) {
8698 // jump target wants DS result - potential load delay effect
8699 printf("load delay @%08x (%08x)\n", addr + i*4, addr);
8701 bt[t+1]=1; // expected return from interpreter
8703 else if(i>=2&&rt1[i-2]==2&&rt1[i]==2&&rs1[i]!=2&&rs2[i]!=2&&rs1[i-1]!=2&&rs2[i-1]!=2&&
8704 !(i>=3&&(itype[i-3]==RJUMP||itype[i-3]==UJUMP||itype[i-3]==CJUMP||itype[i-3]==SJUMP))) {
8705 // v0 overwrite like this is a sign of trouble, bail out
8706 printf("v0 overwrite @%08x (%08x)\n", addr + i*4, addr);
8712 rs2[i-1]=rt1[i-1]=rt2[i-1]=0;
8716 i--; // don't compile the DS
8720 /* Is this the end of the block? */
8721 if(i>0&&(itype[i-1]==UJUMP||itype[i-1]==RJUMP||(source[i-1]>>16)==0x1000)) {
8722 if(rt1[i-1]==0) { // Continue past subroutine call (JAL)
8726 if(stop_after_jal) done=1;
8728 if((source[i+1]&0xfc00003f)==0x0d) done=1;
8730 // Don't recompile stuff that's already compiled
8731 if(check_addr(start+i*4+4)) done=1;
8732 // Don't get too close to the limit
8733 if(i>MAXBLOCK/2) done=1;
8735 if(itype[i]==SYSCALL&&stop_after_jal) done=1;
8736 if(itype[i]==HLECALL||itype[i]==INTCALL) done=2;
8738 // Does the block continue due to a branch?
8741 if(ba[j]==start+i*4) done=j=0; // Branch into delay slot
8742 if(ba[j]==start+i*4+4) done=j=0;
8743 if(ba[j]==start+i*4+8) done=j=0;
8746 //assert(i<MAXBLOCK-1);
8747 if(start+i*4==pagelimit-4) done=1;
8748 assert(start+i*4<pagelimit);
8749 if (i==MAXBLOCK-1) done=1;
8750 // Stop if we're compiling junk
8751 if(itype[i]==NI&&opcode[i]==0x11) {
8752 done=stop_after_jal=1;
8753 printf("Disabled speculative precompilation\n");
8757 if(itype[i-1]==UJUMP||itype[i-1]==CJUMP||itype[i-1]==SJUMP||itype[i-1]==RJUMP||itype[i-1]==FJUMP) {
8758 if(start+i*4==pagelimit) {
8764 /* Pass 2 - Register dependencies and branch targets */
8766 unneeded_registers(0,slen-1,0);
8768 /* Pass 3 - Register allocation */
8770 struct regstat current; // Current register allocations/status
8773 current.u=unneeded_reg[0];
8774 current.uu=unneeded_reg_upper[0];
8775 clear_all_regs(current.regmap);
8776 alloc_reg(¤t,0,CCREG);
8777 dirty_reg(¤t,CCREG);
8785 provisional_32bit();
8788 // First instruction is delay slot
8793 unneeded_reg_upper[0]=1;
8794 current.regmap[HOST_BTREG]=BTREG;
8802 for(hr=0;hr<HOST_REGS;hr++)
8804 // Is this really necessary?
8805 if(current.regmap[hr]==0) current.regmap[hr]=-1;
8811 if((opcode[i-2]&0x2f)==0x05) // BNE/BNEL
8813 if(rs1[i-2]==0||rs2[i-2]==0)
8816 current.is32|=1LL<<rs1[i-2];
8817 int hr=get_reg(current.regmap,rs1[i-2]|64);
8818 if(hr>=0) current.regmap[hr]=-1;
8821 current.is32|=1LL<<rs2[i-2];
8822 int hr=get_reg(current.regmap,rs2[i-2]|64);
8823 if(hr>=0) current.regmap[hr]=-1;
8829 // If something jumps here with 64-bit values
8830 // then promote those registers to 64 bits
8833 uint64_t temp_is32=current.is32;
8836 if(ba[j]==start+i*4)
8837 temp_is32&=branch_regs[j].is32;
8841 if(ba[j]==start+i*4)
8845 if(temp_is32!=current.is32) {
8846 //printf("dumping 32-bit regs (%x)\n",start+i*4);
8847 #ifndef DESTRUCTIVE_WRITEBACK
8850 for(hr=0;hr<HOST_REGS;hr++)
8852 int r=current.regmap[hr];
8855 if((current.dirty>>hr)&((current.is32&~temp_is32)>>r)&1) {
8857 //printf("restore %d\n",r);
8861 current.is32=temp_is32;
8868 memcpy(regmap_pre[i],current.regmap,sizeof(current.regmap));
8869 regs[i].wasconst=current.isconst;
8870 regs[i].was32=current.is32;
8871 regs[i].wasdirty=current.dirty;
8872 #if defined(DESTRUCTIVE_WRITEBACK) && !defined(FORCE32)
8873 // To change a dirty register from 32 to 64 bits, we must write
8874 // it out during the previous cycle (for branches, 2 cycles)
8875 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)
8877 uint64_t temp_is32=current.is32;
8880 if(ba[j]==start+i*4+4)
8881 temp_is32&=branch_regs[j].is32;
8885 if(ba[j]==start+i*4+4)
8889 if(temp_is32!=current.is32) {
8890 //printf("pre-dumping 32-bit regs (%x)\n",start+i*4);
8891 for(hr=0;hr<HOST_REGS;hr++)
8893 int r=current.regmap[hr];
8896 if((current.dirty>>hr)&((current.is32&~temp_is32)>>(r&63))&1) {
8897 if(itype[i]!=UJUMP&&itype[i]!=CJUMP&&itype[i]!=SJUMP&&itype[i]!=RJUMP&&itype[i]!=FJUMP)
8899 if(rs1[i]!=(r&63)&&rs2[i]!=(r&63))
8901 //printf("dump %d/r%d\n",hr,r);
8902 current.regmap[hr]=-1;
8903 if(get_reg(current.regmap,r|64)>=0)
8904 current.regmap[get_reg(current.regmap,r|64)]=-1;
8912 else if(i<slen-2&&bt[i+2]&&(source[i-1]>>16)!=0x1000&&(itype[i]==CJUMP||itype[i]==SJUMP||itype[i]==FJUMP))
8914 uint64_t temp_is32=current.is32;
8917 if(ba[j]==start+i*4+8)
8918 temp_is32&=branch_regs[j].is32;
8922 if(ba[j]==start+i*4+8)
8926 if(temp_is32!=current.is32) {
8927 //printf("pre-dumping 32-bit regs (%x)\n",start+i*4);
8928 for(hr=0;hr<HOST_REGS;hr++)
8930 int r=current.regmap[hr];
8933 if((current.dirty>>hr)&((current.is32&~temp_is32)>>(r&63))&1) {
8934 if(rs1[i]!=(r&63)&&rs2[i]!=(r&63)&&rs1[i+1]!=(r&63)&&rs2[i+1]!=(r&63))
8936 //printf("dump %d/r%d\n",hr,r);
8937 current.regmap[hr]=-1;
8938 if(get_reg(current.regmap,r|64)>=0)
8939 current.regmap[get_reg(current.regmap,r|64)]=-1;
8947 if(itype[i]!=UJUMP&&itype[i]!=CJUMP&&itype[i]!=SJUMP&&itype[i]!=RJUMP&&itype[i]!=FJUMP) {
8949 current.u=unneeded_reg[i+1]&~((1LL<<rs1[i])|(1LL<<rs2[i]));
8950 current.uu=unneeded_reg_upper[i+1]&~((1LL<<us1[i])|(1LL<<us2[i]));
8951 if((~current.uu>>rt1[i])&1) current.uu&=~((1LL<<dep1[i])|(1LL<<dep2[i]));
8960 current.u=branch_unneeded_reg[i]&~((1LL<<rs1[i+1])|(1LL<<rs2[i+1]));
8961 current.uu=branch_unneeded_reg_upper[i]&~((1LL<<us1[i+1])|(1LL<<us2[i+1]));
8962 if((~current.uu>>rt1[i+1])&1) current.uu&=~((1LL<<dep1[i+1])|(1LL<<dep2[i+1]));
8963 current.u&=~((1LL<<rs1[i])|(1LL<<rs2[i]));
8964 current.uu&=~((1LL<<us1[i])|(1LL<<us2[i]));
8967 } else { printf("oops, branch at end of block with no delay slot\n");exit(1); }
8971 ds=0; // Skip delay slot, already allocated as part of branch
8972 // ...but we need to alloc it in case something jumps here
8974 current.u=branch_unneeded_reg[i-1]&unneeded_reg[i+1];
8975 current.uu=branch_unneeded_reg_upper[i-1]&unneeded_reg_upper[i+1];
8977 current.u=branch_unneeded_reg[i-1];
8978 current.uu=branch_unneeded_reg_upper[i-1];
8980 current.u&=~((1LL<<rs1[i])|(1LL<<rs2[i]));
8981 current.uu&=~((1LL<<us1[i])|(1LL<<us2[i]));
8982 if((~current.uu>>rt1[i])&1) current.uu&=~((1LL<<dep1[i])|(1LL<<dep2[i]));
8985 struct regstat temp;
8986 memcpy(&temp,¤t,sizeof(current));
8987 temp.wasdirty=temp.dirty;
8988 temp.was32=temp.is32;
8989 // TODO: Take into account unconditional branches, as below
8990 delayslot_alloc(&temp,i);
8991 memcpy(regs[i].regmap,temp.regmap,sizeof(temp.regmap));
8992 regs[i].wasdirty=temp.wasdirty;
8993 regs[i].was32=temp.was32;
8994 regs[i].dirty=temp.dirty;
8995 regs[i].is32=temp.is32;
8999 // Create entry (branch target) regmap
9000 for(hr=0;hr<HOST_REGS;hr++)
9002 int r=temp.regmap[hr];
9004 if(r!=regmap_pre[i][hr]) {
9005 regs[i].regmap_entry[hr]=-1;
9010 if((current.u>>r)&1) {
9011 regs[i].regmap_entry[hr]=-1;
9012 regs[i].regmap[hr]=-1;
9013 //Don't clear regs in the delay slot as the branch might need them
9014 //current.regmap[hr]=-1;
9016 regs[i].regmap_entry[hr]=r;
9019 if((current.uu>>(r&63))&1) {
9020 regs[i].regmap_entry[hr]=-1;
9021 regs[i].regmap[hr]=-1;
9022 //Don't clear regs in the delay slot as the branch might need them
9023 //current.regmap[hr]=-1;
9025 regs[i].regmap_entry[hr]=r;
9029 // First instruction expects CCREG to be allocated
9030 if(i==0&&hr==HOST_CCREG)
9031 regs[i].regmap_entry[hr]=CCREG;
9033 regs[i].regmap_entry[hr]=-1;
9037 else { // Not delay slot
9040 //current.isconst=0; // DEBUG
9041 //current.wasconst=0; // DEBUG
9042 //regs[i].wasconst=0; // DEBUG
9043 clear_const(¤t,rt1[i]);
9044 alloc_cc(¤t,i);
9045 dirty_reg(¤t,CCREG);
9047 alloc_reg(¤t,i,31);
9048 dirty_reg(¤t,31);
9049 //assert(rs1[i+1]!=31&&rs2[i+1]!=31);
9050 //assert(rt1[i+1]!=rt1[i]);
9052 alloc_reg(¤t,i,PTEMP);
9054 //current.is32|=1LL<<rt1[i];
9057 delayslot_alloc(¤t,i+1);
9058 //current.isconst=0; // DEBUG
9060 //printf("i=%d, isconst=%x\n",i,current.isconst);
9063 //current.isconst=0;
9064 //current.wasconst=0;
9065 //regs[i].wasconst=0;
9066 clear_const(¤t,rs1[i]);
9067 clear_const(¤t,rt1[i]);
9068 alloc_cc(¤t,i);
9069 dirty_reg(¤t,CCREG);
9070 if(rs1[i]!=rt1[i+1]&&rs1[i]!=rt2[i+1]) {
9071 alloc_reg(¤t,i,rs1[i]);
9073 alloc_reg(¤t,i,rt1[i]);
9074 dirty_reg(¤t,rt1[i]);
9075 assert(rs1[i+1]!=rt1[i]&&rs2[i+1]!=rt1[i]);
9076 assert(rt1[i+1]!=rt1[i]);
9078 alloc_reg(¤t,i,PTEMP);
9082 if(rs1[i]==31) { // JALR
9083 alloc_reg(¤t,i,RHASH);
9084 #ifndef HOST_IMM_ADDR32
9085 alloc_reg(¤t,i,RHTBL);
9089 delayslot_alloc(¤t,i+1);
9091 // The delay slot overwrites our source register,
9092 // allocate a temporary register to hold the old value.
9096 delayslot_alloc(¤t,i+1);
9098 alloc_reg(¤t,i,RTEMP);
9100 //current.isconst=0; // DEBUG
9105 //current.isconst=0;
9106 //current.wasconst=0;
9107 //regs[i].wasconst=0;
9108 clear_const(¤t,rs1[i]);
9109 clear_const(¤t,rs2[i]);
9110 if((opcode[i]&0x3E)==4) // BEQ/BNE
9112 alloc_cc(¤t,i);
9113 dirty_reg(¤t,CCREG);
9114 if(rs1[i]) alloc_reg(¤t,i,rs1[i]);
9115 if(rs2[i]) alloc_reg(¤t,i,rs2[i]);
9116 if(!((current.is32>>rs1[i])&(current.is32>>rs2[i])&1))
9118 if(rs1[i]) alloc_reg64(¤t,i,rs1[i]);
9119 if(rs2[i]) alloc_reg64(¤t,i,rs2[i]);
9121 if((rs1[i]&&(rs1[i]==rt1[i+1]||rs1[i]==rt2[i+1]))||
9122 (rs2[i]&&(rs2[i]==rt1[i+1]||rs2[i]==rt2[i+1]))) {
9123 // The delay slot overwrites one of our conditions.
9124 // Allocate the branch condition registers instead.
9128 if(rs1[i]) alloc_reg(¤t,i,rs1[i]);
9129 if(rs2[i]) alloc_reg(¤t,i,rs2[i]);
9130 if(!((current.is32>>rs1[i])&(current.is32>>rs2[i])&1))
9132 if(rs1[i]) alloc_reg64(¤t,i,rs1[i]);
9133 if(rs2[i]) alloc_reg64(¤t,i,rs2[i]);
9139 delayslot_alloc(¤t,i+1);
9143 if((opcode[i]&0x3E)==6) // BLEZ/BGTZ
9145 alloc_cc(¤t,i);
9146 dirty_reg(¤t,CCREG);
9147 alloc_reg(¤t,i,rs1[i]);
9148 if(!(current.is32>>rs1[i]&1))
9150 alloc_reg64(¤t,i,rs1[i]);
9152 if(rs1[i]&&(rs1[i]==rt1[i+1]||rs1[i]==rt2[i+1])) {
9153 // The delay slot overwrites one of our conditions.
9154 // Allocate the branch condition registers instead.
9158 if(rs1[i]) alloc_reg(¤t,i,rs1[i]);
9159 if(!((current.is32>>rs1[i])&1))
9161 if(rs1[i]) alloc_reg64(¤t,i,rs1[i]);
9167 delayslot_alloc(¤t,i+1);
9171 // Don't alloc the delay slot yet because we might not execute it
9172 if((opcode[i]&0x3E)==0x14) // BEQL/BNEL
9177 alloc_cc(¤t,i);
9178 dirty_reg(¤t,CCREG);
9179 alloc_reg(¤t,i,rs1[i]);
9180 alloc_reg(¤t,i,rs2[i]);
9181 if(!((current.is32>>rs1[i])&(current.is32>>rs2[i])&1))
9183 alloc_reg64(¤t,i,rs1[i]);
9184 alloc_reg64(¤t,i,rs2[i]);
9188 if((opcode[i]&0x3E)==0x16) // BLEZL/BGTZL
9193 alloc_cc(¤t,i);
9194 dirty_reg(¤t,CCREG);
9195 alloc_reg(¤t,i,rs1[i]);
9196 if(!(current.is32>>rs1[i]&1))
9198 alloc_reg64(¤t,i,rs1[i]);
9202 //current.isconst=0;
9205 //current.isconst=0;
9206 //current.wasconst=0;
9207 //regs[i].wasconst=0;
9208 clear_const(¤t,rs1[i]);
9209 clear_const(¤t,rt1[i]);
9210 //if((opcode2[i]&0x1E)==0x0) // BLTZ/BGEZ
9211 if((opcode2[i]&0x0E)==0x0) // BLTZ/BGEZ
9213 alloc_cc(¤t,i);
9214 dirty_reg(¤t,CCREG);
9215 alloc_reg(¤t,i,rs1[i]);
9216 if(!(current.is32>>rs1[i]&1))
9218 alloc_reg64(¤t,i,rs1[i]);
9220 if (rt1[i]==31) { // BLTZAL/BGEZAL
9221 alloc_reg(¤t,i,31);
9222 dirty_reg(¤t,31);
9223 //#ifdef REG_PREFETCH
9224 //alloc_reg(¤t,i,PTEMP);
9226 //current.is32|=1LL<<rt1[i];
9228 if((rs1[i]&&(rs1[i]==rt1[i+1]||rs1[i]==rt2[i+1])) // The delay slot overwrites the branch condition.
9229 ||(rt1[i]==31&&(rs1[i+1]==31||rs2[i+1]==31||rt1[i+1]==31||rt2[i+1]==31))) { // DS touches $ra
9230 // Allocate the branch condition registers instead.
9234 if(rs1[i]) alloc_reg(¤t,i,rs1[i]);
9235 if(!((current.is32>>rs1[i])&1))
9237 if(rs1[i]) alloc_reg64(¤t,i,rs1[i]);
9243 delayslot_alloc(¤t,i+1);
9247 // Don't alloc the delay slot yet because we might not execute it
9248 if((opcode2[i]&0x1E)==0x2) // BLTZL/BGEZL
9253 alloc_cc(¤t,i);
9254 dirty_reg(¤t,CCREG);
9255 alloc_reg(¤t,i,rs1[i]);
9256 if(!(current.is32>>rs1[i]&1))
9258 alloc_reg64(¤t,i,rs1[i]);
9262 //current.isconst=0;
9268 if(likely[i]==0) // BC1F/BC1T
9270 // TODO: Theoretically we can run out of registers here on x86.
9271 // The delay slot can allocate up to six, and we need to check
9272 // CSREG before executing the delay slot. Possibly we can drop
9273 // the cycle count and then reload it after checking that the
9274 // FPU is in a usable state, or don't do out-of-order execution.
9275 alloc_cc(¤t,i);
9276 dirty_reg(¤t,CCREG);
9277 alloc_reg(¤t,i,FSREG);
9278 alloc_reg(¤t,i,CSREG);
9279 if(itype[i+1]==FCOMP) {
9280 // The delay slot overwrites the branch condition.
9281 // Allocate the branch condition registers instead.
9282 alloc_cc(¤t,i);
9283 dirty_reg(¤t,CCREG);
9284 alloc_reg(¤t,i,CSREG);
9285 alloc_reg(¤t,i,FSREG);
9289 delayslot_alloc(¤t,i+1);
9290 alloc_reg(¤t,i+1,CSREG);
9294 // Don't alloc the delay slot yet because we might not execute it
9295 if(likely[i]) // BC1FL/BC1TL
9297 alloc_cc(¤t,i);
9298 dirty_reg(¤t,CCREG);
9299 alloc_reg(¤t,i,CSREG);
9300 alloc_reg(¤t,i,FSREG);
9306 imm16_alloc(¤t,i);
9310 load_alloc(¤t,i);
9314 store_alloc(¤t,i);
9317 alu_alloc(¤t,i);
9320 shift_alloc(¤t,i);
9323 multdiv_alloc(¤t,i);
9326 shiftimm_alloc(¤t,i);
9329 mov_alloc(¤t,i);
9332 cop0_alloc(¤t,i);
9336 cop1_alloc(¤t,i);
9339 c1ls_alloc(¤t,i);
9342 c2ls_alloc(¤t,i);
9345 c2op_alloc(¤t,i);
9348 fconv_alloc(¤t,i);
9351 float_alloc(¤t,i);
9354 fcomp_alloc(¤t,i);
9359 syscall_alloc(¤t,i);
9362 pagespan_alloc(¤t,i);
9366 // Drop the upper half of registers that have become 32-bit
9367 current.uu|=current.is32&((1LL<<rt1[i])|(1LL<<rt2[i]));
9368 if(itype[i]!=UJUMP&&itype[i]!=CJUMP&&itype[i]!=SJUMP&&itype[i]!=RJUMP&&itype[i]!=FJUMP) {
9369 current.uu&=~((1LL<<us1[i])|(1LL<<us2[i]));
9370 if((~current.uu>>rt1[i])&1) current.uu&=~((1LL<<dep1[i])|(1LL<<dep2[i]));
9373 current.uu|=current.is32&((1LL<<rt1[i+1])|(1LL<<rt2[i+1]));
9374 current.uu&=~((1LL<<us1[i+1])|(1LL<<us2[i+1]));
9375 if((~current.uu>>rt1[i+1])&1) current.uu&=~((1LL<<dep1[i+1])|(1LL<<dep2[i+1]));
9376 current.uu&=~((1LL<<us1[i])|(1LL<<us2[i]));
9380 // Create entry (branch target) regmap
9381 for(hr=0;hr<HOST_REGS;hr++)
9384 r=current.regmap[hr];
9386 if(r!=regmap_pre[i][hr]) {
9387 // TODO: delay slot (?)
9388 or=get_reg(regmap_pre[i],r); // Get old mapping for this register
9389 if(or<0||(r&63)>=TEMPREG){
9390 regs[i].regmap_entry[hr]=-1;
9394 // Just move it to a different register
9395 regs[i].regmap_entry[hr]=r;
9396 // If it was dirty before, it's still dirty
9397 if((regs[i].wasdirty>>or)&1) dirty_reg(¤t,r&63);
9404 regs[i].regmap_entry[hr]=0;
9408 if((current.u>>r)&1) {
9409 regs[i].regmap_entry[hr]=-1;
9410 //regs[i].regmap[hr]=-1;
9411 current.regmap[hr]=-1;
9413 regs[i].regmap_entry[hr]=r;
9416 if((current.uu>>(r&63))&1) {
9417 regs[i].regmap_entry[hr]=-1;
9418 //regs[i].regmap[hr]=-1;
9419 current.regmap[hr]=-1;
9421 regs[i].regmap_entry[hr]=r;
9425 // Branches expect CCREG to be allocated at the target
9426 if(regmap_pre[i][hr]==CCREG)
9427 regs[i].regmap_entry[hr]=CCREG;
9429 regs[i].regmap_entry[hr]=-1;
9432 memcpy(regs[i].regmap,current.regmap,sizeof(current.regmap));
9434 /* Branch post-alloc */
9437 current.was32=current.is32;
9438 current.wasdirty=current.dirty;
9439 switch(itype[i-1]) {
9441 memcpy(&branch_regs[i-1],¤t,sizeof(current));
9442 branch_regs[i-1].isconst=0;
9443 branch_regs[i-1].wasconst=0;
9444 branch_regs[i-1].u=branch_unneeded_reg[i-1]&~((1LL<<rs1[i-1])|(1LL<<rs2[i-1]));
9445 branch_regs[i-1].uu=branch_unneeded_reg_upper[i-1]&~((1LL<<us1[i-1])|(1LL<<us2[i-1]));
9446 alloc_cc(&branch_regs[i-1],i-1);
9447 dirty_reg(&branch_regs[i-1],CCREG);
9448 if(rt1[i-1]==31) { // JAL
9449 alloc_reg(&branch_regs[i-1],i-1,31);
9450 dirty_reg(&branch_regs[i-1],31);
9451 branch_regs[i-1].is32|=1LL<<31;
9453 memcpy(&branch_regs[i-1].regmap_entry,&branch_regs[i-1].regmap,sizeof(current.regmap));
9454 memcpy(constmap[i],constmap[i-1],sizeof(current.constmap));
9457 memcpy(&branch_regs[i-1],¤t,sizeof(current));
9458 branch_regs[i-1].isconst=0;
9459 branch_regs[i-1].wasconst=0;
9460 branch_regs[i-1].u=branch_unneeded_reg[i-1]&~((1LL<<rs1[i-1])|(1LL<<rs2[i-1]));
9461 branch_regs[i-1].uu=branch_unneeded_reg_upper[i-1]&~((1LL<<us1[i-1])|(1LL<<us2[i-1]));
9462 alloc_cc(&branch_regs[i-1],i-1);
9463 dirty_reg(&branch_regs[i-1],CCREG);
9464 alloc_reg(&branch_regs[i-1],i-1,rs1[i-1]);
9465 if(rt1[i-1]!=0) { // JALR
9466 alloc_reg(&branch_regs[i-1],i-1,rt1[i-1]);
9467 dirty_reg(&branch_regs[i-1],rt1[i-1]);
9468 branch_regs[i-1].is32|=1LL<<rt1[i-1];
9471 if(rs1[i-1]==31) { // JALR
9472 alloc_reg(&branch_regs[i-1],i-1,RHASH);
9473 #ifndef HOST_IMM_ADDR32
9474 alloc_reg(&branch_regs[i-1],i-1,RHTBL);
9478 memcpy(&branch_regs[i-1].regmap_entry,&branch_regs[i-1].regmap,sizeof(current.regmap));
9479 memcpy(constmap[i],constmap[i-1],sizeof(current.constmap));
9482 if((opcode[i-1]&0x3E)==4) // BEQ/BNE
9484 alloc_cc(¤t,i-1);
9485 dirty_reg(¤t,CCREG);
9486 if((rs1[i-1]&&(rs1[i-1]==rt1[i]||rs1[i-1]==rt2[i]))||
9487 (rs2[i-1]&&(rs2[i-1]==rt1[i]||rs2[i-1]==rt2[i]))) {
9488 // The delay slot overwrote one of our conditions
9489 // Delay slot goes after the test (in order)
9490 current.u=branch_unneeded_reg[i-1]&~((1LL<<rs1[i])|(1LL<<rs2[i]));
9491 current.uu=branch_unneeded_reg_upper[i-1]&~((1LL<<us1[i])|(1LL<<us2[i]));
9492 if((~current.uu>>rt1[i])&1) current.uu&=~((1LL<<dep1[i])|(1LL<<dep2[i]));
9495 delayslot_alloc(¤t,i);
9500 current.u=branch_unneeded_reg[i-1]&~((1LL<<rs1[i-1])|(1LL<<rs2[i-1]));
9501 current.uu=branch_unneeded_reg_upper[i-1]&~((1LL<<us1[i-1])|(1LL<<us2[i-1]));
9502 // Alloc the branch condition registers
9503 if(rs1[i-1]) alloc_reg(¤t,i-1,rs1[i-1]);
9504 if(rs2[i-1]) alloc_reg(¤t,i-1,rs2[i-1]);
9505 if(!((current.is32>>rs1[i-1])&(current.is32>>rs2[i-1])&1))
9507 if(rs1[i-1]) alloc_reg64(¤t,i-1,rs1[i-1]);
9508 if(rs2[i-1]) alloc_reg64(¤t,i-1,rs2[i-1]);
9511 memcpy(&branch_regs[i-1],¤t,sizeof(current));
9512 branch_regs[i-1].isconst=0;
9513 branch_regs[i-1].wasconst=0;
9514 memcpy(&branch_regs[i-1].regmap_entry,¤t.regmap,sizeof(current.regmap));
9515 memcpy(constmap[i],constmap[i-1],sizeof(current.constmap));
9518 if((opcode[i-1]&0x3E)==6) // BLEZ/BGTZ
9520 alloc_cc(¤t,i-1);
9521 dirty_reg(¤t,CCREG);
9522 if(rs1[i-1]==rt1[i]||rs1[i-1]==rt2[i]) {
9523 // The delay slot overwrote the branch condition
9524 // Delay slot goes after the test (in order)
9525 current.u=branch_unneeded_reg[i-1]&~((1LL<<rs1[i])|(1LL<<rs2[i]));
9526 current.uu=branch_unneeded_reg_upper[i-1]&~((1LL<<us1[i])|(1LL<<us2[i]));
9527 if((~current.uu>>rt1[i])&1) current.uu&=~((1LL<<dep1[i])|(1LL<<dep2[i]));
9530 delayslot_alloc(¤t,i);
9535 current.u=branch_unneeded_reg[i-1]&~(1LL<<rs1[i-1]);
9536 current.uu=branch_unneeded_reg_upper[i-1]&~(1LL<<us1[i-1]);
9537 // Alloc the branch condition register
9538 alloc_reg(¤t,i-1,rs1[i-1]);
9539 if(!(current.is32>>rs1[i-1]&1))
9541 alloc_reg64(¤t,i-1,rs1[i-1]);
9544 memcpy(&branch_regs[i-1],¤t,sizeof(current));
9545 branch_regs[i-1].isconst=0;
9546 branch_regs[i-1].wasconst=0;
9547 memcpy(&branch_regs[i-1].regmap_entry,¤t.regmap,sizeof(current.regmap));
9548 memcpy(constmap[i],constmap[i-1],sizeof(current.constmap));
9551 // Alloc the delay slot in case the branch is taken
9552 if((opcode[i-1]&0x3E)==0x14) // BEQL/BNEL
9554 memcpy(&branch_regs[i-1],¤t,sizeof(current));
9555 branch_regs[i-1].u=(branch_unneeded_reg[i-1]&~((1LL<<rs1[i])|(1LL<<rs2[i])|(1LL<<rt1[i])|(1LL<<rt2[i])))|1;
9556 branch_regs[i-1].uu=(branch_unneeded_reg_upper[i-1]&~((1LL<<us1[i])|(1LL<<us2[i])|(1LL<<rt1[i])|(1LL<<rt2[i])))|1;
9557 if((~branch_regs[i-1].uu>>rt1[i])&1) branch_regs[i-1].uu&=~((1LL<<dep1[i])|(1LL<<dep2[i]))|1;
9558 alloc_cc(&branch_regs[i-1],i);
9559 dirty_reg(&branch_regs[i-1],CCREG);
9560 delayslot_alloc(&branch_regs[i-1],i);
9561 branch_regs[i-1].isconst=0;
9562 alloc_reg(¤t,i,CCREG); // Not taken path
9563 dirty_reg(¤t,CCREG);
9564 memcpy(&branch_regs[i-1].regmap_entry,&branch_regs[i-1].regmap,sizeof(current.regmap));
9567 if((opcode[i-1]&0x3E)==0x16) // BLEZL/BGTZL
9569 memcpy(&branch_regs[i-1],¤t,sizeof(current));
9570 branch_regs[i-1].u=(branch_unneeded_reg[i-1]&~((1LL<<rs1[i])|(1LL<<rs2[i])|(1LL<<rt1[i])|(1LL<<rt2[i])))|1;
9571 branch_regs[i-1].uu=(branch_unneeded_reg_upper[i-1]&~((1LL<<us1[i])|(1LL<<us2[i])|(1LL<<rt1[i])|(1LL<<rt2[i])))|1;
9572 if((~branch_regs[i-1].uu>>rt1[i])&1) branch_regs[i-1].uu&=~((1LL<<dep1[i])|(1LL<<dep2[i]))|1;
9573 alloc_cc(&branch_regs[i-1],i);
9574 dirty_reg(&branch_regs[i-1],CCREG);
9575 delayslot_alloc(&branch_regs[i-1],i);
9576 branch_regs[i-1].isconst=0;
9577 alloc_reg(¤t,i,CCREG); // Not taken path
9578 dirty_reg(¤t,CCREG);
9579 memcpy(&branch_regs[i-1].regmap_entry,&branch_regs[i-1].regmap,sizeof(current.regmap));
9583 //if((opcode2[i-1]&0x1E)==0) // BLTZ/BGEZ
9584 if((opcode2[i-1]&0x0E)==0) // BLTZ/BGEZ
9586 alloc_cc(¤t,i-1);
9587 dirty_reg(¤t,CCREG);
9588 if(rs1[i-1]==rt1[i]||rs1[i-1]==rt2[i]) {
9589 // The delay slot overwrote the branch condition
9590 // Delay slot goes after the test (in order)
9591 current.u=branch_unneeded_reg[i-1]&~((1LL<<rs1[i])|(1LL<<rs2[i]));
9592 current.uu=branch_unneeded_reg_upper[i-1]&~((1LL<<us1[i])|(1LL<<us2[i]));
9593 if((~current.uu>>rt1[i])&1) current.uu&=~((1LL<<dep1[i])|(1LL<<dep2[i]));
9596 delayslot_alloc(¤t,i);
9601 current.u=branch_unneeded_reg[i-1]&~(1LL<<rs1[i-1]);
9602 current.uu=branch_unneeded_reg_upper[i-1]&~(1LL<<us1[i-1]);
9603 // Alloc the branch condition register
9604 alloc_reg(¤t,i-1,rs1[i-1]);
9605 if(!(current.is32>>rs1[i-1]&1))
9607 alloc_reg64(¤t,i-1,rs1[i-1]);
9610 memcpy(&branch_regs[i-1],¤t,sizeof(current));
9611 branch_regs[i-1].isconst=0;
9612 branch_regs[i-1].wasconst=0;
9613 memcpy(&branch_regs[i-1].regmap_entry,¤t.regmap,sizeof(current.regmap));
9614 memcpy(constmap[i],constmap[i-1],sizeof(current.constmap));
9617 // Alloc the delay slot in case the branch is taken
9618 if((opcode2[i-1]&0x1E)==2) // BLTZL/BGEZL
9620 memcpy(&branch_regs[i-1],¤t,sizeof(current));
9621 branch_regs[i-1].u=(branch_unneeded_reg[i-1]&~((1LL<<rs1[i])|(1LL<<rs2[i])|(1LL<<rt1[i])|(1LL<<rt2[i])))|1;
9622 branch_regs[i-1].uu=(branch_unneeded_reg_upper[i-1]&~((1LL<<us1[i])|(1LL<<us2[i])|(1LL<<rt1[i])|(1LL<<rt2[i])))|1;
9623 if((~branch_regs[i-1].uu>>rt1[i])&1) branch_regs[i-1].uu&=~((1LL<<dep1[i])|(1LL<<dep2[i]))|1;
9624 alloc_cc(&branch_regs[i-1],i);
9625 dirty_reg(&branch_regs[i-1],CCREG);
9626 delayslot_alloc(&branch_regs[i-1],i);
9627 branch_regs[i-1].isconst=0;
9628 alloc_reg(¤t,i,CCREG); // Not taken path
9629 dirty_reg(¤t,CCREG);
9630 memcpy(&branch_regs[i-1].regmap_entry,&branch_regs[i-1].regmap,sizeof(current.regmap));
9632 // FIXME: BLTZAL/BGEZAL
9633 if(opcode2[i-1]&0x10) { // BxxZAL
9634 alloc_reg(&branch_regs[i-1],i-1,31);
9635 dirty_reg(&branch_regs[i-1],31);
9636 branch_regs[i-1].is32|=1LL<<31;
9640 if(likely[i-1]==0) // BC1F/BC1T
9642 alloc_cc(¤t,i-1);
9643 dirty_reg(¤t,CCREG);
9644 if(itype[i]==FCOMP) {
9645 // The delay slot overwrote the branch condition
9646 // Delay slot goes after the test (in order)
9647 delayslot_alloc(¤t,i);
9652 current.u=branch_unneeded_reg[i-1]&~(1LL<<rs1[i-1]);
9653 current.uu=branch_unneeded_reg_upper[i-1]&~(1LL<<us1[i-1]);
9654 // Alloc the branch condition register
9655 alloc_reg(¤t,i-1,FSREG);
9657 memcpy(&branch_regs[i-1],¤t,sizeof(current));
9658 memcpy(&branch_regs[i-1].regmap_entry,¤t.regmap,sizeof(current.regmap));
9662 // Alloc the delay slot in case the branch is taken
9663 memcpy(&branch_regs[i-1],¤t,sizeof(current));
9664 branch_regs[i-1].u=(branch_unneeded_reg[i-1]&~((1LL<<rs1[i])|(1LL<<rs2[i])|(1LL<<rt1[i])|(1LL<<rt2[i])))|1;
9665 branch_regs[i-1].uu=(branch_unneeded_reg_upper[i-1]&~((1LL<<us1[i])|(1LL<<us2[i])|(1LL<<rt1[i])|(1LL<<rt2[i])))|1;
9666 if((~branch_regs[i-1].uu>>rt1[i])&1) branch_regs[i-1].uu&=~((1LL<<dep1[i])|(1LL<<dep2[i]))|1;
9667 alloc_cc(&branch_regs[i-1],i);
9668 dirty_reg(&branch_regs[i-1],CCREG);
9669 delayslot_alloc(&branch_regs[i-1],i);
9670 branch_regs[i-1].isconst=0;
9671 alloc_reg(¤t,i,CCREG); // Not taken path
9672 dirty_reg(¤t,CCREG);
9673 memcpy(&branch_regs[i-1].regmap_entry,&branch_regs[i-1].regmap,sizeof(current.regmap));
9678 if(itype[i-1]==UJUMP||itype[i-1]==RJUMP||(source[i-1]>>16)==0x1000)
9680 if(rt1[i-1]==31) // JAL/JALR
9682 // Subroutine call will return here, don't alloc any registers
9685 clear_all_regs(current.regmap);
9686 alloc_reg(¤t,i,CCREG);
9687 dirty_reg(¤t,CCREG);
9691 // Internal branch will jump here, match registers to caller
9692 current.is32=0x3FFFFFFFFLL;
9694 clear_all_regs(current.regmap);
9695 alloc_reg(¤t,i,CCREG);
9696 dirty_reg(¤t,CCREG);
9699 if(ba[j]==start+i*4+4) {
9700 memcpy(current.regmap,branch_regs[j].regmap,sizeof(current.regmap));
9701 current.is32=branch_regs[j].is32;
9702 current.dirty=branch_regs[j].dirty;
9707 if(ba[j]==start+i*4+4) {
9708 for(hr=0;hr<HOST_REGS;hr++) {
9709 if(current.regmap[hr]!=branch_regs[j].regmap[hr]) {
9710 current.regmap[hr]=-1;
9712 current.is32&=branch_regs[j].is32;
9713 current.dirty&=branch_regs[j].dirty;
9722 // Count cycles in between branches
9724 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||itype[i]==HLECALL))
9729 else if(/*itype[i]==LOAD||*/itype[i]==STORE||itype[i]==C1LS) // load causes weird timing issues
9731 cc+=2; // 2 cycle penalty (after CLOCK_DIVIDER)
9733 else if(itype[i]==C2LS)
9743 flush_dirty_uppers(¤t);
9745 regs[i].is32=current.is32;
9746 regs[i].dirty=current.dirty;
9747 regs[i].isconst=current.isconst;
9748 memcpy(constmap[i],current.constmap,sizeof(current.constmap));
9750 for(hr=0;hr<HOST_REGS;hr++) {
9751 if(hr!=EXCLUDE_REG&®s[i].regmap[hr]>=0) {
9752 if(regmap_pre[i][hr]!=regs[i].regmap[hr]) {
9753 regs[i].wasconst&=~(1<<hr);
9757 if(current.regmap[HOST_BTREG]==BTREG) current.regmap[HOST_BTREG]=-1;
9760 /* Pass 4 - Cull unused host registers */
9764 for (i=slen-1;i>=0;i--)
9767 if(itype[i]==RJUMP||itype[i]==UJUMP||itype[i]==CJUMP||itype[i]==SJUMP||itype[i]==FJUMP)
9769 if(ba[i]<start || ba[i]>=(start+slen*4))
9771 // Branch out of this block, don't need anything
9777 // Need whatever matches the target
9779 int t=(ba[i]-start)>>2;
9780 for(hr=0;hr<HOST_REGS;hr++)
9782 if(regs[i].regmap_entry[hr]>=0) {
9783 if(regs[i].regmap_entry[hr]==regs[t].regmap_entry[hr]) nr|=1<<hr;
9787 // Conditional branch may need registers for following instructions
9788 if(itype[i]!=RJUMP&&itype[i]!=UJUMP&&(source[i]>>16)!=0x1000)
9791 nr|=needed_reg[i+2];
9792 for(hr=0;hr<HOST_REGS;hr++)
9794 if(regmap_pre[i+2][hr]>=0&&get_reg(regs[i+2].regmap_entry,regmap_pre[i+2][hr])<0) nr&=~(1<<hr);
9795 //if((regmap_entry[i+2][hr])>=0) if(!((nr>>hr)&1)) printf("%x-bogus(%d=%d)\n",start+i*4,hr,regmap_entry[i+2][hr]);
9799 // Don't need stuff which is overwritten
9800 //if(regs[i].regmap[hr]!=regmap_pre[i][hr]) nr&=~(1<<hr);
9801 //if(regs[i].regmap[hr]<0) nr&=~(1<<hr);
9802 // Merge in delay slot
9803 for(hr=0;hr<HOST_REGS;hr++)
9806 // These are overwritten unless the branch is "likely"
9807 // and the delay slot is nullified if not taken
9808 if(rt1[i+1]&&rt1[i+1]==(regs[i].regmap[hr]&63)) nr&=~(1<<hr);
9809 if(rt2[i+1]&&rt2[i+1]==(regs[i].regmap[hr]&63)) nr&=~(1<<hr);
9811 if(us1[i+1]==(regmap_pre[i][hr]&63)) nr|=1<<hr;
9812 if(us2[i+1]==(regmap_pre[i][hr]&63)) nr|=1<<hr;
9813 if(rs1[i+1]==regmap_pre[i][hr]) nr|=1<<hr;
9814 if(rs2[i+1]==regmap_pre[i][hr]) nr|=1<<hr;
9815 if(us1[i+1]==(regs[i].regmap_entry[hr]&63)) nr|=1<<hr;
9816 if(us2[i+1]==(regs[i].regmap_entry[hr]&63)) nr|=1<<hr;
9817 if(rs1[i+1]==regs[i].regmap_entry[hr]) nr|=1<<hr;
9818 if(rs2[i+1]==regs[i].regmap_entry[hr]) nr|=1<<hr;
9819 if(dep1[i+1]&&!((unneeded_reg_upper[i]>>dep1[i+1])&1)) {
9820 if(dep1[i+1]==(regmap_pre[i][hr]&63)) nr|=1<<hr;
9821 if(dep2[i+1]==(regmap_pre[i][hr]&63)) nr|=1<<hr;
9823 if(dep2[i+1]&&!((unneeded_reg_upper[i]>>dep2[i+1])&1)) {
9824 if(dep1[i+1]==(regs[i].regmap_entry[hr]&63)) nr|=1<<hr;
9825 if(dep2[i+1]==(regs[i].regmap_entry[hr]&63)) nr|=1<<hr;
9827 if(itype[i+1]==STORE || itype[i+1]==STORELR || (opcode[i+1]&0x3b)==0x39 || (opcode[i+1]&0x3b)==0x3a) {
9828 if(regmap_pre[i][hr]==INVCP) nr|=1<<hr;
9829 if(regs[i].regmap_entry[hr]==INVCP) nr|=1<<hr;
9833 else if(itype[i]==SYSCALL||itype[i]==HLECALL||itype[i]==INTCALL)
9835 // SYSCALL instruction (software interrupt)
9838 else if(itype[i]==COP0 && (source[i]&0x3f)==0x18)
9840 // ERET instruction (return from interrupt)
9846 for(hr=0;hr<HOST_REGS;hr++) {
9847 if(regmap_pre[i+1][hr]>=0&&get_reg(regs[i+1].regmap_entry,regmap_pre[i+1][hr])<0) nr&=~(1<<hr);
9848 if(regs[i].regmap[hr]!=regmap_pre[i+1][hr]) nr&=~(1<<hr);
9849 if(regs[i].regmap[hr]!=regmap_pre[i][hr]) nr&=~(1<<hr);
9850 if(regs[i].regmap[hr]<0) nr&=~(1<<hr);
9854 for(hr=0;hr<HOST_REGS;hr++)
9856 // Overwritten registers are not needed
9857 if(rt1[i]&&rt1[i]==(regs[i].regmap[hr]&63)) nr&=~(1<<hr);
9858 if(rt2[i]&&rt2[i]==(regs[i].regmap[hr]&63)) nr&=~(1<<hr);
9859 if(FTEMP==(regs[i].regmap[hr]&63)) nr&=~(1<<hr);
9860 // Source registers are needed
9861 if(us1[i]==(regmap_pre[i][hr]&63)) nr|=1<<hr;
9862 if(us2[i]==(regmap_pre[i][hr]&63)) nr|=1<<hr;
9863 if(rs1[i]==regmap_pre[i][hr]) nr|=1<<hr;
9864 if(rs2[i]==regmap_pre[i][hr]) nr|=1<<hr;
9865 if(us1[i]==(regs[i].regmap_entry[hr]&63)) nr|=1<<hr;
9866 if(us2[i]==(regs[i].regmap_entry[hr]&63)) nr|=1<<hr;
9867 if(rs1[i]==regs[i].regmap_entry[hr]) nr|=1<<hr;
9868 if(rs2[i]==regs[i].regmap_entry[hr]) nr|=1<<hr;
9869 if(dep1[i]&&!((unneeded_reg_upper[i]>>dep1[i])&1)) {
9870 if(dep1[i]==(regmap_pre[i][hr]&63)) nr|=1<<hr;
9871 if(dep1[i]==(regs[i].regmap_entry[hr]&63)) nr|=1<<hr;
9873 if(dep2[i]&&!((unneeded_reg_upper[i]>>dep2[i])&1)) {
9874 if(dep2[i]==(regmap_pre[i][hr]&63)) nr|=1<<hr;
9875 if(dep2[i]==(regs[i].regmap_entry[hr]&63)) nr|=1<<hr;
9877 if(itype[i]==STORE || itype[i]==STORELR || (opcode[i]&0x3b)==0x39 || (opcode[i]&0x3b)==0x3a) {
9878 if(regmap_pre[i][hr]==INVCP) nr|=1<<hr;
9879 if(regs[i].regmap_entry[hr]==INVCP) nr|=1<<hr;
9881 // Don't store a register immediately after writing it,
9882 // may prevent dual-issue.
9883 // But do so if this is a branch target, otherwise we
9884 // might have to load the register before the branch.
9885 if(i>0&&!bt[i]&&((regs[i].wasdirty>>hr)&1)) {
9886 if((regmap_pre[i][hr]>0&®map_pre[i][hr]<64&&!((unneeded_reg[i]>>regmap_pre[i][hr])&1)) ||
9887 (regmap_pre[i][hr]>64&&!((unneeded_reg_upper[i]>>(regmap_pre[i][hr]&63))&1)) ) {
9888 if(rt1[i-1]==(regmap_pre[i][hr]&63)) nr|=1<<hr;
9889 if(rt2[i-1]==(regmap_pre[i][hr]&63)) nr|=1<<hr;
9891 if((regs[i].regmap_entry[hr]>0&®s[i].regmap_entry[hr]<64&&!((unneeded_reg[i]>>regs[i].regmap_entry[hr])&1)) ||
9892 (regs[i].regmap_entry[hr]>64&&!((unneeded_reg_upper[i]>>(regs[i].regmap_entry[hr]&63))&1)) ) {
9893 if(rt1[i-1]==(regs[i].regmap_entry[hr]&63)) nr|=1<<hr;
9894 if(rt2[i-1]==(regs[i].regmap_entry[hr]&63)) nr|=1<<hr;
9898 // Cycle count is needed at branches. Assume it is needed at the target too.
9899 if(i==0||bt[i]||itype[i]==CJUMP||itype[i]==FJUMP||itype[i]==SPAN) {
9900 if(regmap_pre[i][HOST_CCREG]==CCREG) nr|=1<<HOST_CCREG;
9901 if(regs[i].regmap_entry[HOST_CCREG]==CCREG) nr|=1<<HOST_CCREG;
9906 // Deallocate unneeded registers
9907 for(hr=0;hr<HOST_REGS;hr++)
9910 if(regs[i].regmap_entry[hr]!=CCREG) regs[i].regmap_entry[hr]=-1;
9911 if((regs[i].regmap[hr]&63)!=rs1[i] && (regs[i].regmap[hr]&63)!=rs2[i] &&
9912 (regs[i].regmap[hr]&63)!=rt1[i] && (regs[i].regmap[hr]&63)!=rt2[i] &&
9913 (regs[i].regmap[hr]&63)!=PTEMP && (regs[i].regmap[hr]&63)!=CCREG)
9915 if(itype[i]!=RJUMP&&itype[i]!=UJUMP&&(source[i]>>16)!=0x1000)
9918 regs[i].regmap[hr]=-1;
9919 regs[i].isconst&=~(1<<hr);
9921 regmap_pre[i+2][hr]=-1;
9922 regs[i+2].wasconst&=~(1<<hr);
9927 if(itype[i]==RJUMP||itype[i]==UJUMP||itype[i]==CJUMP||itype[i]==SJUMP||itype[i]==FJUMP)
9929 int d1=0,d2=0,map=0,temp=0;
9930 if(get_reg(regs[i].regmap,rt1[i+1]|64)>=0||get_reg(branch_regs[i].regmap,rt1[i+1]|64)>=0)
9936 if(itype[i+1]==LOAD || itype[i+1]==LOADLR ||
9937 itype[i+1]==STORE || itype[i+1]==STORELR ||
9938 itype[i+1]==C1LS || itype[i+1]==C2LS)
9941 if(itype[i+1]==STORE || itype[i+1]==STORELR ||
9942 (opcode[i+1]&0x3b)==0x39 || (opcode[i+1]&0x3b)==0x3a) { // SWC1/SDC1 || SWC2/SDC2
9945 if(itype[i+1]==LOADLR || itype[i+1]==STORELR ||
9946 itype[i+1]==C1LS || itype[i+1]==C2LS)
9948 if((regs[i].regmap[hr]&63)!=rs1[i] && (regs[i].regmap[hr]&63)!=rs2[i] &&
9949 (regs[i].regmap[hr]&63)!=rt1[i] && (regs[i].regmap[hr]&63)!=rt2[i] &&
9950 (regs[i].regmap[hr]&63)!=rt1[i+1] && (regs[i].regmap[hr]&63)!=rt2[i+1] &&
9951 (regs[i].regmap[hr]^64)!=us1[i+1] && (regs[i].regmap[hr]^64)!=us2[i+1] &&
9952 (regs[i].regmap[hr]^64)!=d1 && (regs[i].regmap[hr]^64)!=d2 &&
9953 regs[i].regmap[hr]!=rs1[i+1] && regs[i].regmap[hr]!=rs2[i+1] &&
9954 (regs[i].regmap[hr]&63)!=temp && regs[i].regmap[hr]!=PTEMP &&
9955 regs[i].regmap[hr]!=RHASH && regs[i].regmap[hr]!=RHTBL &&
9956 regs[i].regmap[hr]!=RTEMP && regs[i].regmap[hr]!=CCREG &&
9957 regs[i].regmap[hr]!=map )
9959 regs[i].regmap[hr]=-1;
9960 regs[i].isconst&=~(1<<hr);
9961 if((branch_regs[i].regmap[hr]&63)!=rs1[i] && (branch_regs[i].regmap[hr]&63)!=rs2[i] &&
9962 (branch_regs[i].regmap[hr]&63)!=rt1[i] && (branch_regs[i].regmap[hr]&63)!=rt2[i] &&
9963 (branch_regs[i].regmap[hr]&63)!=rt1[i+1] && (branch_regs[i].regmap[hr]&63)!=rt2[i+1] &&
9964 (branch_regs[i].regmap[hr]^64)!=us1[i+1] && (branch_regs[i].regmap[hr]^64)!=us2[i+1] &&
9965 (branch_regs[i].regmap[hr]^64)!=d1 && (branch_regs[i].regmap[hr]^64)!=d2 &&
9966 branch_regs[i].regmap[hr]!=rs1[i+1] && branch_regs[i].regmap[hr]!=rs2[i+1] &&
9967 (branch_regs[i].regmap[hr]&63)!=temp && branch_regs[i].regmap[hr]!=PTEMP &&
9968 branch_regs[i].regmap[hr]!=RHASH && branch_regs[i].regmap[hr]!=RHTBL &&
9969 branch_regs[i].regmap[hr]!=RTEMP && branch_regs[i].regmap[hr]!=CCREG &&
9970 branch_regs[i].regmap[hr]!=map)
9972 branch_regs[i].regmap[hr]=-1;
9973 branch_regs[i].regmap_entry[hr]=-1;
9974 if(itype[i]!=RJUMP&&itype[i]!=UJUMP&&(source[i]>>16)!=0x1000)
9976 if(!likely[i]&&i<slen-2) {
9977 regmap_pre[i+2][hr]=-1;
9978 regs[i+2].wasconst&=~(1<<hr);
9989 int d1=0,d2=0,map=-1,temp=-1;
9990 if(get_reg(regs[i].regmap,rt1[i]|64)>=0)
9996 if(itype[i]==LOAD || itype[i]==LOADLR ||
9997 itype[i]==STORE || itype[i]==STORELR ||
9998 itype[i]==C1LS || itype[i]==C2LS)
10000 } else if(itype[i]==STORE || itype[i]==STORELR ||
10001 (opcode[i]&0x3b)==0x39 || (opcode[i]&0x3b)==0x3a) { // SWC1/SDC1 || SWC2/SDC2
10004 if(itype[i]==LOADLR || itype[i]==STORELR ||
10005 itype[i]==C1LS || itype[i]==C2LS)
10007 if((regs[i].regmap[hr]&63)!=rt1[i] && (regs[i].regmap[hr]&63)!=rt2[i] &&
10008 (regs[i].regmap[hr]^64)!=us1[i] && (regs[i].regmap[hr]^64)!=us2[i] &&
10009 (regs[i].regmap[hr]^64)!=d1 && (regs[i].regmap[hr]^64)!=d2 &&
10010 regs[i].regmap[hr]!=rs1[i] && regs[i].regmap[hr]!=rs2[i] &&
10011 (regs[i].regmap[hr]&63)!=temp && regs[i].regmap[hr]!=map &&
10012 (itype[i]!=SPAN||regs[i].regmap[hr]!=CCREG))
10014 if(i<slen-1&&!is_ds[i]) {
10015 if(regmap_pre[i+1][hr]!=-1 || regs[i].regmap[hr]!=-1)
10016 if(regmap_pre[i+1][hr]!=regs[i].regmap[hr])
10017 if(regs[i].regmap[hr]<64||!((regs[i].was32>>(regs[i].regmap[hr]&63))&1))
10019 printf("fail: %x (%d %d!=%d)\n",start+i*4,hr,regmap_pre[i+1][hr],regs[i].regmap[hr]);
10020 assert(regmap_pre[i+1][hr]==regs[i].regmap[hr]);
10022 regmap_pre[i+1][hr]=-1;
10023 if(regs[i+1].regmap_entry[hr]==CCREG) regs[i+1].regmap_entry[hr]=-1;
10024 regs[i+1].wasconst&=~(1<<hr);
10026 regs[i].regmap[hr]=-1;
10027 regs[i].isconst&=~(1<<hr);
10035 /* Pass 5 - Pre-allocate registers */
10037 // If a register is allocated during a loop, try to allocate it for the
10038 // entire loop, if possible. This avoids loading/storing registers
10039 // inside of the loop.
10041 signed char f_regmap[HOST_REGS];
10042 clear_all_regs(f_regmap);
10043 for(i=0;i<slen-1;i++)
10045 if(itype[i]==UJUMP||itype[i]==CJUMP||itype[i]==SJUMP||itype[i]==FJUMP)
10047 if(ba[i]>=start && ba[i]<(start+i*4))
10048 if(itype[i+1]==NOP||itype[i+1]==MOV||itype[i+1]==ALU
10049 ||itype[i+1]==SHIFTIMM||itype[i+1]==IMM16||itype[i+1]==LOAD
10050 ||itype[i+1]==STORE||itype[i+1]==STORELR||itype[i+1]==C1LS
10051 ||itype[i+1]==SHIFT||itype[i+1]==COP1||itype[i+1]==FLOAT
10052 ||itype[i+1]==FCOMP||itype[i+1]==FCONV
10053 ||itype[i+1]==COP2||itype[i+1]==C2LS||itype[i+1]==C2OP)
10055 int t=(ba[i]-start)>>2;
10056 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
10057 if(t<2||(itype[t-2]!=UJUMP&&itype[t-2]!=RJUMP)||rt1[t-2]!=31) // call/ret assumes no registers allocated
10058 for(hr=0;hr<HOST_REGS;hr++)
10060 if(regs[i].regmap[hr]>64) {
10061 if(!((regs[i].dirty>>hr)&1))
10062 f_regmap[hr]=regs[i].regmap[hr];
10063 else f_regmap[hr]=-1;
10065 else if(regs[i].regmap[hr]>=0) {
10066 if(f_regmap[hr]!=regs[i].regmap[hr]) {
10067 // dealloc old register
10069 for(n=0;n<HOST_REGS;n++)
10071 if(f_regmap[n]==regs[i].regmap[hr]) {f_regmap[n]=-1;}
10073 // and alloc new one
10074 f_regmap[hr]=regs[i].regmap[hr];
10077 if(branch_regs[i].regmap[hr]>64) {
10078 if(!((branch_regs[i].dirty>>hr)&1))
10079 f_regmap[hr]=branch_regs[i].regmap[hr];
10080 else f_regmap[hr]=-1;
10082 else if(branch_regs[i].regmap[hr]>=0) {
10083 if(f_regmap[hr]!=branch_regs[i].regmap[hr]) {
10084 // dealloc old register
10086 for(n=0;n<HOST_REGS;n++)
10088 if(f_regmap[n]==branch_regs[i].regmap[hr]) {f_regmap[n]=-1;}
10090 // and alloc new one
10091 f_regmap[hr]=branch_regs[i].regmap[hr];
10095 if(count_free_regs(regs[i].regmap)<=minimum_free_regs[i+1])
10096 f_regmap[hr]=branch_regs[i].regmap[hr];
10098 if(count_free_regs(branch_regs[i].regmap)<=minimum_free_regs[i+1])
10099 f_regmap[hr]=branch_regs[i].regmap[hr];
10101 // Avoid dirty->clean transition
10102 #ifdef DESTRUCTIVE_WRITEBACK
10103 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;
10105 // This check is only strictly required in the DESTRUCTIVE_WRITEBACK
10106 // case above, however it's always a good idea. We can't hoist the
10107 // load if the register was already allocated, so there's no point
10108 // wasting time analyzing most of these cases. It only "succeeds"
10109 // when the mapping was different and the load can be replaced with
10110 // a mov, which is of negligible benefit. So such cases are
10112 if(f_regmap[hr]>0) {
10113 if(regs[t].regmap[hr]==f_regmap[hr]||(regs[t].regmap_entry[hr]<0&&get_reg(regmap_pre[t],f_regmap[hr])<0)) {
10114 int r=f_regmap[hr];
10117 //printf("Test %x -> %x, %x %d/%d\n",start+i*4,ba[i],start+j*4,hr,r);
10118 if(r<34&&((unneeded_reg[j]>>r)&1)) break;
10119 if(r>63&&((unneeded_reg_upper[j]>>(r&63))&1)) break;
10121 // NB This can exclude the case where the upper-half
10122 // register is lower numbered than the lower-half
10123 // register. Not sure if it's worth fixing...
10124 if(get_reg(regs[j].regmap,r&63)<0) break;
10125 if(get_reg(regs[j].regmap_entry,r&63)<0) break;
10126 if(regs[j].is32&(1LL<<(r&63))) break;
10128 if(regs[j].regmap[hr]==f_regmap[hr]&&(f_regmap[hr]&63)<TEMPREG) {
10129 //printf("Hit %x -> %x, %x %d/%d\n",start+i*4,ba[i],start+j*4,hr,r);
10131 if(regs[i].regmap[hr]==-1&&branch_regs[i].regmap[hr]==-1) {
10132 if(get_reg(regs[i+2].regmap,f_regmap[hr])>=0) break;
10134 if(get_reg(regs[i].regmap,r&63)<0) break;
10135 if(get_reg(branch_regs[i].regmap,r&63)<0) break;
10138 while(k>1&®s[k-1].regmap[hr]==-1) {
10139 if(count_free_regs(regs[k-1].regmap)<=minimum_free_regs[k-1]) {
10140 //printf("no free regs for store %x\n",start+(k-1)*4);
10143 if(get_reg(regs[k-1].regmap,f_regmap[hr])>=0) {
10144 //printf("no-match due to different register\n");
10147 if(itype[k-2]==UJUMP||itype[k-2]==RJUMP||itype[k-2]==CJUMP||itype[k-2]==SJUMP||itype[k-2]==FJUMP) {
10148 //printf("no-match due to branch\n");
10151 // call/ret fast path assumes no registers allocated
10152 if(k>2&&(itype[k-3]==UJUMP||itype[k-3]==RJUMP)&&rt1[k-3]==31) {
10156 // NB This can exclude the case where the upper-half
10157 // register is lower numbered than the lower-half
10158 // register. Not sure if it's worth fixing...
10159 if(get_reg(regs[k-1].regmap,r&63)<0) break;
10160 if(regs[k-1].is32&(1LL<<(r&63))) break;
10165 if((regs[k].is32&(1LL<<f_regmap[hr]))!=
10166 (regs[i+2].was32&(1LL<<f_regmap[hr]))) {
10167 //printf("bad match after branch\n");
10171 if(regs[k-1].regmap[hr]==f_regmap[hr]&®map_pre[k][hr]==f_regmap[hr]) {
10172 //printf("Extend r%d, %x ->\n",hr,start+k*4);
10174 regs[k].regmap_entry[hr]=f_regmap[hr];
10175 regs[k].regmap[hr]=f_regmap[hr];
10176 regmap_pre[k+1][hr]=f_regmap[hr];
10177 regs[k].wasdirty&=~(1<<hr);
10178 regs[k].dirty&=~(1<<hr);
10179 regs[k].wasdirty|=(1<<hr)®s[k-1].dirty;
10180 regs[k].dirty|=(1<<hr)®s[k].wasdirty;
10181 regs[k].wasconst&=~(1<<hr);
10182 regs[k].isconst&=~(1<<hr);
10187 //printf("Fail Extend r%d, %x ->\n",hr,start+k*4);
10190 assert(regs[i-1].regmap[hr]==f_regmap[hr]);
10191 if(regs[i-1].regmap[hr]==f_regmap[hr]&®map_pre[i][hr]==f_regmap[hr]) {
10192 //printf("OK fill %x (r%d)\n",start+i*4,hr);
10193 regs[i].regmap_entry[hr]=f_regmap[hr];
10194 regs[i].regmap[hr]=f_regmap[hr];
10195 regs[i].wasdirty&=~(1<<hr);
10196 regs[i].dirty&=~(1<<hr);
10197 regs[i].wasdirty|=(1<<hr)®s[i-1].dirty;
10198 regs[i].dirty|=(1<<hr)®s[i-1].dirty;
10199 regs[i].wasconst&=~(1<<hr);
10200 regs[i].isconst&=~(1<<hr);
10201 branch_regs[i].regmap_entry[hr]=f_regmap[hr];
10202 branch_regs[i].wasdirty&=~(1<<hr);
10203 branch_regs[i].wasdirty|=(1<<hr)®s[i].dirty;
10204 branch_regs[i].regmap[hr]=f_regmap[hr];
10205 branch_regs[i].dirty&=~(1<<hr);
10206 branch_regs[i].dirty|=(1<<hr)®s[i].dirty;
10207 branch_regs[i].wasconst&=~(1<<hr);
10208 branch_regs[i].isconst&=~(1<<hr);
10209 if(itype[i]!=RJUMP&&itype[i]!=UJUMP&&(source[i]>>16)!=0x1000) {
10210 regmap_pre[i+2][hr]=f_regmap[hr];
10211 regs[i+2].wasdirty&=~(1<<hr);
10212 regs[i+2].wasdirty|=(1<<hr)®s[i].dirty;
10213 assert((branch_regs[i].is32&(1LL<<f_regmap[hr]))==
10214 (regs[i+2].was32&(1LL<<f_regmap[hr])));
10219 // Alloc register clean at beginning of loop,
10220 // but may dirty it in pass 6
10221 regs[k].regmap_entry[hr]=f_regmap[hr];
10222 regs[k].regmap[hr]=f_regmap[hr];
10223 regs[k].dirty&=~(1<<hr);
10224 regs[k].wasconst&=~(1<<hr);
10225 regs[k].isconst&=~(1<<hr);
10226 if(itype[k]==UJUMP||itype[k]==RJUMP||itype[k]==CJUMP||itype[k]==SJUMP||itype[k]==FJUMP) {
10227 branch_regs[k].regmap_entry[hr]=f_regmap[hr];
10228 branch_regs[k].regmap[hr]=f_regmap[hr];
10229 branch_regs[k].dirty&=~(1<<hr);
10230 branch_regs[k].wasconst&=~(1<<hr);
10231 branch_regs[k].isconst&=~(1<<hr);
10232 if(itype[k]!=RJUMP&&itype[k]!=UJUMP&&(source[k]>>16)!=0x1000) {
10233 regmap_pre[k+2][hr]=f_regmap[hr];
10234 regs[k+2].wasdirty&=~(1<<hr);
10235 assert((branch_regs[k].is32&(1LL<<f_regmap[hr]))==
10236 (regs[k+2].was32&(1LL<<f_regmap[hr])));
10241 regmap_pre[k+1][hr]=f_regmap[hr];
10242 regs[k+1].wasdirty&=~(1<<hr);
10245 if(regs[j].regmap[hr]==f_regmap[hr])
10246 regs[j].regmap_entry[hr]=f_regmap[hr];
10250 if(regs[j].regmap[hr]>=0)
10252 if(get_reg(regs[j].regmap,f_regmap[hr])>=0) {
10253 //printf("no-match due to different register\n");
10256 if((regs[j+1].is32&(1LL<<f_regmap[hr]))!=(regs[j].is32&(1LL<<f_regmap[hr]))) {
10257 //printf("32/64 mismatch %x %d\n",start+j*4,hr);
10260 if(itype[j]==UJUMP||itype[j]==RJUMP||(source[j]>>16)==0x1000)
10262 // Stop on unconditional branch
10265 if(itype[j]==CJUMP||itype[j]==SJUMP||itype[j]==FJUMP)
10268 if(count_free_regs(regs[j].regmap)<=minimum_free_regs[j+1])
10271 if(count_free_regs(branch_regs[j].regmap)<=minimum_free_regs[j+1])
10274 if(get_reg(branch_regs[j].regmap,f_regmap[hr])>=0) {
10275 //printf("no-match due to different register (branch)\n");
10279 if(count_free_regs(regs[j].regmap)<=minimum_free_regs[j]) {
10280 //printf("No free regs for store %x\n",start+j*4);
10283 if(f_regmap[hr]>=64) {
10284 if(regs[j].is32&(1LL<<(f_regmap[hr]&63))) {
10289 if(get_reg(regs[j].regmap,f_regmap[hr]&63)<0) {
10300 // Non branch or undetermined branch target
10301 for(hr=0;hr<HOST_REGS;hr++)
10303 if(hr!=EXCLUDE_REG) {
10304 if(regs[i].regmap[hr]>64) {
10305 if(!((regs[i].dirty>>hr)&1))
10306 f_regmap[hr]=regs[i].regmap[hr];
10308 else if(regs[i].regmap[hr]>=0) {
10309 if(f_regmap[hr]!=regs[i].regmap[hr]) {
10310 // dealloc old register
10312 for(n=0;n<HOST_REGS;n++)
10314 if(f_regmap[n]==regs[i].regmap[hr]) {f_regmap[n]=-1;}
10316 // and alloc new one
10317 f_regmap[hr]=regs[i].regmap[hr];
10322 // Try to restore cycle count at branch targets
10324 for(j=i;j<slen-1;j++) {
10325 if(regs[j].regmap[HOST_CCREG]!=-1) break;
10326 if(count_free_regs(regs[j].regmap)<=minimum_free_regs[j]) {
10327 //printf("no free regs for store %x\n",start+j*4);
10331 if(regs[j].regmap[HOST_CCREG]==CCREG) {
10333 //printf("Extend CC, %x -> %x\n",start+k*4,start+j*4);
10335 regs[k].regmap_entry[HOST_CCREG]=CCREG;
10336 regs[k].regmap[HOST_CCREG]=CCREG;
10337 regmap_pre[k+1][HOST_CCREG]=CCREG;
10338 regs[k+1].wasdirty|=1<<HOST_CCREG;
10339 regs[k].dirty|=1<<HOST_CCREG;
10340 regs[k].wasconst&=~(1<<HOST_CCREG);
10341 regs[k].isconst&=~(1<<HOST_CCREG);
10344 regs[j].regmap_entry[HOST_CCREG]=CCREG;
10346 // Work backwards from the branch target
10347 if(j>i&&f_regmap[HOST_CCREG]==CCREG)
10349 //printf("Extend backwards\n");
10352 while(regs[k-1].regmap[HOST_CCREG]==-1) {
10353 if(count_free_regs(regs[k-1].regmap)<=minimum_free_regs[k-1]) {
10354 //printf("no free regs for store %x\n",start+(k-1)*4);
10359 if(regs[k-1].regmap[HOST_CCREG]==CCREG) {
10360 //printf("Extend CC, %x ->\n",start+k*4);
10362 regs[k].regmap_entry[HOST_CCREG]=CCREG;
10363 regs[k].regmap[HOST_CCREG]=CCREG;
10364 regmap_pre[k+1][HOST_CCREG]=CCREG;
10365 regs[k+1].wasdirty|=1<<HOST_CCREG;
10366 regs[k].dirty|=1<<HOST_CCREG;
10367 regs[k].wasconst&=~(1<<HOST_CCREG);
10368 regs[k].isconst&=~(1<<HOST_CCREG);
10373 //printf("Fail Extend CC, %x ->\n",start+k*4);
10377 if(itype[i]!=STORE&&itype[i]!=STORELR&&itype[i]!=C1LS&&itype[i]!=SHIFT&&
10378 itype[i]!=NOP&&itype[i]!=MOV&&itype[i]!=ALU&&itype[i]!=SHIFTIMM&&
10379 itype[i]!=IMM16&&itype[i]!=LOAD&&itype[i]!=COP1&&itype[i]!=FLOAT&&
10380 itype[i]!=FCONV&&itype[i]!=FCOMP)
10382 memcpy(f_regmap,regs[i].regmap,sizeof(f_regmap));
10387 // Cache memory offset or tlb map pointer if a register is available
10388 #ifndef HOST_IMM_ADDR32
10393 int earliest_available[HOST_REGS];
10394 int loop_start[HOST_REGS];
10395 int score[HOST_REGS];
10396 int end[HOST_REGS];
10397 int reg=using_tlb?MMREG:ROREG;
10400 for(hr=0;hr<HOST_REGS;hr++) {
10401 score[hr]=0;earliest_available[hr]=0;
10402 loop_start[hr]=MAXBLOCK;
10404 for(i=0;i<slen-1;i++)
10406 // Can't do anything if no registers are available
10407 if(count_free_regs(regs[i].regmap)<=minimum_free_regs[i]) {
10408 for(hr=0;hr<HOST_REGS;hr++) {
10409 score[hr]=0;earliest_available[hr]=i+1;
10410 loop_start[hr]=MAXBLOCK;
10413 if(itype[i]==UJUMP||itype[i]==RJUMP||itype[i]==CJUMP||itype[i]==SJUMP||itype[i]==FJUMP) {
10415 if(count_free_regs(branch_regs[i].regmap)<=minimum_free_regs[i+1]) {
10416 for(hr=0;hr<HOST_REGS;hr++) {
10417 score[hr]=0;earliest_available[hr]=i+1;
10418 loop_start[hr]=MAXBLOCK;
10422 if(count_free_regs(regs[i].regmap)<=minimum_free_regs[i+1]) {
10423 for(hr=0;hr<HOST_REGS;hr++) {
10424 score[hr]=0;earliest_available[hr]=i+1;
10425 loop_start[hr]=MAXBLOCK;
10430 // Mark unavailable registers
10431 for(hr=0;hr<HOST_REGS;hr++) {
10432 if(regs[i].regmap[hr]>=0) {
10433 score[hr]=0;earliest_available[hr]=i+1;
10434 loop_start[hr]=MAXBLOCK;
10436 if(itype[i]==UJUMP||itype[i]==RJUMP||itype[i]==CJUMP||itype[i]==SJUMP||itype[i]==FJUMP) {
10437 if(branch_regs[i].regmap[hr]>=0) {
10438 score[hr]=0;earliest_available[hr]=i+2;
10439 loop_start[hr]=MAXBLOCK;
10443 // No register allocations after unconditional jumps
10444 if(itype[i]==UJUMP||itype[i]==RJUMP||(source[i]>>16)==0x1000)
10446 for(hr=0;hr<HOST_REGS;hr++) {
10447 score[hr]=0;earliest_available[hr]=i+2;
10448 loop_start[hr]=MAXBLOCK;
10450 i++; // Skip delay slot too
10451 //printf("skip delay slot: %x\n",start+i*4);
10455 if(itype[i]==LOAD||itype[i]==LOADLR||
10456 itype[i]==STORE||itype[i]==STORELR||itype[i]==C1LS) {
10457 for(hr=0;hr<HOST_REGS;hr++) {
10458 if(hr!=EXCLUDE_REG) {
10460 for(j=i;j<slen-1;j++) {
10461 if(regs[j].regmap[hr]>=0) break;
10462 if(itype[j]==UJUMP||itype[j]==RJUMP||itype[j]==CJUMP||itype[j]==SJUMP||itype[j]==FJUMP) {
10463 if(branch_regs[j].regmap[hr]>=0) break;
10465 if(count_free_regs(regs[j].regmap)<=minimum_free_regs[j+1]) break;
10467 if(count_free_regs(branch_regs[j].regmap)<=minimum_free_regs[j+1]) break;
10470 else if(count_free_regs(regs[j].regmap)<=minimum_free_regs[j]) break;
10471 if(itype[j]==UJUMP||itype[j]==RJUMP||itype[j]==CJUMP||itype[j]==SJUMP||itype[j]==FJUMP) {
10472 int t=(ba[j]-start)>>2;
10473 if(t<j&&t>=earliest_available[hr]) {
10474 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
10475 // Score a point for hoisting loop invariant
10476 if(t<loop_start[hr]) loop_start[hr]=t;
10477 //printf("set loop_start: i=%x j=%x (%x)\n",start+i*4,start+j*4,start+t*4);
10483 if(regs[t].regmap[hr]==reg) {
10484 // Score a point if the branch target matches this register
10489 if(itype[j+1]==LOAD||itype[j+1]==LOADLR||
10490 itype[j+1]==STORE||itype[j+1]==STORELR||itype[j+1]==C1LS) {
10495 if(itype[j]==UJUMP||itype[j]==RJUMP||(source[j]>>16)==0x1000)
10497 // Stop on unconditional branch
10501 if(itype[j]==LOAD||itype[j]==LOADLR||
10502 itype[j]==STORE||itype[j]==STORELR||itype[j]==C1LS) {
10509 // Find highest score and allocate that register
10511 for(hr=0;hr<HOST_REGS;hr++) {
10512 if(hr!=EXCLUDE_REG) {
10513 if(score[hr]>score[maxscore]) {
10515 //printf("highest score: %d %d (%x->%x)\n",score[hr],hr,start+i*4,start+end[hr]*4);
10519 if(score[maxscore]>1)
10521 if(i<loop_start[maxscore]) loop_start[maxscore]=i;
10522 for(j=loop_start[maxscore];j<slen&&j<=end[maxscore];j++) {
10523 //if(regs[j].regmap[maxscore]>=0) {printf("oops: %x %x was %d=%d\n",loop_start[maxscore]*4+start,j*4+start,maxscore,regs[j].regmap[maxscore]);}
10524 assert(regs[j].regmap[maxscore]<0);
10525 if(j>loop_start[maxscore]) regs[j].regmap_entry[maxscore]=reg;
10526 regs[j].regmap[maxscore]=reg;
10527 regs[j].dirty&=~(1<<maxscore);
10528 regs[j].wasconst&=~(1<<maxscore);
10529 regs[j].isconst&=~(1<<maxscore);
10530 if(itype[j]==UJUMP||itype[j]==RJUMP||itype[j]==CJUMP||itype[j]==SJUMP||itype[j]==FJUMP) {
10531 branch_regs[j].regmap[maxscore]=reg;
10532 branch_regs[j].wasdirty&=~(1<<maxscore);
10533 branch_regs[j].dirty&=~(1<<maxscore);
10534 branch_regs[j].wasconst&=~(1<<maxscore);
10535 branch_regs[j].isconst&=~(1<<maxscore);
10536 if(itype[j]!=RJUMP&&itype[j]!=UJUMP&&(source[j]>>16)!=0x1000) {
10537 regmap_pre[j+2][maxscore]=reg;
10538 regs[j+2].wasdirty&=~(1<<maxscore);
10540 // loop optimization (loop_preload)
10541 int t=(ba[j]-start)>>2;
10542 if(t==loop_start[maxscore]) {
10543 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
10544 regs[t].regmap_entry[maxscore]=reg;
10549 if(j<1||(itype[j-1]!=RJUMP&&itype[j-1]!=UJUMP&&itype[j-1]!=CJUMP&&itype[j-1]!=SJUMP&&itype[j-1]!=FJUMP)) {
10550 regmap_pre[j+1][maxscore]=reg;
10551 regs[j+1].wasdirty&=~(1<<maxscore);
10556 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
10557 for(hr=0;hr<HOST_REGS;hr++) {
10558 score[hr]=0;earliest_available[hr]=i+i;
10559 loop_start[hr]=MAXBLOCK;
10567 // This allocates registers (if possible) one instruction prior
10568 // to use, which can avoid a load-use penalty on certain CPUs.
10569 for(i=0;i<slen-1;i++)
10571 if(!i||(itype[i-1]!=UJUMP&&itype[i-1]!=CJUMP&&itype[i-1]!=SJUMP&&itype[i-1]!=RJUMP&&itype[i-1]!=FJUMP))
10575 if(itype[i]==ALU||itype[i]==MOV||itype[i]==LOAD||itype[i]==SHIFTIMM||itype[i]==IMM16
10576 ||((itype[i]==COP1||itype[i]==COP2)&&opcode2[i]<3))
10579 if((hr=get_reg(regs[i+1].regmap,rs1[i+1]))>=0)
10581 if(regs[i].regmap[hr]<0&®s[i+1].regmap_entry[hr]<0)
10583 regs[i].regmap[hr]=regs[i+1].regmap[hr];
10584 regmap_pre[i+1][hr]=regs[i+1].regmap[hr];
10585 regs[i+1].regmap_entry[hr]=regs[i+1].regmap[hr];
10586 regs[i].isconst&=~(1<<hr);
10587 regs[i].isconst|=regs[i+1].isconst&(1<<hr);
10588 constmap[i][hr]=constmap[i+1][hr];
10589 regs[i+1].wasdirty&=~(1<<hr);
10590 regs[i].dirty&=~(1<<hr);
10595 if((hr=get_reg(regs[i+1].regmap,rs2[i+1]))>=0)
10597 if(regs[i].regmap[hr]<0&®s[i+1].regmap_entry[hr]<0)
10599 regs[i].regmap[hr]=regs[i+1].regmap[hr];
10600 regmap_pre[i+1][hr]=regs[i+1].regmap[hr];
10601 regs[i+1].regmap_entry[hr]=regs[i+1].regmap[hr];
10602 regs[i].isconst&=~(1<<hr);
10603 regs[i].isconst|=regs[i+1].isconst&(1<<hr);
10604 constmap[i][hr]=constmap[i+1][hr];
10605 regs[i+1].wasdirty&=~(1<<hr);
10606 regs[i].dirty&=~(1<<hr);
10610 // Preload target address for load instruction (non-constant)
10611 if(itype[i+1]==LOAD&&rs1[i+1]&&get_reg(regs[i+1].regmap,rs1[i+1])<0) {
10612 if((hr=get_reg(regs[i+1].regmap,rt1[i+1]))>=0)
10614 if(regs[i].regmap[hr]<0&®s[i+1].regmap_entry[hr]<0)
10616 regs[i].regmap[hr]=rs1[i+1];
10617 regmap_pre[i+1][hr]=rs1[i+1];
10618 regs[i+1].regmap_entry[hr]=rs1[i+1];
10619 regs[i].isconst&=~(1<<hr);
10620 regs[i].isconst|=regs[i+1].isconst&(1<<hr);
10621 constmap[i][hr]=constmap[i+1][hr];
10622 regs[i+1].wasdirty&=~(1<<hr);
10623 regs[i].dirty&=~(1<<hr);
10627 // Load source into target register
10628 if(lt1[i+1]&&get_reg(regs[i+1].regmap,rs1[i+1])<0) {
10629 if((hr=get_reg(regs[i+1].regmap,rt1[i+1]))>=0)
10631 if(regs[i].regmap[hr]<0&®s[i+1].regmap_entry[hr]<0)
10633 regs[i].regmap[hr]=rs1[i+1];
10634 regmap_pre[i+1][hr]=rs1[i+1];
10635 regs[i+1].regmap_entry[hr]=rs1[i+1];
10636 regs[i].isconst&=~(1<<hr);
10637 regs[i].isconst|=regs[i+1].isconst&(1<<hr);
10638 constmap[i][hr]=constmap[i+1][hr];
10639 regs[i+1].wasdirty&=~(1<<hr);
10640 regs[i].dirty&=~(1<<hr);
10644 // Preload map address
10645 #ifndef HOST_IMM_ADDR32
10646 if(itype[i+1]==LOAD||itype[i+1]==LOADLR||itype[i+1]==STORE||itype[i+1]==STORELR||itype[i+1]==C1LS||itype[i+1]==C2LS) {
10647 hr=get_reg(regs[i+1].regmap,TLREG);
10649 int sr=get_reg(regs[i+1].regmap,rs1[i+1]);
10650 if(sr>=0&&((regs[i+1].wasconst>>sr)&1)) {
10652 if(regs[i].regmap[hr]<0&®s[i+1].regmap_entry[hr]<0)
10654 regs[i].regmap[hr]=MGEN1+((i+1)&1);
10655 regmap_pre[i+1][hr]=MGEN1+((i+1)&1);
10656 regs[i+1].regmap_entry[hr]=MGEN1+((i+1)&1);
10657 regs[i].isconst&=~(1<<hr);
10658 regs[i].isconst|=regs[i+1].isconst&(1<<hr);
10659 constmap[i][hr]=constmap[i+1][hr];
10660 regs[i+1].wasdirty&=~(1<<hr);
10661 regs[i].dirty&=~(1<<hr);
10663 else if((nr=get_reg2(regs[i].regmap,regs[i+1].regmap,-1))>=0)
10665 // move it to another register
10666 regs[i+1].regmap[hr]=-1;
10667 regmap_pre[i+2][hr]=-1;
10668 regs[i+1].regmap[nr]=TLREG;
10669 regmap_pre[i+2][nr]=TLREG;
10670 regs[i].regmap[nr]=MGEN1+((i+1)&1);
10671 regmap_pre[i+1][nr]=MGEN1+((i+1)&1);
10672 regs[i+1].regmap_entry[nr]=MGEN1+((i+1)&1);
10673 regs[i].isconst&=~(1<<nr);
10674 regs[i+1].isconst&=~(1<<nr);
10675 regs[i].dirty&=~(1<<nr);
10676 regs[i+1].wasdirty&=~(1<<nr);
10677 regs[i+1].dirty&=~(1<<nr);
10678 regs[i+2].wasdirty&=~(1<<nr);
10684 // Address for store instruction (non-constant)
10685 if(itype[i+1]==STORE||itype[i+1]==STORELR
10686 ||(opcode[i+1]&0x3b)==0x39||(opcode[i+1]&0x3b)==0x3a) { // SB/SH/SW/SD/SWC1/SDC1/SWC2/SDC2
10687 if(get_reg(regs[i+1].regmap,rs1[i+1])<0) {
10688 hr=get_reg2(regs[i].regmap,regs[i+1].regmap,-1);
10689 if(hr<0) hr=get_reg(regs[i+1].regmap,-1);
10690 else {regs[i+1].regmap[hr]=AGEN1+((i+1)&1);regs[i+1].isconst&=~(1<<hr);}
10692 if(regs[i].regmap[hr]<0&®s[i+1].regmap_entry[hr]<0)
10694 regs[i].regmap[hr]=rs1[i+1];
10695 regmap_pre[i+1][hr]=rs1[i+1];
10696 regs[i+1].regmap_entry[hr]=rs1[i+1];
10697 regs[i].isconst&=~(1<<hr);
10698 regs[i].isconst|=regs[i+1].isconst&(1<<hr);
10699 constmap[i][hr]=constmap[i+1][hr];
10700 regs[i+1].wasdirty&=~(1<<hr);
10701 regs[i].dirty&=~(1<<hr);
10705 if(itype[i+1]==LOADLR||(opcode[i+1]&0x3b)==0x31||(opcode[i+1]&0x3b)==0x32) { // LWC1/LDC1, LWC2/LDC2
10706 if(get_reg(regs[i+1].regmap,rs1[i+1])<0) {
10708 hr=get_reg(regs[i+1].regmap,FTEMP);
10710 if(regs[i].regmap[hr]<0&®s[i+1].regmap_entry[hr]<0)
10712 regs[i].regmap[hr]=rs1[i+1];
10713 regmap_pre[i+1][hr]=rs1[i+1];
10714 regs[i+1].regmap_entry[hr]=rs1[i+1];
10715 regs[i].isconst&=~(1<<hr);
10716 regs[i].isconst|=regs[i+1].isconst&(1<<hr);
10717 constmap[i][hr]=constmap[i+1][hr];
10718 regs[i+1].wasdirty&=~(1<<hr);
10719 regs[i].dirty&=~(1<<hr);
10721 else if((nr=get_reg2(regs[i].regmap,regs[i+1].regmap,-1))>=0)
10723 // move it to another register
10724 regs[i+1].regmap[hr]=-1;
10725 regmap_pre[i+2][hr]=-1;
10726 regs[i+1].regmap[nr]=FTEMP;
10727 regmap_pre[i+2][nr]=FTEMP;
10728 regs[i].regmap[nr]=rs1[i+1];
10729 regmap_pre[i+1][nr]=rs1[i+1];
10730 regs[i+1].regmap_entry[nr]=rs1[i+1];
10731 regs[i].isconst&=~(1<<nr);
10732 regs[i+1].isconst&=~(1<<nr);
10733 regs[i].dirty&=~(1<<nr);
10734 regs[i+1].wasdirty&=~(1<<nr);
10735 regs[i+1].dirty&=~(1<<nr);
10736 regs[i+2].wasdirty&=~(1<<nr);
10740 if(itype[i+1]==LOAD||itype[i+1]==LOADLR||itype[i+1]==STORE||itype[i+1]==STORELR/*||itype[i+1]==C1LS||||itype[i+1]==C2LS*/) {
10741 if(itype[i+1]==LOAD)
10742 hr=get_reg(regs[i+1].regmap,rt1[i+1]);
10743 if(itype[i+1]==LOADLR||(opcode[i+1]&0x3b)==0x31||(opcode[i+1]&0x3b)==0x32) // LWC1/LDC1, LWC2/LDC2
10744 hr=get_reg(regs[i+1].regmap,FTEMP);
10745 if(itype[i+1]==STORE||itype[i+1]==STORELR||(opcode[i+1]&0x3b)==0x39||(opcode[i+1]&0x3b)==0x3a) { // SWC1/SDC1/SWC2/SDC2
10746 hr=get_reg(regs[i+1].regmap,AGEN1+((i+1)&1));
10747 if(hr<0) hr=get_reg(regs[i+1].regmap,-1);
10749 if(hr>=0&®s[i].regmap[hr]<0) {
10750 int rs=get_reg(regs[i+1].regmap,rs1[i+1]);
10751 if(rs>=0&&((regs[i+1].wasconst>>rs)&1)) {
10752 regs[i].regmap[hr]=AGEN1+((i+1)&1);
10753 regmap_pre[i+1][hr]=AGEN1+((i+1)&1);
10754 regs[i+1].regmap_entry[hr]=AGEN1+((i+1)&1);
10755 regs[i].isconst&=~(1<<hr);
10756 regs[i+1].wasdirty&=~(1<<hr);
10757 regs[i].dirty&=~(1<<hr);
10766 /* Pass 6 - Optimize clean/dirty state */
10767 clean_registers(0,slen-1,1);
10769 /* Pass 7 - Identify 32-bit registers */
10775 for (i=slen-1;i>=0;i--)
10778 if(itype[i]==RJUMP||itype[i]==UJUMP||itype[i]==CJUMP||itype[i]==SJUMP||itype[i]==FJUMP)
10780 if(ba[i]<start || ba[i]>=(start+slen*4))
10782 // Branch out of this block, don't need anything
10788 // Need whatever matches the target
10789 // (and doesn't get overwritten by the delay slot instruction)
10791 int t=(ba[i]-start)>>2;
10792 if(ba[i]>start+i*4) {
10794 if(!(requires_32bit[t]&~regs[i].was32))
10795 r32|=requires_32bit[t]&(~(1LL<<rt1[i+1]))&(~(1LL<<rt2[i+1]));
10798 //if(!(regs[t].was32&~unneeded_reg_upper[t]&~regs[i].was32))
10799 // r32|=regs[t].was32&~unneeded_reg_upper[t]&(~(1LL<<rt1[i+1]))&(~(1LL<<rt2[i+1]));
10800 if(!(pr32[t]&~regs[i].was32))
10801 r32|=pr32[t]&(~(1LL<<rt1[i+1]))&(~(1LL<<rt2[i+1]));
10804 // Conditional branch may need registers for following instructions
10805 if(itype[i]!=RJUMP&&itype[i]!=UJUMP&&(source[i]>>16)!=0x1000)
10808 r32|=requires_32bit[i+2];
10809 r32&=regs[i].was32;
10810 // Mark this address as a branch target since it may be called
10811 // upon return from interrupt
10815 // Merge in delay slot
10817 // These are overwritten unless the branch is "likely"
10818 // and the delay slot is nullified if not taken
10819 r32&=~(1LL<<rt1[i+1]);
10820 r32&=~(1LL<<rt2[i+1]);
10822 // Assume these are needed (delay slot)
10825 if((regs[i].was32>>us1[i+1])&1) r32|=1LL<<us1[i+1];
10829 if((regs[i].was32>>us2[i+1])&1) r32|=1LL<<us2[i+1];
10831 if(dep1[i+1]&&!((unneeded_reg_upper[i]>>dep1[i+1])&1))
10833 if((regs[i].was32>>dep1[i+1])&1) r32|=1LL<<dep1[i+1];
10835 if(dep2[i+1]&&!((unneeded_reg_upper[i]>>dep2[i+1])&1))
10837 if((regs[i].was32>>dep2[i+1])&1) r32|=1LL<<dep2[i+1];
10840 else if(itype[i]==SYSCALL||itype[i]==HLECALL||itype[i]==INTCALL)
10842 // SYSCALL instruction (software interrupt)
10845 else if(itype[i]==COP0 && (source[i]&0x3f)==0x18)
10847 // ERET instruction (return from interrupt)
10851 r32&=~(1LL<<rt1[i]);
10852 r32&=~(1LL<<rt2[i]);
10855 if((regs[i].was32>>us1[i])&1) r32|=1LL<<us1[i];
10859 if((regs[i].was32>>us2[i])&1) r32|=1LL<<us2[i];
10861 if(dep1[i]&&!((unneeded_reg_upper[i]>>dep1[i])&1))
10863 if((regs[i].was32>>dep1[i])&1) r32|=1LL<<dep1[i];
10865 if(dep2[i]&&!((unneeded_reg_upper[i]>>dep2[i])&1))
10867 if((regs[i].was32>>dep2[i])&1) r32|=1LL<<dep2[i];
10869 requires_32bit[i]=r32;
10871 // Dirty registers which are 32-bit, require 32-bit input
10872 // as they will be written as 32-bit values
10873 for(hr=0;hr<HOST_REGS;hr++)
10875 if(regs[i].regmap_entry[hr]>0&®s[i].regmap_entry[hr]<64) {
10876 if((regs[i].was32>>regs[i].regmap_entry[hr])&(regs[i].wasdirty>>hr)&1) {
10877 if(!((unneeded_reg_upper[i]>>regs[i].regmap_entry[hr])&1))
10878 requires_32bit[i]|=1LL<<regs[i].regmap_entry[hr];
10882 //requires_32bit[i]=is32[i]&~unneeded_reg_upper[i]; // DEBUG
10885 for (i=slen-1;i>=0;i--)
10887 if(itype[i]==CJUMP||itype[i]==SJUMP||itype[i]==FJUMP)
10889 // Conditional branch
10890 if((source[i]>>16)!=0x1000&&i<slen-2) {
10891 // Mark this address as a branch target since it may be called
10892 // upon return from interrupt
10899 if(itype[slen-1]==SPAN) {
10900 bt[slen-1]=1; // Mark as a branch target so instruction can restart after exception
10904 /* Debug/disassembly */
10905 for(i=0;i<slen;i++)
10909 for(r=1;r<=CCREG;r++) {
10910 if((unneeded_reg[i]>>r)&1) {
10911 if(r==HIREG) printf(" HI");
10912 else if(r==LOREG) printf(" LO");
10913 else printf(" r%d",r);
10918 for(r=1;r<=CCREG;r++) {
10919 if(((unneeded_reg_upper[i]&~unneeded_reg[i])>>r)&1) {
10920 if(r==HIREG) printf(" HI");
10921 else if(r==LOREG) printf(" LO");
10922 else printf(" r%d",r);
10926 for(r=0;r<=CCREG;r++) {
10927 //if(((is32[i]>>r)&(~unneeded_reg[i]>>r))&1) {
10928 if((regs[i].was32>>r)&1) {
10929 if(r==CCREG) printf(" CC");
10930 else if(r==HIREG) printf(" HI");
10931 else if(r==LOREG) printf(" LO");
10932 else printf(" r%d",r);
10937 #if defined(__i386__) || defined(__x86_64__)
10938 printf("pre: eax=%d ecx=%d edx=%d ebx=%d ebp=%d esi=%d edi=%d\n",regmap_pre[i][0],regmap_pre[i][1],regmap_pre[i][2],regmap_pre[i][3],regmap_pre[i][5],regmap_pre[i][6],regmap_pre[i][7]);
10941 printf("pre: r0=%d r1=%d r2=%d r3=%d r4=%d r5=%d r6=%d r7=%d r8=%d r9=%d r10=%d r12=%d\n",regmap_pre[i][0],regmap_pre[i][1],regmap_pre[i][2],regmap_pre[i][3],regmap_pre[i][4],regmap_pre[i][5],regmap_pre[i][6],regmap_pre[i][7],regmap_pre[i][8],regmap_pre[i][9],regmap_pre[i][10],regmap_pre[i][12]);
10944 if(needed_reg[i]&1) printf("eax ");
10945 if((needed_reg[i]>>1)&1) printf("ecx ");
10946 if((needed_reg[i]>>2)&1) printf("edx ");
10947 if((needed_reg[i]>>3)&1) printf("ebx ");
10948 if((needed_reg[i]>>5)&1) printf("ebp ");
10949 if((needed_reg[i]>>6)&1) printf("esi ");
10950 if((needed_reg[i]>>7)&1) printf("edi ");
10952 for(r=0;r<=CCREG;r++) {
10953 //if(((requires_32bit[i]>>r)&(~unneeded_reg[i]>>r))&1) {
10954 if((requires_32bit[i]>>r)&1) {
10955 if(r==CCREG) printf(" CC");
10956 else if(r==HIREG) printf(" HI");
10957 else if(r==LOREG) printf(" LO");
10958 else printf(" r%d",r);
10963 for(r=0;r<=CCREG;r++) {
10964 //if(((requires_32bit[i]>>r)&(~unneeded_reg[i]>>r))&1) {
10965 if((pr32[i]>>r)&1) {
10966 if(r==CCREG) printf(" CC");
10967 else if(r==HIREG) printf(" HI");
10968 else if(r==LOREG) printf(" LO");
10969 else printf(" r%d",r);
10972 if(pr32[i]!=requires_32bit[i]) printf(" OOPS");
10974 #if defined(__i386__) || defined(__x86_64__)
10975 printf("entry: eax=%d ecx=%d edx=%d ebx=%d ebp=%d esi=%d edi=%d\n",regs[i].regmap_entry[0],regs[i].regmap_entry[1],regs[i].regmap_entry[2],regs[i].regmap_entry[3],regs[i].regmap_entry[5],regs[i].regmap_entry[6],regs[i].regmap_entry[7]);
10977 if(regs[i].wasdirty&1) printf("eax ");
10978 if((regs[i].wasdirty>>1)&1) printf("ecx ");
10979 if((regs[i].wasdirty>>2)&1) printf("edx ");
10980 if((regs[i].wasdirty>>3)&1) printf("ebx ");
10981 if((regs[i].wasdirty>>5)&1) printf("ebp ");
10982 if((regs[i].wasdirty>>6)&1) printf("esi ");
10983 if((regs[i].wasdirty>>7)&1) printf("edi ");
10986 printf("entry: r0=%d r1=%d r2=%d r3=%d r4=%d r5=%d r6=%d r7=%d r8=%d r9=%d r10=%d r12=%d\n",regs[i].regmap_entry[0],regs[i].regmap_entry[1],regs[i].regmap_entry[2],regs[i].regmap_entry[3],regs[i].regmap_entry[4],regs[i].regmap_entry[5],regs[i].regmap_entry[6],regs[i].regmap_entry[7],regs[i].regmap_entry[8],regs[i].regmap_entry[9],regs[i].regmap_entry[10],regs[i].regmap_entry[12]);
10988 if(regs[i].wasdirty&1) printf("r0 ");
10989 if((regs[i].wasdirty>>1)&1) printf("r1 ");
10990 if((regs[i].wasdirty>>2)&1) printf("r2 ");
10991 if((regs[i].wasdirty>>3)&1) printf("r3 ");
10992 if((regs[i].wasdirty>>4)&1) printf("r4 ");
10993 if((regs[i].wasdirty>>5)&1) printf("r5 ");
10994 if((regs[i].wasdirty>>6)&1) printf("r6 ");
10995 if((regs[i].wasdirty>>7)&1) printf("r7 ");
10996 if((regs[i].wasdirty>>8)&1) printf("r8 ");
10997 if((regs[i].wasdirty>>9)&1) printf("r9 ");
10998 if((regs[i].wasdirty>>10)&1) printf("r10 ");
10999 if((regs[i].wasdirty>>12)&1) printf("r12 ");
11002 disassemble_inst(i);
11003 //printf ("ccadj[%d] = %d\n",i,ccadj[i]);
11004 #if defined(__i386__) || defined(__x86_64__)
11005 printf("eax=%d ecx=%d edx=%d ebx=%d ebp=%d esi=%d edi=%d dirty: ",regs[i].regmap[0],regs[i].regmap[1],regs[i].regmap[2],regs[i].regmap[3],regs[i].regmap[5],regs[i].regmap[6],regs[i].regmap[7]);
11006 if(regs[i].dirty&1) printf("eax ");
11007 if((regs[i].dirty>>1)&1) printf("ecx ");
11008 if((regs[i].dirty>>2)&1) printf("edx ");
11009 if((regs[i].dirty>>3)&1) printf("ebx ");
11010 if((regs[i].dirty>>5)&1) printf("ebp ");
11011 if((regs[i].dirty>>6)&1) printf("esi ");
11012 if((regs[i].dirty>>7)&1) printf("edi ");
11015 printf("r0=%d r1=%d r2=%d r3=%d r4=%d r5=%d r6=%d r7=%d r8=%d r9=%d r10=%d r12=%d dirty: ",regs[i].regmap[0],regs[i].regmap[1],regs[i].regmap[2],regs[i].regmap[3],regs[i].regmap[4],regs[i].regmap[5],regs[i].regmap[6],regs[i].regmap[7],regs[i].regmap[8],regs[i].regmap[9],regs[i].regmap[10],regs[i].regmap[12]);
11016 if(regs[i].dirty&1) printf("r0 ");
11017 if((regs[i].dirty>>1)&1) printf("r1 ");
11018 if((regs[i].dirty>>2)&1) printf("r2 ");
11019 if((regs[i].dirty>>3)&1) printf("r3 ");
11020 if((regs[i].dirty>>4)&1) printf("r4 ");
11021 if((regs[i].dirty>>5)&1) printf("r5 ");
11022 if((regs[i].dirty>>6)&1) printf("r6 ");
11023 if((regs[i].dirty>>7)&1) printf("r7 ");
11024 if((regs[i].dirty>>8)&1) printf("r8 ");
11025 if((regs[i].dirty>>9)&1) printf("r9 ");
11026 if((regs[i].dirty>>10)&1) printf("r10 ");
11027 if((regs[i].dirty>>12)&1) printf("r12 ");
11030 if(regs[i].isconst) {
11031 printf("constants: ");
11032 #if defined(__i386__) || defined(__x86_64__)
11033 if(regs[i].isconst&1) printf("eax=%x ",(int)constmap[i][0]);
11034 if((regs[i].isconst>>1)&1) printf("ecx=%x ",(int)constmap[i][1]);
11035 if((regs[i].isconst>>2)&1) printf("edx=%x ",(int)constmap[i][2]);
11036 if((regs[i].isconst>>3)&1) printf("ebx=%x ",(int)constmap[i][3]);
11037 if((regs[i].isconst>>5)&1) printf("ebp=%x ",(int)constmap[i][5]);
11038 if((regs[i].isconst>>6)&1) printf("esi=%x ",(int)constmap[i][6]);
11039 if((regs[i].isconst>>7)&1) printf("edi=%x ",(int)constmap[i][7]);
11042 if(regs[i].isconst&1) printf("r0=%x ",(int)constmap[i][0]);
11043 if((regs[i].isconst>>1)&1) printf("r1=%x ",(int)constmap[i][1]);
11044 if((regs[i].isconst>>2)&1) printf("r2=%x ",(int)constmap[i][2]);
11045 if((regs[i].isconst>>3)&1) printf("r3=%x ",(int)constmap[i][3]);
11046 if((regs[i].isconst>>4)&1) printf("r4=%x ",(int)constmap[i][4]);
11047 if((regs[i].isconst>>5)&1) printf("r5=%x ",(int)constmap[i][5]);
11048 if((regs[i].isconst>>6)&1) printf("r6=%x ",(int)constmap[i][6]);
11049 if((regs[i].isconst>>7)&1) printf("r7=%x ",(int)constmap[i][7]);
11050 if((regs[i].isconst>>8)&1) printf("r8=%x ",(int)constmap[i][8]);
11051 if((regs[i].isconst>>9)&1) printf("r9=%x ",(int)constmap[i][9]);
11052 if((regs[i].isconst>>10)&1) printf("r10=%x ",(int)constmap[i][10]);
11053 if((regs[i].isconst>>12)&1) printf("r12=%x ",(int)constmap[i][12]);
11059 for(r=0;r<=CCREG;r++) {
11060 if((regs[i].is32>>r)&1) {
11061 if(r==CCREG) printf(" CC");
11062 else if(r==HIREG) printf(" HI");
11063 else if(r==LOREG) printf(" LO");
11064 else printf(" r%d",r);
11070 for(r=0;r<=CCREG;r++) {
11071 if((p32[i]>>r)&1) {
11072 if(r==CCREG) printf(" CC");
11073 else if(r==HIREG) printf(" HI");
11074 else if(r==LOREG) printf(" LO");
11075 else printf(" r%d",r);
11078 if(p32[i]!=regs[i].is32) printf(" NO MATCH\n");
11079 else printf("\n");*/
11080 if(itype[i]==RJUMP||itype[i]==UJUMP||itype[i]==CJUMP||itype[i]==SJUMP||itype[i]==FJUMP) {
11081 #if defined(__i386__) || defined(__x86_64__)
11082 printf("branch(%d): eax=%d ecx=%d edx=%d ebx=%d ebp=%d esi=%d edi=%d dirty: ",i,branch_regs[i].regmap[0],branch_regs[i].regmap[1],branch_regs[i].regmap[2],branch_regs[i].regmap[3],branch_regs[i].regmap[5],branch_regs[i].regmap[6],branch_regs[i].regmap[7]);
11083 if(branch_regs[i].dirty&1) printf("eax ");
11084 if((branch_regs[i].dirty>>1)&1) printf("ecx ");
11085 if((branch_regs[i].dirty>>2)&1) printf("edx ");
11086 if((branch_regs[i].dirty>>3)&1) printf("ebx ");
11087 if((branch_regs[i].dirty>>5)&1) printf("ebp ");
11088 if((branch_regs[i].dirty>>6)&1) printf("esi ");
11089 if((branch_regs[i].dirty>>7)&1) printf("edi ");
11092 printf("branch(%d): r0=%d r1=%d r2=%d r3=%d r4=%d r5=%d r6=%d r7=%d r8=%d r9=%d r10=%d r12=%d dirty: ",i,branch_regs[i].regmap[0],branch_regs[i].regmap[1],branch_regs[i].regmap[2],branch_regs[i].regmap[3],branch_regs[i].regmap[4],branch_regs[i].regmap[5],branch_regs[i].regmap[6],branch_regs[i].regmap[7],branch_regs[i].regmap[8],branch_regs[i].regmap[9],branch_regs[i].regmap[10],branch_regs[i].regmap[12]);
11093 if(branch_regs[i].dirty&1) printf("r0 ");
11094 if((branch_regs[i].dirty>>1)&1) printf("r1 ");
11095 if((branch_regs[i].dirty>>2)&1) printf("r2 ");
11096 if((branch_regs[i].dirty>>3)&1) printf("r3 ");
11097 if((branch_regs[i].dirty>>4)&1) printf("r4 ");
11098 if((branch_regs[i].dirty>>5)&1) printf("r5 ");
11099 if((branch_regs[i].dirty>>6)&1) printf("r6 ");
11100 if((branch_regs[i].dirty>>7)&1) printf("r7 ");
11101 if((branch_regs[i].dirty>>8)&1) printf("r8 ");
11102 if((branch_regs[i].dirty>>9)&1) printf("r9 ");
11103 if((branch_regs[i].dirty>>10)&1) printf("r10 ");
11104 if((branch_regs[i].dirty>>12)&1) printf("r12 ");
11108 for(r=0;r<=CCREG;r++) {
11109 if((branch_regs[i].is32>>r)&1) {
11110 if(r==CCREG) printf(" CC");
11111 else if(r==HIREG) printf(" HI");
11112 else if(r==LOREG) printf(" LO");
11113 else printf(" r%d",r);
11122 /* Pass 8 - Assembly */
11123 linkcount=0;stubcount=0;
11124 ds=0;is_delayslot=0;
11126 uint64_t is32_pre=0;
11128 u_int beginning=(u_int)out;
11129 if((u_int)addr&1) {
11133 u_int instr_addr0_override=0;
11136 if (start == 0x80030000) {
11137 // nasty hack for fastbios thing
11138 // override block entry to this code
11139 instr_addr0_override=(u_int)out;
11140 emit_movimm(start,0);
11141 // abuse io address var as a flag that we
11142 // have already returned here once
11143 emit_readword((int)&address,1);
11144 emit_writeword(0,(int)&pcaddr);
11145 emit_writeword(0,(int)&address);
11147 emit_jne((int)new_dyna_leave);
11150 for(i=0;i<slen;i++)
11152 //if(ds) printf("ds: ");
11153 disassemble_inst(i);
11155 ds=0; // Skip delay slot
11156 if(bt[i]) assem_debug("OOPS - branch into delay slot\n");
11159 speculate_register_values(i);
11160 #ifndef DESTRUCTIVE_WRITEBACK
11161 if(i<2||(itype[i-2]!=UJUMP&&itype[i-2]!=RJUMP&&(source[i-2]>>16)!=0x1000))
11163 wb_sx(regmap_pre[i],regs[i].regmap_entry,regs[i].wasdirty,is32_pre,regs[i].was32,
11164 unneeded_reg[i],unneeded_reg_upper[i]);
11165 wb_valid(regmap_pre[i],regs[i].regmap_entry,dirty_pre,regs[i].wasdirty,is32_pre,
11166 unneeded_reg[i],unneeded_reg_upper[i]);
11168 if((itype[i]==CJUMP||itype[i]==SJUMP||itype[i]==FJUMP)&&!likely[i]) {
11169 is32_pre=branch_regs[i].is32;
11170 dirty_pre=branch_regs[i].dirty;
11172 is32_pre=regs[i].is32;
11173 dirty_pre=regs[i].dirty;
11177 if(i<2||(itype[i-2]!=UJUMP&&itype[i-2]!=RJUMP&&(source[i-2]>>16)!=0x1000))
11179 wb_invalidate(regmap_pre[i],regs[i].regmap_entry,regs[i].wasdirty,regs[i].was32,
11180 unneeded_reg[i],unneeded_reg_upper[i]);
11181 loop_preload(regmap_pre[i],regs[i].regmap_entry);
11183 // branch target entry point
11184 instr_addr[i]=(u_int)out;
11185 assem_debug("<->\n");
11187 if(regs[i].regmap_entry[HOST_CCREG]==CCREG&®s[i].regmap[HOST_CCREG]!=CCREG)
11188 wb_register(CCREG,regs[i].regmap_entry,regs[i].wasdirty,regs[i].was32);
11189 load_regs(regs[i].regmap_entry,regs[i].regmap,regs[i].was32,rs1[i],rs2[i]);
11190 address_generation(i,®s[i],regs[i].regmap_entry);
11191 load_consts(regmap_pre[i],regs[i].regmap,regs[i].was32,i);
11192 if(itype[i]==RJUMP||itype[i]==UJUMP||itype[i]==CJUMP||itype[i]==SJUMP||itype[i]==FJUMP)
11194 // Load the delay slot registers if necessary
11195 if(rs1[i+1]!=rs1[i]&&rs1[i+1]!=rs2[i]&&(rs1[i+1]!=rt1[i]||rt1[i]==0))
11196 load_regs(regs[i].regmap_entry,regs[i].regmap,regs[i].was32,rs1[i+1],rs1[i+1]);
11197 if(rs2[i+1]!=rs1[i+1]&&rs2[i+1]!=rs1[i]&&rs2[i+1]!=rs2[i]&&(rs2[i+1]!=rt1[i]||rt1[i]==0))
11198 load_regs(regs[i].regmap_entry,regs[i].regmap,regs[i].was32,rs2[i+1],rs2[i+1]);
11199 if(itype[i+1]==STORE||itype[i+1]==STORELR||(opcode[i+1]&0x3b)==0x39||(opcode[i+1]&0x3b)==0x3a)
11200 load_regs(regs[i].regmap_entry,regs[i].regmap,regs[i].was32,INVCP,INVCP);
11204 // Preload registers for following instruction
11205 if(rs1[i+1]!=rs1[i]&&rs1[i+1]!=rs2[i])
11206 if(rs1[i+1]!=rt1[i]&&rs1[i+1]!=rt2[i])
11207 load_regs(regs[i].regmap_entry,regs[i].regmap,regs[i].was32,rs1[i+1],rs1[i+1]);
11208 if(rs2[i+1]!=rs1[i+1]&&rs2[i+1]!=rs1[i]&&rs2[i+1]!=rs2[i])
11209 if(rs2[i+1]!=rt1[i]&&rs2[i+1]!=rt2[i])
11210 load_regs(regs[i].regmap_entry,regs[i].regmap,regs[i].was32,rs2[i+1],rs2[i+1]);
11212 // TODO: if(is_ooo(i)) address_generation(i+1);
11213 if(itype[i]==CJUMP||itype[i]==FJUMP)
11214 load_regs(regs[i].regmap_entry,regs[i].regmap,regs[i].was32,CCREG,CCREG);
11215 if(itype[i]==STORE||itype[i]==STORELR||(opcode[i]&0x3b)==0x39||(opcode[i]&0x3b)==0x3a)
11216 load_regs(regs[i].regmap_entry,regs[i].regmap,regs[i].was32,INVCP,INVCP);
11217 if(bt[i]) cop1_usable=0;
11221 alu_assemble(i,®s[i]);break;
11223 imm16_assemble(i,®s[i]);break;
11225 shift_assemble(i,®s[i]);break;
11227 shiftimm_assemble(i,®s[i]);break;
11229 load_assemble(i,®s[i]);break;
11231 loadlr_assemble(i,®s[i]);break;
11233 store_assemble(i,®s[i]);break;
11235 storelr_assemble(i,®s[i]);break;
11237 cop0_assemble(i,®s[i]);break;
11239 cop1_assemble(i,®s[i]);break;
11241 c1ls_assemble(i,®s[i]);break;
11243 cop2_assemble(i,®s[i]);break;
11245 c2ls_assemble(i,®s[i]);break;
11247 c2op_assemble(i,®s[i]);break;
11249 fconv_assemble(i,®s[i]);break;
11251 float_assemble(i,®s[i]);break;
11253 fcomp_assemble(i,®s[i]);break;
11255 multdiv_assemble(i,®s[i]);break;
11257 mov_assemble(i,®s[i]);break;
11259 syscall_assemble(i,®s[i]);break;
11261 hlecall_assemble(i,®s[i]);break;
11263 intcall_assemble(i,®s[i]);break;
11265 ujump_assemble(i,®s[i]);ds=1;break;
11267 rjump_assemble(i,®s[i]);ds=1;break;
11269 cjump_assemble(i,®s[i]);ds=1;break;
11271 sjump_assemble(i,®s[i]);ds=1;break;
11273 fjump_assemble(i,®s[i]);ds=1;break;
11275 pagespan_assemble(i,®s[i]);break;
11277 if(itype[i]==UJUMP||itype[i]==RJUMP||(source[i]>>16)==0x1000)
11278 literal_pool(1024);
11280 literal_pool_jumpover(256);
11283 //assert(itype[i-2]==UJUMP||itype[i-2]==RJUMP||(source[i-2]>>16)==0x1000);
11284 // If the block did not end with an unconditional branch,
11285 // add a jump to the next instruction.
11287 if(itype[i-2]!=UJUMP&&itype[i-2]!=RJUMP&&(source[i-2]>>16)!=0x1000&&itype[i-1]!=SPAN) {
11288 assert(itype[i-1]!=UJUMP&&itype[i-1]!=CJUMP&&itype[i-1]!=SJUMP&&itype[i-1]!=RJUMP&&itype[i-1]!=FJUMP);
11290 if(itype[i-2]!=CJUMP&&itype[i-2]!=SJUMP&&itype[i-2]!=FJUMP) {
11291 store_regs_bt(regs[i-1].regmap,regs[i-1].is32,regs[i-1].dirty,start+i*4);
11292 if(regs[i-1].regmap[HOST_CCREG]!=CCREG)
11293 emit_loadreg(CCREG,HOST_CCREG);
11294 emit_addimm(HOST_CCREG,CLOCK_DIVIDER*(ccadj[i-1]+1),HOST_CCREG);
11296 else if(!likely[i-2])
11298 store_regs_bt(branch_regs[i-2].regmap,branch_regs[i-2].is32,branch_regs[i-2].dirty,start+i*4);
11299 assert(branch_regs[i-2].regmap[HOST_CCREG]==CCREG);
11303 store_regs_bt(regs[i-2].regmap,regs[i-2].is32,regs[i-2].dirty,start+i*4);
11304 assert(regs[i-2].regmap[HOST_CCREG]==CCREG);
11306 add_to_linker((int)out,start+i*4,0);
11313 assert(itype[i-1]!=UJUMP&&itype[i-1]!=CJUMP&&itype[i-1]!=SJUMP&&itype[i-1]!=RJUMP&&itype[i-1]!=FJUMP);
11314 store_regs_bt(regs[i-1].regmap,regs[i-1].is32,regs[i-1].dirty,start+i*4);
11315 if(regs[i-1].regmap[HOST_CCREG]!=CCREG)
11316 emit_loadreg(CCREG,HOST_CCREG);
11317 emit_addimm(HOST_CCREG,CLOCK_DIVIDER*(ccadj[i-1]+1),HOST_CCREG);
11318 add_to_linker((int)out,start+i*4,0);
11322 // TODO: delay slot stubs?
11324 for(i=0;i<stubcount;i++)
11326 switch(stubs[i][0])
11334 do_readstub(i);break;
11339 do_writestub(i);break;
11341 do_ccstub(i);break;
11343 do_invstub(i);break;
11345 do_cop1stub(i);break;
11347 do_unalignedwritestub(i);break;
11351 if (instr_addr0_override)
11352 instr_addr[0] = instr_addr0_override;
11354 /* Pass 9 - Linker */
11355 for(i=0;i<linkcount;i++)
11357 assem_debug("%8x -> %8x\n",link_addr[i][0],link_addr[i][1]);
11359 if(!link_addr[i][2])
11362 void *addr=check_addr(link_addr[i][1]);
11363 emit_extjump(link_addr[i][0],link_addr[i][1]);
11365 set_jump_target(link_addr[i][0],(int)addr);
11366 add_link(link_addr[i][1],stub);
11368 else set_jump_target(link_addr[i][0],(int)stub);
11373 int target=(link_addr[i][1]-start)>>2;
11374 assert(target>=0&&target<slen);
11375 assert(instr_addr[target]);
11376 //#ifdef CORTEX_A8_BRANCH_PREDICTION_HACK
11377 //set_jump_target_fillslot(link_addr[i][0],instr_addr[target],link_addr[i][2]>>1);
11379 set_jump_target(link_addr[i][0],instr_addr[target]);
11383 // External Branch Targets (jump_in)
11384 if(copy+slen*4>(void *)shadow+sizeof(shadow)) copy=shadow;
11385 for(i=0;i<slen;i++)
11389 if(instr_addr[i]) // TODO - delay slots (=null)
11391 u_int vaddr=start+i*4;
11392 u_int page=get_page(vaddr);
11393 u_int vpage=get_vpage(vaddr);
11395 //if(!(is32[i]&(~unneeded_reg_upper[i])&~(1LL<<CCREG)))
11397 if(!requires_32bit[i])
11402 assem_debug("%8x (%d) <- %8x\n",instr_addr[i],i,start+i*4);
11403 assem_debug("jump_in: %x\n",start+i*4);
11404 ll_add(jump_dirty+vpage,vaddr,(void *)out);
11405 int entry_point=do_dirty_stub(i);
11406 ll_add(jump_in+page,vaddr,(void *)entry_point);
11407 // If there was an existing entry in the hash table,
11408 // replace it with the new address.
11409 // Don't add new entries. We'll insert the
11410 // ones that actually get used in check_addr().
11411 int *ht_bin=hash_table[((vaddr>>16)^vaddr)&0xFFFF];
11412 if(ht_bin[0]==vaddr) {
11413 ht_bin[1]=entry_point;
11415 if(ht_bin[2]==vaddr) {
11416 ht_bin[3]=entry_point;
11421 u_int r=requires_32bit[i]|!!(requires_32bit[i]>>32);
11422 assem_debug("%8x (%d) <- %8x\n",instr_addr[i],i,start+i*4);
11423 assem_debug("jump_in: %x (restricted - %x)\n",start+i*4,r);
11424 //int entry_point=(int)out;
11425 ////assem_debug("entry_point: %x\n",entry_point);
11426 //load_regs_entry(i);
11427 //if(entry_point==(int)out)
11428 // entry_point=instr_addr[i];
11430 // emit_jmp(instr_addr[i]);
11431 //ll_add_32(jump_in+page,vaddr,r,(void *)entry_point);
11432 ll_add_32(jump_dirty+vpage,vaddr,r,(void *)out);
11433 int entry_point=do_dirty_stub(i);
11434 ll_add_32(jump_in+page,vaddr,r,(void *)entry_point);
11439 // Write out the literal pool if necessary
11441 #ifdef CORTEX_A8_BRANCH_PREDICTION_HACK
11443 if(((u_int)out)&7) emit_addnop(13);
11445 assert((u_int)out-beginning<MAX_OUTPUT_BLOCK_SIZE);
11446 //printf("shadow buffer: %x-%x\n",(int)copy,(int)copy+slen*4);
11447 memcpy(copy,source,slen*4);
11451 __clear_cache((void *)beginning,out);
11454 // If we're within 256K of the end of the buffer,
11455 // start over from the beginning. (Is 256K enough?)
11456 if((int)out>BASE_ADDR+(1<<TARGET_SIZE_2)-MAX_OUTPUT_BLOCK_SIZE) out=(u_char *)BASE_ADDR;
11458 // Trap writes to any of the pages we compiled
11459 for(i=start>>12;i<=(start+slen*4)>>12;i++) {
11461 #ifndef DISABLE_TLB
11462 memory_map[i]|=0x40000000;
11463 if((signed int)start>=(signed int)0xC0000000) {
11465 j=(((u_int)i<<12)+(memory_map[i]<<2)-(u_int)rdram+(u_int)0x80000000)>>12;
11467 memory_map[j]|=0x40000000;
11468 //printf("write protect physical page: %x (virtual %x)\n",j<<12,start);
11472 inv_code_start=inv_code_end=~0;
11474 // for PCSX we need to mark all mirrors too
11475 if(get_page(start)<(RAM_SIZE>>12))
11476 for(i=start>>12;i<=(start+slen*4)>>12;i++)
11477 invalid_code[((u_int)0x00000000>>12)|(i&0x1ff)]=
11478 invalid_code[((u_int)0x80000000>>12)|(i&0x1ff)]=
11479 invalid_code[((u_int)0xa0000000>>12)|(i&0x1ff)]=0;
11482 /* Pass 10 - Free memory by expiring oldest blocks */
11484 int end=((((int)out-BASE_ADDR)>>(TARGET_SIZE_2-16))+16384)&65535;
11485 while(expirep!=end)
11487 int shift=TARGET_SIZE_2-3; // Divide into 8 blocks
11488 int base=BASE_ADDR+((expirep>>13)<<shift); // Base address of this block
11489 inv_debug("EXP: Phase %d\n",expirep);
11490 switch((expirep>>11)&3)
11493 // Clear jump_in and jump_dirty
11494 ll_remove_matching_addrs(jump_in+(expirep&2047),base,shift);
11495 ll_remove_matching_addrs(jump_dirty+(expirep&2047),base,shift);
11496 ll_remove_matching_addrs(jump_in+2048+(expirep&2047),base,shift);
11497 ll_remove_matching_addrs(jump_dirty+2048+(expirep&2047),base,shift);
11501 ll_kill_pointers(jump_out[expirep&2047],base,shift);
11502 ll_kill_pointers(jump_out[(expirep&2047)+2048],base,shift);
11505 // Clear hash table
11506 for(i=0;i<32;i++) {
11507 int *ht_bin=hash_table[((expirep&2047)<<5)+i];
11508 if((ht_bin[3]>>shift)==(base>>shift) ||
11509 ((ht_bin[3]-MAX_OUTPUT_BLOCK_SIZE)>>shift)==(base>>shift)) {
11510 inv_debug("EXP: Remove hash %x -> %x\n",ht_bin[2],ht_bin[3]);
11511 ht_bin[2]=ht_bin[3]=-1;
11513 if((ht_bin[1]>>shift)==(base>>shift) ||
11514 ((ht_bin[1]-MAX_OUTPUT_BLOCK_SIZE)>>shift)==(base>>shift)) {
11515 inv_debug("EXP: Remove hash %x -> %x\n",ht_bin[0],ht_bin[1]);
11516 ht_bin[0]=ht_bin[2];
11517 ht_bin[1]=ht_bin[3];
11518 ht_bin[2]=ht_bin[3]=-1;
11525 if((expirep&2047)==0)
11528 ll_remove_matching_addrs(jump_out+(expirep&2047),base,shift);
11529 ll_remove_matching_addrs(jump_out+2048+(expirep&2047),base,shift);
11532 expirep=(expirep+1)&65535;
11537 // vim:shiftwidth=2:expandtab