LLVM  10.0.0svn
XCoreMCInstLower.cpp
Go to the documentation of this file.
1 //===-- XCoreMCInstLower.cpp - Convert XCore 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 /// \file
10 /// This file contains code to lower XCore MachineInstrs to their
11 /// corresponding MCInst records.
12 ///
13 //===----------------------------------------------------------------------===//
14 #include "XCoreMCInstLower.h"
19 #include "llvm/IR/Mangler.h"
20 #include "llvm/MC/MCContext.h"
21 #include "llvm/MC/MCExpr.h"
22 #include "llvm/MC/MCInst.h"
23 
24 using namespace llvm;
25 
27  : Printer(asmprinter) {}
28 
30 
31 MCOperand XCoreMCInstLower::LowerSymbolOperand(const MachineOperand &MO,
32  MachineOperandType MOTy,
33  unsigned Offset) const {
35  const MCSymbol *Symbol;
36 
37  switch (MOTy) {
39  Symbol = MO.getMBB()->getSymbol();
40  break;
42  Symbol = Printer.getSymbol(MO.getGlobal());
43  Offset += MO.getOffset();
44  break;
46  Symbol = Printer.GetBlockAddressSymbol(MO.getBlockAddress());
47  Offset += MO.getOffset();
48  break;
50  Symbol = Printer.GetExternalSymbolSymbol(MO.getSymbolName());
51  Offset += MO.getOffset();
52  break;
54  Symbol = Printer.GetJTISymbol(MO.getIndex());
55  break;
57  Symbol = Printer.GetCPISymbol(MO.getIndex());
58  Offset += MO.getOffset();
59  break;
60  default:
61  llvm_unreachable("<unknown operand type>");
62  }
63 
64  const MCSymbolRefExpr *MCSym = MCSymbolRefExpr::create(Symbol, Kind, *Ctx);
65 
66  if (!Offset)
67  return MCOperand::createExpr(MCSym);
68 
69  // Assume offset is never negative.
70  assert(Offset > 0);
71 
72  const MCConstantExpr *OffsetExpr = MCConstantExpr::create(Offset, *Ctx);
73  const MCBinaryExpr *Add = MCBinaryExpr::createAdd(MCSym, OffsetExpr, *Ctx);
74  return MCOperand::createExpr(Add);
75 }
76 
78  unsigned offset) const {
79  MachineOperandType MOTy = MO.getType();
80 
81  switch (MOTy) {
82  default: llvm_unreachable("unknown operand type");
84  // Ignore all implicit register operands.
85  if (MO.isImplicit()) break;
86  return MCOperand::createReg(MO.getReg());
88  return MCOperand::createImm(MO.getImm() + offset);
95  return LowerSymbolOperand(MO, MOTy, offset);
97  break;
98  }
99 
100  return MCOperand();
101 }
102 
103 void XCoreMCInstLower::Lower(const MachineInstr *MI, MCInst &OutMI) const {
104  OutMI.setOpcode(MI->getOpcode());
105 
106  for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
107  const MachineOperand &MO = MI->getOperand(i);
108  MCOperand MCOp = LowerOperand(MO);
109 
110  if (MCOp.isValid())
111  OutMI.addOperand(MCOp);
112  }
113 }
uint64_t CallInst * C
MachineBasicBlock * getMBB() const
MCSymbol * GetExternalSymbolSymbol(StringRef Sym) const
Return the MCSymbol for the specified ExternalSymbol.
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx)
Definition: MCExpr.h:329
This class represents lattice values for constants.
Definition: AllocatorList.h:23
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
XCoreMCInstLower(class AsmPrinter &asmprinter)
static MCOperand createExpr(const MCExpr *Val)
Definition: MCInst.h:136
Address of indexed Jump Table for switch.
MachineBasicBlock reference.
print alias Alias Set Printer
Mask of preserved registers.
static MCOperand createReg(unsigned Reg)
Definition: MCInst.h:115
unsigned getNumOperands() const
Retuns the total number of operands.
Definition: MachineInstr.h:413
Name of external global symbol.
Represent a reference to a symbol from inside an expression.
Definition: MCExpr.h:169
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
Definition: MachineInstr.h:410
const char * getSymbolName() const
Context object for machine code objects.
Definition: MCContext.h:65
void Initialize(MCContext *C)
MCOperand LowerOperand(const MachineOperand &MO, unsigned offset=0) const
static const MCBinaryExpr * createAdd(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition: MCExpr.h:467
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:158
static const MCConstantExpr * create(int64_t Value, MCContext &Ctx, bool PrintInHex=false)
Definition: MCExpr.cpp:169
Address of a global value.
const GlobalValue * getGlobal() const
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:78
Address of a basic block.
constexpr double e
Definition: MathExtras.h:57
Binary assembler expressions.
Definition: MCExpr.h:423
virtual MCSymbol * GetCPISymbol(unsigned CPID) const
Return the symbol for the specified constant pool entry.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
void setOpcode(unsigned Op)
Definition: MCInst.h:170
MCSymbol * GetBlockAddressSymbol(const BlockAddress *BA) const
Return the MCSymbol used to satisfy BlockAddress uses of the specified basic block.
MCSymbol * getSymbol(const GlobalValue *GV) const
Definition: AsmPrinter.cpp:449
MachineOperand class - Representation of each machine instruction operand.
int64_t getImm() const
Representation of each machine instruction.
Definition: MachineInstr.h:63
MCSymbol * GetJTISymbol(unsigned JTID, bool isLinkerPrivate=false) const
Return the symbol for the specified jump table entry.
int64_t getOffset() const
Return the offset from the symbol in this operand.
const BlockAddress * getBlockAddress() const
MCSymbol * getSymbol() const
Return the MCSymbol for this basic block.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
IRTranslator LLVM IR MI
void addOperand(const MCOperand &Op)
Definition: MCInst.h:183
bool isValid() const
Definition: MCInst.h:56
Address of indexed Constant in Constant Pool.
Register getReg() const
getReg - Returns the register number.
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:415
Instances of this class represent operands of the MCInst class.
Definition: MCInst.h:34
MachineOperandType getType() const
getType - Returns the MachineOperandType for this operand.
static MCOperand createImm(int64_t Val)
Definition: MCInst.h:122
void Lower(const MachineInstr *MI, MCInst &OutMI) const
bool isImplicit() const