LLVM 22.0.0git
MipsMCInstLower.cpp
Go to the documentation of this file.
1//===- MipsMCInstLower.cpp - Convert Mips MachineInstr to 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 Mips MachineInstrs to their corresponding
10// MCInst records.
11//
12//===----------------------------------------------------------------------===//
13
14#include "MipsMCInstLower.h"
17#include "MipsAsmPrinter.h"
21#include "llvm/MC/MCContext.h"
22#include "llvm/MC/MCExpr.h"
23#include "llvm/MC/MCInst.h"
25
26using namespace llvm;
27
29 : AsmPrinter(asmprinter) {}
30
32 Ctx = C;
33}
34
35MCOperand MipsMCInstLower::LowerSymbolOperand(const MachineOperand &MO,
36 MachineOperandType MOTy,
37 int64_t Offset) const {
38 Mips::Specifier TargetKind = Mips::S_None;
39 bool IsGpOff = false;
40 const MCSymbol *Symbol;
42 unsigned TargetFlags = MO.getTargetFlags();
43
44 if (TargetFlags & MipsII::MO_DLLIMPORT) {
45 // Handle dllimport linkage
46 Name += "__imp_";
47 TargetFlags &= ~MipsII::MO_DLLIMPORT;
48 }
49
50 switch (TargetFlags) {
51 default:
52 llvm_unreachable("Invalid target flag!");
54 break;
56 TargetKind = Mips::S_GPREL;
57 break;
59 TargetKind = Mips::S_GOT_CALL;
60 break;
61 case MipsII::MO_GOT:
62 TargetKind = Mips::S_GOT;
63 break;
65 TargetKind = Mips::S_HI;
66 break;
68 TargetKind = Mips::S_LO;
69 break;
71 TargetKind = Mips::S_TLSGD;
72 break;
74 TargetKind = Mips::S_TLSLDM;
75 break;
77 TargetKind = Mips::S_DTPREL_HI;
78 break;
80 TargetKind = Mips::S_DTPREL_LO;
81 break;
83 TargetKind = Mips::S_GOTTPREL;
84 break;
86 TargetKind = Mips::S_TPREL_HI;
87 break;
89 TargetKind = Mips::S_TPREL_LO;
90 break;
92 TargetKind = Mips::S_HI;
93 IsGpOff = true;
94 break;
96 TargetKind = Mips::S_LO;
97 IsGpOff = true;
98 break;
100 TargetKind = Mips::S_GOT_DISP;
101 break;
103 TargetKind = Mips::S_GOT_HI16;
104 break;
106 TargetKind = Mips::S_GOT_LO16;
107 break;
109 TargetKind = Mips::S_GOT_PAGE;
110 break;
112 TargetKind = Mips::S_GOT_OFST;
113 break;
115 TargetKind = Mips::S_HIGHER;
116 break;
118 TargetKind = Mips::S_HIGHEST;
119 break;
121 TargetKind = Mips::S_CALL_HI16;
122 break;
124 TargetKind = Mips::S_CALL_LO16;
125 break;
126 case MipsII::MO_JALR:
127 return MCOperand();
128 }
129
130 switch (MOTy) {
132 Symbol = MO.getMBB()->getSymbol();
133 break;
134
138 Offset += MO.getOffset();
139 break;
140
143 Offset += MO.getOffset();
144 break;
145
148 Offset += MO.getOffset();
149 break;
150
152 Symbol = MO.getMCSymbol();
153 Offset += MO.getOffset();
154 break;
155
158 break;
159
162 Offset += MO.getOffset();
163 break;
164
165 default:
166 llvm_unreachable("<unknown operand type>");
167 }
168
169 const MCExpr *Expr = MCSymbolRefExpr::create(Symbol, *Ctx);
170
171 if (Offset) {
172 // Note: Offset can also be negative
174 *Ctx);
175 }
176
177 if (IsGpOff)
178 Expr = Mips::createGpOff(Expr, TargetKind, *Ctx);
179 else if (TargetKind != Mips::S_None)
180 Expr = MCSpecifierExpr::create(Expr, TargetKind, *Ctx);
181
182 return MCOperand::createExpr(Expr);
183}
184
186 int64_t offset) const {
187 MachineOperandType MOTy = MO.getType();
188
189 switch (MOTy) {
190 default: llvm_unreachable("unknown operand type");
192 // Ignore all implicit register operands.
193 if (MO.isImplicit()) break;
194 return MCOperand::createReg(MO.getReg());
196 return MCOperand::createImm(MO.getImm() + offset);
204 return LowerSymbolOperand(MO, MOTy, offset);
206 break;
207 }
208
209 return MCOperand();
210}
211
212MCOperand MipsMCInstLower::createSub(MachineBasicBlock *BB1,
214 Mips::Specifier Kind) const {
215 const MCSymbolRefExpr *Sym1 = MCSymbolRefExpr::create(BB1->getSymbol(), *Ctx);
216 const MCSymbolRefExpr *Sym2 = MCSymbolRefExpr::create(BB2->getSymbol(), *Ctx);
217 const MCBinaryExpr *Sub = MCBinaryExpr::createSub(Sym1, Sym2, *Ctx);
218
220}
221
222void MipsMCInstLower::
223lowerLongBranchLUi(const MachineInstr *MI, MCInst &OutMI) const {
224 OutMI.setOpcode(Mips::LUi);
225
226 // Lower register operand.
227 OutMI.addOperand(LowerOperand(MI->getOperand(0)));
228
230 unsigned TargetFlags = MI->getOperand(1).getTargetFlags();
231 switch (TargetFlags) {
234 break;
237 break;
240 break;
243 break;
244 default:
245 report_fatal_error("Unexpected flags for lowerLongBranchLUi");
246 }
247
248 if (MI->getNumOperands() == 2) {
249 const MCExpr *Expr =
250 MCSymbolRefExpr::create(MI->getOperand(1).getMBB()->getSymbol(), *Ctx);
251 const auto *MipsExpr = MCSpecifierExpr::create(Expr, Spec, *Ctx);
252 OutMI.addOperand(MCOperand::createExpr(MipsExpr));
253 } else if (MI->getNumOperands() == 3) {
254 // Create %hi($tgt-$baltgt).
255 OutMI.addOperand(createSub(MI->getOperand(1).getMBB(),
256 MI->getOperand(2).getMBB(), Spec));
257 }
258}
259
260void MipsMCInstLower::lowerLongBranchADDiu(const MachineInstr *MI,
261 MCInst &OutMI, int Opcode) const {
262 OutMI.setOpcode(Opcode);
263
265 unsigned TargetFlags = MI->getOperand(2).getTargetFlags();
266 switch (TargetFlags) {
269 break;
272 break;
275 break;
278 break;
279 default:
280 report_fatal_error("Unexpected flags for lowerLongBranchADDiu");
281 }
282
283 // Lower two register operands.
284 for (unsigned I = 0, E = 2; I != E; ++I) {
285 const MachineOperand &MO = MI->getOperand(I);
286 OutMI.addOperand(LowerOperand(MO));
287 }
288
289 if (MI->getNumOperands() == 3) {
290 // Lower register operand.
291 const MCExpr *Expr =
292 MCSymbolRefExpr::create(MI->getOperand(2).getMBB()->getSymbol(), *Ctx);
293 const auto *MipsExpr = MCSpecifierExpr::create(Expr, Spec, *Ctx);
294 OutMI.addOperand(MCOperand::createExpr(MipsExpr));
295 } else if (MI->getNumOperands() == 4) {
296 // Create %lo($tgt-$baltgt) or %hi($tgt-$baltgt).
297 OutMI.addOperand(createSub(MI->getOperand(2).getMBB(),
298 MI->getOperand(3).getMBB(), Spec));
299 }
300}
301
302bool MipsMCInstLower::lowerLongBranch(const MachineInstr *MI,
303 MCInst &OutMI) const {
304 switch (MI->getOpcode()) {
305 default:
306 return false;
307 case Mips::LONG_BRANCH_LUi:
308 case Mips::LONG_BRANCH_LUi2Op:
309 case Mips::LONG_BRANCH_LUi2Op_64:
310 lowerLongBranchLUi(MI, OutMI);
311 return true;
312 case Mips::LONG_BRANCH_ADDiu:
313 case Mips::LONG_BRANCH_ADDiu2Op:
314 lowerLongBranchADDiu(MI, OutMI, Mips::ADDiu);
315 return true;
316 case Mips::LONG_BRANCH_DADDiu:
317 case Mips::LONG_BRANCH_DADDiu2Op:
318 lowerLongBranchADDiu(MI, OutMI, Mips::DADDiu);
319 return true;
320 }
321}
322
323void MipsMCInstLower::Lower(const MachineInstr *MI, MCInst &OutMI) const {
324 if (lowerLongBranch(MI, OutMI))
325 return;
326
327 OutMI.setOpcode(MI->getOpcode());
328
329 for (const MachineOperand &MO : MI->operands()) {
330 MCOperand MCOp = LowerOperand(MO);
331
332 if (MCOp.isValid())
333 OutMI.addOperand(MCOp);
334 }
335}
std::string Name
IRTranslator LLVM IR MI
#define I(x, y, z)
Definition: MD5.cpp:58
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:90
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.
void getNameWithPrefix(SmallVectorImpl< char > &Name, const GlobalValue *GV) const
Definition: AsmPrinter.cpp:701
MCSymbol * GetBlockAddressSymbol(const BlockAddress *BA) const
Return the MCSymbol used to satisfy BlockAddress uses of the specified basic block.
MCSymbol * GetExternalSymbolSymbol(const Twine &Sym) const
Return the MCSymbol for the specified ExternalSymbol.
Binary assembler expressions.
Definition: MCExpr.h:299
static const MCBinaryExpr * createAdd(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx, SMLoc Loc=SMLoc())
Definition: MCExpr.h:343
static const MCBinaryExpr * createSub(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition: MCExpr.h:428
static LLVM_ABI const MCConstantExpr * create(int64_t Value, MCContext &Ctx, bool PrintInHex=false, unsigned SizeInBytes=0)
Definition: MCExpr.cpp:212
Context object for machine code objects.
Definition: MCContext.h:83
LLVM_ABI MCSymbol * getOrCreateSymbol(const Twine &Name)
Lookup the symbol inside with the specified Name.
Definition: MCContext.cpp:203
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
bool isValid() const
Definition: MCInst.h:64
static const MCSpecifierExpr * create(const MCExpr *Expr, Spec S, MCContext &Ctx, SMLoc Loc=SMLoc())
Definition: MCExpr.cpp:743
Represent a reference to a symbol from inside an expression.
Definition: MCExpr.h:190
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
LLVM_ABI MCSymbol * getSymbol() const
Return the MCSymbol for this basic block.
Representation of each machine instruction.
Definition: MachineInstr.h:72
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.
int64_t getOffset() const
Return the offset from the symbol in this operand.
void Lower(const MachineInstr *MI, MCInst &OutMI) const
MCOperand LowerOperand(const MachineOperand &MO, int64_t offset=0) const
void Initialize(MCContext *C)
MipsMCInstLower(MipsAsmPrinter &asmprinter)
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition: SmallString.h:26
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
const MCSpecifierExpr * createGpOff(const MCExpr *Expr, Specifier S, MCContext &Ctx)
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:477
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition: Error.cpp:167
@ Sub
Subtraction of integers.