LLVM  3.7.0
ARMMCInstLower.cpp
Go to the documentation of this file.
1 //===-- ARMMCInstLower.cpp - Convert ARM MachineInstr to an MCInst --------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains code to lower ARM MachineInstrs to their corresponding
11 // MCInst records.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "ARM.h"
16 #include "ARMAsmPrinter.h"
18 #include "MCTargetDesc/ARMMCExpr.h"
20 #include "llvm/IR/Constants.h"
21 #include "llvm/IR/Mangler.h"
22 #include "llvm/MC/MCExpr.h"
23 #include "llvm/MC/MCInst.h"
24 using namespace llvm;
25 
26 
27 MCOperand ARMAsmPrinter::GetSymbolRef(const MachineOperand &MO,
28  const MCSymbol *Symbol) {
29  const MCExpr *Expr;
30  unsigned Option = MO.getTargetFlags() & ARMII::MO_OPTION_MASK;
31  switch (Option) {
32  default: {
34  OutContext);
35  switch (Option) {
36  default: llvm_unreachable("Unknown target flag on symbol operand");
37  case ARMII::MO_NO_FLAG:
38  break;
39  case ARMII::MO_LO16:
41  OutContext);
43  break;
44  case ARMII::MO_HI16:
46  OutContext);
48  break;
49  }
50  break;
51  }
52 
53  case ARMII::MO_PLT:
55  OutContext);
56  break;
57  }
58 
59  if (!MO.isJTI() && MO.getOffset())
60  Expr = MCBinaryExpr::createAdd(Expr,
62  OutContext),
63  OutContext);
64  return MCOperand::createExpr(Expr);
65 
66 }
67 
69  MCOperand &MCOp) {
70  switch (MO.getType()) {
71  default: llvm_unreachable("unknown operand type");
73  // Ignore all non-CPSR implicit register operands.
74  if (MO.isImplicit() && MO.getReg() != ARM::CPSR)
75  return false;
76  assert(!MO.getSubReg() && "Subregs should be eliminated!");
77  MCOp = MCOperand::createReg(MO.getReg());
78  break;
80  MCOp = MCOperand::createImm(MO.getImm());
81  break;
84  MO.getMBB()->getSymbol(), OutContext));
85  break;
87  MCOp = GetSymbolRef(MO,
88  GetARMGVSymbol(MO.getGlobal(), MO.getTargetFlags()));
89  break;
90  }
92  MCOp = GetSymbolRef(MO,
94  break;
96  MCOp = GetSymbolRef(MO, GetJTISymbol(MO.getIndex()));
97  break;
99  MCOp = GetSymbolRef(MO, GetCPISymbol(MO.getIndex()));
100  break;
102  MCOp = GetSymbolRef(MO, GetBlockAddressSymbol(MO.getBlockAddress()));
103  break;
105  APFloat Val = MO.getFPImm()->getValueAPF();
106  bool ignored;
109  break;
110  }
112  // Ignore call clobbers.
113  return false;
114  }
115  return true;
116 }
117 
119  ARMAsmPrinter &AP) {
120  OutMI.setOpcode(MI->getOpcode());
121 
122  // In the MC layer, we keep modified immediates in their encoded form
123  bool EncodeImms = false;
124  switch (MI->getOpcode()) {
125  default: break;
126  case ARM::MOVi:
127  case ARM::MVNi:
128  case ARM::CMPri:
129  case ARM::CMNri:
130  case ARM::TSTri:
131  case ARM::TEQri:
132  case ARM::MSRi:
133  case ARM::ADCri:
134  case ARM::ADDri:
135  case ARM::ADDSri:
136  case ARM::SBCri:
137  case ARM::SUBri:
138  case ARM::SUBSri:
139  case ARM::ANDri:
140  case ARM::ORRri:
141  case ARM::EORri:
142  case ARM::BICri:
143  case ARM::RSBri:
144  case ARM::RSBSri:
145  case ARM::RSCri:
146  EncodeImms = true;
147  break;
148  }
149 
150  for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
151  const MachineOperand &MO = MI->getOperand(i);
152 
153  MCOperand MCOp;
154  if (AP.lowerOperand(MO, MCOp)) {
155  if (MCOp.isImm() && EncodeImms) {
156  int32_t Enc = ARM_AM::getSOImmVal(MCOp.getImm());
157  if (Enc != -1)
158  MCOp.setImm(Enc);
159  }
160  OutMI.addOperand(MCOp);
161  }
162  }
163 }
bool isImplicit() const
const GlobalValue * getGlobal() const
bool lowerOperand(const MachineOperand &MO, MCOperand &MCOp)
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx)
Definition: MCExpr.h:315
static const fltSemantics IEEEdouble
Definition: APFloat.h:133
const ConstantFP * getFPImm() const
MachineBasicBlock * getMBB() const
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:39
MCContext & OutContext
This is the context for the output file that we are streaming.
Definition: AsmPrinter.h:78
static MCOperand createExpr(const MCExpr *Val)
Definition: MCInst.h:129
Address of indexed Jump Table for switch.
MachineBasicBlock reference.
const char * getSymbolName() const
MO_PLT - On a symbol operand, this represents an ELF PLT reference on a call operand.
Definition: ARMBaseInfo.h:294
Mask of preserved registers.
bool isJTI() const
isJTI - Tests if this is a MO_JumpTableIndex operand.
static MCOperand createReg(unsigned Reg)
Definition: MCInst.h:111
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:98
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:33
Name of external global symbol.
MCSymbol * GetJTISymbol(unsigned JTID, bool isLinkerPrivate=false) const
Return the symbol for the specified jump table entry.
unsigned getNumOperands() const
Access to explicit operands of the instruction.
Definition: MachineInstr.h:271
static const ARMMCExpr * createLower16(const MCExpr *Expr, MCContext &Ctx)
Definition: ARMMCExpr.h:43
double convertToDouble() const
Definition: APFloat.cpp:3116
int64_t getImm() const
static const MCBinaryExpr * createAdd(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition: MCExpr.h:446
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:150
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
Definition: MachineInstr.h:267
bool isImm() const
Definition: MCInst.h:57
A self-contained host- and target-independent arbitrary-precision floating-point software implementat...
Definition: APFloat.h:122
Address of a global value.
void setImm(int64_t Val)
Definition: MCInst.h:78
unsigned getTargetFlags() const
This file contains the declarations for the subclasses of Constant, which represent the different fla...
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:273
Address of a basic block.
opStatus convert(const fltSemantics &, roundingMode, bool *)
APFloat::convert - convert a value of one floating point type to another.
Definition: APFloat.cpp:1972
int64_t getOffset() const
Return the offset from the symbol in this operand.
unsigned getSubReg() const
MCSymbol * getSymbol() const
getSymbol - Return the MCSymbol for this basic block.
MO_LO16 - On a symbol operand, this represents a relocation containing lower 16 bit of the address...
Definition: ARMBaseInfo.h:286
void setOpcode(unsigned Op)
Definition: MCInst.h:158
MachineOperand class - Representation of each machine instruction operand.
static MCOperand createFPImm(double Val)
Definition: MCInst.h:123
void LowerARMMachineInstrToMCInst(const MachineInstr *MI, MCInst &OutMI, ARMAsmPrinter &AP)
int64_t getImm() const
Definition: MCInst.h:74
static int getSOImmVal(unsigned Arg)
getSOImmVal - Given a 32-bit immediate, if it is something that can fit into an shifter_operand immed...
Representation of each machine instruction.
Definition: MachineInstr.h:51
MachineOperandType getType() const
getType - Returns the MachineOperandType for this operand.
MCSymbol * GetBlockAddressSymbol(const BlockAddress *BA) const
Return the MCSymbol used to satisfy BlockAddress uses of the specified basic block.
static const ARMMCExpr * createUpper16(const MCExpr *Expr, MCContext &Ctx)
Definition: ARMMCExpr.h:39
MO_OPTION_MASK - Most flags are mutually exclusive; this mask selects just that part of the flag set...
Definition: ARMBaseInfo.h:298
virtual MCSymbol * GetCPISymbol(unsigned CPID) const
Return the symbol for the specified constant pool entry.
const APFloat & getValueAPF() const
Definition: Constants.h:270
unsigned getReg() const
getReg - Returns the register number.
Floating-point immediate operand.
void addOperand(const MCOperand &Op)
Definition: MCInst.h:168
const BlockAddress * getBlockAddress() const
Address of indexed Constant in Constant Pool.
Instances of this class represent operands of the MCInst class.
Definition: MCInst.h:33
MCSymbol * GetExternalSymbolSymbol(StringRef Sym) const
Return the MCSymbol for the specified ExternalSymbol.
static MCOperand createImm(int64_t Val)
Definition: MCInst.h:117
static const MCConstantExpr * create(int64_t Value, MCContext &Ctx)
Definition: MCExpr.cpp:150
MO_HI16 - On a symbol operand, this represents a relocation containing higher 16 bit of the address...
Definition: ARMBaseInfo.h:290