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);
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.ExportInfo.size()) {
184 raw_ostream &SubOS = SubSection.getStream();
186 for (
const WasmYAML::DylinkExportInfo &Info :
Section.ExportInfo) {
192 if (
Section.ImportInfo.size()) {
194 raw_ostream &SubOS = SubSection.getStream();
196 for (
const WasmYAML::DylinkImportInfo &Info :
Section.ImportInfo) {
203 if (
Section.RuntimePath.size()) {
205 raw_ostream &SubOS = SubSection.getStream();
207 for (StringRef Path :
Section.RuntimePath)
211 if (!
Section.TargetArch.empty()) {
213 raw_ostream &SubOS = SubSection.getStream();
219void WasmWriter::writeSectionContent(raw_ostream &OS,
220 WasmYAML::LinkingSection &Section) {
224 SubSectionWriter SubSection(OS);
227 if (
Section.SymbolTable.size()) {
231 const WasmYAML::SymbolInfo &
Info = Sym.value();
271 if (
Section.SegmentInfos.size()) {
274 for (
const WasmYAML::SegmentInfo &SegmentInfo :
Section.SegmentInfos) {
283 if (
Section.InitFunctions.size()) {
286 for (
const WasmYAML::InitFunction &Func :
Section.InitFunctions) {
297 for (
const auto &
C :
Section.Comdats) {
301 for (
const WasmYAML::ComdatEntry &Entry :
C.Entries) {
310 if (!
Section.TargetArch.empty()) {
312 raw_ostream &SubOS = SubSection.getStream();
318void WasmWriter::writeSectionContent(raw_ostream &OS,
319 WasmYAML::NameSection &Section) {
321 if (
Section.FunctionNames.size()) {
324 SubSectionWriter SubSection(OS);
327 for (
const WasmYAML::NameEntry &NameEntry :
Section.FunctionNames) {
334 if (
Section.GlobalNames.size()) {
337 SubSectionWriter SubSection(OS);
340 for (
const WasmYAML::NameEntry &NameEntry :
Section.GlobalNames) {
347 if (
Section.DataSegmentNames.size()) {
350 SubSectionWriter SubSection(OS);
353 for (
const WasmYAML::NameEntry &NameEntry :
Section.DataSegmentNames) {
362void WasmWriter::writeSectionContent(raw_ostream &OS,
363 WasmYAML::ProducersSection &Section) {
365 int Fields = int(!
Section.Languages.empty()) + int(!
Section.Tools.empty()) +
370 for (
auto &
Field : {std::make_pair(StringRef(
"language"), &
Section.Languages),
371 std::make_pair(StringRef(
"processed-by"), &
Section.Tools),
372 std::make_pair(StringRef(
"sdk"), &
Section.SDKs)}) {
373 if (
Field.second->empty())
377 for (
auto &Entry : *
Field.second) {
384void WasmWriter::writeSectionContent(raw_ostream &OS,
385 WasmYAML::TargetFeaturesSection &Section) {
394void WasmWriter::writeSectionContent(raw_ostream &OS,
395 WasmYAML::CustomSection &Section) {
408 Section.Payload.writeAsBinary(OS);
412void WasmWriter::writeSectionContent(raw_ostream &OS,
413 WasmYAML::TypeSection &Section) {
415 uint32_t ExpectedIndex = 0;
416 for (
const WasmYAML::Signature &Sig :
Section.Signatures) {
417 if (Sig.
Index != ExpectedIndex) {
432void WasmWriter::writeSectionContent(raw_ostream &OS,
433 WasmYAML::ImportSection &Section) {
442 NumImportedFunctions++;
447 NumImportedGlobals++;
469void WasmWriter::writeSectionContent(raw_ostream &OS,
470 WasmYAML::FunctionSection &Section) {
472 for (uint32_t FuncType :
Section.FunctionTypes)
476void WasmWriter::writeSectionContent(raw_ostream &OS,
477 WasmYAML::ExportSection &Section) {
486void WasmWriter::writeSectionContent(raw_ostream &OS,
487 WasmYAML::StartSection &Section) {
491void WasmWriter::writeSectionContent(raw_ostream &OS,
492 WasmYAML::TableSection &Section) {
494 uint32_t ExpectedIndex = NumImportedTables;
496 if (
Table.Index != ExpectedIndex) {
506void WasmWriter::writeSectionContent(raw_ostream &OS,
507 WasmYAML::MemorySection &Section) {
509 for (
const WasmYAML::Limits &Mem :
Section.Memories)
513void WasmWriter::writeSectionContent(raw_ostream &OS,
514 WasmYAML::TagSection &Section) {
516 for (uint32_t TagType :
Section.TagTypes) {
522void WasmWriter::writeSectionContent(raw_ostream &OS,
523 WasmYAML::GlobalSection &Section) {
525 uint32_t ExpectedIndex = NumImportedGlobals;
527 if (
Global.Index != ExpectedIndex) {
534 writeInitExpr(OS,
Global.Init);
538void WasmWriter::writeSectionContent(raw_ostream &OS,
539 WasmYAML::ElemSection &Section) {
541 for (
auto &Segment :
Section.Segments) {
546 writeInitExpr(OS, Segment.Offset);
552 if (Segment.ElemKind != uint32_t(wasm::ValType::FUNCREF)) {
553 reportError(
"unexpected elemkind: " + Twine(Segment.ElemKind));
556 const uint8_t ElemKind = 0;
561 for (
auto &
Function : Segment.Functions)
566void WasmWriter::writeSectionContent(raw_ostream &OS,
567 WasmYAML::CodeSection &Section) {
569 uint32_t ExpectedIndex = NumImportedFunctions;
570 for (
auto &Func :
Section.Functions) {
571 std::string OutString;
572 raw_string_ostream StringStream(OutString);
573 if (
Func.Index != ExpectedIndex) {
580 for (
auto &LocalDecl :
Func.Locals) {
585 Func.Body.writeAsBinary(StringStream);
593void WasmWriter::writeSectionContent(raw_ostream &OS,
594 WasmYAML::DataSection &Section) {
596 for (
auto &Segment :
Section.Segments) {
601 writeInitExpr(OS, Segment.Offset);
603 Segment.Content.writeAsBinary(OS);
607void WasmWriter::writeSectionContent(raw_ostream &OS,
608 WasmYAML::DataCountSection &Section) {
612void WasmWriter::writeRelocSection(raw_ostream &OS, WasmYAML::Section &Sec,
613 uint32_t SectionIndex) {
642bool WasmWriter::writeWasm(raw_ostream &OS) {
648 llvm::object::WasmSectionOrderChecker Checker;
649 for (
const std::unique_ptr<WasmYAML::Section> &Sec : Obj.
Sections) {
650 StringRef SecName =
"";
659 std::string OutString;
660 raw_string_ostream StringStream(OutString);
690 reportError(
"unknown section type: " + Twine(Sec->Type));
695 unsigned HeaderSecSizeEncodingLen =
696 Sec->HeaderSecSizeEncodingLen.value_or(5);
700 if (HeaderSecSizeEncodingLen < RequiredLen) {
701 reportError(
"section header length can't be encoded in a LEB of size " +
702 Twine(HeaderSecSizeEncodingLen));
706 encodeULEB128(OutString.size(), OS, HeaderSecSizeEncodingLen);
711 uint32_t SectionIndex = 0;
712 for (
const std::unique_ptr<WasmYAML::Section> &Sec : Obj.
Sections) {
713 if (Sec->Relocations.empty()) {
719 std::string OutString;
720 raw_string_ostream StringStream(OutString);
721 writeRelocSection(StringStream, *Sec, SectionIndex++);
734 WasmWriter Writer(Doc, EH);
735 return Writer.writeWasm(Out);
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static Error reportError(StringRef Message)
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static bool writeSectionContent(COFFParser &CP, COFFYAML::Section &S, ContiguousBlobAccumulator &CBA)
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)
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.
NodeAddr< FuncNode * > Func
void write64le(void *P, uint64_t V)
void write32le(void *P, uint32_t V)
const unsigned WASM_SYMBOL_UNDEFINED
@ WASM_NAMES_DATA_SEGMENT
@ WASM_LIMITS_FLAG_HAS_MAX
@ WASM_SYMBOL_TYPE_GLOBAL
@ WASM_SYMBOL_TYPE_SECTION
@ WASM_SYMBOL_TYPE_FUNCTION
const unsigned WASM_SYMBOL_BINDING_COMMON
const unsigned WASM_SYMBOL_BINDING_MASK
@ WASM_ELEM_SEGMENT_HAS_TABLE_NUMBER
@ WASM_DYLINK_RUNTIME_PATH
@ WASM_DYLINK_EXPORT_INFO
@ WASM_DYLINK_IMPORT_INFO
@ WASM_DYLINK_TARGET_ARCH
@ WASM_DATA_SEGMENT_IS_PASSIVE
@ WASM_DATA_SEGMENT_HAS_MEMINDEX
LLVM_ABI bool relocTypeHasAddend(uint32_t type)
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.
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::@234311111124136373374304035273310237314141125040 Value
Common declarations for yaml2obj.