drc: avoid excessive recursion in hle mode
[pcsx_rearmed.git] / libpcsxcore / new_dynarec / new_dynarec.c
1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2  *   Mupen64plus - new_dynarec.c                                           *
3  *   Copyright (C) 2009-2011 Ari64                                         *
4  *                                                                         *
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.                                   *
9  *                                                                         *
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.                          *
14  *                                                                         *
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  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
20
21 #include <stdlib.h>
22 #include <stdint.h> //include for uint64_t
23 #include <assert.h>
24 #include <errno.h>
25 #include <sys/mman.h>
26 #ifdef __MACH__
27 #include <libkern/OSCacheControl.h>
28 #endif
29 #ifdef _3DS
30 #include <3ds_utils.h>
31 #endif
32 #ifdef HAVE_LIBNX
33 #include <switch.h>
34 static Jit g_jit;
35 #endif
36
37 #include "new_dynarec_config.h"
38 #include "../psxhle.h"
39 #include "../psxinterpreter.h"
40 #include "../psxcounters.h"
41 #include "../gte.h"
42 #include "emu_if.h" // emulator interface
43 #include "linkage_offsets.h"
44 #include "compiler_features.h"
45 #include "arm_features.h"
46
47 #ifndef ARRAY_SIZE
48 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
49 #endif
50 #ifndef min
51 #define min(a, b) ((b) < (a) ? (b) : (a))
52 #endif
53 #ifndef max
54 #define max(a, b) ((b) > (a) ? (b) : (a))
55 #endif
56
57 //#define DISASM
58 //#define ASSEM_PRINT
59 //#define REGMAP_PRINT // with DISASM only
60 //#define INV_DEBUG_W
61 //#define STAT_PRINT
62
63 #ifdef ASSEM_PRINT
64 #define assem_debug printf
65 #else
66 #define assem_debug(...)
67 #endif
68 //#define inv_debug printf
69 #define inv_debug(...)
70
71 #ifdef __i386__
72 #include "assem_x86.h"
73 #endif
74 #ifdef __x86_64__
75 #include "assem_x64.h"
76 #endif
77 #ifdef __arm__
78 #include "assem_arm.h"
79 #endif
80 #ifdef __aarch64__
81 #include "assem_arm64.h"
82 #endif
83
84 #define RAM_SIZE 0x200000
85 #define MAXBLOCK 2048
86 #define MAX_OUTPUT_BLOCK_SIZE 262144
87 #define EXPIRITY_OFFSET (MAX_OUTPUT_BLOCK_SIZE * 2)
88 #define PAGE_COUNT 1024
89
90 #if defined(HAVE_CONDITIONAL_CALL) && !defined(DESTRUCTIVE_SHIFT)
91 #define INVALIDATE_USE_COND_CALL
92 #endif
93
94 #ifdef VITA
95 // apparently Vita has a 16MB limit, so either we cut tc in half,
96 // or use this hack (it's a hack because tc size was designed to be power-of-2)
97 #define TC_REDUCE_BYTES 4096
98 #else
99 #define TC_REDUCE_BYTES 0
100 #endif
101
102 struct ndrc_tramp
103 {
104   struct tramp_insns ops[2048 / sizeof(struct tramp_insns)];
105   const void *f[2048 / sizeof(void *)];
106 };
107
108 struct ndrc_mem
109 {
110   u_char translation_cache[(1 << TARGET_SIZE_2) - TC_REDUCE_BYTES];
111   struct ndrc_tramp tramp;
112 };
113
114 #ifdef BASE_ADDR_DYNAMIC
115 static struct ndrc_mem *ndrc;
116 #else
117 static struct ndrc_mem ndrc_ __attribute__((aligned(4096)));
118 static struct ndrc_mem *ndrc = &ndrc_;
119 #endif
120 #ifdef TC_WRITE_OFFSET
121 # ifdef __GLIBC__
122 # include <sys/types.h>
123 # include <sys/stat.h>
124 # include <fcntl.h>
125 # include <unistd.h>
126 # endif
127 static long ndrc_write_ofs;
128 #define NDRC_WRITE_OFFSET(x) (void *)((char *)(x) + ndrc_write_ofs)
129 #else
130 #define NDRC_WRITE_OFFSET(x) (x)
131 #endif
132
133 // stubs
134 enum stub_type {
135   CC_STUB = 1,
136   //FP_STUB = 2,
137   LOADB_STUB = 3,
138   LOADH_STUB = 4,
139   LOADW_STUB = 5,
140   //LOADD_STUB = 6,
141   LOADBU_STUB = 7,
142   LOADHU_STUB = 8,
143   STOREB_STUB = 9,
144   STOREH_STUB = 10,
145   STOREW_STUB = 11,
146   //STORED_STUB = 12,
147   STORELR_STUB = 13,
148   INVCODE_STUB = 14,
149   OVERFLOW_STUB = 15,
150   ALIGNMENT_STUB = 16,
151 };
152
153 // regmap_pre[i]    - regs before [i] insn starts; dirty things here that
154 //                    don't match .regmap will be written back
155 // [i].regmap_entry - regs that must be set up if someone jumps here
156 // [i].regmap       - regs [i] insn will read/(over)write
157 // branch_regs[i].* - same as above but for branches, takes delay slot into account
158 struct regstat
159 {
160   signed char regmap_entry[HOST_REGS];
161   signed char regmap[HOST_REGS];
162   u_int wasdirty;
163   u_int dirty;
164   u_int wasconst;                // before; for example 'lw r2, (r2)' wasconst is true
165   u_int isconst;                 //  ... but isconst is false when r2 is known (hr)
166   u_int loadedconst;             // host regs that have constants loaded
167   u_int noevict;                 // can't evict this hr (alloced by current op)
168   //u_int waswritten;              // MIPS regs that were used as store base before
169   uint64_t u;
170 };
171
172 struct ht_entry
173 {
174   u_int vaddr[2];
175   void *tcaddr[2];
176 };
177
178 struct code_stub
179 {
180   enum stub_type type;
181   void *addr;
182   void *retaddr;
183   u_int a;
184   uintptr_t b;
185   uintptr_t c;
186   u_int d;
187   u_int e;
188 };
189
190 struct link_entry
191 {
192   void *addr;
193   u_int target;
194   u_int internal;
195 };
196
197 struct block_info
198 {
199   struct block_info *next;
200   const void *source;
201   const void *copy;
202   u_int start; // vaddr of the block start
203   u_int len;   // of the whole block source
204   u_int tc_offs;
205   //u_int tc_len;
206   u_int reg_sv_flags;
207   u_char is_dirty;
208   u_char inv_near_misses;
209   u_short jump_in_cnt;
210   struct {
211     u_int vaddr;
212     void *addr;
213   } jump_in[0];
214 };
215
216 struct jump_info
217 {
218   int alloc;
219   int count;
220   struct {
221     u_int target_vaddr;
222     void *stub;
223   } e[0];
224 };
225
226 static struct decoded_insn
227 {
228   u_char itype;
229   u_char opcode;   // bits 31-26
230   u_char opcode2;  // (depends on opcode)
231   u_char rs1;
232   u_char rs2;
233   u_char rt1;
234   u_char rt2;
235   u_char use_lt1:1;
236   u_char bt:1;
237   u_char ooo:1;
238   u_char is_ds:1;
239   u_char is_jump:1;
240   u_char is_ujump:1;
241   u_char is_load:1;
242   u_char is_store:1;
243   u_char is_delay_load:1; // is_load + MFC/CFC
244   u_char is_exception:1;  // unconditional, also interp. fallback
245   u_char may_except:1;    // might generate an exception
246   u_char ls_type:2;       // load/store type (ls_width_type)
247 } dops[MAXBLOCK];
248
249 enum ls_width_type {
250   LS_8 = 0, LS_16, LS_32, LS_LR
251 };
252
253 static struct compile_info
254 {
255   int imm;
256   u_int ba;
257   int ccadj;
258   signed char min_free_regs;
259   signed char addr;
260   signed char reserved[2];
261 } cinfo[MAXBLOCK];
262
263   static u_char *out;
264   static char invalid_code[0x100000];
265   static struct ht_entry hash_table[65536];
266   static struct block_info *blocks[PAGE_COUNT];
267   static struct jump_info *jumps[PAGE_COUNT];
268   static u_int start;
269   static u_int *source;
270   static uint64_t gte_rs[MAXBLOCK]; // gte: 32 data and 32 ctl regs
271   static uint64_t gte_rt[MAXBLOCK];
272   static uint64_t gte_unneeded[MAXBLOCK];
273   static u_int smrv[32]; // speculated MIPS register values
274   static u_int smrv_strong; // mask or regs that are likely to have correct values
275   static u_int smrv_weak; // same, but somewhat less likely
276   static u_int smrv_strong_next; // same, but after current insn executes
277   static u_int smrv_weak_next;
278   static uint64_t unneeded_reg[MAXBLOCK];
279   static uint64_t branch_unneeded_reg[MAXBLOCK];
280   // see 'struct regstat' for a description
281   static signed char regmap_pre[MAXBLOCK][HOST_REGS];
282   // contains 'real' consts at [i] insn, but may differ from what's actually
283   // loaded in host reg as 'final' value is always loaded, see get_final_value()
284   static uint32_t current_constmap[HOST_REGS];
285   static uint32_t constmap[MAXBLOCK][HOST_REGS];
286   static struct regstat regs[MAXBLOCK];
287   static struct regstat branch_regs[MAXBLOCK];
288   static int slen;
289   static void *instr_addr[MAXBLOCK];
290   static struct link_entry link_addr[MAXBLOCK];
291   static int linkcount;
292   static struct code_stub stubs[MAXBLOCK*3];
293   static int stubcount;
294   static u_int literals[1024][2];
295   static int literalcount;
296   static int is_delayslot;
297   static char shadow[1048576]  __attribute__((aligned(16)));
298   static void *copy;
299   static u_int expirep;
300   static u_int stop_after_jal;
301   static u_int f1_hack;
302 #ifdef STAT_PRINT
303   static int stat_bc_direct;
304   static int stat_bc_pre;
305   static int stat_bc_restore;
306   static int stat_ht_lookups;
307   static int stat_jump_in_lookups;
308   static int stat_restore_tries;
309   static int stat_restore_compares;
310   static int stat_inv_addr_calls;
311   static int stat_inv_hits;
312   static int stat_blocks;
313   static int stat_links;
314   #define stat_inc(s) s++
315   #define stat_dec(s) s--
316   #define stat_clear(s) s = 0
317 #else
318   #define stat_inc(s)
319   #define stat_dec(s)
320   #define stat_clear(s)
321 #endif
322
323   int new_dynarec_hacks;
324   int new_dynarec_hacks_pergame;
325   int new_dynarec_hacks_old;
326   int new_dynarec_did_compile;
327
328   #define HACK_ENABLED(x) ((new_dynarec_hacks | new_dynarec_hacks_pergame) & (x))
329
330   extern int cycle_count; // ... until end of the timeslice, counts -N -> 0 (CCREG)
331   extern int last_count;  // last absolute target, often = next_interupt
332   extern int pcaddr;
333   extern int pending_exception;
334   extern int branch_target;
335   extern uintptr_t ram_offset;
336   extern uintptr_t mini_ht[32][2];
337
338   /* registers that may be allocated */
339   /* 1-31 gpr */
340 #define LOREG 32 // lo
341 #define HIREG 33 // hi
342 //#define FSREG 34 // FPU status (FCSR)
343 //#define CSREG 35 // Coprocessor status
344 #define CCREG 36 // Cycle count
345 #define INVCP 37 // Pointer to invalid_code
346 //#define MMREG 38 // Pointer to memory_map
347 #define ROREG 39 // ram offset (if psxM != 0x80000000)
348 #define TEMPREG 40
349 #define FTEMP 40 // Load/store temporary register (was fpu)
350 #define PTEMP 41 // Prefetch temporary register
351 //#define TLREG 42 // TLB mapping offset
352 #define RHASH 43 // Return address hash
353 #define RHTBL 44 // Return address hash table address
354 #define RTEMP 45 // JR/JALR address register
355 #define MAXREG 45
356 #define AGEN1 46 // Address generation temporary register (pass5b_preallocate2)
357 //#define AGEN2 47 // Address generation temporary register
358
359   /* instruction types */
360 #define NOP 0     // No operation
361 #define LOAD 1    // Load
362 #define STORE 2   // Store
363 #define LOADLR 3  // Unaligned load
364 #define STORELR 4 // Unaligned store
365 #define MOV 5     // Move (hi/lo only)
366 #define ALU 6     // Arithmetic/logic
367 #define MULTDIV 7 // Multiply/divide
368 #define SHIFT 8   // Shift by register
369 #define SHIFTIMM 9// Shift by immediate
370 #define IMM16 10  // 16-bit immediate
371 #define RJUMP 11  // Unconditional jump to register
372 #define UJUMP 12  // Unconditional jump
373 #define CJUMP 13  // Conditional branch (BEQ/BNE/BGTZ/BLEZ)
374 #define SJUMP 14  // Conditional branch (regimm format)
375 #define COP0 15   // Coprocessor 0
376 #define RFE 16
377 #define SYSCALL 22// SYSCALL,BREAK
378 #define OTHER 23  // Other/unknown - do nothing
379 #define HLECALL 26// PCSX fake opcodes for HLE
380 #define COP2 27   // Coprocessor 2 move
381 #define C2LS 28   // Coprocessor 2 load/store
382 #define C2OP 29   // Coprocessor 2 operation
383 #define INTCALL 30// Call interpreter to handle rare corner cases
384
385   /* branch codes */
386 #define TAKEN 1
387 #define NOTTAKEN 2
388
389 #define DJT_1 (void *)1l // no function, just a label in assem_debug log
390 #define DJT_2 (void *)2l
391
392 // asm linkage
393 void dyna_linker();
394 void cc_interrupt();
395 void jump_syscall   (u_int u0, u_int u1, u_int pc);
396 void jump_syscall_ds(u_int u0, u_int u1, u_int pc);
397 void jump_break   (u_int u0, u_int u1, u_int pc);
398 void jump_break_ds(u_int u0, u_int u1, u_int pc);
399 void jump_overflow   (u_int u0, u_int u1, u_int pc);
400 void jump_overflow_ds(u_int u0, u_int u1, u_int pc);
401 void jump_addrerror   (u_int cause, u_int addr, u_int pc);
402 void jump_addrerror_ds(u_int cause, u_int addr, u_int pc);
403 void jump_to_new_pc();
404 void call_gteStall();
405 void new_dyna_leave();
406
407 void *ndrc_get_addr_ht_param(u_int vaddr, int can_compile);
408 void *ndrc_get_addr_ht(u_int vaddr);
409 void ndrc_add_jump_out(u_int vaddr, void *src);
410 void ndrc_write_invalidate_one(u_int addr);
411 static void ndrc_write_invalidate_many(u_int addr, u_int end);
412
413 static int new_recompile_block(u_int addr);
414 static void invalidate_block(struct block_info *block);
415 static void exception_assemble(int i, const struct regstat *i_regs, int ccadj_);
416
417 // Needed by assembler
418 static void wb_register(signed char r, const signed char regmap[], u_int dirty);
419 static void wb_dirtys(const signed char i_regmap[], u_int i_dirty);
420 static void wb_needed_dirtys(const signed char i_regmap[], u_int i_dirty, int addr);
421 static void load_all_regs(const signed char i_regmap[]);
422 static void load_needed_regs(const signed char i_regmap[], const signed char next_regmap[]);
423 static void load_regs_entry(int t);
424 static void load_all_consts(const signed char regmap[], u_int dirty, int i);
425 static u_int get_host_reglist(const signed char *regmap);
426
427 static int get_final_value(int hr, int i, u_int *value);
428 static void add_stub(enum stub_type type, void *addr, void *retaddr,
429   u_int a, uintptr_t b, uintptr_t c, u_int d, u_int e);
430 static void add_stub_r(enum stub_type type, void *addr, void *retaddr,
431   int i, int addr_reg, const struct regstat *i_regs, int ccadj, u_int reglist);
432 static void add_to_linker(void *addr, u_int target, int ext);
433 static void *get_direct_memhandler(void *table, u_int addr,
434   enum stub_type type, uintptr_t *addr_host);
435 static void cop2_do_stall_check(u_int op, int i, const struct regstat *i_regs, u_int reglist);
436 static void pass_args(int a0, int a1);
437 static void emit_far_jump(const void *f);
438 static void emit_far_call(const void *f);
439
440 #ifdef VITA
441 #include <psp2/kernel/sysmem.h>
442 static int sceBlock;
443 // note: this interacts with RetroArch's Vita bootstrap code: bootstrap/vita/sbrk.c
444 extern int getVMBlock();
445 int _newlib_vm_size_user = sizeof(*ndrc);
446 #endif
447
448 static void mprotect_w_x(void *start, void *end, int is_x)
449 {
450 #ifdef NO_WRITE_EXEC
451   #if defined(VITA)
452   // *Open* enables write on all memory that was
453   // allocated by sceKernelAllocMemBlockForVM()?
454   if (is_x)
455     sceKernelCloseVMDomain();
456   else
457     sceKernelOpenVMDomain();
458   #elif defined(HAVE_LIBNX)
459   Result rc;
460   // check to avoid the full flush in jitTransitionToExecutable()
461   if (g_jit.type != JitType_CodeMemory) {
462     if (is_x)
463       rc = jitTransitionToExecutable(&g_jit);
464     else
465       rc = jitTransitionToWritable(&g_jit);
466     if (R_FAILED(rc))
467       ;//SysPrintf("jitTransition %d %08x\n", is_x, rc);
468   }
469   #elif defined(TC_WRITE_OFFSET)
470   // separated rx and rw areas are always available
471   #else
472   u_long mstart = (u_long)start & ~4095ul;
473   u_long mend = (u_long)end;
474   if (mprotect((void *)mstart, mend - mstart,
475                PROT_READ | (is_x ? PROT_EXEC : PROT_WRITE)) != 0)
476     SysPrintf("mprotect(%c) failed: %s\n", is_x ? 'x' : 'w', strerror(errno));
477   #endif
478 #endif
479 }
480
481 static void start_tcache_write(void *start, void *end)
482 {
483   mprotect_w_x(start, end, 0);
484 }
485
486 static void end_tcache_write(void *start, void *end)
487 {
488 #if defined(__arm__) || defined(__aarch64__)
489   size_t len = (char *)end - (char *)start;
490   #if   defined(__BLACKBERRY_QNX__)
491   msync(start, len, MS_SYNC | MS_CACHE_ONLY | MS_INVALIDATE_ICACHE);
492   #elif defined(__MACH__)
493   sys_cache_control(kCacheFunctionPrepareForExecution, start, len);
494   #elif defined(VITA)
495   sceKernelSyncVMDomain(sceBlock, start, len);
496   #elif defined(_3DS)
497   ctr_flush_invalidate_cache();
498   #elif defined(HAVE_LIBNX)
499   if (g_jit.type == JitType_CodeMemory) {
500     armDCacheClean(start, len);
501     armICacheInvalidate((char *)start - ndrc_write_ofs, len);
502     // as of v4.2.1 libnx lacks isb
503     __asm__ volatile("isb" ::: "memory");
504   }
505   #elif defined(__aarch64__)
506   // as of 2021, __clear_cache() is still broken on arm64
507   // so here is a custom one :(
508   clear_cache_arm64(start, end);
509   #else
510   __clear_cache(start, end);
511   #endif
512   (void)len;
513 #endif
514
515   mprotect_w_x(start, end, 1);
516 }
517
518 static void *start_block(void)
519 {
520   u_char *end = out + MAX_OUTPUT_BLOCK_SIZE;
521   if (end > ndrc->translation_cache + sizeof(ndrc->translation_cache))
522     end = ndrc->translation_cache + sizeof(ndrc->translation_cache);
523   start_tcache_write(NDRC_WRITE_OFFSET(out), NDRC_WRITE_OFFSET(end));
524   return out;
525 }
526
527 static void end_block(void *start)
528 {
529   end_tcache_write(NDRC_WRITE_OFFSET(start), NDRC_WRITE_OFFSET(out));
530 }
531
532 #ifdef NDRC_CACHE_FLUSH_ALL
533
534 static int needs_clear_cache;
535
536 static void mark_clear_cache(void *target)
537 {
538   if (!needs_clear_cache) {
539     start_tcache_write(NDRC_WRITE_OFFSET(ndrc), NDRC_WRITE_OFFSET(ndrc + 1));
540     needs_clear_cache = 1;
541   }
542 }
543
544 static void do_clear_cache(void)
545 {
546   if (needs_clear_cache) {
547     end_tcache_write(NDRC_WRITE_OFFSET(ndrc), NDRC_WRITE_OFFSET(ndrc + 1));
548     needs_clear_cache = 0;
549   }
550 }
551
552 #else
553
554 // also takes care of w^x mappings when patching code
555 static u_int needs_clear_cache[1<<(TARGET_SIZE_2-17)];
556
557 static void mark_clear_cache(void *target)
558 {
559   uintptr_t offset = (u_char *)target - ndrc->translation_cache;
560   u_int mask = 1u << ((offset >> 12) & 31);
561   if (!(needs_clear_cache[offset >> 17] & mask)) {
562     char *start = (char *)NDRC_WRITE_OFFSET((uintptr_t)target & ~4095l);
563     start_tcache_write(start, start + 4095);
564     needs_clear_cache[offset >> 17] |= mask;
565   }
566 }
567
568 // Clearing the cache is rather slow on ARM Linux, so mark the areas
569 // that need to be cleared, and then only clear these areas once.
570 static void do_clear_cache(void)
571 {
572   int i, j;
573   for (i = 0; i < (1<<(TARGET_SIZE_2-17)); i++)
574   {
575     u_int bitmap = needs_clear_cache[i];
576     if (!bitmap)
577       continue;
578     for (j = 0; j < 32; j++)
579     {
580       u_char *start, *end;
581       if (!(bitmap & (1u << j)))
582         continue;
583
584       start = ndrc->translation_cache + i*131072 + j*4096;
585       end = start + 4095;
586       for (j++; j < 32; j++) {
587         if (!(bitmap & (1u << j)))
588           break;
589         end += 4096;
590       }
591       end_tcache_write(NDRC_WRITE_OFFSET(start), NDRC_WRITE_OFFSET(end));
592     }
593     needs_clear_cache[i] = 0;
594   }
595 }
596
597 #endif // NDRC_CACHE_FLUSH_ALL
598
599 #define NO_CYCLE_PENALTY_THR 12
600
601 int cycle_multiplier_old;
602 static int cycle_multiplier_active;
603
604 static int CLOCK_ADJUST(int x)
605 {
606   int m = cycle_multiplier_active;
607   int s = (x >> 31) | 1;
608   return (x * m + s * 50) / 100;
609 }
610
611 static int ds_writes_rjump_rs(int i)
612 {
613   return dops[i].rs1 != 0
614    && (dops[i].rs1 == dops[i+1].rt1 || dops[i].rs1 == dops[i+1].rt2
615     || dops[i].rs1 == dops[i].rt1); // overwrites itself - same effect
616 }
617
618 // psx addr mirror masking (for invalidation)
619 static u_int pmmask(u_int vaddr)
620 {
621   vaddr &= ~0xe0000000;
622   if (vaddr < 0x01000000)
623     vaddr &= ~0x00e00000; // RAM mirrors
624   return vaddr;
625 }
626
627 static u_int get_page(u_int vaddr)
628 {
629   u_int page = pmmask(vaddr) >> 12;
630   if (page >= PAGE_COUNT / 2)
631     page = PAGE_COUNT / 2 + (page & (PAGE_COUNT / 2 - 1));
632   return page;
633 }
634
635 // get a page for looking for a block that has vaddr
636 // (needed because the block may start in previous page)
637 static u_int get_page_prev(u_int vaddr)
638 {
639   assert(MAXBLOCK <= (1 << 12));
640   u_int page = get_page(vaddr);
641   if (page & 511)
642     page--;
643   return page;
644 }
645
646 static struct ht_entry *hash_table_get(u_int vaddr)
647 {
648   return &hash_table[((vaddr>>16)^vaddr)&0xFFFF];
649 }
650
651 #define HASH_TABLE_BAD 0xbac
652
653 static void hash_table_clear(void)
654 {
655   struct ht_entry *ht_bin;
656   int i, j;
657   for (i = 0; i < ARRAY_SIZE(hash_table); i++) {
658     for (j = 0; j < ARRAY_SIZE(hash_table[i].vaddr); j++) {
659       hash_table[i].vaddr[j] = ~0;
660       hash_table[i].tcaddr[j] = (void *)(uintptr_t)HASH_TABLE_BAD;
661     }
662   }
663   // don't allow ~0 to hit
664   ht_bin = hash_table_get(~0);
665   for (j = 0; j < ARRAY_SIZE(ht_bin->vaddr); j++)
666     ht_bin->vaddr[j] = 1;
667 }
668
669 static void hash_table_add(u_int vaddr, void *tcaddr)
670 {
671   struct ht_entry *ht_bin = hash_table_get(vaddr);
672   assert(tcaddr);
673   ht_bin->vaddr[1] = ht_bin->vaddr[0];
674   ht_bin->tcaddr[1] = ht_bin->tcaddr[0];
675   ht_bin->vaddr[0] = vaddr;
676   ht_bin->tcaddr[0] = tcaddr;
677 }
678
679 static void hash_table_remove(int vaddr)
680 {
681   //printf("remove hash: %x\n",vaddr);
682   struct ht_entry *ht_bin = hash_table_get(vaddr);
683   if (ht_bin->vaddr[1] == vaddr) {
684     ht_bin->vaddr[1] = ~0;
685     ht_bin->tcaddr[1] = (void *)(uintptr_t)HASH_TABLE_BAD;
686   }
687   if (ht_bin->vaddr[0] == vaddr) {
688     ht_bin->vaddr[0] = ht_bin->vaddr[1];
689     ht_bin->tcaddr[0] = ht_bin->tcaddr[1];
690     ht_bin->vaddr[1] = ~0;
691     ht_bin->tcaddr[1] = (void *)(uintptr_t)HASH_TABLE_BAD;
692   }
693 }
694
695 static void mini_ht_clear(void)
696 {
697 #ifdef USE_MINI_HT
698   int i;
699   for (i = 0; i < ARRAY_SIZE(mini_ht) - 1; i++) {
700     mini_ht[i][0] = ~0;
701     mini_ht[i][1] = HASH_TABLE_BAD;
702   }
703   mini_ht[i][0] = 1;
704   mini_ht[i][1] = HASH_TABLE_BAD;
705 #endif
706 }
707
708 static void mark_invalid_code(u_int vaddr, u_int len, char invalid)
709 {
710   u_int vaddr_m = vaddr & 0x1fffffff;
711   u_int i, j;
712   for (i = vaddr_m & ~0xfff; i < vaddr_m + len; i += 0x1000) {
713     // ram mirrors, but should not hurt bios
714     for (j = 0; j < 0x800000; j += 0x200000) {
715       invalid_code[(i|j) >> 12] =
716       invalid_code[(i|j|0x80000000u) >> 12] =
717       invalid_code[(i|j|0xa0000000u) >> 12] = invalid;
718     }
719   }
720   if (!invalid && vaddr + len > inv_code_start && vaddr <= inv_code_end)
721     inv_code_start = inv_code_end = ~0;
722 }
723
724 static int doesnt_expire_soon(u_char *tcaddr)
725 {
726   u_int diff = (u_int)(tcaddr - out) & ((1u << TARGET_SIZE_2) - 1u);
727   return diff > EXPIRITY_OFFSET + MAX_OUTPUT_BLOCK_SIZE;
728 }
729
730 static unused void check_for_block_changes(u_int start, u_int end)
731 {
732   u_int start_page = get_page_prev(start);
733   u_int end_page = get_page(end - 1);
734   u_int page;
735
736   for (page = start_page; page <= end_page; page++) {
737     struct block_info *block;
738     for (block = blocks[page]; block != NULL; block = block->next) {
739       if (block->is_dirty)
740         continue;
741       if (memcmp(block->source, block->copy, block->len)) {
742         printf("bad block %08x-%08x %016llx %016llx @%08x\n",
743           block->start, block->start + block->len,
744           *(long long *)block->source, *(long long *)block->copy, psxRegs.pc);
745         fflush(stdout);
746         abort();
747       }
748     }
749   }
750 }
751
752 static void *try_restore_block(u_int vaddr, u_int start_page, u_int end_page)
753 {
754   void *found_clean = NULL;
755   u_int i, page;
756
757   stat_inc(stat_restore_tries);
758   for (page = start_page; page <= end_page; page++) {
759     struct block_info *block;
760     for (block = blocks[page]; block != NULL; block = block->next) {
761       if (vaddr < block->start)
762         break;
763       if (!block->is_dirty || vaddr >= block->start + block->len)
764         continue;
765       for (i = 0; i < block->jump_in_cnt; i++)
766         if (block->jump_in[i].vaddr == vaddr)
767           break;
768       if (i == block->jump_in_cnt)
769         continue;
770       assert(block->source && block->copy);
771       stat_inc(stat_restore_compares);
772       if (memcmp(block->source, block->copy, block->len))
773         continue;
774
775       block->is_dirty = block->inv_near_misses = 0;
776       found_clean = block->jump_in[i].addr;
777       hash_table_add(vaddr, found_clean);
778       mark_invalid_code(block->start, block->len, 0);
779       stat_inc(stat_bc_restore);
780       inv_debug("INV: restored %08x %p (%d)\n", vaddr, found_clean, block->jump_in_cnt);
781       return found_clean;
782     }
783   }
784   return NULL;
785 }
786
787 // this doesn't normally happen
788 static noinline u_int generate_exception(u_int pc)
789 {
790   //if (execBreakCheck(&psxRegs, pc))
791   //  return psxRegs.pc;
792
793   // generate an address or bus error
794   psxRegs.CP0.n.Cause &= 0x300;
795   psxRegs.CP0.n.EPC = pc;
796   if (pc & 3) {
797     psxRegs.CP0.n.Cause |= R3000E_AdEL << 2;
798     psxRegs.CP0.n.BadVAddr = pc;
799 #ifdef DRC_DBG
800     last_count -= 2;
801 #endif
802   } else
803     psxRegs.CP0.n.Cause |= R3000E_IBE << 2;
804   return (psxRegs.pc = 0x80000080);
805 }
806
807 // Get address from virtual address
808 // This is called from the recompiled JR/JALR instructions
809 static void noinline *get_addr(u_int vaddr, int can_compile)
810 {
811   u_int start_page = get_page_prev(vaddr);
812   u_int i, page, end_page = get_page(vaddr);
813   void *found_clean = NULL;
814
815   stat_inc(stat_jump_in_lookups);
816   for (page = start_page; page <= end_page; page++) {
817     const struct block_info *block;
818     for (block = blocks[page]; block != NULL; block = block->next) {
819       if (vaddr < block->start)
820         break;
821       if (block->is_dirty || vaddr >= block->start + block->len)
822         continue;
823       for (i = 0; i < block->jump_in_cnt; i++)
824         if (block->jump_in[i].vaddr == vaddr)
825           break;
826       if (i == block->jump_in_cnt)
827         continue;
828       found_clean = block->jump_in[i].addr;
829       hash_table_add(vaddr, found_clean);
830       return found_clean;
831     }
832   }
833   found_clean = try_restore_block(vaddr, start_page, end_page);
834   if (found_clean)
835     return found_clean;
836
837   if (!can_compile)
838     return NULL;
839
840   int r = new_recompile_block(vaddr);
841   if (likely(r == 0))
842     return ndrc_get_addr_ht(vaddr);
843
844   return ndrc_get_addr_ht(generate_exception(vaddr));
845 }
846
847 // Look up address in hash table first
848 void *ndrc_get_addr_ht_param(u_int vaddr, int can_compile)
849 {
850   //check_for_block_changes(vaddr, vaddr + MAXBLOCK);
851   const struct ht_entry *ht_bin = hash_table_get(vaddr);
852   u_int vaddr_a = vaddr & ~3;
853   stat_inc(stat_ht_lookups);
854   if (ht_bin->vaddr[0] == vaddr_a) return ht_bin->tcaddr[0];
855   if (ht_bin->vaddr[1] == vaddr_a) return ht_bin->tcaddr[1];
856   return get_addr(vaddr, can_compile);
857 }
858
859 void *ndrc_get_addr_ht(u_int vaddr)
860 {
861   return ndrc_get_addr_ht_param(vaddr, 1);
862 }
863
864 static void clear_all_regs(signed char regmap[])
865 {
866   memset(regmap, -1, sizeof(regmap[0]) * HOST_REGS);
867 }
868
869 // get_reg: get allocated host reg from mips reg
870 // returns -1 if no such mips reg was allocated
871 #if defined(__arm__) && defined(HAVE_ARMV6) && HOST_REGS == 13 && EXCLUDE_REG == 11
872
873 extern signed char get_reg(const signed char regmap[], signed char r);
874
875 #else
876
877 static signed char get_reg(const signed char regmap[], signed char r)
878 {
879   int hr;
880   for (hr = 0; hr < HOST_REGS; hr++) {
881     if (hr == EXCLUDE_REG)
882       continue;
883     if (regmap[hr] == r)
884       return hr;
885   }
886   return -1;
887 }
888
889 #endif
890
891 // get reg suitable for writing
892 static signed char get_reg_w(const signed char regmap[], signed char r)
893 {
894   return r == 0 ? -1 : get_reg(regmap, r);
895 }
896
897 // get reg as mask bit (1 << hr)
898 static u_int get_regm(const signed char regmap[], signed char r)
899 {
900   return (1u << (get_reg(regmap, r) & 31)) & ~(1u << 31);
901 }
902
903 static signed char get_reg_temp(const signed char regmap[])
904 {
905   int hr;
906   for (hr = 0; hr < HOST_REGS; hr++) {
907     if (hr == EXCLUDE_REG)
908       continue;
909     if (regmap[hr] == (signed char)-1)
910       return hr;
911   }
912   return -1;
913 }
914
915 // Find a register that is available for two consecutive cycles
916 static signed char get_reg2(signed char regmap1[], const signed char regmap2[], int r)
917 {
918   int hr;
919   for (hr=0;hr<HOST_REGS;hr++) if(hr!=EXCLUDE_REG&&regmap1[hr]==r&&regmap2[hr]==r) return hr;
920   return -1;
921 }
922
923 // reverse reg map: mips -> host
924 #define RRMAP_SIZE 64
925 static void make_rregs(const signed char regmap[], signed char rrmap[RRMAP_SIZE],
926   u_int *regs_can_change)
927 {
928   u_int r, hr, hr_can_change = 0;
929   memset(rrmap, -1, RRMAP_SIZE);
930   for (hr = 0; hr < HOST_REGS; )
931   {
932     r = regmap[hr];
933     rrmap[r & (RRMAP_SIZE - 1)] = hr;
934     // only add mips $1-$31+$lo, others shifted out
935     hr_can_change |= (uint64_t)1 << (hr + ((r - 1) & 32));
936     hr++;
937     if (hr == EXCLUDE_REG)
938       hr++;
939   }
940   hr_can_change |= 1u << (rrmap[33] & 31);
941   hr_can_change |= 1u << (rrmap[CCREG] & 31);
942   hr_can_change &= ~(1u << 31);
943   *regs_can_change = hr_can_change;
944 }
945
946 // same as get_reg, but takes rrmap
947 static signed char get_rreg(signed char rrmap[RRMAP_SIZE], signed char r)
948 {
949   assert(0 <= r && r < RRMAP_SIZE);
950   return rrmap[r];
951 }
952
953 static int count_free_regs(const signed char regmap[])
954 {
955   int count=0;
956   int hr;
957   for(hr=0;hr<HOST_REGS;hr++)
958   {
959     if(hr!=EXCLUDE_REG) {
960       if(regmap[hr]<0) count++;
961     }
962   }
963   return count;
964 }
965
966 static void dirty_reg(struct regstat *cur, signed char reg)
967 {
968   int hr;
969   if (!reg) return;
970   hr = get_reg(cur->regmap, reg);
971   if (hr >= 0)
972     cur->dirty |= 1<<hr;
973 }
974
975 static void set_const(struct regstat *cur, signed char reg, uint32_t value)
976 {
977   int hr;
978   if (!reg) return;
979   hr = get_reg(cur->regmap, reg);
980   if (hr >= 0) {
981     cur->isconst |= 1<<hr;
982     current_constmap[hr] = value;
983   }
984 }
985
986 static void clear_const(struct regstat *cur, signed char reg)
987 {
988   int hr;
989   if (!reg) return;
990   hr = get_reg(cur->regmap, reg);
991   if (hr >= 0)
992     cur->isconst &= ~(1<<hr);
993 }
994
995 static int is_const(const struct regstat *cur, signed char reg)
996 {
997   int hr;
998   if (reg < 0) return 0;
999   if (!reg) return 1;
1000   hr = get_reg(cur->regmap, reg);
1001   if (hr >= 0)
1002     return (cur->isconst>>hr)&1;
1003   return 0;
1004 }
1005
1006 static uint32_t get_const(const struct regstat *cur, signed char reg)
1007 {
1008   int hr;
1009   if (!reg) return 0;
1010   hr = get_reg(cur->regmap, reg);
1011   if (hr >= 0)
1012     return current_constmap[hr];
1013
1014   SysPrintf("Unknown constant in r%d\n", reg);
1015   abort();
1016 }
1017
1018 // Least soon needed registers
1019 // Look at the next ten instructions and see which registers
1020 // will be used.  Try not to reallocate these.
1021 static void lsn(u_char hsn[], int i)
1022 {
1023   int j;
1024   int b=-1;
1025   for(j=0;j<9;j++)
1026   {
1027     if(i+j>=slen) {
1028       j=slen-i-1;
1029       break;
1030     }
1031     if (dops[i+j].is_ujump)
1032     {
1033       // Don't go past an unconditonal jump
1034       j++;
1035       break;
1036     }
1037   }
1038   for(;j>=0;j--)
1039   {
1040     if(dops[i+j].rs1) hsn[dops[i+j].rs1]=j;
1041     if(dops[i+j].rs2) hsn[dops[i+j].rs2]=j;
1042     if(dops[i+j].rt1) hsn[dops[i+j].rt1]=j;
1043     if(dops[i+j].rt2) hsn[dops[i+j].rt2]=j;
1044     if(dops[i+j].itype==STORE || dops[i+j].itype==STORELR) {
1045       // Stores can allocate zero
1046       hsn[dops[i+j].rs1]=j;
1047       hsn[dops[i+j].rs2]=j;
1048     }
1049     if (ram_offset && (dops[i+j].is_load || dops[i+j].is_store))
1050       hsn[ROREG] = j;
1051     // On some architectures stores need invc_ptr
1052     #if defined(HOST_IMM8)
1053     if (dops[i+j].is_store)
1054       hsn[INVCP] = j;
1055     #endif
1056     if(i+j>=0&&(dops[i+j].itype==UJUMP||dops[i+j].itype==CJUMP||dops[i+j].itype==SJUMP))
1057     {
1058       hsn[CCREG]=j;
1059       b=j;
1060     }
1061   }
1062   if(b>=0)
1063   {
1064     if(cinfo[i+b].ba>=start && cinfo[i+b].ba<(start+slen*4))
1065     {
1066       // Follow first branch
1067       int t=(cinfo[i+b].ba-start)>>2;
1068       j=7-b;if(t+j>=slen) j=slen-t-1;
1069       for(;j>=0;j--)
1070       {
1071         if(dops[t+j].rs1) if(hsn[dops[t+j].rs1]>j+b+2) hsn[dops[t+j].rs1]=j+b+2;
1072         if(dops[t+j].rs2) if(hsn[dops[t+j].rs2]>j+b+2) hsn[dops[t+j].rs2]=j+b+2;
1073         //if(dops[t+j].rt1) if(hsn[dops[t+j].rt1]>j+b+2) hsn[dops[t+j].rt1]=j+b+2;
1074         //if(dops[t+j].rt2) if(hsn[dops[t+j].rt2]>j+b+2) hsn[dops[t+j].rt2]=j+b+2;
1075       }
1076     }
1077     // TODO: preferred register based on backward branch
1078   }
1079   // Delay slot should preferably not overwrite branch conditions or cycle count
1080   if (i > 0 && dops[i-1].is_jump) {
1081     if(dops[i-1].rs1) if(hsn[dops[i-1].rs1]>1) hsn[dops[i-1].rs1]=1;
1082     if(dops[i-1].rs2) if(hsn[dops[i-1].rs2]>1) hsn[dops[i-1].rs2]=1;
1083     hsn[CCREG]=1;
1084     // ...or hash tables
1085     hsn[RHASH]=1;
1086     hsn[RHTBL]=1;
1087   }
1088   // Coprocessor load/store needs FTEMP, even if not declared
1089   if(dops[i].itype==C2LS) {
1090     hsn[FTEMP]=0;
1091   }
1092   // Load/store L/R also uses FTEMP as a temporary register
1093   if (dops[i].itype == LOADLR || dops[i].itype == STORELR) {
1094     hsn[FTEMP]=0;
1095   }
1096   // Don't remove the miniht registers
1097   if(dops[i].itype==UJUMP||dops[i].itype==RJUMP)
1098   {
1099     hsn[RHASH]=0;
1100     hsn[RHTBL]=0;
1101   }
1102 }
1103
1104 // We only want to allocate registers if we're going to use them again soon
1105 static int needed_again(int r, int i)
1106 {
1107   int j;
1108   int b=-1;
1109   int rn=10;
1110
1111   if (i > 0 && dops[i-1].is_ujump)
1112   {
1113     if(cinfo[i-1].ba<start || cinfo[i-1].ba>start+slen*4-4)
1114       return 0; // Don't need any registers if exiting the block
1115   }
1116   for(j=0;j<9;j++)
1117   {
1118     if(i+j>=slen) {
1119       j=slen-i-1;
1120       break;
1121     }
1122     if (dops[i+j].is_ujump)
1123     {
1124       // Don't go past an unconditonal jump
1125       j++;
1126       break;
1127     }
1128     if (dops[i+j].is_exception)
1129     {
1130       break;
1131     }
1132   }
1133   for(;j>=1;j--)
1134   {
1135     if(dops[i+j].rs1==r) rn=j;
1136     if(dops[i+j].rs2==r) rn=j;
1137     if((unneeded_reg[i+j]>>r)&1) rn=10;
1138     if(i+j>=0&&(dops[i+j].itype==UJUMP||dops[i+j].itype==CJUMP||dops[i+j].itype==SJUMP))
1139     {
1140       b=j;
1141     }
1142   }
1143   if(rn<10) return 1;
1144   (void)b;
1145   return 0;
1146 }
1147
1148 // Try to match register allocations at the end of a loop with those
1149 // at the beginning
1150 static int loop_reg(int i, int r, int hr)
1151 {
1152   int j,k;
1153   for(j=0;j<9;j++)
1154   {
1155     if(i+j>=slen) {
1156       j=slen-i-1;
1157       break;
1158     }
1159     if (dops[i+j].is_ujump)
1160     {
1161       // Don't go past an unconditonal jump
1162       j++;
1163       break;
1164     }
1165   }
1166   k=0;
1167   if(i>0){
1168     if(dops[i-1].itype==UJUMP||dops[i-1].itype==CJUMP||dops[i-1].itype==SJUMP)
1169       k--;
1170   }
1171   for(;k<j;k++)
1172   {
1173     assert(r < 64);
1174     if((unneeded_reg[i+k]>>r)&1) return hr;
1175     if(i+k>=0&&(dops[i+k].itype==UJUMP||dops[i+k].itype==CJUMP||dops[i+k].itype==SJUMP))
1176     {
1177       if(cinfo[i+k].ba>=start && cinfo[i+k].ba<(start+i*4))
1178       {
1179         int t=(cinfo[i+k].ba-start)>>2;
1180         int reg=get_reg(regs[t].regmap_entry,r);
1181         if(reg>=0) return reg;
1182         //reg=get_reg(regs[t+1].regmap_entry,r);
1183         //if(reg>=0) return reg;
1184       }
1185     }
1186   }
1187   return hr;
1188 }
1189
1190
1191 // Allocate every register, preserving source/target regs
1192 static void alloc_all(struct regstat *cur,int i)
1193 {
1194   int hr;
1195
1196   for(hr=0;hr<HOST_REGS;hr++) {
1197     if(hr!=EXCLUDE_REG) {
1198       if((cur->regmap[hr]!=dops[i].rs1)&&(cur->regmap[hr]!=dops[i].rs2)&&
1199          (cur->regmap[hr]!=dops[i].rt1)&&(cur->regmap[hr]!=dops[i].rt2))
1200       {
1201         cur->regmap[hr]=-1;
1202         cur->dirty&=~(1<<hr);
1203       }
1204       // Don't need zeros
1205       if(cur->regmap[hr]==0)
1206       {
1207         cur->regmap[hr]=-1;
1208         cur->dirty&=~(1<<hr);
1209       }
1210     }
1211   }
1212 }
1213
1214 #ifndef NDEBUG
1215 static int host_tempreg_in_use;
1216
1217 static void host_tempreg_acquire(void)
1218 {
1219   assert(!host_tempreg_in_use);
1220   host_tempreg_in_use = 1;
1221 }
1222
1223 static void host_tempreg_release(void)
1224 {
1225   host_tempreg_in_use = 0;
1226 }
1227 #else
1228 static void host_tempreg_acquire(void) {}
1229 static void host_tempreg_release(void) {}
1230 #endif
1231
1232 #ifdef ASSEM_PRINT
1233 extern void gen_interupt();
1234 extern void do_insn_cmp();
1235 #define FUNCNAME(f) { f, " " #f }
1236 static const struct {
1237   void *addr;
1238   const char *name;
1239 } function_names[] = {
1240   FUNCNAME(cc_interrupt),
1241   FUNCNAME(gen_interupt),
1242   FUNCNAME(ndrc_get_addr_ht),
1243   FUNCNAME(jump_handler_read8),
1244   FUNCNAME(jump_handler_read16),
1245   FUNCNAME(jump_handler_read32),
1246   FUNCNAME(jump_handler_write8),
1247   FUNCNAME(jump_handler_write16),
1248   FUNCNAME(jump_handler_write32),
1249   FUNCNAME(ndrc_write_invalidate_one),
1250   FUNCNAME(ndrc_write_invalidate_many),
1251   FUNCNAME(jump_to_new_pc),
1252   FUNCNAME(jump_break),
1253   FUNCNAME(jump_break_ds),
1254   FUNCNAME(jump_syscall),
1255   FUNCNAME(jump_syscall_ds),
1256   FUNCNAME(jump_overflow),
1257   FUNCNAME(jump_overflow_ds),
1258   FUNCNAME(jump_addrerror),
1259   FUNCNAME(jump_addrerror_ds),
1260   FUNCNAME(call_gteStall),
1261   FUNCNAME(new_dyna_leave),
1262   FUNCNAME(pcsx_mtc0),
1263   FUNCNAME(pcsx_mtc0_ds),
1264   FUNCNAME(execI),
1265 #ifdef __aarch64__
1266   FUNCNAME(do_memhandler_pre),
1267   FUNCNAME(do_memhandler_post),
1268 #endif
1269 #ifdef DRC_DBG
1270 # ifdef __aarch64__
1271   FUNCNAME(do_insn_cmp_arm64),
1272 # else
1273   FUNCNAME(do_insn_cmp),
1274 # endif
1275 #endif
1276 };
1277
1278 static const char *func_name(const void *a)
1279 {
1280   int i;
1281   for (i = 0; i < sizeof(function_names)/sizeof(function_names[0]); i++)
1282     if (function_names[i].addr == a)
1283       return function_names[i].name;
1284   return "";
1285 }
1286
1287 static const char *fpofs_name(u_int ofs)
1288 {
1289   u_int *p = (u_int *)&dynarec_local + ofs/sizeof(u_int);
1290   static char buf[64];
1291   switch (ofs) {
1292   #define ofscase(x) case LO_##x: return " ; " #x
1293   ofscase(next_interupt);
1294   ofscase(cycle_count);
1295   ofscase(last_count);
1296   ofscase(pending_exception);
1297   ofscase(stop);
1298   ofscase(address);
1299   ofscase(lo);
1300   ofscase(hi);
1301   ofscase(PC);
1302   ofscase(cycle);
1303   ofscase(mem_rtab);
1304   ofscase(mem_wtab);
1305   ofscase(psxH_ptr);
1306   ofscase(invc_ptr);
1307   ofscase(ram_offset);
1308   #undef ofscase
1309   }
1310   buf[0] = 0;
1311   if      (psxRegs.GPR.r <= p && p < &psxRegs.GPR.r[32])
1312     snprintf(buf, sizeof(buf), " ; r%d", (int)(p - psxRegs.GPR.r));
1313   else if (psxRegs.CP0.r <= p && p < &psxRegs.CP0.r[32])
1314     snprintf(buf, sizeof(buf), " ; cp0 $%d", (int)(p - psxRegs.CP0.r));
1315   else if (psxRegs.CP2D.r <= p && p < &psxRegs.CP2D.r[32])
1316     snprintf(buf, sizeof(buf), " ; cp2d $%d", (int)(p - psxRegs.CP2D.r));
1317   else if (psxRegs.CP2C.r <= p && p < &psxRegs.CP2C.r[32])
1318     snprintf(buf, sizeof(buf), " ; cp2c $%d", (int)(p - psxRegs.CP2C.r));
1319   return buf;
1320 }
1321 #else
1322 #define func_name(x) ""
1323 #define fpofs_name(x) ""
1324 #endif
1325
1326 #ifdef __i386__
1327 #include "assem_x86.c"
1328 #endif
1329 #ifdef __x86_64__
1330 #include "assem_x64.c"
1331 #endif
1332 #ifdef __arm__
1333 #include "assem_arm.c"
1334 #endif
1335 #ifdef __aarch64__
1336 #include "assem_arm64.c"
1337 #endif
1338
1339 static void *get_trampoline(const void *f)
1340 {
1341   struct ndrc_tramp *tramp = NDRC_WRITE_OFFSET(&ndrc->tramp);
1342   size_t i;
1343
1344   for (i = 0; i < ARRAY_SIZE(tramp->f); i++) {
1345     if (tramp->f[i] == f || tramp->f[i] == NULL)
1346       break;
1347   }
1348   if (i == ARRAY_SIZE(tramp->f)) {
1349     SysPrintf("trampoline table is full, last func %p\n", f);
1350     abort();
1351   }
1352   if (tramp->f[i] == NULL) {
1353     start_tcache_write(&tramp->f[i], &tramp->f[i + 1]);
1354     tramp->f[i] = f;
1355     end_tcache_write(&tramp->f[i], &tramp->f[i + 1]);
1356 #ifdef HAVE_LIBNX
1357     // invalidate the RX mirror (unsure if necessary, but just in case...)
1358     armDCacheFlush(&ndrc->tramp.f[i], sizeof(ndrc->tramp.f[i]));
1359 #endif
1360   }
1361   return &ndrc->tramp.ops[i];
1362 }
1363
1364 static void emit_far_jump(const void *f)
1365 {
1366   if (can_jump_or_call(f)) {
1367     emit_jmp(f);
1368     return;
1369   }
1370
1371   f = get_trampoline(f);
1372   emit_jmp(f);
1373 }
1374
1375 static void emit_far_call(const void *f)
1376 {
1377   if (can_jump_or_call(f)) {
1378     emit_call(f);
1379     return;
1380   }
1381
1382   f = get_trampoline(f);
1383   emit_call(f);
1384 }
1385
1386 // Check if an address is already compiled
1387 // but don't return addresses which are about to expire from the cache
1388 static void *check_addr(u_int vaddr)
1389 {
1390   struct ht_entry *ht_bin = hash_table_get(vaddr);
1391   size_t i;
1392   for (i = 0; i < ARRAY_SIZE(ht_bin->vaddr); i++) {
1393     if (ht_bin->vaddr[i] == vaddr)
1394       if (doesnt_expire_soon(ht_bin->tcaddr[i]))
1395         return ht_bin->tcaddr[i];
1396   }
1397
1398   // refactor to get_addr_nocompile?
1399   u_int start_page = get_page_prev(vaddr);
1400   u_int page, end_page = get_page(vaddr);
1401
1402   stat_inc(stat_jump_in_lookups);
1403   for (page = start_page; page <= end_page; page++) {
1404     const struct block_info *block;
1405     for (block = blocks[page]; block != NULL; block = block->next) {
1406       if (vaddr < block->start)
1407         break;
1408       if (block->is_dirty || vaddr >= block->start + block->len)
1409         continue;
1410       if (!doesnt_expire_soon(ndrc->translation_cache + block->tc_offs))
1411         continue;
1412       for (i = 0; i < block->jump_in_cnt; i++)
1413         if (block->jump_in[i].vaddr == vaddr)
1414           break;
1415       if (i == block->jump_in_cnt)
1416         continue;
1417
1418       // Update existing entry with current address
1419       void *addr = block->jump_in[i].addr;
1420       if (ht_bin->vaddr[0] == vaddr) {
1421         ht_bin->tcaddr[0] = addr;
1422         return addr;
1423       }
1424       if (ht_bin->vaddr[1] == vaddr) {
1425         ht_bin->tcaddr[1] = addr;
1426         return addr;
1427       }
1428       // Insert into hash table with low priority.
1429       // Don't evict existing entries, as they are probably
1430       // addresses that are being accessed frequently.
1431       if (ht_bin->vaddr[0] == -1) {
1432         ht_bin->vaddr[0] = vaddr;
1433         ht_bin->tcaddr[0] = addr;
1434       }
1435       else if (ht_bin->vaddr[1] == -1) {
1436         ht_bin->vaddr[1] = vaddr;
1437         ht_bin->tcaddr[1] = addr;
1438       }
1439       return addr;
1440     }
1441   }
1442   return NULL;
1443 }
1444
1445 static void blocks_clear(struct block_info **head)
1446 {
1447   struct block_info *cur, *next;
1448
1449   if ((cur = *head)) {
1450     *head = NULL;
1451     while (cur) {
1452       next = cur->next;
1453       free(cur);
1454       cur = next;
1455     }
1456   }
1457 }
1458
1459 static int blocks_remove_matching_addrs(struct block_info **head,
1460   u_int base_offs, int shift)
1461 {
1462   struct block_info *next;
1463   int hit = 0;
1464   while (*head) {
1465     if ((((*head)->tc_offs ^ base_offs) >> shift) == 0) {
1466       inv_debug("EXP: rm block %08x (tc_offs %x)\n", (*head)->start, (*head)->tc_offs);
1467       invalidate_block(*head);
1468       next = (*head)->next;
1469       free(*head);
1470       *head = next;
1471       stat_dec(stat_blocks);
1472       hit = 1;
1473     }
1474     else
1475     {
1476       head = &((*head)->next);
1477     }
1478   }
1479   return hit;
1480 }
1481
1482 // This is called when we write to a compiled block (see do_invstub)
1483 static void unlink_jumps_vaddr_range(u_int start, u_int end)
1484 {
1485   u_int page, start_page = get_page(start), end_page = get_page(end - 1);
1486   int i;
1487
1488   for (page = start_page; page <= end_page; page++) {
1489     struct jump_info *ji = jumps[page];
1490     if (ji == NULL)
1491       continue;
1492     for (i = 0; i < ji->count; ) {
1493       if (ji->e[i].target_vaddr < start || ji->e[i].target_vaddr >= end) {
1494         i++;
1495         continue;
1496       }
1497
1498       inv_debug("INV: rm link to %08x (tc_offs %zx)\n", ji->e[i].target_vaddr,
1499         (u_char *)ji->e[i].stub - ndrc->translation_cache);
1500       void *host_addr = find_extjump_insn(ji->e[i].stub);
1501       mark_clear_cache(host_addr);
1502       set_jump_target(host_addr, ji->e[i].stub); // point back to dyna_linker stub
1503
1504       stat_dec(stat_links);
1505       ji->count--;
1506       if (i < ji->count) {
1507         ji->e[i] = ji->e[ji->count];
1508         continue;
1509       }
1510       i++;
1511     }
1512   }
1513 }
1514
1515 static void unlink_jumps_tc_range(struct jump_info *ji, u_int base_offs, int shift)
1516 {
1517   int i;
1518   if (ji == NULL)
1519     return;
1520   for (i = 0; i < ji->count; ) {
1521     u_int tc_offs = (u_char *)ji->e[i].stub - ndrc->translation_cache;
1522     if (((tc_offs ^ base_offs) >> shift) != 0) {
1523       i++;
1524       continue;
1525     }
1526
1527     inv_debug("EXP: rm link to %08x (tc_offs %x)\n", ji->e[i].target_vaddr, tc_offs);
1528     stat_dec(stat_links);
1529     ji->count--;
1530     if (i < ji->count) {
1531       ji->e[i] = ji->e[ji->count];
1532       continue;
1533     }
1534     i++;
1535   }
1536 }
1537
1538 static void invalidate_block(struct block_info *block)
1539 {
1540   u_int i;
1541
1542   block->is_dirty = 1;
1543   unlink_jumps_vaddr_range(block->start, block->start + block->len);
1544   for (i = 0; i < block->jump_in_cnt; i++)
1545     hash_table_remove(block->jump_in[i].vaddr);
1546 }
1547
1548 static int invalidate_range(u_int start, u_int end,
1549   u32 *inv_start_ret, u32 *inv_end_ret)
1550 {
1551   struct block_info *last_block = NULL;
1552   u_int start_page = get_page_prev(start);
1553   u_int end_page = get_page(end - 1);
1554   u_int start_m = pmmask(start);
1555   u_int end_m = pmmask(end - 1);
1556   u_int inv_start, inv_end;
1557   u_int blk_start_m, blk_end_m;
1558   u_int page;
1559   int hit = 0;
1560
1561   // additional area without code (to supplement invalid_code[]), [start, end)
1562   // avoids excessive ndrc_write_invalidate*() calls
1563   inv_start = start_m & ~0xfff;
1564   inv_end = end_m | 0xfff;
1565
1566   for (page = start_page; page <= end_page; page++) {
1567     struct block_info *block;
1568     for (block = blocks[page]; block != NULL; block = block->next) {
1569       if (block->is_dirty)
1570         continue;
1571       last_block = block;
1572       blk_end_m = pmmask(block->start + block->len);
1573       if (blk_end_m <= start_m) {
1574         inv_start = max(inv_start, blk_end_m);
1575         continue;
1576       }
1577       blk_start_m = pmmask(block->start);
1578       if (end_m <= blk_start_m) {
1579         inv_end = min(inv_end, blk_start_m - 1);
1580         continue;
1581       }
1582       if (!block->source) // "hack" block - leave it alone
1583         continue;
1584
1585       hit++;
1586       invalidate_block(block);
1587       stat_inc(stat_inv_hits);
1588     }
1589   }
1590
1591   if (!hit && last_block && last_block->source) {
1592     // could be some leftover unused block, uselessly trapping writes
1593     last_block->inv_near_misses++;
1594     if (last_block->inv_near_misses > 128) {
1595       invalidate_block(last_block);
1596       stat_inc(stat_inv_hits);
1597       hit++;
1598     }
1599   }
1600   if (hit) {
1601     do_clear_cache();
1602     mini_ht_clear();
1603   }
1604
1605   if (inv_start <= (start_m & ~0xfff) && inv_end >= (start_m | 0xfff))
1606     // the whole page is empty now
1607     mark_invalid_code(start, 1, 1);
1608
1609   if (inv_start_ret) *inv_start_ret = inv_start | (start & 0xe0000000);
1610   if (inv_end_ret) *inv_end_ret = inv_end | (end & 0xe0000000);
1611   return hit;
1612 }
1613
1614 void new_dynarec_invalidate_range(unsigned int start, unsigned int end)
1615 {
1616   invalidate_range(start, end, NULL, NULL);
1617 }
1618
1619 static void ndrc_write_invalidate_many(u_int start, u_int end)
1620 {
1621   // this check is done by the caller
1622   //if (inv_code_start<=addr&&addr<=inv_code_end) { rhits++; return; }
1623   int ret = invalidate_range(start, end, &inv_code_start, &inv_code_end);
1624 #ifdef INV_DEBUG_W
1625   int invc = invalid_code[start >> 12];
1626   u_int len = end - start;
1627   if (ret)
1628     printf("INV ADDR: %08x/%02x hit %d blocks\n", start, len, ret);
1629   else
1630     printf("INV ADDR: %08x/%02x miss, inv %08x-%08x invc %d->%d\n", start, len,
1631       inv_code_start, inv_code_end, invc, invalid_code[start >> 12]);
1632   check_for_block_changes(start, end);
1633 #endif
1634   stat_inc(stat_inv_addr_calls);
1635   (void)ret;
1636 }
1637
1638 void ndrc_write_invalidate_one(u_int addr)
1639 {
1640   ndrc_write_invalidate_many(addr, addr + 4);
1641 }
1642
1643 // This is called when loading a save state.
1644 // Anything could have changed, so invalidate everything.
1645 void new_dynarec_invalidate_all_pages(void)
1646 {
1647   struct block_info *block;
1648   u_int page;
1649   for (page = 0; page < ARRAY_SIZE(blocks); page++) {
1650     for (block = blocks[page]; block != NULL; block = block->next) {
1651       if (block->is_dirty)
1652         continue;
1653       if (!block->source) // hack block?
1654         continue;
1655       invalidate_block(block);
1656     }
1657   }
1658
1659   do_clear_cache();
1660   mini_ht_clear();
1661 }
1662
1663 // Add an entry to jump_out after making a link
1664 // src should point to code by emit_extjump()
1665 void ndrc_add_jump_out(u_int vaddr, void *src)
1666 {
1667   inv_debug("ndrc_add_jump_out: %p -> %x\n", src, vaddr);
1668   u_int page = get_page(vaddr);
1669   struct jump_info *ji;
1670
1671   stat_inc(stat_links);
1672   check_extjump2(src);
1673   ji = jumps[page];
1674   if (ji == NULL) {
1675     ji = malloc(sizeof(*ji) + sizeof(ji->e[0]) * 16);
1676     ji->alloc = 16;
1677     ji->count = 0;
1678   }
1679   else if (ji->count >= ji->alloc) {
1680     ji->alloc += 16;
1681     ji = realloc(ji, sizeof(*ji) + sizeof(ji->e[0]) * ji->alloc);
1682   }
1683   jumps[page] = ji;
1684   ji->e[ji->count].target_vaddr = vaddr;
1685   ji->e[ji->count].stub = src;
1686   ji->count++;
1687 }
1688
1689 /* Register allocation */
1690
1691 static void alloc_set(struct regstat *cur, int reg, int hr)
1692 {
1693   cur->regmap[hr] = reg;
1694   cur->dirty &= ~(1u << hr);
1695   cur->isconst &= ~(1u << hr);
1696   cur->noevict |= 1u << hr;
1697 }
1698
1699 static void evict_alloc_reg(struct regstat *cur, int i, int reg, int preferred_hr)
1700 {
1701   u_char hsn[MAXREG+1];
1702   int j, r, hr;
1703   memset(hsn, 10, sizeof(hsn));
1704   lsn(hsn, i);
1705   //printf("hsn(%x): %d %d %d %d %d %d %d\n",start+i*4,hsn[cur->regmap[0]&63],hsn[cur->regmap[1]&63],hsn[cur->regmap[2]&63],hsn[cur->regmap[3]&63],hsn[cur->regmap[5]&63],hsn[cur->regmap[6]&63],hsn[cur->regmap[7]&63]);
1706   if(i>0) {
1707     // Don't evict the cycle count at entry points, otherwise the entry
1708     // stub will have to write it.
1709     if(dops[i].bt&&hsn[CCREG]>2) hsn[CCREG]=2;
1710     if (i>1 && hsn[CCREG] > 2 && dops[i-2].is_jump) hsn[CCREG]=2;
1711     for(j=10;j>=3;j--)
1712     {
1713       // Alloc preferred register if available
1714       if (!((cur->noevict >> preferred_hr) & 1)
1715           && hsn[cur->regmap[preferred_hr]] == j)
1716       {
1717         alloc_set(cur, reg, preferred_hr);
1718         return;
1719       }
1720       for(r=1;r<=MAXREG;r++)
1721       {
1722         if(hsn[r]==j&&r!=dops[i-1].rs1&&r!=dops[i-1].rs2&&r!=dops[i-1].rt1&&r!=dops[i-1].rt2) {
1723           for(hr=0;hr<HOST_REGS;hr++) {
1724             if (hr == EXCLUDE_REG || ((cur->noevict >> hr) & 1))
1725               continue;
1726             if(hr!=HOST_CCREG||j<hsn[CCREG]) {
1727               if(cur->regmap[hr]==r) {
1728                 alloc_set(cur, reg, hr);
1729                 return;
1730               }
1731             }
1732           }
1733         }
1734       }
1735     }
1736   }
1737   for(j=10;j>=0;j--)
1738   {
1739     for(r=1;r<=MAXREG;r++)
1740     {
1741       if(hsn[r]==j) {
1742         for(hr=0;hr<HOST_REGS;hr++) {
1743           if (hr == EXCLUDE_REG || ((cur->noevict >> hr) & 1))
1744             continue;
1745           if(cur->regmap[hr]==r) {
1746             alloc_set(cur, reg, hr);
1747             return;
1748           }
1749         }
1750       }
1751     }
1752   }
1753   SysPrintf("This shouldn't happen (evict_alloc_reg)\n");
1754   abort();
1755 }
1756
1757 // Note: registers are allocated clean (unmodified state)
1758 // if you intend to modify the register, you must call dirty_reg().
1759 static void alloc_reg(struct regstat *cur,int i,signed char reg)
1760 {
1761   int r,hr;
1762   int preferred_reg = PREFERRED_REG_FIRST
1763     + reg % (PREFERRED_REG_LAST - PREFERRED_REG_FIRST + 1);
1764   if (reg == CCREG) preferred_reg = HOST_CCREG;
1765   if (reg == PTEMP || reg == FTEMP) preferred_reg = 12;
1766   assert(PREFERRED_REG_FIRST != EXCLUDE_REG && EXCLUDE_REG != HOST_REGS);
1767   assert(reg >= 0);
1768
1769   // Don't allocate unused registers
1770   if((cur->u>>reg)&1) return;
1771
1772   // see if it's already allocated
1773   if ((hr = get_reg(cur->regmap, reg)) >= 0) {
1774     cur->noevict |= 1u << hr;
1775     return;
1776   }
1777
1778   // Keep the same mapping if the register was already allocated in a loop
1779   preferred_reg = loop_reg(i,reg,preferred_reg);
1780
1781   // Try to allocate the preferred register
1782   if (cur->regmap[preferred_reg] == -1) {
1783     alloc_set(cur, reg, preferred_reg);
1784     return;
1785   }
1786   r=cur->regmap[preferred_reg];
1787   assert(r < 64);
1788   if((cur->u>>r)&1) {
1789     alloc_set(cur, reg, preferred_reg);
1790     return;
1791   }
1792
1793   // Clear any unneeded registers
1794   // We try to keep the mapping consistent, if possible, because it
1795   // makes branches easier (especially loops).  So we try to allocate
1796   // first (see above) before removing old mappings.  If this is not
1797   // possible then go ahead and clear out the registers that are no
1798   // longer needed.
1799   for(hr=0;hr<HOST_REGS;hr++)
1800   {
1801     r=cur->regmap[hr];
1802     if(r>=0) {
1803       assert(r < 64);
1804       if((cur->u>>r)&1) {cur->regmap[hr]=-1;break;}
1805     }
1806   }
1807
1808   // Try to allocate any available register, but prefer
1809   // registers that have not been used recently.
1810   if (i > 0) {
1811     for (hr = PREFERRED_REG_FIRST; ; ) {
1812       if (cur->regmap[hr] < 0) {
1813         int oldreg = regs[i-1].regmap[hr];
1814         if (oldreg < 0 || (oldreg != dops[i-1].rs1 && oldreg != dops[i-1].rs2
1815              && oldreg != dops[i-1].rt1 && oldreg != dops[i-1].rt2))
1816         {
1817           alloc_set(cur, reg, hr);
1818           return;
1819         }
1820       }
1821       hr++;
1822       if (hr == EXCLUDE_REG)
1823         hr++;
1824       if (hr == HOST_REGS)
1825         hr = 0;
1826       if (hr == PREFERRED_REG_FIRST)
1827         break;
1828     }
1829   }
1830
1831   // Try to allocate any available register
1832   for (hr = PREFERRED_REG_FIRST; ; ) {
1833     if (cur->regmap[hr] < 0) {
1834       alloc_set(cur, reg, hr);
1835       return;
1836     }
1837     hr++;
1838     if (hr == EXCLUDE_REG)
1839       hr++;
1840     if (hr == HOST_REGS)
1841       hr = 0;
1842     if (hr == PREFERRED_REG_FIRST)
1843       break;
1844   }
1845
1846   // Ok, now we have to evict someone
1847   // Pick a register we hopefully won't need soon
1848   evict_alloc_reg(cur, i, reg, preferred_reg);
1849 }
1850
1851 // Allocate a temporary register.  This is done without regard to
1852 // dirty status or whether the register we request is on the unneeded list
1853 // Note: This will only allocate one register, even if called multiple times
1854 static void alloc_reg_temp(struct regstat *cur,int i,signed char reg)
1855 {
1856   int r,hr;
1857
1858   // see if it's already allocated
1859   for (hr = 0; hr < HOST_REGS; hr++)
1860   {
1861     if (hr != EXCLUDE_REG && cur->regmap[hr] == reg) {
1862       cur->noevict |= 1u << hr;
1863       return;
1864     }
1865   }
1866
1867   // Try to allocate any available register
1868   for(hr=HOST_REGS-1;hr>=0;hr--) {
1869     if(hr!=EXCLUDE_REG&&cur->regmap[hr]==-1) {
1870       alloc_set(cur, reg, hr);
1871       return;
1872     }
1873   }
1874
1875   // Find an unneeded register
1876   for(hr=HOST_REGS-1;hr>=0;hr--)
1877   {
1878     r=cur->regmap[hr];
1879     if(r>=0) {
1880       assert(r < 64);
1881       if((cur->u>>r)&1) {
1882         if(i==0||((unneeded_reg[i-1]>>r)&1)) {
1883           alloc_set(cur, reg, hr);
1884           return;
1885         }
1886       }
1887     }
1888   }
1889
1890   // Ok, now we have to evict someone
1891   // Pick a register we hopefully won't need soon
1892   evict_alloc_reg(cur, i, reg, 0);
1893 }
1894
1895 static void mov_alloc(struct regstat *current,int i)
1896 {
1897   if (dops[i].rs1 == HIREG || dops[i].rs1 == LOREG) {
1898     alloc_cc(current,i); // for stalls
1899     dirty_reg(current,CCREG);
1900   }
1901
1902   // Note: Don't need to actually alloc the source registers
1903   //alloc_reg(current,i,dops[i].rs1);
1904   alloc_reg(current,i,dops[i].rt1);
1905
1906   clear_const(current,dops[i].rs1);
1907   clear_const(current,dops[i].rt1);
1908   dirty_reg(current,dops[i].rt1);
1909 }
1910
1911 static void shiftimm_alloc(struct regstat *current,int i)
1912 {
1913   if(dops[i].opcode2<=0x3) // SLL/SRL/SRA
1914   {
1915     if(dops[i].rt1) {
1916       if(dops[i].rs1&&needed_again(dops[i].rs1,i)) alloc_reg(current,i,dops[i].rs1);
1917       else dops[i].use_lt1=!!dops[i].rs1;
1918       alloc_reg(current,i,dops[i].rt1);
1919       dirty_reg(current,dops[i].rt1);
1920       if(is_const(current,dops[i].rs1)) {
1921         int v=get_const(current,dops[i].rs1);
1922         if(dops[i].opcode2==0x00) set_const(current,dops[i].rt1,v<<cinfo[i].imm);
1923         if(dops[i].opcode2==0x02) set_const(current,dops[i].rt1,(u_int)v>>cinfo[i].imm);
1924         if(dops[i].opcode2==0x03) set_const(current,dops[i].rt1,v>>cinfo[i].imm);
1925       }
1926       else clear_const(current,dops[i].rt1);
1927     }
1928   }
1929   else
1930   {
1931     clear_const(current,dops[i].rs1);
1932     clear_const(current,dops[i].rt1);
1933   }
1934
1935   if(dops[i].opcode2>=0x38&&dops[i].opcode2<=0x3b) // DSLL/DSRL/DSRA
1936   {
1937     assert(0);
1938   }
1939   if(dops[i].opcode2==0x3c) // DSLL32
1940   {
1941     assert(0);
1942   }
1943   if(dops[i].opcode2==0x3e) // DSRL32
1944   {
1945     assert(0);
1946   }
1947   if(dops[i].opcode2==0x3f) // DSRA32
1948   {
1949     assert(0);
1950   }
1951 }
1952
1953 static void shift_alloc(struct regstat *current,int i)
1954 {
1955   if(dops[i].rt1) {
1956       if(dops[i].rs1) alloc_reg(current,i,dops[i].rs1);
1957       if(dops[i].rs2) alloc_reg(current,i,dops[i].rs2);
1958       alloc_reg(current,i,dops[i].rt1);
1959       if(dops[i].rt1==dops[i].rs2) {
1960         alloc_reg_temp(current,i,-1);
1961         cinfo[i].min_free_regs=1;
1962       }
1963     clear_const(current,dops[i].rs1);
1964     clear_const(current,dops[i].rs2);
1965     clear_const(current,dops[i].rt1);
1966     dirty_reg(current,dops[i].rt1);
1967   }
1968 }
1969
1970 static void alu_alloc(struct regstat *current,int i)
1971 {
1972   if(dops[i].opcode2>=0x20&&dops[i].opcode2<=0x23) { // ADD/ADDU/SUB/SUBU
1973     if(dops[i].rt1) {
1974       if(dops[i].rs1&&dops[i].rs2) {
1975         alloc_reg(current,i,dops[i].rs1);
1976         alloc_reg(current,i,dops[i].rs2);
1977       }
1978       else {
1979         if(dops[i].rs1&&needed_again(dops[i].rs1,i)) alloc_reg(current,i,dops[i].rs1);
1980         if(dops[i].rs2&&needed_again(dops[i].rs2,i)) alloc_reg(current,i,dops[i].rs2);
1981       }
1982       alloc_reg(current,i,dops[i].rt1);
1983     }
1984     if (dops[i].may_except) {
1985       alloc_cc_optional(current, i); // for exceptions
1986       alloc_reg_temp(current, i, -1);
1987       cinfo[i].min_free_regs = 1;
1988     }
1989   }
1990   else if(dops[i].opcode2==0x2a||dops[i].opcode2==0x2b) { // SLT/SLTU
1991     if(dops[i].rt1) {
1992       alloc_reg(current,i,dops[i].rs1);
1993       alloc_reg(current,i,dops[i].rs2);
1994       alloc_reg(current,i,dops[i].rt1);
1995     }
1996   }
1997   else if(dops[i].opcode2>=0x24&&dops[i].opcode2<=0x27) { // AND/OR/XOR/NOR
1998     if(dops[i].rt1) {
1999       if(dops[i].rs1&&dops[i].rs2) {
2000         alloc_reg(current,i,dops[i].rs1);
2001         alloc_reg(current,i,dops[i].rs2);
2002       }
2003       else
2004       {
2005         if(dops[i].rs1&&needed_again(dops[i].rs1,i)) alloc_reg(current,i,dops[i].rs1);
2006         if(dops[i].rs2&&needed_again(dops[i].rs2,i)) alloc_reg(current,i,dops[i].rs2);
2007       }
2008       alloc_reg(current,i,dops[i].rt1);
2009     }
2010   }
2011   clear_const(current,dops[i].rs1);
2012   clear_const(current,dops[i].rs2);
2013   clear_const(current,dops[i].rt1);
2014   dirty_reg(current,dops[i].rt1);
2015 }
2016
2017 static void imm16_alloc(struct regstat *current,int i)
2018 {
2019   if(dops[i].rs1&&needed_again(dops[i].rs1,i)) alloc_reg(current,i,dops[i].rs1);
2020   else dops[i].use_lt1=!!dops[i].rs1;
2021   if(dops[i].rt1) alloc_reg(current,i,dops[i].rt1);
2022   if(dops[i].opcode==0x0a||dops[i].opcode==0x0b) { // SLTI/SLTIU
2023     clear_const(current,dops[i].rs1);
2024     clear_const(current,dops[i].rt1);
2025   }
2026   else if(dops[i].opcode>=0x0c&&dops[i].opcode<=0x0e) { // ANDI/ORI/XORI
2027     if(is_const(current,dops[i].rs1)) {
2028       int v=get_const(current,dops[i].rs1);
2029       if(dops[i].opcode==0x0c) set_const(current,dops[i].rt1,v&cinfo[i].imm);
2030       if(dops[i].opcode==0x0d) set_const(current,dops[i].rt1,v|cinfo[i].imm);
2031       if(dops[i].opcode==0x0e) set_const(current,dops[i].rt1,v^cinfo[i].imm);
2032     }
2033     else clear_const(current,dops[i].rt1);
2034   }
2035   else if(dops[i].opcode==0x08||dops[i].opcode==0x09) { // ADDI/ADDIU
2036     if(is_const(current,dops[i].rs1)) {
2037       int v=get_const(current,dops[i].rs1);
2038       set_const(current,dops[i].rt1,v+cinfo[i].imm);
2039     }
2040     else clear_const(current,dops[i].rt1);
2041     if (dops[i].may_except) {
2042       alloc_cc_optional(current, i); // for exceptions
2043       alloc_reg_temp(current, i, -1);
2044       cinfo[i].min_free_regs = 1;
2045     }
2046   }
2047   else {
2048     set_const(current,dops[i].rt1,cinfo[i].imm<<16); // LUI
2049   }
2050   dirty_reg(current,dops[i].rt1);
2051 }
2052
2053 static void load_alloc(struct regstat *current,int i)
2054 {
2055   int need_temp = 0;
2056   clear_const(current,dops[i].rt1);
2057   //if(dops[i].rs1!=dops[i].rt1&&needed_again(dops[i].rs1,i)) clear_const(current,dops[i].rs1); // Does this help or hurt?
2058   if(!dops[i].rs1) current->u&=~1LL; // Allow allocating r0 if it's the source register
2059   if (needed_again(dops[i].rs1, i))
2060     alloc_reg(current, i, dops[i].rs1);
2061   if (ram_offset)
2062     alloc_reg(current, i, ROREG);
2063   if (dops[i].may_except) {
2064     alloc_cc_optional(current, i); // for exceptions
2065     need_temp = 1;
2066   }
2067   if(dops[i].rt1&&!((current->u>>dops[i].rt1)&1)) {
2068     alloc_reg(current,i,dops[i].rt1);
2069     assert(get_reg_w(current->regmap, dops[i].rt1)>=0);
2070     dirty_reg(current,dops[i].rt1);
2071     // LWL/LWR need a temporary register for the old value
2072     if(dops[i].opcode==0x22||dops[i].opcode==0x26)
2073     {
2074       alloc_reg(current,i,FTEMP);
2075       need_temp = 1;
2076     }
2077   }
2078   else
2079   {
2080     // Load to r0 or unneeded register (dummy load)
2081     // but we still need a register to calculate the address
2082     if(dops[i].opcode==0x22||dops[i].opcode==0x26)
2083       alloc_reg(current,i,FTEMP); // LWL/LWR need another temporary
2084     need_temp = 1;
2085   }
2086   if (need_temp) {
2087     alloc_reg_temp(current, i, -1);
2088     cinfo[i].min_free_regs = 1;
2089   }
2090 }
2091
2092 // this may eat up to 7 registers
2093 static void store_alloc(struct regstat *current, int i)
2094 {
2095   clear_const(current,dops[i].rs2);
2096   if(!(dops[i].rs2)) current->u&=~1LL; // Allow allocating r0 if necessary
2097   if(needed_again(dops[i].rs1,i)) alloc_reg(current,i,dops[i].rs1);
2098   alloc_reg(current,i,dops[i].rs2);
2099   if (ram_offset)
2100     alloc_reg(current, i, ROREG);
2101   #if defined(HOST_IMM8)
2102   // On CPUs without 32-bit immediates we need a pointer to invalid_code
2103   alloc_reg(current, i, INVCP);
2104   #endif
2105   if (dops[i].opcode == 0x2a || dops[i].opcode == 0x2e) { // SWL/SWL
2106     alloc_reg(current,i,FTEMP);
2107   }
2108   if (dops[i].may_except)
2109     alloc_cc_optional(current, i); // for exceptions
2110   // We need a temporary register for address generation
2111   alloc_reg_temp(current,i,-1);
2112   cinfo[i].min_free_regs=1;
2113 }
2114
2115 static void c2ls_alloc(struct regstat *current, int i)
2116 {
2117   clear_const(current,dops[i].rt1);
2118   if(needed_again(dops[i].rs1,i)) alloc_reg(current,i,dops[i].rs1);
2119   alloc_reg(current,i,FTEMP);
2120   if (ram_offset)
2121     alloc_reg(current, i, ROREG);
2122   #if defined(HOST_IMM8)
2123   // On CPUs without 32-bit immediates we need a pointer to invalid_code
2124   if (dops[i].opcode == 0x3a) // SWC2
2125     alloc_reg(current,i,INVCP);
2126   #endif
2127   if (dops[i].may_except)
2128     alloc_cc_optional(current, i); // for exceptions
2129   // We need a temporary register for address generation
2130   alloc_reg_temp(current,i,-1);
2131   cinfo[i].min_free_regs=1;
2132 }
2133
2134 #ifndef multdiv_alloc
2135 static void multdiv_alloc(struct regstat *current,int i)
2136 {
2137   //  case 0x18: MULT
2138   //  case 0x19: MULTU
2139   //  case 0x1A: DIV
2140   //  case 0x1B: DIVU
2141   clear_const(current,dops[i].rs1);
2142   clear_const(current,dops[i].rs2);
2143   alloc_cc(current,i); // for stalls
2144   dirty_reg(current,CCREG);
2145   current->u &= ~(1ull << HIREG);
2146   current->u &= ~(1ull << LOREG);
2147   alloc_reg(current, i, HIREG);
2148   alloc_reg(current, i, LOREG);
2149   dirty_reg(current, HIREG);
2150   dirty_reg(current, LOREG);
2151   if ((dops[i].opcode2 & 0x3e) == 0x1a || (dops[i].rs1 && dops[i].rs2)) // div(u)
2152   {
2153     alloc_reg(current, i, dops[i].rs1);
2154     alloc_reg(current, i, dops[i].rs2);
2155   }
2156   // else multiply by zero is zero
2157 }
2158 #endif
2159
2160 static void cop0_alloc(struct regstat *current,int i)
2161 {
2162   if(dops[i].opcode2==0) // MFC0
2163   {
2164     if(dops[i].rt1) {
2165       clear_const(current,dops[i].rt1);
2166       alloc_reg(current,i,dops[i].rt1);
2167       dirty_reg(current,dops[i].rt1);
2168     }
2169   }
2170   else if(dops[i].opcode2==4) // MTC0
2171   {
2172     if (((source[i]>>11)&0x1e) == 12) {
2173       alloc_cc(current, i);
2174       dirty_reg(current, CCREG);
2175     }
2176     if(dops[i].rs1){
2177       clear_const(current,dops[i].rs1);
2178       alloc_reg(current,i,dops[i].rs1);
2179       alloc_all(current,i);
2180     }
2181     else {
2182       alloc_all(current,i); // FIXME: Keep r0
2183       current->u&=~1LL;
2184       alloc_reg(current,i,0);
2185     }
2186     cinfo[i].min_free_regs = HOST_REGS;
2187   }
2188 }
2189
2190 static void rfe_alloc(struct regstat *current, int i)
2191 {
2192   alloc_all(current, i);
2193   cinfo[i].min_free_regs = HOST_REGS;
2194 }
2195
2196 static void cop2_alloc(struct regstat *current,int i)
2197 {
2198   if (dops[i].opcode2 < 3) // MFC2/CFC2
2199   {
2200     alloc_cc(current,i); // for stalls
2201     dirty_reg(current,CCREG);
2202     if(dops[i].rt1){
2203       clear_const(current,dops[i].rt1);
2204       alloc_reg(current,i,dops[i].rt1);
2205       dirty_reg(current,dops[i].rt1);
2206     }
2207   }
2208   else if (dops[i].opcode2 > 3) // MTC2/CTC2
2209   {
2210     if(dops[i].rs1){
2211       clear_const(current,dops[i].rs1);
2212       alloc_reg(current,i,dops[i].rs1);
2213     }
2214     else {
2215       current->u&=~1LL;
2216       alloc_reg(current,i,0);
2217     }
2218   }
2219   alloc_reg_temp(current,i,-1);
2220   cinfo[i].min_free_regs=1;
2221 }
2222
2223 static void c2op_alloc(struct regstat *current,int i)
2224 {
2225   alloc_cc(current,i); // for stalls
2226   dirty_reg(current,CCREG);
2227   alloc_reg_temp(current,i,-1);
2228 }
2229
2230 static void syscall_alloc(struct regstat *current,int i)
2231 {
2232   alloc_cc(current,i);
2233   dirty_reg(current,CCREG);
2234   alloc_all(current,i);
2235   cinfo[i].min_free_regs=HOST_REGS;
2236   current->isconst=0;
2237 }
2238
2239 static void delayslot_alloc(struct regstat *current,int i)
2240 {
2241   switch(dops[i].itype) {
2242     case UJUMP:
2243     case CJUMP:
2244     case SJUMP:
2245     case RJUMP:
2246     case SYSCALL:
2247     case HLECALL:
2248     case IMM16:
2249       imm16_alloc(current,i);
2250       break;
2251     case LOAD:
2252     case LOADLR:
2253       load_alloc(current,i);
2254       break;
2255     case STORE:
2256     case STORELR:
2257       store_alloc(current,i);
2258       break;
2259     case ALU:
2260       alu_alloc(current,i);
2261       break;
2262     case SHIFT:
2263       shift_alloc(current,i);
2264       break;
2265     case MULTDIV:
2266       multdiv_alloc(current,i);
2267       break;
2268     case SHIFTIMM:
2269       shiftimm_alloc(current,i);
2270       break;
2271     case MOV:
2272       mov_alloc(current,i);
2273       break;
2274     case COP0:
2275       cop0_alloc(current,i);
2276       break;
2277     case RFE:
2278       rfe_alloc(current,i);
2279       break;
2280     case COP2:
2281       cop2_alloc(current,i);
2282       break;
2283     case C2LS:
2284       c2ls_alloc(current,i);
2285       break;
2286     case C2OP:
2287       c2op_alloc(current,i);
2288       break;
2289   }
2290 }
2291
2292 static void add_stub(enum stub_type type, void *addr, void *retaddr,
2293   u_int a, uintptr_t b, uintptr_t c, u_int d, u_int e)
2294 {
2295   assert(stubcount < ARRAY_SIZE(stubs));
2296   stubs[stubcount].type = type;
2297   stubs[stubcount].addr = addr;
2298   stubs[stubcount].retaddr = retaddr;
2299   stubs[stubcount].a = a;
2300   stubs[stubcount].b = b;
2301   stubs[stubcount].c = c;
2302   stubs[stubcount].d = d;
2303   stubs[stubcount].e = e;
2304   stubcount++;
2305 }
2306
2307 static void add_stub_r(enum stub_type type, void *addr, void *retaddr,
2308   int i, int addr_reg, const struct regstat *i_regs, int ccadj, u_int reglist)
2309 {
2310   add_stub(type, addr, retaddr, i, addr_reg, (uintptr_t)i_regs, ccadj, reglist);
2311 }
2312
2313 // Write out a single register
2314 static void wb_register(signed char r, const signed char regmap[], u_int dirty)
2315 {
2316   int hr;
2317   for(hr=0;hr<HOST_REGS;hr++) {
2318     if(hr!=EXCLUDE_REG) {
2319       if(regmap[hr]==r) {
2320         if((dirty>>hr)&1) {
2321           assert(regmap[hr]<64);
2322           emit_storereg(r,hr);
2323         }
2324         break;
2325       }
2326     }
2327   }
2328 }
2329
2330 static void wb_valid(signed char pre[],signed char entry[],u_int dirty_pre,u_int dirty,uint64_t u)
2331 {
2332   //if(dirty_pre==dirty) return;
2333   int hr, r;
2334   for (hr = 0; hr < HOST_REGS; hr++) {
2335     r = pre[hr];
2336     if (r < 1 || r > 33 || ((u >> r) & 1))
2337       continue;
2338     if (((dirty_pre & ~dirty) >> hr) & 1)
2339       emit_storereg(r, hr);
2340   }
2341 }
2342
2343 // trashes r2
2344 static void pass_args(int a0, int a1)
2345 {
2346   if(a0==1&&a1==0) {
2347     // must swap
2348     emit_mov(a0,2); emit_mov(a1,1); emit_mov(2,0);
2349   }
2350   else if(a0!=0&&a1==0) {
2351     emit_mov(a1,1);
2352     if (a0>=0) emit_mov(a0,0);
2353   }
2354   else {
2355     if(a0>=0&&a0!=0) emit_mov(a0,0);
2356     if(a1>=0&&a1!=1) emit_mov(a1,1);
2357   }
2358 }
2359
2360 static void alu_assemble(int i, const struct regstat *i_regs, int ccadj_)
2361 {
2362   if(dops[i].opcode2>=0x20&&dops[i].opcode2<=0x23) { // ADD/ADDU/SUB/SUBU
2363     int do_oflow = dops[i].may_except; // ADD/SUB with exceptions enabled
2364     if (dops[i].rt1 || do_oflow) {
2365       int do_exception_check = 0;
2366       signed char s1, s2, t, tmp;
2367       t = get_reg_w(i_regs->regmap, dops[i].rt1);
2368       tmp = get_reg_temp(i_regs->regmap);
2369       if (do_oflow)
2370         assert(tmp >= 0);
2371       if (t < 0 && do_oflow)
2372         t = tmp;
2373       if (t >= 0) {
2374         s1 = get_reg(i_regs->regmap, dops[i].rs1);
2375         s2 = get_reg(i_regs->regmap, dops[i].rs2);
2376         if (dops[i].rs1 && dops[i].rs2) {
2377           assert(s1>=0);
2378           assert(s2>=0);
2379           if (dops[i].opcode2 & 2) {
2380             if (do_oflow) {
2381               emit_subs(s1, s2, tmp);
2382               do_exception_check = 1;
2383             }
2384             else
2385               emit_sub(s1,s2,t);
2386           }
2387           else {
2388             if (do_oflow) {
2389               emit_adds(s1, s2, tmp);
2390               do_exception_check = 1;
2391             }
2392             else
2393               emit_add(s1,s2,t);
2394           }
2395         }
2396         else if(dops[i].rs1) {
2397           if(s1>=0) emit_mov(s1,t);
2398           else emit_loadreg(dops[i].rs1,t);
2399         }
2400         else if(dops[i].rs2) {
2401           if (s2 < 0) {
2402             emit_loadreg(dops[i].rs2, t);
2403             s2 = t;
2404           }
2405           if (dops[i].opcode2 & 2) {
2406             if (do_oflow) {
2407               emit_negs(s2, tmp);
2408               do_exception_check = 1;
2409             }
2410             else
2411               emit_neg(s2, t);
2412           }
2413           else if (s2 != t)
2414             emit_mov(s2, t);
2415         }
2416         else
2417           emit_zeroreg(t);
2418       }
2419       if (do_exception_check) {
2420         void *jaddr = out;
2421         emit_jo(0);
2422         if (t >= 0 && tmp != t)
2423           emit_mov(tmp, t);
2424         add_stub_r(OVERFLOW_STUB, jaddr, out, i, 0, i_regs, ccadj_, 0);
2425       }
2426     }
2427   }
2428   else if(dops[i].opcode2==0x2a||dops[i].opcode2==0x2b) { // SLT/SLTU
2429     if(dops[i].rt1) {
2430       signed char s1l,s2l,t;
2431       {
2432         t=get_reg_w(i_regs->regmap, dops[i].rt1);
2433         //assert(t>=0);
2434         if(t>=0) {
2435           s1l=get_reg(i_regs->regmap,dops[i].rs1);
2436           s2l=get_reg(i_regs->regmap,dops[i].rs2);
2437           if(dops[i].rs2==0) // rx<r0
2438           {
2439             if(dops[i].opcode2==0x2a&&dops[i].rs1!=0) { // SLT
2440               assert(s1l>=0);
2441               emit_shrimm(s1l,31,t);
2442             }
2443             else // SLTU (unsigned can not be less than zero, 0<0)
2444               emit_zeroreg(t);
2445           }
2446           else if(dops[i].rs1==0) // r0<rx
2447           {
2448             assert(s2l>=0);
2449             if(dops[i].opcode2==0x2a) // SLT
2450               emit_set_gz32(s2l,t);
2451             else // SLTU (set if not zero)
2452               emit_set_nz32(s2l,t);
2453           }
2454           else{
2455             assert(s1l>=0);assert(s2l>=0);
2456             if(dops[i].opcode2==0x2a) // SLT
2457               emit_set_if_less32(s1l,s2l,t);
2458             else // SLTU
2459               emit_set_if_carry32(s1l,s2l,t);
2460           }
2461         }
2462       }
2463     }
2464   }
2465   else if(dops[i].opcode2>=0x24&&dops[i].opcode2<=0x27) { // AND/OR/XOR/NOR
2466     if(dops[i].rt1) {
2467       signed char s1l,s2l,tl;
2468       tl=get_reg_w(i_regs->regmap, dops[i].rt1);
2469       {
2470         if(tl>=0) {
2471           s1l=get_reg(i_regs->regmap,dops[i].rs1);
2472           s2l=get_reg(i_regs->regmap,dops[i].rs2);
2473           if(dops[i].rs1&&dops[i].rs2) {
2474             assert(s1l>=0);
2475             assert(s2l>=0);
2476             if(dops[i].opcode2==0x24) { // AND
2477               emit_and(s1l,s2l,tl);
2478             } else
2479             if(dops[i].opcode2==0x25) { // OR
2480               emit_or(s1l,s2l,tl);
2481             } else
2482             if(dops[i].opcode2==0x26) { // XOR
2483               emit_xor(s1l,s2l,tl);
2484             } else
2485             if(dops[i].opcode2==0x27) { // NOR
2486               emit_or(s1l,s2l,tl);
2487               emit_not(tl,tl);
2488             }
2489           }
2490           else
2491           {
2492             if(dops[i].opcode2==0x24) { // AND
2493               emit_zeroreg(tl);
2494             } else
2495             if(dops[i].opcode2==0x25||dops[i].opcode2==0x26) { // OR/XOR
2496               if(dops[i].rs1){
2497                 if(s1l>=0) emit_mov(s1l,tl);
2498                 else emit_loadreg(dops[i].rs1,tl); // CHECK: regmap_entry?
2499               }
2500               else
2501               if(dops[i].rs2){
2502                 if(s2l>=0) emit_mov(s2l,tl);
2503                 else emit_loadreg(dops[i].rs2,tl); // CHECK: regmap_entry?
2504               }
2505               else emit_zeroreg(tl);
2506             } else
2507             if(dops[i].opcode2==0x27) { // NOR
2508               if(dops[i].rs1){
2509                 if(s1l>=0) emit_not(s1l,tl);
2510                 else {
2511                   emit_loadreg(dops[i].rs1,tl);
2512                   emit_not(tl,tl);
2513                 }
2514               }
2515               else
2516               if(dops[i].rs2){
2517                 if(s2l>=0) emit_not(s2l,tl);
2518                 else {
2519                   emit_loadreg(dops[i].rs2,tl);
2520                   emit_not(tl,tl);
2521                 }
2522               }
2523               else emit_movimm(-1,tl);
2524             }
2525           }
2526         }
2527       }
2528     }
2529   }
2530 }
2531
2532 static void imm16_assemble(int i, const struct regstat *i_regs, int ccadj_)
2533 {
2534   if (dops[i].opcode==0x0f) { // LUI
2535     if(dops[i].rt1) {
2536       signed char t;
2537       t=get_reg_w(i_regs->regmap, dops[i].rt1);
2538       //assert(t>=0);
2539       if(t>=0) {
2540         if(!((i_regs->isconst>>t)&1))
2541           emit_movimm(cinfo[i].imm<<16,t);
2542       }
2543     }
2544   }
2545   if(dops[i].opcode==0x08||dops[i].opcode==0x09) { // ADDI/ADDIU
2546     int is_addi = dops[i].may_except;
2547     if (dops[i].rt1 || is_addi) {
2548       signed char s, t, tmp;
2549       t=get_reg_w(i_regs->regmap, dops[i].rt1);
2550       s=get_reg(i_regs->regmap,dops[i].rs1);
2551       if(dops[i].rs1) {
2552         tmp = get_reg_temp(i_regs->regmap);
2553         if (is_addi) {
2554           assert(tmp >= 0);
2555           if (t < 0) t = tmp;
2556         }
2557         if(t>=0) {
2558           if(!((i_regs->isconst>>t)&1)) {
2559             int sum, do_exception_check = 0;
2560             if (s < 0) {
2561               if(i_regs->regmap_entry[t]!=dops[i].rs1) emit_loadreg(dops[i].rs1,t);
2562               if (is_addi) {
2563                 emit_addimm_and_set_flags3(t, cinfo[i].imm, tmp);
2564                 do_exception_check = 1;
2565               }
2566               else
2567                 emit_addimm(t, cinfo[i].imm, t);
2568             } else {
2569               if (!((i_regs->wasconst >> s) & 1)) {
2570                 if (is_addi) {
2571                   emit_addimm_and_set_flags3(s, cinfo[i].imm, tmp);
2572                   do_exception_check = 1;
2573                 }
2574                 else
2575                   emit_addimm(s, cinfo[i].imm, t);
2576               }
2577               else {
2578                 int oflow = add_overflow(constmap[i][s], cinfo[i].imm, sum);
2579                 if (is_addi && oflow)
2580                   do_exception_check = 2;
2581                 else
2582                   emit_movimm(sum, t);
2583               }
2584             }
2585             if (do_exception_check) {
2586               void *jaddr = out;
2587               if (do_exception_check == 2)
2588                 emit_jmp(0);
2589               else {
2590                 emit_jo(0);
2591                 if (tmp != t)
2592                   emit_mov(tmp, t);
2593               }
2594               add_stub_r(OVERFLOW_STUB, jaddr, out, i, 0, i_regs, ccadj_, 0);
2595             }
2596           }
2597         }
2598       } else {
2599         if(t>=0) {
2600           if(!((i_regs->isconst>>t)&1))
2601             emit_movimm(cinfo[i].imm,t);
2602         }
2603       }
2604     }
2605   }
2606   else if(dops[i].opcode==0x0a||dops[i].opcode==0x0b) { // SLTI/SLTIU
2607     if(dops[i].rt1) {
2608       //assert(dops[i].rs1!=0); // r0 might be valid, but it's probably a bug
2609       signed char sl,t;
2610       t=get_reg_w(i_regs->regmap, dops[i].rt1);
2611       sl=get_reg(i_regs->regmap,dops[i].rs1);
2612       //assert(t>=0);
2613       if(t>=0) {
2614         if(dops[i].rs1>0) {
2615             if(dops[i].opcode==0x0a) { // SLTI
2616               if(sl<0) {
2617                 if(i_regs->regmap_entry[t]!=dops[i].rs1) emit_loadreg(dops[i].rs1,t);
2618                 emit_slti32(t,cinfo[i].imm,t);
2619               }else{
2620                 emit_slti32(sl,cinfo[i].imm,t);
2621               }
2622             }
2623             else { // SLTIU
2624               if(sl<0) {
2625                 if(i_regs->regmap_entry[t]!=dops[i].rs1) emit_loadreg(dops[i].rs1,t);
2626                 emit_sltiu32(t,cinfo[i].imm,t);
2627               }else{
2628                 emit_sltiu32(sl,cinfo[i].imm,t);
2629               }
2630             }
2631         }else{
2632           // SLTI(U) with r0 is just stupid,
2633           // nonetheless examples can be found
2634           if(dops[i].opcode==0x0a) // SLTI
2635             if(0<cinfo[i].imm) emit_movimm(1,t);
2636             else emit_zeroreg(t);
2637           else // SLTIU
2638           {
2639             if(cinfo[i].imm) emit_movimm(1,t);
2640             else emit_zeroreg(t);
2641           }
2642         }
2643       }
2644     }
2645   }
2646   else if(dops[i].opcode>=0x0c&&dops[i].opcode<=0x0e) { // ANDI/ORI/XORI
2647     if(dops[i].rt1) {
2648       signed char sl,tl;
2649       tl=get_reg_w(i_regs->regmap, dops[i].rt1);
2650       sl=get_reg(i_regs->regmap,dops[i].rs1);
2651       if(tl>=0 && !((i_regs->isconst>>tl)&1)) {
2652         if(dops[i].opcode==0x0c) //ANDI
2653         {
2654           if(dops[i].rs1) {
2655             if(sl<0) {
2656               if(i_regs->regmap_entry[tl]!=dops[i].rs1) emit_loadreg(dops[i].rs1,tl);
2657               emit_andimm(tl,cinfo[i].imm,tl);
2658             }else{
2659               if(!((i_regs->wasconst>>sl)&1))
2660                 emit_andimm(sl,cinfo[i].imm,tl);
2661               else
2662                 emit_movimm(constmap[i][sl]&cinfo[i].imm,tl);
2663             }
2664           }
2665           else
2666             emit_zeroreg(tl);
2667         }
2668         else
2669         {
2670           if(dops[i].rs1) {
2671             if(sl<0) {
2672               if(i_regs->regmap_entry[tl]!=dops[i].rs1) emit_loadreg(dops[i].rs1,tl);
2673             }
2674             if(dops[i].opcode==0x0d) { // ORI
2675               if(sl<0) {
2676                 emit_orimm(tl,cinfo[i].imm,tl);
2677               }else{
2678                 if(!((i_regs->wasconst>>sl)&1))
2679                   emit_orimm(sl,cinfo[i].imm,tl);
2680                 else
2681                   emit_movimm(constmap[i][sl]|cinfo[i].imm,tl);
2682               }
2683             }
2684             if(dops[i].opcode==0x0e) { // XORI
2685               if(sl<0) {
2686                 emit_xorimm(tl,cinfo[i].imm,tl);
2687               }else{
2688                 if(!((i_regs->wasconst>>sl)&1))
2689                   emit_xorimm(sl,cinfo[i].imm,tl);
2690                 else
2691                   emit_movimm(constmap[i][sl]^cinfo[i].imm,tl);
2692               }
2693             }
2694           }
2695           else {
2696             emit_movimm(cinfo[i].imm,tl);
2697           }
2698         }
2699       }
2700     }
2701   }
2702 }
2703
2704 static void shiftimm_assemble(int i, const struct regstat *i_regs)
2705 {
2706   if(dops[i].opcode2<=0x3) // SLL/SRL/SRA
2707   {
2708     if(dops[i].rt1) {
2709       signed char s,t;
2710       t=get_reg_w(i_regs->regmap, dops[i].rt1);
2711       s=get_reg(i_regs->regmap,dops[i].rs1);
2712       //assert(t>=0);
2713       if(t>=0&&!((i_regs->isconst>>t)&1)){
2714         if(dops[i].rs1==0)
2715         {
2716           emit_zeroreg(t);
2717         }
2718         else
2719         {
2720           if(s<0&&i_regs->regmap_entry[t]!=dops[i].rs1) emit_loadreg(dops[i].rs1,t);
2721           if(cinfo[i].imm) {
2722             if(dops[i].opcode2==0) // SLL
2723             {
2724               emit_shlimm(s<0?t:s,cinfo[i].imm,t);
2725             }
2726             if(dops[i].opcode2==2) // SRL
2727             {
2728               emit_shrimm(s<0?t:s,cinfo[i].imm,t);
2729             }
2730             if(dops[i].opcode2==3) // SRA
2731             {
2732               emit_sarimm(s<0?t:s,cinfo[i].imm,t);
2733             }
2734           }else{
2735             // Shift by zero
2736             if(s>=0 && s!=t) emit_mov(s,t);
2737           }
2738         }
2739       }
2740       //emit_storereg(dops[i].rt1,t); //DEBUG
2741     }
2742   }
2743   if(dops[i].opcode2>=0x38&&dops[i].opcode2<=0x3b) // DSLL/DSRL/DSRA
2744   {
2745     assert(0);
2746   }
2747   if(dops[i].opcode2==0x3c) // DSLL32
2748   {
2749     assert(0);
2750   }
2751   if(dops[i].opcode2==0x3e) // DSRL32
2752   {
2753     assert(0);
2754   }
2755   if(dops[i].opcode2==0x3f) // DSRA32
2756   {
2757     assert(0);
2758   }
2759 }
2760
2761 #ifndef shift_assemble
2762 static void shift_assemble(int i, const struct regstat *i_regs)
2763 {
2764   signed char s,t,shift;
2765   if (dops[i].rt1 == 0)
2766     return;
2767   assert(dops[i].opcode2<=0x07); // SLLV/SRLV/SRAV
2768   t = get_reg(i_regs->regmap, dops[i].rt1);
2769   s = get_reg(i_regs->regmap, dops[i].rs1);
2770   shift = get_reg(i_regs->regmap, dops[i].rs2);
2771   if (t < 0)
2772     return;
2773
2774   if(dops[i].rs1==0)
2775     emit_zeroreg(t);
2776   else if(dops[i].rs2==0) {
2777     assert(s>=0);
2778     if(s!=t) emit_mov(s,t);
2779   }
2780   else {
2781     host_tempreg_acquire();
2782     emit_andimm(shift,31,HOST_TEMPREG);
2783     switch(dops[i].opcode2) {
2784     case 4: // SLLV
2785       emit_shl(s,HOST_TEMPREG,t);
2786       break;
2787     case 6: // SRLV
2788       emit_shr(s,HOST_TEMPREG,t);
2789       break;
2790     case 7: // SRAV
2791       emit_sar(s,HOST_TEMPREG,t);
2792       break;
2793     default:
2794       assert(0);
2795     }
2796     host_tempreg_release();
2797   }
2798 }
2799
2800 #endif
2801
2802 enum {
2803   MTYPE_8000 = 0,
2804   MTYPE_8020,
2805   MTYPE_0000,
2806   MTYPE_A000,
2807   MTYPE_1F80,
2808 };
2809
2810 static int get_ptr_mem_type(u_int a)
2811 {
2812   if(a < 0x00200000) {
2813     if(a<0x1000&&((start>>20)==0xbfc||(start>>24)==0xa0))
2814       // return wrong, must use memhandler for BIOS self-test to pass
2815       // 007 does similar stuff from a00 mirror, weird stuff
2816       return MTYPE_8000;
2817     return MTYPE_0000;
2818   }
2819   if(0x1f800000 <= a && a < 0x1f801000)
2820     return MTYPE_1F80;
2821   if(0x80200000 <= a && a < 0x80800000)
2822     return MTYPE_8020;
2823   if(0xa0000000 <= a && a < 0xa0200000)
2824     return MTYPE_A000;
2825   return MTYPE_8000;
2826 }
2827
2828 static int get_ro_reg(const struct regstat *i_regs, int host_tempreg_free)
2829 {
2830   int r = get_reg(i_regs->regmap, ROREG);
2831   if (r < 0 && host_tempreg_free) {
2832     host_tempreg_acquire();
2833     emit_loadreg(ROREG, r = HOST_TEMPREG);
2834   }
2835   if (r < 0)
2836     abort();
2837   return r;
2838 }
2839
2840 static void *emit_fastpath_cmp_jump(int i, const struct regstat *i_regs,
2841   int addr, int *offset_reg, int *addr_reg_override, int ccadj_)
2842 {
2843   void *jaddr = NULL;
2844   int type = 0;
2845   int mr = dops[i].rs1;
2846   assert(addr >= 0);
2847   *offset_reg = -1;
2848   if(((smrv_strong|smrv_weak)>>mr)&1) {
2849     type=get_ptr_mem_type(smrv[mr]);
2850     //printf("set %08x @%08x r%d %d\n", smrv[mr], start+i*4, mr, type);
2851   }
2852   else {
2853     // use the mirror we are running on
2854     type=get_ptr_mem_type(start);
2855     //printf("set nospec   @%08x r%d %d\n", start+i*4, mr, type);
2856   }
2857
2858   if (dops[i].may_except) {
2859     // alignment check
2860     u_int op = dops[i].opcode;
2861     int mask = ((op & 0x37) == 0x21 || op == 0x25) ? 1 : 3; // LH/SH/LHU
2862     void *jaddr2;
2863     emit_testimm(addr, mask);
2864     jaddr2 = out;
2865     emit_jne(0);
2866     add_stub_r(ALIGNMENT_STUB, jaddr2, out, i, addr, i_regs, ccadj_, 0);
2867   }
2868
2869   if(type==MTYPE_8020) { // RAM 80200000+ mirror
2870     host_tempreg_acquire();
2871     emit_andimm(addr,~0x00e00000,HOST_TEMPREG);
2872     addr=*addr_reg_override=HOST_TEMPREG;
2873     type=0;
2874   }
2875   else if(type==MTYPE_0000) { // RAM 0 mirror
2876     host_tempreg_acquire();
2877     emit_orimm(addr,0x80000000,HOST_TEMPREG);
2878     addr=*addr_reg_override=HOST_TEMPREG;
2879     type=0;
2880   }
2881   else if(type==MTYPE_A000) { // RAM A mirror
2882     host_tempreg_acquire();
2883     emit_andimm(addr,~0x20000000,HOST_TEMPREG);
2884     addr=*addr_reg_override=HOST_TEMPREG;
2885     type=0;
2886   }
2887   else if(type==MTYPE_1F80) { // scratchpad
2888     if (psxH == (void *)0x1f800000) {
2889       host_tempreg_acquire();
2890       emit_xorimm(addr,0x1f800000,HOST_TEMPREG);
2891       emit_cmpimm(HOST_TEMPREG,0x1000);
2892       host_tempreg_release();
2893       jaddr=out;
2894       emit_jc(0);
2895     }
2896     else {
2897       // do the usual RAM check, jump will go to the right handler
2898       type=0;
2899     }
2900   }
2901
2902   if (type == 0) // need ram check
2903   {
2904     emit_cmpimm(addr,RAM_SIZE);
2905     jaddr = out;
2906     #ifdef CORTEX_A8_BRANCH_PREDICTION_HACK
2907     // Hint to branch predictor that the branch is unlikely to be taken
2908     if (dops[i].rs1 >= 28)
2909       emit_jno_unlikely(0);
2910     else
2911     #endif
2912       emit_jno(0);
2913     if (ram_offset != 0)
2914       *offset_reg = get_ro_reg(i_regs, 0);
2915   }
2916
2917   return jaddr;
2918 }
2919
2920 // return memhandler, or get directly accessable address and return 0
2921 static void *get_direct_memhandler(void *table, u_int addr,
2922   enum stub_type type, uintptr_t *addr_host)
2923 {
2924   uintptr_t msb = 1ull << (sizeof(uintptr_t)*8 - 1);
2925   uintptr_t l1, l2 = 0;
2926   l1 = ((uintptr_t *)table)[addr>>12];
2927   if (!(l1 & msb)) {
2928     uintptr_t v = l1 << 1;
2929     *addr_host = v + addr;
2930     return NULL;
2931   }
2932   else {
2933     l1 <<= 1;
2934     if (type == LOADB_STUB || type == LOADBU_STUB || type == STOREB_STUB)
2935       l2 = ((uintptr_t *)l1)[0x1000/4 + 0x1000/2 + (addr&0xfff)];
2936     else if (type == LOADH_STUB || type == LOADHU_STUB || type == STOREH_STUB)
2937       l2 = ((uintptr_t *)l1)[0x1000/4 + (addr&0xfff)/2];
2938     else
2939       l2 = ((uintptr_t *)l1)[(addr&0xfff)/4];
2940     if (!(l2 & msb)) {
2941       uintptr_t v = l2 << 1;
2942       *addr_host = v + (addr&0xfff);
2943       return NULL;
2944     }
2945     return (void *)(l2 << 1);
2946   }
2947 }
2948
2949 static u_int get_host_reglist(const signed char *regmap)
2950 {
2951   u_int reglist = 0, hr;
2952   for (hr = 0; hr < HOST_REGS; hr++) {
2953     if (hr != EXCLUDE_REG && regmap[hr] >= 0)
2954       reglist |= 1 << hr;
2955   }
2956   return reglist;
2957 }
2958
2959 static u_int reglist_exclude(u_int reglist, int r1, int r2)
2960 {
2961   if (r1 >= 0)
2962     reglist &= ~(1u << r1);
2963   if (r2 >= 0)
2964     reglist &= ~(1u << r2);
2965   return reglist;
2966 }
2967
2968 // find a temp caller-saved register not in reglist (so assumed to be free)
2969 static int reglist_find_free(u_int reglist)
2970 {
2971   u_int free_regs = ~reglist & CALLER_SAVE_REGS;
2972   if (free_regs == 0)
2973     return -1;
2974   return __builtin_ctz(free_regs);
2975 }
2976
2977 static void do_load_word(int a, int rt, int offset_reg)
2978 {
2979   if (offset_reg >= 0)
2980     emit_ldr_dualindexed(offset_reg, a, rt);
2981   else
2982     emit_readword_indexed(0, a, rt);
2983 }
2984
2985 static void do_store_word(int a, int ofs, int rt, int offset_reg, int preseve_a)
2986 {
2987   if (offset_reg < 0) {
2988     emit_writeword_indexed(rt, ofs, a);
2989     return;
2990   }
2991   if (ofs != 0)
2992     emit_addimm(a, ofs, a);
2993   emit_str_dualindexed(offset_reg, a, rt);
2994   if (ofs != 0 && preseve_a)
2995     emit_addimm(a, -ofs, a);
2996 }
2997
2998 static void do_store_hword(int a, int ofs, int rt, int offset_reg, int preseve_a)
2999 {
3000   if (offset_reg < 0) {
3001     emit_writehword_indexed(rt, ofs, a);
3002     return;
3003   }
3004   if (ofs != 0)
3005     emit_addimm(a, ofs, a);
3006   emit_strh_dualindexed(offset_reg, a, rt);
3007   if (ofs != 0 && preseve_a)
3008     emit_addimm(a, -ofs, a);
3009 }
3010
3011 static void do_store_byte(int a, int rt, int offset_reg)
3012 {
3013   if (offset_reg >= 0)
3014     emit_strb_dualindexed(offset_reg, a, rt);
3015   else
3016     emit_writebyte_indexed(rt, 0, a);
3017 }
3018
3019 static void load_assemble(int i, const struct regstat *i_regs, int ccadj_)
3020 {
3021   int addr = cinfo[i].addr;
3022   int s,tl;
3023   int offset;
3024   void *jaddr=0;
3025   int memtarget=0,c=0;
3026   int offset_reg = -1;
3027   int fastio_reg_override = -1;
3028   u_int reglist=get_host_reglist(i_regs->regmap);
3029   tl=get_reg_w(i_regs->regmap, dops[i].rt1);
3030   s=get_reg(i_regs->regmap,dops[i].rs1);
3031   offset=cinfo[i].imm;
3032   if(i_regs->regmap[HOST_CCREG]==CCREG) reglist&=~(1<<HOST_CCREG);
3033   if(s>=0) {
3034     c=(i_regs->wasconst>>s)&1;
3035     if (c) {
3036       memtarget=((signed int)(constmap[i][s]+offset))<(signed int)0x80000000+RAM_SIZE;
3037     }
3038   }
3039   //printf("load_assemble: c=%d\n",c);
3040   //if(c) printf("load_assemble: const=%lx\n",(long)constmap[i][s]+offset);
3041   if(tl<0 && ((!c||(((u_int)constmap[i][s]+offset)>>16)==0x1f80) || dops[i].rt1==0)) {
3042       // could be FIFO, must perform the read
3043       // ||dummy read
3044       assem_debug("(forced read)\n");
3045       tl = get_reg_temp(i_regs->regmap); // may be == addr
3046       assert(tl>=0);
3047   }
3048   assert(addr >= 0);
3049  if(tl>=0) {
3050   //printf("load_assemble: c=%d\n",c);
3051   //if(c) printf("load_assemble: const=%lx\n",(long)constmap[i][s]+offset);
3052   reglist&=~(1<<tl);
3053   if(!c) {
3054     #ifdef R29_HACK
3055     // Strmnnrmn's speed hack
3056     if(dops[i].rs1!=29||start<0x80001000||start>=0x80000000+RAM_SIZE)
3057     #endif
3058     {
3059       jaddr = emit_fastpath_cmp_jump(i, i_regs, addr,
3060                 &offset_reg, &fastio_reg_override, ccadj_);
3061     }
3062   }
3063   else if (ram_offset && memtarget) {
3064     offset_reg = get_ro_reg(i_regs, 0);
3065   }
3066   int dummy=(dops[i].rt1==0)||(tl!=get_reg_w(i_regs->regmap, dops[i].rt1)); // ignore loads to r0 and unneeded reg
3067   switch (dops[i].opcode) {
3068   case 0x20: // LB
3069     if(!c||memtarget) {
3070       if(!dummy) {
3071         int a = addr;
3072         if (fastio_reg_override >= 0)
3073           a = fastio_reg_override;
3074
3075         if (offset_reg >= 0)
3076           emit_ldrsb_dualindexed(offset_reg, a, tl);
3077         else
3078           emit_movsbl_indexed(0, a, tl);
3079       }
3080       if(jaddr)
3081         add_stub_r(LOADB_STUB,jaddr,out,i,addr,i_regs,ccadj_,reglist);
3082     }
3083     else
3084       inline_readstub(LOADB_STUB,i,constmap[i][s]+offset,i_regs->regmap,dops[i].rt1,ccadj_,reglist);
3085     break;
3086   case 0x21: // LH
3087     if(!c||memtarget) {
3088       if(!dummy) {
3089         int a = addr;
3090         if (fastio_reg_override >= 0)
3091           a = fastio_reg_override;
3092         if (offset_reg >= 0)
3093           emit_ldrsh_dualindexed(offset_reg, a, tl);
3094         else
3095           emit_movswl_indexed(0, a, tl);
3096       }
3097       if(jaddr)
3098         add_stub_r(LOADH_STUB,jaddr,out,i,addr,i_regs,ccadj_,reglist);
3099     }
3100     else
3101       inline_readstub(LOADH_STUB,i,constmap[i][s]+offset,i_regs->regmap,dops[i].rt1,ccadj_,reglist);
3102     break;
3103   case 0x23: // LW
3104     if(!c||memtarget) {
3105       if(!dummy) {
3106         int a = addr;
3107         if (fastio_reg_override >= 0)
3108           a = fastio_reg_override;
3109         do_load_word(a, tl, offset_reg);
3110       }
3111       if(jaddr)
3112         add_stub_r(LOADW_STUB,jaddr,out,i,addr,i_regs,ccadj_,reglist);
3113     }
3114     else
3115       inline_readstub(LOADW_STUB,i,constmap[i][s]+offset,i_regs->regmap,dops[i].rt1,ccadj_,reglist);
3116     break;
3117   case 0x24: // LBU
3118     if(!c||memtarget) {
3119       if(!dummy) {
3120         int a = addr;
3121         if (fastio_reg_override >= 0)
3122           a = fastio_reg_override;
3123
3124         if (offset_reg >= 0)
3125           emit_ldrb_dualindexed(offset_reg, a, tl);
3126         else
3127           emit_movzbl_indexed(0, a, tl);
3128       }
3129       if(jaddr)
3130         add_stub_r(LOADBU_STUB,jaddr,out,i,addr,i_regs,ccadj_,reglist);
3131     }
3132     else
3133       inline_readstub(LOADBU_STUB,i,constmap[i][s]+offset,i_regs->regmap,dops[i].rt1,ccadj_,reglist);
3134     break;
3135   case 0x25: // LHU
3136     if(!c||memtarget) {
3137       if(!dummy) {
3138         int a = addr;
3139         if (fastio_reg_override >= 0)
3140           a = fastio_reg_override;
3141         if (offset_reg >= 0)
3142           emit_ldrh_dualindexed(offset_reg, a, tl);
3143         else
3144           emit_movzwl_indexed(0, a, tl);
3145       }
3146       if(jaddr)
3147         add_stub_r(LOADHU_STUB,jaddr,out,i,addr,i_regs,ccadj_,reglist);
3148     }
3149     else
3150       inline_readstub(LOADHU_STUB,i,constmap[i][s]+offset,i_regs->regmap,dops[i].rt1,ccadj_,reglist);
3151     break;
3152   default:
3153     assert(0);
3154   }
3155  } // tl >= 0
3156  if (fastio_reg_override == HOST_TEMPREG || offset_reg == HOST_TEMPREG)
3157    host_tempreg_release();
3158 }
3159
3160 #ifndef loadlr_assemble
3161 static void loadlr_assemble(int i, const struct regstat *i_regs, int ccadj_)
3162 {
3163   int addr = cinfo[i].addr;
3164   int s,tl,temp,temp2;
3165   int offset;
3166   void *jaddr=0;
3167   int memtarget=0,c=0;
3168   int offset_reg = -1;
3169   int fastio_reg_override = -1;
3170   u_int reglist=get_host_reglist(i_regs->regmap);
3171   tl=get_reg_w(i_regs->regmap, dops[i].rt1);
3172   s=get_reg(i_regs->regmap,dops[i].rs1);
3173   temp=get_reg_temp(i_regs->regmap);
3174   temp2=get_reg(i_regs->regmap,FTEMP);
3175   offset=cinfo[i].imm;
3176   reglist|=1<<temp;
3177   assert(addr >= 0);
3178   if(s>=0) {
3179     c=(i_regs->wasconst>>s)&1;
3180     if(c) {
3181       memtarget=((signed int)(constmap[i][s]+offset))<(signed int)0x80000000+RAM_SIZE;
3182     }
3183   }
3184   if(!c) {
3185     emit_shlimm(addr,3,temp);
3186     if (dops[i].opcode==0x22||dops[i].opcode==0x26) {
3187       emit_andimm(addr,0xFFFFFFFC,temp2); // LWL/LWR
3188     }else{
3189       emit_andimm(addr,0xFFFFFFF8,temp2); // LDL/LDR
3190     }
3191     jaddr = emit_fastpath_cmp_jump(i, i_regs, temp2,
3192               &offset_reg, &fastio_reg_override, ccadj_);
3193   }
3194   else {
3195     if (ram_offset && memtarget) {
3196       offset_reg = get_ro_reg(i_regs, 0);
3197     }
3198     if (dops[i].opcode==0x22||dops[i].opcode==0x26) {
3199       emit_movimm(((constmap[i][s]+offset)<<3)&24,temp); // LWL/LWR
3200     }else{
3201       emit_movimm(((constmap[i][s]+offset)<<3)&56,temp); // LDL/LDR
3202     }
3203   }
3204   if (dops[i].opcode==0x22||dops[i].opcode==0x26) { // LWL/LWR
3205     if(!c||memtarget) {
3206       int a = temp2;
3207       if (fastio_reg_override >= 0)
3208         a = fastio_reg_override;
3209       do_load_word(a, temp2, offset_reg);
3210       if (fastio_reg_override == HOST_TEMPREG || offset_reg == HOST_TEMPREG)
3211         host_tempreg_release();
3212       if(jaddr) add_stub_r(LOADW_STUB,jaddr,out,i,temp2,i_regs,ccadj_,reglist);
3213     }
3214     else
3215       inline_readstub(LOADW_STUB,i,(constmap[i][s]+offset)&0xFFFFFFFC,i_regs->regmap,FTEMP,ccadj_,reglist);
3216     if(dops[i].rt1) {
3217       assert(tl>=0);
3218       emit_andimm(temp,24,temp);
3219       if (dops[i].opcode==0x22) // LWL
3220         emit_xorimm(temp,24,temp);
3221       host_tempreg_acquire();
3222       emit_movimm(-1,HOST_TEMPREG);
3223       if (dops[i].opcode==0x26) {
3224         emit_shr(temp2,temp,temp2);
3225         emit_bic_lsr(tl,HOST_TEMPREG,temp,tl);
3226       }else{
3227         emit_shl(temp2,temp,temp2);
3228         emit_bic_lsl(tl,HOST_TEMPREG,temp,tl);
3229       }
3230       host_tempreg_release();
3231       emit_or(temp2,tl,tl);
3232     }
3233     //emit_storereg(dops[i].rt1,tl); // DEBUG
3234   }
3235   if (dops[i].opcode==0x1A||dops[i].opcode==0x1B) { // LDL/LDR
3236     assert(0);
3237   }
3238 }
3239 #endif
3240
3241 static void do_invstub(int n)
3242 {
3243   literal_pool(20);
3244   assem_debug("do_invstub %x\n", start + stubs[n].e*4);
3245   u_int reglist = stubs[n].a;
3246   u_int addrr = stubs[n].b;
3247   int ofs_start = stubs[n].c;
3248   int ofs_end = stubs[n].d;
3249   int len = ofs_end - ofs_start;
3250   u_int rightr = 0;
3251
3252   set_jump_target(stubs[n].addr, out);
3253   save_regs(reglist);
3254   if (addrr != 0 || ofs_start != 0)
3255     emit_addimm(addrr, ofs_start, 0);
3256   emit_readword(&inv_code_start, 2);
3257   emit_readword(&inv_code_end, 3);
3258   if (len != 0)
3259     emit_addimm(0, len + 4, (rightr = 1));
3260   emit_cmp(0, 2);
3261   emit_cmpcs(3, rightr);
3262   void *jaddr = out;
3263   emit_jc(0);
3264   void *func = (len != 0)
3265     ? (void *)ndrc_write_invalidate_many
3266     : (void *)ndrc_write_invalidate_one;
3267   emit_far_call(func);
3268   set_jump_target(jaddr, out);
3269   restore_regs(reglist);
3270   emit_jmp(stubs[n].retaddr);
3271 }
3272
3273 static void do_store_smc_check(int i, const struct regstat *i_regs, u_int reglist, int addr)
3274 {
3275   if (HACK_ENABLED(NDHACK_NO_SMC_CHECK))
3276     return;
3277   // this can't be used any more since we started to check exact
3278   // block boundaries in invalidate_range()
3279   //if (i_regs->waswritten & (1<<dops[i].rs1))
3280   //  return;
3281   // (naively) assume nobody will run code from stack
3282   if (dops[i].rs1 == 29)
3283     return;
3284
3285   int j, imm_maxdiff = 32, imm_min = cinfo[i].imm, imm_max = cinfo[i].imm, count = 1;
3286   if (i < slen - 1 && dops[i+1].is_store && dops[i+1].rs1 == dops[i].rs1
3287       && abs(cinfo[i+1].imm - cinfo[i].imm) <= imm_maxdiff)
3288     return;
3289   for (j = i - 1; j >= 0; j--) {
3290     if (!dops[j].is_store || dops[j].rs1 != dops[i].rs1
3291         || abs(cinfo[j].imm - cinfo[j+1].imm) > imm_maxdiff)
3292       break;
3293     count++;
3294     if (imm_min > cinfo[j].imm)
3295       imm_min = cinfo[j].imm;
3296     if (imm_max < cinfo[j].imm)
3297       imm_max = cinfo[j].imm;
3298   }
3299 #if defined(HOST_IMM8)
3300   int ir = get_reg(i_regs->regmap, INVCP);
3301   assert(ir >= 0);
3302   host_tempreg_acquire();
3303   emit_ldrb_indexedsr12_reg(ir, addr, HOST_TEMPREG);
3304 #else
3305   emit_cmpmem_indexedsr12_imm(invalid_code, addr, 1);
3306   #error not handled
3307 #endif
3308 #ifdef INVALIDATE_USE_COND_CALL
3309   if (count == 1) {
3310     emit_cmpimm(HOST_TEMPREG, 1);
3311     emit_callne(invalidate_addr_reg[addr]);
3312     host_tempreg_release();
3313     return;
3314   }
3315 #endif
3316   void *jaddr = emit_cbz(HOST_TEMPREG, 0);
3317   host_tempreg_release();
3318   imm_min -= cinfo[i].imm;
3319   imm_max -= cinfo[i].imm;
3320   add_stub(INVCODE_STUB, jaddr, out, reglist|(1<<HOST_CCREG),
3321     addr, imm_min, imm_max, i);
3322 }
3323
3324 // determines if code overwrite checking is needed only
3325 // (also true non-existent 0x20000000 mirror that shouldn't matter)
3326 #define is_ram_addr(a) !((a) & 0x5f800000)
3327
3328 static void store_assemble(int i, const struct regstat *i_regs, int ccadj_)
3329 {
3330   int s,tl;
3331   int addr = cinfo[i].addr;
3332   int offset;
3333   void *jaddr=0;
3334   enum stub_type type=0;
3335   int memtarget=0,c=0;
3336   int offset_reg = -1;
3337   int fastio_reg_override = -1;
3338   u_int addr_const = ~0;
3339   u_int reglist=get_host_reglist(i_regs->regmap);
3340   tl=get_reg(i_regs->regmap,dops[i].rs2);
3341   s=get_reg(i_regs->regmap,dops[i].rs1);
3342   offset=cinfo[i].imm;
3343   if(s>=0) {
3344     c=(i_regs->wasconst>>s)&1;
3345     if (c) {
3346       addr_const = constmap[i][s] + offset;
3347       memtarget = ((signed int)addr_const) < (signed int)(0x80000000 + RAM_SIZE);
3348     }
3349   }
3350   assert(tl>=0);
3351   assert(addr >= 0);
3352   if(i_regs->regmap[HOST_CCREG]==CCREG) reglist&=~(1<<HOST_CCREG);
3353   reglist |= 1u << addr;
3354   if (!c) {
3355     jaddr = emit_fastpath_cmp_jump(i, i_regs, addr,
3356               &offset_reg, &fastio_reg_override, ccadj_);
3357   }
3358   else if (ram_offset && memtarget) {
3359     offset_reg = get_ro_reg(i_regs, 0);
3360   }
3361
3362   switch (dops[i].opcode) {
3363   case 0x28: // SB
3364     if(!c||memtarget) {
3365       int a = addr;
3366       if (fastio_reg_override >= 0)
3367         a = fastio_reg_override;
3368       do_store_byte(a, tl, offset_reg);
3369     }
3370     type = STOREB_STUB;
3371     break;
3372   case 0x29: // SH
3373     if(!c||memtarget) {
3374       int a = addr;
3375       if (fastio_reg_override >= 0)
3376         a = fastio_reg_override;
3377       do_store_hword(a, 0, tl, offset_reg, 1);
3378     }
3379     type = STOREH_STUB;
3380     break;
3381   case 0x2B: // SW
3382     if(!c||memtarget) {
3383       int a = addr;
3384       if (fastio_reg_override >= 0)
3385         a = fastio_reg_override;
3386       do_store_word(a, 0, tl, offset_reg, 1);
3387     }
3388     type = STOREW_STUB;
3389     break;
3390   default:
3391     assert(0);
3392   }
3393   if (fastio_reg_override == HOST_TEMPREG || offset_reg == HOST_TEMPREG)
3394     host_tempreg_release();
3395   if (jaddr) {
3396     // PCSX store handlers don't check invcode again
3397     add_stub_r(type,jaddr,out,i,addr,i_regs,ccadj_,reglist);
3398   }
3399   if (!c || is_ram_addr(addr_const))
3400     do_store_smc_check(i, i_regs, reglist, addr);
3401   if (c && !memtarget)
3402     inline_writestub(type, i, addr_const, i_regs->regmap, dops[i].rs2, ccadj_, reglist);
3403   // basic current block modification detection..
3404   // not looking back as that should be in mips cache already
3405   // (see Spyro2 title->attract mode)
3406   if (start + i*4 < addr_const && addr_const < start + slen*4) {
3407     SysPrintf("write to %08x hits block %08x, pc=%08x\n", addr_const, start, start+i*4);
3408     assert(i_regs->regmap==regs[i].regmap); // not delay slot
3409     if(i_regs->regmap==regs[i].regmap) {
3410       load_all_consts(regs[i].regmap_entry,regs[i].wasdirty,i);
3411       wb_dirtys(regs[i].regmap_entry,regs[i].wasdirty);
3412       emit_movimm(start+i*4+4,0);
3413       emit_writeword(0,&pcaddr);
3414       emit_addimm(HOST_CCREG,2,HOST_CCREG);
3415       emit_far_call(ndrc_get_addr_ht);
3416       emit_jmpreg(0);
3417     }
3418   }
3419 }
3420
3421 static void storelr_assemble(int i, const struct regstat *i_regs, int ccadj_)
3422 {
3423   int addr = cinfo[i].addr;
3424   int s,tl;
3425   int offset;
3426   void *jaddr=0;
3427   void *case1, *case23, *case3;
3428   void *done0, *done1, *done2;
3429   int memtarget=0,c=0;
3430   int offset_reg = -1;
3431   u_int addr_const = ~0;
3432   u_int reglist = get_host_reglist(i_regs->regmap);
3433   tl=get_reg(i_regs->regmap,dops[i].rs2);
3434   s=get_reg(i_regs->regmap,dops[i].rs1);
3435   offset=cinfo[i].imm;
3436   if(s>=0) {
3437     c = (i_regs->isconst >> s) & 1;
3438     if (c) {
3439       addr_const = constmap[i][s] + offset;
3440       memtarget = ((signed int)addr_const) < (signed int)(0x80000000 + RAM_SIZE);
3441     }
3442   }
3443   assert(tl>=0);
3444   assert(addr >= 0);
3445   reglist |= 1u << addr;
3446   if(!c) {
3447     emit_cmpimm(addr, RAM_SIZE);
3448     jaddr=out;
3449     emit_jno(0);
3450   }
3451   else
3452   {
3453     if(!memtarget||!dops[i].rs1) {
3454       jaddr=out;
3455       emit_jmp(0);
3456     }
3457   }
3458   if (ram_offset)
3459     offset_reg = get_ro_reg(i_regs, 0);
3460
3461   emit_testimm(addr,2);
3462   case23=out;
3463   emit_jne(0);
3464   emit_testimm(addr,1);
3465   case1=out;
3466   emit_jne(0);
3467   // 0
3468   if (dops[i].opcode == 0x2A) { // SWL
3469     // Write msb into least significant byte
3470     if (dops[i].rs2) emit_rorimm(tl, 24, tl);
3471     do_store_byte(addr, tl, offset_reg);
3472     if (dops[i].rs2) emit_rorimm(tl, 8, tl);
3473   }
3474   else if (dops[i].opcode == 0x2E) { // SWR
3475     // Write entire word
3476     do_store_word(addr, 0, tl, offset_reg, 1);
3477   }
3478   done0 = out;
3479   emit_jmp(0);
3480   // 1
3481   set_jump_target(case1, out);
3482   if (dops[i].opcode == 0x2A) { // SWL
3483     // Write two msb into two least significant bytes
3484     if (dops[i].rs2) emit_rorimm(tl, 16, tl);
3485     do_store_hword(addr, -1, tl, offset_reg, 1);
3486     if (dops[i].rs2) emit_rorimm(tl, 16, tl);
3487   }
3488   else if (dops[i].opcode == 0x2E) { // SWR
3489     // Write 3 lsb into three most significant bytes
3490     do_store_byte(addr, tl, offset_reg);
3491     if (dops[i].rs2) emit_rorimm(tl, 8, tl);
3492     do_store_hword(addr, 1, tl, offset_reg, 1);
3493     if (dops[i].rs2) emit_rorimm(tl, 24, tl);
3494   }
3495   done1=out;
3496   emit_jmp(0);
3497   // 2,3
3498   set_jump_target(case23, out);
3499   emit_testimm(addr,1);
3500   case3 = out;
3501   emit_jne(0);
3502   // 2
3503   if (dops[i].opcode==0x2A) { // SWL
3504     // Write 3 msb into three least significant bytes
3505     if (dops[i].rs2) emit_rorimm(tl, 8, tl);
3506     do_store_hword(addr, -2, tl, offset_reg, 1);
3507     if (dops[i].rs2) emit_rorimm(tl, 16, tl);
3508     do_store_byte(addr, tl, offset_reg);
3509     if (dops[i].rs2) emit_rorimm(tl, 8, tl);
3510   }
3511   else if (dops[i].opcode == 0x2E) { // SWR
3512     // Write two lsb into two most significant bytes
3513     do_store_hword(addr, 0, tl, offset_reg, 1);
3514   }
3515   done2 = out;
3516   emit_jmp(0);
3517   // 3
3518   set_jump_target(case3, out);
3519   if (dops[i].opcode == 0x2A) { // SWL
3520     do_store_word(addr, -3, tl, offset_reg, 1);
3521   }
3522   else if (dops[i].opcode == 0x2E) { // SWR
3523     do_store_byte(addr, tl, offset_reg);
3524   }
3525   set_jump_target(done0, out);
3526   set_jump_target(done1, out);
3527   set_jump_target(done2, out);
3528   if (offset_reg == HOST_TEMPREG)
3529     host_tempreg_release();
3530   if (!c || !memtarget)
3531     add_stub_r(STORELR_STUB,jaddr,out,i,addr,i_regs,ccadj_,reglist);
3532   if (!c || is_ram_addr(addr_const))
3533     do_store_smc_check(i, i_regs, reglist, addr);
3534 }
3535
3536 static void cop0_assemble(int i, const struct regstat *i_regs, int ccadj_)
3537 {
3538   if(dops[i].opcode2==0) // MFC0
3539   {
3540     signed char t=get_reg_w(i_regs->regmap, dops[i].rt1);
3541     u_int copr=(source[i]>>11)&0x1f;
3542     if(t>=0&&dops[i].rt1!=0) {
3543       emit_readword(&reg_cop0[copr],t);
3544     }
3545   }
3546   else if(dops[i].opcode2==4) // MTC0
3547   {
3548     int s = get_reg(i_regs->regmap, dops[i].rs1);
3549     int cc = get_reg(i_regs->regmap, CCREG);
3550     char copr=(source[i]>>11)&0x1f;
3551     assert(s>=0);
3552     wb_register(dops[i].rs1,i_regs->regmap,i_regs->dirty);
3553     if (copr == 12 || copr == 13) {
3554       emit_readword(&last_count,HOST_TEMPREG);
3555       if (cc != HOST_CCREG)
3556         emit_loadreg(CCREG, HOST_CCREG);
3557       emit_add(HOST_CCREG, HOST_TEMPREG, HOST_CCREG);
3558       emit_addimm(HOST_CCREG, ccadj_ + 2, HOST_CCREG);
3559       emit_writeword(HOST_CCREG, &psxRegs.cycle);
3560       if (is_delayslot) {
3561         // burn cycles to cause cc_interrupt, which will
3562         // reschedule next_interupt. Relies on CCREG from above.
3563         assem_debug("MTC0 DS %d\n", copr);
3564         emit_writeword(HOST_CCREG,&last_count);
3565         emit_movimm(0,HOST_CCREG);
3566         emit_storereg(CCREG,HOST_CCREG);
3567         emit_loadreg(dops[i].rs1,1);
3568         emit_movimm(copr,0);
3569         emit_far_call(pcsx_mtc0_ds);
3570         emit_loadreg(dops[i].rs1,s);
3571         return;
3572       }
3573       emit_movimm(start+i*4+4,HOST_TEMPREG);
3574       emit_writeword(HOST_TEMPREG,&pcaddr);
3575       emit_movimm(0,HOST_TEMPREG);
3576       emit_writeword(HOST_TEMPREG,&pending_exception);
3577     }
3578     if( s != 1)
3579       emit_mov(s, 1);
3580     emit_movimm(copr, 0);
3581     emit_far_call(pcsx_mtc0);
3582     if (copr == 12 || copr == 13) {
3583       emit_readword(&psxRegs.cycle,HOST_CCREG);
3584       emit_readword(&last_count,HOST_TEMPREG);
3585       emit_sub(HOST_CCREG,HOST_TEMPREG,HOST_CCREG);
3586       //emit_writeword(HOST_TEMPREG,&last_count);
3587       assert(!is_delayslot);
3588       emit_readword(&pending_exception,HOST_TEMPREG);
3589       emit_test(HOST_TEMPREG,HOST_TEMPREG);
3590       void *jaddr = out;
3591       emit_jeq(0);
3592       emit_readword(&pcaddr, 0);
3593       emit_far_call(ndrc_get_addr_ht);
3594       emit_jmpreg(0);
3595       set_jump_target(jaddr, out);
3596       emit_addimm(HOST_CCREG, -ccadj_ - 2, HOST_CCREG);
3597       if (cc != HOST_CCREG)
3598         emit_storereg(CCREG, HOST_CCREG);
3599     }
3600     emit_loadreg(dops[i].rs1,s);
3601   }
3602 }
3603
3604 static void rfe_assemble(int i, const struct regstat *i_regs)
3605 {
3606   emit_readword(&psxRegs.CP0.n.SR, 0);
3607   emit_andimm(0, 0x3c, 1);
3608   emit_andimm(0, ~0xf, 0);
3609   emit_orrshr_imm(1, 2, 0);
3610   emit_writeword(0, &psxRegs.CP0.n.SR);
3611 }
3612
3613 static int cop2_is_stalling_op(int i, int *cycles)
3614 {
3615   if (dops[i].opcode == 0x3a) { // SWC2
3616     *cycles = 0;
3617     return 1;
3618   }
3619   if (dops[i].itype == COP2 && (dops[i].opcode2 == 0 || dops[i].opcode2 == 2)) { // MFC2/CFC2
3620     *cycles = 0;
3621     return 1;
3622   }
3623   if (dops[i].itype == C2OP) {
3624     *cycles = gte_cycletab[source[i] & 0x3f];
3625     return 1;
3626   }
3627   // ... what about MTC2/CTC2/LWC2?
3628   return 0;
3629 }
3630
3631 #if 0
3632 static void log_gte_stall(int stall, u_int cycle)
3633 {
3634   if ((u_int)stall <= 44)
3635     printf("x    stall %2d %u\n", stall, cycle + last_count);
3636 }
3637
3638 static void emit_log_gte_stall(int i, int stall, u_int reglist)
3639 {
3640   save_regs(reglist);
3641   if (stall > 0)
3642     emit_movimm(stall, 0);
3643   else
3644     emit_mov(HOST_TEMPREG, 0);
3645   emit_addimm(HOST_CCREG, cinfo[i].ccadj, 1);
3646   emit_far_call(log_gte_stall);
3647   restore_regs(reglist);
3648 }
3649 #endif
3650
3651 static void cop2_do_stall_check(u_int op, int i, const struct regstat *i_regs, u_int reglist)
3652 {
3653   int j = i, other_gte_op_cycles = -1, stall = -MAXBLOCK, cycles_passed;
3654   int rtmp = reglist_find_free(reglist);
3655
3656   if (HACK_ENABLED(NDHACK_NO_STALLS))
3657     return;
3658   if (get_reg(i_regs->regmap, CCREG) != HOST_CCREG) {
3659     // happens occasionally... cc evicted? Don't bother then
3660     //printf("no cc %08x\n", start + i*4);
3661     return;
3662   }
3663   if (!dops[i].bt) {
3664     for (j = i - 1; j >= 0; j--) {
3665       //if (dops[j].is_ds) break;
3666       if (cop2_is_stalling_op(j, &other_gte_op_cycles) || dops[j].bt)
3667         break;
3668       if (j > 0 && cinfo[j - 1].ccadj > cinfo[j].ccadj)
3669         break;
3670     }
3671     j = max(j, 0);
3672   }
3673   cycles_passed = cinfo[i].ccadj - cinfo[j].ccadj;
3674   if (other_gte_op_cycles >= 0)
3675     stall = other_gte_op_cycles - cycles_passed;
3676   else if (cycles_passed >= 44)
3677     stall = 0; // can't stall
3678   if (stall == -MAXBLOCK && rtmp >= 0) {
3679     // unknown stall, do the expensive runtime check
3680     assem_debug("; cop2_do_stall_check\n");
3681 #if 0 // too slow
3682     save_regs(reglist);
3683     emit_movimm(gte_cycletab[op], 0);
3684     emit_addimm(HOST_CCREG, cinfo[i].ccadj, 1);
3685     emit_far_call(call_gteStall);
3686     restore_regs(reglist);
3687 #else
3688     host_tempreg_acquire();
3689     emit_readword(&psxRegs.gteBusyCycle, rtmp);
3690     emit_addimm(rtmp, -cinfo[i].ccadj, rtmp);
3691     emit_sub(rtmp, HOST_CCREG, HOST_TEMPREG);
3692     emit_cmpimm(HOST_TEMPREG, 44);
3693     emit_cmovb_reg(rtmp, HOST_CCREG);
3694     //emit_log_gte_stall(i, 0, reglist);
3695     host_tempreg_release();
3696 #endif
3697   }
3698   else if (stall > 0) {
3699     //emit_log_gte_stall(i, stall, reglist);
3700     emit_addimm(HOST_CCREG, stall, HOST_CCREG);
3701   }
3702
3703   // save gteBusyCycle, if needed
3704   if (gte_cycletab[op] == 0)
3705     return;
3706   other_gte_op_cycles = -1;
3707   for (j = i + 1; j < slen; j++) {
3708     if (cop2_is_stalling_op(j, &other_gte_op_cycles))
3709       break;
3710     if (dops[j].is_jump) {
3711       // check ds
3712       if (j + 1 < slen && cop2_is_stalling_op(j + 1, &other_gte_op_cycles))
3713         j++;
3714       break;
3715     }
3716   }
3717   if (other_gte_op_cycles >= 0)
3718     // will handle stall when assembling that op
3719     return;
3720   cycles_passed = cinfo[min(j, slen -1)].ccadj - cinfo[i].ccadj;
3721   if (cycles_passed >= 44)
3722     return;
3723   assem_debug("; save gteBusyCycle\n");
3724   host_tempreg_acquire();
3725 #if 0
3726   emit_readword(&last_count, HOST_TEMPREG);
3727   emit_add(HOST_TEMPREG, HOST_CCREG, HOST_TEMPREG);
3728   emit_addimm(HOST_TEMPREG, cinfo[i].ccadj, HOST_TEMPREG);
3729   emit_addimm(HOST_TEMPREG, gte_cycletab[op]), HOST_TEMPREG);
3730   emit_writeword(HOST_TEMPREG, &psxRegs.gteBusyCycle);
3731 #else
3732   emit_addimm(HOST_CCREG, cinfo[i].ccadj + gte_cycletab[op], HOST_TEMPREG);
3733   emit_writeword(HOST_TEMPREG, &psxRegs.gteBusyCycle);
3734 #endif
3735   host_tempreg_release();
3736 }
3737
3738 static int is_mflohi(int i)
3739 {
3740   return (dops[i].itype == MOV && (dops[i].rs1 == HIREG || dops[i].rs1 == LOREG));
3741 }
3742
3743 static int check_multdiv(int i, int *cycles)
3744 {
3745   if (dops[i].itype != MULTDIV)
3746     return 0;
3747   if (dops[i].opcode2 == 0x18 || dops[i].opcode2 == 0x19) // MULT(U)
3748     *cycles = 11; // approx from 7 11 14
3749   else
3750     *cycles = 37;
3751   return 1;
3752 }
3753
3754 static void multdiv_prepare_stall(int i, const struct regstat *i_regs, int ccadj_)
3755 {
3756   int j, found = 0, c = 0;
3757   if (HACK_ENABLED(NDHACK_NO_STALLS))
3758     return;
3759   if (get_reg(i_regs->regmap, CCREG) != HOST_CCREG) {
3760     // happens occasionally... cc evicted? Don't bother then
3761     return;
3762   }
3763   for (j = i + 1; j < slen; j++) {
3764     if (dops[j].bt)
3765       break;
3766     if ((found = is_mflohi(j)))
3767       break;
3768     if (dops[j].is_jump) {
3769       // check ds
3770       if (j + 1 < slen && (found = is_mflohi(j + 1)))
3771         j++;
3772       break;
3773     }
3774   }
3775   if (found)
3776     // handle all in multdiv_do_stall()
3777     return;
3778   check_multdiv(i, &c);
3779   assert(c > 0);
3780   assem_debug("; muldiv prepare stall %d\n", c);
3781   host_tempreg_acquire();
3782   emit_addimm(HOST_CCREG, ccadj_ + c, HOST_TEMPREG);
3783   emit_writeword(HOST_TEMPREG, &psxRegs.muldivBusyCycle);
3784   host_tempreg_release();
3785 }
3786
3787 static void multdiv_do_stall(int i, const struct regstat *i_regs)
3788 {
3789   int j, known_cycles = 0;
3790   u_int reglist = get_host_reglist(i_regs->regmap);
3791   int rtmp = get_reg_temp(i_regs->regmap);
3792   if (rtmp < 0)
3793     rtmp = reglist_find_free(reglist);
3794   if (HACK_ENABLED(NDHACK_NO_STALLS))
3795     return;
3796   if (get_reg(i_regs->regmap, CCREG) != HOST_CCREG || rtmp < 0) {
3797     // happens occasionally... cc evicted? Don't bother then
3798     //printf("no cc/rtmp %08x\n", start + i*4);
3799     return;
3800   }
3801   if (!dops[i].bt) {
3802     for (j = i - 1; j >= 0; j--) {
3803       if (dops[j].is_ds) break;
3804       if (check_multdiv(j, &known_cycles))
3805         break;
3806       if (is_mflohi(j))
3807         // already handled by this op
3808         return;
3809       if (dops[j].bt || (j > 0 && cinfo[j - 1].ccadj > cinfo[j].ccadj))
3810         break;
3811     }
3812     j = max(j, 0);
3813   }
3814   if (known_cycles > 0) {
3815     known_cycles -= cinfo[i].ccadj - cinfo[j].ccadj;
3816     assem_debug("; muldiv stall resolved %d\n", known_cycles);
3817     if (known_cycles > 0)
3818       emit_addimm(HOST_CCREG, known_cycles, HOST_CCREG);
3819     return;
3820   }
3821   assem_debug("; muldiv stall unresolved\n");
3822   host_tempreg_acquire();
3823   emit_readword(&psxRegs.muldivBusyCycle, rtmp);
3824   emit_addimm(rtmp, -cinfo[i].ccadj, rtmp);
3825   emit_sub(rtmp, HOST_CCREG, HOST_TEMPREG);
3826   emit_cmpimm(HOST_TEMPREG, 37);
3827   emit_cmovb_reg(rtmp, HOST_CCREG);
3828   //emit_log_gte_stall(i, 0, reglist);
3829   host_tempreg_release();
3830 }
3831
3832 static void cop2_get_dreg(u_int copr,signed char tl,signed char temp)
3833 {
3834   switch (copr) {
3835     case 1:
3836     case 3:
3837     case 5:
3838     case 8:
3839     case 9:
3840     case 10:
3841     case 11:
3842       emit_readword(&reg_cop2d[copr],tl);
3843       emit_signextend16(tl,tl);
3844       emit_writeword(tl,&reg_cop2d[copr]); // hmh
3845       break;
3846     case 7:
3847     case 16:
3848     case 17:
3849     case 18:
3850     case 19:
3851       emit_readword(&reg_cop2d[copr],tl);
3852       emit_andimm(tl,0xffff,tl);
3853       emit_writeword(tl,&reg_cop2d[copr]);
3854       break;
3855     case 15:
3856       emit_readword(&reg_cop2d[14],tl); // SXY2
3857       emit_writeword(tl,&reg_cop2d[copr]);
3858       break;
3859     case 28:
3860     case 29:
3861       c2op_mfc2_29_assemble(tl,temp);
3862       break;
3863     default:
3864       emit_readword(&reg_cop2d[copr],tl);
3865       break;
3866   }
3867 }
3868
3869 static void cop2_put_dreg(u_int copr,signed char sl,signed char temp)
3870 {
3871   switch (copr) {
3872     case 15:
3873       emit_readword(&reg_cop2d[13],temp);  // SXY1
3874       emit_writeword(sl,&reg_cop2d[copr]);
3875       emit_writeword(temp,&reg_cop2d[12]); // SXY0
3876       emit_readword(&reg_cop2d[14],temp);  // SXY2
3877       emit_writeword(sl,&reg_cop2d[14]);
3878       emit_writeword(temp,&reg_cop2d[13]); // SXY1
3879       break;
3880     case 28:
3881       emit_andimm(sl,0x001f,temp);
3882       emit_shlimm(temp,7,temp);
3883       emit_writeword(temp,&reg_cop2d[9]);
3884       emit_andimm(sl,0x03e0,temp);
3885       emit_shlimm(temp,2,temp);
3886       emit_writeword(temp,&reg_cop2d[10]);
3887       emit_andimm(sl,0x7c00,temp);
3888       emit_shrimm(temp,3,temp);
3889       emit_writeword(temp,&reg_cop2d[11]);
3890       emit_writeword(sl,&reg_cop2d[28]);
3891       break;
3892     case 30:
3893       emit_xorsar_imm(sl,sl,31,temp);
3894 #if defined(HAVE_ARMV5) || defined(__aarch64__)
3895       emit_clz(temp,temp);
3896 #else
3897       emit_movs(temp,HOST_TEMPREG);
3898       emit_movimm(0,temp);
3899       emit_jeq((int)out+4*4);
3900       emit_addpl_imm(temp,1,temp);
3901       emit_lslpls_imm(HOST_TEMPREG,1,HOST_TEMPREG);
3902       emit_jns((int)out-2*4);
3903 #endif
3904       emit_writeword(sl,&reg_cop2d[30]);
3905       emit_writeword(temp,&reg_cop2d[31]);
3906       break;
3907     case 31:
3908       break;
3909     default:
3910       emit_writeword(sl,&reg_cop2d[copr]);
3911       break;
3912   }
3913 }
3914
3915 static void c2ls_assemble(int i, const struct regstat *i_regs, int ccadj_)
3916 {
3917   int s,tl;
3918   int ar;
3919   int offset;
3920   int memtarget=0,c=0;
3921   void *jaddr2=NULL;
3922   enum stub_type type;
3923   int offset_reg = -1;
3924   int fastio_reg_override = -1;
3925   u_int addr_const = ~0;
3926   u_int reglist=get_host_reglist(i_regs->regmap);
3927   u_int copr=(source[i]>>16)&0x1f;
3928   s=get_reg(i_regs->regmap,dops[i].rs1);
3929   tl=get_reg(i_regs->regmap,FTEMP);
3930   offset=cinfo[i].imm;
3931   assert(tl>=0);
3932
3933   if(i_regs->regmap[HOST_CCREG]==CCREG)
3934     reglist&=~(1<<HOST_CCREG);
3935
3936   // get the address
3937   ar = cinfo[i].addr;
3938   assert(ar >= 0);
3939   if (dops[i].opcode==0x3a) { // SWC2
3940     reglist |= 1<<ar;
3941   }
3942   if (s >= 0) {
3943     c = (i_regs->isconst >> s) & 1;
3944     if (c) {
3945       addr_const = constmap[i][s] + offset;
3946       memtarget = ((signed int)addr_const) < (signed int)(0x80000000 + RAM_SIZE);
3947     }
3948   }
3949
3950   cop2_do_stall_check(0, i, i_regs, reglist);
3951
3952   if (dops[i].opcode==0x3a) { // SWC2
3953     cop2_get_dreg(copr,tl,-1);
3954     type=STOREW_STUB;
3955   }
3956   else
3957     type=LOADW_STUB;
3958
3959   if(c&&!memtarget) {
3960     jaddr2=out;
3961     emit_jmp(0); // inline_readstub/inline_writestub?
3962   }
3963   else {
3964     if(!c) {
3965       jaddr2 = emit_fastpath_cmp_jump(i, i_regs, ar,
3966                 &offset_reg, &fastio_reg_override, ccadj_);
3967     }
3968     else if (ram_offset && memtarget) {
3969       offset_reg = get_ro_reg(i_regs, 0);
3970     }
3971     switch (dops[i].opcode) {
3972     case 0x32: { // LWC2
3973       int a = ar;
3974       if (fastio_reg_override >= 0)
3975         a = fastio_reg_override;
3976       do_load_word(a, tl, offset_reg);
3977       break;
3978     }
3979     case 0x3a: { // SWC2
3980       #ifdef DESTRUCTIVE_SHIFT
3981       if(!offset&&!c&&s>=0) emit_mov(s,ar);
3982       #endif
3983       int a = ar;
3984       if (fastio_reg_override >= 0)
3985         a = fastio_reg_override;
3986       do_store_word(a, 0, tl, offset_reg, 1);
3987       break;
3988     }
3989     default:
3990       assert(0);
3991     }
3992   }
3993   if (fastio_reg_override == HOST_TEMPREG || offset_reg == HOST_TEMPREG)
3994     host_tempreg_release();
3995   if(jaddr2)
3996     add_stub_r(type,jaddr2,out,i,ar,i_regs,ccadj_,reglist);
3997   if (dops[i].opcode == 0x3a && (!c || is_ram_addr(addr_const))) // SWC2
3998     do_store_smc_check(i, i_regs, reglist, ar);
3999   if (dops[i].opcode == 0x32) { // LWC2
4000     host_tempreg_acquire();
4001     cop2_put_dreg(copr,tl,HOST_TEMPREG);
4002     host_tempreg_release();
4003   }
4004 }
4005
4006 static void cop2_assemble(int i, const struct regstat *i_regs)
4007 {
4008   u_int copr = (source[i]>>11) & 0x1f;
4009   signed char temp = get_reg_temp(i_regs->regmap);
4010
4011   if (!HACK_ENABLED(NDHACK_NO_STALLS)) {
4012     u_int reglist = reglist_exclude(get_host_reglist(i_regs->regmap), temp, -1);
4013     if (dops[i].opcode2 == 0 || dops[i].opcode2 == 2) { // MFC2/CFC2
4014       signed char tl = get_reg(i_regs->regmap, dops[i].rt1);
4015       reglist = reglist_exclude(reglist, tl, -1);
4016     }
4017     cop2_do_stall_check(0, i, i_regs, reglist);
4018   }
4019   if (dops[i].opcode2==0) { // MFC2
4020     signed char tl=get_reg_w(i_regs->regmap, dops[i].rt1);
4021     if(tl>=0&&dops[i].rt1!=0)
4022       cop2_get_dreg(copr,tl,temp);
4023   }
4024   else if (dops[i].opcode2==4) { // MTC2
4025     signed char sl=get_reg(i_regs->regmap,dops[i].rs1);
4026     cop2_put_dreg(copr,sl,temp);
4027   }
4028   else if (dops[i].opcode2==2) // CFC2
4029   {
4030     signed char tl=get_reg_w(i_regs->regmap, dops[i].rt1);
4031     if(tl>=0&&dops[i].rt1!=0)
4032       emit_readword(&reg_cop2c[copr],tl);
4033   }
4034   else if (dops[i].opcode2==6) // CTC2
4035   {
4036     signed char sl=get_reg(i_regs->regmap,dops[i].rs1);
4037     switch(copr) {
4038       case 4:
4039       case 12:
4040       case 20:
4041       case 26:
4042       case 27:
4043       case 29:
4044       case 30:
4045         emit_signextend16(sl,temp);
4046         break;
4047       case 31:
4048         c2op_ctc2_31_assemble(sl,temp);
4049         break;
4050       default:
4051         temp=sl;
4052         break;
4053     }
4054     emit_writeword(temp,&reg_cop2c[copr]);
4055     assert(sl>=0);
4056   }
4057 }
4058
4059 static void do_unalignedwritestub(int n)
4060 {
4061   assem_debug("do_unalignedwritestub %x\n",start+stubs[n].a*4);
4062   literal_pool(256);
4063   set_jump_target(stubs[n].addr, out);
4064
4065   int i=stubs[n].a;
4066   struct regstat *i_regs=(struct regstat *)stubs[n].c;
4067   int addr=stubs[n].b;
4068   u_int reglist=stubs[n].e;
4069   signed char *i_regmap=i_regs->regmap;
4070   int temp2=get_reg(i_regmap,FTEMP);
4071   int rt;
4072   rt=get_reg(i_regmap,dops[i].rs2);
4073   assert(rt>=0);
4074   assert(addr>=0);
4075   assert(dops[i].opcode==0x2a||dops[i].opcode==0x2e); // SWL/SWR only implemented
4076   reglist|=(1<<addr);
4077   reglist&=~(1<<temp2);
4078
4079   // don't bother with it and call write handler
4080   save_regs(reglist);
4081   pass_args(addr,rt);
4082   int cc=get_reg(i_regmap,CCREG);
4083   if(cc<0)
4084     emit_loadreg(CCREG,2);
4085   emit_addimm(cc<0?2:cc,(int)stubs[n].d+1,2);
4086   emit_movimm(start + i*4,3);
4087   emit_writeword(3,&psxRegs.pc);
4088   emit_far_call((dops[i].opcode==0x2a?jump_handle_swl:jump_handle_swr));
4089   emit_addimm(0,-((int)stubs[n].d+1),cc<0?2:cc);
4090   if(cc<0)
4091     emit_storereg(CCREG,2);
4092   restore_regs(reglist);
4093   emit_jmp(stubs[n].retaddr); // return address
4094 }
4095
4096 static void do_overflowstub(int n)
4097 {
4098   assem_debug("do_overflowstub %x\n", start + (u_int)stubs[n].a * 4);
4099   literal_pool(24);
4100   int i = stubs[n].a;
4101   struct regstat *i_regs = (struct regstat *)stubs[n].c;
4102   int ccadj = stubs[n].d;
4103   set_jump_target(stubs[n].addr, out);
4104   wb_dirtys(regs[i].regmap, regs[i].dirty);
4105   exception_assemble(i, i_regs, ccadj);
4106 }
4107
4108 static void do_alignmentstub(int n)
4109 {
4110   assem_debug("do_alignmentstub %x\n", start + (u_int)stubs[n].a * 4);
4111   literal_pool(24);
4112   int i = stubs[n].a;
4113   struct regstat *i_regs = (struct regstat *)stubs[n].c;
4114   int ccadj = stubs[n].d;
4115   int is_store = dops[i].itype == STORE || dops[i].opcode == 0x3A; // SWC2
4116   int cause = (dops[i].opcode & 3) << 28;
4117   cause |= is_store ? (R3000E_AdES << 2) : (R3000E_AdEL << 2);
4118   set_jump_target(stubs[n].addr, out);
4119   wb_dirtys(regs[i].regmap, regs[i].dirty);
4120   if (stubs[n].b != 1)
4121     emit_mov(stubs[n].b, 1); // faulting address
4122   emit_movimm(cause, 0);
4123   exception_assemble(i, i_regs, ccadj);
4124 }
4125
4126 #ifndef multdiv_assemble
4127 void multdiv_assemble(int i,struct regstat *i_regs)
4128 {
4129   printf("Need multdiv_assemble for this architecture.\n");
4130   abort();
4131 }
4132 #endif
4133
4134 static void mov_assemble(int i, const struct regstat *i_regs)
4135 {
4136   //if(dops[i].opcode2==0x10||dops[i].opcode2==0x12) { // MFHI/MFLO
4137   //if(dops[i].opcode2==0x11||dops[i].opcode2==0x13) { // MTHI/MTLO
4138   if(dops[i].rt1) {
4139     signed char sl,tl;
4140     tl=get_reg_w(i_regs->regmap, dops[i].rt1);
4141     //assert(tl>=0);
4142     if(tl>=0) {
4143       sl=get_reg(i_regs->regmap,dops[i].rs1);
4144       if(sl>=0) emit_mov(sl,tl);
4145       else emit_loadreg(dops[i].rs1,tl);
4146     }
4147   }
4148   if (dops[i].rs1 == HIREG || dops[i].rs1 == LOREG) // MFHI/MFLO
4149     multdiv_do_stall(i, i_regs);
4150 }
4151
4152 // call interpreter, exception handler, things that change pc/regs/cycles ...
4153 static void call_c_cpu_handler(int i, const struct regstat *i_regs, int ccadj_, u_int pc, void *func)
4154 {
4155   signed char ccreg=get_reg(i_regs->regmap,CCREG);
4156   assert(ccreg==HOST_CCREG);
4157   assert(!is_delayslot);
4158   (void)ccreg;
4159
4160   emit_movimm(pc,3); // Get PC
4161   emit_readword(&last_count,2);
4162   emit_writeword(3,&psxRegs.pc);
4163   emit_addimm(HOST_CCREG,ccadj_,HOST_CCREG);
4164   emit_add(2,HOST_CCREG,2);
4165   emit_writeword(2,&psxRegs.cycle);
4166   emit_addimm_ptr(FP,(u_char *)&psxRegs - (u_char *)&dynarec_local,0);
4167   emit_far_call(func);
4168   emit_far_jump(jump_to_new_pc);
4169 }
4170
4171 static void exception_assemble(int i, const struct regstat *i_regs, int ccadj_)
4172 {
4173   // 'break' tends to be littered around to catch things like
4174   // division by 0 and is almost never executed, so don't emit much code here
4175   void *func;
4176   if (dops[i].itype == ALU || dops[i].itype == IMM16)
4177     func = is_delayslot ? jump_overflow_ds : jump_overflow;
4178   else if (dops[i].itype == LOAD || dops[i].itype == STORE)
4179     func = is_delayslot ? jump_addrerror_ds : jump_addrerror;
4180   else if (dops[i].opcode2 == 0x0C)
4181     func = is_delayslot ? jump_syscall_ds : jump_syscall;
4182   else
4183     func = is_delayslot ? jump_break_ds : jump_break;
4184   if (get_reg(i_regs->regmap, CCREG) != HOST_CCREG) // evicted
4185     emit_loadreg(CCREG, HOST_CCREG);
4186   emit_movimm(start + i*4, 2); // pc
4187   emit_addimm(HOST_CCREG, ccadj_ + CLOCK_ADJUST(1), HOST_CCREG);
4188   emit_far_jump(func);
4189 }
4190
4191 static void hlecall_bad()
4192 {
4193   assert(0);
4194 }
4195
4196 static void hlecall_assemble(int i, const struct regstat *i_regs, int ccadj_)
4197 {
4198   void *hlefunc = hlecall_bad;
4199   uint32_t hleCode = source[i] & 0x03ffffff;
4200   if (hleCode < ARRAY_SIZE(psxHLEt))
4201     hlefunc = psxHLEt[hleCode];
4202
4203   call_c_cpu_handler(i, i_regs, ccadj_, start + i*4+4, hlefunc);
4204 }
4205
4206 static void intcall_assemble(int i, const struct regstat *i_regs, int ccadj_)
4207 {
4208   call_c_cpu_handler(i, i_regs, ccadj_, start + i*4, execI);
4209 }
4210
4211 static void speculate_mov(int rs,int rt)
4212 {
4213   if(rt!=0) {
4214     smrv_strong_next|=1<<rt;
4215     smrv[rt]=smrv[rs];
4216   }
4217 }
4218
4219 static void speculate_mov_weak(int rs,int rt)
4220 {
4221   if(rt!=0) {
4222     smrv_weak_next|=1<<rt;
4223     smrv[rt]=smrv[rs];
4224   }
4225 }
4226
4227 static void speculate_register_values(int i)
4228 {
4229   if(i==0) {
4230     memcpy(smrv,psxRegs.GPR.r,sizeof(smrv));
4231     // gp,sp are likely to stay the same throughout the block
4232     smrv_strong_next=(1<<28)|(1<<29)|(1<<30);
4233     smrv_weak_next=~smrv_strong_next;
4234     //printf(" llr %08x\n", smrv[4]);
4235   }
4236   smrv_strong=smrv_strong_next;
4237   smrv_weak=smrv_weak_next;
4238   switch(dops[i].itype) {
4239     case ALU:
4240       if     ((smrv_strong>>dops[i].rs1)&1) speculate_mov(dops[i].rs1,dops[i].rt1);
4241       else if((smrv_strong>>dops[i].rs2)&1) speculate_mov(dops[i].rs2,dops[i].rt1);
4242       else if((smrv_weak>>dops[i].rs1)&1) speculate_mov_weak(dops[i].rs1,dops[i].rt1);
4243       else if((smrv_weak>>dops[i].rs2)&1) speculate_mov_weak(dops[i].rs2,dops[i].rt1);
4244       else {
4245         smrv_strong_next&=~(1<<dops[i].rt1);
4246         smrv_weak_next&=~(1<<dops[i].rt1);
4247       }
4248       break;
4249     case SHIFTIMM:
4250       smrv_strong_next&=~(1<<dops[i].rt1);
4251       smrv_weak_next&=~(1<<dops[i].rt1);
4252       // fallthrough
4253     case IMM16:
4254       if(dops[i].rt1&&is_const(&regs[i],dops[i].rt1)) {
4255         int hr = get_reg_w(regs[i].regmap, dops[i].rt1);
4256         u_int value;
4257         if(hr>=0) {
4258           if(get_final_value(hr,i,&value))
4259                smrv[dops[i].rt1]=value;
4260           else smrv[dops[i].rt1]=constmap[i][hr];
4261           smrv_strong_next|=1<<dops[i].rt1;
4262         }
4263       }
4264       else {
4265         if     ((smrv_strong>>dops[i].rs1)&1) speculate_mov(dops[i].rs1,dops[i].rt1);
4266         else if((smrv_weak>>dops[i].rs1)&1) speculate_mov_weak(dops[i].rs1,dops[i].rt1);
4267       }
4268       break;
4269     case LOAD:
4270       if(start<0x2000&&(dops[i].rt1==26||(smrv[dops[i].rt1]>>24)==0xa0)) {
4271         // special case for BIOS
4272         smrv[dops[i].rt1]=0xa0000000;
4273         smrv_strong_next|=1<<dops[i].rt1;
4274         break;
4275       }
4276       // fallthrough
4277     case SHIFT:
4278     case LOADLR:
4279     case MOV:
4280       smrv_strong_next&=~(1<<dops[i].rt1);
4281       smrv_weak_next&=~(1<<dops[i].rt1);
4282       break;
4283     case COP0:
4284     case COP2:
4285       if(dops[i].opcode2==0||dops[i].opcode2==2) { // MFC/CFC
4286         smrv_strong_next&=~(1<<dops[i].rt1);
4287         smrv_weak_next&=~(1<<dops[i].rt1);
4288       }
4289       break;
4290     case C2LS:
4291       if (dops[i].opcode==0x32) { // LWC2
4292         smrv_strong_next&=~(1<<dops[i].rt1);
4293         smrv_weak_next&=~(1<<dops[i].rt1);
4294       }
4295       break;
4296   }
4297 #if 0
4298   int r=4;
4299   printf("x %08x %08x %d %d c %08x %08x\n",smrv[r],start+i*4,
4300     ((smrv_strong>>r)&1),(smrv_weak>>r)&1,regs[i].isconst,regs[i].wasconst);
4301 #endif
4302 }
4303
4304 static void ujump_assemble(int i, const struct regstat *i_regs);
4305 static void rjump_assemble(int i, const struct regstat *i_regs);
4306 static void cjump_assemble(int i, const struct regstat *i_regs);
4307 static void sjump_assemble(int i, const struct regstat *i_regs);
4308
4309 static int assemble(int i, const struct regstat *i_regs, int ccadj_)
4310 {
4311   int ds = 0;
4312   switch (dops[i].itype) {
4313     case ALU:
4314       alu_assemble(i, i_regs, ccadj_);
4315       break;
4316     case IMM16:
4317       imm16_assemble(i, i_regs, ccadj_);
4318       break;
4319     case SHIFT:
4320       shift_assemble(i, i_regs);
4321       break;
4322     case SHIFTIMM:
4323       shiftimm_assemble(i, i_regs);
4324       break;
4325     case LOAD:
4326       load_assemble(i, i_regs, ccadj_);
4327       break;
4328     case LOADLR:
4329       loadlr_assemble(i, i_regs, ccadj_);
4330       break;
4331     case STORE:
4332       store_assemble(i, i_regs, ccadj_);
4333       break;
4334     case STORELR:
4335       storelr_assemble(i, i_regs, ccadj_);
4336       break;
4337     case COP0:
4338       cop0_assemble(i, i_regs, ccadj_);
4339       break;
4340     case RFE:
4341       rfe_assemble(i, i_regs);
4342       break;
4343     case COP2:
4344       cop2_assemble(i, i_regs);
4345       break;
4346     case C2LS:
4347       c2ls_assemble(i, i_regs, ccadj_);
4348       break;
4349     case C2OP:
4350       c2op_assemble(i, i_regs);
4351       break;
4352     case MULTDIV:
4353       multdiv_assemble(i, i_regs);
4354       multdiv_prepare_stall(i, i_regs, ccadj_);
4355       break;
4356     case MOV:
4357       mov_assemble(i, i_regs);
4358       break;
4359     case SYSCALL:
4360       exception_assemble(i, i_regs, ccadj_);
4361       break;
4362     case HLECALL:
4363       hlecall_assemble(i, i_regs, ccadj_);
4364       break;
4365     case INTCALL:
4366       intcall_assemble(i, i_regs, ccadj_);
4367       break;
4368     case UJUMP:
4369       ujump_assemble(i, i_regs);
4370       ds = 1;
4371       break;
4372     case RJUMP:
4373       rjump_assemble(i, i_regs);
4374       ds = 1;
4375       break;
4376     case CJUMP:
4377       cjump_assemble(i, i_regs);
4378       ds = 1;
4379       break;
4380     case SJUMP:
4381       sjump_assemble(i, i_regs);
4382       ds = 1;
4383       break;
4384     case NOP:
4385     case OTHER:
4386       // not handled, just skip
4387       break;
4388     default:
4389       assert(0);
4390   }
4391   return ds;
4392 }
4393
4394 static void ds_assemble(int i, const struct regstat *i_regs)
4395 {
4396   speculate_register_values(i);
4397   is_delayslot = 1;
4398   switch (dops[i].itype) {
4399     case SYSCALL:
4400     case HLECALL:
4401     case INTCALL:
4402     case UJUMP:
4403     case RJUMP:
4404     case CJUMP:
4405     case SJUMP:
4406       SysPrintf("Jump in the delay slot.  This is probably a bug.\n");
4407       break;
4408     default:
4409       assemble(i, i_regs, cinfo[i].ccadj);
4410   }
4411   is_delayslot = 0;
4412 }
4413
4414 // Is the branch target a valid internal jump?
4415 static int internal_branch(int addr)
4416 {
4417   if(addr&1) return 0; // Indirect (register) jump
4418   if(addr>=start && addr<start+slen*4-4)
4419   {
4420     return 1;
4421   }
4422   return 0;
4423 }
4424
4425 static void wb_invalidate(signed char pre[],signed char entry[],uint64_t dirty,uint64_t u)
4426 {
4427   int hr;
4428   for(hr=0;hr<HOST_REGS;hr++) {
4429     if(hr!=EXCLUDE_REG) {
4430       if(pre[hr]!=entry[hr]) {
4431         if(pre[hr]>=0) {
4432           if((dirty>>hr)&1) {
4433             if(get_reg(entry,pre[hr])<0) {
4434               assert(pre[hr]<64);
4435               if(!((u>>pre[hr])&1))
4436                 emit_storereg(pre[hr],hr);
4437             }
4438           }
4439         }
4440       }
4441     }
4442   }
4443   // Move from one register to another (no writeback)
4444   for(hr=0;hr<HOST_REGS;hr++) {
4445     if(hr!=EXCLUDE_REG) {
4446       if(pre[hr]!=entry[hr]) {
4447         if(pre[hr]>=0&&pre[hr]<TEMPREG) {
4448           int nr;
4449           if((nr=get_reg(entry,pre[hr]))>=0) {
4450             emit_mov(hr,nr);
4451           }
4452         }
4453       }
4454     }
4455   }
4456 }
4457
4458 // Load the specified registers
4459 // This only loads the registers given as arguments because
4460 // we don't want to load things that will be overwritten
4461 static inline void load_reg(signed char entry[], signed char regmap[], int rs)
4462 {
4463   int hr = get_reg(regmap, rs);
4464   if (hr >= 0 && entry[hr] != regmap[hr])
4465     emit_loadreg(regmap[hr], hr);
4466 }
4467
4468 static void load_regs(signed char entry[], signed char regmap[], int rs1, int rs2)
4469 {
4470   load_reg(entry, regmap, rs1);
4471   if (rs1 != rs2)
4472     load_reg(entry, regmap, rs2);
4473 }
4474
4475 // Load registers prior to the start of a loop
4476 // so that they are not loaded within the loop
4477 static void loop_preload(signed char pre[],signed char entry[])
4478 {
4479   int hr;
4480   for (hr = 0; hr < HOST_REGS; hr++) {
4481     int r = entry[hr];
4482     if (r >= 0 && pre[hr] != r && get_reg(pre, r) < 0) {
4483       assem_debug("loop preload:\n");
4484       if (r < TEMPREG)
4485         emit_loadreg(r, hr);
4486     }
4487   }
4488 }
4489
4490 // Generate address for load/store instruction
4491 // goes to AGEN (or temp) for writes, FTEMP for LOADLR and cop1/2 loads
4492 // AGEN is assigned by pass5b_preallocate2
4493 static void address_generation(int i, const struct regstat *i_regs, signed char entry[])
4494 {
4495   if (dops[i].is_load || dops[i].is_store) {
4496     int ra = -1;
4497     int agr = AGEN1 + (i&1);
4498     if(dops[i].itype==LOAD) {
4499       if (!dops[i].may_except)
4500         ra = get_reg_w(i_regs->regmap, dops[i].rt1); // reuse dest for agen
4501       if (ra < 0)
4502         ra = get_reg_temp(i_regs->regmap);
4503     }
4504     if(dops[i].itype==LOADLR) {
4505       ra=get_reg(i_regs->regmap,FTEMP);
4506     }
4507     if(dops[i].itype==STORE||dops[i].itype==STORELR) {
4508       ra=get_reg(i_regs->regmap,agr);
4509       if(ra<0) ra=get_reg_temp(i_regs->regmap);
4510     }
4511     if(dops[i].itype==C2LS) {
4512       if (dops[i].opcode == 0x32) // LWC2
4513         ra=get_reg(i_regs->regmap,FTEMP);
4514       else { // SWC2
4515         ra=get_reg(i_regs->regmap,agr);
4516         if(ra<0) ra=get_reg_temp(i_regs->regmap);
4517       }
4518     }
4519     int rs = get_reg(i_regs->regmap, dops[i].rs1);
4520     //if(ra>=0)
4521     {
4522       int offset = cinfo[i].imm;
4523       int add_offset = offset != 0;
4524       int c = rs >= 0 && ((i_regs->wasconst >> rs) & 1);
4525       if(dops[i].rs1==0) {
4526         // Using r0 as a base address
4527         assert(ra >= 0);
4528         if(!entry||entry[ra]!=agr) {
4529           if (dops[i].opcode==0x22||dops[i].opcode==0x26) {
4530             emit_movimm(offset&0xFFFFFFFC,ra); // LWL/LWR
4531           }else{
4532             emit_movimm(offset,ra);
4533           }
4534         } // else did it in the previous cycle
4535         cinfo[i].addr = ra;
4536         add_offset = 0;
4537       }
4538       else if (rs < 0) {
4539         assert(ra >= 0);
4540         if (!entry || entry[ra] != dops[i].rs1)
4541           emit_loadreg(dops[i].rs1, ra);
4542         cinfo[i].addr = ra;
4543         //if(!entry||entry[ra]!=dops[i].rs1)
4544         //  printf("poor load scheduling!\n");
4545       }
4546       else if(c) {
4547         if(dops[i].rs1!=dops[i].rt1||dops[i].itype!=LOAD) {
4548           assert(ra >= 0);
4549           if(!entry||entry[ra]!=agr) {
4550             if (dops[i].opcode==0x22||dops[i].opcode==0x26) {
4551               emit_movimm((constmap[i][rs]+offset)&0xFFFFFFFC,ra); // LWL/LWR
4552             }else{
4553               emit_movimm(constmap[i][rs]+offset,ra);
4554               regs[i].loadedconst|=1<<ra;
4555             }
4556           } // else did it in the previous cycle
4557           cinfo[i].addr = ra;
4558         }
4559         else // else load_consts already did it
4560           cinfo[i].addr = rs;
4561         add_offset = 0;
4562       }
4563       else
4564         cinfo[i].addr = rs;
4565       if (add_offset) {
4566         assert(ra >= 0);
4567         if(rs>=0) {
4568           emit_addimm(rs,offset,ra);
4569         }else{
4570           emit_addimm(ra,offset,ra);
4571         }
4572         cinfo[i].addr = ra;
4573       }
4574     }
4575     assert(cinfo[i].addr >= 0);
4576   }
4577   // Preload constants for next instruction
4578   if (dops[i+1].is_load || dops[i+1].is_store) {
4579     int agr,ra;
4580     // Actual address
4581     agr=AGEN1+((i+1)&1);
4582     ra=get_reg(i_regs->regmap,agr);
4583     if(ra>=0) {
4584       int rs=get_reg(regs[i+1].regmap,dops[i+1].rs1);
4585       int offset=cinfo[i+1].imm;
4586       int c=(regs[i+1].wasconst>>rs)&1;
4587       if(c&&(dops[i+1].rs1!=dops[i+1].rt1||dops[i+1].itype!=LOAD)) {
4588         if (dops[i+1].opcode==0x22||dops[i+1].opcode==0x26) {
4589           emit_movimm((constmap[i+1][rs]+offset)&0xFFFFFFFC,ra); // LWL/LWR
4590         }else if (dops[i+1].opcode==0x1a||dops[i+1].opcode==0x1b) {
4591           emit_movimm((constmap[i+1][rs]+offset)&0xFFFFFFF8,ra); // LDL/LDR
4592         }else{
4593           emit_movimm(constmap[i+1][rs]+offset,ra);
4594           regs[i+1].loadedconst|=1<<ra;
4595         }
4596       }
4597       else if(dops[i+1].rs1==0) {
4598         // Using r0 as a base address
4599         if (dops[i+1].opcode==0x22||dops[i+1].opcode==0x26) {
4600           emit_movimm(offset&0xFFFFFFFC,ra); // LWL/LWR
4601         }else if (dops[i+1].opcode==0x1a||dops[i+1].opcode==0x1b) {
4602           emit_movimm(offset&0xFFFFFFF8,ra); // LDL/LDR
4603         }else{
4604           emit_movimm(offset,ra);
4605         }
4606       }
4607     }
4608   }
4609 }
4610
4611 static int get_final_value(int hr, int i, u_int *value)
4612 {
4613   int reg=regs[i].regmap[hr];
4614   while(i<slen-1) {
4615     if(regs[i+1].regmap[hr]!=reg) break;
4616     if(!((regs[i+1].isconst>>hr)&1)) break;
4617     if(dops[i+1].bt) break;
4618     i++;
4619   }
4620   if(i<slen-1) {
4621     if (dops[i].is_jump) {
4622       *value=constmap[i][hr];
4623       return 1;
4624     }
4625     if(!dops[i+1].bt) {
4626       if (dops[i+1].is_jump) {
4627         // Load in delay slot, out-of-order execution
4628         if(dops[i+2].itype==LOAD&&dops[i+2].rs1==reg&&dops[i+2].rt1==reg&&((regs[i+1].wasconst>>hr)&1))
4629         {
4630           // Precompute load address
4631           *value=constmap[i][hr]+cinfo[i+2].imm;
4632           return 1;
4633         }
4634       }
4635       if(dops[i+1].itype==LOAD&&dops[i+1].rs1==reg&&dops[i+1].rt1==reg)
4636       {
4637         // Precompute load address
4638         *value=constmap[i][hr]+cinfo[i+1].imm;
4639         //printf("c=%x imm=%lx\n",(long)constmap[i][hr],cinfo[i+1].imm);
4640         return 1;
4641       }
4642     }
4643   }
4644   *value=constmap[i][hr];
4645   //printf("c=%lx\n",(long)constmap[i][hr]);
4646   if(i==slen-1) return 1;
4647   assert(reg < 64);
4648   return !((unneeded_reg[i+1]>>reg)&1);
4649 }
4650
4651 // Load registers with known constants
4652 static void load_consts(signed char pre[],signed char regmap[],int i)
4653 {
4654   int hr,hr2;
4655   // propagate loaded constant flags
4656   if(i==0||dops[i].bt)
4657     regs[i].loadedconst=0;
4658   else {
4659     for (hr = 0; hr < HOST_REGS; hr++) {
4660       if (hr == EXCLUDE_REG || regmap[hr] < 0 || pre[hr] != regmap[hr])
4661         continue;
4662       if ((((regs[i-1].isconst & regs[i-1].loadedconst) >> hr) & 1)
4663           && regmap[hr] == regs[i-1].regmap[hr])
4664       {
4665         regs[i].loadedconst |= 1u << hr;
4666       }
4667     }
4668   }
4669   // Load 32-bit regs
4670   for(hr=0;hr<HOST_REGS;hr++) {
4671     if(hr!=EXCLUDE_REG&&regmap[hr]>=0) {
4672       //if(entry[hr]!=regmap[hr]) {
4673       if(!((regs[i].loadedconst>>hr)&1)) {
4674         assert(regmap[hr]<64);
4675         if(((regs[i].isconst>>hr)&1)&&regmap[hr]>0) {
4676           u_int value, similar=0;
4677           if(get_final_value(hr,i,&value)) {
4678             // see if some other register has similar value
4679             for(hr2=0;hr2<HOST_REGS;hr2++) {
4680               if(hr2!=EXCLUDE_REG&&((regs[i].loadedconst>>hr2)&1)) {
4681                 if(is_similar_value(value,constmap[i][hr2])) {
4682                   similar=1;
4683                   break;
4684                 }
4685               }
4686             }
4687             if(similar) {
4688               u_int value2;
4689               if(get_final_value(hr2,i,&value2)) // is this needed?
4690                 emit_movimm_from(value2,hr2,value,hr);
4691               else
4692                 emit_movimm(value,hr);
4693             }
4694             else if(value==0) {
4695               emit_zeroreg(hr);
4696             }
4697             else {
4698               emit_movimm(value,hr);
4699             }
4700           }
4701           regs[i].loadedconst|=1<<hr;
4702         }
4703       }
4704     }
4705   }
4706 }
4707
4708 static void load_all_consts(const signed char regmap[], u_int dirty, int i)
4709 {
4710   int hr;
4711   // Load 32-bit regs
4712   for(hr=0;hr<HOST_REGS;hr++) {
4713     if(hr!=EXCLUDE_REG&&regmap[hr]>=0&&((dirty>>hr)&1)) {
4714       assert(regmap[hr] < 64);
4715       if(((regs[i].isconst>>hr)&1)&&regmap[hr]>0) {
4716         int value=constmap[i][hr];
4717         if(value==0) {
4718           emit_zeroreg(hr);
4719         }
4720         else {
4721           emit_movimm(value,hr);
4722         }
4723       }
4724     }
4725   }
4726 }
4727
4728 // Write out all dirty registers (except cycle count)
4729 #ifndef wb_dirtys
4730 static void wb_dirtys(const signed char i_regmap[], u_int i_dirty)
4731 {
4732   int hr;
4733   for(hr=0;hr<HOST_REGS;hr++) {
4734     if(hr!=EXCLUDE_REG) {
4735       if(i_regmap[hr]>0) {
4736         if(i_regmap[hr]!=CCREG) {
4737           if((i_dirty>>hr)&1) {
4738             assert(i_regmap[hr]<64);
4739             emit_storereg(i_regmap[hr],hr);
4740           }
4741         }
4742       }
4743     }
4744   }
4745 }
4746 #endif
4747
4748 // Write out dirty registers that we need to reload (pair with load_needed_regs)
4749 // This writes the registers not written by store_regs_bt
4750 static void wb_needed_dirtys(const signed char i_regmap[], u_int i_dirty, int addr)
4751 {
4752   int hr;
4753   int t=(addr-start)>>2;
4754   for(hr=0;hr<HOST_REGS;hr++) {
4755     if(hr!=EXCLUDE_REG) {
4756       if(i_regmap[hr]>0) {
4757         if(i_regmap[hr]!=CCREG) {
4758           if(i_regmap[hr]==regs[t].regmap_entry[hr] && ((regs[t].dirty>>hr)&1)) {
4759             if((i_dirty>>hr)&1) {
4760               assert(i_regmap[hr]<64);
4761               emit_storereg(i_regmap[hr],hr);
4762             }
4763           }
4764         }
4765       }
4766     }
4767   }
4768 }
4769
4770 // Load all registers (except cycle count)
4771 #ifndef load_all_regs
4772 static void load_all_regs(const signed char i_regmap[])
4773 {
4774   int hr;
4775   for(hr=0;hr<HOST_REGS;hr++) {
4776     if(hr!=EXCLUDE_REG) {
4777       if(i_regmap[hr]==0) {
4778         emit_zeroreg(hr);
4779       }
4780       else
4781       if(i_regmap[hr]>0 && i_regmap[hr]<TEMPREG && i_regmap[hr]!=CCREG)
4782       {
4783         emit_loadreg(i_regmap[hr],hr);
4784       }
4785     }
4786   }
4787 }
4788 #endif
4789
4790 // Load all current registers also needed by next instruction
4791 static void load_needed_regs(const signed char i_regmap[], const signed char next_regmap[])
4792 {
4793   signed char regmap_sel[HOST_REGS];
4794   int hr;
4795   for (hr = 0; hr < HOST_REGS; hr++) {
4796     regmap_sel[hr] = -1;
4797     if (hr != EXCLUDE_REG)
4798       if (next_regmap[hr] == i_regmap[hr] || get_reg(next_regmap, i_regmap[hr]) >= 0)
4799         regmap_sel[hr] = i_regmap[hr];
4800   }
4801   load_all_regs(regmap_sel);
4802 }
4803
4804 // Load all regs, storing cycle count if necessary
4805 static void load_regs_entry(int t)
4806 {
4807   if(dops[t].is_ds) emit_addimm(HOST_CCREG,CLOCK_ADJUST(1),HOST_CCREG);
4808   else if(cinfo[t].ccadj) emit_addimm(HOST_CCREG,-cinfo[t].ccadj,HOST_CCREG);
4809   if(regs[t].regmap_entry[HOST_CCREG]!=CCREG) {
4810     emit_storereg(CCREG,HOST_CCREG);
4811   }
4812   load_all_regs(regs[t].regmap_entry);
4813 }
4814
4815 // Store dirty registers prior to branch
4816 static void store_regs_bt(signed char i_regmap[],uint64_t i_dirty,int addr)
4817 {
4818   if(internal_branch(addr))
4819   {
4820     int t=(addr-start)>>2;
4821     int hr;
4822     for(hr=0;hr<HOST_REGS;hr++) {
4823       if(hr!=EXCLUDE_REG) {
4824         if(i_regmap[hr]>0 && i_regmap[hr]!=CCREG) {
4825           if(i_regmap[hr]!=regs[t].regmap_entry[hr] || !((regs[t].dirty>>hr)&1)) {
4826             if((i_dirty>>hr)&1) {
4827               assert(i_regmap[hr]<64);
4828               if(!((unneeded_reg[t]>>i_regmap[hr])&1))
4829                 emit_storereg(i_regmap[hr],hr);
4830             }
4831           }
4832         }
4833       }
4834     }
4835   }
4836   else
4837   {
4838     // Branch out of this block, write out all dirty regs
4839     wb_dirtys(i_regmap,i_dirty);
4840   }
4841 }
4842
4843 // Load all needed registers for branch target
4844 static void load_regs_bt(signed char i_regmap[],uint64_t i_dirty,int addr)
4845 {
4846   //if(addr>=start && addr<(start+slen*4))
4847   if(internal_branch(addr))
4848   {
4849     int t=(addr-start)>>2;
4850     int hr;
4851     // Store the cycle count before loading something else
4852     if(i_regmap[HOST_CCREG]!=CCREG) {
4853       assert(i_regmap[HOST_CCREG]==-1);
4854     }
4855     if(regs[t].regmap_entry[HOST_CCREG]!=CCREG) {
4856       emit_storereg(CCREG,HOST_CCREG);
4857     }
4858     // Load 32-bit regs
4859     for(hr=0;hr<HOST_REGS;hr++) {
4860       if(hr!=EXCLUDE_REG&&regs[t].regmap_entry[hr]>=0&&regs[t].regmap_entry[hr]<TEMPREG) {
4861         if(i_regmap[hr]!=regs[t].regmap_entry[hr]) {
4862           if(regs[t].regmap_entry[hr]==0) {
4863             emit_zeroreg(hr);
4864           }
4865           else if(regs[t].regmap_entry[hr]!=CCREG)
4866           {
4867             emit_loadreg(regs[t].regmap_entry[hr],hr);
4868           }
4869         }
4870       }
4871     }
4872   }
4873 }
4874
4875 static int match_bt(signed char i_regmap[],uint64_t i_dirty,int addr)
4876 {
4877   if(addr>=start && addr<start+slen*4-4)
4878   {
4879     int t=(addr-start)>>2;
4880     int hr;
4881     if(regs[t].regmap_entry[HOST_CCREG]!=CCREG) return 0;
4882     for(hr=0;hr<HOST_REGS;hr++)
4883     {
4884       if(hr!=EXCLUDE_REG)
4885       {
4886         if(i_regmap[hr]!=regs[t].regmap_entry[hr])
4887         {
4888           if(regs[t].regmap_entry[hr]>=0&&(regs[t].regmap_entry[hr]|64)<TEMPREG+64)
4889           {
4890             return 0;
4891           }
4892           else
4893           if((i_dirty>>hr)&1)
4894           {
4895             if(i_regmap[hr]<TEMPREG)
4896             {
4897               if(!((unneeded_reg[t]>>i_regmap[hr])&1))
4898                 return 0;
4899             }
4900             else if(i_regmap[hr]>=64&&i_regmap[hr]<TEMPREG+64)
4901             {
4902               assert(0);
4903             }
4904           }
4905         }
4906         else // Same register but is it 32-bit or dirty?
4907         if(i_regmap[hr]>=0)
4908         {
4909           if(!((regs[t].dirty>>hr)&1))
4910           {
4911             if((i_dirty>>hr)&1)
4912             {
4913               if(!((unneeded_reg[t]>>i_regmap[hr])&1))
4914               {
4915                 //printf("%x: dirty no match\n",addr);
4916                 return 0;
4917               }
4918             }
4919           }
4920         }
4921       }
4922     }
4923     // Delay slots are not valid branch targets
4924     //if(t>0&&(dops[t-1].is_jump) return 0;
4925     // Delay slots require additional processing, so do not match
4926     if(dops[t].is_ds) return 0;
4927   }
4928   else
4929   {
4930     int hr;
4931     for(hr=0;hr<HOST_REGS;hr++)
4932     {
4933       if(hr!=EXCLUDE_REG)
4934       {
4935         if(i_regmap[hr]>=0)
4936         {
4937           if(hr!=HOST_CCREG||i_regmap[hr]!=CCREG)
4938           {
4939             if((i_dirty>>hr)&1)
4940             {
4941               return 0;
4942             }
4943           }
4944         }
4945       }
4946     }
4947   }
4948   return 1;
4949 }
4950
4951 #ifdef DRC_DBG
4952 static void drc_dbg_emit_do_cmp(int i, int ccadj_)
4953 {
4954   extern void do_insn_cmp();
4955   //extern int cycle;
4956   u_int hr, reglist = get_host_reglist(regs[i].regmap);
4957   reglist |= get_host_reglist(regs[i].regmap_entry);
4958   reglist &= DRC_DBG_REGMASK;
4959
4960   assem_debug("//do_insn_cmp %08x\n", start+i*4);
4961   save_regs(reglist);
4962   // write out changed consts to match the interpreter
4963   if (i > 0 && !dops[i].bt) {
4964     for (hr = 0; hr < HOST_REGS; hr++) {
4965       int reg = regs[i].regmap_entry[hr]; // regs[i-1].regmap[hr];
4966       if (hr == EXCLUDE_REG || reg <= 0)
4967         continue;
4968       if (!((regs[i-1].isconst >> hr) & 1))
4969         continue;
4970       if (i > 1 && reg == regs[i-2].regmap[hr] && constmap[i-1][hr] == constmap[i-2][hr])
4971         continue;
4972       emit_movimm(constmap[i-1][hr],0);
4973       emit_storereg(reg, 0);
4974     }
4975   }
4976   if (dops[i].opcode == 0x0f) { // LUI
4977     emit_movimm(cinfo[i].imm << 16, 0);
4978     emit_storereg(dops[i].rt1, 0);
4979   }
4980   emit_movimm(start+i*4,0);
4981   emit_writeword(0,&pcaddr);
4982   int cc = get_reg(regs[i].regmap_entry, CCREG);
4983   if (cc < 0)
4984     emit_loadreg(CCREG, cc = 0);
4985   emit_addimm(cc, ccadj_, 0);
4986   emit_writeword(0, &psxRegs.cycle);
4987   emit_far_call(do_insn_cmp);
4988   //emit_readword(&cycle,0);
4989   //emit_addimm(0,2,0);
4990   //emit_writeword(0,&cycle);
4991   (void)get_reg2;
4992   restore_regs(reglist);
4993   assem_debug("\\\\do_insn_cmp\n");
4994 }
4995 static void drc_dbg_emit_wb_dirtys(int i, const struct regstat *i_regs)
4996 {
4997   // write-out non-consts, consts are likely different because of get_final_value()
4998   if (i_regs->dirty & ~i_regs->loadedconst) {
4999     assem_debug("/ drc_dbg_wb\n");
5000     wb_dirtys(i_regs->regmap, i_regs->dirty & ~i_regs->loadedconst);
5001     assem_debug("\\ drc_dbg_wb\n");
5002   }
5003 }
5004 #else
5005 #define drc_dbg_emit_do_cmp(x,y)
5006 #define drc_dbg_emit_wb_dirtys(x,y)
5007 #endif
5008
5009 // Used when a branch jumps into the delay slot of another branch
5010 static void ds_assemble_entry(int i)
5011 {
5012   int t = (cinfo[i].ba - start) >> 2;
5013   int ccadj_ = -CLOCK_ADJUST(1);
5014   if (!instr_addr[t])
5015     instr_addr[t] = out;
5016   assem_debug("Assemble delay slot at %x\n",cinfo[i].ba);
5017   assem_debug("<->\n");
5018   drc_dbg_emit_do_cmp(t, ccadj_);
5019   if(regs[t].regmap_entry[HOST_CCREG]==CCREG&&regs[t].regmap[HOST_CCREG]!=CCREG)
5020     wb_register(CCREG,regs[t].regmap_entry,regs[t].wasdirty);
5021   load_regs(regs[t].regmap_entry,regs[t].regmap,dops[t].rs1,dops[t].rs2);
5022   address_generation(t,&regs[t],regs[t].regmap_entry);
5023   if (ram_offset && (dops[t].is_load || dops[t].is_store))
5024     load_reg(regs[t].regmap_entry,regs[t].regmap,ROREG);
5025   if (dops[t].is_store)
5026     load_reg(regs[t].regmap_entry,regs[t].regmap,INVCP);
5027   is_delayslot=0;
5028   switch (dops[t].itype) {
5029     case SYSCALL:
5030     case HLECALL:
5031     case INTCALL:
5032     case UJUMP:
5033     case RJUMP:
5034     case CJUMP:
5035     case SJUMP:
5036       SysPrintf("Jump in the delay slot.  This is probably a bug.\n");
5037       break;
5038     default:
5039       assemble(t, &regs[t], ccadj_);
5040   }
5041   store_regs_bt(regs[t].regmap,regs[t].dirty,cinfo[i].ba+4);
5042   load_regs_bt(regs[t].regmap,regs[t].dirty,cinfo[i].ba+4);
5043   if(internal_branch(cinfo[i].ba+4))
5044     assem_debug("branch: internal\n");
5045   else
5046     assem_debug("branch: external\n");
5047   assert(internal_branch(cinfo[i].ba+4));
5048   add_to_linker(out,cinfo[i].ba+4,internal_branch(cinfo[i].ba+4));
5049   emit_jmp(0);
5050 }
5051
5052 // Load 2 immediates optimizing for small code size
5053 static void emit_mov2imm_compact(int imm1,u_int rt1,int imm2,u_int rt2)
5054 {
5055   emit_movimm(imm1,rt1);
5056   emit_movimm_from(imm1,rt1,imm2,rt2);
5057 }
5058
5059 static void do_cc(int i, const signed char i_regmap[], int *adj,
5060   int addr, int taken, int invert)
5061 {
5062   int count, count_plus2;
5063   void *jaddr;
5064   void *idle=NULL;
5065   int t=0;
5066   if(dops[i].itype==RJUMP)
5067   {
5068     *adj=0;
5069   }
5070   //if(cinfo[i].ba>=start && cinfo[i].ba<(start+slen*4))
5071   if(internal_branch(cinfo[i].ba))
5072   {
5073     t=(cinfo[i].ba-start)>>2;
5074     if(dops[t].is_ds) *adj=-CLOCK_ADJUST(1); // Branch into delay slot adds an extra cycle
5075     else *adj=cinfo[t].ccadj;
5076   }
5077   else
5078   {
5079     *adj=0;
5080   }
5081   count = cinfo[i].ccadj;
5082   count_plus2 = count + CLOCK_ADJUST(2);
5083   if(taken==TAKEN && i==(cinfo[i].ba-start)>>2 && source[i+1]==0) {
5084     // Idle loop
5085     if(count&1) emit_addimm_and_set_flags(2*(count+2),HOST_CCREG);
5086     idle=out;
5087     //emit_subfrommem(&idlecount,HOST_CCREG); // Count idle cycles
5088     emit_andimm(HOST_CCREG,3,HOST_CCREG);
5089     jaddr=out;
5090     emit_jmp(0);
5091   }
5092   else if(*adj==0||invert) {
5093     int cycles = count_plus2;
5094     // faster loop HACK
5095 #if 0
5096     if (t&&*adj) {
5097       int rel=t-i;
5098       if(-NO_CYCLE_PENALTY_THR<rel&&rel<0)
5099         cycles=*adj+count+2-*adj;
5100     }
5101 #endif
5102     emit_addimm_and_set_flags(cycles, HOST_CCREG);
5103     jaddr = out;
5104     emit_jns(0);
5105   }
5106   else
5107   {
5108     emit_cmpimm(HOST_CCREG, -count_plus2);
5109     jaddr = out;
5110     emit_jns(0);
5111   }
5112   add_stub(CC_STUB,jaddr,idle?idle:out,(*adj==0||invert||idle)?0:count_plus2,i,addr,taken,0);
5113 }
5114
5115 static void do_ccstub(int n)
5116 {
5117   literal_pool(256);
5118   assem_debug("do_ccstub %x\n",start+(u_int)stubs[n].b*4);
5119   set_jump_target(stubs[n].addr, out);
5120   int i=stubs[n].b;
5121   if (stubs[n].d != TAKEN) {
5122     wb_dirtys(branch_regs[i].regmap,branch_regs[i].dirty);
5123   }
5124   else {
5125     if(internal_branch(cinfo[i].ba))
5126       wb_needed_dirtys(branch_regs[i].regmap,branch_regs[i].dirty,cinfo[i].ba);
5127   }
5128   if(stubs[n].c!=-1)
5129   {
5130     // Save PC as return address
5131     emit_movimm(stubs[n].c,0);
5132     emit_writeword(0,&pcaddr);
5133   }
5134   else
5135   {
5136     // Return address depends on which way the branch goes
5137     if(dops[i].itype==CJUMP||dops[i].itype==SJUMP)
5138     {
5139       int s1l=get_reg(branch_regs[i].regmap,dops[i].rs1);
5140       int s2l=get_reg(branch_regs[i].regmap,dops[i].rs2);
5141       if(dops[i].rs1==0)
5142       {
5143         s1l=s2l;
5144         s2l=-1;
5145       }
5146       else if(dops[i].rs2==0)
5147       {
5148         s2l=-1;
5149       }
5150       assert(s1l>=0);
5151       #ifdef DESTRUCTIVE_WRITEBACK
5152       if(dops[i].rs1) {
5153         if((branch_regs[i].dirty>>s1l)&&1)
5154           emit_loadreg(dops[i].rs1,s1l);
5155       }
5156       else {
5157         if((branch_regs[i].dirty>>s1l)&1)
5158           emit_loadreg(dops[i].rs2,s1l);
5159       }
5160       if(s2l>=0)
5161         if((branch_regs[i].dirty>>s2l)&1)
5162           emit_loadreg(dops[i].rs2,s2l);
5163       #endif
5164       int hr=0;
5165       int addr=-1,alt=-1,ntaddr=-1;
5166       while(hr<HOST_REGS)
5167       {
5168         if(hr!=EXCLUDE_REG && hr!=HOST_CCREG &&
5169            branch_regs[i].regmap[hr]!=dops[i].rs1 &&
5170            branch_regs[i].regmap[hr]!=dops[i].rs2 )
5171         {
5172           addr=hr++;break;
5173         }
5174         hr++;
5175       }
5176       while(hr<HOST_REGS)
5177       {
5178         if(hr!=EXCLUDE_REG && hr!=HOST_CCREG &&
5179            branch_regs[i].regmap[hr]!=dops[i].rs1 &&
5180            branch_regs[i].regmap[hr]!=dops[i].rs2 )
5181         {
5182           alt=hr++;break;
5183         }
5184         hr++;
5185       }
5186       if ((dops[i].opcode & 0x3e) == 6) // BLEZ/BGTZ needs another register
5187       {
5188         while(hr<HOST_REGS)
5189         {
5190           if(hr!=EXCLUDE_REG && hr!=HOST_CCREG &&
5191              branch_regs[i].regmap[hr]!=dops[i].rs1 &&
5192              branch_regs[i].regmap[hr]!=dops[i].rs2 )
5193           {
5194             ntaddr=hr;break;
5195           }
5196           hr++;
5197         }
5198         assert(hr<HOST_REGS);
5199       }
5200       if (dops[i].opcode == 4) // BEQ
5201       {
5202         #ifdef HAVE_CMOV_IMM
5203         if(s2l>=0) emit_cmp(s1l,s2l);
5204         else emit_test(s1l,s1l);
5205         emit_cmov2imm_e_ne_compact(cinfo[i].ba,start+i*4+8,addr);
5206         #else
5207         emit_mov2imm_compact(cinfo[i].ba,addr,start+i*4+8,alt);
5208         if(s2l>=0) emit_cmp(s1l,s2l);
5209         else emit_test(s1l,s1l);
5210         emit_cmovne_reg(alt,addr);
5211         #endif
5212       }
5213       else if (dops[i].opcode == 5) // BNE
5214       {
5215         #ifdef HAVE_CMOV_IMM
5216         if(s2l>=0) emit_cmp(s1l,s2l);
5217         else emit_test(s1l,s1l);
5218         emit_cmov2imm_e_ne_compact(start+i*4+8,cinfo[i].ba,addr);
5219         #else
5220         emit_mov2imm_compact(start+i*4+8,addr,cinfo[i].ba,alt);
5221         if(s2l>=0) emit_cmp(s1l,s2l);
5222         else emit_test(s1l,s1l);
5223         emit_cmovne_reg(alt,addr);
5224         #endif
5225       }
5226       else if (dops[i].opcode == 6) // BLEZ
5227       {
5228         //emit_movimm(cinfo[i].ba,alt);
5229         //emit_movimm(start+i*4+8,addr);
5230         emit_mov2imm_compact(cinfo[i].ba,alt,start+i*4+8,addr);
5231         emit_cmpimm(s1l,1);
5232         emit_cmovl_reg(alt,addr);
5233       }
5234       else if (dops[i].opcode == 7) // BGTZ
5235       {
5236         //emit_movimm(cinfo[i].ba,addr);
5237         //emit_movimm(start+i*4+8,ntaddr);
5238         emit_mov2imm_compact(cinfo[i].ba,addr,start+i*4+8,ntaddr);
5239         emit_cmpimm(s1l,1);
5240         emit_cmovl_reg(ntaddr,addr);
5241       }
5242       else if (dops[i].itype == SJUMP) // BLTZ/BGEZ
5243       {
5244         //emit_movimm(cinfo[i].ba,alt);
5245         //emit_movimm(start+i*4+8,addr);
5246         if (dops[i].rs1) {
5247           emit_mov2imm_compact(cinfo[i].ba,
5248             (dops[i].opcode2 & 1) ? addr : alt, start + i*4 + 8,
5249             (dops[i].opcode2 & 1) ? alt : addr);
5250           emit_test(s1l,s1l);
5251           emit_cmovs_reg(alt,addr);
5252         }
5253         else
5254           emit_movimm((dops[i].opcode2 & 1) ? cinfo[i].ba : start + i*4 + 8, addr);
5255       }
5256       emit_writeword(addr, &pcaddr);
5257     }
5258     else
5259     if(dops[i].itype==RJUMP)
5260     {
5261       int r=get_reg(branch_regs[i].regmap,dops[i].rs1);
5262       if (ds_writes_rjump_rs(i)) {
5263         r=get_reg(branch_regs[i].regmap,RTEMP);
5264       }
5265       emit_writeword(r,&pcaddr);
5266     }
5267     else {SysPrintf("Unknown branch type in do_ccstub\n");abort();}
5268   }
5269   // Update cycle count
5270   assert(branch_regs[i].regmap[HOST_CCREG]==CCREG||branch_regs[i].regmap[HOST_CCREG]==-1);
5271   if(stubs[n].a) emit_addimm(HOST_CCREG,(int)stubs[n].a,HOST_CCREG);
5272   emit_far_call(cc_interrupt);
5273   if(stubs[n].a) emit_addimm(HOST_CCREG,-(int)stubs[n].a,HOST_CCREG);
5274   if(stubs[n].d==TAKEN) {
5275     if(internal_branch(cinfo[i].ba))
5276       load_needed_regs(branch_regs[i].regmap,regs[(cinfo[i].ba-start)>>2].regmap_entry);
5277     else if(dops[i].itype==RJUMP) {
5278       if(get_reg(branch_regs[i].regmap,RTEMP)>=0)
5279         emit_readword(&pcaddr,get_reg(branch_regs[i].regmap,RTEMP));
5280       else
5281         emit_loadreg(dops[i].rs1,get_reg(branch_regs[i].regmap,dops[i].rs1));
5282     }
5283   }else if(stubs[n].d==NOTTAKEN) {
5284     if(i<slen-2) load_needed_regs(branch_regs[i].regmap,regmap_pre[i+2]);
5285     else load_all_regs(branch_regs[i].regmap);
5286   }else{
5287     load_all_regs(branch_regs[i].regmap);
5288   }
5289   if (stubs[n].retaddr)
5290     emit_jmp(stubs[n].retaddr);
5291   else
5292     do_jump_vaddr(stubs[n].e);
5293 }
5294
5295 static void add_to_linker(void *addr, u_int target, int is_internal)
5296 {
5297   assert(linkcount < ARRAY_SIZE(link_addr));
5298   link_addr[linkcount].addr = addr;
5299   link_addr[linkcount].target = target;
5300   link_addr[linkcount].internal = is_internal;
5301   linkcount++;
5302 }
5303
5304 static void ujump_assemble_write_ra(int i)
5305 {
5306   int rt;
5307   unsigned int return_address;
5308   rt=get_reg(branch_regs[i].regmap,31);
5309   //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]);
5310   //assert(rt>=0);
5311   return_address=start+i*4+8;
5312   if(rt>=0) {
5313     #ifdef USE_MINI_HT
5314     if(internal_branch(return_address)&&dops[i+1].rt1!=31) {
5315       int temp=-1; // note: must be ds-safe
5316       #ifdef HOST_TEMPREG
5317       temp=HOST_TEMPREG;
5318       #endif
5319       if(temp>=0) do_miniht_insert(return_address,rt,temp);
5320       else emit_movimm(return_address,rt);
5321     }
5322     else
5323     #endif
5324     {
5325       #ifdef REG_PREFETCH
5326       if(temp>=0)
5327       {
5328         if(i_regmap[temp]!=PTEMP) emit_movimm((uintptr_t)hash_table_get(return_address),temp);
5329       }
5330       #endif
5331       if (!((regs[i].loadedconst >> rt) & 1))
5332         emit_movimm(return_address, rt); // PC into link register
5333       #ifdef IMM_PREFETCH
5334       emit_prefetch(hash_table_get(return_address));
5335       #endif
5336     }
5337   }
5338 }
5339
5340 static void ujump_assemble(int i, const struct regstat *i_regs)
5341 {
5342   if(i==(cinfo[i].ba-start)>>2) assem_debug("idle loop\n");
5343   address_generation(i+1,i_regs,regs[i].regmap_entry);
5344   #ifdef REG_PREFETCH
5345   int temp=get_reg(branch_regs[i].regmap,PTEMP);
5346   if(dops[i].rt1==31&&temp>=0)
5347   {
5348     signed char *i_regmap=i_regs->regmap;
5349     int return_address=start+i*4+8;
5350     if(get_reg(branch_regs[i].regmap,31)>0)
5351     if(i_regmap[temp]==PTEMP) emit_movimm((uintptr_t)hash_table_get(return_address),temp);
5352   }
5353   #endif
5354   if (dops[i].rt1 == 31)
5355     ujump_assemble_write_ra(i); // writeback ra for DS
5356   ds_assemble(i+1,i_regs);
5357   uint64_t bc_unneeded=branch_regs[i].u;
5358   bc_unneeded|=1|(1LL<<dops[i].rt1);
5359   wb_invalidate(regs[i].regmap,branch_regs[i].regmap,regs[i].dirty,bc_unneeded);
5360   load_reg(regs[i].regmap,branch_regs[i].regmap,CCREG);
5361   int cc,adj;
5362   cc=get_reg(branch_regs[i].regmap,CCREG);
5363   assert(cc==HOST_CCREG);
5364   store_regs_bt(branch_regs[i].regmap,branch_regs[i].dirty,cinfo[i].ba);
5365   #ifdef REG_PREFETCH
5366   if(dops[i].rt1==31&&temp>=0) emit_prefetchreg(temp);
5367   #endif
5368   do_cc(i,branch_regs[i].regmap,&adj,cinfo[i].ba,TAKEN,0);
5369   if(adj) emit_addimm(cc, cinfo[i].ccadj + CLOCK_ADJUST(2) - adj, cc);
5370   load_regs_bt(branch_regs[i].regmap,branch_regs[i].dirty,cinfo[i].ba);
5371   if(internal_branch(cinfo[i].ba))
5372     assem_debug("branch: internal\n");
5373   else
5374     assem_debug("branch: external\n");
5375   if (internal_branch(cinfo[i].ba) && dops[(cinfo[i].ba-start)>>2].is_ds) {
5376     ds_assemble_entry(i);
5377   }
5378   else {
5379     add_to_linker(out,cinfo[i].ba,internal_branch(cinfo[i].ba));
5380     emit_jmp(0);
5381   }
5382 }
5383
5384 static void rjump_assemble_write_ra(int i)
5385 {
5386   int rt,return_address;
5387   rt=get_reg_w(branch_regs[i].regmap, dops[i].rt1);
5388   //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]);
5389   assert(rt>=0);
5390   return_address=start+i*4+8;
5391   #ifdef REG_PREFETCH
5392   if(temp>=0)
5393   {
5394     if(i_regmap[temp]!=PTEMP) emit_movimm((uintptr_t)hash_table_get(return_address),temp);
5395   }
5396   #endif
5397   if (!((regs[i].loadedconst >> rt) & 1))
5398     emit_movimm(return_address, rt); // PC into link register
5399   #ifdef IMM_PREFETCH
5400   emit_prefetch(hash_table_get(return_address));
5401   #endif
5402 }
5403
5404 static void rjump_assemble(int i, const struct regstat *i_regs)
5405 {
5406   int temp;
5407   int rs,cc;
5408   rs=get_reg(branch_regs[i].regmap,dops[i].rs1);
5409   assert(rs>=0);
5410   if (ds_writes_rjump_rs(i)) {
5411     // Delay slot abuse, make a copy of the branch address register
5412     temp=get_reg(branch_regs[i].regmap,RTEMP);
5413     assert(temp>=0);
5414     assert(regs[i].regmap[temp]==RTEMP);
5415     emit_mov(rs,temp);
5416     rs=temp;
5417   }
5418   address_generation(i+1,i_regs,regs[i].regmap_entry);
5419   #ifdef REG_PREFETCH
5420   if(dops[i].rt1==31)
5421   {
5422     if((temp=get_reg(branch_regs[i].regmap,PTEMP))>=0) {
5423       signed char *i_regmap=i_regs->regmap;
5424       int return_address=start+i*4+8;
5425       if(i_regmap[temp]==PTEMP) emit_movimm((uintptr_t)hash_table_get(return_address),temp);
5426     }
5427   }
5428   #endif
5429   #ifdef USE_MINI_HT
5430   if(dops[i].rs1==31) {
5431     int rh=get_reg(regs[i].regmap,RHASH);
5432     if(rh>=0) do_preload_rhash(rh);
5433   }
5434   #endif
5435   if (dops[i].rt1 != 0)
5436     rjump_assemble_write_ra(i);
5437   ds_assemble(i+1,i_regs);
5438   uint64_t bc_unneeded=branch_regs[i].u;
5439   bc_unneeded|=1|(1LL<<dops[i].rt1);
5440   bc_unneeded&=~(1LL<<dops[i].rs1);
5441   wb_invalidate(regs[i].regmap,branch_regs[i].regmap,regs[i].dirty,bc_unneeded);
5442   load_regs(regs[i].regmap,branch_regs[i].regmap,dops[i].rs1,CCREG);
5443   cc=get_reg(branch_regs[i].regmap,CCREG);
5444   assert(cc==HOST_CCREG);
5445   (void)cc;
5446   #ifdef USE_MINI_HT
5447   int rh=get_reg(branch_regs[i].regmap,RHASH);
5448   int ht=get_reg(branch_regs[i].regmap,RHTBL);
5449   if(dops[i].rs1==31) {
5450     if(regs[i].regmap[rh]!=RHASH) do_preload_rhash(rh);
5451     do_preload_rhtbl(ht);
5452     do_rhash(rs,rh);
5453   }
5454   #endif
5455   store_regs_bt(branch_regs[i].regmap,branch_regs[i].dirty,-1);
5456   #ifdef DESTRUCTIVE_WRITEBACK
5457   if((branch_regs[i].dirty>>rs)&1) {
5458     if(dops[i].rs1!=dops[i+1].rt1&&dops[i].rs1!=dops[i+1].rt2) {
5459       emit_loadreg(dops[i].rs1,rs);
5460     }
5461   }
5462   #endif
5463   #ifdef REG_PREFETCH
5464   if(dops[i].rt1==31&&temp>=0) emit_prefetchreg(temp);
5465   #endif
5466   #ifdef USE_MINI_HT
5467   if(dops[i].rs1==31) {
5468     do_miniht_load(ht,rh);
5469   }
5470   #endif
5471   //do_cc(i,branch_regs[i].regmap,&adj,-1,TAKEN);
5472   //if(adj) emit_addimm(cc,2*(cinfo[i].ccadj+2-adj),cc); // ??? - Shouldn't happen
5473   //assert(adj==0);
5474   emit_addimm_and_set_flags(cinfo[i].ccadj + CLOCK_ADJUST(2), HOST_CCREG);
5475   add_stub(CC_STUB,out,NULL,0,i,-1,TAKEN,rs);
5476   if (dops[i+1].itype == RFE)
5477     // special case for RFE
5478     emit_jmp(0);
5479   else
5480     emit_jns(0);
5481   //load_regs_bt(branch_regs[i].regmap,branch_regs[i].dirty,-1);
5482   #ifdef USE_MINI_HT
5483   if(dops[i].rs1==31) {
5484     do_miniht_jump(rs,rh,ht);
5485   }
5486   else
5487   #endif
5488   {
5489     do_jump_vaddr(rs);
5490   }
5491   #ifdef CORTEX_A8_BRANCH_PREDICTION_HACK
5492   if(dops[i].rt1!=31&&i<slen-2&&(((u_int)out)&7)) emit_mov(13,13);
5493   #endif
5494 }
5495
5496 static void cjump_assemble(int i, const struct regstat *i_regs)
5497 {
5498   const signed char *i_regmap = i_regs->regmap;
5499   int cc;
5500   int match;
5501   match=match_bt(branch_regs[i].regmap,branch_regs[i].dirty,cinfo[i].ba);
5502   assem_debug("match=%d\n",match);
5503   int s1l,s2l;
5504   int unconditional=0,nop=0;
5505   int invert=0;
5506   int internal=internal_branch(cinfo[i].ba);
5507   if(i==(cinfo[i].ba-start)>>2) assem_debug("idle loop\n");
5508   if(!match) invert=1;
5509   #ifdef CORTEX_A8_BRANCH_PREDICTION_HACK
5510   if(i>(cinfo[i].ba-start)>>2) invert=1;
5511   #endif
5512   #ifdef __aarch64__
5513   invert=1; // because of near cond. branches
5514   #endif
5515
5516   if(dops[i].ooo) {
5517     s1l=get_reg(branch_regs[i].regmap,dops[i].rs1);
5518     s2l=get_reg(branch_regs[i].regmap,dops[i].rs2);
5519   }
5520   else {
5521     s1l=get_reg(i_regmap,dops[i].rs1);
5522     s2l=get_reg(i_regmap,dops[i].rs2);
5523   }
5524   if(dops[i].rs1==0&&dops[i].rs2==0)
5525   {
5526     if(dops[i].opcode&1) nop=1;
5527     else unconditional=1;
5528     //assert(dops[i].opcode!=5);
5529     //assert(dops[i].opcode!=7);
5530     //assert(dops[i].opcode!=0x15);
5531     //assert(dops[i].opcode!=0x17);
5532   }
5533   else if(dops[i].rs1==0)
5534   {
5535     s1l=s2l;
5536     s2l=-1;
5537   }
5538   else if(dops[i].rs2==0)
5539   {
5540     s2l=-1;
5541   }
5542
5543   if(dops[i].ooo) {
5544     // Out of order execution (delay slot first)
5545     //printf("OOOE\n");
5546     address_generation(i+1,i_regs,regs[i].regmap_entry);
5547     ds_assemble(i+1,i_regs);
5548     int adj;
5549     uint64_t bc_unneeded=branch_regs[i].u;
5550     bc_unneeded&=~((1LL<<dops[i].rs1)|(1LL<<dops[i].rs2));
5551     bc_unneeded|=1;
5552     wb_invalidate(regs[i].regmap,branch_regs[i].regmap,regs[i].dirty,bc_unneeded);
5553     load_regs(regs[i].regmap,branch_regs[i].regmap,dops[i].rs1,dops[i].rs2);
5554     load_reg(regs[i].regmap,branch_regs[i].regmap,CCREG);
5555     cc=get_reg(branch_regs[i].regmap,CCREG);
5556     assert(cc==HOST_CCREG);
5557     if(unconditional)
5558       store_regs_bt(branch_regs[i].regmap,branch_regs[i].dirty,cinfo[i].ba);
5559     //do_cc(i,branch_regs[i].regmap,&adj,unconditional?cinfo[i].ba:-1,unconditional);
5560     //assem_debug("cycle count (adj)\n");
5561     if(unconditional) {
5562       do_cc(i,branch_regs[i].regmap,&adj,cinfo[i].ba,TAKEN,0);
5563       if(i!=(cinfo[i].ba-start)>>2 || source[i+1]!=0) {
5564         if(adj) emit_addimm(cc, cinfo[i].ccadj + CLOCK_ADJUST(2) - adj, cc);
5565         load_regs_bt(branch_regs[i].regmap,branch_regs[i].dirty,cinfo[i].ba);
5566         if(internal)
5567           assem_debug("branch: internal\n");
5568         else
5569           assem_debug("branch: external\n");
5570         if (internal && dops[(cinfo[i].ba-start)>>2].is_ds) {
5571           ds_assemble_entry(i);
5572         }
5573         else {
5574           add_to_linker(out,cinfo[i].ba,internal);
5575           emit_jmp(0);
5576         }
5577         #ifdef CORTEX_A8_BRANCH_PREDICTION_HACK
5578         if(((u_int)out)&7) emit_addnop(0);
5579         #endif
5580       }
5581     }
5582     else if(nop) {
5583       emit_addimm_and_set_flags(cinfo[i].ccadj + CLOCK_ADJUST(2), cc);
5584       void *jaddr=out;
5585       emit_jns(0);
5586       add_stub(CC_STUB,jaddr,out,0,i,start+i*4+8,NOTTAKEN,0);
5587     }
5588     else {
5589       void *taken = NULL, *nottaken = NULL, *nottaken1 = NULL;
5590       do_cc(i,branch_regs[i].regmap,&adj,-1,0,invert);
5591       if(adj&&!invert) emit_addimm(cc, cinfo[i].ccadj + CLOCK_ADJUST(2) - adj, cc);
5592
5593       //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]);
5594       assert(s1l>=0);
5595       if(dops[i].opcode==4) // BEQ
5596       {
5597         if(s2l>=0) emit_cmp(s1l,s2l);
5598         else emit_test(s1l,s1l);
5599         if(invert){
5600           nottaken=out;
5601           emit_jne(DJT_1);
5602         }else{
5603           add_to_linker(out,cinfo[i].ba,internal);
5604           emit_jeq(0);
5605         }
5606       }
5607       if(dops[i].opcode==5) // BNE
5608       {
5609         if(s2l>=0) emit_cmp(s1l,s2l);
5610         else emit_test(s1l,s1l);
5611         if(invert){
5612           nottaken=out;
5613           emit_jeq(DJT_1);
5614         }else{
5615           add_to_linker(out,cinfo[i].ba,internal);
5616           emit_jne(0);
5617         }
5618       }
5619       if(dops[i].opcode==6) // BLEZ
5620       {
5621         emit_cmpimm(s1l,1);
5622         if(invert){
5623           nottaken=out;
5624           emit_jge(DJT_1);
5625         }else{
5626           add_to_linker(out,cinfo[i].ba,internal);
5627           emit_jl(0);
5628         }
5629       }
5630       if(dops[i].opcode==7) // BGTZ
5631       {
5632         emit_cmpimm(s1l,1);
5633         if(invert){
5634           nottaken=out;
5635           emit_jl(DJT_1);
5636         }else{
5637           add_to_linker(out,cinfo[i].ba,internal);
5638           emit_jge(0);
5639         }
5640       }
5641       if(invert) {
5642         if(taken) set_jump_target(taken, out);
5643         #ifdef CORTEX_A8_BRANCH_PREDICTION_HACK
5644         if (match && (!internal || !dops[(cinfo[i].ba-start)>>2].is_ds)) {
5645           if(adj) {
5646             emit_addimm(cc,-adj,cc);
5647             add_to_linker(out,cinfo[i].ba,internal);
5648           }else{
5649             emit_addnop(13);
5650             add_to_linker(out,cinfo[i].ba,internal*2);
5651           }
5652           emit_jmp(0);
5653         }else
5654         #endif
5655         {
5656           if(adj) emit_addimm(cc,-adj,cc);
5657           store_regs_bt(branch_regs[i].regmap,branch_regs[i].dirty,cinfo[i].ba);
5658           load_regs_bt(branch_regs[i].regmap,branch_regs[i].dirty,cinfo[i].ba);
5659           if(internal)
5660             assem_debug("branch: internal\n");
5661           else
5662             assem_debug("branch: external\n");
5663           if (internal && dops[(cinfo[i].ba - start) >> 2].is_ds) {
5664             ds_assemble_entry(i);
5665           }
5666           else {
5667             add_to_linker(out,cinfo[i].ba,internal);
5668             emit_jmp(0);
5669           }
5670         }
5671         set_jump_target(nottaken, out);
5672       }
5673
5674       if(nottaken1) set_jump_target(nottaken1, out);
5675       if(adj) {
5676         if(!invert) emit_addimm(cc,adj,cc);
5677       }
5678     } // (!unconditional)
5679   } // if(ooo)
5680   else
5681   {
5682     // In-order execution (branch first)
5683     void *taken = NULL, *nottaken = NULL, *nottaken1 = NULL;
5684     if(!unconditional&&!nop) {
5685       //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]);
5686       assert(s1l>=0);
5687       if((dops[i].opcode&0x2f)==4) // BEQ
5688       {
5689         if(s2l>=0) emit_cmp(s1l,s2l);
5690         else emit_test(s1l,s1l);
5691         nottaken=out;
5692         emit_jne(DJT_2);
5693       }
5694       if((dops[i].opcode&0x2f)==5) // BNE
5695       {
5696         if(s2l>=0) emit_cmp(s1l,s2l);
5697         else emit_test(s1l,s1l);
5698         nottaken=out;
5699         emit_jeq(DJT_2);
5700       }
5701       if((dops[i].opcode&0x2f)==6) // BLEZ
5702       {
5703         emit_cmpimm(s1l,1);
5704         nottaken=out;
5705         emit_jge(DJT_2);
5706       }
5707       if((dops[i].opcode&0x2f)==7) // BGTZ
5708       {
5709         emit_cmpimm(s1l,1);
5710         nottaken=out;
5711         emit_jl(DJT_2);
5712       }
5713     } // if(!unconditional)
5714     int adj;
5715     uint64_t ds_unneeded=branch_regs[i].u;
5716     ds_unneeded&=~((1LL<<dops[i+1].rs1)|(1LL<<dops[i+1].rs2));
5717     ds_unneeded|=1;
5718     // branch taken
5719     if(!nop) {
5720       if(taken) set_jump_target(taken, out);
5721       assem_debug("1:\n");
5722       wb_invalidate(regs[i].regmap,branch_regs[i].regmap,regs[i].dirty,ds_unneeded);
5723       // load regs
5724       load_regs(regs[i].regmap,branch_regs[i].regmap,dops[i+1].rs1,dops[i+1].rs2);
5725       address_generation(i+1,&branch_regs[i],0);
5726       if (ram_offset)
5727         load_reg(regs[i].regmap,branch_regs[i].regmap,ROREG);
5728       load_regs(regs[i].regmap,branch_regs[i].regmap,CCREG,INVCP);
5729       ds_assemble(i+1,&branch_regs[i]);
5730       drc_dbg_emit_wb_dirtys(i+1, &branch_regs[i]);
5731       cc=get_reg(branch_regs[i].regmap,CCREG);
5732       if(cc==-1) {
5733         emit_loadreg(CCREG,cc=HOST_CCREG);
5734         // CHECK: Is the following instruction (fall thru) allocated ok?
5735       }
5736       assert(cc==HOST_CCREG);
5737       store_regs_bt(branch_regs[i].regmap,branch_regs[i].dirty,cinfo[i].ba);
5738       do_cc(i,i_regmap,&adj,cinfo[i].ba,TAKEN,0);
5739       assem_debug("cycle count (adj)\n");
5740       if(adj) emit_addimm(cc, cinfo[i].ccadj + CLOCK_ADJUST(2) - adj, cc);
5741       load_regs_bt(branch_regs[i].regmap,branch_regs[i].dirty,cinfo[i].ba);
5742       if(internal)
5743         assem_debug("branch: internal\n");
5744       else
5745         assem_debug("branch: external\n");
5746       if (internal && dops[(cinfo[i].ba - start) >> 2].is_ds) {
5747         ds_assemble_entry(i);
5748       }
5749       else {
5750         add_to_linker(out,cinfo[i].ba,internal);
5751         emit_jmp(0);
5752       }
5753     }
5754     // branch not taken
5755     if(!unconditional) {
5756       if(nottaken1) set_jump_target(nottaken1, out);
5757       set_jump_target(nottaken, out);
5758       assem_debug("2:\n");
5759       wb_invalidate(regs[i].regmap,branch_regs[i].regmap,regs[i].dirty,ds_unneeded);
5760       // load regs
5761       load_regs(regs[i].regmap,branch_regs[i].regmap,dops[i+1].rs1,dops[i+1].rs2);
5762       address_generation(i+1,&branch_regs[i],0);
5763       if (ram_offset)
5764         load_reg(regs[i].regmap,branch_regs[i].regmap,ROREG);
5765       load_regs(regs[i].regmap,branch_regs[i].regmap,CCREG,INVCP);
5766       ds_assemble(i+1,&branch_regs[i]);
5767       cc=get_reg(branch_regs[i].regmap,CCREG);
5768       if (cc == -1) {
5769         // Cycle count isn't in a register, temporarily load it then write it out
5770         emit_loadreg(CCREG,HOST_CCREG);
5771         emit_addimm_and_set_flags(cinfo[i].ccadj + CLOCK_ADJUST(2), HOST_CCREG);
5772         void *jaddr=out;
5773         emit_jns(0);
5774         add_stub(CC_STUB,jaddr,out,0,i,start+i*4+8,NOTTAKEN,0);
5775         emit_storereg(CCREG,HOST_CCREG);
5776       }
5777       else{
5778         cc=get_reg(i_regmap,CCREG);
5779         assert(cc==HOST_CCREG);
5780         emit_addimm_and_set_flags(cinfo[i].ccadj + CLOCK_ADJUST(2), cc);
5781         void *jaddr=out;
5782         emit_jns(0);
5783         add_stub(CC_STUB,jaddr,out,0,i,start+i*4+8,NOTTAKEN,0);
5784       }
5785     }
5786   }
5787 }
5788
5789 static void sjump_assemble(int i, const struct regstat *i_regs)
5790 {
5791   const signed char *i_regmap = i_regs->regmap;
5792   int cc;
5793   int match;
5794   match=match_bt(branch_regs[i].regmap,branch_regs[i].dirty,cinfo[i].ba);
5795   assem_debug("smatch=%d ooo=%d\n", match, dops[i].ooo);
5796   int s1l;
5797   int unconditional=0,nevertaken=0;
5798   int invert=0;
5799   int internal=internal_branch(cinfo[i].ba);
5800   if(i==(cinfo[i].ba-start)>>2) assem_debug("idle loop\n");
5801   if(!match) invert=1;
5802   #ifdef CORTEX_A8_BRANCH_PREDICTION_HACK
5803   if(i>(cinfo[i].ba-start)>>2) invert=1;
5804   #endif
5805   #ifdef __aarch64__
5806   invert=1; // because of near cond. branches
5807   #endif
5808
5809   //if(dops[i].opcode2>=0x10) return; // FIXME (BxxZAL)
5810   //assert(dops[i].opcode2<0x10||dops[i].rs1==0); // FIXME (BxxZAL)
5811
5812   if(dops[i].ooo) {
5813     s1l=get_reg(branch_regs[i].regmap,dops[i].rs1);
5814   }
5815   else {
5816     s1l=get_reg(i_regmap,dops[i].rs1);
5817   }
5818   if(dops[i].rs1==0)
5819   {
5820     if(dops[i].opcode2&1) unconditional=1;
5821     else nevertaken=1;
5822     // These are never taken (r0 is never less than zero)
5823     //assert(dops[i].opcode2!=0);
5824     //assert(dops[i].opcode2!=2);
5825     //assert(dops[i].opcode2!=0x10);
5826     //assert(dops[i].opcode2!=0x12);
5827   }
5828
5829   if(dops[i].ooo) {
5830     // Out of order execution (delay slot first)
5831     //printf("OOOE\n");
5832     address_generation(i+1,i_regs,regs[i].regmap_entry);
5833     ds_assemble(i+1,i_regs);
5834     int adj;
5835     uint64_t bc_unneeded=branch_regs[i].u;
5836     bc_unneeded&=~((1LL<<dops[i].rs1)|(1LL<<dops[i].rs2));
5837     bc_unneeded|=1;
5838     wb_invalidate(regs[i].regmap,branch_regs[i].regmap,regs[i].dirty,bc_unneeded);
5839     load_regs(regs[i].regmap,branch_regs[i].regmap,dops[i].rs1,dops[i].rs1);
5840     load_reg(regs[i].regmap,branch_regs[i].regmap,CCREG);
5841     if(dops[i].rt1==31) {
5842       int rt,return_address;
5843       rt=get_reg(branch_regs[i].regmap,31);
5844       //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]);
5845       if(rt>=0) {
5846         // Save the PC even if the branch is not taken
5847         return_address=start+i*4+8;
5848         emit_movimm(return_address,rt); // PC into link register
5849         #ifdef IMM_PREFETCH
5850         if(!nevertaken) emit_prefetch(hash_table_get(return_address));
5851         #endif
5852       }
5853     }
5854     cc=get_reg(branch_regs[i].regmap,CCREG);
5855     assert(cc==HOST_CCREG);
5856     if(unconditional)
5857       store_regs_bt(branch_regs[i].regmap,branch_regs[i].dirty,cinfo[i].ba);
5858     //do_cc(i,branch_regs[i].regmap,&adj,unconditional?cinfo[i].ba:-1,unconditional);
5859     assem_debug("cycle count (adj)\n");
5860     if(unconditional) {
5861       do_cc(i,branch_regs[i].regmap,&adj,cinfo[i].ba,TAKEN,0);
5862       if(i!=(cinfo[i].ba-start)>>2 || source[i+1]!=0) {
5863         if(adj) emit_addimm(cc, cinfo[i].ccadj + CLOCK_ADJUST(2) - adj, cc);
5864         load_regs_bt(branch_regs[i].regmap,branch_regs[i].dirty,cinfo[i].ba);
5865         if(internal)
5866           assem_debug("branch: internal\n");
5867         else
5868           assem_debug("branch: external\n");
5869         if (internal && dops[(cinfo[i].ba - start) >> 2].is_ds) {
5870           ds_assemble_entry(i);
5871         }
5872         else {
5873           add_to_linker(out,cinfo[i].ba,internal);
5874           emit_jmp(0);
5875         }
5876         #ifdef CORTEX_A8_BRANCH_PREDICTION_HACK
5877         if(((u_int)out)&7) emit_addnop(0);
5878         #endif
5879       }
5880     }
5881     else if(nevertaken) {
5882       emit_addimm_and_set_flags(cinfo[i].ccadj + CLOCK_ADJUST(2), cc);
5883       void *jaddr=out;
5884       emit_jns(0);
5885       add_stub(CC_STUB,jaddr,out,0,i,start+i*4+8,NOTTAKEN,0);
5886     }
5887     else {
5888       void *nottaken = NULL;
5889       do_cc(i,branch_regs[i].regmap,&adj,-1,0,invert);
5890       if(adj&&!invert) emit_addimm(cc, cinfo[i].ccadj + CLOCK_ADJUST(2) - adj, cc);
5891       {
5892         assert(s1l>=0);
5893         if ((dops[i].opcode2 & 1) == 0) // BLTZ/BLTZAL
5894         {
5895           emit_test(s1l,s1l);
5896           if(invert){
5897             nottaken=out;
5898             emit_jns(DJT_1);
5899           }else{
5900             add_to_linker(out,cinfo[i].ba,internal);
5901             emit_js(0);
5902           }
5903         }
5904         else // BGEZ/BGEZAL
5905         {
5906           emit_test(s1l,s1l);
5907           if(invert){
5908             nottaken=out;
5909             emit_js(DJT_1);
5910           }else{
5911             add_to_linker(out,cinfo[i].ba,internal);
5912             emit_jns(0);
5913           }
5914         }
5915       }
5916
5917       if(invert) {
5918         #ifdef CORTEX_A8_BRANCH_PREDICTION_HACK
5919         if (match && (!internal || !dops[(cinfo[i].ba - start) >> 2].is_ds)) {
5920           if(adj) {
5921             emit_addimm(cc,-adj,cc);
5922             add_to_linker(out,cinfo[i].ba,internal);
5923           }else{
5924             emit_addnop(13);
5925             add_to_linker(out,cinfo[i].ba,internal*2);
5926           }
5927           emit_jmp(0);
5928         }else
5929         #endif
5930         {
5931           if(adj) emit_addimm(cc,-adj,cc);
5932           store_regs_bt(branch_regs[i].regmap,branch_regs[i].dirty,cinfo[i].ba);
5933           load_regs_bt(branch_regs[i].regmap,branch_regs[i].dirty,cinfo[i].ba);
5934           if(internal)
5935             assem_debug("branch: internal\n");
5936           else
5937             assem_debug("branch: external\n");
5938           if (internal && dops[(cinfo[i].ba - start) >> 2].is_ds) {
5939             ds_assemble_entry(i);
5940           }
5941           else {
5942             add_to_linker(out,cinfo[i].ba,internal);
5943             emit_jmp(0);
5944           }
5945         }
5946         set_jump_target(nottaken, out);
5947       }
5948
5949       if(adj) {
5950         if(!invert) emit_addimm(cc,adj,cc);
5951       }
5952     } // (!unconditional)
5953   } // if(ooo)
5954   else
5955   {
5956     // In-order execution (branch first)
5957     //printf("IOE\n");
5958     void *nottaken = NULL;
5959     if (!unconditional && !nevertaken) {
5960       assert(s1l >= 0);
5961       emit_test(s1l, s1l);
5962     }
5963     if (dops[i].rt1 == 31) {
5964       int rt, return_address;
5965       rt = get_reg(branch_regs[i].regmap,31);
5966       if(rt >= 0) {
5967         // Save the PC even if the branch is not taken
5968         return_address = start + i*4+8;
5969         emit_movimm(return_address, rt); // PC into link register
5970         #ifdef IMM_PREFETCH
5971         emit_prefetch(hash_table_get(return_address));
5972         #endif
5973       }
5974     }
5975     if (!unconditional && !nevertaken) {
5976       nottaken = out;
5977       if (!(dops[i].opcode2 & 1)) // BLTZ/BLTZAL
5978         emit_jns(DJT_1);
5979       else                        // BGEZ/BGEZAL
5980         emit_js(DJT_1);
5981     }
5982     int adj;
5983     uint64_t ds_unneeded=branch_regs[i].u;
5984     ds_unneeded&=~((1LL<<dops[i+1].rs1)|(1LL<<dops[i+1].rs2));
5985     ds_unneeded|=1;
5986     // branch taken
5987     if(!nevertaken) {
5988       //assem_debug("1:\n");
5989       wb_invalidate(regs[i].regmap,branch_regs[i].regmap,regs[i].dirty,ds_unneeded);
5990       // load regs
5991       load_regs(regs[i].regmap,branch_regs[i].regmap,dops[i+1].rs1,dops[i+1].rs2);
5992       address_generation(i+1,&branch_regs[i],0);
5993       if (ram_offset)
5994         load_reg(regs[i].regmap,branch_regs[i].regmap,ROREG);
5995       load_regs(regs[i].regmap,branch_regs[i].regmap,CCREG,INVCP);
5996       ds_assemble(i+1,&branch_regs[i]);
5997       cc=get_reg(branch_regs[i].regmap,CCREG);
5998       if(cc==-1) {
5999         emit_loadreg(CCREG,cc=HOST_CCREG);
6000         // CHECK: Is the following instruction (fall thru) allocated ok?
6001       }
6002       assert(cc==HOST_CCREG);
6003       store_regs_bt(branch_regs[i].regmap,branch_regs[i].dirty,cinfo[i].ba);
6004       do_cc(i,i_regmap,&adj,cinfo[i].ba,TAKEN,0);
6005       assem_debug("cycle count (adj)\n");
6006       if(adj) emit_addimm(cc, cinfo[i].ccadj + CLOCK_ADJUST(2) - adj, cc);
6007       load_regs_bt(branch_regs[i].regmap,branch_regs[i].dirty,cinfo[i].ba);
6008       if(internal)
6009         assem_debug("branch: internal\n");
6010       else
6011         assem_debug("branch: external\n");
6012       if (internal && dops[(cinfo[i].ba - start) >> 2].is_ds) {
6013         ds_assemble_entry(i);
6014       }
6015       else {
6016         add_to_linker(out,cinfo[i].ba,internal);
6017         emit_jmp(0);
6018       }
6019     }
6020     // branch not taken
6021     if(!unconditional) {
6022       if (!nevertaken) {
6023         assert(nottaken);
6024         set_jump_target(nottaken, out);
6025       }
6026       assem_debug("1:\n");
6027       wb_invalidate(regs[i].regmap,branch_regs[i].regmap,regs[i].dirty,ds_unneeded);
6028       load_regs(regs[i].regmap,branch_regs[i].regmap,dops[i+1].rs1,dops[i+1].rs2);
6029       address_generation(i+1,&branch_regs[i],0);
6030       if (ram_offset)
6031         load_reg(regs[i].regmap,branch_regs[i].regmap,ROREG);
6032       load_regs(regs[i].regmap,branch_regs[i].regmap,CCREG,INVCP);
6033       ds_assemble(i+1,&branch_regs[i]);
6034       cc=get_reg(branch_regs[i].regmap,CCREG);
6035       if (cc == -1) {
6036         // Cycle count isn't in a register, temporarily load it then write it out
6037         emit_loadreg(CCREG,HOST_CCREG);
6038         emit_addimm_and_set_flags(cinfo[i].ccadj + CLOCK_ADJUST(2), HOST_CCREG);
6039         void *jaddr=out;
6040         emit_jns(0);
6041         add_stub(CC_STUB,jaddr,out,0,i,start+i*4+8,NOTTAKEN,0);
6042         emit_storereg(CCREG,HOST_CCREG);
6043       }
6044       else{
6045         cc=get_reg(i_regmap,CCREG);
6046         assert(cc==HOST_CCREG);
6047         emit_addimm_and_set_flags(cinfo[i].ccadj + CLOCK_ADJUST(2), cc);
6048         void *jaddr=out;
6049         emit_jns(0);
6050         add_stub(CC_STUB,jaddr,out,0,i,start+i*4+8,NOTTAKEN,0);
6051       }
6052     }
6053   }
6054 }
6055
6056 static void check_regmap(signed char *regmap)
6057 {
6058 #ifndef NDEBUG
6059   int i,j;
6060   for (i = 0; i < HOST_REGS; i++) {
6061     if (regmap[i] < 0)
6062       continue;
6063     for (j = i + 1; j < HOST_REGS; j++)
6064       assert(regmap[i] != regmap[j]);
6065   }
6066 #endif
6067 }
6068
6069 #ifdef DISASM
6070 #include <inttypes.h>
6071 static char insn[MAXBLOCK][10];
6072
6073 #define set_mnemonic(i_, n_) \
6074   strcpy(insn[i_], n_)
6075
6076 void print_regmap(const char *name, const signed char *regmap)
6077 {
6078   char buf[5];
6079   int i, l;
6080   fputs(name, stdout);
6081   for (i = 0; i < HOST_REGS; i++) {
6082     l = 0;
6083     if (regmap[i] >= 0)
6084       l = snprintf(buf, sizeof(buf), "$%d", regmap[i]);
6085     for (; l < 3; l++)
6086       buf[l] = ' ';
6087     buf[l] = 0;
6088     printf(" r%d=%s", i, buf);
6089   }
6090   fputs("\n", stdout);
6091 }
6092
6093   /* disassembly */
6094 void disassemble_inst(int i)
6095 {
6096     if (dops[i].bt) printf("*"); else printf(" ");
6097     switch(dops[i].itype) {
6098       case UJUMP:
6099         printf (" %x: %s %8x\n",start+i*4,insn[i],cinfo[i].ba);break;
6100       case CJUMP:
6101         printf (" %x: %s r%d,r%d,%8x\n",start+i*4,insn[i],dops[i].rs1,dops[i].rs2,i?start+i*4+4+((signed int)((unsigned int)source[i]<<16)>>14):cinfo[i].ba);break;
6102       case SJUMP:
6103         printf (" %x: %s r%d,%8x\n",start+i*4,insn[i],dops[i].rs1,start+i*4+4+((signed int)((unsigned int)source[i]<<16)>>14));break;
6104       case RJUMP:
6105         if (dops[i].opcode2 == 9 && dops[i].rt1 != 31)
6106           printf (" %x: %s r%d,r%d\n",start+i*4,insn[i],dops[i].rt1,dops[i].rs1);
6107         else
6108           printf (" %x: %s r%d\n",start+i*4,insn[i],dops[i].rs1);
6109         break;
6110       case IMM16:
6111         if(dops[i].opcode==0xf) //LUI
6112           printf (" %x: %s r%d,%4x0000\n",start+i*4,insn[i],dops[i].rt1,cinfo[i].imm&0xffff);
6113         else
6114           printf (" %x: %s r%d,r%d,%d\n",start+i*4,insn[i],dops[i].rt1,dops[i].rs1,cinfo[i].imm);
6115         break;
6116       case LOAD:
6117       case LOADLR:
6118         printf (" %x: %s r%d,r%d+%x\n",start+i*4,insn[i],dops[i].rt1,dops[i].rs1,cinfo[i].imm);
6119         break;
6120       case STORE:
6121       case STORELR:
6122         printf (" %x: %s r%d,r%d+%x\n",start+i*4,insn[i],dops[i].rs2,dops[i].rs1,cinfo[i].imm);
6123         break;
6124       case ALU:
6125       case SHIFT:
6126         printf (" %x: %s r%d,r%d,r%d\n",start+i*4,insn[i],dops[i].rt1,dops[i].rs1,dops[i].rs2);
6127         break;
6128       case MULTDIV:
6129         printf (" %x: %s r%d,r%d\n",start+i*4,insn[i],dops[i].rs1,dops[i].rs2);
6130         break;
6131       case SHIFTIMM:
6132         printf (" %x: %s r%d,r%d,%d\n",start+i*4,insn[i],dops[i].rt1,dops[i].rs1,cinfo[i].imm);
6133         break;
6134       case MOV:
6135         if((dops[i].opcode2&0x1d)==0x10)
6136           printf (" %x: %s r%d\n",start+i*4,insn[i],dops[i].rt1);
6137         else if((dops[i].opcode2&0x1d)==0x11)
6138           printf (" %x: %s r%d\n",start+i*4,insn[i],dops[i].rs1);
6139         else
6140           printf (" %x: %s\n",start+i*4,insn[i]);
6141         break;
6142       case COP0:
6143         if(dops[i].opcode2==0)
6144           printf (" %x: %s r%d,cpr0[%d]\n",start+i*4,insn[i],dops[i].rt1,(source[i]>>11)&0x1f); // MFC0
6145         else if(dops[i].opcode2==4)
6146           printf (" %x: %s r%d,cpr0[%d]\n",start+i*4,insn[i],dops[i].rs1,(source[i]>>11)&0x1f); // MTC0
6147         else printf (" %x: %s\n",start+i*4,insn[i]);
6148         break;
6149       case COP2:
6150         if(dops[i].opcode2<3)
6151           printf (" %x: %s r%d,cpr2[%d]\n",start+i*4,insn[i],dops[i].rt1,(source[i]>>11)&0x1f); // MFC2
6152         else if(dops[i].opcode2>3)
6153           printf (" %x: %s r%d,cpr2[%d]\n",start+i*4,insn[i],dops[i].rs1,(source[i]>>11)&0x1f); // MTC2
6154         else printf (" %x: %s\n",start+i*4,insn[i]);
6155         break;
6156       case C2LS:
6157         printf (" %x: %s cpr2[%d],r%d+%x\n",start+i*4,insn[i],(source[i]>>16)&0x1f,dops[i].rs1,cinfo[i].imm);
6158         break;
6159       case INTCALL:
6160         printf (" %x: %s (INTCALL)\n",start+i*4,insn[i]);
6161         break;
6162       default:
6163         //printf (" %s %8x\n",insn[i],source[i]);
6164         printf (" %x: %s\n",start+i*4,insn[i]);
6165     }
6166     #ifndef REGMAP_PRINT
6167     return;
6168     #endif
6169     printf("D: %x  WD: %x  U: %"PRIx64"  hC: %x  hWC: %x  hLC: %x\n",
6170       regs[i].dirty, regs[i].wasdirty, unneeded_reg[i],
6171       regs[i].isconst, regs[i].wasconst, regs[i].loadedconst);
6172     print_regmap("pre:   ", regmap_pre[i]);
6173     print_regmap("entry: ", regs[i].regmap_entry);
6174     print_regmap("map:   ", regs[i].regmap);
6175     if (dops[i].is_jump) {
6176       print_regmap("bentry:", branch_regs[i].regmap_entry);
6177       print_regmap("bmap:  ", branch_regs[i].regmap);
6178     }
6179 }
6180 #else
6181 #define set_mnemonic(i_, n_)
6182 static void disassemble_inst(int i) {}
6183 #endif // DISASM
6184
6185 #define DRC_TEST_VAL 0x74657374
6186
6187 static noinline void new_dynarec_test(void)
6188 {
6189   int (*testfunc)(void);
6190   void *beginning;
6191   int ret[2];
6192   size_t i;
6193
6194   // check structure linkage
6195   if ((u_char *)rcnts - (u_char *)&psxRegs != sizeof(psxRegs))
6196   {
6197     SysPrintf("linkage_arm* miscompilation/breakage detected.\n");
6198   }
6199
6200   SysPrintf("(%p) testing if we can run recompiled code @%p...\n",
6201     new_dynarec_test, out);
6202   ((volatile u_int *)NDRC_WRITE_OFFSET(out))[0]++; // make the cache dirty
6203
6204   for (i = 0; i < ARRAY_SIZE(ret); i++) {
6205     out = ndrc->translation_cache;
6206     beginning = start_block();
6207     emit_movimm(DRC_TEST_VAL + i, 0); // test
6208     emit_ret();
6209     literal_pool(0);
6210     end_block(beginning);
6211     testfunc = beginning;
6212     ret[i] = testfunc();
6213   }
6214
6215   if (ret[0] == DRC_TEST_VAL && ret[1] == DRC_TEST_VAL + 1)
6216     SysPrintf("test passed.\n");
6217   else
6218     SysPrintf("test failed, will likely crash soon (r=%08x %08x)\n", ret[0], ret[1]);
6219   out = ndrc->translation_cache;
6220 }
6221
6222 static int get_cycle_multiplier(void)
6223 {
6224   return Config.cycle_multiplier_override && Config.cycle_multiplier == CYCLE_MULT_DEFAULT
6225      ? Config.cycle_multiplier_override : Config.cycle_multiplier;
6226 }
6227
6228 // clear the state completely, instead of just marking
6229 // things invalid like invalidate_all_pages() does
6230 void new_dynarec_clear_full(void)
6231 {
6232   int n;
6233   out = ndrc->translation_cache;
6234   memset(invalid_code,1,sizeof(invalid_code));
6235   memset(shadow,0,sizeof(shadow));
6236   hash_table_clear();
6237   mini_ht_clear();
6238   copy=shadow;
6239   expirep = EXPIRITY_OFFSET;
6240   pending_exception=0;
6241   literalcount=0;
6242   stop_after_jal=0;
6243   inv_code_start=inv_code_end=~0;
6244   hack_addr=0;
6245   f1_hack=0;
6246   for (n = 0; n < ARRAY_SIZE(blocks); n++)
6247     blocks_clear(&blocks[n]);
6248   for (n = 0; n < ARRAY_SIZE(jumps); n++) {
6249     free(jumps[n]);
6250     jumps[n] = NULL;
6251   }
6252   stat_clear(stat_blocks);
6253   stat_clear(stat_links);
6254
6255   if (cycle_multiplier_old != Config.cycle_multiplier
6256       || new_dynarec_hacks_old != new_dynarec_hacks)
6257   {
6258     SysPrintf("ndrc config: mul=%d, ha=%x, pex=%d\n",
6259       get_cycle_multiplier(), new_dynarec_hacks, Config.PreciseExceptions);
6260   }
6261   cycle_multiplier_old = Config.cycle_multiplier;
6262   new_dynarec_hacks_old = new_dynarec_hacks;
6263 }
6264
6265 void new_dynarec_init(void)
6266 {
6267   SysPrintf("Init new dynarec, ndrc size %x\n", (int)sizeof(*ndrc));
6268
6269 #ifdef _3DS
6270   check_rosalina();
6271 #endif
6272 #ifdef BASE_ADDR_DYNAMIC
6273   #ifdef VITA
6274   sceBlock = getVMBlock(); //sceKernelAllocMemBlockForVM("code", sizeof(*ndrc));
6275   if (sceBlock <= 0)
6276     SysPrintf("sceKernelAllocMemBlockForVM failed: %x\n", sceBlock);
6277   int ret = sceKernelGetMemBlockBase(sceBlock, (void **)&ndrc);
6278   if (ret < 0)
6279     SysPrintf("sceKernelGetMemBlockBase failed: %x\n", ret);
6280   sceKernelOpenVMDomain();
6281   sceClibPrintf("translation_cache = 0x%08lx\n ", (long)ndrc->translation_cache);
6282   #elif defined(_MSC_VER)
6283   ndrc = VirtualAlloc(NULL, sizeof(*ndrc), MEM_COMMIT | MEM_RESERVE,
6284     PAGE_EXECUTE_READWRITE);
6285   #elif defined(HAVE_LIBNX)
6286   Result rc = jitCreate(&g_jit, sizeof(*ndrc));
6287   if (R_FAILED(rc))
6288     SysPrintf("jitCreate failed: %08x\n", rc);
6289   SysPrintf("jitCreate: RX: %p RW: %p type: %d\n", g_jit.rx_addr, g_jit.rw_addr, g_jit.type);
6290   jitTransitionToWritable(&g_jit);
6291   ndrc = g_jit.rx_addr;
6292   ndrc_write_ofs = (char *)g_jit.rw_addr - (char *)ndrc;
6293   memset(NDRC_WRITE_OFFSET(&ndrc->tramp), 0, sizeof(ndrc->tramp));
6294   #else
6295   uintptr_t desired_addr = 0;
6296   int prot = PROT_READ | PROT_WRITE | PROT_EXEC;
6297   int flags = MAP_PRIVATE | MAP_ANONYMOUS;
6298   int fd = -1;
6299   #ifdef __ELF__
6300   extern char _end;
6301   desired_addr = ((uintptr_t)&_end + 0xffffff) & ~0xffffffl;
6302   #endif
6303   #ifdef TC_WRITE_OFFSET
6304   // mostly for testing
6305   fd = open("/dev/shm/pcsxr", O_CREAT | O_RDWR, 0600);
6306   ftruncate(fd, sizeof(*ndrc));
6307   void *mw = mmap(NULL, sizeof(*ndrc), PROT_READ | PROT_WRITE,
6308                   (flags = MAP_SHARED), fd, 0);
6309   assert(mw != MAP_FAILED);
6310   prot = PROT_READ | PROT_EXEC;
6311   #endif
6312   ndrc = mmap((void *)desired_addr, sizeof(*ndrc), prot, flags, fd, 0);
6313   if (ndrc == MAP_FAILED) {
6314     SysPrintf("mmap() failed: %s\n", strerror(errno));
6315     abort();
6316   }
6317   #ifdef TC_WRITE_OFFSET
6318   ndrc_write_ofs = (char *)mw - (char *)ndrc;
6319   #endif
6320   #endif
6321 #else
6322   #ifndef NO_WRITE_EXEC
6323   // not all systems allow execute in data segment by default
6324   // size must be 4K aligned for 3DS?
6325   if (mprotect(ndrc, sizeof(*ndrc),
6326                PROT_READ | PROT_WRITE | PROT_EXEC) != 0)
6327     SysPrintf("mprotect() failed: %s\n", strerror(errno));
6328   #endif
6329 #endif
6330   out = ndrc->translation_cache;
6331   new_dynarec_clear_full();
6332 #ifdef HOST_IMM8
6333   // Copy this into local area so we don't have to put it in every literal pool
6334   invc_ptr=invalid_code;
6335 #endif
6336   arch_init();
6337   new_dynarec_test();
6338   ram_offset = (uintptr_t)psxM - 0x80000000;
6339   if (ram_offset!=0)
6340     SysPrintf("warning: RAM is not directly mapped, performance will suffer\n");
6341   SysPrintf("Mapped (RAM/scrp/ROM/LUTs/TC):\n");
6342   SysPrintf("%p/%p/%p/%p/%p\n", psxM, psxH, psxR, mem_rtab, out);
6343 }
6344
6345 void new_dynarec_cleanup(void)
6346 {
6347   int n;
6348 #ifdef BASE_ADDR_DYNAMIC
6349   #ifdef VITA
6350   // sceBlock is managed by retroarch's bootstrap code
6351   //sceKernelFreeMemBlock(sceBlock);
6352   //sceBlock = -1;
6353   #elif defined(HAVE_LIBNX)
6354   jitClose(&g_jit);
6355   ndrc = NULL;
6356   #else
6357   if (munmap(ndrc, sizeof(*ndrc)) < 0)
6358     SysPrintf("munmap() failed\n");
6359   ndrc = NULL;
6360   #endif
6361 #endif
6362   for (n = 0; n < ARRAY_SIZE(blocks); n++)
6363     blocks_clear(&blocks[n]);
6364   for (n = 0; n < ARRAY_SIZE(jumps); n++) {
6365     free(jumps[n]);
6366     jumps[n] = NULL;
6367   }
6368   stat_clear(stat_blocks);
6369   stat_clear(stat_links);
6370   new_dynarec_print_stats();
6371 }
6372
6373 static u_int *get_source_start(u_int addr, u_int *limit)
6374 {
6375   if (addr < 0x00800000
6376       || (0x80000000 <= addr && addr < 0x80800000)
6377       || (0xa0000000 <= addr && addr < 0xa0800000))
6378   {
6379     // used for BIOS calls mostly?
6380     *limit = (addr & 0xa0600000) + 0x00200000;
6381     return (u_int *)(psxM + (addr & 0x1fffff));
6382   }
6383   else if (
6384     /* (0x9fc00000 <= addr && addr < 0x9fc80000) ||*/
6385     (0xbfc00000 <= addr && addr < 0xbfc80000))
6386   {
6387     // BIOS. The multiplier should be much higher as it's uncached 8bit mem,
6388     // but timings in PCSX are too tied to the interpreter's 2-per-insn assumption
6389     if (!HACK_ENABLED(NDHACK_OVERRIDE_CYCLE_M))
6390       cycle_multiplier_active = 200;
6391
6392     *limit = (addr & 0xfff00000) | 0x80000;
6393     return (u_int *)((u_char *)psxR + (addr&0x7ffff));
6394   }
6395   return NULL;
6396 }
6397
6398 static u_int scan_for_ret(u_int addr)
6399 {
6400   u_int limit = 0;
6401   u_int *mem;
6402
6403   mem = get_source_start(addr, &limit);
6404   if (mem == NULL)
6405     return addr;
6406
6407   if (limit > addr + 0x1000)
6408     limit = addr + 0x1000;
6409   for (; addr < limit; addr += 4, mem++) {
6410     if (*mem == 0x03e00008) // jr $ra
6411       return addr + 8;
6412   }
6413   return addr;
6414 }
6415
6416 struct savestate_block {
6417   uint32_t addr;
6418   uint32_t regflags;
6419 };
6420
6421 static int addr_cmp(const void *p1_, const void *p2_)
6422 {
6423   const struct savestate_block *p1 = p1_, *p2 = p2_;
6424   return p1->addr - p2->addr;
6425 }
6426
6427 int new_dynarec_save_blocks(void *save, int size)
6428 {
6429   struct savestate_block *sblocks = save;
6430   int maxcount = size / sizeof(sblocks[0]);
6431   struct savestate_block tmp_blocks[1024];
6432   struct block_info *block;
6433   int p, s, d, o, bcnt;
6434   u_int addr;
6435
6436   o = 0;
6437   for (p = 0; p < ARRAY_SIZE(blocks); p++) {
6438     bcnt = 0;
6439     for (block = blocks[p]; block != NULL; block = block->next) {
6440       if (block->is_dirty)
6441         continue;
6442       tmp_blocks[bcnt].addr = block->start;
6443       tmp_blocks[bcnt].regflags = block->reg_sv_flags;
6444       bcnt++;
6445     }
6446     if (bcnt < 1)
6447       continue;
6448     qsort(tmp_blocks, bcnt, sizeof(tmp_blocks[0]), addr_cmp);
6449
6450     addr = tmp_blocks[0].addr;
6451     for (s = d = 0; s < bcnt; s++) {
6452       if (tmp_blocks[s].addr < addr)
6453         continue;
6454       if (d == 0 || tmp_blocks[d-1].addr != tmp_blocks[s].addr)
6455         tmp_blocks[d++] = tmp_blocks[s];
6456       addr = scan_for_ret(tmp_blocks[s].addr);
6457     }
6458
6459     if (o + d > maxcount)
6460       d = maxcount - o;
6461     memcpy(&sblocks[o], tmp_blocks, d * sizeof(sblocks[0]));
6462     o += d;
6463   }
6464
6465   return o * sizeof(sblocks[0]);
6466 }
6467
6468 void new_dynarec_load_blocks(const void *save, int size)
6469 {
6470   const struct savestate_block *sblocks = save;
6471   int count = size / sizeof(sblocks[0]);
6472   struct block_info *block;
6473   u_int regs_save[32];
6474   u_int page;
6475   uint32_t f;
6476   int i, b;
6477
6478   // restore clean blocks, if any
6479   for (page = 0, b = i = 0; page < ARRAY_SIZE(blocks); page++) {
6480     for (block = blocks[page]; block != NULL; block = block->next, b++) {
6481       if (!block->is_dirty)
6482         continue;
6483       assert(block->source && block->copy);
6484       if (memcmp(block->source, block->copy, block->len))
6485         continue;
6486
6487       // see try_restore_block
6488       block->is_dirty = 0;
6489       mark_invalid_code(block->start, block->len, 0);
6490       i++;
6491     }
6492   }
6493   inv_debug("load_blocks: %d/%d clean blocks\n", i, b);
6494
6495   // change GPRs for speculation to at least partially work..
6496   memcpy(regs_save, &psxRegs.GPR, sizeof(regs_save));
6497   for (i = 1; i < 32; i++)
6498     psxRegs.GPR.r[i] = 0x80000000;
6499
6500   for (b = 0; b < count; b++) {
6501     for (f = sblocks[b].regflags, i = 0; f; f >>= 1, i++) {
6502       if (f & 1)
6503         psxRegs.GPR.r[i] = 0x1f800000;
6504     }
6505
6506     ndrc_get_addr_ht(sblocks[b].addr);
6507
6508     for (f = sblocks[b].regflags, i = 0; f; f >>= 1, i++) {
6509       if (f & 1)
6510         psxRegs.GPR.r[i] = 0x80000000;
6511     }
6512   }
6513
6514   memcpy(&psxRegs.GPR, regs_save, sizeof(regs_save));
6515 }
6516
6517 void new_dynarec_print_stats(void)
6518 {
6519 #ifdef STAT_PRINT
6520   printf("cc %3d,%3d,%3d lu%6d,%3d,%3d c%3d inv%3d,%3d tc_offs %zu b %u,%u\n",
6521     stat_bc_pre, stat_bc_direct, stat_bc_restore,
6522     stat_ht_lookups, stat_jump_in_lookups, stat_restore_tries,
6523     stat_restore_compares, stat_inv_addr_calls, stat_inv_hits,
6524     out - ndrc->translation_cache, stat_blocks, stat_links);
6525   stat_bc_direct = stat_bc_pre = stat_bc_restore =
6526   stat_ht_lookups = stat_jump_in_lookups = stat_restore_tries =
6527   stat_restore_compares = stat_inv_addr_calls = stat_inv_hits = 0;
6528 #endif
6529 }
6530
6531 static void force_intcall(int i)
6532 {
6533   memset(&dops[i], 0, sizeof(dops[i]));
6534   dops[i].itype = INTCALL;
6535   dops[i].rs1 = CCREG;
6536   dops[i].is_exception = 1;
6537   cinfo[i].ba = -1;
6538 }
6539
6540 static int apply_hacks(void)
6541 {
6542   int i;
6543   if (HACK_ENABLED(NDHACK_NO_COMPAT_HACKS))
6544     return 0;
6545   /* special hack(s) */
6546   for (i = 0; i < slen - 4; i++)
6547   {
6548     // lui a4, 0xf200; jal <rcnt_read>; addu a0, 2; slti v0, 28224
6549     if (source[i] == 0x3c04f200 && dops[i+1].itype == UJUMP
6550         && source[i+2] == 0x34840002 && dops[i+3].opcode == 0x0a
6551         && cinfo[i+3].imm == 0x6e40 && dops[i+3].rs1 == 2)
6552     {
6553       SysPrintf("PE2 hack @%08x\n", start + (i+3)*4);
6554       dops[i + 3].itype = NOP;
6555     }
6556   }
6557   i = slen;
6558   if (i > 10 && source[i-1] == 0 && source[i-2] == 0x03e00008
6559       && source[i-4] == 0x8fbf0018 && source[i-6] == 0x00c0f809
6560       && dops[i-7].itype == STORE)
6561   {
6562     i = i-8;
6563     if (dops[i].itype == IMM16)
6564       i--;
6565     // swl r2, 15(r6); swr r2, 12(r6); sw r6, *; jalr r6
6566     if (dops[i].itype == STORELR && dops[i].rs1 == 6
6567       && dops[i-1].itype == STORELR && dops[i-1].rs1 == 6)
6568     {
6569       SysPrintf("F1 hack from %08x, old dst %08x\n", start, hack_addr);
6570       f1_hack = 1;
6571       return 1;
6572     }
6573   }
6574   if (Config.HLE)
6575   {
6576     if (start <= psxRegs.biosBranchCheck && psxRegs.biosBranchCheck < start + i*4)
6577     {
6578       i = (psxRegs.biosBranchCheck - start) / 4u + 23;
6579       if (dops[i].is_jump && !dops[i+1].bt)
6580       {
6581         force_intcall(i);
6582         dops[i+1].is_ds = 0;
6583       }
6584     }
6585   }
6586   return 0;
6587 }
6588
6589 static int is_ld_use_hazard(const struct decoded_insn *op_ld,
6590   const struct decoded_insn *op)
6591 {
6592   if (op_ld->rt1 == 0 || (op_ld->rt1 != op->rs1 && op_ld->rt1 != op->rs2))
6593     return 0;
6594   if (op_ld->itype == LOADLR && op->itype == LOADLR)
6595     return op_ld->rt1 == op_ld->rs1;
6596   return op->itype != CJUMP && op->itype != SJUMP;
6597 }
6598
6599 static void disassemble_one(int i, u_int src)
6600 {
6601     unsigned int type, op, op2, op3;
6602     enum ls_width_type ls_type = LS_32;
6603     memset(&dops[i], 0, sizeof(dops[i]));
6604     memset(&cinfo[i], 0, sizeof(cinfo[i]));
6605     cinfo[i].ba = -1;
6606     cinfo[i].addr = -1;
6607     dops[i].opcode = op = src >> 26;
6608     op2 = 0;
6609     type = INTCALL;
6610     set_mnemonic(i, "???");
6611     switch(op)
6612     {
6613       case 0x00: set_mnemonic(i, "special");
6614         op2 = src & 0x3f;
6615         switch(op2)
6616         {
6617           case 0x00: set_mnemonic(i, "SLL"); type=SHIFTIMM; break;
6618           case 0x02: set_mnemonic(i, "SRL"); type=SHIFTIMM; break;
6619           case 0x03: set_mnemonic(i, "SRA"); type=SHIFTIMM; break;
6620           case 0x04: set_mnemonic(i, "SLLV"); type=SHIFT; break;
6621           case 0x06: set_mnemonic(i, "SRLV"); type=SHIFT; break;
6622           case 0x07: set_mnemonic(i, "SRAV"); type=SHIFT; break;
6623           case 0x08: set_mnemonic(i, "JR"); type=RJUMP; break;
6624           case 0x09: set_mnemonic(i, "JALR"); type=RJUMP; break;
6625           case 0x0C: set_mnemonic(i, "SYSCALL"); type=SYSCALL; break;
6626           case 0x0D: set_mnemonic(i, "BREAK"); type=SYSCALL; break;
6627           case 0x10: set_mnemonic(i, "MFHI"); type=MOV; break;
6628           case 0x11: set_mnemonic(i, "MTHI"); type=MOV; break;
6629           case 0x12: set_mnemonic(i, "MFLO"); type=MOV; break;
6630           case 0x13: set_mnemonic(i, "MTLO"); type=MOV; break;
6631           case 0x18: set_mnemonic(i, "MULT"); type=MULTDIV; break;
6632           case 0x19: set_mnemonic(i, "MULTU"); type=MULTDIV; break;
6633           case 0x1A: set_mnemonic(i, "DIV"); type=MULTDIV; break;
6634           case 0x1B: set_mnemonic(i, "DIVU"); type=MULTDIV; break;
6635           case 0x20: set_mnemonic(i, "ADD"); type=ALU; break;
6636           case 0x21: set_mnemonic(i, "ADDU"); type=ALU; break;
6637           case 0x22: set_mnemonic(i, "SUB"); type=ALU; break;
6638           case 0x23: set_mnemonic(i, "SUBU"); type=ALU; break;
6639           case 0x24: set_mnemonic(i, "AND"); type=ALU; break;
6640           case 0x25: set_mnemonic(i, "OR"); type=ALU; break;
6641           case 0x26: set_mnemonic(i, "XOR"); type=ALU; break;
6642           case 0x27: set_mnemonic(i, "NOR"); type=ALU; break;
6643           case 0x2A: set_mnemonic(i, "SLT"); type=ALU; break;
6644           case 0x2B: set_mnemonic(i, "SLTU"); type=ALU; break;
6645         }
6646         break;
6647       case 0x01: set_mnemonic(i, "regimm");
6648         type = SJUMP;
6649         op2 = (src >> 16) & 0x1f;
6650         switch(op2)
6651         {
6652           case 0x10: set_mnemonic(i, "BLTZAL"); break;
6653           case 0x11: set_mnemonic(i, "BGEZAL"); break;
6654           default:
6655             if (op2 & 1)
6656               set_mnemonic(i, "BGEZ");
6657             else
6658               set_mnemonic(i, "BLTZ");
6659         }
6660         break;
6661       case 0x02: set_mnemonic(i, "J"); type=UJUMP; break;
6662       case 0x03: set_mnemonic(i, "JAL"); type=UJUMP; break;
6663       case 0x04: set_mnemonic(i, "BEQ"); type=CJUMP; break;
6664       case 0x05: set_mnemonic(i, "BNE"); type=CJUMP; break;
6665       case 0x06: set_mnemonic(i, "BLEZ"); type=CJUMP; break;
6666       case 0x07: set_mnemonic(i, "BGTZ"); type=CJUMP; break;
6667       case 0x08: set_mnemonic(i, "ADDI"); type=IMM16; break;
6668       case 0x09: set_mnemonic(i, "ADDIU"); type=IMM16; break;
6669       case 0x0A: set_mnemonic(i, "SLTI"); type=IMM16; break;
6670       case 0x0B: set_mnemonic(i, "SLTIU"); type=IMM16; break;
6671       case 0x0C: set_mnemonic(i, "ANDI"); type=IMM16; break;
6672       case 0x0D: set_mnemonic(i, "ORI"); type=IMM16; break;
6673       case 0x0E: set_mnemonic(i, "XORI"); type=IMM16; break;
6674       case 0x0F: set_mnemonic(i, "LUI"); type=IMM16; break;
6675       case 0x10: set_mnemonic(i, "COP0");
6676         op2 = (src >> 21) & 0x1f;
6677         if (op2 & 0x10) {
6678           op3 = src & 0x1f;
6679           switch (op3)
6680           {
6681             case 0x01: case 0x02: case 0x06: case 0x08: type = INTCALL; break;
6682             case 0x10: set_mnemonic(i, "RFE"); type=RFE; break;
6683             default:   type = OTHER; break;
6684           }
6685           break;
6686         }
6687         switch(op2)
6688         {
6689           u32 rd;
6690           case 0x00:
6691             set_mnemonic(i, "MFC0");
6692             rd = (src >> 11) & 0x1F;
6693             if (!(0x00000417u & (1u << rd)))
6694               type = COP0;
6695             break;
6696           case 0x04: set_mnemonic(i, "MTC0"); type=COP0; break;
6697           case 0x02:
6698           case 0x06: type = INTCALL; break;
6699           default:   type = OTHER; break;
6700         }
6701         break;
6702       case 0x11: set_mnemonic(i, "COP1");
6703         op2 = (src >> 21) & 0x1f;
6704         break;
6705       case 0x12: set_mnemonic(i, "COP2");
6706         op2 = (src >> 21) & 0x1f;
6707         if (op2 & 0x10) {
6708           type = OTHER;
6709           if (gte_handlers[src & 0x3f] != NULL) {
6710 #ifdef DISASM
6711             if (gte_regnames[src & 0x3f] != NULL)
6712               strcpy(insn[i], gte_regnames[src & 0x3f]);
6713             else
6714               snprintf(insn[i], sizeof(insn[i]), "COP2 %x", src & 0x3f);
6715 #endif
6716             type = C2OP;
6717           }
6718         }
6719         else switch(op2)
6720         {
6721           case 0x00: set_mnemonic(i, "MFC2"); type=COP2; break;
6722           case 0x02: set_mnemonic(i, "CFC2"); type=COP2; break;
6723           case 0x04: set_mnemonic(i, "MTC2"); type=COP2; break;
6724           case 0x06: set_mnemonic(i, "CTC2"); type=COP2; break;
6725         }
6726         break;
6727       case 0x13: set_mnemonic(i, "COP3");
6728         op2 = (src >> 21) & 0x1f;
6729         break;
6730       case 0x20: set_mnemonic(i, "LB"); type=LOAD; ls_type = LS_8; break;
6731       case 0x21: set_mnemonic(i, "LH"); type=LOAD; ls_type = LS_16; break;
6732       case 0x22: set_mnemonic(i, "LWL"); type=LOADLR; ls_type = LS_LR; break;
6733       case 0x23: set_mnemonic(i, "LW"); type=LOAD; ls_type = LS_32; break;
6734       case 0x24: set_mnemonic(i, "LBU"); type=LOAD; ls_type = LS_8; break;
6735       case 0x25: set_mnemonic(i, "LHU"); type=LOAD; ls_type = LS_16; break;
6736       case 0x26: set_mnemonic(i, "LWR"); type=LOADLR; ls_type = LS_LR; break;
6737       case 0x28: set_mnemonic(i, "SB"); type=STORE; ls_type = LS_8; break;
6738       case 0x29: set_mnemonic(i, "SH"); type=STORE; ls_type = LS_16; break;
6739       case 0x2A: set_mnemonic(i, "SWL"); type=STORELR; ls_type = LS_LR; break;
6740       case 0x2B: set_mnemonic(i, "SW"); type=STORE; ls_type = LS_32; break;
6741       case 0x2E: set_mnemonic(i, "SWR"); type=STORELR; ls_type = LS_LR; break;
6742       case 0x32: set_mnemonic(i, "LWC2"); type=C2LS; ls_type = LS_32; break;
6743       case 0x3A: set_mnemonic(i, "SWC2"); type=C2LS; ls_type = LS_32; break;
6744       case 0x3B:
6745         if (Config.HLE && (src & 0x03ffffff) < ARRAY_SIZE(psxHLEt)) {
6746           set_mnemonic(i, "HLECALL");
6747           type = HLECALL;
6748         }
6749         break;
6750       default:
6751         break;
6752     }
6753     if (type == INTCALL)
6754       SysPrintf("NI %08x @%08x (%08x)\n", src, start + i*4, start);
6755     dops[i].itype = type;
6756     dops[i].opcode2 = op2;
6757     dops[i].ls_type = ls_type;
6758     /* Get registers/immediates */
6759     dops[i].use_lt1=0;
6760     gte_rs[i]=gte_rt[i]=0;
6761     dops[i].rs1 = 0;
6762     dops[i].rs2 = 0;
6763     dops[i].rt1 = 0;
6764     dops[i].rt2 = 0;
6765     switch(type) {
6766       case LOAD:
6767         dops[i].rs1 = (src >> 21) & 0x1f;
6768         dops[i].rt1 = (src >> 16) & 0x1f;
6769         cinfo[i].imm = (short)src;
6770         break;
6771       case STORE:
6772       case STORELR:
6773         dops[i].rs1 = (src >> 21) & 0x1f;
6774         dops[i].rs2 = (src >> 16) & 0x1f;
6775         cinfo[i].imm = (short)src;
6776         break;
6777       case LOADLR:
6778         // LWL/LWR only load part of the register,
6779         // therefore the target register must be treated as a source too
6780         dops[i].rs1 = (src >> 21) & 0x1f;
6781         dops[i].rs2 = (src >> 16) & 0x1f;
6782         dops[i].rt1 = (src >> 16) & 0x1f;
6783         cinfo[i].imm = (short)src;
6784         break;
6785       case IMM16:
6786         if (op==0x0f) dops[i].rs1=0; // LUI instruction has no source register
6787         else dops[i].rs1 = (src >> 21) & 0x1f;
6788         dops[i].rs2 = 0;
6789         dops[i].rt1 = (src >> 16) & 0x1f;
6790         if(op>=0x0c&&op<=0x0e) { // ANDI/ORI/XORI
6791           cinfo[i].imm = (unsigned short)src;
6792         }else{
6793           cinfo[i].imm = (short)src;
6794         }
6795         break;
6796       case UJUMP:
6797         // The JAL instruction writes to r31.
6798         if (op&1) {
6799           dops[i].rt1=31;
6800         }
6801         dops[i].rs2=CCREG;
6802         break;
6803       case RJUMP:
6804         dops[i].rs1 = (src >> 21) & 0x1f;
6805         // The JALR instruction writes to rd.
6806         if (op2&1) {
6807           dops[i].rt1 = (src >> 11) & 0x1f;
6808         }
6809         dops[i].rs2=CCREG;
6810         break;
6811       case CJUMP:
6812         dops[i].rs1 = (src >> 21) & 0x1f;
6813         dops[i].rs2 = (src >> 16) & 0x1f;
6814         if(op&2) { // BGTZ/BLEZ
6815           dops[i].rs2=0;
6816         }
6817         break;
6818       case SJUMP:
6819         dops[i].rs1 = (src >> 21) & 0x1f;
6820         dops[i].rs2 = CCREG;
6821         if (op2 == 0x10 || op2 == 0x11) { // BxxAL
6822           dops[i].rt1 = 31;
6823           // NOTE: If the branch is not taken, r31 is still overwritten
6824         }
6825         break;
6826       case ALU:
6827         dops[i].rs1=(src>>21)&0x1f; // source
6828         dops[i].rs2=(src>>16)&0x1f; // subtract amount
6829         dops[i].rt1=(src>>11)&0x1f; // destination
6830         break;
6831       case MULTDIV:
6832         dops[i].rs1=(src>>21)&0x1f; // source
6833         dops[i].rs2=(src>>16)&0x1f; // divisor
6834         dops[i].rt1=HIREG;
6835         dops[i].rt2=LOREG;
6836         break;
6837       case MOV:
6838         if(op2==0x10) dops[i].rs1=HIREG; // MFHI
6839         if(op2==0x11) dops[i].rt1=HIREG; // MTHI
6840         if(op2==0x12) dops[i].rs1=LOREG; // MFLO
6841         if(op2==0x13) dops[i].rt1=LOREG; // MTLO
6842         if((op2&0x1d)==0x10) dops[i].rt1=(src>>11)&0x1f; // MFxx
6843         if((op2&0x1d)==0x11) dops[i].rs1=(src>>21)&0x1f; // MTxx
6844         break;
6845       case SHIFT:
6846         dops[i].rs1=(src>>16)&0x1f; // target of shift
6847         dops[i].rs2=(src>>21)&0x1f; // shift amount
6848         dops[i].rt1=(src>>11)&0x1f; // destination
6849         break;
6850       case SHIFTIMM:
6851         dops[i].rs1=(src>>16)&0x1f;
6852         dops[i].rs2=0;
6853         dops[i].rt1=(src>>11)&0x1f;
6854         cinfo[i].imm=(src>>6)&0x1f;
6855         break;
6856       case COP0:
6857         if(op2==0) dops[i].rt1=(src>>16)&0x1F; // MFC0
6858         if(op2==4) dops[i].rs1=(src>>16)&0x1F; // MTC0
6859         if(op2==4&&((src>>11)&0x1e)==12) dops[i].rs2=CCREG;
6860         break;
6861       case COP2:
6862         if(op2<3) dops[i].rt1=(src>>16)&0x1F; // MFC2/CFC2
6863         if(op2>3) dops[i].rs1=(src>>16)&0x1F; // MTC2/CTC2
6864         int gr=(src>>11)&0x1F;
6865         switch(op2)
6866         {
6867           case 0x00: gte_rs[i]=1ll<<gr; break; // MFC2
6868           case 0x04: gte_rt[i]=1ll<<gr; break; // MTC2
6869           case 0x02: gte_rs[i]=1ll<<(gr+32); break; // CFC2
6870           case 0x06: gte_rt[i]=1ll<<(gr+32); break; // CTC2
6871         }
6872         break;
6873       case C2LS:
6874         dops[i].rs1=(src>>21)&0x1F;
6875         cinfo[i].imm=(short)src;
6876         if(op==0x32) gte_rt[i]=1ll<<((src>>16)&0x1F); // LWC2
6877         else gte_rs[i]=1ll<<((src>>16)&0x1F); // SWC2
6878         break;
6879       case C2OP:
6880         gte_rs[i]=gte_reg_reads[src&0x3f];
6881         gte_rt[i]=gte_reg_writes[src&0x3f];
6882         gte_rt[i]|=1ll<<63; // every op changes flags
6883         if((src&0x3f)==GTE_MVMVA) {
6884           int v = (src >> 15) & 3;
6885           gte_rs[i]&=~0xe3fll;
6886           if(v==3) gte_rs[i]|=0xe00ll;
6887           else gte_rs[i]|=3ll<<(v*2);
6888         }
6889         break;
6890       case SYSCALL:
6891       case HLECALL:
6892       case INTCALL:
6893         dops[i].rs1=CCREG;
6894         break;
6895       default:
6896         break;
6897     }
6898 }
6899
6900 static noinline void pass1_disassemble(u_int pagelimit)
6901 {
6902   int i, j, done = 0, ni_count = 0;
6903   int ds_next = 0;
6904
6905   for (i = 0; !done; i++)
6906   {
6907     int force_j_to_interpreter = 0;
6908     unsigned int type, op, op2;
6909
6910     disassemble_one(i, source[i]);
6911     dops[i].is_ds = ds_next; ds_next = 0;
6912     type = dops[i].itype;
6913     op = dops[i].opcode;
6914     op2 = dops[i].opcode2;
6915
6916     /* Calculate branch target addresses */
6917     if(type==UJUMP)
6918       cinfo[i].ba=((start+i*4+4)&0xF0000000)|(((unsigned int)source[i]<<6)>>4);
6919     else if(type==CJUMP&&dops[i].rs1==dops[i].rs2&&(op&1))
6920       cinfo[i].ba=start+i*4+8; // Ignore never taken branch
6921     else if(type==SJUMP&&dops[i].rs1==0&&!(op2&1))
6922       cinfo[i].ba=start+i*4+8; // Ignore never taken branch
6923     else if(type==CJUMP||type==SJUMP)
6924       cinfo[i].ba=start+i*4+4+((signed int)((unsigned int)source[i]<<16)>>14);
6925
6926     /* simplify always (not)taken branches */
6927     if (type == CJUMP && dops[i].rs1 == dops[i].rs2) {
6928       dops[i].rs1 = dops[i].rs2 = 0;
6929       if (!(op & 1)) {
6930         dops[i].itype = type = UJUMP;
6931         dops[i].rs2 = CCREG;
6932       }
6933     }
6934     else if (type == SJUMP && dops[i].rs1 == 0 && (op2 & 1))
6935       dops[i].itype = type = UJUMP;
6936
6937     dops[i].is_jump  = type == RJUMP || type == UJUMP || type == CJUMP || type == SJUMP;
6938     dops[i].is_ujump = type == RJUMP || type == UJUMP;
6939     dops[i].is_load  = type == LOAD || type == LOADLR || op == 0x32; // LWC2
6940     dops[i].is_delay_load = (dops[i].is_load || (source[i] & 0xf3d00000) == 0x40000000); // MFC/CFC
6941     dops[i].is_store = type == STORE || type == STORELR || op == 0x3a; // SWC2
6942     dops[i].is_exception = type == SYSCALL || type == HLECALL || type == INTCALL;
6943     dops[i].may_except = dops[i].is_exception || (type == ALU && (op2 == 0x20 || op2 == 0x22)) || op == 8;
6944     ds_next = dops[i].is_jump;
6945
6946     if (((op & 0x37) == 0x21 || op == 0x25) // LH/SH/LHU
6947         && ((cinfo[i].imm & 1) || Config.PreciseExceptions))
6948       dops[i].may_except = 1;
6949     if (((op & 0x37) == 0x23 || (op & 0x37) == 0x32) // LW/SW/LWC2/SWC2
6950         && ((cinfo[i].imm & 3) || Config.PreciseExceptions))
6951       dops[i].may_except = 1;
6952
6953     /* rare messy cases to just pass over to the interpreter */
6954     if (i > 0 && dops[i-1].is_jump) {
6955       j = i - 1;
6956       // branch in delay slot?
6957       if (dops[i].is_jump) {
6958         // don't handle first branch and call interpreter if it's hit
6959         SysPrintf("branch in DS @%08x (%08x)\n", start + i*4, start);
6960         force_j_to_interpreter = 1;
6961       }
6962       // load delay detection through a branch
6963       else if (dops[i].is_delay_load && dops[i].rt1 != 0) {
6964         const struct decoded_insn *dop = NULL;
6965         int t = -1;
6966         if (cinfo[i-1].ba != -1) {
6967           t = (cinfo[i-1].ba - start) / 4;
6968           if (t < 0 || t > i) {
6969             u_int limit = 0;
6970             u_int *mem = get_source_start(cinfo[i-1].ba, &limit);
6971             if (mem != NULL) {
6972               disassemble_one(MAXBLOCK - 1, mem[0]);
6973               dop = &dops[MAXBLOCK - 1];
6974             }
6975           }
6976           else
6977             dop = &dops[t];
6978         }
6979         if ((dop && is_ld_use_hazard(&dops[i], dop))
6980             || (!dop && Config.PreciseExceptions)) {
6981           // jump target wants DS result - potential load delay effect
6982           SysPrintf("load delay in DS @%08x (%08x)\n", start + i*4, start);
6983           force_j_to_interpreter = 1;
6984           if (0 <= t && t < i)
6985             dops[t + 1].bt = 1; // expected return from interpreter
6986         }
6987         else if(i>=2&&dops[i-2].rt1==2&&dops[i].rt1==2&&dops[i].rs1!=2&&dops[i].rs2!=2&&dops[i-1].rs1!=2&&dops[i-1].rs2!=2&&
6988               !(i>=3&&dops[i-3].is_jump)) {
6989           // v0 overwrite like this is a sign of trouble, bail out
6990           SysPrintf("v0 overwrite @%08x (%08x)\n", start + i*4, start);
6991           force_j_to_interpreter = 1;
6992         }
6993       }
6994     }
6995     else if (i > 0 && dops[i-1].is_delay_load
6996              && is_ld_use_hazard(&dops[i-1], &dops[i])
6997              && (i < 2 || !dops[i-2].is_ujump)) {
6998       SysPrintf("load delay @%08x (%08x)\n", start + i*4, start);
6999       for (j = i - 1; j > 0 && dops[j-1].is_delay_load; j--)
7000         if (dops[j-1].rt1 != dops[i-1].rt1)
7001           break;
7002       force_j_to_interpreter = 1;
7003     }
7004     if (force_j_to_interpreter) {
7005       force_intcall(j);
7006       done = 2;
7007       i = j; // don't compile the problematic branch/load/etc
7008     }
7009     if (dops[i].is_exception && i > 0 && dops[i-1].is_jump) {
7010       SysPrintf("exception in DS @%08x (%08x)\n", start + i*4, start);
7011       i--;
7012       force_intcall(i);
7013       done = 2;
7014     }
7015     if (i >= 2 && (source[i-2] & 0xffe0f800) == 0x40806000) // MTC0 $12
7016       dops[i].bt = 1;
7017     if (i >= 1 && (source[i-1] & 0xffe0f800) == 0x40806800) // MTC0 $13
7018       dops[i].bt = 1;
7019
7020     /* Is this the end of the block? */
7021     if (i > 0 && dops[i-1].is_ujump) {
7022       if (dops[i-1].rt1 == 0) { // not jal
7023         int found_bbranch = 0, t = (cinfo[i-1].ba - start) / 4;
7024         if ((u_int)(t - i) < 64 && start + (t+64)*4 < pagelimit) {
7025           // scan for a branch back to i+1
7026           for (j = t; j < t + 64; j++) {
7027             int tmpop = source[j] >> 26;
7028             if (tmpop == 1 || ((tmpop & ~3) == 4)) {
7029               int t2 = j + 1 + (int)(signed short)source[j];
7030               if (t2 == i + 1) {
7031                 //printf("blk expand %08x<-%08x\n", start + (i+1)*4, start + j*4);
7032                 found_bbranch = 1;
7033                 break;
7034               }
7035             }
7036           }
7037         }
7038         if (!found_bbranch)
7039           done = 2;
7040       }
7041       else {
7042         if(stop_after_jal) done=1;
7043         // Stop on BREAK
7044         if((source[i+1]&0xfc00003f)==0x0d) done=1;
7045       }
7046       // Don't recompile stuff that's already compiled
7047       if(check_addr(start+i*4+4)) done=1;
7048       // Don't get too close to the limit
7049       if (i > MAXBLOCK - 64)
7050         done = 1;
7051     }
7052     if (dops[i].itype == HLECALL)
7053       done = 1;
7054     else if (dops[i].itype == INTCALL)
7055       done = 2;
7056     else if (dops[i].is_exception)
7057       done = stop_after_jal ? 1 : 2;
7058     if (done == 2) {
7059       // Does the block continue due to a branch?
7060       for(j=i-1;j>=0;j--)
7061       {
7062         if(cinfo[j].ba==start+i*4) done=j=0; // Branch into delay slot
7063         if(cinfo[j].ba==start+i*4+4) done=j=0;
7064         if(cinfo[j].ba==start+i*4+8) done=j=0;
7065       }
7066     }
7067     //assert(i<MAXBLOCK-1);
7068     if(start+i*4==pagelimit-4) done=1;
7069     assert(start+i*4<pagelimit);
7070     if (i == MAXBLOCK - 2)
7071       done = 1;
7072     // Stop if we're compiling junk
7073     if (dops[i].itype == INTCALL && (++ni_count > 8 || dops[i].opcode == 0x11)) {
7074       done=stop_after_jal=1;
7075       SysPrintf("Disabled speculative precompilation\n");
7076     }
7077   }
7078   while (i > 0 && dops[i-1].is_jump)
7079     i--;
7080   assert(i > 0);
7081   assert(!dops[i-1].is_jump);
7082   slen = i;
7083 }
7084
7085 // Basic liveness analysis for MIPS registers
7086 static noinline void pass2_unneeded_regs(int istart,int iend,int r)
7087 {
7088   int i;
7089   uint64_t u,gte_u,b,gte_b;
7090   uint64_t temp_u,temp_gte_u=0;
7091   uint64_t gte_u_unknown=0;
7092   if (HACK_ENABLED(NDHACK_GTE_UNNEEDED))
7093     gte_u_unknown=~0ll;
7094   if(iend==slen-1) {
7095     u=1;
7096     gte_u=gte_u_unknown;
7097   }else{
7098     //u=unneeded_reg[iend+1];
7099     u=1;
7100     gte_u=gte_unneeded[iend+1];
7101   }
7102
7103   for (i=iend;i>=istart;i--)
7104   {
7105     //printf("unneeded registers i=%d (%d,%d) r=%d\n",i,istart,iend,r);
7106     if(dops[i].is_jump)
7107     {
7108       // If subroutine call, flag return address as a possible branch target
7109       if(dops[i].rt1==31 && i<slen-2) dops[i+2].bt=1;
7110
7111       if(cinfo[i].ba<start || cinfo[i].ba>=(start+slen*4))
7112       {
7113         // Branch out of this block, flush all regs
7114         u=1;
7115         gte_u=gte_u_unknown;
7116         branch_unneeded_reg[i]=u;
7117         // Merge in delay slot
7118         u|=(1LL<<dops[i+1].rt1)|(1LL<<dops[i+1].rt2);
7119         u&=~((1LL<<dops[i+1].rs1)|(1LL<<dops[i+1].rs2));
7120         u|=1;
7121         gte_u|=gte_rt[i+1];
7122         gte_u&=~gte_rs[i+1];
7123       }
7124       else
7125       {
7126         // Internal branch, flag target
7127         dops[(cinfo[i].ba-start)>>2].bt=1;
7128         if(cinfo[i].ba<=start+i*4) {
7129           // Backward branch
7130           if(dops[i].is_ujump)
7131           {
7132             // Unconditional branch
7133             temp_u=1;
7134             temp_gte_u=0;
7135           } else {
7136             // Conditional branch (not taken case)
7137             temp_u=unneeded_reg[i+2];
7138             temp_gte_u&=gte_unneeded[i+2];
7139           }
7140           // Merge in delay slot
7141           temp_u|=(1LL<<dops[i+1].rt1)|(1LL<<dops[i+1].rt2);
7142           temp_u&=~((1LL<<dops[i+1].rs1)|(1LL<<dops[i+1].rs2));
7143           temp_u|=1;
7144           temp_gte_u|=gte_rt[i+1];
7145           temp_gte_u&=~gte_rs[i+1];
7146           temp_u|=(1LL<<dops[i].rt1)|(1LL<<dops[i].rt2);
7147           temp_u&=~((1LL<<dops[i].rs1)|(1LL<<dops[i].rs2));
7148           temp_u|=1;
7149           temp_gte_u|=gte_rt[i];
7150           temp_gte_u&=~gte_rs[i];
7151           unneeded_reg[i]=temp_u;
7152           gte_unneeded[i]=temp_gte_u;
7153           // Only go three levels deep.  This recursion can take an
7154           // excessive amount of time if there are a lot of nested loops.
7155           if(r<2) {
7156             pass2_unneeded_regs((cinfo[i].ba-start)>>2,i-1,r+1);
7157           }else{
7158             unneeded_reg[(cinfo[i].ba-start)>>2]=1;
7159             gte_unneeded[(cinfo[i].ba-start)>>2]=gte_u_unknown;
7160           }
7161         } /*else*/ if(1) {
7162           if (dops[i].is_ujump)
7163           {
7164             // Unconditional branch
7165             u=unneeded_reg[(cinfo[i].ba-start)>>2];
7166             gte_u=gte_unneeded[(cinfo[i].ba-start)>>2];
7167             branch_unneeded_reg[i]=u;
7168             // Merge in delay slot
7169             u|=(1LL<<dops[i+1].rt1)|(1LL<<dops[i+1].rt2);
7170             u&=~((1LL<<dops[i+1].rs1)|(1LL<<dops[i+1].rs2));
7171             u|=1;
7172             gte_u|=gte_rt[i+1];
7173             gte_u&=~gte_rs[i+1];
7174           } else {
7175             // Conditional branch
7176             b=unneeded_reg[(cinfo[i].ba-start)>>2];
7177             gte_b=gte_unneeded[(cinfo[i].ba-start)>>2];
7178             branch_unneeded_reg[i]=b;
7179             // Branch delay slot
7180             b|=(1LL<<dops[i+1].rt1)|(1LL<<dops[i+1].rt2);
7181             b&=~((1LL<<dops[i+1].rs1)|(1LL<<dops[i+1].rs2));
7182             b|=1;
7183             gte_b|=gte_rt[i+1];
7184             gte_b&=~gte_rs[i+1];
7185             u&=b;
7186             gte_u&=gte_b;
7187             if(i<slen-1) {
7188               branch_unneeded_reg[i]&=unneeded_reg[i+2];
7189             } else {
7190               branch_unneeded_reg[i]=1;
7191             }
7192           }
7193         }
7194       }
7195     }
7196     //u=1; // DEBUG
7197     // Written registers are unneeded
7198     u|=1LL<<dops[i].rt1;
7199     u|=1LL<<dops[i].rt2;
7200     gte_u|=gte_rt[i];
7201     // Accessed registers are needed
7202     u&=~(1LL<<dops[i].rs1);
7203     u&=~(1LL<<dops[i].rs2);
7204     gte_u&=~gte_rs[i];
7205     if(gte_rs[i]&&dops[i].rt1&&(unneeded_reg[i+1]&(1ll<<dops[i].rt1)))
7206       gte_u|=gte_rs[i]&gte_unneeded[i+1]; // MFC2/CFC2 to dead register, unneeded
7207     if (dops[i].may_except || dops[i].itype == RFE)
7208     {
7209       // SYSCALL instruction, etc or conditional exception
7210       u=1;
7211     }
7212     // Source-target dependencies
7213     // R0 is always unneeded
7214     u|=1;
7215     // Save it
7216     unneeded_reg[i]=u;
7217     gte_unneeded[i]=gte_u;
7218     /*
7219     printf("ur (%d,%d) %x: ",istart,iend,start+i*4);
7220     printf("U:");
7221     int r;
7222     for(r=1;r<=CCREG;r++) {
7223       if((unneeded_reg[i]>>r)&1) {
7224         if(r==HIREG) printf(" HI");
7225         else if(r==LOREG) printf(" LO");
7226         else printf(" r%d",r);
7227       }
7228     }
7229     printf("\n");
7230     */
7231   }
7232 }
7233
7234 static noinline void pass2a_unneeded_other(void)
7235 {
7236   int i, j;
7237   for (i = 0; i < slen; i++)
7238   {
7239     // remove redundant alignment checks
7240     if (dops[i].may_except && (dops[i].is_load || dops[i].is_store)
7241         && dops[i].rt1 != dops[i].rs1 && !dops[i].is_ds)
7242     {
7243       int base = dops[i].rs1, lsb = cinfo[i].imm, ls_type = dops[i].ls_type;
7244       int mask = ls_type == LS_32 ? 3 : 1;
7245       lsb &= mask;
7246       for (j = i + 1; j < slen; j++) {
7247         if (dops[j].bt || dops[j].is_jump)
7248           break;
7249         if ((dops[j].is_load || dops[j].is_store) && dops[j].rs1 == base
7250             && dops[j].ls_type == ls_type && (cinfo[j].imm & mask) == lsb)
7251           dops[j].may_except = 0;
7252         if (dops[j].rt1 == base)
7253           break;
7254       }
7255     }
7256   }
7257 }
7258
7259 static noinline void pass3_register_alloc(u_int addr)
7260 {
7261   struct regstat current; // Current register allocations/status
7262   clear_all_regs(current.regmap_entry);
7263   clear_all_regs(current.regmap);
7264   current.wasdirty = current.dirty = 0;
7265   current.u = unneeded_reg[0];
7266   alloc_reg(&current, 0, CCREG);
7267   dirty_reg(&current, CCREG);
7268   current.wasconst = 0;
7269   current.isconst = 0;
7270   current.loadedconst = 0;
7271   current.noevict = 0;
7272   //current.waswritten = 0;
7273   int ds=0;
7274   int cc=0;
7275   int hr;
7276   int i, j;
7277
7278   if (addr & 1) {
7279     // First instruction is delay slot
7280     cc=-1;
7281     dops[1].bt=1;
7282     ds=1;
7283     unneeded_reg[0]=1;
7284   }
7285
7286   for(i=0;i<slen;i++)
7287   {
7288     if(dops[i].bt)
7289     {
7290       for(hr=0;hr<HOST_REGS;hr++)
7291       {
7292         // Is this really necessary?
7293         if(current.regmap[hr]==0) current.regmap[hr]=-1;
7294       }
7295       current.isconst=0;
7296       //current.waswritten=0;
7297     }
7298
7299     memcpy(regmap_pre[i],current.regmap,sizeof(current.regmap));
7300     regs[i].wasconst=current.isconst;
7301     regs[i].wasdirty=current.dirty;
7302     regs[i].dirty=0;
7303     regs[i].u=0;
7304     regs[i].isconst=0;
7305     regs[i].loadedconst=0;
7306     if (!dops[i].is_jump) {
7307       if(i+1<slen) {
7308         current.u=unneeded_reg[i+1]&~((1LL<<dops[i].rs1)|(1LL<<dops[i].rs2));
7309         current.u|=1;
7310       } else {
7311         current.u=1;
7312       }
7313     } else {
7314       if(i+1<slen) {
7315         current.u=branch_unneeded_reg[i]&~((1LL<<dops[i+1].rs1)|(1LL<<dops[i+1].rs2));
7316         current.u&=~((1LL<<dops[i].rs1)|(1LL<<dops[i].rs2));
7317         current.u|=1;
7318       } else {
7319         SysPrintf("oops, branch at end of block with no delay slot @%08x\n", start + i*4);
7320         abort();
7321       }
7322     }
7323     assert(dops[i].is_ds == ds);
7324     if(ds) {
7325       ds=0; // Skip delay slot, already allocated as part of branch
7326       // ...but we need to alloc it in case something jumps here
7327       if(i+1<slen) {
7328         current.u=branch_unneeded_reg[i-1]&unneeded_reg[i+1];
7329       }else{
7330         current.u=branch_unneeded_reg[i-1];
7331       }
7332       current.u&=~((1LL<<dops[i].rs1)|(1LL<<dops[i].rs2));
7333       current.u|=1;
7334       struct regstat temp;
7335       memcpy(&temp,&current,sizeof(current));
7336       temp.wasdirty=temp.dirty;
7337       // TODO: Take into account unconditional branches, as below
7338       delayslot_alloc(&temp,i);
7339       memcpy(regs[i].regmap,temp.regmap,sizeof(temp.regmap));
7340       regs[i].wasdirty=temp.wasdirty;
7341       regs[i].dirty=temp.dirty;
7342       regs[i].isconst=0;
7343       regs[i].wasconst=0;
7344       current.isconst=0;
7345       // Create entry (branch target) regmap
7346       for(hr=0;hr<HOST_REGS;hr++)
7347       {
7348         int r=temp.regmap[hr];
7349         if(r>=0) {
7350           if(r!=regmap_pre[i][hr]) {
7351             regs[i].regmap_entry[hr]=-1;
7352           }
7353           else
7354           {
7355               assert(r < 64);
7356               if((current.u>>r)&1) {
7357                 regs[i].regmap_entry[hr]=-1;
7358                 regs[i].regmap[hr]=-1;
7359                 //Don't clear regs in the delay slot as the branch might need them
7360                 //current.regmap[hr]=-1;
7361               }else
7362                 regs[i].regmap_entry[hr]=r;
7363           }
7364         } else {
7365           // First instruction expects CCREG to be allocated
7366           if(i==0&&hr==HOST_CCREG)
7367             regs[i].regmap_entry[hr]=CCREG;
7368           else
7369             regs[i].regmap_entry[hr]=-1;
7370         }
7371       }
7372     }
7373     else { // Not delay slot
7374       current.noevict = 0;
7375       switch(dops[i].itype) {
7376         case UJUMP:
7377           //current.isconst=0; // DEBUG
7378           //current.wasconst=0; // DEBUG
7379           //regs[i].wasconst=0; // DEBUG
7380           clear_const(&current,dops[i].rt1);
7381           alloc_cc(&current,i);
7382           dirty_reg(&current,CCREG);
7383           if (dops[i].rt1==31) {
7384             alloc_reg(&current,i,31);
7385             dirty_reg(&current,31);
7386             //assert(dops[i+1].rs1!=31&&dops[i+1].rs2!=31);
7387             //assert(dops[i+1].rt1!=dops[i].rt1);
7388             #ifdef REG_PREFETCH
7389             alloc_reg(&current,i,PTEMP);
7390             #endif
7391           }
7392           dops[i].ooo=1;
7393           delayslot_alloc(&current,i+1);
7394           //current.isconst=0; // DEBUG
7395           ds=1;
7396           break;
7397         case RJUMP:
7398           //current.isconst=0;
7399           //current.wasconst=0;
7400           //regs[i].wasconst=0;
7401           clear_const(&current,dops[i].rs1);
7402           clear_const(&current,dops[i].rt1);
7403           alloc_cc(&current,i);
7404           dirty_reg(&current,CCREG);
7405           if (!ds_writes_rjump_rs(i)) {
7406             alloc_reg(&current,i,dops[i].rs1);
7407             if (dops[i].rt1!=0) {
7408               alloc_reg(&current,i,dops[i].rt1);
7409               dirty_reg(&current,dops[i].rt1);
7410               #ifdef REG_PREFETCH
7411               alloc_reg(&current,i,PTEMP);
7412               #endif
7413             }
7414             #ifdef USE_MINI_HT
7415             if(dops[i].rs1==31) { // JALR
7416               alloc_reg(&current,i,RHASH);
7417               alloc_reg(&current,i,RHTBL);
7418             }
7419             #endif
7420             delayslot_alloc(&current,i+1);
7421           } else {
7422             // The delay slot overwrites our source register,
7423             // allocate a temporary register to hold the old value.
7424             current.isconst=0;
7425             current.wasconst=0;
7426             regs[i].wasconst=0;
7427             delayslot_alloc(&current,i+1);
7428             current.isconst=0;
7429             alloc_reg(&current,i,RTEMP);
7430           }
7431           //current.isconst=0; // DEBUG
7432           dops[i].ooo=1;
7433           ds=1;
7434           break;
7435         case CJUMP:
7436           //current.isconst=0;
7437           //current.wasconst=0;
7438           //regs[i].wasconst=0;
7439           clear_const(&current,dops[i].rs1);
7440           clear_const(&current,dops[i].rs2);
7441           if((dops[i].opcode&0x3E)==4) // BEQ/BNE
7442           {
7443             alloc_cc(&current,i);
7444             dirty_reg(&current,CCREG);
7445             if(dops[i].rs1) alloc_reg(&current,i,dops[i].rs1);
7446             if(dops[i].rs2) alloc_reg(&current,i,dops[i].rs2);
7447             if((dops[i].rs1&&(dops[i].rs1==dops[i+1].rt1||dops[i].rs1==dops[i+1].rt2))||
7448                (dops[i].rs2&&(dops[i].rs2==dops[i+1].rt1||dops[i].rs2==dops[i+1].rt2))) {
7449               // The delay slot overwrites one of our conditions.
7450               // Allocate the branch condition registers instead.
7451               current.isconst=0;
7452               current.wasconst=0;
7453               regs[i].wasconst=0;
7454               if(dops[i].rs1) alloc_reg(&current,i,dops[i].rs1);
7455               if(dops[i].rs2) alloc_reg(&current,i,dops[i].rs2);
7456             }
7457             else
7458             {
7459               dops[i].ooo=1;
7460               delayslot_alloc(&current,i+1);
7461             }
7462           }
7463           else
7464           if((dops[i].opcode&0x3E)==6) // BLEZ/BGTZ
7465           {
7466             alloc_cc(&current,i);
7467             dirty_reg(&current,CCREG);
7468             alloc_reg(&current,i,dops[i].rs1);
7469             if(dops[i].rs1&&(dops[i].rs1==dops[i+1].rt1||dops[i].rs1==dops[i+1].rt2)) {
7470               // The delay slot overwrites one of our conditions.
7471               // Allocate the branch condition registers instead.
7472               current.isconst=0;
7473               current.wasconst=0;
7474               regs[i].wasconst=0;
7475               if(dops[i].rs1) alloc_reg(&current,i,dops[i].rs1);
7476             }
7477             else
7478             {
7479               dops[i].ooo=1;
7480               delayslot_alloc(&current,i+1);
7481             }
7482           }
7483           else
7484           // Don't alloc the delay slot yet because we might not execute it
7485           if((dops[i].opcode&0x3E)==0x14) // BEQL/BNEL
7486           {
7487             current.isconst=0;
7488             current.wasconst=0;
7489             regs[i].wasconst=0;
7490             alloc_cc(&current,i);
7491             dirty_reg(&current,CCREG);
7492             alloc_reg(&current,i,dops[i].rs1);
7493             alloc_reg(&current,i,dops[i].rs2);
7494           }
7495           else
7496           if((dops[i].opcode&0x3E)==0x16) // BLEZL/BGTZL
7497           {
7498             current.isconst=0;
7499             current.wasconst=0;
7500             regs[i].wasconst=0;
7501             alloc_cc(&current,i);
7502             dirty_reg(&current,CCREG);
7503             alloc_reg(&current,i,dops[i].rs1);
7504           }
7505           ds=1;
7506           //current.isconst=0;
7507           break;
7508         case SJUMP:
7509           clear_const(&current,dops[i].rs1);
7510           clear_const(&current,dops[i].rt1);
7511           {
7512             alloc_cc(&current,i);
7513             dirty_reg(&current,CCREG);
7514             alloc_reg(&current,i,dops[i].rs1);
7515             if (dops[i].rt1 == 31) { // BLTZAL/BGEZAL
7516               alloc_reg(&current,i,31);
7517               dirty_reg(&current,31);
7518             }
7519             if ((dops[i].rs1 &&
7520                  (dops[i].rs1==dops[i+1].rt1||dops[i].rs1==dops[i+1].rt2)) // The delay slot overwrites the branch condition.
7521                ||(dops[i].rt1 == 31 && dops[i].rs1 == 31) // overwrites it's own condition
7522                ||(dops[i].rt1==31&&(dops[i+1].rs1==31||dops[i+1].rs2==31||dops[i+1].rt1==31||dops[i+1].rt2==31))) { // DS touches $ra
7523               // Allocate the branch condition registers instead.
7524               current.isconst=0;
7525               current.wasconst=0;
7526               regs[i].wasconst=0;
7527               if(dops[i].rs1) alloc_reg(&current,i,dops[i].rs1);
7528             }
7529             else
7530             {
7531               dops[i].ooo=1;
7532               delayslot_alloc(&current,i+1);
7533             }
7534           }
7535           ds=1;
7536           //current.isconst=0;
7537           break;
7538         case IMM16:
7539           imm16_alloc(&current,i);
7540           break;
7541         case LOAD:
7542         case LOADLR:
7543           load_alloc(&current,i);
7544           break;
7545         case STORE:
7546         case STORELR:
7547           store_alloc(&current,i);
7548           break;
7549         case ALU:
7550           alu_alloc(&current,i);
7551           break;
7552         case SHIFT:
7553           shift_alloc(&current,i);
7554           break;
7555         case MULTDIV:
7556           multdiv_alloc(&current,i);
7557           break;
7558         case SHIFTIMM:
7559           shiftimm_alloc(&current,i);
7560           break;
7561         case MOV:
7562           mov_alloc(&current,i);
7563           break;
7564         case COP0:
7565           cop0_alloc(&current,i);
7566           break;
7567         case RFE:
7568           rfe_alloc(&current,i);
7569           break;
7570         case COP2:
7571           cop2_alloc(&current,i);
7572           break;
7573         case C2LS:
7574           c2ls_alloc(&current,i);
7575           break;
7576         case C2OP:
7577           c2op_alloc(&current,i);
7578           break;
7579         case SYSCALL:
7580         case HLECALL:
7581         case INTCALL:
7582           syscall_alloc(&current,i);
7583           break;
7584       }
7585
7586       // Create entry (branch target) regmap
7587       for(hr=0;hr<HOST_REGS;hr++)
7588       {
7589         int r,or;
7590         r=current.regmap[hr];
7591         if(r>=0) {
7592           if(r!=regmap_pre[i][hr]) {
7593             // TODO: delay slot (?)
7594             or=get_reg(regmap_pre[i],r); // Get old mapping for this register
7595             if(or<0||r>=TEMPREG){
7596               regs[i].regmap_entry[hr]=-1;
7597             }
7598             else
7599             {
7600               // Just move it to a different register
7601               regs[i].regmap_entry[hr]=r;
7602               // If it was dirty before, it's still dirty
7603               if((regs[i].wasdirty>>or)&1) dirty_reg(&current,r);
7604             }
7605           }
7606           else
7607           {
7608             // Unneeded
7609             if(r==0){
7610               regs[i].regmap_entry[hr]=0;
7611             }
7612             else
7613             {
7614               assert(r<64);
7615               if((current.u>>r)&1) {
7616                 regs[i].regmap_entry[hr]=-1;
7617                 //regs[i].regmap[hr]=-1;
7618                 current.regmap[hr]=-1;
7619               }else
7620                 regs[i].regmap_entry[hr]=r;
7621             }
7622           }
7623         } else {
7624           // Branches expect CCREG to be allocated at the target
7625           if(regmap_pre[i][hr]==CCREG)
7626             regs[i].regmap_entry[hr]=CCREG;
7627           else
7628             regs[i].regmap_entry[hr]=-1;
7629         }
7630       }
7631       memcpy(regs[i].regmap,current.regmap,sizeof(current.regmap));
7632     }
7633
7634 #if 0 // see do_store_smc_check()
7635     if(i>0&&(dops[i-1].itype==STORE||dops[i-1].itype==STORELR||(dops[i-1].itype==C2LS&&dops[i-1].opcode==0x3a))&&(u_int)cinfo[i-1].imm<0x800)
7636       current.waswritten|=1<<dops[i-1].rs1;
7637     current.waswritten&=~(1<<dops[i].rt1);
7638     current.waswritten&=~(1<<dops[i].rt2);
7639     if((dops[i].itype==STORE||dops[i].itype==STORELR||(dops[i].itype==C2LS&&dops[i].opcode==0x3a))&&(u_int)cinfo[i].imm>=0x800)
7640       current.waswritten&=~(1<<dops[i].rs1);
7641 #endif
7642
7643     /* Branch post-alloc */
7644     if(i>0)
7645     {
7646       current.wasdirty=current.dirty;
7647       switch(dops[i-1].itype) {
7648         case UJUMP:
7649           memcpy(&branch_regs[i-1],&current,sizeof(current));
7650           branch_regs[i-1].isconst=0;
7651           branch_regs[i-1].wasconst=0;
7652           branch_regs[i-1].u=branch_unneeded_reg[i-1]&~((1LL<<dops[i-1].rs1)|(1LL<<dops[i-1].rs2));
7653           alloc_cc(&branch_regs[i-1],i-1);
7654           dirty_reg(&branch_regs[i-1],CCREG);
7655           if(dops[i-1].rt1==31) { // JAL
7656             alloc_reg(&branch_regs[i-1],i-1,31);
7657             dirty_reg(&branch_regs[i-1],31);
7658           }
7659           memcpy(&branch_regs[i-1].regmap_entry,&branch_regs[i-1].regmap,sizeof(current.regmap));
7660           memcpy(constmap[i],constmap[i-1],sizeof(constmap[i]));
7661           break;
7662         case RJUMP:
7663           memcpy(&branch_regs[i-1],&current,sizeof(current));
7664           branch_regs[i-1].isconst=0;
7665           branch_regs[i-1].wasconst=0;
7666           branch_regs[i-1].u=branch_unneeded_reg[i-1]&~((1LL<<dops[i-1].rs1)|(1LL<<dops[i-1].rs2));
7667           alloc_cc(&branch_regs[i-1],i-1);
7668           dirty_reg(&branch_regs[i-1],CCREG);
7669           alloc_reg(&branch_regs[i-1],i-1,dops[i-1].rs1);
7670           if(dops[i-1].rt1!=0) { // JALR
7671             alloc_reg(&branch_regs[i-1],i-1,dops[i-1].rt1);
7672             dirty_reg(&branch_regs[i-1],dops[i-1].rt1);
7673           }
7674           #ifdef USE_MINI_HT
7675           if(dops[i-1].rs1==31) { // JALR
7676             alloc_reg(&branch_regs[i-1],i-1,RHASH);
7677             alloc_reg(&branch_regs[i-1],i-1,RHTBL);
7678           }
7679           #endif
7680           memcpy(&branch_regs[i-1].regmap_entry,&branch_regs[i-1].regmap,sizeof(current.regmap));
7681           memcpy(constmap[i],constmap[i-1],sizeof(constmap[i]));
7682           break;
7683         case CJUMP:
7684           if((dops[i-1].opcode&0x3E)==4) // BEQ/BNE
7685           {
7686             alloc_cc(&current,i-1);
7687             dirty_reg(&current,CCREG);
7688             if((dops[i-1].rs1&&(dops[i-1].rs1==dops[i].rt1||dops[i-1].rs1==dops[i].rt2))||
7689                (dops[i-1].rs2&&(dops[i-1].rs2==dops[i].rt1||dops[i-1].rs2==dops[i].rt2))) {
7690               // The delay slot overwrote one of our conditions
7691               // Delay slot goes after the test (in order)
7692               current.u=branch_unneeded_reg[i-1]&~((1LL<<dops[i].rs1)|(1LL<<dops[i].rs2));
7693               current.u|=1;
7694               delayslot_alloc(&current,i);
7695               current.isconst=0;
7696             }
7697             else
7698             {
7699               current.u=branch_unneeded_reg[i-1]&~((1LL<<dops[i-1].rs1)|(1LL<<dops[i-1].rs2));
7700               // Alloc the branch condition registers
7701               if(dops[i-1].rs1) alloc_reg(&current,i-1,dops[i-1].rs1);
7702               if(dops[i-1].rs2) alloc_reg(&current,i-1,dops[i-1].rs2);
7703             }
7704             memcpy(&branch_regs[i-1],&current,sizeof(current));
7705             branch_regs[i-1].isconst=0;
7706             branch_regs[i-1].wasconst=0;
7707             memcpy(&branch_regs[i-1].regmap_entry,&current.regmap,sizeof(current.regmap));
7708             memcpy(constmap[i],constmap[i-1],sizeof(constmap[i]));
7709           }
7710           else
7711           if((dops[i-1].opcode&0x3E)==6) // BLEZ/BGTZ
7712           {
7713             alloc_cc(&current,i-1);
7714             dirty_reg(&current,CCREG);
7715             if(dops[i-1].rs1==dops[i].rt1||dops[i-1].rs1==dops[i].rt2) {
7716               // The delay slot overwrote the branch condition
7717               // Delay slot goes after the test (in order)
7718               current.u=branch_unneeded_reg[i-1]&~((1LL<<dops[i].rs1)|(1LL<<dops[i].rs2));
7719               current.u|=1;
7720               delayslot_alloc(&current,i);
7721               current.isconst=0;
7722             }
7723             else
7724             {
7725               current.u=branch_unneeded_reg[i-1]&~(1LL<<dops[i-1].rs1);
7726               // Alloc the branch condition register
7727               alloc_reg(&current,i-1,dops[i-1].rs1);
7728             }
7729             memcpy(&branch_regs[i-1],&current,sizeof(current));
7730             branch_regs[i-1].isconst=0;
7731             branch_regs[i-1].wasconst=0;
7732             memcpy(&branch_regs[i-1].regmap_entry,&current.regmap,sizeof(current.regmap));
7733             memcpy(constmap[i],constmap[i-1],sizeof(constmap[i]));
7734           }
7735           break;
7736         case SJUMP:
7737           {
7738             alloc_cc(&current,i-1);
7739             dirty_reg(&current,CCREG);
7740             if(dops[i-1].rs1==dops[i].rt1||dops[i-1].rs1==dops[i].rt2) {
7741               // The delay slot overwrote the branch condition
7742               // Delay slot goes after the test (in order)
7743               current.u=branch_unneeded_reg[i-1]&~((1LL<<dops[i].rs1)|(1LL<<dops[i].rs2));
7744               current.u|=1;
7745               delayslot_alloc(&current,i);
7746               current.isconst=0;
7747             }
7748             else
7749             {
7750               current.u=branch_unneeded_reg[i-1]&~(1LL<<dops[i-1].rs1);
7751               // Alloc the branch condition register
7752               alloc_reg(&current,i-1,dops[i-1].rs1);
7753             }
7754             memcpy(&branch_regs[i-1],&current,sizeof(current));
7755             branch_regs[i-1].isconst=0;
7756             branch_regs[i-1].wasconst=0;
7757             memcpy(&branch_regs[i-1].regmap_entry,&current.regmap,sizeof(current.regmap));
7758             memcpy(constmap[i],constmap[i-1],sizeof(constmap[i]));
7759           }
7760           break;
7761       }
7762
7763       if (dops[i-1].is_ujump)
7764       {
7765         if(dops[i-1].rt1==31) // JAL/JALR
7766         {
7767           // Subroutine call will return here, don't alloc any registers
7768           current.dirty=0;
7769           clear_all_regs(current.regmap);
7770           alloc_reg(&current,i,CCREG);
7771           dirty_reg(&current,CCREG);
7772         }
7773         else if(i+1<slen)
7774         {
7775           // Internal branch will jump here, match registers to caller
7776           current.dirty=0;
7777           clear_all_regs(current.regmap);
7778           alloc_reg(&current,i,CCREG);
7779           dirty_reg(&current,CCREG);
7780           for(j=i-1;j>=0;j--)
7781           {
7782             if(cinfo[j].ba==start+i*4+4) {
7783               memcpy(current.regmap,branch_regs[j].regmap,sizeof(current.regmap));
7784               current.dirty=branch_regs[j].dirty;
7785               break;
7786             }
7787           }
7788           while(j>=0) {
7789             if(cinfo[j].ba==start+i*4+4) {
7790               for(hr=0;hr<HOST_REGS;hr++) {
7791                 if(current.regmap[hr]!=branch_regs[j].regmap[hr]) {
7792                   current.regmap[hr]=-1;
7793                 }
7794                 current.dirty&=branch_regs[j].dirty;
7795               }
7796             }
7797             j--;
7798           }
7799         }
7800       }
7801     }
7802
7803     // Count cycles in between branches
7804     cinfo[i].ccadj = CLOCK_ADJUST(cc);
7805     if (i > 0 && (dops[i-1].is_jump || dops[i].is_exception))
7806     {
7807       cc=0;
7808     }
7809 #if !defined(DRC_DBG)
7810     else if(dops[i].itype==C2OP&&gte_cycletab[source[i]&0x3f]>2)
7811     {
7812       // this should really be removed since the real stalls have been implemented,
7813       // but doing so causes sizeable perf regression against the older version
7814       u_int gtec = gte_cycletab[source[i] & 0x3f];
7815       cc += HACK_ENABLED(NDHACK_NO_STALLS) ? gtec/2 : 2;
7816     }
7817     else if(i>1&&dops[i].itype==STORE&&dops[i-1].itype==STORE&&dops[i-2].itype==STORE&&!dops[i].bt)
7818     {
7819       cc+=4;
7820     }
7821     else if(dops[i].itype==C2LS)
7822     {
7823       // same as with C2OP
7824       cc += HACK_ENABLED(NDHACK_NO_STALLS) ? 4 : 2;
7825     }
7826 #endif
7827     else
7828     {
7829       cc++;
7830     }
7831
7832     if(!dops[i].is_ds) {
7833       regs[i].dirty=current.dirty;
7834       regs[i].isconst=current.isconst;
7835       memcpy(constmap[i],current_constmap,sizeof(constmap[i]));
7836     }
7837     for(hr=0;hr<HOST_REGS;hr++) {
7838       if(hr!=EXCLUDE_REG&&regs[i].regmap[hr]>=0) {
7839         if(regmap_pre[i][hr]!=regs[i].regmap[hr]) {
7840           regs[i].wasconst&=~(1<<hr);
7841         }
7842       }
7843     }
7844     //regs[i].waswritten=current.waswritten;
7845   }
7846 }
7847
7848 static noinline void pass4_cull_unused_regs(void)
7849 {
7850   u_int last_needed_regs[4] = {0,0,0,0};
7851   u_int nr=0;
7852   int i;
7853
7854   for (i=slen-1;i>=0;i--)
7855   {
7856     int hr;
7857     __builtin_prefetch(regs[i-2].regmap);
7858     if(dops[i].is_jump)
7859     {
7860       if(cinfo[i].ba<start || cinfo[i].ba>=(start+slen*4))
7861       {
7862         // Branch out of this block, don't need anything
7863         nr=0;
7864       }
7865       else
7866       {
7867         // Internal branch
7868         // Need whatever matches the target
7869         nr=0;
7870         int t=(cinfo[i].ba-start)>>2;
7871         for(hr=0;hr<HOST_REGS;hr++)
7872         {
7873           if(regs[i].regmap_entry[hr]>=0) {
7874             if(regs[i].regmap_entry[hr]==regs[t].regmap_entry[hr]) nr|=1<<hr;
7875           }
7876         }
7877       }
7878       // Conditional branch may need registers for following instructions
7879       if (!dops[i].is_ujump)
7880       {
7881         if(i<slen-2) {
7882           nr |= last_needed_regs[(i+2) & 3];
7883           for(hr=0;hr<HOST_REGS;hr++)
7884           {
7885             if(regmap_pre[i+2][hr]>=0&&get_reg(regs[i+2].regmap_entry,regmap_pre[i+2][hr])<0) nr&=~(1<<hr);
7886             //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]);
7887           }
7888         }
7889       }
7890       // Don't need stuff which is overwritten
7891       //if(regs[i].regmap[hr]!=regmap_pre[i][hr]) nr&=~(1<<hr);
7892       //if(regs[i].regmap[hr]<0) nr&=~(1<<hr);
7893       // Merge in delay slot
7894       if (dops[i+1].rt1) nr &= ~get_regm(regs[i].regmap, dops[i+1].rt1);
7895       if (dops[i+1].rt2) nr &= ~get_regm(regs[i].regmap, dops[i+1].rt2);
7896       nr |= get_regm(regmap_pre[i], dops[i+1].rs1);
7897       nr |= get_regm(regmap_pre[i], dops[i+1].rs2);
7898       nr |= get_regm(regs[i].regmap_entry, dops[i+1].rs1);
7899       nr |= get_regm(regs[i].regmap_entry, dops[i+1].rs2);
7900       if (ram_offset && (dops[i+1].is_load || dops[i+1].is_store)) {
7901         nr |= get_regm(regmap_pre[i], ROREG);
7902         nr |= get_regm(regs[i].regmap_entry, ROREG);
7903       }
7904       if (dops[i+1].is_store) {
7905         nr |= get_regm(regmap_pre[i], INVCP);
7906         nr |= get_regm(regs[i].regmap_entry, INVCP);
7907       }
7908     }
7909     else if (dops[i].is_exception)
7910     {
7911       // SYSCALL instruction, etc
7912       nr=0;
7913     }
7914     else // Non-branch
7915     {
7916       if(i<slen-1) {
7917         for(hr=0;hr<HOST_REGS;hr++) {
7918           if(regmap_pre[i+1][hr]>=0&&get_reg(regs[i+1].regmap_entry,regmap_pre[i+1][hr])<0) nr&=~(1<<hr);
7919           if(regs[i].regmap[hr]!=regmap_pre[i+1][hr]) nr&=~(1<<hr);
7920           if(regs[i].regmap[hr]!=regmap_pre[i][hr]) nr&=~(1<<hr);
7921           if(regs[i].regmap[hr]<0) nr&=~(1<<hr);
7922         }
7923       }
7924     }
7925     // Overwritten registers are not needed
7926     if (dops[i].rt1) nr &= ~get_regm(regs[i].regmap, dops[i].rt1);
7927     if (dops[i].rt2) nr &= ~get_regm(regs[i].regmap, dops[i].rt2);
7928     nr &= ~get_regm(regs[i].regmap, FTEMP);
7929     // Source registers are needed
7930     nr |= get_regm(regmap_pre[i], dops[i].rs1);
7931     nr |= get_regm(regmap_pre[i], dops[i].rs2);
7932     nr |= get_regm(regs[i].regmap_entry, dops[i].rs1);
7933     nr |= get_regm(regs[i].regmap_entry, dops[i].rs2);
7934     if (ram_offset && (dops[i].is_load || dops[i].is_store)) {
7935       nr |= get_regm(regmap_pre[i], ROREG);
7936       nr |= get_regm(regs[i].regmap_entry, ROREG);
7937     }
7938     if (dops[i].is_store) {
7939       nr |= get_regm(regmap_pre[i], INVCP);
7940       nr |= get_regm(regs[i].regmap_entry, INVCP);
7941     }
7942
7943     if (i > 0 && !dops[i].bt && regs[i].wasdirty)
7944     for(hr=0;hr<HOST_REGS;hr++)
7945     {
7946       // Don't store a register immediately after writing it,
7947       // may prevent dual-issue.
7948       // But do so if this is a branch target, otherwise we
7949       // might have to load the register before the branch.
7950       if((regs[i].wasdirty>>hr)&1) {
7951         if((regmap_pre[i][hr]>0&&!((unneeded_reg[i]>>regmap_pre[i][hr])&1))) {
7952           if(dops[i-1].rt1==regmap_pre[i][hr]) nr|=1<<hr;
7953           if(dops[i-1].rt2==regmap_pre[i][hr]) nr|=1<<hr;
7954         }
7955         if((regs[i].regmap_entry[hr]>0&&!((unneeded_reg[i]>>regs[i].regmap_entry[hr])&1))) {
7956           if(dops[i-1].rt1==regs[i].regmap_entry[hr]) nr|=1<<hr;
7957           if(dops[i-1].rt2==regs[i].regmap_entry[hr]) nr|=1<<hr;
7958         }
7959       }
7960     }
7961     // Cycle count is needed at branches.  Assume it is needed at the target too.
7962     if (i == 0 || dops[i].bt || dops[i].may_except || dops[i].itype == CJUMP) {
7963       if(regmap_pre[i][HOST_CCREG]==CCREG) nr|=1<<HOST_CCREG;
7964       if(regs[i].regmap_entry[HOST_CCREG]==CCREG) nr|=1<<HOST_CCREG;
7965     }
7966     // Save it
7967     last_needed_regs[i & 3] = nr;
7968
7969     // Deallocate unneeded registers
7970     for(hr=0;hr<HOST_REGS;hr++)
7971     {
7972       if(!((nr>>hr)&1)) {
7973         if(regs[i].regmap_entry[hr]!=CCREG) regs[i].regmap_entry[hr]=-1;
7974         if(dops[i].is_jump)
7975         {
7976           int map1 = 0, map2 = 0, temp = 0; // or -1 ??
7977           if (dops[i+1].is_load || dops[i+1].is_store)
7978             map1 = ROREG;
7979           if (dops[i+1].is_store)
7980             map2 = INVCP;
7981           if(dops[i+1].itype==LOADLR || dops[i+1].itype==STORELR || dops[i+1].itype==C2LS)
7982             temp = FTEMP;
7983           if(regs[i].regmap[hr]!=dops[i].rs1 && regs[i].regmap[hr]!=dops[i].rs2 &&
7984              regs[i].regmap[hr]!=dops[i].rt1 && regs[i].regmap[hr]!=dops[i].rt2 &&
7985              regs[i].regmap[hr]!=dops[i+1].rt1 && regs[i].regmap[hr]!=dops[i+1].rt2 &&
7986              regs[i].regmap[hr]!=dops[i+1].rs1 && regs[i].regmap[hr]!=dops[i+1].rs2 &&
7987              regs[i].regmap[hr]!=temp && regs[i].regmap[hr]!=PTEMP &&
7988              regs[i].regmap[hr]!=RHASH && regs[i].regmap[hr]!=RHTBL &&
7989              regs[i].regmap[hr]!=RTEMP && regs[i].regmap[hr]!=CCREG &&
7990              regs[i].regmap[hr]!=map1 && regs[i].regmap[hr]!=map2)
7991           {
7992             regs[i].regmap[hr]=-1;
7993             regs[i].isconst&=~(1<<hr);
7994             regs[i].dirty&=~(1<<hr);
7995             regs[i+1].wasdirty&=~(1<<hr);
7996             if(branch_regs[i].regmap[hr]!=dops[i].rs1 && branch_regs[i].regmap[hr]!=dops[i].rs2 &&
7997                branch_regs[i].regmap[hr]!=dops[i].rt1 && branch_regs[i].regmap[hr]!=dops[i].rt2 &&
7998                branch_regs[i].regmap[hr]!=dops[i+1].rt1 && branch_regs[i].regmap[hr]!=dops[i+1].rt2 &&
7999                branch_regs[i].regmap[hr]!=dops[i+1].rs1 && branch_regs[i].regmap[hr]!=dops[i+1].rs2 &&
8000                branch_regs[i].regmap[hr]!=temp && branch_regs[i].regmap[hr]!=PTEMP &&
8001                branch_regs[i].regmap[hr]!=RHASH && branch_regs[i].regmap[hr]!=RHTBL &&
8002                branch_regs[i].regmap[hr]!=RTEMP && branch_regs[i].regmap[hr]!=CCREG &&
8003                branch_regs[i].regmap[hr]!=map1 && branch_regs[i].regmap[hr]!=map2)
8004             {
8005               branch_regs[i].regmap[hr]=-1;
8006               branch_regs[i].regmap_entry[hr]=-1;
8007               if (!dops[i].is_ujump)
8008               {
8009                 if (i < slen-2) {
8010                   regmap_pre[i+2][hr]=-1;
8011                   regs[i+2].wasconst&=~(1<<hr);
8012                 }
8013               }
8014             }
8015           }
8016         }
8017         else
8018         {
8019           // Non-branch
8020           if(i>0)
8021           {
8022             int map1 = -1, map2 = -1, temp=-1;
8023             if (dops[i].is_load || dops[i].is_store)
8024               map1 = ROREG;
8025             if (dops[i].is_store)
8026               map2 = INVCP;
8027             if (dops[i].itype==LOADLR || dops[i].itype==STORELR || dops[i].itype==C2LS)
8028               temp = FTEMP;
8029             if(regs[i].regmap[hr]!=dops[i].rt1 && regs[i].regmap[hr]!=dops[i].rt2 &&
8030                regs[i].regmap[hr]!=dops[i].rs1 && regs[i].regmap[hr]!=dops[i].rs2 &&
8031                regs[i].regmap[hr]!=temp && regs[i].regmap[hr]!=map1 && regs[i].regmap[hr]!=map2 &&
8032                //(dops[i].itype!=SPAN||regs[i].regmap[hr]!=CCREG)
8033                regs[i].regmap[hr] != CCREG)
8034             {
8035               if(i<slen-1&&!dops[i].is_ds) {
8036                 assert(regs[i].regmap[hr]<64);
8037                 if(regmap_pre[i+1][hr]!=-1 || regs[i].regmap[hr]>0)
8038                 if(regmap_pre[i+1][hr]!=regs[i].regmap[hr])
8039                 {
8040                   SysPrintf("fail: %x (%d %d!=%d)\n",start+i*4,hr,regmap_pre[i+1][hr],regs[i].regmap[hr]);
8041                   assert(regmap_pre[i+1][hr]==regs[i].regmap[hr]);
8042                 }
8043                 regmap_pre[i+1][hr]=-1;
8044                 if(regs[i+1].regmap_entry[hr]==CCREG) regs[i+1].regmap_entry[hr]=-1;
8045                 regs[i+1].wasconst&=~(1<<hr);
8046               }
8047               regs[i].regmap[hr]=-1;
8048               regs[i].isconst&=~(1<<hr);
8049               regs[i].dirty&=~(1<<hr);
8050               regs[i+1].wasdirty&=~(1<<hr);
8051             }
8052           }
8053         }
8054       } // if needed
8055     } // for hr
8056   }
8057 }
8058
8059 // If a register is allocated during a loop, try to allocate it for the
8060 // entire loop, if possible.  This avoids loading/storing registers
8061 // inside of the loop.
8062 static noinline void pass5a_preallocate1(void)
8063 {
8064   int i, j, hr;
8065   signed char f_regmap[HOST_REGS];
8066   clear_all_regs(f_regmap);
8067   for(i=0;i<slen-1;i++)
8068   {
8069     if(dops[i].itype==UJUMP||dops[i].itype==CJUMP||dops[i].itype==SJUMP)
8070     {
8071       if(cinfo[i].ba>=start && cinfo[i].ba<(start+i*4))
8072       if(dops[i+1].itype==NOP||dops[i+1].itype==MOV||dops[i+1].itype==ALU
8073       ||dops[i+1].itype==SHIFTIMM||dops[i+1].itype==IMM16||dops[i+1].itype==LOAD
8074       ||dops[i+1].itype==STORE||dops[i+1].itype==STORELR
8075       ||dops[i+1].itype==SHIFT
8076       ||dops[i+1].itype==COP2||dops[i+1].itype==C2LS||dops[i+1].itype==C2OP)
8077       {
8078         int t=(cinfo[i].ba-start)>>2;
8079         if(t > 0 && !dops[t-1].is_jump) // loop_preload can't handle jumps into delay slots
8080         if(t<2||(dops[t-2].itype!=UJUMP&&dops[t-2].itype!=RJUMP)||dops[t-2].rt1!=31) // call/ret assumes no registers allocated
8081         for(hr=0;hr<HOST_REGS;hr++)
8082         {
8083           if(regs[i].regmap[hr]>=0) {
8084             if(f_regmap[hr]!=regs[i].regmap[hr]) {
8085               // dealloc old register
8086               int n;
8087               for(n=0;n<HOST_REGS;n++)
8088               {
8089                 if(f_regmap[n]==regs[i].regmap[hr]) {f_regmap[n]=-1;}
8090               }
8091               // and alloc new one
8092               f_regmap[hr]=regs[i].regmap[hr];
8093             }
8094           }
8095           if(branch_regs[i].regmap[hr]>=0) {
8096             if(f_regmap[hr]!=branch_regs[i].regmap[hr]) {
8097               // dealloc old register
8098               int n;
8099               for(n=0;n<HOST_REGS;n++)
8100               {
8101                 if(f_regmap[n]==branch_regs[i].regmap[hr]) {f_regmap[n]=-1;}
8102               }
8103               // and alloc new one
8104               f_regmap[hr]=branch_regs[i].regmap[hr];
8105             }
8106           }
8107           if(dops[i].ooo) {
8108             if(count_free_regs(regs[i].regmap)<=cinfo[i+1].min_free_regs)
8109               f_regmap[hr]=branch_regs[i].regmap[hr];
8110           }else{
8111             if(count_free_regs(branch_regs[i].regmap)<=cinfo[i+1].min_free_regs)
8112               f_regmap[hr]=branch_regs[i].regmap[hr];
8113           }
8114           // Avoid dirty->clean transition
8115           #ifdef DESTRUCTIVE_WRITEBACK
8116           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;
8117           #endif
8118           // This check is only strictly required in the DESTRUCTIVE_WRITEBACK
8119           // case above, however it's always a good idea.  We can't hoist the
8120           // load if the register was already allocated, so there's no point
8121           // wasting time analyzing most of these cases.  It only "succeeds"
8122           // when the mapping was different and the load can be replaced with
8123           // a mov, which is of negligible benefit.  So such cases are
8124           // skipped below.
8125           if(f_regmap[hr]>0) {
8126             if(regs[t].regmap[hr]==f_regmap[hr]||(regs[t].regmap_entry[hr]<0&&get_reg(regmap_pre[t],f_regmap[hr])<0)) {
8127               int r=f_regmap[hr];
8128               for(j=t;j<=i;j++)
8129               {
8130                 //printf("Test %x -> %x, %x %d/%d\n",start+i*4,cinfo[i].ba,start+j*4,hr,r);
8131                 if(r<34&&((unneeded_reg[j]>>r)&1)) break;
8132                 assert(r < 64);
8133                 if(regs[j].regmap[hr]==f_regmap[hr]&&f_regmap[hr]<TEMPREG) {
8134                   //printf("Hit %x -> %x, %x %d/%d\n",start+i*4,cinfo[i].ba,start+j*4,hr,r);
8135                   int k;
8136                   if(regs[i].regmap[hr]==-1&&branch_regs[i].regmap[hr]==-1) {
8137                     if(get_reg(regs[i].regmap,f_regmap[hr])>=0) break;
8138                     if(get_reg(regs[i+2].regmap,f_regmap[hr])>=0) break;
8139                     k=i;
8140                     while(k>1&&regs[k-1].regmap[hr]==-1) {
8141                       if(count_free_regs(regs[k-1].regmap)<=cinfo[k-1].min_free_regs) {
8142                         //printf("no free regs for store %x\n",start+(k-1)*4);
8143                         break;
8144                       }
8145                       if(get_reg(regs[k-1].regmap,f_regmap[hr])>=0) {
8146                         //printf("no-match due to different register\n");
8147                         break;
8148                       }
8149                       if (dops[k-2].is_jump) {
8150                         //printf("no-match due to branch\n");
8151                         break;
8152                       }
8153                       // call/ret fast path assumes no registers allocated
8154                       if(k>2&&(dops[k-3].itype==UJUMP||dops[k-3].itype==RJUMP)&&dops[k-3].rt1==31) {
8155                         break;
8156                       }
8157                       k--;
8158                     }
8159                     if(regs[k-1].regmap[hr]==f_regmap[hr]&&regmap_pre[k][hr]==f_regmap[hr]) {
8160                       //printf("Extend r%d, %x ->\n",hr,start+k*4);
8161                       while(k<i) {
8162                         regs[k].regmap_entry[hr]=f_regmap[hr];
8163                         regs[k].regmap[hr]=f_regmap[hr];
8164                         regmap_pre[k+1][hr]=f_regmap[hr];
8165                         regs[k].wasdirty&=~(1<<hr);
8166                         regs[k].dirty&=~(1<<hr);
8167                         regs[k].wasdirty|=(1<<hr)&regs[k-1].dirty;
8168                         regs[k].dirty|=(1<<hr)&regs[k].wasdirty;
8169                         regs[k].wasconst&=~(1<<hr);
8170                         regs[k].isconst&=~(1<<hr);
8171                         k++;
8172                       }
8173                     }
8174                     else {
8175                       //printf("Fail Extend r%d, %x ->\n",hr,start+k*4);
8176                       break;
8177                     }
8178                     assert(regs[i-1].regmap[hr]==f_regmap[hr]);
8179                     if(regs[i-1].regmap[hr]==f_regmap[hr]&&regmap_pre[i][hr]==f_regmap[hr]) {
8180                       //printf("OK fill %x (r%d)\n",start+i*4,hr);
8181                       regs[i].regmap_entry[hr]=f_regmap[hr];
8182                       regs[i].regmap[hr]=f_regmap[hr];
8183                       regs[i].wasdirty&=~(1<<hr);
8184                       regs[i].dirty&=~(1<<hr);
8185                       regs[i].wasdirty|=(1<<hr)&regs[i-1].dirty;
8186                       regs[i].dirty|=(1<<hr)&regs[i-1].dirty;
8187                       regs[i].wasconst&=~(1<<hr);
8188                       regs[i].isconst&=~(1<<hr);
8189                       branch_regs[i].regmap_entry[hr]=f_regmap[hr];
8190                       branch_regs[i].wasdirty&=~(1<<hr);
8191                       branch_regs[i].wasdirty|=(1<<hr)&regs[i].dirty;
8192                       branch_regs[i].regmap[hr]=f_regmap[hr];
8193                       branch_regs[i].dirty&=~(1<<hr);
8194                       branch_regs[i].dirty|=(1<<hr)&regs[i].dirty;
8195                       branch_regs[i].wasconst&=~(1<<hr);
8196                       branch_regs[i].isconst&=~(1<<hr);
8197                       if (!dops[i].is_ujump) {
8198                         regmap_pre[i+2][hr]=f_regmap[hr];
8199                         regs[i+2].wasdirty&=~(1<<hr);
8200                         regs[i+2].wasdirty|=(1<<hr)&regs[i].dirty;
8201                       }
8202                     }
8203                   }
8204                   for(k=t;k<j;k++) {
8205                     // Alloc register clean at beginning of loop,
8206                     // but may dirty it in pass 6
8207                     regs[k].regmap_entry[hr]=f_regmap[hr];
8208                     regs[k].regmap[hr]=f_regmap[hr];
8209                     regs[k].dirty&=~(1<<hr);
8210                     regs[k].wasconst&=~(1<<hr);
8211                     regs[k].isconst&=~(1<<hr);
8212                     if (dops[k].is_jump) {
8213                       branch_regs[k].regmap_entry[hr]=f_regmap[hr];
8214                       branch_regs[k].regmap[hr]=f_regmap[hr];
8215                       branch_regs[k].dirty&=~(1<<hr);
8216                       branch_regs[k].wasconst&=~(1<<hr);
8217                       branch_regs[k].isconst&=~(1<<hr);
8218                       if (!dops[k].is_ujump) {
8219                         regmap_pre[k+2][hr]=f_regmap[hr];
8220                         regs[k+2].wasdirty&=~(1<<hr);
8221                       }
8222                     }
8223                     else
8224                     {
8225                       regmap_pre[k+1][hr]=f_regmap[hr];
8226                       regs[k+1].wasdirty&=~(1<<hr);
8227                     }
8228                   }
8229                   if(regs[j].regmap[hr]==f_regmap[hr])
8230                     regs[j].regmap_entry[hr]=f_regmap[hr];
8231                   break;
8232                 }
8233                 if(j==i) break;
8234                 if(regs[j].regmap[hr]>=0)
8235                   break;
8236                 if(get_reg(regs[j].regmap,f_regmap[hr])>=0) {
8237                   //printf("no-match due to different register\n");
8238                   break;
8239                 }
8240                 if (dops[j].is_ujump)
8241                 {
8242                   // Stop on unconditional branch
8243                   break;
8244                 }
8245                 if(dops[j].itype==CJUMP||dops[j].itype==SJUMP)
8246                 {
8247                   if(dops[j].ooo) {
8248                     if(count_free_regs(regs[j].regmap)<=cinfo[j+1].min_free_regs)
8249                       break;
8250                   }else{
8251                     if(count_free_regs(branch_regs[j].regmap)<=cinfo[j+1].min_free_regs)
8252                       break;
8253                   }
8254                   if(get_reg(branch_regs[j].regmap,f_regmap[hr])>=0) {
8255                     //printf("no-match due to different register (branch)\n");
8256                     break;
8257                   }
8258                 }
8259                 if(count_free_regs(regs[j].regmap)<=cinfo[j].min_free_regs) {
8260                   //printf("No free regs for store %x\n",start+j*4);
8261                   break;
8262                 }
8263                 assert(f_regmap[hr]<64);
8264               }
8265             }
8266           }
8267         }
8268       }
8269     }else{
8270       // Non branch or undetermined branch target
8271       for(hr=0;hr<HOST_REGS;hr++)
8272       {
8273         if(hr!=EXCLUDE_REG) {
8274           if(regs[i].regmap[hr]>=0) {
8275             if(f_regmap[hr]!=regs[i].regmap[hr]) {
8276               // dealloc old register
8277               int n;
8278               for(n=0;n<HOST_REGS;n++)
8279               {
8280                 if(f_regmap[n]==regs[i].regmap[hr]) {f_regmap[n]=-1;}
8281               }
8282               // and alloc new one
8283               f_regmap[hr]=regs[i].regmap[hr];
8284             }
8285           }
8286         }
8287       }
8288       // Try to restore cycle count at branch targets
8289       if(dops[i].bt) {
8290         for(j=i;j<slen-1;j++) {
8291           if(regs[j].regmap[HOST_CCREG]!=-1) break;
8292           if(count_free_regs(regs[j].regmap)<=cinfo[j].min_free_regs) {
8293             //printf("no free regs for store %x\n",start+j*4);
8294             break;
8295           }
8296         }
8297         if(regs[j].regmap[HOST_CCREG]==CCREG) {
8298           int k=i;
8299           //printf("Extend CC, %x -> %x\n",start+k*4,start+j*4);
8300           while(k<j) {
8301             regs[k].regmap_entry[HOST_CCREG]=CCREG;
8302             regs[k].regmap[HOST_CCREG]=CCREG;
8303             regmap_pre[k+1][HOST_CCREG]=CCREG;
8304             regs[k+1].wasdirty|=1<<HOST_CCREG;
8305             regs[k].dirty|=1<<HOST_CCREG;
8306             regs[k].wasconst&=~(1<<HOST_CCREG);
8307             regs[k].isconst&=~(1<<HOST_CCREG);
8308             k++;
8309           }
8310           regs[j].regmap_entry[HOST_CCREG]=CCREG;
8311         }
8312         // Work backwards from the branch target
8313         if(j>i&&f_regmap[HOST_CCREG]==CCREG)
8314         {
8315           //printf("Extend backwards\n");
8316           int k;
8317           k=i;
8318           while(regs[k-1].regmap[HOST_CCREG]==-1) {
8319             if(count_free_regs(regs[k-1].regmap)<=cinfo[k-1].min_free_regs) {
8320               //printf("no free regs for store %x\n",start+(k-1)*4);
8321               break;
8322             }
8323             k--;
8324           }
8325           if(regs[k-1].regmap[HOST_CCREG]==CCREG) {
8326             //printf("Extend CC, %x ->\n",start+k*4);
8327             while(k<=i) {
8328               regs[k].regmap_entry[HOST_CCREG]=CCREG;
8329               regs[k].regmap[HOST_CCREG]=CCREG;
8330               regmap_pre[k+1][HOST_CCREG]=CCREG;
8331               regs[k+1].wasdirty|=1<<HOST_CCREG;
8332               regs[k].dirty|=1<<HOST_CCREG;
8333               regs[k].wasconst&=~(1<<HOST_CCREG);
8334               regs[k].isconst&=~(1<<HOST_CCREG);
8335               k++;
8336             }
8337           }
8338           else {
8339             //printf("Fail Extend CC, %x ->\n",start+k*4);
8340           }
8341         }
8342       }
8343       if(dops[i].itype!=STORE&&dops[i].itype!=STORELR&&dops[i].itype!=SHIFT&&
8344          dops[i].itype!=NOP&&dops[i].itype!=MOV&&dops[i].itype!=ALU&&dops[i].itype!=SHIFTIMM&&
8345          dops[i].itype!=IMM16&&dops[i].itype!=LOAD)
8346       {
8347         memcpy(f_regmap,regs[i].regmap,sizeof(f_regmap));
8348       }
8349     }
8350   }
8351 }
8352
8353 // This allocates registers (if possible) one instruction prior
8354 // to use, which can avoid a load-use penalty on certain CPUs.
8355 static noinline void pass5b_preallocate2(void)
8356 {
8357   int i, hr;
8358   for(i=0;i<slen-1;i++)
8359   {
8360     if (!i || !dops[i-1].is_jump)
8361     {
8362       if(!dops[i+1].bt)
8363       {
8364         int j, can_steal = 1;
8365         for (j = i; j < i + 2; j++) {
8366           int free_regs = 0;
8367           if (cinfo[j].min_free_regs == 0)
8368             continue;
8369           for (hr = 0; hr < HOST_REGS; hr++)
8370             if (hr != EXCLUDE_REG && regs[j].regmap[hr] < 0)
8371               free_regs++;
8372           if (free_regs <= cinfo[j].min_free_regs) {
8373             can_steal = 0;
8374             break;
8375           }
8376         }
8377         if (!can_steal)
8378           continue;
8379         if(dops[i].itype==ALU||dops[i].itype==MOV||dops[i].itype==LOAD||dops[i].itype==SHIFTIMM||dops[i].itype==IMM16
8380            ||(dops[i].itype==COP2&&dops[i].opcode2<3))
8381         {
8382           if(dops[i+1].rs1) {
8383             if((hr=get_reg(regs[i+1].regmap,dops[i+1].rs1))>=0)
8384             {
8385               if(regs[i].regmap[hr]<0&&regs[i+1].regmap_entry[hr]<0)
8386               {
8387                 regs[i].regmap[hr]=regs[i+1].regmap[hr];
8388                 regmap_pre[i+1][hr]=regs[i+1].regmap[hr];
8389                 regs[i+1].regmap_entry[hr]=regs[i+1].regmap[hr];
8390                 regs[i].isconst&=~(1<<hr);
8391                 regs[i].isconst|=regs[i+1].isconst&(1<<hr);
8392                 constmap[i][hr]=constmap[i+1][hr];
8393                 regs[i+1].wasdirty&=~(1<<hr);
8394                 regs[i].dirty&=~(1<<hr);
8395               }
8396             }
8397           }
8398           if(dops[i+1].rs2) {
8399             if((hr=get_reg(regs[i+1].regmap,dops[i+1].rs2))>=0)
8400             {
8401               if(regs[i].regmap[hr]<0&&regs[i+1].regmap_entry[hr]<0)
8402               {
8403                 regs[i].regmap[hr]=regs[i+1].regmap[hr];
8404                 regmap_pre[i+1][hr]=regs[i+1].regmap[hr];
8405                 regs[i+1].regmap_entry[hr]=regs[i+1].regmap[hr];
8406                 regs[i].isconst&=~(1<<hr);
8407                 regs[i].isconst|=regs[i+1].isconst&(1<<hr);
8408                 constmap[i][hr]=constmap[i+1][hr];
8409                 regs[i+1].wasdirty&=~(1<<hr);
8410                 regs[i].dirty&=~(1<<hr);
8411               }
8412             }
8413           }
8414           // Preload target address for load instruction (non-constant)
8415           if(dops[i+1].itype==LOAD&&dops[i+1].rs1&&get_reg(regs[i+1].regmap,dops[i+1].rs1)<0) {
8416             if((hr=get_reg_w(regs[i+1].regmap, dops[i+1].rt1))>=0)
8417             {
8418               if(regs[i].regmap[hr]<0&&regs[i+1].regmap_entry[hr]<0)
8419               {
8420                 regs[i].regmap[hr]=dops[i+1].rs1;
8421                 regmap_pre[i+1][hr]=dops[i+1].rs1;
8422                 regs[i+1].regmap_entry[hr]=dops[i+1].rs1;
8423                 regs[i].isconst&=~(1<<hr);
8424                 regs[i].isconst|=regs[i+1].isconst&(1<<hr);
8425                 constmap[i][hr]=constmap[i+1][hr];
8426                 regs[i+1].wasdirty&=~(1<<hr);
8427                 regs[i].dirty&=~(1<<hr);
8428               }
8429             }
8430           }
8431           // Load source into target register
8432           if(dops[i+1].use_lt1&&get_reg(regs[i+1].regmap,dops[i+1].rs1)<0) {
8433             if((hr=get_reg_w(regs[i+1].regmap, dops[i+1].rt1))>=0)
8434             {
8435               if(regs[i].regmap[hr]<0&&regs[i+1].regmap_entry[hr]<0)
8436               {
8437                 regs[i].regmap[hr]=dops[i+1].rs1;
8438                 regmap_pre[i+1][hr]=dops[i+1].rs1;
8439                 regs[i+1].regmap_entry[hr]=dops[i+1].rs1;
8440                 regs[i].isconst&=~(1<<hr);
8441                 regs[i].isconst|=regs[i+1].isconst&(1<<hr);
8442                 constmap[i][hr]=constmap[i+1][hr];
8443                 regs[i+1].wasdirty&=~(1<<hr);
8444                 regs[i].dirty&=~(1<<hr);
8445               }
8446             }
8447           }
8448           // Address for store instruction (non-constant)
8449           if (dops[i+1].is_store) { // SB/SH/SW/SWC2
8450             if(get_reg(regs[i+1].regmap,dops[i+1].rs1)<0) {
8451               hr=get_reg2(regs[i].regmap,regs[i+1].regmap,-1);
8452               if(hr<0) hr=get_reg_temp(regs[i+1].regmap);
8453               else {
8454                 regs[i+1].regmap[hr]=AGEN1+((i+1)&1);
8455                 regs[i+1].isconst&=~(1<<hr);
8456                 regs[i+1].dirty&=~(1<<hr);
8457                 regs[i+2].wasdirty&=~(1<<hr);
8458               }
8459               assert(hr>=0);
8460               #if 0 // what is this for? double allocs $0 in ps1_rom.bin
8461               if(regs[i].regmap[hr]<0&&regs[i+1].regmap_entry[hr]<0)
8462               {
8463                 regs[i].regmap[hr]=dops[i+1].rs1;
8464                 regmap_pre[i+1][hr]=dops[i+1].rs1;
8465                 regs[i+1].regmap_entry[hr]=dops[i+1].rs1;
8466                 regs[i].isconst&=~(1<<hr);
8467                 regs[i].isconst|=regs[i+1].isconst&(1<<hr);
8468                 constmap[i][hr]=constmap[i+1][hr];
8469                 regs[i+1].wasdirty&=~(1<<hr);
8470                 regs[i].dirty&=~(1<<hr);
8471               }
8472               #endif
8473             }
8474           }
8475           if (dops[i+1].itype == LOADLR || dops[i+1].opcode == 0x32) { // LWC2
8476             if(get_reg(regs[i+1].regmap,dops[i+1].rs1)<0) {
8477               int nr;
8478               hr=get_reg(regs[i+1].regmap,FTEMP);
8479               assert(hr>=0);
8480               if(regs[i].regmap[hr]<0&&regs[i+1].regmap_entry[hr]<0)
8481               {
8482                 regs[i].regmap[hr]=dops[i+1].rs1;
8483                 regmap_pre[i+1][hr]=dops[i+1].rs1;
8484                 regs[i+1].regmap_entry[hr]=dops[i+1].rs1;
8485                 regs[i].isconst&=~(1<<hr);
8486                 regs[i].isconst|=regs[i+1].isconst&(1<<hr);
8487                 constmap[i][hr]=constmap[i+1][hr];
8488                 regs[i+1].wasdirty&=~(1<<hr);
8489                 regs[i].dirty&=~(1<<hr);
8490               }
8491               else if((nr=get_reg2(regs[i].regmap,regs[i+1].regmap,-1))>=0)
8492               {
8493                 // move it to another register
8494                 regs[i+1].regmap[hr]=-1;
8495                 regmap_pre[i+2][hr]=-1;
8496                 regs[i+1].regmap[nr]=FTEMP;
8497                 regmap_pre[i+2][nr]=FTEMP;
8498                 regs[i].regmap[nr]=dops[i+1].rs1;
8499                 regmap_pre[i+1][nr]=dops[i+1].rs1;
8500                 regs[i+1].regmap_entry[nr]=dops[i+1].rs1;
8501                 regs[i].isconst&=~(1<<nr);
8502                 regs[i+1].isconst&=~(1<<nr);
8503                 regs[i].dirty&=~(1<<nr);
8504                 regs[i+1].wasdirty&=~(1<<nr);
8505                 regs[i+1].dirty&=~(1<<nr);
8506                 regs[i+2].wasdirty&=~(1<<nr);
8507               }
8508             }
8509           }
8510           if(dops[i+1].itype==LOAD||dops[i+1].itype==LOADLR||dops[i+1].itype==STORE||dops[i+1].itype==STORELR/*||dops[i+1].itype==C2LS*/) {
8511             hr = -1;
8512             if(dops[i+1].itype==LOAD)
8513               hr=get_reg_w(regs[i+1].regmap, dops[i+1].rt1);
8514             if (dops[i+1].itype == LOADLR || dops[i+1].opcode == 0x32) // LWC2
8515               hr=get_reg(regs[i+1].regmap,FTEMP);
8516             if (dops[i+1].is_store) {
8517               hr=get_reg(regs[i+1].regmap,AGEN1+((i+1)&1));
8518               if(hr<0) hr=get_reg_temp(regs[i+1].regmap);
8519             }
8520             if(hr>=0&&regs[i].regmap[hr]<0) {
8521               int rs=get_reg(regs[i+1].regmap,dops[i+1].rs1);
8522               if(rs>=0&&((regs[i+1].wasconst>>rs)&1)) {
8523                 regs[i].regmap[hr]=AGEN1+((i+1)&1);
8524                 regmap_pre[i+1][hr]=AGEN1+((i+1)&1);
8525                 regs[i+1].regmap_entry[hr]=AGEN1+((i+1)&1);
8526                 regs[i].isconst&=~(1<<hr);
8527                 regs[i+1].wasdirty&=~(1<<hr);
8528                 regs[i].dirty&=~(1<<hr);
8529               }
8530             }
8531           }
8532         }
8533       }
8534     }
8535   }
8536 }
8537
8538 // Write back dirty registers as soon as we will no longer modify them,
8539 // so that we don't end up with lots of writes at the branches.
8540 static noinline void pass6_clean_registers(int istart, int iend, int wr)
8541 {
8542   static u_int wont_dirty[MAXBLOCK];
8543   static u_int will_dirty[MAXBLOCK];
8544   int i;
8545   int r;
8546   u_int will_dirty_i,will_dirty_next,temp_will_dirty;
8547   u_int wont_dirty_i,wont_dirty_next,temp_wont_dirty;
8548   if(iend==slen-1) {
8549     will_dirty_i=will_dirty_next=0;
8550     wont_dirty_i=wont_dirty_next=0;
8551   }else{
8552     will_dirty_i=will_dirty_next=will_dirty[iend+1];
8553     wont_dirty_i=wont_dirty_next=wont_dirty[iend+1];
8554   }
8555   for (i=iend;i>=istart;i--)
8556   {
8557     signed char rregmap_i[RRMAP_SIZE];
8558     u_int hr_candirty = 0;
8559     assert(HOST_REGS < 32);
8560     make_rregs(regs[i].regmap, rregmap_i, &hr_candirty);
8561     __builtin_prefetch(regs[i-1].regmap);
8562     if(dops[i].is_jump)
8563     {
8564       signed char branch_rregmap_i[RRMAP_SIZE];
8565       u_int branch_hr_candirty = 0;
8566       make_rregs(branch_regs[i].regmap, branch_rregmap_i, &branch_hr_candirty);
8567       if(cinfo[i].ba<start || cinfo[i].ba>=(start+slen*4))
8568       {
8569         // Branch out of this block, flush all regs
8570         will_dirty_i = 0;
8571         will_dirty_i |= 1u << (get_rreg(branch_rregmap_i, dops[i].rt1) & 31);
8572         will_dirty_i |= 1u << (get_rreg(branch_rregmap_i, dops[i].rt2) & 31);
8573         will_dirty_i |= 1u << (get_rreg(branch_rregmap_i, dops[i+1].rt1) & 31);
8574         will_dirty_i |= 1u << (get_rreg(branch_rregmap_i, dops[i+1].rt2) & 31);
8575         will_dirty_i |= 1u << (get_rreg(branch_rregmap_i, CCREG) & 31);
8576         will_dirty_i &= branch_hr_candirty;
8577         if (dops[i].is_ujump)
8578         {
8579           // Unconditional branch
8580           wont_dirty_i = 0;
8581           // Merge in delay slot (will dirty)
8582           will_dirty_i |= 1u << (get_rreg(rregmap_i, dops[i].rt1) & 31);
8583           will_dirty_i |= 1u << (get_rreg(rregmap_i, dops[i].rt2) & 31);
8584           will_dirty_i |= 1u << (get_rreg(rregmap_i, dops[i+1].rt1) & 31);
8585           will_dirty_i |= 1u << (get_rreg(rregmap_i, dops[i+1].rt2) & 31);
8586           will_dirty_i |= 1u << (get_rreg(rregmap_i, CCREG) & 31);
8587           will_dirty_i &= hr_candirty;
8588         }
8589         else
8590         {
8591           // Conditional branch
8592           wont_dirty_i = wont_dirty_next;
8593           // Merge in delay slot (will dirty)
8594           // (the original code had no explanation why these 2 are commented out)
8595           //will_dirty_i |= 1u << (get_rreg(rregmap_i, dops[i].rt1) & 31);
8596           //will_dirty_i |= 1u << (get_rreg(rregmap_i, dops[i].rt2) & 31);
8597           will_dirty_i |= 1u << (get_rreg(rregmap_i, dops[i+1].rt1) & 31);
8598           will_dirty_i |= 1u << (get_rreg(rregmap_i, dops[i+1].rt2) & 31);
8599           will_dirty_i |= 1u << (get_rreg(rregmap_i, CCREG) & 31);
8600           will_dirty_i &= hr_candirty;
8601         }
8602         // Merge in delay slot (wont dirty)
8603         wont_dirty_i |= 1u << (get_rreg(rregmap_i, dops[i].rt1) & 31);
8604         wont_dirty_i |= 1u << (get_rreg(rregmap_i, dops[i].rt2) & 31);
8605         wont_dirty_i |= 1u << (get_rreg(rregmap_i, dops[i+1].rt1) & 31);
8606         wont_dirty_i |= 1u << (get_rreg(rregmap_i, dops[i+1].rt2) & 31);
8607         wont_dirty_i |= 1u << (get_rreg(rregmap_i, CCREG) & 31);
8608         wont_dirty_i |= 1u << (get_rreg(branch_rregmap_i, dops[i].rt1) & 31);
8609         wont_dirty_i |= 1u << (get_rreg(branch_rregmap_i, dops[i].rt2) & 31);
8610         wont_dirty_i |= 1u << (get_rreg(branch_rregmap_i, dops[i+1].rt1) & 31);
8611         wont_dirty_i |= 1u << (get_rreg(branch_rregmap_i, dops[i+1].rt2) & 31);
8612         wont_dirty_i |= 1u << (get_rreg(branch_rregmap_i, CCREG) & 31);
8613         wont_dirty_i &= ~(1u << 31);
8614         if(wr) {
8615           #ifndef DESTRUCTIVE_WRITEBACK
8616           branch_regs[i].dirty&=wont_dirty_i;
8617           #endif
8618           branch_regs[i].dirty|=will_dirty_i;
8619         }
8620       }
8621       else
8622       {
8623         // Internal branch
8624         if(cinfo[i].ba<=start+i*4) {
8625           // Backward branch
8626           if (dops[i].is_ujump)
8627           {
8628             // Unconditional branch
8629             temp_will_dirty=0;
8630             temp_wont_dirty=0;
8631             // Merge in delay slot (will dirty)
8632             temp_will_dirty |= 1u << (get_rreg(branch_rregmap_i, dops[i].rt1) & 31);
8633             temp_will_dirty |= 1u << (get_rreg(branch_rregmap_i, dops[i].rt2) & 31);
8634             temp_will_dirty |= 1u << (get_rreg(branch_rregmap_i, dops[i+1].rt1) & 31);
8635             temp_will_dirty |= 1u << (get_rreg(branch_rregmap_i, dops[i+1].rt2) & 31);
8636             temp_will_dirty |= 1u << (get_rreg(branch_rregmap_i, CCREG) & 31);
8637             temp_will_dirty &= branch_hr_candirty;
8638             temp_will_dirty |= 1u << (get_rreg(rregmap_i, dops[i].rt1) & 31);
8639             temp_will_dirty |= 1u << (get_rreg(rregmap_i, dops[i].rt2) & 31);
8640             temp_will_dirty |= 1u << (get_rreg(rregmap_i, dops[i+1].rt1) & 31);
8641             temp_will_dirty |= 1u << (get_rreg(rregmap_i, dops[i+1].rt2) & 31);
8642             temp_will_dirty |= 1u << (get_rreg(rregmap_i, CCREG) & 31);
8643             temp_will_dirty &= hr_candirty;
8644           } else {
8645             // Conditional branch (not taken case)
8646             temp_will_dirty=will_dirty_next;
8647             temp_wont_dirty=wont_dirty_next;
8648             // Merge in delay slot (will dirty)
8649             temp_will_dirty |= 1u << (get_rreg(branch_rregmap_i, dops[i].rt1) & 31);
8650             temp_will_dirty |= 1u << (get_rreg(branch_rregmap_i, dops[i].rt2) & 31);
8651             temp_will_dirty |= 1u << (get_rreg(branch_rregmap_i, dops[i+1].rt1) & 31);
8652             temp_will_dirty |= 1u << (get_rreg(branch_rregmap_i, dops[i+1].rt2) & 31);
8653             temp_will_dirty |= 1u << (get_rreg(branch_rregmap_i, CCREG) & 31);
8654             temp_will_dirty &= branch_hr_candirty;
8655             //temp_will_dirty |= 1u << (get_rreg(rregmap_i, dops[i].rt1) & 31);
8656             //temp_will_dirty |= 1u << (get_rreg(rregmap_i, dops[i].rt2) & 31);
8657             temp_will_dirty |= 1u << (get_rreg(rregmap_i, dops[i+1].rt1) & 31);
8658             temp_will_dirty |= 1u << (get_rreg(rregmap_i, dops[i+1].rt2) & 31);
8659             temp_will_dirty |= 1u << (get_rreg(rregmap_i, CCREG) & 31);
8660             temp_will_dirty &= hr_candirty;
8661           }
8662           // Merge in delay slot (wont dirty)
8663           temp_wont_dirty |= 1u << (get_rreg(rregmap_i, dops[i].rt1) & 31);
8664           temp_wont_dirty |= 1u << (get_rreg(rregmap_i, dops[i].rt2) & 31);
8665           temp_wont_dirty |= 1u << (get_rreg(rregmap_i, dops[i+1].rt1) & 31);
8666           temp_wont_dirty |= 1u << (get_rreg(rregmap_i, dops[i+1].rt2) & 31);
8667           temp_wont_dirty |= 1u << (get_rreg(rregmap_i, CCREG) & 31);
8668           temp_wont_dirty |= 1u << (get_rreg(branch_rregmap_i, dops[i].rt1) & 31);
8669           temp_wont_dirty |= 1u << (get_rreg(branch_rregmap_i, dops[i].rt2) & 31);
8670           temp_wont_dirty |= 1u << (get_rreg(branch_rregmap_i, dops[i+1].rt1) & 31);
8671           temp_wont_dirty |= 1u << (get_rreg(branch_rregmap_i, dops[i+1].rt2) & 31);
8672           temp_wont_dirty |= 1u << (get_rreg(branch_rregmap_i, CCREG) & 31);
8673           temp_wont_dirty &= ~(1u << 31);
8674           // Deal with changed mappings
8675           if(i<iend) {
8676             for(r=0;r<HOST_REGS;r++) {
8677               if(r!=EXCLUDE_REG) {
8678                 if(regs[i].regmap[r]!=regmap_pre[i][r]) {
8679                   temp_will_dirty&=~(1<<r);
8680                   temp_wont_dirty&=~(1<<r);
8681                   if(regmap_pre[i][r]>0 && regmap_pre[i][r]<34) {
8682                     temp_will_dirty|=((unneeded_reg[i]>>regmap_pre[i][r])&1)<<r;
8683                     temp_wont_dirty|=((unneeded_reg[i]>>regmap_pre[i][r])&1)<<r;
8684                   } else {
8685                     temp_will_dirty|=1<<r;
8686                     temp_wont_dirty|=1<<r;
8687                   }
8688                 }
8689               }
8690             }
8691           }
8692           if(wr) {
8693             will_dirty[i]=temp_will_dirty;
8694             wont_dirty[i]=temp_wont_dirty;
8695             pass6_clean_registers((cinfo[i].ba-start)>>2,i-1,0);
8696           }else{
8697             // Limit recursion.  It can take an excessive amount
8698             // of time if there are a lot of nested loops.
8699             will_dirty[(cinfo[i].ba-start)>>2]=0;
8700             wont_dirty[(cinfo[i].ba-start)>>2]=-1;
8701           }
8702         }
8703         /*else*/ if(1)
8704         {
8705           if (dops[i].is_ujump)
8706           {
8707             // Unconditional branch
8708             will_dirty_i=0;
8709             wont_dirty_i=0;
8710           //if(cinfo[i].ba>start+i*4) { // Disable recursion (for debugging)
8711             for(r=0;r<HOST_REGS;r++) {
8712               if(r!=EXCLUDE_REG) {
8713                 if(branch_regs[i].regmap[r]==regs[(cinfo[i].ba-start)>>2].regmap_entry[r]) {
8714                   will_dirty_i|=will_dirty[(cinfo[i].ba-start)>>2]&(1<<r);
8715                   wont_dirty_i|=wont_dirty[(cinfo[i].ba-start)>>2]&(1<<r);
8716                 }
8717                 if(branch_regs[i].regmap[r]>=0) {
8718                   will_dirty_i|=((unneeded_reg[(cinfo[i].ba-start)>>2]>>branch_regs[i].regmap[r])&1)<<r;
8719                   wont_dirty_i|=((unneeded_reg[(cinfo[i].ba-start)>>2]>>branch_regs[i].regmap[r])&1)<<r;
8720                 }
8721               }
8722             }
8723           //}
8724             // Merge in delay slot
8725             will_dirty_i |= 1u << (get_rreg(branch_rregmap_i, dops[i].rt1) & 31);
8726             will_dirty_i |= 1u << (get_rreg(branch_rregmap_i, dops[i].rt2) & 31);
8727             will_dirty_i |= 1u << (get_rreg(branch_rregmap_i, dops[i+1].rt1) & 31);
8728             will_dirty_i |= 1u << (get_rreg(branch_rregmap_i, dops[i+1].rt2) & 31);
8729             will_dirty_i |= 1u << (get_rreg(branch_rregmap_i, CCREG) & 31);
8730             will_dirty_i &= branch_hr_candirty;
8731             will_dirty_i |= 1u << (get_rreg(rregmap_i, dops[i].rt1) & 31);
8732             will_dirty_i |= 1u << (get_rreg(rregmap_i, dops[i].rt2) & 31);
8733             will_dirty_i |= 1u << (get_rreg(rregmap_i, dops[i+1].rt1) & 31);
8734             will_dirty_i |= 1u << (get_rreg(rregmap_i, dops[i+1].rt2) & 31);
8735             will_dirty_i |= 1u << (get_rreg(rregmap_i, CCREG) & 31);
8736             will_dirty_i &= hr_candirty;
8737           } else {
8738             // Conditional branch
8739             will_dirty_i=will_dirty_next;
8740             wont_dirty_i=wont_dirty_next;
8741           //if(cinfo[i].ba>start+i*4) // Disable recursion (for debugging)
8742             for(r=0;r<HOST_REGS;r++) {
8743               if(r!=EXCLUDE_REG) {
8744                 signed char target_reg=branch_regs[i].regmap[r];
8745                 if(target_reg==regs[(cinfo[i].ba-start)>>2].regmap_entry[r]) {
8746                   will_dirty_i&=will_dirty[(cinfo[i].ba-start)>>2]&(1<<r);
8747                   wont_dirty_i|=wont_dirty[(cinfo[i].ba-start)>>2]&(1<<r);
8748                 }
8749                 else if(target_reg>=0) {
8750                   will_dirty_i&=((unneeded_reg[(cinfo[i].ba-start)>>2]>>target_reg)&1)<<r;
8751                   wont_dirty_i|=((unneeded_reg[(cinfo[i].ba-start)>>2]>>target_reg)&1)<<r;
8752                 }
8753               }
8754             }
8755             // Merge in delay slot
8756             will_dirty_i |= 1u << (get_rreg(branch_rregmap_i, dops[i].rt1) & 31);
8757             will_dirty_i |= 1u << (get_rreg(branch_rregmap_i, dops[i].rt2) & 31);
8758             will_dirty_i |= 1u << (get_rreg(branch_rregmap_i, dops[i+1].rt1) & 31);
8759             will_dirty_i |= 1u << (get_rreg(branch_rregmap_i, dops[i+1].rt2) & 31);
8760             will_dirty_i |= 1u << (get_rreg(branch_rregmap_i, CCREG) & 31);
8761             will_dirty_i &= branch_hr_candirty;
8762             //will_dirty_i |= 1u << (get_rreg(rregmap_i, dops[i].rt1) & 31);
8763             //will_dirty_i |= 1u << (get_rreg(rregmap_i, dops[i].rt2) & 31);
8764             will_dirty_i |= 1u << (get_rreg(rregmap_i, dops[i+1].rt1) & 31);
8765             will_dirty_i |= 1u << (get_rreg(rregmap_i, dops[i+1].rt2) & 31);
8766             will_dirty_i |= 1u << (get_rreg(rregmap_i, CCREG) & 31);
8767             will_dirty_i &= hr_candirty;
8768           }
8769           // Merge in delay slot (won't dirty)
8770           wont_dirty_i |= 1u << (get_rreg(rregmap_i, dops[i].rt1) & 31);
8771           wont_dirty_i |= 1u << (get_rreg(rregmap_i, dops[i].rt2) & 31);
8772           wont_dirty_i |= 1u << (get_rreg(rregmap_i, dops[i+1].rt1) & 31);
8773           wont_dirty_i |= 1u << (get_rreg(rregmap_i, dops[i+1].rt2) & 31);
8774           wont_dirty_i |= 1u << (get_rreg(rregmap_i, CCREG) & 31);
8775           wont_dirty_i |= 1u << (get_rreg(branch_rregmap_i, dops[i].rt1) & 31);
8776           wont_dirty_i |= 1u << (get_rreg(branch_rregmap_i, dops[i].rt2) & 31);
8777           wont_dirty_i |= 1u << (get_rreg(branch_rregmap_i, dops[i+1].rt1) & 31);
8778           wont_dirty_i |= 1u << (get_rreg(branch_rregmap_i, dops[i+1].rt2) & 31);
8779           wont_dirty_i |= 1u << (get_rreg(branch_rregmap_i, CCREG) & 31);
8780           wont_dirty_i &= ~(1u << 31);
8781           if(wr) {
8782             #ifndef DESTRUCTIVE_WRITEBACK
8783             branch_regs[i].dirty&=wont_dirty_i;
8784             #endif
8785             branch_regs[i].dirty|=will_dirty_i;
8786           }
8787         }
8788       }
8789     }
8790     else if (dops[i].is_exception)
8791     {
8792       // SYSCALL instruction, etc
8793       will_dirty_i=0;
8794       wont_dirty_i=0;
8795     }
8796     will_dirty_next=will_dirty_i;
8797     wont_dirty_next=wont_dirty_i;
8798     will_dirty_i |= 1u << (get_rreg(rregmap_i, dops[i].rt1) & 31);
8799     will_dirty_i |= 1u << (get_rreg(rregmap_i, dops[i].rt2) & 31);
8800     will_dirty_i |= 1u << (get_rreg(rregmap_i, CCREG) & 31);
8801     will_dirty_i &= hr_candirty;
8802     wont_dirty_i |= 1u << (get_rreg(rregmap_i, dops[i].rt1) & 31);
8803     wont_dirty_i |= 1u << (get_rreg(rregmap_i, dops[i].rt2) & 31);
8804     wont_dirty_i |= 1u << (get_rreg(rregmap_i, CCREG) & 31);
8805     wont_dirty_i &= ~(1u << 31);
8806     if (i > istart && !dops[i].is_jump) {
8807       // Don't store a register immediately after writing it,
8808       // may prevent dual-issue.
8809       wont_dirty_i |= 1u << (get_rreg(rregmap_i, dops[i-1].rt1) & 31);
8810       wont_dirty_i |= 1u << (get_rreg(rregmap_i, dops[i-1].rt2) & 31);
8811     }
8812     // Save it
8813     will_dirty[i]=will_dirty_i;
8814     wont_dirty[i]=wont_dirty_i;
8815     // Mark registers that won't be dirtied as not dirty
8816     if(wr) {
8817         regs[i].dirty|=will_dirty_i;
8818         #ifndef DESTRUCTIVE_WRITEBACK
8819         regs[i].dirty&=wont_dirty_i;
8820         if(dops[i].is_jump)
8821         {
8822           if (i < iend-1 && !dops[i].is_ujump) {
8823             for(r=0;r<HOST_REGS;r++) {
8824               if(r!=EXCLUDE_REG) {
8825                 if(regs[i].regmap[r]==regmap_pre[i+2][r]) {
8826                   regs[i+2].wasdirty&=wont_dirty_i|~(1<<r);
8827                 }else {/*printf("i: %x (%d) mismatch(+2): %d\n",start+i*4,i,r);assert(!((wont_dirty_i>>r)&1));*/}
8828               }
8829             }
8830           }
8831         }
8832         else
8833         {
8834           if(i<iend) {
8835             for(r=0;r<HOST_REGS;r++) {
8836               if(r!=EXCLUDE_REG) {
8837                 if(regs[i].regmap[r]==regmap_pre[i+1][r]) {
8838                   regs[i+1].wasdirty&=wont_dirty_i|~(1<<r);
8839                 }else {/*printf("i: %x (%d) mismatch(+1): %d\n",start+i*4,i,r);assert(!((wont_dirty_i>>r)&1));*/}
8840               }
8841             }
8842           }
8843         }
8844         #endif
8845     }
8846     // Deal with changed mappings
8847     temp_will_dirty=will_dirty_i;
8848     temp_wont_dirty=wont_dirty_i;
8849     for(r=0;r<HOST_REGS;r++) {
8850       if(r!=EXCLUDE_REG) {
8851         int nr;
8852         if(regs[i].regmap[r]==regmap_pre[i][r]) {
8853           if(wr) {
8854             #ifndef DESTRUCTIVE_WRITEBACK
8855             regs[i].wasdirty&=wont_dirty_i|~(1<<r);
8856             #endif
8857             regs[i].wasdirty|=will_dirty_i&(1<<r);
8858           }
8859         }
8860         else if(regmap_pre[i][r]>=0&&(nr=get_rreg(rregmap_i,regmap_pre[i][r]))>=0) {
8861           // Register moved to a different register
8862           will_dirty_i&=~(1<<r);
8863           wont_dirty_i&=~(1<<r);
8864           will_dirty_i|=((temp_will_dirty>>nr)&1)<<r;
8865           wont_dirty_i|=((temp_wont_dirty>>nr)&1)<<r;
8866           if(wr) {
8867             #ifndef DESTRUCTIVE_WRITEBACK
8868             regs[i].wasdirty&=wont_dirty_i|~(1<<r);
8869             #endif
8870             regs[i].wasdirty|=will_dirty_i&(1<<r);
8871           }
8872         }
8873         else {
8874           will_dirty_i&=~(1<<r);
8875           wont_dirty_i&=~(1<<r);
8876           if(regmap_pre[i][r]>0 && regmap_pre[i][r]<34) {
8877             will_dirty_i|=((unneeded_reg[i]>>regmap_pre[i][r])&1)<<r;
8878             wont_dirty_i|=((unneeded_reg[i]>>regmap_pre[i][r])&1)<<r;
8879           } else {
8880             wont_dirty_i|=1<<r;
8881             /*printf("i: %x (%d) mismatch: %d\n",start+i*4,i,r);assert(!((will_dirty>>r)&1));*/
8882           }
8883         }
8884       }
8885     }
8886   }
8887 }
8888
8889 static noinline void pass10_expire_blocks(void)
8890 {
8891   u_int step = MAX_OUTPUT_BLOCK_SIZE / PAGE_COUNT / 2;
8892   // not sizeof(ndrc->translation_cache) due to vita hack
8893   u_int step_mask = ((1u << TARGET_SIZE_2) - 1u) & ~(step - 1u);
8894   u_int end = (out - ndrc->translation_cache + EXPIRITY_OFFSET) & step_mask;
8895   u_int base_shift = __builtin_ctz(MAX_OUTPUT_BLOCK_SIZE);
8896   int hit;
8897
8898   for (; expirep != end; expirep = ((expirep + step) & step_mask))
8899   {
8900     u_int base_offs = expirep & ~(MAX_OUTPUT_BLOCK_SIZE - 1);
8901     u_int block_i = expirep / step & (PAGE_COUNT - 1);
8902     u_int phase = (expirep >> (base_shift - 1)) & 1u;
8903     if (!(expirep & (MAX_OUTPUT_BLOCK_SIZE / 2 - 1))) {
8904       inv_debug("EXP: base_offs %x/%lx phase %u\n", base_offs,
8905         (long)(out - ndrc->translation_cache), phase);
8906     }
8907
8908     if (!phase) {
8909       hit = blocks_remove_matching_addrs(&blocks[block_i], base_offs, base_shift);
8910       if (hit) {
8911         do_clear_cache();
8912         mini_ht_clear();
8913       }
8914     }
8915     else
8916       unlink_jumps_tc_range(jumps[block_i], base_offs, base_shift);
8917   }
8918 }
8919
8920 static struct block_info *new_block_info(u_int start, u_int len,
8921   const void *source, const void *copy, u_char *beginning, u_short jump_in_count)
8922 {
8923   struct block_info **b_pptr;
8924   struct block_info *block;
8925   u_int page = get_page(start);
8926
8927   block = malloc(sizeof(*block) + jump_in_count * sizeof(block->jump_in[0]));
8928   assert(block);
8929   assert(jump_in_count > 0);
8930   block->source = source;
8931   block->copy = copy;
8932   block->start = start;
8933   block->len = len;
8934   block->reg_sv_flags = 0;
8935   block->tc_offs = beginning - ndrc->translation_cache;
8936   //block->tc_len = out - beginning;
8937   block->is_dirty = 0;
8938   block->inv_near_misses = 0;
8939   block->jump_in_cnt = jump_in_count;
8940
8941   // insert sorted by start mirror-unmasked vaddr
8942   for (b_pptr = &blocks[page]; ; b_pptr = &((*b_pptr)->next)) {
8943     if (*b_pptr == NULL || (*b_pptr)->start >= start) {
8944       block->next = *b_pptr;
8945       *b_pptr = block;
8946       break;
8947     }
8948   }
8949   stat_inc(stat_blocks);
8950   return block;
8951 }
8952
8953 static int new_recompile_block(u_int addr)
8954 {
8955   u_int pagelimit = 0;
8956   u_int state_rflags = 0;
8957   int i;
8958
8959   assem_debug("NOTCOMPILED: addr = %x -> %p\n", addr, out);
8960
8961   if (addr & 3) {
8962     if (addr != hack_addr) {
8963       SysPrintf("game crash @%08x, ra=%08x\n", addr, psxRegs.GPR.n.ra);
8964       hack_addr = addr;
8965     }
8966     return -1;
8967   }
8968
8969   // this is just for speculation
8970   for (i = 1; i < 32; i++) {
8971     if ((psxRegs.GPR.r[i] & 0xffff0000) == 0x1f800000)
8972       state_rflags |= 1 << i;
8973   }
8974
8975   start = addr;
8976   new_dynarec_did_compile=1;
8977   if (Config.HLE && start == 0x80001000) // hlecall
8978   {
8979     void *beginning = start_block();
8980
8981     emit_movimm(start,0);
8982     emit_writeword(0,&pcaddr);
8983     emit_far_jump(new_dyna_leave);
8984     literal_pool(0);
8985     end_block(beginning);
8986     struct block_info *block = new_block_info(start, 4, NULL, NULL, beginning, 1);
8987     block->jump_in[0].vaddr = start;
8988     block->jump_in[0].addr = beginning;
8989     return 0;
8990   }
8991   else if (f1_hack && hack_addr == 0) {
8992     void *beginning = start_block();
8993     emit_movimm(start, 0);
8994     emit_writeword(0, &hack_addr);
8995     emit_readword(&psxRegs.GPR.n.sp, 0);
8996     emit_readptr(&mem_rtab, 1);
8997     emit_shrimm(0, 12, 2);
8998     emit_readptr_dualindexedx_ptrlen(1, 2, 1);
8999     emit_addimm(0, 0x18, 0);
9000     emit_adds_ptr(1, 1, 1);
9001     emit_ldr_dualindexed(1, 0, 0);
9002     emit_writeword(0, &psxRegs.GPR.r[26]); // lw k0, 0x18(sp)
9003     emit_far_call(ndrc_get_addr_ht);
9004     emit_jmpreg(0); // jr k0
9005     literal_pool(0);
9006     end_block(beginning);
9007
9008     struct block_info *block = new_block_info(start, 4, NULL, NULL, beginning, 1);
9009     block->jump_in[0].vaddr = start;
9010     block->jump_in[0].addr = beginning;
9011     SysPrintf("F1 hack to   %08x\n", start);
9012     return 0;
9013   }
9014
9015   cycle_multiplier_active = get_cycle_multiplier();
9016
9017   source = get_source_start(start, &pagelimit);
9018   if (source == NULL) {
9019     if (addr != hack_addr) {
9020       SysPrintf("Compile at bogus memory address: %08x, ra=%x\n",
9021         addr, psxRegs.GPR.n.ra);
9022       hack_addr = addr;
9023     }
9024     //abort();
9025     return -1;
9026   }
9027
9028   /* Pass 1: disassemble */
9029   /* Pass 2: register dependencies, branch targets */
9030   /* Pass 3: register allocation */
9031   /* Pass 4: branch dependencies */
9032   /* Pass 5: pre-alloc */
9033   /* Pass 6: optimize clean/dirty state */
9034   /* Pass 7: flag 32-bit registers */
9035   /* Pass 8: assembly */
9036   /* Pass 9: linker */
9037   /* Pass 10: garbage collection / free memory */
9038
9039   /* Pass 1 disassembly */
9040
9041   pass1_disassemble(pagelimit);
9042
9043   int clear_hack_addr = apply_hacks();
9044
9045   /* Pass 2 - Register dependencies and branch targets */
9046
9047   pass2_unneeded_regs(0,slen-1,0);
9048
9049   pass2a_unneeded_other();
9050
9051   /* Pass 3 - Register allocation */
9052
9053   pass3_register_alloc(addr);
9054
9055   /* Pass 4 - Cull unused host registers */
9056
9057   pass4_cull_unused_regs();
9058
9059   /* Pass 5 - Pre-allocate registers */
9060
9061   pass5a_preallocate1();
9062   pass5b_preallocate2();
9063
9064   /* Pass 6 - Optimize clean/dirty state */
9065   pass6_clean_registers(0, slen-1, 1);
9066
9067   /* Pass 7 */
9068   for (i=slen-1;i>=0;i--)
9069   {
9070     if(dops[i].itype==CJUMP||dops[i].itype==SJUMP)
9071     {
9072       // Conditional branch
9073       if((source[i]>>16)!=0x1000&&i<slen-2) {
9074         // Mark this address as a branch target since it may be called
9075         // upon return from interrupt
9076         dops[i+2].bt=1;
9077       }
9078     }
9079   }
9080
9081   /* Pass 8 - Assembly */
9082   linkcount=0;stubcount=0;
9083   is_delayslot=0;
9084   u_int dirty_pre=0;
9085   void *beginning=start_block();
9086   void *instr_addr0_override = NULL;
9087   int ds = 0;
9088
9089   if ((Config.HLE && start == 0x80000080) || start == 0x80030000) {
9090     instr_addr0_override = out;
9091     emit_movimm(start, 0);
9092     if (start == 0x80030000) {
9093       // for BiosBootBypass() to work
9094       // io address var abused as a "already been here" flag
9095       emit_readword(&address, 1);
9096       emit_writeword(0, &pcaddr);
9097       emit_writeword(0, &address);
9098       emit_cmp(0, 1);
9099     }
9100     else {
9101       emit_readword(&psxRegs.cpuInRecursion, 1);
9102       emit_writeword(0, &pcaddr);
9103       emit_test(1, 1);
9104     }
9105     #ifdef __aarch64__
9106     emit_jeq(out + 4*2);
9107     emit_far_jump(new_dyna_leave);
9108     #else
9109     emit_jne(new_dyna_leave);
9110     #endif
9111   }
9112   for(i=0;i<slen;i++)
9113   {
9114     __builtin_prefetch(regs[i+1].regmap);
9115     check_regmap(regmap_pre[i]);
9116     check_regmap(regs[i].regmap_entry);
9117     check_regmap(regs[i].regmap);
9118     //if(ds) printf("ds: ");
9119     disassemble_inst(i);
9120     if(ds) {
9121       ds=0; // Skip delay slot
9122       if(dops[i].bt) assem_debug("OOPS - branch into delay slot\n");
9123       instr_addr[i] = NULL;
9124     } else {
9125       speculate_register_values(i);
9126       #ifndef DESTRUCTIVE_WRITEBACK
9127       if (i < 2 || !dops[i-2].is_ujump)
9128       {
9129         wb_valid(regmap_pre[i],regs[i].regmap_entry,dirty_pre,regs[i].wasdirty,unneeded_reg[i]);
9130       }
9131       if((dops[i].itype==CJUMP||dops[i].itype==SJUMP)) {
9132         dirty_pre=branch_regs[i].dirty;
9133       }else{
9134         dirty_pre=regs[i].dirty;
9135       }
9136       #endif
9137       // write back
9138       if (i < 2 || !dops[i-2].is_ujump)
9139       {
9140         wb_invalidate(regmap_pre[i],regs[i].regmap_entry,regs[i].wasdirty,unneeded_reg[i]);
9141         loop_preload(regmap_pre[i],regs[i].regmap_entry);
9142       }
9143       // branch target entry point
9144       instr_addr[i] = out;
9145       assem_debug("<->\n");
9146       drc_dbg_emit_do_cmp(i, cinfo[i].ccadj);
9147       if (clear_hack_addr) {
9148         emit_movimm(0, 0);
9149         emit_writeword(0, &hack_addr);
9150         clear_hack_addr = 0;
9151       }
9152
9153       // load regs
9154       if(regs[i].regmap_entry[HOST_CCREG]==CCREG&&regs[i].regmap[HOST_CCREG]!=CCREG)
9155         wb_register(CCREG,regs[i].regmap_entry,regs[i].wasdirty);
9156       load_regs(regs[i].regmap_entry,regs[i].regmap,dops[i].rs1,dops[i].rs2);
9157       address_generation(i,&regs[i],regs[i].regmap_entry);
9158       load_consts(regmap_pre[i],regs[i].regmap,i);
9159       if(dops[i].is_jump)
9160       {
9161         // Load the delay slot registers if necessary
9162         if(dops[i+1].rs1!=dops[i].rs1&&dops[i+1].rs1!=dops[i].rs2&&(dops[i+1].rs1!=dops[i].rt1||dops[i].rt1==0))
9163           load_regs(regs[i].regmap_entry,regs[i].regmap,dops[i+1].rs1,dops[i+1].rs1);
9164         if(dops[i+1].rs2!=dops[i+1].rs1&&dops[i+1].rs2!=dops[i].rs1&&dops[i+1].rs2!=dops[i].rs2&&(dops[i+1].rs2!=dops[i].rt1||dops[i].rt1==0))
9165           load_regs(regs[i].regmap_entry,regs[i].regmap,dops[i+1].rs2,dops[i+1].rs2);
9166         if (ram_offset && (dops[i+1].is_load || dops[i+1].is_store))
9167           load_reg(regs[i].regmap_entry,regs[i].regmap,ROREG);
9168         if (dops[i+1].is_store)
9169           load_reg(regs[i].regmap_entry,regs[i].regmap,INVCP);
9170       }
9171       else if(i+1<slen)
9172       {
9173         // Preload registers for following instruction
9174         if(dops[i+1].rs1!=dops[i].rs1&&dops[i+1].rs1!=dops[i].rs2)
9175           if(dops[i+1].rs1!=dops[i].rt1&&dops[i+1].rs1!=dops[i].rt2)
9176             load_regs(regs[i].regmap_entry,regs[i].regmap,dops[i+1].rs1,dops[i+1].rs1);
9177         if(dops[i+1].rs2!=dops[i+1].rs1&&dops[i+1].rs2!=dops[i].rs1&&dops[i+1].rs2!=dops[i].rs2)
9178           if(dops[i+1].rs2!=dops[i].rt1&&dops[i+1].rs2!=dops[i].rt2)
9179             load_regs(regs[i].regmap_entry,regs[i].regmap,dops[i+1].rs2,dops[i+1].rs2);
9180       }
9181       // TODO: if(is_ooo(i)) address_generation(i+1);
9182       if (!dops[i].is_jump || dops[i].itype == CJUMP)
9183         load_reg(regs[i].regmap_entry,regs[i].regmap,CCREG);
9184       if (ram_offset && (dops[i].is_load || dops[i].is_store))
9185         load_reg(regs[i].regmap_entry,regs[i].regmap,ROREG);
9186       if (dops[i].is_store)
9187         load_reg(regs[i].regmap_entry,regs[i].regmap,INVCP);
9188
9189       ds = assemble(i, &regs[i], cinfo[i].ccadj);
9190
9191       drc_dbg_emit_wb_dirtys(i, &regs[i]);
9192       if (dops[i].is_ujump)
9193         literal_pool(1024);
9194       else
9195         literal_pool_jumpover(256);
9196     }
9197   }
9198
9199   assert(slen > 0);
9200   if (slen > 0 && dops[slen-1].itype == INTCALL) {
9201     // no ending needed for this block since INTCALL never returns
9202   }
9203   // If the block did not end with an unconditional branch,
9204   // add a jump to the next instruction.
9205   else if (i > 1) {
9206     if (!dops[i-2].is_ujump) {
9207       assert(!dops[i-1].is_jump);
9208       assert(i==slen);
9209       if(dops[i-2].itype!=CJUMP&&dops[i-2].itype!=SJUMP) {
9210         store_regs_bt(regs[i-1].regmap,regs[i-1].dirty,start+i*4);
9211         if(regs[i-1].regmap[HOST_CCREG]!=CCREG)
9212           emit_loadreg(CCREG,HOST_CCREG);
9213         emit_addimm(HOST_CCREG, cinfo[i-1].ccadj + CLOCK_ADJUST(1), HOST_CCREG);
9214       }
9215       else
9216       {
9217         store_regs_bt(branch_regs[i-2].regmap,branch_regs[i-2].dirty,start+i*4);
9218         assert(branch_regs[i-2].regmap[HOST_CCREG]==CCREG);
9219       }
9220       add_to_linker(out,start+i*4,0);
9221       emit_jmp(0);
9222     }
9223   }
9224   else
9225   {
9226     assert(i>0);
9227     assert(!dops[i-1].is_jump);
9228     store_regs_bt(regs[i-1].regmap,regs[i-1].dirty,start+i*4);
9229     if(regs[i-1].regmap[HOST_CCREG]!=CCREG)
9230       emit_loadreg(CCREG,HOST_CCREG);
9231     emit_addimm(HOST_CCREG, cinfo[i-1].ccadj + CLOCK_ADJUST(1), HOST_CCREG);
9232     add_to_linker(out,start+i*4,0);
9233     emit_jmp(0);
9234   }
9235
9236   // Stubs
9237   for(i = 0; i < stubcount; i++)
9238   {
9239     switch(stubs[i].type)
9240     {
9241       case LOADB_STUB:
9242       case LOADH_STUB:
9243       case LOADW_STUB:
9244       case LOADBU_STUB:
9245       case LOADHU_STUB:
9246         do_readstub(i);break;
9247       case STOREB_STUB:
9248       case STOREH_STUB:
9249       case STOREW_STUB:
9250         do_writestub(i);break;
9251       case CC_STUB:
9252         do_ccstub(i);break;
9253       case INVCODE_STUB:
9254         do_invstub(i);break;
9255       case STORELR_STUB:
9256         do_unalignedwritestub(i);break;
9257       case OVERFLOW_STUB:
9258         do_overflowstub(i); break;
9259       case ALIGNMENT_STUB:
9260         do_alignmentstub(i); break;
9261       default:
9262         assert(0);
9263     }
9264   }
9265
9266   if (instr_addr0_override)
9267     instr_addr[0] = instr_addr0_override;
9268
9269 #if 0
9270   /* check for improper expiration */
9271   for (i = 0; i < ARRAY_SIZE(jumps); i++) {
9272     int j;
9273     if (!jumps[i])
9274       continue;
9275     for (j = 0; j < jumps[i]->count; j++)
9276       assert(jumps[i]->e[j].stub < beginning || (u_char *)jumps[i]->e[j].stub > out);
9277   }
9278 #endif
9279
9280   /* Pass 9 - Linker */
9281   for(i=0;i<linkcount;i++)
9282   {
9283     assem_debug("%p -> %8x\n",link_addr[i].addr,link_addr[i].target);
9284     literal_pool(64);
9285     if (!link_addr[i].internal)
9286     {
9287       void *stub = out;
9288       void *addr = check_addr(link_addr[i].target);
9289       emit_extjump(link_addr[i].addr, link_addr[i].target);
9290       if (addr) {
9291         set_jump_target(link_addr[i].addr, addr);
9292         ndrc_add_jump_out(link_addr[i].target,stub);
9293       }
9294       else
9295         set_jump_target(link_addr[i].addr, stub);
9296     }
9297     else
9298     {
9299       // Internal branch
9300       int target=(link_addr[i].target-start)>>2;
9301       assert(target>=0&&target<slen);
9302       assert(instr_addr[target]);
9303       //#ifdef CORTEX_A8_BRANCH_PREDICTION_HACK
9304       //set_jump_target_fillslot(link_addr[i].addr,instr_addr[target],link_addr[i].ext>>1);
9305       //#else
9306       set_jump_target(link_addr[i].addr, instr_addr[target]);
9307       //#endif
9308     }
9309   }
9310
9311   u_int source_len = slen*4;
9312   if (dops[slen-1].itype == INTCALL && source_len > 4)
9313     // no need to treat the last instruction as compiled
9314     // as interpreter fully handles it
9315     source_len -= 4;
9316
9317   if ((u_char *)copy + source_len > (u_char *)shadow + sizeof(shadow))
9318     copy = shadow;
9319
9320   // External Branch Targets (jump_in)
9321   int jump_in_count = 1;
9322   assert(instr_addr[0]);
9323   for (i = 1; i < slen; i++)
9324   {
9325     if (dops[i].bt && instr_addr[i])
9326       jump_in_count++;
9327   }
9328
9329   struct block_info *block =
9330     new_block_info(start, slen * 4, source, copy, beginning, jump_in_count);
9331   block->reg_sv_flags = state_rflags;
9332
9333   int jump_in_i = 0;
9334   for (i = 0; i < slen; i++)
9335   {
9336     if ((i == 0 || dops[i].bt) && instr_addr[i])
9337     {
9338       assem_debug("%p (%d) <- %8x\n", instr_addr[i], i, start + i*4);
9339       u_int vaddr = start + i*4;
9340
9341       literal_pool(256);
9342       void *entry = out;
9343       load_regs_entry(i);
9344       if (entry == out)
9345         entry = instr_addr[i];
9346       else
9347         emit_jmp(instr_addr[i]);
9348
9349       block->jump_in[jump_in_i].vaddr = vaddr;
9350       block->jump_in[jump_in_i].addr = entry;
9351       jump_in_i++;
9352     }
9353   }
9354   assert(jump_in_i == jump_in_count);
9355   hash_table_add(block->jump_in[0].vaddr, block->jump_in[0].addr);
9356   // Write out the literal pool if necessary
9357   literal_pool(0);
9358   #ifdef CORTEX_A8_BRANCH_PREDICTION_HACK
9359   // Align code
9360   if(((u_int)out)&7) emit_addnop(13);
9361   #endif
9362   assert(out - (u_char *)beginning < MAX_OUTPUT_BLOCK_SIZE);
9363   //printf("shadow buffer: %p-%p\n",copy,(u_char *)copy+slen*4);
9364   memcpy(copy, source, source_len);
9365   copy += source_len;
9366
9367   end_block(beginning);
9368
9369   // If we're within 256K of the end of the buffer,
9370   // start over from the beginning. (Is 256K enough?)
9371   if (out > ndrc->translation_cache + sizeof(ndrc->translation_cache) - MAX_OUTPUT_BLOCK_SIZE)
9372     out = ndrc->translation_cache;
9373
9374   // Trap writes to any of the pages we compiled
9375   mark_invalid_code(start, slen*4, 0);
9376
9377   /* Pass 10 - Free memory by expiring oldest blocks */
9378
9379   pass10_expire_blocks();
9380
9381 #ifdef ASSEM_PRINT
9382   fflush(stdout);
9383 #endif
9384   stat_inc(stat_bc_direct);
9385   return 0;
9386 }
9387
9388 // vim:shiftwidth=2:expandtab