diff options
-rw-r--r-- | README.md | 2 | ||||
-rw-r--r-- | include/wabt/c-writer.h | 1 | ||||
-rw-r--r-- | include/wabt/ir.h | 14 | ||||
-rw-r--r-- | src/binary-reader-ir.cc | 14 | ||||
-rw-r--r-- | src/c-writer.cc | 579 | ||||
-rw-r--r-- | src/prebuilt/wasm2c_source_declarations.cc | 52 | ||||
-rw-r--r-- | src/template/wasm2c.declarations.c | 32 | ||||
-rw-r--r-- | src/tools/wasm2c.cc | 21 | ||||
-rw-r--r-- | test/pipes.txt | 2 | ||||
-rwxr-xr-x | test/run-spec-wasm2c.py | 3 | ||||
-rw-r--r-- | test/wasm2c/add.txt | 32 | ||||
-rw-r--r-- | test/wasm2c/check-imports.txt | 34 | ||||
-rw-r--r-- | test/wasm2c/export-names.txt | 32 | ||||
-rw-r--r-- | test/wasm2c/hello.txt | 34 | ||||
-rw-r--r-- | test/wasm2c/minimal.txt | 32 | ||||
-rw-r--r-- | test/wasm2c/spec/tail-call/return_call.txt | 6 | ||||
-rw-r--r-- | test/wasm2c/spec/tail-call/return_call_indirect.txt | 6 | ||||
-rw-r--r-- | test/wasm2c/tail-calls.txt | 961 | ||||
-rw-r--r-- | wasm2c/examples/callback/main.c | 3 | ||||
-rw-r--r-- | wasm2c/examples/fac/fac.c | 32 | ||||
-rw-r--r-- | wasm2c/wasm-rt-impl.c | 2 | ||||
-rw-r--r-- | wasm2c/wasm-rt.h | 15 |
22 files changed, 1768 insertions, 141 deletions
@@ -54,7 +54,7 @@ Wabt has been compiled to JavaScript via emscripten. Some of the functionality i | [simd][] | `--disable-simd` | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | | [threads][] | `--enable-threads` | | ✓ | ✓ | ✓ | ✓ | | | [multi-value][] | `--disable-multi-value` | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | -| [tail-call][] | `--enable-tail-call` | | ✓ | ✓ | ✓ | ✓ | | +| [tail-call][] | `--enable-tail-call` | | ✓ | ✓ | ✓ | ✓ | ✓ | | [bulk memory][] | `--disable-bulk-memory` | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | | [reference types][] | `--disable-reference-types` | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | | [annotations][] | `--enable-annotations` | | | ✓ | | | | diff --git a/include/wabt/c-writer.h b/include/wabt/c-writer.h index b9fb40de..8e400674 100644 --- a/include/wabt/c-writer.h +++ b/include/wabt/c-writer.h @@ -29,6 +29,7 @@ class Stream; struct WriteCOptions { std::string_view module_name; + Features features; /* * name_to_output_file_index takes const iterators to begin and end of a list * of all functions in the module, number of imported functions, and number of diff --git a/include/wabt/ir.h b/include/wabt/ir.h index 2bc5d9bd..49b7e2b0 100644 --- a/include/wabt/ir.h +++ b/include/wabt/ir.h @@ -310,6 +310,13 @@ class FuncType : public TypeEntry { Type GetResultType(Index index) const { return sig.GetResultType(index); } FuncSignature sig; + + // The BinaryReaderIR tracks whether a FuncType is the target of a tailcall + // (via a return_call_indirect). wasm2c (CWriter) uses this information to + // limit its output in some cases. + struct { + bool tailcall = false; + } features_used; }; struct Field { @@ -893,6 +900,13 @@ struct Func { BindingHash bindings; ExprList exprs; Location loc; + + // For a subset of features, the BinaryReaderIR tracks whether they are + // actually used by the function. wasm2c (CWriter) uses this information to + // limit its output in some cases. + struct { + bool tailcall = false; + } features_used; }; struct Global { diff --git a/src/binary-reader-ir.cc b/src/binary-reader-ir.cc index 490fea34..82500b93 100644 --- a/src/binary-reader-ir.cc +++ b/src/binary-reader-ir.cc @@ -895,15 +895,29 @@ Result BinaryReaderIR::OnCallRefExpr() { } Result BinaryReaderIR::OnReturnCallExpr(Index func_index) { + if (current_func_) { + // syntactically, a return_call expr can occur in an init expression + // (outside a function) + current_func_->features_used.tailcall = true; + } return AppendExpr( std::make_unique<ReturnCallExpr>(Var(func_index, GetLocation()))); } Result BinaryReaderIR::OnReturnCallIndirectExpr(Index sig_index, Index table_index) { + if (current_func_) { + // syntactically, a return_call_indirect expr can occur in an init + // expression (outside a function) + current_func_->features_used.tailcall = true; + } auto expr = std::make_unique<ReturnCallIndirectExpr>(); SetFuncDeclaration(&expr->decl, Var(sig_index, GetLocation())); expr->table = Var(table_index, GetLocation()); + FuncType* type = module_->GetFuncType(Var(sig_index, GetLocation())); + if (type) { + type->features_used.tailcall = true; + } return AppendExpr(std::move(expr)); } diff --git a/src/c-writer.cc b/src/c-writer.cc index 446c66e1..0c92238c 100644 --- a/src/c-writer.cc +++ b/src/c-writer.cc @@ -103,6 +103,11 @@ struct ExternalRef : GlobalName { using GlobalName::GlobalName; }; +struct TailCallRef : GlobalName { + explicit TailCallRef(const std::string& name) + : GlobalName(ModuleFieldType::Func, name) {} +}; + struct ExternalInstancePtr : GlobalName { using GlobalName::GlobalName; }; @@ -265,6 +270,9 @@ class CWriter { static std::string ExportName(std::string_view module_name, std::string_view export_name); std::string ExportName(std::string_view export_name) const; + static std::string TailCallExportName(std::string_view module_name, + std::string_view export_name); + std::string TailCallExportName(std::string_view export_name) const; std::string ModuleInstanceTypeName() const; static std::string ModuleInstanceTypeName(std::string_view module_name); void ClaimName(SymbolSet& set, @@ -296,6 +304,7 @@ class CWriter { std::string GetGlobalName(ModuleFieldType, const std::string&) const; std::string GetLocalName(const std::string&, bool is_label) const; + std::string GetTailCallRef(const std::string&) const; void Indent(int size = INDENT_SIZE); void Dedent(int size = INDENT_SIZE); @@ -332,6 +341,7 @@ class CWriter { void Write(const GlobalName&); void Write(const TagSymbol&); void Write(const ExternalRef&); + void Write(const TailCallRef&); void Write(const ExternalInstancePtr&); void Write(const ExternalInstanceRef&); void Write(Type); @@ -350,6 +360,7 @@ class CWriter { void WriteMultiCTop(); void WriteMultiCTopEmpty(); void DeclareStruct(const TypeVector&); + void WriteTailcalleeParamTypes(); void WriteMultivalueResultTypes(); void WriteTagTypes(); void WriteFuncTypeDecls(); @@ -360,8 +371,10 @@ class CWriter { void ComputeUniqueImports(); void BeginInstance(); void WriteImports(); + void WriteTailCallWeakImports(); void WriteFuncDeclarations(); void WriteFuncDeclaration(const FuncDeclaration&, const std::string&); + void WriteTailCallFuncDeclaration(const std::string&); void WriteImportFuncDeclaration(const FuncDeclaration&, const std::string& module_name, const std::string&); @@ -390,6 +403,7 @@ class CWriter { void WriteElemInitializers(); void WriteElemTableInit(bool, const ElemSegment*, const Table*); void WriteExports(CWriterPhase); + void WriteTailCallExports(CWriterPhase); void WriteInitDecl(); void WriteFreeDecl(); void WriteGetFuncTypeDecl(); @@ -399,7 +413,10 @@ class CWriter { void WriteInitInstanceImport(); void WriteImportProperties(CWriterPhase); void WriteFuncs(); + void BeginFunction(const Func&); + void FinishFunction(); void Write(const Func&); + void WriteTailCallee(const Func&); void WriteParamsAndLocals(); void WriteParams(const std::vector<std::string>& index_to_name); void WriteParamSymbols(const std::vector<std::string>& index_to_name); @@ -407,14 +424,20 @@ class CWriter { void WriteLocals(const std::vector<std::string>& index_to_name); void WriteStackVarDeclarations(); void Write(const ExprList&); + void WriteTailCallAsserts(const FuncSignature&); + void WriteTailCallStack(); void WriteUnwindTryCatchStack(const Label*); - void Spill(const TypeVector&, bool); - void Unspill(const TypeVector&, bool); + void FinishReturnCall(); + void Spill(const TypeVector&); + void Unspill(const TypeVector&); + + template <typename Vars, typename TypeOf, typename ToDo> + void WriteVarsByType(const Vars&, const TypeOf&, const ToDo&); template <typename sources> - void Spill(const TypeVector&, bool, const sources& src); + void Spill(const TypeVector&, const sources& src); template <typename targets> - void Unspill(const TypeVector&, bool, const targets& tgt); + void Unspill(const TypeVector&, const targets& tgt); enum class AssignOp { Disallowed, @@ -502,6 +525,8 @@ class CWriter { name_to_output_file_index_; bool simd_used_in_header_; + + bool in_tail_callee_; }; // TODO: if WABT begins supporting debug names for labels, @@ -517,6 +542,9 @@ static constexpr char kLabelSuffix = kParamSuffix + 1; static constexpr char kGlobalSymbolPrefix[] = "w2c_"; static constexpr char kLocalSymbolPrefix[] = "var_"; static constexpr char kAdminSymbolPrefix[] = "wasm2c_"; +static constexpr char kTailCallSymbolPrefix[] = "wasm_tailcall_"; +static constexpr char kTailCallFallbackPrefix[] = "wasm_fallback_"; +static constexpr unsigned int kTailCallStackSize = 1024; size_t CWriter::MarkTypeStack() const { return type_stack_.size(); @@ -656,6 +684,18 @@ std::string CWriter::ExportName(std::string_view module_name, MangleName(export_name); } +/* The C symbol for a tail-callee export from this module. */ +std::string CWriter::TailCallExportName(std::string_view export_name) const { + return kTailCallSymbolPrefix + ExportName(export_name); +} + +/* The C symbol for a tail-callee export from an arbitrary module. */ +// static +std::string CWriter::TailCallExportName(std::string_view module_name, + std::string_view export_name) { + return kTailCallSymbolPrefix + ExportName(module_name, export_name); +} + /* The type name of an instance of this module. */ std::string CWriter::ModuleInstanceTypeName() const { return kGlobalSymbolPrefix + module_prefix_; @@ -926,6 +966,10 @@ std::string CWriter::GetLocalName(const std::string& name, return local_sym_map_.at(mangled); } +std::string CWriter::GetTailCallRef(const std::string& name) const { + return kTailCallSymbolPrefix + GetGlobalName(ModuleFieldType::Func, name); +} + std::string CWriter::DefineParamName(std::string_view name) { return DefineLocalScopeName(name, false); } @@ -1070,6 +1114,10 @@ void CWriter::Write(const ExternalRef& name) { } } +void CWriter::Write(const TailCallRef& name) { + Write(GetTailCallRef(name.name)); +} + void CWriter::Write(const ExternalInstanceRef& name) { if (IsImport(name.name)) { Write("(*instance->", GlobalName(name), ")"); @@ -1369,7 +1417,14 @@ void CWriter::WriteInitExprTerminal(const Expr* expr) { Write("(wasm_rt_funcref_t){", FuncTypeExpr(func_type), ", ", "(wasm_rt_function_ptr_t)", - ExternalRef(ModuleFieldType::Func, func->name), ", "); + ExternalRef(ModuleFieldType::Func, func->name), ", {"); + if (options_.features.tail_call_enabled() && + (IsImport(func->name) || func->features_used.tailcall)) { + Write(TailCallRef(func->name)); + } else { + Write("NULL"); + } + Write("}, "); if (IsImport(func->name)) { Write("instance->", GlobalName(ModuleFieldType::Import, @@ -1455,6 +1510,30 @@ void CWriter::DeclareStruct(const TypeVector& types) { Write(CloseBrace(), ";", Newline(), "#endif /* ", name, " */", Newline()); } +void CWriter::WriteTailcalleeParamTypes() { + // A structure for the spilled parameters of a tail-callee is needed in + // three cases: for a function that makes a tail-call (and therefore + // will have a tail-callee version generated), for any imported function + // (for which wasm2c generates a weak import that presents the tail-callee + // interface, in case the exporting module doesn't generate one itself), and + // for any type entry referenced in a return_call_indirect instruction. + + for (const Func* func : module_->funcs) { + if (IsImport(func->name) || func->features_used.tailcall) { + if (func->decl.sig.GetNumParams() > 1) { + DeclareStruct(func->decl.sig.param_types); + } + } + } + + for (TypeEntry* type : module_->types) { + FuncType* func_type = cast<FuncType>(type); + if (func_type->GetNumParams() > 1 && func_type->features_used.tailcall) { + DeclareStruct(func_type->sig.param_types); + } + } +} + void CWriter::WriteMultivalueResultTypes() { for (TypeEntry* type : module_->types) { FuncType* func_type = cast<FuncType>(type); @@ -1733,8 +1812,11 @@ void CWriter::WriteImports() { WriteImportFuncDeclaration( func.decl, import->module_name, ExportName(import->module_name, import->field_name)); - Write(";"); - Write(Newline()); + Write(";", Newline()); + if (options_.features.tail_call_enabled()) { + WriteTailCallFuncDeclaration(GetTailCallRef(func.name)); + Write(";", Newline()); + } } else if (import->kind() == ExternalKind::Tag) { Write(Newline(), "/* import: '", SanitizeForComment(import->module_name), "' '", SanitizeForComment(import->field_name), "' */", Newline()); @@ -1745,6 +1827,55 @@ void CWriter::WriteImports() { } } +void CWriter::WriteTailCallWeakImports() { + for (const Import* import : unique_imports_) { + if (import->kind() != ExternalKind::Func) { + continue; + } + const Func& func = cast<FuncImport>(import)->func; + Write(Newline(), "/* handler for missing tail-call on import: '", + SanitizeForComment(import->module_name), "' '", + SanitizeForComment(import->field_name), "' */", Newline()); + Write("WEAK_FUNC_DECL(", + TailCallExportName(import->module_name, import->field_name), ", ", + kTailCallFallbackPrefix, module_prefix_, "_", + ExportName(import->module_name, import->field_name), ")", Newline()); + Write(OpenBrace(), "next->fn = NULL;", Newline()); + + Index num_params = func.GetNumParams(); + Index num_results = func.GetNumResults(); + if (num_params >= 1) { + Write(func.decl.sig.param_types, " params;", Newline()); + Write("wasm_rt_memcpy(params, tail_call_stack, sizeof(params);", + Newline()); + } + + if (num_results >= 1) { + Write(func.decl.sig.result_types, " result = "); + } + + Write(ExportName(import->module_name, import->field_name), + "(*instance_ptr"); + + if (num_params == 1) { + Write(", params"); + } else { + for (Index i = 0; i < num_params; ++i) { + Writef(", params.%c%d", MangleType(func.GetParamType(i)), i); + } + } + + Write(");", Newline()); + + if (num_results >= 1) { + Write("wasm_rt_memcpy(tail_call_stack, &result, sizeof(result));", + Newline()); + } + + Write(CloseBrace(), Newline()); + } +} + void CWriter::WriteFuncDeclarations() { if (module_->funcs.size() == module_->num_func_imports) return; @@ -1759,6 +1890,12 @@ void CWriter::WriteFuncDeclarations() { WriteFuncDeclaration( func->decl, DefineGlobalScopeName(ModuleFieldType::Func, func->name)); Write(";", Newline()); + + if (func->features_used.tailcall) { + Write(InternalSymbolScope()); + WriteTailCallFuncDeclaration(GetTailCallRef(func->name)); + Write(";", Newline()); + } } ++func_index; } @@ -1772,6 +1909,12 @@ void CWriter::WriteFuncDeclaration(const FuncDeclaration& decl, Write(")"); } +void CWriter::WriteTailCallFuncDeclaration(const std::string& mangled_name) { + Write("void ", mangled_name, + "(void **instance_ptr, void *tail_call_stack, wasm_rt_tailcallee_t " + "*next)"); +} + void CWriter::WriteImportFuncDeclaration(const FuncDeclaration& decl, const std::string& module_name, const std::string& name) { @@ -2135,7 +2278,15 @@ void CWriter::WriteElemInitializers() { const FuncType* func_type = module_->GetFuncType(func->decl.type_var); Write("{RefFunc, ", FuncTypeExpr(func_type), ", (wasm_rt_function_ptr_t)", - ExternalRef(ModuleFieldType::Func, func->name), ", "); + ExternalRef(ModuleFieldType::Func, func->name), ", {"); + if (options_.features.tail_call_enabled() && + (IsImport(func->name) || func->features_used.tailcall)) { + Write(TailCallRef(func->name)); + } else { + Write("NULL"); + } + Write("}, "); + if (IsImport(func->name)) { Write("offsetof(", ModuleInstanceTypeName(), ", ", GlobalName(ModuleFieldType::Import, @@ -2147,14 +2298,15 @@ void CWriter::WriteElemInitializers() { Write("},", Newline()); } break; case ExprType::RefNull: - Write("{RefNull, NULL, NULL, 0},", Newline()); + Write("{RefNull, NULL, NULL, {NULL}, 0},", Newline()); break; case ExprType::GlobalGet: { const Global* global = module_->GetGlobal(cast<GlobalGetExpr>(&expr)->var); assert(IsImport(global->name)); - Write("{GlobalGet, NULL, NULL, offsetof(", ModuleInstanceTypeName(), - ", ", GlobalName(ModuleFieldType::Global, global->name), ")},", + Write("{GlobalGet, NULL, NULL, {NULL}, offsetof(", + ModuleInstanceTypeName(), ", ", + GlobalName(ModuleFieldType::Global, global->name), ")},", Newline()); } break; default: @@ -2381,6 +2533,35 @@ void CWriter::WriteExports(CWriterPhase kind) { } } +void CWriter::WriteTailCallExports(CWriterPhase kind) { + for (const Export* export_ : module_->exports) { + if (export_->kind != ExternalKind::Func) { + continue; + } + + const Func* func = module_->GetFunc(export_->var); + + if (!IsImport(func->name) && !func->features_used.tailcall) { + continue; + } + + const std::string mangled_name = TailCallExportName(export_->name); + + Write(Newline(), "/* export for tail-call of '", + SanitizeForComment(export_->name), "' */", Newline()); + if (kind == CWriterPhase::Declarations) { + WriteTailCallFuncDeclaration(mangled_name); + Write(";", Newline()); + } else { + WriteTailCallFuncDeclaration(mangled_name); + Write(" ", OpenBrace()); + const Func* func = module_->GetFunc(export_->var); + Write(TailCallRef(func->name), "(instance_ptr, tail_call_stack, next);", + Newline(), CloseBrace(), Newline()); + } + } +} + void CWriter::WriteInit() { Write(Newline(), "void ", kAdminSymbolPrefix, module_prefix_, "_instantiate(", ModuleInstanceTypeName(), "* instance"); @@ -2594,6 +2775,9 @@ void CWriter::WriteFuncs() { if (!is_import) { stream_ = c_streams_.at(c_stream_assignment.at(func_index)); Write(*func); + if (func->features_used.tailcall) { + WriteTailCallee(*func); + } } ++func_index; } @@ -2609,29 +2793,62 @@ bool CWriter::IsImport(const std::string& name) const { } template <typename sources> -void CWriter::Spill(const TypeVector& types, bool ptr, const sources& src) { +void CWriter::Spill(const TypeVector& types, const sources& src) { + assert(types.size() > 0); + + if (types.size() == 1) { + Write("tmp = ", src(0), ";", Newline()); + return; + } + for (Index i = 0; i < types.size(); ++i) { - Write("tmp", ptr ? "->" : "."); - Writef("%c%d = ", MangleType(types.at(i)), i); + Writef("tmp.%c%d = ", MangleType(types.at(i)), i); Write(src(i), ";", Newline()); } } -void CWriter::Spill(const TypeVector& types, bool ptr) { - Spill(types, ptr, [&](auto i) { return StackVar(types.size() - i - 1); }); +void CWriter::Spill(const TypeVector& types) { + Spill(types, [&](auto i) { return StackVar(types.size() - i - 1); }); } template <typename targets> -void CWriter::Unspill(const TypeVector& types, bool ptr, const targets& tgt) { +void CWriter::Unspill(const TypeVector& types, const targets& tgt) { + assert(types.size() > 0); + + if (types.size() == 1) { + Write(tgt(0), " = tmp;", Newline()); + return; + } + for (Index i = 0; i < types.size(); ++i) { - Write(tgt(i), " = tmp", ptr ? "->" : "."); - Writef("%c%d;", MangleType(types.at(i)), i); + Write(tgt(i)); + Writef(" = tmp.%c%d;", MangleType(types.at(i)), i); Write(Newline()); } } -void CWriter::Unspill(const TypeVector& types, bool ptr) { - Unspill(types, ptr, [&](auto i) { return StackVar(types.size() - i - 1); }); +void CWriter::Unspill(const TypeVector& types) { + Unspill(types, [&](auto i) { return StackVar(types.size() - i - 1); }); +} + +void CWriter::WriteTailCallAsserts(const FuncSignature& sig) { + if (sig.param_types.size()) { + Write("static_assert(sizeof(", sig.param_types, ") <= ", kTailCallStackSize, + ");", Newline()); + } + if (sig.result_types.size() && sig.result_types != sig.param_types) { + Write("static_assert(sizeof(", sig.result_types, + ") <= ", kTailCallStackSize, ");", Newline()); + } +} + +void CWriter::WriteTailCallStack() { + Write("void *instance_ptr_storage;", Newline()); + Write("void **instance_ptr = &instance_ptr_storage;", Newline()); + Write("char tail_call_stack[", std::to_string(kTailCallStackSize), "];", + Newline()); + Write("wasm_rt_tailcallee_t next_storage;", Newline()); + Write("wasm_rt_tailcallee_t *next = &next_storage;", Newline()); } void CWriter::WriteUnwindTryCatchStack(const Label* label) { @@ -2645,16 +2862,35 @@ void CWriter::WriteUnwindTryCatchStack(const Label* label) { } } -void CWriter::Write(const Func& func) { +void CWriter::FinishReturnCall() { + if (in_tail_callee_) { + Write(CloseBrace(), Newline(), "return;", Newline()); + return; + } + + Write("while (next->fn) { next->fn(instance_ptr, tail_call_stack, next); }", + Newline()); + PushTypes(func_->decl.sig.result_types); + if (!func_->decl.sig.result_types.empty()) { + Write(OpenBrace(), func_->decl.sig.result_types, " tmp;", Newline(), + "wasm_rt_memcpy(&tmp, tail_call_stack, sizeof(tmp));", Newline()); + Unspill(func_->decl.sig.result_types); + Write(CloseBrace(), Newline()); + } + + Write(CloseBrace(), Newline(), "goto ", LabelName(kImplicitFuncLabel), ";", + Newline()); +} + +void CWriter::BeginFunction(const Func& func) { func_ = &func; + in_tail_callee_ = false; local_syms_.clear(); local_sym_map_.clear(); stack_var_sym_map_.clear(); func_sections_.clear(); func_includes_.clear(); - Stream* prev_stream = stream_; - /* * If offset of stream_ is 0, this is the first time some function is written * to this stream, then write multi c top. @@ -2663,7 +2899,30 @@ void CWriter::Write(const Func& func) { WriteMultiCTop(); } Write(Newline()); +} +void CWriter::FinishFunction() { + for (size_t i = 0; i < func_sections_.size(); ++i) { + auto& [condition, stream] = func_sections_.at(i); + std::unique_ptr<OutputBuffer> buf = stream.ReleaseOutputBuffer(); + if (condition.empty() || func_includes_.count(condition)) { + stream_->WriteData(buf->data.data(), buf->data.size()); + } + + if (i == 0) { + WriteStackVarDeclarations(); // these come immediately after section #0 + // (return type/name/params/locals) + } + } + + Write(CloseBrace(), Newline()); + + func_ = nullptr; +} + +void CWriter::Write(const Func& func) { + Stream* prev_stream = stream_; + BeginFunction(func); PushFuncSection(); Write(func.decl.sig.result_types, " ", GlobalName(ModuleFieldType::Func, func.name), "("); @@ -2688,28 +2947,93 @@ void CWriter::Write(const Func& func) { Write("return ", StackVar(0), ";", Newline()); } else if (num_results >= 2) { Write(OpenBrace(), func.decl.sig.result_types, " tmp;", Newline()); - Spill(func.decl.sig.result_types, false); - Write("return tmp;", CloseBrace(), Newline()); + Spill(func.decl.sig.result_types); + Write("return tmp;", Newline(), CloseBrace(), Newline()); } stream_ = prev_stream; + FinishFunction(); +} - for (size_t i = 0; i < func_sections_.size(); ++i) { - auto& [condition, stream] = func_sections_.at(i); - std::unique_ptr<OutputBuffer> buf = stream.ReleaseOutputBuffer(); - if (condition.empty() || func_includes_.count(condition)) { - stream_->WriteData(buf->data.data(), buf->data.size()); - } +template <typename Vars, typename TypeOf, typename ToDo> +void CWriter::WriteVarsByType(const Vars& vars, + const TypeOf& typeof, + const ToDo& todo) { + for (Type type : {Type::I32, Type::I64, Type::F32, Type::F64, Type::V128, + Type::FuncRef, Type::ExternRef}) { + Index var_index = 0; + size_t count = 0; + for (const auto& var : vars) { + if (typeof(var) == type) { + if (count == 0) { + Write(type, " "); + Indent(4); + } else { + Write(", "); + if ((count % 8) == 0) + Write(Newline()); + } - if (i == 0) { - WriteStackVarDeclarations(); // these come immediately after section #0 - // (return type/name/params/locals) + todo(var_index, var); + ++count; + } + ++var_index; + } + if (count != 0) { + Dedent(4); + Write(";", Newline()); } } +} - Write(CloseBrace(), Newline()); +void CWriter::WriteTailCallee(const Func& func) { + Stream* prev_stream = stream_; + BeginFunction(func); + in_tail_callee_ = true; + PushFuncSection(); + WriteTailCallFuncDeclaration(GetTailCallRef(func.name)); + Write(" ", OpenBrace()); + WriteTailCallAsserts(func.decl.sig); + Write(ModuleInstanceTypeName(), "* instance = *instance_ptr;", Newline()); - func_ = nullptr; + std::vector<std::string> index_to_name; + MakeTypeBindingReverseMapping(func.GetNumParamsAndLocals(), func.bindings, + &index_to_name); + if (func.GetNumParams()) { + WriteVarsByType( + func.decl.sig.param_types, [](auto x) { return x; }, + [&](Index i, Type) { Write(DefineParamName(index_to_name[i])); }); + Write(OpenBrace(), func.decl.sig.param_types, " tmp;", Newline(), + "wasm_rt_memcpy(&tmp, tail_call_stack, sizeof(tmp));", Newline()); + Unspill(func.decl.sig.param_types, + [&](auto i) { return ParamName(index_to_name[i]); }); + Write(CloseBrace(), Newline()); + } + + WriteLocals(index_to_name); + + PushFuncSection(); + + std::string label = DefineLabelName(kImplicitFuncLabel); + ResetTypeStack(0); + std::string empty; // Must not be temporary, since address is taken by Label. + PushLabel(LabelType::Func, empty, func.decl.sig); + Write(func.exprs, LabelDecl(label)); + PopLabel(); + ResetTypeStack(0); + PushTypes(func.decl.sig.result_types); + + // Return the top of the stack implicitly. + if (func.GetNumResults()) { + Write(OpenBrace(), func.decl.sig.result_types, " tmp;", Newline()); + Spill(func.decl.sig.result_types); + Write("wasm_rt_memcpy(tail_call_stack, &tmp, sizeof(tmp));", Newline()); + Write(CloseBrace(), Newline()); + } + Write("next->fn = NULL;", Newline()); + + stream_ = prev_stream; + FinishFunction(); } void CWriter::WriteParamsAndLocals() { @@ -2763,21 +3087,9 @@ void CWriter::WriteParamTypes(const FuncDeclaration& decl) { void CWriter::WriteLocals(const std::vector<std::string>& index_to_name) { Index num_params = func_->GetNumParams(); - for (Type type : {Type::I32, Type::I64, Type::F32, Type::F64, Type::V128, - Type::FuncRef, Type::ExternRef}) { - Index local_index = 0; - size_t count = 0; - for (Type local_type : func_->local_types) { - if (local_type == type) { - if (count == 0) { - Write(type, " "); - Indent(4); - } else { - Write(", "); - if ((count % 8) == 0) - Write(Newline()); - } - + WriteVarsByType( + func_->local_types, [](auto x) { return x; }, + [&](Index local_index, Type local_type) { Write(DefineParamName(index_to_name[num_params + local_index]), " = "); if (local_type == Type::FuncRef || local_type == Type::ExternRef) { Write(GetReferenceNullValue(local_type)); @@ -2786,43 +3098,13 @@ void CWriter::WriteLocals(const std::vector<std::string>& index_to_name) { } else { Write("0"); } - ++count; - } - ++local_index; - } - if (count != 0) { - Dedent(4); - Write(";", Newline()); - } - } + }); } void CWriter::WriteStackVarDeclarations() { - for (Type type : {Type::I32, Type::I64, Type::F32, Type::F64, Type::V128, - Type::FuncRef, Type::ExternRef}) { - size_t count = 0; - for (const auto& [pair, name] : stack_var_sym_map_) { - Type stp_type = pair.second; - - if (stp_type == type) { - if (count == 0) { - Write(type, " "); - Indent(4); - } else { - Write(", "); - if ((count % 8) == 0) - Write(Newline()); - } - - Write(name); - ++count; - } - } - if (count != 0) { - Dedent(4); - Write(";", Newline()); - } - } + WriteVarsByType( + stack_var_sym_map_, [](auto stp_name) { return stp_name.first.second; }, + [&](Index, auto& stp_name) { Write(stp_name.second); }); } void CWriter::Write(const Block& block) { @@ -2938,7 +3220,7 @@ void CWriter::Write(const Catch& c) { } else if (num_params > 1) { Write(OpenBrace(), tag_type.sig.param_types, " tmp;", Newline()); Write("wasm_rt_memcpy(&tmp, wasm_rt_exception(), sizeof(tmp));", Newline()); - Unspill(tag_type.sig.param_types, false); + Unspill(tag_type.sig.param_types); Write(CloseBrace(), Newline()); } @@ -3077,7 +3359,7 @@ void CWriter::Write(const ExprList& exprs) { DropTypes(num_params); PushTypes(func.decl.sig.result_types); if (num_results > 1) { - Unspill(func.decl.sig.result_types, false); + Unspill(func.decl.sig.result_types); Write(CloseBrace(), Newline()); } break; @@ -3113,7 +3395,7 @@ void CWriter::Write(const ExprList& exprs) { DropTypes(num_params + 1); PushTypes(decl.sig.result_types); if (num_results > 1) { - Unspill(decl.sig.result_types, false); + Unspill(decl.sig.result_types); Write(CloseBrace(), Newline()); } break; @@ -3375,7 +3657,14 @@ void CWriter::Write(const ExprList& exprs) { Write(StackVar(0), " = (wasm_rt_funcref_t){", FuncTypeExpr(func_type), ", (wasm_rt_function_ptr_t)", - ExternalRef(ModuleFieldType::Func, func->name), ", "); + ExternalRef(ModuleFieldType::Func, func->name), ", {"); + if (options_.features.tail_call_enabled() && + (IsImport(func->name) || func->features_used.tailcall)) { + Write(TailCallRef(func->name)); + } else { + Write("NULL"); + } + Write("}, "); if (IsImport(func->name)) { Write("instance->", GlobalName(ModuleFieldType::Import, @@ -3511,7 +3800,7 @@ void CWriter::Write(const ExprList& exprs) { Newline()); } else { Write(OpenBrace(), tag->decl.sig.param_types, " tmp;", Newline()); - Spill(tag->decl.sig.param_types, false); + Spill(tag->decl.sig.param_types); Write("wasm_rt_load_exception(", TagSymbol(tag->name), ", sizeof(tmp), &tmp);", Newline(), CloseBrace(), Newline()); } @@ -3569,10 +3858,104 @@ void CWriter::Write(const ExprList& exprs) { break; } + case ExprType::ReturnCall: { + const auto inst = cast<ReturnCallExpr>(&expr); + const Func& func = *module_->GetFunc(inst->var); + + const FuncDeclaration& decl = func.decl; + assert(decl.sig.result_types == func_->decl.sig.result_types); + WriteUnwindTryCatchStack(FindLabel(Var(label_stack_.size() - 1, {}))); + + if (!IsImport(func.name) && !func.features_used.tailcall) { + // make normal call, then return + Write(ExprList{std::make_unique<CallExpr>(inst->var, inst->loc)}); + Write("goto ", LabelName(kImplicitFuncLabel), ";", Newline()); + return; + } + + WriteTailCallAsserts(decl.sig); + Write(OpenBrace()); + if (!in_tail_callee_) { + WriteTailCallStack(); + } + + const Index num_params = decl.GetNumParams(); + if (decl.GetNumParams()) { + Write(OpenBrace(), decl.sig.param_types, " tmp;", Newline()); + Spill(decl.sig.param_types); + Write("wasm_rt_memcpy(tail_call_stack, &tmp, sizeof(tmp));", + Newline()); + Write(CloseBrace(), Newline()); + } + + Write("next->fn = ", TailCallRef(func.name), ";", Newline()); + if (IsImport(func.name)) { + Write("*instance_ptr = ", + GlobalName(ModuleFieldType::Import, + import_module_sym_map_.at(func.name)), + ";", Newline()); + } + DropTypes(num_params); + FinishReturnCall(); + return; + } + + case ExprType::ReturnCallIndirect: { + const auto inst = cast<ReturnCallIndirectExpr>(&expr); + const FuncDeclaration& decl = inst->decl; + assert(decl.sig.result_types == func_->decl.sig.result_types); + assert(decl.has_func_type); + const Index num_params = decl.GetNumParams(); + WriteTailCallAsserts(decl.sig); + WriteUnwindTryCatchStack(FindLabel(Var(label_stack_.size() - 1, {}))); + const Table* table = module_->GetTable(inst->table); + Write("CHECK_CALL_INDIRECT(", + ExternalInstanceRef(ModuleFieldType::Table, table->name), ", ", + FuncTypeExpr(module_->GetFuncType(decl.type_var)), ", ", + StackVar(0), ");", Newline()); + + Write("if (!", ExternalInstanceRef(ModuleFieldType::Table, table->name), + ".data[", StackVar(0), "].func_tailcallee.fn) ", OpenBrace()); + auto ci = std::make_unique<CallIndirectExpr>(inst->loc); + std::tie(ci->decl, ci->table) = std::make_pair(inst->decl, inst->table); + Write(ExprList{std::move(ci)}); + if (in_tail_callee_) { + Write("next->fn = NULL;", Newline()); + } + Write(CloseBrace(), " else ", OpenBrace()); + + DropTypes(decl.GetNumResults()); + PushTypes(decl.sig.param_types); + PushType(Type::I32); + + if (!in_tail_callee_) { + WriteTailCallStack(); + } + + if (num_params) { + Write(OpenBrace(), decl.sig.param_types, " tmp;", Newline()); + Spill(decl.sig.param_types, + [&](auto i) { return StackVar(num_params - i); }); + Write("wasm_rt_memcpy(tail_call_stack, &tmp, sizeof(tmp));", + Newline()); + Write(CloseBrace(), Newline()); + } + + assert(decl.has_func_type); + Write("next->fn = ", + ExternalInstanceRef(ModuleFieldType::Table, table->name), + ".data[", StackVar(0), "].func_tailcallee.fn;", Newline()); + Write("*instance_ptr = ", + ExternalInstanceRef(ModuleFieldType::Table, table->name), + ".data[", StackVar(0), "].module_instance;", Newline()); + + DropTypes(num_params + 1); + FinishReturnCall(); + return; + } + case ExprType::AtomicWait: case ExprType::AtomicNotify: - case ExprType::ReturnCall: - case ExprType::ReturnCallIndirect: case ExprType::CallRef: UNIMPLEMENTED("..."); break; @@ -5353,6 +5736,9 @@ void CWriter::WriteCHeader() { WriteImports(); WriteImportProperties(CWriterPhase::Declarations); WriteExports(CWriterPhase::Declarations); + if (options_.features.tail_call_enabled()) { + WriteTailCallExports(CWriterPhase::Declarations); + } Write(Newline()); Write(s_header_bottom); Write(Newline(), "#endif /* ", guard, " */", Newline()); @@ -5371,6 +5757,9 @@ void CWriter::WriteCSource() { WriteFuncDeclarations(); WriteDataInitializerDecls(); WriteElemInitializerDecls(); + if (options_.features.tail_call_enabled()) { + WriteTailcalleeParamTypes(); + } /* Write the module-wide material to the first output stream */ stream_ = c_streams_.front(); @@ -5381,6 +5770,10 @@ void CWriter::WriteCSource() { WriteDataInitializers(); WriteElemInitializers(); WriteExports(CWriterPhase::Definitions); + if (options_.features.tail_call_enabled()) { + WriteTailCallExports(CWriterPhase::Definitions); + WriteTailCallWeakImports(); + } WriteInitInstanceImport(); WriteImportProperties(CWriterPhase::Definitions); WriteInit(); diff --git a/src/prebuilt/wasm2c_source_declarations.cc b/src/prebuilt/wasm2c_source_declarations.cc index 946421df..414e6ef0 100644 --- a/src/prebuilt/wasm2c_source_declarations.cc +++ b/src/prebuilt/wasm2c_source_declarations.cc @@ -35,15 +35,23 @@ R"w2c_template( return (a == b) || LIKELY(a && b && !memcmp(a, b, 32)); R"w2c_template(} )w2c_template" R"w2c_template( -#define CALL_INDIRECT(table, t, ft, x, ...) \ +#define CHECK_CALL_INDIRECT(table, ft, x) \ )w2c_template" R"w2c_template( (LIKELY((x) < table.size && table.data[x].func && \ )w2c_template" R"w2c_template( func_types_eq(ft, table.data[x].func_type)) || \ )w2c_template" -R"w2c_template( TRAP(CALL_INDIRECT), \ +R"w2c_template( TRAP(CALL_INDIRECT)) )w2c_template" -R"w2c_template( ((t)table.data[x].func)(__VA_ARGS__)) +R"w2c_template( +#define DO_CALL_INDIRECT(table, t, x, ...) ((t)table.data[x].func)(__VA_ARGS__) +)w2c_template" +R"w2c_template( +#define CALL_INDIRECT(table, t, ft, x, ...) \ +)w2c_template" +R"w2c_template( (CHECK_CALL_INDIRECT(table, ft, x), \ +)w2c_template" +R"w2c_template( DO_CALL_INDIRECT(table, t, x, __VA_ARGS__)) )w2c_template" R"w2c_template( #ifdef SUPPORT_MEMORY64 @@ -942,6 +950,8 @@ R"w2c_template( wasm_rt_func_type_t type; )w2c_template" R"w2c_template( wasm_rt_function_ptr_t func; )w2c_template" +R"w2c_template( wasm_rt_tailcallee_t func_tailcallee; +)w2c_template" R"w2c_template( size_t module_offset; )w2c_template" R"w2c_template(} wasm_elem_segment_expr_t; @@ -981,7 +991,7 @@ R"w2c_template( case RefFunc: )w2c_template" R"w2c_template( *dest_val = (wasm_rt_funcref_t){ )w2c_template" -R"w2c_template( src_expr->type, src_expr->func, +R"w2c_template( src_expr->type, src_expr->func, src_expr->func_tailcallee, )w2c_template" R"w2c_template( (char*)module_instance + src_expr->module_offset}; )w2c_template" @@ -1151,4 +1161,38 @@ R"w2c_template(#define FUNC_TYPE_T(x) static const char x[] )w2c_template" R"w2c_template(#endif )w2c_template" +R"w2c_template( +#if (__STDC_VERSION__ < 201112L) && !defined(static_assert) +)w2c_template" +R"w2c_template(#define static_assert(X) \ +)w2c_template" +R"w2c_template( extern int(*assertion(void))[!!sizeof(struct { int x : (X) ? 2 : -1; })]; +)w2c_template" +R"w2c_template(#endif +)w2c_template" +R"w2c_template( +#ifdef _MSC_VER +)w2c_template" +R"w2c_template(#define WEAK_FUNC_DECL(func, fallback) \ +)w2c_template" +R"w2c_template( __pragma(comment(linker, "/alternatename:" #func "=" #fallback)) \ +)w2c_template" +R"w2c_template( \ +)w2c_template" +R"w2c_template( void \ +)w2c_template" +R"w2c_template( fallback(void** instance_ptr, void* tail_call_stack, \ +)w2c_template" +R"w2c_template( wasm_rt_tailcallee_t* next) +)w2c_template" +R"w2c_template(#else +)w2c_template" +R"w2c_template(#define WEAK_FUNC_DECL(func, fallback) \ +)w2c_template" +R"w2c_template( __attribute__((weak)) void func(void** instance_ptr, void* tail_call_stack, \ +)w2c_template" +R"w2c_template( wasm_rt_tailcallee_t* next) +)w2c_template" +R"w2c_template(#endif +)w2c_template" ; diff --git a/src/template/wasm2c.declarations.c b/src/template/wasm2c.declarations.c index a0a26dff..c0a44707 100644 --- a/src/template/wasm2c.declarations.c +++ b/src/template/wasm2c.declarations.c @@ -20,11 +20,16 @@ static inline bool func_types_eq(const wasm_rt_func_type_t a, return (a == b) || LIKELY(a && b && !memcmp(a, b, 32)); } -#define CALL_INDIRECT(table, t, ft, x, ...) \ +#define CHECK_CALL_INDIRECT(table, ft, x) \ (LIKELY((x) < table.size && table.data[x].func && \ func_types_eq(ft, table.data[x].func_type)) || \ - TRAP(CALL_INDIRECT), \ - ((t)table.data[x].func)(__VA_ARGS__)) + TRAP(CALL_INDIRECT)) + +#define DO_CALL_INDIRECT(table, t, x, ...) ((t)table.data[x].func)(__VA_ARGS__) + +#define CALL_INDIRECT(table, t, ft, x, ...) \ + (CHECK_CALL_INDIRECT(table, ft, x), \ + DO_CALL_INDIRECT(table, t, x, __VA_ARGS__)) #ifdef SUPPORT_MEMORY64 #define RANGE_CHECK(mem, offset, len) \ @@ -503,6 +508,7 @@ typedef struct { enum { RefFunc, RefNull, GlobalGet } expr_type; wasm_rt_func_type_t type; wasm_rt_function_ptr_t func; + wasm_rt_tailcallee_t func_tailcallee; size_t module_offset; } wasm_elem_segment_expr_t; @@ -523,7 +529,7 @@ static inline void funcref_table_init(wasm_rt_funcref_table_t* dest, switch (src_expr->expr_type) { case RefFunc: *dest_val = (wasm_rt_funcref_t){ - src_expr->type, src_expr->func, + src_expr->type, src_expr->func, src_expr->func_tailcallee, (char*)module_instance + src_expr->module_offset}; break; case RefNull: @@ -613,3 +619,21 @@ DEFINE_TABLE_FILL(externref) #define FUNC_TYPE_EXTERN_T(x) const char x[] #define FUNC_TYPE_T(x) static const char x[] #endif + +#if (__STDC_VERSION__ < 201112L) && !defined(static_assert) +#define static_assert(X) \ + extern int(*assertion(void))[!!sizeof(struct { int x : (X) ? 2 : -1; })]; +#endif + +#ifdef _MSC_VER +#define WEAK_FUNC_DECL(func, fallback) \ + __pragma(comment(linker, "/alternatename:" #func "=" #fallback)) \ + \ + void \ + fallback(void** instance_ptr, void* tail_call_stack, \ + wasm_rt_tailcallee_t* next) +#else +#define WEAK_FUNC_DECL(func, fallback) \ + __attribute__((weak)) void func(void** instance_ptr, void* tail_call_stack, \ + wasm_rt_tailcallee_t* next) +#endif diff --git a/src/tools/wasm2c.cc b/src/tools/wasm2c.cc index 10d9210e..a76ff78a 100644 --- a/src/tools/wasm2c.cc +++ b/src/tools/wasm2c.cc @@ -41,7 +41,6 @@ static int s_verbose; static std::string s_infile; static std::string s_outfile; static unsigned int s_num_outputs = 1; -static Features s_features; static WriteCOptions s_write_c_options; static bool s_read_debug_names = true; static std::unique_ptr<FileStream> s_log_stream; @@ -61,7 +60,7 @@ examples: static const std::string supported_features[] = { "multi-memory", "multi-value", "sign-extension", "saturating-float-to-int", "exceptions", "memory64", "extended-const", "simd", - "threads"}; + "threads", "tail-call"}; static bool IsFeatureSupported(const std::string& feature) { return std::find(std::begin(supported_features), std::end(supported_features), @@ -92,7 +91,7 @@ static void ParseOptions(int argc, char** argv) { "names section is used. If that is not present the name of the input\n" "file is used as the default.\n", [](const char* argument) { s_write_c_options.module_name = argument; }); - s_features.AddOptions(&parser); + s_write_c_options.features.AddOptions(&parser); parser.AddOption("no-debug-names", "Ignore debug names in the binary file", []() { s_read_debug_names = false; }); parser.AddArgument("filename", OptionParser::ArgumentCount::One, @@ -103,10 +102,11 @@ static void ParseOptions(int argc, char** argv) { parser.Parse(argc, argv); bool any_non_supported_feature = false; -#define WABT_FEATURE(variable, flag, default_, help) \ - any_non_supported_feature |= \ - (s_features.variable##_enabled() != default_) && \ - s_features.variable##_enabled() && !IsFeatureSupported(flag); +#define WABT_FEATURE(variable, flag, default_, help) \ + any_non_supported_feature |= \ + (s_write_c_options.features.variable##_enabled() != default_) && \ + s_write_c_options.features.variable##_enabled() && \ + !IsFeatureSupported(flag); #include "wabt/feature.def" #undef WABT_FEATURE @@ -139,11 +139,12 @@ Result Wasm2cMain(Errors& errors) { Module module; const bool kStopOnFirstError = true; const bool kFailOnCustomSectionError = true; - ReadBinaryOptions options(s_features, s_log_stream.get(), s_read_debug_names, - kStopOnFirstError, kFailOnCustomSectionError); + ReadBinaryOptions options(s_write_c_options.features, s_log_stream.get(), + s_read_debug_names, kStopOnFirstError, + kFailOnCustomSectionError); CHECK_RESULT(ReadBinaryIr(s_infile.c_str(), file_data.data(), file_data.size(), options, &errors, &module)); - CHECK_RESULT(ValidateModule(&module, &errors, s_features)); + CHECK_RESULT(ValidateModule(&module, &errors, s_write_c_options.features)); CHECK_RESULT(GenerateNames(&module)); /* TODO(binji): This shouldn't fail; if a name can't be applied * (because the index is invalid, say) it should just be skipped. */ diff --git a/test/pipes.txt b/test/pipes.txt index fbe67136..07ec43f1 100644 --- a/test/pipes.txt +++ b/test/pipes.txt @@ -2,6 +2,8 @@ ;;; RUN: bash -c '%(wasm-stats)s <(%(wat2wasm)s /dev/stdin <<<"(module)" -o /dev/stdout)' (;; STDOUT ;;; Total opcodes: 0 + Opcode counts: + Opcode counts with immediates: ;;; STDOUT ;;) diff --git a/test/run-spec-wasm2c.py b/test/run-spec-wasm2c.py index fec091ba..533ec045 100755 --- a/test/run-spec-wasm2c.py +++ b/test/run-spec-wasm2c.py @@ -534,6 +534,7 @@ def main(args): parser.add_argument('--enable-memory64', action='store_true') parser.add_argument('--enable-extended-const', action='store_true') parser.add_argument('--enable-threads', action='store_true') + parser.add_argument('--enable-tail-call', action='store_true') parser.add_argument('--disable-bulk-memory', action='store_true') parser.add_argument('--disable-reference-types', action='store_true') parser.add_argument('--debug-names', action='store_true') @@ -554,6 +555,7 @@ def main(args): '--enable-memory64': options.enable_memory64, '--enable-extended-const': options.enable_extended_const, '--enable-threads': options.enable_threads, + '--enable-tail-call': options.enable_tail_call, '--enable-multi-memory': options.enable_multi_memory, '--disable-bulk-memory': options.disable_bulk_memory, '--disable-reference-types': options.disable_reference_types, @@ -572,6 +574,7 @@ def main(args): '--enable-memory64': options.enable_memory64, '--enable-extended-const': options.enable_extended_const, '--enable-threads': options.enable_threads, + '--enable-tail-call': options.enable_tail_call, '--enable-multi-memory': options.enable_multi_memory}) options.cflags += shlex.split(os.environ.get('WASM2C_CFLAGS', '')) diff --git a/test/wasm2c/add.txt b/test/wasm2c/add.txt index e400a92e..b7ff8d9b 100644 --- a/test/wasm2c/add.txt +++ b/test/wasm2c/add.txt @@ -87,11 +87,16 @@ static inline bool func_types_eq(const wasm_rt_func_type_t a, return (a == b) || LIKELY(a && b && !memcmp(a, b, 32)); } -#define CALL_INDIRECT(table, t, ft, x, ...) \ +#define CHECK_CALL_INDIRECT(table, ft, x) \ (LIKELY((x) < table.size && table.data[x].func && \ func_types_eq(ft, table.data[x].func_type)) || \ - TRAP(CALL_INDIRECT), \ - ((t)table.data[x].func)(__VA_ARGS__)) + TRAP(CALL_INDIRECT)) + +#define DO_CALL_INDIRECT(table, t, x, ...) ((t)table.data[x].func)(__VA_ARGS__) + +#define CALL_INDIRECT(table, t, ft, x, ...) \ + (CHECK_CALL_INDIRECT(table, ft, x), \ + DO_CALL_INDIRECT(table, t, x, __VA_ARGS__)) #ifdef SUPPORT_MEMORY64 #define RANGE_CHECK(mem, offset, len) \ @@ -570,6 +575,7 @@ typedef struct { enum { RefFunc, RefNull, GlobalGet } expr_type; wasm_rt_func_type_t type; wasm_rt_function_ptr_t func; + wasm_rt_tailcallee_t func_tailcallee; size_t module_offset; } wasm_elem_segment_expr_t; @@ -590,7 +596,7 @@ static inline void funcref_table_init(wasm_rt_funcref_table_t* dest, switch (src_expr->expr_type) { case RefFunc: *dest_val = (wasm_rt_funcref_t){ - src_expr->type, src_expr->func, + src_expr->type, src_expr->func, src_expr->func_tailcallee, (char*)module_instance + src_expr->module_offset}; break; case RefNull: @@ -681,6 +687,24 @@ DEFINE_TABLE_FILL(externref) #define FUNC_TYPE_T(x) static const char x[] #endif +#if (__STDC_VERSION__ < 201112L) && !defined(static_assert) +#define static_assert(X) \ + extern int(*assertion(void))[!!sizeof(struct { int x : (X) ? 2 : -1; })]; +#endif + +#ifdef _MSC_VER +#define WEAK_FUNC_DECL(func, fallback) \ + __pragma(comment(linker, "/alternatename:" #func "=" #fallback)) \ + \ + void \ + fallback(void** instance_ptr, void* tail_call_stack, \ + wasm_rt_tailcallee_t* next) +#else +#define WEAK_FUNC_DECL(func, fallback) \ + __attribute__((weak)) void func(void** instance_ptr, void* tail_call_stack, \ + wasm_rt_tailcallee_t* next) +#endif + static u32 w2c_test_add_0(w2c_test*, u32, u32); FUNC_TYPE_T(w2c_test_t0) = "\x92\xfb\x6a\xdf\x49\x07\x0a\x83\xbe\x08\x02\x68\xcd\xf6\x95\x27\x4a\xc2\xf3\xe5\xe4\x7d\x29\x49\xe8\xed\x42\x92\x6a\x9d\xda\xf0"; diff --git a/test/wasm2c/check-imports.txt b/test/wasm2c/check-imports.txt index f6761275..60d38348 100644 --- a/test/wasm2c/check-imports.txt +++ b/test/wasm2c/check-imports.txt @@ -110,11 +110,16 @@ static inline bool func_types_eq(const wasm_rt_func_type_t a, return (a == b) || LIKELY(a && b && !memcmp(a, b, 32)); } -#define CALL_INDIRECT(table, t, ft, x, ...) \ +#define CHECK_CALL_INDIRECT(table, ft, x) \ (LIKELY((x) < table.size && table.data[x].func && \ func_types_eq(ft, table.data[x].func_type)) || \ - TRAP(CALL_INDIRECT), \ - ((t)table.data[x].func)(__VA_ARGS__)) + TRAP(CALL_INDIRECT)) + +#define DO_CALL_INDIRECT(table, t, x, ...) ((t)table.data[x].func)(__VA_ARGS__) + +#define CALL_INDIRECT(table, t, ft, x, ...) \ + (CHECK_CALL_INDIRECT(table, ft, x), \ + DO_CALL_INDIRECT(table, t, x, __VA_ARGS__)) #ifdef SUPPORT_MEMORY64 #define RANGE_CHECK(mem, offset, len) \ @@ -593,6 +598,7 @@ typedef struct { enum { RefFunc, RefNull, GlobalGet } expr_type; wasm_rt_func_type_t type; wasm_rt_function_ptr_t func; + wasm_rt_tailcallee_t func_tailcallee; size_t module_offset; } wasm_elem_segment_expr_t; @@ -613,7 +619,7 @@ static inline void funcref_table_init(wasm_rt_funcref_table_t* dest, switch (src_expr->expr_type) { case RefFunc: *dest_val = (wasm_rt_funcref_t){ - src_expr->type, src_expr->func, + src_expr->type, src_expr->func, src_expr->func_tailcallee, (char*)module_instance + src_expr->module_offset}; break; case RefNull: @@ -704,6 +710,24 @@ DEFINE_TABLE_FILL(externref) #define FUNC_TYPE_T(x) static const char x[] #endif +#if (__STDC_VERSION__ < 201112L) && !defined(static_assert) +#define static_assert(X) \ + extern int(*assertion(void))[!!sizeof(struct { int x : (X) ? 2 : -1; })]; +#endif + +#ifdef _MSC_VER +#define WEAK_FUNC_DECL(func, fallback) \ + __pragma(comment(linker, "/alternatename:" #func "=" #fallback)) \ + \ + void \ + fallback(void** instance_ptr, void* tail_call_stack, \ + wasm_rt_tailcallee_t* next) +#else +#define WEAK_FUNC_DECL(func, fallback) \ + __attribute__((weak)) void func(void** instance_ptr, void* tail_call_stack, \ + wasm_rt_tailcallee_t* next) +#endif + static u32 w2c_test_f0(w2c_test*, u32); static u32 w2c_test_f1(w2c_test*); static u32 w2c_test_f2(w2c_test*, u32, u32); @@ -716,7 +740,7 @@ static void init_memories(w2c_test* instance) { } static const wasm_elem_segment_expr_t elem_segment_exprs_w2c_test_e0[] = { - {RefFunc, w2c_test_t1, (wasm_rt_function_ptr_t)w2c_test_f1, 0}, + {RefFunc, w2c_test_t1, (wasm_rt_function_ptr_t)w2c_test_f1, {NULL}, 0}, }; static void init_tables(w2c_test* instance) { diff --git a/test/wasm2c/export-names.txt b/test/wasm2c/export-names.txt index 41fccd08..89501325 100644 --- a/test/wasm2c/export-names.txt +++ b/test/wasm2c/export-names.txt @@ -110,11 +110,16 @@ static inline bool func_types_eq(const wasm_rt_func_type_t a, return (a == b) || LIKELY(a && b && !memcmp(a, b, 32)); } -#define CALL_INDIRECT(table, t, ft, x, ...) \ +#define CHECK_CALL_INDIRECT(table, ft, x) \ (LIKELY((x) < table.size && table.data[x].func && \ func_types_eq(ft, table.data[x].func_type)) || \ - TRAP(CALL_INDIRECT), \ - ((t)table.data[x].func)(__VA_ARGS__)) + TRAP(CALL_INDIRECT)) + +#define DO_CALL_INDIRECT(table, t, x, ...) ((t)table.data[x].func)(__VA_ARGS__) + +#define CALL_INDIRECT(table, t, ft, x, ...) \ + (CHECK_CALL_INDIRECT(table, ft, x), \ + DO_CALL_INDIRECT(table, t, x, __VA_ARGS__)) #ifdef SUPPORT_MEMORY64 #define RANGE_CHECK(mem, offset, len) \ @@ -593,6 +598,7 @@ typedef struct { enum { RefFunc, RefNull, GlobalGet } expr_type; wasm_rt_func_type_t type; wasm_rt_function_ptr_t func; + wasm_rt_tailcallee_t func_tailcallee; size_t module_offset; } wasm_elem_segment_expr_t; @@ -613,7 +619,7 @@ static inline void funcref_table_init(wasm_rt_funcref_table_t* dest, switch (src_expr->expr_type) { case RefFunc: *dest_val = (wasm_rt_funcref_t){ - src_expr->type, src_expr->func, + src_expr->type, src_expr->func, src_expr->func_tailcallee, (char*)module_instance + src_expr->module_offset}; break; case RefNull: @@ -704,6 +710,24 @@ DEFINE_TABLE_FILL(externref) #define FUNC_TYPE_T(x) static const char x[] #endif +#if (__STDC_VERSION__ < 201112L) && !defined(static_assert) +#define static_assert(X) \ + extern int(*assertion(void))[!!sizeof(struct { int x : (X) ? 2 : -1; })]; +#endif + +#ifdef _MSC_VER +#define WEAK_FUNC_DECL(func, fallback) \ + __pragma(comment(linker, "/alternatename:" #func "=" #fallback)) \ + \ + void \ + fallback(void** instance_ptr, void* tail_call_stack, \ + wasm_rt_tailcallee_t* next) +#else +#define WEAK_FUNC_DECL(func, fallback) \ + __attribute__((weak)) void func(void** instance_ptr, void* tail_call_stack, \ + wasm_rt_tailcallee_t* next) +#endif + static void w2c_test__0(w2c_test*); FUNC_TYPE_T(w2c_test_t0) = "\x36\xa9\xe7\xf1\xc9\x5b\x82\xff\xb9\x97\x43\xe0\xc5\xc4\xce\x95\xd8\x3c\x9a\x43\x0a\xac\x59\xf8\x4e\xf3\xcb\xfa\xb6\x14\x50\x68"; diff --git a/test/wasm2c/hello.txt b/test/wasm2c/hello.txt index fb14aede..f11c9005 100644 --- a/test/wasm2c/hello.txt +++ b/test/wasm2c/hello.txt @@ -118,11 +118,16 @@ static inline bool func_types_eq(const wasm_rt_func_type_t a, return (a == b) || LIKELY(a && b && !memcmp(a, b, 32)); } -#define CALL_INDIRECT(table, t, ft, x, ...) \ +#define CHECK_CALL_INDIRECT(table, ft, x) \ (LIKELY((x) < table.size && table.data[x].func && \ func_types_eq(ft, table.data[x].func_type)) || \ - TRAP(CALL_INDIRECT), \ - ((t)table.data[x].func)(__VA_ARGS__)) + TRAP(CALL_INDIRECT)) + +#define DO_CALL_INDIRECT(table, t, x, ...) ((t)table.data[x].func)(__VA_ARGS__) + +#define CALL_INDIRECT(table, t, ft, x, ...) \ + (CHECK_CALL_INDIRECT(table, ft, x), \ + DO_CALL_INDIRECT(table, t, x, __VA_ARGS__)) #ifdef SUPPORT_MEMORY64 #define RANGE_CHECK(mem, offset, len) \ @@ -601,6 +606,7 @@ typedef struct { enum { RefFunc, RefNull, GlobalGet } expr_type; wasm_rt_func_type_t type; wasm_rt_function_ptr_t func; + wasm_rt_tailcallee_t func_tailcallee; size_t module_offset; } wasm_elem_segment_expr_t; @@ -621,7 +627,7 @@ static inline void funcref_table_init(wasm_rt_funcref_table_t* dest, switch (src_expr->expr_type) { case RefFunc: *dest_val = (wasm_rt_funcref_t){ - src_expr->type, src_expr->func, + src_expr->type, src_expr->func, src_expr->func_tailcallee, (char*)module_instance + src_expr->module_offset}; break; case RefNull: @@ -712,6 +718,24 @@ DEFINE_TABLE_FILL(externref) #define FUNC_TYPE_T(x) static const char x[] #endif +#if (__STDC_VERSION__ < 201112L) && !defined(static_assert) +#define static_assert(X) \ + extern int(*assertion(void))[!!sizeof(struct { int x : (X) ? 2 : -1; })]; +#endif + +#ifdef _MSC_VER +#define WEAK_FUNC_DECL(func, fallback) \ + __pragma(comment(linker, "/alternatename:" #func "=" #fallback)) \ + \ + void \ + fallback(void** instance_ptr, void* tail_call_stack, \ + wasm_rt_tailcallee_t* next) +#else +#define WEAK_FUNC_DECL(func, fallback) \ + __attribute__((weak)) void func(void** instance_ptr, void* tail_call_stack, \ + wasm_rt_tailcallee_t* next) +#endif + static void w2c_test_0x5Fstart_0(w2c_test*); FUNC_TYPE_T(w2c_test_t0) = "\xf6\x98\x1b\xc6\x10\xda\xb7\xb2\x63\x37\xcd\xdc\x72\xca\xe9\x50\x00\x13\xba\x10\x6c\xde\x87\x27\x10\xf8\x86\x2f\xe3\xdb\x94\xe4"; @@ -732,7 +756,7 @@ static void init_data_instances(w2c_test *instance) { } static const wasm_elem_segment_expr_t elem_segment_exprs_w2c_test_e0[] = { - {RefFunc, w2c_test_t0, (wasm_rt_function_ptr_t)w2c_wasi__snapshot__preview1_fd_write, offsetof(w2c_test, w2c_wasi__snapshot__preview1_instance)}, + {RefFunc, w2c_test_t0, (wasm_rt_function_ptr_t)w2c_wasi__snapshot__preview1_fd_write, {NULL}, offsetof(w2c_test, w2c_wasi__snapshot__preview1_instance)}, }; static void init_tables(w2c_test* instance) { diff --git a/test/wasm2c/minimal.txt b/test/wasm2c/minimal.txt index f206bf13..71e641d9 100644 --- a/test/wasm2c/minimal.txt +++ b/test/wasm2c/minimal.txt @@ -81,11 +81,16 @@ static inline bool func_types_eq(const wasm_rt_func_type_t a, return (a == b) || LIKELY(a && b && !memcmp(a, b, 32)); } -#define CALL_INDIRECT(table, t, ft, x, ...) \ +#define CHECK_CALL_INDIRECT(table, ft, x) \ (LIKELY((x) < table.size && table.data[x].func && \ func_types_eq(ft, table.data[x].func_type)) || \ - TRAP(CALL_INDIRECT), \ - ((t)table.data[x].func)(__VA_ARGS__)) + TRAP(CALL_INDIRECT)) + +#define DO_CALL_INDIRECT(table, t, x, ...) ((t)table.data[x].func)(__VA_ARGS__) + +#define CALL_INDIRECT(table, t, ft, x, ...) \ + (CHECK_CALL_INDIRECT(table, ft, x), \ + DO_CALL_INDIRECT(table, t, x, __VA_ARGS__)) #ifdef SUPPORT_MEMORY64 #define RANGE_CHECK(mem, offset, len) \ @@ -564,6 +569,7 @@ typedef struct { enum { RefFunc, RefNull, GlobalGet } expr_type; wasm_rt_func_type_t type; wasm_rt_function_ptr_t func; + wasm_rt_tailcallee_t func_tailcallee; size_t module_offset; } wasm_elem_segment_expr_t; @@ -584,7 +590,7 @@ static inline void funcref_table_init(wasm_rt_funcref_table_t* dest, switch (src_expr->expr_type) { case RefFunc: *dest_val = (wasm_rt_funcref_t){ - src_expr->type, src_expr->func, + src_expr->type, src_expr->func, src_expr->func_tailcallee, (char*)module_instance + src_expr->module_offset}; break; case RefNull: @@ -675,6 +681,24 @@ DEFINE_TABLE_FILL(externref) #define FUNC_TYPE_T(x) static const char x[] #endif +#if (__STDC_VERSION__ < 201112L) && !defined(static_assert) +#define static_assert(X) \ + extern int(*assertion(void))[!!sizeof(struct { int x : (X) ? 2 : -1; })]; +#endif + +#ifdef _MSC_VER +#define WEAK_FUNC_DECL(func, fallback) \ + __pragma(comment(linker, "/alternatename:" #func "=" #fallback)) \ + \ + void \ + fallback(void** instance_ptr, void* tail_call_stack, \ + wasm_rt_tailcallee_t* next) +#else +#define WEAK_FUNC_DECL(func, fallback) \ + __attribute__((weak)) void func(void** instance_ptr, void* tail_call_stack, \ + wasm_rt_tailcallee_t* next) +#endif + void wasm2c_test_instantiate(w2c_test* instance) { assert(wasm_rt_is_initialized()); } diff --git a/test/wasm2c/spec/tail-call/return_call.txt b/test/wasm2c/spec/tail-call/return_call.txt new file mode 100644 index 00000000..6218eeac --- /dev/null +++ b/test/wasm2c/spec/tail-call/return_call.txt @@ -0,0 +1,6 @@ +;;; TOOL: run-spec-wasm2c +;;; STDIN_FILE: third_party/testsuite/proposals/tail-call/return_call.wast +;;; ARGS*: --enable-tail-call +(;; STDOUT ;;; +31/31 tests passed. +;;; STDOUT ;;) diff --git a/test/wasm2c/spec/tail-call/return_call_indirect.txt b/test/wasm2c/spec/tail-call/return_call_indirect.txt new file mode 100644 index 00000000..8cf46bd9 --- /dev/null +++ b/test/wasm2c/spec/tail-call/return_call_indirect.txt @@ -0,0 +1,6 @@ +;;; TOOL: run-spec-wasm2c +;;; STDIN_FILE: third_party/testsuite/proposals/tail-call/return_call_indirect.wast +;;; ARGS*: --enable-tail-call +(;; STDOUT ;;; +47/47 tests passed. +;;; STDOUT ;;) diff --git a/test/wasm2c/tail-calls.txt b/test/wasm2c/tail-calls.txt new file mode 100644 index 00000000..9bb317c5 --- /dev/null +++ b/test/wasm2c/tail-calls.txt @@ -0,0 +1,961 @@ +;;; TOOL: run-wasm2c +;;; ARGS0: --debug-names +;;; ARGS*: --enable-tail-call +(type $i32_f32 (func (param i32 f32))) +(import "spectest" "print_i32_f32" (func $print_i32_f32 (type $i32_f32))) +(table $tab funcref (elem $print_i32_f32)) +(func (export "tailcaller") + (return_call_indirect (type $i32_f32) + (i32.const 1) (f32.const 2.0) (i32.const 0))) +(func $infiniteloop (param i32 f64) (result i32 f64) + (return_call $infiniteloop (local.get 0) (local.get 1))) +(;; STDOUT ;;; +/* Automatically generated by wasm2c */ +#ifndef WASM_H_GENERATED_ +#define WASM_H_GENERATED_ + +#include "wasm-rt.h" + +#include <stdint.h> + +#ifndef WASM_RT_CORE_TYPES_DEFINED +#define WASM_RT_CORE_TYPES_DEFINED +typedef uint8_t u8; +typedef int8_t s8; +typedef uint16_t u16; +typedef int16_t s16; +typedef uint32_t u32; +typedef int32_t s32; +typedef uint64_t u64; +typedef int64_t s64; +typedef float f32; +typedef double f64; +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +struct w2c_spectest; + +typedef struct w2c_test { + struct w2c_spectest* w2c_spectest_instance; + wasm_rt_funcref_table_t w2c_tab; +} w2c_test; + +void wasm2c_test_instantiate(w2c_test*, struct w2c_spectest*); +void wasm2c_test_free(w2c_test*); +wasm_rt_func_type_t wasm2c_test_get_func_type(uint32_t param_count, uint32_t result_count, ...); + +#ifndef wasm_multi_id +#define wasm_multi_id wasm_multi_id +struct wasm_multi_id { + u32 i0; + f64 d1; +}; +#endif /* wasm_multi_id */ + +/* import: 'spectest' 'print_i32_f32' */ +void w2c_spectest_print_i32_f32(struct w2c_spectest*, u32, f32); +void wasm_tailcall_w2c_spectest_print_i32_f32(void **instance_ptr, void *tail_call_stack, wasm_rt_tailcallee_t *next); + +/* export: 'tailcaller' */ +void w2c_test_tailcaller(w2c_test*); + +/* export for tail-call of 'tailcaller' */ +void wasm_tailcall_w2c_test_tailcaller(void **instance_ptr, void *tail_call_stack, wasm_rt_tailcallee_t *next); + +#ifdef __cplusplus +} +#endif + +#endif /* WASM_H_GENERATED_ */ +/* Automatically generated by wasm2c */ +#include <assert.h> +#include <math.h> +#include <stdarg.h> +#include <stddef.h> +#include <string.h> +#if defined(__MINGW32__) +#include <malloc.h> +#elif defined(_MSC_VER) +#include <intrin.h> +#include <malloc.h> +#define alloca _alloca +#elif defined(__FreeBSD__) || defined(__OpenBSD__) +#include <stdlib.h> +#else +#include <alloca.h> +#endif + +#include "wasm.h" + +#define TRAP(x) (wasm_rt_trap(WASM_RT_TRAP_##x), 0) + +#if WASM_RT_USE_STACK_DEPTH_COUNT +#define FUNC_PROLOGUE \ + if (++wasm_rt_call_stack_depth > WASM_RT_MAX_CALL_STACK_DEPTH) \ + TRAP(EXHAUSTION); + +#define FUNC_EPILOGUE --wasm_rt_call_stack_depth +#else +#define FUNC_PROLOGUE + +#define FUNC_EPILOGUE +#endif + +#define UNREACHABLE TRAP(UNREACHABLE) + +static inline bool func_types_eq(const wasm_rt_func_type_t a, + const wasm_rt_func_type_t b) { + return (a == b) || LIKELY(a && b && !memcmp(a, b, 32)); +} + +#define CHECK_CALL_INDIRECT(table, ft, x) \ + (LIKELY((x) < table.size && table.data[x].func && \ + func_types_eq(ft, table.data[x].func_type)) || \ + TRAP(CALL_INDIRECT)) + +#define DO_CALL_INDIRECT(table, t, x, ...) ((t)table.data[x].func)(__VA_ARGS__) + +#define CALL_INDIRECT(table, t, ft, x, ...) \ + (CHECK_CALL_INDIRECT(table, ft, x), \ + DO_CALL_INDIRECT(table, t, x, __VA_ARGS__)) + +#ifdef SUPPORT_MEMORY64 +#define RANGE_CHECK(mem, offset, len) \ + do { \ + uint64_t res; \ + if (__builtin_add_overflow(offset, len, &res)) \ + TRAP(OOB); \ + if (UNLIKELY(res > mem->size)) \ + TRAP(OOB); \ + } while (0); +#else +#define RANGE_CHECK(mem, offset, len) \ + if (UNLIKELY(offset + (uint64_t)len > mem->size)) \ + TRAP(OOB); +#endif + +#if WASM_RT_MEMCHECK_GUARD_PAGES +#define MEMCHECK(mem, a, t) +#else +#define MEMCHECK(mem, a, t) RANGE_CHECK(mem, a, sizeof(t)) +#endif + +#ifdef __GNUC__ +#define FORCE_READ_INT(var) __asm__("" ::"r"(var)); +// Clang on Mips requires "f" constraints on floats +// See https://github.com/llvm/llvm-project/issues/64241 +#if defined(__clang__) && \ + (defined(mips) || defined(__mips__) || defined(__mips)) +#define FORCE_READ_FLOAT(var) __asm__("" ::"f"(var)); +#else +#define FORCE_READ_FLOAT(var) __asm__("" ::"r"(var)); +#endif +#else +#define FORCE_READ_INT(var) +#define FORCE_READ_FLOAT(var) +#endif + +#if WABT_BIG_ENDIAN +static inline void load_data(void* dest, const void* src, size_t n) { + if (!n) { + return; + } + size_t i = 0; + u8* dest_chars = dest; + wasm_rt_memcpy(dest, src, n); + for (i = 0; i < (n >> 1); i++) { + u8 cursor = dest_chars[i]; + dest_chars[i] = dest_chars[n - i - 1]; + dest_chars[n - i - 1] = cursor; + } +} +#define LOAD_DATA(m, o, i, s) \ + do { \ + RANGE_CHECK((&m), m.size - o - s, s); \ + load_data(&(m.data[m.size - o - s]), i, s); \ + } while (0) +#define DEFINE_LOAD(name, t1, t2, t3, force_read) \ + static inline t3 name(wasm_rt_memory_t* mem, u64 addr) { \ + MEMCHECK(mem, addr, t1); \ + t1 result; \ + wasm_rt_memcpy(&result, &mem->data[mem->size - addr - sizeof(t1)], \ + sizeof(t1)); \ + force_read(result); \ + return (t3)(t2)result; \ + } + +#define DEFINE_STORE(name, t1, t2) \ + static inline void name(wasm_rt_memory_t* mem, u64 addr, t2 value) { \ + MEMCHECK(mem, addr, t1); \ + t1 wrapped = (t1)value; \ + wasm_rt_memcpy(&mem->data[mem->size - addr - sizeof(t1)], &wrapped, \ + sizeof(t1)); \ + } +#else +static inline void load_data(void* dest, const void* src, size_t n) { + if (!n) { + return; + } + wasm_rt_memcpy(dest, src, n); +} +#define LOAD_DATA(m, o, i, s) \ + do { \ + RANGE_CHECK((&m), o, s); \ + load_data(&(m.data[o]), i, s); \ + } while (0) +#define DEFINE_LOAD(name, t1, t2, t3, force_read) \ + static inline t3 name(wasm_rt_memory_t* mem, u64 addr) { \ + MEMCHECK(mem, addr, t1); \ + t1 result; \ + wasm_rt_memcpy(&result, &mem->data[addr], sizeof(t1)); \ + force_read(result); \ + return (t3)(t2)result; \ + } + +#define DEFINE_STORE(name, t1, t2) \ + static inline void name(wasm_rt_memory_t* mem, u64 addr, t2 value) { \ + MEMCHECK(mem, addr, t1); \ + t1 wrapped = (t1)value; \ + wasm_rt_memcpy(&mem->data[addr], &wrapped, sizeof(t1)); \ + } +#endif + +DEFINE_LOAD(i32_load, u32, u32, u32, FORCE_READ_INT) +DEFINE_LOAD(i64_load, u64, u64, u64, FORCE_READ_INT) +DEFINE_LOAD(f32_load, f32, f32, f32, FORCE_READ_FLOAT) +DEFINE_LOAD(f64_load, f64, f64, f64, FORCE_READ_FLOAT) +DEFINE_LOAD(i32_load8_s, s8, s32, u32, FORCE_READ_INT) +DEFINE_LOAD(i64_load8_s, s8, s64, u64, FORCE_READ_INT) +DEFINE_LOAD(i32_load8_u, u8, u32, u32, FORCE_READ_INT) +DEFINE_LOAD(i64_load8_u, u8, u64, u64, FORCE_READ_INT) +DEFINE_LOAD(i32_load16_s, s16, s32, u32, FORCE_READ_INT) +DEFINE_LOAD(i64_load16_s, s16, s64, u64, FORCE_READ_INT) +DEFINE_LOAD(i32_load16_u, u16, u32, u32, FORCE_READ_INT) +DEFINE_LOAD(i64_load16_u, u16, u64, u64, FORCE_READ_INT) +DEFINE_LOAD(i64_load32_s, s32, s64, u64, FORCE_READ_INT) +DEFINE_LOAD(i64_load32_u, u32, u64, u64, FORCE_READ_INT) +DEFINE_STORE(i32_store, u32, u32) +DEFINE_STORE(i64_store, u64, u64) +DEFINE_STORE(f32_store, f32, f32) +DEFINE_STORE(f64_store, f64, f64) +DEFINE_STORE(i32_store8, u8, u32) +DEFINE_STORE(i32_store16, u16, u32) +DEFINE_STORE(i64_store8, u8, u64) +DEFINE_STORE(i64_store16, u16, u64) +DEFINE_STORE(i64_store32, u32, u64) + +#if defined(_MSC_VER) + +// Adapted from +// https://github.com/nemequ/portable-snippets/blob/master/builtin/builtin.h + +static inline int I64_CLZ(unsigned long long v) { + unsigned long r = 0; +#if defined(_M_AMD64) || defined(_M_ARM) + if (_BitScanReverse64(&r, v)) { + return 63 - r; + } +#else + if (_BitScanReverse(&r, (unsigned long)(v >> 32))) { + return 31 - r; + } else if (_BitScanReverse(&r, (unsigned long)v)) { + return 63 - r; + } +#endif + return 64; +} + +static inline int I32_CLZ(unsigned long v) { + unsigned long r = 0; + if (_BitScanReverse(&r, v)) { + return 31 - r; + } + return 32; +} + +static inline int I64_CTZ(unsigned long long v) { + if (!v) { + return 64; + } + unsigned long r = 0; +#if defined(_M_AMD64) || defined(_M_ARM) + _BitScanForward64(&r, v); + return (int)r; +#else + if (_BitScanForward(&r, (unsigned int)(v))) { + return (int)(r); + } + + _BitScanForward(&r, (unsigned int)(v >> 32)); + return (int)(r + 32); +#endif +} + +static inline int I32_CTZ(unsigned long v) { + if (!v) { + return 32; + } + unsigned long r = 0; + _BitScanForward(&r, v); + return (int)r; +} + +#define POPCOUNT_DEFINE_PORTABLE(f_n, T) \ + static inline u32 f_n(T x) { \ + x = x - ((x >> 1) & (T) ~(T)0 / 3); \ + x = (x & (T) ~(T)0 / 15 * 3) + ((x >> 2) & (T) ~(T)0 / 15 * 3); \ + x = (x + (x >> 4)) & (T) ~(T)0 / 255 * 15; \ + return (T)(x * ((T) ~(T)0 / 255)) >> (sizeof(T) - 1) * 8; \ + } + +POPCOUNT_DEFINE_PORTABLE(I32_POPCNT, u32) +POPCOUNT_DEFINE_PORTABLE(I64_POPCNT, u64) + +#undef POPCOUNT_DEFINE_PORTABLE + +#else + +#define I32_CLZ(x) ((x) ? __builtin_clz(x) : 32) +#define I64_CLZ(x) ((x) ? __builtin_clzll(x) : 64) +#define I32_CTZ(x) ((x) ? __builtin_ctz(x) : 32) +#define I64_CTZ(x) ((x) ? __builtin_ctzll(x) : 64) +#define I32_POPCNT(x) (__builtin_popcount(x)) +#define I64_POPCNT(x) (__builtin_popcountll(x)) + +#endif + +#define DIV_S(ut, min, x, y) \ + ((UNLIKELY((y) == 0)) \ + ? TRAP(DIV_BY_ZERO) \ + : (UNLIKELY((x) == min && (y) == -1)) ? TRAP(INT_OVERFLOW) \ + : (ut)((x) / (y))) + +#define REM_S(ut, min, x, y) \ + ((UNLIKELY((y) == 0)) \ + ? TRAP(DIV_BY_ZERO) \ + : (UNLIKELY((x) == min && (y) == -1)) ? 0 : (ut)((x) % (y))) + +#define I32_DIV_S(x, y) DIV_S(u32, INT32_MIN, (s32)x, (s32)y) +#define I64_DIV_S(x, y) DIV_S(u64, INT64_MIN, (s64)x, (s64)y) +#define I32_REM_S(x, y) REM_S(u32, INT32_MIN, (s32)x, (s32)y) +#define I64_REM_S(x, y) REM_S(u64, INT64_MIN, (s64)x, (s64)y) + +#define DIVREM_U(op, x, y) \ + ((UNLIKELY((y) == 0)) ? TRAP(DIV_BY_ZERO) : ((x)op(y))) + +#define DIV_U(x, y) DIVREM_U(/, x, y) +#define REM_U(x, y) DIVREM_U(%, x, y) + +#define ROTL(x, y, mask) \ + (((x) << ((y) & (mask))) | ((x) >> (((mask) - (y) + 1) & (mask)))) +#define ROTR(x, y, mask) \ + (((x) >> ((y) & (mask))) | ((x) << (((mask) - (y) + 1) & (mask)))) + +#define I32_ROTL(x, y) ROTL(x, y, 31) +#define I64_ROTL(x, y) ROTL(x, y, 63) +#define I32_ROTR(x, y) ROTR(x, y, 31) +#define I64_ROTR(x, y) ROTR(x, y, 63) + +#define FMIN(x, y) \ + ((UNLIKELY((x) != (x))) \ + ? NAN \ + : (UNLIKELY((y) != (y))) \ + ? NAN \ + : (UNLIKELY((x) == 0 && (y) == 0)) ? (signbit(x) ? x : y) \ + : (x < y) ? x : y) + +#define FMAX(x, y) \ + ((UNLIKELY((x) != (x))) \ + ? NAN \ + : (UNLIKELY((y) != (y))) \ + ? NAN \ + : (UNLIKELY((x) == 0 && (y) == 0)) ? (signbit(x) ? y : x) \ + : (x > y) ? x : y) + +#define TRUNC_S(ut, st, ft, min, minop, max, x) \ + ((UNLIKELY((x) != (x))) \ + ? TRAP(INVALID_CONVERSION) \ + : (UNLIKELY(!((x)minop(min) && (x) < (max)))) ? TRAP(INT_OVERFLOW) \ + : (ut)(st)(x)) + +#define I32_TRUNC_S_F32(x) \ + TRUNC_S(u32, s32, f32, (f32)INT32_MIN, >=, 2147483648.f, x) +#define I64_TRUNC_S_F32(x) \ + TRUNC_S(u64, s64, f32, (f32)INT64_MIN, >=, (f32)INT64_MAX, x) +#define I32_TRUNC_S_F64(x) \ + TRUNC_S(u32, s32, f64, -2147483649., >, 2147483648., x) +#define I64_TRUNC_S_F64(x) \ + TRUNC_S(u64, s64, f64, (f64)INT64_MIN, >=, (f64)INT64_MAX, x) + +#define TRUNC_U(ut, ft, max, x) \ + ((UNLIKELY((x) != (x))) \ + ? TRAP(INVALID_CONVERSION) \ + : (UNLIKELY(!((x) > (ft)-1 && (x) < (max)))) ? TRAP(INT_OVERFLOW) \ + : (ut)(x)) + +#define I32_TRUNC_U_F32(x) TRUNC_U(u32, f32, 4294967296.f, x) +#define I64_TRUNC_U_F32(x) TRUNC_U(u64, f32, (f32)UINT64_MAX, x) +#define I32_TRUNC_U_F64(x) TRUNC_U(u32, f64, 4294967296., x) +#define I64_TRUNC_U_F64(x) TRUNC_U(u64, f64, (f64)UINT64_MAX, x) + +#define TRUNC_SAT_S(ut, st, ft, min, smin, minop, max, smax, x) \ + ((UNLIKELY((x) != (x))) \ + ? 0 \ + : (UNLIKELY(!((x)minop(min)))) \ + ? smin \ + : (UNLIKELY(!((x) < (max)))) ? smax : (ut)(st)(x)) + +#define I32_TRUNC_SAT_S_F32(x) \ + TRUNC_SAT_S(u32, s32, f32, (f32)INT32_MIN, INT32_MIN, >=, 2147483648.f, \ + INT32_MAX, x) +#define I64_TRUNC_SAT_S_F32(x) \ + TRUNC_SAT_S(u64, s64, f32, (f32)INT64_MIN, INT64_MIN, >=, (f32)INT64_MAX, \ + INT64_MAX, x) +#define I32_TRUNC_SAT_S_F64(x) \ + TRUNC_SAT_S(u32, s32, f64, -2147483649., INT32_MIN, >, 2147483648., \ + INT32_MAX, x) +#define I64_TRUNC_SAT_S_F64(x) \ + TRUNC_SAT_S(u64, s64, f64, (f64)INT64_MIN, INT64_MIN, >=, (f64)INT64_MAX, \ + INT64_MAX, x) + +#define TRUNC_SAT_U(ut, ft, max, smax, x) \ + ((UNLIKELY((x) != (x))) ? 0 \ + : (UNLIKELY(!((x) > (ft)-1))) \ + ? 0 \ + : (UNLIKELY(!((x) < (max)))) ? smax : (ut)(x)) + +#define I32_TRUNC_SAT_U_F32(x) \ + TRUNC_SAT_U(u32, f32, 4294967296.f, UINT32_MAX, x) +#define I64_TRUNC_SAT_U_F32(x) \ + TRUNC_SAT_U(u64, f32, (f32)UINT64_MAX, UINT64_MAX, x) +#define I32_TRUNC_SAT_U_F64(x) TRUNC_SAT_U(u32, f64, 4294967296., UINT32_MAX, x) +#define I64_TRUNC_SAT_U_F64(x) \ + TRUNC_SAT_U(u64, f64, (f64)UINT64_MAX, UINT64_MAX, x) + +#define DEFINE_REINTERPRET(name, t1, t2) \ + static inline t2 name(t1 x) { \ + t2 result; \ + wasm_rt_memcpy(&result, &x, sizeof(result)); \ + return result; \ + } + +DEFINE_REINTERPRET(f32_reinterpret_i32, u32, f32) +DEFINE_REINTERPRET(i32_reinterpret_f32, f32, u32) +DEFINE_REINTERPRET(f64_reinterpret_i64, u64, f64) +DEFINE_REINTERPRET(i64_reinterpret_f64, f64, u64) + +static float quiet_nanf(float x) { + uint32_t tmp; + wasm_rt_memcpy(&tmp, &x, 4); + tmp |= 0x7fc00000lu; + wasm_rt_memcpy(&x, &tmp, 4); + return x; +} + +static double quiet_nan(double x) { + uint64_t tmp; + wasm_rt_memcpy(&tmp, &x, 8); + tmp |= 0x7ff8000000000000llu; + wasm_rt_memcpy(&x, &tmp, 8); + return x; +} + +static double wasm_quiet(double x) { + if (UNLIKELY(isnan(x))) { + return quiet_nan(x); + } + return x; +} + +static float wasm_quietf(float x) { + if (UNLIKELY(isnan(x))) { + return quiet_nanf(x); + } + return x; +} + +static double wasm_floor(double x) { + if (UNLIKELY(isnan(x))) { + return quiet_nan(x); + } + return floor(x); +} + +static float wasm_floorf(float x) { + if (UNLIKELY(isnan(x))) { + return quiet_nanf(x); + } + return floorf(x); +} + +static double wasm_ceil(double x) { + if (UNLIKELY(isnan(x))) { + return quiet_nan(x); + } + return ceil(x); +} + +static float wasm_ceilf(float x) { + if (UNLIKELY(isnan(x))) { + return quiet_nanf(x); + } + return ceilf(x); +} + +static double wasm_trunc(double x) { + if (UNLIKELY(isnan(x))) { + return quiet_nan(x); + } + return trunc(x); +} + +static float wasm_truncf(float x) { + if (UNLIKELY(isnan(x))) { + return quiet_nanf(x); + } + return truncf(x); +} + +static float wasm_nearbyintf(float x) { + if (UNLIKELY(isnan(x))) { + return quiet_nanf(x); + } + return nearbyintf(x); +} + +static double wasm_nearbyint(double x) { + if (UNLIKELY(isnan(x))) { + return quiet_nan(x); + } + return nearbyint(x); +} + +static float wasm_fabsf(float x) { + if (UNLIKELY(isnan(x))) { + uint32_t tmp; + wasm_rt_memcpy(&tmp, &x, 4); + tmp = tmp & ~(1UL << 31); + wasm_rt_memcpy(&x, &tmp, 4); + return x; + } + return fabsf(x); +} + +static double wasm_fabs(double x) { + if (UNLIKELY(isnan(x))) { + uint64_t tmp; + wasm_rt_memcpy(&tmp, &x, 8); + tmp = tmp & ~(1ULL << 63); + wasm_rt_memcpy(&x, &tmp, 8); + return x; + } + return fabs(x); +} + +static double wasm_sqrt(double x) { + if (UNLIKELY(isnan(x))) { + return quiet_nan(x); + } + return sqrt(x); +} + +static float wasm_sqrtf(float x) { + if (UNLIKELY(isnan(x))) { + return quiet_nanf(x); + } + return sqrtf(x); +} + +static inline void memory_fill(wasm_rt_memory_t* mem, u32 d, u32 val, u32 n) { + RANGE_CHECK(mem, d, n); + memset(mem->data + d, val, n); +} + +static inline void memory_copy(wasm_rt_memory_t* dest, + const wasm_rt_memory_t* src, + u32 dest_addr, + u32 src_addr, + u32 n) { + RANGE_CHECK(dest, dest_addr, n); + RANGE_CHECK(src, src_addr, n); + memmove(dest->data + dest_addr, src->data + src_addr, n); +} + +static inline void memory_init(wasm_rt_memory_t* dest, + const u8* src, + u32 src_size, + u32 dest_addr, + u32 src_addr, + u32 n) { + if (UNLIKELY(src_addr + (uint64_t)n > src_size)) + TRAP(OOB); + LOAD_DATA((*dest), dest_addr, src + src_addr, n); +} + +typedef struct { + enum { RefFunc, RefNull, GlobalGet } expr_type; + wasm_rt_func_type_t type; + wasm_rt_function_ptr_t func; + wasm_rt_tailcallee_t func_tailcallee; + size_t module_offset; +} wasm_elem_segment_expr_t; + +static inline void funcref_table_init(wasm_rt_funcref_table_t* dest, + const wasm_elem_segment_expr_t* src, + u32 src_size, + u32 dest_addr, + u32 src_addr, + u32 n, + void* module_instance) { + if (UNLIKELY(src_addr + (uint64_t)n > src_size)) + TRAP(OOB); + if (UNLIKELY(dest_addr + (uint64_t)n > dest->size)) + TRAP(OOB); + for (u32 i = 0; i < n; i++) { + const wasm_elem_segment_expr_t* const src_expr = &src[src_addr + i]; + wasm_rt_funcref_t* const dest_val = &(dest->data[dest_addr + i]); + switch (src_expr->expr_type) { + case RefFunc: + *dest_val = (wasm_rt_funcref_t){ + src_expr->type, src_expr->func, src_expr->func_tailcallee, + (char*)module_instance + src_expr->module_offset}; + break; + case RefNull: + *dest_val = wasm_rt_funcref_null_value; + break; + case GlobalGet: + *dest_val = **(wasm_rt_funcref_t**)((char*)module_instance + + src_expr->module_offset); + break; + } + } +} + +// Currently wasm2c only supports initializing externref tables with ref.null. +static inline void externref_table_init(wasm_rt_externref_table_t* dest, + u32 src_size, + u32 dest_addr, + u32 src_addr, + u32 n) { + if (UNLIKELY(src_addr + (uint64_t)n > src_size)) + TRAP(OOB); + if (UNLIKELY(dest_addr + (uint64_t)n > dest->size)) + TRAP(OOB); + for (u32 i = 0; i < n; i++) { + dest->data[dest_addr + i] = wasm_rt_externref_null_value; + } +} + +#define DEFINE_TABLE_COPY(type) \ + static inline void type##_table_copy(wasm_rt_##type##_table_t* dest, \ + const wasm_rt_##type##_table_t* src, \ + u32 dest_addr, u32 src_addr, u32 n) { \ + if (UNLIKELY(dest_addr + (uint64_t)n > dest->size)) \ + TRAP(OOB); \ + if (UNLIKELY(src_addr + (uint64_t)n > src->size)) \ + TRAP(OOB); \ + \ + memmove(dest->data + dest_addr, src->data + src_addr, \ + n * sizeof(wasm_rt_##type##_t)); \ + } + +DEFINE_TABLE_COPY(funcref) +DEFINE_TABLE_COPY(externref) + +#define DEFINE_TABLE_GET(type) \ + static inline wasm_rt_##type##_t type##_table_get( \ + const wasm_rt_##type##_table_t* table, u32 i) { \ + if (UNLIKELY(i >= table->size)) \ + TRAP(OOB); \ + return table->data[i]; \ + } + +DEFINE_TABLE_GET(funcref) +DEFINE_TABLE_GET(externref) + +#define DEFINE_TABLE_SET(type) \ + static inline void type##_table_set(const wasm_rt_##type##_table_t* table, \ + u32 i, const wasm_rt_##type##_t val) { \ + if (UNLIKELY(i >= table->size)) \ + TRAP(OOB); \ + table->data[i] = val; \ + } + +DEFINE_TABLE_SET(funcref) +DEFINE_TABLE_SET(externref) + +#define DEFINE_TABLE_FILL(type) \ + static inline void type##_table_fill(const wasm_rt_##type##_table_t* table, \ + u32 d, const wasm_rt_##type##_t val, \ + u32 n) { \ + if (UNLIKELY((uint64_t)d + n > table->size)) \ + TRAP(OOB); \ + for (uint32_t i = d; i < d + n; i++) { \ + table->data[i] = val; \ + } \ + } + +DEFINE_TABLE_FILL(funcref) +DEFINE_TABLE_FILL(externref) + +#if defined(__GNUC__) || defined(__clang__) +#define FUNC_TYPE_DECL_EXTERN_T(x) extern const char* const x +#define FUNC_TYPE_EXTERN_T(x) const char* const x +#define FUNC_TYPE_T(x) static const char* const x +#else +#define FUNC_TYPE_DECL_EXTERN_T(x) extern const char x[] +#define FUNC_TYPE_EXTERN_T(x) const char x[] +#define FUNC_TYPE_T(x) static const char x[] +#endif + +#if (__STDC_VERSION__ < 201112L) && !defined(static_assert) +#define static_assert(X) \ + extern int(*assertion(void))[!!sizeof(struct { int x : (X) ? 2 : -1; })]; +#endif + +#ifdef _MSC_VER +#define WEAK_FUNC_DECL(func, fallback) \ + __pragma(comment(linker, "/alternatename:" #func "=" #fallback)) \ + \ + void \ + fallback(void** instance_ptr, void* tail_call_stack, \ + wasm_rt_tailcallee_t* next) +#else +#define WEAK_FUNC_DECL(func, fallback) \ + __attribute__((weak)) void func(void** instance_ptr, void* tail_call_stack, \ + wasm_rt_tailcallee_t* next) +#endif + +static void w2c_test_tailcaller_0(w2c_test*); +static void wasm_tailcall_w2c_test_tailcaller_0(void **instance_ptr, void *tail_call_stack, wasm_rt_tailcallee_t *next); +static struct wasm_multi_id w2c_test_infiniteloop(w2c_test*, u32, f64); +static void wasm_tailcall_w2c_test_infiniteloop(void **instance_ptr, void *tail_call_stack, wasm_rt_tailcallee_t *next); + +#ifndef wasm_multi_if +#define wasm_multi_if wasm_multi_if +struct wasm_multi_if { + u32 i0; + f32 f1; +}; +#endif /* wasm_multi_if */ + +FUNC_TYPE_T(w2c_test_i32_f32) = "\x98\x89\x5c\xbd\x28\xfd\x0e\x4d\xc5\xdc\x68\x2c\x7c\xee\x61\x09\x14\x19\x30\x62\xc2\x2f\x49\xc5\xb5\x81\x57\x55\x6b\xe7\xa5\xb9"; +FUNC_TYPE_T(w2c_test_t1) = "\x36\xa9\xe7\xf1\xc9\x5b\x82\xff\xb9\x97\x43\xe0\xc5\xc4\xce\x95\xd8\x3c\x9a\x43\x0a\xac\x59\xf8\x4e\xf3\xcb\xfa\xb6\x14\x50\x68"; +FUNC_TYPE_T(w2c_test_t2) = "\xe5\x11\x86\xc7\x24\xdb\x44\x80\xbe\xd1\xe0\x89\xbc\xc0\x20\xea\xfb\x1c\x9a\x27\xa5\xc3\xdb\xca\x5d\xb0\x05\x0f\x7c\x03\x74\x0a"; + +static const wasm_elem_segment_expr_t elem_segment_exprs_w2c_test_e0[] = { + {RefFunc, w2c_test_i32_f32, (wasm_rt_function_ptr_t)w2c_spectest_print_i32_f32, {wasm_tailcall_w2c_spectest_print_i32_f32}, offsetof(w2c_test, w2c_spectest_instance)}, +}; + +static void init_tables(w2c_test* instance) { + wasm_rt_allocate_funcref_table(&instance->w2c_tab, 1, 1); + funcref_table_init(&instance->w2c_tab, elem_segment_exprs_w2c_test_e0, 1, 0u, 0, 1, instance); +} + +static void init_elem_instances(w2c_test *instance) { +} + +/* export: 'tailcaller' */ +void w2c_test_tailcaller(w2c_test* instance) { + w2c_test_tailcaller_0(instance); +} + +/* export for tail-call of 'tailcaller' */ +void wasm_tailcall_w2c_test_tailcaller(void **instance_ptr, void *tail_call_stack, wasm_rt_tailcallee_t *next) { + wasm_tailcall_w2c_test_tailcaller_0(instance_ptr, tail_call_stack, next); +} + +/* handler for missing tail-call on import: 'spectest' 'print_i32_f32' */ +WEAK_FUNC_DECL(wasm_tailcall_w2c_spectest_print_i32_f32, wasm_fallback_test_w2c_spectest_print_i32_f32) +{ + next->fn = NULL; + struct wasm_multi_if params; + wasm_rt_memcpy(params, tail_call_stack, sizeof(params); + w2c_spectest_print_i32_f32(*instance_ptr, params.i0, params.f1); +} + +static void init_instance_import(w2c_test* instance, struct w2c_spectest* w2c_spectest_instance) { + instance->w2c_spectest_instance = w2c_spectest_instance; +} + +void wasm2c_test_instantiate(w2c_test* instance, struct w2c_spectest* w2c_spectest_instance) { + assert(wasm_rt_is_initialized()); + init_instance_import(instance, w2c_spectest_instance); + init_tables(instance); + init_elem_instances(instance); +} + +void wasm2c_test_free(w2c_test* instance) { + wasm_rt_free_funcref_table(&instance->w2c_tab); +} + +wasm_rt_func_type_t wasm2c_test_get_func_type(uint32_t param_count, uint32_t result_count, ...) { + va_list args; + + if (param_count == 2 && result_count == 0) { + va_start(args, result_count); + if (true && va_arg(args, wasm_rt_type_t) == WASM_RT_I32 && va_arg(args, wasm_rt_type_t) == WASM_RT_F32) { + va_end(args); + return w2c_test_i32_f32; + } + va_end(args); + } + + if (param_count == 0 && result_count == 0) { + va_start(args, result_count); + if (true) { + va_end(args); + return w2c_test_t1; + } + va_end(args); + } + + if (param_count == 2 && result_count == 2) { + va_start(args, result_count); + if (true && va_arg(args, wasm_rt_type_t) == WASM_RT_I32 && va_arg(args, wasm_rt_type_t) == WASM_RT_F64 && va_arg(args, wasm_rt_type_t) == WASM_RT_I32 && va_arg(args, wasm_rt_type_t) == WASM_RT_F64) { + va_end(args); + return w2c_test_t2; + } + va_end(args); + } + + return NULL; +} + +void w2c_test_tailcaller_0(w2c_test* instance) { + FUNC_PROLOGUE; + u32 var_i0, var_i2; + f32 var_f1; + var_i0 = 1u; + var_f1 = 2; + var_i2 = 0u; + static_assert(sizeof(struct wasm_multi_if) <= 1024); + CHECK_CALL_INDIRECT(instance->w2c_tab, w2c_test_i32_f32, var_i2); + if (!instance->w2c_tab.data[var_i2].func_tailcallee.fn) { + CALL_INDIRECT(instance->w2c_tab, void (*)(void*, u32, f32), w2c_test_i32_f32, var_i2, instance->w2c_tab.data[var_i2].module_instance, var_i0, var_f1); + } else { + void *instance_ptr_storage; + void **instance_ptr = &instance_ptr_storage; + char tail_call_stack[1024]; + wasm_rt_tailcallee_t next_storage; + wasm_rt_tailcallee_t *next = &next_storage; + { + struct wasm_multi_if tmp; + tmp.i0 = var_i0; + tmp.f1 = var_f1; + wasm_rt_memcpy(tail_call_stack, &tmp, sizeof(tmp)); + } + next->fn = instance->w2c_tab.data[var_i2].func_tailcallee.fn; + *instance_ptr = instance->w2c_tab.data[var_i2].module_instance; + while (next->fn) { next->fn(instance_ptr, tail_call_stack, next); } + } + goto var_Bfunc; + var_Bfunc:; + FUNC_EPILOGUE; +} + +void wasm_tailcall_w2c_test_tailcaller_0(void **instance_ptr, void *tail_call_stack, wasm_rt_tailcallee_t *next) { + w2c_test* instance = *instance_ptr; + u32 var_i0, var_i2; + f32 var_f1; + var_i0 = 1u; + var_f1 = 2; + var_i2 = 0u; + static_assert(sizeof(struct wasm_multi_if) <= 1024); + CHECK_CALL_INDIRECT(instance->w2c_tab, w2c_test_i32_f32, var_i2); + if (!instance->w2c_tab.data[var_i2].func_tailcallee.fn) { + CALL_INDIRECT(instance->w2c_tab, void (*)(void*, u32, f32), w2c_test_i32_f32, var_i2, instance->w2c_tab.data[var_i2].module_instance, var_i0, var_f1); + next->fn = NULL; + } else { + { + struct wasm_multi_if tmp; + tmp.i0 = var_i0; + tmp.f1 = var_f1; + wasm_rt_memcpy(tail_call_stack, &tmp, sizeof(tmp)); + } + next->fn = instance->w2c_tab.data[var_i2].func_tailcallee.fn; + *instance_ptr = instance->w2c_tab.data[var_i2].module_instance; + } + return; + var_Bfunc:; + next->fn = NULL; +} + +struct wasm_multi_id w2c_test_infiniteloop(w2c_test* instance, u32 var_p0, f64 var_p1) { + FUNC_PROLOGUE; + u32 var_i0; + f64 var_d1; + var_i0 = var_p0; + var_d1 = var_p1; + static_assert(sizeof(struct wasm_multi_id) <= 1024); + { + void *instance_ptr_storage; + void **instance_ptr = &instance_ptr_storage; + char tail_call_stack[1024]; + wasm_rt_tailcallee_t next_storage; + wasm_rt_tailcallee_t *next = &next_storage; + { + struct wasm_multi_id tmp; + tmp.i0 = var_i0; + tmp.d1 = var_d1; + wasm_rt_memcpy(tail_call_stack, &tmp, sizeof(tmp)); + } + next->fn = wasm_tailcall_w2c_test_infiniteloop; + while (next->fn) { next->fn(instance_ptr, tail_call_stack, next); } + { + struct wasm_multi_id tmp; + wasm_rt_memcpy(&tmp, tail_call_stack, sizeof(tmp)); + var_i0 = tmp.i0; + var_d1 = tmp.d1; + } + } + goto var_Bfunc; + var_Bfunc:; + FUNC_EPILOGUE; + { + struct wasm_multi_id tmp; + tmp.i0 = var_i0; + tmp.d1 = var_d1; + return tmp; + } +} + +void wasm_tailcall_w2c_test_infiniteloop(void **instance_ptr, void *tail_call_stack, wasm_rt_tailcallee_t *next) { + static_assert(sizeof(struct wasm_multi_id) <= 1024); + w2c_test* instance = *instance_ptr; + u32 var_p0; + f64 var_p1; + { + struct wasm_multi_id tmp; + wasm_rt_memcpy(&tmp, tail_call_stack, sizeof(tmp)); + var_p0 = tmp.i0; + var_p1 = tmp.d1; + } + u32 var_i0; + f64 var_d1; + var_i0 = var_p0; + var_d1 = var_p1; + static_assert(sizeof(struct wasm_multi_id) <= 1024); + { + { + struct wasm_multi_id tmp; + tmp.i0 = var_i0; + tmp.d1 = var_d1; + wasm_rt_memcpy(tail_call_stack, &tmp, sizeof(tmp)); + } + next->fn = wasm_tailcall_w2c_test_infiniteloop; + } + return; + var_Bfunc:; + { + struct wasm_multi_id tmp; + tmp.i0 = var_i0; + tmp.d1 = var_d1; + wasm_rt_memcpy(tail_call_stack, &tmp, sizeof(tmp)); + } + next->fn = NULL; +} +;;; STDOUT ;;) diff --git a/wasm2c/examples/callback/main.c b/wasm2c/examples/callback/main.c index 8c6f9db0..8d9ab59e 100644 --- a/wasm2c/examples/callback/main.c +++ b/wasm2c/examples/callback/main.c @@ -26,7 +26,8 @@ int main(int argc, char** argv) { */ wasm_rt_func_type_t fn_type = wasm2c_callback_get_func_type(1, 0, WASM_RT_I32); - wasm_rt_funcref_t fn_ref = {fn_type, (wasm_rt_function_ptr_t)print, &inst}; + wasm_rt_funcref_t fn_ref = { + fn_type, (wasm_rt_function_ptr_t)print, {NULL}, &inst}; w2c_callback_set_print_function(&inst, fn_ref); /* "say_hello" uses the previously installed callback. */ diff --git a/wasm2c/examples/fac/fac.c b/wasm2c/examples/fac/fac.c index f6d6d0de..33151cdd 100644 --- a/wasm2c/examples/fac/fac.c +++ b/wasm2c/examples/fac/fac.c @@ -39,11 +39,16 @@ static inline bool func_types_eq(const wasm_rt_func_type_t a, return (a == b) || LIKELY(a && b && !memcmp(a, b, 32)); } -#define CALL_INDIRECT(table, t, ft, x, ...) \ +#define CHECK_CALL_INDIRECT(table, ft, x) \ (LIKELY((x) < table.size && table.data[x].func && \ func_types_eq(ft, table.data[x].func_type)) || \ - TRAP(CALL_INDIRECT), \ - ((t)table.data[x].func)(__VA_ARGS__)) + TRAP(CALL_INDIRECT)) + +#define DO_CALL_INDIRECT(table, t, x, ...) ((t)table.data[x].func)(__VA_ARGS__) + +#define CALL_INDIRECT(table, t, ft, x, ...) \ + (CHECK_CALL_INDIRECT(table, ft, x), \ + DO_CALL_INDIRECT(table, t, x, __VA_ARGS__)) #ifdef SUPPORT_MEMORY64 #define RANGE_CHECK(mem, offset, len) \ @@ -522,6 +527,7 @@ typedef struct { enum { RefFunc, RefNull, GlobalGet } expr_type; wasm_rt_func_type_t type; wasm_rt_function_ptr_t func; + wasm_rt_tailcallee_t func_tailcallee; size_t module_offset; } wasm_elem_segment_expr_t; @@ -542,7 +548,7 @@ static inline void funcref_table_init(wasm_rt_funcref_table_t* dest, switch (src_expr->expr_type) { case RefFunc: *dest_val = (wasm_rt_funcref_t){ - src_expr->type, src_expr->func, + src_expr->type, src_expr->func, src_expr->func_tailcallee, (char*)module_instance + src_expr->module_offset}; break; case RefNull: @@ -633,6 +639,24 @@ DEFINE_TABLE_FILL(externref) #define FUNC_TYPE_T(x) static const char x[] #endif +#if (__STDC_VERSION__ < 201112L) && !defined(static_assert) +#define static_assert(X) \ + extern int(*assertion(void))[!!sizeof(struct { int x : (X) ? 2 : -1; })]; +#endif + +#ifdef _MSC_VER +#define WEAK_FUNC_DECL(func, fallback) \ + __pragma(comment(linker, "/alternatename:" #func "=" #fallback)) \ + \ + void \ + fallback(void** instance_ptr, void* tail_call_stack, \ + wasm_rt_tailcallee_t* next) +#else +#define WEAK_FUNC_DECL(func, fallback) \ + __attribute__((weak)) void func(void** instance_ptr, void* tail_call_stack, \ + wasm_rt_tailcallee_t* next) +#endif + static u32 w2c_fac_fac_0(w2c_fac*, u32); FUNC_TYPE_T(w2c_fac_t0) = "\x07\x80\x96\x7a\x42\xf7\x3e\xe6\x70\x5c\x2f\xac\x83\xf5\x67\xd2\xa2\xa0\x69\x41\x5f\xf8\xe7\x96\x7f\x23\xab\x00\x03\x5f\x4a\x3c"; diff --git a/wasm2c/wasm-rt-impl.c b/wasm2c/wasm-rt-impl.c index 54223087..99bae707 100644 --- a/wasm2c/wasm-rt-impl.c +++ b/wasm2c/wasm-rt-impl.c @@ -414,7 +414,7 @@ const char* wasm_rt_strerror(wasm_rt_trap_t trap) { case WASM_RT_TRAP_UNREACHABLE: return "Unreachable instruction executed"; case WASM_RT_TRAP_CALL_INDIRECT: - return "Invalid call_indirect"; + return "Invalid call_indirect or return_call_indirect"; case WASM_RT_TRAP_UNCAUGHT_EXCEPTION: return "Uncaught exception"; case WASM_RT_TRAP_UNALIGNED: diff --git a/wasm2c/wasm-rt.h b/wasm2c/wasm-rt.h index bdeecd56..e6df7d09 100644 --- a/wasm2c/wasm-rt.h +++ b/wasm2c/wasm-rt.h @@ -246,6 +246,17 @@ typedef enum { typedef void (*wasm_rt_function_ptr_t)(void); /** + * A pointer to a "tail-callee" function, called by a tail-call + * trampoline or by another tail-callee function. (The definition uses a + * single-member struct to allow a recursive definition.) + */ +typedef struct wasm_rt_tailcallee_t { + void (*fn)(void** instance_ptr, + void* tail_call_stack, + struct wasm_rt_tailcallee_t* next); +} wasm_rt_tailcallee_t; + +/** * The type of a function (an arbitrary number of param and result types). * This is represented as an opaque 256-bit ID. */ @@ -259,6 +270,8 @@ typedef struct { /** The function. The embedder must know the actual C signature of the * function and cast to it before calling. */ wasm_rt_function_ptr_t func; + /** An alternate version of the function to be used when tail-called. */ + wasm_rt_tailcallee_t func_tailcallee; /** A function instance is a closure of the function over an instance * of the originating module. The module_instance element will be passed into * the function at runtime. */ @@ -266,7 +279,7 @@ typedef struct { } wasm_rt_funcref_t; /** Default (null) value of a funcref */ -static const wasm_rt_funcref_t wasm_rt_funcref_null_value = {NULL, NULL, NULL}; +static const wasm_rt_funcref_t wasm_rt_funcref_null_value; /** The type of an external reference (opaque to WebAssembly). */ typedef void* wasm_rt_externref_t; |