Skip to content

Commit

Permalink
c-writer.cc: Add local symbol prefix. (#2171)
Browse files Browse the repository at this point in the history
Add kLocalSymbolPrefix which is used for names of params, locals and
stack vars. This allows c-writer to not assign global_sym_map_ to
local_sym_map_ for writing each individual function, since local names
can't duplicate global names.
  • Loading branch information
yhdengh authored Mar 15, 2023
1 parent 0ab3ee7 commit 905f614
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 66 deletions.
29 changes: 15 additions & 14 deletions src/c-writer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,8 @@ static constexpr char kParamSuffix =
'a' + static_cast<char>(ModuleFieldType::Tag) + 1;
static constexpr char kLabelSuffix = kParamSuffix + 1;

static constexpr char kSymbolPrefix[] = "w2c_";
static constexpr char kGlobalSymbolPrefix[] = "w2c_";
static constexpr char kLocalSymbolPrefix[] = "var_";
static constexpr char kAdminSymbolPrefix[] = "wasm2c_";

size_t CWriter::MarkTypeStack() const {
Expand Down Expand Up @@ -567,26 +568,26 @@ std::string CWriter::MangleTagTypes(const TypeVector& types) {

/* The C symbol for an export from this module. */
std::string CWriter::ExportName(std::string_view export_name) {
return kSymbolPrefix + module_prefix_ + '_' + MangleName(export_name);
return kGlobalSymbolPrefix + module_prefix_ + '_' + MangleName(export_name);
}

/* The C symbol for an export from an arbitrary module. */
// static
std::string CWriter::ExportName(std::string_view module_name,
std::string_view export_name) {
return kSymbolPrefix + MangleModuleName(module_name) + '_' +
return kGlobalSymbolPrefix + MangleModuleName(module_name) + '_' +
MangleName(export_name);
}

/* The type name of an instance of this module. */
std::string CWriter::ModuleInstanceTypeName() const {
return kSymbolPrefix + module_prefix_;
return kGlobalSymbolPrefix + module_prefix_;
}

/* The type name of an instance of an arbitrary module. */
// static
std::string CWriter::ModuleInstanceTypeName(std::string_view module_name) {
return kSymbolPrefix + MangleModuleName(module_name);
return kGlobalSymbolPrefix + MangleModuleName(module_name);
}

/*
Expand Down Expand Up @@ -827,12 +828,12 @@ std::string CWriter::DefineGlobalScopeName(ModuleFieldType type,
ExportName(StripLeadingDollar(name)));
}

/* Names for params, locals, and stack vars are formatted as "w2c_" + name. */
/* Names for params, locals, and stack vars are formatted as "var_" + name. */
std::string CWriter::DefineLocalScopeName(std::string_view name,
bool is_label) {
return ClaimUniqueName(local_syms_, local_sym_map_,
is_label ? kLabelSuffix : kParamSuffix, name,
kSymbolPrefix + MangleName(StripLeadingDollar(name)));
return ClaimUniqueName(
local_syms_, local_sym_map_, is_label ? kLabelSuffix : kParamSuffix, name,
kLocalSymbolPrefix + MangleName(StripLeadingDollar(name)));
}

std::string CWriter::DefineParamName(std::string_view name) {
Expand All @@ -847,7 +848,7 @@ std::string CWriter::DefineStackVarName(Index index,
Type type,
std::string_view name) {
std::string unique =
FindUniqueName(local_syms_, kSymbolPrefix + MangleName(name));
FindUniqueName(local_syms_, kLocalSymbolPrefix + MangleName(name));
StackTypePair stp = {index, type};
[[maybe_unused]] bool success =
stack_var_sym_map_.emplace(stp, unique).second;
Expand All @@ -861,8 +862,9 @@ std::string CWriter::DefineStackVarName(Index index,
*/
std::string CWriter::DefineInstanceMemberName(ModuleFieldType type,
std::string_view name) {
return ClaimUniqueName(global_syms_, global_sym_map_, MangleField(type), name,
kSymbolPrefix + MangleName(StripLeadingDollar(name)));
return ClaimUniqueName(
global_syms_, global_sym_map_, MangleField(type), name,
kGlobalSymbolPrefix + MangleName(StripLeadingDollar(name)));
}

/*
Expand Down Expand Up @@ -2359,8 +2361,7 @@ void CWriter::PushFuncSection(std::string_view include_condition) {

void CWriter::Write(const Func& func) {
func_ = &func;
// Copy symbols from global symbol table so we don't shadow them.
local_syms_ = global_syms_;
local_syms_.clear();
local_sym_map_.clear();
stack_var_sym_map_.clear();
func_sections_.clear();
Expand Down
16 changes: 8 additions & 8 deletions test/wasm2c/add.txt
Original file line number Diff line number Diff line change
Expand Up @@ -786,19 +786,19 @@ FUNC_TYPE_T(w2c_test_t0) = "\x92\xfb\x6a\xdf\x49\x07\x0a\x83\xbe\x08\x02\x68\xcd
static u32 w2c_test_add_0(w2c_test*, u32, u32);
static u32 w2c_test_add_0(w2c_test* instance, u32 w2c_p0, u32 w2c_p1) {
static u32 w2c_test_add_0(w2c_test* instance, u32 var_p0, u32 var_p1) {
FUNC_PROLOGUE;
u32 w2c_i0, w2c_i1;
w2c_i0 = w2c_p0;
w2c_i1 = w2c_p1;
w2c_i0 += w2c_i1;
u32 var_i0, var_i1;
var_i0 = var_p0;
var_i1 = var_p1;
var_i0 += var_i1;
FUNC_EPILOGUE;
return w2c_i0;
return var_i0;
}
/* export: 'add' */
u32 w2c_test_add(w2c_test* instance, u32 w2c_p0, u32 w2c_p1) {
return w2c_test_add_0(instance, w2c_p0, w2c_p1);
u32 w2c_test_add(w2c_test* instance, u32 var_p0, u32 var_p1) {
return w2c_test_add_0(instance, var_p0, var_p1);
}
void wasm2c_test_instantiate(w2c_test* instance) {
Expand Down
28 changes: 14 additions & 14 deletions test/wasm2c/hello.txt
Original file line number Diff line number Diff line change
Expand Up @@ -846,20 +846,20 @@ static void init_elem_instances(w2c_test *instance) {
static void w2c_test_0x5Fstart_0(w2c_test* instance) {
FUNC_PROLOGUE;
u32 w2c_i0, w2c_i1, w2c_i2, w2c_i3, w2c_i4;
w2c_i0 = 0u;
w2c_i1 = 8u;
i32_store(&instance->w2c_memory, (u64)(w2c_i0), w2c_i1);
w2c_i0 = 4u;
w2c_i1 = 14u;
i32_store(&instance->w2c_memory, (u64)(w2c_i0), w2c_i1);
w2c_i0 = 1u;
w2c_i1 = 0u;
w2c_i2 = 1u;
w2c_i3 = 0u;
w2c_i4 = 0u;
w2c_i0 = CALL_INDIRECT(instance->w2c_T0, u32 (*)(void*, u32, u32, u32, u32), w2c_test_t0, w2c_i4, instance->w2c_T0.data[w2c_i4].module_instance, w2c_i0, w2c_i1, w2c_i2, w2c_i3);
(*w2c_wasi__snapshot__preview1_proc_exit)(instance->w2c_wasi__snapshot__preview1_instance, w2c_i0);
u32 var_i0, var_i1, var_i2, var_i3, var_i4;
var_i0 = 0u;
var_i1 = 8u;
i32_store(&instance->w2c_memory, (u64)(var_i0), var_i1);
var_i0 = 4u;
var_i1 = 14u;
i32_store(&instance->w2c_memory, (u64)(var_i0), var_i1);
var_i0 = 1u;
var_i1 = 0u;
var_i2 = 1u;
var_i3 = 0u;
var_i4 = 0u;
var_i0 = CALL_INDIRECT(instance->w2c_T0, u32 (*)(void*, u32, u32, u32, u32), w2c_test_t0, var_i4, instance->w2c_T0.data[var_i4].module_instance, var_i0, var_i1, var_i2, var_i3);
(*w2c_wasi__snapshot__preview1_proc_exit)(instance->w2c_wasi__snapshot__preview1_instance, var_i0);
FUNC_EPILOGUE;
}
Expand Down
28 changes: 14 additions & 14 deletions wasm2c/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -458,24 +458,24 @@ module doesn't use any globals, memory or tables.
The most interesting part is the definition of the function `fac`:

```c
static u32 w2c_fac_fac_0(w2c_fac* instance, u32 w2c_p0) {
static u32 w2c_fac_fac_0(w2c_fac* instance, u32 var_p0) {
FUNC_PROLOGUE;
u32 w2c_i0, w2c_i1, w2c_i2;
w2c_i0 = w2c_p0;
w2c_i1 = 0u;
w2c_i0 = w2c_i0 == w2c_i1;
if (w2c_i0) {
w2c_i0 = 1u;
u32 var_i0, var_i1, var_i2;
var_i0 = var_p0;
var_i1 = 0u;
var_i0 = var_i0 == var_i1;
if (var_i0) {
var_i0 = 1u;
} else {
w2c_i0 = w2c_p0;
w2c_i1 = w2c_p0;
w2c_i2 = 1u;
w2c_i1 -= w2c_i2;
w2c_i1 = w2c_fac_fac_0(instance, w2c_i1);
w2c_i0 *= w2c_i1;
var_i0 = var_p0;
var_i1 = var_p0;
var_i2 = 1u;
var_i1 -= var_i2;
var_i1 = w2c_fac_fac_0(instance, var_i1);
var_i0 *= var_i1;
}
FUNC_EPILOGUE;
return w2c_i0;
return var_i0;
}
```
Expand Down
32 changes: 16 additions & 16 deletions wasm2c/examples/fac/fac.c
Original file line number Diff line number Diff line change
Expand Up @@ -728,29 +728,29 @@ FUNC_TYPE_T(w2c_fac_t0) = "\x07\x80\x96\x7a\x42\xf7\x3e\xe6\x70\x5c\x2f\xac\x83\

static u32 w2c_fac_fac_0(w2c_fac*, u32);

static u32 w2c_fac_fac_0(w2c_fac* instance, u32 w2c_p0) {
static u32 w2c_fac_fac_0(w2c_fac* instance, u32 var_p0) {
FUNC_PROLOGUE;
u32 w2c_i0, w2c_i1, w2c_i2;
w2c_i0 = w2c_p0;
w2c_i1 = 0u;
w2c_i0 = w2c_i0 == w2c_i1;
if (w2c_i0) {
w2c_i0 = 1u;
u32 var_i0, var_i1, var_i2;
var_i0 = var_p0;
var_i1 = 0u;
var_i0 = var_i0 == var_i1;
if (var_i0) {
var_i0 = 1u;
} else {
w2c_i0 = w2c_p0;
w2c_i1 = w2c_p0;
w2c_i2 = 1u;
w2c_i1 -= w2c_i2;
w2c_i1 = w2c_fac_fac_0(instance, w2c_i1);
w2c_i0 *= w2c_i1;
var_i0 = var_p0;
var_i1 = var_p0;
var_i2 = 1u;
var_i1 -= var_i2;
var_i1 = w2c_fac_fac_0(instance, var_i1);
var_i0 *= var_i1;
}
FUNC_EPILOGUE;
return w2c_i0;
return var_i0;
}

/* export: 'fac' */
u32 w2c_fac_fac(w2c_fac* instance, u32 w2c_p0) {
return w2c_fac_fac_0(instance, w2c_p0);
u32 w2c_fac_fac(w2c_fac* instance, u32 var_p0) {
return w2c_fac_fac_0(instance, var_p0);
}

void wasm2c_fac_instantiate(w2c_fac* instance) {
Expand Down

0 comments on commit 905f614

Please sign in to comment.