TargetRegistry.h 54 KB

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