make SectionForGlobal non-virtual, add a hook for pic16 to do its "address=" hack.
[oota-llvm.git] / lib / Target / PIC16 / PIC16TargetAsmInfo.cpp
index 8be9a33b9308b20dce3f7f2682b93da516927d55..fedb0b82ca3a290e186d77d8dc9e9f5cf3a14182 100644 (file)
@@ -23,9 +23,14 @@ PIC16TargetAsmInfo::
 PIC16TargetAsmInfo(const PIC16TargetMachine &TM) 
   : TargetAsmInfo(TM) {
   CommentString = ";";
+  GlobalPrefix = PAN::getTagName(PAN::PREFIX_SYMBOL);
+  GlobalDirective = "\tglobal\t";
+  ExternDirective = "\textern\t";
+
   Data8bitsDirective = " db ";
   Data16bitsDirective = " dw ";
   Data32bitsDirective = " dl ";
+  Data64bitsDirective = NULL;
   RomData8bitsDirective = " dw ";
   RomData16bitsDirective = " rom_di ";
   RomData32bitsDirective = " rom_dl ";
@@ -40,39 +45,42 @@ 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);
+  PIC16Section *ROSection = new PIC16Section(getReadOnlySection());
+  ROSections.push_back(ROSection);
+  ExternalVarDecls = new PIC16Section(getNamedSection("ExternalVarDecls"));
+  ExternalVarDefs = new PIC16Section(getNamedSection("ExternalVarDefs"));
+  // Set it to false because we weed to generate c file name and not bc file
+  // name.
+  HasSingleParameterDotFile = false;
 }
 
