Path: blob/main/contrib/llvm-project/llvm/lib/Target/CSKY/CSKYMCInstLower.cpp
35266 views
//===-- CSKYMCInstLower.cpp - Convert CSKY MachineInstr to an MCInst --------=//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 contains code to lower CSKY MachineInstrs to their corresponding9// MCInst records.10//11//===----------------------------------------------------------------------===//1213#include "CSKYMCInstLower.h"14#include "MCTargetDesc/CSKYBaseInfo.h"15#include "MCTargetDesc/CSKYMCExpr.h"16#include "llvm/CodeGen/AsmPrinter.h"17#include "llvm/MC/MCExpr.h"1819#define DEBUG_TYPE "csky-mcinst-lower"2021using namespace llvm;2223CSKYMCInstLower::CSKYMCInstLower(MCContext &Ctx, AsmPrinter &Printer)24: Ctx(Ctx), Printer(Printer) {}2526void CSKYMCInstLower::Lower(const MachineInstr *MI, MCInst &OutMI) const {27OutMI.setOpcode(MI->getOpcode());2829for (const MachineOperand &MO : MI->operands()) {30MCOperand MCOp;31if (lowerOperand(MO, MCOp))32OutMI.addOperand(MCOp);33}34}3536MCOperand CSKYMCInstLower::lowerSymbolOperand(const MachineOperand &MO,37MCSymbol *Sym) const {38CSKYMCExpr::VariantKind Kind;39MCContext &Ctx = Printer.OutContext;4041switch (MO.getTargetFlags()) {42default:43llvm_unreachable("Unknown target flag.");44case CSKYII::MO_None:45Kind = CSKYMCExpr::VK_CSKY_None;46break;47case CSKYII::MO_GOT32:48Kind = CSKYMCExpr::VK_CSKY_GOT;49break;50case CSKYII::MO_GOTOFF:51Kind = CSKYMCExpr::VK_CSKY_GOTOFF;52break;53case CSKYII::MO_ADDR32:54Kind = CSKYMCExpr::VK_CSKY_ADDR;55break;56case CSKYII::MO_PLT32:57Kind = CSKYMCExpr::VK_CSKY_PLT;58break;59case CSKYII::MO_ADDR_HI16:60Kind = CSKYMCExpr::VK_CSKY_ADDR_HI16;61break;62case CSKYII::MO_ADDR_LO16:63Kind = CSKYMCExpr::VK_CSKY_ADDR_LO16;64break;65}66const MCExpr *ME =67MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, Ctx);6869if (Kind != CSKYMCExpr::VK_CSKY_None)70ME = CSKYMCExpr::create(ME, Kind, Ctx);7172return MCOperand::createExpr(ME);73}7475bool CSKYMCInstLower::lowerOperand(const MachineOperand &MO,76MCOperand &MCOp) const {77switch (MO.getType()) {78default:79llvm_unreachable("unknown operand type");80case MachineOperand::MO_RegisterMask:81break;82case MachineOperand::MO_Immediate:83MCOp = MCOperand::createImm(MO.getImm());84break;85case MachineOperand::MO_Register:86if (MO.isImplicit())87return false;88MCOp = MCOperand::createReg(MO.getReg());89break;90case MachineOperand::MO_MachineBasicBlock:91MCOp = MCOperand::createExpr(92MCSymbolRefExpr::create(MO.getMBB()->getSymbol(), Ctx));93break;94case MachineOperand::MO_GlobalAddress:95MCOp = lowerSymbolOperand(MO, Printer.getSymbol(MO.getGlobal()));96break;97case MachineOperand::MO_BlockAddress:98MCOp = lowerSymbolOperand(99MO, Printer.GetBlockAddressSymbol(MO.getBlockAddress()));100break;101case MachineOperand::MO_ExternalSymbol:102MCOp = lowerSymbolOperand(103MO, Printer.GetExternalSymbolSymbol(MO.getSymbolName()));104break;105case MachineOperand::MO_ConstantPoolIndex:106MCOp = lowerSymbolOperand(MO, Printer.GetCPISymbol(MO.getIndex()));107break;108case MachineOperand::MO_JumpTableIndex:109MCOp = lowerSymbolOperand(MO, Printer.GetJTISymbol(MO.getIndex()));110break;111case MachineOperand::MO_MCSymbol:112MCOp = lowerSymbolOperand(MO, MO.getMCSymbol());113break;114}115return true;116}117118119