| 1 |
// Copyright 2021 the V8 project authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style license that can be |
| 3 |
// found in the LICENSE file. |
| 4 |
#include "jit/riscv64/constant/Base-constant-riscv.h" |
| 5 |
|
| 6 |
#include "mozilla/Assertions.h" |
| 7 |
#include "mozilla/Types.h" |
| 8 |
|
| 9 |
#include <stdio.h> |
| 10 |
|
| 11 |
#include "jit/riscv64/constant/Constant-riscv-c.h" |
| 12 |
#include "jit/riscv64/constant/Constant-riscv-d.h" |
| 13 |
#include "jit/riscv64/constant/Constant-riscv-f.h" |
| 14 |
#include "jit/riscv64/constant/Constant-riscv-i.h" |
| 15 |
#include "jit/riscv64/constant/Constant-riscv-m.h" |
| 16 |
#include "jit/riscv64/constant/Constant-riscv-v.h" |
| 17 |
#include "jit/riscv64/constant/Constant-riscv-zicsr.h" |
| 18 |
#include "jit/riscv64/constant/Constant-riscv-zifencei.h" |
| 19 |
#include "jit/riscv64/Simulator-riscv64.h" |
| 20 |
namespace js { |
| 21 |
namespace jit { |
| 22 |
|
| 23 |
int32_t ImmBranchMaxForwardOffset(OffsetSize bits) { |
| 24 |
return (1 << (bits - 1)) - 1; |
| 25 |
} |
| 26 |
|
| 27 |
bool InstructionBase::IsShortInstruction() const { |
| 28 |
uint8_t FirstByte = *reinterpret_cast<const uint8_t*>(this); |
| 29 |
return (FirstByte & 0x03) <= C2; |
| 30 |
} |
| 31 |
|
| 32 |
template <class T> |
| 33 |
int InstructionGetters<T>::RvcRdValue() const { |
| 34 |
MOZ_ASSERT(this->IsShortInstruction()); |
| 35 |
return this->Bits(kRvcRdShift + kRvcRdBits - 1, kRvcRdShift); |
| 36 |
} |
| 37 |
|
| 38 |
template <class T> |
| 39 |
int InstructionGetters<T>::RvcRs2Value() const { |
| 40 |
MOZ_ASSERT(this->IsShortInstruction()); |
| 41 |
return this->Bits(kRvcRs2Shift + kRvcRs2Bits - 1, kRvcRs2Shift); |
| 42 |
} |
| 43 |
|
| 44 |
template <class T> |
| 45 |
int InstructionGetters<T>::RvcRs1sValue() const { |
| 46 |
MOZ_ASSERT(this->IsShortInstruction()); |
| 47 |
return 0b1000 + this->Bits(kRvcRs1sShift + kRvcRs1sBits - 1, kRvcRs1sShift); |
| 48 |
} |
| 49 |
|
| 50 |
template <class T> |
| 51 |
int InstructionGetters<T>::RvcRs2sValue() const { |
| 52 |
MOZ_ASSERT(this->IsShortInstruction()); |
| 53 |
return 0b1000 + this->Bits(kRvcRs2sShift + kRvcRs2sBits - 1, kRvcRs2sShift); |
| 54 |
} |
| 55 |
|
| 56 |
template <class T> |
| 57 |
inline int InstructionGetters<T>::RvcFunct6Value() const { |
| 58 |
MOZ_ASSERT(this->IsShortInstruction()); |
| 59 |
return this->Bits(kRvcFunct6Shift + kRvcFunct6Bits - 1, kRvcFunct6Shift); |
| 60 |
} |
| 61 |
|
| 62 |
template <class T> |
| 63 |
inline int InstructionGetters<T>::RvcFunct4Value() const { |
| 64 |
MOZ_ASSERT(this->IsShortInstruction()); |
| 65 |
return this->Bits(kRvcFunct4Shift + kRvcFunct4Bits - 1, kRvcFunct4Shift); |
| 66 |
} |
| 67 |
|
| 68 |
template <class T> |
| 69 |
inline int InstructionGetters<T>::RvcFunct3Value() const { |
| 70 |
MOZ_ASSERT(this->IsShortInstruction()); |
| 71 |
return this->Bits(kRvcFunct3Shift + kRvcFunct3Bits - 1, kRvcFunct3Shift); |
| 72 |
} |
| 73 |
|
| 74 |
template <class T> |
| 75 |
inline int InstructionGetters<T>::RvcFunct2Value() const { |
| 76 |
MOZ_ASSERT(this->IsShortInstruction()); |
| 77 |
return this->Bits(kRvcFunct2Shift + kRvcFunct2Bits - 1, kRvcFunct2Shift); |
| 78 |
} |
| 79 |
|
| 80 |
template <class T> |
| 81 |
inline int InstructionGetters<T>::RvcFunct2BValue() const { |
| 82 |
MOZ_ASSERT(this->IsShortInstruction()); |
| 83 |
return this->Bits(kRvcFunct2BShift + kRvcFunct2Bits - 1, kRvcFunct2BShift); |
| 84 |
} |
| 85 |
|
| 86 |
template <class T> |
| 87 |
uint32_t InstructionGetters<T>::Rvvzimm() const { |
| 88 |
if ((this->InstructionBits() & |
| 89 |
(kBaseOpcodeMask | kFunct3Mask | 0x80000000)) == RO_V_VSETVLI) { |
| 90 |
uint32_t Bits = this->InstructionBits(); |
| 91 |
uint32_t zimm = Bits & kRvvZimmMask; |
| 92 |
return zimm >> kRvvZimmShift; |
| 93 |
} else { |
| 94 |
MOZ_ASSERT((this->InstructionBits() & |
| 95 |
(kBaseOpcodeMask | kFunct3Mask | 0xC0000000)) == RO_V_VSETIVLI); |
| 96 |
uint32_t Bits = this->InstructionBits(); |
| 97 |
uint32_t zimm = Bits & kRvvZimmMask; |
| 98 |
return (zimm >> kRvvZimmShift) & 0x3FF; |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
template <class T> |
| 103 |
uint32_t InstructionGetters<T>::Rvvuimm() const { |
| 104 |
MOZ_ASSERT((this->InstructionBits() & |
| 105 |
(kBaseOpcodeMask | kFunct3Mask | 0xC0000000)) == RO_V_VSETIVLI); |
| 106 |
uint32_t Bits = this->InstructionBits(); |
| 107 |
uint32_t uimm = Bits & kRvvUimmMask; |
| 108 |
return uimm >> kRvvUimmShift; |
| 109 |
} |
| 110 |
|
| 111 |
template class InstructionGetters<InstructionBase>; |
| 112 |
#ifdef JS_SIMULATOR_RISCV64 |
| 113 |
template class InstructionGetters<SimInstructionBase>; |
| 114 |
#endif |
| 115 |
|
| 116 |
OffsetSize InstructionBase::GetOffsetSize() const { |
| 117 |
if (IsIllegalInstruction()) { |
| 118 |
MOZ_CRASH("IllegalInstruction"); |
| 119 |
} |
| 120 |
if (IsShortInstruction()) { |
| 121 |
switch (InstructionBits() & kRvcOpcodeMask) { |
| 122 |
case RO_C_J: |
| 123 |
return kOffset11; |
| 124 |
case RO_C_BEQZ: |
| 125 |
case RO_C_BNEZ: |
| 126 |
return kOffset9; |
| 127 |
default: |
| 128 |
MOZ_CRASH("IllegalInstruction"); |
| 129 |
} |
| 130 |
} else { |
| 131 |
switch (InstructionBits() & kBaseOpcodeMask) { |
| 132 |
case BRANCH: |
| 133 |
return kOffset13; |
| 134 |
case JAL: |
| 135 |
return kOffset21; |
| 136 |
default: |
| 137 |
MOZ_CRASH("IllegalInstruction"); |
| 138 |
} |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
InstructionBase::Type InstructionBase::InstructionType() const { |
| 143 |
if (IsIllegalInstruction()) { |
| 144 |
return kUnsupported; |
| 145 |
} |
| 146 |
// RV64C Instruction |
| 147 |
if (IsShortInstruction()) { |
| 148 |
switch (InstructionBits() & kRvcOpcodeMask) { |
| 149 |
case RO_C_ADDI4SPN: |
| 150 |
return kCIWType; |
| 151 |
case RO_C_FLD: |
| 152 |
case RO_C_LW: |
| 153 |
#ifdef JS_CODEGEN_RISCV64 |
| 154 |
case RO_C_LD: |
| 155 |
#endif |
| 156 |
return kCLType; |
| 157 |
case RO_C_FSD: |
| 158 |
case RO_C_SW: |
| 159 |
#ifdef JS_CODEGEN_RISCV64 |
| 160 |
case RO_C_SD: |
| 161 |
#endif |
| 162 |
return kCSType; |
| 163 |
case RO_C_NOP_ADDI: |
| 164 |
case RO_C_LI: |
| 165 |
#ifdef JS_CODEGEN_RISCV64 |
| 166 |
case RO_C_ADDIW: |
| 167 |
#endif |
| 168 |
case RO_C_LUI_ADD: |
| 169 |
return kCIType; |
| 170 |
case RO_C_MISC_ALU: |
| 171 |
if (Bits(11, 10) != 0b11) |
| 172 |
return kCBType; |
| 173 |
else |
| 174 |
return kCAType; |
| 175 |
case RO_C_J: |
| 176 |
return kCJType; |
| 177 |
case RO_C_BEQZ: |
| 178 |
case RO_C_BNEZ: |
| 179 |
return kCBType; |
| 180 |
case RO_C_SLLI: |
| 181 |
case RO_C_FLDSP: |
| 182 |
case RO_C_LWSP: |
| 183 |
#ifdef JS_CODEGEN_RISCV64 |
| 184 |
case RO_C_LDSP: |
| 185 |
#endif |
| 186 |
return kCIType; |
| 187 |
case RO_C_JR_MV_ADD: |
| 188 |
return kCRType; |
| 189 |
case RO_C_FSDSP: |
| 190 |
case RO_C_SWSP: |
| 191 |
#ifdef JS_CODEGEN_RISCV64 |
| 192 |
case RO_C_SDSP: |
| 193 |
#endif |
| 194 |
return kCSSType; |
| 195 |
default: |
| 196 |
break; |
| 197 |
} |
| 198 |
} else { |
| 199 |
// RISCV routine |
| 200 |
switch (InstructionBits() & kBaseOpcodeMask) { |
| 201 |
case LOAD: |
| 202 |
return kIType; |
| 203 |
case LOAD_FP: |
| 204 |
return kIType; |
| 205 |
case MISC_MEM: |
| 206 |
return kIType; |
| 207 |
case OP_IMM: |
| 208 |
return kIType; |
| 209 |
case AUIPC: |
| 210 |
return kUType; |
| 211 |
case OP_IMM_32: |
| 212 |
return kIType; |
| 213 |
case STORE: |
| 214 |
return kSType; |
| 215 |
case STORE_FP: |
| 216 |
return kSType; |
| 217 |
case AMO: |
| 218 |
return kRType; |
| 219 |
case OP: |
| 220 |
return kRType; |
| 221 |
case LUI: |
| 222 |
return kUType; |
| 223 |
case OP_32: |
| 224 |
return kRType; |
| 225 |
case MADD: |
| 226 |
case MSUB: |
| 227 |
case NMSUB: |
| 228 |
case NMADD: |
| 229 |
return kR4Type; |
| 230 |
case OP_FP: |
| 231 |
return kRType; |
| 232 |
case BRANCH: |
| 233 |
return kBType; |
| 234 |
case JALR: |
| 235 |
return kIType; |
| 236 |
case JAL: |
| 237 |
return kJType; |
| 238 |
case SYSTEM: |
| 239 |
return kIType; |
| 240 |
case OP_V: |
| 241 |
return kVType; |
| 242 |
} |
| 243 |
} |
| 244 |
return kUnsupported; |
| 245 |
} |
| 246 |
} // namespace jit |
| 247 |
} // namespace js |