LLVM 20.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"
31using namespace llvm;
32
33#define DEBUG_TYPE "asm-printer"
34
35namespace {
36 class MSP430AsmPrinter : public AsmPrinter {
37 public:
38 MSP430AsmPrinter(TargetMachine &TM, std::unique_ptr<MCStreamer> Streamer)
39 : AsmPrinter(TM, std::move(Streamer)) {}
40
41 StringRef getPassName() const override { return "MSP430 Assembly Printer"; }
42
43 bool runOnMachineFunction(MachineFunction &MF) override;
44
45 void PrintSymbolOperand(const MachineOperand &MO, raw_ostream &O) override;
46 void printOperand(const MachineInstr *MI, int OpNum,
47 raw_ostream &O, const char* Modifier = nullptr);
48 void printSrcMemOperand(const MachineInstr *MI, int OpNum,
49 raw_ostream &O);
50 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
51 const char *ExtraCode, raw_ostream &O) override;
52 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
53 const char *ExtraCode, raw_ostream &O) override;
54 void emitInstruction(const MachineInstr *MI) override;
55
56 void EmitInterruptVectorSection(MachineFunction &ISR);
57 };
58} // end of anonymous namespace
59
60void MSP430AsmPrinter::PrintSymbolOperand(const MachineOperand &MO,
61 raw_ostream &O) {
63 if (Offset)
64 O << '(' << Offset << '+';
65
66 getSymbol(MO.getGlobal())->print(O, MAI);
67
68 if (Offset)
69 O << ')';
70}
71
72void MSP430AsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
73 raw_ostream &O, const char *Modifier) {
74 const MachineOperand &MO = MI->getOperand(OpNum);
75 switch (MO.getType()) {
76 default: llvm_unreachable("Not implemented yet!");
79 return;
81 if (!Modifier || strcmp(Modifier, "nohash"))
82 O << '#';
83 O << MO.getImm();
84 return;
86 MO.getMBB()->getSymbol()->print(O, MAI);
87 return;
89 // If the global address expression is a part of displacement field with a
90 // register base, we should not emit any prefix symbol here, e.g.
91 // mov.w glb(r1), r2
92 // Otherwise (!) msp430-as will silently miscompile the output :(
93 if (!Modifier || strcmp(Modifier, "nohash"))
94 O << '#';
95 PrintSymbolOperand(MO, O);
96 return;
97 }
98 }
99}
100
101void MSP430AsmPrinter::printSrcMemOperand(const MachineInstr *MI, int OpNum,
102 raw_ostream &O) {
103 const MachineOperand &Base = MI->getOperand(OpNum);
104 const MachineOperand &Disp = MI->getOperand(OpNum+1);
105
106 // Print displacement first
107
108 // Imm here is in fact global address - print extra modifier.
109 if (Disp.isImm() && Base.getReg() == MSP430::SR)
110 O << '&';
111 printOperand(MI, OpNum+1, O, "nohash");
112
113 // Print register base field
114 if (Base.getReg() != MSP430::SR && Base.getReg() != MSP430::PC) {
115 O << '(';
116 printOperand(MI, OpNum, O);
117 O << ')';
118 }
119}
120
121/// PrintAsmOperand - Print out an operand for an inline asm expression.
122///
123bool MSP430AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
124 const char *ExtraCode, raw_ostream &O) {
125 // Does this asm operand have a single letter operand modifier?
126 if (ExtraCode && ExtraCode[0])
127 return AsmPrinter::PrintAsmOperand(MI, OpNo, ExtraCode, O);
128
129 printOperand(MI, OpNo, O);
130 return false;
131}
132
133bool MSP430AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
134 unsigned OpNo,
135 const char *ExtraCode,
136 raw_ostream &O) {
137 if (ExtraCode && ExtraCode[0]) {
138 return true; // Unknown modifier.
139 }
140 printSrcMemOperand(MI, OpNo, O);
141 return false;
142}
143
144//===----------------------------------------------------------------------===//
145void MSP430AsmPrinter::emitInstruction(const MachineInstr *MI) {
146 MSP430_MC::verifyInstructionPredicates(MI->getOpcode(),
147 getSubtargetInfo().getFeatureBits());
148
149 MSP430MCInstLower MCInstLowering(OutContext, *this);
150
151 MCInst TmpInst;
152 MCInstLowering.Lower(MI, TmpInst);
153 EmitToStreamer(*OutStreamer, TmpInst);
154}
155
156void MSP430AsmPrinter::EmitInterruptVectorSection(MachineFunction &ISR) {
157 MCSection *Cur = OutStreamer->getCurrentSectionOnly();
158 const auto *F = &ISR.getFunction();
159 if (F->getCallingConv() != CallingConv::MSP430_INTR) {
160 report_fatal_error("Functions with 'interrupt' attribute must have msp430_intrcc CC");
161 }
162 StringRef IVIdx = F->getFnAttribute("interrupt").getValueAsString();
163 MCSection *IV = OutStreamer->getContext().getELFSection(
164 "__interrupt_vector_" + IVIdx,
166 OutStreamer->switchSection(IV);
167
168 const MCSymbol *FunctionSymbol = getSymbol(F);
169 OutStreamer->emitSymbolValue(FunctionSymbol, TM.getProgramPointerSize());
170 OutStreamer->switchSection(Cur);
171}
172
173bool MSP430AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
174 // Emit separate section for an interrupt vector if ISR
175 if (MF.getFunction().hasFnAttribute("interrupt")) {
176 EmitInterruptVectorSection(MF);
177 }
178
179 SetupMachineFunction(MF);
180 emitFunctionBody();
181 return false;
182}
183
184// Force static initialization.
187}
#define LLVM_EXTERNAL_VISIBILITY
Definition: Compiler.h:128
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
IRTranslator LLVM IR MI
#define F(x, y, z)
Definition: MD5.cpp:55
LLVM_EXTERNAL_VISIBILITY void LLVMInitializeMSP430AsmPrinter()
This file declares the MachineConstantPool class which is an abstract constant pool to keep track of ...
static bool printOperand(raw_ostream &OS, const SelectionDAG *G, const SDValue Value)
static const uint32_t IV[8]
Definition: blake3_impl.h:78
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:86
virtual void emitInstruction(const MachineInstr *)
Targets should implement this to emit instructions.
Definition: AsmPrinter.h:561
virtual void PrintSymbolOperand(const MachineOperand &MO, raw_ostream &OS)
Print the MachineOperand as a symbol.
bool runOnMachineFunction(MachineFunction &MF) override
Emit the specified function out to the OutStreamer.
Definition: AsmPrinter.h:387
virtual bool PrintAsmMemoryOperand(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 as...
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.
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
Definition: Function.cpp:731
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:185
Instances of this class represent a uniqued identifier for a section in the current translation unit.
Definition: MCSection.h:36
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
void print(raw_ostream &OS, const MCAsmInfo *MAI) const
print - Print the value to the stream OS.
Definition: MCSymbol.cpp:58
static const char * getRegisterName(MCRegister Reg)
MSP430MCInstLower - This class is used to lower an MachineInstr into an MCInst.
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.
Definition: MachineInstr.h:69
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.
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
Definition: Pass.cpp:81
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:77
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ MSP430_INTR
Used for MSP430 interrupt routines.
Definition: CallingConv.h:117
@ SHT_PROGBITS
Definition: ELF.h:1090
@ SHF_ALLOC
Definition: ELF.h:1188
@ SHF_EXECINSTR
Definition: ELF.h:1191
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:480
Target & getTheMSP430Target()
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:167
RegisterAsmPrinter - Helper template for registering a target specific assembly printer,...