MCize a bunch more stuff, eliminating a lot of uses of the mangler
[oota-llvm.git] / lib / Target / MSP430 / AsmPrinter / 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 "MSP430InstPrinter.h"
19 #include "MSP430MCAsmInfo.h"
20 #include "MSP430MCInstLower.h"
21 #include "MSP430TargetMachine.h"
22 #include "llvm/Constants.h"
23 #include "llvm/DerivedTypes.h"
24 #include "llvm/Module.h"
25 #include "llvm/Assembly/Writer.h"
26 #include "llvm/CodeGen/AsmPrinter.h"
27 #include "llvm/CodeGen/DwarfWriter.h"
28 #include "llvm/CodeGen/MachineModuleInfo.h"
29 #include "llvm/CodeGen/MachineFunctionPass.h"
30 #include "llvm/CodeGen/MachineConstantPool.h"
31 #include "llvm/CodeGen/MachineInstr.h"
32 #include "llvm/MC/MCInst.h"
33 #include "llvm/MC/MCStreamer.h"
34 #include "llvm/MC/MCSymbol.h"
35 #include "llvm/Target/TargetData.h"
36 #include "llvm/Target/TargetLoweringObjectFile.h"
37 #include "llvm/Target/TargetRegistry.h"
38 #include "llvm/ADT/Statistic.h"
39 #include "llvm/Support/CommandLine.h"
40 #include "llvm/Support/FormattedStream.h"
41 #include "llvm/Support/ErrorHandling.h"
42 using namespace llvm;
43
44 STATISTIC(EmittedInsts, "Number of machine instrs printed");
45
46 static cl::opt<bool>
47 EnableMCInst("enable-msp430-mcinst-printer", cl::Hidden,
48              cl::desc("enable experimental mcinst gunk in the msp430 backend"));
49
50 namespace {
51   class MSP430AsmPrinter : public AsmPrinter {
52   public:
53     MSP430AsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
54                      const MCAsmInfo *MAI, bool V)
55       : AsmPrinter(O, TM, MAI, V) {}
56
57     virtual const char *getPassName() const {
58       return "MSP430 Assembly Printer";
59     }
60
61     void printMCInst(const MCInst *MI) {
62       MSP430InstPrinter(O, *MAI).printInstruction(MI);
63     }
64     void printOperand(const MachineInstr *MI, int OpNum,
65                       const char* Modifier = 0);
66     void printPCRelImmOperand(const MachineInstr *MI, int OpNum) {
67       printOperand(MI, OpNum);
68     }
69     void printSrcMemOperand(const MachineInstr *MI, int OpNum,
70                             const char* Modifier = 0);
71     void printCCOperand(const MachineInstr *MI, int OpNum);
72     void printMachineInstruction(const MachineInstr * MI);
73     bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
74                          unsigned AsmVariant,
75                          const char *ExtraCode);
76     bool PrintAsmMemoryOperand(const MachineInstr *MI,
77                                unsigned OpNo, unsigned AsmVariant,
78                                const char *ExtraCode);
79     void printInstructionThroughMCStreamer(const MachineInstr *MI);
80
81     void PrintGlobalVariable(const GlobalVariable* GVar);
82     void emitFunctionHeader(const MachineFunction &MF);
83     bool runOnMachineFunction(MachineFunction &F);
84
85     void getAnalysisUsage(AnalysisUsage &AU) const {
86       AsmPrinter::getAnalysisUsage(AU);
87       AU.setPreservesAll();
88     }
89   };
90 } // end of anonymous namespace
91
92 void MSP430AsmPrinter::PrintGlobalVariable(const GlobalVariable* GVar) {
93   if (!GVar->hasInitializer())
94     return;   // External global require no code
95
96   // Check to see if this is a special global used by LLVM, if so, emit it.
97   if (EmitSpecialLLVMGlobal(GVar))
98     return;
99
100   const TargetData *TD = TM.getTargetData();
101
102   MCSymbol *GVarSym = GetGlobalValueSymbol(GVar);
103   Constant *C = GVar->getInitializer();
104   unsigned Size = TD->getTypeAllocSize(C->getType());
105   unsigned Align = TD->getPreferredAlignmentLog(GVar);
106
107   printVisibility(GVarSym, GVar->getVisibility());
108
109   O << "\t.type\t";
110   GVarSym->print(O, MAI);
111   O << ",@object\n";
112
113   OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(GVar, Mang,
114                                                                   TM));
115
116   if (C->isNullValue() && !GVar->hasSection() &&
117       !GVar->isThreadLocal() &&
118       (GVar->hasLocalLinkage() || GVar->isWeakForLinker())) {
119
120     if (Size == 0) Size = 1;   // .comm Foo, 0 is undefined, avoid it.
121
122     if (GVar->hasLocalLinkage()) {
123       O << "\t.local\t";
124       GVarSym->print(O, MAI);
125       O << '\n';
126     }
127
128     O << MAI->getCOMMDirective();
129     GVarSym->print(O, MAI);
130     O << ',' << Size;
131     if (MAI->getCOMMDirectiveTakesAlignment())
132       O << ',' << (MAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
133
134     if (VerboseAsm) {
135       O.PadToColumn(MAI->getCommentColumn());
136       O << MAI->getCommentString() << ' ';
137       WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
138     }
139     O << '\n';
140     return;
141   }
142
143   switch (GVar->getLinkage()) {
144   case GlobalValue::CommonLinkage:
145   case GlobalValue::LinkOnceAnyLinkage:
146   case GlobalValue::LinkOnceODRLinkage:
147   case GlobalValue::WeakAnyLinkage:
148   case GlobalValue::WeakODRLinkage:
149     O << "\t.weak\t";
150     GVarSym->print(O, MAI);
151     O << '\n';
152     break;
153   case GlobalValue::DLLExportLinkage:
154   case GlobalValue::AppendingLinkage:
155     // FIXME: appending linkage variables should go into a section of
156     // their name or something.  For now, just emit them as external.
157   case GlobalValue::ExternalLinkage:
158     // If external or appending, declare as a global symbol
159     O << "\t.globl ";
160     GVarSym->print(O, MAI);
161     O << '\n';
162     // FALL THROUGH
163   case GlobalValue::PrivateLinkage:
164   case GlobalValue::LinkerPrivateLinkage:
165   case GlobalValue::InternalLinkage:
166      break;
167   default:
168     assert(0 && "Unknown linkage type!");
169   }
170
171   // Use 16-bit alignment by default to simplify bunch of stuff
172   EmitAlignment(Align, GVar);
173   GVarSym->print(O, MAI);
174   O << ":";
175   if (VerboseAsm) {
176     O.PadToColumn(MAI->getCommentColumn());
177     O << MAI->getCommentString() << ' ';
178     WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
179   }
180   O << '\n';
181
182   EmitGlobalConstant(C);
183
184   if (MAI->hasDotTypeDotSizeDirective()) {
185     O << "\t.size\t";
186     GVarSym->print(O, MAI);
187     O << ", " << Size << '\n';
188   }
189 }
190
191 void MSP430AsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
192   const Function *F = MF.getFunction();
193
194   OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
195
196   unsigned FnAlign = MF.getAlignment();
197   EmitAlignment(FnAlign, F);
198
199   switch (F->getLinkage()) {
200   default: llvm_unreachable("Unknown linkage type!");
201   case Function::InternalLinkage:  // Symbols default to internal.
202   case Function::PrivateLinkage:
203   case Function::LinkerPrivateLinkage:
204     break;
205   case Function::ExternalLinkage:
206     O << "\t.globl\t";
207     CurrentFnSym->print(O, MAI);
208     O << '\n';
209     break;
210   case Function::LinkOnceAnyLinkage:
211   case Function::LinkOnceODRLinkage:
212   case Function::WeakAnyLinkage:
213   case Function::WeakODRLinkage:
214     O << "\t.weak\t";
215     CurrentFnSym->print(O, MAI);
216     O << '\n';
217     break;
218   }
219
220   printVisibility(CurrentFnSym, F->getVisibility());
221
222   O << "\t.type\t";
223   CurrentFnSym->print(O, MAI);
224   O << ",@function\n";
225   CurrentFnSym->print(O, MAI);
226   O << ":\n";
227 }
228
229 bool MSP430AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
230   SetupMachineFunction(MF);
231   O << "\n\n";
232
233   // Print the 'header' of function
234   emitFunctionHeader(MF);
235
236   // Print out code for the function.
237   for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
238        I != E; ++I) {
239     // Print a label for the basic block.
240     EmitBasicBlockStart(I);
241
242     for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
243          II != E; ++II)
244       // Print the assembly for the instruction.
245       printMachineInstruction(II);
246   }
247
248   if (MAI->hasDotTypeDotSizeDirective()) {
249     O << "\t.size\t";
250     CurrentFnSym->print(O, MAI);
251     O << ", .-";
252     CurrentFnSym->print(O, MAI);
253     O << '\n';
254   }
255
256   // We didn't modify anything
257   return false;
258 }
259
260 void MSP430AsmPrinter::printMachineInstruction(const MachineInstr *MI) {
261   ++EmittedInsts;
262
263   processDebugLoc(MI, true);
264
265   printInstructionThroughMCStreamer(MI);
266
267   if (VerboseAsm)
268     EmitComments(*MI);
269   O << '\n';
270
271   processDebugLoc(MI, false);
272 }
273
274 void MSP430AsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
275                                     const char* Modifier) {
276   const MachineOperand &MO = MI->getOperand(OpNum);
277   switch (MO.getType()) {
278   case MachineOperand::MO_Register:
279     O << MSP430InstPrinter::getRegisterName(MO.getReg());
280     return;
281   case MachineOperand::MO_Immediate:
282     if (!Modifier || strcmp(Modifier, "nohash"))
283       O << '#';
284     O << MO.getImm();
285     return;
286   case MachineOperand::MO_MachineBasicBlock:
287     GetMBBSymbol(MO.getMBB()->getNumber())->print(O, MAI);
288     return;
289   case MachineOperand::MO_GlobalAddress: {
290     bool isMemOp  = Modifier && !strcmp(Modifier, "mem");
291     uint64_t Offset = MO.getOffset();
292
293     O << (isMemOp ? '&' : '#');
294     if (Offset)
295       O << '(' << Offset << '+';
296
297     GetGlobalValueSymbol(MO.getGlobal())->print(O, MAI);
298     
299     if (Offset)
300       O << ')';
301
302     return;
303   }
304   case MachineOperand::MO_ExternalSymbol: {
305     bool isMemOp  = Modifier && !strcmp(Modifier, "mem");
306     O << (isMemOp ? '&' : '#');
307     O << MAI->getGlobalPrefix() << MO.getSymbolName();
308     return;
309   }
310   default:
311     llvm_unreachable("Not implemented yet!");
312   }
313 }
314
315 void MSP430AsmPrinter::printSrcMemOperand(const MachineInstr *MI, int OpNum,
316                                           const char* Modifier) {
317   const MachineOperand &Base = MI->getOperand(OpNum);
318   const MachineOperand &Disp = MI->getOperand(OpNum+1);
319
320   // Print displacement first
321   if (!Disp.isImm()) {
322     printOperand(MI, OpNum+1, "mem");
323   } else {
324     if (!Base.getReg())
325       O << '&';
326
327     printOperand(MI, OpNum+1, "nohash");
328   }
329
330
331   // Print register base field
332   if (Base.getReg()) {
333     O << '(';
334     printOperand(MI, OpNum);
335     O << ')';
336   }
337 }
338
339 void MSP430AsmPrinter::printCCOperand(const MachineInstr *MI, int OpNum) {
340   unsigned CC = MI->getOperand(OpNum).getImm();
341
342   switch (CC) {
343   default:
344    llvm_unreachable("Unsupported CC code");
345    break;
346   case MSP430CC::COND_E:
347    O << "eq";
348    break;
349   case MSP430CC::COND_NE:
350    O << "ne";
351    break;
352   case MSP430CC::COND_HS:
353    O << "hs";
354    break;
355   case MSP430CC::COND_LO:
356    O << "lo";
357    break;
358   case MSP430CC::COND_GE:
359    O << "ge";
360    break;
361   case MSP430CC::COND_L:
362    O << 'l';
363    break;
364   }
365 }
366
367 /// PrintAsmOperand - Print out an operand for an inline asm expression.
368 ///
369 bool MSP430AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
370                                        unsigned AsmVariant,
371                                        const char *ExtraCode) {
372   // Does this asm operand have a single letter operand modifier?
373   if (ExtraCode && ExtraCode[0])
374     return true; // Unknown modifier.
375
376   printOperand(MI, OpNo);
377   return false;
378 }
379
380 bool MSP430AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
381                                              unsigned OpNo, unsigned AsmVariant,
382                                              const char *ExtraCode) {
383   if (ExtraCode && ExtraCode[0]) {
384     return true; // Unknown modifier.
385   }
386   printSrcMemOperand(MI, OpNo);
387   return false;
388 }
389
390 //===----------------------------------------------------------------------===//
391 void MSP430AsmPrinter::printInstructionThroughMCStreamer(const MachineInstr *MI){
392
393   MSP430MCInstLower MCInstLowering(OutContext, *Mang, *this);
394
395   switch (MI->getOpcode()) {
396   case TargetInstrInfo::DBG_LABEL:
397   case TargetInstrInfo::EH_LABEL:
398   case TargetInstrInfo::GC_LABEL:
399     printLabel(MI);
400     return;
401   case TargetInstrInfo::KILL:
402     printKill(MI);
403     return;
404   case TargetInstrInfo::INLINEASM:
405     printInlineAsm(MI);
406     return;
407   case TargetInstrInfo::IMPLICIT_DEF:
408     printImplicitDef(MI);
409     return;
410   default: break;
411   }
412
413   MCInst TmpInst;
414   MCInstLowering.Lower(MI, TmpInst);
415
416   printMCInst(&TmpInst);
417 }
418
419 static MCInstPrinter *createMSP430MCInstPrinter(const Target &T,
420                                                 unsigned SyntaxVariant,
421                                                 const MCAsmInfo &MAI,
422                                                 raw_ostream &O) {
423   if (SyntaxVariant == 0)
424     return new MSP430InstPrinter(O, MAI);
425   return 0;
426 }
427
428 // Force static initialization.
429 extern "C" void LLVMInitializeMSP430AsmPrinter() {
430   RegisterAsmPrinter<MSP430AsmPrinter> X(TheMSP430Target);
431   TargetRegistry::RegisterMCInstPrinter(TheMSP430Target,
432                                         createMSP430MCInstPrinter);
433 }