Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions auto_update_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@
actual += run_command(cmd)
with open(os.path.join('test', 'passes', passname + '.txt'), 'w') as o: o.write(actual)

print '\n[ checking wasm-opt -o notation... ]\n'

wast = os.path.join('test', 'hello_world.wast')
cmd = [os.path.join('bin', 'wasm-opt'), wast, '-o', 'a.wast']
run_command(cmd)
open(wast, 'w').write(open('a.wast').read())

print '\n[ checking binary format testcases... ]\n'

for wast in sorted(os.listdir('test')):
Expand Down Expand Up @@ -145,4 +152,15 @@
# Also removes debug directory produced on Mac OS
shutil.rmtree(output_file + '.dSYM')

print '\n[ checking wasm-opt testcases... ]\n'

for t in os.listdir('test'):
if t.endswith('.wast') and not t.startswith('spec'):
print '..', t
t = os.path.join('test', t)
cmd = [os.path.join('bin', 'wasm-opt'), t, '--print']
actual = run_command(cmd)
actual = actual.replace('printing before:\n', '')
open(t, 'w').write(actual)

print '\n[ success! ]'
53 changes: 43 additions & 10 deletions src/wasm-s-parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -312,21 +312,49 @@ class SExpressionWasmBuilder {
}
functionNames.push_back(name);
functionCounter++;
FunctionType* type = nullptr;
functionTypes[name] = none;
std::vector<WasmType> params;
for (;i < s.size(); i++) {
Element& curr = *s[i];
IString id = curr[0]->str();
if (id == RESULT) {
functionTypes[name] = stringToWasmType(curr[1]->str());
return;
} else if (id == TYPE) {
Name typeName = curr[1]->str();
if (!wasm.checkFunctionType(typeName)) throw ParseException("unknown function");
FunctionType* type = wasm.getFunctionType(typeName);
type = wasm.getFunctionType(typeName);
functionTypes[name] = type->result;
return;
} else if (id == PARAM && curr.size() > 1) {
Index j = 1;
if (curr[j]->dollared()) {
// dollared input symbols cannot be types
params.push_back(stringToWasmType(curr[j + 1]->str(), true));
} else {
while (j < curr.size()) {
params.push_back(stringToWasmType(curr[j++]->str(), true));
}
}
}
}
if (!type) {
// if no function type provided, generate one, but reuse a previous one with the
// right structure if there is one.
// see https://github.com/WebAssembly/spec/pull/301
bool need = true;
std::unique_ptr<FunctionType> functionType = make_unique<FunctionType>();
functionType->result = functionTypes[name];
functionType->params = std::move(params);
for (auto& existing : wasm.functionTypes) {
if (existing->structuralComparison(*functionType)) {
need = false;
break;
}
}
if (need) {
wasm.addFunctionType(functionType.release());
}
}
functionTypes[name] = none;
}

void preParseImports(Element& curr) {
Expand Down Expand Up @@ -500,16 +528,21 @@ class SExpressionWasmBuilder {
body = allocator.alloc<Nop>();
}
if (currFunction->result != result) throw ParseException("bad func declaration", s.line, s.col);
/* TODO: spec in flux, https://github.com/WebAssembly/spec/pull/301
// see https://github.com/WebAssembly/spec/pull/301
if (type.isNull()) {
// if no function type provided, generate a private one for this function
auto* functionType = sigToFunctionType(getSig(currFunction.get()));
wasm.addFunctionType(functionType);
type = functionType->name;
// if no function type name provided, then we generated one
std::unique_ptr<FunctionType> functionType = std::unique_ptr<FunctionType>(sigToFunctionType(getSig(currFunction.get())));
for (auto& existing : wasm.functionTypes) {
if (existing->structuralComparison(*functionType)) {
type = existing->name;
break;
}
}
if (!type.is()) throw ParseException("no function type [internal error?]", s.line, s.col);
}
*/
currFunction->body = body;
currFunction->type = type;

wasm.addFunction(currFunction.release());
currLocalTypes.clear();
labelStack.clear();
Expand Down
8 changes: 6 additions & 2 deletions src/wasm.h
Original file line number Diff line number Diff line change
Expand Up @@ -1065,15 +1065,19 @@ class FunctionType {

FunctionType() : result(none) {}

bool operator==(FunctionType& b) {
if (name != b.name) return false; // XXX
bool structuralComparison(FunctionType& b) {
if (result != b.result) return false;
if (params.size() != b.params.size()) return false;
for (size_t i = 0; i < params.size(); i++) {
if (params[i] != b.params[i]) return false;
}
return true;
}

bool operator==(FunctionType& b) {
if (name != b.name) return false;
return structuralComparison(b);
}
bool operator!=(FunctionType& b) {
return !(*this == b);
}
Expand Down
3 changes: 2 additions & 1 deletion test/hello_world.wast
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
(module
(memory 256 256)
(type $0 (func (param i32 i32) (result i32)))
(export "add" $add)
(func $add (param $x i32) (param $y i32) (result i32)
(func $add (type $0) (param $x i32) (param $y i32) (result i32)
(i32.add
(get_local $x)
(get_local $y)
Expand Down
3 changes: 2 additions & 1 deletion test/kitchen_sink.wast
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
(memory 4096 4096
(segment 1026 "\14\00")
)
(func $kitchensink (result i32)
(type $0 (func (result i32)))
(func $kitchensink (type $0) (result i32)
(block $block0
(i32.add
(i32.const 10)
Expand Down
12 changes: 8 additions & 4 deletions test/min.wast
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
(module
(memory 256 256)
(type $0 (func (param f32) (result f32)))
(type $1 (func (param i32 i32) (result f32)))
(type $2 (func (param i32) (result i32)))
(type $3 (func (param i32 i32 i32) (result i32)))
(export "floats" $floats)
(func $floats (param $f f32) (result f32)
(func $floats (type $0) (param $f f32) (result f32)
(local $t f32)
(f32.add
(get_local $t)
(get_local $f)
)
)
(func $neg (param $k i32) (param $p i32) (result f32)
(func $neg (type $1) (param $k i32) (param $p i32) (result f32)
(local $n f32)
(set_local $n
(f32.neg
Expand All @@ -24,7 +28,7 @@
)
)
)
(func $littleswitch (param $x i32) (result i32)
(func $littleswitch (type $2) (param $x i32) (result i32)
(block $topmost
(block $switch-case$2
(block $switch-case$1
Expand All @@ -45,7 +49,7 @@
(i32.const 0)
)
)
(func $f1 (param $i1 i32) (param $i2 i32) (param $i3 i32) (result i32)
(func $f1 (type $3) (param $i1 i32) (param $i2 i32) (param $i3 i32) (result i32)
(block $topmost
(get_local $i3)
)
Expand Down
Loading