LLVM  13.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 
30 using 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 
38 MCSymbol *
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)
68  Sym = Ctx.getOrCreateSymbol(Name);
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");
83  case M68kII::MO_NO_FLAG:
86  break;
89  break;
90  case M68kII::MO_GOT:
91  RefKind = MCSymbolRefExpr::VK_GOT;
92  break;
93  case M68kII::MO_GOTOFF:
95  break;
96  case M68kII::MO_PLT:
97  RefKind = MCSymbolRefExpr::VK_PLT;
98  break;
99  }
100 
101  if (!Expr) {
102  Expr = MCSymbolRefExpr::create(Sym, RefKind, Ctx);
103  }
104 
105  if (!MO.isJTI() && !MO.isMBB() && MO.getOffset()) {
107  Expr, MCConstantExpr::create(MO.getOffset(), Ctx), Ctx);
108  }
109 
110  return MCOperand::createExpr(Expr);
111 }
112 
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 None;
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 None;
142  }
143 }
144 
145 void M68kMCInstLower::Lower(const MachineInstr *MI, MCInst &OutMI) const {
146  unsigned Opcode = MI->getOpcode();
147  OutMI.setOpcode(Opcode);
148 
149  for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
150  const MachineOperand &MO = MI->getOperand(i);
151  Optional<MCOperand> MCOp = LowerOperand(MI, MO);
152 
153  if (MCOp.hasValue() && MCOp.getValue().isValid())
154  OutMI.addOperand(MCOp.getValue());
155  }
156 
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  }
170 }
i
i
Definition: README.txt:29
llvm::MachineOperand::MO_BlockAddress
@ MO_BlockAddress
Address of a basic block.
Definition: MachineOperand.h:63
M68kMCInstLower.h
MI
IRTranslator LLVM IR MI
Definition: IRTranslator.cpp:100
MachineInstr.h
llvm::MachineOperand::MO_Immediate
@ MO_Immediate
Immediate operand.
Definition: MachineOperand.h:53
llvm
Definition: AllocatorList.h:23
llvm::MCSymbol
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
llvm::StringRef::empty
LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:156
llvm::SystemZISD::TM
@ TM
Definition: SystemZISelLowering.h:65
llvm::DataLayout
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:112
llvm::MCSymbolRefExpr::VK_GOT
@ VK_GOT
Definition: MCExpr.h:198
llvm::MCOperand::createExpr
static MCOperand createExpr(const MCExpr *Val)
Definition: MCInst.h:161
llvm::MachineOperand::getGlobal
const GlobalValue * getGlobal() const
Definition: MachineOperand.h:563
llvm::MCOperand::createImm
static MCOperand createImm(int64_t Val)
Definition: MCInst.h:140
llvm::M68kMCInstLower::M68kMCInstLower
M68kMCInstLower(MachineFunction &MF, M68kAsmPrinter &AP)
Definition: M68kMCInstLower.cpp:34
llvm::MCConstantExpr::create
static const MCConstantExpr * create(int64_t Value, MCContext &Ctx, bool PrintInHex=false, unsigned SizeInBytes=0)
Definition: MCExpr.cpp:194
llvm::MachineOperand::getBlockAddress
const BlockAddress * getBlockAddress() const
Definition: MachineOperand.h:568
llvm::M68kMCInstLower::GetSymbolFromOperand
MCSymbol * GetSymbolFromOperand(const MachineOperand &MO) const
Lower an MO_GlobalAddress or MO_ExternalSymbol operand to an MCSymbol.
Definition: M68kMCInstLower.cpp:39
llvm::AsmPrinter::getNameWithPrefix
void getNameWithPrefix(SmallVectorImpl< char > &Name, const GlobalValue *GV) const
Definition: AsmPrinter.cpp:473
llvm::MachineOperand::isSymbol
bool isSymbol() const
isSymbol - Tests if this is a MO_ExternalSymbol operand.
Definition: MachineOperand.h:341
llvm::MachineOperand::isJTI
bool isJTI() const
isJTI - Tests if this is a MO_JumpTableIndex operand.
Definition: MachineOperand.h:337
M68kAsmPrinter.h
llvm::M68kII::MO_PLT
@ MO_PLT
On a symbol operand this indicates that the immediate is offset to the PLT entry of symbol name from ...
Definition: M68kBaseInfo.h:114
llvm::Optional
Definition: APInt.h:33
llvm::M68kII::MO_GOT
@ 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:96
llvm::MCInst
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:183
llvm::MCSymbolRefExpr::VK_GOTOFF
@ VK_GOTOFF
Definition: MCExpr.h:199
llvm::M68kII::MO_PC_RELATIVE_ADDRESS
@ MO_PC_RELATIVE_ADDRESS
On a symbol operand this indicates that the immediate is the pc-relative address of the symbol.
Definition: M68kBaseInfo.h:90
llvm::MachineOperand::getOffset
int64_t getOffset() const
Return the offset from the symbol in this operand.
Definition: MachineOperand.h:600
llvm::MCInst::getNumOperands
unsigned getNumOperands() const
Definition: MCInst.h:207
llvm::M68kMCInstLower::Lower
void Lower(const MachineInstr *MI, MCInst &OutMI) const
Definition: M68kMCInstLower.cpp:145
llvm::MachineOperand::getMCSymbol
MCSymbol * getMCSymbol() const
Definition: MachineOperand.h:573
llvm::MachineOperand::MO_Register
@ MO_Register
Register operand.
Definition: MachineOperand.h:52
llvm::MCInst::setOpcode
void setOpcode(unsigned Op)
Definition: MCInst.h:196
M68kInstrInfo.h
llvm::Optional::hasValue
constexpr bool hasValue() const
Definition: Optional.h:286
llvm::MachineBasicBlock::getSymbol
MCSymbol * getSymbol() const
Return the MCSymbol for this basic block.
Definition: MachineBasicBlock.cpp:59
llvm::MCContext::getOrCreateSymbol
MCSymbol * getOrCreateSymbol(const Twine &Name)
Lookup the symbol inside with the specified Name.
Definition: MCContext.cpp:187
llvm::MachineOperand::isImplicit
bool isImplicit() const
Definition: MachineOperand.h:380
llvm::M68kII::MO_GOTOFF
@ MO_GOTOFF
On a symbol operand this indicates that the immediate is the offset to the location of the symbol nam...
Definition: M68kBaseInfo.h:102
llvm::MachineOperand::MO_GlobalAddress
@ MO_GlobalAddress
Address of a global value.
Definition: MachineOperand.h:62
llvm::MachineOperand::getImm
int64_t getImm() const
Definition: MachineOperand.h:537
llvm::M68kMCInstLower::LowerSymbolOperand
MCOperand LowerSymbolOperand(const MachineOperand &MO, MCSymbol *Sym) const
Definition: M68kMCInstLower.cpp:73
MCContext.h
MCInst.h
llvm::MachineOperand::isMBB
bool isMBB() const
isMBB - Tests if this is a MO_MachineBasicBlock operand.
Definition: MachineOperand.h:329
llvm::Mangler::getNameWithPrefix
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:114
llvm::MachineOperand
MachineOperand class - Representation of each machine instruction operand.
Definition: MachineOperand.h:49
llvm::M68kAsmPrinter
Definition: M68kAsmPrinter.h:37
llvm::None
const NoneType None
Definition: None.h:23
llvm::MCInst::addOperand
void addOperand(const MCOperand Op)
Definition: MCInst.h:209
llvm::SmallString< 128 >
llvm::M68kMCInstLower::LowerOperand
Optional< MCOperand > LowerOperand(const MachineInstr *MI, const MachineOperand &MO) const
Definition: M68kMCInstLower.cpp:114
llvm::GlobalValue
Definition: GlobalValue.h:44
llvm::MCSymbolRefExpr::VariantKind
VariantKind
Definition: MCExpr.h:194
llvm::MachineOperand::getTargetFlags
unsigned getTargetFlags() const
Definition: MachineOperand.h:221
llvm::AsmPrinter::GetCPISymbol
virtual MCSymbol * GetCPISymbol(unsigned CPID) const
Return the symbol for the specified constant pool entry.
Definition: AsmPrinter.cpp:3062
llvm::MachineInstr
Representation of each machine instruction.
Definition: MachineInstr.h:64
llvm::MCOperand::isValid
bool isValid() const
Definition: MCInst.h:59
llvm::AsmPrinter::GetBlockAddressSymbol
MCSymbol * GetBlockAddressSymbol(const BlockAddress *BA) const
Return the MCSymbol used to satisfy BlockAddress uses of the specified basic block.
Definition: AsmPrinter.cpp:3053
llvm::MCSymbolRefExpr::VK_PLT
@ VK_PLT
Definition: MCExpr.h:207
llvm::numbers::e
constexpr double e
Definition: MathExtras.h:57
llvm::AsmPrinter::getSymbolPreferLocal
MCSymbol * getSymbolPreferLocal(const GlobalValue &GV) const
Similar to getSymbol() but preferred for references.
Definition: AsmPrinter.cpp:482
llvm::MachineOperand::MO_MCSymbol
@ MO_MCSymbol
MCSymbol reference (for debug/eh info)
Definition: MachineOperand.h:67
llvm::MachineOperand::getType
MachineOperandType getType() const
getType - Returns the MachineOperandType for this operand.
Definition: MachineOperand.h:219
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::MachineOperand::getReg
Register getReg() const
getReg - Returns the register number.
Definition: MachineOperand.h:360
llvm::MachineFunction
Definition: MachineFunction.h:230
Mangler.h
llvm::MachineOperand::MO_JumpTableIndex
@ MO_JumpTableIndex
Address of indexed Jump Table for switch.
Definition: MachineOperand.h:60
llvm::MCBinaryExpr::createAdd
static const MCBinaryExpr * createAdd(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition: MCExpr.h:523
llvm::M68kII::MO_ABSOLUTE_ADDRESS
@ MO_ABSOLUTE_ADDRESS
On a symbol operand this indicates that the immediate is the absolute address of the symbol.
Definition: M68kBaseInfo.h:86
llvm::MachineOperand::getMBB
MachineBasicBlock * getMBB() const
Definition: MachineOperand.h:552
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:57
llvm::AsmPrinter::GetJTISymbol
MCSymbol * GetJTISymbol(unsigned JTID, bool isLinkerPrivate=false) const
Return the symbol for the specified jump table entry.
Definition: AsmPrinter.cpp:3090
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:136
llvm::MCOperand::createReg
static MCOperand createReg(unsigned Reg)
Definition: MCInst.h:133
llvm::MachineOperand::MO_MachineBasicBlock
@ MO_MachineBasicBlock
MachineBasicBlock reference.
Definition: MachineOperand.h:56
DL
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Definition: AArch64SLSHardening.cpp:76
llvm::MCSymbolRefExpr::VK_GOTPCREL
@ VK_GOTPCREL
Definition: MCExpr.h:202
llvm::GraphProgram::Name
Name
Definition: GraphWriter.h:52
llvm::MachineOperand::MO_ExternalSymbol
@ MO_ExternalSymbol
Name of external global symbol.
Definition: MachineOperand.h:61
llvm::MachineOperand::getIndex
int getIndex() const
Definition: MachineOperand.h:557
llvm::AsmPrinter
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:82
llvm::MCSymbolRefExpr::create
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx)
Definition: MCExpr.h:384
llvm::MachineOperand::getSymbolName
const char * getSymbolName() const
Definition: MachineOperand.h:608
llvm::TargetMachine::getTargetTriple
const Triple & getTargetTriple() const
Definition: TargetMachine.h:123
M68kBaseInfo.h
llvm::MachineFunction::getDataLayout
const DataLayout & getDataLayout() const
Return the DataLayout attached to the Module associated to this MF.
Definition: MachineFunction.cpp:260
MachineOperand.h
llvm::M68kII::MO_GOTPCREL
@ MO_GOTPCREL
On a symbol operand this indicates that the immediate is offset to the GOT entry for the symbol name ...
Definition: M68kBaseInfo.h:108
llvm::M68kII::MO_NO_FLAG
@ MO_NO_FLAG
Definition: M68kBaseInfo.h:82
llvm::MCOperand
Instances of this class represent operands of the MCInst class.
Definition: MCInst.h:35
llvm::MCSymbolRefExpr::VK_None
@ VK_None
Definition: MCExpr.h:195
llvm::MachineOperand::MO_RegisterMask
@ MO_RegisterMask
Mask of preserved registers.
Definition: MachineOperand.h:64
MachineFunction.h
MCExpr.h
llvm::MCExpr
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:35
llvm::Optional::getValue
constexpr const T & getValue() const LLVM_LVALUE_FUNCTION
Definition: Optional.h:280
llvm::MachineOperand::isGlobal
bool isGlobal() const
isGlobal - Tests if this is a MO_GlobalAddress operand.
Definition: MachineOperand.h:339
llvm::MachineOperand::MO_ConstantPoolIndex
@ MO_ConstantPoolIndex
Address of indexed Constant in Constant Pool.
Definition: MachineOperand.h:58