29 : Obj(Obj), ErrHandler(EH) {}
30 bool writeWasm(raw_ostream &OS);
33 void writeRelocSection(raw_ostream &OS, WasmYAML::Section &Sec,
34 uint32_t SectionIndex);
36 void writeInitExpr(raw_ostream &OS,
const WasmYAML::InitExpr &InitExpr);
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);
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;
66 bool HasError =
false;
71class SubSectionWriter {
73 std::string OutString;
74 raw_string_ostream StringStream;
77 SubSectionWriter(raw_ostream &OS) : OS(OS), StringStream(OutString) {}
85 raw_ostream &getStream() {
return StringStream; }
125void WasmWriter::reportError(
const Twine &Msg) {
130void WasmWriter::writeInitExpr(raw_ostream &OS,
131 const WasmYAML::InitExpr &InitExpr) {
161void WasmWriter::writeSectionContent(raw_ostream &OS,
162 WasmYAML::DylinkSection &Section) {
166 SubSectionWriter SubSection(OS);
167 raw_ostream &SubOS = SubSection.getStream();
176 raw_ostream &SubOS = SubSection.getStream();
178 for (StringRef Needed :
Section.Needed)
182 if (
Section.RuntimePath.size()) {
184 raw_ostream &SubOS = SubSection.getStream();
186 for (StringRef Path :
Section.RuntimePath)
192void WasmWriter::writeSectionContent(raw_ostream &OS,
193 WasmYAML::LinkingSection &Section) {
197 SubSectionWriter SubSection(OS);
200 if (
Section.SymbolTable.size()) {
204 const WasmYAML::SymbolInfo &
Info = Sym.value();
238 if (
Section.SegmentInfos.size()) {
241 for (
const WasmYAML::SegmentInfo &SegmentInfo :
Section.SegmentInfos) {
250 if (
Section.InitFunctions.size()) {
253 for (
const WasmYAML::InitFunction &Func :
Section.InitFunctions) {
264 for (
const auto &
C :
Section.Comdats) {
268 for (
const WasmYAML::ComdatEntry &Entry :
C.Entries) {
277void WasmWriter::writeSectionContent(raw_ostream &OS,
278 WasmYAML::NameSection &Section) {
280 if (
Section.FunctionNames.size()) {
283 SubSectionWriter SubSection(OS);
286 for (
const WasmYAML::NameEntry &NameEntry :
Section.FunctionNames) {
293 if (
Section.GlobalNames.size()) {
296 SubSectionWriter SubSection(OS);
299 for (
const WasmYAML::NameEntry &NameEntry :
Section.GlobalNames) {
306 if (
Section.DataSegmentNames.size()) {
309 SubSectionWriter SubSection(OS);
312 for (
const WasmYAML::NameEntry &NameEntry :
Section.DataSegmentNames) {
321void WasmWriter::writeSectionContent(raw_ostream &OS,
322 WasmYAML::ProducersSection &Section) {
324 int Fields = int(!
Section.Languages.empty()) + int(!
Section.Tools.empty()) +
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())
336 for (
auto &Entry : *
Field.second) {
343void WasmWriter::writeSectionContent(raw_ostream &OS,
344 WasmYAML::TargetFeaturesSection &Section) {
353void WasmWriter::writeSectionContent(raw_ostream &OS,
354 WasmYAML::CustomSection &Section) {
356 writeSectionContent(OS, *S);
358 writeSectionContent(OS, *S);
360 writeSectionContent(OS, *S);
362 writeSectionContent(OS, *S);
364 writeSectionContent(OS, *S);
367 Section.Payload.writeAsBinary(OS);
371void WasmWriter::writeSectionContent(raw_ostream &OS,
372 WasmYAML::TypeSection &Section) {
374 uint32_t ExpectedIndex = 0;
375 for (
const WasmYAML::Signature &Sig :
Section.Signatures) {
376 if (Sig.
Index != ExpectedIndex) {
391void WasmWriter::writeSectionContent(raw_ostream &OS,
392 WasmYAML::ImportSection &Section) {
401 NumImportedFunctions++;
406 NumImportedGlobals++;
428void WasmWriter::writeSectionContent(raw_ostream &OS,
429 WasmYAML::FunctionSection &Section) {
431 for (uint32_t FuncType :
Section.FunctionTypes)
435void WasmWriter::writeSectionContent(raw_ostream &OS,
436 WasmYAML::ExportSection &Section) {
445void WasmWriter::writeSectionContent(raw_ostream &OS,
446 WasmYAML::StartSection &Section) {
450void WasmWriter::writeSectionContent(raw_ostream &OS,
451 WasmYAML::TableSection &Section) {
453 uint32_t ExpectedIndex = NumImportedTables;
454 for (
auto &Table :
Section.Tables) {
455 if (Table.Index != ExpectedIndex) {
456 reportError(
"unexpected table index: " + Twine(Table.Index));
465void WasmWriter::writeSectionContent(raw_ostream &OS,
466 WasmYAML::MemorySection &Section) {
468 for (
const WasmYAML::Limits &Mem :
Section.Memories)
472void WasmWriter::writeSectionContent(raw_ostream &OS,
473 WasmYAML::TagSection &Section) {
475 for (uint32_t TagType :
Section.TagTypes) {
481void WasmWriter::writeSectionContent(raw_ostream &OS,
482 WasmYAML::GlobalSection &Section) {
484 uint32_t ExpectedIndex = NumImportedGlobals;
486 if (
Global.Index != ExpectedIndex) {
493 writeInitExpr(OS,
Global.Init);
497void WasmWriter::writeSectionContent(raw_ostream &OS,
498 WasmYAML::ElemSection &Section) {
500 for (
auto &Segment :
Section.Segments) {
505 writeInitExpr(OS, Segment.Offset);
511 if (Segment.ElemKind != uint32_t(wasm::ValType::FUNCREF)) {
512 reportError(
"unexpected elemkind: " + Twine(Segment.ElemKind));
515 const uint8_t ElemKind = 0;
520 for (
auto &Function : Segment.Functions)
525void WasmWriter::writeSectionContent(raw_ostream &OS,
526 WasmYAML::CodeSection &Section) {
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) {
539 for (
auto &LocalDecl :
Func.Locals) {
544 Func.Body.writeAsBinary(StringStream);
552void WasmWriter::writeSectionContent(raw_ostream &OS,
553 WasmYAML::DataSection &Section) {
555 for (
auto &Segment :
Section.Segments) {
560 writeInitExpr(OS, Segment.Offset);
562 Segment.Content.writeAsBinary(OS);
566void WasmWriter::writeSectionContent(raw_ostream &OS,
567 WasmYAML::DataCountSection &Section) {
571void WasmWriter::writeRelocSection(raw_ostream &OS, WasmYAML::Section &Sec,
572 uint32_t SectionIndex) {
601bool WasmWriter::writeWasm(raw_ostream &OS) {
607 llvm::object::WasmSectionOrderChecker Checker;
608 for (
const std::unique_ptr<WasmYAML::Section> &Sec : Obj.
Sections) {
609 StringRef SecName =
"";
618 std::string OutString;
619 raw_string_ostream StringStream(OutString);
621 writeSectionContent(StringStream, *S);
623 writeSectionContent(StringStream, *S);
625 writeSectionContent(StringStream, *S);
627 writeSectionContent(StringStream, *S);
629 writeSectionContent(StringStream, *S);
631 writeSectionContent(StringStream, *S);
633 writeSectionContent(StringStream, *S);
635 writeSectionContent(StringStream, *S);
637 writeSectionContent(StringStream, *S);
639 writeSectionContent(StringStream, *S);
641 writeSectionContent(StringStream, *S);
643 writeSectionContent(StringStream, *S);
645 writeSectionContent(StringStream, *S);
647 writeSectionContent(StringStream, *S);
649 reportError(
"unknown section type: " + Twine(Sec->Type));
654 unsigned HeaderSecSizeEncodingLen =
655 Sec->HeaderSecSizeEncodingLen.value_or(5);
659 if (HeaderSecSizeEncodingLen < RequiredLen) {
660 reportError(
"section header length can't be encoded in a LEB of size " +
661 Twine(HeaderSecSizeEncodingLen));
665 encodeULEB128(OutString.size(), OS, HeaderSecSizeEncodingLen);
670 uint32_t SectionIndex = 0;
671 for (
const std::unique_ptr<WasmYAML::Section> &Sec : Obj.
Sections) {
672 if (Sec->Relocations.empty()) {
678 std::string OutString;
679 raw_string_ostream StringStream(OutString);
680 writeRelocSection(StringStream, *Sec, SectionIndex++);
693 WasmWriter Writer(Doc, EH);
694 return Writer.writeWasm(Out);
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
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.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
LLVM Value Representation.
LLVM_ABI bool isValidSectionOrder(unsigned ID, StringRef CustomSectionName="")
This class implements an extremely fast bulk output stream that can only output to a stream.
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...
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ C
The default llvm calling convention, compatible with C.
NodeAddr< FuncNode * > Func
void write64le(void *P, uint64_t V)
void write32le(void *P, uint32_t V)
const unsigned WASM_SYMBOL_UNDEFINED
@ WASM_LIMITS_FLAG_HAS_MAX
@ WASM_DATA_SEGMENT_IS_PASSIVE
@ WASM_DATA_SEGMENT_HAS_MEMINDEX
@ WASM_SYMBOL_TYPE_GLOBAL
@ WASM_SYMBOL_TYPE_SECTION
@ WASM_SYMBOL_TYPE_FUNCTION
@ WASM_ELEM_SEGMENT_HAS_TABLE_NUMBER
LLVM_ABI bool relocTypeHasAddend(uint32_t type)
@ WASM_DYLINK_RUNTIME_PATH
@ WASM_NAMES_DATA_SEGMENT
LLVM_ABI llvm::StringRef sectionTypeToString(uint32_t type)
const unsigned WASM_SYMBOL_EXPLICIT_NAME
const unsigned WASM_ELEM_SEGMENT_MASK_HAS_ELEM_DESC
llvm::function_ref< void(const Twine &Msg)> ErrorHandler
LLVM_ABI bool yaml2wasm(WasmYAML::Object &Doc, raw_ostream &Out, ErrorHandler EH)
This is an optimization pass for GlobalISel generic memory operations.
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are tuples (A, B,...
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
@ Export
Export information to summary.
@ Import
Import information from summary.
@ Global
Append to llvm.global_dtors.
LLVM_ABI unsigned getULEB128Size(uint64_t Value)
Utility function to get the size of the ULEB128-encoded value.
FunctionAddr VTableAddr uintptr_t uintptr_t Data
unsigned encodeSLEB128(int64_t Value, raw_ostream &OS, unsigned PadTo=0)
Utility function to encode a SLEB128 value to an output stream.
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
unsigned encodeULEB128(uint64_t Value, raw_ostream &OS, unsigned PadTo=0)
Utility function to encode a ULEB128 value to an output stream.
wasm::WasmInitExprMVP Inst
std::vector< std::unique_ptr< Section > > Sections
std::vector< Relocation > Relocations
std::vector< ValueType > ReturnTypes
std::vector< ValueType > ParamTypes
union llvm::wasm::WasmInitExprMVP::@021046217255127373215144224227277301132130072341 Value
Common declarations for yaml2obj.