rec_arm64.cpp 53.5 KB
Newer Older
Flyinghead's avatar
Flyinghead committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/*
	Copyright 2019 flyinghead

	This file is part of reicast.

    reicast is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 2 of the License, or
    (at your option) any later version.

    reicast is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with reicast.  If not, see <https://www.gnu.org/licenses/>.
 */

#include "types.h"

#if FEAT_SHREC == DYNAREC_JIT

#include <unistd.h>
#include <map>
26
#include <setjmp.h>
Flyinghead's avatar
Flyinghead committed
27
28
29
30

#include "deps/vixl/aarch64/macro-assembler-aarch64.h"
using namespace vixl::aarch64;

Libretro-Admin's avatar
Libretro-Admin committed
31
//#define NO_BLOCK_LINKING
32

Flyinghead's avatar
Flyinghead committed
33
34
35
36
37
38
39
40
#include "hw/sh4/sh4_opcode_list.h"

#include "hw/sh4/sh4_mmr.h"
#include "hw/sh4/sh4_interrupts.h"
#include "hw/sh4/sh4_core.h"
#include "hw/sh4/dyna/ngen.h"
#include "hw/sh4/sh4_mem.h"
#include "hw/sh4/sh4_rom.h"
41
#include "hw/mem/vmem32.h"
Flyinghead's avatar
Flyinghead committed
42
43
44
45
#include "arm64_regalloc.h"

#undef do_sqw_nommu

46
47
48
49
extern "C" void ngen_blockcheckfail(u32 pc);
extern "C" void ngen_LinkBlock_Generic_stub();
extern "C" void ngen_LinkBlock_cond_Branch_stub();
extern "C" void ngen_LinkBlock_cond_Next_stub();
50
51
extern "C" void ngen_FailedToFindBlock_mmu();
extern "C" void ngen_FailedToFindBlock_nommu();
52
extern void vmem_platform_flush_cache(void *icache_start, void *icache_end, void *dcache_start, void *dcache_end);
53
static void generate_mainloop();
54

Libretro-Admin's avatar
Libretro-Admin committed
55
56
57
u32 mem_writes, mem_reads;
u32 mem_rewrites_w, mem_rewrites_r;

Flyinghead's avatar
Flyinghead committed
58
59
struct DynaRBI : RuntimeBlockInfo
{
60
	virtual u32 Relink() override;
Flyinghead's avatar
Flyinghead committed
61

62
	virtual void Relocate(void* dst) override {
Flyinghead's avatar
Flyinghead committed
63
64
65
66
		verify(false);
	}
};

Libretro-Admin's avatar
Libretro-Admin committed
67
68
double host_cpu_time;
u64 guest_cpu_cycles;
69
70
71
72
static jmp_buf jmp_env;
static u32 cycle_counter;

static void (*mainloop)(void *context);
73
static int (*arm64_intc_sched)();
74
75
static void (*arm64_no_update)();

Libretro-Admin's avatar
Libretro-Admin committed
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#ifdef PROFILING
#include <time.h>

static clock_t slice_start;
extern "C"
{
static __attribute((used)) void start_slice()
{
	slice_start = clock();
}
static __attribute((used)) void end_slice()
{
	host_cpu_time += (double)(clock() - slice_start) / CLOCKS_PER_SEC;
}
}
#endif

__asm__
(
		".hidden ngen_LinkBlock_cond_Branch_stub	\n\t"
		".globl ngen_LinkBlock_cond_Branch_stub		\n\t"
	"ngen_LinkBlock_cond_Branch_stub:		\n\t"
		"mov w1, #1							\n\t"
		"b ngen_LinkBlock_Shared_stub		\n"

		".hidden ngen_LinkBlock_cond_Next_stub	\n\t"
		".globl ngen_LinkBlock_cond_Next_stub	\n\t"
	"ngen_LinkBlock_cond_Next_stub:			\n\t"
		"mov w1, #0							\n\t"
		"b ngen_LinkBlock_Shared_stub		\n"

		".hidden ngen_LinkBlock_Generic_stub	\n\t"
		".globl ngen_LinkBlock_Generic_stub	\n\t"
	"ngen_LinkBlock_Generic_stub:			\n\t"
		"mov w1, w29						\n\t"	// djump/pc -> in case we need it ..
		//"b ngen_LinkBlock_Shared_stub		\n"

		".hidden ngen_LinkBlock_Shared_stub	\n\t"
		".globl ngen_LinkBlock_Shared_stub	\n\t"
	"ngen_LinkBlock_Shared_stub:			\n\t"
		"sub x0, lr, #4						\n\t"	// go before the call
		"bl rdv_LinkBlock					\n\t"   // returns an RX addr
		"br x0								\n"

		".hidden ngen_FailedToFindBlock_nommu	\n\t"
		".globl ngen_FailedToFindBlock_nommu	\n\t"
	"ngen_FailedToFindBlock_nommu:			\n\t"
		"mov w0, w29						\n\t"
		"bl rdv_FailedToFindBlock			\n\t"
		"br x0								\n"

		".hidden ngen_FailedToFindBlock_mmu	\n\t"
		".globl ngen_FailedToFindBlock_mmu	\n\t"
	"ngen_FailedToFindBlock_mmu:			\n\t"
		"bl rdv_FailedToFindBlock_pc		\n\t"
		"br x0								\n"

		".hidden ngen_blockcheckfail		\n\t"
		".globl ngen_blockcheckfail			\n\t"
	"ngen_blockcheckfail:					\n\t"
		"bl rdv_BlockCheckFail				\n\t"
		"br x0								\n"
);

140
static bool restarting;
Flyinghead's avatar
Flyinghead committed
141
142
143

void ngen_mainloop(void* v_cntx)
{
144
145
146
147
148
149
150
151
	do {
		restarting = false;
		generate_mainloop();

		mainloop(v_cntx);
		if (restarting)
			p_sh4rcb->cntx.CpuRunning = 1;
	} while (restarting);
Flyinghead's avatar
Flyinghead committed
152
153
}

Libretro-Admin's avatar
Libretro-Admin committed
154
void ngen_init()
Flyinghead's avatar
Flyinghead committed
155
{
Flyinghead's avatar
Flyinghead committed
156
	INFO_LOG(DYNAREC, "Initializing the ARM64 dynarec");
157
	ngen_FailedToFindBlock = &ngen_FailedToFindBlock_nommu;
Flyinghead's avatar
Flyinghead committed
158
159
}

Libretro-Admin's avatar
Libretro-Admin committed
160
void ngen_ResetBlocks()
Flyinghead's avatar
Flyinghead committed
161
{
162
163
164
165
166
167
168
169
170
171
172
173
174
	mainloop = NULL;
	if (mmu_enabled())
		ngen_FailedToFindBlock = &ngen_FailedToFindBlock_mmu;
	else
		ngen_FailedToFindBlock = &ngen_FailedToFindBlock_nommu;
	if (p_sh4rcb->cntx.CpuRunning)
	{
		// Force the dynarec out of mainloop() to regenerate it
		p_sh4rcb->cntx.CpuRunning = 0;
		restarting = true;
	}
}

