LLVM 22.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
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 = AVRMCExpr::create(Subtarget.hasEIJMPCALL() ? AVR::S_LO8_GS
47 Expr, IsNegated, Ctx);
48 } else {
49 Expr = AVRMCExpr::create(AVR::S_LO8, Expr, IsNegated, Ctx);
50 }
51 } else if (TF & AVRII::MO_HI) {
52 if (IsFunction) {
53 Expr = AVRMCExpr::create(Subtarget.hasEIJMPCALL() ? AVR::S_HI8_GS
55 Expr, IsNegated, Ctx);
56 } else {
57 Expr = AVRMCExpr::create(AVR::S_HI8, Expr, IsNegated, Ctx);
58 }
59 } else if (TF != 0) {
60 llvm_unreachable("Unknown target flag on symbol operand");
61 }
62
63 return MCOperand::createExpr(Expr);
64}
65
67 MCInst &OutMI) const {
68 auto &Subtarget = MI.getParent()->getParent()->getSubtarget<AVRSubtarget>();
69 OutMI.setOpcode(MI.getOpcode());
70
71 for (MachineOperand const &MO : MI.operands()) {
72 MCOperand MCOp;
73
74 switch (MO.getType()) {
75 default:
76 MI.print(errs());
77 llvm_unreachable("unknown operand type");
79 // Ignore all implicit register operands.
80 if (MO.isImplicit())
81 continue;
82 MCOp = MCOperand::createReg(MO.getReg());
83 break;
85 MCOp = MCOperand::createImm(MO.getImm());
86 break;
88 MCOp =
89 lowerSymbolOperand(MO, Printer.getSymbol(MO.getGlobal()), Subtarget);
90 break;
92 MCOp = lowerSymbolOperand(
93 MO, Printer.GetExternalSymbolSymbol(MO.getSymbolName()), Subtarget);
94 break;
97 MCSymbolRefExpr::create(MO.getMBB()->getSymbol(), Ctx));
98 break;
100 continue;
102 MCOp = lowerSymbolOperand(
103 MO, Printer.GetBlockAddressSymbol(MO.getBlockAddress()), Subtarget);
104 break;
106 MCOp = lowerSymbolOperand(MO, Printer.GetJTISymbol(MO.getIndex()),
107 Subtarget);
108 break;
110 MCOp = lowerSymbolOperand(MO, Printer.GetCPISymbol(MO.getIndex()),
111 Subtarget);
112 break;
113 }
114
115 OutMI.addOperand(MCOp);
116 }
117}
118
119} // end of namespace llvm
IRTranslator LLVM IR MI
static const AVRMCExpr * create(Specifier S, const MCExpr *Expr, bool isNegated, MCContext &Ctx)
Specifies the type of an expression.
Definition AVRMCExpr.cpp:17
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.
static const MCBinaryExpr * createAdd(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx, SMLoc Loc=SMLoc())
Definition MCExpr.h:343
static LLVM_ABI const MCConstantExpr * create(int64_t Value, MCContext &Ctx, bool PrintInHex=false, unsigned SizeInBytes=0)
Definition MCExpr.cpp:212
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:188
void addOperand(const MCOperand Op)
Definition MCInst.h:215
void setOpcode(unsigned Op)
Definition MCInst.h:201
Instances of this class represent operands of the MCInst class.
Definition MCInst.h:40
static MCOperand createExpr(const MCExpr *Val)
Definition MCInst.h:166
static MCOperand createReg(MCRegister Reg)
Definition MCInst.h:138
static MCOperand createImm(int64_t Val)
Definition MCInst.h:145
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx, SMLoc Loc=SMLoc())
Definition MCExpr.h:214
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition MCSymbol.h:42
Representation of each machine instruction.
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.
@ MO_NEG
On a symbol operand, this represents it has to be negated.
@ MO_LO
On a symbol operand, this represents the lo part.
@ S_LO8
Corresponds to lo8().
@ S_LO8_GS
Corresponds to lo8(gs()).
@ S_PM_LO8
Corresponds to pm_lo8().
@ S_HI8_GS
Corresponds to hi8(gs()).
@ S_PM_HI8
Corresponds to pm_hi8().
@ S_HI8
Corresponds to hi8().
This is an optimization pass for GlobalISel generic memory operations.
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:548
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.