1 //===-- LanaiMCTargetDesc.cpp - Lanai Target Descriptions -----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file provides Lanai specific target descriptions.
12 //===----------------------------------------------------------------------===//
14 #include "LanaiMCAsmInfo.h"
15 #include "LanaiMCTargetDesc.h"
16 #include "InstPrinter/LanaiInstPrinter.h"
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/ADT/Triple.h"
19 #include "llvm/MC/MCInst.h"
20 #include "llvm/MC/MCInstrAnalysis.h"
21 #include "llvm/MC/MCInstrInfo.h"
22 #include "llvm/MC/MCRegisterInfo.h"
23 #include "llvm/MC/MCStreamer.h"
24 #include "llvm/MC/MCSubtargetInfo.h"
25 #include "llvm/Support/ErrorHandling.h"
26 #include "llvm/Support/TargetRegistry.h"
30 #define GET_INSTRINFO_MC_DESC
31 #include "LanaiGenInstrInfo.inc"
33 #define GET_SUBTARGETINFO_MC_DESC
34 #include "LanaiGenSubtargetInfo.inc"
36 #define GET_REGINFO_MC_DESC
37 #include "LanaiGenRegisterInfo.inc"
41 static MCInstrInfo *createLanaiMCInstrInfo() {
42 MCInstrInfo *X = new MCInstrInfo();
43 InitLanaiMCInstrInfo(X);
47 static MCRegisterInfo *createLanaiMCRegisterInfo(const Triple & /*TT*/) {
48 MCRegisterInfo *X = new MCRegisterInfo();
49 InitLanaiMCRegisterInfo(X, Lanai::RCA, 0, 0, Lanai::PC);
53 static MCSubtargetInfo *
54 createLanaiMCSubtargetInfo(const Triple &TT, StringRef CPU, StringRef FS) {
55 std::string CPUName = CPU;
59 return createLanaiMCSubtargetInfoImpl(TT, CPUName, FS);
62 static MCStreamer *createMCStreamer(const Triple &T, MCContext &Context,
63 MCAsmBackend &MAB, raw_pwrite_stream &OS,
64 MCCodeEmitter *Emitter, bool RelaxAll) {
65 if (!T.isOSBinFormatELF())
66 llvm_unreachable("OS not supported");
68 return createELFStreamer(Context, MAB, OS, Emitter, RelaxAll);
71 static MCInstPrinter *createLanaiMCInstPrinter(const Triple & /*T*/,
72 unsigned SyntaxVariant,
74 const MCInstrInfo &MII,
75 const MCRegisterInfo &MRI) {
76 if (SyntaxVariant == 0)
77 return new LanaiInstPrinter(MAI, MII, MRI);
81 static MCRelocationInfo *createLanaiElfRelocation(const Triple &TheTriple,
83 return createMCRelocationInfo(TheTriple, Ctx);
88 class LanaiMCInstrAnalysis : public MCInstrAnalysis {
90 explicit LanaiMCInstrAnalysis(const MCInstrInfo *Info)
91 : MCInstrAnalysis(Info) {}
93 bool evaluateBranch(const MCInst &Inst, uint64_t Addr, uint64_t Size,
94 uint64_t &Target) const override {
95 if (Inst.getNumOperands() == 0)
98 if (Info->get(Inst.getOpcode()).OpInfo[0].OperandType ==
99 MCOI::OPERAND_PCREL) {
100 int64_t Imm = Inst.getOperand(0).getImm();
101 Target = Addr + Size + Imm;
104 int64_t Imm = Inst.getOperand(0).getImm();
106 // Skip case where immediate is 0 as that occurs in file that isn't linked
107 // and the branch target inferred would be wrong.
117 } // end anonymous namespace
119 static MCInstrAnalysis *createLanaiInstrAnalysis(const MCInstrInfo *Info) {
120 return new LanaiMCInstrAnalysis(Info);
123 extern "C" void LLVMInitializeLanaiTargetMC() {
124 // Register the MC asm info.
125 RegisterMCAsmInfo<LanaiMCAsmInfo> X(getTheLanaiTarget());
127 // Register the MC instruction info.
128 TargetRegistry::RegisterMCInstrInfo(getTheLanaiTarget(),
129 createLanaiMCInstrInfo);
131 // Register the MC register info.
132 TargetRegistry::RegisterMCRegInfo(getTheLanaiTarget(),
133 createLanaiMCRegisterInfo);
135 // Register the MC subtarget info.
136 TargetRegistry::RegisterMCSubtargetInfo(getTheLanaiTarget(),
137 createLanaiMCSubtargetInfo);
139 // Register the MC code emitter
140 TargetRegistry::RegisterMCCodeEmitter(getTheLanaiTarget(),
141 createLanaiMCCodeEmitter);
143 // Register the ASM Backend
144 TargetRegistry::RegisterMCAsmBackend(getTheLanaiTarget(),
145 createLanaiAsmBackend);
147 // Register the MCInstPrinter.
148 TargetRegistry::RegisterMCInstPrinter(getTheLanaiTarget(),
149 createLanaiMCInstPrinter);
151 // Register the ELF streamer.
152 TargetRegistry::RegisterELFStreamer(getTheLanaiTarget(), createMCStreamer);
154 // Register the MC relocation info.
155 TargetRegistry::RegisterMCRelocationInfo(getTheLanaiTarget(),
156 createLanaiElfRelocation);
158 // Register the MC instruction analyzer.
159 TargetRegistry::RegisterMCInstrAnalysis(getTheLanaiTarget(),
160 createLanaiInstrAnalysis);