Remove a couple of already-implemented notes.
[oota-llvm.git] / lib / Target / PIC16 / PIC16TargetAsmInfo.cpp
1 //===-- PIC16TargetAsmInfo.cpp - PIC16 asm properties ---------------------===//
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 the declarations of the PIC16TargetAsmInfo properties.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "PIC16TargetAsmInfo.h"
15 #include "PIC16TargetMachine.h"
16 #include "llvm/GlobalValue.h"
17 #include "llvm/GlobalVariable.h"
18 #include "llvm/DerivedTypes.h"
19
20 using namespace llvm;
21
22 PIC16TargetAsmInfo::
23 PIC16TargetAsmInfo(const PIC16TargetMachine &TM) 
24   : TargetAsmInfo(TM) {
25   CommentString = ";";
26   GlobalPrefix = PAN::getTagName(PAN::PREFIX_SYMBOL);
27   GlobalDirective = "\tglobal\t";
28   ExternDirective = "\textern\t";
29
30   Data8bitsDirective = " db ";
31   Data16bitsDirective = " dw ";
32   Data32bitsDirective = " dl ";
33   Data64bitsDirective = NULL;
34   RomData8bitsDirective = " dw ";
35   RomData16bitsDirective = " rom_di ";
36   RomData32bitsDirective = " rom_dl ";
37   ZeroDirective = NULL;
38   AsciiDirective = " dt ";
39   AscizDirective = NULL;
40   BSSSection_  = getNamedSection("udata.# UDATA",
41                               SectionFlags::Writeable | SectionFlags::BSS);
42   ReadOnlySection = getNamedSection("romdata.# ROMDATA", SectionFlags::None);
43   DataSection = getNamedSection("idata.# IDATA", SectionFlags::Writeable);
44   SwitchToSectionDirective = "";
45   // Need because otherwise a .text symbol is emitted by DwarfWriter
46   // in BeginModule, and gpasm cribbs for that .text symbol.
47   TextSection = getUnnamedSection("", SectionFlags::Code);
48   PIC16Section *ROSection = new PIC16Section(getReadOnlySection());
49   ROSections.push_back(ROSection);
50   ExternalVarDecls = new PIC16Section(getNamedSection("ExternalVarDecls"));
51   ExternalVarDefs = new PIC16Section(getNamedSection("ExternalVarDefs"));
52   // Set it to false because we weed to generate c file name and not bc file
53   // name.
54   HasSingleParameterDotFile = false;
55 }
56
57 const char *PIC16TargetAsmInfo::getRomDirective(unsigned Size) const {
58   switch (Size) {
59   case  8: return RomData8bitsDirective;
60   case 16: return RomData16bitsDirective;
61   case 32: return RomData32bitsDirective;
62   default: return NULL;
63   }
64 }
65
66
67 const char *PIC16TargetAsmInfo::
68 getDataASDirective(unsigned Size, unsigned AS) const {
69   if (AS == PIC16ISD::ROM_SPACE)
70     return getRomDirective(Size);
71   return NULL;
72 }
73
74 const Section *
75 PIC16TargetAsmInfo::getBSSSectionForGlobal(const GlobalVariable *GV) const {
76   assert(GV->hasInitializer() && "This global doesn't need space");
77   Constant *C = GV->getInitializer();
78   assert(C->isNullValue() && "Unitialized globals has non-zero initializer");
79
80   // Find how much space this global needs.
81   const TargetData *TD = TM.getTargetData();
82   const Type *Ty = C->getType(); 
83   unsigned ValSize = TD->getTypeAllocSize(Ty);
84  
85   // Go through all BSS Sections and assign this variable
86   // to the first available section having enough space.
87   PIC16Section *FoundBSS = NULL;
88   for (unsigned i = 0; i < BSSSections.size(); i++) {
89     if (DataBankSize - BSSSections[i]->Size >= ValSize) {
90       FoundBSS = BSSSections[i];
91       break;
92     }
93   }
94
95   // No BSS section spacious enough was found. Crate a new one.
96   if (!FoundBSS) {
97     std::string name = PAN::getUdataSectionName(BSSSections.size());
98     const Section *NewSection = getNamedSection(name.c_str());
99
100     FoundBSS = new PIC16Section(NewSection);
101
102     // Add this newly created BSS section to the list of BSSSections.
103     BSSSections.push_back(FoundBSS);
104   }
105   
106   // Insert the GV into this BSS.
107   FoundBSS->Items.push_back(GV);
108   FoundBSS->Size += ValSize;
109   return FoundBSS->S_;
110
111
112 const Section *
113 PIC16TargetAsmInfo::getIDATASectionForGlobal(const GlobalVariable *GV) const {
114   assert(GV->hasInitializer() && "This global doesn't need space");
115   Constant *C = GV->getInitializer();
116   assert(!C->isNullValue() && "initialized globals has zero initializer");
117   assert(GV->getType()->getAddressSpace() == PIC16ISD::RAM_SPACE &&
118          "can split initialized RAM data only");
119
120   // Find how much space this global needs.
121   const TargetData *TD = TM.getTargetData();
122   const Type *Ty = C->getType(); 
123   unsigned ValSize = TD->getTypeAllocSize(Ty);
124  
125   // Go through all IDATA Sections and assign this variable
126   // to the first available section having enough space.
127   PIC16Section *FoundIDATA = NULL;
128   for (unsigned i = 0; i < IDATASections.size(); i++) {
129     if (DataBankSize - IDATASections[i]->Size >= ValSize) {
130       FoundIDATA = IDATASections[i]; 
131       break;
132     }
133   }
134
135   // No IDATA section spacious enough was found. Crate a new one.
136   if (!FoundIDATA) {
137     std::string name = PAN::getIdataSectionName(IDATASections.size());
138     const Section *NewSection = getNamedSection(name.c_str());
139
140     FoundIDATA = new PIC16Section(NewSection);
141
142     // Add this newly created IDATA section to the list of IDATASections.
143     IDATASections.push_back(FoundIDATA);
144   }
145   
146   // Insert the GV into this IDATA.
147   FoundIDATA->Items.push_back(GV);
148   FoundIDATA->Size += ValSize;
149   return FoundIDATA->S_;
150
151
152 // Get the section for an automatic variable of a function.
153 // For PIC16 they are globals only with mangled names.
154 const Section *
155 PIC16TargetAsmInfo::getSectionForAuto(const GlobalVariable *GV) const {
156
157   const std::string name = PAN::getSectionNameForSym(GV->getName());
158
159   // Go through all Auto Sections and assign this variable
160   // to the appropriate section.
161   PIC16Section *FoundAutoSec = NULL;
162   for (unsigned i = 0; i < AutosSections.size(); i++) {
163     if (AutosSections[i]->S_->getName() == name) {
164       FoundAutoSec = AutosSections[i];
165       break;
166     }
167   }
168
169   // No Auto section was found. Crate a new one.
170   if (!FoundAutoSec) {
171     const Section *NewSection = getNamedSection(name.c_str());
172
173     FoundAutoSec = new PIC16Section(NewSection);
174
175     // Add this newly created autos section to the list of AutosSections.
176     AutosSections.push_back(FoundAutoSec);
177   }
178
179   // Insert the auto into this section.
180   FoundAutoSec->Items.push_back(GV);
181
182   return FoundAutoSec->S_;
183 }
184
185
186 // Override default implementation to put the true globals into
187 // multiple data sections if required.
188 const Section*
189 PIC16TargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV1) const {
190   // We select the section based on the initializer here, so it really
191   // has to be a GlobalVariable.
192   const GlobalVariable *GV = dyn_cast<GlobalVariable>(GV1); 
193   if (!GV)
194     return TargetAsmInfo::SelectSectionForGlobal(GV1);
195
196   // Record Exteranl Var Decls.
197   if (GV->isDeclaration()) {
198     ExternalVarDecls->Items.push_back(GV);
199     return ExternalVarDecls->S_;
200   }
201     
202   assert(GV->hasInitializer() && "A def without initializer?");
203
204   // First, if this is an automatic variable for a function, get the section
205   // name for it and return.
206   const std::string name = GV->getName();
207   if (PAN::isLocalName(name)) {
208     return getSectionForAuto(GV);
209   }
210
211   // Record Exteranl Var Defs.
212   if (GV->hasExternalLinkage() || GV->hasCommonLinkage()) {
213     ExternalVarDefs->Items.push_back(GV);
214   }
215
216   // See if this is an uninitialized global.
217   const Constant *C = GV->getInitializer();
218   if (C->isNullValue()) 
219     return getBSSSectionForGlobal(GV); 
220
221   // If this is initialized data in RAM. Put it in the correct IDATA section.
222   if (GV->getType()->getAddressSpace() == PIC16ISD::RAM_SPACE) 
223     return getIDATASectionForGlobal(GV);
224
225   // This is initialized data in rom, put it in the readonly section.
226   if (GV->getType()->getAddressSpace() == PIC16ISD::ROM_SPACE) 
227     return getROSectionForGlobal(GV);
228
229   // Else let the default implementation take care of it.
230   return TargetAsmInfo::SelectSectionForGlobal(GV);
231 }
232
233 PIC16TargetAsmInfo::~PIC16TargetAsmInfo() {
234   for (unsigned i = 0; i < BSSSections.size(); i++)
235     delete BSSSections[i]; 
236   for (unsigned i = 0; i < IDATASections.size(); i++)
237     delete IDATASections[i]; 
238   for (unsigned i = 0; i < AutosSections.size(); i++)
239     delete AutosSections[i]; 
240   for (unsigned i = 0; i < ROSections.size(); i++)
241     delete ROSections[i];
242   delete ExternalVarDecls;
243   delete ExternalVarDefs;
244 }
245
246 // Override the default implementation. Create PIC16sections for variables 
247 // which have a section name or address.
248 const Section* 
249 PIC16TargetAsmInfo::SectionForGlobal(const GlobalValue *GV) const {
250   const Section* S;
251   // If GV has a sectin name or section address create that section now.
252   if (GV->hasSection()) {
253     std::string SectName = GV->getSection();
254     // If address for a variable is specified, get the address and create
255     // section.
256     std::string AddrStr = "Address=";
257     if (SectName.compare(0, AddrStr.length(), AddrStr) == 0) {
258       std::string SectAddr = SectName.substr(AddrStr.length());
259       S = CreateSectionForGlobal(GV, SectAddr);
260     } else {
261       S = CreateSectionForGlobal(GV);
262     } 
263   } else {
264     // Use section depending on the 'type' of variable
265     S = SelectSectionForGlobal(GV);
266   }
267   return S;
268 }
269
270 // Create a new section for global variable. If Addr is given then create
271 // section at that address else create by name.
272 const Section *
273 PIC16TargetAsmInfo::CreateSectionForGlobal(const GlobalValue *GV1,
274                                            std::string Addr) const {
275   const GlobalVariable *GV = dyn_cast<GlobalVariable>(GV1);
276
277   if (!GV)
278     return TargetAsmInfo::SectionForGlobal(GV1);
279
280   // See if this is an uninitialized global.
281   const Constant *C = GV->getInitializer();
282   if (C->isNullValue())
283     return CreateBSSSectionForGlobal(GV, Addr);
284
285   // If this is initialized data in RAM. Put it in the correct IDATA section.
286   if (GV->getType()->getAddressSpace() == PIC16ISD::RAM_SPACE)
287     return CreateIDATASectionForGlobal(GV, Addr);
288
289   // This is initialized data in rom, put it in the readonly section.
290   if (GV->getType()->getAddressSpace() == PIC16ISD::ROM_SPACE) 
291     return CreateROSectionForGlobal(GV, Addr);
292
293   // Else let the default implementation take care of it.
294   return TargetAsmInfo::SectionForGlobal(GV);
295 }
296
297 // Create uninitialized section for a variable.
298 const Section *
299 PIC16TargetAsmInfo::CreateBSSSectionForGlobal(const GlobalVariable *GV,
300                                               std::string Addr) const {
301   assert(GV->hasInitializer() && "This global doesn't need space");
302   assert(GV->getInitializer()->isNullValue() &&
303          "Unitialized global has non-zero initializer");
304   std::string Name;
305   // If address is given then create a section at that address else create a
306   // section by section name specified in GV.
307   PIC16Section *FoundBSS = NULL;
308   if (Addr.empty()) { 
309     Name = GV->getSection() + " UDATA";
310     for (unsigned i = 0; i < BSSSections.size(); i++) {
311       if (BSSSections[i]->S_->getName() == Name) {
312         FoundBSS = BSSSections[i];
313         break;
314       }
315     }
316   } else {
317     std::string Prefix = GV->getName() + "." + Addr + ".";
318     Name = PAN::getUdataSectionName(BSSSections.size(), Prefix) + " " + Addr;
319   }
320   
321   PIC16Section *NewBSS = FoundBSS;
322   if (NewBSS == NULL) {
323     const Section *NewSection = getNamedSection(Name.c_str());
324     NewBSS = new PIC16Section(NewSection);
325     BSSSections.push_back(NewBSS);
326   }
327
328   // Insert the GV into this BSS.
329   NewBSS->Items.push_back(GV);
330
331   // We do not want to put any  GV without explicit section into this section
332   // so set its size to DatabankSize.
333   NewBSS->Size = DataBankSize;
334   return NewBSS->S_;
335 }
336
337 // Get rom section for a variable. Currently there can be only one rom section
338 // unless a variable explicitly requests a section.
339 const Section *
340 PIC16TargetAsmInfo::getROSectionForGlobal(const GlobalVariable *GV) const {
341   ROSections[0]->Items.push_back(GV);
342   return ROSections[0]->S_;
343 }
344
345 // Create initialized data section for a variable.
346 const Section *
347 PIC16TargetAsmInfo::CreateIDATASectionForGlobal(const GlobalVariable *GV,
348                                                 std::string Addr) const {
349   assert(GV->hasInitializer() && "This global doesn't need space");
350   assert(!GV->getInitializer()->isNullValue() &&
351          "initialized global has zero initializer");
352   assert(GV->getType()->getAddressSpace() == PIC16ISD::RAM_SPACE &&
353          "can be used for initialized RAM data only");
354
355   std::string Name;
356   // If address is given then create a section at that address else create a
357   // section by section name specified in GV.
358   PIC16Section *FoundIDATASec = NULL;
359   if (Addr.empty()) {
360     Name = GV->getSection() + " IDATA";
361     for (unsigned i = 0; i < IDATASections.size(); i++) {
362       if (IDATASections[i]->S_->getName() == Name) {
363         FoundIDATASec = IDATASections[i];
364         break;
365       }
366     }
367   } else {
368     std::string Prefix = GV->getName() + "." + Addr + ".";
369     Name = PAN::getIdataSectionName(IDATASections.size(), Prefix) + " " + Addr;
370   }
371
372   PIC16Section *NewIDATASec = FoundIDATASec;
373   if (NewIDATASec == NULL) {
374     const Section *NewSection = getNamedSection(Name.c_str());
375     NewIDATASec = new PIC16Section(NewSection);
376     IDATASections.push_back(NewIDATASec);
377   }
378   // Insert the GV into this IDATA Section.
379   NewIDATASec->Items.push_back(GV);
380   // We do not want to put any  GV without explicit section into this section 
381   // so set its size to DatabankSize.
382   NewIDATASec->Size = DataBankSize;
383   return NewIDATASec->S_;
384 }
385
386 // Create a section in rom for a variable.
387 const Section *
388 PIC16TargetAsmInfo::CreateROSectionForGlobal(const GlobalVariable *GV,
389                                              std::string Addr) const {
390   assert(GV->getType()->getAddressSpace() == PIC16ISD::ROM_SPACE &&
391          "can be used for ROM data only");
392
393   std::string Name;
394   // If address is given then create a section at that address else create a
395   // section by section name specified in GV.
396   PIC16Section *FoundROSec = NULL;
397   if (Addr.empty()) {
398     Name = GV->getSection() + " ROMDATA";
399     for (unsigned i = 1; i < ROSections.size(); i++) {
400       if (ROSections[i]->S_->getName() == Name) {
401         FoundROSec = ROSections[i];
402         break;
403       }
404     }
405   } else {
406     std::string Prefix = GV->getName() + "." + Addr + ".";
407     Name = PAN::getRomdataSectionName(ROSections.size(), Prefix) + " " + Addr;
408   }
409
410   PIC16Section *NewRomSec = FoundROSec;
411   if (NewRomSec == NULL) {
412     const Section *NewSection = getNamedSection(Name.c_str());
413     NewRomSec = new PIC16Section(NewSection);
414     ROSections.push_back(NewRomSec);
415   }
416
417   // Insert the GV into this ROM Section.
418   NewRomSec->Items.push_back(GV);
419   return NewRomSec->S_;
420 }
421