rec_arm64.cpp 52.3 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;

31
//#define EXPLODE_SPANS
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

Flyinghead's avatar
Flyinghead committed
55
56
struct DynaRBI : RuntimeBlockInfo
{
57
	virtual u32 Relink() override;
Flyinghead's avatar
Flyinghead committed
58

59
	virtual void Relocate(void* dst) override {
Flyinghead's avatar
Flyinghead committed
60
61
62
63
		verify(false);
	}
};

64
65
66
67
static jmp_buf jmp_env;
static u32 cycle_counter;

static void (*mainloop)(void *context);
68
static int (*arm64_intc_sched)();
69
70
static void (*arm64_no_update)();

71
void(*ngen_FailedToFindBlock)();
72
static bool restarting;
Flyinghead's avatar
Flyinghead committed
73
74
75

void ngen_mainloop(void* v_cntx)
{
76
77
78
79
80
81
82
83
	do {
		restarting = false;
		generate_mainloop();

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

86
void ngen_init()
Flyinghead's avatar
Flyinghead committed
87
{
Flyinghead's avatar
Flyinghead committed
88
	INFO_LOG(DYNAREC, "Initializing the ARM64 dynarec");
89
	ngen_FailedToFindBlock = &ngen_FailedToFindBlock_nommu;
Flyinghead's avatar
Flyinghead committed
90
91
}

92
void ngen_ResetBlocks()
Flyinghead's avatar
Flyinghead committed
93
{
94
95
96
97
98
99
100
101
102
103
104
105
106
107
	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;
	}
}

template<typename T>
108
static T ReadMemNoEx(u32 addr, u32, u32 pc)
109
110
111
112
113
114
{
#ifndef NO_MMU
	u32 ex;
	T rv = mmu_ReadMemNoEx<T>(addr, &ex);
	if (ex)
	{
115
		spc = pc;
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
		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)
	{
131
		spc = pc;
132
133
134
135
136
137
138
139
140
141
142
		longjmp(jmp_env, 1);
	}
#endif
}

static void interpreter_fallback(u16 op, OpCallFP *oph, u32 pc)
{
	try {
		oph(op);
	} catch (SH4ThrownException& ex) {
		if (pc & 1)
143
		{
144
145
146
			// Delay slot
			AdjustDelaySlotException(ex);
			pc--;
147
		}
148
149
150
151
152
153
154
155
156
157
158
		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)
159
		{
160
161
162
163
164
165
166
			// Delay slot
			AdjustDelaySlotException(ex);
			pc--;
		}
		Do_Exception(pc, ex.expEvn, ex.callVect);
		longjmp(jmp_env, 1);
	}
Flyinghead's avatar
Flyinghead committed
167
168
169
170
171
172
}

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);
173
	typedef void (MacroAssembler::*Arm64Fop_RRR)(const VRegister&, const VRegister&, const VRegister&);
Flyinghead's avatar
Flyinghead committed
174
175

public:
176
177
178
	Arm64Assembler() : Arm64Assembler(emit_GetCCPtr())
	{
	}
179
	Arm64Assembler(void *buffer) : MacroAssembler((u8 *)buffer, emit_FreeSpace()), regalloc(this)
Flyinghead's avatar
Flyinghead committed
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
	{
		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);
	}

226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
	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
