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 "PIC16Section.h"
12 #include "PIC16ISelLowering.h"
13 #include "PIC16TargetMachine.h"
14 #include "llvm/DerivedTypes.h"
15 #include "llvm/Module.h"
16 #include "llvm/MC/MCSection.h"
17 #include "llvm/MC/MCContext.h"
20 PIC16TargetObjectFile::PIC16TargetObjectFile()
21 : ExternalVarDecls(0), ExternalVarDefs(0) {
24 const MCSection *PIC16TargetObjectFile::
25 getPIC16Section(const char *Name, bool isDirective, SectionKind Kind) const {
26 if (MCSection *S = getContext().GetSection(Name))
28 return MCSectionPIC16::Create(Name, isDirective, Kind, getContext());
32 void PIC16TargetObjectFile::Initialize(MCContext &Ctx, const TargetMachine &tm){
33 TargetLoweringObjectFile::Initialize(Ctx, tm);
36 BSSSection = getPIC16Section("udata.# UDATA", false,
37 SectionKind::getBSS());
38 ReadOnlySection = getPIC16Section("romdata.# ROMDATA", false,
39 SectionKind::getReadOnly());
40 DataSection = getPIC16Section("idata.# IDATA", false,
41 SectionKind::getDataRel());
43 // Need because otherwise a .text symbol is emitted by DwarfWriter
44 // in BeginModule, and gpasm cribbs for that .text symbol.
45 TextSection = getPIC16Section("", true, SectionKind::getText());
47 ROSections.push_back(new PIC16Section(ReadOnlySection));
49 // FIXME: I don't know what the classification of these sections really is.
50 ExternalVarDecls = new PIC16Section(getPIC16Section("ExternalVarDecls",
52 SectionKind::getMetadata()));
53 ExternalVarDefs = new PIC16Section(getPIC16Section("ExternalVarDefs",
55 SectionKind::getMetadata()));
58 const MCSection *PIC16TargetObjectFile::
59 getSectionForFunction(const std::string &FnName) const {
60 std::string T = PAN::getCodeSectionName(FnName);
61 return getPIC16Section(T.c_str(), false, SectionKind::getText());
65 const MCSection *PIC16TargetObjectFile::
66 getSectionForFunctionFrame(const std::string &FnName) const {
67 std::string T = PAN::getFrameSectionName(FnName);
68 return getPIC16Section(T.c_str(), false, SectionKind::getDataRel());
72 PIC16TargetObjectFile::getBSSSectionForGlobal(const GlobalVariable *GV) const {
73 assert(GV->hasInitializer() && "This global doesn't need space");
74 Constant *C = GV->getInitializer();
75 assert(C->isNullValue() && "Unitialized globals has non-zero initializer");
77 // Find how much space this global needs.
78 const TargetData *TD = TM->getTargetData();
79 const Type *Ty = C->getType();
80 unsigned ValSize = TD->getTypeAllocSize(Ty);
82 // Go through all BSS Sections and assign this variable
83 // to the first available section having enough space.
84 PIC16Section *FoundBSS = NULL;
85 for (unsigned i = 0; i < BSSSections.size(); i++) {
86 if (DataBankSize - BSSSections[i]->Size >= ValSize) {
87 FoundBSS = BSSSections[i];
92 // No BSS section spacious enough was found. Crate a new one.
94 std::string name = PAN::getUdataSectionName(BSSSections.size());
95 const MCSection *NewSection = getPIC16Section(name.c_str(), false,
97 SectionKind::getMetadata());
99 FoundBSS = new PIC16Section(NewSection);
101 // Add this newly created BSS section to the list of BSSSections.
102 BSSSections.push_back(FoundBSS);
105 // Insert the GV into this BSS.
106 FoundBSS->Items.push_back(GV);
107 FoundBSS->Size += ValSize;
112 PIC16TargetObjectFile::getIDATASectionForGlobal(const GlobalVariable *GV) const{
113 assert(GV->hasInitializer() && "This global doesn't need space");
114 Constant *C = GV->getInitializer();
115 assert(!C->isNullValue() && "initialized globals has zero initializer");
116 assert(GV->getType()->getAddressSpace() == PIC16ISD::RAM_SPACE &&
117 "can split initialized RAM data only");
119 // Find how much space this global needs.
120 const TargetData *TD = TM->getTargetData();
121 const Type *Ty = C->getType();
122 unsigned ValSize = TD->getTypeAllocSize(Ty);
124 // Go through all IDATA Sections and assign this variable
125 // to the first available section having enough space.
126 PIC16Section *FoundIDATA = NULL;
127 for (unsigned i = 0; i < IDATASections.size(); i++) {
128 if (DataBankSize - IDATASections[i]->Size >= ValSize) {
129 FoundIDATA = IDATASections[i];
134 // No IDATA section spacious enough was found. Crate a new one.
136 std::string name = PAN::getIdataSectionName(IDATASections.size());
137 const MCSection *NewSection = getPIC16Section(name.c_str(), false,
139 SectionKind::getMetadata());
141 FoundIDATA = new PIC16Section(NewSection);
143 // Add this newly created IDATA section to the list of IDATASections.
144 IDATASections.push_back(FoundIDATA);
147 // Insert the GV into this IDATA.
148 FoundIDATA->Items.push_back(GV);
149 FoundIDATA->Size += ValSize;
150 return FoundIDATA->S_;
153 // Get the section for an automatic variable of a function.
154 // For PIC16 they are globals only with mangled names.
156 PIC16TargetObjectFile::getSectionForAuto(const GlobalVariable *GV) const {
158 const std::string name = PAN::getSectionNameForSym(GV->getName());
160 // Go through all Auto Sections and assign this variable
161 // to the appropriate section.
162 PIC16Section *FoundAutoSec = NULL;
163 for (unsigned i = 0; i < AutosSections.size(); i++) {
164 if (AutosSections[i]->S_->getName() == name) {
165 FoundAutoSec = AutosSections[i];
170 // No Auto section was found. Crate a new one.
172 const MCSection *NewSection = getPIC16Section(name.c_str(),
175 SectionKind::getMetadata());
177 FoundAutoSec = new PIC16Section(NewSection);
179 // Add this newly created autos section to the list of AutosSections.
180 AutosSections.push_back(FoundAutoSec);
183 // Insert the auto into this section.
184 FoundAutoSec->Items.push_back(GV);
186 return FoundAutoSec->S_;
190 // Override default implementation to put the true globals into
191 // multiple data sections if required.
193 PIC16TargetObjectFile::SelectSectionForGlobal(const GlobalValue *GV1,
196 const TargetMachine &TM) const {
197 // We select the section based on the initializer here, so it really
198 // has to be a GlobalVariable.
199 const GlobalVariable *GV = dyn_cast<GlobalVariable>(GV1);
201 return TargetLoweringObjectFile::SelectSectionForGlobal(GV1, Kind, Mang,TM);
203 // Record External Var Decls.
204 if (GV->isDeclaration()) {
205 ExternalVarDecls->Items.push_back(GV);
206 return ExternalVarDecls->S_;
209 assert(GV->hasInitializer() && "A def without initializer?");
211 // First, if this is an automatic variable for a function, get the section
212 // name for it and return.
213 std::string name = GV->getName();
214 if (PAN::isLocalName(name))
215 return getSectionForAuto(GV);
217 // Record Exteranl Var Defs.
218 if (GV->hasExternalLinkage() || GV->hasCommonLinkage())
219 ExternalVarDefs->Items.push_back(GV);
221 // See if this is an uninitialized global.
222 const Constant *C = GV->getInitializer();
223 if (C->isNullValue())
224 return getBSSSectionForGlobal(GV);
226 // If this is initialized data in RAM. Put it in the correct IDATA section.
227 if (GV->getType()->getAddressSpace() == PIC16ISD::RAM_SPACE)
228 return getIDATASectionForGlobal(GV);
230 // This is initialized data in rom, put it in the readonly section.
231 if (GV->getType()->getAddressSpace() == PIC16ISD::ROM_SPACE)
232 return getROSectionForGlobal(GV);
234 // Else let the default implementation take care of it.
235 return TargetLoweringObjectFile::SelectSectionForGlobal(GV, Kind, Mang,TM);
238 PIC16TargetObjectFile::~PIC16TargetObjectFile() {
239 for (unsigned i = 0; i < BSSSections.size(); i++)
240 delete BSSSections[i];
241 for (unsigned i = 0; i < IDATASections.size(); i++)
242 delete IDATASections[i];
243 for (unsigned i = 0; i < AutosSections.size(); i++)
244 delete AutosSections[i];
245 for (unsigned i = 0; i < ROSections.size(); i++)
246 delete ROSections[i];
247 delete ExternalVarDecls;
248 delete ExternalVarDefs;
252 /// getSpecialCasedSectionGlobals - Allow the target to completely override
253 /// section assignment of a global.
254 const MCSection *PIC16TargetObjectFile::
255 getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
256 Mangler *Mang, const TargetMachine &TM) const {
257 assert(GV->hasSection());
259 if (const GlobalVariable *GVar = cast<GlobalVariable>(GV)) {
260 std::string SectName = GVar->getSection();
261 // If address for a variable is specified, get the address and create
263 std::string AddrStr = "Address=";
264 if (SectName.compare(0, AddrStr.length(), AddrStr) == 0) {
265 std::string SectAddr = SectName.substr(AddrStr.length());
266 return CreateSectionForGlobal(GVar, Mang, SectAddr);
269 // Create the section specified with section attribute.
270 return CreateSectionForGlobal(GVar, Mang);
273 return getPIC16Section(GV->getSection().c_str(), false, Kind);
276 // Create a new section for global variable. If Addr is given then create
277 // section at that address else create by name.
279 PIC16TargetObjectFile::CreateSectionForGlobal(const GlobalVariable *GV,
281 const std::string &Addr) const {
282 // See if this is an uninitialized global.
283 const Constant *C = GV->getInitializer();
284 if (C->isNullValue())
285 return CreateBSSSectionForGlobal(GV, Addr);
287 // If this is initialized data in RAM. Put it in the correct IDATA section.
288 if (GV->getType()->getAddressSpace() == PIC16ISD::RAM_SPACE)
289 return CreateIDATASectionForGlobal(GV, Addr);
291 // This is initialized data in rom, put it in the readonly section.
292 if (GV->getType()->getAddressSpace() == PIC16ISD::ROM_SPACE)
293 return CreateROSectionForGlobal(GV, Addr);
295 // Else let the default implementation take care of it.
296 return TargetLoweringObjectFile::SectionForGlobal(GV, Mang, *TM);
299 // Create uninitialized section for a variable.
301 PIC16TargetObjectFile::CreateBSSSectionForGlobal(const GlobalVariable *GV,
302 std::string Addr) const {
303 assert(GV->hasInitializer() && "This global doesn't need space");
304 assert(GV->getInitializer()->isNullValue() &&
305 "Unitialized global has non-zero initializer");
307 // If address is given then create a section at that address else create a
308 // section by section name specified in GV.
309 PIC16Section *FoundBSS = NULL;
311 Name = GV->getSection() + " UDATA";
312 for (unsigned i = 0; i < BSSSections.size(); i++) {
313 if (BSSSections[i]->S_->getName() == Name) {
314 FoundBSS = BSSSections[i];
319 std::string Prefix = GV->getNameStr() + "." + Addr + ".";
320 Name = PAN::getUdataSectionName(BSSSections.size(), Prefix) + " " + Addr;
323 PIC16Section *NewBSS = FoundBSS;
324 if (NewBSS == NULL) {
325 const MCSection *NewSection = getPIC16Section(Name.c_str(), false,
326 SectionKind::getBSS());
327 NewBSS = new PIC16Section(NewSection);
328 BSSSections.push_back(NewBSS);
331 // Insert the GV into this BSS.
332 NewBSS->Items.push_back(GV);
334 // We do not want to put any GV without explicit section into this section
335 // so set its size to DatabankSize.
336 NewBSS->Size = DataBankSize;
340 // Get rom section for a variable. Currently there can be only one rom section
341 // unless a variable explicitly requests a section.
343 PIC16TargetObjectFile::getROSectionForGlobal(const GlobalVariable *GV) const {
344 ROSections[0]->Items.push_back(GV);
345 return ROSections[0]->S_;
348 // Create initialized data section for a variable.
350 PIC16TargetObjectFile::CreateIDATASectionForGlobal(const GlobalVariable *GV,
351 std::string Addr) const {
352 assert(GV->hasInitializer() && "This global doesn't need space");
353 assert(!GV->getInitializer()->isNullValue() &&
354 "initialized global has zero initializer");
355 assert(GV->getType()->getAddressSpace() == PIC16ISD::RAM_SPACE &&
356 "can be used for initialized RAM data only");
359 // If address is given then create a section at that address else create a
360 // section by section name specified in GV.
361 PIC16Section *FoundIDATASec = NULL;
363 Name = GV->getSection() + " IDATA";
364 for (unsigned i = 0; i < IDATASections.size(); i++) {
365 if (IDATASections[i]->S_->getName() == Name) {
366 FoundIDATASec = IDATASections[i];
371 std::string Prefix = GV->getNameStr() + "." + Addr + ".";
372 Name = PAN::getIdataSectionName(IDATASections.size(), Prefix) + " " + Addr;
375 PIC16Section *NewIDATASec = FoundIDATASec;
376 if (NewIDATASec == NULL) {
377 const MCSection *NewSection = getPIC16Section(Name.c_str(), false,
379 SectionKind::getMetadata());
380 NewIDATASec = new PIC16Section(NewSection);
381 IDATASections.push_back(NewIDATASec);
383 // Insert the GV into this IDATA Section.
384 NewIDATASec->Items.push_back(GV);
385 // We do not want to put any GV without explicit section into this section
386 // so set its size to DatabankSize.
387 NewIDATASec->Size = DataBankSize;
388 return NewIDATASec->S_;
391 // Create a section in rom for a variable.
393 PIC16TargetObjectFile::CreateROSectionForGlobal(const GlobalVariable *GV,
394 std::string Addr) const {
395 assert(GV->getType()->getAddressSpace() == PIC16ISD::ROM_SPACE &&
396 "can be used for ROM data only");
399 // If address is given then create a section at that address else create a
400 // section by section name specified in GV.
401 PIC16Section *FoundROSec = NULL;
403 Name = GV->getSection() + " ROMDATA";
404 for (unsigned i = 1; i < ROSections.size(); i++) {
405 if (ROSections[i]->S_->getName() == Name) {
406 FoundROSec = ROSections[i];
411 std::string Prefix = GV->getNameStr() + "." + Addr + ".";
412 Name = PAN::getRomdataSectionName(ROSections.size(), Prefix) + " " + Addr;
415 PIC16Section *NewRomSec = FoundROSec;
416 if (NewRomSec == NULL) {
417 const MCSection *NewSection = getPIC16Section(Name.c_str(), false,
418 SectionKind::getReadOnly());
419 NewRomSec = new PIC16Section(NewSection);
420 ROSections.push_back(NewRomSec);
423 // Insert the GV into this ROM Section.
424 NewRomSec->Items.push_back(GV);
425 return NewRomSec->S_;