wasm2c: give an imported function in an element segment the instance of its import

An element segment that puts an imported function into a table wrote the entry
as RefFunc with the offset of the field of the module instance that holds the
pointer to the instance of the import. funcref_table_init then stored the
address of that field (module instance + offset) instead of the pointer it
holds, so a call_indirect of the entry gave the wrapper of the import a pointer
to something else.

Write such entries as RefImportFunc, which reads the pointer from the field the
way the entry of a defined function reads the module instance. The ref.func
expression was already right.

The spec tests' host function spectest.print_i32 now checks the instance it is
given, and a new test calls an imported function through a table.
diff --git a/src/c-writer.cc b/src/c-writer.cc
index b73bd31..185b8a7 100644
--- a/src/c-writer.cc
+++ b/src/c-writer.cc
@@ -2489,8 +2489,13 @@
         case ExprType::RefFunc: {
           const Func* func = module_->GetFunc(cast<RefFuncExpr>(&expr)->var);
           const FuncType* func_type = module_->GetFuncType(func->decl.type_var);
-          Write("{RefFunc, ", FuncTypeExpr(func_type),
-                ", (wasm_rt_function_ptr_t)", WrapperRef(func->name), ", {");
+          // The entry of an imported function reads the pointer to the
+          // instance of the import from the module instance (see
+          // funcref_table_init); a function defined here uses the module
+          // instance itself.
+          Write(IsImport(func->name) ? "{RefImportFunc, " : "{RefFunc, ",
+                FuncTypeExpr(func_type), ", (wasm_rt_function_ptr_t)",
+                WrapperRef(func->name), ", {");
           if (options_.features.tail_call_enabled() &&
               (IsImport(func->name) || func->features_used.tailcall)) {
             Write(TailCallRef(func->name));
diff --git a/src/prebuilt/wasm2c_source_declarations.cc b/src/prebuilt/wasm2c_source_declarations.cc
index 4217a7e..82a0a22 100644
--- a/src/prebuilt/wasm2c_source_declarations.cc
+++ b/src/prebuilt/wasm2c_source_declarations.cc
@@ -1315,7 +1315,17 @@
 R"w2c_template(}
 )w2c_template"
 R"w2c_template(
-typedef enum { RefFunc, RefNull, GlobalGet } wasm_elem_segment_expr_type_t;
+typedef enum {
+)w2c_template"
+R"w2c_template(  RefFunc,
+)w2c_template"
+R"w2c_template(  RefNull,
+)w2c_template"
+R"w2c_template(  GlobalGet,
+)w2c_template"
+R"w2c_template(  RefImportFunc
+)w2c_template"
+R"w2c_template(} wasm_elem_segment_expr_type_t;
 )w2c_template"
 R"w2c_template(
 typedef struct {
@@ -1371,6 +1381,16 @@
 )w2c_template"
 R"w2c_template(        break;
 )w2c_template"
+R"w2c_template(      case RefImportFunc:
+)w2c_template"
+R"w2c_template(        *dest_val = (wasm_rt_funcref_t){
+)w2c_template"
+R"w2c_template(            src_expr->type, src_expr->func, src_expr->func_tailcallee,
+)w2c_template"
+R"w2c_template(            *(void**)((char*)module_instance + src_expr->module_offset)};
+)w2c_template"
+R"w2c_template(        break;
+)w2c_template"
 R"w2c_template(      case RefNull:
 )w2c_template"
 R"w2c_template(        *dest_val = wasm_rt_funcref_null_value;
diff --git a/src/template/wasm2c.declarations.c b/src/template/wasm2c.declarations.c
index 0ebfd12..5c4ea12 100644
--- a/src/template/wasm2c.declarations.c
+++ b/src/template/wasm2c.declarations.c
@@ -700,7 +700,12 @@
   LOAD_DATA((*dest), dest_addr, src + src_addr, n);
 }
 
-typedef enum { RefFunc, RefNull, GlobalGet } wasm_elem_segment_expr_type_t;
+typedef enum {
+  RefFunc,
+  RefNull,
+  GlobalGet,
+  RefImportFunc
+} wasm_elem_segment_expr_type_t;
 
 typedef struct {
   wasm_elem_segment_expr_type_t expr_type;
@@ -729,6 +734,11 @@
             src_expr->type, src_expr->func, src_expr->func_tailcallee,
             (char*)module_instance + src_expr->module_offset};
         break;
+      case RefImportFunc:
+        *dest_val = (wasm_rt_funcref_t){
+            src_expr->type, src_expr->func, src_expr->func_tailcallee,
+            *(void**)((char*)module_instance + src_expr->module_offset)};
+        break;
       case RefNull:
         *dest_val = wasm_rt_funcref_null_value;
         break;
diff --git a/test/spec-wasm2c-prefix.c b/test/spec-wasm2c-prefix.c
index bc86f8f..db5d67f 100644
--- a/test/spec-wasm2c-prefix.c
+++ b/test/spec-wasm2c-prefix.c
@@ -371,6 +371,13 @@
 }
 
 void w2c_spectest_print_i32(w2c_spectest* instance, uint32_t i) {
+  // A call through a table must hand the host function the instance of the
+  // import, as a direct call does.
+  if (instance != &spectest_instance) {
+    printf("spectest.print_i32: wrong instance %p, expected %p\n",
+           (void*)instance, (void*)&spectest_instance);
+    abort();
+  }
   printf("spectest.print_i32(%d)\n", i);
 }
 