-const char *PIC16TargetAsmInfo::getRomDirective(unsigned size) const
-{
-  if (size == 8)
-    return RomData8bitsDirective;
-  else if (size == 16)
-    return RomData16bitsDirective;
-  else if (size == 32)
-    return RomData32bitsDirective;
-  else
-    return NULL;
+const char *PIC16TargetAsmInfo::getRomDirective(unsigned Size) const {
+  switch (Size) {
+  case  8: return RomData8bitsDirective;
+  case 16: return RomData16bitsDirective;
+  case 32: return RomData32bitsDirective;
+  default: return NULL;
+  }
 }
 
 
-const char *PIC16TargetAsmInfo::getASDirective(unsigned size, 
-                                               unsigned AS) const {
+const char *PIC16TargetAsmInfo::
+getDataASDirective(unsigned Size, unsigned AS) const {
   if (AS == PIC16ISD::ROM_SPACE)
-    return getRomDirective(size);
-  else
-    return NULL;
+    return getRomDirective(Size);
+  return NULL;
 }
 
 const Section *
 PIC16TargetAsmInfo::getBSSSectionForGlobal(const GlobalVariable *GV) const {
-  assert (GV->hasInitializer() && "This global doesn't need space");
+  assert(GV->hasInitializer() && "This global doesn't need space");
   Constant *C = GV->getInitializer();
-  assert (C->isNullValue() && "Unitialized globals has non-zero initializer");
+  assert(C->isNullValue() && "Unitialized globals has non-zero initializer");
 
   // Find how much space this global needs.
   const TargetData *TD = TM.getTargetData();
   const Type *Ty = C->getType(); 
-  unsigned ValSize = TD->getTypePaddedSize(Ty);
+  unsigned ValSize = TD->getTypeAllocSize(Ty);
  
   // Go through all BSS Sections and assign this variable
   // to the first available section having enough space.
@@ -85,10 +93,9 @@ PIC16TargetAsmInfo::getBSSSectionForGlobal(const GlobalVariable *GV) const {
   }
 
   // No BSS section spacious enough was found. Crate a new one.
-  if (! FoundBSS) {
-    char *name = new char[32];
-    sprintf (name, "udata.%d.# UDATA", (int)BSSSections.size());
-    const Section *NewSection = getNamedSection (name);
+  if (!FoundBSS) {
+    std::string name = PAN::getUdataSectionName(BSSSections.size());
+    const Section *NewSection = getNamedSection(name.c_str());
 
     FoundBSS = new PIC16Section(NewSection);
 
@@ -99,42 +106,36 @@ PIC16TargetAsmInfo::getBSSSectionForGlobal(const GlobalVariable *GV) const {
   // Insert the GV into this BSS.
   FoundBSS->Items.push_back(GV);
   FoundBSS->Size += ValSize;
-
-  // We can't do this here because GV is const .
-  // const std::string SName = FoundBSS->S_->getName();
-  // GV->setSection(SName);
-
   return FoundBSS->S_;
 } 
 
 const Section *
 PIC16TargetAsmInfo::getIDATASectionForGlobal(const GlobalVariable *GV) const {
-  assert (GV->hasInitializer() && "This global doesn't need space");
+  assert(GV->hasInitializer() && "This global doesn't need space");
   Constant *C = GV->getInitializer();
-  assert (!C->isNullValue() && "initialized globals has zero initializer");
-  assert (GV->getType()->getAddressSpace() == PIC16ISD::RAM_SPACE &&
-          "can split initialized RAM data only");
+  assert(!C->isNullValue() && "initialized globals has zero initializer");
+  assert(GV->getType()->getAddressSpace() == PIC16ISD::RAM_SPACE &&
+         "can split initialized RAM data only");
 
   // Find how much space this global needs.
   const TargetData *TD = TM.getTargetData();
   const Type *Ty = C->getType(); 
-  unsigned ValSize = TD->getTypePaddedSize(Ty);
+  unsigned ValSize = TD->getTypeAllocSize(Ty);
  
   // Go through all IDATA Sections and assign this variable
   // to the first available section having enough space.
   PIC16Section *FoundIDATA = NULL;
   for (unsigned i = 0; i < IDATASections.size(); i++) {
-    if ( DataBankSize - IDATASections[i]->Size >= ValSize) {
+    if (DataBankSize - IDATASections[i]->Size >= ValSize) {
       FoundIDATA = IDATASections[i]; 
       break;
     }
   }
 
   // No IDATA section spacious enough was found. Crate a new one.
-  if (! FoundIDATA) {
-    char *name = new char[32];
-    sprintf (name, "idata.%d.# IDATA", (int)IDATASections.size());
-    const Section *NewSection = getNamedSection (name);
+  if (!FoundIDATA) {
+    std::string name = PAN::getIdataSectionName(IDATASections.size());
+    const Section *NewSection = getNamedSection(name.c_str());
 
     FoundIDATA = new PIC16Section(NewSection);
 
@@ -145,70 +146,268 @@ PIC16TargetAsmInfo::getIDATASectionForGlobal(const GlobalVariable *GV) const {
   // Insert the GV into this IDATA.
   FoundIDATA->Items.push_back(GV);
   FoundIDATA->Size += ValSize;
-
-  // We can't do this here because GV is const .
-  // GV->setSection(FoundIDATA->S->getName());
-
   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*
 PIC16TargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV1) const {
   // We select the section based on the initializer here, so it really
   // has to be a GlobalVariable.
-  if (!isa<GlobalVariable>(GV1))
+  const GlobalVariable *GV = dyn_cast<GlobalVariable>(GV1); 
+  if (!GV)
     return TargetAsmInfo::SelectSectionForGlobal(GV1);
 
-  const GlobalVariable *GV = dyn_cast<GlobalVariable>(GV1); 
-  // We are only dealing with true globals here. So names with a "."
-  // are local globals. Also declarations are not entertained.
+  // Record Exteranl Var Decls.
+  if (GV->isDeclaration()) {
+    ExternalVarDecls->Items.push_back(GV);
+    return ExternalVarDecls->S_;
+  }
+    
+  assert(GV->hasInitializer() && "A def without initializer?");
+
+  // First, if this is an automatic variable for a function, get the section
+  // name for it and return.
   std::string name = GV->getName();
-  if (name.find(".auto.") != std::string::npos
-      || name.find(".arg.") != std::string::npos || !GV->hasInitializer())
-    return TargetAsmInfo::SelectSectionForGlobal(GV);
+  if (PAN::isLocalName(name))
+    return getSectionForAuto(GV);
+
+  // Record Exteranl Var Defs.
+  if (GV->hasExternalLinkage() || GV->hasCommonLinkage())
+    ExternalVarDefs->Items.push_back(GV);
 
-  const Constant *C = GV->getInitializer();
   // See if this is an uninitialized global.
+  const Constant *C = GV->getInitializer();
   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) 
+    return getROSectionForGlobal(GV);
+
   // Else let the default implementation take care of it.
   return TargetAsmInfo::SelectSectionForGlobal(GV);
 }
 
-void PIC16TargetAsmInfo::SetSectionForGVs(Module &M) {
-  for (Module::global_iterator I = M.global_begin(), E = M.global_end();
-       I != E; ++I) {
-    if (!I->hasInitializer())   // External global require no code.
-      continue;
-
-    // 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 = I->getName();
-    if (name.find(".auto.") != std::string::npos
-      || name.find(".args.") != std::string::npos)
-      continue;
-    int AddrSpace = I->getType()->getAddressSpace();
-
-    if (AddrSpace == PIC16ISD::RAM_SPACE)
-      I->setSection(SectionForGlobal(I)->getName());
+PIC16TargetAsmInfo::~PIC16TargetAsmInfo() {
+  for (unsigned i = 0; i < BSSSections.size(); i++)
+    delete BSSSections[i]; 
+  for (unsigned i = 0; i < IDATASections.size(); i++)
+    delete IDATASections[i]; 
+  for (unsigned i = 0; i < AutosSections.size(); i++)
+    delete AutosSections[i]; 
+  for (unsigned i = 0; i < ROSections.size(); i++)
+    delete ROSections[i];
+  delete ExternalVarDecls;
+  delete ExternalVarDefs;
+}
+
+
+/// getSpecialCasedSectionGlobals - Allow the target to completely override
+/// section assignment of a global.
+const Section *
+PIC16TargetAsmInfo::getSpecialCasedSectionGlobals(const GlobalValue *GV,
+                                                  SectionKind::Kind Kind) const{
+  // If GV has a sectin name or section address create that section now.
+  if (GV->hasSection()) {
+    if (const GlobalVariable *GVar = cast<GlobalVariable>(GV)) {
+      std::string SectName = GVar->getSection();
+      // If address for a variable is specified, get the address and create
+      // section.
+      std::string AddrStr = "Address=";
+      if (SectName.compare(0, AddrStr.length(), AddrStr) == 0) {
+        std::string SectAddr = SectName.substr(AddrStr.length());
+        return CreateSectionForGlobal(GVar, SectAddr);
+      }
+    }
   }
+
+  return 0;
 }
 
+// Create a new section for global variable. If Addr is given then create
+// section at that address else create by name.
+const Section *
+PIC16TargetAsmInfo::CreateSectionForGlobal(const GlobalVariable *GV,
+                                           const std::string &Addr) const {
+  // See if this is an uninitialized global.
+  const Constant *C = GV->getInitializer();
+  if (C->isNullValue())
+    return CreateBSSSectionForGlobal(GV, Addr);
 
-PIC16TargetAsmInfo::~PIC16TargetAsmInfo() {
+  // If this is initialized data in RAM. Put it in the correct IDATA section.
+  if (GV->getType()->getAddressSpace() == PIC16ISD::RAM_SPACE)
+    return CreateIDATASectionForGlobal(GV, Addr);
+
+  // This is initialized data in rom, put it in the readonly section.
+  if (GV->getType()->getAddressSpace() == PIC16ISD::ROM_SPACE) 
+    return CreateROSectionForGlobal(GV, Addr);
+
+  // Else let the default implementation take care of it.
+  return TargetAsmInfo::SectionForGlobal(GV);
+}
+
+// Create uninitialized section for a variable.
+const Section *
+PIC16TargetAsmInfo::CreateBSSSectionForGlobal(const GlobalVariable *GV,
+                                              std::string Addr) const {
+  assert(GV->hasInitializer() && "This global doesn't need space");
+  assert(GV->getInitializer()->isNullValue() &&
+         "Unitialized global has non-zero initializer");
+  std::string Name;
+  // If address is given then create a section at that address else create a
+  // section by section name specified in GV.
+  PIC16Section *FoundBSS = NULL;
+  if (Addr.empty()) { 
+    Name = GV->getSection() + " UDATA";
+    for (unsigned i = 0; i < BSSSections.size(); i++) {
+      if (BSSSections[i]->S_->getName() == Name) {
+        FoundBSS = BSSSections[i];
+        break;
+      }
+    }
+  } else {
+    std::string Prefix = GV->getNameStr() + "." + Addr + ".";
+    Name = PAN::getUdataSectionName(BSSSections.size(), Prefix) + " " + Addr;
+  }
   
-  for (unsigned i = 0; i < BSSSections.size(); i++) {
-      delete BSSSections[i]; 
+  PIC16Section *NewBSS = FoundBSS;
+  if (NewBSS == NULL) {
+    const Section *NewSection = getNamedSection(Name.c_str());
+    NewBSS = new PIC16Section(NewSection);
+    BSSSections.push_back(NewBSS);
   }
 
-  for (unsigned i = 0; i < IDATASections.size(); i++) {
-      delete IDATASections[i]; 
+  // Insert the GV into this BSS.
+  NewBSS->Items.push_back(GV);
+
+  // We do not want to put any  GV without explicit section into this section
+  // so set its size to DatabankSize.
+  NewBSS->Size = DataBankSize;
+  return NewBSS->S_;
+}
+
+// Get rom section for a variable. Currently there can be only one rom section
+// unless a variable explicitly requests a section.
+const Section *
+PIC16TargetAsmInfo::getROSectionForGlobal(const GlobalVariable *GV) const {
+  ROSections[0]->Items.push_back(GV);
+  return ROSections[0]->S_;
+}
+
+// Create initialized data section for a variable.
+const Section *
+PIC16TargetAsmInfo::CreateIDATASectionForGlobal(const GlobalVariable *GV,
+                                                std::string Addr) const {
+  assert(GV->hasInitializer() && "This global doesn't need space");
+  assert(!GV->getInitializer()->isNullValue() &&
+         "initialized global has zero initializer");
+  assert(GV->getType()->getAddressSpace() == PIC16ISD::RAM_SPACE &&
+         "can be used for initialized RAM data only");
+
+  std::string Name;
+  // If address is given then create a section at that address else create a
+  // section by section name specified in GV.
+  PIC16Section *FoundIDATASec = NULL;
+  if (Addr.empty()) {
+    Name = GV->getSection() + " IDATA";
+    for (unsigned i = 0; i < IDATASections.size(); i++) {
+      if (IDATASections[i]->S_->getName() == Name) {
+        FoundIDATASec = IDATASections[i];
+        break;
+      }
+    }
+  } else {
+    std::string Prefix = GV->getNameStr() + "." + Addr + ".";
+    Name = PAN::getIdataSectionName(IDATASections.size(), Prefix) + " " + Addr;
+  }
+
+  PIC16Section *NewIDATASec = FoundIDATASec;
+  if (NewIDATASec == NULL) {
+    const Section *NewSection = getNamedSection(Name.c_str());
+    NewIDATASec = new PIC16Section(NewSection);
+    IDATASections.push_back(NewIDATASec);
+  }
+  // Insert the GV into this IDATA Section.
+  NewIDATASec->Items.push_back(GV);
+  // We do not want to put any  GV without explicit section into this section 
+  // so set its size to DatabankSize.
+  NewIDATASec->Size = DataBankSize;
+  return NewIDATASec->S_;
+}
+
+// Create a section in rom for a variable.
+const Section *
+PIC16TargetAsmInfo::CreateROSectionForGlobal(const GlobalVariable *GV,
+                                             std::string Addr) const {
+  assert(GV->getType()->getAddressSpace() == PIC16ISD::ROM_SPACE &&
+         "can be used for ROM data only");
+
+  std::string Name;
+  // If address is given then create a section at that address else create a
+  // section by section name specified in GV.
+  PIC16Section *FoundROSec = NULL;
+  if (Addr.empty()) {
+    Name = GV->getSection() + " ROMDATA";
+    for (unsigned i = 1; i < ROSections.size(); i++) {
+      if (ROSections[i]->S_->getName() == Name) {
+        FoundROSec = ROSections[i];
+        break;
+      }
+    }
+  } else {
+    std::string Prefix = GV->getNameStr() + "." + Addr + ".";
+    Name = PAN::getRomdataSectionName(ROSections.size(), Prefix) + " " + Addr;
   }
+
+  PIC16Section *NewRomSec = FoundROSec;
+  if (NewRomSec == NULL) {
+    const Section *NewSection = getNamedSection(Name.c_str());
+    NewRomSec = new PIC16Section(NewSection);
+    ROSections.push_back(NewRomSec);
+  }
+
+  // Insert the GV into this ROM Section.
+  NewRomSec->Items.push_back(GV);
+  return NewRomSec->S_;
 }
+