Libretro-Admin's avatar
Libretro-Admin committed
175
176
177
178
179
180
void ngen_GetFeatures(ngen_features* dst)
{
	dst->InterpreterFallback = false;
	dst->OnlyDynamicEnds = false;
}

181
template<typename T>
182
static T ReadMemNoEx(u32 addr, u32, u32 pc)
183
184
185
186
187
188
{
#ifndef NO_MMU
	u32 ex;
	T rv = mmu_ReadMemNoEx<T>(addr, &ex);
	if (ex)
	{
189
		spc = pc;
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
		longjmp(jmp_env, 1);
	}
	return rv;
#else
	return (T)0;	// not used
#endif
}

template<typename T>
static void WriteMemNoEx(u32 addr, T data, u32 pc)
{
#ifndef NO_MMU
	u32 ex = mmu_WriteMemNoEx<T>(addr, data);
	if (ex)
	{
205
		spc = pc;
206
207
208
209
210
211
212
213
214
215
216
		longjmp(jmp_env, 1);
	}
#endif
}

static void interpreter_fallback(u16 op, OpCallFP *oph, u32 pc)
{
	try {
		oph(op);
	} catch (SH4ThrownException& ex) {
		if (pc & 1)
217
		{
218
219
220
			// Delay slot
			AdjustDelaySlotException(ex);
			pc--;
221
		}
222
223
224
225
226
227
228
229
230
231
232
		Do_Exception(pc, ex.expEvn, ex.callVect);
		longjmp(jmp_env, 1);
	}
}

static void do_sqw_mmu_no_ex(u32 addr, u32 pc)
{
	try {
		do_sqw_mmu(addr);
	} catch (SH4ThrownException& ex) {
		if (pc & 1)
233
		{
234
235
236
237
238
239
240
			// Delay slot
			AdjustDelaySlotException(ex);
			pc--;
		}
		Do_Exception(pc, ex.expEvn, ex.callVect);
		longjmp(jmp_env, 1);
	}
Flyinghead's avatar
Flyinghead committed
241
242
243
244
245
246
}

class Arm64Assembler : public MacroAssembler
{
	typedef void (MacroAssembler::*Arm64Op_RRO)(const Register&, const Register&, const Operand&);
	typedef void (MacroAssembler::*Arm64Op_RROF)(const Register&, const Register&, const Operand&, enum FlagsUpdate);
247
	typedef void (MacroAssembler::*Arm64Fop_RRR)(const VRegister&, const VRegister&, const VRegister&);
Flyinghead's avatar
Flyinghead committed
248
249

public:
250
251
252
	Arm64Assembler() : Arm64Assembler(emit_GetCCPtr())
	{
	}
253
	Arm64Assembler(void *buffer) : MacroAssembler((u8 *)buffer, emit_FreeSpace()), regalloc(this)
Flyinghead's avatar
Flyinghead committed
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
	{
		call_regs.push_back(&w0);
		call_regs.push_back(&w1);
		call_regs.push_back(&w2);
		call_regs.push_back(&w3);
		call_regs.push_back(&w4);
		call_regs.push_back(&w5);
		call_regs.push_back(&w6);
		call_regs.push_back(&w7);

		call_regs64.push_back(&x0);
		call_regs64.push_back(&x1);
		call_regs64.push_back(&x2);
		call_regs64.push_back(&x3);
		call_regs64.push_back(&x4);
		call_regs64.push_back(&x5);
		call_regs64.push_back(&x6);
		call_regs64.push_back(&x7);

		call_fregs.push_back(&s0);
		call_fregs.push_back(&s1);
		call_fregs.push_back(&s2);
		call_fregs.push_back(&s3);
		call_fregs.push_back(&s4);
		call_fregs.push_back(&s5);
		call_fregs.push_back(&s6);
		call_fregs.push_back(&s7);
	}

	void ngen_BinaryOp_RRO(shil_opcode* op, Arm64Op_RRO arm_op, Arm64Op_RROF arm_op2)
	{
		Operand op3 = Operand(0);
		if (op->rs2.is_imm())
		{
			op3 = Operand(op->rs2._imm);
		}
		else if (op->rs2.is_r32i())
		{
			op3 = Operand(regalloc.MapRegister(op->rs2));
		}
		if (arm_op != NULL)
			((*this).*arm_op)(regalloc.MapRegister(op->rd), regalloc.MapRegister(op->rs1), op3);
		else
			((*this).*arm_op2)(regalloc.MapRegister(op->rd), regalloc.MapRegister(op->rs1), op3, LeaveFlags);
	}

300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
	void ngen_BinaryFop(shil_opcode* op, Arm64Fop_RRR arm_op)
	{
		VRegister reg1;
		VRegister reg2;
		if (op->rs1.is_imm())
		{
			Fmov(s0, reinterpret_cast<f32&>(op->rs1._imm));
			reg1 = s0;
		}
		else
		{
			reg1 = regalloc.MapVRegister(op->rs1);
		}
		if (op->rs2.is_imm())
		{
			Fmov(s1, reinterpret_cast<f32&>(op->rs2._imm));
			reg2 = s1;
		}
		else
		{
			reg2 = regalloc.MapVRegister(op->rs2);
		}
		((*this).*arm_op)(regalloc.MapVRegister(op->rd), reg1, reg2);
	}

Flyinghead's avatar
Flyinghead committed
325
326
327
328
329
330
	const Register& GenMemAddr(const shil_opcode& op, const Register* raddr = NULL)
	{
		const Register* ret_reg = raddr == NULL ? &w0 : raddr;

		if (op.rs3.is_imm())
		{
331
			if (regalloc.IsAllocg(op.rs1))
332
				Add(*ret_reg, regalloc.MapRegister(op.rs1), op.rs3._imm);
333
334
335
336
			else
			{
				Ldr(*ret_reg, sh4_context_mem_operand(op.rs1.reg_ptr()));
				Add(*ret_reg, *ret_reg, op.rs3._imm);
337
			}
Flyinghead's avatar
Flyinghead committed
338
339
340
		}
		else if (op.rs3.is_r32i())
		{
341
			if (regalloc.IsAllocg(op.rs1) && regalloc.IsAllocg(op.rs3))
342
				Add(*ret_reg, regalloc.MapRegister(op.rs1), regalloc.MapRegister(op.rs3));
343
344
345
346
347
348
			else
			{
				Ldr(*ret_reg, sh4_context_mem_operand(op.rs1.reg_ptr()));
				Ldr(w8, sh4_context_mem_operand(op.rs3.reg_ptr()));
				Add(*ret_reg, *ret_reg, w8);
			}
Flyinghead's avatar
Flyinghead committed
349
350
351
352
353
		}
		else if (!op.rs3.is_null())
		{
			die("invalid rs3");
		}
354
355
356
		else if (op.rs1.is_reg())
		{
			if (regalloc.IsAllocg(op.rs1))
357
358
359
360
361
362
			{
				if (raddr == NULL)
					ret_reg = &regalloc.MapRegister(op.rs1);
				else
					Mov(*ret_reg, regalloc.MapRegister(op.rs1));
			}
363
364
365
366
367
368
369
370
371
372
			else
			{
				Ldr(*ret_reg, sh4_context_mem_operand(op.rs1.reg_ptr()));
			}
		}
		else
		{
			verify(op.rs1.is_imm());
			Mov(*ret_reg, op.rs1._imm);
		}
Flyinghead's avatar
Flyinghead committed
373
374
375
376
377
378
379

		return *ret_reg;
	}

