1 //===- WasmYAML.cpp - Wasm YAMLIO implementation --------------------------===// 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 // This file defines classes for handling the YAML representation of wasm. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "llvm/ObjectYAML/WasmYAML.h" 15 #include "llvm/Object/Wasm.h" 16 #include "llvm/Support/Casting.h" 17 #include "llvm/Support/MipsABIFlags.h" 18 19 namespace llvm { 20 21 namespace WasmYAML { 22 23 // Declared here rather than in the header to comply with: 24 // http://llvm.org/docs/CodingStandards.html#provide-a-virtual-method-anchor-for-classes-in-headers 25 Section::~Section() {} 26 27 } // end namespace WasmYAML 28 29 namespace yaml { 30 31 void MappingTraits<WasmYAML::FileHeader>::mapping( 32 IO &IO, WasmYAML::FileHeader &FileHdr) { 33 IO.mapRequired("Version", FileHdr.Version); 34 } 35 36 void MappingTraits<WasmYAML::Object>::mapping(IO &IO, 37 WasmYAML::Object &Object) { 38 IO.setContext(&Object); 39 IO.mapTag("!WASM", true); 40 IO.mapRequired("FileHeader", Object.Header); 41 IO.mapOptional("Sections", Object.Sections); 42 IO.setContext(nullptr); 43 } 44 45 static void commonSectionMapping(IO &IO, WasmYAML::Section &Section) { 46 IO.mapRequired("Type", Section.Type); 47 IO.mapOptional("Relocations", Section.Relocations); 48 } 49 50 static void sectionMapping(IO &IO, WasmYAML::NameSection &Section) { 51 commonSectionMapping(IO, Section); 52 IO.mapRequired("Name", Section.Name); 53 IO.mapOptional("FunctionNames", Section.FunctionNames); 54 } 55 56 static void sectionMapping(IO &IO, WasmYAML::LinkingSection &Section) { 57 commonSectionMapping(IO, Section); 58 IO.mapRequired("Name", Section.Name); 59 IO.mapRequired("DataSize", Section.DataSize); 60 IO.mapRequired("DataAlignment", Section.DataAlignment); 61 IO.mapRequired("SymbolInfo", Section.SymbolInfos); 62 } 63 64 static void sectionMapping(IO &IO, WasmYAML::CustomSection &Section) { 65 commonSectionMapping(IO, Section); 66 IO.mapRequired("Name", Section.Name); 67 IO.mapRequired("Payload", Section.Payload); 68 } 69 70 static void sectionMapping(IO &IO, WasmYAML::TypeSection &Section) { 71 commonSectionMapping(IO, Section); 72 IO.mapOptional("Signatures", Section.Signatures); 73 } 74 75 static void sectionMapping(IO &IO, WasmYAML::ImportSection &Section) { 76 commonSectionMapping(IO, Section); 77 IO.mapOptional("Imports", Section.Imports); 78 } 79 80 static void sectionMapping(IO &IO, WasmYAML::FunctionSection &Section) { 81 commonSectionMapping(IO, Section); 82 IO.mapOptional("FunctionTypes", Section.FunctionTypes); 83 } 84 85 static void sectionMapping(IO &IO, WasmYAML::TableSection &Section) { 86 commonSectionMapping(IO, Section); 87 IO.mapOptional("Tables", Section.Tables); 88 } 89 90 static void sectionMapping(IO &IO, WasmYAML::MemorySection &Section) { 91 commonSectionMapping(IO, Section); 92 IO.mapOptional("Memories", Section.Memories); 93 } 94 95 static void sectionMapping(IO &IO, WasmYAML::GlobalSection &Section) { 96 commonSectionMapping(IO, Section); 97 IO.mapOptional("Globals", Section.Globals); 98 } 99 100 static void sectionMapping(IO &IO, WasmYAML::ExportSection &Section) { 101 commonSectionMapping(IO, Section); 102 IO.mapOptional("Exports", Section.Exports); 103 } 104 105 static void sectionMapping(IO &IO, WasmYAML::StartSection &Section) { 106 commonSectionMapping(IO, Section); 107 IO.mapOptional("StartFunction", Section.StartFunction); 108 } 109 110 static void sectionMapping(IO &IO, WasmYAML::ElemSection &Section) { 111 commonSectionMapping(IO, Section); 112 IO.mapOptional("Segments", Section.Segments); 113 } 114 115 static void sectionMapping(IO &IO, WasmYAML::CodeSection &Section) { 116 commonSectionMapping(IO, Section); 117 IO.mapRequired("Functions", Section.Functions); 118 } 119 120 static void sectionMapping(IO &IO, WasmYAML::DataSection &Section) { 121 commonSectionMapping(IO, Section); 122 IO.mapRequired("Segments", Section.Segments); 123 } 124 125 void MappingTraits<std::unique_ptr<WasmYAML::Section>>::mapping( 126 IO &IO, std::unique_ptr<WasmYAML::Section> &Section) { 127 WasmYAML::SectionType SectionType; 128 if (IO.outputting()) 129 SectionType = Section->Type; 130 else 131 IO.mapRequired("Type", SectionType); 132 133 switch (SectionType) { 134 case wasm::WASM_SEC_CUSTOM: { 135 StringRef SectionName; 136 if (IO.outputting()) { 137 auto CustomSection = cast<WasmYAML::CustomSection>(Section.get()); 138 SectionName = CustomSection->Name; 139 } else { 140 IO.mapRequired("Name", SectionName); 141 } 142 if (SectionName == "linking") { 143 if (!IO.outputting()) 144 Section.reset(new WasmYAML::LinkingSection()); 145 sectionMapping(IO, *cast<WasmYAML::LinkingSection>(Section.get())); 146 } else if (SectionName == "name") { 147 if (!IO.outputting()) 148 Section.reset(new WasmYAML::NameSection()); 149 sectionMapping(IO, *cast<WasmYAML::NameSection>(Section.get())); 150 } else { 151 if (!IO.outputting()) 152 Section.reset(new WasmYAML::CustomSection(SectionName)); 153 sectionMapping(IO, *cast<WasmYAML::CustomSection>(Section.get())); 154 } 155 break; 156 } 157 case wasm::WASM_SEC_TYPE: 158 if (!IO.outputting()) 159 Section.reset(new WasmYAML::TypeSection()); 160 sectionMapping(IO, *cast<WasmYAML::TypeSection>(Section.get())); 161 break; 162 case wasm::WASM_SEC_IMPORT: 163 if (!IO.outputting()) 164 Section.reset(new WasmYAML::ImportSection()); 165 sectionMapping(IO, *cast<WasmYAML::ImportSection>(Section.get())); 166 break; 167 case wasm::WASM_SEC_FUNCTION: 168 if (!IO.outputting()) 169 Section.reset(new WasmYAML::FunctionSection()); 170 sectionMapping(IO, *cast<WasmYAML::FunctionSection>(Section.get())); 171 break; 172 case wasm::WASM_SEC_TABLE: 173 if (!IO.outputting()) 174 Section.reset(new WasmYAML::TableSection()); 175 sectionMapping(IO, *cast<WasmYAML::TableSection>(Section.get())); 176 break; 177 case wasm::WASM_SEC_MEMORY: 178 if (!IO.outputting()) 179 Section.reset(new WasmYAML::MemorySection()); 180 sectionMapping(IO, *cast<WasmYAML::MemorySection>(Section.get())); 181 break; 182 case wasm::WASM_SEC_GLOBAL: 183 if (!IO.outputting()) 184 Section.reset(new WasmYAML::GlobalSection()); 185 sectionMapping(IO, *cast<WasmYAML::GlobalSection>(Section.get())); 186 break; 187 case wasm::WASM_SEC_EXPORT: 188 if (!IO.outputting()) 189 Section.reset(new WasmYAML::ExportSection()); 190 sectionMapping(IO, *cast<WasmYAML::ExportSection>(Section.get())); 191 break; 192 case wasm::WASM_SEC_START: 193 if (!IO.outputting()) 194 Section.reset(new WasmYAML::StartSection()); 195 sectionMapping(IO, *cast<WasmYAML::StartSection>(Section.get())); 196 break; 197 case wasm::WASM_SEC_ELEM: 198 if (!IO.outputting()) 199 Section.reset(new WasmYAML::ElemSection()); 200 sectionMapping(IO, *cast<WasmYAML::ElemSection>(Section.get())); 201 break; 202 case wasm::WASM_SEC_CODE: 203 if (!IO.outputting()) 204 Section.reset(new WasmYAML::CodeSection()); 205 sectionMapping(IO, *cast<WasmYAML::CodeSection>(Section.get())); 206 break; 207 case wasm::WASM_SEC_DATA: 208 if (!IO.outputting()) 209 Section.reset(new WasmYAML::DataSection()); 210 sectionMapping(IO, *cast<WasmYAML::DataSection>(Section.get())); 211 break; 212 default: 213 llvm_unreachable("Unknown section type"); 214 } 215 } 216 217 void ScalarEnumerationTraits<WasmYAML::SectionType>::enumeration( 218 IO &IO, WasmYAML::SectionType &Type) { 219 #define ECase(X) IO.enumCase(Type, #X, wasm::WASM_SEC_##X); 220 ECase(CUSTOM); 221 ECase(TYPE); 222 ECase(IMPORT); 223 ECase(FUNCTION); 224 ECase(TABLE); 225 ECase(MEMORY); 226 ECase(GLOBAL); 227 ECase(EXPORT); 228 ECase(START); 229 ECase(ELEM); 230 ECase(CODE); 231 ECase(DATA); 232 #undef ECase 233 } 234 235 void MappingTraits<WasmYAML::Signature>::mapping( 236 IO &IO, WasmYAML::Signature &Signature) { 237 IO.mapOptional("Index", Signature.Index); 238 IO.mapRequired("ReturnType", Signature.ReturnType); 239 IO.mapRequired("ParamTypes", Signature.ParamTypes); 240 } 241 242 void MappingTraits<WasmYAML::Table>::mapping(IO &IO, WasmYAML::Table &Table) { 243 IO.mapRequired("ElemType", Table.ElemType); 244 IO.mapRequired("Limits", Table.TableLimits); 245 } 246 247 void MappingTraits<WasmYAML::Function>::mapping(IO &IO, 248 WasmYAML::Function &Function) { 249 IO.mapRequired("Locals", Function.Locals); 250 IO.mapRequired("Body", Function.Body); 251 } 252 253 void MappingTraits<WasmYAML::Relocation>::mapping( 254 IO &IO, WasmYAML::Relocation &Relocation) { 255 IO.mapRequired("Type", Relocation.Type); 256 IO.mapRequired("Index", Relocation.Index); 257 IO.mapRequired("Offset", Relocation.Offset); 258 IO.mapOptional("Addend", Relocation.Addend, 0); 259 } 260 261 void MappingTraits<WasmYAML::NameEntry>::mapping( 262 IO &IO, WasmYAML::NameEntry &NameEntry) { 263 IO.mapRequired("Index", NameEntry.Index); 264 IO.mapRequired("Name", NameEntry.Name); 265 } 266 267 void MappingTraits<WasmYAML::LocalDecl>::mapping( 268 IO &IO, WasmYAML::LocalDecl &LocalDecl) { 269 IO.mapRequired("Type", LocalDecl.Type); 270 IO.mapRequired("Count", LocalDecl.Count); 271 } 272 273 void MappingTraits<WasmYAML::Limits>::mapping(IO &IO, 274 WasmYAML::Limits &Limits) { 275 if (!IO.outputting() || Limits.Flags) 276 IO.mapOptional("Flags", Limits.Flags); 277 IO.mapRequired("Initial", Limits.Initial); 278 if (!IO.outputting() || Limits.Flags & wasm::WASM_LIMITS_FLAG_HAS_MAX) 279 IO.mapOptional("Maximum", Limits.Maximum); 280 } 281 282 void MappingTraits<WasmYAML::ElemSegment>::mapping( 283 IO &IO, WasmYAML::ElemSegment &Segment) { 284 IO.mapRequired("Offset", Segment.Offset); 285 IO.mapRequired("Functions", Segment.Functions); 286 } 287 288 void MappingTraits<WasmYAML::Import>::mapping(IO &IO, 289 WasmYAML::Import &Import) { 290 IO.mapRequired("Module", Import.Module); 291 IO.mapRequired("Field", Import.Field); 292 IO.mapRequired("Kind", Import.Kind); 293 if (Import.Kind == wasm::WASM_EXTERNAL_FUNCTION) { 294 IO.mapRequired("SigIndex", Import.SigIndex); 295 } else if (Import.Kind == wasm::WASM_EXTERNAL_GLOBAL) { 296 IO.mapRequired("GlobalType", Import.GlobalImport.Type); 297 IO.mapRequired("GlobalMutable", Import.GlobalImport.Mutable); 298 } else if (Import.Kind == wasm::WASM_EXTERNAL_TABLE) { 299 IO.mapRequired("Table", Import.TableImport); 300 } else if (Import.Kind == wasm::WASM_EXTERNAL_MEMORY ) { 301 IO.mapRequired("Memory", Import.Memory); 302 } else { 303 llvm_unreachable("unhandled import type"); 304 } 305 } 306 307 void MappingTraits<WasmYAML::Export>::mapping(IO &IO, 308 WasmYAML::Export &Export) { 309 IO.mapRequired("Name", Export.Name); 310 IO.mapRequired("Kind", Export.Kind); 311 IO.mapRequired("Index", Export.Index); 312 } 313 314 void MappingTraits<WasmYAML::Global>::mapping(IO &IO, 315 WasmYAML::Global &Global) { 316 IO.mapRequired("Type", Global.Type); 317 IO.mapRequired("Mutable", Global.Mutable); 318 IO.mapRequired("InitExpr", Global.InitExpr); 319 } 320 321 void MappingTraits<wasm::WasmInitExpr>::mapping(IO &IO, 322 wasm::WasmInitExpr &Expr) { 323 WasmYAML::Opcode Op = Expr.Opcode; 324 IO.mapRequired("Opcode", Op); 325 Expr.Opcode = Op; 326 switch (Expr.Opcode) { 327 case wasm::WASM_OPCODE_I32_CONST: 328 IO.mapRequired("Value", Expr.Value.Int32); 329 break; 330 case wasm::WASM_OPCODE_I64_CONST: 331 IO.mapRequired("Value", Expr.Value.Int64); 332 break; 333 case wasm::WASM_OPCODE_F32_CONST: 334 IO.mapRequired("Value", Expr.Value.Float32); 335 break; 336 case wasm::WASM_OPCODE_F64_CONST: 337 IO.mapRequired("Value", Expr.Value.Float64); 338 break; 339 case wasm::WASM_OPCODE_GET_GLOBAL: 340 IO.mapRequired("Index", Expr.Value.Global); 341 break; 342 } 343 } 344 345 void MappingTraits<WasmYAML::DataSegment>::mapping( 346 IO &IO, WasmYAML::DataSegment &Segment) { 347 IO.mapRequired("Index", Segment.Index); 348 IO.mapRequired("Offset", Segment.Offset); 349 IO.mapRequired("Content", Segment.Content); 350 } 351 352 void MappingTraits<WasmYAML::SymbolInfo>::mapping(IO &IO, 353 WasmYAML::SymbolInfo &Info) { 354 IO.mapRequired("Name", Info.Name); 355 IO.mapRequired("Flags", Info.Flags); 356 } 357 358 void ScalarEnumerationTraits<WasmYAML::ValueType>::enumeration( 359 IO &IO, WasmYAML::ValueType &Type) { 360 #define ECase(X) IO.enumCase(Type, #X, wasm::WASM_TYPE_##X); 361 ECase(I32); 362 ECase(I64); 363 ECase(F32); 364 ECase(F64); 365 ECase(ANYFUNC); 366 ECase(FUNC); 367 ECase(NORESULT); 368 #undef ECase 369 } 370 371 void ScalarEnumerationTraits<WasmYAML::ExportKind>::enumeration( 372 IO &IO, WasmYAML::ExportKind &Kind) { 373 #define ECase(X) IO.enumCase(Kind, #X, wasm::WASM_EXTERNAL_##X); 374 ECase(FUNCTION); 375 ECase(TABLE); 376 ECase(MEMORY); 377 ECase(GLOBAL); 378 #undef ECase 379 } 380 381 void ScalarEnumerationTraits<WasmYAML::Opcode>::enumeration( 382 IO &IO, WasmYAML::Opcode &Code) { 383 #define ECase(X) IO.enumCase(Code, #X, wasm::WASM_OPCODE_##X); 384 ECase(END); 385 ECase(I32_CONST); 386 ECase(I64_CONST); 387 ECase(F64_CONST); 388 ECase(F32_CONST); 389 ECase(GET_GLOBAL); 390 #undef ECase 391 } 392 393 void ScalarEnumerationTraits<WasmYAML::TableType>::enumeration( 394 IO &IO, WasmYAML::TableType &Type) { 395 #define ECase(X) IO.enumCase(Type, #X, wasm::WASM_TYPE_##X); 396 ECase(ANYFUNC); 397 #undef ECase 398 } 399 400 void ScalarEnumerationTraits<WasmYAML::RelocType>::enumeration( 401 IO &IO, WasmYAML::RelocType &Type) { 402 #define WASM_RELOC(name, value) IO.enumCase(Type, #name, wasm::name); 403 #include "llvm/BinaryFormat/WasmRelocs/WebAssembly.def" 404 #undef WASM_RELOC 405 } 406 407 } // end namespace yaml 408 } // end namespace llvm 409