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