251
252
253
254
255
256
	const Register& GenMemAddr(const shil_opcode& op, const Register* raddr = NULL)
	{
		const Register* ret_reg = raddr == NULL ? &w0 : raddr;

		if (op.rs3.is_imm())
		{
257
			if (regalloc.IsAllocg(op.rs1))
258
				Add(*ret_reg, regalloc.MapRegister(op.rs1), op.rs3._imm);
259
260
261
262
			else
			{
				Ldr(*ret_reg, sh4_context_mem_operand(op.rs1.reg_ptr()));
				Add(*ret_reg, *ret_reg, op.rs3._imm);
263
			}
Flyinghead's avatar
Flyinghead committed
264
265
266
		}
		else if (op.rs3.is_r32i())
		{
267
			if (regalloc.IsAllocg(op.rs1) && regalloc.IsAllocg(op.rs3))
268
				Add(*ret_reg, regalloc.MapRegister(op.rs1), regalloc.MapRegister(op.rs3));
269
270
271
272
273
274
			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
275
276
277
278
279
		}
		else if (!op.rs3.is_null())
		{
			die("invalid rs3");
		}
280
281
282
		else if (op.rs1.is_reg())
		{
			if (regalloc.IsAllocg(op.rs1))
283
284
285
286
287
288
			{
				if (raddr == NULL)
					ret_reg = &regalloc.MapRegister(op.rs1);
				else
					Mov(*ret_reg, regalloc.MapRegister(op.rs1));
			}
289
290
291
292
293
294
295
296
297
298
			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
299
300
301
302
303
304
305

		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);
306
		this->block = block;
307
		CheckBlock(force_checks, block);
308

Flyinghead's avatar
Flyinghead committed
309
310
311
312
		// run register allocator
		regalloc.DoAlloc(block);

		// scheduler
313
314
315
316
317
318
319
320
321
		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
		{
322
			Subs(w27, w27, block->guest_cycles);
323
		}
324
325
		Label cycles_remaining;
		B(&cycles_remaining, pl);
326
		GenCall(*arm64_intc_sched);
327
328
329
330
331
332
		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);
333
		Bind(&cycles_remaining);
Flyinghead's avatar
Flyinghead committed
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349

		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);

350
351
				if (!mmu_enabled())
				{
352
					GenCallRuntime(OpDesc[op.rs3._imm]->oph);
353
354
355
356
357
358
359
360
361
				}
				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
362
363
364
365
				break;

			case shop_jcond:
			case shop_jdyn:
366
367
				{
					const Register rd = regalloc.MapRegister(op.rd);
368
					if (op.rs2.is_imm())
369
						Add(rd, regalloc.MapRegister(op.rs1), op.rs2._imm);
370
					else
371
						Mov(rd, regalloc.MapRegister(op.rs1));
372
					// Save it for the branching at the end of the block
373
374
					Mov(w29, rd);
				}
Flyinghead's avatar
Flyinghead committed
375
376
377
378
379
380
381
382
				break;

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

				if (regalloc.IsAllocf(op.rd))
				{
383
					const VRegister rd = regalloc.MapVRegister(op.rd);
Flyinghead's avatar
Flyinghead committed
384
					if (op.rs1.is_imm())
385
						Fmov(rd, reinterpret_cast<f32&>(op.rs1._imm));
Flyinghead's avatar
Flyinghead committed
386
					else if (regalloc.IsAllocf(op.rs1))
387
						Fmov(rd, regalloc.MapVRegister(op.rs1));
Flyinghead's avatar
Flyinghead committed
388
					else
389
						Fmov(rd, regalloc.MapRegister(op.rs1));
Flyinghead's avatar
Flyinghead committed
390
391
392
				}
				else
				{
393
					const Register rd = regalloc.MapRegister(op.rd);
Flyinghead's avatar
Flyinghead committed
394
					if (op.rs1.is_imm())
395
						Mov(rd, op.rs1._imm);
Flyinghead's avatar
Flyinghead committed
396
					else if (regalloc.IsAllocg(op.rs1))
397
						Mov(rd, regalloc.MapRegister(op.rs1));
Flyinghead's avatar
Flyinghead committed
398
					else
399
						Fmov(rd, regalloc.MapVRegister(op.rs1));
Flyinghead's avatar
Flyinghead committed
400
401
402
403
404
405
406
				}
				break;

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

407
408
409
410
#ifdef EXPLODE_SPANS
				Fmov(regalloc.MapVRegister(op.rd, 0), regalloc.MapVRegister(op.rs1, 0));
				Fmov(regalloc.MapVRegister(op.rd, 1), regalloc.MapVRegister(op.rs1, 1));
