-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcodegen.c
678 lines (532 loc) · 16 KB
/
codegen.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
/* codegen --- code generation routines 2022-08-12 */
/* Copyright (c) 2022 John Honniball. All rights reserved */
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include "codegen.h"
#define NAME_PREFIX ('_')
static int NextLabel = 0;
static FILE *Asm = NULL;
/* CodeGenInit --- initialise this module */
void CodeGenInit(void)
{
}
/* OpenAssemblerFile --- open the output file and write a header */
bool OpenAssemblerFile(const char fname[])
{
char asmName[256];
char *p;
strncpy(asmName, fname, sizeof (asmName) - 2);
p = strrchr(asmName, '.');
strcpy(p, ".asm");
Asm = fopen(asmName, "w");
fprintf(Asm, " setdp 0\n");
fprintf(Asm, " org $0400\n");
fprintf(Asm, "appEntry sts saveSP ; Save initial SP in case we call 'exit()'\n");
fprintf(Asm, " jsr _main\n");
fprintf(Asm, "appExit rts\n");
fprintf(Asm, "saveSP fdb 0 ; Space to store initial SP\n");
fprintf(Asm, "_exit lds saveSP ; Recover initial SP\n");
fprintf(Asm, " bra appExit ; Branch to instruction after 'main()'\n");
fprintf(Asm, "_vduchar tfr b,a ; Move char into A register\n");
fprintf(Asm, " jmp $a020 ; Send one char to the VDU\n");
fprintf(Asm, "_vdustr tfr d,x ; Move pointer into X register\n");
fprintf(Asm, " jmp $a014 ; Print a nul-terminated string\n");
fprintf(Asm, "_getchar jsr $a252 ; Get char and show cursor\n");
fprintf(Asm, " tfr a,b ; Move returned ASCII char into LSB of D register\n");
fprintf(Asm, " clra ; Make sure MSB is zero\n");
fprintf(Asm, " rts\n");
fprintf(Asm, "_hex2ou tfr b,a ; Move char into A register\n");
fprintf(Asm, " jmp $a17c ; Call hex2ou in EPROM\n");
fprintf(Asm, "_hex4ou jmp $a189 ; Call hex4ou in EPROM\n");
fprintf(Asm, "_hex8ou pshs d ; Save D\n");
fprintf(Asm, " tfr w,d ; Transfer hi word to D\n");
fprintf(Asm, " jsr $a189 ; Call hex4ou in EPROM (hi)\n");
fprintf(Asm, " puls d ; Restore D\n");
fprintf(Asm, " jmp $a189 ; Call hex4ou in EPROM (lo)\n");
return (true);
}
/* CloseAssemblerFile --- write a trailer and close the output file */
bool CloseAssemblerFile(void)
{
fprintf(Asm, " end appEntry\n");
fclose(Asm);
return (true);
}
/* Emit --- emit a single assembly-language instruction */
int Emit(const char inst[], const char oper[], const char comment[])
{
fprintf(Asm, " %-4s %-32s ; %s\n", inst, oper, comment);
return (1);
}
/* AllocLabel --- allocate a new label for a given purpose */
int AllocLabel(const char purpose)
{
return (NextLabel++);
}
/* EmitLabel --- emit an allocated label into the assembly-language file */
void EmitLabel(const int label)
{
fprintf(Asm, "l%04d\n", label);
}
/* EmitFunctionEntry --- emit a label and setup code for a function */
void EmitFunctionEntry(const char name[], const int nBytes, const int nRegister)
{
fprintf(Asm, "%c%-44s ; Function entry point\n", NAME_PREFIX, name);
if (nRegister == 0) {
Emit("pshs", "u", "Save old frame pointer");
}
else {
Emit("pshs", "u,y", "Save old frame pointer & register variable");
}
Emit("tfr", "s,u", "Make new frame pointer");
if (nBytes != 0) {
char frame[32];
snprintf(frame, sizeof (frame), "-%d,s", nBytes);
Emit("leas", frame, "Allocate stack frame");
}
}
/* EmitFunctionExit --- emit return label and function exit code */
void EmitFunctionExit(const int returnLabel, const int nRegister)
{
EmitLabel(returnLabel);
Emit("tfr", "u,s", "Deallocate stack frame");
if (nRegister == 0) {
Emit("puls", "u", "Restore frame pointer");
}
else {
Emit("puls", "u,y", "Restore frame pointer & register variable");
}
Emit("rts", "", "Return to caller");
}
/* EmitStackCleanup --- emit code to clean up stack after a funtion call */
void EmitStackCleanup(const int nBytes)
{
if (nBytes != 0) {
char buf[32];
snprintf(buf, sizeof (buf), "%d,s", nBytes);
Emit("leas", buf, "clear actual parameters off stack");
}
}
/* EmitStaticCharArray --- emit declaration for an initialised char array or string */
void EmitStaticCharArray(const struct StringConstant *sc, const char name[])
{
char target[8];
char bytes[64];
int i, n;
snprintf(target, sizeof (target), "l%04d", sc->label);
bytes[0] = '\0';
if (sc->sLength > 7) {
n = 7;
}
else {
n = sc->sLength;
}
for (i = 0; i < n; i++) {
char hex[8];
sprintf(hex, "$%02x", sc->sValue[i] & 0xff);
strcat(bytes, hex);
if (i < (n - 1)) {
strcat(bytes, ",");
}
}
fprintf(Asm, "%-7s fcb %-32s ; const char %s[%d] = %s\n", target, bytes, name, sc->sLength - 1, sc->str);
while (i < sc->sLength) {
n += 7;
if (n > sc->sLength) {
n = sc->sLength;
}
bytes[0] = '\0';
for ( ; i < n; i++) {
char hex[8];
sprintf(hex, "$%02x", sc->sValue[i] & 0xff);
strcat(bytes, hex);
if (i < (n - 1)) {
strcat(bytes, ",");
}
}
fprintf(Asm, " fcb %s\n", bytes);
}
}
/* GenTargetOperand --- generate the assembler operand to address a scalar variable */
static void GenTargetOperand(const struct Symbol *const sym, const int offset, char target[])
{
if (offset == 0) {
switch (sym->storageClass) {
case SCEXTERN:
snprintf(target, MAXNAME + 1, "%c%s", NAME_PREFIX, sym->name);
break;
case SCSTATIC:
snprintf(target, MAXNAME + 1, "l%04d", sym->label);
break;
case SCAUTO:
snprintf(target, MAXNAME + 1, "%d,u", sym->fpOffset);
break;
case SCREGISTER:
snprintf(target, MAXNAME + 1, "#0");
break;
}
}
else {
switch (sym->storageClass) {
case SCEXTERN:
snprintf(target, MAXNAME + 1, "%c%s+%d", NAME_PREFIX, sym->name, offset);
break;
case SCSTATIC:
snprintf(target, MAXNAME + 1, "l%04d+%d", sym->label, offset);
break;
case SCAUTO:
snprintf(target, MAXNAME + 1, "%d,u", sym->fpOffset + offset);
break;
case SCREGISTER:
snprintf(target, MAXNAME + 1, "#0");
break;
}
}
}
/* storageClassAsString --- generate a string representation of a storage class */
static char *storageClassAsString(const int sc)
{
char *str = "";
switch (sc) {
case SCEXTERN:
str = "extern";
break;
case SCSTATIC:
str = "static";
break;
case SCAUTO:
str = "auto";
break;
case SCREGISTER:
str = "register";
break;
}
return (str);
}
/* typeAsString --- generate a string representation of a type */
static char *typeAsString(const int ty)
{
char *str = "";
switch (ty) {
case T_CHAR:
str = "char";
break;
case T_UCHAR:
str = "unsigned char";
break;
case T_INT:
str = "int";
break;
case T_UINT:
str = "unsigned int";
break;
case T_FLOAT:
str = "float";
break;
case T_DOUBLE:
str = "double";
break;
}
return (str);
}
/* LoadScalar --- load a scalar variable into D or Q */
void LoadScalar(const struct Symbol *const sym)
{
char comment[MAXNAME + 64];
const char *sc = storageClassAsString(sym->storageClass);
const char *ty = typeAsString(sym->type);
snprintf(comment, sizeof (comment), "Load %s %s %s", sc, ty, sym->name);
if (sym->storageClass == SCREGISTER) {
switch (sym->type) {
case T_CHAR:
Emit("tfr", "y,d", comment);
Emit("sex", "", "Sign extend to 16 bits");
break;
case T_UCHAR:
Emit("tfr", "y,d", comment);
Emit("clra", "", "No sign extension");
break;
case T_SHORT:
case T_USHORT:
case T_INT:
case T_UINT:
Emit("tfr", "y,d", comment);
break;
}
}
else {
char target[MAXNAME + 1];
GenTargetOperand(sym, 0, target);
switch (sym->type) {
case T_CHAR:
Emit("ldb", target, comment);
Emit("sex", "", "Sign extend to 16 bits");
break;
case T_UCHAR:
Emit("ldb", target, comment);
Emit("clra", "", "No sign extension");
break;
case T_SHORT:
case T_USHORT:
case T_INT:
case T_UINT:
Emit("ldd", target, comment);
break;
case T_LONG:
case T_ULONG:
Emit("ldq", target, comment);
break;
case T_FLOAT:
Emit("ldq", target, comment);
break;
case T_DOUBLE:
Emit("ldq", target, comment);
Emit("nop", "", "How to load low 32 bits?");
break;
}
}
}
/* StoreScalar --- store a scalar variable from D or Q */
void StoreScalar(const struct Symbol *const sym)
{
char comment[MAXNAME + 64];
const char *sc = storageClassAsString(sym->storageClass);
const char *ty = typeAsString(sym->type);
snprintf(comment, sizeof (comment), "Store %s %s %s", sc, ty, sym->name);
if (sym->storageClass == SCREGISTER) {
switch (sym->type) {
case T_CHAR:
case T_UCHAR:
Emit("tfr", "d,y", comment);
break;
case T_SHORT:
case T_USHORT:
case T_INT:
case T_UINT:
Emit("tfr", "d,y", comment);
break;
}
}
else {
char target[MAXNAME + 1];
GenTargetOperand(sym, 0, target);
switch (sym->type) {
case T_CHAR:
case T_UCHAR:
Emit("stb", target, comment);
break;
case T_SHORT:
case T_USHORT:
case T_INT:
case T_UINT:
Emit("std", target, comment);
break;
case T_LONG:
case T_ULONG:
Emit("stq", target, comment);
break;
case T_FLOAT:
Emit("stq", target, comment);
break;
case T_DOUBLE:
Emit("stq", target, comment);
GenTargetOperand(sym, 4, target);
Emit("stq", target, "Store low 32 bits");
break;
case T_VOID:
break;
}
}
}
/* LoadIntConstant --- load an int constant into a given register */
void LoadIntConstant(const int val, const int reg, const char comment[])
{
char immediate[8];
snprintf(immediate, sizeof (immediate), "#%d", val);
switch (reg) {
case 'D':
case 'd':
Emit("ldd", immediate, comment);
break;
case 'X':
case 'x':
Emit("ldx", immediate, comment);
break;
case 'Y':
case 'y':
Emit("ldy", immediate, comment);
break;
}
}
/* LoadLabelAddr --- load the address of a generated label */
void LoadLabelAddr(const int label, const char comment[])
{
char target[8];
snprintf(target, sizeof (target), "#l%04d", label);
Emit("ldd", target, comment);
}
/* EmitExternScalar --- emit declaration for an extern scalar variable */
void EmitExternScalar(const struct Symbol *const sym, const int init, const double fInit)
{
char name[MAXNAME + 1];
char *storage = "";
int b1, b2, b3, b4, b5, b6, b7, b8;
union {
float f;
unsigned char b[4];
} f;
union {
double d;
unsigned char b[4];
} d;
if (sym->storageClass == SCEXTERN) {
snprintf(name, sizeof (name), "%c%s", NAME_PREFIX, sym->name);
}
else if (sym->storageClass == SCSTATIC) {
snprintf(name, sizeof (name), "l%04d", sym->label);
storage = "static ";
}
else {
fprintf(stderr, "Only 'extern' or 'static' is valid\n");
}
if (sym->pLevel == 0) {
switch (sym->type) {
case T_CHAR:
case T_UCHAR:
fprintf(Asm, "%-30s fcb %d ; %schar %s = %d\n", name, init, storage, sym->name, init);
break;
case T_SHORT:
case T_USHORT:
case T_INT:
case T_UINT:
fprintf(Asm, "%-30s fdb %d ; %sint %s = %d\n", name, init, storage, sym->name, init);
break;
case T_LONG:
case T_ULONG:
fprintf(Asm, "%-30s fqb %d ; %slong int %s = %d\n", name, init, storage, sym->name, init);
break;
case T_FLOAT:
f.f = fInit;
b1 = f.b[3]; // 6809 is big-endian
b2 = f.b[2];
b3 = f.b[1];
b4 = f.b[0];
fprintf(Asm, "%-30s fcb %d,%d,%d,%d ; %sfloat %s = %g\n", name, b1, b2, b3, b4, storage, sym->name, fInit);
break;
case T_DOUBLE:
d.d = fInit;
b1 = d.b[7]; // 6809 is big-endian
b2 = d.b[6];
b3 = d.b[5];
b4 = d.b[4];
b5 = d.b[3];
b6 = d.b[2];
b7 = d.b[1];
b8 = d.b[0];
fprintf(Asm, "%-30s fcb %d,%d,%d,%d,%d,%d,%d,%d ; %sdouble %s = %g\n", name, b1, b2, b3, b4, b5, b6, b7, b8, storage, sym->name, fInit);
break;
}
}
else {
fprintf(Asm, "%-30s fdb %d ; %spointer %s = %d\n", name, init, storage, sym->name, init);
}
}
/* EmitJump --- emit a jump (possibly relative) to a given label */
void EmitJump(const int label, const char comment[])
{
char target[8];
snprintf(target, sizeof (target), "l%04d", label);
Emit("jmp", target, comment);
}
/* EmitBranchIfEqual --- emit a BEQ to a label */
void EmitBranchIfEqual(const int label, const char comment[])
{
char target[8];
snprintf(target, sizeof (target), "l%04d", label);
Emit("lbeq", target, comment);
}
/* EmitBranchNotEqual --- emit a BNE to a label */
void EmitBranchNotEqual(const int label, const char comment[])
{
char target[8];
snprintf(target, sizeof (target), "l%04d", label);
Emit("lbne", target, comment);
}
/* EmitIncScalar --- emit an INC for a scalar variable */
void EmitIncScalar(const struct Symbol *const sym, const int amount)
{
char op[30];
char comment[MAXNAME + 64];
const char *sc = storageClassAsString(sym->storageClass);
const char *ty = typeAsString(sym->type);
char *incDec = "inc";
if (amount < 0) {
incDec = "dec";
}
snprintf(comment, sizeof (comment), "%s %s %s %s", incDec, sc, ty, sym->name);
if (sym->storageClass == SCREGISTER) {
snprintf(op, sizeof (op), "%d,y", amount);
switch (sym->type) {
case T_CHAR:
case T_UCHAR:
case T_SHORT:
case T_USHORT:
case T_INT:
case T_UINT:
Emit("leay", op, comment);
break;
}
}
else {
char target[MAXNAME + 1];
GenTargetOperand(sym, 0, target);
snprintf(op, sizeof (op), "%d,x", amount);
switch (sym->type) {
case T_CHAR:
case T_UCHAR:
if (amount == 1) {
Emit("inc", target, comment);
}
else if (amount == -1) {
Emit("dec", target, comment);
}
else {
fprintf(stderr, "Inc/Dec by more than 1\n");
}
break;
case T_SHORT:
case T_USHORT:
case T_INT:
case T_UINT:
Emit("ldx", target, comment);
Emit("leax", op, incDec);
Emit("stx", target, comment);
break;
case T_LONG:
case T_ULONG:
Emit("nop", "", comment);
break;
case T_FLOAT:
Emit("nop", "", comment);
break;
case T_DOUBLE:
Emit("nop", "", comment);
break;
}
}
}
/* EmitCompareIntConstant --- code to compare an integer constant */
void EmitCompareIntConstant(const int compare, const char comment[])
{
char cmpArg[16];
snprintf(cmpArg, sizeof (cmpArg), "#%d", compare);
Emit("cmpd", cmpArg, comment);
}
/* EmitCallFunction --- code to call a function */
void EmitCallFunction(const char name[], const char comment[])
{
char target[30];
snprintf(target, sizeof (target), "%c%s", NAME_PREFIX, name);
Emit("jsr", target, comment);
}