1 //===-- PIC16TargetObjectFile.cpp - PIC16 object files --------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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"
22 PIC16TargetObjectFile::PIC16TargetObjectFile() {
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 {
30 /// Return if we have an already existing one.
31 PIC16Section *&Entry = SectionsByName[Name];
36 Entry = PIC16Section::Create(Name, Ty, Address, Color, getContext());
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 {
46 /// Return if we have an already existing one.
47 PIC16Section *&Entry = SectionsByName[Name];
52 /// Else create a new one and add it to appropriate section list.
53 Entry = PIC16Section::Create(Name, Ty, Address, Color, getContext());
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;
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 {
72 /// Return if we have an already existing one.
73 PIC16Section *&Entry = SectionsByName[Name];
78 /// Else create a new one and add it to appropriate section list.
79 Entry = PIC16Section::Create(Name, Ty, Address, Color, getContext());
81 assert (Ty == UDATA_OVR && "incorrect section type for autos");
82 AUTOSections_.push_back(Entry);
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 {
93 /// Return if we have an already existing one.
94 PIC16Section *&Entry = SectionsByName[Name];
99 /// Else create a new one and add it to appropriate section list.
100 Entry = PIC16Section::Create(Name, Ty, Address, Color, getContext());
102 USERSections_.push_back(Entry);
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);
112 // BSSSection = getPIC16DataSection("udata.#", UDATA);
113 // ReadOnlySection = getPIC16DataSection("romdata.#", ROMDATA);
114 // DataSection = getPIC16DataSection("idata.#", IDATA);
116 // Need because otherwise a .text symbol is emitted by DwarfWriter
117 // in BeginModule, and gpasm cribbs for that .text symbol.
119 // TextSection = getPIC16DataSection("", UDATA);
120 ROMDATASection_ = NULL;
123 /// allocateUDATA - Allocate a un-initialized global to an existing or new UDATA
124 /// section and return that section.
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");
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);
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];
146 // No UDATA section spacious enough was found. Crate a new one.
148 std::string name = PAN::getUdataSectionName(UDATASections_.size());
149 Found = getPIC16DataSection(name.c_str(), UDATA);
152 // Insert the GV into this UDATA section.
153 Found->Items.push_back(GV);
154 Found->setSize(Found->getSize() + ValSize);
158 /// allocateIDATA - allocate an initialized global into an existing
159 /// or new section and return that section.
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");
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);
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];
183 // No IDATA section spacious enough was found. Crate a new one.
185 std::string name = PAN::getIdataSectionName(IDATASections_.size());
186 Found = getPIC16DataSection(name.c_str(), IDATA);
189 // Insert the GV into this IDATA.
190 Found->Items.push_back(GV);
191 Found->setSize(Found->getSize() + ValSize);
195 // Allocate a program memory variable into ROMDATA section.
197 PIC16TargetObjectFile::allocateROMDATA(const GlobalVariable *GV) const {
199 std::string name = PAN::getRomdataSectionName();
200 PIC16Section *S = getPIC16DataSection(name.c_str(), ROMDATA);
202 S->Items.push_back(GV);
206 // Get the section for an automatic variable of a function.
207 // For PIC16 they are globals only with mangled names.
209 PIC16TargetObjectFile::allocateAUTO(const GlobalVariable *GV) const {
211 const std::string name = PAN::getSectionNameForSym(GV->getName());
212 PIC16Section *S = getPIC16AutoSection(name.c_str());
214 S->Items.push_back(GV);
219 // Override default implementation to put the true globals into
220 // multiple data sections if required.
222 PIC16TargetObjectFile::SelectSectionForGlobal(const GlobalValue *GV1,
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);
230 return TargetLoweringObjectFile::SelectSectionForGlobal(GV1, Kind, Mang,TM);
232 assert(GV->hasInitializer() && "A def without initializer?");
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);
240 // See if this is an uninitialized global.
241 const Constant *C = GV->getInitializer();
242 if (C->isNullValue())
243 return allocateUDATA(GV);
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);
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);
253 // Else let the default implementation take care of it.
254 return TargetLoweringObjectFile::SelectSectionForGlobal(GV, Kind, Mang,TM);
257 PIC16TargetObjectFile::~PIC16TargetObjectFile() {
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];
264 delete ROMDATASection_;
266 for (unsigned i = 0; i < AUTOSections_.size(); i++)
267 delete AUTOSections_[i];
269 for (unsigned i = 0; i < USERSections_.size(); i++)
270 delete USERSections_[i];
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());
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
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);
293 // Create the section specified with section attribute.
294 return allocateInGivenSection(GVar);
297 return getPIC16DataSection(GV->getSection().c_str(), UDATA);
300 // Interface used by AsmPrinter to get a code section for a function.
302 PIC16TargetObjectFile::SectionForCode(const std::string &FnName) const {
303 const std::string &sec_name = PAN::getCodeSectionName(FnName);
304 return getPIC16Section(sec_name, CODE);
307 // Interface used by AsmPrinter to get a frame section for a function.
309 PIC16TargetObjectFile::SectionForFrame(const std::string &FnName) const {
310 const std::string &sec_name = PAN::getFrameSectionName(FnName);
311 return getPIC16Section(sec_name, UDATA_OVR);
314 // Allocate a global var in existing or new section of given name.
316 PIC16TargetObjectFile::allocateInGivenSection(const GlobalVariable *GV) const {
317 // Determine the type of section that we need to create.
318 PIC16SectionType SecTy;
320 // See if this is an uninitialized global.
321 const Constant *C = GV->getInitializer();
322 if (C->isNullValue())
324 // If this is initialized data in RAM. Put it in the correct IDATA section.
325 else if (GV->getType()->getAddressSpace() == PIC16ISD::RAM_SPACE)
327 // This is initialized data in rom, put it in the readonly section.
328 else if (GV->getType()->getAddressSpace() == PIC16ISD::ROM_SPACE)
331 llvm_unreachable ("Could not determine section type for global");
333 PIC16Section *S = getPIC16UserSection(GV->getSection().c_str(), SecTy);
334 S->Items.push_back(GV);
338 // Allocate a global var in a new absolute sections at given address.
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;
345 // See if this is an uninitialized global.
346 const Constant *C = GV->getInitializer();
347 if (C->isNullValue())
349 // If this is initialized data in RAM. Put it in the correct IDATA section.
350 else if (GV->getType()->getAddressSpace() == PIC16ISD::RAM_SPACE)
352 // This is initialized data in rom, put it in the readonly section.
353 else if (GV->getType()->getAddressSpace() == PIC16ISD::ROM_SPACE)
356 llvm_unreachable ("Could not determine section type for global");
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);