Skip to content

Commit 2d906d8

Browse files
committed
wasm2c: Remove implicit void* conversions in tail calls
1 parent 32f04e7 commit 2d906d8

File tree

6 files changed

+21
-10
lines changed

6 files changed

+21
-10
lines changed

include/wabt/ir.h

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -901,7 +901,8 @@ inline bool operator!=(const LocalTypes::const_iterator& lhs,
901901
}
902902

903903
struct Func {
904-
explicit Func(std::string_view name) : name(name) {}
904+
explicit Func(std::string_view name, std::string_view ext_module_name)
905+
: name(name), ext_module_name(ext_module_name) {}
905906

906907
Type GetParamType(Index index) const { return decl.GetParamType(index); }
907908
Type GetResultType(Index index) const { return decl.GetResultType(index); }
@@ -916,6 +917,7 @@ struct Func {
916917
Index GetLocalIndex(const Var&) const;
917918

918919
std::string name;
920+
std::string ext_module_name;
919921
FuncDeclaration decl;
920922
LocalTypes local_types;
921923
BindingHash bindings;
@@ -1010,8 +1012,9 @@ class ImportMixin : public Import {
10101012

10111013
class FuncImport : public ImportMixin<ExternalKind::Func> {
10121014
public:
1013-
explicit FuncImport(std::string_view name = std::string_view())
1014-
: ImportMixin<ExternalKind::Func>(), func(name) {}
1015+
explicit FuncImport(std::string_view name = std::string_view(),
1016+
std::string_view ext_module_name = std::string_view())
1017+
: ImportMixin<ExternalKind::Func>(), func(name, ext_module_name) {}
10151018

10161019
Func func;
10171020
};
@@ -1099,9 +1102,12 @@ class ModuleFieldMixin : public ModuleField {
10991102

11001103
class FuncModuleField : public ModuleFieldMixin<ModuleFieldType::Func> {
11011104
public:
1102-
explicit FuncModuleField(const Location& loc = Location(),
1103-
std::string_view name = std::string_view())
1104-
: ModuleFieldMixin<ModuleFieldType::Func>(loc), func(name) {}
1105+
explicit FuncModuleField(
1106+
const Location& loc = Location(),
1107+
std::string_view name = std::string_view(),
1108+
std::string_view ext_module_name = std::string_view())
1109+
: ModuleFieldMixin<ModuleFieldType::Func>(loc),
1110+
func(name, ext_module_name) {}
11051111

11061112
Func func;
11071113
};

src/binary-reader-ir.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,7 @@ Result BinaryReaderIR::OnImportFunc(Index import_index,
601601
import->module_name = module_name;
602602
import->field_name = field_name;
603603
SetFuncDeclaration(&import->func.decl, Var(sig_index, GetLocation()));
604+
import->func.ext_module_name = module_name;
604605
module_->AppendField(
605606
std::make_unique<ImportModuleField>(std::move(import), GetLocation()));
606607
return Result::Ok;

src/c-writer.cc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2351,7 +2351,11 @@ void CWriter::WriteFuncRefWrapper(const Func* func) {
23512351
Write(") ", OpenBrace());
23522352
Write("return ");
23532353
Write(ExternalRef(ModuleFieldType::Func, func->name));
2354-
Write("(instance");
2354+
std::string target_module_name =
2355+
func->ext_module_name == ""
2356+
? ModuleInstanceTypeName()
2357+
: ModuleInstanceTypeName(func->ext_module_name);
2358+
Write("((struct ", target_module_name, "*) instance");
23552359
if (func->GetNumParams() != 0) {
23562360
Indent(4);
23572361
for (Index i = 0; i < func->GetNumParams(); ++i) {

test/wasm2c/check-imports.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -831,7 +831,7 @@ FUNC_TYPE_T(w2c_test_t1) = "\x72\xab\x00\xdf\x20\x3d\xce\xa1\xf2\x29\xc7\x9d\x13
831831
FUNC_TYPE_T(w2c_test_t2) = "\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";
832832

833833
static u32 wrap_w2c_test_f1(void *instance) {
834-
return w2c_test_f1(instance);
834+
return w2c_test_f1((struct w2c_test*) instance);
835835
}
836836

837837
static void init_memories(w2c_test* instance) {

test/wasm2c/hello.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -836,7 +836,7 @@ FUNC_TYPE_T(w2c_test_t1) = "\x89\x3a\x3d\x2c\x8f\x4d\x7f\x6d\x6c\x9d\x62\x67\x29
836836
FUNC_TYPE_T(w2c_test_t2) = "\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";
837837

838838
static u32 wrap_w2c_wasi__snapshot__preview1_fd_write(void *instance, u32 var_0, u32 var_1, u32 var_2, u32 var_3) {
839-
return w2c_wasi__snapshot__preview1_fd_write(instance, var_0, var_1, var_2, var_3);
839+
return w2c_wasi__snapshot__preview1_fd_write((struct w2c_wasi__snapshot__preview1*) instance, var_0, var_1, var_2, var_3);
840840
}
841841

842842
static const u8 data_segment_data_w2c_test_d0[] = {

test/wasm2c/tail-calls.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -839,7 +839,7 @@ FUNC_TYPE_T(w2c_test_t1) = "\x36\xa9\xe7\xf1\xc9\x5b\x82\xff\xb9\x97\x43\xe0\xc5
839839
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";
840840

841841
static void wrap_w2c_spectest_print_i32_f32(void *instance, u32 var_0, f32 var_1) {
842-
return w2c_spectest_print_i32_f32(instance, var_0, var_1);
842+
return w2c_spectest_print_i32_f32((struct w2c_spectest*) instance, var_0, var_1);
843843
}
844844

845845
static const wasm_elem_segment_expr_t elem_segment_exprs_w2c_test_e0[] = {

0 commit comments

Comments
 (0)