Skip to content

Tables and memories #668

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 16, 2016
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
10,955 changes: 5,843 additions & 5,112 deletions bin/wasm.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion check.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ def minify_check(wast, verify_final_result=True):
print '\n[ checking wasm-shell spec testcases... ]\n'

if len(requested) == 0:
BLACKLIST = []
BLACKLIST = ['memory.wast'] # FIXME we support old and new memory formats, for now, until 0xc, and so can't pass this old-style test
spec_tests = [os.path.join('spec', t) for t in sorted(os.listdir(os.path.join('test', 'spec'))) if t not in BLACKLIST]
else:
spec_tests = requested[:]
Expand Down
9 changes: 7 additions & 2 deletions src/asm2wasm.h
Original file line number Diff line number Diff line change
Expand Up @@ -660,12 +660,17 @@ void Asm2WasmBuilder::processAsm(Ref ast) {
// TODO: when not using aliasing function pointers, we could merge them by noticing that
// index 0 in each table is the null func, and each other index should only have one
// non-null func. However, that breaks down when function pointer casts are emulated.
functionTableStarts[name] = wasm.table.names.size(); // this table starts here
if (wasm.table.segments.size() == 0) {
wasm.table.segments.emplace_back(wasm.allocator.alloc<Const>()->set(Literal(uint32_t(0))));
}
auto& segment = wasm.table.segments[0];
functionTableStarts[name] = segment.data.size(); // this table starts here
Ref contents = value[1];
for (unsigned k = 0; k < contents->size(); k++) {
IString curr = contents[k][1]->getIString();
wasm.table.names.push_back(curr);
segment.data.push_back(curr);
}
wasm.table.initial = wasm.table.max = segment.data.size();
} else {
abort_on("invalid var element", pair);
}
Expand Down
13 changes: 8 additions & 5 deletions src/binaryen-c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -729,14 +729,17 @@ void BinaryenSetFunctionTable(BinaryenModuleRef module, BinaryenFunctionRef* fun
}

auto* wasm = (Module*)module;
Table::Segment segment(wasm->allocator.alloc<Const>()->set(Literal(int32_t(0))));
for (BinaryenIndex i = 0; i < numFuncs; i++) {
wasm->table.names.push_back(((Function*)funcs[i])->name);
segment.data.push_back(((Function*)funcs[i])->name);
}
wasm->table.segments.push_back(segment);
wasm->table.initial = wasm->table.max = numFuncs;
}

// Memory. One per module

