Iterate over globals once and sectionize them into appropriate sections.
authorSanjiv Gupta <sanjiv.gupta@microchip.com>
Tue, 12 May 2009 17:07:27 +0000 (17:07 +0000)
committerSanjiv Gupta <sanjiv.gupta@microchip.com>
Tue, 12 May 2009 17:07:27 +0000 (17:07 +0000)
Later in asmprinter, go over thsese sections and print them.
Do not print empty sections.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@71560 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Target/PIC16/PIC16AsmPrinter.cpp
lib/Target/PIC16/PIC16AsmPrinter.h
lib/Target/PIC16/PIC16TargetAsmInfo.cpp
lib/Target/PIC16/PIC16TargetAsmInfo.h

index 1792eaa835184bbb613d8d447074ce66ac6d1f8a..8c39272e0311610c03beb553dc9b913b9bed587a 100644 (file)
@@ -164,7 +164,6 @@ void PIC16AsmPrinter::printDecls(void) {
   for (std::list<const char*>::const_iterator I = Decls.begin(); 
        I != Decls.end(); I++) {
     O << TAI->getExternDirective() << *I << "\n";
-    // FIXME: Use PAN::getXXXLabel() funtions hrer.
     O << TAI->getExternDirective() << PAN::getArgsLabel(*I) << "\n";
     O << TAI->getExternDirective() << PAN::getRetvalLabel(*I) << "\n";
   }
@@ -183,8 +182,16 @@ bool PIC16AsmPrinter::doInitialization (Module &M) {
   assert(DW && "Dwarf Writer is not available");
   DW->BeginModule(&M, MMI, O, this, TAI);
 
+  // Set the section names for all globals.
+  for (Module::global_iterator I = M.global_begin(), E = M.global_end();
+       I != E; ++I) {
+    I->setSection(TAI->SectionForGlobal(I)->getName());
+  }
+
   EmitExternsAndGlobals (M);
-  EmitGlobalData(M);
+  EmitIData(M);
+  EmitUData(M);
+  EmitAutos(M);
   EmitRomData(M);
   return Result;
 }
@@ -238,31 +245,19 @@ void PIC16AsmPrinter::EmitExternsAndGlobals (Module &M) {
 
 void PIC16AsmPrinter::EmitRomData (Module &M)
 {
-  SwitchToSection(TAI->getReadOnlySection());
-  for (Module::global_iterator I = M.global_begin(), E = M.global_end();
-       I != E; ++I) {
-    if (!I->hasInitializer())   // External global require no code.
-      continue;
-
-    Constant *C = I->getInitializer();
-    const PointerType *PtrTy = I->getType();
-    int AddrSpace = PtrTy->getAddressSpace();
-    if ((!C->isNullValue()) && (AddrSpace == PIC16ISD::ROM_SPACE)) {
-
-      if (EmitSpecialLLVMGlobal(I))
-        continue;
+  const PIC16TargetAsmInfo *PTAI = static_cast<const PIC16TargetAsmInfo *>(TAI);
 
-      // Any variables reaching here with "." in its name is a local scope
-      // variable and should not be printed in global data section.
-      std::string name = Mang->getValueName(I);
-      if (PAN::isLocalName(name))
-        continue;
+  std::vector<const GlobalVariable*> Items = PTAI->ROSection->Items;
+  if (! Items.size()) return;
 
-      I->setSection(TAI->getReadOnlySection()->getName());
-      O << name;
-      EmitGlobalConstant(C, AddrSpace);
-      O << "\n";
-    }
+  // Print ROData ection.
+  O << "\n";
+  SwitchToSection(PTAI->ROSection->S_);
+  for (unsigned j = 0; j < Items.size(); j++) {
+    O << Mang->getValueName(Items[j]);
+    Constant *C = Items[j]->getInitializer();
+    int AddrSpace = Items[j]->getType()->getAddressSpace();
+    EmitGlobalConstant(C, AddrSpace);
   }
 }
 
@@ -276,9 +271,7 @@ bool PIC16AsmPrinter::doFinalization(Module &M) {
 void PIC16AsmPrinter::emitFunctionData(MachineFunction &MF) {
   const Function *F = MF.getFunction();
   std::string FuncName = Mang->getValueName(F);
-  Module *M = const_cast<Module *>(F->getParent());
   const TargetData *TD = TM.getTargetData();
-  unsigned FrameSize = 0;
   // Emit the data section name.
   O << "\n"; 
   const char *SectionName = PAN::getFrameSectionName(CurrentFnName).c_str();
@@ -318,57 +311,15 @@ void PIC16AsmPrinter::emitFunctionData(MachineFunction &MF) {
   int TempSize = PTLI->GetTmpSize();
   if (TempSize > 0 )
     O << PAN::getTempdataLabel(CurrentFnName) << " RES  " << TempSize <<"\n";
-
-  // Emit the section name for local variables.
-  O << "\n";
-  const char* SecNameLocals = PAN::getAutosSectionName(CurrentFnName).c_str() ;
-
-  const Section *fADataSection = TAI->getNamedSection(SecNameLocals,
-                                                      SectionFlags::Writeable);
-  SwitchToSection(fADataSection);
-
-  // Emit the function variables. 
-   
-  // In PIC16 all the function arguments and local variables are global.
-  // Therefore to get the variable belonging to this function entire
-  // global list will be traversed and variables belonging to this function
-  // will be emitted in the current data section.
-  for (Module::global_iterator I = M->global_begin(), E = M->global_end();
-       I != E; ++I) {
-    std::string VarName = Mang->getValueName(I);
-    
-    // The variables of a function are of form FuncName.* . If this variable
-    // does not belong to this function then continue. 
-    // Static local varilabes of a function does not have .auto. in their
-    // name. They are not printed as part of function data but module
-    // level global data.
-    if (! PAN::isLocalToFunc(FuncName, VarName))
-     continue;
-
-    I->setSection(TAI->SectionForGlobal(I)->getName());
-    Constant *C = I->getInitializer();
-    const Type *Ty = C->getType();
-    unsigned Size = TD->getTypeAllocSize(Ty);
-    FrameSize += Size; 
-    // Emit memory reserve directive.
-    O << VarName << "  RES  " << Size << "\n";
-  }
 }
 
-void PIC16AsmPrinter::EmitGlobalData (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(TAI->SectionForGlobal(I)->getName());
-  }
-
+void PIC16AsmPrinter::EmitIData (Module &M) {
   const PIC16TargetAsmInfo *PTAI = static_cast<const PIC16TargetAsmInfo *>(TAI);
-  const TargetData *TD = TM.getTargetData();
 
-  // Now print all IDATA sections.
+  // Print all IDATA sections.
   std::vector <PIC16Section *>IDATASections = PTAI->IDATASections;
   for (unsigned i = 0; i < IDATASections.size(); i++) {
+    O << "\n";
     SwitchToSection(IDATASections[i]->S_);
     std::vector<const GlobalVariable*> Items = IDATASections[i]->Items;
     for (unsigned j = 0; j < Items.size(); j++) {
@@ -379,10 +330,16 @@ void PIC16AsmPrinter::EmitGlobalData (Module &M)
       EmitGlobalConstant(C, AddrSpace);
     }
   }
+}
+
+void PIC16AsmPrinter::EmitUData (Module &M) {
+  const PIC16TargetAsmInfo *PTAI = static_cast<const PIC16TargetAsmInfo *>(TAI);
+  const TargetData *TD = TM.getTargetData();
 
-  // Now print all BSS sections.
+  // Print all BSS sections.
   std::vector <PIC16Section *>BSSSections = PTAI->BSSSections;
   for (unsigned i = 0; i < BSSSections.size(); i++) {
+    O << "\n";
     SwitchToSection(BSSSections[i]->S_);
     std::vector<const GlobalVariable*> Items = BSSSections[i]->Items;
     for (unsigned j = 0; j < Items.size(); j++) {
@@ -397,3 +354,26 @@ void PIC16AsmPrinter::EmitGlobalData (Module &M)
   }
 }
 
+void PIC16AsmPrinter::EmitAutos (Module &M)
+{
+  // Section names for all globals are already set.
+
+  const PIC16TargetAsmInfo *PTAI = static_cast<const PIC16TargetAsmInfo *>(TAI);
+  const TargetData *TD = TM.getTargetData();
+
+  // Now print all Autos sections.
+  std::vector <PIC16Section *>AutosSections = PTAI->AutosSections;
+  for (unsigned i = 0; i < AutosSections.size(); i++) {
+    O << "\n";
+    SwitchToSection(AutosSections[i]->S_);
+    std::vector<const GlobalVariable*> Items = AutosSections[i]->Items;
+    for (unsigned j = 0; j < Items.size(); j++) {
+      std::string VarName = Mang->getValueName(Items[j]);
+      Constant *C = Items[j]->getInitializer();
+      const Type *Ty = C->getType();
+      unsigned Size = TD->getTypeAllocSize(Ty);
+      // Emit memory reserve directive.
+      O << VarName << "  RES  " << Size << "\n";
+    }
+  }
+}
index 2bc8a57e7170309ceb30eb35ec057d8d6226acc1..f021695eab69e5caebf318c2e6e8ac1adb077d74 100644 (file)
@@ -43,7 +43,9 @@ namespace llvm {
     bool printInstruction(const MachineInstr *MI); // definition autogenerated.
     bool printMachineInstruction(const MachineInstr *MI);
     void EmitExternsAndGlobals (Module &M);
-    void EmitGlobalData (Module &M);
+    void EmitIData (Module &M);
+    void EmitUData (Module &M);
+    void EmitAutos (Module &M);
     void EmitRomData (Module &M);
     void emitFunctionData(MachineFunction &MF);
     void printDecls(void);
index 3837f9ef7b55305dd86db8604c662d83836d6e55..295188f0b414d2cd52216ce960ae1b372ac0ca1c 100644 (file)
@@ -44,6 +44,7 @@ PIC16TargetAsmInfo(const PIC16TargetMachine &TM)
   // Need because otherwise a .text symbol is emitted by DwarfWriter
   // in BeginModule, and gpasm cribbs for that .text symbol.
   TextSection = getUnnamedSection("", SectionFlags::Code);
+  ROSection = new PIC16Section(getReadOnlySection());
 }
 
 const char *PIC16TargetAsmInfo::getRomDirective(unsigned size) const
@@ -154,6 +155,40 @@ PIC16TargetAsmInfo::getIDATASectionForGlobal(const GlobalVariable *GV) const {
   return FoundIDATA->S_;
 } 
 
+// Get the section for an automatic variable of a function.
+// For PIC16 they are globals only with mangled names.
+const Section *
+PIC16TargetAsmInfo::getSectionForAuto(const GlobalVariable *GV) const {
+
+  const std::string name = PAN::getSectionNameForSym(GV->getName());
+
+  // Go through all Auto Sections and assign this variable
+  // to the appropriate section.
+  PIC16Section *FoundAutoSec = NULL;
+  for (unsigned i = 0; i < AutosSections.size(); i++) {
+    if ( AutosSections[i]->S_->getName() == name) {
+      FoundAutoSec = AutosSections[i];
+      break;
+    }
+  }
+
+  // No Auto section was found. Crate a new one.
+  if (! FoundAutoSec) {
+    const Section *NewSection = getNamedSection (name.c_str());
+
+    FoundAutoSec = new PIC16Section(NewSection);
+
+    // Add this newly created autos section to the list of AutosSections.
+    AutosSections.push_back(FoundAutoSec);
+  }
+
+  // Insert the auto into this section.
+  FoundAutoSec->Items.push_back(GV);
+
+  return FoundAutoSec->S_;
+}
+
+
 // Override default implementation to put the true globals into
 // multiple data sections if required.
 const Section*
@@ -168,8 +203,7 @@ PIC16TargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV1) const {
   // name for it and return.
   const std::string name = GV->getName();
   if (PAN::isLocalName(name)) {
-    const std::string Sec_Name = PAN::getSectionNameForSym(name);
-    return getNamedSection(Sec_Name.c_str());
+    return getSectionForAuto(GV);
   }
 
   // See if this is an uninitialized global.
@@ -177,11 +211,16 @@ PIC16TargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV1) const {
   if (C->isNullValue()) 
     return getBSSSectionForGlobal(GV); 
 
-  // This is initialized data. We only deal with initialized data in RAM.
+  // If this is initialized data in RAM. Put it in the correct IDATA section.
   if (GV->getType()->getAddressSpace() == PIC16ISD::RAM_SPACE) 
     return getIDATASectionForGlobal(GV);
 
-    
+  // This is initialized data in rom, put it in the readonly section.
+  if (GV->getType()->getAddressSpace() == PIC16ISD::ROM_SPACE) {
+    ROSection->Items.push_back(GV);
+    return ROSection->S_;
+  }
+
   // Else let the default implementation take care of it.
   return TargetAsmInfo::SelectSectionForGlobal(GV);
 }
@@ -195,4 +234,10 @@ PIC16TargetAsmInfo::~PIC16TargetAsmInfo() {
   for (unsigned i = 0; i < IDATASections.size(); i++) {
       delete IDATASections[i]; 
   }
+
+  for (unsigned i = 0; i < AutosSections.size(); i++) {
+      delete AutosSections[i]; 
+  }
+
+  delete ROSection;
 }
index 8eaa48a96dce9cb54f15ff569bdf4809bd5af343..a39209daaf65180a720e64ec7539057f2bef77ff 100644 (file)
@@ -43,6 +43,8 @@ namespace llvm {
     PIC16TargetAsmInfo(const PIC16TargetMachine &TM);
     mutable std::vector<PIC16Section *> BSSSections;
     mutable std::vector<PIC16Section *> IDATASections;
+    mutable std::vector<PIC16Section *> AutosSections;
+    mutable PIC16Section *ROSection;
     virtual ~PIC16TargetAsmInfo();
 
     private:
@@ -53,6 +55,7 @@ namespace llvm {
     virtual const char *getASDirective(unsigned size, unsigned AS) const;
     const Section *getBSSSectionForGlobal(const GlobalVariable *GV) const;
     const Section *getIDATASectionForGlobal(const GlobalVariable *GV) const;
+    const Section *getSectionForAuto(const GlobalVariable *GV) const;
     virtual const Section *SelectSectionForGlobal(const GlobalValue *GV) const;
     public:
     void SetSectionForGVs(Module &M);
@@ -62,6 +65,9 @@ namespace llvm {
     std::vector<PIC16Section *> getIDATASections()  const {
       return IDATASections;
     }
+    std::vector<PIC16Section *> getAutosSections()  const {
+      return AutosSections;
+    }
   };
 
 } // namespace llvm