LLVM 20.0.0git
MipsMCTargetDesc.cpp
Go to the documentation of this file.
1//===-- MipsMCTargetDesc.cpp - Mips 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 Mips specific target descriptions.
10//
11//===----------------------------------------------------------------------===//
12
13#include "MipsMCTargetDesc.h"
14#include "MipsAsmBackend.h"
15#include "MipsBaseInfo.h"
16#include "MipsELFStreamer.h"
17#include "MipsInstPrinter.h"
18#include "MipsMCAsmInfo.h"
19#include "MipsMCNaCl.h"
20#include "MipsTargetStreamer.h"
26#include "llvm/MC/MCInstrInfo.h"
30#include "llvm/MC/MCSymbol.h"
35
36using namespace llvm;
37
38#define GET_INSTRINFO_MC_DESC
39#define ENABLE_INSTR_PREDICATE_VERIFIER
40#include "MipsGenInstrInfo.inc"
41
42#define GET_SUBTARGETINFO_MC_DESC
43#include "MipsGenSubtargetInfo.inc"
44
45#define GET_REGINFO_MC_DESC
46#include "MipsGenRegisterInfo.inc"
47
49 // Mapping from CodeView to MC register id.
50 static const struct {
52 MCPhysReg Reg;
53 } RegMap[] = {
54 {codeview::RegisterId::MIPS_ZERO, Mips::ZERO},
55 {codeview::RegisterId::MIPS_AT, Mips::AT},
56 {codeview::RegisterId::MIPS_V0, Mips::V0},
57 {codeview::RegisterId::MIPS_V1, Mips::V1},
58 {codeview::RegisterId::MIPS_A0, Mips::A0},
59 {codeview::RegisterId::MIPS_A1, Mips::A1},
60 {codeview::RegisterId::MIPS_A2, Mips::A2},
61 {codeview::RegisterId::MIPS_A3, Mips::A3},
62 {codeview::RegisterId::MIPS_T0, Mips::T0},
63 {codeview::RegisterId::MIPS_T1, Mips::T1},
64 {codeview::RegisterId::MIPS_T2, Mips::T2},
65 {codeview::RegisterId::MIPS_T3, Mips::T3},
66 {codeview::RegisterId::MIPS_T4, Mips::T4},
67 {codeview::RegisterId::MIPS_T5, Mips::T5},
68 {codeview::RegisterId::MIPS_T6, Mips::T6},
69 {codeview::RegisterId::MIPS_T7, Mips::T7},
70 {codeview::RegisterId::MIPS_S0, Mips::S0},
71 {codeview::RegisterId::MIPS_S1, Mips::S1},
72 {codeview::RegisterId::MIPS_S2, Mips::S2},
73 {codeview::RegisterId::MIPS_S3, Mips::S3},
74 {codeview::RegisterId::MIPS_S4, Mips::S4},
75 {codeview::RegisterId::MIPS_S5, Mips::S5},
76 {codeview::RegisterId::MIPS_S6, Mips::S6},
77 {codeview::RegisterId::MIPS_S7, Mips::S7},
78 {codeview::RegisterId::MIPS_T8, Mips::T8},
79 {codeview::RegisterId::MIPS_T9, Mips::T9},
80 {codeview::RegisterId::MIPS_K0, Mips::K0},
81 {codeview::RegisterId::MIPS_K1, Mips::K1},
82 {codeview::RegisterId::MIPS_GP, Mips::GP},
83 {codeview::RegisterId::MIPS_SP, Mips::SP},
84 {codeview::RegisterId::MIPS_S8, Mips::FP},
85 {codeview::RegisterId::MIPS_RA, Mips::RA},
86 {codeview::RegisterId::MIPS_LO, Mips::HI0},
87 {codeview::RegisterId::MIPS_HI, Mips::LO0},
88 {codeview::RegisterId::MIPS_Fir, Mips::FCR0},
89 {codeview::RegisterId::MIPS_Psr, Mips::COP012}, // CP0.Status
90 {codeview::RegisterId::MIPS_F0, Mips::F0},
91 {codeview::RegisterId::MIPS_F1, Mips::F1},
92 {codeview::RegisterId::MIPS_F2, Mips::F2},
93 {codeview::RegisterId::MIPS_F3, Mips::F3},
94 {codeview::RegisterId::MIPS_F4, Mips::F4},
95 {codeview::RegisterId::MIPS_F5, Mips::F5},
96 {codeview::RegisterId::MIPS_F6, Mips::F6},
97 {codeview::RegisterId::MIPS_F7, Mips::F7},
98 {codeview::RegisterId::MIPS_F8, Mips::F8},
99 {codeview::RegisterId::MIPS_F9, Mips::F9},
100 {codeview::RegisterId::MIPS_F10, Mips::F10},
101 {codeview::RegisterId::MIPS_F11, Mips::F11},
102 {codeview::RegisterId::MIPS_F12, Mips::F12},
103 {codeview::RegisterId::MIPS_F13, Mips::F13},
104 {codeview::RegisterId::MIPS_F14, Mips::F14},
105 {codeview::RegisterId::MIPS_F15, Mips::F15},
106 {codeview::RegisterId::MIPS_F16, Mips::F16},
107 {codeview::RegisterId::MIPS_F17, Mips::F17},
108 {codeview::RegisterId::MIPS_F18, Mips::F18},
109 {codeview::RegisterId::MIPS_F19, Mips::F19},
110 {codeview::RegisterId::MIPS_F20, Mips::F20},
111 {codeview::RegisterId::MIPS_F21, Mips::F21},
112 {codeview::RegisterId::MIPS_F22, Mips::F22},
113 {codeview::RegisterId::MIPS_F23, Mips::F23},
114 {codeview::RegisterId::MIPS_F24, Mips::F24},
115 {codeview::RegisterId::MIPS_F25, Mips::F25},
116 {codeview::RegisterId::MIPS_F26, Mips::F26},
117 {codeview::RegisterId::MIPS_F27, Mips::F27},
118 {codeview::RegisterId::MIPS_F28, Mips::F28},
119 {codeview::RegisterId::MIPS_F29, Mips::F29},
120 {codeview::RegisterId::MIPS_F30, Mips::F30},
121 {codeview::RegisterId::MIPS_F31, Mips::F31},
122 {codeview::RegisterId::MIPS_Fsr, Mips::FCR31},
123 };
124 for (const auto &I : RegMap)
125 MRI->mapLLVMRegToCVReg(I.Reg, static_cast<int>(I.CVReg));
126}
127
128namespace {
129class MipsWinCOFFTargetStreamer : public MipsTargetStreamer {
130public:
131 MipsWinCOFFTargetStreamer(MCStreamer &S) : MipsTargetStreamer(S) {}
132};
133} // end namespace
134
135/// Select the Mips CPU for the given triple and cpu name.
137 if (CPU.empty() || CPU == "generic") {
138 if (TT.getSubArch() == llvm::Triple::MipsSubArch_r6) {
139 if (TT.isMIPS32())
140 CPU = "mips32r6";
141 else
142 CPU = "mips64r6";
143 } else {
144 if (TT.isMIPS32())
145 CPU = "mips32";
146 else
147 CPU = "mips64";
148 }
149 }
150 return CPU;
151}
152
154 MCInstrInfo *X = new MCInstrInfo();
155 InitMipsMCInstrInfo(X);
156 return X;
157}
158
161 InitMipsMCRegisterInfo(X, Mips::RA);
162 return X;
163}
164
166 StringRef CPU, StringRef FS) {
167 CPU = MIPS_MC::selectMipsCPU(TT, CPU);
168 return createMipsMCSubtargetInfoImpl(TT, CPU, /*TuneCPU*/ CPU, FS);
169}
170
172 const Triple &TT,
173 const MCTargetOptions &Options) {
174 MCAsmInfo *MAI;
175
176 if (TT.isOSWindows())
177 MAI = new MipsCOFFMCAsmInfo();
178 else
179 MAI = new MipsELFMCAsmInfo(TT, Options);
180
181 unsigned SP = MRI.getDwarfRegNum(Mips::SP, true);
183 MAI->addInitialFrameState(Inst);
184
185 return MAI;
186}
187
189 unsigned SyntaxVariant,
190 const MCAsmInfo &MAI,
191 const MCInstrInfo &MII,
192 const MCRegisterInfo &MRI) {
193 return new MipsInstPrinter(MAI, MII, MRI);
194}
195
197 std::unique_ptr<MCAsmBackend> &&MAB,
198 std::unique_ptr<MCObjectWriter> &&OW,
199 std::unique_ptr<MCCodeEmitter> &&Emitter) {
200 MCStreamer *S;
201 if (!T.isOSNaCl())
202 S = createMipsELFStreamer(Context, std::move(MAB), std::move(OW),
203 std::move(Emitter));
204 else
205 S = createMipsNaClELFStreamer(Context, std::move(MAB), std::move(OW),
206 std::move(Emitter));
207 return S;
208}
209
212 MCInstPrinter *InstPrint) {
213 return new MipsTargetAsmStreamer(S, OS);
214}
215
217 return new MipsTargetStreamer(S);
218}
219
220static MCTargetStreamer *
223 return new MipsWinCOFFTargetStreamer(S);
224 return new MipsTargetELFStreamer(S, STI);
225}
226
227namespace {
228
229class MipsMCInstrAnalysis : public MCInstrAnalysis {
230public:
231 MipsMCInstrAnalysis(const MCInstrInfo *Info) : MCInstrAnalysis(Info) {}
232
233 bool evaluateBranch(const MCInst &Inst, uint64_t Addr, uint64_t Size,
234 uint64_t &Target) const override {
235 unsigned NumOps = Inst.getNumOperands();
236 if (NumOps == 0)
237 return false;
238 switch (Info->get(Inst.getOpcode()).operands()[NumOps - 1].OperandType) {
241 // j, jal, jalx, jals
242 // Absolute branch within the current 256 MB-aligned region
243 uint64_t Region = Addr & ~uint64_t(0xfffffff);
244 Target = Region + Inst.getOperand(NumOps - 1).getImm();
245 return true;
246 }
248 // b, beq ...
249 Target = Addr + Inst.getOperand(NumOps - 1).getImm();
250 return true;
251 default:
252 return false;
253 }
254 }
255};
256}
257
259 return new MipsMCInstrAnalysis(Info);
260}
261
265 // Register the MC asm info.
267
268 // Register the MC instruction info.
270
271 // Register the MC register info.
273
274 // Register the elf streamer.
276
277 // Register the asm target streamer.
279
282
284
285 // Register the MC subtarget info.
287
288 // Register the MC instruction analyzer.
290
291 // Register the MCInstPrinter.
293
296
297 // Register the asm backend.
299 }
300
301 // Register the MC Code Emitter
304
307}
unsigned const MachineRegisterInfo * MRI
Analysis containing CSE Info
Definition: CSEInfo.cpp:27
#define LLVM_EXTERNAL_VISIBILITY
Definition: Compiler.h:128
dxil DXContainer Global Emitter
uint64_t Addr
uint64_t Size
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
static LVOptions Options
Definition: LVOptions.cpp:25
#define I(x, y, z)
Definition: MD5.cpp:58
static MCRegisterInfo * createMipsMCRegisterInfo(const Triple &TT)
static MCTargetStreamer * createMipsObjectTargetStreamer(MCStreamer &S, const MCSubtargetInfo &STI)
LLVM_EXTERNAL_VISIBILITY void LLVMInitializeMipsTargetMC()
static MCTargetStreamer * createMipsAsmTargetStreamer(MCStreamer &S, formatted_raw_ostream &OS, MCInstPrinter *InstPrint)
static MCStreamer * createMCStreamer(const Triple &T, MCContext &Context, std::unique_ptr< MCAsmBackend > &&MAB, std::unique_ptr< MCObjectWriter > &&OW, std::unique_ptr< MCCodeEmitter > &&Emitter)
static MCSubtargetInfo * createMipsMCSubtargetInfo(const Triple &TT, StringRef CPU, StringRef FS)
static MCInstrInfo * createMipsMCInstrInfo()
static MCInstPrinter * createMipsMCInstPrinter(const Triple &T, unsigned SyntaxVariant, const MCAsmInfo &MAI, const MCInstrInfo &MII, const MCRegisterInfo &MRI)
static MCTargetStreamer * createMipsNullTargetStreamer(MCStreamer &S)
static MCAsmInfo * createMipsMCAsmInfo(const MCRegisterInfo &MRI, const Triple &TT, const MCTargetOptions &Options)
static MCInstrAnalysis * createMipsMCInstrAnalysis(const MCInstrInfo *Info)
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 createDefCfaRegister(MCSymbol *L, unsigned Register, SMLoc Loc={})
.cfi_def_cfa_register modifies a rule for computing CFA.
Definition: MCDwarf.h:582
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 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.
Interface to description of machine instruction set.
Definition: MCInstrInfo.h:26
int64_t getImm() const
Definition: MCInst.h:81
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 isOSBinFormatCOFF() const
Tests whether the OS uses the COFF binary format.
Definition: Triple.h:752
@ MipsSubArch_r6
Definition: Triple.h:155
formatted_raw_ostream - A raw_ostream that wraps another one and keeps track of line and column posit...
@ OPERAND_IMMEDIATE
Definition: MCInstrDesc.h:60
@ OPERAND_UNKNOWN
Definition: MCInstrDesc.h:59
StringRef selectMipsCPU(const Triple &TT, StringRef CPU)
Select the Mips CPU for the given triple and cpu name.
void initLLVMToCVRegMapping(MCRegisterInfo *MRI)
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
MCCodeEmitter * createMipsMCCodeEmitterEL(const MCInstrInfo &MCII, MCContext &Ctx)
MCCodeEmitter * createMipsMCCodeEmitterEB(const MCInstrInfo &MCII, MCContext &Ctx)
MCELFStreamer * createMipsELFStreamer(MCContext &Context, std::unique_ptr< MCAsmBackend > MAB, std::unique_ptr< MCObjectWriter > OW, std::unique_ptr< MCCodeEmitter > Emitter)
Target & getTheMips64Target()
MCStreamer * createMipsWinCOFFStreamer(MCContext &C, std::unique_ptr< MCAsmBackend > &&AB, std::unique_ptr< MCObjectWriter > &&OW, std::unique_ptr< MCCodeEmitter > &&CE)
Construct an MIPS Windows COFF machine code streamer which will generate PE/COFF format object files.
Target & getTheMips64elTarget()
Target & getTheMipselTarget()
MCELFStreamer * createMipsNaClELFStreamer(MCContext &Context, std::unique_ptr< MCAsmBackend > TAB, std::unique_ptr< MCObjectWriter > OW, std::unique_ptr< MCCodeEmitter > Emitter)
Target & getTheMipsTarget()
MCAsmBackend * createMipsAsmBackend(const Target &T, const MCSubtargetInfo &STI, const MCRegisterInfo &MRI, const MCTargetOptions &Options)
RegisterMCAsmInfoFn - Helper template for registering a target assembly info implementation.
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 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 RegisterNullTargetStreamer(Target &T, Target::NullTargetStreamerCtorTy Fn)
static void RegisterMCInstPrinter(Target &T, Target::MCInstPrinterCtorTy Fn)
RegisterMCInstPrinter - Register a MCInstPrinter implementation for the given target.
static void RegisterCOFFStreamer(Target &T, Target::COFFStreamerCtorTy Fn)
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)