Path: blob/main/contrib/llvm-project/llvm/lib/Target/Lanai/MCTargetDesc/LanaiMCTargetDesc.cpp
35295 views
//===-- LanaiMCTargetDesc.cpp - Lanai Target Descriptions -----------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file provides Lanai specific target descriptions.9//10//===----------------------------------------------------------------------===//1112#include "LanaiMCTargetDesc.h"13#include "LanaiInstPrinter.h"14#include "LanaiMCAsmInfo.h"15#include "TargetInfo/LanaiTargetInfo.h"16#include "llvm/ADT/StringRef.h"17#include "llvm/MC/MCInst.h"18#include "llvm/MC/MCInstrAnalysis.h"19#include "llvm/MC/MCInstrInfo.h"20#include "llvm/MC/MCRegisterInfo.h"21#include "llvm/MC/MCStreamer.h"22#include "llvm/MC/MCSubtargetInfo.h"23#include "llvm/MC/TargetRegistry.h"24#include "llvm/Support/ErrorHandling.h"25#include "llvm/TargetParser/Triple.h"26#include <cstdint>27#include <string>2829#define GET_INSTRINFO_MC_DESC30#define ENABLE_INSTR_PREDICATE_VERIFIER31#include "LanaiGenInstrInfo.inc"3233#define GET_SUBTARGETINFO_MC_DESC34#include "LanaiGenSubtargetInfo.inc"3536#define GET_REGINFO_MC_DESC37#include "LanaiGenRegisterInfo.inc"3839using namespace llvm;4041static MCInstrInfo *createLanaiMCInstrInfo() {42MCInstrInfo *X = new MCInstrInfo();43InitLanaiMCInstrInfo(X);44return X;45}4647static MCRegisterInfo *createLanaiMCRegisterInfo(const Triple & /*TT*/) {48MCRegisterInfo *X = new MCRegisterInfo();49InitLanaiMCRegisterInfo(X, Lanai::RCA, 0, 0, Lanai::PC);50return X;51}5253static MCSubtargetInfo *54createLanaiMCSubtargetInfo(const Triple &TT, StringRef CPU, StringRef FS) {55std::string CPUName = std::string(CPU);56if (CPUName.empty())57CPUName = "generic";5859return createLanaiMCSubtargetInfoImpl(TT, CPUName, /*TuneCPU*/ CPUName, FS);60}6162static MCStreamer *createMCStreamer(const Triple &T, MCContext &Context,63std::unique_ptr<MCAsmBackend> &&MAB,64std::unique_ptr<MCObjectWriter> &&OW,65std::unique_ptr<MCCodeEmitter> &&Emitter) {66if (!T.isOSBinFormatELF())67llvm_unreachable("OS not supported");6869return createELFStreamer(Context, std::move(MAB), std::move(OW),70std::move(Emitter));71}7273static MCInstPrinter *createLanaiMCInstPrinter(const Triple & /*T*/,74unsigned SyntaxVariant,75const MCAsmInfo &MAI,76const MCInstrInfo &MII,77const MCRegisterInfo &MRI) {78if (SyntaxVariant == 0)79return new LanaiInstPrinter(MAI, MII, MRI);80return nullptr;81}8283static MCRelocationInfo *createLanaiElfRelocation(const Triple &TheTriple,84MCContext &Ctx) {85return createMCRelocationInfo(TheTriple, Ctx);86}8788namespace {8990class LanaiMCInstrAnalysis : public MCInstrAnalysis {91public:92explicit LanaiMCInstrAnalysis(const MCInstrInfo *Info)93: MCInstrAnalysis(Info) {}9495bool evaluateBranch(const MCInst &Inst, uint64_t Addr, uint64_t Size,96uint64_t &Target) const override {97if (Inst.getNumOperands() == 0)98return false;99if (!isConditionalBranch(Inst) && !isUnconditionalBranch(Inst) &&100!isCall(Inst))101return false;102103if (Info->get(Inst.getOpcode()).operands()[0].OperandType ==104MCOI::OPERAND_PCREL) {105int64_t Imm = Inst.getOperand(0).getImm();106Target = Addr + Size + Imm;107return true;108} else {109int64_t Imm = Inst.getOperand(0).getImm();110111// Skip case where immediate is 0 as that occurs in file that isn't linked112// and the branch target inferred would be wrong.113if (Imm == 0)114return false;115116Target = Imm;117return true;118}119}120};121122} // end anonymous namespace123124static MCInstrAnalysis *createLanaiInstrAnalysis(const MCInstrInfo *Info) {125return new LanaiMCInstrAnalysis(Info);126}127128extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeLanaiTargetMC() {129// Register the MC asm info.130RegisterMCAsmInfo<LanaiMCAsmInfo> X(getTheLanaiTarget());131132// Register the MC instruction info.133TargetRegistry::RegisterMCInstrInfo(getTheLanaiTarget(),134createLanaiMCInstrInfo);135136// Register the MC register info.137TargetRegistry::RegisterMCRegInfo(getTheLanaiTarget(),138createLanaiMCRegisterInfo);139140// Register the MC subtarget info.141TargetRegistry::RegisterMCSubtargetInfo(getTheLanaiTarget(),142createLanaiMCSubtargetInfo);143144// Register the MC code emitter145TargetRegistry::RegisterMCCodeEmitter(getTheLanaiTarget(),146createLanaiMCCodeEmitter);147148// Register the ASM Backend149TargetRegistry::RegisterMCAsmBackend(getTheLanaiTarget(),150createLanaiAsmBackend);151152// Register the MCInstPrinter.153TargetRegistry::RegisterMCInstPrinter(getTheLanaiTarget(),154createLanaiMCInstPrinter);155156// Register the ELF streamer.157TargetRegistry::RegisterELFStreamer(getTheLanaiTarget(), createMCStreamer);158159// Register the MC relocation info.160TargetRegistry::RegisterMCRelocationInfo(getTheLanaiTarget(),161createLanaiElfRelocation);162163// Register the MC instruction analyzer.164TargetRegistry::RegisterMCInstrAnalysis(getTheLanaiTarget(),165createLanaiInstrAnalysis);166}167168169