	void ngen_Compile(RuntimeBlockInfo* block, bool force_checks, bool reset, bool staging, bool optimise)
	{
		//printf("REC-ARM64 compiling %08x\n", block->addr);
Libretro-Admin's avatar
Libretro-Admin committed
380
381
382
#ifdef PROFILING
		SaveFramePointer();
#endif
383
		this->block = block;
384
		CheckBlock(force_checks, block);
Libretro-Admin's avatar
Libretro-Admin committed
385
		
Flyinghead's avatar
Flyinghead committed
386
387
388
389
		// run register allocator
		regalloc.DoAlloc(block);

		// scheduler
390
391
392
393
394
395
396
397
398
		if (mmu_enabled())
		{
			Mov(x1, reinterpret_cast<uintptr_t>(&cycle_counter));
			Ldr(w0, MemOperand(x1));
			Subs(w0, w0, block->guest_cycles);
			Str(w0, MemOperand(x1));
		}
		else
		{
399
			Subs(w27, w27, block->guest_cycles);
400
		}
401
402
		Label cycles_remaining;
		B(&cycles_remaining, pl);
403
		GenCall(*arm64_intc_sched);
404
405
406
407
408
409
		Label cpu_running;
		Cbnz(w0, &cpu_running);
		Mov(w29, block->vaddr);
		Str(w29, sh4_context_mem_operand(&next_pc));
		GenBranch(*arm64_no_update);
		Bind(&cpu_running);
410
		Bind(&cycles_remaining);
Flyinghead's avatar
Flyinghead committed
411

Libretro-Admin's avatar
Libretro-Admin committed
412
413
414
415
416
417
#ifdef PROFILING
		Ldr(x11, (uintptr_t)&guest_cpu_cycles);
		Ldr(x0, MemOperand(x11));
		Add(x0, x0, block->guest_cycles);
		Str(x0, MemOperand(x11));
#endif
Flyinghead's avatar
Flyinghead committed
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
		for (size_t i = 0; i < block->oplist.size(); i++)
		{
			shil_opcode& op  = block->oplist[i];
			regalloc.OpBegin(&op, i);

			switch (op.op)
			{
			case shop_ifb:	// Interpreter fallback
				if (op.rs1._imm)	// if NeedPC()
				{
					Mov(w10, op.rs2._imm);
					Str(w10, sh4_context_mem_operand(&next_pc));
				}
				Mov(*call_regs[0], op.rs3._imm);

433
434
				if (!mmu_enabled())
				{
435
					GenCallRuntime(OpDesc[op.rs3._imm]->oph);
436
437
438
439
440
441
442
443
444
				}
				else
				{
					Mov(*call_regs64[1], reinterpret_cast<uintptr_t>(*OpDesc[op.rs3._imm]->oph));	// op handler
					Mov(*call_regs[2], block->vaddr + op.guest_offs - (op.delay_slot ? 1 : 0));	// pc

					GenCallRuntime(interpreter_fallback);
				}

Flyinghead's avatar
Flyinghead committed
445
446
447
448
				break;

			case shop_jcond:
			case shop_jdyn:
449
450
				{
					const Register rd = regalloc.MapRegister(op.rd);
451
					if (op.rs2.is_imm())
452
						Add(rd, regalloc.MapRegister(op.rs1), op.rs2._imm);
453
					else
454
						Mov(rd, regalloc.MapRegister(op.rs1));
455
					// Save it for the branching at the end of the block
456
457
					Mov(w29, rd);
				}
Flyinghead's avatar
Flyinghead committed
458
459
460
461
462
463
464
465
				break;

			case shop_mov32:
				verify(op.rd.is_reg());
				verify(op.rs1.is_reg() || op.rs1.is_imm());

				if (regalloc.IsAllocf(op.rd))
				{
466
					const VRegister rd = regalloc.MapVRegister(op.rd);
Flyinghead's avatar
Flyinghead committed
467
					if (op.rs1.is_imm())
468
						Fmov(rd, reinterpret_cast<f32&>(op.rs1._imm));
Flyinghead's avatar
Flyinghead committed
469
					else if (regalloc.IsAllocf(op.rs1))
470
						Fmov(rd, regalloc.MapVRegister(op.rs1));
Flyinghead's avatar
Flyinghead committed
471
					else
472
						Fmov(rd, regalloc.MapRegister(op.rs1));
Flyinghead's avatar
Flyinghead committed
473
474
475
				}
				else
				{
476
					const Register rd = regalloc.MapRegister(op.rd);
Flyinghead's avatar
Flyinghead committed
477
					if (op.rs1.is_imm())
478
						Mov(rd, op.rs1._imm);
Flyinghead's avatar
Flyinghead committed
479
					else if (regalloc.IsAllocg(op.rs1))
480
						Mov(rd, regalloc.MapRegister(op.rs1));
Flyinghead's avatar
Flyinghead committed
481
					else
482
						Fmov(rd, regalloc.MapVRegister(op.rs1));
Flyinghead's avatar
Flyinghead committed
483
484
485
486
487
488
489
490
491
492
493
494
				}
				break;

			case shop_mov64:
				verify(op.rd.is_reg());
				verify(op.rs1.is_reg() || op.rs1.is_imm());

				shil_param_to_host_reg(op.rs1, x15);
				host_reg_to_shil_param(op.rd, x15);
				break;

			case shop_readm:
495
				GenReadMemory(op, i, optimise);
496
				break;
Flyinghead's avatar
Flyinghead committed
497
498

			case shop_writem:
499
				GenWriteMemory(op, i, optimise);
500
				break;
Flyinghead's avatar
Flyinghead committed
501
502

			case shop_sync_sr:
503
				GenCallRuntime(UpdateSR);
Flyinghead's avatar
Flyinghead committed
504
505
				break;
			case shop_sync_fpscr:
506
				GenCallRuntime(UpdateFPSCR);
Flyinghead's avatar
Flyinghead committed
507
508
509
				break;

			case shop_swaplb:
510
511
512
513
514
515
516
				{
					const Register rs1 = regalloc.MapRegister(op.rs1);
					const Register rd = regalloc.MapRegister(op.rd);
					Mov(w9, Operand(rs1, LSR, 16));
					Rev16(rd, rs1);
					Bfi(rd, w9, 16, 16);
				}
Flyinghead's avatar
Flyinghead committed
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
				break;

			case shop_neg:
				Neg(regalloc.MapRegister(op.rd), regalloc.MapRegister(op.rs1));
				break;
			case shop_not:
				Mvn(regalloc.MapRegister(op.rd), regalloc.MapRegister(op.rs1));
				break;

			case shop_and:
				ngen_BinaryOp_RRO(&op, &MacroAssembler::And, NULL);
				break;
			case shop_or:
				ngen_BinaryOp_RRO(&op, &MacroAssembler::Orr, NULL);
				break;
			case shop_xor:
				ngen_BinaryOp_RRO(&op, &MacroAssembler::Eor, NULL);
				break;
			case shop_add:
				ngen_BinaryOp_RRO(&op, NULL, &MacroAssembler::Add);
				break;
			case shop_sub:
				ngen_BinaryOp_RRO(&op, NULL, &MacroAssembler::Sub);
				break;
			case shop_shl:
				if (op.rs2.is_imm())
					Lsl(regalloc.MapRegister(op.rd), regalloc.MapRegister(op.rs1), op.rs2._imm);
				else if (op.rs2.is_reg())
					Lsl(regalloc.MapRegister(op.rd), regalloc.MapRegister(op.rs1), regalloc.MapRegister(op.rs2));
				break;
			case shop_shr:
				if (op.rs2.is_imm())
					Lsr(regalloc.MapRegister(op.rd), regalloc.MapRegister(op.rs1), op.rs2._imm);
				else if (op.rs2.is_reg())
					Lsr(regalloc.MapRegister(op.rd), regalloc.MapRegister(op.rs1), regalloc.MapRegister(op.rs2));
				break;
			case shop_sar:
				if (op.rs2.is_imm())
					Asr(regalloc.MapRegister(op.rd), regalloc.MapRegister(op.rs1), op.rs2._imm);
				else if (op.rs2.is_reg())
					Asr(regalloc.MapRegister(op.rd), regalloc.MapRegister(op.rs1), regalloc.MapRegister(op.rs2));
				break;
			case shop_ror:
				if (op.rs2.is_imm())
					Ror(regalloc.MapRegister(op.rd), regalloc.MapRegister(op.rs1), op.rs2._imm);
				else if (op.rs2.is_reg())
					Ror(regalloc.MapRegister(op.rd), regalloc.MapRegister(op.rs1), regalloc.MapRegister(op.rs2));
				break;

			case shop_adc:
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
				{
					Register reg1;
					Operand op2;
					Register reg3;
					if (op.rs1.is_imm())
					{
						Mov(w0, op.rs1.imm_value());
						reg1 = w0;
					}
					else
					{
						reg1 = regalloc.MapRegister(op.rs1);
					}
					if (op.rs2.is_imm())
						op2 = Operand(op.rs2.imm_value());
					else
						op2 = regalloc.MapRegister(op.rs2);
					if (op.rs3.is_imm())
					{
						Mov(w1, op.rs3.imm_value());
						reg3 = w1;
					}
					else
					{
						reg3 = regalloc.MapRegister(op.rs3);
					}
					Cmp(reg3, 1);	// C = rs3
					Adcs(regalloc.MapRegister(op.rd), reg1, op2); // (C,rd)=rs1+rs2+rs3(C)
595
					Cset(regalloc.MapRegister(op.rd2), cs);	// rd2 = C
596
				}
Flyinghead's avatar
Flyinghead committed
597
598
				break;
			case shop_sbc:
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
				{
					Register reg1;
					Operand op2;
					Operand op3;
					if (op.rs1.is_imm())
					{
						Mov(w0, op.rs1.imm_value());
						reg1 = w0;
					}
					else
					{
						reg1 = regalloc.MapRegister(op.rs1);
					}
					if (op.rs2.is_imm())
						op2 = Operand(op.rs2.imm_value());
					else
						op2 = regalloc.MapRegister(op.rs2);
					if (op.rs3.is_imm())
						op3 = Operand(op.rs3.imm_value());
					else
						op3 = regalloc.MapRegister(op.rs3);
					Cmp(wzr, op3);	// C = ~rs3
					Sbcs(regalloc.MapRegister(op.rd), reg1, op2); // (C,rd) = rs1 - rs2 - ~rs3(C)
622
					Cset(regalloc.MapRegister(op.rd2), cc);	// rd2 = ~C
623
				}
Flyinghead's avatar
Flyinghead committed
624
				break;
625
			case shop_negc:
626
627
628
629
630
631
632
633
634
635
636
637
638
				{
					Operand op1;
					Operand op2;
					if (op.rs1.is_imm())
						op1 = Operand(op.rs1.imm_value());
					else
						op1 = regalloc.MapRegister(op.rs1);
					if (op.rs2.is_imm())
						op2 = Operand(op.rs2.imm_value());
					else
						op2 = regalloc.MapRegister(op.rs2);
					Cmp(wzr, op2);	// C = ~rs2
					Sbcs(regalloc.MapRegister(op.rd), wzr, op1);	// (C,rd) = 0 - rs1 - ~rs2(C)
639
					Cset(regalloc.MapRegister(op.rd2), cc);			// rd2 = ~C
640
				}
641
				break;
Flyinghead's avatar
Flyinghead committed
642
643

			case shop_rocr:
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
				{
					Register reg1;
					Register reg2;
					if (op.rs1.is_imm())
					{
						Mov(w1, op.rs1.imm_value());
						reg1 = w1;
					}
					else
					{
						reg1 = regalloc.MapRegister(op.rs1);
					}
					if (op.rs2.is_imm())
					{
						Mov(w2, op.rs2.imm_value());
						reg2 = w2;
					}
					else
					{
						reg2 = regalloc.MapRegister(op.rs2);
					}
					Ubfx(w0, reg1, 0, 1);										// w0 = rs1[0] (new C)
					const Register rd = regalloc.MapRegister(op.rd);
					Mov(rd, Operand(reg1, LSR, 1));	// rd = rs1 >> 1
					Bfi(rd, reg2, 31, 1);				// rd |= C << 31
669
					Mov(regalloc.MapRegister(op.rd2), w0);						// rd2 = w0 (new C)
670
				}
Flyinghead's avatar
Flyinghead committed
671
672
				break;
			case shop_rocl:
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
				{
					Register reg1;
					Register reg2;
					if (op.rs1.is_imm())
					{
						Mov(w0, op.rs1.imm_value());
						reg1 = w0;
					}
					else
					{
						reg1 = regalloc.MapRegister(op.rs1);
					}
					if (op.rs2.is_imm())
					{
						Mov(w1, op.rs2.imm_value());
						reg2 = w1;
					}
					else
					{
						reg2 = regalloc.MapRegister(op.rs2);
					}
					Tst(reg1, 0x80000000);						// Z = ~rs1[31]
					Orr(regalloc.MapRegister(op.rd), reg2, Operand(reg1, LSL, 1)); // rd = rs1 << 1 | rs2(C)
696
					Cset(regalloc.MapRegister(op.rd2), ne);		// rd2 = ~Z(C)
697
				}
Flyinghead's avatar
Flyinghead committed
698
699
700
701
				break;

			case shop_shld:
			case shop_shad:
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
				{
					Register reg1;
					if (op.rs1.is_imm())
					{
						Mov(w0, op.rs1.imm_value());
						reg1 = w0;
					}
					else
					{
						reg1 = regalloc.MapRegister(op.rs1);
					}
					Label positive_shift, negative_shift, end;
					const Register rs2 = regalloc.MapRegister(op.rs2);
					Tbz(rs2, 31, &positive_shift);
					Cmn(rs2, 32);
					B(&negative_shift, ne);
					const Register rd = regalloc.MapRegister(op.rd);
					// rs2 == -32 => rd = 0 (logical) or 0/-1 (arith)
					if (op.op == shop_shld)
						// Logical shift
						//Lsr(rd, reg1, 31);
						Mov(rd, wzr);
					else
						// Arithmetic shift
						Asr(rd, reg1, 31);
					B(&end);

					Bind(&positive_shift);
					// rs2 >= 0 => left shift
					Lsl(rd, reg1, rs2);
					B(&end);

					Bind(&negative_shift);
					// rs2 < 0 => right shift
					Neg(w1, rs2);
					if (op.op == shop_shld)
738
						// Logical shift
739
						Lsr(rd, reg1, w1);
740
741
					else
						// Arithmetic shift
742
743
744
						Asr(rd, reg1, w1);
					Bind(&end);
				}
Flyinghead's avatar
Flyinghead committed
745
746
747
748
749
750
751
752
753
				break;

			case shop_test:
			case shop_seteq:
			case shop_setge:
			case shop_setgt:
			case shop_setae:
			case shop_setab:
				{
754
					const Register rs1 = regalloc.MapRegister(op.rs1);
Flyinghead's avatar
Flyinghead committed
755
756
757
					if (op.op == shop_test)
					{
						if (op.rs2.is_imm())
758
							Tst(rs1, op.rs2._imm);
Flyinghead's avatar
Flyinghead committed
759
						else
760
							Tst(rs1, regalloc.MapRegister(op.rs2));
Flyinghead's avatar
Flyinghead committed
761
762
763
764
					}
					else
					{
						if (op.rs2.is_imm())
765
							Cmp(rs1, op.rs2._imm);
Flyinghead's avatar
Flyinghead committed
766
						else
767
							Cmp(rs1, regalloc.MapRegister(op.rs2));
Flyinghead's avatar
Flyinghead committed
768
769
770
771
772
773
774
775
					}

					static const Condition shop_conditions[] = { eq, eq, ge, gt, hs, hi };

					Cset(regalloc.MapRegister(op.rd), shop_conditions[op.op - shop_test]);
				}
				break;
			case shop_setpeq:
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
				{
					Register reg1;
					Register reg2;
					if (op.rs1.is_imm())
					{
						Mov(w0, op.rs1.imm_value());
						reg1 = w0;
					}
					else
					{
						reg1 = regalloc.MapRegister(op.rs1);
					}
					if (op.rs2.is_imm())
					{
						Mov(w1, op.rs2.imm_value());
						reg2 = w1;
					}
					else
					{
						reg2 = regalloc.MapRegister(op.rs2);
					}
					Eor(w1, reg1, reg2);
					const Register rd = regalloc.MapRegister(op.rd);
					Mov(rd, wzr);
800
801
					Mov(w2, wzr);	// wzr not supported by csinc (?!)
					Tst(w1, 0xFF000000);
802
					Csinc(rd, rd, w2, ne);
803
					Tst(w1, 0x00FF0000);
804
					Csinc(rd, rd, w2, ne);
805
					Tst(w1, 0x0000FF00);
806
					Csinc(rd, rd, w2, ne);
807
					Tst(w1, 0x000000FF);
808
809
					Csinc(rd, rd, w2, ne);
				}
Flyinghead's avatar
Flyinghead committed
810
811
812
				break;

			case shop_mul_u16:
813
814
815
816
817
818
819
820
821
822
823
				{
					Register reg2;
					if (op.rs2.is_imm())
					{
						Mov(w0, op.rs2.imm_value());
						reg2 = w0;
					}
					else
					{
						reg2 = regalloc.MapRegister(op.rs2);
					}
824
					Uxth(w10, regalloc.MapRegister(op.rs1));
825
					Uxth(w11, reg2);
826
					Mul(regalloc.MapRegister(op.rd), w10, w11);
827
				}
Flyinghead's avatar
Flyinghead committed
828
829
				break;
			case shop_mul_s16:
830
831
832
833
834
835
836
837
838
839
840
				{
					Register reg2;
					if (op.rs2.is_imm())
					{
						Mov(w0, op.rs2.imm_value());
						reg2 = w0;
					}
					else
					{
						reg2 = regalloc.MapRegister(op.rs2);
					}
841
					Sxth(w10, regalloc.MapRegister(op.rs1));
842
					Sxth(w11, reg2);
843
					Mul(regalloc.MapRegister(op.rd), w10, w11);
844
				}
Flyinghead's avatar
Flyinghead committed
845
846
				break;
			case shop_mul_i32:
847
848
849
850
851
852
853
854
855
856
857
858
859
				{
					Register reg2;
					if (op.rs2.is_imm())
					{
						Mov(w0, op.rs2.imm_value());
						reg2 = w0;
					}
					else
					{
						reg2 = regalloc.MapRegister(op.rs2);
					}
					Mul(regalloc.MapRegister(op.rd), regalloc.MapRegister(op.rs1), reg2);
				}
Flyinghead's avatar
Flyinghead committed
860
861
862
863
				break;
			case shop_mul_u64:
			case shop_mul_s64:
				{
864
865
866
867
868
869
870
871
872
873
					Register reg2;
					if (op.rs2.is_imm())
					{
						Mov(w0, op.rs2.imm_value());
						reg2 = w0;
					}
					else
					{
						reg2 = regalloc.MapRegister(op.rs2);
					}
Flyinghead's avatar
Flyinghead committed
874
875
					const Register& rd_xreg = Register::GetXRegFromCode(regalloc.MapRegister(op.rd).GetCode());
					if (op.op == shop_mul_u64)
876
						Umull(rd_xreg, regalloc.MapRegister(op.rs1), reg2);
Flyinghead's avatar
Flyinghead committed
877
					else
878
						Smull(rd_xreg, regalloc.MapRegister(op.rs1), reg2);
Flyinghead's avatar
Flyinghead committed
879
880
881
882
883
884
885
					const Register& rd2_xreg = Register::GetXRegFromCode(regalloc.MapRegister(op.rd2).GetCode());
					Lsr(rd2_xreg, rd_xreg, 32);
				}
				break;

			case shop_pref:
				{
886
887
888
889
890
891
					Label not_sqw;
					if (op.rs1.is_imm())
						Mov(*call_regs[0], op.rs1._imm);
					else
					{
						if (regalloc.IsAllocg(op.rs1))
892
							Lsr(w1, regalloc.MapRegister(op.rs1), 26);
893
894
895
896
897
						else
						{
							Ldr(w0, sh4_context_mem_operand(op.rs1.reg_ptr()));
							Lsr(w1, w0, 26);
						}
898
899
						Cmp(w1, 0x38);
						B(&not_sqw, ne);
900
						if (regalloc.IsAllocg(op.rs1))
901
							Mov(w0, regalloc.MapRegister(op.rs1));
902
903
904
905
906
					}

					if (mmu_enabled())
					{
						Mov(*call_regs[1], block->vaddr + op.guest_offs - (op.delay_slot ? 1 : 0));	// pc
Flyinghead's avatar
Flyinghead committed
907

908
909
910
911
						GenCallRuntime(do_sqw_mmu_no_ex);
					}
					else
					{
912
913
914
915
916
917
918
919
920
921
922
						if (CCN_MMUCR.AT)
						{
							Ldr(x9, reinterpret_cast<uintptr_t>(&do_sqw_mmu));
						}
						else
						{
							Sub(x9, x28, offsetof(Sh4RCB, cntx) - offsetof(Sh4RCB, do_sqw_nommu));
							Ldr(x9, MemOperand(x9));
							Sub(x1, x28, offsetof(Sh4RCB, cntx) - offsetof(Sh4RCB, sq_buffer));
						}
						Blr(x9);
923
					}
924
					Bind(&not_sqw);
Flyinghead's avatar
Flyinghead committed
925
926
927
928
929
930
931
932
933
934
				}
				break;

			case shop_ext_s8:
				Sxtb(regalloc.MapRegister(op.rd), regalloc.MapRegister(op.rs1));
				break;
			case shop_ext_s16:
				Sxth(regalloc.MapRegister(op.rd), regalloc.MapRegister(op.rs1));
				break;

935
			case shop_xtrct:
936
937
				{
					const Register rd = regalloc.MapRegister(op.rd);
938
939
940
941
942
943
944
945
946
947
948
949
950
					const Register rs1 = regalloc.MapRegister(op.rs1);
					const Register rs2 = regalloc.MapRegister(op.rs2);
					if (op.rs1._reg == op.rd._reg)
					{
						verify(op.rs2._reg != op.rd._reg);
						Lsr(rd, rs1, 16);
						Lsl(w0, rs2, 16);
					}
					else
					{
						Lsl(rd, rs2, 16);
						Lsr(w0, rs1, 16);
					}
951
952
					Orr(rd, rd, w0);
				}
953
954
				break;

Flyinghead's avatar
Flyinghead committed
955
956
957
958
959
			//
			// FPU
			//

			case shop_fadd:
960
				ngen_BinaryFop(&op, &MacroAssembler::Fadd);
Flyinghead's avatar
Flyinghead committed
961
962
				break;
			case shop_fsub:
963
				ngen_BinaryFop(&op, &MacroAssembler::Fsub);
Flyinghead's avatar
Flyinghead committed
964
965
				break;
			case shop_fmul:
966
				ngen_BinaryFop(&op, &MacroAssembler::Fmul);
Flyinghead's avatar
Flyinghead committed
967
968
				break;
			case shop_fdiv:
969
				ngen_BinaryFop(&op, &MacroAssembler::Fdiv);
Flyinghead's avatar
Flyinghead committed
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
				break;

			case shop_fabs:
				Fabs(regalloc.MapVRegister(op.rd), regalloc.MapVRegister(op.rs1));
				break;
			case shop_fneg:
				Fneg(regalloc.MapVRegister(op.rd), regalloc.MapVRegister(op.rs1));
				break;
			case shop_fsqrt:
				Fsqrt(regalloc.MapVRegister(op.rd), regalloc.MapVRegister(op.rs1));
				break;

			case shop_fmac:
				Fmadd(regalloc.MapVRegister(op.rd), regalloc.MapVRegister(op.rs3), regalloc.MapVRegister(op.rs2), regalloc.MapVRegister(op.rs1));
				break;

			case shop_fsrra:
				Fsqrt(s0, regalloc.MapVRegister(op.rs1));
				Fmov(s1, 1.f);
				Fdiv(regalloc.MapVRegister(op.rd), s1, s0);
				break;

			case shop_fsetgt:
			case shop_fseteq:
				Fcmp(regalloc.MapVRegister(op.rs1), regalloc.MapVRegister(op.rs2));
				Cset(regalloc.MapRegister(op.rd), op.op == shop_fsetgt ? gt : eq);
				break;

			case shop_fsca:
				Mov(x1, reinterpret_cast<uintptr_t>(&sin_table));
1000
1001
1002
1003
				if (op.rs1.is_reg())
					Add(x1, x1, Operand(regalloc.MapRegister(op.rs1), UXTH, 3));
				else
					Add(x1, x1, Operand(op.rs1.imm_value() << 3));
Flyinghead's avatar
Flyinghead committed
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
				Ldr(x2, MemOperand(x1));
				Str(x2, sh4_context_mem_operand(op.rd.reg_ptr()));
				break;

			case shop_fipr:
				Add(x9, x28, sh4_context_mem_operand(op.rs1.reg_ptr()).GetOffset());
				Ld1(v0.V4S(), MemOperand(x9));
				if (op.rs1._reg != op.rs2._reg)
				{
					Add(x9, x28, sh4_context_mem_operand(op.rs2.reg_ptr()).GetOffset());
					Ld1(v1.V4S(), MemOperand(x9));
					Fmul(v0.V4S(), v0.V4S(), v1.V4S());
				}
				else
					Fmul(v0.V4S(), v0.V4S(), v0.V4S());
				Faddp(v1.V4S(), v0.V4S(), v0.V4S());
				Faddp(regalloc.MapVRegister(op.rd), v1.V2S());
				break;

			case shop_ftrv:
				Add(x9, x28, sh4_context_mem_operand(op.rs1.reg_ptr()).GetOffset());
				Ld1(v0.V4S(), MemOperand(x9));
				Add(x9, x28, sh4_context_mem_operand(op.rs2.reg_ptr()).GetOffset());
				Ld1(v1.V4S(), MemOperand(x9, 16, PostIndex));
				Ld1(v2.V4S(), MemOperand(x9, 16, PostIndex));
				Ld1(v3.V4S(), MemOperand(x9, 16, PostIndex));
				Ld1(v4.V4S(), MemOperand(x9, 16, PostIndex));
				Fmul(v5.V4S(), v1.V4S(), s0, 0);
				Fmla(v5.V4S(), v2.V4S(), s0, 1);
				Fmla(v5.V4S(), v3.V4S(), s0, 2);
				Fmla(v5.V4S(), v4.V4S(), s0, 3);
				Add(x9, x28, sh4_context_mem_operand(op.rd.reg_ptr()).GetOffset());
				St1(v5.V4S(), MemOperand(x9));
				break;

			case shop_frswap:
				Add(x9, x28, sh4_context_mem_operand(op.rs1.reg_ptr()).GetOffset());
				Add(x10, x28, sh4_context_mem_operand(op.rd.reg_ptr()).GetOffset());
				Ld4(v0.V2D(), v1.V2D(), v2.V2D(), v3.V2D(), MemOperand(x9));
				Ld4(v4.V2D(), v5.V2D(), v6.V2D(), v7.V2D(), MemOperand(x10));
				St4(v4.V2D(), v5.V2D(), v6.V2D(), v7.V2D(), MemOperand(x9));
				St4(v0.V2D(), v1.V2D(), v2.V2D(), v3.V2D(), MemOperand(x10));
				break;

			case shop_cvt_f2i_t:
				Fcvtzs(regalloc.MapRegister(op.rd), regalloc.MapVRegister(op.rs1));
				break;
			case shop_cvt_i2f_n:
			case shop_cvt_i2f_z:
				Scvtf(regalloc.MapVRegister(op.rd), regalloc.MapRegister(op.rs1));
				break;

			default:
				shil_chf[op.op](&op);
				break;
			}
			regalloc.OpEnd(&op);
		}
1062
		regalloc.Cleanup();
Flyinghead's avatar
Flyinghead committed
1063

1064
1065
		block->relink_offset = (u32)GetBuffer()->GetCursorOffset();
		block->relink_data = 0;
Flyinghead's avatar
Flyinghead committed
1066

1067
		RelinkBlock(block);
Flyinghead's avatar
Flyinghead committed
1068

1069
		Finalize();
Flyinghead's avatar
Flyinghead committed
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
	}

