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 PIC16TargetObjectFile::PIC16TargetObjectFile()
19 : ExternalVarDecls(0), ExternalVarDefs(0)
23 void PIC16TargetObjectFile::Initialize(MCContext &Ctx, const TargetMachine &tm){
24 TargetLoweringObjectFile::Initialize(Ctx, tm);
27 BSSSection = getOrCreateSection("udata.# UDATA", false,
28 SectionKind::getBSS());
29 ReadOnlySection = getOrCreateSection("romdata.# ROMDATA", false,
30 SectionKind::getReadOnly());
31 DataSection = getOrCreateSection("idata.# IDATA", false,
32 SectionKind::getDataRel());
34 // Need because otherwise a .text symbol is emitted by DwarfWriter
35 // in BeginModule, and gpasm cribbs for that .text symbol.
36 TextSection = getOrCreateSection("", true,
37 SectionKind::getText());
39 ROSections.push_back(new PIC16Section(ReadOnlySection));
41 // FIXME: I don't know what the classification of these sections really is.
42 ExternalVarDecls = new PIC16Section(getOrCreateSection("ExternalVarDecls",
44 SectionKind::getMetadata()));
45 ExternalVarDefs = new PIC16Section(getOrCreateSection("ExternalVarDefs",
47 SectionKind::getMetadata()));
52 PIC16TargetObjectFile::getBSSSectionForGlobal(const GlobalVariable *GV) const {
53 assert(GV->hasInitializer() && "This global doesn't need space");
54 Constant *C = GV->getInitializer();
55 assert(C->isNullValue() && "Unitialized globals has non-zero initializer");
57 // Find how much space this global needs.
58 const TargetData *TD = TM->getTargetData();
59 const Type *Ty = C->getType();
60 unsigned ValSize = TD->getTypeAllocSize(Ty);
62 // Go through all BSS Sections and assign this variable
63 // to the first available section having enough space.
64 PIC16Section *FoundBSS = NULL;
65 for (unsigned i = 0; i < BSSSections.size(); i++) {
66 if (DataBankSize - BSSSections[i]->Size >= ValSize) {
67 FoundBSS = BSSSections[i];
72 // No BSS section spacious enough was found. Crate a new one.
74 std::string name = PAN::getUdataSectionName(BSSSections.size());
75 const MCSection *NewSection = getOrCreateSection(name.c_str(), false,
77 SectionKind::getMetadata());
79 FoundBSS = new PIC16Section(NewSection);
81 // Add this newly created BSS section to the list of BSSSections.
82 BSSSections.push_back(FoundBSS);
85 // Insert the GV into this BSS.
86 FoundBSS->Items.push_back(GV);
87 FoundBSS->Size += ValSize;
92 PIC16TargetObjectFile::getIDATASectionForGlobal(const GlobalVariable *GV) const{
93 assert(GV->hasInitializer() && "This global doesn't need space");
94 Constant *C = GV->getInitializer();
95 assert(!C->isNullValue() && "initialized globals has zero initializer");
96 assert(GV->getType()->getAddressSpace() == PIC16ISD::RAM_SPACE &&
97 "can split initialized RAM data only");
99 // Find how much space this global needs.
100 const TargetData *TD = TM->getTargetData();
101 const Type *Ty = C->getType();
102 unsigned ValSize = TD->getTypeAllocSize(Ty);
104 // Go through all IDATA Sections and assign this variable
105 // to the first available section having enough space.
106 PIC16Section *FoundIDATA = NULL;
107 for (unsigned i = 0; i < IDATASections.size(); i++) {
108 if (DataBankSize - IDATASections[i]->Size >= ValSize) {
109 FoundIDATA = IDATASections[i];
114 // No IDATA section spacious enough was found. Crate a new one.
116 std::string name = PAN::getIdataSectionName(IDATASections.size());
117 const MCSection *NewSection = getOrCreateSection(name.c_str(), false,
119 SectionKind::getMetadata());
121 FoundIDATA = new PIC16Section(NewSection);
123 // Add this newly created IDATA section to the list of IDATASections.
124 IDATASections.push_back(FoundIDATA);
127 // Insert the GV into this IDATA.
128 FoundIDATA->Items.push_back(GV);
129 FoundIDATA->Size += ValSize;
130 return FoundIDATA->S_;
133 // Get the section for an automatic variable of a function.
134 // For PIC16 they are globals only with mangled names.
136 PIC16TargetObjectFile::getSectionForAuto(const GlobalVariable *GV) const {
138 const std::string name = PAN::getSectionNameForSym(GV->getName());
140 // Go through all Auto Sections and assign this variable
141 // to the appropriate section.
142 PIC16Section *FoundAutoSec = NULL;
143 for (unsigned i = 0; i < AutosSections.size(); i++) {
144 if (AutosSections[i]->S_->getName() == name) {
145 FoundAutoSec = AutosSections[i];
150 // No Auto section was found. Crate a new one.
152 const MCSection *NewSection = getOrCreateSection(name.c_str(),
155 SectionKind::getMetadata());
157 FoundAutoSec = new PIC16Section(NewSection);
159 // Add this newly created autos section to the list of AutosSections.
160 AutosSections.push_back(FoundAutoSec);
163 // Insert the auto into this section.
164 FoundAutoSec->Items.push_back(GV);
166 return FoundAutoSec->S_;
170 // Override default implementation to put the true globals into
171 // multiple data sections if required.
173 PIC16TargetObjectFile::SelectSectionForGlobal(const GlobalValue *GV1,
176 const TargetMachine &TM) const {
177 // We select the section based on the initializer here, so it really
178 // has to be a GlobalVariable.
179 const GlobalVariable *GV = dyn_cast<GlobalVariable>(GV1);
181 return TargetLoweringObjectFile::SelectSectionForGlobal(GV1, Kind, Mang,TM);
183 // Record External Var Decls.
184 if (GV->isDeclaration()) {
185 ExternalVarDecls->Items.push_back(GV);
186 return ExternalVarDecls->S_;
189 assert(GV->hasInitializer() && "A def without initializer?");
191 // First, if this is an automatic variable for a function, get the section
192 // name for it and return.
193 std::string name = GV->getName();
194 if (PAN::isLocalName(name))
195 return getSectionForAuto(GV);
197 // Record Exteranl Var Defs.
198 if (GV->hasExternalLinkage() || GV->hasCommonLinkage())
199 ExternalVarDefs->Items.push_back(GV);
201 // See if this is an uninitialized global.
202 const Constant *C = GV->getInitializer();
203 if (C->isNullValue())
204 return getBSSSectionForGlobal(GV);
206 // If this is initialized data in RAM. Put it in the correct IDATA section.
207 if (GV->getType()->getAddressSpace() == PIC16ISD::RAM_SPACE)
208 return getIDATASectionForGlobal(GV);
210 // This is initialized data in rom, put it in the readonly section.
211 if (GV->getType()->getAddressSpace() == PIC16ISD::ROM_SPACE)
212 return getROSectionForGlobal(GV);
214 // Else let the default implementation take care of it.
215 return TargetLoweringObjectFile::SelectSectionForGlobal(GV, Kind, Mang,TM);
218 PIC16TargetObjectFile::~PIC16TargetObjectFile() {
219 for (unsigned i = 0; i < BSSSections.size(); i++)
220 delete BSSSections[i];
221 for (unsigned i = 0; i < IDATASections.size(); i++)
222 delete IDATASections[i];
223 for (unsigned i = 0; i < AutosSections.size(); i++)
224 delete AutosSections[i];
225 for (unsigned i = 0; i < ROSections.size(); i++)
226 delete ROSections[i];
227 delete ExternalVarDecls;
228 delete ExternalVarDefs;
232 /// getSpecialCasedSectionGlobals - Allow the target to completely override
233 /// section assignment of a global.
235 PIC16TargetObjectFile::getSpecialCasedSectionGlobals(const GlobalValue *GV,
237 SectionKind Kind) const {
238 // If GV has a sectin name or section address create that section now.
239 if (GV->hasSection()) {
240 if (const GlobalVariable *GVar = cast<GlobalVariable>(GV)) {
241 std::string SectName = GVar->getSection();
242 // If address for a variable is specified, get the address and create
244 std::string AddrStr = "Address=";
245 if (SectName.compare(0, AddrStr.length(), AddrStr) == 0) {
246 std::string SectAddr = SectName.substr(AddrStr.length());
247 return CreateSectionForGlobal(GVar, Mang, SectAddr);
250 // Create the section specified with section attribute.
251 return CreateSectionForGlobal(GVar, Mang);
258 // Create a new section for global variable. If Addr is given then create
259 // section at that address else create by name.
261 PIC16TargetObjectFile::CreateSectionForGlobal(const GlobalVariable *GV,
263 const std::string &Addr) const {
264 // See if this is an uninitialized global.
265 const Constant *C = GV->getInitializer();
266 if (C->isNullValue())
267 return CreateBSSSectionForGlobal(GV, Addr);
269 // If this is initialized data in RAM. Put it in the correct IDATA section.
270 if (GV->getType()->getAddressSpace() == PIC16ISD::RAM_SPACE)
271 return CreateIDATASectionForGlobal(GV, Addr);
273 // This is initialized data in rom, put it in the readonly section.
274 if (GV->getType()->getAddressSpace() == PIC16ISD::ROM_SPACE)
275 return CreateROSectionForGlobal(GV, Addr);
277 // Else let the default implementation take care of it.
278 return TargetLoweringObjectFile::SectionForGlobal(GV, Mang, *TM);
281 // Create uninitialized section for a variable.
283 PIC16TargetObjectFile::CreateBSSSectionForGlobal(const GlobalVariable *GV,
284 std::string Addr) const {
285 assert(GV->hasInitializer() && "This global doesn't need space");
286 assert(GV->getInitializer()->isNullValue() &&
287 "Unitialized global has non-zero initializer");
289 // If address is given then create a section at that address else create a
290 // section by section name specified in GV.
291 PIC16Section *FoundBSS = NULL;
293 Name = GV->getSection() + " UDATA";
294 for (unsigned i = 0; i < BSSSections.size(); i++) {
295 if (BSSSections[i]->S_->getName() == Name) {
296 FoundBSS = BSSSections[i];
301 std::string Prefix = GV->getNameStr() + "." + Addr + ".";
302 Name = PAN::getUdataSectionName(BSSSections.size(), Prefix) + " " + Addr;
305 PIC16Section *NewBSS = FoundBSS;
306 if (NewBSS == NULL) {
307 const MCSection *NewSection = getOrCreateSection(Name.c_str(), false,
308 SectionKind::getBSS());
309 NewBSS = new PIC16Section(NewSection);
310 BSSSections.push_back(NewBSS);
313 // Insert the GV into this BSS.
314 NewBSS->Items.push_back(GV);
316 // We do not want to put any GV without explicit section into this section
317 // so set its size to DatabankSize.
318 NewBSS->Size = DataBankSize;
322 // Get rom section for a variable. Currently there can be only one rom section
323 // unless a variable explicitly requests a section.
325 PIC16TargetObjectFile::getROSectionForGlobal(const GlobalVariable *GV) const {
326 ROSections[0]->Items.push_back(GV);
327 return ROSections[0]->S_;
330 // Create initialized data section for a variable.
332 PIC16TargetObjectFile::CreateIDATASectionForGlobal(const GlobalVariable *GV,
333 std::string Addr) const {
334 assert(GV->hasInitializer() && "This global doesn't need space");
335 assert(!GV->getInitializer()->isNullValue() &&
336 "initialized global has zero initializer");
337 assert(GV->getType()->getAddressSpace() == PIC16ISD::RAM_SPACE &&
338 "can be used for initialized RAM data only");
341 // If address is given then create a section at that address else create a
342 // section by section name specified in GV.
343 PIC16Section *FoundIDATASec = NULL;
345 Name = GV->getSection() + " IDATA";
346 for (unsigned i = 0; i < IDATASections.size(); i++) {
347 if (IDATASections[i]->S_->getName() == Name) {
348 FoundIDATASec = IDATASections[i];
353 std::string Prefix = GV->getNameStr() + "." + Addr + ".";
354 Name = PAN::getIdataSectionName(IDATASections.size(), Prefix) + " " + Addr;
357 PIC16Section *NewIDATASec = FoundIDATASec;
358 if (NewIDATASec == NULL) {
359 const MCSection *NewSection = getOrCreateSection(Name.c_str(), false,
361 SectionKind::getMetadata());
362 NewIDATASec = new PIC16Section(NewSection);
363 IDATASections.push_back(NewIDATASec);
365 // Insert the GV into this IDATA Section.
366 NewIDATASec->Items.push_back(GV);
367 // We do not want to put any GV without explicit section into this section
368 // so set its size to DatabankSize.
369 NewIDATASec->Size = DataBankSize;
370 return NewIDATASec->S_;
373 // Create a section in rom for a variable.
375 PIC16TargetObjectFile::CreateROSectionForGlobal(const GlobalVariable *GV,
376 std::string Addr) const {
377 assert(GV->getType()->getAddressSpace() == PIC16ISD::ROM_SPACE &&
378 "can be used for ROM data only");
381 // If address is given then create a section at that address else create a
382 // section by section name specified in GV.
383 PIC16Section *FoundROSec = NULL;
385 Name = GV->getSection() + " ROMDATA";
386 for (unsigned i = 1; i < ROSections.size(); i++) {
387 if (ROSections[i]->S_->getName() == Name) {
388 FoundROSec = ROSections[i];
393 std::string Prefix = GV->getNameStr() + "." + Addr + ".";
394 Name = PAN::getRomdataSectionName(ROSections.size(), Prefix) + " " + Addr;
397 PIC16Section *NewRomSec = FoundROSec;
398 if (NewRomSec == NULL) {
399 const MCSection *NewSection = getOrCreateSection(Name.c_str(), false,
400 SectionKind::getReadOnly());
401 NewRomSec = new PIC16Section(NewSection);
402 ROSections.push_back(NewRomSec);
405 // Insert the GV into this ROM Section.
406 NewRomSec->Items.push_back(GV);
407 return NewRomSec->S_;