Complete Rewrite of AsmPrinter, TargetObjectFile based on new PIC16Section class
[oota-llvm.git] / lib / Target / PIC16 / PIC16TargetObjectFile.cpp
1 //===-- PIC16TargetObjectFile.cpp - PIC16 object files --------------------===//
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 #include "PIC16TargetObjectFile.h"
11 #include "PIC16ISelLowering.h"
12 #include "PIC16TargetMachine.h"
13 #include "PIC16Section.h"
14 #include "llvm/DerivedTypes.h"
15 #include "llvm/Module.h"
16 #include "llvm/MC/MCSection.h"
17 #include "llvm/MC/MCContext.h"
18 #include "llvm/Support/raw_ostream.h"
19 using namespace llvm;
20
21
22 PIC16TargetObjectFile::PIC16TargetObjectFile() {
23 }
24
25 /// Find a pic16 section. If not found, create one.
26 PIC16Section *PIC16TargetObjectFile::
27 getPIC16Section(const std::string &Name, PIC16SectionType Ty, 
28                 const std::string &Address, int Color) const {
29
30   /// Return if we have an already existing one.
31   PIC16Section *&Entry = SectionsByName[Name];
32   if (Entry)
33     return Entry;
34
35
36   Entry = PIC16Section::Create(Name, Ty, Address, Color, getContext());
37   return Entry;
38 }
39
40 /// Find a standard pic16 data section. If not found, create one and keep
41 /// track of it by adding it to appropriate std section list.
42 PIC16Section *PIC16TargetObjectFile::
43 getPIC16DataSection(const std::string &Name, PIC16SectionType Ty, 
44                     const std::string &Address, int Color) const {
45
46   /// Return if we have an already existing one.
47   PIC16Section *&Entry = SectionsByName[Name];
48   if (Entry)
49     return Entry;
50
51
52   /// Else create a new one and add it to appropriate section list.
53   Entry = PIC16Section::Create(Name, Ty, Address, Color, getContext());
54
55   switch (Ty) {
56   default: llvm_unreachable ("unknow standard section type.");
57   case UDATA: UDATASections_.push_back(Entry); break;
58   case IDATA: IDATASections_.push_back(Entry); break;
59   case ROMDATA: ROMDATASection_ = Entry; break;
60   }
61
62   return Entry;
63 }
64     
65
66 /// Find a standard pic16 autos section. If not found, create one and keep
67 /// track of it by adding it to appropriate std section list.
68 PIC16Section *PIC16TargetObjectFile::
69 getPIC16AutoSection(const std::string &Name, PIC16SectionType Ty, 
70                     const std::string &Address, int Color) const {
71
72   /// Return if we have an already existing one.
73   PIC16Section *&Entry = SectionsByName[Name];
74   if (Entry)
75     return Entry;
76
77
78   /// Else create a new one and add it to appropriate section list.
79   Entry = PIC16Section::Create(Name, Ty, Address, Color, getContext());
80
81   assert (Ty == UDATA_OVR && "incorrect section type for autos");
82   AUTOSections_.push_back(Entry);
83
84   return Entry;
85 }
86     
87 /// Find a pic16 user section. If not found, create one and keep
88 /// track of it by adding it to appropriate std section list.
89 PIC16Section *PIC16TargetObjectFile::
90 getPIC16UserSection(const std::string &Name, PIC16SectionType Ty, 
91                     const std::string &Address, int Color) const {
92
93   /// Return if we have an already existing one.
94   PIC16Section *&Entry = SectionsByName[Name];
95   if (Entry)
96     return Entry;
97
98
99   /// Else create a new one and add it to appropriate section list.
100   Entry = PIC16Section::Create(Name, Ty, Address, Color, getContext());
101
102   USERSections_.push_back(Entry);
103
104   return Entry;
105 }
106
107 /// Do some standard llvm stuff. PIC16 really does not need any of this.
108 void PIC16TargetObjectFile::Initialize(MCContext &Ctx, const TargetMachine &tm){
109   TargetLoweringObjectFile::Initialize(Ctx, tm);
110   TM = &tm;
111   
112   // BSSSection = getPIC16DataSection("udata.#", UDATA);
113   // ReadOnlySection = getPIC16DataSection("romdata.#", ROMDATA);
114   // DataSection = getPIC16DataSection("idata.#", IDATA);
115   
116   // Need because otherwise a .text symbol is emitted by DwarfWriter
117   // in BeginModule, and gpasm cribbs for that .text symbol.
118   // FIXME: below
119   // TextSection = getPIC16DataSection("", UDATA);
120   ROMDATASection_ = NULL;
121 }
122
123 /// allocateUDATA - Allocate a un-initialized global to an existing or new UDATA
124 /// section and return that section.
125 const MCSection *
126 PIC16TargetObjectFile::allocateUDATA(const GlobalVariable *GV) const {
127   assert(GV->hasInitializer() && "This global doesn't need space");
128   Constant *C = GV->getInitializer();
129   assert(C->isNullValue() && "Unitialized globals has non-zero initializer");
130
131   // Find how much space this global needs.
132   const TargetData *TD = TM->getTargetData();
133   const Type *Ty = C->getType(); 
134   unsigned ValSize = TD->getTypeAllocSize(Ty);
135  
136   // Go through all UDATA Sections and assign this variable
137   // to the first available section having enough space.
138   PIC16Section *Found = NULL;
139   for (unsigned i = 0; i < UDATASections_.size(); i++) {
140     if (DataBankSize - UDATASections_[i]->getSize() >= ValSize) {
141       Found = UDATASections_[i];
142       break;
143     }
144   }
145
146   // No UDATA section spacious enough was found. Crate a new one.
147   if (!Found) {
148     std::string name = PAN::getUdataSectionName(UDATASections_.size());
149     Found = getPIC16DataSection(name.c_str(), UDATA);
150   }
151   
152   // Insert the GV into this UDATA section.
153   Found->Items.push_back(GV);
154   Found->setSize(Found->getSize() + ValSize);
155   return Found;
156
157
158 /// allocateIDATA - allocate an initialized global into an existing
159 /// or new section and return that section.
160 const MCSection *
161 PIC16TargetObjectFile::allocateIDATA(const GlobalVariable *GV) const{
162   assert(GV->hasInitializer() && "This global doesn't need space");
163   Constant *C = GV->getInitializer();
164   assert(!C->isNullValue() && "initialized globals has zero initializer");
165   assert(GV->getType()->getAddressSpace() == PIC16ISD::RAM_SPACE &&
166          "can allocate initialized RAM data only");
167
168   // Find how much space this global needs.
169   const TargetData *TD = TM->getTargetData();
170   const Type *Ty = C->getType(); 
171   unsigned ValSize = TD->getTypeAllocSize(Ty);
172  
173   // Go through all IDATA Sections and assign this variable
174   // to the first available section having enough space.
175   PIC16Section *Found = NULL;
176   for (unsigned i = 0; i < IDATASections_.size(); i++) {
177     if (DataBankSize - IDATASections_[i]->getSize() >= ValSize) {
178       Found = IDATASections_[i]; 
179       break;
180     }
181   }
182
183   // No IDATA section spacious enough was found. Crate a new one.
184   if (!Found) {
185     std::string name = PAN::getIdataSectionName(IDATASections_.size());
186     Found = getPIC16DataSection(name.c_str(), IDATA);
187   }
188   
189   // Insert the GV into this IDATA.
190   Found->Items.push_back(GV);
191   Found->setSize(Found->getSize() + ValSize);
192   return Found;
193
194
195 // Allocate a program memory variable into ROMDATA section.
196 const MCSection *
197 PIC16TargetObjectFile::allocateROMDATA(const GlobalVariable *GV) const {
198
199   std::string name = PAN::getRomdataSectionName();
200   PIC16Section *S = getPIC16DataSection(name.c_str(), ROMDATA);
201
202   S->Items.push_back(GV);
203   return S;
204 }
205
206 // Get the section for an automatic variable of a function.
207 // For PIC16 they are globals only with mangled names.
208 const MCSection *
209 PIC16TargetObjectFile::allocateAUTO(const GlobalVariable *GV) const {
210
211   const std::string name = PAN::getSectionNameForSym(GV->getName());
212   PIC16Section *S = getPIC16AutoSection(name.c_str());
213
214   S->Items.push_back(GV);
215   return S;
216 }
217
218
219 // Override default implementation to put the true globals into
220 // multiple data sections if required.
221 const MCSection *
222 PIC16TargetObjectFile::SelectSectionForGlobal(const GlobalValue *GV1,
223                                               SectionKind Kind,
224                                               Mangler *Mang,
225                                               const TargetMachine &TM) const {
226   // We select the section based on the initializer here, so it really
227   // has to be a GlobalVariable.
228   const GlobalVariable *GV = dyn_cast<GlobalVariable>(GV1); 
229   if (!GV)
230     return TargetLoweringObjectFile::SelectSectionForGlobal(GV1, Kind, Mang,TM);
231
232   assert(GV->hasInitializer() && "A def without initializer?");
233
234   // First, if this is an automatic variable for a function, get the section
235   // name for it and return.
236   std::string name = GV->getName();
237   if (PAN::isLocalName(name))
238     return allocateAUTO(GV);
239
240   // See if this is an uninitialized global.
241   const Constant *C = GV->getInitializer();
242   if (C->isNullValue()) 
243     return allocateUDATA(GV);
244
245   // If this is initialized data in RAM. Put it in the correct IDATA section.
246   if (GV->getType()->getAddressSpace() == PIC16ISD::RAM_SPACE) 
247     return allocateIDATA(GV);
248
249   // This is initialized data in rom, put it in the readonly section.
250   if (GV->getType()->getAddressSpace() == PIC16ISD::ROM_SPACE) 
251     return allocateROMDATA(GV);
252
253   // Else let the default implementation take care of it.
254   return TargetLoweringObjectFile::SelectSectionForGlobal(GV, Kind, Mang,TM);
255 }
256
257 PIC16TargetObjectFile::~PIC16TargetObjectFile() {
258 #if 0
259   for (unsigned i = 0; i < UDATASections_.size(); i++)
260     delete UDATASections_[i]; 
261   for (unsigned i = 0; i < IDATASections_.size(); i++)
262     delete IDATASections_[i]; 
263   
264   delete ROMDATASection_;
265
266   for (unsigned i = 0; i < AUTOSections_.size(); i++)
267     delete AUTOSections_[i]; 
268
269   for (unsigned i = 0; i < USERSections_.size(); i++)
270     delete USERSections_[i];
271 #endif
272 }
273
274
275 /// getExplicitSectionGlobal - Allow the target to completely override
276 /// section assignment of a global.
277 const MCSection *PIC16TargetObjectFile::
278 getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind, 
279                          Mangler *Mang, const TargetMachine &TM) const {
280   assert(GV->hasSection());
281   
282   if (const GlobalVariable *GVar = cast<GlobalVariable>(GV)) {
283     std::string SectName = GVar->getSection();
284     // If address for a variable is specified, get the address and create
285     // section.
286     // FIXME: move this attribute checking in PAN.
287     std::string AddrStr = "Address=";
288     if (SectName.compare(0, AddrStr.length(), AddrStr) == 0) {
289       std::string SectAddr = SectName.substr(AddrStr.length());
290       return allocateAtGivenAddress(GVar, SectAddr);
291     }
292      
293     // Create the section specified with section attribute. 
294     return allocateInGivenSection(GVar);
295   }
296
297   return getPIC16DataSection(GV->getSection().c_str(), UDATA);
298 }
299
300 // Interface used by AsmPrinter to get a code section for a function.
301 const PIC16Section *
302 PIC16TargetObjectFile::SectionForCode(const std::string &FnName) const {
303   const std::string &sec_name = PAN::getCodeSectionName(FnName);
304   return getPIC16Section(sec_name, CODE);
305 }
306
307 // Interface used by AsmPrinter to get a frame section for a function.
308 const PIC16Section *
309 PIC16TargetObjectFile::SectionForFrame(const std::string &FnName) const {
310   const std::string &sec_name = PAN::getFrameSectionName(FnName);
311   return getPIC16Section(sec_name, UDATA_OVR);
312 }
313
314 // Allocate a global var in existing or new section of given name.
315 const MCSection *
316 PIC16TargetObjectFile::allocateInGivenSection(const GlobalVariable *GV) const {
317   // Determine the type of section that we need to create.
318   PIC16SectionType SecTy;
319
320   // See if this is an uninitialized global.
321   const Constant *C = GV->getInitializer();
322   if (C->isNullValue())
323     SecTy = UDATA;
324   // If this is initialized data in RAM. Put it in the correct IDATA section.
325   else if (GV->getType()->getAddressSpace() == PIC16ISD::RAM_SPACE)
326     SecTy = IDATA;
327   // This is initialized data in rom, put it in the readonly section.
328   else if (GV->getType()->getAddressSpace() == PIC16ISD::ROM_SPACE) 
329     SecTy = ROMDATA;
330   else
331     llvm_unreachable ("Could not determine section type for global");
332
333   PIC16Section *S = getPIC16UserSection(GV->getSection().c_str(), SecTy);
334   S->Items.push_back(GV);
335   return S;
336 }
337
338 // Allocate a global var in a new absolute sections at given address.
339 const MCSection *
340 PIC16TargetObjectFile::allocateAtGivenAddress(const GlobalVariable *GV,
341                                                const std::string &Addr) const {
342   // Determine the type of section that we need to create.
343   PIC16SectionType SecTy;
344
345   // See if this is an uninitialized global.
346   const Constant *C = GV->getInitializer();
347   if (C->isNullValue())
348     SecTy = UDATA;
349   // If this is initialized data in RAM. Put it in the correct IDATA section.
350   else if (GV->getType()->getAddressSpace() == PIC16ISD::RAM_SPACE)
351     SecTy = IDATA;
352   // This is initialized data in rom, put it in the readonly section.
353   else if (GV->getType()->getAddressSpace() == PIC16ISD::ROM_SPACE) 
354     SecTy = ROMDATA;
355   else
356     llvm_unreachable ("Could not determine section type for global");
357
358   std::string Prefix = GV->getNameStr() + "." + Addr + ".";
359   std::string SName = PAN::getUserSectionName(Prefix);
360   PIC16Section *S = getPIC16UserSection(SName.c_str(), SecTy, Addr.c_str());
361   S->Items.push_back(GV);
362   return S;
363 }
364
365