LLVM  6.0.0svn
AVRMCInstLower.cpp
Go to the documentation of this file.
1 //===-- AVRMCInstLower.cpp - Convert AVR 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 AVR MachineInstrs to their corresponding
11 // MCInst records.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "AVRMCInstLower.h"
16 
17 #include "AVRInstrInfo.h"
18 #include "MCTargetDesc/AVRMCExpr.h"
19 
21 #include "llvm/IR/Mangler.h"
22 #include "llvm/MC/MCInst.h"
24 
25 namespace llvm {
26 
28  MCSymbol *Sym) const {
29  unsigned char TF = MO.getTargetFlags();
30  const MCExpr *Expr = MCSymbolRefExpr::create(Sym, Ctx);
31 
32  bool IsNegated = false;
33  if (TF & AVRII::MO_NEG) { IsNegated = true; }
34 
35  if (!MO.isJTI() && MO.getOffset()) {
37  Expr, MCConstantExpr::create(MO.getOffset(), Ctx), Ctx);
38  }
39 
40  bool IsFunction = MO.isGlobal() && isa<Function>(MO.getGlobal());
41 
42  if (TF & AVRII::MO_LO) {
43  if (IsFunction) {
44  // N.B. Should we use _GS fixups here to cope with >128k progmem?
45  Expr = AVRMCExpr::create(AVRMCExpr::VK_AVR_PM_LO8, Expr, IsNegated, Ctx);
46  } else {
47  Expr = AVRMCExpr::create(AVRMCExpr::VK_AVR_LO8, Expr, IsNegated, Ctx);
48  }
49  } else if (TF & AVRII::MO_HI) {
50  if (IsFunction) {
51  // N.B. Should we use _GS fixups here to cope with >128k progmem?
52  Expr = AVRMCExpr::create(AVRMCExpr::VK_AVR_PM_HI8, Expr, IsNegated, Ctx);
53  } else {
54  Expr = AVRMCExpr::create(AVRMCExpr::VK_AVR_HI8, Expr, IsNegated, Ctx);
55  }
56  } else if (TF != 0) {
57  llvm_unreachable("Unknown target flag on symbol operand");
58  }
59 
60  return MCOperand::createExpr(Expr);
61 }
62 
64  OutMI.setOpcode(MI.getOpcode());
65 
66  for (MachineOperand const &MO : MI.operands()) {
67  MCOperand MCOp;
68 
69  switch (MO.getType()) {
70  default:
71  MI.print(errs());
72  llvm_unreachable("unknown operand type");
74  // Ignore all implicit register operands.
75  if (MO.isImplicit())
76  continue;
77  MCOp = MCOperand::createReg(MO.getReg());
78  break;
80  MCOp = MCOperand::createImm(MO.getImm());
81  break;
83  MCOp = lowerSymbolOperand(MO, Printer.getSymbol(MO.getGlobal()));
84  break;
86  MCOp = lowerSymbolOperand(
87  MO, Printer.GetExternalSymbolSymbol(MO.getSymbolName()));
88  break;
90  MCOp = MCOperand::createExpr(
91  MCSymbolRefExpr::create(MO.getMBB()->getSymbol(), Ctx));
92  break;
94  continue;
96  MCOp = lowerSymbolOperand(
97  MO, Printer.GetBlockAddressSymbol(MO.getBlockAddress()));
98  break;
100  MCOp = lowerSymbolOperand(MO, Printer.GetJTISymbol(MO.getIndex()));
101  break;
103  MCOp = lowerSymbolOperand(MO, Printer.GetCPISymbol(MO.getIndex()));
104  break;
105  }
106 
107  OutMI.addOperand(MCOp);
108  }
109 }
110 
111 } // end of namespace llvm
112 
unsigned getTargetFlags() const
MCOperand lowerSymbolOperand(const MachineOperand &MO, MCSymbol *Sym) const
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
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
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:42
void lowerInstruction(const MachineInstr &MI, MCInst &OutMI) const
Lowers a MachineInstr into a MCInst.
static MCOperand createExpr(const MCExpr *Val)
Definition: MCInst.h:137
Address of indexed Jump Table for switch.
MachineBasicBlock reference.
iterator_range< mop_iterator > operands()
Definition: MachineInstr.h:332
Mask of preserved registers.
static MCOperand createReg(unsigned Reg)
Definition: MCInst.h:116
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:290
static const MCBinaryExpr * createAdd(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition: MCExpr.h:443
On a symbol operand, this represents it has to be negated.
Definition: AVRInstrInfo.h:59
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:159
Address of a global value.
Corresponds to pm_lo8().
Definition: AVRMCExpr.h:31
const GlobalValue * getGlobal() const
Corresponds to hi8().
Definition: AVRMCExpr.h:26
Address of a basic block.
On a symbol operand, this represents the hi part.
Definition: AVRInstrInfo.h:56
virtual MCSymbol * GetCPISymbol(unsigned CPID) const
Return the symbol for the specified constant pool entry.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
void setOpcode(unsigned Op)
Definition: MCInst.h:171
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.
bool isGlobal() const
isGlobal - Tests if this is a MO_GlobalAddress operand.
MCSymbol * getSymbol(const GlobalValue *GV) const
Definition: AsmPrinter.cpp:433
MachineOperand class - Representation of each machine instruction operand.
Corresponds to pm_hi8().
Definition: AVRMCExpr.h:32
On a symbol operand, this represents the lo part.
Definition: AVRInstrInfo.h:53
Representation of each machine instruction.
Definition: MachineInstr.h:59
MCSymbol * GetJTISymbol(unsigned JTID, bool isLinkerPrivate=false) const
Return the symbol for the specified jump table entry.
Corresponds to lo8().
Definition: AVRMCExpr.h:27
int64_t getOffset() const
Return the offset from the symbol in this operand.
static const AVRMCExpr * create(VariantKind Kind, const MCExpr *Expr, bool isNegated, MCContext &Ctx)
Creates an AVR machine code expression.
Definition: AVRMCExpr.cpp:36
void print(raw_ostream &OS, bool SkipOpers=false, bool SkipDebugLoc=false, const TargetInstrInfo *TII=nullptr) const
Debugging supportPrint this MI to OS.
IRTranslator LLVM IR MI
void addOperand(const MCOperand &Op)
Definition: MCInst.h:184
Address of indexed Constant in Constant Pool.
Instances of this class represent operands of the MCInst class.
Definition: MCInst.h:35
static MCOperand createImm(int64_t Val)
Definition: MCInst.h:123
static const MCConstantExpr * create(int64_t Value, MCContext &Ctx)
Definition: MCExpr.cpp:159