LLVM  13.0.0git
LanaiMCInstLower.cpp
Go to the documentation of this file.
1 //=-- LanaiMCInstLower.cpp - Convert Lanai 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 Lanai MachineInstrs to their corresponding
10 // MCInst records.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "LanaiMCInstLower.h"
15 
18 #include "llvm/ADT/SmallString.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"
29 
30 using namespace llvm;
31 
32 MCSymbol *
34  return Printer.getSymbol(MO.getGlobal());
35 }
36 
37 MCSymbol *
39  return Printer.GetBlockAddressSymbol(MO.getBlockAddress());
40 }
41 
42 MCSymbol *
44  return Printer.GetExternalSymbolSymbol(MO.getSymbolName());
45 }
46 
49  raw_svector_ostream(Name) << Printer.MAI->getPrivateGlobalPrefix() << "JTI"
50  << Printer.getFunctionNumber() << '_'
51  << MO.getIndex();
52  // Create a symbol for the name.
53  return Ctx.getOrCreateSymbol(Name.str());
54 }
55 
56 MCSymbol *
59  raw_svector_ostream(Name) << Printer.MAI->getPrivateGlobalPrefix() << "CPI"
60  << Printer.getFunctionNumber() << '_'
61  << MO.getIndex();
62  // Create a symbol for the name.
63  return Ctx.getOrCreateSymbol(Name.str());
64 }
65 
67  MCSymbol *Sym) const {
69 
70  switch (MO.getTargetFlags()) {
73  break;
74  case LanaiII::MO_ABS_HI:
76  break;
77  case LanaiII::MO_ABS_LO:
79  break;
80  default:
81  llvm_unreachable("Unknown target flag on GV operand");
82  }
83 
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);
91 }
92 
93 void LanaiMCInstLower::Lower(const MachineInstr *MI, MCInst &OutMI) const {
94  OutMI.setOpcode(MI->getOpcode());
95 
96  for (unsigned I = 0, E = MI->getNumOperands(); I != E; ++I) {
97  const MachineOperand &MO = MI->getOperand(I);
98 
99  MCOperand MCOp;
100  switch (MO.getType()) {
102  // Ignore all implicit register operands.
103  if (MO.isImplicit())
104  continue;
105  MCOp = MCOperand::createReg(MO.getReg());
106  break;
108  MCOp = MCOperand::createImm(MO.getImm());
109  break;
111  MCOp = MCOperand::createExpr(
113  break;
115  continue;
118  break;
121  break;
124  break;
126  MCOp = LowerSymbolOperand(MO, GetJumpTableSymbol(MO));
127  break;
130  break;
131  default:
132  MI->print(errs());
133  llvm_unreachable("unknown operand type");
134  }
135 
136  OutMI.addOperand(MCOp);
137  }
138 }
AsmPrinter.h
llvm::MachineOperand::MO_BlockAddress
@ MO_BlockAddress
Address of a basic block.
Definition: MachineOperand.h:63
LanaiMCInstLower.h
MI
IRTranslator LLVM IR MI
Definition: IRTranslator.cpp:102
MachineInstr.h
llvm::MachineOperand::MO_Immediate
@ MO_Immediate
Immediate operand.
Definition: MachineOperand.h:53
llvm
Definition: AllocatorList.h:23
llvm::LanaiMCInstLower::GetBlockAddressSymbol
MCSymbol * GetBlockAddressSymbol(const MachineOperand &MO) const
Definition: LanaiMCInstLower.cpp:38
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:563
llvm::MCOperand::createImm
static MCOperand createImm(int64_t Val)
Definition: MCInst.h:141
llvm::AsmPrinter::MAI
const MCAsmInfo * MAI
Target Asm Printer information.
Definition: AsmPrinter.h:88
llvm::MCAsmInfo::getPrivateGlobalPrefix
StringRef getPrivateGlobalPrefix() const
Definition: MCAsmInfo.h:643
llvm::MCConstantExpr::create
static const MCConstantExpr * create(int64_t Value, MCContext &Ctx, bool PrintInHex=false, unsigned SizeInBytes=0)
Definition: MCExpr.cpp:194
llvm::MachineOperand::getBlockAddress
const BlockAddress * getBlockAddress() const
Definition: MachineOperand.h:568
LanaiBaseInfo.h
ErrorHandling.h
MachineBasicBlock.h
llvm::MachineOperand::isJTI
bool isJTI() const
isJTI - Tests if this is a MO_JumpTableIndex operand.
Definition: MachineOperand.h:337
llvm::LanaiII::MO_ABS_LO
@ MO_ABS_LO
Definition: LanaiBaseInfo.h:36
llvm::errs
raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
Definition: raw_ostream.cpp:892
llvm::MCInst
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:184
llvm::MachineOperand::getOffset
int64_t getOffset() const
Return the offset from the symbol in this operand.
Definition: MachineOperand.h:600
llvm::MachineOperand::MO_Register
@ MO_Register
Register operand.
Definition: MachineOperand.h:52
llvm::LanaiII::MO_NO_FLAG
@ MO_NO_FLAG
Definition: LanaiBaseInfo.h:31
llvm::MCInst::setOpcode
void setOpcode(unsigned Op)
Definition: MCInst.h:197
llvm::LanaiMCInstLower::GetConstantPoolIndexSymbol
MCSymbol * GetConstantPoolIndexSymbol(const MachineOperand &MO) const
Definition: LanaiMCInstLower.cpp:57
llvm::MachineBasicBlock::getSymbol
MCSymbol * getSymbol() const
Return the MCSymbol for this basic block.
Definition: MachineBasicBlock.cpp:60
llvm::MCContext::getOrCreateSymbol
MCSymbol * getOrCreateSymbol(const Twine &Name)
Lookup the symbol inside with the specified Name.
Definition: MCContext.cpp:187
llvm::MachineOperand::isImplicit
bool isImplicit() const
Definition: MachineOperand.h:380
Constants.h
SmallString.h
llvm::MachineOperand::MO_GlobalAddress
@ MO_GlobalAddress
Address of a global value.
Definition: MachineOperand.h:62
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
llvm::MachineOperand::getImm
int64_t getImm() const
Definition: MachineOperand.h:537
llvm::LanaiMCExpr::create
static const LanaiMCExpr * create(VariantKind Kind, const MCExpr *Expr, MCContext &Ctx)
Definition: LanaiMCExpr.cpp:17
MCContext.h
llvm::LanaiMCExpr::VK_Lanai_ABS_HI
@ VK_Lanai_ABS_HI
Definition: LanaiMCExpr.h:19
MCInst.h
llvm::MachineOperand
MachineOperand class - Representation of each machine instruction operand.
Definition: MachineOperand.h:49
llvm::lltok::Kind
Kind
Definition: LLToken.h:18
llvm::MCInst::addOperand
void addOperand(const MCOperand Op)
Definition: MCInst.h:210
llvm::SmallString< 256 >
llvm::Pass::print
virtual void print(raw_ostream &OS, const Module *M) const
print - Print out the internal state of the pass.
Definition: Pass.cpp:125
llvm::LanaiMCInstLower::GetGlobalAddressSymbol
MCSymbol * GetGlobalAddressSymbol(const MachineOperand &MO) const
Definition: LanaiMCInstLower.cpp:33
llvm::MachineOperand::getTargetFlags
unsigned getTargetFlags() const
Definition: MachineOperand.h:221
llvm::MachineInstr
Representation of each machine instruction.
Definition: MachineInstr.h:64
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:3089
I
#define I(x, y, z)
Definition: MD5.cpp:59
llvm::MachineOperand::getType
MachineOperandType getType() const
getType - Returns the MachineOperandType for this operand.
Definition: MachineOperand.h:219
llvm::LanaiMCInstLower::LowerSymbolOperand
MCOperand LowerSymbolOperand(const MachineOperand &MO, MCSymbol *Sym) const
Definition: LanaiMCInstLower.cpp:66
llvm::AsmPrinter::getSymbol
MCSymbol * getSymbol(const GlobalValue *GV) const
Definition: AsmPrinter.cpp:478
llvm::LanaiMCInstLower::GetJumpTableSymbol
MCSymbol * GetJumpTableSymbol(const MachineOperand &MO) const
Definition: LanaiMCInstLower.cpp:47
llvm::MachineOperand::getReg
Register getReg() const
getReg - Returns the register number.
Definition: MachineOperand.h:360
llvm::MachineOperand::MO_JumpTableIndex
@ MO_JumpTableIndex
Address of indexed Jump Table for switch.
Definition: MachineOperand.h:60
llvm::MCBinaryExpr::createAdd
static const MCBinaryExpr * createAdd(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition: MCExpr.h:523
llvm::AsmPrinter::GetExternalSymbolSymbol
MCSymbol * GetExternalSymbolSymbol(StringRef Sym) const
Return the MCSymbol for the specified ExternalSymbol.
Definition: AsmPrinter.cpp:3145
llvm::MachineOperand::getMBB
MachineBasicBlock * getMBB() const
Definition: MachineOperand.h:552
MCAsmInfo.h
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:136
llvm::MCOperand::createReg
static MCOperand createReg(unsigned Reg)
Definition: MCInst.h:134
llvm::MachineOperand::MO_MachineBasicBlock
@ MO_MachineBasicBlock
MachineBasicBlock reference.
Definition: MachineOperand.h:56
llvm::LanaiMCInstLower::Lower
void Lower(const MachineInstr *MI, MCInst &OutMI) const
Definition: LanaiMCInstLower.cpp:93
llvm::LanaiMCExpr::VariantKind
VariantKind
Definition: LanaiMCExpr.h:19
llvm::GraphProgram::Name
Name
Definition: GraphWriter.h:52
llvm::LanaiMCExpr::VK_Lanai_None
@ VK_Lanai_None
Definition: LanaiMCExpr.h:19
llvm::MachineOperand::MO_ExternalSymbol
@ MO_ExternalSymbol
Name of external global symbol.
Definition: MachineOperand.h:61
llvm::MachineOperand::getIndex
int getIndex() const
Definition: MachineOperand.h:557
llvm::MCSymbolRefExpr::create
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx)
Definition: MCExpr.h:384
llvm::MachineOperand::getSymbolName
const char * getSymbolName() const
Definition: MachineOperand.h:608
llvm::LanaiMCInstLower::GetExternalSymbolSymbol
MCSymbol * GetExternalSymbolSymbol(const MachineOperand &MO) const
Definition: LanaiMCInstLower.cpp:43
llvm::raw_svector_ostream
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:647
LanaiMCExpr.h
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::MachineOperand::MO_RegisterMask
@ MO_RegisterMask
Mask of preserved registers.
Definition: MachineOperand.h:64
raw_ostream.h
MCExpr.h
llvm::AsmPrinter::getFunctionNumber
unsigned getFunctionNumber() const
Return a unique ID for the current function.
Definition: AsmPrinter.cpp:219
llvm::MCExpr
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:35
llvm::LanaiII::MO_ABS_HI
@ MO_ABS_HI
Definition: LanaiBaseInfo.h:35
llvm::LanaiMCExpr::VK_Lanai_ABS_LO
@ VK_Lanai_ABS_LO
Definition: LanaiMCExpr.h:19
llvm::MachineOperand::MO_ConstantPoolIndex
@ MO_ConstantPoolIndex
Address of indexed Constant in Constant Pool.
Definition: MachineOperand.h:58