forked from Rosalie241/parallel-rsp
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrsp_jit.cpp
2287 lines (1976 loc) · 62 KB
/
rsp_jit.cpp
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
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
216
217
218
219
220
221
222
223
224
225
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
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
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
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
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
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
665
666
667
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
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
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
916
917
918
919
920
921
922
923
924
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
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
994
995
996
997
998
999
1000
#include "rsp_jit.hpp"
#include "rsp_disasm.hpp"
#include <utility>
#include <assert.h>
using namespace std;
//#define TRACE
//#define TRACE_ENTER
//#define TRACE_DISASM
// We're only guaranteed 3 V registers (x86).
#define JIT_REGISTER_STATE JIT_V0
#define JIT_REGISTER_DMEM JIT_V1
#define JIT_REGISTER_INDIRECT_PC JIT_V2
#define JIT_REGISTER_MODE JIT_R1
#define JIT_REGISTER_NEXT_PC JIT_R0
#define JIT_FRAME_SIZE 256
#if __WORDSIZE == 32
#undef jit_ldxr_ui
#define jit_ldxr_ui jit_ldxr_i
#undef jit_ldxi_ui
#define jit_ldxi_ui jit_ldxi_i
#endif
namespace RSP
{
namespace JIT
{
CPU::CPU()
{
init_jit("RSP");
init_jit_thunks();
}
CPU::~CPU()
{
finish_jit();
}
void CPU::invalidate_imem()
{
for (unsigned i = 0; i < CODE_BLOCKS; i++)
if (memcmp(cached_imem + i * CODE_BLOCK_WORDS, state.imem + i * CODE_BLOCK_WORDS, CODE_BLOCK_SIZE))
state.dirty_blocks |= (0x3 << i) >> 1;
}
void CPU::invalidate_code()
{
if (!state.dirty_blocks)
return;
for (unsigned i = 0; i < CODE_BLOCKS; i++)
{
if (state.dirty_blocks & (1 << i))
{
memset(blocks + i * CODE_BLOCK_WORDS, 0, CODE_BLOCK_WORDS * sizeof(blocks[0]));
memcpy(cached_imem + i * CODE_BLOCK_WORDS, state.imem + i * CODE_BLOCK_WORDS, CODE_BLOCK_SIZE);
}
}
state.dirty_blocks = 0;
}
// Need super-fast hash here.
uint64_t CPU::hash_imem(unsigned pc, unsigned count) const
{
size_t size = count;
// FNV-1.
const auto *data = state.imem + pc;
uint64_t h = 0xcbf29ce484222325ull;
h = (h * 0x100000001b3ull) ^ pc;
h = (h * 0x100000001b3ull) ^ count;
for (size_t i = 0; i < size; i++)
h = (h * 0x100000001b3ull) ^ data[i];
return h;
}
#ifdef TRACE
static uint64_t hash_registers(const CPUState *rsp)
{
const auto *data = rsp->sr;
uint64_t h = 0xcbf29ce484222325ull;
for (size_t i = 1; i < 32; i++)
h = (h * 0x100000001b3ull) ^ data[i];
data = reinterpret_cast<const uint32_t *>(&rsp->cp2);
unsigned words = sizeof(rsp->cp2) >> 2;
for (size_t i = 0; i < words; i++)
h = (h * 0x100000001b3ull) ^ data[i];
return h;
}
static uint64_t hash_dmem(const CPUState *rsp)
{
const auto *data = rsp->dmem;
uint64_t h = 0xcbf29ce484222325ull;
for (size_t i = 0; i < 1024; i++)
h = (h * 0x100000001b3ull) ^ data[i];
return h;
}
#endif
unsigned CPU::analyze_static_end(unsigned pc, unsigned end)
{
// Scans through IMEM and finds the logical "end" of the instruction stream.
// A logical end of the instruction stream is where execution must terminate.
// If we have forward branches into this block, i.e. gotos, they extend the execution stream.
// However, we cannot execute beyond end.
unsigned max_static_pc = pc;
unsigned count = end - pc;
for (unsigned i = 0; i < count; i++)
{
uint32_t instr = state.imem[pc + i];
uint32_t type = instr >> 26;
uint32_t target;
bool forward_goto;
if (pc + i + 1 >= max_static_pc)
{
forward_goto = false;
max_static_pc = pc + i + 1;
}
else
forward_goto = true;
// VU
if ((instr >> 25) == 0x25)
continue;
switch (type)
{
case 000:
switch (instr & 63)
{
case 010:
case 011:
// JR and JALR always terminate execution of the block.
// We execute the next instruction via delay slot and exit.
// Unless we can branch past the JR
// (max_static_pc will be higher than expected),
// this will be the static end.
if (!forward_goto)
{
max_static_pc = max(pc + i + 2, max_static_pc);
goto end;
}
break;
case 015:
// BREAK always terminates.
if (!forward_goto)
goto end;
break;
default:
break;
}
break;
case 001: // REGIMM
switch ((instr >> 16) & 31)
{
case 000: // BLTZ
case 001: // BGEZ
case 021: // BGEZAL
case 020: // BLTZAL
// TODO/Optimization: Handle static branch case where $0 is used.
target = (pc + i + 1 + instr) & 0x3ff;
if (target >= pc && target < end) // goto
max_static_pc = max(max_static_pc, target + 1);
break;
default:
break;
}
break;
case 002: // J
case 003: // JAL
// Where we choose to end the block here is critical for performance, since otherwise
// we end up hashing a lot of garbage as it turns out ...
// J is resolved by goto. Same with JAL if call target happens to be inside the block.
target = instr & 0x3ff;
if (target >= pc && target < end) // goto
{
// J is a static jump, so if we aren't branching
// past this instruction and we're branching backwards,
// we can end the block here.
if (!forward_goto)
{
max_static_pc = max(pc + i + 2, max_static_pc);
goto end;
}
else
max_static_pc = max(max_static_pc, target + 1);
}
else if (!forward_goto)
{
// If we have static branch outside our block,
// we terminate the block.
max_static_pc = max(pc + i + 2, max_static_pc);
goto end;
}
break;
case 004: // BEQ
case 005: // BNE
case 006: // BLEZ
case 007: // BGTZ
// TODO/Optimization: Handle static branch case where $0 is used.
target = (pc + i + 1 + instr) & 0x3ff;
if (target >= pc && target < end) // goto
max_static_pc = max(max_static_pc, target + 1);
break;
default:
break;
}
}
end:
unsigned ret = min(max_static_pc, end);
return ret;
}
extern "C"
{
#define BYTE_ENDIAN_FIXUP(x, off) ((((x) + (off)) ^ 3) & 0xfffu)
static Func rsp_enter(void *cpu, unsigned pc)
{
return static_cast<CPU *>(cpu)->get_jit_block(pc);
}
static jit_word_t rsp_unaligned_lh(const uint8_t *dram, jit_word_t addr)
{
auto off0 = BYTE_ENDIAN_FIXUP(addr, 0);
auto off1 = BYTE_ENDIAN_FIXUP(addr, 1);
return jit_word_t(int16_t((dram[off0] << 8) |
(dram[off1] << 0)));
}
static jit_word_t rsp_unaligned_lw(const uint8_t *dram, jit_word_t addr)
{
auto off0 = BYTE_ENDIAN_FIXUP(addr, 0);
auto off1 = BYTE_ENDIAN_FIXUP(addr, 1);
auto off2 = BYTE_ENDIAN_FIXUP(addr, 2);
auto off3 = BYTE_ENDIAN_FIXUP(addr, 3);
// To sign extend, or not to sign extend, hm ...
return jit_word_t((int32_t(dram[off0]) << 24) |
(int32_t(dram[off1]) << 16) |
(int32_t(dram[off2]) << 8) |
(int32_t(dram[off3]) << 0));
}
static jit_uword_t rsp_unaligned_lhu(const uint8_t *dram, jit_word_t addr)
{
auto off0 = BYTE_ENDIAN_FIXUP(addr, 0);
auto off1 = BYTE_ENDIAN_FIXUP(addr, 1);
return jit_word_t(uint16_t((dram[off0] << 8) |
(dram[off1] << 0)));
}
static void rsp_unaligned_sh(uint8_t *dram, jit_word_t addr, jit_word_t data)
{
auto off0 = BYTE_ENDIAN_FIXUP(addr, 0);
auto off1 = BYTE_ENDIAN_FIXUP(addr, 1);
dram[off0] = (data >> 8) & 0xff;
dram[off1] = (data >> 0) & 0xff;
}
static void rsp_unaligned_sw(uint8_t *dram, jit_word_t addr, jit_word_t data)
{
auto off0 = BYTE_ENDIAN_FIXUP(addr, 0);
auto off1 = BYTE_ENDIAN_FIXUP(addr, 1);
auto off2 = BYTE_ENDIAN_FIXUP(addr, 2);
auto off3 = BYTE_ENDIAN_FIXUP(addr, 3);
dram[off0] = (data >> 24) & 0xff;
dram[off1] = (data >> 16) & 0xff;
dram[off2] = (data >> 8) & 0xff;
dram[off3] = (data >> 0) & 0xff;
}
#ifdef TRACE
static void rsp_report_pc(const CPUState *state, jit_uword_t pc, jit_uword_t instr)
{
auto disasm = disassemble(pc, instr);
disasm += " (" + std::to_string(hash_registers(state)) + ") (" + std::to_string(hash_dmem(state)) + ")";
puts(disasm.c_str());
}
#endif
#ifdef TRACE_ENTER
static void rsp_report_enter(jit_uword_t pc)
{
printf(" ... Enter 0x%03x ... ", unsigned(pc & 0xffcu));
}
#endif
}
void CPU::jit_save_indirect_register(jit_state_t *_jit, unsigned mips_register)
{
unsigned jit_reg = regs.load_mips_register_noext(_jit, mips_register);
jit_movr(JIT_REGISTER_INDIRECT_PC, jit_reg);
regs.unlock_mips_register(mips_register);
}
void CPU::jit_save_illegal_indirect_register(jit_state_t *_jit)
{
jit_stxi(-JIT_FRAME_SIZE + 3 * sizeof(jit_word_t), JIT_FP, JIT_REGISTER_INDIRECT_PC);
}
void CPU::jit_load_indirect_register(jit_state_t *_jit, unsigned jit_reg)
{
jit_movr(jit_reg, JIT_REGISTER_INDIRECT_PC);
}
void CPU::jit_load_illegal_indirect_register(jit_state_t *_jit, unsigned jit_reg)
{
jit_ldxi(jit_reg, JIT_FP, -JIT_FRAME_SIZE + 3 * sizeof(jit_word_t));
}
void CPU::jit_begin_call(jit_state_t *_jit)
{
// Workarounds weird Lightning behavior around register usage.
// It has been observed that EBX (V0) is clobbered on x86 Linux when
// calling out to C code.
jit_live(JIT_REGISTER_STATE);
jit_live(JIT_REGISTER_DMEM);
jit_live(JIT_REGISTER_INDIRECT_PC);
jit_prepare();
}
void CPU::jit_end_call(jit_state_t *_jit, jit_pointer_t ptr)
{
jit_finishi(ptr);
// Workarounds weird Lightning behavior around register usage.
// It has been observed that EBX (V0) is clobbered on x86 Linux when
// calling out to C code.
jit_live(JIT_REGISTER_STATE);
jit_live(JIT_REGISTER_DMEM);
jit_live(JIT_REGISTER_INDIRECT_PC);
}
void CPU::jit_save_illegal_cond_branch_taken(jit_state_t *_jit)
{
unsigned cond_reg = regs.load_mips_register_noext(_jit, RegisterCache::COND_BRANCH_TAKEN);
jit_stxi(-JIT_FRAME_SIZE + sizeof(jit_word_t), JIT_FP, cond_reg);
regs.unlock_mips_register(RegisterCache::COND_BRANCH_TAKEN);
}
void CPU::jit_restore_illegal_cond_branch_taken(jit_state_t *_jit, unsigned reg)
{
jit_ldxi(reg, JIT_FP, -JIT_FRAME_SIZE + sizeof(jit_word_t));
}
void CPU::jit_clear_illegal_cond_branch_taken(jit_state_t *_jit, unsigned tmp_reg)
{
jit_movi(tmp_reg, 0);
jit_stxi(-JIT_FRAME_SIZE + sizeof(jit_word_t), JIT_FP, tmp_reg);
}
void CPU::init_jit_thunks()
{
jit_state_t *_jit = jit_new_state();
jit_prolog();
// Saves registers from C++ code.
jit_frame(JIT_FRAME_SIZE);
auto *state = jit_arg();
// These registers remain fixed and all called thunks will poke into these registers as necessary.
jit_getarg(JIT_REGISTER_STATE, state);
jit_ldxi_i(JIT_REGISTER_NEXT_PC, JIT_REGISTER_STATE, offsetof(CPUState, pc));
jit_ldxi(JIT_REGISTER_DMEM, JIT_REGISTER_STATE, offsetof(CPUState, dmem));
// When thunks need non-local goto, they jump here.
auto *entry_label = jit_indirect();
#ifdef TRACE_ENTER
{
// Save PC.
jit_stxi_i(offsetof(CPUState, pc), JIT_REGISTER_STATE, JIT_REGISTER_NEXT_PC);
jit_prepare();
jit_pushargr(JIT_REGISTER_NEXT_PC);
jit_finishi(reinterpret_cast<jit_pointer_t>(rsp_report_enter));
jit_ldxi_i(JIT_REGISTER_NEXT_PC, JIT_REGISTER_STATE, offsetof(CPUState, pc));
}
#endif
jit_prepare();
jit_pushargr(JIT_REGISTER_STATE);
jit_pushargr(JIT_REGISTER_NEXT_PC);
jit_finishi(reinterpret_cast<jit_pointer_t>(rsp_enter));
jit_retval(JIT_REGISTER_NEXT_PC);
// Jump to thunk.
// Clear out branch delay slots.
jit_clear_illegal_cond_branch_taken(_jit, JIT_REGISTER_MODE);
jit_stxi_i(offsetof(CPUState, sr) + RegisterCache::COND_BRANCH_TAKEN * 4, JIT_REGISTER_STATE, JIT_REGISTER_MODE);
jit_jmpr(JIT_REGISTER_NEXT_PC);
// When we want to return, JIT thunks will jump here.
auto *return_label = jit_indirect();
// Save PC.
jit_stxi_i(offsetof(CPUState, pc), JIT_REGISTER_STATE, JIT_REGISTER_NEXT_PC);
// Return status. This register is considered common for all thunks.
jit_retr(JIT_REGISTER_MODE);
jit_realize();
jit_word_t code_size;
jit_get_code(&code_size);
void *thunk_code = allocator.allocate_code(code_size);
if (!thunk_code)
abort();
jit_set_code(thunk_code, code_size);
thunks.enter_frame = reinterpret_cast<int (*)(void *)>(jit_emit());
thunks.enter_thunk = jit_address(entry_label);
thunks.return_thunk = jit_address(return_label);
//printf(" === DISASM ===\n");
//jit_disassemble();
jit_clear_state();
//printf(" === END DISASM ===\n");
jit_destroy_state();
if (!Allocator::commit_code(thunk_code, code_size))
abort();
}
Func CPU::get_jit_block(uint32_t pc)
{
pc &= IMEM_SIZE - 1;
uint32_t word_pc = pc >> 2;
auto &block = blocks[word_pc];
if (!block)
{
unsigned end = (pc + (CODE_BLOCK_SIZE * 2)) >> CODE_BLOCK_SIZE_LOG2;
end <<= CODE_BLOCK_SIZE_LOG2 - 2;
end = min(end, unsigned(IMEM_SIZE >> 2));
end = analyze_static_end(word_pc, end);
uint64_t hash = hash_imem(word_pc, end - word_pc);
auto &ptr = cached_blocks[word_pc][hash];
if (ptr)
block = ptr;
else
block = ptr = jit_region(hash, word_pc, end - word_pc);
}
return block;
}
int CPU::enter(uint32_t pc)
{
// Top level enter.
state.pc = pc;
static_assert(offsetof(CPU, state) == 0, "CPU state must lie on first byte.");
int ret = thunks.enter_frame(this);
return ret;
}
void CPU::jit_end_of_block(jit_state_t *_jit, uint32_t pc, const CPU::InstructionInfo &last_info)
{
// If we run off the end of a block with a pending delay slot, we need to move it to CPUState.
// We always branch to the next PC, and the delay slot will be handled after the first instruction in next block.
unsigned cond_branch_reg = 0;
if (last_info.branch && last_info.conditional)
{
cond_branch_reg = regs.load_mips_register_noext(_jit, RegisterCache::COND_BRANCH_TAKEN);
regs.unlock_mips_register(RegisterCache::COND_BRANCH_TAKEN);
}
unsigned scratch_reg = regs.modify_mips_register(_jit, RegisterCache::SCRATCH_REGISTER0);
regs.unlock_mips_register(RegisterCache::SCRATCH_REGISTER0);
regs.flush_register_window(_jit);
jit_node_t *forward = nullptr;
if (last_info.branch)
{
if (last_info.conditional)
forward = jit_beqi(cond_branch_reg, 0);
if (last_info.indirect)
jit_load_indirect_register(_jit, scratch_reg);
else
jit_movi(scratch_reg, last_info.branch_target);
jit_stxi_i(offsetof(CPUState, branch_target), JIT_REGISTER_STATE, scratch_reg);
jit_movi(scratch_reg, 1);
jit_stxi_i(offsetof(CPUState, has_delay_slot), JIT_REGISTER_STATE, scratch_reg);
}
if (forward)
jit_patch(forward);
jit_movi(JIT_REGISTER_NEXT_PC, pc);
jit_patch_abs(jit_jmpi(), thunks.enter_thunk);
}
void CPU::jit_handle_impossible_delay_slot(jit_state_t *_jit, const InstructionInfo &info,
const InstructionInfo &last_info, uint32_t base_pc,
uint32_t end_pc)
{
unsigned cond_branch_reg = regs.load_mips_register_noext(_jit, RegisterCache::COND_BRANCH_TAKEN);
unsigned scratch_reg = regs.modify_mips_register(_jit, RegisterCache::SCRATCH_REGISTER0);
unsigned illegal_cond_reg = regs.modify_mips_register(_jit, RegisterCache::SCRATCH_REGISTER1);
regs.unlock_mips_register(RegisterCache::COND_BRANCH_TAKEN);
regs.unlock_mips_register(RegisterCache::SCRATCH_REGISTER0);
regs.unlock_mips_register(RegisterCache::SCRATCH_REGISTER1);
regs.flush_register_window(_jit);
// We can still use the registers after flushing,
// but we cannot call on the register cache any more until we resolve the branch.
// A case here would be:
// beq r0, r1, somewhere
// beq r1, r2, somewhere
// <-- we are here ...
// add r0, r1, r2
// This case should normally never happen, but you never know what happens on a fixed platform ...
// Cond branch information for the first branch is found in JIT_FP[-JIT_FRAME_SIZE].
// Cond branch information for the second branch is found in COND_BRANCH_TAKEN.
// If the first branch was taken, we will transfer control, but we will never use a local goto here
// since we potentially need to set the has_delay_slot argument.
// If the first branch is not taken, we will defer any control transfer until the next instruction, nothing happens,
// except that FP[0] is cleared.
jit_node_t *nobranch = nullptr;
if (last_info.conditional)
{
jit_restore_illegal_cond_branch_taken(_jit, illegal_cond_reg);
jit_clear_illegal_cond_branch_taken(_jit, scratch_reg);
nobranch = jit_beqi(illegal_cond_reg, 0);
}
else
jit_clear_illegal_cond_branch_taken(_jit, cond_branch_reg);
// ... But do we have a delay slot to take care of?
if (!info.conditional)
jit_movi(cond_branch_reg, 1);
jit_stxi_i(offsetof(CPUState, has_delay_slot), JIT_REGISTER_STATE, cond_branch_reg);
if (info.indirect)
jit_load_indirect_register(_jit, cond_branch_reg);
else
jit_movi(cond_branch_reg, info.branch_target);
jit_stxi_i(offsetof(CPUState, branch_target), JIT_REGISTER_STATE, cond_branch_reg);
// We are done with register use.
// Here we *will* take the branch.
if (last_info.indirect)
jit_load_illegal_indirect_register(_jit, JIT_REGISTER_NEXT_PC);
else
jit_movi(JIT_REGISTER_NEXT_PC, last_info.branch_target);
jit_patch_abs(jit_jmpi(), thunks.enter_thunk);
if (nobranch)
jit_patch(nobranch);
}
void CPU::jit_handle_delay_slot(jit_state_t *_jit, const InstructionInfo &last_info,
uint32_t base_pc, uint32_t end_pc)
{
unsigned scratch_cond_reg = 0;
if (last_info.conditional)
{
regs.load_mips_register_noext(_jit, RegisterCache::COND_BRANCH_TAKEN);
unsigned cond_branch_reg = regs.modify_mips_register(_jit, RegisterCache::COND_BRANCH_TAKEN);
scratch_cond_reg = regs.modify_mips_register(_jit, RegisterCache::SCRATCH_REGISTER0);
// Clear out branch state.
jit_movr(scratch_cond_reg, cond_branch_reg);
jit_movi(cond_branch_reg, 0);
regs.unlock_mips_register(RegisterCache::COND_BRANCH_TAKEN);
regs.unlock_mips_register(RegisterCache::COND_BRANCH_TAKEN);
regs.unlock_mips_register(RegisterCache::SCRATCH_REGISTER0);
}
else
{
unsigned cond_branch_reg = regs.modify_mips_register(_jit, RegisterCache::COND_BRANCH_TAKEN);
jit_movi(cond_branch_reg, 0);
regs.unlock_mips_register(RegisterCache::COND_BRANCH_TAKEN);
}
regs.flush_register_window(_jit);
if (last_info.conditional)
{
if (!last_info.indirect && last_info.branch_target >= base_pc && last_info.branch_target < end_pc)
{
// Patch this up later.
unsigned local_index = (last_info.branch_target - base_pc) >> 2;
local_branches.push_back({ jit_bnei(scratch_cond_reg, 0), local_index });
}
else
{
auto *no_branch = jit_beqi(scratch_cond_reg, 0);
if (last_info.indirect)
jit_load_indirect_register(_jit, JIT_REGISTER_NEXT_PC);
else
jit_movi(JIT_REGISTER_NEXT_PC, last_info.branch_target);
jit_patch_abs(jit_jmpi(), thunks.enter_thunk);
jit_patch(no_branch);
}
}
else
{
if (!last_info.indirect && last_info.branch_target >= base_pc && last_info.branch_target < end_pc)
{
// Patch this up later.
unsigned local_index = (last_info.branch_target - base_pc) >> 2;
local_branches.push_back({ jit_jmpi(), local_index });
}
else
{
if (last_info.indirect)
jit_load_indirect_register(_jit, JIT_REGISTER_NEXT_PC);
else
jit_movi(JIT_REGISTER_NEXT_PC, last_info.branch_target);
jit_patch_abs(jit_jmpi(), thunks.enter_thunk);
}
}
}
void CPU::jit_exit(jit_state_t *_jit, uint32_t pc, const InstructionInfo &last_info,
ReturnMode mode, bool first_instruction)
{
regs.flush_register_window(_jit);
jit_movi(JIT_REGISTER_MODE, mode);
jit_exit_dynamic(_jit, pc, last_info, first_instruction);
}
void CPU::jit_exit_dynamic(jit_state_t *_jit, uint32_t pc, const InstructionInfo &last_info, bool first_instruction)
{
// We must not touch REGISTER_MODE / TMP1 here, fortunately we don't need to.
if (first_instruction)
{
// Need to consider that we need to move delay slot to PC.
jit_ldxi_i(JIT_REGISTER_NEXT_PC, JIT_REGISTER_STATE, offsetof(CPUState, has_delay_slot));
auto *latent_delay_slot = jit_bnei(JIT_REGISTER_NEXT_PC, 0);
// Common case.
// Immediately exit.
jit_movi(JIT_REGISTER_NEXT_PC, (pc + 4) & 0xffcu);
jit_patch_abs(jit_jmpi(), thunks.return_thunk);
// If we had a latent delay slot, we handle it here.
jit_patch(latent_delay_slot);
// jit_exit is never called from a branch instruction, so we do not have to handle double branch delay slots here.
jit_movi(JIT_REGISTER_NEXT_PC, 0);
jit_stxi_i(offsetof(CPUState, has_delay_slot), JIT_REGISTER_STATE, JIT_REGISTER_NEXT_PC);
jit_ldxi_i(JIT_REGISTER_NEXT_PC, JIT_REGISTER_STATE, offsetof(CPUState, branch_target));
}
else if (!last_info.branch)
{
// Immediately exit.
jit_movi(JIT_REGISTER_NEXT_PC, (pc + 4) & 0xffcu);
}
else if (!last_info.indirect && !last_info.conditional)
{
// Redirect PC to whatever value we were supposed to branch to.
jit_movi(JIT_REGISTER_NEXT_PC, last_info.branch_target);
}
else if (!last_info.conditional)
{
// We have an indirect branch, load that register into PC.
jit_load_indirect_register(_jit, JIT_REGISTER_NEXT_PC);
}
else if (last_info.indirect)
{
// Indirect conditional branch.
jit_ldxi_i(JIT_REGISTER_NEXT_PC, JIT_REGISTER_STATE,
offsetof(CPUState, sr) + RegisterCache::COND_BRANCH_TAKEN * 4);
auto *node = jit_beqi(JIT_REGISTER_NEXT_PC, 0);
jit_load_indirect_register(_jit, JIT_REGISTER_NEXT_PC);
auto *to_end = jit_jmpi();
jit_patch(node);
jit_movi(JIT_REGISTER_NEXT_PC, (pc + 4) & 0xffcu);
jit_patch(to_end);
}
else
{
// Direct conditional branch.
jit_ldxi_i(JIT_REGISTER_NEXT_PC, JIT_REGISTER_STATE,
offsetof(CPUState, sr) + RegisterCache::COND_BRANCH_TAKEN * 4);
auto *node = jit_beqi(JIT_REGISTER_NEXT_PC, 0);
jit_movi(JIT_REGISTER_NEXT_PC, last_info.branch_target);
auto *to_end = jit_jmpi();
jit_patch(node);
jit_movi(JIT_REGISTER_NEXT_PC, (pc + 4) & 0xffcu);
jit_patch(to_end);
}
jit_patch_abs(jit_jmpi(), thunks.return_thunk);
}
void CPU::jit_emit_store_operation(jit_state_t *_jit,
uint32_t pc, uint32_t instr,
void (*jit_emitter)(jit_state_t *jit, unsigned, unsigned, unsigned), const char *asmop,
jit_pointer_t rsp_unaligned_op,
uint32_t endian_flip,
const InstructionInfo &last_info)
{
uint32_t align_mask = 3 - endian_flip;
unsigned rt = (instr >> 16) & 31;
int16_t simm = int16_t(instr);
unsigned rs = (instr >> 21) & 31;
unsigned rt_reg = regs.load_mips_register_noext(_jit, rt);
unsigned rs_reg = regs.load_mips_register_noext(_jit, rs);
unsigned rs_tmp_reg = regs.modify_mips_register(_jit, RegisterCache::SCRATCH_REGISTER0);
jit_addi(rs_tmp_reg, rs_reg, simm);
jit_andi(rs_tmp_reg, rs_tmp_reg, 0xfffu);
// If we are unaligned, it gets very messy to JIT, so just thunk it out to C code.
jit_node_t *unaligned = nullptr;
if (align_mask)
{
regs.unlock_mips_register(rt);
regs.unlock_mips_register(rs);
regs.unlock_mips_register(RegisterCache::SCRATCH_REGISTER0);
// We're going to call, so need to save caller-save register we care about.
regs.flush_caller_save_registers(_jit);
unaligned = jit_bmsi(rs_tmp_reg, align_mask);
}
// The MIPS is big endian, but the words are swapped per word in integration, so it's kinda little-endian,
// except we need to XOR the address for byte and half-word accesses.
if (endian_flip != 0)
jit_xori(rs_tmp_reg, rs_tmp_reg, endian_flip);
jit_emitter(_jit, rs_tmp_reg, JIT_REGISTER_DMEM, rt_reg);
jit_node_t *aligned = nullptr;
if (align_mask)
{
aligned = jit_jmpi();
jit_patch(unaligned);
jit_begin_call(_jit);
jit_pushargr(JIT_REGISTER_DMEM);
jit_pushargr(rs_tmp_reg);
jit_pushargr(rt_reg);
jit_end_call(_jit, rsp_unaligned_op);
jit_patch(aligned);
}
else
{
regs.unlock_mips_register(rt);
regs.unlock_mips_register(rs);
regs.unlock_mips_register(RegisterCache::SCRATCH_REGISTER0);
}
}
// The RSP may or may not have a load-delay slot, but it doesn't seem to matter in practice, so just emulate without
// a load-delay slot.
void CPU::jit_emit_load_operation(jit_state_t *_jit,
uint32_t pc, uint32_t instr,
void (*jit_emitter)(jit_state_t *jit, unsigned, unsigned, unsigned), const char *asmop,
jit_pointer_t rsp_unaligned_op,
uint32_t endian_flip,
const InstructionInfo &last_info)
{
uint32_t align_mask = endian_flip ^ 3;
unsigned rt = (instr >> 16) & 31;
if (rt == 0)
return;
int16_t simm = int16_t(instr);
unsigned rs = (instr >> 21) & 31;
unsigned rs_reg = regs.load_mips_register_noext(_jit, rs);
unsigned rs_tmp_reg = regs.modify_mips_register(_jit, RegisterCache::SCRATCH_REGISTER0);
jit_addi(rs_tmp_reg, rs_reg, simm);
jit_andi(rs_tmp_reg, rs_tmp_reg, 0xfffu);
unsigned ret_reg = regs.modify_mips_register(_jit, RegisterCache::SCRATCH_REGISTER1);
// If we are unaligned, it gets very messy to JIT, so just thunk it out to C code.
jit_node_t *unaligned = nullptr;
if (align_mask)
{
// Flush the register cache here since we might call.
// We will still use rs_reg/rt_reg, but they only live for this short burst only.
regs.unlock_mips_register(rs);
regs.unlock_mips_register(RegisterCache::SCRATCH_REGISTER0);
regs.unlock_mips_register(RegisterCache::SCRATCH_REGISTER1);
regs.flush_caller_save_registers(_jit);
unaligned = jit_bmsi(rs_tmp_reg, align_mask);
}
// The MIPS is big endian, but the words are swapped per word in integration, so it's kinda little-endian,
// except we need to XOR the address for byte and half-word accesses.
if (endian_flip != 0)
jit_xori(rs_tmp_reg, rs_tmp_reg, endian_flip);
jit_emitter(_jit, ret_reg, JIT_REGISTER_DMEM, rs_tmp_reg);
jit_node_t *aligned = nullptr;
if (align_mask)
{
aligned = jit_jmpi();
jit_patch(unaligned);
}
if (align_mask)
{
// We're going to call, so need to save caller-save register we care about.
jit_begin_call(_jit);
jit_pushargr(JIT_REGISTER_DMEM);
jit_pushargr(rs_tmp_reg);
jit_end_call(_jit, rsp_unaligned_op);
jit_retval(ret_reg);
jit_patch(aligned);
}
else
{
regs.unlock_mips_register(rs);
regs.unlock_mips_register(RegisterCache::SCRATCH_REGISTER0);
regs.unlock_mips_register(RegisterCache::SCRATCH_REGISTER1);
}
unsigned rt_reg = regs.modify_mips_register(_jit, rt);
jit_movr(rt_reg, ret_reg);
regs.unlock_mips_register(rt);
}
void CPU::jit_instruction(jit_state_t *_jit, uint32_t pc, uint32_t instr,
InstructionInfo &info, const InstructionInfo &last_info,
bool first_instruction, bool next_instruction_is_branch_target)
{
#ifdef TRACE
regs.flush_register_window(_jit);
jit_begin_call(_jit);
jit_pushargr(JIT_REGISTER_STATE);
jit_pushargi(pc);
jit_pushargi(instr);
jit_end_call(_jit, reinterpret_cast<jit_pointer_t>(rsp_report_pc));
#endif
// VU
if ((instr >> 25) == 0x25)
{
// VU instruction. COP2, and high bit of opcode is set.
uint32_t op = instr & 63;
uint32_t vd = (instr >> 6) & 31;
uint32_t vs = (instr >> 11) & 31;
uint32_t vt = (instr >> 16) & 31;
uint32_t e = (instr >> 21) & 15;
using VUOp = void (*)(RSP::CPUState *, unsigned vd, unsigned vs, unsigned vt, unsigned e);
static const VUOp ops[64] = {
RSP_VMULF, RSP_VMULU, nullptr, nullptr, RSP_VMUDL, RSP_VMUDM, RSP_VMUDN, RSP_VMUDH, RSP_VMACF, RSP_VMACU, nullptr,
nullptr, RSP_VMADL, RSP_VMADM, RSP_VMADN, RSP_VMADH, RSP_VADD, RSP_VSUB, nullptr, RSP_VABS, RSP_VADDC, RSP_VSUBC,
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, RSP_VSAR, nullptr, nullptr, RSP_VLT,
RSP_VEQ, RSP_VNE, RSP_VGE, RSP_VCL, RSP_VCH, RSP_VCR, RSP_VMRG, RSP_VAND, RSP_VNAND, RSP_VOR, RSP_VNOR,
RSP_VXOR, RSP_VNXOR, nullptr, nullptr, RSP_VRCP, RSP_VRCPL, RSP_VRCPH, RSP_VMOV, RSP_VRSQ, RSP_VRSQL, RSP_VRSQH,
RSP_VNOP,
};
auto *vuop = ops[op];
if (!vuop)
vuop = RSP_RESERVED;
regs.flush_caller_save_registers(_jit);
jit_begin_call(_jit);
jit_pushargr(JIT_REGISTER_STATE);
jit_pushargi(vd);
jit_pushargi(vs);
jit_pushargi(vt);
jit_pushargi(e);
jit_end_call(_jit ,reinterpret_cast<jit_pointer_t>(vuop));
return;
}
// TODO: Meaningful register allocation.
// For now, always flush register state to memory after an instruction for simplicity.
// Should be red-hot in L1 cache, so probably won't be that bad.
// On x86 and x64, we unfortunately have an anemic register bank to work with in Lightning.
uint32_t type = instr >> 26;
#define NOP_IF_RD_ZERO() if (rd == 0) { break; }
#define NOP_IF_RT_ZERO() if (rt == 0) { break; }
switch (type)
{
case 000:
{
auto rd = (instr >> 11) & 31;
auto rt = (instr >> 16) & 31;
auto shift = (instr >> 6) & 31;
auto rs = (instr >> 21) & 31;
switch (instr & 63)
{
case 000: // SLL
{
NOP_IF_RD_ZERO();
unsigned rt_reg = regs.load_mips_register_noext(_jit, rt);
unsigned rd_reg = regs.modify_mips_register(_jit, rd);
jit_lshi(rd_reg, rt_reg, shift);
regs.unlock_mips_register(rt);
regs.unlock_mips_register(rd);
break;
}
case 002: // SRL
{
NOP_IF_RD_ZERO();
unsigned rt_reg = regs.load_mips_register_zext(_jit, rt);
unsigned rd_reg = regs.modify_mips_register(_jit, rd);
jit_rshi_u(rd_reg, rt_reg, shift);
regs.unlock_mips_register(rt);
regs.unlock_mips_register(rd);
break;
}
case 003: // SRA
{
NOP_IF_RD_ZERO();
unsigned rt_reg = regs.load_mips_register_sext(_jit, rt);
unsigned rd_reg = regs.modify_mips_register(_jit, rd);
jit_rshi(rd_reg, rt_reg, shift);
regs.unlock_mips_register(rt);
regs.unlock_mips_register(rd);
break;
}
case 004: // SLLV
{
NOP_IF_RD_ZERO();
unsigned rt_reg = regs.load_mips_register_noext(_jit, rt);
unsigned rs_reg = regs.load_mips_register_noext(_jit, rs);
unsigned rs_tmp_reg = regs.modify_mips_register(_jit, RegisterCache::SCRATCH_REGISTER0);
jit_andi(rs_tmp_reg, rs_reg, 31);
regs.unlock_mips_register(rs);
unsigned rd_reg = regs.modify_mips_register(_jit, rd);
jit_lshr(rd_reg, rt_reg, rs_tmp_reg);
regs.unlock_mips_register(rt);
regs.unlock_mips_register(rd);
regs.unlock_mips_register(RegisterCache::SCRATCH_REGISTER0);
break;
}
case 006: // SRLV
{
NOP_IF_RD_ZERO();
unsigned rt_reg = regs.load_mips_register_zext(_jit, rt);
unsigned rs_reg = regs.load_mips_register_noext(_jit, rs);
unsigned rs_tmp_reg = regs.modify_mips_register(_jit, RegisterCache::SCRATCH_REGISTER0);
jit_andi(rs_tmp_reg, rs_reg, 31);
regs.unlock_mips_register(rs);
unsigned rd_reg = regs.modify_mips_register(_jit, rd);
jit_rshr_u(rd_reg, rt_reg, rs_tmp_reg);
regs.unlock_mips_register(rt);
regs.unlock_mips_register(rd);
regs.unlock_mips_register(RegisterCache::SCRATCH_REGISTER0);
break;
}
case 007: // SRAV
{
unsigned rt_reg = regs.load_mips_register_sext(_jit, rt);
unsigned rs_reg = regs.load_mips_register_noext(_jit, rs);
unsigned rs_tmp_reg = regs.modify_mips_register(_jit, RegisterCache::SCRATCH_REGISTER0);
jit_andi(rs_tmp_reg, rs_reg, 31);
regs.unlock_mips_register(rs);
unsigned rd_reg = regs.modify_mips_register(_jit, rd);
jit_rshr(rd_reg, rt_reg, rs_tmp_reg);
regs.unlock_mips_register(rt);
regs.unlock_mips_register(rd);
regs.unlock_mips_register(RegisterCache::SCRATCH_REGISTER0);
break;
}