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/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 std::unique_ptr<MCAsmBackend> &&MAB, 64 std::unique_ptr<MCObjectWriter> &&OW, 65 std::unique_ptr<MCCodeEmitter> &&Emitter, 66 bool RelaxAll) { 67 if (!T.isOSBinFormatELF()) 68 llvm_unreachable("OS not supported"); 69 70 return createELFStreamer(Context, std::move(MAB), std::move(OW), 71 std::move(Emitter), RelaxAll); 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(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 101 if (Info->get(Inst.getOpcode()).OpInfo[0].OperandType == 102 MCOI::OPERAND_PCREL) { 103 int64_t Imm = Inst.getOperand(0).getImm(); 104 Target = Addr + Size + Imm; 105 return true; 106 } else { 107 int64_t Imm = Inst.getOperand(0).getImm(); 108 109 // Skip case where immediate is 0 as that occurs in file that isn't linked 110 // and the branch target inferred would be wrong. 111 if (Imm == 0) 112 return false; 113 114 Target = Imm; 115 return true; 116 } 117 } 118 }; 119 120 } // end anonymous namespace 121 122 static MCInstrAnalysis *createLanaiInstrAnalysis(const MCInstrInfo *Info) { 123 return new LanaiMCInstrAnalysis(Info); 124 } 125 126 extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeLanaiTargetMC() { 127 // Register the MC asm info. 128 RegisterMCAsmInfo<LanaiMCAsmInfo> X(getTheLanaiTarget()); 129 130 // Register the MC instruction info. 131 TargetRegistry::RegisterMCInstrInfo(getTheLanaiTarget(), 132 createLanaiMCInstrInfo); 133 134 // Register the MC register info. 135 TargetRegistry::RegisterMCRegInfo(getTheLanaiTarget(), 136 createLanaiMCRegisterInfo); 137 138 // Register the MC subtarget info. 139 TargetRegistry::RegisterMCSubtargetInfo(getTheLanaiTarget(), 140 createLanaiMCSubtargetInfo); 141 142 // Register the MC code emitter 143 TargetRegistry::RegisterMCCodeEmitter(getTheLanaiTarget(), 144 createLanaiMCCodeEmitter); 145 146 // Register the ASM Backend 147 TargetRegistry::RegisterMCAsmBackend(getTheLanaiTarget(), 148 createLanaiAsmBackend); 149 150 // Register the MCInstPrinter. 151 TargetRegistry::RegisterMCInstPrinter(getTheLanaiTarget(), 152 createLanaiMCInstPrinter); 153 154 // Register the ELF streamer. 155 TargetRegistry::RegisterELFStreamer(getTheLanaiTarget(), createMCStreamer); 156 157 // Register the MC relocation info. 158 TargetRegistry::RegisterMCRelocationInfo(getTheLanaiTarget(), 159 createLanaiElfRelocation); 160 161 // Register the MC instruction analyzer. 162 TargetRegistry::RegisterMCInstrAnalysis(getTheLanaiTarget(), 163 createLanaiInstrAnalysis); 164 } 165