diff options
Diffstat (limited to 'src')
-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 |
5 files changed, 587 insertions, 111 deletions
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. */ |