rec_arm64.cpp 52.4 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
68
69
70
static jmp_buf jmp_env;
static u32 cycle_counter;

static void (*mainloop)(void *context);
static void (*arm64_intc_sched)();
static void (*arm64_no_update)();

Flyinghead's avatar
Flyinghead committed
71

72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
__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"
		"mov x0, lr							\n\t"
		"sub x0, x0, #4						\n\t"	// go before the call
97
		"bl rdv_LinkBlock					\n\t"		// returns an RX addr
98
99
		"br x0								\n"

100
101
102
		".hidden ngen_FailedToFindBlock_nommu	\n\t"
		".globl ngen_FailedToFindBlock_nommu	\n\t"
	"ngen_FailedToFindBlock_nommu:			\n\t"
103
104
105
106
		"mov w0, w29						\n\t"
		"bl rdv_FailedToFindBlock			\n\t"
		"br x0								\n"

107
108
109
110
111
112
		".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"

113
114
115
116
117
118
119
		".hidden ngen_blockcheckfail		\n\t"
		".globl ngen_blockcheckfail			\n\t"
	"ngen_blockcheckfail:					\n\t"
		"bl rdv_BlockCheckFail				\n\t"
		"br x0								\n"
);

120
121
void(*ngen_FailedToFindBlock)();
static bool restarting;
Flyinghead's avatar
Flyinghead committed
122
123
124

void ngen_mainloop(void* v_cntx)
{
125
126
127
128
129
130
131
132
	do {
		restarting = false;
		generate_mainloop();

		mainloop(v_cntx);
		if (restarting)
			p_sh4rcb->cntx.CpuRunning = 1;
	} while (restarting);
Flyinghead's avatar
Flyinghead committed
133
134
135
136
}

void ngen_init_arm64()
{
Flyinghead's avatar
Flyinghead committed
137
	INFO_LOG(DYNAREC, "Initializing the ARM64 dynarec");
138
	ngen_FailedToFindBlock = &ngen_FailedToFindBlock_nommu;
Flyinghead's avatar
Flyinghead committed
139
140
}

141
void ngen_ResetBlocks_arm64()
Flyinghead's avatar
Flyinghead committed
142
{
143
144
145
146
147
148
149
150
151
152
153
154
155
156
	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>
157
static T ReadMemNoEx(u32 addr, u32, u32 pc)
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
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
{
#ifndef NO_MMU
	u32 ex;
	T rv = mmu_ReadMemNoEx<T>(addr, &ex);
	if (ex)
	{
			spc = pc;
		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)
	{
			spc = pc;
		longjmp(jmp_env, 1);
	}
#endif
}

static void interpreter_fallback(u16 op, OpCallFP *oph, u32 pc)
{
	try {
		oph(op);
	} catch (SH4ThrownException& ex) {
		if (pc & 1)
{
			// Delay slot
			AdjustDelaySlotException(ex);
			pc--;
}
		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)
{
			// Delay slot
			AdjustDelaySlotException(ex);
			pc--;
		}
		Do_Exception(pc, ex.expEvn, ex.callVect);
		longjmp(jmp_env, 1);
	}
Flyinghead's avatar
Flyinghead committed
216
217
218
219
220
221
}

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

public:
225
226
227
	Arm64Assembler() : Arm64Assembler(emit_GetCCPtr())
	{
	}
228
	Arm64Assembler(void *buffer) : MacroAssembler((u8 *)buffer, emit_FreeSpace()), regalloc(this)
Flyinghead's avatar
Flyinghead committed
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
	{
		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);
	}

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
	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