	void ngen_CC_Start(shil_opcode* op)
	{
		CC_pars.clear();
	}

	void ngen_CC_Param(shil_opcode& op, shil_param& prm, CanonicalParamType tp)
	{
		switch (tp)
		{

		case CPT_u32:
		case CPT_ptr:
		case CPT_f32:
		{
			CC_PS t = { tp, &prm };
			CC_pars.push_back(t);
		}
		break;

		case CPT_u64rvL:
		case CPT_u32rv:
			host_reg_to_shil_param(prm, w0);
			break;

		case CPT_u64rvH:
			Lsr(x10, x0, 32);
			host_reg_to_shil_param(prm, w10);
			break;

		case CPT_f32rv:
			host_reg_to_shil_param(prm, s0);
			break;
		}
	}

	void ngen_CC_Call(shil_opcode*op, void* function)
	{
		int regused = 0;
		int fregused = 0;

		// Args are pushed in reverse order by shil_canonical
		for (int i = CC_pars.size(); i-- > 0;)
		{
			verify(fregused < call_fregs.size() && regused < call_regs.size());
			shil_param& prm = *CC_pars[i].prm;
			switch (CC_pars[i].type)
			{
			// push the params

			case CPT_u32:
				shil_param_to_host_reg(prm, *call_regs[regused++]);

				break;

			case CPT_f32:
1127
				if (prm.is_reg())
Flyinghead's avatar
Flyinghead committed
1128
					Fmov(*call_fregs[fregused], regalloc.MapVRegister(prm));
1129
1130
1131
				else if (prm.is_imm())
					Fmov(*call_fregs[fregused], reinterpret_cast<f32&>(prm._imm));
				else
Flyinghead's avatar
Flyinghead committed
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
					verify(prm.is_null());
				fregused++;
				break;

			case CPT_ptr:
				verify(prm.is_reg());
				// push the ptr itself
				Mov(*call_regs64[regused++], reinterpret_cast<uintptr_t>(prm.reg_ptr()));

				break;
			case CPT_u32rv:
			case CPT_u64rvL:
			case CPT_u64rvH:
			case CPT_f32rv:
				// return values are handled in ngen_CC_param()
				break;
			}
		}
1150
		GenCallRuntime((void (*)())function);
Flyinghead's avatar
Flyinghead committed
1151
1152
1153
1154
1155
1156
1157
1158
1159
	}

