Targets.cpp 28 KB

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