xref: /llvm-project/llvm/lib/Target/CSKY/MCTargetDesc/CSKYMCTargetDesc.cpp (revision 8f14e39e59c9dc0ea93b098cf8d03fc23c23a7ec)
1ec17c4f0SZi Xuan Wu //===-- CSKYMCTargetDesc.cpp - CSKY Target Descriptions -------------------===//
2ec17c4f0SZi Xuan Wu //
3ec17c4f0SZi Xuan Wu // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4ec17c4f0SZi Xuan Wu // See https://llvm.org/LICENSE.txt for license information.
5ec17c4f0SZi Xuan Wu // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6ec17c4f0SZi Xuan Wu //
7ec17c4f0SZi Xuan Wu //===----------------------------------------------------------------------===//
8ec17c4f0SZi Xuan Wu ///
9ec17c4f0SZi Xuan Wu /// This file provides CSKY specific target descriptions.
10ec17c4f0SZi Xuan Wu ///
11ec17c4f0SZi Xuan Wu //===----------------------------------------------------------------------===//
12ec17c4f0SZi Xuan Wu 
13ec17c4f0SZi Xuan Wu #include "CSKYMCTargetDesc.h"
14ec17c4f0SZi Xuan Wu #include "CSKYAsmBackend.h"
1532977589SZi Xuan Wu #include "CSKYELFStreamer.h"
168ba622baSZi Xuan Wu #include "CSKYInstPrinter.h"
17ec17c4f0SZi Xuan Wu #include "CSKYMCAsmInfo.h"
18ec17c4f0SZi Xuan Wu #include "CSKYMCCodeEmitter.h"
1932977589SZi Xuan Wu #include "CSKYTargetStreamer.h"
20ec17c4f0SZi Xuan Wu #include "TargetInfo/CSKYTargetInfo.h"
21e8b30371SZi Xuan Wu #include "llvm/MC/MCAssembler.h"
22582836faSZi Xuan Wu #include "llvm/MC/MCInstrAnalysis.h"
23ec17c4f0SZi Xuan Wu #include "llvm/MC/MCInstrInfo.h"
24ec17c4f0SZi Xuan Wu #include "llvm/MC/MCRegisterInfo.h"
25ec17c4f0SZi Xuan Wu #include "llvm/MC/MCSubtargetInfo.h"
2689b57061SReid Kleckner #include "llvm/MC/TargetRegistry.h"
27ec17c4f0SZi Xuan Wu 
28ec17c4f0SZi Xuan Wu #define GET_INSTRINFO_MC_DESC
293e0bf1c7SDavid Green #define ENABLE_INSTR_PREDICATE_VERIFIER
30ec17c4f0SZi Xuan Wu #include "CSKYGenInstrInfo.inc"
31ec17c4f0SZi Xuan Wu 
32ec17c4f0SZi Xuan Wu #define GET_REGINFO_MC_DESC
33ec17c4f0SZi Xuan Wu #include "CSKYGenRegisterInfo.inc"
34ec17c4f0SZi Xuan Wu 
358ba622baSZi Xuan Wu #define GET_SUBTARGETINFO_MC_DESC
368ba622baSZi Xuan Wu #include "CSKYGenSubtargetInfo.inc"
378ba622baSZi Xuan Wu 
38ec17c4f0SZi Xuan Wu using namespace llvm;
39ec17c4f0SZi Xuan Wu 
40ec17c4f0SZi Xuan Wu static MCAsmInfo *createCSKYMCAsmInfo(const MCRegisterInfo &MRI,
41ec17c4f0SZi Xuan Wu                                       const Triple &TT,
42ec17c4f0SZi Xuan Wu                                       const MCTargetOptions &Options) {
43ec17c4f0SZi Xuan Wu   MCAsmInfo *MAI = new CSKYMCAsmInfo(TT);
44ec17c4f0SZi Xuan Wu 
45ec17c4f0SZi Xuan Wu   // Initial state of the frame pointer is SP.
46ec17c4f0SZi Xuan Wu   unsigned Reg = MRI.getDwarfRegNum(CSKY::R14, true);
47ec17c4f0SZi Xuan Wu   MCCFIInstruction Inst = MCCFIInstruction::cfiDefCfa(nullptr, Reg, 0);
48ec17c4f0SZi Xuan Wu   MAI->addInitialFrameState(Inst);
49ec17c4f0SZi Xuan Wu   return MAI;
50ec17c4f0SZi Xuan Wu }
51ec17c4f0SZi Xuan Wu 
52ec17c4f0SZi Xuan Wu static MCInstrInfo *createCSKYMCInstrInfo() {
53ec17c4f0SZi Xuan Wu   MCInstrInfo *Info = new MCInstrInfo();
54ec17c4f0SZi Xuan Wu   InitCSKYMCInstrInfo(Info);
55ec17c4f0SZi Xuan Wu   return Info;
56ec17c4f0SZi Xuan Wu }
57ec17c4f0SZi Xuan Wu 
588ba622baSZi Xuan Wu static MCInstPrinter *createCSKYMCInstPrinter(const Triple &T,
598ba622baSZi Xuan Wu                                               unsigned SyntaxVariant,
608ba622baSZi Xuan Wu                                               const MCAsmInfo &MAI,
618ba622baSZi Xuan Wu                                               const MCInstrInfo &MII,
628ba622baSZi Xuan Wu                                               const MCRegisterInfo &MRI) {
638ba622baSZi Xuan Wu   return new CSKYInstPrinter(MAI, MII, MRI);
648ba622baSZi Xuan Wu }
658ba622baSZi Xuan Wu 
66ec17c4f0SZi Xuan Wu static MCRegisterInfo *createCSKYMCRegisterInfo(const Triple &TT) {
67ec17c4f0SZi Xuan Wu   MCRegisterInfo *Info = new MCRegisterInfo();
68ec17c4f0SZi Xuan Wu   InitCSKYMCRegisterInfo(Info, CSKY::R15);
69ec17c4f0SZi Xuan Wu   return Info;
70ec17c4f0SZi Xuan Wu }
71ec17c4f0SZi Xuan Wu 
728ba622baSZi Xuan Wu static MCSubtargetInfo *createCSKYMCSubtargetInfo(const Triple &TT,
738ba622baSZi Xuan Wu                                                   StringRef CPU, StringRef FS) {
748ba622baSZi Xuan Wu   std::string CPUName = std::string(CPU);
758ba622baSZi Xuan Wu   if (CPUName.empty())
768ba622baSZi Xuan Wu     CPUName = "generic";
778ba622baSZi Xuan Wu   return createCSKYMCSubtargetInfoImpl(TT, CPUName, /*TuneCPU=*/CPUName, FS);
788ba622baSZi Xuan Wu }
798ba622baSZi Xuan Wu 
8032977589SZi Xuan Wu static MCTargetStreamer *
8132977589SZi Xuan Wu createCSKYObjectTargetStreamer(MCStreamer &S, const MCSubtargetInfo &STI) {
8232977589SZi Xuan Wu   const Triple &TT = STI.getTargetTriple();
8332977589SZi Xuan Wu   if (TT.isOSBinFormatELF())
8432977589SZi Xuan Wu     return new CSKYTargetELFStreamer(S, STI);
8532977589SZi Xuan Wu   return nullptr;
8632977589SZi Xuan Wu }
8732977589SZi Xuan Wu 
8832977589SZi Xuan Wu static MCStreamer *createELFStreamer(const Triple &T, MCContext &Ctx,
8932977589SZi Xuan Wu                                      std::unique_ptr<MCAsmBackend> &&MAB,
9032977589SZi Xuan Wu                                      std::unique_ptr<MCObjectWriter> &&OW,
914e340356SFangrui Song                                      std::unique_ptr<MCCodeEmitter> &&Emitter) {
9232977589SZi Xuan Wu   CSKYELFStreamer *S = new CSKYELFStreamer(Ctx, std::move(MAB), std::move(OW),
9332977589SZi Xuan Wu                                            std::move(Emitter));
9432977589SZi Xuan Wu 
9532977589SZi Xuan Wu   return S;
9632977589SZi Xuan Wu }
9732977589SZi Xuan Wu 
98*8f14e39eSFangrui Song static MCTargetStreamer *
99*8f14e39eSFangrui Song createCSKYAsmTargetStreamer(MCStreamer &S, formatted_raw_ostream &OS,
100*8f14e39eSFangrui Song                             MCInstPrinter *InstPrinter) {
10132977589SZi Xuan Wu   return new CSKYTargetAsmStreamer(S, OS);
10232977589SZi Xuan Wu }
10332977589SZi Xuan Wu 
10432977589SZi Xuan Wu static MCTargetStreamer *createCSKYNullTargetStreamer(MCStreamer &S) {
10532977589SZi Xuan Wu   return new CSKYTargetStreamer(S);
10632977589SZi Xuan Wu }
10732977589SZi Xuan Wu 
108582836faSZi Xuan Wu namespace {
109582836faSZi Xuan Wu 
110582836faSZi Xuan Wu class CSKYMCInstrAnalysis : public MCInstrAnalysis {
111582836faSZi Xuan Wu public:
112582836faSZi Xuan Wu   explicit CSKYMCInstrAnalysis(const MCInstrInfo *Info)
113582836faSZi Xuan Wu       : MCInstrAnalysis(Info) {}
114582836faSZi Xuan Wu 
115582836faSZi Xuan Wu   bool evaluateBranch(const MCInst &Inst, uint64_t Addr, uint64_t Size,
116582836faSZi Xuan Wu                       uint64_t &Target) const override {
117582836faSZi Xuan Wu     if (isConditionalBranch(Inst) || isUnconditionalBranch(Inst)) {
118582836faSZi Xuan Wu       int64_t Imm;
119582836faSZi Xuan Wu       Imm = Inst.getOperand(Inst.getNumOperands() - 1).getImm();
120582836faSZi Xuan Wu       Target = Addr + Imm;
121582836faSZi Xuan Wu       return true;
122582836faSZi Xuan Wu     }
123582836faSZi Xuan Wu 
124582836faSZi Xuan Wu     if (Inst.getOpcode() == CSKY::BSR32) {
125582836faSZi Xuan Wu       Target = Addr + Inst.getOperand(0).getImm();
126582836faSZi Xuan Wu       return true;
127582836faSZi Xuan Wu     }
128582836faSZi Xuan Wu 
129582836faSZi Xuan Wu     switch (Inst.getOpcode()) {
130582836faSZi Xuan Wu     default:
131582836faSZi Xuan Wu       return false;
132582836faSZi Xuan Wu     case CSKY::LRW16:
133582836faSZi Xuan Wu     case CSKY::LRW32:
134582836faSZi Xuan Wu     case CSKY::JSRI32:
135582836faSZi Xuan Wu     case CSKY::JMPI32:
136582836faSZi Xuan Wu       int64_t Imm = Inst.getOperand(Inst.getNumOperands() - 1).getImm();
137582836faSZi Xuan Wu       Target = ((Addr + Imm) & 0xFFFFFFFC);
138582836faSZi Xuan Wu       return true;
139582836faSZi Xuan Wu     }
140582836faSZi Xuan Wu 
141582836faSZi Xuan Wu     return false;
142582836faSZi Xuan Wu   }
143582836faSZi Xuan Wu };
144582836faSZi Xuan Wu 
145582836faSZi Xuan Wu } // end anonymous namespace
146582836faSZi Xuan Wu 
147582836faSZi Xuan Wu static MCInstrAnalysis *createCSKYInstrAnalysis(const MCInstrInfo *Info) {
148582836faSZi Xuan Wu   return new CSKYMCInstrAnalysis(Info);
149582836faSZi Xuan Wu }
150582836faSZi Xuan Wu 
151ec17c4f0SZi Xuan Wu extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeCSKYTargetMC() {
152ec17c4f0SZi Xuan Wu   auto &CSKYTarget = getTheCSKYTarget();
153ec17c4f0SZi Xuan Wu   TargetRegistry::RegisterMCAsmBackend(CSKYTarget, createCSKYAsmBackend);
154ec17c4f0SZi Xuan Wu   TargetRegistry::RegisterMCAsmInfo(CSKYTarget, createCSKYMCAsmInfo);
155ec17c4f0SZi Xuan Wu   TargetRegistry::RegisterMCInstrInfo(CSKYTarget, createCSKYMCInstrInfo);
156ec17c4f0SZi Xuan Wu   TargetRegistry::RegisterMCRegInfo(CSKYTarget, createCSKYMCRegisterInfo);
157ec17c4f0SZi Xuan Wu   TargetRegistry::RegisterMCCodeEmitter(CSKYTarget, createCSKYMCCodeEmitter);
1588ba622baSZi Xuan Wu   TargetRegistry::RegisterMCInstPrinter(CSKYTarget, createCSKYMCInstPrinter);
1598ba622baSZi Xuan Wu   TargetRegistry::RegisterMCSubtargetInfo(CSKYTarget,
1608ba622baSZi Xuan Wu                                           createCSKYMCSubtargetInfo);
16132977589SZi Xuan Wu   TargetRegistry::RegisterELFStreamer(CSKYTarget, createELFStreamer);
16232977589SZi Xuan Wu   TargetRegistry::RegisterObjectTargetStreamer(CSKYTarget,
16332977589SZi Xuan Wu                                                createCSKYObjectTargetStreamer);
16432977589SZi Xuan Wu   TargetRegistry::RegisterAsmTargetStreamer(CSKYTarget,
16532977589SZi Xuan Wu                                             createCSKYAsmTargetStreamer);
16632977589SZi Xuan Wu   // Register the null target streamer.
16732977589SZi Xuan Wu   TargetRegistry::RegisterNullTargetStreamer(CSKYTarget,
16832977589SZi Xuan Wu                                              createCSKYNullTargetStreamer);
169582836faSZi Xuan Wu   TargetRegistry::RegisterMCInstrAnalysis(CSKYTarget, createCSKYInstrAnalysis);
170ec17c4f0SZi Xuan Wu }
171