LLVM  14.0.0git
RISCVMCTargetDesc.cpp
Go to the documentation of this file.
1 //===-- RISCVMCTargetDesc.cpp - RISCV Target Descriptions -----------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 ///
9 /// This file provides RISCV-specific target descriptions.
10 ///
11 //===----------------------------------------------------------------------===//
12 
13 #include "RISCVMCTargetDesc.h"
14 #include "RISCVBaseInfo.h"
15 #include "RISCVELFStreamer.h"
16 #include "RISCVInstPrinter.h"
17 #include "RISCVMCAsmInfo.h"
18 #include "RISCVMCObjectFileInfo.h"
19 #include "RISCVTargetStreamer.h"
21 #include "llvm/ADT/STLExtras.h"
22 #include "llvm/MC/MCAsmBackend.h"
23 #include "llvm/MC/MCAsmInfo.h"
24 #include "llvm/MC/MCCodeEmitter.h"
26 #include "llvm/MC/MCInstrInfo.h"
28 #include "llvm/MC/MCObjectWriter.h"
29 #include "llvm/MC/MCRegisterInfo.h"
30 #include "llvm/MC/MCStreamer.h"
34 
35 #define GET_INSTRINFO_MC_DESC
36 #include "RISCVGenInstrInfo.inc"
37 
38 #define GET_REGINFO_MC_DESC
39 #include "RISCVGenRegisterInfo.inc"
40 
41 #define GET_SUBTARGETINFO_MC_DESC
42 #include "RISCVGenSubtargetInfo.inc"
43 
44 using namespace llvm;
45 
47  MCInstrInfo *X = new MCInstrInfo();
48  InitRISCVMCInstrInfo(X);
49  return X;
50 }
51 
54  InitRISCVMCRegisterInfo(X, RISCV::X1);
55  return X;
56 }
57 
59  const Triple &TT,
60  const MCTargetOptions &Options) {
61  MCAsmInfo *MAI = new RISCVMCAsmInfo(TT);
62 
63  MCRegister SP = MRI.getDwarfRegNum(RISCV::X2, true);
64  MCCFIInstruction Inst = MCCFIInstruction::cfiDefCfa(nullptr, SP, 0);
65  MAI->addInitialFrameState(Inst);
66 
67  return MAI;
68 }
69 
70 static MCObjectFileInfo *
72  bool LargeCodeModel = false) {
74  MOFI->initMCObjectFileInfo(Ctx, PIC, LargeCodeModel);
75  return MOFI;
76 }
77 
79  StringRef CPU, StringRef FS) {
80  if (CPU.empty())
81  CPU = TT.isArch64Bit() ? "generic-rv64" : "generic-rv32";
82  if (CPU == "generic")
83  report_fatal_error(Twine("CPU 'generic' is not supported. Use ") +
84  (TT.isArch64Bit() ? "generic-rv64" : "generic-rv32"));
85  return createRISCVMCSubtargetInfoImpl(TT, CPU, /*TuneCPU*/ CPU, FS);
86 }
87 
89  unsigned SyntaxVariant,
90  const MCAsmInfo &MAI,
91  const MCInstrInfo &MII,
92  const MCRegisterInfo &MRI) {
93  return new RISCVInstPrinter(MAI, MII, MRI);
94 }
95 
96 static MCTargetStreamer *
98  const Triple &TT = STI.getTargetTriple();
99  if (TT.isOSBinFormatELF())
100  return new RISCVTargetELFStreamer(S, STI);
101  return nullptr;
102 }
103 
106  MCInstPrinter *InstPrint,
107  bool isVerboseAsm) {
108  return new RISCVTargetAsmStreamer(S, OS);
109 }
110 
112  return new RISCVTargetStreamer(S);
113 }
114 
115 namespace {
116 
117 class RISCVMCInstrAnalysis : public MCInstrAnalysis {
118 public:
119  explicit RISCVMCInstrAnalysis(const MCInstrInfo *Info)
120  : MCInstrAnalysis(Info) {}
121 
122  bool evaluateBranch(const MCInst &Inst, uint64_t Addr, uint64_t Size,
123  uint64_t &Target) const override {
124  if (isConditionalBranch(Inst)) {
125  int64_t Imm;
126  if (Size == 2)
127  Imm = Inst.getOperand(1).getImm();
128  else
129  Imm = Inst.getOperand(2).getImm();
130  Target = Addr + Imm;
131  return true;
132  }
133 
134  if (Inst.getOpcode() == RISCV::C_JAL || Inst.getOpcode() == RISCV::C_J) {
135  Target = Addr + Inst.getOperand(0).getImm();
136  return true;
137  }
138 
139  if (Inst.getOpcode() == RISCV::JAL) {
140  Target = Addr + Inst.getOperand(1).getImm();
141  return true;
142  }
143 
144  return false;
145  }
146 };
147 
148 } // end anonymous namespace
149 
151  return new RISCVMCInstrAnalysis(Info);
152 }
153 
154 namespace {
156  std::unique_ptr<MCAsmBackend> &&MAB,
157  std::unique_ptr<MCObjectWriter> &&MOW,
158  std::unique_ptr<MCCodeEmitter> &&MCE,
159  bool RelaxAll) {
161  std::move(MCE), RelaxAll);
162 }
163 } // end anonymous namespace
164 
166  for (Target *T : {&getTheRISCV32Target(), &getTheRISCV64Target()}) {
179 
180  // Register the asm target streamer.
182  // Register the null target streamer.
185  }
186 }
llvm::Check::Size
@ Size
Definition: FileCheck.h:73
llvm::createRISCVELFStreamer
MCELFStreamer * createRISCVELFStreamer(MCContext &C, std::unique_ptr< MCAsmBackend > MAB, std::unique_ptr< MCObjectWriter > MOW, std::unique_ptr< MCCodeEmitter > MCE, bool RelaxAll)
Definition: RISCVELFStreamer.cpp:252
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
llvm::StringRef::empty
LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:153
llvm::getTheRISCV64Target
Target & getTheRISCV64Target()
Definition: RISCVTargetInfo.cpp:18
RISCVELFStreamer.h
llvm::MCContext
Context object for machine code objects.
Definition: MCContext.h:72
MCCodeEmitter.h
llvm::Target
Target - Wrapper for Target specific information.
Definition: TargetRegistry.h:137
llvm::MCAsmInfo
This class is intended to be used as a base class for asm properties and features specific to the tar...
Definition: MCAsmInfo.h:56
createRISCVNullTargetStreamer
static MCTargetStreamer * createRISCVNullTargetStreamer(MCStreamer &S)
Definition: RISCVMCTargetDesc.cpp:111
ErrorHandling.h
llvm::TargetRegistry::RegisterAsmTargetStreamer
static void RegisterAsmTargetStreamer(Target &T, Target::AsmTargetStreamerCtorTy Fn)
Definition: TargetRegistry.h:972
llvm::TargetRegistry::RegisterMCInstrAnalysis
static void RegisterMCInstrAnalysis(Target &T, Target::MCInstrAnalysisCtorFnTy Fn)
RegisterMCInstrAnalysis - Register a MCInstrAnalysis implementation for the given target.
Definition: TargetRegistry.h:823
llvm::Triple
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:45
MCObjectFileInfo.h
PIC
PassInstrumentationCallbacks PIC
Definition: PassBuilderBindings.cpp:55
RISCVMCObjectFileInfo.h
T
#define T
Definition: Mips16ISelLowering.cpp:341
llvm::TargetRegistry::RegisterMCObjectFileInfo
static void RegisterMCObjectFileInfo(Target &T, Target::MCObjectFileInfoCtorFnTy Fn)
Register a MCObjectFileInfo implementation for the given target.
Definition: TargetRegistry.h:803
STLExtras.h
llvm::MCInst
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:184
createRISCVMCSubtargetInfo
static MCSubtargetInfo * createRISCVMCSubtargetInfo(const Triple &TT, StringRef CPU, StringRef FS)
Definition: RISCVMCTargetDesc.cpp:78
llvm::TargetRegistry::RegisterMCInstPrinter
static void RegisterMCInstPrinter(Target &T, Target::MCInstPrinterCtorTy Fn)
RegisterMCInstPrinter - Register a MCInstPrinter implementation for the given target.
Definition: TargetRegistry.h:930
createRISCVObjectTargetStreamer
static MCTargetStreamer * createRISCVObjectTargetStreamer(MCStreamer &S, const MCSubtargetInfo &STI)
Definition: RISCVMCTargetDesc.cpp:97
RISCVInstPrinter.h
Context
LLVMContext & Context
Definition: NVVMIntrRange.cpp:66
createRISCVAsmTargetStreamer
static MCTargetStreamer * createRISCVAsmTargetStreamer(MCStreamer &S, formatted_raw_ostream &OS, MCInstPrinter *InstPrint, bool isVerboseAsm)
Definition: RISCVMCTargetDesc.cpp:104
llvm::MCStreamer
Streaming machine code generation interface.
Definition: MCStreamer.h:199
llvm::createRISCVAsmBackend
MCAsmBackend * createRISCVAsmBackend(const Target &T, const MCSubtargetInfo &STI, const MCRegisterInfo &MRI, const MCTargetOptions &Options)
Definition: RISCVAsmBackend.cpp:638
MCAsmBackend.h
llvm::RISCVInstPrinter
Definition: RISCVInstPrinter.h:21
LLVMInitializeRISCVTargetMC
LLVM_EXTERNAL_VISIBILITY void LLVMInitializeRISCVTargetMC()
Definition: RISCVMCTargetDesc.cpp:165
llvm::MCSubtargetInfo::getTargetTriple
const Triple & getTargetTriple() const
Definition: MCSubtargetInfo.h:107
llvm::RISCVMCAsmInfo
Definition: RISCVMCAsmInfo.h:21
MCInstrInfo.h
llvm::MCOperand::getImm
int64_t getImm() const
Definition: MCInst.h:80
llvm::MCInstrAnalysis
Definition: MCInstrAnalysis.h:27
MCSubtargetInfo.h
RISCVMCTargetDesc.h
llvm::report_fatal_error
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:140
Options
const char LLVMTargetMachineRef LLVMPassBuilderOptionsRef Options
Definition: PassBuilderBindings.cpp:48
llvm::MCTargetStreamer
Target specific streamer interface.
Definition: MCStreamer.h:91
Info
Analysis containing CSE Info
Definition: CSEInfo.cpp:27
llvm::MCCFIInstruction
Definition: MCDwarf.h:434
X
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
RISCVTargetStreamer.h
llvm::TargetRegistry::RegisterMCAsmBackend
static void RegisterMCAsmBackend(Target &T, Target::MCAsmBackendCtorTy Fn)
RegisterMCAsmBackend - Register a MCAsmBackend implementation for the given target.
Definition: TargetRegistry.h:877
uint64_t
llvm::MCInstPrinter
This is an instance of a target assembly language printer that converts an MCInst to valid target ass...
Definition: MCInstPrinter.h:43
LLVM_EXTERNAL_VISIBILITY
#define LLVM_EXTERNAL_VISIBILITY
Definition: Compiler.h:132
Addr
uint64_t Addr
Definition: ELFObjHandler.cpp:80
createRISCVMCObjectFileInfo
static MCObjectFileInfo * createRISCVMCObjectFileInfo(MCContext &Ctx, bool PIC, bool LargeCodeModel=false)
Definition: RISCVMCTargetDesc.cpp:71
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
llvm::createRISCVMCCodeEmitter
MCCodeEmitter * createRISCVMCCodeEmitter(const MCInstrInfo &MCII, const MCRegisterInfo &MRI, MCContext &Ctx)
Definition: RISCVMCCodeEmitter.cpp:95
llvm::TargetRegistry::RegisterObjectTargetStreamer
static void RegisterObjectTargetStreamer(Target &T, Target::ObjectTargetStreamerCtorTy Fn)
Definition: TargetRegistry.h:978
MCRegisterInfo.h
RISCVMCAsmInfo.h
llvm::formatted_raw_ostream
formatted_raw_ostream - A raw_ostream that wraps another one and keeps track of line and column posit...
Definition: FormattedStream.h:30
llvm::TargetRegistry::RegisterMCAsmInfo
static void RegisterMCAsmInfo(Target &T, Target::MCAsmInfoCtorFnTy Fn)
RegisterMCAsmInfo - Register a MCAsmInfo implementation for the given target.
Definition: TargetRegistry.h:791
llvm::MCCFIInstruction::cfiDefCfa
static MCCFIInstruction cfiDefCfa(MCSymbol *L, unsigned Register, int Offset)
.cfi_def_cfa defines a rule for computing CFA as: take address from Register and add Offset to it.
Definition: MCDwarf.h:488
llvm::MCAsmInfo::addInitialFrameState
void addInitialFrameState(const MCCFIInstruction &Inst)
Definition: MCAsmInfo.cpp:86
createRISCVInstrAnalysis
static MCInstrAnalysis * createRISCVInstrAnalysis(const MCInstrInfo *Info)
Definition: RISCVMCTargetDesc.cpp:150
createRISCVMCInstrInfo
static MCInstrInfo * createRISCVMCInstrInfo()
Definition: RISCVMCTargetDesc.cpp:46
llvm::X86AS::FS
@ FS
Definition: X86.h:188
createRISCVMCInstPrinter
static MCInstPrinter * createRISCVMCInstPrinter(const Triple &T, unsigned SyntaxVariant, const MCAsmInfo &MAI, const MCInstrInfo &MII, const MCRegisterInfo &MRI)
Definition: RISCVMCTargetDesc.cpp:88
llvm::TargetRegistry::RegisterMCSubtargetInfo
static void RegisterMCSubtargetInfo(Target &T, Target::MCSubtargetInfoCtorFnTy Fn)
RegisterMCSubtargetInfo - Register a MCSubtargetInfo implementation for the given target.
Definition: TargetRegistry.h:850
llvm::MCTargetOptions
Definition: MCTargetOptions.h:36
llvm::MCObjectFileInfo::initMCObjectFileInfo
void initMCObjectFileInfo(MCContext &MCCtx, bool PIC, bool LargeCodeModel=false)
Definition: MCObjectFileInfo.cpp:986
llvm::RISCVTargetAsmStreamer
Definition: RISCVTargetStreamer.h:42
MCInstrAnalysis.h
MCAsmInfo.h
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
S
add sub stmia L5 ldr r0 bl L_printf $stub Instead of a and a wouldn t it be better to do three moves *Return an aggregate type is even return S
Definition: README.txt:210
llvm::MCRegisterInfo
MCRegisterInfo base class - We assume that the target defines a static array of MCRegisterDesc object...
Definition: MCRegisterInfo.h:135
MRI
unsigned const MachineRegisterInfo * MRI
Definition: AArch64AdvSIMDScalarPass.cpp:105
MCObjectWriter.h
llvm::TargetRegistry::RegisterMCInstrInfo
static void RegisterMCInstrInfo(Target &T, Target::MCInstrInfoCtorFnTy Fn)
RegisterMCInstrInfo - Register a MCInstrInfo implementation for the given target.
Definition: TargetRegistry.h:817
llvm::Twine
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:83
llvm::TargetRegistry::RegisterMCCodeEmitter
static void RegisterMCCodeEmitter(Target &T, Target::MCCodeEmitterCtorTy Fn)
RegisterMCCodeEmitter - Register a MCCodeEmitter implementation for the given target.
Definition: TargetRegistry.h:943
llvm::MCInstrInfo
Interface to description of machine instruction set.
Definition: MCInstrInfo.h:25
createRISCVMCRegisterInfo
static MCRegisterInfo * createRISCVMCRegisterInfo(const Triple &TT)
Definition: RISCVMCTargetDesc.cpp:52
llvm::TargetRegistry::RegisterMCRegInfo
static void RegisterMCRegInfo(Target &T, Target::MCRegInfoCtorFnTy Fn)
RegisterMCRegInfo - Register a MCRegisterInfo implementation for the given target.
Definition: TargetRegistry.h:837
llvm::TargetRegistry::RegisterELFStreamer
static void RegisterELFStreamer(Target &T, Target::ELFStreamerCtorTy Fn)
Definition: TargetRegistry.h:955
llvm::MCInst::getOpcode
unsigned getOpcode() const
Definition: MCInst.h:198
RISCVBaseInfo.h
llvm::TargetRegistry::RegisterNullTargetStreamer
static void RegisterNullTargetStreamer(Target &T, Target::NullTargetStreamerCtorTy Fn)
Definition: TargetRegistry.h:967
llvm::RISCVTargetELFStreamer
Definition: RISCVELFStreamer.h:17
MCStreamer.h
llvm::MCInst::getOperand
const MCOperand & getOperand(unsigned i) const
Definition: MCInst.h:206
RISCVTargetInfo.h
llvm::RISCVTargetStreamer
Definition: RISCVTargetStreamer.h:19
llvm::RISCVMCObjectFileInfo
Definition: RISCVMCObjectFileInfo.h:20
TargetRegistry.h
llvm::MCSubtargetInfo
Generic base class for all target subtargets.
Definition: MCSubtargetInfo.h:75
createRISCVMCAsmInfo
static MCAsmInfo * createRISCVMCAsmInfo(const MCRegisterInfo &MRI, const Triple &TT, const MCTargetOptions &Options)
Definition: RISCVMCTargetDesc.cpp:58
llvm::getTheRISCV32Target
Target & getTheRISCV32Target()
Definition: RISCVTargetInfo.cpp:13
llvm::MCObjectFileInfo
Definition: MCObjectFileInfo.h:26
llvm::MCRegister
Wrapper class representing physical registers. Should be passed by value.
Definition: MCRegister.h:23