change a ton of code to not implicitly use the "O" raw_ostream
[oota-llvm.git] / lib / Target / PIC16 / AsmPrinter / PIC16AsmPrinter.cpp
1 //===-- PIC16AsmPrinter.cpp - PIC16 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 PIC16 assembly language.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "PIC16ABINames.h"
16 #include "PIC16AsmPrinter.h"
17 #include "PIC16Section.h"
18 #include "PIC16MCAsmInfo.h"
19 #include "llvm/DerivedTypes.h"
20 #include "llvm/Function.h"
21 #include "llvm/Module.h"
22 #include "llvm/CodeGen/DwarfWriter.h"
23 #include "llvm/CodeGen/MachineFrameInfo.h"
24 #include "llvm/CodeGen/DwarfWriter.h"
25 #include "llvm/CodeGen/MachineModuleInfo.h"
26 #include "llvm/MC/MCStreamer.h"
27 #include "llvm/MC/MCSymbol.h"
28 #include "llvm/Target/Mangler.h"
29 #include "llvm/Target/TargetRegistry.h"
30 #include "llvm/Target/TargetLoweringObjectFile.h"
31 #include "llvm/Support/ErrorHandling.h"
32 #include "llvm/Support/FormattedStream.h"
33 #include <cstring>
34 using namespace llvm;
35
36 #include "PIC16GenAsmWriter.inc"
37
38 PIC16AsmPrinter::PIC16AsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
39                                  MCStreamer &Streamer)
40 : AsmPrinter(O, TM, Streamer), DbgInfo(O, TM.getMCAsmInfo()) {
41   PTLI = static_cast<PIC16TargetLowering*>(TM.getTargetLowering());
42   PMAI = static_cast<const PIC16MCAsmInfo*>(TM.getMCAsmInfo());
43   PTOF = (PIC16TargetObjectFile *)&PTLI->getObjFileLowering();
44 }
45
46 void PIC16AsmPrinter::EmitInstruction(const MachineInstr *MI) {
47   printInstruction(MI, O);
48   OutStreamer.AddBlankLine();
49 }
50
51 static int getFunctionColor(const Function *F) {
52   if (F->hasSection()) {
53     std::string Sectn = F->getSection();
54     std::string StrToFind = "Overlay=";
55     std::string::size_type Pos = Sectn.find(StrToFind);
56
57     // Retreive the color number if the key is found.
58     if (Pos != std::string::npos) {
59       Pos += StrToFind.length();
60       std::string Color = "";
61       char c = Sectn.at(Pos);
62       // A Color can only consist of digits.
63       while (c >= '0' && c<= '9') {
64         Color.append(1,c);
65         Pos++;
66         if (Pos >= Sectn.length())
67           break;
68         c = Sectn.at(Pos);
69       }
70       return atoi(Color.c_str());
71     }
72   }
73
74   // Color was not set for function, so return -1.
75   return -1;
76 }
77
78 // Color the Auto section of the given function. 
79 void PIC16AsmPrinter::ColorAutoSection(const Function *F) {
80   std::string SectionName = PAN::getAutosSectionName(CurrentFnSym->getName());
81   PIC16Section* Section = PTOF->findPIC16Section(SectionName);
82   if (Section != NULL) {
83     int Color = getFunctionColor(F);
84     if (Color >= 0)
85       Section->setColor(Color);
86   }
87 }
88
89
90 /// runOnMachineFunction - This emits the frame section, autos section and 
91 /// assembly for each instruction. Also takes care of function begin debug
92 /// directive and file begin debug directive (if required) for the function.
93 ///
94 bool PIC16AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
95   // This calls the base class function required to be called at beginning
96   // of runOnMachineFunction.
97   SetupMachineFunction(MF);
98
99   // Put the color information from function to its auto section.
100   const Function *F = MF.getFunction();
101   ColorAutoSection(F);
102
103   // Emit the function frame (args and temps).
104   EmitFunctionFrame(MF);
105
106   DbgInfo.BeginFunction(MF);
107
108   // Now emit the instructions of function in its code section.
109   const MCSection *fCodeSection = 
110     getObjFileLowering().SectionForCode(CurrentFnSym->getName(), 
111                                         PAN::isISR(F->getSection()));
112
113   // Start the Code Section.
114   O <<  "\n";
115   OutStreamer.SwitchSection(fCodeSection);
116
117   // Emit the frame address of the function at the beginning of code.
118   O << "\tretlw  low(" << PAN::getFrameLabel(CurrentFnSym->getName()) << ")\n";
119   O << "\tretlw  high(" << PAN::getFrameLabel(CurrentFnSym->getName()) << ")\n";
120
121   // Emit function start label.
122   O << *CurrentFnSym << ":\n";
123
124   DebugLoc CurDL;
125   O << "\n"; 
126   // Print out code for the function.
127   for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
128        I != E; ++I) {
129
130     // Print a label for the basic block.
131     if (I != MF.begin()) {
132       EmitBasicBlockStart(I);
133     }
134     
135     // Print a basic block.
136     for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
137          II != E; ++II) {
138
139       // Emit the line directive if source line changed.
140       const DebugLoc DL = II->getDebugLoc();
141       if (!DL.isUnknown() && DL != CurDL) {
142         DbgInfo.ChangeDebugLoc(MF, DL);
143         CurDL = DL;
144       }
145         
146       // Print the assembly for the instruction.
147       EmitInstruction(II);
148     }
149   }
150   
151   // Emit function end debug directives.
152   DbgInfo.EndFunction(MF);
153
154   return false;  // we didn't modify anything.
155 }
156
157
158 // printOperand - print operand of insn.
159 void PIC16AsmPrinter::printOperand(const MachineInstr *MI, int opNum,
160                                    raw_ostream &O) {
161   const MachineOperand &MO = MI->getOperand(opNum);
162   const Function *F = MI->getParent()->getParent()->getFunction();
163
164   switch (MO.getType()) {
165     case MachineOperand::MO_Register:
166       {
167         // For indirect load/store insns, the fsr name is printed as INDF.
168         std::string RegName = getRegisterName(MO.getReg());
169         if ((MI->getOpcode() == PIC16::load_indirect) ||
170             (MI->getOpcode() == PIC16::store_indirect))
171           RegName.replace (0, 3, "INDF");
172         O << RegName;
173       }
174       return;
175
176     case MachineOperand::MO_Immediate:
177       O << (int)MO.getImm();
178       return;
179
180     case MachineOperand::MO_GlobalAddress: {
181       MCSymbol *Sym = Mang->getSymbol(MO.getGlobal());
182       // FIXME: currently we do not have a memcpy def coming in the module
183       // by any chance, as we do not link in those as .bc lib. So these calls
184       // are always external and it is safe to emit an extern.
185       if (PAN::isMemIntrinsic(Sym->getName()))
186         LibcallDecls.insert(Sym->getName());
187
188       O << *Sym;
189       break;
190     }
191     case MachineOperand::MO_ExternalSymbol: {
192        const char *Sname = MO.getSymbolName();
193        std::string Printname = Sname;
194
195       // Intrinsic stuff needs to be renamed if we are printing IL fn. 
196       if (PAN::isIntrinsicStuff(Printname)) {
197         if (PAN::isISR(F->getSection())) {
198           Printname = PAN::Rename(Sname);
199         }
200         // Record these decls, we need to print them in asm as extern.
201         LibcallDecls.insert(Printname);
202       }
203
204       O << Printname;
205       break;
206     }
207     case MachineOperand::MO_MachineBasicBlock:
208       O << *MO.getMBB()->getSymbol();
209       return;
210
211     default:
212       llvm_unreachable(" Operand type not supported.");
213   }
214 }
215
216 /// printCCOperand - Print the cond code operand.
217 ///
218 void PIC16AsmPrinter::printCCOperand(const MachineInstr *MI, int opNum,
219                                      raw_ostream &O) {
220   int CC = (int)MI->getOperand(opNum).getImm();
221   O << PIC16CondCodeToString((PIC16CC::CondCodes)CC);
222 }
223
224 /// printLibcallDecls - print the extern declarations for compiler 
225 /// intrinsics.
226 ///
227 void PIC16AsmPrinter::printLibcallDecls() {
228   // If no libcalls used, return.
229   if (LibcallDecls.empty()) return;
230
231   O << MAI->getCommentString() << "External decls for libcalls - BEGIN." <<"\n";
232
233   for (std::set<std::string>::const_iterator I = LibcallDecls.begin(),
234        E = LibcallDecls.end(); I != E; I++) {
235     O << MAI->getExternDirective() << *I << "\n";
236   }
237   O << MAI->getCommentString() << "External decls for libcalls - END." <<"\n";
238 }
239
240 /// doInitialization - Perform Module level initializations here.
241 /// One task that we do here is to sectionize all global variables.
242 /// The MemSelOptimizer pass depends on the sectionizing.
243 ///
244 bool PIC16AsmPrinter::doInitialization(Module &M) {
245   bool Result = AsmPrinter::doInitialization(M);
246
247   // Every asmbly contains these std headers. 
248   O << "\n#include p16f1xxx.inc";
249   O << "\n#include stdmacros.inc";
250
251   // Set the section names for all globals.
252   for (Module::global_iterator I = M.global_begin(), E = M.global_end();
253        I != E; ++I) {
254
255     // Record External Var Decls.
256     if (I->isDeclaration()) {
257       ExternalVarDecls.push_back(I);
258       continue;
259     }
260
261     // Record Exteranl Var Defs.
262     if (I->hasExternalLinkage() || I->hasCommonLinkage()) {
263       ExternalVarDefs.push_back(I);
264     }
265
266     // Sectionify actual data.
267     if (!I->hasAvailableExternallyLinkage()) {
268       const MCSection *S = getObjFileLowering().SectionForGlobal(I, Mang, TM);
269       
270       I->setSection(((const PIC16Section *)S)->getName());
271     }
272   }
273
274   DbgInfo.BeginModule(M);
275   EmitFunctionDecls(M);
276   EmitUndefinedVars(M);
277   EmitDefinedVars(M);
278   EmitIData(M);
279   EmitUData(M);
280   EmitRomData(M);
281   EmitSharedUdata(M);
282   EmitUserSections(M);
283   return Result;
284 }
285
286 /// Emit extern decls for functions imported from other modules, and emit
287 /// global declarations for function defined in this module and which are
288 /// available to other modules.
289 ///
290 void PIC16AsmPrinter::EmitFunctionDecls(Module &M) {
291  // Emit declarations for external functions.
292   O <<"\n"<<MAI->getCommentString() << "Function Declarations - BEGIN." <<"\n";
293   for (Module::iterator I = M.begin(), E = M.end(); I != E; I++) {
294     if (I->isIntrinsic() || I->getName() == "@abort")
295       continue;
296     
297     if (!I->isDeclaration() && !I->hasExternalLinkage())
298       continue;
299
300     MCSymbol *Sym = Mang->getSymbol(I);
301     
302     // Do not emit memcpy, memset, and memmove here.
303     // Calls to these routines can be generated in two ways,
304     // 1. User calling the standard lib function
305     // 2. Codegen generating these calls for llvm intrinsics.
306     // In the first case a prototype is alread availale, while in
307     // second case the call is via and externalsym and the prototype is missing.
308     // So declarations for these are currently always getting printing by
309     // tracking both kind of references in printInstrunction.
310     if (I->isDeclaration() && PAN::isMemIntrinsic(Sym->getName())) continue;
311
312     const char *directive = I->isDeclaration() ? MAI->getExternDirective() :
313                                                  MAI->getGlobalDirective();
314       
315     O << directive << Sym->getName() << "\n";
316     O << directive << PAN::getRetvalLabel(Sym->getName()) << "\n";
317     O << directive << PAN::getArgsLabel(Sym->getName()) << "\n";
318   }
319
320   O << MAI->getCommentString() << "Function Declarations - END." <<"\n";
321 }
322
323 // Emit variables imported from other Modules.
324 void PIC16AsmPrinter::EmitUndefinedVars(Module &M) {
325   std::vector<const GlobalVariable*> Items = ExternalVarDecls;
326   if (!Items.size()) return;
327
328   O << "\n" << MAI->getCommentString() << "Imported Variables - BEGIN" << "\n";
329   for (unsigned j = 0; j < Items.size(); j++)
330     O << MAI->getExternDirective() << *Mang->getSymbol(Items[j]) << "\n";
331   O << MAI->getCommentString() << "Imported Variables - END" << "\n";
332 }
333
334 // Emit variables defined in this module and are available to other modules.
335 void PIC16AsmPrinter::EmitDefinedVars(Module &M) {
336   std::vector<const GlobalVariable*> Items = ExternalVarDefs;
337   if (!Items.size()) return;
338
339   O << "\n" << MAI->getCommentString() << "Exported Variables - BEGIN" << "\n";
340   for (unsigned j = 0; j < Items.size(); j++)
341     O << MAI->getGlobalDirective() << *Mang->getSymbol(Items[j]) << "\n";
342   O <<  MAI->getCommentString() << "Exported Variables - END" << "\n";
343 }
344
345 // Emit initialized data placed in ROM.
346 void PIC16AsmPrinter::EmitRomData(Module &M) {
347   EmitSingleSection(PTOF->ROMDATASection());
348 }
349
350 // Emit Shared section udata.
351 void PIC16AsmPrinter::EmitSharedUdata(Module &M) {
352   EmitSingleSection(PTOF->SHAREDUDATASection());
353 }
354
355 bool PIC16AsmPrinter::doFinalization(Module &M) {
356   EmitAllAutos(M);
357   printLibcallDecls();
358   DbgInfo.EndModule(M);
359   O << "\n\t" << "END\n";
360   return AsmPrinter::doFinalization(M);
361 }
362
363 void PIC16AsmPrinter::EmitFunctionFrame(MachineFunction &MF) {
364   const Function *F = MF.getFunction();
365   const TargetData *TD = TM.getTargetData();
366   // Emit the data section name.
367   O << "\n"; 
368   
369   PIC16Section *fPDataSection =
370     const_cast<PIC16Section *>(getObjFileLowering().
371                                 SectionForFrame(CurrentFnSym->getName()));
372  
373   fPDataSection->setColor(getFunctionColor(F)); 
374   OutStreamer.SwitchSection(fPDataSection);
375   
376   // Emit function frame label
377   O << PAN::getFrameLabel(CurrentFnSym->getName()) << ":\n";
378
379   const Type *RetType = F->getReturnType();
380   unsigned RetSize = 0; 
381   if (RetType->getTypeID() != Type::VoidTyID) 
382     RetSize = TD->getTypeAllocSize(RetType);
383   
384   //Emit function return value space
385   // FIXME: Do not emit RetvalLable when retsize is zero. To do this
386   // we will need to avoid printing a global directive for Retval label
387   // in emitExternandGloblas.
388   if(RetSize > 0)
389      O << PAN::getRetvalLabel(CurrentFnSym->getName())
390        << " RES " << RetSize << "\n";
391   else
392      O << PAN::getRetvalLabel(CurrentFnSym->getName()) << ": \n";
393    
394   // Emit variable to hold the space for function arguments 
395   unsigned ArgSize = 0;
396   for (Function::const_arg_iterator argi = F->arg_begin(),
397            arge = F->arg_end(); argi != arge ; ++argi) {
398     const Type *Ty = argi->getType();
399     ArgSize += TD->getTypeAllocSize(Ty);
400    }
401
402   O << PAN::getArgsLabel(CurrentFnSym->getName()) << " RES " << ArgSize << "\n";
403
404   // Emit temporary space
405   int TempSize = PTLI->GetTmpSize();
406   if (TempSize > 0)
407     O << PAN::getTempdataLabel(CurrentFnSym->getName()) << " RES  "
408       << TempSize << '\n';
409 }
410
411
412 void PIC16AsmPrinter::EmitInitializedDataSection(const PIC16Section *S) {
413   /// Emit Section header.
414   OutStreamer.SwitchSection(S);
415
416     std::vector<const GlobalVariable*> Items = S->Items;
417     for (unsigned j = 0; j < Items.size(); j++) {
418       Constant *C = Items[j]->getInitializer();
419       int AddrSpace = Items[j]->getType()->getAddressSpace();
420       O << *Mang->getSymbol(Items[j]);
421       EmitGlobalConstant(C, AddrSpace);
422    }
423 }
424
425 // Print all IDATA sections.
426 void PIC16AsmPrinter::EmitIData(Module &M) {
427   EmitSectionList (M, PTOF->IDATASections());
428 }
429
430 void PIC16AsmPrinter::
431 EmitUninitializedDataSection(const PIC16Section *S) {
432     const TargetData *TD = TM.getTargetData();
433     OutStreamer.SwitchSection(S);
434     std::vector<const GlobalVariable*> Items = S->Items;
435     for (unsigned j = 0; j < Items.size(); j++) {
436       Constant *C = Items[j]->getInitializer();
437       const Type *Ty = C->getType();
438       unsigned Size = TD->getTypeAllocSize(Ty);
439       O << *Mang->getSymbol(Items[j]) << " RES " << Size << "\n";
440     }
441 }
442
443 // Print all UDATA sections.
444 void PIC16AsmPrinter::EmitUData(Module &M) {
445   EmitSectionList (M, PTOF->UDATASections());
446 }
447
448 // Print all USER sections.
449 void PIC16AsmPrinter::EmitUserSections(Module &M) {
450   EmitSectionList (M, PTOF->USERSections());
451 }
452
453 // Print all AUTO sections.
454 void PIC16AsmPrinter::EmitAllAutos(Module &M) {
455   EmitSectionList (M, PTOF->AUTOSections());
456 }
457
458 extern "C" void LLVMInitializePIC16AsmPrinter() { 
459   RegisterAsmPrinter<PIC16AsmPrinter> X(ThePIC16Target);
460 }
461
462 // Emit one data section using correct section emitter based on section type.
463 void PIC16AsmPrinter::EmitSingleSection(const PIC16Section *S) {
464   if (S == NULL) return;
465
466   switch (S->getType()) {
467     default: llvm_unreachable ("unknow user section type");
468     case UDATA:
469     case UDATA_SHR:
470     case UDATA_OVR:
471       EmitUninitializedDataSection(S);
472       break;
473     case IDATA:
474     case ROMDATA:
475       EmitInitializedDataSection(S);
476       break;
477   }
478 }
479
480 // Emit a list of sections.
481 void PIC16AsmPrinter::
482 EmitSectionList(Module &M, const std::vector<PIC16Section *> &SList) {
483   for (unsigned i = 0; i < SList.size(); i++) {
484     // Exclude llvm specific metadata sections.
485     if (SList[i]->getName().find("llvm.") != std::string::npos)
486       continue;
487     O << "\n";
488     EmitSingleSection(SList[i]);
489   }
490 }
491