-
Notifications
You must be signed in to change notification settings - Fork 13
/
backend-z8.c
3736 lines (3504 loc) · 79.7 KB
/
backend-z8.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
/*
* Z8 code generator
*
* TODO
* - using tcm/tm for bitops
* - using incw/decw for cmp -1 or 1
* - efficient comparisons
* - flag switching on jtrue/false for comparisons
* - CCONLY Z80 style
*/
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include "compiler.h"
#include "backend.h"
#define BYTE(x) (((unsigned)(x)) & 0xFF)
#define WORD(x) (((unsigned)(x)) & 0xFFFF)
#define ARGBASE 2 /* Bytes between arguments and locals if no reg saves */
#define T_NREF (T_USER) /* Load of C global/static */
#define T_CALLNAME (T_USER+1) /* Function call by name */
#define T_NSTORE (T_USER+2) /* Store to a C global/static */
#define T_LREF (T_USER+3) /* Ditto for local */
#define T_LSTORE (T_USER+4)
#define T_LBREF (T_USER+5) /* Ditto for labelled strings or local static */
#define T_LBSTORE (T_USER+6)
#define T_RREF (T_USER+7)
#define T_RSTORE (T_USER+8)
#define T_RDEREF (T_USER+9) /* *regptr */
#define T_REQ (T_USER+10) /* *regptr = */
#define T_RDEREFPLUS (T_USER+11) /* *regptr++ */
#define T_REQPLUS (T_USER+12) /* *regptr++ = */
#define T_LSTREF (T_USER+13) /* reference via a local ptr to struct field */
#define T_LSTSTORE (T_USER+14) /* store ref via a local ptr to struct field */
/*
* State for the current function
*/
static unsigned frame_len; /* Number of bytes of stack frame */
static unsigned sp; /* Stack pointer offset tracking */
static unsigned argbase; /* Argument offset in current function */
static unsigned unreachable; /* Code following an unconditional jump */
static unsigned label_count; /* Used for internal labels X%u: */
static unsigned r14_sp; /* R14/15 address relative to SP */
static unsigned r14_valid; /* R14/15 are a valid local ptr */
static unsigned r2_sp; /* R2/3 address relative to SP */
static unsigned r2_valid; /* R2/3 are a valid local ptr */
static uint8_t r_val[16];
static uint8_t r_type[16]; /* For now just unknown and const */
#define RV_UNKNOWN 0
#define RV_CONST 1
static struct node ac_node; /* What is in AC 0 type = unknown */
/* Minimal tracking on the working register only */
static void invalidate_ac(void)
{
if (ac_node.op)
printf(";invalidate ac\n");
ac_node.op = 0;
}
static void set_ac_node(struct node *n)
{
register unsigned op = n->op;
/* printf(";ac node now O %u T %u V %lu\n",
op, n->type, n->value); */
/* Whatever was in AC just got stored so we are now a valid lref */
if (op == T_LSTORE)
op = T_LREF;
if (op == T_RSTORE)
op = T_RREF;
if (op != T_LREF && op != T_RREF) {
ac_node.type = 0;
return;
}
memcpy(&ac_node, n, sizeof(ac_node));
ac_node.op = op;
}
static void flush_all(unsigned f)
{
}
static void gen_symref(struct node *n)
{
register unsigned op = n->op;
register unsigned v = n->value;
if (op == T_NREF || op == T_NSTORE || op == T_NAME)
printf("\t.word _%s+%u\n", namestr(n->snum), v);
else if (op == T_LBREF || op == T_LBSTORE || op == T_LABEL)
printf("\t.word T%u+%u\n", n->val2, v);
else
error("gsr");
}
#ifdef SUPER8
/* The Z8800 register arrangement is quite different */
#define R_SPL 217
#define R_SPH 216
/* 0xx is special encoding form. We borrow that internally to indicate
using the currennt working register byte (usually encoded as 0-3) */
#define R_ACCHAR 0xC3
#define R_ACINT 0xC2 /* Start reg for ptr/int */
#define R_ACPTR 0xC2
#define R_ACLONG 0xC0
#define R_ISAC(x) (((x) & 0xF0) == 0xC0)
#define R_AC 0xC0
#define REGBASE 2 /* Reg vars 4,6,8,10 numbered 1,2,3,4 */
#define R_REG(x) (((x) * 2) + REGBASE)
#define R_REG_S(x,s) (((x) * 2) + REGBASE + (2 - (s)))
#define R_WORK 12 /* 12/13 are work ptr */
#define R_INDEX 14 /* 14/15 are usually index */
#else
#define R_SPL 255
#define R_SPH 254
/* 0xEx is special encoding form. We borrow that internally to indicate
using the currennt working register byte (usually encoded as 0-3) */
#define R_ACCHAR 0xE3
#define R_ACINT 0xE2 /* Start reg for ptr/int */
#define R_ACPTR 0xE2
#define R_ACLONG 0xE0
#define R_ISAC(x) (((x) & 0xF0) == 0xE0)
#define R_AC 0xE0
#define REGBASE 2 /* Reg vars 4,6,8,10 numbered 1,2,3,4 */
#define R_REG(x) (((x) * 2) + REGBASE)
#define R_REG_S(x,s) (((x) * 2) + REGBASE + (2 - (s)))
#define R_WORK 12 /* 12/13 are work ptr */
#define R_INDEX 14 /* 14/15 are usually index */
#endif
/*
* Beginning of hooks for modify tracking etc
*/
static int find_const(int r, unsigned char v)
{
int i = 0;
uint8_t *p = r_type;
/* Best option is always same register */
if (r != -1 && r_type[r] == RV_CONST && r_val[r] == v)
return r;
/* Look for a matching entry */
while(i < 16) {
if (*p++ == RV_CONST && r_val[i] == v)
return i;
i++;
}
return -1;
}
static void r_modify(unsigned r, unsigned size)
{
if (R_ISAC(r))
r = 4 - size;
while(size--) {
if (r == 14 || r == 15)
r14_valid = 0;
if (r == 2 || r == 3) {
r2_valid = 0;
invalidate_ac();
}
r_type[r++] = RV_UNKNOWN;
}
}
/* Until we do value track */
static void r_set(unsigned r, unsigned v)
{
if (r == 14 || r == 15)
r14_valid = 0;
if (r == 2 || r == 3) {
r2_valid = 0;
invalidate_ac();
}
r_val[r] = v & 0xFF;
r_type[r] = RV_CONST;
}
static unsigned r_do_adjust(unsigned r, int by, unsigned size, unsigned *c)
{
unsigned rb = r;
unsigned n;
if (c)
*c = 0;
r += size - 1;
if (r_type[r] == RV_CONST) {
n = r_val[r] + (by & 0xFF);
/* Carry occurred */
if (c && (n & 0x100))
*c = 1;
r_val[r] = n & 0xFF;
while(--size) {
--r;
by >>= 8;
if (r_type[r] == RV_CONST) {
n = r_val[r] + (by & 0xFF) + (n >> 8);
r_val[r] = n;
} else
break;
}
}
/* Mark any unknown bytes as unknown */
if (size)
r_modify(rb, size);
return size;
}
static void r_adjust(unsigned r, int by, unsigned size, unsigned *c)
{
if (r == 14 && size == 2 && r14_valid) {
r14_sp += by;
r_do_adjust(14, by, 2, c);
r14_valid = 1;
} else if (r == 2 && size == 2 && r2_valid) {
r2_sp += by;
r_do_adjust(2, by, 2, c);
r2_valid = 1;
} else
size = r_do_adjust(r, by , size, c);
}
static void invalidate_all(void)
{
invalidate_ac();
r_modify(0, 16);
}
/*
* These will eventually be smart and track values etc but for now
* just do dumb codegen
*/
static void r_dec(unsigned r)
{
printf("\tdec r%u\n", r);
r_adjust(r, -1, 1, NULL);
}
static void r_inc(unsigned r)
{
printf("\tinc r%u\n", r);
r_adjust(r, 1, 1, NULL);
}
static void rr_decw(unsigned rr)
{
if (R_ISAC(rr))
rr = R_ACPTR;
printf("\tdecw rr%u\n", rr);
r_adjust(rr, -1, 2, NULL);
}
static void RR_decw(unsigned rr)
{
printf("\tdecw %u\n", rr);
}
static void rr_incw(unsigned rr)
{
if (R_ISAC(rr))
rr = R_ACPTR;
printf("\tincw rr%u\n", rr);
r_adjust(rr, 1, 2, NULL);
}
static void RR_incw(unsigned rr)
{
printf("\tincw %u\n", rr);
}
static void load_r_name(unsigned r, struct node *n, unsigned off)
{
const char *c = namestr(n->snum);
if (R_ISAC(r)) {
r = 2;
set_ac_node(n);
}
r_modify(r, 2);
#ifdef SUPER8
if (!(r & 1)) {
printf("\tldw rr%u,#_%s+%u\n", r, c, off);
return;
}
#endif
printf("\tld r%u,#>_%s+%u\n", r, c, off);
printf("\tld r%u,#<_%s+%u\n", r + 1, c, off);
}
/* ?? should these be including n->value as well */
static void load_r_label(unsigned r, struct node *n, unsigned off)
{
if (R_ISAC(r)) {
r = 2;
set_ac_node(n);
}
r_modify(r, 2);
#ifdef SUPER8
if (!(r & 1)) {
printf("\tldw rr%u,#T%u+%u\n", r, n->val2, off);
return;
}
#endif
printf("\tld r%u,#>T%u+%u\n", r, n->val2, off);
printf("\tld r%u,#<T%u+%u\n", r + 1, n->val2, off);
}
static void load_r_r(unsigned r1, unsigned r2)
{
if (R_ISAC(r1))
r1 = r1 & 0x0F;
if (R_ISAC(r2))
r2 = r2 & 0x0F;
if (r1 < 4)
invalidate_ac();
r_modify(r1, 1);
printf("\tld r%u,r%u\n", r1, r2);
}
static void load_rr_rr(unsigned r1, unsigned r2)
{
if (R_ISAC(r1))
r1 = r1 & 0x0F;
if (R_ISAC(r2))
r2 = r2 & 0x0F;
if (r1 < 4)
invalidate_ac();
r_modify(r1, 2);
#ifdef SUPER8
if (!((r1 | r2) & 1)) {
printf("\tldw rr%u,rr%u\n", r1, r2);
return;
}
#endif
printf("\tld r%u,r%u\n", r1, r2);
printf("\tld r%u,r%u\n", r1 + 1, r2 + 1);
}
static void load_r_R(unsigned r1, unsigned r2)
{
if (R_ISAC(r1))
r1 = r1 & 0x0F;
r_modify(r1,1);
printf("\tld r%u,%u\n", r1, r2);
}
#ifdef SUPER8
static void load_rr_RR(unsigned r1, unsigned r2)
{
if (R_ISAC(r1))
r1 = r1 & 0x0F;
r_modify(r1,2);
if (!((r1 | r2) & 1)) {
printf("\tldw rr%u,%u\n", r1, r2);
return;
}
printf("\tld r%u,%u\n", r1, r2);
printf("\tld r%u,%u\n", r1 + 1, r2 + 1);
}
#else
static void add_rr_RR(unsigned r1, unsigned r2)
{
if (R_ISAC(r1))
r1 = r1 & 0x0F;
r_modify(r1,2);
printf("\tadd r%u,%u\n", r1 + 1, r2 + 1);
printf("\tadc r%u,%u\n", r1, r2);
}
#endif
static void load_R_r(unsigned r1, unsigned r2)
{
if (R_ISAC(r2))
r2 = r2 & 0x0F;
printf("\tld %u,r%u\n", r1, r2);
}
/* Low level ops so we can track them later */
static void op_r_c(unsigned r1, unsigned val, const char *op)
{
int i = find_const(val, -1);
if (r1 < 4)
invalidate_ac();
/* It's usually better to use reg/reg when we can. For some
ops it is smaller for others it makes no change */
if (i != -1)
printf("\t%s r%u,r%u\n", op, r1, i);
else
printf("\t%s r%u,#%u\n", op, r1, val);
r_modify(r1, 1);
}
static void op_r_r(unsigned r1, unsigned r2, const char *op)
{
if (r1 < 4)
invalidate_ac();
r_modify(r1, 1);
printf("\t%s r%u,r%u\n", op, r1, r2);
}
static void opnoeff_r_r(unsigned r1, unsigned r2, const char *op)
{
printf("\t%s r%u,r%u\n", op, r1, r2);
}
static void load_r_constb(unsigned r, unsigned char v)
{
/* Once we do reg tracking we'll be able to deal with dups
using ld r1,r2 */
int i;
if (R_ISAC(r))
r &= 0x0F;
if (v == 0) {
if (r_type[r] != RV_CONST || r_val[r] != 0) {
printf("\tclr r%u\n", r);
r_set(r, v);
}
return;
} else if (r_type[r] == RV_CONST && r_val[r] == v - 1) {
printf("\tinc r%u\n", r);
r_set(r, v);
return;
} else {
i = find_const(r, v);
if (i == r) {
printf("; r%u alread %u\n", r, v);
return;
}
if (i != -1) {
printf("; r%u get %u from %u\n", r, v, i);
load_r_r(r, i);
r_set(r, v);
return;
}
}
printf("\tld r%u,#%u\n", r, v);
r_set(r, v);
}
static void load_r_constw(unsigned r, unsigned v)
{
/* The Super8 has word load */
#ifdef SUPER8
if (v) {
printf("\tldw rr%u, #%u\n", r, v);
r_set(r, v >> 8);
r_set(r + 1, v);
return;
}
#endif
load_r_constb(r, v >> 8);
load_r_constb(r + 1, v);
}
static void load_r_const(unsigned r, unsigned long v, unsigned size)
{
if (R_ISAC(r))
r = 4 - size;
/* Lots to do here for optimizing - using clr, copying bytes
between regs */
if (size == 4) {
load_r_constw(r, v >> 16);
r += 2;
size -= 2;
}
if (size == 2)
load_r_constw(r, v);
else
load_r_constb(r, v);
}
static void add_r_const(unsigned r, unsigned long v, unsigned size)
{
int i;
unsigned c;
if (R_ISAC(r))
r = 3;
else
r += size - 1;
if (r < 4)
invalidate_ac();
/* Eliminate any low bytes that are not changed */
while(size && (v & 0xFF) == 0x00) {
r--;
size--;
v >>= 8;
}
if (size == 0)
return;
/* ADD to r%d is 3 bytes so the only useful cases are word inc/dec of
1 which are two bytes as incw/decw and four as add/adc */
if (size == 2) {
if (v == 2) {
rr_incw(r - 1);
rr_incw(r - 1);
return;
}
if ((signed)v == -1) {
rr_decw(r - 1);
return;
}
if (v == 1) {
rr_incw(r - 1);
return;
}
if ((signed)v == -2) {
rr_decw(r - 1);
rr_decw(r - 1);
return;
}
}
/* inc rn is 1 byte, dec rn is 2 ! */
if (size == 1) {
if (v == 2) {
r_inc(r);
r_inc(r);
return;
}
if ((signed)v == -1) {
r_dec(r);
return;
}
if (v == 1) {
r_inc(r);
return;
}
/* For -2 dec, dec is 4 bytes whilst add is 3 */
}
/* Enabling this breaks divmod TODO debug the trace */
i = find_const(v & 0xFF, -1);
i = -1;
if (i != -1)
printf("\tadd r%u,r%u\n", r, i);
else
printf("\tadd r%u,#%u\n", r, (unsigned)v & 0xFF);
r_adjust(r--, v & 0xFF, 1, &c);
while(--size) {
v >>= 8;
i = find_const(v & 0xFF, -1);
i = -1;
if (i != -1)
printf("\tadc r%u,r%u\n", r, i);
else
printf("\tadc r%u,#%u\n", r, (unsigned)v & 0xFF);
r_adjust(r--, (v + c) & 0xFF, 1, &c);
}
}
static void add_R_const(unsigned r, unsigned long v, unsigned size)
{
int i;
r += size - 1;
/* Eliminate any low bytes that are not changed */
while(size && (v & 0xFF) == 0x00) {
r--;
size--;
v >>= 8;
}
if (size == 0)
return;
/* TODO: spot cases to use inc/incw/dec/decw */
i = find_const(v & 0xFF, -1);
if (i != -1)
printf("\tadd %u,r%u\n", r--, i);
else
printf("\tadd %u,#%u\n", r--, (unsigned)v & 0xFF);
while(--size) {
v >>= 8;
i = find_const(v & 0xFF, -1);
if (i != -1)
printf("\tadc %u,r%u\n", r--, i);
else
printf("\tadc %u,#%u\n", r--, (unsigned)v & 0xFF);
}
}
/* Both registers may be the same */
static void add_r_r(unsigned r1, unsigned r2, unsigned size)
{
if (R_ISAC(r1))
r1 = 3;
else
r1 += size - 1;
if (R_ISAC(r2))
r2 = 3;
else
r2 += size - 1;
if (r1 < 4)
invalidate_ac();
r_modify(r1, 2);
printf("\tadd r%u,r%u\n", r1--, r2--);
while(--size)
printf("\tadc r%u,r%u\n", r1--, r2--);
}
static void sub_r_r(unsigned r1, unsigned r2, unsigned size)
{
if (R_ISAC(r1))
r1 = 3;
else
r1 += size - 1;
if (R_ISAC(r2))
r2 = 3;
else
r2 += size - 1;
if (r1 < 4)
invalidate_ac();
r_modify(r1 - size + 1, size);
printf("\tsub r%u,r%u\n", r1--, r2--);
while(--size)
printf("\tsbc r%u,r%u\n", r1--, r2--);
}
static void sub_r_const(unsigned r, unsigned long v, unsigned size)
{
if (R_ISAC(r))
r = 3;
else
r += size - 1;
if (r < 4)
invalidate_ac();
/* SUB to r%d is 2 bytes so the only useful cases are word inc/dec of
1 which are two bytes as incw/decw and four as add/adc */
if (size == 2) {
if (v == 1) {
rr_decw(r - 1);
return;
}
if ((signed)v == -1) {
rr_decw(r - 1);
return;
}
}
if (size == 1) {
if (v == 1) {
r_dec(r);
return;
}
if (v == -1) {
r_inc(r);
return;
}
if (v == -2) {
r_inc(r);
r_inc(r);
return;
}
}
r_adjust(r - size + 1, -v, size, NULL);
/* Eliminate any low bytes that are not changed */
while(size && (v & 0xFF) == 0x00) {
r--;
size--;
v >>= 8;
}
if (size == 0)
return;
/* TODO: adjust and carry optimization link plus */
printf("\tsub r%u,#%u\n", r--, (unsigned)v & 0xFF);
while(--size) {
v >>= 8;
printf("\tsbc r%u,#%u\n", r--, (unsigned)v & 0xFF);
}
}
/* Same FIXME as cmp_r_const. Will be far better once we have cc tests */
static void cmpne_r_0(unsigned r, unsigned size)
{
if (R_ISAC(r))
r = 4 - size;
else
load_r_constb(3, 0);
if (size == 1)
printf("\tor r3, r%u\n", r);
else while(--size)
printf("\tor r3, r%u\n", r++);
r_modify(3, 1);
load_r_constb(2,0);
printf("\tjr z, X%u\n", ++label_count);
load_r_constb(3, 1);
printf("X%u:\n", label_count);
}
static void cmpeq_r_0(unsigned r, unsigned size)
{
/* Will be able to do better for T_BOOL cases and T_BANG */
cmpne_r_0(r, size);
op_r_c(3, 1, "xor");
r_modify(3, 1);
}
static void mono_r(unsigned r, unsigned size, const char *op)
{
if (R_ISAC(r))
r = 4 - size;
if (r < 4)
invalidate_ac();
r_modify(r, size);
while(size--)
printf("\t%s r%u\n", op, r++);
}
static void djnz_r(unsigned r, unsigned l)
{
if (R_ISAC(r))
r = 3;
r_modify(r, 1);
printf("\tdjnz r%u, X%u\n", r, l);
}
static unsigned label(void)
{
invalidate_all();
unreachable = 0;
printf("X%u:\n", ++label_count);
return label_count;
}
/*
* Perform a right shift of a register set signed or unsigned.
* Doesn't currently know about 8bit at a time shifts for unsigned
* in particular. Might want to make long non fast const a helper ?
*/
static void rshift_r(unsigned r, unsigned size, unsigned l, unsigned uns)
{
unsigned x;
if (R_ISAC(r))
r = 4 - size;
if (r < 4)
invalidate_ac();
r_modify(r, size);
l &= (8 * size) - 1;
if (l == 0)
return;
if (uns) {
/* These can only occur for 32bit shift */
if (l == 24) {
load_r_r(r + 3, r);
load_r_constw(r, 0);
load_r_constb(r + 2, 0);
return;
}
if (l == 16) {
load_rr_rr(r + 2, r);
load_r_constw(r, 0);
return;
}
if (l == 8) {
if (size == 2) {
load_r_r(r + 1, r);
load_r_constb(r, 0);
} else {
load_r_r(r + 1, r);
load_r_r(r + 2, r + 1);
load_r_r(r + 3, r + 2);
load_r_constb(r, 0);
}
return;
}
}
if (l > 1) {
load_r_constb(R_INDEX, l);
x = label();
}
if (uns)
printf("\trcf\n\trrc r%u\n", r++);
else
printf("\tsra r%u\n", r++);
while(--size)
printf("\trrc r%u\n", r++);
if (l > 1)
djnz_r(R_INDEX, x);
}
#define OP_AND 0
#define OP_OR 1
#define OP_XOR 2
static void logic_r_const(unsigned r, unsigned long v, unsigned size, unsigned op)
{
const char *opn = "and\0or\0\0xor" + op * 4;
unsigned n;
if (R_ISAC(r))
r = 4;
else
r += size;
if (r <= 4)
invalidate_ac();
/* R is now 1 after low byte */
while(size--) {
r--;
n = v & 0xFF;
v >>= 8;
if (n == 0xFF) {
if (op == OP_OR)
load_r_constb(r, 0xFF);
else if (op == OP_XOR) {
printf("\tcom r%u\n", r);
r_modify(r, 1);
}
/* and nothing for and FF */
continue;
}
else if (n == 0x00) {
if (op == OP_AND)
load_r_constb(r, 0x00);
/* OR and XOR do nothing */
continue;
} else {
int i = find_const(n, -1);
if (i != -1)
printf("\t%s r%u, r%u\n", opn, r, i);
else
printf("\t%s r%u, #%u\n", opn, r, n);
/* TODO: can calc this if know, not clear it is useful */
r_modify(r, 1);
}
}
}
static void logic_r_r(unsigned r1, unsigned r2, unsigned size, unsigned op)
{
const char *opn = "and\0or\0\0xor" + op * 4;
if (R_ISAC(r1))
r1 = 4;
else
r1 += size;
if (R_ISAC(r2))
r2 = 4;
else
r2 += size;
if (r1 <= 4)
invalidate_ac();
r_modify(r1, size);
/* R is now 1 after low byte */
while(size--)
printf("\t%s r%u, r%u\n", opn, --r1, --r2);
}
static void load_l_sprel(unsigned r, unsigned off)
{
#ifdef SUPER8
load_rr_RR(r, R_SPH);
add_r_const(r, off, 2);
#else
/* On Z8 we have no ldw #const but we have a good chance
that this will turn into a clr somewhere or a transfer */
load_r_const(r, off, 2);
add_rr_RR(r, R_SPH);
#endif
if (r == R_INDEX) {
r14_valid = 1;
r14_sp = off - sp;
printf(";r14 valid %d\n", r14_sp);
}
if (r == 2) {
r2_valid = 1;
r2_sp = off - sp;
printf(";r2 valid %d\n", r2_sp);
}
}
static void load_l_mod(unsigned r, unsigned rs, unsigned diff, unsigned off)
{
if (r != rs) {
load_r_r(r, rs);
load_r_r(r + 1, rs + 1);
}
add_r_const(r, -diff, 2);
if (r == R_INDEX) {
r14_valid = 1;
r14_sp = off - sp;
printf(";r14 valid %d\n", r14_sp);
}
if (r == 2) {
r2_valid = 1;
r2_sp = off - sp;
printf(";r2 valid %d\n", r2_sp);
}
}
static unsigned load_l_cost(int diff)
{
unsigned n = abs(diff);
if (n <= 2)
return 2 * n;
return 6;
}
static void load_r_local(unsigned r, unsigned off)
{
int diff14;
int diff2;
unsigned cost2 = 0xFFFF, cost14 = 0xFFFF, costsp;
if (R_ISAC(r))
r = 2;
diff14 = r14_sp - (off - sp);
diff2 = r2_sp - (off - sp);
printf("; load r local %d\n", off);
if (r14_valid)
printf(";14: local %d (cache %d) diff %d\n", off - sp, r14_sp, diff14);
if (r2_valid)
printf(";2: local %d (cache %d) diff %d\n", off - sp, r2_sp, diff2);
/* Work out the cost for each */
if (r14_valid) {
cost14 = load_l_cost(diff14);
if (r != 14)
cost14 += 4;
}
if (r2_valid) {
cost2 = load_l_cost(diff2);
if (r != 2)
cost2 += 4;
}
costsp = load_l_cost(off) + 6;
printf(";costs r2 %d r4 %d rsp %d\n", cost2, cost14, costsp);
/* Cheapest is to load from SP */
if (costsp <= cost2 && costsp <= cost14) {
load_l_sprel(r, off);
return;
}
if (cost2 < cost14)
load_l_mod(r, 2, diff2, off);
else
load_l_mod(r, 14, diff14, off);
}
static void load_local_helper(unsigned v, unsigned size)
{
/* Only works for R_AC case */
/* We add the extra 2 because the call adjusted the stack as well */
v += sp;
if (v < 254) {
load_r_const(R_INDEX + 1, v + 2, 1);
printf("\tcall __gargr%u\n", size);
r_modify(14,2);