blob: 330722d4a9c34f228d7da0b81b9e8bfae04963f1 [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 Gupta8da373c2009-10-15 10:10:43 +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 Gupta8da373c2009-10-15 10:10:43 +000022PIC16TargetObjectFile::PIC16TargetObjectFile() {
Chris Lattner93b6db32009-08-08 23:39:42 +000023}
24
Sanjiv Gupta8da373c2009-10-15 10:10:43 +000025/// Find a pic16 section. If not found, create one.
26PIC16Section *PIC16TargetObjectFile::
27getPIC16Section(const std::string &Name, PIC16SectionType Ty,
28 const std::string &Address, int Color) const {
Chris Lattner93b6db32009-08-08 23:39:42 +000029
Sanjiv Gupta8da373c2009-10-15 10:10:43 +000030 /// Return if we have an already existing one.
31 PIC16Section *&Entry = SectionsByName[Name];
Chris Lattner873bc4c2009-08-13 00:26:52 +000032 if (Entry)
33 return Entry;
34
Sanjiv Gupta8da373c2009-10-15 10:10:43 +000035
36 Entry = PIC16Section::Create(Name, Ty, Address, Color, getContext());
37 return Entry;
Chris Lattnerfbf1d272009-08-08 20:14:13 +000038}
39
Sanjiv Gupta8da373c2009-10-15 10:10:43 +000040/// 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.
42PIC16Section *PIC16TargetObjectFile::
43getPIC16DataSection(const std::string &Name, PIC16SectionType Ty,
44 const std::string &Address, int Color) const {
Chris Lattnerfbf1d272009-08-08 20:14:13 +000045
Sanjiv Gupta8da373c2009-10-15 10:10:43 +000046 /// 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.
68PIC16Section *PIC16TargetObjectFile::
69getPIC16AutoSection(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.
89PIC16Section *PIC16TargetObjectFile::
90getPIC16UserSection(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.
Chris Lattnera87dea42009-07-31 18:48:30 +0000108void PIC16TargetObjectFile::Initialize(MCContext &Ctx, const TargetMachine &tm){
109 TargetLoweringObjectFile::Initialize(Ctx, tm);
110 TM = &tm;
111
Sanjiv Gupta8da373c2009-10-15 10:10:43 +0000112 // BSSSection = getPIC16DataSection("udata.#", UDATA);
113 // ReadOnlySection = getPIC16DataSection("romdata.#", ROMDATA);
114 // DataSection = getPIC16DataSection("idata.#", IDATA);
Chris Lattnerf0144122009-07-28 03:13:23 +0000115
116 // Need because otherwise a .text symbol is emitted by DwarfWriter
117 // in BeginModule, and gpasm cribbs for that .text symbol.
Sanjiv Gupta8da373c2009-10-15 10:10:43 +0000118 // FIXME: below
119 // TextSection = getPIC16DataSection("", UDATA);
120 ROMDATASection_ = NULL;
Chris Lattnerf0144122009-07-28 03:13:23 +0000121}
122
Sanjiv Gupta8da373c2009-10-15 10:10:43 +0000123/// allocateUDATA - Allocate a un-initialized global to an existing or new UDATA
124/// section and return that section.
Chris Lattnera87dea42009-07-31 18:48:30 +0000125const MCSection *
Sanjiv Gupta8da373c2009-10-15 10:10:43 +0000126PIC16TargetObjectFile::allocateUDATA(const GlobalVariable *GV) const {
Chris Lattnerf0144122009-07-28 03:13:23 +0000127 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.
Chris Lattnera87dea42009-07-31 18:48:30 +0000132 const TargetData *TD = TM->getTargetData();
Chris Lattnerf0144122009-07-28 03:13:23 +0000133 const Type *Ty = C->getType();
134 unsigned ValSize = TD->getTypeAllocSize(Ty);
135
Sanjiv Gupta8da373c2009-10-15 10:10:43 +0000136 // Go through all UDATA Sections and assign this variable
Chris Lattnerf0144122009-07-28 03:13:23 +0000137 // to the first available section having enough space.
Sanjiv Gupta8da373c2009-10-15 10:10:43 +0000138 PIC16Section *Found = NULL;
139 for (unsigned i = 0; i < UDATASections_.size(); i++) {
140 if (DataBankSize - UDATASections_[i]->getSize() >= ValSize) {
141 Found = UDATASections_[i];
Chris Lattnerf0144122009-07-28 03:13:23 +0000142 break;
143 }
144 }
145
Sanjiv Gupta8da373c2009-10-15 10:10:43 +0000146 // 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);
Chris Lattnerf0144122009-07-28 03:13:23 +0000150 }
151
Sanjiv Gupta8da373c2009-10-15 10:10:43 +0000152 // Insert the GV into this UDATA section.
153 Found->Items.push_back(GV);
154 Found->setSize(Found->getSize() + ValSize);
155 return Found;
Chris Lattnerf0144122009-07-28 03:13:23 +0000156}
157
Sanjiv Gupta8da373c2009-10-15 10:10:43 +0000158/// allocateIDATA - allocate an initialized global into an existing
159/// or new section and return that section.
Chris Lattnera87dea42009-07-31 18:48:30 +0000160const MCSection *
Sanjiv Gupta8da373c2009-10-15 10:10:43 +0000161PIC16TargetObjectFile::allocateIDATA(const GlobalVariable *GV) const{
Chris Lattnerf0144122009-07-28 03:13:23 +0000162 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 &&
Sanjiv Gupta8da373c2009-10-15 10:10:43 +0000166 "can allocate initialized RAM data only");
Chris Lattnerf0144122009-07-28 03:13:23 +0000167
168 // Find how much space this global needs.
Chris Lattnera87dea42009-07-31 18:48:30 +0000169 const TargetData *TD = TM->getTargetData();
Chris Lattnerf0144122009-07-28 03:13:23 +0000170 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.
Sanjiv Gupta8da373c2009-10-15 10:10:43 +0000175 PIC16Section *Found = NULL;
176 for (unsigned i = 0; i < IDATASections_.size(); i++) {
177 if (DataBankSize - IDATASections_[i]->getSize() >= ValSize) {
178 Found = IDATASections_[i];
Chris Lattnerf0144122009-07-28 03:13:23 +0000179 break;
180 }
181 }
182
183 // No IDATA section spacious enough was found. Crate a new one.
Sanjiv Gupta8da373c2009-10-15 10:10:43 +0000184 if (!Found) {
185 std::string name = PAN::getIdataSectionName(IDATASections_.size());
186 Found = getPIC16DataSection(name.c_str(), IDATA);
Chris Lattnerf0144122009-07-28 03:13:23 +0000187 }
188
189 // Insert the GV into this IDATA.
Sanjiv Gupta8da373c2009-10-15 10:10:43 +0000190 Found->Items.push_back(GV);
191 Found->setSize(Found->getSize() + ValSize);
192 return Found;
Chris Lattnerf0144122009-07-28 03:13:23 +0000193}
194
Sanjiv Gupta8da373c2009-10-15 10:10:43 +0000195// Allocate a program memory variable into ROMDATA section.
196const MCSection *
197PIC16TargetObjectFile::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
Chris Lattnerf0144122009-07-28 03:13:23 +0000206// Get the section for an automatic variable of a function.
207// For PIC16 they are globals only with mangled names.
Chris Lattnera87dea42009-07-31 18:48:30 +0000208const MCSection *
Sanjiv Gupta8da373c2009-10-15 10:10:43 +0000209PIC16TargetObjectFile::allocateAUTO(const GlobalVariable *GV) const {
Chris Lattnerf0144122009-07-28 03:13:23 +0000210
211 const std::string name = PAN::getSectionNameForSym(GV->getName());
Sanjiv Gupta8da373c2009-10-15 10:10:43 +0000212 PIC16Section *S = getPIC16AutoSection(name.c_str());
Chris Lattnerf0144122009-07-28 03:13:23 +0000213
Sanjiv Gupta8da373c2009-10-15 10:10:43 +0000214 S->Items.push_back(GV);
215 return S;
Chris Lattnerf0144122009-07-28 03:13:23 +0000216}
217
218
219// Override default implementation to put the true globals into
220// multiple data sections if required.
Chris Lattnera87dea42009-07-31 18:48:30 +0000221const MCSection *
Chris Lattnerf0144122009-07-28 03:13:23 +0000222PIC16TargetObjectFile::SelectSectionForGlobal(const GlobalValue *GV1,
Chris Lattnerf9650c02009-08-01 21:46:23 +0000223 SectionKind Kind,
Chris Lattnere53a6002009-07-29 05:09:30 +0000224 Mangler *Mang,
Chris Lattnerf0144122009-07-28 03:13:23 +0000225 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)
Chris Lattnerf9650c02009-08-01 21:46:23 +0000230 return TargetLoweringObjectFile::SelectSectionForGlobal(GV1, Kind, Mang,TM);
Chris Lattnerf0144122009-07-28 03:13:23 +0000231
Chris Lattnerf0144122009-07-28 03:13:23 +0000232 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))
Sanjiv Gupta8da373c2009-10-15 10:10:43 +0000238 return allocateAUTO(GV);
Chris Lattnerf0144122009-07-28 03:13:23 +0000239
240 // See if this is an uninitialized global.
241 const Constant *C = GV->getInitializer();
242 if (C->isNullValue())
Sanjiv Gupta8da373c2009-10-15 10:10:43 +0000243 return allocateUDATA(GV);
Chris Lattnerf0144122009-07-28 03:13:23 +0000244
245 // If this is initialized data in RAM. Put it in the correct IDATA section.
246 if (GV->getType()->getAddressSpace() == PIC16ISD::RAM_SPACE)
Sanjiv Gupta8da373c2009-10-15 10:10:43 +0000247 return allocateIDATA(GV);
Chris Lattnerf0144122009-07-28 03:13:23 +0000248
249 // This is initialized data in rom, put it in the readonly section.
250 if (GV->getType()->getAddressSpace() == PIC16ISD::ROM_SPACE)
Sanjiv Gupta8da373c2009-10-15 10:10:43 +0000251 return allocateROMDATA(GV);
Chris Lattnerf0144122009-07-28 03:13:23 +0000252
253 // Else let the default implementation take care of it.
Chris Lattnerf9650c02009-08-01 21:46:23 +0000254 return TargetLoweringObjectFile::SelectSectionForGlobal(GV, Kind, Mang,TM);
Chris Lattnerf0144122009-07-28 03:13:23 +0000255}
256
257PIC16TargetObjectFile::~PIC16TargetObjectFile() {
Sanjiv Gupta8da373c2009-10-15 10:10:43 +0000258#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
Chris Lattnerf0144122009-07-28 03:13:23 +0000272}
273
274
Sanjiv Gupta8da373c2009-10-15 10:10:43 +0000275/// getExplicitSectionGlobal - Allow the target to completely override
Chris Lattnerf0144122009-07-28 03:13:23 +0000276/// section assignment of a global.
Chris Lattner24f654c2009-08-06 16:39:58 +0000277const MCSection *PIC16TargetObjectFile::
278getExplicitSectionGlobal(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.
Sanjiv Gupta8da373c2009-10-15 10:10:43 +0000286 // FIXME: move this attribute checking in PAN.
Chris Lattner24f654c2009-08-06 16:39:58 +0000287 std::string AddrStr = "Address=";
288 if (SectName.compare(0, AddrStr.length(), AddrStr) == 0) {
289 std::string SectAddr = SectName.substr(AddrStr.length());
Sanjiv Gupta8da373c2009-10-15 10:10:43 +0000290 return allocateAtGivenAddress(GVar, SectAddr);
Chris Lattnerf0144122009-07-28 03:13:23 +0000291 }
Chris Lattner24f654c2009-08-06 16:39:58 +0000292
293 // Create the section specified with section attribute.
Sanjiv Gupta8da373c2009-10-15 10:10:43 +0000294 return allocateInGivenSection(GVar);
Chris Lattnerf0144122009-07-28 03:13:23 +0000295 }
296
Sanjiv Gupta8da373c2009-10-15 10:10:43 +0000297 return getPIC16DataSection(GV->getSection().c_str(), UDATA);
Chris Lattnerf0144122009-07-28 03:13:23 +0000298}
299
Sanjiv Gupta8da373c2009-10-15 10:10:43 +0000300// Interface used by AsmPrinter to get a code section for a function.
301const PIC16Section *
302PIC16TargetObjectFile::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.
308const PIC16Section *
309PIC16TargetObjectFile::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.
Chris Lattnera87dea42009-07-31 18:48:30 +0000315const MCSection *
Sanjiv Gupta8da373c2009-10-15 10:10:43 +0000316PIC16TargetObjectFile::allocateInGivenSection(const GlobalVariable *GV) const {
317 // Determine the type of section that we need to create.
318 PIC16SectionType SecTy;
319
Chris Lattnerf0144122009-07-28 03:13:23 +0000320 // See if this is an uninitialized global.
321 const Constant *C = GV->getInitializer();
322 if (C->isNullValue())
Sanjiv Gupta8da373c2009-10-15 10:10:43 +0000323 SecTy = UDATA;
Chris Lattnerf0144122009-07-28 03:13:23 +0000324 // If this is initialized data in RAM. Put it in the correct IDATA section.
Sanjiv Gupta8da373c2009-10-15 10:10:43 +0000325 else if (GV->getType()->getAddressSpace() == PIC16ISD::RAM_SPACE)
326 SecTy = IDATA;
Chris Lattnerf0144122009-07-28 03:13:23 +0000327 // This is initialized data in rom, put it in the readonly section.
Sanjiv Gupta8da373c2009-10-15 10:10:43 +0000328 else if (GV->getType()->getAddressSpace() == PIC16ISD::ROM_SPACE)
329 SecTy = ROMDATA;
330 else
331 llvm_unreachable ("Could not determine section type for global");
Chris Lattnerf0144122009-07-28 03:13:23 +0000332
Sanjiv Gupta8da373c2009-10-15 10:10:43 +0000333 PIC16Section *S = getPIC16UserSection(GV->getSection().c_str(), SecTy);
334 S->Items.push_back(GV);
335 return S;
Chris Lattnerf0144122009-07-28 03:13:23 +0000336}
337
Sanjiv Gupta8da373c2009-10-15 10:10:43 +0000338// Allocate a global var in a new absolute sections at given address.
Chris Lattnera87dea42009-07-31 18:48:30 +0000339const MCSection *
Sanjiv Gupta8da373c2009-10-15 10:10:43 +0000340PIC16TargetObjectFile::allocateAtGivenAddress(const GlobalVariable *GV,
341 const std::string &Addr) const {
342 // Determine the type of section that we need to create.
343 PIC16SectionType SecTy;
Chris Lattnerf0144122009-07-28 03:13:23 +0000344
Sanjiv Gupta8da373c2009-10-15 10:10:43 +0000345 // 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");
Chris Lattnerf0144122009-07-28 03:13:23 +0000357
Sanjiv Gupta8da373c2009-10-15 10:10:43 +0000358 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;
Chris Lattnerf0144122009-07-28 03:13:23 +0000363}
364
Chris Lattnerf0144122009-07-28 03:13:23 +0000365