Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 1 | //===- WasmYAML.cpp - Wasm YAMLIO implementation --------------------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file defines classes for handling the YAML representation of wasm. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "llvm/ObjectYAML/WasmYAML.h" |
Eugene Zelenko | 28082ab | 2017-07-01 01:35:55 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/StringRef.h" |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 15 | #include "llvm/Support/Casting.h" |
Eugene Zelenko | 28082ab | 2017-07-01 01:35:55 +0000 | [diff] [blame] | 16 | #include "llvm/Support/ErrorHandling.h" |
| 17 | #include "llvm/Support/YAMLTraits.h" |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 18 | |
| 19 | namespace llvm { |
Derek Schuff | c5b472f | 2017-03-31 22:14:14 +0000 | [diff] [blame] | 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 |
Eugene Zelenko | 28082ab | 2017-07-01 01:35:55 +0000 | [diff] [blame] | 25 | Section::~Section() = default; |
Derek Schuff | c5b472f | 2017-03-31 22:14:14 +0000 | [diff] [blame] | 26 | |
| 27 | } // end namespace WasmYAML |
| 28 | |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 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 | |
Sam Clegg | e4afbc6 | 2018-11-14 18:36:24 +0000 | [diff] [blame] | 50 | static void sectionMapping(IO &IO, WasmYAML::DylinkSection &Section) { |
| 51 | commonSectionMapping(IO, Section); |
| 52 | IO.mapRequired("Name", Section.Name); |
| 53 | IO.mapRequired("MemorySize", Section.MemorySize); |
| 54 | IO.mapRequired("MemoryAlignment", Section.MemoryAlignment); |
| 55 | IO.mapRequired("TableSize", Section.TableSize); |
| 56 | IO.mapRequired("TableAlignment", Section.TableAlignment); |
Sam Clegg | 0380125 | 2018-12-12 23:40:58 +0000 | [diff] [blame] | 57 | IO.mapRequired("Needed", Section.Needed); |
Sam Clegg | e4afbc6 | 2018-11-14 18:36:24 +0000 | [diff] [blame] | 58 | } |
| 59 | |
Sam Clegg | b7787fd | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 60 | static void sectionMapping(IO &IO, WasmYAML::NameSection &Section) { |
| 61 | commonSectionMapping(IO, Section); |
| 62 | IO.mapRequired("Name", Section.Name); |
| 63 | IO.mapOptional("FunctionNames", Section.FunctionNames); |
| 64 | } |
| 65 | |
| 66 | static void sectionMapping(IO &IO, WasmYAML::LinkingSection &Section) { |
| 67 | commonSectionMapping(IO, Section); |
| 68 | IO.mapRequired("Name", Section.Name); |
Sam Clegg | 6bb5a41 | 2018-04-26 18:15:32 +0000 | [diff] [blame] | 69 | IO.mapRequired("Version", Section.Version); |
Sam Clegg | 6c899ba | 2018-02-23 05:08:34 +0000 | [diff] [blame] | 70 | IO.mapOptional("SymbolTable", Section.SymbolTable); |
Sam Clegg | 63ebb81 | 2017-09-29 16:50:08 +0000 | [diff] [blame] | 71 | IO.mapOptional("SegmentInfo", Section.SegmentInfos); |
Sam Clegg | 4273998 | 2017-12-14 21:10:03 +0000 | [diff] [blame] | 72 | IO.mapOptional("InitFunctions", Section.InitFunctions); |
Sam Clegg | ea7cace | 2018-01-09 23:43:14 +0000 | [diff] [blame] | 73 | IO.mapOptional("Comdats", Section.Comdats); |
Sam Clegg | b7787fd | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Thomas Lively | cbda16e | 2019-01-17 02:29:55 +0000 | [diff] [blame] | 76 | static void sectionMapping(IO &IO, WasmYAML::ProducersSection &Section) { |
| 77 | commonSectionMapping(IO, Section); |
| 78 | IO.mapRequired("Name", Section.Name); |
| 79 | IO.mapOptional("Languages", Section.Languages); |
| 80 | IO.mapOptional("Tools", Section.Tools); |
| 81 | IO.mapOptional("SDKs", Section.SDKs); |
| 82 | } |
| 83 | |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 84 | static void sectionMapping(IO &IO, WasmYAML::CustomSection &Section) { |
| 85 | commonSectionMapping(IO, Section); |
| 86 | IO.mapRequired("Name", Section.Name); |
Sam Clegg | b7787fd | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 87 | IO.mapRequired("Payload", Section.Payload); |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | static void sectionMapping(IO &IO, WasmYAML::TypeSection &Section) { |
| 91 | commonSectionMapping(IO, Section); |
| 92 | IO.mapOptional("Signatures", Section.Signatures); |
| 93 | } |
| 94 | |
| 95 | static void sectionMapping(IO &IO, WasmYAML::ImportSection &Section) { |
| 96 | commonSectionMapping(IO, Section); |
| 97 | IO.mapOptional("Imports", Section.Imports); |
| 98 | } |
| 99 | |
| 100 | static void sectionMapping(IO &IO, WasmYAML::FunctionSection &Section) { |
| 101 | commonSectionMapping(IO, Section); |
| 102 | IO.mapOptional("FunctionTypes", Section.FunctionTypes); |
| 103 | } |
| 104 | |
| 105 | static void sectionMapping(IO &IO, WasmYAML::TableSection &Section) { |
| 106 | commonSectionMapping(IO, Section); |
| 107 | IO.mapOptional("Tables", Section.Tables); |
| 108 | } |
| 109 | |
| 110 | static void sectionMapping(IO &IO, WasmYAML::MemorySection &Section) { |
| 111 | commonSectionMapping(IO, Section); |
| 112 | IO.mapOptional("Memories", Section.Memories); |
| 113 | } |
| 114 | |
| 115 | static void sectionMapping(IO &IO, WasmYAML::GlobalSection &Section) { |
| 116 | commonSectionMapping(IO, Section); |
| 117 | IO.mapOptional("Globals", Section.Globals); |
| 118 | } |
| 119 | |
Heejin Ahn | da419bd | 2018-11-14 02:46:21 +0000 | [diff] [blame] | 120 | static void sectionMapping(IO &IO, WasmYAML::EventSection &Section) { |
| 121 | commonSectionMapping(IO, Section); |
| 122 | IO.mapOptional("Events", Section.Events); |
| 123 | } |
| 124 | |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 125 | static void sectionMapping(IO &IO, WasmYAML::ExportSection &Section) { |
| 126 | commonSectionMapping(IO, Section); |
| 127 | IO.mapOptional("Exports", Section.Exports); |
| 128 | } |
| 129 | |
| 130 | static void sectionMapping(IO &IO, WasmYAML::StartSection &Section) { |
| 131 | commonSectionMapping(IO, Section); |
| 132 | IO.mapOptional("StartFunction", Section.StartFunction); |
| 133 | } |
| 134 | |
| 135 | static void sectionMapping(IO &IO, WasmYAML::ElemSection &Section) { |
| 136 | commonSectionMapping(IO, Section); |
| 137 | IO.mapOptional("Segments", Section.Segments); |
| 138 | } |
| 139 | |
| 140 | static void sectionMapping(IO &IO, WasmYAML::CodeSection &Section) { |
| 141 | commonSectionMapping(IO, Section); |
| 142 | IO.mapRequired("Functions", Section.Functions); |
| 143 | } |
| 144 | |
| 145 | static void sectionMapping(IO &IO, WasmYAML::DataSection &Section) { |
| 146 | commonSectionMapping(IO, Section); |
| 147 | IO.mapRequired("Segments", Section.Segments); |
| 148 | } |
| 149 | |
| 150 | void MappingTraits<std::unique_ptr<WasmYAML::Section>>::mapping( |
| 151 | IO &IO, std::unique_ptr<WasmYAML::Section> &Section) { |
| 152 | WasmYAML::SectionType SectionType; |
| 153 | if (IO.outputting()) |
| 154 | SectionType = Section->Type; |
| 155 | else |
| 156 | IO.mapRequired("Type", SectionType); |
| 157 | |
| 158 | switch (SectionType) { |
Sam Clegg | b7787fd | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 159 | case wasm::WASM_SEC_CUSTOM: { |
| 160 | StringRef SectionName; |
| 161 | if (IO.outputting()) { |
| 162 | auto CustomSection = cast<WasmYAML::CustomSection>(Section.get()); |
| 163 | SectionName = CustomSection->Name; |
| 164 | } else { |
| 165 | IO.mapRequired("Name", SectionName); |
| 166 | } |
Sam Clegg | e4afbc6 | 2018-11-14 18:36:24 +0000 | [diff] [blame] | 167 | if (SectionName == "dylink") { |
| 168 | if (!IO.outputting()) |
| 169 | Section.reset(new WasmYAML::DylinkSection()); |
| 170 | sectionMapping(IO, *cast<WasmYAML::DylinkSection>(Section.get())); |
| 171 | } else if (SectionName == "linking") { |
Sam Clegg | b7787fd | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 172 | if (!IO.outputting()) |
| 173 | Section.reset(new WasmYAML::LinkingSection()); |
| 174 | sectionMapping(IO, *cast<WasmYAML::LinkingSection>(Section.get())); |
| 175 | } else if (SectionName == "name") { |
| 176 | if (!IO.outputting()) |
| 177 | Section.reset(new WasmYAML::NameSection()); |
| 178 | sectionMapping(IO, *cast<WasmYAML::NameSection>(Section.get())); |
Thomas Lively | cbda16e | 2019-01-17 02:29:55 +0000 | [diff] [blame] | 179 | } else if (SectionName == "producers") { |
| 180 | if (!IO.outputting()) |
| 181 | Section.reset(new WasmYAML::ProducersSection()); |
| 182 | sectionMapping(IO, *cast<WasmYAML::ProducersSection>(Section.get())); |
Sam Clegg | b7787fd | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 183 | } else { |
| 184 | if (!IO.outputting()) |
| 185 | Section.reset(new WasmYAML::CustomSection(SectionName)); |
| 186 | sectionMapping(IO, *cast<WasmYAML::CustomSection>(Section.get())); |
| 187 | } |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 188 | break; |
Sam Clegg | b7787fd | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 189 | } |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 190 | case wasm::WASM_SEC_TYPE: |
| 191 | if (!IO.outputting()) |
| 192 | Section.reset(new WasmYAML::TypeSection()); |
| 193 | sectionMapping(IO, *cast<WasmYAML::TypeSection>(Section.get())); |
| 194 | break; |
| 195 | case wasm::WASM_SEC_IMPORT: |
| 196 | if (!IO.outputting()) |
| 197 | Section.reset(new WasmYAML::ImportSection()); |
| 198 | sectionMapping(IO, *cast<WasmYAML::ImportSection>(Section.get())); |
| 199 | break; |
| 200 | case wasm::WASM_SEC_FUNCTION: |
| 201 | if (!IO.outputting()) |
| 202 | Section.reset(new WasmYAML::FunctionSection()); |
| 203 | sectionMapping(IO, *cast<WasmYAML::FunctionSection>(Section.get())); |
| 204 | break; |
| 205 | case wasm::WASM_SEC_TABLE: |
| 206 | if (!IO.outputting()) |
| 207 | Section.reset(new WasmYAML::TableSection()); |
| 208 | sectionMapping(IO, *cast<WasmYAML::TableSection>(Section.get())); |
| 209 | break; |
| 210 | case wasm::WASM_SEC_MEMORY: |
| 211 | if (!IO.outputting()) |
| 212 | Section.reset(new WasmYAML::MemorySection()); |
| 213 | sectionMapping(IO, *cast<WasmYAML::MemorySection>(Section.get())); |
| 214 | break; |
| 215 | case wasm::WASM_SEC_GLOBAL: |
| 216 | if (!IO.outputting()) |
| 217 | Section.reset(new WasmYAML::GlobalSection()); |
| 218 | sectionMapping(IO, *cast<WasmYAML::GlobalSection>(Section.get())); |
| 219 | break; |
Heejin Ahn | da419bd | 2018-11-14 02:46:21 +0000 | [diff] [blame] | 220 | case wasm::WASM_SEC_EVENT: |
| 221 | if (!IO.outputting()) |
| 222 | Section.reset(new WasmYAML::EventSection()); |
| 223 | sectionMapping(IO, *cast<WasmYAML::EventSection>(Section.get())); |
| 224 | break; |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 225 | case wasm::WASM_SEC_EXPORT: |
| 226 | if (!IO.outputting()) |
| 227 | Section.reset(new WasmYAML::ExportSection()); |
| 228 | sectionMapping(IO, *cast<WasmYAML::ExportSection>(Section.get())); |
| 229 | break; |
| 230 | case wasm::WASM_SEC_START: |
| 231 | if (!IO.outputting()) |
| 232 | Section.reset(new WasmYAML::StartSection()); |
| 233 | sectionMapping(IO, *cast<WasmYAML::StartSection>(Section.get())); |
| 234 | break; |
| 235 | case wasm::WASM_SEC_ELEM: |
| 236 | if (!IO.outputting()) |
| 237 | Section.reset(new WasmYAML::ElemSection()); |
| 238 | sectionMapping(IO, *cast<WasmYAML::ElemSection>(Section.get())); |
| 239 | break; |
| 240 | case wasm::WASM_SEC_CODE: |
| 241 | if (!IO.outputting()) |
| 242 | Section.reset(new WasmYAML::CodeSection()); |
| 243 | sectionMapping(IO, *cast<WasmYAML::CodeSection>(Section.get())); |
| 244 | break; |
| 245 | case wasm::WASM_SEC_DATA: |
| 246 | if (!IO.outputting()) |
| 247 | Section.reset(new WasmYAML::DataSection()); |
| 248 | sectionMapping(IO, *cast<WasmYAML::DataSection>(Section.get())); |
| 249 | break; |
| 250 | default: |
| 251 | llvm_unreachable("Unknown section type"); |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | void ScalarEnumerationTraits<WasmYAML::SectionType>::enumeration( |
| 256 | IO &IO, WasmYAML::SectionType &Type) { |
| 257 | #define ECase(X) IO.enumCase(Type, #X, wasm::WASM_SEC_##X); |
| 258 | ECase(CUSTOM); |
| 259 | ECase(TYPE); |
| 260 | ECase(IMPORT); |
| 261 | ECase(FUNCTION); |
| 262 | ECase(TABLE); |
| 263 | ECase(MEMORY); |
| 264 | ECase(GLOBAL); |
Heejin Ahn | da419bd | 2018-11-14 02:46:21 +0000 | [diff] [blame] | 265 | ECase(EVENT); |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 266 | ECase(EXPORT); |
| 267 | ECase(START); |
| 268 | ECase(ELEM); |
| 269 | ECase(CODE); |
| 270 | ECase(DATA); |
| 271 | #undef ECase |
| 272 | } |
| 273 | |
| 274 | void MappingTraits<WasmYAML::Signature>::mapping( |
| 275 | IO &IO, WasmYAML::Signature &Signature) { |
Sam Clegg | e53af7f | 2018-01-09 21:38:53 +0000 | [diff] [blame] | 276 | IO.mapRequired("Index", Signature.Index); |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 277 | IO.mapRequired("ReturnType", Signature.ReturnType); |
| 278 | IO.mapRequired("ParamTypes", Signature.ParamTypes); |
| 279 | } |
| 280 | |
| 281 | void MappingTraits<WasmYAML::Table>::mapping(IO &IO, WasmYAML::Table &Table) { |
| 282 | IO.mapRequired("ElemType", Table.ElemType); |
| 283 | IO.mapRequired("Limits", Table.TableLimits); |
| 284 | } |
| 285 | |
| 286 | void MappingTraits<WasmYAML::Function>::mapping(IO &IO, |
| 287 | WasmYAML::Function &Function) { |
Sam Clegg | e53af7f | 2018-01-09 21:38:53 +0000 | [diff] [blame] | 288 | IO.mapRequired("Index", Function.Index); |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 289 | IO.mapRequired("Locals", Function.Locals); |
| 290 | IO.mapRequired("Body", Function.Body); |
| 291 | } |
| 292 | |
| 293 | void MappingTraits<WasmYAML::Relocation>::mapping( |
| 294 | IO &IO, WasmYAML::Relocation &Relocation) { |
| 295 | IO.mapRequired("Type", Relocation.Type); |
| 296 | IO.mapRequired("Index", Relocation.Index); |
| 297 | IO.mapRequired("Offset", Relocation.Offset); |
Sam Clegg | cc182aa | 2017-04-26 00:02:31 +0000 | [diff] [blame] | 298 | IO.mapOptional("Addend", Relocation.Addend, 0); |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 299 | } |
| 300 | |
Sam Clegg | 03cdd12 | 2017-05-05 18:12:34 +0000 | [diff] [blame] | 301 | void MappingTraits<WasmYAML::NameEntry>::mapping( |
| 302 | IO &IO, WasmYAML::NameEntry &NameEntry) { |
| 303 | IO.mapRequired("Index", NameEntry.Index); |
| 304 | IO.mapRequired("Name", NameEntry.Name); |
| 305 | } |
| 306 | |
Thomas Lively | cbda16e | 2019-01-17 02:29:55 +0000 | [diff] [blame] | 307 | void MappingTraits<WasmYAML::ProducerEntry>::mapping( |
| 308 | IO &IO, WasmYAML::ProducerEntry &ProducerEntry) { |
| 309 | IO.mapRequired("Name", ProducerEntry.Name); |
| 310 | IO.mapRequired("Version", ProducerEntry.Version); |
| 311 | } |
| 312 | |
Sam Clegg | 63ebb81 | 2017-09-29 16:50:08 +0000 | [diff] [blame] | 313 | void MappingTraits<WasmYAML::SegmentInfo>::mapping( |
| 314 | IO &IO, WasmYAML::SegmentInfo &SegmentInfo) { |
| 315 | IO.mapRequired("Index", SegmentInfo.Index); |
| 316 | IO.mapRequired("Name", SegmentInfo.Name); |
| 317 | IO.mapRequired("Alignment", SegmentInfo.Alignment); |
| 318 | IO.mapRequired("Flags", SegmentInfo.Flags); |
| 319 | } |
| 320 | |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 321 | void MappingTraits<WasmYAML::LocalDecl>::mapping( |
| 322 | IO &IO, WasmYAML::LocalDecl &LocalDecl) { |
| 323 | IO.mapRequired("Type", LocalDecl.Type); |
| 324 | IO.mapRequired("Count", LocalDecl.Count); |
| 325 | } |
| 326 | |
| 327 | void MappingTraits<WasmYAML::Limits>::mapping(IO &IO, |
| 328 | WasmYAML::Limits &Limits) { |
| 329 | if (!IO.outputting() || Limits.Flags) |
| 330 | IO.mapOptional("Flags", Limits.Flags); |
| 331 | IO.mapRequired("Initial", Limits.Initial); |
| 332 | if (!IO.outputting() || Limits.Flags & wasm::WASM_LIMITS_FLAG_HAS_MAX) |
| 333 | IO.mapOptional("Maximum", Limits.Maximum); |
| 334 | } |
| 335 | |
| 336 | void MappingTraits<WasmYAML::ElemSegment>::mapping( |
| 337 | IO &IO, WasmYAML::ElemSegment &Segment) { |
| 338 | IO.mapRequired("Offset", Segment.Offset); |
| 339 | IO.mapRequired("Functions", Segment.Functions); |
| 340 | } |
| 341 | |
| 342 | void MappingTraits<WasmYAML::Import>::mapping(IO &IO, |
| 343 | WasmYAML::Import &Import) { |
| 344 | IO.mapRequired("Module", Import.Module); |
| 345 | IO.mapRequired("Field", Import.Field); |
| 346 | IO.mapRequired("Kind", Import.Kind); |
| 347 | if (Import.Kind == wasm::WASM_EXTERNAL_FUNCTION) { |
| 348 | IO.mapRequired("SigIndex", Import.SigIndex); |
| 349 | } else if (Import.Kind == wasm::WASM_EXTERNAL_GLOBAL) { |
Sam Clegg | 41db519 | 2017-05-10 00:14:04 +0000 | [diff] [blame] | 350 | IO.mapRequired("GlobalType", Import.GlobalImport.Type); |
| 351 | IO.mapRequired("GlobalMutable", Import.GlobalImport.Mutable); |
Heejin Ahn | da419bd | 2018-11-14 02:46:21 +0000 | [diff] [blame] | 352 | } else if (Import.Kind == wasm::WASM_EXTERNAL_EVENT) { |
| 353 | IO.mapRequired("EventAttribute", Import.EventImport.Attribute); |
| 354 | IO.mapRequired("EventSigIndex", Import.EventImport.SigIndex); |
Sam Clegg | 2ffff5a | 2017-05-09 23:48:41 +0000 | [diff] [blame] | 355 | } else if (Import.Kind == wasm::WASM_EXTERNAL_TABLE) { |
Sam Clegg | 41db519 | 2017-05-10 00:14:04 +0000 | [diff] [blame] | 356 | IO.mapRequired("Table", Import.TableImport); |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 357 | } else if (Import.Kind == wasm::WASM_EXTERNAL_MEMORY) { |
Sam Clegg | 2ffff5a | 2017-05-09 23:48:41 +0000 | [diff] [blame] | 358 | IO.mapRequired("Memory", Import.Memory); |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 359 | } else { |
| 360 | llvm_unreachable("unhandled import type"); |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | void MappingTraits<WasmYAML::Export>::mapping(IO &IO, |
| 365 | WasmYAML::Export &Export) { |
| 366 | IO.mapRequired("Name", Export.Name); |
| 367 | IO.mapRequired("Kind", Export.Kind); |
| 368 | IO.mapRequired("Index", Export.Index); |
| 369 | } |
| 370 | |
| 371 | void MappingTraits<WasmYAML::Global>::mapping(IO &IO, |
| 372 | WasmYAML::Global &Global) { |
Sam Clegg | e53af7f | 2018-01-09 21:38:53 +0000 | [diff] [blame] | 373 | IO.mapRequired("Index", Global.Index); |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 374 | IO.mapRequired("Type", Global.Type); |
| 375 | IO.mapRequired("Mutable", Global.Mutable); |
| 376 | IO.mapRequired("InitExpr", Global.InitExpr); |
| 377 | } |
| 378 | |
| 379 | void MappingTraits<wasm::WasmInitExpr>::mapping(IO &IO, |
| 380 | wasm::WasmInitExpr &Expr) { |
| 381 | WasmYAML::Opcode Op = Expr.Opcode; |
| 382 | IO.mapRequired("Opcode", Op); |
| 383 | Expr.Opcode = Op; |
| 384 | switch (Expr.Opcode) { |
| 385 | case wasm::WASM_OPCODE_I32_CONST: |
| 386 | IO.mapRequired("Value", Expr.Value.Int32); |
| 387 | break; |
| 388 | case wasm::WASM_OPCODE_I64_CONST: |
| 389 | IO.mapRequired("Value", Expr.Value.Int64); |
| 390 | break; |
| 391 | case wasm::WASM_OPCODE_F32_CONST: |
| 392 | IO.mapRequired("Value", Expr.Value.Float32); |
| 393 | break; |
| 394 | case wasm::WASM_OPCODE_F64_CONST: |
| 395 | IO.mapRequired("Value", Expr.Value.Float64); |
| 396 | break; |
Thomas Lively | 6a87dda | 2019-01-08 06:25:55 +0000 | [diff] [blame] | 397 | case wasm::WASM_OPCODE_GLOBAL_GET: |
Sam Clegg | 7fb391f | 2017-04-25 17:11:56 +0000 | [diff] [blame] | 398 | IO.mapRequired("Index", Expr.Value.Global); |
| 399 | break; |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 400 | } |
| 401 | } |
| 402 | |
| 403 | void MappingTraits<WasmYAML::DataSegment>::mapping( |
| 404 | IO &IO, WasmYAML::DataSegment &Segment) { |
Sam Clegg | 9c07f94 | 2017-07-12 00:24:54 +0000 | [diff] [blame] | 405 | IO.mapOptional("SectionOffset", Segment.SectionOffset); |
| 406 | IO.mapRequired("MemoryIndex", Segment.MemoryIndex); |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 407 | IO.mapRequired("Offset", Segment.Offset); |
| 408 | IO.mapRequired("Content", Segment.Content); |
| 409 | } |
| 410 | |
Sam Clegg | 4273998 | 2017-12-14 21:10:03 +0000 | [diff] [blame] | 411 | void MappingTraits<WasmYAML::InitFunction>::mapping( |
| 412 | IO &IO, WasmYAML::InitFunction &Init) { |
| 413 | IO.mapRequired("Priority", Init.Priority); |
Sam Clegg | 6c899ba | 2018-02-23 05:08:34 +0000 | [diff] [blame] | 414 | IO.mapRequired("Symbol", Init.Symbol); |
Sam Clegg | 4273998 | 2017-12-14 21:10:03 +0000 | [diff] [blame] | 415 | } |
| 416 | |
Sam Clegg | ea7cace | 2018-01-09 23:43:14 +0000 | [diff] [blame] | 417 | void ScalarEnumerationTraits<WasmYAML::ComdatKind>::enumeration( |
| 418 | IO &IO, WasmYAML::ComdatKind &Kind) { |
| 419 | #define ECase(X) IO.enumCase(Kind, #X, wasm::WASM_COMDAT_##X); |
| 420 | ECase(FUNCTION); |
| 421 | ECase(DATA); |
| 422 | #undef ECase |
| 423 | } |
| 424 | |
| 425 | void MappingTraits<WasmYAML::ComdatEntry>::mapping( |
| 426 | IO &IO, WasmYAML::ComdatEntry &ComdatEntry) { |
| 427 | IO.mapRequired("Kind", ComdatEntry.Kind); |
| 428 | IO.mapRequired("Index", ComdatEntry.Index); |
| 429 | } |
| 430 | |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 431 | void MappingTraits<WasmYAML::Comdat>::mapping(IO &IO, |
| 432 | WasmYAML::Comdat &Comdat) { |
Sam Clegg | ea7cace | 2018-01-09 23:43:14 +0000 | [diff] [blame] | 433 | IO.mapRequired("Name", Comdat.Name); |
| 434 | IO.mapRequired("Entries", Comdat.Entries); |
| 435 | } |
| 436 | |
Sam Clegg | b7787fd | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 437 | void MappingTraits<WasmYAML::SymbolInfo>::mapping(IO &IO, |
| 438 | WasmYAML::SymbolInfo &Info) { |
Sam Clegg | 6c899ba | 2018-02-23 05:08:34 +0000 | [diff] [blame] | 439 | IO.mapRequired("Index", Info.Index); |
| 440 | IO.mapRequired("Kind", Info.Kind); |
Sam Clegg | b7787fd | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 441 | IO.mapRequired("Name", Info.Name); |
| 442 | IO.mapRequired("Flags", Info.Flags); |
Sam Clegg | 6c899ba | 2018-02-23 05:08:34 +0000 | [diff] [blame] | 443 | if (Info.Kind == wasm::WASM_SYMBOL_TYPE_FUNCTION) { |
| 444 | IO.mapRequired("Function", Info.ElementIndex); |
| 445 | } else if (Info.Kind == wasm::WASM_SYMBOL_TYPE_GLOBAL) { |
| 446 | IO.mapRequired("Global", Info.ElementIndex); |
Heejin Ahn | da419bd | 2018-11-14 02:46:21 +0000 | [diff] [blame] | 447 | } else if (Info.Kind == wasm::WASM_SYMBOL_TYPE_EVENT) { |
| 448 | IO.mapRequired("Event", Info.ElementIndex); |
Sam Clegg | 6c899ba | 2018-02-23 05:08:34 +0000 | [diff] [blame] | 449 | } else if (Info.Kind == wasm::WASM_SYMBOL_TYPE_DATA) { |
| 450 | if ((Info.Flags & wasm::WASM_SYMBOL_UNDEFINED) == 0) { |
| 451 | IO.mapRequired("Segment", Info.DataRef.Segment); |
| 452 | IO.mapOptional("Offset", Info.DataRef.Offset, 0u); |
| 453 | IO.mapRequired("Size", Info.DataRef.Size); |
| 454 | } |
Sam Clegg | 6a31a0d | 2018-04-26 19:27:28 +0000 | [diff] [blame] | 455 | } else if (Info.Kind == wasm::WASM_SYMBOL_TYPE_SECTION) { |
| 456 | IO.mapRequired("Section", Info.ElementIndex); |
Sam Clegg | 6c899ba | 2018-02-23 05:08:34 +0000 | [diff] [blame] | 457 | } else { |
| 458 | llvm_unreachable("unsupported symbol kind"); |
| 459 | } |
Sam Clegg | b7787fd | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 460 | } |
| 461 | |
Heejin Ahn | da419bd | 2018-11-14 02:46:21 +0000 | [diff] [blame] | 462 | void MappingTraits<WasmYAML::Event>::mapping(IO &IO, WasmYAML::Event &Event) { |
| 463 | IO.mapRequired("Index", Event.Index); |
| 464 | IO.mapRequired("Attribute", Event.Attribute); |
| 465 | IO.mapRequired("SigIndex", Event.SigIndex); |
| 466 | } |
| 467 | |
Sam Clegg | 0fc5599 | 2017-12-13 22:02:25 +0000 | [diff] [blame] | 468 | void ScalarBitSetTraits<WasmYAML::LimitFlags>::bitset( |
| 469 | IO &IO, WasmYAML::LimitFlags &Value) { |
| 470 | #define BCase(X) IO.bitSetCase(Value, #X, wasm::WASM_LIMITS_FLAG_##X) |
| 471 | BCase(HAS_MAX); |
Derek Schuff | 6881806 | 2018-11-06 17:27:25 +0000 | [diff] [blame] | 472 | BCase(IS_SHARED); |
Sam Clegg | 0fc5599 | 2017-12-13 22:02:25 +0000 | [diff] [blame] | 473 | #undef BCase |
| 474 | } |
| 475 | |
| 476 | void ScalarBitSetTraits<WasmYAML::SegmentFlags>::bitset( |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 477 | IO &IO, WasmYAML::SegmentFlags &Value) {} |
Sam Clegg | 0fc5599 | 2017-12-13 22:02:25 +0000 | [diff] [blame] | 478 | |
| 479 | void ScalarBitSetTraits<WasmYAML::SymbolFlags>::bitset( |
| 480 | IO &IO, WasmYAML::SymbolFlags &Value) { |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 481 | #define BCaseMask(M, X) \ |
| 482 | IO.maskedBitSetCase(Value, #X, wasm::WASM_SYMBOL_##X, wasm::WASM_SYMBOL_##M) |
| 483 | // BCaseMask(BINDING_MASK, BINDING_GLOBAL); |
Sam Clegg | 0fc5599 | 2017-12-13 22:02:25 +0000 | [diff] [blame] | 484 | BCaseMask(BINDING_MASK, BINDING_WEAK); |
| 485 | BCaseMask(BINDING_MASK, BINDING_LOCAL); |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 486 | // BCaseMask(VISIBILITY_MASK, VISIBILITY_DEFAULT); |
Sam Clegg | 0fc5599 | 2017-12-13 22:02:25 +0000 | [diff] [blame] | 487 | BCaseMask(VISIBILITY_MASK, VISIBILITY_HIDDEN); |
Sam Clegg | 6c899ba | 2018-02-23 05:08:34 +0000 | [diff] [blame] | 488 | BCaseMask(UNDEFINED, UNDEFINED); |
Sam Clegg | d6ef8da | 2019-02-07 01:24:44 +0000 | [diff] [blame^] | 489 | BCaseMask(EXPORTED, EXPORTED); |
Sam Clegg | 0fc5599 | 2017-12-13 22:02:25 +0000 | [diff] [blame] | 490 | #undef BCaseMask |
| 491 | } |
| 492 | |
Sam Clegg | 6c899ba | 2018-02-23 05:08:34 +0000 | [diff] [blame] | 493 | void ScalarEnumerationTraits<WasmYAML::SymbolKind>::enumeration( |
| 494 | IO &IO, WasmYAML::SymbolKind &Kind) { |
| 495 | #define ECase(X) IO.enumCase(Kind, #X, wasm::WASM_SYMBOL_TYPE_##X); |
| 496 | ECase(FUNCTION); |
| 497 | ECase(DATA); |
| 498 | ECase(GLOBAL); |
Sam Clegg | 6a31a0d | 2018-04-26 19:27:28 +0000 | [diff] [blame] | 499 | ECase(SECTION); |
Heejin Ahn | da419bd | 2018-11-14 02:46:21 +0000 | [diff] [blame] | 500 | ECase(EVENT); |
Sam Clegg | 6c899ba | 2018-02-23 05:08:34 +0000 | [diff] [blame] | 501 | #undef ECase |
| 502 | } |
| 503 | |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 504 | void ScalarEnumerationTraits<WasmYAML::ValueType>::enumeration( |
| 505 | IO &IO, WasmYAML::ValueType &Type) { |
| 506 | #define ECase(X) IO.enumCase(Type, #X, wasm::WASM_TYPE_##X); |
| 507 | ECase(I32); |
| 508 | ECase(I64); |
| 509 | ECase(F32); |
| 510 | ECase(F64); |
Thomas Lively | 6f21a13 | 2018-09-20 22:04:44 +0000 | [diff] [blame] | 511 | ECase(V128); |
Thomas Lively | 6a87dda | 2019-01-08 06:25:55 +0000 | [diff] [blame] | 512 | ECase(FUNCREF); |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 513 | ECase(FUNC); |
| 514 | ECase(NORESULT); |
| 515 | #undef ECase |
| 516 | } |
| 517 | |
| 518 | void ScalarEnumerationTraits<WasmYAML::ExportKind>::enumeration( |
| 519 | IO &IO, WasmYAML::ExportKind &Kind) { |
| 520 | #define ECase(X) IO.enumCase(Kind, #X, wasm::WASM_EXTERNAL_##X); |
| 521 | ECase(FUNCTION); |
| 522 | ECase(TABLE); |
| 523 | ECase(MEMORY); |
| 524 | ECase(GLOBAL); |
Heejin Ahn | da419bd | 2018-11-14 02:46:21 +0000 | [diff] [blame] | 525 | ECase(EVENT); |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 526 | #undef ECase |
| 527 | } |
| 528 | |
| 529 | void ScalarEnumerationTraits<WasmYAML::Opcode>::enumeration( |
| 530 | IO &IO, WasmYAML::Opcode &Code) { |
| 531 | #define ECase(X) IO.enumCase(Code, #X, wasm::WASM_OPCODE_##X); |
| 532 | ECase(END); |
| 533 | ECase(I32_CONST); |
| 534 | ECase(I64_CONST); |
| 535 | ECase(F64_CONST); |
| 536 | ECase(F32_CONST); |
Thomas Lively | 6a87dda | 2019-01-08 06:25:55 +0000 | [diff] [blame] | 537 | ECase(GLOBAL_GET); |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 538 | #undef ECase |
| 539 | } |
| 540 | |
| 541 | void ScalarEnumerationTraits<WasmYAML::TableType>::enumeration( |
| 542 | IO &IO, WasmYAML::TableType &Type) { |
| 543 | #define ECase(X) IO.enumCase(Type, #X, wasm::WASM_TYPE_##X); |
Thomas Lively | 6a87dda | 2019-01-08 06:25:55 +0000 | [diff] [blame] | 544 | ECase(FUNCREF); |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 545 | #undef ECase |
| 546 | } |
| 547 | |
| 548 | void ScalarEnumerationTraits<WasmYAML::RelocType>::enumeration( |
| 549 | IO &IO, WasmYAML::RelocType &Type) { |
| 550 | #define WASM_RELOC(name, value) IO.enumCase(Type, #name, wasm::name); |
Sam Clegg | c5d8bc8 | 2017-12-21 03:16:34 +0000 | [diff] [blame] | 551 | #include "llvm/BinaryFormat/WasmRelocs.def" |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 552 | #undef WASM_RELOC |
| 553 | } |
| 554 | |
| 555 | } // end namespace yaml |
Eugene Zelenko | 28082ab | 2017-07-01 01:35:55 +0000 | [diff] [blame] | 556 | |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 557 | } // end namespace llvm |