Skip to content

Commit 5cbf716

Browse files
committed
Add support for more of the new things to the disassembler
Still not complete, but now supports the different types of invokables and some more bytecodes. Signed-off-by: Stefan Marr <git@stefan-marr.de>
1 parent 15de0b5 commit 5cbf716

13 files changed

+173
-44
lines changed

src/compiler/Disassembler.cpp

Lines changed: 127 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,8 @@ void Disassembler::Dump(VMClass* cl) {
9696
VMSymbol* cname = cl->GetName();
9797
DebugDump("%s>>%s = ", cname->GetStdString().c_str(),
9898
sig->GetStdString().c_str());
99-
if (inv->IsPrimitive()) {
100-
DebugPrint("<primitive>\n");
101-
continue;
102-
}
103-
// output actual method
104-
DumpMethod(static_cast<VMMethod*>(inv), "\t");
99+
100+
inv->Dump("\t", true);
105101
}
106102
}
107103

@@ -154,6 +150,12 @@ void Disassembler::dumpMethod(uint8_t* bytecodes, size_t numberOfBytecodes,
154150
}
155151

156152
switch (bytecode) {
153+
case BC_PUSH_0:
154+
case BC_PUSH_1:
155+
case BC_PUSH_NIL: {
156+
// no more details to be printed
157+
break;
158+
}
157159
case BC_PUSH_LOCAL_0: {
158160
DebugPrint("local: 0, context: 0\n");
159161
break;
@@ -199,11 +201,16 @@ void Disassembler::dumpMethod(uint8_t* bytecodes, size_t numberOfBytecodes,
199201
if (method != nullptr && printObjects) {
200202
vm_oop_t constant = method->GetConstant(bc_idx);
201203
VMClass* cl = CLASS_OF(constant);
202-
VMSymbol* cname = cl->GetName();
203-
204-
DebugPrint("(index: %d) value: (%s) ",
205-
bytecodes[bc_idx + 1],
206-
cname->GetStdString().c_str());
204+
if (cl == nullptr) {
205+
DebugPrint("(index: %d) value: (%s) ",
206+
bytecodes[bc_idx + 1],
207+
"class==nullptr");
208+
} else {
209+
VMSymbol* cname = cl->GetName();
210+
DebugPrint("(index: %d) value: (%s) ",
211+
bytecodes[bc_idx + 1],
212+
cname->GetStdString().c_str());
213+
}
207214
dispatch(constant);
208215
} else {
209216
DebugPrint("(index: %d)", bytecodes[bc_idx + 1]);
@@ -235,6 +242,15 @@ void Disassembler::dumpMethod(uint8_t* bytecodes, size_t numberOfBytecodes,
235242
DebugPrint("local: %d, context: %d\n", bytecodes[bc_idx + 1],
236243
bytecodes[bc_idx + 2]);
237244
break;
245+
case BC_POP_LOCAL_0:
246+
DebugPrint("local: 0, context: 0\n");
247+
break;
248+
case BC_POP_LOCAL_1:
249+
DebugPrint("local: 1, context: 0\n");
250+
break;
251+
case BC_POP_LOCAL_2:
252+
DebugPrint("local: 2, context: 0\n");
253+
break;
238254
case BC_POP_ARGUMENT:
239255
DebugPrint("argument: %d, context: %d\n", bytecodes[bc_idx + 1],
240256
bytecodes[bc_idx + 2]);
@@ -266,7 +282,8 @@ void Disassembler::dumpMethod(uint8_t* bytecodes, size_t numberOfBytecodes,
266282
}
267283
break;
268284
}
269-
case BC_SEND: {
285+
case BC_SEND:
286+
case BC_SEND_1: {
270287
if (method != nullptr && printObjects) {
271288
auto* name =
272289
static_cast<VMSymbol*>(method->GetConstant(bc_idx));
@@ -295,12 +312,22 @@ void Disassembler::dumpMethod(uint8_t* bytecodes, size_t numberOfBytecodes,
295312
case BC_JUMP_ON_TRUE_POP:
296313
case BC_JUMP_ON_FALSE_TOP_NIL:
297314
case BC_JUMP_ON_TRUE_TOP_NIL:
315+
case BC_JUMP_ON_NOT_NIL_POP:
316+
case BC_JUMP_ON_NIL_POP:
317+
case BC_JUMP_ON_NOT_NIL_TOP_TOP:
318+
case BC_JUMP_ON_NIL_TOP_TOP:
319+
case BC_JUMP_IF_GREATER:
298320
case BC_JUMP_BACKWARD:
299321
case BC_JUMP2:
300322
case BC_JUMP2_ON_FALSE_POP:
301323
case BC_JUMP2_ON_TRUE_POP:
302324
case BC_JUMP2_ON_FALSE_TOP_NIL:
303325
case BC_JUMP2_ON_TRUE_TOP_NIL:
326+
case BC_JUMP2_ON_NOT_NIL_POP:
327+
case BC_JUMP2_ON_NIL_POP:
328+
case BC_JUMP2_ON_NOT_NIL_TOP_TOP:
329+
case BC_JUMP2_ON_NIL_TOP_TOP:
330+
case BC_JUMP2_IF_GREATER:
304331
case BC_JUMP2_BACKWARD: {
305332
uint16_t const offset =
306333
ComputeOffset(bytecodes[bc_idx + 1], bytecodes[bc_idx + 2]);
@@ -331,6 +358,47 @@ void Disassembler::dumpMethod(uint8_t* bytecodes, size_t numberOfBytecodes,
331358
#define BC_1 method->GetBytecode(bc_idx + 1)
332359
#define BC_2 method->GetBytecode(bc_idx + 2)
333360

361+
void Disassembler::printArgument(uint8_t idx, uint8_t ctx, VMClass* cl,
362+
VMFrame* frame) {
363+
vm_oop_t o = frame->GetArgument(idx, ctx);
364+
DebugPrint("argument: %d, context: %d", idx, ctx);
365+
366+
if (cl != nullptr) {
367+
VMClass* c = CLASS_OF(o);
368+
VMSymbol* cname = c->GetName();
369+
370+
DebugPrint("<(%s) ", cname->GetStdString().c_str());
371+
dispatch(o);
372+
DebugPrint(">");
373+
}
374+
DebugPrint("\n");
375+
}
376+
377+
void Disassembler::printPopLocal(uint8_t idx, uint8_t ctx, VMFrame* frame) {
378+
vm_oop_t o = frame->GetStackElement(0);
379+
VMClass* c = CLASS_OF(o);
380+
VMSymbol* cname = c->GetName();
381+
382+
DebugPrint("popped local: %d, context: %d <(%s) ", idx, ctx,
383+
cname->GetStdString().c_str());
384+
dispatch(o);
385+
DebugPrint(">\n");
386+
}
387+
388+
void Disassembler::printNth(uint8_t idx, VMFrame* frame, const char* op) {
389+
vm_oop_t o = frame->GetStackElement(idx);
390+
if (o != nullptr) {
391+
VMClass* c = CLASS_OF(o);
392+
VMSymbol* cname = c->GetName();
393+
394+
DebugPrint("<to %s: (%s) ", op, cname->GetStdString().c_str());
395+
dispatch(o);
396+
} else {
397+
DebugPrint("<to %s: address: %p", op, (void*)o);
398+
}
399+
DebugPrint(">\n");
400+
}
401+
334402
/**
335403
* Dump bytecode from the frame running
336404
*/
@@ -361,22 +429,30 @@ void Disassembler::DumpBytecode(VMFrame* frame, VMMethod* method,
361429
}
362430

363431
switch (bc) {
432+
case BC_PUSH_0:
433+
case BC_PUSH_1:
434+
case BC_PUSH_NIL: {
435+
// no more details to be printed
436+
break;
437+
}
364438
case BC_HALT: {
365439
DebugPrint("<halting>\n\n\n");
366440
break;
367441
}
368442
case BC_DUP: {
369-
vm_oop_t o = frame->GetStackElement(0);
370-
if (o != nullptr) {
371-
VMClass* c = CLASS_OF(o);
372-
VMSymbol* cname = c->GetName();
373-
374-
DebugPrint("<to dup: (%s) ", cname->GetStdString().c_str());
375-
dispatch(o);
376-
} else {
377-
DebugPrint("<to dup: address: %p", (void*)o);
378-
}
379-
DebugPrint(">\n");
443+
printNth(0, frame, "dup");
444+
break;
445+
}
446+
case BC_DUP_SECOND: {
447+
printNth(1, frame, "dup-second");
448+
break;
449+
}
450+
case BC_INC: {
451+
printNth(0, frame, "inc");
452+
break;
453+
}
454+
case BC_DEC: {
455+
printNth(0, frame, "dec");
380456
break;
381457
}
382458
case BC_PUSH_LOCAL: {
@@ -425,21 +501,22 @@ void Disassembler::DumpBytecode(VMFrame* frame, VMMethod* method,
425501
DebugPrint(">\n");
426502
break;
427503
}
504+
case BC_PUSH_SELF: {
505+
printArgument(0, 0, cl, frame);
506+
break;
507+
}
508+
case BC_PUSH_ARG_1: {
509+
printArgument(1, 0, cl, frame);
510+
break;
511+
}
512+
case BC_PUSH_ARG_2: {
513+
printArgument(2, 0, cl, frame);
514+
break;
515+
}
428516
case BC_PUSH_ARGUMENT: {
429517
uint8_t const bc1 = BC_1;
430518
uint8_t const bc2 = BC_2;
431-
vm_oop_t o = frame->GetArgument(bc1, bc2);
432-
DebugPrint("argument: %d, context: %d", bc1, bc2);
433-
434-
if (cl != nullptr) {
435-
VMClass* c = CLASS_OF(o);
436-
VMSymbol* cname = c->GetName();
437-
438-
DebugPrint("<(%s) ", cname->GetStdString().c_str());
439-
dispatch(o);
440-
DebugPrint(">");
441-
}
442-
DebugPrint("\n");
519+
printArgument(bc1, bc2, cl, frame);
443520
break;
444521
}
445522
case BC_PUSH_BLOCK: {
@@ -493,14 +570,19 @@ void Disassembler::DumpBytecode(VMFrame* frame, VMMethod* method,
493570
break;
494571
}
495572
case BC_POP_LOCAL: {
496-
vm_oop_t o = frame->GetStackElement(0);
497-
VMClass* c = CLASS_OF(o);
498-
VMSymbol* cname = c->GetName();
499-
500-
DebugPrint("popped local: %d, context: %d <(%s) ", BC_1, BC_2,
501-
cname->GetStdString().c_str());
502-
dispatch(o);
503-
DebugPrint(">\n");
573+
printPopLocal(BC_1, BC_2, frame);
574+
break;
575+
}
576+
case BC_POP_LOCAL_0: {
577+
printPopLocal(0, 0, frame);
578+
break;
579+
}
580+
case BC_POP_LOCAL_1: {
581+
printPopLocal(1, 0, frame);
582+
break;
583+
}
584+
case BC_POP_LOCAL_2: {
585+
printPopLocal(2, 0, frame);
504586
break;
505587
}
506588
case BC_POP_ARGUMENT: {
@@ -535,7 +617,8 @@ void Disassembler::DumpBytecode(VMFrame* frame, VMMethod* method,
535617
break;
536618
}
537619
case BC_SUPER_SEND:
538-
case BC_SEND: {
620+
case BC_SEND:
621+
case BC_SEND_1: {
539622
auto* sel = static_cast<VMSymbol*>(method->GetConstant(bc_idx));
540623

541624
DebugPrint("(index: %d) signature: %s (", BC_1,

src/compiler/Disassembler.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ class Disassembler {
3737
static void DumpMethod(VMMethod* method, const char* indent,
3838
bool printObjects = true);
3939
static void DumpMethod(MethodGenerationContext* mgenc, const char* indent);
40+
static void extracted(uint8_t bc1, uint8_t bc2, VMClass* cl,
41+
VMFrame* frame);
42+
4043
static void DumpBytecode(VMFrame* frame, VMMethod* method, size_t bc_idx);
4144

4245
private:
@@ -45,4 +48,9 @@ class Disassembler {
4548
static void dumpMethod(uint8_t* bytecodes, size_t numberOfBytecodes,
4649
const char* indent, VMMethod* method,
4750
bool printObjects);
51+
52+
static void printArgument(uint8_t idx, uint8_t ctx, VMClass* cl,
53+
VMFrame* frame);
54+
static void printPopLocal(uint8_t idx, uint8_t ctx, VMFrame* frame);
55+
static void printNth(uint8_t idx, VMFrame* frame, const char* op);
4856
};

src/vmobjects/VMEvaluationPrimitive.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include <string>
3333

3434
#include "../memory/Heap.h"
35+
#include "../misc/debug.h"
3536
#include "../misc/defs.h"
3637
#include "../vm/Print.h"
3738
#include "../vm/Symbols.h"
@@ -139,3 +140,8 @@ void VMEvaluationPrimitive::InlineInto(MethodGenerationContext& /*mgenc*/,
139140
"VMEvaluationPrimitive::InlineInto is not supported, and should not be "
140141
"reached");
141142
}
143+
144+
void VMEvaluationPrimitive::Dump(const char* /*indent*/,
145+
bool /*printObjects*/) {
146+
DebugPrint("<primitive>\n");
147+
}

src/vmobjects/VMEvaluationPrimitive.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ class VMEvaluationPrimitive : public VMInvokable {
5858
return numberOfArguments;
5959
}
6060

61+
void Dump(const char* indent, bool printObjects) override;
62+
6163
private:
6264
static VMSymbol* computeSignatureString(size_t argc);
6365
void evaluationRoutine(VMFrame*);

src/vmobjects/VMInvokable.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ class VMInvokable : public AbstractVMObject {
7676
holder = (GCClass*)INVALID_GC_POINTER;
7777
}
7878

79+
virtual void Dump(const char* indent, bool printObjects) = 0;
80+
7981
protected:
8082
make_testable(public);
8183

src/vmobjects/VMMethod.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include <string>
3535

3636
#include "../compiler/BytecodeGenerator.h"
37+
#include "../compiler/Disassembler.h"
3738
#include "../compiler/LexicalScope.h"
3839
#include "../compiler/MethodGenerationContext.h"
3940
#include "../compiler/Variable.h"
@@ -164,6 +165,10 @@ void VMMethod::SetHolderAll(VMClass* hld) {
164165
}
165166
}
166167

168+
void VMMethod::Dump(const char* indent, bool printObjects) {
169+
Disassembler::DumpMethod(this, indent, printObjects);
170+
}
171+
167172
std::string VMMethod::AsDebugString() const {
168173
VMClass* holder = GetHolder();
169174
std::string holder_str;

src/vmobjects/VMMethod.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,8 @@ class VMMethod : public VMInvokable {
181181
return lexicalScope->GetArgument(index, contextLevel);
182182
}
183183

184+
void Dump(const char* indent, bool printObjects) override;
185+
184186
private:
185187
void inlineInto(MethodGenerationContext& mgenc);
186188
std::priority_queue<BackJump> createBackJumpHeap();

src/vmobjects/VMPrimitive.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include <string>
3030

3131
#include "../memory/Heap.h"
32+
#include "../misc/debug.h"
3233
#include "../misc/defs.h"
3334
#include "../primitivesCore/Primitives.h"
3435
#include "../vm/Globals.h" // NOLINT (misc-include-cleaner)
@@ -73,3 +74,7 @@ std::string VMPrimitive::AsDebugString() const {
7374
return "Primitive(" + GetClass()->GetName()->GetStdString() + ">>#" +
7475
GetSignature()->GetStdString() + ")";
7576
}
77+
78+
void VMPrimitive::Dump(const char* /*indent*/, bool /*printObjects*/) {
79+
DebugPrint("<primitive>\n");
80+
}

src/vmobjects/VMPrimitive.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ class VMPrimitive : public VMInvokable {
8686
return Signature::GetNumberOfArguments(load_ptr(signature));
8787
}
8888

89+
void Dump(const char* indent, bool printObjects) override;
90+
8991
private:
9092
make_testable(public);
9193

src/vmobjects/VMSafePrimitive.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <string>
44

55
#include "../memory/Heap.h"
6+
#include "../misc/debug.h"
67
#include "../misc/defs.h"
78
#include "../primitivesCore/Primitives.h"
89
#include "../vm/Print.h"
@@ -97,3 +98,7 @@ void VMSafePrimitive::InlineInto(MethodGenerationContext& /*mgenc*/,
9798
ErrorExit(
9899
"VMPrimitive::InlineInto is not supported, and should not be reached");
99100
}
101+
102+
void VMSafePrimitive::Dump(const char* /*indent*/, bool /*printObjects*/) {
103+
DebugPrint("<primitive>\n");
104+
}

0 commit comments

Comments
 (0)