1 //===-- MipsMCTargetDesc.cpp - Mips Target Descriptions -------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file provides Mips specific target descriptions.
11 //===----------------------------------------------------------------------===//
13 #include "MipsMCTargetDesc.h"
14 #include "InstPrinter/MipsInstPrinter.h"
15 #include "MipsAsmBackend.h"
16 #include "MipsELFStreamer.h"
17 #include "MipsMCAsmInfo.h"
18 #include "MipsMCNaCl.h"
19 #include "MipsTargetStreamer.h"
20 #include "llvm/ADT/Triple.h"
21 #include "llvm/MC/MCCodeEmitter.h"
22 #include "llvm/MC/MCELFStreamer.h"
23 #include "llvm/MC/MCInstrAnalysis.h"
24 #include "llvm/MC/MCInstrInfo.h"
25 #include "llvm/MC/MCObjectWriter.h"
26 #include "llvm/MC/MCRegisterInfo.h"
27 #include "llvm/MC/MCSubtargetInfo.h"
28 #include "llvm/MC/MCSymbol.h"
29 #include "llvm/MC/MachineLocation.h"
30 #include "llvm/Support/ErrorHandling.h"
31 #include "llvm/Support/FormattedStream.h"
32 #include "llvm/Support/TargetRegistry.h"
36 #define GET_INSTRINFO_MC_DESC
37 #include "MipsGenInstrInfo.inc"
39 #define GET_SUBTARGETINFO_MC_DESC
40 #include "MipsGenSubtargetInfo.inc"
42 #define GET_REGINFO_MC_DESC
43 #include "MipsGenRegisterInfo.inc"
45 /// Select the Mips CPU for the given triple and cpu name.
46 /// FIXME: Merge with the copy in MipsSubtarget.cpp
47 StringRef
MIPS_MC::selectMipsCPU(const Triple
&TT
, StringRef CPU
) {
48 if (CPU
.empty() || CPU
== "generic") {
49 if (TT
.getSubArch() == llvm::Triple::MipsSubArch_r6
) {
64 static MCInstrInfo
*createMipsMCInstrInfo() {
65 MCInstrInfo
*X
= new MCInstrInfo();
66 InitMipsMCInstrInfo(X
);
70 static MCRegisterInfo
*createMipsMCRegisterInfo(const Triple
&TT
) {
71 MCRegisterInfo
*X
= new MCRegisterInfo();
72 InitMipsMCRegisterInfo(X
, Mips::RA
);
76 static MCSubtargetInfo
*createMipsMCSubtargetInfo(const Triple
&TT
,
77 StringRef CPU
, StringRef FS
) {
78 CPU
= MIPS_MC::selectMipsCPU(TT
, CPU
);
79 return createMipsMCSubtargetInfoImpl(TT
, CPU
, FS
);
82 static MCAsmInfo
*createMipsMCAsmInfo(const MCRegisterInfo
&MRI
,
84 MCAsmInfo
*MAI
= new MipsMCAsmInfo(TT
);
86 unsigned SP
= MRI
.getDwarfRegNum(Mips::SP
, true);
87 MCCFIInstruction Inst
= MCCFIInstruction::createDefCfa(nullptr, SP
, 0);
88 MAI
->addInitialFrameState(Inst
);
93 static MCInstPrinter
*createMipsMCInstPrinter(const Triple
&T
,
94 unsigned SyntaxVariant
,
96 const MCInstrInfo
&MII
,
97 const MCRegisterInfo
&MRI
) {
98 return new MipsInstPrinter(MAI
, MII
, MRI
);
101 static MCStreamer
*createMCStreamer(const Triple
&T
, MCContext
&Context
,
102 std::unique_ptr
<MCAsmBackend
> &&MAB
,
103 std::unique_ptr
<MCObjectWriter
> &&OW
,
104 std::unique_ptr
<MCCodeEmitter
> &&Emitter
,
108 S
= createMipsELFStreamer(Context
, std::move(MAB
), std::move(OW
),
109 std::move(Emitter
), RelaxAll
);
111 S
= createMipsNaClELFStreamer(Context
, std::move(MAB
), std::move(OW
),
112 std::move(Emitter
), RelaxAll
);
116 static MCTargetStreamer
*createMipsAsmTargetStreamer(MCStreamer
&S
,
117 formatted_raw_ostream
&OS
,
118 MCInstPrinter
*InstPrint
,
120 return new MipsTargetAsmStreamer(S
, OS
);
123 static MCTargetStreamer
*createMipsNullTargetStreamer(MCStreamer
&S
) {
124 return new MipsTargetStreamer(S
);
127 static MCTargetStreamer
*
128 createMipsObjectTargetStreamer(MCStreamer
&S
, const MCSubtargetInfo
&STI
) {
129 return new MipsTargetELFStreamer(S
, STI
);
134 class MipsMCInstrAnalysis
: public MCInstrAnalysis
{
136 MipsMCInstrAnalysis(const MCInstrInfo
*Info
) : MCInstrAnalysis(Info
) {}
138 bool evaluateBranch(const MCInst
&Inst
, uint64_t Addr
, uint64_t Size
,
139 uint64_t &Target
) const override
{
140 unsigned NumOps
= Inst
.getNumOperands();
143 switch (Info
->get(Inst
.getOpcode()).OpInfo
[NumOps
- 1].OperandType
) {
144 case MCOI::OPERAND_UNKNOWN
:
145 case MCOI::OPERAND_IMMEDIATE
:
147 Target
= Inst
.getOperand(NumOps
- 1).getImm();
149 case MCOI::OPERAND_PCREL
:
151 Target
= Addr
+ Inst
.getOperand(NumOps
- 1).getImm();
160 static MCInstrAnalysis
*createMipsMCInstrAnalysis(const MCInstrInfo
*Info
) {
161 return new MipsMCInstrAnalysis(Info
);
164 extern "C" void LLVMInitializeMipsTargetMC() {
165 for (Target
*T
: {&getTheMipsTarget(), &getTheMipselTarget(),
166 &getTheMips64Target(), &getTheMips64elTarget()}) {
167 // Register the MC asm info.
168 RegisterMCAsmInfoFn
X(*T
, createMipsMCAsmInfo
);
170 // Register the MC instruction info.
171 TargetRegistry::RegisterMCInstrInfo(*T
, createMipsMCInstrInfo
);
173 // Register the MC register info.
174 TargetRegistry::RegisterMCRegInfo(*T
, createMipsMCRegisterInfo
);
176 // Register the elf streamer.
177 TargetRegistry::RegisterELFStreamer(*T
, createMCStreamer
);
179 // Register the asm target streamer.
180 TargetRegistry::RegisterAsmTargetStreamer(*T
, createMipsAsmTargetStreamer
);
182 TargetRegistry::RegisterNullTargetStreamer(*T
,
183 createMipsNullTargetStreamer
);
185 // Register the MC subtarget info.
186 TargetRegistry::RegisterMCSubtargetInfo(*T
, createMipsMCSubtargetInfo
);
188 // Register the MC instruction analyzer.
189 TargetRegistry::RegisterMCInstrAnalysis(*T
, createMipsMCInstrAnalysis
);
191 // Register the MCInstPrinter.
192 TargetRegistry::RegisterMCInstPrinter(*T
, createMipsMCInstPrinter
);
194 TargetRegistry::RegisterObjectTargetStreamer(
195 *T
, createMipsObjectTargetStreamer
);
197 // Register the asm backend.
198 TargetRegistry::RegisterMCAsmBackend(*T
, createMipsAsmBackend
);
201 // Register the MC Code Emitter
202 for (Target
*T
: {&getTheMipsTarget(), &getTheMips64Target()})
203 TargetRegistry::RegisterMCCodeEmitter(*T
, createMipsMCCodeEmitterEB
);
205 for (Target
*T
: {&getTheMipselTarget(), &getTheMips64elTarget()})
206 TargetRegistry::RegisterMCCodeEmitter(*T
, createMipsMCCodeEmitterEL
);