#else
Flyinghead's avatar
Flyinghead committed
411
412
				shil_param_to_host_reg(op.rs1, x15);
				host_reg_to_shil_param(op.rd, x15);
413
#endif
Flyinghead's avatar
Flyinghead committed
414
415
416
				break;

			case shop_readm:
417
				GenReadMemory(op, i, optimise);
418
				break;
Flyinghead's avatar
Flyinghead committed
419
420

			case shop_writem:
421
				GenWriteMemory(op, i, optimise);
422
				break;
Flyinghead's avatar
Flyinghead committed
423
424

			case shop_sync_sr:
425
				GenCallRuntime(UpdateSR);
Flyinghead's avatar
Flyinghead committed
426
427
				break;
			case shop_sync_fpscr:
428
				GenCallRuntime(UpdateFPSCR);
Flyinghead's avatar
Flyinghead committed
429
430
431
				break;

			case shop_swaplb:
432
433
434
435
436
437
438
				{
					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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
				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:
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
				{
					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)
517
					Cset(regalloc.MapRegister(op.rd2), cs);	// rd2 = C
518
				}
Flyinghead's avatar
Flyinghead committed
519
520
				break;
			case shop_sbc:
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
				{
					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)
544
					Cset(regalloc.MapRegister(op.rd2), cc);	// rd2 = ~C
545
				}
Flyinghead's avatar
Flyinghead committed
546
				break;
547
			case shop_negc:
548
549
550
551
552
553
554
555
556
557
558
559
560
				{
					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)
561
					Cset(regalloc.MapRegister(op.rd2), cc);			// rd2 = ~C
562
				}
563
				break;
Flyinghead's avatar
Flyinghead committed
564
565

			case shop_rocr:
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
				{
					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
591
					Mov(regalloc.MapRegister(op.rd2), w0);						// rd2 = w0 (new C)
592
				}
Flyinghead's avatar
Flyinghead committed
593
594
				break;
			case shop_rocl:
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
				{
					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)
618
					Cset(regalloc.MapRegister(op.rd2), ne);		// rd2 = ~Z(C)
619
				}
Flyinghead's avatar
Flyinghead committed
620
621
622
623
				break;

			case shop_shld:
			case shop_shad:
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
				{
					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)
660
						// Logical shift
661
						Lsr(rd, reg1, w1);
662
663
					else
						// Arithmetic shift
664
665
666
						Asr(rd, reg1, w1);
					Bind(&end);
				}
Flyinghead's avatar
Flyinghead committed
667
668
669
670
671
672
673
674
675
				break;

			case shop_test:
			case shop_seteq:
			case shop_setge:
			case shop_setgt:
			case shop_setae:
			case shop_setab:
				{
676
					const Register rs1 = regalloc.MapRegister(op.rs1);
Flyinghead's avatar
Flyinghead committed
677
678
679
					if (op.op == shop_test)
					{
						if (op.rs2.is_imm())
680
							Tst(rs1, op.rs2._imm);
Flyinghead's avatar
Flyinghead committed
681
						else
682
							Tst(rs1, regalloc.MapRegister(op.rs2));
Flyinghead's avatar
Flyinghead committed
683
684
685
686
					}
					else
					{
						if (op.rs2.is_imm())
687
							Cmp(rs1, op.rs2._imm);
Flyinghead's avatar
Flyinghead committed
688
						else
689
							Cmp(rs1, regalloc.MapRegister(op.rs2));
Flyinghead's avatar
Flyinghead committed
690
691
692
693
694
695
696
697
					}

					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:
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
				{
					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);
722
723
					Mov(w2, wzr);	// wzr not supported by csinc (?!)
					Tst(w1, 0xFF000000);
724
					Csinc(rd, rd, w2, ne);
725
					Tst(w1, 0x00FF0000);
726
					Csinc(rd, rd, w2, ne);
727
					Tst(w1, 0x0000FF00);
728
					Csinc(rd, rd, w2, ne);
729
					Tst(w1, 0x000000FF);
730
731
					Csinc(rd, rd, w2, ne);
				}
