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 "llvm/DerivedTypes.h"
14 #include "llvm/Module.h"
15 #include "llvm/MC/MCSection.h"
18 void PIC16TargetObjectFile::Initialize(MCContext &Ctx, const TargetMachine &tm){
19 TargetLoweringObjectFile::Initialize(Ctx, tm);
22 BSSSection_ = getOrCreateSection("udata.# UDATA", false, SectionKind::BSS);
23 ReadOnlySection = getOrCreateSection("romdata.# ROMDATA", false,
24 SectionKind::ReadOnly);
25 DataSection = getOrCreateSection("idata.# IDATA", false,SectionKind::DataRel);
27 // Need because otherwise a .text symbol is emitted by DwarfWriter
28 // in BeginModule, and gpasm cribbs for that .text symbol.
29 TextSection = getOrCreateSection("", true, SectionKind::Text);
31 ROSections.push_back(new PIC16Section(ReadOnlySection));
33 // FIXME: I don't know what the classification of these sections really is.
34 ExternalVarDecls = new PIC16Section(getOrCreateSection("ExternalVarDecls",
36 SectionKind::Metadata));
37 ExternalVarDefs = new PIC16Section(getOrCreateSection("ExternalVarDefs",
39 SectionKind::Metadata));
44 PIC16TargetObjectFile::getBSSSectionForGlobal(const GlobalVariable *GV) const {
45 assert(GV->hasInitializer() && "This global doesn't need space");
46 Constant *C = GV->getInitializer();
47 assert(C->isNullValue() && "Unitialized globals has non-zero initializer");
49 // Find how much space this global needs.
50 const TargetData *TD = TM->getTargetData();
51 const Type *Ty = C->getType();
52 unsigned ValSize = TD->getTypeAllocSize(Ty);
54 // Go through all BSS Sections and assign this variable
55 // to the first available section having enough space.
56 PIC16Section *FoundBSS = NULL;
57 for (unsigned i = 0; i < BSSSections.size(); i++) {
58 if (DataBankSize - BSSSections[i]->Size >= ValSize) {
59 FoundBSS = BSSSections[i];
64 // No BSS section spacious enough was found. Crate a new one.
66 std::string name = PAN::getUdataSectionName(BSSSections.size());
67 const MCSection *NewSection = getOrCreateSection(name.c_str(), false,
69 SectionKind::Metadata);
71 FoundBSS = new PIC16Section(NewSection);
73 // Add this newly created BSS section to the list of BSSSections.
74 BSSSections.push_back(FoundBSS);
77 // Insert the GV into this BSS.
78 FoundBSS->Items.push_back(GV);
79 FoundBSS->Size += ValSize;
84 PIC16TargetObjectFile::getIDATASectionForGlobal(const GlobalVariable *GV) const{
85 assert(GV->hasInitializer() && "This global doesn't need space");
86 Constant *C = GV->getInitializer();
87 assert(!C->isNullValue() && "initialized globals has zero initializer");
88 assert(GV->getType()->getAddressSpace() == PIC16ISD::RAM_SPACE &&
89 "can split initialized RAM data only");
91 // Find how much space this global needs.
92 const TargetData *TD = TM->getTargetData();
93 const Type *Ty = C->getType();
94 unsigned ValSize = TD->getTypeAllocSize(Ty);
96 // Go through all IDATA Sections and assign this variable
97 // to the first available section having enough space.
98 PIC16Section *FoundIDATA = NULL;
99 for (unsigned i = 0; i < IDATASections.size(); i++) {
100 if (DataBankSize - IDATASections[i]->Size >= ValSize) {
101 FoundIDATA = IDATASections[i];
106 // No IDATA section spacious enough was found. Crate a new one.
108 std::string name = PAN::getIdataSectionName(IDATASections.size());
109 const MCSection *NewSection = getOrCreateSection(name.c_str(), false,
111 SectionKind::Metadata);
113 FoundIDATA = new PIC16Section(NewSection);
115 // Add this newly created IDATA section to the list of IDATASections.
116 IDATASections.push_back(FoundIDATA);
119 // Insert the GV into this IDATA.
120 FoundIDATA->Items.push_back(GV);
121 FoundIDATA->Size += ValSize;
122 return FoundIDATA->S_;
125 // Get the section for an automatic variable of a function.
126 // For PIC16 they are globals only with mangled names.
128 PIC16TargetObjectFile::getSectionForAuto(const GlobalVariable *GV) const {
130 const std::string name = PAN::getSectionNameForSym(GV->getName());
132 // Go through all Auto Sections and assign this variable
133 // to the appropriate section.
134 PIC16Section *FoundAutoSec = NULL;
135 for (unsigned i = 0; i < AutosSections.size(); i++) {
136 if (AutosSections[i]->S_->getName() == name) {
137 FoundAutoSec = AutosSections[i];
142 // No Auto section was found. Crate a new one.
144 const MCSection *NewSection = getOrCreateSection(name.c_str(),
147 SectionKind::Metadata);
149 FoundAutoSec = new PIC16Section(NewSection);
151 // Add this newly created autos section to the list of AutosSections.
152 AutosSections.push_back(FoundAutoSec);
155 // Insert the auto into this section.
156 FoundAutoSec->Items.push_back(GV);
158 return FoundAutoSec->S_;
162 // Override default implementation to put the true globals into
163 // multiple data sections if required.
165 PIC16TargetObjectFile::SelectSectionForGlobal(const GlobalValue *GV1,
168 const TargetMachine &TM) const {
169 // We select the section based on the initializer here, so it really
170 // has to be a GlobalVariable.
171 const GlobalVariable *GV = dyn_cast<GlobalVariable>(GV1);
173 return TargetLoweringObjectFile::SelectSectionForGlobal(GV1, Kind, Mang,TM);
175 // Record External Var Decls.
176 if (GV->isDeclaration()) {
177 ExternalVarDecls->Items.push_back(GV);
178 return ExternalVarDecls->S_;
181 assert(GV->hasInitializer() && "A def without initializer?");
183 // First, if this is an automatic variable for a function, get the section
184 // name for it and return.
185 std::string name = GV->getName();
186 if (PAN::isLocalName(name))
187 return getSectionForAuto(GV);
189 // Record Exteranl Var Defs.
190 if (GV->hasExternalLinkage() || GV->hasCommonLinkage())
191 ExternalVarDefs->Items.push_back(GV);
193 // See if this is an uninitialized global.
194 const Constant *C = GV->getInitializer();
195 if (C->isNullValue())
196 return getBSSSectionForGlobal(GV);
198 // If this is initialized data in RAM. Put it in the correct IDATA section.
199 if (GV->getType()->getAddressSpace() == PIC16ISD::RAM_SPACE)
200 return getIDATASectionForGlobal(GV);
202 // This is initialized data in rom, put it in the readonly section.
203 if (GV->getType()->getAddressSpace() == PIC16ISD::ROM_SPACE)
204 return getROSectionForGlobal(GV);
206 // Else let the default implementation take care of it.
207 return TargetLoweringObjectFile::SelectSectionForGlobal(GV, Kind, Mang,TM);
210 PIC16TargetObjectFile::~PIC16TargetObjectFile() {
211 for (unsigned i = 0; i < BSSSections.size(); i++)
212 delete BSSSections[i];
213 for (unsigned i = 0; i < IDATASections.size(); i++)
214 delete IDATASections[i];
215 for (unsigned i = 0; i < AutosSections.size(); i++)
216 delete AutosSections[i];
217 for (unsigned i = 0; i < ROSections.size(); i++)
218 delete ROSections[i];
219 delete ExternalVarDecls;
220 delete ExternalVarDefs;
224 /// getSpecialCasedSectionGlobals - Allow the target to completely override
225 /// section assignment of a global.
227 PIC16TargetObjectFile::getSpecialCasedSectionGlobals(const GlobalValue *GV,
229 SectionKind Kind) const {
230 // If GV has a sectin name or section address create that section now.
231 if (GV->hasSection()) {
232 if (const GlobalVariable *GVar = cast<GlobalVariable>(GV)) {
233 std::string SectName = GVar->getSection();
234 // If address for a variable is specified, get the address and create
236 std::string AddrStr = "Address=";
237 if (SectName.compare(0, AddrStr.length(), AddrStr) == 0) {
238 std::string SectAddr = SectName.substr(AddrStr.length());
239 return CreateSectionForGlobal(GVar, Mang, SectAddr);
242 // Create the section specified with section attribute.
243 return CreateSectionForGlobal(GVar, Mang);
250 // Create a new section for global variable. If Addr is given then create
251 // section at that address else create by name.
253 PIC16TargetObjectFile::CreateSectionForGlobal(const GlobalVariable *GV,
255 const std::string &Addr) const {
256 // See if this is an uninitialized global.
257 const Constant *C = GV->getInitializer();
258 if (C->isNullValue())
259 return CreateBSSSectionForGlobal(GV, Addr);
261 // If this is initialized data in RAM. Put it in the correct IDATA section.
262 if (GV->getType()->getAddressSpace() == PIC16ISD::RAM_SPACE)
263 return CreateIDATASectionForGlobal(GV, Addr);
265 // This is initialized data in rom, put it in the readonly section.
266 if (GV->getType()->getAddressSpace() == PIC16ISD::ROM_SPACE)
267 return CreateROSectionForGlobal(GV, Addr);
269 // Else let the default implementation take care of it.
270 return TargetLoweringObjectFile::SectionForGlobal(GV, Mang, *TM);
273 // Create uninitialized section for a variable.
275 PIC16TargetObjectFile::CreateBSSSectionForGlobal(const GlobalVariable *GV,
276 std::string Addr) const {
277 assert(GV->hasInitializer() && "This global doesn't need space");
278 assert(GV->getInitializer()->isNullValue() &&
279 "Unitialized global has non-zero initializer");
281 // If address is given then create a section at that address else create a
282 // section by section name specified in GV.
283 PIC16Section *FoundBSS = NULL;
285 Name = GV->getSection() + " UDATA";
286 for (unsigned i = 0; i < BSSSections.size(); i++) {
287 if (BSSSections[i]->S_->getName() == Name) {
288 FoundBSS = BSSSections[i];
293 std::string Prefix = GV->getNameStr() + "." + Addr + ".";
294 Name = PAN::getUdataSectionName(BSSSections.size(), Prefix) + " " + Addr;
297 PIC16Section *NewBSS = FoundBSS;
298 if (NewBSS == NULL) {
299 const MCSection *NewSection = getOrCreateSection(Name.c_str(), false,
301 NewBSS = new PIC16Section(NewSection);
302 BSSSections.push_back(NewBSS);
305 // Insert the GV into this BSS.
306 NewBSS->Items.push_back(GV);
308 // We do not want to put any GV without explicit section into this section
309 // so set its size to DatabankSize.
310 NewBSS->Size = DataBankSize;
314 // Get rom section for a variable. Currently there can be only one rom section
315 // unless a variable explicitly requests a section.
317 PIC16TargetObjectFile::getROSectionForGlobal(const GlobalVariable *GV) const {
318 ROSections[0]->Items.push_back(GV);
319 return ROSections[0]->S_;
322 // Create initialized data section for a variable.
324 PIC16TargetObjectFile::CreateIDATASectionForGlobal(const GlobalVariable *GV,
325 std::string Addr) const {
326 assert(GV->hasInitializer() && "This global doesn't need space");
327 assert(!GV->getInitializer()->isNullValue() &&
328 "initialized global has zero initializer");
329 assert(GV->getType()->getAddressSpace() == PIC16ISD::RAM_SPACE &&
330 "can be used for initialized RAM data only");
333 // If address is given then create a section at that address else create a
334 // section by section name specified in GV.
335 PIC16Section *FoundIDATASec = NULL;
337 Name = GV->getSection() + " IDATA";
338 for (unsigned i = 0; i < IDATASections.size(); i++) {
339 if (IDATASections[i]->S_->getName() == Name) {
340 FoundIDATASec = IDATASections[i];
345 std::string Prefix = GV->getNameStr() + "." + Addr + ".";
346 Name = PAN::getIdataSectionName(IDATASections.size(), Prefix) + " " + Addr;
349 PIC16Section *NewIDATASec = FoundIDATASec;
350 if (NewIDATASec == NULL) {
351 const MCSection *NewSection = getOrCreateSection(Name.c_str(), false,
353 SectionKind::Metadata);
354 NewIDATASec = new PIC16Section(NewSection);
355 IDATASections.push_back(NewIDATASec);
357 // Insert the GV into this IDATA Section.
358 NewIDATASec->Items.push_back(GV);
359 // We do not want to put any GV without explicit section into this section
360 // so set its size to DatabankSize.
361 NewIDATASec->Size = DataBankSize;
362 return NewIDATASec->S_;
365 // Create a section in rom for a variable.
367 PIC16TargetObjectFile::CreateROSectionForGlobal(const GlobalVariable *GV,
368 std::string Addr) const {
369 assert(GV->getType()->getAddressSpace() == PIC16ISD::ROM_SPACE &&
370 "can be used for ROM data only");
373 // If address is given then create a section at that address else create a
374 // section by section name specified in GV.
375 PIC16Section *FoundROSec = NULL;
377 Name = GV->getSection() + " ROMDATA";
378 for (unsigned i = 1; i < ROSections.size(); i++) {
379 if (ROSections[i]->S_->getName() == Name) {
380 FoundROSec = ROSections[i];
385 std::string Prefix = GV->getNameStr() + "." + Addr + ".";
386 Name = PAN::getRomdataSectionName(ROSections.size(), Prefix) + " " + Addr;
389 PIC16Section *NewRomSec = FoundROSec;
390 if (NewRomSec == NULL) {
391 const MCSection *NewSection = getOrCreateSection(Name.c_str(), false,
392 SectionKind::ReadOnly);
393 NewRomSec = new PIC16Section(NewSection);
394 ROSections.push_back(NewRomSec);
397 // Insert the GV into this ROM Section.
398 NewRomSec->Items.push_back(GV);
399 return NewRomSec->S_;