300
301
302
303
304
305
	const Register& GenMemAddr(const shil_opcode& op, const Register* raddr = NULL)
	{
		const Register* ret_reg = raddr == NULL ? &w0 : raddr;

		if (op.rs3.is_imm())
		{
306
			if (regalloc.IsAllocg(op.rs1))
Flyinghead's avatar
Flyinghead committed
307
			Add(*ret_reg, regalloc.MapRegister(op.rs1), op.rs3._imm);
308
309
310
311
312
			else
			{
				Ldr(*ret_reg, sh4_context_mem_operand(op.rs1.reg_ptr()));
				Add(*ret_reg, *ret_reg, op.rs3._imm);
		}
Flyinghead's avatar
Flyinghead committed
313
314
315
		}
		else if (op.rs3.is_r32i())
		{
316
			if (regalloc.IsAllocg(op.rs1) && regalloc.IsAllocg(op.rs3))
Flyinghead's avatar
Flyinghead committed
317
			Add(*ret_reg, regalloc.MapRegister(op.rs1), regalloc.MapRegister(op.rs3));
318
319
320
321
322
323
			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
324
325
326
327
328
		}
		else if (!op.rs3.is_null())
		{
			die("invalid rs3");
		}
329
330
331
		else if (op.rs1.is_reg())
		{
			if (regalloc.IsAllocg(op.rs1))
Flyinghead's avatar
Flyinghead committed
332
333
334
335
336
337
		{
			if (raddr == NULL)
				ret_reg = &regalloc.MapRegister(op.rs1);
			else
				Mov(*ret_reg, regalloc.MapRegister(op.rs1));
		}
338
339
340
341
342
343
344
345
346
347
			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
348
349
350
351
352
353
354

		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);
355
		this->block = block;
Flyinghead's avatar
Flyinghead committed
356
357
358
359
360
361
362
		if (force_checks)
			CheckBlock(block);

		// run register allocator
		regalloc.DoAlloc(block);

		// scheduler
363
364
365
366
367
368
369
370
371
		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
		{
372
		Subs(w27, w27, block->guest_cycles);
373
		}
374
375
		Label cycles_remaining;
		B(&cycles_remaining, pl);
376
		GenCall(*arm64_intc_sched);
377
		Bind(&cycles_remaining);
Flyinghead's avatar
Flyinghead committed
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393

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

394
395
				if (!mmu_enabled())
				{
396
				GenCallRuntime(OpDesc[op.rs3._imm]->oph);
397
398
399
400
401
402
403
404
405
				}
				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
406
407
408
409
				break;

			case shop_jcond:
			case shop_jdyn:
410
411
				{
					const Register rd = regalloc.MapRegister(op.rd);
Flyinghead's avatar
Flyinghead committed
412
				if (op.rs2.is_imm())
413
						Add(rd, regalloc.MapRegister(op.rs1), op.rs2._imm);
Flyinghead's avatar
Flyinghead committed
414
				else
415
						Mov(rd, regalloc.MapRegister(op.rs1));
416
				// Save it for the branching at the end of the block
417
418
					Mov(w29, rd);
				}
Flyinghead's avatar
Flyinghead committed
419
420
421
422
423
424
425
426
				break;

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

				if (regalloc.IsAllocf(op.rd))
				{
427
					const VRegister rd = regalloc.MapVRegister(op.rd);
Flyinghead's avatar
Flyinghead committed
428
					if (op.rs1.is_imm())
429
						Fmov(rd, reinterpret_cast<f32&>(op.rs1._imm));
Flyinghead's avatar
Flyinghead committed
430
					else if (regalloc.IsAllocf(op.rs1))
431
						Fmov(rd, regalloc.MapVRegister(op.rs1));
Flyinghead's avatar
Flyinghead committed
432
					else
433
						Fmov(rd, regalloc.MapRegister(op.rs1));
Flyinghead's avatar
Flyinghead committed
434
435
436
				}
				else
				{
437
					const Register rd = regalloc.MapRegister(op.rd);
Flyinghead's avatar
Flyinghead committed
438
					if (op.rs1.is_imm())
439
						Mov(rd, op.rs1._imm);
Flyinghead's avatar
Flyinghead committed
440
					else if (regalloc.IsAllocg(op.rs1))
441
						Mov(rd, regalloc.MapRegister(op.rs1));
Flyinghead's avatar
Flyinghead committed
442
					else
443
						Fmov(rd, regalloc.MapVRegister(op.rs1));
Flyinghead's avatar
Flyinghead committed
444
445
446
447
448
449
450
				}
				break;

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

451
452
453
454
#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
455
456
				shil_param_to_host_reg(op.rs1, x15);
				host_reg_to_shil_param(op.rd, x15);
457
#endif
Flyinghead's avatar
Flyinghead committed
458
459
460
				break;

			case shop_readm:
461
				GenReadMemory(op, i, optimise);
Flyinghead's avatar
Flyinghead committed
462
463
464
			break;

			case shop_writem:
465
				GenWriteMemory(op, i, optimise);
Flyinghead's avatar
Flyinghead committed
466
467
468
					break;

			case shop_sync_sr:
469
				GenCallRuntime(UpdateSR);
Flyinghead's avatar
Flyinghead committed
470
471
				break;
			case shop_sync_fpscr:
472
				GenCallRuntime(UpdateFPSCR);
Flyinghead's avatar
Flyinghead committed
473
474
475
				break;

			case shop_swaplb:
476
477
478
479
480
481
482
				{
					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
483
484
485
486
487
488
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
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
				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:
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
				{
					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)
Flyinghead's avatar
Flyinghead committed
561
				Cset(regalloc.MapRegister(op.rd2), cs);	// rd2 = C
562
				}
Flyinghead's avatar
Flyinghead committed
563
564
				break;
			case shop_sbc:
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
				{
					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)
Flyinghead's avatar
Flyinghead committed
588
				Cset(regalloc.MapRegister(op.rd2), cc);	// rd2 = ~C
589
				}
