summaryrefslogtreecommitdiff
path: root/dis.c
diff options
context:
space:
mode:
Diffstat (limited to 'dis.c')
-rw-r--r--dis.c36
1 files changed, 36 insertions, 0 deletions
diff --git a/dis.c b/dis.c
new file mode 100644
index 0000000..09e7571
--- /dev/null
+++ b/dis.c
@@ -0,0 +1,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 : ",valty_str(k.ty));
+ 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
+}
+
+
+
+