blob: a745f8be19ba889f480ebf52e8d6ca43fc6893c8 [file] [log] [blame]
David L. Jonesf561aba2017-03-08 01:02:16 +00001//===--- Hexagon.cpp - Hexagon ToolChain Implementations --------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "Hexagon.h"
11#include "InputInfo.h"
12#include "CommonArgs.h"
13#include "clang/Basic/VirtualFileSystem.h"
14#include "clang/Config/config.h"
15#include "clang/Driver/Compilation.h"
16#include "clang/Driver/Driver.h"
17#include "clang/Driver/DriverDiagnostic.h"
18#include "clang/Driver/Options.h"
19#include "llvm/ADT/StringExtras.h"
20#include "llvm/Option/ArgList.h"
21#include "llvm/Support/FileSystem.h"
22#include "llvm/Support/Path.h"
23
24using namespace clang::driver;
25using namespace clang::driver::tools;
26using namespace clang::driver::toolchains;
27using namespace clang;
28using namespace llvm::opt;
29
Sumanth Gundapaneni2d9aa742017-10-04 19:09:29 +000030// Hexagon target features.
31void hexagon::getHexagonTargetFeatures(const ArgList &Args,
32 std::vector<StringRef> &Features) {
33 handleTargetFeaturesGroup(Args, Features,
34 options::OPT_m_hexagon_Features_Group);
35
36 bool UseLongCalls = false;
37 if (Arg *A = Args.getLastArg(options::OPT_mlong_calls,
38 options::OPT_mno_long_calls)) {
39 if (A->getOption().matches(options::OPT_mlong_calls))
40 UseLongCalls = true;
41 }
42
43 Features.push_back(UseLongCalls ? "+long-calls" : "-long-calls");
44}
45
David L. Jonesf561aba2017-03-08 01:02:16 +000046// Hexagon tools start.
47void hexagon::Assembler::RenderExtraToolArgs(const JobAction &JA,
48 ArgStringList &CmdArgs) const {
49}
50
51void hexagon::Assembler::ConstructJob(Compilation &C, const JobAction &JA,
52 const InputInfo &Output,
53 const InputInfoList &Inputs,
54 const ArgList &Args,
55 const char *LinkingOutput) const {
56 claimNoWarnArgs(Args);
57
58 auto &HTC = static_cast<const toolchains::HexagonToolChain&>(getToolChain());
59 const Driver &D = HTC.getDriver();
60 ArgStringList CmdArgs;
61
62 std::string MArchString = "-march=hexagon";
63 CmdArgs.push_back(Args.MakeArgString(MArchString));
64
65 RenderExtraToolArgs(JA, CmdArgs);
66
67 std::string AsName = "hexagon-llvm-mc";
68 std::string MCpuString = "-mcpu=hexagon" +
69 toolchains::HexagonToolChain::GetTargetCPUVersion(Args).str();
70 CmdArgs.push_back("-filetype=obj");
71 CmdArgs.push_back(Args.MakeArgString(MCpuString));
72
73 if (Output.isFilename()) {
74 CmdArgs.push_back("-o");
75 CmdArgs.push_back(Output.getFilename());
76 } else {
77 assert(Output.isNothing() && "Unexpected output");
78 CmdArgs.push_back("-fsyntax-only");
79 }
80
81 if (auto G = toolchains::HexagonToolChain::getSmallDataThreshold(Args)) {
82 std::string N = llvm::utostr(G.getValue());
83 CmdArgs.push_back(Args.MakeArgString(std::string("-gpsize=") + N));
84 }
85
86 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, options::OPT_Xassembler);
87
88 // Only pass -x if gcc will understand it; otherwise hope gcc
89 // understands the suffix correctly. The main use case this would go
90 // wrong in is for linker inputs if they happened to have an odd
91 // suffix; really the only way to get this to happen is a command
92 // like '-x foobar a.c' which will treat a.c like a linker input.
93 //
94 // FIXME: For the linker case specifically, can we safely convert
95 // inputs into '-Wl,' options?
96 for (const auto &II : Inputs) {
97 // Don't try to pass LLVM or AST inputs to a generic gcc.
98 if (types::isLLVMIR(II.getType()))
99 D.Diag(clang::diag::err_drv_no_linker_llvm_support)
100 << HTC.getTripleString();
101 else if (II.getType() == types::TY_AST)
102 D.Diag(clang::diag::err_drv_no_ast_support)
103 << HTC.getTripleString();
104 else if (II.getType() == types::TY_ModuleFile)
105 D.Diag(diag::err_drv_no_module_support)
106 << HTC.getTripleString();
107
108 if (II.isFilename())
109 CmdArgs.push_back(II.getFilename());
110 else
111 // Don't render as input, we need gcc to do the translations.
112 // FIXME: What is this?
113 II.getInputArg().render(Args, CmdArgs);
114 }
115
116 auto *Exec = Args.MakeArgString(HTC.GetProgramPath(AsName.c_str()));
117 C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs));
118}
119
120void hexagon::Linker::RenderExtraToolArgs(const JobAction &JA,
121 ArgStringList &CmdArgs) const {
122}
123
124static void
125constructHexagonLinkArgs(Compilation &C, const JobAction &JA,
126 const toolchains::HexagonToolChain &HTC,
127 const InputInfo &Output, const InputInfoList &Inputs,
128 const ArgList &Args, ArgStringList &CmdArgs,
129 const char *LinkingOutput) {
130
131 const Driver &D = HTC.getDriver();
132
133 //----------------------------------------------------------------------------
134 //
135 //----------------------------------------------------------------------------
136 bool IsStatic = Args.hasArg(options::OPT_static);
137 bool IsShared = Args.hasArg(options::OPT_shared);
138 bool IsPIE = Args.hasArg(options::OPT_pie);
139 bool IncStdLib = !Args.hasArg(options::OPT_nostdlib);
140 bool IncStartFiles = !Args.hasArg(options::OPT_nostartfiles);
141 bool IncDefLibs = !Args.hasArg(options::OPT_nodefaultlibs);
142 bool UseG0 = false;
143 bool UseShared = IsShared && !IsStatic;
144
145 //----------------------------------------------------------------------------
146 // Silence warnings for various options
147 //----------------------------------------------------------------------------
148 Args.ClaimAllArgs(options::OPT_g_Group);
149 Args.ClaimAllArgs(options::OPT_emit_llvm);
150 Args.ClaimAllArgs(options::OPT_w); // Other warning options are already
151 // handled somewhere else.
152 Args.ClaimAllArgs(options::OPT_static_libgcc);
153
154 //----------------------------------------------------------------------------
155 //
156 //----------------------------------------------------------------------------
157 if (Args.hasArg(options::OPT_s))
158 CmdArgs.push_back("-s");
159
160 if (Args.hasArg(options::OPT_r))
161 CmdArgs.push_back("-r");
162
163 for (const auto &Opt : HTC.ExtraOpts)
164 CmdArgs.push_back(Opt.c_str());
165
166 CmdArgs.push_back("-march=hexagon");
167 std::string CpuVer =
168 toolchains::HexagonToolChain::GetTargetCPUVersion(Args).str();
169 std::string MCpuString = "-mcpu=hexagon" + CpuVer;
170 CmdArgs.push_back(Args.MakeArgString(MCpuString));
171
172 if (IsShared) {
173 CmdArgs.push_back("-shared");
174 // The following should be the default, but doing as hexagon-gcc does.
175 CmdArgs.push_back("-call_shared");
176 }
177
178 if (IsStatic)
179 CmdArgs.push_back("-static");
180
181 if (IsPIE && !IsShared)
182 CmdArgs.push_back("-pie");
183
184 if (auto G = toolchains::HexagonToolChain::getSmallDataThreshold(Args)) {
185 std::string N = llvm::utostr(G.getValue());
186 CmdArgs.push_back(Args.MakeArgString(std::string("-G") + N));
187 UseG0 = G.getValue() == 0;
188 }
189
190 //----------------------------------------------------------------------------
191 //
192 //----------------------------------------------------------------------------
193 CmdArgs.push_back("-o");
194 CmdArgs.push_back(Output.getFilename());
195
196 //----------------------------------------------------------------------------
197 // moslib
198 //----------------------------------------------------------------------------
199 std::vector<std::string> OsLibs;
200 bool HasStandalone = false;
201
202 for (const Arg *A : Args.filtered(options::OPT_moslib_EQ)) {
203 A->claim();
204 OsLibs.emplace_back(A->getValue());
205 HasStandalone = HasStandalone || (OsLibs.back() == "standalone");
206 }
207 if (OsLibs.empty()) {
208 OsLibs.push_back("standalone");
209 HasStandalone = true;
210 }
211
212 //----------------------------------------------------------------------------
213 // Start Files
214 //----------------------------------------------------------------------------
215 const std::string MCpuSuffix = "/" + CpuVer;
216 const std::string MCpuG0Suffix = MCpuSuffix + "/G0";
217 const std::string RootDir =
218 HTC.getHexagonTargetDir(D.InstalledDir, D.PrefixDirs) + "/";
219 const std::string StartSubDir =
220 "hexagon/lib" + (UseG0 ? MCpuG0Suffix : MCpuSuffix);
221
222 auto Find = [&HTC] (const std::string &RootDir, const std::string &SubDir,
223 const char *Name) -> std::string {
224 std::string RelName = SubDir + Name;
225 std::string P = HTC.GetFilePath(RelName.c_str());
226 if (llvm::sys::fs::exists(P))
227 return P;
228 return RootDir + RelName;
229 };
230
231 if (IncStdLib && IncStartFiles) {
232 if (!IsShared) {
233 if (HasStandalone) {
234 std::string Crt0SA = Find(RootDir, StartSubDir, "/crt0_standalone.o");
235 CmdArgs.push_back(Args.MakeArgString(Crt0SA));
236 }
237 std::string Crt0 = Find(RootDir, StartSubDir, "/crt0.o");
238 CmdArgs.push_back(Args.MakeArgString(Crt0));
239 }
240 std::string Init = UseShared
241 ? Find(RootDir, StartSubDir + "/pic", "/initS.o")
242 : Find(RootDir, StartSubDir, "/init.o");
243 CmdArgs.push_back(Args.MakeArgString(Init));
244 }
245
246 //----------------------------------------------------------------------------
247 // Library Search Paths
248 //----------------------------------------------------------------------------
249 const ToolChain::path_list &LibPaths = HTC.getFilePaths();
250 for (const auto &LibPath : LibPaths)
251 CmdArgs.push_back(Args.MakeArgString(StringRef("-L") + LibPath));
252
253 //----------------------------------------------------------------------------
254 //
255 //----------------------------------------------------------------------------
256 Args.AddAllArgs(CmdArgs,
257 {options::OPT_T_Group, options::OPT_e, options::OPT_s,
258 options::OPT_t, options::OPT_u_Group});
259
260 AddLinkerInputs(HTC, Inputs, Args, CmdArgs, JA);
261
262 //----------------------------------------------------------------------------
263 // Libraries
264 //----------------------------------------------------------------------------
265 if (IncStdLib && IncDefLibs) {
266 if (D.CCCIsCXX()) {
Nico Weber0ee47d92017-07-25 18:02:57 +0000267 if (HTC.ShouldLinkCXXStdlib(Args))
268 HTC.AddCXXStdlibLibArgs(Args, CmdArgs);
David L. Jonesf561aba2017-03-08 01:02:16 +0000269 CmdArgs.push_back("-lm");
270 }
271
272 CmdArgs.push_back("--start-group");
273
274 if (!IsShared) {
275 for (const std::string &Lib : OsLibs)
276 CmdArgs.push_back(Args.MakeArgString("-l" + Lib));
277 CmdArgs.push_back("-lc");
278 }
279 CmdArgs.push_back("-lgcc");
280
281 CmdArgs.push_back("--end-group");
282 }
283
284 //----------------------------------------------------------------------------
285 // End files
286 //----------------------------------------------------------------------------
287 if (IncStdLib && IncStartFiles) {
288 std::string Fini = UseShared
289 ? Find(RootDir, StartSubDir + "/pic", "/finiS.o")
290 : Find(RootDir, StartSubDir, "/fini.o");
291 CmdArgs.push_back(Args.MakeArgString(Fini));
292 }
293}
294
295void hexagon::Linker::ConstructJob(Compilation &C, const JobAction &JA,
296 const InputInfo &Output,
297 const InputInfoList &Inputs,
298 const ArgList &Args,
299 const char *LinkingOutput) const {
300 auto &HTC = static_cast<const toolchains::HexagonToolChain&>(getToolChain());
301
302 ArgStringList CmdArgs;
303 constructHexagonLinkArgs(C, JA, HTC, Output, Inputs, Args, CmdArgs,
304 LinkingOutput);
305
306 std::string Linker = HTC.GetProgramPath("hexagon-link");
307 C.addCommand(llvm::make_unique<Command>(JA, *this, Args.MakeArgString(Linker),
308 CmdArgs, Inputs));
309}
310// Hexagon tools end.
311
312/// Hexagon Toolchain
313
314std::string HexagonToolChain::getHexagonTargetDir(
315 const std::string &InstalledDir,
316 const SmallVectorImpl<std::string> &PrefixDirs) const {
317 std::string InstallRelDir;
318 const Driver &D = getDriver();
319
320 // Locate the rest of the toolchain ...
321 for (auto &I : PrefixDirs)
322 if (D.getVFS().exists(I))
323 return I;
324
325 if (getVFS().exists(InstallRelDir = InstalledDir + "/../target"))
326 return InstallRelDir;
327
328 return InstalledDir;
329}
330
331Optional<unsigned> HexagonToolChain::getSmallDataThreshold(
332 const ArgList &Args) {
333 StringRef Gn = "";
334 if (Arg *A = Args.getLastArg(options::OPT_G)) {
335 Gn = A->getValue();
336 } else if (Args.getLastArg(options::OPT_shared, options::OPT_fpic,
337 options::OPT_fPIC)) {
338 Gn = "0";
339 }
340
341 unsigned G;
342 if (!Gn.getAsInteger(10, G))
343 return G;
344
345 return None;
346}
347
348void HexagonToolChain::getHexagonLibraryPaths(const ArgList &Args,
349 ToolChain::path_list &LibPaths) const {
350 const Driver &D = getDriver();
351
352 //----------------------------------------------------------------------------
353 // -L Args
354 //----------------------------------------------------------------------------
355 for (Arg *A : Args.filtered(options::OPT_L))
356 for (const char *Value : A->getValues())
357 LibPaths.push_back(Value);
358
359 //----------------------------------------------------------------------------
360 // Other standard paths
361 //----------------------------------------------------------------------------
362 std::vector<std::string> RootDirs;
363 std::copy(D.PrefixDirs.begin(), D.PrefixDirs.end(),
364 std::back_inserter(RootDirs));
365
366 std::string TargetDir = getHexagonTargetDir(D.getInstalledDir(),
367 D.PrefixDirs);
368 if (std::find(RootDirs.begin(), RootDirs.end(), TargetDir) == RootDirs.end())
369 RootDirs.push_back(TargetDir);
370
371 bool HasPIC = Args.hasArg(options::OPT_fpic, options::OPT_fPIC);
372 // Assume G0 with -shared.
373 bool HasG0 = Args.hasArg(options::OPT_shared);
374 if (auto G = getSmallDataThreshold(Args))
375 HasG0 = G.getValue() == 0;
376
377 const std::string CpuVer = GetTargetCPUVersion(Args).str();
378 for (auto &Dir : RootDirs) {
379 std::string LibDir = Dir + "/hexagon/lib";
380 std::string LibDirCpu = LibDir + '/' + CpuVer;
381 if (HasG0) {
382 if (HasPIC)
383 LibPaths.push_back(LibDirCpu + "/G0/pic");
384 LibPaths.push_back(LibDirCpu + "/G0");
385 }
386 LibPaths.push_back(LibDirCpu);
387 LibPaths.push_back(LibDir);
388 }
389}
390
391HexagonToolChain::HexagonToolChain(const Driver &D, const llvm::Triple &Triple,
392 const llvm::opt::ArgList &Args)
393 : Linux(D, Triple, Args) {
394 const std::string TargetDir = getHexagonTargetDir(D.getInstalledDir(),
395 D.PrefixDirs);
396
397 // Note: Generic_GCC::Generic_GCC adds InstalledDir and getDriver().Dir to
398 // program paths
399 const std::string BinDir(TargetDir + "/bin");
400 if (D.getVFS().exists(BinDir))
401 getProgramPaths().push_back(BinDir);
402
403 ToolChain::path_list &LibPaths = getFilePaths();
404
405 // Remove paths added by Linux toolchain. Currently Hexagon_TC really targets
406 // 'elf' OS type, so the Linux paths are not appropriate. When we actually
407 // support 'linux' we'll need to fix this up
408 LibPaths.clear();
409 getHexagonLibraryPaths(Args, LibPaths);
410}
411
412HexagonToolChain::~HexagonToolChain() {}
413
414Tool *HexagonToolChain::buildAssembler() const {
415 return new tools::hexagon::Assembler(*this);
416}
417
418Tool *HexagonToolChain::buildLinker() const {
419 return new tools::hexagon::Linker(*this);
420}
421
Krzysztof Parzyszekdcda9452017-04-25 20:51:51 +0000422unsigned HexagonToolChain::getOptimizationLevel(
423 const llvm::opt::ArgList &DriverArgs) const {
424 // Copied in large part from lib/Frontend/CompilerInvocation.cpp.
425 Arg *A = DriverArgs.getLastArg(options::OPT_O_Group);
426 if (!A)
427 return 0;
428
429 if (A->getOption().matches(options::OPT_O0))
430 return 0;
Krzysztof Parzyszekd6d41272017-04-25 21:31:55 +0000431 if (A->getOption().matches(options::OPT_Ofast) ||
432 A->getOption().matches(options::OPT_O4))
Krzysztof Parzyszekdcda9452017-04-25 20:51:51 +0000433 return 3;
434 assert(A->getNumValues() != 0);
435 StringRef S(A->getValue());
436 if (S == "s" || S == "z" || S.empty())
437 return 2;
438 if (S == "g")
439 return 1;
440
441 unsigned OptLevel;
442 if (S.getAsInteger(10, OptLevel))
443 return 0;
444 return OptLevel;
445}
446
447void HexagonToolChain::addClangTargetOptions(const ArgList &DriverArgs,
Gheorghe-Teodor Berceaf0f29602017-07-06 16:22:21 +0000448 ArgStringList &CC1Args,
449 Action::OffloadKind) const {
Krzysztof Parzyszekdcda9452017-04-25 20:51:51 +0000450 if (DriverArgs.hasArg(options::OPT_ffp_contract))
451 return;
452 unsigned OptLevel = getOptimizationLevel(DriverArgs);
453 if (OptLevel >= 3)
454 CC1Args.push_back("-ffp-contract=fast");
455}
456
David L. Jonesf561aba2017-03-08 01:02:16 +0000457void HexagonToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
458 ArgStringList &CC1Args) const {
459 if (DriverArgs.hasArg(options::OPT_nostdinc) ||
460 DriverArgs.hasArg(options::OPT_nostdlibinc))
461 return;
462
463 const Driver &D = getDriver();
464 std::string TargetDir = getHexagonTargetDir(D.getInstalledDir(),
465 D.PrefixDirs);
466 addExternCSystemInclude(DriverArgs, CC1Args, TargetDir + "/hexagon/include");
467}
468
469
470void HexagonToolChain::addLibStdCxxIncludePaths(
471 const llvm::opt::ArgList &DriverArgs,
472 llvm::opt::ArgStringList &CC1Args) const {
473 const Driver &D = getDriver();
474 std::string TargetDir = getHexagonTargetDir(D.InstalledDir, D.PrefixDirs);
475 addLibStdCXXIncludePaths(TargetDir, "/hexagon/include/c++", "", "", "", "",
476 DriverArgs, CC1Args);
477}
478
479ToolChain::CXXStdlibType
480HexagonToolChain::GetCXXStdlibType(const ArgList &Args) const {
481 Arg *A = Args.getLastArg(options::OPT_stdlib_EQ);
482 if (!A)
483 return ToolChain::CST_Libstdcxx;
484
485 StringRef Value = A->getValue();
486 if (Value != "libstdc++")
487 getDriver().Diag(diag::err_drv_invalid_stdlib_name) << A->getAsString(Args);
488
489 return ToolChain::CST_Libstdcxx;
490}
491
492//
493// Returns the default CPU for Hexagon. This is the default compilation target
494// if no Hexagon processor is selected at the command-line.
495//
496const StringRef HexagonToolChain::GetDefaultCPU() {
497 return "hexagonv60";
498}
499
500const StringRef HexagonToolChain::GetTargetCPUVersion(const ArgList &Args) {
501 Arg *CpuArg = nullptr;
502 if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ, options::OPT_march_EQ))
503 CpuArg = A;
504
505 StringRef CPU = CpuArg ? CpuArg->getValue() : GetDefaultCPU();
506 if (CPU.startswith("hexagon"))
507 return CPU.substr(sizeof("hexagon") - 1);
508 return CPU;
509}