Flyinghead's avatar
Flyinghead committed
590
				break;
591
			case shop_negc:
592
593
594
595
596
597
598
599
600
601
602
603
604
				{
					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)
605
				Cset(regalloc.MapRegister(op.rd2), cc);	// rd2 = ~C
606
				}
607
				break;
Flyinghead's avatar
Flyinghead committed
608
609

			case shop_rocr:
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
				{
					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
Flyinghead's avatar
Flyinghead committed
635
				Mov(regalloc.MapRegister(op.rd2), w0);												// rd2 = w0 (new C)
636
				}
Flyinghead's avatar
Flyinghead committed
637
638
				break;
			case shop_rocl:
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
				{
					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)
Flyinghead's avatar
Flyinghead committed
662
				Cset(regalloc.MapRegister(op.rd2), ne);			// rd2 = ~Z(C)
663
				}
Flyinghead's avatar
Flyinghead committed
664
665
666
667
				break;

			case shop_shld:
			case shop_shad:
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
				{
					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)
Flyinghead's avatar
Flyinghead committed
704
					// Logical shift
705
						Lsr(rd, reg1, w1);
Flyinghead's avatar
Flyinghead committed
706
707
				else
					// Arithmetic shift
708
709
710
						Asr(rd, reg1, w1);
					Bind(&end);
				}
Flyinghead's avatar
Flyinghead committed
711
712
713
714
715
716
717
718
719
				break;

			case shop_test:
			case shop_seteq:
			case shop_setge:
			case shop_setgt:
			case shop_setae:
			case shop_setab:
				{
720
					const Register rs1 = regalloc.MapRegister(op.rs1);
Flyinghead's avatar
Flyinghead committed
721
722
723
					if (op.op == shop_test)
					{
						if (op.rs2.is_imm())
724
							Tst(rs1, op.rs2._imm);
Flyinghead's avatar
Flyinghead committed
725
						else
726
							Tst(rs1, regalloc.MapRegister(op.rs2));
Flyinghead's avatar
Flyinghead committed
727
728
729
730
					}
					else
					{
						if (op.rs2.is_imm())
731
							Cmp(rs1, op.rs2._imm);
Flyinghead's avatar
Flyinghead committed
732
						else
733
							Cmp(rs1, regalloc.MapRegister(op.rs2));
Flyinghead's avatar
Flyinghead committed
734
735
736
737
738
739
740
741
					}

					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:
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
				{
					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);
Flyinghead's avatar
Flyinghead committed
766
767
				Mov(w2, wzr);	// wzr not supported by csinc (?!)
				Tst(w1, 0xFF000000);
768
					Csinc(rd, rd, w2, ne);
Flyinghead's avatar
Flyinghead committed
769
				Tst(w1, 0x00FF0000);
770
					Csinc(rd, rd, w2, ne);
Flyinghead's avatar
Flyinghead committed
771
				Tst(w1, 0x0000FF00);
772
					Csinc(rd, rd, w2, ne);
Flyinghead's avatar
Flyinghead committed
773
				Tst(w1, 0x000000FF);
774
775
					Csinc(rd, rd, w2, ne);
				}
Flyinghead's avatar
Flyinghead committed
776
777
778
				break;

			case shop_mul_u16:
779
780
781
782
783
784
785
786
787
788
789
				{
					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
790
				Uxth(w10, regalloc.MapRegister(op.rs1));
791
					Uxth(w11, reg2);
Flyinghead's avatar
Flyinghead committed
792
				Mul(regalloc.MapRegister(op.rd), w10, w11);
793
				}
Flyinghead's avatar
Flyinghead committed
794
795
				break;
			case shop_mul_s16:
