Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:22

0001 //===- WasmYAML.h - Wasm YAMLIO implementation ------------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 ///
0009 /// \file
0010 /// This file declares classes for handling the YAML representation
0011 /// of wasm binaries.
0012 ///
0013 //===----------------------------------------------------------------------===//
0014 
0015 #ifndef LLVM_OBJECTYAML_WASMYAML_H
0016 #define LLVM_OBJECTYAML_WASMYAML_H
0017 
0018 #include "llvm/ADT/StringRef.h"
0019 #include "llvm/BinaryFormat/Wasm.h"
0020 #include "llvm/ObjectYAML/YAML.h"
0021 #include "llvm/Support/Casting.h"
0022 #include <cstdint>
0023 #include <memory>
0024 #include <vector>
0025 
0026 namespace llvm {
0027 namespace WasmYAML {
0028 
0029 LLVM_YAML_STRONG_TYPEDEF(uint32_t, SectionType)
0030 LLVM_YAML_STRONG_TYPEDEF(uint32_t, ValueType)
0031 LLVM_YAML_STRONG_TYPEDEF(uint32_t, TableType)
0032 LLVM_YAML_STRONG_TYPEDEF(uint32_t, SignatureForm)
0033 LLVM_YAML_STRONG_TYPEDEF(uint32_t, ExportKind)
0034 LLVM_YAML_STRONG_TYPEDEF(uint32_t, Opcode)
0035 LLVM_YAML_STRONG_TYPEDEF(uint32_t, RelocType)
0036 LLVM_YAML_STRONG_TYPEDEF(uint32_t, SymbolFlags)
0037 LLVM_YAML_STRONG_TYPEDEF(uint32_t, SymbolKind)
0038 LLVM_YAML_STRONG_TYPEDEF(uint32_t, SegmentFlags)
0039 LLVM_YAML_STRONG_TYPEDEF(uint32_t, LimitFlags)
0040 LLVM_YAML_STRONG_TYPEDEF(uint32_t, ComdatKind)
0041 LLVM_YAML_STRONG_TYPEDEF(uint32_t, FeaturePolicyPrefix)
0042 
0043 struct FileHeader {
0044   yaml::Hex32 Version;
0045 };
0046 
0047 struct Limits {
0048   LimitFlags Flags;
0049   yaml::Hex32 Minimum;
0050   yaml::Hex32 Maximum;
0051 };
0052 
0053 struct Table {
0054   TableType ElemType;
0055   Limits TableLimits;
0056   uint32_t Index;
0057 };
0058 
0059 struct Export {
0060   StringRef Name;
0061   ExportKind Kind;
0062   uint32_t Index;
0063 };
0064 
0065 struct InitExpr {
0066   InitExpr() {}
0067   bool Extended;
0068   union {
0069     wasm::WasmInitExprMVP Inst;
0070     yaml::BinaryRef Body;
0071   };
0072 };
0073 
0074 struct ElemSegment {
0075   uint32_t Flags;
0076   uint32_t TableNumber;
0077   ValueType ElemKind;
0078   InitExpr Offset;
0079   std::vector<uint32_t> Functions;
0080 };
0081 
0082 struct Global {
0083   uint32_t Index;
0084   ValueType Type;
0085   bool Mutable;
0086   InitExpr Init;
0087 };
0088 
0089 struct Import {
0090   Import() {}
0091   StringRef Module;
0092   StringRef Field;
0093   ExportKind Kind;
0094   union {
0095     uint32_t SigIndex;
0096     Table TableImport;
0097     Limits Memory;
0098     uint32_t TagIndex;
0099     Global GlobalImport;
0100   };
0101 };
0102 
0103 struct LocalDecl {
0104   ValueType Type;
0105   uint32_t Count;
0106 };
0107 
0108 struct Function {
0109   uint32_t Index;
0110   std::vector<LocalDecl> Locals;
0111   yaml::BinaryRef Body;
0112 };
0113 
0114 struct Relocation {
0115   RelocType Type;
0116   uint32_t Index;
0117   // TODO(wvo): this would strictly be better as Hex64, but that will change
0118   // all existing obj2yaml output.
0119   yaml::Hex32 Offset;
0120   int64_t Addend;
0121 };
0122 
0123 struct DataSegment {
0124   uint32_t SectionOffset;
0125   uint32_t InitFlags;
0126   uint32_t MemoryIndex;
0127   InitExpr Offset;
0128   yaml::BinaryRef Content;
0129 };
0130 
0131 struct NameEntry {
0132   uint32_t Index;
0133   StringRef Name;
0134 };
0135 
0136 struct ProducerEntry {
0137   std::string Name;
0138   std::string Version;
0139 };
0140 
0141 struct FeatureEntry {
0142   FeaturePolicyPrefix Prefix;
0143   std::string Name;
0144 };
0145 
0146 struct SegmentInfo {
0147   uint32_t Index;
0148   StringRef Name;
0149   uint32_t Alignment;
0150   SegmentFlags Flags;
0151 };
0152 
0153 struct Signature {
0154   uint32_t Index;
0155   SignatureForm Form = wasm::WASM_TYPE_FUNC;
0156   std::vector<ValueType> ParamTypes;
0157   std::vector<ValueType> ReturnTypes;
0158 };
0159 
0160 struct SymbolInfo {
0161   uint32_t Index;
0162   StringRef Name;
0163   SymbolKind Kind;
0164   SymbolFlags Flags;
0165   union {
0166     uint32_t ElementIndex;
0167     wasm::WasmDataReference DataRef;
0168   };
0169 };
0170 
0171 struct InitFunction {
0172   uint32_t Priority;
0173   uint32_t Symbol;
0174 };
0175 
0176 struct ComdatEntry {
0177   ComdatKind Kind;
0178   uint32_t Index;
0179 };
0180 
0181 struct Comdat {
0182   StringRef Name;
0183   std::vector<ComdatEntry> Entries;
0184 };
0185 
0186 struct Section {
0187   explicit Section(SectionType SecType) : Type(SecType) {}
0188   virtual ~Section();
0189 
0190   SectionType Type;
0191   std::vector<Relocation> Relocations;
0192   std::optional<uint8_t> HeaderSecSizeEncodingLen;
0193 };
0194 
0195 struct CustomSection : Section {
0196   explicit CustomSection(StringRef Name)
0197       : Section(wasm::WASM_SEC_CUSTOM), Name(Name) {}
0198 
0199   static bool classof(const Section *S) {
0200     return S->Type == wasm::WASM_SEC_CUSTOM;
0201   }
0202 
0203   StringRef Name;
0204   yaml::BinaryRef Payload;
0205 };
0206 
0207 struct DylinkImportInfo {
0208   StringRef Module;
0209   StringRef Field;
0210   SymbolFlags Flags;
0211 };
0212 
0213 struct DylinkExportInfo {
0214   StringRef Name;
0215   SymbolFlags Flags;
0216 };
0217 
0218 struct DylinkSection : CustomSection {
0219   DylinkSection() : CustomSection("dylink.0") {}
0220 
0221   static bool classof(const Section *S) {
0222     auto C = dyn_cast<CustomSection>(S);
0223     return C && C->Name == "dylink.0";
0224   }
0225 
0226   uint32_t MemorySize;
0227   uint32_t MemoryAlignment;
0228   uint32_t TableSize;
0229   uint32_t TableAlignment;
0230   std::vector<StringRef> Needed;
0231   std::vector<DylinkImportInfo> ImportInfo;
0232   std::vector<DylinkExportInfo> ExportInfo;
0233 };
0234 
0235 struct NameSection : CustomSection {
0236   NameSection() : CustomSection("name") {}
0237 
0238   static bool classof(const Section *S) {
0239     auto C = dyn_cast<CustomSection>(S);
0240     return C && C->Name == "name";
0241   }
0242 
0243   std::vector<NameEntry> FunctionNames;
0244   std::vector<NameEntry> GlobalNames;
0245   std::vector<NameEntry> DataSegmentNames;
0246 };
0247 
0248 struct LinkingSection : CustomSection {
0249   LinkingSection() : CustomSection("linking") {}
0250 
0251   static bool classof(const Section *S) {
0252     auto C = dyn_cast<CustomSection>(S);
0253     return C && C->Name == "linking";
0254   }
0255 
0256   uint32_t Version;
0257   std::vector<SymbolInfo> SymbolTable;
0258   std::vector<SegmentInfo> SegmentInfos;
0259   std::vector<InitFunction> InitFunctions;
0260   std::vector<Comdat> Comdats;
0261 };
0262 
0263 struct ProducersSection : CustomSection {
0264   ProducersSection() : CustomSection("producers") {}
0265 
0266   static bool classof(const Section *S) {
0267     auto C = dyn_cast<CustomSection>(S);
0268     return C && C->Name == "producers";
0269   }
0270 
0271   std::vector<ProducerEntry> Languages;
0272   std::vector<ProducerEntry> Tools;
0273   std::vector<ProducerEntry> SDKs;
0274 };
0275 
0276 struct TargetFeaturesSection : CustomSection {
0277   TargetFeaturesSection() : CustomSection("target_features") {}
0278 
0279   static bool classof(const Section *S) {
0280     auto C = dyn_cast<CustomSection>(S);
0281     return C && C->Name == "target_features";
0282   }
0283 
0284   std::vector<FeatureEntry> Features;
0285 };
0286 
0287 struct TypeSection : Section {
0288   TypeSection() : Section(wasm::WASM_SEC_TYPE) {}
0289 
0290   static bool classof(const Section *S) {
0291     return S->Type == wasm::WASM_SEC_TYPE;
0292   }
0293 
0294   std::vector<Signature> Signatures;
0295 };
0296 
0297 struct ImportSection : Section {
0298   ImportSection() : Section(wasm::WASM_SEC_IMPORT) {}
0299 
0300   static bool classof(const Section *S) {
0301     return S->Type == wasm::WASM_SEC_IMPORT;
0302   }
0303 
0304   std::vector<Import> Imports;
0305 };
0306 
0307 struct FunctionSection : Section {
0308   FunctionSection() : Section(wasm::WASM_SEC_FUNCTION) {}
0309 
0310   static bool classof(const Section *S) {
0311     return S->Type == wasm::WASM_SEC_FUNCTION;
0312   }
0313 
0314   std::vector<uint32_t> FunctionTypes;
0315 };
0316 
0317 struct TableSection : Section {
0318   TableSection() : Section(wasm::WASM_SEC_TABLE) {}
0319 
0320   static bool classof(const Section *S) {
0321     return S->Type == wasm::WASM_SEC_TABLE;
0322   }
0323 
0324   std::vector<Table> Tables;
0325 };
0326 
0327 struct MemorySection : Section {
0328   MemorySection() : Section(wasm::WASM_SEC_MEMORY) {}
0329 
0330   static bool classof(const Section *S) {
0331     return S->Type == wasm::WASM_SEC_MEMORY;
0332   }
0333 
0334   std::vector<Limits> Memories;
0335 };
0336 
0337 struct TagSection : Section {
0338   TagSection() : Section(wasm::WASM_SEC_TAG) {}
0339 
0340   static bool classof(const Section *S) {
0341     return S->Type == wasm::WASM_SEC_TAG;
0342   }
0343 
0344   std::vector<uint32_t> TagTypes;
0345 };
0346 
0347 struct GlobalSection : Section {
0348   GlobalSection() : Section(wasm::WASM_SEC_GLOBAL) {}
0349 
0350   static bool classof(const Section *S) {
0351     return S->Type == wasm::WASM_SEC_GLOBAL;
0352   }
0353 
0354   std::vector<Global> Globals;
0355 };
0356 
0357 struct ExportSection : Section {
0358   ExportSection() : Section(wasm::WASM_SEC_EXPORT) {}
0359 
0360   static bool classof(const Section *S) {
0361     return S->Type == wasm::WASM_SEC_EXPORT;
0362   }
0363 
0364   std::vector<Export> Exports;
0365 };
0366 
0367 struct StartSection : Section {
0368   StartSection() : Section(wasm::WASM_SEC_START) {}
0369 
0370   static bool classof(const Section *S) {
0371     return S->Type == wasm::WASM_SEC_START;
0372   }
0373 
0374   uint32_t StartFunction;
0375 };
0376 
0377 struct ElemSection : Section {
0378   ElemSection() : Section(wasm::WASM_SEC_ELEM) {}
0379 
0380   static bool classof(const Section *S) {
0381     return S->Type == wasm::WASM_SEC_ELEM;
0382   }
0383 
0384   std::vector<ElemSegment> Segments;
0385 };
0386 
0387 struct CodeSection : Section {
0388   CodeSection() : Section(wasm::WASM_SEC_CODE) {}
0389 
0390   static bool classof(const Section *S) {
0391     return S->Type == wasm::WASM_SEC_CODE;
0392   }
0393 
0394   std::vector<Function> Functions;
0395 };
0396 
0397 struct DataSection : Section {
0398   DataSection() : Section(wasm::WASM_SEC_DATA) {}
0399 
0400   static bool classof(const Section *S) {
0401     return S->Type == wasm::WASM_SEC_DATA;
0402   }
0403 
0404   std::vector<DataSegment> Segments;
0405 };
0406 
0407 struct DataCountSection : Section {
0408   DataCountSection() : Section(wasm::WASM_SEC_DATACOUNT) {}
0409 
0410   static bool classof(const Section *S) {
0411     return S->Type == wasm::WASM_SEC_DATACOUNT;
0412   }
0413 
0414   uint32_t Count;
0415 };
0416 
0417 struct Object {
0418   FileHeader Header;
0419   std::vector<std::unique_ptr<Section>> Sections;
0420 };
0421 
0422 } // end namespace WasmYAML
0423 } // end namespace llvm
0424 
0425 LLVM_YAML_IS_SEQUENCE_VECTOR(std::unique_ptr<llvm::WasmYAML::Section>)
0426 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Signature)
0427 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::ValueType)
0428 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Table)
0429 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Import)
0430 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Export)
0431 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::ElemSegment)
0432 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Limits)
0433 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::DataSegment)
0434 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Global)
0435 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Function)
0436 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::LocalDecl)
0437 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Relocation)
0438 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::NameEntry)
0439 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::ProducerEntry)
0440 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::FeatureEntry)
0441 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::SegmentInfo)
0442 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::SymbolInfo)
0443 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::InitFunction)
0444 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::ComdatEntry)
0445 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Comdat)
0446 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::DylinkImportInfo)
0447 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::DylinkExportInfo)
0448 
0449 namespace llvm {
0450 namespace yaml {
0451 
0452 template <> struct MappingTraits<WasmYAML::FileHeader> {
0453   static void mapping(IO &IO, WasmYAML::FileHeader &FileHdr);
0454 };
0455 
0456 template <> struct MappingTraits<std::unique_ptr<WasmYAML::Section>> {
0457   static void mapping(IO &IO, std::unique_ptr<WasmYAML::Section> &Section);
0458 };
0459 
0460 template <> struct MappingTraits<WasmYAML::Object> {
0461   static void mapping(IO &IO, WasmYAML::Object &Object);
0462 };
0463 
0464 template <> struct MappingTraits<WasmYAML::Import> {
0465   static void mapping(IO &IO, WasmYAML::Import &Import);
0466 };
0467 
0468 template <> struct MappingTraits<WasmYAML::Export> {
0469   static void mapping(IO &IO, WasmYAML::Export &Export);
0470 };
0471 
0472 template <> struct MappingTraits<WasmYAML::Global> {
0473   static void mapping(IO &IO, WasmYAML::Global &Global);
0474 };
0475 
0476 template <> struct ScalarBitSetTraits<WasmYAML::LimitFlags> {
0477   static void bitset(IO &IO, WasmYAML::LimitFlags &Value);
0478 };
0479 
0480 template <> struct ScalarBitSetTraits<WasmYAML::SymbolFlags> {
0481   static void bitset(IO &IO, WasmYAML::SymbolFlags &Value);
0482 };
0483 
0484 template <> struct ScalarEnumerationTraits<WasmYAML::SymbolKind> {
0485   static void enumeration(IO &IO, WasmYAML::SymbolKind &Kind);
0486 };
0487 
0488 template <> struct ScalarBitSetTraits<WasmYAML::SegmentFlags> {
0489   static void bitset(IO &IO, WasmYAML::SegmentFlags &Value);
0490 };
0491 
0492 template <> struct ScalarEnumerationTraits<WasmYAML::SectionType> {
0493   static void enumeration(IO &IO, WasmYAML::SectionType &Type);
0494 };
0495 
0496 template <> struct MappingTraits<WasmYAML::Signature> {
0497   static void mapping(IO &IO, WasmYAML::Signature &Signature);
0498 };
0499 
0500 template <> struct MappingTraits<WasmYAML::Table> {
0501   static void mapping(IO &IO, WasmYAML::Table &Table);
0502 };
0503 
0504 template <> struct MappingTraits<WasmYAML::Limits> {
0505   static void mapping(IO &IO, WasmYAML::Limits &Limits);
0506 };
0507 
0508 template <> struct MappingTraits<WasmYAML::Function> {
0509   static void mapping(IO &IO, WasmYAML::Function &Function);
0510 };
0511 
0512 template <> struct MappingTraits<WasmYAML::Relocation> {
0513   static void mapping(IO &IO, WasmYAML::Relocation &Relocation);
0514 };
0515 
0516 template <> struct MappingTraits<WasmYAML::NameEntry> {
0517   static void mapping(IO &IO, WasmYAML::NameEntry &NameEntry);
0518 };
0519 
0520 template <> struct MappingTraits<WasmYAML::ProducerEntry> {
0521   static void mapping(IO &IO, WasmYAML::ProducerEntry &ProducerEntry);
0522 };
0523 
0524 template <> struct ScalarEnumerationTraits<WasmYAML::FeaturePolicyPrefix> {
0525   static void enumeration(IO &IO, WasmYAML::FeaturePolicyPrefix &Prefix);
0526 };
0527 
0528 template <> struct MappingTraits<WasmYAML::FeatureEntry> {
0529   static void mapping(IO &IO, WasmYAML::FeatureEntry &FeatureEntry);
0530 };
0531 
0532 template <> struct MappingTraits<WasmYAML::SegmentInfo> {
0533   static void mapping(IO &IO, WasmYAML::SegmentInfo &SegmentInfo);
0534 };
0535 
0536 template <> struct MappingTraits<WasmYAML::LocalDecl> {
0537   static void mapping(IO &IO, WasmYAML::LocalDecl &LocalDecl);
0538 };
0539 
0540 template <> struct MappingTraits<WasmYAML::InitExpr> {
0541   static void mapping(IO &IO, WasmYAML::InitExpr &Expr);
0542 };
0543 
0544 template <> struct MappingTraits<WasmYAML::DataSegment> {
0545   static void mapping(IO &IO, WasmYAML::DataSegment &Segment);
0546 };
0547 
0548 template <> struct MappingTraits<WasmYAML::ElemSegment> {
0549   static void mapping(IO &IO, WasmYAML::ElemSegment &Segment);
0550 };
0551 
0552 template <> struct MappingTraits<WasmYAML::SymbolInfo> {
0553   static void mapping(IO &IO, WasmYAML::SymbolInfo &Info);
0554 };
0555 
0556 template <> struct MappingTraits<WasmYAML::InitFunction> {
0557   static void mapping(IO &IO, WasmYAML::InitFunction &Init);
0558 };
0559 
0560 template <> struct ScalarEnumerationTraits<WasmYAML::ComdatKind> {
0561   static void enumeration(IO &IO, WasmYAML::ComdatKind &Kind);
0562 };
0563 
0564 template <> struct MappingTraits<WasmYAML::ComdatEntry> {
0565   static void mapping(IO &IO, WasmYAML::ComdatEntry &ComdatEntry);
0566 };
0567 
0568 template <> struct MappingTraits<WasmYAML::Comdat> {
0569   static void mapping(IO &IO, WasmYAML::Comdat &Comdat);
0570 };
0571 
0572 template <> struct ScalarEnumerationTraits<WasmYAML::ValueType> {
0573   static void enumeration(IO &IO, WasmYAML::ValueType &Type);
0574 };
0575 
0576 template <> struct ScalarEnumerationTraits<WasmYAML::ExportKind> {
0577   static void enumeration(IO &IO, WasmYAML::ExportKind &Kind);
0578 };
0579 
0580 template <> struct ScalarEnumerationTraits<WasmYAML::TableType> {
0581   static void enumeration(IO &IO, WasmYAML::TableType &Type);
0582 };
0583 
0584 template <> struct ScalarEnumerationTraits<WasmYAML::Opcode> {
0585   static void enumeration(IO &IO, WasmYAML::Opcode &Opcode);
0586 };
0587 
0588 template <> struct ScalarEnumerationTraits<WasmYAML::RelocType> {
0589   static void enumeration(IO &IO, WasmYAML::RelocType &Kind);
0590 };
0591 
0592 template <> struct MappingTraits<WasmYAML::DylinkImportInfo> {
0593   static void mapping(IO &IO, WasmYAML::DylinkImportInfo &Info);
0594 };
0595 
0596 template <> struct MappingTraits<WasmYAML::DylinkExportInfo> {
0597   static void mapping(IO &IO, WasmYAML::DylinkExportInfo &Info);
0598 };
0599 
0600 } // end namespace yaml
0601 } // end namespace llvm
0602 
0603 #endif // LLVM_OBJECTYAML_WASMYAML_H