Permalink
Browse files

support function table initial and max sizes, and new printing format

  • Loading branch information...
kripken committed Aug 12, 2016
1 parent 7e39178 commit 35bd9109ab07aa21f9e55214a61390fa61c93638
Showing with 114 additions and 37 deletions.
  1. +1 −0 src/asm2wasm.h
  2. +1 −0 src/binaryen-c.cpp
  3. +5 −1 src/passes/Print.cpp
  4. +2 −0 src/shared-constants.h
  5. +1 −0 src/wasm-binary.h
  6. +3 −0 src/wasm-linker.cpp
  7. +24 −1 src/wasm-s-parser.h
  8. +2 −0 src/wasm.cpp
  9. +5 −0 src/wasm.h
  10. +2 −1 test/dot_s/alias.wast
  11. +2 −1 test/dot_s/basics.wast
  12. +2 −1 test/dot_s/bcp-1.wast
  13. +2 −1 test/dot_s/dyncall.wast
  14. +2 −1 test/dot_s/indidx.wast
  15. +2 −1 test/dot_s/indirect-import.wast
  16. +2 −1 test/dot_s/invoke_wrapper.wast
  17. +2 −1 test/emcc_O2_hello_world.fromasm
  18. +2 −1 test/emcc_O2_hello_world.fromasm.imprecise
  19. +2 −1 test/emcc_O2_hello_world.fromasm.imprecise.no-opts
  20. +2 −1 test/emcc_O2_hello_world.fromasm.no-opts
  21. +2 −1 test/emcc_hello_world.fromasm
  22. +2 −1 test/emcc_hello_world.fromasm.imprecise
  23. +2 −1 test/emcc_hello_world.fromasm.imprecise.no-opts
  24. +2 −1 test/emcc_hello_world.fromasm.no-opts
  25. +4 −2 test/example/c-api-kitchen-sink.txt
  26. +2 −1 test/example/c-api-kitchen-sink.txt.txt
  27. +2 −1 test/memorygrowth.fromasm
  28. +2 −1 test/memorygrowth.fromasm.imprecise
  29. +2 −1 test/memorygrowth.fromasm.imprecise.no-opts
  30. +2 −1 test/memorygrowth.fromasm.no-opts
  31. +2 −1 test/passes/dce.txt
  32. +8 −4 test/passes/duplicate-function-elimination.txt
  33. +2 −1 test/passes/remove-unused-functions.txt
  34. +2 −1 test/passes/remove-unused-names_merge-blocks.txt
  35. +2 −1 test/unit.fromasm
  36. +2 −1 test/unit.fromasm.imprecise
  37. +2 −1 test/unit.fromasm.imprecise.no-opts
  38. +2 −1 test/unit.fromasm.no-opts
  39. +2 −1 test/unit.wast
  40. +2 −1 test/unit.wast.fromBinary
