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 "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;
37 
38 #define DEBUG_TYPE "asm-printer"
39 
40 namespace {
41   class MSP430AsmPrinter : public AsmPrinter {
42   public:
43     MSP430AsmPrinter(TargetMachine &TM, std::unique_ptr<MCStreamer> Streamer)
44         : AsmPrinter(TM, std::move(Streamer)) {}
45 
46     StringRef getPassName() const override { return "MSP430 Assembly Printer"; }
47 
48     bool runOnMachineFunction(MachineFunction &MF) override;
49 
50     void PrintSymbolOperand(const MachineOperand &MO, raw_ostream &O) override;
51     void printOperand(const MachineInstr *MI, int OpNum,
52                       raw_ostream &O, const char* Modifier = nullptr);
53     void printSrcMemOperand(const MachineInstr *MI, int OpNum,
54                             raw_ostream &O);
55     bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
56                          const char *ExtraCode, raw_ostream &O) override;
57     bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
58                                const char *ExtraCode, raw_ostream &O) override;
59     void EmitInstruction(const MachineInstr *MI) override;
60 
61     void EmitInterruptVectorSection(MachineFunction &ISR);
62   };
63 } // end of anonymous namespace
64 
65 void MSP430AsmPrinter::PrintSymbolOperand(const MachineOperand &MO,
66                                           raw_ostream &O) {
67   uint64_t Offset = MO.getOffset();
68   if (Offset)
69     O << '(' << Offset << '+';
70 
71   getSymbol(MO.getGlobal())->print(O, MAI);
72 
73   if (Offset)
74     O << ')';
75 }
76 
77 void MSP430AsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
78                                     raw_ostream &O, const char *Modifier) {
79   const MachineOperand &MO = MI->getOperand(OpNum);
80   switch (MO.getType()) {
81   default: llvm_unreachable("Not implemented yet!");
82   case MachineOperand::MO_Register:
83     O << MSP430InstPrinter::getRegisterName(MO.getReg());
84     return;
85   case MachineOperand::MO_Immediate:
86     if (!Modifier || strcmp(Modifier, "nohash"))
87       O << '#';
88     O << MO.getImm();
89     return;
90   case MachineOperand::MO_MachineBasicBlock:
91     MO.getMBB()->getSymbol()->print(O, MAI);
92     return;
93   case MachineOperand::MO_GlobalAddress: {
94     // If the global address expression is a part of displacement field with a
95     // register base, we should not emit any prefix symbol here, e.g.
96     //   mov.w glb(r1), r2
97     // Otherwise (!) msp430-as will silently miscompile the output :(
98     if (!Modifier || strcmp(Modifier, "nohash"))
99       O << '#';
100     PrintSymbolOperand(MO, O);
101     return;
102   }
103   }
104 }
105 
106 void MSP430AsmPrinter::printSrcMemOperand(const MachineInstr *MI, int OpNum,
107                                           raw_ostream &O) {
108   const MachineOperand &Base = MI->getOperand(OpNum);
109   const MachineOperand &Disp = MI->getOperand(OpNum+1);
110 
111   // Print displacement first
112 
113   // Imm here is in fact global address - print extra modifier.
114   if (Disp.isImm() && Base.getReg() == MSP430::SR)
115     O << '&';
116   printOperand(MI, OpNum+1, O, "nohash");
117 
118   // Print register base field
119   if (Base.getReg() != MSP430::SR && Base.getReg() != MSP430::PC) {
120     O << '(';
121     printOperand(MI, OpNum, O);
122     O << ')';
123   }
124 }
125 
126 /// PrintAsmOperand - Print out an operand for an inline asm expression.
127 ///
128 bool MSP430AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
129                                        const char *ExtraCode, raw_ostream &O) {
130   // Does this asm operand have a single letter operand modifier?
131   if (ExtraCode && ExtraCode[0])
132     return AsmPrinter::PrintAsmOperand(MI, OpNo, ExtraCode, O);
133 
134   printOperand(MI, OpNo, O);
135   return false;
136 }
137 
138 bool MSP430AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
139                                              unsigned OpNo,
140                                              const char *ExtraCode,
141                                              raw_ostream &O) {
142   if (ExtraCode && ExtraCode[0]) {
143     return true; // Unknown modifier.
144   }
145   printSrcMemOperand(MI, OpNo, O);
146   return false;
147 }
148 
149 //===----------------------------------------------------------------------===//
150 void MSP430AsmPrinter::EmitInstruction(const MachineInstr *MI) {
151   MSP430MCInstLower MCInstLowering(OutContext, *this);
152 
153   MCInst TmpInst;
154   MCInstLowering.Lower(MI, TmpInst);
155   EmitToStreamer(*OutStreamer, TmpInst);
156 }
157 
158 void MSP430AsmPrinter::EmitInterruptVectorSection(MachineFunction &ISR) {
159   MCSection *Cur = OutStreamer->getCurrentSectionOnly();
160   const auto *F = &ISR.getFunction();
161   assert(F->hasFnAttribute("interrupt") &&
162          "Functions with MSP430_INTR CC should have 'interrupt' attribute");
163   StringRef IVIdx = F->getFnAttribute("interrupt").getValueAsString();
164   MCSection *IV = OutStreamer->getContext().getELFSection(
165     "__interrupt_vector_" + IVIdx,
166     ELF::SHT_PROGBITS, ELF::SHF_ALLOC | ELF::SHF_EXECINSTR);
167   OutStreamer->SwitchSection(IV);
168 
169   const MCSymbol *FunctionSymbol = getSymbol(F);
170   OutStreamer->EmitSymbolValue(FunctionSymbol, TM.getProgramPointerSize());
171   OutStreamer->SwitchSection(Cur);
172 }
173 
174 bool MSP430AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
175   // Emit separate section for an interrupt vector if ISR
176   if (MF.getFunction().getCallingConv() == CallingConv::MSP430_INTR)
177     EmitInterruptVectorSection(MF);
178 
179   SetupMachineFunction(MF);
180   EmitFunctionBody();
181   return false;
182 }
183 
184 // Force static initialization.
185 extern "C" void LLVMInitializeMSP430AsmPrinter() {
186   RegisterAsmPrinter<MSP430AsmPrinter> X(getTheMSP430Target());
187 }
188