]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/include/llvm/BinaryFormat/Wasm.h
Merge ^/head r343807 through r343955.
[FreeBSD/FreeBSD.git] / contrib / llvm / include / llvm / BinaryFormat / Wasm.h
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
29 const uint32_t WasmMetadataVersion = 0x2;
30 // Wasm uses a 64k page size
31 const uint32_t WasmPageSize = 65536;
32
33 struct WasmObjectHeader {
34   StringRef Magic;
35   uint32_t Version;
36 };
37
38 struct WasmDylinkInfo {
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 {
47   StringRef Name;
48   uint8_t Kind;
49   uint32_t Index;
50 };
51
52 struct WasmLimits {
53   uint8_t Flags;
54   uint32_t Initial;
55   uint32_t Maximum;
56 };
57
58 struct WasmTable {
59   uint8_t ElemType;
60   WasmLimits Limits;
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;
70     uint32_t Global;
71   } Value;
72 };
73
74 struct WasmGlobalType {
75   uint8_t Type;
76   bool Mutable;
77 };
78
79 struct WasmGlobal {
80   uint32_t Index;
81   WasmGlobalType Type;
82   WasmInitExpr InitExpr;
83   StringRef SymbolName; // from the "linking" section
84 };
85
86 struct WasmEventType {
87   // Kind of event. Currently only WASM_EVENT_ATTRIBUTE_EXCEPTION is possible.
88   uint32_t Attribute;
89   uint32_t SigIndex;
90 };
91
92 struct WasmEvent {
93   uint32_t Index;
94   WasmEventType Type;
95   StringRef SymbolName; // from the "linking" section
96 };
97
98 struct WasmImport {
99   StringRef Module;
100   StringRef Field;
101   uint8_t Kind;
102   union {
103     uint32_t SigIndex;
104     WasmGlobalType Global;
105     WasmTable Table;
106     WasmLimits Memory;
107     WasmEventType Event;
108   };
109 };
110
111 struct WasmLocalDecl {
112   uint8_t Type;
113   uint32_t Count;
114 };
115
116 struct WasmFunction {
117   uint32_t Index;
118   std::vector<WasmLocalDecl> Locals;
119   ArrayRef<uint8_t> Body;
120   uint32_t CodeSectionOffset;
121   uint32_t Size;
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
128 struct WasmDataSegment {
129   uint32_t MemoryIndex;
130   WasmInitExpr Offset;
131   ArrayRef<uint8_t> Content;
132   StringRef Name; // from the "segment info" section
133   uint32_t Alignment;
134   uint32_t Flags;
135   uint32_t Comdat; // from the "comdat info" section
136 };
137
138 struct WasmElemSegment {
139   uint32_t TableIndex;
140   WasmInitExpr Offset;
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.
146 struct WasmDataReference {
147   uint32_t Segment;
148   uint32_t Offset;
149   uint32_t Size;
150 };
151
152 struct WasmRelocation {
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 {
160   uint32_t Priority;
161   uint32_t Symbol;
162 };
163
164 struct WasmSymbolInfo {
165   StringRef Name;
166   uint8_t Kind;
167   uint32_t Flags;
168   StringRef Module; // For undefined symbols the module name of the import
169   union {
170     // For function or global symbols, the index in function or global index
171     // space.
172     uint32_t ElementIndex;
173     // For a data symbols, the address of the data relative to segment.
174     WasmDataReference DataRef;
175   };
176 };
177
178 struct WasmFunctionName {
179   uint32_t Index;
180   StringRef Name;
181 };
182
183 struct WasmLinkingData {
184   uint32_t Version;
185   std::vector<WasmInitFunc> InitFunctions;
186   std::vector<StringRef> Comdats;
187   std::vector<WasmSymbolInfo> SymbolTable;
188 };
189
190 enum : unsigned {
191   WASM_SEC_CUSTOM = 0,     // Custom / User-defined section
192   WASM_SEC_TYPE = 1,       // Function signature declarations
193   WASM_SEC_IMPORT = 2,     // Import declarations
194   WASM_SEC_FUNCTION = 3,   // Function declarations
195   WASM_SEC_TABLE = 4,      // Indirect function table and other tables
196   WASM_SEC_MEMORY = 5,     // Memory attributes
197   WASM_SEC_GLOBAL = 6,     // Global declarations
198   WASM_SEC_EXPORT = 7,     // Exports
199   WASM_SEC_START = 8,      // Start function declaration
200   WASM_SEC_ELEM = 9,       // Elements section
201   WASM_SEC_CODE = 10,      // Function bodies (code)
202   WASM_SEC_DATA = 11,      // Data segments
203   WASM_SEC_DATACOUNT = 12, // Data segment count
204   WASM_SEC_EVENT = 13      // Event declarations
205 };
206
207 // Type immediate encodings used in various contexts.
208 enum : unsigned {
209   WASM_TYPE_I32 = 0x7F,
210   WASM_TYPE_I64 = 0x7E,
211   WASM_TYPE_F32 = 0x7D,
212   WASM_TYPE_F64 = 0x7C,
213   WASM_TYPE_V128 = 0x7B,
214   WASM_TYPE_FUNCREF = 0x70,
215   WASM_TYPE_EXCEPT_REF = 0x68,
216   WASM_TYPE_FUNC = 0x60,
217   WASM_TYPE_NORESULT = 0x40, // for blocks with no result values
218 };
219
220 // Kinds of externals (for imports and exports).
221 enum : unsigned {
222   WASM_EXTERNAL_FUNCTION = 0x0,
223   WASM_EXTERNAL_TABLE = 0x1,
224   WASM_EXTERNAL_MEMORY = 0x2,
225   WASM_EXTERNAL_GLOBAL = 0x3,
226   WASM_EXTERNAL_EVENT = 0x4,
227 };
228
229 // Opcodes used in initializer expressions.
230 enum : unsigned {
231   WASM_OPCODE_END = 0x0b,
232   WASM_OPCODE_GLOBAL_GET = 0x23,
233   WASM_OPCODE_I32_CONST = 0x41,
234   WASM_OPCODE_I64_CONST = 0x42,
235   WASM_OPCODE_F32_CONST = 0x43,
236   WASM_OPCODE_F64_CONST = 0x44,
237 };
238
239 enum : unsigned {
240   WASM_LIMITS_FLAG_HAS_MAX = 0x1,
241   WASM_LIMITS_FLAG_IS_SHARED = 0x2,
242 };
243
244 // Kind codes used in the custom "name" section
245 enum : unsigned {
246   WASM_NAMES_FUNCTION = 0x1,
247   WASM_NAMES_LOCAL = 0x2,
248 };
249
250 // Kind codes used in the custom "linking" section
251 enum : unsigned {
252   WASM_SEGMENT_INFO = 0x5,
253   WASM_INIT_FUNCS = 0x6,
254   WASM_COMDAT_INFO = 0x7,
255   WASM_SYMBOL_TABLE = 0x8,
256 };
257
258 // Kind codes used in the custom "linking" section in the WASM_COMDAT_INFO
259 enum : unsigned {
260   WASM_COMDAT_DATA = 0x0,
261   WASM_COMDAT_FUNCTION = 0x1,
262 };
263
264 // Kind codes used in the custom "linking" section in the WASM_SYMBOL_TABLE
265 enum WasmSymbolType : unsigned {
266   WASM_SYMBOL_TYPE_FUNCTION = 0x0,
267   WASM_SYMBOL_TYPE_DATA = 0x1,
268   WASM_SYMBOL_TYPE_GLOBAL = 0x2,
269   WASM_SYMBOL_TYPE_SECTION = 0x3,
270   WASM_SYMBOL_TYPE_EVENT = 0x4,
271 };
272
273 // Kinds of event attributes.
274 enum WasmEventAttribute : unsigned {
275   WASM_EVENT_ATTRIBUTE_EXCEPTION = 0x0,
276 };
277
278 const unsigned WASM_SYMBOL_BINDING_MASK = 0x3;
279 const unsigned WASM_SYMBOL_VISIBILITY_MASK = 0xc;
280
281 const unsigned WASM_SYMBOL_BINDING_GLOBAL = 0x0;
282 const unsigned WASM_SYMBOL_BINDING_WEAK = 0x1;
283 const unsigned WASM_SYMBOL_BINDING_LOCAL = 0x2;
284 const unsigned WASM_SYMBOL_VISIBILITY_DEFAULT = 0x0;
285 const unsigned WASM_SYMBOL_VISIBILITY_HIDDEN = 0x4;
286 const unsigned WASM_SYMBOL_UNDEFINED = 0x10;
287
288 #define WASM_RELOC(name, value) name = value,
289
290 enum : unsigned {
291 #include "WasmRelocs.def"
292 };
293
294 #undef WASM_RELOC
295
296 // Subset of types that a value can have
297 enum class ValType {
298   I32 = WASM_TYPE_I32,
299   I64 = WASM_TYPE_I64,
300   F32 = WASM_TYPE_F32,
301   F64 = WASM_TYPE_F64,
302   V128 = WASM_TYPE_V128,
303   EXCEPT_REF = WASM_TYPE_EXCEPT_REF,
304 };
305
306 struct WasmSignature {
307   SmallVector<wasm::ValType, 1> Returns;
308   SmallVector<wasm::ValType, 4> Params;
309   // Support empty and tombstone instances, needed by DenseMap.
310   enum { Plain, Empty, Tombstone } State = Plain;
311
312   WasmSignature(SmallVector<wasm::ValType, 1> &&InReturns,
313                 SmallVector<wasm::ValType, 4> &&InParams)
314       : Returns(InReturns), Params(InParams) {}
315   WasmSignature() = default;
316 };
317
318 // Useful comparison operators
319 inline bool operator==(const WasmSignature &LHS, const WasmSignature &RHS) {
320   return LHS.State == RHS.State && LHS.Returns == RHS.Returns &&
321          LHS.Params == RHS.Params;
322 }
323
324 inline bool operator!=(const WasmSignature &LHS, const WasmSignature &RHS) {
325   return !(LHS == RHS);
326 }
327
328 inline bool operator==(const WasmGlobalType &LHS, const WasmGlobalType &RHS) {
329   return LHS.Type == RHS.Type && LHS.Mutable == RHS.Mutable;
330 }
331
332 inline bool operator!=(const WasmGlobalType &LHS, const WasmGlobalType &RHS) {
333   return !(LHS == RHS);
334 }
335
336 std::string toString(wasm::WasmSymbolType type);
337 std::string relocTypetoString(uint32_t type);
338
339 } // end namespace wasm
340 } // end namespace llvm
341
342 #endif