First pass at functions + tests

This commit is contained in:
Stephen Dolan committed 2012-08-21 18:14:13 +01:00
1 parent dd0d340eba
commit c128c2029e
15 files changed
+461 -89

No files matched your search

+36 -7
View File
@@ -4,12 +4,29 @@
#include "bytecode.h"
#include "opcode.h"
void dump_disassembly(struct bytecode* bc) {
static int bytecode_operation_length(uint16_t* codeptr) {
if (opcode_describe(*codeptr)->flags & OP_HAS_VARIABLE_LENGTH_ARGLIST) {
return 2 + codeptr[1] * 2;
} else {
return opcode_length(*codeptr);
}
}
void dump_disassembly(int indent, struct bytecode* bc) {
dump_code(indent, bc);
for (int i=0; i<bc->nsubfunctions; i++) {
printf("%*ssubfn[%d]:\n", indent, "", i);
dump_disassembly(indent+2, bc->subfunctions[i]);
}
}
void dump_code(int indent, struct bytecode* bc) {
int pc = 0;
while (pc < bc->codelen) {
printf("%*s", indent, "");
dump_operation(bc, bc->code + pc);
printf("\n");
pc += opcode_length(bc->code[pc]);
pc += bytecode_operation_length(bc->code + pc);
}
}
@@ -20,16 +37,28 @@ void dump_operation(struct bytecode* bc, uint16_t* codeptr) {
printf("%s", op->name);
if (op->flags & OP_HAS_IMMEDIATE) {
uint16_t imm = bc->code[pc++];
printf(" ");
if (op->flags & OP_HAS_BRANCH) {
printf("%04d", pc + imm);
if (op->flags & OP_HAS_VARIABLE_LENGTH_ARGLIST) {
for (int i=0; i<imm; i++) {
uint16_t level = bc->code[pc++];
uint16_t idx = bc->code[pc++];
if (idx & ARG_NEWCLOSURE) {
printf(" subfn[%d]", idx & ~ARG_NEWCLOSURE);
} else {
printf(" param[%d]", idx);
}
if (level) {
printf("^%d", level);
}
}
} else if (op->flags & OP_HAS_BRANCH) {
printf(" %04d", pc + imm);
} else if (op->flags & OP_HAS_CONSTANT) {
json_dumpf(json_array_get(bc->constants, imm),
stdout, JSON_ENCODE_ANY);
} else if (op->flags & OP_HAS_VARIABLE) {
printf("v%d", imm);
printf(" v%d", imm);
} else {
printf("%d", imm);
printf(" %d", imm);
}
}
}