Skip to content

Commit 77c6602

Browse files
authored
Emit imports before defined things in text format (#1715)
That is the correct order in the text format, wabt errors otherwise. See AssemblyScript/assemblyscript#310
1 parent 7d3ddd0 commit 77c6602

File tree

188 files changed

+297
-260
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

188 files changed

+297
-260
lines changed

src/ir/module-utils.h

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,35 @@ inline void copyModule(Module& in, Module& out) {
128128
out.debugInfoFileNames = in.debugInfoFileNames;
129129
}
130130

131-
// Convenient iteration over imported/non-imported functions/globals
131+
// Convenient iteration over imported/non-imported module elements
132+
133+
template<typename T>
134+
inline void iterImportedMemories(Module& wasm, T visitor) {
135+
if (wasm.memory.exists && wasm.memory.imported()) {
136+
visitor(&wasm.memory);
137+
}
138+
}
139+
140+
template<typename T>
141+
inline void iterDefinedMemories(Module& wasm, T visitor) {
142+
if (wasm.memory.exists && !wasm.memory.imported()) {
143+
visitor(&wasm.memory);
144+
}
145+
}
146+
147+
template<typename T>
148+
inline void iterImportedTables(Module& wasm, T visitor) {
149+
if (wasm.table.exists && wasm.table.imported()) {
150+
visitor(&wasm.table);
151+
}
152+
}
153+
154+
template<typename T>
155+
inline void iterDefinedTables(Module& wasm, T visitor) {
156+
if (wasm.table.exists && !wasm.table.imported()) {
157+
visitor(&wasm.table);
158+
}
159+
}
132160

133161
template<typename T>
134162
inline void iterImportedGlobals(Module& wasm, T visitor) {

src/passes/Print.cpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -957,6 +957,7 @@ struct PrintSExpression : public Visitor<PrintSExpression> {
957957
void printTableHeader(Table* curr) {
958958
o << '(';
959959
printMedium(o, "table") << ' ';
960+
printName(curr->name, o) << ' ';
960961
o << curr->initial;
961962
if (curr->hasMax()) o << ' ' << curr->max;
962963
o << " anyfunc)";
@@ -1056,16 +1057,24 @@ struct PrintSExpression : public Visitor<PrintSExpression> {
10561057
visitFunctionType(child.get());
10571058
o << ")" << maybeNewLine;
10581059
}
1059-
visitMemory(&curr->memory);
1060-
if (curr->table.exists) {
1061-
visitTable(&curr->table); // Prints its own newlines
1062-
}
1060+
ModuleUtils::iterImportedMemories(*curr, [&](Memory* memory) {
1061+
visitMemory(memory);
1062+
});
1063+
ModuleUtils::iterImportedTables(*curr, [&](Table* table) {
1064+
visitTable(table);
1065+
});
10631066
ModuleUtils::iterImportedGlobals(*curr, [&](Global* global) {
10641067
visitGlobal(global);
10651068
});
10661069
ModuleUtils::iterImportedFunctions(*curr, [&](Function* func) {
10671070
visitFunction(func);
10681071
});
1072+
ModuleUtils::iterDefinedMemories(*curr, [&](Memory* memory) {
1073+
visitMemory(memory);
1074+
});
1075+
ModuleUtils::iterDefinedTables(*curr, [&](Table* table) {
1076+
visitTable(table);
1077+
});
10691078
ModuleUtils::iterDefinedGlobals(*curr, [&](Global* global) {
10701079
visitGlobal(global);
10711080
});

test/bad_params.fromasm.clamp.no-opts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
(module
22
(import "env" "memory" (memory $memory 256 256))
3-
(import "env" "table" (table 0 0 anyfunc))
3+
(import "env" "table" (table $table 0 0 anyfunc))
44
(import "env" "memoryBase" (global $memoryBase i32))
55
(import "env" "tableBase" (global $tableBase i32))
66
(export "ex" (func $ex))

test/bad_params.fromasm.imprecise.no-opts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
(module
22
(import "env" "memory" (memory $memory 256 256))
3-
(import "env" "table" (table 0 0 anyfunc))
3+
(import "env" "table" (table $table 0 0 anyfunc))
44
(import "env" "memoryBase" (global $memoryBase i32))
55
(import "env" "tableBase" (global $tableBase i32))
66
(export "ex" (func $ex))

test/bad_params.fromasm.no-opts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
(module
22
(import "env" "memory" (memory $memory 256 256))
3-
(import "env" "table" (table 0 0 anyfunc))
3+
(import "env" "table" (table $table 0 0 anyfunc))
44
(import "env" "memoryBase" (global $memoryBase i32))
55
(import "env" "tableBase" (global $tableBase i32))
66
(export "ex" (func $ex))

test/badvartype.wasm.fromBinary

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
(type $8 (func (param f64) (result f64)))
1111
(memory $0 (shared 1 1))
1212
(data (i32.const 0) "\00\00\00\00\00\00\00\00X\00\00\00U\00\00\0b\00\00\00\00\00\00\00k\00\00")
13-
(table 2 anyfunc)
13+
(table $0 2 anyfunc)
1414
(elem (i32.const 0) $1 $5)
1515
(global $global$0 (mut i32) (i32.const 255))
1616
(global $global$1 (mut i32) (i32.const -7045592))

test/binaryen.js/kitchen-sink.js.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@ getExpressionInfo(f64.const)={"id":14,"type":4,"value":9.5}
4646
(type $fiF (func (param i32 f64) (result f32)))
4747
(type $v (func))
4848
(type $3 (func))
49+
(import "module" "base" (func $an-imported (param i32 f64) (result f32)))
4950
(memory $0 1 256)
5051
(data (i32.const 10) "hello, world")
51-
(table 1 anyfunc)
52+
(table $0 1 anyfunc)
5253
(elem (i32.const 0) "$kitchen()sinker")
53-
(import "module" "base" (func $an-imported (param i32 f64) (result f32)))
5454
(export "kitchen_sinker" (func "$kitchen()sinker"))
5555
(export "mem" (memory $0))
5656
(start $starter)
@@ -1502,11 +1502,11 @@ getExpressionInfo(f64.const)={"id":14,"type":4,"value":9.5}
15021502
(type $fiF (func (param i32 f64) (result f32)))
15031503
(type $v (func))
15041504
(type $3 (func))
1505+
(import "module" "base" (func $an-imported (param i32 f64) (result f32)))
15051506
(memory $0 1 256)
15061507
(data (i32.const 10) "hello, world")
1507-
(table 1 anyfunc)
1508+
(table $0 1 anyfunc)
15081509
(elem (i32.const 0) "$kitchen()sinker")
1509-
(import "module" "base" (func $an-imported (param i32 f64) (result f32)))
15101510
(export "kitchen_sinker" (func "$kitchen()sinker"))
15111511
(export "mem" (memory $0))
15121512
(start $starter)

test/ctor-eval/bad-indirect-call.wast.out

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
(type $v (func))
33
(memory $0 256 256)
44
(data (i32.const 10) "waka waka waka waka waka")
5-
(table 1 1 anyfunc)
5+
(table $0 1 1 anyfunc)
66
(elem (i32.const 0) $call-indirect)
77
(export "test1" (func $test1))
88
(func $test1 (; 0 ;) (type $v)

test/ctor-eval/bad-indirect-call2.wast.out

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
(module
22
(type $FUNCSIG$v (func))
3+
(import "env" "_abort" (func $_abort))
34
(memory $0 256 256)
45
(data (i32.const 10) "waka waka waka waka waka")
5-
(table 2 2 anyfunc)
6+
(table $0 2 2 anyfunc)
67
(elem (i32.const 0) $_abort $call-indirect)
7-
(import "env" "_abort" (func $_abort))
88
(export "test1" (func $test1))
99
(func $test1 (; 1 ;) (type $FUNCSIG$v)
1010
(call_indirect (type $FUNCSIG$v)

test/ctor-eval/indirect-call3.wast.out

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
(module
22
(type $FUNCSIG$v (func))
3-
(memory $0 256 256)
4-
(data (i32.const 10) "waka waka xaka waka waka\00\00\00\00\00\00C")
53
(import "env" "tableBase" (global $tableBase i32))
64
(import "env" "_abort" (func $_abort))
5+
(memory $0 256 256)
6+
(data (i32.const 10) "waka waka xaka waka waka\00\00\00\00\00\00C")
77
(func $call-indirect (; 1 ;) (type $FUNCSIG$v)
88
(i32.store8
99
(i32.const 40)

0 commit comments

Comments
 (0)