rec_arm64.cpp 52.1 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
71
72
static jmp_buf jmp_env;
static u32 cycle_counter;

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

void(*ngen_FailedToFindBlock)();
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
87
}

void ngen_init_arm64()
{
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_arm64()
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);
Flyinghead's avatar
Flyinghead committed
308
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
		Bind(&cycles_remaining);
Flyinghead's avatar
Flyinghead committed
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343

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

344
345
				if (!mmu_enabled())
				{
346
					GenCallRuntime(OpDesc[op.rs3._imm]->oph);
347
348
349
350
351
352
353
354
355
				}
				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
356
357
358
359
				break;

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

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

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

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

401
402
403
404
#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
405
406
				shil_param_to_host_reg(op.rs1, x15);
				host_reg_to_shil_param(op.rd, x15);
407
#endif
Flyinghead's avatar
Flyinghead committed
408
409
410
				break;

			case shop_readm:
411
				GenReadMemory(op, i, optimise);
412
				break;
Flyinghead's avatar
Flyinghead committed
413
414

			case shop_writem:
415
				GenWriteMemory(op, i, optimise);
416
				break;
Flyinghead's avatar
Flyinghead committed
417
418

			case shop_sync_sr:
419
				GenCallRuntime(UpdateSR);
Flyinghead's avatar
Flyinghead committed
420
421
				break;
			case shop_sync_fpscr:
422
				GenCallRuntime(UpdateFPSCR);
Flyinghead's avatar
Flyinghead committed
423
424
425
				break;

			case shop_swaplb:
426
427
428
429
430
431
432
				{
					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
433
434
435
436
437
438
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
				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:
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
				{
					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)
511
					Cset(regalloc.MapRegister(op.rd2), cs);	// rd2 = C
512
				}
Flyinghead's avatar
Flyinghead committed
513
514
				break;
			case shop_sbc:
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
				{
					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)
538
					Cset(regalloc.MapRegister(op.rd2), cc);	// rd2 = ~C
539
				}
Flyinghead's avatar
Flyinghead committed
540
				break;
541
			case shop_negc:
542
543
544
545
546
547
548
549
550
551
552
553
554
				{
					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)
555
					Cset(regalloc.MapRegister(op.rd2), cc);			// rd2 = ~C
556
				}
557
				break;
Flyinghead's avatar
Flyinghead committed
558
559

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

			case shop_shld:
			case shop_shad:
618
619
620
621
622
623
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
				{
					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)
654
						// Logical shift
655
						Lsr(rd, reg1, w1);
656
657
					else
						// Arithmetic shift
658
659
660
						Asr(rd, reg1, w1);
					Bind(&end);
				}
Flyinghead's avatar
Flyinghead committed
661
662
663
664
665
666
667
668
669
				break;

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

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

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

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

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

824
825
826
827
						GenCallRuntime(do_sqw_mmu_no_ex);
					}
					else
					{
828
829
830
831
832
833
834
835
836
837
838
						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);
839
					}
840
					Bind(&not_sqw);
Flyinghead's avatar
Flyinghead committed
841
842
843
844
845
846
847
848
849
850
				}
				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;

851
			case shop_xtrct:
852
853
				{
					const Register rd = regalloc.MapRegister(op.rd);
854
855
856
857
858
859
860
861
862
863
864
865
866
					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);
					}
867
868
					Orr(rd, rd, w0);
				}
869
870
				break;

Flyinghead's avatar
Flyinghead committed
871
872
873
874
875
			//
			// FPU
			//

			case shop_fadd:
876
				ngen_BinaryFop(&op, &MacroAssembler::Fadd);
Flyinghead's avatar
Flyinghead committed
877
878
				break;
			case shop_fsub:
879
				ngen_BinaryFop(&op, &MacroAssembler::Fsub);
Flyinghead's avatar
Flyinghead committed
880
881
				break;
			case shop_fmul:
882
				ngen_BinaryFop(&op, &MacroAssembler::Fmul);
Flyinghead's avatar
Flyinghead committed
883
884
				break;
			case shop_fdiv:
885
				ngen_BinaryFop(&op, &MacroAssembler::Fdiv);
Flyinghead's avatar
Flyinghead committed
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
				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));
916
917
918
919
				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));
920
921
922
923
#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
924
925
				Ldr(x2, MemOperand(x1));
				Str(x2, sh4_context_mem_operand(op.rd.reg_ptr()));
926
#endif
Flyinghead's avatar
Flyinghead committed
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
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
				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);
		}
983
		regalloc.Cleanup();
Flyinghead's avatar
Flyinghead committed
984

985
986
		block->relink_offset = (u32)GetBuffer()->GetCursorOffset();
		block->relink_data = 0;
Flyinghead's avatar
Flyinghead committed
987

988
		RelinkBlock(block);
Flyinghead's avatar
Flyinghead committed
989

990
		Finalize();
Flyinghead's avatar
Flyinghead committed
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
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
	}

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

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

1081
	void GenReadMemorySlow(u32 size)
