Commit c4588df1 authored by titzer's avatar titzer Committed by Commit bot

[wasm] Cleanup AST decoder. Remove Tree and TreeResult.

R=ahaas@chromium.org
BUG=

Review-Url: https://codereview.chromium.org/2110053002
Cr-Commit-Position: refs/heads/master@{#37387}
parent e0c87cfc
...@@ -29,7 +29,7 @@ namespace wasm { ...@@ -29,7 +29,7 @@ namespace wasm {
struct ModuleEnv; struct ModuleEnv;
struct WasmFunction; struct WasmFunction;
class ErrorThrower; class ErrorThrower;
struct Tree; struct DecodeStruct;
// Expose {Node} and {Graph} opaquely as {wasm::TFNode} and {wasm::TFGraph}. // Expose {Node} and {Graph} opaquely as {wasm::TFNode} and {wasm::TFGraph}.
typedef compiler::Node TFNode; typedef compiler::Node TFNode;
...@@ -72,7 +72,7 @@ class WasmCompilationUnit final { ...@@ -72,7 +72,7 @@ class WasmCompilationUnit final {
CompilationInfo info_; CompilationInfo info_;
base::SmartPointer<CompilationJob> job_; base::SmartPointer<CompilationJob> job_;
uint32_t index_; uint32_t index_;
wasm::Result<wasm::Tree*> graph_construction_result_; wasm::Result<wasm::DecodeStruct*> graph_construction_result_;
bool ok_; bool ok_;
}; };
......
...@@ -31,17 +31,6 @@ namespace wasm { ...@@ -31,17 +31,6 @@ namespace wasm {
#define TRACE(...) #define TRACE(...)
#endif #endif
// The root of a decoded tree.
struct Tree {
LocalType type; // tree type.
uint32_t count; // number of children.
const byte* pc; // start of the syntax tree.
TFNode* node; // node in the TurboFan graph.
Tree* children[1]; // pointers to children.
WasmOpcode opcode() const { return static_cast<WasmOpcode>(*pc); }
};
// An SsaEnv environment carries the current local variable renaming // An SsaEnv environment carries the current local variable renaming
// as well as the current effect and control dependency in the TF graph. // as well as the current effect and control dependency in the TF graph.
// It maintains a control state that tracks whether the environment // It maintains a control state that tracks whether the environment
...@@ -385,11 +374,11 @@ class WasmDecoder : public Decoder { ...@@ -385,11 +374,11 @@ class WasmDecoder : public Decoder {
} }
}; };
// A shift-reduce-parser strategy for decoding Wasm code that uses an explicit // The full WASM decoder for bytecode. Both verifies bytecode and generates
// shift-reduce strategy with multiple internal stacks. // a TurboFan IR graph.
class SR_WasmDecoder : public WasmDecoder { class WasmFullDecoder : public WasmDecoder {
public: public:
SR_WasmDecoder(Zone* zone, TFBuilder* builder, const FunctionBody& body) WasmFullDecoder(Zone* zone, TFBuilder* builder, const FunctionBody& body)
: WasmDecoder(body.module, body.sig, body.start, body.end), : WasmDecoder(body.module, body.sig, body.start, body.end),
zone_(zone), zone_(zone),
builder_(builder), builder_(builder),
...@@ -1471,39 +1460,24 @@ bool DecodeLocalDecls(AstLocalDecls& decls, const byte* start, ...@@ -1471,39 +1460,24 @@ bool DecodeLocalDecls(AstLocalDecls& decls, const byte* start,
base::AccountingAllocator allocator; base::AccountingAllocator allocator;
Zone tmp(&allocator); Zone tmp(&allocator);
FunctionBody body = {nullptr, nullptr, nullptr, start, end}; FunctionBody body = {nullptr, nullptr, nullptr, start, end};
SR_WasmDecoder decoder(&tmp, nullptr, body); WasmFullDecoder decoder(&tmp, nullptr, body);
return decoder.DecodeLocalDecls(decls); return decoder.DecodeLocalDecls(decls);
} }
TreeResult VerifyWasmCode(base::AccountingAllocator* allocator, DecodeResult VerifyWasmCode(base::AccountingAllocator* allocator,
FunctionBody& body) { FunctionBody& body) {
Zone zone(allocator); Zone zone(allocator);
SR_WasmDecoder decoder(&zone, nullptr, body); WasmFullDecoder decoder(&zone, nullptr, body);
decoder.Decode(); decoder.Decode();
return decoder.toResult<Tree*>(nullptr); return decoder.toResult<DecodeStruct*>(nullptr);
} }
TreeResult BuildTFGraph(base::AccountingAllocator* allocator, DecodeResult BuildTFGraph(base::AccountingAllocator* allocator,
TFBuilder* builder, FunctionBody& body) { TFBuilder* builder, FunctionBody& body) {
Zone zone(allocator); Zone zone(allocator);
SR_WasmDecoder decoder(&zone, builder, body); WasmFullDecoder decoder(&zone, builder, body);
decoder.Decode(); decoder.Decode();
return decoder.toResult<Tree*>(nullptr); return decoder.toResult<DecodeStruct*>(nullptr);
}
std::ostream& operator<<(std::ostream& os, const Tree& tree) {
if (tree.pc == nullptr) {
os << "null";
return os;
}
PrintF("%s", WasmOpcodes::OpcodeName(tree.opcode()));
if (tree.count > 0) os << "(";
for (uint32_t i = 0; i < tree.count; ++i) {
if (i > 0) os << ", ";
os << *tree.children[i];
}
if (tree.count > 0) os << ")";
return os;
} }
unsigned OpcodeLength(const byte* pc, const byte* end) { unsigned OpcodeLength(const byte* pc, const byte* end) {
...@@ -1526,7 +1500,7 @@ bool PrintAst(base::AccountingAllocator* allocator, const FunctionBody& body, ...@@ -1526,7 +1500,7 @@ bool PrintAst(base::AccountingAllocator* allocator, const FunctionBody& body,
std::ostream& os, std::ostream& os,
std::vector<std::tuple<uint32_t, int, int>>* offset_table) { std::vector<std::tuple<uint32_t, int, int>>* offset_table) {
Zone zone(allocator); Zone zone(allocator);
SR_WasmDecoder decoder(&zone, nullptr, body); WasmFullDecoder decoder(&zone, nullptr, body);
int line_nr = 0; int line_nr = 0;
// Print the function signature. // Print the function signature.
...@@ -1655,7 +1629,7 @@ bool PrintAst(base::AccountingAllocator* allocator, const FunctionBody& body, ...@@ -1655,7 +1629,7 @@ bool PrintAst(base::AccountingAllocator* allocator, const FunctionBody& body,
BitVector* AnalyzeLoopAssignmentForTesting(Zone* zone, size_t num_locals, BitVector* AnalyzeLoopAssignmentForTesting(Zone* zone, size_t num_locals,
const byte* start, const byte* end) { const byte* start, const byte* end) {
FunctionBody body = {nullptr, nullptr, nullptr, start, end}; FunctionBody body = {nullptr, nullptr, nullptr, start, end};
SR_WasmDecoder decoder(zone, nullptr, body); WasmFullDecoder decoder(zone, nullptr, body);
return decoder.AnalyzeLoopAssignmentForTesting(start, num_locals); return decoder.AnalyzeLoopAssignmentForTesting(start, num_locals);
} }
......
...@@ -222,15 +222,18 @@ static inline FunctionBody FunctionBodyForTesting(const byte* start, ...@@ -222,15 +222,18 @@ static inline FunctionBody FunctionBodyForTesting(const byte* start,
return {nullptr, nullptr, start, start, end}; return {nullptr, nullptr, start, start, end};
} }
struct Tree; struct DecodeStruct {
typedef Result<Tree*> TreeResult; int unused;
};
std::ostream& operator<<(std::ostream& os, const Tree& tree); typedef Result<DecodeStruct*> DecodeResult;
inline std::ostream& operator<<(std::ostream& os, const DecodeStruct& tree) {
return os;
}
TreeResult VerifyWasmCode(base::AccountingAllocator* allocator, DecodeResult VerifyWasmCode(base::AccountingAllocator* allocator,
FunctionBody& body); FunctionBody& body);
TreeResult BuildTFGraph(base::AccountingAllocator* allocator, DecodeResult BuildTFGraph(base::AccountingAllocator* allocator,
TFBuilder* builder, FunctionBody& body); TFBuilder* builder, FunctionBody& body);
bool PrintAst(base::AccountingAllocator* allocator, const FunctionBody& body, bool PrintAst(base::AccountingAllocator* allocator, const FunctionBody& body,
std::ostream& os, std::ostream& os,
std::vector<std::tuple<uint32_t, int, int>>* offset_table); std::vector<std::tuple<uint32_t, int, int>>* offset_table);
...@@ -238,17 +241,17 @@ bool PrintAst(base::AccountingAllocator* allocator, const FunctionBody& body, ...@@ -238,17 +241,17 @@ bool PrintAst(base::AccountingAllocator* allocator, const FunctionBody& body,
// A simplified form of AST printing, e.g. from a debugger. // A simplified form of AST printing, e.g. from a debugger.
void PrintAstForDebugging(const byte* start, const byte* end); void PrintAstForDebugging(const byte* start, const byte* end);
inline TreeResult VerifyWasmCode(base::AccountingAllocator* allocator, inline DecodeResult VerifyWasmCode(base::AccountingAllocator* allocator,
ModuleEnv* module, FunctionSig* sig, ModuleEnv* module, FunctionSig* sig,
const byte* start, const byte* end) { const byte* start, const byte* end) {
FunctionBody body = {module, sig, nullptr, start, end}; FunctionBody body = {module, sig, nullptr, start, end};
return VerifyWasmCode(allocator, body); return VerifyWasmCode(allocator, body);
} }
inline TreeResult BuildTFGraph(base::AccountingAllocator* allocator, inline DecodeResult BuildTFGraph(base::AccountingAllocator* allocator,
TFBuilder* builder, ModuleEnv* module, TFBuilder* builder, ModuleEnv* module,
FunctionSig* sig, const byte* start, FunctionSig* sig, const byte* start,
const byte* end) { const byte* end) {
FunctionBody body = {module, sig, nullptr, start, end}; FunctionBody body = {module, sig, nullptr, start, end};
return BuildTFGraph(allocator, builder, body); return BuildTFGraph(allocator, builder, body);
} }
......
...@@ -546,7 +546,7 @@ class ModuleDecoder : public Decoder { ...@@ -546,7 +546,7 @@ class ModuleDecoder : public Decoder {
FunctionBody body = {menv, function->sig, start_, FunctionBody body = {menv, function->sig, start_,
start_ + function->code_start_offset, start_ + function->code_start_offset,
start_ + function->code_end_offset}; start_ + function->code_end_offset};
TreeResult result = VerifyWasmCode(module_zone->allocator(), body); DecodeResult result = VerifyWasmCode(module_zone->allocator(), body);
if (result.failed()) { if (result.failed()) {
// Wrap the error message from the function decoder. // Wrap the error message from the function decoder.
std::ostringstream str; std::ostringstream str;
......
...@@ -269,7 +269,7 @@ inline void TestBuildingGraph(Zone* zone, JSGraph* jsgraph, ModuleEnv* module, ...@@ -269,7 +269,7 @@ inline void TestBuildingGraph(Zone* zone, JSGraph* jsgraph, ModuleEnv* module,
SourcePositionTable* source_position_table, SourcePositionTable* source_position_table,
const byte* start, const byte* end) { const byte* start, const byte* end) {
compiler::WasmGraphBuilder builder(zone, jsgraph, sig, source_position_table); compiler::WasmGraphBuilder builder(zone, jsgraph, sig, source_position_table);
TreeResult result = DecodeResult result =
BuildTFGraph(zone->allocator(), &builder, module, sig, start, end); BuildTFGraph(zone->allocator(), &builder, module, sig, start, end);
if (result.failed()) { if (result.failed()) {
ptrdiff_t pc = result.error_pc - result.start; ptrdiff_t pc = result.error_pc - result.start;
......
...@@ -84,7 +84,7 @@ class AstDecoderTest : public TestWithZone { ...@@ -84,7 +84,7 @@ class AstDecoderTest : public TestWithZone {
const byte* end) { const byte* end) {
local_decls.Prepend(zone(), &start, &end); local_decls.Prepend(zone(), &start, &end);
// Verify the code. // Verify the code.
TreeResult result = DecodeResult result =
VerifyWasmCode(zone()->allocator(), module, sig, start, end); VerifyWasmCode(zone()->allocator(), module, sig, start, end);
if (result.error_code != expected) { if (result.error_code != expected) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment