Pass in the unfortunately named "LessPrivatePrefix" for the
[oota-llvm.git] / lib / Target / MSP430 / MSP430AsmPrinter.cpp
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 #define DEBUG_TYPE "asm-printer"
16 #include "MSP430.h"
17 #include "MSP430InstrInfo.h"
18 #include "MSP430TargetMachine.h"
19 #include "llvm/Constants.h"
20 #include "llvm/DerivedTypes.h"
21 #include "llvm/Module.h"
22 #include "llvm/CodeGen/AsmPrinter.h"
23 #include "llvm/CodeGen/DwarfWriter.h"
24 #include "llvm/CodeGen/MachineModuleInfo.h"
25 #include "llvm/CodeGen/MachineFunctionPass.h"
26 #include "llvm/CodeGen/MachineConstantPool.h"
27 #include "llvm/CodeGen/MachineInstr.h"
28 #include "llvm/Target/TargetAsmInfo.h"
29 #include "llvm/Target/TargetData.h"
30 #include "llvm/ADT/Statistic.h"
31 #include "llvm/Support/Compiler.h"
32 #include "llvm/Support/FormattedStream.h"
33 #include "llvm/Support/Mangler.h"
34 #include "llvm/Support/ErrorHandling.h"
35
36 using namespace llvm;
37
38 STATISTIC(EmittedInsts, "Number of machine instrs printed");
39
40 namespace {
41   class VISIBILITY_HIDDEN MSP430AsmPrinter : public AsmPrinter {
42   public:
43     MSP430AsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
44                      const TargetAsmInfo *TAI, bool V)
45       : AsmPrinter(O, TM, TAI, V) {}
46
47     virtual const char *getPassName() const {
48       return "MSP430 Assembly Printer";
49     }
50
51     void printOperand(const MachineInstr *MI, int OpNum,
52                       const char* Modifier = 0);
53     void printSrcMemOperand(const MachineInstr *MI, int OpNum,
54                             const char* Modifier = 0);
55     void printCCOperand(const MachineInstr *MI, int OpNum);
56     bool printInstruction(const MachineInstr *MI);  // autogenerated.
57     void printMachineInstruction(const MachineInstr * MI);
58
59     void emitFunctionHeader(const MachineFunction &MF);
60     bool runOnMachineFunction(MachineFunction &F);
61     bool doInitialization(Module &M);
62     bool doFinalization(Module &M);
63
64     void getAnalysisUsage(AnalysisUsage &AU) const {
65       AsmPrinter::getAnalysisUsage(AU);
66       AU.setPreservesAll();
67     }
68   };
69 } // end of anonymous namespace
70
71 #include "MSP430GenAsmWriter.inc"
72
73 /// createMSP430CodePrinterPass - Returns a pass that prints the MSP430
74 /// assembly code for a MachineFunction to the given output stream,
75 /// using the given target machine description.  This should work
76 /// regardless of whether the function is in SSA form.
77 ///
78 FunctionPass *llvm::createMSP430CodePrinterPass(formatted_raw_ostream &o,
79                                                 TargetMachine &tm,
80                                                 bool verbose) {
81   return new MSP430AsmPrinter(o, tm, tm.getTargetAsmInfo(), verbose);
82 }
83
84 bool MSP430AsmPrinter::doInitialization(Module &M) {
85   Mang = new Mangler(M, "", TAI->getPrivateGlobalPrefix(),
86                      TAI->getLessPrivateGlobalPrefix());
87   return false; // success
88 }
89
90
91 bool MSP430AsmPrinter::doFinalization(Module &M) {
92   return AsmPrinter::doFinalization(M);
93 }
94
95 void MSP430AsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
96   const Function *F = MF.getFunction();
97
98   SwitchToSection(TAI->SectionForGlobal(F));
99
100   unsigned FnAlign = MF.getAlignment();
101   EmitAlignment(FnAlign, F);
102
103   switch (F->getLinkage()) {
104   default: llvm_unreachable("Unknown linkage type!");
105   case Function::InternalLinkage:  // Symbols default to internal.
106   case Function::PrivateLinkage:
107   case Function::LinkerPrivateLinkage:
108     break;
109   case Function::ExternalLinkage:
110     O << "\t.globl\t" << CurrentFnName << '\n';
111     break;
112   case Function::LinkOnceAnyLinkage:
113   case Function::LinkOnceODRLinkage:
114   case Function::WeakAnyLinkage:
115   case Function::WeakODRLinkage:
116     O << "\t.weak\t" << CurrentFnName << '\n';
117     break;
118   }
119
120   printVisibility(CurrentFnName, F->getVisibility());
121
122   O << "\t.type\t" << CurrentFnName << ",@function\n"
123     << CurrentFnName << ":\n";
124 }
125
126 bool MSP430AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
127   SetupMachineFunction(MF);
128   O << "\n\n";
129
130   // Print the 'header' of function
131   emitFunctionHeader(MF);
132
133   // Print out code for the function.
134   for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
135        I != E; ++I) {
136     // Print a label for the basic block.
137     if (!VerboseAsm && (I->pred_empty() || I->isOnlyReachableByFallthrough())) {
138       // This is an entry block or a block that's only reachable via a
139       // fallthrough edge. In non-VerboseAsm mode, don't print the label.
140     } else {
141       printBasicBlockLabel(I, true, true, VerboseAsm);
142       O << '\n';
143     }
144
145     for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
146          II != E; ++II)
147       // Print the assembly for the instruction.
148       printMachineInstruction(II);
149   }
150
151   if (TAI->hasDotTypeDotSizeDirective())
152     O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
153
154   O.flush();
155
156   // We didn't modify anything
157   return false;
158 }
159
160 void MSP430AsmPrinter::printMachineInstruction(const MachineInstr *MI) {
161   ++EmittedInsts;
162
163   // Call the autogenerated instruction printer routines.
164   if (printInstruction(MI))
165     return;
166
167   llvm_unreachable("Should not happen");
168 }
169
170 void MSP430AsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
171                                     const char* Modifier) {
172   const MachineOperand &MO = MI->getOperand(OpNum);
173   switch (MO.getType()) {
174   case MachineOperand::MO_Register:
175     assert (TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
176             "Virtual registers should be already mapped!");
177     O << TM.getRegisterInfo()->get(MO.getReg()).AsmName;
178     return;
179   case MachineOperand::MO_Immediate:
180     if (!Modifier || strcmp(Modifier, "nohash"))
181       O << '#';
182     O << MO.getImm();
183     return;
184   case MachineOperand::MO_MachineBasicBlock:
185     printBasicBlockLabel(MO.getMBB());
186     return;
187   case MachineOperand::MO_GlobalAddress: {
188     bool isMemOp  = Modifier && !strcmp(Modifier, "mem");
189     bool isCallOp = Modifier && !strcmp(Modifier, "call");
190     std::string Name = Mang->getMangledName(MO.getGlobal());
191     assert(MO.getOffset() == 0 && "No offsets allowed!");
192
193     if (isCallOp)
194       O << '#';
195     else if (isMemOp)
196       O << '&';
197
198     O << Name;
199
200     return;
201   }
202   case MachineOperand::MO_ExternalSymbol: {
203     bool isCallOp = Modifier && !strcmp(Modifier, "call");
204     std::string Name(TAI->getGlobalPrefix());
205     Name += MO.getSymbolName();
206     if (isCallOp)
207       O << '#';
208     O << Name;
209     return;
210   }
211   default:
212     llvm_unreachable("Not implemented yet!");
213   }
214 }
215
216 void MSP430AsmPrinter::printSrcMemOperand(const MachineInstr *MI, int OpNum,
217                                           const char* Modifier) {
218   const MachineOperand &Base = MI->getOperand(OpNum);
219   const MachineOperand &Disp = MI->getOperand(OpNum+1);
220
221   if (Base.isGlobal())
222     printOperand(MI, OpNum, "mem");
223   else if (Disp.isImm() && !Base.getReg())
224     printOperand(MI, OpNum);
225   else if (Base.getReg()) {
226     if (Disp.getImm()) {
227       printOperand(MI, OpNum + 1, "nohash");
228       O << '(';
229       printOperand(MI, OpNum);
230       O << ')';
231     } else {
232       O << '@';
233       printOperand(MI, OpNum);
234     }
235   } else
236     llvm_unreachable("Unsupported memory operand");
237 }
238
239 void MSP430AsmPrinter::printCCOperand(const MachineInstr *MI, int OpNum) {
240   unsigned CC = MI->getOperand(OpNum).getImm();
241
242   switch (CC) {
243   default:
244    llvm_unreachable("Unsupported CC code");
245    break;
246   case MSP430::COND_E:
247    O << "eq";
248    break;
249   case MSP430::COND_NE:
250    O << "ne";
251    break;
252   case MSP430::COND_HS:
253    O << "hs";
254    break;
255   case MSP430::COND_LO:
256    O << "lo";
257    break;
258   case MSP430::COND_GE:
259    O << "ge";
260    break;
261   case MSP430::COND_L:
262    O << 'l';
263    break;
264   }
265 }