Skip to content

Commit

Permalink
Only export functions with default visibility
Browse files Browse the repository at this point in the history
As discussed in #585, this change makes s2wasm more aware of the
symbol visibility output from LLVM, and only exports global functions
with default visibility (instead of all globals as before).
  • Loading branch information
dschuff committed Jun 14, 2016
1 parent 6fdd9dd commit 1ee1d55
Show file tree
Hide file tree
Showing 11 changed files with 24 additions and 35 deletions.
9 changes: 8 additions & 1 deletion src/s2wasm.h
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,10 @@ class S2WasmBuilder {
s++;
if (match("text")) parseText();
else if (match("type")) parseType();
else if (match("weak") || match("hidden") || match("protected") || match("internal")) getStr(); // contents are in the content that follows
else if (match("weak")) getStr(); // contents are in the content that follows
else if (match("hidden")) parseVisibility(LinkerObject::Visibility::kHidden);
else if (match("internal")) parseVisibility(LinkerObject::Visibility::kInternal);
else if (match("protected")) parseVisibility(LinkerObject::Visibility::kProtected);
else if (match("imports")) skipImports();
else if (match("data")) {}
else if (match("ident")) skipToEOL();
Expand Down Expand Up @@ -481,6 +484,10 @@ class S2WasmBuilder {
WASM_UNUSED(filename); // TODO: use the filename
}

void parseVisibility(LinkerObject::Visibility vis) {
linkerObj->setVisibility(getStr(), vis);
skipWhitespace();
}
void parseGlobl() {
linkerObj->addGlobal(getStr());
skipWhitespace();
Expand Down
13 changes: 11 additions & 2 deletions src/wasm-linker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,17 @@ void Linker::layout() {
if (userMaxMemory) out.wasm.memory.max = userMaxMemory / Memory::kPageSize;
out.wasm.memory.exportName = MEMORY;

// XXX For now, export all functions marked .globl.
for (Name name : out.globls) exportFunction(name, false);
// Export all globl functions with default visibility. Since .s files usually
// only output visibility information if it is not default, symbols are
// considered to have default visibility if they are not in the visibility table.
// Also export main for now because emscripten and the binaryen-shell tests use it.
for (Name name : out.globls) {
if (out.visibilities.count(name) == 0 ||
out.visibilities.at(name) == LinkerObject::Visibility::kDefault ||
name == "main") {
exportFunction(name, false);
}
}
for (Name name : out.initializerFunctions) exportFunction(name, true);

auto ensureFunctionIndex = [this](Name name) {
Expand Down
5 changes: 5 additions & 0 deletions src/wasm-linker.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ class LinkerObject {
other.aliasedFunctions.end());
}
};
enum class Visibility { kDefault, kInternal, kHidden, kProtected };

LinkerObject() {}

Expand All @@ -75,6 +76,9 @@ class LinkerObject {
staticObjects.emplace_back(allocSize, alignment, name);
}

void setVisibility(Name name, Visibility vis) {
visibilities[name] = vis;
}
void addGlobal(Name name) {
globls.push_back(name);
}
Expand Down Expand Up @@ -145,6 +149,7 @@ class LinkerObject {
allocSize(allocSize), alignment(alignment), name(name) {}
};

std::unordered_map<cashew::IString, LinkerObject::Visibility> visibilities;
std::vector<Name> globls;

std::vector<StaticObject> staticObjects;
Expand Down
2 changes: 0 additions & 2 deletions test/dot_s/alias.wast
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
(memory 1)
(export "memory" memory)
(type $FUNCSIG$v (func))
(export "__exit" $__exit)
(export "__needs_exit" $__needs_exit)
(export "dynCall_v" $dynCall_v)
(table $__exit)
(func $__exit (type $FUNCSIG$v)
Expand Down
17 changes: 0 additions & 17 deletions test/dot_s/bcp-1.wast
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,6 @@
(type $FUNCSIG$vi (func (param i32)))
(import $abort "env" "abort")
(import $exit "env" "exit" (param i32))
(export "bad0" $bad0)
(export "bad1" $bad1)
(export "bad2" $bad2)
(export "bad3" $bad3)
(export "bad4" $bad4)
(export "bad5" $bad5)
(export "bad6" $bad6)
(export "bad7" $bad7)
(export "bad8" $bad8)
(export "bad9" $bad9)
(export "bad10" $bad10)
(export "good0" $good0)
(export "good1" $good1)
(export "good2" $good2)
(export "opt0" $opt0)
(export "opt1" $opt1)
(export "opt2" $opt2)
(export "main" $main)
(export "dynCall_i" $dynCall_i)
(export "dynCall_ii" $dynCall_ii)
Expand Down
1 change: 0 additions & 1 deletion test/dot_s/debug.wast
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
(module
(memory 1)
(export "memory" memory)
(export "fib" $fib)
(func $fib (param $0 i32) (result i32)
(local $1 i32)
(local $2 i32)
Expand Down
5 changes: 0 additions & 5 deletions test/dot_s/dyncall.wast
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@
(type $FUNCSIG$if (func (param f32) (result i32)))
(type $FUNCSIG$vd (func (param f64)))
(type $FUNCSIG$ffjjdi (func (param f32 i64 i64 f64 i32) (result f32)))
(export "i" $i)
(export "i_f" $i_f)
(export "vd" $vd)
(export "ffjjdi" $ffjjdi)
(export "vd2" $vd2)
(export "main" $main)
(export "dynCall_i" $dynCall_i)
(export "dynCall_if" $dynCall_if)
Expand Down
2 changes: 0 additions & 2 deletions test/dot_s/indirect-import.wast
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
(import $extern_fd "env" "extern_fd" (param f64) (result f32))
(import $extern_struct "env" "extern_struct" (param i32))
(import $extern_sret "env" "extern_sret" (param i32))
(export "bar" $bar)
(export "baz" $baz)
(export "dynCall_fd" $dynCall_fd)
(export "dynCall_v" $dynCall_v)
(export "dynCall_vi" $dynCall_vi)
Expand Down
3 changes: 0 additions & 3 deletions test/dot_s/visibilities.wast
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
(module
(memory 1)
(export "memory" memory)
(export "foo" $foo)
(export "bar" $bar)
(export "qux" $qux)
(func $foo
(return)
)
Expand Down
1 change: 0 additions & 1 deletion test/llvm_autogenerated/reg-stackify.wast
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
(export "no_sink_readonly_call" $no_sink_readonly_call)
(export "stack_uses" $stack_uses)
(export "multiple_uses" $multiple_uses)
(export "stackify_store_across_side_effects" $stackify_store_across_side_effects)
(export "div_tree" $div_tree)
(export "simple_multiple_use" $simple_multiple_use)
(export "multiple_uses_in_same_insn" $multiple_uses_in_same_insn)
Expand Down
1 change: 0 additions & 1 deletion test/llvm_autogenerated/store-results.wast
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
(export "single_block" $single_block)
(export "foo" $foo)
(export "bar" $bar)
(export "fi_ret" $fi_ret)
(func $single_block (param $0 i32) (result i32)
(return
(i32.store
Expand Down

0 comments on commit 1ee1d55

Please sign in to comment.