summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/passes/EmscriptenPIC.cpp85
-rw-r--r--test/lld/gdollar_mainmodule.wat20
-rw-r--r--test/lld/gdollar_mainmodule.wat.out73
-rw-r--r--test/lld/main_module.wat.out21
-rw-r--r--test/lld/main_module_table.wat.out24
-rw-r--r--test/lld/main_module_table_2.wat.out25
-rw-r--r--test/lld/main_module_table_3.wat.out25
-rw-r--r--test/lld/main_module_table_4.wat.out21
-rw-r--r--test/lld/main_module_table_5.wat.out14
9 files changed, 42 insertions, 266 deletions
diff --git a/src/passes/EmscriptenPIC.cpp b/src/passes/EmscriptenPIC.cpp
index 495d46358..51f7ead6e 100644
--- a/src/passes/EmscriptenPIC.cpp
+++ b/src/passes/EmscriptenPIC.cpp
@@ -43,23 +43,6 @@
namespace wasm {
-static Global* ensureGlobalImport(Module* module, Name name, Type type) {
- // See if its already imported.
- // FIXME: O(N)
- ImportInfo info(*module);
- if (auto* g = info.getImportedGlobal(ENV, name)) {
- return g;
- }
- // Failing that create a new import.
- auto import = new Global;
- import->name = name;
- import->module = ENV;
- import->base = name;
- import->type = type;
- module->addGlobal(import);
- return import;
-}
-
static Function*
ensureFunctionImport(Module* module, Name name, Signature sig) {
// See if its already imported.
@@ -110,34 +93,8 @@ struct EmscriptenPIC : public WalkerPass<PostWalker<EmscriptenPIC>> {
Block* block = builder.makeBlock();
assignFunc->body = block;
- bool hasSingleMemorySegment =
- module->memory.exists && module->memory.segments.size() == 1;
-
for (Global* g : gotMemEntries) {
- // If this global is defined in this module, we export its address
- // relative to the relocatable memory. If we are in a main module, we can
- // just use that location (since if other modules have this symbol too, we
- // will "win" as we are loaded first). Otherwise, import a g$ getter. Note
- // that this depends on memory having a single segment, so we know the
- // offset, and that the export is a global.
auto base = g->base;
- if (hasSingleMemorySegment && !sideModule) {
- if (auto* ex = module->getExportOrNull(base)) {
- if (ex->kind == ExternalKind::Global) {
- // The base relative to which we are computed is the offset of the
- // singleton segment.
- auto* relativeBase = ExpressionManipulator::copy(
- module->memory.segments[0].offset, *module);
-
- auto* offset = builder.makeGlobalGet(
- ex->value, module->getGlobal(ex->value)->type);
- auto* add = builder.makeBinary(AddInt32, relativeBase, offset);
- GlobalSet* globalSet = builder.makeGlobalSet(g->name, add);
- block->list.push_back(globalSet);
- continue;
- }
- }
- }
Name getter(std::string("g$") + base.c_str());
ensureFunctionImport(module, getter, Signature(Type::none, Type::i32));
Expression* call = builder.makeCall(getter, {}, Type::i32);
@@ -147,52 +104,16 @@ struct EmscriptenPIC : public WalkerPass<PostWalker<EmscriptenPIC>> {
ImportInfo importInfo(*module);
- // We may have to add things to the table.
- Global* tableBase = nullptr;
-
for (Global* g : gotFuncEntries) {
// The function has to exist either as export or an import.
// Note that we don't search for the function by name since its internal
// name may be different.
auto* ex = module->getExportOrNull(g->base);
- // If this is exported then it must be one of the functions implemented
- // here, and if this is a main module, then we can simply place the
- // function in the table: the loader will see it there and resolve all
- // other uses to this one.
- if (ex && !sideModule) {
- assert(ex->kind == ExternalKind::Function);
- auto* f = module->getFunction(ex->value);
- if (f->imported()) {
- Fatal() << "GOT.func entry is both imported and exported: "
- << g->base;
- }
- // The base relative to which we are computed is the offset of the
- // singleton segment, which we must ensure exists
- if (!tableBase) {
- tableBase = ensureGlobalImport(module, TABLE_BASE, Type::i32);
- }
- if (!module->table.exists) {
- module->table.exists = true;
- }
- if (module->table.segments.empty()) {
- module->table.segments.resize(1);
- module->table.segments[0].offset =
- builder.makeGlobalGet(tableBase->name, Type::i32);
- }
- auto tableIndex =
- TableUtils::getOrAppend(module->table, f->name, *module);
- auto* c = LiteralUtils::makeFromInt32(tableIndex, Type::i32, *module);
- auto* getBase = builder.makeGlobalGet(tableBase->name, Type::i32);
- auto* add = builder.makeBinary(AddInt32, getBase, c);
- auto* globalSet = builder.makeGlobalSet(g->name, add);
- block->list.push_back(globalSet);
- continue;
- }
- // This is imported or in a side module. Create an fp$ import to get the
- // function table index from the dynamic loader.
+ // This is imported create an fp$ import to get the function table index
+ // from the dynamic loader.
auto* f = importInfo.getImportedFunction(ENV, g->base);
if (!f) {
- if (!ex) {
+ if (!module->getExportOrNull(g->base)) {
Fatal() << "GOT.func entry with no import/export: " << g->base;
}
f = module->getFunction(ex->value);
diff --git a/test/lld/gdollar_mainmodule.wat b/test/lld/gdollar_mainmodule.wat
deleted file mode 100644
index 2b9a30439..000000000
--- a/test/lld/gdollar_mainmodule.wat
+++ /dev/null
@@ -1,20 +0,0 @@
-(module
- (import "env" "memory" (memory $0 0))
- (data (global.get $gimport$3) "")
- (import "env" "__indirect_function_table" (table $timport$1 0 funcref))
- (import "env" "__stack_pointer" (global $gimport$2 (mut i32)))
- (import "env" "__memory_base" (global $gimport$3 i32))
- (import "env" "__table_base" (global $gimport$4 i32))
- (import "GOT.mem" "someglobal1" (global $gimport$6 (mut i32)))
- (import "GOT.mem" "someglobal2" (global $gimport$7 (mut i32)))
- (import "GOT.mem" "someglobal3" (global $gimport$8 (mut i32)))
- (global $global$1 i32 (i32.const 54))
- (global $global$2 i32 (i32.const 0))
- (export "someglobal2" (global $global$1))
- (export "main" (func $main))
- (export "__data_end" (global $global$2))
- (func $main (param $0 i32) (param $1 i32) (result i32)
- (i32.const 0)
- )
-)
-
diff --git a/test/lld/gdollar_mainmodule.wat.out b/test/lld/gdollar_mainmodule.wat.out
deleted file mode 100644
index 29eec0b0f..000000000
--- a/test/lld/gdollar_mainmodule.wat.out
+++ /dev/null
@@ -1,73 +0,0 @@
-(module
- (type $none_=>_i32 (func (result i32)))
- (type $none_=>_none (func))
- (type $i32_i32_=>_i32 (func (param i32 i32) (result i32)))
- (import "env" "memory" (memory $0 0))
- (data (global.get $gimport$3) "")
- (import "env" "table" (table $timport$1 0 funcref))
- (import "env" "__stack_pointer" (global $gimport$2_import i32))
- (import "env" "__memory_base" (global $gimport$3 i32))
- (import "env" "__table_base" (global $gimport$4 i32))
- (import "env" "g$someglobal1" (func $g$someglobal1 (result i32)))
- (import "env" "g$someglobal3" (func $g$someglobal3 (result i32)))
- (global $gimport$6 (mut i32) (i32.const 0))
- (global $gimport$7 (mut i32) (i32.const 0))
- (global $gimport$8 (mut i32) (i32.const 0))
- (global $global$1 i32 (i32.const 54))
- (global $global$2 i32 (i32.const 0))
- (global $gimport$2 (mut i32) (global.get $gimport$2_import))
- (export "someglobal2" (global $global$1))
- (export "main" (func $main))
- (export "__data_end" (global $global$2))
- (export "__assign_got_enties" (func $__assign_got_enties))
- (func $main (param $0 i32) (param $1 i32) (result i32)
- (i32.const 0)
- )
- (func $__assign_got_enties
- (global.set $gimport$6
- (call $g$someglobal1)
- )
- (global.set $gimport$7
- (i32.add
- (global.get $gimport$3)
- (global.get $global$1)
- )
- )
- (global.set $gimport$8
- (call $g$someglobal3)
- )
- )
-)
-(;
---BEGIN METADATA --
-{
- "staticBump": 4294966728,
- "tableSize": 0,
- "initializers": [
- "__assign_got_enties"
- ],
- "declares": [
- "g$someglobal1",
- "g$someglobal3"
- ],
- "externs": [
- "___stack_pointer",
- "___memory_base",
- "___table_base"
- ],
- "exports": [
- "main",
- "__assign_got_enties"
- ],
- "namedGlobals": {
- "someglobal2" : "54",
- "__data_end" : "0"
- },
- "invokeFuncs": [
- ],
- "mainReadsParams": 1,
- "features": [
- ]
-}
--- END METADATA --
-;)
diff --git a/test/lld/main_module.wat.out b/test/lld/main_module.wat.out
index 6ff88c31a..4a482d755 100644
--- a/test/lld/main_module.wat.out
+++ b/test/lld/main_module.wat.out
@@ -4,14 +4,14 @@
(type $i32_=>_i32 (func (param i32) (result i32)))
(import "env" "memory" (memory $0 0))
(data (global.get $gimport$2) "Hello, world\00\00\00\00\00\00\00\00\00\00\00\00")
- (import "env" "table" (table $timport$1 1 funcref))
- (elem (global.get $gimport$3) $print_message\28\29)
+ (import "env" "table" (table $timport$1 0 funcref))
(import "env" "__stack_pointer" (global $sp_import i32))
(import "env" "__memory_base" (global $gimport$2 i32))
(import "env" "__table_base" (global $gimport$3 i32))
(import "env" "puts" (func $puts (param i32) (result i32)))
(import "env" "g$external_var" (func $g$external_var (result i32)))
(import "env" "fp$puts$ii" (func $fp$puts$ii (result i32)))
+ (import "env" "fp$_Z13print_messagev$i" (func $fp$_Z13print_messagev$i (result i32)))
(global $gimport$5 (mut i32) (i32.const 0))
(global $gimport$6 (mut i32) (i32.const 0))
(global $gimport$7 (mut i32) (i32.const 0))
@@ -24,7 +24,6 @@
(export "ptr_puts" (global $global$0))
(export "ptr_local_func" (global $global$1))
(export "__data_end" (global $global))
- (export "dynCall_i" (func $dynCall_i))
(export "__assign_got_enties" (func $__assign_got_enties))
(func $__wasm_call_ctors
(call $__wasm_apply_relocs)
@@ -66,15 +65,7 @@
(call $fp$puts$ii)
)
(global.set $gimport$7
- (i32.add
- (global.get $gimport$3)
- (i32.const 0)
- )
- )
- )
- (func $dynCall_i (param $fptr i32) (result i32)
- (call_indirect (type $none_=>_i32)
- (local.get $fptr)
+ (call $fp$_Z13print_messagev$i)
)
)
)
@@ -82,7 +73,7 @@
--BEGIN METADATA --
{
"staticBump": 4294966770,
- "tableSize": 1,
+ "tableSize": 0,
"initializers": [
"__assign_got_enties",
"__wasm_call_ctors"
@@ -90,7 +81,8 @@
"declares": [
"puts",
"g$external_var",
- "fp$puts$ii"
+ "fp$puts$ii",
+ "fp$_Z13print_messagev$i"
],
"externs": [
"___stack_pointer",
@@ -100,7 +92,6 @@
"exports": [
"__wasm_call_ctors",
"_Z13print_messagev",
- "dynCall_i",
"__assign_got_enties"
],
"namedGlobals": {
diff --git a/test/lld/main_module_table.wat.out b/test/lld/main_module_table.wat.out
index 99939706f..740ab5317 100644
--- a/test/lld/main_module_table.wat.out
+++ b/test/lld/main_module_table.wat.out
@@ -1,31 +1,20 @@
(module
(type $none_=>_none (func))
- (type $i32_=>_none (func (param i32)))
+ (type $none_=>_i32 (func (result i32)))
(import "env" "__stack_pointer" (global $sp_import i32))
- (import "env" "__table_base" (global $__table_base i32))
- (table $0 1 funcref)
- (elem (global.get $__table_base) $__stdio_write)
+ (import "env" "fp$__stdio_write$v" (func $fp$__stdio_write$v (result i32)))
(global $gimport$9 (mut i32) (i32.const 0))
(global $global i32 (i32.const 42))
(global $sp (mut i32) (global.get $sp_import))
(export "__stdio_write" (func $__stdio_write))
(export "__data_end" (global $global))
- (export "dynCall_v" (func $dynCall_v))
(export "__assign_got_enties" (func $__assign_got_enties))
(func $__stdio_write
(nop)
)
(func $__assign_got_enties
(global.set $gimport$9
- (i32.add
- (global.get $__table_base)
- (i32.const 0)
- )
- )
- )
- (func $dynCall_v (param $fptr i32)
- (call_indirect (type $none_=>_none)
- (local.get $fptr)
+ (call $fp$__stdio_write$v)
)
)
)
@@ -33,19 +22,18 @@
--BEGIN METADATA --
{
"staticBump": 4294966770,
- "tableSize": 1,
+ "tableSize": 0,
"initializers": [
"__assign_got_enties"
],
"declares": [
+ "fp$__stdio_write$v"
],
"externs": [
- "___stack_pointer",
- "___table_base"
+ "___stack_pointer"
],
"exports": [
"__stdio_write",
- "dynCall_v",
"__assign_got_enties"
],
"namedGlobals": {
diff --git a/test/lld/main_module_table_2.wat.out b/test/lld/main_module_table_2.wat.out
index 69ef69622..201a58fa4 100644
--- a/test/lld/main_module_table_2.wat.out
+++ b/test/lld/main_module_table_2.wat.out
@@ -1,31 +1,21 @@
(module
(type $none_=>_none (func))
- (type $i32_=>_none (func (param i32)))
- (import "env" "table" (table $timport$9 2 funcref))
- (elem (global.get $__table_base) $__stdio_write)
+ (type $none_=>_i32 (func (result i32)))
+ (import "env" "table" (table $timport$9 1 funcref))
(import "env" "__stack_pointer" (global $sp_import i32))
- (import "env" "__table_base" (global $__table_base i32))
+ (import "env" "fp$__stdio_write$v" (func $fp$__stdio_write$v (result i32)))
(global $gimport$9 (mut i32) (i32.const 0))
(global $global i32 (i32.const 42))
(global $sp (mut i32) (global.get $sp_import))
(export "__stdio_write" (func $__stdio_write))
(export "__data_end" (global $global))
- (export "dynCall_v" (func $dynCall_v))
(export "__assign_got_enties" (func $__assign_got_enties))
(func $__stdio_write
(nop)
)
(func $__assign_got_enties
(global.set $gimport$9
- (i32.add
- (global.get $__table_base)
- (i32.const 0)
- )
- )
- )
- (func $dynCall_v (param $fptr i32)
- (call_indirect (type $none_=>_none)
- (local.get $fptr)
+ (call $fp$__stdio_write$v)
)
)
)
@@ -33,19 +23,18 @@
--BEGIN METADATA --
{
"staticBump": 4294966770,
- "tableSize": 2,
+ "tableSize": 1,
"initializers": [
"__assign_got_enties"
],
"declares": [
+ "fp$__stdio_write$v"
],
"externs": [
- "___stack_pointer",
- "___table_base"
+ "___stack_pointer"
],
"exports": [
"__stdio_write",
- "dynCall_v",
"__assign_got_enties"
],
"namedGlobals": {
diff --git a/test/lld/main_module_table_3.wat.out b/test/lld/main_module_table_3.wat.out
index 783c8914e..201a58fa4 100644
--- a/test/lld/main_module_table_3.wat.out
+++ b/test/lld/main_module_table_3.wat.out
@@ -1,31 +1,21 @@
(module
(type $none_=>_none (func))
- (type $i32_=>_none (func (param i32)))
- (import "env" "table" (table $timport$9 2 funcref))
- (elem (i32.const 0) $__stdio_write)
+ (type $none_=>_i32 (func (result i32)))
+ (import "env" "table" (table $timport$9 1 funcref))
(import "env" "__stack_pointer" (global $sp_import i32))
- (import "env" "__table_base" (global $__table_base i32))
+ (import "env" "fp$__stdio_write$v" (func $fp$__stdio_write$v (result i32)))
(global $gimport$9 (mut i32) (i32.const 0))
(global $global i32 (i32.const 42))
(global $sp (mut i32) (global.get $sp_import))
(export "__stdio_write" (func $__stdio_write))
(export "__data_end" (global $global))
- (export "dynCall_v" (func $dynCall_v))
(export "__assign_got_enties" (func $__assign_got_enties))
(func $__stdio_write
(nop)
)
(func $__assign_got_enties
(global.set $gimport$9
- (i32.add
- (global.get $__table_base)
- (i32.const 0)
- )
- )
- )
- (func $dynCall_v (param $fptr i32)
- (call_indirect (type $none_=>_none)
- (local.get $fptr)
+ (call $fp$__stdio_write$v)
)
)
)
@@ -33,19 +23,18 @@
--BEGIN METADATA --
{
"staticBump": 4294966770,
- "tableSize": 2,
+ "tableSize": 1,
"initializers": [
"__assign_got_enties"
],
"declares": [
+ "fp$__stdio_write$v"
],
"externs": [
- "___stack_pointer",
- "___table_base"
+ "___stack_pointer"
],
"exports": [
"__stdio_write",
- "dynCall_v",
"__assign_got_enties"
],
"namedGlobals": {
diff --git a/test/lld/main_module_table_4.wat.out b/test/lld/main_module_table_4.wat.out
index 809a7506e..ab25bc110 100644
--- a/test/lld/main_module_table_4.wat.out
+++ b/test/lld/main_module_table_4.wat.out
@@ -1,31 +1,22 @@
(module
(type $none_=>_none (func))
- (type $i32_=>_none (func (param i32)))
- (import "env" "table" (table $timport$9 2 funcref))
- (elem (global.get $tb) $__stdio_write)
+ (type $none_=>_i32 (func (result i32)))
+ (import "env" "table" (table $timport$9 1 funcref))
(import "env" "__stack_pointer" (global $sp_import i32))
(import "env" "__table_base" (global $tb i32))
+ (import "env" "fp$__stdio_write$v" (func $fp$__stdio_write$v (result i32)))
(global $gimport$9 (mut i32) (i32.const 0))
(global $global i32 (i32.const 42))
(global $sp (mut i32) (global.get $sp_import))
(export "__stdio_write" (func $__stdio_write))
(export "__data_end" (global $global))
- (export "dynCall_v" (func $dynCall_v))
(export "__assign_got_enties" (func $__assign_got_enties))
(func $__stdio_write
(nop)
)
(func $__assign_got_enties
(global.set $gimport$9
- (i32.add
- (global.get $tb)
- (i32.const 0)
- )
- )
- )
- (func $dynCall_v (param $fptr i32)
- (call_indirect (type $none_=>_none)
- (local.get $fptr)
+ (call $fp$__stdio_write$v)
)
)
)
@@ -33,11 +24,12 @@
--BEGIN METADATA --
{
"staticBump": 4294966770,
- "tableSize": 2,
+ "tableSize": 1,
"initializers": [
"__assign_got_enties"
],
"declares": [
+ "fp$__stdio_write$v"
],
"externs": [
"___stack_pointer",
@@ -45,7 +37,6 @@
],
"exports": [
"__stdio_write",
- "dynCall_v",
"__assign_got_enties"
],
"namedGlobals": {
diff --git a/test/lld/main_module_table_5.wat.out b/test/lld/main_module_table_5.wat.out
index dd5e24830..bdc75b825 100644
--- a/test/lld/main_module_table_5.wat.out
+++ b/test/lld/main_module_table_5.wat.out
@@ -1,10 +1,12 @@
(module
(type $none_=>_none (func))
(type $i32_=>_none (func (param i32)))
- (import "env" "table" (table $timport$9 2 funcref))
- (elem (global.get $tb) $other $stuff $__stdio_write)
+ (type $none_=>_i32 (func (result i32)))
+ (import "env" "table" (table $timport$9 1 funcref))
+ (elem (global.get $tb) $other $stuff)
(import "env" "__stack_pointer" (global $sp_import i32))
(import "env" "__table_base" (global $tb i32))
+ (import "env" "fp$__stdio_write$v" (func $fp$__stdio_write$v (result i32)))
(global $gimport$9 (mut i32) (i32.const 0))
(global $global i32 (i32.const 42))
(global $sp (mut i32) (global.get $sp_import))
@@ -23,10 +25,7 @@
)
(func $__assign_got_enties
(global.set $gimport$9
- (i32.add
- (global.get $tb)
- (i32.const 2)
- )
+ (call $fp$__stdio_write$v)
)
)
(func $dynCall_v (param $fptr i32)
@@ -39,11 +38,12 @@
--BEGIN METADATA --
{
"staticBump": 4294966770,
- "tableSize": 2,
+ "tableSize": 1,
"initializers": [
"__assign_got_enties"
],
"declares": [
+ "fp$__stdio_write$v"
],
"externs": [
"___stack_pointer",