LLVM  14.0.0git
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) {
33  IsNegated = true;
34  }
35 
36  if (!MO.isJTI() && MO.getOffset()) {
38  Expr, MCConstantExpr::create(MO.getOffset(), Ctx), Ctx);
39  }
40 
41  bool IsFunction = MO.isGlobal() && isa<Function>(MO.getGlobal());
42 
43  if (TF & AVRII::MO_LO) {
44  if (IsFunction) {
45  // N.B. Should we use _GS fixups here to cope with >128k progmem?
46  Expr = AVRMCExpr::create(AVRMCExpr::VK_AVR_PM_LO8, Expr, IsNegated, Ctx);
47  } else {
48  Expr = AVRMCExpr::create(AVRMCExpr::VK_AVR_LO8, Expr, IsNegated, Ctx);
49  }
50  } else if (TF & AVRII::MO_HI) {
51  if (IsFunction) {
52  // N.B. Should we use _GS fixups here to cope with >128k progmem?
53  Expr = AVRMCExpr::create(AVRMCExpr::VK_AVR_PM_HI8, Expr, IsNegated, Ctx);
54  } else {
55  Expr = AVRMCExpr::create(AVRMCExpr::VK_AVR_HI8, Expr, IsNegated, Ctx);
56  }
57  } else if (TF != 0) {
58  llvm_unreachable("Unknown target flag on symbol operand");
59  }
60 
61  return MCOperand::createExpr(Expr);
62 }
63 
65  MCInst &OutMI) const {
66  OutMI.setOpcode(MI.getOpcode());
67 
68  for (MachineOperand const &MO : MI.operands()) {
69  MCOperand MCOp;
70 
71  switch (MO.getType()) {
72  default:
73  MI.print(errs());
74  llvm_unreachable("unknown operand type");
76  // Ignore all implicit register operands.
77  if (MO.isImplicit())
78  continue;
79  MCOp = MCOperand::createReg(MO.getReg());
80  break;
82  MCOp = MCOperand::createImm(MO.getImm());
83  break;
85  MCOp = lowerSymbolOperand(MO, Printer.getSymbol(MO.getGlobal()));
86  break;
88  MCOp = lowerSymbolOperand(
89  MO, Printer.GetExternalSymbolSymbol(MO.getSymbolName()));
90  break;
92  MCOp = MCOperand::createExpr(
93  MCSymbolRefExpr::create(MO.getMBB()->getSymbol(), Ctx));
94  break;
96  continue;
98  MCOp = lowerSymbolOperand(
99  MO, Printer.GetBlockAddressSymbol(MO.getBlockAddress()));
100  break;
102  MCOp = lowerSymbolOperand(MO, Printer.GetJTISymbol(MO.getIndex()));
103  break;
105  MCOp = lowerSymbolOperand(MO, Printer.GetCPISymbol(MO.getIndex()));
106  break;
107  }
108 
109  OutMI.addOperand(MCOp);
110  }
111 }
112 
113 } // end of namespace llvm
AsmPrinter.h
llvm::MachineOperand::MO_BlockAddress
@ MO_BlockAddress
Address of a basic block.
Definition: MachineOperand.h:63
llvm::AVRMCExpr::VK_AVR_PM_HI8
@ VK_AVR_PM_HI8
Corresponds to pm_hi8().
Definition: AVRMCExpr.h:32
MI
IRTranslator LLVM IR MI
Definition: IRTranslator.cpp:103
llvm::MachineOperand::MO_Immediate
@ MO_Immediate
Immediate operand.
Definition: MachineOperand.h:53
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
llvm::MCSymbol
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
llvm::MCOperand::createExpr
static MCOperand createExpr(const MCExpr *Val)
Definition: MCInst.h:162
llvm::MachineOperand::getGlobal
const GlobalValue * getGlobal() const
Definition: MachineOperand.h:563
llvm::MCOperand::createImm
static MCOperand createImm(int64_t Val)
Definition: MCInst.h:141
AVRMCInstLower.h
llvm::AVRMCInstLower::lowerSymbolOperand
MCOperand lowerSymbolOperand(const MachineOperand &MO, MCSymbol *Sym) const
Definition: AVRMCInstLower.cpp:26
llvm::MCConstantExpr::create
static const MCConstantExpr * create(int64_t Value, MCContext &Ctx, bool PrintInHex=false, unsigned SizeInBytes=0)
Definition: MCExpr.cpp:194
ErrorHandling.h
llvm::AVRMCExpr::VK_AVR_LO8
@ VK_AVR_LO8
Corresponds to lo8().
Definition: AVRMCExpr.h:26
llvm::MachineOperand::isJTI
bool isJTI() const
isJTI - Tests if this is a MO_JumpTableIndex operand.
Definition: MachineOperand.h:337
llvm::errs
raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
Definition: raw_ostream.cpp:892
llvm::MCInst
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:184
llvm::MachineOperand::getOffset
int64_t getOffset() const
Return the offset from the symbol in this operand.
Definition: MachineOperand.h:600
llvm::MachineOperand::MO_Register
@ MO_Register
Register operand.
Definition: MachineOperand.h:52
AVRMCExpr.h
llvm::MCInst::setOpcode
void setOpcode(unsigned Op)
Definition: MCInst.h:197
llvm::MachineOperand::MO_GlobalAddress
@ MO_GlobalAddress
Address of a global value.
Definition: MachineOperand.h:62
MCInst.h
llvm::MachineOperand
MachineOperand class - Representation of each machine instruction operand.
Definition: MachineOperand.h:49
llvm::MCInst::addOperand
void addOperand(const MCOperand Op)
Definition: MCInst.h:210
llvm::AVRMCExpr::create
static const AVRMCExpr * create(VariantKind Kind, const MCExpr *Expr, bool isNegated, MCContext &Ctx)
Creates an AVR machine code expression.
Definition: AVRMCExpr.cpp:38
llvm::MachineOperand::getTargetFlags
unsigned getTargetFlags() const
Definition: MachineOperand.h:221
llvm::AsmPrinter::GetCPISymbol
virtual MCSymbol * GetCPISymbol(unsigned CPID) const
Return the symbol for the specified constant pool entry.
Definition: AsmPrinter.cpp:3125
llvm::MachineInstr
Representation of each machine instruction.
Definition: MachineInstr.h:64
llvm::AsmPrinter::GetBlockAddressSymbol
MCSymbol * GetBlockAddressSymbol(const BlockAddress *BA) const
Return the MCSymbol used to satisfy BlockAddress uses of the specified basic block.
Definition: AsmPrinter.cpp:3116
llvm::AsmPrinter::getSymbol
MCSymbol * getSymbol(const GlobalValue *GV) const
Definition: AsmPrinter.cpp:486
llvm::AVRII::MO_NEG
@ MO_NEG
On a symbol operand, this represents it has to be negated.
Definition: AVRInstrInfo.h:58
Mangler.h
llvm::MachineOperand::MO_JumpTableIndex
@ MO_JumpTableIndex
Address of indexed Jump Table for switch.
Definition: MachineOperand.h:60
llvm::MCBinaryExpr::createAdd
static const MCBinaryExpr * createAdd(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition: MCExpr.h:524
llvm::AsmPrinter::GetExternalSymbolSymbol
MCSymbol * GetExternalSymbolSymbol(StringRef Sym) const
Return the MCSymbol for the specified ExternalSymbol.
Definition: AsmPrinter.cpp:3172
llvm::AsmPrinter::GetJTISymbol
MCSymbol * GetJTISymbol(unsigned JTID, bool isLinkerPrivate=false) const
Return the symbol for the specified jump table entry.
Definition: AsmPrinter.cpp:3153
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:136
llvm::MCOperand::createReg
static MCOperand createReg(unsigned Reg)
Definition: MCInst.h:134
llvm::MachineOperand::MO_MachineBasicBlock
@ MO_MachineBasicBlock
MachineBasicBlock reference.
Definition: MachineOperand.h:56
llvm::MachineOperand::MO_ExternalSymbol
@ MO_ExternalSymbol
Name of external global symbol.
Definition: MachineOperand.h:61
llvm::MCSymbolRefExpr::create
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx)
Definition: MCExpr.h:385
AVRInstrInfo.h
llvm::MCOperand
Instances of this class represent operands of the MCInst class.
Definition: MCInst.h:36
llvm::MachineOperand::MO_RegisterMask
@ MO_RegisterMask
Mask of preserved registers.
Definition: MachineOperand.h:64
llvm::AVRMCExpr::VK_AVR_HI8
@ VK_AVR_HI8
Corresponds to hi8().
Definition: AVRMCExpr.h:25
llvm::AVRII::MO_LO
@ MO_LO
On a symbol operand, this represents the lo part.
Definition: AVRInstrInfo.h:52
llvm::MCExpr
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:35
llvm::AVRMCInstLower::lowerInstruction
void lowerInstruction(const MachineInstr &MI, MCInst &OutMI) const
Lowers a MachineInstr into a MCInst.
Definition: AVRMCInstLower.cpp:64
llvm::AVRMCExpr::VK_AVR_PM_LO8
@ VK_AVR_PM_LO8
Corresponds to pm_lo8().
Definition: AVRMCExpr.h:31
llvm::AVRII::MO_HI
@ MO_HI
On a symbol operand, this represents the hi part.
Definition: AVRInstrInfo.h:55
llvm::MachineOperand::isGlobal
bool isGlobal() const
isGlobal - Tests if this is a MO_GlobalAddress operand.
Definition: MachineOperand.h:339
llvm::MachineOperand::MO_ConstantPoolIndex
@ MO_ConstantPoolIndex
Address of indexed Constant in Constant Pool.
Definition: MachineOperand.h:58