xref: /llvm-project/clang/lib/Basic/Targets/CSKY.cpp (revision 34dd8ec8aec17965313b17308faaa6272c235c53)
197e49605SZi Xuan Wu //===--- CSKY.cpp - Implement CSKY target feature support -----------------===//
297e49605SZi Xuan Wu //
397e49605SZi Xuan Wu // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
497e49605SZi Xuan Wu // See https://llvm.org/LICENSE.txt for license information.
597e49605SZi Xuan Wu // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
697e49605SZi Xuan Wu //
797e49605SZi Xuan Wu //===----------------------------------------------------------------------===//
897e49605SZi Xuan Wu //
997e49605SZi Xuan Wu // This file implements CSKY TargetInfo objects.
1097e49605SZi Xuan Wu //
1197e49605SZi Xuan Wu //===----------------------------------------------------------------------===//
1297e49605SZi Xuan Wu 
1397e49605SZi Xuan Wu #include "CSKY.h"
1497e49605SZi Xuan Wu 
1597e49605SZi Xuan Wu using namespace clang;
1697e49605SZi Xuan Wu using namespace clang::targets;
1797e49605SZi Xuan Wu 
isValidCPUName(StringRef Name) const1897e49605SZi Xuan Wu bool CSKYTargetInfo::isValidCPUName(StringRef Name) const {
1997e49605SZi Xuan Wu   return llvm::CSKY::parseCPUArch(Name) != llvm::CSKY::ArchKind::INVALID;
2097e49605SZi Xuan Wu }
2197e49605SZi Xuan Wu 
setCPU(const std::string & Name)2297e49605SZi Xuan Wu bool CSKYTargetInfo::setCPU(const std::string &Name) {
2397e49605SZi Xuan Wu   llvm::CSKY::ArchKind archKind = llvm::CSKY::parseCPUArch(Name);
2497e49605SZi Xuan Wu   bool isValid = (archKind != llvm::CSKY::ArchKind::INVALID);
2597e49605SZi Xuan Wu 
2697e49605SZi Xuan Wu   if (isValid) {
2797e49605SZi Xuan Wu     CPU = Name;
2897e49605SZi Xuan Wu     Arch = archKind;
2997e49605SZi Xuan Wu   }
3097e49605SZi Xuan Wu 
3197e49605SZi Xuan Wu   return isValid;
3297e49605SZi Xuan Wu }
3397e49605SZi Xuan Wu 
getTargetDefines(const LangOptions & Opts,MacroBuilder & Builder) const3497e49605SZi Xuan Wu void CSKYTargetInfo::getTargetDefines(const LangOptions &Opts,
3597e49605SZi Xuan Wu                                       MacroBuilder &Builder) const {
3697e49605SZi Xuan Wu   Builder.defineMacro("__csky__", "2");
3797e49605SZi Xuan Wu   Builder.defineMacro("__CSKY__", "2");
3897e49605SZi Xuan Wu   Builder.defineMacro("__ckcore__", "2");
3997e49605SZi Xuan Wu   Builder.defineMacro("__CKCORE__", "2");
4097e49605SZi Xuan Wu 
4197e49605SZi Xuan Wu   Builder.defineMacro("__CSKYABI__", ABI == "abiv2" ? "2" : "1");
4297e49605SZi Xuan Wu   Builder.defineMacro("__cskyabi__", ABI == "abiv2" ? "2" : "1");
4397e49605SZi Xuan Wu 
4497e49605SZi Xuan Wu   StringRef ArchName = "ck810";
4597e49605SZi Xuan Wu   StringRef CPUName = "ck810";
4697e49605SZi Xuan Wu 
4797e49605SZi Xuan Wu   if (Arch != llvm::CSKY::ArchKind::INVALID) {
4897e49605SZi Xuan Wu     ArchName = llvm::CSKY::getArchName(Arch);
4997e49605SZi Xuan Wu     CPUName = CPU;
5097e49605SZi Xuan Wu   }
5197e49605SZi Xuan Wu 
5297e49605SZi Xuan Wu   Builder.defineMacro("__" + ArchName.upper() + "__");
5397e49605SZi Xuan Wu   Builder.defineMacro("__" + ArchName.lower() + "__");
5478bf8a0aSJohn Brawn   if (ArchName != CPUName) {
5597e49605SZi Xuan Wu     Builder.defineMacro("__" + CPUName.upper() + "__");
5697e49605SZi Xuan Wu     Builder.defineMacro("__" + CPUName.lower() + "__");
5778bf8a0aSJohn Brawn   }
5897e49605SZi Xuan Wu 
5997e49605SZi Xuan Wu   // TODO: Add support for BE if BE was supported later
6097e49605SZi Xuan Wu   StringRef endian = "__cskyLE__";
6197e49605SZi Xuan Wu 
6297e49605SZi Xuan Wu   Builder.defineMacro(endian);
6397e49605SZi Xuan Wu   Builder.defineMacro(endian.upper());
6497e49605SZi Xuan Wu   Builder.defineMacro(endian.lower());
6597e49605SZi Xuan Wu 
6697e49605SZi Xuan Wu   if (DSPV2) {
6797e49605SZi Xuan Wu     StringRef dspv2 = "__CSKY_DSPV2__";
6897e49605SZi Xuan Wu     Builder.defineMacro(dspv2);
6997e49605SZi Xuan Wu     Builder.defineMacro(dspv2.lower());
7097e49605SZi Xuan Wu   }
7197e49605SZi Xuan Wu 
7297e49605SZi Xuan Wu   if (VDSPV2) {
7397e49605SZi Xuan Wu     StringRef vdspv2 = "__CSKY_VDSPV2__";
7497e49605SZi Xuan Wu     Builder.defineMacro(vdspv2);
7597e49605SZi Xuan Wu     Builder.defineMacro(vdspv2.lower());
7697e49605SZi Xuan Wu 
7797e49605SZi Xuan Wu     if (HardFloat) {
7897e49605SZi Xuan Wu       StringRef vdspv2_f = "__CSKY_VDSPV2_F__";
7997e49605SZi Xuan Wu       Builder.defineMacro(vdspv2_f);
8097e49605SZi Xuan Wu       Builder.defineMacro(vdspv2_f.lower());
8197e49605SZi Xuan Wu     }
8297e49605SZi Xuan Wu   }
8397e49605SZi Xuan Wu   if (VDSPV1) {
8497e49605SZi Xuan Wu     StringRef vdspv1_64 = "__CSKY_VDSP64__";
8597e49605SZi Xuan Wu     StringRef vdspv1_128 = "__CSKY_VDSP128__";
8697e49605SZi Xuan Wu 
8797e49605SZi Xuan Wu     Builder.defineMacro(vdspv1_64);
8897e49605SZi Xuan Wu     Builder.defineMacro(vdspv1_64.lower());
8997e49605SZi Xuan Wu     Builder.defineMacro(vdspv1_128);
9097e49605SZi Xuan Wu     Builder.defineMacro(vdspv1_128.lower());
9197e49605SZi Xuan Wu   }
9297e49605SZi Xuan Wu   if (is3E3R1) {
9397e49605SZi Xuan Wu     StringRef is3e3r1 = "__CSKY_3E3R1__";
9497e49605SZi Xuan Wu     Builder.defineMacro(is3e3r1);
9597e49605SZi Xuan Wu     Builder.defineMacro(is3e3r1.lower());
9697e49605SZi Xuan Wu   }
9797e49605SZi Xuan Wu }
9897e49605SZi Xuan Wu 
hasFeature(StringRef Feature) const99b86440ecSZi Xuan Wu (Zeson) bool CSKYTargetInfo::hasFeature(StringRef Feature) const {
100b86440ecSZi Xuan Wu (Zeson)   return llvm::StringSwitch<bool>(Feature)
101b86440ecSZi Xuan Wu (Zeson)       .Case("hard-float", HardFloat)
102b86440ecSZi Xuan Wu (Zeson)       .Case("hard-float-abi", HardFloatABI)
103b86440ecSZi Xuan Wu (Zeson)       .Case("fpuv2_sf", FPUV2_SF)
104b86440ecSZi Xuan Wu (Zeson)       .Case("fpuv2_df", FPUV2_DF)
105b86440ecSZi Xuan Wu (Zeson)       .Case("fpuv3_sf", FPUV3_SF)
106b86440ecSZi Xuan Wu (Zeson)       .Case("fpuv3_df", FPUV3_DF)
107b86440ecSZi Xuan Wu (Zeson)       .Case("vdspv2", VDSPV2)
108b86440ecSZi Xuan Wu (Zeson)       .Case("dspv2", DSPV2)
109b86440ecSZi Xuan Wu (Zeson)       .Case("vdspv1", VDSPV1)
110b86440ecSZi Xuan Wu (Zeson)       .Case("3e3r1", is3E3R1)
111b86440ecSZi Xuan Wu (Zeson)       .Default(false);
112b86440ecSZi Xuan Wu (Zeson) }
113b86440ecSZi Xuan Wu (Zeson) 
handleTargetFeatures(std::vector<std::string> & Features,DiagnosticsEngine & Diags)11497e49605SZi Xuan Wu bool CSKYTargetInfo::handleTargetFeatures(std::vector<std::string> &Features,
11597e49605SZi Xuan Wu                                           DiagnosticsEngine &Diags) {
11697e49605SZi Xuan Wu   for (const auto &Feature : Features) {
11797e49605SZi Xuan Wu     if (Feature == "+hard-float")
11897e49605SZi Xuan Wu       HardFloat = true;
119b86440ecSZi Xuan Wu (Zeson)     if (Feature == "+hard-float-abi")
120b86440ecSZi Xuan Wu (Zeson)       HardFloatABI = true;
121b86440ecSZi Xuan Wu (Zeson)     if (Feature == "+fpuv2_sf")
122b86440ecSZi Xuan Wu (Zeson)       FPUV2_SF = true;
123b86440ecSZi Xuan Wu (Zeson)     if (Feature == "+fpuv2_df")
124b86440ecSZi Xuan Wu (Zeson)       FPUV2_DF = true;
125b86440ecSZi Xuan Wu (Zeson)     if (Feature == "+fpuv3_sf")
126b86440ecSZi Xuan Wu (Zeson)       FPUV3_SF = true;
127b86440ecSZi Xuan Wu (Zeson)     if (Feature == "+fpuv3_df")
128b86440ecSZi Xuan Wu (Zeson)       FPUV3_DF = true;
12997e49605SZi Xuan Wu     if (Feature == "+vdspv2")
13097e49605SZi Xuan Wu       VDSPV2 = true;
13197e49605SZi Xuan Wu     if (Feature == "+dspv2")
13297e49605SZi Xuan Wu       DSPV2 = true;
13397e49605SZi Xuan Wu     if (Feature == "+vdspv1")
13497e49605SZi Xuan Wu       VDSPV1 = true;
13597e49605SZi Xuan Wu     if (Feature == "+3e3r1")
13697e49605SZi Xuan Wu       is3E3R1 = true;
13797e49605SZi Xuan Wu   }
13897e49605SZi Xuan Wu 
13997e49605SZi Xuan Wu   return true;
14097e49605SZi Xuan Wu }
14197e49605SZi Xuan Wu 
getTargetBuiltins() const14297e49605SZi Xuan Wu ArrayRef<Builtin::Info> CSKYTargetInfo::getTargetBuiltins() const {
14397e49605SZi Xuan Wu   return ArrayRef<Builtin::Info>();
14497e49605SZi Xuan Wu }
14597e49605SZi Xuan Wu 
getGCCRegNames() const14697e49605SZi Xuan Wu ArrayRef<const char *> CSKYTargetInfo::getGCCRegNames() const {
14797e49605SZi Xuan Wu   static const char *const GCCRegNames[] = {
14897e49605SZi Xuan Wu       // Integer registers
14997e49605SZi Xuan Wu       "r0",
15097e49605SZi Xuan Wu       "r1",
15197e49605SZi Xuan Wu       "r2",
15297e49605SZi Xuan Wu       "r3",
15397e49605SZi Xuan Wu       "r4",
15497e49605SZi Xuan Wu       "r5",
15597e49605SZi Xuan Wu       "r6",
15697e49605SZi Xuan Wu       "r7",
15797e49605SZi Xuan Wu       "r8",
15897e49605SZi Xuan Wu       "r9",
15997e49605SZi Xuan Wu       "r10",
16097e49605SZi Xuan Wu       "r11",
16197e49605SZi Xuan Wu       "r12",
16297e49605SZi Xuan Wu       "r13",
16397e49605SZi Xuan Wu       "r14",
16497e49605SZi Xuan Wu       "r15",
16597e49605SZi Xuan Wu       "r16",
16697e49605SZi Xuan Wu       "r17",
16797e49605SZi Xuan Wu       "r18",
16897e49605SZi Xuan Wu       "r19",
16997e49605SZi Xuan Wu       "r20",
17097e49605SZi Xuan Wu       "r21",
17197e49605SZi Xuan Wu       "r22",
17297e49605SZi Xuan Wu       "r23",
17397e49605SZi Xuan Wu       "r24",
17497e49605SZi Xuan Wu       "r25",
17597e49605SZi Xuan Wu       "r26",
17697e49605SZi Xuan Wu       "r27",
17797e49605SZi Xuan Wu       "r28",
17897e49605SZi Xuan Wu       "r29",
17997e49605SZi Xuan Wu       "r30",
18097e49605SZi Xuan Wu       "r31",
18197e49605SZi Xuan Wu 
18297e49605SZi Xuan Wu       // Floating point registers
18397e49605SZi Xuan Wu       "fr0",
18497e49605SZi Xuan Wu       "fr1",
18597e49605SZi Xuan Wu       "fr2",
18697e49605SZi Xuan Wu       "fr3",
18797e49605SZi Xuan Wu       "fr4",
18897e49605SZi Xuan Wu       "fr5",
18997e49605SZi Xuan Wu       "fr6",
19097e49605SZi Xuan Wu       "fr7",
19197e49605SZi Xuan Wu       "fr8",
19297e49605SZi Xuan Wu       "fr9",
19397e49605SZi Xuan Wu       "fr10",
19497e49605SZi Xuan Wu       "fr11",
19597e49605SZi Xuan Wu       "fr12",
19697e49605SZi Xuan Wu       "fr13",
19797e49605SZi Xuan Wu       "fr14",
19897e49605SZi Xuan Wu       "fr15",
19997e49605SZi Xuan Wu       "fr16",
20097e49605SZi Xuan Wu       "fr17",
20197e49605SZi Xuan Wu       "fr18",
20297e49605SZi Xuan Wu       "fr19",
20397e49605SZi Xuan Wu       "fr20",
20497e49605SZi Xuan Wu       "fr21",
20597e49605SZi Xuan Wu       "fr22",
20697e49605SZi Xuan Wu       "fr23",
20797e49605SZi Xuan Wu       "fr24",
20897e49605SZi Xuan Wu       "fr25",
20997e49605SZi Xuan Wu       "fr26",
21097e49605SZi Xuan Wu       "fr27",
21197e49605SZi Xuan Wu       "fr28",
21297e49605SZi Xuan Wu       "fr29",
21397e49605SZi Xuan Wu       "fr30",
21497e49605SZi Xuan Wu       "fr31",
21597e49605SZi Xuan Wu 
21697e49605SZi Xuan Wu   };
217a3c248dbSserge-sans-paille   return llvm::ArrayRef(GCCRegNames);
21897e49605SZi Xuan Wu }
21997e49605SZi Xuan Wu 
getGCCRegAliases() const22097e49605SZi Xuan Wu ArrayRef<TargetInfo::GCCRegAlias> CSKYTargetInfo::getGCCRegAliases() const {
22197e49605SZi Xuan Wu   static const TargetInfo::GCCRegAlias GCCRegAliases[] = {
22297e49605SZi Xuan Wu       {{"a0"}, "r0"},
22397e49605SZi Xuan Wu       {{"a1"}, "r1"},
22497e49605SZi Xuan Wu       {{"a2"}, "r2"},
22597e49605SZi Xuan Wu       {{"a3"}, "r3"},
22697e49605SZi Xuan Wu       {{"l0"}, "r4"},
22797e49605SZi Xuan Wu       {{"l1"}, "r5"},
22897e49605SZi Xuan Wu       {{"l2"}, "r6"},
22997e49605SZi Xuan Wu       {{"l3"}, "r7"},
23097e49605SZi Xuan Wu       {{"l4"}, "r8"},
23197e49605SZi Xuan Wu       {{"l5"}, "r9"},
23297e49605SZi Xuan Wu       {{"l6"}, "r10"},
23397e49605SZi Xuan Wu       {{"l7"}, "r11"},
23497e49605SZi Xuan Wu       {{"t0"}, "r12"},
23597e49605SZi Xuan Wu       {{"t1"}, "r13"},
23697e49605SZi Xuan Wu       {{"sp"}, "r14"},
23797e49605SZi Xuan Wu       {{"lr"}, "r15"},
23897e49605SZi Xuan Wu       {{"l8"}, "r16"},
23997e49605SZi Xuan Wu       {{"l9"}, "r17"},
24097e49605SZi Xuan Wu       {{"t2"}, "r18"},
24197e49605SZi Xuan Wu       {{"t3"}, "r19"},
24297e49605SZi Xuan Wu       {{"t4"}, "r20"},
24397e49605SZi Xuan Wu       {{"t5"}, "r21"},
24497e49605SZi Xuan Wu       {{"t6"}, "r22"},
24597e49605SZi Xuan Wu       {{"t7", "fp"}, "r23"},
24697e49605SZi Xuan Wu       {{"t8", "top"}, "r24"},
24797e49605SZi Xuan Wu       {{"t9", "bsp"}, "r25"},
24897e49605SZi Xuan Wu       {{"r26"}, "r26"},
24997e49605SZi Xuan Wu       {{"r27"}, "r27"},
25097e49605SZi Xuan Wu       {{"gb", "rgb", "rdb"}, "r28"},
25197e49605SZi Xuan Wu       {{"tb", "rtb"}, "r29"},
25297e49605SZi Xuan Wu       {{"svbr"}, "r30"},
25397e49605SZi Xuan Wu       {{"tls"}, "r31"},
25497e49605SZi Xuan Wu 
25597e49605SZi Xuan Wu       {{"vr0"}, "fr0"},
25697e49605SZi Xuan Wu       {{"vr1"}, "fr1"},
25797e49605SZi Xuan Wu       {{"vr2"}, "fr2"},
25897e49605SZi Xuan Wu       {{"vr3"}, "fr3"},
25997e49605SZi Xuan Wu       {{"vr4"}, "fr4"},
26097e49605SZi Xuan Wu       {{"vr5"}, "fr5"},
26197e49605SZi Xuan Wu       {{"vr6"}, "fr6"},
26297e49605SZi Xuan Wu       {{"vr7"}, "fr7"},
26397e49605SZi Xuan Wu       {{"vr8"}, "fr8"},
26497e49605SZi Xuan Wu       {{"vr9"}, "fr9"},
26597e49605SZi Xuan Wu       {{"vr10"}, "fr10"},
26697e49605SZi Xuan Wu       {{"vr11"}, "fr11"},
26797e49605SZi Xuan Wu       {{"vr12"}, "fr12"},
26897e49605SZi Xuan Wu       {{"vr13"}, "fr13"},
26997e49605SZi Xuan Wu       {{"vr14"}, "fr14"},
27097e49605SZi Xuan Wu       {{"vr15"}, "fr15"},
27197e49605SZi Xuan Wu       {{"vr16"}, "fr16"},
27297e49605SZi Xuan Wu       {{"vr17"}, "fr17"},
27397e49605SZi Xuan Wu       {{"vr18"}, "fr18"},
27497e49605SZi Xuan Wu       {{"vr19"}, "fr19"},
27597e49605SZi Xuan Wu       {{"vr20"}, "fr20"},
27697e49605SZi Xuan Wu       {{"vr21"}, "fr21"},
27797e49605SZi Xuan Wu       {{"vr22"}, "fr22"},
27897e49605SZi Xuan Wu       {{"vr23"}, "fr23"},
27997e49605SZi Xuan Wu       {{"vr24"}, "fr24"},
28097e49605SZi Xuan Wu       {{"vr25"}, "fr25"},
28197e49605SZi Xuan Wu       {{"vr26"}, "fr26"},
28297e49605SZi Xuan Wu       {{"vr27"}, "fr27"},
28397e49605SZi Xuan Wu       {{"vr28"}, "fr28"},
28497e49605SZi Xuan Wu       {{"vr29"}, "fr29"},
28597e49605SZi Xuan Wu       {{"vr30"}, "fr30"},
28697e49605SZi Xuan Wu       {{"vr31"}, "fr31"},
28797e49605SZi Xuan Wu 
28897e49605SZi Xuan Wu   };
289a3c248dbSserge-sans-paille   return llvm::ArrayRef(GCCRegAliases);
29097e49605SZi Xuan Wu }
29197e49605SZi Xuan Wu 
validateAsmConstraint(const char * & Name,TargetInfo::ConstraintInfo & Info) const29297e49605SZi Xuan Wu bool CSKYTargetInfo::validateAsmConstraint(
29397e49605SZi Xuan Wu     const char *&Name, TargetInfo::ConstraintInfo &Info) const {
29497e49605SZi Xuan Wu   switch (*Name) {
29597e49605SZi Xuan Wu   default:
29697e49605SZi Xuan Wu     return false;
29797e49605SZi Xuan Wu   case 'a':
29897e49605SZi Xuan Wu   case 'b':
29997e49605SZi Xuan Wu   case 'c':
30097e49605SZi Xuan Wu   case 'y':
30197e49605SZi Xuan Wu   case 'l':
30297e49605SZi Xuan Wu   case 'h':
30397e49605SZi Xuan Wu   case 'w':
30497e49605SZi Xuan Wu   case 'v': // A floating-point and vector register.
30597e49605SZi Xuan Wu   case 'z':
30697e49605SZi Xuan Wu     Info.setAllowsRegister();
30797e49605SZi Xuan Wu     return true;
30897e49605SZi Xuan Wu   }
30997e49605SZi Xuan Wu }
31097e49605SZi Xuan Wu 
getMinGlobalAlign(uint64_t Size,bool HasNonWeakDef) const311*34dd8ec8SJonas Paulsson unsigned CSKYTargetInfo::getMinGlobalAlign(uint64_t Size,
312*34dd8ec8SJonas Paulsson                                            bool HasNonWeakDef) const {
31397e49605SZi Xuan Wu   if (Size >= 32)
31497e49605SZi Xuan Wu     return 32;
31597e49605SZi Xuan Wu   return 0;
31697e49605SZi Xuan Wu }
317