Skip to content

Commit

Permalink
Added debug.h DebugDumpMethod() for convenience
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan Marr <[email protected]>
  • Loading branch information
smarr committed Jul 31, 2024
1 parent 61bb66a commit ed8667e
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 15 deletions.
26 changes: 13 additions & 13 deletions src/compiler/Disassembler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,20 +105,20 @@ void Disassembler::Dump(VMClass* cl) {
/**
* Dump all Bytecode of a method.
*/
void Disassembler::DumpMethod(VMMethod* method, const char* indent) {
dumpMethod(method->GetBytecodes(), method->GetNumberOfBytecodes(), indent, method);
void Disassembler::DumpMethod(VMMethod* method, const char* indent, bool printObjects) {
dumpMethod(method->GetBytecodes(), method->GetNumberOfBytecodes(), indent, method, printObjects);
}

void Disassembler::DumpMethod(MethodGenerationContext* mgenc, const char* indent) {
auto bytecodes = mgenc->GetBytecodes();
dumpMethod(bytecodes.data(), bytecodes.size(), indent, nullptr);
dumpMethod(bytecodes.data(), bytecodes.size(), indent, nullptr, true);
}

void Disassembler::dumpMethod(uint8_t* bytecodes, size_t numberOfBytecodes, const char* indent, VMMethod* method) {
void Disassembler::dumpMethod(uint8_t* bytecodes, size_t numberOfBytecodes, const char* indent, VMMethod* method, bool printObjects) {
DebugPrint("(\n");
if (method != nullptr) { // output stack information
long locals = method->GetNumberOfLocals();
long max_stack = method->GetMaximumNumberOfStackElements();
size_t locals = method->GetNumberOfLocals();
size_t max_stack = method->GetMaximumNumberOfStackElements();
DebugDump("%s<%d locals, %d stack, %d bc_count>\n", indent, locals,
max_stack, method->GetNumberOfBytecodes());

Expand Down Expand Up @@ -163,7 +163,7 @@ void Disassembler::dumpMethod(uint8_t* bytecodes, size_t numberOfBytecodes, cons
}
case BC_PUSH_FIELD: {
long fieldIdx = bytecodes[bc_idx+1];
if (method != nullptr) {
if (method != nullptr && printObjects) {
VMClass* holder = dynamic_cast<VMClass*>((VMObject*) method->GetHolder());
if (holder) {
VMSymbol* name = holder->GetInstanceFieldName(fieldIdx);
Expand All @@ -184,7 +184,7 @@ void Disassembler::dumpMethod(uint8_t* bytecodes, size_t numberOfBytecodes, cons
char* nindent = new char[indent_size];
DebugPrint("block: (index: %d) ", bytecodes[bc_idx+1]);

if (method != nullptr) {
if (method != nullptr && printObjects) {
snprintf(nindent, indent_size, "%s\t", indent);
Disassembler::DumpMethod(static_cast<VMMethod*>(method->GetConstant(bc_idx)), nindent);
} else {
Expand All @@ -194,7 +194,7 @@ void Disassembler::dumpMethod(uint8_t* bytecodes, size_t numberOfBytecodes, cons
break;
}
case BC_PUSH_CONSTANT: {
if (method != nullptr) {
if (method != nullptr && printObjects) {
vm_oop_t constant = method->GetConstant(bc_idx);
VMClass* cl = CLASS_OF(constant);
VMSymbol* cname = cl->GetName();
Expand All @@ -209,7 +209,7 @@ void Disassembler::dumpMethod(uint8_t* bytecodes, size_t numberOfBytecodes, cons
break;
}
case BC_PUSH_GLOBAL: {
if (method != nullptr) {
if (method != nullptr && printObjects) {
vm_oop_t cst = method->GetConstant(bc_idx);
if (cst != nullptr) {
VMSymbol* name = static_cast<VMSymbol*>(cst);
Expand All @@ -233,7 +233,7 @@ void Disassembler::dumpMethod(uint8_t* bytecodes, size_t numberOfBytecodes, cons
break;
case BC_POP_FIELD: {
long fieldIdx = bytecodes[bc_idx+1];
if (method != nullptr) {
if (method != nullptr && printObjects) {
VMClass* holder = dynamic_cast<VMClass*>((VMObject*) method->GetHolder());
if (holder) {
VMSymbol* name = holder->GetInstanceFieldName(fieldIdx);
Expand All @@ -247,7 +247,7 @@ void Disassembler::dumpMethod(uint8_t* bytecodes, size_t numberOfBytecodes, cons
break;
}
case BC_SEND: {
if (method != nullptr) {
if (method != nullptr && printObjects) {
VMSymbol* name = static_cast<VMSymbol*>(method->GetConstant(bc_idx));
DebugPrint("(index: %d) signature: %s\n", bytecodes[bc_idx+1], name->GetStdString().c_str());
} else {
Expand All @@ -256,7 +256,7 @@ void Disassembler::dumpMethod(uint8_t* bytecodes, size_t numberOfBytecodes, cons
break;
}
case BC_SUPER_SEND: {
if (method != nullptr) {
if (method != nullptr && printObjects) {
VMSymbol* name = static_cast<VMSymbol*>(method->GetConstant(bc_idx));
DebugPrint("(index: %d) signature: %s\n", bytecodes[bc_idx+1], name->GetStdString().c_str());
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/Disassembler.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@
class Disassembler {
public:
static void Dump(VMClass* cl);
static void DumpMethod(VMMethod* method, const char* indent);
static void DumpMethod(VMMethod* method, const char* indent, bool printObjects = true);
static void DumpMethod(MethodGenerationContext* mgenc, const char* indent);
static void DumpBytecode(VMFrame* frame, VMMethod* method, long bc_idx);
private:
static void dispatch(vm_oop_t o);

static void dumpMethod(uint8_t* bytecodes, size_t numberOfBytecodes, const char* indent, VMMethod* method);
static void dumpMethod(uint8_t* bytecodes, size_t numberOfBytecodes, const char* indent, VMMethod* method, bool printObjects);
};
5 changes: 5 additions & 0 deletions src/misc/debug.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <string>

#include "../compiler/Disassembler.h"
#include "../vmobjects/ObjectFormats.h"
#include "../vmobjects/VMClass.h"
#include "../vmobjects/VMSymbol.h"
Expand All @@ -13,3 +14,7 @@ std::string DebugGetClassName(vm_oop_t obj) {
std::string DebugGetClassName(gc_oop_t obj) {
return CLASS_OF(obj)->GetName()->GetStdString();
}

void DebugDumpMethod(VMMethod* method) {
Disassembler::DumpMethod(method, "", false);
}
1 change: 1 addition & 0 deletions src/misc/debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,4 @@ static inline void DebugTrace(const char* fmt, ...) {

std::string DebugGetClassName(vm_oop_t);
std::string DebugGetClassName(gc_oop_t);
void DebugDumpMethod(VMMethod* method);

0 comments on commit ed8667e

Please sign in to comment.