LLVM 17.0.0git
Go to the documentation of this file.
1//===-- M68kMCInstLower.cpp - M68k MachineInstr to MCInst -------*- C++ -*-===//
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
9/// \file
10/// This file contains code to lower M68k MachineInstrs to their
11/// corresponding MCInst records.
15#include "M68kMCInstLower.h"
17#include "M68kAsmPrinter.h"
18#include "M68kInstrInfo.h"
25#include "llvm/IR/Mangler.h"
26#include "llvm/MC/MCContext.h"
27#include "llvm/MC/MCExpr.h"
28#include "llvm/MC/MCInst.h"
30using namespace llvm;
32#define DEBUG_TYPE "m68k-mc-inst-lower"
35 : Ctx(MF.getContext()), MF(MF), TM(MF.getTarget()), MAI(*TM.getMCAsmInfo()),
36 AsmPrinter(AP) {}
40 assert((MO.isGlobal() || MO.isSymbol() || MO.isMBB()) &&
41 "Isn't a symbol reference");
43 const auto &TT = TM.getTargetTriple();
44 if (MO.isGlobal() && TT.isOSBinFormatELF())
47 const DataLayout &DL = MF.getDataLayout();
49 MCSymbol *Sym = nullptr;
51 StringRef Suffix;
53 if (!Suffix.empty())
54 Name += DL.getPrivateGlobalPrefix();
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 }
66 Name += Suffix;
67 if (!Sym)
68 Sym = Ctx.getOrCreateSymbol(Name);
70 return Sym;
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;
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;
99 }
101 if (!Expr) {
102 Expr = MCSymbolRefExpr::create(Sym, RefKind, Ctx);
103 }
105 if (!MO.isJTI() && !MO.isMBB() && MO.getOffset()) {
107 Expr, MCConstantExpr::create(MO.getOffset(), Ctx), Ctx);
108 }
110 return MCOperand::createExpr(Expr);
115 const MachineOperand &MO) const {
116 switch (MO.getType()) {
117 default:
118 llvm_unreachable("unknown operand type");
120 // Ignore all implicit register operands.
121 if (MO.isImplicit())
122 return std::nullopt;
123 return MCOperand::createReg(MO.getReg());
125 return MCOperand::createImm(MO.getImm());
131 return LowerSymbolOperand(MO, MO.getMCSymbol());
137 return LowerSymbolOperand(
140 // Ignore call clobbers.
141 return std::nullopt;
142 }
145void M68kMCInstLower::Lower(const MachineInstr *MI, MCInst &OutMI) const {
146 unsigned Opcode = MI->getOpcode();
147 OutMI.setOpcode(Opcode);
149 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
150 const MachineOperand &MO = MI->getOperand(i);
151 std::optional<MCOperand> MCOp = LowerOperand(MI, MO);
153 if (MCOp.has_value() && MCOp.value().isValid())
154 OutMI.addOperand(MCOp.value());
155 }
157 // TAILJMPj, TAILJMPq - Lower to the correct jump instructions.
158 if (Opcode == M68k::TAILJMPj || Opcode == M68k::TAILJMPq) {
159 assert(OutMI.getNumOperands() == 1 && "Unexpected number of operands");
160 switch (Opcode) {
161 case M68k::TAILJMPj:
162 Opcode = M68k::JMP32j;
163 break;
164 case M68k::TAILJMPq:
165 Opcode = M68k::BRA8;
166 break;
167 }
168 OutMI.setOpcode(Opcode);
169 }
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
std::string Name
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:666
void getNameWithPrefix(SmallVectorImpl< char > &Name, const GlobalValue *GV) const
Definition: AsmPrinter.cpp:657
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:525
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:386
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.
const DataLayout & getDataLayout() const
Return the DataLayout attached to the Module associated to this MF.
Representation of each machine instruction.
Definition: MachineInstr.h:68
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:119
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.
On a symbol operand this indicates that the immediate is the offset to the location of the symbol nam...
Definition: M68kBaseInfo.h:147
On a symbol operand this indicates that the immediate is offset to the PLT entry of symbol name from ...
Definition: M68kBaseInfo.h:159
On a symbol operand this indicates that the immediate is the offset to the GOT entry for the symbol n...
Definition: M68kBaseInfo.h:141
On a symbol operand this indicates that the immediate is the absolute address of the symbol.
Definition: M68kBaseInfo.h:131
On a symbol operand this indicates that the immediate is offset to the GOT entry for the symbol name ...
Definition: M68kBaseInfo.h:153
On a symbol operand this indicates that the immediate is the pc-relative address of the symbol.
Definition: M68kBaseInfo.h:135
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18