-
Notifications
You must be signed in to change notification settings - Fork 15
/
be-codegen-6800.c
1928 lines (1868 loc) · 44.1 KB
/
be-codegen-6800.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
/*
* 6800: original in the line of processors ending with 68HC11
* - Only has 8bit operations
* - Cannot push x (hack to pop it)
* - Cannot move between accumulators and X except via memory
* - Can copy X to and from S but can only move to/from accumulator
* via memory
* - Some useful flag behaviour on late devices is not present.
*
* 6803:
* - Adds 16bit operations
* - Adds ABX
* - Can push or pull X
*
* 6303: like 6803
* - Adds some interesting bit ops
* - Adds XGDX
*
* 68HC11: like 6803
* - Adds a bunch of bitops, branch on bit and other stuff
* - Adds a Y register
* - Has XGDX and XGDY
*
* TODO; swap sub around for non direct cases ?
* cleanup x++ and ++x style ops for 6800 in particular
* use ldx _foo inx stx _foo for 1-4 byte 16bit
* spot the common *x++= and blah *x++ cases
* and do these for small values directly as
* ldx _foo inx stx _foo [dex] stb ,x etc
*/
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "compiler.h"
#include "backend.h"
#include "backend-6800.h"
/*
* Helpers for code generation and tracking
*/
/*
* Size handling
*/
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;
}
unsigned get_stack_size(unsigned t)
{
return get_size(t);
}
void repeated_op(unsigned n, const char *op)
{
while(n--)
printf("\t%s\n", op);
}
/* 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;
}
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;
}
/*
* Stick simple things on the right. Elsewhere the code assumes that
* 9 or higher score means we can do it without eating X
*/
static unsigned is_simple(struct node *n)
{
unsigned op = n->op;
/* We can load these directly */
if (op == T_CONSTANT)
return 10;
/* These are as easy but we also have some helpers that benefit
most from right constant */
if (op == T_NAME || op == T_LABEL)
return 9;
if (op == T_LBREF || op == T_NREF)
return 9;
if (op == T_ARGUMENT || op == T_LOCAL)
return 9;
/* Harder as uses X */
if (op == T_LREF) {
/* 6809 has ,S ops */
if (cpu == 6809)
return 9;
return 5;
}
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(struct node *n)
{
register struct node *r = n->right;
register struct node *l = n->left;
register unsigned op = n->op;
unsigned nt = n->type;
unsigned off;
/* 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);
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;
}
/* Merge offset to object into a single direct reference */
if (op == T_PLUS && r->op == T_CONSTANT &&
(l->op == T_LOCAL || l->op == T_NAME || l->op == T_LABEL || l->op == T_ARGUMENT)) {
/* We don't care if the right offset is 16bit or 32 as we've
got 16bit pointers */
printf(";merge right %x %lu+%lu\n",
op, l->value, r->value);
l->value += r->value;
free_node(r);
free_node(n);
return l;
}
/* Label array referencing for locals to help the 6800 */
if (optsize && !cpu_has_d) {
if (op == T_PLUS) {
off = 0;
if (l->op == T_LOCAL || l->op == T_ARGUMENT) {
if (r->op == T_ARGUMENT)
off = argbase + frame_len;
squash_left(n, T_LPLUS);
n->value += off;
return n;
}
}
}
/* Turn a deref of an offset to an object into a single op so we can
generate a single lda offset,x in the code generator. This happens
in some array dereferencing and especially struct pointer access */
if (op == T_DEREF || op == T_DEREFPLUS) {
if (op == T_DEREF)
n->value = 0; /* So we can treat deref/derefplus together */
if (r->op == T_PLUS) {
off = n->value + r->right->value;
if (r->right->op == T_CONSTANT && off < 253) {
n->op = T_DEREFPLUS;
free_node(r->right);
n->right = r->left;
n->value = off;
free_node(r);
/* We might then rewrite this again */
return gen_rewrite_node(n);
}
}
}
if (op == T_EQ || op == T_EQPLUS) {
if (op == T_EQ)
n->value = 0; /* So we can treat deref/derefplus together */
if (l->op == T_PLUS) {
off = n->value + l->right->value;
if (l->right->op == T_CONSTANT && off < 253) {
n->op = T_EQPLUS;
free_node(l->right);
n->left = l->left;
n->value = off;
free_node(l);
/* We might then rewrite this again */
return gen_rewrite_node(n);
}
}
}
/* regptr++ The size will always be the true size for ++ and const */
if (op == T_DEREF && r->op == T_PLUSPLUS && r->left->op == T_REG)
{
n->op = T_RDEREFPLUS;
n->value = r->left->value;
free_node(r->left);
free_node(r->right);
free_node(r);
n->right = NULL;
return n;
}
/* *regptr++ = again the size will be const and right */
if (op == T_EQ && l->op == T_PLUSPLUS && l->left->op == T_REG)
{
n->op = T_REQPLUS;
n->value = l->left->value;
free_node(l->left);
free_node(l->right);
free_node(l);
n->left = NULL;
return n;
}
/* *(reg + offset). Optimize this specially as it occurs a lot */
if (op == T_DEREF && r->op == T_PLUS && r->left->op == T_RREF &&
r->right->op == T_CONSTANT) {
n->op = T_RDEREF;
n->right = NULL;
n->val2 = r->right->value; /* Offset to add */
n->value = r->left->value; /* Register number */
free_node(r->right); /* Discard constant */
free_node(r->left); /* Discard T_REG */
free_node(r); /* Discsrd plus */
return n;
}
/* *regptr */
if (op == T_DEREF && r->op == T_RREF) {
n->op = T_RDEREF;
n->right = NULL;
n->val2 = 0;
n->value = r->value;
free_node(r);
return n;
}
/* *(reg + offset) = Optimize this specially as it occurs a lot */
if (op == T_EQ && l->op == T_PLUS && l->left->op == T_RREF &&
l->right->op == T_CONSTANT) {
n->op = T_REQ;
n->val2 = l->right->value; /* Offset to add */
n->value = l->left->value; /* Register number */
free_node(l->right); /* Discard constant */
free_node(l->left); /* Discard T_REG */
free_node(l); /* Discsrd plus */
n->left = NULL;
return n;
}
/* *regptr = */
if (op == T_EQ && l->op == T_RREF) {
n->op = T_REQ;
n->val2 = 0;
n->value = l->value;
n->left = NULL;
free_node(l);
return n;
}
if ((op == T_DEREF || op == T_DEREFPLUS) && r->op == T_LREF) {
/* At this point r->value is the offset for the local */
/* n->value is the offset for the ptr load */
r->val2 = n->value; /* Save the offset so it is squashed in */
squash_right(n, T_LDEREF); /* n->value becomes the local ref */
return n;
}
if ((op == T_EQ || op == T_EQPLUS) && l->op == T_LREF) {
/* At this point r->value is the offset for the local */
/* n->value is the offset for the ptr load */
l->val2 = n->value; /* Save the offset so it is squashed in */
squash_left(n, T_LEQ); /* n->value becomes the local ref */
return n;
}
/* Optimizations for 6809 indirect addressing */
if (cpu_is_09 && n->value == 0 && (op == T_DEREF || op == T_DEREFPLUS) && r->op == T_NREF) {
/* At this point r->value is the offset for the local */
/* n->value is the offset for the ptr load */
squash_right(n, T_NDEREF); /* n->value becomes the local ref */
return n;
}
if (cpu_is_09 && n->value == 0 && (op == T_DEREF || op == T_DEREFPLUS) && r->op == T_LBREF) {
/* At this point r->value is the offset for the local */
/* n->value is the offset for the ptr load */
squash_right(n, T_LBDEREF); /* n->value becomes the local ref */
return n;
}
if (cpu_is_09 && n->value == 0 && (op == T_EQ || op == T_EQPLUS) && l->op == T_NREF) {
squash_left(n, T_NEQ); /* n->value becomes the local ref */
return n;
}
if (cpu_is_09 && n->value == 0 && (op == T_EQ || op == T_EQPLUS) && l->op == T_LBREF) {
l->val2 = n->value; /* Save the offset so it is squashed in */
squash_left(n, T_LBEQ); /* n->value becomes the local ref */
return n;
}
/* Rewrite references into a load operation */
/* For now leave long types out of this unless we have a Y register as the @hireg forms
are more complex */
if (cpu_has_y || 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 (r->op == T_RREF) {
squash_right(n, T_RDEREF);
n->val2 = 0;
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;
}
}
}
/* Commutive operations. We can swap the sides over on these */
/* We want to put things we can easily use on the right so that
we have the best chance of being able to do operations without
having to stack values */
if (op == T_AND || op == T_OR || op == T_HAT || op == T_STAR || op == T_PLUS) {
if (is_simple(n->left) > is_simple(n->right)) {
n->right = l;
n->left = r;
}
}
return n;
}
/* Negate the 16bit working value */
static void negate_d(void)
{
unsigned v;
puts("\tnega\n\tnegb\n\tsbca #0\n");
if (d_valid) {
v = (a_val << 8) + b_val;
v = WORD(-v);
modify_a(v >> 8);
modify_b(v);
}
}
/*
* Perform an optimized 32bit constant add
*
* For stuff using @hireg we try and optimize the common cases of adding
* smaller constants. For stuff where we add high bits only shortcut
* some of the work.
*
* Care is needed as add_d_const() will not leave carry valid if asked
* to add 0, hence the use of op.
*/
static unsigned gen_add32(unsigned long v)
{
char *op = "add";
unsigned l = WORD(v);
unsigned h = WORD(v >> 16);
if (v == 0)
return 1;
if (cpu_has_y) {
if (l) {
add_d_const(l);
swap_d_y();
printf("\tadcb #%u\n", h & 0xFF);
printf("\tadca #%u\n", (h >> 8) & 0xFF);
swap_d_y();
} else if (cpu_has_lea)
printf("\tleay %u,y\n", h);
else {
swap_d_y();
printf("\taddd #%u\n", h);
swap_d_y();
}
return 1;
}
if (l) {
add_d_const(l);
op = "adc";
if (h == 0) {
l = ++label;
printf("\tbcc X%u\n\tinc @hireg+1\n\tbne X%u\n\tinc @hireg\nX%u:\n", l, l, l);
return 1;
}
}
puts("\tpsha");
if (h & 0xFF) {
printf("\tldaa @hireg+1\n\t%sa #%u\n\tstaa @hireg+1\n", op, h & 0xFF);
op = "adc";
if (!(h & 0xFF00)) {
l = ++label;
printf("\tbcc X%u\n\tinc @hireg\nX%u:\n", l, l);
return 1;
}
}
printf("\tldaa @hireg\n\t%sa #%u\n\tstaa @hireg\n",
op, (h >> 8) & 0xFF);
puts("\tpula");
return 1;
}
/*
* 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 s = get_size(n->type);
struct node *r = n->right;
unsigned off;
uint16_t v;
uint16_t hv;
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:
sp -= r->value;
if (cpu_has_d || n->val2) /* Varargs */
adjust_s(r->value, (func_flags & F_VOIDRET) ? 0 : 1);
return 1;
case T_EQ:
case T_EQPLUS:
off = n->value;
v = r->value;
/* If the left side was not simple then try and shortcut the
right side */
if (cpu_has_xgdx && r->op == T_CONSTANT) {
swap_d_x();
if (s == 1) {
load_b_const(v);
op8_on_ptr("st", off);
return 1;
} else if (s == 2) {
load_d_const(v);
op16d_on_ptr("st", "st", off);
return 1;
} else if (s == 4 && cpu_has_y) {
load_d_const(v);
printf("\tldy #%u\n", (unsigned)(r->value >> 16));
op32d_on_ptr("st", "st", off);
return 1;
}
}
/* TODO: for many CPU variants we need a list of things we
* can short load into D without touching X and to use them
* here
*/
break;
case T_PLUS:
/* So we can track this common case later */
/* TODO: if the low word is zero or low 24 bits are 0 we can generate stuff like
leay %d,y */
if (r->op == T_CONSTANT && r->type != FLOAT) {
if (s == 4)
return gen_add32(r->value);
if (s == 2) {
add_d_const(r->value);
return 1;
}
if (s == 1) {
add_b_const(r->value);
return 1;
}
}
if (s == 2 && r->op == T_LOCAL) {
v = r->value;
if (!cpu_is_09)
v++;
if (cpu_has_d)
puts("\tsts @tmp\n\taddd @tmp");
else
puts("\tsts @tmp\n\taddb @tmp+1\n\tadca @tmp");
invalidate_d();
add_d_const(v);
return 1;
}
return write_opd(r, "add", "adc", 0);
case T_MINUS:
if (r->op == T_CONSTANT && r->type != FLOAT) {
if (s == 4 && cpu_has_y)
return gen_add32(-r->value);
if (s == 2) {
add_d_const(-r->value);
return 1;
}
if (s == 1) {
add_b_const(-r->value);
b_val -= r->value;
return 1;
}
}
return write_opd(r, "sub", "sbc", 0);
case T_AND:
if (r->op == T_CONSTANT) { /* No need to type check - canno tbe float */
v = r->value & 0xFFFF;
hv = r->value >> 16;
/* Check if it makes sense to do long inline. Only
do so if we have Y or the upper half is trivial */
if (s == 4 && !cpu_has_y && hv && hv != 0xFFFF)
return 0;
if ((v & 0xFF) != 0xFF) {
if (v & 0xFF)
printf("\tandb #%u\n", v & 0xFF);
else
puts("\tclrb");
modify_b(b_val & v);
}
if (s >= 2) {
v >>= 8;
if (v != 0xFF) {
if (v)
printf("\tanda #%u\n", v);
else
puts("\tclra");
}
modify_a(a_val & v);
}
if (s == 4) {
if (hv == 0x0000) {
if (cpu_has_y)
puts("\tldy #0");
else {
puts("\tclr @hireg\n\tclr @hireg+1");
}
} else if (hv != 0xFFFF) {
swap_d_y();
if (hv & 0xFF)
printf("\tandb #%u\n", hv & 0xFF);
else
puts("\tclrb");
hv >>= 8;
if (hv & 0xFF)
printf("\tanda #%u\n", hv & 0xFF);
else
puts("\tclra");
swap_d_y();
}
}
return 1;
}
return write_op(r, "and", "and", 0);
case T_OR:
if (r->op == T_CONSTANT) {
v = r->value & 0xFFFF;
hv = r->value >> 16;
/* Check if it makes sense to do long inline. Only
do so if we have Y or the upper half is trivial */
if (s == 4 && !cpu_has_y && hv)
return 0;
if (v & 0xFF) {
printf("\torb #%u\n", v & 0xFF);
modify_b(b_val | v);
}
if (s >= 2) {
v >>= 8;
if (v)
printf("\tora #%u\n", v);
modify_a(a_val | v);
}
if (s == 4) {
if (hv == 0xFFFF)
puts("\tldy #0xFFFF");
else if (hv) {
swap_d_y();
if (hv & 0xFF)
printf("\torb #%u\n", hv & 0xFF);
hv >>= 8;
if (hv & 0xFF)
printf("\tora #%u\n", hv & 0xFF);
swap_d_y();
}
}
return 1;
}
return write_op(r, "or", "or", 0);
case T_HAT:
if (r->op == T_CONSTANT) {
v = r->value & 0xFFFF;
hv = r->value >> 16;
if (s == 4 && !cpu_has_y && hv && hv != 0xFFFF)
return 0;
if ((v & 0xFF) == 0xFF)
puts("\tcomb");
else if (v & 0xFF)
printf("\teorb #%u\n", v & 0xFF);
modify_b(b_val ^ v);
if (s >= 2) {
v >>= 8;
if (v == 0xFF)
puts("\tcoma");
else if (v & 0xFF)
printf("\teora #%u\n", v);
modify_a(a_val ^ v);
}
if (s == 4) {
if (hv == 0xFFFF && !cpu_has_y) {
puts("\tcom @hireg\n\tcom @hireg+1");
} else if (hv) {
swap_d_y();
if ((hv & 0xFF) == 0xFF)
puts("\tcomb");
else if (hv & 0xFF)
printf("\teorb #%u\n", hv & 0xFF);
hv >>= 8;
if (hv == 0xFF)
puts("\tcoma");
else if (hv & 0xFF)
printf("\teora #%u\n", hv);
swap_d_y();
}
}
return 1;
}
return write_op(r, "eor", "eor", 0);
case T_LTLT:
return left_shift(n);
case T_GTGT:
return right_shift(n);
case T_EQEQ:
return cmp_direct(n, "booleq", "booleq");
case T_BANGEQ:
return cmp_direct(n, "boolne", "boolne");
case T_LT:
return cmp_direct(n, "boolult", "boollt");
case T_GT:
return cmp_direct(n, "boolugt", "boolgt");
case T_LTEQ:
return cmp_direct(n, "boolule", "boolle");
case T_GTEQ:
return cmp_direct(n, "booluge", "boolge");
case T_SLASH:
if (r->op == T_CONSTANT) {
if (s <= 2 && gen_fast_div(s, r->value, n->type))
return 1;
}
break;
case T_STAR:
if (r->op == T_CONSTANT) {
v = r->value;
if (s <= 2)
return gen_fast_mul(s, v);
}
break;
}
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)
{
unsigned s = get_size(n->type);
struct node *r = n->right;
unsigned nr = n->flags & NORETURN;
unsigned off;
switch(n->op) {
case T_LEQ:
/* We have a specific optimization case that occurs a lot
*auto = 0, that we can optimize nicely */
if (r->op == T_CONSTANT && r->value == 0 && nr) {
if (cpu_is_09) {
off = n->val2 + sp;
while(s--)
printf("\tclr [%u,s]\n", off++);
return 1;
}
/* Offset of pointer in local */
/* val2 is the local offset, value the data offset */
off = make_local_ptr(n->value, 256 - s);
/* off,X is now the pointer */
printf("\tldx %u,x\n", off);
invalidate_x();
uniop_on_ptr("clr", n->val2, s);
return 1;
}
return 0;
case T_LBEQ:
if (r->op == T_CONSTANT && r->value == 0 && nr) {
if (s == 1 && nr) {
printf("\tclr [T%u+%u]\n", n->val2, (unsigned)n->value);
return 1;
}
}
return 0;
case T_NEQ:
if (r->op == T_CONSTANT && r->value == 0 && nr) {
if (s == 1 && nr) {
printf("\tclr [_%s+%u]\n", namestr(n->snum), (unsigned)n->value);
return 1;
}
}
return 0;
/* Writes of 0 to an object we can use clr for providing the
result is not then re-used */
case T_LSTORE:
case T_LBSTORE:
case T_NSTORE:
/* Optimizations for constants */
if (nr && r->op == T_CONSTANT && r->value == 0) {
if (write_uni_op(n, "clr", 0)) {
invalidate_d();
return 1;
}
}
return 0;
}
return 0;
}
/*
* Try and build an op where we load X with the pointer,
* AB with the data and call a helper. Some of these may also
* benefit from inline forms later. 32bit also works as the
* value ends up in @hireg|Y/AB which is all safe from the load of
* the X pointer.
*/
unsigned do_xptrop(struct node *n, const char *op, unsigned off)
{
unsigned size;
switch(n->op) {
case T_ANDEQ:
op_on_ptr(n, "and", off);
break;
case T_OREQ:
op_on_ptr(n, "or", off);
break;
case T_HATEQ:
op_on_ptr(n, "eor", off);
break;
case T_PLUSEQ:
opd_on_ptr(n, "add", "adc", off);
break;
case T_MINUSEQ:
/* We want to subtract D *from* .X. Negate D and add
as this seems the cheapest approach */
size = get_size(n->type);
if (size == 1) {
puts("\tnegb");
opd_on_ptr(n, "add", "adc", off);
break;
}
if (size == 2) {
negate_d();
opd_on_ptr(n, "add", "adc", off);
break;
}
helper(n, "negate");
opd_on_ptr(n, "add", "adc", off);
break;
case T_STAREQ:
/* If we have D we have mul */
size = get_size(n->type);
if (size == 1 && cpu_has_d) {
printf("\tlda %u,x\n\tmul\n\tstb %u,x\n", off, off);
return 1;
}
/* Fall through */
/* TODO:
MINUSEQ
PLUSPLUS
MINUSMINUS
shift prob not */
default:
/* For the cases we can't inline we may have to adjust X because
we can't propogate the offset nicely into the helper. On 6809 this
is easy, on 6800 it's ugly and we should have a version of the helper
that adds a const then falls into the main implementation */
if (off) {
if (cpu_has_lea)
printf("\tleax %u,x\n", off);
else if (cpu_has_xgdx)
printf("\txgdx\n\taddd #%u\n\txgdx\n", off);
else {
if (off > 0 && off <= 5 + opt)
repeated_op(off, "inx");
else if (off < 0 && off >= -5 - opt)
repeated_op(-off, "dex");
else {
printf("\tjsr __addxconst\n\t.word %u\n", off);
}
}
invalidate_x();
}
helper_s(n, op);
return 1;
}
/* Stuff D back into ,X */
opd_on_ptr(n, "st", "st", off);
return 1;
}
static unsigned deref_op(unsigned op)
{
switch(op) {
case T_NAME:
return T_NREF;
case T_LOCAL:
return T_LREF;
case T_LABEL:
return T_LBREF;
}
return 0;
}
unsigned write_xsimple(struct node *n, unsigned via_ptr)
{
const char *op, *op2;
unsigned off = 0;
unsigned op16 = 0;
struct node *l = n->left;
struct node *r = n->right;
unsigned top = n->op;
switch(n->op) {
case T_ANDEQ:
op = op2 = "and";
break;
case T_OREQ:
op = op2 = "or";
break;
case T_HATEQ:
op = op2 = "eor";
break;
case T_PLUSEQ:
op = "add";
op2 = "adc";
op16 = 1;
break;
case T_MINUSEQ:
op = "sub";
op2 = "sbc";
op16 = 1;
break;
default:
return 0;
}
top = deref_op(l->op);
if (via_ptr || top == 0) {
off = load_x_with(l, 0);
opd_on_ptr(n, "ld", "ld", off);
via_ptr = 1;
} else {
/* Not sure we should encourage this kind of behaviour ;) */
/* TODO: turn this simple/simple stuff into a tree rewrite */
l->op = top;
l->type = r->type;
write_opd(l, "ld", "ld", off);
}
if (op16)
write_opd(r, op, op2, 0);
else
write_op(r, op, op2, 0);
if (via_ptr)
opd_on_ptr(n, "st", "st", off);
else
write_opd(l, "st", "st", off);
set_d_node(l);
return 1;
}
/* TODO: 6809 could index locals via S so if n->left is LOCAL or
ARGUMENT we can special case it */
unsigned do_xeqop(struct node *n, const char *op)
{
unsigned off;
struct node *l = n->left;
struct node *r = n->right;
/* Float always goes via the helper */
if (n->type == FLOAT)
return 0;
/* Handle simpler cases of -= the other way around */
/* Avoid cases that also need X */
if (is_simple(r) > 8 && get_size(n->type) <= 2) {
if (is_simple(l)) {
if (write_xsimple(n, 0))
return 1;
} else if (can_load_r_with(l, 0)) {
if (write_xsimple(n, 1))
return 1;
}
}
if (!can_load_r_with(n->left, 0)) {
printf(";can't load x %u\n", n->left->op);
/* Compute the left side and stack it */
codegen_lr(n->left);
gen_push(n->left);
/* Get D right, then pull the pointer into X */
codegen_lr(n->right);
pop_x();
sp -= 2; /* Pulling the pointer back off */
off = 0;
/* and drop into he helper */
} else {
printf("; right into D path\n");
/* Get the value part into AB */
codegen_lr(n->right);
/* Load X (lval of the eq op) up (doesn't disturb AB) */
off = load_x_with(n->left, 0);
}
/* Things we can then inline */
if (do_xptrop(n, op, off) == 0)
error("xptrop");
set_d_node_ptr(n->left);
return 1;
}
unsigned do_stkeqop(struct node *n, const char *op)
{
if (n->type == FLOAT)
return 0;
if (cpu_is_09)
puts("\tpuls x");
else if (cpu_has_pshx)
puts("\tpulx");
else /** Fun fun. Fake pulx on a 6800 */
puts("\ttsx\n\tldx ,x\n\tins\n\tins");
invalidate_x();
return do_xptrop(n, op, 0);
}
/*
* Things we can try and do as a direct memory op for bytes
*/