LLVM  6.0.0svn
MSP430MCInstLower.cpp
Go to the documentation of this file.
1 //===-- MSP430MCInstLower.cpp - Convert MSP430 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 MSP430 MachineInstrs to their corresponding
11 // MCInst records.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "MSP430MCInstLower.h"
16 #include "llvm/ADT/SmallString.h"
20 #include "llvm/IR/DataLayout.h"
21 #include "llvm/IR/Mangler.h"
22 #include "llvm/MC/MCAsmInfo.h"
23 #include "llvm/MC/MCContext.h"
24 #include "llvm/MC/MCExpr.h"
25 #include "llvm/MC/MCInst.h"
29 using namespace llvm;
30 
33  switch (MO.getTargetFlags()) {
34  default: llvm_unreachable("Unknown target flag on GV operand");
35  case 0: break;
36  }
37 
38  return Printer.getSymbol(MO.getGlobal());
39 }
40 
43  switch (MO.getTargetFlags()) {
44  default: llvm_unreachable("Unknown target flag on GV operand");
45  case 0: break;
46  }
47 
48  return Printer.GetExternalSymbolSymbol(MO.getSymbolName());
49 }
50 
53  const DataLayout &DL = Printer.getDataLayout();
55  raw_svector_ostream(Name) << DL.getPrivateGlobalPrefix() << "JTI"
56  << Printer.getFunctionNumber() << '_'
57  << MO.getIndex();
58 
59  switch (MO.getTargetFlags()) {
60  default: llvm_unreachable("Unknown target flag on GV operand");
61  case 0: break;
62  }
63 
64  // Create a symbol for the name.
65  return Ctx.getOrCreateSymbol(Name);
66 }
67 
70  const DataLayout &DL = Printer.getDataLayout();
72  raw_svector_ostream(Name) << DL.getPrivateGlobalPrefix() << "CPI"
73  << Printer.getFunctionNumber() << '_'
74  << MO.getIndex();
75 
76  switch (MO.getTargetFlags()) {
77  default: llvm_unreachable("Unknown target flag on GV operand");
78  case 0: break;
79  }
80 
81  // Create a symbol for the name.
82  return Ctx.getOrCreateSymbol(Name);
83 }
84 
87  switch (MO.getTargetFlags()) {
88  default: llvm_unreachable("Unknown target flag on GV operand");
89  case 0: break;
90  }
91 
92  return Printer.GetBlockAddressSymbol(MO.getBlockAddress());
93 }
94 
96 LowerSymbolOperand(const MachineOperand &MO, MCSymbol *Sym) const {
97  // FIXME: We would like an efficient form for this, so we don't have to do a
98  // lot of extra uniquing.
99  const MCExpr *Expr = MCSymbolRefExpr::create(Sym, Ctx);
100 
101  switch (MO.getTargetFlags()) {
102  default: llvm_unreachable("Unknown target flag on GV operand");
103  case 0: break;
104  }
105 
106  if (!MO.isJTI() && MO.getOffset())
107  Expr = MCBinaryExpr::createAdd(Expr,
109  Ctx);
110  return MCOperand::createExpr(Expr);
111 }
112 
113 void MSP430MCInstLower::Lower(const MachineInstr *MI, MCInst &OutMI) const {
114  OutMI.setOpcode(MI->getOpcode());
115 
116  for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
117  const MachineOperand &MO = MI->getOperand(i);
118 
119  MCOperand MCOp;
120  switch (MO.getType()) {
121  default:
122  MI->print(errs());
123  llvm_unreachable("unknown operand type");
125  // Ignore all implicit register operands.
126  if (MO.isImplicit()) continue;
127  MCOp = MCOperand::createReg(MO.getReg());
128  break;
130  MCOp = MCOperand::createImm(MO.getImm());
131  break;
134  MO.getMBB()->getSymbol(), Ctx));
135  break;
138  break;
141  break;
143  MCOp = LowerSymbolOperand(MO, GetJumpTableSymbol(MO));
144  break;
147  break;
150  break;
152  continue;
153  }
154 
155  OutMI.addOperand(MCOp);
156  }
157 }
unsigned getTargetFlags() const
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:109
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
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:305
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
StringRef getPrivateGlobalPrefix() const
Definition: DataLayout.h:281
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:42
MCSymbol * GetConstantPoolIndexSymbol(const MachineOperand &MO) const
static MCOperand createExpr(const MCExpr *Val)
Definition: MCInst.h:137
unsigned getReg() const
getReg - Returns the register number.
Address of indexed Jump Table for switch.
MCSymbol * GetBlockAddressSymbol(const MachineOperand &MO) const
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:493
MachineBasicBlock reference.
MCSymbol * GetGlobalAddressSymbol(const MachineOperand &MO) const
Mask of preserved registers.
static MCOperand createReg(unsigned Reg)
Definition: MCInst.h:116
unsigned getNumOperands() const
Access to explicit operands of the instruction.
Definition: MachineInstr.h:282
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:36
Name of external global symbol.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
Definition: MachineInstr.h:279
const char * getSymbolName() const
MCSymbol * GetExternalSymbolSymbol(const MachineOperand &MO) const
static const MCBinaryExpr * createAdd(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition: MCExpr.h:443
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:159
Address of a global value.
const GlobalValue * getGlobal() const
Address of a basic block.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
unsigned getFunctionNumber() const
Return a unique ID for the current function.
Definition: AsmPrinter.cpp:189
void setOpcode(unsigned Op)
Definition: MCInst.h:167
MCSymbol * GetBlockAddressSymbol(const BlockAddress *BA) const
Return the MCSymbol used to satisfy BlockAddress uses of the specified basic block.
bool isJTI() const
isJTI - Tests if this is a MO_JumpTableIndex operand.
MCSymbol * getSymbol(const GlobalValue *GV) const
Definition: AsmPrinter.cpp:422
MachineOperand class - Representation of each machine instruction operand.
int64_t getImm() const
void Lower(const MachineInstr *MI, MCInst &OutMI) const
Representation of each machine instruction.
Definition: MachineInstr.h:59
MCSymbol * getOrCreateSymbol(const Twine &Name)
Lookup the symbol inside with the specified Name.
Definition: MCContext.cpp:121
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.
void print(raw_ostream &OS, bool SkipOpers=false, bool SkipDebugLoc=false, const TargetInstrInfo *TII=nullptr) const
Debugging supportPrint this MI to OS.
MCSymbol * GetJumpTableSymbol(const MachineOperand &MO) const
MCOperand LowerSymbolOperand(const MachineOperand &MO, MCSymbol *Sym) const
const DataLayout & getDataLayout() const
Return information about data layout.
Definition: AsmPrinter.cpp:197
IRTranslator LLVM IR MI
void addOperand(const MCOperand &Op)
Definition: MCInst.h:177
Address of indexed Constant in Constant Pool.
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:284
Instances of this class represent operands of the MCInst class.
Definition: MCInst.h:35
MachineOperandType getType() const
getType - Returns the MachineOperandType for this operand.
static MCOperand createImm(int64_t Val)
Definition: MCInst.h:123
static const MCConstantExpr * create(int64_t Value, MCContext &Ctx)
Definition: MCExpr.cpp:159
bool isImplicit() const