10b57cec5SDimitry Andric //===--- Mips.cpp - Implement Mips target feature support -----------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file implements Mips TargetInfo objects.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric
130b57cec5SDimitry Andric #include "Mips.h"
140b57cec5SDimitry Andric #include "Targets.h"
150b57cec5SDimitry Andric #include "clang/Basic/Diagnostic.h"
160b57cec5SDimitry Andric #include "clang/Basic/MacroBuilder.h"
170b57cec5SDimitry Andric #include "clang/Basic/TargetBuiltins.h"
180b57cec5SDimitry Andric #include "llvm/ADT/StringSwitch.h"
190b57cec5SDimitry Andric
200b57cec5SDimitry Andric using namespace clang;
210b57cec5SDimitry Andric using namespace clang::targets;
220b57cec5SDimitry Andric
23bdd1243dSDimitry Andric static constexpr Builtin::Info BuiltinInfo[] = {
240b57cec5SDimitry Andric #define BUILTIN(ID, TYPE, ATTRS) \
25bdd1243dSDimitry Andric {#ID, TYPE, ATTRS, nullptr, HeaderDesc::NO_HEADER, ALL_LANGUAGES},
260b57cec5SDimitry Andric #define LIBBUILTIN(ID, TYPE, ATTRS, HEADER) \
27bdd1243dSDimitry Andric {#ID, TYPE, ATTRS, nullptr, HeaderDesc::HEADER, ALL_LANGUAGES},
280b57cec5SDimitry Andric #include "clang/Basic/BuiltinsMips.def"
290b57cec5SDimitry Andric };
300b57cec5SDimitry Andric
processorSupportsGPR64() const310b57cec5SDimitry Andric bool MipsTargetInfo::processorSupportsGPR64() const {
320b57cec5SDimitry Andric return llvm::StringSwitch<bool>(CPU)
330b57cec5SDimitry Andric .Case("mips3", true)
340b57cec5SDimitry Andric .Case("mips4", true)
350b57cec5SDimitry Andric .Case("mips5", true)
360b57cec5SDimitry Andric .Case("mips64", true)
370b57cec5SDimitry Andric .Case("mips64r2", true)
380b57cec5SDimitry Andric .Case("mips64r3", true)
390b57cec5SDimitry Andric .Case("mips64r5", true)
400b57cec5SDimitry Andric .Case("mips64r6", true)
410b57cec5SDimitry Andric .Case("octeon", true)
420b57cec5SDimitry Andric .Case("octeon+", true)
430b57cec5SDimitry Andric .Default(false);
440b57cec5SDimitry Andric }
450b57cec5SDimitry Andric
460b57cec5SDimitry Andric static constexpr llvm::StringLiteral ValidCPUNames[] = {
470b57cec5SDimitry Andric {"mips1"}, {"mips2"}, {"mips3"}, {"mips4"}, {"mips5"},
480b57cec5SDimitry Andric {"mips32"}, {"mips32r2"}, {"mips32r3"}, {"mips32r5"}, {"mips32r6"},
490b57cec5SDimitry Andric {"mips64"}, {"mips64r2"}, {"mips64r3"}, {"mips64r5"}, {"mips64r6"},
500b57cec5SDimitry Andric {"octeon"}, {"octeon+"}, {"p5600"}};
510b57cec5SDimitry Andric
isValidCPUName(StringRef Name) const520b57cec5SDimitry Andric bool MipsTargetInfo::isValidCPUName(StringRef Name) const {
53349cc55cSDimitry Andric return llvm::is_contained(ValidCPUNames, Name);
540b57cec5SDimitry Andric }
550b57cec5SDimitry Andric
fillValidCPUList(SmallVectorImpl<StringRef> & Values) const560b57cec5SDimitry Andric void MipsTargetInfo::fillValidCPUList(
570b57cec5SDimitry Andric SmallVectorImpl<StringRef> &Values) const {
580b57cec5SDimitry Andric Values.append(std::begin(ValidCPUNames), std::end(ValidCPUNames));
590b57cec5SDimitry Andric }
600b57cec5SDimitry Andric
getISARev() const610b57cec5SDimitry Andric unsigned MipsTargetInfo::getISARev() const {
620b57cec5SDimitry Andric return llvm::StringSwitch<unsigned>(getCPU())
630b57cec5SDimitry Andric .Cases("mips32", "mips64", 1)
640b57cec5SDimitry Andric .Cases("mips32r2", "mips64r2", "octeon", "octeon+", 2)
650b57cec5SDimitry Andric .Cases("mips32r3", "mips64r3", 3)
660b57cec5SDimitry Andric .Cases("mips32r5", "mips64r5", 5)
670b57cec5SDimitry Andric .Cases("mips32r6", "mips64r6", 6)
680b57cec5SDimitry Andric .Default(0);
690b57cec5SDimitry Andric }
700b57cec5SDimitry Andric
getTargetDefines(const LangOptions & Opts,MacroBuilder & Builder) const710b57cec5SDimitry Andric void MipsTargetInfo::getTargetDefines(const LangOptions &Opts,
720b57cec5SDimitry Andric MacroBuilder &Builder) const {
730b57cec5SDimitry Andric if (BigEndian) {
740b57cec5SDimitry Andric DefineStd(Builder, "MIPSEB", Opts);
750b57cec5SDimitry Andric Builder.defineMacro("_MIPSEB");
760b57cec5SDimitry Andric } else {
770b57cec5SDimitry Andric DefineStd(Builder, "MIPSEL", Opts);
780b57cec5SDimitry Andric Builder.defineMacro("_MIPSEL");
790b57cec5SDimitry Andric }
800b57cec5SDimitry Andric
810b57cec5SDimitry Andric Builder.defineMacro("__mips__");
820b57cec5SDimitry Andric Builder.defineMacro("_mips");
830b57cec5SDimitry Andric if (Opts.GNUMode)
840b57cec5SDimitry Andric Builder.defineMacro("mips");
850b57cec5SDimitry Andric
860b57cec5SDimitry Andric if (ABI == "o32") {
870b57cec5SDimitry Andric Builder.defineMacro("__mips", "32");
880b57cec5SDimitry Andric Builder.defineMacro("_MIPS_ISA", "_MIPS_ISA_MIPS32");
890b57cec5SDimitry Andric } else {
900b57cec5SDimitry Andric Builder.defineMacro("__mips", "64");
910b57cec5SDimitry Andric Builder.defineMacro("__mips64");
920b57cec5SDimitry Andric Builder.defineMacro("__mips64__");
930b57cec5SDimitry Andric Builder.defineMacro("_MIPS_ISA", "_MIPS_ISA_MIPS64");
940b57cec5SDimitry Andric }
950b57cec5SDimitry Andric
960b57cec5SDimitry Andric const std::string ISARev = std::to_string(getISARev());
970b57cec5SDimitry Andric
980b57cec5SDimitry Andric if (!ISARev.empty())
990b57cec5SDimitry Andric Builder.defineMacro("__mips_isa_rev", ISARev);
1000b57cec5SDimitry Andric
1010b57cec5SDimitry Andric if (ABI == "o32") {
1020b57cec5SDimitry Andric Builder.defineMacro("__mips_o32");
1030b57cec5SDimitry Andric Builder.defineMacro("_ABIO32", "1");
1040b57cec5SDimitry Andric Builder.defineMacro("_MIPS_SIM", "_ABIO32");
1050b57cec5SDimitry Andric } else if (ABI == "n32") {
1060b57cec5SDimitry Andric Builder.defineMacro("__mips_n32");
1070b57cec5SDimitry Andric Builder.defineMacro("_ABIN32", "2");
1080b57cec5SDimitry Andric Builder.defineMacro("_MIPS_SIM", "_ABIN32");
1090b57cec5SDimitry Andric } else if (ABI == "n64") {
1100b57cec5SDimitry Andric Builder.defineMacro("__mips_n64");
1110b57cec5SDimitry Andric Builder.defineMacro("_ABI64", "3");
1120b57cec5SDimitry Andric Builder.defineMacro("_MIPS_SIM", "_ABI64");
1130b57cec5SDimitry Andric } else
1140b57cec5SDimitry Andric llvm_unreachable("Invalid ABI.");
1150b57cec5SDimitry Andric
1160b57cec5SDimitry Andric if (!IsNoABICalls) {
1170b57cec5SDimitry Andric Builder.defineMacro("__mips_abicalls");
1180b57cec5SDimitry Andric if (CanUseBSDABICalls)
1190b57cec5SDimitry Andric Builder.defineMacro("__ABICALLS__");
1200b57cec5SDimitry Andric }
1210b57cec5SDimitry Andric
1220b57cec5SDimitry Andric Builder.defineMacro("__REGISTER_PREFIX__", "");
1230b57cec5SDimitry Andric
1240b57cec5SDimitry Andric switch (FloatABI) {
1250b57cec5SDimitry Andric case HardFloat:
1260b57cec5SDimitry Andric Builder.defineMacro("__mips_hard_float", Twine(1));
1270b57cec5SDimitry Andric break;
1280b57cec5SDimitry Andric case SoftFloat:
1290b57cec5SDimitry Andric Builder.defineMacro("__mips_soft_float", Twine(1));
1300b57cec5SDimitry Andric break;
1310b57cec5SDimitry Andric }
1320b57cec5SDimitry Andric
1330b57cec5SDimitry Andric if (IsSingleFloat)
1340b57cec5SDimitry Andric Builder.defineMacro("__mips_single_float", Twine(1));
1350b57cec5SDimitry Andric
1360b57cec5SDimitry Andric switch (FPMode) {
1370b57cec5SDimitry Andric case FPXX:
1380b57cec5SDimitry Andric Builder.defineMacro("__mips_fpr", Twine(0));
1390b57cec5SDimitry Andric break;
1400b57cec5SDimitry Andric case FP32:
1410b57cec5SDimitry Andric Builder.defineMacro("__mips_fpr", Twine(32));
1420b57cec5SDimitry Andric break;
1430b57cec5SDimitry Andric case FP64:
1440b57cec5SDimitry Andric Builder.defineMacro("__mips_fpr", Twine(64));
1450b57cec5SDimitry Andric break;
1460b57cec5SDimitry Andric }
1470b57cec5SDimitry Andric
1480b57cec5SDimitry Andric if (FPMode == FP64 || IsSingleFloat)
1490b57cec5SDimitry Andric Builder.defineMacro("_MIPS_FPSET", Twine(32));
1500b57cec5SDimitry Andric else
1510b57cec5SDimitry Andric Builder.defineMacro("_MIPS_FPSET", Twine(16));
1525f757f3fSDimitry Andric if (NoOddSpreg)
1535f757f3fSDimitry Andric Builder.defineMacro("_MIPS_SPFPSET", Twine(16));
1545f757f3fSDimitry Andric else
1555f757f3fSDimitry Andric Builder.defineMacro("_MIPS_SPFPSET", Twine(32));
1560b57cec5SDimitry Andric
1570b57cec5SDimitry Andric if (IsMips16)
1580b57cec5SDimitry Andric Builder.defineMacro("__mips16", Twine(1));
1590b57cec5SDimitry Andric
1600b57cec5SDimitry Andric if (IsMicromips)
1610b57cec5SDimitry Andric Builder.defineMacro("__mips_micromips", Twine(1));
1620b57cec5SDimitry Andric
1630b57cec5SDimitry Andric if (IsNan2008)
1640b57cec5SDimitry Andric Builder.defineMacro("__mips_nan2008", Twine(1));
1650b57cec5SDimitry Andric
1660b57cec5SDimitry Andric if (IsAbs2008)
1670b57cec5SDimitry Andric Builder.defineMacro("__mips_abs2008", Twine(1));
1680b57cec5SDimitry Andric
1690b57cec5SDimitry Andric switch (DspRev) {
1700b57cec5SDimitry Andric default:
1710b57cec5SDimitry Andric break;
1720b57cec5SDimitry Andric case DSP1:
1730b57cec5SDimitry Andric Builder.defineMacro("__mips_dsp_rev", Twine(1));
1740b57cec5SDimitry Andric Builder.defineMacro("__mips_dsp", Twine(1));
1750b57cec5SDimitry Andric break;
1760b57cec5SDimitry Andric case DSP2:
1770b57cec5SDimitry Andric Builder.defineMacro("__mips_dsp_rev", Twine(2));
1780b57cec5SDimitry Andric Builder.defineMacro("__mips_dspr2", Twine(1));
1790b57cec5SDimitry Andric Builder.defineMacro("__mips_dsp", Twine(1));
1800b57cec5SDimitry Andric break;
1810b57cec5SDimitry Andric }
1820b57cec5SDimitry Andric
1830b57cec5SDimitry Andric if (HasMSA)
1840b57cec5SDimitry Andric Builder.defineMacro("__mips_msa", Twine(1));
1850b57cec5SDimitry Andric
1860b57cec5SDimitry Andric if (DisableMadd4)
1870b57cec5SDimitry Andric Builder.defineMacro("__mips_no_madd4", Twine(1));
1880b57cec5SDimitry Andric
189bdd1243dSDimitry Andric Builder.defineMacro("_MIPS_SZPTR", Twine(getPointerWidth(LangAS::Default)));
1900b57cec5SDimitry Andric Builder.defineMacro("_MIPS_SZINT", Twine(getIntWidth()));
1910b57cec5SDimitry Andric Builder.defineMacro("_MIPS_SZLONG", Twine(getLongWidth()));
1920b57cec5SDimitry Andric
1930b57cec5SDimitry Andric Builder.defineMacro("_MIPS_ARCH", "\"" + CPU + "\"");
1940b57cec5SDimitry Andric if (CPU == "octeon+")
1950b57cec5SDimitry Andric Builder.defineMacro("_MIPS_ARCH_OCTEONP");
1960b57cec5SDimitry Andric else
1970b57cec5SDimitry Andric Builder.defineMacro("_MIPS_ARCH_" + StringRef(CPU).upper());
1980b57cec5SDimitry Andric
1995f757f3fSDimitry Andric if (StringRef(CPU).starts_with("octeon"))
2000b57cec5SDimitry Andric Builder.defineMacro("__OCTEON__");
2010b57cec5SDimitry Andric
202bdd1243dSDimitry Andric if (CPU != "mips1") {
2030b57cec5SDimitry Andric Builder.defineMacro("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_1");
2040b57cec5SDimitry Andric Builder.defineMacro("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_2");
2050b57cec5SDimitry Andric Builder.defineMacro("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4");
206bdd1243dSDimitry Andric }
2070b57cec5SDimitry Andric
2080b57cec5SDimitry Andric // 32-bit MIPS processors don't have the necessary lld/scd instructions
2090b57cec5SDimitry Andric // found in 64-bit processors. In the case of O32 on a 64-bit processor,
2100b57cec5SDimitry Andric // the instructions exist but using them violates the ABI since they
2110b57cec5SDimitry Andric // require 64-bit GPRs and O32 only supports 32-bit GPRs.
2120b57cec5SDimitry Andric if (ABI == "n32" || ABI == "n64")
2130b57cec5SDimitry Andric Builder.defineMacro("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_8");
2140b57cec5SDimitry Andric }
2150b57cec5SDimitry Andric
hasFeature(StringRef Feature) const2160b57cec5SDimitry Andric bool MipsTargetInfo::hasFeature(StringRef Feature) const {
2170b57cec5SDimitry Andric return llvm::StringSwitch<bool>(Feature)
2180b57cec5SDimitry Andric .Case("mips", true)
219480093f4SDimitry Andric .Case("dsp", DspRev >= DSP1)
220480093f4SDimitry Andric .Case("dspr2", DspRev >= DSP2)
2210b57cec5SDimitry Andric .Case("fp64", FPMode == FP64)
222480093f4SDimitry Andric .Case("msa", HasMSA)
2230b57cec5SDimitry Andric .Default(false);
2240b57cec5SDimitry Andric }
2250b57cec5SDimitry Andric
getTargetBuiltins() const2260b57cec5SDimitry Andric ArrayRef<Builtin::Info> MipsTargetInfo::getTargetBuiltins() const {
227bdd1243dSDimitry Andric return llvm::ArrayRef(BuiltinInfo,
228bdd1243dSDimitry Andric clang::Mips::LastTSBuiltin - Builtin::FirstTSBuiltin);
2290b57cec5SDimitry Andric }
2300b57cec5SDimitry Andric
getUnwindWordWidth() const2310b57cec5SDimitry Andric unsigned MipsTargetInfo::getUnwindWordWidth() const {
2320b57cec5SDimitry Andric return llvm::StringSwitch<unsigned>(ABI)
2330b57cec5SDimitry Andric .Case("o32", 32)
2340b57cec5SDimitry Andric .Case("n32", 64)
2350b57cec5SDimitry Andric .Case("n64", 64)
236bdd1243dSDimitry Andric .Default(getPointerWidth(LangAS::Default));
2370b57cec5SDimitry Andric }
2380b57cec5SDimitry Andric
validateTarget(DiagnosticsEngine & Diags) const2390b57cec5SDimitry Andric bool MipsTargetInfo::validateTarget(DiagnosticsEngine &Diags) const {
2400b57cec5SDimitry Andric // microMIPS64R6 backend was removed.
2410b57cec5SDimitry Andric if (getTriple().isMIPS64() && IsMicromips && (ABI == "n32" || ABI == "n64")) {
2420b57cec5SDimitry Andric Diags.Report(diag::err_target_unsupported_cpu_for_micromips) << CPU;
2430b57cec5SDimitry Andric return false;
2440b57cec5SDimitry Andric }
2450b57cec5SDimitry Andric
2460b57cec5SDimitry Andric // 64-bit ABI's require 64-bit CPU's.
2470b57cec5SDimitry Andric if (!processorSupportsGPR64() && (ABI == "n32" || ABI == "n64")) {
2480b57cec5SDimitry Andric Diags.Report(diag::err_target_unsupported_abi) << ABI << CPU;
2490b57cec5SDimitry Andric return false;
2500b57cec5SDimitry Andric }
2510b57cec5SDimitry Andric
2520b57cec5SDimitry Andric // -fpxx is valid only for the o32 ABI
2530b57cec5SDimitry Andric if (FPMode == FPXX && (ABI == "n32" || ABI == "n64")) {
2540b57cec5SDimitry Andric Diags.Report(diag::err_unsupported_abi_for_opt) << "-mfpxx" << "o32";
2550b57cec5SDimitry Andric return false;
2560b57cec5SDimitry Andric }
2570b57cec5SDimitry Andric
2580b57cec5SDimitry Andric // -mfp32 and n32/n64 ABIs are incompatible
2590b57cec5SDimitry Andric if (FPMode != FP64 && FPMode != FPXX && !IsSingleFloat &&
2600b57cec5SDimitry Andric (ABI == "n32" || ABI == "n64")) {
2610b57cec5SDimitry Andric Diags.Report(diag::err_opt_not_valid_with_opt) << "-mfpxx" << CPU;
2620b57cec5SDimitry Andric return false;
2630b57cec5SDimitry Andric }
2640b57cec5SDimitry Andric // Mips revision 6 and -mfp32 are incompatible
2650b57cec5SDimitry Andric if (FPMode != FP64 && FPMode != FPXX && (CPU == "mips32r6" ||
2660b57cec5SDimitry Andric CPU == "mips64r6")) {
2670b57cec5SDimitry Andric Diags.Report(diag::err_opt_not_valid_with_opt) << "-mfp32" << CPU;
2680b57cec5SDimitry Andric return false;
2690b57cec5SDimitry Andric }
2700b57cec5SDimitry Andric // Option -mfp64 permitted on Mips32 iff revision 2 or higher is present
2710b57cec5SDimitry Andric if (FPMode == FP64 && (CPU == "mips1" || CPU == "mips2" ||
2720b57cec5SDimitry Andric getISARev() < 2) && ABI == "o32") {
2730b57cec5SDimitry Andric Diags.Report(diag::err_mips_fp64_req) << "-mfp64";
2740b57cec5SDimitry Andric return false;
2750b57cec5SDimitry Andric }
276*0fca6ea1SDimitry Andric // FPXX requires mips2+
277*0fca6ea1SDimitry Andric if (FPMode == FPXX && CPU == "mips1") {
278*0fca6ea1SDimitry Andric Diags.Report(diag::err_opt_not_valid_with_opt) << "-mfpxx" << CPU;
279*0fca6ea1SDimitry Andric return false;
280*0fca6ea1SDimitry Andric }
281*0fca6ea1SDimitry Andric // -mmsa with -msoft-float makes nonsense
282*0fca6ea1SDimitry Andric if (FloatABI == SoftFloat && HasMSA) {
283*0fca6ea1SDimitry Andric Diags.Report(diag::err_opt_not_valid_with_opt) << "-msoft-float"
284*0fca6ea1SDimitry Andric << "-mmsa";
285*0fca6ea1SDimitry Andric return false;
286*0fca6ea1SDimitry Andric }
287*0fca6ea1SDimitry Andric // Option -mmsa permitted on Mips32 iff revision 2 or higher is present
288*0fca6ea1SDimitry Andric if (HasMSA && (CPU == "mips1" || CPU == "mips2" || getISARev() < 2) &&
289*0fca6ea1SDimitry Andric ABI == "o32") {
290*0fca6ea1SDimitry Andric Diags.Report(diag::err_mips_fp64_req) << "-mmsa";
291*0fca6ea1SDimitry Andric return false;
292*0fca6ea1SDimitry Andric }
293*0fca6ea1SDimitry Andric // MSA requires FP64
294*0fca6ea1SDimitry Andric if (FPMode == FPXX && HasMSA) {
295*0fca6ea1SDimitry Andric Diags.Report(diag::err_opt_not_valid_with_opt) << "-mfpxx"
296*0fca6ea1SDimitry Andric << "-mmsa";
297*0fca6ea1SDimitry Andric return false;
298*0fca6ea1SDimitry Andric }
299*0fca6ea1SDimitry Andric if (FPMode == FP32 && HasMSA) {
300*0fca6ea1SDimitry Andric Diags.Report(diag::err_opt_not_valid_with_opt) << "-mfp32"
301*0fca6ea1SDimitry Andric << "-mmsa";
302*0fca6ea1SDimitry Andric return false;
303*0fca6ea1SDimitry Andric }
3040b57cec5SDimitry Andric
3050b57cec5SDimitry Andric return true;
3060b57cec5SDimitry Andric }
307