From b08aa103597b00a2b4a54d81cde6454f3082b4d5 Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Mon, 18 Apr 2016 11:47:04 -0700 Subject: index locals, so that get_local and set_local have just an index, and local names are kept on the Function object (#354) --- src/wasm-builder.h | 75 ++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 67 insertions(+), 8 deletions(-) (limited to 'src/wasm-builder.h') diff --git a/src/wasm-builder.h b/src/wasm-builder.h index 362f1a871..0d6ba30cd 100644 --- a/src/wasm-builder.h +++ b/src/wasm-builder.h @@ -21,21 +21,46 @@ namespace wasm { +// Useful data structures + +struct NameType { + Name name; + WasmType type; + NameType() : name(nullptr), type(none) {} + NameType(Name name, WasmType type) : name(name), type(type) {} +}; + +// General AST node builder + class Builder { MixedArena &allocator; public: Builder(AllocatingModule& wasm) : allocator(wasm.allocator) {} + // make* functions, create nodes + Function* makeFunction(Name name, std::vector&& params, WasmType resultType, - std::vector&& vars) { + std::vector&& vars, + Expression* body = nullptr) { auto* func = allocator.alloc(); func->name = name; - func->params = params; func->result = resultType; - func->vars = vars; + func->body = body; + + for (auto& param : params) { + func->params.push_back(param.type); + func->localIndices[param.name] = func->localNames.size(); + func->localNames.push_back(param.name); + } + for (auto& var : vars) { + func->vars.push_back(var.type); + func->localIndices[var.name] = func->localNames.size(); + func->localNames.push_back(var.name); + } + return func; } @@ -63,15 +88,15 @@ public: return call; } // FunctionType - GetLocal* makeGetLocal(Name name, WasmType type) { + GetLocal* makeGetLocal(Index index, WasmType type) { auto* ret = allocator.alloc(); - ret->name = name; + ret->index = index; ret->type = type; return ret; } - SetLocal* makeSetLocal(Name name, Expression* value) { + SetLocal* makeSetLocal(Index index, Expression* value) { auto* ret = allocator.alloc(); - ret->name = name; + ret->index = index; ret->value = value; ret->type = value->type; return ret; @@ -130,9 +155,43 @@ public: ret->value = value; return ret; } - // Host + Host* makeHost(HostOp op, Name nameOperand, ExpressionList&& operands) { + auto* ret = allocator.alloc(); + ret->op = op; + ret->nameOperand = nameOperand; + ret->operands = operands; + return ret; + } // Unreachable + // Additional utility functions for building on top of nodes + + static Index addParam(Function* func, Name name, WasmType type) { + // only ok to add a param if no vars, otherwise indices are invalidated + assert(func->localIndices.size() == func->params.size()); + func->params.push_back(type); + Index index = func->localNames.size(); + func->localIndices[name] = index; + func->localNames.push_back(name); + return index; + } + + static Index addVar(Function* func, Name name, WasmType type) { + // always ok to add a var, it does not affect other indices + assert(func->localIndices.size() == func->params.size() + func->vars.size()); + func->vars.emplace_back(type); + Index index = func->localNames.size(); + func->localIndices[name] = index; + func->localNames.push_back(name); + return index; + } + + static void clearLocals(Function* func) { + func->params.clear(); + func->vars.clear(); + func->localNames.clear(); + func->localIndices.clear(); + } }; } // namespace wasm -- cgit v1.2.3