2019-06-02 14:58:15 -05:00
|
|
|
#include "debug.h"
|
|
|
|
#include "chunk.h"
|
|
|
|
#include "common.h"
|
|
|
|
#include "value.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
void disassembleChunk(Chunk* chunk, const char* name) {
|
|
|
|
printf("== %s ==\n", name);
|
|
|
|
|
|
|
|
for (int offset = 0; offset < chunk->count;) {
|
|
|
|
offset = disassembleInstruction(chunk, offset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int constantInstruction(const char* name, Chunk* chunk, int offset) {
|
|
|
|
uint8_t constant = chunk->code[offset + 1];
|
|
|
|
printf("%-16s %4d '", name, constant);
|
|
|
|
printValue(chunk->constants.values[constant]);
|
|
|
|
printf("'\n");
|
|
|
|
return offset + 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int longConstantInstruction(const char* name, Chunk* chunk, int offset) {
|
|
|
|
uint8_t constant_lo = chunk->code[offset + 3];
|
|
|
|
uint8_t constant_mid = chunk->code[offset + 2];
|
|
|
|
uint8_t constant_hi = chunk->code[offset + 1];
|
|
|
|
int constant = constant_lo | (constant_mid << 8) | constant_hi << 16;
|
|
|
|
printf("%-16s %9d '", name, constant);
|
|
|
|
// printValue(chunk->constants.values[constant]);
|
|
|
|
printf("'\n");
|
|
|
|
return offset + 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int simpleInstruction(const char* name, int offset) {
|
|
|
|
printf("%s\n", name);
|
|
|
|
return offset + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int byteInstruction(const char* name, Chunk* chunk, int offset) {
|
|
|
|
uint8_t slot = chunk->code[offset + 1];
|
|
|
|
printf("%-16s %4d\n", name, slot);
|
|
|
|
return offset + 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int threeByteInstruction(const char* name, Chunk* chunk, int offset) {
|
|
|
|
uint8_t slot_lo = chunk->code[offset + 3];
|
|
|
|
uint8_t slot_mid = chunk->code[offset + 2];
|
|
|
|
uint8_t slot_hi = chunk->code[offset + 1];
|
|
|
|
int slot = slot_lo | (slot_mid << 8) | slot_hi << 16;
|
|
|
|
printf("%-16s %9d\n", name, slot);
|
|
|
|
return offset + 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int jumpInstruction(const char* name, int sign, Chunk* chunk, int offset) {
|
|
|
|
uint16_t jump = (uint16_t) (chunk->code[offset + 2] << 8);
|
|
|
|
jump |= chunk->code[offset + 1];
|
|
|
|
|
|
|
|
printf("%-16s %4d -> %d\n", name, offset, offset + 3 + sign * jump);
|
|
|
|
return offset + 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
int disassembleInstruction(Chunk* chunk, int offset) {
|
|
|
|
printf("%04d ", offset);
|
|
|
|
if (offset > 0 && chunk->lines[offset] == chunk->lines[offset - 1]) {
|
|
|
|
printf(" | ");
|
|
|
|
} else {
|
|
|
|
printf("%4d ", chunk->lines[offset]);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t instruction = chunk->code[offset];
|
|
|
|
switch (instruction) {
|
|
|
|
case OP_ARRAY:
|
|
|
|
return byteInstruction("OP_ARRAY", chunk, offset);
|
|
|
|
case OP_CONSTANT:
|
|
|
|
return constantInstruction("OP_CONSTANT", chunk, offset);
|
|
|
|
case OP_CONSTANT_LONG:
|
|
|
|
return longConstantInstruction("OP_CONSTANT_LONG", chunk, offset);
|
|
|
|
case OP_HASH:
|
|
|
|
return byteInstruction("OP_HASH", chunk, offset);
|
|
|
|
case OP_NIL:
|
|
|
|
return simpleInstruction("OP_NIL", offset);
|
|
|
|
case OP_TRUE:
|
|
|
|
return simpleInstruction("OP_TRUE", offset);
|
|
|
|
case OP_FALSE:
|
|
|
|
return simpleInstruction("OP_FALSE", offset);
|
|
|
|
case OP_POP:
|
|
|
|
return simpleInstruction("OP_POP", offset);
|
|
|
|
case OP_INDEX:
|
|
|
|
return simpleInstruction("OP_INDEX", offset);
|
2019-06-03 14:38:44 -05:00
|
|
|
case OP_INDEX_FLIPPED:
|
|
|
|
return simpleInstruction("OP_INDEX_FLIPPED", offset);
|
|
|
|
case OP_SET_INDEX:
|
|
|
|
return simpleInstruction("OP_SET_INDEX", offset);
|
2019-06-02 14:58:15 -05:00
|
|
|
case OP_GET_LOCAL:
|
|
|
|
return byteInstruction("OP_GET_LOCAL", chunk, offset);
|
|
|
|
case OP_SET_LOCAL:
|
|
|
|
return byteInstruction("OP_SET_LOCAL", chunk, offset);
|
|
|
|
case OP_GET_LOCAL_LONG:
|
|
|
|
return threeByteInstruction("OP_GET_LOCAL_LONG", chunk, offset);
|
|
|
|
case OP_SET_LOCAL_LONG:
|
|
|
|
return threeByteInstruction("OP_SET_LOCAL_LONG", chunk, offset);
|
|
|
|
case OP_GET_GLOBAL:
|
|
|
|
return constantInstruction("OP_GET_GLOBAL", chunk, offset);
|
|
|
|
case OP_GET_GLOBAL_LONG:
|
|
|
|
return longConstantInstruction("OP_GET_GLOBAL_LONG", chunk, offset);
|
|
|
|
case OP_DEFINE_GLOBAL:
|
|
|
|
return constantInstruction("OP_DEFINE_GLOBAL", chunk, offset);
|
|
|
|
case OP_DEFINE_GLOBAL_LONG:
|
|
|
|
return longConstantInstruction("OP_DEFINE_GLOBAL_LONG", chunk, offset);
|
|
|
|
case OP_SET_GLOBAL:
|
|
|
|
return constantInstruction("OP_SET_GLOBAL", chunk, offset);
|
|
|
|
case OP_SET_GLOBAL_LONG:
|
|
|
|
return longConstantInstruction("OP_SET_GLOBAL_LONG", chunk, offset);
|
|
|
|
case OP_EQUAL:
|
|
|
|
return simpleInstruction("OP_EQUAL", offset);
|
|
|
|
case OP_GREATER:
|
|
|
|
return simpleInstruction("OP_GREATER", offset);
|
|
|
|
case OP_LESS:
|
|
|
|
return simpleInstruction("OP_LESS", offset);
|
|
|
|
case OP_ADD:
|
|
|
|
return simpleInstruction("OP_ADD", offset);
|
|
|
|
case OP_SUBTRACT:
|
|
|
|
return simpleInstruction("OP_SUBTRACT", offset);
|
|
|
|
case OP_MULTIPLY:
|
|
|
|
return simpleInstruction("OP_MULTIPLY", offset);
|
|
|
|
case OP_DIVIDE:
|
|
|
|
return simpleInstruction("OP_DIVIDE", offset);
|
|
|
|
case OP_NOT:
|
|
|
|
return simpleInstruction("OP_NOT", offset);
|
|
|
|
case OP_NEGATE:
|
|
|
|
return simpleInstruction("OP_NEGATE", offset);
|
|
|
|
case OP_PRINT:
|
|
|
|
return simpleInstruction("OP_PRINT", offset);
|
|
|
|
case OP_JUMP:
|
|
|
|
return jumpInstruction("OP_JUMP", 1, chunk, offset);
|
|
|
|
case OP_JUMP_IF_FALSE:
|
|
|
|
return jumpInstruction("OP_JUMP_IF_FALSE", 1, chunk, offset);
|
|
|
|
case OP_LOOP:
|
|
|
|
return jumpInstruction("OP_LOOP", -1, chunk, offset);
|
|
|
|
case OP_RETURN:
|
|
|
|
return simpleInstruction("OP_RETURN", offset);
|
|
|
|
default:
|
|
|
|
printf("Unknown opcode %d\n", instruction);
|
|
|
|
return offset + 1;
|
|
|
|
}
|
|
|
|
}
|