llvm.org GIT mirror llvm / 6b54fc3
[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 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@322121 91177308-0d34-0410-b5e6-96231b3b80d8 Sam Clegg 1 year, 8 months ago
23 changed file(s) with 168 addition(s) and 75 deletion(s). Raw diff Collapse all Expand all
6565 };
6666
6767 struct WasmGlobal {
68 uint32_t Index;
6869 int32_t Type;
6970 bool Mutable;
7071 WasmInitExpr InitExpr;
8889 };
8990
9091 struct WasmFunction {
92 uint32_t Index;
9193 std::vector Locals;
9294 ArrayRef Body;
9395 uint32_t CodeSectionOffset;
6565 };
6666
6767 struct Global {
68 uint32_t Index;
6869 ValueType Type;
6970 bool Mutable;
7071 wasm::WasmInitExpr InitExpr;
8889 };
8990
9091 struct Function {
92 uint32_t Index;
9193 std::vector Locals;
9294 yaml::BinaryRef Body;
9395 };
631631 Globals.reserve(Count);
632632 while (Count--) {
633633 wasm::WasmGlobal Global;
634 Global.Index = NumImportedGlobals + Globals.size();
634635 Global.Type = readVarint7(Ptr);
635636 Global.Mutable = readVaruint1(Ptr);
636637 if (Error Err = readInitExpr(Global.InitExpr, Ptr))
705706 uint32_t Size = readVaruint32(Ptr);
706707 const uint8_t *FunctionEnd = Ptr + Size;
707708
709 Function.Index = NumImportedFunctions + Functions.size();
708710 Function.CodeSectionOffset = FunctionStart - CodeSectionStart;
709711 Function.Size = FunctionEnd - FunctionStart;
710712
857859 case WasmSymbol::SymbolType::GLOBAL_EXPORT: {
858860 uint32_t GlobalIndex = Sym.ElementIndex - NumImportedGlobals;
859861 assert(GlobalIndex < Globals.size());
860 const wasm::WasmGlobal& Global = Globals[GlobalIndex];
862 const wasm::WasmGlobal &Global = Globals[GlobalIndex];
861863 // WasmSymbols correspond only to I32_CONST globals
862864 assert(Global.InitExpr.Opcode == wasm::WASM_OPCODE_I32_CONST);
863865 return Global.InitExpr.Value.Int32;
235235
236236 void MappingTraits::mapping(
237237 IO &IO, WasmYAML::Signature &Signature) {
238 IO.mapOptional("Index", Signature.Index);
238 IO.mapRequired("Index", Signature.Index);
239239 IO.mapRequired("ReturnType", Signature.ReturnType);
240240 IO.mapRequired("ParamTypes", Signature.ParamTypes);
241241 }
247247
248248 void MappingTraits::mapping(IO &IO,
249249 WasmYAML::Function &Function) {
250 IO.mapRequired("Index", Function.Index);
250251 IO.mapRequired("Locals", Function.Locals);
251252 IO.mapRequired("Body", Function.Body);
252253 }
322323
323324 void MappingTraits::mapping(IO &IO,
324325 WasmYAML::Global &Global) {
326 IO.mapRequired("Index", Global.Index);
325327 IO.mapRequired("Type", Global.Type);
326328 IO.mapRequired("Mutable", Global.Mutable);
327329 IO.mapRequired("InitExpr", Global.InitExpr);
88
99 ; CHECK: - Type: GLOBAL
1010 ; CHECK-NEXT: Globals:
11 ; CHECK-NEXT: - Type: I32
11 ; CHECK-NEXT: - Index: 0
12 ; CHECK-NEXT: Type: I32
1213 ; CHECK-NEXT: Mutable: false
1314 ; CHECK-NEXT: InitExpr:
1415 ; CHECK-NEXT: Opcode: I32_CONST
1516 ; CHECK-NEXT: Value: 0
16 ; CHECK-NEXT: - Type: I32
17 ; CHECK-NEXT: - Index: 1
18 ; CHECK-NEXT: Type: I32
1719 ; CHECK-NEXT: Mutable: false
1820 ; CHECK-NEXT: InitExpr:
1921 ; CHECK-NEXT: Opcode: I32_CONST
2022 ; CHECK-NEXT: Value: 4
21 ; CHECK-NEXT: - Type: I32
23 ; CHECK-NEXT: - Index: 2
24 ; CHECK-NEXT: Type: I32
2225 ; CHECK-NEXT: Mutable: false
2326 ; CHECK-NEXT: InitExpr:
2427 ; CHECK-NEXT: Opcode: I32_CONST
2528 ; CHECK-NEXT: Value: 8
26 ; CHECK-NEXT: - Type: I32
29 ; CHECK-NEXT: - Index: 3
30 ; CHECK-NEXT: Type: I32
2731 ; CHECK-NEXT: Mutable: false
2832 ; CHECK-NEXT: InitExpr:
2933 ; CHECK-NEXT: Opcode: I32_CONST
88
99 ; CHECK: - Type: GLOBAL
1010 ; CHECK-NEXT: Globals:
11 ; CHECK-NEXT: - Type: I32
11 ; CHECK-NEXT: - Index: 0
12 ; CHECK-NEXT: Type: I32
1213 ; CHECK-NEXT: Mutable: false
1314 ; CHECK-NEXT: InitExpr:
1415 ; CHECK-NEXT: Opcode: I32_CONST
1516 ; CHECK-NEXT: Value: 0
16 ; CHECK-NEXT: - Type: I32
17 ; CHECK-NEXT: - Index: 1
18 ; CHECK-NEXT: Type: I32
1719 ; CHECK-NEXT: Mutable: false
1820 ; CHECK-NEXT: InitExpr:
1921 ; CHECK-NEXT: Opcode: I32_CONST
2022 ; CHECK-NEXT: Value: 8
21 ; CHECK-NEXT: - Type: I32
23 ; CHECK-NEXT: - Index: 2
24 ; CHECK-NEXT: Type: I32
2225 ; CHECK-NEXT: Mutable: false
2326 ; CHECK-NEXT: InitExpr:
2427 ; CHECK-NEXT: Opcode: I32_CONST
2528 ; CHECK-NEXT: Value: 16
26 ; CHECK-NEXT: - Type: I32
29 ; CHECK-NEXT: - Index: 3
30 ; CHECK-NEXT: Type: I32
2731 ; CHECK-NEXT: Mutable: false
2832 ; CHECK-NEXT: InitExpr:
2933 ; CHECK-NEXT: Opcode: I32_CONST
5454 ; CHECK-NEXT: FunctionTypes: [ 0, 1, 0, 1 ]
5555 ; CHECK-NEXT: - Type: GLOBAL
5656 ; CHECK-NEXT: Globals:
57 ; CHECK-NEXT: - Type: I32
57 ; CHECK-NEXT: - Index: 1
58 ; CHECK-NEXT: Type: I32
5859 ; CHECK-NEXT: Mutable: false
5960 ; CHECK-NEXT: InitExpr:
6061 ; CHECK-NEXT: Opcode: I32_CONST
109110 ; CHECK-NEXT: Index: 1
110111 ; CHECK-NEXT: Offset: 0x00000045
111112 ; CHECK-NEXT: Functions:
112 ; CHECK-NEXT: - Locals:
113 ; CHECK-NEXT: - Index: 5
114 ; CHECK-NEXT: Locals:
113115 ; CHECK-NEXT: Body: 1080808080000B
114 ; CHECK-NEXT: - Locals:
116 ; CHECK-NEXT: - Index: 6
117 ; CHECK-NEXT: Locals:
115118 ; CHECK-NEXT: Body: 0240418080808000410041FFFFFFFF7F1081808080000D000F0B00000B
116 ; CHECK-NEXT: - Locals:
119 ; CHECK-NEXT: - Index: 7
120 ; CHECK-NEXT: Locals:
117121 ; CHECK-NEXT: Body: 1082808080000B
118 ; CHECK-NEXT: - Locals:
122 ; CHECK-NEXT: - Index: 8
123 ; CHECK-NEXT: Locals:
119124 ; CHECK-NEXT: Body: 0240418180808000410041FFFFFFFF7F1081808080000D000F0B00000B
120125 ; CHECK-NEXT: - Type: DATA
121126 ; CHECK-NEXT: Segments:
88
99 ; CHECK: - Type: GLOBAL
1010 ; CHECK-NEXT: Globals:
11 ; CHECK-NEXT: - Type: I32
11 ; CHECK-NEXT: - Index: 0
12 ; CHECK-NEXT: Type: I32
1213 ; CHECK-NEXT: Mutable: false
1314 ; CHECK-NEXT: InitExpr:
1415 ; CHECK-NEXT: Opcode: I32_CONST
1516 ; CHECK-NEXT: Value: 0
16 ; CHECK-NEXT: - Type: I32
17 ; CHECK-NEXT: - Index: 1
18 ; CHECK-NEXT: Type: I32
1719 ; CHECK-NEXT: Mutable: false
1820 ; CHECK-NEXT: InitExpr:
1921 ; CHECK-NEXT: Opcode: I32_CONST
2022 ; CHECK-NEXT: Value: 6
21 ; CHECK-NEXT: - Type: I32
23 ; CHECK-NEXT: - Index: 2
24 ; CHECK-NEXT: Type: I32
2225 ; CHECK-NEXT: Mutable: false
2326 ; CHECK-NEXT: InitExpr:
2427 ; CHECK-NEXT: Opcode: I32_CONST
2528 ; CHECK-NEXT: Value: 16
26 ; CHECK-NEXT: - Type: I32
29 ; CHECK-NEXT: - Index: 3
30 ; CHECK-NEXT: Type: I32
2731 ; CHECK-NEXT: Mutable: false
2832 ; CHECK-NEXT: InitExpr:
2933 ; CHECK-NEXT: Opcode: I32_CONST
7575 ; CHECK-NEXT: FunctionTypes: [ 0, 0, 0, 0, 0 ]
7676 ; CHECK-NEXT: - Type: GLOBAL
7777 ; CHECK-NEXT: Globals:
78 ; CHECK-NEXT: - Type: I32
78 ; CHECK-NEXT: - Index: 1
79 ; CHECK-NEXT: Type: I32
7980 ; CHECK-NEXT: Mutable: false
8081 ; CHECK-NEXT: InitExpr:
8182 ; CHECK-NEXT: Opcode: I32_CONST
8283 ; CHECK-NEXT: Value: 8
83 ; CHECK-NEXT: - Type: I32
84 ; CHECK-NEXT: - Index: 2
85 ; CHECK-NEXT: Type: I32
8486 ; CHECK-NEXT: Mutable: false
8587 ; CHECK-NEXT: InitExpr:
8688 ; CHECK-NEXT: Opcode: I32_CONST
8789 ; CHECK-NEXT: Value: 16
88 ; CHECK-NEXT: - Type: I32
90 ; CHECK-NEXT: - Index: 3
91 ; CHECK-NEXT: Type: I32
8992 ; CHECK-NEXT: Mutable: false
9093 ; CHECK-NEXT: InitExpr:
9194 ; CHECK-NEXT: Opcode: I32_CONST
149152 ; CHECK-NEXT: Index: 0
150153 ; CHECK-NEXT: Offset: 0x00000037
151154 ; CHECK-NEXT: Functions:
152 ; CHECK-NEXT: - Locals:
155 ; CHECK-NEXT: - Index: 1
156 ; CHECK-NEXT: Locals:
153157 ; CHECK-NEXT: Body: 41000B
154 ; CHECK-NEXT: - Locals:
158 ; CHECK-NEXT: - Index: 2
159 ; CHECK-NEXT: Locals:
155160 ; CHECK-NEXT: Body: 1081808080000B
156 ; CHECK-NEXT: - Locals:
161 ; CHECK-NEXT: - Index: 3
162 ; CHECK-NEXT: Locals:
157163 ; CHECK-NEXT: Body: 1080808080000B
158 ; CHECK-NEXT: - Locals:
164 ; CHECK-NEXT: - Index: 4
165 ; CHECK-NEXT: Locals:
159166 ; CHECK-NEXT: Body: 410028028880808000118080808000000B
160 ; CHECK-NEXT: - Locals:
167 ; CHECK-NEXT: - Index: 5
168 ; CHECK-NEXT: Locals:
161169 ; CHECK-NEXT: Body: 410028029080808000118080808000000B
162170 ; CHECK-NEXT: - Type: DATA
163171 ; CHECK-NEXT: Relocations:
44 Sections:
55 - Type: TYPE
66 Signatures:
7 - ReturnType: F32
7 - Index: 0
8 ReturnType: F32
89 ParamTypes:
910 - I32
10 - ReturnType: NORESULT
11 - Index: 1
12 ReturnType: NORESULT
1113 ParamTypes:
1214 - I32
1315 - I64
2426 Index: 1
2527 Offset: 0x00000025
2628 Functions:
27 - Locals:
29 - Index: 0
30 Locals:
2831 - Type: I32
2932 Count: 3
3033 Body: 418080808000210020002101200111808080800000210220020F0B
31 - Locals:
34 - Index: 1
35 Locals:
3236 - Type: I32
3337 Count: 1
3438 Body: 108180808000210020000F0B
5761 # CHECK: Index: 1
5862 # CHECK: Offset: 0x00000025
5963 # CHECK: Functions:
60 # CHECK: - Locals:
64 # CHECK: - Index: 0
65 # CHECK: Locals:
6166 # CHECK: - Type: I32
6267 # CHECK: Count: 3
6368 # CHECK: Body: 418080808000210020002101200111808080800000210220020F0B
64 # CHECK: - Locals:
69 # CHECK: - Index: 1
70 # CHECK: Locals:
6571 # CHECK: - Type: I32
6672 # CHECK: Count: 1
6773 # CHECK: Body: 108180808000210020000F0B
66 FunctionTypes: [ 0, 0 ]
77 - Type: GLOBAL
88 Globals:
9 - Type: I32
9 - Index: 0
10 Type: I32
1011 Mutable: false
1112 InitExpr:
1213 Opcode: I64_CONST
1314 Value: 32
14 - Type: I32
15 - Index: 1
16 Type: I32
1517 Mutable: false
1618 InitExpr:
1719 Opcode: I64_CONST
44 Sections:
55 - Type: GLOBAL
66 Globals:
7 - Type: I32
7 - Index: 0
8 Type: I32
89 Mutable: false
910 InitExpr:
1011 Opcode: I64_CONST
1617 # CHECK: Sections:
1718 # CHECK: - Type: GLOBAL
1819 # CHECK: Globals:
19 # CHECK: - Type: I32
20 # CHECK: - Index: 0
21 # CHECK: Type: I32
2022 # CHECK: Mutable: false
2123 # CHECK: InitExpr:
2224 # CHECK: Opcode: I64_CONST
44 Sections:
55 - Type: TYPE
66 Signatures:
7 - ReturnType: I32
7 - Index: 0
8 ReturnType: I32
89 ParamTypes:
910 - I32
1011 - Type: IMPORT
44 Sections:
55 - Type: TYPE
66 Signatures:
7 - ReturnType: I32
7 - Index: 0
8 ReturnType: I32
89 ParamTypes:
910 - I32
1011 - Type: IMPORT
44 Sections:
55 - Type: TYPE
66 Signatures:
7 - ReturnType: I32
7 - Index: 0
8 ReturnType: I32
89 ParamTypes:
910 - I32
1011 - Type: IMPORT
55 Sections:
66 - Type: TYPE
77 Signatures:
8 - ReturnType: I32
8 - Index: 0
9 ReturnType: I32
910 ParamTypes:
1011 - F32
1112 - F32
44 Sections:
55 - Type: TYPE
66 Signatures:
7 - ReturnType: I32
7 - Index: 0
8 ReturnType: I32
89 ParamTypes:
910 - F32
1011 - F32
11 - ReturnType: I64
12 - Index: 1
13 ReturnType: I64
1214 ParamTypes:
1315 - F64
1416 - F64
44 Sections:
55 - Type: TYPE
66 Signatures:
7 - ReturnType: I32
7 - Index: 0
8 ReturnType: I32
89 ParamTypes:
910 - Type: FUNCTION
1011 FunctionTypes: [ 0, 0 ]
1112 - Type: GLOBAL
1213 Globals:
13 - Type: I32
14 - Index: 0
15 Type: I32
1416 Mutable: false
1517 InitExpr:
1618 Opcode: I32_CONST
88 Sections:
99 - Type: TYPE
1010 Signatures:
11 - ReturnType: I32
11 - Index: 0
12 ReturnType: I32
1213 ParamTypes:
1314 - I32
14 - Type: FUNCTION
15 FunctionTypes: [ 0, 0, 0, 0, 0 ]
16 - Type: GLOBAL
17 Globals:
18 - Type: I32
19 Mutable: false
20 InitExpr:
21 Opcode: I64_CONST
22 Value: 32
23 - Type: I32
24 Mutable: false
25 InitExpr:
26 Opcode: I32_CONST
27 Value: 64
28 - Type: I32
29 Mutable: false
30 InitExpr:
31 Opcode: I32_CONST
32 Value: 1024
3315 - Type: IMPORT
3416 Imports:
3517 - Module: env
4123 Kind: GLOBAL
4224 GlobalType: I32
4325 GlobalMutable: false
26 - Type: FUNCTION
27 FunctionTypes: [ 0, 0, 0, 0, 0 ]
28 - Type: GLOBAL
29 Globals:
30 - Index: 1
31 Type: I32
32 Mutable: false
33 InitExpr:
34 Opcode: I64_CONST
35 Value: 32
36 - Index: 2
37 Type: I32
38 Mutable: false
39 InitExpr:
40 Opcode: I32_CONST
41 Value: 64
42 - Index: 3
43 Type: I32
44 Mutable: false
45 InitExpr:
46 Opcode: I32_CONST
47 Value: 1024
4448 - Type: EXPORT
4549 Exports:
4650 - Name: foo
55 Sections:
66 - Type: TYPE
77 Signatures:
8 - ReturnType: I32
8 - Index: 0
9 ReturnType: I32
910 ParamTypes:
1011 - I32
1112 - Type: IMPORT
88 Sections:
99 - Type: TYPE
1010 Signatures:
11 - ReturnType: I32
11 - Index: 0
12 ReturnType: I32
1213 ParamTypes:
1314 - I32
14 - Type: FUNCTION
15 FunctionTypes: [ 0, 0, 0, 0 ]
1615 - Type: IMPORT
1716 Imports:
1817 - Module: env
2423 Kind: GLOBAL
2524 GlobalType: I32
2625 GlobalMutable: false
26 - Type: FUNCTION
27 FunctionTypes: [ 0, 0, 0, 0 ]
2728 - Type: GLOBAL
2829 Globals:
29 - Type: I32
30 - Index: 1
31 Type: I32
3032 Mutable: false
3133 InitExpr:
3234 Opcode: I64_CONST
3335 Value: 32
34 - Type: I32
36 - Index: 2
37 Type: I32
3538 Mutable: false
3639 InitExpr:
3740 Opcode: I32_CONST
3841 Value: 64
39 - Type: I32
42 - Index: 3
43 Type: I32
4044 Mutable: false
4145 InitExpr:
4246 Opcode: I32_CONST
185185 auto GlobalSec = make_unique();
186186 for (auto &Global : Obj.globals()) {
187187 WasmYAML::Global G;
188 G.Index = Global.Index;
188189 G.Type = Global.Type;
189190 G.Mutable = Global.Mutable;
190191 G.InitExpr = Global.InitExpr;
229230 auto CodeSec = make_unique();
230231 for (auto &Func : Obj.functions()) {
231232 WasmYAML::Function Function;
233 Function.Index = Func.Index;
232234 for (auto &Local : Func.Locals) {
233235 WasmYAML::LocalDecl LocalDecl;
234236 LocalDecl.Type = Local.Type;
4444 int writeSectionContent(raw_ostream &OS, WasmYAML::NameSection &Section);
4545 int writeSectionContent(raw_ostream &OS, WasmYAML::LinkingSection &Section);
4646 WasmYAML::Object &Obj;
47 uint32_t NumImportedFunctions = 0;
48 uint32_t NumImportedGlobals = 0;
4749 };
4850
4951 static int writeUint64(raw_ostream &OS, uint64_t Value) {
100102 encodeULEB128(InitExpr.Value.Global, OS);
101103 break;
102104 default:
103 errs() << "Unknown opcode in init_expr: " << InitExpr.Opcode;
105 errs() << "Unknown opcode in init_expr: " << InitExpr.Opcode << "\n";
104106 return 1;
105107 }
106108 writeUint8(OS, wasm::WASM_OPCODE_END);
210212 int WasmWriter::writeSectionContent(raw_ostream &OS,
211213 WasmYAML::TypeSection &Section) {
212214 encodeULEB128(Section.Signatures.size(), OS);
215 uint32_t ExpectedIndex = 0;
213216 for (const WasmYAML::Signature &Sig : Section.Signatures) {
217 if (Sig.Index != ExpectedIndex) {
218 errs() << "Unexpected type index: " << Sig.Index << "\n";
219 return 1;
220 }
221 ++ExpectedIndex;
214222 encodeSLEB128(Sig.Form, OS);
215223 encodeULEB128(Sig.ParamTypes.size(), OS);
216224 for (auto ParamType : Sig.ParamTypes)
235243 switch (Import.Kind) {
236244 case wasm::WASM_EXTERNAL_FUNCTION:
237245 encodeULEB128(Import.SigIndex, OS);
246 NumImportedFunctions++;
238247 break;
239248 case wasm::WASM_EXTERNAL_GLOBAL:
240249 encodeSLEB128(Import.GlobalImport.Type, OS);
241250 writeUint8(OS, Import.GlobalImport.Mutable);
251 NumImportedGlobals++;
242252 break;
243253 case wasm::WASM_EXTERNAL_MEMORY:
244254 writeLimits(Import.Memory, OS);
248258 writeLimits(Import.TableImport.TableLimits, OS);
249259 break;
250260 default:
251 errs() << "Unknown import type: " << Import.Kind;
261 errs() << "Unknown import type: " << Import.Kind << "\n";
252262 return 1;
253263 }
254264 }
303313 int WasmWriter::writeSectionContent(raw_ostream &OS,
304314 WasmYAML::GlobalSection &Section) {
305315 encodeULEB128(Section.Globals.size(), OS);
316 uint32_t ExpectedIndex = NumImportedGlobals;
306317 for (auto &Global : Section.Globals) {
318 if (Global.Index != ExpectedIndex) {
319 errs() << "Unexpected global index: " << Global.Index << "\n";
320 return 1;
321 }
322 ++ExpectedIndex;
307323 encodeSLEB128(Global.Type, OS);
308324 writeUint8(OS, Global.Mutable);
309325 writeInitExpr(Global.InitExpr, OS);
329345 int WasmWriter::writeSectionContent(raw_ostream &OS,
330346 WasmYAML::CodeSection &Section) {
331347 encodeULEB128(Section.Functions.size(), OS);
348 uint32_t ExpectedIndex = NumImportedFunctions;
332349 for (auto &Func : Section.Functions) {
333350 std::string OutString;
334351 raw_string_ostream StringStream(OutString);
352 if (Func.Index != ExpectedIndex) {
353 errs() << "Unexpected function index: " << Func.Index << "\n";
354 return 1;
355 }
356 ++ExpectedIndex;
335357
336358 encodeULEB128(Func.Locals.size(), StringStream);
337359 for (auto &LocalDecl : Func.Locals) {
401423 writeUint32(OS, Obj.Header.Version);
402424
403425 // Write each section
426 uint32_t LastType = 0;
404427 for (const std::unique_ptr &Sec : Obj.Sections) {
428 uint32_t Type = Sec->Type;
429 if (Type != wasm::WASM_SEC_CUSTOM) {
430 if (Type < LastType) {
431 errs() << "Out of order section type: " << Type << "\n";
432 return 1;
433 }
434 LastType = Type;
435 }
436
405437 encodeULEB128(Sec->Type, OS);
406
407438 std::string OutString;
408439 raw_string_ostream StringStream(OutString);
409440 if (auto S = dyn_cast(Sec.get())) {