LLVM 22.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
15#include "MSP430MCInstLower.h"
16#include "MSP430TargetMachine.h"
23#include "llvm/IR/Mangler.h"
24#include "llvm/MC/MCAsmInfo.h"
25#include "llvm/MC/MCInst.h"
27#include "llvm/MC/MCStreamer.h"
28#include "llvm/MC/MCSymbol.h"
32using namespace llvm;
33
34#define DEBUG_TYPE "asm-printer"
35
36namespace {
37 class MSP430AsmPrinter : public AsmPrinter {
38 public:
39 MSP430AsmPrinter(TargetMachine &TM, std::unique_ptr<MCStreamer> Streamer)
40 : AsmPrinter(TM, std::move(Streamer), ID) {}
41
42 StringRef getPassName() const override { return "MSP430 Assembly Printer"; }
43
44 bool runOnMachineFunction(MachineFunction &MF) override;
45
46 void PrintSymbolOperand(const MachineOperand &MO, raw_ostream &O) override;
47 void printOperand(const MachineInstr *MI, int OpNum, raw_ostream &O,
48 bool PrefixHash = true);
49 void printSrcMemOperand(const MachineInstr *MI, int OpNum,
50 raw_ostream &O);
51 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
52 const char *ExtraCode, raw_ostream &O) override;
53 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
54 const char *ExtraCode, raw_ostream &O) override;
55 void emitInstruction(const MachineInstr *MI) override;
56
57 void EmitInterruptVectorSection(MachineFunction &ISR);
58
59 static char ID;
60 };
61} // end of anonymous namespace
62
63void MSP430AsmPrinter::PrintSymbolOperand(const MachineOperand &MO,
64 raw_ostream &O) {
65 uint64_t Offset = MO.getOffset();
66 if (Offset)
67 O << '(' << Offset << '+';
68
69 getSymbol(MO.getGlobal())->print(O, MAI);
70
71 if (Offset)
72 O << ')';
73}
74
75void MSP430AsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
76 raw_ostream &O, bool PrefixHash) {
77 const MachineOperand &MO = MI->getOperand(OpNum);
78 switch (MO.getType()) {
79 default: llvm_unreachable("Not implemented yet!");
82 return;
84 if (PrefixHash)
85 O << '#';
86 O << MO.getImm();
87 return;
89 MO.getMBB()->getSymbol()->print(O, MAI);
90 return;
92 // If the global address expression is a part of displacement field with a
93 // register base, we should not emit any prefix symbol here, e.g.
94 // mov.w glb(r1), r2
95 // Otherwise (!) msp430-as will silently miscompile the output :(
96 if (PrefixHash)
97 O << '#';
98 PrintSymbolOperand(MO, O);
99 return;
100 }
101 }
102}
103
104void MSP430AsmPrinter::printSrcMemOperand(const MachineInstr *MI, int OpNum,
105 raw_ostream &O) {
106 const MachineOperand &Base = MI->getOperand(OpNum);
107 const MachineOperand &Disp = MI->getOperand(OpNum+1);
108
109 // Print displacement first
110
111 // Imm here is in fact global address - print extra modifier.
112 if (Disp.isImm() && Base.getReg() == MSP430::SR)
113 O << '&';
114 printOperand(MI, OpNum + 1, O, /*PrefixHash=*/false);
115
116 // Print register base field
117 if (Base.getReg() != MSP430::SR && Base.getReg() != MSP430::PC) {
118 O << '(';
119 printOperand(MI, OpNum, O);
120 O << ')';
121 }
122}
123
124/// PrintAsmOperand - Print out an operand for an inline asm expression.
125///
126bool MSP430AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
127 const char *ExtraCode, raw_ostream &O) {
128 // Does this asm operand have a single letter operand modifier?
129 if (ExtraCode && ExtraCode[0])
130 return AsmPrinter::PrintAsmOperand(MI, OpNo, ExtraCode, O);
131
132 printOperand(MI, OpNo, O);
133 return false;
134}
135
136bool MSP430AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
137 unsigned OpNo,
138 const char *ExtraCode,
139 raw_ostream &O) {
140 if (ExtraCode && ExtraCode[0]) {
141 return true; // Unknown modifier.
142 }
143 printSrcMemOperand(MI, OpNo, O);
144 return false;
145}
146
147//===----------------------------------------------------------------------===//
148void MSP430AsmPrinter::emitInstruction(const MachineInstr *MI) {
149 MSP430_MC::verifyInstructionPredicates(MI->getOpcode(),
150 getSubtargetInfo().getFeatureBits());
151
152 MSP430MCInstLower MCInstLowering(OutContext, *this);
153
154 MCInst TmpInst;
155 MCInstLowering.Lower(MI, TmpInst);
156 EmitToStreamer(*OutStreamer, TmpInst);
157}
158
159void MSP430AsmPrinter::EmitInterruptVectorSection(MachineFunction &ISR) {
160 MCSection *Cur = OutStreamer->getCurrentSectionOnly();
161 const auto *F = &ISR.getFunction();
162 if (F->getCallingConv() != CallingConv::MSP430_INTR) {
163 report_fatal_error("Functions with 'interrupt' attribute must have msp430_intrcc CC");
164 }
165 StringRef IVIdx = F->getFnAttribute("interrupt").getValueAsString();
166 MCSection *IV = OutStreamer->getContext().getELFSection(
167 "__interrupt_vector_" + IVIdx,
169 OutStreamer->switchSection(IV);
170
171 const MCSymbol *FunctionSymbol = getSymbol(F);
172 OutStreamer->emitSymbolValue(FunctionSymbol, TM.getProgramPointerSize());
173 OutStreamer->switchSection(Cur);
174}
175
176bool MSP430AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
177 // Emit separate section for an interrupt vector if ISR
178 if (MF.getFunction().hasFnAttribute("interrupt")) {
179 EmitInterruptVectorSection(MF);
180 }
181
182 SetupMachineFunction(MF);
183 emitFunctionBody();
184 return false;
185}
186
187char MSP430AsmPrinter::ID = 0;
188
189INITIALIZE_PASS(MSP430AsmPrinter, "msp430-asm-printer",
190 "MSP430 Assembly Printer", false, false)
191
192// Force static initialization.
194LLVMInitializeMSP430AsmPrinter() {
196}
#define LLVM_ABI
Definition Compiler.h:213
#define LLVM_EXTERNAL_VISIBILITY
Definition Compiler.h:132
IRTranslator LLVM IR MI
#define F(x, y, z)
Definition MD5.cpp:55
This file declares the MachineConstantPool class which is an abstract constant pool to keep track of ...
#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 TableGen::Emitter::OptClass< SkeletonEmitter > X("gen-skeleton-class", "Generate example skeleton class")
static const uint32_t IV[8]
Definition blake3_impl.h:83
This class is intended to be used as a driving class for all asm writers.
Definition AsmPrinter.h:91
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:762
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
Definition Function.cpp:727
LLVM_ABI void print(raw_ostream &OS, const MCAsmInfo *MAI) const
print - Print the value to the stream OS.
Definition MCSymbol.cpp:59
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.
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
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
#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
@ SHT_PROGBITS
Definition ELF.h:1143
@ SHF_ALLOC
Definition ELF.h:1243
@ SHF_EXECINSTR
Definition ELF.h:1246
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:477
Target & getTheMSP430Target()
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:167
RegisterAsmPrinter - Helper template for registering a target specific assembly printer,...