summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/binaryen-c.cpp6
-rw-r--r--src/wasm.h20
-rw-r--r--src/wasm/wasm-binary.cpp18
-rw-r--r--src/wasm/wasm-s-parser.cpp24
4 files changed, 40 insertions, 28 deletions
diff --git a/src/binaryen-c.cpp b/src/binaryen-c.cpp
index bbacd517b..4a9d0594f 100644
--- a/src/binaryen-c.cpp
+++ b/src/binaryen-c.cpp
@@ -3131,7 +3131,7 @@ BinaryenFunctionRef BinaryenAddFunction(BinaryenModuleRef module,
BinaryenIndex numVarTypes,
BinaryenExpressionRef body) {
auto* ret = new Function;
- ret->name = name;
+ ret->setExplicitName(name);
ret->sig = Signature(Type(params), Type(results));
for (BinaryenIndex i = 0; i < numVarTypes; i++) {
ret->vars.push_back(Type(varTypes[i]));
@@ -3174,7 +3174,7 @@ BinaryenGlobalRef BinaryenAddGlobal(BinaryenModuleRef module,
int8_t mutable_,
BinaryenExpressionRef init) {
auto* ret = new Global();
- ret->name = name;
+ ret->setExplicitName(name);
ret->type = Type(type);
ret->mutable_ = !!mutable_;
ret->init = (Expression*)init;
@@ -3197,7 +3197,7 @@ BinaryenEventRef BinaryenAddEvent(BinaryenModuleRef module,
BinaryenType params,
BinaryenType results) {
auto* ret = new Event();
- ret->name = name;
+ ret->setExplicitName(name);
ret->attribute = attribute;
ret->sig = Signature(Type(params), Type(results));
((Module*)module)->addEvent(ret);
diff --git a/src/wasm.h b/src/wasm.h
index c75c47d1f..40859eefc 100644
--- a/src/wasm.h
+++ b/src/wasm.h
@@ -1335,10 +1335,25 @@ public:
// Globals
struct Importable {
+ Name name;
+
+ // Explicit names are ones that we read from the input file and
+ // will be written the name section in the output file.
+ // Implicit names are names that binaryen generated for internal
+ // use only and will not be written the name section.
+ bool hasExplicitName = false;
+
// If these are set, then this is an import, as module.base
Name module, base;
bool imported() { return module.is(); }
+
+ void setName(Name name_, bool hasExplicitName_) {
+ name = name_;
+ hasExplicitName = hasExplicitName_;
+ }
+
+ void setExplicitName(Name name_) { setName(name_, true); }
};
class Function;
@@ -1411,7 +1426,6 @@ using StackIR = std::vector<StackInst*>;
class Function : public Importable {
public:
- Name name;
Signature sig; // parameters and return value
IRProfile profile = IRProfile::Normal;
std::vector<Type> vars; // non-param locals
@@ -1523,7 +1537,6 @@ public:
// been defined or imported. The table can exist but be empty and have no
// defined initial or max size.
bool exists = false;
- Name name;
Address initial = 0;
Address max = kMaxSize;
std::vector<Segment> segments;
@@ -1569,7 +1582,6 @@ public:
};
bool exists = false;
- Name name;
Address initial = 0; // sizes are in pages
Address max = kMaxSize32;
std::vector<Segment> segments;
@@ -1594,7 +1606,6 @@ public:
class Global : public Importable {
public:
- Name name;
Type type;
Expression* init = nullptr;
bool mutable_ = false;
@@ -1605,7 +1616,6 @@ enum WasmEventAttribute : unsigned { WASM_EVENT_ATTRIBUTE_EXCEPTION = 0x0 };
class Event : public Importable {
public:
- Name name;
// Kind of event. Currently only WASM_EVENT_ATTRIBUTE_EXCEPTION is possible.
uint32_t attribute = WASM_EVENT_ATTRIBUTE_EXCEPTION;
Signature sig;
diff --git a/src/wasm/wasm-binary.cpp b/src/wasm/wasm-binary.cpp
index 9376c88ad..b6e926f6a 100644
--- a/src/wasm/wasm-binary.cpp
+++ b/src/wasm/wasm-binary.cpp
@@ -628,7 +628,7 @@ void WasmBinaryWriter::writeNames() {
}
// table names
- if (wasm->table.exists && wasm->table.name.is()) {
+ if (wasm->table.exists && wasm->table.hasExplicitName) {
auto substart =
startSubsection(BinaryConsts::UserSections::Subsection::NameTable);
o << U32LEB(1) << U32LEB(0); // currently exactly 1 table at index 0
@@ -637,7 +637,7 @@ void WasmBinaryWriter::writeNames() {
}
// memory names
- if (wasm->memory.exists && wasm->memory.name.is()) {
+ if (wasm->memory.exists && wasm->memory.hasExplicitName) {
auto substart =
startSubsection(BinaryConsts::UserSections::Subsection::NameMemory);
o << U32LEB(1) << U32LEB(0); // currently exactly 1 memory at index 0
@@ -650,7 +650,7 @@ void WasmBinaryWriter::writeNames() {
std::vector<std::pair<Index, Global*>> globalsWithNames;
Index checked = 0;
auto check = [&](Global* curr) {
- if (curr->name.is()) {
+ if (curr->hasExplicitName) {
globalsWithNames.push_back({checked, curr});
}
checked++;
@@ -2301,9 +2301,9 @@ void WasmBinaryBuilder::readNames(size_t payloadLen) {
}
auto numFunctionImports = functionImports.size();
if (index < numFunctionImports) {
- functionImports[index]->name = name;
+ functionImports[index]->setExplicitName(name);
} else if (index - numFunctionImports < functions.size()) {
- functions[index - numFunctionImports]->name = name;
+ functions[index - numFunctionImports]->setExplicitName(name);
} else {
std::cerr << "warning: function index out of bounds in name section, "
"function subsection: "
@@ -2358,7 +2358,7 @@ void WasmBinaryBuilder::readNames(size_t payloadLen) {
auto index = getU32LEB();
auto rawName = getInlineString();
if (index == 0) {
- wasm.table.name = escape(rawName);
+ wasm.table.setExplicitName(escape(rawName));
} else {
std::cerr << "warning: table index out of bounds in name section, "
"table subsection: "
@@ -2372,7 +2372,7 @@ void WasmBinaryBuilder::readNames(size_t payloadLen) {
auto index = getU32LEB();
auto rawName = getInlineString();
if (index == 0) {
- wasm.memory.name = escape(rawName);
+ wasm.memory.setExplicitName(escape(rawName));
} else {
std::cerr << "warning: memory index out of bounds in name section, "
"memory subsection: "
@@ -2394,9 +2394,9 @@ void WasmBinaryBuilder::readNames(size_t payloadLen) {
}
auto numGlobalImports = globalImports.size();
if (index < numGlobalImports) {
- globalImports[index]->name = name;
+ globalImports[index]->setExplicitName(name);
} else if (index - numGlobalImports < globals.size()) {
- globals[index - numGlobalImports]->name = name;
+ globals[index - numGlobalImports]->setExplicitName(name);
} else {
std::cerr << "warning: global index out of bounds in name section, "
"global subsection: "
diff --git a/src/wasm/wasm-s-parser.cpp b/src/wasm/wasm-s-parser.cpp
index d3367d3f6..c4de76987 100644
--- a/src/wasm/wasm-s-parser.cpp
+++ b/src/wasm/wasm-s-parser.cpp
@@ -714,6 +714,7 @@ void SExpressionWasmBuilder::parseFunction(Element& s, bool preParseImport) {
Name name, exportName;
size_t i = parseFunctionNames(s, name, exportName);
+ bool hasExplicitName = name.is();
if (!preParseImport) {
if (!name.is()) {
// unnamed, use an index
@@ -762,7 +763,7 @@ void SExpressionWasmBuilder::parseFunction(Element& s, bool preParseImport) {
throw ParseException("!preParseImport in func", s.line, s.col);
}
auto im = make_unique<Function>();
- im->name = name;
+ im->setName(name, hasExplicitName);
im->module = importModule;
im->base = importBase;
im->sig = sig;
@@ -2185,7 +2186,7 @@ void SExpressionWasmBuilder::parseMemory(Element& s, bool preParseImport) {
wasm.memory.shared = false;
Index i = 1;
if (s[i]->dollared()) {
- wasm.memory.name = s[i++]->str();
+ wasm.memory.setExplicitName(s[i++]->str());
}
i = parseMemoryIndex(s, i);
Name importModule, importBase;
@@ -2358,7 +2359,8 @@ void SExpressionWasmBuilder::parseImport(Element& s) {
(*s[3])[newStyleInner]->dollared()) {
name = (*s[3])[newStyleInner++]->str();
}
- if (!name.is()) {
+ bool hasExplicitName = name.is();
+ if (!hasExplicitName) {
if (kind == ExternalKind::Function) {
name = Name("fimport$" + std::to_string(functionCounter++));
functionNames.push_back(name);
@@ -2396,7 +2398,7 @@ void SExpressionWasmBuilder::parseImport(Element& s) {
auto func = make_unique<Function>();
j = parseTypeUse(inner, j, func->sig);
- func->name = name;
+ func->setName(name, hasExplicitName);
func->module = module;
func->base = base;
functionTypes[name] = func->sig.results;
@@ -2415,14 +2417,14 @@ void SExpressionWasmBuilder::parseImport(Element& s) {
mutable_ = true;
}
auto global = make_unique<Global>();
- global->name = name;
+ global->setName(name, hasExplicitName);
global->module = module;
global->base = base;
global->type = type;
global->mutable_ = mutable_;
wasm.addGlobal(global.release());
} else if (kind == ExternalKind::Table) {
- wasm.table.name = name;
+ wasm.table.setName(name, hasExplicitName);
wasm.table.module = module;
wasm.table.base = base;
if (j < inner.size() - 1) {
@@ -2440,7 +2442,7 @@ void SExpressionWasmBuilder::parseImport(Element& s) {
j++; // funcref
// ends with the table element type
} else if (kind == ExternalKind::Memory) {
- wasm.memory.name = name;
+ wasm.memory.setName(name, hasExplicitName);
wasm.memory.module = module;
wasm.memory.base = base;
if (inner[j]->isList()) {
@@ -2465,7 +2467,7 @@ void SExpressionWasmBuilder::parseImport(Element& s) {
}
event->attribute = atoi(attrElem[1]->c_str());
j = parseTypeUse(inner, j, event->sig);
- event->name = name;
+ event->setName(name, hasExplicitName);
event->module = module;
event->base = base;
wasm.addEvent(event.release());
@@ -2480,7 +2482,7 @@ void SExpressionWasmBuilder::parseGlobal(Element& s, bool preParseImport) {
std::unique_ptr<Global> global = make_unique<Global>();
size_t i = 1;
if (s[i]->dollared() && !(s[i]->isStr() && isType(s[i]->str()))) {
- global->name = s[i++]->str();
+ global->setExplicitName(s[i++]->str());
} else {
global->name = Name::fromInt(globalCounter);
}
@@ -2569,7 +2571,7 @@ void SExpressionWasmBuilder::parseTable(Element& s, bool preParseImport) {
return; // empty table in old notation
}
if (s[i]->dollared()) {
- wasm.table.name = s[i++]->str();
+ wasm.table.setExplicitName(s[i++]->str());
}
if (i == s.size()) {
return;
@@ -2695,7 +2697,7 @@ void SExpressionWasmBuilder::parseEvent(Element& s, bool preParseImport) {
// Parse name
if (s[i]->isStr() && s[i]->dollared()) {
auto& inner = *s[i++];
- event->name = inner.str();
+ event->setExplicitName(inner.str());
if (wasm.getEventOrNull(event->name)) {
throw ParseException("duplicate event", inner.line, inner.col);
}