1 //===-- LoongArchMCTargetDesc.cpp - LoongArch 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 LoongArch specific target descriptions. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "LoongArchMCTargetDesc.h" 14 #include "LoongArchBaseInfo.h" 15 #include "LoongArchELFStreamer.h" 16 #include "LoongArchInstPrinter.h" 17 #include "LoongArchMCAsmInfo.h" 18 #include "TargetInfo/LoongArchTargetInfo.h" 19 #include "llvm/MC/MCAsmBackend.h" 20 #include "llvm/MC/MCAsmInfo.h" 21 #include "llvm/MC/MCCodeEmitter.h" 22 #include "llvm/MC/MCDwarf.h" 23 #include "llvm/MC/MCInstrAnalysis.h" 24 #include "llvm/MC/MCInstrInfo.h" 25 #include "llvm/MC/MCObjectWriter.h" 26 #include "llvm/MC/MCRegisterInfo.h" 27 #include "llvm/MC/MCSubtargetInfo.h" 28 #include "llvm/MC/TargetRegistry.h" 29 #include "llvm/Support/Compiler.h" 30 31 #define GET_INSTRINFO_MC_DESC 32 #define ENABLE_INSTR_PREDICATE_VERIFIER 33 #include "LoongArchGenInstrInfo.inc" 34 35 #define GET_REGINFO_MC_DESC 36 #include "LoongArchGenRegisterInfo.inc" 37 38 #define GET_SUBTARGETINFO_MC_DESC 39 #include "LoongArchGenSubtargetInfo.inc" 40 41 using namespace llvm; 42 43 static MCRegisterInfo *createLoongArchMCRegisterInfo(const Triple &TT) { 44 MCRegisterInfo *X = new MCRegisterInfo(); 45 InitLoongArchMCRegisterInfo(X, LoongArch::R1); 46 return X; 47 } 48 49 static MCInstrInfo *createLoongArchMCInstrInfo() { 50 MCInstrInfo *X = new MCInstrInfo(); 51 InitLoongArchMCInstrInfo(X); 52 return X; 53 } 54 55 static MCSubtargetInfo * 56 createLoongArchMCSubtargetInfo(const Triple &TT, StringRef CPU, StringRef FS) { 57 if (CPU.empty() || CPU == "generic") 58 CPU = TT.isArch64Bit() ? "la464" : "generic-la32"; 59 return createLoongArchMCSubtargetInfoImpl(TT, CPU, /*TuneCPU*/ CPU, FS); 60 } 61 62 static MCAsmInfo *createLoongArchMCAsmInfo(const MCRegisterInfo &MRI, 63 const Triple &TT, 64 const MCTargetOptions &Options) { 65 MCAsmInfo *MAI = new LoongArchMCAsmInfo(TT); 66 67 // Initial state of the frame pointer is sp(r3). 68 MCRegister SP = MRI.getDwarfRegNum(LoongArch::R3, true); 69 MCCFIInstruction Inst = MCCFIInstruction::cfiDefCfa(nullptr, SP, 0); 70 MAI->addInitialFrameState(Inst); 71 72 return MAI; 73 } 74 75 static MCInstPrinter *createLoongArchMCInstPrinter(const Triple &T, 76 unsigned SyntaxVariant, 77 const MCAsmInfo &MAI, 78 const MCInstrInfo &MII, 79 const MCRegisterInfo &MRI) { 80 return new LoongArchInstPrinter(MAI, MII, MRI); 81 } 82 83 static MCTargetStreamer * 84 createLoongArchObjectTargetStreamer(MCStreamer &S, const MCSubtargetInfo &STI) { 85 return STI.getTargetTriple().isOSBinFormatELF() 86 ? new LoongArchTargetELFStreamer(S, STI) 87 : nullptr; 88 } 89 90 namespace { 91 92 class LoongArchMCInstrAnalysis : public MCInstrAnalysis { 93 public: 94 explicit LoongArchMCInstrAnalysis(const MCInstrInfo *Info) 95 : MCInstrAnalysis(Info) {} 96 97 bool evaluateBranch(const MCInst &Inst, uint64_t Addr, uint64_t Size, 98 uint64_t &Target) const override { 99 unsigned NumOps = Inst.getNumOperands(); 100 if (isBranch(Inst) || Inst.getOpcode() == LoongArch::BL) { 101 Target = Addr + Inst.getOperand(NumOps - 1).getImm(); 102 return true; 103 } 104 105 return false; 106 } 107 }; 108 109 } // end namespace 110 111 static MCInstrAnalysis *createLoongArchInstrAnalysis(const MCInstrInfo *Info) { 112 return new LoongArchMCInstrAnalysis(Info); 113 } 114 115 namespace { 116 MCStreamer *createLoongArchELFStreamer(const Triple &T, MCContext &Context, 117 std::unique_ptr<MCAsmBackend> &&MAB, 118 std::unique_ptr<MCObjectWriter> &&MOW, 119 std::unique_ptr<MCCodeEmitter> &&MCE, 120 bool RelaxAll) { 121 return createLoongArchELFStreamer(Context, std::move(MAB), std::move(MOW), 122 std::move(MCE), RelaxAll); 123 } 124 } // end namespace 125 126 extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeLoongArchTargetMC() { 127 for (Target *T : {&getTheLoongArch32Target(), &getTheLoongArch64Target()}) { 128 TargetRegistry::RegisterMCRegInfo(*T, createLoongArchMCRegisterInfo); 129 TargetRegistry::RegisterMCInstrInfo(*T, createLoongArchMCInstrInfo); 130 TargetRegistry::RegisterMCSubtargetInfo(*T, createLoongArchMCSubtargetInfo); 131 TargetRegistry::RegisterMCAsmInfo(*T, createLoongArchMCAsmInfo); 132 TargetRegistry::RegisterMCCodeEmitter(*T, createLoongArchMCCodeEmitter); 133 TargetRegistry::RegisterMCAsmBackend(*T, createLoongArchAsmBackend); 134 TargetRegistry::RegisterMCInstPrinter(*T, createLoongArchMCInstPrinter); 135 TargetRegistry::RegisterMCInstrAnalysis(*T, createLoongArchInstrAnalysis); 136 TargetRegistry::RegisterELFStreamer(*T, createLoongArchELFStreamer); 137 TargetRegistry::RegisterObjectTargetStreamer( 138 *T, createLoongArchObjectTargetStreamer); 139 } 140 } 141