llvm-project/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchMCTargetDesc.cpp
Weining Lu 3347f77b80 [LoongArch] Add basic support to AsmParser
This patch adds basic support to AsmParser which can handle basic
instructions with register or immediate operands. With the addition of
the parser, now it's possible to test instructions encoding with `llvm-mc`.

Disassembler will be added later and then we can do `round-trip` test.

Reviewed By: xen0n, MaskRay, myhsu

Differential Revision: https://reviews.llvm.org/D120476
2022-03-09 16:20:36 +08:00

114 lines
4.0 KiB
C++

//===-- LoongArchMCTargetDesc.cpp - LoongArch Target Descriptions ---------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file provides LoongArch specific target descriptions.
//
//===----------------------------------------------------------------------===//
#include "LoongArchMCTargetDesc.h"
#include "LoongArchBaseInfo.h"
#include "LoongArchInstPrinter.h"
#include "LoongArchMCAsmInfo.h"
#include "TargetInfo/LoongArchTargetInfo.h"
#include "llvm/MC/MCAsmInfo.h"
#include "llvm/MC/MCDwarf.h"
#include "llvm/MC/MCInstrAnalysis.h"
#include "llvm/MC/MCInstrInfo.h"
#include "llvm/MC/MCRegisterInfo.h"
#include "llvm/MC/MCSubtargetInfo.h"
#include "llvm/MC/TargetRegistry.h"
#include "llvm/Support/Compiler.h"
#define GET_INSTRINFO_MC_DESC
#include "LoongArchGenInstrInfo.inc"
#define GET_REGINFO_MC_DESC
#include "LoongArchGenRegisterInfo.inc"
#define GET_SUBTARGETINFO_MC_DESC
#include "LoongArchGenSubtargetInfo.inc"
using namespace llvm;
static MCRegisterInfo *createLoongArchMCRegisterInfo(const Triple &TT) {
MCRegisterInfo *X = new MCRegisterInfo();
InitLoongArchMCRegisterInfo(X, LoongArch::R1);
return X;
}
static MCInstrInfo *createLoongArchMCInstrInfo() {
MCInstrInfo *X = new MCInstrInfo();
InitLoongArchMCInstrInfo(X);
return X;
}
static MCSubtargetInfo *
createLoongArchMCSubtargetInfo(const Triple &TT, StringRef CPU, StringRef FS) {
if (CPU.empty())
CPU = TT.isArch64Bit() ? "la464" : "generic-la32";
return createLoongArchMCSubtargetInfoImpl(TT, CPU, /*TuneCPU*/ CPU, FS);
}
static MCAsmInfo *createLoongArchMCAsmInfo(const MCRegisterInfo &MRI,
const Triple &TT,
const MCTargetOptions &Options) {
MCAsmInfo *MAI = new LoongArchMCAsmInfo(TT);
MCRegister SP = MRI.getDwarfRegNum(LoongArch::R2, true);
MCCFIInstruction Inst = MCCFIInstruction::cfiDefCfa(nullptr, SP, 0);
MAI->addInitialFrameState(Inst);
return MAI;
}
static MCInstPrinter *createLoongArchMCInstPrinter(const Triple &T,
unsigned SyntaxVariant,
const MCAsmInfo &MAI,
const MCInstrInfo &MII,
const MCRegisterInfo &MRI) {
return new LoongArchInstPrinter(MAI, MII, MRI);
}
namespace {
class LoongArchMCInstrAnalysis : public MCInstrAnalysis {
public:
explicit LoongArchMCInstrAnalysis(const MCInstrInfo *Info)
: MCInstrAnalysis(Info) {}
bool evaluateBranch(const MCInst &Inst, uint64_t Addr, uint64_t Size,
uint64_t &Target) const override {
unsigned NumOps = Inst.getNumOperands();
if (isBranch(Inst) || Inst.getOpcode() == LoongArch::BL) {
Target = Addr + Inst.getOperand(NumOps - 1).getImm();
return true;
}
return false;
}
};
} // end anonymous namespace
static MCInstrAnalysis *createLoongArchInstrAnalysis(const MCInstrInfo *Info) {
return new LoongArchMCInstrAnalysis(Info);
}
extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeLoongArchTargetMC() {
for (Target *T : {&getTheLoongArch32Target(), &getTheLoongArch64Target()}) {
TargetRegistry::RegisterMCRegInfo(*T, createLoongArchMCRegisterInfo);
TargetRegistry::RegisterMCInstrInfo(*T, createLoongArchMCInstrInfo);
TargetRegistry::RegisterMCSubtargetInfo(*T, createLoongArchMCSubtargetInfo);
TargetRegistry::RegisterMCAsmInfo(*T, createLoongArchMCAsmInfo);
TargetRegistry::RegisterMCCodeEmitter(*T, createLoongArchMCCodeEmitter);
TargetRegistry::RegisterMCAsmBackend(*T, createLoongArchAsmBackend);
TargetRegistry::RegisterMCInstPrinter(*T, createLoongArchMCInstPrinter);
TargetRegistry::RegisterMCInstrAnalysis(*T, createLoongArchInstrAnalysis);
}
}