LCOV - code coverage report
Current view: top level - lib/ObjectYAML - WasmYAML.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 277 284 97.5 %
Date: 2017-09-14 15:23:50 Functions: 38 38 100.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.13