void BinaryenSetMemory(BinaryenModuleRef module, BinaryenIndex initial, BinaryenIndex maximum, const char* exportName, const char **segments, BinaryenIndex* segmentOffsets, BinaryenIndex* segmentSizes, BinaryenIndex numSegments) {
void BinaryenSetMemory(BinaryenModuleRef module, BinaryenIndex initial, BinaryenIndex maximum, const char* exportName, const char **segments, BinaryenExpressionRef* segmentOffsets, BinaryenIndex* segmentSizes, BinaryenIndex numSegments) {
if (tracing) {
std::cout << " {\n";
for (BinaryenIndex i = 0; i < numSegments; i++) {
Expand All @@ -754,10 +757,10 @@ void BinaryenSetMemory(BinaryenModuleRef module, BinaryenIndex initial, Binaryen
}
if (numSegments == 0) std::cout << "0"; // ensure the array is not empty, otherwise a compiler error on VS
std::cout << " };\n";
std::cout << " BinaryenIndex segmentOffsets[] = { ";
std::cout << " BinaryenExpressionRef segmentOffsets[] = { ";
for (BinaryenIndex i = 0; i < numSegments; i++) {
if (i > 0) std::cout << ", ";
std::cout << segmentOffsets[i];
std::cout << "expressions[" << expressions[segmentOffsets[i]] << "]";
}
if (numSegments == 0) std::cout << "0"; // ensure the array is not empty, otherwise a compiler error on VS
std::cout << " };\n";
Expand All @@ -779,7 +782,7 @@ void BinaryenSetMemory(BinaryenModuleRef module, BinaryenIndex initial, Binaryen
wasm->memory.max = maximum;
if (exportName) wasm->memory.exportName = exportName;
for (BinaryenIndex i = 0; i < numSegments; i++) {
wasm->memory.segments.emplace_back(segmentOffsets[i], segments[i], segmentSizes[i]);
wasm->memory.segments.emplace_back((Expression*)segmentOffsets[i], segments[i], segmentSizes[i]);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/binaryen-c.h
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ void BinaryenSetFunctionTable(BinaryenModuleRef module, BinaryenFunctionRef* fun

// Each segment has data in segments, a start offset in segmentOffsets, and a size in segmentSizes.
// exportName can be NULL
void BinaryenSetMemory(BinaryenModuleRef module, BinaryenIndex initial, BinaryenIndex maximum, const char* exportName, const char **segments, BinaryenIndex* segmentOffsets, BinaryenIndex* segmentSizes, BinaryenIndex numSegments);
void BinaryenSetMemory(BinaryenModuleRef module, BinaryenIndex initial, BinaryenIndex maximum, const char* exportName, const char **segments, BinaryenExpressionRef* segmentOffsets, BinaryenIndex* segmentSizes, BinaryenIndex numSegments);

// Start function. One per module

Expand Down
10 changes: 6 additions & 4 deletions src/passes/DuplicateFunctionElimination.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,12 @@ struct DuplicateFunctionElimination : public Pass {
replacerRunner.add<FunctionReplacer>(&replacements);
replacerRunner.run();
// replace in table
for (auto& name : module->table.names) {
auto iter = replacements.find(name);
if (iter != replacements.end()) {
name = iter->second;
for (auto& segment : module->table.segments) {
for (auto& name : segment.data) {
auto iter = replacements.find(name);
if (iter != replacements.end()) {
name = iter->second;
}
}
}
// replace in start
Expand Down
30 changes: 19 additions & 11 deletions src/passes/Print.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -575,12 +575,19 @@ struct PrintSExpression : public Visitor<PrintSExpression> {
decIndent();
}
void visitTable(Table *curr) {
printOpening(o, "table");
for (auto name : curr->names) {
o << ' ';
printName(name);
printOpening(o, "table") << ' ' << curr->initial;
if (curr->max && curr->max != Table::kMaxSize) o << ' ' << curr->max;
o << " anyfunc)\n";
doIndent(o, indent);
for (auto& segment : curr->segments) {
printOpening(o, "elem ", true);
visit(segment.offset);
for (auto name : segment.data) {
o << ' ';
printName(name);
}
o << ')';
}
o << ')';
}
void visitModule(Module *curr) {
currModule = curr;
Expand All @@ -589,9 +596,12 @@ struct PrintSExpression : public Visitor<PrintSExpression> {
doIndent(o, indent);
printOpening(o, "memory") << ' ' << curr->memory.initial;
if (curr->memory.max && curr->memory.max != Memory::kMaxSize) o << ' ' << curr->memory.max;
o << ")\n";
for (auto segment : curr->memory.segments) {
o << maybeNewLine;
o << (minify ? "" : " ") << "(segment " << segment.offset << " \"";
doIndent(o, indent);
printOpening(o, "data ", true);
visit(segment.offset);
o << " \"";
for (size_t i = 0; i < segment.data.size(); i++) {
unsigned char c = segment.data[i];
switch (c) {
Expand All @@ -612,10 +622,8 @@ struct PrintSExpression : public Visitor<PrintSExpression> {
}
}
}
o << "\")";
o << "\")\n";
}
o << ((curr->memory.segments.size() > 0 && !minify) ? "\n " : "") << ')';
o << maybeNewLine;
if (curr->memory.exportName.is()) {
doIndent(o, indent);
printOpening(o, "export ");
Expand Down Expand Up @@ -647,7 +655,7 @@ struct PrintSExpression : public Visitor<PrintSExpression> {
visitGlobal(child.get());
o << maybeNewLine;
}
if (curr->table.names.size() > 0) {
if (curr->table.segments.size() > 0 || curr->table.initial > 0 || curr->table.max != Table::kMaxSize) {
doIndent(o, indent);
visitTable(&curr->table);
o << maybeNewLine;
Expand Down
6 changes: 4 additions & 2 deletions src/passes/RemoveUnusedFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@ struct RemoveUnusedFunctions : public Pass {
root.push_back(module->getFunction(curr->value));
}
// For now, all functions that can be called indirectly are marked as roots.
for (auto& curr : module->table.names) {
root.push_back(module->getFunction(curr));
for (auto& segment : module->table.segments) {
for (auto& curr : segment.data) {
root.push_back(module->getFunction(curr));
}
}
// Compute function reachability starting from the root set.
DirectCallGraphAnalyzer analyzer(module, root);
Expand Down
6 changes: 4 additions & 2 deletions src/passes/ReorderFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@ struct ReorderFunctions : public WalkerPass<PostWalker<ReorderFunctions, Visitor
for (auto& curr : module->exports) {
counts[curr->value]++;
}
for (auto& curr : module->table.names) {
counts[curr]++;
for (auto& segment : module->table.segments) {
for (auto& curr : segment.data) {
counts[curr]++;
}
}
std::sort(module->functions.begin(), module->functions.end(), [this](
const std::unique_ptr<Function>& a,
Expand Down
3 changes: 3 additions & 0 deletions src/shared-constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ extern Name GROW_WASM_MEMORY,
PARAM,
RESULT,
MEMORY,
DATA,
SEGMENT,
EXPORT,
IMPORT,
TABLE,
ELEM,
LOCAL,
TYPE,
CALL,
Expand All @@ -43,6 +45,7 @@ extern Name GROW_WASM_MEMORY,
NEG_NAN,
CASE,
BR,
ANYFUNC,
FAKE_RETURN,
ASSERT_RETURN,
ASSERT_TRAP,
Expand Down
29 changes: 27 additions & 2 deletions src/shell-interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,27 @@ struct ShellExternalInterface : ModuleInstance::ExternalInterface {
}
} memory;

std::vector<Name> table;

ShellExternalInterface() : memory() {}

void init(Module& wasm) override {
memory.resize(wasm.memory.initial * wasm::Memory::kPageSize);
// apply memory segments
for (auto& segment : wasm.memory.segments) {
assert(segment.offset + segment.data.size() <= wasm.memory.initial * wasm::Memory::kPageSize);
Address offset = ConstantExpressionRunner().visit(segment.offset).value.geti32();
assert(offset + segment.data.size() <= wasm.memory.initial * wasm::Memory::kPageSize);
for (size_t i = 0; i != segment.data.size(); ++i) {
memory.set(offset + i, segment.data[i]);
}
}

table.resize(wasm.table.initial);
for (auto& segment : wasm.table.segments) {
Address offset = ConstantExpressionRunner().visit(segment.offset).value.geti32();
assert(offset + segment.data.size() <= wasm.table.initial);
for (size_t i = 0; i != segment.data.size(); ++i) {
memory.set(segment.offset + i, segment.data[i]);
table[offset + i] = segment.data[i];
}
}
}
Expand All @@ -114,6 +126,19 @@ struct ShellExternalInterface : ModuleInstance::ExternalInterface {
abort();
}

Literal callTable(Index index, Name type, LiteralList& arguments, ModuleInstance& instance) override {
if (index >= table.size()) trap("callTable overflow");
auto* func = instance.wasm.getFunction(table[index]);
if (func->type.is() && func->type != type) trap("callIndirect: bad type");
if (func->params.size() != arguments.size()) trap("callIndirect: bad # of arguments");
for (size_t i = 0; i < func->params.size(); i++) {
if (func->params[i] != arguments[i].type) {
trap("callIndirect: bad argument type");
}
}
return instance.callFunctionInternal(func->name, arguments);
}

Literal load(Load* load, Address addr) override {
switch (load->type) {
case i32: {
Expand Down
56 changes: 39 additions & 17 deletions src/wasm-binary.h
Original file line number Diff line number Diff line change
Expand Up @@ -706,7 +706,8 @@ class WasmBinaryWriter : public Visitor<WasmBinaryWriter, void> {
o << U32LEB(num);
for (auto& segment : wasm->memory.segments) {
if (segment.data.size() == 0) continue;
o << U32LEB(segment.offset);
writeExpression(segment.offset);
o << int8_t(BinaryConsts::End);
writeInlineBuffer(&segment.data[0], segment.data.size());
}
finishSection(start);
Expand Down Expand Up @@ -739,12 +740,19 @@ class WasmBinaryWriter : public Visitor<WasmBinaryWriter, void> {
}

void writeFunctionTable() {
if (wasm->table.names.size() == 0) return;
if (wasm->table.segments.size() == 0) return;
if (debug) std::cerr << "== writeFunctionTable" << std::endl;
auto start = startSection(BinaryConsts::Section::FunctionTable);
o << U32LEB(wasm->table.names.size());
for (auto name : wasm->table.names) {
o << U32LEB(getFunctionIndex(name));
o << U32LEB(wasm->table.initial);
o << U32LEB(wasm->table.max);
o << U32LEB(wasm->table.segments.size());
for (auto& segment : wasm->table.segments) {
writeExpression(segment.offset);
o << int8_t(BinaryConsts::End);
o << U32LEB(segment.data.size());
for (auto name : segment.data) {
o << U32LEB(getFunctionIndex(name));
}
}
finishSection(start);
}
Expand Down Expand Up @@ -1572,6 +1580,15 @@ class WasmBinaryBuilder {
}
}

Expression* readExpression() {
assert(depth == 0);
processExpressions();
assert(expressionStack.size() == 1);
auto* ret = popExpression();
assert(depth == 0);
return ret;
}

void readGlobals() {
if (debug) std::cerr << "== readGlobals" << std::endl;
size_t num = getU32LEB();
Expand All @@ -1580,11 +1597,7 @@ class WasmBinaryBuilder {
if (debug) std::cerr << "read one" << std::endl;
auto curr = new Global;
curr->type = getWasmType();
assert(depth == 0);
processExpressions();
assert(expressionStack.size() == 1);
curr->init = popExpression();
assert(depth == 0);
curr->init = readExpression();
wasm.addGlobal(curr);
}
}
Expand Down Expand Up @@ -1638,9 +1651,12 @@ class WasmBinaryBuilder {
}
}

for (size_t index : functionTable) {
assert(index < wasm.functions.size());
wasm.table.names.push_back(wasm.functions[index]->name);
for (auto& pair : functionTable) {
auto i = pair.first;
auto& indexes = pair.second;
for (auto j : indexes) {
wasm.table.segments[i].data.push_back(wasm.functions[j]->name);
}
}
}

Expand All @@ -1649,7 +1665,7 @@ class WasmBinaryBuilder {
auto num = getU32LEB();
for (size_t i = 0; i < num; i++) {
Memory::Segment curr;
auto offset = getU32LEB();
auto offset = readExpression();
auto size = getU32LEB();
std::vector<char> buffer;
buffer.resize(size);
Expand All @@ -1660,14 +1676,20 @@ class WasmBinaryBuilder {
}
}

std::vector<size_t> functionTable;
std::map<Index, std::vector<Index>> functionTable;

void readFunctionTable() {
if (debug) std::cerr << "== readFunctionTable" << std::endl;
wasm.table.initial = getU32LEB();
wasm.table.max = getU32LEB();
auto num = getU32LEB();
for (size_t i = 0; i < num; i++) {
auto index = getU32LEB();
functionTable.push_back(index);
wasm.table.segments.emplace_back(readExpression());
auto& temporary = functionTable[i];
auto size = getU32LEB();
for (Index j = 0; j < size; j++) {
temporary.push_back(getU32LEB());
}
}
}

Expand Down
Loading