summaryrefslogtreecommitdiffstats
path: root/llvm/include/llvm/Support/Wasm.h
blob: 553ed721870bdba563fa4cf25250376fe5cebc22 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
//===- Wasm.h - Wasm object file format -------------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file defines manifest constants for the wasm object file format.
// See: https://github.com/WebAssembly/design/blob/master/BinaryEncoding.md
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_SUPPORT_WASM_H
#define LLVM_SUPPORT_WASM_H

#include "llvm/ADT/ArrayRef.h"

namespace llvm {
namespace wasm {

// Object file magic string.
const char WasmMagic[] = {'\0', 'a', 's', 'm'};
// Wasm binary format version
const uint32_t WasmVersion = 0x1;

struct WasmObjectHeader {
  StringRef Magic;
  uint32_t Version;
};

struct WasmSection {
  uint32_t Type;             // Section type (See below)
  uint32_t Offset;           // Offset with in the file
  StringRef Name;            // Section name (User-defined sections only)
  ArrayRef<uint8_t> Content; // Section content
};

enum : unsigned {
  WASM_SEC_CUSTOM = 0,   // Custom / User-defined section
  WASM_SEC_TYPE = 1,     // Function signature declarations
  WASM_SEC_IMPORT = 2,   // Import declarations
  WASM_SEC_FUNCTION = 3, // Function declarations
  WASM_SEC_TABLE = 4,    // Indirect function table and other tables
  WASM_SEC_MEMORY = 5,   // Memory attributes
  WASM_SEC_GLOBAL = 6,   // Global declarations
  WASM_SEC_EXPORT = 7,   // Exports
  WASM_SEC_START = 8,    // Start function declaration
  WASM_SEC_ELEM = 9,     // Elements section
  WASM_SEC_CODE = 10,    // Function bodies (code)
  WASM_SEC_DATA = 11     // Data segments
};

// Type immediate encodings used in various contexts.
enum {
  WASM_TYPE_I32          = -0x01,
  WASM_TYPE_I64          = -0x02,
  WASM_TYPE_F32          = -0x03,
  WASM_TYPE_F64          = -0x04,
  WASM_TYPE_ANYFUNC      = -0x10,
  WASM_TYPE_FUNC         = -0x20,
  WASM_TYPE_NORESULT     = -0x40, // for blocks with no result values
};

// Kinds of externals (for imports and exports).
enum : unsigned {
  WASM_EXTERNAL_FUNCTION = 0x0,
  WASM_EXTERNAL_TABLE    = 0x1,
  WASM_EXTERNAL_MEMORY   = 0x2,
  WASM_EXTERNAL_GLOBAL   = 0x3,
};

// Opcodes used in initializer expressions.
enum : unsigned {
  WASM_OPCODE_END        = 0x0b,
  WASM_OPCODE_GET_GLOBAL = 0x23,
  WASM_OPCODE_I32_CONST  = 0x41,
  WASM_OPCODE_I64_CONST  = 0x42,
  WASM_OPCODE_F32_CONST  = 0x43,
  WASM_OPCODE_F64_CONST  = 0x44,
};

enum : unsigned {
  WASM_NAMES_FUNCTION    = 0x1,
  WASM_NAMES_LOCAL       = 0x2,
};

// Subset of types that a value can have
enum class ValType {
  I32 = WASM_TYPE_I32,
  I64 = WASM_TYPE_I64,
  F32 = WASM_TYPE_F32,
  F64 = WASM_TYPE_F64,
};

#define WASM_RELOC(name, value) name = value,

enum : unsigned {
#include "WasmRelocs/WebAssembly.def"
};

} // end namespace wasm
} // end namespace llvm

#endif
OpenPOWER on IntegriCloud