aboutsummaryrefslogtreecommitdiff
path: root/src/c8asm/parser.c
blob: e0f366026fcd14d077f225dc8ad010bbdf0b1a7f (plain) (blame)
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
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>

#include <da.h>

#include "cerr.h"
#include "common.h"
#include "lexer.h"
#include "macros.h"
#include "parser.h"

#define E_BADLABEL  "identifier ‘%.*s’ cannot be used as a label"
#define E_EARLY     "expected %s but input ended prematurely"
#define E_EXPECTED2 "expected %s but got %s"
#define E_EXPECTED  "expected %s but got %s ‘%.*s’"
#define E_INSTR     "got unknown instruction ‘%.*s’"
#define E_TOOLARGE  "expected %s but got out-of-range integer ‘%.*s’"

#define die_with_off(P, ...) die_with_off(filename, (P)-baseptr, __VA_ARGS__)

enum numsize {
	NS_NIBBLE = 0xF,
	NS_BYTE = 0xFF,
	NS_ADDR = 0xFFF,
};

enum regtype {
	RT_NONE,
	RT_DT,
	RT_I,
	RT_K,
	RT_ST,
	RT_VX,
};

static bool parselabel(void);
static void parseline(void);
static void parseop(void);
static struct raw_addr parseaddr(struct token);
static struct token reqnext(const char *, tokkind);

static uint16_t hexval(char);
static uint16_t parsenum(struct token, enum numsize);
static enum regtype regtype(struct u8view);

static void parseop_add(void), parseop_and(void), parseop_bcd(void),
	parseop_call(void), parseop_cls(void), parseop_db(void), parseop_drw(void),
	parseop_hex(void), parseop_jp(void), parseop_ld(void), parseop_or(void),
	parseop_ret(void), parseop_rnd(void), parseop_rstr(void), parseop_se(void),
	parseop_shl(void), parseop_shr(void), parseop_sknp(void), parseop_skp(void),
	parseop_sne(void), parseop_stor(void), parseop_sub(void),
	parseop_subn(void), parseop_sys(void), parseop_xor(void);
#include "autogen-lookup.h"

static size_t i;
static struct ast ast;
static struct tokens *tokens;

struct ast
parsefile(struct tokens toks)
{
	ast.len = i = 0;
	tokens = &toks;

	while (i < toks.len)
		parseline();

	/* We can safely not do this, but GCC disagrees.  This gets GCC to not
	   complain about dangling pointers. */
	tokens = nullptr;

	return ast;
}

void
parseline(void)
{
	while (parselabel())
		;
	parseop();
	reqnext("end of line", T_EOL);
}

bool
parselabel(void)
{
	if (tokens->len - i >= 2 && tokens->buf[i].kind == T_IDENT
	    && tokens->buf[i + 1].kind == T_COLON)
	{
		struct dir lbl = {
			.kind = D_LABEL,
			.name = tokens->buf[i].sv,
		};
		if (regtype(lbl.name) != RT_NONE)
			die_with_off(lbl.name.p, E_BADLABEL, U8_PRI_ARGS(lbl.name));
		dapush(&ast, lbl);
		i += 2;
		return true;
	}

	return false;
}

void
parseop(void)
{
	const struct opf_pair *op;
	struct token tok = reqnext("instruction or end of line", T_IDENT | T_EOL);

	if (tok.kind == T_EOL) {
		i--;
		return;
	}

	if (!(op = oplookup(tok.sv.p, tok.sv.len)))
		die_with_off(tok.sv.p, E_INSTR, U8_PRI_ARGS(tok.sv));
	op->pfn();
}

struct raw_addr
parseaddr(struct token tok)
{
	if (tok.kind == T_NUMBER)
		return (struct raw_addr){.val = parsenum(tok, NS_ADDR)};
	if (tok.kind == T_IDENT) {
		if (regtype(tok.sv) != RT_NONE)
			die_with_off(tok.sv.p, E_BADLABEL, U8_PRI_ARGS(tok.sv));
		return (struct raw_addr){.label = true, .sv = tok.sv};
	}
	unreachable();
}

