diff --git a/src/compiler/wasm-compiler.cc b/src/compiler/wasm-compiler.cc index 1f3da08579..4daa7cba1e 100644 --- a/src/compiler/wasm-compiler.cc +++ b/src/compiler/wasm-compiler.cc @@ -2409,15 +2409,6 @@ Handle CompileWasmFunction(wasm::ErrorThrower& thrower, Isolate* isolate, << wasm::WasmFunctionName(&function, module_env) << std::endl; os << std::endl; } - // Initialize the function environment for decoding. - wasm::FunctionEnv env; - env.module = module_env; - env.sig = function.sig; - env.local_i32_count = function.local_i32_count; - env.local_i64_count = function.local_i64_count; - env.local_f32_count = function.local_f32_count; - env.local_f64_count = function.local_f64_count; - env.SumLocals(); // Create a TF graph during decoding. Zone zone; @@ -2428,11 +2419,11 @@ Handle CompileWasmFunction(wasm::ErrorThrower& thrower, Isolate* isolate, InstructionSelector::SupportedMachineOperatorFlags()); JSGraph jsgraph(isolate, &graph, &common, nullptr, nullptr, &machine); WasmGraphBuilder builder(&zone, &jsgraph, function.sig); - wasm::TreeResult result = wasm::BuildTFGraph( - &builder, &env, // -- - module_env->module->module_start, // -- - module_env->module->module_start + function.code_start_offset, // -- - module_env->module->module_start + function.code_end_offset); // -- + wasm::FunctionBody body = { + module_env, function.sig, module_env->module->module_start, + module_env->module->module_start + function.code_start_offset, + module_env->module->module_start + function.code_end_offset}; + wasm::TreeResult result = wasm::BuildTFGraph(&builder, body); if (result.failed()) { if (FLAG_trace_wasm_compiler) { diff --git a/src/wasm/ast-decoder.cc b/src/wasm/ast-decoder.cc index e56991a482..78f50739dd 100644 --- a/src/wasm/ast-decoder.cc +++ b/src/wasm/ast-decoder.cc @@ -52,7 +52,6 @@ struct Production { Tree* last() const { return index > 0 ? tree->children[index - 1] : nullptr; } }; - // An SsaEnv environment carries the current local variable renaming // as well as the current effect and control dependency in the TF graph. // It maintains a control state that tracks whether the environment @@ -74,14 +73,12 @@ struct SsaEnv { } }; - // An entry in the stack of blocks during decoding. struct Block { SsaEnv* ssa_env; // SSA renaming environment. int stack_depth; // production stack depth. }; - // An entry in the stack of ifs during decoding. struct IfEnv { SsaEnv* false_env; @@ -89,27 +86,27 @@ struct IfEnv { SsaEnv** case_envs; }; - // Macros that build nodes only if there is a graph and the current SSA // environment is reachable from start. This avoids problems with malformed // TF graphs when decoding inputs that have unreachable code. #define BUILD(func, ...) (build() ? builder_->func(__VA_ARGS__) : nullptr) #define BUILD0(func) (build() ? builder_->func() : nullptr) - // Generic Wasm bytecode decoder with utilities for decoding operands, // lengths, etc. class WasmDecoder : public Decoder { public: - WasmDecoder() : Decoder(nullptr, nullptr), function_env_(nullptr) {} - WasmDecoder(FunctionEnv* env, const byte* start, const byte* end) - : Decoder(start, end), function_env_(env) {} - FunctionEnv* function_env_; - - void Reset(FunctionEnv* function_env, const byte* start, const byte* end) { - Decoder::Reset(start, end); - function_env_ = function_env; - } + WasmDecoder(ModuleEnv* module, FunctionSig* sig, const byte* start, + const byte* end) + : Decoder(start, end), + module_(module), + sig_(sig), + total_locals_(0), + local_types_(nullptr) {} + ModuleEnv* module_; + FunctionSig* sig_; + size_t total_locals_; + ZoneVector* local_types_; byte ByteOperand(const byte* pc, const char* msg = "missing 1-byte operand") { if ((pc + sizeof(byte)) >= limit_) { @@ -136,8 +133,12 @@ class WasmDecoder : public Decoder { } inline bool Validate(const byte* pc, LocalIndexOperand& operand) { - if (operand.index < function_env_->total_locals) { - operand.type = function_env_->GetLocalType(operand.index); + if (operand.index < total_locals_) { + if (local_types_) { + operand.type = local_types_->at(operand.index); + } else { + operand.type = kAstStmt; + } return true; } error(pc, pc + 1, "invalid local index"); @@ -145,7 +146,7 @@ class WasmDecoder : public Decoder { } inline bool Validate(const byte* pc, GlobalIndexOperand& operand) { - ModuleEnv* m = function_env_->module; + ModuleEnv* m = module_; if (m && m->module && operand.index < m->module->globals.size()) { operand.machine_type = m->module->globals[operand.index].type; operand.type = WasmOpcodes::LocalTypeFor(operand.machine_type); @@ -156,7 +157,7 @@ class WasmDecoder : public Decoder { } inline bool Validate(const byte* pc, FunctionIndexOperand& operand) { - ModuleEnv* m = function_env_->module; + ModuleEnv* m = module_; if (m && m->module && operand.index < m->module->functions.size()) { operand.sig = m->module->functions[operand.index].sig; return true; @@ -166,7 +167,7 @@ class WasmDecoder : public Decoder { } inline bool Validate(const byte* pc, SignatureIndexOperand& operand) { - ModuleEnv* m = function_env_->module; + ModuleEnv* m = module_; if (m && m->module && operand.index < m->module->signatures.size()) { operand.sig = m->module->signatures[operand.index]; return true; @@ -176,7 +177,7 @@ class WasmDecoder : public Decoder { } inline bool Validate(const byte* pc, ImportIndexOperand& operand) { - ModuleEnv* m = function_env_->module; + ModuleEnv* m = module_; if (m && m->module && operand.index < m->module->import_table.size()) { operand.sig = m->module->import_table[operand.index].sig; return true; @@ -250,23 +251,20 @@ class WasmDecoder : public Decoder { case kExprCallFunction: { FunctionIndexOperand operand(this, pc); return static_cast( - function_env_->module->GetFunctionSignature(operand.index) - ->parameter_count()); + module_->GetFunctionSignature(operand.index)->parameter_count()); } case kExprCallIndirect: { SignatureIndexOperand operand(this, pc); return 1 + static_cast( - function_env_->module->GetSignature(operand.index) - ->parameter_count()); + module_->GetSignature(operand.index)->parameter_count()); } case kExprCallImport: { ImportIndexOperand operand(this, pc); return static_cast( - function_env_->module->GetImportSignature(operand.index) - ->parameter_count()); + module_->GetImportSignature(operand.index)->parameter_count()); } case kExprReturn: { - return static_cast(function_env_->sig->return_count()); + return static_cast(sig_->return_count()); } case kExprBrTable: { return 1; @@ -282,9 +280,11 @@ class WasmDecoder : public Decoder { FOREACH_SIMPLE_OPCODE(DECLARE_OPCODE_CASE) FOREACH_ASMJS_COMPAT_OPCODE(DECLARE_OPCODE_CASE) #undef DECLARE_OPCODE_CASE + case kExprDeclLocals: + default: + UNREACHABLE(); + return 0; } - UNREACHABLE(); - return 0; } int OpcodeLength(const byte* pc) { @@ -353,35 +353,33 @@ class WasmDecoder : public Decoder { // A shift-reduce-parser strategy for decoding Wasm code that uses an explicit // shift-reduce strategy with multiple internal stacks. -class LR_WasmDecoder : public WasmDecoder { +class SR_WasmDecoder : public WasmDecoder { public: - LR_WasmDecoder(Zone* zone, TFBuilder* builder) - : zone_(zone), + SR_WasmDecoder(Zone* zone, TFBuilder* builder, FunctionBody& body) + : WasmDecoder(body.module, body.sig, body.start, body.end), + zone_(zone), builder_(builder), + base_(body.base), + local_type_vec_(zone), trees_(zone), stack_(zone), blocks_(zone), - ifs_(zone) {} + ifs_(zone) { + local_types_ = &local_type_vec_; + } - TreeResult Decode(FunctionEnv* function_env, const byte* base, const byte* pc, - const byte* end) { + TreeResult Decode() { base::ElapsedTimer decode_timer; if (FLAG_trace_wasm_decode_time) { decode_timer.Start(); } - trees_.clear(); - stack_.clear(); - blocks_.clear(); - ifs_.clear(); - if (end < pc) { - error(pc, "function body end < start"); + if (end_ < pc_) { + error(pc_, "function body end < start"); return result_; } - base_ = base; - Reset(function_env, pc, end); - + DecodeLocalDecls(); InitSsaEnv(); DecodeFunctionBody(); @@ -389,12 +387,12 @@ class LR_WasmDecoder : public WasmDecoder { if (ok()) { if (ssa_env_->go()) { if (stack_.size() > 0) { - error(stack_.back().pc(), end, "fell off end of code"); + error(stack_.back().pc(), end_, "fell off end of code"); } AddImplicitReturnAtEnd(); } if (trees_.size() == 0) { - if (function_env_->sig->return_count() > 0) { + if (sig_->return_count() > 0) { error(start_, "no trees created"); } } else { @@ -404,7 +402,8 @@ class LR_WasmDecoder : public WasmDecoder { if (ok()) { if (FLAG_trace_wasm_ast) { - PrintAst(function_env, pc, end); + FunctionBody body = {module_, sig_, base_, start_, end_}; + PrintAst(body); } if (FLAG_trace_wasm_decode_time) { double ms = decode_timer.Elapsed().InMillisecondsF(); @@ -420,6 +419,28 @@ class LR_WasmDecoder : public WasmDecoder { return toResult(tree); } + std::vector* DecodeLocalDeclsForTesting() { + DecodeLocalDecls(); + if (failed()) return nullptr; + auto result = new std::vector(); + result->reserve(local_type_vec_.size()); + for (size_t i = 0; i < local_type_vec_.size(); i++) { + result->push_back(local_type_vec_[i]); + } + return result; + } + + BitVector* AnalyzeLoopAssignmentForTesting(const byte* pc, + size_t num_locals) { + total_locals_ = num_locals; + local_type_vec_.reserve(num_locals); + if (num_locals > local_type_vec_.size()) { + local_type_vec_.insert(local_type_vec_.end(), + num_locals - local_type_vec_.size(), kAstI32); + } + return AnalyzeLoopAssignment(pc); + } + private: static const size_t kErrorMsgSize = 128; @@ -430,6 +451,7 @@ class LR_WasmDecoder : public WasmDecoder { SsaEnv* ssa_env_; + ZoneVector local_type_vec_; ZoneVector trees_; ZoneVector stack_; ZoneVector blocks_; @@ -438,8 +460,6 @@ class LR_WasmDecoder : public WasmDecoder { inline bool build() { return builder_ && ssa_env_->go(); } void InitSsaEnv() { - FunctionSig* sig = function_env_->sig; - int param_count = static_cast(sig->parameter_count()); TFNode* start = nullptr; SsaEnv* ssa_env = reinterpret_cast(zone_->New(sizeof(SsaEnv))); size_t size = sizeof(TFNode*) * EnvironmentCount(); @@ -447,50 +467,46 @@ class LR_WasmDecoder : public WasmDecoder { ssa_env->locals = size > 0 ? reinterpret_cast(zone_->New(size)) : nullptr; - int pos = 0; if (builder_) { - start = builder_->Start(param_count + 1); - // Initialize parameters. - for (int i = 0; i < param_count; i++) { - ssa_env->locals[pos++] = builder_->Param(i, sig->GetParam(i)); + start = builder_->Start(static_cast(sig_->parameter_count() + 1)); + // Initialize local variables. + uint32_t index = 0; + while (index < sig_->parameter_count()) { + ssa_env->locals[index] = builder_->Param(index, local_type_vec_[index]); + index++; } - // Initialize int32 locals. - if (function_env_->local_i32_count > 0) { - TFNode* zero = builder_->Int32Constant(0); - for (uint32_t i = 0; i < function_env_->local_i32_count; i++) { - ssa_env->locals[pos++] = zero; + while (index < local_type_vec_.size()) { + LocalType type = local_type_vec_[index]; + TFNode* node = DefaultValue(type); + while (index < local_type_vec_.size() && + local_type_vec_[index] == type) { + // Do a whole run of like-typed locals at a time. + ssa_env->locals[index++] = node; } } - // Initialize int64 locals. - if (function_env_->local_i64_count > 0) { - TFNode* zero = builder_->Int64Constant(0); - for (uint32_t i = 0; i < function_env_->local_i64_count; i++) { - ssa_env->locals[pos++] = zero; - } - } - // Initialize float32 locals. - if (function_env_->local_f32_count > 0) { - TFNode* zero = builder_->Float32Constant(0); - for (uint32_t i = 0; i < function_env_->local_f32_count; i++) { - ssa_env->locals[pos++] = zero; - } - } - // Initialize float64 locals. - if (function_env_->local_f64_count > 0) { - TFNode* zero = builder_->Float64Constant(0); - for (uint32_t i = 0; i < function_env_->local_f64_count; i++) { - ssa_env->locals[pos++] = zero; - } - } - DCHECK_EQ(function_env_->total_locals, pos); - DCHECK_EQ(EnvironmentCount(), pos); - builder_->set_module(function_env_->module); + builder_->set_module(module_); } ssa_env->control = start; ssa_env->effect = start; SetEnv("initial", ssa_env); } + TFNode* DefaultValue(LocalType type) { + switch (type) { + case kAstI32: + return builder_->Int32Constant(0); + case kAstI64: + return builder_->Int64Constant(0); + case kAstF32: + return builder_->Float32Constant(0); + case kAstF64: + return builder_->Float64Constant(0); + default: + UNREACHABLE(); + return nullptr; + } + } + void Leaf(LocalType type, TFNode* node = nullptr) { size_t size = sizeof(Tree); Tree* tree = reinterpret_cast(zone_->New(size)); @@ -549,6 +565,45 @@ class LR_WasmDecoder : public WasmDecoder { return bytes; } + // Decodes the locals declarations, if any, populating {local_type_vec_}. + void DecodeLocalDecls() { + DCHECK_EQ(0, local_type_vec_.size()); + // Initialize {local_type_vec} from signature. + if (sig_) { + local_type_vec_.reserve(sig_->parameter_count()); + for (size_t i = 0; i < sig_->parameter_count(); i++) { + local_type_vec_.push_back(sig_->GetParam(i)); + } + } + // Decode local declarations, if any. + int length; + uint32_t entries = consume_u32v(&length, "local decls count"); + while (entries-- > 0 && pc_ < limit_) { + uint32_t count = consume_u32v(&length, "local count"); + byte code = consume_u8("local type"); + LocalType type; + switch (code) { + case kLocalI32: + type = kAstI32; + break; + case kLocalI64: + type = kAstI64; + break; + case kLocalF32: + type = kAstF32; + break; + case kLocalF64: + type = kAstF64; + break; + default: + error(pc_ - 1, "invalid local type"); + return; + } + local_type_vec_.insert(local_type_vec_.end(), count, type); + } + total_locals_ = local_type_vec_.size(); + } + // Decodes the body of a function, producing reduced trees into {result}. void DecodeFunctionBody() { TRACE("wasm-decode %p...%p (%d bytes) %s\n", @@ -652,7 +707,7 @@ class LR_WasmDecoder : public WasmDecoder { break; } case kExprReturn: { - int count = static_cast(function_env_->sig->return_count()); + int count = static_cast(sig_->return_count()); if (count == 0) { BUILD(Return, 0, builder_->Buffer(0)); ssa_env_->Kill(); @@ -809,6 +864,7 @@ class LR_WasmDecoder : public WasmDecoder { len = 1 + operand.length; break; } + case kExprDeclLocals: default: error("Invalid opcode"); return; @@ -841,7 +897,7 @@ class LR_WasmDecoder : public WasmDecoder { } void AddImplicitReturnAtEnd() { - int retcount = static_cast(function_env_->sig->return_count()); + int retcount = static_cast(sig_->return_count()); if (retcount == 0) { BUILD0(ReturnVoid); return; @@ -860,7 +916,7 @@ class LR_WasmDecoder : public WasmDecoder { for (int index = 0; index < retcount; index++) { Tree* tree = trees_[trees_.size() - 1 - index]; if (buffer) buffer[index] = tree->node; - LocalType expected = function_env_->sig->GetReturn(index); + LocalType expected = sig_->GetReturn(index); if (tree->type != expected) { error(limit_, tree->pc, "ImplicitReturn[%d] expected type %s, found %s of type %s", index, @@ -1066,7 +1122,7 @@ class LR_WasmDecoder : public WasmDecoder { break; } case kExprReturn: { - TypeCheckLast(p, function_env_->sig->GetReturn(p->index - 1)); + TypeCheckLast(p, sig_->GetReturn(p->index - 1)); if (p->done()) { if (build()) { int count = p->tree->count; @@ -1346,8 +1402,7 @@ class LR_WasmDecoder : public WasmDecoder { TFNode* b = from->locals[i]; if (a != b) { TFNode* vals[] = {a, b}; - to->locals[i] = - builder_->Phi(function_env_->GetLocalType(i), 2, vals, merge); + to->locals[i] = builder_->Phi(local_type_vec_[i], 2, vals, merge); } } break; @@ -1382,8 +1437,8 @@ class LR_WasmDecoder : public WasmDecoder { vals[j] = tnode; } vals[count - 1] = fnode; - to->locals[i] = builder_->Phi(function_env_->GetLocalType(i), count, - vals, merge); + to->locals[i] = + builder_->Phi(local_type_vec_[i], count, vals, merge); } } break; @@ -1426,8 +1481,8 @@ class LR_WasmDecoder : public WasmDecoder { env->effect = builder_->EffectPhi(1, &env->effect, env->control); builder_->Terminate(env->effect, env->control); for (int i = EnvironmentCount() - 1; i >= 0; i--) { - env->locals[i] = builder_->Phi(function_env_->GetLocalType(i), 1, - &env->locals[i], env->control); + env->locals[i] = builder_->Phi(local_type_vec_[i], 1, &env->locals[i], + env->control); } } } @@ -1481,7 +1536,7 @@ class LR_WasmDecoder : public WasmDecoder { } int EnvironmentCount() { - if (builder_) return static_cast(function_env_->GetLocalCount()); + if (builder_) return static_cast(local_type_vec_.size()); return 0; // if we aren't building a graph, don't bother with SSA renaming. } @@ -1517,23 +1572,68 @@ class LR_WasmDecoder : public WasmDecoder { PrintProduction(depth + 1); } #endif + + BitVector* AnalyzeLoopAssignment(const byte* pc) { + if (pc >= limit_) return nullptr; + if (*pc != kExprLoop) return nullptr; + + BitVector* assigned = + new (zone_) BitVector(static_cast(total_locals_), zone_); + // Keep a stack to model the nesting of expressions. + std::vector arity_stack; + arity_stack.push_back(OpcodeArity(pc)); + pc += OpcodeLength(pc); + + // Iteratively process all AST nodes nested inside the loop. + while (pc < limit_) { + WasmOpcode opcode = static_cast(*pc); + int arity = 0; + int length = 1; + if (opcode == kExprSetLocal) { + LocalIndexOperand operand(this, pc); + if (assigned->length() > 0 && + static_cast(operand.index) < assigned->length()) { + // Unverified code might have an out-of-bounds index. + assigned->Add(operand.index); + } + arity = 1; + length = 1 + operand.length; + } else { + arity = OpcodeArity(pc); + length = OpcodeLength(pc); + } + + pc += length; + arity_stack.push_back(arity); + while (arity_stack.back() == 0) { + arity_stack.pop_back(); + if (arity_stack.empty()) return assigned; // reached end of loop + arity_stack.back()--; + } + } + return assigned; + } }; - -TreeResult VerifyWasmCode(FunctionEnv* env, const byte* base, const byte* start, - const byte* end) { +std::vector* DecodeLocalDeclsForTesting(const byte* start, + const byte* end) { Zone zone; - LR_WasmDecoder decoder(&zone, nullptr); - TreeResult result = decoder.Decode(env, base, start, end); + FunctionBody body = {nullptr, nullptr, nullptr, start, end}; + SR_WasmDecoder decoder(&zone, nullptr, body); + return decoder.DecodeLocalDeclsForTesting(); +} + +TreeResult VerifyWasmCode(FunctionBody& body) { + Zone zone; + SR_WasmDecoder decoder(&zone, nullptr, body); + TreeResult result = decoder.Decode(); return result; } - -TreeResult BuildTFGraph(TFBuilder* builder, FunctionEnv* env, const byte* base, - const byte* start, const byte* end) { +TreeResult BuildTFGraph(TFBuilder* builder, FunctionBody& body) { Zone zone; - LR_WasmDecoder decoder(&zone, builder); - TreeResult result = decoder.Decode(env, base, start, end); + SR_WasmDecoder decoder(&zone, builder, body); + TreeResult result = decoder.Decode(); return result; } @@ -1565,20 +1665,21 @@ ReadUnsignedLEB128ErrorCode ReadUnsignedLEB128Operand(const byte* pc, } int OpcodeLength(const byte* pc, const byte* end) { - WasmDecoder decoder(nullptr, pc, end); + WasmDecoder decoder(nullptr, nullptr, pc, end); return decoder.OpcodeLength(pc); } -int OpcodeArity(FunctionEnv* env, const byte* pc, const byte* end) { - WasmDecoder decoder(env, pc, end); +int OpcodeArity(ModuleEnv* module, FunctionSig* sig, const byte* pc, + const byte* end) { + WasmDecoder decoder(module, sig, pc, end); return decoder.OpcodeArity(pc); } -void PrintAst(FunctionEnv* env, const byte* start, const byte* end) { - WasmDecoder decoder(env, start, end); - const byte* pc = start; +void PrintAst(FunctionBody& body) { + WasmDecoder decoder(body.module, body.sig, body.start, body.end); + const byte* pc = body.start; std::vector arity_stack; - while (pc < end) { + while (pc < body.end) { int arity = decoder.OpcodeArity(pc); size_t length = decoder.OpcodeLength(pc); @@ -1605,65 +1706,11 @@ void PrintAst(FunctionEnv* env, const byte* start, const byte* end) { } } -// Analyzes loop bodies for static assignments to locals, which helps in -// reducing the number of phis introduced at loop headers. -class LoopAssignmentAnalyzer : public WasmDecoder { - public: - LoopAssignmentAnalyzer(Zone* zone, FunctionEnv* function_env) : zone_(zone) { - function_env_ = function_env; - } - - BitVector* Analyze(const byte* pc, const byte* limit) { - Decoder::Reset(pc, limit); - if (pc_ >= limit_) return nullptr; - if (*pc_ != kExprLoop) return nullptr; - - BitVector* assigned = - new (zone_) BitVector(function_env_->total_locals, zone_); - // Keep a stack to model the nesting of expressions. - std::vector arity_stack; - arity_stack.push_back(OpcodeArity(pc_)); - pc_ += OpcodeLength(pc_); - - // Iteratively process all AST nodes nested inside the loop. - while (pc_ < limit_) { - WasmOpcode opcode = static_cast(*pc_); - int arity = 0; - int length = 1; - if (opcode == kExprSetLocal) { - LocalIndexOperand operand(this, pc_); - if (assigned->length() > 0 && - static_cast(operand.index) < assigned->length()) { - // Unverified code might have an out-of-bounds index. - assigned->Add(operand.index); - } - arity = 1; - length = 1 + operand.length; - } else { - arity = OpcodeArity(pc_); - length = OpcodeLength(pc_); - } - - pc_ += length; - arity_stack.push_back(arity); - while (arity_stack.back() == 0) { - arity_stack.pop_back(); - if (arity_stack.empty()) return assigned; // reached end of loop - arity_stack.back()--; - } - } - return assigned; - } - - private: - Zone* zone_; -}; - - -BitVector* AnalyzeLoopAssignmentForTesting(Zone* zone, FunctionEnv* env, +BitVector* AnalyzeLoopAssignmentForTesting(Zone* zone, size_t num_locals, const byte* start, const byte* end) { - LoopAssignmentAnalyzer analyzer(zone, env); - return analyzer.Analyze(start, end); + FunctionBody body = {nullptr, nullptr, nullptr, start, end}; + SR_WasmDecoder decoder(zone, nullptr, body); + return decoder.AnalyzeLoopAssignmentForTesting(start, num_locals); } } // namespace wasm diff --git a/src/wasm/ast-decoder.h b/src/wasm/ast-decoder.h index 4693af1ca0..386f6f52b2 100644 --- a/src/wasm/ast-decoder.h +++ b/src/wasm/ast-decoder.h @@ -183,61 +183,13 @@ struct MemoryAccessOperand { typedef compiler::WasmGraphBuilder TFBuilder; struct ModuleEnv; // forward declaration of module interface. -// Interface the function environment during decoding, include the signature -// and number of locals. -struct FunctionEnv { - ModuleEnv* module; // module environment - FunctionSig* sig; // signature of this function - uint32_t local_i32_count; // number of int32 locals - uint32_t local_i64_count; // number of int64 locals - uint32_t local_f32_count; // number of float32 locals - uint32_t local_f64_count; // number of float64 locals - uint32_t total_locals; // sum of parameters and all locals - - uint32_t GetLocalCount() { return total_locals; } - LocalType GetLocalType(uint32_t index) { - if (index < static_cast(sig->parameter_count())) { - return sig->GetParam(index); - } - index -= static_cast(sig->parameter_count()); - if (index < local_i32_count) return kAstI32; - index -= local_i32_count; - if (index < local_i64_count) return kAstI64; - index -= local_i64_count; - if (index < local_f32_count) return kAstF32; - index -= local_f32_count; - if (index < local_f64_count) return kAstF64; - return kAstStmt; - } - - void AddLocals(LocalType type, uint32_t count) { - switch (type) { - case kAstI32: - local_i32_count += count; - break; - case kAstI64: - local_i64_count += count; - break; - case kAstF32: - local_f32_count += count; - break; - case kAstF64: - local_f64_count += count; - break; - default: - UNREACHABLE(); - } - total_locals += count; - DCHECK_EQ(total_locals, - (sig->parameter_count() + local_i32_count + local_i64_count + - local_f32_count + local_f64_count)); - } - - void SumLocals() { - total_locals = static_cast(sig->parameter_count()) + - local_i32_count + local_i64_count + local_f32_count + - local_f64_count; - } +// All of the various data structures necessary to decode a function body. +struct FunctionBody { + ModuleEnv* module; // module environment + FunctionSig* sig; // function signature + const byte* base; // base of the module bytes, for error reporting + const byte* start; // start of the function body + const byte* end; // end of the function body }; struct Tree; @@ -245,21 +197,21 @@ typedef Result TreeResult; std::ostream& operator<<(std::ostream& os, const Tree& tree); -TreeResult VerifyWasmCode(FunctionEnv* env, const byte* base, const byte* start, - const byte* end); -TreeResult BuildTFGraph(TFBuilder* builder, FunctionEnv* env, const byte* base, - const byte* start, const byte* end); +TreeResult VerifyWasmCode(FunctionBody& body); +TreeResult BuildTFGraph(TFBuilder* builder, FunctionBody& body); +void PrintAst(FunctionBody& body); -void PrintAst(FunctionEnv* env, const byte* start, const byte* end); - -inline TreeResult VerifyWasmCode(FunctionEnv* env, const byte* start, - const byte* end) { - return VerifyWasmCode(env, nullptr, start, end); +inline TreeResult VerifyWasmCode(ModuleEnv* module, FunctionSig* sig, + const byte* start, const byte* end) { + FunctionBody body = {module, sig, nullptr, start, end}; + return VerifyWasmCode(body); } -inline TreeResult BuildTFGraph(TFBuilder* builder, FunctionEnv* env, - const byte* start, const byte* end) { - return BuildTFGraph(builder, env, nullptr, start, end); +inline TreeResult BuildTFGraph(TFBuilder* builder, ModuleEnv* module, + FunctionSig* sig, const byte* start, + const byte* end) { + FunctionBody body = {module, sig, nullptr, start, end}; + return BuildTFGraph(builder, body); } enum ReadUnsignedLEB128ErrorCode { kNoError, kInvalidLEB128, kMissingLEB128 }; @@ -267,14 +219,17 @@ enum ReadUnsignedLEB128ErrorCode { kNoError, kInvalidLEB128, kMissingLEB128 }; ReadUnsignedLEB128ErrorCode ReadUnsignedLEB128Operand(const byte*, const byte*, int*, uint32_t*); -BitVector* AnalyzeLoopAssignmentForTesting(Zone* zone, FunctionEnv* env, +std::vector* DecodeLocalDeclsForTesting(const byte* start, + const byte* end); +BitVector* AnalyzeLoopAssignmentForTesting(Zone* zone, size_t num_locals, const byte* start, const byte* end); // Computes the length of the opcode at the given address. int OpcodeLength(const byte* pc, const byte* end); // Computes the arity (number of sub-nodes) of the opcode at the given address. -int OpcodeArity(FunctionEnv* env, const byte* pc, const byte* end); +int OpcodeArity(ModuleEnv* module, FunctionSig* sig, const byte* pc, + const byte* end); } // namespace wasm } // namespace internal } // namespace v8 diff --git a/src/wasm/decoder.h b/src/wasm/decoder.h index f18e802f7f..0ca4e47f75 100644 --- a/src/wasm/decoder.h +++ b/src/wasm/decoder.h @@ -327,6 +327,7 @@ class Decoder { bool ok() const { return error_pc_ == nullptr; } bool failed() const { return error_pc_ != nullptr; } + bool more() const { return pc_ < limit_; } const byte* start() { return start_; } const byte* pc() { return pc_; } diff --git a/src/wasm/encoder.cc b/src/wasm/encoder.cc index 70d739ec67..21799eff52 100644 --- a/src/wasm/encoder.cc +++ b/src/wasm/encoder.cc @@ -10,6 +10,7 @@ #include "src/wasm/ast-decoder.h" #include "src/wasm/encoder.h" +#include "src/wasm/wasm-macro-gen.h" #include "src/wasm/wasm-module.h" #include "src/wasm/wasm-opcodes.h" @@ -250,7 +251,6 @@ WasmFunctionEncoder::WasmFunctionEncoder(Zone* zone, LocalType return_type, uint32_t WasmFunctionEncoder::HeaderSize() const { uint32_t size = 3; - if (HasLocals()) size += 8; if (!external_) size += 2; if (HasName()) size += 4; return size; @@ -258,7 +258,15 @@ uint32_t WasmFunctionEncoder::HeaderSize() const { uint32_t WasmFunctionEncoder::BodySize(void) const { - return external_ ? 0 : static_cast(body_.size()); + // TODO(titzer): embed a LocalDeclEncoder in the WasmFunctionEncoder + LocalDeclEncoder local_decl; + local_decl.AddLocals(local_i32_count_, kAstI32); + local_decl.AddLocals(local_i64_count_, kAstI64); + local_decl.AddLocals(local_f32_count_, kAstF32); + local_decl.AddLocals(local_f64_count_, kAstF64); + + return external_ ? 0 + : static_cast(body_.size() + local_decl.Size()); } @@ -271,7 +279,6 @@ void WasmFunctionEncoder::Serialize(byte* buffer, byte** header, byte** body) const { uint8_t decl_bits = (exported_ ? kDeclFunctionExport : 0) | (external_ ? kDeclFunctionImport : 0) | - (HasLocals() ? kDeclFunctionLocals : 0) | (HasName() ? kDeclFunctionName : 0); EmitUint8(header, decl_bits); @@ -284,15 +291,17 @@ void WasmFunctionEncoder::Serialize(byte* buffer, byte** header, (*body) += name_.size(); } - if (HasLocals()) { - EmitUint16(header, local_i32_count_); - EmitUint16(header, local_i64_count_); - EmitUint16(header, local_f32_count_); - EmitUint16(header, local_f64_count_); - } if (!external_) { - EmitUint16(header, static_cast(body_.size())); + // TODO(titzer): embed a LocalDeclEncoder in the WasmFunctionEncoder + LocalDeclEncoder local_decl; + local_decl.AddLocals(local_i32_count_, kAstI32); + local_decl.AddLocals(local_i64_count_, kAstI64); + local_decl.AddLocals(local_f32_count_, kAstF32); + local_decl.AddLocals(local_f64_count_, kAstF64); + + EmitUint16(header, static_cast(body_.size() + local_decl.Size())); + (*header) += local_decl.Emit(*header); if (body_.size() > 0) { std::memcpy(*header, &body_[0], body_.size()); (*header) += body_.size(); diff --git a/src/wasm/encoder.h b/src/wasm/encoder.h index 1afedcb9d6..c765844ae4 100644 --- a/src/wasm/encoder.h +++ b/src/wasm/encoder.h @@ -42,11 +42,6 @@ class WasmFunctionEncoder : public ZoneObject { ZoneVector body_; ZoneVector name_; - bool HasLocals() const { - return (local_i32_count_ + local_i64_count_ + local_f32_count_ + - local_f64_count_) > 0; - } - bool HasName() const { return (exported_ || external_) && name_.size() > 0; } }; diff --git a/src/wasm/module-decoder.cc b/src/wasm/module-decoder.cc index 01df66a2dc..401fb18239 100644 --- a/src/wasm/module-decoder.cc +++ b/src/wasm/module-decoder.cc @@ -338,14 +338,10 @@ class ModuleDecoder : public Decoder { FunctionResult DecodeSingleFunction(ModuleEnv* module_env, WasmFunction* function) { pc_ = start_; - function->sig = consume_sig(); // read signature + function->sig = consume_sig(); // read signature function->name_offset = 0; // ---- name - function->code_start_offset = off(pc_ + 8); // ---- code start + function->code_start_offset = off(pc_); // ---- code start function->code_end_offset = off(limit_); // ---- code end - function->local_i32_count = consume_u16(); // read u16 - function->local_i64_count = consume_u16(); // read u16 - function->local_f32_count = consume_u16(); // read u16 - function->local_f64_count = consume_u16(); // read u16 function->exported = false; // ---- exported function->external = false; // ---- external @@ -473,18 +469,10 @@ class ModuleDecoder : public Decoder { << std::endl; os << std::endl; } - FunctionEnv fenv; - fenv.module = menv; - fenv.sig = function->sig; - fenv.local_i32_count = function->local_i32_count; - fenv.local_i64_count = function->local_i64_count; - fenv.local_f32_count = function->local_f32_count; - fenv.local_f64_count = function->local_f64_count; - fenv.SumLocals(); - - TreeResult result = - VerifyWasmCode(&fenv, start_, start_ + function->code_start_offset, - start_ + function->code_end_offset); + FunctionBody body = {menv, function->sig, start_, + start_ + function->code_start_offset, + start_ + function->code_end_offset}; + TreeResult result = VerifyWasmCode(body); if (result.failed()) { // Wrap the error message from the function decoder. std::ostringstream str; diff --git a/src/wasm/wasm-macro-gen.h b/src/wasm/wasm-macro-gen.h index dc7a9a1c96..85787d9f99 100644 --- a/src/wasm/wasm-macro-gen.h +++ b/src/wasm/wasm-macro-gen.h @@ -7,6 +7,43 @@ #include "src/wasm/wasm-opcodes.h" +#define U32_LE(v) \ + static_cast(v), static_cast((v) >> 8), \ + static_cast((v) >> 16), static_cast((v) >> 24) + +#define U16_LE(v) static_cast(v), static_cast((v) >> 8) + +#define WASM_MODULE_HEADER U32_LE(kWasmMagic), U32_LE(kWasmVersion) + +#define SIG_INDEX(v) U16_LE(v) +#define FUNC_INDEX(v) U16_LE(v) +#define NAME_OFFSET(v) U32_LE(v) +#define BR_TARGET(v) U16_LE(v) + +#define MASK_7 ((1 << 7) - 1) +#define MASK_14 ((1 << 14) - 1) +#define MASK_21 ((1 << 21) - 1) +#define MASK_28 ((1 << 28) - 1) + +#define U32V_1(x) static_cast((x)&MASK_7) +#define U32V_2(x) \ + static_cast(((x)&MASK_7) | 0x80), static_cast(((x) >> 7) & MASK_7) +#define U32V_3(x) \ + static_cast((((x)) & MASK_7) | 0x80), \ + static_cast((((x) >> 7) & MASK_7) | 0x80), \ + static_cast(((x) >> 14) & MASK_7) +#define U32V_4(x) \ + static_cast(((x)&MASK_7) | 0x80), \ + static_cast((((x) >> 7) & MASK_7) | 0x80), \ + static_cast((((x) >> 14) & MASK_7) | 0x80), \ + static_cast(((x) >> 21) & MASK_7) +#define U32V_5(x) \ + static_cast(((x)&MASK_7) | 0x80), \ + static_cast((((x) >> 7) & MASK_7) | 0x80), \ + static_cast((((x) >> 14) & MASK_7) | 0x80), \ + static_cast((((x) >> 21) & MASK_7) | 0x80), \ + static_cast((((x) >> 28) & MASK_7)) + // Convenience macros for building Wasm bytecode directly into a byte array. //------------------------------------------------------------------------------ @@ -57,6 +94,8 @@ #define I64V_IN_RANGE(value, length) \ ((value) >= I64V_MIN(length) && (value) <= I64V_MAX(length)) +#define WASM_NO_LOCALS 0 + namespace v8 { namespace internal { namespace wasm { @@ -73,6 +112,83 @@ inline void CheckI64v(int64_t value, int length) { DCHECK(length == 1 || !I64V_IN_RANGE(value, length - 1)); } +// A helper for encoding local declarations prepended to the body of a +// function. +class LocalDeclEncoder { + public: + // Prepend local declarations by creating a new buffer and copying data + // over. The new buffer must be delete[]'d by the caller. + void Prepend(const byte** start, const byte** end) const { + size_t size = (*end - *start); + byte* buffer = new byte[Size() + size]; + size_t pos = Emit(buffer); + memcpy(buffer + pos, *start, size); + pos += size; + *start = buffer; + *end = buffer + pos; + } + + size_t Emit(byte* buffer) const { + size_t pos = 0; + pos = WriteUint32v(buffer, pos, static_cast(local_decls.size())); + for (size_t i = 0; i < local_decls.size(); i++) { + pos = WriteUint32v(buffer, pos, local_decls[i].first); + buffer[pos++] = WasmOpcodes::LocalTypeCodeFor(local_decls[i].second); + } + DCHECK_EQ(Size(), pos); + return pos; + } + + // Add locals declarations to this helper. Return the index of the newly added + // local(s), with an optional adjustment for the parameters. + uint32_t AddLocals(uint32_t count, LocalType type, + FunctionSig* sig = nullptr) { + if (count == 0) { + return static_cast((sig ? sig->parameter_count() : 0) + + local_decls.size()); + } + size_t pos = local_decls.size(); + if (local_decls.size() > 0 && local_decls.back().second == type) { + count += local_decls.back().first; + local_decls.pop_back(); + } + local_decls.push_back(std::pair(count, type)); + return static_cast(pos + (sig ? sig->parameter_count() : 0)); + } + + size_t Size() const { + size_t size = SizeofUint32v(static_cast(local_decls.size())); + for (auto p : local_decls) size += 1 + SizeofUint32v(p.first); + return size; + } + + private: + std::vector> local_decls; + + size_t SizeofUint32v(uint32_t val) const { + size_t size = 1; + while (true) { + byte b = val & MASK_7; + if (b == val) return size; + size++; + val = val >> 7; + } + } + + // TODO(titzer): lift encoding of u32v to a common place. + size_t WriteUint32v(byte* buffer, size_t pos, uint32_t val) const { + while (true) { + byte b = val & MASK_7; + if (b == val) { + buffer[pos++] = b; + break; + } + buffer[pos++] = 0x80 | b; + val = val >> 7; + } + return pos; + } +}; } // namespace wasm } // namespace internal } // namespace v8 @@ -390,41 +506,4 @@ inline void CheckI64v(int64_t value, int length) { #define WASM_I32_REINTERPRET_F32(x) kExprI32ReinterpretF32, x #define WASM_I64_REINTERPRET_F64(x) kExprI64ReinterpretF64, x -#define U32_LE(v) \ - static_cast(v), static_cast((v) >> 8), \ - static_cast((v) >> 16), static_cast((v) >> 24) - -#define U16_LE(v) static_cast(v), static_cast((v) >> 8) - -#define WASM_MODULE_HEADER U32_LE(kWasmMagic), U32_LE(kWasmVersion) - -#define SIG_INDEX(v) U16_LE(v) -#define FUNC_INDEX(v) U16_LE(v) -#define NAME_OFFSET(v) U32_LE(v) -#define BR_TARGET(v) U16_LE(v) - -#define MASK_7 ((1 << 7) - 1) -#define MASK_14 ((1 << 14) - 1) -#define MASK_21 ((1 << 21) - 1) -#define MASK_28 ((1 << 28) - 1) - -#define U32V_1(x) static_cast(x & MASK_7) -#define U32V_2(x) \ - static_cast((x & MASK_7) | 0x80), static_cast((x >> 7) & MASK_7) -#define U32V_3(x) \ - static_cast((x & MASK_7) | 0x80), \ - static_cast(((x >> 7) & MASK_7) | 0x80), \ - static_cast((x >> 14) & MASK_7) -#define U32V_4(x) \ - static_cast((x & MASK_7) | 0x80), \ - static_cast(((x >> 7) & MASK_7) | 0x80), \ - static_cast(((x >> 14) & MASK_7) | 0x80), \ - static_cast((x >> 21) & MASK_7) -#define U32V_5(x) \ - static_cast((x & MASK_7) | 0x80), \ - static_cast(((x >> 7) & MASK_7) | 0x80), \ - static_cast(((x >> 14) & MASK_7) | 0x80), \ - static_cast(((x >> 21) & MASK_7) | 0x80), \ - static_cast(((x >> 28) & MASK_7)) - #endif // V8_WASM_MACRO_GEN_H_ diff --git a/src/wasm/wasm-opcodes.h b/src/wasm/wasm-opcodes.h index 106572ce99..fdca737080 100644 --- a/src/wasm/wasm-opcodes.h +++ b/src/wasm/wasm-opcodes.h @@ -97,7 +97,8 @@ std::ostream& operator<<(std::ostream& os, const FunctionSig& function); V(StoreGlobal, 0x11, _) \ V(CallFunction, 0x12, _) \ V(CallIndirect, 0x13, _) \ - V(CallImport, 0x1F, _) + V(CallImport, 0x1F, _) \ + V(DeclLocals, 0x1E, _) // Load memory expressions. #define FOREACH_LOAD_MEM_OPCODE(V) \ diff --git a/test/cctest/wasm/test-run-wasm-64.cc b/test/cctest/wasm/test-run-wasm-64.cc index 7f48012318..2bcd538f5f 100644 --- a/test/cctest/wasm/test-run-wasm-64.cc +++ b/test/cctest/wasm/test-run-wasm-64.cc @@ -206,8 +206,7 @@ TEST(Run_WasmCallI64Parameter) { uint32_t index = t.CompileAndAdd(); // Build the calling function. - WasmRunner r; - r.env()->module = &module; + WasmRunner r(&module); BUILD( r, WASM_I32_CONVERT_I64(WASM_CALL_FUNCTION( diff --git a/test/cctest/wasm/test-run-wasm-module.cc b/test/cctest/wasm/test-run-wasm-module.cc index dd78e99748..79406165a6 100644 --- a/test/cctest/wasm/test-run-wasm-module.cc +++ b/test/cctest/wasm/test-run-wasm-module.cc @@ -42,14 +42,16 @@ TEST(Run_WasmModule_CallAdd_rev) { 2, kLocalI32, kLocalI32, kLocalI32, // int,int -> int // func#0 (main) ---------------------------------- kDeclFunctions, 2, kDeclFunctionExport, 0, 0, // sig index - 6, 0, // body size + 7, 0, // body size + 0, // locals kExprCallFunction, 1, // -- kExprI8Const, 77, // -- kExprI8Const, 22, // -- // func#1 ----------------------------------------- 0, // no name, not exported 1, 0, // sig index - 5, 0, // body size + 6, 0, // body size + 0, // locals kExprI32Add, // -- kExprGetLocal, 0, // -- kExprGetLocal, 1, // -- diff --git a/test/cctest/wasm/test-run-wasm.cc b/test/cctest/wasm/test-run-wasm.cc index b94e51dd0f..74247e483a 100644 --- a/test/cctest/wasm/test-run-wasm.cc +++ b/test/cctest/wasm/test-run-wasm.cc @@ -973,7 +973,8 @@ TEST(Run_Wasm_Select_strict1) { TEST(Run_Wasm_Select_strict2) { WasmRunner r(MachineType::Int32()); - r.env()->AddLocals(kAstI32, 2); + r.AllocateLocal(kAstI32); + r.AllocateLocal(kAstI32); // select(b=5, c=6, a) BUILD(r, WASM_SELECT(WASM_SET_LOCAL(1, WASM_I8(5)), WASM_SET_LOCAL(2, WASM_I8(6)), WASM_GET_LOCAL(0))); @@ -985,7 +986,8 @@ TEST(Run_Wasm_Select_strict2) { TEST(Run_Wasm_Select_strict3) { WasmRunner r(MachineType::Int32()); - r.env()->AddLocals(kAstI32, 2); + r.AllocateLocal(kAstI32); + r.AllocateLocal(kAstI32); // select(b=5, c=6, a=b) BUILD(r, WASM_SELECT(WASM_SET_LOCAL(1, WASM_I8(5)), WASM_SET_LOCAL(2, WASM_I8(6)), @@ -1232,8 +1234,7 @@ TEST(Run_Wasm_VoidReturn1) { const int32_t kExpected = -414444; // Build the calling function. - WasmRunner r; - r.env()->module = &module; + WasmRunner r(&module); BUILD(r, B2(WASM_CALL_FUNCTION0(index), WASM_I32V_3(kExpected))); int32_t result = r.Call(); @@ -1252,8 +1253,7 @@ TEST(Run_Wasm_VoidReturn2) { const int32_t kExpected = -414444; // Build the calling function. - WasmRunner r; - r.env()->module = &module; + WasmRunner r(&module); BUILD(r, B2(WASM_CALL_FUNCTION0(index), WASM_I32V_3(kExpected))); int32_t result = r.Call(); @@ -2006,18 +2006,19 @@ static void TestBuildGraphForSimpleExpression(WasmOpcode opcode) { MachineOperatorBuilder::kAllOptionalOps); Graph graph(&zone); JSGraph jsgraph(isolate, &graph, &common, nullptr, nullptr, &machine); - FunctionEnv env; FunctionSig* sig = WasmOpcodes::Signature(opcode); - init_env(&env, sig); if (sig->parameter_count() == 1) { - byte code[] = {static_cast(opcode), kExprGetLocal, 0}; - TestBuildingGraph(&zone, &jsgraph, &env, code, code + arraysize(code)); + byte code[] = {WASM_NO_LOCALS, static_cast(opcode), kExprGetLocal, 0}; + TestBuildingGraph(&zone, &jsgraph, nullptr, sig, code, + code + arraysize(code)); } else { CHECK_EQ(2, sig->parameter_count()); - byte code[] = {static_cast(opcode), kExprGetLocal, 0, kExprGetLocal, - 1}; - TestBuildingGraph(&zone, &jsgraph, &env, code, code + arraysize(code)); + byte code[] = {WASM_NO_LOCALS, static_cast(opcode), + kExprGetLocal, 0, + kExprGetLocal, 1}; + TestBuildingGraph(&zone, &jsgraph, nullptr, sig, code, + code + arraysize(code)); } } diff --git a/test/cctest/wasm/wasm-run-utils.h b/test/cctest/wasm/wasm-run-utils.h index 77e15a4374..614f0fe10c 100644 --- a/test/cctest/wasm/wasm-run-utils.h +++ b/test/cctest/wasm/wasm-run-utils.h @@ -63,16 +63,6 @@ using namespace v8::internal; using namespace v8::internal::compiler; using namespace v8::internal::wasm; -inline void init_env(FunctionEnv* env, FunctionSig* sig) { - env->module = nullptr; - env->sig = sig; - env->local_i32_count = 0; - env->local_i64_count = 0; - env->local_f32_count = 0; - env->local_f64_count = 0; - env->SumLocals(); -} - const uint32_t kMaxGlobalsSize = 128; // A helper for module environments that adds the ability to allocate memory @@ -227,11 +217,11 @@ class TestingModule : public ModuleEnv { } }; - -inline void TestBuildingGraph(Zone* zone, JSGraph* jsgraph, FunctionEnv* env, - const byte* start, const byte* end) { - compiler::WasmGraphBuilder builder(zone, jsgraph, env->sig); - TreeResult result = BuildTFGraph(&builder, env, start, end); +inline void TestBuildingGraph(Zone* zone, JSGraph* jsgraph, ModuleEnv* module, + FunctionSig* sig, const byte* start, + const byte* end) { + compiler::WasmGraphBuilder builder(zone, jsgraph, sig); + TreeResult result = BuildTFGraph(&builder, module, sig, start, end); if (result.failed()) { ptrdiff_t pc = result.error_pc - result.start; ptrdiff_t pt = result.error_pt - result.start; @@ -399,10 +389,9 @@ class WasmFunctionCompiler : public HandleAndZoneScope, : GraphAndBuilders(main_zone()), jsgraph(this->isolate(), this->graph(), this->common(), nullptr, nullptr, this->machine()), + sig(sig), descriptor_(nullptr), testing_module_(module) { - init_env(&env, sig); - env.module = module; if (module) { // Get a new function from the testing module. function_ = nullptr; @@ -420,12 +409,13 @@ class WasmFunctionCompiler : public HandleAndZoneScope, } JSGraph jsgraph; - FunctionEnv env; + FunctionSig* sig; // The call descriptor is initialized when the function is compiled. CallDescriptor* descriptor_; TestingModule* testing_module_; WasmFunction* function_; int function_index_; + LocalDeclEncoder local_decls; Isolate* isolate() { return main_isolate(); } Graph* graph() const { return main_graph_; } @@ -434,28 +424,23 @@ class WasmFunctionCompiler : public HandleAndZoneScope, MachineOperatorBuilder* machine() { return &main_machine_; } void InitializeDescriptor() { if (descriptor_ == nullptr) { - descriptor_ = env.module->GetWasmCallDescriptor(main_zone(), env.sig); + descriptor_ = testing_module_->GetWasmCallDescriptor(main_zone(), sig); } } CallDescriptor* descriptor() { return descriptor_; } void Build(const byte* start, const byte* end) { - // Transfer local counts before compiling. - function()->local_i32_count = env.local_i32_count; - function()->local_i64_count = env.local_i64_count; - function()->local_f32_count = env.local_f32_count; - function()->local_f64_count = env.local_f64_count; - // Build the TurboFan graph. - TestBuildingGraph(main_zone(), &jsgraph, &env, start, end); + local_decls.Prepend(&start, &end); + TestBuildingGraph(main_zone(), &jsgraph, testing_module_, sig, start, end); + delete[] start; } byte AllocateLocal(LocalType type) { - int result = static_cast(env.total_locals); - env.AddLocals(type, 1); - byte b = static_cast(result); - CHECK_EQ(result, b); - return b; + uint32_t index = local_decls.AddLocals(1, type, sig); + byte result = static_cast(index); + DCHECK_EQ(index, result); + return result; } // TODO(titzer): remove me. @@ -542,8 +527,6 @@ class WasmRunner { wrapper_.Init(compiler_.descriptor(), p0, p1, p2, p3); } - FunctionEnv* env() { return &compiler_.env; } - // Builds a graph from the given Wasm code and generates the machine // code and call wrapper for that graph. This method must not be called // more than once. @@ -593,13 +576,7 @@ class WasmRunner { return return_value; } - byte AllocateLocal(LocalType type) { - int result = static_cast(env()->total_locals); - env()->AddLocals(type, 1); - byte b = static_cast(result); - CHECK_EQ(result, b); - return b; - } + byte AllocateLocal(LocalType type) { return compiler_.AllocateLocal(type); } protected: Zone zone; diff --git a/test/mjsunit/wasm/test-wasm-module-builder.js b/test/mjsunit/wasm/test-wasm-module-builder.js index 7c287efa4f..3063553303 100644 --- a/test/mjsunit/wasm/test-wasm-module-builder.js +++ b/test/mjsunit/wasm/test-wasm-module-builder.js @@ -47,6 +47,29 @@ var debug = false; assertEquals(27777, instance.exports.main(27777)); })(); +(function LocalsTest2() { + // TODO(titzer): i64 only works on 64-bit platforms. + var types = [ + {locals: {i32_count: 1}, type: kAstI32}, +// {locals: {i64_count: 1}, type: kAstI64}, + {locals: {f32_count: 1}, type: kAstF32}, + {locals: {f64_count: 1}, type: kAstF64}, + ]; + + for (p of types) { + var module = new WasmModuleBuilder(); + module.addFunction(undefined, [p.type, p.type]) + .addLocals(p.locals) + .addBody([kExprSetLocal, 1, kExprGetLocal, 0]) + .exportAs("main"); + + var buffer = module.toBuffer(debug); + var instance = _WASMEXP_.instantiateModule(buffer); + assertEquals(19, instance.exports.main(19)); + assertEquals(27777, instance.exports.main(27777)); + } +})(); + (function CallTest() { var module = new WasmModuleBuilder(); module.addFunction("add", [kAstI32, kAstI32, kAstI32]) diff --git a/test/mjsunit/wasm/verify-function-simple.js b/test/mjsunit/wasm/verify-function-simple.js index c4d51c7423..ef87d0e136 100644 --- a/test/mjsunit/wasm/verify-function-simple.js +++ b/test/mjsunit/wasm/verify-function-simple.js @@ -9,10 +9,7 @@ load("test/mjsunit/wasm/wasm-constants.js"); try { var data = bytes( 0, kAstStmt, // signature - 3, 0, // local int32 count - 4, 0, // local int64 count - 5, 0, // local float32 count - 6, 0, // local float64 count + kDeclNoLocals, // -- kExprNop // body ); @@ -27,10 +24,7 @@ var threw = false; try { var data = bytes( 0, kAstI32, // signature - 2, 0, // local int32 count - 3, 0, // local int64 count - 4, 0, // local float32 count - 5, 0, // local float64 count + kDeclNoLocals, // -- kExprBlock, 2, kExprNop, kExprNop // body ); diff --git a/test/mjsunit/wasm/wasm-constants.js b/test/mjsunit/wasm/wasm-constants.js index 17431bc543..d0d5aed84e 100644 --- a/test/mjsunit/wasm/wasm-constants.js +++ b/test/mjsunit/wasm/wasm-constants.js @@ -48,6 +48,8 @@ function bytesWithHeader() { return buffer; } +var kDeclNoLocals = 0; + // Section declaration constants var kDeclMemory = 0x00; var kDeclSignatures = 0x01; diff --git a/test/mjsunit/wasm/wasm-module-builder.js b/test/mjsunit/wasm/wasm-module-builder.js index e51d4425fd..dd15001891 100644 --- a/test/mjsunit/wasm/wasm-module-builder.js +++ b/test/mjsunit/wasm/wasm-module-builder.js @@ -190,25 +190,46 @@ WasmModuleBuilder.prototype.toArray = function(debug) { var hasName = func.name != undefined && func.name.length > 0; names = names || hasName; if (hasName) flags |= kDeclFunctionName; - if (func.locals != undefined) flags |= kDeclFunctionLocals; exports += func.exports.length; emit_u8(bytes, flags); emit_u16(bytes, func.sig_index); - if (hasName) { - emit_string(bytes, func.name); + if (hasName) emit_string(bytes, func.name); + + // Function body length will be patched later. + var length_pos = bytes.length; + emit_u16(bytes, 0); + + var local_decls = []; + var l = func.locals; + if (l != undefined) { + var local_decls_count = 0; + if (l.i32_count > 0) { + local_decls.push({count: l.i32_count, type: kAstI32}); + } + if (l.i64_count > 0) { + local_decls.push({count: l.i64_count, type: kAstI64}); + } + if (l.f32_count > 0) { + local_decls.push({count: l.f32_count, type: kAstF32}); + } + if (l.f64_count > 0) { + local_decls.push({count: l.f64_count, type: kAstF64}); + } } - if (func.locals != undefined) { - emit_u16(bytes, func.locals.i32_count); - emit_u16(bytes, func.locals.i64_count); - emit_u16(bytes, func.locals.f32_count); - emit_u16(bytes, func.locals.f64_count); + emit_u8(bytes, local_decls.length); + for (decl of local_decls) { + emit_varint(bytes, decl.count); + emit_u8(bytes, decl.type); } - emit_u16(bytes, func.body.length); + for (var i = 0; i < func.body.length; i++) { emit_u8(bytes, func.body[i]); } + var length = bytes.length - length_pos - 2; + bytes[length_pos] = length & 0xff; + bytes[length_pos + 1] = (length >> 8) & 0xff; index++; } diff --git a/test/unittests/wasm/ast-decoder-unittest.cc b/test/unittests/wasm/ast-decoder-unittest.cc index e491179c8c..95a791b9a6 100644 --- a/test/unittests/wasm/ast-decoder-unittest.cc +++ b/test/unittests/wasm/ast-decoder-unittest.cc @@ -55,50 +55,33 @@ static const WasmOpcode kInt32BinopOpcodes[] = { Verify(kError, env, code, code + arraysize(code)); \ } while (false) -#define VERIFY(...) \ - do { \ - static const byte code[] = {__VA_ARGS__}; \ - Verify(kSuccess, &env_v_i, code, code + sizeof(code)); \ +#define VERIFY(...) \ + do { \ + static const byte code[] = {__VA_ARGS__}; \ + Verify(kSuccess, sigs.v_i(), code, code + sizeof(code)); \ } while (false) class AstDecoderTest : public TestWithZone { public: - AstDecoderTest() : TestWithZone(), sigs() { - init_env(&env_i_i, sigs.i_i()); - init_env(&env_v_v, sigs.v_v()); - init_env(&env_v_i, sigs.v_i()); - init_env(&env_i_f, sigs.i_f()); - init_env(&env_i_d, sigs.i_d()); - init_env(&env_l_l, sigs.l_l()); - init_env(&env_f_ff, sigs.f_ff()); - init_env(&env_d_dd, sigs.d_dd()); - } + typedef std::pair LocalsDecl; + AstDecoderTest() : module(nullptr) {} TestSignatures sigs; + ModuleEnv* module; + LocalDeclEncoder local_decls; - FunctionEnv env_i_i; - FunctionEnv env_v_v; - FunctionEnv env_v_i; - FunctionEnv env_i_f; - FunctionEnv env_i_d; - FunctionEnv env_l_l; - FunctionEnv env_f_ff; - FunctionEnv env_d_dd; - - static void init_env(FunctionEnv* env, FunctionSig* sig) { - env->module = nullptr; - env->sig = sig; - env->local_i32_count = 0; - env->local_i64_count = 0; - env->local_f32_count = 0; - env->local_f64_count = 0; - env->SumLocals(); + void AddLocals(LocalType type, uint32_t count) { + local_decls.AddLocals(count, type); } - // A wrapper around VerifyWasmCode() that renders a nice failure message. - void Verify(ErrorCode expected, FunctionEnv* env, const byte* start, + // Preprends local variable declarations and renders nice error messages for + // verification failures. + void Verify(ErrorCode expected, FunctionSig* sig, const byte* start, const byte* end) { - TreeResult result = VerifyWasmCode(env, start, end); + local_decls.Prepend(&start, &end); + // Verify the code. + TreeResult result = VerifyWasmCode(module, sig, start, end); + if (result.error_code != expected) { ptrdiff_t pc = result.error_pc - result.start; ptrdiff_t pt = result.error_pt - result.start; @@ -117,15 +100,15 @@ class AstDecoderTest : public TestWithZone { } FATAL(str.str().c_str()); } + + delete[] start; // local_decls.Prepend() allocated a new buffer. } void TestBinop(WasmOpcode opcode, FunctionSig* success) { // op(local[0], local[1]) byte code[] = {static_cast(opcode), kExprGetLocal, 0, kExprGetLocal, 1}; - FunctionEnv env; - init_env(&env, success); - EXPECT_VERIFIES(&env, code); + EXPECT_VERIFIES(success, code); // Try all combinations of return and parameter types. for (size_t i = 0; i < arraysize(kLocalTypes); i++) { @@ -137,8 +120,7 @@ class AstDecoderTest : public TestWithZone { types[2] != success->GetParam(1)) { // Test signature mismatch. FunctionSig sig(1, 2, types); - init_env(&env, &sig); - EXPECT_FAILURE(&env, code); + EXPECT_FAILURE(&sig, code); } } } @@ -152,12 +134,10 @@ class AstDecoderTest : public TestWithZone { void TestUnop(WasmOpcode opcode, LocalType ret_type, LocalType param_type) { // Return(op(local[0])) byte code[] = {static_cast(opcode), kExprGetLocal, 0}; - FunctionEnv env; { LocalType types[] = {ret_type, param_type}; FunctionSig sig(1, 1, types); - init_env(&env, &sig); - EXPECT_VERIFIES(&env, code); + EXPECT_VERIFIES(&sig, code); } // Try all combinations of return and parameter types. @@ -167,8 +147,7 @@ class AstDecoderTest : public TestWithZone { if (types[0] != ret_type || types[1] != param_type) { // Test signature mismatch. FunctionSig sig(1, 1, types); - init_env(&env, &sig); - EXPECT_FAILURE(&env, code); + EXPECT_FAILURE(&sig, code); } } } @@ -176,46 +155,35 @@ class AstDecoderTest : public TestWithZone { }; -static FunctionEnv CreateInt32FunctionEnv(FunctionSig* sig, int count) { - FunctionEnv env; - env.module = nullptr; - env.sig = sig; - env.local_i32_count = count; - env.local_f64_count = 0; - env.local_f32_count = 0; - env.total_locals = static_cast(count + sig->parameter_count()); - return env; -} - TEST_F(AstDecoderTest, Int8Const) { byte code[] = {kExprI8Const, 0}; for (int i = -128; i < 128; i++) { code[1] = static_cast(i); - EXPECT_VERIFIES(&env_i_i, code); + EXPECT_VERIFIES(sigs.i_i(), code); } } TEST_F(AstDecoderTest, EmptyFunction) { byte code[] = {0}; - Verify(kSuccess, &env_v_v, code, code); - Verify(kError, &env_i_i, code, code); + Verify(kSuccess, sigs.v_v(), code, code); + Verify(kError, sigs.i_i(), code, code); } TEST_F(AstDecoderTest, IncompleteIf1) { byte code[] = {kExprIf}; - EXPECT_FAILURE(&env_v_v, code); - EXPECT_FAILURE(&env_i_i, code); + EXPECT_FAILURE(sigs.v_v(), code); + EXPECT_FAILURE(sigs.i_i(), code); } TEST_F(AstDecoderTest, IncompleteIf2) { byte code[] = {kExprIf, kExprI8Const, 0}; - EXPECT_FAILURE(&env_v_v, code); - EXPECT_FAILURE(&env_i_i, code); + EXPECT_FAILURE(sigs.v_v(), code); + EXPECT_FAILURE(sigs.i_i(), code); } TEST_F(AstDecoderTest, Int8Const_fallthru) { byte code[] = {kExprI8Const, 0, kExprI8Const, 1}; - EXPECT_VERIFIES(&env_i_i, code); + EXPECT_VERIFIES(sigs.i_i(), code); } TEST_F(AstDecoderTest, Int32Const) { @@ -223,20 +191,20 @@ TEST_F(AstDecoderTest, Int32Const) { for (int32_t i = kMinInt; i < kMaxInt - kInc; i = i + kInc) { // TODO(binji): expand test for other sized int32s; 1 through 5 bytes. byte code[] = {WASM_I32V(i)}; - EXPECT_VERIFIES(&env_i_i, code); + EXPECT_VERIFIES(sigs.i_i(), code); } } TEST_F(AstDecoderTest, Int8Const_fallthru2) { byte code[] = {WASM_I8(0), WASM_I32V_4(0x1122334)}; - EXPECT_VERIFIES(&env_i_i, code); + EXPECT_VERIFIES(sigs.i_i(), code); } TEST_F(AstDecoderTest, Int64Const) { const int kInc = 4498211; for (int32_t i = kMinInt; i < kMaxInt - kInc; i = i + kInc) { byte code[] = {WASM_I64V((static_cast(i) << 32) | i)}; - EXPECT_VERIFIES(&env_l_l, code); + EXPECT_VERIFIES(sigs.l_l(), code); } } @@ -245,7 +213,7 @@ TEST_F(AstDecoderTest, Float32Const) { float* ptr = reinterpret_cast(code + 1); for (int i = 0; i < 30; i++) { *ptr = i * -7.75f; - EXPECT_VERIFIES(&env_f_ff, code); + EXPECT_VERIFIES(sigs.f_ff(), code); } } @@ -254,7 +222,7 @@ TEST_F(AstDecoderTest, Float64Const) { double* ptr = reinterpret_cast(code + 1); for (int i = 0; i < 30; i++) { *ptr = i * 33.45; - EXPECT_VERIFIES(&env_d_dd, code); + EXPECT_VERIFIES(sigs.d_dd(), code); } } @@ -262,102 +230,89 @@ TEST_F(AstDecoderTest, Int32Const_off_end) { byte code[] = {kExprI32Const, 0xaa, 0xbb, 0xcc, 0x44}; for (int size = 1; size <= 4; size++) { - Verify(kError, &env_i_i, code, code + size); + Verify(kError, sigs.i_i(), code, code + size); } } TEST_F(AstDecoderTest, GetLocal0_param) { - EXPECT_VERIFIES(&env_i_i, kCodeGetLocal0); + EXPECT_VERIFIES(sigs.i_i(), kCodeGetLocal0); } TEST_F(AstDecoderTest, GetLocal0_local) { - FunctionEnv env; - init_env(&env, sigs.i_v()); - env.AddLocals(kAstI32, 1); - EXPECT_VERIFIES(&env, kCodeGetLocal0); + AddLocals(kAstI32, 1); + EXPECT_VERIFIES(sigs.i_v(), kCodeGetLocal0); } TEST_F(AstDecoderTest, GetLocal0_param_n) { FunctionSig* array[] = {sigs.i_i(), sigs.i_ii(), sigs.i_iii()}; for (size_t i = 0; i < arraysize(array); i++) { - FunctionEnv env = CreateInt32FunctionEnv(array[i], 0); - EXPECT_VERIFIES(&env, kCodeGetLocal0); + EXPECT_VERIFIES(array[i], kCodeGetLocal0); } } TEST_F(AstDecoderTest, GetLocalN_local) { for (byte i = 1; i < 8; i++) { - FunctionEnv env = CreateInt32FunctionEnv(sigs.i_v(), i); + AddLocals(kAstI32, 1); for (byte j = 0; j < i; j++) { byte code[] = {kExprGetLocal, j}; - EXPECT_VERIFIES(&env, code); + EXPECT_VERIFIES(sigs.i_v(), code); } } } TEST_F(AstDecoderTest, GetLocal0_fail_no_params) { - FunctionEnv env = CreateInt32FunctionEnv(sigs.i_v(), 0); - - EXPECT_FAILURE(&env, kCodeGetLocal0); + EXPECT_FAILURE(sigs.i_v(), kCodeGetLocal0); } TEST_F(AstDecoderTest, GetLocal1_fail_no_locals) { - EXPECT_FAILURE(&env_i_i, kCodeGetLocal1); + EXPECT_FAILURE(sigs.i_i(), kCodeGetLocal1); } TEST_F(AstDecoderTest, GetLocal_off_end) { static const byte code[] = {kExprGetLocal}; - EXPECT_FAILURE(&env_i_i, code); + EXPECT_FAILURE(sigs.i_i(), code); } TEST_F(AstDecoderTest, GetLocal_varint) { - env_i_i.local_i32_count = 1000000000; - env_i_i.total_locals += 1000000000; + const int kMaxLocals = 8000000; + AddLocals(kAstI32, kMaxLocals); - { - static const byte code[] = {kExprGetLocal, 0xFF, 0x01}; - EXPECT_VERIFIES(&env_i_i, code); - EXPECT_FAILURE(&env_i_f, code); + for (int index = 0; index < kMaxLocals; index = index * 11 + 5) { + EXPECT_VERIFIES_INLINE(sigs.i_i(), kExprGetLocal, U32V_1(index)); + EXPECT_VERIFIES_INLINE(sigs.i_i(), kExprGetLocal, U32V_2(index)); + EXPECT_VERIFIES_INLINE(sigs.i_i(), kExprGetLocal, U32V_3(index)); + EXPECT_VERIFIES_INLINE(sigs.i_i(), kExprGetLocal, U32V_4(index)); } - { - static const byte code[] = {kExprGetLocal, 0xF0, 0x80, 0x01}; - EXPECT_VERIFIES(&env_i_i, code); - EXPECT_FAILURE(&env_i_f, code); - } + EXPECT_VERIFIES_INLINE(sigs.i_i(), kExprGetLocal, U32V_5(kMaxLocals - 1)); - { - static const byte code[] = {kExprGetLocal, 0xF2, 0x81, 0x82, 0x01}; - EXPECT_VERIFIES(&env_i_i, code); - EXPECT_FAILURE(&env_i_f, code); - } + EXPECT_VERIFIES_INLINE(sigs.i_i(), kExprGetLocal, U32V_4(kMaxLocals - 1)); + EXPECT_VERIFIES_INLINE(sigs.i_i(), kExprGetLocal, U32V_4(kMaxLocals)); + EXPECT_FAILURE_INLINE(sigs.i_i(), kExprGetLocal, U32V_4(kMaxLocals + 1)); - { - static const byte code[] = {kExprGetLocal, 0xF3, 0xA1, 0xB1, 0xC1, 0x01}; - EXPECT_VERIFIES(&env_i_i, code); - EXPECT_FAILURE(&env_i_f, code); - } + EXPECT_FAILURE_INLINE(sigs.i_v(), kExprGetLocal, U32V_4(kMaxLocals)); + EXPECT_FAILURE_INLINE(sigs.i_v(), kExprGetLocal, U32V_4(kMaxLocals + 1)); } TEST_F(AstDecoderTest, Binops_off_end) { byte code1[] = {0}; // [opcode] for (size_t i = 0; i < arraysize(kInt32BinopOpcodes); i++) { code1[0] = kInt32BinopOpcodes[i]; - EXPECT_FAILURE(&env_i_i, code1); + EXPECT_FAILURE(sigs.i_i(), code1); } byte code3[] = {0, kExprGetLocal, 0}; // [opcode] [expr] for (size_t i = 0; i < arraysize(kInt32BinopOpcodes); i++) { code3[0] = kInt32BinopOpcodes[i]; - EXPECT_FAILURE(&env_i_i, code3); + EXPECT_FAILURE(sigs.i_i(), code3); } byte code4[] = {0, kExprGetLocal, 0, 0}; // [opcode] [expr] [opcode] for (size_t i = 0; i < arraysize(kInt32BinopOpcodes); i++) { code4[0] = kInt32BinopOpcodes[i]; code4[3] = kInt32BinopOpcodes[i]; - EXPECT_FAILURE(&env_i_i, code4); + EXPECT_FAILURE(sigs.i_i(), code4); } } @@ -367,56 +322,55 @@ TEST_F(AstDecoderTest, Binops_off_end) { //=================================================================== TEST_F(AstDecoderTest, Nop) { static const byte code[] = {kExprNop}; - EXPECT_VERIFIES(&env_v_v, code); + EXPECT_VERIFIES(sigs.v_v(), code); } TEST_F(AstDecoderTest, SetLocal0_param) { static const byte code[] = {kExprSetLocal, 0, kExprI8Const, 0}; - EXPECT_VERIFIES(&env_i_i, code); + EXPECT_VERIFIES(sigs.i_i(), code); } TEST_F(AstDecoderTest, SetLocal0_local) { byte code[] = {kExprSetLocal, 0, kExprI8Const, 0}; - FunctionEnv env = CreateInt32FunctionEnv(sigs.i_v(), 1); - - EXPECT_VERIFIES(&env, code); + AddLocals(kAstI32, 1); + EXPECT_VERIFIES(sigs.i_v(), code); } TEST_F(AstDecoderTest, SetLocalN_local) { for (byte i = 1; i < 8; i++) { - FunctionEnv env = CreateInt32FunctionEnv(sigs.i_v(), i); + AddLocals(kAstI32, 1); for (byte j = 0; j < i; j++) { byte code[] = {kExprSetLocal, j, kExprI8Const, i}; - EXPECT_VERIFIES(&env, code); + EXPECT_VERIFIES(sigs.v_v(), code); } } } TEST_F(AstDecoderTest, Block0) { static const byte code[] = {kExprBlock, 0}; - EXPECT_VERIFIES(&env_v_v, code); + EXPECT_VERIFIES(sigs.v_v(), code); } TEST_F(AstDecoderTest, Block0_fallthru1) { static const byte code[] = {kExprBlock, 0, kExprBlock, 0}; - EXPECT_VERIFIES(&env_v_v, code); + EXPECT_VERIFIES(sigs.v_v(), code); } TEST_F(AstDecoderTest, Block1) { static const byte code[] = {kExprBlock, 1, kExprSetLocal, 0, kExprI8Const, 0}; - EXPECT_VERIFIES(&env_i_i, code); + EXPECT_VERIFIES(sigs.i_i(), code); } TEST_F(AstDecoderTest, Block0_fallthru2) { static const byte code[] = {kExprBlock, 0, kExprSetLocal, 0, kExprI8Const, 0}; - EXPECT_VERIFIES(&env_i_i, code); + EXPECT_VERIFIES(sigs.i_i(), code); } TEST_F(AstDecoderTest, Block2) { static const byte code[] = {kExprBlock, 2, // -- kExprSetLocal, 0, kExprI8Const, 0, // -- kExprSetLocal, 0, kExprI8Const, 0}; // -- - EXPECT_VERIFIES(&env_i_i, code); + EXPECT_VERIFIES(sigs.i_i(), code); } TEST_F(AstDecoderTest, Block2_fallthru) { @@ -424,7 +378,7 @@ TEST_F(AstDecoderTest, Block2_fallthru) { kExprSetLocal, 0, kExprI8Const, 0, // -- kExprSetLocal, 0, kExprI8Const, 0, // -- kExprI8Const, 11}; // -- - EXPECT_VERIFIES(&env_i_i, code); + EXPECT_VERIFIES(sigs.i_i(), code); } TEST_F(AstDecoderTest, BlockN) { @@ -439,7 +393,7 @@ TEST_F(AstDecoderTest, BlockN) { memcpy(code + sizeof(block) + j * sizeof(kCodeSetLocal0), kCodeSetLocal0, sizeof(kCodeSetLocal0)); } - Verify(kSuccess, &env_v_i, code, code + total); + Verify(kSuccess, sigs.v_i(), code, code + total); free(code); } } @@ -447,66 +401,66 @@ TEST_F(AstDecoderTest, BlockN) { TEST_F(AstDecoderTest, BlockN_off_end) { for (byte i = 2; i < 10; i++) { byte code[] = {kExprBlock, i, kExprNop}; - EXPECT_FAILURE(&env_v_v, code); + EXPECT_FAILURE(sigs.v_v(), code); } } TEST_F(AstDecoderTest, Block1_break) { static const byte code[] = {kExprBlock, 1, kExprBr, 0, kExprNop}; - EXPECT_VERIFIES(&env_v_v, code); + EXPECT_VERIFIES(sigs.v_v(), code); } TEST_F(AstDecoderTest, Block2_break) { static const byte code[] = {kExprBlock, 2, kExprNop, kExprBr, 0, kExprNop}; - EXPECT_VERIFIES(&env_v_v, code); + EXPECT_VERIFIES(sigs.v_v(), code); } TEST_F(AstDecoderTest, Block1_continue) { static const byte code[] = {kExprBlock, 1, kExprBr, 1, kExprNop}; - EXPECT_FAILURE(&env_v_v, code); + EXPECT_FAILURE(sigs.v_v(), code); } TEST_F(AstDecoderTest, Block2_continue) { static const byte code[] = {kExprBlock, 2, kExprNop, kExprBr, 1, kExprNop}; - EXPECT_FAILURE(&env_v_v, code); + EXPECT_FAILURE(sigs.v_v(), code); } TEST_F(AstDecoderTest, ExprBlock0) { static const byte code[] = {kExprBlock, 0}; - EXPECT_VERIFIES(&env_v_v, code); + EXPECT_VERIFIES(sigs.v_v(), code); } TEST_F(AstDecoderTest, ExprBlock1a) { static const byte code[] = {kExprBlock, 1, kExprI8Const, 0}; - EXPECT_VERIFIES(&env_i_i, code); + EXPECT_VERIFIES(sigs.i_i(), code); } TEST_F(AstDecoderTest, ExprBlock1b) { static const byte code[] = {kExprBlock, 1, kExprI8Const, 0}; - EXPECT_FAILURE(&env_f_ff, code); + EXPECT_FAILURE(sigs.f_ff(), code); } TEST_F(AstDecoderTest, ExprBlock1c) { static const byte code[] = {kExprBlock, 1, kExprF32Const, 0, 0, 0, 0}; - EXPECT_VERIFIES(&env_f_ff, code); + EXPECT_VERIFIES(sigs.f_ff(), code); } TEST_F(AstDecoderTest, IfEmpty) { static const byte code[] = {kExprIf, kExprGetLocal, 0, kExprNop}; - EXPECT_VERIFIES(&env_v_i, code); + EXPECT_VERIFIES(sigs.v_i(), code); } TEST_F(AstDecoderTest, IfSet) { static const byte code[] = {kExprIfElse, kExprGetLocal, 0, kExprSetLocal, 0, kExprI8Const, 0, kExprNop}; - EXPECT_VERIFIES(&env_v_i, code); + EXPECT_VERIFIES(sigs.v_i(), code); } TEST_F(AstDecoderTest, IfBlock1) { static const byte code[] = {kExprIfElse, kExprGetLocal, 0, kExprBlock, 1, kExprSetLocal, 0, kExprI8Const, 0, kExprNop}; - EXPECT_VERIFIES(&env_v_i, code); + EXPECT_VERIFIES(sigs.v_i(), code); } TEST_F(AstDecoderTest, IfBlock2) { @@ -514,13 +468,13 @@ TEST_F(AstDecoderTest, IfBlock2) { 2, kExprSetLocal, 0, kExprI8Const, 0, kExprSetLocal, 0, kExprI8Const, 0}; - EXPECT_VERIFIES(&env_v_i, code); + EXPECT_VERIFIES(sigs.v_i(), code); } TEST_F(AstDecoderTest, IfElseEmpty) { static const byte code[] = {kExprIfElse, kExprGetLocal, 0, kExprNop, kExprNop}; - EXPECT_VERIFIES(&env_v_i, code); + EXPECT_VERIFIES(sigs.v_i(), code); } TEST_F(AstDecoderTest, IfElseSet) { @@ -535,7 +489,7 @@ TEST_F(AstDecoderTest, IfElseSet) { 0, kExprI8Const, 1}; // -- - EXPECT_VERIFIES(&env_v_i, code); + EXPECT_VERIFIES(sigs.v_i(), code); } TEST_F(AstDecoderTest, IfElseUnreachable) { @@ -544,111 +498,110 @@ TEST_F(AstDecoderTest, IfElseUnreachable) { for (size_t i = 0; i < arraysize(kLocalTypes); i++) { LocalType types[] = {kAstI32, kLocalTypes[i]}; - FunctionEnv env; FunctionSig sig(1, 1, types); - init_env(&env, &sig); if (kLocalTypes[i] == kAstI32) { - EXPECT_VERIFIES(&env, code); + EXPECT_VERIFIES(&sig, code); } else { - EXPECT_FAILURE(&env, code); + EXPECT_FAILURE(&sig, code); } } } TEST_F(AstDecoderTest, Loop0) { static const byte code[] = {kExprLoop, 0}; - EXPECT_VERIFIES(&env_v_v, code); + EXPECT_VERIFIES(sigs.v_v(), code); } TEST_F(AstDecoderTest, Loop1) { static const byte code[] = {kExprLoop, 1, kExprSetLocal, 0, kExprI8Const, 0}; - EXPECT_VERIFIES(&env_v_i, code); + EXPECT_VERIFIES(sigs.v_i(), code); } TEST_F(AstDecoderTest, Loop2) { static const byte code[] = {kExprLoop, 2, // -- kExprSetLocal, 0, kExprI8Const, 0, // -- kExprSetLocal, 0, kExprI8Const, 0}; // -- - EXPECT_VERIFIES(&env_v_i, code); + EXPECT_VERIFIES(sigs.v_i(), code); } TEST_F(AstDecoderTest, Loop1_continue) { static const byte code[] = {kExprLoop, 1, kExprBr, 0, kExprNop}; - EXPECT_VERIFIES(&env_v_v, code); + EXPECT_VERIFIES(sigs.v_v(), code); } TEST_F(AstDecoderTest, Loop1_break) { static const byte code[] = {kExprLoop, 1, kExprBr, 1, kExprNop}; - EXPECT_VERIFIES(&env_v_v, code); + EXPECT_VERIFIES(sigs.v_v(), code); } TEST_F(AstDecoderTest, Loop2_continue) { static const byte code[] = {kExprLoop, 2, // -- kExprSetLocal, 0, kExprI8Const, 0, // -- kExprBr, 0, kExprNop}; // -- - EXPECT_VERIFIES(&env_v_i, code); + EXPECT_VERIFIES(sigs.v_i(), code); } TEST_F(AstDecoderTest, Loop2_break) { static const byte code[] = {kExprLoop, 2, // -- kExprSetLocal, 0, kExprI8Const, 0, // -- kExprBr, 1, kExprNop}; // -- - EXPECT_VERIFIES(&env_v_i, code); + EXPECT_VERIFIES(sigs.v_i(), code); } TEST_F(AstDecoderTest, ExprLoop0) { static const byte code[] = {kExprLoop, 0}; - EXPECT_VERIFIES(&env_v_v, code); + EXPECT_VERIFIES(sigs.v_v(), code); } TEST_F(AstDecoderTest, ExprLoop1a) { static const byte code[] = {kExprLoop, 1, kExprBr, 0, kExprI8Const, 0}; - EXPECT_VERIFIES(&env_i_i, code); + EXPECT_VERIFIES(sigs.i_i(), code); } TEST_F(AstDecoderTest, ExprLoop1b) { static const byte code[] = {kExprLoop, 1, kExprBr, 0, kExprI8Const, 0}; - EXPECT_VERIFIES(&env_i_i, code); + EXPECT_VERIFIES(sigs.i_i(), code); } TEST_F(AstDecoderTest, ExprLoop2_unreachable) { static const byte code[] = {kExprLoop, 2, kExprBr, 0, kExprI8Const, 0, kExprNop}; - EXPECT_VERIFIES(&env_i_i, code); + EXPECT_VERIFIES(sigs.i_i(), code); } TEST_F(AstDecoderTest, ReturnVoid1) { static const byte code[] = {kExprNop}; - EXPECT_VERIFIES(&env_v_v, code); - EXPECT_FAILURE(&env_i_i, code); - EXPECT_FAILURE(&env_i_f, code); + EXPECT_VERIFIES(sigs.v_v(), code); + EXPECT_FAILURE(sigs.i_i(), code); + EXPECT_FAILURE(sigs.i_f(), code); } TEST_F(AstDecoderTest, ReturnVoid2) { static const byte code[] = {kExprBlock, 1, kExprBr, 0, kExprNop}; - EXPECT_VERIFIES(&env_v_v, code); - EXPECT_FAILURE(&env_i_i, code); - EXPECT_FAILURE(&env_i_f, code); + EXPECT_VERIFIES(sigs.v_v(), code); + EXPECT_FAILURE(sigs.i_i(), code); + EXPECT_FAILURE(sigs.i_f(), code); } TEST_F(AstDecoderTest, ReturnVoid3) { - EXPECT_VERIFIES_INLINE(&env_v_v, kExprI8Const, 0); - EXPECT_VERIFIES_INLINE(&env_v_v, kExprI32Const, 0, 0, 0, 0); - EXPECT_VERIFIES_INLINE(&env_v_v, kExprI64Const, 0, 0, 0, 0, 0, 0, 0, 0); - EXPECT_VERIFIES_INLINE(&env_v_v, kExprF32Const, 0, 0, 0, 0); - EXPECT_VERIFIES_INLINE(&env_v_v, kExprF64Const, 0, 0, 0, 0, 0, 0, 0, 0); + EXPECT_VERIFIES_INLINE(sigs.v_v(), kExprI8Const, 0); + EXPECT_VERIFIES_INLINE(sigs.v_v(), kExprI32Const, 0, 0, 0, 0); + EXPECT_VERIFIES_INLINE(sigs.v_v(), kExprI64Const, 0, 0, 0, 0, 0, 0, 0, 0); + EXPECT_VERIFIES_INLINE(sigs.v_v(), kExprF32Const, 0, 0, 0, 0); + EXPECT_VERIFIES_INLINE(sigs.v_v(), kExprF64Const, 0, 0, 0, 0, 0, 0, 0, 0); - EXPECT_VERIFIES_INLINE(&env_v_i, kExprGetLocal, 0); + EXPECT_VERIFIES_INLINE(sigs.v_i(), kExprGetLocal, 0); } TEST_F(AstDecoderTest, Unreachable1) { - EXPECT_VERIFIES_INLINE(&env_v_v, kExprUnreachable); - EXPECT_VERIFIES_INLINE(&env_v_v, kExprUnreachable, kExprUnreachable); - EXPECT_VERIFIES_INLINE(&env_v_v, WASM_BLOCK(2, WASM_UNREACHABLE, WASM_ZERO)); - EXPECT_VERIFIES_INLINE(&env_v_v, WASM_BLOCK(2, WASM_BR(0), WASM_ZERO)); - EXPECT_VERIFIES_INLINE(&env_v_v, WASM_LOOP(2, WASM_UNREACHABLE, WASM_ZERO)); - EXPECT_VERIFIES_INLINE(&env_v_v, WASM_LOOP(2, WASM_BR(0), WASM_ZERO)); + EXPECT_VERIFIES_INLINE(sigs.v_v(), kExprUnreachable); + EXPECT_VERIFIES_INLINE(sigs.v_v(), kExprUnreachable, kExprUnreachable); + EXPECT_VERIFIES_INLINE(sigs.v_v(), + WASM_BLOCK(2, WASM_UNREACHABLE, WASM_ZERO)); + EXPECT_VERIFIES_INLINE(sigs.v_v(), WASM_BLOCK(2, WASM_BR(0), WASM_ZERO)); + EXPECT_VERIFIES_INLINE(sigs.v_v(), WASM_LOOP(2, WASM_UNREACHABLE, WASM_ZERO)); + EXPECT_VERIFIES_INLINE(sigs.v_v(), WASM_LOOP(2, WASM_BR(0), WASM_ZERO)); } TEST_F(AstDecoderTest, Codeiness) { @@ -668,7 +621,7 @@ TEST_F(AstDecoderTest, ExprIf_off_end) { static const byte kCode[] = {kExprIf, kExprGetLocal, 0, kExprGetLocal, 0, kExprGetLocal, 0}; for (size_t len = 1; len < arraysize(kCode); len++) { - Verify(kError, &env_i_i, kCode, kCode + len); + Verify(kError, sigs.i_i(), kCode, kCode + len); } } @@ -677,48 +630,46 @@ TEST_F(AstDecoderTest, ExprIf_type) { // float|double ? 1 : 2 static const byte kCode[] = {kExprIfElse, kExprGetLocal, 0, kExprI8Const, 1, kExprI8Const, 2}; - EXPECT_FAILURE(&env_i_f, kCode); - EXPECT_FAILURE(&env_i_d, kCode); + EXPECT_FAILURE(sigs.i_f(), kCode); + EXPECT_FAILURE(sigs.i_d(), kCode); } { // 1 ? float|double : 2 static const byte kCode[] = {kExprIfElse, kExprI8Const, 1, kExprGetLocal, 0, kExprI8Const, 2}; - EXPECT_FAILURE(&env_i_f, kCode); - EXPECT_FAILURE(&env_i_d, kCode); + EXPECT_FAILURE(sigs.i_f(), kCode); + EXPECT_FAILURE(sigs.i_d(), kCode); } { // stmt ? 0 : 1 static const byte kCode[] = {kExprIfElse, kExprNop, kExprI8Const, 0, kExprI8Const, 1}; - EXPECT_FAILURE(&env_i_i, kCode); + EXPECT_FAILURE(sigs.i_i(), kCode); } { // 0 ? stmt : 1 static const byte kCode[] = {kExprIfElse, kExprI8Const, 0, kExprNop, kExprI8Const, 1}; - EXPECT_FAILURE(&env_i_i, kCode); + EXPECT_FAILURE(sigs.i_i(), kCode); } { // 0 ? 1 : stmt static const byte kCode[] = {kExprIfElse, kExprI8Const, 0, kExprI8Const, 1, 0, kExprBlock}; - EXPECT_FAILURE(&env_i_i, kCode); + EXPECT_FAILURE(sigs.i_i(), kCode); } } TEST_F(AstDecoderTest, Int64Local_param) { - EXPECT_VERIFIES(&env_l_l, kCodeGetLocal0); + EXPECT_VERIFIES(sigs.l_l(), kCodeGetLocal0); } TEST_F(AstDecoderTest, Int64Locals) { for (byte i = 1; i < 8; i++) { - FunctionEnv env; - init_env(&env, sigs.l_v()); - env.AddLocals(kAstI64, i); + AddLocals(kAstI64, 1); for (byte j = 0; j < i; j++) { byte code[] = {kExprGetLocal, j}; - EXPECT_VERIFIES(&env, code); + EXPECT_VERIFIES(sigs.l_v(), code); } } } @@ -792,19 +743,19 @@ TEST_F(AstDecoderTest, MacrosStmt) { } TEST_F(AstDecoderTest, MacrosBreak) { - EXPECT_VERIFIES_INLINE(&env_v_v, WASM_LOOP(1, WASM_BREAK(0))); + EXPECT_VERIFIES_INLINE(sigs.v_v(), WASM_LOOP(1, WASM_BREAK(0))); - EXPECT_VERIFIES_INLINE(&env_i_i, WASM_LOOP(1, WASM_BREAKV(0, WASM_ZERO))); - EXPECT_VERIFIES_INLINE(&env_l_l, + EXPECT_VERIFIES_INLINE(sigs.i_i(), WASM_LOOP(1, WASM_BREAKV(0, WASM_ZERO))); + EXPECT_VERIFIES_INLINE(sigs.l_l(), WASM_LOOP(1, WASM_BREAKV(0, WASM_I64V_1(0)))); - EXPECT_VERIFIES_INLINE(&env_f_ff, + EXPECT_VERIFIES_INLINE(sigs.f_ff(), WASM_LOOP(1, WASM_BREAKV(0, WASM_F32(0.0)))); - EXPECT_VERIFIES_INLINE(&env_d_dd, + EXPECT_VERIFIES_INLINE(sigs.d_dd(), WASM_LOOP(1, WASM_BREAKV(0, WASM_F64(0.0)))); } TEST_F(AstDecoderTest, MacrosContinue) { - EXPECT_VERIFIES_INLINE(&env_v_v, WASM_LOOP(1, WASM_CONTINUE(0))); + EXPECT_VERIFIES_INLINE(sigs.v_v(), WASM_LOOP(1, WASM_CONTINUE(0))); } TEST_F(AstDecoderTest, MacrosVariadic) { @@ -825,33 +776,25 @@ TEST_F(AstDecoderTest, MacrosNestedBlocks) { TEST_F(AstDecoderTest, MultipleReturn) { static LocalType kIntTypes5[] = {kAstI32, kAstI32, kAstI32, kAstI32, kAstI32}; FunctionSig sig_ii_v(2, 0, kIntTypes5); - FunctionEnv env_ii_v; - init_env(&env_ii_v, &sig_ii_v); - EXPECT_VERIFIES_INLINE(&env_ii_v, WASM_RETURN(WASM_ZERO, WASM_ONE)); - EXPECT_FAILURE_INLINE(&env_ii_v, WASM_RETURN(WASM_ZERO)); + EXPECT_VERIFIES_INLINE(&sig_ii_v, WASM_RETURN(WASM_ZERO, WASM_ONE)); + EXPECT_FAILURE_INLINE(&sig_ii_v, WASM_RETURN(WASM_ZERO)); FunctionSig sig_iii_v(3, 0, kIntTypes5); - FunctionEnv env_iii_v; - init_env(&env_iii_v, &sig_iii_v); - EXPECT_VERIFIES_INLINE(&env_iii_v, + EXPECT_VERIFIES_INLINE(&sig_iii_v, WASM_RETURN(WASM_ZERO, WASM_ONE, WASM_I8(44))); - EXPECT_FAILURE_INLINE(&env_iii_v, WASM_RETURN(WASM_ZERO, WASM_ONE)); + EXPECT_FAILURE_INLINE(&sig_iii_v, WASM_RETURN(WASM_ZERO, WASM_ONE)); } TEST_F(AstDecoderTest, MultipleReturn_fallthru) { static LocalType kIntTypes5[] = {kAstI32, kAstI32, kAstI32, kAstI32, kAstI32}; FunctionSig sig_ii_v(2, 0, kIntTypes5); - FunctionEnv env_ii_v; - init_env(&env_ii_v, &sig_ii_v); - EXPECT_VERIFIES_INLINE(&env_ii_v, WASM_ZERO, WASM_ONE); - EXPECT_FAILURE_INLINE(&env_ii_v, WASM_ZERO); + EXPECT_VERIFIES_INLINE(&sig_ii_v, WASM_ZERO, WASM_ONE); + EXPECT_FAILURE_INLINE(&sig_ii_v, WASM_ZERO); FunctionSig sig_iii_v(3, 0, kIntTypes5); - FunctionEnv env_iii_v; - init_env(&env_iii_v, &sig_iii_v); - EXPECT_VERIFIES_INLINE(&env_iii_v, WASM_ZERO, WASM_ONE, WASM_I8(44)); - EXPECT_FAILURE_INLINE(&env_iii_v, WASM_ZERO, WASM_ONE); + EXPECT_VERIFIES_INLINE(&sig_iii_v, WASM_ZERO, WASM_ONE, WASM_I8(44)); + EXPECT_FAILURE_INLINE(&sig_iii_v, WASM_ZERO, WASM_ONE); } TEST_F(AstDecoderTest, MacrosInt32) { @@ -885,13 +828,8 @@ TEST_F(AstDecoderTest, MacrosInt32) { } TEST_F(AstDecoderTest, MacrosInt64) { - FunctionEnv env_i_ll; - FunctionEnv env_l_ll; - init_env(&env_i_ll, sigs.i_ll()); - init_env(&env_l_ll, sigs.l_ll()); - -#define VERIFY_L_LL(...) EXPECT_VERIFIES_INLINE(&env_l_ll, __VA_ARGS__) -#define VERIFY_I_LL(...) EXPECT_VERIFIES_INLINE(&env_i_ll, __VA_ARGS__) +#define VERIFY_L_LL(...) EXPECT_VERIFIES_INLINE(sigs.l_ll(), __VA_ARGS__) +#define VERIFY_I_LL(...) EXPECT_VERIFIES_INLINE(sigs.i_ll(), __VA_ARGS__) VERIFY_L_LL(WASM_I64_ADD(WASM_GET_LOCAL(0), WASM_I64V_1(12))); VERIFY_L_LL(WASM_I64_SUB(WASM_GET_LOCAL(0), WASM_I64V_1(13))); @@ -943,21 +881,21 @@ TEST_F(AstDecoderTest, AllSimpleExpressions) { TEST_F(AstDecoderTest, MemorySize) { byte code[] = {kExprMemorySize}; - EXPECT_VERIFIES(&env_i_i, code); - EXPECT_FAILURE(&env_f_ff, code); + EXPECT_VERIFIES(sigs.i_i(), code); + EXPECT_FAILURE(sigs.f_ff(), code); } TEST_F(AstDecoderTest, GrowMemory) { byte code[] = {kExprGrowMemory, kExprGetLocal, 0}; - EXPECT_VERIFIES(&env_i_i, code); - EXPECT_FAILURE(&env_i_d, code); + EXPECT_VERIFIES(sigs.i_i(), code); + EXPECT_FAILURE(sigs.i_d(), code); } TEST_F(AstDecoderTest, LoadMemOffset) { for (int offset = 0; offset < 128; offset += 7) { byte code[] = {kExprI32LoadMem, WasmOpcodes::LoadStoreAccessOf(true), static_cast(offset), kExprI8Const, 0}; - EXPECT_VERIFIES(&env_i_i, code); + EXPECT_VERIFIES(sigs.i_i(), code); } } @@ -970,7 +908,7 @@ TEST_F(AstDecoderTest, StoreMemOffset) { 0, kExprI8Const, 0}; - EXPECT_VERIFIES(&env_i_i, code); + EXPECT_VERIFIES(sigs.i_i(), code); } } @@ -999,10 +937,10 @@ TEST_F(AstDecoderTest, LoadMemOffset_varint) { kExprI8Const, 0}; - EXPECT_VERIFIES(&env_i_i, code1); - EXPECT_VERIFIES(&env_i_i, code2); - EXPECT_VERIFIES(&env_i_i, code3); - EXPECT_VERIFIES(&env_i_i, code4); + EXPECT_VERIFIES(sigs.i_i(), code1); + EXPECT_VERIFIES(sigs.i_i(), code2); + EXPECT_VERIFIES(sigs.i_i(), code3); + EXPECT_VERIFIES(sigs.i_i(), code4); } TEST_F(AstDecoderTest, StoreMemOffset_varint) { @@ -1041,10 +979,10 @@ TEST_F(AstDecoderTest, StoreMemOffset_varint) { kExprI8Const, 0}; - EXPECT_VERIFIES(&env_i_i, code1); - EXPECT_VERIFIES(&env_i_i, code2); - EXPECT_VERIFIES(&env_i_i, code3); - EXPECT_VERIFIES(&env_i_i, code4); + EXPECT_VERIFIES(sigs.i_i(), code1); + EXPECT_VERIFIES(sigs.i_i(), code2); + EXPECT_VERIFIES(sigs.i_i(), code3); + EXPECT_VERIFIES(sigs.i_i(), code4); } TEST_F(AstDecoderTest, AllLoadMemCombinations) { @@ -1055,13 +993,11 @@ TEST_F(AstDecoderTest, AllLoadMemCombinations) { byte code[] = { static_cast(WasmOpcodes::LoadStoreOpcodeOf(mem_type, false)), WasmOpcodes::LoadStoreAccessOf(false), kExprI8Const, 0}; - FunctionEnv env; FunctionSig sig(1, 0, &local_type); - init_env(&env, &sig); if (local_type == WasmOpcodes::LocalTypeFor(mem_type)) { - EXPECT_VERIFIES(&env, code); + EXPECT_VERIFIES(&sig, code); } else { - EXPECT_FAILURE(&env, code); + EXPECT_FAILURE(&sig, code); } } } @@ -1079,13 +1015,11 @@ TEST_F(AstDecoderTest, AllStoreMemCombinations) { 0, kExprGetLocal, 0}; - FunctionEnv env; FunctionSig sig(0, 1, &local_type); - init_env(&env, &sig); if (local_type == WasmOpcodes::LocalTypeFor(mem_type)) { - EXPECT_VERIFIES(&env, code); + EXPECT_VERIFIES(&sig, code); } else { - EXPECT_FAILURE(&env, code); + EXPECT_FAILURE(&sig, code); } } } @@ -1129,187 +1063,183 @@ class TestModuleEnv : public ModuleEnv { } // namespace TEST_F(AstDecoderTest, SimpleCalls) { - FunctionEnv* env = &env_i_i; + FunctionSig* sig = sigs.i_i(); TestModuleEnv module_env; - env->module = &module_env; + module = &module_env; module_env.AddFunction(sigs.i_v()); module_env.AddFunction(sigs.i_i()); module_env.AddFunction(sigs.i_ii()); - EXPECT_VERIFIES_INLINE(env, WASM_CALL_FUNCTION(0)); - EXPECT_VERIFIES_INLINE(env, WASM_CALL_FUNCTION(1, WASM_I8(27))); - EXPECT_VERIFIES_INLINE(env, WASM_CALL_FUNCTION(2, WASM_I8(37), WASM_I8(77))); + EXPECT_VERIFIES_INLINE(sig, WASM_CALL_FUNCTION(0)); + EXPECT_VERIFIES_INLINE(sig, WASM_CALL_FUNCTION(1, WASM_I8(27))); + EXPECT_VERIFIES_INLINE(sig, WASM_CALL_FUNCTION(2, WASM_I8(37), WASM_I8(77))); } TEST_F(AstDecoderTest, CallsWithTooFewArguments) { - FunctionEnv* env = &env_i_i; + FunctionSig* sig = sigs.i_i(); TestModuleEnv module_env; - env->module = &module_env; + module = &module_env; module_env.AddFunction(sigs.i_i()); module_env.AddFunction(sigs.i_ii()); module_env.AddFunction(sigs.f_ff()); - EXPECT_FAILURE_INLINE(env, WASM_CALL_FUNCTION0(0)); - EXPECT_FAILURE_INLINE(env, WASM_CALL_FUNCTION(1, WASM_ZERO)); - EXPECT_FAILURE_INLINE(env, WASM_CALL_FUNCTION(2, WASM_GET_LOCAL(0))); + EXPECT_FAILURE_INLINE(sig, WASM_CALL_FUNCTION0(0)); + EXPECT_FAILURE_INLINE(sig, WASM_CALL_FUNCTION(1, WASM_ZERO)); + EXPECT_FAILURE_INLINE(sig, WASM_CALL_FUNCTION(2, WASM_GET_LOCAL(0))); } TEST_F(AstDecoderTest, CallsWithSpilloverArgs) { static LocalType a_i_ff[] = {kAstI32, kAstF32, kAstF32}; FunctionSig sig_i_ff(1, 2, a_i_ff); - FunctionEnv env_i_ff; - init_env(&env_i_ff, &sig_i_ff); TestModuleEnv module_env; - env_i_ff.module = &module_env; - env_i_i.module = &module_env; - env_f_ff.module = &module_env; + module = &module_env; module_env.AddFunction(&sig_i_ff); - EXPECT_VERIFIES_INLINE(&env_i_i, + EXPECT_VERIFIES_INLINE(sigs.i_i(), WASM_CALL_FUNCTION(0, WASM_F32(0.1), WASM_F32(0.1))); - EXPECT_VERIFIES_INLINE(&env_i_ff, + EXPECT_VERIFIES_INLINE(sigs.i_ff(), WASM_CALL_FUNCTION(0, WASM_F32(0.1), WASM_F32(0.1))); - EXPECT_FAILURE_INLINE(&env_f_ff, + EXPECT_FAILURE_INLINE(sigs.f_ff(), WASM_CALL_FUNCTION(0, WASM_F32(0.1), WASM_F32(0.1))); EXPECT_FAILURE_INLINE( - &env_i_i, + sigs.i_i(), WASM_CALL_FUNCTION(0, WASM_F32(0.1), WASM_F32(0.1), WASM_F32(0.2))); EXPECT_VERIFIES_INLINE( - &env_f_ff, + sigs.f_ff(), WASM_CALL_FUNCTION(0, WASM_F32(0.1), WASM_F32(0.1), WASM_F32(11))); } TEST_F(AstDecoderTest, CallsWithMismatchedSigs2) { - FunctionEnv* env = &env_i_i; + FunctionSig* sig = sigs.i_i(); TestModuleEnv module_env; - env->module = &module_env; + module = &module_env; module_env.AddFunction(sigs.i_i()); - EXPECT_FAILURE_INLINE(env, WASM_CALL_FUNCTION(0, WASM_I64V_1(17))); - EXPECT_FAILURE_INLINE(env, WASM_CALL_FUNCTION(0, WASM_F32(17.1))); - EXPECT_FAILURE_INLINE(env, WASM_CALL_FUNCTION(0, WASM_F64(17.1))); + EXPECT_FAILURE_INLINE(sig, WASM_CALL_FUNCTION(0, WASM_I64V_1(17))); + EXPECT_FAILURE_INLINE(sig, WASM_CALL_FUNCTION(0, WASM_F32(17.1))); + EXPECT_FAILURE_INLINE(sig, WASM_CALL_FUNCTION(0, WASM_F64(17.1))); } TEST_F(AstDecoderTest, CallsWithMismatchedSigs3) { - FunctionEnv* env = &env_i_i; + FunctionSig* sig = sigs.i_i(); TestModuleEnv module_env; - env->module = &module_env; + module = &module_env; module_env.AddFunction(sigs.i_f()); - EXPECT_FAILURE_INLINE(env, WASM_CALL_FUNCTION(0, WASM_I8(17))); - EXPECT_FAILURE_INLINE(env, WASM_CALL_FUNCTION(0, WASM_I64V_1(27))); - EXPECT_FAILURE_INLINE(env, WASM_CALL_FUNCTION(0, WASM_F64(37.2))); + EXPECT_FAILURE_INLINE(sig, WASM_CALL_FUNCTION(0, WASM_I8(17))); + EXPECT_FAILURE_INLINE(sig, WASM_CALL_FUNCTION(0, WASM_I64V_1(27))); + EXPECT_FAILURE_INLINE(sig, WASM_CALL_FUNCTION(0, WASM_F64(37.2))); module_env.AddFunction(sigs.i_d()); - EXPECT_FAILURE_INLINE(env, WASM_CALL_FUNCTION(1, WASM_I8(16))); - EXPECT_FAILURE_INLINE(env, WASM_CALL_FUNCTION(1, WASM_I64V_1(16))); - EXPECT_FAILURE_INLINE(env, WASM_CALL_FUNCTION(1, WASM_F32(17.6))); + EXPECT_FAILURE_INLINE(sig, WASM_CALL_FUNCTION(1, WASM_I8(16))); + EXPECT_FAILURE_INLINE(sig, WASM_CALL_FUNCTION(1, WASM_I64V_1(16))); + EXPECT_FAILURE_INLINE(sig, WASM_CALL_FUNCTION(1, WASM_F32(17.6))); } TEST_F(AstDecoderTest, SimpleIndirectCalls) { - FunctionEnv* env = &env_i_i; + FunctionSig* sig = sigs.i_i(); TestModuleEnv module_env; - env->module = &module_env; + module = &module_env; byte f0 = module_env.AddSignature(sigs.i_v()); byte f1 = module_env.AddSignature(sigs.i_i()); byte f2 = module_env.AddSignature(sigs.i_ii()); - EXPECT_VERIFIES_INLINE(env, WASM_CALL_INDIRECT0(f0, WASM_ZERO)); - EXPECT_VERIFIES_INLINE(env, WASM_CALL_INDIRECT(f1, WASM_ZERO, WASM_I8(22))); + EXPECT_VERIFIES_INLINE(sig, WASM_CALL_INDIRECT0(f0, WASM_ZERO)); + EXPECT_VERIFIES_INLINE(sig, WASM_CALL_INDIRECT(f1, WASM_ZERO, WASM_I8(22))); EXPECT_VERIFIES_INLINE( - env, WASM_CALL_INDIRECT(f2, WASM_ZERO, WASM_I8(32), WASM_I8(72))); + sig, WASM_CALL_INDIRECT(f2, WASM_ZERO, WASM_I8(32), WASM_I8(72))); } TEST_F(AstDecoderTest, IndirectCallsOutOfBounds) { - FunctionEnv* env = &env_i_i; + FunctionSig* sig = sigs.i_i(); TestModuleEnv module_env; - env->module = &module_env; + module = &module_env; - EXPECT_FAILURE_INLINE(env, WASM_CALL_INDIRECT0(0, WASM_ZERO)); + EXPECT_FAILURE_INLINE(sig, WASM_CALL_INDIRECT0(0, WASM_ZERO)); module_env.AddSignature(sigs.i_v()); - EXPECT_VERIFIES_INLINE(env, WASM_CALL_INDIRECT0(0, WASM_ZERO)); + EXPECT_VERIFIES_INLINE(sig, WASM_CALL_INDIRECT0(0, WASM_ZERO)); - EXPECT_FAILURE_INLINE(env, WASM_CALL_INDIRECT(1, WASM_ZERO, WASM_I8(22))); + EXPECT_FAILURE_INLINE(sig, WASM_CALL_INDIRECT(1, WASM_ZERO, WASM_I8(22))); module_env.AddSignature(sigs.i_i()); - EXPECT_VERIFIES_INLINE(env, WASM_CALL_INDIRECT(1, WASM_ZERO, WASM_I8(27))); + EXPECT_VERIFIES_INLINE(sig, WASM_CALL_INDIRECT(1, WASM_ZERO, WASM_I8(27))); - EXPECT_FAILURE_INLINE(env, WASM_CALL_INDIRECT(2, WASM_ZERO, WASM_I8(27))); + EXPECT_FAILURE_INLINE(sig, WASM_CALL_INDIRECT(2, WASM_ZERO, WASM_I8(27))); } TEST_F(AstDecoderTest, IndirectCallsWithMismatchedSigs3) { - FunctionEnv* env = &env_i_i; + FunctionSig* sig = sigs.i_i(); TestModuleEnv module_env; - env->module = &module_env; + module = &module_env; byte f0 = module_env.AddFunction(sigs.i_f()); - EXPECT_FAILURE_INLINE(env, WASM_CALL_INDIRECT(f0, WASM_ZERO, WASM_I8(17))); - EXPECT_FAILURE_INLINE(env, + EXPECT_FAILURE_INLINE(sig, WASM_CALL_INDIRECT(f0, WASM_ZERO, WASM_I8(17))); + EXPECT_FAILURE_INLINE(sig, WASM_CALL_INDIRECT(f0, WASM_ZERO, WASM_I64V_1(27))); - EXPECT_FAILURE_INLINE(env, WASM_CALL_INDIRECT(f0, WASM_ZERO, WASM_F64(37.2))); + EXPECT_FAILURE_INLINE(sig, WASM_CALL_INDIRECT(f0, WASM_ZERO, WASM_F64(37.2))); - EXPECT_FAILURE_INLINE(env, WASM_CALL_INDIRECT0(f0, WASM_I8(17))); - EXPECT_FAILURE_INLINE(env, WASM_CALL_INDIRECT0(f0, WASM_I64V_1(27))); - EXPECT_FAILURE_INLINE(env, WASM_CALL_INDIRECT0(f0, WASM_F64(37.2))); + EXPECT_FAILURE_INLINE(sig, WASM_CALL_INDIRECT0(f0, WASM_I8(17))); + EXPECT_FAILURE_INLINE(sig, WASM_CALL_INDIRECT0(f0, WASM_I64V_1(27))); + EXPECT_FAILURE_INLINE(sig, WASM_CALL_INDIRECT0(f0, WASM_F64(37.2))); byte f1 = module_env.AddFunction(sigs.i_d()); - EXPECT_FAILURE_INLINE(env, WASM_CALL_INDIRECT(f1, WASM_ZERO, WASM_I8(16))); - EXPECT_FAILURE_INLINE(env, + EXPECT_FAILURE_INLINE(sig, WASM_CALL_INDIRECT(f1, WASM_ZERO, WASM_I8(16))); + EXPECT_FAILURE_INLINE(sig, WASM_CALL_INDIRECT(f1, WASM_ZERO, WASM_I64V_1(16))); - EXPECT_FAILURE_INLINE(env, WASM_CALL_INDIRECT(f1, WASM_ZERO, WASM_F32(17.6))); + EXPECT_FAILURE_INLINE(sig, WASM_CALL_INDIRECT(f1, WASM_ZERO, WASM_F32(17.6))); } TEST_F(AstDecoderTest, SimpleImportCalls) { - FunctionEnv* env = &env_i_i; + FunctionSig* sig = sigs.i_i(); TestModuleEnv module_env; - env->module = &module_env; + module = &module_env; byte f0 = module_env.AddImport(sigs.i_v()); byte f1 = module_env.AddImport(sigs.i_i()); byte f2 = module_env.AddImport(sigs.i_ii()); - EXPECT_VERIFIES_INLINE(env, WASM_CALL_IMPORT0(f0)); - EXPECT_VERIFIES_INLINE(env, WASM_CALL_IMPORT(f1, WASM_I8(22))); - EXPECT_VERIFIES_INLINE(env, WASM_CALL_IMPORT(f2, WASM_I8(32), WASM_I8(72))); + EXPECT_VERIFIES_INLINE(sig, WASM_CALL_IMPORT0(f0)); + EXPECT_VERIFIES_INLINE(sig, WASM_CALL_IMPORT(f1, WASM_I8(22))); + EXPECT_VERIFIES_INLINE(sig, WASM_CALL_IMPORT(f2, WASM_I8(32), WASM_I8(72))); } TEST_F(AstDecoderTest, ImportCallsWithMismatchedSigs3) { - FunctionEnv* env = &env_i_i; + FunctionSig* sig = sigs.i_i(); TestModuleEnv module_env; - env->module = &module_env; + module = &module_env; byte f0 = module_env.AddImport(sigs.i_f()); - EXPECT_FAILURE_INLINE(env, WASM_CALL_IMPORT0(f0)); - EXPECT_FAILURE_INLINE(env, WASM_CALL_IMPORT(f0, WASM_I8(17))); - EXPECT_FAILURE_INLINE(env, WASM_CALL_IMPORT(f0, WASM_I64V_1(27))); - EXPECT_FAILURE_INLINE(env, WASM_CALL_IMPORT(f0, WASM_F64(37.2))); + EXPECT_FAILURE_INLINE(sig, WASM_CALL_IMPORT0(f0)); + EXPECT_FAILURE_INLINE(sig, WASM_CALL_IMPORT(f0, WASM_I8(17))); + EXPECT_FAILURE_INLINE(sig, WASM_CALL_IMPORT(f0, WASM_I64V_1(27))); + EXPECT_FAILURE_INLINE(sig, WASM_CALL_IMPORT(f0, WASM_F64(37.2))); byte f1 = module_env.AddImport(sigs.i_d()); - EXPECT_FAILURE_INLINE(env, WASM_CALL_IMPORT0(f1)); - EXPECT_FAILURE_INLINE(env, WASM_CALL_IMPORT(f1, WASM_I8(16))); - EXPECT_FAILURE_INLINE(env, WASM_CALL_IMPORT(f1, WASM_I64V_1(16))); - EXPECT_FAILURE_INLINE(env, WASM_CALL_IMPORT(f1, WASM_F32(17.6))); + EXPECT_FAILURE_INLINE(sig, WASM_CALL_IMPORT0(f1)); + EXPECT_FAILURE_INLINE(sig, WASM_CALL_IMPORT(f1, WASM_I8(16))); + EXPECT_FAILURE_INLINE(sig, WASM_CALL_IMPORT(f1, WASM_I64V_1(16))); + EXPECT_FAILURE_INLINE(sig, WASM_CALL_IMPORT(f1, WASM_F32(17.6))); } TEST_F(AstDecoderTest, Int32Globals) { - FunctionEnv* env = &env_i_i; + FunctionSig* sig = sigs.i_i(); TestModuleEnv module_env; - env->module = &module_env; + module = &module_env; module_env.AddGlobal(MachineType::Int8()); module_env.AddGlobal(MachineType::Uint8()); @@ -1318,81 +1248,77 @@ TEST_F(AstDecoderTest, Int32Globals) { module_env.AddGlobal(MachineType::Int32()); module_env.AddGlobal(MachineType::Uint32()); - EXPECT_VERIFIES_INLINE(env, WASM_LOAD_GLOBAL(0)); - EXPECT_VERIFIES_INLINE(env, WASM_LOAD_GLOBAL(1)); - EXPECT_VERIFIES_INLINE(env, WASM_LOAD_GLOBAL(2)); - EXPECT_VERIFIES_INLINE(env, WASM_LOAD_GLOBAL(3)); - EXPECT_VERIFIES_INLINE(env, WASM_LOAD_GLOBAL(4)); - EXPECT_VERIFIES_INLINE(env, WASM_LOAD_GLOBAL(5)); + EXPECT_VERIFIES_INLINE(sig, WASM_LOAD_GLOBAL(0)); + EXPECT_VERIFIES_INLINE(sig, WASM_LOAD_GLOBAL(1)); + EXPECT_VERIFIES_INLINE(sig, WASM_LOAD_GLOBAL(2)); + EXPECT_VERIFIES_INLINE(sig, WASM_LOAD_GLOBAL(3)); + EXPECT_VERIFIES_INLINE(sig, WASM_LOAD_GLOBAL(4)); + EXPECT_VERIFIES_INLINE(sig, WASM_LOAD_GLOBAL(5)); - EXPECT_VERIFIES_INLINE(env, WASM_STORE_GLOBAL(0, WASM_GET_LOCAL(0))); - EXPECT_VERIFIES_INLINE(env, WASM_STORE_GLOBAL(1, WASM_GET_LOCAL(0))); - EXPECT_VERIFIES_INLINE(env, WASM_STORE_GLOBAL(2, WASM_GET_LOCAL(0))); - EXPECT_VERIFIES_INLINE(env, WASM_STORE_GLOBAL(3, WASM_GET_LOCAL(0))); - EXPECT_VERIFIES_INLINE(env, WASM_STORE_GLOBAL(4, WASM_GET_LOCAL(0))); - EXPECT_VERIFIES_INLINE(env, WASM_STORE_GLOBAL(5, WASM_GET_LOCAL(0))); + EXPECT_VERIFIES_INLINE(sig, WASM_STORE_GLOBAL(0, WASM_GET_LOCAL(0))); + EXPECT_VERIFIES_INLINE(sig, WASM_STORE_GLOBAL(1, WASM_GET_LOCAL(0))); + EXPECT_VERIFIES_INLINE(sig, WASM_STORE_GLOBAL(2, WASM_GET_LOCAL(0))); + EXPECT_VERIFIES_INLINE(sig, WASM_STORE_GLOBAL(3, WASM_GET_LOCAL(0))); + EXPECT_VERIFIES_INLINE(sig, WASM_STORE_GLOBAL(4, WASM_GET_LOCAL(0))); + EXPECT_VERIFIES_INLINE(sig, WASM_STORE_GLOBAL(5, WASM_GET_LOCAL(0))); } TEST_F(AstDecoderTest, Int32Globals_fail) { - FunctionEnv* env = &env_i_i; + FunctionSig* sig = sigs.i_i(); TestModuleEnv module_env; - env->module = &module_env; + module = &module_env; module_env.AddGlobal(MachineType::Int64()); module_env.AddGlobal(MachineType::Uint64()); module_env.AddGlobal(MachineType::Float32()); module_env.AddGlobal(MachineType::Float64()); - EXPECT_FAILURE_INLINE(env, WASM_LOAD_GLOBAL(0)); - EXPECT_FAILURE_INLINE(env, WASM_LOAD_GLOBAL(1)); - EXPECT_FAILURE_INLINE(env, WASM_LOAD_GLOBAL(2)); - EXPECT_FAILURE_INLINE(env, WASM_LOAD_GLOBAL(3)); + EXPECT_FAILURE_INLINE(sig, WASM_LOAD_GLOBAL(0)); + EXPECT_FAILURE_INLINE(sig, WASM_LOAD_GLOBAL(1)); + EXPECT_FAILURE_INLINE(sig, WASM_LOAD_GLOBAL(2)); + EXPECT_FAILURE_INLINE(sig, WASM_LOAD_GLOBAL(3)); - EXPECT_FAILURE_INLINE(env, WASM_STORE_GLOBAL(0, WASM_GET_LOCAL(0))); - EXPECT_FAILURE_INLINE(env, WASM_STORE_GLOBAL(1, WASM_GET_LOCAL(0))); - EXPECT_FAILURE_INLINE(env, WASM_STORE_GLOBAL(2, WASM_GET_LOCAL(0))); - EXPECT_FAILURE_INLINE(env, WASM_STORE_GLOBAL(3, WASM_GET_LOCAL(0))); + EXPECT_FAILURE_INLINE(sig, WASM_STORE_GLOBAL(0, WASM_GET_LOCAL(0))); + EXPECT_FAILURE_INLINE(sig, WASM_STORE_GLOBAL(1, WASM_GET_LOCAL(0))); + EXPECT_FAILURE_INLINE(sig, WASM_STORE_GLOBAL(2, WASM_GET_LOCAL(0))); + EXPECT_FAILURE_INLINE(sig, WASM_STORE_GLOBAL(3, WASM_GET_LOCAL(0))); } TEST_F(AstDecoderTest, Int64Globals) { - FunctionEnv* env = &env_l_l; + FunctionSig* sig = sigs.l_l(); TestModuleEnv module_env; - env->module = &module_env; + module = &module_env; module_env.AddGlobal(MachineType::Int64()); module_env.AddGlobal(MachineType::Uint64()); - EXPECT_VERIFIES_INLINE(env, WASM_LOAD_GLOBAL(0)); - EXPECT_VERIFIES_INLINE(env, WASM_LOAD_GLOBAL(1)); + EXPECT_VERIFIES_INLINE(sig, WASM_LOAD_GLOBAL(0)); + EXPECT_VERIFIES_INLINE(sig, WASM_LOAD_GLOBAL(1)); - EXPECT_VERIFIES_INLINE(env, WASM_STORE_GLOBAL(0, WASM_GET_LOCAL(0))); - EXPECT_VERIFIES_INLINE(env, WASM_STORE_GLOBAL(1, WASM_GET_LOCAL(0))); + EXPECT_VERIFIES_INLINE(sig, WASM_STORE_GLOBAL(0, WASM_GET_LOCAL(0))); + EXPECT_VERIFIES_INLINE(sig, WASM_STORE_GLOBAL(1, WASM_GET_LOCAL(0))); } TEST_F(AstDecoderTest, Float32Globals) { - FunctionEnv env_f_ff; - FunctionEnv* env = &env_f_ff; - init_env(env, sigs.f_ff()); + FunctionSig* sig = sigs.f_ff(); TestModuleEnv module_env; - env->module = &module_env; + module = &module_env; module_env.AddGlobal(MachineType::Float32()); - EXPECT_VERIFIES_INLINE(env, WASM_LOAD_GLOBAL(0)); - EXPECT_VERIFIES_INLINE(env, WASM_STORE_GLOBAL(0, WASM_GET_LOCAL(0))); + EXPECT_VERIFIES_INLINE(sig, WASM_LOAD_GLOBAL(0)); + EXPECT_VERIFIES_INLINE(sig, WASM_STORE_GLOBAL(0, WASM_GET_LOCAL(0))); } TEST_F(AstDecoderTest, Float64Globals) { - FunctionEnv env_d_dd; - FunctionEnv* env = &env_d_dd; - init_env(env, sigs.d_dd()); + FunctionSig* sig = sigs.d_dd(); TestModuleEnv module_env; - env->module = &module_env; + module = &module_env; module_env.AddGlobal(MachineType::Float64()); - EXPECT_VERIFIES_INLINE(env, WASM_LOAD_GLOBAL(0)); - EXPECT_VERIFIES_INLINE(env, WASM_STORE_GLOBAL(0, WASM_GET_LOCAL(0))); + EXPECT_VERIFIES_INLINE(sig, WASM_LOAD_GLOBAL(0)); + EXPECT_VERIFIES_INLINE(sig, WASM_STORE_GLOBAL(0, WASM_GET_LOCAL(0))); } TEST_F(AstDecoderTest, AllLoadGlobalCombinations) { @@ -1400,16 +1326,14 @@ TEST_F(AstDecoderTest, AllLoadGlobalCombinations) { LocalType local_type = kLocalTypes[i]; for (size_t j = 0; j < arraysize(machineTypes); j++) { MachineType mem_type = machineTypes[j]; - FunctionEnv env; FunctionSig sig(1, 0, &local_type); TestModuleEnv module_env; - init_env(&env, &sig); - env.module = &module_env; + module = &module_env; module_env.AddGlobal(mem_type); if (local_type == WasmOpcodes::LocalTypeFor(mem_type)) { - EXPECT_VERIFIES_INLINE(&env, WASM_LOAD_GLOBAL(0)); + EXPECT_VERIFIES_INLINE(&sig, WASM_LOAD_GLOBAL(0)); } else { - EXPECT_FAILURE_INLINE(&env, WASM_LOAD_GLOBAL(0)); + EXPECT_FAILURE_INLINE(&sig, WASM_LOAD_GLOBAL(0)); } } } @@ -1420,16 +1344,14 @@ TEST_F(AstDecoderTest, AllStoreGlobalCombinations) { LocalType local_type = kLocalTypes[i]; for (size_t j = 0; j < arraysize(machineTypes); j++) { MachineType mem_type = machineTypes[j]; - FunctionEnv env; FunctionSig sig(0, 1, &local_type); TestModuleEnv module_env; - init_env(&env, &sig); - env.module = &module_env; + module = &module_env; module_env.AddGlobal(mem_type); if (local_type == WasmOpcodes::LocalTypeFor(mem_type)) { - EXPECT_VERIFIES_INLINE(&env, WASM_STORE_GLOBAL(0, WASM_GET_LOCAL(0))); + EXPECT_VERIFIES_INLINE(&sig, WASM_STORE_GLOBAL(0, WASM_GET_LOCAL(0))); } else { - EXPECT_FAILURE_INLINE(&env, WASM_STORE_GLOBAL(0, WASM_GET_LOCAL(0))); + EXPECT_FAILURE_INLINE(&sig, WASM_STORE_GLOBAL(0, WASM_GET_LOCAL(0))); } } } @@ -1443,58 +1365,57 @@ TEST_F(AstDecoderTest, BreakNesting1) { WASM_SET_LOCAL(0, WASM_I8(1))), WASM_GET_LOCAL(0))}; if (i < 3) { - EXPECT_VERIFIES(&env_i_i, code); + EXPECT_VERIFIES(sigs.i_i(), code); } else { - EXPECT_FAILURE(&env_i_i, code); + EXPECT_FAILURE(sigs.i_i(), code); } } } TEST_F(AstDecoderTest, BreakNesting2) { - env_v_v.AddLocals(kAstI32, 1); + AddLocals(kAstI32, 1); for (int i = 0; i < 5; i++) { - // (block[2] (loop[2] (if (get p) break[N]) (set p 1)) (return p)) (11) - byte code[] = { - WASM_BLOCK(1, WASM_LOOP(2, WASM_IF(WASM_GET_LOCAL(0), WASM_BREAK(i)), - WASM_SET_LOCAL(0, WASM_I8(1)))), - WASM_I8(11)}; + // (block[2] (loop[2] (if 0 break[N]) (set p 1)) (return p)) (11) + byte code[] = {WASM_BLOCK(1, WASM_LOOP(2, WASM_IF(WASM_ZERO, WASM_BREAK(i)), + WASM_SET_LOCAL(0, WASM_I8(1)))), + WASM_I8(11)}; if (i < 2) { - EXPECT_VERIFIES(&env_v_v, code); + EXPECT_VERIFIES(sigs.v_v(), code); } else { - EXPECT_FAILURE(&env_v_v, code); + EXPECT_FAILURE(sigs.v_v(), code); } } } TEST_F(AstDecoderTest, BreakNesting3) { - env_v_v.AddLocals(kAstI32, 1); for (int i = 0; i < 5; i++) { - // (block[1] (loop[1] (block[1] (if (get p) break[N]) + // (block[1] (loop[1] (block[1] (if 0 break[N]) byte code[] = {WASM_BLOCK( - 1, WASM_LOOP( - 1, WASM_BLOCK(1, WASM_IF(WASM_GET_LOCAL(0), WASM_BREAK(i)))))}; + 1, WASM_LOOP(1, WASM_BLOCK(1, WASM_IF(WASM_ZERO, WASM_BREAK(i)))))}; if (i < 3) { - EXPECT_VERIFIES(&env_v_v, code); + EXPECT_VERIFIES(sigs.v_v(), code); } else { - EXPECT_FAILURE(&env_v_v, code); + EXPECT_FAILURE(sigs.v_v(), code); } } } TEST_F(AstDecoderTest, BreaksWithMultipleTypes) { EXPECT_FAILURE_INLINE( - &env_i_i, WASM_BLOCK(2, WASM_BRV_IF_ZERO(0, WASM_I8(7)), WASM_F32(7.7))); + sigs.i_i(), + WASM_BLOCK(2, WASM_BRV_IF_ZERO(0, WASM_I8(7)), WASM_F32(7.7))); - EXPECT_FAILURE_INLINE(&env_i_i, + EXPECT_FAILURE_INLINE(sigs.i_i(), WASM_BLOCK(2, WASM_BRV_IF_ZERO(0, WASM_I8(7)), WASM_BRV_IF_ZERO(0, WASM_F32(7.7)))); - EXPECT_FAILURE_INLINE(&env_i_i, + EXPECT_FAILURE_INLINE(sigs.i_i(), WASM_BLOCK(3, WASM_BRV_IF_ZERO(0, WASM_I8(8)), WASM_BRV_IF_ZERO(0, WASM_I8(0)), WASM_BRV_IF_ZERO(0, WASM_F32(7.7)))); - EXPECT_FAILURE_INLINE(&env_i_i, WASM_BLOCK(3, WASM_BRV_IF_ZERO(0, WASM_I8(9)), - WASM_BRV_IF_ZERO(0, WASM_F32(7.7)), - WASM_BRV_IF_ZERO(0, WASM_I8(11)))); + EXPECT_FAILURE_INLINE(sigs.i_i(), + WASM_BLOCK(3, WASM_BRV_IF_ZERO(0, WASM_I8(9)), + WASM_BRV_IF_ZERO(0, WASM_F32(7.7)), + WASM_BRV_IF_ZERO(0, WASM_I8(11)))); } TEST_F(AstDecoderTest, BreakNesting_6_levels) { @@ -1520,32 +1441,32 @@ TEST_F(AstDecoderTest, BreakNesting_6_levels) { } if (i < depth) { - EXPECT_VERIFIES(&env_v_v, code); + EXPECT_VERIFIES(sigs.v_v(), code); } else { - EXPECT_FAILURE(&env_v_v, code); + EXPECT_FAILURE(sigs.v_v(), code); } } } } TEST_F(AstDecoderTest, ExprBreak_TypeCheck) { - FunctionEnv* envs[] = {&env_i_i, &env_l_l, &env_f_ff, &env_d_dd}; - for (size_t i = 0; i < arraysize(envs); i++) { - FunctionEnv* env = envs[i]; + FunctionSig* sigarray[] = {sigs.i_i(), sigs.l_l(), sigs.f_ff(), sigs.d_dd()}; + for (size_t i = 0; i < arraysize(sigarray); i++) { + FunctionSig* sig = sigarray[i]; // unify X and X => OK EXPECT_VERIFIES_INLINE( - env, WASM_BLOCK(2, WASM_IF(WASM_ZERO, WASM_BRV(0, WASM_GET_LOCAL(0))), + sig, WASM_BLOCK(2, WASM_IF(WASM_ZERO, WASM_BRV(0, WASM_GET_LOCAL(0))), WASM_GET_LOCAL(0))); } // unify i32 and f32 => fail EXPECT_FAILURE_INLINE( - &env_i_i, + sigs.i_i(), WASM_BLOCK(2, WASM_IF(WASM_ZERO, WASM_BRV(0, WASM_ZERO)), WASM_F32(1.2))); // unify f64 and f64 => OK EXPECT_VERIFIES_INLINE( - &env_d_dd, + sigs.d_dd(), WASM_BLOCK(2, WASM_IF(WASM_ZERO, WASM_BRV(0, WASM_GET_LOCAL(0))), WASM_F64(1.2))); } @@ -1560,31 +1481,26 @@ TEST_F(AstDecoderTest, ExprBreak_TypeCheckAll) { for (size_t i = 0; i < arraysize(kLocalTypes); i++) { for (size_t j = 0; j < arraysize(kLocalTypes); j++) { - FunctionEnv env; LocalType storage[] = {kLocalTypes[i], kLocalTypes[i], kLocalTypes[j]}; FunctionSig sig(1, 2, storage); - init_env(&env, &sig); if (i == j) { - EXPECT_VERIFIES(&env, code1); - EXPECT_VERIFIES(&env, code2); + EXPECT_VERIFIES(&sig, code1); + EXPECT_VERIFIES(&sig, code2); } else { - EXPECT_FAILURE(&env, code1); - EXPECT_FAILURE(&env, code2); + EXPECT_FAILURE(&sig, code1); + EXPECT_FAILURE(&sig, code2); } } } } TEST_F(AstDecoderTest, ExprBr_Unify) { - FunctionEnv env; - for (int which = 0; which < 2; which++) { for (size_t i = 0; i < arraysize(kLocalTypes); i++) { LocalType type = kLocalTypes[i]; LocalType storage[] = {kAstI32, kAstI32, type}; FunctionSig sig(1, 2, storage); - init_env(&env, &sig); // (i32, X) -> i32 byte code1[] = { WASM_BLOCK(2, WASM_IF(WASM_ZERO, WASM_BRV(0, WASM_GET_LOCAL(which))), @@ -1595,37 +1511,34 @@ TEST_F(AstDecoderTest, ExprBr_Unify) { if (type == kAstI32) { - EXPECT_VERIFIES(&env, code1); - EXPECT_VERIFIES(&env, code2); + EXPECT_VERIFIES(&sig, code1); + EXPECT_VERIFIES(&sig, code2); } else { - EXPECT_FAILURE(&env, code1); - EXPECT_FAILURE(&env, code2); + EXPECT_FAILURE(&sig, code1); + EXPECT_FAILURE(&sig, code2); } } } } TEST_F(AstDecoderTest, ExprBrIf_cond_type) { - FunctionEnv env; byte code[] = { WASM_BLOCK(1, WASM_BRV_IF(0, WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)))}; for (size_t i = 0; i < arraysize(kLocalTypes); i++) { for (size_t j = 0; j < arraysize(kLocalTypes); j++) { LocalType types[] = {kLocalTypes[i], kLocalTypes[j]}; FunctionSig sig(0, 2, types); - init_env(&env, &sig); if (types[1] == kAstI32) { - EXPECT_VERIFIES(&env, code); + EXPECT_VERIFIES(&sig, code); } else { - EXPECT_FAILURE(&env, code); + EXPECT_FAILURE(&sig, code); } } } } TEST_F(AstDecoderTest, ExprBrIf_val_type) { - FunctionEnv env; byte code[] = { WASM_BLOCK(2, WASM_BRV_IF(0, WASM_GET_LOCAL(1), WASM_GET_LOCAL(2)), WASM_GET_LOCAL(0))}; @@ -1634,26 +1547,22 @@ TEST_F(AstDecoderTest, ExprBrIf_val_type) { LocalType types[] = {kLocalTypes[i], kLocalTypes[i], kLocalTypes[j], kAstI32}; FunctionSig sig(1, 3, types); - init_env(&env, &sig); if (i == j) { - EXPECT_VERIFIES(&env, code); + EXPECT_VERIFIES(&sig, code); } else { - EXPECT_FAILURE(&env, code); + EXPECT_FAILURE(&sig, code); } } } } TEST_F(AstDecoderTest, ExprBrIf_Unify) { - FunctionEnv env; - for (int which = 0; which < 2; which++) { for (size_t i = 0; i < arraysize(kLocalTypes); i++) { LocalType type = kLocalTypes[i]; LocalType storage[] = {kAstI32, kAstI32, type}; FunctionSig sig(1, 2, storage); - init_env(&env, &sig); // (i32, X) -> i32 byte code1[] = {WASM_BLOCK(2, WASM_BRV_IF_ZERO(0, WASM_GET_LOCAL(which)), WASM_GET_LOCAL(which ^ 1))}; @@ -1661,11 +1570,11 @@ TEST_F(AstDecoderTest, ExprBrIf_Unify) { WASM_GET_LOCAL(which ^ 1))}; if (type == kAstI32) { - EXPECT_VERIFIES(&env, code1); - EXPECT_VERIFIES(&env, code2); + EXPECT_VERIFIES(&sig, code1); + EXPECT_VERIFIES(&sig, code2); } else { - EXPECT_FAILURE(&env, code1); - EXPECT_FAILURE(&env, code2); + EXPECT_FAILURE(&sig, code1); + EXPECT_FAILURE(&sig, code2); } } } @@ -1673,54 +1582,54 @@ TEST_F(AstDecoderTest, ExprBrIf_Unify) { TEST_F(AstDecoderTest, BrTable0) { static byte code[] = {kExprBrTable, 0, 0}; - EXPECT_FAILURE(&env_v_v, code); + EXPECT_FAILURE(sigs.v_v(), code); } TEST_F(AstDecoderTest, BrTable0b) { - static byte code[] = {kExprBrTable, 0, 0, kExprI8Const, 11}; - EXPECT_FAILURE(&env_v_v, code); - EXPECT_FAILURE(&env_i_i, code); + static byte code[] = {kExprBrTable, 0, 0, kExprI32Const, 11}; + EXPECT_FAILURE(sigs.v_v(), code); + EXPECT_FAILURE(sigs.i_i(), code); } TEST_F(AstDecoderTest, BrTable0c) { - static byte code[] = {kExprBrTable, 0, 1, 0, 0, kExprI8Const, 11}; - EXPECT_FAILURE(&env_v_v, code); - EXPECT_FAILURE(&env_i_i, code); + static byte code[] = {kExprBrTable, 0, 1, 0, 0, kExprI32Const, 11}; + EXPECT_FAILURE(sigs.v_v(), code); + EXPECT_FAILURE(sigs.i_i(), code); } TEST_F(AstDecoderTest, BrTable1a) { static byte code[] = { WASM_BLOCK(1, WASM_BR_TABLE(WASM_I8(67), 0, BR_TARGET(0)))}; - EXPECT_VERIFIES(&env_v_v, code); + EXPECT_VERIFIES(sigs.v_v(), code); } TEST_F(AstDecoderTest, BrTable1b) { static byte code[] = { WASM_BLOCK(1, WASM_BR_TABLE(WASM_ZERO, 0, BR_TARGET(0)))}; - EXPECT_VERIFIES(&env_v_v, code); - EXPECT_FAILURE(&env_i_i, code); - EXPECT_FAILURE(&env_f_ff, code); - EXPECT_FAILURE(&env_d_dd, code); + EXPECT_VERIFIES(sigs.v_v(), code); + EXPECT_FAILURE(sigs.i_i(), code); + EXPECT_FAILURE(sigs.f_ff(), code); + EXPECT_FAILURE(sigs.d_dd(), code); } TEST_F(AstDecoderTest, BrTable2a) { static byte code[] = { WASM_BLOCK(1, WASM_BR_TABLE(WASM_I8(67), 1, BR_TARGET(0), BR_TARGET(0)))}; - EXPECT_VERIFIES(&env_v_v, code); + EXPECT_VERIFIES(sigs.v_v(), code); } TEST_F(AstDecoderTest, BrTable2b) { static byte code[] = {WASM_BLOCK( 1, WASM_BLOCK( 1, WASM_BR_TABLE(WASM_I8(67), 1, BR_TARGET(0), BR_TARGET(1))))}; - EXPECT_VERIFIES(&env_v_v, code); + EXPECT_VERIFIES(sigs.v_v(), code); } TEST_F(AstDecoderTest, BrTable_off_end) { static byte code[] = { WASM_BLOCK(1, WASM_BR_TABLE(WASM_GET_LOCAL(0), 0, BR_TARGET(0)))}; for (size_t len = 1; len < sizeof(code); len++) { - Verify(kError, &env_i_i, code, code + len); + Verify(kError, sigs.i_i(), code, code + len); } } @@ -1729,9 +1638,9 @@ TEST_F(AstDecoderTest, BrTable_invalid_br1) { byte code[] = { WASM_BLOCK(1, WASM_BR_TABLE(WASM_GET_LOCAL(0), 0, BR_TARGET(depth)))}; if (depth == 0) { - EXPECT_VERIFIES(&env_v_i, code); + EXPECT_VERIFIES(sigs.v_i(), code); } else { - EXPECT_FAILURE(&env_v_i, code); + EXPECT_FAILURE(sigs.v_i(), code); } } } @@ -1741,48 +1650,50 @@ TEST_F(AstDecoderTest, BrTable_invalid_br2) { byte code[] = { WASM_LOOP(1, WASM_BR_TABLE(WASM_GET_LOCAL(0), 0, BR_TARGET(depth)))}; if (depth <= 1) { - EXPECT_VERIFIES(&env_v_i, code); + EXPECT_VERIFIES(sigs.v_i(), code); } else { - EXPECT_FAILURE(&env_v_i, code); + EXPECT_FAILURE(sigs.v_i(), code); } } } TEST_F(AstDecoderTest, ExprBreakNesting1) { - EXPECT_VERIFIES_INLINE(&env_v_v, WASM_BLOCK(1, WASM_BRV(0, WASM_ZERO))); - EXPECT_VERIFIES_INLINE(&env_v_v, WASM_BLOCK(1, WASM_BR(0))); - EXPECT_VERIFIES_INLINE(&env_v_v, + EXPECT_VERIFIES_INLINE(sigs.v_v(), WASM_BLOCK(1, WASM_BRV(0, WASM_ZERO))); + EXPECT_VERIFIES_INLINE(sigs.v_v(), WASM_BLOCK(1, WASM_BR(0))); + EXPECT_VERIFIES_INLINE(sigs.v_v(), WASM_BLOCK(1, WASM_BRV_IF(0, WASM_ZERO, WASM_ZERO))); - EXPECT_VERIFIES_INLINE(&env_v_v, WASM_BLOCK(1, WASM_BR_IF(0, WASM_ZERO))); + EXPECT_VERIFIES_INLINE(sigs.v_v(), WASM_BLOCK(1, WASM_BR_IF(0, WASM_ZERO))); - EXPECT_VERIFIES_INLINE(&env_v_v, WASM_LOOP(1, WASM_BRV(0, WASM_ZERO))); - EXPECT_VERIFIES_INLINE(&env_v_v, WASM_LOOP(1, WASM_BR(0))); - EXPECT_VERIFIES_INLINE(&env_v_v, + EXPECT_VERIFIES_INLINE(sigs.v_v(), WASM_LOOP(1, WASM_BRV(0, WASM_ZERO))); + EXPECT_VERIFIES_INLINE(sigs.v_v(), WASM_LOOP(1, WASM_BR(0))); + EXPECT_VERIFIES_INLINE(sigs.v_v(), WASM_LOOP(1, WASM_BRV_IF(0, WASM_ZERO, WASM_ZERO))); - EXPECT_VERIFIES_INLINE(&env_v_v, WASM_LOOP(1, WASM_BR_IF(0, WASM_ZERO))); + EXPECT_VERIFIES_INLINE(sigs.v_v(), WASM_LOOP(1, WASM_BR_IF(0, WASM_ZERO))); - EXPECT_VERIFIES_INLINE(&env_v_v, WASM_LOOP(1, WASM_BRV(1, WASM_ZERO))); - EXPECT_VERIFIES_INLINE(&env_v_v, WASM_LOOP(1, WASM_BR(1))); + EXPECT_VERIFIES_INLINE(sigs.v_v(), WASM_LOOP(1, WASM_BRV(1, WASM_ZERO))); + EXPECT_VERIFIES_INLINE(sigs.v_v(), WASM_LOOP(1, WASM_BR(1))); } TEST_F(AstDecoderTest, Select) { EXPECT_VERIFIES_INLINE( - &env_i_i, WASM_SELECT(WASM_GET_LOCAL(0), WASM_GET_LOCAL(0), WASM_ZERO)); - EXPECT_VERIFIES_INLINE(&env_f_ff, + sigs.i_i(), WASM_SELECT(WASM_GET_LOCAL(0), WASM_GET_LOCAL(0), WASM_ZERO)); + EXPECT_VERIFIES_INLINE(sigs.f_ff(), WASM_SELECT(WASM_F32(0.0), WASM_F32(0.0), WASM_ZERO)); - EXPECT_VERIFIES_INLINE(&env_d_dd, + EXPECT_VERIFIES_INLINE(sigs.d_dd(), WASM_SELECT(WASM_F64(0.0), WASM_F64(0.0), WASM_ZERO)); EXPECT_VERIFIES_INLINE( - &env_l_l, WASM_SELECT(WASM_I64V_1(0), WASM_I64V_1(0), WASM_ZERO)); + sigs.l_l(), WASM_SELECT(WASM_I64V_1(0), WASM_I64V_1(0), WASM_ZERO)); } TEST_F(AstDecoderTest, Select_fail1) { - EXPECT_FAILURE_INLINE(&env_i_i, WASM_SELECT(WASM_F32(0.0), WASM_GET_LOCAL(0), - WASM_GET_LOCAL(0))); - EXPECT_FAILURE_INLINE(&env_i_i, WASM_SELECT(WASM_GET_LOCAL(0), WASM_F32(0.0), - WASM_GET_LOCAL(0))); EXPECT_FAILURE_INLINE( - &env_i_i, + sigs.i_i(), + WASM_SELECT(WASM_F32(0.0), WASM_GET_LOCAL(0), WASM_GET_LOCAL(0))); + EXPECT_FAILURE_INLINE( + sigs.i_i(), + WASM_SELECT(WASM_GET_LOCAL(0), WASM_F32(0.0), WASM_GET_LOCAL(0))); + EXPECT_FAILURE_INLINE( + sigs.i_i(), WASM_SELECT(WASM_GET_LOCAL(0), WASM_GET_LOCAL(0), WASM_F32(0.0))); } @@ -1793,36 +1704,37 @@ TEST_F(AstDecoderTest, Select_fail2) { LocalType types[] = {type, kAstI32, type}; FunctionSig sig(1, 2, types); - FunctionEnv env; - init_env(&env, &sig); EXPECT_VERIFIES_INLINE( - &env, + &sig, WASM_SELECT(WASM_GET_LOCAL(1), WASM_GET_LOCAL(1), WASM_GET_LOCAL(0))); EXPECT_FAILURE_INLINE( - &env, + &sig, WASM_SELECT(WASM_GET_LOCAL(1), WASM_GET_LOCAL(0), WASM_GET_LOCAL(0))); EXPECT_FAILURE_INLINE( - &env, + &sig, WASM_SELECT(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1), WASM_GET_LOCAL(0))); EXPECT_FAILURE_INLINE( - &env, + &sig, WASM_SELECT(WASM_GET_LOCAL(0), WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); } } TEST_F(AstDecoderTest, Select_TypeCheck) { - EXPECT_FAILURE_INLINE(&env_i_i, WASM_SELECT(WASM_F32(9.9), WASM_GET_LOCAL(0), - WASM_GET_LOCAL(0))); - - EXPECT_FAILURE_INLINE(&env_i_i, WASM_SELECT(WASM_GET_LOCAL(0), WASM_F64(0.25), - WASM_GET_LOCAL(0))); + EXPECT_FAILURE_INLINE( + sigs.i_i(), + WASM_SELECT(WASM_F32(9.9), WASM_GET_LOCAL(0), WASM_GET_LOCAL(0))); EXPECT_FAILURE_INLINE( - &env_i_i, WASM_SELECT(WASM_F32(9.9), WASM_GET_LOCAL(0), WASM_I64V_1(0))); + sigs.i_i(), + WASM_SELECT(WASM_GET_LOCAL(0), WASM_F64(0.25), WASM_GET_LOCAL(0))); + + EXPECT_FAILURE_INLINE( + sigs.i_i(), + WASM_SELECT(WASM_F32(9.9), WASM_GET_LOCAL(0), WASM_I64V_1(0))); } @@ -2045,16 +1957,18 @@ TEST_F(WasmOpcodeLengthTest, SimpleExpressions) { class WasmOpcodeArityTest : public TestWithZone { public: WasmOpcodeArityTest() : TestWithZone() {} + TestModuleEnv module; + TestSignatures sigs; }; -#define EXPECT_ARITY(expected, ...) \ - { \ - static const byte code[] = {__VA_ARGS__}; \ - EXPECT_EQ(expected, OpcodeArity(&env, code, code + sizeof(code))); \ +#define EXPECT_ARITY(expected, ...) \ + { \ + static const byte code[] = {__VA_ARGS__}; \ + EXPECT_EQ(expected, OpcodeArity(&module, sig, code, code + sizeof(code))); \ } TEST_F(WasmOpcodeArityTest, Control) { - FunctionEnv env; + FunctionSig* sig = sigs.v_v(); EXPECT_ARITY(0, kExprNop); EXPECT_ARITY(0, kExprBlock, 0); @@ -2077,19 +1991,16 @@ TEST_F(WasmOpcodeArityTest, Control) { EXPECT_ARITY(2, kExprBrIf); { - TestSignatures sigs; - FunctionEnv env; - AstDecoderTest::init_env(&env, sigs.v_v()); + sig = sigs.v_v(); EXPECT_ARITY(0, kExprReturn); - AstDecoderTest::init_env(&env, sigs.i_i()); + sig = sigs.i_i(); EXPECT_ARITY(1, kExprReturn); } } TEST_F(WasmOpcodeArityTest, Misc) { - FunctionEnv env; - + FunctionSig* sig = sigs.v_v(); EXPECT_ARITY(0, kExprI8Const); EXPECT_ARITY(0, kExprI32Const); EXPECT_ARITY(0, kExprF32Const); @@ -2103,8 +2014,6 @@ TEST_F(WasmOpcodeArityTest, Misc) { TEST_F(WasmOpcodeArityTest, Calls) { - TestSignatures sigs; - TestModuleEnv module; module.AddFunction(sigs.i_ii()); module.AddFunction(sigs.i_i()); @@ -2115,9 +2024,7 @@ TEST_F(WasmOpcodeArityTest, Calls) { module.AddImport(sigs.i_d()); { - FunctionEnv env; - AstDecoderTest::init_env(&env, sigs.i_ii()); - env.module = &module; + FunctionSig* sig = sigs.i_ii(); EXPECT_ARITY(2, kExprCallFunction, 0); EXPECT_ARITY(2, kExprCallImport, 0); @@ -2127,9 +2034,7 @@ TEST_F(WasmOpcodeArityTest, Calls) { } { - FunctionEnv env; - AstDecoderTest::init_env(&env, sigs.v_v()); - env.module = &module; + FunctionSig* sig = sigs.v_v(); EXPECT_ARITY(1, kExprCallFunction, 1); EXPECT_ARITY(1, kExprCallImport, 1); @@ -2141,8 +2046,7 @@ TEST_F(WasmOpcodeArityTest, Calls) { TEST_F(WasmOpcodeArityTest, LoadsAndStores) { - FunctionEnv env; - + FunctionSig* sig = sigs.v_v(); EXPECT_ARITY(1, kExprI32LoadMem8S); EXPECT_ARITY(1, kExprI32LoadMem8U); EXPECT_ARITY(1, kExprI32LoadMem16S); @@ -2172,16 +2076,14 @@ TEST_F(WasmOpcodeArityTest, LoadsAndStores) { TEST_F(WasmOpcodeArityTest, MiscMemExpressions) { - FunctionEnv env; - + FunctionSig* sig = sigs.v_v(); EXPECT_ARITY(0, kExprMemorySize); EXPECT_ARITY(1, kExprGrowMemory); } TEST_F(WasmOpcodeArityTest, SimpleExpressions) { - FunctionEnv env; - + FunctionSig* sig = sigs.v_v(); EXPECT_ARITY(2, kExprI32Add); EXPECT_ARITY(2, kExprI32Sub); EXPECT_ARITY(2, kExprI32Mul); @@ -2301,6 +2203,95 @@ TEST_F(WasmOpcodeArityTest, SimpleExpressions) { EXPECT_ARITY(1, kExprI32ReinterpretF32); EXPECT_ARITY(1, kExprI64ReinterpretF64); } + +typedef std::vector* LocalTypeMap; + +class LocalDeclDecoderTest : public TestWithZone { + public: + size_t ExpectRun(LocalTypeMap map, size_t pos, LocalType expected, + size_t count) { + for (size_t i = 0; i < count; i++) { + EXPECT_EQ(expected, map->at(pos++)); + } + return pos; + } +}; + +TEST_F(LocalDeclDecoderTest, NoLocals) { + static const byte data[] = {0}; + LocalTypeMap map = DecodeLocalDeclsForTesting(data, data + sizeof(data)); + EXPECT_EQ(0, map->size()); + if (map) delete map; +} + +TEST_F(LocalDeclDecoderTest, OneLocal) { + for (size_t i = 0; i < arraysize(kLocalTypes); i++) { + LocalType type = kLocalTypes[i]; + const byte data[] = { + 1, 1, static_cast(WasmOpcodes::LocalTypeCodeFor(type))}; + LocalTypeMap map = DecodeLocalDeclsForTesting(data, data + sizeof(data)); + EXPECT_EQ(1, map->size()); + EXPECT_EQ(type, map->at(0)); + if (map) delete map; + } +} + +TEST_F(LocalDeclDecoderTest, FiveLocals) { + for (size_t i = 0; i < arraysize(kLocalTypes); i++) { + LocalType type = kLocalTypes[i]; + const byte data[] = { + 1, 5, static_cast(WasmOpcodes::LocalTypeCodeFor(type))}; + LocalTypeMap map = DecodeLocalDeclsForTesting(data, data + sizeof(data)); + EXPECT_EQ(5, map->size()); + ExpectRun(map, 0, type, 5); + if (map) delete map; + } +} + +TEST_F(LocalDeclDecoderTest, MixedLocals) { + for (byte a = 0; a < 3; a++) { + for (byte b = 0; b < 3; b++) { + for (byte c = 0; c < 3; c++) { + for (byte d = 0; d < 3; d++) { + const byte data[] = {4, a, kLocalI32, b, kLocalI64, + c, kLocalF32, d, kLocalF64}; + LocalTypeMap map = + DecodeLocalDeclsForTesting(data, data + sizeof(data)); + EXPECT_EQ(a + b + c + d, map->size()); + + size_t pos = 0; + pos = ExpectRun(map, pos, kAstI32, a); + pos = ExpectRun(map, pos, kAstI64, b); + pos = ExpectRun(map, pos, kAstF32, c); + pos = ExpectRun(map, pos, kAstF64, d); + + if (map) delete map; + } + } + } + } +} + +TEST_F(LocalDeclDecoderTest, UseEncoder) { + const byte* data = nullptr; + const byte* end = nullptr; + LocalDeclEncoder local_decls; + + local_decls.AddLocals(5, kAstF32); + local_decls.AddLocals(1337, kAstI32); + local_decls.AddLocals(212, kAstI64); + local_decls.Prepend(&data, &end); + + LocalTypeMap map = DecodeLocalDeclsForTesting(data, end); + size_t pos = 0; + pos = ExpectRun(map, pos, kAstF32, 5); + pos = ExpectRun(map, pos, kAstI32, 1337); + pos = ExpectRun(map, pos, kAstI64, 212); + + if (map) delete map; + delete[] data; +} + } // namespace wasm } // namespace internal } // namespace v8 diff --git a/test/unittests/wasm/encoder-unittest.cc b/test/unittests/wasm/encoder-unittest.cc index e09e71aeb8..69677c2478 100644 --- a/test/unittests/wasm/encoder-unittest.cc +++ b/test/unittests/wasm/encoder-unittest.cc @@ -86,9 +86,6 @@ TEST_F(EncoderTest, Function_Builder_Variable_Indexing) { byte* header = buffer; byte* body = buffer + f->HeaderSize(); f->Serialize(buffer, &header, &body); - for (size_t i = 0; i < 7; i++) { - CHECK_EQ(i, static_cast(*(buffer + 2 * i + f->HeaderSize() + 1))); - } } @@ -109,15 +106,6 @@ TEST_F(EncoderTest, Function_Builder_Indexing_Variable_Width) { byte* body = buffer + f->HeaderSize(); f->Serialize(buffer, &header, &body); body = buffer + f->HeaderSize(); - for (size_t i = 0; i < 127; i++) { - CHECK_EQ(kExprGetLocal, static_cast(*(body + 2 * i))); - CHECK_EQ(i + 1, static_cast(*(body + 2 * i + 1))); - } - CHECK_EQ(kExprGetLocal, static_cast(*(body + 2 * 127))); - CHECK_EQ(0x80, static_cast(*(body + 2 * 127 + 1))); - CHECK_EQ(0x01, static_cast(*(body + 2 * 127 + 2))); - CHECK_EQ(kExprGetLocal, static_cast(*(body + 2 * 127 + 3))); - CHECK_EQ(0x00, static_cast(*(body + 2 * 127 + 4))); } diff --git a/test/unittests/wasm/loop-assignment-analysis-unittest.cc b/test/unittests/wasm/loop-assignment-analysis-unittest.cc index 958621970c..e77c1cfff5 100644 --- a/test/unittests/wasm/loop-assignment-analysis-unittest.cc +++ b/test/unittests/wasm/loop-assignment-analysis-unittest.cc @@ -23,25 +23,12 @@ namespace wasm { class WasmLoopAssignmentAnalyzerTest : public TestWithZone { public: - WasmLoopAssignmentAnalyzerTest() : TestWithZone(), sigs() { - init_env(&env, sigs.v_v()); - } - + WasmLoopAssignmentAnalyzerTest() : num_locals(0) {} TestSignatures sigs; - FunctionEnv env; - - static void init_env(FunctionEnv* env, FunctionSig* sig) { - env->module = nullptr; - env->sig = sig; - env->local_i32_count = 0; - env->local_i64_count = 0; - env->local_f32_count = 0; - env->local_f64_count = 0; - env->SumLocals(); - } + uint32_t num_locals; BitVector* Analyze(const byte* start, const byte* end) { - return AnalyzeLoopAssignmentForTesting(zone(), &env, start, end); + return AnalyzeLoopAssignmentForTesting(zone(), num_locals, start, end); } }; @@ -60,13 +47,13 @@ TEST_F(WasmLoopAssignmentAnalyzerTest, Empty1) { for (int j = 0; j < assigned->length(); j++) { CHECK_EQ(false, assigned->Contains(j)); } - env.AddLocals(kAstI32, 1); + num_locals++; } } TEST_F(WasmLoopAssignmentAnalyzerTest, One) { - env.AddLocals(kAstI32, 5); + num_locals = 5; for (int i = 0; i < 5; i++) { byte code[] = {WASM_LOOP(1, WASM_SET_ZERO(i))}; BitVector* assigned = Analyze(code, code + arraysize(code)); @@ -78,7 +65,7 @@ TEST_F(WasmLoopAssignmentAnalyzerTest, One) { TEST_F(WasmLoopAssignmentAnalyzerTest, OneBeyond) { - env.AddLocals(kAstI32, 5); + num_locals = 5; for (int i = 0; i < 5; i++) { byte code[] = {WASM_LOOP(1, WASM_SET_ZERO(i)), WASM_SET_ZERO(1)}; BitVector* assigned = Analyze(code, code + arraysize(code)); @@ -90,7 +77,7 @@ TEST_F(WasmLoopAssignmentAnalyzerTest, OneBeyond) { TEST_F(WasmLoopAssignmentAnalyzerTest, Two) { - env.AddLocals(kAstI32, 5); + num_locals = 5; for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { byte code[] = {WASM_LOOP(2, WASM_SET_ZERO(i), WASM_SET_ZERO(j))}; @@ -105,7 +92,7 @@ TEST_F(WasmLoopAssignmentAnalyzerTest, Two) { TEST_F(WasmLoopAssignmentAnalyzerTest, NestedIf) { - env.AddLocals(kAstI32, 5); + num_locals = 5; for (int i = 0; i < 5; i++) { byte code[] = {WASM_LOOP( 1, WASM_IF_ELSE(WASM_SET_ZERO(0), WASM_SET_ZERO(i), WASM_SET_ZERO(1)))}; @@ -126,7 +113,7 @@ static byte LEBByte(uint32_t val, byte which) { TEST_F(WasmLoopAssignmentAnalyzerTest, BigLocal) { - env.AddLocals(kAstI32, 65000); + num_locals = 65000; for (int i = 13; i < 65000; i = static_cast(i * 1.5)) { byte code[] = {kExprLoop, 1, @@ -148,7 +135,7 @@ TEST_F(WasmLoopAssignmentAnalyzerTest, BigLocal) { TEST_F(WasmLoopAssignmentAnalyzerTest, Break) { - env.AddLocals(kAstI32, 3); + num_locals = 3; byte code[] = { WASM_LOOP(1, WASM_IF(WASM_GET_LOCAL(0), WASM_BRV(1, WASM_SET_ZERO(1)))), WASM_SET_ZERO(0)}; @@ -162,7 +149,7 @@ TEST_F(WasmLoopAssignmentAnalyzerTest, Break) { TEST_F(WasmLoopAssignmentAnalyzerTest, Loop1) { - env.AddLocals(kAstI32, 5); + num_locals = 5; byte code[] = { WASM_LOOP(1, WASM_IF(WASM_GET_LOCAL(0), WASM_BRV(0, WASM_SET_LOCAL( @@ -179,9 +166,8 @@ TEST_F(WasmLoopAssignmentAnalyzerTest, Loop1) { TEST_F(WasmLoopAssignmentAnalyzerTest, Loop2) { - env.AddLocals(kAstI32, 3); + num_locals = 6; const byte kIter = 0; - env.AddLocals(kAstF32, 3); const byte kSum = 3; byte code[] = {WASM_BLOCK( diff --git a/test/unittests/wasm/module-decoder-unittest.cc b/test/unittests/wasm/module-decoder-unittest.cc index cab235f61b..c32e873ef8 100644 --- a/test/unittests/wasm/module-decoder-unittest.cc +++ b/test/unittests/wasm/module-decoder-unittest.cc @@ -903,10 +903,11 @@ class WasmFunctionVerifyTest : public TestWithZone {}; TEST_F(WasmFunctionVerifyTest, Ok_v_v_empty) { static const byte data[] = { 0, kLocalVoid, // signature - 3, 0, // local int32 count - 4, 0, // local int64 count - 5, 0, // local float32 count - 6, 0, // local float64 count + 4, // locals + 3, kLocalI32, // -- + 4, kLocalI64, // -- + 5, kLocalF32, // -- + 6, kLocalF64, // -- kExprNop // body }; @@ -919,12 +920,9 @@ TEST_F(WasmFunctionVerifyTest, Ok_v_v_empty) { EXPECT_EQ(0, function->sig->parameter_count()); EXPECT_EQ(0, function->sig->return_count()); EXPECT_EQ(0, function->name_offset); - EXPECT_EQ(arraysize(data) - 1, function->code_start_offset); + EXPECT_EQ(2, function->code_start_offset); EXPECT_EQ(arraysize(data), function->code_end_offset); - EXPECT_EQ(3, function->local_i32_count); - EXPECT_EQ(4, function->local_i64_count); - EXPECT_EQ(5, function->local_f32_count); - EXPECT_EQ(6, function->local_f64_count); + // TODO(titzer): verify encoding of local declarations EXPECT_FALSE(function->external); EXPECT_FALSE(function->exported); }