796
797
798
799
800
801
802
803
804
805
806
				{
					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
807
				Sxth(w10, regalloc.MapRegister(op.rs1));
808
					Sxth(w11, reg2);
Flyinghead's avatar
Flyinghead committed
809
				Mul(regalloc.MapRegister(op.rd), w10, w11);
810
				}
Flyinghead's avatar
Flyinghead committed
811
812
				break;
			case shop_mul_i32:
813
814
815
816
817
818
819
820
821
822
823
824
825
				{
					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
826
827
828
829
				break;
			case shop_mul_u64:
			case shop_mul_s64:
				{
830
831
832
833
834
835
836
837
838
839
					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
840
841
					const Register& rd_xreg = Register::GetXRegFromCode(regalloc.MapRegister(op.rd).GetCode());
					if (op.op == shop_mul_u64)
842
						Umull(rd_xreg, regalloc.MapRegister(op.rs1), reg2);
Flyinghead's avatar
Flyinghead committed
843
					else
844
						Smull(rd_xreg, regalloc.MapRegister(op.rs1), reg2);
Flyinghead's avatar
Flyinghead committed
845
846
847
848
849
850
851
					const Register& rd2_xreg = Register::GetXRegFromCode(regalloc.MapRegister(op.rd2).GetCode());
					Lsr(rd2_xreg, rd_xreg, 32);
				}
				break;

			case shop_pref:
				{
852
853
854
855
856
857
					Label not_sqw;
					if (op.rs1.is_imm())
						Mov(*call_regs[0], op.rs1._imm);
					else
					{
						if (regalloc.IsAllocg(op.rs1))
Flyinghead's avatar
Flyinghead committed
858
					Lsr(w1, regalloc.MapRegister(op.rs1), 26);
859
860
861
862
863
						else
						{
							Ldr(w0, sh4_context_mem_operand(op.rs1.reg_ptr()));
							Lsr(w1, w0, 26);
						}
Flyinghead's avatar
Flyinghead committed
864
					Cmp(w1, 0x38);
865
					B(&not_sqw, ne);
866
						if (regalloc.IsAllocg(op.rs1))
867
					Mov(w0, regalloc.MapRegister(op.rs1));
868
869
870
871
872
					}

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

874
875
876
877
						GenCallRuntime(do_sqw_mmu_no_ex);
					}
					else
					{
878
879
880
881
882
883
884
885
886
887
					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));
					}
Flyinghead's avatar
Flyinghead committed
888
					Blr(x9);
889
					}
890
					Bind(&not_sqw);
Flyinghead's avatar
Flyinghead committed
891
892
893
894
895
896
897
898
899
900
				}
				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;

901
			case shop_xtrct:
902
903
904
				{
					const Register rd = regalloc.MapRegister(op.rd);
					Lsr(rd, regalloc.MapRegister(op.rs1), 16);
905
				Lsl(w0, regalloc.MapRegister(op.rs2), 16);
906
907
					Orr(rd, rd, w0);
				}
908
909
				break;

Flyinghead's avatar
Flyinghead committed
910
911
912
913
914
			//
			// FPU
			//

			case shop_fadd:
915
				ngen_BinaryFop(&op, &MacroAssembler::Fadd);
Flyinghead's avatar
Flyinghead committed
916
917
				break;
			case shop_fsub:
918
				ngen_BinaryFop(&op, &MacroAssembler::Fsub);
Flyinghead's avatar
Flyinghead committed
919
920
				break;
			case shop_fmul:
921
				ngen_BinaryFop(&op, &MacroAssembler::Fmul);
Flyinghead's avatar
Flyinghead committed
922
923
				break;
			case shop_fdiv:
924
				ngen_BinaryFop(&op, &MacroAssembler::Fdiv);
Flyinghead's avatar
Flyinghead committed
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
				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));
955
956
957
958
				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));
959
960
961
962
#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
963
964
				Ldr(x2, MemOperand(x1));
				Str(x2, sh4_context_mem_operand(op.rd.reg_ptr()));
965
#endif
Flyinghead's avatar
Flyinghead committed
966
967
968
969
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
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
				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);
		}
1022
		regalloc.Cleanup();
Flyinghead's avatar
Flyinghead committed
1023

1024
1025
		block->relink_offset = (u32)GetBuffer()->GetCursorOffset();
		block->relink_data = 0;
Flyinghead's avatar
Flyinghead committed
1026

1027
		RelinkBlock(block);
Flyinghead's avatar
Flyinghead committed
1028

1029
		Finalize();
Flyinghead's avatar
Flyinghead committed
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
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
	}

	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:
