summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2016-08-31 16:53:12 -0700
committerAlon Zakai <alonzakai@gmail.com>2016-09-07 09:55:58 -0700
commite62f54d9d38e8f6b999d5f18f052424b7d603b6b (patch)
tree6e9af7b90a6563cfe15db6ab6325e8eb345a9e4c /src
parentf1c74afc64d9f195729e1ad7e203f27566248e26 (diff)
downloadbinaryen-e62f54d9d38e8f6b999d5f18f052424b7d603b6b.tar.gz
binaryen-e62f54d9d38e8f6b999d5f18f052424b7d603b6b.tar.bz2
binaryen-e62f54d9d38e8f6b999d5f18f052424b7d603b6b.zip
new import syntax in spec repo
Diffstat (limited to 'src')
-rw-r--r--src/passes/Print.cpp36
-rw-r--r--src/wasm-s-parser.h35
2 files changed, 40 insertions, 31 deletions
diff --git a/src/passes/Print.cpp b/src/passes/Print.cpp
index 0071ede38..d25b64886 100644
--- a/src/passes/Print.cpp
+++ b/src/passes/Print.cpp
@@ -514,11 +514,9 @@ struct PrintSExpression : public Visitor<PrintSExpression> {
printMinorOpening(o, "unreachable") << ')';
}
// Module-level visitors
- void visitFunctionType(FunctionType *curr, bool full=false) {
- if (full) {
- printOpening(o, "type") << ' ';
- printName(curr->name) << " (func";
- }
+ void visitFunctionType(FunctionType *curr, Name* internalName = nullptr) {
+ o << "(func";
+ if (internalName) o << ' ' << *internalName;
if (curr->params.size() > 0) {
o << maybeSpace;
printMinorOpening(o, "param");
@@ -531,27 +529,17 @@ struct PrintSExpression : public Visitor<PrintSExpression> {
o << maybeSpace;
printMinorOpening(o, "result ") << printWasmType(curr->result) << ')';
}
- if (full) {
- o << "))";
- }
+ o << ")";
}
void visitImport(Import *curr) {
printOpening(o, "import ");
- printName(curr->name) << ' ';
- switch (curr->kind) {
- case Export::Function: break;
- case Export::Table: o << "table "; break;
- case Export::Memory: o << "memory "; break;
- case Export::Global: o << "global "; break;
- default: WASM_UNREACHABLE();
- }
printText(o, curr->module.str) << ' ';
- printText(o, curr->base.str);
+ printText(o, curr->base.str) << ' ';
switch (curr->kind) {
- case Export::Function: if (curr->functionType) visitFunctionType(curr->functionType); break;
- case Export::Table: break;
- case Export::Memory: break;
- case Export::Global: o << ' ' << printWasmType(curr->globalType); break;
+ case Export::Function: if (curr->functionType) visitFunctionType(curr->functionType, &curr->name); break;
+ case Export::Table: o << "(table " << curr->name << ")"; break;
+ case Export::Memory: o << "(memory " << curr->name << ")"; break;
+ case Export::Global: o << "(global " << curr->name << ' ' << printWasmType(curr->globalType) << ")"; break;
default: WASM_UNREACHABLE();
}
o << ')';
@@ -666,8 +654,10 @@ struct PrintSExpression : public Visitor<PrintSExpression> {
}
for (auto& child : curr->functionTypes) {
doIndent(o, indent);
- visitFunctionType(child.get(), true);
- o << maybeNewLine;
+ printOpening(o, "type") << ' ';
+ printName(child->name) << ' ';
+ visitFunctionType(child.get());
+ o << ")" << maybeNewLine;
}
for (auto& child : curr->imports) {
doIndent(o, indent);
diff --git a/src/wasm-s-parser.h b/src/wasm-s-parser.h
index 65335c1be..3ac900213 100644
--- a/src/wasm-s-parser.h
+++ b/src/wasm-s-parser.h
@@ -1429,8 +1429,24 @@ private:
void parseImport(Element& s) {
std::unique_ptr<Import> im = make_unique<Import>();
size_t i = 1;
+ bool newStyle = s.size() == 4 && s[3]->isList(); // (import "env" "STACKTOP" (global $stackTop i32))
+ if (newStyle) {
+ if ((*s[3])[0]->str() == FUNC) {
+ im->kind = Import::Function;
+ } else if ((*s[3])[0]->str() == MEMORY) {
+ im->kind = Import::Memory;
+ } else if ((*s[3])[0]->str() == TABLE) {
+ im->kind = Import::Table;
+ } else if ((*s[3])[0]->str() == GLOBAL) {
+ im->kind = Import::Global;
+ } else {
+ newStyle = false; // either (param..) or (result..)
+ }
+ }
if (s.size() > 3 && s[3]->isStr()) {
im->name = s[i++]->str();
+ } else if (newStyle) {
+ im->name = (*s[3])[1]->str();
} else {
im->name = Name::fromInt(importCounter);
}
@@ -1438,24 +1454,27 @@ private:
if (!s[i]->quoted()) {
if (s[i]->str() == MEMORY) {
im->kind = Import::Memory;
- } else if (s[2]->str() == TABLE) {
+ } else if (s[i]->str() == TABLE) {
im->kind = Import::Table;
- } else if (s[2]->str() == GLOBAL) {
+ } else if (s[i]->str() == GLOBAL) {
im->kind = Import::Global;
} else {
WASM_UNREACHABLE();
}
i++;
- } else {
+ } else if (!newStyle) {
im->kind = Import::Function;
}
im->module = s[i++]->str();
if (!s[i]->isStr()) throw ParseException("no name for import");
im->base = s[i++]->str();
+ // parse internals
+ Element& inner = newStyle ? *s[3] : s;
+ Index j = newStyle ? 2 : i;
if (im->kind == Import::Function) {
std::unique_ptr<FunctionType> type = make_unique<FunctionType>();
- if (s.size() > i) {
- Element& params = *s[i];
+ if (inner.size() > j) {
+ Element& params = *inner[j];
IString id = params[0]->str();
if (id == PARAM) {
for (size_t i = 1; i < params.size(); i++) {
@@ -1470,15 +1489,15 @@ private:
} else {
throw ParseException("bad import element");
}
- if (s.size() > i+1) {
- Element& result = *s[i+1];
+ if (inner.size() > j+1) {
+ Element& result = *inner[j+1];
assert(result[0]->str() == RESULT);
type->result = stringToWasmType(result[1]->str());
}
}
im->functionType = ensureFunctionType(getSig(type.get()), &wasm);
} else if (im->kind == Import::Global) {
- im->globalType = stringToWasmType(s[i]->str());
+ im->globalType = stringToWasmType(inner[j]->str());
}
wasm.addImport(im.release());
}