Home | History | Annotate | Download | only in Support
      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_SUPPORT_WASM_H
     16 #define LLVM_SUPPORT_WASM_H
     17 
     18 #include "llvm/ADT/ArrayRef.h"
     19 
     20 namespace llvm {
     21 namespace wasm {
     22 
     23 // Object file magic string.
     24 const char WasmMagic[] = {'\0', 'a', 's', 'm'};
     25 // Wasm binary format version
     26 const uint32_t WasmVersion = 0x1;
     27 
     28 struct WasmObjectHeader {
     29   StringRef Magic;
     30   uint32_t Version;
     31 };
     32 
     33 struct WasmSignature {
     34   std::vector<int32_t> ParamTypes;
     35   int32_t ReturnType;
     36 };
     37 
     38 struct WasmImport {
     39   StringRef Module;
     40   StringRef Field;
     41   uint32_t Kind;
     42   union {
     43     uint32_t SigIndex;
     44     int32_t GlobalType;
     45   };
     46   bool GlobalMutable;
     47 };
     48 
     49 struct WasmExport {
     50   StringRef Name;
     51   uint32_t Kind;
     52   uint32_t Index;
     53 };
     54 
     55 struct WasmLimits {
     56   uint32_t Flags;
     57   uint32_t Initial;
     58   uint32_t Maximum;
     59 };
     60 
     61 struct WasmTable {
     62   int32_t ElemType;
     63   WasmLimits Limits;
     64 };
     65 
     66 struct WasmInitExpr {
     67   uint8_t Opcode;
     68   union {
     69     int32_t Int32;
     70     int64_t Int64;
     71     int32_t Float32;
     72     int64_t Float64;
     73     uint32_t Global;
     74   } Value;
     75 };
     76 
     77 struct WasmGlobal {
     78   int32_t Type;
     79   bool Mutable;
     80   WasmInitExpr InitExpr;
     81 };
     82 
     83 struct WasmLocalDecl {
     84   int32_t Type;
     85   uint32_t Count;
     86 };
     87 
     88 struct WasmFunction {
     89   std::vector<WasmLocalDecl> Locals;
     90   ArrayRef<uint8_t> Body;
     91 };
     92 
     93 struct WasmDataSegment {
     94   uint32_t Index;
     95   WasmInitExpr Offset;
     96   ArrayRef<uint8_t> Content;
     97 };
     98 
     99 struct WasmElemSegment {
    100   uint32_t TableIndex;
    101   WasmInitExpr Offset;
    102   std::vector<uint32_t> Functions;
    103 };
    104 
    105 struct WasmRelocation {
    106   uint32_t Type;         // The type of the relocation.
    107   int32_t Index;         // Index into function to global index space.
    108   uint64_t Offset;       // Offset from the start of the section.
    109   uint64_t Addend;       // A value to add to the symbol.
    110 };
    111 
    112 enum : unsigned {
    113   WASM_SEC_CUSTOM = 0,   // Custom / User-defined section
    114   WASM_SEC_TYPE = 1,     // Function signature declarations
    115   WASM_SEC_IMPORT = 2,   // Import declarations
    116   WASM_SEC_FUNCTION = 3, // Function declarations
    117   WASM_SEC_TABLE = 4,    // Indirect function table and other tables
    118   WASM_SEC_MEMORY = 5,   // Memory attributes
    119   WASM_SEC_GLOBAL = 6,   // Global declarations
    120   WASM_SEC_EXPORT = 7,   // Exports
    121   WASM_SEC_START = 8,    // Start function declaration
    122   WASM_SEC_ELEM = 9,     // Elements section
    123   WASM_SEC_CODE = 10,    // Function bodies (code)
    124   WASM_SEC_DATA = 11     // Data segments
    125 };
    126 
    127 // Type immediate encodings used in various contexts.
    128 enum {
    129   WASM_TYPE_I32          = -0x01,
    130   WASM_TYPE_I64          = -0x02,
    131   WASM_TYPE_F32          = -0x03,
    132   WASM_TYPE_F64          = -0x04,
    133   WASM_TYPE_ANYFUNC      = -0x10,
    134   WASM_TYPE_FUNC         = -0x20,
    135   WASM_TYPE_NORESULT     = -0x40, // for blocks with no result values
    136 };
    137 
    138 // Kinds of externals (for imports and exports).
    139 enum : unsigned {
    140   WASM_EXTERNAL_FUNCTION = 0x0,
    141   WASM_EXTERNAL_TABLE    = 0x1,
    142   WASM_EXTERNAL_MEMORY   = 0x2,
    143   WASM_EXTERNAL_GLOBAL   = 0x3,
    144 };
    145 
    146 // Opcodes used in initializer expressions.
    147 enum : unsigned {
    148   WASM_OPCODE_END        = 0x0b,
    149   WASM_OPCODE_GET_GLOBAL = 0x23,
    150   WASM_OPCODE_I32_CONST  = 0x41,
    151   WASM_OPCODE_I64_CONST  = 0x42,
    152   WASM_OPCODE_F32_CONST  = 0x43,
    153   WASM_OPCODE_F64_CONST  = 0x44,
    154 };
    155 
    156 enum : unsigned {
    157   WASM_NAMES_FUNCTION    = 0x1,
    158   WASM_NAMES_LOCAL       = 0x2,
    159 };
    160 
    161 enum : unsigned {
    162   WASM_LIMITS_FLAG_HAS_MAX = 0x1,
    163 };
    164 
    165 // Subset of types that a value can have
    166 enum class ValType {
    167   I32 = WASM_TYPE_I32,
    168   I64 = WASM_TYPE_I64,
    169   F32 = WASM_TYPE_F32,
    170   F64 = WASM_TYPE_F64,
    171 };
    172 
    173 // Linking metadata kinds.
    174 enum : unsigned {
    175   WASM_STACK_POINTER = 0x1,
    176 };
    177 
    178 #define WASM_RELOC(name, value) name = value,
    179 
    180 enum : unsigned {
    181 #include "WasmRelocs/WebAssembly.def"
    182 };
    183 
    184 #undef WASM_RELOC
    185 
    186 struct Global {
    187   ValType Type;
    188   bool Mutable;
    189 
    190   // The initial value for this global is either the value of an imported
    191   // global, in which case InitialModule and InitialName specify the global
    192   // import, or a value, in which case InitialModule is empty and InitialValue
    193   // holds the value.
    194   StringRef InitialModule;
    195   StringRef InitialName;
    196   uint64_t InitialValue;
    197 };
    198 
    199 } // end namespace wasm
    200 } // end namespace llvm
    201 
    202 #endif
    203