Targets.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765
  1. //===--- Targets.cpp - Implement target feature support -------------------===//
  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. // This file implements construction of a TargetInfo object from a
  10. // target triple.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "Targets.h"
  14. #include "Targets/AArch64.h"
  15. #include "Targets/AMDGPU.h"
  16. #include "Targets/ARC.h"
  17. #include "Targets/ARM.h"
  18. #include "Targets/AVR.h"
  19. #include "Targets/BPF.h"
  20. #include "Targets/Hexagon.h"
  21. #include "Targets/Lanai.h"
  22. #include "Targets/Le64.h"
  23. #include "Targets/M68k.h"
  24. #include "Targets/MSP430.h"
  25. #include "Targets/Mips.h"
  26. #include "Targets/NVPTX.h"
  27. #include "Targets/OSTargets.h"
  28. #include "Targets/PNaCl.h"
  29. #include "Targets/PPC.h"
  30. #include "Targets/RISCV.h"
  31. #include "Targets/SPIR.h"
  32. #include "Targets/Sparc.h"
  33. #include "Targets/SystemZ.h"
  34. #include "Targets/TCE.h"
  35. #include "Targets/VE.h"
  36. #include "Targets/WebAssembly.h"
  37. #include "Targets/X86.h"
  38. #include "Targets/XCore.h"
  39. #include "clang/Basic/Diagnostic.h"
  40. #include "llvm/ADT/StringExtras.h"
  41. #include "llvm/ADT/Triple.h"
  42. using namespace clang;
  43. namespace clang {
  44. namespace targets {
  45. //===----------------------------------------------------------------------===//
  46. // Common code shared among targets.
  47. //===----------------------------------------------------------------------===//
  48. /// DefineStd - Define a macro name and standard variants. For example if
  49. /// MacroName is "unix", then this will define "__unix", "__unix__", and "unix"
  50. /// when in GNU mode.
  51. void DefineStd(MacroBuilder &Builder, StringRef MacroName,
  52. const LangOptions &Opts) {
  53. assert(MacroName[0] != '_' && "Identifier should be in the user's namespace");
  54. // If in GNU mode (e.g. -std=gnu99 but not -std=c99) define the raw identifier
  55. // in the user's namespace.
  56. if (Opts.GNUMode)
  57. Builder.defineMacro(MacroName);
  58. // Define __unix.
  59. Builder.defineMacro("__" + MacroName);
  60. // Define __unix__.
  61. Builder.defineMacro("__" + MacroName + "__");
  62. }
  63. void defineCPUMacros(MacroBuilder &Builder, StringRef CPUName, bool Tuning) {
  64. Builder.defineMacro("__" + CPUName);
  65. Builder.defineMacro("__" + CPUName + "__");
  66. if (Tuning)
  67. Builder.defineMacro("__tune_" + CPUName + "__");
  68. }
  69. void addCygMingDefines(const LangOptions &Opts, MacroBuilder &Builder) {
  70. // Mingw and cygwin define __declspec(a) to __attribute__((a)). Clang
  71. // supports __declspec natively under -fms-extensions, but we define a no-op
  72. // __declspec macro anyway for pre-processor compatibility.
  73. if (Opts.MicrosoftExt)
  74. Builder.defineMacro("__declspec", "__declspec");
  75. else
  76. Builder.defineMacro("__declspec(a)", "__attribute__((a))");
  77. if (!Opts.MicrosoftExt) {
  78. // Provide macros for all the calling convention keywords. Provide both
  79. // single and double underscore prefixed variants. These are available on
  80. // x64 as well as x86, even though they have no effect.
  81. const char *CCs[] = {"cdecl", "stdcall", "fastcall", "thiscall", "pascal"};
  82. for (const char *CC : CCs) {
  83. std::string GCCSpelling = "__attribute__((__";
  84. GCCSpelling += CC;
  85. GCCSpelling += "__))";
  86. Builder.defineMacro(Twine("_") + CC, GCCSpelling);
  87. Builder.defineMacro(Twine("__") + CC, GCCSpelling);
  88. }
  89. }
  90. }
  91. //===----------------------------------------------------------------------===//
  92. // Driver code
  93. //===----------------------------------------------------------------------===//
  94. TargetInfo *AllocateTarget(const llvm::Triple &Triple,
  95. const TargetOptions &Opts) {
  96. llvm::Triple::OSType os = Triple.getOS();
  97. switch (Triple.getArch()) {
  98. default:
  99. return nullptr;
  100. case llvm::Triple::arc:
  101. return new ARCTargetInfo(Triple, Opts);
  102. case llvm::Triple::xcore:
  103. return new XCoreTargetInfo(Triple, Opts);
  104. case llvm::Triple::hexagon:
  105. if (os == llvm::Triple::Linux &&
  106. Triple.getEnvironment() == llvm::Triple::Musl)
  107. return new LinuxTargetInfo<HexagonTargetInfo>(Triple, Opts);
  108. return new HexagonTargetInfo(Triple, Opts);
  109. case llvm::Triple::lanai:
  110. return new LanaiTargetInfo(Triple, Opts);
  111. case llvm::Triple::aarch64_32:
  112. if (Triple.isOSDarwin())
  113. return new DarwinAArch64TargetInfo(Triple, Opts);
  114. return nullptr;
  115. case llvm::Triple::aarch64:
  116. if (Triple.isOSDarwin())
  117. return new DarwinAArch64TargetInfo(Triple, Opts);
  118. switch (os) {
  119. case llvm::Triple::CloudABI:
  120. return new CloudABITargetInfo<AArch64leTargetInfo>(Triple, Opts);
  121. case llvm::Triple::FreeBSD:
  122. return new FreeBSDTargetInfo<AArch64leTargetInfo>(Triple, Opts);
  123. case llvm::Triple::Fuchsia:
  124. return new FuchsiaTargetInfo<AArch64leTargetInfo>(Triple, Opts);
  125. case llvm::Triple::Linux:
  126. return new LinuxTargetInfo<AArch64leTargetInfo>(Triple, Opts);
  127. case llvm::Triple::NetBSD:
  128. return new NetBSDTargetInfo<AArch64leTargetInfo>(Triple, Opts);
  129. case llvm::Triple::OpenBSD:
  130. return new OpenBSDTargetInfo<AArch64leTargetInfo>(Triple, Opts);
  131. case llvm::Triple::Win32:
  132. switch (Triple.getEnvironment()) {
  133. case llvm::Triple::GNU:
  134. return new MinGWARM64TargetInfo(Triple, Opts);
  135. case llvm::Triple::MSVC:
  136. default: // Assume MSVC for unknown environments
  137. return new MicrosoftARM64TargetInfo(Triple, Opts);
  138. }
  139. default:
  140. return new AArch64leTargetInfo(Triple, Opts);
  141. }
  142. case llvm::Triple::aarch64_be:
  143. switch (os) {
  144. case llvm::Triple::FreeBSD:
  145. return new FreeBSDTargetInfo<AArch64beTargetInfo>(Triple, Opts);
  146. case llvm::Triple::Fuchsia:
  147. return new FuchsiaTargetInfo<AArch64beTargetInfo>(Triple, Opts);
  148. case llvm::Triple::Linux:
  149. return new LinuxTargetInfo<AArch64beTargetInfo>(Triple, Opts);
  150. case llvm::Triple::NetBSD:
  151. return new NetBSDTargetInfo<AArch64beTargetInfo>(Triple, Opts);
  152. default:
  153. return new AArch64beTargetInfo(Triple, Opts);
  154. }
  155. case llvm::Triple::arm:
  156. case llvm::Triple::thumb:
  157. if (Triple.isOSBinFormatMachO())
  158. return new DarwinARMTargetInfo(Triple, Opts);
  159. switch (os) {
  160. case llvm::Triple::CloudABI:
  161. return new CloudABITargetInfo<ARMleTargetInfo>(Triple, Opts);
  162. case llvm::Triple::Linux:
  163. return new LinuxTargetInfo<ARMleTargetInfo>(Triple, Opts);
  164. case llvm::Triple::FreeBSD:
  165. return new FreeBSDTargetInfo<ARMleTargetInfo>(Triple, Opts);
  166. case llvm::Triple::NetBSD:
  167. return new NetBSDTargetInfo<ARMleTargetInfo>(Triple, Opts);
  168. case llvm::Triple::OpenBSD:
  169. return new OpenBSDTargetInfo<ARMleTargetInfo>(Triple, Opts);
  170. case llvm::Triple::RTEMS:
  171. return new RTEMSTargetInfo<ARMleTargetInfo>(Triple, Opts);
  172. case llvm::Triple::NaCl:
  173. return new NaClTargetInfo<ARMleTargetInfo>(Triple, Opts);
  174. case llvm::Triple::Win32:
  175. switch (Triple.getEnvironment()) {
  176. case llvm::Triple::Cygnus:
  177. return new CygwinARMTargetInfo(Triple, Opts);
  178. case llvm::Triple::GNU:
  179. return new MinGWARMTargetInfo(Triple, Opts);
  180. case llvm::Triple::Itanium:
  181. return new ItaniumWindowsARMleTargetInfo(Triple, Opts);
  182. case llvm::Triple::MSVC:
  183. default: // Assume MSVC for unknown environments
  184. return new MicrosoftARMleTargetInfo(Triple, Opts);
  185. }
  186. default:
  187. return new ARMleTargetInfo(Triple, Opts);
  188. }
  189. case llvm::Triple::armeb:
  190. case llvm::Triple::thumbeb:
  191. if (Triple.isOSDarwin())
  192. return new DarwinARMTargetInfo(Triple, Opts);
  193. switch (os) {
  194. case llvm::Triple::Linux:
  195. return new LinuxTargetInfo<ARMbeTargetInfo>(Triple, Opts);
  196. case llvm::Triple::FreeBSD:
  197. return new FreeBSDTargetInfo<ARMbeTargetInfo>(Triple, Opts);
  198. case llvm::Triple::NetBSD:
  199. return new NetBSDTargetInfo<ARMbeTargetInfo>(Triple, Opts);
  200. case llvm::Triple::OpenBSD:
  201. return new OpenBSDTargetInfo<ARMbeTargetInfo>(Triple, Opts);
  202. case llvm::Triple::RTEMS:
  203. return new RTEMSTargetInfo<ARMbeTargetInfo>(Triple, Opts);
  204. case llvm::Triple::NaCl:
  205. return new NaClTargetInfo<ARMbeTargetInfo>(Triple, Opts);
  206. default:
  207. return new ARMbeTargetInfo(Triple, Opts);
  208. }
  209. case llvm::Triple::avr:
  210. return new AVRTargetInfo(Triple, Opts);
  211. case llvm::Triple::bpfeb:
  212. case llvm::Triple::bpfel:
  213. return new BPFTargetInfo(Triple, Opts);
  214. case llvm::Triple::msp430:
  215. return new MSP430TargetInfo(Triple, Opts);
  216. case llvm::Triple::mips:
  217. switch (os) {
  218. case llvm::Triple::Linux:
  219. return new LinuxTargetInfo<MipsTargetInfo>(Triple, Opts);
  220. case llvm::Triple::RTEMS:
  221. return new RTEMSTargetInfo<MipsTargetInfo>(Triple, Opts);
  222. case llvm::Triple::FreeBSD:
  223. return new FreeBSDTargetInfo<MipsTargetInfo>(Triple, Opts);
  224. case llvm::Triple::NetBSD:
  225. return new NetBSDTargetInfo<MipsTargetInfo>(Triple, Opts);
  226. default:
  227. return new MipsTargetInfo(Triple, Opts);
  228. }
  229. case llvm::Triple::mipsel:
  230. switch (os) {
  231. case llvm::Triple::Linux:
  232. return new LinuxTargetInfo<MipsTargetInfo>(Triple, Opts);
  233. case llvm::Triple::RTEMS:
  234. return new RTEMSTargetInfo<MipsTargetInfo>(Triple, Opts);
  235. case llvm::Triple::FreeBSD:
  236. return new FreeBSDTargetInfo<MipsTargetInfo>(Triple, Opts);
  237. case llvm::Triple::NetBSD:
  238. return new NetBSDTargetInfo<MipsTargetInfo>(Triple, Opts);
  239. case llvm::Triple::NaCl:
  240. return new NaClTargetInfo<NaClMips32TargetInfo>(Triple, Opts);
  241. default:
  242. return new MipsTargetInfo(Triple, Opts);
  243. }
  244. case llvm::Triple::mips64:
  245. switch (os) {
  246. case llvm::Triple::Linux:
  247. return new LinuxTargetInfo<MipsTargetInfo>(Triple, Opts);
  248. case llvm::Triple::RTEMS:
  249. return new RTEMSTargetInfo<MipsTargetInfo>(Triple, Opts);
  250. case llvm::Triple::FreeBSD:
  251. return new FreeBSDTargetInfo<MipsTargetInfo>(Triple, Opts);
  252. case llvm::Triple::NetBSD:
  253. return new NetBSDTargetInfo<MipsTargetInfo>(Triple, Opts);
  254. case llvm::Triple::OpenBSD:
  255. return new OpenBSDTargetInfo<MipsTargetInfo>(Triple, Opts);
  256. default:
  257. return new MipsTargetInfo(Triple, Opts);
  258. }
  259. case llvm::Triple::mips64el:
  260. switch (os) {
  261. case llvm::Triple::Linux:
  262. return new LinuxTargetInfo<MipsTargetInfo>(Triple, Opts);
  263. case llvm::Triple::RTEMS:
  264. return new RTEMSTargetInfo<MipsTargetInfo>(Triple, Opts);
  265. case llvm::Triple::FreeBSD:
  266. return new FreeBSDTargetInfo<MipsTargetInfo>(Triple, Opts);
  267. case llvm::Triple::NetBSD:
  268. return new NetBSDTargetInfo<MipsTargetInfo>(Triple, Opts);
  269. case llvm::Triple::OpenBSD:
  270. return new OpenBSDTargetInfo<MipsTargetInfo>(Triple, Opts);
  271. default:
  272. return new MipsTargetInfo(Triple, Opts);
  273. }
  274. case llvm::Triple::m68k:
  275. switch (os) {
  276. case llvm::Triple::Linux:
  277. return new LinuxTargetInfo<M68kTargetInfo>(Triple, Opts);
  278. case llvm::Triple::NetBSD:
  279. return new NetBSDTargetInfo<M68kTargetInfo>(Triple, Opts);
  280. default:
  281. return new M68kTargetInfo(Triple, Opts);
  282. }
  283. case llvm::Triple::le32:
  284. switch (os) {
  285. case llvm::Triple::NaCl:
  286. return new NaClTargetInfo<PNaClTargetInfo>(Triple, Opts);
  287. default:
  288. return nullptr;
  289. }
  290. case llvm::Triple::le64:
  291. return new Le64TargetInfo(Triple, Opts);
  292. case llvm::Triple::ppc:
  293. if (Triple.isOSDarwin())
  294. return new DarwinPPC32TargetInfo(Triple, Opts);
  295. switch (os) {
  296. case llvm::Triple::Linux:
  297. return new LinuxTargetInfo<PPC32TargetInfo>(Triple, Opts);
  298. case llvm::Triple::FreeBSD:
  299. return new FreeBSDTargetInfo<PPC32TargetInfo>(Triple, Opts);
  300. case llvm::Triple::NetBSD:
  301. return new NetBSDTargetInfo<PPC32TargetInfo>(Triple, Opts);
  302. case llvm::Triple::OpenBSD:
  303. return new OpenBSDTargetInfo<PPC32TargetInfo>(Triple, Opts);
  304. case llvm::Triple::RTEMS:
  305. return new RTEMSTargetInfo<PPC32TargetInfo>(Triple, Opts);
  306. case llvm::Triple::AIX:
  307. return new AIXPPC32TargetInfo(Triple, Opts);
  308. default:
  309. return new PPC32TargetInfo(Triple, Opts);
  310. }
  311. case llvm::Triple::ppcle:
  312. switch (os) {
  313. case llvm::Triple::Linux:
  314. return new LinuxTargetInfo<PPC32TargetInfo>(Triple, Opts);
  315. case llvm::Triple::FreeBSD:
  316. return new FreeBSDTargetInfo<PPC32TargetInfo>(Triple, Opts);
  317. default:
  318. return new PPC32TargetInfo(Triple, Opts);
  319. }
  320. case llvm::Triple::ppc64:
  321. if (Triple.isOSDarwin())
  322. return new DarwinPPC64TargetInfo(Triple, Opts);
  323. switch (os) {
  324. case llvm::Triple::Linux:
  325. return new LinuxTargetInfo<PPC64TargetInfo>(Triple, Opts);
  326. case llvm::Triple::Lv2:
  327. return new PS3PPUTargetInfo<PPC64TargetInfo>(Triple, Opts);
  328. case llvm::Triple::FreeBSD:
  329. return new FreeBSDTargetInfo<PPC64TargetInfo>(Triple, Opts);
  330. case llvm::Triple::NetBSD:
  331. return new NetBSDTargetInfo<PPC64TargetInfo>(Triple, Opts);
  332. case llvm::Triple::OpenBSD:
  333. return new OpenBSDTargetInfo<PPC64TargetInfo>(Triple, Opts);
  334. case llvm::Triple::AIX:
  335. return new AIXPPC64TargetInfo(Triple, Opts);
  336. default:
  337. return new PPC64TargetInfo(Triple, Opts);
  338. }
  339. case llvm::Triple::ppc64le:
  340. switch (os) {
  341. case llvm::Triple::Linux:
  342. return new LinuxTargetInfo<PPC64TargetInfo>(Triple, Opts);
  343. case llvm::Triple::FreeBSD:
  344. return new FreeBSDTargetInfo<PPC64TargetInfo>(Triple, Opts);
  345. case llvm::Triple::NetBSD:
  346. return new NetBSDTargetInfo<PPC64TargetInfo>(Triple, Opts);
  347. case llvm::Triple::OpenBSD:
  348. return new OpenBSDTargetInfo<PPC64TargetInfo>(Triple, Opts);
  349. default:
  350. return new PPC64TargetInfo(Triple, Opts);
  351. }
  352. case llvm::Triple::nvptx:
  353. return new NVPTXTargetInfo(Triple, Opts, /*TargetPointerWidth=*/32);
  354. case llvm::Triple::nvptx64:
  355. return new NVPTXTargetInfo(Triple, Opts, /*TargetPointerWidth=*/64);
  356. case llvm::Triple::amdgcn:
  357. case llvm::Triple::r600:
  358. return new AMDGPUTargetInfo(Triple, Opts);
  359. case llvm::Triple::riscv32:
  360. // TODO: add cases for NetBSD, RTEMS once tested.
  361. switch (os) {
  362. case llvm::Triple::FreeBSD:
  363. return new FreeBSDTargetInfo<RISCV32TargetInfo>(Triple, Opts);
  364. case llvm::Triple::Linux:
  365. return new LinuxTargetInfo<RISCV32TargetInfo>(Triple, Opts);
  366. default:
  367. return new RISCV32TargetInfo(Triple, Opts);
  368. }
  369. case llvm::Triple::riscv64:
  370. // TODO: add cases for NetBSD, RTEMS once tested.
  371. switch (os) {
  372. case llvm::Triple::FreeBSD:
  373. return new FreeBSDTargetInfo<RISCV64TargetInfo>(Triple, Opts);
  374. case llvm::Triple::OpenBSD:
  375. return new OpenBSDTargetInfo<RISCV64TargetInfo>(Triple, Opts);
  376. case llvm::Triple::Fuchsia:
  377. return new FuchsiaTargetInfo<RISCV64TargetInfo>(Triple, Opts);
  378. case llvm::Triple::Linux:
  379. return new LinuxTargetInfo<RISCV64TargetInfo>(Triple, Opts);
  380. default:
  381. return new RISCV64TargetInfo(Triple, Opts);
  382. }
  383. case llvm::Triple::sparc:
  384. switch (os) {
  385. case llvm::Triple::Linux:
  386. return new LinuxTargetInfo<SparcV8TargetInfo>(Triple, Opts);
  387. case llvm::Triple::Solaris:
  388. return new SolarisTargetInfo<SparcV8TargetInfo>(Triple, Opts);
  389. case llvm::Triple::NetBSD:
  390. return new NetBSDTargetInfo<SparcV8TargetInfo>(Triple, Opts);
  391. case llvm::Triple::RTEMS:
  392. return new RTEMSTargetInfo<SparcV8TargetInfo>(Triple, Opts);
  393. default:
  394. return new SparcV8TargetInfo(Triple, Opts);
  395. }
  396. // The 'sparcel' architecture copies all the above cases except for Solaris.
  397. case llvm::Triple::sparcel:
  398. switch (os) {
  399. case llvm::Triple::Linux:
  400. return new LinuxTargetInfo<SparcV8elTargetInfo>(Triple, Opts);
  401. case llvm::Triple::NetBSD:
  402. return new NetBSDTargetInfo<SparcV8elTargetInfo>(Triple, Opts);
  403. case llvm::Triple::RTEMS:
  404. return new RTEMSTargetInfo<SparcV8elTargetInfo>(Triple, Opts);
  405. default:
  406. return new SparcV8elTargetInfo(Triple, Opts);
  407. }
  408. case llvm::Triple::sparcv9:
  409. switch (os) {
  410. case llvm::Triple::Linux:
  411. return new LinuxTargetInfo<SparcV9TargetInfo>(Triple, Opts);
  412. case llvm::Triple::Solaris:
  413. return new SolarisTargetInfo<SparcV9TargetInfo>(Triple, Opts);
  414. case llvm::Triple::NetBSD:
  415. return new NetBSDTargetInfo<SparcV9TargetInfo>(Triple, Opts);
  416. case llvm::Triple::OpenBSD:
  417. return new OpenBSDTargetInfo<SparcV9TargetInfo>(Triple, Opts);
  418. case llvm::Triple::FreeBSD:
  419. return new FreeBSDTargetInfo<SparcV9TargetInfo>(Triple, Opts);
  420. default:
  421. return new SparcV9TargetInfo(Triple, Opts);
  422. }
  423. case llvm::Triple::systemz:
  424. switch (os) {
  425. case llvm::Triple::Linux:
  426. return new LinuxTargetInfo<SystemZTargetInfo>(Triple, Opts);
  427. case llvm::Triple::ZOS:
  428. return new ZOSTargetInfo<SystemZTargetInfo>(Triple, Opts);
  429. default:
  430. return new SystemZTargetInfo(Triple, Opts);
  431. }
  432. case llvm::Triple::tce:
  433. return new TCETargetInfo(Triple, Opts);
  434. case llvm::Triple::tcele:
  435. return new TCELETargetInfo(Triple, Opts);
  436. case llvm::Triple::x86:
  437. if (Triple.isOSDarwin())
  438. return new DarwinI386TargetInfo(Triple, Opts);
  439. switch (os) {
  440. case llvm::Triple::Ananas:
  441. return new AnanasTargetInfo<X86_32TargetInfo>(Triple, Opts);
  442. case llvm::Triple::CloudABI:
  443. return new CloudABITargetInfo<X86_32TargetInfo>(Triple, Opts);
  444. case llvm::Triple::Linux: {
  445. switch (Triple.getEnvironment()) {
  446. default:
  447. return new LinuxTargetInfo<X86_32TargetInfo>(Triple, Opts);
  448. case llvm::Triple::Android:
  449. return new AndroidX86_32TargetInfo(Triple, Opts);
  450. }
  451. }
  452. case llvm::Triple::DragonFly:
  453. return new DragonFlyBSDTargetInfo<X86_32TargetInfo>(Triple, Opts);
  454. case llvm::Triple::NetBSD:
  455. return new NetBSDI386TargetInfo(Triple, Opts);
  456. case llvm::Triple::OpenBSD:
  457. return new OpenBSDI386TargetInfo(Triple, Opts);
  458. case llvm::Triple::FreeBSD:
  459. return new FreeBSDTargetInfo<X86_32TargetInfo>(Triple, Opts);
  460. case llvm::Triple::Fuchsia:
  461. return new FuchsiaTargetInfo<X86_32TargetInfo>(Triple, Opts);
  462. case llvm::Triple::KFreeBSD:
  463. return new KFreeBSDTargetInfo<X86_32TargetInfo>(Triple, Opts);
  464. case llvm::Triple::Minix:
  465. return new MinixTargetInfo<X86_32TargetInfo>(Triple, Opts);
  466. case llvm::Triple::Solaris:
  467. return new SolarisTargetInfo<X86_32TargetInfo>(Triple, Opts);
  468. case llvm::Triple::Win32: {
  469. switch (Triple.getEnvironment()) {
  470. case llvm::Triple::Cygnus:
  471. return new CygwinX86_32TargetInfo(Triple, Opts);
  472. case llvm::Triple::GNU:
  473. return new MinGWX86_32TargetInfo(Triple, Opts);
  474. case llvm::Triple::Itanium:
  475. case llvm::Triple::MSVC:
  476. default: // Assume MSVC for unknown environments
  477. return new MicrosoftX86_32TargetInfo(Triple, Opts);
  478. }
  479. }
  480. case llvm::Triple::Haiku:
  481. return new HaikuX86_32TargetInfo(Triple, Opts);
  482. case llvm::Triple::RTEMS:
  483. return new RTEMSX86_32TargetInfo(Triple, Opts);
  484. case llvm::Triple::NaCl:
  485. return new NaClTargetInfo<X86_32TargetInfo>(Triple, Opts);
  486. case llvm::Triple::ELFIAMCU:
  487. return new MCUX86_32TargetInfo(Triple, Opts);
  488. case llvm::Triple::Hurd:
  489. return new HurdTargetInfo<X86_32TargetInfo>(Triple, Opts);
  490. default:
  491. return new X86_32TargetInfo(Triple, Opts);
  492. }
  493. case llvm::Triple::x86_64:
  494. if (Triple.isOSDarwin() || Triple.isOSBinFormatMachO())
  495. return new DarwinX86_64TargetInfo(Triple, Opts);
  496. switch (os) {
  497. case llvm::Triple::Ananas:
  498. return new AnanasTargetInfo<X86_64TargetInfo>(Triple, Opts);
  499. case llvm::Triple::CloudABI:
  500. return new CloudABITargetInfo<X86_64TargetInfo>(Triple, Opts);
  501. case llvm::Triple::Linux: {
  502. switch (Triple.getEnvironment()) {
  503. default:
  504. return new LinuxTargetInfo<X86_64TargetInfo>(Triple, Opts);
  505. case llvm::Triple::Android:
  506. return new AndroidX86_64TargetInfo(Triple, Opts);
  507. }
  508. }
  509. case llvm::Triple::DragonFly:
  510. return new DragonFlyBSDTargetInfo<X86_64TargetInfo>(Triple, Opts);
  511. case llvm::Triple::NetBSD:
  512. return new NetBSDTargetInfo<X86_64TargetInfo>(Triple, Opts);
  513. case llvm::Triple::OpenBSD:
  514. return new OpenBSDX86_64TargetInfo(Triple, Opts);
  515. case llvm::Triple::FreeBSD:
  516. return new FreeBSDTargetInfo<X86_64TargetInfo>(Triple, Opts);
  517. case llvm::Triple::Fuchsia:
  518. return new FuchsiaTargetInfo<X86_64TargetInfo>(Triple, Opts);
  519. case llvm::Triple::KFreeBSD:
  520. return new KFreeBSDTargetInfo<X86_64TargetInfo>(Triple, Opts);
  521. case llvm::Triple::Solaris:
  522. return new SolarisTargetInfo<X86_64TargetInfo>(Triple, Opts);
  523. case llvm::Triple::Win32: {
  524. switch (Triple.getEnvironment()) {
  525. case llvm::Triple::Cygnus:
  526. return new CygwinX86_64TargetInfo(Triple, Opts);
  527. case llvm::Triple::GNU:
  528. return new MinGWX86_64TargetInfo(Triple, Opts);
  529. case llvm::Triple::MSVC:
  530. default: // Assume MSVC for unknown environments
  531. return new MicrosoftX86_64TargetInfo(Triple, Opts);
  532. }
  533. }
  534. case llvm::Triple::Haiku:
  535. return new HaikuTargetInfo<X86_64TargetInfo>(Triple, Opts);
  536. case llvm::Triple::NaCl:
  537. return new NaClTargetInfo<X86_64TargetInfo>(Triple, Opts);
  538. case llvm::Triple::PS4:
  539. return new PS4OSTargetInfo<X86_64TargetInfo>(Triple, Opts);
  540. default:
  541. return new X86_64TargetInfo(Triple, Opts);
  542. }
  543. case llvm::Triple::spir: {
  544. if (os != llvm::Triple::UnknownOS ||
  545. Triple.getEnvironment() != llvm::Triple::UnknownEnvironment)
  546. return nullptr;
  547. return new SPIR32TargetInfo(Triple, Opts);
  548. }
  549. case llvm::Triple::spir64: {
  550. if (os != llvm::Triple::UnknownOS ||
  551. Triple.getEnvironment() != llvm::Triple::UnknownEnvironment)
  552. return nullptr;
  553. return new SPIR64TargetInfo(Triple, Opts);
  554. }
  555. case llvm::Triple::spirv32: {
  556. if (os != llvm::Triple::UnknownOS ||
  557. Triple.getEnvironment() != llvm::Triple::UnknownEnvironment)
  558. return nullptr;
  559. return new SPIRV32TargetInfo(Triple, Opts);
  560. }
  561. case llvm::Triple::spirv64: {
  562. if (os != llvm::Triple::UnknownOS ||
  563. Triple.getEnvironment() != llvm::Triple::UnknownEnvironment)
  564. return nullptr;
  565. return new SPIRV64TargetInfo(Triple, Opts);
  566. }
  567. case llvm::Triple::wasm32:
  568. if (Triple.getSubArch() != llvm::Triple::NoSubArch ||
  569. Triple.getVendor() != llvm::Triple::UnknownVendor ||
  570. !Triple.isOSBinFormatWasm())
  571. return nullptr;
  572. switch (os) {
  573. case llvm::Triple::WASI:
  574. return new WASITargetInfo<WebAssembly32TargetInfo>(Triple, Opts);
  575. case llvm::Triple::Emscripten:
  576. return new EmscriptenTargetInfo<WebAssembly32TargetInfo>(Triple, Opts);
  577. case llvm::Triple::UnknownOS:
  578. return new WebAssemblyOSTargetInfo<WebAssembly32TargetInfo>(Triple, Opts);
  579. default:
  580. return nullptr;
  581. }
  582. case llvm::Triple::wasm64:
  583. if (Triple.getSubArch() != llvm::Triple::NoSubArch ||
  584. Triple.getVendor() != llvm::Triple::UnknownVendor ||
  585. !Triple.isOSBinFormatWasm())
  586. return nullptr;
  587. switch (os) {
  588. case llvm::Triple::WASI:
  589. return new WASITargetInfo<WebAssembly64TargetInfo>(Triple, Opts);
  590. case llvm::Triple::Emscripten:
  591. return new EmscriptenTargetInfo<WebAssembly64TargetInfo>(Triple, Opts);
  592. case llvm::Triple::UnknownOS:
  593. return new WebAssemblyOSTargetInfo<WebAssembly64TargetInfo>(Triple, Opts);
  594. default:
  595. return nullptr;
  596. }
  597. case llvm::Triple::renderscript32:
  598. return new LinuxTargetInfo<RenderScript32TargetInfo>(Triple, Opts);
  599. case llvm::Triple::renderscript64:
  600. return new LinuxTargetInfo<RenderScript64TargetInfo>(Triple, Opts);
  601. case llvm::Triple::ve:
  602. return new LinuxTargetInfo<VETargetInfo>(Triple, Opts);
  603. }
  604. }
  605. } // namespace targets
  606. } // namespace clang
  607. using namespace clang::targets;
  608. /// CreateTargetInfo - Return the target info object for the specified target
  609. /// options.
  610. TargetInfo *
  611. TargetInfo::CreateTargetInfo(DiagnosticsEngine &Diags,
  612. const std::shared_ptr<TargetOptions> &Opts) {
  613. llvm::Triple Triple(Opts->Triple);
  614. // Construct the target
  615. std::unique_ptr<TargetInfo> Target(AllocateTarget(Triple, *Opts));
  616. if (!Target) {
  617. Diags.Report(diag::err_target_unknown_triple) << Triple.str();
  618. return nullptr;
  619. }
  620. Target->TargetOpts = Opts;
  621. // Set the target CPU if specified.
  622. if (!Opts->CPU.empty() && !Target->setCPU(Opts->CPU)) {
  623. Diags.Report(diag::err_target_unknown_cpu) << Opts->CPU;
  624. SmallVector<StringRef, 32> ValidList;
  625. Target->fillValidCPUList(ValidList);
  626. if (!ValidList.empty())
  627. Diags.Report(diag::note_valid_options) << llvm::join(ValidList, ", ");
  628. return nullptr;
  629. }
  630. // Check the TuneCPU name if specified.
  631. if (!Opts->TuneCPU.empty() &&
  632. !Target->isValidTuneCPUName(Opts->TuneCPU)) {
  633. Diags.Report(diag::err_target_unknown_cpu) << Opts->TuneCPU;
  634. SmallVector<StringRef, 32> ValidList;
  635. Target->fillValidTuneCPUList(ValidList);
  636. if (!ValidList.empty())
  637. Diags.Report(diag::note_valid_options) << llvm::join(ValidList, ", ");
  638. return nullptr;
  639. }
  640. // Set the target ABI if specified.
  641. if (!Opts->ABI.empty() && !Target->setABI(Opts->ABI)) {
  642. Diags.Report(diag::err_target_unknown_abi) << Opts->ABI;
  643. return nullptr;
  644. }
  645. // Set the fp math unit.
  646. if (!Opts->FPMath.empty() && !Target->setFPMath(Opts->FPMath)) {
  647. Diags.Report(diag::err_target_unknown_fpmath) << Opts->FPMath;
  648. return nullptr;
  649. }
  650. // Compute the default target features, we need the target to handle this
  651. // because features may have dependencies on one another.
  652. if (!Target->initFeatureMap(Opts->FeatureMap, Diags, Opts->CPU,
  653. Opts->FeaturesAsWritten))
  654. return nullptr;
  655. // Add the features to the compile options.
  656. Opts->Features.clear();
  657. for (const auto &F : Opts->FeatureMap)
  658. Opts->Features.push_back((F.getValue() ? "+" : "-") + F.getKey().str());
  659. // Sort here, so we handle the features in a predictable order. (This matters
  660. // when we're dealing with features that overlap.)
  661. llvm::sort(Opts->Features);
  662. if (!Target->handleTargetFeatures(Opts->Features, Diags))
  663. return nullptr;
  664. Target->setSupportedOpenCLOpts();
  665. Target->setCommandLineOpenCLOpts();
  666. Target->setMaxAtomicWidth();
  667. if (!Target->validateTarget(Diags))
  668. return nullptr;
  669. Target->CheckFixedPointBits();
  670. return Target.release();
  671. }
  672. /// validateOpenCLTarget - Check that OpenCL target has valid
  673. /// options setting based on OpenCL version.
  674. bool TargetInfo::validateOpenCLTarget(const LangOptions &Opts,
  675. DiagnosticsEngine &Diags) const {
  676. const llvm::StringMap<bool> &OpenCLFeaturesMap = getSupportedOpenCLOpts();
  677. auto diagnoseNotSupportedCore = [&](llvm::StringRef Name, auto... OptArgs) {
  678. if (OpenCLOptions::isOpenCLOptionCoreIn(Opts, OptArgs...) &&
  679. !hasFeatureEnabled(OpenCLFeaturesMap, Name))
  680. Diags.Report(diag::warn_opencl_unsupported_core_feature)
  681. << Name << Opts.OpenCLCPlusPlus
  682. << Opts.getOpenCLVersionTuple().getAsString();
  683. };
  684. #define OPENCL_GENERIC_EXTENSION(Ext, ...) \
  685. diagnoseNotSupportedCore(#Ext, __VA_ARGS__);
  686. #include "clang/Basic/OpenCLExtensions.def"
  687. // Validate that feature macros are set properly for OpenCL C 3.0.
  688. // In other cases assume that target is always valid.
  689. if (Opts.getOpenCLCompatibleVersion() < 300)
  690. return true;
  691. return OpenCLOptions::diagnoseUnsupportedFeatureDependencies(*this, Diags) &&
  692. OpenCLOptions::diagnoseFeatureExtensionDifferences(*this, Diags);
  693. }