OSTargets.h 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982
  1. //===--- OSTargets.h - Declare OS target feature support --------*- 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. // This file declares OS specific TargetInfo types.
  10. //===----------------------------------------------------------------------===//
  11. #ifndef LLVM_CLANG_LIB_BASIC_TARGETS_OSTARGETS_H
  12. #define LLVM_CLANG_LIB_BASIC_TARGETS_OSTARGETS_H
  13. #include "Targets.h"
  14. namespace clang {
  15. namespace targets {
  16. template <typename TgtInfo>
  17. class LLVM_LIBRARY_VISIBILITY OSTargetInfo : public TgtInfo {
  18. protected:
  19. virtual void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
  20. MacroBuilder &Builder) const = 0;
  21. public:
  22. OSTargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts)
  23. : TgtInfo(Triple, Opts) {}
  24. void getTargetDefines(const LangOptions &Opts,
  25. MacroBuilder &Builder) const override {
  26. TgtInfo::getTargetDefines(Opts, Builder);
  27. getOSDefines(Opts, TgtInfo::getTriple(), Builder);
  28. }
  29. };
  30. // CloudABI Target
  31. template <typename Target>
  32. class LLVM_LIBRARY_VISIBILITY CloudABITargetInfo : public OSTargetInfo<Target> {
  33. protected:
  34. void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
  35. MacroBuilder &Builder) const override {
  36. Builder.defineMacro("__CloudABI__");
  37. Builder.defineMacro("__ELF__");
  38. // CloudABI uses ISO/IEC 10646:2012 for wchar_t, char16_t and char32_t.
  39. Builder.defineMacro("__STDC_ISO_10646__", "201206L");
  40. Builder.defineMacro("__STDC_UTF_16__");
  41. Builder.defineMacro("__STDC_UTF_32__");
  42. }
  43. public:
  44. CloudABITargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts)
  45. : OSTargetInfo<Target>(Triple, Opts) {}
  46. };
  47. // Ananas target
  48. template <typename Target>
  49. class LLVM_LIBRARY_VISIBILITY AnanasTargetInfo : public OSTargetInfo<Target> {
  50. protected:
  51. void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
  52. MacroBuilder &Builder) const override {
  53. // Ananas defines
  54. Builder.defineMacro("__Ananas__");
  55. Builder.defineMacro("__ELF__");
  56. }
  57. public:
  58. AnanasTargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts)
  59. : OSTargetInfo<Target>(Triple, Opts) {}
  60. };
  61. void getDarwinDefines(MacroBuilder &Builder, const LangOptions &Opts,
  62. const llvm::Triple &Triple, StringRef &PlatformName,
  63. VersionTuple &PlatformMinVersion);
  64. template <typename Target>
  65. class LLVM_LIBRARY_VISIBILITY DarwinTargetInfo : public OSTargetInfo<Target> {
  66. protected:
  67. void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
  68. MacroBuilder &Builder) const override {
  69. getDarwinDefines(Builder, Opts, Triple, this->PlatformName,
  70. this->PlatformMinVersion);
  71. }
  72. public:
  73. DarwinTargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts)
  74. : OSTargetInfo<Target>(Triple, Opts) {
  75. // By default, no TLS, and we list permitted architecture/OS
  76. // combinations.
  77. this->TLSSupported = false;
  78. if (Triple.isMacOSX())
  79. this->TLSSupported = !Triple.isMacOSXVersionLT(10, 7);
  80. else if (Triple.isiOS()) {
  81. // 64-bit iOS supported it from 8 onwards, 32-bit device from 9 onwards,
  82. // 32-bit simulator from 10 onwards.
  83. if (Triple.isArch64Bit())
  84. this->TLSSupported = !Triple.isOSVersionLT(8);
  85. else if (Triple.isArch32Bit()) {
  86. if (!Triple.isSimulatorEnvironment())
  87. this->TLSSupported = !Triple.isOSVersionLT(9);
  88. else
  89. this->TLSSupported = !Triple.isOSVersionLT(10);
  90. }
  91. } else if (Triple.isWatchOS()) {
  92. if (!Triple.isSimulatorEnvironment())
  93. this->TLSSupported = !Triple.isOSVersionLT(2);
  94. else
  95. this->TLSSupported = !Triple.isOSVersionLT(3);
  96. }
  97. this->MCountName = "\01mcount";
  98. }
  99. const char *getStaticInitSectionSpecifier() const override {
  100. // FIXME: We should return 0 when building kexts.
  101. return "__TEXT,__StaticInit,regular,pure_instructions";
  102. }
  103. /// Darwin does not support protected visibility. Darwin's "default"
  104. /// is very similar to ELF's "protected"; Darwin requires a "weak"
  105. /// attribute on declarations that can be dynamically replaced.
  106. bool hasProtectedVisibility() const override { return false; }
  107. unsigned getExnObjectAlignment() const override {
  108. // Older versions of libc++abi guarantee an alignment of only 8-bytes for
  109. // exception objects because of a bug in __cxa_exception that was
  110. // eventually fixed in r319123.
  111. llvm::VersionTuple MinVersion;
  112. const llvm::Triple &T = this->getTriple();
  113. // Compute the earliest OS versions that have the fix to libc++abi.
  114. switch (T.getOS()) {
  115. case llvm::Triple::Darwin:
  116. case llvm::Triple::MacOSX: // Earliest supporting version is 10.14.
  117. MinVersion = llvm::VersionTuple(10U, 14U);
  118. break;
  119. case llvm::Triple::IOS:
  120. case llvm::Triple::TvOS: // Earliest supporting version is 12.0.0.
  121. MinVersion = llvm::VersionTuple(12U);
  122. break;
  123. case llvm::Triple::WatchOS: // Earliest supporting version is 5.0.0.
  124. MinVersion = llvm::VersionTuple(5U);
  125. break;
  126. default:
  127. // Conservatively return 8 bytes if OS is unknown.
  128. return 64;
  129. }
  130. if (T.getOSVersion() < MinVersion)
  131. return 64;
  132. return OSTargetInfo<Target>::getExnObjectAlignment();
  133. }
  134. TargetInfo::IntType getLeastIntTypeByWidth(unsigned BitWidth,
  135. bool IsSigned) const final {
  136. // Darwin uses `long long` for `int_least64_t` and `int_fast64_t`.
  137. return BitWidth == 64
  138. ? (IsSigned ? TargetInfo::SignedLongLong
  139. : TargetInfo::UnsignedLongLong)
  140. : TargetInfo::getLeastIntTypeByWidth(BitWidth, IsSigned);
  141. }
  142. };
  143. // DragonFlyBSD Target
  144. template <typename Target>
  145. class LLVM_LIBRARY_VISIBILITY DragonFlyBSDTargetInfo
  146. : public OSTargetInfo<Target> {
  147. protected:
  148. void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
  149. MacroBuilder &Builder) const override {
  150. // DragonFly defines; list based off of gcc output
  151. Builder.defineMacro("__DragonFly__");
  152. Builder.defineMacro("__DragonFly_cc_version", "100001");
  153. Builder.defineMacro("__ELF__");
  154. Builder.defineMacro("__KPRINTF_ATTRIBUTE__");
  155. Builder.defineMacro("__tune_i386__");
  156. DefineStd(Builder, "unix", Opts);
  157. if (this->HasFloat128)
  158. Builder.defineMacro("__FLOAT128__");
  159. }
  160. public:
  161. DragonFlyBSDTargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts)
  162. : OSTargetInfo<Target>(Triple, Opts) {
  163. switch (Triple.getArch()) {
  164. default:
  165. case llvm::Triple::x86:
  166. case llvm::Triple::x86_64:
  167. this->HasFloat128 = true;
  168. this->MCountName = ".mcount";
  169. break;
  170. }
  171. }
  172. };
  173. #ifndef FREEBSD_CC_VERSION
  174. #define FREEBSD_CC_VERSION 0U
  175. #endif
  176. // FreeBSD Target
  177. template <typename Target>
  178. class LLVM_LIBRARY_VISIBILITY FreeBSDTargetInfo : public OSTargetInfo<Target> {
  179. protected:
  180. void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
  181. MacroBuilder &Builder) const override {
  182. // FreeBSD defines; list based off of gcc output
  183. unsigned Release = Triple.getOSMajorVersion();
  184. if (Release == 0U)
  185. Release = 8U;
  186. unsigned CCVersion = FREEBSD_CC_VERSION;
  187. if (CCVersion == 0U)
  188. CCVersion = Release * 100000U + 1U;
  189. Builder.defineMacro("__FreeBSD__", Twine(Release));
  190. Builder.defineMacro("__FreeBSD_cc_version", Twine(CCVersion));
  191. Builder.defineMacro("__KPRINTF_ATTRIBUTE__");
  192. DefineStd(Builder, "unix", Opts);
  193. Builder.defineMacro("__ELF__");
  194. // On FreeBSD, wchar_t contains the number of the code point as
  195. // used by the character set of the locale. These character sets are
  196. // not necessarily a superset of ASCII.
  197. //
  198. // FIXME: This is wrong; the macro refers to the numerical values
  199. // of wchar_t *literals*, which are not locale-dependent. However,
  200. // FreeBSD systems apparently depend on us getting this wrong, and
  201. // setting this to 1 is conforming even if all the basic source
  202. // character literals have the same encoding as char and wchar_t.
  203. Builder.defineMacro("__STDC_MB_MIGHT_NEQ_WC__", "1");
  204. }
  205. public:
  206. FreeBSDTargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts)
  207. : OSTargetInfo<Target>(Triple, Opts) {
  208. switch (Triple.getArch()) {
  209. default:
  210. case llvm::Triple::x86:
  211. case llvm::Triple::x86_64:
  212. this->MCountName = ".mcount";
  213. break;
  214. case llvm::Triple::mips:
  215. case llvm::Triple::mipsel:
  216. case llvm::Triple::ppc:
  217. case llvm::Triple::ppcle:
  218. case llvm::Triple::ppc64:
  219. case llvm::Triple::ppc64le:
  220. this->MCountName = "_mcount";
  221. break;
  222. case llvm::Triple::arm:
  223. this->MCountName = "__mcount";
  224. break;
  225. case llvm::Triple::riscv32:
  226. case llvm::Triple::riscv64:
  227. break;
  228. }
  229. }
  230. };
  231. // GNU/kFreeBSD Target
  232. template <typename Target>
  233. class LLVM_LIBRARY_VISIBILITY KFreeBSDTargetInfo : public OSTargetInfo<Target> {
  234. protected:
  235. void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
  236. MacroBuilder &Builder) const override {
  237. // GNU/kFreeBSD defines; list based off of gcc output
  238. DefineStd(Builder, "unix", Opts);
  239. Builder.defineMacro("__FreeBSD_kernel__");
  240. Builder.defineMacro("__GLIBC__");
  241. Builder.defineMacro("__ELF__");
  242. if (Opts.POSIXThreads)
  243. Builder.defineMacro("_REENTRANT");
  244. if (Opts.CPlusPlus)
  245. Builder.defineMacro("_GNU_SOURCE");
  246. }
  247. public:
  248. KFreeBSDTargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts)
  249. : OSTargetInfo<Target>(Triple, Opts) {}
  250. };
  251. // Haiku Target
  252. template <typename Target>
  253. class LLVM_LIBRARY_VISIBILITY HaikuTargetInfo : public OSTargetInfo<Target> {
  254. protected:
  255. void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
  256. MacroBuilder &Builder) const override {
  257. // Haiku defines; list based off of gcc output
  258. Builder.defineMacro("__HAIKU__");
  259. Builder.defineMacro("__ELF__");
  260. DefineStd(Builder, "unix", Opts);
  261. if (this->HasFloat128)
  262. Builder.defineMacro("__FLOAT128__");
  263. }
  264. public:
  265. HaikuTargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts)
  266. : OSTargetInfo<Target>(Triple, Opts) {
  267. this->SizeType = TargetInfo::UnsignedLong;
  268. this->IntPtrType = TargetInfo::SignedLong;
  269. this->PtrDiffType = TargetInfo::SignedLong;
  270. this->ProcessIDType = TargetInfo::SignedLong;
  271. this->TLSSupported = false;
  272. switch (Triple.getArch()) {
  273. default:
  274. break;
  275. case llvm::Triple::x86:
  276. case llvm::Triple::x86_64:
  277. this->HasFloat128 = true;
  278. break;
  279. }
  280. }
  281. };
  282. // Hurd target
  283. template <typename Target>
  284. class LLVM_LIBRARY_VISIBILITY HurdTargetInfo : public OSTargetInfo<Target> {
  285. protected:
  286. void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
  287. MacroBuilder &Builder) const override {
  288. // Hurd defines; list based off of gcc output.
  289. DefineStd(Builder, "unix", Opts);
  290. Builder.defineMacro("__GNU__");
  291. Builder.defineMacro("__gnu_hurd__");
  292. Builder.defineMacro("__MACH__");
  293. Builder.defineMacro("__GLIBC__");
  294. Builder.defineMacro("__ELF__");
  295. if (Opts.POSIXThreads)
  296. Builder.defineMacro("_REENTRANT");
  297. if (Opts.CPlusPlus)
  298. Builder.defineMacro("_GNU_SOURCE");
  299. }
  300. public:
  301. HurdTargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts)
  302. : OSTargetInfo<Target>(Triple, Opts) {}
  303. };
  304. // Minix Target
  305. template <typename Target>
  306. class LLVM_LIBRARY_VISIBILITY MinixTargetInfo : public OSTargetInfo<Target> {
  307. protected:
  308. void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
  309. MacroBuilder &Builder) const override {
  310. // Minix defines
  311. Builder.defineMacro("__minix", "3");
  312. Builder.defineMacro("_EM_WSIZE", "4");
  313. Builder.defineMacro("_EM_PSIZE", "4");
  314. Builder.defineMacro("_EM_SSIZE", "2");
  315. Builder.defineMacro("_EM_LSIZE", "4");
  316. Builder.defineMacro("_EM_FSIZE", "4");
  317. Builder.defineMacro("_EM_DSIZE", "8");
  318. Builder.defineMacro("__ELF__");
  319. DefineStd(Builder, "unix", Opts);
  320. }
  321. public:
  322. MinixTargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts)
  323. : OSTargetInfo<Target>(Triple, Opts) {}
  324. };
  325. // Linux target
  326. template <typename Target>
  327. class LLVM_LIBRARY_VISIBILITY LinuxTargetInfo : public OSTargetInfo<Target> {
  328. protected:
  329. void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
  330. MacroBuilder &Builder) const override {
  331. // Linux defines; list based off of gcc output
  332. DefineStd(Builder, "unix", Opts);
  333. DefineStd(Builder, "linux", Opts);
  334. Builder.defineMacro("__ELF__");
  335. if (Triple.isAndroid()) {
  336. Builder.defineMacro("__ANDROID__", "1");
  337. this->PlatformName = "android";
  338. this->PlatformMinVersion = Triple.getEnvironmentVersion();
  339. const unsigned Maj = this->PlatformMinVersion.getMajor();
  340. if (Maj) {
  341. Builder.defineMacro("__ANDROID_MIN_SDK_VERSION__", Twine(Maj));
  342. // This historical but ambiguous name for the minSdkVersion macro. Keep
  343. // defined for compatibility.
  344. Builder.defineMacro("__ANDROID_API__", "__ANDROID_MIN_SDK_VERSION__");
  345. }
  346. } else {
  347. Builder.defineMacro("__gnu_linux__");
  348. }
  349. if (Opts.POSIXThreads)
  350. Builder.defineMacro("_REENTRANT");
  351. if (Opts.CPlusPlus)
  352. Builder.defineMacro("_GNU_SOURCE");
  353. if (this->HasFloat128)
  354. Builder.defineMacro("__FLOAT128__");
  355. }
  356. public:
  357. LinuxTargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts)
  358. : OSTargetInfo<Target>(Triple, Opts) {
  359. this->WIntType = TargetInfo::UnsignedInt;
  360. switch (Triple.getArch()) {
  361. default:
  362. break;
  363. case llvm::Triple::mips:
  364. case llvm::Triple::mipsel:
  365. case llvm::Triple::mips64:
  366. case llvm::Triple::mips64el:
  367. case llvm::Triple::ppc:
  368. case llvm::Triple::ppcle:
  369. case llvm::Triple::ppc64:
  370. case llvm::Triple::ppc64le:
  371. this->MCountName = "_mcount";
  372. break;
  373. case llvm::Triple::x86:
  374. case llvm::Triple::x86_64:
  375. this->HasFloat128 = true;
  376. break;
  377. }
  378. }
  379. const char *getStaticInitSectionSpecifier() const override {
  380. return ".text.startup";
  381. }
  382. };
  383. // NetBSD Target
  384. template <typename Target>
  385. class LLVM_LIBRARY_VISIBILITY NetBSDTargetInfo : public OSTargetInfo<Target> {
  386. protected:
  387. void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
  388. MacroBuilder &Builder) const override {
  389. // NetBSD defines; list based off of gcc output
  390. Builder.defineMacro("__NetBSD__");
  391. Builder.defineMacro("__unix__");
  392. Builder.defineMacro("__ELF__");
  393. if (Opts.POSIXThreads)
  394. Builder.defineMacro("_REENTRANT");
  395. }
  396. public:
  397. NetBSDTargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts)
  398. : OSTargetInfo<Target>(Triple, Opts) {
  399. this->MCountName = "__mcount";
  400. }
  401. };
  402. // OpenBSD Target
  403. template <typename Target>
  404. class LLVM_LIBRARY_VISIBILITY OpenBSDTargetInfo : public OSTargetInfo<Target> {
  405. protected:
  406. void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
  407. MacroBuilder &Builder) const override {
  408. // OpenBSD defines; list based off of gcc output
  409. Builder.defineMacro("__OpenBSD__");
  410. DefineStd(Builder, "unix", Opts);
  411. Builder.defineMacro("__ELF__");
  412. if (Opts.POSIXThreads)
  413. Builder.defineMacro("_REENTRANT");
  414. if (this->HasFloat128)
  415. Builder.defineMacro("__FLOAT128__");
  416. if (Opts.C11)
  417. Builder.defineMacro("__STDC_NO_THREADS__");
  418. }
  419. public:
  420. OpenBSDTargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts)
  421. : OSTargetInfo<Target>(Triple, Opts) {
  422. this->WCharType = this->WIntType = this->SignedInt;
  423. this->IntMaxType = TargetInfo::SignedLongLong;
  424. this->Int64Type = TargetInfo::SignedLongLong;
  425. switch (Triple.getArch()) {
  426. case llvm::Triple::x86:
  427. case llvm::Triple::x86_64:
  428. this->HasFloat128 = true;
  429. LLVM_FALLTHROUGH;
  430. default:
  431. this->MCountName = "__mcount";
  432. break;
  433. case llvm::Triple::mips64:
  434. case llvm::Triple::mips64el:
  435. case llvm::Triple::ppc:
  436. case llvm::Triple::ppc64:
  437. case llvm::Triple::ppc64le:
  438. case llvm::Triple::sparcv9:
  439. this->MCountName = "_mcount";
  440. break;
  441. case llvm::Triple::riscv32:
  442. case llvm::Triple::riscv64:
  443. break;
  444. }
  445. }
  446. };
  447. // PSP Target
  448. template <typename Target>
  449. class LLVM_LIBRARY_VISIBILITY PSPTargetInfo : public OSTargetInfo<Target> {
  450. protected:
  451. void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
  452. MacroBuilder &Builder) const override {
  453. // PSP defines; list based on the output of the pspdev gcc toolchain.
  454. Builder.defineMacro("PSP");
  455. Builder.defineMacro("_PSP");
  456. Builder.defineMacro("__psp__");
  457. Builder.defineMacro("__ELF__");
  458. }
  459. public:
  460. PSPTargetInfo(const llvm::Triple &Triple) : OSTargetInfo<Target>(Triple) {}
  461. };
  462. // PS3 PPU Target
  463. template <typename Target>
  464. class LLVM_LIBRARY_VISIBILITY PS3PPUTargetInfo : public OSTargetInfo<Target> {
  465. protected:
  466. void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
  467. MacroBuilder &Builder) const override {
  468. // PS3 PPU defines.
  469. Builder.defineMacro("__PPC__");
  470. Builder.defineMacro("__PPU__");
  471. Builder.defineMacro("__CELLOS_LV2__");
  472. Builder.defineMacro("__ELF__");
  473. Builder.defineMacro("__LP32__");
  474. Builder.defineMacro("_ARCH_PPC64");
  475. Builder.defineMacro("__powerpc64__");
  476. }
  477. public:
  478. PS3PPUTargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts)
  479. : OSTargetInfo<Target>(Triple, Opts) {
  480. this->LongWidth = this->LongAlign = 32;
  481. this->PointerWidth = this->PointerAlign = 32;
  482. this->IntMaxType = TargetInfo::SignedLongLong;
  483. this->Int64Type = TargetInfo::SignedLongLong;
  484. this->SizeType = TargetInfo::UnsignedInt;
  485. this->resetDataLayout("E-m:e-p:32:32-i64:64-n32:64");
  486. }
  487. };
  488. template <typename Target>
  489. class LLVM_LIBRARY_VISIBILITY PS4OSTargetInfo : public OSTargetInfo<Target> {
  490. protected:
  491. void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
  492. MacroBuilder &Builder) const override {
  493. Builder.defineMacro("__FreeBSD__", "9");
  494. Builder.defineMacro("__FreeBSD_cc_version", "900001");
  495. Builder.defineMacro("__KPRINTF_ATTRIBUTE__");
  496. DefineStd(Builder, "unix", Opts);
  497. Builder.defineMacro("__ELF__");
  498. Builder.defineMacro("__SCE__");
  499. Builder.defineMacro("__ORBIS__");
  500. }
  501. public:
  502. PS4OSTargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts)
  503. : OSTargetInfo<Target>(Triple, Opts) {
  504. this->WCharType = TargetInfo::UnsignedShort;
  505. // On PS4, TLS variable cannot be aligned to more than 32 bytes (256 bits).
  506. this->MaxTLSAlign = 256;
  507. // On PS4, do not honor explicit bit field alignment,
  508. // as in "__attribute__((aligned(2))) int b : 1;".
  509. this->UseExplicitBitFieldAlignment = false;
  510. switch (Triple.getArch()) {
  511. default:
  512. case llvm::Triple::x86_64:
  513. this->MCountName = ".mcount";
  514. this->NewAlign = 256;
  515. break;
  516. }
  517. }
  518. TargetInfo::CallingConvCheckResult
  519. checkCallingConvention(CallingConv CC) const override {
  520. return (CC == CC_C) ? TargetInfo::CCCR_OK : TargetInfo::CCCR_Error;
  521. }
  522. };
  523. // RTEMS Target
  524. template <typename Target>
  525. class LLVM_LIBRARY_VISIBILITY RTEMSTargetInfo : public OSTargetInfo<Target> {
  526. protected:
  527. void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
  528. MacroBuilder &Builder) const override {
  529. // RTEMS defines; list based off of gcc output
  530. Builder.defineMacro("__rtems__");
  531. Builder.defineMacro("__ELF__");
  532. if (Opts.CPlusPlus)
  533. Builder.defineMacro("_GNU_SOURCE");
  534. }
  535. public:
  536. RTEMSTargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts)
  537. : OSTargetInfo<Target>(Triple, Opts) {
  538. switch (Triple.getArch()) {
  539. default:
  540. case llvm::Triple::x86:
  541. // this->MCountName = ".mcount";
  542. break;
  543. case llvm::Triple::mips:
  544. case llvm::Triple::mipsel:
  545. case llvm::Triple::ppc:
  546. case llvm::Triple::ppc64:
  547. case llvm::Triple::ppc64le:
  548. // this->MCountName = "_mcount";
  549. break;
  550. case llvm::Triple::arm:
  551. // this->MCountName = "__mcount";
  552. break;
  553. }
  554. }
  555. };
  556. // Solaris target
  557. template <typename Target>
  558. class LLVM_LIBRARY_VISIBILITY SolarisTargetInfo : public OSTargetInfo<Target> {
  559. protected:
  560. void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
  561. MacroBuilder &Builder) const override {
  562. DefineStd(Builder, "sun", Opts);
  563. DefineStd(Builder, "unix", Opts);
  564. Builder.defineMacro("__ELF__");
  565. Builder.defineMacro("__svr4__");
  566. Builder.defineMacro("__SVR4");
  567. // Solaris headers require _XOPEN_SOURCE to be set to 600 for C99 and
  568. // newer, but to 500 for everything else. feature_test.h has a check to
  569. // ensure that you are not using C99 with an old version of X/Open or C89
  570. // with a new version.
  571. if (Opts.C99)
  572. Builder.defineMacro("_XOPEN_SOURCE", "600");
  573. else
  574. Builder.defineMacro("_XOPEN_SOURCE", "500");
  575. if (Opts.CPlusPlus) {
  576. Builder.defineMacro("__C99FEATURES__");
  577. Builder.defineMacro("_FILE_OFFSET_BITS", "64");
  578. }
  579. // GCC restricts the next two to C++.
  580. Builder.defineMacro("_LARGEFILE_SOURCE");
  581. Builder.defineMacro("_LARGEFILE64_SOURCE");
  582. Builder.defineMacro("__EXTENSIONS__");
  583. if (Opts.POSIXThreads)
  584. Builder.defineMacro("_REENTRANT");
  585. if (this->HasFloat128)
  586. Builder.defineMacro("__FLOAT128__");
  587. }
  588. public:
  589. SolarisTargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts)
  590. : OSTargetInfo<Target>(Triple, Opts) {
  591. if (this->PointerWidth == 64) {
  592. this->WCharType = this->WIntType = this->SignedInt;
  593. } else {
  594. this->WCharType = this->WIntType = this->SignedLong;
  595. }
  596. switch (Triple.getArch()) {
  597. default:
  598. break;
  599. case llvm::Triple::x86:
  600. case llvm::Triple::x86_64:
  601. this->HasFloat128 = true;
  602. break;
  603. }
  604. }
  605. };
  606. // AIX Target
  607. template <typename Target>
  608. class AIXTargetInfo : public OSTargetInfo<Target> {
  609. protected:
  610. void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
  611. MacroBuilder &Builder) const override {
  612. DefineStd(Builder, "unix", Opts);
  613. Builder.defineMacro("_IBMR2");
  614. Builder.defineMacro("_POWER");
  615. Builder.defineMacro("__THW_BIG_ENDIAN__");
  616. Builder.defineMacro("_AIX");
  617. Builder.defineMacro("__TOS_AIX__");
  618. Builder.defineMacro("__HOS_AIX__");
  619. if (Opts.C11) {
  620. Builder.defineMacro("__STDC_NO_ATOMICS__");
  621. Builder.defineMacro("__STDC_NO_THREADS__");
  622. }
  623. if (Opts.EnableAIXExtendedAltivecABI)
  624. Builder.defineMacro("__EXTABI__");
  625. VersionTuple OsVersion = Triple.getOSVersion();
  626. // Define AIX OS-Version Macros.
  627. // Includes logic for legacy versions of AIX; no specific intent to support.
  628. if (OsVersion >= VersionTuple(3, 2))
  629. Builder.defineMacro("_AIX32");
  630. if (OsVersion >= VersionTuple(4, 1))
  631. Builder.defineMacro("_AIX41");
  632. if (OsVersion >= VersionTuple(4, 3))
  633. Builder.defineMacro("_AIX43");
  634. if (OsVersion >= VersionTuple(5, 0))
  635. Builder.defineMacro("_AIX50");
  636. if (OsVersion >= VersionTuple(5, 1))
  637. Builder.defineMacro("_AIX51");
  638. if (OsVersion >= VersionTuple(5, 2))
  639. Builder.defineMacro("_AIX52");
  640. if (OsVersion >= VersionTuple(5, 3))
  641. Builder.defineMacro("_AIX53");
  642. if (OsVersion >= VersionTuple(6, 1))
  643. Builder.defineMacro("_AIX61");
  644. if (OsVersion >= VersionTuple(7, 1))
  645. Builder.defineMacro("_AIX71");
  646. if (OsVersion >= VersionTuple(7, 2))
  647. Builder.defineMacro("_AIX72");
  648. if (OsVersion >= VersionTuple(7, 3))
  649. Builder.defineMacro("_AIX73");
  650. // FIXME: Do not define _LONG_LONG when -fno-long-long is specified.
  651. Builder.defineMacro("_LONG_LONG");
  652. if (Opts.POSIXThreads) {
  653. Builder.defineMacro("_THREAD_SAFE");
  654. }
  655. if (this->PointerWidth == 64) {
  656. Builder.defineMacro("__64BIT__");
  657. }
  658. // Define _WCHAR_T when it is a fundamental type
  659. // (i.e., for C++ without -fno-wchar).
  660. if (Opts.CPlusPlus && Opts.WChar) {
  661. Builder.defineMacro("_WCHAR_T");
  662. }
  663. }
  664. public:
  665. AIXTargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts)
  666. : OSTargetInfo<Target>(Triple, Opts) {
  667. this->TheCXXABI.set(TargetCXXABI::XL);
  668. if (this->PointerWidth == 64) {
  669. this->WCharType = this->UnsignedInt;
  670. } else {
  671. this->WCharType = this->UnsignedShort;
  672. }
  673. this->UseZeroLengthBitfieldAlignment = true;
  674. }
  675. // AIX sets FLT_EVAL_METHOD to be 1.
  676. unsigned getFloatEvalMethod() const override { return 1; }
  677. bool defaultsToAIXPowerAlignment() const override { return true; }
  678. };
  679. // z/OS target
  680. template <typename Target>
  681. class LLVM_LIBRARY_VISIBILITY ZOSTargetInfo : public OSTargetInfo<Target> {
  682. protected:
  683. void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
  684. MacroBuilder &Builder) const override {
  685. // FIXME: _LONG_LONG should not be defined under -std=c89.
  686. Builder.defineMacro("_LONG_LONG");
  687. Builder.defineMacro("_OPEN_DEFAULT");
  688. // _UNIX03_WITHDRAWN is required to build libcxx.
  689. Builder.defineMacro("_UNIX03_WITHDRAWN");
  690. Builder.defineMacro("__370__");
  691. Builder.defineMacro("__BFP__");
  692. // FIXME: __BOOL__ should not be defined under -std=c89.
  693. Builder.defineMacro("__BOOL__");
  694. Builder.defineMacro("__LONGNAME__");
  695. Builder.defineMacro("__MVS__");
  696. Builder.defineMacro("__THW_370__");
  697. Builder.defineMacro("__THW_BIG_ENDIAN__");
  698. Builder.defineMacro("__TOS_390__");
  699. Builder.defineMacro("__TOS_MVS__");
  700. Builder.defineMacro("__XPLINK__");
  701. if (this->PointerWidth == 64)
  702. Builder.defineMacro("__64BIT__");
  703. if (Opts.CPlusPlus) {
  704. Builder.defineMacro("__DLL__");
  705. // _XOPEN_SOURCE=600 is required to build libcxx.
  706. Builder.defineMacro("_XOPEN_SOURCE", "600");
  707. }
  708. if (Opts.GNUMode) {
  709. Builder.defineMacro("_MI_BUILTIN");
  710. Builder.defineMacro("_EXT");
  711. }
  712. if (Opts.CPlusPlus && Opts.WChar) {
  713. // Macro __wchar_t is defined so that the wchar_t data
  714. // type is not declared as a typedef in system headers.
  715. Builder.defineMacro("__wchar_t");
  716. }
  717. this->PlatformName = llvm::Triple::getOSTypeName(Triple.getOS());
  718. }
  719. public:
  720. ZOSTargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts)
  721. : OSTargetInfo<Target>(Triple, Opts) {
  722. this->WCharType = TargetInfo::UnsignedInt;
  723. this->MaxAlignedAttribute = 128;
  724. this->UseBitFieldTypeAlignment = false;
  725. this->UseZeroLengthBitfieldAlignment = true;
  726. this->UseLeadingZeroLengthBitfield = false;
  727. this->ZeroLengthBitfieldBoundary = 32;
  728. }
  729. };
  730. void addWindowsDefines(const llvm::Triple &Triple, const LangOptions &Opts,
  731. MacroBuilder &Builder);
  732. // Windows target
  733. template <typename Target>
  734. class LLVM_LIBRARY_VISIBILITY WindowsTargetInfo : public OSTargetInfo<Target> {
  735. protected:
  736. void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
  737. MacroBuilder &Builder) const override {
  738. addWindowsDefines(Triple, Opts, Builder);
  739. }
  740. public:
  741. WindowsTargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts)
  742. : OSTargetInfo<Target>(Triple, Opts) {
  743. this->WCharType = TargetInfo::UnsignedShort;
  744. this->WIntType = TargetInfo::UnsignedShort;
  745. }
  746. };
  747. template <typename Target>
  748. class LLVM_LIBRARY_VISIBILITY NaClTargetInfo : public OSTargetInfo<Target> {
  749. protected:
  750. void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
  751. MacroBuilder &Builder) const override {
  752. if (Opts.POSIXThreads)
  753. Builder.defineMacro("_REENTRANT");
  754. if (Opts.CPlusPlus)
  755. Builder.defineMacro("_GNU_SOURCE");
  756. DefineStd(Builder, "unix", Opts);
  757. Builder.defineMacro("__ELF__");
  758. Builder.defineMacro("__native_client__");
  759. }
  760. public:
  761. NaClTargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts)
  762. : OSTargetInfo<Target>(Triple, Opts) {
  763. this->LongAlign = 32;
  764. this->LongWidth = 32;
  765. this->PointerAlign = 32;
  766. this->PointerWidth = 32;
  767. this->IntMaxType = TargetInfo::SignedLongLong;
  768. this->Int64Type = TargetInfo::SignedLongLong;
  769. this->DoubleAlign = 64;
  770. this->LongDoubleWidth = 64;
  771. this->LongDoubleAlign = 64;
  772. this->LongLongWidth = 64;
  773. this->LongLongAlign = 64;
  774. this->SizeType = TargetInfo::UnsignedInt;
  775. this->PtrDiffType = TargetInfo::SignedInt;
  776. this->IntPtrType = TargetInfo::SignedInt;
  777. // RegParmMax is inherited from the underlying architecture.
  778. this->LongDoubleFormat = &llvm::APFloat::IEEEdouble();
  779. if (Triple.getArch() == llvm::Triple::arm) {
  780. // Handled in ARM's setABI().
  781. } else if (Triple.getArch() == llvm::Triple::x86) {
  782. this->resetDataLayout("e-m:e-p:32:32-p270:32:32-p271:32:32-p272:64:64-"
  783. "i64:64-n8:16:32-S128");
  784. } else if (Triple.getArch() == llvm::Triple::x86_64) {
  785. this->resetDataLayout("e-m:e-p:32:32-p270:32:32-p271:32:32-p272:64:64-"
  786. "i64:64-n8:16:32:64-S128");
  787. } else if (Triple.getArch() == llvm::Triple::mipsel) {
  788. // Handled on mips' setDataLayout.
  789. } else {
  790. assert(Triple.getArch() == llvm::Triple::le32);
  791. this->resetDataLayout("e-p:32:32-i64:64");
  792. }
  793. }
  794. };
  795. // Fuchsia Target
  796. template <typename Target>
  797. class LLVM_LIBRARY_VISIBILITY FuchsiaTargetInfo : public OSTargetInfo<Target> {
  798. protected:
  799. void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
  800. MacroBuilder &Builder) const override {
  801. Builder.defineMacro("__Fuchsia__");
  802. Builder.defineMacro("__ELF__");
  803. if (Opts.POSIXThreads)
  804. Builder.defineMacro("_REENTRANT");
  805. // Required by the libc++ locale support.
  806. if (Opts.CPlusPlus)
  807. Builder.defineMacro("_GNU_SOURCE");
  808. Builder.defineMacro("__Fuchsia_API_level__", Twine(Opts.FuchsiaAPILevel));
  809. this->PlatformName = "fuchsia";
  810. this->PlatformMinVersion = VersionTuple(Opts.FuchsiaAPILevel);
  811. }
  812. public:
  813. FuchsiaTargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts)
  814. : OSTargetInfo<Target>(Triple, Opts) {
  815. this->MCountName = "__mcount";
  816. this->TheCXXABI.set(TargetCXXABI::Fuchsia);
  817. }
  818. };
  819. // WebAssembly target
  820. template <typename Target>
  821. class LLVM_LIBRARY_VISIBILITY WebAssemblyOSTargetInfo
  822. : public OSTargetInfo<Target> {
  823. protected:
  824. void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
  825. MacroBuilder &Builder) const override {
  826. // A common platform macro.
  827. if (Opts.POSIXThreads)
  828. Builder.defineMacro("_REENTRANT");
  829. // Follow g++ convention and predefine _GNU_SOURCE for C++.
  830. if (Opts.CPlusPlus)
  831. Builder.defineMacro("_GNU_SOURCE");
  832. // Indicate that we have __float128.
  833. Builder.defineMacro("__FLOAT128__");
  834. }
  835. public:
  836. explicit WebAssemblyOSTargetInfo(const llvm::Triple &Triple,
  837. const TargetOptions &Opts)
  838. : OSTargetInfo<Target>(Triple, Opts) {
  839. this->MCountName = "__mcount";
  840. this->TheCXXABI.set(TargetCXXABI::WebAssembly);
  841. this->HasFloat128 = true;
  842. }
  843. };
  844. // WASI target
  845. template <typename Target>
  846. class LLVM_LIBRARY_VISIBILITY WASITargetInfo
  847. : public WebAssemblyOSTargetInfo<Target> {
  848. void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
  849. MacroBuilder &Builder) const final {
  850. WebAssemblyOSTargetInfo<Target>::getOSDefines(Opts, Triple, Builder);
  851. Builder.defineMacro("__wasi__");
  852. }
  853. public:
  854. explicit WASITargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts)
  855. : WebAssemblyOSTargetInfo<Target>(Triple, Opts) {}
  856. };
  857. // Emscripten target
  858. template <typename Target>
  859. class LLVM_LIBRARY_VISIBILITY EmscriptenTargetInfo
  860. : public WebAssemblyOSTargetInfo<Target> {
  861. void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
  862. MacroBuilder &Builder) const final {
  863. WebAssemblyOSTargetInfo<Target>::getOSDefines(Opts, Triple, Builder);
  864. DefineStd(Builder, "unix", Opts);
  865. Builder.defineMacro("__EMSCRIPTEN__");
  866. if (Opts.POSIXThreads)
  867. Builder.defineMacro("__EMSCRIPTEN_PTHREADS__");
  868. }
  869. public:
  870. explicit EmscriptenTargetInfo(const llvm::Triple &Triple,
  871. const TargetOptions &Opts)
  872. : WebAssemblyOSTargetInfo<Target>(Triple, Opts) {
  873. // Keeping the alignment of long double to 8 bytes even though its size is
  874. // 16 bytes allows emscripten to have an 8-byte-aligned max_align_t which
  875. // in turn gives is a 8-byte aligned malloc.
  876. // Emscripten's ABI is unstable and we may change this back to 128 to match
  877. // the WebAssembly default in the future.
  878. this->LongDoubleAlign = 64;
  879. }
  880. };
  881. } // namespace targets
  882. } // namespace clang
  883. #endif // LLVM_CLANG_LIB_BASIC_TARGETS_OSTARGETS_H