-
Notifications
You must be signed in to change notification settings - Fork 13
/
backend-threadcode.c
481 lines (437 loc) · 9.88 KB
/
backend-threadcode.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
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include "compiler.h"
#include "backend.h"
/* For now assume 8/16bit */
#define BYTE(x) (((unsigned)(x)) & 0xFF)
#define WORD(x) (((unsigned)(x)) & 0xFFFF)
/*
* State for the current function
*/
static unsigned frame_len; /* Number of bytes of stack frame */
static unsigned sp; /* Stack pointer offset tracking */
static unsigned frame_off; /* Space between arg and local */
static unsigned has_fp; /* Uses a frame pointer ? */
static unsigned big_endian; /* Machine word endianness */
#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;
}
/* 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 for the thread code
* 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) {
if (r->op == T_ARGUMENT)
r->value += frame_len;
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 += frame_len;
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;
}
return n;
}
/* 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%s\n", codeseg);
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)
{
printf("_%s:\n", name);
}
/* Generate the stack frame */
void gen_frame(unsigned size, unsigned aframe)
{
frame_len = size + frame_off;
sp += size;
gen_helpcall(NULL);
printf("fnenter\n");
gen_value(UINT, size);
}
void gen_epilogue(unsigned size, unsigned argsize)
{
if (sp != size) {
error("sp");
}
sp -= size;
gen_helpcall(NULL);
printf("fnexit\n");
}
void gen_label(const char *tail, unsigned n)
{
printf("L%d%s:\n", n, tail);
}
unsigned gen_exit(const char *tail, unsigned n)
{
gen_helpcall(NULL);
printf("fnexit\n");
return 1;
}
void gen_jump(const char *tail, unsigned n)
{
printf("\t.word __jump\n");
printf("\t.word L%d%s\n", n, tail);
}
void gen_jfalse(const char *tail, unsigned n)
{
printf("\t.word __jfalse\n");
printf("\t.word L%d%s\n", n, tail);
}
void gen_jtrue(const char *tail, unsigned n)
{
printf("\t.word __jtrue\n");
printf("\t.word L%d%s\n", n, tail);
}
void gen_switch(unsigned n, unsigned type)
{
gen_helpcall(NULL);
printf("switch");
helper_type(type, 0);
printf("\n\t.word Sw%d\n", n);
}
void gen_switchdata(unsigned n, unsigned size)
{
printf("Sw%d:\n", n);
printf("\t.word %d\n", size);
}
void gen_case(unsigned tag, unsigned entry)
{
printf("Sw%d_%d:\n", tag, entry);
}
void gen_case_label(unsigned tag, unsigned entry)
{
printf("Sw%d_%d:\n", tag, entry);
}
void gen_case_data(unsigned tag, unsigned entry)
{
printf("\t.word Sw%d_%d\n", tag, entry);
}
void gen_helpcall(struct node *n)
{
printf("\t.word __");
}
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:
/* Word endianness of target */
if (big_endian) {
printf("\t.word %d\n", (unsigned) ((value >> 16) & 0xFFFF));
printf("\t.word %d\n", (unsigned) (value & 0xFFFF));
} else {
printf("\t.word %d\n", (unsigned) (value & 0xFFFF));
printf("\t.word %d\n", (unsigned) ((value >> 16) & 0xFFFF));
}
break;
default:
error("unsuported type");
}
}
void gen_start(void)
{
if (cpu == 1802) {
frame_off = 4; /* Two words - old FP and old BPC */
has_fp = 1; /* Offsets are FP relative */
big_endian = 0; /* Little endian */
} else {
fprintf(stderr, "threadcode: unknown CPU\n");
exit(1);
}
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;
}
unsigned gen_push(struct node *n)
{
/* Our push will put the object on the stack, so account for it */
helper(n, "push");
sp += get_stack_size(n->type);
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)
{
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:
gen_helpcall(NULL);
printf("cleanup\n");
gen_value(UINT, n->right->value);
sp -= n->right->value;
return 1;
}
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;
}
/*
* Allow the code generator to shortcut trees it knows
*/
unsigned gen_shortcut(struct node *n)
{
struct node *l = n->left;
struct node *r = n->right;
/* The comma operator discards the result of the left side, then
evaluates the right. Avoid pushing/popping and generating stuff
that is surplus */
if (n->op == T_COMMA) {
l->flags |= NORETURN;
codegen_lr(l);
/* Parent determines child node requirements */
r->flags |= (n->flags & NORETURN);
codegen_lr(r);
return 1;
}
return 0;
}
unsigned gen_node(struct node *n)
{
unsigned v = n->value;
unsigned nr = n->flags & NORETURN;
/* Function call arguments are special - they are removed by the
act of call/return and reported via T_CLEANUP */
if (n->left && n->op != T_ARGCOMMA && n->op != T_FUNCCALL && n->op != T_CALLNAME)
sp -= get_stack_size(n->left->type);
switch(n->op) {
case T_NREF:
helper(n, "nref");
printf("\t.word _%s+%d\n", namestr(n->snum), v);
return 1;
case T_LBREF:
helper(n, "lbref");
printf("\t.word T%ds+%d\n", n->val2, v);
return 1;
case T_LREF:
if (nr)
return 1;
helper(n, "lref");
printf("\t.word %d\n", v);
return 1;
case T_NSTORE:
helper(n, "nstore");
printf("\t.word _%s+%d\n", namestr(n->snum), v);
return 1;
case T_LBSTORE:
helper(n, "lbstore");
printf("\t.word T%ds+%d\n", n->val2, v);
return 1;
case T_LSTORE:
helper(n, "lstore");
printf("\t.word %d\n", v);
return 1;
case T_CALLNAME:
helper(n, "callfn");
printf("\t.word _%s+%d\n", namestr(n->snum), v);
return 1;
case T_ARGUMENT:
/* Turn argument into local effectively */
helper(n, "loadl");
if (!has_fp)
v += sp;
printf("\t.word %d\n", v + frame_len);
return 1;
case T_LOCAL:
/* Adjust offsets of convenience */
helper(n, "loadl");
if (!has_fp)
v += sp;
printf("\t.word %d\n", v);;
return 1;
case T_FUNCCALL:
/* Can't use helper() directly as type is return type */
printf("\t.word __callfunc\n");
return 1;
}
return 0;
}