enum regtype
regtype(struct u8view v)
{
	if (v.len == 0 || v.len > 2)
		return RT_NONE;
	if (v.len == 1)
		return v.p[0] == 'i' ? RT_I : v.p[0] == 'k' ? RT_K : RT_NONE;
	if (memeq(v.p, "dt", 2))
		return RT_DT;
	if (memeq(v.p, "st", 2))
		return RT_ST;
	return v.p[0] == 'v'
	            && ((v.p[1] >= '0' && v.p[1] <= '9')
	                || (v.p[1] >= 'a' && v.p[1] <= 'f'))
	         ? RT_VX
	         : RT_NONE;
}

uint16_t
hexval(char ch)
{
	return ch >= '0' && ch <= '9' ? ch - '0'
	     : ch >= 'a' && ch <= 'f' ? ch - 'a' + 10
	                              : (unreachable(), 0);
}

uint16_t
parsenum(struct token tok, enum numsize size)
{
	char ch;
	uint16_t acc, cutoff, cutlim;
	struct u8view v = tok.sv;

	acc = 0;
	cutoff = size;
	cutlim = cutoff % tok.base;
	cutoff /= tok.base;

	if (v.len >= 2 && v.p[0] == '0' && v.p[1] > '9') {
		v.p += 2;
		v.len -= 2;
	}

	for (ch = *v.p; v.len; v.p++, v.len--, ch = *v.p) {
		if (ch == '\'')
			continue;
		else if (ch >= '0' && ch <= '9')
			ch -= '0';
		else if (ch >= 'a' && ch <= 'f')
			ch -= 'a' - 10;
		else if (ch >= 'A' && ch <= 'F')
			ch -= 'A' - 10;
		else
			unreachable();

		if (acc > cutoff || (acc == cutoff && ch > cutlim)) {
			const char *s = size == NS_NIBBLE ? "nibble"
			              : size == NS_BYTE   ? "byte"
			              : size == NS_ADDR   ? "address"
			                                  : (unreachable(), nullptr);
			die_with_off(tok.sv.p, E_TOOLARGE, s, U8_PRI_ARGS(tok.sv));
		}

		acc *= tok.base;
		acc += ch;
	}

	return acc;
}

struct token
reqnext(const char *want, tokkind msk)
{
	struct token t;
	if (i >= tokens->len)
		die_with_off(baseptr + filesize - 1, E_EARLY, want);

	if ((t = tokens->buf[i++]).kind & msk)
		return t;
	if (t.kind == T_EOL)
		die_with_off(t.sv.p, E_EXPECTED2, want, tokrepr(t.kind));
	die_with_off(t.sv.p, E_EXPECTED, want, tokrepr(t.kind), U8_PRI_ARGS(t.sv));
}

#define I(...) ((struct dir){.kind = D_INSTR, .instr = (__VA_ARGS__)})

/* Common implementations of instructions that always take 1 or 2 v-registers */
#define ONE_VREG(T) \
	do { \
		struct instr ins = {.kind = (T)}; \
		struct token tok = reqnext("v-register", T_IDENT); \
		if (regtype(tok.sv) & ~RT_VX) { \
			die_with_off(tok.sv.p, E_EXPECTED, "v-register", \
			             tokrepr(tok.kind), U8_PRI_ARGS(tok.sv)); \
		} \
		ins.args[ins.len++].val = hexval(tok.sv.p[1]); \
		dapush(&ast, I(ins)); \
	} while (false)
