-
Notifications
You must be signed in to change notification settings - Fork 50
/
as.c
416 lines (353 loc) · 9.66 KB
/
as.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "elf.h"
#include "opcode.h"
#include "private.h"
#include "vm.h"
#include "vm_codegen.h"
#define NOT_IN_QUOTE '\0'
#define IS_QUOTE(qs, c) \
((qs == NOT_IN_QUOTE) ? (c == '"' || c == '\'') : c == qs)
#define SET_QUOTE(qs, c) \
do { \
qs = c; \
} while (0)
#define IS_ESC(c) (c == '\\')
/*
* A customized version of strsep. It is different in that:
*
* 1. Takes care of quoting - when quote characters appear, it will skip the
* comparison to characters specified in 'sep'.
* 2. Original strsep does not skip consecutive characters that all appear in
* 'sep'. It would return one NULL for every appearing characters. This
* variant, instead, skips all of them but return only once. This is the
* more desired behavior of an assembly parser.
*
* See 'man strsep' for more information on original strsep().
*/
static char *quoted_strsep(char **str, const char *sep)
{
char *p, *head;
char quote_sym = NOT_IN_QUOTE;
if (!str || *str == NULL || **str == '\0' || !sep)
return NULL;
p = *str;
/* skip all prefix characters in 'sep' */
while (*p != '\0') {
if (strchr(sep, *p) == NULL)
break;
p++;
}
if (*p == '\0') {
*str = p;
return NULL;
}
head = p;
/* run through all character and consider quotes */
while (1) {
if (*p == '\0') {
*str = p;
break;
}
if (quote_sym == NOT_IN_QUOTE) {
if (strchr(sep, *p)) {
*p++ = '\0';
*str = p;
break;
}
if (IS_QUOTE(quote_sym, *p))
SET_QUOTE(quote_sym, *p);
} else {
if (IS_QUOTE(quote_sym, *p))
SET_QUOTE(quote_sym, NOT_IN_QUOTE);
if (IS_ESC(*p)) {
p++;
if (*p == '\0') {
*str = p;
break;
}
}
}
p++;
}
return head;
}
/*
* Duplicate (and process escaping characters) the string and return it.
* Return NULL if the opening and closing quotes are unbalanced.
*
* See 'man strdup' for more information on original strdup().
*/
static char *quoted_strdup(const char *data)
{
int len;
const char *p;
char *dup;
char *q;
char quote_sym = NOT_IN_QUOTE;
if (IS_QUOTE(quote_sym, data[0])) {
SET_QUOTE(quote_sym, data[0]);
len = 0;
p = &data[1];
while (*p != '\0' && !IS_QUOTE(quote_sym, *p)) {
if (*p == '\\')
p++;
len++;
p++;
}
if (*p == '\0') /* unbalanced quotes */
return NULL;
dup = malloc(len + 1);
q = dup;
p = &data[1];
while (*p != data[0]) {
if (*p != '\\')
*q = *p;
else {
p++;
switch (*p) {
case 't':
*q = '\t';
break;
case 'n':
*q = '\n';
break;
case 'r':
*q = '\r';
break;
case '0':
*q = '\0';
break;
default:
*q = *p;
break;
}
}
p++;
q++;
}
*q = '\0';
return dup;
} else {
return strdup(data);
}
}
static const struct instruction *find_inst(const char *name)
{
for (int i = 0; instrs[i].name; ++i)
if (!strcmp(name, instrs[i].name))
return &instrs[i];
return NULL;
}
static inline vm_operand make_operand(vm_env *env, char *line, const char *data)
{
vm_operand op;
if (data == NULL || data[0] == ';') {
printf("Error: missing operand in the following line\n");
printf(" %s\n", line);
free(line);
exit(-1);
}
switch (data[0]) {
case '$': {
int temp = atoi(data + 1); /* filter the first char */
op.type = CONST;
op.value.id = vm_add_const(env, INT, &temp);
break;
}
case '#':
op.type = TEMP;
op.value.id = atoi(data + 1);
break;
case '"':
case '\'':
op.type = CONST;
op.value.id = vm_add_const(env, STR, quoted_strdup(data));
break;
case ':': {
int label = vm_find_label(env, &data[1]);
if (~label) {
op.type = CONST;
op.value.id = vm_add_const(env, INT, &label);
} else {
printf("Error: specified label name '%s' not found.\n", data);
free(line);
exit(-1);
}
break;
}
default:
printf(
"Error: please specify operand type for '%s' in the following "
"line\n"
" %s\n\n"
"Supported types:\n"
" $ (constant integer)\n"
" # (temp integer)\n"
" \" (string literal)\n"
" : (label name)\n",
data, line);
free(line);
exit(-1);
break;
}
return op;
}
static inline int make_result(vm_env *env, char *line, const char *data)
{
if (data == NULL || data[0] == ';') {
printf(
"Error: missing result in the following line\n"
" %s\n",
line);
free(line);
exit(-1);
}
if (data[0] != '#') {
printf(
"Error: please specify result for '%s' in the following line\n"
" %s\n\n"
"Supported types:\n"
" # (temp integer)\n",
data, line);
free(line);
exit(-1);
}
return atoi(data + 1);
}
static void assemble_line(vm_env *env, char *line)
{
char *line_backup = strdup(line);
char *mnemonic = quoted_strsep(&line, " ");
char *op1 = quoted_strsep(&line, " ");
char *op2 = quoted_strsep(&line, " ");
char *result = quoted_strsep(&line, " ");
vm_inst new_inst;
const struct instruction *inst = find_inst(mnemonic);
if (!inst)
FATALX(1, "instruction `%s' not found\n", mnemonic);
memset(&new_inst, 0, sizeof(vm_inst));
new_inst.opcode = inst->opcode;
if (inst->has_op1)
new_inst.op1 = make_operand(env, line_backup, op1);
if (inst->has_op2)
new_inst.op2 = make_operand(env, line_backup, op2);
if (inst->has_result)
new_inst.result = make_result(env, line_backup, result);
vm_add_inst(env, new_inst);
free(line_backup);
}
void assemble_from_fd(vm_env *env, int fd)
{
char *line = NULL;
size_t size = 0;
FILE *fp = fdopen(fd, "r");
int insts_count = 0;
/* preprocess label */
while (getline(&line, &size, fp) != -1) {
if (line[0] == ';' || line[0] == '\n')
continue;
/* Remove trailing newline feed */
line[strcspn(line, "\r\n")] = 0;
if (line[strlen(line) - 1] == ':') {
line[strlen(line) - 1] = 0;
vm_make_label(env, line, insts_count);
} else
insts_count++;
}
fseek(fp, 0, SEEK_SET);
while (getline(&line, &size, fp) != -1) {
if (line[0] == ';' || line[0] == '\n' || line[strlen(line) - 2] == ':')
continue;
/* Remove trailing newline feed */
line[strcspn(line, "\r\n")] = 0;
assemble_line(env, line);
}
free(line);
}
#define ALIGN_TYPE long
#define ALIGN_WIDTH sizeof(ALIGN_TYPE)
static inline int mem_not_empty(void *p, size_t len)
{
void *c = p;
while (c < (p + len) && ((ALIGN_TYPE) c % ALIGN_WIDTH)) {
if (*(char *) p)
return 1;
c++;
}
while (c < (p + len - ALIGN_WIDTH + 1)) {
if (*(ALIGN_TYPE *) c)
return 1;
c += ALIGN_WIDTH;
}
while (c < (p + len)) {
if (*(char *) p)
return 1;
c++;
}
return 0;
}
int write_to_elf(vm_env *env, int fd)
{
vm_seg_info *info_head;
vm_seg_info *s;
elf *e;
int sz;
int len;
unsigned short ph_rows;
ph_rows = vm_get_seg_info(env, &info_head);
e = elf_init();
elf_new_prog_hdrs(e, ph_rows);
s = info_head;
for (int i = 0; i < ph_rows; i++, s = vm_get_next_seg_info(s)) {
size_t ssz = vm_get_seg_size(s);
void *mem = vm_get_seg_mem(s);
elf_set_prog_hdr(e, i, ssz, ssz * mem_not_empty(mem, ssz));
}
len = elf_write_file_hdr(fd, e);
if (len < 0) {
vm_free_seg_info(info_head);
return -1;
}
s = info_head;
for (int i = 0; i < ph_rows; i++, s = vm_get_next_seg_info(s)) {
void *mem = vm_get_seg_mem(s);
if ((sz = elf_write_prog_seg(e, fd, i, mem)) < 0) {
vm_free_seg_info(info_head);
return -1;
} else
len += sz;
}
vm_free_seg_info(info_head);
return len;
}
int load_from_elf(vm_env *env, int fd)
{
vm_seg_info *info_head = NULL;
vm_seg_info *s;
elf *e;
unsigned short ph_rows;
e = elf_read_file_hdr(fd);
if (!e)
return -1;
ph_rows = elf_read_prog_hdrs(e, fd);
for (int i = 0; i < ph_rows; i++) {
char *mem = NULL;
size_t sz = 0;
if (elf_read_prog_seg(e, fd, i, &mem, &sz) < 0)
; /* TODO */
s = vm_new_seg_info(mem, sz);
if (!info_head) {
info_head = s;
} else {
vm_append_seg_info(info_head, s);
}
}
if (vm_set_seg_info(env, info_head) < 0) {
vm_seg_info_free_list(info_head);
return -1;
}
vm_seg_info_free_list(info_head);
return -1;
}