-
Notifications
You must be signed in to change notification settings - Fork 0
/
Debug.cpp
50 lines (43 loc) · 1.71 KB
/
Debug.cpp
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
37
38
39
40
41
42
43
44
45
46
47
48
49
#include "Debug.h"
void Debug::disassembleBatch(const Batch &batch) {
std::cout << "== Batch Disassembly ==\n";
std::cout << "[Constants]:\n";
for (auto& constant : batch.constantPool) {
std::cout << "'" << constant.toString() << "'\n";
}
std::cout << "[Op Codes]:\n";
for (size_t instructionIndex = 0; instructionIndex < batch.opcodes.size();) {
instructionIndex += disassembleInstruction(batch, instructionIndex);
}
std::cout << "\n";
}
int Debug::disassembleInstruction(const Batch &batch, size_t instructionIndex) {
//OpCode index
std::cout << instructionIndex << "\t";
//Print piping if on the same line, otherwise print the line number
if (instructionIndex > 0 && batch.lines[instructionIndex] == batch.lines[instructionIndex - 1]) {
std::cout << "| ";
}
else {
std::cout << batch.lines[instructionIndex] << " ";
}
//OpCode name
Op::Code instructionOpCode = batch.opcodes[instructionIndex];
std::cout << Op::instructionName(instructionOpCode) << "\t";
//OpCode-specific behaviour
switch (instructionOpCode) {
case Op::Constant32: {
size_t constantIndex = Memory::toValue<size_t>(batch.opcodes[instructionIndex + 1], batch.opcodes[instructionIndex + 2]);
std::cout << constantIndex << " " << batch.constantPool[constantIndex].toString();
break;
}
case Op::Constant: {
size_t constantIndex = batch.opcodes[instructionIndex + 1];
std::cout << constantIndex << " " << batch.constantPool[constantIndex].toString();
break;
}
default: break;
}
std::cout << "\n";
return Op::instructionLength(instructionOpCode);
}