#define TWO_VREG(T) \
	do { \
		struct instr ins = {.kind = (T)}; \
		struct token lhs = reqnext("v-register", T_IDENT); \
		struct token rhs = reqnext("v-register", T_IDENT); \
		if (regtype(lhs.sv) & ~RT_VX) { \
			die_with_off(lhs.sv.p, E_EXPECTED, "v-register", \
			             tokrepr(lhs.kind), U8_PRI_ARGS(lhs.sv)); \
		} \
		if (regtype(rhs.sv) & ~RT_VX) { \
			die_with_off(rhs.sv.p, E_EXPECTED, "v-register", \
			             tokrepr(rhs.kind), U8_PRI_ARGS(rhs.sv)); \
		} \
		ins.args[ins.len++].val = hexval(lhs.sv.p[1]); \
		ins.args[ins.len++].val = hexval(rhs.sv.p[1]); \
		dapush(&ast, I(ins)); \
	} while (false)

void
parseop_add(void)
{
	enum regtype rt;
	struct instr ins = {};
	struct token tok = reqnext("v- or i-register", T_IDENT);

	switch (rt = regtype(tok.sv)) {
	case RT_VX:
		ins.args[ins.len++].val = hexval(tok.sv.p[1]);
		tok = reqnext("byte or v-register", T_IDENT | T_NUMBER);

		if (tok.kind == T_NUMBER) {
			ins.kind = I_ADD_VX_B;
			ins.args[ins.len++].val = parsenum(tok, NS_BYTE);
		} else if (regtype(tok.sv) != RT_VX) {
			die_with_off(tok.sv.p, E_EXPECTED, "v-register", tokrepr(tok.kind),
			             U8_PRI_ARGS(tok.sv));
		} else {
			ins.kind = I_ADD_VX_VY;
			ins.args[ins.len++].val = hexval(tok.sv.p[1]);
		}
		break;
	case RT_I:
		ins.kind = I_ADD_I_VX;
		tok = reqnext("v-register", T_IDENT);
		if (regtype(tok.sv) != RT_VX) {
			die_with_off(tok.sv.p, E_EXPECTED, "v-register", tokrepr(tok.kind),
			             U8_PRI_ARGS(tok.sv));
		}
		ins.args[ins.len++].val = hexval(tok.sv.p[1]);
		break;
	default:
		die_with_off(tok.sv.p, E_EXPECTED, "v- or i-register",
		             tokrepr(tok.kind), U8_PRI_ARGS(tok.sv));
	}

	dapush(&ast, I(ins));
}

void
parseop_and(void)
{
	TWO_VREG(I_AND);
}

void
parseop_bcd(void)
{
	ONE_VREG(I_BCD);
}

void
parseop_call(void)
{
	struct instr ins = {.kind = I_CALL};
	struct token tok = reqnext("address", T_IDENT | T_NUMBER);
	ins.args[ins.len++] = parseaddr(tok);
	dapush(&ast, I(ins));
}

void
parseop_cls(void)
{
	dapush(&ast, I((struct instr){.kind = I_CLS}));
}

void
parseop_db(void)
{
	struct instr ins = {.kind = I_DB};
	do {
		struct token tok = reqnext("byte or string", T_NUMBER | T_STRING);
		switch (tok.kind) {
		case T_NUMBER:
			dapush(&ins, parsenum(tok, NS_BYTE));
			break;
		case T_STRING:
			for (size_t i = 1; i < tok.sv.len - 1; i++)
				dapush(&ins, tok.sv.p[i]);
			break;
		default:
			unreachable();
		}
	} while (i < tokens->len && tokens->buf[i].kind != T_EOL);

	dapush(&ast, I(ins));
}

void
parseop_drw(void)
{
	struct instr ins = {.kind = I_DRW};
	struct token op1, op2, op3;

	op1 = reqnext("v-register", T_IDENT);
	op2 = reqnext("v-register", T_IDENT);
	op3 = reqnext("nibble", T_NUMBER);

	if (regtype(op1.sv) != RT_VX) {
		die_with_off(op1.sv.p, E_EXPECTED, "v-register", tokrepr(op1.kind),
		             U8_PRI_ARGS(op1.sv));
	}
	if (regtype(op2.sv) != RT_VX) {
		die_with_off(op2.sv.p, E_EXPECTED, "v-register", tokrepr(op2.kind),
		             U8_PRI_ARGS(op2.sv));
	}

	ins.args[ins.len++].val = hexval(op1.sv.p[1]);
	ins.args[ins.len++].val = hexval(op2.sv.p[1]);
	ins.args[ins.len++].val = parsenum(op3, NS_NIBBLE);
	dapush(&ast, I(ins));
}

