Thanks to visit codestin.com
Credit goes to llvm.org

LLVM 22.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
22#include "llvm/MC/MCAsmInfo.h"
23#include "llvm/MC/MCContext.h"
24#include "llvm/MC/MCExpr.h"
25#include "llvm/MC/MCInst.h"
28
29using namespace llvm;
30
33 return Printer.getSymbol(MO.getGlobal());
34}
35
38 return Printer.GetBlockAddressSymbol(MO.getBlockAddress());
39}
40
43 return Printer.GetExternalSymbolSymbol(MO.getSymbolName());
44}
45
48 raw_svector_ostream(Name) << Printer.MAI->getPrivateGlobalPrefix() << "JTI"
49 << Printer.getFunctionNumber() << '_'
50 << MO.getIndex();
51 // Create a symbol for the name.
52 return Ctx.getOrCreateSymbol(Name.str());
53}
54
58 raw_svector_ostream(Name) << Printer.MAI->getPrivateGlobalPrefix() << "CPI"
59 << Printer.getFunctionNumber() << '_'
60 << MO.getIndex();
61 // Create a symbol for the name.
62 return Ctx.getOrCreateSymbol(Name.str());
63}
64
66 MCSymbol *Sym) const {
68 switch (MO.getTargetFlags()) {
70 Kind = Lanai::S_None;
71 break;
73 Kind = Lanai::S_ABS_HI;
74 break;
76 Kind = Lanai::S_ABS_LO;
77 break;
78 default:
79 llvm_unreachable("Unknown target flag on GV operand");
80 }
81
82 const MCExpr *Expr = MCSymbolRefExpr::create(Sym, Ctx);
83 if (!MO.isJTI() && MO.getOffset())
85 Expr, MCConstantExpr::create(MO.getOffset(), Ctx), Ctx);
86 Expr = MCSpecifierExpr::create(Expr, Kind, Ctx);
87 return MCOperand::createExpr(Expr);
88}
89
90void LanaiMCInstLower::Lower(const MachineInstr *MI, MCInst &OutMI) const {
91 OutMI.setOpcode(MI->getOpcode());
92
93 for (const MachineOperand &MO : MI->operands()) {
94 MCOperand MCOp;
95 switch (MO.getType()) {
97 // Ignore all implicit register operands.
98 if (MO.isImplicit())
99 continue;
100 MCOp = MCOperand::createReg(MO.getReg());
101 break;
103 MCOp = MCOperand::createImm(MO.getImm());
104 break;
107 MCSymbolRefExpr::create(MO.getMBB()->getSymbol(), Ctx));
108 break;
110 continue;
113 break;
116 break;
119 break;
122 break;
125 break;
126 default:
127 MI->print(errs());
128 llvm_unreachable("unknown operand type");
129 }
130
131 OutMI.addOperand(MCOp);
132 }
133}
IRTranslator LLVM IR MI
This file defines the SmallString class.
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
static const MCBinaryExpr * createAdd(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx, SMLoc Loc=SMLoc())
Definition MCExpr.h:343
static LLVM_ABI const MCConstantExpr * create(int64_t Value, MCContext &Ctx, bool PrintInHex=false, unsigned SizeInBytes=0)
Definition MCExpr.cpp:212
Base class for the full range of assembler expressions which are needed for parsing.
Definition MCExpr.h:34
Instances of this class represent a single low-level machine instruction.
Definition MCInst.h:188
void addOperand(const MCOperand Op)
Definition MCInst.h:215
void setOpcode(unsigned Op)
Definition MCInst.h:201
Instances of this class represent operands of the MCInst class.
Definition MCInst.h:40
static MCOperand createExpr(const MCExpr *Val)
Definition MCInst.h:166
static MCOperand createReg(MCRegister Reg)
Definition MCInst.h:138
static MCOperand createImm(int64_t Val)
Definition MCInst.h:145
static const MCSpecifierExpr * create(const MCExpr *Expr, Spec S, MCContext &Ctx, SMLoc Loc=SMLoc())
Definition MCExpr.cpp:743
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx, SMLoc Loc=SMLoc())
Definition MCExpr.h:214
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition MCSymbol.h:42
Representation of each machine instruction.
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.
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.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
uint8_t Specifier
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.