summaryrefslogtreecommitdiff
path: root/dis.c
blob: 9033bff274d23462d8a1372a94df711e6f7ae7c0 (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
#include "dis.h"

#include <stdio.h>
#include <stdint.h>

static void print_const(Chunk *ch, uint8_t ix) {
	Val k = ch->consts.d[ix];
	printf("%-4s : ", typename_str(k));
	println_val(k);
}

void disasm_chunk(Chunk *ch) {
	for (size_t ip = 0; ip < ch->bc.len; ) {
		uint8_t instr = ch->bc.d[ip];
		printf("%04zd\t",ip);
		ip ++;
		switch (instr) {
		case OP_LOADK: {
			uint8_t ix = ch->bc.d[ip++];
			printf("loadk #%d\t; ",ix);
			print_const(ch, ix);
			break;
		}
		case OP_GETGLOBAL: {
			uint8_t ix = ch->bc.d[ip++];
			printf("getglobal #%d\t; ",ix);
			print_const(ch, ix);
			break;
		}
		case OP_SETGLOBAL: {
			uint8_t ix = ch->bc.d[ip++];
			printf("setglobal #%d\t; ",ix);
			print_const(ch, ix);
			break;
		}
		#define RSHORT() (uint16_t)( ch->bc.d[ip-2] | ch->bc.d[ip-1] << 8 )
		case OP_SKIP: {
			ip += 2;
			uint16_t offset = RSHORT();
			printf("skip +%5hu\t; -> %04zd\n", offset, ip + offset);
			break;
		}
		case OP_0BRANCH: {
			ip += 2;
			uint16_t offset = RSHORT();
			printf("0branch +%5hu\t; -> %04zd\n", offset, ip + offset);
			break;
		}
		case OP_REDO: {
			ip += 2;
			uint16_t offset = RSHORT();
			printf("0branch -%5hu\t; -> %04zd\n", offset, ip - offset);
			break;
		}
		#undef RSHORT


#define SIMPLE_INSTR(opcode, str) \
	case opcode: puts(str); break;
		SIMPLE_INSTR(OP_RET, "ret")
		SIMPLE_INSTR(OP_PRINT, "print")
		SIMPLE_INSTR(OP_DROP, "drop")
		SIMPLE_INSTR(OP_ADD, "add")
		SIMPLE_INSTR(OP_SUB, "sub")
		SIMPLE_INSTR(OP_MUL, "mul")
		SIMPLE_INSTR(OP_DIV, "div")
		SIMPLE_INSTR(OP_NIL, "nil")
		SIMPLE_INSTR(OP_TRUE, "true")
		SIMPLE_INSTR(OP_FALSE, "false")
#undef SIMPLE_INSTR

		}
	}
}