2
0

TargetRegistry.h 45 KB

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