LLVM  9.0.0svn
Wasm.h
Go to the documentation of this file.
1 //===- Wasm.h - Wasm object file format -------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines manifest constants for the wasm object file format.
10 // See: https://github.com/WebAssembly/design/blob/master/BinaryEncoding.md
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_BINARYFORMAT_WASM_H
15 #define LLVM_BINARYFORMAT_WASM_H
16 
17 #include "llvm/ADT/ArrayRef.h"
18 #include "llvm/ADT/SmallVector.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 
38  uint32_t MemorySize; // Memory size in bytes
39  uint32_t MemoryAlignment; // P2 alignment of memory
40  uint32_t TableSize; // Table size in elements
41  uint32_t TableAlignment; // P2 alignment of table
42  std::vector<StringRef> Needed; // Shared library depenedencies
43 };
44 
46  std::vector<std::pair<std::string, std::string>> Languages;
47  std::vector<std::pair<std::string, std::string>> Tools;
48  std::vector<std::pair<std::string, std::string>> SDKs;
49 };
50 
51 struct WasmExport {
53  uint8_t Kind;
55 };
56 
57 struct WasmLimits {
58  uint8_t Flags;
61 };
62 
63 struct WasmTable {
64  uint8_t ElemType;
66 };
67 
68 struct WasmInitExpr {
69  uint8_t Opcode;
70  union {
71  int32_t Int32;
72  int64_t Int64;
73  int32_t Float32;
74  int64_t Float64;
76  } Value;
77 };
78 
80  uint8_t Type;
81  bool Mutable;
82 };
83 
84 struct WasmGlobal {
88  StringRef SymbolName; // from the "linking" section
89 };
90 
91 struct WasmEventType {
92  // Kind of event. Currently only WASM_EVENT_ATTRIBUTE_EXCEPTION is possible.
95 };
96 
97 struct WasmEvent {
100  StringRef SymbolName; // from the "linking" section
101 };
102 
103 struct WasmImport {
106  uint8_t Kind;
107  union {
113  };
114 };
115 
117  uint8_t Type;
119 };
120 
121 struct WasmFunction {
123  std::vector<WasmLocalDecl> Locals;
127  uint32_t CodeOffset; // start of Locals and Body
128  StringRef SymbolName; // from the "linking" section
129  StringRef DebugName; // from the "name" section
130  uint32_t Comdat; // from the "comdat info" section
131 };
132 
137  StringRef Name; // from the "segment info" section
140  uint32_t Comdat; // from the "comdat info" section
141 };
142 
146  std::vector<uint32_t> Functions;
147 };
148 
149 // Represents the location of a Wasm data symbol within a WasmDataSegment, as
150 // the index of the segment, and the offset and size within the segment.
155 };
156 
158  uint8_t Type; // The type of the relocation.
159  uint32_t Index; // Index into either symbol or type index space.
160  uint64_t Offset; // Offset from the start of the section.
161  int64_t Addend; // A value to add to the symbol.
162 };
163 
164 struct WasmInitFunc {
167 };
168 
171  uint8_t Kind;
173  StringRef Module; // For undefined symbols the module name of the import
174  union {
175  // For function or global symbols, the index in function or global index
176  // space.
178  // For a data symbols, the address of the data relative to segment.
180  };
181 };
182 
186 };
187 
190  std::vector<WasmInitFunc> InitFunctions;
191  std::vector<StringRef> Comdats;
192  std::vector<WasmSymbolInfo> SymbolTable;
193 };
194 
195 enum : unsigned {
196  WASM_SEC_CUSTOM = 0, // Custom / User-defined section
197  WASM_SEC_TYPE = 1, // Function signature declarations
198  WASM_SEC_IMPORT = 2, // Import declarations
199  WASM_SEC_FUNCTION = 3, // Function declarations
200  WASM_SEC_TABLE = 4, // Indirect function table and other tables
201  WASM_SEC_MEMORY = 5, // Memory attributes
202  WASM_SEC_GLOBAL = 6, // Global declarations
203  WASM_SEC_EXPORT = 7, // Exports
204  WASM_SEC_START = 8, // Start function declaration
205  WASM_SEC_ELEM = 9, // Elements section
206  WASM_SEC_CODE = 10, // Function bodies (code)
207  WASM_SEC_DATA = 11, // Data segments
208  WASM_SEC_DATACOUNT = 12, // Data segment count
209  WASM_SEC_EVENT = 13 // Event declarations
210 };
211 
212 // Type immediate encodings used in various contexts.
213 enum : unsigned {
222  WASM_TYPE_NORESULT = 0x40, // for blocks with no result values
223 };
224 
225 // Kinds of externals (for imports and exports).
226 enum : unsigned {
232 };
233 
234 // Opcodes used in initializer expressions.
235 enum : unsigned {
242 };
243 
244 enum : unsigned {
247 };
248 
249 // Kind codes used in the custom "name" section
250 enum : unsigned {
253 };
254 
255 // Kind codes used in the custom "linking" section
256 enum : unsigned {
261 };
262 
263 // Kind codes used in the custom "linking" section in the WASM_COMDAT_INFO
264 enum : unsigned {
267 };
268 
269 // Kind codes used in the custom "linking" section in the WASM_SYMBOL_TABLE
270 enum WasmSymbolType : unsigned {
276 };
277 
278 // Kinds of event attributes.
279 enum WasmEventAttribute : unsigned {
281 };
282 
283 const unsigned WASM_SYMBOL_BINDING_MASK = 0x3;
284 const unsigned WASM_SYMBOL_VISIBILITY_MASK = 0xc;
285 
286 const unsigned WASM_SYMBOL_BINDING_GLOBAL = 0x0;
287 const unsigned WASM_SYMBOL_BINDING_WEAK = 0x1;
288 const unsigned WASM_SYMBOL_BINDING_LOCAL = 0x2;
289 const unsigned WASM_SYMBOL_VISIBILITY_DEFAULT = 0x0;
290 const unsigned WASM_SYMBOL_VISIBILITY_HIDDEN = 0x4;
291 const unsigned WASM_SYMBOL_UNDEFINED = 0x10;
292 
293 #define WASM_RELOC(name, value) name = value,
294 
295 enum : unsigned {
296 #include "WasmRelocs.def"
297 };
298 
299 #undef WASM_RELOC
300 
301 // Subset of types that a value can have
302 enum class ValType {
303  I32 = WASM_TYPE_I32,
304  I64 = WASM_TYPE_I64,
305  F32 = WASM_TYPE_F32,
306  F64 = WASM_TYPE_F64,
309 };
310 
314  // Support empty and tombstone instances, needed by DenseMap.
315  enum { Plain, Empty, Tombstone } State = Plain;
316 
319  : Returns(InReturns), Params(InParams) {}
320  WasmSignature() = default;
321 };
322 
323 // Useful comparison operators
324 inline bool operator==(const WasmSignature &LHS, const WasmSignature &RHS) {
325  return LHS.State == RHS.State && LHS.Returns == RHS.Returns &&
326  LHS.Params == RHS.Params;
327 }
328 
329 inline bool operator!=(const WasmSignature &LHS, const WasmSignature &RHS) {
330  return !(LHS == RHS);
331 }
332 
333 inline bool operator==(const WasmGlobalType &LHS, const WasmGlobalType &RHS) {
334  return LHS.Type == RHS.Type && LHS.Mutable == RHS.Mutable;
335 }
336 
337 inline bool operator!=(const WasmGlobalType &LHS, const WasmGlobalType &RHS) {
338  return !(LHS == RHS);
339 }
340 
341 std::string toString(wasm::WasmSymbolType type);
342 std::string relocTypetoString(uint32_t type);
343 
344 } // end namespace wasm
345 } // end namespace llvm
346 
347 #endif
uint32_t TableAlignment
Definition: Wasm.h:41
std::vector< WasmInitFunc > InitFunctions
Definition: Wasm.h:190
WasmSymbolType
Definition: Wasm.h:270
This class represents lattice values for constants.
Definition: AllocatorList.h:23
const unsigned WASM_SYMBOL_BINDING_LOCAL
Definition: Wasm.h:288
StringRef SymbolName
Definition: Wasm.h:100
WasmGlobalType Type
Definition: Wasm.h:86
const uint32_t WasmMetadataVersion
Definition: Wasm.h:28
SmallVector< wasm::ValType, 1 > Returns
Definition: Wasm.h:312
std::vector< std::pair< std::string, std::string > > Languages
Definition: Wasm.h:46
std::vector< StringRef > Comdats
Definition: Wasm.h:191
WasmEventType Type
Definition: Wasm.h:99
StringRef SymbolName
Definition: Wasm.h:128
const unsigned WASM_SYMBOL_UNDEFINED
Definition: Wasm.h:291
bool operator==(const WasmSignature &LHS, const WasmSignature &RHS)
Definition: Wasm.h:324
WasmTable Table
Definition: Wasm.h:110
StringRef Module
Definition: Wasm.h:104
bool operator!=(const WasmSignature &LHS, const WasmSignature &RHS)
Definition: Wasm.h:329
uint32_t CodeOffset
Definition: Wasm.h:127
const uint32_t WasmVersion
Definition: Wasm.h:26
ArrayRef< uint8_t > Content
Definition: Wasm.h:136
ArrayRef< uint8_t > Body
Definition: Wasm.h:124
uint32_t Index
Definition: Wasm.h:98
uint32_t Attribute
Definition: Wasm.h:93
std::string relocTypetoString(uint32_t type)
Definition: Wasm.cpp:27
uint32_t Initial
Definition: Wasm.h:59
const unsigned WASM_SYMBOL_VISIBILITY_DEFAULT
Definition: Wasm.h:289
uint32_t SigIndex
Definition: Wasm.h:108
WasmLimits Memory
Definition: Wasm.h:111
std::vector< uint32_t > Functions
Definition: Wasm.h:146
const unsigned WASM_SYMBOL_VISIBILITY_HIDDEN
Definition: Wasm.h:290
uint8_t ElemType
Definition: Wasm.h:64
uint32_t MemoryAlignment
Definition: Wasm.h:39
const unsigned WASM_SYMBOL_BINDING_WEAK
Definition: Wasm.h:287
uint32_t CodeSectionOffset
Definition: Wasm.h:125
WasmInitExpr Offset
Definition: Wasm.h:135
enum llvm::wasm::WasmSignature::@148 State
WasmEventType Event
Definition: Wasm.h:112
const unsigned WASM_SYMBOL_VISIBILITY_MASK
Definition: Wasm.h:284
StringRef DebugName
Definition: Wasm.h:129
std::vector< std::pair< std::string, std::string > > SDKs
Definition: Wasm.h:48
const char WasmMagic[]
Definition: Wasm.h:24
std::vector< StringRef > Needed
Definition: Wasm.h:42
WasmInitExpr InitExpr
Definition: Wasm.h:87
const unsigned WASM_SYMBOL_BINDING_GLOBAL
Definition: Wasm.h:286
WasmEventAttribute
Definition: Wasm.h:279
std::vector< std::pair< std::string, std::string > > Tools
Definition: Wasm.h:47
WasmLimits Limits
Definition: Wasm.h:65
WasmGlobalType Global
Definition: Wasm.h:109
const unsigned WASM_SYMBOL_BINDING_MASK
Definition: Wasm.h:283
WasmSignature(SmallVector< wasm::ValType, 1 > &&InReturns, SmallVector< wasm::ValType, 4 > &&InParams)
Definition: Wasm.h:317
uint32_t Index
Definition: Wasm.h:54
WasmDataReference DataRef
Definition: Wasm.h:179
LLVM Value Representation.
Definition: Value.h:72
uint32_t Index
Definition: Wasm.h:85
StringRef Name
Definition: Wasm.h:52
const uint32_t WasmPageSize
Definition: Wasm.h:30
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
StringRef SymbolName
Definition: Wasm.h:88
std::string toString(wasm::WasmSymbolType type)
Definition: Wasm.cpp:11
std::vector< WasmSymbolInfo > SymbolTable
Definition: Wasm.h:192
std::vector< WasmLocalDecl > Locals
Definition: Wasm.h:123
SmallVector< wasm::ValType, 4 > Params
Definition: Wasm.h:313
WasmInitExpr Offset
Definition: Wasm.h:145
uint32_t Maximum
Definition: Wasm.h:60
StringRef Field
Definition: Wasm.h:105