1 | //===-- LanaiMCTargetDesc.cpp - Lanai 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 Lanai specific target descriptions. |
10 | // |
11 | //===----------------------------------------------------------------------===// |
12 | |
13 | #include "LanaiMCTargetDesc.h" |
14 | #include "LanaiInstPrinter.h" |
15 | #include "LanaiMCAsmInfo.h" |
16 | #include "TargetInfo/LanaiTargetInfo.h" |
17 | #include "llvm/ADT/StringRef.h" |
18 | #include "llvm/MC/MCInst.h" |
19 | #include "llvm/MC/MCInstrAnalysis.h" |
20 | #include "llvm/MC/MCInstrInfo.h" |
21 | #include "llvm/MC/MCRegisterInfo.h" |
22 | #include "llvm/MC/MCStreamer.h" |
23 | #include "llvm/MC/MCSubtargetInfo.h" |
24 | #include "llvm/MC/TargetRegistry.h" |
25 | #include "llvm/Support/ErrorHandling.h" |
26 | #include "llvm/TargetParser/Triple.h" |
27 | #include <cstdint> |
28 | #include <string> |
29 | |
30 | #define GET_INSTRINFO_MC_DESC |
31 | #define ENABLE_INSTR_PREDICATE_VERIFIER |
32 | #include "LanaiGenInstrInfo.inc" |
33 | |
34 | #define GET_SUBTARGETINFO_MC_DESC |
35 | #include "LanaiGenSubtargetInfo.inc" |
36 | |
37 | #define GET_REGINFO_MC_DESC |
38 | #include "LanaiGenRegisterInfo.inc" |
39 | |
40 | using namespace llvm; |
41 | |
42 | static MCInstrInfo *createLanaiMCInstrInfo() { |
43 | MCInstrInfo *X = new MCInstrInfo(); |
44 | InitLanaiMCInstrInfo(II: X); |
45 | return X; |
46 | } |
47 | |
48 | static MCRegisterInfo *createLanaiMCRegisterInfo(const Triple & /*TT*/) { |
49 | MCRegisterInfo *X = new MCRegisterInfo(); |
50 | InitLanaiMCRegisterInfo(RI: X, RA: Lanai::RCA, DwarfFlavour: 0, EHFlavour: 0, PC: Lanai::PC); |
51 | return X; |
52 | } |
53 | |
54 | static MCSubtargetInfo * |
55 | createLanaiMCSubtargetInfo(const Triple &TT, StringRef CPU, StringRef FS) { |
56 | std::string CPUName = std::string(CPU); |
57 | if (CPUName.empty()) |
58 | CPUName = "generic" ; |
59 | |
60 | return createLanaiMCSubtargetInfoImpl(TT, CPU: CPUName, /*TuneCPU*/ CPUName, FS); |
61 | } |
62 | |
63 | static MCStreamer *createMCStreamer(const Triple &T, MCContext &Context, |
64 | std::unique_ptr<MCAsmBackend> &&MAB, |
65 | std::unique_ptr<MCObjectWriter> &&OW, |
66 | std::unique_ptr<MCCodeEmitter> &&Emitter) { |
67 | if (!T.isOSBinFormatELF()) |
68 | llvm_unreachable("OS not supported" ); |
69 | |
70 | return createELFStreamer(Ctx&: Context, TAB: std::move(MAB), OW: std::move(OW), |
71 | CE: std::move(Emitter)); |
72 | } |
73 | |
74 | static MCInstPrinter *createLanaiMCInstPrinter(const Triple & /*T*/, |
75 | unsigned SyntaxVariant, |
76 | const MCAsmInfo &MAI, |
77 | const MCInstrInfo &MII, |
78 | const MCRegisterInfo &MRI) { |
79 | if (SyntaxVariant == 0) |
80 | return new LanaiInstPrinter(MAI, MII, MRI); |
81 | return nullptr; |
82 | } |
83 | |
84 | static MCRelocationInfo *createLanaiElfRelocation(const Triple &TheTriple, |
85 | MCContext &Ctx) { |
86 | return createMCRelocationInfo(TT: TheTriple, Ctx); |
87 | } |
88 | |
89 | namespace { |
90 | |
91 | class LanaiMCInstrAnalysis : public MCInstrAnalysis { |
92 | public: |
93 | explicit LanaiMCInstrAnalysis(const MCInstrInfo *Info) |
94 | : MCInstrAnalysis(Info) {} |
95 | |
96 | bool evaluateBranch(const MCInst &Inst, uint64_t Addr, uint64_t Size, |
97 | uint64_t &Target) const override { |
98 | if (Inst.getNumOperands() == 0) |
99 | return false; |
100 | if (!isConditionalBranch(Inst) && !isUnconditionalBranch(Inst) && |
101 | !isCall(Inst)) |
102 | return false; |
103 | |
104 | if (Info->get(Opcode: Inst.getOpcode()).operands()[0].OperandType == |
105 | MCOI::OPERAND_PCREL) { |
106 | int64_t Imm = Inst.getOperand(i: 0).getImm(); |
107 | Target = Addr + Size + Imm; |
108 | return true; |
109 | } else { |
110 | int64_t Imm = Inst.getOperand(i: 0).getImm(); |
111 | |
112 | // Skip case where immediate is 0 as that occurs in file that isn't linked |
113 | // and the branch target inferred would be wrong. |
114 | if (Imm == 0) |
115 | return false; |
116 | |
117 | Target = Imm; |
118 | return true; |
119 | } |
120 | } |
121 | }; |
122 | |
123 | } // end anonymous namespace |
124 | |
125 | static MCInstrAnalysis *createLanaiInstrAnalysis(const MCInstrInfo *Info) { |
126 | return new LanaiMCInstrAnalysis(Info); |
127 | } |
128 | |
129 | extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeLanaiTargetMC() { |
130 | // Register the MC asm info. |
131 | RegisterMCAsmInfo<LanaiMCAsmInfo> X(getTheLanaiTarget()); |
132 | |
133 | // Register the MC instruction info. |
134 | TargetRegistry::RegisterMCInstrInfo(T&: getTheLanaiTarget(), |
135 | Fn: createLanaiMCInstrInfo); |
136 | |
137 | // Register the MC register info. |
138 | TargetRegistry::RegisterMCRegInfo(T&: getTheLanaiTarget(), |
139 | Fn: createLanaiMCRegisterInfo); |
140 | |
141 | // Register the MC subtarget info. |
142 | TargetRegistry::RegisterMCSubtargetInfo(T&: getTheLanaiTarget(), |
143 | Fn: createLanaiMCSubtargetInfo); |
144 | |
145 | // Register the MC code emitter |
146 | TargetRegistry::RegisterMCCodeEmitter(T&: getTheLanaiTarget(), |
147 | Fn: createLanaiMCCodeEmitter); |
148 | |
149 | // Register the ASM Backend |
150 | TargetRegistry::RegisterMCAsmBackend(T&: getTheLanaiTarget(), |
151 | Fn: createLanaiAsmBackend); |
152 | |
153 | // Register the MCInstPrinter. |
154 | TargetRegistry::RegisterMCInstPrinter(T&: getTheLanaiTarget(), |
155 | Fn: createLanaiMCInstPrinter); |
156 | |
157 | // Register the ELF streamer. |
158 | TargetRegistry::RegisterELFStreamer(T&: getTheLanaiTarget(), Fn: createMCStreamer); |
159 | |
160 | // Register the MC relocation info. |
161 | TargetRegistry::RegisterMCRelocationInfo(T&: getTheLanaiTarget(), |
162 | Fn: createLanaiElfRelocation); |
163 | |
164 | // Register the MC instruction analyzer. |
165 | TargetRegistry::RegisterMCInstrAnalysis(T&: getTheLanaiTarget(), |
166 | Fn: createLanaiInstrAnalysis); |
167 | } |
168 | |