diff --git a/test/wasm2c/add.txt b/test/wasm2c/add.txt
index 7b06d08..c62c30b 100644
--- a/test/wasm2c/add.txt
+++ b/test/wasm2c/add.txt
@@ -809,7 +809,12 @@
   LOAD_DATA((*dest), dest_addr, src + src_addr, n);
 }
 
-typedef enum { RefFunc, RefNull, GlobalGet } wasm_elem_segment_expr_type_t;
+typedef enum {
+  RefFunc,
+  RefNull,
+  GlobalGet,
+  RefImportFunc
+} wasm_elem_segment_expr_type_t;
 
 typedef struct {
   wasm_elem_segment_expr_type_t expr_type;
@@ -838,6 +843,11 @@
             src_expr->type, src_expr->func, src_expr->func_tailcallee,
             (char*)module_instance + src_expr->module_offset};
         break;
+      case RefImportFunc:
+        *dest_val = (wasm_rt_funcref_t){
+            src_expr->type, src_expr->func, src_expr->func_tailcallee,
+            *(void**)((char*)module_instance + src_expr->module_offset)};
+        break;
       case RefNull:
         *dest_val = wasm_rt_funcref_null_value;
         break;
diff --git a/test/wasm2c/check-imports.txt b/test/wasm2c/check-imports.txt
index c93f739..c165128 100644
--- a/test/wasm2c/check-imports.txt
+++ b/test/wasm2c/check-imports.txt
@@ -834,7 +834,12 @@
   LOAD_DATA((*dest), dest_addr, src + src_addr, n);
 }
 
-typedef enum { RefFunc, RefNull, GlobalGet } wasm_elem_segment_expr_type_t;
+typedef enum {
+  RefFunc,
+  RefNull,
+  GlobalGet,
+  RefImportFunc
+} wasm_elem_segment_expr_type_t;
 
 typedef struct {
   wasm_elem_segment_expr_type_t expr_type;
@@ -863,6 +868,11 @@
             src_expr->type, src_expr->func, src_expr->func_tailcallee,
             (char*)module_instance + src_expr->module_offset};
         break;
+      case RefImportFunc:
+        *dest_val = (wasm_rt_funcref_t){
+            src_expr->type, src_expr->func, src_expr->func_tailcallee,
+            *(void**)((char*)module_instance + src_expr->module_offset)};
+        break;
       case RefNull:
         *dest_val = wasm_rt_funcref_null_value;
         break;
diff --git a/test/wasm2c/export-names.txt b/test/wasm2c/export-names.txt
index 5f045cb..8eedcb6 100644
--- a/test/wasm2c/export-names.txt
+++ b/test/wasm2c/export-names.txt
@@ -834,7 +834,12 @@
   LOAD_DATA((*dest), dest_addr, src + src_addr, n);
 }
 
-typedef enum { RefFunc, RefNull, GlobalGet } wasm_elem_segment_expr_type_t;
+typedef enum {
+  RefFunc,
+  RefNull,
+  GlobalGet,
+  RefImportFunc
+} wasm_elem_segment_expr_type_t;
 
 typedef struct {
   wasm_elem_segment_expr_type_t expr_type;
@@ -863,6 +868,11 @@
             src_expr->type, src_expr->func, src_expr->func_tailcallee,
             (char*)module_instance + src_expr->module_offset};
         break;
+      case RefImportFunc:
+        *dest_val = (wasm_rt_funcref_t){
+            src_expr->type, src_expr->func, src_expr->func_tailcallee,
+            *(void**)((char*)module_instance + src_expr->module_offset)};
+        break;
       case RefNull:
         *dest_val = wasm_rt_funcref_null_value;
         break;
diff --git a/test/wasm2c/hello.txt b/test/wasm2c/hello.txt
index 13bd422..15d5a57 100644
--- a/test/wasm2c/hello.txt
+++ b/test/wasm2c/hello.txt
@@ -843,7 +843,12 @@
   LOAD_DATA((*dest), dest_addr, src + src_addr, n);
 }
 
-typedef enum { RefFunc, RefNull, GlobalGet } wasm_elem_segment_expr_type_t;
+typedef enum {
+  RefFunc,
+  RefNull,
+  GlobalGet,
+  RefImportFunc
+} wasm_elem_segment_expr_type_t;
 
 typedef struct {
   wasm_elem_segment_expr_type_t expr_type;
@@ -872,6 +877,11 @@
             src_expr->type, src_expr->func, src_expr->func_tailcallee,
             (char*)module_instance + src_expr->module_offset};
         break;
+      case RefImportFunc:
+        *dest_val = (wasm_rt_funcref_t){
+            src_expr->type, src_expr->func, src_expr->func_tailcallee,
+            *(void**)((char*)module_instance + src_expr->module_offset)};
+        break;
       case RefNull:
         *dest_val = wasm_rt_funcref_null_value;
         break;
