-
Notifications
You must be signed in to change notification settings - Fork 13
/
backend-8080.c
2237 lines (2117 loc) · 54.1 KB
/
backend-8080.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
/*
* Things to improve
* - Use the BC register either via a register hint in the compiler or by looking for
* a non pointer arg candidate (especially on 8080 where the loads are expensive)
* - Improve initialized variable generation in stack frame creation
* - Avoid the two xchg calls on a void function cleanup (see gen_cleanup)
* - Do we want a single "LBREF or NREF name print" fuinction
* - Optimize xor 0xff with cpl ?
* - Inline load and store of long to static/global/label (certainly for -O2)
* - See if we can think down support routines that use the retaddr patching (ideally
* remove them, if not fix Fuzix task switch to save/restore it)
*/
#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 LWDIRECT 24 /* Number of __ldword1 __ldword2 etc forms for fastest access */
/*
* 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 */
/*
* Output side logic. For now dumb but route everything here so
* we can do more useful stuff later
*/
#define OP_XCHG 1
#define OP_PUSH 2
#define OP_LXI 3
#define OP_DAD 4
#define OP_SPHL 5
#define OP_INX 6
#define OP_DCX 7
#define OP_RET 8
#define OP_POP 9
#define OP_MOV 10
#define OP_MVI 11
#define OP_LHLD 12
#define OP_SHLD 13
#define OP_LDA 14
#define OP_STA 15
#define OP_ARHL 16
#define OP_DSUB 17
#define OP_LDAX 18
#define OP_STAX 19
#define OP_XTHL 20
#define OP_LHLX 21
#define OP_SHLX 22
#define OP_LDSI 23
#define OP_LDHI 24
#define OP_CALL 25
#define OP_JUMP 26
#define OP_DATA 27
#define OP_COMMENT 28
#define OP_LABEL 29
#define OP_INC 30
#define OP_DEC 31
#define OP_ADD 32
#define OP_ANA 33
#define OP_ORA 34
#define R_A 1
#define R_PSW 1 /* Unless we start CC tracking */
#define R_B 2
#define R_C 4
#define R_BC (R_B|R_C)
#define R_D 8
#define R_E 16
#define R_DE (R_D|R_E)
#define R_H 32
#define R_L 64
#define R_HL (R_H|R_L)
#define R_M 128
#define R_SP 256
#define R_MEM 512
#define R_KEEP 1024 /* Not to be removed */
#define R_ALL (R_PSW|R_BC|R_DE|R_HL|R_SP|R_MEM)
static void opcode(unsigned code, unsigned rs, unsigned rd, const char *p, ...)
{
va_list v;
va_start(v, p);
if (code != OP_LABEL && code != OP_COMMENT)
putchar('\t');
vprintf(p, v);
putchar('\n');
va_end(v);
}
static void set_segment(unsigned seg)
{
/* Track segments for output */
}
/* Finish and write any data */
static void opcode_flush(void)
{
}
/*
* 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;
}
static unsigned get_stack_size(unsigned t)
{
unsigned n = get_size(t);
if (n == 1)
return 2;
return n;
}
#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 */
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. For 8080 we are quite limited especially
* with locals. In theory we could extend some things to 8bit
* locals on 8085 (ldsi, ldax d, mov e,a)
*/
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)
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;
}
/* Chance to rewrite the tree from the top rather than none by node
upwards. We will use this for 8bit ops at some point and for cconly
propagation */
struct node *gen_rewrite(struct node *n)
{
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;
}
}
return n;
}
/* Export the C symbol */
void gen_export(const char *name)
{
printf(" .export _%s\n", name);
}
void gen_segment(unsigned segment)
{
set_segment(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)
{
opcode(OP_LABEL, 0, R_KEEP, "_%s:\n", name);
unreachable = 0;
}
/* Generate the stack frame */
/* TODO: defer this to statements so we can ld/push initializers */
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)) {
opcode(OP_PUSH, R_BC|R_SP, R_SP, "push b");
argbase += 2;
}
if (size > 10) {
opcode(OP_LXI, 0, R_HL, "lxi h,%u", (-size) & 0xFFFF);
opcode(OP_DAD, R_SP|R_HL, R_HL, "dad sp");
opcode(OP_SPHL, R_HL, R_SP, "sphl");
return;
}
if (size & 1) {
opcode(OP_DCX, R_SP, R_SP, "dcx sp");
size--;
}
while(size) {
opcode(OP_PUSH, R_HL|R_SP, R_SP, "push h");
size -= 2;
}
}
void gen_epilogue(unsigned size, unsigned argsize)
{
unsigned x = func_flags & F_VOIDRET;
if (sp != 0)
error("sp");
if (unreachable)
return;
/* Return in HL, does need care on stack. TOOD: flag void functions
where we can burn the return */
sp -= size;
if (cpu == 8085 && size <= 255 && size > 4) {
opcode(OP_LDSI, R_SP, R_DE, "ldsi %u", size);
opcode(OP_XCHG, R_DE|R_HL, R_DE|R_HL, "xchg");
opcode(OP_SPHL, R_HL, R_SP, "sphl");
opcode(OP_XCHG, R_DE|R_HL, R_DE|R_HL, "xchg");
} else if (size > 10) {
if (!x)
opcode(OP_XCHG, R_DE|R_HL, R_DE|R_HL, "xchg");
opcode(OP_LXI, 0, R_HL, "lxi h,0x%x", (uint16_t)size);
opcode(OP_DAD, R_SP|R_HL, R_HL, "dad sp");
opcode(OP_SPHL, R_HL, R_SP, "sphl");
if (!x)
opcode(OP_XCHG, R_DE|R_HL, R_DE|R_HL, "xchg");
} else {
if (size & 1) {
opcode(OP_INX, R_SP, R_SP, "inx sp");
size--;
}
while (size) {
opcode(OP_POP, R_SP, R_SP|R_DE, "pop d");
size -= 2;
}
}
if (func_flags & F_REG(1))
opcode(OP_POP, R_SP, R_SP|R_BC, "pop b");
/* TODO: make this a little "ret" func as it has several users */
if (x)
opcode(OP_RET, 0, 0, "ret");
else
opcode(OP_RET, R_HL, 0, "ret");
}
void gen_label(const char *tail, unsigned n)
{
unreachable = 0;
/* A branch label means the state is unknown so force any
existing state and don't assume anything */
opcode(OP_LABEL, R_ALL, R_ALL, "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 {
/* FIXME: R_HL depends on func != void */
opcode(OP_RET, R_HL, 0, "ret");
unreachable = 1;
return 1;
}
}
void gen_jump(const char *tail, unsigned n)
{
/* Force anything deferred to complete before the jump */
opcode(OP_JUMP, R_ALL, 0, "jmp L%u%s", n, tail);
unreachable = 1;
}
void gen_jfalse(const char *tail, unsigned n)
{
opcode(OP_JUMP, R_ALL, 0, "jz L%u%s", n, tail);
}
void gen_jtrue(const char *tail, unsigned n)
{
opcode(OP_JUMP, R_ALL, 0, "jnz L%u%s", n, tail);
}
static void gen_cleanup(unsigned v)
{
/* CLEANUP is special and needs to be handled directly */
sp -= v;
if (v > 10) {
/* This is more expensive, but we don't often pass that many
arguments so it seems a win to stay in HL */
unsigned x = func_flags & F_VOIDRET;
if (!x)
opcode(OP_XCHG, R_DE|R_HL, R_DE|R_HL, "xchg");
opcode(OP_LXI, 0, R_HL, "lxi h,%u", v);
opcode(OP_DAD, R_SP|R_HL, R_HL, "dad sp");
opcode(OP_SPHL, R_HL, R_SP, "sphl");
if (!x)
opcode(OP_XCHG, R_DE|R_HL, R_DE|R_HL, "xchg");
} else {
while(v >= 2) {
opcode(OP_POP, R_SP, R_DE|R_SP, "pop d");
v -= 2;
}
if (v)
opcode(OP_INX, R_SP, R_SP, "inx sp");
}
}
/*
* 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 */
if (n->flags & ISBOOL)
printf("\txra a\n\tcmp l\n");
}
}
void gen_switch(unsigned n, unsigned type)
{
opcode(OP_LXI, 0, R_DE, "lxi d,Sw%u", n);
/* Nothing is preserved over a switch */
printf("\tjmp __switch");
helper_type(type, 0);
putchar('\n');
}
void gen_switchdata(unsigned n, unsigned size)
{
opcode(OP_LABEL, 0, 0, "Sw%u:", n);
opcode(OP_DATA, 0, 0, ".word %u", size);
}
void gen_case_label(unsigned tag, unsigned entry)
{
unreachable = 0;
opcode(OP_LABEL, 0, 0, "Sw%u_%u:", tag, entry);
}
void gen_case_data(unsigned tag, unsigned entry)
{
opcode(OP_LABEL, 0, 0, ".word Sw%u_%u", tag, entry);
}
void gen_data_label(const char *name, unsigned align)
{
opcode(OP_LABEL, 0, 0, "_%s:", name);
}
void gen_space(unsigned value)
{
opcode(OP_DATA, 0, 0, ".ds %u", value);
}
void gen_text_data(struct node *n)
{
opcode(OP_DATA, 0, 0, ".word T%u", n->val2);
}
/* The label for a literal (currently only strings) */
void gen_literal(unsigned n)
{
if (n)
opcode(OP_LABEL, 0, 0, "T%u:", n);
}
void gen_name(struct node *n)
{
opcode(OP_DATA, 0, 0, ".word _%s+%u", namestr(n->snum), WORD(n->value));
}
void gen_value(unsigned type, unsigned long value)
{
unsigned w = WORD(value);
if (PTR(type)) {
opcode(OP_DATA, 0, 0, ".word %u", w);
return;
}
switch (type) {
case CCHAR:
case UCHAR:
opcode(OP_DATA, 0, 0, ".byte %u", BYTE(w));
break;
case CSHORT:
case USHORT:
opcode(OP_DATA, 0, 0, ".word %u", w);
break;
case CLONG:
case ULONG:
case FLOAT:
/* We are little endian */
opcode(OP_DATA, 0, 0, ".word %u\n", w);
opcode(OP_DATA, 0, 0, ".word %u\n", (unsigned) ((value >> 16) & 0xFFFF));
break;
default:
error("unsuported type");
}
}
void gen_start(void)
{
printf("\t.setcpu %u\n", cpu);
}
void gen_end(void)
{
opcode_flush();
}
void gen_tree(struct node *n)
{
codegen_lr(n);
opcode(OP_COMMENT, 0, 0, ";");
/* printf(";SP=%d\n", sp); */
}
/*
* Get a local variable into HL or DE.
*
* 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)
{
const char *name;
/* Trivial case: if the variable is top of stack then just pop and
push it back */
if (v == 0 && size == 2) {
if (to_de) {
opcode(OP_POP, R_SP, R_SP|R_DE, "pop d");
opcode(OP_PUSH, R_SP|R_DE, R_SP, "push d");
} else {
opcode(OP_POP, R_SP, R_SP|R_HL, "pop h");
opcode(OP_PUSH, R_SP|R_HL, R_SP, "push h");
}
return 1;
}
/* The 8085 has LDSI and LHLX so we can fast access anything up to
255 bytes from SP. However we end up trashing DE in doing so. For
now don't use this for DE loads, look at saving stuff later TODO */
if (cpu == 8085 && v <= 255 && !to_de) {
opcode(OP_LDSI, R_SP, R_DE, "ldsi %u", v);
if (size == 2)
opcode(OP_LHLX, R_DE|R_M, R_HL, "lhlx");
else {
opcode(OP_LDAX, R_DE|R_M, R_A, "ldax d");
opcode(OP_MOV, R_A, R_L, "mov l,a");
}
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) {
opcode(OP_POP, R_SP, R_DE|R_SP, "pop d");
opcode(OP_POP, R_SP, R_HL|R_SP, "pop h");
opcode(OP_PUSH, R_SP, R_DE|R_SP, "push h");
opcode(OP_PUSH, R_SP, R_HL|R_SP, "push d");
return 1;
}
/* Byte load is shorter inline for most cases */
if (size == 1 && (!optsize || v >= LWDIRECT || to_de)) {
if (to_de)
opcode(OP_XCHG, R_DE|R_HL, R_DE|R_HL, "xchg");
opcode(OP_LXI, 0, R_HL, "lxi h,%u", v);
opcode(OP_DAD, R_SP|R_HL, R_HL, "dad sp");
opcode(OP_MOV, R_HL|R_M, R_L, "mov l,m");
if (to_de)
opcode(OP_XCHG, R_DE|R_HL, R_DE|R_HL, "xchg");
return 1;
}
/* Longer reach for 8085 is via addition games, but only for HL
as lhlx requires we use DE and HL */
if (cpu == 8085 && size == 2 && !to_de) {
opcode(OP_LXI, 0, R_HL, "lxi h,%u", WORD(v));
opcode(OP_DAD, R_SP|R_HL, R_HL, "dad sp");
opcode(OP_XCHG, R_DE|R_HL, R_DE|R_HL, "xchg");
opcode(OP_LHLX, R_DE|R_M, R_HL, "lhlx");
return 1;
}
/* Word load is long winded on 8080 but if the user asked for it */
/* This also gets used for 8085 as a fallback for the DE case */
if (size == 2 && opt > 2) {
if (to_de)
opcode(OP_XCHG, R_DE|R_HL, R_DE|R_HL, "xchg");
opcode(OP_DAD, R_SP|R_HL, R_HL, "dad sp");
opcode(OP_MOV, R_M|R_HL, R_A, "mov a,m");
opcode(OP_INX, R_HL, R_HL, "inx h");
opcode(OP_MOV, R_M|R_HL, R_L, "mov h,m");
opcode(OP_MOV, R_A, R_L, " mov l,a");
if (to_de)
opcode(OP_XCHG, R_DE|R_HL, R_DE|R_HL, "xchg");
return 1;
}
/* The 8080 has a bunch of helpers as we just xchg around them for
the DE case. The 8085 has only a word helper and for both the
word helper has it use HL and DE. Those cases go via stack */
if (to_de && (cpu == 8085 || v >= 253))
return 0; /* Go via stack */
/* Via helper magic for compactness on 8080 */
if (size == 1)
name = "ldbyte";
else if (size == 2)
name = "ldword";
else
return 0; /* Can't happen currently but trap it */
/* We do a call so the stack offset is two bigger */
if (to_de)
opcode(OP_XCHG, R_DE|R_HL, R_DE|R_HL, "xchg");
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)
opcode(OP_XCHG, R_DE|R_HL, R_DE|R_HL, "xchg");
return 1;
}
/*
* 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;
/* The 8080 we can reliably access stuff within 253 bytes of the
current stack pointer. 8085 we don't have the short helpers as they
are not worth it for this case, but we can do it via the 4 byte one */
if (op == T_LREF && n->value + sp < 253)
return 1;
/* We can direct access integer or smaller types that are constants
global/static or string labels */
/* TODO group the user ones together for a range check ? */
if (op != T_CONSTANT && op != T_NAME && op != T_LABEL &&
op != T_NREF && op != T_LBREF && op != T_RREF)
return 0;
if (!PTR(n->type) && (n->type & ~UNSIGNED) > CSHORT)
return 0;
return 1;
}
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;
}
/*
* 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 mask)
{
unsigned v = WORD(n->value);
const char *name;
switch(n->op) {
case T_NAME:
opcode(OP_LXI, 0, mask, "lxi %c,_%s+%u", r, namestr(n->snum), v);
return 1;
case T_LABEL:
opcode(OP_LXI, 0, mask, "lxi %c,T%u+%u", r, n->val2, v);
return 1;
case T_CONSTANT:
/* We know this is not a long from the checks above */
opcode(OP_LXI, 0, mask, "lxi %c,%u", r, v);
return 1;
case T_NREF:
name = namestr(n->snum);
if (r == 'b')
return 0;
else if (r == 'h') {
opcode(OP_LHLD, R_MEM, R_HL, "lhld _%s+%u", name, v);
return 1;
} else if (r == 'd') {
/* We know it is int or pointer */
opcode(OP_XCHG, R_DE|R_HL, R_DE|R_HL, "xchg");
opcode(OP_LHLD, R_MEM, R_HL, "lhld _%s+%u\n", name, v);
opcode(OP_XCHG, R_DE|R_HL, R_DE|R_HL, "xchg");
return 1;
}
break;
/* TODO: fold together cleanly with NREF */
case T_LBREF:
if (r == 'b')
return 0;
else if (r == 'h') {
opcode(OP_LHLD, R_MEM, R_HL, "lhld T%u+%u", n->val2, v);
return 1;
} else if (r == 'd') {
/* We know it is int or pointer */
opcode(OP_XCHG, R_DE|R_HL, R_DE|R_HL, "xchg");
opcode(OP_LHLD, R_MEM, R_HL, "lhld T%u+%u", n->val2, v);
opcode(OP_XCHG, R_DE|R_HL, R_DE|R_HL, "xchg");
return 1;
}
break;
case T_RREF:
if (r == 'd') {
opcode(OP_MOV, R_B, R_D, "mov d,b");
opcode(OP_MOV, R_C, R_E, "mov e,c\n");
} else if (r == 'h') {
opcode(OP_MOV, R_B, R_H, "mov h,b");
opcode(OP_MOV, R_C, R_L, "mov l,c\n");
}
/* Assumes that BC isn't corrupted yet so is already the right value. Use
this quirk with care (eg T_MINUS) */
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('b', n, R_BC);
}
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('d', n, R_DE);
}
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('h', n, R_HL);
}
static unsigned load_a_with(struct node *n)
{
switch(n->op) {
case T_CONSTANT:
/* We know this is not a long from the checks above */
opcode(OP_MVI, 0, R_A, "mvi a,%u", BYTE(n->value));
break;
case T_NREF:
opcode(OP_LDA, R_MEM, R_A, "lda _%s+%u", namestr(n->snum), WORD(n->value));
break;
case T_LBREF:
opcode(OP_LDA, R_MEM, R_A, "lda T%u+%u", n->val2, WORD(n->value));
break;
case T_RREF:
opcode(OP_MOV, R_C, R_A, "mov a,c");
break;
case T_LREF:
/* We don't want to trash HL as we may be doing an HL:A op */
opcode(OP_XCHG, R_DE|R_HL, R_DE|R_HL, "xchg");
opcode(OP_LXI, 0, R_HL, "lxi h,%u", WORD(n->value));
opcode(OP_DAD, R_HL|R_SP, R_HL, "dad sp");
opcode(OP_MOV, R_HL|R_M, R_A, "mov a,m");
opcode(OP_XCHG, R_DE|R_HL, R_DE|R_HL, "xchg");
break;
default:
return 0;
}
return 1;
}
/* TODO: needs a format change */
static void repeated_op(const char *o, unsigned n)
{
while(n--)
printf("\t%s\n", o);
}
static void loadhl(struct node *n, unsigned s)
{
if (n && (n->flags & NORETURN))
return;
opcode(OP_MOV, R_C, R_L, "mov l,c");
if (s == 2)
opcode(OP_MOV, R_B, R_H, "mov h,b");
}
static void loadbc(unsigned s)
{
opcode(OP_MOV, R_L, R_C, "mov c,l");
if (s == 2)
opcode(OP_MOV, R_H, R_B, "mov b,h");
}
/* 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, struct node *n, struct node *r, unsigned sign)
{
unsigned s = get_size(n->type);
if (s > 2)
return 0;
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;
}
/* TODO: someone needs to own eliminating no side effect impossible
or true expressions like unsigned < 0 */
static unsigned gen_compc(const char *op, struct node *n, struct node *r, unsigned sign)
{
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;
return 1;
}
if (gen_deop(op, n, r, sign)) {
n->flags |= ISBOOL;
return 1;
}
return 0;
}
static int count_mul_cost(unsigned n)
{
int cost = 0;
if ((n & 0xFF) == 0) {
n >>= 8;
cost += 3; /* mov mvi */
}
while(n > 1) {
if (n & 1)
cost += 3; /* push pop dad d */
n >>= 1;
cost++; /* dad h */
}
return cost;
}