LLVM  8.0.1
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 #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
30 // Wasm uses a 64k page size
31 const uint32_t WasmPageSize = 65536;
32 
36 };
37 
39  uint32_t MemorySize; // Memory size in bytes
40  uint32_t MemoryAlignment; // P2 alignment of memory
41  uint32_t TableSize; // Table size in elements
42  uint32_t TableAlignment; // P2 alignment of table
43  std::vector<StringRef> Needed; // Shared library depenedencies
44 };
45 
46 struct WasmExport {
48  uint8_t Kind;
50 };
51 
52 struct WasmLimits {
53  uint8_t Flags;
56 };
57 
58 struct WasmTable {
59  uint8_t ElemType;
61 };
62 
63 struct WasmInitExpr {
64  uint8_t Opcode;
65  union {
66  int32_t Int32;
67  int64_t Int64;
68  int32_t Float32;
69  int64_t Float64;
71  } Value;
72 };
73 
75  uint8_t Type;
76  bool Mutable;
77 };
78 
79 struct WasmGlobal {
83  StringRef SymbolName; // from the "linking" section
84 };
85 
86 struct WasmEventType {
87  // Kind of event. Currently only WASM_EVENT_ATTRIBUTE_EXCEPTION is possible.
90 };
91 
92 struct WasmEvent {
95  StringRef SymbolName; // from the "linking" section
96 };
97 
98 struct WasmImport {
101  uint8_t Kind;
102  union {
108  };
109 };
110 
112  uint8_t Type;
114 };
115 
116 struct WasmFunction {
118  std::vector<WasmLocalDecl> Locals;
122  uint32_t CodeOffset; // start of Locals and Body
123  StringRef SymbolName; // from the "linking" section
124  StringRef DebugName; // from the "name" section
125  uint32_t Comdat; // from the "comdat info" section
126 };
127 
132  StringRef Name; // from the "segment info" section
135  uint32_t Comdat; // from the "comdat info" section
136 };
137 
141  std::vector<uint32_t> Functions;
142 };
143 
144 // Represents the location of a Wasm data symbol within a WasmDataSegment, as
145 // the index of the segment, and the offset and size within the segment.
150 };
151 
153  uint8_t Type; // The type of the relocation.
154  uint32_t Index; // Index into either symbol or type index space.
155  uint64_t Offset; // Offset from the start of the section.
156  int64_t Addend; // A value to add to the symbol.
157 };
158 
159 struct WasmInitFunc {
162 };
163 
166  uint8_t Kind;
168  StringRef ImportModule; // For undefined symbols the module of the import
169  StringRef ImportName; // For undefined symbols the name of the import
170  union {
171  // For function or global symbols, the index in function or global index
172  // space.
174  // For a data symbols, the address of the data relative to segment.
176  };
177 };
178 
182 };
183 
186  std::vector<WasmInitFunc> InitFunctions;
187  std::vector<StringRef> Comdats;
188  std::vector<WasmSymbolInfo> SymbolTable;
189 };
190 
191 enum : unsigned {
192  WASM_SEC_CUSTOM = 0, // Custom / User-defined section
193  WASM_SEC_TYPE = 1, // Function signature declarations
194  WASM_SEC_IMPORT = 2, // Import declarations
195  WASM_SEC_FUNCTION = 3, // Function declarations
196  WASM_SEC_TABLE = 4, // Indirect function table and other tables
197  WASM_SEC_MEMORY = 5, // Memory attributes
198  WASM_SEC_GLOBAL = 6, // Global declarations
199  WASM_SEC_EXPORT = 7, // Exports
200  WASM_SEC_START = 8, // Start function declaration
201  WASM_SEC_ELEM = 9, // Elements section
202  WASM_SEC_CODE = 10, // Function bodies (code)
203  WASM_SEC_DATA = 11, // Data segments
204  WASM_SEC_DATACOUNT = 12, // Data segment count
205  WASM_SEC_EVENT = 13 // Event declarations
206 };
207 
208 // Type immediate encodings used in various contexts.
209 enum : unsigned {
218  WASM_TYPE_NORESULT = 0x40, // for blocks with no result values
219 };
220 
221 // Kinds of externals (for imports and exports).
222 enum : unsigned {
228 };
229 
230 // Opcodes used in initializer expressions.
231 enum : unsigned {
238 };
239 
240 enum : unsigned {
243 };
244 
245 // Kind codes used in the custom "name" section
246 enum : unsigned {
249 };
250 
251 // Kind codes used in the custom "linking" section
252 enum : unsigned {
257 };
258 
259 // Kind codes used in the custom "linking" section in the WASM_COMDAT_INFO
260 enum : unsigned {
263 };
264 
265 // Kind codes used in the custom "linking" section in the WASM_SYMBOL_TABLE
266 enum WasmSymbolType : unsigned {
272 };
273 
274 // Kinds of event attributes.
275 enum WasmEventAttribute : unsigned {
277 };
278 
279 const unsigned WASM_SYMBOL_BINDING_MASK = 0x3;
280 const unsigned WASM_SYMBOL_VISIBILITY_MASK = 0xc;
281 
282 const unsigned WASM_SYMBOL_BINDING_GLOBAL = 0x0;
283 const unsigned WASM_SYMBOL_BINDING_WEAK = 0x1;
284 const unsigned WASM_SYMBOL_BINDING_LOCAL = 0x2;
285 const unsigned WASM_SYMBOL_VISIBILITY_DEFAULT = 0x0;
286 const unsigned WASM_SYMBOL_VISIBILITY_HIDDEN = 0x4;
287 const unsigned WASM_SYMBOL_UNDEFINED = 0x10;
288 const unsigned WASM_SYMBOL_EXPLICIT_NAME = 0x40;
289 
290 #define WASM_RELOC(name, value) name = value,
291 
292 enum : unsigned {
293 #include "WasmRelocs.def"
294 };
295 
296 #undef WASM_RELOC
297 
298 // Subset of types that a value can have
299 enum class ValType {
300  I32 = WASM_TYPE_I32,
301  I64 = WASM_TYPE_I64,
302  F32 = WASM_TYPE_F32,
303  F64 = WASM_TYPE_F64,
306 };
307 
311  // Support empty and tombstone instances, needed by DenseMap.
312  enum { Plain, Empty, Tombstone } State = Plain;
313 
316  : Returns(InReturns), Params(InParams) {}
317  WasmSignature() = default;
318 };
319 
320 // Useful comparison operators
321 inline bool operator==(const WasmSignature &LHS, const WasmSignature &RHS) {
322  return LHS.State == RHS.State && LHS.Returns == RHS.Returns &&
323  LHS.Params == RHS.Params;
324 }
325 
326 inline bool operator!=(const WasmSignature &LHS, const WasmSignature &RHS) {
327  return !(LHS == RHS);
328 }
329 
330 inline bool operator==(const WasmGlobalType &LHS, const WasmGlobalType &RHS) {
331  return LHS.Type == RHS.Type && LHS.Mutable == RHS.Mutable;
332 }
333 
334 inline bool operator!=(const WasmGlobalType &LHS, const WasmGlobalType &RHS) {
335  return !(LHS == RHS);
336 }
337 
338 std::string toString(wasm::WasmSymbolType type);
339 std::string relocTypetoString(uint32_t type);
340 
341 } // end namespace wasm
342 } // end namespace llvm
343 
344 #endif
uint32_t TableAlignment
Definition: Wasm.h:42
std::vector< WasmInitFunc > InitFunctions
Definition: Wasm.h:186
WasmSymbolType
Definition: Wasm.h:266
This class represents lattice values for constants.
Definition: AllocatorList.h:24
const unsigned WASM_SYMBOL_BINDING_LOCAL
Definition: Wasm.h:284
StringRef SymbolName
Definition: Wasm.h:95
WasmGlobalType Type
Definition: Wasm.h:81
const uint32_t WasmMetadataVersion
Definition: Wasm.h:29
SmallVector< wasm::ValType, 1 > Returns
Definition: Wasm.h:309
StringRef ImportModule
Definition: Wasm.h:168
std::vector< StringRef > Comdats
Definition: Wasm.h:187
WasmEventType Type
Definition: Wasm.h:94
StringRef SymbolName
Definition: Wasm.h:123
const unsigned WASM_SYMBOL_UNDEFINED
Definition: Wasm.h:287
bool operator==(const WasmSignature &LHS, const WasmSignature &RHS)
Definition: Wasm.h:321
WasmTable Table
Definition: Wasm.h:105
StringRef Module
Definition: Wasm.h:99
bool operator!=(const WasmSignature &LHS, const WasmSignature &RHS)
Definition: Wasm.h:326
uint32_t CodeOffset
Definition: Wasm.h:122
const uint32_t WasmVersion
Definition: Wasm.h:27
ArrayRef< uint8_t > Content
Definition: Wasm.h:131
ArrayRef< uint8_t > Body
Definition: Wasm.h:119
uint32_t Index
Definition: Wasm.h:93
uint32_t Attribute
Definition: Wasm.h:88
std::string relocTypetoString(uint32_t type)
Definition: Wasm.cpp:28
uint32_t Initial
Definition: Wasm.h:54
const unsigned WASM_SYMBOL_VISIBILITY_DEFAULT
Definition: Wasm.h:285
uint32_t SigIndex
Definition: Wasm.h:103
WasmLimits Memory
Definition: Wasm.h:106
std::vector< uint32_t > Functions
Definition: Wasm.h:141
const unsigned WASM_SYMBOL_VISIBILITY_HIDDEN
Definition: Wasm.h:286
uint8_t ElemType
Definition: Wasm.h:59
uint32_t MemoryAlignment
Definition: Wasm.h:40
const unsigned WASM_SYMBOL_BINDING_WEAK
Definition: Wasm.h:283
uint32_t CodeSectionOffset
Definition: Wasm.h:120
WasmInitExpr Offset
Definition: Wasm.h:130
enum llvm::wasm::WasmSignature::@148 State
WasmEventType Event
Definition: Wasm.h:107
const unsigned WASM_SYMBOL_VISIBILITY_MASK
Definition: Wasm.h:280
StringRef DebugName
Definition: Wasm.h:124
const char WasmMagic[]
Definition: Wasm.h:25
std::vector< StringRef > Needed
Definition: Wasm.h:43
WasmInitExpr InitExpr
Definition: Wasm.h:82
const unsigned WASM_SYMBOL_EXPLICIT_NAME
Definition: Wasm.h:288
const unsigned WASM_SYMBOL_BINDING_GLOBAL
Definition: Wasm.h:282
WasmEventAttribute
Definition: Wasm.h:275
WasmLimits Limits
Definition: Wasm.h:60
WasmGlobalType Global
Definition: Wasm.h:104
const unsigned WASM_SYMBOL_BINDING_MASK
Definition: Wasm.h:279
WasmSignature(SmallVector< wasm::ValType, 1 > &&InReturns, SmallVector< wasm::ValType, 4 > &&InParams)
Definition: Wasm.h:314
uint32_t Index
Definition: Wasm.h:49
WasmDataReference DataRef
Definition: Wasm.h:175
LLVM Value Representation.
Definition: Value.h:73
uint32_t Index
Definition: Wasm.h:80
StringRef Name
Definition: Wasm.h:47
const uint32_t WasmPageSize
Definition: Wasm.h:31
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
StringRef SymbolName
Definition: Wasm.h:83
std::string toString(wasm::WasmSymbolType type)
Definition: Wasm.cpp:12
std::vector< WasmSymbolInfo > SymbolTable
Definition: Wasm.h:188
std::vector< WasmLocalDecl > Locals
Definition: Wasm.h:118
SmallVector< wasm::ValType, 4 > Params
Definition: Wasm.h:310
WasmInitExpr Offset
Definition: Wasm.h:140
uint32_t Maximum
Definition: Wasm.h:55
StringRef Field
Definition: Wasm.h:100
StringRef ImportName
Definition: Wasm.h:169