LLVM 20.0.0git
LoongArchMCInstLower.cpp
Go to the documentation of this file.
1//=- LoongArchMCInstLower.cpp - Convert LoongArch 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 LoongArch MachineInstrs to their
10// corresponding MCInst records.
11//
12//===----------------------------------------------------------------------===//
13
14#include "LoongArch.h"
15#include "LoongArchSubtarget.h"
21#include "llvm/MC/MCAsmInfo.h"
22#include "llvm/MC/MCContext.h"
24
25using namespace llvm;
26
28 const AsmPrinter &AP) {
29 MCContext &Ctx = AP.OutContext;
31
32 switch (MO.getTargetFlags()) {
33 default:
34 llvm_unreachable("Unknown target flag on GV operand");
37 break;
40 break;
43 break;
46 break;
49 break;
52 break;
55 break;
58 break;
61 break;
64 break;
67 break;
70 break;
73 break;
76 break;
79 break;
82 break;
85 break;
88 break;
91 break;
94 break;
97 break;
100 break;
103 break;
106 break;
109 break;
112 break;
115 break;
118 break;
119 // TODO: Handle more target-flags.
120 }
121
122 const MCExpr *ME =
124
125 if (!MO.isJTI() && !MO.isMBB() && MO.getOffset())
127 ME, MCConstantExpr::create(MO.getOffset(), Ctx), Ctx);
128
130 ME = LoongArchMCExpr::create(ME, Kind, Ctx);
131 return MCOperand::createExpr(ME);
132}
133
135 MCOperand &MCOp,
136 const AsmPrinter &AP) {
137 switch (MO.getType()) {
138 default:
140 "lowerLoongArchMachineOperandToMCOperand: unknown operand type");
142 // Ignore all implicit register operands.
143 if (MO.isImplicit())
144 return false;
145 MCOp = MCOperand::createReg(MO.getReg());
146 break;
148 // Regmasks are like implicit defs.
149 return false;
151 MCOp = MCOperand::createImm(MO.getImm());
152 break;
154 MCOp = lowerSymbolOperand(MO, AP.GetCPISymbol(MO.getIndex()), AP);
155 break;
157 MCOp = lowerSymbolOperand(MO, AP.getSymbolPreferLocal(*MO.getGlobal()), AP);
158 break;
160 MCOp = lowerSymbolOperand(MO, MO.getMBB()->getSymbol(), AP);
161 break;
163 MCOp = lowerSymbolOperand(
164 MO, AP.GetExternalSymbolSymbol(MO.getSymbolName()), AP);
165 break;
167 MCOp = lowerSymbolOperand(
168 MO, AP.GetBlockAddressSymbol(MO.getBlockAddress()), AP);
169 break;
171 MCOp = lowerSymbolOperand(MO, AP.GetJTISymbol(MO.getIndex()), AP);
172 break;
173 }
174 return true;
175}
176
178 MCInst &OutMI, AsmPrinter &AP) {
179 OutMI.setOpcode(MI->getOpcode());
180
181 for (const MachineOperand &MO : MI->operands()) {
182 MCOperand MCOp;
184 OutMI.addOperand(MCOp);
185 }
186 return false;
187}
Symbol * Sym
Definition: ELF_riscv.cpp:479
IRTranslator LLVM IR MI
static MCOperand lowerSymbolOperand(const MachineOperand &MO, MCSymbol *Sym, const AsmPrinter &AP)
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:86
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.
MCSymbol * getSymbolPreferLocal(const GlobalValue &GV) const
Similar to getSymbol() but preferred for references.
Definition: AsmPrinter.cpp:680
MCContext & OutContext
This is the context for the output file that we are streaming.
Definition: AsmPrinter.h:96
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 LoongArchMCExpr * create(const MCExpr *Expr, VariantKind Kind, MCContext &Ctx, bool Hint=false)
static const MCBinaryExpr * createAdd(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition: MCExpr.h:532
static const MCConstantExpr * create(int64_t Value, MCContext &Ctx, bool PrintInHex=false, unsigned SizeInBytes=0)
Definition: MCExpr.cpp:193
Context object for machine code objects.
Definition: MCContext.h:83
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: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:393
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
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
bool isJTI() const
isJTI - Tests if this is a MO_JumpTableIndex operand.
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.
@ 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.
bool isMBB() const
isMBB - Tests if this is a MO_MachineBasicBlock operand.
#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
bool lowerLoongArchMachineInstrToMCInst(const MachineInstr *MI, MCInst &OutMI, AsmPrinter &AP)
bool lowerLoongArchMachineOperandToMCOperand(const MachineOperand &MO, MCOperand &MCOp, const AsmPrinter &AP)
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:167