LLVM 23.0.0git
MSP430AsmPrinter.cpp
Go to the documentation of this file.
1//===-- MSP430AsmPrinter.cpp - MSP430 LLVM assembly writer ----------------===//
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 a printer that converts from our internal representation
10// of machine-dependent LLVM code to the MSP430 assembly language.
11//
12//===----------------------------------------------------------------------===//
13
14#include "MSP430AsmPrinter.h"
16#include "MSP430MCInstLower.h"
17#include "MSP430TargetMachine.h"
27#include "llvm/IR/Analysis.h"
28#include "llvm/IR/Mangler.h"
29#include "llvm/IR/PassManager.h"
30#include "llvm/MC/MCAsmInfo.h"
31#include "llvm/MC/MCInst.h"
33#include "llvm/MC/MCStreamer.h"
34#include "llvm/MC/MCSymbol.h"
38using namespace llvm;
39
40#define DEBUG_TYPE "asm-printer"
41
42namespace {
43 class MSP430AsmPrinter : public AsmPrinter {
44 public:
45 MSP430AsmPrinter(TargetMachine &TM, std::unique_ptr<MCStreamer> Streamer)
46 : AsmPrinter(TM, std::move(Streamer), ID) {}
47
48 StringRef getPassName() const override { return "MSP430 Assembly Printer"; }
49
50 bool runOnMachineFunction(MachineFunction &MF) override;
51
52 void PrintSymbolOperand(const MachineOperand &MO, raw_ostream &O) override;
53 void printOperand(const MachineInstr *MI, int OpNum, raw_ostream &O,
54 bool PrefixHash = true);
55 void printSrcMemOperand(const MachineInstr *MI, int OpNum,
56 raw_ostream &O);
57 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
58 const char *ExtraCode, raw_ostream &O) override;
59 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
60 const char *ExtraCode, raw_ostream &O) override;
61 void emitInstruction(const MachineInstr *MI) override;
62
63 void EmitInterruptVectorSection(MachineFunction &ISR);
64
65 static char ID;
66 };
67} // end of anonymous namespace
68
69void MSP430AsmPrinter::PrintSymbolOperand(const MachineOperand &MO,
70 raw_ostream &O) {
71 uint64_t Offset = MO.getOffset();
72 if (Offset)
73 O << '(' << Offset << '+';
74
75 getSymbol(MO.getGlobal())->print(O, MAI);
76
77 if (Offset)
78 O << ')';
79}
80
81void MSP430AsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
82 raw_ostream &O, bool PrefixHash) {
83 const MachineOperand &MO = MI->getOperand(OpNum);
84 switch (MO.getType()) {
85 default: llvm_unreachable("Not implemented yet!");
88 return;
90 if (PrefixHash)
91 O << '#';
92 O << MO.getImm();
93 return;
95 MO.getMBB()->getSymbol()->print(O, MAI);
96 return;
98 // If the global address expression is a part of displacement field with a
99 // register base, we should not emit any prefix symbol here, e.g.
100 // mov.w glb(r1), r2
101 // Otherwise (!) msp430-as will silently miscompile the output :(
102 if (PrefixHash)
103 O << '#';
104 PrintSymbolOperand(MO, O);
105 return;
106 }
107 }
108}
109
110void MSP430AsmPrinter::printSrcMemOperand(const MachineInstr *MI, int OpNum,
111 raw_ostream &O) {
112 const MachineOperand &Base = MI->getOperand(OpNum);
113 const MachineOperand &Disp = MI->getOperand(OpNum+1);
114
115 // Print displacement first
116
117 // Imm here is in fact global address - print extra modifier.
118 if (Disp.isImm() && Base.getReg() == MSP430::SR)
119 O << '&';
120 printOperand(MI, OpNum + 1, O, /*PrefixHash=*/false);
121
122 // Print register base field
123 if (Base.getReg() != MSP430::SR && Base.getReg() != MSP430::PC) {
124 O << '(';
125 printOperand(MI, OpNum, O);
126 O << ')';
127 }
128}
129
130/// PrintAsmOperand - Print out an operand for an inline asm expression.
131///
132bool MSP430AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
133 const char *ExtraCode, raw_ostream &O) {
134 // Does this asm operand have a single letter operand modifier?
135 if (ExtraCode && ExtraCode[0])
136 return AsmPrinter::PrintAsmOperand(MI, OpNo, ExtraCode, O);
137
138 printOperand(MI, OpNo, O);
139 return false;
140}
141
142bool MSP430AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
143 unsigned OpNo,
144 const char *ExtraCode,
145 raw_ostream &O) {
146 if (ExtraCode && ExtraCode[0]) {
147 return true; // Unknown modifier.
148 }
149 printSrcMemOperand(MI, OpNo, O);
150 return false;
151}
152
153//===----------------------------------------------------------------------===//
154void MSP430AsmPrinter::emitInstruction(const MachineInstr *MI) {
155 MSP430_MC::verifyInstructionPredicates(MI->getOpcode(),
156 getSubtargetInfo().getFeatureBits());
157
158 MSP430MCInstLower MCInstLowering(OutContext, *this);
159
160 MCInst TmpInst;
161 MCInstLowering.Lower(MI, TmpInst);
162 EmitToStreamer(*OutStreamer, TmpInst);
163}
164
165void MSP430AsmPrinter::EmitInterruptVectorSection(MachineFunction &ISR) {
166 MCSection *Cur = OutStreamer->getCurrentSectionOnly();
167 const auto *F = &ISR.getFunction();
168 if (F->getCallingConv() != CallingConv::MSP430_INTR) {
169 report_fatal_error("Functions with 'interrupt' attribute must have msp430_intrcc CC");
170 }
171 StringRef IVIdx = F->getFnAttribute("interrupt").getValueAsString();
172 MCSection *IV = OutStreamer->getContext().getELFSection(
173 "__interrupt_vector_" + IVIdx,
175 OutStreamer->switchSection(IV);
176
177 const MCSymbol *FunctionSymbol = getSymbol(F);
178 OutStreamer->emitSymbolValue(FunctionSymbol, TM.getProgramPointerSize());
179 OutStreamer->switchSection(Cur);
180}
181
182bool MSP430AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
183 // Emit separate section for an interrupt vector if ISR
184 if (MF.getFunction().hasFnAttribute("interrupt")) {
185 EmitInterruptVectorSection(MF);
186 }
187
188 SetupMachineFunction(MF);
189 emitFunctionBody();
190 return false;
191}
192
193char MSP430AsmPrinter::ID = 0;
194
195INITIALIZE_PASS(MSP430AsmPrinter, "msp430-asm-printer",
196 "MSP430 Assembly Printer", false, false)
197
198// Force static initialization.
200LLVMInitializeMSP430AsmPrinter() {
202}
203
206 MSP430AsmPrinter &AsmPrinter = static_cast<MSP430AsmPrinter &>(
207 MAM.getResult<AsmPrinterAnalysis>(M).getPrinter());
210 return PreservedAnalyses::all();
211}
212
216 MSP430AsmPrinter &AsmPrinter = static_cast<MSP430AsmPrinter &>(
218 .getCachedResult<AsmPrinterAnalysis>(*MF.getFunction().getParent())
219 ->getPrinter());
222 return PreservedAnalyses::all();
223}
224
227 MSP430AsmPrinter &AsmPrinter = static_cast<MSP430AsmPrinter &>(
228 MAM.getResult<AsmPrinterAnalysis>(M).getPrinter());
231 return PreservedAnalyses::all();
232}
static const Function * getParent(const Value *V)
#define X(NUM, ENUM, NAME)
Definition ELF.h:856
#define LLVM_ABI
Definition Compiler.h:215
#define LLVM_EXTERNAL_VISIBILITY
Definition Compiler.h:132
IRTranslator LLVM IR MI
This header defines various interfaces for pass management in LLVM.
#define F(x, y, z)
Definition MD5.cpp:54
This file declares the MachineConstantPool class which is an abstract constant pool to keep track of ...
ModuleAnalysisManager MAM
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
static bool printOperand(raw_ostream &OS, const SelectionDAG *G, const SDValue Value)
static const uint32_t IV[8]
Definition blake3_impl.h:83
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
This class is intended to be used as a driving class for all asm writers.
Definition AsmPrinter.h:91
bool doInitialization(Module &M) override
Set up the AsmPrinter when we are working on a new module.
bool doFinalization(Module &M) override
Shut down the asmprinter.
bool runOnMachineFunction(MachineFunction &MF) override
Emit the specified function out to the OutStreamer.
Definition AsmPrinter.h:453
virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, const char *ExtraCode, raw_ostream &OS)
Print the specified operand of MI, an INLINEASM instruction, using the specified assembler variant.
LLVM_ABI StringRef getValueAsString() const
Return the attribute's value as a string.
Attribute getFnAttribute(Attribute::AttrKind Kind) const
Return the attribute for the given attribute kind.
Definition Function.cpp:758
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
Definition Function.cpp:723
LLVM_ABI void print(raw_ostream &OS, const MCAsmInfo *MAI) const
print - Print the value to the stream OS.
Definition MCSymbol.cpp:59
PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM)
PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM)
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
static const char * getRegisterName(MCRegister Reg)
LLVM_ABI MCSymbol * getSymbol() const
Return the MCSymbol for this basic block.
Function & getFunction()
Return the LLVM function that this machine code represents.
Representation of each machine instruction.
MachineOperand class - Representation of each machine instruction operand.
const GlobalValue * getGlobal() const
int64_t getImm() const
MachineBasicBlock * getMBB() const
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
MachineOperandType getType() const
getType - Returns the MachineOperandType for this operand.
Register getReg() const
getReg - Returns the register number.
@ MO_Immediate
Immediate operand.
@ MO_GlobalAddress
Address of a global value.
@ MO_MachineBasicBlock
MachineBasicBlock reference.
@ MO_Register
Register operand.
int64_t getOffset() const
Return the offset from the symbol in this operand.
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
Primary interface to the complete machine description for the target machine.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
Pass manager infrastructure for declaring and invalidating analyses.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
@ SHF_ALLOC
Definition ELF.h:1256
@ SHF_EXECINSTR
Definition ELF.h:1259
@ SHT_PROGBITS
Definition ELF.h:1155
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:573
OuterAnalysisManagerProxy< ModuleAnalysisManager, MachineFunction > ModuleAnalysisManagerMachineFunctionProxy
Provide the ModuleAnalysisManager to Function proxy.
Target & getTheMSP430Target()
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
LLVM_ABI void setupModuleAsmPrinter(Module &M, ModuleAnalysisManager &MAM, AsmPrinter &AsmPrinter)
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:163
LLVM_ABI void setupMachineFunctionAsmPrinter(MachineFunctionAnalysisManager &MFAM, MachineFunction &MF, AsmPrinter &AsmPrinter)
AnalysisManager< Module > ModuleAnalysisManager
Convenience typedef for the Module analysis manager.
Definition MIRParser.h:39
RegisterAsmPrinter - Helper template for registering a target specific assembly printer,...