LLVM  5.0.0
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<int32_t> ParamTypes;
37  int32_t ReturnType;
38 };
39 
40 struct WasmExport {
44 };
45 
46 struct WasmLimits {
50 };
51 
52 struct WasmTable {
53  int32_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 
68 struct WasmGlobal {
69  int32_t Type;
70  bool Mutable;
72 };
73 
74 struct WasmImport {
78  union {
83  };
84 };
85 
86 struct WasmLocalDecl {
87  int32_t Type;
89 };
90 
91 struct WasmFunction {
92  std::vector<WasmLocalDecl> Locals;
94 };
95 
100 };
101 
105  std::vector<uint32_t> Functions;
106 };
107 
109  uint32_t Type; // The type of the relocation.
110  uint32_t Index; // Index into function to global index space.
111  uint64_t Offset; // Offset from the start of the section.
112  int64_t Addend; // A value to add to the symbol.
113 };
114 
118 };
119 
120 enum : unsigned {
121  WASM_SEC_CUSTOM = 0, // Custom / User-defined section
122  WASM_SEC_TYPE = 1, // Function signature declarations
123  WASM_SEC_IMPORT = 2, // Import declarations
124  WASM_SEC_FUNCTION = 3, // Function declarations
125  WASM_SEC_TABLE = 4, // Indirect function table and other tables
126  WASM_SEC_MEMORY = 5, // Memory attributes
127  WASM_SEC_GLOBAL = 6, // Global declarations
128  WASM_SEC_EXPORT = 7, // Exports
129  WASM_SEC_START = 8, // Start function declaration
130  WASM_SEC_ELEM = 9, // Elements section
131  WASM_SEC_CODE = 10, // Function bodies (code)
132  WASM_SEC_DATA = 11 // Data segments
133 };
134 
135 // Type immediate encodings used in various contexts.
136 enum {
137  WASM_TYPE_I32 = -0x01,
138  WASM_TYPE_I64 = -0x02,
139  WASM_TYPE_F32 = -0x03,
140  WASM_TYPE_F64 = -0x04,
142  WASM_TYPE_FUNC = -0x20,
143  WASM_TYPE_NORESULT = -0x40, // for blocks with no result values
144 };
145 
146 // Kinds of externals (for imports and exports).
147 enum : unsigned {
152 };
153 
154 // Opcodes used in initializer expressions.
155 enum : unsigned {
162 };
163 
164 enum : unsigned {
167 };
168 
169 enum : unsigned {
171 };
172 
173 // Subset of types that a value can have
174 enum class ValType {
175  I32 = WASM_TYPE_I32,
176  I64 = WASM_TYPE_I64,
177  F32 = WASM_TYPE_F32,
178  F64 = WASM_TYPE_F64,
179 };
180 
181 // Linking metadata kinds.
182 enum : unsigned {
187 };
188 
189 enum : unsigned {
191 };
192 
193 #define WASM_RELOC(name, value) name = value,
194 
195 enum : unsigned {
196 #include "WasmRelocs/WebAssembly.def"
197 };
198 
199 #undef WASM_RELOC
200 
201 struct Global {
203  bool Mutable;
204 
205  // The initial value for this global is either the value of an imported
206  // global, in which case InitialModule and InitialName specify the global
207  // import, or a value, in which case InitialModule is empty and InitialValue
208  // holds the value.
211  uint64_t InitialValue;
212 };
213 
214 } // end namespace wasm
215 } // end namespace llvm
216 
217 #endif
ValType Type
Definition: Wasm.h:202
StringRef InitialModule
Definition: Wasm.h:209
WasmTable Table
Definition: Wasm.h:81
StringRef Module
Definition: Wasm.h:75
const uint32_t WasmVersion
Definition: Wasm.h:26
ArrayRef< uint8_t > Content
Definition: Wasm.h:99
ArrayRef< uint8_t > Body
Definition: Wasm.h:93
uint32_t Initial
Definition: Wasm.h:48
uint32_t SigIndex
Definition: Wasm.h:79
WasmLimits Memory
Definition: Wasm.h:82
std::vector< uint32_t > Functions
Definition: Wasm.h:105
uint32_t Kind
Definition: Wasm.h:77
uint32_t Flags
Definition: Wasm.h:47
WasmInitExpr Offset
Definition: Wasm.h:98
union llvm::wasm::WasmInitExpr::@125 Value
uint32_t Kind
Definition: Wasm.h:42
const char WasmMagic[]
Definition: Wasm.h:24
WasmInitExpr InitExpr
Definition: Wasm.h:71
WasmGlobal Global
Definition: Wasm.h:80
uint64_t InitialValue
Definition: Wasm.h:211
WasmLimits Limits
Definition: Wasm.h:54
int32_t ElemType
Definition: Wasm.h:53
StringRef InitialName
Definition: Wasm.h:210
uint32_t Index
Definition: Wasm.h:43
std::vector< int32_t > ParamTypes
Definition: Wasm.h:36
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
std::vector< WasmLocalDecl > Locals
Definition: Wasm.h:92
WasmInitExpr Offset
Definition: Wasm.h:104
uint32_t Maximum
Definition: Wasm.h:49
StringRef Field
Definition: Wasm.h:76