-
Notifications
You must be signed in to change notification settings - Fork 0
/
compemu_support.c
executable file
·6202 lines (5366 loc) · 130 KB
/
compemu_support.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
#define writemem_special writemem
#define readmem_special readmem
#define USE_MATCHSTATE 0
#define setzflg_uses_bsf 0
#include "sysconfig.h"
#include "sysdeps.h"
#include "options.h"
#include "events.h"
#include "include/memory.h"
#include "custom.h"
#include "newcpu.h"
#include "comptbl.h"
#include "compemu.h"
// %%% BRIAN KING WAS HERE %%%
extern int canbang;
compop_func *compfunctbl[65536];
compop_func *nfcompfunctbl[65536];
#ifdef NOFLAGS_SUPPORT
cpuop_func *nfcpufunctbl[65536];
#endif
uae_u8* comp_pc_p;
uae_u8* start_pc_p;
uae_u32 start_pc;
uae_u32 current_block_pc_p;
uae_u32 current_block_start_target;
uae_u32 needed_flags;
static uae_u32 next_pc_p;
static uae_u32 taken_pc_p;
static int branch_cc;
int segvcount=0;
int soft_flush_count=0;
int hard_flush_count=0;
int compile_count=0;
int checksum_count=0;
static uae_u8* current_compile_p=NULL;
static uae_u8* max_compile_start;
uae_u8* compiled_code=NULL;
static uae_s32 reg_alloc_run;
static int have_rat_stall=0;
void* pushall_call_handler=NULL;
static void* popall_do_nothing=NULL;
static void* popall_exec_nostats=NULL;
static void* popall_execute_normal=NULL;
static void* popall_cache_miss=NULL;
static void* popall_recompile_block=NULL;
static void* popall_check_checksum=NULL;
extern uae_u32 oink;
extern unsigned long foink3;
extern unsigned long foink;
/* The 68k only ever executes from even addresses. So right now, we
waste half the entries in this array
UPDATE: We now use those entries to store the start of the linked
lists that we maintain for each hash result. */
cacheline cache_tags[TAGSIZE];
int letit=0;
blockinfo* hold_bi[MAX_HOLD_BI];
blockinfo* active;
blockinfo* dormant;
op_properties prop[65536];
#ifdef NOFLAGS_SUPPORT
/* 68040 */
extern const struct cputbl op_smalltbl_0_nf[];
#endif
extern const struct comptbl op_smalltbl_0_comp_nf[];
extern const struct comptbl op_smalltbl_0_comp_ff[];
#ifdef NOFLAGS_SUPPORT
/* 68020 + 68881 */
extern const struct cputbl op_smalltbl_1_nf[];
/* 68020 */
extern const struct cputbl op_smalltbl_2_nf[];
/* 68010 */
extern const struct cputbl op_smalltbl_3_nf[];
/* 68000 */
extern const struct cputbl op_smalltbl_4_nf[];
#ifdef CPUEMU_5
/* 68000 slow but compatible. */
extern const struct cputbl op_smalltbl_5_nf[];
#endif
#endif
static void flush_icache_hard(int n);
bigstate live;
smallstate empty_ss;
smallstate default_ss;
static int optlev;
static int writereg(int r, int size);
static void unlock(int r);
static void setlock(int r);
static int readreg_specific(int r, int size, int spec);
static int writereg_specific(int r, int size, int spec);
static void prepare_for_call_1(void);
static void prepare_for_call_2(void);
static void align_target(uae_u32 a);
static uae_s32 nextused[VREGS];
static uae_u8 *popallspace;
uae_u32 m68k_pc_offset;
/* Some arithmetic ooperations can be optimized away if the operands
are known to be constant. But that's only a good idea when the
side effects they would have on the flags are not important. This
variable indicates whether we need the side effects or not
*/
uae_u32 needflags=0;
/* Flag handling is complicated.
x86 instructions create flags, which quite often are exactly what we
want. So at times, the "68k" flags are actually in the x86 flags.
Then again, sometimes we do x86 instructions that clobber the x86
flags, but don't represent a corresponding m68k instruction. In that
case, we have to save them.
We used to save them to the stack, but now store them back directly
into the regflags.cznv of the traditional emulation. Thus some odd
names.
So flags can be in either of two places (used to be three; boy were
things complicated back then!); And either place can contain either
valid flags or invalid trash (and on the stack, there was also the
option of "nothing at all", now gone). A couple of variables keep
track of the respective states.
To make things worse, we might or might not be interested in the flags.
by default, we are, but a call to dont_care_flags can change that
until the next call to live_flags. If we are not, pretty much whatever
is in the register and/or the native flags is seen as valid.
*/
STATIC_INLINE blockinfo* get_blockinfo(uae_u32 cl)
{
return cache_tags[cl+1].bi;
}
STATIC_INLINE blockinfo* get_blockinfo_addr(void* addr)
{
blockinfo* bi=get_blockinfo(cacheline(addr));
while (bi) {
if (bi->pc_p==addr)
return bi;
bi=bi->next_same_cl;
}
return NULL;
}
/*******************************************************************
* All sorts of list related functions for all of the lists *
*******************************************************************/
STATIC_INLINE void remove_from_cl_list(blockinfo* bi)
{
uae_u32 cl=cacheline(bi->pc_p);
if (bi->prev_same_cl_p)
*(bi->prev_same_cl_p)=bi->next_same_cl;
if (bi->next_same_cl)
bi->next_same_cl->prev_same_cl_p=bi->prev_same_cl_p;
if (cache_tags[cl+1].bi)
cache_tags[cl].handler=cache_tags[cl+1].bi->handler_to_use;
else
cache_tags[cl].handler=popall_execute_normal;
}
STATIC_INLINE void remove_from_list(blockinfo* bi)
{
if (bi->prev_p)
*(bi->prev_p)=bi->next;
if (bi->next)
bi->next->prev_p=bi->prev_p;
}
STATIC_INLINE void remove_from_lists(blockinfo* bi)
{
remove_from_list(bi);
remove_from_cl_list(bi);
}
STATIC_INLINE void add_to_cl_list(blockinfo* bi)
{
uae_u32 cl=cacheline(bi->pc_p);
if (cache_tags[cl+1].bi)
cache_tags[cl+1].bi->prev_same_cl_p=&(bi->next_same_cl);
bi->next_same_cl=cache_tags[cl+1].bi;
cache_tags[cl+1].bi=bi;
bi->prev_same_cl_p=&(cache_tags[cl+1].bi);
cache_tags[cl].handler=bi->handler_to_use;
}
STATIC_INLINE void raise_in_cl_list(blockinfo* bi)
{
remove_from_cl_list(bi);
add_to_cl_list(bi);
}
STATIC_INLINE void add_to_active(blockinfo* bi)
{
if (active)
active->prev_p=&(bi->next);
bi->next=active;
active=bi;
bi->prev_p=&active;
}
STATIC_INLINE void add_to_dormant(blockinfo* bi)
{
if (dormant)
dormant->prev_p=&(bi->next);
bi->next=dormant;
dormant=bi;
bi->prev_p=&dormant;
}
STATIC_INLINE void remove_dep(dependency* d)
{
if (d->prev_p)
*(d->prev_p)=d->next;
if (d->next)
d->next->prev_p=d->prev_p;
d->prev_p=NULL;
d->next=NULL;
}
/* This block's code is about to be thrown away, so it no longer
depends on anything else */
STATIC_INLINE void remove_deps(blockinfo* bi)
{
remove_dep(&(bi->dep[0]));
remove_dep(&(bi->dep[1]));
}
STATIC_INLINE void adjust_jmpdep(dependency* d, void* a)
{
*(d->jmp_off)=(uae_u32)a-((uae_u32)d->jmp_off+4);
}
/********************************************************************
* Soft flush handling support functions *
********************************************************************/
STATIC_INLINE void set_dhtu(blockinfo* bi, void* dh)
{
//printf("bi is %p\n",bi);
if (dh!=bi->direct_handler_to_use) {
dependency* x=bi->deplist;
//printf("bi->deplist=%p\n",bi->deplist);
while (x) {
//printf("x is %p\n",x);
//printf("x->next is %p\n",x->next);
//printf("x->prev_p is %p\n",x->prev_p);
if (x->jmp_off) {
adjust_jmpdep(x,dh);
}
x=x->next;
}
bi->direct_handler_to_use=dh;
}
}
STATIC_INLINE void invalidate_block(blockinfo* bi)
{
int i;
bi->optlevel=0;
bi->count=currprefs.optcount[0]-1;
bi->handler=NULL;
bi->handler_to_use=popall_execute_normal;
bi->direct_handler=NULL;
set_dhtu(bi,bi->direct_pen);
bi->needed_flags=0xff;
for (i=0;i<2;i++) {
bi->dep[i].jmp_off=NULL;
bi->dep[i].target=NULL;
}
remove_deps(bi);
}
STATIC_INLINE void create_jmpdep(blockinfo* bi, int i, uae_u32* jmpaddr, uae_u32 target)
{
blockinfo* tbi=get_blockinfo_addr((void*)target);
Dif(!tbi) {
write_log ("JIT: Could not create jmpdep!\n");
abort();
}
bi->dep[i].jmp_off=jmpaddr;
bi->dep[i].target=tbi;
bi->dep[i].next=tbi->deplist;
if (bi->dep[i].next)
bi->dep[i].next->prev_p=&(bi->dep[i].next);
bi->dep[i].prev_p=&(tbi->deplist);
tbi->deplist=&(bi->dep[i]);
}
STATIC_INLINE void big_to_small_state(bigstate* b, smallstate* s)
{
int i;
int count=0;
for (i=0;i<N_REGS;i++) {
s->nat[i].validsize=0;
s->nat[i].dirtysize=0;
if (b->nat[i].nholds) {
int index=b->nat[i].nholds-1;
int r=b->nat[i].holds[index];
s->nat[i].holds=r;
s->nat[i].validsize=b->state[r].validsize;
s->nat[i].dirtysize=b->state[r].dirtysize;
count++;
}
}
write_log ("JIT: count=%d\n", count);
for (i=0;i<N_REGS;i++) { // FIXME --- don't do dirty yet
s->nat[i].dirtysize=0;
}
}
STATIC_INLINE void attached_state(blockinfo* bi)
{
bi->havestate=1;
if (bi->direct_handler_to_use==bi->direct_handler)
set_dhtu(bi,bi->direct_pen);
bi->direct_handler=bi->direct_pen;
bi->status=BI_TARGETTED;
}
STATIC_INLINE blockinfo* get_blockinfo_addr_new(void* addr, int setstate)
{
blockinfo* bi=get_blockinfo_addr(addr);
int i;
#if USE_OPTIMIZER
if (reg_alloc_run)
return NULL;
#endif
if (!bi) {
for (i=0;i<MAX_HOLD_BI && !bi;i++) {
if (hold_bi[i]) {
uae_u32 cl=cacheline(addr);
bi=hold_bi[i];
hold_bi[i]=NULL;
bi->pc_p=addr;
invalidate_block(bi);
add_to_active(bi);
add_to_cl_list(bi);
}
}
}
if (!bi) {
write_log ("JIT: Looking for blockinfo, can't find free one\n");
abort();
}
#if USE_MATCHSTATE
if (setstate &&
!bi->havestate) {
big_to_small_state(&live,&(bi->env));
attached_state(bi);
}
#endif
return bi;
}
static void prepare_block(blockinfo* bi);
STATIC_INLINE void alloc_blockinfos(void)
{
int i;
blockinfo* bi;
for (i=0;i<MAX_HOLD_BI;i++) {
if (hold_bi[i])
return;
bi=hold_bi[i]=(blockinfo*)current_compile_p;
current_compile_p+=sizeof(blockinfo);
prepare_block(bi);
}
}
/********************************************************************
* Preferences handling. This is just a convenient place to put it *
********************************************************************/
extern int have_done_picasso;
void check_prefs_changed_comp (void)
{
currprefs.comptrustbyte = changed_prefs.comptrustbyte;
currprefs.comptrustword = changed_prefs.comptrustword;
currprefs.comptrustlong = changed_prefs.comptrustlong;
currprefs.comptrustnaddr= changed_prefs.comptrustnaddr;
currprefs.compnf = changed_prefs.compnf;
currprefs.comp_hardflush= changed_prefs.comp_hardflush;
currprefs.comp_constjump= changed_prefs.comp_constjump;
currprefs.comp_oldsegv= changed_prefs.comp_oldsegv;
currprefs.compfpu= changed_prefs.compfpu;
if (currprefs.cachesize!=changed_prefs.cachesize) {
currprefs.cachesize = changed_prefs.cachesize;
alloc_cache();
}
// Turn off illegal-mem logging when using JIT...
if( currprefs.cachesize )
currprefs.illegal_mem = changed_prefs.illegal_mem;// = 0;
currprefs.comp_midopt=changed_prefs.comp_midopt;
currprefs.comp_lowopt=changed_prefs.comp_lowopt;
if ( ( !canbang || !currprefs.cachesize ) &&
currprefs.comptrustbyte != 1 )
{
// Set all of these to indirect when canbang == 0
// Basically, set the compforcesettings option...
currprefs.comptrustbyte = 1;
currprefs.comptrustword = 1;
currprefs.comptrustlong = 1;
currprefs.comptrustnaddr= 1;
currprefs.compforcesettings = 1;
changed_prefs.comptrustbyte = 1;
changed_prefs.comptrustword = 1;
changed_prefs.comptrustlong = 1;
changed_prefs.comptrustnaddr= 1;
changed_prefs.compforcesettings = 1;
if( currprefs.cachesize )
write_log ("JIT: Reverting to \"indirect\" access, because canbang is zero!\n");
}
#if 0
if (!currprefs.compforcesettings && !have_done_picasso) {
int stop=0;
if (currprefs.comptrustbyte!=0 && currprefs.comptrustbyte!=3)
stop = 1, write_log ("JIT: comptrustbyte is not 'direct' or 'afterpic'\n");
if (currprefs.comptrustword!=0 && currprefs.comptrustword!=3)
stop = 1, write_log ("JIT: comptrustword is not 'direct' or 'afterpic'\n");
if (currprefs.comptrustlong!=0 && currprefs.comptrustlong!=3)
stop = 1, write_log ("JIT: comptrustlong is not 'direct' or 'afterpic'\n");
if (currprefs.comptrustnaddr!=0 && currprefs.comptrustnaddr!=3)
stop = 1, write_log ("JIT: comptrustnaddr is not 'direct' or 'afterpic'\n");
if (currprefs.compnf!=1)
stop = 1, write_log ("JIT: compnf is not 'yes'\n");
if (currprefs.cachesize<1024)
stop = 1, write_log ("JIT: cachesize is less than 1024\n");
if (currprefs.comp_hardflush)
stop = 1, write_log ("JIT: comp_flushmode is 'hard'\n");
if (!canbang)
stop = 1, write_log ("JIT: Cannot use most direct memory access,\n"
" and unable to recover from failed guess!\n");
if (stop) {
gui_message("JIT: Configuration problems were detected!\n"
"JIT: These will adversely affect performance, and should\n"
"JIT: not be used. For more info, please see README.JIT-tuning\n"
"JIT: in the UAE documentation directory. You can force\n"
"JIT: your settings to be used by setting\n"
"JIT: 'compforcesettings=yes'\n"
"JIT: in your config file\n");
exit(1);
}
}
#endif
}
/********************************************************************
* Get the optimizer stuff *
********************************************************************/
#include "compemu_optimizer.c"
#include "compemu_optimizer_x86.c"
/********************************************************************
* Functions to emit data into memory, and other general support *
********************************************************************/
static uae_u8* target;
static void emit_init(void)
{
}
STATIC_INLINE void emit_byte(uae_u8 x)
{
*target++=x;
}
STATIC_INLINE void emit_word(uae_u16 x)
{
*((uae_u16*)target)=x;
target+=2;
}
STATIC_INLINE void emit_long(uae_u32 x)
{
*((uae_u32*)target)=x;
target+=4;
}
STATIC_INLINE uae_u32 reverse32(uae_u32 oldv)
{
return ((oldv>>24)&0xff) | ((oldv>>8)&0xff00) |
((oldv<<8)&0xff0000) | ((oldv<<24)&0xff000000);
}
void set_target(uae_u8* t)
{
lopt_emit_all();
target=t;
}
STATIC_INLINE uae_u8* get_target_noopt(void)
{
return target;
}
STATIC_INLINE uae_u8* get_target(void)
{
lopt_emit_all();
return get_target_noopt();
}
/********************************************************************
* Getting the information about the target CPU *
********************************************************************/
#include "compemu_raw_x86.c"
/********************************************************************
* Flags status handling. EMIT TIME! *
********************************************************************/
static void bt_l_ri_noclobber(R4 r, IMM i);
static void make_flags_live_internal(void)
{
if (live.flags_in_flags==VALID)
return;
Dif (live.flags_on_stack==TRASH) {
write_log ("JIT: Want flags, got something on stack, but it is TRASH\n");
abort();
}
if (live.flags_on_stack==VALID) {
int tmp;
tmp=readreg_specific(FLAGTMP,4,FLAG_NREG2);
raw_reg_to_flags(tmp);
unlock(tmp);
live.flags_in_flags=VALID;
return;
}
write_log ("JIT: Huh? live.flags_in_flags=%d, live.flags_on_stack=%d, but need to make live\n",
live.flags_in_flags, live.flags_on_stack);
abort();
}
static void flags_to_stack(void)
{
if (live.flags_on_stack==VALID)
return;
if (!live.flags_are_important) {
live.flags_on_stack=VALID;
return;
}
Dif (live.flags_in_flags!=VALID)
abort();
else {
int tmp;
tmp=writereg_specific(FLAGTMP,4,FLAG_NREG1);
raw_flags_to_reg(tmp);
unlock(tmp);
}
live.flags_on_stack=VALID;
}
STATIC_INLINE void clobber_flags(void)
{
if (live.flags_in_flags==VALID && live.flags_on_stack!=VALID)
flags_to_stack();
live.flags_in_flags=TRASH;
}
/* Prepare for leaving the compiled stuff */
STATIC_INLINE void flush_flags(void)
{
flags_to_stack();
return;
}
int touchcnt;
/********************************************************************
* register allocation per block logging *
********************************************************************/
static uae_s8 vstate[VREGS];
static uae_s8 nstate[N_REGS];
#define L_UNKNOWN -127
#define L_UNAVAIL -1
#define L_NEEDED -2
#define L_UNNEEDED -3
STATIC_INLINE void log_startblock(void)
{
int i;
for (i=0;i<VREGS;i++)
vstate[i]=L_UNKNOWN;
for (i=0;i<N_REGS;i++)
nstate[i]=L_UNKNOWN;
}
STATIC_INLINE void log_isused(int n)
{
if (nstate[n]==L_UNKNOWN)
nstate[n]=L_UNAVAIL;
}
STATIC_INLINE void log_isreg(int n, int r)
{
if (nstate[n]==L_UNKNOWN)
nstate[n]=r;
if (vstate[r]==L_UNKNOWN)
vstate[r]=L_NEEDED;
}
STATIC_INLINE void log_clobberreg(int r)
{
if (vstate[r]==L_UNKNOWN)
vstate[r]=L_UNNEEDED;
}
/* This ends all possibility of clever register allocation */
STATIC_INLINE void log_flush(void)
{
int i;
for (i=0;i<VREGS;i++)
if (vstate[i]==L_UNKNOWN)
vstate[i]=L_NEEDED;
for (i=0;i<N_REGS;i++)
if (nstate[i]==L_UNKNOWN)
nstate[i]=L_UNAVAIL;
}
STATIC_INLINE void log_dump(void)
{
int i;
return;
write_log("----------------------\n");
for (i=0;i<N_REGS;i++) {
switch(nstate[i]) {
case L_UNKNOWN: write_log("Nat %d : UNKNOWN\n",i); break;
case L_UNAVAIL: write_log("Nat %d : UNAVAIL\n",i); break;
default: write_log("Nat %d : %d\n",i,nstate[i]); break;
}
}
for (i=0;i<VREGS;i++) {
if (vstate[i]==L_UNNEEDED)
write_log("Virt %d: UNNEEDED\n",i);
}
}
/********************************************************************
* register status handling. EMIT TIME! *
********************************************************************/
STATIC_INLINE void set_status(int r, int status)
{
if (status==ISCONST)
log_clobberreg(r);
live.state[r].status=status;
}
STATIC_INLINE int isinreg(int r)
{
return live.state[r].status==CLEAN || live.state[r].status==DIRTY;
}
STATIC_INLINE void adjust_nreg(int r, uae_u32 val)
{
if (!val)
return;
raw_lea_l_brr(r,r,val);
}
static void tomem(int r)
{
int rr=live.state[r].realreg;
if (isinreg(r)) {
if (live.state[r].val &&
live.nat[rr].nholds==1 &&
!live.nat[rr].locked) {
// printf("RemovingA offset %x from reg %d (%d) at %p\n",
// live.state[r].val,r,rr,target);
adjust_nreg(rr,live.state[r].val);
live.state[r].val=0;
live.state[r].dirtysize=4;
set_status(r,DIRTY);
}
}
if (live.state[r].status==DIRTY) {
switch (live.state[r].dirtysize) {
case 1: raw_mov_b_mr((uae_u32)live.state[r].mem,rr); break;
case 2: raw_mov_w_mr((uae_u32)live.state[r].mem,rr); break;
case 4: raw_mov_l_mr((uae_u32)live.state[r].mem,rr); break;
default: abort();
}
set_status(r,CLEAN);
live.state[r].dirtysize=0;
}
}
STATIC_INLINE int isconst(int r)
{
return live.state[r].status==ISCONST;
}
int is_const(int r)
{
return isconst(r);
}
STATIC_INLINE void writeback_const(int r)
{
if (!isconst(r))
return;
Dif (live.state[r].needflush==NF_HANDLER) {
write_log ("JIT: Trying to write back constant NF_HANDLER!\n");
abort();
}
raw_mov_l_mi((uae_u32)live.state[r].mem,live.state[r].val);
live.state[r].val=0;
set_status(r,INMEM);
}
STATIC_INLINE void tomem_c(int r)
{
if (isconst(r)) {
writeback_const(r);
}
else
tomem(r);
}
static void evict(int r)
{
int rr;
if (!isinreg(r))
return;
tomem(r);
rr=live.state[r].realreg;
Dif (live.nat[rr].locked &&
live.nat[rr].nholds==1) {
write_log ("JIT: register %d in nreg %d is locked!\n", r, live.state[r].realreg);
abort();
}
live.nat[rr].nholds--;
if (live.nat[rr].nholds!=live.state[r].realind) { /* Was not last */
int topreg=live.nat[rr].holds[live.nat[rr].nholds];
int thisind=live.state[r].realind;
live.nat[rr].holds[thisind]=topreg;
live.state[topreg].realind=thisind;
}
live.state[r].realreg=-1;
set_status(r,INMEM);
}
STATIC_INLINE void free_nreg(int r)
{
int i=live.nat[r].nholds;
while (i) {
int vr;
--i;
vr=live.nat[r].holds[i];
evict(vr);
}
Dif (live.nat[r].nholds!=0) {
write_log ("JIT: Failed to free nreg %d, nholds is %d\n", r, live.nat[r].nholds);
abort();
}
}
/* Use with care! */
STATIC_INLINE void isclean(int r)
{
if (!isinreg(r))
return;
live.state[r].validsize=4;
live.state[r].dirtysize=0;
live.state[r].val=0;
set_status(r,CLEAN);
}
STATIC_INLINE void disassociate(int r)
{
isclean(r);
evict(r);
}
STATIC_INLINE void set_const(int r, uae_u32 val)
{
disassociate(r);
live.state[r].val=val;
set_status(r,ISCONST);
}
STATIC_INLINE uae_u32 get_offset(int r)
{
return live.state[r].val;
}
static int alloc_reg_hinted(int r, int size, int willclobber, int hint)
{
int bestreg;
uae_s32 when;
int i;
uae_s32 badness=0; /* to shut up gcc */
bestreg=-1;
when=2000000000;
for (i=N_REGS;i--;) {
badness=live.nat[i].touched;
if (live.nat[i].nholds==0)
badness=0;
if (i==hint)
badness-=200000000;
if (!live.nat[i].locked && badness<when) {
if ((size==1 && live.nat[i].canbyte) ||
(size==2 && live.nat[i].canword) ||
(size==4)) {
bestreg=i;
when=badness;
if (live.nat[i].nholds==0 && hint<0)
break;
if (i==hint)
break;
}
}
}
Dif (bestreg==-1)
abort();
if (live.nat[bestreg].nholds>0) {
free_nreg(bestreg);
}
if (isinreg(r)) {
int rr=live.state[r].realreg;
/* This will happen if we read a partially dirty register at a
bigger size */
Dif (willclobber || live.state[r].validsize>=size)
abort();
Dif (live.nat[rr].nholds!=1)
abort();
if (size==4 && live.state[r].validsize==2) {
log_isused(bestreg);
raw_mov_l_rm(bestreg,(uae_u32)live.state[r].mem);
raw_bswap_32(bestreg);
raw_zero_extend_16_rr(rr,rr);
raw_zero_extend_16_rr(bestreg,bestreg);
raw_bswap_32(bestreg);
raw_lea_l_brr_indexed(rr,rr,bestreg,1,0);
live.state[r].validsize=4;
live.nat[rr].touched=touchcnt++;
return rr;
}
if (live.state[r].validsize==1) {
/* Nothing yet */
}
evict(r);
}
if (!willclobber) {
if (live.state[r].status!=UNDEF) {
if (isconst(r)) {
raw_mov_l_ri(bestreg,live.state[r].val);
live.state[r].val=0;
live.state[r].dirtysize=4;
set_status(r,DIRTY);
log_isused(bestreg);
}
else {
if (r==FLAGTMP)
raw_load_flagreg(bestreg,r);
else if (r==FLAGX)
raw_load_flagx(bestreg,r);
else {
raw_mov_l_rm(bestreg,(uae_u32)live.state[r].mem);
}
live.state[r].dirtysize=0;
set_status(r,CLEAN);
log_isreg(bestreg,r);
}
}
else {
live.state[r].val=0;
live.state[r].dirtysize=0;
set_status(r,CLEAN);
log_isused(bestreg);
}
live.state[r].validsize=4;
}
else { /* this is the easiest way, but not optimal. FIXME! */
/* Now it's trickier, but hopefully still OK */
if (!isconst(r) || size==4) {
live.state[r].validsize=size;
live.state[r].dirtysize=size;
live.state[r].val=0;
set_status(r,DIRTY);
if (size==4)
log_isused(bestreg);
else
log_isreg(bestreg,r);
}
else {
if (live.state[r].status!=UNDEF)
raw_mov_l_ri(bestreg,live.state[r].val);
live.state[r].val=0;
live.state[r].validsize=4;
live.state[r].dirtysize=4;
set_status(r,DIRTY);
log_isused(bestreg);
}
}
live.state[r].realreg=bestreg;
live.state[r].realind=live.nat[bestreg].nholds;
live.nat[bestreg].touched=touchcnt++;
live.nat[bestreg].holds[live.nat[bestreg].nholds]=r;
live.nat[bestreg].nholds++;
return bestreg;
}
static int alloc_reg(int r, int size, int willclobber)
{
return alloc_reg_hinted(r,size,willclobber,-1);
}
static void unlock(int r)
{
Dif (!live.nat[r].locked)
abort();
live.nat[r].locked--;
}
static void setlock(int r)
{
live.nat[r].locked++;
}
static void mov_nregs(int d, int s)
{
int ns=live.nat[s].nholds;
int nd=live.nat[d].nholds;
int i;
if (s==d)
return;