LLVM 20.0.0git
SparcMCInstLower.cpp
Go to the documentation of this file.
1//===-- SparcMCInstLower.cpp - Convert Sparc 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 Sparc MachineInstrs to their corresponding
10// MCInst records.
11//
12//===----------------------------------------------------------------------===//
13
15#include "Sparc.h"
20#include "llvm/IR/Mangler.h"
21#include "llvm/MC/MCAsmInfo.h"
22#include "llvm/MC/MCContext.h"
23#include "llvm/MC/MCExpr.h"
24#include "llvm/MC/MCInst.h"
25
26using namespace llvm;
27
28
30 const MachineOperand &MO,
31 AsmPrinter &AP) {
32
35 const MCSymbol *Symbol = nullptr;
36
37 switch(MO.getType()) {
38 default: llvm_unreachable("Unknown type in LowerSymbolOperand");
40 Symbol = MO.getMBB()->getSymbol();
41 break;
42
44 Symbol = AP.getSymbol(MO.getGlobal());
45 break;
46
48 Symbol = AP.GetBlockAddressSymbol(MO.getBlockAddress());
49 break;
50
52 Symbol = AP.GetExternalSymbolSymbol(MO.getSymbolName());
53 break;
54
56 Symbol = AP.GetCPISymbol(MO.getIndex());
57 break;
58 }
59
60 const MCSymbolRefExpr *MCSym = MCSymbolRefExpr::create(Symbol,
61 AP.OutContext);
62 const SparcMCExpr *expr = SparcMCExpr::create(Kind, MCSym,
63 AP.OutContext);
64 return MCOperand::createExpr(expr);
65}
66
68 const MachineOperand &MO,
69 AsmPrinter &AP) {
70 switch(MO.getType()) {
71 default: llvm_unreachable("unknown operand type"); break;
73 if (MO.isImplicit())
74 break;
75 return MCOperand::createReg(MO.getReg());
76
78 return MCOperand::createImm(MO.getImm());
79
85 return LowerSymbolOperand(MI, MO, AP);
86
88
89 }
90 return MCOperand();
91}
92
94 MCInst &OutMI,
95 AsmPrinter &AP)
96{
97
98 OutMI.setOpcode(MI->getOpcode());
99
100 for (const MachineOperand &MO : MI->operands()) {
101 MCOperand MCOp = LowerOperand(MI, MO, AP);
102
103 if (MCOp.isValid())
104 OutMI.addOperand(MCOp);
105 }
106}
IRTranslator LLVM IR MI
static MCOperand LowerSymbolOperand(const MachineInstr *MI, const MachineOperand &MO, AsmPrinter &AP)
static MCOperand LowerOperand(const MachineInstr *MI, const MachineOperand &MO, AsmPrinter &AP)
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:86
MCSymbol * getSymbol(const GlobalValue *GV) const
Definition: AsmPrinter.cpp:676
virtual MCSymbol * GetCPISymbol(unsigned CPID) const
Return the symbol for the specified constant pool entry.
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.
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
bool isValid() const
Definition: MCInst.h:60
Represent a reference to a symbol from inside an expression.
Definition: MCExpr.h:188
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
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.
static const SparcMCExpr * create(VariantKind Kind, const MCExpr *Expr, MCContext &Ctx)
Definition: SparcMCExpr.cpp:27
#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
void LowerSparcMachineInstrToMCInst(const MachineInstr *MI, MCInst &OutMI, AsmPrinter &AP)