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