Coverage Report

Created: 2018-11-16 02:38

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