blob: 7dca89ab822d8319f304584eab3ab1a215d78ab3 [file] [log] [blame]
Dan Gohman10e730a2015-06-29 23:51:55 +00001//==- WebAssemblyMCTargetDesc.h - WebAssembly Target Descriptions -*- 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/// \file
11/// \brief This file provides WebAssembly-specific target descriptions.
12///
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_LIB_TARGET_WEBASSEMBLY_MCTARGETDESC_WEBASSEMBLYMCTARGETDESC_H
16#define LLVM_LIB_TARGET_WEBASSEMBLY_MCTARGETDESC_WEBASSEMBLYMCTARGETDESC_H
17
Zachary Turner264b5d92017-06-07 03:48:56 +000018#include "llvm/BinaryFormat/Wasm.h"
Dan Gohmana11fb232016-01-12 03:09:16 +000019#include "llvm/MC/MCInstrDesc.h"
Dan Gohman10e730a2015-06-29 23:51:55 +000020#include "llvm/Support/DataTypes.h"
Derek Schuff669300d2017-10-10 17:31:43 +000021#include <memory>
Dan Gohman10e730a2015-06-29 23:51:55 +000022
23namespace llvm {
24
Dan Gohman10e730a2015-06-29 23:51:55 +000025class MCAsmBackend;
26class MCCodeEmitter;
27class MCContext;
28class MCInstrInfo;
Dan Gohman10e730a2015-06-29 23:51:55 +000029class MCObjectWriter;
Dan Gohman10e730a2015-06-29 23:51:55 +000030class MCSubtargetInfo;
Dan Gohman3acb1872016-10-24 23:27:49 +000031class MVT;
Dan Gohman10e730a2015-06-29 23:51:55 +000032class Target;
33class Triple;
Dan Gohman53828fd2015-11-23 16:50:18 +000034class raw_pwrite_stream;
Dan Gohman10e730a2015-06-29 23:51:55 +000035
Mehdi Aminif42454b2016-10-09 23:00:34 +000036Target &getTheWebAssemblyTarget32();
37Target &getTheWebAssemblyTarget64();
Dan Gohman10e730a2015-06-29 23:51:55 +000038
Sam Clegg9d24fb72017-06-16 23:59:10 +000039MCCodeEmitter *createWebAssemblyMCCodeEmitter(const MCInstrInfo &MCII);
Dan Gohman53828fd2015-11-23 16:50:18 +000040
Dan Gohmancceedf72016-01-08 00:43:54 +000041MCAsmBackend *createWebAssemblyAsmBackend(const Triple &TT);
Dan Gohman10e730a2015-06-29 23:51:55 +000042
Derek Schuff669300d2017-10-10 17:31:43 +000043std::unique_ptr<MCObjectWriter>
44createWebAssemblyELFObjectWriter(raw_pwrite_stream &OS,
45 bool Is64Bit, uint8_t OSABI);
Dan Gohman05ac43f2015-12-17 01:39:00 +000046
Derek Schuff669300d2017-10-10 17:31:43 +000047std::unique_ptr<MCObjectWriter>
48createWebAssemblyWasmObjectWriter(raw_pwrite_stream &OS,
49 bool Is64Bit);
Dan Gohman18eafb62017-02-22 01:23:18 +000050
Dan Gohmana11fb232016-01-12 03:09:16 +000051namespace WebAssembly {
52enum OperandType {
53 /// Basic block label in a branch construct.
54 OPERAND_BASIC_BLOCK = MCOI::OPERAND_FIRST_TARGET,
Dan Gohman4fc4e422016-10-24 19:49:43 +000055 /// Local index.
56 OPERAND_LOCAL,
Dan Gohmanb89f2d32017-02-02 19:29:44 +000057 /// Global index.
58 OPERAND_GLOBAL,
Dan Gohman5a68ec72016-10-05 21:24:08 +000059 /// 32-bit integer immediates.
60 OPERAND_I32IMM,
61 /// 64-bit integer immediates.
62 OPERAND_I64IMM,
Dan Gohmanaa742912016-02-16 15:14:23 +000063 /// 32-bit floating-point immediates.
Dan Gohman4b8e8be2016-10-03 21:31:31 +000064 OPERAND_F32IMM,
Dan Gohmanaa742912016-02-16 15:14:23 +000065 /// 64-bit floating-point immediates.
Dan Gohman4b8e8be2016-10-03 21:31:31 +000066 OPERAND_F64IMM,
Dan Gohman00d734d2016-12-23 03:23:52 +000067 /// 32-bit unsigned function indices.
68 OPERAND_FUNCTION32,
69 /// 32-bit unsigned memory offsets.
70 OPERAND_OFFSET32,
Dan Gohmanbb372242016-01-26 03:39:31 +000071 /// p2align immediate for load and store address alignment.
Dan Gohman2726b882016-10-06 22:29:32 +000072 OPERAND_P2ALIGN,
73 /// signature immediate for block/loop.
Dan Gohmand934cb82017-02-24 23:18:00 +000074 OPERAND_SIGNATURE,
75 /// type signature immediate for call_indirect.
76 OPERAND_TYPEINDEX,
Dan Gohmana11fb232016-01-12 03:09:16 +000077};
78} // end namespace WebAssembly
79
80namespace WebAssemblyII {
81enum {
82 // For variadic instructions, this flag indicates whether an operand
83 // in the variable_ops range is an immediate value.
Dan Gohman3469ee12016-01-12 20:30:51 +000084 VariableOpIsImmediate = (1 << 0),
Dan Gohman1d68e80f2016-01-12 19:14:46 +000085 // For immediate values in the variable_ops range, this flag indicates
86 // whether the value represents a control-flow label.
Dan Gohman3acb1872016-10-24 23:27:49 +000087 VariableOpImmediateIsLabel = (1 << 1)
Dan Gohmana11fb232016-01-12 03:09:16 +000088};
89} // end namespace WebAssemblyII
90
Dan Gohman10e730a2015-06-29 23:51:55 +000091} // end namespace llvm
92
93// Defines symbolic names for WebAssembly registers. This defines a mapping from
94// register name to register number.
95//
JF Bastien5ca0bac2015-07-10 18:23:10 +000096#define GET_REGINFO_ENUM
97#include "WebAssemblyGenRegisterInfo.inc"
98
JF Bastienb9073fb2015-07-22 21:28:15 +000099// Defines symbolic names for the WebAssembly instructions.
100//
101#define GET_INSTRINFO_ENUM
102#include "WebAssemblyGenInstrInfo.inc"
103
Dan Gohman10e730a2015-06-29 23:51:55 +0000104#define GET_SUBTARGETINFO_ENUM
105#include "WebAssemblyGenSubtargetInfo.inc"
106
Dan Gohmanbb372242016-01-26 03:39:31 +0000107namespace llvm {
108namespace WebAssembly {
109
110/// Return the default p2align value for a load or store with the given opcode.
111inline unsigned GetDefaultP2Align(unsigned Opcode) {
112 switch (Opcode) {
113 case WebAssembly::LOAD8_S_I32:
114 case WebAssembly::LOAD8_U_I32:
115 case WebAssembly::LOAD8_S_I64:
116 case WebAssembly::LOAD8_U_I64:
Derek Schuff885dc592017-10-05 21:18:42 +0000117 case WebAssembly::ATOMIC_LOAD8_U_I32:
118 case WebAssembly::ATOMIC_LOAD8_U_I64:
Dan Gohmanbb372242016-01-26 03:39:31 +0000119 case WebAssembly::STORE8_I32:
120 case WebAssembly::STORE8_I64:
121 return 0;
122 case WebAssembly::LOAD16_S_I32:
123 case WebAssembly::LOAD16_U_I32:
124 case WebAssembly::LOAD16_S_I64:
125 case WebAssembly::LOAD16_U_I64:
Derek Schuff885dc592017-10-05 21:18:42 +0000126 case WebAssembly::ATOMIC_LOAD16_U_I32:
127 case WebAssembly::ATOMIC_LOAD16_U_I64:
Dan Gohmanbb372242016-01-26 03:39:31 +0000128 case WebAssembly::STORE16_I32:
129 case WebAssembly::STORE16_I64:
130 return 1;
131 case WebAssembly::LOAD_I32:
132 case WebAssembly::LOAD_F32:
133 case WebAssembly::STORE_I32:
134 case WebAssembly::STORE_F32:
135 case WebAssembly::LOAD32_S_I64:
136 case WebAssembly::LOAD32_U_I64:
137 case WebAssembly::STORE32_I64:
Derek Schuff18ba1922017-08-30 18:07:45 +0000138 case WebAssembly::ATOMIC_LOAD_I32:
Derek Schuff885dc592017-10-05 21:18:42 +0000139 case WebAssembly::ATOMIC_LOAD32_U_I64:
Dan Gohmanbb372242016-01-26 03:39:31 +0000140 return 2;
141 case WebAssembly::LOAD_I64:
142 case WebAssembly::LOAD_F64:
143 case WebAssembly::STORE_I64:
144 case WebAssembly::STORE_F64:
Derek Schuff885dc592017-10-05 21:18:42 +0000145 case WebAssembly::ATOMIC_LOAD_I64:
Dan Gohmanbb372242016-01-26 03:39:31 +0000146 return 3;
Derek Schuffc64d7652016-08-01 22:25:02 +0000147 default:
148 llvm_unreachable("Only loads and stores have p2align values");
Dan Gohmanbb372242016-01-26 03:39:31 +0000149 }
150}
151
Derek Schuffc97ba932016-01-30 21:43:08 +0000152/// The operand number of the load or store address in load/store instructions.
Dan Gohman48abaa92016-10-25 00:17:11 +0000153static const unsigned LoadAddressOperandNo = 3;
154static const unsigned StoreAddressOperandNo = 2;
Dan Gohman7f1bdb22016-10-06 22:08:28 +0000155
156/// The operand number of the load or store p2align in load/store instructions.
Dan Gohman48abaa92016-10-25 00:17:11 +0000157static const unsigned LoadP2AlignOperandNo = 1;
158static const unsigned StoreP2AlignOperandNo = 0;
Dan Gohmanbb372242016-01-26 03:39:31 +0000159
Dan Gohman2726b882016-10-06 22:29:32 +0000160/// This is used to indicate block signatures.
Dan Gohman4fc4e422016-10-24 19:49:43 +0000161enum class ExprType {
Derek Schuffe2688c42017-03-14 20:23:22 +0000162 Void = -0x40,
163 I32 = -0x01,
164 I64 = -0x02,
165 F32 = -0x03,
166 F64 = -0x04,
167 I8x16 = -0x05,
168 I16x8 = -0x06,
169 I32x4 = -0x07,
170 F32x4 = -0x08,
171 B8x16 = -0x09,
172 B16x8 = -0x0a,
173 B32x4 = -0x0b
Dan Gohman4fc4e422016-10-24 19:49:43 +0000174};
175
Dan Gohman3acb1872016-10-24 23:27:49 +0000176/// Instruction opcodes emitted via means other than CodeGen.
177static const unsigned Nop = 0x01;
178static const unsigned End = 0x0b;
179
Derek Schuffe2688c42017-03-14 20:23:22 +0000180wasm::ValType toValType(const MVT &Ty);
Dan Gohman3acb1872016-10-24 23:27:49 +0000181
Dan Gohmanbb372242016-01-26 03:39:31 +0000182} // end namespace WebAssembly
183} // end namespace llvm
184
Dan Gohman10e730a2015-06-29 23:51:55 +0000185#endif