LLVM  9.0.0svn
AVRMCInstLower.cpp
Go to the documentation of this file.
1 //===-- AVRMCInstLower.cpp - Convert AVR 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 AVR MachineInstrs to their corresponding
10 // MCInst records.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "AVRMCInstLower.h"
15 
16 #include "AVRInstrInfo.h"
17 #include "MCTargetDesc/AVRMCExpr.h"
18 
20 #include "llvm/IR/Mangler.h"
21 #include "llvm/MC/MCInst.h"
23 
24 namespace llvm {
25 
27  MCSymbol *Sym) const {
28  unsigned char TF = MO.getTargetFlags();
29  const MCExpr *Expr = MCSymbolRefExpr::create(Sym, Ctx);
30 
31  bool IsNegated = false;
32  if (TF & AVRII::MO_NEG) { IsNegated = true; }
33 
34  if (!MO.isJTI() && MO.getOffset()) {
36  Expr, MCConstantExpr::create(MO.getOffset(), Ctx), Ctx);
37  }
38 
39  bool IsFunction = MO.isGlobal() && isa<Function>(MO.getGlobal());
40 
41  if (TF & AVRII::MO_LO) {
42  if (IsFunction) {
43  // N.B. Should we use _GS fixups here to cope with >128k progmem?
44  Expr = AVRMCExpr::create(AVRMCExpr::VK_AVR_PM_LO8, Expr, IsNegated, Ctx);
45  } else {
46  Expr = AVRMCExpr::create(AVRMCExpr::VK_AVR_LO8, Expr, IsNegated, Ctx);
47  }
48  } else if (TF & AVRII::MO_HI) {
49  if (IsFunction) {
50  // N.B. Should we use _GS fixups here to cope with >128k progmem?
51  Expr = AVRMCExpr::create(AVRMCExpr::VK_AVR_PM_HI8, Expr, IsNegated, Ctx);
52  } else {
53  Expr = AVRMCExpr::create(AVRMCExpr::VK_AVR_HI8, Expr, IsNegated, Ctx);
54  }
55  } else if (TF != 0) {
56  llvm_unreachable("Unknown target flag on symbol operand");
57  }
58 
59  return MCOperand::createExpr(Expr);
60 }
61 
63  OutMI.setOpcode(MI.getOpcode());
64 
65  for (MachineOperand const &MO : MI.operands()) {
66  MCOperand MCOp;
67 
68  switch (MO.getType()) {
69  default:
70  MI.print(errs());
71  llvm_unreachable("unknown operand type");
73  // Ignore all implicit register operands.
74  if (MO.isImplicit())
75  continue;
76  MCOp = MCOperand::createReg(MO.getReg());
77  break;
79  MCOp = MCOperand::createImm(MO.getImm());
80  break;
82  MCOp = lowerSymbolOperand(MO, Printer.getSymbol(MO.getGlobal()));
83  break;
85  MCOp = lowerSymbolOperand(
86  MO, Printer.GetExternalSymbolSymbol(MO.getSymbolName()));
87  break;
89  MCOp = MCOperand::createExpr(
90  MCSymbolRefExpr::create(MO.getMBB()->getSymbol(), Ctx));
91  break;
93  continue;
95  MCOp = lowerSymbolOperand(
96  MO, Printer.GetBlockAddressSymbol(MO.getBlockAddress()));
97  break;
99  MCOp = lowerSymbolOperand(MO, Printer.GetJTISymbol(MO.getIndex()));
100  break;
102  MCOp = lowerSymbolOperand(MO, Printer.GetCPISymbol(MO.getIndex()));
103  break;
104  }
105 
106  OutMI.addOperand(MCOp);
107  }
108 }
109 
110 } // end of namespace llvm
111 
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:321
This class represents lattice values for constants.
Definition: AllocatorList.h:23
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
void lowerInstruction(const MachineInstr &MI, MCInst &OutMI) const
Lowers a MachineInstr into a MCInst.
static MCOperand createExpr(const MCExpr *Val)
Definition: MCInst.h:136
Address of indexed Jump Table for switch.
MachineBasicBlock reference.
iterator_range< mop_iterator > operands()
Definition: MachineInstr.h:458
Mask of preserved registers.
static MCOperand createReg(unsigned Reg)
Definition: MCInst.h:115
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:408
static const MCBinaryExpr * createAdd(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition: MCExpr.h:459
On a symbol operand, this represents it has to be negated.
Definition: AVRInstrInfo.h:58
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:158
Address of a global value.
Corresponds to pm_lo8().
Definition: AVRMCExpr.h:30
const GlobalValue * getGlobal() const
Corresponds to hi8().
Definition: AVRMCExpr.h:25
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.
On a symbol operand, this represents the hi part.
Definition: AVRInstrInfo.h:55
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: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.
bool isGlobal() const
isGlobal - Tests if this is a MO_GlobalAddress operand.
MCSymbol * getSymbol(const GlobalValue *GV) const
Definition: AsmPrinter.cpp:440
MachineOperand class - Representation of each machine instruction operand.
Corresponds to pm_hi8().
Definition: AVRMCExpr.h:31
On a symbol operand, this represents the lo part.
Definition: AVRInstrInfo.h:52
Representation of each machine instruction.
Definition: MachineInstr.h:63
MCSymbol * GetJTISymbol(unsigned JTID, bool isLinkerPrivate=false) const
Return the symbol for the specified jump table entry.
Corresponds to lo8().
Definition: AVRMCExpr.h:26
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:38
IRTranslator LLVM IR MI
void addOperand(const MCOperand &Op)
Definition: MCInst.h:183
Address of indexed Constant in Constant Pool.
Instances of this class represent operands of the MCInst class.
Definition: MCInst.h:34
static MCOperand createImm(int64_t Val)
Definition: MCInst.h:122
static const MCConstantExpr * create(int64_t Value, MCContext &Ctx)
Definition: MCExpr.cpp:163