LLVM  10.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 #include "llvm/ADT/StringRef.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 
47  std::vector<std::pair<std::string, std::string>> Languages;
48  std::vector<std::pair<std::string, std::string>> Tools;
49  std::vector<std::pair<std::string, std::string>> SDKs;
50 };
51 
53  uint8_t Prefix;
54  std::string Name;
55 };
56 
57 struct WasmExport {
59  uint8_t Kind;
61 };
62 
63 struct WasmLimits {
64  uint8_t Flags;
67 };
68 
69 struct WasmTable {
70  uint8_t ElemType;
72 };
73 
74 struct WasmInitExpr {
75  uint8_t Opcode;
76  union {
77  int32_t Int32;
78  int64_t Int64;
79  int32_t Float32;
80  int64_t Float64;
82  } Value;
83 };
84 
86  uint8_t Type;
87  bool Mutable;
88 };
89 
90 struct WasmGlobal {
94  StringRef SymbolName; // from the "linking" section
95 };
96 
97 struct WasmEventType {
98  // Kind of event. Currently only WASM_EVENT_ATTRIBUTE_EXCEPTION is possible.
101 };
102 
103 struct WasmEvent {
106  StringRef SymbolName; // from the "linking" section
107 };
108 
109 struct WasmImport {
112  uint8_t Kind;
113  union {
119  };
120 };
121 
123  uint8_t Type;
125 };
126 
127 struct WasmFunction {
129  std::vector<WasmLocalDecl> Locals;
133  uint32_t CodeOffset; // start of Locals and Body
134  StringRef SymbolName; // from the "linking" section
135  StringRef DebugName; // from the "name" section
136  uint32_t Comdat; // from the "comdat info" section
137 };
138 
141  uint32_t MemoryIndex; // present if InitFlags & WASM_SEGMENT_HAS_MEMINDEX
142  WasmInitExpr Offset; // present if InitFlags & WASM_SEGMENT_IS_PASSIVE == 0
144  StringRef Name; // from the "segment info" section
147  uint32_t Comdat; // from the "comdat info" section
148 };
149 
153  std::vector<uint32_t> Functions;
154 };
155 
156 // Represents the location of a Wasm data symbol within a WasmDataSegment, as
157 // the index of the segment, and the offset and size within the segment.
162 };
163 
165  uint8_t Type; // The type of the relocation.
166  uint32_t Index; // Index into either symbol or type index space.
167  uint64_t Offset; // Offset from the start of the section.
168  int64_t Addend; // A value to add to the symbol.
169 };
170 
171 struct WasmInitFunc {
174 };
175 
178  uint8_t Kind;
180  StringRef ImportModule; // For undefined symbols the module of the import
181  StringRef ImportName; // For undefined symbols the name of the import
182  union {
183  // For function or global symbols, the index in function or global index
184  // space.
186  // For a data symbols, the address of the data relative to segment.
188  };
189 };
190 
194 };
195 
198  std::vector<WasmInitFunc> InitFunctions;
199  std::vector<StringRef> Comdats;
200  std::vector<WasmSymbolInfo> SymbolTable;
201 };
202 
203 enum : unsigned {
204  WASM_SEC_CUSTOM = 0, // Custom / User-defined section
205  WASM_SEC_TYPE = 1, // Function signature declarations
206  WASM_SEC_IMPORT = 2, // Import declarations
207  WASM_SEC_FUNCTION = 3, // Function declarations
208  WASM_SEC_TABLE = 4, // Indirect function table and other tables
209  WASM_SEC_MEMORY = 5, // Memory attributes
210  WASM_SEC_GLOBAL = 6, // Global declarations
211  WASM_SEC_EXPORT = 7, // Exports
212  WASM_SEC_START = 8, // Start function declaration
213  WASM_SEC_ELEM = 9, // Elements section
214  WASM_SEC_CODE = 10, // Function bodies (code)
215  WASM_SEC_DATA = 11, // Data segments
216  WASM_SEC_DATACOUNT = 12, // Data segment count
217  WASM_SEC_EVENT = 13 // Event declarations
218 };
219 
220 // Type immediate encodings used in various contexts.
221 enum : unsigned {
230  WASM_TYPE_NORESULT = 0x40, // for blocks with no result values
231 };
232 
233 // Kinds of externals (for imports and exports).
234 enum : unsigned {
240 };
241 
242 // Opcodes used in initializer expressions.
243 enum : unsigned {
255 };
256 
257 // Opcodes used in synthetic functions.
258 enum : unsigned {
270 };
271 
272 enum : unsigned {
275 };
276 
277 enum : unsigned {
280 };
281 
282 // Feature policy prefixes used in the custom "target_features" section
283 enum : uint8_t {
287 };
288 
289 // Kind codes used in the custom "name" section
290 enum : unsigned {
293 };
294 
295 // Kind codes used in the custom "linking" section
296 enum : unsigned {
301 };
302 
303 // Kind codes used in the custom "linking" section in the WASM_COMDAT_INFO
304 enum : unsigned {
307 };
308 
309 // Kind codes used in the custom "linking" section in the WASM_SYMBOL_TABLE
310 enum WasmSymbolType : unsigned {
316 };
317 
318 // Kinds of event attributes.
319 enum WasmEventAttribute : unsigned {
321 };
322 
323 const unsigned WASM_SYMBOL_BINDING_MASK = 0x3;
324 const unsigned WASM_SYMBOL_VISIBILITY_MASK = 0xc;
325 
326 const unsigned WASM_SYMBOL_BINDING_GLOBAL = 0x0;
327 const unsigned WASM_SYMBOL_BINDING_WEAK = 0x1;
328 const unsigned WASM_SYMBOL_BINDING_LOCAL = 0x2;
329 const unsigned WASM_SYMBOL_VISIBILITY_DEFAULT = 0x0;
330 const unsigned WASM_SYMBOL_VISIBILITY_HIDDEN = 0x4;
331 const unsigned WASM_SYMBOL_UNDEFINED = 0x10;
332 const unsigned WASM_SYMBOL_EXPORTED = 0x20;
333 const unsigned WASM_SYMBOL_EXPLICIT_NAME = 0x40;
334 const unsigned WASM_SYMBOL_NO_STRIP = 0x80;
335 
336 #define WASM_RELOC(name, value) name = value,
337 
338 enum : unsigned {
339 #include "WasmRelocs.def"
340 };
341 
342 #undef WASM_RELOC
343 
344 // Subset of types that a value can have
345 enum class ValType {
346  I32 = WASM_TYPE_I32,
347  I64 = WASM_TYPE_I64,
348  F32 = WASM_TYPE_F32,
349  F64 = WASM_TYPE_F64,
352 };
353 
357  // Support empty and tombstone instances, needed by DenseMap.
358  enum { Plain, Empty, Tombstone } State = Plain;
359 
361  SmallVector<ValType, 4> &&InParams)
362  : Returns(InReturns), Params(InParams) {}
363  WasmSignature() = default;
364 };
365 
366 // Useful comparison operators
367 inline bool operator==(const WasmSignature &LHS, const WasmSignature &RHS) {
368  return LHS.State == RHS.State && LHS.Returns == RHS.Returns &&
369  LHS.Params == RHS.Params;
370 }
371 
372 inline bool operator!=(const WasmSignature &LHS, const WasmSignature &RHS) {
373  return !(LHS == RHS);
374 }
375 
376 inline bool operator==(const WasmGlobalType &LHS, const WasmGlobalType &RHS) {
377  return LHS.Type == RHS.Type && LHS.Mutable == RHS.Mutable;
378 }
379 
380 inline bool operator!=(const WasmGlobalType &LHS, const WasmGlobalType &RHS) {
381  return !(LHS == RHS);
382 }
383 
384 std::string toString(WasmSymbolType type);
385 std::string relocTypetoString(uint32_t type);
386 bool relocTypeHasAddend(uint32_t type);
387 
388 } // end namespace wasm
389 } // end namespace llvm
390 
391 #endif
uint32_t TableAlignment
Definition: Wasm.h:42
std::vector< WasmInitFunc > InitFunctions
Definition: Wasm.h:198
WasmSymbolType
Definition: Wasm.h:310
This class represents lattice values for constants.
Definition: AllocatorList.h:23
bool relocTypeHasAddend(uint32_t type)
Definition: Wasm.cpp:39
const unsigned WASM_SYMBOL_BINDING_LOCAL
Definition: Wasm.h:328
StringRef SymbolName
Definition: Wasm.h:106
WasmGlobalType Type
Definition: Wasm.h:92
const uint32_t WasmMetadataVersion
Definition: Wasm.h:29
std::vector< std::pair< std::string, std::string > > Languages
Definition: Wasm.h:47
StringRef ImportModule
Definition: Wasm.h:180
std::vector< StringRef > Comdats
Definition: Wasm.h:199
WasmEventType Type
Definition: Wasm.h:105
StringRef SymbolName
Definition: Wasm.h:134
const unsigned WASM_SYMBOL_UNDEFINED
Definition: Wasm.h:331
bool operator==(const WasmSignature &LHS, const WasmSignature &RHS)
Definition: Wasm.h:367
WasmTable Table
Definition: Wasm.h:116
SmallVector< ValType, 1 > Returns
Definition: Wasm.h:355
const unsigned WASM_SYMBOL_NO_STRIP
Definition: Wasm.h:334
StringRef Module
Definition: Wasm.h:110
bool operator!=(const WasmSignature &LHS, const WasmSignature &RHS)
Definition: Wasm.h:372
uint32_t CodeOffset
Definition: Wasm.h:133
const uint32_t WasmVersion
Definition: Wasm.h:27
SmallVector< ValType, 4 > Params
Definition: Wasm.h:356
ArrayRef< uint8_t > Content
Definition: Wasm.h:143
ArrayRef< uint8_t > Body
Definition: Wasm.h:130
uint32_t Index
Definition: Wasm.h:104
uint32_t Attribute
Definition: Wasm.h:99
std::string relocTypetoString(uint32_t type)
Definition: Wasm.cpp:27
uint32_t Initial
Definition: Wasm.h:65
const unsigned WASM_SYMBOL_VISIBILITY_DEFAULT
Definition: Wasm.h:329
uint32_t SigIndex
Definition: Wasm.h:114
WasmLimits Memory
Definition: Wasm.h:117
std::vector< uint32_t > Functions
Definition: Wasm.h:153
const unsigned WASM_SYMBOL_VISIBILITY_HIDDEN
Definition: Wasm.h:330
uint8_t ElemType
Definition: Wasm.h:70
uint32_t MemoryAlignment
Definition: Wasm.h:40
const unsigned WASM_SYMBOL_BINDING_WEAK
Definition: Wasm.h:327
uint32_t CodeSectionOffset
Definition: Wasm.h:131
WasmInitExpr Offset
Definition: Wasm.h:142
WasmEventType Event
Definition: Wasm.h:118
const unsigned WASM_SYMBOL_VISIBILITY_MASK
Definition: Wasm.h:324
StringRef DebugName
Definition: Wasm.h:135
std::vector< std::pair< std::string, std::string > > SDKs
Definition: Wasm.h:49
const char WasmMagic[]
Definition: Wasm.h:25
std::vector< StringRef > Needed
Definition: Wasm.h:43
std::string toString(WasmSymbolType type)
Definition: Wasm.cpp:11
WasmInitExpr InitExpr
Definition: Wasm.h:93
const unsigned WASM_SYMBOL_EXPLICIT_NAME
Definition: Wasm.h:333
const unsigned WASM_SYMBOL_BINDING_GLOBAL
Definition: Wasm.h:326
WasmEventAttribute
Definition: Wasm.h:319
std::vector< std::pair< std::string, std::string > > Tools
Definition: Wasm.h:48
WasmSignature(SmallVector< ValType, 1 > &&InReturns, SmallVector< ValType, 4 > &&InParams)
Definition: Wasm.h:360
WasmLimits Limits
Definition: Wasm.h:71
enum llvm::wasm::WasmSignature::@156 State
WasmGlobalType Global
Definition: Wasm.h:115
const unsigned WASM_SYMBOL_BINDING_MASK
Definition: Wasm.h:323
uint32_t Index
Definition: Wasm.h:60
WasmDataReference DataRef
Definition: Wasm.h:187
uint32_t Index
Definition: Wasm.h:91
StringRef Name
Definition: Wasm.h:58
const uint32_t WasmPageSize
Definition: Wasm.h:31
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
StringRef SymbolName
Definition: Wasm.h:94
const unsigned WASM_SYMBOL_EXPORTED
Definition: Wasm.h:332
std::vector< WasmSymbolInfo > SymbolTable
Definition: Wasm.h:200
std::vector< WasmLocalDecl > Locals
Definition: Wasm.h:129
WasmInitExpr Offset
Definition: Wasm.h:152
uint32_t Maximum
Definition: Wasm.h:66
StringRef Field
Definition: Wasm.h:111
StringRef ImportName
Definition: Wasm.h:181