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

	This file is part of reicast.

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

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

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

#include "types.h"

#if FEAT_SHREC == DYNAREC_JIT

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

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

31
//#define EXPLODE_SPANS
32

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

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

#undef do_sqw_nommu

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

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

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

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

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

static bool restarting;
Flyinghead's avatar
Flyinghead committed
72
73
74

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

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

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

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

106
107
108
109
110
111
void ngen_GetFeatures(ngen_features* dst)
{
	dst->InterpreterFallback = false;
	dst->OnlyDynamicEnds     = false;
}

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

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

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

public:
181
182
183
	Arm64Assembler() : Arm64Assembler(emit_GetCCPtr())
	{
	}
184
	Arm64Assembler(void *buffer) : MacroAssembler((u8 *)buffer, emit_FreeSpace()), regalloc(this)
Flyinghead's avatar
Flyinghead committed
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
226
227
228
229
230
	{
		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);
	}

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
	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
256
257
258
259
260
261
	const Register& GenMemAddr(const shil_opcode& op, const Register* raddr = NULL)
	{
		const Register* ret_reg = raddr == NULL ? &w0 : raddr;

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

		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);
311
		this->block = block;
312
		CheckBlock(force_checks, block);
313

Flyinghead's avatar
Flyinghead committed
314
315
316
317
		// run register allocator
		regalloc.DoAlloc(block);

		// scheduler
318
319
320
321
322
323
324
325
326
		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
		{
327
			Subs(w27, w27, block->guest_cycles);
328
		}
329
330
		Label cycles_remaining;
		B(&cycles_remaining, pl);
331
		GenCall(*arm64_intc_sched);
332
333
334
335
336
337
		Label cpu_running;
		Cbnz(w0, &cpu_running);
		Mov(w29, block->vaddr);
		Str(w29, sh4_context_mem_operand(&next_pc));
		GenBranch(*arm64_no_update);
		Bind(&cpu_running);
338
		Bind(&cycles_remaining);
Flyinghead's avatar
Flyinghead committed
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354

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

355
356
				if (!mmu_enabled())
				{
357
					GenCallRuntime(OpDesc[op.rs3._imm]->oph);
358
359
360
361
362
363
364
365
366
				}
				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
367
368
369
370
				break;

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

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

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

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

412
413
414
415
#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
416
417
				shil_param_to_host_reg(op.rs1, x15);
				host_reg_to_shil_param(op.rd, x15);
418
#endif
Flyinghead's avatar
Flyinghead committed
419
420
421
				break;

			case shop_readm:
422
				GenReadMemory(op, i, optimise);
423
				break;
Flyinghead's avatar
Flyinghead committed
424
425

			case shop_writem:
426
				GenWriteMemory(op, i, optimise);
427
				break;
Flyinghead's avatar
Flyinghead committed
428
429

			case shop_sync_sr:
430
				GenCallRuntime(UpdateSR);
Flyinghead's avatar
Flyinghead committed
431
432
				break;
			case shop_sync_fpscr:
433
				GenCallRuntime(UpdateFPSCR);
Flyinghead's avatar
Flyinghead committed
434
435
436
				break;

			case shop_swaplb:
437
438
439
440
441
442
443
				{
					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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
				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:
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
				{
					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)
522
					Cset(regalloc.MapRegister(op.rd2), cs);	// rd2 = C
523
				}
Flyinghead's avatar
Flyinghead committed
524
525
				break;
			case shop_sbc:
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
				{
					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)
549
					Cset(regalloc.MapRegister(op.rd2), cc);	// rd2 = ~C
550
				}
Flyinghead's avatar
Flyinghead committed
551
				break;
552
			case shop_negc:
553
554
555
556
557
558
559
560
561
562
563
564
565
				{
					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)
566
					Cset(regalloc.MapRegister(op.rd2), cc);			// rd2 = ~C
567
				}
568
				break;
Flyinghead's avatar
Flyinghead committed
569
570

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

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

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

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

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

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

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

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

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

