Skip to content

Commit c117ee2

Browse files
committed
Respond to review
1 parent 5fd4b71 commit c117ee2

File tree

12 files changed

+164
-78
lines changed

12 files changed

+164
-78
lines changed

src/c-writer.cc

Lines changed: 21 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,7 @@ class CWriter {
413413
void WriteInitInstanceImport();
414414
void WriteImportProperties(CWriterPhase);
415415
void WriteFuncs();
416+
void BeginFunction(const Func&);
416417
void Write(const Func&);
417418
void WriteTailCallee(const Func&);
418419
void WriteParamsAndLocals();
@@ -537,8 +538,8 @@ static constexpr char kLabelSuffix = kParamSuffix + 1;
537538
static constexpr char kGlobalSymbolPrefix[] = "w2c_";
538539
static constexpr char kLocalSymbolPrefix[] = "var_";
539540
static constexpr char kAdminSymbolPrefix[] = "wasm2c_";
540-
static constexpr char kTailCallSymbolPrefix[] = "wasm2c_tailcall_";
541-
static constexpr char kTailCallFallbackPrefix[] = "wasm2c_fallback_";
541+
static constexpr char kTailCallSymbolPrefix[] = "wasm_tailcall_";
542+
static constexpr char kTailCallFallbackPrefix[] = "wasm_fallback_";
542543
static constexpr unsigned int kTailCallStackSize = 1024;
543544

544545
size_t CWriter::MarkTypeStack() const {
@@ -1829,21 +1830,11 @@ void CWriter::WriteTailCallWeakImports() {
18291830
Write(Newline(), "/* handler for missing tail-call on import: '",
18301831
SanitizeForComment(import->module_name), "' '",
18311832
SanitizeForComment(import->field_name), "' */", Newline());
1832-
Write("#ifdef _MSC_VER", Newline(),
1833-
"#pragma comment(linker, \"/alternatename:",
1834-
TailCallExportName(import->module_name, import->field_name), "=",
1833+
Write("WEAK_FUNC_DECL(",
1834+
TailCallExportName(import->module_name, import->field_name), ", ",
18351835
kTailCallFallbackPrefix, module_prefix_, "_",
1836-
ExportName(import->module_name, import->field_name), "\")",
1837-
Newline());
1838-
WriteTailCallFuncDeclaration(
1839-
kTailCallFallbackPrefix + module_prefix_ + '_' +
1840-
ExportName(import->module_name, import->field_name));
1841-
Write(Newline(), "#else", Newline());
1842-
Write("__attribute__((weak)) ");
1843-
WriteTailCallFuncDeclaration(
1844-
TailCallExportName(import->module_name, import->field_name));
1845-
Write(Newline(), "#endif", Newline(), OpenBrace());
1846-
Write("next->fn = NULL;", Newline());
1836+
ExportName(import->module_name, import->field_name), ")", Newline());
1837+
Write(OpenBrace(), "next->fn = NULL;", Newline());
18471838

18481839
Index num_params = func.GetNumParams();
18491840
Index num_results = func.GetNumResults();
@@ -2855,7 +2846,7 @@ void CWriter::WriteUnwindTryCatchStack(const Label* label) {
28552846

28562847
void CWriter::FinishReturnCall() {
28572848
if (in_tail_callee_) {
2858-
Write("return;", Newline());
2849+
Write(CloseBrace(), Newline(), "return;", Newline());
28592850
return;
28602851
}
28612852

@@ -2872,10 +2863,11 @@ void CWriter::FinishReturnCall() {
28722863
Write(CloseBrace(), Newline());
28732864
}
28742865

2875-
Write("goto ", LabelName(kImplicitFuncLabel), ";", Newline());
2866+
Write(CloseBrace(), Newline(), "goto ", LabelName(kImplicitFuncLabel), ";",
2867+
Newline());
28762868
}
28772869

2878-
void CWriter::Write(const Func& func) {
2870+
void CWriter::BeginFunction(const Func& func) {
28792871
func_ = &func;
28802872
in_tail_callee_ = false;
28812873
local_syms_.clear();
@@ -2884,8 +2876,6 @@ void CWriter::Write(const Func& func) {
28842876
func_sections_.clear();
28852877
func_includes_.clear();
28862878

2887-
Stream* prev_stream = stream_;
2888-
28892879
/*
28902880
* If offset of stream_ is 0, this is the first time some function is written
28912881
* to this stream, then write multi c top.
@@ -2894,7 +2884,11 @@ void CWriter::Write(const Func& func) {
28942884
WriteMultiCTop();
28952885
}
28962886
Write(Newline());
2887+
}
28972888

2889+
void CWriter::Write(const Func& func) {
2890+
Stream* prev_stream = stream_;
2891+
BeginFunction(func);
28982892
PushFuncSection();
28992893
Write(func.decl.sig.result_types, " ",
29002894
GlobalName(ModuleFieldType::Func, func.name), "(");
@@ -2944,18 +2938,9 @@ void CWriter::Write(const Func& func) {
29442938
}
29452939

29462940
void CWriter::WriteTailCallee(const Func& func) {
2947-
func_ = &func;
2948-
in_tail_callee_ = true;
2949-
local_syms_.clear();
2950-
local_sym_map_.clear();
2951-
stack_var_sym_map_.clear();
2952-
func_sections_.clear();
2953-
func_includes_.clear();
2954-
29552941
Stream* prev_stream = stream_;
2956-
2957-
Write(Newline());
2958-
2942+
BeginFunction(func);
2943+
in_tail_callee_ = true;
29592944
PushFuncSection();
29602945
WriteTailCallFuncDeclaration(GetTailCallRef(func.name));
29612946
Write(" ", OpenBrace());
@@ -3951,7 +3936,6 @@ void CWriter::Write(const ExprList& exprs) {
39513936
}
39523937
DropTypes(num_params);
39533938
FinishReturnCall();
3954-
Write(CloseBrace(), Newline());
39553939
return;
39563940
}
39573941

@@ -3974,14 +3958,15 @@ void CWriter::Write(const ExprList& exprs) {
39743958
auto ci = std::make_unique<CallIndirectExpr>(inst->loc);
39753959
std::tie(ci->decl, ci->table) = std::make_pair(inst->decl, inst->table);
39763960
Write(ExprList{std::move(ci)});
3977-
Write("goto ", LabelName(kImplicitFuncLabel), ";", Newline());
3978-
Write(CloseBrace(), Newline());
3961+
if (in_tail_callee_) {
3962+
Write("next->fn = NULL;", Newline());
3963+
}
3964+
Write(CloseBrace(), " else ", OpenBrace());
39793965

39803966
DropTypes(decl.GetNumResults());
39813967
PushTypes(decl.sig.param_types);
39823968
PushType(Type::I32);
39833969

3984-
Write(OpenBrace());
39853970
if (!in_tail_callee_) {
39863971
WriteTailCallStack();
39873972
}
@@ -4008,7 +3993,6 @@ void CWriter::Write(const ExprList& exprs) {
40083993

40093994
DropTypes(num_params + 1);
40103995
FinishReturnCall();
4011-
Write(CloseBrace(), Newline());
40123996
return;
40133997
}
40143998

src/prebuilt/wasm2c_source_declarations.cc

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1174,4 +1174,27 @@ R"w2c_template( extern int(*wasm2c_assert(void))[!!sizeof(struct { int x : (X)
11741174
)w2c_template"
11751175
R"w2c_template(#endif
11761176
)w2c_template"
1177+
R"w2c_template(
1178+
#ifdef _MSC_VER
1179+
)w2c_template"
1180+
R"w2c_template(#define DO_PRAGMA(x) _Pragma(#x)
1181+
)w2c_template"
1182+
R"w2c_template(#define WEAK_FUNC_DECL(func, fallback) \
1183+
)w2c_template"
1184+
R"w2c_template( DO_PRAGMA("comment(linker, \"/alternatename:" #func "=" #fallback "\")"); \
1185+
)w2c_template"
1186+
R"w2c_template( void fallback(void** instance_ptr, void* tail_call_stack, \
1187+
)w2c_template"
1188+
R"w2c_template( wasm_rt_tailcallee_t* next)
1189+
)w2c_template"
1190+
R"w2c_template(#else
1191+
)w2c_template"
1192+
R"w2c_template(#define WEAK_FUNC_DECL(func, fallback) \
1193+
)w2c_template"
1194+
R"w2c_template( __attribute__((weak)) void func(void** instance_ptr, void* tail_call_stack, \
1195+
)w2c_template"
1196+
R"w2c_template( wasm_rt_tailcallee_t* next)
1197+
)w2c_template"
1198+
R"w2c_template(#endif
1199+
)w2c_template"
11771200
;

src/template/wasm2c.declarations.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,3 +626,15 @@ DEFINE_TABLE_FILL(externref)
626626
#define wasm_static_assert(X) \
627627
extern int(*wasm2c_assert(void))[!!sizeof(struct { int x : (X) ? 2 : -1; })];
628628
#endif
629+
630+
#ifdef _MSC_VER
631+
#define DO_PRAGMA(x) _Pragma(#x)
632+
#define WEAK_FUNC_DECL(func, fallback) \
633+
DO_PRAGMA("comment(linker, \"/alternatename:" #func "=" #fallback "\")"); \
634+
void fallback(void** instance_ptr, void* tail_call_stack, \
635+
wasm_rt_tailcallee_t* next)
636+
#else
637+
#define WEAK_FUNC_DECL(func, fallback) \
638+
__attribute__((weak)) void func(void** instance_ptr, void* tail_call_stack, \
639+
wasm_rt_tailcallee_t* next)
640+
#endif

test/pipes.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
;;; RUN: bash -c '%(wasm-stats)s <(%(wat2wasm)s /dev/stdin <<<"(module)" -o /dev/stdout)'
33
(;; STDOUT ;;;
44
Total opcodes: 0
5+
56
Opcode counts:
7+
68
Opcode counts with immediates:
79
;;; STDOUT ;;)

test/wasm2c/add.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,18 @@ DEFINE_TABLE_FILL(externref)
694694
extern int(*wasm2c_assert(void))[!!sizeof(struct { int x : (X) ? 2 : -1; })];
695695
#endif
696696

697+
#ifdef _MSC_VER
698+
#define DO_PRAGMA(x) _Pragma(#x)
699+
#define WEAK_FUNC_DECL(func, fallback) \
700+
DO_PRAGMA("comment(linker, \"/alternatename:" #func "=" #fallback "\")"); \
701+
void fallback(void** instance_ptr, void* tail_call_stack, \
702+
wasm_rt_tailcallee_t* next)
703+
#else
704+
#define WEAK_FUNC_DECL(func, fallback) \
705+
__attribute__((weak)) void func(void** instance_ptr, void* tail_call_stack, \
706+
wasm_rt_tailcallee_t* next)
707+
#endif
708+
697709
static u32 w2c_test_add_0(w2c_test*, u32, u32);
698710

699711
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";

test/wasm2c/check-imports.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -717,6 +717,18 @@ DEFINE_TABLE_FILL(externref)
717717
extern int(*wasm2c_assert(void))[!!sizeof(struct { int x : (X) ? 2 : -1; })];
718718
#endif
719719

720+
#ifdef _MSC_VER
721+
#define DO_PRAGMA(x) _Pragma(#x)
722+
#define WEAK_FUNC_DECL(func, fallback) \
723+
DO_PRAGMA("comment(linker, \"/alternatename:" #func "=" #fallback "\")"); \
724+
void fallback(void** instance_ptr, void* tail_call_stack, \
725+
wasm_rt_tailcallee_t* next)
726+
#else
727+
#define WEAK_FUNC_DECL(func, fallback) \
728+
__attribute__((weak)) void func(void** instance_ptr, void* tail_call_stack, \
729+
wasm_rt_tailcallee_t* next)
730+
#endif
731+
720732
static u32 w2c_test_f0(w2c_test*, u32);
721733
static u32 w2c_test_f1(w2c_test*);
722734
static u32 w2c_test_f2(w2c_test*, u32, u32);

test/wasm2c/export-names.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -717,6 +717,18 @@ DEFINE_TABLE_FILL(externref)
717717
extern int(*wasm2c_assert(void))[!!sizeof(struct { int x : (X) ? 2 : -1; })];
718718
#endif
719719

720+
#ifdef _MSC_VER
721+
#define DO_PRAGMA(x) _Pragma(#x)
722+
#define WEAK_FUNC_DECL(func, fallback) \
723+
DO_PRAGMA("comment(linker, \"/alternatename:" #func "=" #fallback "\")"); \
724+
void fallback(void** instance_ptr, void* tail_call_stack, \
725+
wasm_rt_tailcallee_t* next)
726+
#else
727+
#define WEAK_FUNC_DECL(func, fallback) \
728+
__attribute__((weak)) void func(void** instance_ptr, void* tail_call_stack, \
729+
wasm_rt_tailcallee_t* next)
730+
#endif
731+
720732
static void w2c_test__0(w2c_test*);
721733

722734
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";

test/wasm2c/hello.txt

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,11 @@ wasm_rt_func_type_t wasm2c_test_get_func_type(uint32_t param_count, uint32_t res
6262

6363
/* import: 'wasi_snapshot_preview1' 'fd_write' */
6464
u32 w2c_wasi__snapshot__preview1_fd_write(struct w2c_wasi__snapshot__preview1*, u32, u32, u32, u32);
65-
void wasm2c_tailcall_w2c_wasi__snapshot__preview1_fd_write(void **instance_ptr, void *tail_call_stack, wasm_rt_tailcallee_t *next);
65+
void wasm_tailcall_w2c_wasi__snapshot__preview1_fd_write(void **instance_ptr, void *tail_call_stack, wasm_rt_tailcallee_t *next);
6666

6767
/* import: 'wasi_snapshot_preview1' 'proc_exit' */
6868
void w2c_wasi__snapshot__preview1_proc_exit(struct w2c_wasi__snapshot__preview1*, u32);
69-
void wasm2c_tailcall_w2c_wasi__snapshot__preview1_proc_exit(void **instance_ptr, void *tail_call_stack, wasm_rt_tailcallee_t *next);
69+
void wasm_tailcall_w2c_wasi__snapshot__preview1_proc_exit(void **instance_ptr, void *tail_call_stack, wasm_rt_tailcallee_t *next);
7070

7171
/* export: 'memory' */
7272
wasm_rt_memory_t* w2c_test_memory(w2c_test* instance);
@@ -727,6 +727,18 @@ DEFINE_TABLE_FILL(externref)
727727
extern int(*wasm2c_assert(void))[!!sizeof(struct { int x : (X) ? 2 : -1; })];
728728
#endif
729729

730+
#ifdef _MSC_VER
731+
#define DO_PRAGMA(x) _Pragma(#x)
732+
#define WEAK_FUNC_DECL(func, fallback) \
733+
DO_PRAGMA("comment(linker, \"/alternatename:" #func "=" #fallback "\")"); \
734+
void fallback(void** instance_ptr, void* tail_call_stack, \
735+
wasm_rt_tailcallee_t* next)
736+
#else
737+
#define WEAK_FUNC_DECL(func, fallback) \
738+
__attribute__((weak)) void func(void** instance_ptr, void* tail_call_stack, \
739+
wasm_rt_tailcallee_t* next)
740+
#endif
741+
730742
static void w2c_test_0x5Fstart_0(w2c_test*);
731743

732744
#ifndef wasm_multi_iiii
@@ -757,7 +769,7 @@ static void init_data_instances(w2c_test *instance) {
757769
}
758770

759771
static const wasm_elem_segment_expr_t elem_segment_exprs_w2c_test_e0[] = {
760-
{RefFunc, w2c_test_t0, (wasm_rt_function_ptr_t)w2c_wasi__snapshot__preview1_fd_write, {wasm2c_tailcall_w2c_wasi__snapshot__preview1_fd_write}, offsetof(w2c_test, w2c_wasi__snapshot__preview1_instance)},
772+
{RefFunc, w2c_test_t0, (wasm_rt_function_ptr_t)w2c_wasi__snapshot__preview1_fd_write, {wasm_tailcall_w2c_wasi__snapshot__preview1_fd_write}, offsetof(w2c_test, w2c_wasi__snapshot__preview1_instance)},
761773
};
762774

763775
static void init_tables(w2c_test* instance) {
@@ -779,12 +791,7 @@ void w2c_test_0x5Fstart(w2c_test* instance) {
779791
}
780792

781793
/* handler for missing tail-call on import: 'wasi_snapshot_preview1' 'fd_write' */
782-
#ifdef _MSC_VER
783-
#pragma comment(linker, "/alternatename:wasm2c_tailcall_w2c_wasi__snapshot__preview1_fd_write=wasm2c_fallback_test_w2c_wasi__snapshot__preview1_fd_write")
784-
void wasm2c_fallback_test_w2c_wasi__snapshot__preview1_fd_write(void **instance_ptr, void *tail_call_stack, wasm_rt_tailcallee_t *next)
785-
#else
786-
__attribute__((weak)) void wasm2c_tailcall_w2c_wasi__snapshot__preview1_fd_write(void **instance_ptr, void *tail_call_stack, wasm_rt_tailcallee_t *next)
787-
#endif
794+
WEAK_FUNC_DECL(wasm_tailcall_w2c_wasi__snapshot__preview1_fd_write, wasm_fallback_test_w2c_wasi__snapshot__preview1_fd_write)
788795
{
789796
next->fn = NULL;
790797
struct wasm_multi_iiii *params = tail_call_stack;
@@ -793,12 +800,7 @@ __attribute__((weak)) void wasm2c_tailcall_w2c_wasi__snapshot__preview1_fd_write
793800
}
794801

795802
/* handler for missing tail-call on import: 'wasi_snapshot_preview1' 'proc_exit' */
796-
#ifdef _MSC_VER
797-
#pragma comment(linker, "/alternatename:wasm2c_tailcall_w2c_wasi__snapshot__preview1_proc_exit=wasm2c_fallback_test_w2c_wasi__snapshot__preview1_proc_exit")
798-
void wasm2c_fallback_test_w2c_wasi__snapshot__preview1_proc_exit(void **instance_ptr, void *tail_call_stack, wasm_rt_tailcallee_t *next)
799-
#else
800-
__attribute__((weak)) void wasm2c_tailcall_w2c_wasi__snapshot__preview1_proc_exit(void **instance_ptr, void *tail_call_stack, wasm_rt_tailcallee_t *next)
801-
#endif
803+
WEAK_FUNC_DECL(wasm_tailcall_w2c_wasi__snapshot__preview1_proc_exit, wasm_fallback_test_w2c_wasi__snapshot__preview1_proc_exit)
802804
{
803805
next->fn = NULL;
804806
u32 param = *(u32*)tail_call_stack;

test/wasm2c/minimal.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,18 @@ DEFINE_TABLE_FILL(externref)
688688
extern int(*wasm2c_assert(void))[!!sizeof(struct { int x : (X) ? 2 : -1; })];
689689
#endif
690690

691+
#ifdef _MSC_VER
692+
#define DO_PRAGMA(x) _Pragma(#x)
693+
#define WEAK_FUNC_DECL(func, fallback) \
694+
DO_PRAGMA("comment(linker, \"/alternatename:" #func "=" #fallback "\")"); \
695+
void fallback(void** instance_ptr, void* tail_call_stack, \
696+
wasm_rt_tailcallee_t* next)
697+
#else
698+
#define WEAK_FUNC_DECL(func, fallback) \
699+
__attribute__((weak)) void func(void** instance_ptr, void* tail_call_stack, \
700+
wasm_rt_tailcallee_t* next)
701+
#endif
702+
691703
void wasm2c_test_instantiate(w2c_test* instance) {
692704
assert(wasm_rt_is_initialized());
693705
}

0 commit comments

Comments
 (0)