-
Notifications
You must be signed in to change notification settings - Fork 13
/
backend-ee200.c
1431 lines (1349 loc) · 29.7 KB
/
backend-ee200.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
/*
* Electrodata EE200
*
* Like an 8086 our register choices are mostly prescribed by the
* instruction set. In particular we have almost no indirect ops
* so have to use A and B for this. A, B and X have a good range of
* load and stores but X only 16bit ones. X is also used by the rather
* strange hybrid call stack/link. Y and Z are indexes but have no
* easy to way to load and save them directly. S is the stack pointer
*
* Everything is big endian.
*
* The Centurion CPU4/5 are basically the same but the CPU6 is quite
* different and will require some extra handling
* - Registers may not be accessible aliased to low RAM
* - Single reg ops have a second argument which specifies repeats
* (eg inc by 2 or shift by 5 are possible) and load with small
* constant
* - Unsigned 16->32bit MUL and 32->16 DIV instructions uses adjacent
* registers (or memory)
* - Direct constant loads to any reg via xfr
* - Proper PUSH/POP range instructions allow Y and Z to be pushed too
* - Block moves
* - Bignum maths (up to 128bit memory->memory including add sub
* mul (signed but buggy for negatives), div (signed) with or without
* remainder
* - some single word ops can also operate on memory
*
* Things to do are numerous
* - Float
* - Multiply and divide optimised forms via shifting
* - Rewrite some ops to use indirection like 6809 has
* - Tracking B and what A and X point to
* - register variable support for Y and Z
* - CCONLY tracking
* - Use X instead of A whenever X happens to hold the right stuff
* (and vice versa)
* - CPU6 support
*/
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include "compiler.h"
#include "backend.h"
#define BYTE(x) (((unsigned)(x)) & 0xFF)
#define WORD(x) (((unsigned)(x)) & 0xFFFF)
#define ARGBASE 4 /* Bytes between arguments and locals if no reg saves */
/*
* State for the current function
*/
static unsigned frame_len; /* Number of bytes of stack frame */
static unsigned sp; /* Stack pointer offset tracking */
static unsigned unreachable; /* Code is unreachable */
static unsigned argbase; /* Argument offset in current function */
/* 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");
}
}
void gen_prologue(const char *name)
{
unreachable = 0;
printf("_%s:\n", name);
}
/* Generate the stack frame */
void gen_frame(unsigned size, unsigned aframe)
{
frame_len = size;
argbase = ARGBASE;
printf("\tstx (-s)\n"); /* Stack X (our return addr) */
if (func_flags & F_REG(1)) {
printf("\txfr y,x\n");
printf("\tstx (-s)\n");
argbase += 2;
}
if (func_flags & F_REG(2)) {
printf("\txfr z,x\n");
printf("\tstx (-s)\n");
argbase += 2;
}
if (size) {
if (size <= 2) {
printf("\tdcr s\n");
if (size == 2)
printf("\tdcr s\n");
} else {
printf("\tlda %u\n", ((unsigned)-size) & 0xFFFF);
printf("\tadd a,s\n");
}
}
sp = 0;
}
void gen_epilogue(unsigned size, unsigned argsize)
{
if (sp != 0) {
error("sp");
}
if (unreachable == 1)
return;
if (size == 1)
printf("\tinr s\n");
else if (size == 2)
printf("\tinr s\n\tinr s\n");
else if (size) {
printf("\tlda %u\n", size);
printf("\tadd a,s\n");
}
if (func_flags & F_REG(2)) {
printf("\tlda (s+)\n");
printf("\txaz\n");
}
if (func_flags & F_REG(1)) {
printf("\tlda (s+)\n");
printf("\txay\n");
}
printf("\tldx (s+)\n");
printf("\trsr\n");
unreachable = 1;
}
void gen_label(const char *tail, unsigned n)
{
printf("L%d%s:\n", n, tail);
unreachable = 0;
}
unsigned gen_exit(const char *tail, unsigned n)
{
printf("\tjmp L%d%s\n", n, tail);
unreachable = 1;
return 0;
}
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("\tjz L%d%s\n", n, tail);
}
void gen_jtrue(const char *tail, unsigned n)
{
printf("\tjnz L%d%s\n", n, tail);
}
void gen_switch(unsigned n, unsigned type)
{
printf("\tldx Sw%u\n", n);
printf("\tjmp __switch");
helper_type(type, 0);
printf("\n");
unreachable = 1;
}
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)
{
printf("Sw%d_%d:\n", tag, entry);
unreachable = 0;
}
void gen_case_data(unsigned tag, unsigned entry)
{
printf("\t.word Sw%d_%d\n", tag, entry);
}
void gen_helpcall(struct node *n)
{
printf("\tjsr __");
}
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)
{
printf("\t.ds %d\n", value);
}
void gen_text_data(struct node *n)
{
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)
{
printf("\t.word _%s+%d\n", namestr(n->snum), WORD(n->value));
}
void gen_value(unsigned type, unsigned long value)
{
if (PTR(type)) {
printf("\t.word %u\n", (unsigned) value);
return;
}
switch (type) {
case CCHAR:
case UCHAR:
printf("\t.byte %u\n", (unsigned) value & 0xFF);
break;
case CSHORT:
case USHORT:
printf("\t.word %d\n", (unsigned) value & 0xFFFF);
break;
case CLONG:
case ULONG:
case FLOAT:
/* We are big endian */
printf("\t.word %d\n", (unsigned) ((value >> 16) & 0xFFFF));
printf("\t.word %d\n", (unsigned) (value & 0xFFFF));
break;
default:
error("unsuported type");
}
}
void gen_start(void)
{
printf("\t.setcpu %u\n", cpu);
printf("\t.code\n");
}
void gen_end(void)
{
}
void gen_tree(struct node *n)
{
codegen_lr(n);
printf(";\n");
}
/*
* Example size handling. In this case for a system that always
* pushes words.
*/
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;
}
/*
* Heuristic for guessing what to put on the right. Anything we can
* get into A without trashing B.
*/
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 these directly into a register */
if (op == T_NREF || op == T_LBREF)
return 10;
/* We can these into A in two */
if (op == T_LOCAL || op == T_ARGUMENT)
return 5;
/* We can usually get this into A */
if (op == T_LREF)
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
* 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;
/* 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) {
/* Arguments are pushed word sized, but big endian
so a byte argument is one byte on for an argument
but not a local */
if (r->op == T_ARGUMENT) {
r->value += argbase + frame_len;
if (nt == CCHAR || nt == UCHAR)
r->value++;
}
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;
/* Adjust arguments for big endian word
push of local */
if (nt == CCHAR || nt == UCHAR)
l->value++;
}
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 ||
(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(l), is_simple(r)); */
if (is_simple(l) > is_simple(r)) {
n->right = l;
n->left = r;
}
}
return n;
}
unsigned gen_push(struct node *n)
{
unsigned size = get_stack_size(n->type);
printf("\tstb (-s)\n");
if (size == 4) {
printf("\tlda __hireg\n");
printf("\tsta (-s)\n");
}
/* Our push will put the object on the stack, so account for it */
sp += get_stack_size(n->type);
return 1;
}
unsigned op_into_r(char r, struct node *n, unsigned s, const char *b, const char *w)
{
unsigned v = n->value;
/* Avoid long stuff for now */
if (s > 2)
return 0;
switch(n->op) {
case T_NAME:
printf("\tld%c _%s+%u\n", r, namestr(n->snum), v);
break;
case T_LABEL:
printf("\tld%c T%u+%u\n", r, n->val2, v);
break;
case T_ARGUMENT:
v += argbase + frame_len;
/* Word pushed big endian - so argument is byte after */
if (s == 1)
v++;
/* Fall through */
case T_LOCAL:
v += sp;
printf("\tld%c %u\n", r, v);
printf("\tadd s,%c\n", r);
break;
case T_CONSTANT:
/* Slightly messy as we want to use implicit form
for A as it is smaller */
if (r == 'a') {
if (s == 1 && (v & 0xFF) == 0) {
printf("\tclab\n");
break;
} else if (s == 2 && (v & 0xFFFF) == 0) {
printf("\tcla\n");
break;
}
} else if (s == 2 && (v & 0xFFFF) == 0) {
printf("\tclr %c\n", r);
break;
}
if (s == 1)
printf("\tld%cb %u\n", r, v & 0xFF);
else
printf("\tld%c %u\n", r, v & 0xFFFF);
break;
case T_LREF:
v += sp;
if (v < 128) {
if (s == 1)
printf("\tld%cb %u(s)\n", r, v);
else
printf("\tld%c %u(s)\n", r, v);
} else {
printf("\tld%c %u\n", r, v);
printf("\tadd s,%c\n", r);
if (s == 1)
printf("\tld%cb (%c)\n", r, r);
else
printf("\tld%c (%c)\n", r, r);
}
break;
case T_NREF:
if (s == 1)
printf("\tld%cb (_%s+%u)\n", r, namestr(n->snum), v);
else
printf("\tld%c (_%s+%u)\n", r, namestr(n->snum), v);
break;
case T_LBREF:
if (s == 1)
printf("\tld%cb (T%u+%u)\n", r, n->val2, v);
else
printf("\tld%c (T%u+%u)\n", r, n->val2, v);
break;
default:
return 0;
}
if (b)
printf("\t%s\n", (s == 1) ? b : w);
return 1;
}
unsigned can_op_into_r(char r, struct node *n, unsigned s)
{
/* Avoid long stuff for now */
if (s > 2)
return 0;
switch(n->op) {
case T_NAME:
case T_LABEL:
case T_ARGUMENT:
case T_LOCAL:
case T_CONSTANT:
case T_LREF:
case T_NREF:
case T_LBREF:
return 1;
}
return 0;
}
/* Anything goes providing B survives so we should look at folding more
complex expressions when possible */
unsigned op_into_a(struct node *n, unsigned s, const char *b, const char *w)
{
return op_into_r('a', n, s, b, w);
}
unsigned op_into_b(struct node *n, unsigned s, const char *b, const char *w)
{
return op_into_r('b', n, s, b, w);
}
unsigned op_into_x(struct node *n, unsigned s, const char *b, const char *w)
{
/* X only does word sized */
if (s == 1)
return 0;
return op_into_r('x', n, s, b, w);
}
/* Load a reference to an object into a register and return the reg,offset
pair to use */
int load_register(struct node *n, unsigned s, char *r)
{
unsigned v = n->value;
*r = 'x';
/* Avoid long stuff for now */
if (s != 2)
return -1;
/* Cases we can use off,s directly */
switch(n->op) {
case T_ARGUMENT:
v += argbase + frame_len;
/* Fall through */
case T_LOCAL:
v += sp;
if (v < 128) {
*r = 's';
return v;
}
break;
case T_LREF:
v += sp;
if (v < 128) {
printf("\tldx %u(s)\n", v);
return 0;
}
break;
}
/* Try and get it into X */
if (op_into_x(n, s, 0, 0) == 0)
return -1;
/* It's in X offset 0 */
return 0;
}
unsigned can_load_reg(struct node *n, unsigned s)
{
/* Avoid long stuff for now */
if (s > 2)
return 0;
switch(n->op) {
case T_NAME:
case T_LABEL:
case T_ARGUMENT:
case T_LOCAL:
case T_CONSTANT:
case T_LREF:
case T_NREF:
case T_LBREF:
return 1;
default:
return 0;
}
return 1;
}
unsigned condop(struct node *n, const char * o, const char *ou)
{
struct node *r = n->right;
unsigned s = get_size(r->type);
if (s == 4)
return 0;
if (r->type & UNSIGNED)
o = ou;
if (op_into_a(r, s, "sabb", "sab") == 0)
return 0;
printf("\tjsr __%s\n", ou);
n->flags |= ISBOOL;
return 1;
}
void repeated_op(unsigned n, const char *op)
{
while(n--)
printf("\t%s\n", op);
}
/*
* If possible turn this node into a direct access. We've already checked
* that the right hand side is suitable. If this returns 0 it will instead
* fall back to doing it stack based.
*/
unsigned gen_direct(struct node *n)
{
unsigned v;
unsigned s = get_size(n->type);
struct node *r = n->right;
unsigned nr = n->flags & NORETURN;
switch(n->op) {
/* Clean up is special and must be handled directly. It also has the
type of the function return so don't use that for the cleanup value
in n->right */
case T_CLEANUP:
v = r->value;
if (v == 1)
printf("\tinr s\n");
else {
printf("\tlda %u\n", v);
printf("\tadd a,s\n");
}
sp -= v;
return 1;
case T_EQ:
return op_into_a(r, s, "stab (b)", "sta (b)");
case T_PLUS:
v = r->value;
if (s <= 2 && r->op == T_CONSTANT && v <= 2) {
repeated_op(v, s == 1 ? "inrb bl" : "inr b");
return 1;
}
return op_into_a(r, s, "aabb", "aab");
case T_AND:
/* TODO: long cases */
if (r->op == T_CONSTANT && s == 2) {
v = r->value;
if (v == 0) {
printf("\tcla\n");
return 1;
}
if (v == 0xFFFF)
return 1;
}
return op_into_a(r, s, "nabb", "nab");
case T_OR:
if (r->op == T_CONSTANT && s == 2) {
v = r->value;
if (v == 0)
return 1;
if (v == 0xFFFF) {
printf("\tldb 0xFFFF\n");
return 1;
}
}
return op_into_a(r, s, "orib al,bl", "ori a,b");
case T_HAT:
if (r->op == T_CONSTANT && s == 2) {
v = r->value;
if (v == 0)
return 1;
if (v == 0xFFFF) {
printf("\tivr b\n");
return 1;
}
}
return op_into_a(r, s, "oreb al,bl", "ore a,b");
case T_MINUS: /* We have a reverse subtract so just do the
simple case */
if (r->op == T_CONSTANT) {
v = r->value;
if (s == 1) {
if (v <= 2) {
repeated_op(v, "dcrb b");
return 1;
}
printf("\tldab %u\n", (-v) & 0xFF);
printf("\taabb\n");
return 1;
}
if (s == 2) {
if (v <= 2) {
repeated_op(v, "dcr b");
return 1;
}
printf("\tlda %u\n", (-v) & 0xFFFF);
printf("\taab\n");
return 1;
}
}
/* Our subtract is dst = src - dst, but at this point we
have the left side in B */
if (op_into_a(r, s, NULL, NULL)) {
/* We now want to do B - A into B */
/* A = B - A */
printf("\tsub b,a\n");
/* And into B */
printf("\txab\n");
return 1;
}
break;
case T_PLUSEQ:
if (op_into_a(r, s, NULL, NULL)) {
if (s == 1) {
printf("\txfr b,x\n");
printf("\tldbb (x)\n");
printf("\taabb\n");
printf("\tstbb (x)\n");
return 1;
}
if (s == 2) {
printf("\txfr b,x\n");
printf("\tldb (x)\n");
printf("\taab\n");
printf("\tstb (x)\n");
return 1;
}
}
break;
case T_PLUSPLUS:
/* R is alway a constant */
if (op_into_a(r, s, NULL, NULL)) {
if (s == 1) {
printf("\txfr b,x\n");
printf("\tldbb (x)\n");
if (!nr)
printf("\tstb (-s)\n");
printf("\taabb\n");
printf("\tstb (x)\n");
if (!nr)
printf("\tldb (s+)\n");
return 1;
}
if (s == 2) {
printf("\txfr b,x\n");
printf("\tlda (x)\n");
if (!nr)
printf("\tsta (-s)\n");
printf("\taab\n");
printf("\tsta (x)\n");
if (!nr)
printf("\tlda (s+)\n");
return 1;
}
}
break;
case T_MINUSEQ:
/* We have a reverse subtract so we need the value in B */
if (can_op_into_r('a', r, s)) {
printf("\txfr b,x\n");
op_into_b(r, s, NULL, NULL);
if (s == 1) {
printf("\tldab (x)\n");
printf("\tsabb\n");
printf("\tstbb (x)\n");
return 1;
}
if (s == 2) {
printf("\tlda (x)\n");
printf("\tsab\n");
printf("\tstb (x)\n");
return 1;
}
}
break;
case T_MINUSMINUS:
/* r is always constant but might be float or oversize */
if (can_op_into_r('a', r, s)) {
printf("\txfr b,x\n");
op_into_b(r, s, NULL, NULL);
if (s == 1) {
printf("\tldab (x)\n");
if (!nr)
printf("\tsta (-s)\n");
printf("\tsabb\n");
printf("\tstbb (x)\n");
if (!nr)
printf("\tldb (s+)\n");
return 1;
}
if (s == 2) {
printf("\tlda (x)\n");
if (!nr)
printf("\tsta (-s)\n");
printf("\tsab\n");
printf("\tstb (x)\n");
if (!nr)
printf("\tldb (s+)\n");
return 1;
}
}
break;
/* TODO - optimized constant forms
case T_ANDEQ:
case T_OREQ:
case T_HATEQ:
*/
case T_LTLT:
if (r->op == T_CONSTANT && s <= 2) {
v = r->value;
if (v >= s * 8) {
if (s == 1)
printf("\tclrb bl\n");
else
printf("\tclr b\n");
return 1;
}
if (v >= 8) {
printf("\txfrb bl,bh\n");
printf("\tclrb bl\n");
v -= 8;
}
if (v < 3 || opt > 1) {
repeated_op(v, s == 1 ? "slrb b" : "slr b");
return 1;
}
printf("\tjsr __shl%u\n", v);
return 1;
}
return 0;
case T_GTGT:
if (r->op == T_CONSTANT && s <= 2) {
v = r->value & 15;
if (v == 0)
return 1;
/* No right shift unsigned */
if (n->type & UNSIGNED) {
if (v >= 8) {
printf("\txfrb ah,al\n");
printf("\tclrb ah\n");
v -= 8;
/* High bit is clear so signed is fine */
if (v > 2)
printf("\tjsr __shr%u\n", v);
else
repeated_op(v, s == 1 ? "srrb b" : "srr b");
} else {
/* Do one bit by hand then helper */
printf("\trl\n");
printf("\trrr b\n");
v--;
if (v > 2)
printf("\tjsr __shr%u\n", v);
else
repeated_op(v, s == 1 ? "srrb b" : "srr b");
}
return 0;
} else {
if (v >= s * 8) {
if (s == 1)
printf("\tclrb bl\n");
else
printf("\tclr b\n");
return 1;
}
if (v >= 8) {
printf("\tjsr __shr8\n");
v -= 8;
}
if (v < 3 || opt > 1) {
repeated_op(v, s == 1 ? "srrb b" : "srr b");
return 1;
}
printf("\tjsr __shr%u\n", v);
return 1;
}
}
return 0;
/* These come out reversed due to the way SAB works */
case T_EQEQ:
return condop(n, "cceq", "cceq");
case T_BANGEQ:
return condop(n, "ccne", "ccne");
case T_GT:
return condop(n, "cclt", "ccltu");
case T_LTEQ:
return condop(n, "ccgteq", "ccgtequ");
case T_LT:
return condop(n, "ccgt", "ccgtu");
case T_GTEQ:
return condop(n, "cclteq", "ccltequ");
}
return 0;
}
/*
* Allow the code generator to shortcut the generation of the argument
* of a single argument operator (for example to shortcut constant cases
* or simple name loads that can be done better directly)
*/
unsigned gen_uni_direct(struct node *n)
{
return 0;
}
unsigned short_op(struct node *n, unsigned s, const char *opb, const char *op)
{
char r;
int off;
if (s > 2)
return 0;
if (s == 1)
op = opb;
codegen_lr(n->right);
off = load_register(n->left, 2, &r);
if (s == 1) {
printf("\tldab %u(%c)\n", off, r);
printf("\t%s\n", op);
printf("\tstbb %u(%c)\n", off, r);