	MemOperand sh4_context_mem_operand(void *p)
	{
		u32 offset = (u8*)p - (u8*)&p_sh4rcb->cntx;
		verify((offset & 3) == 0 && offset <= 16380);	// FIXME 64-bit regs need multiple of 8 up to 32760
		return MemOperand(x28, offset);
	}

1160
	void GenReadMemorySlow(u32 size)
1161
1162
	{
		Instruction *start_instruction = GetCursorAddress<Instruction *>();
1163

1164
1165
1166
		switch (size)
		{
		case 1:
1167
			if (!mmu_enabled())
1168
				GenCallRuntime(ReadMem8);
1169
1170
			else
				GenCallRuntime(ReadMemNoEx<u8>);
1171
1172
1173
1174
			Sxtb(w0, w0);
			break;

		case 2:
1175
			if (!mmu_enabled())
1176
				GenCallRuntime(ReadMem16);
1177
1178
			else
				GenCallRuntime(ReadMemNoEx<u16>);
1179
1180
1181
1182
			Sxth(w0, w0);
			break;

		case 4:
1183
			if (!mmu_enabled())
1184
				GenCallRuntime(ReadMem32);
1185
1186
			else
				GenCallRuntime(ReadMemNoEx<u32>);
1187
1188
1189
			break;

		case 8:
1190
			if (!mmu_enabled())
1191
				GenCallRuntime(ReadMem64);
1192
1193
			else
				GenCallRuntime(ReadMemNoEx<u64>);
1194
1195
1196
1197
1198
1199
1200
1201
1202
			break;

		default:
			die("1..8 bytes");
			break;
		}
		EnsureCodeSize(start_instruction, read_memory_rewrite_size);
	}

1203
	void GenWriteMemorySlow(u32 size)
1204
1205
	{
		Instruction *start_instruction = GetCursorAddress<Instruction *>();
1206

1207
1208
1209
		switch (size)
		{
		case 1:
1210
			if (!mmu_enabled())
1211
				GenCallRuntime(WriteMem8);
1212
1213
			else
				GenCallRuntime(WriteMemNoEx<u8>);
1214
1215
1216
			break;

		case 2:
1217
			if (!mmu_enabled())
1218
				GenCallRuntime(WriteMem16);
1219
1220
			else
				GenCallRuntime(WriteMemNoEx<u16>);
1221
1222
1223
			break;

		case 4:
1224
			if (!mmu_enabled())
1225
				GenCallRuntime(WriteMem32);
1226
1227
			else
				GenCallRuntime(WriteMemNoEx<u32>);
1228
1229
1230
			break;

		case 8:
1231
			if (!mmu_enabled())
1232
				GenCallRuntime(WriteMem64);
1233
1234
			else
				GenCallRuntime(WriteMemNoEx<u64>);
1235
1236
1237
1238
1239
1240
1241
1242
1243
			break;

		default:
			die("1..8 bytes");
			break;
		}
		EnsureCodeSize(start_instruction, write_memory_rewrite_size);
	}

1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
	u32 RelinkBlock(RuntimeBlockInfo *block)
	{
		ptrdiff_t start_offset = GetBuffer()->GetCursorOffset();

		switch (block->BlockType)
		{

		case BET_StaticJump:
		case BET_StaticCall:
			// next_pc = block->BranchBlock;
Libretro-Admin's avatar
Libretro-Admin committed
1254
1255
1256
1257
#ifndef NO_BLOCK_LINKING
			if (block->pBranchBlock != NULL)
				GenBranch(block->pBranchBlock->code);
			else
1258
1259
			{
				if (!mmu_enabled())
1260
1261
					GenCallRuntime(ngen_LinkBlock_Generic_stub);
				else
Libretro-Admin's avatar
Libretro-Admin committed
1262
1263
1264
#else
			{
#endif
1265
1266
1267
1268
1269
1270
				{
					Mov(w29, block->BranchBlock);
					Str(w29, sh4_context_mem_operand(&next_pc));
					GenBranch(*arm64_no_update);
				}
			}
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
			break;

		case BET_Cond_0:
		case BET_Cond_1:
			{
				// next_pc = next_pc_value;
				// if (*jdyn == 0)
				//   next_pc = branch_pc_value;

				if (block->has_jcond)
					Ldr(w11, sh4_context_mem_operand(&Sh4cntx.jdyn));
				else
					Ldr(w11, sh4_context_mem_operand(&sr.T));

				Cmp(w11, block->BlockType & 1);

				Label branch_not_taken;

				B(ne, &branch_not_taken);
Libretro-Admin's avatar
Libretro-Admin committed
1290
#ifndef NO_BLOCK_LINKING
1291
1292
1293
				if (block->pBranchBlock != NULL)
					GenBranch(block->pBranchBlock->code);
				else
1294
1295
				{
					if (!mmu_enabled())
1296
						GenCallRuntime(ngen_LinkBlock_cond_Branch_stub);
1297
					else
Libretro-Admin's avatar
Libretro-Admin committed
1298
1299
1300
#else
				{
#endif
1301
1302
1303
1304
1305
1306
					{
						Mov(w29, block->BranchBlock);
						Str(w29, sh4_context_mem_operand(&next_pc));
						GenBranch(*arm64_no_update);
					}
				}
1307
1308
1309

				Bind(&branch_not_taken);

Libretro-Admin's avatar
Libretro-Admin committed
1310
#ifndef NO_BLOCK_LINKING
1311
1312
1313
				if (block->pNextBlock != NULL)
					GenBranch(block->pNextBlock->code);
				else
1314
1315
				{
					if (!mmu_enabled())
1316
						GenCallRuntime(ngen_LinkBlock_cond_Next_stub);
1317
					else
Libretro-Admin's avatar
Libretro-Admin committed
1318
1319
1320
#else
				{
#endif
1321
1322
1323
1324
					{
						Mov(w29, block->NextBlock);
						Str(w29, sh4_context_mem_operand(&next_pc));
						GenBranch(*arm64_no_update);
1325
					}
1326
				}
1327
1328
1329
1330
1331
1332
1333
1334
1335
			}
			break;

		case BET_DynamicJump:
		case BET_DynamicCall:
		case BET_DynamicRet:
			// next_pc = *jdyn;

			Str(w29, sh4_context_mem_operand(&next_pc));
1336
1337
			if (!mmu_enabled())
			{
1338
1339
1340
1341
				// TODO Call no_update instead (and check CpuRunning less frequently?)
				Mov(x2, sizeof(Sh4RCB));
				Sub(x2, x28, x2);
				Add(x2, x2, sizeof(Sh4Context));		// x2 now points to FPCB
1342
#if RAM_SIZE_MAX == 33554432
1343
				Ubfx(w1, w29, 1, 24);
1344
1345
1346
#else
				Ubfx(w1, w29, 1, 23);
#endif
1347
1348
				Ldr(x15, MemOperand(x2, x1, LSL, 3));	// Get block entry point
				Br(x15);
1349
1350
1351
1352
1353
			}
			else
			{
				GenBranch(*arm64_no_update);
			}
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368

			break;

		case BET_DynamicIntr:
		case BET_StaticIntr:
			if (block->BlockType == BET_StaticIntr)
				// next_pc = next_pc_value;
				Mov(w29, block->NextBlock);
			// else next_pc = *jdyn (already in w29)

			Str(w29, sh4_context_mem_operand(&next_pc));

			GenCallRuntime(UpdateINTC);

			Ldr(w29, sh4_context_mem_operand(&next_pc));
1369
			GenBranch(*arm64_no_update);
1370
1371
1372
1373
1374
1375
1376
1377

			break;

		default:
			die("Invalid block end type");
		}

		return GetBuffer()->GetCursorOffset() - start_offset;
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
	}

	void Finalize(bool rewrite = false)
	{
		Label code_end;
		Bind(&code_end);

		FinalizeCode();

		if (!rewrite)
		{
			block->code = GetBuffer()->GetStartAddress<DynarecCodeEntryPtr>();
			block->host_code_size = GetBuffer()->GetSizeInBytes();
			block->host_opcodes = GetLabelAddress<u32*>(&code_end) - GetBuffer()->GetStartAddress<u32*>();

			emit_Skip(block->host_code_size);
		}
1395

1396
1397
1398
1399
		// Flush and invalidate caches
		vmem_platform_flush_cache(
			CC_RW2RX(GetBuffer()->GetStartAddress<void*>()), CC_RW2RX(GetBuffer()->GetEndAddress<void*>()),
			GetBuffer()->GetStartAddress<void*>(), GetBuffer()->GetEndAddress<void*>());
1400
#if 0
1401
		if (rewrite && block != NULL)
1402
		{
Flyinghead's avatar
Flyinghead committed
1403
			INFO_LOG(DYNAREC, "BLOCK %08x", block->vaddr);
1404
1405
1406
			Instruction* instr_start = (Instruction*)block->code;
//			Instruction* instr_end = GetLabelAddress<Instruction*>(&code_end);
			Instruction* instr_end = (Instruction*)((u8 *)block->code + block->host_code_size);
1407
1408
1409
1410
1411
1412
			Decoder decoder;
			Disassembler disasm;
			decoder.AppendVisitor(&disasm);
			Instruction* instr;
			for (instr = instr_start; instr < instr_end; instr += kInstructionSize) {
				decoder.Decode(instr);
Flyinghead's avatar
Flyinghead committed
1413
				INFO_LOG(DYNAREC, "VIXL  %p:  %s",
1414
1415
1416
1417
1418
1419
1420
						   reinterpret_cast<void*>(instr),
						   disasm.GetOutput());
			}
		}
#endif
	}

1421
1422
1423
1424
1425
1426
	void GenMainloop()
	{
		Label no_update;
		Label intc_sched;
		Label end_mainloop;

1427
1428
		// int intc_sched()
		arm64_intc_sched = GetCursorAddress<int (*)()>();
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
		B(&intc_sched);

		// void no_update()
		Bind(&no_update);				// next_pc _MUST_ be on w29

		Ldr(w0, MemOperand(x28, offsetof(Sh4Context, CpuRunning)));
		Cbz(w0, &end_mainloop);
		if (!mmu_enabled())
		{
			Sub(x2, x28, offsetof(Sh4RCB, cntx));
			if (RAM_SIZE == 32 * 1024 * 1024)
				Ubfx(w1, w29, 1, 24);	// 24+1 bits: 32 MB
			else if (RAM_SIZE == 16 * 1024 * 1024)
				Ubfx(w1, w29, 1, 23);	// 23+1 bits: 16 MB
			else
				die("Unsupported RAM_SIZE");
			Ldr(x0, MemOperand(x2, x1, LSL, 3));
		}
		else
		{
			Mov(w0, w29);
			GenCallRuntime(bm_GetCodeByVAddr);
		}
		Br(x0);

		// void mainloop(void *context)
Libretro-Admin's avatar
Libretro-Admin committed
1455
		mainloop = (void (*)(void *))CC_RW2RX(GetCursorAddress<uintptr_t>());