LLVM 19.0.0git
M68kMCInstLower.cpp
Go to the documentation of this file.
1//===-- M68kMCInstLower.cpp - M68k MachineInstr to MCInst -------*- C++ -*-===//
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/// \file
10/// This file contains code to lower M68k MachineInstrs to their
11/// corresponding MCInst records.
12///
13//===----------------------------------------------------------------------===//
14
15#include "M68kMCInstLower.h"
16
17#include "M68kAsmPrinter.h"
18#include "M68kInstrInfo.h"
19
21
25#include "llvm/IR/Mangler.h"
26#include "llvm/MC/MCContext.h"
27#include "llvm/MC/MCExpr.h"
28#include "llvm/MC/MCInst.h"
29
30using namespace llvm;
31
32#define DEBUG_TYPE "m68k-mc-inst-lower"
33
35 : Ctx(MF.getContext()), MF(MF), TM(MF.getTarget()), MAI(*TM.getMCAsmInfo()),
36 AsmPrinter(AP) {}
37
40 assert((MO.isGlobal() || MO.isSymbol() || MO.isMBB()) &&
41 "Isn't a symbol reference");
42
43 const auto &TT = TM.getTargetTriple();
44 if (MO.isGlobal() && TT.isOSBinFormatELF())
46
47 const DataLayout &DL = MF.getDataLayout();
48
49 MCSymbol *Sym = nullptr;
51 StringRef Suffix;
52
53 if (!Suffix.empty())
54 Name += DL.getPrivateGlobalPrefix();
55
56 if (MO.isGlobal()) {
57 const GlobalValue *GV = MO.getGlobal();
59 } else if (MO.isSymbol()) {
61 } else if (MO.isMBB()) {
62 assert(Suffix.empty());
63 Sym = MO.getMBB()->getSymbol();
64 }
65
66 Name += Suffix;
67 if (!Sym)
69
70 return Sym;
71}
72
74 MCSymbol *Sym) const {
75 // FIXME We would like an efficient form for this, so we don't have to do a
76 // lot of extra uniquing. This fixme is originally from X86
77 const MCExpr *Expr = nullptr;
79
80 switch (MO.getTargetFlags()) {
81 default:
82 llvm_unreachable("Unknown target flag on GV operand");
86 break;
89 break;
90 case M68kII::MO_GOT:
92 break;
95 break;
96 case M68kII::MO_PLT:
98 break;
101 break;
102 case M68kII::MO_TLSLD:
104 break;
107 break;
108 case M68kII::MO_TLSIE:
110 break;
111 case M68kII::MO_TLSLE:
113 break;
114 }
115
116 if (!Expr) {
117 Expr = MCSymbolRefExpr::create(Sym, RefKind, Ctx);
118 }
119
120 if (!MO.isJTI() && !MO.isMBB() && MO.getOffset()) {
122 Expr, MCConstantExpr::create(MO.getOffset(), Ctx), Ctx);
123 }
124
125 return MCOperand::createExpr(Expr);
126}
127
128std::optional<MCOperand>
130 const MachineOperand &MO) const {
131 switch (MO.getType()) {
132 default:
133 llvm_unreachable("unknown operand type");
135 // Ignore all implicit register operands.
136 if (MO.isImplicit())
137 return std::nullopt;
138 return MCOperand::createReg(MO.getReg());
140 return MCOperand::createImm(MO.getImm());
146 return LowerSymbolOperand(MO, MO.getMCSymbol());
152 return LowerSymbolOperand(
155 // Ignore call clobbers.
156 return std::nullopt;
157 }
158}
159
160void M68kMCInstLower::Lower(const MachineInstr *MI, MCInst &OutMI) const {
161 unsigned Opcode = MI->getOpcode();
162 OutMI.setOpcode(Opcode);
163
164 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
165 const MachineOperand &MO = MI->getOperand(i);
166 std::optional<MCOperand> MCOp = LowerOperand(MI, MO);
167
168 if (MCOp.has_value() && MCOp.value().isValid())
169 OutMI.addOperand(MCOp.value());
170 }
171
172 // TAILJMPj, TAILJMPq - Lower to the correct jump instructions.
173 if (Opcode == M68k::TAILJMPj || Opcode == M68k::TAILJMPq) {
174 assert(OutMI.getNumOperands() == 1 && "Unexpected number of operands");
175 switch (Opcode) {
176 case M68k::TAILJMPj:
177 Opcode = M68k::JMP32j;
178 break;
179 case M68k::TAILJMPq:
180 Opcode = M68k::BRA8;
181 break;
182 }
183 OutMI.setOpcode(Opcode);
184 }
185}
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
std::string Name
Symbol * Sym
Definition: ELF_riscv.cpp:479
IRTranslator LLVM IR MI
This file contains M68k assembler printer declarations.
This file contains small standalone helper functions and enum definitions for the M68k target useful ...
This file contains the M68k implementation of the TargetInstrInfo class.
This file contains code to lower M68k MachineInstrs to their corresponding MCInst records.
const char LLVMTargetMachineRef TM
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:84
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:704
void getNameWithPrefix(SmallVectorImpl< char > &Name, const GlobalValue *GV) const
Definition: AsmPrinter.cpp:695
MCSymbol * GetBlockAddressSymbol(const BlockAddress *BA) const
Return the MCSymbol used to satisfy BlockAddress uses of the specified basic block.
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:110
MCSymbol * GetSymbolFromOperand(const MachineOperand &MO) const
Lower an MO_GlobalAddress or MO_ExternalSymbol operand to an MCSymbol.
M68kMCInstLower(MachineFunction &MF, M68kAsmPrinter &AP)
void Lower(const MachineInstr *MI, MCInst &OutMI) const
MCOperand LowerSymbolOperand(const MachineOperand &MO, MCSymbol *Sym) const
std::optional< MCOperand > LowerOperand(const MachineInstr *MI, const MachineOperand &MO) const
static const MCBinaryExpr * createAdd(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition: MCExpr.h:536
static const MCConstantExpr * create(int64_t Value, MCContext &Ctx, bool PrintInHex=false, unsigned SizeInBytes=0)
Definition: MCExpr.cpp:194
MCSymbol * getOrCreateSymbol(const Twine &Name)
Lookup the symbol inside with the specified Name.
Definition: MCContext.cpp:201
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:35
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:184
unsigned getNumOperands() const
Definition: MCInst.h:208
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:397
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:40
MCSymbol * getSymbol() const
Return the MCSymbol for this basic block.
const DataLayout & getDataLayout() const
Return the DataLayout attached to the Module associated to this MF.
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 isSymbol() const
isSymbol - Tests if this is a MO_ExternalSymbol operand.
bool isJTI() const
isJTI - Tests if this is a MO_JumpTableIndex operand.
const BlockAddress * getBlockAddress() const
unsigned getTargetFlags() const
bool isGlobal() const
isGlobal - Tests if this is a MO_GlobalAddress operand.
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.
bool isMBB() const
isMBB - Tests if this is a MO_MachineBasicBlock operand.
void getNameWithPrefix(raw_ostream &OS, const GlobalValue *GV, bool CannotUsePrivateLabel) const
Print the appropriate prefix and the specified global variable's name.
Definition: Mangler.cpp:120
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition: SmallString.h:26
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
constexpr bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:134
const Triple & getTargetTriple() const
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ MO_GOTOFF
On a symbol operand this indicates that the immediate is the offset to the location of the symbol nam...
Definition: M68kBaseInfo.h:147
@ MO_TLSLDM
On a symbol operand, this indicates that the immediate is the offset to the slot in GOT which stores ...
Definition: M68kBaseInfo.h:177
@ MO_PLT
On a symbol operand this indicates that the immediate is offset to the PLT entry of symbol name from ...
Definition: M68kBaseInfo.h:159
@ MO_TLSLE
On a symbol operand, this indicates that the immediate is the offset to the variable within in the th...
Definition: M68kBaseInfo.h:189
@ MO_GOT
On a symbol operand this indicates that the immediate is the offset to the GOT entry for the symbol n...
Definition: M68kBaseInfo.h:141
@ MO_TLSGD
On a symbol operand, this indicates that the immediate is the offset to the slot in GOT which stores ...
Definition: M68kBaseInfo.h:165
@ MO_ABSOLUTE_ADDRESS
On a symbol operand this indicates that the immediate is the absolute address of the symbol.
Definition: M68kBaseInfo.h:131
@ MO_GOTPCREL
On a symbol operand this indicates that the immediate is offset to the GOT entry for the symbol name ...
Definition: M68kBaseInfo.h:153
@ MO_PC_RELATIVE_ADDRESS
On a symbol operand this indicates that the immediate is the pc-relative address of the symbol.
Definition: M68kBaseInfo.h:135
@ MO_TLSIE
On a symbol operand, this indicates that the immediate is the offset to the variable within the threa...
Definition: M68kBaseInfo.h:183
@ MO_TLSLD
On a symbol operand, this indicates that the immediate is the offset to variable within the thread lo...
Definition: M68kBaseInfo.h:171
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18