1//===--- PPC.cpp - PPC Helpers for Tools ------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "PPC.h"
10#include "clang/Driver/CommonArgs.h"
11#include "clang/Driver/Driver.h"
12#include "clang/Options/Options.h"
13#include "llvm/ADT/StringSwitch.h"
14#include "llvm/Option/ArgList.h"
15#include "llvm/TargetParser/Host.h"
16
17using namespace clang::driver;
18using namespace clang::driver::tools;
19using namespace clang;
20using namespace llvm::opt;
21
22const char *ppc::getPPCAsmModeForCPU(StringRef Name) {
23 return llvm::StringSwitch<const char *>(Name)
24 .Case(S: "pwr7", Value: "-mpower7")
25 .Case(S: "power7", Value: "-mpower7")
26 .Case(S: "pwr8", Value: "-mpower8")
27 .Case(S: "power8", Value: "-mpower8")
28 .Case(S: "ppc64le", Value: "-mpower8")
29 .Case(S: "pwr9", Value: "-mpower9")
30 .Case(S: "power9", Value: "-mpower9")
31 .Case(S: "pwr10", Value: "-mpower10")
32 .Case(S: "power10", Value: "-mpower10")
33 .Case(S: "pwr11", Value: "-mpower11")
34 .Case(S: "power11", Value: "-mpower11")
35 .Default(Value: "-many");
36}
37
38void ppc::getPPCTargetFeatures(const Driver &D, const llvm::Triple &Triple,
39 const ArgList &Args,
40 std::vector<StringRef> &Features) {
41 if (Triple.getSubArch() == llvm::Triple::PPCSubArch_spe)
42 Features.push_back(x: "+spe");
43
44 handleTargetFeaturesGroup(D, Triple, Args, Features,
45 Group: options::OPT_m_ppc_Features_Group);
46
47 ppc::FloatABI FloatABI = ppc::getPPCFloatABI(D, Args);
48 if (FloatABI == ppc::FloatABI::Soft)
49 Features.push_back(x: "-hard-float");
50
51 ppc::ReadGOTPtrMode ReadGOT = ppc::getPPCReadGOTPtrMode(D, Triple, Args);
52 if (ReadGOT == ppc::ReadGOTPtrMode::SecurePlt)
53 Features.push_back(x: "+secure-plt");
54
55 bool UseSeparateSections = isUseSeparateSections(Triple);
56 bool HasDefaultDataSections = Triple.isOSBinFormatXCOFF();
57 if (Args.hasArg(Ids: options::OPT_maix_small_local_exec_tls) ||
58 Args.hasArg(Ids: options::OPT_maix_small_local_dynamic_tls)) {
59 if (!Triple.isOSAIX() || !Triple.isArch64Bit())
60 D.Diag(DiagID: diag::err_opt_not_valid_on_target)
61 << "-maix-small-local-[exec|dynamic]-tls";
62
63 // The -maix-small-local-[exec|dynamic]-tls option should only be used with
64 // -fdata-sections, as having data sections turned off with this option
65 // is not ideal for performance. Moreover, the
66 // small-local-[exec|dynamic]-tls region is a limited resource, and should
67 // not be used for variables that may be replaced.
68 if (!Args.hasFlag(Pos: options::OPT_fdata_sections,
69 Neg: options::OPT_fno_data_sections,
70 Default: UseSeparateSections || HasDefaultDataSections))
71 D.Diag(DiagID: diag::err_drv_argument_only_allowed_with)
72 << "-maix-small-local-[exec|dynamic]-tls" << "-fdata-sections";
73 }
74
75 if (Args.hasArg(Ids: options::OPT_maix_shared_lib_tls_model_opt) &&
76 !(Triple.isOSAIX() && Triple.isArch64Bit()))
77 D.Diag(DiagID: diag::err_opt_not_valid_on_target)
78 << "-maix-shared-lib-tls-model-opt";
79
80 // The integrated assembler counts as a "modern AIX assembler" for the
81 // purposes of the modern-aix-as.
82 if (Args.hasFlag(Pos: options::OPT_fintegrated_as, Neg: options::OPT_fno_integrated_as,
83 Default: true) &&
84 Triple.isOSAIX())
85 Features.push_back(x: "+modern-aix-as");
86}
87
88ppc::ReadGOTPtrMode ppc::getPPCReadGOTPtrMode(const Driver &D, const llvm::Triple &Triple,
89 const ArgList &Args) {
90 if (Args.getLastArg(Ids: options::OPT_msecure_plt))
91 return ppc::ReadGOTPtrMode::SecurePlt;
92 if (Triple.isPPC32SecurePlt())
93 return ppc::ReadGOTPtrMode::SecurePlt;
94 else
95 return ppc::ReadGOTPtrMode::Bss;
96}
97
98ppc::FloatABI ppc::getPPCFloatABI(const Driver &D, const ArgList &Args) {
99 ppc::FloatABI ABI = ppc::FloatABI::Invalid;
100 if (Arg *A =
101 Args.getLastArg(Ids: options::OPT_msoft_float, Ids: options::OPT_mhard_float,
102 Ids: options::OPT_mfloat_abi_EQ)) {
103 if (A->getOption().matches(ID: options::OPT_msoft_float))
104 ABI = ppc::FloatABI::Soft;
105 else if (A->getOption().matches(ID: options::OPT_mhard_float))
106 ABI = ppc::FloatABI::Hard;
107 else {
108 ABI = llvm::StringSwitch<ppc::FloatABI>(A->getValue())
109 .Case(S: "soft", Value: ppc::FloatABI::Soft)
110 .Case(S: "hard", Value: ppc::FloatABI::Hard)
111 .Default(Value: ppc::FloatABI::Invalid);
112 if (ABI == ppc::FloatABI::Invalid && !StringRef(A->getValue()).empty()) {
113 D.Diag(DiagID: clang::diag::err_drv_invalid_mfloat_abi) << A->getAsString(Args);
114 ABI = ppc::FloatABI::Hard;
115 }
116 }
117 }
118
119 // If unspecified, choose the default based on the platform.
120 if (ABI == ppc::FloatABI::Invalid) {
121 ABI = ppc::FloatABI::Hard;
122 }
123
124 return ABI;
125}
126
127bool ppc::hasPPCAbiArg(const ArgList &Args, const char *Value) {
128 Arg *A = Args.getLastArg(Ids: options::OPT_mabi_EQ);
129 return A && (A->getValue() == StringRef(Value));
130}
131