Skip to content

Commit

Permalink
c-writer.cc: cleanup name lookups to happen consistently in one place
Browse files Browse the repository at this point in the history
  • Loading branch information
keithw committed Apr 23, 2023
1 parent 10bdb51 commit 0b82271
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions src/c-writer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ class CWriter {
static void SerializeFuncType(const FuncType&, std::string&);

std::string GetGlobalName(ModuleFieldType, const std::string&) const;
std::string GetLocalName(const std::string&, bool is_label) const;

void Indent(int size = INDENT_SIZE);
void Dedent(int size = INDENT_SIZE);
Expand Down Expand Up @@ -910,6 +911,13 @@ std::string CWriter::DefineLocalScopeName(std::string_view name,
kLocalSymbolPrefix + MangleName(StripLeadingDollar(name)));
}

std::string CWriter::GetLocalName(const std::string& name,
bool is_label) const {
std::string mangled = name + (is_label ? kLabelSuffix : kParamSuffix);
assert(local_sym_map_.count(mangled) == 1);
return local_sym_map_.at(mangled);
}

std::string CWriter::DefineParamName(std::string_view name) {
return DefineLocalScopeName(name, false);
}
Expand Down Expand Up @@ -1021,21 +1029,15 @@ void CWriter::Write(std::string_view s) {
}

void CWriter::Write(const ParamName& name) {
std::string mangled = name.name + kParamSuffix;
assert(local_sym_map_.count(mangled) == 1);
Write(local_sym_map_[mangled]);
Write(GetLocalName(name.name, false));
}

void CWriter::Write(const LabelName& name) {
std::string mangled = name.name + kLabelSuffix;
assert(local_sym_map_.count(mangled) == 1);
Write(local_sym_map_[mangled]);
Write(GetLocalName(name.name, true));
}

void CWriter::Write(const GlobalName& name) {
std::string mangled = name.name + MangleField(name.type);
assert(global_sym_map_.count(mangled) == 1);
Write(global_sym_map_.at(mangled));
Write(GetGlobalName(name.type, name.name));
}

void CWriter::Write(const ExternalPtr& name) {
Expand Down

0 comments on commit 0b82271

Please sign in to comment.