1 //===--- RISCV.cpp - Implement RISCV target feature support ---------------===// 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 // This file implements RISCV TargetInfo objects. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "RISCV.h" 15 #include "clang/Basic/MacroBuilder.h" 16 #include "llvm/ADT/StringSwitch.h" 17 18 using namespace clang; 19 using namespace clang::targets; 20 21 ArrayRef<const char *> RISCVTargetInfo::getGCCRegNames() const { 22 static const char *const GCCRegNames[] = { 23 "x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7", 24 "x8", "x9", "x10", "x11", "x12", "x13", "x14", "x15", 25 "x16", "x17", "x18", "x19", "x20", "x21", "x22", "x23", 26 "x24", "x25", "x26", "x27", "x28", "x29", "x30", "x31"}; 27 return llvm::makeArrayRef(GCCRegNames); 28 } 29 30 ArrayRef<TargetInfo::GCCRegAlias> RISCVTargetInfo::getGCCRegAliases() const { 31 static const TargetInfo::GCCRegAlias GCCRegAliases[] = { 32 {{"zero"}, "x0"}, {{"ra"}, "x1"}, {{"sp"}, "x2"}, {{"gp"}, "x3"}, 33 {{"tp"}, "x4"}, {{"t0"}, "x5"}, {{"t1"}, "x6"}, {{"t2"}, "x7"}, 34 {{"s0"}, "x8"}, {{"s1"}, "x9"}, {{"a0"}, "x10"}, {{"a1"}, "x11"}, 35 {{"a2"}, "x12"}, {{"a3"}, "x13"}, {{"a4"}, "x15"}, {{"a5"}, "x15"}, 36 {{"a6"}, "x16"}, {{"a7"}, "x17"}, {{"s2"}, "x18"}, {{"s3"}, "x19"}, 37 {{"s4"}, "x20"}, {{"s5"}, "x21"}, {{"s6"}, "x22"}, {{"s7"}, "x23"}, 38 {{"s8"}, "x24"}, {{"s9"}, "x25"}, {{"s10"}, "x26"}, {{"s11"}, "x27"}, 39 {{"t3"}, "x28"}, {{"t4"}, "x29"}, {{"t5"}, "x30"}, {{"t6"}, "x31"}}; 40 return llvm::makeArrayRef(GCCRegAliases); 41 } 42 43 void RISCVTargetInfo::getTargetDefines(const LangOptions &Opts, 44 MacroBuilder &Builder) const { 45 Builder.defineMacro("__ELF__"); 46 Builder.defineMacro("__riscv"); 47 bool Is64Bit = getTriple().getArch() == llvm::Triple::riscv64; 48 Builder.defineMacro("__riscv_xlen", Is64Bit ? "64" : "32"); 49 // TODO: modify when more code models and ABIs are supported. 50 Builder.defineMacro("__riscv_cmodel_medlow"); 51 Builder.defineMacro("__riscv_float_abi_soft"); 52 53 if (HasM) { 54 Builder.defineMacro("__riscv_mul"); 55 Builder.defineMacro("__riscv_div"); 56 Builder.defineMacro("__riscv_muldiv"); 57 } 58 59 if (HasA) 60 Builder.defineMacro("__riscv_atomic"); 61 62 if (HasF || HasD) { 63 Builder.defineMacro("__riscv_flen", HasD ? "64" : "32"); 64 Builder.defineMacro("__riscv_fdiv"); 65 Builder.defineMacro("__riscv_fsqrt"); 66 } 67 68 if (HasC) 69 Builder.defineMacro("__riscv_compressed"); 70 } 71 72 /// Return true if has this feature, need to sync with handleTargetFeatures. 73 bool RISCVTargetInfo::hasFeature(StringRef Feature) const { 74 bool Is64Bit = getTriple().getArch() == llvm::Triple::riscv64; 75 return llvm::StringSwitch<bool>(Feature) 76 .Case("riscv", true) 77 .Case("riscv32", !Is64Bit) 78 .Case("riscv64", Is64Bit) 79 .Case("m", HasM) 80 .Case("a", HasA) 81 .Case("f", HasF) 82 .Case("d", HasD) 83 .Case("c", HasC) 84 .Default(false); 85 } 86 87 /// Perform initialization based on the user configured set of features. 88 bool RISCVTargetInfo::handleTargetFeatures(std::vector<std::string> &Features, 89 DiagnosticsEngine &Diags) { 90 for (const auto &Feature : Features) { 91 if (Feature == "+m") 92 HasM = true; 93 else if (Feature == "+a") 94 HasA = true; 95 else if (Feature == "+f") 96 HasF = true; 97 else if (Feature == "+d") 98 HasD = true; 99 else if (Feature == "+c") 100 HasC = true; 101 } 102 103 return true; 104 } 105