blob: 11c579cc570067d528b7b97e671d6dc31c8d27b2 [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 Gupta753ec152009-10-15 19:26:25 +000028/// Find a pic16 section. If not found, create one.
29PIC16Section *PIC16TargetObjectFile::
30getPIC16Section(const std::string &Name, PIC16SectionType Ty,
31 const std::string &Address, int Color) const {
Chris Lattner93b6db32009-08-08 23:39:42 +000032
Sanjiv Gupta753ec152009-10-15 19:26:25 +000033 /// Return if we have an already existing one.
34 PIC16Section *&Entry = SectionsByName[Name];
Chris Lattner873bc4c2009-08-13 00:26:52 +000035 if (Entry)
36 return Entry;
37
Sanjiv Gupta753ec152009-10-15 19:26:25 +000038
39 Entry = PIC16Section::Create(Name, Ty, Address, Color, getContext());
40 return Entry;
Chris Lattnerfbf1d272009-08-08 20:14:13 +000041}
42
Sanjiv Gupta753ec152009-10-15 19:26:25 +000043/// Find a standard pic16 data section. If not found, create one and keep
44/// track of it by adding it to appropriate std section list.
45PIC16Section *PIC16TargetObjectFile::
46getPIC16DataSection(const std::string &Name, PIC16SectionType Ty,
47 const std::string &Address, int Color) const {
Chris Lattnerfbf1d272009-08-08 20:14:13 +000048
Sanjiv Gupta753ec152009-10-15 19:26:25 +000049 /// Return if we have an already existing one.
50 PIC16Section *&Entry = SectionsByName[Name];
51 if (Entry)
52 return Entry;
53
54
55 /// Else create a new one and add it to appropriate section list.
56 Entry = PIC16Section::Create(Name, Ty, Address, Color, getContext());
57
58 switch (Ty) {
59 default: llvm_unreachable ("unknow standard section type.");
60 case UDATA: UDATASections_.push_back(Entry); break;
61 case IDATA: IDATASections_.push_back(Entry); break;
62 case ROMDATA: ROMDATASection_ = Entry; break;
63 }
64
65 return Entry;
66}
67
68
69/// Find a standard pic16 autos section. If not found, create one and keep
70/// track of it by adding it to appropriate std section list.
71PIC16Section *PIC16TargetObjectFile::
72getPIC16AutoSection(const std::string &Name, PIC16SectionType Ty,
73 const std::string &Address, int Color) const {
74
75 /// Return if we have an already existing one.
76 PIC16Section *&Entry = SectionsByName[Name];
77 if (Entry)
78 return Entry;
79
80
81 /// Else create a new one and add it to appropriate section list.
82 Entry = PIC16Section::Create(Name, Ty, Address, Color, getContext());
83
84 assert (Ty == UDATA_OVR && "incorrect section type for autos");
85 AUTOSections_.push_back(Entry);
86
87 return Entry;
88}
89
90/// Find a pic16 user section. If not found, create one and keep
91/// track of it by adding it to appropriate std section list.
92PIC16Section *PIC16TargetObjectFile::
93getPIC16UserSection(const std::string &Name, PIC16SectionType Ty,
94 const std::string &Address, int Color) const {
95
96 /// Return if we have an already existing one.
97 PIC16Section *&Entry = SectionsByName[Name];
98 if (Entry)
99 return Entry;
100
101
102 /// Else create a new one and add it to appropriate section list.
103 Entry = PIC16Section::Create(Name, Ty, Address, Color, getContext());
104
105 USERSections_.push_back(Entry);
106
107 return Entry;
108}
109
Sanjiv Gupta5386a352009-10-16 08:58:34 +0000110/// Do some standard initialization.
Chris Lattnera87dea42009-07-31 18:48:30 +0000111void PIC16TargetObjectFile::Initialize(MCContext &Ctx, const TargetMachine &tm){
112 TargetLoweringObjectFile::Initialize(Ctx, tm);
113 TM = &tm;
114
Sanjiv Gupta753ec152009-10-15 19:26:25 +0000115 ROMDATASection_ = NULL;
Chris Lattnerf0144122009-07-28 03:13:23 +0000116}
117
Sanjiv Gupta753ec152009-10-15 19:26:25 +0000118/// allocateUDATA - Allocate a un-initialized global to an existing or new UDATA
119/// section and return that section.
Chris Lattnera87dea42009-07-31 18:48:30 +0000120const MCSection *
Sanjiv Gupta753ec152009-10-15 19:26:25 +0000121PIC16TargetObjectFile::allocateUDATA(const GlobalVariable *GV) const {
Chris Lattnerf0144122009-07-28 03:13:23 +0000122 assert(GV->hasInitializer() && "This global doesn't need space");
123 Constant *C = GV->getInitializer();
124 assert(C->isNullValue() && "Unitialized globals has non-zero initializer");
125
126 // Find how much space this global needs.
Chris Lattnera87dea42009-07-31 18:48:30 +0000127 const TargetData *TD = TM->getTargetData();
Chris Lattnerf0144122009-07-28 03:13:23 +0000128 const Type *Ty = C->getType();
129 unsigned ValSize = TD->getTypeAllocSize(Ty);
130
Sanjiv Gupta753ec152009-10-15 19:26:25 +0000131 // Go through all UDATA Sections and assign this variable
Chris Lattnerf0144122009-07-28 03:13:23 +0000132 // to the first available section having enough space.
Sanjiv Gupta753ec152009-10-15 19:26:25 +0000133 PIC16Section *Found = NULL;
134 for (unsigned i = 0; i < UDATASections_.size(); i++) {
135 if (DataBankSize - UDATASections_[i]->getSize() >= ValSize) {
136 Found = UDATASections_[i];
Chris Lattnerf0144122009-07-28 03:13:23 +0000137 break;
138 }
139 }
140
Sanjiv Gupta753ec152009-10-15 19:26:25 +0000141 // No UDATA section spacious enough was found. Crate a new one.
142 if (!Found) {
143 std::string name = PAN::getUdataSectionName(UDATASections_.size());
144 Found = getPIC16DataSection(name.c_str(), UDATA);
Chris Lattnerf0144122009-07-28 03:13:23 +0000145 }
146
Sanjiv Gupta753ec152009-10-15 19:26:25 +0000147 // Insert the GV into this UDATA section.
148 Found->Items.push_back(GV);
149 Found->setSize(Found->getSize() + ValSize);
150 return Found;
Chris Lattnerf0144122009-07-28 03:13:23 +0000151}
152
Sanjiv Gupta753ec152009-10-15 19:26:25 +0000153/// allocateIDATA - allocate an initialized global into an existing
154/// or new section and return that section.
Chris Lattnera87dea42009-07-31 18:48:30 +0000155const MCSection *
Sanjiv Gupta753ec152009-10-15 19:26:25 +0000156PIC16TargetObjectFile::allocateIDATA(const GlobalVariable *GV) const{
Chris Lattnerf0144122009-07-28 03:13:23 +0000157 assert(GV->hasInitializer() && "This global doesn't need space");
158 Constant *C = GV->getInitializer();
159 assert(!C->isNullValue() && "initialized globals has zero initializer");
160 assert(GV->getType()->getAddressSpace() == PIC16ISD::RAM_SPACE &&
Sanjiv Gupta753ec152009-10-15 19:26:25 +0000161 "can allocate initialized RAM data only");
Chris Lattnerf0144122009-07-28 03:13:23 +0000162
163 // Find how much space this global needs.
Chris Lattnera87dea42009-07-31 18:48:30 +0000164 const TargetData *TD = TM->getTargetData();
Chris Lattnerf0144122009-07-28 03:13:23 +0000165 const Type *Ty = C->getType();
166 unsigned ValSize = TD->getTypeAllocSize(Ty);
167
168 // Go through all IDATA Sections and assign this variable
169 // to the first available section having enough space.
Sanjiv Gupta753ec152009-10-15 19:26:25 +0000170 PIC16Section *Found = NULL;
171 for (unsigned i = 0; i < IDATASections_.size(); i++) {
172 if (DataBankSize - IDATASections_[i]->getSize() >= ValSize) {
173 Found = IDATASections_[i];
Chris Lattnerf0144122009-07-28 03:13:23 +0000174 break;
175 }
176 }
177
178 // No IDATA section spacious enough was found. Crate a new one.
Sanjiv Gupta753ec152009-10-15 19:26:25 +0000179 if (!Found) {
180 std::string name = PAN::getIdataSectionName(IDATASections_.size());
181 Found = getPIC16DataSection(name.c_str(), IDATA);
Chris Lattnerf0144122009-07-28 03:13:23 +0000182 }
183
184 // Insert the GV into this IDATA.
Sanjiv Gupta753ec152009-10-15 19:26:25 +0000185 Found->Items.push_back(GV);
186 Found->setSize(Found->getSize() + ValSize);
187 return Found;
Chris Lattnerf0144122009-07-28 03:13:23 +0000188}
189
Sanjiv Gupta753ec152009-10-15 19:26:25 +0000190// Allocate a program memory variable into ROMDATA section.
191const MCSection *
192PIC16TargetObjectFile::allocateROMDATA(const GlobalVariable *GV) const {
193
194 std::string name = PAN::getRomdataSectionName();
195 PIC16Section *S = getPIC16DataSection(name.c_str(), ROMDATA);
196
197 S->Items.push_back(GV);
198 return S;
199}
200
Chris Lattnerf0144122009-07-28 03:13:23 +0000201// Get the section for an automatic variable of a function.
202// For PIC16 they are globals only with mangled names.
Chris Lattnera87dea42009-07-31 18:48:30 +0000203const MCSection *
Sanjiv Gupta753ec152009-10-15 19:26:25 +0000204PIC16TargetObjectFile::allocateAUTO(const GlobalVariable *GV) const {
Chris Lattnerf0144122009-07-28 03:13:23 +0000205
206 const std::string name = PAN::getSectionNameForSym(GV->getName());
Sanjiv Gupta753ec152009-10-15 19:26:25 +0000207 PIC16Section *S = getPIC16AutoSection(name.c_str());
Chris Lattnerf0144122009-07-28 03:13:23 +0000208
Sanjiv Gupta753ec152009-10-15 19:26:25 +0000209 S->Items.push_back(GV);
210 return S;
Chris Lattnerf0144122009-07-28 03:13:23 +0000211}
212
213
214// Override default implementation to put the true globals into
215// multiple data sections if required.
Chris Lattnera87dea42009-07-31 18:48:30 +0000216const MCSection *
Chris Lattnerf0144122009-07-28 03:13:23 +0000217PIC16TargetObjectFile::SelectSectionForGlobal(const GlobalValue *GV1,
Chris Lattnerf9650c02009-08-01 21:46:23 +0000218 SectionKind Kind,
Chris Lattnere53a6002009-07-29 05:09:30 +0000219 Mangler *Mang,
Chris Lattnerf0144122009-07-28 03:13:23 +0000220 const TargetMachine &TM) const {
221 // We select the section based on the initializer here, so it really
222 // has to be a GlobalVariable.
223 const GlobalVariable *GV = dyn_cast<GlobalVariable>(GV1);
224 if (!GV)
Chris Lattnerf9650c02009-08-01 21:46:23 +0000225 return TargetLoweringObjectFile::SelectSectionForGlobal(GV1, Kind, Mang,TM);
Chris Lattnerf0144122009-07-28 03:13:23 +0000226
Chris Lattnerf0144122009-07-28 03:13:23 +0000227 assert(GV->hasInitializer() && "A def without initializer?");
228
229 // First, if this is an automatic variable for a function, get the section
230 // name for it and return.
231 std::string name = GV->getName();
232 if (PAN::isLocalName(name))
Sanjiv Gupta753ec152009-10-15 19:26:25 +0000233 return allocateAUTO(GV);
Chris Lattnerf0144122009-07-28 03:13:23 +0000234
235 // See if this is an uninitialized global.
236 const Constant *C = GV->getInitializer();
237 if (C->isNullValue())
Sanjiv Gupta753ec152009-10-15 19:26:25 +0000238 return allocateUDATA(GV);
Chris Lattnerf0144122009-07-28 03:13:23 +0000239
240 // If this is initialized data in RAM. Put it in the correct IDATA section.
241 if (GV->getType()->getAddressSpace() == PIC16ISD::RAM_SPACE)
Sanjiv Gupta753ec152009-10-15 19:26:25 +0000242 return allocateIDATA(GV);
Chris Lattnerf0144122009-07-28 03:13:23 +0000243
244 // This is initialized data in rom, put it in the readonly section.
245 if (GV->getType()->getAddressSpace() == PIC16ISD::ROM_SPACE)
Sanjiv Gupta753ec152009-10-15 19:26:25 +0000246 return allocateROMDATA(GV);
Chris Lattnerf0144122009-07-28 03:13:23 +0000247
248 // Else let the default implementation take care of it.
Chris Lattnerf9650c02009-08-01 21:46:23 +0000249 return TargetLoweringObjectFile::SelectSectionForGlobal(GV, Kind, Mang,TM);
Chris Lattnerf0144122009-07-28 03:13:23 +0000250}
251
Sanjiv Gupta753ec152009-10-15 19:26:25 +0000252
Chris Lattnerf0144122009-07-28 03:13:23 +0000253
254
Sanjiv Gupta753ec152009-10-15 19:26:25 +0000255/// getExplicitSectionGlobal - Allow the target to completely override
Chris Lattnerf0144122009-07-28 03:13:23 +0000256/// section assignment of a global.
Chris Lattner24f654c2009-08-06 16:39:58 +0000257const MCSection *PIC16TargetObjectFile::
258getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
259 Mangler *Mang, const TargetMachine &TM) const {
260 assert(GV->hasSection());
261
262 if (const GlobalVariable *GVar = cast<GlobalVariable>(GV)) {
263 std::string SectName = GVar->getSection();
264 // If address for a variable is specified, get the address and create
265 // section.
Sanjiv Gupta753ec152009-10-15 19:26:25 +0000266 // FIXME: move this attribute checking in PAN.
Chris Lattner24f654c2009-08-06 16:39:58 +0000267 std::string AddrStr = "Address=";
268 if (SectName.compare(0, AddrStr.length(), AddrStr) == 0) {
269 std::string SectAddr = SectName.substr(AddrStr.length());
Sanjiv Gupta753ec152009-10-15 19:26:25 +0000270 return allocateAtGivenAddress(GVar, SectAddr);
Chris Lattnerf0144122009-07-28 03:13:23 +0000271 }
Chris Lattner24f654c2009-08-06 16:39:58 +0000272
273 // Create the section specified with section attribute.
Sanjiv Gupta753ec152009-10-15 19:26:25 +0000274 return allocateInGivenSection(GVar);
Chris Lattnerf0144122009-07-28 03:13:23 +0000275 }
276
Sanjiv Gupta753ec152009-10-15 19:26:25 +0000277 return getPIC16DataSection(GV->getSection().c_str(), UDATA);
Chris Lattnerf0144122009-07-28 03:13:23 +0000278}
279
Sanjiv Gupta753ec152009-10-15 19:26:25 +0000280// Interface used by AsmPrinter to get a code section for a function.
281const PIC16Section *
282PIC16TargetObjectFile::SectionForCode(const std::string &FnName) const {
283 const std::string &sec_name = PAN::getCodeSectionName(FnName);
284 return getPIC16Section(sec_name, CODE);
285}
286
287// Interface used by AsmPrinter to get a frame section for a function.
288const PIC16Section *
289PIC16TargetObjectFile::SectionForFrame(const std::string &FnName) const {
290 const std::string &sec_name = PAN::getFrameSectionName(FnName);
291 return getPIC16Section(sec_name, UDATA_OVR);
292}
293
294// Allocate a global var in existing or new section of given name.
Chris Lattnera87dea42009-07-31 18:48:30 +0000295const MCSection *
Sanjiv Gupta753ec152009-10-15 19:26:25 +0000296PIC16TargetObjectFile::allocateInGivenSection(const GlobalVariable *GV) const {
297 // Determine the type of section that we need to create.
298 PIC16SectionType SecTy;
299
Chris Lattnerf0144122009-07-28 03:13:23 +0000300 // See if this is an uninitialized global.
301 const Constant *C = GV->getInitializer();
302 if (C->isNullValue())
Sanjiv Gupta753ec152009-10-15 19:26:25 +0000303 SecTy = UDATA;
Daniel Dunbar1ead1502009-10-15 15:02:14 +0000304 // If this is initialized data in RAM. Put it in the correct IDATA section.
Sanjiv Gupta753ec152009-10-15 19:26:25 +0000305 else if (GV->getType()->getAddressSpace() == PIC16ISD::RAM_SPACE)
306 SecTy = IDATA;
Daniel Dunbar1ead1502009-10-15 15:02:14 +0000307 // This is initialized data in rom, put it in the readonly section.
Sanjiv Gupta753ec152009-10-15 19:26:25 +0000308 else if (GV->getType()->getAddressSpace() == PIC16ISD::ROM_SPACE)
309 SecTy = ROMDATA;
310 else
311 llvm_unreachable ("Could not determine section type for global");
Daniel Dunbar1ead1502009-10-15 15:02:14 +0000312
Sanjiv Gupta753ec152009-10-15 19:26:25 +0000313 PIC16Section *S = getPIC16UserSection(GV->getSection().c_str(), SecTy);
314 S->Items.push_back(GV);
315 return S;
Chris Lattnerf0144122009-07-28 03:13:23 +0000316}
317
Sanjiv Gupta753ec152009-10-15 19:26:25 +0000318// Allocate a global var in a new absolute sections at given address.
Chris Lattnera87dea42009-07-31 18:48:30 +0000319const MCSection *
Sanjiv Gupta753ec152009-10-15 19:26:25 +0000320PIC16TargetObjectFile::allocateAtGivenAddress(const GlobalVariable *GV,
321 const std::string &Addr) const {
322 // Determine the type of section that we need to create.
323 PIC16SectionType SecTy;
Chris Lattnerf0144122009-07-28 03:13:23 +0000324
Sanjiv Gupta753ec152009-10-15 19:26:25 +0000325 // See if this is an uninitialized global.
326 const Constant *C = GV->getInitializer();
327 if (C->isNullValue())
328 SecTy = UDATA;
329 // If this is initialized data in RAM. Put it in the correct IDATA section.
330 else if (GV->getType()->getAddressSpace() == PIC16ISD::RAM_SPACE)
331 SecTy = IDATA;
332 // This is initialized data in rom, put it in the readonly section.
333 else if (GV->getType()->getAddressSpace() == PIC16ISD::ROM_SPACE)
334 SecTy = ROMDATA;
335 else
336 llvm_unreachable ("Could not determine section type for global");
Chris Lattnerf0144122009-07-28 03:13:23 +0000337
Sanjiv Gupta753ec152009-10-15 19:26:25 +0000338 std::string Prefix = GV->getNameStr() + "." + Addr + ".";
339 std::string SName = PAN::getUserSectionName(Prefix);
340 PIC16Section *S = getPIC16UserSection(SName.c_str(), SecTy, Addr.c_str());
341 S->Items.push_back(GV);
342 return S;
Chris Lattnerf0144122009-07-28 03:13:23 +0000343}
344
Chris Lattnerf0144122009-07-28 03:13:23 +0000345