LCOV - code coverage report
Current view: top level - include/llvm/BinaryFormat - Wasm.h (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 11 13 84.6 %
Date: 2018-10-20 13:21:21 Functions: 2 3 66.7 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===- Wasm.h - Wasm object file format -------------------------*- C++ -*-===//
       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 manifest constants for the wasm object file format.
      11             : // See: https://github.com/WebAssembly/design/blob/master/BinaryEncoding.md
      12             : //
      13             : //===----------------------------------------------------------------------===//
      14             : 
      15             : #ifndef LLVM_BINARYFORMAT_WASM_H
      16             : #define LLVM_BINARYFORMAT_WASM_H
      17             : 
      18             : #include "llvm/ADT/ArrayRef.h"
      19             : #include "llvm/ADT/SmallVector.h"
      20             : 
      21             : namespace llvm {
      22             : namespace wasm {
      23             : 
      24             : // Object file magic string.
      25             : const char WasmMagic[] = {'\0', 'a', 's', 'm'};
      26             : // Wasm binary format version
      27             : const uint32_t WasmVersion = 0x1;
      28             : // Wasm linking metadata version
      29             : const uint32_t WasmMetadataVersion = 0x1;
      30             : // Wasm uses a 64k page size
      31             : const uint32_t WasmPageSize = 65536;
      32             : 
      33             : struct WasmObjectHeader {
      34             :   StringRef Magic;
      35             :   uint32_t Version;
      36             : };
      37             : 
      38             : struct WasmExport {
      39             :   StringRef Name;
      40             :   uint8_t Kind;
      41             :   uint32_t Index;
      42             : };
      43             : 
      44             : struct WasmLimits {
      45             :   uint8_t Flags;
      46             :   uint32_t Initial;
      47             :   uint32_t Maximum;
      48             : };
      49             : 
      50             : struct WasmTable {
      51             :   uint8_t ElemType;
      52             :   WasmLimits Limits;
      53             : };
      54             : 
      55             : struct WasmInitExpr {
      56             :   uint8_t Opcode;
      57             :   union {
      58             :     int32_t Int32;
      59             :     int64_t Int64;
      60             :     int32_t Float32;
      61             :     int64_t Float64;
      62             :     uint32_t Global;
      63             :   } Value;
      64             : };
      65             : 
      66             : struct WasmGlobalType {
      67             :   uint8_t Type;
      68             :   bool Mutable;
      69             : };
      70             : 
      71             : struct WasmGlobal {
      72             :   uint32_t Index;
      73             :   WasmGlobalType Type;
      74             :   WasmInitExpr InitExpr;
      75             :   StringRef SymbolName; // from the "linking" section
      76             : };
      77             : 
      78             : struct WasmImport {
      79             :   StringRef Module;
      80             :   StringRef Field;
      81             :   uint8_t Kind;
      82             :   union {
      83             :     uint32_t SigIndex;
      84             :     WasmGlobalType Global;
      85             :     WasmTable Table;
      86             :     WasmLimits Memory;
      87             :   };
      88             : };
      89             : 
      90             : struct WasmLocalDecl {
      91             :   uint8_t Type;
      92             :   uint32_t Count;
      93             : };
      94             : 
      95        4154 : struct WasmFunction {
      96             :   uint32_t Index;
      97             :   std::vector<WasmLocalDecl> Locals;
      98             :   ArrayRef<uint8_t> Body;
      99             :   uint32_t CodeSectionOffset;
     100             :   uint32_t Size;
     101             :   uint32_t CodeOffset;  // start of Locals and Body
     102             :   StringRef SymbolName; // from the "linking" section
     103             :   StringRef DebugName;  // from the "name" section
     104             :   uint32_t Comdat;      // from the "comdat info" section
     105             : };
     106             : 
     107             : struct WasmDataSegment {
     108             :   uint32_t MemoryIndex;
     109             :   WasmInitExpr Offset;
     110             :   ArrayRef<uint8_t> Content;
     111             :   StringRef Name; // from the "segment info" section
     112             :   uint32_t Alignment;
     113             :   uint32_t Flags;
     114             :   uint32_t Comdat; // from the "comdat info" section
     115             : };
     116             : 
     117         102 : struct WasmElemSegment {
     118             :   uint32_t TableIndex;
     119             :   WasmInitExpr Offset;
     120             :   std::vector<uint32_t> Functions;
     121             : };
     122             : 
     123             : // Represents the location of a Wasm data symbol within a WasmDataSegment, as
     124             : // the index of the segment, and the offset and size within the segment.
     125             : struct WasmDataReference {
     126             :   uint32_t Segment;
     127             :   uint32_t Offset;
     128             :   uint32_t Size;
     129             : };
     130             : 
     131             : struct WasmRelocation {
     132             :   uint8_t Type;    // The type of the relocation.
     133             :   uint32_t Index;  // Index into either symbol or type index space.
     134             :   uint64_t Offset; // Offset from the start of the section.
     135             :   int64_t Addend;  // A value to add to the symbol.
     136             : };
     137             : 
     138             : struct WasmInitFunc {
     139             :   uint32_t Priority;
     140             :   uint32_t Symbol;
     141             : };
     142             : 
     143             : struct WasmSymbolInfo {
     144             :   StringRef Name;
     145             :   uint8_t Kind;
     146             :   uint32_t Flags;
     147             :   StringRef Module; // For undefined symbols the module name of the import
     148             :   union {
     149             :     // For function or global symbols, the index in function or global index
     150             :     // space.
     151             :     uint32_t ElementIndex;
     152             :     // For a data symbols, the address of the data relative to segment.
     153             :     WasmDataReference DataRef;
     154             :   };
     155             : };
     156             : 
     157             : struct WasmFunctionName {
     158             :   uint32_t Index;
     159             :   StringRef Name;
     160             : };
     161             : 
     162             : struct WasmLinkingData {
     163             :   uint32_t Version;
     164             :   std::vector<WasmInitFunc> InitFunctions;
     165             :   std::vector<StringRef> Comdats;
     166             :   std::vector<WasmSymbolInfo> SymbolTable;
     167             : };
     168             : 
     169             : enum : unsigned {
     170             :   WASM_SEC_CUSTOM = 0,   // Custom / User-defined section
     171             :   WASM_SEC_TYPE = 1,     // Function signature declarations
     172             :   WASM_SEC_IMPORT = 2,   // Import declarations
     173             :   WASM_SEC_FUNCTION = 3, // Function declarations
     174             :   WASM_SEC_TABLE = 4,    // Indirect function table and other tables
     175             :   WASM_SEC_MEMORY = 5,   // Memory attributes
     176             :   WASM_SEC_GLOBAL = 6,   // Global declarations
     177             :   WASM_SEC_EXPORT = 7,   // Exports
     178             :   WASM_SEC_START = 8,    // Start function declaration
     179             :   WASM_SEC_ELEM = 9,     // Elements section
     180             :   WASM_SEC_CODE = 10,    // Function bodies (code)
     181             :   WASM_SEC_DATA = 11     // Data segments
     182             : };
     183             : 
     184             : // Type immediate encodings used in various contexts.
     185             : enum : unsigned {
     186             :   WASM_TYPE_I32 = 0x7F,
     187             :   WASM_TYPE_I64 = 0x7E,
     188             :   WASM_TYPE_F32 = 0x7D,
     189             :   WASM_TYPE_F64 = 0x7C,
     190             :   WASM_TYPE_V128 = 0x7B,
     191             :   WASM_TYPE_ANYFUNC = 0x70,
     192             :   WASM_TYPE_EXCEPT_REF = 0x68,
     193             :   WASM_TYPE_FUNC = 0x60,
     194             :   WASM_TYPE_NORESULT = 0x40, // for blocks with no result values
     195             : };
     196             : 
     197             : // Kinds of externals (for imports and exports).
     198             : enum : unsigned {
     199             :   WASM_EXTERNAL_FUNCTION = 0x0,
     200             :   WASM_EXTERNAL_TABLE = 0x1,
     201             :   WASM_EXTERNAL_MEMORY = 0x2,
     202             :   WASM_EXTERNAL_GLOBAL = 0x3,
     203             : };
     204             : 
     205             : // Opcodes used in initializer expressions.
     206             : enum : unsigned {
     207             :   WASM_OPCODE_END = 0x0b,
     208             :   WASM_OPCODE_GET_GLOBAL = 0x23,
     209             :   WASM_OPCODE_I32_CONST = 0x41,
     210             :   WASM_OPCODE_I64_CONST = 0x42,
     211             :   WASM_OPCODE_F32_CONST = 0x43,
     212             :   WASM_OPCODE_F64_CONST = 0x44,
     213             : };
     214             : 
     215             : enum : unsigned {
     216             :   WASM_LIMITS_FLAG_HAS_MAX = 0x1,
     217             : };
     218             : 
     219             : // Kind codes used in the custom "name" section
     220             : enum : unsigned {
     221             :   WASM_NAMES_FUNCTION = 0x1,
     222             :   WASM_NAMES_LOCAL = 0x2,
     223             : };
     224             : 
     225             : // Kind codes used in the custom "linking" section
     226             : enum : unsigned {
     227             :   WASM_SEGMENT_INFO = 0x5,
     228             :   WASM_INIT_FUNCS = 0x6,
     229             :   WASM_COMDAT_INFO = 0x7,
     230             :   WASM_SYMBOL_TABLE = 0x8,
     231             : };
     232             : 
     233             : // Kind codes used in the custom "linking" section in the WASM_COMDAT_INFO
     234             : enum : unsigned {
     235             :   WASM_COMDAT_DATA = 0x0,
     236             :   WASM_COMDAT_FUNCTION = 0x1,
     237             : };
     238             : 
     239             : // Kind codes used in the custom "linking" section in the WASM_SYMBOL_TABLE
     240             : enum WasmSymbolType : unsigned {
     241             :   WASM_SYMBOL_TYPE_FUNCTION = 0x0,
     242             :   WASM_SYMBOL_TYPE_DATA = 0x1,
     243             :   WASM_SYMBOL_TYPE_GLOBAL = 0x2,
     244             :   WASM_SYMBOL_TYPE_SECTION = 0x3,
     245             : };
     246             : 
     247             : const unsigned WASM_SYMBOL_BINDING_MASK = 0x3;
     248             : const unsigned WASM_SYMBOL_VISIBILITY_MASK = 0xc;
     249             : 
     250             : const unsigned WASM_SYMBOL_BINDING_GLOBAL = 0x0;
     251             : const unsigned WASM_SYMBOL_BINDING_WEAK = 0x1;
     252             : const unsigned WASM_SYMBOL_BINDING_LOCAL = 0x2;
     253             : const unsigned WASM_SYMBOL_VISIBILITY_DEFAULT = 0x0;
     254             : const unsigned WASM_SYMBOL_VISIBILITY_HIDDEN = 0x4;
     255             : const unsigned WASM_SYMBOL_UNDEFINED = 0x10;
     256             : 
     257             : #define WASM_RELOC(name, value) name = value,
     258             : 
     259             : enum : unsigned {
     260             : #include "WasmRelocs.def"
     261             : };
     262             : 
     263             : #undef WASM_RELOC
     264             : 
     265             : // Subset of types that a value can have
     266             : enum class ValType {
     267             :   I32 = WASM_TYPE_I32,
     268             :   I64 = WASM_TYPE_I64,
     269             :   F32 = WASM_TYPE_F32,
     270             :   F64 = WASM_TYPE_F64,
     271             :   V128 = WASM_TYPE_V128,
     272             :   EXCEPT_REF = WASM_TYPE_EXCEPT_REF,
     273             : };
     274             : 
     275           0 : struct WasmSignature {
     276             :   SmallVector<wasm::ValType, 1> Returns;
     277             :   SmallVector<wasm::ValType, 4> Params;
     278             :   // Support empty and tombstone instances, needed by DenseMap.
     279             :   enum { Plain, Empty, Tombstone } State = Plain;
     280             : 
     281         285 :   WasmSignature(SmallVector<wasm::ValType, 1> &&InReturns,
     282             :                 SmallVector<wasm::ValType, 4> &&InParams)
     283         285 :       : Returns(InReturns), Params(InParams) {}
     284         505 :   WasmSignature() = default;
     285             : };
     286             : 
     287             : // Useful comparison operators
     288        7894 : inline bool operator==(const WasmSignature &LHS, const WasmSignature &RHS) {
     289       22577 :   return LHS.State == RHS.State && LHS.Returns == RHS.Returns &&
     290        7894 :          LHS.Params == RHS.Params;
     291             : }
     292             : 
     293             : inline bool operator!=(const WasmSignature &LHS, const WasmSignature &RHS) {
     294          65 :   return !(LHS == RHS);
     295             : }
     296             : 
     297           0 : inline bool operator==(const WasmGlobalType &LHS, const WasmGlobalType &RHS) {
     298           3 :   return LHS.Type == RHS.Type && LHS.Mutable == RHS.Mutable;
     299             : }
     300             : 
     301             : inline bool operator!=(const WasmGlobalType &LHS, const WasmGlobalType &RHS) {
     302           3 :   return !(LHS == RHS);
     303             : }
     304             : 
     305             : std::string toString(wasm::WasmSymbolType type);
     306             : std::string relocTypetoString(uint32_t type);
     307             : 
     308             : } // end namespace wasm
     309             : } // end namespace llvm
     310             : 
     311             : #endif

Generated by: LCOV version 1.13