-
Notifications
You must be signed in to change notification settings - Fork 15
/
backend-gb.c
2489 lines (2357 loc) · 51.3 KB
/
backend-gb.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
/*
* The gameboy is an 8080 missing a few bits (some useful) and with
* Z80isms and some random other stuff thrown in.
*
* TODO: Track HL pointing versus SP to reduce LDHL usage and use inc/dec
* TODO: Can we do better logic for stuff we move into DE so we load into DE
* in the first place ?
* TODO: import volatile handling into this, Z80 and 8080
*/
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include "compiler.h"
#include "backend.h"
#include "backend-byte.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 */
/* Check if a single bit is set or clear */
int bitcheckb1(uint8_t n)
{
unsigned m = 1;
unsigned i;
for (i = 0; i < 8; i++) {
if (n == m)
return i;
m <<= 1;
}
return -1;
}
int bitcheck1(unsigned n, unsigned s)
{
register unsigned i;
unsigned m = 1;
if (s == 1)
return bitcheckb1(n);
for (i = 0; i < 16; i++) {
if (n == m)
return i;
m <<= 1;
}
return -1;
}
int bitcheck0(unsigned n, unsigned s)
{
if (s == 1)
return bitcheckb1((~n) & 0xFF);
return bitcheck1((~n) & 0xFFFF, 2);
}
/*
* 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 func_cleanup; /* Zero if we can just ret out */
static unsigned label; /* Used to hand out local labels in the form X%u */
static unsigned ccvalid; /* State of condition codes */
#define CC_UNDEF 0 /* Who knows */
#define CC_VALID 1 /* Matches (Z/NZ) */
#define CC_INVERSE 2 /* Matches the inverse (Z NZ) */
/* Set CC correctly */
static void outputcc(const char *p, ...)
{
va_list v;
if (strchr(p, ':') == NULL)
putchar('\t');
va_start(v, p);
vprintf(p, v);
putchar('\n');
va_end(v);
ccvalid = CC_VALID;
}
/* CC other */
static void output(const char *p, ...)
{
va_list v;
if (strchr(p, ':') == NULL)
putchar('\t');
va_start(v, p);
vprintf(p, v);
putchar('\n');
va_end(v);
ccvalid = CC_UNDEF;
}
/* CC no effect */
static void outputne(const char *p, ...)
{
va_list v;
if (strchr(p, ':') == NULL)
putchar('\t');
va_start(v, p);
vprintf(p, v);
putchar('\n');
va_end(v);
}
/* CC inverted but valid */
static void outputinv(const char *p, ...)
{
va_list v;
if (strchr(p, ':') == NULL)
putchar('\t');
va_start(v, p);
vprintf(p, v);
putchar('\n');
va_end(v);
ccvalid = CC_INVERSE;
}
/*
* Object sizes
*/
static unsigned get_size(unsigned t)
{
if (PTR(t))
return 2;
if (t == CSHORT || t == USHORT)
return 2;
if (t == CCHAR || t == UCHAR)
return 1;
if (t == CLONG || t == ULONG || t == FLOAT)
return 4;
if (t == CLONGLONG || t == ULONGLONG || t == DOUBLE)
return 8;
if (t == VOID)
return 0;
fprintf(stderr, "type %x\n", t);
error("gs");
return 0;
}
#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_BTST (T_USER+11) /* single bit testing */
static void squash_node(struct node *n, struct node *o)
{
n->value = o->value;
n->val2 = o->val2;
n->snum = o->snum;
free_node(o);
}
static void squash_left(struct node *n, unsigned op)
{
struct node *l = n->left;
n->op = op;
squash_node(n, l);
n->left = NULL;
}
static void squash_right(struct node *n, unsigned op)
{
struct node *r = n->right;
n->op = op;
squash_node(n, r);
n->right = NULL;
}
/*
* Heuristic for guessing what to put on the right. This is very
* processor dependent.
*/
static unsigned is_simple(struct node *n)
{
unsigned op = n->op;
/* Multi-word objects are never simple */
if (!PTR(n->type) && (n->type & ~UNSIGNED) > CSHORT)
return 0;
/* We can load these directly into a register */
if (op == T_CONSTANT || op == T_LABEL || op == T_NAME || op == T_RREF)
return 10;
/* We can load this directly into a register but may need xchg pairs */
if (op == T_NREF || op == T_LBREF)
return 1;
return 0;
}
/*
* Turn it 8bit
*/
struct node *gen_rewrite(struct node *n)
{
byte_label_tree(n, BTF_RELABEL);
return n;
}
/*
* Our chance to do tree rewriting. We don't do much for the 8080
* at this point, but we do rewrite name references and function calls
* to make them easier to process.
*/
struct node *gen_rewrite_node(struct node *n)
{
struct node *l = n->left;
struct node *r = n->right;
unsigned op = n->op;
unsigned nt = n->type;
/* TODO
- rewrite some reg ops
*/
/* *regptr. Only works for bytes, so if the size is wrong (e.g. the pointer
was cast) then force it into HL and the usual path */
if (op == T_DEREF && r->op == T_RREF && (nt == CCHAR || nt == UCHAR)) {
n->op = T_RDEREF;
n->right = NULL;
n->val2 = 0;
n->value = r->value;
free_node(r);
return n;
}
/* *regptr = */
if (op == T_EQ && l->op == T_RREF && (nt == CCHAR || nt == UCHAR)) {
n->op = T_REQ;
n->val2 = 0;
n->value = l->value;
n->left = NULL;
free_node(l);
return n;
}
/* Rewrite references into a load operation */
if (nt == CCHAR || nt == UCHAR || nt == CSHORT || nt == USHORT || PTR(nt)) {
if (op == T_DEREF) {
if (r->op == T_LOCAL || r->op == T_ARGUMENT) {
if (r->op == T_ARGUMENT)
r->value += argbase + frame_len;
squash_right(n, T_LREF);
return n;
}
if (r->op == T_REG) {
squash_right(n, T_RREF);
return n;
}
if (r->op == T_NAME) {
squash_right(n, T_NREF);
return n;
}
if (r->op == T_LABEL) {
squash_right(n, T_LBREF);
return n;
}
}
if (op == T_EQ) {
if (l->op == T_NAME) {
squash_left(n, T_NSTORE);
return n;
}
if (l->op == T_LABEL) {
squash_left(n, T_LBSTORE);
return n;
}
if (l->op == T_LOCAL || l->op == T_ARGUMENT) {
if (l->op == T_ARGUMENT)
l->value += argbase + frame_len;
squash_left(n, T_LSTORE);
return n;
}
if (l->op == T_REG) {
squash_left(n, T_RSTORE);
return n;
}
}
}
/* Eliminate casts for sign, pointer conversion or same */
if (op == T_CAST) {
if (nt == r->type || (nt ^ r->type) == UNSIGNED ||
(PTR(nt) && PTR(r->type))) {
free_node(n);
return r;
}
}
/* Rewrite function call of a name into a new node so we can
turn it easily into call xyz */
if (op == T_FUNCCALL && r->op == T_NAME && PTR(r->type) == 1) {
n->op = T_CALLNAME;
n->snum = r->snum;
n->value = r->value;
free_node(r);
n->right = NULL;
}
/* Commutive operations. We can swap the sides over on these */
if (op == T_AND || op == T_OR || op == T_HAT || op == T_STAR || op == T_PLUS) {
/* printf(";left %d right %d\n", is_simple(n->left), is_simple(n->right)); */
if (is_simple(n->left) > is_simple(n->right)) {
n->right = l;
n->left = r;
}
}
/* Turn ++ and -- into easier forms when possible */
if (op == T_PLUSPLUS && (n->flags & NORETURN))
n->op = T_PLUSEQ;
if (op == T_MINUSMINUS && (n->flags & NORETURN))
n->op = T_MINUSEQ;
return n;
}
/* Export the C symbol */
void gen_export(const char *name)
{
printf(" .export _%s\n", name);
}
void gen_segment(unsigned segment)
{
switch(segment) {
case A_CODE:
printf("\t.%s\n", codeseg);
break;
case A_DATA:
printf("\t.data\n");
break;
case A_BSS:
printf("\t.bss\n");
break;
case A_LITERAL:
printf("\t.literal\n");
break;
default:
error("gseg");
}
}
/* Generate the function prologue - may want to defer this until
gen_frame for the most part */
void gen_prologue(const char *name)
{
output("_%s:", name);
unreachable = 0;
}
/* Generate the stack frame */
void gen_frame(unsigned size, unsigned aframe)
{
frame_len = size;
sp = 0;
if (size || func_flags & F_REG(1))
func_cleanup = 1;
else
func_cleanup = 0;
argbase = ARGBASE;
if (func_flags & F_REG(1)) {
outputne("push bc");
argbase += 2;
}
while(size >= 128) {
outputne("add sp,#-128");
size -= 128;
}
if (size)
outputne("add sp,-%u", size);
}
void gen_epilogue(unsigned size, unsigned argsize)
{
if (sp != 0)
error("sp");
if (unreachable)
return;
while(size >= 127) {
outputne("add sp,#127");
size -= 127;
}
if (size)
outputne("add sp,%u", size);
outputne("ret");
}
void gen_label(const char *tail, unsigned n)
{
unreachable = 0;
output("L%u%s:", n, tail);
}
/* A return statement. We can sometimes shortcut this if we have
no cleanup to do */
unsigned gen_exit(const char *tail, unsigned n)
{
if (unreachable)
return 1;
if (func_cleanup) {
gen_jump(tail, n);
unreachable = 1;
return 0;
} else {
outputne("ret");
unreachable = 1;
return 1;
}
}
void gen_jump(const char *tail, unsigned n)
{
/* Force anything deferred to complete before the jump */
outputne("jr L%u%s", n, tail);
unreachable = 1;
}
void gen_jfalse(const char *tail, unsigned n)
{
switch(ccvalid) {
case CC_VALID:
outputne("jr nz, L%u%s", n, tail);
break;
case CC_INVERSE:
outputne("jr z, L%u%s", n, tail);
break;
default:
error("jfu");
}
}
void gen_jtrue(const char *tail, unsigned n)
{
switch(ccvalid) {
case CC_VALID:
outputne("jr z, L%u%s", n, tail);
break;
case CC_INVERSE:
outputne("jr nz, L%u%s", n, tail);
break;
default:
error("jtu");
}
}
static void gen_cleanup(unsigned v)
{
/* CLEANUP is special and needs to be handled directly */
sp -= v;
while(v >= 127) {
outputne("add sp,%u", v);
v -= 127;
}
if (v)
outputne("add sp,%u", v);
}
/*
* Helper handlers. We use a tight format for integers but C
* style for float as we'll have C coded float support if any
*/
/* True if the helper is to be called C style */
static unsigned c_style(struct node *np)
{
register struct node *n = np;
/* Assignment is done asm style */
if (n->op == T_EQ)
return 0;
/* Float ops otherwise are C style */
if (n->type == FLOAT)
return 1;
n = n->right;
if (n && n->type == FLOAT)
return 1;
return 0;
}
void gen_helpcall(struct node *n)
{
/* Check both N and right because we handle casts to/from float in
C call format */
if (c_style(n))
gen_push(n->right);
printf("\tcall __");
}
void gen_helptail(struct node *n)
{
}
void gen_helpclean(struct node *n)
{
unsigned s;
if (c_style(n)) {
s = 0;
if (n->left) {
s += get_size(n->left->type);
/* gen_node already accounted for removing this thinking
the helper did the work, adjust it back as we didn't */
sp += s;
}
s += get_size(n->right->type);
gen_cleanup(s);
/* C style ops that are ISBOOL didn't set the bool flags */
/* Need to think about keeping bool stuff 8bit here */
if (n->flags & ISBOOL)
printf("\txor a\n\tcp l\n");
}
if (n->flags & ISBOOL)
ccvalid = CC_VALID;
}
void gen_switch(unsigned n, unsigned type)
{
outputne("ld de,Sw%u", n);
printf("\tjp __switch");
helper_type(type, 0);
putchar('\n');
}
void gen_switchdata(unsigned n, unsigned size)
{
outputne("Sw%u:", n);
outputne(".word %u", size);
}
void gen_case_label(unsigned tag, unsigned entry)
{
unreachable = 0;
output("Sw%u_%u:", tag, entry);
}
void gen_case_data(unsigned tag, unsigned entry)
{
outputne(".word Sw%u_%u", tag, entry);
}
void gen_data_label(const char *name, unsigned align)
{
outputne("_%s:", name);
}
void gen_space(unsigned value)
{
outputne(".ds %u", value);
}
void gen_text_data(struct node *n)
{
outputne(".word T%u", n->val2);
}
/* The label for a literal (currently only strings) */
void gen_literal(unsigned n)
{
if (n)
outputne("T%u:", n);
}
void gen_name(struct node *n)
{
outputne(".word _%s+%u", namestr(n->snum), WORD(n->value));
}
void gen_value(unsigned type, unsigned long value)
{
unsigned w = WORD(value);
if (PTR(type)) {
outputne(".word %u", w);
return;
}
switch (type) {
case CCHAR:
case UCHAR:
outputne(".byte %u", BYTE(w));
break;
case CSHORT:
case USHORT:
outputne(".word %u", w);
break;
case CLONG:
case ULONG:
case FLOAT:
/* We are little endian */
outputne(".word %u\n", w);
outputne(".word %u\n", (unsigned) ((value >> 16) & 0xFFFF));
break;
default:
error("unsuported type");
}
}
void gen_start(void)
{
}
void gen_end(void)
{
}
void gen_tree(struct node *n)
{
codegen_lr(n);
outputne(";:");
/* printf(";SP=%d\n", sp); */
}
/* Make HL = SP + offset */
static void hl_from_sp(unsigned off)
{
if (off < 128)
outputne("ldhl sp,%u", off);
else {
outputne("ld hl,%u", off);
output("add hl,sp");
}
}
/*
* Get a local variable into HL or DE. DE can only be used for size 2
*
* Loading into HL may not fail (return 0 is a compiler abort) but may
* trash DE as well. Loading into DE may fail and is not permitted
* to trash HL.
*/
unsigned gen_lref(unsigned v, unsigned size, unsigned to_de)
{
/* Trivial case: if the variable is top of stack then just pop and
push it back */
if (v == 0 && size == 2) {
if (to_de) {
output("pop de");
output("push de");
} else {
output("pop hl");
output("push hl");
}
return 1;
}
/*
* We can get at the second variable fastest by popping two
* things but must destroy DE so can only use this for HL
*/
if (!to_de && v == 2 && size == 2) {
output("pop de");
output("pop hl");
output("push hl");
output("push de");
return 1;
}
/*
* Shortest forms use ldhl sp,#n for range up to 127
*/
if (size <= 2) {
if (to_de && size > 1)
outputne("push hl");
hl_from_sp(v);
if (size == 2) {
output("ldi a,(hl)");
output("ld h,(hl)");
output("ld l,a");
} else {
if (to_de)
output("ld l,(hl)");
else
output("ld a,(hl)");
}
if (to_de && size > 1)
outputne("pop de");
return 1;
}
return 0; /* Can't happen currently but trap it */
}
/*
* Try and generate shorter code for stuff we can directly access
*/
/*
* 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.
*/
static unsigned access_direct(struct node *n)
{
unsigned op = n->op;
/* We can direct access integer or smaller types that are constants
global/static or string labels */
if (op != T_CONSTANT && op != T_NAME && op != T_LABEL &&
op != T_NREF && op != T_LBREF && op != T_RREF && op != T_LREF)
return 0;
if (!PTR(n->type) && (n->type & ~UNSIGNED) > CSHORT)
return 0;
return 1;
}
#if 0
static unsigned access_direct_b(struct node *n)
{
/* We can't access as much via B directly because we've got no easy xchg with b */
/* TODO: if we want BC we need to know if BC currently holds the reg var */
if (n->op != T_CONSTANT && n->op != T_NAME && n->op != T_LABEL && n->op != T_REG)
return 0;
if (!PTR(n->type) && (n->type & ~UNSIGNED) > CSHORT)
return 0;
return 1;
}
#endif
static unsigned can_point_hl_at(struct node *n)
{
switch(n->op) {
case T_NREF:
case T_NSTORE:
case T_NAME:
case T_LBREF:
case T_LBSTORE:
case T_LABEL:
case T_ARGUMENT:
case T_LOCAL:
case T_LREF:
case T_LSTORE:
return 1;
}
return 0;
}
static unsigned point_hl_at(struct node *n)
{
unsigned v = n->value;
switch(n->op) {
case T_NREF:
case T_NSTORE:
case T_NAME:
output("ld hl,_%s+%u", namestr(n->snum), v);
break;
case T_LBREF:
case T_LBSTORE:
case T_LABEL:
output("ld hl,T%u+%u", n->val2, v);
break;
case T_ARGUMENT:
v += frame_len + argbase;
case T_LOCAL:
case T_LREF:
case T_LSTORE:
v += sp;
hl_from_sp(v);
break;
default:
return 0;
}
return 1;
}
static void load_via_hl(unsigned r, unsigned s)
{
if (s == 1) {
if (r == 'h' || r == 'a')
output("ld a,(hl)");
else if (r == 'd')
output("ld e,(hl)");
else error("lhlr1");
} else if (s == 2) {
if (r == 'h') {
output("ldi a,(hl)");
output("ld h,(hl)");
output("ld l,a");
} else if (r == 'd') {
output("ld e,(hl)");
output("inc hl");
output("ld d,(hl)");
} else error("lhlr2");
} else
error("lhlrs");
}
static void store_via_hl(unsigned r, unsigned s)
{
if (s == 1) {
if (r == 'h' || r == 'a')
outputne("ld (hl),a");
else if (r == 'd')
outputne("(hl),e");
else error("shlr1");
} else if (s == 2) {
if (r == 'd') {
outputne("ld (hl),d");
outputne("inc hl");
outputne("ld (hl),d");
} else error("shlr2");
} else
error("shlrs");
}
/*
* 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(const char *r, struct node *n)
{
unsigned v = WORD(n->value);
const char *name;
switch(n->op) {
case T_NAME:
output("ld %s,_%s+%u", r, namestr(n->snum), v);
return 1;
case T_LABEL:
output("ld %s,T%u+%u", r, n->val2, v);
return 1;
case T_CONSTANT:
/* We know this is not a long from the checks above */
output("ld %s,%u", r, v);
return 1;
case T_NREF:
if (*r == 'b')
return 0;
/* no ex de,hl so for now no name into DE */
if (*r == 'd')
return 0;
point_hl_at(n);
load_via_hl(*r, 2);
return 1;
/* TODO: fold together cleanly with NREF */
case T_LBREF:
if (*r == 'b')
return 0;
if (*r == 'd')
return 0;
point_hl_at(n);
load_via_hl(*r, 2);
return 1;
case T_RREF:
if (*r == 'd') {
outputne("ld d,b");
outputne("ld e,c");
} else if (*r == 'h') {
output("ld h,b");
output("ld l,c");
}
/* Assumes that BC isn't corrupted yet so is already the right value. Use
this quirk with care */
return 1;
default:
return 0;
}
return 1;
}
static unsigned load_bc_with(struct node *n)
{
/* No lref direct to BC option for now */
return load_r_with("bc", n);
}
static unsigned load_de_with(struct node *n)
{
if (n->op == T_LREF)
return gen_lref(n->value + sp, 2, 1);
return load_r_with("de", n);
}
#if 0
static unsigned load_hl_with(struct node *n)
{
if (n->op == T_LREF)
return gen_lref(n->value + sp, 2, 0);
return load_r_with("hl", n);
}
#endif
/* TODO: needs a 'save HL' indicator */
static unsigned load_a_with(struct node *n)
{
unsigned v = WORD(n->value);
switch(n->op) {
case T_CONSTANT:
/* We know this is not a long from the checks above */
output("ld a,%u", BYTE(v));
break;
case T_NREF:
output("ld a,(_%s+%u)", namestr(n->snum), v);
break;
case T_LBREF:
output("ld a,(T%u+%u)", n->val2, v);
break;
case T_RREF:
output("ld a,c");
break;
case T_LREF:
/* We don't want to trash HL as we may be doing an HL:A op */
outputne("push hl");
hl_from_sp(v);
output("ld a,(hl)");
outputne("pop hl");
break;
default:
return 0;
}
return 1;
}
static void repeated_op(const char *o, unsigned n)
{
while(n--)
output(o);
}
static void loadhl(struct node *n, unsigned s)
{
if (n && (n->flags & NORETURN))
return;
if (s == 1)
output("ld a,c");
else if (s == 2) {
output("ld l,c");
output("ld h,b");
} else
error("ldhl");
}
static void loadbc(unsigned s)
{
if (s == 1)
outputne("ld c,a");
else if (s == 2) {
outputne("ld c,l");
outputne("ld b,h");
} else
error("ldbc");
}
/*
* We split the direct two operand stuff into two forms
* - Stuff with HL or A and constants (in E or DE)
* - Anything else with HL or A and (HL)
*/
static unsigned gen_twoop(const char *op, struct node *n, struct node *r, unsigned sign, unsigned s)
{
char opc[16];
if (s > 2)
return 0;
if (r->op == T_CONSTANT) {
sprintf(opc, "%scon", op);
op = opc;
}
if (s == 2) {
if (r->op == T_CONSTANT)
output("ld de,%u", WORD(r->value));
else if (can_point_hl_at(r)) {
output("ld d,h");
output("ld e,l");
if (point_hl_at(r) == 0)
error("cpha");
} else
return 0;
} else if (s == 1) {
if (r->op == T_CONSTANT)
output("ld e,%u", WORD(r->value));
else if (point_hl_at(r) == 0)
return 0;
/* For now byte ops are done A,(HL) which works nicely */
}
if (sign)
helper_s(n, op);
else
helper(n, op);
return 1;
}
static unsigned gen_compc(const char *op, struct node *n, struct node *r, unsigned sign)
{
/* Comparison sizing comes from the children */
unsigned s = get_size(r->type);
if (r->op == T_CONSTANT && 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;