void
parseop_hex(void)
{
	ONE_VREG(I_HEX);
}

void
parseop_jp(void)
{
	enum regtype rt;
	struct instr ins = {};
	struct token op = reqnext("v0-register or address", T_IDENT | T_NUMBER);

	if (op.kind == T_IDENT)
		rt = regtype(op.sv);
	if (op.kind == T_NUMBER || (op.kind == T_IDENT && rt == RT_NONE)) {
		ins.kind = I_JP_A;
		ins.args[ins.len++] = parseaddr(op);
	} else if (op.kind == T_IDENT) {
		ins.kind = I_JP_V0_A;
		if (op.sv.len != 2 || !memeq(op.sv.p, "v0", 2)) {
			die_with_off(op.sv.p, E_EXPECTED, "v0-register or address",
			             tokrepr(op.kind), U8_PRI_ARGS(op.sv));
		}
		ins.args[ins.len++] = parseaddr(reqnext("address", T_NUMBER | T_IDENT));
	} else
		unreachable();

	dapush(&ast, I(ins));
}

void
parseop_ld(void)
{
	enum regtype rt;
	struct instr ins = {};
	struct token op = reqnext("v-, i-, dt-, or st-register", T_IDENT);

	switch (rt = regtype(op.sv)) {
	case RT_DT:
	case RT_ST:
		ins.kind = rt == RT_DT ? I_LD_DT : I_LD_ST;
		op = reqnext("v-register", T_IDENT);
		if (regtype(op.sv) != RT_VX) {
			die_with_off(op.sv.p, E_EXPECTED, "v-register", tokrepr(op.kind),
			             U8_PRI_ARGS(op.sv));
		}
		ins.args[ins.len++].val = hexval(op.sv.p[1]);
		break;

	case RT_I:
		ins.kind = I_LD_I;
		ins.args[ins.len++] = parseaddr(reqnext("address", T_NUMBER | T_IDENT));
		break;

	case RT_VX:
		ins.args[ins.len++].val = hexval(op.sv.p[1]);
		op = reqnext("v-, k-, or dt-register, or byte", T_IDENT | T_NUMBER);

		switch (op.kind) {
		case T_IDENT:
			switch (rt = regtype(op.sv)) {
			case RT_DT:
				ins.kind = I_LD_VX_DT;
				break;
			case RT_K:
				ins.kind = I_LD_VX_K;
				break;
			case RT_VX:
				ins.kind = I_LD_VX_VY;
				ins.args[ins.len++].val = hexval(op.sv.p[1]);
				break;
			default:
				die_with_off(op.sv.p, E_EXPECTED,
				             "v-, k-, or dt-register, or byte",
				             tokrepr(op.kind), U8_PRI_ARGS(op.sv));
			}

			break;
		case T_NUMBER:
			ins.kind = I_LD_VX_B;
			ins.args[ins.len++].val = parsenum(op, NS_BYTE);
			break;
		default:
			unreachable();
		}
		break;

	default:
		die_with_off(op.sv.p, E_EXPECTED, "v-, i-, dt-, or st-register",
		             tokrepr(op.kind), U8_PRI_ARGS(op.sv));
	}

	dapush(&ast, I(ins));
}

void
parseop_or(void)
{
	TWO_VREG(I_OR);
}

void
parseop_ret(void)
{
	dapush(&ast, I((struct instr){.kind = I_RET}));
}

