LLVM 20.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#include "AVRInstrInfo.h"
17
19#include "llvm/IR/Mangler.h"
20#include "llvm/MC/MCInst.h"
22
23namespace llvm {
24
25MCOperand
27 const AVRSubtarget &Subtarget) 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 Expr =
46 AVRMCExpr::create(Subtarget.hasEIJMPCALL() ? AVRMCExpr::VK_AVR_LO8_GS
48 Expr, IsNegated, Ctx);
49 } else {
50 Expr = AVRMCExpr::create(AVRMCExpr::VK_AVR_LO8, Expr, IsNegated, Ctx);
51 }
52 } else if (TF & AVRII::MO_HI) {
53 if (IsFunction) {
54 Expr =
55 AVRMCExpr::create(Subtarget.hasEIJMPCALL() ? AVRMCExpr::VK_AVR_HI8_GS
57 Expr, IsNegated, Ctx);
58 } else {
59 Expr = AVRMCExpr::create(AVRMCExpr::VK_AVR_HI8, Expr, IsNegated, Ctx);
60 }
61 } else if (TF != 0) {
62 llvm_unreachable("Unknown target flag on symbol operand");
63 }
64
65 return MCOperand::createExpr(Expr);
66}
67
69 MCInst &OutMI) const {
70 auto &Subtarget = MI.getParent()->getParent()->getSubtarget<AVRSubtarget>();
71 OutMI.setOpcode(MI.getOpcode());
72
73 for (MachineOperand const &MO : MI.operands()) {
74 MCOperand MCOp;
75
76 switch (MO.getType()) {
77 default:
78 MI.print(errs());
79 llvm_unreachable("unknown operand type");
81 // Ignore all implicit register operands.
82 if (MO.isImplicit())
83 continue;
84 MCOp = MCOperand::createReg(MO.getReg());
85 break;
87 MCOp = MCOperand::createImm(MO.getImm());
88 break;
90 MCOp =
91 lowerSymbolOperand(MO, Printer.getSymbol(MO.getGlobal()), Subtarget);
92 break;
94 MCOp = lowerSymbolOperand(
95 MO, Printer.GetExternalSymbolSymbol(MO.getSymbolName()), Subtarget);
96 break;
99 MCSymbolRefExpr::create(MO.getMBB()->getSymbol(), Ctx));
100 break;
102 continue;
104 MCOp = lowerSymbolOperand(
105 MO, Printer.GetBlockAddressSymbol(MO.getBlockAddress()), Subtarget);
106 break;
108 MCOp = lowerSymbolOperand(MO, Printer.GetJTISymbol(MO.getIndex()),
109 Subtarget);
110 break;
112 MCOp = lowerSymbolOperand(MO, Printer.GetCPISymbol(MO.getIndex()),
113 Subtarget);
114 break;
115 }
116
117 OutMI.addOperand(MCOp);
118 }
119}
120
121} // end of namespace llvm
Symbol * Sym
Definition: ELF_riscv.cpp:479
IRTranslator LLVM IR MI
static const AVRMCExpr * create(VariantKind Kind, const MCExpr *Expr, bool isNegated, MCContext &Ctx)
Creates an AVR machine code expression.
Definition: AVRMCExpr.cpp:37
@ VK_AVR_PM_LO8
Corresponds to pm_lo8().
Definition: AVRMCExpr.h:31
@ VK_AVR_LO8
Corresponds to lo8().
Definition: AVRMCExpr.h:26
@ VK_AVR_PM_HI8
Corresponds to pm_hi8().
Definition: AVRMCExpr.h:32
@ VK_AVR_HI8_GS
Corresponds to hi8(gs()).
Definition: AVRMCExpr.h:36
@ VK_AVR_LO8_GS
Corresponds to lo8(gs()).
Definition: AVRMCExpr.h:35
@ VK_AVR_HI8
Corresponds to hi8().
Definition: AVRMCExpr.h:25
void lowerInstruction(const MachineInstr &MI, MCInst &OutMI) const
Lowers a MachineInstr into a MCInst.
MCOperand lowerSymbolOperand(const MachineOperand &MO, MCSymbol *Sym, const AVRSubtarget &Subtarget) const
A specific AVR target MCU.
Definition: AVRSubtarget.h:32
MCSymbol * getSymbol(const GlobalValue *GV) const
Definition: AsmPrinter.cpp:676
virtual MCSymbol * GetCPISymbol(unsigned CPID) const
Return the symbol for the specified constant pool entry.
MCSymbol * GetJTISymbol(unsigned JTID, bool isLinkerPrivate=false) const
Return the symbol for the specified jump table entry.
MCSymbol * GetExternalSymbolSymbol(Twine Sym) const
Return the MCSymbol for the specified ExternalSymbol.
MCSymbol * GetBlockAddressSymbol(const BlockAddress *BA) const
Return the MCSymbol used to satisfy BlockAddress uses of the specified basic block.
static const MCBinaryExpr * createAdd(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition: MCExpr.h:532
static const MCConstantExpr * create(int64_t Value, MCContext &Ctx, bool PrintInHex=false, unsigned SizeInBytes=0)
Definition: MCExpr.cpp:193
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:34
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:184
void addOperand(const MCOperand Op)
Definition: MCInst.h:210
void setOpcode(unsigned Op)
Definition: MCInst.h:197
Instances of this class represent operands of the MCInst class.
Definition: MCInst.h:36
static MCOperand createReg(unsigned Reg)
Definition: MCInst.h:134
static MCOperand createExpr(const MCExpr *Val)
Definition: MCInst.h:162
static MCOperand createImm(int64_t Val)
Definition: MCInst.h:141
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx)
Definition: MCExpr.h:393
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
Representation of each machine instruction.
Definition: MachineInstr.h:69
MachineOperand class - Representation of each machine instruction operand.
const GlobalValue * getGlobal() const
bool isJTI() const
isJTI - Tests if this is a MO_JumpTableIndex operand.
unsigned getTargetFlags() const
bool isGlobal() const
isGlobal - Tests if this is a MO_GlobalAddress operand.
@ MO_Immediate
Immediate operand.
@ MO_ConstantPoolIndex
Address of indexed Constant in Constant Pool.
@ MO_GlobalAddress
Address of a global value.
@ MO_RegisterMask
Mask of preserved registers.
@ MO_BlockAddress
Address of a basic block.
@ MO_MachineBasicBlock
MachineBasicBlock reference.
@ MO_Register
Register operand.
@ MO_ExternalSymbol
Name of external global symbol.
@ MO_JumpTableIndex
Address of indexed Jump Table for switch.
int64_t getOffset() const
Return the offset from the symbol in this operand.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ MO_HI
On a symbol operand, this represents the hi part.
Definition: AVRInstrInfo.h:57
@ MO_NEG
On a symbol operand, this represents it has to be negated.
Definition: AVRInstrInfo.h:60
@ MO_LO
On a symbol operand, this represents the lo part.
Definition: AVRInstrInfo.h:54
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.