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