From 6f0609ae19a3124010eaceba17de02c026cbb9df Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Mon, 27 Jun 2016 16:05:28 -0700 Subject: simplify wasm-builder: it has an allocator, and should never need to access a module (#605) --- src/wasm-builder.h | 47 ++++++++++++++++++++++++----------------------- 1 file changed, 24 insertions(+), 23 deletions(-) (limited to 'src') diff --git a/src/wasm-builder.h b/src/wasm-builder.h index 501124999..e73b707b9 100644 --- a/src/wasm-builder.h +++ b/src/wasm-builder.h @@ -33,10 +33,11 @@ struct NameType { // General AST node builder class Builder { - Module& wasm; + MixedArena& allocator; public: - Builder(Module& wasm) : wasm(wasm) {} + Builder(MixedArena& allocator) : allocator(allocator) {} + Builder(Module& wasm) : allocator(wasm.allocator) {} // make* functions, create nodes @@ -65,10 +66,10 @@ public: } Nop* makeNop() { - return wasm.allocator.alloc(); + return allocator.alloc(); } Block* makeBlock(Expression* first = nullptr) { - auto* ret = wasm.allocator.alloc(); + auto* ret = allocator.alloc(); if (first) { ret->list.push_back(first); ret->finalize(); @@ -76,46 +77,46 @@ public: return ret; } If* makeIf(Expression* condition, Expression* ifTrue, Expression* ifFalse=nullptr) { - auto* ret = wasm.allocator.alloc(); + auto* ret = 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 = wasm.allocator.alloc(); + auto* ret = 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 = wasm.allocator.alloc(); + auto* ret = allocator.alloc(); ret->name = name; ret->value = value; ret->condition = condition; ret->finalize(); return ret; } template Switch* makeSwitch(T& list, Name default_, Expression* condition, Expression* value = nullptr) { - auto* ret = wasm.allocator.alloc(); + auto* ret = allocator.alloc(); ret->targets.set(list); ret->default_ = default_; ret->value = value; ret->condition = condition; return ret; } Call* makeCall(Name target, const std::vector& args, WasmType type) { - auto* call = wasm.allocator.alloc(); + auto* call = allocator.alloc(); call->type = type; // not all functions may exist yet, so type must be provided call->target = target; call->operands.set(args); return call; } CallImport* makeCallImport(Name target, const std::vector& args, WasmType type) { - auto* call = wasm.allocator.alloc(); + auto* call = allocator.alloc(); call->type = type; // similar to makeCall, for consistency call->target = target; call->operands.set(args); return call; } CallIndirect* makeCallIndirect(FunctionType* type, Expression* target, const std::vector& args) { - auto* call = wasm.allocator.alloc(); + auto* call = allocator.alloc(); call->fullType = type->name; call->type = type->result; call->target = target; @@ -123,7 +124,7 @@ public: return call; } CallIndirect* makeCallIndirect(Name fullType, Expression* target, const std::vector& args, WasmType type) { - auto* call = wasm.allocator.alloc(); + auto* call = allocator.alloc(); call->fullType = fullType; call->type = type; call->target = target; @@ -132,69 +133,69 @@ public: } // FunctionType GetLocal* makeGetLocal(Index index, WasmType type) { - auto* ret = wasm.allocator.alloc(); + auto* ret = allocator.alloc(); ret->index = index; ret->type = type; return ret; } SetLocal* makeSetLocal(Index index, Expression* value) { - auto* ret = wasm.allocator.alloc(); + auto* ret = allocator.alloc(); ret->index = index; ret->value = value; ret->type = value->type; return ret; } Load* makeLoad(unsigned bytes, bool signed_, uint32_t offset, unsigned align, Expression *ptr, WasmType type) { - auto* ret = wasm.allocator.alloc(); + auto* ret = allocator.alloc(); ret->bytes = bytes; ret->signed_ = signed_; ret->offset = offset; ret->align = align; ret->ptr = ptr; ret->type = type; return ret; } Store* makeStore(unsigned bytes, uint32_t offset, unsigned align, Expression *ptr, Expression *value) { - auto* ret = wasm.allocator.alloc(); + auto* ret = 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) { assert(isConcreteWasmType(value.type)); - auto* ret = wasm.allocator.alloc(); + auto* ret = allocator.alloc(); ret->value = value; ret->type = value.type; return ret; } Unary* makeUnary(UnaryOp op, Expression *value) { - auto* ret = wasm.allocator.alloc(); + auto* ret = allocator.alloc(); ret->op = op; ret->value = value; ret->finalize(); return ret; } Binary* makeBinary(BinaryOp op, Expression *left, Expression *right) { - auto* ret = wasm.allocator.alloc(); + auto* ret = allocator.alloc(); ret->op = op; ret->left = left; ret->right = right; ret->finalize(); return ret; } Select* makeSelect(Expression* condition, Expression *ifTrue, Expression *ifFalse) { - auto* ret = wasm.allocator.alloc(); ret->condition = condition; ret->ifTrue = ifTrue; ret->ifFalse = ifFalse; ret->finalize(); return ret; } Return* makeReturn(Expression *value) { - auto* ret = wasm.allocator.alloc(); + auto* ret = allocator.alloc(); ret->value = value; return ret; } Host* makeHost(HostOp op, Name nameOperand, std::vector&& operands) { - auto* ret = wasm.allocator.alloc(); + auto* ret = allocator.alloc(); ret->op = op; ret->nameOperand = nameOperand; ret->operands.set(operands); return ret; } Unreachable* makeUnreachable() { - return wasm.allocator.alloc(); + return allocator.alloc(); } // Additional utility functions for building on top of nodes -- cgit v1.2.3