LLVM  15.0.0git
CSKYMCInstLower.cpp
Go to the documentation of this file.
1 //===-- CSKYMCInstLower.cpp - Convert CSKY MachineInstr to an MCInst --------=//
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 contains code to lower CSKY MachineInstrs to their corresponding
10 // MCInst records.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "CSKYMCInstLower.h"
18 #include "llvm/MC/MCExpr.h"
19 
20 #define DEBUG_TYPE "csky-mcinst-lower"
21 
22 using namespace llvm;
23 
25  : Ctx(Ctx), Printer(Printer) {}
26 
27 void CSKYMCInstLower::Lower(const MachineInstr *MI, MCInst &OutMI) const {
28  OutMI.setOpcode(MI->getOpcode());
29 
30  for (const MachineOperand &MO : MI->operands()) {
31  MCOperand MCOp;
32  if (lowerOperand(MO, MCOp))
33  OutMI.addOperand(MCOp);
34  }
35 }
36 
38  MCSymbol *Sym) const {
40  MCContext &Ctx = Printer.OutContext;
41 
42  switch (MO.getTargetFlags()) {
43  default:
44  llvm_unreachable("Unknown target flag.");
45  case CSKYII::MO_None:
47  break;
48  case CSKYII::MO_GOT32:
50  break;
51  case CSKYII::MO_GOTOFF:
53  break;
54  case CSKYII::MO_ADDR32:
56  break;
57  case CSKYII::MO_PLT32:
59  break;
62  break;
65  break;
66  }
67  const MCExpr *ME =
69 
71  ME = CSKYMCExpr::create(ME, Kind, Ctx);
72 
73  return MCOperand::createExpr(ME);
74 }
75 
77  MCOperand &MCOp) const {
78  switch (MO.getType()) {
79  default:
80  llvm_unreachable("unknown operand type");
82  break;
84  MCOp = MCOperand::createImm(MO.getImm());
85  break;
87  if (MO.isImplicit())
88  return false;
89  MCOp = MCOperand::createReg(MO.getReg());
90  break;
92  MCOp = MCOperand::createExpr(
94  break;
96  MCOp = lowerSymbolOperand(MO, Printer.getSymbol(MO.getGlobal()));
97  break;
99  MCOp = lowerSymbolOperand(
100  MO, Printer.GetBlockAddressSymbol(MO.getBlockAddress()));
101  break;
103  MCOp = lowerSymbolOperand(
104  MO, Printer.GetExternalSymbolSymbol(MO.getSymbolName()));
105  break;
107  MCOp = lowerSymbolOperand(MO, Printer.GetCPISymbol(MO.getIndex()));
108  break;
110  MCOp = lowerSymbolOperand(MO, Printer.GetJTISymbol(MO.getIndex()));
111  break;
113  MCOp = lowerSymbolOperand(MO, MO.getMCSymbol());
114  break;
115  }
116  return true;
117 }
llvm::CSKYMCInstLower::CSKYMCInstLower
CSKYMCInstLower(MCContext &Ctx, AsmPrinter &Printer)
Definition: CSKYMCInstLower.cpp:24
llvm::CSKYMCExpr::VK_CSKY_GOTOFF
@ VK_CSKY_GOTOFF
Definition: CSKYMCExpr.h:28
AsmPrinter.h
llvm::MachineOperand::MO_BlockAddress
@ MO_BlockAddress
Address of a basic block.
Definition: MachineOperand.h:62
MI
IRTranslator LLVM IR MI
Definition: IRTranslator.cpp:104
llvm::MachineOperand::MO_Immediate
@ MO_Immediate
Immediate operand.
Definition: MachineOperand.h:52
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:17
llvm::MCSymbol
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
llvm::MCOperand::createExpr
static MCOperand createExpr(const MCExpr *Val)
Definition: MCInst.h:162
llvm::MachineOperand::getGlobal
const GlobalValue * getGlobal() const
Definition: MachineOperand.h:572
CSKYBaseInfo.h
llvm::CSKYII::MO_ADDR_LO16
@ MO_ADDR_LO16
Definition: CSKYBaseInfo.h:47
llvm::MCOperand::createImm
static MCOperand createImm(int64_t Val)
Definition: MCInst.h:141
llvm::MCContext
Context object for machine code objects.
Definition: MCContext.h:74
CSKYMCInstLower.h
llvm::MachineOperand::getBlockAddress
const BlockAddress * getBlockAddress() const
Definition: MachineOperand.h:577
llvm::CSKYMCExpr::create
static const CSKYMCExpr * create(const MCExpr *Expr, VariantKind Kind, MCContext &Ctx)
Definition: CSKYMCExpr.cpp:22
llvm::CSKYMCInstLower::lowerSymbolOperand
MCOperand lowerSymbolOperand(const MachineOperand &MO, MCSymbol *Sym) const
Definition: CSKYMCInstLower.cpp:37
llvm::CSKYMCExpr::VK_CSKY_GOT
@ VK_CSKY_GOT
Definition: CSKYMCExpr.h:25
llvm::MCInst
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:184
llvm::MachineOperand::getMCSymbol
MCSymbol * getMCSymbol() const
Definition: MachineOperand.h:582
llvm::CSKYMCExpr::VK_CSKY_None
@ VK_CSKY_None
Definition: CSKYMCExpr.h:20
llvm::MachineOperand::MO_Register
@ MO_Register
Register operand.
Definition: MachineOperand.h:51
llvm::MCInst::setOpcode
void setOpcode(unsigned Op)
Definition: MCInst.h:197
llvm::MachineBasicBlock::getSymbol
MCSymbol * getSymbol() const
Return the MCSymbol for this basic block.
Definition: MachineBasicBlock.cpp:57
Printer
print alias Alias Set Printer
Definition: AliasSetTracker.cpp:758
llvm::MachineOperand::isImplicit
bool isImplicit() const
Definition: MachineOperand.h:379
llvm::CSKYII::MO_GOT32
@ MO_GOT32
Definition: CSKYBaseInfo.h:43
llvm::CSKYMCExpr::VK_CSKY_ADDR_HI16
@ VK_CSKY_ADDR_HI16
Definition: CSKYMCExpr.h:22
llvm::MachineOperand::MO_GlobalAddress
@ MO_GlobalAddress
Address of a global value.
Definition: MachineOperand.h:61
llvm::MachineOperand::getImm
int64_t getImm() const
Definition: MachineOperand.h:546
llvm::CSKYMCExpr::VK_CSKY_ADDR
@ VK_CSKY_ADDR
Definition: CSKYMCExpr.h:21
llvm::CSKYMCInstLower::lowerOperand
bool lowerOperand(const MachineOperand &MO, MCOperand &MCOp) const
Definition: CSKYMCInstLower.cpp:76
llvm::MachineOperand
MachineOperand class - Representation of each machine instruction operand.
Definition: MachineOperand.h:48
llvm::CSKYMCExpr::VK_CSKY_PLT
@ VK_CSKY_PLT
Definition: CSKYMCExpr.h:29
llvm::lltok::Kind
Kind
Definition: LLToken.h:18
llvm::CSKYMCExpr::VK_CSKY_ADDR_LO16
@ VK_CSKY_ADDR_LO16
Definition: CSKYMCExpr.h:23
llvm::MCInst::addOperand
void addOperand(const MCOperand Op)
Definition: MCInst.h:210
llvm::CSKYMCInstLower::Lower
void Lower(const MachineInstr *MI, MCInst &OutMI) const
Definition: CSKYMCInstLower.cpp:27
llvm::CSKYII::MO_GOTOFF
@ MO_GOTOFF
Definition: CSKYBaseInfo.h:44
llvm::MachineOperand::getTargetFlags
unsigned getTargetFlags() const
Definition: MachineOperand.h:220
llvm::AsmPrinter::GetCPISymbol
virtual MCSymbol * GetCPISymbol(unsigned CPID) const
Return the symbol for the specified constant pool entry.
Definition: AsmPrinter.cpp:3330
llvm::MachineInstr
Representation of each machine instruction.
Definition: MachineInstr.h:66
llvm::CSKYII::MO_None
@ MO_None
Definition: CSKYBaseInfo.h:41
llvm::AsmPrinter::GetBlockAddressSymbol
MCSymbol * GetBlockAddressSymbol(const BlockAddress *BA) const
Return the MCSymbol used to satisfy BlockAddress uses of the specified basic block.
Definition: AsmPrinter.cpp:3320
llvm::MachineOperand::MO_MCSymbol
@ MO_MCSymbol
MCSymbol reference (for debug/eh info)
Definition: MachineOperand.h:66
llvm::MachineOperand::getType
MachineOperandType getType() const
getType - Returns the MachineOperandType for this operand.
Definition: MachineOperand.h:218
llvm::CSKYMCExpr::VariantKind
VariantKind
Definition: CSKYMCExpr.h:19
llvm::AsmPrinter::getSymbol
MCSymbol * getSymbol(const GlobalValue *GV) const
Definition: AsmPrinter.cpp:654
llvm::AsmPrinter::OutContext
MCContext & OutContext
This is the context for the output file that we are streaming.
Definition: AsmPrinter.h:91
llvm::MachineOperand::getReg
Register getReg() const
getReg - Returns the register number.
Definition: MachineOperand.h:359
llvm::MachineOperand::MO_JumpTableIndex
@ MO_JumpTableIndex
Address of indexed Jump Table for switch.
Definition: MachineOperand.h:59
llvm::AsmPrinter::GetExternalSymbolSymbol
MCSymbol * GetExternalSymbolSymbol(StringRef Sym) const
Return the MCSymbol for the specified ExternalSymbol.
Definition: AsmPrinter.cpp:3377
llvm::MachineOperand::getMBB
MachineBasicBlock * getMBB() const
Definition: MachineOperand.h:561
llvm::AsmPrinter::GetJTISymbol
MCSymbol * GetJTISymbol(unsigned JTID, bool isLinkerPrivate=false) const
Return the symbol for the specified jump table entry.
Definition: AsmPrinter.cpp:3358
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:143
llvm::MCOperand::createReg
static MCOperand createReg(unsigned Reg)
Definition: MCInst.h:134
llvm::CSKYII::MO_ADDR_HI16
@ MO_ADDR_HI16
Definition: CSKYBaseInfo.h:46
llvm::MachineOperand::MO_MachineBasicBlock
@ MO_MachineBasicBlock
MachineBasicBlock reference.
Definition: MachineOperand.h:55
llvm::MachineOperand::MO_ExternalSymbol
@ MO_ExternalSymbol
Name of external global symbol.
Definition: MachineOperand.h:60
llvm::MachineOperand::getIndex
int getIndex() const
Definition: MachineOperand.h:566
llvm::AsmPrinter
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:81
llvm::MCSymbolRefExpr::create
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx)
Definition: MCExpr.h:386
CSKYMCExpr.h
llvm::MachineOperand::getSymbolName
const char * getSymbolName() const
Definition: MachineOperand.h:617
llvm::CSKYII::MO_ADDR32
@ MO_ADDR32
Definition: CSKYBaseInfo.h:42
llvm::MCOperand
Instances of this class represent operands of the MCInst class.
Definition: MCInst.h:36
llvm::MCSymbolRefExpr::VK_None
@ VK_None
Definition: MCExpr.h:195
llvm::CSKYII::MO_PLT32
@ MO_PLT32
Definition: CSKYBaseInfo.h:45
llvm::MachineOperand::MO_RegisterMask
@ MO_RegisterMask
Mask of preserved registers.
Definition: MachineOperand.h:63
MCExpr.h
llvm::MCExpr
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:35
llvm::MachineOperand::MO_ConstantPoolIndex
@ MO_ConstantPoolIndex
Address of indexed Constant in Constant Pool.
Definition: MachineOperand.h:57