Jacques Pienaar | fcef3e4 | 2016-03-28 13:09:54 +0000 | [diff] [blame] | 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 "LanaiMCTargetDesc.h" |
| 15 | |
| 16 | #include "InstPrinter/LanaiInstPrinter.h" |
| 17 | #include "LanaiMCAsmInfo.h" |
| 18 | #include "llvm/MC/MCCodeGenInfo.h" |
| 19 | #include "llvm/MC/MCInstrAnalysis.h" |
| 20 | #include "llvm/MC/MCInstrInfo.h" |
| 21 | #include "llvm/MC/MCStreamer.h" |
| 22 | #include "llvm/MC/MCSubtargetInfo.h" |
| 23 | #include "llvm/Support/ErrorHandling.h" |
| 24 | #include "llvm/Support/TargetRegistry.h" |
| 25 | |
| 26 | #define GET_INSTRINFO_MC_DESC |
| 27 | #include "LanaiGenInstrInfo.inc" |
| 28 | |
| 29 | #define GET_SUBTARGETINFO_MC_DESC |
| 30 | #include "LanaiGenSubtargetInfo.inc" |
| 31 | |
| 32 | #define GET_REGINFO_MC_DESC |
| 33 | #include "LanaiGenRegisterInfo.inc" |
| 34 | |
| 35 | using namespace llvm; |
| 36 | |
| 37 | static MCInstrInfo *createLanaiMCInstrInfo() { |
| 38 | MCInstrInfo *X = new MCInstrInfo(); |
| 39 | InitLanaiMCInstrInfo(X); |
| 40 | return X; |
| 41 | } |
| 42 | |
| 43 | static MCRegisterInfo *createLanaiMCRegisterInfo(const Triple &TT) { |
| 44 | MCRegisterInfo *X = new MCRegisterInfo(); |
| 45 | InitLanaiMCRegisterInfo(X, Lanai::RCA, 0, 0, Lanai::PC); |
| 46 | return X; |
| 47 | } |
| 48 | |
| 49 | static MCSubtargetInfo * |
| 50 | createLanaiMCSubtargetInfo(const Triple &TT, StringRef CPU, StringRef FS) { |
| 51 | std::string CPUName = CPU; |
| 52 | if (CPUName.empty()) |
| 53 | CPUName = "generic"; |
| 54 | |
| 55 | return createLanaiMCSubtargetInfoImpl(TT, CPUName, FS); |
| 56 | } |
| 57 | |
| 58 | static MCCodeGenInfo *createLanaiMCCodeGenInfo(const Triple &TT, |
| 59 | Reloc::Model RM, |
| 60 | CodeModel::Model CM, |
| 61 | CodeGenOpt::Level OL) { |
| 62 | MCCodeGenInfo *X = new MCCodeGenInfo(); |
| 63 | X->initMCCodeGenInfo(RM, CM, OL); |
| 64 | return X; |
| 65 | } |
| 66 | |
| 67 | static MCStreamer *createMCStreamer(const Triple &T, MCContext &Context, |
| 68 | MCAsmBackend &MAB, raw_pwrite_stream &OS, |
| 69 | MCCodeEmitter *Emitter, bool RelaxAll) { |
| 70 | if (!T.isOSBinFormatELF()) |
| 71 | llvm_unreachable("OS not supported"); |
| 72 | |
| 73 | return createELFStreamer(Context, MAB, OS, Emitter, RelaxAll); |
| 74 | } |
| 75 | |
| 76 | static MCInstPrinter *createLanaiMCInstPrinter(const Triple &T, |
| 77 | unsigned SyntaxVariant, |
| 78 | const MCAsmInfo &MAI, |
| 79 | const MCInstrInfo &MII, |
| 80 | const MCRegisterInfo &MRI) { |
| 81 | if (SyntaxVariant == 0) |
| 82 | return new LanaiInstPrinter(MAI, MII, MRI); |
| 83 | return 0; |
| 84 | } |
| 85 | |
| 86 | MCRelocationInfo *createLanaiElfRelocation(const Triple &TheTriple, |
| 87 | MCContext &Ctx) { |
| 88 | return createMCRelocationInfo(TheTriple, Ctx); |
| 89 | } |
| 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 | static MCInstrAnalysis *createLanaiInstrAnalysis(const MCInstrInfo *Info) { |
| 121 | return new LanaiMCInstrAnalysis(Info); |
| 122 | } |
| 123 | |
| 124 | extern "C" void LLVMInitializeLanaiTargetMC() { |
| 125 | // Register the MC asm info. |
| 126 | RegisterMCAsmInfo<LanaiMCAsmInfo> X(TheLanaiTarget); |
| 127 | |
| 128 | // Register the MC codegen info. |
| 129 | TargetRegistry::RegisterMCCodeGenInfo(TheLanaiTarget, |
| 130 | createLanaiMCCodeGenInfo); |
| 131 | |
| 132 | // Register the MC instruction info. |
| 133 | TargetRegistry::RegisterMCInstrInfo(TheLanaiTarget, createLanaiMCInstrInfo); |
| 134 | |
| 135 | // Register the MC register info. |
| 136 | TargetRegistry::RegisterMCRegInfo(TheLanaiTarget, createLanaiMCRegisterInfo); |
| 137 | |
| 138 | // Register the MC subtarget info. |
| 139 | TargetRegistry::RegisterMCSubtargetInfo(TheLanaiTarget, |
| 140 | createLanaiMCSubtargetInfo); |
| 141 | |
| 142 | // Register the MC code emitter |
| 143 | TargetRegistry::RegisterMCCodeEmitter(TheLanaiTarget, |
| 144 | llvm::createLanaiMCCodeEmitter); |
| 145 | |
| 146 | // Register the ASM Backend |
| 147 | TargetRegistry::RegisterMCAsmBackend(TheLanaiTarget, createLanaiAsmBackend); |
| 148 | |
| 149 | // Register the MCInstPrinter. |
| 150 | TargetRegistry::RegisterMCInstPrinter(TheLanaiTarget, |
| 151 | createLanaiMCInstPrinter); |
| 152 | |
| 153 | // Register the ELF streamer. |
| 154 | TargetRegistry::RegisterELFStreamer(TheLanaiTarget, createMCStreamer); |
| 155 | |
| 156 | // Register the MC relocation info. |
| 157 | TargetRegistry::RegisterMCRelocationInfo(TheLanaiTarget, |
| 158 | createLanaiElfRelocation); |
| 159 | |
| 160 | // Register the MC instruction analyzer. |
| 161 | TargetRegistry::RegisterMCInstrAnalysis(TheLanaiTarget, |
| 162 | createLanaiInstrAnalysis); |
| 163 | } |