LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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/MC/MCAsmInfo.h"
21 #include "llvm/MC/MCContext.h"
22 #include "llvm/MC/MCExpr.h"
23 #include "llvm/MC/MCInst.h"
26 #include "llvm/Target/Mangler.h"
27 using namespace llvm;
28 
31  switch (MO.getTargetFlags()) {
32  default: llvm_unreachable("Unknown target flag on GV operand");
33  case 0: break;
34  }
35 
36  return Printer.getSymbol(MO.getGlobal());
37 }
38 
41  switch (MO.getTargetFlags()) {
42  default: llvm_unreachable("Unknown target flag on GV operand");
43  case 0: break;
44  }
45 
46  return Printer.GetExternalSymbolSymbol(MO.getSymbolName());
47 }
48 
52  raw_svector_ostream(Name) << Printer.MAI->getPrivateGlobalPrefix() << "JTI"
53  << Printer.getFunctionNumber() << '_'
54  << MO.getIndex();
55 
56  switch (MO.getTargetFlags()) {
57  default: llvm_unreachable("Unknown target flag on GV operand");
58  case 0: break;
59  }
60 
61  // Create a symbol for the name.
62  return Ctx.GetOrCreateSymbol(Name.str());
63 }
64 
68  raw_svector_ostream(Name) << Printer.MAI->getPrivateGlobalPrefix() << "CPI"
69  << Printer.getFunctionNumber() << '_'
70  << MO.getIndex();
71 
72  switch (MO.getTargetFlags()) {
73  default: llvm_unreachable("Unknown target flag on GV operand");
74  case 0: break;
75  }
76 
77  // Create a symbol for the name.
78  return Ctx.GetOrCreateSymbol(Name.str());
79 }
80 
83  switch (MO.getTargetFlags()) {
84  default: llvm_unreachable("Unknown target flag on GV operand");
85  case 0: break;
86  }
87 
88  return Printer.GetBlockAddressSymbol(MO.getBlockAddress());
89 }
90 
92 LowerSymbolOperand(const MachineOperand &MO, MCSymbol *Sym) const {
93  // FIXME: We would like an efficient form for this, so we don't have to do a
94  // lot of extra uniquing.
95  const MCExpr *Expr = MCSymbolRefExpr::Create(Sym, Ctx);
96 
97  switch (MO.getTargetFlags()) {
98  default: llvm_unreachable("Unknown target flag on GV operand");
99  case 0: break;
100  }
101 
102  if (!MO.isJTI() && MO.getOffset())
103  Expr = MCBinaryExpr::CreateAdd(Expr,
105  Ctx);
106  return MCOperand::CreateExpr(Expr);
107 }
108 
109 void MSP430MCInstLower::Lower(const MachineInstr *MI, MCInst &OutMI) const {
110  OutMI.setOpcode(MI->getOpcode());
111 
112  for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
113  const MachineOperand &MO = MI->getOperand(i);
114 
115  MCOperand MCOp;
116  switch (MO.getType()) {
117  default:
118  MI->dump();
119  llvm_unreachable("unknown operand type");
121  // Ignore all implicit register operands.
122  if (MO.isImplicit()) continue;
123  MCOp = MCOperand::CreateReg(MO.getReg());
124  break;
126  MCOp = MCOperand::CreateImm(MO.getImm());
127  break;
130  MO.getMBB()->getSymbol(), Ctx));
131  break;
134  break;
137  break;
139  MCOp = LowerSymbolOperand(MO, GetJumpTableSymbol(MO));
140  break;
143  break;
146  break;
148  continue;
149  }
150 
151  OutMI.addOperand(MCOp);
152  }
153 }
bool isImplicit() const
const GlobalValue * getGlobal() const
MCSymbol * getSymbol(const GlobalValue *GV) const
Definition: AsmPrinter.cpp:277
static MCOperand CreateReg(unsigned Reg)
Definition: MCInst.h:111
MachineBasicBlock * getMBB() const
static const MCConstantExpr * Create(int64_t Value, MCContext &Ctx)
Definition: MCExpr.cpp:152
const char * getPrivateGlobalPrefix() const
Definition: MCAsmInfo.h:434
static MCOperand CreateExpr(const MCExpr *Val)
Definition: MCInst.h:129
unsigned getFunctionNumber() const
Definition: AsmPrinter.cpp:125
const char * getSymbolName() const
MCOperand LowerSymbolOperand(const MachineOperand &MO, MCSymbol *Sym) const
Address of indexed Jump Table for switch.
bool isJTI() const
isJTI - Tests if this is a MO_JumpTableIndex operand.
MCSymbol * GetOrCreateSymbol(StringRef Name)
Definition: MCContext.cpp:118
#define llvm_unreachable(msg)
unsigned getNumOperands() const
Definition: MachineInstr.h:265
MCSymbol * GetExternalSymbolSymbol(const MachineOperand &MO) const
int getOpcode() const
Definition: MachineInstr.h:261
int64_t getImm() const
Address of indexed Constant in Constant Pool.
static const MCSymbolRefExpr * Create(const MCSymbol *Symbol, MCContext &Ctx)
Definition: MCExpr.h:270
unsigned getTargetFlags() const
const MCAsmInfo * MAI
Definition: AsmPrinter.h:66
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:267
int64_t getOffset() const
MCSymbol * GetGlobalAddressSymbol(const MachineOperand &MO) const
MCSymbol * getSymbol() const
void Lower(const MachineInstr *MI, MCInst &OutMI) const
void setOpcode(unsigned Op)
Definition: MCInst.h:157
MCSymbol * GetJumpTableSymbol(const MachineOperand &MO) const
void dump() const
StringRef str() const
Explicit conversion to StringRef.
Definition: SmallString.h:270
static const MCBinaryExpr * CreateAdd(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition: MCExpr.h:396
MachineOperandType getType() const
static MCOperand CreateImm(int64_t Val)
Definition: MCInst.h:117
MCSymbol * GetBlockAddressSymbol(const BlockAddress *BA) const
Mask of preserved registers.
unsigned getReg() const
getReg - Returns the register number.
Address of a basic block.
MCSymbol * GetConstantPoolIndexSymbol(const MachineOperand &MO) const
void addOperand(const MCOperand &Op)
Definition: MCInst.h:167
const BlockAddress * getBlockAddress() const
MCSymbol * GetExternalSymbolSymbol(StringRef Sym) const
MCSymbol * GetBlockAddressSymbol(const MachineOperand &MO) const
MachineBasicBlock reference.
Address of a global value.
Name of external global symbol.