-
Notifications
You must be signed in to change notification settings - Fork 15
/
be-code-6800.c
1213 lines (1116 loc) · 24.3 KB
/
be-code-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
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "compiler.h"
#include "backend.h"
#include "backend-6800.h"
unsigned label; /* Used to hand out local labels of the form X%u */
/*
* Fix up weirdness in the asm formats.
*/
/* 16bit constant load */
void load_d_const(uint16_t n)
{
register unsigned hi,lo;
lo = n & 0xFF;
hi = n >> 8;
/* printf(";want %04X have %02X:%02X val %d %d\n",
n, a_val, b_val, a_valid, b_valid); */
if (cpu_has_d) {
if (n == 0) {
if (!a_valid || a_val)
puts("\tclra");
if (!b_valid || b_val)
puts("\tclrb");
} else if (!a_valid || !b_valid || a_val != hi || b_val != lo) {
printf("\tldd #%u\n", n);
}
} else {
/* TODO: track AB here and see if we can use existing values */
lo = n & 0xFF;
hi = n >> 8;
if (a_valid == 0 || hi != a_val) {
if (hi == 0)
puts("\tclra");
else if (b_valid && hi == b_val)
puts("\ttba");
else
printf("\tldaa #%d\n", hi);
}
if (b_valid == 0 || lo != b_val) {
if (lo == 0)
puts("\tclrb");
else if (lo == hi)
puts("\ttab");
else
printf("\tldab #%d\n", lo);
}
}
a_valid = 1; /* We know the byte values */
b_valid = 1;
d_valid = 0; /* No longer an object reference */
a_val = hi;
b_val = lo;
}
void load_a_const(register uint8_t n)
{
if (a_valid && n == a_val)
return;
if (n == 0)
puts("\tclra");
else if (b_valid && n == b_val)
puts("\ttba");
else
printf("\tldaa #%u\n", n & 0xFF);
a_valid = 1;
a_val = n;
d_valid = 0;
}
void load_b_const(register uint8_t n)
{
if (b_valid && n == b_val)
return;
if (n == 0)
puts("\tclrb");
else if (a_valid && n == a_val)
puts("\ttab");
else
printf("\tldab #%u\n", n & 0xFF);
b_valid = 1;
b_val = n;
d_valid = 0;
}
void add_d_const(register uint16_t n)
{
if (n == 0)
return;
/* TODO: can do better in terms of obj/offset but not clear it is
that useful */
d_valid = 0;
if (cpu_has_d)
printf("\taddd #%u\n", n);
else {
if (n & 0xFF) {
printf("\taddb #%u\n", n & 0xFF);
printf("\tadca #%u\n", n >> 8);
} else
printf("\tadda #%u\n", n >> 8);
}
if (b_val + (n & 0xFF) < b_val)
a_val += (n >> 8) + 1;
else
a_val += (n >> 8);
b_val += (n & 0xFF);
}
void add_b_const(register uint8_t n)
{
if (n == 0)
return;
printf("\taddb #%u\n", n & 0xFF);
b_val += n;
d_valid = 0;
}
void load_a_b(void)
{
puts("\ttba");
a_val = b_val;
a_valid = b_valid;
d_valid = 0;
}
void load_b_a(void)
{
puts("\ttab");
b_val = a_val;
b_valid = a_valid;
d_valid = 0;
}
void move_s_d(void)
{
puts("\tsts @tmp");
if (cpu_has_d)
puts("\tldd @tmp");
else
puts("\tldaa @tmp\n\tldab @tmp+1");
invalidate_work();
}
void move_d_s(void)
{
if (cpu_has_d)
puts("\tstd @tmp");
else
puts("\tstaa @tmp\n\tstab @tmp+1");
puts("\tlds @tmp");
}
void swap_d_y(void)
{
puts("\txgdy");
}
void swap_d_x(void)
{
/* Should really track on the exchange later */
puts("\txgdx");
invalidate_work();
invalidate_x();
}
/* Get D into X (may trash D) */
void make_x_d(void)
{
if (cpu_has_xgdx)
swap_d_x();
else {
if (cpu_has_d)
puts("\tstd @tmp\n\tldx @tmp");
else
puts("\tstaa @tmp\n\tstab @tmp+1\n\tldx @tmp");
}
/* TODO: d -> x see if we know d type */
invalidate_x();
}
/* Get X into D (may trash X) */
void make_d_x(void)
{
if (cpu_has_xgdx)
swap_d_x();
else {
if (cpu_has_d)
puts("\tstx @tmp\n\tldd @tmp");
else
puts("\tstx @tmp\n\tldaa @tmp\n\tldab @tmp+1");
}
/* TODO: x->d see if we know x type */
invalidate_work();
}
void pop_x(void)
{
/* Must remember this trashes X, or could make it smart
when we track and use offsets of current X then ins ins */
/* Easier said than done on a 6800 */
if (cpu_has_pshx)
puts("\tpulx");
else
puts("\ttsx\n\tldx ,x\n\tins\n\tins");
invalidate_x();
}
/*
* There are multiple strategies depnding on chip features
* available.
*/
void adjust_s(register int n, unsigned save_d)
{
register unsigned abxcost = 3 + 2 * save_d + n / 255;
register unsigned hardcost;
unsigned cost;
if (cpu_has_d)
hardcost = 15 + 4 * save_d;
else
hardcost = 18 + 2 * save_d;
cost = hardcost;
/* Processors with XGDX always have PULX so we use whichever is
the shorter of the two approaches */
if (cpu_has_xgdx) {
if (n > 14 || n < -14) {
printf("\ttsx\n\txgdx\n\taddd #%u\n\txgdx\n\ttxs\n", WORD(n));
x_fprel = 1;
x_fpoff = sp;
return;
}
invalidate_x();
if (n > 0) {
/* Otherwise we know pulx is cheapest */
repeated_op(n / 2, "pulx");
if (n & 1)
puts("\tins");
}
if (n < 0) {
/* pshx likewise is an option */
repeated_op(-n / 2, "pshx");
if (n & 1)
puts("\tdes");
}
return;
}
if (cpu_has_abx && abxcost < hardcost)
cost = abxcost;
/* PULX might be fastest */
if (n > 0 && cpu_has_pshx && (n / 2) + (n & 1) <= cost) {
invalidate_x();
repeated_op(n / 2, "pulx");
if (n & 1)
puts("\tins");
return;
}
/* If not check if ins is */
if (n >= 0 && n <= cost) {
repeated_op(n, "ins");
return;
}
/* ABX is the cheapest option if we have it */
if (n > 0 && cpu_has_abx && cost == abxcost) {
/* TODO track b properly when save save_d */
/* FIXME: need top put S into X and back.. */
puts("\ttsx");
if (save_d)
puts("\tpshb");
if(n > 255) {
load_b_const(255);
while(n >= 255) {
puts("\tabx");
n -= 255;
}
}
if (n) {
load_b_const(n);
puts("\tabx");
}
if (save_d)
puts("\tpulb");
puts("\ttxs");
x_fprel = 1;
x_fpoff = 0;
return;
}
/* Forms where ins/des are always best */
if (n >=0 && n <= 4) {
repeated_op(n, "ins");
return;
}
if (cpu_has_pshx && n < 0 && -n/2 + (n & 1) <= hardcost) {
repeated_op(-n/2, "pshx");
if (n & 1)
puts("\tdes");
return;
}
if (n < 0 && n >= -4) {
repeated_op(-n, "des");
return;
}
if (optsize) {
if (n > 0 && n <= 255) {
printf("\tjsr __addsp8\n\t.byte %u\n", n & 0xFF);
return;
}
if (n <0 && n >= -255) {
printf("\tjsr __subsp8\n\t.byte %u\n", -n & 0xFF);
return;
}
printf("\tjsr __modsp16\n\t.word %u\n", WORD(n));
return;
}
if (n >=0 && n <= hardcost) {
repeated_op(n, "ins");
return;
}
if (n < 0 && -n <= hardcost) {
repeated_op(-n, "des");
return;
}
/* TODO: if we save_d we need to keep and valid */
/* Inline */
if (save_d)
puts("\tstaa @tmp2\n\tstab @tmp2+1");
move_s_d();
add_d_const(n);
move_d_s();
if (save_d)
puts("\tldaa @tmp2\n\tldab @tmp2+1");
}
void op8_on_ptr(const char *op, unsigned off)
{
printf("\t%sb %u,x\n", op, off);
}
/* Do the low byte first in case it's add adc etc */
void op16_on_ptr(const char *op, const char *op2, unsigned off)
{
/* Big endian */
printf("\t%sb %u,x\n\t%sa %u,x\n", op, off + 1, op2, off);
}
/* Operations where D can be used on later processors */
void op16d_on_ptr(const char *op, const char *op2, unsigned off)
{
/* Big endian */
if (cpu_has_d)
printf("\t%sd %u,x\n", op, off);
else
printf("\t%sb %u,x\n\t%sa %u,x\n", op, off + 1, op2, off);
}
static void op32_on_ptr(const char *op, const char *op2, unsigned off)
{
printf("\t%sb %u,x\n\t%sa %u,x\n", op, off + 3, op2, off + 2);
if (cpu_has_y) {
swap_d_y();
printf("\t%sb %u,x\n\t%sa %u,x\n", op2, off + 1, op2, off);
swap_d_y();
} else {
puts("\tpshb\n\tpsha\n\tldaa @hireg\n\tldab @hireg+1");
printf("\t%sb %u,x\n\t%sa %u,x\n", op2, off + 1, op2, off);
puts("\tstaa @hireg\n\tstab @hireg+1\n\tpula\n\tpulb");
}
}
void op32d_on_ptr(const char *op, const char *op2, unsigned off)
{
if (!cpu_has_d) {
op32_on_ptr(op, op2, off);
return;
}
printf("\t%sd %u,x\n", op, off + 2);
if (cpu_has_y) {
swap_d_y();
printf("\t%sb %u,x\n\t%sa %u,x\n", op2, off + 1, op2, off);
swap_d_y();
} else {
puts("\tpshb\n\tpsha\n\tldd @hireg");
printf("\t%sb %u,x\n\t%sa %u,x\n", op2, off + 1, op2, off);
puts("\tstd @hireg\n\tpula\n\tpulb");
}
}
void load32(register unsigned off)
{
if (cpu_has_y) {
printf("\tldd %u,x\n", off);
swap_d_y();
printf("\tldd %u,x\n", off + 2);
} else if (cpu_has_d)
printf("\tldd %u,x\n\tstd @hireg\n\tldd %u,x\n", off, off + 2);
else {
printf("\tldaa %u,x\n\tldab %u,x\n\tldx %u,x\n\tstx @hireg\n",
off + 2, off + 3, off);
invalidate_x();
}
}
void store32(register unsigned off, unsigned nr)
{
if (cpu_has_y)
printf("\tsty %u,x\n\tstd %u,x\n", off, off + 2);
else if (cpu_has_d) {
printf("\tstd %u,x\n\tldd @hireg\n\tstd %u,x\n", off + 2, off);
if (nr)
printf("\tldd %u,x\n", off + 2);
} else {
printf("\tstab %u,x\n\tstaa %u,x\n\tpsha\n", off + 3, off + 2);
printf("\tldaa @hireg+1\n\tstaa %u,x\n\tldaa @hireg\n\tstaa %u,x\n\tpula\n", off + 1, off);
}
}
void uniop_on_ptr(register const char *op, register unsigned off,
register unsigned size)
{
off += size;
while(size--)
printf("\t%s %u,x\n", op, --off);
}
/*
* Generate a reference via X to a local
*/
unsigned make_local_ptr(unsigned off, unsigned rlim)
{
/* Both relative to frame base */
int noff = off - x_fpoff;
printf(";make local ptr off %u, rlim %u noff %u\n", off, rlim, noff);
/* TODO: if we can d a small < 7 or so shift by decrement then
it may beat going via tsx */
if (x_fprel == 0 || noff < 0) {
printf("\ttsx\n");
x_fprel = 1;
x_fpoff = sp;
}
off += x_fpoff;
if (off <= rlim)
return off;
/* It is cheaper to inx than mess around with calls for smaller
values - 7 or 5 if no save needed */
if (off - rlim < 7) {
repeated_op(off - rlim, "inx");
x_fpoff -= off - rlim;
return rlim;
}
/* These cases push and pop the old D but we don't have a tracking
mechanism to optimise it. FIXME one day. The first half of things
is fine as we push and load so can optimize, but the resulting
case we can currently only invalidate for */
if (off - rlim < 256) {
puts("\tpshb");
load_b_const(off - rlim);
if (cpu_has_d) /* And thus ABX */
puts("\tabx");
else
puts("\tjsr __abx");
puts("\tpulb");
x_fpoff -= off - rlim;
return rlim;
} else {
/* This case is (thankfully) fairly rare */
printf("\tjsr __addxconst\n\t.word %u\n", off);
x_fpoff -= off;
return 0;
}
}
/* Get pointer to the top of stack. We can optimize this in some cases
when we track but it will be limited. The 6800 is quite weak on ops
between register so we sometimes need to build ops against top of stack.
This needs some care because the stack pointer has already been adjusted
in codegen-6800 to allow for the removal of the data, thus we actually
need to offset by the passed size */
static unsigned make_tos_ptr(unsigned s)
{
printf(";make_tos_ptr %u %u, %u\n", x_fpoff, sp + s, x_fprel);
/* TODO: we might have X pointing below the stack but in range and that would be just fine */
if (x_fpoff == sp + s && x_fprel == 1)
return 0;
puts("\ttsx");
x_fpoff = sp + s;
x_fprel = 1;
return 0;
}
static char *addr_form(register struct node *r, unsigned off, unsigned s)
{
static char addr[32];
const char *mod = "";
unsigned v = r->value;
if (s == 1)
mod = "<";
else if (s == 3)
mod = ">";
switch(r->op) {
case T_CONSTANT:
if (s == 1)
v &= 0xFF;
sprintf(addr, "#%s%u", mod, v + off);
return addr;
case T_NAME:
sprintf(addr, "#%s_%s+%u", mod, namestr(r->snum), v + off);
return addr;
case T_NSTORE:
case T_NREF:
sprintf(addr, "_%s+%u%s", namestr(r->snum), v + off, pic_op);
return addr;
case T_LABEL:
sprintf(addr, "#%sT%u+%u", mod, r->val2, v + off);
return addr;
case T_LBSTORE:
sprintf(addr, "T%u+%u%s", r->val2, v + off, pic_op);
return addr;
case T_LBREF:
sprintf(addr, "T%u+%u%s", r->val2, v + off, pic_op);
return addr;
default:
error("aform");
}
return NULL;
}
/* Those with address forms we can directly load */
unsigned can_load_d_nox(struct node *n, unsigned off)
{
register unsigned op = n->op;
if (op == T_CONSTANT || op == T_NAME || op == T_LABEL || op == T_NREF || op == T_LBREF)
return 1;
return 0;
}
/* These functions must not touch X on the 6809, they can on others */
unsigned op8_on_node(struct node *r, const char *op, unsigned off)
{
unsigned v = r->value;
invalidate_work();
switch(r->op) {
case T_LSTORE:
case T_LREF:
off = make_local_ptr(v + off, 255);
op8_on_ptr(op, off);
break;
case T_CONSTANT:
case T_LBSTORE:
case T_LBREF:
case T_LABEL:
case T_NSTORE:
case T_NREF:
case T_NAME:
printf("\t%sb %s\n", op, addr_form(r, off, 1));
break;
default:
return 0;
}
return 1;
}
/* Do the low byte first in case it's add adc etc */
unsigned op16_on_node(register struct node *r, const char *op, const char *op2, unsigned off)
{
register unsigned v = r->value;
invalidate_work();
switch(r->op) {
case T_LSTORE:
case T_LREF:
off = make_local_ptr(v + off, 254);
op16_on_ptr(op, op2, off);
break;
case T_CONSTANT:
printf("\t%sb #<%u\n", op, (v + off) & 0xFFFF);
printf("\t%sa #>%u\n", op2, (v + off) & 0xFFFF);
break;
case T_LBSTORE:
case T_LBREF:
case T_NSTORE:
case T_NREF:
printf("\t%sb %s\n", op, addr_form(r, off + 1, 1));
printf("\t%sa %s\n", op2, addr_form(r, off, 1));
break;
case T_NAME:
case T_LABEL:
printf("\t%sb %s\n", op, addr_form(r, off, 1));
printf("\t%sa %s\n", op2, addr_form(r, off, 3));
break;
default:
return 0;
}
return 1;
}
unsigned op16d_on_node(register struct node *r, const char *op, const char *op2, unsigned off)
{
unsigned v = r->value;
invalidate_work();
switch(r->op) {
case T_LSTORE:
case T_LREF:
off = make_local_ptr(v + off, 254);
op16d_on_ptr(op, op2, off);
break;
case T_CONSTANT:
case T_LBSTORE:
case T_LBREF:
case T_LABEL:
case T_NSTORE:
case T_NREF:
case T_NAME:
printf("\t%sd %s\n", op, addr_form(r, off, 2));
break;
default:
return 0;
}
return 1;
}
unsigned op16y_on_node(register struct node *r, const char *op, unsigned off)
{
unsigned v = r->value;
switch(r->op) {
case T_LSTORE:
case T_LREF:
off = make_local_ptr(v + off, 254);
printf("\t%sy %u,x\n", op, off);
break;
case T_CONSTANT:
case T_LBSTORE:
case T_LBREF:
case T_LABEL:
case T_NSTORE:
case T_NREF:
case T_NAME:
printf("\t%sy %s\n", op, addr_form(r, off, 2));
default:
return 0;
}
return 1;
}
unsigned write_op(register struct node *r, const char *op, const char *op2, unsigned off)
{
unsigned s = get_size(r->type);
if (s == 2)
return op16_on_node(r, op, op2, off);
if (s == 1)
return op8_on_node(r, op, off);
return 0;
}
unsigned write_opd(struct node *r, const char *op, const char *op2, unsigned off)
{
unsigned s = get_size(r->type);
if (s == 2) {
if (!cpu_has_d)
return op16_on_node(r, op, op2, off);
return op16d_on_node(r, op, op2, off);
}
if (s == 1)
return op8_on_node(r, op, off);
return 0;
}
unsigned write_uni_op(register struct node *r, const char *op, unsigned off)
{
unsigned v = r->value;
unsigned s = get_size(r->type);
if (s == 4)
return 0;
switch(r->op) {
case T_LSTORE:
case T_LREF:
off = make_local_ptr(v + off, 254);
uniop_on_ptr(op, off, s);
break;
case T_LBSTORE:
case T_LBREF:
case T_NSTORE:
case T_NREF:
printf("\t%s %s\n", op, addr_form(r, off, 1));
if (s == 2)
printf("\t%s %s\n", op, addr_form(r, off + 1, 1));
break;
default:
return 0;
}
return 1;
}
static void op8_on_tos(const char *op)
{
unsigned off = make_tos_ptr(1);
printf("\t%sb %u,x\n\tins\n", op, off);
}
static void op16_on_tos(const char *op)
{
register unsigned off;
invalidate_work();
off = make_tos_ptr(2);
printf("\t%sb %u,x\n\t%sa %u,x\n\tins\n\tins\n", op, off + 1, op, off);
}
static void op16d_on_tos(const char *op)
{
unsigned off;
invalidate_work();
off = make_tos_ptr(2);
printf("\t%sd %u,x\n\tins\n\tins\n", op, off);
}
/* Only used for operations where there is no ordering requirement */
unsigned write_tos_op(struct node *n, register const char *op)
{
register unsigned s = get_size(n->type);
if (s > 2 && !cpu_has_y)
return 0;
if (s == 4) {
swap_d_y();
/* So that the second 16bit op is correctly offset */
sp += 2;
op16_on_tos(op);
sp -= 2;
swap_d_y();
op16_on_tos(op);
} else if (s == 2)
op16_on_tos(op);
else
op8_on_tos(op);
invalidate_work();
return 1;
}
unsigned write_tos_opd(struct node *n, const char *op, const char *unused)
{
unsigned s = get_size(n->type);
if (s > 2)
return 0;
else if (s == 2)
op16d_on_tos(op);
else
op8_on_tos(op);
invalidate_work();
return 1;
}
static void uniop8_on_tos(const char *op)
{
unsigned off = make_tos_ptr(1);
invalidate_work();
printf("\t%s %u,x\n\tins\n", op, off);
}
static void uniop16_on_tos(register const char *op)
{
register unsigned off = make_tos_ptr(2);
invalidate_work();
printf("\t%s %u,x\n\t%s %u,x\n\tins\n\tins\n", op, off + 1, op, off);
}
unsigned write_tos_uniop(struct node *n, register const char *op)
{
unsigned s = get_size(n->type);
if (s > 2)
return 0;
if (s == 2)
uniop16_on_tos(op);
else
uniop8_on_tos(op);
return 1;
}
/* TODO: decide how much we inline for -Os */
unsigned left_shift(register struct node *n)
{
register unsigned s = get_size(n->type);
register unsigned v;
if (s > 2 || n->right->op != T_CONSTANT)
return 0;
v = n->right->value;
if (s == 1) {
if (v >= 8) {
load_b_const(0);
return 1;
}
invalidate_work();
repeated_op(v, "lslb");
return 1;
}
if (s == 2) {
if (v >= 16) {
load_d_const(0);
return 1;
}
if (v >= 8) {
load_a_b();
load_b_const(0);
v -= 8;
if (v) {
invalidate_work();
repeated_op(v, "lsla");
}
return 1;
}
while(v--)
puts("\tlslb\n\trola");
invalidate_work();
return 1;
}
return 0;
}
unsigned right_shift(register struct node *n)
{
register unsigned s = get_size(n->type);
register unsigned v;
register const char *op = "asr";
if (n->type & UNSIGNED)
op = "lsr";
if (s > 2 || n->right->op != T_CONSTANT)
return 0;
v = n->right->value;
if (s == 1) {
if (v >= 8) {
load_b_const(0);
return 1;
}
invalidate_work();
repeated_op(v, op);
return 1;
}
if (s == 2) {
if (v >= 16) {
load_d_const(0);
return 1;
}
if (v >= 8 && (n->type & UNSIGNED)) {
load_b_a();
load_a_const(0);
v -= 8;
if (v) {
while(v--)
printf("\t%sb\n", op);
invalidate_work();
}
return 1;
}
while(v--)
printf("\t%sa\n\trorb\n", op);
invalidate_work();
return 1;
}
return 0;
}
/* See if we can easily get the value we want into X/Y/U rather than D. Must
not harm D in the process. We can make this smarter over time if needed.
Might be worth passing if we can trash D as it will help make_local_ptr
later, and will be true for some load cases */
unsigned can_load_r_simple(struct node *r, unsigned off)
{
unsigned s = get_size(r->type);
if (s == 4)
return 0;
switch(r->op) {
case T_ARGUMENT:
case T_LOCAL:
case T_LREF:
case T_CONSTANT:
case T_LBREF:
case T_NREF:
case T_NAME:
case T_LABEL:
case T_RREF:
case T_RDEREF:
return 1;
}
return 0;
}
/* Also allow offset in the result and some level of complexity via
lea for offsets on things like struct */
unsigned can_load_r_with(struct node *r, unsigned off)
{
unsigned s = get_size(r->type);
if (s == 4)
return 0;
switch(r->op) {
case T_ARGUMENT:
case T_LOCAL:
case T_LREF:
case T_CONSTANT:
case T_LBREF:
case T_NREF:
case T_NAME:
case T_LABEL:
return 1;
}
return 0;
}
/* For 6800 at least it is usually cheaper to reload even if the value
we want is in D */
static unsigned load_r_with(char reg, register struct node *r, unsigned off)
{
register unsigned v = r->value;
switch(r->op) {
case T_ARGUMENT:
v += argbase + frame_len;
case T_LOCAL:
/* TODO: will need a Y specific rule for HC11 */
/* Worst case for size is 252 */
return make_local_ptr(v + off, 252);
case T_LREF:
off = make_local_ptr(v + off, 252);
printf("\tld%c %u,x\n", reg, off);
if(reg == 'x')
invalidate_x();
break;
case T_CONSTANT:
case T_LBREF:
case T_LABEL:
case T_NREF:
case T_NAME:
printf("\tld%c %s\n", reg, addr_form(r, off, 2));
if(reg == 'x')
invalidate_x();
break;
default:
error("lxw");
}
return 0;
}
unsigned load_x_with(struct node *r, unsigned off)
{
return load_r_with('x', r, off);
}
/* 6809 specific register loading */
unsigned load_u_with(struct node *r, unsigned off)
{
return load_r_with('u', r, off);
}
unsigned cmp_direct(struct node *n, const char *uop, const char *op)
{
register struct node *r = n->right;
register unsigned v = r->value;
register unsigned s = get_size(r->type);
if (r->op != T_CONSTANT)
return 0;
if (r->type & UNSIGNED)
op = uop;
/* If we knoww the upper half matches - eg a byte var that has been expanded then
shortcut it. Need to think about this for signed math. I think we can do signed
math if we use uop in the size 2 upper match case : TODO*/
if (s == 1 || (op == uop && a_valid && (v >> 8) == a_val)) {
printf("\tcmpb #%u\n\t%s %s\n", v & 0xFF, jsr_op, op);
n->flags |= ISBOOL;
invalidate_b();
return 1;