blob: 846715de6f3e0f3a1bafbdf96c7176bdaa14ae9a [file] [log] [blame]
Chris Lattnerf0144122009-07-28 03:13:23 +00001//===-- 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"
Sanjiv Gupta753ec152009-10-15 19:26:25 +000013#include "PIC16Section.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000014#include "llvm/DerivedTypes.h"
15#include "llvm/Module.h"
Chris Lattnera87dea42009-07-31 18:48:30 +000016#include "llvm/MC/MCSection.h"
Chris Lattnerfbf1d272009-08-08 20:14:13 +000017#include "llvm/MC/MCContext.h"
Chris Lattner93b6db32009-08-08 23:39:42 +000018#include "llvm/Support/raw_ostream.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000019using namespace llvm;
20
Chris Lattner93b6db32009-08-08 23:39:42 +000021
Sanjiv Gupta753ec152009-10-15 19:26:25 +000022PIC16TargetObjectFile::PIC16TargetObjectFile() {
Chris Lattner93b6db32009-08-08 23:39:42 +000023}
24
Sanjiv Gupta5386a352009-10-16 08:58:34 +000025PIC16TargetObjectFile::~PIC16TargetObjectFile() {
26}
27
Sanjiv Gupta4e4bba52009-10-21 10:42:44 +000028/// Find a pic16 section. Return null if not found. Do not create one.
29PIC16Section *PIC16TargetObjectFile::
30findPIC16Section(const std::string &Name) {
31 /// Return if we have an already existing one.
32 PIC16Section *Entry = SectionsByName[Name];
33 if (Entry)
34 return Entry;
35
36 return NULL;
37}
38
39
Sanjiv Gupta753ec152009-10-15 19:26:25 +000040/// Find a pic16 section. If not found, create one.
41PIC16Section *PIC16TargetObjectFile::
42getPIC16Section(const std::string &Name, PIC16SectionType Ty,
43 const std::string &Address, int Color) const {
Chris Lattner93b6db32009-08-08 23:39:42 +000044
Sanjiv Gupta753ec152009-10-15 19:26:25 +000045 /// Return if we have an already existing one.
46 PIC16Section *&Entry = SectionsByName[Name];
Chris Lattner873bc4c2009-08-13 00:26:52 +000047 if (Entry)
48 return Entry;
49
Sanjiv Gupta753ec152009-10-15 19:26:25 +000050
51 Entry = PIC16Section::Create(Name, Ty, Address, Color, getContext());
52 return Entry;
Chris Lattnerfbf1d272009-08-08 20:14:13 +000053}
54
Sanjiv Gupta753ec152009-10-15 19:26:25 +000055/// Find a standard pic16 data section. If not found, create one and keep
56/// track of it by adding it to appropriate std section list.
57PIC16Section *PIC16TargetObjectFile::
58getPIC16DataSection(const std::string &Name, PIC16SectionType Ty,
59 const std::string &Address, int Color) const {
Chris Lattnerfbf1d272009-08-08 20:14:13 +000060
Sanjiv Gupta753ec152009-10-15 19:26:25 +000061 /// Return if we have an already existing one.
62 PIC16Section *&Entry = SectionsByName[Name];
63 if (Entry)
64 return Entry;
65
66
67 /// Else create a new one and add it to appropriate section list.
68 Entry = PIC16Section::Create(Name, Ty, Address, Color, getContext());
69
70 switch (Ty) {
71 default: llvm_unreachable ("unknow standard section type.");
72 case UDATA: UDATASections_.push_back(Entry); break;
73 case IDATA: IDATASections_.push_back(Entry); break;
74 case ROMDATA: ROMDATASection_ = Entry; break;
Sanjiv Gupta209e6c62009-10-24 18:02:44 +000075 case UDATA_SHR: SHAREDUDATASection_ = Entry; break;
Sanjiv Gupta753ec152009-10-15 19:26:25 +000076 }
77
78 return Entry;
79}
80
81
82/// Find a standard pic16 autos section. If not found, create one and keep
83/// track of it by adding it to appropriate std section list.
84PIC16Section *PIC16TargetObjectFile::
85getPIC16AutoSection(const std::string &Name, PIC16SectionType Ty,
86 const std::string &Address, int Color) const {
87
88 /// Return if we have an already existing one.
89 PIC16Section *&Entry = SectionsByName[Name];
90 if (Entry)
91 return Entry;
92
93
94 /// Else create a new one and add it to appropriate section list.
95 Entry = PIC16Section::Create(Name, Ty, Address, Color, getContext());
96
97 assert (Ty == UDATA_OVR && "incorrect section type for autos");
98 AUTOSections_.push_back(Entry);
99
100 return Entry;
101}
102
103/// Find a pic16 user section. If not found, create one and keep
104/// track of it by adding it to appropriate std section list.
105PIC16Section *PIC16TargetObjectFile::
106getPIC16UserSection(const std::string &Name, PIC16SectionType Ty,
107 const std::string &Address, int Color) const {
108
109 /// Return if we have an already existing one.
110 PIC16Section *&Entry = SectionsByName[Name];
111 if (Entry)
112 return Entry;
113
114
115 /// Else create a new one and add it to appropriate section list.
116 Entry = PIC16Section::Create(Name, Ty, Address, Color, getContext());
117
118 USERSections_.push_back(Entry);
119
120 return Entry;
121}
122
Sanjiv Gupta5386a352009-10-16 08:58:34 +0000123/// Do some standard initialization.
Chris Lattnera87dea42009-07-31 18:48:30 +0000124void PIC16TargetObjectFile::Initialize(MCContext &Ctx, const TargetMachine &tm){
125 TargetLoweringObjectFile::Initialize(Ctx, tm);
126 TM = &tm;
127
Sanjiv Gupta753ec152009-10-15 19:26:25 +0000128 ROMDATASection_ = NULL;
Chris Lattnerf0144122009-07-28 03:13:23 +0000129}
130
Sanjiv Gupta753ec152009-10-15 19:26:25 +0000131/// allocateUDATA - Allocate a un-initialized global to an existing or new UDATA
132/// section and return that section.
Chris Lattnera87dea42009-07-31 18:48:30 +0000133const MCSection *
Sanjiv Gupta753ec152009-10-15 19:26:25 +0000134PIC16TargetObjectFile::allocateUDATA(const GlobalVariable *GV) const {
Chris Lattnerf0144122009-07-28 03:13:23 +0000135 assert(GV->hasInitializer() && "This global doesn't need space");
136 Constant *C = GV->getInitializer();
137 assert(C->isNullValue() && "Unitialized globals has non-zero initializer");
138
139 // Find how much space this global needs.
Chris Lattnera87dea42009-07-31 18:48:30 +0000140 const TargetData *TD = TM->getTargetData();
Chris Lattnerf0144122009-07-28 03:13:23 +0000141 const Type *Ty = C->getType();
142 unsigned ValSize = TD->getTypeAllocSize(Ty);
143
Sanjiv Gupta753ec152009-10-15 19:26:25 +0000144 // Go through all UDATA Sections and assign this variable
Chris Lattnerf0144122009-07-28 03:13:23 +0000145 // to the first available section having enough space.
Sanjiv Gupta753ec152009-10-15 19:26:25 +0000146 PIC16Section *Found = NULL;
147 for (unsigned i = 0; i < UDATASections_.size(); i++) {
148 if (DataBankSize - UDATASections_[i]->getSize() >= ValSize) {
149 Found = UDATASections_[i];
Chris Lattnerf0144122009-07-28 03:13:23 +0000150 break;
151 }
152 }
153
Sanjiv Gupta753ec152009-10-15 19:26:25 +0000154 // No UDATA section spacious enough was found. Crate a new one.
155 if (!Found) {
156 std::string name = PAN::getUdataSectionName(UDATASections_.size());
157 Found = getPIC16DataSection(name.c_str(), UDATA);
Chris Lattnerf0144122009-07-28 03:13:23 +0000158 }
159
Sanjiv Gupta753ec152009-10-15 19:26:25 +0000160 // Insert the GV into this UDATA section.
161 Found->Items.push_back(GV);
162 Found->setSize(Found->getSize() + ValSize);
163 return Found;
Chris Lattnerf0144122009-07-28 03:13:23 +0000164}
165
Sanjiv Gupta753ec152009-10-15 19:26:25 +0000166/// allocateIDATA - allocate an initialized global into an existing
167/// or new section and return that section.
Chris Lattnera87dea42009-07-31 18:48:30 +0000168const MCSection *
Sanjiv Gupta753ec152009-10-15 19:26:25 +0000169PIC16TargetObjectFile::allocateIDATA(const GlobalVariable *GV) const{
Chris Lattnerf0144122009-07-28 03:13:23 +0000170 assert(GV->hasInitializer() && "This global doesn't need space");
171 Constant *C = GV->getInitializer();
172 assert(!C->isNullValue() && "initialized globals has zero initializer");
173 assert(GV->getType()->getAddressSpace() == PIC16ISD::RAM_SPACE &&
Sanjiv Gupta753ec152009-10-15 19:26:25 +0000174 "can allocate initialized RAM data only");
Chris Lattnerf0144122009-07-28 03:13:23 +0000175
176 // Find how much space this global needs.
Chris Lattnera87dea42009-07-31 18:48:30 +0000177 const TargetData *TD = TM->getTargetData();
Chris Lattnerf0144122009-07-28 03:13:23 +0000178 const Type *Ty = C->getType();
179 unsigned ValSize = TD->getTypeAllocSize(Ty);
180
181 // Go through all IDATA Sections and assign this variable
182 // to the first available section having enough space.
Sanjiv Gupta753ec152009-10-15 19:26:25 +0000183 PIC16Section *Found = NULL;
184 for (unsigned i = 0; i < IDATASections_.size(); i++) {
185 if (DataBankSize - IDATASections_[i]->getSize() >= ValSize) {
186 Found = IDATASections_[i];
Chris Lattnerf0144122009-07-28 03:13:23 +0000187 break;
188 }
189 }
190
191 // No IDATA section spacious enough was found. Crate a new one.
Sanjiv Gupta753ec152009-10-15 19:26:25 +0000192 if (!Found) {
193 std::string name = PAN::getIdataSectionName(IDATASections_.size());
194 Found = getPIC16DataSection(name.c_str(), IDATA);
Chris Lattnerf0144122009-07-28 03:13:23 +0000195 }
196
197 // Insert the GV into this IDATA.
Sanjiv Gupta753ec152009-10-15 19:26:25 +0000198 Found->Items.push_back(GV);
199 Found->setSize(Found->getSize() + ValSize);
200 return Found;
Chris Lattnerf0144122009-07-28 03:13:23 +0000201}
202
Sanjiv Gupta753ec152009-10-15 19:26:25 +0000203// Allocate a program memory variable into ROMDATA section.
204const MCSection *
205PIC16TargetObjectFile::allocateROMDATA(const GlobalVariable *GV) const {
206
207 std::string name = PAN::getRomdataSectionName();
208 PIC16Section *S = getPIC16DataSection(name.c_str(), ROMDATA);
209
210 S->Items.push_back(GV);
211 return S;
212}
213
Chris Lattnerf0144122009-07-28 03:13:23 +0000214// Get the section for an automatic variable of a function.
215// For PIC16 they are globals only with mangled names.
Chris Lattnera87dea42009-07-31 18:48:30 +0000216const MCSection *
Sanjiv Gupta753ec152009-10-15 19:26:25 +0000217PIC16TargetObjectFile::allocateAUTO(const GlobalVariable *GV) const {
Chris Lattnerf0144122009-07-28 03:13:23 +0000218
219 const std::string name = PAN::getSectionNameForSym(GV->getName());
Sanjiv Gupta753ec152009-10-15 19:26:25 +0000220 PIC16Section *S = getPIC16AutoSection(name.c_str());
Chris Lattnerf0144122009-07-28 03:13:23 +0000221
Sanjiv Gupta753ec152009-10-15 19:26:25 +0000222 S->Items.push_back(GV);
223 return S;
Chris Lattnerf0144122009-07-28 03:13:23 +0000224}
225
226
227// Override default implementation to put the true globals into
228// multiple data sections if required.
Chris Lattnera87dea42009-07-31 18:48:30 +0000229const MCSection *
Chris Lattnerf0144122009-07-28 03:13:23 +0000230PIC16TargetObjectFile::SelectSectionForGlobal(const GlobalValue *GV1,
Chris Lattnerf9650c02009-08-01 21:46:23 +0000231 SectionKind Kind,
Chris Lattnere53a6002009-07-29 05:09:30 +0000232 Mangler *Mang,
Chris Lattnerf0144122009-07-28 03:13:23 +0000233 const TargetMachine &TM) const {
234 // We select the section based on the initializer here, so it really
235 // has to be a GlobalVariable.
236 const GlobalVariable *GV = dyn_cast<GlobalVariable>(GV1);
237 if (!GV)
Chris Lattnerf9650c02009-08-01 21:46:23 +0000238 return TargetLoweringObjectFile::SelectSectionForGlobal(GV1, Kind, Mang,TM);
Chris Lattnerf0144122009-07-28 03:13:23 +0000239
Chris Lattnerf0144122009-07-28 03:13:23 +0000240 assert(GV->hasInitializer() && "A def without initializer?");
241
242 // First, if this is an automatic variable for a function, get the section
243 // name for it and return.
244 std::string name = GV->getName();
245 if (PAN::isLocalName(name))
Sanjiv Gupta753ec152009-10-15 19:26:25 +0000246 return allocateAUTO(GV);
Chris Lattnerf0144122009-07-28 03:13:23 +0000247
248 // See if this is an uninitialized global.
249 const Constant *C = GV->getInitializer();
250 if (C->isNullValue())
Sanjiv Gupta753ec152009-10-15 19:26:25 +0000251 return allocateUDATA(GV);
Chris Lattnerf0144122009-07-28 03:13:23 +0000252
253 // If this is initialized data in RAM. Put it in the correct IDATA section.
254 if (GV->getType()->getAddressSpace() == PIC16ISD::RAM_SPACE)
Sanjiv Gupta753ec152009-10-15 19:26:25 +0000255 return allocateIDATA(GV);
Chris Lattnerf0144122009-07-28 03:13:23 +0000256
257 // This is initialized data in rom, put it in the readonly section.
258 if (GV->getType()->getAddressSpace() == PIC16ISD::ROM_SPACE)
Sanjiv Gupta753ec152009-10-15 19:26:25 +0000259 return allocateROMDATA(GV);
Chris Lattnerf0144122009-07-28 03:13:23 +0000260
261 // Else let the default implementation take care of it.
Chris Lattnerf9650c02009-08-01 21:46:23 +0000262 return TargetLoweringObjectFile::SelectSectionForGlobal(GV, Kind, Mang,TM);
Chris Lattnerf0144122009-07-28 03:13:23 +0000263}
264
Sanjiv Gupta753ec152009-10-15 19:26:25 +0000265
Chris Lattnerf0144122009-07-28 03:13:23 +0000266
267
Sanjiv Gupta753ec152009-10-15 19:26:25 +0000268/// getExplicitSectionGlobal - Allow the target to completely override
Chris Lattnerf0144122009-07-28 03:13:23 +0000269/// section assignment of a global.
Chris Lattner24f654c2009-08-06 16:39:58 +0000270const MCSection *PIC16TargetObjectFile::
271getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
272 Mangler *Mang, const TargetMachine &TM) const {
273 assert(GV->hasSection());
274
275 if (const GlobalVariable *GVar = cast<GlobalVariable>(GV)) {
276 std::string SectName = GVar->getSection();
277 // If address for a variable is specified, get the address and create
278 // section.
Sanjiv Gupta753ec152009-10-15 19:26:25 +0000279 // FIXME: move this attribute checking in PAN.
Chris Lattner24f654c2009-08-06 16:39:58 +0000280 std::string AddrStr = "Address=";
281 if (SectName.compare(0, AddrStr.length(), AddrStr) == 0) {
282 std::string SectAddr = SectName.substr(AddrStr.length());
Sanjiv Gupta209e6c62009-10-24 18:02:44 +0000283 if (SectAddr.compare("NEAR") == 0)
284 return allocateSHARED(GVar, Mang);
285 else
286 return allocateAtGivenAddress(GVar, SectAddr);
Chris Lattnerf0144122009-07-28 03:13:23 +0000287 }
Chris Lattner24f654c2009-08-06 16:39:58 +0000288
289 // Create the section specified with section attribute.
Sanjiv Gupta753ec152009-10-15 19:26:25 +0000290 return allocateInGivenSection(GVar);
Chris Lattnerf0144122009-07-28 03:13:23 +0000291 }
292
Sanjiv Gupta753ec152009-10-15 19:26:25 +0000293 return getPIC16DataSection(GV->getSection().c_str(), UDATA);
Chris Lattnerf0144122009-07-28 03:13:23 +0000294}
295
Sanjiv Gupta209e6c62009-10-24 18:02:44 +0000296const MCSection *
297PIC16TargetObjectFile::allocateSHARED(const GlobalVariable *GV,
298 Mangler *Mang) const {
299 // Make sure that this is an uninitialized global.
300 assert(GV->hasInitializer() && "This global doesn't need space");
301 if (!GV->getInitializer()->isNullValue()) {
302 // FIXME: Generate a warning in this case that near qualifier will be
303 // ignored.
304 return SelectSectionForGlobal(GV, SectionKind::getDataRel(), Mang, *TM);
305 }
306 std::string Name = PAN::getSharedUDataSectionName();
307
308 PIC16Section *SharedUDataSect = getPIC16DataSection(Name.c_str(), UDATA_SHR);
309 // Insert the GV into shared section.
310 SharedUDataSect->Items.push_back(GV);
311 return SharedUDataSect;
312}
313
314
Sanjiv Gupta753ec152009-10-15 19:26:25 +0000315// Interface used by AsmPrinter to get a code section for a function.
316const PIC16Section *
317PIC16TargetObjectFile::SectionForCode(const std::string &FnName) const {
318 const std::string &sec_name = PAN::getCodeSectionName(FnName);
319 return getPIC16Section(sec_name, CODE);
320}
321
322// Interface used by AsmPrinter to get a frame section for a function.
323const PIC16Section *
324PIC16TargetObjectFile::SectionForFrame(const std::string &FnName) const {
325 const std::string &sec_name = PAN::getFrameSectionName(FnName);
326 return getPIC16Section(sec_name, UDATA_OVR);
327}
328
329// Allocate a global var in existing or new section of given name.
Chris Lattnera87dea42009-07-31 18:48:30 +0000330const MCSection *
Sanjiv Gupta753ec152009-10-15 19:26:25 +0000331PIC16TargetObjectFile::allocateInGivenSection(const GlobalVariable *GV) const {
332 // Determine the type of section that we need to create.
333 PIC16SectionType SecTy;
334
Chris Lattnerf0144122009-07-28 03:13:23 +0000335 // See if this is an uninitialized global.
336 const Constant *C = GV->getInitializer();
337 if (C->isNullValue())
Sanjiv Gupta753ec152009-10-15 19:26:25 +0000338 SecTy = UDATA;
Daniel Dunbar1ead1502009-10-15 15:02:14 +0000339 // If this is initialized data in RAM. Put it in the correct IDATA section.
Sanjiv Gupta753ec152009-10-15 19:26:25 +0000340 else if (GV->getType()->getAddressSpace() == PIC16ISD::RAM_SPACE)
341 SecTy = IDATA;
Daniel Dunbar1ead1502009-10-15 15:02:14 +0000342 // This is initialized data in rom, put it in the readonly section.
Sanjiv Gupta753ec152009-10-15 19:26:25 +0000343 else if (GV->getType()->getAddressSpace() == PIC16ISD::ROM_SPACE)
344 SecTy = ROMDATA;
345 else
346 llvm_unreachable ("Could not determine section type for global");
Daniel Dunbar1ead1502009-10-15 15:02:14 +0000347
Sanjiv Gupta753ec152009-10-15 19:26:25 +0000348 PIC16Section *S = getPIC16UserSection(GV->getSection().c_str(), SecTy);
349 S->Items.push_back(GV);
350 return S;
Chris Lattnerf0144122009-07-28 03:13:23 +0000351}
352
Sanjiv Gupta753ec152009-10-15 19:26:25 +0000353// Allocate a global var in a new absolute sections at given address.
Chris Lattnera87dea42009-07-31 18:48:30 +0000354const MCSection *
Sanjiv Gupta753ec152009-10-15 19:26:25 +0000355PIC16TargetObjectFile::allocateAtGivenAddress(const GlobalVariable *GV,
356 const std::string &Addr) const {
357 // Determine the type of section that we need to create.
358 PIC16SectionType SecTy;
Chris Lattnerf0144122009-07-28 03:13:23 +0000359
Sanjiv Gupta753ec152009-10-15 19:26:25 +0000360 // See if this is an uninitialized global.
361 const Constant *C = GV->getInitializer();
362 if (C->isNullValue())
363 SecTy = UDATA;
364 // If this is initialized data in RAM. Put it in the correct IDATA section.
365 else if (GV->getType()->getAddressSpace() == PIC16ISD::RAM_SPACE)
366 SecTy = IDATA;
367 // This is initialized data in rom, put it in the readonly section.
368 else if (GV->getType()->getAddressSpace() == PIC16ISD::ROM_SPACE)
369 SecTy = ROMDATA;
370 else
371 llvm_unreachable ("Could not determine section type for global");
Chris Lattnerf0144122009-07-28 03:13:23 +0000372
Sanjiv Gupta753ec152009-10-15 19:26:25 +0000373 std::string Prefix = GV->getNameStr() + "." + Addr + ".";
374 std::string SName = PAN::getUserSectionName(Prefix);
375 PIC16Section *S = getPIC16UserSection(SName.c_str(), SecTy, Addr.c_str());
376 S->Items.push_back(GV);
377 return S;
Chris Lattnerf0144122009-07-28 03:13:23 +0000378}
379
Chris Lattnerf0144122009-07-28 03:13:23 +0000380