Skip to content

Commit e53af7f

Browse files
committedJan 9, 2018
[WebAssembly] Explicitly specify function/global index space in YAML
These indexes are useful because they are not always zero based and functions and globals are referenced elsewhere by their index. This matches what we already do for the type index space. Differential Revision: https://reviews.llvm.org/D41877 llvm-svn: 322121
1 parent d68fa1b commit e53af7f

23 files changed

+163
-70
lines changed
 

‎llvm/include/llvm/BinaryFormat/Wasm.h

+2
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ struct WasmInitExpr {
6666
};
6767

6868
struct WasmGlobal {
69+
uint32_t Index;
6970
int32_t Type;
7071
bool Mutable;
7172
WasmInitExpr InitExpr;
@@ -89,6 +90,7 @@ struct WasmLocalDecl {
8990
};
9091

9192
struct WasmFunction {
93+
uint32_t Index;
9294
std::vector<WasmLocalDecl> Locals;
9395
ArrayRef<uint8_t> Body;
9496
uint32_t CodeSectionOffset;

‎llvm/include/llvm/ObjectYAML/WasmYAML.h

+2
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ struct ElemSegment {
6666
};
6767

6868
struct Global {
69+
uint32_t Index;
6970
ValueType Type;
7071
bool Mutable;
7172
wasm::WasmInitExpr InitExpr;
@@ -89,6 +90,7 @@ struct LocalDecl {
8990
};
9091

9192
struct Function {
93+
uint32_t Index;
9294
std::vector<LocalDecl> Locals;
9395
yaml::BinaryRef Body;
9496
};

‎llvm/lib/Object/WasmObjectFile.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,7 @@ Error WasmObjectFile::parseGlobalSection(const uint8_t *Ptr, const uint8_t *End)
632632
Globals.reserve(Count);
633633
while (Count--) {
634634
wasm::WasmGlobal Global;
635+
Global.Index = NumImportedGlobals + Globals.size();
635636
Global.Type = readVarint7(Ptr);
636637
Global.Mutable = readVaruint1(Ptr);
637638
if (Error Err = readInitExpr(Global.InitExpr, Ptr))
@@ -706,6 +707,7 @@ Error WasmObjectFile::parseCodeSection(const uint8_t *Ptr, const uint8_t *End) {
706707
uint32_t Size = readVaruint32(Ptr);
707708
const uint8_t *FunctionEnd = Ptr + Size;
708709

710+
Function.Index = NumImportedFunctions + Functions.size();
709711
Function.CodeSectionOffset = FunctionStart - CodeSectionStart;
710712
Function.Size = FunctionEnd - FunctionStart;
711713

@@ -858,7 +860,7 @@ uint64_t WasmObjectFile::getWasmSymbolValue(const WasmSymbol& Sym) const {
858860
case WasmSymbol::SymbolType::GLOBAL_EXPORT: {
859861
uint32_t GlobalIndex = Sym.ElementIndex - NumImportedGlobals;
860862
assert(GlobalIndex < Globals.size());
861-
const wasm::WasmGlobal& Global = Globals[GlobalIndex];
863+
const wasm::WasmGlobal &Global = Globals[GlobalIndex];
862864
// WasmSymbols correspond only to I32_CONST globals
863865
assert(Global.InitExpr.Opcode == wasm::WASM_OPCODE_I32_CONST);
864866
return Global.InitExpr.Value.Int32;

‎llvm/lib/ObjectYAML/WasmYAML.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ void ScalarEnumerationTraits<WasmYAML::SectionType>::enumeration(
236236

237237
void MappingTraits<WasmYAML::Signature>::mapping(
238238
IO &IO, WasmYAML::Signature &Signature) {
239-
IO.mapOptional("Index", Signature.Index);
239+
IO.mapRequired("Index", Signature.Index);
240240
IO.mapRequired("ReturnType", Signature.ReturnType);
241241
IO.mapRequired("ParamTypes", Signature.ParamTypes);
242242
}
@@ -248,6 +248,7 @@ void MappingTraits<WasmYAML::Table>::mapping(IO &IO, WasmYAML::Table &Table) {
248248

249249
void MappingTraits<WasmYAML::Function>::mapping(IO &IO,
250250
WasmYAML::Function &Function) {
251+
IO.mapRequired("Index", Function.Index);
251252
IO.mapRequired("Locals", Function.Locals);
252253
IO.mapRequired("Body", Function.Body);
253254
}
@@ -323,6 +324,7 @@ void MappingTraits<WasmYAML::Export>::mapping(IO &IO,
323324

324325
void MappingTraits<WasmYAML::Global>::mapping(IO &IO,
325326
WasmYAML::Global &Global) {
327+
IO.mapRequired("Index", Global.Index);
326328
IO.mapRequired("Type", Global.Type);
327329
IO.mapRequired("Mutable", Global.Mutable);
328330
IO.mapRequired("InitExpr", Global.InitExpr);

‎llvm/test/MC/WebAssembly/bss.ll

+8-4
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,26 @@
99

1010
; CHECK: - Type: GLOBAL
1111
; CHECK-NEXT: Globals:
12-
; CHECK-NEXT: - Type: I32
12+
; CHECK-NEXT: - Index: 0
13+
; CHECK-NEXT: Type: I32
1314
; CHECK-NEXT: Mutable: false
1415
; CHECK-NEXT: InitExpr:
1516
; CHECK-NEXT: Opcode: I32_CONST
1617
; CHECK-NEXT: Value: 0
17-
; CHECK-NEXT: - Type: I32
18+
; CHECK-NEXT: - Index: 1
19+
; CHECK-NEXT: Type: I32
1820
; CHECK-NEXT: Mutable: false
1921
; CHECK-NEXT: InitExpr:
2022
; CHECK-NEXT: Opcode: I32_CONST
2123
; CHECK-NEXT: Value: 4
22-
; CHECK-NEXT: - Type: I32
24+
; CHECK-NEXT: - Index: 2
25+
; CHECK-NEXT: Type: I32
2326
; CHECK-NEXT: Mutable: false
2427
; CHECK-NEXT: InitExpr:
2528
; CHECK-NEXT: Opcode: I32_CONST
2629
; CHECK-NEXT: Value: 8
27-
; CHECK-NEXT: - Type: I32
30+
; CHECK-NEXT: - Index: 3
31+
; CHECK-NEXT: Type: I32
2832
; CHECK-NEXT: Mutable: false
2933
; CHECK-NEXT: InitExpr:
3034
; CHECK-NEXT: Opcode: I32_CONST

‎llvm/test/MC/WebAssembly/explicit-sections.ll

+8-4
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,26 @@
99

1010
; CHECK: - Type: GLOBAL
1111
; CHECK-NEXT: Globals:
12-
; CHECK-NEXT: - Type: I32
12+
; CHECK-NEXT: - Index: 0
13+
; CHECK-NEXT: Type: I32
1314
; CHECK-NEXT: Mutable: false
1415
; CHECK-NEXT: InitExpr:
1516
; CHECK-NEXT: Opcode: I32_CONST
1617
; CHECK-NEXT: Value: 0
17-
; CHECK-NEXT: - Type: I32
18+
; CHECK-NEXT: - Index: 1
19+
; CHECK-NEXT: Type: I32
1820
; CHECK-NEXT: Mutable: false
1921
; CHECK-NEXT: InitExpr:
2022
; CHECK-NEXT: Opcode: I32_CONST
2123
; CHECK-NEXT: Value: 8
22-
; CHECK-NEXT: - Type: I32
24+
; CHECK-NEXT: - Index: 2
25+
; CHECK-NEXT: Type: I32
2326
; CHECK-NEXT: Mutable: false
2427
; CHECK-NEXT: InitExpr:
2528
; CHECK-NEXT: Opcode: I32_CONST
2629
; CHECK-NEXT: Value: 16
27-
; CHECK-NEXT: - Type: I32
30+
; CHECK-NEXT: - Index: 3
31+
; CHECK-NEXT: Type: I32
2832
; CHECK-NEXT: Mutable: false
2933
; CHECK-NEXT: InitExpr:
3034
; CHECK-NEXT: Opcode: I32_CONST

‎llvm/test/MC/WebAssembly/init-fini-array.ll

+10-5
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ declare void @func3()
5555
; CHECK-NEXT: FunctionTypes: [ 0, 1, 0, 1 ]
5656
; CHECK-NEXT: - Type: GLOBAL
5757
; CHECK-NEXT: Globals:
58-
; CHECK-NEXT: - Type: I32
58+
; CHECK-NEXT: - Index: 1
59+
; CHECK-NEXT: Type: I32
5960
; CHECK-NEXT: Mutable: false
6061
; CHECK-NEXT: InitExpr:
6162
; CHECK-NEXT: Opcode: I32_CONST
@@ -110,13 +111,17 @@ declare void @func3()
110111
; CHECK-NEXT: Index: 1
111112
; CHECK-NEXT: Offset: 0x00000045
112113
; CHECK-NEXT: Functions:
113-
; CHECK-NEXT: - Locals:
114+
; CHECK-NEXT: - Index: 5
115+
; CHECK-NEXT: Locals:
114116
; CHECK-NEXT: Body: 1080808080000B
115-
; CHECK-NEXT: - Locals:
117+
; CHECK-NEXT: - Index: 6
118+
; CHECK-NEXT: Locals:
116119
; CHECK-NEXT: Body: 0240418080808000410041FFFFFFFF7F1081808080000D000F0B00000B
117-
; CHECK-NEXT: - Locals:
120+
; CHECK-NEXT: - Index: 7
121+
; CHECK-NEXT: Locals:
118122
; CHECK-NEXT: Body: 1082808080000B
119-
; CHECK-NEXT: - Locals:
123+
; CHECK-NEXT: - Index: 8
124+
; CHECK-NEXT: Locals:
120125
; CHECK-NEXT: Body: 0240418180808000410041FFFFFFFF7F1081808080000D000F0B00000B
121126
; CHECK-NEXT: - Type: DATA
122127
; CHECK-NEXT: Segments:

‎llvm/test/MC/WebAssembly/unnamed-data.ll

+8-4
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,26 @@
99

1010
; CHECK: - Type: GLOBAL
1111
; CHECK-NEXT: Globals:
12-
; CHECK-NEXT: - Type: I32
12+
; CHECK-NEXT: - Index: 0
13+
; CHECK-NEXT: Type: I32
1314
; CHECK-NEXT: Mutable: false
1415
; CHECK-NEXT: InitExpr:
1516
; CHECK-NEXT: Opcode: I32_CONST
1617
; CHECK-NEXT: Value: 0
17-
; CHECK-NEXT: - Type: I32
18+
; CHECK-NEXT: - Index: 1
19+
; CHECK-NEXT: Type: I32
1820
; CHECK-NEXT: Mutable: false
1921
; CHECK-NEXT: InitExpr:
2022
; CHECK-NEXT: Opcode: I32_CONST
2123
; CHECK-NEXT: Value: 6
22-
; CHECK-NEXT: - Type: I32
24+
; CHECK-NEXT: - Index: 2
25+
; CHECK-NEXT: Type: I32
2326
; CHECK-NEXT: Mutable: false
2427
; CHECK-NEXT: InitExpr:
2528
; CHECK-NEXT: Opcode: I32_CONST
2629
; CHECK-NEXT: Value: 16
27-
; CHECK-NEXT: - Type: I32
30+
; CHECK-NEXT: - Index: 3
31+
; CHECK-NEXT: Type: I32
2832
; CHECK-NEXT: Mutable: false
2933
; CHECK-NEXT: InitExpr:
3034
; CHECK-NEXT: Opcode: I32_CONST

‎llvm/test/MC/WebAssembly/weak-alias.ll

+16-8
Original file line numberDiff line numberDiff line change
@@ -76,17 +76,20 @@ entry:
7676
; CHECK-NEXT: FunctionTypes: [ 0, 0, 0, 0, 0 ]
7777
; CHECK-NEXT: - Type: GLOBAL
7878
; CHECK-NEXT: Globals:
79-
; CHECK-NEXT: - Type: I32
79+
; CHECK-NEXT: - Index: 1
80+
; CHECK-NEXT: Type: I32
8081
; CHECK-NEXT: Mutable: false
8182
; CHECK-NEXT: InitExpr:
8283
; CHECK-NEXT: Opcode: I32_CONST
8384
; CHECK-NEXT: Value: 8
84-
; CHECK-NEXT: - Type: I32
85+
; CHECK-NEXT: - Index: 2
86+
; CHECK-NEXT: Type: I32
8587
; CHECK-NEXT: Mutable: false
8688
; CHECK-NEXT: InitExpr:
8789
; CHECK-NEXT: Opcode: I32_CONST
8890
; CHECK-NEXT: Value: 16
89-
; CHECK-NEXT: - Type: I32
91+
; CHECK-NEXT: - Index: 3
92+
; CHECK-NEXT: Type: I32
9093
; CHECK-NEXT: Mutable: false
9194
; CHECK-NEXT: InitExpr:
9295
; CHECK-NEXT: Opcode: I32_CONST
@@ -150,15 +153,20 @@ entry:
150153
; CHECK-NEXT: Index: 0
151154
; CHECK-NEXT: Offset: 0x00000037
152155
; CHECK-NEXT: Functions:
153-
; CHECK-NEXT: - Locals:
156+
; CHECK-NEXT: - Index: 1
157+
; CHECK-NEXT: Locals:
154158
; CHECK-NEXT: Body: 41000B
155-
; CHECK-NEXT: - Locals:
159+
; CHECK-NEXT: - Index: 2
160+
; CHECK-NEXT: Locals:
156161
; CHECK-NEXT: Body: 1081808080000B
157-
; CHECK-NEXT: - Locals:
162+
; CHECK-NEXT: - Index: 3
163+
; CHECK-NEXT: Locals:
158164
; CHECK-NEXT: Body: 1080808080000B
159-
; CHECK-NEXT: - Locals:
165+
; CHECK-NEXT: - Index: 4
166+
; CHECK-NEXT: Locals:
160167
; CHECK-NEXT: Body: 410028028880808000118080808000000B
161-
; CHECK-NEXT: - Locals:
168+
; CHECK-NEXT: - Index: 5
169+
; CHECK-NEXT: Locals:
162170
; CHECK-NEXT: Body: 410028029080808000118080808000000B
163171
; CHECK-NEXT: - Type: DATA
164172
; CHECK-NEXT: Relocations:

‎llvm/test/ObjectYAML/wasm/code_section.yaml

+12-6
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ FileHeader:
55
Sections:
66
- Type: TYPE
77
Signatures:
8-
- ReturnType: F32
8+
- Index: 0
9+
ReturnType: F32
910
ParamTypes:
1011
- I32
11-
- ReturnType: NORESULT
12+
- Index: 1
13+
ReturnType: NORESULT
1214
ParamTypes:
1315
- I32
1416
- I64
@@ -25,11 +27,13 @@ Sections:
2527
Index: 1
2628
Offset: 0x00000025
2729
Functions:
28-
- Locals:
30+
- Index: 0
31+
Locals:
2932
- Type: I32
3033
Count: 3
3134
Body: 418080808000210020002101200111808080800000210220020F0B
32-
- Locals:
35+
- Index: 1
36+
Locals:
3337
- Type: I32
3438
Count: 1
3539
Body: 108180808000210020000F0B
@@ -58,11 +62,13 @@ Sections:
5862
# CHECK: Index: 1
5963
# CHECK: Offset: 0x00000025
6064
# CHECK: Functions:
61-
# CHECK: - Locals:
65+
# CHECK: - Index: 0
66+
# CHECK: Locals:
6267
# CHECK: - Type: I32
6368
# CHECK: Count: 3
6469
# CHECK: Body: 418080808000210020002101200111808080800000210220020F0B
65-
# CHECK: - Locals:
70+
# CHECK: - Index: 1
71+
# CHECK: Locals:
6672
# CHECK: - Type: I32
6773
# CHECK: Count: 1
6874
# CHECK: Body: 108180808000210020000F0B

‎llvm/test/ObjectYAML/wasm/export_section.yaml

+4-2
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@ Sections:
77
FunctionTypes: [ 0, 0 ]
88
- Type: GLOBAL
99
Globals:
10-
- Type: I32
10+
- Index: 0
11+
Type: I32
1112
Mutable: false
1213
InitExpr:
1314
Opcode: I64_CONST
1415
Value: 32
15-
- Type: I32
16+
- Index: 1
17+
Type: I32
1618
Mutable: false
1719
InitExpr:
1820
Opcode: I64_CONST

‎llvm/test/ObjectYAML/wasm/global_section.yaml

+4-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ FileHeader:
55
Sections:
66
- Type: GLOBAL
77
Globals:
8-
- Type: I32
8+
- Index: 0
9+
Type: I32
910
Mutable: false
1011
InitExpr:
1112
Opcode: I64_CONST
@@ -17,7 +18,8 @@ Sections:
1718
# CHECK: Sections:
1819
# CHECK: - Type: GLOBAL
1920
# CHECK: Globals:
20-
# CHECK: - Type: I32
21+
# CHECK: - Index: 0
22+
# CHECK: Type: I32
2123
# CHECK: Mutable: false
2224
# CHECK: InitExpr:
2325
# CHECK: Opcode: I64_CONST

‎llvm/test/ObjectYAML/wasm/import_section.yaml

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ FileHeader:
55
Sections:
66
- Type: TYPE
77
Signatures:
8-
- ReturnType: I32
8+
- Index: 0
9+
ReturnType: I32
910
ParamTypes:
1011
- I32
1112
- Type: IMPORT

‎llvm/test/ObjectYAML/wasm/linking_section.yaml

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ FileHeader:
55
Sections:
66
- Type: TYPE
77
Signatures:
8-
- ReturnType: I32
8+
- Index: 0
9+
ReturnType: I32
910
ParamTypes:
1011
- I32
1112
- Type: IMPORT

‎llvm/test/ObjectYAML/wasm/name_section.yaml

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ FileHeader:
55
Sections:
66
- Type: TYPE
77
Signatures:
8-
- ReturnType: I32
8+
- Index: 0
9+
ReturnType: I32
910
ParamTypes:
1011
- I32
1112
- Type: IMPORT

‎llvm/test/ObjectYAML/wasm/start_section.yaml

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ FileHeader:
66
Sections:
77
- Type: TYPE
88
Signatures:
9-
- ReturnType: I32
9+
- Index: 0
10+
ReturnType: I32
1011
ParamTypes:
1112
- F32
1213
- F32

‎llvm/test/ObjectYAML/wasm/type_section.yaml

+4-2
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ FileHeader:
55
Sections:
66
- Type: TYPE
77
Signatures:
8-
- ReturnType: I32
8+
- Index: 0
9+
ReturnType: I32
910
ParamTypes:
1011
- F32
1112
- F32
12-
- ReturnType: I64
13+
- Index: 1
14+
ReturnType: I64
1315
ParamTypes:
1416
- F64
1517
- F64

‎llvm/test/ObjectYAML/wasm/weak_symbols.yaml

+4-2
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@ FileHeader:
55
Sections:
66
- Type: TYPE
77
Signatures:
8-
- ReturnType: I32
8+
- Index: 0
9+
ReturnType: I32
910
ParamTypes:
1011
- Type: FUNCTION
1112
FunctionTypes: [ 0, 0 ]
1213
- Type: GLOBAL
1314
Globals:
14-
- Type: I32
15+
- Index: 0
16+
Type: I32
1517
Mutable: false
1618
InitExpr:
1719
Opcode: I32_CONST

‎llvm/test/tools/llvm-nm/wasm/exports.yaml

+19-15
Original file line numberDiff line numberDiff line change
@@ -9,39 +9,43 @@ FileHeader:
99
Sections:
1010
- Type: TYPE
1111
Signatures:
12-
- ReturnType: I32
12+
- Index: 0
13+
ReturnType: I32
1314
ParamTypes:
1415
- I32
16+
- Type: IMPORT
17+
Imports:
18+
- Module: env
19+
Field: fimport
20+
Kind: FUNCTION
21+
SigIndex: 0
22+
- Module: env
23+
Field: gimport
24+
Kind: GLOBAL
25+
GlobalType: I32
26+
GlobalMutable: false
1527
- Type: FUNCTION
1628
FunctionTypes: [ 0, 0, 0, 0, 0 ]
1729
- Type: GLOBAL
1830
Globals:
19-
- Type: I32
31+
- Index: 1
32+
Type: I32
2033
Mutable: false
2134
InitExpr:
2235
Opcode: I64_CONST
2336
Value: 32
24-
- Type: I32
37+
- Index: 2
38+
Type: I32
2539
Mutable: false
2640
InitExpr:
2741
Opcode: I32_CONST
2842
Value: 64
29-
- Type: I32
43+
- Index: 3
44+
Type: I32
3045
Mutable: false
3146
InitExpr:
3247
Opcode: I32_CONST
3348
Value: 1024
34-
- Type: IMPORT
35-
Imports:
36-
- Module: env
37-
Field: fimport
38-
Kind: FUNCTION
39-
SigIndex: 0
40-
- Module: env
41-
Field: gimport
42-
Kind: GLOBAL
43-
GlobalType: I32
44-
GlobalMutable: false
4549
- Type: EXPORT
4650
Exports:
4751
- Name: foo

‎llvm/test/tools/llvm-nm/wasm/imports.yaml

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ FileHeader:
66
Sections:
77
- Type: TYPE
88
Signatures:
9-
- ReturnType: I32
9+
- Index: 0
10+
ReturnType: I32
1011
ParamTypes:
1112
- I32
1213
- Type: IMPORT

‎llvm/test/tools/llvm-nm/wasm/weak-symbols.yaml

+10-6
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,10 @@ FileHeader:
99
Sections:
1010
- Type: TYPE
1111
Signatures:
12-
- ReturnType: I32
12+
- Index: 0
13+
ReturnType: I32
1314
ParamTypes:
1415
- I32
15-
- Type: FUNCTION
16-
FunctionTypes: [ 0, 0, 0, 0 ]
1716
- Type: IMPORT
1817
Imports:
1918
- Module: env
@@ -25,19 +24,24 @@ Sections:
2524
Kind: GLOBAL
2625
GlobalType: I32
2726
GlobalMutable: false
27+
- Type: FUNCTION
28+
FunctionTypes: [ 0, 0, 0, 0 ]
2829
- Type: GLOBAL
2930
Globals:
30-
- Type: I32
31+
- Index: 1
32+
Type: I32
3133
Mutable: false
3234
InitExpr:
3335
Opcode: I64_CONST
3436
Value: 32
35-
- Type: I32
37+
- Index: 2
38+
Type: I32
3639
Mutable: false
3740
InitExpr:
3841
Opcode: I32_CONST
3942
Value: 64
40-
- Type: I32
43+
- Index: 3
44+
Type: I32
4145
Mutable: false
4246
InitExpr:
4347
Opcode: I32_CONST

‎llvm/tools/obj2yaml/wasm2yaml.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ ErrorOr<WasmYAML::Object *> WasmDumper::dump() {
186186
auto GlobalSec = make_unique<WasmYAML::GlobalSection>();
187187
for (auto &Global : Obj.globals()) {
188188
WasmYAML::Global G;
189+
G.Index = Global.Index;
189190
G.Type = Global.Type;
190191
G.Mutable = Global.Mutable;
191192
G.InitExpr = Global.InitExpr;
@@ -230,6 +231,7 @@ ErrorOr<WasmYAML::Object *> WasmDumper::dump() {
230231
auto CodeSec = make_unique<WasmYAML::CodeSection>();
231232
for (auto &Func : Obj.functions()) {
232233
WasmYAML::Function Function;
234+
Function.Index = Func.Index;
233235
for (auto &Local : Func.Locals) {
234236
WasmYAML::LocalDecl LocalDecl;
235237
LocalDecl.Type = Local.Type;

‎llvm/tools/yaml2obj/yaml2wasm.cpp

+34-3
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ class WasmWriter {
4545
int writeSectionContent(raw_ostream &OS, WasmYAML::NameSection &Section);
4646
int writeSectionContent(raw_ostream &OS, WasmYAML::LinkingSection &Section);
4747
WasmYAML::Object &Obj;
48+
uint32_t NumImportedFunctions = 0;
49+
uint32_t NumImportedGlobals = 0;
4850
};
4951

5052
static int writeUint64(raw_ostream &OS, uint64_t Value) {
@@ -101,7 +103,7 @@ static int writeInitExpr(const wasm::WasmInitExpr &InitExpr, raw_ostream &OS) {
101103
encodeULEB128(InitExpr.Value.Global, OS);
102104
break;
103105
default:
104-
errs() << "Unknown opcode in init_expr: " << InitExpr.Opcode;
106+
errs() << "Unknown opcode in init_expr: " << InitExpr.Opcode << "\n";
105107
return 1;
106108
}
107109
writeUint8(OS, wasm::WASM_OPCODE_END);
@@ -211,7 +213,13 @@ int WasmWriter::writeSectionContent(raw_ostream &OS,
211213
int WasmWriter::writeSectionContent(raw_ostream &OS,
212214
WasmYAML::TypeSection &Section) {
213215
encodeULEB128(Section.Signatures.size(), OS);
216+
uint32_t ExpectedIndex = 0;
214217
for (const WasmYAML::Signature &Sig : Section.Signatures) {
218+
if (Sig.Index != ExpectedIndex) {
219+
errs() << "Unexpected type index: " << Sig.Index << "\n";
220+
return 1;
221+
}
222+
++ExpectedIndex;
215223
encodeSLEB128(Sig.Form, OS);
216224
encodeULEB128(Sig.ParamTypes.size(), OS);
217225
for (auto ParamType : Sig.ParamTypes)
@@ -236,10 +244,12 @@ int WasmWriter::writeSectionContent(raw_ostream &OS,
236244
switch (Import.Kind) {
237245
case wasm::WASM_EXTERNAL_FUNCTION:
238246
encodeULEB128(Import.SigIndex, OS);
247+
NumImportedFunctions++;
239248
break;
240249
case wasm::WASM_EXTERNAL_GLOBAL:
241250
encodeSLEB128(Import.GlobalImport.Type, OS);
242251
writeUint8(OS, Import.GlobalImport.Mutable);
252+
NumImportedGlobals++;
243253
break;
244254
case wasm::WASM_EXTERNAL_MEMORY:
245255
writeLimits(Import.Memory, OS);
@@ -249,7 +259,7 @@ int WasmWriter::writeSectionContent(raw_ostream &OS,
249259
writeLimits(Import.TableImport.TableLimits, OS);
250260
break;
251261
default:
252-
errs() << "Unknown import type: " << Import.Kind;
262+
errs() << "Unknown import type: " << Import.Kind << "\n";
253263
return 1;
254264
}
255265
}
@@ -304,7 +314,13 @@ int WasmWriter::writeSectionContent(raw_ostream &OS,
304314
int WasmWriter::writeSectionContent(raw_ostream &OS,
305315
WasmYAML::GlobalSection &Section) {
306316
encodeULEB128(Section.Globals.size(), OS);
317+
uint32_t ExpectedIndex = NumImportedGlobals;
307318
for (auto &Global : Section.Globals) {
319+
if (Global.Index != ExpectedIndex) {
320+
errs() << "Unexpected global index: " << Global.Index << "\n";
321+
return 1;
322+
}
323+
++ExpectedIndex;
308324
encodeSLEB128(Global.Type, OS);
309325
writeUint8(OS, Global.Mutable);
310326
writeInitExpr(Global.InitExpr, OS);
@@ -330,9 +346,15 @@ int WasmWriter::writeSectionContent(raw_ostream &OS,
330346
int WasmWriter::writeSectionContent(raw_ostream &OS,
331347
WasmYAML::CodeSection &Section) {
332348
encodeULEB128(Section.Functions.size(), OS);
349+
uint32_t ExpectedIndex = NumImportedFunctions;
333350
for (auto &Func : Section.Functions) {
334351
std::string OutString;
335352
raw_string_ostream StringStream(OutString);
353+
if (Func.Index != ExpectedIndex) {
354+
errs() << "Unexpected function index: " << Func.Index << "\n";
355+
return 1;
356+
}
357+
++ExpectedIndex;
336358

337359
encodeULEB128(Func.Locals.size(), StringStream);
338360
for (auto &LocalDecl : Func.Locals) {
@@ -402,9 +424,18 @@ int WasmWriter::writeWasm(raw_ostream &OS) {
402424
writeUint32(OS, Obj.Header.Version);
403425

404426
// Write each section
427+
uint32_t LastType = 0;
405428
for (const std::unique_ptr<WasmYAML::Section> &Sec : Obj.Sections) {
406-
encodeULEB128(Sec->Type, OS);
429+
uint32_t Type = Sec->Type;
430+
if (Type != wasm::WASM_SEC_CUSTOM) {
431+
if (Type < LastType) {
432+
errs() << "Out of order section type: " << Type << "\n";
433+
return 1;
434+
}
435+
LastType = Type;
436+
}
407437

438+
encodeULEB128(Sec->Type, OS);
408439
std::string OutString;
409440
raw_string_ostream StringStream(OutString);
410441
if (auto S = dyn_cast<WasmYAML::CustomSection>(Sec.get())) {

0 commit comments

Comments
 (0)
Please sign in to comment.