Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 1 | //===- Wasm.h - Wasm object file format -------------------------*- C++ -*-===// |
| 2 | // |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file defines manifest constants for the wasm object file format. |
| 10 | // See: https://github.com/WebAssembly/design/blob/master/BinaryEncoding.md |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #ifndef LLVM_BINARYFORMAT_WASM_H |
| 15 | #define LLVM_BINARYFORMAT_WASM_H |
| 16 | |
| 17 | #include "llvm/ADT/ArrayRef.h" |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 18 | #include "llvm/ADT/Optional.h" |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/SmallVector.h" |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 20 | #include "llvm/ADT/StringRef.h" |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 21 | |
| 22 | namespace llvm { |
| 23 | namespace wasm { |
| 24 | |
| 25 | // Object file magic string. |
| 26 | const char WasmMagic[] = {'\0', 'a', 's', 'm'}; |
| 27 | // Wasm binary format version |
| 28 | const uint32_t WasmVersion = 0x1; |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 29 | // Wasm linking metadata version |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 30 | const uint32_t WasmMetadataVersion = 0x2; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 31 | // Wasm uses a 64k page size |
| 32 | const uint32_t WasmPageSize = 65536; |
| 33 | |
| 34 | struct WasmObjectHeader { |
| 35 | StringRef Magic; |
| 36 | uint32_t Version; |
| 37 | }; |
| 38 | |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 39 | struct WasmDylinkInfo { |
| 40 | uint32_t MemorySize; // Memory size in bytes |
| 41 | uint32_t MemoryAlignment; // P2 alignment of memory |
| 42 | uint32_t TableSize; // Table size in elements |
| 43 | uint32_t TableAlignment; // P2 alignment of table |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 44 | std::vector<StringRef> Needed; // Shared library dependencies |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 45 | }; |
| 46 | |
| 47 | struct WasmProducerInfo { |
| 48 | std::vector<std::pair<std::string, std::string>> Languages; |
| 49 | std::vector<std::pair<std::string, std::string>> Tools; |
| 50 | std::vector<std::pair<std::string, std::string>> SDKs; |
| 51 | }; |
| 52 | |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 53 | struct WasmFeatureEntry { |
| 54 | uint8_t Prefix; |
| 55 | std::string Name; |
| 56 | }; |
| 57 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 58 | struct WasmExport { |
| 59 | StringRef Name; |
| 60 | uint8_t Kind; |
| 61 | uint32_t Index; |
| 62 | }; |
| 63 | |
| 64 | struct WasmLimits { |
| 65 | uint8_t Flags; |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 66 | uint64_t Initial; |
| 67 | uint64_t Maximum; |
| 68 | }; |
| 69 | |
| 70 | struct WasmTableType { |
| 71 | uint8_t ElemType; |
| 72 | WasmLimits Limits; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 73 | }; |
| 74 | |
| 75 | struct WasmTable { |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 76 | uint32_t Index; |
| 77 | WasmTableType Type; |
| 78 | StringRef SymbolName; // from the "linking" section |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 79 | }; |
| 80 | |
| 81 | struct WasmInitExpr { |
| 82 | uint8_t Opcode; |
| 83 | union { |
| 84 | int32_t Int32; |
| 85 | int64_t Int64; |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 86 | uint32_t Float32; |
| 87 | uint64_t Float64; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 88 | uint32_t Global; |
| 89 | } Value; |
| 90 | }; |
| 91 | |
| 92 | struct WasmGlobalType { |
| 93 | uint8_t Type; |
| 94 | bool Mutable; |
| 95 | }; |
| 96 | |
| 97 | struct WasmGlobal { |
| 98 | uint32_t Index; |
| 99 | WasmGlobalType Type; |
| 100 | WasmInitExpr InitExpr; |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 101 | StringRef SymbolName; // from the "linking" section |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 102 | }; |
| 103 | |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 104 | struct WasmEventType { |
| 105 | // Kind of event. Currently only WASM_EVENT_ATTRIBUTE_EXCEPTION is possible. |
| 106 | uint32_t Attribute; |
| 107 | uint32_t SigIndex; |
| 108 | }; |
| 109 | |
| 110 | struct WasmEvent { |
| 111 | uint32_t Index; |
| 112 | WasmEventType Type; |
| 113 | StringRef SymbolName; // from the "linking" section |
| 114 | }; |
| 115 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 116 | struct WasmImport { |
| 117 | StringRef Module; |
| 118 | StringRef Field; |
| 119 | uint8_t Kind; |
| 120 | union { |
| 121 | uint32_t SigIndex; |
| 122 | WasmGlobalType Global; |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 123 | WasmTableType Table; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 124 | WasmLimits Memory; |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 125 | WasmEventType Event; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 126 | }; |
| 127 | }; |
| 128 | |
| 129 | struct WasmLocalDecl { |
| 130 | uint8_t Type; |
| 131 | uint32_t Count; |
| 132 | }; |
| 133 | |
| 134 | struct WasmFunction { |
| 135 | uint32_t Index; |
| 136 | std::vector<WasmLocalDecl> Locals; |
| 137 | ArrayRef<uint8_t> Body; |
| 138 | uint32_t CodeSectionOffset; |
| 139 | uint32_t Size; |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 140 | uint32_t CodeOffset; // start of Locals and Body |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 141 | Optional<StringRef> ExportName; // from the "export" section |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 142 | StringRef SymbolName; // from the "linking" section |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 143 | StringRef DebugName; // from the "name" section |
| 144 | uint32_t Comdat; // from the "comdat info" section |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 145 | }; |
| 146 | |
| 147 | struct WasmDataSegment { |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 148 | uint32_t InitFlags; |
| 149 | uint32_t MemoryIndex; // present if InitFlags & WASM_SEGMENT_HAS_MEMINDEX |
| 150 | WasmInitExpr Offset; // present if InitFlags & WASM_SEGMENT_IS_PASSIVE == 0 |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 151 | ArrayRef<uint8_t> Content; |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 152 | StringRef Name; // from the "segment info" section |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 153 | uint32_t Alignment; |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 154 | uint32_t LinkerFlags; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 155 | uint32_t Comdat; // from the "comdat info" section |
| 156 | }; |
| 157 | |
| 158 | struct WasmElemSegment { |
| 159 | uint32_t TableIndex; |
| 160 | WasmInitExpr Offset; |
| 161 | std::vector<uint32_t> Functions; |
| 162 | }; |
| 163 | |
| 164 | // Represents the location of a Wasm data symbol within a WasmDataSegment, as |
| 165 | // the index of the segment, and the offset and size within the segment. |
| 166 | struct WasmDataReference { |
| 167 | uint32_t Segment; |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 168 | uint64_t Offset; |
| 169 | uint64_t Size; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 170 | }; |
| 171 | |
| 172 | struct WasmRelocation { |
| 173 | uint8_t Type; // The type of the relocation. |
| 174 | uint32_t Index; // Index into either symbol or type index space. |
| 175 | uint64_t Offset; // Offset from the start of the section. |
| 176 | int64_t Addend; // A value to add to the symbol. |
| 177 | }; |
| 178 | |
| 179 | struct WasmInitFunc { |
| 180 | uint32_t Priority; |
| 181 | uint32_t Symbol; |
| 182 | }; |
| 183 | |
| 184 | struct WasmSymbolInfo { |
| 185 | StringRef Name; |
| 186 | uint8_t Kind; |
| 187 | uint32_t Flags; |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 188 | // For undefined symbols the module of the import |
| 189 | Optional<StringRef> ImportModule; |
| 190 | // For undefined symbols the name of the import |
| 191 | Optional<StringRef> ImportName; |
| 192 | // For symbols to be exported from the final module |
| 193 | Optional<StringRef> ExportName; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 194 | union { |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 195 | // For function, table, or global symbols, the index in function, table, or |
| 196 | // global index space. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 197 | uint32_t ElementIndex; |
| 198 | // For a data symbols, the address of the data relative to segment. |
| 199 | WasmDataReference DataRef; |
| 200 | }; |
| 201 | }; |
| 202 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 203 | enum class NameType { |
| 204 | FUNCTION, |
| 205 | GLOBAL, |
| 206 | DATA_SEGMENT, |
| 207 | }; |
| 208 | |
| 209 | struct WasmDebugName { |
| 210 | NameType Type; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 211 | uint32_t Index; |
| 212 | StringRef Name; |
| 213 | }; |
| 214 | |
| 215 | struct WasmLinkingData { |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 216 | uint32_t Version; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 217 | std::vector<WasmInitFunc> InitFunctions; |
| 218 | std::vector<StringRef> Comdats; |
| 219 | std::vector<WasmSymbolInfo> SymbolTable; |
| 220 | }; |
| 221 | |
| 222 | enum : unsigned { |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 223 | WASM_SEC_CUSTOM = 0, // Custom / User-defined section |
| 224 | WASM_SEC_TYPE = 1, // Function signature declarations |
| 225 | WASM_SEC_IMPORT = 2, // Import declarations |
| 226 | WASM_SEC_FUNCTION = 3, // Function declarations |
| 227 | WASM_SEC_TABLE = 4, // Indirect function table and other tables |
| 228 | WASM_SEC_MEMORY = 5, // Memory attributes |
| 229 | WASM_SEC_GLOBAL = 6, // Global declarations |
| 230 | WASM_SEC_EXPORT = 7, // Exports |
| 231 | WASM_SEC_START = 8, // Start function declaration |
| 232 | WASM_SEC_ELEM = 9, // Elements section |
| 233 | WASM_SEC_CODE = 10, // Function bodies (code) |
| 234 | WASM_SEC_DATA = 11, // Data segments |
| 235 | WASM_SEC_DATACOUNT = 12, // Data segment count |
| 236 | WASM_SEC_EVENT = 13 // Event declarations |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 237 | }; |
| 238 | |
| 239 | // Type immediate encodings used in various contexts. |
| 240 | enum : unsigned { |
| 241 | WASM_TYPE_I32 = 0x7F, |
| 242 | WASM_TYPE_I64 = 0x7E, |
| 243 | WASM_TYPE_F32 = 0x7D, |
| 244 | WASM_TYPE_F64 = 0x7C, |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 245 | WASM_TYPE_V128 = 0x7B, |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 246 | WASM_TYPE_FUNCREF = 0x70, |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 247 | WASM_TYPE_EXTERNREF = 0x6F, |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 248 | WASM_TYPE_FUNC = 0x60, |
| 249 | WASM_TYPE_NORESULT = 0x40, // for blocks with no result values |
| 250 | }; |
| 251 | |
| 252 | // Kinds of externals (for imports and exports). |
| 253 | enum : unsigned { |
| 254 | WASM_EXTERNAL_FUNCTION = 0x0, |
| 255 | WASM_EXTERNAL_TABLE = 0x1, |
| 256 | WASM_EXTERNAL_MEMORY = 0x2, |
| 257 | WASM_EXTERNAL_GLOBAL = 0x3, |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 258 | WASM_EXTERNAL_EVENT = 0x4, |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 259 | }; |
| 260 | |
| 261 | // Opcodes used in initializer expressions. |
| 262 | enum : unsigned { |
| 263 | WASM_OPCODE_END = 0x0b, |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 264 | WASM_OPCODE_CALL = 0x10, |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 265 | WASM_OPCODE_LOCAL_GET = 0x20, |
| 266 | WASM_OPCODE_LOCAL_SET = 0x21, |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 267 | WASM_OPCODE_GLOBAL_GET = 0x23, |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 268 | WASM_OPCODE_GLOBAL_SET = 0x24, |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 269 | WASM_OPCODE_I32_STORE = 0x36, |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 270 | WASM_OPCODE_I64_STORE = 0x37, |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 271 | WASM_OPCODE_I32_CONST = 0x41, |
| 272 | WASM_OPCODE_I64_CONST = 0x42, |
| 273 | WASM_OPCODE_F32_CONST = 0x43, |
| 274 | WASM_OPCODE_F64_CONST = 0x44, |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 275 | WASM_OPCODE_I32_ADD = 0x6a, |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 276 | WASM_OPCODE_I64_ADD = 0x7c, |
| 277 | WASM_OPCODE_REF_NULL = 0xd0, |
| 278 | }; |
| 279 | |
| 280 | // Opcodes used in synthetic functions. |
| 281 | enum : unsigned { |
| 282 | WASM_OPCODE_IF = 0x04, |
| 283 | WASM_OPCODE_ELSE = 0x05, |
| 284 | WASM_OPCODE_DROP = 0x1a, |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 285 | WASM_OPCODE_MISC_PREFIX = 0xfc, |
| 286 | WASM_OPCODE_MEMORY_INIT = 0x08, |
| 287 | WASM_OPCODE_DATA_DROP = 0x09, |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 288 | WASM_OPCODE_ATOMICS_PREFIX = 0xfe, |
| 289 | WASM_OPCODE_ATOMIC_NOTIFY = 0x00, |
| 290 | WASM_OPCODE_I32_ATOMIC_WAIT = 0x01, |
| 291 | WASM_OPCODE_I32_ATOMIC_STORE = 0x17, |
| 292 | WASM_OPCODE_I32_RMW_CMPXCHG = 0x48, |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 293 | }; |
| 294 | |
| 295 | enum : unsigned { |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 296 | WASM_LIMITS_FLAG_NONE = 0x0, |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 297 | WASM_LIMITS_FLAG_HAS_MAX = 0x1, |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 298 | WASM_LIMITS_FLAG_IS_SHARED = 0x2, |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 299 | WASM_LIMITS_FLAG_IS_64 = 0x4, |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 300 | }; |
| 301 | |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 302 | enum : unsigned { |
| 303 | WASM_SEGMENT_IS_PASSIVE = 0x01, |
| 304 | WASM_SEGMENT_HAS_MEMINDEX = 0x02, |
| 305 | }; |
| 306 | |
| 307 | // Feature policy prefixes used in the custom "target_features" section |
| 308 | enum : uint8_t { |
| 309 | WASM_FEATURE_PREFIX_USED = '+', |
| 310 | WASM_FEATURE_PREFIX_REQUIRED = '=', |
| 311 | WASM_FEATURE_PREFIX_DISALLOWED = '-', |
| 312 | }; |
| 313 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 314 | // Kind codes used in the custom "name" section |
| 315 | enum : unsigned { |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 316 | WASM_NAMES_FUNCTION = 1, |
| 317 | WASM_NAMES_LOCAL = 2, |
| 318 | WASM_NAMES_GLOBAL = 7, |
| 319 | WASM_NAMES_DATA_SEGMENT = 9, |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 320 | }; |
| 321 | |
| 322 | // Kind codes used in the custom "linking" section |
| 323 | enum : unsigned { |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 324 | WASM_SEGMENT_INFO = 0x5, |
| 325 | WASM_INIT_FUNCS = 0x6, |
| 326 | WASM_COMDAT_INFO = 0x7, |
| 327 | WASM_SYMBOL_TABLE = 0x8, |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 328 | }; |
| 329 | |
| 330 | // Kind codes used in the custom "linking" section in the WASM_COMDAT_INFO |
| 331 | enum : unsigned { |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 332 | WASM_COMDAT_DATA = 0x0, |
| 333 | WASM_COMDAT_FUNCTION = 0x1, |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 334 | // GLOBAL, EVENT, and TABLE are in here but LLVM doesn't use them yet. |
| 335 | WASM_COMDAT_SECTION = 0x5, |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 336 | }; |
| 337 | |
| 338 | // Kind codes used in the custom "linking" section in the WASM_SYMBOL_TABLE |
| 339 | enum WasmSymbolType : unsigned { |
| 340 | WASM_SYMBOL_TYPE_FUNCTION = 0x0, |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 341 | WASM_SYMBOL_TYPE_DATA = 0x1, |
| 342 | WASM_SYMBOL_TYPE_GLOBAL = 0x2, |
| 343 | WASM_SYMBOL_TYPE_SECTION = 0x3, |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 344 | WASM_SYMBOL_TYPE_EVENT = 0x4, |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 345 | WASM_SYMBOL_TYPE_TABLE = 0x5, |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 346 | }; |
| 347 | |
| 348 | // Kinds of event attributes. |
| 349 | enum WasmEventAttribute : unsigned { |
| 350 | WASM_EVENT_ATTRIBUTE_EXCEPTION = 0x0, |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 351 | }; |
| 352 | |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 353 | const unsigned WASM_SYMBOL_BINDING_MASK = 0x3; |
| 354 | const unsigned WASM_SYMBOL_VISIBILITY_MASK = 0xc; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 355 | |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 356 | const unsigned WASM_SYMBOL_BINDING_GLOBAL = 0x0; |
| 357 | const unsigned WASM_SYMBOL_BINDING_WEAK = 0x1; |
| 358 | const unsigned WASM_SYMBOL_BINDING_LOCAL = 0x2; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 359 | const unsigned WASM_SYMBOL_VISIBILITY_DEFAULT = 0x0; |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 360 | const unsigned WASM_SYMBOL_VISIBILITY_HIDDEN = 0x4; |
| 361 | const unsigned WASM_SYMBOL_UNDEFINED = 0x10; |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 362 | const unsigned WASM_SYMBOL_EXPORTED = 0x20; |
| 363 | const unsigned WASM_SYMBOL_EXPLICIT_NAME = 0x40; |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 364 | const unsigned WASM_SYMBOL_NO_STRIP = 0x80; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 365 | |
| 366 | #define WASM_RELOC(name, value) name = value, |
| 367 | |
| 368 | enum : unsigned { |
| 369 | #include "WasmRelocs.def" |
| 370 | }; |
| 371 | |
| 372 | #undef WASM_RELOC |
| 373 | |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 374 | // Subset of types that a value can have |
| 375 | enum class ValType { |
| 376 | I32 = WASM_TYPE_I32, |
| 377 | I64 = WASM_TYPE_I64, |
| 378 | F32 = WASM_TYPE_F32, |
| 379 | F64 = WASM_TYPE_F64, |
| 380 | V128 = WASM_TYPE_V128, |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 381 | FUNCREF = WASM_TYPE_FUNCREF, |
| 382 | EXTERNREF = WASM_TYPE_EXTERNREF, |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 383 | }; |
| 384 | |
| 385 | struct WasmSignature { |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 386 | SmallVector<ValType, 1> Returns; |
| 387 | SmallVector<ValType, 4> Params; |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 388 | // Support empty and tombstone instances, needed by DenseMap. |
| 389 | enum { Plain, Empty, Tombstone } State = Plain; |
| 390 | |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 391 | WasmSignature(SmallVector<ValType, 1> &&InReturns, |
| 392 | SmallVector<ValType, 4> &&InParams) |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 393 | : Returns(InReturns), Params(InParams) {} |
| 394 | WasmSignature() = default; |
| 395 | }; |
| 396 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 397 | // Useful comparison operators |
| 398 | inline bool operator==(const WasmSignature &LHS, const WasmSignature &RHS) { |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 399 | return LHS.State == RHS.State && LHS.Returns == RHS.Returns && |
| 400 | LHS.Params == RHS.Params; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | inline bool operator!=(const WasmSignature &LHS, const WasmSignature &RHS) { |
| 404 | return !(LHS == RHS); |
| 405 | } |
| 406 | |
| 407 | inline bool operator==(const WasmGlobalType &LHS, const WasmGlobalType &RHS) { |
| 408 | return LHS.Type == RHS.Type && LHS.Mutable == RHS.Mutable; |
| 409 | } |
| 410 | |
| 411 | inline bool operator!=(const WasmGlobalType &LHS, const WasmGlobalType &RHS) { |
| 412 | return !(LHS == RHS); |
| 413 | } |
| 414 | |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 415 | std::string toString(WasmSymbolType type); |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 416 | std::string relocTypetoString(uint32_t type); |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 417 | bool relocTypeHasAddend(uint32_t type); |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 418 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 419 | } // end namespace wasm |
| 420 | } // end namespace llvm |
| 421 | |
| 422 | #endif |