LLVM 20.0.0git
LoongArchMCTargetDesc.cpp
Go to the documentation of this file.
1//===-- LoongArchMCTargetDesc.cpp - LoongArch 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 LoongArch specific target descriptions.
10//
11//===----------------------------------------------------------------------===//
12
16#include "LoongArchMCAsmInfo.h"
19#include "llvm/MC/MCAsmInfo.h"
21#include "llvm/MC/MCDwarf.h"
23#include "llvm/MC/MCInstrInfo.h"
29
30#define GET_INSTRINFO_MC_DESC
31#define ENABLE_INSTR_PREDICATE_VERIFIER
32#include "LoongArchGenInstrInfo.inc"
33
34#define GET_REGINFO_MC_DESC
35#include "LoongArchGenRegisterInfo.inc"
36
37#define GET_SUBTARGETINFO_MC_DESC
38#include "LoongArchGenSubtargetInfo.inc"
39
40using namespace llvm;
41
44 InitLoongArchMCRegisterInfo(X, LoongArch::R1);
45 return X;
46}
47
49 MCInstrInfo *X = new MCInstrInfo();
50 InitLoongArchMCInstrInfo(X);
51 return X;
52}
53
54static MCSubtargetInfo *
56 if (CPU.empty() || CPU == "generic")
57 CPU = TT.isArch64Bit() ? "generic-la64" : "generic-la32";
58 return createLoongArchMCSubtargetInfoImpl(TT, CPU, /*TuneCPU*/ CPU, FS);
59}
60
62 const Triple &TT,
63 const MCTargetOptions &Options) {
64 MCAsmInfo *MAI = new LoongArchMCAsmInfo(TT);
65
66 // Initial state of the frame pointer is sp(r3).
67 unsigned SP = MRI.getDwarfRegNum(LoongArch::R3, true);
68 MCCFIInstruction Inst = MCCFIInstruction::cfiDefCfa(nullptr, SP, 0);
69 MAI->addInitialFrameState(Inst);
70
71 return MAI;
72}
73
75 unsigned SyntaxVariant,
76 const MCAsmInfo &MAI,
77 const MCInstrInfo &MII,
78 const MCRegisterInfo &MRI) {
79 return new LoongArchInstPrinter(MAI, MII, MRI);
80}
81
82static MCTargetStreamer *
85 ? new LoongArchTargetELFStreamer(S, STI)
86 : nullptr;
87}
88
89static MCTargetStreamer *
91 MCInstPrinter *InstPrint) {
92 return new LoongArchTargetAsmStreamer(S, OS);
93}
94
95namespace {
96
97class LoongArchMCInstrAnalysis : public MCInstrAnalysis {
98public:
99 explicit LoongArchMCInstrAnalysis(const MCInstrInfo *Info)
101
102 bool evaluateBranch(const MCInst &Inst, uint64_t Addr, uint64_t Size,
103 uint64_t &Target) const override {
104 unsigned NumOps = Inst.getNumOperands();
105 if ((isBranch(Inst) && !isIndirectBranch(Inst)) ||
106 Inst.getOpcode() == LoongArch::BL) {
107 Target = Addr + Inst.getOperand(NumOps - 1).getImm();
108 return true;
109 }
110
111 return false;
112 }
113
114 bool isTerminator(const MCInst &Inst) const override {
116 return true;
117
118 switch (Inst.getOpcode()) {
119 default:
120 return false;
121 case LoongArch::JIRL:
122 return Inst.getOperand(0).getReg() == LoongArch::R0;
123 }
124 }
125
126 bool isCall(const MCInst &Inst) const override {
127 if (MCInstrAnalysis::isCall(Inst))
128 return true;
129
130 switch (Inst.getOpcode()) {
131 default:
132 return false;
133 case LoongArch::JIRL:
134 return Inst.getOperand(0).getReg() != LoongArch::R0;
135 }
136 }
137
138 bool isReturn(const MCInst &Inst) const override {
140 return true;
141
142 switch (Inst.getOpcode()) {
143 default:
144 return false;
145 case LoongArch::JIRL:
146 return Inst.getOperand(0).getReg() == LoongArch::R0 &&
147 Inst.getOperand(1).getReg() == LoongArch::R1;
148 }
149 }
150
151 bool isBranch(const MCInst &Inst) const override {
153 return true;
154
155 switch (Inst.getOpcode()) {
156 default:
157 return false;
158 case LoongArch::JIRL:
159 return Inst.getOperand(0).getReg() == LoongArch::R0 &&
160 Inst.getOperand(1).getReg() != LoongArch::R1;
161 }
162 }
163
164 bool isUnconditionalBranch(const MCInst &Inst) const override {
166 return true;
167
168 switch (Inst.getOpcode()) {
169 default:
170 return false;
171 case LoongArch::JIRL:
172 return Inst.getOperand(0).getReg() == LoongArch::R0 &&
173 Inst.getOperand(1).getReg() != LoongArch::R1;
174 }
175 }
176
177 bool isIndirectBranch(const MCInst &Inst) const override {
179 return true;
180
181 switch (Inst.getOpcode()) {
182 default:
183 return false;
184 case LoongArch::JIRL:
185 return Inst.getOperand(0).getReg() == LoongArch::R0 &&
186 Inst.getOperand(1).getReg() != LoongArch::R1;
187 }
188 }
189};
190
191} // end namespace
192
194 return new LoongArchMCInstrAnalysis(Info);
195}
196
197namespace {
199 std::unique_ptr<MCAsmBackend> &&MAB,
200 std::unique_ptr<MCObjectWriter> &&MOW,
201 std::unique_ptr<MCCodeEmitter> &&MCE) {
202 return createLoongArchELFStreamer(Context, std::move(MAB), std::move(MOW),
203 std::move(MCE));
204}
205} // end namespace
206
222 }
223}
unsigned const MachineRegisterInfo * MRI
Analysis containing CSE Info
Definition: CSEInfo.cpp:27
#define LLVM_EXTERNAL_VISIBILITY
Definition: Compiler.h:128
uint64_t Addr
uint64_t Size
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
static LVOptions Options
Definition: LVOptions.cpp:25
static MCSubtargetInfo * createLoongArchMCSubtargetInfo(const Triple &TT, StringRef CPU, StringRef FS)
static MCTargetStreamer * createLoongArchObjectTargetStreamer(MCStreamer &S, const MCSubtargetInfo &STI)
static MCInstPrinter * createLoongArchMCInstPrinter(const Triple &T, unsigned SyntaxVariant, const MCAsmInfo &MAI, const MCInstrInfo &MII, const MCRegisterInfo &MRI)
LLVM_EXTERNAL_VISIBILITY void LLVMInitializeLoongArchTargetMC()
static MCRegisterInfo * createLoongArchMCRegisterInfo(const Triple &TT)
static MCTargetStreamer * createLoongArchAsmTargetStreamer(MCStreamer &S, formatted_raw_ostream &OS, MCInstPrinter *InstPrint)
static MCAsmInfo * createLoongArchMCAsmInfo(const MCRegisterInfo &MRI, const Triple &TT, const MCTargetOptions &Options)
static MCInstrAnalysis * createLoongArchInstrAnalysis(const MCInstrInfo *Info)
static MCInstrInfo * createLoongArchMCInstrInfo()
raw_pwrite_stream & OS
This class is intended to be used as a base class for asm properties and features specific to the tar...
Definition: MCAsmInfo.h:56
void addInitialFrameState(const MCCFIInstruction &Inst)
Definition: MCAsmInfo.cpp:75
static MCCFIInstruction cfiDefCfa(MCSymbol *L, unsigned Register, int64_t Offset, SMLoc Loc={})
.cfi_def_cfa defines a rule for computing CFA as: take address from Register and add Offset to it.
Definition: MCDwarf.h:575
Context object for machine code objects.
Definition: MCContext.h:83
This is an instance of a target assembly language printer that converts an MCInst to valid target ass...
Definition: MCInstPrinter.h:46
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:185
unsigned getNumOperands() const
Definition: MCInst.h:209
unsigned getOpcode() const
Definition: MCInst.h:199
const MCOperand & getOperand(unsigned i) const
Definition: MCInst.h:207
virtual bool isCall(const MCInst &Inst) const
virtual bool isBranch(const MCInst &Inst) const
virtual bool isUnconditionalBranch(const MCInst &Inst) const
virtual bool evaluateBranch(const MCInst &Inst, uint64_t Addr, uint64_t Size, uint64_t &Target) const
Given a branch instruction try to get the address the branch targets.
virtual bool isTerminator(const MCInst &Inst) const
virtual bool isReturn(const MCInst &Inst) const
virtual bool isIndirectBranch(const MCInst &Inst) const
Interface to description of machine instruction set.
Definition: MCInstrInfo.h:26
int64_t getImm() const
Definition: MCInst.h:81
MCRegister getReg() const
Returns the register number.
Definition: MCInst.h:70
MCRegisterInfo base class - We assume that the target defines a static array of MCRegisterDesc object...
Streaming machine code generation interface.
Definition: MCStreamer.h:213
Generic base class for all target subtargets.
const Triple & getTargetTriple() const
Target specific streamer interface.
Definition: MCStreamer.h:94
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
constexpr bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:147
Target - Wrapper for Target specific information.
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
bool isOSBinFormatELF() const
Tests whether the OS uses the ELF binary format.
Definition: Triple.h:730
formatted_raw_ostream - A raw_ostream that wraps another one and keeps track of line and column posit...
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
Target & getTheLoongArch64Target()
MCCodeEmitter * createLoongArchMCCodeEmitter(const MCInstrInfo &MCII, MCContext &Ctx)
MCAsmBackend * createLoongArchAsmBackend(const Target &T, const MCSubtargetInfo &STI, const MCRegisterInfo &MRI, const MCTargetOptions &Options)
Target & getTheLoongArch32Target()
MCELFStreamer * createLoongArchELFStreamer(MCContext &C, std::unique_ptr< MCAsmBackend > MAB, std::unique_ptr< MCObjectWriter > MOW, std::unique_ptr< MCCodeEmitter > MCE)
static void RegisterMCRegInfo(Target &T, Target::MCRegInfoCtorFnTy Fn)
RegisterMCRegInfo - Register a MCRegisterInfo implementation for the given target.
static void RegisterMCAsmBackend(Target &T, Target::MCAsmBackendCtorTy Fn)
RegisterMCAsmBackend - Register a MCAsmBackend implementation for the given target.
static void RegisterMCCodeEmitter(Target &T, Target::MCCodeEmitterCtorTy Fn)
RegisterMCCodeEmitter - Register a MCCodeEmitter implementation for the given target.
static void RegisterMCAsmInfo(Target &T, Target::MCAsmInfoCtorFnTy Fn)
RegisterMCAsmInfo - Register a MCAsmInfo implementation for the given target.
static void RegisterMCSubtargetInfo(Target &T, Target::MCSubtargetInfoCtorFnTy Fn)
RegisterMCSubtargetInfo - Register a MCSubtargetInfo implementation for the given target.
static void RegisterObjectTargetStreamer(Target &T, Target::ObjectTargetStreamerCtorTy Fn)
static void RegisterMCInstrAnalysis(Target &T, Target::MCInstrAnalysisCtorFnTy Fn)
RegisterMCInstrAnalysis - Register a MCInstrAnalysis implementation for the given target.
static void RegisterELFStreamer(Target &T, Target::ELFStreamerCtorTy Fn)
static void RegisterMCInstPrinter(Target &T, Target::MCInstPrinterCtorTy Fn)
RegisterMCInstPrinter - Register a MCInstPrinter implementation for the given target.
static void RegisterMCInstrInfo(Target &T, Target::MCInstrInfoCtorFnTy Fn)
RegisterMCInstrInfo - Register a MCInstrInfo implementation for the given target.
static void RegisterAsmTargetStreamer(Target &T, Target::AsmTargetStreamerCtorTy Fn)