summaryrefslogtreecommitdiff
path: root/parse.c
blob: 45758c907d83da18859c921b4f546556c0bd2c30 (plain)
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
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <stdarg.h>
#include <string.h>
#include <stdint.h>
#include <stdbool.h>

#include "lex.h"
#include "parse.h"
#include "instruction.h"
#include "util.h"

static const char *filename;
static FILE *fd;
static struct token *cursor;
static struct token *tokens;
static size_t tokens_pos;
static size_t tokens_count;
static struct label *labels;
static size_t labels_count;
static struct instruction *insts;
static size_t insts_count;
static size_t byte_offset;

static void emit(const char *fmt, ...)
{
	va_list args;
	va_start(args, fmt);
	if (cursor) {
		fprintf(stderr, "%s at (%zd,%zd): ", filename, cursor->line, cursor->column);
		vfprintf(stderr, fmt, args);
		indicate_file_area(fd, cursor->line, cursor->column, cursor->span);
	} else {
		fprintf(stderr, "%s: ", filename);
		vfprintf(stderr, fmt, args);
	}
	va_end(args);
}

#define EXPECT_AND_DISCARD_CRITICAL(type)\
	do {                                 \
		EXPECT_CRITICAL(type)            \
		kerchunk();                      \
	} while (0);

#define EXPECT_CRITICAL(type)\
	if (expect(type)) {  \
		return 1;        \
	}

static int expect(enum TOKEN_TYPE e)
{
	const char *expected_desc = "(internal error)";
	const char *observed_desc = "(internal error)";

	if (!cursor || cursor->type != e) {
		expected_desc = get_token_description(e);
		if (cursor) {
			observed_desc = get_token_description(cursor->type);
		} else {
			observed_desc = "end of file";
		}
		emit("Error: Expected %s, got %s\n", expected_desc, observed_desc);
		return 1;
	}

	return 0;
}

void kerchunk()
{
	if (tokens_pos < tokens_count - 1) {
		cursor = &tokens[++tokens_pos];
	} else {
		cursor = NULL;
	}
}

int parse_eol(void)
{
	EXPECT_AND_DISCARD_CRITICAL(TOKEN_EOL);
	return 0;
}

int parse_comma(void)
{
	EXPECT_AND_DISCARD_CRITICAL(TOKEN_COMMA);
	return 0;
}

int parse_imm(uint16_t *imm)
{
	EXPECT_CRITICAL(TOKEN_NUMERIC);
	*imm = cursor->i_val;
	kerchunk();
	return 0;
}

int parse_ident(char **ident)
{
	EXPECT_CRITICAL(TOKEN_IDENT);
	*ident = cursor->s_val;
	kerchunk();
	return 0;
}

/**
 * FIXME move */

int add_instruction(struct instruction inst)
{
	struct instruction *old_insts = insts;
	insts = realloc(insts, (insts_count + 1) * sizeof(struct instruction));
	if (!insts) {
		free(old_insts);
		perror("realloc");
		return 1;
	}

	insts[insts_count] = inst;

	insts_count++;
	return 0;
}

int new_label(struct label *dest, const char *name)
{
	char *name_clone = strdup(name);

	if (!name_clone) {
		perror("strdup");
		return 1;
	}

	dest->name = name_clone;
	dest->byte_offset = byte_offset;

	return 0;
}

void destroy_label(struct label *l)
{
	free(l->name);
}
/**/

int parse_label()
{
	size_t i = 0;
	struct label l;
	struct label *old_labels = labels;

	EXPECT_CRITICAL(TOKEN_LABEL);

	for (i = 0; i < labels_count; i++) {
		if (strcmp(labels[i].name, cursor->s_val) == 0) {
			emit("Error: duplicate label\n");
			return 1;
		}
	}

	labels = realloc(labels, (labels_count + 1) * sizeof(struct label));
	if (!labels) {
		perror("realloc");
		free(old_labels);
		return 1;
	}

	if (new_label(&l, cursor->s_val))
		return 1;

	labels[labels_count] = l;

	labels_count++;
	kerchunk();
	return 0;
}

int parse_reg(enum REG *reg)
{
	EXPECT_CRITICAL(TOKEN_REGISTER);

	if (get_reg_from_asm(cursor->s_val, reg)) {
		emit("Error: Unknown register\n");
		return 1;
	}

	kerchunk();
	return 0;
}

int parse_i_type(enum OPER oper, enum REG dest, enum REG left, uint16_t imm)
{
	struct instruction i;
	i.type = INST_TYPE_NI;
	i.inst.i.oper = oper;
	i.inst.i.dest = dest;
	i.inst.i.left = left;
	i.inst.i.imm_is_ident = false;
	i.inst.i.imm.value = imm;

	if (add_instruction(i))
		return 1;

	/* FIXME detect narrow/wide */
	byte_offset += NITYPE_SIZE_BYTES;
	return 0;
}

