LLVM  9.0.0svn
MSP430MCInstLower.cpp
Go to the documentation of this file.
1 //===-- MSP430MCInstLower.cpp - Convert MSP430 MachineInstr to an 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 // This file contains code to lower MSP430 MachineInstrs to their corresponding
10 // MCInst records.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "MSP430MCInstLower.h"
15 #include "llvm/ADT/SmallString.h"
19 #include "llvm/IR/DataLayout.h"
20 #include "llvm/IR/Mangler.h"
21 #include "llvm/MC/MCAsmInfo.h"
22 #include "llvm/MC/MCContext.h"
23 #include "llvm/MC/MCExpr.h"
24 #include "llvm/MC/MCInst.h"
28 using namespace llvm;
29 
32  switch (MO.getTargetFlags()) {
33  default: llvm_unreachable("Unknown target flag on GV operand");
34  case 0: break;
35  }
36 
37  return Printer.getSymbol(MO.getGlobal());
38 }
39 
42  switch (MO.getTargetFlags()) {
43  default: llvm_unreachable("Unknown target flag on GV operand");
44  case 0: break;
45  }
46 
47  return Printer.GetExternalSymbolSymbol(MO.getSymbolName());
48 }
49 
52  const DataLayout &DL = Printer.getDataLayout();
54  raw_svector_ostream(Name) << DL.getPrivateGlobalPrefix() << "JTI"
55  << Printer.getFunctionNumber() << '_'
56  << MO.getIndex();
57 
58  switch (MO.getTargetFlags()) {
59  default: llvm_unreachable("Unknown target flag on GV operand");
60  case 0: break;
61  }
62 
63  // Create a symbol for the name.
64  return Ctx.getOrCreateSymbol(Name);
65 }
66 
69  const DataLayout &DL = Printer.getDataLayout();
71  raw_svector_ostream(Name) << DL.getPrivateGlobalPrefix() << "CPI"
72  << Printer.getFunctionNumber() << '_'
73  << MO.getIndex();
74 
75  switch (MO.getTargetFlags()) {
76  default: llvm_unreachable("Unknown target flag on GV operand");
77  case 0: break;
78  }
79 
80  // Create a symbol for the name.
81  return Ctx.getOrCreateSymbol(Name);
82 }
83 
86  switch (MO.getTargetFlags()) {
87  default: llvm_unreachable("Unknown target flag on GV operand");
88  case 0: break;
89  }
90 
91  return Printer.GetBlockAddressSymbol(MO.getBlockAddress());
92 }
93 
95 LowerSymbolOperand(const MachineOperand &MO, MCSymbol *Sym) const {
96  // FIXME: We would like an efficient form for this, so we don't have to do a
97  // lot of extra uniquing.
98  const MCExpr *Expr = MCSymbolRefExpr::create(Sym, Ctx);
99 
100  switch (MO.getTargetFlags()) {
101  default: llvm_unreachable("Unknown target flag on GV operand");
102  case 0: break;
103  }
104 
105  if (!MO.isJTI() && MO.getOffset())
106  Expr = MCBinaryExpr::createAdd(Expr,
108  Ctx);
109  return MCOperand::createExpr(Expr);
110 }
111 
112 #define GET_REGINFO_ENUM
113 #include "MSP430GenRegisterInfo.inc"
114 
115 void MSP430MCInstLower::Lower(const MachineInstr *MI, MCInst &OutMI) const {
116  OutMI.setOpcode(MI->getOpcode());
117 
118  for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
119  const MachineOperand &MO = MI->getOperand(i);
120 
121  MCOperand MCOp;
122  switch (MO.getType()) {
123  default:
124  MI->print(errs());
125  llvm_unreachable("unknown operand type");
127  // Ignore all implicit register operands.
128  if (MO.isImplicit()) continue;
129  MCOp = MCOperand::createReg(MO.getReg());
130  break;
132  MCOp = MCOperand::createImm(MO.getImm());
133  break;
136  MO.getMBB()->getSymbol(), Ctx));
137  break;
140  break;
143  break;
145  MCOp = LowerSymbolOperand(MO, GetJumpTableSymbol(MO));
146  break;
149  break;
152  break;
154  continue;
155  }
156 
157  OutMI.addOperand(MCOp);
158  }
159 }
unsigned getTargetFlags() const
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:110
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:23
StringRef getPrivateGlobalPrefix() const
Definition: DataLayout.h:316
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
MCSymbol * GetConstantPoolIndexSymbol(const MachineOperand &MO) const
static MCOperand createExpr(const MCExpr *Val)
Definition: MCInst.h:136
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:509
MachineBasicBlock reference.
MCSymbol * GetGlobalAddressSymbol(const MachineOperand &MO) const
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:414
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:35
Name of external global symbol.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
Definition: MachineInstr.h:411
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:158
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:210
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.
bool isJTI() const
isJTI - Tests if this is a MO_JumpTableIndex operand.
MCSymbol * getSymbol(const GlobalValue *GV) const
Definition: AsmPrinter.cpp:443
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:127
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:218
IRTranslator LLVM IR MI
void addOperand(const MCOperand &Op)
Definition: MCInst.h:183
Address of indexed Constant in Constant Pool.
Register getReg() const
getReg - Returns the register number.
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:416
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
static const MCConstantExpr * create(int64_t Value, MCContext &Ctx)
Definition: MCExpr.cpp:163
bool isImplicit() const