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