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