LLVM  7.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 uses a 64k page size
28 const uint32_t WasmPageSize = 65536;
29 
33 };
34 
35 struct WasmSignature {
36  std::vector<uint8_t> ParamTypes;
37  uint8_t ReturnType;
38 };
39 
40 struct WasmExport {
42  uint8_t Kind;
44 };
45 
46 struct WasmLimits {
47  uint8_t Flags;
50 };
51 
52 struct WasmTable {
53  uint8_t ElemType;
55 };
56 
57 struct WasmInitExpr {
58  uint8_t Opcode;
59  union {
60  int32_t Int32;
61  int64_t Int64;
62  int32_t Float32;
63  int64_t Float64;
65  } Value;
66 };
67 
69  uint8_t Type;
70  bool Mutable;
71 };
72 
73 struct WasmGlobal {
77  StringRef SymbolName; // from the "linking" section
78 };
79 
80 struct WasmImport {
83  uint8_t Kind;
84  union {
89  };
90 };
91 
92 struct WasmLocalDecl {
93  uint8_t Type;
95 };
96 
97 struct WasmFunction {
99  std::vector<WasmLocalDecl> Locals;
103  StringRef SymbolName; // from the "linking" section
104  StringRef DebugName; // from the "name" section
105  uint32_t Comdat; // from the "comdat info" section
106 };
107 
112  StringRef Name; // from the "segment info" section
115  uint32_t Comdat; // from the "comdat info" section
116 };
117 
121  std::vector<uint32_t> Functions;
122 };
123 
124 // Represents the location of a Wasm data symbol within a WasmDataSegment, as
125 // the index of the segment, and the offset and size within the segment.
130 };
131 
133  uint8_t Type; // The type of the relocation.
134  uint32_t Index; // Index into either symbol or type index space.
135  uint64_t Offset; // Offset from the start of the section.
136  int64_t Addend; // A value to add to the symbol.
137 };
138 
139 struct WasmInitFunc {
142 };
143 
146  uint8_t Kind;
148  union {
149  // For function or global symbols, the index in function or global index
150  // space.
152  // For a data symbols, the address of the data relative to segment.
154  };
155 };
156 
160 };
161 
163  std::vector<WasmInitFunc> InitFunctions;
164  std::vector<StringRef> Comdats;
165  std::vector<WasmSymbolInfo> SymbolTable;
166 };
167 
168 enum : unsigned {
169  WASM_SEC_CUSTOM = 0, // Custom / User-defined section
170  WASM_SEC_TYPE = 1, // Function signature declarations
171  WASM_SEC_IMPORT = 2, // Import declarations
172  WASM_SEC_FUNCTION = 3, // Function declarations
173  WASM_SEC_TABLE = 4, // Indirect function table and other tables
174  WASM_SEC_MEMORY = 5, // Memory attributes
175  WASM_SEC_GLOBAL = 6, // Global declarations
176  WASM_SEC_EXPORT = 7, // Exports
177  WASM_SEC_START = 8, // Start function declaration
178  WASM_SEC_ELEM = 9, // Elements section
179  WASM_SEC_CODE = 10, // Function bodies (code)
180  WASM_SEC_DATA = 11 // Data segments
181 };
182 
183 // Type immediate encodings used in various contexts.
184 enum : unsigned {
192  WASM_TYPE_NORESULT = 0x40, // for blocks with no result values
193 };
194 
195 // Kinds of externals (for imports and exports).
196 enum : unsigned {
201 };
202 
203 // Opcodes used in initializer expressions.
204 enum : unsigned {
211 };
212 
213 enum : unsigned {
215 };
216 
217 // Subset of types that a value can have
218 enum class ValType {
219  I32 = WASM_TYPE_I32,
220  I64 = WASM_TYPE_I64,
221  F32 = WASM_TYPE_F32,
222  F64 = WASM_TYPE_F64,
224 };
225 
226 // Kind codes used in the custom "name" section
227 enum : unsigned {
230 };
231 
232 // Kind codes used in the custom "linking" section
233 enum : unsigned {
238 };
239 
240 // Kind codes used in the custom "linking" section in the WASM_COMDAT_INFO
241 enum : unsigned {
244 };
245 
246 // Kind codes used in the custom "linking" section in the WASM_SYMBOL_TABLE
247 enum WasmSymbolType : unsigned {
251 };
252 
253 const unsigned WASM_SYMBOL_BINDING_MASK = 0x3;
254 const unsigned WASM_SYMBOL_VISIBILITY_MASK = 0xc;
255 
256 const unsigned WASM_SYMBOL_BINDING_GLOBAL = 0x0;
257 const unsigned WASM_SYMBOL_BINDING_WEAK = 0x1;
258 const unsigned WASM_SYMBOL_BINDING_LOCAL = 0x2;
259 const unsigned WASM_SYMBOL_VISIBILITY_DEFAULT = 0x0;
260 const unsigned WASM_SYMBOL_VISIBILITY_HIDDEN = 0x4;
261 const unsigned WASM_SYMBOL_UNDEFINED = 0x10;
262 
263 #define WASM_RELOC(name, value) name = value,
264 
265 enum : unsigned {
266 #include "WasmRelocs.def"
267 };
268 
269 #undef WASM_RELOC
270 
271 // Useful comparison operators
272 inline bool operator==(const WasmSignature &LHS, const WasmSignature &RHS) {
273  return LHS.ReturnType == RHS.ReturnType && LHS.ParamTypes == RHS.ParamTypes;
274 }
275 
276 inline bool operator!=(const WasmSignature &LHS, const WasmSignature &RHS) {
277  return !(LHS == RHS);
278 }
279 
280 inline bool operator==(const WasmGlobalType &LHS, const WasmGlobalType &RHS) {
281  return LHS.Type == RHS.Type && LHS.Mutable == RHS.Mutable;
282 }
283 
284 inline bool operator!=(const WasmGlobalType &LHS, const WasmGlobalType &RHS) {
285  return !(LHS == RHS);
286 }
287 
288 } // end namespace wasm
289 } // end namespace llvm
290 
291 #endif
std::vector< WasmInitFunc > InitFunctions
Definition: Wasm.h:163
WasmSymbolType
Definition: Wasm.h:247
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
const unsigned WASM_SYMBOL_BINDING_LOCAL
Definition: Wasm.h:258
WasmGlobalType Type
Definition: Wasm.h:75
std::vector< StringRef > Comdats
Definition: Wasm.h:164
StringRef SymbolName
Definition: Wasm.h:103
const unsigned WASM_SYMBOL_UNDEFINED
Definition: Wasm.h:261
bool operator==(const WasmSignature &LHS, const WasmSignature &RHS)
Definition: Wasm.h:272
WasmTable Table
Definition: Wasm.h:87
StringRef Module
Definition: Wasm.h:81
bool operator!=(const WasmSignature &LHS, const WasmSignature &RHS)
Definition: Wasm.h:276
const uint32_t WasmVersion
Definition: Wasm.h:26
ArrayRef< uint8_t > Content
Definition: Wasm.h:111
ArrayRef< uint8_t > Body
Definition: Wasm.h:100
uint32_t Initial
Definition: Wasm.h:48
const unsigned WASM_SYMBOL_VISIBILITY_DEFAULT
Definition: Wasm.h:259
uint32_t SigIndex
Definition: Wasm.h:85
WasmLimits Memory
Definition: Wasm.h:88
std::vector< uint32_t > Functions
Definition: Wasm.h:121
const unsigned WASM_SYMBOL_VISIBILITY_HIDDEN
Definition: Wasm.h:260
uint8_t ElemType
Definition: Wasm.h:53
const unsigned WASM_SYMBOL_BINDING_WEAK
Definition: Wasm.h:257
uint32_t CodeSectionOffset
Definition: Wasm.h:101
WasmInitExpr Offset
Definition: Wasm.h:110
const unsigned WASM_SYMBOL_VISIBILITY_MASK
Definition: Wasm.h:254
StringRef DebugName
Definition: Wasm.h:104
const char WasmMagic[]
Definition: Wasm.h:24
WasmInitExpr InitExpr
Definition: Wasm.h:76
std::vector< uint8_t > ParamTypes
Definition: Wasm.h:36
const unsigned WASM_SYMBOL_BINDING_GLOBAL
Definition: Wasm.h:256
WasmLimits Limits
Definition: Wasm.h:54
WasmGlobalType Global
Definition: Wasm.h:86
const unsigned WASM_SYMBOL_BINDING_MASK
Definition: Wasm.h:253
uint32_t Index
Definition: Wasm.h:43
WasmDataReference DataRef
Definition: Wasm.h:153
LLVM Value Representation.
Definition: Value.h:73
uint32_t Index
Definition: Wasm.h:74
StringRef Name
Definition: Wasm.h:41
const uint32_t WasmPageSize
Definition: Wasm.h:28
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
StringRef SymbolName
Definition: Wasm.h:77
std::vector< WasmSymbolInfo > SymbolTable
Definition: Wasm.h:165
std::vector< WasmLocalDecl > Locals
Definition: Wasm.h:99
WasmInitExpr Offset
Definition: Wasm.h:120
uint32_t Maximum
Definition: Wasm.h:49
StringRef Field
Definition: Wasm.h:82