| 1 |
// Copyright (c) 1994-2006 Sun Microsystems Inc. |
| 2 |
// All Rights Reserved. |
| 3 |
// |
| 4 |
// Redistribution and use in source and binary forms, with or without |
| 5 |
// modification, are permitted provided that the following conditions are |
| 6 |
// met: |
| 7 |
// |
| 8 |
// - Redistributions of source code must retain the above copyright notice, |
| 9 |
// this list of conditions and the following disclaimer. |
| 10 |
// |
| 11 |
// - Redistribution in binary form must reproduce the above copyright |
| 12 |
// notice, this list of conditions and the following disclaimer in the |
| 13 |
// documentation and/or other materials provided with the distribution. |
| 14 |
// |
| 15 |
// - Neither the name of Sun Microsystems or the names of contributors may |
| 16 |
// be used to endorse or promote products derived from this software without |
| 17 |
// specific prior written permission. |
| 18 |
// |
| 19 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
| 20 |
// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 21 |
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 22 |
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 23 |
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 24 |
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 25 |
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 26 |
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 27 |
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 28 |
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 29 |
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 |
|
| 31 |
// The original source code covered by the above license above has been |
| 32 |
// modified significantly by Google Inc. |
| 33 |
// Copyright 2021 the V8 project authors. All rights reserved. |
| 34 |
|
| 35 |
#ifndef jit_riscv64_extension_Base_assembler_riscv_h |
| 36 |
#define jit_riscv64_extension_Base_assembler_riscv_h |
| 37 |
|
| 38 |
#include <memory> |
| 39 |
#include <set> |
| 40 |
#include <stdio.h> |
| 41 |
|
| 42 |
#include "jit/Label.h" |
| 43 |
#include "jit/riscv64/Architecture-riscv64.h" |
| 44 |
#include "jit/riscv64/constant/Constant-riscv64.h" |
| 45 |
#include "jit/riscv64/Register-riscv64.h" |
| 46 |
|
| 47 |
#define xlen (uint8_t(sizeof(void*) * 8)) |
| 48 |
|
| 49 |
#define kBitsPerByte 8UL |
| 50 |
// Check number width. |
| 51 |
inline constexpr bool is_intn(int64_t x, unsigned n) { |
| 52 |
MOZ_ASSERT((0 < n) && (n < 64)); |
| 53 |
int64_t limit = static_cast<int64_t>(1) << (n - 1); |
| 54 |
return (-limit <= x) && (x < limit); |
| 55 |
} |
| 56 |
|
| 57 |
inline constexpr bool is_uintn(int64_t x, unsigned n) { |
| 58 |
MOZ_ASSERT((0 < n) && (n < (sizeof(x) * kBitsPerByte))); |
| 59 |
return !(x >> n); |
| 60 |
} |
| 61 |
#undef kBitsPerByte |
| 62 |
// clang-format off |
| 63 |
#define INT_1_TO_63_LIST(V) \ |
| 64 |
V(1) V(2) V(3) V(4) V(5) V(6) V(7) V(8) V(9) V(10) \ |
| 65 |
V(11) V(12) V(13) V(14) V(15) V(16) V(17) V(18) V(19) V(20) \ |
| 66 |
V(21) V(22) V(23) V(24) V(25) V(26) V(27) V(28) V(29) V(30) \ |
| 67 |
V(31) V(32) V(33) V(34) V(35) V(36) V(37) V(38) V(39) V(40) \ |
| 68 |
V(41) V(42) V(43) V(44) V(45) V(46) V(47) V(48) V(49) V(50) \ |
| 69 |
V(51) V(52) V(53) V(54) V(55) V(56) V(57) V(58) V(59) V(60) \ |
| 70 |
V(61) V(62) V(63) |
| 71 |
// clang-format on |
| 72 |
|
| 73 |
#define DECLARE_IS_INT_N(N) \ |
| 74 |
inline constexpr bool is_int##N(int64_t x) { return is_intn(x, N); } |
| 75 |
|
| 76 |
#define DECLARE_IS_UINT_N(N) \ |
| 77 |
template <class T> \ |
| 78 |
inline constexpr bool is_uint##N(T x) { \ |
| 79 |
return is_uintn(x, N); \ |
| 80 |
} |
| 81 |
INT_1_TO_63_LIST(DECLARE_IS_INT_N) |
| 82 |
INT_1_TO_63_LIST(DECLARE_IS_UINT_N) |
| 83 |
|
| 84 |
#undef DECLARE_IS_INT_N |
| 85 |
#undef INT_1_TO_63_LIST |
| 86 |
|
| 87 |
namespace js { |
| 88 |
namespace jit { |
| 89 |
|
| 90 |
typedef FloatRegister FPURegister; |
| 91 |
#define zero_reg zero |
| 92 |
|
| 93 |
#define DEBUG_PRINTF(...) \ |
| 94 |
if (FLAG_riscv_debug) { \ |
| 95 |
std::printf(__VA_ARGS__); \ |
| 96 |
} |
| 97 |
|
| 98 |
int ToNumber(Register reg); |
| 99 |
Register ToRegister(uint32_t num); |
| 100 |
|
| 101 |
class AssemblerRiscvBase { |
| 102 |
protected: |
| 103 |
virtual int32_t branch_offset_helper(Label* L, OffsetSize bits) = 0; |
| 104 |
|
| 105 |
virtual void emit(Instr x) = 0; |
| 106 |
virtual void emit(ShortInstr x) = 0; |
| 107 |
virtual void emit(uint64_t x) = 0; |
| 108 |
virtual uint32_t currentOffset() = 0; |
| 109 |
// Instruction generation. |
| 110 |
|
| 111 |
// ----- Top-level instruction formats match those in the ISA manual |
| 112 |
// (R, I, S, B, U, J). These match the formats defined in LLVM's |
| 113 |
// RISCVInstrFormats.td. |
| 114 |
void GenInstrR(uint8_t funct7, uint8_t funct3, BaseOpcode opcode, Register rd, |
| 115 |
Register rs1, Register rs2); |
| 116 |
void GenInstrR(uint8_t funct7, uint8_t funct3, BaseOpcode opcode, |
| 117 |
FPURegister rd, FPURegister rs1, FPURegister rs2); |
| 118 |
void GenInstrR(uint8_t funct7, uint8_t funct3, BaseOpcode opcode, Register rd, |
| 119 |
FPURegister rs1, Register rs2); |
| 120 |
void GenInstrR(uint8_t funct7, uint8_t funct3, BaseOpcode opcode, |
| 121 |
FPURegister rd, Register rs1, Register rs2); |
| 122 |
void GenInstrR(uint8_t funct7, uint8_t funct3, BaseOpcode opcode, |
| 123 |
FPURegister rd, FPURegister rs1, Register rs2); |
| 124 |
void GenInstrR(uint8_t funct7, uint8_t funct3, BaseOpcode opcode, Register rd, |
| 125 |
FPURegister rs1, FPURegister rs2); |
| 126 |
void GenInstrR4(uint8_t funct2, BaseOpcode opcode, Register rd, Register rs1, |
| 127 |
Register rs2, Register rs3, FPURoundingMode frm); |
| 128 |
void GenInstrR4(uint8_t funct2, BaseOpcode opcode, FPURegister rd, |
| 129 |
FPURegister rs1, FPURegister rs2, FPURegister rs3, |
| 130 |
FPURoundingMode frm); |
| 131 |
void GenInstrRAtomic(uint8_t funct5, bool aq, bool rl, uint8_t funct3, |
| 132 |
Register rd, Register rs1, Register rs2); |
| 133 |
void GenInstrRFrm(uint8_t funct7, BaseOpcode opcode, Register rd, |
| 134 |
Register rs1, Register rs2, FPURoundingMode frm); |
| 135 |
void GenInstrI(uint8_t funct3, BaseOpcode opcode, Register rd, Register rs1, |
| 136 |
int16_t imm12); |
| 137 |
void GenInstrI(uint8_t funct3, BaseOpcode opcode, FPURegister rd, |
| 138 |
Register rs1, int16_t imm12); |
| 139 |
void GenInstrIShift(bool arithshift, uint8_t funct3, BaseOpcode opcode, |
| 140 |
Register rd, Register rs1, uint8_t shamt); |
| 141 |
void GenInstrIShiftW(bool arithshift, uint8_t funct3, BaseOpcode opcode, |
| 142 |
Register rd, Register rs1, uint8_t shamt); |
| 143 |
void GenInstrS(uint8_t funct3, BaseOpcode opcode, Register rs1, Register rs2, |
| 144 |
int16_t imm12); |
| 145 |
void GenInstrS(uint8_t funct3, BaseOpcode opcode, Register rs1, |
| 146 |
FPURegister rs2, int16_t imm12); |
| 147 |
void GenInstrB(uint8_t funct3, BaseOpcode opcode, Register rs1, Register rs2, |
| 148 |
int16_t imm12); |
| 149 |
void GenInstrU(BaseOpcode opcode, Register rd, int32_t imm20); |
| 150 |
void GenInstrJ(BaseOpcode opcode, Register rd, int32_t imm20); |
| 151 |
void GenInstrCR(uint8_t funct4, BaseOpcode opcode, Register rd, Register rs2); |
| 152 |
void GenInstrCA(uint8_t funct6, BaseOpcode opcode, Register rd, uint8_t funct, |
| 153 |
Register rs2); |
| 154 |
void GenInstrCI(uint8_t funct3, BaseOpcode opcode, Register rd, int8_t imm6); |
| 155 |
void GenInstrCIU(uint8_t funct3, BaseOpcode opcode, Register rd, |
| 156 |
uint8_t uimm6); |
| 157 |
void GenInstrCIU(uint8_t funct3, BaseOpcode opcode, FPURegister rd, |
| 158 |
uint8_t uimm6); |
| 159 |
void GenInstrCIW(uint8_t funct3, BaseOpcode opcode, Register rd, |
| 160 |
uint8_t uimm8); |
| 161 |
void GenInstrCSS(uint8_t funct3, BaseOpcode opcode, FPURegister rs2, |
| 162 |
uint8_t uimm6); |
| 163 |
void GenInstrCSS(uint8_t funct3, BaseOpcode opcode, Register rs2, |
| 164 |
uint8_t uimm6); |
| 165 |
void GenInstrCL(uint8_t funct3, BaseOpcode opcode, Register rd, Register rs1, |
| 166 |
uint8_t uimm5); |
| 167 |
void GenInstrCL(uint8_t funct3, BaseOpcode opcode, FPURegister rd, |
| 168 |
Register rs1, uint8_t uimm5); |
| 169 |
void GenInstrCS(uint8_t funct3, BaseOpcode opcode, Register rs2, Register rs1, |
| 170 |
uint8_t uimm5); |
| 171 |
void GenInstrCS(uint8_t funct3, BaseOpcode opcode, FPURegister rs2, |
| 172 |
Register rs1, uint8_t uimm5); |
| 173 |
void GenInstrCJ(uint8_t funct3, BaseOpcode opcode, uint16_t uint11); |
| 174 |
void GenInstrCB(uint8_t funct3, BaseOpcode opcode, Register rs1, |
| 175 |
uint8_t uimm8); |
| 176 |
void GenInstrCBA(uint8_t funct3, uint8_t funct2, BaseOpcode opcode, |
| 177 |
Register rs1, int8_t imm6); |
| 178 |
|
| 179 |
// ----- Instruction class templates match those in LLVM's RISCVInstrInfo.td |
| 180 |
void GenInstrBranchCC_rri(uint8_t funct3, Register rs1, Register rs2, |
| 181 |
int16_t imm12); |
| 182 |
void GenInstrLoad_ri(uint8_t funct3, Register rd, Register rs1, |
| 183 |
int16_t imm12); |
| 184 |
void GenInstrStore_rri(uint8_t funct3, Register rs1, Register rs2, |
| 185 |
int16_t imm12); |
| 186 |
void GenInstrALU_ri(uint8_t funct3, Register rd, Register rs1, int16_t imm12); |
| 187 |
void GenInstrShift_ri(bool arithshift, uint8_t funct3, Register rd, |
| 188 |
Register rs1, uint8_t shamt); |
| 189 |
void GenInstrALU_rr(uint8_t funct7, uint8_t funct3, Register rd, Register rs1, |
| 190 |
Register rs2); |
| 191 |
void GenInstrCSR_ir(uint8_t funct3, Register rd, ControlStatusReg csr, |
| 192 |
Register rs1); |
| 193 |
void GenInstrCSR_ii(uint8_t funct3, Register rd, ControlStatusReg csr, |
| 194 |
uint8_t rs1); |
| 195 |
void GenInstrShiftW_ri(bool arithshift, uint8_t funct3, Register rd, |
| 196 |
Register rs1, uint8_t shamt); |
| 197 |
void GenInstrALUW_rr(uint8_t funct7, uint8_t funct3, Register rd, |
| 198 |
Register rs1, Register rs2); |
| 199 |
void GenInstrPriv(uint8_t funct7, Register rs1, Register rs2); |
| 200 |
void GenInstrLoadFP_ri(uint8_t funct3, FPURegister rd, Register rs1, |
| 201 |
int16_t imm12); |
| 202 |
void GenInstrStoreFP_rri(uint8_t funct3, Register rs1, FPURegister rs2, |
| 203 |
int16_t imm12); |
| 204 |
void GenInstrALUFP_rr(uint8_t funct7, uint8_t funct3, FPURegister rd, |
| 205 |
FPURegister rs1, FPURegister rs2); |
| 206 |
void GenInstrALUFP_rr(uint8_t funct7, uint8_t funct3, FPURegister rd, |
| 207 |
Register rs1, Register rs2); |
| 208 |
void GenInstrALUFP_rr(uint8_t funct7, uint8_t funct3, FPURegister rd, |
| 209 |
FPURegister rs1, Register rs2); |
| 210 |
void GenInstrALUFP_rr(uint8_t funct7, uint8_t funct3, Register rd, |
| 211 |
FPURegister rs1, Register rs2); |
| 212 |
void GenInstrALUFP_rr(uint8_t funct7, uint8_t funct3, Register rd, |
| 213 |
FPURegister rs1, FPURegister rs2); |
| 214 |
}; |
| 215 |
|
| 216 |
} // namespace jit |
| 217 |
} // namespace js |
| 218 |
|
| 219 |
#endif // jit_riscv64_extension_Base_assembler_riscv_h |