1082
1083
	{
		Instruction *start_instruction = GetCursorAddress<Instruction *>();
1084

1085
1086
1087
		switch (size)
		{
		case 1:
1088
			if (!mmu_enabled())
1089
				GenCallRuntime(ReadMem8);
1090
1091
			else
				GenCallRuntime(ReadMemNoEx<u8>);
1092
1093
1094
1095
			Sxtb(w0, w0);
			break;

		case 2:
1096
			if (!mmu_enabled())
1097
				GenCallRuntime(ReadMem16);
1098
1099
			else
				GenCallRuntime(ReadMemNoEx<u16>);
1100
1101
1102
1103
			Sxth(w0, w0);
			break;

		case 4:
1104
			if (!mmu_enabled())
1105
				GenCallRuntime(ReadMem32);
1106
1107
			else
				GenCallRuntime(ReadMemNoEx<u32>);
1108
1109
1110
			break;

		case 8:
1111
			if (!mmu_enabled())
1112
				GenCallRuntime(ReadMem64);
1113
1114
			else
				GenCallRuntime(ReadMemNoEx<u64>);
1115
1116
1117
1118
1119
1120
1121
1122
1123
			break;

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

1124
	void GenWriteMemorySlow(u32 size)
1125
1126
	{
		Instruction *start_instruction = GetCursorAddress<Instruction *>();
1127

1128
1129
1130
		switch (size)
		{
		case 1:
1131
			if (!mmu_enabled())
1132
				GenCallRuntime(WriteMem8);
1133
1134
			else
				GenCallRuntime(WriteMemNoEx<u8>);
1135
1136
1137
			break;

		case 2:
1138
			if (!mmu_enabled())
1139
				GenCallRuntime(WriteMem16);
1140
1141
			else
				GenCallRuntime(WriteMemNoEx<u16>);
1142
1143
1144
			break;

		case 4:
1145
			if (!mmu_enabled())
1146
				GenCallRuntime(WriteMem32);
1147
1148
			else
				GenCallRuntime(WriteMemNoEx<u32>);
1149
1150
1151
			break;

		case 8:
1152
			if (!mmu_enabled())
1153
				GenCallRuntime(WriteMem64);
1154
1155
			else
				GenCallRuntime(WriteMemNoEx<u64>);
1156
1157
1158
1159
1160
1161
1162
1163
1164
			break;

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

1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
	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)
1176
1177
			{
				if (!mmu_enabled())
1178
1179
					GenCallRuntime(ngen_LinkBlock_Generic_stub);
				else
1180
1181
1182
1183
1184
1185
				{
					Mov(w29, block->BranchBlock);
					Str(w29, sh4_context_mem_operand(&next_pc));
					GenBranch(*arm64_no_update);
				}
			}
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
			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
1210
1211
				{
					if (!mmu_enabled())
1212
						GenCallRuntime(ngen_LinkBlock_cond_Branch_stub);
1213
1214
1215
1216
1217
1218
1219
					else
					{
						Mov(w29, block->BranchBlock);
						Str(w29, sh4_context_mem_operand(&next_pc));
						GenBranch(*arm64_no_update);
					}
				}
1220
1221
1222
1223
1224
1225

				Bind(&branch_not_taken);

				if (block->pNextBlock != NULL)
					GenBranch(block->pNextBlock->code);
				else
1226
1227
				{
					if (!mmu_enabled())
1228
						GenCallRuntime(ngen_LinkBlock_cond_Next_stub);
1229
1230
1231
1232
1233
					else
					{
						Mov(w29, block->NextBlock);
						Str(w29, sh4_context_mem_operand(&next_pc));
						GenBranch(*arm64_no_update);
1234
					}
1235
				}
1236
1237
1238
1239
1240
1241
1242
1243
1244
			}
			break;

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

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

			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));
1278
			GenBranch(*arm64_no_update);
1279
1280
1281
1282
1283
1284
1285
1286

			break;

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

		return GetBuffer()->GetCursorOffset() - start_offset;
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
	}

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

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

1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
	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)
m4xw's avatar
m4xw committed
1364
		mainloop = (void (*)(void *)) CC_RW2RX(GetCursorAddress<uintptr_t>());
1365
1366
1367
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

		// 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
1462
private:
1463
1464
1465
	// Runtime branches/calls need to be adjusted if rx space is different to rw space.
	// Therefore can't mix GenBranch with GenBranchRuntime!

1466
1467
1468
	template <typename R, typename... P>
	void GenCallRuntime(R (*function)(P...))
	{
1469
		ptrdiff_t offset = reinterpret_cast<uintptr_t>(function) - reinterpret_cast<uintptr_t>(CC_RW2RX(GetBuffer()->GetStartAddress<void*>()));
1470
1471
		verify(offset >= -128 * 1024 * 1024 && offset <= 128 * 1024 * 1024);
		verify((offset & 3) == 0);
1472
1473
1474
1475
1476
		Label function_label;
		BindToOffset(&function_label, offset);
		Bl(&function_label);
	}

1477
	template <typename R, typename... P>