-
Notifications
You must be signed in to change notification settings - Fork 13
/
be-codegen-z80.c
2200 lines (2108 loc) · 52.2 KB
/
be-codegen-z80.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
/*
* Z80 Backend
*
* The Z80 essentially has three ways of dealing with locals
*
* 1. 8080 style LD HL,offset ADD HL,SP { LD E,(HL), INC HL, LD D,(HL) }
* 7 bytes, 21 + 20 -> 41 clocks.
* 2. Call helpers Akin again to the 8080 port
* 3 - 5 bytes, but slower (28 cycles minimum extra cost) 66 for word helper
* 3. IX frame pointer style
* LD E,(ix + n), LD D,(ix + n + 1)
* 6 bytes 38 cycles
* much higher function entry/exit cost
*
* We use IX, IY and BC as "register" variables. If IX or IY is free and we are not size
* optimizing we use IX or IY as a frame pointer. If not helpers.
*
* The situation only improves if we get to Z280 or Rabbit processors
*
* R2000/R3000: LD HL,(SP + n) 8bit unsigned (ditto IX IY)
* LD HL,(HL + n) ; IX+ IY+
* LD (HL + n), HL ; IX+ IY+
* LD (SP + n), HL ; IX+ IY+
*
* Z80n As Z80 but can add A and constants to 8/16bit directly which helps in
* some stack accessing by avoiding a load and push constant
*
* eZ80 Adds LD BC/DE?HL,(HL) and reverse or HL/IX/IY so now frame pointer
* is actually useful
*
* Z280 Can use IX+off for 16bit ops including loads and add so like the ez80
* frame pointers look useful, but this extends to other ops like ADDW INCW
* etc. Has LDA (ie LEA) so can LDA IX,(SP - n) etc. Can also load and
* save IX IY and HL from (SP + n). Can also push constants and relative
* addresses
*
*/
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "compiler.h"
#include "backend.h"
#include "backend-z80.h"
#define LWDIRECT 24 /* Number of __ldword1 __ldword2 etc forms for fastest access */
const char ccnormal[] = "nzz ";
const char ccinvert[] = "z nz";
/*
* State for the current function
*/
unsigned frame_len; /* Number of bytes of stack frame */
unsigned sp; /* Stack pointer offset tracking */
unsigned argbase; /* Argument offset in current function */
unsigned unreachable; /* Code after an unconditional jump */
unsigned func_cleanup; /* Zero if we can just ret out */
unsigned use_fp; /* Using a frame pointer this function */
static unsigned label; /* Used to hand out local labels of the form X%u */
const char *ccflags = ccnormal;/* True, False flags */
static const char *regnames[] = { /* Register variable names */
NULL,
"bc",
"ix",
"iy"
};
void gen_tree(struct node *n)
{
codegen_lr(n);
printf(";\n");
/* printf(";SP=%u\n", sp); */
}
/*
* Try and generate shorter code for stuff we can directly access
*/
/*
* Locals are complicated on the Z80. There is no simply cheap way
* to access them so put all the gunk in a helper we can refine
* over time for different CPU variants and frame pointers etc
*
* Given a size and offset on the stack, write the code to put
* the value into HL without trashing BC or DE
*
* If to_de is set we want the result in de, otherwise HL
*/
unsigned generate_lref(register unsigned v, register unsigned size, unsigned to_de)
{
const char *name;
const char *rp = "hl";
if (to_de)
rp = "de";
if (size == 4)
return 0;
/* Rabbit amd Z280 have LD HL,(SP + n) */
if (HAS_LDHLSP && v + sp <= 255 && !to_de) {
/* FIXME: We will load an extra byte for 16bit, so hopefully non MMIO TODO */
printf("\tld hl,(sp+%u)\n", v + sp);
return 1;
}
/* This has to be a local so if it is byte sized we will load the
low byte and crap above and all is good */
if (v + sp == 0 && size <= 2) {
printf("\tpop %s\n\tpush %s\n", rp, rp);
return 1;
}
/* Frame pointers */
if (use_fp && v < 128 - size) {
printf("\tld %c,(iy + %u)\n", rp[1], v);
if (size == 2)
printf("\tld %c,(iy + %u)\n", rp[0], v + 1);
return 1;
}
/* Correct for current SP location */
v += sp;
/* Byte load is shorter inline for most cases */
if (size == 1 && (!optsize || v >= LWDIRECT)) {
if (to_de)
printf("\tex de,hl\n");
printf("\tld hl,0x%x\n\tadd hl,sp\n\tld l,(hl)\n", v);
if (to_de)
printf("\tex de,hl\n");
return 1;
}
/* Word load is long winded on Z80 */
if (size == 2 && opt > 2) {
printf("\tld hl,0x%x\n\tadd hl,sp\n", WORD(v));
if (IS_RABBIT)
printf("\tld hl,(hl + %u\n)\n", 0);
else if (IS_EZ80 && to_de)
printf("\tld de,(hl)\n");
else if (to_de)
printf("\tld e,(hl)\n\tinc hl\n\tld d,(hl)\n");
else
printf("\tld a,(hl)\n\tinc hl\n\tld h,(hl)\n\tld l,a\n");
return 1;
}
/* Via helper magic for compactness on Z80 */
if (size == 1)
name = "ldbyte";
else
name = "ldword";
/* Check for lref out of range. Our long form helpers currently
trash DE so can't be used when we are loading DE */
if (to_de && v >= 253)
error("lrr");
/* We do a call so the stack offset is two bigger */
if (to_de)
printf("\tex de,hl\n");
if (v < LWDIRECT)
printf("\tcall __%s%u\n", name, v + 2);
else if (v < 253)
printf("\tcall __%s\n\t.byte %u\n", name, v + 2);
else
printf("\tcall __%sw\n\t.word %u\n", name, v + 2);
if (to_de)
printf("\tex de,hl\n");
return 1;
}
/* Get an lref value into A without destroying HL. DE is fair game. This one
is allowed to fail */
unsigned generate_lref_a(register unsigned v)
{
/* Correct for current SP location */
v += sp;
/* Sadly pop af gets us the wrong byte and sneakily adjusting
sp kills us in an interrupt handler. This is is still cheaper
than messing around */
if (v == 0) {
printf("\tpop de\n\tpush de\n");
printf("\tld a,e\n");
return 1;
}
/* Offset 1 does work however, although we almost never get that */
if (v == 1) {
printf("\tpop af\n\tpush af\n");
return 1;
}
if (use_fp) {
printf("\tld a,(iy + %d)\n", v - sp);
return 1;
}
/* Byte load and inline are about the same size so inline for
speed */
printf("\tex de,hl\n");
printf("\tld hl,0x%x\n\tadd hl,sp\n\tld a,(hl)\n", v);
printf("\tex de,hl\n");
return 1;
}
/*
* Return 1 if the node can be turned into direct access. The VOID check
* is a special case we need to handle stack clean up of void functions.
*
* Note that we think of RDEREF as accessible. There is a specific case
* it is not which is loading IX or IY indirect from a pointer. The
* register callers must handle the fact this returns 1 and load_r_with
* then says 0
*/
static unsigned access_direct(register struct node *n)
{
register unsigned op = n->op;
/* We can direct access integer or smaller types that are constants
global/static or string labels */
/* For now we only do word sized lrefs FIXME */
if (op == T_LREF && get_size(n->type) == 2 && n->value + sp < 253)
return 1;
if (op != T_CONSTANT && op != T_NAME && op != T_LABEL && op != T_NREF && op != T_LBREF && op != T_REG && op != T_RREF && op != T_RDEREF)
return 0;
if (!PTR(n->type) && (n->type & ~UNSIGNED) > CSHORT)
return 0;
return 1;
}
/*
* Get something that passed the access_direct check into de. Could
* we merge this with the similar hl one in the main table ?
*/
static unsigned load_r_with(register const char *rp, register struct node *n)
{
register unsigned v = WORD(n->value);
char r = *rp;
switch(n->op) {
case T_NAME:
printf("\tld %s,_%s+%u\n", rp, namestr(n->snum), v);
return 1;
case T_LABEL:
printf("\tld %s,T%u+%u\n", rp, n->val2, v);
return 1;
case T_CONSTANT:
/* We know this is not a long from the checks above */
printf("\tld %s,0x%x\n", rp, v);
return 1;
case T_NREF:
/* We know it is int or pointer */
printf("\tld %s,(_%s+%u)\n", rp, namestr(n->snum), v);
return 1;
break;
/* TODO: fold together cleanly with NREF */
case T_LBREF:
printf("\tld %s,(T%u+%u)\n", rp, n->val2, v);
return 1;
case T_RREF:
/* Assumes that BC isn't corrupted yet so is already the right value. Use
this quirk with care */
if (n->value == 1 && r != 'i') {
if (r == 'd')
printf("\tld d,b\n\tld e,c\n");
else
printf("\tld h,b\n\tld l,c\n");
return 1;
}
printf("\tpush %s\n\tpop %s\n", regnames[n->value], rp);
return 1;
case T_RDEREF:
/* One oddity here - we can't load IX or IY from (ix) or (iy) */
if (*rp == 'i')
return 0;
printf("\tld %c,(%s + %u)\n", rp[1], regnames[n->value], n->val2);
printf("\tld %c,(%s + %u)\n", *rp, regnames[n->value], n->val2 + 1);
return 1;
default:
return 0;
}
return 1;
}
static unsigned load_de_with(register struct node *n)
{
/* DE we can lref because our helpers don't touch DE so we can
ex de,hl around it */
if (n->op == T_LREF)
return generate_lref(n->value, 2, 1);
return load_r_with("de", n);
}
static unsigned load_hl_with(register struct node *n)
{
/* An HL lref is fine as we can trash HL and A to get HL */
if (n->op == T_LREF)
return generate_lref(n->value, 2, 0);
return load_r_with("hl", n);
}
static unsigned load_a_with(register struct node *n)
{
switch(n->op) {
case T_CONSTANT:
/* We know this is not a long from the checks above */
printf("\tld a,0x%x\n", BYTE(n->value));
break;
case T_NREF:
printf("\tld a,(_%s+%u)\n", namestr(n->snum), WORD(n->value));
break;
case T_LBREF:
printf("\tld a,(T%u+%u)\n", n->val2, WORD(n->value));
break;
case T_RREF:
/* TODO: ix/iy (can they happen ? */
if (n->value == 1) {
printf("\tld a,c\n");
break;
}
return 0;
case T_RDEREF:
if (n->value == 1) {
printf("\tld a,(bc)\n");
break;
}
printf("\tld a,(%s+%u)\n", regnames[n->value], n->val2);
break;
case T_LREF:
return generate_lref_a(n->value);
default:
return 0;
}
return 1;
}
static void repeated_op(register const char *o, register unsigned n)
{
while(n--)
printf("\t%s\n", o);
}
static void get_regvar(register unsigned reg, register struct node *n, unsigned s)
{
if (n && (n->flags & NORETURN))
return;
if (reg == 1) {
printf("\tld l,c\n");
if (s == 2)
printf("\tld h,b\n");
return;
}
printf("\tpush %s\n\tpop hl\n", regnames[reg]);
}
static void load_regvar(unsigned r, unsigned s)
{
if (r == 1) {
printf("\tld c,l\n");
if (s == 2)
printf("\tld b,h\n");
return;
}
printf("\tpush hl\n\tpop %s\n", regnames[r]);
}
/* We use "DE" as a name but A as register for 8bit ops... probably ought to rework one day */
static unsigned gen_deop(const char *op, register struct node *n,
register struct node *r, unsigned sign)
{
register unsigned s = get_size(n->type);
if (s > 2)
return 0;
/* Generate ld e, forms of de helpers if the value is below 256 as
is often the case */
if (optsize && s == 2 && r->op == T_CONSTANT) {
unsigned v= r->value;
if (v < 256) {
printf("\tld e,%u\n", v);
printf("\tcall __%s", op);
if (sign)
helper_type(n->type, sign);
printf("0d\n");
return 1;
}
}
if (s == 2) {
if (load_de_with(r) == 0)
return 0;
} else {
if (load_a_with(r) == 0)
return 0;
}
if (sign)
helper_s(n, op);
else
helper(n, op);
return 1;
}
static unsigned gen_compc(const char *op, register struct node *n,
register struct node *r, unsigned sign)
{
unsigned s = get_size(n->type);
/* TODO: Z280 has CPW HL,DE CPW HL, const */
if (r->op == T_CONSTANT) {
/* Some minimal cases to work out how best to use CCONLY */
if (n->op == T_BANGEQ && (n->flags & CCONLY)) {
if (r->value == 0) {
if (s == 1) {
printf("\txor a\n\tcp l\n");
n->flags |= USECC;
return 1;
}
if (s == 2) {
printf("\tld a,h\n\tor l\n");
n->flags |= USECC;
return 1;
}
}
if (s == 1 && r->value == 255) {
printf("\tinc l\n");
n->flags |= USECC;
return 1;
}
if (s == 2 && r->value == 0xFFFF) {
printf("\tld a,h\n\tand l\n\tinc a\n");
n->flags |= USECC;
return 1;
}
if (s == 2 && r->value <= 0xFF) {
/* The xor is a compare resulting in 0 if equal,
and the or h then checks the high byte matches */
printf("\tld a,0x%x\n", (unsigned)r->value);
printf("\txor l\n");
printf("\tor h\n");
n->flags |= USECC;
return 1;
}
if (s == 2 && (r->value & 0xFF) == 0) {
printf("\tld a,0x%x\n", (unsigned)r->value >> 8);
printf("\txor h\n");
printf("\tor l\n");
n->flags |= USECC;
return 1;
}
if (s == 1) {
printf("\tld a,0x%x\n", (unsigned)r->value & 0xFF);
printf("\tcp l\n");
return 1;
}
}
if (n->op == T_EQEQ && (n->flags & CCONLY) && !(n->flags & CCFIXED)) {
if (r->value == 0) {
if (s == 1) {
printf("\txor a\n\tcp l\n");
ccflags = ccinvert;
n->flags |= USECC;
return 1;
}
if (s == 2) {
printf("\tld a,h\n\tor l\n");
ccflags = ccinvert;
n->flags |= USECC;
return 1;
}
}
if (r->value == 255 && s == 1) {
printf("\tinc l\n");
ccflags = ccinvert;
n->flags |= USECC;
return 1;
}
if (r->value == 0xFFFF && s == 2) {
printf("\tld a,h\n\tand l\n\tinc a\n");
ccflags = ccinvert;
n->flags |= USECC;
return 1;
}
if (s == 2 && r->value <= 0xFF) {
/* The xor is a compare resulting in 0 if equal,
and the or h then checks the high byte matches */
printf("\tld a,0x%x\n", (unsigned)r->value);
printf("\txor l\n");
printf("\tor h\n");
ccflags = ccinvert;
n->flags |= USECC;
return 1;
}
if (s == 2 && (r->value & 0xFF) == 0) {
printf("\tld a,0x%x\n", (unsigned)r->value >> 8);
printf("\txor h\n");
printf("\tor l\n");
ccflags = ccinvert;
printf(";EQEQ z true z nz\n");
n->flags |= USECC;
return 1;
}
if (s == 1) {
printf("\tld a,0x%x\n", (unsigned)r->value & 0xFF);
printf("\tcp l\n");
ccflags = ccinvert;
n->flags |= USECC;
return 1;
}
}
/* TODO : float helper */
if (r->value == 0 && r->type != FLOAT) {
char buf[10];
strcpy(buf, op);
strcat(buf, "0");
if (sign)
helper_s(n, buf);
else
helper(n, buf);
n->flags |= ISBOOL;
return 1;
}
}
/* Generate short cases with BC */
if (r->op == T_RREF && s == 2 && r->value == 1 && (n->flags & CCONLY)) {
if (n->op == T_BANGEQ) {
printf("\tor a\n\tsbc hl,bc\n");
n->flags |= USECC;
return 1;
}
if (n->op == T_EQEQ && !(n->flags & CCFIXED)) {
printf("\tor a\n\tsbc hl,bc\n");
ccflags = ccinvert;
n->flags |= USECC;
return 1;
}
}
/* We need to set a variable to the type of branch to use for the condition and then pick it up
(once) in the following branch instruction to do more than the basics */
if (n->op == T_BANGEQ && (n->flags & CCONLY)) {
if (s == 1 && load_a_with(r) == 1) {
printf("\tcp l\n");
n->flags |= USECC;
return 1;
}
if (s == 2 && load_de_with(r) == 1) {
printf("\tor a\n\tsbc hl,de\n");
n->flags |= USECC;
return 1;
}
}
if (n->op == T_EQEQ && (n->flags & CCONLY) && !(n->flags & CCFIXED)) {
if (s == 1 && load_a_with(r) == 1) {
printf("\tcp l\n");
ccflags = ccinvert;
n->flags |= USECC;
return 1;
}
if (s == 2 && load_de_with(r) == 1) {
printf("\tor a\n\tsbc hl,de\n");
ccflags = ccinvert;
n->flags |= USECC;
return 1;
}
}
/* TODO: unsigned gt and lt etc including moving const by one to
get easy to inline form */
if (gen_deop(op, n, r, sign)) {
n->flags |= ISBOOL;
return 1;
}
return 0;
}
static int count_mul_cost(register unsigned n)
{
register int cost = 0;
if ((n & 0xFF) == 0) {
n >>= 8;
cost += 3; /* mov mvi */
}
while(n > 1) {
if (n & 1)
cost += 3; /* push pop add hl,de */
n >>= 1;
cost++; /* dad h */
}
return cost;
}
/* Write the multiply for any value > 0 */
static void write_mul(register unsigned n)
{
unsigned pops = 0;
if ((n & 0xFF) == 0) {
printf("\tld h,l\n\tld l,0\n");
n >>= 8;
}
while(n > 1) {
if (n & 1) {
pops++;
printf("\tpush hl\n");
}
printf("\tadd hl,hl\n");
n >>= 1;
}
while(pops--) {
printf("\tpop de\n\tadd hl,de\n");
}
}
static unsigned can_fast_mul(unsigned s, unsigned n)
{
/* Pulled out of my hat 8) */
unsigned cost = 15 + 3 * opt;
if (s > 2)
return 0;
/* The base cost of the helper is 6 lxi de, n; call, but this may be too aggressive
given the cost of mulde TODO */
if (optsize)
cost = 10;
if (n == 0 || count_mul_cost(n) <= cost)
return 1;
return 0;
}
static void gen_fast_mul(unsigned s, unsigned n)
{
if (n == 0)
printf("\tld hl,0x0\n");
else
write_mul(n);
}
static unsigned gen_fast_div(register unsigned n, unsigned s, unsigned u)
{
u &= UNSIGNED;
if (s != 2)
return 0;
if (n == 1)
return 1;
if (n == 256 && u) {
printf("\tld l,h\n\tld h,0x0\n");
return 1;
}
if (n & (n - 1))
return 0;
if (u) {
while(n > 1) {
printf("\tsrl h\n\trr l\n");
n >>= 1;
}
} else {
int m = n - 1;
printf("\tbit 7,h\n\tjr z,X%u\n", ++label);
/* We can trash DE */
if (m > 0 && m <= 4)
repeated_op("inc hl", m);
else if (m < 0 && m >= -4)
repeated_op("dec hl", -m);
else
printf("\tld de,%u\n\tadd hl,de\n", (n - 1) & 0xFFFF);
printf("X%u:\n", label);
while(n > 1) {
printf("\tsra h\n\trr l\n");
n >>= 1;
}
}
return 1;
}
/* TODO : we could in theory optimize xor 255 with cpl ? */
static unsigned gen_logicc(register struct node *n, unsigned s, const char *op,
unsigned v, unsigned code)
{
register unsigned h = (v >> 8) & 0xFF;
register unsigned l = v & 0xFF;
/* Rabbit has 16bit logic operators */
if (s == 2 && IS_RABBIT) {
if (load_de_with(n) == 0)
return 0;
printf("\t%s hl,de\n", op);
return 1;
}
if (s > 2 || (n && n->op != T_CONSTANT))
return 0;
/* If we are trying to be compact only inline the short ones */
if (optsize && ((h != 0 && h != 255) || (l != 0 && l != 255)))
return 0;
if (s == 2) {
if (h == 0) {
if (code == 1)
printf("\tld h,0x0\n");
}
else if (h == 255 && code != 3) {
if (code == 2)
printf("\tld h,0xff\n");
} else {
printf("\tld a,h\n\t%s 0x%x\n\tld h,a\n", op, h);
}
}
if (l == 0) {
if (code == 1)
printf("\tld l,0x0\n");
} else if (l == 255 && code != 3) {
if (code == 2)
printf("\tld l,0xff\n");
} else {
printf("\tld a,l\n\t%s 0x%x\n\tld l,a\n", op, l);
}
return 1;
}
static unsigned gen_fast_remainder(register unsigned n, unsigned s)
{
unsigned mask;
if (s != 2)
return 0;
if (n == 1) {
printf("\tld hl,0x00\n");
return 1;
}
if (n == 256) {
printf("\tld h,0x0\n");
return 1;
}
if (n & (n - 1))
return 0;
if (!optsize) {
mask = n - 1;
gen_logicc(NULL, s, "and", mask, 1);
return 1;
}
return 0;
}
/*
* If possible turn this node into a direct access. We've already checked
* that the right hand side is suitable. If this returns 0 it will instead
* fall back to doing it stack based.
*
* The 8080 is pretty basic so there isn't a lot we turn around here. As
* proof of concept we deal with the add case. Other processors may be
* able to handle a lot more.
*
* If your processor is good at subtracts you may also want to rewrite
* constant on the left subtracts in the rewrite rules into some kind of
* rsub operator.
*/
unsigned gen_direct(register struct node *n)
{
register unsigned s = get_size(n->type);
register struct node *r = n->right;
unsigned v;
unsigned nr = n->flags & NORETURN;
/* We only deal with simple cases for now */
if (r) {
if (!access_direct(n->right))
return 0;
v = r->value;
}
switch (n->op) {
case T_CLEANUP:
gen_cleanup(v);
return 1;
case T_NSTORE:
if (s > 2)
return 0;
if (s == 1)
printf("\tld a,l\n");
printf("\tld (_%s+%u),", namestr(n->snum), WORD(n->value));
return 1;
if (s == 1)
printf("a\n");
else
printf("hl\n");
/* TODO 4/8 for long etc */
return 0;
case T_LBSTORE:
if (s > 2)
return 0;
if (s == 1)
printf("\tld a,l\n");
printf("\tld (T%u+%u), ", n->val2, v);
if (s == 1)
printf("a\n");
else
printf("hl\n");
return 1;
case T_RSTORE:
load_regvar(n->value, s);
return 1;
case T_EQ:
/* The address is in HL at this point */
if (IS_EZ80 && s == 2) {
if (load_de_with(r) == 0)
return 0;
printf("\tld (hl),de\n");
if (!nr)
printf("\tex de,hl\n");
return 1;
}
if (s == 1) {
/* We need to end up with the value in l if this is not NORETURN, also
we can optimize constant a step more */
if (r->op == T_CONSTANT && nr)
printf("\tld (hl),0x%x\n", v & 0xFF);
else {
if (load_a_with(r) == 0)
return 0;
printf("\tld (hl),a\n");
if (!nr)
printf("\tld l,a\n");
}
return 1;
}
if (s == 2 && r->op == T_CONSTANT) {
if (nr) { /* usual case */
printf("\tld (hl),0x%x\n", v & 0xFF);
printf("\tinc hl\n");
printf("\tld (hl),0x%x\n", v >> 8);
} else {
printf("\tld de,0x%x\n", v);
printf("\tld (hl),e\n");
printf("\tinc hl\n");
printf("\tld (hl),d\n");
printf("\tex de,hl\n");
}
return 1;
}
if (s == 4 && r->op == T_CONSTANT) {
if (v < 0x02) {
printf("\tcall __assign%ul\n", v);
return 1;
}
if (v <= 0xFF) {
printf("\tld a,0x%x\n", v);
helper(n, "assign0la");
return 1;
}
if (v <= 0xFFFF) {
printf("\tld de,%u\n", v);
helper(n, "assignl0de");
return 1;
}
}
return 0;
case T_PLUS:
/* Zero should be eliminated in cc1 FIXME */
if (r->op == T_CONSTANT) {
if (v == 0)
return 1;
if (v < 4 && s <= 2) {
if (s == 1)
repeated_op("inc l", v);
else
repeated_op("inc hl", v);
return 1;
}
}
/* Faster case for foo + bc to avoid transfer to de */
if (r->op == T_RREF && v == 1) {
printf("\tadd hl,bc\n");
return 1;
}
if (s <= 2) {
/* LHS is in HL at the moment, end up with the result in HL */
if (s == 1) {
if (load_a_with(r) == 0)
return 0;
printf("\tld e,a\n");
}
if (s > 2 || load_de_with(r) == 0)
return 0;
printf("\tadd hl,de\n");
return 1;
}
return 0;
case T_MINUS:
if (r->op == T_CONSTANT) {
if (v == 0)
return 1;
if (v < 6 && s <= 2) {
if (s == 1)
repeated_op("dec l", v);
else
repeated_op("dec hl", v);
return 1;
}
printf("\tld de,0x%x\n", 65536 - v);
printf("\tadd hl,de\n");
return 1;
}
/* Avoid loading BC into DE unnecessarily. No shortcut for IXY though */
if (r->op == T_REG && v == 1) {
printf("\tor a\n\tsbc hl,bc\n");
return 1;
}
/* TODO: Can we ex de,hl, load into hl and go? - check direction too */
if (load_de_with(r) == 0)
return 0;
printf("\tor a\n\tsbc hl,de\n");
return 1;
case T_STAR:
if (r->op == T_CONSTANT) {
if (s <= 2 && can_fast_mul(s, v)) {
gen_fast_mul(s, v);
return 1;
}
}
return gen_deop("mulde", n, r, 0);
case T_SLASH:
if (r->op == T_CONSTANT) {
if (s <= 2 && gen_fast_div(s, v, n->type))
return 1;
}
return gen_deop("divde", n, r, 1);
case T_PERCENT:
if (r->op == T_CONSTANT && (n->type & UNSIGNED)) {
if (s <= 2 && gen_fast_remainder(s, v))
return 1;
}
return gen_deop("remde", n, r, 1);
case T_AND:
if (gen_logicc(r, s, "and", v, 1))
return 1;
if (r->op == T_CONSTANT && s <= 2) {
int b = bitcheck0(v, s);
if (b >= 0) {
/* Single clear bit */
if (b < 8)
printf("\tres %u,l\n", b);
else
printf("\tres %u,h\n", b - 8);
return 1;
}
}
return gen_deop("bandde", n, r, 0);
case T_OR:
if (r->op == T_CONSTANT && v == 0)
return 1;
if (r->op == T_CONSTANT && s <= 2) {
int b = bitcheck1(v, s);
if (b >= 0) {
/* Single set bit */
if (b < 8)
printf("\tset %u,l\n", b);
else
printf("\tset %u,h\n", b - 8);
return 1;
}
/* For small values it's more efficient to do this inline even
in -Os */
if (!(v & 0xFF00)) {
printf("\tld a,0x%x\n\tor l\n\tld l,a\n", v);
return 1;
}
if (!(v & 0x00FF)) {
printf("\tld a,0x%x\n\tor h\n\tld h,a\n", v >> 8);
return 1;
}
}
if (gen_logicc(r, s, "or", r->value, 2))
return 1;
return gen_deop("borde", n, r, 0);
case T_HAT:
/* For small values it's more efficient to do this inline even
in -Os */
if (r->op == T_CONSTANT && s <= 2) {
if (v == 0)
return 1;
if (!(v & 0xFF00)) {
printf("\tld a,0x%x\n\txor l\n\tld l,a\n", v);
return 1;
}
if (!(v & 0x00FF)) {
printf("\tld a,0x%x\n\txor h\n\tld h,a\n", v >> 8);
return 1;
}
}
if (gen_logicc(r, s, "xor", r->value, 3))
return 1;
return gen_deop("bxorde", n, r, 0);
/* TODO: add sbc hl,de etc versions of these when we can - or in optimizer ? */
case T_EQEQ:
return gen_compc("cmpeq", n, r, 0);
case T_GTEQ: