-
Notifications
You must be signed in to change notification settings - Fork 16
/
jit_compiler.c
1978 lines (1789 loc) · 66.2 KB
/
jit_compiler.c
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 "jit_compiler.h"
#include "bbc_options.h"
#include "defs_6502.h"
#include "jit_metadata.h"
#include "jit_opcode.h"
#include "jit_optimizer.h"
#include "log.h"
#include "memory_access.h"
#include "os_fault_platform.h"
#include "state_6502.h"
#include "timing.h"
#include "util.h"
#include "asm/asm_common.h"
#include "asm/asm_defs_host.h"
#include "asm/asm_inturbo.h"
#include "asm/asm_jit.h"
#include "asm/asm_jit_defs.h"
#include "asm/asm_opcodes.h"
#include "asm/asm_util.h"
#include <assert.h>
#include <string.h>
enum {
k_opcode_history_length = 8,
};
struct jit_compile_history {
uint64_t times[k_opcode_history_length];
int32_t opcodes[k_opcode_history_length];
uint8_t was_self_modified[k_opcode_history_length];
uint32_t ring_buffer_index;
int32_t opcode;
};
enum {
k_max_addr_space_per_compile = 256,
};
enum {
k_addr_flag_block_start = 1,
k_addr_flag_block_continuation = 2,
k_addr_flag_has_countdown = 4,
k_addr_flag_has_fixups = 8,
k_addr_flag_has_history = 16,
};
struct jit_compiler {
struct asm_jit_struct* p_asm;
struct timing_struct* p_timing;
struct memory_access* p_memory_access;
struct jit_metadata* p_metadata;
uint8_t* p_mem_read;
int debug;
int log_dynamic;
uint8_t* p_opcode_types;
uint8_t* p_opcode_modes;
uint8_t* p_opcode_mem;
uint8_t* p_opcode_cycles;
int option_accurate_timings;
int option_no_optimize;
int option_no_dynamic_operand;
int option_no_dynamic_opcode;
int option_no_sub_instruction;
int option_no_encoded_callback;
int option_no_collapse_loops;
uint32_t max_6502_opcodes_per_block;
uint32_t dynamic_trigger;
struct util_buffer* p_tmp_buf;
struct util_buffer* p_single_uopcode_buf;
struct util_buffer* p_single_uopcode_epilog_buf;
uint32_t len_asm_jmp;
uint32_t len_asm_invalidated;
uint32_t len_asm_nop;
int compile_for_code_in_zero_page;
uint8_t addr_flags[k_6502_addr_space_size];
struct jit_compile_history history[k_6502_addr_space_size];
int32_t addr_cycles_fixup[k_6502_addr_space_size];
int32_t addr_countdown_adjustment_fixup[k_6502_addr_space_size];
int32_t addr_nz_fixup[k_6502_addr_space_size];
int32_t addr_v_fixup[k_6502_addr_space_size];
int32_t addr_c_fixup[k_6502_addr_space_size];
int32_t addr_a_fixup[k_6502_addr_space_size];
int32_t addr_x_fixup[k_6502_addr_space_size];
int32_t addr_y_fixup[k_6502_addr_space_size];
/* State used within compilation routines and subroutines. */
struct jit_opcode_details opcode_details[k_max_addr_space_per_compile];
uint16_t start_addr_6502;
int32_t sub_instruction_addr_6502;
};
struct jit_compiler*
jit_compiler_create(struct asm_jit_struct* p_asm,
struct timing_struct* p_timing,
struct memory_access* p_memory_access,
struct jit_metadata* p_metadata,
struct bbc_options* p_options,
int debug,
uint8_t* p_opcode_types,
uint8_t* p_opcode_modes,
uint8_t* p_opcode_mem,
uint8_t* p_opcode_cycles) {
struct util_buffer* p_tmp_buf;
uint8_t buf[256];
struct asm_uop tmp_uop;
uint32_t max_6502_opcodes_per_block = 65536;
uint32_t dynamic_trigger = 4;
struct jit_compiler* p_compiler = util_mallocz(sizeof(struct jit_compiler));
/* Check invariants required for compact code generation. */
assert(K_JIT_CONTEXT_OFFSET_JIT_PTRS < 0x80);
p_compiler->p_asm = p_asm;
p_compiler->p_timing = p_timing;
p_compiler->p_memory_access = p_memory_access;
p_compiler->p_metadata = p_metadata;
p_compiler->p_mem_read = p_memory_access->p_mem_read;
p_compiler->debug = debug;
p_compiler->p_opcode_types = p_opcode_types;
p_compiler->p_opcode_modes = p_opcode_modes;
p_compiler->p_opcode_mem = p_opcode_mem;
p_compiler->p_opcode_cycles = p_opcode_cycles;
p_compiler->option_accurate_timings = util_has_option(p_options->p_opt_flags,
"jit:accurate-timings");
if (p_options->accurate) {
p_compiler->option_accurate_timings = 1;
}
p_compiler->option_no_optimize = util_has_option(p_options->p_opt_flags,
"jit:no-optimize");
p_compiler->option_no_dynamic_operand =
util_has_option(p_options->p_opt_flags, "jit:no-dynamic-operand");
p_compiler->option_no_dynamic_opcode =
util_has_option(p_options->p_opt_flags, "jit:no-dynamic-opcode");
p_compiler->option_no_sub_instruction =
util_has_option(p_options->p_opt_flags, "jit:no-sub-instruction");
p_compiler->option_no_encoded_callback =
util_has_option(p_options->p_opt_flags, "jit:no-encoded-callback");
p_compiler->option_no_collapse_loops =
util_has_option(p_options->p_opt_flags, "jit:no-collapse-loops");
assert(asm_inturbo_is_enabled());
p_compiler->log_dynamic = util_has_option(p_options->p_log_flags,
"jit:dynamic");
(void) util_get_u32_option(&max_6502_opcodes_per_block,
p_options->p_opt_flags,
"jit:max-ops");
if (max_6502_opcodes_per_block < 1) {
max_6502_opcodes_per_block = 1;
}
p_compiler->max_6502_opcodes_per_block = max_6502_opcodes_per_block;
(void) util_get_u32_option(&dynamic_trigger,
p_options->p_opt_flags,
"jit:dynamic-trigger=");
if (dynamic_trigger < 1) {
dynamic_trigger = 1;
}
p_compiler->dynamic_trigger = dynamic_trigger;
p_compiler->compile_for_code_in_zero_page = 0;
p_tmp_buf = util_buffer_create();
p_compiler->p_tmp_buf = p_tmp_buf;
p_compiler->p_single_uopcode_buf = util_buffer_create();
p_compiler->p_single_uopcode_epilog_buf = util_buffer_create();
/* Calculate lengths of sequences we need to know. */
util_buffer_setup(p_tmp_buf, &buf[0], sizeof(buf));
util_buffer_set_base_address(p_tmp_buf, NULL);
/* Note: target pointer is a short jump range, in case the asm backend emits
* a shorter sequence for that.
*/
asm_make_uop0(&tmp_uop, k_opcode_JMP);
asm_emit_jit(p_compiler->p_asm, p_tmp_buf, NULL, &tmp_uop);
p_compiler->len_asm_jmp = util_buffer_get_pos(p_tmp_buf);
util_buffer_setup(p_tmp_buf, &buf[0], sizeof(buf));
asm_emit_jit_invalidated(p_tmp_buf);
p_compiler->len_asm_invalidated = util_buffer_get_pos(p_tmp_buf);
util_buffer_setup(p_tmp_buf, &buf[0], sizeof(buf));
asm_emit_instruction_REAL_NOP(p_tmp_buf);
p_compiler->len_asm_nop = util_buffer_get_pos(p_tmp_buf);
assert(p_compiler->len_asm_nop > 0);
return p_compiler;
}
void
jit_compiler_destroy(struct jit_compiler* p_compiler) {
util_buffer_destroy(p_compiler->p_tmp_buf);
util_buffer_destroy(p_compiler->p_single_uopcode_buf);
util_buffer_destroy(p_compiler->p_single_uopcode_epilog_buf);
util_free(p_compiler);
}
static void
jit_compiler_get_opcode_details(struct jit_compiler* p_compiler,
struct jit_opcode_details* p_details,
uint16_t addr_6502) {
uint8_t opcode_6502;
uint16_t operand_6502;
uint8_t optype;
uint8_t opmode;
uint8_t opmem;
uint8_t opreg;
int is_read;
int is_write;
int is_addr_known;
struct memory_access* p_memory_access = p_compiler->p_memory_access;
uint8_t* p_mem_read = p_compiler->p_mem_read;
void* p_memory_callback = p_memory_access->p_callback_obj;
uint16_t addr_plus_1 = (addr_6502 + 1);
uint16_t addr_plus_2 = (addr_6502 + 2);
struct asm_uop* p_uop = &p_details->uops[0];
struct asm_uop* p_first_post_debug_uop = p_uop;
int use_interp = 0;
int use_inturbo = 0;
int uses_callback = 0;
int could_page_cross = 1;
uint16_t rel_target_6502 = 0;
uintptr_t jit_addr = 0;
uint32_t num_callback_uops = 0;
int jit_encoding_ends_block = 0;
uint32_t jit_encoding_extra_cycles = 0;
(void) memset(p_details, '\0', sizeof(struct jit_opcode_details));
p_details->addr_6502 = addr_6502;
p_details->branch_addr_6502 = -1;
p_details->min_6502_addr = -1;
p_details->max_6502_addr = -1;
opcode_6502 = p_mem_read[addr_6502];
optype = p_compiler->p_opcode_types[opcode_6502];
opmode = p_compiler->p_opcode_modes[opcode_6502];
opmem = p_compiler->p_opcode_mem[opcode_6502];
opreg = g_optype_sets_register[optype];
if (opmode == k_acc) {
opreg = k_a;
}
is_read = !!(opmem & k_opmem_read_flag);
is_write = !!(opmem & k_opmem_write_flag);
p_details->opcode_6502 = opcode_6502;
p_details->optype_6502 = optype;
p_details->opmode_6502 = opmode;
p_details->opreg_6502 = opreg;
p_details->opbranch_6502 = g_opbranch[optype];
p_details->opmem_6502 = opmem;
p_details->num_bytes_6502 = g_opmodelens[opmode];
p_details->ends_block = 0;
if (p_details->opbranch_6502 == k_bra_y) {
p_details->ends_block = 1;
}
/* Don't try and handle address space wraps in opcode fetch. */
if ((addr_6502 + p_details->num_bytes_6502) >= k_6502_addr_space_size) {
use_interp = 1;
p_details->num_bytes_6502 = (k_6502_addr_space_size - addr_6502);
}
p_details->p_host_prefix_start = NULL;
p_details->p_host_opcode_start = NULL;
p_details->cycles_run_start = -1;
p_details->countdown_adjustment = -1;
p_details->is_eliminated = 0;
p_details->reg_a = -1;
p_details->reg_x = -1;
p_details->reg_y = -1;
p_details->nz_flags_location = -1;
p_details->c_flag_location = 0;
p_details->v_flag_location = 0;
if (p_compiler->debug) {
asm_make_uop1(p_uop, k_opcode_debug, addr_6502);
p_uop++;
p_first_post_debug_uop = p_uop;
}
if ((optype == k_adc) || (optype == k_sbc)) {
asm_make_uop1(p_uop, k_opcode_check_bcd, addr_6502);
p_uop++;
}
if (g_optype_uses_carry[optype]) {
asm_make_uop0(p_uop, k_opcode_load_carry);
p_uop++;
}
if (g_optype_uses_overflow[optype]) {
asm_make_uop0(p_uop, k_opcode_load_overflow);
p_uop++;
}
/* Mode resolution and possibly per-mode uops. */
operand_6502 = 0;
switch (opmode) {
case 0:
case k_nil:
case k_acc:
break;
case k_imm:
if (optype == k_brk) {
break;
}
operand_6502 = p_mem_read[addr_plus_1];
asm_make_uop1(p_uop, k_opcode_value_set, operand_6502);
p_uop++;
break;
case k_zpg:
operand_6502 = p_mem_read[addr_plus_1];
p_details->min_6502_addr = operand_6502;
p_details->max_6502_addr = operand_6502;
asm_make_uop1(p_uop, k_opcode_addr_set, operand_6502);
p_uop++;
break;
case k_zpx:
operand_6502 = p_mem_read[addr_plus_1];
p_details->min_6502_addr = 0;
p_details->max_6502_addr = 0xFF;
asm_make_uop1(p_uop, k_opcode_addr_set, operand_6502);
p_uop++;
asm_make_uop0(p_uop, k_opcode_addr_add_x_8bit);
p_uop++;
break;
case k_zpy:
operand_6502 = p_mem_read[addr_plus_1];
p_details->min_6502_addr = 0;
p_details->max_6502_addr = 0xFF;
asm_make_uop1(p_uop, k_opcode_addr_set, operand_6502);
p_uop++;
asm_make_uop0(p_uop, k_opcode_addr_add_y_8bit);
p_uop++;
break;
case k_rel:
operand_6502 = p_mem_read[addr_plus_1];
rel_target_6502 = ((int) addr_6502 + 2 + (int8_t) operand_6502);
p_details->branch_addr_6502 = rel_target_6502;
jit_addr =
(uintptr_t) jit_metadata_get_host_block_address(p_compiler->p_metadata,
rel_target_6502);
break;
case k_abs:
case k_abx:
case k_aby:
operand_6502 = ((p_mem_read[addr_plus_2] << 8) | p_mem_read[addr_plus_1]);
p_details->min_6502_addr = operand_6502;
p_details->max_6502_addr = operand_6502;
if ((optype == k_jmp) || (optype == k_jsr)) {
/* JMP / JSR abs isn't really "abs" as it's not fetching data from the
* memory address.
*/
break;
}
asm_make_uop1(p_uop, k_opcode_addr_set, operand_6502);
p_uop++;
if ((operand_6502 & 0xFF) == 0x00) {
could_page_cross = 0;
}
if (opmode == k_abx) {
asm_make_uop0(p_uop, k_opcode_addr_add_x);
p_uop++;
}
if (opmode == k_aby) {
asm_make_uop0(p_uop, k_opcode_addr_add_y);
p_uop++;
}
if (opmode == k_abx || opmode == k_aby) {
p_details->max_6502_addr += 0xFF;
if (p_details->max_6502_addr > 0xFFFF) {
p_details->min_6502_addr = (p_details->max_6502_addr & 0xFFFF);
p_details->max_6502_addr = operand_6502;
/* Use the interpreter for address space wraps. Otherwise the JIT code
* will do an out-of-bounds access.
*/
use_interp = 1;
}
}
if (is_read) {
if (p_memory_access->memory_read_needs_callback(
p_memory_callback, p_details->min_6502_addr)) {
uses_callback = 1;
}
if (p_memory_access->memory_read_needs_callback(
p_memory_callback, p_details->max_6502_addr)) {
uses_callback = 1;
}
}
if (is_write) {
if (p_memory_access->memory_write_needs_callback(
p_memory_callback, p_details->min_6502_addr)) {
uses_callback = 1;
}
if (p_memory_access->memory_write_needs_callback(
p_memory_callback, p_details->max_6502_addr)) {
uses_callback = 1;
}
}
break;
case k_ind:
operand_6502 = ((p_mem_read[addr_plus_2] << 8) | p_mem_read[addr_plus_1]);
p_details->min_6502_addr = 0;
p_details->max_6502_addr = 0xFFFF;
asm_make_uop1(p_uop, k_opcode_addr_set, operand_6502);
p_uop++;
asm_make_uop0(p_uop, k_opcode_value_load_16bit_wrap);
p_uop++;
break;
case k_idx:
operand_6502 = p_mem_read[addr_plus_1];
p_details->min_6502_addr = 0;
p_details->max_6502_addr = 0xFFFF;
asm_make_uop1(p_uop, k_opcode_addr_set, operand_6502);
p_uop++;
asm_make_uop0(p_uop, k_opcode_addr_add_x_8bit);
p_uop++;
asm_make_uop0(p_uop, k_opcode_addr_load_16bit_wrap);
p_uop++;
asm_make_uop1(p_uop, k_opcode_addr_check, addr_6502);
p_uop++;
break;
case k_idy:
operand_6502 = p_mem_read[addr_plus_1];
p_details->min_6502_addr = 0;
p_details->max_6502_addr = 0xFFFF;
asm_make_uop1(p_uop, k_opcode_addr_set, operand_6502);
p_uop++;
asm_make_uop0(p_uop, k_opcode_addr_base_load_16bit_wrap);
p_uop++;
asm_make_uop0(p_uop, k_opcode_addr_add_base_y);
p_uop++;
asm_make_uop1(p_uop, k_opcode_addr_check, addr_6502);
p_uop++;
break;
default:
assert(0);
break;
}
if (is_read) {
asm_make_uop0(p_uop, k_opcode_value_load);
p_uop++;
}
p_details->max_cycles = p_compiler->p_opcode_cycles[opcode_6502];
p_details->operand_6502 = operand_6502;
is_addr_known = (p_details->min_6502_addr == p_details->max_6502_addr);
if (uses_callback) {
use_interp = 1;
}
if (uses_callback &&
is_addr_known &&
is_read &&
!is_write &&
!p_compiler->option_no_encoded_callback) {
uint32_t uops_left = (&p_details->uops[k_max_uops_per_opcode] - p_uop);
/* Write the uops on top of the addr_set and value_load. */
num_callback_uops =
p_memory_access->memory_get_read_jit_encoding(
p_memory_callback,
(p_uop - 2),
&jit_encoding_ends_block,
&jit_encoding_extra_cycles,
(uops_left + 2),
p_details->min_6502_addr,
p_compiler->option_accurate_timings);
if (num_callback_uops > 0) {
p_uop -= 2;
p_uop += num_callback_uops;
}
}
if (p_compiler->option_accurate_timings) {
if ((opmem == k_opmem_read_flag) &&
(opmode == k_abx || opmode == k_aby || opmode == k_idy) &&
could_page_cross) {
p_details->max_cycles++;
} else if (opmode == k_rel) {
/* Taken branches take 1 cycles longer, or 2 cycles longer if there's
* also a page crossing.
*/
if (((addr_6502 + 2) >> 8) ^ (rel_target_6502 >> 8)) {
p_details->max_cycles += 2;
} else {
p_details->max_cycles++;
}
}
}
/* Per-type uops. */
switch (optype) {
case k_adc: asm_make_uop0(p_uop, k_opcode_ADC); p_uop++; break;
case k_alr: asm_make_uop0(p_uop, k_opcode_ALR); p_uop++; break;
case k_and: asm_make_uop0(p_uop, k_opcode_AND); p_uop++; break;
case k_asl:
if (opmode == k_acc) {
asm_make_uop1(p_uop, k_opcode_ASL_acc, 1);
p_uop++;
} else {
asm_make_uop0(p_uop, k_opcode_ASL_value);
p_uop++;
}
break;
case k_bcc: asm_make_uop1(p_uop, k_opcode_BCC, jit_addr); p_uop++; break;
case k_bcs: asm_make_uop1(p_uop, k_opcode_BCS, jit_addr); p_uop++; break;
case k_beq: asm_make_uop1(p_uop, k_opcode_BEQ, jit_addr); p_uop++; break;
case k_bit: asm_make_uop0(p_uop, k_opcode_BIT); p_uop++; break;
case k_bmi: asm_make_uop1(p_uop, k_opcode_BMI, jit_addr); p_uop++; break;
case k_bne: asm_make_uop1(p_uop, k_opcode_BNE, jit_addr); p_uop++; break;
case k_bpl: asm_make_uop1(p_uop, k_opcode_BPL, jit_addr); p_uop++; break;
case k_brk:
asm_make_uop1(p_uop, k_opcode_PUSH_16, (uint16_t) (addr_6502 + 2));
p_uop++;
/* PHP */
asm_make_uop0(p_uop, k_opcode_PHP);
p_uop++;
/* SEI */
asm_make_uop0(p_uop, k_opcode_SEI);
p_uop++;
/* Load IRQ vector. */
asm_make_uop1(p_uop, k_opcode_addr_set, k_6502_vector_irq);
p_uop++;
asm_make_uop0(p_uop, k_opcode_value_load_16bit_wrap);
p_uop++;
/* JMP_SCRATCH_n */
asm_make_uop1(p_uop, k_opcode_JMP_SCRATCH_n, 0);
p_uop++;
break;
case k_bvc: asm_make_uop1(p_uop, k_opcode_BVC, jit_addr); p_uop++; break;
case k_bvs: asm_make_uop1(p_uop, k_opcode_BVS, jit_addr); p_uop++; break;
case k_clc: asm_make_uop0(p_uop, k_opcode_CLC); p_uop++; break;
case k_cld: asm_make_uop0(p_uop, k_opcode_CLD); p_uop++; break;
case k_cli:
asm_make_uop1(p_uop, k_opcode_check_pending_irq, addr_6502);
p_uop++;
asm_make_uop0(p_uop, k_opcode_CLI);
p_uop++;
break;
case k_clv: asm_make_uop0(p_uop, k_opcode_CLV); p_uop++; break;
case k_cmp: asm_make_uop0(p_uop, k_opcode_CMP); p_uop++; break;
case k_cpx: asm_make_uop0(p_uop, k_opcode_CPX); p_uop++; break;
case k_cpy: asm_make_uop0(p_uop, k_opcode_CPY); p_uop++; break;
case k_dec: asm_make_uop0(p_uop, k_opcode_DEC_value); p_uop++; break;
case k_dex: asm_make_uop1(p_uop, k_opcode_DEX, 1); p_uop++; break;
case k_dey: asm_make_uop1(p_uop, k_opcode_DEY, 1); p_uop++; break;
case k_eor: asm_make_uop0(p_uop, k_opcode_EOR); p_uop++; break;
case k_inc: asm_make_uop0(p_uop, k_opcode_INC_value); p_uop++; break;
case k_inx: asm_make_uop1(p_uop, k_opcode_INX, 1); p_uop++; break;
case k_iny: asm_make_uop1(p_uop, k_opcode_INY, 1); p_uop++; break;
case k_jmp:
if (opmode == k_ind) {
asm_make_uop1(p_uop, k_opcode_JMP_SCRATCH_n, 0);
p_uop++;
} else {
assert(opmode == k_abs);
p_details->branch_addr_6502 = operand_6502;
jit_addr = (uintptr_t) jit_metadata_get_host_block_address(
p_compiler->p_metadata,
operand_6502);
asm_make_uop1(p_uop, k_opcode_JMP, jit_addr);
p_uop++;
}
break;
case k_jsr:
p_details->branch_addr_6502 = operand_6502;
jit_addr =
(uintptr_t) jit_metadata_get_host_block_address(p_compiler->p_metadata,
operand_6502);
asm_make_uop1(p_uop, k_opcode_PUSH_16, (uint16_t) (addr_6502 + 2));
p_uop++;
asm_make_uop1(p_uop, k_opcode_JMP, jit_addr);
p_uop++;
if ((addr_6502 >= 0xFE) && (addr_6502 <= 0x1FD)) {
/* A JSR hosted in the stack page can self-modify. */
use_inturbo = 1;
}
break;
case k_lda: asm_make_uop0(p_uop, k_opcode_LDA); p_uop++; break;
case k_ldx: asm_make_uop0(p_uop, k_opcode_LDX); p_uop++; break;
case k_ldy: asm_make_uop0(p_uop, k_opcode_LDY); p_uop++; break;
case k_lsr:
if (opmode == k_acc) {
asm_make_uop1(p_uop, k_opcode_LSR_acc, 1);
p_uop++;
} else {
asm_make_uop0(p_uop, k_opcode_LSR_value);
p_uop++;
}
break;
/* NOTE: sends undocumented modes of NOP along to JIT. Zalaga uses NOP abx
* in a hot path.
*/
case k_nop: asm_make_uop0(p_uop, k_opcode_NOP); p_uop++; break;
case k_ora: asm_make_uop0(p_uop, k_opcode_ORA); p_uop++; break;
case k_pha: asm_make_uop0(p_uop, k_opcode_PHA); p_uop++; break;
case k_pla: asm_make_uop0(p_uop, k_opcode_PLA); p_uop++; break;
case k_php: asm_make_uop0(p_uop, k_opcode_PHP); p_uop++; break;
case k_plp:
asm_make_uop0(p_uop, k_opcode_peek_to_scratch);
p_uop++;
/* Consider the processor I flag, which we just fetched, and can often
* indicated interrupts disabled.
*/
asm_make_uop1(p_uop, k_opcode_check_pending_irq_plp, addr_6502);
p_uop++;
/* The IRQ check might bail so we need to increment the stack afterwards,
* otherwise we will have changed state before bailing!
*/
asm_make_uop0(p_uop, k_opcode_stack_commit_peek_increment);
p_uop++;
asm_make_uop0(p_uop, k_opcode_PLP);
p_uop++;
break;
case k_rol:
if (opmode == k_acc) {
asm_make_uop1(p_uop, k_opcode_ROL_acc, 1);
p_uop++;
} else {
asm_make_uop0(p_uop, k_opcode_ROL_value);
p_uop++;
}
break;
case k_ror:
if (opmode == k_acc) {
asm_make_uop1(p_uop, k_opcode_ROR_acc, 1);
p_uop++;
} else {
asm_make_uop0(p_uop, k_opcode_ROR_value);
p_uop++;
}
break;
case k_rti:
/* Bounce to the interpreter for RTI. The problem with RTI is that it
* might jump all over the place without any particular pattern, because
* interrupts will happen all over the place. If we are not careful, over
* time, RTI will split all of the JIT blocks into 1-instruction blocks,
* which will be super slow.
*/
use_interp = 1;
break;
case k_rts:
asm_make_uop0(p_uop, k_opcode_PULL_16);
p_uop++;
/* TODO: may increment 0xFFFF -> 0x10000, which may crash. */
asm_make_uop1(p_uop, k_opcode_JMP_SCRATCH_n, 1);
p_uop++;
break;
case k_sax:
/* Only send SAX along to the asm backend for the simple mode used by
* Zalaga. This avoids the backend having to implement too much for a
* non-critical opcode.
*/
if ((opmode == k_abs) || (opmode == k_zpg)) {
asm_make_uop0(p_uop, k_opcode_SAX);
p_uop++;
} else {
use_interp = 1;
}
break;
case k_sbc: asm_make_uop0(p_uop, k_opcode_SBC); p_uop++; break;
case k_sec: asm_make_uop0(p_uop, k_opcode_SEC); p_uop++; break;
case k_sed: asm_make_uop0(p_uop, k_opcode_SED); p_uop++; break;
case k_sei: asm_make_uop0(p_uop, k_opcode_SEI); p_uop++; break;
case k_slo:
/* Only send SLO along to the asm backend for the simple mode used by
* Zalaga. This avoids the backend having to implement too much for a
* non-critical opcode.
*/
if (opmode == k_zpg) {
asm_make_uop0(p_uop, k_opcode_SLO);
p_uop++;
} else {
use_interp = 1;
}
break;
case k_sta: asm_make_uop0(p_uop, k_opcode_STA); p_uop++; break;
case k_stx: asm_make_uop0(p_uop, k_opcode_STX); p_uop++; break;
case k_sty: asm_make_uop0(p_uop, k_opcode_STY); p_uop++; break;
case k_tax: asm_make_uop0(p_uop, k_opcode_TAX); p_uop++; break;
case k_tay: asm_make_uop0(p_uop, k_opcode_TAY); p_uop++; break;
case k_tsx: asm_make_uop0(p_uop, k_opcode_TSX); p_uop++; break;
case k_txs: asm_make_uop0(p_uop, k_opcode_TXS); p_uop++; break;
case k_txa: asm_make_uop0(p_uop, k_opcode_TXA); p_uop++; break;
case k_tya: asm_make_uop0(p_uop, k_opcode_TYA); p_uop++; break;
default:
/* Various undocumented opcodes. */
use_interp = 1;
break;
}
if (uses_callback &&
is_addr_known &&
((optype == k_sta) || (optype == k_stx) || (optype == k_sty)) &&
!p_compiler->option_no_encoded_callback) {
uint32_t uops_left = (&p_details->uops[k_max_uops_per_opcode] - p_uop);
num_callback_uops =
p_memory_access->memory_get_write_jit_encoding(
p_memory_callback,
p_uop,
&jit_encoding_ends_block,
&jit_encoding_extra_cycles,
uops_left,
p_details->min_6502_addr,
p_compiler->option_accurate_timings);
if (num_callback_uops > 0) {
/* Eliminate the addr_set. */
p_uop -= 2;
assert(p_uop->uopcode == k_opcode_addr_set);
p_uop->is_eliminated = 1;
p_uop += 2;
p_uop += num_callback_uops;
/* Suppress the generic write opcodes (store, invalidate). */
is_write = 0;
}
}
if (num_callback_uops > 0) {
use_interp = 0;
p_details->max_cycles += jit_encoding_extra_cycles;
if (jit_encoding_ends_block) {
/* Make sure the compiler ends the block by tagging the next address
* as a new block start.
*/
uint16_t next_addr_6502 = (addr_6502 + p_details->num_bytes_6502);
p_compiler->addr_flags[next_addr_6502] |= k_addr_flag_block_start;
}
}
if (use_interp) {
p_uop = p_first_post_debug_uop;
asm_make_uop1(p_uop, k_opcode_interp, addr_6502);
p_uop++;
p_details->ends_block = 1;
p_details->num_uops = (p_uop - &p_details->uops[0]);
assert(p_details->num_uops <= k_max_uops_per_opcode);
return;
}
if (use_inturbo) {
p_uop = p_first_post_debug_uop;
asm_make_uop1(p_uop, k_opcode_inturbo, addr_6502);
p_uop++;
p_details->ends_block = 1;
p_details->max_cycles = 0;
p_details->num_uops = (p_uop - &p_details->uops[0]);
assert(p_details->num_uops <= k_max_uops_per_opcode);
return;
}
/* Emit save carry before save NZ flags. This is because the act of saving
* NZ flags will clobber any unsaved carry / overflow flag in both asm
* backends.
*/
if (g_optype_changes_carry[optype]) {
switch (optype) {
/* These have built-in handling. */
case k_clc: case k_sec: break;
default: asm_make_uop0(p_uop, k_opcode_save_carry); p_uop++; break;
}
}
if (g_optype_changes_overflow[optype]) {
switch (optype) {
/* These have built-in handling. */
case k_bit: case k_clv: case k_plp: break;
default: asm_make_uop0(p_uop, k_opcode_save_overflow); p_uop++; break;
}
}
if (g_optype_changes_nz_flags[optype]) {
switch (g_optype_sets_register[optype]) {
case k_a: asm_make_uop0(p_uop, k_opcode_flags_nz_a); p_uop++; break;
case k_x: asm_make_uop0(p_uop, k_opcode_flags_nz_x); p_uop++; break;
case k_y: asm_make_uop0(p_uop, k_opcode_flags_nz_y); p_uop++; break;
default:
if (opmode == k_acc) {
asm_make_uop0(p_uop, k_opcode_flags_nz_a);
p_uop++;
} else if (opmem == (k_opmem_read_flag | k_opmem_write_flag)) {
asm_make_uop0(p_uop, k_opcode_flags_nz_value);
p_uop++;
} else if ((optype == k_cmp) || (optype == k_cpx) || (optype == k_cpy)) {
asm_make_uop0(p_uop, k_opcode_flags_nz_value);
p_uop++;
}
break;
}
}
/* Post-main per-mode uops. */
/* Opcodes that write, including uopcodes for handling self-modifying code. */
/* TODO: stack page invalidations. */
if (is_write) {
asm_make_uop0(p_uop, k_opcode_value_store);
p_uop++;
switch (opmode) {
case k_abs:
case k_abx:
case k_aby:
case k_idx:
case k_idy:
asm_make_uop0(p_uop, k_opcode_write_inv);
p_uop++;
break;
case k_zpg:
case k_zpx:
case k_zpy:
if (p_compiler->compile_for_code_in_zero_page) {
asm_make_uop0(p_uop, k_opcode_write_inv);
p_uop++;
}
break;
default:
assert(0);
break;
}
}
/* Accurate timings for page crossing cycles. */
if (p_compiler->option_accurate_timings &&
(opmem == k_opmem_read_flag) &&
could_page_cross) {
/* NOTE: must do page crossing cycles fixup after the main uop, because it
* may fault (e.g. for hardware register access) and then fixup. We're
* only guaranteed that the JIT handled the uop if we get here.
*/
switch (opmode) {
case k_abx:
asm_make_uop0(p_uop, k_opcode_check_page_crossing_x);
p_uop++;
break;
case k_aby:
case k_idy:
asm_make_uop0(p_uop, k_opcode_check_page_crossing_y);
p_uop++;
break;
default:
break;
}
}
/* Accurate timings for branches. */
if ((opmode == k_rel) && p_compiler->option_accurate_timings) {
/* Fixup countdown if a branch wasn't taken. */
asm_make_uop1(p_uop,
k_opcode_add_cycles,
(int8_t) (p_details->max_cycles - 2));
p_uop++;
}
p_details->num_uops = (p_uop - &p_details->uops[0]);
assert(p_details->num_uops <= k_max_uops_per_opcode);
}
static void
jit_compiler_add_history(struct jit_compiler* p_compiler,
uint16_t addr_6502,
int32_t opcode_6502,
int is_self_modified,
uint64_t ticks) {
uint32_t ring_buffer_index;
struct jit_compile_history* p_history = &p_compiler->history[addr_6502];
p_history->opcode = opcode_6502;
if (!(p_compiler->addr_flags[addr_6502] & k_addr_flag_has_history)) {
p_compiler->addr_flags[addr_6502] |= k_addr_flag_has_history;
p_history->ring_buffer_index = 0;
}
ring_buffer_index = p_history->ring_buffer_index;
ring_buffer_index++;
if (ring_buffer_index == k_opcode_history_length) {
ring_buffer_index = 0;
}
p_history->ring_buffer_index = ring_buffer_index;
p_history->opcodes[ring_buffer_index] = opcode_6502;
p_history->times[ring_buffer_index] = ticks;
p_history->was_self_modified[ring_buffer_index] = is_self_modified;
}
static inline void
jit_compiler_get_dynamic_history(struct jit_compiler* p_compiler,
uint32_t* p_new_opcode_count,
uint32_t* p_new_opcode_invalidate_count,
uint32_t* p_any_opcode_count,
uint32_t* p_any_opcode_invalidate_count,
uint8_t new_opcode,
uint16_t addr_6502,
int is_self_modify_invalidated) {
uint32_t i;
uint64_t ticks = timing_get_total_timer_ticks(p_compiler->p_timing);
struct jit_compile_history* p_history = &p_compiler->history[addr_6502];
uint32_t index = p_history->ring_buffer_index;
int had_opcode_mismatch = 0;
uint32_t new_opcode_count = 0;
uint32_t new_opcode_invalidate_count = 0;
uint32_t any_opcode_count = 0;
uint32_t any_opcode_invalidate_count = 0;
i = 0;
if (!(p_compiler->addr_flags[addr_6502] & k_addr_flag_has_history)) {
i = k_opcode_history_length;
}
for (; i < k_opcode_history_length; ++i) {
int was_self_modified;
int32_t old_opcode = p_history->opcodes[index];
/* Stop counting if we run out of opcodes. */
if (old_opcode == -1) {
break;
}
/* Stop counting if the events are over a second old. */
/* TODO: the comment says a second but the constant is 100 seconds. */
assert(p_history->times[index] <= ticks);
if ((ticks - p_history->times[index]) > (100 * 2000000)) {
break;
}
/* Switch from dynamic operand to dynamic opcode counting if the opcode
* differs.
*/
if (old_opcode != (int) new_opcode) {
had_opcode_mismatch = 1;
}
was_self_modified = p_history->was_self_modified[index];
if ((i == 0) && is_self_modify_invalidated && !had_opcode_mismatch) {
new_opcode_invalidate_count++;
}
if (!had_opcode_mismatch) {
new_opcode_count++;
if (was_self_modified) {
new_opcode_invalidate_count++;
}
}
any_opcode_count++;
if (was_self_modified) {
any_opcode_invalidate_count++;
}
if (index == 0) {
index = (k_opcode_history_length - 1);
} else {
index--;
}
}
*p_new_opcode_count = new_opcode_count;
*p_new_opcode_invalidate_count = new_opcode_invalidate_count;
*p_any_opcode_count = any_opcode_count;
*p_any_opcode_invalidate_count = any_opcode_invalidate_count;
}
static void
jit_compiler_try_make_dynamic_opcode(struct jit_compiler* p_compiler,
struct jit_opcode_details* p_opcode) {
uint8_t optype;
uint8_t opmode;
struct asm_uop* p_uop;
int32_t index;
int32_t opcode_6502 = p_opcode->opcode_6502;
uint16_t addr = p_opcode->addr_6502;
uint16_t next_addr = (uint16_t) (addr + 1);
if (jit_opcode_find_uop(p_opcode, &index, k_opcode_interp) != NULL) {
return;
}
optype = p_compiler->p_opcode_types[opcode_6502];
opmode = p_compiler->p_opcode_modes[opcode_6502];
switch (opmode) {
case k_imm:
if (optype == k_brk) {
return;
}
/* Examples: Thrust, Stryker's Run. */
p_uop = jit_opcode_find_uop(p_opcode, &index, k_opcode_value_set);
assert(p_uop != NULL);