-
Notifications
You must be signed in to change notification settings - Fork 0
/
compiler.c
4558 lines (4036 loc) · 133 KB
/
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
/*
* UAE - The Un*x Amiga Emulator
*
* m68k emulation
*
* (c) 1995 Bernd Schmidt
*/
#include "sysconfig.h"
#include "sysdeps.h"
#include "config.h"
#include "options.h"
#include "events.h"
#include "gui.h"
#include "memory.h"
#include "custom.h"
#include "machdep/m68k.h"
#include "readcpu.h"
#include "newcpu.h"
#include "ersatz.h"
#include "blitter.h"
#include "debug.h"
#include "autoconf.h"
#include "compiler.h"
#ifdef USE_COMPILER
#include <sys/mman.h>
char *address_space, *good_address_map;
code_execfunc exec_me;
UBYTE nr_bbs_to_run = 1;
int nr_bbs_start = 40;
static int compile_failure;
static int quiet_compile = 1;
int i_want_to_die = 1;
static int n_compiled = 0;
static int n_max_comp = 99999999;
static CPTR call_only_me = 0;
int patched_syscalls = 0;
static int count_bits(UWORD v)
{
int bits = 0;
while (v != 0) {
if (v & 1)
bits++;
v >>= 1;
}
return bits;
}
static UWORD bitswap(UWORD v)
{
UWORD newv = 0;
UWORD m1 = 1, m2 = 0x8000;
int i;
for (i = 0; i < 16; i++) {
if (v & m1)
newv |= m2;
m2 >>= 1;
m1 <<= 1;
}
return newv;
}
static long long compiled_hits = 0;
/* 16K areas with 512 byte blocks */
#define SUBUNIT_ORDER 9
#define PAGE_SUBUNIT (1 << SUBUNIT_ORDER)
#define PAGE_ALLOC_UNIT (PAGE_SUBUNIT * 32)
static int zerofd;
static int zeroff;
static struct code_page *first_code_page;
static struct code_page *new_code_page(void)
{
struct code_page *ncp;
ncp = (struct code_page *)mmap(NULL, PAGE_ALLOC_UNIT,
PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE,
zerofd, zeroff);
zeroff += PAGE_ALLOC_UNIT;
if (ncp) {
ncp->next = first_code_page;
first_code_page = ncp;
ncp->allocmask = 1; /* what a waste */
}
return ncp;
}
#define NUM_HASH 32768 /* larger values cause some paging on my 16MB machine */
#define HASH_MASK (NUM_HASH-1)
#define MAX_UNUSED_HASH 512
static int SCAN_MARK = 1; /* Number of calls after which to scan a function */
static int COMPILE_MARK = 5; /* Number of calls after which to compile a function */
/* The main address -> function lookup hashtable. We use the lower bits of
* the address as hash function. */
static struct hash_entry cpu_hash[NUM_HASH];
/* These aren't really LRU lists... They used to be, but keeping them in that
* order is costly. The hash LRU list is now a two-part list: Functions that have
* no code allocated for them are placed at the beginning. Such entries can be
* recycled when we need a new hash entry. */
static struct hash_block lru_first_block;
static struct hash_entry lru_first_hash;
static struct hash_entry *freelist_hash;
static struct hash_block *freelist_block;
static int num_unused_hash;
static int m68k_scan_func(struct hash_entry *);
static int m68k_compile_block(struct hash_block *);
static char *alloc_code(struct hash_block *hb, int ninsns)
{
struct code_page *cp;
long int allocsize = (ninsns * 32 + PAGE_SUBUNIT-1) & ~(PAGE_SUBUNIT-1);
ULONG allocmask;
int allocbits;
int j;
int last_bit;
if (allocsize >= (PAGE_ALLOC_UNIT - (1 << SUBUNIT_ORDER)))
return NULL;
allocbits = (allocsize >> SUBUNIT_ORDER);
allocmask = (1 << allocbits) - 1;
for (cp = first_code_page; cp != NULL; cp = cp->next) {
ULONG thispage_alloc = cp->allocmask;
for (j = 1; j < (33 - allocbits); j++) {
if ((cp->allocmask & (allocmask << j)) == 0) {
goto found_page;
}
}
}
/* Nothing large enough free: make a new page */
cp = new_code_page();
if (cp == NULL)
return NULL;
j = 1;
found_page:
/* See whether there is in fact more space for us. If so, allocate all of
* it. compile_block() will free everything it didn't need. */
allocmask <<= j;
last_bit = allocbits + j;
while (last_bit < 32 && (cp->allocmask & (1 << last_bit)) == 0) {
allocmask |= 1 << last_bit;
allocsize += PAGE_SUBUNIT;
last_bit++;
}
hb->page_allocmask = allocmask;
hb->cpage = cp;
cp->allocmask |= allocmask;
hb->compile_start = ((char *)cp + (j << SUBUNIT_ORDER));
hb->alloclen = allocsize;
return hb->compile_start;
}
static void remove_hash_from_lists(struct hash_entry *h)
{
h->lru_next->lru_prev = h->lru_prev;
h->lru_prev->lru_next = h->lru_next;
h->next->prev = h->prev;
h->prev->next = h->next;
}
static void lru_touch(struct hash_entry *h)
{
h->lru_next->lru_prev = h->lru_prev;
h->lru_prev->lru_next = h->lru_next;
h->lru_next = &lru_first_hash;
h->lru_prev = lru_first_hash.lru_prev;
h->lru_prev->lru_next = h;
lru_first_hash.lru_prev = h;
}
static void lru_untouch(struct hash_entry *h)
{
h->lru_next->lru_prev = h->lru_prev;
h->lru_prev->lru_next = h->lru_next;
h->lru_prev = &lru_first_hash;
h->lru_next = lru_first_hash.lru_next;
h->lru_next->lru_prev = h;
lru_first_hash.lru_next = h;
}
static void forget_block(struct hash_block *hb)
{
struct hash_entry *h = hb->he_first;
hb->lru_next->lru_prev = hb->lru_prev;
hb->lru_prev->lru_next = hb->lru_next;
hb->lru_next = freelist_block;
freelist_block = hb;
if (hb->cpage != NULL)
fprintf(stderr, "Discarding block with code. Tsk.\n");
do {
struct hash_entry *next = h->next_same_block;
h->block = NULL;
h->execute = NULL;
h->next_same_block = NULL;
h = next;
num_unused_hash++;
lru_untouch(h);
} while (h != hb->he_first);
compiler_flush_jsr_stack();
}
static void lru_touch_block(struct hash_block *h)
{
h->lru_next->lru_prev = h->lru_prev;
h->lru_prev->lru_next = h->lru_next;
h->lru_next = &lru_first_block;
h->lru_prev = lru_first_block.lru_prev;
h->lru_prev->lru_next = h;
lru_first_block.lru_prev = h;
}
static __inline__ int check_block(struct hash_block *hb)
{
#ifndef RELY_ON_LOADSEG_DETECTION
struct hash_entry *h = hb->he_first;
do {
struct hash_entry *next = h->next_same_block;
if (h->matchword != *(ULONG *)get_real_address(h->addr))
return 0;
h = next;
} while (h != hb->he_first);
#endif
return 1;
}
ULONG flush_icache(void)
{
struct hash_block *hb = lru_first_block.lru_next;
while (hb != &lru_first_block) {
struct hash_block *next = hb->lru_next;
if (hb->cpage != NULL) {
/* Address in chipmem? Then forget about block*/
if ((hb->he_first->addr & ~0xF80000) != 0xF80000) {
hb->cpage->allocmask &= ~hb->page_allocmask;
hb->cpage = NULL;
forget_block(hb);
}
}
hb = next;
}
return m68k_dreg(regs, 0);
}
void possible_loadseg(void)
{
fprintf(stderr, "Possible LoadSeg() detected\n");
flush_icache();
}
static struct hash_block *new_block(void)
{
struct hash_block *b = freelist_block;
if (b != NULL) {
freelist_block = b->lru_next;
} else
b = (struct hash_block *)malloc(sizeof *b);
b->nrefs = 0;
b->cpage = NULL;
b->he_first = NULL;
b->translated = b->untranslatable = b->allocfailed = 0;
return b;
}
static struct hash_entry *get_free_hash(void)
{
struct hash_entry *h;
for (;;) {
h = freelist_hash;
if (h != NULL) {
freelist_hash = h->next_same_block;
break;
}
h = lru_first_hash.lru_next;
if (num_unused_hash >= MAX_UNUSED_HASH && h->block == NULL
&& !h->locked)
{
remove_hash_from_lists(h);
num_unused_hash--;
break;
}
h = (struct hash_entry *)malloc(sizeof(struct hash_entry));
h->next_same_block = NULL;
h->addr = -1;
break;
}
num_unused_hash++;
h->block = NULL;
h->ncalls = 0;
h->locked = h->cacheflush = 0;
h->execute = NULL;
return h;
}
static struct hash_entry *new_hash(CPTR addr)
{
struct hash_entry *h = get_free_hash();
h->addr = addr;
/* Chain the new node */
h->prev = cpu_hash + ((addr >> 1) & HASH_MASK);
h->next = h->prev->next;
h->next->prev = h->prev->next = h;
h->lru_next = &lru_first_hash;
h->lru_prev = lru_first_hash.lru_prev;
h->lru_prev->lru_next = h;
lru_first_hash.lru_prev = h;
h->next_same_block = NULL;
return h;
}
static struct hash_entry *find_hash(CPTR addr)
{
struct hash_entry *h;
struct hash_entry *h1 = cpu_hash + ((addr >> 1) & HASH_MASK);
if (h1->next->addr == addr)
return h1->next;
for (h = h1->next; h != h1; h = h->next) {
if (h->addr == addr) {
/* Put it at the head of the list so that the above shortcut
* works the next time we come here */
h->next->prev = h->prev; h->prev->next = h->next;
h->prev = h1;
h->next = h1->next;
h->next->prev = h->prev->next = h;
return h;
}
}
return NULL;
}
static struct hash_entry *get_hash_for_func(CPTR addr, int mark_locked)
{
struct hash_entry *h = find_hash(addr);
if (h == NULL)
h = new_hash (addr);
#if 0 /* Too expensive */
else
lru_touch(h);
#endif
if (mark_locked)
h->locked = 1;
return h;
}
static struct hash_entry *get_hash(CPTR addr)
{
struct hash_entry *h = get_hash_for_func(addr, 0);
if (h->block == NULL) {
if (++h->ncalls == SCAN_MARK) {
m68k_scan_func(h);
}
} else
if (!h->block->untranslatable && h->block->nrefs++ == COMPILE_MARK) {
lru_touch_block(h->block);
if (m68k_compile_block(h->block)) {
h->block->untranslatable = 1;
} else {
h->block->translated = 1;
}
}
return h;
}
void special_flush_hash(CPTR addr)
{
struct hash_entry *h = get_hash_for_func(addr, 0);
h->cacheflush = 1;
}
static __inline__ void m68k_setpc_hash(CPTR newpc)
{
struct hash_entry *h = get_hash(newpc);
if (h->cacheflush)
flush_icache();
if (h->execute != NULL) {
if ((h->addr & 0xF80000) == 0xF80000 || check_block(h->block)) {
compiled_hits++;
if (i_want_to_die && (call_only_me == 0 || call_only_me == newpc)) {
exec_me = h->execute;
nr_bbs_to_run = nr_bbs_start;
regs.spcflags |= SPCFLAG_EXEC;
}
} else
flush_icache();
}
regs.pc = newpc;
regs.pc_p = regs.pc_oldp = get_real_address(newpc);
}
static __inline__ void m68k_setpc_nohash(CPTR newpc)
{
#if 0
/* This is probably not too good for efficiency... FIXME */
struct hash_entry *h = find_hash(newpc);
if (h != NULL && h->cacheflush)
flush_icache();
#endif
regs.pc = newpc;
regs.pc_p = regs.pc_oldp = get_real_address(newpc);
}
void m68k_setpc(CPTR newpc)
{
m68k_setpc_hash(newpc);
}
void m68k_setpc_fast(CPTR newpc)
{
m68k_setpc_nohash(newpc);
}
void m68k_setpc_rte(CPTR newpc)
{
m68k_setpc_nohash(newpc);
}
void m68k_setpc_bcc(CPTR newpc)
{
m68k_setpc_hash(newpc);
}
static void hash_init(void)
{
int i;
struct hash_entry **hepp;
freelist_block = NULL;
freelist_hash = NULL;
for(i = 0; i < NUM_HASH; i++) {
cpu_hash[i].next = cpu_hash[i].prev = cpu_hash + i;
cpu_hash[i].lru_next = cpu_hash[i].lru_prev = NULL;
cpu_hash[i].block = NULL;
cpu_hash[i].locked = 0; cpu_hash[i].cacheflush = 0;
cpu_hash[i].addr = -1;
}
lru_first_hash.lru_next = lru_first_hash.lru_prev = &lru_first_hash;
lru_first_block.lru_next = lru_first_block.lru_prev = &lru_first_block;
num_unused_hash = 0;
}
static void code_init(void)
{
first_code_page = NULL;
zerofd = open("/dev/zero", O_RDWR);
zeroff = 0;
}
#define CC68K_C 16
#define CC68K_V 8
#define CC68K_Z 4
#define CC68K_N 2
#define CC68K_X 1
static __inline__ int cc_flagmask_68k(const int cc)
{
switch(cc){
case 0: return 0; /* T */
case 1: return 0; /* F */
case 2: return CC68K_C|CC68K_Z; /* HI */
case 3: return CC68K_C|CC68K_Z; /* LS */
case 4: return CC68K_C; /* CC */
case 5: return CC68K_C; /* CS */
case 6: return CC68K_Z; /* NE */
case 7: return CC68K_Z; /* EQ */
case 8: return CC68K_V; /* VC */
case 9: return CC68K_V; /* VS */
case 10:return CC68K_N; /* PL */
case 11:return CC68K_N; /* MI */
case 12:return CC68K_N|CC68K_V; /* GE */
case 13:return CC68K_N|CC68K_V; /* LT */
case 14:return CC68K_N|CC68K_V|CC68K_Z; /* GT */
case 15:return CC68K_N|CC68K_V|CC68K_Z; /* LE */
}
abort();
return 0;
}
static __inline__ void translate_step_over_ea(UBYTE **pcpp, amodes m,
wordsizes size)
{
switch (m) {
case Areg:
case Dreg:
case Aind:
case Aipi:
case Apdi:
case immi:
break;
case imm:
if (size == sz_long)
goto is_long;
/* fall through */
case Ad16:
case PC16:
case imm0:
case imm1:
case absw:
(*pcpp)+=2;
break;
case Ad8r:
case PC8r:
{
UWORD extra = *(*pcpp)++;
extra <<= 8;
extra |= *(*pcpp)++;
/* @@@ handle 68020 stuff here */
}
break;
case absl:
case imm2:
is_long:
(*pcpp) += 4;
break;
}
}
static struct instr *translate_getnextinsn(UBYTE **pcpp)
{
UWORD opcode;
struct instr *dp;
opcode = *(*pcpp)++ << 8;
opcode |= *(*pcpp)++;
if (cpufunctbl[opcode] == op_illg) {
opcode = 0x4AFC;
}
dp = table68k + opcode;
if (dp->suse) {
translate_step_over_ea(pcpp, dp->smode, dp->size);
}
if (dp->duse) {
translate_step_over_ea(pcpp, dp->dmode, dp->size);
}
return dp;
}
#define CB_STACKSIZE 200
#define BB_STACKSIZE 200
static ULONG condbranch_stack[CB_STACKSIZE];
static int condbranch_src_stack[CB_STACKSIZE];
struct bb_info {
struct hash_entry *h;
CPTR stopaddr;
int can_compile_last;
struct bb_info *bb_next1, *bb_next2;
int flags_live_at_end;
int flags_live_at_start;
int first_iip, last_iip;
} bb_stack[BB_STACKSIZE];
static int top_bb;
static CPTR bcc_target_stack[BB_STACKSIZE];
static int new_bcc_target(CPTR addr)
{
int i;
for (i = 0; i < top_bb; i++)
if (bcc_target_stack[i] == addr)
return 1;
if (top_bb == BB_STACKSIZE)
return 0;
bcc_target_stack[top_bb++] = addr;
return 1;
}
static int bcc_compfn(const void *a, const void *b)
{
CPTR *a1 = (CPTR *)a, *b1 = (CPTR *)b;
if (*a1 == *b1)
printf("BUG!!\n");
if (*a1 < *b1)
return 1;
return -1;
}
static int bb_compfn(const void *a, const void *b)
{
struct bb_info *a1 = (struct bb_info *)a, *b1 = (struct bb_info *)b;
if (a1->h->addr == b1->h->addr)
printf("BUG!!\n");
if (a1->h->addr < b1->h->addr)
return -1;
return 1;
}
static int find_basic_blocks(struct hash_entry *h)
{
int current_bb = 0;
top_bb = 0;
bcc_target_stack[0] = h->addr;
new_bcc_target(h->addr);
while (top_bb > current_bb) {
CPTR addr = bcc_target_stack[current_bb];
int ninsns = 0;
UBYTE *realpc = get_real_address(addr);
UBYTE *rpc_start = realpc;
for(;;) {
CPTR thisinsn_addr = (realpc - rpc_start) + addr;
UBYTE *rpc_save = realpc;
struct instr *dp = translate_getnextinsn(&realpc);
CPTR nextinsn_addr = (realpc - rpc_start) + addr;
if (dp->mnemo == i_RTS || dp->mnemo == i_RTE
|| dp->mnemo == i_RTR || dp->mnemo == i_RTD
|| dp->mnemo == i_JMP || dp->mnemo == i_ILLG)
{
break;
}
if (dp->mnemo == i_BSR || dp->mnemo == i_JSR) {
if (!new_bcc_target(nextinsn_addr))
return 0;
break;
}
if (dp->mnemo == i_DBcc) {
CPTR newaddr = thisinsn_addr + 2 + (WORD)((*(rpc_save+2) << 8) | *(rpc_save+3));
if (!new_bcc_target(nextinsn_addr))
return 0;
if (!new_bcc_target(newaddr))
return 0;
break;
}
if (dp->mnemo == i_Bcc) {
CPTR newaddr;
if (dp->smode == imm1)
newaddr = thisinsn_addr + 2 + (WORD)((*(rpc_save+2) << 8) | *(rpc_save+3));
else
newaddr = thisinsn_addr + 2 + (BYTE)dp->sreg;
if (dp->cc != 0)
if (!new_bcc_target(nextinsn_addr))
return 0;
if (!new_bcc_target(newaddr))
return 0;
break;
}
}
current_bb++;
}
qsort(bcc_target_stack, top_bb, sizeof (CPTR), bcc_compfn);
return 1;
}
static int m68k_scan_func(struct hash_entry *h)
{
int i;
struct hash_block *found_block;
struct hash_entry **hepp;
if (!find_basic_blocks(h))
return 0;
found_block = NULL;
/* First, lock the hash entries we already have to prevent grief */
for (i = 0; i < top_bb; i++) {
struct hash_entry *h = find_hash(bcc_target_stack[i]);
if (h != NULL)
h->locked = 1;
}
/* Allocate new ones */
for (i = 0; i < top_bb; i++) {
struct hash_entry *h = get_hash_for_func(bcc_target_stack[i], 1);
bb_stack[i].h = h;
#if 0 /* This doesn't work in all cases */
if (h->block != NULL && h->block != found_block) {
if (found_block == NULL) {
if (h->block->cpage != NULL)
fprintf(stderr, "Found compiled code\n");
else
found_block = h->block;
} else {
fprintf(stderr, "Multiple blocks found.\n");
if (h->block->cpage == NULL)
forget_block(h->block);
else if (found_block->cpage == NULL) {
forget_block(found_block);
found_block = h->block;
} else
fprintf(stderr, "Bad case.\n");
}
}
#endif
}
if (found_block == NULL) {
found_block = new_block();
found_block->lru_next = &lru_first_block;
found_block->lru_prev = lru_first_block.lru_prev;
found_block->lru_prev->lru_next = found_block;
lru_first_block.lru_prev = found_block;
}
hepp = &found_block->he_first;
found_block->he_first = NULL;
for (i = 0; i < top_bb; i++) {
struct bb_info *bb = bb_stack + i;
if (bb->h->block == NULL) {
num_unused_hash--;
lru_touch(bb->h);
bb->h->block = found_block;
*hepp = bb->h;
hepp = &bb->h->next_same_block;
}
}
*hepp = found_block->he_first;
return 1;
}
struct ea_reg_info {
enum { eat_reg, eat_imem, eat_amem, eat_const } ea_type;
int regs_set:16;
int regs_used:16;
int nr_scratch;
ULONG temp1, temp2;
};
#define MAX_TRANSLATE 2048
struct insn_info_struct {
CPTR address;
struct instr *dp;
int flags_set;
int flags_used;
int flags_live_at_end;
int jump_target;
int jumps_to;
char *compiled_jumpaddr; /* Address to use for jumps to this insn */
char *compiled_fillin; /* Address where to put offset if this is a Bcc */
int regs_set:16;
int regs_used:16;
int stop_translation:2;
int sync_cache:1;
int sync_flags:1;
int ccuser_follows:1;
} insn_info [MAX_TRANSLATE];
#define EA_NONE 0
#define EA_LOAD 1
#define EA_STORE 2
#define EA_MODIFY 4
#if 0
static void analyze_ea_for_insn(amodes mode, int reg, wordsizes size,
struct ea_reg_info *eai,
UBYTE **pcpp, CPTR pca,
int ea_purpose)
{
UBYTE *p = *pcpp;
switch(mode) {
case Dreg:
eai->ea_type = eat_reg;
if (size != sz_long && (ea_purpose & EA_STORE))
ea_purpose |= EA_LOAD;
if (ea_purpose & EA_LOAD)
eai->regs_used |= 1 << reg;
if (ea_purpose & EA_STORE)
eai->regs_set |= 1 << reg;
break;
case Areg:
eai->ea_type = eat_reg;
if (size != sz_long && (ea_purpose & EA_STORE))
printf("Areg != long\n");
if (ea_purpose & EA_LOAD)
eai->regs_used |= 1 << (8+reg);
if (ea_purpose & EA_STORE)
eai->regs_set |= 1 << (8+reg);
break;
case Ad16:
case Aind:
case Apdi:
case Aipi:
eai->ea_type = eat_imem;
eai->regs_used |= 1 << (8+reg);
break;
case Ad8r:
eai->ea_type = eat_imem;
pii->regs_used |= 1 << (8+reg);
eai->temp = (UWORD)((*p << 8) | *(p+1));
r = (eai->temp & 0x7000) >> 12;
(*pcpp) += 2; p += 2;
if (eai->temp1 & 0x8000)
pii->regs_used |= 1 << (8+r);
else
pii->regs_used |= 1 << r;
break;
case PC8r:
eai->ea_type = eat_imem;
eai->temp1 = (UWORD)do_get_mem_word((UWORD *)p);
eai->temp2 = pca + (BYTE)eai->temp1;
(*pcpp) += 2; p += 2;
r = (eai->temp1 & 0x7000) >> 12;
if (eai->temp1 & 0x8000)
pii->regs_used |= 1 << (8+r);
else
pii->regs_used |= 1 << r;
break;
case PC16:
eai->ea_type = eat_amem;
eai->temp1 = pca + (WORD)do_get_mem_word((UWORD *)p);
(*pcpp) += 2;
break;
case absw:
eai->ea_type = eat_amem;
eai->temp1 = (WORD)do_get_mem_word((UWORD *)p);
(*pcpp) += 2;
break;
case absl:
eai->ea_type = eat_amem;
eai->temp1 = (LONG)do_get_mem_long((ULONG *)p);
(*pcpp) += 4;
break;
case imm:
if (size == sz_long)
goto imm2_const;
if (size == sz_word)
goto imm1_const;
/* fall through */
case imm0:
eai->ea_type = eat_imm;
eai->temp1 = (BYTE)*(p+1);
(*pcpp) += 2;
break;
case imm1:
imm1_const:
eai->ea_type = eat_imm;
eai->temp1 = (WORD)do_get_mem_word((UWORD *)p);
(*pcpp) += 2;
break;
case imm2:
imm2_const:
eai->ea_type = eat_imm;
eai->temp1 = (LONG)do_get_mem_long((ULONG *)p);
(*pcpp) += 4;
break;
case immi:
eai->ea_type = eat_imm;
eai->temp1 = (BYTE)reg;
break;
default:
break;
}
}
#endif
static struct bb_info *find_bb(struct hash_entry *h)
{
int i;
if (h == NULL)
printf("Bug...\n");
for (i = 0; i < top_bb; i++)
if (bb_stack[i].h == h)
return bb_stack + i;
if (!quiet_compile)
fprintf(stderr, "BB not found!\n");
return NULL;
}
static int m68k_scan_block(struct hash_block *hb, int *movem_count)
{
struct hash_entry *h = hb->he_first;
int i, iip, last_iip;
int changed, round;
top_bb = 0;
do {
struct bb_info *bb = bb_stack + top_bb;
bb->h = h;
bb->bb_next1 = NULL;
bb->bb_next2 = NULL;
h = h->next_same_block;
top_bb++;
} while (h != hb->he_first);
qsort(bb_stack, top_bb, sizeof (struct bb_info), bb_compfn);
*movem_count = 0;
iip = 0;
for (i = 0; i < top_bb; i++) {
struct bb_info *bb = bb_stack + i;
UBYTE *realpc = get_real_address(bb->h->addr);
UBYTE *rpc_start = realpc;
CPTR stop_addr = 0;
int live_at_start = 31, may_clear_las = 31;
struct insn_info_struct *prev_ii = NULL;
if (i < top_bb - 1)
stop_addr = (bb+1)->h->addr;
bb->first_iip = iip;
for (;;) {
struct insn_info_struct *thisii = insn_info + iip;
CPTR thisinsn_addr = (realpc - rpc_start) + bb->h->addr;
UBYTE *rpc_save = realpc;
struct instr *dp = translate_getnextinsn(&realpc);
CPTR nextinsn_addr = (realpc - rpc_start) + bb->h->addr;
int fset = dp->flagdead == -1 ? 31 : dp->flagdead;
int fuse = dp->flaglive == -1 ? 31 : dp->flaglive;
if (thisinsn_addr == stop_addr) {
bb->bb_next1 = find_bb (find_hash (thisinsn_addr));
break;
}
if (dp->mnemo == i_Scc || dp->mnemo == i_Bcc || dp->mnemo == i_DBcc) {
fset = 0, fuse = cc_flagmask_68k(dp->cc);
if (prev_ii && dp->mnemo != i_Scc) /* Don't use Scc here: ea can cause an exit */
prev_ii->ccuser_follows = 1;
}
may_clear_las &= ~fuse;
live_at_start &= ~(fset & may_clear_las);
thisii->dp = dp;
thisii->address = thisinsn_addr;
thisii->stop_translation = 0;
thisii->ccuser_follows = 0;