diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/apply-names.cc | 10 | ||||
-rw-r--r-- | src/binary-reader-ir.cc | 24 | ||||
-rw-r--r-- | src/binary-reader-logging.cc | 19 | ||||
-rw-r--r-- | src/binary-reader-logging.h | 4 | ||||
-rw-r--r-- | src/binary-reader-nop.h | 4 | ||||
-rw-r--r-- | src/binary-reader-objdump.cc | 28 | ||||
-rw-r--r-- | src/binary-reader.cc | 32 | ||||
-rw-r--r-- | src/binary-reader.h | 10 | ||||
-rw-r--r-- | src/binary-writer.cc | 19 | ||||
-rw-r--r-- | src/ir.h | 2 | ||||
-rw-r--r-- | src/resolve-names.cc | 9 | ||||
-rw-r--r-- | src/validator.cc | 17 | ||||
-rw-r--r-- | src/wast-parser.cc | 11 | ||||
-rw-r--r-- | src/wat-writer.cc | 19 |
14 files changed, 101 insertions, 107 deletions
diff --git a/src/apply-names.cc b/src/apply-names.cc index 28d9ec5e..0afea6c4 100644 --- a/src/apply-names.cc +++ b/src/apply-names.cc @@ -77,6 +77,7 @@ class NameApplier : public ExprVisitor::DelegateNop { Result UseNameForParamAndLocalVar(Func* func, Var* var); Result VisitFunc(Index func_index, Func* func); Result VisitGlobal(Global* global); + Result VisitEvent(Event* event); Result VisitExport(Index export_index, Export* export_); Result VisitElemSegment(Index elem_segment_index, ElemSegment* segment); Result VisitDataSegment(Index data_segment_index, DataSegment* segment); @@ -372,6 +373,13 @@ Result NameApplier::VisitGlobal(Global* global) { return Result::Ok; } +Result NameApplier::VisitEvent(Event* event) { + if (event->decl.has_func_type) { + CHECK_RESULT(UseNameForFuncTypeVar(&event->decl.type_var)); + } + return Result::Ok; +} + Result NameApplier::VisitExport(Index export_index, Export* export_) { if (export_->kind == ExternalKind::Func) { UseNameForFuncVar(&export_->var); @@ -402,6 +410,8 @@ Result NameApplier::VisitModule(Module* module) { CHECK_RESULT(VisitFunc(i, module->funcs[i])); for (size_t i = 0; i < module->globals.size(); ++i) CHECK_RESULT(VisitGlobal(module->globals[i])); + for (size_t i = 0; i < module->events.size(); ++i) + CHECK_RESULT(VisitEvent(module->events[i])); for (size_t i = 0; i < module->exports.size(); ++i) CHECK_RESULT(VisitExport(i, module->exports[i])); for (size_t i = 0; i < module->elem_segments.size(); ++i) diff --git a/src/binary-reader-ir.cc b/src/binary-reader-ir.cc index e387991e..4a5ce416 100644 --- a/src/binary-reader-ir.cc +++ b/src/binary-reader-ir.cc @@ -82,10 +82,10 @@ class BinaryReaderIR : public BinaryReaderNop { Type type, bool mutable_) override; Result OnImportEvent(Index import_index, - string_view module_name, - string_view field_name, - Index event_index, - TypeVector& sig) override; + string_view module_name, + string_view field_name, + Index event_index, + Index sig_index) override; Result OnFunctionCount(Index count) override; Result OnFunction(Index index, Index sig_index) override; @@ -216,7 +216,7 @@ class BinaryReaderIR : public BinaryReaderNop { Result BeginEventSection(Offset size) override { return Result::Ok; } Result OnEventCount(Index count) override { return Result::Ok; } - Result OnEventType(Index index, TypeVector& types) override; + Result OnEventType(Index index, Index sig_index) override; Result EndEventSection() override { return Result::Ok; } Result OnInitExprF32ConstExpr(Index index, uint32_t value) override; @@ -437,11 +437,13 @@ Result BinaryReaderIR::OnImportEvent(Index import_index, string_view module_name, string_view field_name, Index event_index, - TypeVector& sig) { + Index sig_index) { auto import = MakeUnique<EventImport>(); import->module_name = module_name.to_string(); import->field_name = field_name.to_string(); - import->event.sig = sig; + 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; module_->AppendField( MakeUnique<ImportModuleField>(std::move(import), GetLocation())); return Result::Ok; @@ -552,7 +554,7 @@ Result BinaryReaderIR::OnExport(Index index, assert(item_index < module_->globals.size()); break; case ExternalKind::Event: - // Note: Can't check if index valid, the event section comes later. + assert(item_index < module_->events.size()); break; } export_.var = Var(item_index, GetLocation()); @@ -1127,10 +1129,12 @@ Result BinaryReaderIR::OnLocalName(Index func_index, return Result::Ok; } -Result BinaryReaderIR::OnEventType(Index index, TypeVector& sig) { +Result BinaryReaderIR::OnEventType(Index index, Index sig_index) { auto field = MakeUnique<EventModuleField>(GetLocation()); Event& event = field->event; - event.sig = sig; + event.decl.has_func_type = true; + event.decl.type_var = Var(sig_index, GetLocation()); + event.decl.sig = module_->func_types[sig_index]->sig; module_->AppendField(std::move(field)); return Result::Ok; } diff --git a/src/binary-reader-logging.cc b/src/binary-reader-logging.cc index fb4f50ad..31d2ca7e 100644 --- a/src/binary-reader-logging.cc +++ b/src/binary-reader-logging.cc @@ -211,14 +211,12 @@ Result BinaryReaderLogging::OnImportEvent(Index import_index, string_view module_name, string_view field_name, Index event_index, - TypeVector& sig) { + Index sig_index) { LOGF("OnImportEvent(import_index: %" PRIindex ", event_index: %" PRIindex - ", sig: ", - import_index, event_index); - LogTypes(sig); - LOGF_NOINDENT(")\n"); + ", sig_index: %" PRIindex ")\n", + import_index, event_index, sig_index); return reader_->OnImportEvent(import_index, module_name, field_name, - event_index, sig); + event_index, sig_index); } Result BinaryReaderLogging::OnTable(Index index, @@ -299,13 +297,6 @@ Result BinaryReaderLogging::OnBrTableExpr(Index num_targets, default_target_depth); } -Result BinaryReaderLogging::OnEventType(Index index, TypeVector& sig) { - LOGF("OnEventType(index: %" PRIindex ", values: ", index); - LogTypes(sig); - LOGF_NOINDENT(")\n"); - return reader_->OnEventType(index, sig); -} - Result BinaryReaderLogging::OnF32ConstExpr(uint32_t value_bits) { float value; memcpy(&value, &value_bits, sizeof(value)); @@ -757,7 +748,7 @@ DEFINE_END(EndLinkingSection) DEFINE_BEGIN(BeginEventSection); DEFINE_INDEX(OnEventCount); - +DEFINE_INDEX_INDEX(OnEventType, "index", "sig_index") DEFINE_END(EndEventSection); // We don't need to log these (the individual opcodes are logged instead), but diff --git a/src/binary-reader-logging.h b/src/binary-reader-logging.h index 171dff6f..7f481bdf 100644 --- a/src/binary-reader-logging.h +++ b/src/binary-reader-logging.h @@ -78,7 +78,7 @@ class BinaryReaderLogging : public BinaryReaderDelegate { string_view module_name, string_view field_name, Index event_index, - TypeVector& sig) override; + Index sig_index) override; Result EndImportSection() override; Result BeginFunctionSection(Offset size) override; @@ -305,7 +305,7 @@ class BinaryReaderLogging : public BinaryReaderDelegate { Result BeginEventSection(Offset size) override; Result OnEventCount(Index count) override; - Result OnEventType(Index index, TypeVector& sig) override; + Result OnEventType(Index index, Index sig_index) override; Result EndEventSection() override; Result OnInitExprF32ConstExpr(Index index, uint32_t value) override; diff --git a/src/binary-reader-nop.h b/src/binary-reader-nop.h index b0010da2..250cfc23 100644 --- a/src/binary-reader-nop.h +++ b/src/binary-reader-nop.h @@ -93,7 +93,7 @@ class BinaryReaderNop : public BinaryReaderDelegate { string_view module_name, string_view field_name, Index event_index, - TypeVector& sig) override { + Index sig_index) override { return Result::Ok; } Result EndImportSection() override { return Result::Ok; } @@ -364,7 +364,7 @@ class BinaryReaderNop : public BinaryReaderDelegate { /* Event section */ Result BeginEventSection(Offset size) override { return Result::Ok; } Result OnEventCount(Index count) override { return Result::Ok; } - Result OnEventType(Index index, TypeVector& sig) override { + Result OnEventType(Index index, Index sig_index) override { return Result::Ok; } Result EndEventSection() override { return Result::Ok; } diff --git a/src/binary-reader-objdump.cc b/src/binary-reader-objdump.cc index 04716afc..8996f67c 100644 --- a/src/binary-reader-objdump.cc +++ b/src/binary-reader-objdump.cc @@ -680,7 +680,7 @@ class BinaryReaderObjdump : public BinaryReaderObjdumpBase { string_view module_name, string_view field_name, Index event_index, - TypeVector& sig) override; + Index sig_index) override; Result OnFunctionCount(Index count) override; Result OnFunction(Index index, Index sig_index) override; @@ -792,7 +792,7 @@ class BinaryReaderObjdump : public BinaryReaderObjdumpBase { Result OnInitFunction(uint32_t priority, Index function_index) override; Result OnEventCount(Index count) override; - Result OnEventType(Index index, TypeVector& sig) override; + Result OnEventType(Index index, Index sig_index) override; private: Result HandleInitExpr(const InitExpr& expr); @@ -1060,15 +1060,10 @@ Result BinaryReaderObjdump::OnImportEvent(Index import_index, string_view module_name, string_view field_name, Index event_index, - TypeVector& sig) { - PrintDetails(" - event[%" PRIindex "] (", event_index); - for (Index i = 0; i < sig.size(); ++i) { - if (i != 0) { - PrintDetails(", "); - } - PrintDetails("%s", GetTypeName(sig[i])); - } - PrintDetails(") <- " PRIstringview "." PRIstringview "\n", + Index sig_index) { + PrintDetails(" - event[%" PRIindex "] sig=%" PRIindex, event_index, + sig_index); + PrintDetails(" <- " PRIstringview "." PRIstringview "\n", WABT_PRINTF_STRING_VIEW_ARG(module_name), WABT_PRINTF_STRING_VIEW_ARG(field_name)); return Result::Ok; @@ -1531,18 +1526,11 @@ Result BinaryReaderObjdump::OnEventCount(Index count) { return OnCount(count); } -Result BinaryReaderObjdump::OnEventType(Index index, TypeVector& sig) { +Result BinaryReaderObjdump::OnEventType(Index index, Index sig_index) { if (!ShouldPrintDetails()) { return Result::Ok; } - printf(" - event[%" PRIindex "] (", index); - for (Index i = 0; i < sig.size(); ++i) { - if (i != 0) { - printf(", "); - } - printf("%s", GetTypeName(sig[i])); - } - printf(")\n"); + printf(" - event[%" PRIindex "] sig=%" PRIindex "\n", index, sig_index); return Result::Ok; } diff --git a/src/binary-reader.cc b/src/binary-reader.cc index d99e10ce..820f85c1 100644 --- a/src/binary-reader.cc +++ b/src/binary-reader.cc @@ -119,7 +119,7 @@ class BinaryReader { Limits* out_elem_limits) WABT_WARN_UNUSED; Result ReadMemory(Limits* out_page_limits) WABT_WARN_UNUSED; Result ReadGlobalHeader(Type* out_type, bool* out_mutable) WABT_WARN_UNUSED; - Result ReadEventType(TypeVector& sig) WABT_WARN_UNUSED; + Result ReadEventType(Index* out_sig_index) WABT_WARN_UNUSED; Result ReadFunctionBody(Offset end_offset) WABT_WARN_UNUSED; Result ReadNameSection(Offset section_size) WABT_WARN_UNUSED; Result ReadRelocSection(Offset section_size) WABT_WARN_UNUSED; @@ -1675,18 +1675,11 @@ Result BinaryReader::ReadLinkingSection(Offset section_size) { return Result::Ok; } -Result BinaryReader::ReadEventType(TypeVector& sig) { - Index num_values; - CHECK_RESULT(ReadCount(&num_values, "event type count")); - sig.resize(num_values); - for (Index j = 0; j < num_values; ++j) { - Type value_type; - CHECK_RESULT(ReadType(&value_type, "event value type")); - ERROR_UNLESS(IsConcreteType(value_type), - "expected valid event value type (got %d)", - static_cast<int>(value_type)); - sig[j] = value_type; - } +Result BinaryReader::ReadEventType(Index* out_sig_index) { + uint32_t attribute; + CHECK_RESULT(ReadU32Leb128(&attribute, "event attribute")); + ERROR_UNLESS(attribute == 0, "event attribute must be 0"); + CHECK_RESULT(ReadIndex(out_sig_index, "event signature index")); return Result::Ok; } @@ -1696,9 +1689,10 @@ Result BinaryReader::ReadEventSection(Offset section_size) { CALLBACK(OnEventCount, num_events_); for (Index i = 0; i < num_events_; ++i) { - TypeVector sig; - CHECK_RESULT(ReadEventType(sig)); - CALLBACK(OnEventType, i, sig); + Index event_index = num_event_imports_ + i; + Index sig_index; + CHECK_RESULT(ReadEventType(&sig_index)); + CALLBACK(OnEventType, event_index, sig_index); } CALLBACK(EndEventSection); @@ -1841,11 +1835,11 @@ Result BinaryReader::ReadImportSection(Offset section_size) { case ExternalKind::Event: { ERROR_UNLESS(options_.features.exceptions_enabled(), "invalid import event kind: exceptions not allowed"); - TypeVector sig; - CHECK_RESULT(ReadEventType(sig)); + Index sig_index; + CHECK_RESULT(ReadEventType(&sig_index)); CALLBACK(OnImport, i, module_name, field_name); CALLBACK(OnImportEvent, i, module_name, field_name, num_event_imports_, - sig); + sig_index); num_event_imports_++; break; } diff --git a/src/binary-reader.h b/src/binary-reader.h index fb13819b..46424977 100644 --- a/src/binary-reader.h +++ b/src/binary-reader.h @@ -116,10 +116,10 @@ class BinaryReaderDelegate { Type type, bool mutable_) = 0; virtual Result OnImportEvent(Index import_index, - string_view module_name, - string_view field_name, - Index event_index, - TypeVector& sig) = 0; + string_view module_name, + string_view field_name, + Index event_index, + Index sig_index) = 0; virtual Result EndImportSection() = 0; /* Function section */ @@ -373,7 +373,7 @@ class BinaryReaderDelegate { /* Event section */ virtual Result BeginEventSection(Offset size) = 0; virtual Result OnEventCount(Index count) = 0; - virtual Result OnEventType(Index index, TypeVector& sig) = 0; + virtual Result OnEventType(Index index, Index sig_index) = 0; virtual Result EndEventSection() = 0; /* InitExpr - used by elem, data and global sections; these functions are diff --git a/src/binary-writer.cc b/src/binary-writer.cc index 468d3e0c..9a2a13ad 100644 --- a/src/binary-writer.cc +++ b/src/binary-writer.cc @@ -143,7 +143,7 @@ class BinaryWriter { void WriteTable(const Table* table); void WriteMemory(const Memory* memory); void WriteGlobalHeader(const Global* global); - void WriteEventType(const TypeVector* event_types); + void WriteEventType(const Event* event); void WriteRelocSection(const RelocSection* reloc_section); void WriteLinkingSection(); @@ -703,11 +703,10 @@ void BinaryWriter::WriteGlobalHeader(const Global* global) { stream_->WriteU8(global->mutable_, "global mutability"); } -void BinaryWriter::WriteEventType(const TypeVector* event_types) { - WriteU32Leb128(stream_, event_types->size(), "event type count"); - for (Type ty : *event_types) { - WriteType(stream_, ty); - } +void BinaryWriter::WriteEventType(const Event* event) { + WriteU32Leb128(stream_, 0, "event attribute"); + WriteU32Leb128(stream_, module_->GetFuncTypeIndex(event->decl), + "event signature index"); } void BinaryWriter::WriteRelocSection(const RelocSection* reloc_section) { @@ -837,7 +836,7 @@ Result BinaryWriter::WriteModule() { break; case ExternalKind::Event: - WriteEventType(&cast<EventImport>(import)->event.sig); + WriteEventType(&cast<EventImport>(import)->event); break; } } @@ -905,8 +904,10 @@ Result BinaryWriter::WriteModule() { if (num_events) { BeginKnownSection(BinarySection::Event); WriteU32Leb128(stream_, num_events, "event count"); - for (Index i = module_->num_event_imports; i < num_events; ++i) { - WriteEventType(&module_->events[i]->sig); + for (size_t i = 0; i < num_events; ++i) { + WriteHeader("event", i); + const Event* event = module_->events[i + module_->num_event_imports]; + WriteEventType(event); } EndSection(); } @@ -417,7 +417,7 @@ struct Event { explicit Event(string_view name) : name(name.to_string()) {} std::string name; - TypeVector sig; + FuncDeclaration decl; }; class LocalTypes { diff --git a/src/resolve-names.cc b/src/resolve-names.cc index b93a2221..3ebd3567 100644 --- a/src/resolve-names.cc +++ b/src/resolve-names.cc @@ -85,6 +85,7 @@ class NameResolver : public ExprVisitor::DelegateNop { void VisitFunc(Func* func); void VisitExport(Export* export_); void VisitGlobal(Global* global); + void VisitEvent(Event* event); void VisitElemSegment(ElemSegment* segment); void VisitDataSegment(DataSegment* segment); void VisitScriptModule(ScriptModule* script_module); @@ -404,6 +405,12 @@ void NameResolver::VisitGlobal(Global* global) { visitor_.VisitExprList(global->init_expr); } +void NameResolver::VisitEvent(Event* event) { + if (event->decl.has_func_type) { + ResolveFuncTypeVar(&event->decl.type_var); + } +} + void NameResolver::VisitElemSegment(ElemSegment* segment) { ResolveTableVar(&segment->table_var); visitor_.VisitExprList(segment->offset); @@ -431,6 +438,8 @@ Result NameResolver::VisitModule(Module* module) { VisitExport(export_); for (Global* global : module->globals) VisitGlobal(global); + for (Event* event : module->events) + VisitEvent(event); for (ElemSegment* elem_segment : module->elem_segments) VisitElemSegment(elem_segment); for (DataSegment* data_segment : module->data_segments) diff --git a/src/validator.cc b/src/validator.cc index 00c97d9d..8a3d4940 100644 --- a/src/validator.cc +++ b/src/validator.cc @@ -864,7 +864,7 @@ Result Validator::OnThrowExpr(ThrowExpr* expr) { expr_loc_ = &expr->loc; const Event* event; if (Succeeded(CheckEventVar(&expr->var, &event))) { - typechecker_.OnThrow(event->sig); + typechecker_.OnThrow(event->decl.sig.param_types); } return Result::Ok; } @@ -1343,18 +1343,9 @@ Result Validator::CheckEventVar(const Var* var, const Event** out_event) { } void Validator::CheckEvent(const Location* loc, const Event* event) { - for (Type ty : event->sig) { - switch (ty) { - case Type::I32: - case Type::I64: - case Type::F32: - case Type::F64: - case Type::V128: - break; - default: - PrintError(loc, "Invalid event type: %s", GetTypeName(ty)); - break; - } + CheckFuncSignature(loc, event->decl); + if (event->decl.sig.GetNumResults() > 0) { + PrintError(loc, "Event signature must have 0 results."); } } diff --git a/src/wast-parser.cc b/src/wast-parser.cc index 66898a2f..64071faf 100644 --- a/src/wast-parser.cc +++ b/src/wast-parser.cc @@ -316,12 +316,17 @@ void ResolveFuncTypes(Module* module) { if (auto* func_field = dyn_cast<FuncModuleField>(&field)) { func = &func_field->func; decl = &func->decl; + } else if (auto* event_field = dyn_cast<EventModuleField>(&field)) { + decl = &event_field->event.decl; } else if (auto* import_field = dyn_cast<ImportModuleField>(&field)) { if (auto* func_import = dyn_cast<FuncImport>(import_field->import.get())) { // Only check the declaration, not the function itself, since it is an // import. decl = &func_import->func.decl; + } else if (auto* event_import = + dyn_cast<EventImport>(import_field->import.get())) { + decl = &event_import->event.decl; } else { continue; } @@ -846,7 +851,8 @@ Result WastParser::ParseEventModuleField(Module* module) { auto field = MakeUnique<EventModuleField>(GetLocation()); EXPECT(Event); ParseBindVarOpt(&field->event.name); - CHECK_RESULT(ParseValueTypeList(&field->event.sig)); + CHECK_RESULT(ParseTypeUseOpt(&field->event.decl)); + CHECK_RESULT(ParseUnboundFuncSignature(&field->event.decl.sig)); EXPECT(Rpar); module->AppendField(std::move(field)); return Result::Ok; @@ -1023,7 +1029,8 @@ Result WastParser::ParseImportModuleField(Module* module) { Consume(); ParseBindVarOpt(&name); auto import = MakeUnique<EventImport>(name); - CHECK_RESULT(ParseValueTypeList(&import->event.sig)); + CHECK_RESULT(ParseTypeUseOpt(&import->event.decl)); + CHECK_RESULT(ParseUnboundFuncSignature(&import->event.decl.sig)); EXPECT(Rpar); field = MakeUnique<ImportModuleField>(std::move(import), loc); break; diff --git a/src/wat-writer.cc b/src/wat-writer.cc index 9ac76b59..5a3b1a25 100644 --- a/src/wat-writer.cc +++ b/src/wat-writer.cc @@ -157,7 +157,6 @@ class WatWriter { void WriteFunc(const Func& func); void WriteBeginGlobal(const Global& global); void WriteGlobal(const Global& global); - void WriteBeginEvent(const Event& event); void WriteEvent(const Event& event); void WriteLimits(const Limits& limits); void WriteTable(const Table& table); @@ -1071,7 +1070,7 @@ void WatWriter::WriteFoldedExpr(const Expr* expr) { auto throw_ = cast<ThrowExpr>(expr); Index operand_count = 0; if (Event* event = module_->GetEvent(throw_->var)) { - operand_count = event->sig.size(); + operand_count = event->decl.sig.param_types.size(); } PushExpr(expr, operand_count, 0); break; @@ -1382,17 +1381,18 @@ void WatWriter::WriteGlobal(const Global& global) { WriteCloseNewline(); } -void WatWriter::WriteBeginEvent(const Event& event) { +void WatWriter::WriteEvent(const Event& event) { WriteOpenSpace("event"); WriteNameOrIndex(event.name, event_index_, NextChar::Space); WriteInlineExports(ExternalKind::Event, event_index_); WriteInlineImport(ExternalKind::Event, event_index_); - WriteTypes(event.sig, nullptr); + if (event.decl.has_func_type) { + WriteOpenSpace("type"); + WriteVar(event.decl.type_var, NextChar::None); + WriteCloseSpace(); + } + WriteTypes(event.decl.sig.param_types, "param"); ++event_index_; -} - -void WatWriter::WriteEvent(const Event& event) { - WriteBeginEvent(event); WriteCloseNewline(); } @@ -1482,8 +1482,7 @@ void WatWriter::WriteImport(const Import& import) { break; case ExternalKind::Event: - WriteBeginEvent(cast<EventImport>(&import)->event); - WriteCloseSpace(); + WriteEvent(cast<EventImport>(&import)->event); break; } |