-
Notifications
You must be signed in to change notification settings - Fork 13
/
backend-default.c
306 lines (268 loc) · 5.53 KB
/
backend-default.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
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include "compiler.h"
#include "backend.h"
#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 */
/* 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
* 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)
{
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.code\n");
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;
sp += size;
gen_helpcall(NULL);
printf("enter\n");
gen_value(UINT, size);
}
void gen_epilogue(unsigned size, unsigned argsize)
{
if (sp != size) {
error("sp");
}
sp -= size;
gen_helpcall(NULL);
printf("exit\n");
gen_value(UINT, size);
printf("\tret\n");
}
void gen_label(const char *tail, unsigned n)
{
printf("L%d%s:\n", n, tail);
}
unsigned gen_exit(const char *tail, unsigned n)
{
printf("\tjmp L%d%s\n", n, tail);
return 0;
}
void gen_jump(const char *tail, unsigned n)
{
printf("\tjmp L%d%s\n", n, tail);
}
void gen_jfalse(const char *tail, unsigned n)
{
printf("\tjz L%d%s\n", n, tail);
}
void gen_jtrue(const char *tail, unsigned n)
{
printf("\tjnz 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("\tcall __");
}
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:
/* We are little endian */
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)
{
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 */
sp += get_stack_size(n->type);
return 0;
}
/*
* 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)
{
return 0;
}
unsigned gen_node(struct node *n)
{
/* 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)
sp -= get_stack_size(n->left->type);
return 0;
}