-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathbackend-ddp.c
1912 lines (1791 loc) · 39.7 KB
/
backend-ddp.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 DDP316/516
*
* Registers
* A 16bit accumulator
* B Can be swapped back and forth with A
* We use it for the upper half of a long and scratch
* X Index. Can be loaded/saved but no maths ops
* @SP Page 0 variable holding the stack pointer
* @FP Frame pointer (not useful except in call return)
*
* The machine has no true subtraction. The sub operator is actually
* internally effectively an add of the 2s complement of the contents
* of the EA. Flags thus act accordingly.
*
* The DDP116 has no STX/LDX functions only memory 0. That means that like
* the PDP9/15 you can't directly load and store X. We need to generate
* slightly different code for this and the helpers will need to vary
*
* Various things are signed (DIV, MPY, CAS). The fact CAS is happens to
* be signed is useful. We use SUB/CC for unsigned compares and CAS for
* most signed ones
*
* TODO:
* - div/rem is wrong - hardware is signed 32/16 and it's optional
* - optimizations for rest of += etc ops (notably |=)
* - can we support the 116 ?
*/
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "compiler.h"
#include "backend.h"
#define BYTE(x) (((unsigned)(x)) & 0xFF)
#define WORD(x) (((unsigned)(x)) & 0xFFFF)
/*
* State for the current function
*/
static unsigned frame_len; /* Number of words of stack frame */
static unsigned sp; /* Stack pointer offset tracking */
static unsigned argbase; /* Argument offset */
static unsigned unreachable; /* Is code currently unreachable */
static unsigned label; /* Used for local labels */
#define ARGBASE 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 is_bytepointer(unsigned t)
{
/* A char ** is a word pointer, a char * is a byte pointer */
if (PTR(t) != 1)
return 0;
t = BASE_TYPE(t) & ~UNSIGNED;
if (t == CCHAR || t == VOID)
return 1;
return 0;
}
/*
* As a word machine our types matter for pointer conversions
* and we cannot blithly throw them away as most byte machines
* can. In the Nova case we need to shift it left or right between
* byte pointers (void, char *, unsigned char *) and other types
*/
static unsigned pointer_match(unsigned t1, unsigned t2)
{
if (!PTR(t1) || !PTR(t2))
return 0;
if (is_bytepointer(t1) == is_bytepointer(t2))
return 1;
return 0;
}
/*
* Byte sizes although we are a word machine
*/
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;
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_LDEREF (T_USER+7) /* *local */
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;
}
/* 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
* at this point, but we do rewrite name references and function calls
* to make them easier to process.
*/
struct node *gen_rewrite_node(register struct node *n)
{
register struct node *l = n->left;
register struct node *r = n->right;
register unsigned op = n->op;
register unsigned nt = n->type;
unsigned s= get_size(nt);
/* Turn *local into single node we can use indirection */
if (op == T_DEREF && r->op == T_LREF && s == 2) {
r->val2 = n->value;
squash_right(n, T_LDEREF);
return n;
}
/* Rewrite references into a load operation. Q: is this all types ?? */
if (nt == CCHAR || nt == UCHAR || nt == CSHORT || nt == USHORT || PTR(nt) || nt == CLONG || nt == ULONG || nt == FLOAT) {
if (op == T_DEREF) {
if (r->op == T_LOCAL || r->op == T_ARGUMENT) {
/* Offsets are in bytes, we are a word machine */
if (r->op == T_ARGUMENT)
r->value += (argbase + frame_len) * 2;
squash_right(n, T_LREF);
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) * 2;
squash_left(n, T_LSTORE);
return n;
}
}
}
/* Eliminate casts for sign, pointer conversion or same */
if (op == T_CAST) {
if (nt == r->type || (nt ^ r->type) == UNSIGNED ||
(pointer_match(nt, r->type))) {
free_node(n);
r->type = nt;
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;
}
return n;
}
/* Export the C symbol */
void gen_export(const char *name)
{
printf(" .export _%s\n", name);
}
void gen_segment(unsigned s)
{
switch(s) {
case A_CODE:
printf("\t.code\n");
break;
case A_DATA:
printf("\t.data\n");
break;
case A_LITERAL:
printf("\t.literal\n");
break;
case A_BSS:
printf("\t.bss\n");
break;
default:
error("gseg");
}
}
static unsigned get_stack_size(unsigned t)
{
unsigned n = get_size(t);
if (n == 1)
return 2;
return n;
}
static void repeated_op(unsigned n, char *op)
{
while(n--)
printf("\t%s\n", op);
}
/* Load A with a constant */
void load_a(unsigned n)
{
n = WORD(n);
/* Is it better to have a common const table too - 0xFF, 0xFF00 etc */
switch(n) {
case 0:
puts("\tcra");
break;
case 1:
puts("\tld1");
break;
case 2:
puts("\tcra\n\ta2a");
break;
case 3:
puts("\tld1\n\ta2a");
break;
case 4:
puts("\tld1\n\tlgl 2");
break;
case 8:
puts("\tld1\n\tlgl 3");
break;
case 16:
puts("\tld1\n\tlgl 4");
break;
case 32:
puts("\tld1\n\tlgl 5");
break;
case 64:
puts("\tld1\n\tlgl 6");
break;
case 128:
puts("\tld1\n\tlgl 7");
break;
case 256:
puts("\tld1\n\tica");
break;
case 257:
puts("\tld1\n\tbtl");
break;
case 0xFFFE:
puts("\tld1\n\tcma");
break;
case 0xFFFF:
puts("\tcra\n\tcma");
break;
default:
/* This is ok for small stuff where we have enough pages only */
/* printf("\tlda =%u\n", n);*/
printf("\tjst @loadconst\n\t.const %u\n", n);
}
}
/* Load BA with a constant */
void load_ba(unsigned long n)
{
/* This is ok for small stuff where we have enough pages only */
/* printf("\tlda =%u\n", n);*/
printf("\tjst @loadconstl\n\t.const %u\n\t.const %u\n", WORD(n), WORD(n >> 16));
}
/* Load a byte, word or dword via X. Only some byte cases
are supported */
void load_via_x(struct node *n, int off)
{
unsigned s = get_size(n->type);
unsigned op = n->op;
if (s == 1) {
/* These are always word aligned bases */
if (op == T_LREF || op == T_NREF || op == T_LBREF) {
printf("\tlda @%d,1\n", off);
if (off & 1)
puts("\ticl");
return;
}
/* Arbitrary byte load via X is a no no */
error("lvx");
}
if (s == 4)
printf("\tlda @%d,1\n\tiab", off - 1);
printf("\tlda @%d,1\n", off);
}
/* Store a word or dword via X */
void store_via_x(struct node *n, unsigned off)
{
unsigned s = get_size(n->type);
if (s == 1)
error("stb");
printf("\tsta @%u,1\n", off);
/* TODO: when NR don't do final iab */
if (s == 4)
printf("\tiab\n\tsta @%u1,1\n\tiab\n", off + 1);
}
static unsigned xstate;
static unsigned xstate_sp;
static struct node xnode;
static unsigned xmatch(unsigned op, register struct node *n, unsigned *off)
{
unsigned v = *off;
unsigned s = get_size(n->type);
if (s == 4)
s = 254;
else
s = 255;
if (op != xnode.op || n->val2 != xnode.val2)
return 0;
/* Same object but is it the same position */
v = *off - xnode.value / 2; /* Working in words */
/* Range check */
if (v < 0)
return 0;
if (v > 254)
return 0;
*off = v;
return 1;
}
/* Load X with SP. Preserve A if requested to do so */
static void load_x_sp(unsigned keep)
{
if (cpu == 116) {
if (keep)
puts("\tsta @tmp2");
puts("\tlda @sp\n\tsta @0");
if (keep)
puts("\tlda @tmp2");
} else
puts("\tldx @sp");
xstate = T_LOCAL;
xstate_sp = sp;
}
/* Try and generate the code for an operation to point X
at an object. In the case of a bytepointer we point X
at the object word and return a byte offset, otherwise
we point at the object and return the word offset. */
static unsigned make_x_point(struct node *n, unsigned *off, unsigned keep_a)
{
unsigned v = n->value / 2;
unsigned s = get_size(n->type);
unsigned max = 255;
*off = 0;
/* As we need to access @n+1,1 for these */
if (s == 4)
max = 254;
switch(n->op) {
case T_LDEREF:
case T_LREF:
case T_LSTORE:
v += sp;
if (v > 255) /* Out of range */
return 0;
if (xstate == T_LOCAL) {
v += sp - xstate_sp;
if (v > max)
return 0;
} else
load_x_sp(keep_a);
/* Turn byte offsets back to the correct byte */
if (s == 1)
*off = v * 2+ (n->value & 1);
else
*off = v;
return 1;
case T_LBREF:
case T_LBSTORE:
if (!xmatch(T_LABEL, n, &v)) {
printf("\tjst @loadx\n\t.addr T%u+%u\n", n->val2, v);
xstate = T_LABEL;
memcpy(&xnode, n, sizeof(struct node));
}
return 1;
case T_NREF:
case T_NSTORE:
if (!xmatch(T_NAME, n, &v)) {
printf("\tjst @loadx\n\t.addr _%s+%u\n", namestr(n->snum), v);
xstate = T_NAME;
memcpy(&xnode, n, sizeof(struct node));
}
return 1;
}
return 0;
}
static unsigned can_make_x(struct node *n)
{
unsigned v = n->value / 2;
unsigned s = get_size(n->type);
if (s == 4)
s = 254;
else
s = 255;
switch(n->op) {
case T_ARGUMENT:
v += frame_len + argbase;
case T_LOCAL:
v += sp;
if (v > s) /* Out of range */
return 0;
return 1;
case T_LABEL:
return 1;
case T_NAME:
return 1;
}
return 0;
}
/* TODO: byte pointers */
static unsigned make_x(struct node *n, unsigned *off, unsigned keep_a)
{
unsigned v = n->value / 2;
unsigned s = get_size(n->type);
if (s == 4)
s = 254;
else if (s == 2)
s = 255;
else
error("mxs");
*off = 0;
switch(n->op) {
case T_ARGUMENT:
v += frame_len + argbase;
case T_LOCAL:
v += sp;
if (v > s) /* Out of range */
return 0;
if (xstate == T_LOCAL) {
v += sp - xstate_sp;
if (v > s)
return 0;
} else
load_x_sp(keep_a);
*off = v;
return 1;
case T_LABEL:
if (!xmatch(T_LABEL, n, &v)) {
printf("\tjst @loadx\n\t.addr T%u+%u\n", n->val2, v);
xstate = T_LABEL;
memcpy(&xnode, n, sizeof(struct node));
}
return 1;
case T_NAME:
if (!xmatch(T_NAME, n, &v)) {
printf("\tjst @loadx\n\t.addr _%s+%u\n", namestr(n->snum), v);
xstate = T_NAME;
memcpy(&xnode, n, sizeof(struct node));
}
return 1;
}
return 0;
}
static void modified_x(void)
{
xstate = 0;
}
/* Can destroy A */
static void make_x_a(void)
{
puts("\tsta @0\n");
xstate = 0;
}
/*
* Pop is thankfully simple
*/
static void pop_x(void)
{
puts("\tldx *@sp\n\tirs @sp"); /* Won't skip */
xstate = 0;
}
static void pop_a(void)
{
puts("\tlda *@sp\n\tirs @sp"); /* Won't skip */
}
/* TODO: byte pointers */
static unsigned can_make_a(struct node *n)
{
if (get_size(n->type) == 1)
return 0;
/* Things we can get into A without using X */
switch(n->op) {
case T_CONSTANT:
return 1;
case T_ARGUMENT:
case T_LOCAL:
case T_LABEL:
case T_NAME:
case T_LBREF:
case T_NREF:
return 1;
}
return 0;
}
/* Must not harm @tmp */
static unsigned make_a(struct node *n)
{
unsigned s = get_size(n->type);
unsigned v = n->value / 2;
if (s == 1)
error("ma bp");
switch(n->op) {
case T_CONSTANT:
if (s == 4) {
load_a(n->value >> 16);
puts("\tiab");
}
load_a(n->value);
return 1;
case T_ARGUMENT:
v += frame_len + argbase;
case T_LOCAL:
v += sp;
load_a(v);
puts("\tadd @sp");
return 1;
case T_LABEL:
printf("\tjst @loadconst\n\t.addr T%u+%u\n", n->val2, v);
return 1;
case T_NAME:
printf("\tjst @loadconst\n\t.addr _%s+%u\n", namestr(n->snum), v);
return 1;
case T_LBREF:
printf("\tjst @loadconst\n\t.indirect T%u+%u\n", n->val2, v);
return 1;
case T_NREF:
printf("\tjst @loadconst\n\t.indirect _%s+%u\n", namestr(n->snum), v);
return 1;
}
return 0;
}
/* Make a point to something. If we are working on bytes generate a
byte pointer */
static unsigned make_a_bp(struct node *n)
{
unsigned v = n->value;
unsigned s = get_size(n->type);
switch(n->op) {
case T_LREF:
case T_LSTORE:
if (s > 1)
v /= 2;
v += sp;
load_a(v);
puts("\tadd @sp");
if (s == 1) /* add sp twice as it's in words and
we are in bytes */
puts("\tadd @sp");
return 1;
case T_LBREF:
case T_LBSTORE:
puts("\tjst @loadconst");
if (s == 1)
printf("\t.byteptr T%u+%u\n", n->val2, v);
else
printf("\t.addr T%u+%u\n", n->val2, v / 2);
return 1;
case T_NREF:
case T_NSTORE:
puts("\tjst @loadconst");
if (s == 1)
printf("\t.byteptr _%s+%u\n", namestr(n->snum), v);
else
printf("\t.addr _%s+%u\n", namestr(n->snum), v);
return 1;
}
return 0;
}
static void irs_via_x(struct node *l, unsigned v)
{
unsigned lv = l->value;
unsigned off;
/* Don't be clever with offsets or we may run out of zp */
if (l->op == T_NAME && !l->value) {
while(v--)
printf("\tirs _%s+%u\n\tnop\n", namestr(l->snum), lv);
return;
}
if (l->op == T_LABEL && !l->value) {
while(v--)
printf("\tirs T%u+%u\n\tnop\n", l->val2, lv);
return;
}
make_x(l, &off, 0);
while(v--)
printf("\tirs %u,x\n\tnop\n", off);
}
/* Try and generate the code for an operation.*/
static unsigned op_direct(const char *op, struct node *n)
{
unsigned s = get_size(n->type);
unsigned v = n->value;
unsigned off;
/* For now */
if (s != 2 || is_bytepointer(n->type))
return 0;
switch(n->op) {
case T_CONSTANT:
printf("\t%s =%u\n", op, v);
return 1;
case T_LABEL:
printf("\t%s =T%u+%u\n", op, n->val2, v / 2);
return 1;
case T_NAME:
printf("\t%s =_%s+%u\n", op, namestr(n->snum), v / 2);
return 1;
case T_LBREF:
printf("\t%s T%u+%u\n", op, n->val2, v / 2);
return 1;
case T_NREF:
printf("\t%s _%s+%u\n", op, namestr(n->snum), v / 2);
return 1;
}
if (make_x_point(n, &off, 1)) {
printf("\t%s @%u,1\n", op, off);
return 1;
}
return 0;
}
static unsigned op_a_tmp(unsigned s, const char *op, const char *pre)
{
if (s != 2)
return 0;
puts("\tsta @tmp");
if (pre)
puts(pre);
pop_a();
printf("%s @tmp\n", op);
return 1;
}
/* Boolify the word in A. We implement CCONLY as indicating
Z or NZ is all that is needed */
void boolnot(struct node *n)
{
unsigned s = get_size(n->right->type);
if (n->flags & ISBOOL) {
puts("\txra @one\n");
return;
}
if (s == 1)
puts("\tcal");
else if (s == 4)
puts("\tsnz\n\tiab");
/* Either A is 0 and should be 1, or A is NZ and should be 0 */
puts("\trcb\n\tsnz\n\tscb\n\tcra\n\taca");
n->flags |= ISBOOL;
}
void boolify(struct node *n)
{
unsigned s = get_size(n->right->type);
if (n->flags & ISBOOL)
return;
if (s == 1)
puts("\tcal");
else if (s == 4)
puts("\tsnz\n\tiab");
/* 0 is 0, anything else is 1 */
if (!(n->flags & CCONLY))
puts("\tsze\n\tld1");
n->flags |= ISBOOL;
}
/* Turn carry to boolean */
void boolc(struct node *n)
{
puts("\tcra\n\taca");
n->flags |= ISBOOL;
}
void boolzc(struct node *n)
{
puts("\tsnz\tscb\n\tcra\n\taca");
n->flags |= ISBOOL;
}
void boolnc(struct node *n)
{
puts("\tcra\n\tssc\n\tld1");
n->flags |= ISBOOL;
}
void boolznc(struct node *n)
{
puts("\tsnz\n\trcb\nssc\ncra\n\taca");
n->flags |= ISBOOL;
}
/* Subtract 16bit const */
void subconst(unsigned v)
{
v = WORD(v);
if (v == 0xFFFF)
puts("\taoa"); /* Same as add one */
else if (v == 0xFFFE)
puts("\ta2a");
else if (v)
printf("\tsub =%u\n", v);
}
/* Simple word ops on A or BA */
unsigned wordop(unsigned s, const char *op)
{
if (s == 1)
return 0;
if (s == 2)
printf("\t%s\n", op);
if (s == 4)
printf("\tiab\n\t%s\n\tiab\n", op);
return 1;
}
void gen_prologue(const char *name)
{
unreachable = 0;
printf("_%s:\n", name);
modified_x();
}
/* Generate the stack frame */
void gen_frame(unsigned size, unsigned aframe)
{
size = (size + 1) / 2;
frame_len = size;
argbase = ARGBASE;
sp = 0;
if (size) {
load_a(-size);
puts("\tadd @sp\n\tsta @sp");
}
}
void gen_epilogue(unsigned size, unsigned argsize)
{
if (sp)
error("sp");
if (unreachable)
return;
puts("\tjmp *@pret");
unreachable = 1;
}
void gen_label(const char *tail, unsigned n)
{
unreachable = 0;
modified_x();
printf("L%d%s:\n", n, tail);
}
unsigned gen_exit(const char *tail, unsigned n)
{
puts("\tjmp *@pret");
unreachable = 1;
return 1;
}
void gen_jump(const char *tail, unsigned n)
{
printf("\tjmp L%d%s\n", n, tail);
unreachable = 1;
}
void gen_jfalse(const char *tail, unsigned n)
{
printf("\tsnz\n");
printf("\tjmp L%d%s\n", n, tail);
}
void gen_jtrue(const char *tail, unsigned n)
{
printf("\tsze\n");
printf("\tjmp L%d%s\n", n, tail);
}
void gen_switch(unsigned n, unsigned type)
{
/* Byte switch data is in the upper half of the word. Flip the byte
clear the rest and use the int helper */
if (type == CCHAR || type == UCHAR)
puts("\ticr");
puts("\tjst @switch");
if (type == CLONG || type == ULONG)
putchar('l');
printf("\n\t.addr Sw%d\n", n);
unreachable = 1;
}
void gen_switchdata(unsigned n, unsigned size)
{
printf("Sw%d:\n", n);
printf("\t.const %d\n", size);
}
void gen_case(unsigned tag, unsigned entry)
{
printf("Sw%d_%d:\n", tag, entry);
}
void gen_case_label(unsigned tag, unsigned entry)
{
unreachable = 0;
modified_x();
printf("Sw%d_%d:\n", tag, entry);
}
void gen_case_data(unsigned tag, unsigned entry)
{
printf("\t.addr Sw%d_%d\n", tag, entry);
}
/* We will need to distinguish fast helpers via @zp and slow
helpers */
void gen_helpcall(struct node *n)
{
printf("\tjst *@pcall\n\t.addr ");
}
void gen_helptail(struct node *n)
{
}
void gen_helpclean(struct node *n)
{
}
void gen_data_label(const char *name, unsigned align)
{
printf("_%s:\n", name);
}
void gen_space(unsigned value)
{
/* In words */
printf("\t.ds %d\n", (value + 1) / 2);
}
void gen_text_data(struct node *n)
{
if (is_bytepointer(n->type))
printf("\t.byteptr T%d\n", n->val2);
else
printf("\t.addr T%d\n", n->val2);
}
void gen_literal(unsigned n)
{
if (n)
printf("T%d:\n", n);
}
void gen_name(struct node *n)
{
if (is_bytepointer(n->type))
printf("\t.byteptr _%s+%d\n", namestr(n->snum), WORD(n->value));
else
printf("\t.addr _%s+%d\n", namestr(n->snum), WORD(n->value));
}
void gen_value(unsigned type, unsigned long value)
{
unsigned v = value;
if (PTR(type)) {
if (is_bytepointer(type))
printf("\t.byteptr %u\n", v);
else
printf("\t.const %u\n", v);
return;
}
switch (type) {
case CCHAR:
case UCHAR:
printf("\t.byte %u\n", v & 0xFF);
break;
case CSHORT:
case USHORT:
printf("\t.const %d\n", v & 0xFFFF);
break;
case CLONG:
case ULONG:
case FLOAT:
/* We are little endian */
printf("\t.const %d\n", v);
printf("\t.const %d\n", (unsigned) ((value >> 16) & 0xFFFF));
break;
default:
error("unsuported type");
}
}
/* Byte constants for helpers are written as words */
void gen_wvalue(unsigned type, unsigned long value)
{
unsigned v = value & 0xFFFF;
if (PTR(type)) {
if (is_bytepointer(type))
printf("\t.byteptr %u\n", v);
else
printf("\t.const %u\n", v);
return;
}
switch (type) {