Flyinghead's avatar
Flyinghead committed
882
883
884
885
886
			//
			// FPU
			//

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

996
997
		block->relink_offset = (u32)GetBuffer()->GetCursorOffset();
		block->relink_data = 0;
Flyinghead's avatar
Flyinghead committed
998

999
		RelinkBlock(block);
Flyinghead's avatar
Flyinghead committed
1000

1001
		Finalize();
Flyinghead's avatar
Flyinghead committed
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
	}

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

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

1092
	void GenReadMemorySlow(u32 size)
1093
1094
	{
		Instruction *start_instruction = GetCursorAddress<Instruction *>();
1095

1096
1097
1098
		switch (size)
		{
		case 1:
1099
			if (!mmu_enabled())
1100
				GenCallRuntime(ReadMem8);
1101
1102
			else
				GenCallRuntime(ReadMemNoEx<u8>);
1103
1104
1105
1106
			Sxtb(w0, w0);
			break;

		case 2:
1107
			if (!mmu_enabled())
1108
				GenCallRuntime(ReadMem16);
1109
1110
			else
				GenCallRuntime(ReadMemNoEx<u16>);
1111
1112
1113
1114
			Sxth(w0, w0);
			break;

		case 4:
1115
			if (!mmu_enabled())
1116
				GenCallRuntime(ReadMem32);
1117
1118
			else
				GenCallRuntime(ReadMemNoEx<u32>);
1119
1120
1121
			break;

		case 8:
1122
			if (!mmu_enabled())
1123
				GenCallRuntime(ReadMem64);
1124
1125
			else
				GenCallRuntime(ReadMemNoEx<u64>);
1126
1127
1128
1129
1130
1131
1132
1133
1134
			break;

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

1135
	void GenWriteMemorySlow(u32 size)
1136
1137
	{
		Instruction *start_instruction = GetCursorAddress<Instruction *>();
1138

1139
1140
1141
		switch (size)
		{
		case 1:
1142
			if (!mmu_enabled())
1143
				GenCallRuntime(WriteMem8);
1144
1145
			else
				GenCallRuntime(WriteMemNoEx<u8>);
1146
1147
1148
			break;

		case 2:
1149
			if (!mmu_enabled())
1150
				GenCallRuntime(WriteMem16);
1151
1152
			else
				GenCallRuntime(WriteMemNoEx<u16>);
1153
1154
1155
			break;

		case 4:
1156
			if (!mmu_enabled())
1157
				GenCallRuntime(WriteMem32);
1158
1159
			else
				GenCallRuntime(WriteMemNoEx<u32>);
1160
1161
1162
			break;

		case 8:
1163
			if (!mmu_enabled())
1164
				GenCallRuntime(WriteMem64);
1165
1166
			else
				GenCallRuntime(WriteMemNoEx<u64>);
1167
1168
1169
1170
1171
1172
1173
1174
1175
			break;

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

1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
	u32 RelinkBlock(RuntimeBlockInfo *block)
	{
		ptrdiff_t start_offset = GetBuffer()->GetCursorOffset();

		switch (block->BlockType)
		{

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

				Bind(&branch_not_taken);

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

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

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

			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));
1289
			GenBranch(*arm64_no_update);
1290
1291
1292
1293
1294
1295
1296
1297

			break;

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

		return GetBuffer()->GetCursorOffset() - start_offset;
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
	}

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

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

1341
1342
1343
1344
1345
1346
	void GenMainloop()
	{
		Label no_update;
		Label intc_sched;
		Label end_mainloop;

1347
1348
		// int intc_sched()
		arm64_intc_sched = GetCursorAddress<int (*)()>();
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
		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)
1375
		mainloop = (void (*)(void *)) CC_RW2RX(GetCursorAddress<uintptr_t>());
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

		// 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);
1435
		Ldr(w0, MemOperand(x28, offsetof(Sh4Context, CpuRunning)));
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
		Ret();

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

		B(&no_update