Skip to content

Commit f94bbcd

Browse files
jgravelle-googlekripken
authored andcommitted
Minor improvements to the wasm-interpreter debug messages (WebAssembly#784)
* Minor improvements to the wasm-interpreter debug messages 1. Indent nested blocks for more readable structure (with numeric labels to make it even clearer) 2. Print the names of the variables used for NOTE_EVALs 3. Print the values of arguments when entering a function call * Move Indenter class to wasm-interpreter.cpp
1 parent b9698cc commit f94bbcd

File tree

3 files changed

+68
-8
lines changed

3 files changed

+68
-8
lines changed

Diff for: CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ TARGET_LINK_LIBRARIES(binaryen asmjs ${all_passes} support)
122122

123123
SET(wasm-shell_SOURCES
124124
src/tools/wasm-shell.cpp
125+
src/wasm-interpreter.cpp
125126
src/wasm.cpp
126127
)
127128
ADD_EXECUTABLE(wasm-shell

Diff for: src/wasm-interpreter.cpp

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include "wasm-interpreter.h"
2+
3+
namespace wasm {
4+
5+
#ifdef WASM_INTERPRETER_DEBUG
6+
int Indenter::indentLevel = 0;
7+
8+
Indenter::Indenter(const char* entry) : entryName(entry) {
9+
++indentLevel;
10+
}
11+
Indenter::~Indenter() {
12+
print();
13+
std::cout << "exit " << entryName << '\n';
14+
--indentLevel;
15+
}
16+
void Indenter::print() {
17+
std::cout << indentLevel << ':';
18+
for (int i = 0; i <= indentLevel; ++i) {
19+
std::cout << ' ';
20+
}
21+
}
22+
#endif // WASM_INTERPRETER_DEBUG
23+
24+
} // namespace wasm

Diff for: src/wasm-interpreter.h

+43-8
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
#include "support/bits.h"
3030
#include "wasm.h"
31+
#include "wasm-traversal.h"
3132

3233
#ifdef WASM_INTERPRETER_DEBUG
3334
#include "wasm-printing.h"
@@ -75,11 +76,33 @@ typedef std::vector<Literal> LiteralList;
7576

7677
// Debugging helpers
7778
#ifdef WASM_INTERPRETER_DEBUG
78-
#define NOTE_ENTER(x) { std::cout << "visit " << x << " : " << curr << "\n"; }
79-
#define NOTE_ENTER_(x) { std::cout << "visit " << x << "\n"; }
80-
#define NOTE_NAME(p0) { std::cout << "name " << '(' << Name(p0) << ")\n"; }
81-
#define NOTE_EVAL1(p0) { std::cout << "eval " << '(' << p0 << ")\n"; }
82-
#define NOTE_EVAL2(p0, p1) { std::cout << "eval " << '(' << p0 << ", " << p1 << ")\n"; }
79+
class Indenter {
80+
static int indentLevel;
81+
82+
const char* entryName;
83+
84+
public:
85+
Indenter(const char* entry);
86+
~Indenter();
87+
88+
static void print();
89+
};
90+
91+
#define NOTE_ENTER(x) Indenter _int_blah(x); { \
92+
Indenter::print(); \
93+
std::cout << "visit " << x << " : " << curr << "\n"; }
94+
#define NOTE_ENTER_(x) Indenter _int_blah(x); { \
95+
Indenter::print(); \
96+
std::cout << "visit " << x << "\n"; }
97+
#define NOTE_NAME(p0) { \
98+
Indenter::print(); \
99+
std::cout << "name " << '(' << Name(p0) << ")\n"; }
100+
#define NOTE_EVAL1(p0) { \
101+
Indenter::print(); \
102+
std::cout << "eval " #p0 " (" << p0 << ")\n"; }
103+
#define NOTE_EVAL2(p0, p1) { \
104+
Indenter::print(); \
105+
std::cout << "eval " #p0 " (" << p0 << "), " #p1 " (" << p1 << ")\n"; }
83106
#else // WASM_INTERPRETER_DEBUG
84107
#define NOTE_ENTER(x)
85108
#define NOTE_ENTER_(x)
@@ -725,15 +748,23 @@ class ModuleInstance {
725748
NOTE_ENTER("Load");
726749
Flow flow = visit(curr->ptr);
727750
if (flow.breaking()) return flow;
728-
return instance.externalInterface->load(curr, instance.getFinalAddress(curr, flow.value));
751+
NOTE_EVAL1(flow);
752+
auto addr = instance.getFinalAddress(curr, flow.value);
753+
auto ret = instance.externalInterface->load(curr, addr);
754+
NOTE_EVAL1(addr);
755+
NOTE_EVAL1(ret);
756+
return ret;
729757
}
730758
Flow visitStore(Store *curr) {
731759
NOTE_ENTER("Store");
732760
Flow ptr = visit(curr->ptr);
733761
if (ptr.breaking()) return ptr;
734762
Flow value = visit(curr->value);
735763
if (value.breaking()) return value;
736-
instance.externalInterface->store(curr, instance.getFinalAddress(curr, ptr.value), value.value);
764+
auto addr = instance.getFinalAddress(curr, ptr.value);
765+
NOTE_EVAL1(addr);
766+
NOTE_EVAL1(value);
767+
instance.externalInterface->store(curr, addr, value.value);
737768
return Flow();
738769
}
739770

@@ -781,7 +812,11 @@ class ModuleInstance {
781812
FunctionScope scope(function, arguments);
782813

783814
#ifdef WASM_INTERPRETER_DEBUG
784-
std::cout << "entering " << function->name << '\n';
815+
std::cout << "entering " << function->name
816+
<< "\n with arguments:\n";
817+
for (unsigned i = 0; i < arguments.size(); ++i) {
818+
std::cout << " $" << i << ": " << arguments[i] << '\n';
819+
}
785820
#endif
786821

787822
Flow flow = RuntimeExpressionRunner(*this, scope).visit(function->body);

0 commit comments

Comments
 (0)