1 //===- WasmYAML.cpp - Wasm YAMLIO implementation --------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file defines classes for handling the YAML representation of wasm. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "llvm/ObjectYAML/WasmYAML.h" 14 #include "llvm/ADT/StringRef.h" 15 #include "llvm/BinaryFormat/Wasm.h" 16 #include "llvm/Support/Casting.h" 17 #include "llvm/Support/ErrorHandling.h" 18 #include "llvm/Support/YAMLTraits.h" 19 20 namespace llvm { 21 22 namespace WasmYAML { 23 24 // Declared here rather than in the header to comply with: 25 // http://llvm.org/docs/CodingStandards.html#provide-a-virtual-method-anchor-for-classes-in-headers 26 Section::~Section() = default; 27 28 } // end namespace WasmYAML 29 30 namespace yaml { 31 32 void MappingTraits<WasmYAML::FileHeader>::mapping( 33 IO &IO, WasmYAML::FileHeader &FileHdr) { 34 IO.mapRequired("Version", FileHdr.Version); 35 } 36 37 void MappingTraits<WasmYAML::Object>::mapping(IO &IO, 38 WasmYAML::Object &Object) { 39 IO.setContext(&Object); 40 IO.mapTag("!WASM", true); 41 IO.mapRequired("FileHeader", Object.Header); 42 IO.mapOptional("Sections", Object.Sections); 43 IO.setContext(nullptr); 44 } 45 46 static void commonSectionMapping(IO &IO, WasmYAML::Section &Section) { 47 IO.mapRequired("Type", Section.Type); 48 IO.mapOptional("Relocations", Section.Relocations); 49 IO.mapOptional("HeaderSecSizeEncodingLen", Section.HeaderSecSizeEncodingLen); 50 } 51 52 static void sectionMapping(IO &IO, WasmYAML::DylinkSection &Section) { 53 commonSectionMapping(IO, Section); 54 IO.mapRequired("Name", Section.Name); 55 IO.mapRequired("MemorySize", Section.MemorySize); 56 IO.mapRequired("MemoryAlignment", Section.MemoryAlignment); 57 IO.mapRequired("TableSize", Section.TableSize); 58 IO.mapRequired("TableAlignment", Section.TableAlignment); 59 IO.mapRequired("Needed", Section.Needed); 60 IO.mapOptional("ImportInfo", Section.ImportInfo); 61 IO.mapOptional("ExportInfo", Section.ExportInfo); 62 } 63 64 static void sectionMapping(IO &IO, WasmYAML::NameSection &Section) { 65 commonSectionMapping(IO, Section); 66 IO.mapRequired("Name", Section.Name); 67 IO.mapOptional("FunctionNames", Section.FunctionNames); 68 IO.mapOptional("GlobalNames", Section.GlobalNames); 69 IO.mapOptional("DataSegmentNames", Section.DataSegmentNames); 70 } 71 72 static void sectionMapping(IO &IO, WasmYAML::LinkingSection &Section) { 73 commonSectionMapping(IO, Section); 74 IO.mapRequired("Name", Section.Name); 75 IO.mapRequired("Version", Section.Version); 76 IO.mapOptional("SymbolTable", Section.SymbolTable); 77 IO.mapOptional("SegmentInfo", Section.SegmentInfos); 78 IO.mapOptional("InitFunctions", Section.InitFunctions); 79 IO.mapOptional("Comdats", Section.Comdats); 80 } 81 82 static void sectionMapping(IO &IO, WasmYAML::ProducersSection &Section) { 83 commonSectionMapping(IO, Section); 84 IO.mapRequired("Name", Section.Name); 85 IO.mapOptional("Languages", Section.Languages); 86 IO.mapOptional("Tools", Section.Tools); 87 IO.mapOptional("SDKs", Section.SDKs); 88 } 89 90 static void sectionMapping(IO &IO, WasmYAML::TargetFeaturesSection &Section) { 91 commonSectionMapping(IO, Section); 92 IO.mapRequired("Name", Section.Name); 93 IO.mapRequired("Features", Section.Features); 94 } 95 96 static void sectionMapping(IO &IO, WasmYAML::CustomSection &Section) { 97 commonSectionMapping(IO, Section); 98 IO.mapRequired("Name", Section.Name); 99 IO.mapRequired("Payload", Section.Payload); 100 } 101 102 static void sectionMapping(IO &IO, WasmYAML::TypeSection &Section) { 103 commonSectionMapping(IO, Section); 104 IO.mapOptional("Signatures", Section.Signatures); 105 } 106 107 static void sectionMapping(IO &IO, WasmYAML::ImportSection &Section) { 108 commonSectionMapping(IO, Section); 109 IO.mapOptional("Imports", Section.Imports); 110 } 111 112 static void sectionMapping(IO &IO, WasmYAML::FunctionSection &Section) { 113 commonSectionMapping(IO, Section); 114 IO.mapOptional("FunctionTypes", Section.FunctionTypes); 115 } 116 117 static void sectionMapping(IO &IO, WasmYAML::TableSection &Section) { 118 commonSectionMapping(IO, Section); 119 IO.mapOptional("Tables", Section.Tables); 120 } 121 122 static void sectionMapping(IO &IO, WasmYAML::MemorySection &Section) { 123 commonSectionMapping(IO, Section); 124 IO.mapOptional("Memories", Section.Memories); 125 } 126 127 static void sectionMapping(IO &IO, WasmYAML::TagSection &Section) { 128 commonSectionMapping(IO, Section); 129 IO.mapOptional("TagTypes", Section.TagTypes); 130 } 131 132 static void sectionMapping(IO &IO, WasmYAML::GlobalSection &Section) { 133 commonSectionMapping(IO, Section); 134 IO.mapOptional("Globals", Section.Globals); 135 } 136 137 static void sectionMapping(IO &IO, WasmYAML::ExportSection &Section) { 138 commonSectionMapping(IO, Section); 139 IO.mapOptional("Exports", Section.Exports); 140 } 141 142 static void sectionMapping(IO &IO, WasmYAML::StartSection &Section) { 143 commonSectionMapping(IO, Section); 144 IO.mapOptional("StartFunction", Section.StartFunction); 145 } 146 147 static void sectionMapping(IO &IO, WasmYAML::ElemSection &Section) { 148 commonSectionMapping(IO, Section); 149 IO.mapOptional("Segments", Section.Segments); 150 } 151 152 static void sectionMapping(IO &IO, WasmYAML::CodeSection &Section) { 153 commonSectionMapping(IO, Section); 154 IO.mapRequired("Functions", Section.Functions); 155 } 156 157 static void sectionMapping(IO &IO, WasmYAML::DataSection &Section) { 158 commonSectionMapping(IO, Section); 159 IO.mapRequired("Segments", Section.Segments); 160 } 161 162 static void sectionMapping(IO &IO, WasmYAML::DataCountSection &Section) { 163 commonSectionMapping(IO, Section); 164 IO.mapRequired("Count", Section.Count); 165 } 166 167 void MappingTraits<std::unique_ptr<WasmYAML::Section>>::mapping( 168 IO &IO, std::unique_ptr<WasmYAML::Section> &Section) { 169 WasmYAML::SectionType SectionType; 170 if (IO.outputting()) 171 SectionType = Section->Type; 172 else 173 IO.mapRequired("Type", SectionType); 174 175 switch (SectionType) { 176 case wasm::WASM_SEC_CUSTOM: { 177 StringRef SectionName; 178 if (IO.outputting()) { 179 auto CustomSection = cast<WasmYAML::CustomSection>(Section.get()); 180 SectionName = CustomSection->Name; 181 } else { 182 IO.mapRequired("Name", SectionName); 183 } 184 if (SectionName == "dylink" || SectionName == "dylink.0") { 185 if (!IO.outputting()) 186 Section.reset(new WasmYAML::DylinkSection()); 187 sectionMapping(IO, *cast<WasmYAML::DylinkSection>(Section.get())); 188 } else if (SectionName == "linking") { 189 if (!IO.outputting()) 190 Section.reset(new WasmYAML::LinkingSection()); 191 sectionMapping(IO, *cast<WasmYAML::LinkingSection>(Section.get())); 192 } else if (SectionName == "name") { 193 if (!IO.outputting()) 194 Section.reset(new WasmYAML::NameSection()); 195 sectionMapping(IO, *cast<WasmYAML::NameSection>(Section.get())); 196 } else if (SectionName == "producers") { 197 if (!IO.outputting()) 198 Section.reset(new WasmYAML::ProducersSection()); 199 sectionMapping(IO, *cast<WasmYAML::ProducersSection>(Section.get())); 200 } else if (SectionName == "target_features") { 201 if (!IO.outputting()) 202 Section.reset(new WasmYAML::TargetFeaturesSection()); 203 sectionMapping(IO, *cast<WasmYAML::TargetFeaturesSection>(Section.get())); 204 } else { 205 if (!IO.outputting()) 206 Section.reset(new WasmYAML::CustomSection(SectionName)); 207 sectionMapping(IO, *cast<WasmYAML::CustomSection>(Section.get())); 208 } 209 break; 210 } 211 case wasm::WASM_SEC_TYPE: 212 if (!IO.outputting()) 213 Section.reset(new WasmYAML::TypeSection()); 214 sectionMapping(IO, *cast<WasmYAML::TypeSection>(Section.get())); 215 break; 216 case wasm::WASM_SEC_IMPORT: 217 if (!IO.outputting()) 218 Section.reset(new WasmYAML::ImportSection()); 219 sectionMapping(IO, *cast<WasmYAML::ImportSection>(Section.get())); 220 break; 221 case wasm::WASM_SEC_FUNCTION: 222 if (!IO.outputting()) 223 Section.reset(new WasmYAML::FunctionSection()); 224 sectionMapping(IO, *cast<WasmYAML::FunctionSection>(Section.get())); 225 break; 226 case wasm::WASM_SEC_TABLE: 227 if (!IO.outputting()) 228 Section.reset(new WasmYAML::TableSection()); 229 sectionMapping(IO, *cast<WasmYAML::TableSection>(Section.get())); 230 break; 231 case wasm::WASM_SEC_MEMORY: 232 if (!IO.outputting()) 233 Section.reset(new WasmYAML::MemorySection()); 234 sectionMapping(IO, *cast<WasmYAML::MemorySection>(Section.get())); 235 break; 236 case wasm::WASM_SEC_TAG: 237 if (!IO.outputting()) 238 Section.reset(new WasmYAML::TagSection()); 239 sectionMapping(IO, *cast<WasmYAML::TagSection>(Section.get())); 240 break; 241 case wasm::WASM_SEC_GLOBAL: 242 if (!IO.outputting()) 243 Section.reset(new WasmYAML::GlobalSection()); 244 sectionMapping(IO, *cast<WasmYAML::GlobalSection>(Section.get())); 245 break; 246 case wasm::WASM_SEC_EXPORT: 247 if (!IO.outputting()) 248 Section.reset(new WasmYAML::ExportSection()); 249 sectionMapping(IO, *cast<WasmYAML::ExportSection>(Section.get())); 250 break; 251 case wasm::WASM_SEC_START: 252 if (!IO.outputting()) 253 Section.reset(new WasmYAML::StartSection()); 254 sectionMapping(IO, *cast<WasmYAML::StartSection>(Section.get())); 255 break; 256 case wasm::WASM_SEC_ELEM: 257 if (!IO.outputting()) 258 Section.reset(new WasmYAML::ElemSection()); 259 sectionMapping(IO, *cast<WasmYAML::ElemSection>(Section.get())); 260 break; 261 case wasm::WASM_SEC_CODE: 262 if (!IO.outputting()) 263 Section.reset(new WasmYAML::CodeSection()); 264 sectionMapping(IO, *cast<WasmYAML::CodeSection>(Section.get())); 265 break; 266 case wasm::WASM_SEC_DATA: 267 if (!IO.outputting()) 268 Section.reset(new WasmYAML::DataSection()); 269 sectionMapping(IO, *cast<WasmYAML::DataSection>(Section.get())); 270 break; 271 case wasm::WASM_SEC_DATACOUNT: 272 if (!IO.outputting()) 273 Section.reset(new WasmYAML::DataCountSection()); 274 sectionMapping(IO, *cast<WasmYAML::DataCountSection>(Section.get())); 275 break; 276 default: 277 llvm_unreachable("Unknown section type"); 278 } 279 } 280 281 void ScalarEnumerationTraits<WasmYAML::SectionType>::enumeration( 282 IO &IO, WasmYAML::SectionType &Type) { 283 #define ECase(X) IO.enumCase(Type, #X, wasm::WASM_SEC_##X); 284 ECase(CUSTOM); 285 ECase(TYPE); 286 ECase(IMPORT); 287 ECase(FUNCTION); 288 ECase(TABLE); 289 ECase(MEMORY); 290 ECase(GLOBAL); 291 ECase(TAG); 292 ECase(EXPORT); 293 ECase(START); 294 ECase(ELEM); 295 ECase(CODE); 296 ECase(DATA); 297 ECase(DATACOUNT); 298 #undef ECase 299 } 300 301 void MappingTraits<WasmYAML::Signature>::mapping( 302 IO &IO, WasmYAML::Signature &Signature) { 303 IO.mapRequired("Index", Signature.Index); 304 IO.mapRequired("ParamTypes", Signature.ParamTypes); 305 IO.mapRequired("ReturnTypes", Signature.ReturnTypes); 306 } 307 308 void MappingTraits<WasmYAML::Table>::mapping(IO &IO, WasmYAML::Table &Table) { 309 IO.mapRequired("Index", Table.Index); 310 IO.mapRequired("ElemType", Table.ElemType); 311 IO.mapRequired("Limits", Table.TableLimits); 312 } 313 314 void MappingTraits<WasmYAML::Function>::mapping(IO &IO, 315 WasmYAML::Function &Function) { 316 IO.mapRequired("Index", Function.Index); 317 IO.mapRequired("Locals", Function.Locals); 318 IO.mapRequired("Body", Function.Body); 319 } 320 321 void MappingTraits<WasmYAML::Relocation>::mapping( 322 IO &IO, WasmYAML::Relocation &Relocation) { 323 IO.mapRequired("Type", Relocation.Type); 324 IO.mapRequired("Index", Relocation.Index); 325 IO.mapRequired("Offset", Relocation.Offset); 326 IO.mapOptional("Addend", Relocation.Addend, 0); 327 } 328 329 void MappingTraits<WasmYAML::NameEntry>::mapping( 330 IO &IO, WasmYAML::NameEntry &NameEntry) { 331 IO.mapRequired("Index", NameEntry.Index); 332 IO.mapRequired("Name", NameEntry.Name); 333 } 334 335 void MappingTraits<WasmYAML::ProducerEntry>::mapping( 336 IO &IO, WasmYAML::ProducerEntry &ProducerEntry) { 337 IO.mapRequired("Name", ProducerEntry.Name); 338 IO.mapRequired("Version", ProducerEntry.Version); 339 } 340 341 void ScalarEnumerationTraits<WasmYAML::FeaturePolicyPrefix>::enumeration( 342 IO &IO, WasmYAML::FeaturePolicyPrefix &Kind) { 343 #define ECase(X) IO.enumCase(Kind, #X, wasm::WASM_FEATURE_PREFIX_##X); 344 ECase(USED); 345 ECase(DISALLOWED); 346 #undef ECase 347 } 348 349 void MappingTraits<WasmYAML::FeatureEntry>::mapping( 350 IO &IO, WasmYAML::FeatureEntry &FeatureEntry) { 351 IO.mapRequired("Prefix", FeatureEntry.Prefix); 352 IO.mapRequired("Name", FeatureEntry.Name); 353 } 354 355 void MappingTraits<WasmYAML::SegmentInfo>::mapping( 356 IO &IO, WasmYAML::SegmentInfo &SegmentInfo) { 357 IO.mapRequired("Index", SegmentInfo.Index); 358 IO.mapRequired("Name", SegmentInfo.Name); 359 IO.mapRequired("Alignment", SegmentInfo.Alignment); 360 IO.mapRequired("Flags", SegmentInfo.Flags); 361 } 362 363 void MappingTraits<WasmYAML::LocalDecl>::mapping( 364 IO &IO, WasmYAML::LocalDecl &LocalDecl) { 365 IO.mapRequired("Type", LocalDecl.Type); 366 IO.mapRequired("Count", LocalDecl.Count); 367 } 368 369 void MappingTraits<WasmYAML::Limits>::mapping(IO &IO, 370 WasmYAML::Limits &Limits) { 371 IO.mapOptional("Flags", Limits.Flags, 0); 372 IO.mapRequired("Minimum", Limits.Minimum); 373 if (!IO.outputting() || Limits.Flags & wasm::WASM_LIMITS_FLAG_HAS_MAX) 374 IO.mapOptional("Maximum", Limits.Maximum); 375 } 376 377 void MappingTraits<WasmYAML::ElemSegment>::mapping( 378 IO &IO, WasmYAML::ElemSegment &Segment) { 379 IO.mapOptional("Flags", Segment.Flags, 0); 380 if (!IO.outputting() || 381 Segment.Flags & wasm::WASM_ELEM_SEGMENT_HAS_TABLE_NUMBER) 382 IO.mapOptional("TableNumber", Segment.TableNumber); 383 if (!IO.outputting() || 384 Segment.Flags & wasm::WASM_ELEM_SEGMENT_MASK_HAS_ELEM_DESC) 385 IO.mapOptional("ElemKind", Segment.ElemKind); 386 // TODO: Omit "offset" for passive segments? It's neither meaningful nor 387 // encoded. 388 IO.mapRequired("Offset", Segment.Offset); 389 IO.mapRequired("Functions", Segment.Functions); 390 } 391 392 void MappingTraits<WasmYAML::Import>::mapping(IO &IO, 393 WasmYAML::Import &Import) { 394 IO.mapRequired("Module", Import.Module); 395 IO.mapRequired("Field", Import.Field); 396 IO.mapRequired("Kind", Import.Kind); 397 if (Import.Kind == wasm::WASM_EXTERNAL_FUNCTION || 398 Import.Kind == wasm::WASM_EXTERNAL_TAG) { 399 IO.mapRequired("SigIndex", Import.SigIndex); 400 } else if (Import.Kind == wasm::WASM_EXTERNAL_GLOBAL) { 401 IO.mapRequired("GlobalType", Import.GlobalImport.Type); 402 IO.mapRequired("GlobalMutable", Import.GlobalImport.Mutable); 403 } else if (Import.Kind == wasm::WASM_EXTERNAL_TABLE) { 404 IO.mapRequired("Table", Import.TableImport); 405 } else if (Import.Kind == wasm::WASM_EXTERNAL_MEMORY) { 406 IO.mapRequired("Memory", Import.Memory); 407 } else { 408 llvm_unreachable("unhandled import type"); 409 } 410 } 411 412 void MappingTraits<WasmYAML::Export>::mapping(IO &IO, 413 WasmYAML::Export &Export) { 414 IO.mapRequired("Name", Export.Name); 415 IO.mapRequired("Kind", Export.Kind); 416 IO.mapRequired("Index", Export.Index); 417 } 418 419 void MappingTraits<WasmYAML::Global>::mapping(IO &IO, 420 WasmYAML::Global &Global) { 421 IO.mapRequired("Index", Global.Index); 422 IO.mapRequired("Type", Global.Type); 423 IO.mapRequired("Mutable", Global.Mutable); 424 IO.mapRequired("InitExpr", Global.Init); 425 } 426 427 void MappingTraits<WasmYAML::InitExpr>::mapping(IO &IO, 428 WasmYAML::InitExpr &Expr) { 429 IO.mapOptional("Extended", Expr.Extended, false); 430 if (Expr.Extended) { 431 IO.mapRequired("Body", Expr.Body); 432 } else { 433 WasmYAML::Opcode Op = Expr.Inst.Opcode; 434 IO.mapRequired("Opcode", Op); 435 Expr.Inst.Opcode = Op; 436 switch (Expr.Inst.Opcode) { 437 case wasm::WASM_OPCODE_I32_CONST: 438 IO.mapRequired("Value", Expr.Inst.Value.Int32); 439 break; 440 case wasm::WASM_OPCODE_I64_CONST: 441 IO.mapRequired("Value", Expr.Inst.Value.Int64); 442 break; 443 case wasm::WASM_OPCODE_F32_CONST: 444 IO.mapRequired("Value", Expr.Inst.Value.Float32); 445 break; 446 case wasm::WASM_OPCODE_F64_CONST: 447 IO.mapRequired("Value", Expr.Inst.Value.Float64); 448 break; 449 case wasm::WASM_OPCODE_GLOBAL_GET: 450 IO.mapRequired("Index", Expr.Inst.Value.Global); 451 break; 452 case wasm::WASM_OPCODE_REF_NULL: { 453 WasmYAML::ValueType Ty = wasm::WASM_TYPE_EXTERNREF; 454 IO.mapRequired("Type", Ty); 455 break; 456 } 457 } 458 } 459 } 460 461 void MappingTraits<WasmYAML::DataSegment>::mapping( 462 IO &IO, WasmYAML::DataSegment &Segment) { 463 IO.mapOptional("SectionOffset", Segment.SectionOffset); 464 IO.mapRequired("InitFlags", Segment.InitFlags); 465 if (Segment.InitFlags & wasm::WASM_DATA_SEGMENT_HAS_MEMINDEX) { 466 IO.mapRequired("MemoryIndex", Segment.MemoryIndex); 467 } else { 468 Segment.MemoryIndex = 0; 469 } 470 if ((Segment.InitFlags & wasm::WASM_DATA_SEGMENT_IS_PASSIVE) == 0) { 471 IO.mapRequired("Offset", Segment.Offset); 472 } else { 473 Segment.Offset.Inst.Opcode = wasm::WASM_OPCODE_I32_CONST; 474 Segment.Offset.Inst.Value.Int32 = 0; 475 } 476 IO.mapRequired("Content", Segment.Content); 477 } 478 479 void MappingTraits<WasmYAML::InitFunction>::mapping( 480 IO &IO, WasmYAML::InitFunction &Init) { 481 IO.mapRequired("Priority", Init.Priority); 482 IO.mapRequired("Symbol", Init.Symbol); 483 } 484 485 void ScalarEnumerationTraits<WasmYAML::ComdatKind>::enumeration( 486 IO &IO, WasmYAML::ComdatKind &Kind) { 487 #define ECase(X) IO.enumCase(Kind, #X, wasm::WASM_COMDAT_##X); 488 ECase(FUNCTION); 489 ECase(DATA); 490 ECase(SECTION); 491 #undef ECase 492 } 493 494 void MappingTraits<WasmYAML::ComdatEntry>::mapping( 495 IO &IO, WasmYAML::ComdatEntry &ComdatEntry) { 496 IO.mapRequired("Kind", ComdatEntry.Kind); 497 IO.mapRequired("Index", ComdatEntry.Index); 498 } 499 500 void MappingTraits<WasmYAML::Comdat>::mapping(IO &IO, 501 WasmYAML::Comdat &Comdat) { 502 IO.mapRequired("Name", Comdat.Name); 503 IO.mapRequired("Entries", Comdat.Entries); 504 } 505 506 void MappingTraits<WasmYAML::SymbolInfo>::mapping(IO &IO, 507 WasmYAML::SymbolInfo &Info) { 508 IO.mapRequired("Index", Info.Index); 509 IO.mapRequired("Kind", Info.Kind); 510 if (Info.Kind != wasm::WASM_SYMBOL_TYPE_SECTION) 511 IO.mapRequired("Name", Info.Name); 512 IO.mapRequired("Flags", Info.Flags); 513 if (Info.Kind == wasm::WASM_SYMBOL_TYPE_FUNCTION) { 514 IO.mapRequired("Function", Info.ElementIndex); 515 } else if (Info.Kind == wasm::WASM_SYMBOL_TYPE_GLOBAL) { 516 IO.mapRequired("Global", Info.ElementIndex); 517 } else if (Info.Kind == wasm::WASM_SYMBOL_TYPE_TABLE) { 518 IO.mapRequired("Table", Info.ElementIndex); 519 } else if (Info.Kind == wasm::WASM_SYMBOL_TYPE_TAG) { 520 IO.mapRequired("Tag", Info.ElementIndex); 521 } else if (Info.Kind == wasm::WASM_SYMBOL_TYPE_DATA) { 522 if ((Info.Flags & wasm::WASM_SYMBOL_UNDEFINED) == 0) { 523 if ((Info.Flags & wasm::WASM_SYMBOL_ABSOLUTE) == 0) { 524 IO.mapRequired("Segment", Info.DataRef.Segment); 525 } 526 IO.mapOptional("Offset", Info.DataRef.Offset, 0u); 527 IO.mapRequired("Size", Info.DataRef.Size); 528 } 529 } else if (Info.Kind == wasm::WASM_SYMBOL_TYPE_SECTION) { 530 IO.mapRequired("Section", Info.ElementIndex); 531 } else { 532 llvm_unreachable("unsupported symbol kind"); 533 } 534 } 535 536 void MappingTraits<WasmYAML::DylinkImportInfo>::mapping( 537 IO &IO, WasmYAML::DylinkImportInfo &Info) { 538 IO.mapRequired("Module", Info.Module); 539 IO.mapRequired("Field", Info.Field); 540 IO.mapRequired("Flags", Info.Flags); 541 } 542 543 void MappingTraits<WasmYAML::DylinkExportInfo>::mapping( 544 IO &IO, WasmYAML::DylinkExportInfo &Info) { 545 IO.mapRequired("Name", Info.Name); 546 IO.mapRequired("Flags", Info.Flags); 547 } 548 549 void ScalarBitSetTraits<WasmYAML::LimitFlags>::bitset( 550 IO &IO, WasmYAML::LimitFlags &Value) { 551 #define BCase(X) IO.bitSetCase(Value, #X, wasm::WASM_LIMITS_FLAG_##X) 552 BCase(HAS_MAX); 553 BCase(IS_SHARED); 554 BCase(IS_64); 555 #undef BCase 556 } 557 558 void ScalarBitSetTraits<WasmYAML::SegmentFlags>::bitset( 559 IO &IO, WasmYAML::SegmentFlags &Value) { 560 #define BCase(X) IO.bitSetCase(Value, #X, wasm::WASM_SEG_FLAG_##X) 561 BCase(STRINGS); 562 BCase(TLS); 563 BCase(RETAIN); 564 #undef BCase 565 } 566 567 void ScalarBitSetTraits<WasmYAML::SymbolFlags>::bitset( 568 IO &IO, WasmYAML::SymbolFlags &Value) { 569 #define BCaseMask(M, X) \ 570 IO.maskedBitSetCase(Value, #X, wasm::WASM_SYMBOL_##X, wasm::WASM_SYMBOL_##M) 571 // BCaseMask(BINDING_MASK, BINDING_GLOBAL); 572 BCaseMask(BINDING_MASK, BINDING_WEAK); 573 BCaseMask(BINDING_MASK, BINDING_LOCAL); 574 // BCaseMask(VISIBILITY_MASK, VISIBILITY_DEFAULT); 575 BCaseMask(VISIBILITY_MASK, VISIBILITY_HIDDEN); 576 BCaseMask(UNDEFINED, UNDEFINED); 577 BCaseMask(EXPORTED, EXPORTED); 578 BCaseMask(EXPLICIT_NAME, EXPLICIT_NAME); 579 BCaseMask(NO_STRIP, NO_STRIP); 580 BCaseMask(TLS, TLS); 581 BCaseMask(ABSOLUTE, ABSOLUTE); 582 #undef BCaseMask 583 } 584 585 void ScalarEnumerationTraits<WasmYAML::SymbolKind>::enumeration( 586 IO &IO, WasmYAML::SymbolKind &Kind) { 587 #define ECase(X) IO.enumCase(Kind, #X, wasm::WASM_SYMBOL_TYPE_##X); 588 ECase(FUNCTION); 589 ECase(DATA); 590 ECase(GLOBAL); 591 ECase(TABLE); 592 ECase(SECTION); 593 ECase(TAG); 594 #undef ECase 595 } 596 597 void ScalarEnumerationTraits<WasmYAML::ValueType>::enumeration( 598 IO &IO, WasmYAML::ValueType &Type) { 599 #define CONCAT(X) (uint32_t) wasm::ValType::X 600 #define ECase(X) IO.enumCase(Type, #X, CONCAT(X)); 601 ECase(I32); 602 ECase(I64); 603 ECase(F32); 604 ECase(F64); 605 ECase(V128); 606 ECase(FUNCREF); 607 ECase(EXTERNREF); 608 ECase(EXNREF); 609 ECase(OTHERREF); 610 #undef ECase 611 } 612 613 void ScalarEnumerationTraits<WasmYAML::ExportKind>::enumeration( 614 IO &IO, WasmYAML::ExportKind &Kind) { 615 #define ECase(X) IO.enumCase(Kind, #X, wasm::WASM_EXTERNAL_##X); 616 ECase(FUNCTION); 617 ECase(TABLE); 618 ECase(MEMORY); 619 ECase(GLOBAL); 620 ECase(TAG); 621 #undef ECase 622 } 623 624 void ScalarEnumerationTraits<WasmYAML::Opcode>::enumeration( 625 IO &IO, WasmYAML::Opcode &Code) { 626 #define ECase(X) IO.enumCase(Code, #X, wasm::WASM_OPCODE_##X); 627 ECase(END); 628 ECase(I32_CONST); 629 ECase(I64_CONST); 630 ECase(F64_CONST); 631 ECase(F32_CONST); 632 ECase(GLOBAL_GET); 633 ECase(REF_NULL); 634 #undef ECase 635 } 636 637 void ScalarEnumerationTraits<WasmYAML::TableType>::enumeration( 638 IO &IO, WasmYAML::TableType &Type) { 639 #define CONCAT(X) (uint32_t) wasm::ValType::X 640 #define ECase(X) IO.enumCase(Type, #X, CONCAT(X)); 641 ECase(FUNCREF); 642 ECase(EXTERNREF); 643 ECase(EXNREF); 644 ECase(OTHERREF); 645 #undef ECase 646 } 647 648 void ScalarEnumerationTraits<WasmYAML::RelocType>::enumeration( 649 IO &IO, WasmYAML::RelocType &Type) { 650 #define WASM_RELOC(name, value) IO.enumCase(Type, #name, wasm::name); 651 #include "llvm/BinaryFormat/WasmRelocs.def" 652 #undef WASM_RELOC 653 IO.enumFallback<Hex32>(Type); 654 } 655 656 } // end namespace yaml 657 658 } // end namespace llvm 659