void
parseop_rnd(void)
{
	struct instr ins = {.kind = I_RND};
	struct token op1, op2;

	op1 = reqnext("v-register", T_IDENT);
	op2 = reqnext("byte", T_NUMBER);

	if (regtype(op1.sv) != RT_VX) {
		die_with_off(op1.sv.p, E_EXPECTED, "v-register", tokrepr(op1.kind),
		             U8_PRI_ARGS(op1.sv));
	}

	ins.args[ins.len++].val = hexval(op1.sv.p[1]);
	ins.args[ins.len++].val = parsenum(op2, NS_BYTE);
	dapush(&ast, I(ins));
}

void
parseop_rstr(void)
{
	ONE_VREG(I_RSTR);
}

void
parseop_se(void)
{
	struct instr ins = {};
	struct token op1, op2;

	op1 = reqnext("v-register", T_IDENT);
	op2 = reqnext("byte or v-register", T_IDENT | T_NUMBER);

	if (regtype(op1.sv) != RT_VX) {
		die_with_off(op1.sv.p, E_EXPECTED, "v-register", tokrepr(op1.kind),
		             U8_PRI_ARGS(op1.sv));
	}
	ins.args[ins.len++].val = hexval(op1.sv.p[1]);

	switch (op2.kind) {
	case T_IDENT:
		if (regtype(op2.sv) != RT_VX) {
			die_with_off(op2.sv.p, E_EXPECTED, "v-register", tokrepr(op2.kind),
			             U8_PRI_ARGS(op2.sv));
		}
		ins.kind = I_SE_VX_VY;
		ins.args[ins.len++].val = hexval(op2.sv.p[1]);
		break;
	case T_NUMBER:
		ins.kind = I_SE_VX_B;
		ins.args[ins.len++].val = parsenum(op2, NS_BYTE);
		break;
	default:
		unreachable();
	}

	dapush(&ast, I(ins));
}

void
parseop_shl(void)
{
	ONE_VREG(I_SHL);
}

void
parseop_shr(void)
{
	ONE_VREG(I_SHR);
}

void
parseop_sknp(void)
{
	ONE_VREG(I_SKNP);
}

void
parseop_skp(void)
{
	ONE_VREG(I_SKP);
}

void
parseop_sne(void)
{
	struct instr ins = {};
	struct token op1, op2;

	op1 = reqnext("v-register", T_IDENT);
	op2 = reqnext("byte or v-register", T_IDENT | T_NUMBER);

	if (regtype(op1.sv) != RT_VX) {
		die_with_off(op1.sv.p, E_EXPECTED, "v-register", tokrepr(op1.kind),
		             U8_PRI_ARGS(op1.sv));
	}
	ins.args[ins.len++].val = hexval(op1.sv.p[1]);

	switch (op2.kind) {
	case T_IDENT:
		if (regtype(op2.sv) != RT_VX) {
			die_with_off(op2.sv.p, E_EXPECTED, "v-register", tokrepr(op2.kind),
			             U8_PRI_ARGS(op2.sv));
		}
		ins.kind = I_SNE_VX_VY;
		ins.args[ins.len++].val = hexval(op2.sv.p[1]);
		break;
	case T_NUMBER:
		ins.kind = I_SNE_VX_B;
		ins.args[ins.len++].val = parsenum(op2, NS_BYTE);
		break;
	default:
		unreachable();
	}

	dapush(&ast, I(ins));
}

void
parseop_stor(void)
{
	ONE_VREG(I_STOR);
}

void
parseop_sub(void)
{
	TWO_VREG(I_SUB);
}

void
parseop_subn(void)
{
	TWO_VREG(I_SUBN);
}

void
parseop_sys(void)
{
	struct instr ins = {.kind = I_SYS};
	ins.args[ins.len++] = parseaddr(reqnext("address", T_NUMBER | T_IDENT));
	dapush(&ast, I(ins));
}

void
parseop_xor(void)
{
	TWO_VREG(I_XOR);
}