-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparse.c
More file actions
578 lines (517 loc) · 13 KB
/
parse.c
File metadata and controls
578 lines (517 loc) · 13 KB
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
/*
* Copyright (C) 2012 Sean "rioki" Farrell
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "parse.h"
#include "scan.h"
#include "cpu.h"
static int token = 0;
static int next_token = 0;
static variant_t value;
static variant_t next_value;
static int result = 0;
static program_t* prog;
void read_token()
{
clear_variant(&value);
token = next_token;
value = next_value;
init_variant(&next_value);
next_token = scan_token(&next_value);
}
int add_instruction(program_t* prog, instruction_t instr)
{
assert(prog != NULL);
assert(prog->code_lines <= prog->code_size);
if (prog->code_lines == prog->code_size)
{
if (prog->code_size == 0)
{
prog->code = (instruction_t*)malloc(32 * sizeof(instruction_t));
prog->code_size = 32;
}
else
{
unsigned int new_size = prog->code_size * 2;
prog->code = (instruction_t*)realloc(prog->code, new_size * sizeof(instruction_t));
prog->code_size = new_size;
}
}
assert(prog->code_lines < prog->code_size);
prog->code[prog->code_lines] = instr;
prog->code_lines++;
return 0;
}
void read_label(char** label)
{
assert(label != NULL);
read_token();
assert(token == LABEL_TOKEN);
assert(value.type == STRING_TYPE);
*label = strdup(value.str);
}
int is_opcode(int token)
{
switch (token)
{
case SET_TOKEN:
case ADD_TOKEN:
case SUB_TOKEN:
case MUL_TOKEN:
case DIV_TOKEN:
case MOD_TOKEN:
case SHL_TOKEN:
case SHR_TOKEN:
case AND_TOKEN:
case BOR_TOKEN:
case XOR_TOKEN:
case IFE_TOKEN:
case IFN_TOKEN:
case IFG_TOKEN:
case IFB_TOKEN:
case JSR_TOKEN:
return 1;
default:
return 0;
}
}
int map_opcode(int token)
{
switch (token)
{
case SET_TOKEN:
return SET_OPCODE;
case ADD_TOKEN:
return ADD_OPCODE;
case SUB_TOKEN:
return SUB_OPCODE;
case MUL_TOKEN:
return MUL_OPCODE;
case DIV_TOKEN:
return DIV_OPCODE;
case MOD_TOKEN:
return MOD_OPCODE;
case SHL_TOKEN:
return SHL_OPCODE;
case SHR_TOKEN:
return SHR_OPCODE;
case AND_TOKEN:
return AND_OPCODE;
case BOR_TOKEN:
return BOR_OPCODE;
case XOR_TOKEN:
return XOR_OPCODE;
case IFE_TOKEN:
return IFE_OPCODE;
case IFN_TOKEN:
return IFN_OPCODE;
case IFG_TOKEN:
return IFG_OPCODE;
case IFB_TOKEN:
return IFB_OPCODE;
case JSR_TOKEN:
return JSR_OPCODE;
default:
assert(0 && "Should never happen.");
return 0;
}
}
void read_opcode(int* opcode)
{
assert(opcode != NULL);
read_token();
if (is_opcode(token))
{
*opcode = map_opcode(token);
}
else
{
fprintf(stderr, "%s:%d: error: Expected opcode but got %s.\n", get_scan_file(), get_scan_line(), get_token_name(token));
result = -1;
}
}
int is_value_token(unsigned int token)
{
switch (token)
{
case A_TOKEN:
case B_TOKEN:
case C_TOKEN:
case X_TOKEN:
case Y_TOKEN:
case Z_TOKEN:
case I_TOKEN:
case J_TOKEN:
case POP_TOKEN:
case PEEK_TOKEN:
case PUSH_TOKEN:
case SP_TOKEN:
case PC_TOKEN:
case O_TOKEN:
case IDENTIFIER_TOKEN:
case INTEGER_TOKEN:
return 1;
default:
return 0;
}
}
int is_symbolic(unsigned int token)
{
switch (token)
{
case A_TOKEN:
case B_TOKEN:
case C_TOKEN:
case X_TOKEN:
case Y_TOKEN:
case Z_TOKEN:
case I_TOKEN:
case J_TOKEN:
case POP_TOKEN:
case PEEK_TOKEN:
case PUSH_TOKEN:
case SP_TOKEN:
case PC_TOKEN:
case O_TOKEN:
return 1;
default:
return 0;
}
}
void read_argument(arg_t* arg)
{
assert(arg != NULL);
int as_value = 0;
unsigned int t1 = 0;
variant_t v1;
unsigned int t2 = 0;
init_variant(&v1);
read_token();
if (token == LEFT_BRAKET_TOKEN)
{
as_value = 1;
read_token();
}
else
{
as_value = 0;
}
if (is_value_token(token))
{
t1 = token;
copy_variant(&v1, &value);
}
else
{
fprintf(stderr, "%s:%d: error: Expected identifier or integer but got %s.\n", get_scan_file(), get_scan_line(), get_token_name(token));
result = -1;
return;
}
if (next_token == PLUS_TOKEN)
{
read_token();
assert(token == PLUS_TOKEN);
read_token();
if (is_value_token(token))
{
t2 = token;
}
else
{
fprintf(stderr, "%s:%d: error: Expected identifier or integer but got %s.\n", get_scan_file(), get_scan_line(), get_token_name(token));
result = -1;
return;
}
}
if (as_value == 1)
{
read_token();
if (token != RIGHT_BRAKET_TOKEN)
{
fprintf(stderr, "%s:%d: error: Missmatched braket.\n", get_scan_file(), get_scan_line());
result = -1;
return;
}
}
/*
0x00-0x07: register (A, B, C, X, Y, Z, I or J, in that order)
0x08-0x0f: [register]
0x10-0x17: [next word + register]
0x18: POP / [SP++]
0x19: PEEK / [SP]
0x1a: PUSH / [--SP]
0x1b: SP
0x1c: PC
0x1d: O
0x1e: [next word]
0x1f: next word (literal)
0x20-0x3f: literal value 0x00-0x1f (literal)
*/
if (is_symbolic(t1))
{
switch (t1)
{
case A_TOKEN:
if (as_value)
arg->value = 0x08;
else
arg->value = 0x00;
break;
case B_TOKEN:
if (as_value)
arg->value = 0x09;
else
arg->value = 0x01;
break;
case C_TOKEN:
if (as_value)
arg->value = 0x0A;
else
arg->value = 0x02;
break;
case X_TOKEN:
if (as_value)
arg->value = 0x0B;
else
arg->value = 0x03;
break;
case Y_TOKEN:
if (as_value)
arg->value = 0x0C;
else
arg->value = 0x04;
break;
case Z_TOKEN:
if (as_value)
arg->value = 0x0D;
else
arg->value = 0x05;
break;
case I_TOKEN:
if (as_value)
arg->value = 0x0E;
else
arg->value = 0x06;
break;
case J_TOKEN:
if (as_value)
arg->value = 0x0F;
else
arg->value = 0x07;
break;
case POP_TOKEN:
arg->value = 0x18;
break;
case PEEK_TOKEN:
arg->value = 0x19;
break;
case PUSH_TOKEN:
arg->value = 0x1A;
break;
case SP_TOKEN:
arg->value = 0x1B;
break;
case PC_TOKEN:
arg->value = 0x1C;
break;
case O_TOKEN:
arg->value = 0x1D;
break;
}
/* TODO validate that there is no offset */
}
else if (t1 == IDENTIFIER_TOKEN)
{
// for jumps we encode alwas next word
// this is since we can't resolve the target at this point
arg->value = 0x1F;
copy_variant(&arg->extra, &v1);
/* TODO validate that there is no offset */
}
else
{
assert(t1 == INTEGER_TOKEN);
assert(v1.type == UINT_TYPE);
if (as_value && t2 == 0)
{
arg->value = 0x1E;
copy_variant(&arg->extra, &v1);
}
if (!as_value && t2 == 0)
{
if (v1.ui <= 0x1F)
{
arg->value = 0x20 + v1.ui;
}
else
{
arg->value = 0x1F;
copy_variant(&arg->extra, &v1);
}
}
if (t2 != 0)
{
assert(as_value == 1);
switch (t2)
{
case A_TOKEN:
arg->value = 0x10;
break;
case B_TOKEN:
arg->value = 0x11;
break;
case C_TOKEN:
arg->value = 0x12;
break;
case X_TOKEN:
arg->value = 0x13;
break;
case Y_TOKEN:
arg->value = 0x14;
break;
case Z_TOKEN:
arg->value = 0x15;
break;
case I_TOKEN:
arg->value = 0x16;
break;
case J_TOKEN:
arg->value = 0x17;
break;
default:
assert(0 && "should never happen");
break;
}
copy_variant(&arg->extra, &v1);
}
}
}
/* error recovery: read until EOL or EOF */
void recover_error()
{
while (token != EOL_TOKEN && token != EOF_TOKEN)
{
read_token();
}
}
void read_instruction()
{
instruction_t instr = {0};
// skip empty lines
if (next_token == EOL_TOKEN || next_token == EOF_TOKEN)
{
read_token();
return;
}
if (next_token == LABEL_TOKEN)
{
read_label(&instr.label);
}
else
{
instr.label = 0;
}
read_opcode(&instr.opcode);
if (next_token != EOL_TOKEN)
{
read_argument(&instr.arg1);
}
if (next_token == COMMA_TOKEN)
{
read_token();
assert(token == COMMA_TOKEN);
read_argument(&instr.arg2);
}
read_token();
if (token != EOL_TOKEN)
{
fprintf(stderr, "%s:%d: error: To much input.\n", get_scan_file(), get_scan_line());
result = -1;
recover_error();
return;
}
add_instruction(prog, instr);
}
void read_program()
{
while (token != EOF_TOKEN)
{
read_instruction();
}
}
int parse(const char* file, program_t* p)
{
int r = 0;
FILE* fp = NULL;
prog = p;
prog->file = strdup(file);
assert(prog != NULL);
if (file != NULL)
{
fp = fopen(file, "r");
if (fp == NULL)
{
fprintf(stderr, "Failed to open %s\n", file);
return -1;
}
}
else
{
fp = stdin;
file = "<stdin>";
}
r = start_scan(fp, file);
if (r < 0)
{
return r;
}
init_variant(&value);
init_variant(&next_value);
next_token = scan_token(&next_value);
read_program();
return result;
}
int init_program(program_t* prog)
{
assert(prog != NULL);
prog->file = 0;
prog->code = NULL;
prog->code_lines = 0;
prog->code_size = 0;
return 0;
}
/**
* Clear program memory.
**/
int clear_program(program_t* prog)
{
assert(prog != NULL);
free(prog->file);
prog->file = NULL;
free(prog->code);
prog->code = NULL;
prog->code_lines = 0;
prog->code_size = 0;
return 0;
}