@@ -1008,7 +1018,7 @@
 }
 
 static const wasm_elem_segment_expr_t elem_segment_exprs_w2c_test_e0[] = {
-  {RefFunc, w2c_test_t0, (wasm_rt_function_ptr_t)wrap_w2c_wasi__snapshot__preview1_fd_write, {wasm_tailcall_w2c_wasi__snapshot__preview1_fd_write}, offsetof(w2c_test, w2c_wasi__snapshot__preview1_instance)},
+  {RefImportFunc, w2c_test_t0, (wasm_rt_function_ptr_t)wrap_w2c_wasi__snapshot__preview1_fd_write, {wasm_tailcall_w2c_wasi__snapshot__preview1_fd_write}, offsetof(w2c_test, w2c_wasi__snapshot__preview1_instance)},
 };
 
 static void init_tables(w2c_test* instance) {
diff --git a/test/wasm2c/import-in-elem.txt b/test/wasm2c/import-in-elem.txt
new file mode 100644
index 0000000..3944157
--- /dev/null
+++ b/test/wasm2c/import-in-elem.txt
@@ -0,0 +1,19 @@
+;;; TOOL: run-spec-wasm2c
+;; An imported function put in a table by an element segment, called with
+;; call_indirect: the host function must get the instance of the import (the
+;; prefix of the spec tests checks it in spectest.print_i32), as it does when
+;; it is called directly.
+(module
+  (import "spectest" "print_i32" (func $print (param i32)))
+  (type $t (func (param i32)))
+  (table 2 funcref)
+  (elem (i32.const 0) $print)
+  (func (export "direct") (call $print (i32.const 1)))
+  (func (export "indirect") (call_indirect (type $t) (i32.const 2) (i32.const 0))))
+(assert_return (invoke "direct"))
+(assert_return (invoke "indirect"))
+(;; STDOUT ;;;
+spectest.print_i32(1)
+spectest.print_i32(2)
+2/2 tests passed.
+;;; STDOUT ;;)
diff --git a/test/wasm2c/minimal.txt b/test/wasm2c/minimal.txt
index c353250..1f91163 100644
--- a/test/wasm2c/minimal.txt
+++ b/test/wasm2c/minimal.txt
@@ -803,7 +803,12 @@
   LOAD_DATA((*dest), dest_addr, src + src_addr, n);
 }
 
-typedef enum { RefFunc, RefNull, GlobalGet } wasm_elem_segment_expr_type_t;
+typedef enum {
+  RefFunc,
+  RefNull,
+  GlobalGet,
+  RefImportFunc
+} wasm_elem_segment_expr_type_t;
 
 typedef struct {
   wasm_elem_segment_expr_type_t expr_type;
@@ -832,6 +837,11 @@
             src_expr->type, src_expr->func, src_expr->func_tailcallee,
             (char*)module_instance + src_expr->module_offset};
         break;
+      case RefImportFunc:
+        *dest_val = (wasm_rt_funcref_t){
+            src_expr->type, src_expr->func, src_expr->func_tailcallee,
+            *(void**)((char*)module_instance + src_expr->module_offset)};
+        break;
       case RefNull:
         *dest_val = wasm_rt_funcref_null_value;
         break;
diff --git a/test/wasm2c/tail-calls.txt b/test/wasm2c/tail-calls.txt
index d25dc9d..870fd11 100644
--- a/test/wasm2c/tail-calls.txt
+++ b/test/wasm2c/tail-calls.txt
@@ -832,7 +832,12 @@
   LOAD_DATA((*dest), dest_addr, src + src_addr, n);
 }
 
-typedef enum { RefFunc, RefNull, GlobalGet } wasm_elem_segment_expr_type_t;
+typedef enum {
+  RefFunc,
+  RefNull,
+  GlobalGet,
+  RefImportFunc
+} wasm_elem_segment_expr_type_t;
 
 typedef struct {
   wasm_elem_segment_expr_type_t expr_type;
@@ -861,6 +866,11 @@
             src_expr->type, src_expr->func, src_expr->func_tailcallee,
             (char*)module_instance + src_expr->module_offset};
         break;
+      case RefImportFunc:
+        *dest_val = (wasm_rt_funcref_t){
+            src_expr->type, src_expr->func, src_expr->func_tailcallee,
+            *(void**)((char*)module_instance + src_expr->module_offset)};
+        break;
       case RefNull:
         *dest_val = wasm_rt_funcref_null_value;
         break;
@@ -985,7 +995,7 @@
 }
 
 static const wasm_elem_segment_expr_t elem_segment_exprs_w2c_test_e0[] = {
-  {RefFunc, w2c_test_i32_f32, (wasm_rt_function_ptr_t)wrap_w2c_spectest_print_i32_f32, {wasm_tailcall_w2c_spectest_print_i32_f32}, offsetof(w2c_test, w2c_spectest_instance)},
+  {RefImportFunc, w2c_test_i32_f32, (wasm_rt_function_ptr_t)wrap_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) {