-
Notifications
You must be signed in to change notification settings - Fork 13
/
backend-nova.c
2270 lines (2175 loc) · 49.1 KB
/
backend-nova.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
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "compiler.h"
#include "backend.h"
/*
* The core compiler thinks mostly in bytes. Whilst it understands
* pointer conversions and the like our stack offsets in arguments
* and locals are byte oriented (as we might pack byte variables).
*
* In addition on the Nova our stack grows up through memory so all
* the argument offsets are negative upwards from the frame pointer
* allowing for the SAV frame (5 words)
*
* TODO:
*
* Add support for Nova4 (LDB STB)
* DONE: Add support for Mul/Div hardware
* DONE: Inline small left and unsigned right shifts
*
* DONE Inline shifts (including using ADDZL for double left shift)
* IP Inline easy mul forms (0-16 etc)
* Spot by 16+ shifts and move ?
* Optimized long and or xor const
*
* DONE Shift optimized mul/div constant
* short helper mul/div constant
*
* Compare optimizations. We can do better stuff
* for 0 based compares, for 1 and -1 compares which
* are all common, and for sign bit compares
*
* Inline some cases of deref/lref etc esp stuff like lref where
* we know the offset so in turn can directly reference the right half
*
* Track contents of AC1 so we can avoid reloading constants
* (may be worth tracing AC0 too but less clear)
* Track whether AC0 holds __hireg and optimize load/saves of it
*
* LDEREF and equivalents akin to 6809 as we can also do stuff like
* lda 1,@n,3 to index through a local pointer
*
* Byte LREF/LSTORE etc
* - Optimisations from the DDP where we use the fact the base is
* word aligned to precompute any needed swaps
*
* Floating point (hardware)
*
* Eclipse
* DONE - Multiply/Divide
* DONE - Halve
* DONE - LEA/Immediate forms
* DONE - LEA genera usage
* MOST - ejsr, elda, esta
* DONE - push/pop do 1-4 accumulators at a time
* DONE - msp for stack cleanup
* DONE - mffp and friends instead are memory 040/041
* - signed compare two ac
* DONE - addi/andi etc
* - bitops
* - ldb/stb for eqops
*
* Eclipse C/300
* - eldb/estb
*/
#define HAS_MULDIV 1
#define BYTE(x) (((unsigned)(x)) & 0xFF)
#define WORD(x) (((unsigned)(x)) & 0xFFFF)
/*
* 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 */
static unsigned unreachable; /* Is code currently unreachable */
#define ARGBASE 10 /* 5 words (10 bytes) */
#define ARGBASE_ECLIPSE 10 /* 5 words (10 bytes) */
/* 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;
}
/*
* 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;
}
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)
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;
}
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;
}
/*
* 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;
/* TODO: implement derefplus as we can fold small values into
a deref pattern */
/* Rewrite references into a load operation.
Char is weird as we then use byte pointers and helpers so avoid */
if (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 */
/* Arguments are below the FP, variables above with
a 2 byte offset */
if (r->op == T_ARGUMENT) {
r->value = -(argbase + r->value) / 2;
if (nt == CLONG || nt == ULONG || nt == FLOAT)
r->value --;
} else
/* Always word addressed */
r->value = r->value / 2 + 1;
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 + l->value) / 2;
if (nt == CLONG || nt == ULONG)
l->value --;
} else
/* Word machine */
l->value = l->value / 2 + 1;
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 load_constant(unsigned ac, unsigned v)
{
/* For the lower half of the number space we can use elef to generate
an effective address relative to 0. The rest we can do by negation */
if (cpu >= 100) {
if (v < 0x8000)
printf("\telef %u,%u,0\n", ac, v);
else
printf("\telef %u,%u,0\n\tneg 0,0\n", ac, WORD(-v));
} else {
printf("\tjsr @__const%u,0\n", ac);
printf("\t.word %u\n", v);
}
return 1;
}
void gen_prologue(const char *name)
{
unreachable = 0;
printf("_%s:\n", name);
}
static void repeated_op(unsigned n, char *op)
{
while(n--)
printf("\t%s\n", op);
}
/* Generate the stack frame */
void gen_frame(unsigned size, unsigned aframe)
{
frame_len = size;
sp = 0;
/* Remember the stack grows upwards so values are negative offsets */
if (cpu >= 100) {
/* DG Eclipse */
/* Stack pointer is location 040 frame is 041 */
argbase = ARGBASE_ECLIPSE;
printf("\tsav\n");
if (size == 0)
return;
size = (size + 1) /2;
/* TODO optimize small cases, share with gen_cleanup() */
load_constant(0, size);
printf("\tmsp 0\n");
} else if (cpu >= 3) {
argbase = ARGBASE;
printf("\tsav\n");
printf("\tisz 0,3\n"); /* Will never skip */
if (size == 0)
return;
/* In words, rounded in case an odd number of bytes */
size = (size + 1) / 2;
if (size >= 5) {
printf("\tmfsp 1\n");
printf("\tlda 0,2,1\n");
printf("\tadd 0,1,skp\n");
printf("\t.word %u\n", size);
printf("\tmtsp 1\n");
} else
repeated_op(size, "psha 0");
} else {
/* We push a smaller frame (ret and old fp) */
argbase = 4; /* In bytes */
/* We can uninline most of this */
printf("\tmov 3,2\n");
printf("\tjsr @__enter,0\n");
printf("\t.word %u\n", size);
}
printf(";\n");
}
/* The return restores all the registers so we have to patch the stack frame */
void gen_epilogue(unsigned size, unsigned argsize)
{
if (sp != 0)
error("sp");
if (unreachable)
return;
if (cpu >= 100) {
/* Eclipse */
if (!(func_flags & F_VOIDRET))
printf("\tsta 1,-3,3\n");
puts("\trtn");
}
if (cpu >= 3) {
if (!(func_flags & F_VOIDRET))
printf("\tsta 1,-3,3\n");
printf("\tret\n");
} else
printf("\tjmp @__ret,0\n");
unreachable = 1;
}
void gen_label(const char *tail, unsigned n)
{
unreachable = 0;
printf("L%d%s:\n", n, tail);
}
unsigned gen_exit(const char *tail, unsigned n)
{
unreachable = 1;
/* It's as cheap to return as jmp ahead for some cases */
if (cpu >= 100) {
if (!(func_flags & F_VOIDRET))
printf("\tsta 1,-3,3\n");
printf("\trtn\n");
} else if (cpu >= 3) {
if (!(func_flags & F_VOIDRET))
printf("\tsta 1,-3,3\n");
printf("\tret\n");
} else {
printf("\tjmp @__ret,0\n");
}
return 1;
}
void gen_jump(const char *tail, unsigned n)
{
if (cpu >= 100)
printf("\tejmp L%d%s\n", n, tail);
else {
printf("\tjmp @1,1\n");
printf("\t.word L%d%s\n", n, tail);
}
unreachable = 1;
}
void gen_jfalse(const char *tail, unsigned n)
{
/* TODO we need a self expanding jump with value hiding */
printf("\tjsr @__jf,0\n");
printf("\t.word L%d%s\n", n, tail);
}
void gen_jtrue(const char *tail, unsigned n)
{
printf("\tjsr @__jt,0\n");
printf("\t.word L%d%s\n", n, tail);
}
void gen_switch(unsigned n, unsigned type)
{
printf("\tjsr @__switch");
helper_type(type, 0);
printf(",0\n\t.word Sw%d\n", n);
unreachable = 1;
/* Although we jsr that's just to pass the table ptr */
}
void gen_switchdata(unsigned n, unsigned size)
{
printf("Sw%d:\n", n);
printf("\t.word %d\n", size);
}
void gen_case_label(unsigned tag, unsigned entry)
{
unreachable = 0;
printf("Sw%d_%d:\n", tag, entry);
}
void gen_case_data(unsigned tag, unsigned entry)
{
printf("\t.word Sw%d_%d\n", tag, entry);
}
void gen_cleanup(unsigned v)
{
if (v == 0)
return;
sp -= v;
if (cpu >= 100) {
/* Eclipse has a stack modify op */
load_constant(0, -v);
printf("\tmsp 0\n");
} else if (cpu < 3) {
/* As is common we are switching back to the frame pointer
being the sp base . TODO debug check */
if (sp == 0 && frame_len == 0)
printf("\tsta 3,__sp,0\n");
else if (v > 5) {
printf("\tlda 0,__sp,0\n");
printf("\tlda 2,2,1\n");
printf("\tadd 2,0,skp\n");
printf("\t.word %u\n", (-v) & 0xFFFF);
printf("\tsta 2,__sp,0\n");
} else {
repeated_op(v, "dsz __sp,0");
}
} else {
if (sp == 0 && frame_len == 0)
printf("\tmtsp 3\n");
else if (v > 5) {
printf("\tmfsp 1\n");
printf("\tlda 0,2,1\n");
printf("\tadd 0,1,skp\n");
printf("\t.word %u\n", (-v) & 0xFFFF);
printf("\tmtsp 1\n");
} else
repeated_op(v, "popa 0");
}
}
/* 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)
{
/* TODO need to figure out what is indirected this way and what
is done jsr @1,1 style */
if (cpu > 100) {
if (c_style(n))
gen_push(n->right);
printf("\tejsr __");
} else if (c_style(n)) {
gen_push(n->right);
printf("\tjsr @1,1\n");
printf("\t.word __");
} else
printf("\tjsr @__");
}
void gen_helptail(struct node *n)
{
if (!c_style(n))
printf(",0");
}
void gen_helpclean(register struct node *n)
{
unsigned v = 0;
/* C style helpers pushed a word */
if (!c_style(n))
return;
if (n->left) {
v = get_stack_size(n->left->type);
sp += v / 2;
}
v += get_stack_size(n->right->type);
gen_cleanup(v / 2);
if (n->flags & ISBOOL) {
printf("\tmov 1,1,szr\n");
printf("\tsubzl 1,1\n");
}
}
void gen_data_label(const char *name, unsigned align)
{
printf("_%s:\n", name);
}
void gen_space(unsigned value)
{
/* Word based */
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.word 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.word _%s+%d\n", namestr(n->snum), WORD(n->value));
}
/* FIXME: we will need to add .byte and alignment padding to the
assembler for char arrays. This is now OK to do as the asm/ld think
in bytes for word machines with byte pointers */
void gen_value(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.word %u\n", v);
return;
}
switch (type) {
/* Bytes alone are word aligned on the left of the word */
case CCHAR:
case UCHAR:
printf("\t.byte %u\n", v & 0xFF);
break;
case CSHORT:
case USHORT:
printf("\t.word %u\n", v);
break;
case CLONG:
case ULONG:
case FLOAT:
/* We are big endian - software choice */
printf("\t.word %u\n", (unsigned) ((value >> 16) & 0xFFFF));
printf("\t.word %u\n", v);
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.word %u\n", v);
return;
}
switch (type) {
/* Bytes alone are word aligned on the left of the word */
case CCHAR:
case UCHAR:
case CSHORT:
case USHORT:
printf("\t.word %u\n", v);
break;
case CLONG:
case ULONG:
case FLOAT:
/* We are big endian - software choice */
printf("\t.word %u\n", (unsigned) ((value >> 16) & 0xFFFF));
printf("\t.word %u\n", v);
break;
default:
error("unsuported type");
}
}
void gen_start(void)
{
printf("\t.code\n");
if (cpu >= 100)
cpufeat |= HAS_MULDIV;
}
void gen_end(void)
{
}
void gen_tree(struct node *n)
{
codegen_lr(n);
printf(";\n");
}
/* Use the hardware stack option on the later NOVA and the software
approach otherwise */
void popa(unsigned r)
{
if (cpu >= 3)
printf("\tpopa %u\n", r);
else {
/* Have to do double dec due to the autoinc */
printf("\tdsz __sp,0\n");
printf("\tlda %u,@__sp,0\n", r);
printf("\tdsz __sp,0\n");
}
}
void psha(unsigned r)
{
if (cpu >= 100)
printf("\tpsh %u,%u\n", r, r);
else if (cpu >= 3)
printf("\tpsha %u\n", r);
else
printf("\tsta %u,@__sp,0\n", r);
}
/* So we can track this later and suppress some */
static void load_hireg(unsigned ac)
{
printf("\tlda %u,__hireg,0\n", ac);
}
static void store_hireg(unsigned ac)
{
printf("\tsta %u,__hireg,0\n", ac);
}
static void wipe_hireg(void)
{
load_constant(0,0);
store_hireg(0);
}
unsigned gen_push(struct node *n)
{
/* Our push will put the object on the stack, so account for it */
unsigned s = get_stack_size(n->type);
sp += s / 2;
/* We are big endian but with an upward growing stack so we must
push the high word first */
if (s == 4) {
load_hireg(0);
if (cpu >= 100) {
printf("\tpsh 0,1\n");
return 1;
}
psha(0);
}
psha(1);
printf(";\n");
return 1;
}
static unsigned gen_constant(unsigned r, int16_t v)
{
/* TODO: other values - to begin with byte swap forms of these */
switch(v) {
case 0:
printf("\tsub %u,%u\n", r, r);
return 1;
case 1:
printf("\tsubzl %u,%u\n", r, r);
return 1;
case 2:
printf("\tsub %u,%u\n", r, r);
printf("\tinczl %u,%u\n", r, r);
return 1;
case 3:
printf("\tsub %u,%u\n", r, r);
printf("\tincol %u,%u\n", r, r);
return 1;
case 4:
printf("\tsubzl %u,%u\n", r, r);
printf("\taddzl %u,%u\n", r, r);
return 1;
case 5:
printf("\tsubzl %u,%u\n", r, r); /* 1 */
printf("\tincol %u,%u\n", r, r); /* 5 */
return 1;
case -1:
printf("\tadc %u,%u\n", r, r);
return 1;
case -2:
printf("\tadczl %u,%u\n", r, r);
return 1;
case -4:
printf("\tadczl %u,%u\n", r, r);
printf("\tadd %u,%u\n", r,r);
return 1;
case -8:
printf("\tadczl %u,%u\n", r, r);
/* This will toggle the carry so shift in a 0 */
printf("\taddol %u,%u\n", r,r);
return 1;
default:;
}
if (!optsize && cpu < 100) {
switch(v) {
case 6:
printf("\tsub %u,%u\n", r, r);
printf("\tincol %u,%u\n", r, r);
printf("\tmovl %u,%u\n", r, r);
return 1;
case 8:
printf("\tsubzl %u,%u\n", r, r);
printf("\taddzl %u,%u\n", r, r);
printf("\tmovl %u,%u\n", r, r);
return 1;
case 10:
printf("\tsubzl %u,%u\n", r, r);
printf("\tincol %u,%u\n", r, r);
printf("\tmovl %u,%u\n", r, r);
return 1;
case 11:
printf("\tsubzl %u,%u\n", r, r); /* 1 */
printf("\taddzl %u,%u\n", r, r); /* 4 */
printf("\tincol %u,%u\n", r, r); /* 11 */
return 1;
case 12:
printf("\tsub %u,%u\n", r, r);
printf("\tincol %u,%u\n", r, r);
printf("\taddzl %u,%u\n", r, r);
return 1;
case 13:
printf("\tsubzl %u,%u\n", r, r); /* 1 */
printf("\tincol %u,%u\n", r, r); /* 5 */
printf("\tincol %u,%u\n", r, r); /* 13 */
return 1;
case 16:
printf("\tsubzl %u,%u\n", r, r);
printf("\taddzl %u,%u\n", r, r);
printf("\taddzl %u,%u\n", r, r);
return 1;
case 20:
printf("\tsubzl %u,%u\n", r, r);
printf("\tincol %u,%u\n", r, r);
printf("\taddzl %u,%u\n", r, r);
return 1;
}
}
return 0;
}
/*
* True if we can load ac0 with the value we need without trashing
* AC1. This lets us avoid a lot of the pushing and popping we would
* otherwise do in the code generation. The more we can add to this
* the better. Right now we just do some ops but we could look at
* implementable subtrees even - eg deref of thing we can do or
* maths ops via AC2 on AC0
*/
static unsigned can_load_ac(struct node *n)
{
/* Start with simple stuff */
unsigned s = get_size(n->type);
if (s != 2)
return 0;
switch(n->type) {
case T_LOCAL:
case T_LREF:
case T_NAME:
case T_LABEL:
case T_CONSTANT:
case T_NREF:
case T_LBREF:
return 1;
}
return 0;
}
/*
* The actual helper can do more than can_load_ac but must either
* generate all the code or none. This allows some ops to try and
* see what is possible if they can recover from a "no" answer
*
* TODO: clean up duplication
*/
static unsigned load_ac(unsigned ac, register struct node *n)
{
unsigned v = n->value;
register int16_t d = v;
unsigned s = get_size(n->type);
if (s != 2)
return 0;
switch(n->op) {
/* TODO: use ELEF where we can */
case T_ARGUMENT:
/* Stack grows upward and our offsets are in words */
/* Except for bytes, then our addresses for locals are
byte pointers! */
d = -(argbase + d); /* Bytes */
if (is_bytepointer(n->type)) {
/* Convert to byte pointer */
printf("\tmovzl 3,%u\n", ac);
d++; /* Arguments are stacked as words so the
value is 1 byte offset */
printf("\tlda 2,2,1\n");
printf("\tadd 2,%u,skp\n", ac);
printf("\t.word %d\n", d);
} else {
if (d & 1)
error("waln");
d /= 2; /* Word offset word pointer */
/* Our stack is upward growing so the offsets of the fields
are 0,-1 so adjust here to keep sanity elsewhere */
if (get_size(n->type - PTRTO) == 4)
d--;
if (cpu >= 100)
printf("\telef %u,%u,1\n", ac, d);
else if (gen_constant(ac, d))
printf("\tadd 3,%u\n", ac);
else {
printf("\tlda %u,2,1\n", ac);
printf("\tadd 3,%u,skp\n", ac);
printf("\t.word %d\n", d);
}
}
return 1;
case T_LOCAL:
d += 2; /* Byte offset for FP */
if (is_bytepointer(n->type)) {
/* Convert to byte pointer */
printf("\tmovzl 3,%u\n", ac);
printf("\tlda 2,2,1\n");
printf("\tadd 2,%u,skp\n", ac);
printf("\t.word %d\n", d);
} else {
if (d & 1)
error("waln");
/* Work in words */
d /= 2;
if (cpu >= 100)
printf("\telef %u,%u,1\n", ac, d);
else if (gen_constant(ac, d))
printf("\tadd 3,%u\n", ac);
else {
printf("\tlda %u,2,1\n", ac);
printf("\tadd 3,%u,skp\n", ac);
printf("\t.word %d\n", d);
}
}
return 1;
case T_LREF:
/* Will always be size 2 at this point */
if (d >= -128 && d < 128) {
printf("\tlda %u,%d,3\n", ac, d);
return 1;
}
if (cpu >= 100) {
printf("\telda %u,%d,3\n", ac, d);
return 1;
}
if (gen_constant(2, d))
printf("\tadd 3,2\n");
else {
printf("\tlda 2,2,1\n");
printf("\tadd 3,2,skp\n");
printf("\t.word %d\n", d);
}
printf("\tlda %u,0,2\n", ac);
return 1;
case T_CONSTANT:
load_constant(ac, v);
return 1;
case T_NAME:
printf("\tjsr @__const%u,0\n", ac);
if (is_bytepointer(n->type))
printf("\t.byteptr _%s+%u\n", namestr(n->snum), v);
else
printf("\t.word _%s+%u\n", namestr(n->snum), v);
return 1;
case T_LABEL:
printf("\tjsr @__const%u,0\n", ac);
if (is_bytepointer(n->type))
printf("\t.byteptr T%u+%u\n", n->val2, v);
else
printf("\t.word T%u+%u\n", n->val2, v);
return 1;
case T_NREF:/* Refs are always word at this point */
printf("\tjsr @__iconst%u\n", ac);
printf("\t.word _%s+%u\n", namestr(n->snum), v);
return 1;
case T_LBREF:
printf("\tjsr @__iconst%u\n", ac);
printf("\t.word T%u+%u\n", n->val2, v);
return 1;
}
printf(";couldnt shortcut %x\n", n->op);
return 0;
}
unsigned add_constant(unsigned ac, uint16_t v)
{
char buf[16];
if (ac == 0)
error("ac0");
if (v == 0)
return 1;
if (cpu >= 100) {
if (v <= 4)
printf("\tadi %u, %u\n", v, ac);
else if (v >= 0xFFFC && cpu >= 100)
printf("\tsbi %d, %u\n", -v, ac);
else
printf("\taddi %u, %u\n", v, ac);
return 1;
}
if (v == 0xFFFF) {
printf("\tneg %u,%u\n", ac, ac);
printf("\tcom %u,%u\n", ac, ac);
return 1;
}
if (v <= 3) {
sprintf(buf, "inc %u,%u", ac, ac);
repeated_op(v, buf);
} else if (gen_constant(0, v)) {
printf("\tadd 0,%u\n", ac);
} else {
printf("\tlda 0,2,1\n");
printf("\tadd 0,%u,skp\n", ac);
printf("\t.word %u\n", v & 0xFFFF);
}
return 1;
}