Flyinghead's avatar
Flyinghead committed
732
733
734
				break;

			case shop_mul_u16:
735
736
737
738
739
740
741
742
743
744
745
				{
					Register reg2;
					if (op.rs2.is_imm())
					{
						Mov(w0, op.rs2.imm_value());
						reg2 = w0;
					}
					else
					{
						reg2 = regalloc.MapRegister(op.rs2);
					}
746
					Uxth(w10, regalloc.MapRegister(op.rs1));
747
					Uxth(w11, reg2);
748
					Mul(regalloc.MapRegister(op.rd), w10, w11);
749
				}
Flyinghead's avatar
Flyinghead committed
750
751
				break;
			case shop_mul_s16:
752
753
754
755
756
757
758
759
760
761
762
				{
					Register reg2;
					if (op.rs2.is_imm())
					{
						Mov(w0, op.rs2.imm_value());
						reg2 = w0;
					}
					else
					{
						reg2 = regalloc.MapRegister(op.rs2);
					}
763
					Sxth(w10, regalloc.MapRegister(op.rs1));
764
					Sxth(w11, reg2);
765
					Mul(regalloc.MapRegister(op.rd), w10, w11);
766
				}
Flyinghead's avatar
Flyinghead committed
767
768
				break;
			case shop_mul_i32:
769
770
771
772
773
774
775
776
777
778
779
780
781
				{
					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
782
783
784
785
				break;
			case shop_mul_u64:
			case shop_mul_s64:
				{
786
787
788
789
790
791
792
793
794
795
					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
796
797
					const Register& rd_xreg = Register::GetXRegFromCode(regalloc.MapRegister(op.rd).GetCode());
					if (op.op == shop_mul_u64)
798
						Umull(rd_xreg, regalloc.MapRegister(op.rs1), reg2);
Flyinghead's avatar
Flyinghead committed
799
					else
800
						Smull(rd_xreg, regalloc.MapRegister(op.rs1), reg2);
Flyinghead's avatar
Flyinghead committed
801
802
803
804
805
806
807
					const Register& rd2_xreg = Register::GetXRegFromCode(regalloc.MapRegister(op.rd2).GetCode());
					Lsr(rd2_xreg, rd_xreg, 32);
				}
				break;

			case shop_pref:
				{
808
809
810
811
812
813
					Label not_sqw;
					if (op.rs1.is_imm())
						Mov(*call_regs[0], op.rs1._imm);
					else
					{
						if (regalloc.IsAllocg(op.rs1))
814
							Lsr(w1, regalloc.MapRegister(op.rs1), 26);
815
816
817
818
819
						else
						{
							Ldr(w0, sh4_context_mem_operand(op.rs1.reg_ptr()));
							Lsr(w1, w0, 26);
						}
820
821
						Cmp(w1, 0x38);
						B(&not_sqw, ne);
822
						if (regalloc.IsAllocg(op.rs1))
823
							Mov(w0, regalloc.MapRegister(op.rs1));
824
825
826
827
828
					}

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

830
831
832
833
						GenCallRuntime(do_sqw_mmu_no_ex);
					}
					else
					{
834
835
836
837
838
839
840
841
842
843
844
						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);
845
					}
846
					Bind(&not_sqw);
Flyinghead's avatar
Flyinghead committed
847
848
849
850
851
852
853
854
855
856
				}
				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;

857
			case shop_xtrct:
858
859
				{
					const Register rd = regalloc.MapRegister(op.rd);
860
861
862
863
864
865
866
867
868
869
870
871
872
					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);
					}
873
874
					Orr(rd, rd, w0);
				}
875
876
				break;

Flyinghead's avatar
Flyinghead committed
877
878
879
880
881
			//
			// FPU
			//

			case shop_fadd:
882
				ngen_BinaryFop(&op, &MacroAssembler::Fadd);
Flyinghead's avatar
Flyinghead committed
883
884
				break;
			case shop_fsub:
