| 1 | //===--- Hexagon.cpp - Hexagon ToolChain Implementations --------*- 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 "Hexagon.h" |
| 10 | #include "clang/Driver/CommonArgs.h" |
| 11 | #include "clang/Driver/Compilation.h" |
| 12 | #include "clang/Driver/Driver.h" |
| 13 | #include "clang/Driver/InputInfo.h" |
| 14 | #include "clang/Driver/MultilibBuilder.h" |
| 15 | #include "clang/Driver/SanitizerArgs.h" |
| 16 | #include "clang/Options/Options.h" |
| 17 | #include "llvm/Option/ArgList.h" |
| 18 | #include "llvm/Support/FileSystem.h" |
| 19 | #include "llvm/Support/Path.h" |
| 20 | #include "llvm/Support/VirtualFileSystem.h" |
| 21 | |
| 22 | using namespace clang::driver; |
| 23 | using namespace clang::driver::tools; |
| 24 | using namespace clang::driver::toolchains; |
| 25 | using namespace clang; |
| 26 | using namespace llvm::opt; |
| 27 | |
| 28 | // Default hvx-length for various versions. |
| 29 | static StringRef getDefaultHvxLength(StringRef HvxVer) { |
| 30 | return llvm::StringSwitch<StringRef>(HvxVer) |
| 31 | .Case(S: "v60" , Value: "64b" ) |
| 32 | .Case(S: "v62" , Value: "64b" ) |
| 33 | .Case(S: "v65" , Value: "64b" ) |
| 34 | .Default(Value: "128b" ); |
| 35 | } |
| 36 | |
| 37 | static void handleHVXWarnings(const Driver &D, const ArgList &Args) { |
| 38 | // Handle the unsupported values passed to mhvx-length. |
| 39 | if (Arg *A = Args.getLastArg(Ids: options::OPT_mhexagon_hvx_length_EQ)) { |
| 40 | StringRef Val = A->getValue(); |
| 41 | if (!Val.equals_insensitive(RHS: "64b" ) && !Val.equals_insensitive(RHS: "128b" )) |
| 42 | D.Diag(DiagID: diag::err_drv_unsupported_option_argument) |
| 43 | << A->getSpelling() << Val; |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | // Handle hvx target features explicitly. |
| 48 | static void handleHVXTargetFeatures(const Driver &D, const ArgList &Args, |
| 49 | std::vector<StringRef> &Features, |
| 50 | StringRef Cpu, bool &HasHVX) { |
| 51 | // Handle HVX warnings. |
| 52 | handleHVXWarnings(D, Args); |
| 53 | |
| 54 | auto makeFeature = [&Args](Twine T, bool Enable) -> StringRef { |
| 55 | const std::string &S = T.str(); |
| 56 | StringRef Opt(S); |
| 57 | Opt.consume_back(Suffix: "=" ); |
| 58 | if (Opt.starts_with(Prefix: "mno-" )) |
| 59 | Opt = Opt.drop_front(N: 4); |
| 60 | else if (Opt.starts_with(Prefix: "m" )) |
| 61 | Opt = Opt.drop_front(N: 1); |
| 62 | return Args.MakeArgString(Str: Twine(Enable ? "+" : "-" ) + Twine(Opt)); |
| 63 | }; |
| 64 | |
| 65 | auto withMinus = [](StringRef S) -> std::string { |
| 66 | return "-" + S.str(); |
| 67 | }; |
| 68 | |
| 69 | std::optional<std::string> HvxVer = |
| 70 | toolchains::HexagonToolChain::GetHVXVersion(Args); |
| 71 | HasHVX = HvxVer.has_value(); |
| 72 | if (HasHVX) |
| 73 | Features.push_back(x: makeFeature(Twine("hvx" ) + *HvxVer, true)); |
| 74 | else { |
| 75 | if (Arg *A = Args.getLastArg(Ids: options::OPT_mno_hexagon_hvx)) { |
| 76 | // If there was an explicit -mno-hvx, add -hvx to target features. |
| 77 | Features.push_back(x: makeFeature(A->getOption().getName(), false)); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | StringRef HvxLen = |
| 82 | getDefaultHvxLength(HvxVer: HasHVX ? StringRef(*HvxVer) : StringRef("" )); |
| 83 | |
| 84 | // Handle -mhvx-length=. |
| 85 | if (Arg *A = Args.getLastArg(Ids: options::OPT_mhexagon_hvx_length_EQ)) { |
| 86 | // These flags are valid only if HVX in enabled. |
| 87 | if (!HasHVX) |
| 88 | D.Diag(DiagID: diag::err_drv_needs_hvx) << withMinus(A->getOption().getName()); |
| 89 | else if (A->getOption().matches(ID: options::OPT_mhexagon_hvx_length_EQ)) |
| 90 | HvxLen = A->getValue(); |
| 91 | } |
| 92 | |
| 93 | if (HasHVX) { |
| 94 | StringRef L = makeFeature(Twine("hvx-length" ) + HvxLen.lower(), true); |
| 95 | Features.push_back(x: L); |
| 96 | } |
| 97 | |
| 98 | unsigned HvxVerNum = 0; |
| 99 | // getAsInteger returns 'true' on error. |
| 100 | if (HasHVX) { |
| 101 | StringRef HvxVerRef(*HvxVer); |
| 102 | if (HvxVerRef.size() <= 1 || |
| 103 | HvxVerRef.drop_front(N: 1).getAsInteger(Radix: 10, Result&: HvxVerNum)) |
| 104 | HvxVerNum = 0; |
| 105 | } |
| 106 | |
| 107 | // Handle HVX floating point flags. |
| 108 | auto checkFlagHvxVersion = |
| 109 | [&](auto FlagOn, auto FlagOnWithModes, auto FlagOff, bool CheckMode, |
| 110 | unsigned MinVerNum) -> std::optional<StringRef> { |
| 111 | // Return an std::optional<StringRef>: |
| 112 | // - std::nullopt indicates a verification failure, or that the flag was not |
| 113 | // present in Args. |
| 114 | // - Otherwise the returned value is that name of the feature to add |
| 115 | // to Features. |
| 116 | Arg *A = Args.getLastArg(FlagOn, FlagOnWithModes, FlagOff); |
| 117 | if (!A) |
| 118 | return std::nullopt; |
| 119 | |
| 120 | StringRef OptName = A->getOption().getName(); |
| 121 | if (A->getOption().matches(ID: FlagOff)) |
| 122 | return makeFeature(OptName, false); |
| 123 | |
| 124 | if (!HasHVX) { |
| 125 | D.Diag(DiagID: diag::err_drv_needs_hvx) << withMinus(OptName); |
| 126 | return std::nullopt; |
| 127 | } |
| 128 | if (HvxVerNum < MinVerNum) { |
| 129 | D.Diag(DiagID: diag::err_drv_needs_hvx_version) |
| 130 | << withMinus(OptName) << ("v" + std::to_string(val: HvxVerNum)); |
| 131 | return std::nullopt; |
| 132 | } |
| 133 | |
| 134 | if (CheckMode && A->getOption().matches(ID: FlagOnWithModes)) { |
| 135 | bool ValidMode = |
| 136 | llvm::StringSwitch<bool>(StringRef(A->getValue()).lower()) |
| 137 | .Cases(CaseStrings: {"strict-ieee" , "ieee" , "lossy" , "legacy" }, Value: true) |
| 138 | .Default(Value: false); |
| 139 | if (!ValidMode) |
| 140 | D.Diag(DiagID: diag::err_drv_invalid_value) |
| 141 | << A->getAsString(Args) << A->getValue(); |
| 142 | } |
| 143 | return makeFeature(OptName, true); |
| 144 | }; |
| 145 | |
| 146 | if (auto F = checkFlagHvxVersion( |
| 147 | options::OPT_mhexagon_hvx_qfloat, options::OPT_mhexagon_hvx_qfloat_EQ, |
| 148 | options::OPT_mno_hexagon_hvx_qfloat, /*CheckMode=*/true, 68)) { |
| 149 | Features.push_back(x: *F); |
| 150 | } |
| 151 | if (auto F = checkFlagHvxVersion( |
| 152 | options::OPT_mhexagon_hvx_ieee_fp, options::OPT_mhexagon_hvx_ieee_fp, |
| 153 | options::OPT_mno_hexagon_hvx_ieee_fp, /*CheckMode=*/false, 68)) { |
| 154 | Features.push_back(x: *F); |
| 155 | } |
| 156 | |
| 157 | // On v79 and above, there is no IEEE hardware. Treat -mhvx-ieee-fp |
| 158 | // as "qfloat mode ieee". |
| 159 | if (HvxVerNum >= 79 && Args.getLastArg(Ids: options::OPT_mhexagon_hvx_ieee_fp)) |
| 160 | Features.push_back(x: "+hvx-qfloat" ); |
| 161 | } |
| 162 | |
| 163 | // Hexagon target features. |
| 164 | void hexagon::getHexagonTargetFeatures(const Driver &D, |
| 165 | const llvm::Triple &Triple, |
| 166 | const ArgList &Args, |
| 167 | std::vector<StringRef> &Features) { |
| 168 | handleTargetFeaturesGroup(D, Triple, Args, Features, |
| 169 | Group: options::OPT_m_hexagon_Features_Group); |
| 170 | |
| 171 | bool UseLongCalls = false; |
| 172 | if (Arg *A = Args.getLastArg(Ids: options::OPT_mlong_calls, |
| 173 | Ids: options::OPT_mno_long_calls)) { |
| 174 | if (A->getOption().matches(ID: options::OPT_mlong_calls)) |
| 175 | UseLongCalls = true; |
| 176 | } |
| 177 | |
| 178 | Features.push_back(x: UseLongCalls ? "+long-calls" : "-long-calls" ); |
| 179 | |
| 180 | bool HasHVX = false; |
| 181 | StringRef Cpu(toolchains::HexagonToolChain::GetTargetCPUVersion(Args)); |
| 182 | // 't' in Cpu denotes tiny-core micro-architecture. For now, the co-processors |
| 183 | // have no dependency on micro-architecture. |
| 184 | const bool TinyCore = Cpu.contains(C: 't'); |
| 185 | |
| 186 | if (TinyCore) |
| 187 | Cpu = Cpu.take_front(N: Cpu.size() - 1); |
| 188 | |
| 189 | handleHVXTargetFeatures(D, Args, Features, Cpu, HasHVX); |
| 190 | |
| 191 | if (HexagonToolChain::isAutoHVXEnabled(Args) && !HasHVX) |
| 192 | D.Diag(DiagID: diag::warn_drv_needs_hvx) << "auto-vectorization" ; |
| 193 | } |
| 194 | |
| 195 | // Hexagon tools start. |
| 196 | void hexagon::Assembler::(const JobAction &JA, |
| 197 | ArgStringList &CmdArgs) const { |
| 198 | } |
| 199 | |
| 200 | void hexagon::Assembler::ConstructJob(Compilation &C, const JobAction &JA, |
| 201 | const InputInfo &Output, |
| 202 | const InputInfoList &Inputs, |
| 203 | const ArgList &Args, |
| 204 | const char *LinkingOutput) const { |
| 205 | claimNoWarnArgs(Args); |
| 206 | |
| 207 | auto &HTC = static_cast<const toolchains::HexagonToolChain&>(getToolChain()); |
| 208 | const Driver &D = HTC.getDriver(); |
| 209 | ArgStringList CmdArgs; |
| 210 | |
| 211 | CmdArgs.push_back(Elt: "--arch=hexagon" ); |
| 212 | |
| 213 | RenderExtraToolArgs(JA, CmdArgs); |
| 214 | |
| 215 | const char *AsName = "llvm-mc" ; |
| 216 | CmdArgs.push_back(Elt: "-filetype=obj" ); |
| 217 | CmdArgs.push_back(Elt: Args.MakeArgString( |
| 218 | Str: "-mcpu=hexagon" + |
| 219 | toolchains::HexagonToolChain::GetTargetCPUVersion(Args))); |
| 220 | |
| 221 | addSanitizerRuntimes(TC: HTC, Args, CmdArgs); |
| 222 | |
| 223 | assert((Output.isFilename() || Output.isNothing()) && "Invalid output." ); |
| 224 | if (Output.isFilename()) { |
| 225 | CmdArgs.push_back(Elt: "-o" ); |
| 226 | CmdArgs.push_back(Elt: Output.getFilename()); |
| 227 | } else { |
| 228 | CmdArgs.push_back(Elt: "-fsyntax-only" ); |
| 229 | } |
| 230 | |
| 231 | if (Arg *A = Args.getLastArg(Ids: options::OPT_mhexagon_hvx_ieee_fp, |
| 232 | Ids: options::OPT_mno_hexagon_hvx_ieee_fp)) { |
| 233 | if (A->getOption().matches(ID: options::OPT_mhexagon_hvx_ieee_fp)) |
| 234 | CmdArgs.push_back(Elt: "-mhvx-ieee-fp" ); |
| 235 | } |
| 236 | |
| 237 | if (auto G = toolchains::HexagonToolChain::getSmallDataThreshold(Args)) { |
| 238 | CmdArgs.push_back(Elt: Args.MakeArgString(Str: "-gpsize=" + Twine(*G))); |
| 239 | } |
| 240 | |
| 241 | Args.AddAllArgValues(Output&: CmdArgs, Id0: options::OPT_Wa_COMMA, Id1: options::OPT_Xassembler); |
| 242 | |
| 243 | // Only pass -x if gcc will understand it; otherwise hope gcc |
| 244 | // understands the suffix correctly. The main use case this would go |
| 245 | // wrong in is for linker inputs if they happened to have an odd |
| 246 | // suffix; really the only way to get this to happen is a command |
| 247 | // like '-x foobar a.c' which will treat a.c like a linker input. |
| 248 | // |
| 249 | // FIXME: For the linker case specifically, can we safely convert |
| 250 | // inputs into '-Wl,' options? |
| 251 | for (const auto &II : Inputs) { |
| 252 | // Don't try to pass LLVM or AST inputs to a generic gcc. |
| 253 | if (types::isLLVMIR(Id: II.getType())) |
| 254 | D.Diag(DiagID: clang::diag::err_drv_no_linker_llvm_support) |
| 255 | << HTC.getTripleString(); |
| 256 | else if (II.getType() == types::TY_AST) |
| 257 | D.Diag(DiagID: clang::diag::err_drv_no_ast_support) |
| 258 | << HTC.getTripleString(); |
| 259 | else if (II.getType() == types::TY_ModuleFile) |
| 260 | D.Diag(DiagID: diag::err_drv_no_module_support) |
| 261 | << HTC.getTripleString(); |
| 262 | |
| 263 | if (II.isFilename()) |
| 264 | CmdArgs.push_back(Elt: II.getFilename()); |
| 265 | else |
| 266 | // Don't render as input, we need gcc to do the translations. |
| 267 | // FIXME: What is this? |
| 268 | II.getInputArg().render(Args, Output&: CmdArgs); |
| 269 | } |
| 270 | |
| 271 | auto *Exec = Args.MakeArgString(Str: HTC.GetProgramPath(Name: AsName)); |
| 272 | C.addCommand(Cmd: std::make_unique<Command>(args: JA, args: *this, |
| 273 | args: ResponseFileSupport::AtFileCurCP(), |
| 274 | args&: Exec, args&: CmdArgs, args: Inputs, args: Output)); |
| 275 | } |
| 276 | |
| 277 | void hexagon::Linker::(const JobAction &JA, |
| 278 | ArgStringList &CmdArgs) const { |
| 279 | } |
| 280 | |
| 281 | static void |
| 282 | constructHexagonLinkArgs(Compilation &C, const JobAction &JA, |
| 283 | const toolchains::HexagonToolChain &HTC, |
| 284 | const InputInfo &Output, const InputInfoList &Inputs, |
| 285 | const ArgList &Args, ArgStringList &CmdArgs, |
| 286 | const char *LinkingOutput) { |
| 287 | |
| 288 | const Driver &D = HTC.getDriver(); |
| 289 | |
| 290 | //---------------------------------------------------------------------------- |
| 291 | // |
| 292 | //---------------------------------------------------------------------------- |
| 293 | bool IsStatic = Args.hasArg(Ids: options::OPT_static); |
| 294 | bool IsShared = Args.hasArg(Ids: options::OPT_shared); |
| 295 | bool IsPIE = Args.hasFlag(Pos: options::OPT_pie, Neg: options::OPT_no_pie, |
| 296 | Default: HTC.isPIEDefault(Args)); |
| 297 | bool IncStdLib = !Args.hasArg(Ids: options::OPT_nostdlib); |
| 298 | bool IncStartFiles = !Args.hasArg(Ids: options::OPT_nostartfiles); |
| 299 | bool IncDefLibs = !Args.hasArg(Ids: options::OPT_nodefaultlibs); |
| 300 | bool UseLLD = false; |
| 301 | const char *Exec = Args.MakeArgString(Str: HTC.GetLinkerPath(LinkerIsLLD: &UseLLD)); |
| 302 | UseLLD = UseLLD || llvm::sys::path::filename(path: Exec).ends_with(Suffix: "ld.lld" ) || |
| 303 | llvm::sys::path::stem(path: Exec).ends_with(Suffix: "ld.lld" ); |
| 304 | bool UseShared = IsShared && !IsStatic; |
| 305 | StringRef CpuVer = toolchains::HexagonToolChain::GetTargetCPUVersion(Args); |
| 306 | |
| 307 | bool NeedsSanitizerDeps = addSanitizerRuntimes(TC: HTC, Args, CmdArgs); |
| 308 | bool NeedsXRayDeps = addXRayRuntime(TC: HTC, Args, CmdArgs); |
| 309 | |
| 310 | //---------------------------------------------------------------------------- |
| 311 | // Silence warnings for various options |
| 312 | //---------------------------------------------------------------------------- |
| 313 | Args.ClaimAllArgs(Id0: options::OPT_g_Group); |
| 314 | Args.ClaimAllArgs(Id0: options::OPT_emit_llvm); |
| 315 | Args.ClaimAllArgs(Id0: options::OPT_w); // Other warning options are already |
| 316 | // handled somewhere else. |
| 317 | Args.ClaimAllArgs(Id0: options::OPT_static_libgcc); |
| 318 | |
| 319 | CmdArgs.push_back(Elt: "--eh-frame-hdr" ); |
| 320 | //---------------------------------------------------------------------------- |
| 321 | // |
| 322 | //---------------------------------------------------------------------------- |
| 323 | if (Args.hasArg(Ids: options::OPT_s)) |
| 324 | CmdArgs.push_back(Elt: "-s" ); |
| 325 | |
| 326 | if (Args.hasArg(Ids: options::OPT_r)) |
| 327 | CmdArgs.push_back(Elt: "-r" ); |
| 328 | |
| 329 | for (const auto &Opt : HTC.ExtraOpts) |
| 330 | CmdArgs.push_back(Elt: Opt.c_str()); |
| 331 | |
| 332 | if (!UseLLD) { |
| 333 | CmdArgs.push_back(Elt: "-march=hexagon" ); |
| 334 | CmdArgs.push_back(Elt: Args.MakeArgString(Str: "-mcpu=hexagon" + CpuVer)); |
| 335 | } |
| 336 | |
| 337 | if (IsShared) { |
| 338 | CmdArgs.push_back(Elt: "-shared" ); |
| 339 | // The following should be the default, but doing as hexagon-gcc does. |
| 340 | CmdArgs.push_back(Elt: "-call_shared" ); |
| 341 | } |
| 342 | |
| 343 | if (IsStatic) |
| 344 | CmdArgs.push_back(Elt: "-static" ); |
| 345 | |
| 346 | if (IsPIE && !IsShared && !Args.hasArg(Ids: options::OPT_r)) |
| 347 | CmdArgs.push_back(Elt: "-pie" ); |
| 348 | |
| 349 | if (auto G = toolchains::HexagonToolChain::getSmallDataThreshold(Args)) |
| 350 | CmdArgs.push_back(Elt: Args.MakeArgString(Str: "-G" + Twine(*G))); |
| 351 | |
| 352 | CmdArgs.push_back(Elt: "-o" ); |
| 353 | CmdArgs.push_back(Elt: Output.getFilename()); |
| 354 | |
| 355 | if (HTC.getTriple().isMusl()) { |
| 356 | if (!Args.hasArg(Ids: options::OPT_shared, Ids: options::OPT_static, Ids: options::OPT_r)) |
| 357 | CmdArgs.push_back(Elt: "-dynamic-linker=/lib/ld-musl-hexagon.so.1" ); |
| 358 | |
| 359 | if (!Args.hasArg(Ids: options::OPT_shared, Ids: options::OPT_nostartfiles, |
| 360 | Ids: options::OPT_nostdlib, Ids: options::OPT_r)) |
| 361 | CmdArgs.push_back(Elt: Args.MakeArgString(Str: D.SysRoot + "/usr/lib/crt1.o" )); |
| 362 | else if (Args.hasArg(Ids: options::OPT_shared) && |
| 363 | !Args.hasArg(Ids: options::OPT_nostartfiles, Ids: options::OPT_nostdlib, |
| 364 | Ids: options::OPT_r)) |
| 365 | CmdArgs.push_back(Elt: Args.MakeArgString(Str: D.SysRoot + "/usr/lib/crti.o" )); |
| 366 | |
| 367 | if (!HTC.getSelectedMultilibs().empty() && |
| 368 | !HTC.getSelectedMultilibs().back().isDefault()) { |
| 369 | CmdArgs.push_back( |
| 370 | Elt: Args.MakeArgString(Str: StringRef("-L" ) + D.SysRoot + "/usr/lib" + |
| 371 | HTC.getSelectedMultilibs().back().gccSuffix())); |
| 372 | } |
| 373 | CmdArgs.push_back( |
| 374 | Elt: Args.MakeArgString(Str: StringRef("-L" ) + D.SysRoot + "/usr/lib" )); |
| 375 | Args.addAllArgs(Output&: CmdArgs, Ids: {options::OPT_T_Group, options::OPT_s, |
| 376 | options::OPT_t, options::OPT_u_Group}); |
| 377 | AddLinkerInputs(TC: HTC, Inputs, Args, CmdArgs, JA); |
| 378 | |
| 379 | if (auto LTO = HTC.getLTOMode(Args); LTO != LTOK_None) |
| 380 | addLTOOptions(ToolChain: HTC, Args, CmdArgs, Output, Inputs, IsThinLTO: LTO == LTOK_Thin); |
| 381 | |
| 382 | ToolChain::UnwindLibType UNW = HTC.GetUnwindLibType(Args); |
| 383 | |
| 384 | if (!Args.hasArg(Ids: options::OPT_nostdlib, Ids: options::OPT_nodefaultlibs)) { |
| 385 | if (NeedsSanitizerDeps) { |
| 386 | linkSanitizerRuntimeDeps(TC: HTC, Args, CmdArgs); |
| 387 | |
| 388 | if (UNW != ToolChain::UNW_None) |
| 389 | CmdArgs.push_back(Elt: "-lunwind" ); |
| 390 | } |
| 391 | if (NeedsXRayDeps) |
| 392 | linkXRayRuntimeDeps(TC: HTC, Args, CmdArgs); |
| 393 | |
| 394 | if (!Args.hasArg(Ids: options::OPT_nolibc)) |
| 395 | CmdArgs.push_back(Elt: "-lc" ); |
| 396 | CmdArgs.push_back(Elt: "-lclang_rt.builtins-hexagon" ); |
| 397 | } |
| 398 | if (D.CCCIsCXX()) { |
| 399 | if (HTC.ShouldLinkCXXStdlib(Args)) |
| 400 | HTC.AddCXXStdlibLibArgs(Args, CmdArgs); |
| 401 | } |
| 402 | const ToolChain::path_list &LibPaths = HTC.getFilePaths(); |
| 403 | for (const auto &LibPath : LibPaths) |
| 404 | CmdArgs.push_back(Elt: Args.MakeArgString(Str: StringRef("-L" ) + LibPath)); |
| 405 | Args.ClaimAllArgs(Id0: options::OPT_L); |
| 406 | return; |
| 407 | } |
| 408 | |
| 409 | //---------------------------------------------------------------------------- |
| 410 | // moslib |
| 411 | //---------------------------------------------------------------------------- |
| 412 | std::vector<std::string> OsLibs; |
| 413 | bool HasStandalone = false; |
| 414 | for (const Arg *A : Args.filtered(Ids: options::OPT_moslib_EQ)) { |
| 415 | A->claim(); |
| 416 | OsLibs.emplace_back(args: A->getValue()); |
| 417 | HasStandalone = HasStandalone || (OsLibs.back() == "standalone" ); |
| 418 | } |
| 419 | if (OsLibs.empty()) { |
| 420 | OsLibs.push_back(x: "standalone" ); |
| 421 | HasStandalone = true; |
| 422 | } |
| 423 | |
| 424 | //---------------------------------------------------------------------------- |
| 425 | // Start Files |
| 426 | //---------------------------------------------------------------------------- |
| 427 | SmallString<128> LibraryDir; |
| 428 | HTC.getLibraryDir(Args, LibraryDir); |
| 429 | |
| 430 | if (IncStdLib && IncStartFiles) { |
| 431 | if (!IsShared) { |
| 432 | if (HTC.GetCStdlibType(Args) == ToolChain::CST_Picolibc) { |
| 433 | SmallString<128> Crt0 = LibraryDir; |
| 434 | if (HTC.getTriple().isOSH2()) { |
| 435 | llvm::sys::path::append(path&: Crt0, a: "crt0-noflash-hosted.o" ); |
| 436 | CmdArgs.push_back(Elt: Args.MakeArgString(Str: Crt0)); |
| 437 | } else if (HTC.getTriple().isOSUnknown()) { |
| 438 | llvm::sys::path::append(path&: Crt0, a: "crt0-semihost.o" ); |
| 439 | CmdArgs.push_back(Elt: Args.MakeArgString(Str: Crt0)); |
| 440 | } |
| 441 | // Known OS other than H2: no semihost crt0; OS provides its own. |
| 442 | } else { |
| 443 | if (HasStandalone) { |
| 444 | SmallString<128> Crt0SA = LibraryDir; |
| 445 | llvm::sys::path::append(path&: Crt0SA, a: "crt0_standalone.o" ); |
| 446 | CmdArgs.push_back(Elt: Args.MakeArgString(Str: Crt0SA)); |
| 447 | } |
| 448 | SmallString<128> Crt0 = LibraryDir; |
| 449 | llvm::sys::path::append(path&: Crt0, a: "crt0.o" ); |
| 450 | CmdArgs.push_back(Elt: Args.MakeArgString(Str: Crt0)); |
| 451 | } |
| 452 | } |
| 453 | if (HTC.GetCStdlibType(Args) != ToolChain::CST_Picolibc) { |
| 454 | SmallString<128> Init = LibraryDir; |
| 455 | llvm::sys::path::append(path&: Init, a: UseShared ? "initS.o" : "init.o" ); |
| 456 | CmdArgs.push_back(Elt: Args.MakeArgString(Str: Init)); |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | //---------------------------------------------------------------------------- |
| 461 | // Library Search Paths |
| 462 | //---------------------------------------------------------------------------- |
| 463 | const ToolChain::path_list &LibPaths = HTC.getFilePaths(); |
| 464 | for (const auto &LibPath : LibPaths) |
| 465 | CmdArgs.push_back(Elt: Args.MakeArgString(Str: StringRef("-L" ) + LibPath)); |
| 466 | Args.ClaimAllArgs(Id0: options::OPT_L); |
| 467 | |
| 468 | //---------------------------------------------------------------------------- |
| 469 | // |
| 470 | //---------------------------------------------------------------------------- |
| 471 | Args.addAllArgs(Output&: CmdArgs, Ids: {options::OPT_T_Group, options::OPT_s, |
| 472 | options::OPT_t, options::OPT_u_Group}); |
| 473 | |
| 474 | AddLinkerInputs(TC: HTC, Inputs, Args, CmdArgs, JA); |
| 475 | |
| 476 | if (auto LTO = HTC.getLTOMode(Args); LTO != LTOK_None) |
| 477 | addLTOOptions(ToolChain: HTC, Args, CmdArgs, Output, Inputs, IsThinLTO: LTO == LTOK_Thin); |
| 478 | |
| 479 | //---------------------------------------------------------------------------- |
| 480 | // Libraries |
| 481 | //---------------------------------------------------------------------------- |
| 482 | if (IncStdLib && IncDefLibs) { |
| 483 | if (D.CCCIsCXX()) { |
| 484 | if (HTC.ShouldLinkCXXStdlib(Args)) |
| 485 | HTC.AddCXXStdlibLibArgs(Args, CmdArgs); |
| 486 | CmdArgs.push_back(Elt: "-lm" ); |
| 487 | } |
| 488 | |
| 489 | CmdArgs.push_back(Elt: "--start-group" ); |
| 490 | |
| 491 | if (!IsShared) { |
| 492 | if (HTC.GetCStdlibType(Args) == ToolChain::CST_Picolibc) { |
| 493 | if (HTC.getTriple().isOSH2()) { |
| 494 | CmdArgs.push_back(Elt: "-lh2" ); |
| 495 | CmdArgs.push_back(Elt: "-lsyscall_wrapper" ); |
| 496 | } else if (HTC.getTriple().isOSUnknown()) { |
| 497 | CmdArgs.push_back(Elt: "-lsemihost" ); |
| 498 | } |
| 499 | // Known OS other than H2: no semihost lib; OS provides its own. |
| 500 | } else { |
| 501 | for (StringRef Lib : OsLibs) |
| 502 | CmdArgs.push_back(Elt: Args.MakeArgString(Str: "-l" + Lib)); |
| 503 | } |
| 504 | if (!Args.hasArg(Ids: options::OPT_nolibc)) |
| 505 | CmdArgs.push_back(Elt: "-lc" ); |
| 506 | } |
| 507 | if (HTC.GetCStdlibType(Args) == ToolChain::CST_Picolibc) { |
| 508 | if (HTC.GetRuntimeLibType(Args) == ToolChain::RLT_CompilerRT) |
| 509 | CmdArgs.push_back(Elt: "-lclang_rt.builtins" ); |
| 510 | else |
| 511 | CmdArgs.push_back(Elt: "-lgcc" ); |
| 512 | } else { |
| 513 | CmdArgs.push_back(Elt: "-lgcc" ); |
| 514 | } |
| 515 | |
| 516 | CmdArgs.push_back(Elt: "--end-group" ); |
| 517 | } |
| 518 | |
| 519 | //---------------------------------------------------------------------------- |
| 520 | // End files |
| 521 | //---------------------------------------------------------------------------- |
| 522 | if (IncStdLib && IncStartFiles) { |
| 523 | if (HTC.GetCStdlibType(Args) != ToolChain::CST_Picolibc) { |
| 524 | SmallString<128> Fini = LibraryDir; |
| 525 | llvm::sys::path::append(path&: Fini, a: UseShared ? "finiS.o" : "fini.o" ); |
| 526 | CmdArgs.push_back(Elt: Args.MakeArgString(Str: Fini)); |
| 527 | } |
| 528 | } |
| 529 | } |
| 530 | |
| 531 | void hexagon::Linker::ConstructJob(Compilation &C, const JobAction &JA, |
| 532 | const InputInfo &Output, |
| 533 | const InputInfoList &Inputs, |
| 534 | const ArgList &Args, |
| 535 | const char *LinkingOutput) const { |
| 536 | auto &HTC = static_cast<const toolchains::HexagonToolChain&>(getToolChain()); |
| 537 | |
| 538 | ArgStringList CmdArgs; |
| 539 | constructHexagonLinkArgs(C, JA, HTC, Output, Inputs, Args, CmdArgs, |
| 540 | LinkingOutput); |
| 541 | |
| 542 | const char *Exec = Args.MakeArgString(Str: HTC.GetLinkerPath()); |
| 543 | C.addCommand(Cmd: std::make_unique<Command>(args: JA, args: *this, |
| 544 | args: ResponseFileSupport::AtFileCurCP(), |
| 545 | args&: Exec, args&: CmdArgs, args: Inputs, args: Output)); |
| 546 | } |
| 547 | // Hexagon tools end. |
| 548 | |
| 549 | /// Hexagon Toolchain |
| 550 | |
| 551 | std::string HexagonToolChain::getHexagonTargetDir( |
| 552 | const std::string &InstalledDir, |
| 553 | const SmallVectorImpl<std::string> &PrefixDirs) const { |
| 554 | const Driver &D = getDriver(); |
| 555 | |
| 556 | // Locate the rest of the toolchain ... |
| 557 | for (auto &I : PrefixDirs) |
| 558 | if (D.getVFS().exists(Path: I)) |
| 559 | return I; |
| 560 | |
| 561 | SmallString<128> Dir(InstalledDir); |
| 562 | llvm::sys::path::append(path&: Dir, a: ".." , b: "target" ); |
| 563 | return std::string(Dir); |
| 564 | } |
| 565 | |
| 566 | SmallString<128> |
| 567 | HexagonToolChain::getEffectiveSysRoot(const ArgList &Args) const { |
| 568 | const Driver &D = getDriver(); |
| 569 | // The user-specified `--sysroot` always takes precedence. |
| 570 | if (!D.SysRoot.empty()) |
| 571 | return SmallString<128>(D.SysRoot); |
| 572 | SmallString<128> Dir(getHexagonTargetDir(InstalledDir: D.Dir, PrefixDirs: D.PrefixDirs)); |
| 573 | // For Picolibc, use picolibc/<triple> with no fallback. |
| 574 | if (GetCStdlibType(Args) == ToolChain::CST_Picolibc) { |
| 575 | llvm::sys::path::append(path&: Dir, a: "picolibc" , b: getTripleString()); |
| 576 | return Dir; |
| 577 | } |
| 578 | // Otherwise, try a triple subdirectory first, then fall back to "hexagon". |
| 579 | llvm::sys::path::append(path&: Dir, a: getTripleString()); |
| 580 | if (getVFS().exists(Path: Dir)) |
| 581 | return Dir; |
| 582 | Dir = getHexagonTargetDir(InstalledDir: D.Dir, PrefixDirs: D.PrefixDirs); |
| 583 | llvm::sys::path::append(path&: Dir, a: "hexagon" ); |
| 584 | return Dir; |
| 585 | } |
| 586 | |
| 587 | void HexagonToolChain::getLibraryDir(const ArgList &Args, |
| 588 | llvm::SmallString<128> &Dir) const { |
| 589 | bool IsLinuxMusl = getTriple().isMusl() && getTriple().isOSLinux(); |
| 590 | const llvm::SmallString<128> SysRoot = getEffectiveSysRoot(Args); |
| 591 | // Linux toolchain uses "usr/lib" but it also should accept "lib" in case an |
| 592 | // external sysroot is used. Similar logic is for include paths. |
| 593 | if (IsLinuxMusl) { |
| 594 | Dir = SysRoot; |
| 595 | llvm::sys::path::append(path&: Dir, a: "usr" , b: "lib" ); |
| 596 | } |
| 597 | if (!IsLinuxMusl || !getVFS().exists(Path: Dir)) { |
| 598 | Dir = SysRoot; |
| 599 | llvm::sys::path::append(path&: Dir, a: "lib" ); |
| 600 | } |
| 601 | std::string CpuVer = GetTargetCPUVersion(Args).str(); |
| 602 | llvm::sys::path::append(path&: Dir, a: CpuVer); |
| 603 | if (auto G = toolchains::HexagonToolChain::getSmallDataThreshold(Args)) |
| 604 | if (*G == 0) |
| 605 | llvm::sys::path::append(path&: Dir, a: "G0" ); |
| 606 | bool IsStatic = Args.hasArg(Ids: options::OPT_static); |
| 607 | bool IsShared = Args.hasArg(Ids: options::OPT_shared); |
| 608 | if (IsShared && !IsStatic) |
| 609 | llvm::sys::path::append(path&: Dir, a: "pic" ); |
| 610 | } |
| 611 | |
| 612 | void HexagonToolChain::getBaseIncludeDir(const ArgList &Args, |
| 613 | llvm::SmallString<128> &Dir) const { |
| 614 | bool IsLinuxMusl = getTriple().isMusl() && getTriple().isOSLinux(); |
| 615 | const llvm::SmallString<128> SysRoot = getEffectiveSysRoot(Args); |
| 616 | if (IsLinuxMusl) { |
| 617 | Dir = SysRoot; |
| 618 | llvm::sys::path::append(path&: Dir, a: "usr" , b: "include" ); |
| 619 | } |
| 620 | if (!IsLinuxMusl || !getVFS().exists(Path: Dir)) { |
| 621 | Dir = SysRoot; |
| 622 | llvm::sys::path::append(path&: Dir, a: "include" ); |
| 623 | } |
| 624 | } |
| 625 | |
| 626 | std::optional<unsigned> |
| 627 | HexagonToolChain::getSmallDataThreshold(const ArgList &Args) { |
| 628 | StringRef Gn = "" ; |
| 629 | if (Arg *A = Args.getLastArg(Ids: options::OPT_G)) { |
| 630 | Gn = A->getValue(); |
| 631 | } else if (Args.getLastArg(Ids: options::OPT_shared, Ids: options::OPT_fpic, |
| 632 | Ids: options::OPT_fPIC)) { |
| 633 | Gn = "0" ; |
| 634 | } |
| 635 | |
| 636 | unsigned G; |
| 637 | if (!Gn.getAsInteger(Radix: 10, Result&: G)) |
| 638 | return G; |
| 639 | |
| 640 | return std::nullopt; |
| 641 | } |
| 642 | |
| 643 | std::string HexagonToolChain::getCompilerRTPath() const { |
| 644 | SmallString<128> Dir(getDriver().SysRoot); |
| 645 | llvm::sys::path::append(path&: Dir, a: "usr" , b: "lib" ); |
| 646 | if (!SelectedMultilibs.empty()) { |
| 647 | Dir += SelectedMultilibs.back().gccSuffix(); |
| 648 | } |
| 649 | return std::string(Dir); |
| 650 | } |
| 651 | |
| 652 | void HexagonToolChain::getHexagonLibraryPaths(const ArgList &Args, |
| 653 | ToolChain::path_list &LibPaths) const { |
| 654 | const Driver &D = getDriver(); |
| 655 | |
| 656 | //---------------------------------------------------------------------------- |
| 657 | // -L Args |
| 658 | //---------------------------------------------------------------------------- |
| 659 | for (Arg *A : Args.filtered(Ids: options::OPT_L)) |
| 660 | llvm::append_range(C&: LibPaths, R&: A->getValues()); |
| 661 | |
| 662 | //---------------------------------------------------------------------------- |
| 663 | // Other standard paths |
| 664 | //---------------------------------------------------------------------------- |
| 665 | std::vector<std::string> RootDirs; |
| 666 | std::copy(first: D.PrefixDirs.begin(), last: D.PrefixDirs.end(), |
| 667 | result: std::back_inserter(x&: RootDirs)); |
| 668 | |
| 669 | std::string SysRoot(getEffectiveSysRoot(Args)); |
| 670 | if (!llvm::is_contained(Range&: RootDirs, Element: SysRoot)) |
| 671 | RootDirs.push_back(x: SysRoot); |
| 672 | |
| 673 | bool HasPIC = Args.hasArg(Ids: options::OPT_fpic, Ids: options::OPT_fPIC); |
| 674 | // Assume G0 with -shared. |
| 675 | bool HasG0 = Args.hasArg(Ids: options::OPT_shared); |
| 676 | if (auto G = getSmallDataThreshold(Args)) |
| 677 | HasG0 = *G == 0; |
| 678 | |
| 679 | const std::string CpuVer = GetTargetCPUVersion(Args).str(); |
| 680 | for (auto &Dir : RootDirs) { |
| 681 | std::string LibDir = Dir + "/lib" ; |
| 682 | std::string LibDirCpu = LibDir + '/' + CpuVer; |
| 683 | if (HasG0) { |
| 684 | if (HasPIC) |
| 685 | LibPaths.push_back(Elt: LibDirCpu + "/G0/pic" ); |
| 686 | LibPaths.push_back(Elt: LibDirCpu + "/G0" ); |
| 687 | } |
| 688 | LibPaths.push_back(Elt: LibDirCpu); |
| 689 | LibPaths.push_back(Elt: LibDir); |
| 690 | } |
| 691 | } |
| 692 | |
| 693 | HexagonToolChain::HexagonToolChain(const Driver &D, const llvm::Triple &Triple, |
| 694 | const llvm::opt::ArgList &Args) |
| 695 | : Linux(D, Triple, Args) { |
| 696 | ToolChain::path_list &LibPaths = getFilePaths(); |
| 697 | |
| 698 | // Remove paths added by Linux toolchain. Currently Hexagon_TC really targets |
| 699 | // 'elf' OS type, so the Linux paths are not appropriate. When we actually |
| 700 | // support 'linux' we'll need to fix this up |
| 701 | LibPaths.clear(); |
| 702 | getHexagonLibraryPaths(Args, LibPaths); |
| 703 | |
| 704 | if (getTriple().isMusl()) { |
| 705 | Multilibs.push_back(M: Multilib()); |
| 706 | Multilibs.push_back(M: MultilibBuilder("msan" , {}, {}) |
| 707 | .flag(Flag: "-fsanitize=memory" ) |
| 708 | .makeMultilib()); |
| 709 | Multilibs.push_back(M: MultilibBuilder("asan" , {}, {}) |
| 710 | .flag(Flag: "-fsanitize=address" ) |
| 711 | .makeMultilib()); |
| 712 | |
| 713 | Multilib::flags_list Flags; |
| 714 | addMultilibFlag(Enabled: getSanitizerArgs(JobArgs: Args).needsMsanRt(), Flag: "-fsanitize=memory" , |
| 715 | Flags); |
| 716 | addMultilibFlag(Enabled: getSanitizerArgs(JobArgs: Args).needsAsanRt(), Flag: "-fsanitize=address" , |
| 717 | Flags); |
| 718 | |
| 719 | if (Multilibs.select(D, Flags, SelectedMultilibs)) { |
| 720 | Multilib LastSelected = SelectedMultilibs.back(); |
| 721 | SelectedMultilibs = {LastSelected}; |
| 722 | |
| 723 | if (!SelectedMultilibs.back().isDefault()) { |
| 724 | SmallString<128> SanLibPath(D.SysRoot); |
| 725 | llvm::sys::path::append(path&: SanLibPath, a: "usr" , b: "lib" ); |
| 726 | SanLibPath += SelectedMultilibs.back().gccSuffix(); |
| 727 | LibPaths.insert(I: LibPaths.begin(), Elt: std::string(SanLibPath)); |
| 728 | } |
| 729 | } |
| 730 | } |
| 731 | } |
| 732 | |
| 733 | HexagonToolChain::~HexagonToolChain() {} |
| 734 | |
| 735 | void HexagonToolChain::AddCXXStdlibLibArgs(const ArgList &Args, |
| 736 | ArgStringList &CmdArgs) const { |
| 737 | CXXStdlibType Type = GetCXXStdlibType(Args); |
| 738 | ToolChain::UnwindLibType UNW = GetUnwindLibType(Args); |
| 739 | if (UNW != ToolChain::UNW_None && UNW != ToolChain::UNW_CompilerRT) { |
| 740 | const Arg *A = Args.getLastArg(Ids: options::OPT_unwindlib_EQ); |
| 741 | if (A) { |
| 742 | getDriver().Diag(DiagID: diag::err_drv_unsupported_unwind_for_platform) |
| 743 | << A->getValue() << getTripleString(); |
| 744 | return; |
| 745 | } |
| 746 | } |
| 747 | |
| 748 | switch (Type) { |
| 749 | case ToolChain::CST_Libcxx: |
| 750 | CmdArgs.push_back(Elt: "-lc++" ); |
| 751 | if (Args.hasArg(Ids: options::OPT_fexperimental_library)) |
| 752 | CmdArgs.push_back(Elt: "-lc++experimental" ); |
| 753 | CmdArgs.push_back(Elt: "-lc++abi" ); |
| 754 | if (UNW != ToolChain::UNW_None) |
| 755 | CmdArgs.push_back(Elt: "-lunwind" ); |
| 756 | break; |
| 757 | |
| 758 | case ToolChain::CST_Libstdcxx: |
| 759 | CmdArgs.push_back(Elt: "-lstdc++" ); |
| 760 | break; |
| 761 | } |
| 762 | } |
| 763 | |
| 764 | Tool *HexagonToolChain::buildAssembler() const { |
| 765 | return new tools::hexagon::Assembler(*this); |
| 766 | } |
| 767 | |
| 768 | Tool *HexagonToolChain::buildLinker() const { |
| 769 | return new tools::hexagon::Linker(*this); |
| 770 | } |
| 771 | |
| 772 | unsigned HexagonToolChain::getOptimizationLevel( |
| 773 | const llvm::opt::ArgList &DriverArgs) const { |
| 774 | // Copied in large part from lib/Frontend/CompilerInvocation.cpp. |
| 775 | Arg *A = DriverArgs.getLastArg(Ids: options::OPT_O_Group); |
| 776 | if (!A) |
| 777 | return 0; |
| 778 | |
| 779 | if (A->getOption().matches(ID: options::OPT_O0)) |
| 780 | return 0; |
| 781 | if (A->getOption().matches(ID: options::OPT_Ofast) || |
| 782 | A->getOption().matches(ID: options::OPT_O4)) |
| 783 | return 3; |
| 784 | assert(A->getNumValues() != 0); |
| 785 | StringRef S(A->getValue()); |
| 786 | if (S == "s" || S == "z" || S.empty()) |
| 787 | return 2; |
| 788 | if (S == "g" ) |
| 789 | return 1; |
| 790 | |
| 791 | unsigned OptLevel; |
| 792 | if (S.getAsInteger(Radix: 10, Result&: OptLevel)) |
| 793 | return 0; |
| 794 | return OptLevel; |
| 795 | } |
| 796 | |
| 797 | void HexagonToolChain::addClangTargetOptions(const ArgList &DriverArgs, |
| 798 | ArgStringList &CC1Args, |
| 799 | BoundArch BA, |
| 800 | Action::OffloadKind) const { |
| 801 | |
| 802 | bool UseInitArrayDefault = getTriple().isMusl(); |
| 803 | |
| 804 | if (!DriverArgs.hasFlag(Pos: options::OPT_fuse_init_array, |
| 805 | Neg: options::OPT_fno_use_init_array, |
| 806 | Default: UseInitArrayDefault)) |
| 807 | CC1Args.push_back(Elt: "-fno-use-init-array" ); |
| 808 | |
| 809 | static const std::pair<options::ID, const char *> FixedRegs[] = { |
| 810 | {options::OPT_ffixed_r16, "+reserved-r16" }, |
| 811 | {options::OPT_ffixed_r17, "+reserved-r17" }, |
| 812 | {options::OPT_ffixed_r18, "+reserved-r18" }, |
| 813 | {options::OPT_ffixed_r19, "+reserved-r19" }, |
| 814 | {options::OPT_ffixed_r20, "+reserved-r20" }, |
| 815 | {options::OPT_ffixed_r21, "+reserved-r21" }, |
| 816 | {options::OPT_ffixed_r22, "+reserved-r22" }, |
| 817 | {options::OPT_ffixed_r23, "+reserved-r23" }, |
| 818 | {options::OPT_ffixed_r24, "+reserved-r24" }, |
| 819 | {options::OPT_ffixed_r25, "+reserved-r25" }, |
| 820 | {options::OPT_ffixed_r26, "+reserved-r26" }, |
| 821 | {options::OPT_ffixed_r27, "+reserved-r27" }, |
| 822 | {options::OPT_ffixed_r28, "+reserved-r28" }, |
| 823 | }; |
| 824 | for (const auto &[Opt, Feature] : FixedRegs) { |
| 825 | if (DriverArgs.hasArg(Ids: Opt)) { |
| 826 | CC1Args.push_back(Elt: "-target-feature" ); |
| 827 | CC1Args.push_back(Elt: Feature); |
| 828 | } |
| 829 | } |
| 830 | if (isAutoHVXEnabled(Args: DriverArgs)) { |
| 831 | CC1Args.push_back(Elt: "-mllvm" ); |
| 832 | CC1Args.push_back(Elt: "-hexagon-autohvx" ); |
| 833 | } |
| 834 | } |
| 835 | |
| 836 | void HexagonToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs, |
| 837 | ArgStringList &CC1Args) const { |
| 838 | if (DriverArgs.hasArg(Ids: options::OPT_nostdinc)) |
| 839 | return; |
| 840 | |
| 841 | const Driver &D = getDriver(); |
| 842 | const bool UseBuiltins = !DriverArgs.hasArg(Ids: options::OPT_nobuiltininc); |
| 843 | if (UseBuiltins) { |
| 844 | SmallString<128> ResourceDirInclude(D.ResourceDir); |
| 845 | llvm::sys::path::append(path&: ResourceDirInclude, a: "include" ); |
| 846 | addSystemInclude(DriverArgs, CC1Args, Path: ResourceDirInclude); |
| 847 | } |
| 848 | if (!DriverArgs.hasArg(Ids: options::OPT_nostdlibinc)) { |
| 849 | SmallString<128> CIncludeDir; |
| 850 | getBaseIncludeDir(Args: DriverArgs, Dir&: CIncludeDir); |
| 851 | addExternCSystemInclude(DriverArgs, CC1Args, Path: std::string(CIncludeDir)); |
| 852 | bool IsLinuxMusl = getTriple().isMusl() && getTriple().isOSLinux(); |
| 853 | if (IsLinuxMusl) { |
| 854 | SmallString<128> LocalIncludeDir = getEffectiveSysRoot(Args: DriverArgs); |
| 855 | llvm::sys::path::append(path&: LocalIncludeDir, a: "usr" , b: "local" , c: "include" ); |
| 856 | addSystemInclude(DriverArgs, CC1Args, Path: LocalIncludeDir); |
| 857 | } |
| 858 | // TOOL_INCLUDE_DIR |
| 859 | AddMultilibIncludeArgs(DriverArgs, CC1Args); |
| 860 | } |
| 861 | } |
| 862 | |
| 863 | void HexagonToolChain::addLibCxxIncludePaths( |
| 864 | const llvm::opt::ArgList &DriverArgs, |
| 865 | llvm::opt::ArgStringList &CC1Args) const { |
| 866 | SmallString<128> Dir; |
| 867 | getBaseIncludeDir(Args: DriverArgs, Dir); |
| 868 | llvm::sys::path::append(path&: Dir, a: "c++" , b: "v1" ); |
| 869 | addLibStdCXXIncludePaths(IncludeDir: Dir, Triple: "" , IncludeSuffix: "" , DriverArgs, CC1Args); |
| 870 | } |
| 871 | |
| 872 | void HexagonToolChain::addLibStdCxxIncludePaths( |
| 873 | const llvm::opt::ArgList &DriverArgs, |
| 874 | llvm::opt::ArgStringList &CC1Args) const { |
| 875 | SmallString<128> Dir; |
| 876 | getBaseIncludeDir(Args: DriverArgs, Dir); |
| 877 | llvm::sys::path::append(path&: Dir, a: "c++" ); |
| 878 | addLibStdCXXIncludePaths(IncludeDir: Dir, Triple: "" , IncludeSuffix: "" , DriverArgs, CC1Args); |
| 879 | } |
| 880 | |
| 881 | ToolChain::RuntimeLibType |
| 882 | HexagonToolChain::GetRuntimeLibType(const ArgList &Args) const { |
| 883 | if (GetCStdlibType(Args) == ToolChain::CST_Picolibc) { |
| 884 | if (Args.getLastArg(Ids: options::OPT_rtlib_EQ)) |
| 885 | return ToolChain::GetRuntimeLibType(Args); |
| 886 | return ToolChain::RLT_CompilerRT; |
| 887 | } |
| 888 | return ToolChain::GetRuntimeLibType(Args); |
| 889 | } |
| 890 | |
| 891 | ToolChain::UnwindLibType |
| 892 | HexagonToolChain::GetUnwindLibType(const ArgList &Args) const { |
| 893 | if (GetCStdlibType(Args) == ToolChain::CST_Picolibc) { |
| 894 | if (Args.getLastArg(Ids: options::OPT_unwindlib_EQ)) |
| 895 | return ToolChain::GetUnwindLibType(Args); |
| 896 | return ToolChain::UNW_CompilerRT; |
| 897 | } |
| 898 | return ToolChain::GetUnwindLibType(Args); |
| 899 | } |
| 900 | |
| 901 | ToolChain::CXXStdlibType |
| 902 | HexagonToolChain::GetCXXStdlibType(const ArgList &Args) const { |
| 903 | Arg *A = Args.getLastArg(Ids: options::OPT_stdlib_EQ); |
| 904 | if (!A) { |
| 905 | if (getTriple().isMusl() || GetCStdlibType(Args) == ToolChain::CST_Picolibc) |
| 906 | return ToolChain::CST_Libcxx; |
| 907 | else |
| 908 | return ToolChain::CST_Libstdcxx; |
| 909 | } |
| 910 | StringRef Value = A->getValue(); |
| 911 | if (Value != "libstdc++" && Value != "libc++" ) |
| 912 | getDriver().Diag(DiagID: diag::err_drv_invalid_stdlib_name) << A->getAsString(Args); |
| 913 | |
| 914 | if (Value == "libstdc++" ) |
| 915 | return ToolChain::CST_Libstdcxx; |
| 916 | else if (Value == "libc++" ) |
| 917 | return ToolChain::CST_Libcxx; |
| 918 | else |
| 919 | return ToolChain::CST_Libstdcxx; |
| 920 | } |
| 921 | |
| 922 | bool HexagonToolChain::isAutoHVXEnabled(const llvm::opt::ArgList &Args) { |
| 923 | if (Arg *A = Args.getLastArg(Ids: options::OPT_fvectorize, |
| 924 | Ids: options::OPT_fno_vectorize)) |
| 925 | return A->getOption().matches(ID: options::OPT_fvectorize); |
| 926 | return false; |
| 927 | } |
| 928 | |
| 929 | // |
| 930 | // Returns the default CPU for Hexagon. This is the default compilation target |
| 931 | // if no Hexagon processor is selected at the command-line. |
| 932 | // |
| 933 | StringRef HexagonToolChain::GetDefaultCPU() { return "hexagonv68" ; } |
| 934 | |
| 935 | StringRef HexagonToolChain::GetTargetCPUVersion(const ArgList &Args) { |
| 936 | Arg *CpuArg = nullptr; |
| 937 | if (Arg *A = Args.getLastArg(Ids: options::OPT_mcpu_EQ)) |
| 938 | CpuArg = A; |
| 939 | |
| 940 | StringRef CPU = CpuArg ? CpuArg->getValue() : GetDefaultCPU(); |
| 941 | CPU.consume_front(Prefix: "hexagon" ); |
| 942 | return CPU; |
| 943 | } |
| 944 | |
| 945 | std::optional<std::string> |
| 946 | HexagonToolChain::GetHVXVersion(const ArgList &Args) { |
| 947 | // Handle -mh[v]x= and -mno-hvx. If versioned and versionless flags |
| 948 | // are both present, the last one wins. |
| 949 | Arg *HvxEnablingArg = |
| 950 | Args.getLastArg(Ids: options::OPT_mhexagon_hvx, Ids: options::OPT_mhexagon_hvx_EQ, |
| 951 | Ids: options::OPT_mno_hexagon_hvx); |
| 952 | if (!HvxEnablingArg || |
| 953 | HvxEnablingArg->getOption().matches(ID: options::OPT_mno_hexagon_hvx)) |
| 954 | return std::nullopt; |
| 955 | |
| 956 | StringRef Cpu(toolchains::HexagonToolChain::GetTargetCPUVersion(Args)); |
| 957 | std::string HvxVer; |
| 958 | if (!Cpu.empty() && (Cpu.back() == 'T' || Cpu.back() == 't')) |
| 959 | HvxVer = Cpu.drop_back(N: 1).str(); |
| 960 | else |
| 961 | HvxVer = Cpu.str(); |
| 962 | |
| 963 | if (HvxEnablingArg->getOption().matches(ID: options::OPT_mhexagon_hvx_EQ)) |
| 964 | HvxVer = StringRef(HvxEnablingArg->getValue()).lower(); |
| 965 | |
| 966 | return HvxVer; |
| 967 | } |
| 968 | |
| 969 | // End Hexagon |
| 970 | |