From 996262dcdb88388717aab72bd8f37841aaabb24c Mon Sep 17 00:00:00 2001 From: Derek Schuff Date: Fri, 10 Jun 2016 13:08:58 -0700 Subject: s2wasm: Validate the result module (#574) Add an s2wasm option `--no-validate` to disable validation for debugging purposes. Also fix several validation errors by adding calls to `finalize()` after creating expressions, and ensuring that an import is created earlier in `Linker::getImportThunk`. --- src/wasm-builder.h | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) (limited to 'src/wasm-builder.h') diff --git a/src/wasm-builder.h b/src/wasm-builder.h index 038fdbed5..ae8d5fa4c 100644 --- a/src/wasm-builder.h +++ b/src/wasm-builder.h @@ -33,10 +33,10 @@ struct NameType { // General AST node builder class Builder { - MixedArena &allocator; + Module& wasm; public: - Builder(Module& wasm) : allocator(wasm.allocator) {} + Builder(Module& wasm) : wasm(wasm) {} // make* functions, create nodes @@ -65,10 +65,10 @@ public: } Nop* makeNop() { - return allocator.alloc(); + return wasm.allocator.alloc(); } Block* makeBlock(Expression* first = nullptr) { - auto* ret = allocator.alloc(); + auto* ret = wasm.allocator.alloc(); if (first) { ret->list.push_back(first); ret->finalize(); @@ -76,19 +76,19 @@ public: return ret; } If* makeIf(Expression* condition, Expression* ifTrue, Expression* ifFalse=nullptr) { - auto* ret = allocator.alloc(); + auto* ret = wasm.allocator.alloc(); ret->condition = condition; ret->ifTrue = ifTrue; ret->ifFalse = ifFalse; ret->finalize(); return ret; } Loop* makeLoop(Name out, Name in, Expression* body) { - auto* ret = allocator.alloc(); + auto* ret = wasm.allocator.alloc(); ret->out = out; ret->in = in; ret->body = body; ret->finalize(); return ret; } Break* makeBreak(Name name, Expression* value=nullptr, Expression* condition=nullptr) { - auto* ret = allocator.alloc(); + auto* ret = wasm.allocator.alloc(); ret->name = name; ret->value = value; ret->condition = condition; ret->finalize(); return ret; @@ -98,13 +98,14 @@ public: // Call // Also do a version which takes a sig? CallImport* makeCallImport(Name target, const std::vector& args) { - auto* call = allocator.alloc(); + auto* call = wasm.allocator.alloc(); + call->type = wasm.getImport(target)->type->result; call->target = target; call->operands.set(args); return call; } CallIndirect* makeCallIndirect(FunctionType* type, Expression* target, const std::vector& args) { - auto* call = allocator.alloc(); + auto* call = wasm.allocator.alloc(); call->fullType = type->name; call->type = type->result; call->target = target; @@ -113,13 +114,13 @@ public: } // FunctionType GetLocal* makeGetLocal(Index index, WasmType type) { - auto* ret = allocator.alloc(); + auto* ret = wasm.allocator.alloc(); ret->index = index; ret->type = type; return ret; } SetLocal* makeSetLocal(Index index, Expression* value) { - auto* ret = allocator.alloc(); + auto* ret = wasm.allocator.alloc(); ret->index = index; ret->value = value; ret->type = value->type; @@ -127,37 +128,37 @@ public: } // Load Store* makeStore(unsigned bytes, uint32_t offset, unsigned align, Expression *ptr, Expression *value) { - auto* ret = allocator.alloc(); + auto* ret = wasm.allocator.alloc(); ret->bytes = bytes; ret->offset = offset; ret->align = align; ret->ptr = ptr; ret->value = value; ret->type = value->type; return ret; } Const* makeConst(Literal value) { - auto* ret = allocator.alloc(); + auto* ret = wasm.allocator.alloc(); ret->value = value; ret->type = value.type; return ret; } Unary* makeUnary(UnaryOp op, Expression *value) { - auto* ret = allocator.alloc(); + auto* ret = wasm.allocator.alloc(); ret->op = op; ret->value = value; ret->finalize(); return ret; } Binary* makeBinary(BinaryOp op, Expression *left, Expression *right) { - auto* ret = allocator.alloc(); + auto* ret = wasm.allocator.alloc(); ret->op = op; ret->left = left; ret->right = right; ret->finalize(); return ret; } // Select Return* makeReturn(Expression *value) { - auto* ret = allocator.alloc(); + auto* ret = wasm.allocator.alloc(); ret->value = value; return ret; } Host* makeHost(HostOp op, Name nameOperand, std::vector&& operands) { - auto* ret = allocator.alloc(); + auto* ret = wasm.allocator.alloc(); ret->op = op; ret->nameOperand = nameOperand; ret->operands.set(operands); -- cgit v1.2.3