LLVM 22.0.0git
ARCMCInstLower.cpp
Go to the documentation of this file.
1//===- ARCMCInstLower.cpp - ARC MachineInstr to MCInst ----------*- C++ -*-===//
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/// \file
10/// This file contains code to lower ARC MachineInstrs to their
11/// corresponding MCInst records.
12///
13//===----------------------------------------------------------------------===//
14
15#include "ARCMCInstLower.h"
20#include "llvm/MC/MCContext.h"
21#include "llvm/MC/MCExpr.h"
22#include "llvm/MC/MCInst.h"
23
24using namespace llvm;
25
28
29MCOperand ARCMCInstLower::LowerSymbolOperand(const MachineOperand &MO,
30 MachineOperandType MOTy,
31 unsigned Offset) const {
32 const MCSymbol *Symbol;
33
34 switch (MOTy) {
36 Symbol = MO.getMBB()->getSymbol();
37 break;
39 Symbol = Printer.getSymbol(MO.getGlobal());
40 Offset += MO.getOffset();
41 break;
43 Symbol = Printer.GetBlockAddressSymbol(MO.getBlockAddress());
44 Offset += MO.getOffset();
45 break;
47 Symbol = Printer.GetExternalSymbolSymbol(MO.getSymbolName());
48 Offset += MO.getOffset();
49 break;
51 Symbol = Printer.GetJTISymbol(MO.getIndex());
52 break;
54 Symbol = Printer.GetCPISymbol(MO.getIndex());
55 Offset += MO.getOffset();
56 break;
57 default:
58 llvm_unreachable("<unknown operand type>");
59 }
60
61 assert(Symbol && "Symbol creation failed.\n");
62 const MCSymbolRefExpr *MCSym = MCSymbolRefExpr::create(Symbol, *Ctx);
63
64 if (!Offset)
65 return MCOperand::createExpr(MCSym);
66
67 // Assume offset is never negative.
68 assert(Offset > 0);
69
70 const MCConstantExpr *OffsetExpr = MCConstantExpr::create(Offset, *Ctx);
71 const MCBinaryExpr *Add = MCBinaryExpr::createAdd(MCSym, OffsetExpr, *Ctx);
73}
74
76 unsigned Offset) const {
77 MachineOperandType MOTy = MO.getType();
78
79 switch (MOTy) {
80 default:
81 llvm_unreachable("unknown operand type");
83 // Ignore all implicit register operands.
84 if (MO.isImplicit())
85 break;
86 return MCOperand::createReg(MO.getReg());
88 return MCOperand::createImm(MO.getImm() + Offset);
95 return LowerSymbolOperand(MO, MOTy, Offset);
97 break;
98 }
99
100 return {};
101}
102
103void ARCMCInstLower::Lower(const MachineInstr *MI, MCInst &OutMI) const {
104 OutMI.setOpcode(MI->getOpcode());
105
106 for (const MachineOperand &MO : MI->operands()) {
107 MCOperand MCOp = LowerOperand(MO);
108
109 if (MCOp.isValid())
110 OutMI.addOperand(MCOp);
111 }
112}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
dxil pretty DXIL Metadata Pretty Printer
IRTranslator LLVM IR MI
ARCMCInstLower(MCContext *C, AsmPrinter &asmprinter)
void Lower(const MachineInstr *MI, MCInst &OutMI) const
MCOperand LowerOperand(const MachineOperand &MO, unsigned offset=0) const
This class is intended to be used as a driving class for all asm writers.
Definition AsmPrinter.h:90
Binary assembler expressions.
Definition MCExpr.h:299
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
Context object for machine code objects.
Definition MCContext.h:83
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
bool isValid() const
Definition MCInst.h:64
Represent a reference to a symbol from inside an expression.
Definition MCExpr.h:190
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
LLVM_ABI MCSymbol * getSymbol() const
Return the MCSymbol for this basic block.
Representation of each machine instruction.
MachineOperand class - Representation of each machine instruction operand.
const GlobalValue * getGlobal() const
int64_t getImm() const
MachineBasicBlock * getMBB() const
const BlockAddress * getBlockAddress() const
MachineOperandType getType() const
getType - Returns the MachineOperandType for this operand.
const char * getSymbolName() const
Register getReg() const
getReg - Returns the register number.
@ 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.
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:477
@ Add
Sum of integers.