View
@@ -666,6 +666,7 @@ void Asm2WasmBuilder::processAsm(Ref ast) {
IString curr = contents[k][1]->getIString();
wasm.table.names.push_back(curr);
}
wasm.table.initial = wasm.table.max = wasm.table.names.size();
} else {
abort_on("invalid var element", pair);
}
View
@@ -732,6 +732,7 @@ void BinaryenSetFunctionTable(BinaryenModuleRef module, BinaryenFunctionRef* fun
for (BinaryenIndex i = 0; i < numFuncs; i++) {
wasm->table.names.push_back(((Function*)funcs[i])->name);
}
wasm->table.initial = wasm->table.max = wasm->table.names.size();
}
// Memory. One per module
View
@@ -575,7 +575,11 @@ struct PrintSExpression : public Visitor<PrintSExpression> {
decIndent();
}
void visitTable(Table *curr) {
printOpening(o, "table");
printOpening(o, "table") << ' ' << curr->initial;
if (curr->max && curr->max != Table::kMaxSize) o << ' ' << curr->max;
o << " anyfunc)\n";
doIndent(o, indent);
printOpening(o, "elem", true);
for (auto name : curr->names) {
o << ' ';
printName(name);
View
@@ -29,6 +29,7 @@ extern Name GROW_WASM_MEMORY,
EXPORT,
IMPORT,
TABLE,
ELEM,
LOCAL,
TYPE,
CALL,
@@ -44,6 +45,7 @@ extern Name GROW_WASM_MEMORY,
NEG_NAN,
CASE,
BR,
ANYFUNC,
FAKE_RETURN,
ASSERT_RETURN,
ASSERT_TRAP,
View
@@ -1648,6 +1648,7 @@ class WasmBinaryBuilder {
assert(index < wasm.functions.size());
wasm.table.names.push_back(wasm.functions[index]->name);
}
wasm.table.initial = wasm.table.max = wasm.table.names.size();
}
void readDataSegments() {
View
@@ -223,6 +223,9 @@ void Linker::layout() {
if (out.symbolInfo.implementedFunctions.count("free")) {
exportFunction("free", true);
}
// finalize function table
out.wasm.table.initial = out.wasm.table.max = out.wasm.table.names.size();
}
bool Linker::linkObject(S2WasmBuilder& builder) {
View
@@ -372,6 +372,7 @@ class SExpressionWasmBuilder {
if (id == IMPORT) return; // already done
if (id == GLOBAL) return parseGlobal(curr);
if (id == TABLE) return parseTable(curr);
if (id == ELEM) return parseElem(curr);
if (id == TYPE) return; // already done
std::cerr << "bad module element " << id.str << '\n';
throw ParseException("unknown module element", curr.line, curr.col);
@@ -1424,7 +1425,29 @@ class SExpressionWasmBuilder {
}
void parseTable(Element& s) {
for (size_t i = 1; i < s.size(); i++) {
if (s.size() == 1) return; // empty table in old notation
if (!s[1]->dollared()) {
if (s[1]->str() == ANYFUNC) {
// (table type (elem ..))
parseElem(*s[2]);
wasm.table.initial = wasm.table.max = wasm.table.names.size();
return;
}
// first element isn't dollared, and isn't anyfunc. this could be old syntax for (table 0 1) which means function 0 and 1, or it could be (table initial max? type), look for type
if (s[s.size() - 1]->str() == ANYFUNC) {
// (table initial max? type)
wasm.table.initial = atoi(s[1]->c_str());
wasm.table.max = atoi(s[2]->c_str());
return;
}
}
// old notation (table func1 func2 ..)
parseElem(s);
wasm.table.initial = wasm.table.max = wasm.table.names.size();
}
void parseElem(Element& s) {
for (Index i = 1; i < s.size(); i++) {
wasm.table.names.push_back(getFunctionName(*s[i]));
}
}
View
@@ -54,6 +54,7 @@ Name GROW_WASM_MEMORY("__growWasmMemory"),
EXPORT("export"),
IMPORT("import"),
TABLE("table"),
ELEM("elem"),
LOCAL("local"),
TYPE("type"),
CALL("call"),
@@ -69,6 +70,7 @@ Name GROW_WASM_MEMORY("__growWasmMemory"),
NEG_NAN("-nan"),
CASE("case"),
BR("br"),
ANYFUNC("anyfunc"),
FAKE_RETURN("fake_return_waka123"),
ASSERT_RETURN("assert_return"),
ASSERT_TRAP("assert_trap"),
View
@@ -1432,7 +1432,12 @@ class Export {
class Table {
public:
static const Index kMaxSize = Index(-1);
Address initial, max;
std::vector<Name> names;
Table() : initial(0), max(kMaxSize) {}
};
class Memory {
View
@@ -6,7 +6,8 @@
(export "__exit" $__exit)
(export "__needs_exit" $__needs_exit)
(export "dynCall_v" $dynCall_v)
(table $__wasm_nullptr $__exit)
(table 2 2 anyfunc)
(elem $__wasm_nullptr $__exit)
(func $__exit (type $FUNCSIG$v)
(return
(i32.add
View
@@ -10,7 +10,8 @@
(import $puts "env" "puts" (param i32))
(export "main" $main)
(export "dynCall_iii" $dynCall_iii)
(table $__wasm_nullptr $main)
(table 2 2 anyfunc)
(elem $__wasm_nullptr $main)
(func $main (type $FUNCSIG$iii) (param $0 i32) (param $1 i32) (result i32)
(call_import $puts
(i32.const 16)
View
@@ -34,7 +34,8 @@
(export "main" $main)
(export "dynCall_i" $dynCall_i)
(export "dynCall_ii" $dynCall_ii)
(table $__wasm_nullptr $bad0 $bad1 $bad5 $bad7 $bad8 $bad10 $bad2 $bad3 $bad6 $bad4 $bad9 $good0 $good1 $good2 $opt0 $opt1 $opt2)
(table 18 18 anyfunc)
(elem $__wasm_nullptr $bad0 $bad1 $bad5 $bad7 $bad8 $bad10 $bad2 $bad3 $bad6 $bad4 $bad9 $good0 $good1 $good2 $opt0 $opt1 $opt2)
(func $bad0 (type $FUNCSIG$i) (result i32)
(return
(i32.const 0)
View
@@ -15,7 +15,8 @@
(export "dynCall_i" $dynCall_i)
(export "dynCall_if" $dynCall_if)
(export "dynCall_vd" $dynCall_vd)
(table $__wasm_nullptr $i $i_f $vd $ffjjdi $vd2)
(table 6 6 anyfunc)
(elem $__wasm_nullptr $i $i_f $vd $ffjjdi $vd2)
(func $i (type $FUNCSIG$i) (result i32)
(i32.const 0)
)
View
@@ -7,7 +7,8 @@
(import $getchar "env" "getchar" (result i32))
(export "main" $main)
(export "dynCall_i" $dynCall_i)
(table $__wasm_nullptr $c $b $d $a)
(table 5 5 anyfunc)
(elem $__wasm_nullptr $c $b $d $a)
(func $a (type $FUNCSIG$i) (result i32)
(i32.const 0)
)
@@ -17,7 +17,8 @@
(export "dynCall_fd" $dynCall_fd)
(export "dynCall_v" $dynCall_v)
(export "dynCall_vi" $dynCall_vi)
(table $__wasm_nullptr $__importThunk_extern_fd $__importThunk_extern_vj $__importThunk_extern_v $__importThunk_extern_ijidf $__importThunk_extern_struct $__importThunk_extern_sret)
(table 7 7 anyfunc)
(elem $__wasm_nullptr $__importThunk_extern_fd $__importThunk_extern_vj $__importThunk_extern_v $__importThunk_extern_ijidf $__importThunk_extern_struct $__importThunk_extern_sret)
(func $bar (result i32)
(local $0 i32)
(local $1 i32)
@@ -17,7 +17,8 @@
(export "dynCall_iiii" $dynCall_iiii)
(export "dynCall_ffd" $dynCall_ffd)
(export "dynCall_iii" $dynCall_iii)
(table $__wasm_nullptr $_Z5func1v $_Z5func2iii $_Z5func3fd $_Z5func4P8mystructS_)
(table 5 5 anyfunc)
(elem $__wasm_nullptr $_Z5func1v $_Z5func2iii $_Z5func3fd $_Z5func4P8mystructS_)
(func $_Z5func1v (type $FUNCSIG$v)
)
(func $_Z5func2iii (type $FUNCSIG$iiii) (param $0 i32) (param $1 i32) (param $2 i32) (result i32)
@@ -42,7 +42,8 @@
(export "dynCall_ii" $dynCall_ii)
(export "dynCall_iiii" $dynCall_iiii)
(export "dynCall_vi" $dynCall_vi)
(table $b0 $___stdio_close $b1 $b1 $___stdout_write $___stdio_seek $b1 $___stdio_write $b1 $b1 $b2 $b2 $b2 $b2 $_cleanup_418 $b2 $b2 $b2)
(table 18 18 anyfunc)
(elem $b0 $___stdio_close $b1 $b1 $___stdout_write $___stdio_seek $b1 $___stdio_write $b1 $b1 $b2 $b2 $b2 $b2 $_cleanup_418 $b2 $b2 $b2)
(func $_malloc (param $0 i32) (result i32)
(local $1 i32)
(local $2 i32)
@@ -41,7 +41,8 @@
(export "dynCall_ii" $dynCall_ii)
(export "dynCall_iiii" $dynCall_iiii)
(export "dynCall_vi" $dynCall_vi)
(table $b0 $___stdio_close $b1 $b1 $___stdout_write $___stdio_seek $b1 $___stdio_write $b1 $b1 $b2 $b2 $b2 $b2 $_cleanup_418 $b2 $b2 $b2)
(table 18 18 anyfunc)
(elem $b0 $___stdio_close $b1 $b1 $___stdout_write $___stdio_seek $b1 $___stdio_write $b1 $b1 $b2 $b2 $b2 $b2 $_cleanup_418 $b2 $b2 $b2)
(func $_malloc (param $0 i32) (result i32)
(local $1 i32)
(local $2 i32)
@@ -41,7 +41,8 @@
(export "dynCall_ii" $dynCall_ii)
(export "dynCall_iiii" $dynCall_iiii)
(export "dynCall_vi" $dynCall_vi)
(table $b0 $___stdio_close $b1 $b1 $___stdout_write $___stdio_seek $b1 $___stdio_write $b1 $b1 $b2 $b2 $b2 $b2 $_cleanup_418 $b2 $b2 $b2)
(table 18 18 anyfunc)
(elem $b0 $___stdio_close $b1 $b1 $___stdout_write $___stdio_seek $b1 $___stdio_write $b1 $b1 $b2 $b2 $b2 $b2 $_cleanup_418 $b2 $b2 $b2)
(func $_malloc (param $i1 i32) (result i32)
(local $i2 i32)
(local $i3 i32)
@@ -42,7 +42,8 @@
(export "dynCall_ii" $dynCall_ii)
(export "dynCall_iiii" $dynCall_iiii)
(export "dynCall_vi" $dynCall_vi)
(table $b0 $___stdio_close $b1 $b1 $___stdout_write $___stdio_seek $b1 $___stdio_write $b1 $b1 $b2 $b2 $b2 $b2 $_cleanup_418 $b2 $b2 $b2)
(table 18 18 anyfunc)
(elem $b0 $___stdio_close $b1 $b1 $___stdout_write $___stdio_seek $b1 $___stdio_write $b1 $b1 $b2 $b2 $b2 $b2 $_cleanup_418 $b2 $b2 $b2)
(func $_malloc (param $i1 i32) (result i32)
(local $i2 i32)
(local $i3 i32)
@@ -55,7 +55,8 @@
(export "dynCall_iiii" $dynCall_iiii)
(export "dynCall_vi" $dynCall_vi)
(export "___udivmoddi4" $___udivmoddi4)
(table $b0 $___stdio_close $b1 $b1 $___stdout_write $___stdio_seek $___stdio_write $b1 $b1 $b1 $b2 $b2 $b2 $b2 $b2 $_cleanup $b2 $b2)
(table 18 18 anyfunc)
(elem $b0 $___stdio_close $b1 $b1 $___stdout_write $___stdio_seek $___stdio_write $b1 $b1 $b1 $b2 $b2 $b2 $b2 $b2 $_cleanup $b2 $b2)
(func $stackAlloc (param $0 i32) (result i32)
(local $1 i32)
(set_local $1
@@ -49,7 +49,8 @@
(export "dynCall_iiii" $dynCall_iiii)
(export "dynCall_vi" $dynCall_vi)
(export "___udivmoddi4" $___udivmoddi4)
(table $b0 $___stdio_close $b1 $b1 $___stdout_write $___stdio_seek $___stdio_write $b1 $b1 $b1 $b2 $b2 $b2 $b2 $b2 $_cleanup $b2 $b2)
(table 18 18 anyfunc)
(elem $b0 $___stdio_close $b1 $b1 $___stdout_write $___stdio_seek $___stdio_write $b1 $b1 $b1 $b2 $b2 $b2 $b2 $b2 $_cleanup $b2 $b2)
(func $stackAlloc (param $0 i32) (result i32)
(local $1 i32)
(set_local $1
@@ -49,7 +49,8 @@
(export "dynCall_iiii" $dynCall_iiii)
(export "dynCall_vi" $dynCall_vi)
(export "___udivmoddi4" $___udivmoddi4)
(table $b0 $___stdio_close $b1 $b1 $___stdout_write $___stdio_seek $___stdio_write $b1 $b1 $b1 $b2 $b2 $b2 $b2 $b2 $_cleanup $b2 $b2)
(table 18 18 anyfunc)
(elem $b0 $___stdio_close $b1 $b1 $___stdout_write $___stdio_seek $___stdio_write $b1 $b1 $b1 $b2 $b2 $b2 $b2 $b2 $_cleanup $b2 $b2)
(func $stackAlloc (param $size i32) (result i32)
(local $ret i32)
(set_local $ret
@@ -55,7 +55,8 @@
(export "dynCall_iiii" $dynCall_iiii)
(export "dynCall_vi" $dynCall_vi)
(export "___udivmoddi4" $___udivmoddi4)
(table $b0 $___stdio_close $b1 $b1 $___stdout_write $___stdio_seek $___stdio_write $b1 $b1 $b1 $b2 $b2 $b2 $b2 $b2 $_cleanup $b2 $b2)
(table 18 18 anyfunc)
(elem $b0 $___stdio_close $b1 $b1 $___stdout_write $___stdio_seek $___stdio_write $b1 $b1 $b1 $b2 $b2 $b2 $b2 $b2 $_cleanup $b2 $b2)
(func $stackAlloc (param $size i32) (result i32)
(local $ret i32)
(set_local $ret
@@ -17,7 +17,8 @@ BinaryenFloat64: 4
(type $3 (func))
(import $an-imported "module" "base" (param i32 f64) (result f32))
(export "kitchen_sinker" "$kitchen()sinker")
(table "$kitchen()sinker")
(table 1 1 anyfunc)
(elem "$kitchen()sinker")
(func "$kitchen()sinker" (type $iiIfF) (param $0 i32) (param $1 i64) (param $2 f32) (param $3 f64) (result i32)
(local $4 i32)
(block $the-body
@@ -1409,7 +1410,8 @@ int main() {
(type $3 (func))
(import $an-imported "module" "base" (param i32 f64) (result f32))
(export "kitchen_sinker" "$kitchen()sinker")
(table "$kitchen()sinker")
(table 1 1 anyfunc)
(elem "$kitchen()sinker")
(func "$kitchen()sinker" (type $iiIfF) (param $0 i32) (param $1 i64) (param $2 f32) (param $3 f64) (result i32)
(local $4 i32)
(block $the-body
@@ -12,7 +12,8 @@
(type $3 (func))
(import $an-imported "module" "base" (param i32 f64) (result f32))
(export "kitchen_sinker" "$kitchen()sinker")
(table "$kitchen()sinker")
(table 1 1 anyfunc)
(elem "$kitchen()sinker")
(func "$kitchen()sinker" (type $iiIfF) (param $0 i32) (param $1 i64) (param $2 f32) (param $3 f64) (result i32)
(local $4 i32)
(block $the-body
@@ -40,7 +40,8 @@
(export "dynCall_iiii" $lb)
(export "dynCall_vi" $mb)
(export "__growWasmMemory" $__growWasmMemory)
(table $nb $Oa $ob $Va $Ua $Ra $pb $Sa)
(table 8 8 anyfunc)
(elem $nb $Oa $ob $Va $Ua $Ra $pb $Sa)
(func $eb (param $0 i32) (result i32)
(local $1 i32)
(local $2 i32)
@@ -39,7 +39,8 @@
(export "dynCall_iiii" $lb)
(export "dynCall_vi" $mb)
(export "__growWasmMemory" $__growWasmMemory)
(table $nb $Oa $ob $Va $Ua $Ra $pb $Sa)
(table 8 8 anyfunc)
(elem $nb $Oa $ob $Va $Ua $Ra $pb $Sa)
(func $eb (param $0 i32) (result i32)
(local $1 i32)
(local $2 i32)
@@ -39,7 +39,8 @@
(export "dynCall_iiii" $lb)
(export "dynCall_vi" $mb)
(export "__growWasmMemory" $__growWasmMemory)
(table $nb $Oa $ob $Va $Ua $Ra $pb $Sa)
(table 8 8 anyfunc)
(elem $nb $Oa $ob $Va $Ua $Ra $pb $Sa)
(func $eb (param $a i32) (result i32)
(local $b i32)
(local $c i32)
@@ -40,7 +40,8 @@
(export "dynCall_iiii" $lb)
(export "dynCall_vi" $mb)
(export "__growWasmMemory" $__growWasmMemory)
(table $nb $Oa $ob $Va $Ua $Ra $pb $Sa)
(table 8 8 anyfunc)
(elem $nb $Oa $ob $Va $Ua $Ra $pb $Sa)
(func $eb (param $a i32) (result i32)
(local $b i32)
(local $c i32)
View
@@ -2,7 +2,8 @@
(memory 10)
(type $ii (func (param i32 i32)))
(type $1 (func))
(table $call-me)
(table 1 1 anyfunc)
(elem $call-me)
(func $call-me (type $ii) (param $0 i32) (param $1 i32)
(nop)
)
Oops, something went wrong.

0 comments on commit 35bd910

Please sign in to comment.