106c3fb27SDimitry Andric //===-- RISCVBaseInfo.cpp - Top level definitions for RISC-V MC -----------===//
2e8d8bef9SDimitry Andric //
3e8d8bef9SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e8d8bef9SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5e8d8bef9SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e8d8bef9SDimitry Andric //
7e8d8bef9SDimitry Andric //===----------------------------------------------------------------------===//
8e8d8bef9SDimitry Andric //
906c3fb27SDimitry Andric // This file contains small standalone enum definitions for the RISC-V target
10e8d8bef9SDimitry Andric // useful for the compiler back-end and the MC libraries.
11e8d8bef9SDimitry Andric //
12e8d8bef9SDimitry Andric //===----------------------------------------------------------------------===//
13e8d8bef9SDimitry Andric
14e8d8bef9SDimitry Andric #include "RISCVBaseInfo.h"
15e8d8bef9SDimitry Andric #include "llvm/ADT/ArrayRef.h"
16bdd1243dSDimitry Andric #include "llvm/MC/MCInst.h"
17bdd1243dSDimitry Andric #include "llvm/MC/MCRegisterInfo.h"
18349cc55cSDimitry Andric #include "llvm/MC/MCSubtargetInfo.h"
19e8d8bef9SDimitry Andric #include "llvm/Support/raw_ostream.h"
2006c3fb27SDimitry Andric #include "llvm/TargetParser/TargetParser.h"
2106c3fb27SDimitry Andric #include "llvm/TargetParser/Triple.h"
22e8d8bef9SDimitry Andric
23e8d8bef9SDimitry Andric namespace llvm {
24349cc55cSDimitry Andric
25349cc55cSDimitry Andric extern const SubtargetFeatureKV RISCVFeatureKV[RISCV::NumSubtargetFeatures];
26349cc55cSDimitry Andric
27e8d8bef9SDimitry Andric namespace RISCVSysReg {
28e8d8bef9SDimitry Andric #define GET_SysRegsList_IMPL
29e8d8bef9SDimitry Andric #include "RISCVGenSearchableTables.inc"
30e8d8bef9SDimitry Andric } // namespace RISCVSysReg
31e8d8bef9SDimitry Andric
320eae32dcSDimitry Andric namespace RISCVInsnOpcode {
330eae32dcSDimitry Andric #define GET_RISCVOpcodesList_IMPL
340eae32dcSDimitry Andric #include "RISCVGenSearchableTables.inc"
350eae32dcSDimitry Andric } // namespace RISCVInsnOpcode
360eae32dcSDimitry Andric
37e8d8bef9SDimitry Andric namespace RISCVABI {
computeTargetABI(const Triple & TT,const FeatureBitset & FeatureBits,StringRef ABIName)3806c3fb27SDimitry Andric ABI computeTargetABI(const Triple &TT, const FeatureBitset &FeatureBits,
39e8d8bef9SDimitry Andric StringRef ABIName) {
40e8d8bef9SDimitry Andric auto TargetABI = getTargetABI(ABIName);
41e8d8bef9SDimitry Andric bool IsRV64 = TT.isArch64Bit();
42*0fca6ea1SDimitry Andric bool IsRVE = FeatureBits[RISCV::FeatureStdExtE];
43e8d8bef9SDimitry Andric
44e8d8bef9SDimitry Andric if (!ABIName.empty() && TargetABI == ABI_Unknown) {
45e8d8bef9SDimitry Andric errs()
46e8d8bef9SDimitry Andric << "'" << ABIName
47e8d8bef9SDimitry Andric << "' is not a recognized ABI for this target (ignoring target-abi)\n";
485f757f3fSDimitry Andric } else if (ABIName.starts_with("ilp32") && IsRV64) {
49e8d8bef9SDimitry Andric errs() << "32-bit ABIs are not supported for 64-bit targets (ignoring "
50e8d8bef9SDimitry Andric "target-abi)\n";
51e8d8bef9SDimitry Andric TargetABI = ABI_Unknown;
525f757f3fSDimitry Andric } else if (ABIName.starts_with("lp64") && !IsRV64) {
53e8d8bef9SDimitry Andric errs() << "64-bit ABIs are not supported for 32-bit targets (ignoring "
54e8d8bef9SDimitry Andric "target-abi)\n";
55e8d8bef9SDimitry Andric TargetABI = ABI_Unknown;
5606c3fb27SDimitry Andric } else if (!IsRV64 && IsRVE && TargetABI != ABI_ILP32E &&
5706c3fb27SDimitry Andric TargetABI != ABI_Unknown) {
58e8d8bef9SDimitry Andric // TODO: move this checking to RISCVTargetLowering and RISCVAsmParser
59e8d8bef9SDimitry Andric errs()
60e8d8bef9SDimitry Andric << "Only the ilp32e ABI is supported for RV32E (ignoring target-abi)\n";
61e8d8bef9SDimitry Andric TargetABI = ABI_Unknown;
6206c3fb27SDimitry Andric } else if (IsRV64 && IsRVE && TargetABI != ABI_LP64E &&
6306c3fb27SDimitry Andric TargetABI != ABI_Unknown) {
6406c3fb27SDimitry Andric // TODO: move this checking to RISCVTargetLowering and RISCVAsmParser
6506c3fb27SDimitry Andric errs()
6606c3fb27SDimitry Andric << "Only the lp64e ABI is supported for RV64E (ignoring target-abi)\n";
6706c3fb27SDimitry Andric TargetABI = ABI_Unknown;
68e8d8bef9SDimitry Andric }
69e8d8bef9SDimitry Andric
707a6dacacSDimitry Andric if ((TargetABI == RISCVABI::ABI::ABI_ILP32E ||
717a6dacacSDimitry Andric (TargetABI == ABI_Unknown && IsRVE && !IsRV64)) &&
727a6dacacSDimitry Andric FeatureBits[RISCV::FeatureStdExtD])
737a6dacacSDimitry Andric report_fatal_error("ILP32E cannot be used with the D ISA extension");
747a6dacacSDimitry Andric
75e8d8bef9SDimitry Andric if (TargetABI != ABI_Unknown)
76e8d8bef9SDimitry Andric return TargetABI;
77e8d8bef9SDimitry Andric
7881ad6265SDimitry Andric // If no explicit ABI is given, try to compute the default ABI.
7981ad6265SDimitry Andric auto ISAInfo = RISCVFeatures::parseFeatureBits(IsRV64, FeatureBits);
8081ad6265SDimitry Andric if (!ISAInfo)
8181ad6265SDimitry Andric report_fatal_error(ISAInfo.takeError());
8281ad6265SDimitry Andric return getTargetABI((*ISAInfo)->computeDefaultABI());
83e8d8bef9SDimitry Andric }
84e8d8bef9SDimitry Andric
getTargetABI(StringRef ABIName)85e8d8bef9SDimitry Andric ABI getTargetABI(StringRef ABIName) {
86e8d8bef9SDimitry Andric auto TargetABI = StringSwitch<ABI>(ABIName)
87e8d8bef9SDimitry Andric .Case("ilp32", ABI_ILP32)
88e8d8bef9SDimitry Andric .Case("ilp32f", ABI_ILP32F)
89e8d8bef9SDimitry Andric .Case("ilp32d", ABI_ILP32D)
90e8d8bef9SDimitry Andric .Case("ilp32e", ABI_ILP32E)
91e8d8bef9SDimitry Andric .Case("lp64", ABI_LP64)
92e8d8bef9SDimitry Andric .Case("lp64f", ABI_LP64F)
93e8d8bef9SDimitry Andric .Case("lp64d", ABI_LP64D)
9406c3fb27SDimitry Andric .Case("lp64e", ABI_LP64E)
95e8d8bef9SDimitry Andric .Default(ABI_Unknown);
96e8d8bef9SDimitry Andric return TargetABI;
97e8d8bef9SDimitry Andric }
98e8d8bef9SDimitry Andric
99e8d8bef9SDimitry Andric // To avoid the BP value clobbered by a function call, we need to choose a
100e8d8bef9SDimitry Andric // callee saved register to save the value. RV32E only has X8 and X9 as callee
101e8d8bef9SDimitry Andric // saved registers and X8 will be used as fp. So we choose X9 as bp.
getBPReg()102e8d8bef9SDimitry Andric MCRegister getBPReg() { return RISCV::X9; }
103e8d8bef9SDimitry Andric
104e8d8bef9SDimitry Andric // Returns the register holding shadow call stack pointer.
getSCSPReg()10506c3fb27SDimitry Andric MCRegister getSCSPReg() { return RISCV::X3; }
106e8d8bef9SDimitry Andric
107e8d8bef9SDimitry Andric } // namespace RISCVABI
108e8d8bef9SDimitry Andric
109e8d8bef9SDimitry Andric namespace RISCVFeatures {
110e8d8bef9SDimitry Andric
validate(const Triple & TT,const FeatureBitset & FeatureBits)111e8d8bef9SDimitry Andric void validate(const Triple &TT, const FeatureBitset &FeatureBits) {
112fe6060f1SDimitry Andric if (TT.isArch64Bit() && !FeatureBits[RISCV::Feature64Bit])
113fe6060f1SDimitry Andric report_fatal_error("RV64 target requires an RV64 CPU");
114bdd1243dSDimitry Andric if (!TT.isArch64Bit() && !FeatureBits[RISCV::Feature32Bit])
115fe6060f1SDimitry Andric report_fatal_error("RV32 target requires an RV32 CPU");
116bdd1243dSDimitry Andric if (FeatureBits[RISCV::Feature32Bit] &&
117bdd1243dSDimitry Andric FeatureBits[RISCV::Feature64Bit])
118bdd1243dSDimitry Andric report_fatal_error("RV32 and RV64 can't be combined");
119e8d8bef9SDimitry Andric }
120e8d8bef9SDimitry Andric
12181ad6265SDimitry Andric llvm::Expected<std::unique_ptr<RISCVISAInfo>>
parseFeatureBits(bool IsRV64,const FeatureBitset & FeatureBits)12281ad6265SDimitry Andric parseFeatureBits(bool IsRV64, const FeatureBitset &FeatureBits) {
12381ad6265SDimitry Andric unsigned XLen = IsRV64 ? 64 : 32;
12481ad6265SDimitry Andric std::vector<std::string> FeatureVector;
12581ad6265SDimitry Andric // Convert FeatureBitset to FeatureVector.
126349cc55cSDimitry Andric for (auto Feature : RISCVFeatureKV) {
127349cc55cSDimitry Andric if (FeatureBits[Feature.Value] &&
128349cc55cSDimitry Andric llvm::RISCVISAInfo::isSupportedExtensionFeature(Feature.Key))
129349cc55cSDimitry Andric FeatureVector.push_back(std::string("+") + Feature.Key);
130349cc55cSDimitry Andric }
13181ad6265SDimitry Andric return llvm::RISCVISAInfo::parseFeatures(XLen, FeatureVector);
132349cc55cSDimitry Andric }
133349cc55cSDimitry Andric
134e8d8bef9SDimitry Andric } // namespace RISCVFeatures
135e8d8bef9SDimitry Andric
136bdd1243dSDimitry Andric // Include the auto-generated portion of the compress emitter.
137bdd1243dSDimitry Andric #define GEN_UNCOMPRESS_INSTR
138bdd1243dSDimitry Andric #define GEN_COMPRESS_INSTR
139bdd1243dSDimitry Andric #include "RISCVGenCompressInstEmitter.inc"
140bdd1243dSDimitry Andric
compress(MCInst & OutInst,const MCInst & MI,const MCSubtargetInfo & STI)141bdd1243dSDimitry Andric bool RISCVRVC::compress(MCInst &OutInst, const MCInst &MI,
142bdd1243dSDimitry Andric const MCSubtargetInfo &STI) {
143bdd1243dSDimitry Andric return compressInst(OutInst, MI, STI);
144bdd1243dSDimitry Andric }
145bdd1243dSDimitry Andric
uncompress(MCInst & OutInst,const MCInst & MI,const MCSubtargetInfo & STI)146bdd1243dSDimitry Andric bool RISCVRVC::uncompress(MCInst &OutInst, const MCInst &MI,
147bdd1243dSDimitry Andric const MCSubtargetInfo &STI) {
148bdd1243dSDimitry Andric return uncompressInst(OutInst, MI, STI);
149bdd1243dSDimitry Andric }
150bdd1243dSDimitry Andric
15106c3fb27SDimitry Andric // Lookup table for fli.s for entries 2-31.
15206c3fb27SDimitry Andric static constexpr std::pair<uint8_t, uint8_t> LoadFP32ImmArr[] = {
15306c3fb27SDimitry Andric {0b01101111, 0b00}, {0b01110000, 0b00}, {0b01110111, 0b00},
15406c3fb27SDimitry Andric {0b01111000, 0b00}, {0b01111011, 0b00}, {0b01111100, 0b00},
15506c3fb27SDimitry Andric {0b01111101, 0b00}, {0b01111101, 0b01}, {0b01111101, 0b10},
15606c3fb27SDimitry Andric {0b01111101, 0b11}, {0b01111110, 0b00}, {0b01111110, 0b01},
15706c3fb27SDimitry Andric {0b01111110, 0b10}, {0b01111110, 0b11}, {0b01111111, 0b00},
15806c3fb27SDimitry Andric {0b01111111, 0b01}, {0b01111111, 0b10}, {0b01111111, 0b11},
15906c3fb27SDimitry Andric {0b10000000, 0b00}, {0b10000000, 0b01}, {0b10000000, 0b10},
16006c3fb27SDimitry Andric {0b10000001, 0b00}, {0b10000010, 0b00}, {0b10000011, 0b00},
16106c3fb27SDimitry Andric {0b10000110, 0b00}, {0b10000111, 0b00}, {0b10001110, 0b00},
16206c3fb27SDimitry Andric {0b10001111, 0b00}, {0b11111111, 0b00}, {0b11111111, 0b10},
16306c3fb27SDimitry Andric };
16406c3fb27SDimitry Andric
getLoadFPImm(APFloat FPImm)16506c3fb27SDimitry Andric int RISCVLoadFPImm::getLoadFPImm(APFloat FPImm) {
16606c3fb27SDimitry Andric assert((&FPImm.getSemantics() == &APFloat::IEEEsingle() ||
16706c3fb27SDimitry Andric &FPImm.getSemantics() == &APFloat::IEEEdouble() ||
16806c3fb27SDimitry Andric &FPImm.getSemantics() == &APFloat::IEEEhalf()) &&
16906c3fb27SDimitry Andric "Unexpected semantics");
17006c3fb27SDimitry Andric
17106c3fb27SDimitry Andric // Handle the minimum normalized value which is different for each type.
1725f757f3fSDimitry Andric if (FPImm.isSmallestNormalized() && !FPImm.isNegative())
17306c3fb27SDimitry Andric return 1;
17406c3fb27SDimitry Andric
17506c3fb27SDimitry Andric // Convert to single precision to use its lookup table.
17606c3fb27SDimitry Andric bool LosesInfo;
17706c3fb27SDimitry Andric APFloat::opStatus Status = FPImm.convert(
17806c3fb27SDimitry Andric APFloat::IEEEsingle(), APFloat::rmNearestTiesToEven, &LosesInfo);
17906c3fb27SDimitry Andric if (Status != APFloat::opOK || LosesInfo)
18006c3fb27SDimitry Andric return -1;
18106c3fb27SDimitry Andric
18206c3fb27SDimitry Andric APInt Imm = FPImm.bitcastToAPInt();
18306c3fb27SDimitry Andric
18406c3fb27SDimitry Andric if (Imm.extractBitsAsZExtValue(21, 0) != 0)
18506c3fb27SDimitry Andric return -1;
18606c3fb27SDimitry Andric
18706c3fb27SDimitry Andric bool Sign = Imm.extractBitsAsZExtValue(1, 31);
18806c3fb27SDimitry Andric uint8_t Mantissa = Imm.extractBitsAsZExtValue(2, 21);
18906c3fb27SDimitry Andric uint8_t Exp = Imm.extractBitsAsZExtValue(8, 23);
19006c3fb27SDimitry Andric
19106c3fb27SDimitry Andric auto EMI = llvm::lower_bound(LoadFP32ImmArr, std::make_pair(Exp, Mantissa));
19206c3fb27SDimitry Andric if (EMI == std::end(LoadFP32ImmArr) || EMI->first != Exp ||
19306c3fb27SDimitry Andric EMI->second != Mantissa)
19406c3fb27SDimitry Andric return -1;
19506c3fb27SDimitry Andric
19606c3fb27SDimitry Andric // Table doesn't have entry 0 or 1.
19706c3fb27SDimitry Andric int Entry = std::distance(std::begin(LoadFP32ImmArr), EMI) + 2;
19806c3fb27SDimitry Andric
19906c3fb27SDimitry Andric // The only legal negative value is -1.0(entry 0). 1.0 is entry 16.
20006c3fb27SDimitry Andric if (Sign) {
20106c3fb27SDimitry Andric if (Entry == 16)
20206c3fb27SDimitry Andric return 0;
2035f757f3fSDimitry Andric return -1;
20406c3fb27SDimitry Andric }
20506c3fb27SDimitry Andric
20606c3fb27SDimitry Andric return Entry;
20706c3fb27SDimitry Andric }
20806c3fb27SDimitry Andric
getFPImm(unsigned Imm)20906c3fb27SDimitry Andric float RISCVLoadFPImm::getFPImm(unsigned Imm) {
21006c3fb27SDimitry Andric assert(Imm != 1 && Imm != 30 && Imm != 31 && "Unsupported immediate");
21106c3fb27SDimitry Andric
21206c3fb27SDimitry Andric // Entry 0 is -1.0, the only negative value. Entry 16 is 1.0.
21306c3fb27SDimitry Andric uint32_t Sign = 0;
21406c3fb27SDimitry Andric if (Imm == 0) {
21506c3fb27SDimitry Andric Sign = 0b1;
21606c3fb27SDimitry Andric Imm = 16;
21706c3fb27SDimitry Andric }
21806c3fb27SDimitry Andric
21906c3fb27SDimitry Andric uint32_t Exp = LoadFP32ImmArr[Imm - 2].first;
22006c3fb27SDimitry Andric uint32_t Mantissa = LoadFP32ImmArr[Imm - 2].second;
22106c3fb27SDimitry Andric
22206c3fb27SDimitry Andric uint32_t I = Sign << 31 | Exp << 23 | Mantissa << 21;
22306c3fb27SDimitry Andric return bit_cast<float>(I);
22406c3fb27SDimitry Andric }
22506c3fb27SDimitry Andric
printRlist(unsigned SlistEncode,raw_ostream & OS)22606c3fb27SDimitry Andric void RISCVZC::printRlist(unsigned SlistEncode, raw_ostream &OS) {
22706c3fb27SDimitry Andric OS << "{ra";
22806c3fb27SDimitry Andric if (SlistEncode > 4) {
22906c3fb27SDimitry Andric OS << ", s0";
23006c3fb27SDimitry Andric if (SlistEncode == 15)
23106c3fb27SDimitry Andric OS << "-s11";
23206c3fb27SDimitry Andric else if (SlistEncode > 5 && SlistEncode <= 14)
23306c3fb27SDimitry Andric OS << "-s" << (SlistEncode - 5);
23406c3fb27SDimitry Andric }
23506c3fb27SDimitry Andric OS << "}";
23606c3fb27SDimitry Andric }
23706c3fb27SDimitry Andric
238e8d8bef9SDimitry Andric } // namespace llvm
239