885
				ngen_BinaryFop(&op, &MacroAssembler::Fsub);
Flyinghead's avatar
Flyinghead committed
886
887
				break;
			case shop_fmul:
888
				ngen_BinaryFop(&op, &MacroAssembler::Fmul);
Flyinghead's avatar
Flyinghead committed
889
890
				break;
			case shop_fdiv:
891
				ngen_BinaryFop(&op, &MacroAssembler::Fdiv);
Flyinghead's avatar
Flyinghead committed
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
				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));
922
923
924
925
				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));
926
927
928
929
#ifdef EXPLODE_SPANS
				Ldr(regalloc.MapVRegister(op.rd, 0), MemOperand(x1, 4, PostIndex));
				Ldr(regalloc.MapVRegister(op.rd, 1), MemOperand(x1));
#else
Flyinghead's avatar
Flyinghead committed
930
931
				Ldr(x2, MemOperand(x1));
				Str(x2, sh4_context_mem_operand(op.rd.reg_ptr()));
932
#endif
Flyinghead's avatar
Flyinghead committed
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
				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);
		}
989
		regalloc.Cleanup();
Flyinghead's avatar
Flyinghead committed
990

991
992
		block->relink_offset = (u32)GetBuffer()->GetCursorOffset();
		block->relink_data = 0;
Flyinghead's avatar
Flyinghead committed
993

994
		RelinkBlock(block);
Flyinghead's avatar
Flyinghead committed
995

996
		Finalize();
Flyinghead's avatar
Flyinghead committed
997
998
999
1000
1001
1002
1003
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
	}

	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:
1054
				if (prm.is_reg())
Flyinghead's avatar
Flyinghead committed
1055
					Fmov(*call_fregs[fregused], regalloc.MapVRegister(prm));
1056
1057
1058
				else if (prm.is_imm())
					Fmov(*call_fregs[fregused], reinterpret_cast<f32&>(prm._imm));
				else
Flyinghead's avatar
Flyinghead committed
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
					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;
			}
		}
1077
		GenCallRuntime((void (*)())function);
Flyinghead's avatar
Flyinghead committed
1078
1079
1080
1081
1082
1083
1084
1085
1086
	}

	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);
	}

1087
	void GenReadMemorySlow(u32 size)
