LLVM 18.0.0git
Go to the documentation of this file.
1//=-- LanaiMCInstLower.cpp - Convert Lanai MachineInstr to an MCInst --------=//
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
9// This file contains code to lower Lanai MachineInstrs to their corresponding
10// MCInst records.
14#include "LanaiMCInstLower.h"
22#include "llvm/IR/Constants.h"
23#include "llvm/MC/MCAsmInfo.h"
24#include "llvm/MC/MCContext.h"
25#include "llvm/MC/MCExpr.h"
26#include "llvm/MC/MCInst.h"
30using namespace llvm;
34 return Printer.getSymbol(MO.getGlobal());
39 return Printer.GetBlockAddressSymbol(MO.getBlockAddress());
44 return Printer.GetExternalSymbolSymbol(MO.getSymbolName());
50 << Printer.getFunctionNumber() << '_'
51 << MO.getIndex();
52 // Create a symbol for the name.
53 return Ctx.getOrCreateSymbol(Name.str());
60 << Printer.getFunctionNumber() << '_'
61 << MO.getIndex();
62 // Create a symbol for the name.
63 return Ctx.getOrCreateSymbol(Name.str());
67 MCSymbol *Sym) const {
70 switch (MO.getTargetFlags()) {
73 break;
76 break;
79 break;
80 default:
81 llvm_unreachable("Unknown target flag on GV operand");
82 }
84 const MCExpr *Expr =
86 if (!MO.isJTI() && MO.getOffset())
88 Expr, MCConstantExpr::create(MO.getOffset(), Ctx), Ctx);
89 Expr = LanaiMCExpr::create(Kind, Expr, Ctx);
90 return MCOperand::createExpr(Expr);
93void LanaiMCInstLower::Lower(const MachineInstr *MI, MCInst &OutMI) const {
94 OutMI.setOpcode(MI->getOpcode());
96 for (const MachineOperand &MO : MI->operands()) {
97 MCOperand MCOp;
98 switch (MO.getType()) {
100 // Ignore all implicit register operands.
101 if (MO.isImplicit())
102 continue;
103 MCOp = MCOperand::createReg(MO.getReg());
104 break;
106 MCOp = MCOperand::createImm(MO.getImm());
107 break;
110 MCSymbolRefExpr::create(MO.getMBB()->getSymbol(), Ctx));
111 break;
113 continue;
116 break;
119 break;
122 break;
125 break;
128 break;
129 default:
130 MI->print(errs());
131 llvm_unreachable("unknown operand type");
132 }
134 OutMI.addOperand(MCOp);
135 }
This file contains the declarations for the subclasses of Constant, which represent the different fla...
std::string Name
Symbol * Sym
Definition: ELF_riscv.cpp:468
IRTranslator LLVM IR MI
This file defines the SmallString class.
MCSymbol * getSymbol(const GlobalValue *GV) const
Definition: AsmPrinter.cpp:671
const MCAsmInfo * MAI
Target Asm Printer information.
Definition: AsmPrinter.h:91
unsigned getFunctionNumber() const
Return a unique ID for the current function.
Definition: AsmPrinter.cpp:377
MCSymbol * GetExternalSymbolSymbol(StringRef 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 LanaiMCExpr * create(VariantKind Kind, const MCExpr *Expr, MCContext &Ctx)
Definition: LanaiMCExpr.cpp:17
MCSymbol * GetGlobalAddressSymbol(const MachineOperand &MO) const
MCSymbol * GetExternalSymbolSymbol(const MachineOperand &MO) const
MCSymbol * GetConstantPoolIndexSymbol(const MachineOperand &MO) const
MCOperand LowerSymbolOperand(const MachineOperand &MO, MCSymbol *Sym) const
MCSymbol * GetBlockAddressSymbol(const MachineOperand &MO) const
MCSymbol * GetJumpTableSymbol(const MachineOperand &MO) const
void Lower(const MachineInstr *MI, MCInst &OutMI) const
StringRef getPrivateGlobalPrefix() const
Definition: MCAsmInfo.h:668
static const MCBinaryExpr * createAdd(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition: MCExpr.h:528
static const MCConstantExpr * create(int64_t Value, MCContext &Ctx, bool PrintInHex=false, unsigned SizeInBytes=0)
Definition: MCExpr.cpp:194
MCSymbol * getOrCreateSymbol(const Twine &Name)
Lookup the symbol inside with the specified Name.
Definition: MCContext.cpp:201
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:389
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
Representation of each machine instruction.
Definition: MachineInstr.h:68
MachineOperand class - Representation of each machine instruction operand.
const GlobalValue * getGlobal() const
bool isJTI() const
isJTI - Tests if this is a MO_JumpTableIndex operand.
const BlockAddress * getBlockAddress() const
unsigned getTargetFlags() const
const char * getSymbolName() const
@ MO_Immediate
Immediate operand.
@ MO_ConstantPoolIndex
Address of indexed Constant in Constant Pool.
@ 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.
int64_t getOffset() const
Return the offset from the symbol in this operand.
virtual void print(raw_ostream &OS, const Module *M) const
print - Print out the internal state of the pass.
Definition: Pass.cpp:130
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition: SmallString.h:26
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:672
#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
raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.