1087
				if (prm.is_reg())
Flyinghead's avatar
Flyinghead committed
1088
					Fmov(*call_fregs[fregused], regalloc.MapVRegister(prm));
1089
1090
1091
				else if (prm.is_imm())
					Fmov(*call_fregs[fregused], reinterpret_cast<f32&>(prm._imm));
				else
Flyinghead's avatar
Flyinghead committed
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
					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;
			}
		}
1110
		GenCallRuntime((void (*)())function);
Flyinghead's avatar
Flyinghead committed
1111
1112
1113
1114
1115
1116
1117
1118
1119
	}

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

1120
	void GenReadMemorySlow(u32 size)
1121
1122
	{
		Instruction *start_instruction = GetCursorAddress<Instruction *>();
1123

1124
1125
1126
		switch (size)
		{
		case 1:
1127
			if (!mmu_enabled())
1128
			GenCallRuntime(ReadMem8);
1129
1130
			else
				GenCallRuntime(ReadMemNoEx<u8>);
1131
1132
1133
1134
			Sxtb(w0, w0);
			break;

		case 2:
1135
			if (!mmu_enabled())
1136
			GenCallRuntime(ReadMem16);
1137
1138
			else
				GenCallRuntime(ReadMemNoEx<u16>);
1139
1140
1141
1142
			Sxth(w0, w0);
			break;

		case 4:
1143
			if (!mmu_enabled())
1144
			GenCallRuntime(ReadMem32);
1145
1146
			else
				GenCallRuntime(ReadMemNoEx<u32>);
1147
1148
1149
			break;

		case 8:
1150
			if (!mmu_enabled())
1151
			GenCallRuntime(ReadMem64);
1152
1153
			else
				GenCallRuntime(ReadMemNoEx<u64>);
1154
1155
1156
1157
1158
1159
1160
1161
1162
			break;

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

1163
	void GenWriteMemorySlow(u32 size)
1164
1165
	{
		Instruction *start_instruction = GetCursorAddress<Instruction *>();
1166

1167
1168
1169
		switch (size)
		{
		case 1:
1170
			if (!mmu_enabled())
1171
				GenCallRuntime(WriteMem8);
1172
1173
			else
				GenCallRuntime(WriteMemNoEx<u8>);
1174
1175
1176
			break;

		case 2:
1177
			if (!mmu_enabled())
1178
				GenCallRuntime(WriteMem16);
1179
1180
			else
				GenCallRuntime(WriteMemNoEx<u16>);
1181
1182
1183
			break;

		case 4:
1184
			if (!mmu_enabled())
1185
				GenCallRuntime(WriteMem32);
1186
1187
			else
				GenCallRuntime(WriteMemNoEx<u32>);
1188
1189
1190
			break;

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

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

1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
	u32 RelinkBlock(RuntimeBlockInfo *block)
	{
		ptrdiff_t start_offset = GetBuffer()->GetCursorOffset();

		switch (block->BlockType)
		{

		case BET_StaticJump:
		case BET_StaticCall:
			// next_pc = block->BranchBlock;
			if (block->pBranchBlock == NULL)
1215
1216
			{
				if (!mmu_enabled())
1217
				GenCallRuntime(ngen_LinkBlock_Generic_stub);
1218
1219
1220
1221
1222
1223
1224
			else
				{
					Mov(w29, block->BranchBlock);
					Str(w29, sh4_context_mem_operand(&next_pc));
					GenBranch(*arm64_no_update);
				}
			}
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
			else
				GenBranch(block->pBranchBlock->code);
			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
1249
1250
				{
					if (!mmu_enabled())
1251
					GenCallRuntime(ngen_LinkBlock_cond_Branch_stub);
1252
1253
1254
1255
1256
1257
1258
					else
					{
						Mov(w29, block->BranchBlock);
						Str(w29, sh4_context_mem_operand(&next_pc));
						GenBranch(*arm64_no_update);
					}
				}
1259
1260
1261
1262
1263
1264

				Bind(&branch_not_taken);

				if (block->pNextBlock != NULL)
					GenBranch(block->pNextBlock->code);
				else
1265
1266
				{
					if (!mmu_enabled())
1267
					GenCallRuntime(ngen_LinkBlock_cond_Next_stub);
1268
1269
1270
1271
1272
1273
1274
					else
					{
						Mov(w29, block->NextBlock);
						Str(w29, sh4_context_mem_operand(&next_pc));
						GenBranch(*arm64_no_update);
			}
				}
1275
1276
1277
1278
1279
1280
1281
1282
1283
			}
			break;

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

			Str(w29, sh4_context_mem_operand(&next_pc));
1284
1285
			if (!mmu_enabled())
			{
1286
1287
1288
1289
			// 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
1290
#if RAM_SIZE_MAX == 33554432
1291
			Ubfx(w1, w29, 1, 24);
1292
1293
1294
#else
				Ubfx(w1, w29, 1, 23);
#endif
1295
1296
			Ldr(x15, MemOperand(x2, x1, LSL, 3));	// Get block entry point
			Br(x15);
1297
1298
1299
1300
1301
			}
			else
			{
				GenBranch(*arm64_no_update);
			}
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316

			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));
1317
			GenBranch(*arm64_no_update);
1318
1319
1320
1321
1322
1323
1324
1325

			break;

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

		return GetBuffer()->GetCursorOffset() - start_offset;
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
	}

	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);
		}