int parse_i_ident_type(enum OPER oper, enum REG dest, enum REG left, char *ident)
{
	struct instruction i;
	i.type = INST_TYPE_NI;
	i.inst.i.oper = oper;
	i.inst.i.dest = dest;
	i.inst.i.left = left;
	i.inst.i.imm_is_ident = true;
	i.inst.i.imm.label = ident;

	if (add_instruction(i))
		return 1;

	/* FIXME detect narrow/wide */
	byte_offset += NITYPE_SIZE_BYTES;
	return 0;
}

int parse_r_type(enum OPER oper, enum REG dest, enum REG left, enum REG right)
{
	struct instruction i;
	i.type = INST_TYPE_R;
	i.inst.r.oper = oper;
	i.inst.r.dest = dest;
	i.inst.r.left = left;
	i.inst.r.right = right;

	if (add_instruction(i))
		return 1;

	byte_offset += RTYPE_SIZE_BYTES;
	return 0;
}

int parse_j_reg_type(enum JCOND cond, enum REG reg)
{
	struct instruction i;
	i.type = INST_TYPE_JR;
	i.inst.jr.cond = cond;
	i.inst.jr.reg = reg;

	if (add_instruction(i))
		return 1;

	byte_offset += JRTYPE_SIZE_BYTES;
	return 0;
}

int parse_j_imm_type(enum JCOND cond, uint16_t imm)
{
	struct instruction i;
	i.type = INST_TYPE_JI;
	i.inst.ji.cond = cond;
	i.inst.ji.imm_is_ident = false;
	i.inst.ji.imm.value = imm;

	if (add_instruction(i))
		return 1;

	byte_offset += JITYPE_SIZE_BYTES;
	return 0;
}

int parse_j_ident_type(enum JCOND cond, char *ident)
{
	struct instruction i;

	i.type = INST_TYPE_JI;
	i.inst.ji.cond = cond;
	i.inst.ji.imm_is_ident = true;
	i.inst.ji.imm.label = ident;

	if (add_instruction(i))
		return 1;

	byte_offset += JITYPE_SIZE_BYTES;
	return 0;
}

int parse_b_imm_type(enum JCOND cond, int16_t imm)
{
	struct instruction i;

	i.type = INST_TYPE_B;
	i.inst.b.cond = cond;
	i.inst.b.imm_is_ident = false;
	i.inst.b.imm.value = imm;

	if (add_instruction(i))
		return 1;

	byte_offset += BTYPE_SIZE_BYTES;
	return 0;
}

int parse_b_ident_type(enum JCOND cond, char *ident)
{
	struct instruction i;

	i.type = INST_TYPE_B;
	i.inst.b.cond = cond;
	i.inst.b.imm_is_ident = true;
	i.inst.b.imm.label = ident;

	if (add_instruction(i))
		return 1;

	byte_offset += BTYPE_SIZE_BYTES;
	return 0;
}

int parse_instruction(void)
{
	enum REG reg_left;
	enum REG reg_right;
	enum REG reg;
	uint16_t imm;
	char *ident = NULL;
	/**
	 * Based on the operands in assembly, instructions fall into 6 categories:
	 *
	 * REG, REG, REG (verbose R-Type)
	 * REG, REG, IMM (verbose I-Type)
	 * REG, REG      (terse R-type (alias), e.g. `ld $2, $3`)
	 * REG, IMM      (terse I-Type (alias), e.g. `ldi $2, 100`)
	 * REG           (very terse R-type (alias), e.g. `not $2`, OR J-Type)
	 * IMM			 j-type
	 * (none)        (e.g. `nop` (virtual))
	 */
	/* Special cases: catch alias instructions first */
	if (strcmp(cursor->s_val, "nop") == 0) {
		/* `nop` => `add $0,$0,$0` */
		kerchunk();
		if (parse_eol())
			return 1;
		return parse_r_type(OPER_ADD, REG_0, REG_0, REG_0);
	} else if (strcmp(cursor->s_val, "not") == 0) {
		/* `not $1` => `xor $1, $1, $H` */
		kerchunk();
		if (parse_reg(&reg) || parse_eol())
			return 1;
		return parse_r_type(OPER_XOR, reg, reg, REG_H);
	} else if (strcmp(cursor->s_val, "neg") == 0) {
		/* `neg $1` => `sub $1, $0, $1` */
		kerchunk();
		if (parse_reg(&reg) || parse_eol())
			return 1;
		return parse_r_type(OPER_SUB, reg, REG_0, reg);
	} else if (strcmp(cursor->s_val, "mv") == 0) {
		/* `mv $1,$2` => `add $1,$2,$0` */
		kerchunk();
		if (parse_reg(&reg_left) || parse_comma() || parse_reg(&reg_right) || parse_eol