1088
1089
	{
		Instruction *start_instruction = GetCursorAddress<Instruction *>();
1090

1091
1092
1093
		switch (size)
		{
		case 1:
1094
			if (!mmu_enabled())
1095
				GenCallRuntime(ReadMem8);
1096
1097
			else
				GenCallRuntime(ReadMemNoEx<u8>);
1098
1099
1100
1101
			Sxtb(w0, w0);
			break;

		case 2:
1102
			if (!mmu_enabled())
1103
				GenCallRuntime(ReadMem16);
1104
1105
			else
				GenCallRuntime(ReadMemNoEx<u16>);
1106
1107
1108
1109
			Sxth(w0, w0);
			break;

		case 4:
1110
			if (!mmu_enabled())
1111
				GenCallRuntime(ReadMem32);
1112
1113
			else
				GenCallRuntime(ReadMemNoEx<u32>);
1114
1115
1116
			break;

		case 8:
1117
			if (!mmu_enabled())
1118
				GenCallRuntime(ReadMem64);
1119
1120
			else
				GenCallRuntime(ReadMemNoEx<u64>);
1121
1122
1123
1124
1125
1126
1127
1128
1129
			break;

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

1130
	void GenWriteMemorySlow(u32 size)
1131
1132
	{
		Instruction *start_instruction = GetCursorAddress<Instruction *>();
1133

1134
1135
1136
		switch (size)
		{
		case 1:
1137
			if (!mmu_enabled())
1138
				GenCallRuntime(WriteMem8);
1139
1140
			else
				GenCallRuntime(WriteMemNoEx<u8>);
1141
1142
1143
			break;

		case 2:
1144
			if (!mmu_enabled())
1145
				GenCallRuntime(WriteMem16);
1146
1147
			else
				GenCallRuntime(WriteMemNoEx<u16>);
1148
1149
1150
			break;

		case 4:
1151
			if (!mmu_enabled())
1152
				GenCallRuntime(WriteMem32);
1153
1154
			else
				GenCallRuntime(WriteMemNoEx<u32>);
1155
1156
1157
			break;

		case 8:
1158
			if (!mmu_enabled())
1159
				GenCallRuntime(WriteMem64);
1160
1161
			else
				GenCallRuntime(WriteMemNoEx<u64>);
1162
1163
1164
1165
1166
1167
1168
1169
1170
			break;

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

1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
	u32 RelinkBlock(RuntimeBlockInfo *block)
	{
		ptrdiff_t start_offset = GetBuffer()->GetCursorOffset();

		switch (block->BlockType)
		{

		case BET_StaticJump:
		case BET_StaticCall:
			// next_pc = block->BranchBlock;
1181
			if (block->pBranchBlock == NULL)
1182
1183
			{
				if (!mmu_enabled())
1184
1185
					GenCallRuntime(ngen_LinkBlock_Generic_stub);
				else
1186
1187
1188
1189
1190
1191
				{
					Mov(w29, block->BranchBlock);
					Str(w29, sh4_context_mem_operand(&next_pc));
					GenBranch(*arm64_no_update);
				}
			}
1192
1193
			else
				GenBranch(block->pBranchBlock->code);
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
			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);
				if (block->pBranchBlock != NULL)
					GenBranch(block->pBranchBlock->code);
				else
1216
1217
				{
					if (!mmu_enabled())
1218
						GenCallRuntime(ngen_LinkBlock_cond_Branch_stub);
1219
1220
1221
1222
1223
1224
1225
					else
					{
						Mov(w29, block->BranchBlock);
						Str(w29, sh4_context_mem_operand(&next_pc));
						GenBranch(*arm64_no_update);
					}
				}
1226
1227
1228
1229
1230
1231

				Bind(&branch_not_taken);

				if (block->pNextBlock != NULL)
					GenBranch(block->pNextBlock->code);
				else
1232
1233
				{
					if (!mmu_enabled())
1234
						GenCallRuntime(ngen_LinkBlock_cond_Next_stub);
1235
1236
1237
1238
1239
					else
					{
						Mov(w29, block->NextBlock);
						Str(w29, sh4_context_mem_operand(&next_pc));
						GenBranch(*arm64_no_update);
1240
					}
1241
				}
1242
1243
1244
1245
1246
1247
1248
1249
1250
			}
			break;

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

			Str(w29, sh4_context_mem_operand(&next_pc));
1251
1252
			if (!mmu_enabled())
			{
1253
1254
1255
1256
				// 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
1257
#if RAM_SIZE_MAX == 33554432
1258
				Ubfx(w1, w29, 1, 24);
1259
1260
1261
#else
				Ubfx(w1, w29, 1, 23);
#endif
1262
1263
				Ldr(x15, MemOperand(x2, x1, LSL, 3));	// Get block entry point
				Br(x15);
1264
1265
1266
1267
1268
			}
			else
			{
				GenBranch(*arm64_no_update);
			}
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283

			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));
1284
			GenBranch(*arm64_no_update);
1285
1286
1287
1288
1289
1290
1291
1292

			break;

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

		return GetBuffer()->GetCursorOffset() - start_offset;
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
	}

	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);
		}
1310

1311
1312
1313
1314
		// Flush and invalidate caches
		vmem_platform_flush_cache(
			CC_RW2RX(GetBuffer()->GetStartAddress<void*>()), CC_RW2RX(GetBuffer()->GetEndAddress<void*>()),
			GetBuffer()->GetStartAddress<void*>(), GetBuffer()->GetEndAddress<void*>());
1315
#if 0
1316
		if (rewrite && block != NULL)
