LLVM 19.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
22using namespace llvm;
23
25 : Ctx(Ctx), Printer(Printer) {}
26
27void 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;
50 break;
53 break;
56 break;
59 break;
62 break;
65 break;
66 }
67 const MCExpr *ME =
69
70 if (Kind != CSKYMCExpr::VK_CSKY_None)
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;
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}
dxil pretty DXIL Metadata Pretty Printer
Symbol * Sym
Definition: ELF_riscv.cpp:479
IRTranslator LLVM IR MI
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:84
MCSymbol * getSymbol(const GlobalValue *GV) const
Definition: AsmPrinter.cpp:700
virtual MCSymbol * GetCPISymbol(unsigned CPID) const
Return the symbol for the specified constant pool entry.
MCSymbol * GetJTISymbol(unsigned JTID, bool isLinkerPrivate=false) const
Return the symbol for the specified jump table entry.
MCContext & OutContext
This is the context for the output file that we are streaming.
Definition: AsmPrinter.h:94
MCSymbol * GetExternalSymbolSymbol(Twine Sym) const
Return the MCSymbol for the specified ExternalSymbol.
MCSymbol * GetBlockAddressSymbol(const BlockAddress *BA) const
Return the MCSymbol used to satisfy BlockAddress uses of the specified basic block.
static const CSKYMCExpr * create(const MCExpr *Expr, VariantKind Kind, MCContext &Ctx)
Definition: CSKYMCExpr.cpp:22
bool lowerOperand(const MachineOperand &MO, MCOperand &MCOp) const
void Lower(const MachineInstr *MI, MCInst &OutMI) const
CSKYMCInstLower(MCContext &Ctx, AsmPrinter &Printer)
MCOperand lowerSymbolOperand(const MachineOperand &MO, MCSymbol *Sym) const
Context object for machine code objects.
Definition: MCContext.h:81
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:35
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:184
void addOperand(const MCOperand Op)
Definition: MCInst.h:210
void setOpcode(unsigned Op)
Definition: MCInst.h:197
Instances of this class represent operands of the MCInst class.
Definition: MCInst.h:36
static MCOperand createReg(unsigned Reg)
Definition: MCInst.h:134
static MCOperand createExpr(const MCExpr *Val)
Definition: MCInst.h:162
static MCOperand createImm(int64_t Val)
Definition: MCInst.h:141
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx)
Definition: MCExpr.h:397
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:40
MCSymbol * getSymbol() const
Return the MCSymbol for this basic block.
Representation of each machine instruction.
Definition: MachineInstr.h:69
MachineOperand class - Representation of each machine instruction operand.
const GlobalValue * getGlobal() const
int64_t getImm() const
bool isImplicit() const
MachineBasicBlock * getMBB() const
const BlockAddress * getBlockAddress() const
unsigned getTargetFlags() const
MachineOperandType getType() const
getType - Returns the MachineOperandType for this operand.
const char * getSymbolName() const
Register getReg() const
getReg - Returns the register number.
MCSymbol * getMCSymbol() const
@ MO_Immediate
Immediate operand.
@ MO_ConstantPoolIndex
Address of indexed Constant in Constant Pool.
@ MO_MCSymbol
MCSymbol reference (for debug/eh info)
@ MO_GlobalAddress
Address of a global value.
@ MO_RegisterMask
Mask of preserved registers.
@ MO_BlockAddress
Address of a basic block.
@ MO_MachineBasicBlock
MachineBasicBlock reference.
@ MO_Register
Register operand.
@ MO_ExternalSymbol
Name of external global symbol.
@ MO_JumpTableIndex
Address of indexed Jump Table for switch.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18