LLVM  8.0.0svn
Wasm.h
Go to the documentation of this file.
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 
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 // Wasm linking metadata version
29 // Wasm uses a 64k page size
30 const uint32_t WasmPageSize = 65536;
31 
35 };
36 
37 struct WasmSignature {
38  std::vector<uint8_t> ParamTypes;
39  uint8_t ReturnType;
40 };
41 
42 struct WasmExport {
44  uint8_t Kind;
46 };
47 
48 struct WasmLimits {
49  uint8_t Flags;
52 };
53 
54 struct WasmTable {
55  uint8_t ElemType;
57 };
58 
59 struct WasmInitExpr {
60  uint8_t Opcode;
61  union {
62  int32_t Int32;
63  int64_t Int64;
64  int32_t Float32;
65  int64_t Float64;
67  } Value;
68 };
69 
71  uint8_t Type;
72  bool Mutable;
73 };
74 
75 struct WasmGlobal {
79  StringRef SymbolName; // from the "linking" section
80 };
81 
82 struct WasmImport {
85  uint8_t Kind;
86  union {
91  };
92 };
93 
94 struct WasmLocalDecl {
95  uint8_t Type;
97 };
98 
99 struct WasmFunction {
101  std::vector<WasmLocalDecl> Locals;
105  uint32_t CodeOffset; // start of Locals and Body
106  StringRef SymbolName; // from the "linking" section
107  StringRef DebugName; // from the "name" section
108  uint32_t Comdat; // from the "comdat info" section
109 };
110 
115  StringRef Name; // from the "segment info" section
118  uint32_t Comdat; // from the "comdat info" section
119 };
120 
124  std::vector<uint32_t> Functions;
125 };
126 
127 // Represents the location of a Wasm data symbol within a WasmDataSegment, as
128 // the index of the segment, and the offset and size within the segment.
133 };
134 
136  uint8_t Type; // The type of the relocation.
137  uint32_t Index; // Index into either symbol or type index space.
138  uint64_t Offset; // Offset from the start of the section.
139  int64_t Addend; // A value to add to the symbol.
140 };
141 
142 struct WasmInitFunc {
145 };
146 
149  uint8_t Kind;
151  StringRef Module; // For undefined symbols the module name of the import
152  union {
153  // For function or global symbols, the index in function or global index
154  // space.
156  // For a data symbols, the address of the data relative to segment.
158  };
159 };
160 
164 };
165 
168  std::vector<WasmInitFunc> InitFunctions;
169  std::vector<StringRef> Comdats;
170  std::vector<WasmSymbolInfo> SymbolTable;
171 };
172 
173 enum : unsigned {
174  WASM_SEC_CUSTOM = 0, // Custom / User-defined section
175  WASM_SEC_TYPE = 1, // Function signature declarations
176  WASM_SEC_IMPORT = 2, // Import declarations
177  WASM_SEC_FUNCTION = 3, // Function declarations
178  WASM_SEC_TABLE = 4, // Indirect function table and other tables
179  WASM_SEC_MEMORY = 5, // Memory attributes
180  WASM_SEC_GLOBAL = 6, // Global declarations
181  WASM_SEC_EXPORT = 7, // Exports
182  WASM_SEC_START = 8, // Start function declaration
183  WASM_SEC_ELEM = 9, // Elements section
184  WASM_SEC_CODE = 10, // Function bodies (code)
185  WASM_SEC_DATA = 11 // Data segments
186 };
187 
188 // Type immediate encodings used in various contexts.
189 enum : unsigned {
198  WASM_TYPE_NORESULT = 0x40, // for blocks with no result values
199 };
200 
201 // Kinds of externals (for imports and exports).
202 enum : unsigned {
207 };
208 
209 // Opcodes used in initializer expressions.
210 enum : unsigned {
217 };
218 
219 enum : unsigned {
221 };
222 
223 // Subset of types that a value can have
224 enum class ValType {
225  I32 = WASM_TYPE_I32,
226  I64 = WASM_TYPE_I64,
227  F32 = WASM_TYPE_F32,
228  F64 = WASM_TYPE_F64,
231 };
232 
233 // Kind codes used in the custom "name" section
234 enum : unsigned {
237 };
238 
239 // Kind codes used in the custom "linking" section
240 enum : unsigned {
245 };
246 
247 // Kind codes used in the custom "linking" section in the WASM_COMDAT_INFO
248 enum : unsigned {
251 };
252 
253 // Kind codes used in the custom "linking" section in the WASM_SYMBOL_TABLE
254 enum WasmSymbolType : unsigned {
259 };
260 
261 const unsigned WASM_SYMBOL_BINDING_MASK = 0x3;
262 const unsigned WASM_SYMBOL_VISIBILITY_MASK = 0xc;
263 
264 const unsigned WASM_SYMBOL_BINDING_GLOBAL = 0x0;
265 const unsigned WASM_SYMBOL_BINDING_WEAK = 0x1;
266 const unsigned WASM_SYMBOL_BINDING_LOCAL = 0x2;
267 const unsigned WASM_SYMBOL_VISIBILITY_DEFAULT = 0x0;
268 const unsigned WASM_SYMBOL_VISIBILITY_HIDDEN = 0x4;
269 const unsigned WASM_SYMBOL_UNDEFINED = 0x10;
270 
271 #define WASM_RELOC(name, value) name = value,
272 
273 enum : unsigned {
274 #include "WasmRelocs.def"
275 };
276 
277 #undef WASM_RELOC
278 
279 // Useful comparison operators
280 inline bool operator==(const WasmSignature &LHS, const WasmSignature &RHS) {
281  return LHS.ReturnType == RHS.ReturnType && LHS.ParamTypes == RHS.ParamTypes;
282 }
283 
284 inline bool operator!=(const WasmSignature &LHS, const WasmSignature &RHS) {
285  return !(LHS == RHS);
286 }
287 
288 inline bool operator==(const WasmGlobalType &LHS, const WasmGlobalType &RHS) {
289  return LHS.Type == RHS.Type && LHS.Mutable == RHS.Mutable;
290 }
291 
292 inline bool operator!=(const WasmGlobalType &LHS, const WasmGlobalType &RHS) {
293  return !(LHS == RHS);
294 }
295 
296 std::string toString(wasm::WasmSymbolType type);
297 std::string relocTypetoString(uint32_t type);
298 
299 } // end namespace wasm
300 } // end namespace llvm
301 
302 #endif
std::vector< WasmInitFunc > InitFunctions
Definition: Wasm.h:168
WasmSymbolType
Definition: Wasm.h:254
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
const unsigned WASM_SYMBOL_BINDING_LOCAL
Definition: Wasm.h:266
WasmGlobalType Type
Definition: Wasm.h:77
const uint32_t WasmMetadataVersion
Definition: Wasm.h:28
std::vector< StringRef > Comdats
Definition: Wasm.h:169
StringRef SymbolName
Definition: Wasm.h:106
const unsigned WASM_SYMBOL_UNDEFINED
Definition: Wasm.h:269
bool operator==(const WasmSignature &LHS, const WasmSignature &RHS)
Definition: Wasm.h:280
WasmTable Table
Definition: Wasm.h:89
StringRef Module
Definition: Wasm.h:83
bool operator!=(const WasmSignature &LHS, const WasmSignature &RHS)
Definition: Wasm.h:284
uint32_t CodeOffset
Definition: Wasm.h:105
const uint32_t WasmVersion
Definition: Wasm.h:26
ArrayRef< uint8_t > Content
Definition: Wasm.h:114
ArrayRef< uint8_t > Body
Definition: Wasm.h:102
std::string relocTypetoString(uint32_t type)
Definition: Wasm.cpp:26
uint32_t Initial
Definition: Wasm.h:50
const unsigned WASM_SYMBOL_VISIBILITY_DEFAULT
Definition: Wasm.h:267
uint32_t SigIndex
Definition: Wasm.h:87
WasmLimits Memory
Definition: Wasm.h:90
std::vector< uint32_t > Functions
Definition: Wasm.h:124
const unsigned WASM_SYMBOL_VISIBILITY_HIDDEN
Definition: Wasm.h:268
uint8_t ElemType
Definition: Wasm.h:55
const unsigned WASM_SYMBOL_BINDING_WEAK
Definition: Wasm.h:265
uint32_t CodeSectionOffset
Definition: Wasm.h:103
WasmInitExpr Offset
Definition: Wasm.h:113
const unsigned WASM_SYMBOL_VISIBILITY_MASK
Definition: Wasm.h:262
StringRef DebugName
Definition: Wasm.h:107
const char WasmMagic[]
Definition: Wasm.h:24
WasmInitExpr InitExpr
Definition: Wasm.h:78
std::vector< uint8_t > ParamTypes
Definition: Wasm.h:38
const unsigned WASM_SYMBOL_BINDING_GLOBAL
Definition: Wasm.h:264
WasmLimits Limits
Definition: Wasm.h:56
WasmGlobalType Global
Definition: Wasm.h:88
const unsigned WASM_SYMBOL_BINDING_MASK
Definition: Wasm.h:261
uint32_t Index
Definition: Wasm.h:45
WasmDataReference DataRef
Definition: Wasm.h:157
LLVM Value Representation.
Definition: Value.h:73
uint32_t Index
Definition: Wasm.h:76
StringRef Name
Definition: Wasm.h:43
const uint32_t WasmPageSize
Definition: Wasm.h:30
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
StringRef SymbolName
Definition: Wasm.h:79
std::string toString(wasm::WasmSymbolType type)
Definition: Wasm.cpp:12
std::vector< WasmSymbolInfo > SymbolTable
Definition: Wasm.h:170
std::vector< WasmLocalDecl > Locals
Definition: Wasm.h:101
WasmInitExpr Offset
Definition: Wasm.h:123
uint32_t Maximum
Definition: Wasm.h:51
StringRef Field
Definition: Wasm.h:84