Skip to content

Commit d18b5f7

Browse files
symbolic runtime for explore tree
1 parent 61215b6 commit d18b5f7

File tree

2 files changed

+90
-4
lines changed

2 files changed

+90
-4
lines changed

headers/wasm/symbolic_rt.hpp

Lines changed: 82 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
#define WASM_SYMBOLIC_RT_HPP
33

44
#include "concrete_rt.hpp"
5+
#include <cassert>
6+
#include <iterator>
7+
#include <memory>
58
#include <variant>
69

710
class SymVal {
@@ -75,18 +78,93 @@ static SymVal Concrete(Num num) {
7578
return SymVal();
7679
}
7780

81+
struct Node;
82+
83+
struct NodeBox {
84+
explicit NodeBox();
85+
std::unique_ptr<Node> node;
86+
NodeBox *parent;
87+
};
88+
89+
struct Node {
90+
virtual ~Node(){};
91+
virtual std::string to_string() = 0;
92+
};
93+
94+
struct IfElseNode : Node {
95+
SymVal cond;
96+
std::unique_ptr<NodeBox> true_branch;
97+
std::unique_ptr<NodeBox> false_branch;
98+
99+
IfElseNode(SymVal cond)
100+
: cond(cond), true_branch(std::make_unique<NodeBox>()),
101+
false_branch(std::make_unique<NodeBox>()) {}
102+
103+
std::string to_string() override {
104+
std::string result = "IfElseNode {\n";
105+
result += " true_branch: ";
106+
if (true_branch) {
107+
result += true_branch->node->to_string();
108+
} else {
109+
result += "nullptr";
110+
}
111+
result += "\n";
112+
113+
result += " false_branch: ";
114+
if (false_branch) {
115+
result += false_branch->node->to_string();
116+
} else {
117+
result += "nullptr";
118+
}
119+
result += "\n";
120+
result += "}";
121+
return result;
122+
}
123+
};
124+
125+
struct UnExploredNode : Node {
126+
UnExploredNode() {}
127+
std::string to_string() override { return "UnexploredNode"; }
128+
};
129+
130+
static UnExploredNode unexplored;
131+
132+
inline NodeBox::NodeBox()
133+
: node(std::make_unique<
134+
UnExploredNode>() /* TODO: avoid allocation of unexplored node */) {}
135+
78136
class ExploreTree_t {
79137
public:
80-
std::monostate fillIfElseNode(SymVal s) {
81-
// fill the current node with the branch condition s
82-
// parameter branch is redundant, to hint which branch we've entered
83-
// Not implemented yet
138+
explicit ExploreTree_t()
139+
: root(std::make_unique<NodeBox>()), cursor(root.get()) {}
140+
std::monostate fillIfElseNode(SymVal cond) {
141+
// fill the current node with an ifelse branch node
142+
cursor->node = std::make_unique<IfElseNode>(cond);
84143
return std::monostate();
85144
}
86145

87146
std::monostate moveCursor(bool branch) {
147+
assert(cursor != nullptr);
148+
auto if_else_node = dynamic_cast<IfElseNode *>(cursor->node.get());
149+
assert(
150+
if_else_node != nullptr &&
151+
"Can't move cursor when the branch node is not initialized correctly!");
152+
if (branch) {
153+
cursor = if_else_node->true_branch.get();
154+
} else {
155+
cursor = if_else_node->false_branch.get();
156+
}
88157
return std::monostate();
89158
}
159+
160+
std::monostate print() {
161+
std::cout << root->node->to_string() << std::endl;
162+
return std::monostate();
163+
}
164+
165+
private:
166+
std::unique_ptr<NodeBox> root;
167+
NodeBox *cursor;
90168
};
91169

92170
static ExploreTree_t ExploreTree;

src/main/scala/wasm/StagedConcolicMiniWasm.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import gensym.wasm.miniwasm.{ModuleInstance}
1616
import gensym.wasm.symbolic.{SymVal}
1717
import gensym.lmsx.{SAIDriver, StringOps, SAIOps, SAICodeGenBase, CppSAIDriver, CppSAICodeGenBase}
1818
import gensym.wasm.symbolic.Concrete
19+
import gensym.wasm.symbolic.ExploreTree
1920

2021
@virtualize
2122
trait StagedWasmEvaluator extends SAIOps {
@@ -405,6 +406,7 @@ trait StagedWasmEvaluator extends SAIOps {
405406
info("Exiting the program...")
406407
if (printRes) {
407408
Stack.print()
409+
ExploreTree.print()
408410
}
409411
"no-op".reflectCtrlWith[Unit]()
410412
}
@@ -590,6 +592,10 @@ trait StagedWasmEvaluator extends SAIOps {
590592
def moveCursor(branch: Boolean): Rep[Unit] = {
591593
"tree-move-cursor".reflectCtrlWith[Unit](branch)
592594
}
595+
596+
def print(): Rep[Unit] = {
597+
"tree-print".reflectCtrlWith[Unit]()
598+
}
593599
}
594600

595601
// runtime Num type
@@ -908,6 +914,8 @@ trait StagedWasmCppGen extends CGenBase with CppSAICodeGenBase {
908914
emit("ExploreTree.fillIfElseNode("); shallow(s); emit(")")
909915
case Node(_, "tree-move-cursor", List(b), _) =>
910916
emit("ExploreTree.moveCursor("); shallow(b); emit(")")
917+
case Node(_, "tree-print", List(), _) =>
918+
emit("ExploreTree.print()")
911919
case Node(_, "sym-not", List(s), _) =>
912920
shallow(s); emit(".negate()")
913921
case Node(_, "dummy", _, _) => emit("std::monostate()")

0 commit comments

Comments
 (0)