diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/binary-reader-ir.cc | 24 | ||||
-rw-r--r-- | src/binary-writer.cc | 8 | ||||
-rw-r--r-- | src/c-writer.cc | 5 | ||||
-rw-r--r-- | src/decompiler-naming.h | 2 | ||||
-rw-r--r-- | src/generate-names.cc | 11 | ||||
-rw-r--r-- | src/ir.cc | 27 | ||||
-rw-r--r-- | src/ir.h | 54 | ||||
-rw-r--r-- | src/resolve-names.cc | 4 | ||||
-rw-r--r-- | src/validator.cc | 18 | ||||
-rw-r--r-- | src/wast-parser.cc | 11 | ||||
-rw-r--r-- | src/wat-writer.cc | 5 |
11 files changed, 103 insertions, 66 deletions
diff --git a/src/binary-reader-ir.cc b/src/binary-reader-ir.cc index 9ebd1252..0bc03ebe 100644 --- a/src/binary-reader-ir.cc +++ b/src/binary-reader-ir.cc @@ -354,7 +354,7 @@ void BinaryReaderIR::SetBlockDeclaration(BlockDeclaration* decl, Index type_index = GetTypeIndex(sig_type); decl->has_func_type = true; decl->type_var = Var(type_index); - decl->sig = module_->func_types[type_index]->sig; + decl->sig = cast<FuncType>(module_->types[type_index])->sig; } else { decl->has_func_type = false; decl->sig.param_types.clear(); @@ -379,7 +379,7 @@ bool BinaryReaderIR::OnError(const Error& error) { Result BinaryReaderIR::OnTypeCount(Index count) { WABT_TRY - module_->func_types.reserve(count); + module_->types.reserve(count); WABT_CATCH_BAD_ALLOC return Result::Ok; } @@ -389,8 +389,8 @@ Result BinaryReaderIR::OnType(Index index, Type* param_types, Index result_count, Type* result_types) { - auto field = MakeUnique<FuncTypeModuleField>(GetLocation()); - FuncType& func_type = field->func_type; + auto field = MakeUnique<TypeModuleField>(GetLocation()); + FuncType& func_type = *cast<FuncType>(field->type.get()); func_type.sig.param_types.assign(param_types, param_types + param_count); func_type.sig.result_types.assign(result_types, result_types + result_count); module_->AppendField(std::move(field)); @@ -414,7 +414,7 @@ Result BinaryReaderIR::OnImportFunc(Index import_index, import->field_name = field_name.to_string(); import->func.decl.has_func_type = true; import->func.decl.type_var = Var(sig_index, GetLocation()); - import->func.decl.sig = module_->func_types[sig_index]->sig; + import->func.decl.sig = cast<FuncType>(module_->types[sig_index])->sig; module_->AppendField( MakeUnique<ImportModuleField>(std::move(import), GetLocation())); return Result::Ok; @@ -475,7 +475,7 @@ Result BinaryReaderIR::OnImportEvent(Index import_index, import->field_name = field_name.to_string(); import->event.decl.has_func_type = true; import->event.decl.type_var = Var(sig_index, GetLocation()); - import->event.decl.sig = module_->func_types[sig_index]->sig; + import->event.decl.sig = cast<FuncType>(module_->types[sig_index])->sig; module_->AppendField( MakeUnique<ImportModuleField>(std::move(import), GetLocation())); return Result::Ok; @@ -493,7 +493,7 @@ Result BinaryReaderIR::OnFunction(Index index, Index sig_index) { Func& func = field->func; func.decl.has_func_type = true; func.decl.type_var = Var(sig_index, GetLocation()); - func.decl.sig = module_->func_types[sig_index]->sig; + func.decl.sig = cast<FuncType>(module_->types[sig_index])->sig; module_->AppendField(std::move(field)); return Result::Ok; } @@ -707,11 +707,11 @@ Result BinaryReaderIR::OnCallExpr(Index func_index) { } Result BinaryReaderIR::OnCallIndirectExpr(Index sig_index, Index table_index) { - assert(sig_index < module_->func_types.size()); + assert(sig_index < module_->types.size()); auto expr = MakeUnique<CallIndirectExpr>(); expr->decl.has_func_type = true; expr->decl.type_var = Var(sig_index, GetLocation()); - expr->decl.sig = module_->func_types[sig_index]->sig; + expr->decl.sig = cast<FuncType>(module_->types[sig_index])->sig; expr->table = Var(table_index); return AppendExpr(std::move(expr)); } @@ -722,11 +722,11 @@ Result BinaryReaderIR::OnReturnCallExpr(Index func_index) { } Result BinaryReaderIR::OnReturnCallIndirectExpr(Index sig_index, Index table_index) { - assert(sig_index < module_->func_types.size()); + assert(sig_index < module_->types.size()); auto expr = MakeUnique<ReturnCallIndirectExpr>(); expr->decl.has_func_type = true; expr->decl.type_var = Var(sig_index, GetLocation()); - expr->decl.sig = module_->func_types[sig_index]->sig; + expr->decl.sig = cast<FuncType>(module_->types[sig_index])->sig; expr->table = Var(table_index); return AppendExpr(std::move(expr)); } @@ -1244,7 +1244,7 @@ Result BinaryReaderIR::OnEventType(Index index, Index sig_index) { Event& event = field->event; event.decl.has_func_type = true; event.decl.type_var = Var(sig_index, GetLocation()); - event.decl.sig = module_->func_types[sig_index]->sig; + event.decl.sig = cast<FuncType>(module_->types[sig_index])->sig; module_->AppendField(std::move(field)); return Result::Ok; } diff --git a/src/binary-writer.cc b/src/binary-writer.cc index 543d8a7d..66ec14f3 100644 --- a/src/binary-writer.cc +++ b/src/binary-writer.cc @@ -876,11 +876,11 @@ Result BinaryWriter::WriteModule() { stream_->WriteU32(WABT_BINARY_MAGIC, "WASM_BINARY_MAGIC"); stream_->WriteU32(WABT_BINARY_VERSION, "WASM_BINARY_VERSION"); - if (module_->func_types.size()) { + if (module_->types.size()) { BeginKnownSection(BinarySection::Type); - WriteU32Leb128(stream_, module_->func_types.size(), "num types"); - for (size_t i = 0; i < module_->func_types.size(); ++i) { - const FuncType* func_type = module_->func_types[i]; + WriteU32Leb128(stream_, module_->types.size(), "num types"); + for (size_t i = 0; i < module_->types.size(); ++i) { + const FuncType* func_type = cast<FuncType>(module_->types[i]); const FuncSignature* sig = &func_type->sig; WriteHeader("type", i); WriteType(stream_, Type::Func); diff --git a/src/c-writer.cc b/src/c-writer.cc index 9d562f4e..083c6151 100644 --- a/src/c-writer.cc +++ b/src/c-writer.cc @@ -886,11 +886,12 @@ void CWriter::WriteSourceTop() { void CWriter::WriteFuncTypes() { Write(Newline()); - Writef("static u32 func_types[%" PRIzd "];", module_->func_types.size()); + Writef("static u32 func_types[%" PRIzd "];", module_->types.size()); Write(Newline(), Newline()); Write("static void init_func_types(void) {", Newline()); Index func_type_index = 0; - for (FuncType* func_type : module_->func_types) { + for (TypeEntry* type : module_->types) { + FuncType* func_type = cast<FuncType>(type); Index num_params = func_type->GetNumParams(); Index num_results = func_type->GetNumResults(); Write(" func_types[", func_type_index, "] = wasm_rt_register_func_type(", diff --git a/src/decompiler-naming.h b/src/decompiler-naming.h index fcf982ac..5400ef93 100644 --- a/src/decompiler-naming.h +++ b/src/decompiler-naming.h @@ -196,7 +196,7 @@ void RenameAll(Module& module) { RenameToIdentifiers(module.tables, module.table_bindings, nullptr); RenameToIdentifiers(module.events, module.event_bindings, nullptr); RenameToIdentifiers(module.exports, module.export_bindings, nullptr); - RenameToIdentifiers(module.func_types, module.func_type_bindings, nullptr); + RenameToIdentifiers(module.types, module.type_bindings, nullptr); RenameToIdentifiers(module.memories, module.memory_bindings, nullptr); RenameToIdentifiers(module.data_segments, module.data_segment_bindings, nullptr); diff --git a/src/generate-names.cc b/src/generate-names.cc index e615d471..03758e94 100644 --- a/src/generate-names.cc +++ b/src/generate-names.cc @@ -86,7 +86,7 @@ class NameGenerator : public ExprVisitor::DelegateNop { Result VisitFunc(Index func_index, Func* func); Result VisitGlobal(Index global_index, Global* global); - Result VisitFuncType(Index func_type_index, FuncType* func_type); + Result VisitType(Index func_type_index, TypeEntry* type); Result VisitTable(Index table_index, Table* table); Result VisitMemory(Index memory_index, Memory* memory); Result VisitEvent(Index event_index, Event* event); @@ -241,10 +241,9 @@ Result NameGenerator::VisitGlobal(Index global_index, Global* global) { return Result::Ok; } -Result NameGenerator::VisitFuncType(Index func_type_index, - FuncType* func_type) { - MaybeGenerateAndBindName(&module_->func_type_bindings, "t", func_type_index, - &func_type->name); +Result NameGenerator::VisitType(Index type_index, TypeEntry* type) { + MaybeGenerateAndBindName(&module_->type_bindings, "t", type_index, + &type->name); return Result::Ok; } @@ -411,7 +410,7 @@ Result NameGenerator::VisitModule(Module* module) { } VisitAll(module->globals, &NameGenerator::VisitGlobal); - VisitAll(module->func_types, &NameGenerator::VisitFuncType); + VisitAll(module->types, &NameGenerator::VisitType); VisitAll(module->funcs, &NameGenerator::VisitFunc); VisitAll(module->tables, &NameGenerator::VisitTable); VisitAll(module->memories, &NameGenerator::VisitMemory); @@ -128,7 +128,7 @@ Index Module::GetMemoryIndex(const Var& var) const { } Index Module::GetFuncTypeIndex(const Var& var) const { - return func_type_bindings.FindIndex(var); + return type_bindings.FindIndex(var); } Index Module::GetEventIndex(const Var& var) const { @@ -310,16 +310,16 @@ const FuncType* Module::GetFuncType(const Var& var) const { } FuncType* Module::GetFuncType(const Var& var) { - Index index = func_type_bindings.FindIndex(var); - if (index >= func_types.size()) { + Index index = type_bindings.FindIndex(var); + if (index >= types.size()) { return nullptr; } - return func_types[index]; + return cast<FuncType>(types[index]); } Index Module::GetFuncTypeIndex(const FuncSignature& sig) const { - for (size_t i = 0; i < func_types.size(); ++i) { - if (func_types[i]->sig == sig) { + for (size_t i = 0; i < types.size(); ++i) { + if (cast<FuncType>(types[i])->sig == sig) { return i; } } @@ -380,13 +380,12 @@ void Module::AppendField(std::unique_ptr<FuncModuleField> field) { fields.push_back(std::move(field)); } -void Module::AppendField(std::unique_ptr<FuncTypeModuleField> field) { - FuncType& func_type = field->func_type; - if (!func_type.name.empty()) { - func_type_bindings.emplace(func_type.name, - Binding(field->loc, func_types.size())); +void Module::AppendField(std::unique_ptr<TypeModuleField> field) { + TypeEntry& type = *field->type; + if (!type.name.empty()) { + type_bindings.emplace(type.name, Binding(field->loc, types.size())); } - func_types.push_back(&func_type); + types.push_back(&type); fields.push_back(std::move(field)); } @@ -506,8 +505,8 @@ void Module::AppendField(std::unique_ptr<ModuleField> field) { AppendField(cast<ExportModuleField>(std::move(field))); break; - case ModuleFieldType::FuncType: - AppendField(cast<FuncTypeModuleField>(std::move(field))); + case ModuleFieldType::Type: + AppendField(cast<TypeModuleField>(std::move(field))); break; case ModuleFieldType::Table: @@ -140,15 +140,44 @@ struct FuncSignature { bool operator==(const FuncSignature&) const; }; -struct FuncType { - explicit FuncType(string_view name) : name(name.to_string()) {} +enum class TypeEntryKind { + Func, +}; + +class TypeEntry { + public: + WABT_DISALLOW_COPY_AND_ASSIGN(TypeEntry); + + virtual ~TypeEntry() = default; + + TypeEntryKind kind() const { return kind_; } + + Location loc; + std::string name; + + protected: + explicit TypeEntry(TypeEntryKind kind, + string_view name = string_view(), + const Location& loc = Location()) + : loc(loc), name(name.to_string()), kind_(kind) {} + + TypeEntryKind kind_; +}; + +class FuncType : public TypeEntry { + public: + static bool classof(const TypeEntry* entry) { + return entry->kind() == TypeEntryKind::Func; + } + + explicit FuncType(string_view name = string_view()) + : TypeEntry(TypeEntryKind::Func, name) {} Index GetNumParams() const { return sig.GetNumParams(); } Index GetNumResults() const { return sig.GetNumResults(); } Type GetParamType(Index index) const { return sig.GetParamType(index); } Type GetResultType(Index index) const { return sig.GetResultType(index); } - std::string name; FuncSignature sig; }; @@ -711,7 +740,7 @@ enum class ModuleFieldType { Global, Import, Export, - FuncType, + Type, Table, ElemSegment, Memory, @@ -787,13 +816,14 @@ class ExportModuleField : public ModuleFieldMixin<ModuleFieldType::Export> { Export export_; }; -class FuncTypeModuleField : public ModuleFieldMixin<ModuleFieldType::FuncType> { +class TypeModuleField : public ModuleFieldMixin<ModuleFieldType::Type> { public: - explicit FuncTypeModuleField(const Location& loc = Location(), - string_view name = string_view()) - : ModuleFieldMixin<ModuleFieldType::FuncType>(loc), func_type(name) {} + explicit TypeModuleField(const Location& loc = Location(), + string_view name = string_view()) + : ModuleFieldMixin<ModuleFieldType::Type>(loc), + type(MakeUnique<FuncType>(name)) {} - FuncType func_type; + std::unique_ptr<TypeEntry> type; }; class TableModuleField : public ModuleFieldMixin<ModuleFieldType::Table> { @@ -892,7 +922,7 @@ struct Module { void AppendField(std::unique_ptr<EventModuleField>); void AppendField(std::unique_ptr<ExportModuleField>); void AppendField(std::unique_ptr<FuncModuleField>); - void AppendField(std::unique_ptr<FuncTypeModuleField>); + void AppendField(std::unique_ptr<TypeModuleField>); void AppendField(std::unique_ptr<GlobalModuleField>); void AppendField(std::unique_ptr<ImportModuleField>); void AppendField(std::unique_ptr<MemoryModuleField>); @@ -918,7 +948,7 @@ struct Module { std::vector<Global*> globals; std::vector<Import*> imports; std::vector<Export*> exports; - std::vector<FuncType*> func_types; + std::vector<TypeEntry*> types; std::vector<Table*> tables; std::vector<ElemSegment*> elem_segments; std::vector<Memory*> memories; @@ -929,7 +959,7 @@ struct Module { BindingHash func_bindings; BindingHash global_bindings; BindingHash export_bindings; - BindingHash func_type_bindings; + BindingHash type_bindings; BindingHash table_bindings; BindingHash memory_bindings; BindingHash data_segment_bindings; diff --git a/src/resolve-names.cc b/src/resolve-names.cc index 6391e2af..2dff2a62 100644 --- a/src/resolve-names.cc +++ b/src/resolve-names.cc @@ -186,7 +186,7 @@ void NameResolver::ResolveGlobalVar(Var* var) { } void NameResolver::ResolveFuncTypeVar(Var* var) { - ResolveVar(¤t_module_->func_type_bindings, var, "function type"); + ResolveVar(¤t_module_->type_bindings, var, "type"); } void NameResolver::ResolveTableVar(Var* var) { @@ -477,7 +477,7 @@ Result NameResolver::VisitModule(Module* module) { CheckDuplicateBindings(&module->elem_segment_bindings, "elem"); CheckDuplicateBindings(&module->func_bindings, "function"); CheckDuplicateBindings(&module->global_bindings, "global"); - CheckDuplicateBindings(&module->func_type_bindings, "function type"); + CheckDuplicateBindings(&module->type_bindings, "type"); CheckDuplicateBindings(&module->table_bindings, "table"); CheckDuplicateBindings(&module->memory_bindings, "memory"); CheckDuplicateBindings(&module->event_bindings, "event"); diff --git a/src/validator.cc b/src/validator.cc index 2de234ca..ce25df9e 100644 --- a/src/validator.cc +++ b/src/validator.cc @@ -584,12 +584,18 @@ Result Validator::CheckModule() { // Type section. for (const ModuleField& field : module->fields) { - if (auto* f = dyn_cast<FuncTypeModuleField>(&field)) { - result_ |= - validator_.OnType(field.loc, f->func_type.sig.param_types.size(), - f->func_type.sig.param_types.data(), - f->func_type.sig.result_types.size(), - f->func_type.sig.result_types.data()); + if (auto* f = dyn_cast<TypeModuleField>(&field)) { + switch (f->type->kind()) { + case TypeEntryKind::Func: { + FuncType* func_type = cast<FuncType>(f->type.get()); + result_ |= + validator_.OnType(field.loc, func_type->sig.param_types.size(), + func_type->sig.param_types.data(), + func_type->sig.result_types.size(), + func_type->sig.result_types.data()); + break; + } + } } } diff --git a/src/wast-parser.cc b/src/wast-parser.cc index 42286e08..3468e008 100644 --- a/src/wast-parser.cc +++ b/src/wast-parser.cc @@ -260,8 +260,8 @@ void ResolveImplicitlyDefinedFunctionType(const Location& loc, if (!decl.has_func_type) { Index func_type_index = module->GetFuncTypeIndex(decl.sig); if (func_type_index == kInvalidIndex) { - auto func_type_field = MakeUnique<FuncTypeModuleField>(loc); - func_type_field->func_type.sig = decl.sig; + auto func_type_field = MakeUnique<TypeModuleField>(loc); + cast<FuncType>(func_type_field->type.get())->sig = decl.sig; module->AppendField(std::move(func_type_field)); } } @@ -1182,13 +1182,14 @@ Result WastParser::ParseFuncModuleField(Module* module) { Result WastParser::ParseTypeModuleField(Module* module) { WABT_TRACE(ParseTypeModuleField); EXPECT(Lpar); - auto field = MakeUnique<FuncTypeModuleField>(GetLocation()); + auto field = MakeUnique<TypeModuleField>(GetLocation()); + FuncType& func_type = *cast<FuncType>(field->type.get()); EXPECT(Type); - ParseBindVarOpt(&field->func_type.name); + ParseBindVarOpt(&func_type.name); EXPECT(Lpar); EXPECT(Func); BindingHash bindings; - CHECK_RESULT(ParseFuncSignature(&field->func_type.sig, &bindings)); + CHECK_RESULT(ParseFuncSignature(&func_type.sig, &bindings)); CHECK_RESULT(ErrorIfLpar({"param", "result"})); EXPECT(Rpar); EXPECT(Rpar); diff --git a/src/wat-writer.cc b/src/wat-writer.cc index 1214b42a..ee3c1059 100644 --- a/src/wat-writer.cc +++ b/src/wat-writer.cc @@ -1418,8 +1418,9 @@ Result WatWriter::WriteModule() { case ModuleFieldType::DataSegment: WriteDataSegment(cast<DataSegmentModuleField>(&field)->data_segment); break; - case ModuleFieldType::FuncType: - WriteFuncType(cast<FuncTypeModuleField>(&field)->func_type); + case ModuleFieldType::Type: + WriteFuncType( + *cast<FuncType>(cast<TypeModuleField>(&field)->type.get())); break; case ModuleFieldType::Start: WriteStartFunction(cast<StartModuleField>(&field)->start); |