LLVM 22.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
22
26#include "llvm/IR/Mangler.h"
27#include "llvm/MC/MCContext.h"
28#include "llvm/MC/MCExpr.h"
29#include "llvm/MC/MCInst.h"
30
31using namespace llvm;
32
33#define DEBUG_TYPE "m68k-mc-inst-lower"
34
36 : Ctx(AP.OutContext), MF(MF), TM(MF.getTarget()), MAI(*TM.getMCAsmInfo()),
37 AsmPrinter(AP) {}
38
41 assert((MO.isGlobal() || MO.isSymbol() || MO.isMBB()) &&
42 "Isn't a symbol reference");
43
44 const auto &TT = TM.getTargetTriple();
45 if (MO.isGlobal() && TT.isOSBinFormatELF())
47
48 const DataLayout &DL = MF.getDataLayout();
49
50 MCSymbol *Sym = nullptr;
52 StringRef Suffix;
53
54 if (!Suffix.empty())
55 Name += DL.getPrivateGlobalPrefix();
56
57 if (MO.isGlobal()) {
58 const GlobalValue *GV = MO.getGlobal();
60 } else if (MO.isSymbol()) {
62 } else if (MO.isMBB()) {
63 assert(Suffix.empty());
64 Sym = MO.getMBB()->getSymbol();
65 }
66
67 Name += Suffix;
68 if (!Sym)
70
71 return Sym;
72}
73
75 MCSymbol *Sym) const {
76 // FIXME We would like an efficient form for this, so we don't have to do a
77 // lot of extra uniquing. This fixme is originally from X86
78 const MCExpr *Expr = nullptr;
80
81 switch (MO.getTargetFlags()) {
82 default:
83 llvm_unreachable("Unknown target flag on GV operand");
87 break;
89 RefKind = M68k::S_GOTPCREL;
90 break;
91 case M68kII::MO_GOT:
92 RefKind = M68k::S_GOT;
93 break;
95 RefKind = M68k::S_GOTOFF;
96 break;
97 case M68kII::MO_PLT:
98 RefKind = M68k::S_PLT;
99 break;
100 case M68kII::MO_TLSGD:
101 RefKind = M68k::S_TLSGD;
102 break;
103 case M68kII::MO_TLSLD:
104 RefKind = M68k::S_TLSLD;
105 break;
107 RefKind = M68k::S_TLSLDM;
108 break;
109 case M68kII::MO_TLSIE:
110 RefKind = M68k::S_GOTTPOFF;
111 break;
112 case M68kII::MO_TLSLE:
113 RefKind = M68k::S_TPOFF;
114 break;
115 }
116
117 if (!Expr) {
118 Expr = MCSymbolRefExpr::create(Sym, RefKind, Ctx);
119 }
120
121 if (!MO.isJTI() && !MO.isMBB() && MO.getOffset()) {
123 Expr, MCConstantExpr::create(MO.getOffset(), Ctx), Ctx);
124 }
125
126 return MCOperand::createExpr(Expr);
127}
128
129std::optional<MCOperand>
131 const MachineOperand &MO) const {
132 switch (MO.getType()) {
133 default:
134 llvm_unreachable("unknown operand type");
136 // Ignore all implicit register operands.
137 if (MO.isImplicit())
138 return std::nullopt;
139 return MCOperand::createReg(MO.getReg());
141 return MCOperand::createImm(MO.getImm());
147 return LowerSymbolOperand(MO, MO.getMCSymbol());
153 return LowerSymbolOperand(
156 // Ignore call clobbers.
157 return std::nullopt;
158 }
159}
160
161void M68kMCInstLower::Lower(const MachineInstr *MI, MCInst &OutMI) const {
162 unsigned Opcode = MI->getOpcode();
163 OutMI.setOpcode(Opcode);
164
165 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
166 const MachineOperand &MO = MI->getOperand(i);
167 std::optional<MCOperand> MCOp = LowerOperand(MI, MO);
168
169 if (MCOp.has_value() && MCOp.value().isValid())
170 OutMI.addOperand(MCOp.value());
171 }
172
173 // TAILJMPj, TAILJMPq - Lower to the correct jump instructions.
174 if (Opcode == M68k::TAILJMPj || Opcode == M68k::TAILJMPq) {
175 assert(OutMI.getNumOperands() == 1 && "Unexpected number of operands");
176 switch (Opcode) {
177 case M68k::TAILJMPj:
178 Opcode = M68k::JMP32j;
179 break;
180 case M68k::TAILJMPq:
181 Opcode = M68k::BRA8;
182 break;
183 }
184 OutMI.setOpcode(Opcode);
185 }
186}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
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 the declarations of the M68k MCAsmInfo properties.
This file contains code to lower M68k MachineInstrs to their corresponding MCInst records.
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.
MCSymbol * getSymbolPreferLocal(const GlobalValue &GV) const
Similar to getSymbol() but preferred for references.
Definition: AsmPrinter.cpp:710
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.
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:63
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, 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
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
unsigned getNumOperands() const
Definition: MCInst.h:212
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 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.
const DataLayout & getDataLayout() const
Return the DataLayout attached to the Module associated to this MF.
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
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.
LLVM_ABI 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:121
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:55
constexpr bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:151
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