Add contexts to some of the MVT APIs. No functionality change yet, just the infrastr...
[oota-llvm.git] / lib / Target / PIC16 / PIC16AsmPrinter.cpp
index 4a35a1e28792002f4b0d1037a98af4d7264982e7..39ebfa4439905cd9cf8d01d91a56aec7391c6328 100644 (file)
 //===----------------------------------------------------------------------===//
 
 #include "PIC16AsmPrinter.h"
+#include "PIC16Section.h"
 #include "PIC16TargetAsmInfo.h"
 #include "llvm/DerivedTypes.h"
 #include "llvm/Function.h"
 #include "llvm/Module.h"
 #include "llvm/CodeGen/DwarfWriter.h"
-#include "llvm/Support/FormattedStream.h"
 #include "llvm/CodeGen/MachineFrameInfo.h"
-#include "llvm/Support/Mangler.h"
-#include "llvm/Support/ErrorHandling.h"
 #include "llvm/CodeGen/DwarfWriter.h"
 #include "llvm/CodeGen/MachineModuleInfo.h"
 #include "llvm/Target/TargetRegistry.h"
 #include "llvm/Target/TargetLoweringObjectFile.h"
-
+#include "llvm/Support/ErrorHandling.h"
+#include "llvm/Support/FormattedStream.h"
+#include "llvm/Support/Mangler.h"
+#include <cstring>
 using namespace llvm;
 
 #include "PIC16GenAsmWriter.inc"
@@ -34,7 +35,7 @@ using namespace llvm;
 PIC16AsmPrinter::PIC16AsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
                                  const TargetAsmInfo *T, bool V)
 : AsmPrinter(O, TM, T, V), DbgInfo(O, T) {
-  PTLI = static_cast<const PIC16TargetLowering*>(TM.getTargetLowering());
+  PTLI = static_cast<PIC16TargetLowering*>(TM.getTargetLowering());
   PTAI = static_cast<const PIC16TargetAsmInfo*>(T);
   PTOF = (PIC16TargetObjectFile*)&PTLI->getObjFileLowering();
 }
@@ -68,12 +69,8 @@ bool PIC16AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
   EmitAutos(CurrentFnName);
 
   // Now emit the instructions of function in its code section.
-  std::string T = PAN::getCodeSectionName(CurrentFnName);
-  const char *codeSection = T.c_str();
-  const Section *fCodeSection = 
-    getObjFileLowering().getOrCreateSection(codeSection, false, 
-                                            SectionKind::Text);
+  const MCSection *fCodeSection = 
+    getObjFileLowering().getSectionForFunction(CurrentFnName);
   // Start the Code Section.
   O <<  "\n";
   SwitchToSection(fCodeSection);
@@ -137,7 +134,15 @@ void PIC16AsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
       return;
 
     case MachineOperand::MO_GlobalAddress: {
-      O << Mang->getMangledName(MO.getGlobal());
+      std::string Sname = Mang->getMangledName(MO.getGlobal());
+      // FIXME: currently we do not have a memcpy def coming in the module
+      // by any chance, as we do not link in those as .bc lib. So these calls
+      // are always external and it is safe to emit an extern.
+      if (PAN::isMemIntrinsic(Sname)) {
+        LibcallDecls.push_back(createESName(Sname));
+      }
+
+      O << Sname;
       break;
     }
     case MachineOperand::MO_ExternalSymbol: {
@@ -148,7 +153,14 @@ void PIC16AsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
         LibcallDecls.push_back(Sname);
       }
 
-      O  << Sname;
+      // Record a call to intrinsic to print the extern declaration for it.
+      std::string Sym = Sname;  
+      if (PAN::isMemIntrinsic(Sym)) {
+        Sym = PAN::addPrefix(Sym);
+        LibcallDecls.push_back(createESName(Sym));
+      }
+
+      O  << Sym;
       break;
     }
     case MachineOperand::MO_MachineBasicBlock:
@@ -170,21 +182,13 @@ void PIC16AsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
 // This function is used to sort the decls list.
 // should return true if s1 should come before s2.
 static bool is_before(const char *s1, const char *s2) {
-  std::string str1 = s1;
-  std::string str2 = s2;
-  int i = str1.compare(str2);
-  // Return true if s1 is smaller or equal.
-  if (i <= 0) return true;
-  // false if s1 should come after s2.
-  return false;
+  return strcmp(s1, s2) <= 0;
 }
 
 // This is used by list::unique below. 
 // unique will filter out duplicates if it knows them.
 static bool is_duplicate(const char *s1, const char *s2) {
-  std::string str1 = s1;
-  std::string str2 = s2;
-  return str1 == str2;
+  return !strcmp(s1, s2);
 }
 
 /// printLibcallDecls - print the extern declarations for compiler 
@@ -223,7 +227,11 @@ bool PIC16AsmPrinter::doInitialization(Module &M) {
   // Set the section names for all globals.
   for (Module::global_iterator I = M.global_begin(), E = M.global_end();
        I != E; ++I)
-    I->setSection(getObjFileLowering().SectionForGlobal(I, Mang,TM)->getName());
+    if (!I->isDeclaration() && !I->hasAvailableExternallyLinkage()) {
+      const MCSection *S = getObjFileLowering().SectionForGlobal(I, Mang, TM);
+      
+      I->setSection(((const MCSectionPIC16*)S)->getName());
+    }
 
   DbgInfo.BeginModule(M);
   EmitFunctionDecls(M);
@@ -253,6 +261,16 @@ void PIC16AsmPrinter::EmitFunctionDecls(Module &M) {
     if (!I->isDeclaration() && !I->hasExternalLinkage())
       continue;
 
+    // Do not emit memcpy, memset, and memmove here.
+    // Calls to these routines can be generated in two ways,
+    // 1. User calling the standard lib function
+    // 2. Codegen generating these calls for llvm intrinsics.
+    // In the first case a prototype is alread availale, while in
+    // second case the call is via and externalsym and the prototype is missing.
+    // So declarations for these are currently always getting printing by
+    // tracking both kind of references in printInstrunction.
+    if (I->isDeclaration() && PAN::isMemIntrinsic(Name)) continue;
+
     const char *directive = I->isDeclaration() ? TAI->getExternDirective() :
                                                  TAI->getGlobalDirective();
       
@@ -320,12 +338,9 @@ void PIC16AsmPrinter::EmitFunctionFrame(MachineFunction &MF) {
   const TargetData *TD = TM.getTargetData();
   // Emit the data section name.
   O << "\n"; 
-  std::string T = PAN::getFrameSectionName(CurrentFnName);
-  const char *SectionName = T.c_str();
-
-  const Section *fPDataSection =
-    getObjFileLowering().getOrCreateSection(SectionName, false,
-                                            SectionKind::DataRel);
+  
+  const MCSection *fPDataSection =
+    getObjFileLowering().getSectionForFunctionFrame(CurrentFnName);
   SwitchToSection(fPDataSection);
   
   // Emit function frame label