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