summaryrefslogtreecommitdiffstats
path: root/asm.c
blob: cd1874738e59ecae8ecae1087a02032917c72b7f (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
#include "asm.h"
#include "util.h"
#include "tpu.h"

#include <stdarg.h>
#include <string.h>
#include <stdint.h>

#define NAMEALPH "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_"
#define WHITESPACE " \t\v\r\n,"

enum asm_tok {
	/* Global */
	TOK_STDIN, TOK_STDOUT, TOK_TPU, TOK_END,

	/* Operands (order like OP_*) */
	TOK_ACC, TOK_BAK, TOK_NIL, TOK_LEFT, TOK_RIGHT,
	TOK_UP, TOK_DOWN, TOK_ANY, TOK_LAST, TOK_LIT, TOK_NAME,

	/* Instructions (order like INST_*) */
	TOK_NOP, TOK_MOV, TOK_SWP, TOK_SAV, TOK_ADD, TOK_SUB,
	TOK_NEG, TOK_JMP, TOK_JEZ, TOK_JNZ, TOK_JGZ, TOK_JLZ, TOK_JRO,

	/* Misc */
	TOK_COMMENT, TOK_LABEL, TOK_XPOS, TOK_YPOS, TOK_NL, TOK_EOF
};

struct asm_tokenizer {
	const char *filepath;
	FILE *file;
	enum asm_tok tok;
	char *tokstr;
	size_t lineno, off;
	char linebuf[256];
};

static const char *tok_reprs[] = {
	/* Global */
	"'STDIN'", "'STDOUT'", "'TPU'", "'END'",

	/* Operands (order like OP_*) */
	"'ACC'", "'BAK'", "'NIL'", "'LEFT'", "'RIGHT'",
	"'UP'", "'DOWN'", "'ANY'", "'LAST'", "<LIT>", "<NAME>",

	/* Instructions (order like INST_*) */
	"'NOP'", "'MOV'", "'SWP'", "'SAV'", "'ADD'", "'SUB'",
	"'NEG'", "'JMP'", "'JEZ'", "'JNZ'", "'JGZ'", "'JLZ'", "'JRO'",

	/* Misc */
	"#<COMMENT>", "<LABEL>:", "X<INT>", "Y<INT>", "<NL>", "<EOF>"
};

static bool
is_lit(const char *str)
{
	unsigned long v;
	char *end;

	v = strtoul(str, &end, 10);
	if (!end || *end) return false;

	return v < 256;
}

static uint8_t
str_to_lit(const char *str)
{
	return (uint8_t) atoi(str);
}

enum tpu_inst_type
tok_to_inst(enum asm_tok tok)
{
	if (tok < TOK_NOP || tok > TOK_JRO) abort();
	return tok - TOK_NOP + INST_NOP;
}

enum tpu_inst_op_type
tok_to_optype(enum asm_tok tok)
{
	if (tok < TOK_ACC || tok > TOK_NAME) abort();
	return tok - TOK_ACC + OP_ACC;
}

static size_t
strlcat_op_name(char *buf, struct tpu_inst_op *op, size_t n)
{
	char hhbuf[4];

	if (op->type == OP_LIT) {
		snprintf(hhbuf, 4, "%hhu", op->val.lit);
		return strdcat(buf, hhbuf, n);
	} else if (op->type == OP_LABEL) {
		return strdcat(buf, op->val.label, n);
	} else {
		return strdcat(buf, op_reprs[op->type], n);
	}
}

size_t
asm_print_inst(char *buf, size_t n, struct tpu_inst *inst)
{
	size_t len;

	len = strdcpy(buf, inst_reprs[inst->type], n);
	if (inst->opcnt >= 1) {
		len += strdcat(buf, " ", n);
		len += strlcat_op_name(buf, &inst->ops[0], n);
	}
	if (inst->opcnt >= 2) {
		len += strdcat(buf, ", ", n);
		len += strlcat_op_name(buf, &inst->ops[1], n);
	}

	return len;
}

static enum asm_tok
tok_next(struct asm_tokenizer *tok)
{
	size_t len;
	char *s;

	if (!tok->linebuf[tok->off]) {
		if (feof(tok->file)) return TOK_EOF;
		s = fgets(tok->linebuf, sizeof(tok->linebuf), tok->file);
		if (!s && !feof(tok->file)) die("fgets:");
		if (!s) return TOK_NL;

		len = strlen(s);
		if (len && s[len-1] != '\n' && !feof(tok->file))
			die("load: line %lu too long", tok->lineno);
		if (len && s[len-1] == '\n') s[len-1] = '\0';

		tok->lineno += 1;
		tok->tokstr = s;
		tok->off = 0;
		return TOK_NL;
	}

	s = tok->linebuf + tok->off;
	len = strspn(s, WHITESPACE);
	tok->off += len;
	if (!s[len]) return TOK_NL;
	tok->tokstr = (s += len);

	len = strcspn(s, WHITESPACE);
	tok->off += len;
	if (s[len]) {
		s[len] = '\0';
		tok->off += 1;
	}

	//printf("> %s\n", tok->tokstr);

	if (!strcasecmp(s, "stdin")) {
		return TOK_STDIN;
	} else if (!strcasecmp(s, "stdout")) {
		return TOK_STDOUT;
	} else if (!strcasecmp(s, "tpu")) {
		return TOK_TPU;
	} else if (!strcasecmp(s, "end")) {
		return TOK_END;
	} else if (!strcasecmp(s, "acc")) {
		return TOK_ACC;
	} else if (!strcasecmp(s, "bak")) {
		return TOK_BAK;
	} else if (!strcasecmp(s, "nil")) {
		return TOK_NIL;
	} else if (!strcasecmp(s, "left")) {
		return TOK_LEFT;
	} else if (!strcasecmp(s, "right")) {
		return TOK_RIGHT;
	} else if (!strcasecmp(s, "up")) {
		return TOK_UP;
	} else if (!strcasecmp(s, "down")) {
		return TOK_DOWN;
	} else if (!strcasecmp(s, "any")) {
		return TOK_ANY;
	} else if (!strcasecmp(s, "last")) {
		return TOK_LAST;
	} else if (!strcasecmp(s, "nop")) {
		return TOK_NOP;
	} else if (!strcasecmp(s, "mov")) {
		return TOK_MOV;
	} else if (!strcasecmp(s, "swp")) {
		return TOK_SWP;
	} else if (!strcasecmp(s, "sav")) {
		return TOK_SAV;
	} else if (!strcasecmp(s, "add")) {
		return TOK_ADD;
	} else if (!strcasecmp(s, "sub")) {
		return TOK_SUB;
	} else if (!strcasecmp(s, "neg")) {
		return TOK_NEG;
	} else if (!strcasecmp(s, "jmp")) {
		return TOK_JMP;
	} else if (!strcasecmp(s, "jez")) {
		return TOK_JEZ;
	} else if (!strcasecmp(s, "jnz")) {
		return TOK_JNZ;
	} else if (!strcasecmp(s, "jgz")) {
		return TOK_JGZ;
	} else if (!strcasecmp(s, "jlz")) {
		return TOK_JLZ;
	} else if (!strcasecmp(s, "jro")) {
		return TOK_JRO;
	} else if (is_lit(s)) {
		return TOK_LIT;
	} else if (*s == '#') {
		tok->off += strlen(tok->linebuf + tok->off);
		return TOK_COMMENT;
	} else if (len && strspn(s, NAMEALPH) == len-1 && s[len-1] == ':') {
		s[len-1] = '\0';
		return TOK_LABEL;
	} else if (*s == 'X' && atoi(s+1) > 0) {
		return TOK_XPOS;
	} else if (*s == 'Y' && atoi(s+1) > 0) {
		return TOK_YPOS;
	} else if (strspn(s, NAMEALPH) == strlen(s)) {
		return TOK_NAME;
	} else {
		die("load: line %lu, invalid token '%s'", tok->lineno, s);
	}
}

static enum asm_tok
tok_next_in(struct asm_tokenizer *tokenizer, ...)
{
	va_list ap, cpy;
	enum asm_tok tok;
	bool first;
	int arg;

	tok = tok_next(tokenizer);

	va_copy(cpy, ap);

	va_start(cpy, tokenizer);
	while ((arg = va_arg(cpy, int)) > 0) {
		if (tok == arg) return tok;
	}
	va_end(cpy);

	fprintf(stderr, "tis-as: load: ");
	fprintf(stderr, "line %lu, got tok %s, expected one of (",
		tokenizer->lineno, tok_reprs[tok]);
	first = true;
	va_start(ap, tokenizer);
	while ((arg = va_arg(ap, int)) > 0) {
		if (!first) fputc(',', stderr);
		fputs(tok_reprs[arg], stderr);
		first = false;
	}
	va_end(ap);
	fputs(")\n", stderr);

	exit(1);
}

static void
tpu_validate(struct tpu *tpu)
{
	size_t dst;
	int i;

	for (i = 0; i < TPU_MAX_INST; i++) {
		if (tpu->insts[i].ops[0].type == OP_LABEL) {
			dst = label_map_get(&tpu->labels,
				tpu->insts[i].ops[0].val.label);
			if (dst == TPU_MAX_INST)
				die("load: tpu X%lu Y%lu, label '%s' not defined",
					tpu->x, tpu->y,
					tpu->insts[i].ops[0].val.label);
		}
	}
}

void
tis_load(struct tis *tis, const char *filepath)
{
	struct asm_tokenizer tokenizer;
	int op1, op2;
	enum asm_tok op1_tok, op2_tok;
	union tpu_inst_op_val op1v, op2v;
	enum tpu_inst_type inst;
	struct tpu *tpu = NULL;
	struct tpu_map_link *link;
	struct tpu_port *port;
	int stdin_x, stdin_y;
	int stdout_x, stdout_y;
	enum asm_tok tok;
	size_t i;

	tis_deinit(tis);
	tis_init(tis);

	stdin_x = stdin_y = 0;
	stdout_x = stdout_y = 0;

	tokenizer.filepath = filepath;
	tokenizer.file = fopen(filepath, "r");
	if (!tokenizer.file) die("load: fopen '%s':", filepath);

	tokenizer.lineno = 0;
	tokenizer.off = 0;
	tokenizer.tokstr = NULL;
	tokenizer.linebuf[tokenizer.off] = '\0';
	while ((tok = tok_next(&tokenizer)) != TOK_EOF) {
		switch (tok) {
		case TOK_STDIN:
			if (tpu) goto disallowed;
			tok_next_in(&tokenizer, TOK_XPOS, -1);
			stdin_x = atoi(tokenizer.tokstr + 1);
			tok_next_in(&tokenizer, TOK_YPOS, -1);
			stdin_y = atoi(tokenizer.tokstr + 1);
			tok_next_in(&tokenizer, TOK_NL, -1);
			break;
		case TOK_STDOUT:
			if (tpu) goto disallowed;
			tok_next_in(&tokenizer, TOK_XPOS, -1);
			stdout_x = atoi(tokenizer.tokstr + 1);
			tok_next_in(&tokenizer, TOK_YPOS, -1);
			stdout_y = atoi(tokenizer.tokstr + 1);
			tok_next_in(&tokenizer, TOK_NL, -1);
			break;
		case TOK_TPU:
			if (tpu) goto disallowed;
			tpu = malloc(sizeof(struct tpu));
			if (!tpu) die("malloc:");
			tpu_init(tpu);
			tok_next_in(&tokenizer, TOK_XPOS, -1);
			tpu->x = (size_t) atoi(tokenizer.tokstr + 1);
			tok_next_in(&tokenizer, TOK_YPOS, -1);
			tpu->y = (size_t) atoi(tokenizer.tokstr + 1);
			tok_next_in(&tokenizer, TOK_NL, -1);
			tpu_map_add(&tis->tpu_map, tpu);
			break;
		case TOK_END:
			if (!tpu) goto disallowed;
			tpu_validate(tpu);
			tpu = NULL;
			tok_next_in(&tokenizer, TOK_NL, -1);
			break;
		case TOK_NOP: case TOK_MOV: case TOK_SWP: case TOK_SAV:
		case TOK_ADD: case TOK_SUB: case TOK_NEG: case TOK_JMP:
		case TOK_JEZ: case TOK_JNZ: case TOK_JGZ: case TOK_JLZ:
		case TOK_JRO:
			if (!tpu) goto disallowed;
			inst = tok_to_inst(tok);

			op1_tok = tok_next_in(&tokenizer, TOK_ACC, TOK_BAK,
				TOK_NIL, TOK_LEFT, TOK_RIGHT, TOK_UP, TOK_DOWN,
				TOK_ANY, TOK_LAST, TOK_LIT, TOK_NAME, TOK_NL, -1);
			if (op1_tok == TOK_NL) {
				if (!tpu_add_inst(tpu, inst, -1, op1v, -1, op2v))
					die("load: line %lu, invalid instruction",
						tokenizer.lineno);
				break;
			}

			op1 = (int) tok_to_optype(op1_tok);
			if (op1 == OP_LIT)
				op1v.lit = str_to_lit(tokenizer.tokstr);
			else if (op1 == OP_LABEL) {
				op1v.label = strdup(tokenizer.tokstr);
				if (!op1v.label) die("strdup:");
			}

			op2_tok = tok_next_in(&tokenizer, TOK_ACC, TOK_BAK,
				TOK_NIL, TOK_LEFT, TOK_RIGHT, TOK_UP, TOK_DOWN,
				TOK_ANY, TOK_LAST, TOK_LIT, TOK_NAME, TOK_NL, -1);
			if (op2_tok == TOK_NL) {
				if (!tpu_add_inst(tpu, inst, op1, op1v, -1, op2v))
					die("load: line %lu, invalid instruction",
						tokenizer.lineno);
				break;
			}

			op2 = (int) tok_to_optype(op2_tok);
			if (op2 == OP_LIT)
				op2v.lit = str_to_lit(tokenizer.tokstr);
			else if (op2 == OP_LABEL) {
				op2v.label = strdup(tokenizer.tokstr);
				if (!op2v.label) die("strdup:");
			}

			if (!tpu_add_inst(tpu, inst, op1, op1v, op2, op2v))
				die("load: line %lu, invalid instruction",
					tokenizer.lineno);
			tok_next_in(&tokenizer, TOK_NL, -1);
			break;
		case TOK_COMMENT:
			tok_next_in(&tokenizer, TOK_NL, -1);
			break;
		case TOK_LABEL:
			if (!label_map_add(&tpu->labels,
					tokenizer.tokstr, tpu->inst_cnt))
				die("load: line %lu, duplicate label",
					tokenizer.lineno);
			break;
		case TOK_NL:
			break;
		default:
			goto disallowed;
		}
	}

	if (stdin_x == 0 || stdin_y == 0)
		die("load: stdin tpu not set");

	if (stdout_x == 0 || stdout_y == 0)
		die("load: stdout tpu not set");

	for (i = 0; i < TPU_MAP_BUCKETS; i++) {
		for (link = tis->tpu_map.buckets[i]; link; link = link->next) {
			tpu_init_ports(link->tpu, &tis->tpu_map);

			if (link->x == stdin_x && link->y == stdin_y) {
				port = &link->tpu->ports[DIR_UP];
				if (port->attached)
					die("load: stdin port in use");
				port->attached = true;
				port->dst_tpu = NULL;
				port->dst_port = &tis->stdin_port;
				port->type = PORT_IN;
				tis->stdin_port.attached = true;
				tis->stdin_port.dst_tpu = link->tpu;
				tis->stdin_port.dst_port = port;
			}

			if (link->x == stdout_x && link->y == stdout_y) {
				port = &link->tpu->ports[DIR_DOWN];
				if (port->attached)
					die("load: stdout port in use");
				port->attached = true;
				port->dst_tpu = NULL;
				port->dst_port = &tis->stdout_port;
				port->type = PORT_OUT;
				tis->stdout_port.attached = true;
				tis->stdout_port.dst_tpu = link->tpu;
				tis->stdout_port.dst_port = port;
			}
		}
	}

	if (!tis->stdin_port.attached)
		die("load: stdin tpu (X%i Y%i) not found",
			stdin_x, stdin_y);

	if (!tis->stdout_port.attached)
		die("load: stdout tpu (X%i Y%i) not found",
			stdout_x, stdout_y);

	fclose(tokenizer.file);

	return;

disallowed:
	if (tok == TOK_NAME) {
		die("load: line %lu, unexpected token '%s'",
			tokenizer.lineno, tokenizer.tokstr);
	} else {
		die("load: line %lu, token %s not allowed here",
			tokenizer.lineno, tok_reprs[tok]);
	}
}