LLVM 20.0.0git
MSP430MCInstLower.cpp
Go to the documentation of this file.
1//===-- MSP430MCInstLower.cpp - Convert MSP430 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 MSP430 MachineInstrs to their corresponding
10// MCInst records.
11//
12//===----------------------------------------------------------------------===//
13
14#include "MSP430MCInstLower.h"
19#include "llvm/IR/DataLayout.h"
20#include "llvm/IR/Mangler.h"
21#include "llvm/MC/MCAsmInfo.h"
22#include "llvm/MC/MCContext.h"
23#include "llvm/MC/MCExpr.h"
24#include "llvm/MC/MCInst.h"
28using namespace llvm;
29
32 switch (MO.getTargetFlags()) {
33 default: llvm_unreachable("Unknown target flag on GV operand");
34 case 0: break;
35 }
36
37 return Printer.getSymbol(MO.getGlobal());
38}
39
42 switch (MO.getTargetFlags()) {
43 default: llvm_unreachable("Unknown target flag on GV operand");
44 case 0: break;
45 }
46
47 return Printer.GetExternalSymbolSymbol(MO.getSymbolName());
48}
49
51GetJumpTableSymbol(const MachineOperand &MO) const {
52 const DataLayout &DL = Printer.getDataLayout();
54 raw_svector_ostream(Name) << DL.getPrivateGlobalPrefix() << "JTI"
55 << Printer.getFunctionNumber() << '_'
56 << MO.getIndex();
57
58 switch (MO.getTargetFlags()) {
59 default: llvm_unreachable("Unknown target flag on GV operand");
60 case 0: break;
61 }
62
63 // Create a symbol for the name.
64 return Ctx.getOrCreateSymbol(Name);
65}
66
69 const DataLayout &DL = Printer.getDataLayout();
71 raw_svector_ostream(Name) << DL.getPrivateGlobalPrefix() << "CPI"
72 << Printer.getFunctionNumber() << '_'
73 << MO.getIndex();
74
75 switch (MO.getTargetFlags()) {
76 default: llvm_unreachable("Unknown target flag on GV operand");
77 case 0: break;
78 }
79
80 // Create a symbol for the name.
81 return Ctx.getOrCreateSymbol(Name);
82}
83
86 switch (MO.getTargetFlags()) {
87 default: llvm_unreachable("Unknown target flag on GV operand");
88 case 0: break;
89 }
90
91 return Printer.GetBlockAddressSymbol(MO.getBlockAddress());
92}
93
96 // FIXME: We would like an efficient form for this, so we don't have to do a
97 // lot of extra uniquing.
98 const MCExpr *Expr = MCSymbolRefExpr::create(Sym, Ctx);
99
100 switch (MO.getTargetFlags()) {
101 default: llvm_unreachable("Unknown target flag on GV operand");
102 case 0: break;
103 }
104
105 if (!MO.isJTI() && MO.getOffset())
106 Expr = MCBinaryExpr::createAdd(Expr,
108 Ctx);
109 return MCOperand::createExpr(Expr);
110}
111
112#define GET_REGINFO_ENUM
113#include "MSP430GenRegisterInfo.inc"
114
115void MSP430MCInstLower::Lower(const MachineInstr *MI, MCInst &OutMI) const {
116 OutMI.setOpcode(MI->getOpcode());
117
118 for (const MachineOperand &MO : MI->operands()) {
119 MCOperand MCOp;
120 switch (MO.getType()) {
121 default:
122 MI->print(errs());
123 llvm_unreachable("unknown operand type");
125 // Ignore all implicit register operands.
126 if (MO.isImplicit()) continue;
127 MCOp = MCOperand::createReg(MO.getReg());
128 break;
130 MCOp = MCOperand::createImm(MO.getImm());
131 break;
134 MO.getMBB()->getSymbol(), Ctx));
135 break;
138 break;
141 break;
144 break;
147 break;
150 break;
152 continue;
153 }
154
155 OutMI.addOperand(MCOp);
156 }
157}
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
std::string Name
Symbol * Sym
Definition: ELF_riscv.cpp:479
IRTranslator LLVM IR MI
This file defines the SmallString class.
MCSymbol * getSymbol(const GlobalValue *GV) const
Definition: AsmPrinter.cpp:676
unsigned getFunctionNumber() const
Return a unique ID for the current function.
Definition: AsmPrinter.cpp:379
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.
const DataLayout & getDataLayout() const
Return information about data layout.
Definition: AsmPrinter.cpp:387
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:63
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
MCSymbol * getOrCreateSymbol(const Twine &Name)
Lookup the symbol inside with the specified Name.
Definition: MCContext.cpp:213
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
void Lower(const MachineInstr *MI, MCInst &OutMI) const
MCSymbol * GetExternalSymbolSymbol(const MachineOperand &MO) const
MCOperand LowerSymbolOperand(const MachineOperand &MO, MCSymbol *Sym) const
MCSymbol * GetGlobalAddressSymbol(const MachineOperand &MO) const
MCSymbol * GetBlockAddressSymbol(const MachineOperand &MO) const
MCSymbol * GetConstantPoolIndexSymbol(const MachineOperand &MO) const
MCSymbol * GetJumpTableSymbol(const MachineOperand &MO) const
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.
const BlockAddress * getBlockAddress() const
unsigned getTargetFlags() const
const char * getSymbolName() const
@ 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.
virtual void print(raw_ostream &OS, const Module *M) const
print - Print out the internal state of the pass.
Definition: Pass.cpp:130
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition: SmallString.h:26
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:691
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
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.