Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1104 lines
43 KiB

  1. //===-- Support/TargetRegistry.h - Target Registration ----------*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file exposes the TargetRegistry interface, which tools can use to access
  11. // the appropriate target specific classes (TargetMachine, AsmPrinter, etc.)
  12. // which have been registered.
  13. //
  14. // Target specific class implementations should register themselves using the
  15. // appropriate TargetRegistry interfaces.
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_SUPPORT_TARGETREGISTRY_H
  19. #define LLVM_SUPPORT_TARGETREGISTRY_H
  20. #include "llvm/ADT/Triple.h"
  21. #include "llvm/Support/CodeGen.h"
  22. #include <cassert>
  23. #include <string>
  24. namespace llvm {
  25. class AsmPrinter;
  26. class Module;
  27. class MCAssembler;
  28. class MCAsmBackend;
  29. class MCAsmInfo;
  30. class MCAsmParser;
  31. class MCCodeEmitter;
  32. class MCCodeGenInfo;
  33. class MCContext;
  34. class MCDisassembler;
  35. class MCInstrAnalysis;
  36. class MCInstPrinter;
  37. class MCInstrInfo;
  38. class MCRegisterInfo;
  39. class MCStreamer;
  40. class MCSubtargetInfo;
  41. class MCTargetAsmParser;
  42. class TargetMachine;
  43. class TargetOptions;
  44. class raw_ostream;
  45. class formatted_raw_ostream;
  46. MCStreamer *createAsmStreamer(MCContext &Ctx, formatted_raw_ostream &OS,
  47. bool isVerboseAsm,
  48. bool useLoc, bool useCFI,
  49. bool useDwarfDirectory,
  50. MCInstPrinter *InstPrint,
  51. MCCodeEmitter *CE,
  52. MCAsmBackend *TAB,
  53. bool ShowInst);
  54. /// Target - Wrapper for Target specific information.
  55. ///
  56. /// For registration purposes, this is a POD type so that targets can be
  57. /// registered without the use of static constructors.
  58. ///
  59. /// Targets should implement a single global instance of this class (which
  60. /// will be zero initialized), and pass that instance to the TargetRegistry as
  61. /// part of their initialization.
  62. class Target {
  63. public:
  64. friend struct TargetRegistry;
  65. typedef unsigned (*TripleMatchQualityFnTy)(const std::string &TT);
  66. typedef MCAsmInfo *(*MCAsmInfoCtorFnTy)(const Target &T,
  67. StringRef TT);
  68. typedef MCCodeGenInfo *(*MCCodeGenInfoCtorFnTy)(StringRef TT,
  69. Reloc::Model RM,
  70. CodeModel::Model CM,
  71. CodeGenOpt::Level OL);
  72. typedef MCInstrInfo *(*MCInstrInfoCtorFnTy)(void);
  73. typedef MCInstrAnalysis *(*MCInstrAnalysisCtorFnTy)(const MCInstrInfo*Info);
  74. typedef MCRegisterInfo *(*MCRegInfoCtorFnTy)(StringRef TT);
  75. typedef MCSubtargetInfo *(*MCSubtargetInfoCtorFnTy)(StringRef TT,
  76. StringRef CPU,
  77. StringRef Features);
  78. typedef TargetMachine *(*TargetMachineCtorTy)(const Target &T,
  79. StringRef TT,
  80. StringRef CPU,
  81. StringRef Features,
  82. const TargetOptions &Options,
  83. Reloc::Model RM,
  84. CodeModel::Model CM,
  85. CodeGenOpt::Level OL);
  86. typedef AsmPrinter *(*AsmPrinterCtorTy)(TargetMachine &TM,
  87. MCStreamer &Streamer);
  88. typedef MCAsmBackend *(*MCAsmBackendCtorTy)(const Target &T,
  89. StringRef TT,
  90. StringRef CPU);
  91. typedef MCTargetAsmParser *(*MCAsmParserCtorTy)(MCSubtargetInfo &STI,
  92. MCAsmParser &P);
  93. typedef MCDisassembler *(*MCDisassemblerCtorTy)(const Target &T,
  94. const MCSubtargetInfo &STI);
  95. typedef MCInstPrinter *(*MCInstPrinterCtorTy)(const Target &T,
  96. unsigned SyntaxVariant,
  97. const MCAsmInfo &MAI,
  98. const MCInstrInfo &MII,
  99. const MCRegisterInfo &MRI,
  100. const MCSubtargetInfo &STI);
  101. typedef MCCodeEmitter *(*MCCodeEmitterCtorTy)(const MCInstrInfo &II,
  102. const MCRegisterInfo &MRI,
  103. const MCSubtargetInfo &STI,
  104. MCContext &Ctx);
  105. typedef MCStreamer *(*MCObjectStreamerCtorTy)(const Target &T,
  106. StringRef TT,
  107. MCContext &Ctx,
  108. MCAsmBackend &TAB,
  109. raw_ostream &_OS,
  110. MCCodeEmitter *_Emitter,
  111. bool RelaxAll,
  112. bool NoExecStack);
  113. typedef MCStreamer *(*AsmStreamerCtorTy)(MCContext &Ctx,
  114. formatted_raw_ostream &OS,
  115. bool isVerboseAsm,
  116. bool useLoc,
  117. bool useCFI,
  118. bool useDwarfDirectory,
  119. MCInstPrinter *InstPrint,
  120. MCCodeEmitter *CE,
  121. MCAsmBackend *TAB,
  122. bool ShowInst);
  123. private:
  124. /// Next - The next registered target in the linked list, maintained by the
  125. /// TargetRegistry.
  126. Target *Next;
  127. /// TripleMatchQualityFn - The target function for rating the match quality
  128. /// of a triple.
  129. TripleMatchQualityFnTy TripleMatchQualityFn;
  130. /// Name - The target name.
  131. const char *Name;
  132. /// ShortDesc - A short description of the target.
  133. const char *ShortDesc;
  134. /// HasJIT - Whether this target supports the JIT.
  135. bool HasJIT;
  136. /// MCAsmInfoCtorFn - Constructor function for this target's MCAsmInfo, if
  137. /// registered.
  138. MCAsmInfoCtorFnTy MCAsmInfoCtorFn;
  139. /// MCCodeGenInfoCtorFn - Constructor function for this target's
  140. /// MCCodeGenInfo, if registered.
  141. MCCodeGenInfoCtorFnTy MCCodeGenInfoCtorFn;
  142. /// MCInstrInfoCtorFn - Constructor function for this target's MCInstrInfo,
  143. /// if registered.
  144. MCInstrInfoCtorFnTy MCInstrInfoCtorFn;
  145. /// MCInstrAnalysisCtorFn - Constructor function for this target's
  146. /// MCInstrAnalysis, if registered.
  147. MCInstrAnalysisCtorFnTy MCInstrAnalysisCtorFn;
  148. /// MCRegInfoCtorFn - Constructor function for this target's MCRegisterInfo,
  149. /// if registered.
  150. MCRegInfoCtorFnTy MCRegInfoCtorFn;
  151. /// MCSubtargetInfoCtorFn - Constructor function for this target's
  152. /// MCSubtargetInfo, if registered.
  153. MCSubtargetInfoCtorFnTy MCSubtargetInfoCtorFn;
  154. /// TargetMachineCtorFn - Construction function for this target's
  155. /// TargetMachine, if registered.
  156. TargetMachineCtorTy TargetMachineCtorFn;
  157. /// MCAsmBackendCtorFn - Construction function for this target's
  158. /// MCAsmBackend, if registered.
  159. MCAsmBackendCtorTy MCAsmBackendCtorFn;
  160. /// MCAsmParserCtorFn - Construction function for this target's
  161. /// MCTargetAsmParser, if registered.
  162. MCAsmParserCtorTy MCAsmParserCtorFn;
  163. /// AsmPrinterCtorFn - Construction function for this target's AsmPrinter,
  164. /// if registered.
  165. AsmPrinterCtorTy AsmPrinterCtorFn;
  166. /// MCDisassemblerCtorFn - Construction function for this target's
  167. /// MCDisassembler, if registered.
  168. MCDisassemblerCtorTy MCDisassemblerCtorFn;
  169. /// MCInstPrinterCtorFn - Construction function for this target's
  170. /// MCInstPrinter, if registered.
  171. MCInstPrinterCtorTy MCInstPrinterCtorFn;
  172. /// MCCodeEmitterCtorFn - Construction function for this target's
  173. /// CodeEmitter, if registered.
  174. MCCodeEmitterCtorTy MCCodeEmitterCtorFn;
  175. /// MCObjectStreamerCtorFn - Construction function for this target's
  176. /// MCObjectStreamer, if registered.
  177. MCObjectStreamerCtorTy MCObjectStreamerCtorFn;
  178. /// AsmStreamerCtorFn - Construction function for this target's
  179. /// AsmStreamer, if registered (default = llvm::createAsmStreamer).
  180. AsmStreamerCtorTy AsmStreamerCtorFn;
  181. public:
  182. Target() : AsmStreamerCtorFn(llvm::createAsmStreamer) {}
  183. /// @name Target Information
  184. /// @{
  185. // getNext - Return the next registered target.
  186. const Target *getNext() const { return Next; }
  187. /// getName - Get the target name.
  188. const char *getName() const { return Name; }
  189. /// getShortDescription - Get a short description of the target.
  190. const char *getShortDescription() const { return ShortDesc; }
  191. /// @}
  192. /// @name Feature Predicates
  193. /// @{
  194. /// hasJIT - Check if this targets supports the just-in-time compilation.
  195. bool hasJIT() const { return HasJIT; }
  196. /// hasTargetMachine - Check if this target supports code generation.
  197. bool hasTargetMachine() const { return TargetMachineCtorFn != 0; }
  198. /// hasMCAsmBackend - Check if this target supports .o generation.
  199. bool hasMCAsmBackend() const { return MCAsmBackendCtorFn != 0; }
  200. /// hasAsmParser - Check if this target supports .s parsing.
  201. bool hasMCAsmParser() const { return MCAsmParserCtorFn != 0; }
  202. /// hasAsmPrinter - Check if this target supports .s printing.
  203. bool hasAsmPrinter() const { return AsmPrinterCtorFn != 0; }
  204. /// hasMCDisassembler - Check if this target has a disassembler.
  205. bool hasMCDisassembler() const { return MCDisassemblerCtorFn != 0; }
  206. /// hasMCInstPrinter - Check if this target has an instruction printer.
  207. bool hasMCInstPrinter() const { return MCInstPrinterCtorFn != 0; }
  208. /// hasMCCodeEmitter - Check if this target supports instruction encoding.
  209. bool hasMCCodeEmitter() const { return MCCodeEmitterCtorFn != 0; }
  210. /// hasMCObjectStreamer - Check if this target supports streaming to files.
  211. bool hasMCObjectStreamer() const { return MCObjectStreamerCtorFn != 0; }
  212. /// hasAsmStreamer - Check if this target supports streaming to files.
  213. bool hasAsmStreamer() const { return AsmStreamerCtorFn != 0; }
  214. /// @}
  215. /// @name Feature Constructors
  216. /// @{
  217. /// createMCAsmInfo - Create a MCAsmInfo implementation for the specified
  218. /// target triple.
  219. ///
  220. /// \param Triple This argument is used to determine the target machine
  221. /// feature set; it should always be provided. Generally this should be
  222. /// either the target triple from the module, or the target triple of the
  223. /// host if that does not exist.
  224. MCAsmInfo *createMCAsmInfo(StringRef Triple) const {
  225. if (!MCAsmInfoCtorFn)
  226. return 0;
  227. return MCAsmInfoCtorFn(*this, Triple);
  228. }
  229. /// createMCCodeGenInfo - Create a MCCodeGenInfo implementation.
  230. ///
  231. MCCodeGenInfo *createMCCodeGenInfo(StringRef Triple, Reloc::Model RM,
  232. CodeModel::Model CM,
  233. CodeGenOpt::Level OL) const {
  234. if (!MCCodeGenInfoCtorFn)
  235. return 0;
  236. return MCCodeGenInfoCtorFn(Triple, RM, CM, OL);
  237. }
  238. /// createMCInstrInfo - Create a MCInstrInfo implementation.
  239. ///
  240. MCInstrInfo *createMCInstrInfo() const {
  241. if (!MCInstrInfoCtorFn)
  242. return 0;
  243. return MCInstrInfoCtorFn();
  244. }
  245. /// createMCInstrAnalysis - Create a MCInstrAnalysis implementation.
  246. ///
  247. MCInstrAnalysis *createMCInstrAnalysis(const MCInstrInfo *Info) const {
  248. if (!MCInstrAnalysisCtorFn)
  249. return 0;
  250. return MCInstrAnalysisCtorFn(Info);
  251. }
  252. /// createMCRegInfo - Create a MCRegisterInfo implementation.
  253. ///
  254. MCRegisterInfo *createMCRegInfo(StringRef Triple) const {
  255. if (!MCRegInfoCtorFn)
  256. return 0;
  257. return MCRegInfoCtorFn(Triple);
  258. }
  259. /// createMCSubtargetInfo - Create a MCSubtargetInfo implementation.
  260. ///
  261. /// \param Triple This argument is used to determine the target machine
  262. /// feature set; it should always be provided. Generally this should be
  263. /// either the target triple from the module, or the target triple of the
  264. /// host if that does not exist.
  265. /// \param CPU This specifies the name of the target CPU.
  266. /// \param Features This specifies the string representation of the
  267. /// additional target features.
  268. MCSubtargetInfo *createMCSubtargetInfo(StringRef Triple, StringRef CPU,
  269. StringRef Features) const {
  270. if (!MCSubtargetInfoCtorFn)
  271. return 0;
  272. return MCSubtargetInfoCtorFn(Triple, CPU, Features);
  273. }
  274. /// createTargetMachine - Create a target specific machine implementation
  275. /// for the specified \p Triple.
  276. ///
  277. /// \param Triple This argument is used to determine the target machine
  278. /// feature set; it should always be provided. Generally this should be
  279. /// either the target triple from the module, or the target triple of the
  280. /// host if that does not exist.
  281. TargetMachine *createTargetMachine(StringRef Triple, StringRef CPU,
  282. StringRef Features, const TargetOptions &Options,
  283. Reloc::Model RM = Reloc::Default,
  284. CodeModel::Model CM = CodeModel::Default,
  285. CodeGenOpt::Level OL = CodeGenOpt::Default) const {
  286. if (!TargetMachineCtorFn)
  287. return 0;
  288. return TargetMachineCtorFn(*this, Triple, CPU, Features, Options,
  289. RM, CM, OL);
  290. }
  291. /// createMCAsmBackend - Create a target specific assembly parser.
  292. ///
  293. /// \param Triple The target triple string.
  294. MCAsmBackend *createMCAsmBackend(StringRef Triple, StringRef CPU) const {
  295. if (!MCAsmBackendCtorFn)
  296. return 0;
  297. return MCAsmBackendCtorFn(*this, Triple, CPU);
  298. }
  299. /// createMCAsmParser - Create a target specific assembly parser.
  300. ///
  301. /// \param Parser The target independent parser implementation to use for
  302. /// parsing and lexing.
  303. MCTargetAsmParser *createMCAsmParser(MCSubtargetInfo &STI,
  304. MCAsmParser &Parser) const {
  305. if (!MCAsmParserCtorFn)
  306. return 0;
  307. return MCAsmParserCtorFn(STI, Parser);
  308. }
  309. /// createAsmPrinter - Create a target specific assembly printer pass. This
  310. /// takes ownership of the MCStreamer object.
  311. AsmPrinter *createAsmPrinter(TargetMachine &TM, MCStreamer &Streamer) const{
  312. if (!AsmPrinterCtorFn)
  313. return 0;
  314. return AsmPrinterCtorFn(TM, Streamer);
  315. }
  316. MCDisassembler *createMCDisassembler(const MCSubtargetInfo &STI) const {
  317. if (!MCDisassemblerCtorFn)
  318. return 0;
  319. return MCDisassemblerCtorFn(*this, STI);
  320. }
  321. MCInstPrinter *createMCInstPrinter(unsigned SyntaxVariant,
  322. const MCAsmInfo &MAI,
  323. const MCInstrInfo &MII,
  324. const MCRegisterInfo &MRI,
  325. const MCSubtargetInfo &STI) const {
  326. if (!MCInstPrinterCtorFn)
  327. return 0;
  328. return MCInstPrinterCtorFn(*this, SyntaxVariant, MAI, MII, MRI, STI);
  329. }
  330. /// createMCCodeEmitter - Create a target specific code emitter.
  331. MCCodeEmitter *createMCCodeEmitter(const MCInstrInfo &II,
  332. const MCRegisterInfo &MRI,
  333. const MCSubtargetInfo &STI,
  334. MCContext &Ctx) const {
  335. if (!MCCodeEmitterCtorFn)
  336. return 0;
  337. return MCCodeEmitterCtorFn(II, MRI, STI, Ctx);
  338. }
  339. /// createMCObjectStreamer - Create a target specific MCStreamer.
  340. ///
  341. /// \param TT The target triple.
  342. /// \param Ctx The target context.
  343. /// \param TAB The target assembler backend object. Takes ownership.
  344. /// \param _OS The stream object.
  345. /// \param _Emitter The target independent assembler object.Takes ownership.
  346. /// \param RelaxAll Relax all fixups?
  347. /// \param NoExecStack Mark file as not needing a executable stack.
  348. MCStreamer *createMCObjectStreamer(StringRef TT, MCContext &Ctx,
  349. MCAsmBackend &TAB,
  350. raw_ostream &_OS,
  351. MCCodeEmitter *_Emitter,
  352. bool RelaxAll,
  353. bool NoExecStack) const {
  354. if (!MCObjectStreamerCtorFn)
  355. return 0;
  356. return MCObjectStreamerCtorFn(*this, TT, Ctx, TAB, _OS, _Emitter,
  357. RelaxAll, NoExecStack);
  358. }
  359. /// createAsmStreamer - Create a target specific MCStreamer.
  360. MCStreamer *createAsmStreamer(MCContext &Ctx,
  361. formatted_raw_ostream &OS,
  362. bool isVerboseAsm,
  363. bool useLoc,
  364. bool useCFI,
  365. bool useDwarfDirectory,
  366. MCInstPrinter *InstPrint,
  367. MCCodeEmitter *CE,
  368. MCAsmBackend *TAB,
  369. bool ShowInst) const {
  370. // AsmStreamerCtorFn is default to llvm::createAsmStreamer
  371. return AsmStreamerCtorFn(Ctx, OS, isVerboseAsm, useLoc, useCFI,
  372. useDwarfDirectory, InstPrint, CE, TAB, ShowInst);
  373. }
  374. /// @}
  375. };
  376. /// TargetRegistry - Generic interface to target specific features.
  377. struct TargetRegistry {
  378. class iterator {
  379. const Target *Current;
  380. explicit iterator(Target *T) : Current(T) {}
  381. friend struct TargetRegistry;
  382. public:
  383. iterator(const iterator &I) : Current(I.Current) {}
  384. iterator() : Current(0) {}
  385. bool operator==(const iterator &x) const {
  386. return Current == x.Current;
  387. }
  388. bool operator!=(const iterator &x) const {
  389. return !operator==(x);
  390. }
  391. // Iterator traversal: forward iteration only
  392. iterator &operator++() { // Preincrement
  393. assert(Current && "Cannot increment end iterator!");
  394. Current = Current->getNext();
  395. return *this;
  396. }
  397. iterator operator++(int) { // Postincrement
  398. iterator tmp = *this;
  399. ++*this;
  400. return tmp;
  401. }
  402. const Target &operator*() const {
  403. assert(Current && "Cannot dereference end iterator!");
  404. return *Current;
  405. }
  406. const Target *operator->() const {
  407. return &operator*();
  408. }
  409. };
  410. /// printRegisteredTargetsForVersion - Print the registered targets
  411. /// appropriately for inclusion in a tool's version output.
  412. static void printRegisteredTargetsForVersion();
  413. /// @name Registry Access
  414. /// @{
  415. static iterator begin();
  416. static iterator end() { return iterator(); }
  417. /// lookupTarget - Lookup a target based on a target triple.
  418. ///
  419. /// \param Triple - The triple to use for finding a target.
  420. /// \param Error - On failure, an error string describing why no target was
  421. /// found.
  422. static const Target *lookupTarget(const std::string &Triple,
  423. std::string &Error);
  424. /// lookupTarget - Lookup a target based on an architecture name
  425. /// and a target triple. If the architecture name is non-empty,
  426. /// then the lookup is done by architecture. Otherwise, the target
  427. /// triple is used.
  428. ///
  429. /// \param ArchName - The architecture to use for finding a target.
  430. /// \param TheTriple - The triple to use for finding a target. The
  431. /// triple is updated with canonical architecture name if a lookup
  432. /// by architecture is done.
  433. /// \param Error - On failure, an error string describing why no target was
  434. /// found.
  435. static const Target *lookupTarget(const std::string &ArchName,
  436. Triple &TheTriple,
  437. std::string &Error);
  438. /// getClosestTargetForJIT - Pick the best target that is compatible with
  439. /// the current host. If no close target can be found, this returns null
  440. /// and sets the Error string to a reason.
  441. ///
  442. /// Maintained for compatibility through 2.6.
  443. static const Target *getClosestTargetForJIT(std::string &Error);
  444. /// @}
  445. /// @name Target Registration
  446. /// @{
  447. /// RegisterTarget - Register the given target. Attempts to register a
  448. /// target which has already been registered will be ignored.
  449. ///
  450. /// Clients are responsible for ensuring that registration doesn't occur
  451. /// while another thread is attempting to access the registry. Typically
  452. /// this is done by initializing all targets at program startup.
  453. ///
  454. /// @param T - The target being registered.
  455. /// @param Name - The target name. This should be a static string.
  456. /// @param ShortDesc - A short target description. This should be a static
  457. /// string.
  458. /// @param TQualityFn - The triple match quality computation function for
  459. /// this target.
  460. /// @param HasJIT - Whether the target supports JIT code
  461. /// generation.
  462. static void RegisterTarget(Target &T,
  463. const char *Name,
  464. const char *ShortDesc,
  465. Target::TripleMatchQualityFnTy TQualityFn,
  466. bool HasJIT = false);
  467. /// RegisterMCAsmInfo - Register a MCAsmInfo implementation for the
  468. /// given target.
  469. ///
  470. /// Clients are responsible for ensuring that registration doesn't occur
  471. /// while another thread is attempting to access the registry. Typically
  472. /// this is done by initializing all targets at program startup.
  473. ///
  474. /// @param T - The target being registered.
  475. /// @param Fn - A function to construct a MCAsmInfo for the target.
  476. static void RegisterMCAsmInfo(Target &T, Target::MCAsmInfoCtorFnTy Fn) {
  477. // Ignore duplicate registration.
  478. if (!T.MCAsmInfoCtorFn)
  479. T.MCAsmInfoCtorFn = Fn;
  480. }
  481. /// RegisterMCCodeGenInfo - Register a MCCodeGenInfo implementation for the
  482. /// given target.
  483. ///
  484. /// Clients are responsible for ensuring that registration doesn't occur
  485. /// while another thread is attempting to access the registry. Typically
  486. /// this is done by initializing all targets at program startup.
  487. ///
  488. /// @param T - The target being registered.
  489. /// @param Fn - A function to construct a MCCodeGenInfo for the target.
  490. static void RegisterMCCodeGenInfo(Target &T,
  491. Target::MCCodeGenInfoCtorFnTy Fn) {
  492. // Ignore duplicate registration.
  493. if (!T.MCCodeGenInfoCtorFn)
  494. T.MCCodeGenInfoCtorFn = Fn;
  495. }
  496. /// RegisterMCInstrInfo - Register a MCInstrInfo implementation for the
  497. /// given target.
  498. ///
  499. /// Clients are responsible for ensuring that registration doesn't occur
  500. /// while another thread is attempting to access the registry. Typically
  501. /// this is done by initializing all targets at program startup.
  502. ///
  503. /// @param T - The target being registered.
  504. /// @param Fn - A function to construct a MCInstrInfo for the target.
  505. static void RegisterMCInstrInfo(Target &T, Target::MCInstrInfoCtorFnTy Fn) {
  506. // Ignore duplicate registration.
  507. if (!T.MCInstrInfoCtorFn)
  508. T.MCInstrInfoCtorFn = Fn;
  509. }
  510. /// RegisterMCInstrAnalysis - Register a MCInstrAnalysis implementation for
  511. /// the given target.
  512. static void RegisterMCInstrAnalysis(Target &T,
  513. Target::MCInstrAnalysisCtorFnTy Fn) {
  514. // Ignore duplicate registration.
  515. if (!T.MCInstrAnalysisCtorFn)
  516. T.MCInstrAnalysisCtorFn = Fn;
  517. }
  518. /// RegisterMCRegInfo - Register a MCRegisterInfo implementation for the
  519. /// given target.
  520. ///
  521. /// Clients are responsible for ensuring that registration doesn't occur
  522. /// while another thread is attempting to access the registry. Typically
  523. /// this is done by initializing all targets at program startup.
  524. ///
  525. /// @param T - The target being registered.
  526. /// @param Fn - A function to construct a MCRegisterInfo for the target.
  527. static void RegisterMCRegInfo(Target &T, Target::MCRegInfoCtorFnTy Fn) {
  528. // Ignore duplicate registration.
  529. if (!T.MCRegInfoCtorFn)
  530. T.MCRegInfoCtorFn = Fn;
  531. }
  532. /// RegisterMCSubtargetInfo - Register a MCSubtargetInfo implementation for
  533. /// the given target.
  534. ///
  535. /// Clients are responsible for ensuring that registration doesn't occur
  536. /// while another thread is attempting to access the registry. Typically
  537. /// this is done by initializing all targets at program startup.
  538. ///
  539. /// @param T - The target being registered.
  540. /// @param Fn - A function to construct a MCSubtargetInfo for the target.
  541. static void RegisterMCSubtargetInfo(Target &T,
  542. Target::MCSubtargetInfoCtorFnTy Fn) {
  543. // Ignore duplicate registration.
  544. if (!T.MCSubtargetInfoCtorFn)
  545. T.MCSubtargetInfoCtorFn = Fn;
  546. }
  547. /// RegisterTargetMachine - Register a TargetMachine implementation for the
  548. /// given target.
  549. ///
  550. /// Clients are responsible for ensuring that registration doesn't occur
  551. /// while another thread is attempting to access the registry. Typically
  552. /// this is done by initializing all targets at program startup.
  553. ///
  554. /// @param T - The target being registered.
  555. /// @param Fn - A function to construct a TargetMachine for the target.
  556. static void RegisterTargetMachine(Target &T,
  557. Target::TargetMachineCtorTy Fn) {
  558. // Ignore duplicate registration.
  559. if (!T.TargetMachineCtorFn)
  560. T.TargetMachineCtorFn = Fn;
  561. }
  562. /// RegisterMCAsmBackend - Register a MCAsmBackend implementation for the
  563. /// given target.
  564. ///
  565. /// Clients are responsible for ensuring that registration doesn't occur
  566. /// while another thread is attempting to access the registry. Typically
  567. /// this is done by initializing all targets at program startup.
  568. ///
  569. /// @param T - The target being registered.
  570. /// @param Fn - A function to construct an AsmBackend for the target.
  571. static void RegisterMCAsmBackend(Target &T, Target::MCAsmBackendCtorTy Fn) {
  572. if (!T.MCAsmBackendCtorFn)
  573. T.MCAsmBackendCtorFn = Fn;
  574. }
  575. /// RegisterMCAsmParser - Register a MCTargetAsmParser implementation for
  576. /// the given target.
  577. ///
  578. /// Clients are responsible for ensuring that registration doesn't occur
  579. /// while another thread is attempting to access the registry. Typically
  580. /// this is done by initializing all targets at program startup.
  581. ///
  582. /// @param T - The target being registered.
  583. /// @param Fn - A function to construct an MCTargetAsmParser for the target.
  584. static void RegisterMCAsmParser(Target &T, Target::MCAsmParserCtorTy Fn) {
  585. if (!T.MCAsmParserCtorFn)
  586. T.MCAsmParserCtorFn = Fn;
  587. }
  588. /// RegisterAsmPrinter - Register an AsmPrinter implementation for the given
  589. /// target.
  590. ///
  591. /// Clients are responsible for ensuring that registration doesn't occur
  592. /// while another thread is attempting to access the registry. Typically
  593. /// this is done by initializing all targets at program startup.
  594. ///
  595. /// @param T - The target being registered.
  596. /// @param Fn - A function to construct an AsmPrinter for the target.
  597. static void RegisterAsmPrinter(Target &T, Target::AsmPrinterCtorTy Fn) {
  598. // Ignore duplicate registration.
  599. if (!T.AsmPrinterCtorFn)
  600. T.AsmPrinterCtorFn = Fn;
  601. }
  602. /// RegisterMCDisassembler - Register a MCDisassembler implementation for
  603. /// the given target.
  604. ///
  605. /// Clients are responsible for ensuring that registration doesn't occur
  606. /// while another thread is attempting to access the registry. Typically
  607. /// this is done by initializing all targets at program startup.
  608. ///
  609. /// @param T - The target being registered.
  610. /// @param Fn - A function to construct an MCDisassembler for the target.
  611. static void RegisterMCDisassembler(Target &T,
  612. Target::MCDisassemblerCtorTy Fn) {
  613. if (!T.MCDisassemblerCtorFn)
  614. T.MCDisassemblerCtorFn = Fn;
  615. }
  616. /// RegisterMCInstPrinter - Register a MCInstPrinter implementation for the
  617. /// given target.
  618. ///
  619. /// Clients are responsible for ensuring that registration doesn't occur
  620. /// while another thread is attempting to access the registry. Typically
  621. /// this is done by initializing all targets at program startup.
  622. ///
  623. /// @param T - The target being registered.
  624. /// @param Fn - A function to construct an MCInstPrinter for the target.
  625. static void RegisterMCInstPrinter(Target &T,
  626. Target::MCInstPrinterCtorTy Fn) {
  627. if (!T.MCInstPrinterCtorFn)
  628. T.MCInstPrinterCtorFn = Fn;
  629. }
  630. /// RegisterMCCodeEmitter - Register a MCCodeEmitter implementation for the
  631. /// given target.
  632. ///
  633. /// Clients are responsible for ensuring that registration doesn't occur
  634. /// while another thread is attempting to access the registry. Typically
  635. /// this is done by initializing all targets at program startup.
  636. ///
  637. /// @param T - The target being registered.
  638. /// @param Fn - A function to construct an MCCodeEmitter for the target.
  639. static void RegisterMCCodeEmitter(Target &T,
  640. Target::MCCodeEmitterCtorTy Fn) {
  641. if (!T.MCCodeEmitterCtorFn)
  642. T.MCCodeEmitterCtorFn = Fn;
  643. }
  644. /// RegisterMCObjectStreamer - Register a object code MCStreamer
  645. /// implementation for the given target.
  646. ///
  647. /// Clients are responsible for ensuring that registration doesn't occur
  648. /// while another thread is attempting to access the registry. Typically
  649. /// this is done by initializing all targets at program startup.
  650. ///
  651. /// @param T - The target being registered.
  652. /// @param Fn - A function to construct an MCStreamer for the target.
  653. static void RegisterMCObjectStreamer(Target &T,
  654. Target::MCObjectStreamerCtorTy Fn) {
  655. if (!T.MCObjectStreamerCtorFn)
  656. T.MCObjectStreamerCtorFn = Fn;
  657. }
  658. /// RegisterAsmStreamer - Register an assembly MCStreamer implementation
  659. /// for the given target.
  660. ///
  661. /// Clients are responsible for ensuring that registration doesn't occur
  662. /// while another thread is attempting to access the registry. Typically
  663. /// this is done by initializing all targets at program startup.
  664. ///
  665. /// @param T - The target being registered.
  666. /// @param Fn - A function to construct an MCStreamer for the target.
  667. static void RegisterAsmStreamer(Target &T, Target::AsmStreamerCtorTy Fn) {
  668. if (T.AsmStreamerCtorFn == createAsmStreamer)
  669. T.AsmStreamerCtorFn = Fn;
  670. }
  671. /// @}
  672. };
  673. //===--------------------------------------------------------------------===//
  674. /// RegisterTarget - Helper template for registering a target, for use in the
  675. /// target's initialization function. Usage:
  676. ///
  677. ///
  678. /// Target TheFooTarget; // The global target instance.
  679. ///
  680. /// extern "C" void LLVMInitializeFooTargetInfo() {
  681. /// RegisterTarget<Triple::foo> X(TheFooTarget, "foo", "Foo description");
  682. /// }
  683. template<Triple::ArchType TargetArchType = Triple::UnknownArch,
  684. bool HasJIT = false>
  685. struct RegisterTarget {
  686. RegisterTarget(Target &T, const char *Name, const char *Desc) {
  687. TargetRegistry::RegisterTarget(T, Name, Desc,
  688. &getTripleMatchQuality,
  689. HasJIT);
  690. }
  691. static unsigned getTripleMatchQuality(const std::string &TT) {
  692. if (Triple(TT).getArch() == TargetArchType)
  693. return 20;
  694. return 0;
  695. }
  696. };
  697. /// RegisterMCAsmInfo - Helper template for registering a target assembly info
  698. /// implementation. This invokes the static "Create" method on the class to
  699. /// actually do the construction. Usage:
  700. ///
  701. /// extern "C" void LLVMInitializeFooTarget() {
  702. /// extern Target TheFooTarget;
  703. /// RegisterMCAsmInfo<FooMCAsmInfo> X(TheFooTarget);
  704. /// }
  705. template<class MCAsmInfoImpl>
  706. struct RegisterMCAsmInfo {
  707. RegisterMCAsmInfo(Target &T) {
  708. TargetRegistry::RegisterMCAsmInfo(T, &Allocator);
  709. }
  710. private:
  711. static MCAsmInfo *Allocator(const Target &T, StringRef TT) {
  712. return new MCAsmInfoImpl(T, TT);
  713. }
  714. };
  715. /// RegisterMCAsmInfoFn - Helper template for registering a target assembly info
  716. /// implementation. This invokes the specified function to do the
  717. /// construction. Usage:
  718. ///
  719. /// extern "C" void LLVMInitializeFooTarget() {
  720. /// extern Target TheFooTarget;
  721. /// RegisterMCAsmInfoFn X(TheFooTarget, TheFunction);
  722. /// }
  723. struct RegisterMCAsmInfoFn {
  724. RegisterMCAsmInfoFn(Target &T, Target::MCAsmInfoCtorFnTy Fn) {
  725. TargetRegistry::RegisterMCAsmInfo(T, Fn);
  726. }
  727. };
  728. /// RegisterMCCodeGenInfo - Helper template for registering a target codegen info
  729. /// implementation. This invokes the static "Create" method on the class
  730. /// to actually do the construction. Usage:
  731. ///
  732. /// extern "C" void LLVMInitializeFooTarget() {
  733. /// extern Target TheFooTarget;
  734. /// RegisterMCCodeGenInfo<FooMCCodeGenInfo> X(TheFooTarget);
  735. /// }
  736. template<class MCCodeGenInfoImpl>
  737. struct RegisterMCCodeGenInfo {
  738. RegisterMCCodeGenInfo(Target &T) {
  739. TargetRegistry::RegisterMCCodeGenInfo(T, &Allocator);
  740. }
  741. private:
  742. static MCCodeGenInfo *Allocator(StringRef TT, Reloc::Model RM,
  743. CodeModel::Model CM, CodeGenOpt::Level OL) {
  744. return new MCCodeGenInfoImpl();
  745. }
  746. };
  747. /// RegisterMCCodeGenInfoFn - Helper template for registering a target codegen
  748. /// info implementation. This invokes the specified function to do the
  749. /// construction. Usage:
  750. ///
  751. /// extern "C" void LLVMInitializeFooTarget() {
  752. /// extern Target TheFooTarget;
  753. /// RegisterMCCodeGenInfoFn X(TheFooTarget, TheFunction);
  754. /// }
  755. struct RegisterMCCodeGenInfoFn {
  756. RegisterMCCodeGenInfoFn(Target &T, Target::MCCodeGenInfoCtorFnTy Fn) {
  757. TargetRegistry::RegisterMCCodeGenInfo(T, Fn);
  758. }
  759. };
  760. /// RegisterMCInstrInfo - Helper template for registering a target instruction
  761. /// info implementation. This invokes the static "Create" method on the class
  762. /// to actually do the construction. Usage:
  763. ///
  764. /// extern "C" void LLVMInitializeFooTarget() {
  765. /// extern Target TheFooTarget;
  766. /// RegisterMCInstrInfo<FooMCInstrInfo> X(TheFooTarget);
  767. /// }
  768. template<class MCInstrInfoImpl>
  769. struct RegisterMCInstrInfo {
  770. RegisterMCInstrInfo(Target &T) {
  771. TargetRegistry::RegisterMCInstrInfo(T, &Allocator);
  772. }
  773. private:
  774. static MCInstrInfo *Allocator() {
  775. return new MCInstrInfoImpl();
  776. }
  777. };
  778. /// RegisterMCInstrInfoFn - Helper template for registering a target
  779. /// instruction info implementation. This invokes the specified function to
  780. /// do the construction. Usage:
  781. ///
  782. /// extern "C" void LLVMInitializeFooTarget() {
  783. /// extern Target TheFooTarget;
  784. /// RegisterMCInstrInfoFn X(TheFooTarget, TheFunction);
  785. /// }
  786. struct RegisterMCInstrInfoFn {
  787. RegisterMCInstrInfoFn(Target &T, Target::MCInstrInfoCtorFnTy Fn) {
  788. TargetRegistry::RegisterMCInstrInfo(T, Fn);
  789. }
  790. };
  791. /// RegisterMCInstrAnalysis - Helper template for registering a target
  792. /// instruction analyzer implementation. This invokes the static "Create"
  793. /// method on the class to actually do the construction. Usage:
  794. ///
  795. /// extern "C" void LLVMInitializeFooTarget() {
  796. /// extern Target TheFooTarget;
  797. /// RegisterMCInstrAnalysis<FooMCInstrAnalysis> X(TheFooTarget);
  798. /// }
  799. template<class MCInstrAnalysisImpl>
  800. struct RegisterMCInstrAnalysis {
  801. RegisterMCInstrAnalysis(Target &T) {
  802. TargetRegistry::RegisterMCInstrAnalysis(T, &Allocator);
  803. }
  804. private:
  805. static MCInstrAnalysis *Allocator(const MCInstrInfo *Info) {
  806. return new MCInstrAnalysisImpl(Info);
  807. }
  808. };
  809. /// RegisterMCInstrAnalysisFn - Helper template for registering a target
  810. /// instruction analyzer implementation. This invokes the specified function
  811. /// to do the construction. Usage:
  812. ///
  813. /// extern "C" void LLVMInitializeFooTarget() {
  814. /// extern Target TheFooTarget;
  815. /// RegisterMCInstrAnalysisFn X(TheFooTarget, TheFunction);
  816. /// }
  817. struct RegisterMCInstrAnalysisFn {
  818. RegisterMCInstrAnalysisFn(Target &T, Target::MCInstrAnalysisCtorFnTy Fn) {
  819. TargetRegistry::RegisterMCInstrAnalysis(T, Fn);
  820. }
  821. };
  822. /// RegisterMCRegInfo - Helper template for registering a target register info
  823. /// implementation. This invokes the static "Create" method on the class to
  824. /// actually do the construction. Usage:
  825. ///
  826. /// extern "C" void LLVMInitializeFooTarget() {
  827. /// extern Target TheFooTarget;
  828. /// RegisterMCRegInfo<FooMCRegInfo> X(TheFooTarget);
  829. /// }
  830. template<class MCRegisterInfoImpl>
  831. struct RegisterMCRegInfo {
  832. RegisterMCRegInfo(Target &T) {
  833. TargetRegistry::RegisterMCRegInfo(T, &Allocator);
  834. }
  835. private:
  836. static MCRegisterInfo *Allocator(StringRef TT) {
  837. return new MCRegisterInfoImpl();
  838. }
  839. };
  840. /// RegisterMCRegInfoFn - Helper template for registering a target register
  841. /// info implementation. This invokes the specified function to do the
  842. /// construction. Usage:
  843. ///
  844. /// extern "C" void LLVMInitializeFooTarget() {
  845. /// extern Target TheFooTarget;
  846. /// RegisterMCRegInfoFn X(TheFooTarget, TheFunction);
  847. /// }
  848. struct RegisterMCRegInfoFn {
  849. RegisterMCRegInfoFn(Target &T, Target::MCRegInfoCtorFnTy Fn) {
  850. TargetRegistry::RegisterMCRegInfo(T, Fn);
  851. }
  852. };
  853. /// RegisterMCSubtargetInfo - Helper template for registering a target
  854. /// subtarget info implementation. This invokes the static "Create" method
  855. /// on the class to actually do the construction. Usage:
  856. ///
  857. /// extern "C" void LLVMInitializeFooTarget() {
  858. /// extern Target TheFooTarget;
  859. /// RegisterMCSubtargetInfo<FooMCSubtargetInfo> X(TheFooTarget);
  860. /// }
  861. template<class MCSubtargetInfoImpl>
  862. struct RegisterMCSubtargetInfo {
  863. RegisterMCSubtargetInfo(Target &T) {
  864. TargetRegistry::RegisterMCSubtargetInfo(T, &Allocator);
  865. }
  866. private:
  867. static MCSubtargetInfo *Allocator(StringRef TT, StringRef CPU,
  868. StringRef FS) {
  869. return new MCSubtargetInfoImpl();
  870. }
  871. };
  872. /// RegisterMCSubtargetInfoFn - Helper template for registering a target
  873. /// subtarget info implementation. This invokes the specified function to
  874. /// do the construction. Usage:
  875. ///
  876. /// extern "C" void LLVMInitializeFooTarget() {
  877. /// extern Target TheFooTarget;
  878. /// RegisterMCSubtargetInfoFn X(TheFooTarget, TheFunction);
  879. /// }
  880. struct RegisterMCSubtargetInfoFn {
  881. RegisterMCSubtargetInfoFn(Target &T, Target::MCSubtargetInfoCtorFnTy Fn) {
  882. TargetRegistry::RegisterMCSubtargetInfo(T, Fn);
  883. }
  884. };
  885. /// RegisterTargetMachine - Helper template for registering a target machine
  886. /// implementation, for use in the target machine initialization
  887. /// function. Usage:
  888. ///
  889. /// extern "C" void LLVMInitializeFooTarget() {
  890. /// extern Target TheFooTarget;
  891. /// RegisterTargetMachine<FooTargetMachine> X(TheFooTarget);
  892. /// }
  893. template<class TargetMachineImpl>
  894. struct RegisterTargetMachine {
  895. RegisterTargetMachine(Target &T) {
  896. TargetRegistry::RegisterTargetMachine(T, &Allocator);
  897. }
  898. private:
  899. static TargetMachine *Allocator(const Target &T, StringRef TT,
  900. StringRef CPU, StringRef FS,
  901. const TargetOptions &Options,
  902. Reloc::Model RM,
  903. CodeModel::Model CM,
  904. CodeGenOpt::Level OL) {
  905. return new TargetMachineImpl(T, TT, CPU, FS, Options, RM, CM, OL);
  906. }
  907. };
  908. /// RegisterMCAsmBackend - Helper template for registering a target specific
  909. /// assembler backend. Usage:
  910. ///
  911. /// extern "C" void LLVMInitializeFooMCAsmBackend() {
  912. /// extern Target TheFooTarget;
  913. /// RegisterMCAsmBackend<FooAsmLexer> X(TheFooTarget);
  914. /// }
  915. template<class MCAsmBackendImpl>
  916. struct RegisterMCAsmBackend {
  917. RegisterMCAsmBackend(Target &T) {
  918. TargetRegistry::RegisterMCAsmBackend(T, &Allocator);
  919. }
  920. private:
  921. static MCAsmBackend *Allocator(const Target &T, StringRef Triple,
  922. StringRef CPU) {
  923. return new MCAsmBackendImpl(T, Triple, CPU);
  924. }
  925. };
  926. /// RegisterMCAsmParser - Helper template for registering a target specific
  927. /// assembly parser, for use in the target machine initialization
  928. /// function. Usage:
  929. ///
  930. /// extern "C" void LLVMInitializeFooMCAsmParser() {
  931. /// extern Target TheFooTarget;
  932. /// RegisterMCAsmParser<FooAsmParser> X(TheFooTarget);
  933. /// }
  934. template<class MCAsmParserImpl>
  935. struct RegisterMCAsmParser {
  936. RegisterMCAsmParser(Target &T) {
  937. TargetRegistry::RegisterMCAsmParser(T, &Allocator);
  938. }
  939. private:
  940. static MCTargetAsmParser *Allocator(MCSubtargetInfo &STI, MCAsmParser &P) {
  941. return new MCAsmParserImpl(STI, P);
  942. }
  943. };
  944. /// RegisterAsmPrinter - Helper template for registering a target specific
  945. /// assembly printer, for use in the target machine initialization
  946. /// function. Usage:
  947. ///
  948. /// extern "C" void LLVMInitializeFooAsmPrinter() {
  949. /// extern Target TheFooTarget;
  950. /// RegisterAsmPrinter<FooAsmPrinter> X(TheFooTarget);
  951. /// }
  952. template<class AsmPrinterImpl>
  953. struct RegisterAsmPrinter {
  954. RegisterAsmPrinter(Target &T) {
  955. TargetRegistry::RegisterAsmPrinter(T, &Allocator);
  956. }
  957. private:
  958. static AsmPrinter *Allocator(TargetMachine &TM, MCStreamer &Streamer) {
  959. return new AsmPrinterImpl(TM, Streamer);
  960. }
  961. };
  962. /// RegisterMCCodeEmitter - Helper template for registering a target specific
  963. /// machine code emitter, for use in the target initialization
  964. /// function. Usage:
  965. ///
  966. /// extern "C" void LLVMInitializeFooMCCodeEmitter() {
  967. /// extern Target TheFooTarget;
  968. /// RegisterMCCodeEmitter<FooCodeEmitter> X(TheFooTarget);
  969. /// }
  970. template<class MCCodeEmitterImpl>
  971. struct RegisterMCCodeEmitter {
  972. RegisterMCCodeEmitter(Target &T) {
  973. TargetRegistry::RegisterMCCodeEmitter(T, &Allocator);
  974. }
  975. private:
  976. static MCCodeEmitter *Allocator(const MCInstrInfo &II,
  977. const MCRegisterInfo &MRI,
  978. const MCSubtargetInfo &STI,
  979. MCContext &Ctx) {
  980. return new MCCodeEmitterImpl();
  981. }
  982. };
  983. }
  984. #endif