-
Notifications
You must be signed in to change notification settings - Fork 15
/
backend-hc08.c
2509 lines (2342 loc) · 50.2 KB
/
backend-hc08.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
/*
* HC08 backend for the Fuzix C Compiler
*
* Register usage
* A: lower half of working value
* X: upper half of working value or low half of pointer
* H: upper half of pointer
* @tmp: scratch value used extensively
* @tmp2: temporary word following @tmp
* @hireg: upper 16bits of 32bit working values
*
* In many respects the 6502 and 68HC08 are very similar. We however
* have a proper stack and index register which makes life somewhat
* easier.
*
* We are a bit naiive in that we use ,SP forms always whereas it would
* often be shorter to stuff SP into HX and remember the fact so we can
* use the byte shorter ,X modes
*/
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include "compiler.h"
#include "backend.h"
#include "backend-byte.h"
#define BYTE(x) (((unsigned)(x)) & 0xFF)
#define WORD(x) (((unsigned)(x)) & 0xFFFF)
#define USE_HX 0x0080
/*
* 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 following an unconditional jump */
static unsigned xlabel; /* Internal backend generated branches */
static unsigned argbase = 2; /* Track shift between arguments and stack */
static uint8_t hx_live; /* Set when H:X holds a pointer rather than X:A
data */
/*
* Node types we create in rewriting rules
*/
#define T_NREF (T_USER) /* Load of C global/static */
#define T_CALLNAME (T_USER+1) /* Function call by name */
#define T_NSTORE (T_USER+2) /* Store to a C global/static */
#define T_LREF (T_USER+3) /* Ditto for local */
#define T_LSTORE (T_USER+4)
#define T_LBREF (T_USER+5) /* Ditto for labelled strings or local static */
#define T_LBSTORE (T_USER+6)
#define T_RREF (T_USER+7)
#define T_RSTORE (T_USER+8)
#define T_RDEREF (T_USER+9) /* *regptr */
#define T_REQ (T_USER+10) /* *regptr */
/*
* 68HC08 specifics. We need to track some register values to produce
* bearable code
*/
static void output(const char *p, ...)
{
va_list v;
va_start(v, p);
putchar('\t');
vprintf(p, v);
putchar('\n');
va_end(v);
}
static void label(const char *p, ...)
{
va_list v;
va_start(v, p);
vprintf(p, v);
putchar(':');
putchar('\n');
va_end(v);
}
#define R_A 0
#define R_X 1
#define R_H 2
#define INVALID 0
struct regtrack {
unsigned state;
uint8_t value;
unsigned snum;
unsigned offset;
};
static struct regtrack reg[3];
static void invalidate_regs(void)
{
reg[R_A].state = INVALID;
reg[R_X].state = INVALID;
reg[R_H].state = INVALID;
}
static void invalidate_a(void)
{
reg[R_A].state = INVALID;
}
static void invalidate_x(void)
{
reg[R_X].state = INVALID;
}
static void invalidate_hx(void)
{
reg[R_H].state = INVALID;
reg[R_X].state = INVALID;
}
static void const_a_set(unsigned val)
{
if (reg[R_A].state == T_CONSTANT)
reg[R_A].value = val;
else
reg[R_A].state = INVALID;
}
static void const_x_set(unsigned val)
{
if (reg[R_X].state == T_CONSTANT)
reg[R_X].value = val;
else
reg[R_X].state = INVALID;
}
/* Get a value into A, adjust and track */
static void load_a(uint8_t n)
{
if (reg[R_A].state == T_CONSTANT) {
if (reg[R_A].value == n)
return;
}
/* No inca deca */
else if (n == 0)
output("clra");
else if (reg[R_X].state == T_CONSTANT && reg[R_X].value == n)
output("txa");
else
output("lda #%u", n);
reg[R_A].state = T_CONSTANT;
reg[R_A].value = n;
}
/* Get a value into X, adjust and track */
static void load_x(uint8_t n)
{
if (reg[R_X].state == T_CONSTANT) {
if (reg[R_X].value == n)
return;
if (reg[R_X].value == n - 1) {
output("inx");
reg[R_X].value++;
return;
}
if (reg[R_X].value == n + 1) {
output("dex");
reg[R_X].value--;
return;
}
}
else if (n == 0)
output("clrx");
else if (reg[R_A].state == T_CONSTANT && reg[R_A].value == n)
output("tax");
else
output("ldx #%u", n);
reg[R_X].state = T_CONSTANT;
reg[R_X].value = n;
}
/* Get a value into H:X, adjust and track */
static void load_hx(uint16_t n)
{
if (reg[R_X].state == T_CONSTANT && reg[R_H].state == T_CONSTANT) {
if (reg[R_X].value == (n & 0xFF) && reg[R_H].value == (n >> 8))
return;
}
if (n == 0) {
output("clrh");
output("clrx");
} else
output("ldhx #%u", n);
reg[R_X].state = T_CONSTANT;
reg[R_X].value = n & 0xFF;
reg[R_H].state = T_CONSTANT;
reg[R_H].value = n >> 8;
}
/*
* H:X is used for pointer work but is not the same as XA for integers
* This is a pain but not something we can do much about as it's built
* into the CPU design
*/
static void set_hx_node(struct node *n)
{
unsigned op = n->op;
unsigned value = n->value;
/* Turn store forms into ref forms */
switch(op) {
case T_NSTORE:
op = T_NREF;
break;
case T_LBSTORE:
op = T_LBREF;
break;
case T_LSTORE:
op = T_LREF;
break;
case T_NAME:
case T_CONSTANT:
case T_NREF:
case T_LBREF:
case T_LREF:
case T_LOCAL:
case T_ARGUMENT:
break;
default:
invalidate_a();
invalidate_x();
return;
}
reg[R_H].state = op;
reg[R_X].state = op;
reg[R_H].value = value >> 8;
reg[R_X].value = value & 0xFF;
reg[R_H].snum = n->snum;
reg[R_X].snum = n->snum;
return;
}
static unsigned hx_contains(struct node *n)
{
if (n->op == T_NREF && (n->flags & SIDEEFFECT)) /* Volatiles */
return 0;
if (reg[R_H].state != n->op || reg[R_X].state != n->op)
return 0;
if (reg[R_H].value != (n->value >> 8) || reg[R_X].value != (n->value & 0xFF))
return 0;
if (reg[R_H].snum != n->snum || reg[R_X].snum != n->snum)
return 0;
/* Looks good */
return 1;
}
static void set_xa_node(struct node *n)
{
unsigned op = n->op;
unsigned value = n->value;
/* Turn store forms into ref forms */
switch(op) {
case T_NSTORE:
op = T_NREF;
break;
case T_LBSTORE:
op = T_LBREF;
break;
case T_LSTORE:
op = T_LREF;
break;
case T_NAME:
case T_CONSTANT:
case T_NREF:
case T_LBREF:
case T_LREF:
case T_LOCAL:
case T_ARGUMENT:
break;
default:
invalidate_a();
invalidate_x();
return;
}
reg[R_X].state = op;
reg[R_A].state = op;
reg[R_X].value = value >> 8;
reg[R_A].value = value & 0xFF;
reg[R_X].snum = n->snum;
reg[R_A].snum = n->snum;
return;
}
static unsigned xa_contains(struct node *n)
{
if (n->op == T_NREF && (n->flags & SIDEEFFECT)) /* Volatiles */
return 0;
if (reg[R_X].state != n->op || reg[R_A].state != n->op)
return 0;
if (reg[R_X].value != (n->value >> 8) || reg[R_A].value != (n->value & 0xFF))
return 0;
if (reg[R_X].snum != n->snum || reg[R_A].snum != n->snum)
return 0;
/* Looks good */
return 1;
}
static void set_a_node(struct node *n)
{
unsigned op = n->op;
unsigned value = n->value;
switch(op) {
case T_NSTORE:
op = T_NREF;
break;
case T_LBSTORE:
op = T_LBREF;
break;
case T_LSTORE:
op = T_LREF;
break;
case T_NAME:
case T_CONSTANT:
case T_NREF:
case T_LBREF:
case T_LREF:
case T_LOCAL:
case T_ARGUMENT:
break;
default:
invalidate_a();
return;
}
reg[R_A].state = op;
reg[R_A].value = value;
reg[R_A].snum = n->snum;
}
static unsigned a_contains(struct node *n)
{
if (reg[R_A].state != n->op)
return 0;
if (reg[R_A].value != n->value)
return 0;
if (reg[R_A].snum != n->snum)
return 0;
/* Looks good */
return 1;
}
/* Memory writes occured, invalidate according to what we know. Passing
NULL indicates unknown memory changes */
#if 0
static void invalidate_node(struct node *n)
{
/* For now don't deal with the complex cases of whether we might
invalidate another object */
if (reg[R_A].state != T_CONSTANT)
reg[R_A].state = INVALID;
if (reg[R_X].state != T_CONSTANT)
reg[R_X].state = INVALID;
}
#endif
static void invalidate_mem(void)
{
if (reg[R_A].state != T_CONSTANT)
reg[R_A].state = INVALID;
if (reg[R_X].state != T_CONSTANT)
reg[R_X].state = INVALID;
if (reg[R_H].state != T_CONSTANT)
reg[R_H].state = INVALID;
}
static void set_reg(unsigned r, unsigned v)
{
reg[r].state = T_CONSTANT;
reg[r].value = (uint8_t)v;
}
/*
* 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;
}
/*
* For HC08 we keep byte objects byte size
*/
static unsigned get_stack_size(unsigned t)
{
return get_size(t);
}
/* Generate a call to an internal helper */
static void gen_internal(const char *p)
{
invalidate_regs();
output("jsr __%s", p);
hx_live = 0;
}
static void repeated_op(unsigned n, const char *o)
{
while(n--)
output(o);
}
static void use_xa(unsigned n)
{
if (hx_live) {
printf(";use_xa %u\n", n);
output("sthx @tmp");
output("txa");
output("ldx @tmp");
hx_live = 0;
}
}
static void use_hx(unsigned n)
{
if (hx_live == 0) {
printf(";use_hx %u\n", n);
output("stx @tmp");
output("sta @tmp+1");
output("ldhx @tmp");
hx_live = 1;
}
}
static void adjust_hx(int v)
{
invalidate_hx();
while(v >= 127) {
output("aix #127");
v -= 127;
}
while(v < -128) {
output("aix #-128");
v += 128;
}
if (v)
output("aix #%d", v);
}
static void adjust_sp(int v)
{
while(v >= 127) {
output("ais #127");
v -= 127;
}
while(v < -128) {
output("ais #-128");
v += 128;
}
if (v)
output("ais #%d", v);
}
/*
* For endian reasons these are a bit differennt to the 6502. We expect
* the caller to deal with any endian biasing of offsets
*/
/* Construct a direct operation if possible for the primary op */
static int do_pri8(struct node *n, const char *op, void (*pre)(struct node *__n), unsigned bias)
{
struct node *r = n->right;
const char *name;
unsigned v = n->value;
/* We can fold in some simple casting */
if (n->type == T_CAST) {
if ((!PTR(n->type) && n->type != CINT && n->type != UINT) || r->type != UCHAR)
return 0;
/* Just do the right hand side */
n = n->right;
v = n->value;
r = n->right;
}
switch(n->op) {
case T_LABEL:
pre(n);
output("%s #<T%d+%d", op, n->val2, v);
return 1;
case T_NAME:
pre(n);
name = namestr(n->snum);
output("%s #<_%s+%d", op, name, v);
return 1;
case T_CONSTANT:
/* These had the right squashed into them */
case T_LREF:
case T_NREF:
case T_LBREF:
case T_LSTORE:
case T_NSTORE:
case T_LBSTORE:
/* These had the right squashed into them */
r = n;
break;
}
v = r->value;
switch(r->op) {
case T_CONSTANT:
pre(n);
if (strcmp(op, "lda") == 0)
load_a(v);
else if (strcmp(op, "ldx") == 0)
load_x(v);
else
output("%s #%d", op, r->value & 0xFF);
return 1;
case T_LREF:
case T_LSTORE:
pre(n);
output("%s %u,sp", op, v + bias + sp);
return 1;
case T_NREF:
case T_NSTORE:
pre(n);
name = namestr(r->snum);
output("%s _%s+%d", op, name, (unsigned)r->value + bias);
return 1;
case T_LBSTORE:
case T_LBREF:
pre(n);
output("%s T%d+%d", op, r->val2, (unsigned)r->value + bias);
return 1;
/* If we add registers
case T_RREF:
output("%s __reg%d", op, r->val2);
return 1;*/
}
return 0;
}
/* Construct a direct operation if possible for the primary op */
static int do_pri8hi(struct node *n, const char *op, void (*pre)(struct node *__n))
{
struct node *r = n->right;
const char *name;
unsigned v = n->value;
use_xa(0);
/* We can fold in some simple casting */
if (n->type == T_CAST) {
if ((!PTR(n->type) && n->type != CINT && n->type != UINT) || r->type != UCHAR)
return 0;
/* We need to do it on 0 */
load_a(0);
n = n->right;
v = n->value;
r = n->right;
}
switch(n->op) {
case T_LABEL:
pre(n);
output("%s #>T%d+%d", op, n->val2, v);
return 1;
case T_NAME:
pre(n);
name = namestr(n->snum);
output("%s #_%s+%d", op, name, v);
return 1;
case T_CONSTANT:
/* These had the right squashed into them */
case T_LREF:
case T_NREF:
case T_LBREF:
case T_LSTORE:
case T_NSTORE:
case T_LBSTORE:
/* These had the right squashed into them */
r = n;
break;
}
v = r->value;
switch(r->op) {
case T_CONSTANT:
pre(n);
v >>= 8;
if (strcmp(op, "lda") == 0)
load_a(v);
else if (strcmp(op, "ldx") == 0)
load_x(v);
else
output("%s #%d", op, v);
return 1;
case T_LREF:
case T_LSTORE:
pre(n);
printf(";8hi lref %u sp %u\n", v, sp);
output("%s %u,sp", op, v + sp);
return 1;
case T_NREF:
case T_NSTORE:
pre(n);
name = namestr(r->snum);
output("%s _%s+%d", op, name, v);
return 1;
case T_LBSTORE:
case T_LBREF:
pre(n);
output("%s T%d+%d", op, r->val2, v);
return 1;
/* If we add registers
case T_RREF:
output("%s __reg%d+1", op, r->val2);
return 1;*/
}
return 0;
}
/* 16bit: We are rather limited here because we only have a few ops with x */
static int do_pri16(struct node *n, const char *op, void (*pre)(struct node *__n))
{
struct node *r = n->right;
const char *name;
unsigned v = n->value;
/* We can fold in some simple casting */
if (n->type == T_CAST) {
use_xa(10);
if ((!PTR(n->type) && n->type != CINT && n->type != UINT) || r->type != UCHAR)
return 0;
/* Just do the right hand side */
n = n->right;
v = n->value;
r = n->right;
load_x(0);
}
switch(n->op) {
case T_LABEL:
use_xa(11);
pre(n);
output("%sa #<T%d+%d", op, n->val2, v);
output("%sx #>T%d+%d", op, n->val2, v >> 8);
return 1;
case T_NAME:
use_xa(12);
pre(n);
name = namestr(n->snum);
output("%sa #<_%s+%d", op, name, v);
output("%sx #>_%s+%d", op, name, v >> 8);
return 1;
case T_LOCAL:
case T_LREF:
case T_NREF:
case T_LBREF:
case T_LSTORE:
case T_NSTORE:
case T_LBSTORE:
case T_CONSTANT:
/* These had the right squashed into them */
r = n;
}
v = r->value;
if (get_size(r->type) != 2)
error("pri16bt");
switch(r->op) {
case T_CONSTANT:
use_xa(13);
pre(n);
if (strcmp(op, "ld") == 0) {
load_a(v);
load_x(v >> 8);
} else {
output("%sa #%u", op, v & 0xFF);
output("%sx #%u", op, v >> 8);
}
return 1;
case T_LREF:
if (strcmp(op, "ld") == 0) {
use_xa(1);
pre(n);
v += sp;
output("ldx %u,sp\n", v);
output("lda %u,sp\n", v + 1);
return 1;
}
case T_LSTORE:
use_xa(14);
v += sp;
pre(n);
output("%sa %u, sp", op, v);
output("tax");
output("%sa %u, sp", op, v + 1);
return 1;
case T_NSTORE:
case T_NREF:
use_xa(15);
name = namestr(r->snum);
pre(n);
output("%sa _%s+%d", op, name, (unsigned)r->value);
output("%sx _%s+%d", op, name, ((unsigned)r->value) + 1);
return 1;
case T_LBSTORE:
case T_LBREF:
use_xa(16);
pre(n);
output("%sa T%d+%d", op, r->val2, (unsigned)r->value);
output("%sx T%d+%d", op, r->val2, ((unsigned)r->value) + 1);
return 1;
/* If we add registers
case T_RREF:
pre(n);
output("%sa __reg%dd", op, r->val2);
output("%sx __reg%d + 1", op, r->val2);
return 1;*/
}
return 0;
}
/* 16bit via H:X. Only immediate and direct forms for load/store */
static int do_hxpri16(struct node *n, const char *op, void (*pre)(struct node *__n))
{
struct node *r = n->right;
const char *name;
unsigned v = n->value;
switch(n->op) {
case T_LABEL:
pre(n);
output("%shx #T%d+%d", op, n->val2, v);
hx_live = 1;
return 1;
case T_NAME:
pre(n);
name = namestr(n->snum);
output("%shx #_%s+%d", op, name, v);
hx_live = 1;
return 1;
case T_LOCAL:
case T_LREF:
case T_NREF:
case T_LBREF:
case T_LSTORE:
case T_NSTORE:
case T_LBSTORE:
case T_CONSTANT:
/* These had the right squashed into them */
r = n;
}
v = r->value;
if (get_size(r->type) != 2)
error("hxpri16bt");
switch(r->op) {
case T_CONSTANT:
pre(n);
load_hx(v);
hx_live = 1;
return 1;
case T_ARGUMENT:
v += frame_len + argbase;
case T_LOCAL:
v += sp;
pre(n);
output("tsx");
adjust_hx(v);
hx_live = 1;
return 1;
}
return 0;
}
static void pre_none(struct node *n)
{
}
static void pre_txa(struct node *n)
{
output("txa");
hx_live = 0;
}
static void pre_store8(struct node *n)
{
if (hx_live)
output("stx @tmp");
else
output("sta @tmp");
hx_live = 0;
}
static void pre_store16(struct node *n)
{
if (hx_live)
output("sthx @tmp");
else {
output("sta @tmp");
output("stx @tmp+1");
}
hx_live = 0;
}
static void pre_store16clx(struct node *n)
{
if (hx_live) {
output("sthx @tmp");
output("txa");
memcpy(®[R_A], ®[R_X], sizeof(struct regtrack));
hx_live = 0;
} else {
output("sta @tmp");
output("stx @tmp+1");
}
load_x(0);
}
static void pre_psha(struct node *n)
{
output("psha");
sp++;
}
static void pre_hx(struct node *n)
{
use_hx(0);
}
static int pri8(struct node *n, const char *op)
{
return do_pri8(n, op, pre_none, 0);
}
static int pri16(struct node *n, const char *op)
{
return do_pri16(n, op, pre_none);
}
static unsigned fast_castable(struct node *n)
{
struct node *r = n->right;
/* Is this a case we can just flow into the code. Usually that's
a uchar to int */
if (r->op == T_CAST && get_size(r->type) == 2 && r->right->type == UCHAR)
return 1;
return 0;
}
static int pri8_help(struct node *n, char *helper)
{
struct node *r = n->right;
/* Special case for cast first */
if (fast_castable(n)) {
if (do_pri8(r->right, "lda", pre_store8, 0)) {
helper_s(n, helper);
return 1;
}
}
if (do_pri8(r, "lda", pre_store8, 0)) {
/* Helper invalidates A itself */
helper_s(n, helper);
return 1;
}
return 0;
}
static void pre_fastcast(struct node *n)
{
if (hx_live) {
output("sthx @tmp");
hx_live = 0;
} else {
output("stx @tmp");
output("sta @tmp+1");
}
}
static void pre_fastcastx0(struct node *n)
{
if (hx_live) {
output("stx @tmp+1");
hx_live = 0;
} else
output("sta @tmp+1");
output("clr @tmp");
}
static int pri16_help(struct node *n, char *helper)
{
struct node *r = n->right;
unsigned v = r->value;
unsigned s = get_size(r->type);
printf(";pri16_help %x %s\n", n->op, helper);
/* Special case for cast first */
if (fast_castable(n)) {
if (get_size(r->right->type) == 2) {
if (do_pri16(r->right, "ld", pre_fastcast)) {
helper_s(n, helper);
return 1;
}
} else {
if (do_pri8(r->right, "ld", pre_fastcastx0, 1)) {
helper_s(n, helper);
return 1;
}
}
}
if (s == 1) {
if (do_pri8(n, "ld", pre_store16clx, 0)) {
helper_s(n, helper);
return 1;
}
} else if (s == 2) {
if (do_pri16(n, "ld", pre_store16)) {
/* Helper invalidates XA itself */
helper_s(n, helper);
return 1;
}
}
/* As we are saving via @tmp we can do these as well */
switch(r->op) {
case T_ARGUMENT:
v += frame_len + argbase;
case T_LOCAL:
v += sp;
/* TODO: SP biasing */
if (hx_contains(n))
return 1;
if (v < 1024) {
output("tsx");
adjust_hx(v);
set_hx_node(r);
hx_live = 1;
/*hx_*/helper_s(n, helper);
return 1;
}
break;
}
return 0;
}
static int hxpri16_help(struct node *n, const char *op)
{ if (do_hxpri16(n, op, pre_hx)) {
helper_s(n, op);
/* These helpers return the working value in XA */
hx_live = 0;
return 1;
}
return 0;
}
static int pri_help(struct node *n, char *helper)
{
unsigned s = get_size(n->type);
if (s == 1 && pri8_help(n, helper))
return 1;
else if (s == 2 && pri16_help(n, helper))
return 1;
return 0;
}
/* Do a helper op that wants the value in HX */
static int hx_help(struct node *n, char *helper)
{