Skip to content

Commit eabf9ed

Browse files
authored
Factor out elementStartsWith (NFC) (#2137)
Checking if a first string matches a certain string within a list element appears many times within the parser, so extracted it as a helper function.
1 parent b1ecf05 commit eabf9ed

3 files changed

Lines changed: 38 additions & 32 deletions

File tree

src/shared-constants.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ extern Name MUT;
6161
extern Name SPECTEST;
6262
extern Name PRINT;
6363
extern Name EXIT;
64+
extern Name SHARED;
6465

6566
} // namespace wasm
6667

src/wasm/wasm-s-parser.cpp

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ static Address getCheckedAddress(const Element* s, const char* errorText) {
5959
return num;
6060
}
6161

62+
static bool elementStartsWith(Element& s, IString str) {
63+
return s.isList() && s.size() > 0 && s[0]->isStr() && s[0]->str() == str;
64+
}
65+
6266
Element::List& Element::list() {
6367
if (!isList()) {
6468
throw ParseException("expected list", line, col);
@@ -348,7 +352,7 @@ SExpressionWasmBuilder::SExpressionWasmBuilder(Module& wasm,
348352
auto& s = *module[j];
349353
preParseFunctionType(s);
350354
preParseImports(s);
351-
if (s[0]->str() == FUNC && !isImport(s)) {
355+
if (elementStartsWith(s, FUNC) && !isImport(s)) {
352356
implementedFunctions++;
353357
}
354358
}
@@ -363,7 +367,7 @@ SExpressionWasmBuilder::SExpressionWasmBuilder(Module& wasm,
363367
bool SExpressionWasmBuilder::isImport(Element& curr) {
364368
for (Index i = 0; i < curr.size(); i++) {
365369
auto& x = *curr[i];
366-
if (x.isList() && x.size() > 0 && x[0]->isStr() && x[0]->str() == IMPORT) {
370+
if (elementStartsWith(x, IMPORT)) {
367371
return true;
368372
}
369373
}
@@ -489,7 +493,7 @@ std::vector<Type> SExpressionWasmBuilder::parseParamOrLocal(Element& s) {
489493
// If the name is unspecified, it will create one using localIndex.
490494
std::vector<NameType>
491495
SExpressionWasmBuilder::parseNamedParamOrLocal(Element& s, size_t& localIndex) {
492-
assert(s.isList() && (s[0]->str() == PARAM || s[0]->str() == LOCAL));
496+
assert(elementStartsWith(s, PARAM) || elementStartsWith(s, LOCAL));
493497
std::vector<NameType> namedParams;
494498
if (s.size() == 1) { // (param) or (local)
495499
return namedParams;
@@ -518,7 +522,7 @@ SExpressionWasmBuilder::parseNamedParamOrLocal(Element& s, size_t& localIndex) {
518522

519523
// Parses (result type) element. (e.g. (result i32))
520524
Type SExpressionWasmBuilder::parseResult(Element& s) {
521-
assert(s.isList() && s[0]->str() == RESULT);
525+
assert(elementStartsWith(s, RESULT));
522526
if (s.size() != 2) {
523527
throw ParseException("invalid result arity", s.line, s.col);
524528
}
@@ -529,7 +533,7 @@ Type SExpressionWasmBuilder::parseResult(Element& s) {
529533
// should be in the form of (type name) or (type index).
530534
// (e.g. (type $a), (type 0))
531535
FunctionType* SExpressionWasmBuilder::parseTypeRef(Element& s) {
532-
assert(s.isList() && s[0]->str() == TYPE);
536+
assert(elementStartsWith(s, TYPE));
533537
if (s.size() != 2) {
534538
throw ParseException("invalid type reference", s.line, s.col);
535539
}
@@ -616,7 +620,7 @@ size_t SExpressionWasmBuilder::parseFunctionNames(Element& s,
616620
}
617621
if (i < s.size() && s[i]->isList()) {
618622
auto& inner = *s[i];
619-
if (inner.size() > 0 && inner[0]->str() == EXPORT) {
623+
if (elementStartsWith(inner, EXPORT)) {
620624
exportName = inner[1]->str();
621625
i++;
622626
}
@@ -1029,7 +1033,7 @@ Expression* SExpressionWasmBuilder::makeBlock(Element& s) {
10291033
break; // empty block
10301034
}
10311035
auto& first = *s[i];
1032-
if (first[0]->str() == BLOCK) {
1036+
if (elementStartsWith(first, BLOCK)) {
10331037
// recurse
10341038
curr = allocator.alloc<Block>();
10351039
if (first.startLoc) {
@@ -1050,7 +1054,7 @@ Expression* SExpressionWasmBuilder::makeBlock(Element& s) {
10501054
while (i < s.size() && s[i]->isStr()) {
10511055
i++;
10521056
}
1053-
if (i < s.size() && (*s[i])[0]->str() == RESULT) {
1057+
if (i < s.size() && elementStartsWith(*s[i], RESULT)) {
10541058
i++;
10551059
}
10561060
if (t < int(stack.size()) - 1) {
@@ -1548,7 +1552,7 @@ Expression* SExpressionWasmBuilder::makeCallIndirect(Element& s) {
15481552
auto ret = allocator.alloc<CallIndirect>();
15491553
Index i = 1;
15501554
Element& typeElement = *s[i];
1551-
if (typeElement[0]->str() == "type") {
1555+
if (elementStartsWith(typeElement, TYPE)) {
15521556
// type name given
15531557
IString type = typeElement[1]->str();
15541558
auto* fullType = wasm.getFunctionTypeOrNull(type);
@@ -1562,11 +1566,11 @@ Expression* SExpressionWasmBuilder::makeCallIndirect(Element& s) {
15621566
FunctionType type;
15631567
while (1) {
15641568
Element& curr = *s[i];
1565-
if (curr[0]->str() == PARAM) {
1569+
if (elementStartsWith(curr, PARAM)) {
15661570
auto newParams = parseParamOrLocal(curr);
15671571
type.params.insert(
15681572
type.params.end(), newParams.begin(), newParams.end());
1569-
} else if (curr[0]->str() == RESULT) {
1573+
} else if (elementStartsWith(curr, RESULT)) {
15701574
type.result = parseResult(curr);
15711575
} else {
15721576
break;
@@ -1616,7 +1620,7 @@ Expression* SExpressionWasmBuilder::makeBreak(Element& s) {
16161620
if (i == s.size()) {
16171621
return ret;
16181622
}
1619-
if (s[0]->str() == BR_IF) {
1623+
if (elementStartsWith(s, BR_IF)) {
16201624
if (i + 1 < s.size()) {
16211625
ret->value = parseExpression(s[i]);
16221626
i++;
@@ -1732,7 +1736,7 @@ void SExpressionWasmBuilder::parseMemory(Element& s, bool preParseImport) {
17321736
Name importModule, importBase;
17331737
if (s[i]->isList()) {
17341738
auto& inner = *s[i];
1735-
if (inner[0]->str() == EXPORT) {
1739+
if (elementStartsWith(inner, EXPORT)) {
17361740
auto ex = make_unique<Export>();
17371741
ex->name = inner[1]->str();
17381742
ex->value = wasm.memory.name;
@@ -1742,11 +1746,11 @@ void SExpressionWasmBuilder::parseMemory(Element& s, bool preParseImport) {
17421746
}
17431747
wasm.addExport(ex.release());
17441748
i++;
1745-
} else if (inner[0]->str() == IMPORT) {
1749+
} else if (elementStartsWith(inner, IMPORT)) {
17461750
wasm.memory.module = inner[1]->str();
17471751
wasm.memory.base = inner[2]->str();
17481752
i++;
1749-
} else if (inner[0]->str() == "shared") {
1753+
} else if (elementStartsWith(inner, SHARED)) {
17501754
wasm.memory.shared = true;
17511755
parseMemoryLimits(inner, 1);
17521756
i++;
@@ -1770,7 +1774,7 @@ void SExpressionWasmBuilder::parseMemory(Element& s, bool preParseImport) {
17701774
Element& curr = *s[i];
17711775
size_t j = 1;
17721776
Address offsetValue;
1773-
if (curr[0]->str() == DATA) {
1777+
if (elementStartsWith(curr, DATA)) {
17741778
offsetValue = 0;
17751779
} else {
17761780
offsetValue = getCheckedAddress(curr[j++], "excessive memory offset");
@@ -1834,13 +1838,13 @@ void SExpressionWasmBuilder::parseExport(Element& s) {
18341838
if (s[2]->isList()) {
18351839
auto& inner = *s[2];
18361840
ex->value = inner[1]->str();
1837-
if (inner[0]->str() == FUNC) {
1841+
if (elementStartsWith(inner, FUNC)) {
18381842
ex->kind = ExternalKind::Function;
1839-
} else if (inner[0]->str() == MEMORY) {
1843+
} else if (elementStartsWith(inner, MEMORY)) {
18401844
ex->kind = ExternalKind::Memory;
1841-
} else if (inner[0]->str() == TABLE) {
1845+
} else if (elementStartsWith(inner, TABLE)) {
18421846
ex->kind = ExternalKind::Table;
1843-
} else if (inner[0]->str() == GLOBAL) {
1847+
} else if (elementStartsWith(inner, GLOBAL)) {
18441848
ex->kind = ExternalKind::Global;
18451849
} else {
18461850
throw ParseException("invalid export");
@@ -1862,21 +1866,21 @@ void SExpressionWasmBuilder::parseImport(Element& s) {
18621866
bool newStyle = s.size() == 4 && s[3]->isList();
18631867
auto kind = ExternalKind::Invalid;
18641868
if (newStyle) {
1865-
if ((*s[3])[0]->str() == FUNC) {
1869+
if (elementStartsWith(*s[3], FUNC)) {
18661870
kind = ExternalKind::Function;
1867-
} else if ((*s[3])[0]->str() == MEMORY) {
1871+
} else if (elementStartsWith(*s[3], MEMORY)) {
18681872
kind = ExternalKind::Memory;
18691873
if (wasm.memory.exists) {
18701874
throw ParseException("more than one memory");
18711875
}
18721876
wasm.memory.exists = true;
1873-
} else if ((*s[3])[0]->str() == TABLE) {
1877+
} else if (elementStartsWith(*s[3], TABLE)) {
18741878
kind = ExternalKind::Table;
18751879
if (wasm.table.exists) {
18761880
throw ParseException("more than one table");
18771881
}
18781882
wasm.table.exists = true;
1879-
} else if ((*s[3])[0]->str() == GLOBAL) {
1883+
} else if (elementStartsWith(*s[3], GLOBAL)) {
18801884
kind = ExternalKind::Global;
18811885
} else {
18821886
newStyle = false; // either (param..) or (result..)
@@ -1991,7 +1995,7 @@ void SExpressionWasmBuilder::parseImport(Element& s) {
19911995
wasm.memory.base = base;
19921996
if (inner[j]->isList()) {
19931997
auto& limits = *inner[j];
1994-
if (!(limits[0]->isStr() && limits[0]->str() == "shared")) {
1998+
if (!elementStartsWith(limits, SHARED)) {
19951999
throw ParseException("bad memory limit declaration");
19962000
}
19972001
wasm.memory.shared = true;
@@ -2018,7 +2022,7 @@ void SExpressionWasmBuilder::parseGlobal(Element& s, bool preParseImport) {
20182022
Name importModule, importBase;
20192023
while (i < s.size() && s[i]->isList()) {
20202024
auto& inner = *s[i];
2021-
if (inner[0]->str() == EXPORT) {
2025+
if (elementStartsWith(inner, EXPORT)) {
20222026
auto ex = make_unique<Export>();
20232027
ex->name = inner[1]->str();
20242028
ex->value = global->name;
@@ -2029,11 +2033,11 @@ void SExpressionWasmBuilder::parseGlobal(Element& s, bool preParseImport) {
20292033
wasm.addExport(ex.release());
20302034
exported = true;
20312035
i++;
2032-
} else if (inner[0]->str() == IMPORT) {
2036+
} else if (elementStartsWith(inner, IMPORT)) {
20332037
importModule = inner[1]->str();
20342038
importBase = inner[2]->str();
20352039
i++;
2036-
} else if (inner[0]->str() == MUT) {
2040+
} else if (elementStartsWith(inner, MUT)) {
20372041
mutable_ = true;
20382042
type = stringToType(inner[1]->str());
20392043
i++;
@@ -2104,7 +2108,7 @@ void SExpressionWasmBuilder::parseTable(Element& s, bool preParseImport) {
21042108
Name importModule, importBase;
21052109
if (s[i]->isList()) {
21062110
auto& inner = *s[i];
2107-
if (inner[0]->str() == EXPORT) {
2111+
if (elementStartsWith(inner, EXPORT)) {
21082112
auto ex = make_unique<Export>();
21092113
ex->name = inner[1]->str();
21102114
ex->value = wasm.table.name;
@@ -2114,7 +2118,7 @@ void SExpressionWasmBuilder::parseTable(Element& s, bool preParseImport) {
21142118
}
21152119
wasm.addExport(ex.release());
21162120
i++;
2117-
} else if (inner[0]->str() == IMPORT) {
2121+
} else if (elementStartsWith(inner, IMPORT)) {
21182122
if (!preParseImport) {
21192123
throw ParseException("!preParseImport in table");
21202124
}
@@ -2199,11 +2203,11 @@ void SExpressionWasmBuilder::parseType(Element& s) {
21992203
Element& func = *s[i];
22002204
for (size_t k = 1; k < func.size(); k++) {
22012205
Element& curr = *func[k];
2202-
if (curr[0]->str() == PARAM) {
2206+
if (elementStartsWith(curr, PARAM)) {
22032207
auto newParams = parseParamOrLocal(curr);
22042208
type->params.insert(
22052209
type->params.end(), newParams.begin(), newParams.end());
2206-
} else if (curr[0]->str() == RESULT) {
2210+
} else if (elementStartsWith(curr, RESULT)) {
22072211
type->result = parseResult(curr);
22082212
}
22092213
}

src/wasm/wasm.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ Name MUT("mut");
8181
Name SPECTEST("spectest");
8282
Name PRINT("print");
8383
Name EXIT("exit");
84+
Name SHARED("shared");
8485

8586
// Expressions
8687

0 commit comments

Comments
 (0)