]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/Target/Lanai/MCTargetDesc/LanaiMCTargetDesc.cpp
Vendor import of llvm trunk r291274:
[FreeBSD/FreeBSD.git] / lib / Target / Lanai / MCTargetDesc / LanaiMCTargetDesc.cpp
1 //===-- LanaiMCTargetDesc.cpp - Lanai Target Descriptions -----------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file provides Lanai specific target descriptions.
11 //
12 //===----------------------------------------------------------------------===//
13
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"
27 #include <cstdint>
28 #include <string>
29
30 #define GET_INSTRINFO_MC_DESC
31 #include "LanaiGenInstrInfo.inc"
32
33 #define GET_SUBTARGETINFO_MC_DESC
34 #include "LanaiGenSubtargetInfo.inc"
35
36 #define GET_REGINFO_MC_DESC
37 #include "LanaiGenRegisterInfo.inc"
38
39 using namespace llvm;
40
41 static MCInstrInfo *createLanaiMCInstrInfo() {
42   MCInstrInfo *X = new MCInstrInfo();
43   InitLanaiMCInstrInfo(X);
44   return X;
45 }
46
47 static MCRegisterInfo *createLanaiMCRegisterInfo(const Triple & /*TT*/) {
48   MCRegisterInfo *X = new MCRegisterInfo();
49   InitLanaiMCRegisterInfo(X, Lanai::RCA, 0, 0, Lanai::PC);
50   return X;
51 }
52
53 static MCSubtargetInfo *
54 createLanaiMCSubtargetInfo(const Triple &TT, StringRef CPU, StringRef FS) {
55   std::string CPUName = CPU;
56   if (CPUName.empty())
57     CPUName = "generic";
58
59   return createLanaiMCSubtargetInfoImpl(TT, CPUName, FS);
60 }
61
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");
67
68   return createELFStreamer(Context, MAB, OS, Emitter, RelaxAll);
69 }
70
71 static MCInstPrinter *createLanaiMCInstPrinter(const Triple & /*T*/,
72                                                unsigned SyntaxVariant,
73                                                const MCAsmInfo &MAI,
74                                                const MCInstrInfo &MII,
75                                                const MCRegisterInfo &MRI) {
76   if (SyntaxVariant == 0)
77     return new LanaiInstPrinter(MAI, MII, MRI);
78   return nullptr;
79 }
80
81 static MCRelocationInfo *createLanaiElfRelocation(const Triple &TheTriple,
82                                                   MCContext &Ctx) {
83   return createMCRelocationInfo(TheTriple, Ctx);
84 }
85
86 namespace {
87
88 class LanaiMCInstrAnalysis : public MCInstrAnalysis {
89 public:
90   explicit LanaiMCInstrAnalysis(const MCInstrInfo *Info)
91       : MCInstrAnalysis(Info) {}
92
93   bool evaluateBranch(const MCInst &Inst, uint64_t Addr, uint64_t Size,
94                       uint64_t &Target) const override {
95     if (Inst.getNumOperands() == 0)
96       return false;
97
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;
102       return true;
103     } else {
104       int64_t Imm = Inst.getOperand(0).getImm();
105
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.
108       if (Imm == 0)
109         return false;
110
111       Target = Imm;
112       return true;
113     }
114   }
115 };
116
117 } // end anonymous namespace
118
119 static MCInstrAnalysis *createLanaiInstrAnalysis(const MCInstrInfo *Info) {
120   return new LanaiMCInstrAnalysis(Info);
121 }
122
123 extern "C" void LLVMInitializeLanaiTargetMC() {
124   // Register the MC asm info.
125   RegisterMCAsmInfo<LanaiMCAsmInfo> X(getTheLanaiTarget());
126
127   // Register the MC instruction info.
128   TargetRegistry::RegisterMCInstrInfo(getTheLanaiTarget(),
129                                       createLanaiMCInstrInfo);
130
131   // Register the MC register info.
132   TargetRegistry::RegisterMCRegInfo(getTheLanaiTarget(),
133                                     createLanaiMCRegisterInfo);
134
135   // Register the MC subtarget info.
136   TargetRegistry::RegisterMCSubtargetInfo(getTheLanaiTarget(),
137                                           createLanaiMCSubtargetInfo);
138
139   // Register the MC code emitter
140   TargetRegistry::RegisterMCCodeEmitter(getTheLanaiTarget(),
141                                         createLanaiMCCodeEmitter);
142
143   // Register the ASM Backend
144   TargetRegistry::RegisterMCAsmBackend(getTheLanaiTarget(),
145                                        createLanaiAsmBackend);
146
147   // Register the MCInstPrinter.
148   TargetRegistry::RegisterMCInstPrinter(getTheLanaiTarget(),
149                                         createLanaiMCInstPrinter);
150
151   // Register the ELF streamer.
152   TargetRegistry::RegisterELFStreamer(getTheLanaiTarget(), createMCStreamer);
153
154   // Register the MC relocation info.
155   TargetRegistry::RegisterMCRelocationInfo(getTheLanaiTarget(),
156                                            createLanaiElfRelocation);
157
158   // Register the MC instruction analyzer.
159   TargetRegistry::RegisterMCInstrAnalysis(getTheLanaiTarget(),
160                                           createLanaiInstrAnalysis);
161 }