Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame^] | 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/ADT/StringRef.h" |
| 20 | #include "llvm/BinaryFormat/Wasm.h" |
| 21 | #include "llvm/ObjectYAML/YAML.h" |
| 22 | #include "llvm/Support/Casting.h" |
| 23 | #include <cstdint> |
| 24 | #include <memory> |
| 25 | #include <vector> |
| 26 | |
| 27 | namespace llvm { |
| 28 | namespace WasmYAML { |
| 29 | |
| 30 | LLVM_YAML_STRONG_TYPEDEF(uint32_t, SectionType) |
| 31 | LLVM_YAML_STRONG_TYPEDEF(uint32_t, ValueType) |
| 32 | LLVM_YAML_STRONG_TYPEDEF(uint32_t, TableType) |
| 33 | LLVM_YAML_STRONG_TYPEDEF(uint32_t, SignatureForm) |
| 34 | LLVM_YAML_STRONG_TYPEDEF(uint32_t, ExportKind) |
| 35 | LLVM_YAML_STRONG_TYPEDEF(uint32_t, Opcode) |
| 36 | LLVM_YAML_STRONG_TYPEDEF(uint32_t, RelocType) |
| 37 | LLVM_YAML_STRONG_TYPEDEF(uint32_t, SymbolFlags) |
| 38 | LLVM_YAML_STRONG_TYPEDEF(uint32_t, SymbolKind) |
| 39 | LLVM_YAML_STRONG_TYPEDEF(uint32_t, SegmentFlags) |
| 40 | LLVM_YAML_STRONG_TYPEDEF(uint32_t, LimitFlags) |
| 41 | LLVM_YAML_STRONG_TYPEDEF(uint32_t, ComdatKind) |
| 42 | |
| 43 | struct FileHeader { |
| 44 | yaml::Hex32 Version; |
| 45 | }; |
| 46 | |
| 47 | struct Limits { |
| 48 | LimitFlags Flags; |
| 49 | yaml::Hex32 Initial; |
| 50 | yaml::Hex32 Maximum; |
| 51 | }; |
| 52 | |
| 53 | struct Table { |
| 54 | TableType ElemType; |
| 55 | Limits TableLimits; |
| 56 | }; |
| 57 | |
| 58 | struct Export { |
| 59 | StringRef Name; |
| 60 | ExportKind Kind; |
| 61 | uint32_t Index; |
| 62 | }; |
| 63 | |
| 64 | struct ElemSegment { |
| 65 | uint32_t TableIndex; |
| 66 | wasm::WasmInitExpr Offset; |
| 67 | std::vector<uint32_t> Functions; |
| 68 | }; |
| 69 | |
| 70 | struct Global { |
| 71 | uint32_t Index; |
| 72 | ValueType Type; |
| 73 | bool Mutable; |
| 74 | wasm::WasmInitExpr InitExpr; |
| 75 | }; |
| 76 | |
| 77 | struct Import { |
| 78 | StringRef Module; |
| 79 | StringRef Field; |
| 80 | ExportKind Kind; |
| 81 | union { |
| 82 | uint32_t SigIndex; |
| 83 | Global GlobalImport; |
| 84 | Table TableImport; |
| 85 | Limits Memory; |
| 86 | }; |
| 87 | }; |
| 88 | |
| 89 | struct LocalDecl { |
| 90 | ValueType Type; |
| 91 | uint32_t Count; |
| 92 | }; |
| 93 | |
| 94 | struct Function { |
| 95 | uint32_t Index; |
| 96 | std::vector<LocalDecl> Locals; |
| 97 | yaml::BinaryRef Body; |
| 98 | }; |
| 99 | |
| 100 | struct Relocation { |
| 101 | RelocType Type; |
| 102 | uint32_t Index; |
| 103 | yaml::Hex32 Offset; |
| 104 | int32_t Addend; |
| 105 | }; |
| 106 | |
| 107 | struct DataSegment { |
| 108 | uint32_t MemoryIndex; |
| 109 | uint32_t SectionOffset; |
| 110 | wasm::WasmInitExpr Offset; |
| 111 | yaml::BinaryRef Content; |
| 112 | }; |
| 113 | |
| 114 | struct NameEntry { |
| 115 | uint32_t Index; |
| 116 | StringRef Name; |
| 117 | }; |
| 118 | |
| 119 | struct SegmentInfo { |
| 120 | uint32_t Index; |
| 121 | StringRef Name; |
| 122 | uint32_t Alignment; |
| 123 | SegmentFlags Flags; |
| 124 | }; |
| 125 | |
| 126 | struct Signature { |
| 127 | uint32_t Index; |
| 128 | SignatureForm Form = wasm::WASM_TYPE_FUNC; |
| 129 | std::vector<ValueType> ParamTypes; |
| 130 | ValueType ReturnType; |
| 131 | }; |
| 132 | |
| 133 | struct SymbolInfo { |
| 134 | uint32_t Index; |
| 135 | StringRef Name; |
| 136 | SymbolKind Kind; |
| 137 | SymbolFlags Flags; |
| 138 | union { |
| 139 | uint32_t ElementIndex; |
| 140 | wasm::WasmDataReference DataRef; |
| 141 | }; |
| 142 | }; |
| 143 | |
| 144 | struct InitFunction { |
| 145 | uint32_t Priority; |
| 146 | uint32_t Symbol; |
| 147 | }; |
| 148 | |
| 149 | struct ComdatEntry { |
| 150 | ComdatKind Kind; |
| 151 | uint32_t Index; |
| 152 | }; |
| 153 | |
| 154 | struct Comdat { |
| 155 | StringRef Name; |
| 156 | std::vector<ComdatEntry> Entries; |
| 157 | }; |
| 158 | |
| 159 | struct Section { |
| 160 | explicit Section(SectionType SecType) : Type(SecType) {} |
| 161 | virtual ~Section(); |
| 162 | |
| 163 | SectionType Type; |
| 164 | std::vector<Relocation> Relocations; |
| 165 | }; |
| 166 | |
| 167 | struct CustomSection : Section { |
| 168 | explicit CustomSection(StringRef Name) |
| 169 | : Section(wasm::WASM_SEC_CUSTOM), Name(Name) {} |
| 170 | |
| 171 | static bool classof(const Section *S) { |
| 172 | return S->Type == wasm::WASM_SEC_CUSTOM; |
| 173 | } |
| 174 | |
| 175 | StringRef Name; |
| 176 | yaml::BinaryRef Payload; |
| 177 | }; |
| 178 | |
| 179 | struct NameSection : CustomSection { |
| 180 | NameSection() : CustomSection("name") {} |
| 181 | |
| 182 | static bool classof(const Section *S) { |
| 183 | auto C = dyn_cast<CustomSection>(S); |
| 184 | return C && C->Name == "name"; |
| 185 | } |
| 186 | |
| 187 | std::vector<NameEntry> FunctionNames; |
| 188 | }; |
| 189 | |
| 190 | struct LinkingSection : CustomSection { |
| 191 | LinkingSection() : CustomSection("linking") {} |
| 192 | |
| 193 | static bool classof(const Section *S) { |
| 194 | auto C = dyn_cast<CustomSection>(S); |
| 195 | return C && C->Name == "linking"; |
| 196 | } |
| 197 | |
| 198 | std::vector<SymbolInfo> SymbolTable; |
| 199 | std::vector<SegmentInfo> SegmentInfos; |
| 200 | std::vector<InitFunction> InitFunctions; |
| 201 | std::vector<Comdat> Comdats; |
| 202 | }; |
| 203 | |
| 204 | struct TypeSection : Section { |
| 205 | TypeSection() : Section(wasm::WASM_SEC_TYPE) {} |
| 206 | |
| 207 | static bool classof(const Section *S) { |
| 208 | return S->Type == wasm::WASM_SEC_TYPE; |
| 209 | } |
| 210 | |
| 211 | std::vector<Signature> Signatures; |
| 212 | }; |
| 213 | |
| 214 | struct ImportSection : Section { |
| 215 | ImportSection() : Section(wasm::WASM_SEC_IMPORT) {} |
| 216 | |
| 217 | static bool classof(const Section *S) { |
| 218 | return S->Type == wasm::WASM_SEC_IMPORT; |
| 219 | } |
| 220 | |
| 221 | std::vector<Import> Imports; |
| 222 | }; |
| 223 | |
| 224 | struct FunctionSection : Section { |
| 225 | FunctionSection() : Section(wasm::WASM_SEC_FUNCTION) {} |
| 226 | |
| 227 | static bool classof(const Section *S) { |
| 228 | return S->Type == wasm::WASM_SEC_FUNCTION; |
| 229 | } |
| 230 | |
| 231 | std::vector<uint32_t> FunctionTypes; |
| 232 | }; |
| 233 | |
| 234 | struct TableSection : Section { |
| 235 | TableSection() : Section(wasm::WASM_SEC_TABLE) {} |
| 236 | |
| 237 | static bool classof(const Section *S) { |
| 238 | return S->Type == wasm::WASM_SEC_TABLE; |
| 239 | } |
| 240 | |
| 241 | std::vector<Table> Tables; |
| 242 | }; |
| 243 | |
| 244 | struct MemorySection : Section { |
| 245 | MemorySection() : Section(wasm::WASM_SEC_MEMORY) {} |
| 246 | |
| 247 | static bool classof(const Section *S) { |
| 248 | return S->Type == wasm::WASM_SEC_MEMORY; |
| 249 | } |
| 250 | |
| 251 | std::vector<Limits> Memories; |
| 252 | }; |
| 253 | |
| 254 | struct GlobalSection : Section { |
| 255 | GlobalSection() : Section(wasm::WASM_SEC_GLOBAL) {} |
| 256 | |
| 257 | static bool classof(const Section *S) { |
| 258 | return S->Type == wasm::WASM_SEC_GLOBAL; |
| 259 | } |
| 260 | |
| 261 | std::vector<Global> Globals; |
| 262 | }; |
| 263 | |
| 264 | struct ExportSection : Section { |
| 265 | ExportSection() : Section(wasm::WASM_SEC_EXPORT) {} |
| 266 | |
| 267 | static bool classof(const Section *S) { |
| 268 | return S->Type == wasm::WASM_SEC_EXPORT; |
| 269 | } |
| 270 | |
| 271 | std::vector<Export> Exports; |
| 272 | }; |
| 273 | |
| 274 | struct StartSection : Section { |
| 275 | StartSection() : Section(wasm::WASM_SEC_START) {} |
| 276 | |
| 277 | static bool classof(const Section *S) { |
| 278 | return S->Type == wasm::WASM_SEC_START; |
| 279 | } |
| 280 | |
| 281 | uint32_t StartFunction; |
| 282 | }; |
| 283 | |
| 284 | struct ElemSection : Section { |
| 285 | ElemSection() : Section(wasm::WASM_SEC_ELEM) {} |
| 286 | |
| 287 | static bool classof(const Section *S) { |
| 288 | return S->Type == wasm::WASM_SEC_ELEM; |
| 289 | } |
| 290 | |
| 291 | std::vector<ElemSegment> Segments; |
| 292 | }; |
| 293 | |
| 294 | struct CodeSection : Section { |
| 295 | CodeSection() : Section(wasm::WASM_SEC_CODE) {} |
| 296 | |
| 297 | static bool classof(const Section *S) { |
| 298 | return S->Type == wasm::WASM_SEC_CODE; |
| 299 | } |
| 300 | |
| 301 | std::vector<Function> Functions; |
| 302 | }; |
| 303 | |
| 304 | struct DataSection : Section { |
| 305 | DataSection() : Section(wasm::WASM_SEC_DATA) {} |
| 306 | |
| 307 | static bool classof(const Section *S) { |
| 308 | return S->Type == wasm::WASM_SEC_DATA; |
| 309 | } |
| 310 | |
| 311 | std::vector<DataSegment> Segments; |
| 312 | }; |
| 313 | |
| 314 | struct Object { |
| 315 | FileHeader Header; |
| 316 | std::vector<std::unique_ptr<Section>> Sections; |
| 317 | }; |
| 318 | |
| 319 | } // end namespace WasmYAML |
| 320 | } // end namespace llvm |
| 321 | |
| 322 | LLVM_YAML_IS_SEQUENCE_VECTOR(std::unique_ptr<llvm::WasmYAML::Section>) |
| 323 | LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Signature) |
| 324 | LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::ValueType) |
| 325 | LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Table) |
| 326 | LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Import) |
| 327 | LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Export) |
| 328 | LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::ElemSegment) |
| 329 | LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Limits) |
| 330 | LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::DataSegment) |
| 331 | LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Global) |
| 332 | LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Function) |
| 333 | LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::LocalDecl) |
| 334 | LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Relocation) |
| 335 | LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::NameEntry) |
| 336 | LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::SegmentInfo) |
| 337 | LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::SymbolInfo) |
| 338 | LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::InitFunction) |
| 339 | LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::ComdatEntry) |
| 340 | LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Comdat) |
| 341 | |
| 342 | namespace llvm { |
| 343 | namespace yaml { |
| 344 | |
| 345 | template <> struct MappingTraits<WasmYAML::FileHeader> { |
| 346 | static void mapping(IO &IO, WasmYAML::FileHeader &FileHdr); |
| 347 | }; |
| 348 | |
| 349 | template <> struct MappingTraits<std::unique_ptr<WasmYAML::Section>> { |
| 350 | static void mapping(IO &IO, std::unique_ptr<WasmYAML::Section> &Section); |
| 351 | }; |
| 352 | |
| 353 | template <> struct MappingTraits<WasmYAML::Object> { |
| 354 | static void mapping(IO &IO, WasmYAML::Object &Object); |
| 355 | }; |
| 356 | |
| 357 | template <> struct MappingTraits<WasmYAML::Import> { |
| 358 | static void mapping(IO &IO, WasmYAML::Import &Import); |
| 359 | }; |
| 360 | |
| 361 | template <> struct MappingTraits<WasmYAML::Export> { |
| 362 | static void mapping(IO &IO, WasmYAML::Export &Export); |
| 363 | }; |
| 364 | |
| 365 | template <> struct MappingTraits<WasmYAML::Global> { |
| 366 | static void mapping(IO &IO, WasmYAML::Global &Global); |
| 367 | }; |
| 368 | |
| 369 | template <> struct ScalarBitSetTraits<WasmYAML::LimitFlags> { |
| 370 | static void bitset(IO &IO, WasmYAML::LimitFlags &Value); |
| 371 | }; |
| 372 | |
| 373 | template <> struct ScalarBitSetTraits<WasmYAML::SymbolFlags> { |
| 374 | static void bitset(IO &IO, WasmYAML::SymbolFlags &Value); |
| 375 | }; |
| 376 | |
| 377 | template <> struct ScalarEnumerationTraits<WasmYAML::SymbolKind> { |
| 378 | static void enumeration(IO &IO, WasmYAML::SymbolKind &Kind); |
| 379 | }; |
| 380 | |
| 381 | template <> struct ScalarBitSetTraits<WasmYAML::SegmentFlags> { |
| 382 | static void bitset(IO &IO, WasmYAML::SegmentFlags &Value); |
| 383 | }; |
| 384 | |
| 385 | template <> struct ScalarEnumerationTraits<WasmYAML::SectionType> { |
| 386 | static void enumeration(IO &IO, WasmYAML::SectionType &Type); |
| 387 | }; |
| 388 | |
| 389 | template <> struct MappingTraits<WasmYAML::Signature> { |
| 390 | static void mapping(IO &IO, WasmYAML::Signature &Signature); |
| 391 | }; |
| 392 | |
| 393 | template <> struct MappingTraits<WasmYAML::Table> { |
| 394 | static void mapping(IO &IO, WasmYAML::Table &Table); |
| 395 | }; |
| 396 | |
| 397 | template <> struct MappingTraits<WasmYAML::Limits> { |
| 398 | static void mapping(IO &IO, WasmYAML::Limits &Limits); |
| 399 | }; |
| 400 | |
| 401 | template <> struct MappingTraits<WasmYAML::Function> { |
| 402 | static void mapping(IO &IO, WasmYAML::Function &Function); |
| 403 | }; |
| 404 | |
| 405 | template <> struct MappingTraits<WasmYAML::Relocation> { |
| 406 | static void mapping(IO &IO, WasmYAML::Relocation &Relocation); |
| 407 | }; |
| 408 | |
| 409 | template <> struct MappingTraits<WasmYAML::NameEntry> { |
| 410 | static void mapping(IO &IO, WasmYAML::NameEntry &NameEntry); |
| 411 | }; |
| 412 | |
| 413 | template <> struct MappingTraits<WasmYAML::SegmentInfo> { |
| 414 | static void mapping(IO &IO, WasmYAML::SegmentInfo &SegmentInfo); |
| 415 | }; |
| 416 | |
| 417 | template <> struct MappingTraits<WasmYAML::LocalDecl> { |
| 418 | static void mapping(IO &IO, WasmYAML::LocalDecl &LocalDecl); |
| 419 | }; |
| 420 | |
| 421 | template <> struct MappingTraits<wasm::WasmInitExpr> { |
| 422 | static void mapping(IO &IO, wasm::WasmInitExpr &Expr); |
| 423 | }; |
| 424 | |
| 425 | template <> struct MappingTraits<WasmYAML::DataSegment> { |
| 426 | static void mapping(IO &IO, WasmYAML::DataSegment &Segment); |
| 427 | }; |
| 428 | |
| 429 | template <> struct MappingTraits<WasmYAML::ElemSegment> { |
| 430 | static void mapping(IO &IO, WasmYAML::ElemSegment &Segment); |
| 431 | }; |
| 432 | |
| 433 | template <> struct MappingTraits<WasmYAML::SymbolInfo> { |
| 434 | static void mapping(IO &IO, WasmYAML::SymbolInfo &Info); |
| 435 | }; |
| 436 | |
| 437 | template <> struct MappingTraits<WasmYAML::InitFunction> { |
| 438 | static void mapping(IO &IO, WasmYAML::InitFunction &Init); |
| 439 | }; |
| 440 | |
| 441 | template <> struct ScalarEnumerationTraits<WasmYAML::ComdatKind> { |
| 442 | static void enumeration(IO &IO, WasmYAML::ComdatKind &Kind); |
| 443 | }; |
| 444 | |
| 445 | template <> struct MappingTraits<WasmYAML::ComdatEntry> { |
| 446 | static void mapping(IO &IO, WasmYAML::ComdatEntry &ComdatEntry); |
| 447 | }; |
| 448 | |
| 449 | template <> struct MappingTraits<WasmYAML::Comdat> { |
| 450 | static void mapping(IO &IO, WasmYAML::Comdat &Comdat); |
| 451 | }; |
| 452 | |
| 453 | template <> struct ScalarEnumerationTraits<WasmYAML::ValueType> { |
| 454 | static void enumeration(IO &IO, WasmYAML::ValueType &Type); |
| 455 | }; |
| 456 | |
| 457 | template <> struct ScalarEnumerationTraits<WasmYAML::ExportKind> { |
| 458 | static void enumeration(IO &IO, WasmYAML::ExportKind &Kind); |
| 459 | }; |
| 460 | |
| 461 | template <> struct ScalarEnumerationTraits<WasmYAML::TableType> { |
| 462 | static void enumeration(IO &IO, WasmYAML::TableType &Type); |
| 463 | }; |
| 464 | |
| 465 | template <> struct ScalarEnumerationTraits<WasmYAML::Opcode> { |
| 466 | static void enumeration(IO &IO, WasmYAML::Opcode &Opcode); |
| 467 | }; |
| 468 | |
| 469 | template <> struct ScalarEnumerationTraits<WasmYAML::RelocType> { |
| 470 | static void enumeration(IO &IO, WasmYAML::RelocType &Kind); |
| 471 | }; |
| 472 | |
| 473 | } // end namespace yaml |
| 474 | } // end namespace llvm |
| 475 | |
| 476 | #endif // LLVM_OBJECTYAML_WASMYAML_H |