1317
		{
Flyinghead's avatar
Flyinghead committed
1318
			INFO_LOG(DYNAREC, "BLOCK %08x", block->vaddr);
1319
1320
1321
			Instruction* instr_start = (Instruction*)block->code;
//			Instruction* instr_end = GetLabelAddress<Instruction*>(&code_end);
			Instruction* instr_end = (Instruction*)((u8 *)block->code + block->host_code_size);
1322
1323
1324
1325
1326
1327
			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
1328
				INFO_LOG(DYNAREC, "VIXL  %p:  %s",
1329
1330
1331
1332
1333
1334
1335
						   reinterpret_cast<void*>(instr),
						   disasm.GetOutput());
			}
		}
#endif
	}

1336
1337
1338
1339
1340
1341
	void GenMainloop()
	{
		Label no_update;
		Label intc_sched;
		Label end_mainloop;

1342
1343
		// int intc_sched()
		arm64_intc_sched = GetCursorAddress<int (*)()>();
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
		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)
1370
		mainloop = (void (*)(void *)) CC_RW2RX(GetCursorAddress<uintptr_t>());
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429

		// Save registers
		Stp(x19, x20, MemOperand(sp, -160, PreIndex));
		Stp(x21, x22, MemOperand(sp, 16));
		Stp(x23, x24, MemOperand(sp, 32));
		Stp(x25, x26, MemOperand(sp, 48));
		Stp(x27, x28, MemOperand(sp, 64));
		Stp(s14, s15, MemOperand(sp, 80));
		Stp(vixl::aarch64::s8, s9, MemOperand(sp, 96));
		Stp(s10, s11, MemOperand(sp, 112));
		Stp(s12, s13, MemOperand(sp, 128));
		Stp(x29, x30, MemOperand(sp, 144));

		Sub(x0, x0, sizeof(Sh4Context));
		if (mmu_enabled())
		{
			Ldr(x1, reinterpret_cast<uintptr_t>(&cycle_counter));
			// Push context, cycle_counter address
			Stp(x0, x1, MemOperand(sp, -16, PreIndex));
			Mov(w0, SH4_TIMESLICE);
			Str(w0, MemOperand(x1));

			Ldr(x0, reinterpret_cast<uintptr_t>(jmp_env));
			Ldr(x1, reinterpret_cast<uintptr_t>(&setjmp));
			Blr(x1);

			Ldr(x28, MemOperand(sp));	// Set context
		}
		else
		{
			// Use x28 as sh4 context pointer
			Mov(x28, x0);
			// Use x27 as cycle_counter
			Mov(w27, SH4_TIMESLICE);
		}
		Label do_interrupts;

		// w29 is next_pc
		Ldr(w29, MemOperand(x28, offsetof(Sh4Context, pc)));
		B(&no_update);

		Bind(&intc_sched);

		// Add timeslice to cycle counter
		if (!mmu_enabled())
		{
			Add(w27, w27, SH4_TIMESLICE);
		}
		else
		{
			Ldr(x1, MemOperand(sp, 8));	// &cycle_counter
			Ldr(w0, MemOperand(x1));	// cycle_counter
			Add(w0, w0, SH4_TIMESLICE);
			Str(w0, MemOperand(x1));
		}
		Mov(x29, lr);				// Trashing pc here but it will be reset at the end of the block or in DoInterrupts
		GenCallRuntime(UpdateSystem);
		Mov(lr, x29);
		Cbnz(w0, &do_interrupts);
1430
		Ldr(w0, MemOperand(x28, offsetof(Sh4Context, CpuRunning)));
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
		Ret();

		Bind(&do_interrupts);
		Mov(x0, x29);
		GenCallRuntime(rdv_DoInterrupts);	// Updates next_pc based on host pc
		Mov(w29, w0);

		B(&no_update);

		Bind(&end_mainloop);
		if (mmu_enabled())
			// Pop context
			Add(sp, sp, 16);
		// Restore registers
		Ldp(x29, x30, MemOperand(sp, 144));
		Ldp(s12, s13, MemOperand(sp,