[DAGCombiner] Eliminate dead stores to stack.
[llvm-complete.git] / lib / Target / MSP430 / MSP430AsmPrinter.cpp
blob4a3f58d67352abe84758fd4efca147a74602c4eb
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.
12 //===----------------------------------------------------------------------===//
14 #include "InstPrinter/MSP430InstPrinter.h"
15 #include "MSP430.h"
16 #include "MSP430InstrInfo.h"
17 #include "MSP430MCInstLower.h"
18 #include "MSP430TargetMachine.h"
19 #include "llvm/BinaryFormat/ELF.h"
20 #include "llvm/CodeGen/AsmPrinter.h"
21 #include "llvm/CodeGen/MachineConstantPool.h"
22 #include "llvm/CodeGen/MachineFunctionPass.h"
23 #include "llvm/CodeGen/MachineInstr.h"
24 #include "llvm/CodeGen/MachineModuleInfo.h"
25 #include "llvm/IR/Constants.h"
26 #include "llvm/IR/DerivedTypes.h"
27 #include "llvm/IR/Mangler.h"
28 #include "llvm/IR/Module.h"
29 #include "llvm/MC/MCAsmInfo.h"
30 #include "llvm/MC/MCInst.h"
31 #include "llvm/MC/MCSectionELF.h"
32 #include "llvm/MC/MCStreamer.h"
33 #include "llvm/MC/MCSymbol.h"
34 #include "llvm/Support/TargetRegistry.h"
35 #include "llvm/Support/raw_ostream.h"
36 using namespace llvm;
38 #define DEBUG_TYPE "asm-printer"
40 namespace {
41 class MSP430AsmPrinter : public AsmPrinter {
42 public:
43 MSP430AsmPrinter(TargetMachine &TM, std::unique_ptr<MCStreamer> Streamer)
44 : AsmPrinter(TM, std::move(Streamer)) {}
46 StringRef getPassName() const override { return "MSP430 Assembly Printer"; }
48 bool runOnMachineFunction(MachineFunction &MF) override;
50 void printOperand(const MachineInstr *MI, int OpNum,
51 raw_ostream &O, const char* Modifier = nullptr);
52 void printSrcMemOperand(const MachineInstr *MI, int OpNum,
53 raw_ostream &O);
54 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
55 unsigned AsmVariant, const char *ExtraCode,
56 raw_ostream &O) override;
57 bool PrintAsmMemoryOperand(const MachineInstr *MI,
58 unsigned OpNo, unsigned AsmVariant,
59 const char *ExtraCode, raw_ostream &O) override;
60 void EmitInstruction(const MachineInstr *MI) override;
62 void EmitInterruptVectorSection(MachineFunction &ISR);
64 } // end of anonymous namespace
67 void MSP430AsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
68 raw_ostream &O, const char *Modifier) {
69 const MachineOperand &MO = MI->getOperand(OpNum);
70 switch (MO.getType()) {
71 default: llvm_unreachable("Not implemented yet!");
72 case MachineOperand::MO_Register:
73 O << MSP430InstPrinter::getRegisterName(MO.getReg());
74 return;
75 case MachineOperand::MO_Immediate:
76 if (!Modifier || strcmp(Modifier, "nohash"))
77 O << '#';
78 O << MO.getImm();
79 return;
80 case MachineOperand::MO_MachineBasicBlock:
81 MO.getMBB()->getSymbol()->print(O, MAI);
82 return;
83 case MachineOperand::MO_GlobalAddress: {
84 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
85 uint64_t Offset = MO.getOffset();
87 // If the global address expression is a part of displacement field with a
88 // register base, we should not emit any prefix symbol here, e.g.
89 // mov.w &foo, r1
90 // vs
91 // mov.w glb(r1), r2
92 // Otherwise (!) msp430-as will silently miscompile the output :(
93 if (!Modifier || strcmp(Modifier, "nohash"))
94 O << (isMemOp ? '&' : '#');
95 if (Offset)
96 O << '(' << Offset << '+';
98 getSymbol(MO.getGlobal())->print(O, MAI);
100 if (Offset)
101 O << ')';
103 return;
108 void MSP430AsmPrinter::printSrcMemOperand(const MachineInstr *MI, int OpNum,
109 raw_ostream &O) {
110 const MachineOperand &Base = MI->getOperand(OpNum);
111 const MachineOperand &Disp = MI->getOperand(OpNum+1);
113 // Print displacement first
115 // Imm here is in fact global address - print extra modifier.
116 if (Disp.isImm() && Base.getReg() == MSP430::SR)
117 O << '&';
118 printOperand(MI, OpNum+1, O, "nohash");
120 // Print register base field
121 if (Base.getReg() != MSP430::SR && Base.getReg() != MSP430::PC) {
122 O << '(';
123 printOperand(MI, OpNum, O);
124 O << ')';
128 /// PrintAsmOperand - Print out an operand for an inline asm expression.
130 bool MSP430AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
131 unsigned AsmVariant,
132 const char *ExtraCode, raw_ostream &O) {
133 // Does this asm operand have a single letter operand modifier?
134 if (ExtraCode && ExtraCode[0])
135 return true; // Unknown modifier.
137 printOperand(MI, OpNo, O);
138 return false;
141 bool MSP430AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
142 unsigned OpNo, unsigned AsmVariant,
143 const char *ExtraCode,
144 raw_ostream &O) {
145 if (ExtraCode && ExtraCode[0]) {
146 return true; // Unknown modifier.
148 printSrcMemOperand(MI, OpNo, O);
149 return false;
152 //===----------------------------------------------------------------------===//
153 void MSP430AsmPrinter::EmitInstruction(const MachineInstr *MI) {
154 MSP430MCInstLower MCInstLowering(OutContext, *this);
156 MCInst TmpInst;
157 MCInstLowering.Lower(MI, TmpInst);
158 EmitToStreamer(*OutStreamer, TmpInst);
161 void MSP430AsmPrinter::EmitInterruptVectorSection(MachineFunction &ISR) {
162 MCSection *Cur = OutStreamer->getCurrentSectionOnly();
163 const auto *F = &ISR.getFunction();
164 assert(F->hasFnAttribute("interrupt") &&
165 "Functions with MSP430_INTR CC should have 'interrupt' attribute");
166 StringRef IVIdx = F->getFnAttribute("interrupt").getValueAsString();
167 MCSection *IV = OutStreamer->getContext().getELFSection(
168 "__interrupt_vector_" + IVIdx,
169 ELF::SHT_PROGBITS, ELF::SHF_ALLOC | ELF::SHF_EXECINSTR);
170 OutStreamer->SwitchSection(IV);
172 const MCSymbol *FunctionSymbol = getSymbol(F);
173 OutStreamer->EmitSymbolValue(FunctionSymbol, TM.getProgramPointerSize());
174 OutStreamer->SwitchSection(Cur);
177 bool MSP430AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
178 // Emit separate section for an interrupt vector if ISR
179 if (MF.getFunction().getCallingConv() == CallingConv::MSP430_INTR)
180 EmitInterruptVectorSection(MF);
182 SetupMachineFunction(MF);
183 EmitFunctionBody();
184 return false;
187 // Force static initialization.
188 extern "C" void LLVMInitializeMSP430AsmPrinter() {
189 RegisterAsmPrinter<MSP430AsmPrinter> X(getTheMSP430Target());