forked from kspalaiologos/asm2ws
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasm_gen.c
405 lines (353 loc) · 12.6 KB
/
asm_gen.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "asm_common.h"
/* label ordering */
struct _label_t {
char * name;
vector(struct node_t *) references;
uint8_t declared;
uint32_t value;
};
struct label_state_t {
vector(struct _label_t) labels;
};
struct _label_t * getlabel(struct label_state_t * ctx, char * name) {
vector_foreach(struct _label_t, it, ctx->labels)
if(!strcmp(it->name, name))
return it;
return NULL;
}
void push_label(struct label_state_t * ctx, char * name, struct node_t * id) {
struct _label_t * ref = NULL;
if((ref = getlabel(ctx, name + 1)) != NULL) {
if(ref->declared == 1) {
fprintf(stderr, "wsi: label redeclared: '%s'\n", name);
exit(1);
}
ref->declared = 1;
vector_push_back(ref->references, id);
free(name);
return;
}
struct _label_t instance;
instance.declared = 1;
instance.references = NULL;
vector_push_back(instance.references, id);
instance.name = name + 1;
instance.value = vector_size(ctx->labels);
vector_push_back(ctx->labels, instance);
}
void pop_label(struct label_state_t * ctx, char * name, struct node_t * id) {
struct _label_t * ref = NULL;
if((ref = getlabel(ctx, name + 1)) != NULL) {
vector_push_back(ref->references, id);
free(name);
return;
}
struct _label_t instance;
instance.declared = 0;
instance.references = NULL;
vector_push_back(instance.references, id);
instance.name = name + 1;
instance.value = vector_size(ctx->labels);
vector_push_back(ctx->labels, instance);
}
int comparator(const void * a, const void * b) {
const struct _label_t * la = a, * lb = b;
size_t by_refs = vector_size(lb->references) - vector_size(la->references);
return by_refs ? by_refs : la->value - lb->value;
}
void dump_labels(struct label_state_t * ctx) {
vector_foreach(struct _label_t, it, ctx->labels) {
fprintf(stderr, "%s - refs: %lu\n", it->name, vector_size(it->references));
}
}
void finalize_labels(struct label_state_t * ctx) {
uint32_t n = 0;
if(ctx->labels == NULL)
return;
qsort(ctx->labels, vector_size(ctx->labels), sizeof(struct _label_t), comparator);
vector_foreach(struct _label_t, it, ctx->labels) {
if(!it->declared) {
fprintf(stderr, "wsi: unresolved reference: '%s'\n", it->name);
exit(1);
}
if(vector_size(it->references) == 1) {
it->references[0]->data1.type = IMM_LABEL;
} else {
vector_foreach(struct node_t *, it2, it->references)
(*it2)->data1.value = n;
++n;
}
free(it->name - 1);
vector_free(it->references);
}
vector_free(ctx->labels);
}
#define S fputc(' ', output)
#define T fputc('\t', output)
#define N fputc('\n', output)
#define rep(bound) for(x = 0; x < bound; x++)
void numeral(FILE * output, int32_t x) {
uint32_t u = x;
if(x < 0) { T; u = -u; } else { S; }
uint8_t b[32];
int8_t i = 0, j;
while(u) { b[i++] = u & 1; u >>= 1; }
for(j = i - 1; j >= 0; j--)
if(b[j]) T; else S;
N;
}
void unumeral(FILE * output, int32_t x) {
uint8_t b[32];
int8_t i = 0, j;
while(x) { b[i++] = x & 1; x >>= 1; }
for(j = i - 1; j >= 0; j--)
if(b[j]) T; else S;
N;
}
void asm_gen(FILE * output, vector(struct node_t) data, int optlevel) {
if(data) {
int32_t x;
// Pass 1: Labels to numbers.
struct label_state_t s = { NULL };
vector_foreach(struct node_t, it, data) {
if(it->type == LBL) {
it->data1.type = IMM_VALUE;
push_label(&s, it->data1.label, it);
continue;
}
if(it->data1.type == IMM_LABEL) {
it->data1.type = IMM_VALUE;
pop_label(&s, it->data1.label, it);
}
if(it->data2.type == IMM_LABEL) {
it->data2.type = IMM_VALUE;
pop_label(&s, it->data2.label, it);
}
if(it->data3.type == IMM_LABEL) {
it->data3.type = IMM_VALUE;
pop_label(&s, it->data3.label, it);
}
}
finalize_labels(&s);
// Pass 2: Optimizing the code.
asm_optimize(&data, optlevel);
// Pass 3: Generating code.
vector_foreach(struct node_t, it, data) {
switch(it->type) {
case GETC:
if(it->data1.type != IMM_NONE) {
S;S;numeral(output, it->data1.value); // GETC N => PUSH N
}
T;N;T;S; // GETC
break;
case PUTC:
if(it->data2.type != IMM_NONE) {
// REP PUTC X times
rep(it->data1.value) {
T;N;S;S;
}
break;
}
if(it->data1.type != IMM_NONE) {
S;S;numeral(output, it->data1.value); // PUTC N => PUSH N
}
T;N;S;S; // PUTC
break;
case GETN:
if(it->data1.type != IMM_NONE) {
S;S;numeral(output, it->data1.value); // GETN N => PUSH N
}
T;N;T;T; // GETN
break;
case PUTN:
if(it->data2.type != IMM_NONE) {
// REP PUTN X times
rep(it->data1.value) {
T;N;S;T;
}
break;
}
if(it->data1.type != IMM_NONE) {
S;S;numeral(output, it->data1.value); // PUTN N => PUSH N
}
T;N;S;T; // PUTN
break;
case PSH:
if(it->data1.type != IMM_NONE) {
S;S;numeral(output, it->data1.value); // PUSH N => PUSH N
} else {
S;S;numeral(output, 0); // PUSH => PUSH 0
}
break;
case DUP:
if(it->data1.type != IMM_NONE) {
// REP DUP N
rep(it->data1.value) {
S;N;S;
}
} else {
// DUP
S;N;S;
}
break;
case DROP:
if(it->data1.type != IMM_NONE) {
// REP DROP N
rep(it->data1.value) {
S;N;N;
}
} else {
// DROP
S;N;N;
}
break;
case ADD:
// TODO: REP ADD N, M - add N, M times
if(it->data2.type != IMM_NONE) {
// REP ADD N
// `ADD' N times.
rep(it->data1.value) {
T;S;S;S;
}
} else if(it->data1.type != IMM_NONE) {
// PSH N / ADD
S;S;numeral(output, it->data1.value);
T;S;S;S;
} else {
// ADD
T;S;S;S;
}
break;
case SUB:
// TODO: REP SUB N, M - subtract N, M times
if(it->data2.type != IMM_NONE) {
// REP SUB N
// `SUB' N times.
rep(it->data1.value) {
T;S;S;T;
}
} else if(it->data1.type != IMM_NONE) {
// PSH N / SUB
S;S;numeral(output, it->data1.value);
T;S;S;T;
} else {
// SUB
T;S;S;T;
}
break;
case MUL:
// TODO: REP MUL N, M - multiply N, M times
if(it->data2.type != IMM_NONE) {
// REP MUL N
// `MUL' N times.
rep(it->data1.value) {
T;S;S;N;
}
} else if(it->data1.type != IMM_NONE) {
// PSH N / MUL
S;S;numeral(output, it->data1.value);
T;S;S;N;
} else {
// MUL
T;S;S;N;
}
break;
case DIV:
// TODO: REP DIV N, M - divide N, M times
if(it->data2.type != IMM_NONE) {
// REP DIV N
// `DIV' N times.
rep(it->data1.value) {
T;S;T;S;
}
} else if(it->data1.type != IMM_NONE) {
// PSH N / DIV
if(it->data1.value == 0)
fprintf(stderr, "warn: %u:%u: division by zero.", it->line, it->column);
S;S;numeral(output, it->data1.value);
T;S;T;S;
} else {
// DIV
T;S;T;S;
}
break;
case MOD:
// TODO: REP MOD N, M - divide N, M times
if(it->data2.type != IMM_NONE) {
// REP MOD N
// `MOD' N times.
rep(it->data1.value) {
T;S;T;T;
}
} else if(it->data1.type != IMM_NONE) {
// PSH N / MOD
if(it->data1.value == 0)
fprintf(stderr, "warn: %u:%u: division by zero.", it->line, it->column);
S;S;numeral(output, it->data1.value);
T;S;T;T;
} else {
// MOD
T;S;T;T;
}
break;
case STO:
if(it->data2.type != IMM_NONE) {
// STO THERE, THAT
S;S;numeral(output, it->data1.value);
S;S;numeral(output, it->data2.value);
} else if(it->data1.type != IMM_NONE) {
// PSH N / STO
S;S;numeral(output, it->data1.value);
}
// else, STO
T;T;S;
break;
case RCL:
if(it->data1.type != IMM_NONE) {
// PSH N / RCL
S;S;numeral(output, it->data1.value);
}
// else, RCL
T;T;T;
break;
case CALL:
N;S;T;unumeral(output, it->data1.value);
break;
case JMP:
N;S;N;unumeral(output, it->data1.value);
break;
case BZ:
N;T;S;unumeral(output, it->data1.value);
break;
case BLTZ:
N;T;T;unumeral(output, it->data1.value);
break;
case RET:
N;T;N;
break;
case XCHG:
S;N;T;
break;
case STOP:
N;N;N;
break;
case LBL:
if(it->data1.type == IMM_LABEL)
break;
N;S;S;unumeral(output, it->data1.value);
break;
case COPY:
S;T;S;numeral(output, it->data1.value);
break;
case SLIDE:
S;T;N;numeral(output, it->data1.value);
break;
}
}
}
vector_free(data);
return;
}