1 //==- WebAssemblyMCTargetDesc.h - WebAssembly Target Descriptions -*- C++ -*-=// 2 // 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 6 // 7 //===----------------------------------------------------------------------===// 8 /// 9 /// \file 10 /// This file provides WebAssembly-specific target descriptions. 11 /// 12 //===----------------------------------------------------------------------===// 13 14 #ifndef LLVM_LIB_TARGET_WEBASSEMBLY_MCTARGETDESC_WEBASSEMBLYMCTARGETDESC_H 15 #define LLVM_LIB_TARGET_WEBASSEMBLY_MCTARGETDESC_WEBASSEMBLYMCTARGETDESC_H 16 17 #include "../WebAssemblySubtarget.h" 18 #include "llvm/BinaryFormat/Wasm.h" 19 #include "llvm/MC/MCInstrDesc.h" 20 #include "llvm/Support/DataTypes.h" 21 #include <memory> 22 23 namespace llvm { 24 25 class MCAsmBackend; 26 class MCCodeEmitter; 27 class MCInstrInfo; 28 class MCObjectTargetWriter; 29 class MVT; 30 class Triple; 31 32 MCCodeEmitter *createWebAssemblyMCCodeEmitter(const MCInstrInfo &MCII); 33 34 MCAsmBackend *createWebAssemblyAsmBackend(const Triple &TT); 35 36 std::unique_ptr<MCObjectTargetWriter> 37 createWebAssemblyWasmObjectWriter(bool Is64Bit, bool IsEmscripten); 38 39 namespace WebAssembly { 40 enum OperandType { 41 /// Basic block label in a branch construct. 42 OPERAND_BASIC_BLOCK = MCOI::OPERAND_FIRST_TARGET, 43 /// Local index. 44 OPERAND_LOCAL, 45 /// Global index. 46 OPERAND_GLOBAL, 47 /// 32-bit integer immediates. 48 OPERAND_I32IMM, 49 /// 64-bit integer immediates. 50 OPERAND_I64IMM, 51 /// 32-bit floating-point immediates. 52 OPERAND_F32IMM, 53 /// 64-bit floating-point immediates. 54 OPERAND_F64IMM, 55 /// 8-bit vector lane immediate 56 OPERAND_VEC_I8IMM, 57 /// 16-bit vector lane immediate 58 OPERAND_VEC_I16IMM, 59 /// 32-bit vector lane immediate 60 OPERAND_VEC_I32IMM, 61 /// 64-bit vector lane immediate 62 OPERAND_VEC_I64IMM, 63 /// 32-bit unsigned function indices. 64 OPERAND_FUNCTION32, 65 /// 32-bit unsigned memory offsets. 66 OPERAND_OFFSET32, 67 /// 64-bit unsigned memory offsets. 68 OPERAND_OFFSET64, 69 /// p2align immediate for load and store address alignment. 70 OPERAND_P2ALIGN, 71 /// signature immediate for block/loop. 72 OPERAND_SIGNATURE, 73 /// type signature immediate for call_indirect. 74 OPERAND_TYPEINDEX, 75 /// Event index. 76 OPERAND_EVENT, 77 /// A list of branch targets for br_list. 78 OPERAND_BRLIST, 79 }; 80 } // end namespace WebAssembly 81 82 namespace WebAssemblyII { 83 84 /// Target Operand Flag enum. 85 enum TOF { 86 MO_NO_FLAG = 0, 87 88 // On a symbol operand this indicates that the immediate is a wasm global 89 // index. The value of the wasm global will be set to the symbol address at 90 // runtime. This adds a level of indirection similar to the GOT on native 91 // platforms. 92 MO_GOT, 93 94 // On a symbol operand this indicates that the immediate is the symbol 95 // address relative the __memory_base wasm global. 96 // Only applicable to data symbols. 97 MO_MEMORY_BASE_REL, 98 99 // On a symbol operand this indicates that the immediate is the symbol 100 // address relative the __table_base wasm global. 101 // Only applicable to function symbols. 102 MO_TABLE_BASE_REL, 103 }; 104 105 } // end namespace WebAssemblyII 106 107 } // end namespace llvm 108 109 // Defines symbolic names for WebAssembly registers. This defines a mapping from 110 // register name to register number. 111 // 112 #define GET_REGINFO_ENUM 113 #include "WebAssemblyGenRegisterInfo.inc" 114 115 // Defines symbolic names for the WebAssembly instructions. 116 // 117 #define GET_INSTRINFO_ENUM 118 #include "WebAssemblyGenInstrInfo.inc" 119 120 namespace llvm { 121 namespace WebAssembly { 122 123 /// Used as immediate MachineOperands for block signatures 124 enum class BlockType : unsigned { 125 Invalid = 0x00, 126 Void = 0x40, 127 I32 = unsigned(wasm::ValType::I32), 128 I64 = unsigned(wasm::ValType::I64), 129 F32 = unsigned(wasm::ValType::F32), 130 F64 = unsigned(wasm::ValType::F64), 131 V128 = unsigned(wasm::ValType::V128), 132 Exnref = unsigned(wasm::ValType::EXNREF), 133 // Multivalue blocks (and other non-void blocks) are only emitted when the 134 // blocks will never be exited and are at the ends of functions (see 135 // WebAssemblyCFGStackify::fixEndsAtEndOfFunction). They also are never made 136 // to pop values off the stack, so the exact multivalue signature can always 137 // be inferred from the return type of the parent function in MCInstLower. 138 Multivalue = 0xffff, 139 }; 140 141 /// Instruction opcodes emitted via means other than CodeGen. 142 static const unsigned Nop = 0x01; 143 static const unsigned End = 0x0b; 144 145 wasm::ValType toValType(const MVT &Ty); 146 147 /// Return the default p2align value for a load or store with the given opcode. 148 inline unsigned GetDefaultP2AlignAny(unsigned Opc) { 149 switch (Opc) { 150 #define WASM_LOAD_STORE(NAME) \ 151 case WebAssembly::NAME##_A32: \ 152 case WebAssembly::NAME##_A64: \ 153 case WebAssembly::NAME##_A32_S: \ 154 case WebAssembly::NAME##_A64_S: 155 WASM_LOAD_STORE(LOAD8_S_I32) 156 WASM_LOAD_STORE(LOAD8_U_I32) 157 WASM_LOAD_STORE(LOAD8_S_I64) 158 WASM_LOAD_STORE(LOAD8_U_I64) 159 WASM_LOAD_STORE(ATOMIC_LOAD8_U_I32) 160 WASM_LOAD_STORE(ATOMIC_LOAD8_U_I64) 161 WASM_LOAD_STORE(STORE8_I32) 162 WASM_LOAD_STORE(STORE8_I64) 163 WASM_LOAD_STORE(ATOMIC_STORE8_I32) 164 WASM_LOAD_STORE(ATOMIC_STORE8_I64) 165 WASM_LOAD_STORE(ATOMIC_RMW8_U_ADD_I32) 166 WASM_LOAD_STORE(ATOMIC_RMW8_U_ADD_I64) 167 WASM_LOAD_STORE(ATOMIC_RMW8_U_SUB_I32) 168 WASM_LOAD_STORE(ATOMIC_RMW8_U_SUB_I64) 169 WASM_LOAD_STORE(ATOMIC_RMW8_U_AND_I32) 170 WASM_LOAD_STORE(ATOMIC_RMW8_U_AND_I64) 171 WASM_LOAD_STORE(ATOMIC_RMW8_U_OR_I32) 172 WASM_LOAD_STORE(ATOMIC_RMW8_U_OR_I64) 173 WASM_LOAD_STORE(ATOMIC_RMW8_U_XOR_I32) 174 WASM_LOAD_STORE(ATOMIC_RMW8_U_XOR_I64) 175 WASM_LOAD_STORE(ATOMIC_RMW8_U_XCHG_I32) 176 WASM_LOAD_STORE(ATOMIC_RMW8_U_XCHG_I64) 177 WASM_LOAD_STORE(ATOMIC_RMW8_U_CMPXCHG_I32) 178 WASM_LOAD_STORE(ATOMIC_RMW8_U_CMPXCHG_I64) 179 WASM_LOAD_STORE(LOAD_SPLAT_v8x16) 180 return 0; 181 WASM_LOAD_STORE(LOAD16_S_I32) 182 WASM_LOAD_STORE(LOAD16_U_I32) 183 WASM_LOAD_STORE(LOAD16_S_I64) 184 WASM_LOAD_STORE(LOAD16_U_I64) 185 WASM_LOAD_STORE(ATOMIC_LOAD16_U_I32) 186 WASM_LOAD_STORE(ATOMIC_LOAD16_U_I64) 187 WASM_LOAD_STORE(STORE16_I32) 188 WASM_LOAD_STORE(STORE16_I64) 189 WASM_LOAD_STORE(ATOMIC_STORE16_I32) 190 WASM_LOAD_STORE(ATOMIC_STORE16_I64) 191 WASM_LOAD_STORE(ATOMIC_RMW16_U_ADD_I32) 192 WASM_LOAD_STORE(ATOMIC_RMW16_U_ADD_I64) 193 WASM_LOAD_STORE(ATOMIC_RMW16_U_SUB_I32) 194 WASM_LOAD_STORE(ATOMIC_RMW16_U_SUB_I64) 195 WASM_LOAD_STORE(ATOMIC_RMW16_U_AND_I32) 196 WASM_LOAD_STORE(ATOMIC_RMW16_U_AND_I64) 197 WASM_LOAD_STORE(ATOMIC_RMW16_U_OR_I32) 198 WASM_LOAD_STORE(ATOMIC_RMW16_U_OR_I64) 199 WASM_LOAD_STORE(ATOMIC_RMW16_U_XOR_I32) 200 WASM_LOAD_STORE(ATOMIC_RMW16_U_XOR_I64) 201 WASM_LOAD_STORE(ATOMIC_RMW16_U_XCHG_I32) 202 WASM_LOAD_STORE(ATOMIC_RMW16_U_XCHG_I64) 203 WASM_LOAD_STORE(ATOMIC_RMW16_U_CMPXCHG_I32) 204 WASM_LOAD_STORE(ATOMIC_RMW16_U_CMPXCHG_I64) 205 WASM_LOAD_STORE(LOAD_SPLAT_v16x8) 206 return 1; 207 WASM_LOAD_STORE(LOAD_I32) 208 WASM_LOAD_STORE(LOAD_F32) 209 WASM_LOAD_STORE(STORE_I32) 210 WASM_LOAD_STORE(STORE_F32) 211 WASM_LOAD_STORE(LOAD32_S_I64) 212 WASM_LOAD_STORE(LOAD32_U_I64) 213 WASM_LOAD_STORE(STORE32_I64) 214 WASM_LOAD_STORE(ATOMIC_LOAD_I32) 215 WASM_LOAD_STORE(ATOMIC_LOAD32_U_I64) 216 WASM_LOAD_STORE(ATOMIC_STORE_I32) 217 WASM_LOAD_STORE(ATOMIC_STORE32_I64) 218 WASM_LOAD_STORE(ATOMIC_RMW_ADD_I32) 219 WASM_LOAD_STORE(ATOMIC_RMW32_U_ADD_I64) 220 WASM_LOAD_STORE(ATOMIC_RMW_SUB_I32) 221 WASM_LOAD_STORE(ATOMIC_RMW32_U_SUB_I64) 222 WASM_LOAD_STORE(ATOMIC_RMW_AND_I32) 223 WASM_LOAD_STORE(ATOMIC_RMW32_U_AND_I64) 224 WASM_LOAD_STORE(ATOMIC_RMW_OR_I32) 225 WASM_LOAD_STORE(ATOMIC_RMW32_U_OR_I64) 226 WASM_LOAD_STORE(ATOMIC_RMW_XOR_I32) 227 WASM_LOAD_STORE(ATOMIC_RMW32_U_XOR_I64) 228 WASM_LOAD_STORE(ATOMIC_RMW_XCHG_I32) 229 WASM_LOAD_STORE(ATOMIC_RMW32_U_XCHG_I64) 230 WASM_LOAD_STORE(ATOMIC_RMW_CMPXCHG_I32) 231 WASM_LOAD_STORE(ATOMIC_RMW32_U_CMPXCHG_I64) 232 WASM_LOAD_STORE(ATOMIC_NOTIFY) 233 WASM_LOAD_STORE(ATOMIC_WAIT_I32) 234 WASM_LOAD_STORE(LOAD_SPLAT_v32x4) 235 return 2; 236 WASM_LOAD_STORE(LOAD_I64) 237 WASM_LOAD_STORE(LOAD_F64) 238 WASM_LOAD_STORE(STORE_I64) 239 WASM_LOAD_STORE(STORE_F64) 240 WASM_LOAD_STORE(ATOMIC_LOAD_I64) 241 WASM_LOAD_STORE(ATOMIC_STORE_I64) 242 WASM_LOAD_STORE(ATOMIC_RMW_ADD_I64) 243 WASM_LOAD_STORE(ATOMIC_RMW_SUB_I64) 244 WASM_LOAD_STORE(ATOMIC_RMW_AND_I64) 245 WASM_LOAD_STORE(ATOMIC_RMW_OR_I64) 246 WASM_LOAD_STORE(ATOMIC_RMW_XOR_I64) 247 WASM_LOAD_STORE(ATOMIC_RMW_XCHG_I64) 248 WASM_LOAD_STORE(ATOMIC_RMW_CMPXCHG_I64) 249 WASM_LOAD_STORE(ATOMIC_WAIT_I64) 250 WASM_LOAD_STORE(LOAD_SPLAT_v64x2) 251 WASM_LOAD_STORE(LOAD_EXTEND_S_v8i16) 252 WASM_LOAD_STORE(LOAD_EXTEND_U_v8i16) 253 WASM_LOAD_STORE(LOAD_EXTEND_S_v4i32) 254 WASM_LOAD_STORE(LOAD_EXTEND_U_v4i32) 255 WASM_LOAD_STORE(LOAD_EXTEND_S_v2i64) 256 WASM_LOAD_STORE(LOAD_EXTEND_U_v2i64) 257 return 3; 258 WASM_LOAD_STORE(LOAD_V128) 259 WASM_LOAD_STORE(STORE_V128) 260 return 4; 261 default: 262 return -1; 263 } 264 #undef WASM_LOAD_STORE 265 } 266 267 inline unsigned GetDefaultP2Align(unsigned Opc) { 268 auto Align = GetDefaultP2AlignAny(Opc); 269 if (Align == -1U) { 270 llvm_unreachable("Only loads and stores have p2align values"); 271 } 272 return Align; 273 } 274 275 inline bool isArgument(unsigned Opc) { 276 switch (Opc) { 277 case WebAssembly::ARGUMENT_i32: 278 case WebAssembly::ARGUMENT_i32_S: 279 case WebAssembly::ARGUMENT_i64: 280 case WebAssembly::ARGUMENT_i64_S: 281 case WebAssembly::ARGUMENT_f32: 282 case WebAssembly::ARGUMENT_f32_S: 283 case WebAssembly::ARGUMENT_f64: 284 case WebAssembly::ARGUMENT_f64_S: 285 case WebAssembly::ARGUMENT_v16i8: 286 case WebAssembly::ARGUMENT_v16i8_S: 287 case WebAssembly::ARGUMENT_v8i16: 288 case WebAssembly::ARGUMENT_v8i16_S: 289 case WebAssembly::ARGUMENT_v4i32: 290 case WebAssembly::ARGUMENT_v4i32_S: 291 case WebAssembly::ARGUMENT_v2i64: 292 case WebAssembly::ARGUMENT_v2i64_S: 293 case WebAssembly::ARGUMENT_v4f32: 294 case WebAssembly::ARGUMENT_v4f32_S: 295 case WebAssembly::ARGUMENT_v2f64: 296 case WebAssembly::ARGUMENT_v2f64_S: 297 case WebAssembly::ARGUMENT_exnref: 298 case WebAssembly::ARGUMENT_exnref_S: 299 return true; 300 default: 301 return false; 302 } 303 } 304 305 inline bool isCopy(unsigned Opc) { 306 switch (Opc) { 307 case WebAssembly::COPY_I32: 308 case WebAssembly::COPY_I32_S: 309 case WebAssembly::COPY_I64: 310 case WebAssembly::COPY_I64_S: 311 case WebAssembly::COPY_F32: 312 case WebAssembly::COPY_F32_S: 313 case WebAssembly::COPY_F64: 314 case WebAssembly::COPY_F64_S: 315 case WebAssembly::COPY_V128: 316 case WebAssembly::COPY_V128_S: 317 case WebAssembly::COPY_EXNREF: 318 case WebAssembly::COPY_EXNREF_S: 319 return true; 320 default: 321 return false; 322 } 323 } 324 325 inline bool isTee(unsigned Opc) { 326 switch (Opc) { 327 case WebAssembly::TEE_I32: 328 case WebAssembly::TEE_I32_S: 329 case WebAssembly::TEE_I64: 330 case WebAssembly::TEE_I64_S: 331 case WebAssembly::TEE_F32: 332 case WebAssembly::TEE_F32_S: 333 case WebAssembly::TEE_F64: 334 case WebAssembly::TEE_F64_S: 335 case WebAssembly::TEE_V128: 336 case WebAssembly::TEE_V128_S: 337 case WebAssembly::TEE_EXNREF: 338 case WebAssembly::TEE_EXNREF_S: 339 return true; 340 default: 341 return false; 342 } 343 } 344 345 inline bool isCallDirect(unsigned Opc) { 346 switch (Opc) { 347 case WebAssembly::CALL: 348 case WebAssembly::CALL_S: 349 case WebAssembly::RET_CALL: 350 case WebAssembly::RET_CALL_S: 351 return true; 352 default: 353 return false; 354 } 355 } 356 357 inline bool isCallIndirect(unsigned Opc) { 358 switch (Opc) { 359 case WebAssembly::CALL_INDIRECT: 360 case WebAssembly::CALL_INDIRECT_S: 361 case WebAssembly::RET_CALL_INDIRECT: 362 case WebAssembly::RET_CALL_INDIRECT_S: 363 return true; 364 default: 365 return false; 366 } 367 } 368 369 inline bool isBrTable(const MachineInstr &MI) { 370 switch (MI.getOpcode()) { 371 case WebAssembly::BR_TABLE_I32: 372 case WebAssembly::BR_TABLE_I32_S: 373 case WebAssembly::BR_TABLE_I64: 374 case WebAssembly::BR_TABLE_I64_S: 375 return true; 376 default: 377 return false; 378 } 379 } 380 381 inline bool isMarker(unsigned Opc) { 382 switch (Opc) { 383 case WebAssembly::BLOCK: 384 case WebAssembly::BLOCK_S: 385 case WebAssembly::END_BLOCK: 386 case WebAssembly::END_BLOCK_S: 387 case WebAssembly::LOOP: 388 case WebAssembly::LOOP_S: 389 case WebAssembly::END_LOOP: 390 case WebAssembly::END_LOOP_S: 391 case WebAssembly::TRY: 392 case WebAssembly::TRY_S: 393 case WebAssembly::END_TRY: 394 case WebAssembly::END_TRY_S: 395 return true; 396 default: 397 return false; 398 } 399 } 400 401 } // end namespace WebAssembly 402 } // end namespace llvm 403 404 #endif 405