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

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



void disasm_chunk(Chunk *ch) {
	for (size_t ip = 0; ip < ch->blen; ) {
		uint8_t instr = ch->b[ip];
		printf("%04zd\t",ip);
		ip ++;
#define SIMPLE_INSTR(opcode, str) \
	case opcode: puts(str); break;
		switch (instr) {
		SIMPLE_INSTR(OP_RET, "ret")
		case OP_LOADK:;
			uint8_t ix = ch->b[ip++];
			printf("loadk #%d\t; ",ix);
			Val k = ch->c[ix];
			printf("%-4s : ",typename_str(k));
			println_val(k);
			break;
		SIMPLE_INSTR(OP_PRINT, "print")
		SIMPLE_INSTR(OP_ADD, "add")
		SIMPLE_INSTR(OP_SUB, "sub")
		SIMPLE_INSTR(OP_MUL, "mul")
		SIMPLE_INSTR(OP_DIV, "div")
		}
	}
#undef SIMPLE_INSTR
}