rename TargetAsmInfo::getASDirective -> getDataASDirective
[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
110   // We can't do this here because GV is const .
111   // const std::string SName = FoundBSS->S_->getName();
112   // GV->setSection(SName);
113
114   return FoundBSS->S_;
115
116
117 const Section *
118 PIC16TargetAsmInfo::getIDATASectionForGlobal(const GlobalVariable *GV) const {
119   assert (GV->hasInitializer() && "This global doesn't need space");
120   Constant *C = GV->getInitializer();
121   assert (!C->isNullValue() && "initialized globals has zero initializer");
122   assert (GV->getType()->getAddressSpace() == PIC16ISD::RAM_SPACE &&
123           "can split initialized RAM data only");
124
125   // Find how much space this global needs.
126   const TargetData *TD = TM.getTargetData();
127   const Type *Ty = C->getType(); 
128   unsigned ValSize = TD->getTypeAllocSize(Ty);
129  
130   // Go through all IDATA Sections and assign this variable
131   // to the first available section having enough space.
132   PIC16Section *FoundIDATA = NULL;
133   for (unsigned i = 0; i < IDATASections.size(); i++) {
134     if ( DataBankSize - IDATASections[i]->Size >= ValSize) {
135       FoundIDATA = IDATASections[i]; 
136       break;
137     }
138   }
139
140   // No IDATA section spacious enough was found. Crate a new one.
141   if (! FoundIDATA) {
142     std::string name = PAN::getIdataSectionName(IDATASections.size());
143     const Section *NewSection = getNamedSection (name.c_str());
144
145     FoundIDATA = new PIC16Section(NewSection);
146
147     // Add this newly created IDATA section to the list of IDATASections.
148     IDATASections.push_back(FoundIDATA);
149   }
150   
151   // Insert the GV into this IDATA.
152   FoundIDATA->Items.push_back(GV);
153   FoundIDATA->Size += ValSize;
154
155   // We can't do this here because GV is const .
156   // GV->setSection(FoundIDATA->S->getName());
157
158   return FoundIDATA->S_;
159
160
161 // Get the section for an automatic variable of a function.
162 // For PIC16 they are globals only with mangled names.
163 const Section *
164 PIC16TargetAsmInfo::getSectionForAuto(const GlobalVariable *GV) const {
165
166   const std::string name = PAN::getSectionNameForSym(GV->getName());
167
168   // Go through all Auto Sections and assign this variable
169   // to the appropriate section.
170   PIC16Section *FoundAutoSec = NULL;
171   for (unsigned i = 0; i < AutosSections.size(); i++) {
172     if ( AutosSections[i]->S_->getName() == name) {
173       FoundAutoSec = AutosSections[i];
174       break;
175     }
176   }
177
178   // No Auto section was found. Crate a new one.
179   if (! FoundAutoSec) {
180     const Section *NewSection = getNamedSection (name.c_str());
181
182     FoundAutoSec = new PIC16Section(NewSection);
183
184     // Add this newly created autos section to the list of AutosSections.
185     AutosSections.push_back(FoundAutoSec);
186   }
187
188   // Insert the auto into this section.
189   FoundAutoSec->Items.push_back(GV);
190
191   return FoundAutoSec->S_;
192 }
193
194
195 // Override default implementation to put the true globals into
196 // multiple data sections if required.
197 const Section*
198 PIC16TargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV1) const {
199   // We select the section based on the initializer here, so it really
200   // has to be a GlobalVariable.
201   const GlobalVariable *GV = dyn_cast<GlobalVariable>(GV1); 
202
203   if (!GV)
204     return TargetAsmInfo::SelectSectionForGlobal(GV1);
205
206   // Record Exteranl Var Decls.
207   if (GV->isDeclaration()) {
208     ExternalVarDecls->Items.push_back(GV);
209     return ExternalVarDecls->S_;
210   }
211     
212   assert (GV->hasInitializer() && "A def without initializer?");
213
214   // First, if this is an automatic variable for a function, get the section
215   // name for it and return.
216   const std::string name = GV->getName();
217   if (PAN::isLocalName(name)) {
218     return getSectionForAuto(GV);
219   }
220
221   // Record Exteranl Var Defs.
222   if (GV->hasExternalLinkage() || GV->hasCommonLinkage()) {
223     ExternalVarDefs->Items.push_back(GV);
224   }
225
226   // See if this is an uninitialized global.
227   const Constant *C = GV->getInitializer();
228   if (C->isNullValue()) 
229     return getBSSSectionForGlobal(GV); 
230
231   // If this is initialized data in RAM. Put it in the correct IDATA section.
232   if (GV->getType()->getAddressSpace() == PIC16ISD::RAM_SPACE) 
233     return getIDATASectionForGlobal(GV);
234
235   // This is initialized data in rom, put it in the readonly section.
236   if (GV->getType()->getAddressSpace() == PIC16ISD::ROM_SPACE) 
237     return getROSectionForGlobal(GV);
238
239   // Else let the default implementation take care of it.
240   return TargetAsmInfo::SelectSectionForGlobal(GV);
241 }
242
243 PIC16TargetAsmInfo::~PIC16TargetAsmInfo() {
244   
245   for (unsigned i = 0; i < BSSSections.size(); i++) {
246       delete BSSSections[i]; 
247   }
248
249   for (unsigned i = 0; i < IDATASections.size(); i++) {
250       delete IDATASections[i]; 
251   }
252
253   for (unsigned i = 0; i < AutosSections.size(); i++) {
254       delete AutosSections[i]; 
255   }
256
257   for (unsigned i = 0; i < ROSections.size(); i++) {
258       delete ROSections[i];
259   }
260
261   delete ExternalVarDecls;
262   delete ExternalVarDefs;
263 }
264
265 // Override the default implementation. Create PIC16sections for variables 
266 // which have a section name or address.
267 const Section* 
268 PIC16TargetAsmInfo::SectionForGlobal(const GlobalValue *GV) const {
269   const Section* S;
270   // If GV has a sectin name or section address create that section now.
271   if (GV->hasSection()) {
272     std::string SectName = GV->getSection();
273     // If address for a variable is specified, get the address and create
274     // section.
275     std::string AddrStr = "Address=";
276     if (SectName.compare(0, AddrStr.length(), AddrStr) == 0) {
277       std::string SectAddr = SectName.substr(AddrStr.length());
278       S = CreateSectionForGlobal(GV, SectAddr);
279     }
280     else {
281       S = CreateSectionForGlobal(GV);
282     } 
283   } else {
284     // Use section depending on the 'type' of variable
285     S = SelectSectionForGlobal(GV);
286   }
287   return S;
288 }
289
290 // Create a new section for global variable. If Addr is given then create
291 // section at that address else create by name.
292 const Section *
293 PIC16TargetAsmInfo::CreateSectionForGlobal(const GlobalValue *GV1,
294                                            std::string Addr) const {
295   const GlobalVariable *GV = dyn_cast<GlobalVariable>(GV1);
296
297   if (!GV)
298     return TargetAsmInfo::SectionForGlobal(GV1);
299
300   // See if this is an uninitialized global.
301   const Constant *C = GV->getInitializer();
302   if (C->isNullValue())
303     return CreateBSSSectionForGlobal(GV, Addr);
304
305   // If this is initialized data in RAM. Put it in the correct IDATA section.
306   if (GV->getType()->getAddressSpace() == PIC16ISD::RAM_SPACE)
307     return CreateIDATASectionForGlobal(GV, Addr);
308
309   // This is initialized data in rom, put it in the readonly section.
310   if (GV->getType()->getAddressSpace() == PIC16ISD::ROM_SPACE) 
311     return CreateROSectionForGlobal(GV, Addr);
312
313   // Else let the default implementation take care of it.
314   return TargetAsmInfo::SectionForGlobal(GV);
315 }
316
317 // Create uninitialized section for a variable.
318 const Section *
319 PIC16TargetAsmInfo::CreateBSSSectionForGlobal(const GlobalVariable *GV,
320                                               std::string Addr) const {
321   assert (GV->hasInitializer() && "This global doesn't need space");
322   assert (GV->getInitializer()->isNullValue() &&
323           "Unitialized global has non-zero initializer");
324   std::string Name;
325   // If address is given then create a section at that address else create a
326   // section by section name specified in GV.
327   PIC16Section *FoundBSS = NULL;
328   if (Addr.empty()) { 
329     Name = GV->getSection() + " UDATA";
330     for (unsigned i = 0; i < BSSSections.size(); i++) {
331       if (BSSSections[i]->S_->getName() == Name) {
332         FoundBSS = BSSSections[i];
333         break;
334       }
335     }
336   }
337   else {
338     std::string Prefix = GV->getName() + "." + Addr + ".";
339     Name = PAN::getUdataSectionName(BSSSections.size(), Prefix) + " " + Addr;
340   }
341   
342   PIC16Section *NewBSS = FoundBSS;
343   if (NewBSS == NULL) {
344     const Section *NewSection = getNamedSection (Name.c_str());
345     NewBSS = new PIC16Section(NewSection);
346     BSSSections.push_back(NewBSS);
347   }
348
349   // Insert the GV into this BSS.
350   NewBSS->Items.push_back(GV);
351
352   // We do not want to put any  GV without explicit section into this section
353   // so set its size to DatabankSize.
354   NewBSS->Size = DataBankSize;
355   return NewBSS->S_;
356 }
357
358 // Get rom section for a variable. Currently there can be only one rom section
359 // unless a variable explicitly requests a section.
360 const Section *
361 PIC16TargetAsmInfo::getROSectionForGlobal(const GlobalVariable *GV) const {
362   ROSections[0]->Items.push_back(GV);
363   return ROSections[0]->S_;
364 }
365
366 // Create initialized data section for a variable.
367 const Section *
368 PIC16TargetAsmInfo::CreateIDATASectionForGlobal(const GlobalVariable *GV,
369                                                 std::string Addr) const {
370   assert (GV->hasInitializer() && "This global doesn't need space");
371   assert (!GV->getInitializer()->isNullValue() &&
372           "initialized global has zero initializer");
373   assert (GV->getType()->getAddressSpace() == PIC16ISD::RAM_SPACE &&
374           "can be used for initialized RAM data only");
375
376   std::string Name;
377   // If address is given then create a section at that address else create a
378   // section by section name specified in GV.
379   PIC16Section *FoundIDATASec = NULL;
380   if (Addr.empty()) {
381     Name = GV->getSection() + " IDATA";
382     for (unsigned i = 0; i < IDATASections.size(); i++) {
383       if (IDATASections[i]->S_->getName() == Name) {
384         FoundIDATASec = IDATASections[i];
385         break;
386       }
387     }
388   }
389   else {
390     std::string Prefix = GV->getName() + "." + Addr + ".";
391     Name = PAN::getIdataSectionName(IDATASections.size(), Prefix) + " " + Addr;
392   }
393
394   PIC16Section *NewIDATASec = FoundIDATASec;
395   if (NewIDATASec == NULL) {
396     const Section *NewSection = getNamedSection (Name.c_str());
397     NewIDATASec = new PIC16Section(NewSection);
398     IDATASections.push_back(NewIDATASec);
399   }
400   // Insert the GV into this IDATA Section.
401   NewIDATASec->Items.push_back(GV);
402   // We do not want to put any  GV without explicit section into this section 
403   // so set its size to DatabankSize.
404   NewIDATASec->Size = DataBankSize;
405   return NewIDATASec->S_;
406 }
407
408 // Create a section in rom for a variable.
409 const Section *
410 PIC16TargetAsmInfo::CreateROSectionForGlobal(const GlobalVariable *GV,
411                                              std::string Addr) const {
412   assert (GV->getType()->getAddressSpace() == PIC16ISD::ROM_SPACE &&
413           "can be used for ROM data only");
414
415   std::string Name;
416   // If address is given then create a section at that address else create a
417   // section by section name specified in GV.
418   PIC16Section *FoundROSec = NULL;
419   if (Addr.empty()) {
420     Name = GV->getSection() + " ROMDATA";
421     for (unsigned i = 1; i < ROSections.size(); i++) {
422       if (ROSections[i]->S_->getName() == Name) {
423         FoundROSec = ROSections[i];
424         break;
425       }
426     }
427   }
428   else {
429     std::string Prefix = GV->getName() + "." + Addr + ".";
430     Name = PAN::getRomdataSectionName(ROSections.size(), Prefix) + " " + Addr;
431   }
432
433   PIC16Section *NewRomSec = FoundROSec;
434   if (NewRomSec == NULL) {
435     const Section *NewSection = getNamedSection (Name.c_str());
436     NewRomSec = new PIC16Section(NewSection);
437     ROSections.push_back(NewRomSec);
438   }
439
440   // Insert the GV into this ROM Section.
441   NewRomSec->Items.push_back(GV);
442   return NewRomSec->S_;
443 }
444