1 //===-- MSP430AsmPrinter.cpp - MSP430 LLVM assembly writer ----------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains a printer that converts from our internal representation
11 // of machine-dependent LLVM code to the MSP430 assembly language.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "InstPrinter/MSP430InstPrinter.h"
16 #include "MSP430.h"
17 #include "MSP430InstrInfo.h"
18 #include "MSP430MCInstLower.h"
19 #include "MSP430TargetMachine.h"
20 #include "llvm/BinaryFormat/ELF.h"
21 #include "llvm/CodeGen/AsmPrinter.h"
22 #include "llvm/CodeGen/MachineConstantPool.h"
23 #include "llvm/CodeGen/MachineFunctionPass.h"
24 #include "llvm/CodeGen/MachineInstr.h"
25 #include "llvm/CodeGen/MachineModuleInfo.h"
26 #include "llvm/IR/Constants.h"
27 #include "llvm/IR/DerivedTypes.h"
28 #include "llvm/IR/Mangler.h"
29 #include "llvm/IR/Module.h"
30 #include "llvm/MC/MCAsmInfo.h"
31 #include "llvm/MC/MCInst.h"
32 #include "llvm/MC/MCSectionELF.h"
33 #include "llvm/MC/MCStreamer.h"
34 #include "llvm/MC/MCSymbol.h"
35 #include "llvm/Support/TargetRegistry.h"
36 #include "llvm/Support/raw_ostream.h"
37 using namespace llvm;
38
39 #define DEBUG_TYPE "asm-printer"
40
41 namespace {
42 class MSP430AsmPrinter : public AsmPrinter {
43 public:
MSP430AsmPrinter(TargetMachine & TM,std::unique_ptr<MCStreamer> Streamer)44 MSP430AsmPrinter(TargetMachine &TM, std::unique_ptr<MCStreamer> Streamer)
45 : AsmPrinter(TM, std::move(Streamer)) {}
46
getPassName() const47 StringRef getPassName() const override { return "MSP430 Assembly Printer"; }
48
49 bool runOnMachineFunction(MachineFunction &MF) override;
50
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 unsigned AsmVariant, const char *ExtraCode,
57 raw_ostream &O) override;
58 bool PrintAsmMemoryOperand(const MachineInstr *MI,
59 unsigned OpNo, unsigned AsmVariant,
60 const char *ExtraCode, raw_ostream &O) override;
61 void EmitInstruction(const MachineInstr *MI) override;
62
63 void EmitInterruptVectorSection(MachineFunction &ISR);
64 };
65 } // end of anonymous namespace
66
67
printOperand(const MachineInstr * MI,int OpNum,raw_ostream & O,const char * Modifier)68 void MSP430AsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
69 raw_ostream &O, const char *Modifier) {
70 const MachineOperand &MO = MI->getOperand(OpNum);
71 switch (MO.getType()) {
72 default: llvm_unreachable("Not implemented yet!");
73 case MachineOperand::MO_Register:
74 O << MSP430InstPrinter::getRegisterName(MO.getReg());
75 return;
76 case MachineOperand::MO_Immediate:
77 if (!Modifier || strcmp(Modifier, "nohash"))
78 O << '#';
79 O << MO.getImm();
80 return;
81 case MachineOperand::MO_MachineBasicBlock:
82 MO.getMBB()->getSymbol()->print(O, MAI);
83 return;
84 case MachineOperand::MO_GlobalAddress: {
85 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
86 uint64_t Offset = MO.getOffset();
87
88 // If the global address expression is a part of displacement field with a
89 // register base, we should not emit any prefix symbol here, e.g.
90 // mov.w &foo, r1
91 // vs
92 // mov.w glb(r1), r2
93 // Otherwise (!) msp430-as will silently miscompile the output :(
94 if (!Modifier || strcmp(Modifier, "nohash"))
95 O << (isMemOp ? '&' : '#');
96 if (Offset)
97 O << '(' << Offset << '+';
98
99 getSymbol(MO.getGlobal())->print(O, MAI);
100
101 if (Offset)
102 O << ')';
103
104 return;
105 }
106 }
107 }
108
printSrcMemOperand(const MachineInstr * MI,int OpNum,raw_ostream & O)109 void MSP430AsmPrinter::printSrcMemOperand(const MachineInstr *MI, int OpNum,
110 raw_ostream &O) {
111 const MachineOperand &Base = MI->getOperand(OpNum);
112 const MachineOperand &Disp = MI->getOperand(OpNum+1);
113
114 // Print displacement first
115
116 // Imm here is in fact global address - print extra modifier.
117 if (Disp.isImm() && !Base.getReg())
118 O << '&';
119 printOperand(MI, OpNum+1, O, "nohash");
120
121 // Print register base field
122 if (Base.getReg()) {
123 O << '(';
124 printOperand(MI, OpNum, O);
125 O << ')';
126 }
127 }
128
129 /// PrintAsmOperand - Print out an operand for an inline asm expression.
130 ///
PrintAsmOperand(const MachineInstr * MI,unsigned OpNo,unsigned AsmVariant,const char * ExtraCode,raw_ostream & O)131 bool MSP430AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
132 unsigned AsmVariant,
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 true; // Unknown modifier.
137
138 printOperand(MI, OpNo, O);
139 return false;
140 }
141
PrintAsmMemoryOperand(const MachineInstr * MI,unsigned OpNo,unsigned AsmVariant,const char * ExtraCode,raw_ostream & O)142 bool MSP430AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
143 unsigned OpNo, unsigned AsmVariant,
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 //===----------------------------------------------------------------------===//
EmitInstruction(const MachineInstr * MI)154 void MSP430AsmPrinter::EmitInstruction(const MachineInstr *MI) {
155 MSP430MCInstLower MCInstLowering(OutContext, *this);
156
157 MCInst TmpInst;
158 MCInstLowering.Lower(MI, TmpInst);
159 EmitToStreamer(*OutStreamer, TmpInst);
160 }
161
EmitInterruptVectorSection(MachineFunction & ISR)162 void MSP430AsmPrinter::EmitInterruptVectorSection(MachineFunction &ISR) {
163 MCSection *Cur = OutStreamer->getCurrentSectionOnly();
164 const auto *F = &ISR.getFunction();
165 assert(F->hasFnAttribute("interrupt") &&
166 "Functions with MSP430_INTR CC should have 'interrupt' attribute");
167 StringRef IVIdx = F->getFnAttribute("interrupt").getValueAsString();
168 MCSection *IV = OutStreamer->getContext().getELFSection(
169 "__interrupt_vector_" + IVIdx,
170 ELF::SHT_PROGBITS, ELF::SHF_ALLOC | ELF::SHF_EXECINSTR);
171 OutStreamer->SwitchSection(IV);
172
173 const MCSymbol *FunctionSymbol = getSymbol(F);
174 OutStreamer->EmitSymbolValue(FunctionSymbol, TM.getProgramPointerSize());
175 OutStreamer->SwitchSection(Cur);
176 }
177
runOnMachineFunction(MachineFunction & MF)178 bool MSP430AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
179 // Emit separate section for an interrupt vector if ISR
180 if (MF.getFunction().getCallingConv() == CallingConv::MSP430_INTR)
181 EmitInterruptVectorSection(MF);
182
183 SetupMachineFunction(MF);
184 EmitFunctionBody();
185 return false;
186 }
187
188 // Force static initialization.
LLVMInitializeMSP430AsmPrinter()189 extern "C" void LLVMInitializeMSP430AsmPrinter() {
190 RegisterAsmPrinter<MSP430AsmPrinter> X(getTheMSP430Target());
191 }
192