1343
1344
1345
1346
		// Flush and invalidate caches
		vmem_platform_flush_cache(
			CC_RW2RX(GetBuffer()->GetStartAddress<void*>()), CC_RW2RX(GetBuffer()->GetEndAddress<void*>()),
			GetBuffer()->GetStartAddress<void*>(), GetBuffer()->GetEndAddress<void*>());
1347
#if 0
1348
		if (rewrite && block != NULL)
1349
		{
Flyinghead's avatar
Flyinghead committed
1350
			INFO_LOG(DYNAREC, "BLOCK %08x", block->vaddr);
1351
1352
1353
			Instruction* instr_start = (Instruction*)block->code;
//			Instruction* instr_end = GetLabelAddress<Instruction*>(&code_end);
			Instruction* instr_end = (Instruction*)((u8 *)block->code + block->host_code_size);
1354
1355
1356
1357
1358
1359
			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
1360
				INFO_LOG(DYNAREC, "VIXL  %p:  %s",
1361
1362
1363
1364
1365
1366
1367
						   reinterpret_cast<void*>(instr),
						   disasm.GetOutput());
			}
		}
#endif
	}

1368
1369
1370
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
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
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
	void GenMainloop()
	{
		Label no_update;
		Label intc_sched;
		Label end_mainloop;

		// void intc_sched()
		arm64_intc_sched = GetCursorAddress<void (*)()>();
		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)
		mainloop = GetCursorAddress<void (*)(void *)>();

		// 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);
		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, 128));
		Ldp(s10, s11, MemOperand(sp, 112));
		Ldp(vixl::aarch64::s8, s9, MemOperand(sp, 96));
		Ldp(s14, s15, MemOperand(sp, 80));
		Ldp(x27, x28, MemOperand(sp, 64));
		Ldp(x25, x26, MemOperand(sp, 48));
		Ldp(x23, x24, MemOperand(sp, 32));
		Ldp(x21, x22, MemOperand(sp, 16));
		Ldp(x19, x20, MemOperand(sp, 160, PostIndex));
		Ret();

		FinalizeCode();
		emit_Skip(GetBuffer()->GetSizeInBytes());

		arm64_no_update = GetLabelAddress<void (*)()>(&no_update);

		// Flush and invalidate caches
		vmem_platform_flush_cache(
			CC_RW2RX(GetBuffer()->GetStartAddress<void*>()), CC_RW2RX(GetBuffer()->GetEndAddress<void*>()),
			GetBuffer()->GetStartAddress<void*>(), GetBuffer()->GetEndAddress<void*>());
	}


Flyinghead's avatar
Flyinghead committed
1500
private:
1501
1502
1503
	// Runtime branches/calls need to be adjusted if rx space is different to rw space.
	// Therefore can't mix GenBranch with GenBranchRuntime!

1504
1505
1506
	template <typename R, typename... P>
	void GenCallRuntime(R (*function)(P...))
	{
1507
		ptrdiff_t offset = reinterpret_cast<uintptr_t>(function) - reinterpret_cast<uintptr_t>(CC_RW2RX(GetBuffer()->GetStartAddress<void*>()));
1508
1509
		verify(offset >= -128 * 1024 * 1024 && offset <= 128 * 1024 * 1024);
		verify((offset & 3) == 0);
1510
1511
1512
1513
1514
		Label function_label;
		BindToOffset(&function_label, offset);
		Bl(&function_label);
	}

1515
	template <typename R, typename... P>
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
	void GenCall