Skip to content

Commit

Permalink
support function table initial and max sizes, and new printing format
Browse files Browse the repository at this point in the history
  • Loading branch information
kripken committed Aug 13, 2016
1 parent 7e39178 commit 35bd910
Show file tree
Hide file tree
Showing 40 changed files with 114 additions and 37 deletions.
1 change: 1 addition & 0 deletions src/asm2wasm.h
Expand Up @@ -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);
}
Expand Down
1 change: 1 addition & 0 deletions src/binaryen-c.cpp
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion src/passes/Print.cpp
Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions src/shared-constants.h
Expand Up @@ -29,6 +29,7 @@ extern Name GROW_WASM_MEMORY,
EXPORT,
IMPORT,
TABLE,
ELEM,
LOCAL,
TYPE,
CALL,
Expand All @@ -44,6 +45,7 @@ extern Name GROW_WASM_MEMORY,
NEG_NAN,
CASE,
BR,
ANYFUNC,
FAKE_RETURN,
ASSERT_RETURN,
ASSERT_TRAP,
Expand Down
1 change: 1 addition & 0 deletions src/wasm-binary.h
Expand Up @@ -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() {
Expand Down
3 changes: 3 additions & 0 deletions src/wasm-linker.cpp
Expand Up @@ -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) {
Expand Down
25 changes: 24 additions & 1 deletion src/wasm-s-parser.h
Expand Up @@ -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);
Expand Down Expand Up @@ -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]));
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/wasm.cpp
Expand Up @@ -54,6 +54,7 @@ Name GROW_WASM_MEMORY("__growWasmMemory"),
EXPORT("export"),
IMPORT("import"),
TABLE("table"),
ELEM("elem"),
LOCAL("local"),
TYPE("type"),
CALL("call"),
Expand All @@ -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"),
Expand Down
5 changes: 5 additions & 0 deletions src/wasm.h
Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion test/dot_s/alias.wast
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion test/dot_s/basics.wast
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion test/dot_s/bcp-1.wast
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion test/dot_s/dyncall.wast
Expand Up @@ -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)
)
Expand Down
3 changes: 2 additions & 1 deletion test/dot_s/indidx.wast
Expand Up @@ -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)
)
Expand Down
3 changes: 2 additions & 1 deletion test/dot_s/indirect-import.wast
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion test/dot_s/invoke_wrapper.wast
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion test/emcc_O2_hello_world.fromasm
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion test/emcc_O2_hello_world.fromasm.imprecise
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion test/emcc_O2_hello_world.fromasm.imprecise.no-opts
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion test/emcc_O2_hello_world.fromasm.no-opts
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion test/emcc_hello_world.fromasm
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion test/emcc_hello_world.fromasm.imprecise
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion test/emcc_hello_world.fromasm.imprecise.no-opts
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion test/emcc_hello_world.fromasm.no-opts
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions test/example/c-api-kitchen-sink.txt
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion test/example/c-api-kitchen-sink.txt.txt
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion test/memorygrowth.fromasm
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion test/memorygrowth.fromasm.imprecise
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion test/memorygrowth.fromasm.imprecise.no-opts
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion test/memorygrowth.fromasm.no-opts
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion test/passes/dce.txt
Expand Up @@ -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)
)
Expand Down

0 comments on commit 35bd910

Please sign in to comment.