LLVM  8.0.1
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 #define GET_REGINFO_ENUM
114 #include "MSP430GenRegisterInfo.inc"
115 
116 void MSP430MCInstLower::Lower(const MachineInstr *MI, MCInst &OutMI) const {
117  OutMI.setOpcode(MI->getOpcode());
118 
119  for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
120  const MachineOperand &MO = MI->getOperand(i);
121 
122  MCOperand MCOp;
123  switch (MO.getType()) {
124  default:
125  MI->print(errs());
126  llvm_unreachable("unknown operand type");
128  // Ignore all implicit register operands.
129  if (MO.isImplicit()) continue;
130  MCOp = MCOperand::createReg(MO.getReg());
131  break;
133  MCOp = MCOperand::createImm(MO.getImm());
134  break;
137  MO.getMBB()->getSymbol(), Ctx));
138  break;
141  break;
144  break;
146  MCOp = LowerSymbolOperand(MO, GetJumpTableSymbol(MO));
147  break;
150  break;
153  break;
155  continue;
156  }
157 
158  OutMI.addOperand(MCOp);
159  }
160 }
unsigned getTargetFlags() const
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:111
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:323
This class represents lattice values for constants.
Definition: AllocatorList.h:24
StringRef getPrivateGlobalPrefix() const
Definition: DataLayout.h:294
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:510
MachineBasicBlock reference.
MCSymbol * GetGlobalAddressSymbol(const MachineOperand &MO) const
Mask of preserved registers.
amdgpu Simplify well known AMD library false Value Value const Twine & Name
static MCOperand createReg(unsigned Reg)
Definition: MCInst.h:116
unsigned getNumOperands() const
Retuns the total number of operands.
Definition: MachineInstr.h:412
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:409
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:461
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:161
Address of a global value.
const GlobalValue * getGlobal() const
Address of a basic block.
void print(raw_ostream &OS, bool IsStandalone=true, bool SkipOpers=false, bool SkipDebugLoc=false, bool AddNewLine=true, const TargetInstrInfo *TII=nullptr) const
Print this MI to OS.
#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:208
void setOpcode(unsigned Op)
Definition: MCInst.h:173
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:430
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:64
MCSymbol * getOrCreateSymbol(const Twine &Name)
Lookup the symbol inside with the specified Name.
Definition: MCContext.cpp:123
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.
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:216
IRTranslator LLVM IR MI
void addOperand(const MCOperand &Op)
Definition: MCInst.h:186
Address of indexed Constant in Constant Pool.
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:414
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:164
bool isImplicit() const