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