2020-10-28 05:16:30 +00:00
|
|
|
#include "cdebug.h"
|
|
|
|
#include "cvalue.h"
|
|
|
|
#include "cobj.h"
|
|
|
|
|
|
|
|
void printIndent(int indent) {
|
|
|
|
for (int i = 0; i < indent; i++)
|
|
|
|
printf("\t");
|
|
|
|
}
|
|
|
|
|
|
|
|
int simpleInstruction(const char *name, int offset) {
|
|
|
|
printf("%s", name);
|
|
|
|
return offset + 1; // consume opcode
|
|
|
|
}
|
|
|
|
|
2020-11-19 20:41:21 +00:00
|
|
|
int u8OperandInstruction(const char *name, CChunk *chunk, int offset) {
|
2020-10-28 05:16:30 +00:00
|
|
|
printf("%-16s [%03d]", name, readu8Chunk(chunk, offset + 1));
|
|
|
|
return offset + 2;
|
|
|
|
}
|
|
|
|
|
2020-11-19 20:41:21 +00:00
|
|
|
int u16OperandInstruction(const char *name, CChunk *chunk, int offset) {
|
2020-10-28 05:16:30 +00:00
|
|
|
printf("%-16s [%05d]", name, readu16Chunk(chunk, offset + 1));
|
|
|
|
return offset + 1 + (sizeof(uint16_t) / sizeof(INSTRUCTION));
|
|
|
|
}
|
|
|
|
|
2021-01-16 21:40:58 +00:00
|
|
|
int JumpInstruction(const char *name, CChunk *chunk, int offset, int dir) {
|
|
|
|
int jmp = ((int)readu16Chunk(chunk, offset + 1)) * dir;
|
|
|
|
printf("%-16s [%05d] - jumps to %04d", name, jmp, offset + 3 + jmp);
|
|
|
|
return offset + 1 + (sizeof(uint16_t) / sizeof(INSTRUCTION));
|
|
|
|
}
|
|
|
|
|
2020-11-19 20:41:21 +00:00
|
|
|
int u8u8OperandInstruction(const char *name, CChunk *chunk, int offset) {
|
|
|
|
printf("%-16s [%03d] [%03d]", name, readu8Chunk(chunk, offset + 1), readu8Chunk(chunk, offset + 2));
|
|
|
|
return offset + 3; // op + u8 + u8
|
|
|
|
}
|
|
|
|
|
|
|
|
int u8u16OperandInstruction(const char *name, CChunk *chunk, int offset) {
|
|
|
|
printf("%-16s [%03d] [%05d]", name, readu8Chunk(chunk, offset + 1), readu16Chunk(chunk, offset + 2));
|
|
|
|
return offset + 4; // op + u8 + u16
|
|
|
|
}
|
|
|
|
|
2021-01-08 02:02:19 +00:00
|
|
|
int u8u8u16OperandInstruction(const char *name, CChunk *chunk, int offset) {
|
|
|
|
printf("%-16s [%03d] [%03d] [%05d]", name, readu8Chunk(chunk, offset + 1), readu8Chunk(chunk, offset + 2), readu16Chunk(chunk, offset + 3));
|
|
|
|
return offset + 5; // op + u8 + u8 + u16
|
|
|
|
}
|
|
|
|
|
2021-01-02 05:06:24 +00:00
|
|
|
int constInstruction(const char *name, CChunk *chunk, int offset) {
|
2020-10-28 05:16:30 +00:00
|
|
|
int index = readu16Chunk(chunk, offset + 1);
|
|
|
|
printf("%-16s [%05d] - ", name, index);
|
|
|
|
CValue val = chunk->constants.values[index];
|
|
|
|
|
|
|
|
printValue(val);
|
|
|
|
|
|
|
|
return offset + 1 + (sizeof(uint16_t) / sizeof(INSTRUCTION)); // consume opcode + uint
|
|
|
|
}
|
|
|
|
|
|
|
|
// public methods in the cdebug.h header
|
|
|
|
|
|
|
|
void disasmChunk(CChunk *chunk, const char *name, int indent) {
|
|
|
|
printIndent(indent);
|
2021-01-16 21:40:58 +00:00
|
|
|
printf("===[[ disasm for %s ]]===\n", name);
|
2020-10-28 05:16:30 +00:00
|
|
|
|
2021-01-02 05:06:24 +00:00
|
|
|
for (size_t offset = 0; offset < chunk->count;) {
|
2020-10-28 05:16:30 +00:00
|
|
|
offset = disasmInstr(chunk, offset, indent);
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int disasmInstr(CChunk *chunk, int offset, int indent) {
|
|
|
|
printIndent(indent);
|
|
|
|
printf("%04d ", offset);
|
|
|
|
|
|
|
|
INSTRUCTION i = chunk->buf[offset];
|
|
|
|
int line = chunk->lineInfo[offset];
|
|
|
|
|
|
|
|
if (offset > 0 && line == chunk->lineInfo[offset - 1]) {
|
|
|
|
printf(" | ");
|
|
|
|
} else {
|
|
|
|
printf("%4d ", line);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (i) {
|
|
|
|
case OP_LOADCONST:
|
2021-01-02 05:06:24 +00:00
|
|
|
return constInstruction("OP_LOADCONST", chunk, offset);
|
2020-10-28 05:16:30 +00:00
|
|
|
case OP_SETGLOBAL:
|
2021-01-02 05:06:24 +00:00
|
|
|
return constInstruction("OP_SETGLOBAL", chunk, offset);
|
2020-10-28 05:16:30 +00:00
|
|
|
case OP_GETGLOBAL:
|
2021-01-02 05:06:24 +00:00
|
|
|
return constInstruction("OP_GETGLOBAL", chunk, offset);
|
2020-10-28 05:16:30 +00:00
|
|
|
case OP_SETLOCAL:
|
2020-11-19 20:41:21 +00:00
|
|
|
return u8OperandInstruction("OP_SETLOCAL", chunk, offset);
|
2020-10-28 05:16:30 +00:00
|
|
|
case OP_GETLOCAL:
|
2020-11-19 20:41:21 +00:00
|
|
|
return u8OperandInstruction("OP_GETLOCAL", chunk, offset);
|
2020-10-28 05:16:30 +00:00
|
|
|
case OP_SETUPVAL:
|
2020-11-19 20:41:21 +00:00
|
|
|
return u8OperandInstruction("OP_SETUPVAL", chunk, offset);
|
2020-10-28 05:16:30 +00:00
|
|
|
case OP_GETUPVAL:
|
2020-11-19 20:41:21 +00:00
|
|
|
return u8OperandInstruction("OP_GETUPVAL", chunk, offset);
|
2020-10-28 05:16:30 +00:00
|
|
|
case OP_PEJMP:
|
2021-01-16 21:40:58 +00:00
|
|
|
return JumpInstruction("OP_PEJMP", chunk, offset, 1);
|
2020-10-28 05:16:30 +00:00
|
|
|
case OP_EJMP:
|
2021-01-16 21:40:58 +00:00
|
|
|
return JumpInstruction("OP_EJMP", chunk, offset, 1);
|
2020-10-28 05:16:30 +00:00
|
|
|
case OP_JMP:
|
2021-01-16 21:40:58 +00:00
|
|
|
return JumpInstruction("OP_JMP", chunk, offset, 1);
|
2020-10-28 05:16:30 +00:00
|
|
|
case OP_JMPBACK:
|
2021-01-16 21:40:58 +00:00
|
|
|
return JumpInstruction("OP_JMPBACK", chunk, offset, -1);
|
2020-10-28 05:16:30 +00:00
|
|
|
case OP_POP:
|
2020-11-19 20:41:21 +00:00
|
|
|
return u8OperandInstruction("OP_POP", chunk, offset);
|
2020-10-28 05:16:30 +00:00
|
|
|
case OP_CALL:
|
2020-12-13 03:53:12 +00:00
|
|
|
return u8u8OperandInstruction("OP_CALL", chunk, offset);
|
2020-10-28 05:16:30 +00:00
|
|
|
case OP_CLOSURE: {
|
|
|
|
int index = readu16Chunk(chunk, offset + 1);
|
|
|
|
printf("%-16s [%05d] - ", "OP_CLOSURE", index);
|
|
|
|
CValue val = chunk->constants.values[index];
|
2020-12-04 06:04:14 +00:00
|
|
|
CObjFunction *cobjFunc = (CObjFunction*)cosmoV_readObj(val);
|
2020-10-28 05:16:30 +00:00
|
|
|
offset += 3; // we consumed the opcode + u16
|
|
|
|
|
|
|
|
printValue(val);
|
|
|
|
printf("\n");
|
|
|
|
|
|
|
|
// list the upvalues/locals that are captured
|
|
|
|
for (int i = 0; i < cobjFunc->upvals; i++) {
|
|
|
|
uint8_t encoding = readu8Chunk(chunk, offset++);
|
|
|
|
uint8_t index = readu8Chunk(chunk, offset++);
|
|
|
|
printIndent(indent + 1);
|
|
|
|
printf("references %s [%d]\n", encoding == OP_GETLOCAL ? "local" : "upvalue", index);
|
|
|
|
}
|
|
|
|
|
|
|
|
// print the chunk
|
|
|
|
disasmChunk(&cobjFunc->chunk, cobjFunc->name == NULL ? UNNAMEDCHUNK : cobjFunc->name->str, indent+1);
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
case OP_CLOSE:
|
|
|
|
return simpleInstruction("OP_CLOSE", offset);
|
2021-01-08 20:37:36 +00:00
|
|
|
case OP_NEWTABLE:
|
|
|
|
return u16OperandInstruction("OP_NEWTABLE", chunk, offset);
|
2021-01-09 04:47:36 +00:00
|
|
|
case OP_NEWARRAY:
|
|
|
|
return u16OperandInstruction("OP_NEWARRAY", chunk, offset);
|
2020-12-10 02:32:42 +00:00
|
|
|
case OP_INDEX:
|
|
|
|
return simpleInstruction("OP_INDEX", offset);
|
|
|
|
case OP_NEWINDEX:
|
|
|
|
return simpleInstruction("OP_NEWINDEX", offset);
|
2020-11-04 04:10:51 +00:00
|
|
|
case OP_NEWOBJECT:
|
2020-11-19 20:41:21 +00:00
|
|
|
return u16OperandInstruction("OP_NEWOBJECT", chunk, offset);
|
2020-11-04 04:10:51 +00:00
|
|
|
case OP_SETOBJECT:
|
2021-01-07 23:19:17 +00:00
|
|
|
return constInstruction("OP_SETOBJECT", chunk, offset);
|
2021-01-16 21:40:58 +00:00
|
|
|
case OP_GETOBJECT:
|
|
|
|
return constInstruction("OP_GETOBJECT", chunk, offset);
|
|
|
|
case OP_GETMETHOD:
|
|
|
|
return constInstruction("OP_GETMETHOD", chunk, offset);
|
2020-11-13 23:39:47 +00:00
|
|
|
case OP_INVOKE:
|
2021-01-08 02:02:19 +00:00
|
|
|
return u8u8u16OperandInstruction("OP_INVOKE", chunk, offset);
|
2020-12-16 03:21:51 +00:00
|
|
|
case OP_ITER:
|
|
|
|
return simpleInstruction("OP_ITER", offset);
|
|
|
|
case OP_NEXT:
|
|
|
|
return u8u16OperandInstruction("OP_NEXT", chunk, offset);
|
2020-10-28 05:16:30 +00:00
|
|
|
case OP_ADD:
|
|
|
|
return simpleInstruction("OP_ADD", offset);
|
|
|
|
case OP_SUB:
|
|
|
|
return simpleInstruction("OP_SUB", offset);
|
|
|
|
case OP_MULT:
|
|
|
|
return simpleInstruction("OP_MULT", offset);
|
|
|
|
case OP_DIV:
|
|
|
|
return simpleInstruction("OP_DIV", offset);
|
2021-01-01 06:47:15 +00:00
|
|
|
case OP_MOD:
|
|
|
|
return simpleInstruction("OP_MOD", offset);
|
2020-10-28 05:16:30 +00:00
|
|
|
case OP_TRUE:
|
|
|
|
return simpleInstruction("OP_TRUE", offset);
|
|
|
|
case OP_FALSE:
|
|
|
|
return simpleInstruction("OP_FALSE", offset);
|
|
|
|
case OP_NIL:
|
|
|
|
return simpleInstruction("OP_NIL", offset);
|
|
|
|
case OP_NOT:
|
|
|
|
return simpleInstruction("OP_NOT", offset);
|
|
|
|
case OP_EQUAL:
|
|
|
|
return simpleInstruction("OP_EQUAL", offset);
|
|
|
|
case OP_GREATER:
|
|
|
|
return simpleInstruction("OP_GREATER", offset);
|
|
|
|
case OP_GREATER_EQUAL:
|
|
|
|
return simpleInstruction("OP_GREATER_EQUAL", offset);
|
|
|
|
case OP_LESS:
|
|
|
|
return simpleInstruction("OP_LESS", offset);
|
|
|
|
case OP_LESS_EQUAL:
|
|
|
|
return simpleInstruction("OP_LESS_EQUAL", offset);
|
|
|
|
case OP_NEGATE:
|
|
|
|
return simpleInstruction("OP_NEGATE", offset);
|
2020-11-30 18:32:04 +00:00
|
|
|
case OP_COUNT:
|
|
|
|
return simpleInstruction("OP_COUNT", offset);
|
2020-10-28 05:16:30 +00:00
|
|
|
case OP_CONCAT:
|
2020-11-19 20:41:21 +00:00
|
|
|
return u8OperandInstruction("OP_CONCAT", chunk, offset);
|
|
|
|
case OP_INCLOCAL:
|
|
|
|
return u8u8OperandInstruction("OP_INCLOCAL", chunk, offset);
|
|
|
|
case OP_INCGLOBAL:
|
|
|
|
return u8u16OperandInstruction("OP_INCGLOBAL", chunk, offset);
|
|
|
|
case OP_INCUPVAL:
|
2020-12-19 19:32:43 +00:00
|
|
|
return u8u8OperandInstruction("OP_INCUPVAL", chunk, offset);
|
2020-12-10 02:32:42 +00:00
|
|
|
case OP_INCINDEX:
|
|
|
|
return u8OperandInstruction("OP_INCINDEX", chunk, offset);
|
2020-11-19 20:41:21 +00:00
|
|
|
case OP_INCOBJECT:
|
|
|
|
return u8u16OperandInstruction("OP_INCOBJECT", chunk, offset);
|
2020-10-28 05:16:30 +00:00
|
|
|
case OP_RETURN:
|
2020-12-14 20:38:46 +00:00
|
|
|
return u8OperandInstruction("OP_RETURN", chunk, offset);
|
2020-10-28 05:16:30 +00:00
|
|
|
default:
|
|
|
|
printf("Unknown opcode! [%d]\n", i);
|
2021-01-09 04:47:36 +00:00
|
|
|
return 1;
|
2020-10-28 05:16:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return 1;
|
2021-01-02 05:06:24 +00:00
|
|
|
}
|