Home | History | Annotate | Download | only in ObjectYAML
      1 //===- WasmYAML.h - Wasm YAMLIO implementation ------------------*- C++ -*-===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 ///
     10 /// \file
     11 /// \brief This file declares classes for handling the YAML representation
     12 /// of wasm binaries.
     13 ///
     14 //===----------------------------------------------------------------------===//
     15 
     16 #ifndef LLVM_OBJECTYAML_WASMYAML_H
     17 #define LLVM_OBJECTYAML_WASMYAML_H
     18 
     19 #include "llvm/ObjectYAML/YAML.h"
     20 #include "llvm/Support/Wasm.h"
     21 
     22 namespace llvm {
     23 namespace WasmYAML {
     24 
     25 LLVM_YAML_STRONG_TYPEDEF(uint32_t, SectionType)
     26 LLVM_YAML_STRONG_TYPEDEF(int32_t, ValueType)
     27 LLVM_YAML_STRONG_TYPEDEF(int32_t, TableType)
     28 LLVM_YAML_STRONG_TYPEDEF(int32_t, SignatureForm)
     29 LLVM_YAML_STRONG_TYPEDEF(uint32_t, ExportKind)
     30 LLVM_YAML_STRONG_TYPEDEF(uint32_t, Opcode)
     31 LLVM_YAML_STRONG_TYPEDEF(uint32_t, RelocType)
     32 
     33 struct FileHeader {
     34   yaml::Hex32 Version;
     35 };
     36 
     37 struct Import {
     38   StringRef Module;
     39   StringRef Field;
     40   ExportKind Kind;
     41   union {
     42     uint32_t SigIndex;
     43     ValueType GlobalType;
     44   };
     45   bool GlobalMutable;
     46 };
     47 
     48 struct Limits {
     49   yaml::Hex32 Flags;
     50   yaml::Hex32 Initial;
     51   yaml::Hex32 Maximum;
     52 };
     53 
     54 struct Table {
     55   TableType ElemType;
     56   Limits TableLimits;
     57 };
     58 
     59 struct Export {
     60   StringRef Name;
     61   ExportKind Kind;
     62   uint32_t Index;
     63 };
     64 
     65 struct ElemSegment {
     66   uint32_t TableIndex;
     67   wasm::WasmInitExpr Offset;
     68   std::vector<uint32_t> Functions;
     69 };
     70 
     71 struct Global {
     72   ValueType Type;
     73   bool Mutable;
     74   wasm::WasmInitExpr InitExpr;
     75 };
     76 
     77 struct LocalDecl {
     78   ValueType Type;
     79   uint32_t Count;
     80 };
     81 
     82 struct Function {
     83   std::vector<LocalDecl> Locals;
     84   yaml::BinaryRef Body;
     85 };
     86 
     87 struct Relocation {
     88   RelocType Type;
     89   uint32_t Index;
     90   yaml::Hex32 Offset;
     91   yaml::Hex32 Addend;
     92 };
     93 
     94 struct DataSegment {
     95   uint32_t Index;
     96   wasm::WasmInitExpr Offset;
     97   yaml::BinaryRef Content;
     98 };
     99 
    100 struct Signature {
    101   Signature() : Form(wasm::WASM_TYPE_FUNC) {}
    102 
    103   uint32_t Index;
    104   SignatureForm Form;
    105   std::vector<ValueType> ParamTypes;
    106   ValueType ReturnType;
    107 };
    108 
    109 struct Section {
    110   Section(SectionType SecType) : Type(SecType) {}
    111   virtual ~Section();
    112 
    113   SectionType Type;
    114   std::vector<Relocation> Relocations;
    115 };
    116 
    117 struct CustomSection : Section {
    118   CustomSection() : Section(wasm::WASM_SEC_CUSTOM) {}
    119   static bool classof(const Section *S) {
    120     return S->Type == wasm::WASM_SEC_CUSTOM;
    121   }
    122 
    123   StringRef Name;
    124   yaml::BinaryRef Payload;
    125 };
    126 
    127 struct TypeSection : Section {
    128   TypeSection() : Section(wasm::WASM_SEC_TYPE) {}
    129   static bool classof(const Section *S) {
    130     return S->Type == wasm::WASM_SEC_TYPE;
    131   }
    132 
    133   std::vector<Signature> Signatures;
    134 };
    135 
    136 struct ImportSection : Section {
    137   ImportSection() : Section(wasm::WASM_SEC_IMPORT) {}
    138   static bool classof(const Section *S) {
    139     return S->Type == wasm::WASM_SEC_IMPORT;
    140   }
    141 
    142   std::vector<Import> Imports;
    143 };
    144 
    145 struct FunctionSection : Section {
    146   FunctionSection() : Section(wasm::WASM_SEC_FUNCTION) {}
    147   static bool classof(const Section *S) {
    148     return S->Type == wasm::WASM_SEC_FUNCTION;
    149   }
    150 
    151   std::vector<uint32_t> FunctionTypes;
    152 };
    153 
    154 struct TableSection : Section {
    155   TableSection() : Section(wasm::WASM_SEC_TABLE) {}
    156   static bool classof(const Section *S) {
    157     return S->Type == wasm::WASM_SEC_TABLE;
    158   }
    159 
    160   std::vector<Table> Tables;
    161 };
    162 
    163 struct MemorySection : Section {
    164   MemorySection() : Section(wasm::WASM_SEC_MEMORY) {}
    165   static bool classof(const Section *S) {
    166     return S->Type == wasm::WASM_SEC_MEMORY;
    167   }
    168 
    169   std::vector<Limits> Memories;
    170 };
    171 
    172 struct GlobalSection : Section {
    173   GlobalSection() : Section(wasm::WASM_SEC_GLOBAL) {}
    174   static bool classof(const Section *S) {
    175     return S->Type == wasm::WASM_SEC_GLOBAL;
    176   }
    177 
    178   std::vector<Global> Globals;
    179 };
    180 
    181 struct ExportSection : Section {
    182   ExportSection() : Section(wasm::WASM_SEC_EXPORT) {}
    183   static bool classof(const Section *S) {
    184     return S->Type == wasm::WASM_SEC_EXPORT;
    185   }
    186 
    187   std::vector<Export> Exports;
    188 };
    189 
    190 struct StartSection : Section {
    191   StartSection() : Section(wasm::WASM_SEC_START) {}
    192   static bool classof(const Section *S) {
    193     return S->Type == wasm::WASM_SEC_START;
    194   }
    195 
    196   uint32_t StartFunction;
    197 };
    198 
    199 struct ElemSection : Section {
    200   ElemSection() : Section(wasm::WASM_SEC_ELEM) {}
    201   static bool classof(const Section *S) {
    202     return S->Type == wasm::WASM_SEC_ELEM;
    203   }
    204 
    205   std::vector<ElemSegment> Segments;
    206 };
    207 
    208 struct CodeSection : Section {
    209   CodeSection() : Section(wasm::WASM_SEC_CODE) {}
    210   static bool classof(const Section *S) {
    211     return S->Type == wasm::WASM_SEC_CODE;
    212   }
    213 
    214   std::vector<Function> Functions;
    215 };
    216 
    217 struct DataSection : Section {
    218   DataSection() : Section(wasm::WASM_SEC_DATA) {}
    219   static bool classof(const Section *S) {
    220     return S->Type == wasm::WASM_SEC_DATA;
    221   }
    222 
    223   std::vector<DataSegment> Segments;
    224 };
    225 
    226 struct Object {
    227   FileHeader Header;
    228   std::vector<std::unique_ptr<Section>> Sections;
    229 };
    230 
    231 } // end namespace WasmYAML
    232 } // end namespace llvm
    233 
    234 LLVM_YAML_IS_SEQUENCE_VECTOR(std::unique_ptr<llvm::WasmYAML::Section>)
    235 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Signature)
    236 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::ValueType)
    237 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Table)
    238 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Import)
    239 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Export)
    240 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::ElemSegment)
    241 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Limits)
    242 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::DataSegment)
    243 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Global)
    244 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Function)
    245 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::LocalDecl)
    246 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Relocation)
    247 LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(uint32_t)
    248 
    249 namespace llvm {
    250 namespace yaml {
    251 
    252 template <> struct MappingTraits<WasmYAML::FileHeader> {
    253   static void mapping(IO &IO, WasmYAML::FileHeader &FileHdr);
    254 };
    255 
    256 template <> struct MappingTraits<std::unique_ptr<WasmYAML::Section>> {
    257   static void mapping(IO &IO, std::unique_ptr<WasmYAML::Section> &Section);
    258 };
    259 
    260 template <> struct MappingTraits<WasmYAML::Object> {
    261   static void mapping(IO &IO, WasmYAML::Object &Object);
    262 };
    263 
    264 template <> struct MappingTraits<WasmYAML::Import> {
    265   static void mapping(IO &IO, WasmYAML::Import &Import);
    266 };
    267 
    268 template <> struct MappingTraits<WasmYAML::Export> {
    269   static void mapping(IO &IO, WasmYAML::Export &Export);
    270 };
    271 
    272 template <> struct MappingTraits<WasmYAML::Global> {
    273   static void mapping(IO &IO, WasmYAML::Global &Global);
    274 };
    275 
    276 template <> struct ScalarEnumerationTraits<WasmYAML::SectionType> {
    277   static void enumeration(IO &IO, WasmYAML::SectionType &Type);
    278 };
    279 
    280 template <> struct MappingTraits<WasmYAML::Signature> {
    281   static void mapping(IO &IO, WasmYAML::Signature &Signature);
    282 };
    283 
    284 template <> struct MappingTraits<WasmYAML::Table> {
    285   static void mapping(IO &IO, WasmYAML::Table &Table);
    286 };
    287 
    288 template <> struct MappingTraits<WasmYAML::Limits> {
    289   static void mapping(IO &IO, WasmYAML::Limits &Limits);
    290 };
    291 
    292 template <> struct MappingTraits<WasmYAML::Function> {
    293   static void mapping(IO &IO, WasmYAML::Function &Function);
    294 };
    295 
    296 template <> struct MappingTraits<WasmYAML::Relocation> {
    297   static void mapping(IO &IO, WasmYAML::Relocation &Relocation);
    298 };
    299 
    300 template <> struct MappingTraits<WasmYAML::LocalDecl> {
    301   static void mapping(IO &IO, WasmYAML::LocalDecl &LocalDecl);
    302 };
    303 
    304 template <> struct MappingTraits<wasm::WasmInitExpr> {
    305   static void mapping(IO &IO, wasm::WasmInitExpr &Expr);
    306 };
    307 
    308 template <> struct MappingTraits<WasmYAML::DataSegment> {
    309   static void mapping(IO &IO, WasmYAML::DataSegment &Segment);
    310 };
    311 
    312 template <> struct MappingTraits<WasmYAML::ElemSegment> {
    313   static void mapping(IO &IO, WasmYAML::ElemSegment &Segment);
    314 };
    315 
    316 template <> struct ScalarEnumerationTraits<WasmYAML::ValueType> {
    317   static void enumeration(IO &IO, WasmYAML::ValueType &Type);
    318 };
    319 
    320 template <> struct ScalarEnumerationTraits<WasmYAML::ExportKind> {
    321   static void enumeration(IO &IO, WasmYAML::ExportKind &Kind);
    322 };
    323 
    324 template <> struct ScalarEnumerationTraits<WasmYAML::TableType> {
    325   static void enumeration(IO &IO, WasmYAML::TableType &Type);
    326 };
    327 
    328 template <> struct ScalarEnumerationTraits<WasmYAML::Opcode> {
    329   static void enumeration(IO &IO, WasmYAML::Opcode &Opcode);
    330 };
    331 
    332 template <> struct ScalarEnumerationTraits<WasmYAML::RelocType> {
    333   static void enumeration(IO &IO, WasmYAML::RelocType &Kind);
    334 };
    335 
    336 } // end namespace yaml
    337 } // end namespace llvm
    338 
    339 #endif
    340