LLVM 23.0.0git
WasmEmitter.cpp
Go to the documentation of this file.
1//===- yaml2wasm - Convert YAML to a Wasm object file --------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8///
9/// \file
10/// The Wasm component of yaml2obj.
11///
12//===----------------------------------------------------------------------===//
13//
14
15#include "llvm/Object/Wasm.h"
18#include "llvm/Support/Endian.h"
19#include "llvm/Support/LEB128.h"
20
21using namespace llvm;
22
23namespace {
24/// This parses a yaml stream that represents a Wasm object file.
25/// See docs/yaml2obj for the yaml scheema.
26class WasmWriter {
27public:
28 WasmWriter(WasmYAML::Object &Obj, yaml::ErrorHandler EH)
29 : Obj(Obj), ErrHandler(EH) {}
30 bool writeWasm(raw_ostream &OS);
31
32private:
33 void writeRelocSection(raw_ostream &OS, WasmYAML::Section &Sec,
34 uint32_t SectionIndex);
35
36 void writeInitExpr(raw_ostream &OS, const WasmYAML::InitExpr &InitExpr);
37
38 void writeSectionContent(raw_ostream &OS, WasmYAML::CustomSection &Section);
39 void writeSectionContent(raw_ostream &OS, WasmYAML::TypeSection &Section);
40 void writeSectionContent(raw_ostream &OS, WasmYAML::ImportSection &Section);
41 void writeSectionContent(raw_ostream &OS, WasmYAML::FunctionSection &Section);
42 void writeSectionContent(raw_ostream &OS, WasmYAML::TableSection &Section);
43 void writeSectionContent(raw_ostream &OS, WasmYAML::MemorySection &Section);
44 void writeSectionContent(raw_ostream &OS, WasmYAML::TagSection &Section);
45 void writeSectionContent(raw_ostream &OS, WasmYAML::GlobalSection &Section);
46 void writeSectionContent(raw_ostream &OS, WasmYAML::ExportSection &Section);
47 void writeSectionContent(raw_ostream &OS, WasmYAML::StartSection &Section);
48 void writeSectionContent(raw_ostream &OS, WasmYAML::ElemSection &Section);
49 void writeSectionContent(raw_ostream &OS, WasmYAML::CodeSection &Section);
50 void writeSectionContent(raw_ostream &OS, WasmYAML::DataSection &Section);
51 void writeSectionContent(raw_ostream &OS, WasmYAML::DataCountSection &Section);
52
53 // Custom section types
54 void writeSectionContent(raw_ostream &OS, WasmYAML::DylinkSection &Section);
55 void writeSectionContent(raw_ostream &OS, WasmYAML::NameSection &Section);
56 void writeSectionContent(raw_ostream &OS, WasmYAML::LinkingSection &Section);
57 void writeSectionContent(raw_ostream &OS, WasmYAML::ProducersSection &Section);
58 void writeSectionContent(raw_ostream &OS,
59 WasmYAML::TargetFeaturesSection &Section);
60 WasmYAML::Object &Obj;
61 uint32_t NumImportedFunctions = 0;
62 uint32_t NumImportedGlobals = 0;
63 uint32_t NumImportedTables = 0;
64 uint32_t NumImportedTags = 0;
65
66 bool HasError = false;
67 yaml::ErrorHandler ErrHandler;
68 void reportError(const Twine &Msg);
69};
70
71class SubSectionWriter {
72 raw_ostream &OS;
73 std::string OutString;
74 raw_string_ostream StringStream;
75
76public:
77 SubSectionWriter(raw_ostream &OS) : OS(OS), StringStream(OutString) {}
78
79 void done() {
80 encodeULEB128(OutString.size(), OS);
81 OS << OutString;
82 OutString.clear();
83 }
84
85 raw_ostream &getStream() { return StringStream; }
86};
87
88} // end anonymous namespace
89
91 char Data[sizeof(Value)];
93 OS.write(Data, sizeof(Data));
94 return 0;
95}
96
98 char Data[sizeof(Value)];
100 OS.write(Data, sizeof(Data));
101 return 0;
102}
103
105 char Data[sizeof(Value)];
106 memcpy(Data, &Value, sizeof(Data));
107 OS.write(Data, sizeof(Data));
108 return 0;
109}
110
111static int writeStringRef(const StringRef &Str, raw_ostream &OS) {
112 encodeULEB128(Str.size(), OS);
113 OS << Str;
114 return 0;
115}
116
117static int writeLimits(const WasmYAML::Limits &Lim, raw_ostream &OS) {
118 writeUint8(OS, Lim.Flags);
119 encodeULEB128(Lim.Minimum, OS);
121 encodeULEB128(Lim.Maximum, OS);
122 return 0;
123}
124
125void WasmWriter::reportError(const Twine &Msg) {
126 ErrHandler(Msg);
127 HasError = true;
128}
129
130void WasmWriter::writeInitExpr(raw_ostream &OS,
131 const WasmYAML::InitExpr &InitExpr) {
132 if (InitExpr.Extended) {
133 InitExpr.Body.writeAsBinary(OS);
134 } else {
135 writeUint8(OS, InitExpr.Inst.Opcode);
136 switch (InitExpr.Inst.Opcode) {
138 encodeSLEB128(InitExpr.Inst.Value.Int32, OS);
139 break;
141 encodeSLEB128(InitExpr.Inst.Value.Int64, OS);
142 break;
144 writeUint32(OS, InitExpr.Inst.Value.Float32);
145 break;
147 writeUint64(OS, InitExpr.Inst.Value.Float64);
148 break;
150 encodeULEB128(InitExpr.Inst.Value.Global, OS);
151 break;
152 default:
153 reportError("unknown opcode in init_expr: " +
154 Twine(InitExpr.Inst.Opcode));
155 return;
156 }
158 }
159}
160
161void WasmWriter::writeSectionContent(raw_ostream &OS,
162 WasmYAML::DylinkSection &Section) {
163 writeStringRef(Section.Name, OS);
164
166 SubSectionWriter SubSection(OS);
167 raw_ostream &SubOS = SubSection.getStream();
168 encodeULEB128(Section.MemorySize, SubOS);
169 encodeULEB128(Section.MemoryAlignment, SubOS);
170 encodeULEB128(Section.TableSize, SubOS);
171 encodeULEB128(Section.TableAlignment, SubOS);
172 SubSection.done();
173
174 if (Section.Needed.size()) {
176 raw_ostream &SubOS = SubSection.getStream();
177 encodeULEB128(Section.Needed.size(), SubOS);
178 for (StringRef Needed : Section.Needed)
179 writeStringRef(Needed, SubOS);
180 SubSection.done();
181 }
182 if (Section.RuntimePath.size()) {
184 raw_ostream &SubOS = SubSection.getStream();
185 encodeULEB128(Section.RuntimePath.size(), SubOS);
186 for (StringRef Path : Section.RuntimePath)
187 writeStringRef(Path, SubOS);
188 SubSection.done();
189 }
190}
191
192void WasmWriter::writeSectionContent(raw_ostream &OS,
193 WasmYAML::LinkingSection &Section) {
194 writeStringRef(Section.Name, OS);
195 encodeULEB128(Section.Version, OS);
196
197 SubSectionWriter SubSection(OS);
198
199 // SYMBOL_TABLE subsection
200 if (Section.SymbolTable.size()) {
202 encodeULEB128(Section.SymbolTable.size(), SubSection.getStream());
203 for (auto Sym : llvm::enumerate(Section.SymbolTable)) {
204 const WasmYAML::SymbolInfo &Info = Sym.value();
205 assert(Info.Index == Sym.index());
206 writeUint8(SubSection.getStream(), Info.Kind);
207 encodeULEB128(Info.Flags, SubSection.getStream());
208 switch (Info.Kind) {
213 encodeULEB128(Info.ElementIndex, SubSection.getStream());
214 if ((Info.Flags & wasm::WASM_SYMBOL_UNDEFINED) == 0 ||
216 writeStringRef(Info.Name, SubSection.getStream());
217 break;
219 writeStringRef(Info.Name, SubSection.getStream());
220 if ((Info.Flags & wasm::WASM_SYMBOL_UNDEFINED) == 0) {
221 encodeULEB128(Info.DataRef.Segment, SubSection.getStream());
222 encodeULEB128(Info.DataRef.Offset, SubSection.getStream());
223 encodeULEB128(Info.DataRef.Size, SubSection.getStream());
224 }
225 break;
227 encodeULEB128(Info.ElementIndex, SubSection.getStream());
228 break;
229 default:
230 llvm_unreachable("unexpected kind");
231 }
232 }
233
234 SubSection.done();
235 }
236
237 // SEGMENT_NAMES subsection
238 if (Section.SegmentInfos.size()) {
240 encodeULEB128(Section.SegmentInfos.size(), SubSection.getStream());
241 for (const WasmYAML::SegmentInfo &SegmentInfo : Section.SegmentInfos) {
242 writeStringRef(SegmentInfo.Name, SubSection.getStream());
243 encodeULEB128(SegmentInfo.Alignment, SubSection.getStream());
244 encodeULEB128(SegmentInfo.Flags, SubSection.getStream());
245 }
246 SubSection.done();
247 }
248
249 // INIT_FUNCS subsection
250 if (Section.InitFunctions.size()) {
252 encodeULEB128(Section.InitFunctions.size(), SubSection.getStream());
253 for (const WasmYAML::InitFunction &Func : Section.InitFunctions) {
254 encodeULEB128(Func.Priority, SubSection.getStream());
255 encodeULEB128(Func.Symbol, SubSection.getStream());
256 }
257 SubSection.done();
258 }
259
260 // COMDAT_INFO subsection
261 if (Section.Comdats.size()) {
263 encodeULEB128(Section.Comdats.size(), SubSection.getStream());
264 for (const auto &C : Section.Comdats) {
265 writeStringRef(C.Name, SubSection.getStream());
266 encodeULEB128(0, SubSection.getStream()); // flags for future use
267 encodeULEB128(C.Entries.size(), SubSection.getStream());
268 for (const WasmYAML::ComdatEntry &Entry : C.Entries) {
269 writeUint8(SubSection.getStream(), Entry.Kind);
270 encodeULEB128(Entry.Index, SubSection.getStream());
271 }
272 }
273 SubSection.done();
274 }
275}
276
277void WasmWriter::writeSectionContent(raw_ostream &OS,
278 WasmYAML::NameSection &Section) {
279 writeStringRef(Section.Name, OS);
280 if (Section.FunctionNames.size()) {
282
283 SubSectionWriter SubSection(OS);
284
285 encodeULEB128(Section.FunctionNames.size(), SubSection.getStream());
286 for (const WasmYAML::NameEntry &NameEntry : Section.FunctionNames) {
287 encodeULEB128(NameEntry.Index, SubSection.getStream());
288 writeStringRef(NameEntry.Name, SubSection.getStream());
289 }
290
291 SubSection.done();
292 }
293 if (Section.GlobalNames.size()) {
295
296 SubSectionWriter SubSection(OS);
297
298 encodeULEB128(Section.GlobalNames.size(), SubSection.getStream());
299 for (const WasmYAML::NameEntry &NameEntry : Section.GlobalNames) {
300 encodeULEB128(NameEntry.Index, SubSection.getStream());
301 writeStringRef(NameEntry.Name, SubSection.getStream());
302 }
303
304 SubSection.done();
305 }
306 if (Section.DataSegmentNames.size()) {
308
309 SubSectionWriter SubSection(OS);
310
311 encodeULEB128(Section.DataSegmentNames.size(), SubSection.getStream());
312 for (const WasmYAML::NameEntry &NameEntry : Section.DataSegmentNames) {
313 encodeULEB128(NameEntry.Index, SubSection.getStream());
314 writeStringRef(NameEntry.Name, SubSection.getStream());
315 }
316
317 SubSection.done();
318 }
319}
320
321void WasmWriter::writeSectionContent(raw_ostream &OS,
322 WasmYAML::ProducersSection &Section) {
323 writeStringRef(Section.Name, OS);
324 int Fields = int(!Section.Languages.empty()) + int(!Section.Tools.empty()) +
325 int(!Section.SDKs.empty());
326 if (Fields == 0)
327 return;
328 encodeULEB128(Fields, OS);
329 for (auto &Field : {std::make_pair(StringRef("language"), &Section.Languages),
330 std::make_pair(StringRef("processed-by"), &Section.Tools),
331 std::make_pair(StringRef("sdk"), &Section.SDKs)}) {
332 if (Field.second->empty())
333 continue;
334 writeStringRef(Field.first, OS);
335 encodeULEB128(Field.second->size(), OS);
336 for (auto &Entry : *Field.second) {
337 writeStringRef(Entry.Name, OS);
338 writeStringRef(Entry.Version, OS);
339 }
340 }
341}
342
343void WasmWriter::writeSectionContent(raw_ostream &OS,
344 WasmYAML::TargetFeaturesSection &Section) {
345 writeStringRef(Section.Name, OS);
346 encodeULEB128(Section.Features.size(), OS);
347 for (auto &E : Section.Features) {
348 writeUint8(OS, E.Prefix);
349 writeStringRef(E.Name, OS);
350 }
351}
352
353void WasmWriter::writeSectionContent(raw_ostream &OS,
354 WasmYAML::CustomSection &Section) {
355 if (auto S = dyn_cast<WasmYAML::DylinkSection>(&Section)) {
356 writeSectionContent(OS, *S);
357 } else if (auto S = dyn_cast<WasmYAML::NameSection>(&Section)) {
358 writeSectionContent(OS, *S);
359 } else if (auto S = dyn_cast<WasmYAML::LinkingSection>(&Section)) {
360 writeSectionContent(OS, *S);
361 } else if (auto S = dyn_cast<WasmYAML::ProducersSection>(&Section)) {
362 writeSectionContent(OS, *S);
363 } else if (auto S = dyn_cast<WasmYAML::TargetFeaturesSection>(&Section)) {
364 writeSectionContent(OS, *S);
365 } else {
366 writeStringRef(Section.Name, OS);
367 Section.Payload.writeAsBinary(OS);
368 }
369}
370
371void WasmWriter::writeSectionContent(raw_ostream &OS,
372 WasmYAML::TypeSection &Section) {
373 encodeULEB128(Section.Signatures.size(), OS);
374 uint32_t ExpectedIndex = 0;
375 for (const WasmYAML::Signature &Sig : Section.Signatures) {
376 if (Sig.Index != ExpectedIndex) {
377 reportError("unexpected type index: " + Twine(Sig.Index));
378 return;
379 }
380 ++ExpectedIndex;
381 writeUint8(OS, Sig.Form);
382 encodeULEB128(Sig.ParamTypes.size(), OS);
383 for (auto ParamType : Sig.ParamTypes)
384 writeUint8(OS, ParamType);
385 encodeULEB128(Sig.ReturnTypes.size(), OS);
386 for (auto ReturnType : Sig.ReturnTypes)
387 writeUint8(OS, ReturnType);
388 }
389}
390
391void WasmWriter::writeSectionContent(raw_ostream &OS,
392 WasmYAML::ImportSection &Section) {
393 encodeULEB128(Section.Imports.size(), OS);
394 for (const WasmYAML::Import &Import : Section.Imports) {
395 writeStringRef(Import.Module, OS);
396 writeStringRef(Import.Field, OS);
397 writeUint8(OS, Import.Kind);
398 switch (Import.Kind) {
400 encodeULEB128(Import.SigIndex, OS);
401 NumImportedFunctions++;
402 break;
404 writeUint8(OS, Import.GlobalImport.Type);
405 writeUint8(OS, Import.GlobalImport.Mutable);
406 NumImportedGlobals++;
407 break;
409 writeUint8(OS, 0); // Reserved 'attribute' field
410 encodeULEB128(Import.SigIndex, OS);
411 NumImportedTags++;
412 break;
414 writeLimits(Import.Memory, OS);
415 break;
417 writeUint8(OS, Import.TableImport.ElemType);
418 writeLimits(Import.TableImport.TableLimits, OS);
419 NumImportedTables++;
420 break;
421 default:
422 reportError("unknown import type: " +Twine(Import.Kind));
423 return;
424 }
425 }
426}
427
428void WasmWriter::writeSectionContent(raw_ostream &OS,
429 WasmYAML::FunctionSection &Section) {
430 encodeULEB128(Section.FunctionTypes.size(), OS);
431 for (uint32_t FuncType : Section.FunctionTypes)
432 encodeULEB128(FuncType, OS);
433}
434
435void WasmWriter::writeSectionContent(raw_ostream &OS,
436 WasmYAML::ExportSection &Section) {
437 encodeULEB128(Section.Exports.size(), OS);
438 for (const WasmYAML::Export &Export : Section.Exports) {
439 writeStringRef(Export.Name, OS);
440 writeUint8(OS, Export.Kind);
441 encodeULEB128(Export.Index, OS);
442 }
443}
444
445void WasmWriter::writeSectionContent(raw_ostream &OS,
446 WasmYAML::StartSection &Section) {
447 encodeULEB128(Section.StartFunction, OS);
448}
449
450void WasmWriter::writeSectionContent(raw_ostream &OS,
451 WasmYAML::TableSection &Section) {
452 encodeULEB128(Section.Tables.size(), OS);
453 uint32_t ExpectedIndex = NumImportedTables;
454 for (auto &Table : Section.Tables) {
455 if (Table.Index != ExpectedIndex) {
456 reportError("unexpected table index: " + Twine(Table.Index));
457 return;
458 }
459 ++ExpectedIndex;
460 writeUint8(OS, Table.ElemType);
461 writeLimits(Table.TableLimits, OS);
462 }
463}
464
465void WasmWriter::writeSectionContent(raw_ostream &OS,
466 WasmYAML::MemorySection &Section) {
467 encodeULEB128(Section.Memories.size(), OS);
468 for (const WasmYAML::Limits &Mem : Section.Memories)
469 writeLimits(Mem, OS);
470}
471
472void WasmWriter::writeSectionContent(raw_ostream &OS,
473 WasmYAML::TagSection &Section) {
474 encodeULEB128(Section.TagTypes.size(), OS);
475 for (uint32_t TagType : Section.TagTypes) {
476 writeUint8(OS, 0); // Reserved 'attribute' field
477 encodeULEB128(TagType, OS);
478 }
479}
480
481void WasmWriter::writeSectionContent(raw_ostream &OS,
482 WasmYAML::GlobalSection &Section) {
483 encodeULEB128(Section.Globals.size(), OS);
484 uint32_t ExpectedIndex = NumImportedGlobals;
485 for (auto &Global : Section.Globals) {
486 if (Global.Index != ExpectedIndex) {
487 reportError("unexpected global index: " + Twine(Global.Index));
488 return;
489 }
490 ++ExpectedIndex;
491 writeUint8(OS, Global.Type);
492 writeUint8(OS, Global.Mutable);
493 writeInitExpr(OS, Global.Init);
494 }
495}
496
497void WasmWriter::writeSectionContent(raw_ostream &OS,
498 WasmYAML::ElemSection &Section) {
499 encodeULEB128(Section.Segments.size(), OS);
500 for (auto &Segment : Section.Segments) {
501 encodeULEB128(Segment.Flags, OS);
503 encodeULEB128(Segment.TableNumber, OS);
504
505 writeInitExpr(OS, Segment.Offset);
506
507 if (Segment.Flags & wasm::WASM_ELEM_SEGMENT_MASK_HAS_ELEM_DESC) {
508 // We only support active function table initializers, for which the elem
509 // kind is specified to be written as 0x00 and interpreted to mean
510 // "funcref".
511 if (Segment.ElemKind != uint32_t(wasm::ValType::FUNCREF)) {
512 reportError("unexpected elemkind: " + Twine(Segment.ElemKind));
513 return;
514 }
515 const uint8_t ElemKind = 0;
516 writeUint8(OS, ElemKind);
517 }
518
519 encodeULEB128(Segment.Functions.size(), OS);
520 for (auto &Function : Segment.Functions)
521 encodeULEB128(Function, OS);
522 }
523}
524
525void WasmWriter::writeSectionContent(raw_ostream &OS,
526 WasmYAML::CodeSection &Section) {
527 encodeULEB128(Section.Functions.size(), OS);
528 uint32_t ExpectedIndex = NumImportedFunctions;
529 for (auto &Func : Section.Functions) {
530 std::string OutString;
531 raw_string_ostream StringStream(OutString);
532 if (Func.Index != ExpectedIndex) {
533 reportError("unexpected function index: " + Twine(Func.Index));
534 return;
535 }
536 ++ExpectedIndex;
537
538 encodeULEB128(Func.Locals.size(), StringStream);
539 for (auto &LocalDecl : Func.Locals) {
540 encodeULEB128(LocalDecl.Count, StringStream);
541 writeUint8(StringStream, LocalDecl.Type);
542 }
543
544 Func.Body.writeAsBinary(StringStream);
545
546 // Write the section size followed by the content
547 encodeULEB128(OutString.size(), OS);
548 OS << OutString;
549 }
550}
551
552void WasmWriter::writeSectionContent(raw_ostream &OS,
553 WasmYAML::DataSection &Section) {
554 encodeULEB128(Section.Segments.size(), OS);
555 for (auto &Segment : Section.Segments) {
556 encodeULEB128(Segment.InitFlags, OS);
557 if (Segment.InitFlags & wasm::WASM_DATA_SEGMENT_HAS_MEMINDEX)
558 encodeULEB128(Segment.MemoryIndex, OS);
559 if ((Segment.InitFlags & wasm::WASM_DATA_SEGMENT_IS_PASSIVE) == 0)
560 writeInitExpr(OS, Segment.Offset);
561 encodeULEB128(Segment.Content.binary_size(), OS);
562 Segment.Content.writeAsBinary(OS);
563 }
564}
565
566void WasmWriter::writeSectionContent(raw_ostream &OS,
567 WasmYAML::DataCountSection &Section) {
568 encodeULEB128(Section.Count, OS);
569}
570
571void WasmWriter::writeRelocSection(raw_ostream &OS, WasmYAML::Section &Sec,
572 uint32_t SectionIndex) {
573 switch (Sec.Type) {
575 writeStringRef("reloc.CODE", OS);
576 break;
578 writeStringRef("reloc.DATA", OS);
579 break;
581 auto *CustomSection = cast<WasmYAML::CustomSection>(&Sec);
582 writeStringRef(("reloc." + CustomSection->Name).str(), OS);
583 break;
584 }
585 default:
586 llvm_unreachable("not yet implemented");
587 }
588
589 encodeULEB128(SectionIndex, OS);
590 encodeULEB128(Sec.Relocations.size(), OS);
591
592 for (auto Reloc : Sec.Relocations) {
593 writeUint8(OS, Reloc.Type);
594 encodeULEB128(Reloc.Offset, OS);
595 encodeULEB128(Reloc.Index, OS);
596 if (wasm::relocTypeHasAddend(Reloc.Type))
597 encodeSLEB128(Reloc.Addend, OS);
598 }
599}
600
601bool WasmWriter::writeWasm(raw_ostream &OS) {
602 // Write headers
604 writeUint32(OS, Obj.Header.Version);
605
606 // Write each section
607 llvm::object::WasmSectionOrderChecker Checker;
608 for (const std::unique_ptr<WasmYAML::Section> &Sec : Obj.Sections) {
609 StringRef SecName = "";
610 if (auto S = dyn_cast<WasmYAML::CustomSection>(Sec.get()))
611 SecName = S->Name;
612 if (!Checker.isValidSectionOrder(Sec->Type, SecName)) {
613 reportError("out of order section type: " +
614 wasm::sectionTypeToString(Sec->Type));
615 return false;
616 }
617 encodeULEB128(Sec->Type, OS);
618 std::string OutString;
619 raw_string_ostream StringStream(OutString);
620 if (auto S = dyn_cast<WasmYAML::CustomSection>(Sec.get()))
621 writeSectionContent(StringStream, *S);
622 else if (auto S = dyn_cast<WasmYAML::TypeSection>(Sec.get()))
623 writeSectionContent(StringStream, *S);
624 else if (auto S = dyn_cast<WasmYAML::ImportSection>(Sec.get()))
625 writeSectionContent(StringStream, *S);
626 else if (auto S = dyn_cast<WasmYAML::FunctionSection>(Sec.get()))
627 writeSectionContent(StringStream, *S);
628 else if (auto S = dyn_cast<WasmYAML::TableSection>(Sec.get()))
629 writeSectionContent(StringStream, *S);
630 else if (auto S = dyn_cast<WasmYAML::MemorySection>(Sec.get()))
631 writeSectionContent(StringStream, *S);
632 else if (auto S = dyn_cast<WasmYAML::TagSection>(Sec.get()))
633 writeSectionContent(StringStream, *S);
634 else if (auto S = dyn_cast<WasmYAML::GlobalSection>(Sec.get()))
635 writeSectionContent(StringStream, *S);
636 else if (auto S = dyn_cast<WasmYAML::ExportSection>(Sec.get()))
637 writeSectionContent(StringStream, *S);
638 else if (auto S = dyn_cast<WasmYAML::StartSection>(Sec.get()))
639 writeSectionContent(StringStream, *S);
640 else if (auto S = dyn_cast<WasmYAML::ElemSection>(Sec.get()))
641 writeSectionContent(StringStream, *S);
642 else if (auto S = dyn_cast<WasmYAML::CodeSection>(Sec.get()))
643 writeSectionContent(StringStream, *S);
644 else if (auto S = dyn_cast<WasmYAML::DataSection>(Sec.get()))
645 writeSectionContent(StringStream, *S);
646 else if (auto S = dyn_cast<WasmYAML::DataCountSection>(Sec.get()))
647 writeSectionContent(StringStream, *S);
648 else
649 reportError("unknown section type: " + Twine(Sec->Type));
650
651 if (HasError)
652 return false;
653
654 unsigned HeaderSecSizeEncodingLen =
655 Sec->HeaderSecSizeEncodingLen.value_or(5);
656 unsigned RequiredLen = getULEB128Size(OutString.size());
657 // Wasm spec does not allow LEBs larger than 5 bytes
658 assert(RequiredLen <= 5);
659 if (HeaderSecSizeEncodingLen < RequiredLen) {
660 reportError("section header length can't be encoded in a LEB of size " +
661 Twine(HeaderSecSizeEncodingLen));
662 return false;
663 }
664 // Write the section size followed by the content
665 encodeULEB128(OutString.size(), OS, HeaderSecSizeEncodingLen);
666 OS << OutString;
667 }
668
669 // write reloc sections for any section that have relocations
670 uint32_t SectionIndex = 0;
671 for (const std::unique_ptr<WasmYAML::Section> &Sec : Obj.Sections) {
672 if (Sec->Relocations.empty()) {
673 SectionIndex++;
674 continue;
675 }
676
678 std::string OutString;
679 raw_string_ostream StringStream(OutString);
680 writeRelocSection(StringStream, *Sec, SectionIndex++);
681
682 encodeULEB128(OutString.size(), OS);
683 OS << OutString;
684 }
685
686 return true;
687}
688
689namespace llvm {
690namespace yaml {
691
693 WasmWriter Writer(Doc, EH);
694 return Writer.writeWasm(Out);
695}
696
697} // namespace yaml
698} // namespace llvm
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static Error reportError(StringRef Message)
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
Analysis containing CSE Info
Definition CSEInfo.cpp:27
OptimizedStructLayoutField Field
static int writeUint32(raw_ostream &OS, uint32_t Value)
static int writeLimits(const WasmYAML::Limits &Lim, raw_ostream &OS)
static int writeStringRef(const StringRef &Str, raw_ostream &OS)
static int writeUint8(raw_ostream &OS, uint8_t Value)
static int writeUint64(raw_ostream &OS, uint64_t Value)
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
LLVM Value Representation.
Definition Value.h:75
LLVM_ABI bool isValidSectionOrder(unsigned ID, StringRef CustomSectionName="")
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
raw_ostream & write(unsigned char C)
LLVM_ABI void writeAsBinary(raw_ostream &OS, uint64_t N=UINT64_MAX) const
Write the contents (regardless of whether it is binary or a hex string) as binary to the given raw_os...
Definition YAML.cpp:39
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ Entry
Definition COFF.h:862
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
NodeAddr< FuncNode * > Func
Definition RDFGraph.h:393
void write64le(void *P, uint64_t V)
Definition Endian.h:478
void write32le(void *P, uint32_t V)
Definition Endian.h:475
const unsigned WASM_SYMBOL_UNDEFINED
Definition Wasm.h:247
@ WASM_INIT_FUNCS
Definition Wasm.h:196
@ WASM_COMDAT_INFO
Definition Wasm.h:197
@ WASM_SEGMENT_INFO
Definition Wasm.h:195
@ WASM_SYMBOL_TABLE
Definition Wasm.h:198
const char WasmMagic[]
Definition Wasm.h:27
@ WASM_SEC_CODE
Definition Wasm.h:47
@ WASM_SEC_CUSTOM
Definition Wasm.h:37
@ WASM_SEC_DATA
Definition Wasm.h:48
@ WASM_LIMITS_FLAG_HAS_MAX
Definition Wasm.h:159
@ WASM_DATA_SEGMENT_IS_PASSIVE
Definition Wasm.h:166
@ WASM_DATA_SEGMENT_HAS_MEMINDEX
Definition Wasm.h:167
@ WASM_SYMBOL_TYPE_GLOBAL
Definition Wasm.h:222
@ WASM_SYMBOL_TYPE_DATA
Definition Wasm.h:221
@ WASM_SYMBOL_TYPE_TAG
Definition Wasm.h:224
@ WASM_SYMBOL_TYPE_TABLE
Definition Wasm.h:225
@ WASM_SYMBOL_TYPE_SECTION
Definition Wasm.h:223
@ WASM_SYMBOL_TYPE_FUNCTION
Definition Wasm.h:220
@ WASM_ELEM_SEGMENT_HAS_TABLE_NUMBER
Definition Wasm.h:173
LLVM_ABI bool relocTypeHasAddend(uint32_t type)
Definition Wasm.cpp:66
@ WASM_OPCODE_F64_CONST
Definition Wasm.h:107
@ WASM_OPCODE_END
Definition Wasm.h:95
@ WASM_OPCODE_F32_CONST
Definition Wasm.h:106
@ WASM_OPCODE_GLOBAL_GET
Definition Wasm.h:100
@ WASM_OPCODE_I64_CONST
Definition Wasm.h:105
@ WASM_OPCODE_I32_CONST
Definition Wasm.h:104
@ WASM_EXTERNAL_TABLE
Definition Wasm.h:87
@ WASM_EXTERNAL_FUNCTION
Definition Wasm.h:86
@ WASM_EXTERNAL_TAG
Definition Wasm.h:90
@ WASM_EXTERNAL_MEMORY
Definition Wasm.h:88
@ WASM_EXTERNAL_GLOBAL
Definition Wasm.h:89
@ WASM_DYLINK_RUNTIME_PATH
Definition Wasm.h:207
@ WASM_DYLINK_NEEDED
Definition Wasm.h:204
@ WASM_DYLINK_MEM_INFO
Definition Wasm.h:203
@ WASM_NAMES_DATA_SEGMENT
Definition Wasm.h:190
@ WASM_NAMES_GLOBAL
Definition Wasm.h:189
@ WASM_NAMES_FUNCTION
Definition Wasm.h:187
LLVM_ABI llvm::StringRef sectionTypeToString(uint32_t type)
Definition Wasm.cpp:41
const unsigned WASM_SYMBOL_EXPLICIT_NAME
Definition Wasm.h:249
const unsigned WASM_ELEM_SEGMENT_MASK_HAS_ELEM_DESC
Definition Wasm.h:176
llvm::function_ref< void(const Twine &Msg)> ErrorHandler
Definition yaml2obj.h:68
LLVM_ABI bool yaml2wasm(WasmYAML::Object &Doc, raw_ostream &Out, ErrorHandler EH)
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are tuples (A, B,...
Definition STLExtras.h:2544
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
@ Export
Export information to summary.
Definition IPO.h:57
@ Import
Import information from summary.
Definition IPO.h:56
@ Global
Append to llvm.global_dtors.
LLVM_ABI unsigned getULEB128Size(uint64_t Value)
Utility function to get the size of the ULEB128-encoded value.
Definition LEB128.cpp:19
FunctionAddr VTableAddr uintptr_t uintptr_t Data
Definition InstrProf.h:189
unsigned encodeSLEB128(int64_t Value, raw_ostream &OS, unsigned PadTo=0)
Utility function to encode a SLEB128 value to an output stream.
Definition LEB128.h:24
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
unsigned encodeULEB128(uint64_t Value, raw_ostream &OS, unsigned PadTo=0)
Utility function to encode a ULEB128 value to an output stream.
Definition LEB128.h:79
wasm::WasmInitExprMVP Inst
Definition WasmYAML.h:70
yaml::BinaryRef Body
Definition WasmYAML.h:71
yaml::Hex32 Minimum
Definition WasmYAML.h:49
yaml::Hex32 Maximum
Definition WasmYAML.h:50
std::vector< std::unique_ptr< Section > > Sections
Definition WasmYAML.h:421
std::vector< Relocation > Relocations
Definition WasmYAML.h:192
std::vector< ValueType > ReturnTypes
Definition WasmYAML.h:158
std::vector< ValueType > ParamTypes
Definition WasmYAML.h:157
union llvm::wasm::WasmInitExprMVP::@021046217255127373215144224227277301132130072341 Value
Common declarations for yaml2obj.