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"
18 #include "llvm/Support/raw_ostream.h"
21 MCSectionPIC16::MCSectionPIC16(const StringRef &name, SectionKind K,
22 MCContext &Ctx) : MCSection(K), Name(name) {
23 Ctx.SetSection(Name, this);
26 MCSectionPIC16 *MCSectionPIC16::Create(const StringRef &Name,
27 SectionKind K, MCContext &Ctx) {
28 return new (Ctx) MCSectionPIC16(Name, K, Ctx);
32 void MCSectionPIC16::PrintSwitchToSection(const TargetAsmInfo &TAI,
33 raw_ostream &OS) const {
34 OS << getName() << '\n';
40 PIC16TargetObjectFile::PIC16TargetObjectFile()
41 : ExternalVarDecls(0), ExternalVarDefs(0) {
44 const MCSectionPIC16 *PIC16TargetObjectFile::
45 getPIC16Section(const char *Name, SectionKind Kind) const {
46 if (MCSection *S = getContext().GetSection(Name))
47 return (MCSectionPIC16*)S;
48 return MCSectionPIC16::Create(Name, Kind, getContext());
52 void PIC16TargetObjectFile::Initialize(MCContext &Ctx, const TargetMachine &tm){
53 TargetLoweringObjectFile::Initialize(Ctx, tm);
56 BSSSection = getPIC16Section("udata.# UDATA", SectionKind::getBSS());
57 ReadOnlySection = getPIC16Section("romdata.# ROMDATA",
58 SectionKind::getReadOnly());
59 DataSection = getPIC16Section("idata.# IDATA", SectionKind::getDataRel());
61 // Need because otherwise a .text symbol is emitted by DwarfWriter
62 // in BeginModule, and gpasm cribbs for that .text symbol.
63 TextSection = getPIC16Section("", SectionKind::getText());
65 ROSections.push_back(new PIC16Section((MCSectionPIC16*)ReadOnlySection));
67 // FIXME: I don't know what the classification of these sections really is.
68 ExternalVarDecls = new PIC16Section(getPIC16Section("ExternalVarDecls",
69 SectionKind::getMetadata()));
70 ExternalVarDefs = new PIC16Section(getPIC16Section("ExternalVarDefs",
71 SectionKind::getMetadata()));
74 const MCSection *PIC16TargetObjectFile::
75 getSectionForFunction(const std::string &FnName) const {
76 std::string T = PAN::getCodeSectionName(FnName);
77 return getPIC16Section(T.c_str(), SectionKind::getText());
81 const MCSection *PIC16TargetObjectFile::
82 getSectionForFunctionFrame(const std::string &FnName) const {
83 std::string T = PAN::getFrameSectionName(FnName);
84 return getPIC16Section(T.c_str(), SectionKind::getDataRel());
88 PIC16TargetObjectFile::getBSSSectionForGlobal(const GlobalVariable *GV) const {
89 assert(GV->hasInitializer() && "This global doesn't need space");
90 Constant *C = GV->getInitializer();
91 assert(C->isNullValue() && "Unitialized globals has non-zero initializer");
93 // Find how much space this global needs.
94 const TargetData *TD = TM->getTargetData();
95 const Type *Ty = C->getType();
96 unsigned ValSize = TD->getTypeAllocSize(Ty);
98 // Go through all BSS Sections and assign this variable
99 // to the first available section having enough space.
100 PIC16Section *FoundBSS = NULL;
101 for (unsigned i = 0; i < BSSSections.size(); i++) {
102 if (DataBankSize - BSSSections[i]->Size >= ValSize) {
103 FoundBSS = BSSSections[i];
108 // No BSS section spacious enough was found. Crate a new one.
110 std::string name = PAN::getUdataSectionName(BSSSections.size());
111 const MCSectionPIC16 *NewSection
112 = getPIC16Section(name.c_str(), /*FIXME*/ SectionKind::getMetadata());
114 FoundBSS = new PIC16Section(NewSection);
116 // Add this newly created BSS section to the list of BSSSections.
117 BSSSections.push_back(FoundBSS);
120 // Insert the GV into this BSS.
121 FoundBSS->Items.push_back(GV);
122 FoundBSS->Size += ValSize;
127 PIC16TargetObjectFile::getIDATASectionForGlobal(const GlobalVariable *GV) const{
128 assert(GV->hasInitializer() && "This global doesn't need space");
129 Constant *C = GV->getInitializer();
130 assert(!C->isNullValue() && "initialized globals has zero initializer");
131 assert(GV->getType()->getAddressSpace() == PIC16ISD::RAM_SPACE &&
132 "can split initialized RAM data only");
134 // Find how much space this global needs.
135 const TargetData *TD = TM->getTargetData();
136 const Type *Ty = C->getType();
137 unsigned ValSize = TD->getTypeAllocSize(Ty);
139 // Go through all IDATA Sections and assign this variable
140 // to the first available section having enough space.
141 PIC16Section *FoundIDATA = NULL;
142 for (unsigned i = 0; i < IDATASections.size(); i++) {
143 if (DataBankSize - IDATASections[i]->Size >= ValSize) {
144 FoundIDATA = IDATASections[i];
149 // No IDATA section spacious enough was found. Crate a new one.
151 std::string name = PAN::getIdataSectionName(IDATASections.size());
152 const MCSectionPIC16 *NewSection =
153 getPIC16Section(name.c_str(), /*FIXME*/ SectionKind::getMetadata());
155 FoundIDATA = new PIC16Section(NewSection);
157 // Add this newly created IDATA section to the list of IDATASections.
158 IDATASections.push_back(FoundIDATA);
161 // Insert the GV into this IDATA.
162 FoundIDATA->Items.push_back(GV);
163 FoundIDATA->Size += ValSize;
164 return FoundIDATA->S_;
167 // Get the section for an automatic variable of a function.
168 // For PIC16 they are globals only with mangled names.
170 PIC16TargetObjectFile::getSectionForAuto(const GlobalVariable *GV) const {
172 const std::string name = PAN::getSectionNameForSym(GV->getName());
174 // Go through all Auto Sections and assign this variable
175 // to the appropriate section.
176 PIC16Section *FoundAutoSec = NULL;
177 for (unsigned i = 0; i < AutosSections.size(); i++) {
178 if (AutosSections[i]->S_->getName() == name) {
179 FoundAutoSec = AutosSections[i];
184 // No Auto section was found. Crate a new one.
186 const MCSectionPIC16 *NewSection =
187 getPIC16Section(name.c_str(), /*FIXME*/ SectionKind::getMetadata());
189 FoundAutoSec = new PIC16Section(NewSection);
191 // Add this newly created autos section to the list of AutosSections.
192 AutosSections.push_back(FoundAutoSec);
195 // Insert the auto into this section.
196 FoundAutoSec->Items.push_back(GV);
198 return FoundAutoSec->S_;
202 // Override default implementation to put the true globals into
203 // multiple data sections if required.
205 PIC16TargetObjectFile::SelectSectionForGlobal(const GlobalValue *GV1,
208 const TargetMachine &TM) const {
209 // We select the section based on the initializer here, so it really
210 // has to be a GlobalVariable.
211 const GlobalVariable *GV = dyn_cast<GlobalVariable>(GV1);
213 return TargetLoweringObjectFile::SelectSectionForGlobal(GV1, Kind, Mang,TM);
215 // Record External Var Decls.
216 if (GV->isDeclaration()) {
217 ExternalVarDecls->Items.push_back(GV);
218 return ExternalVarDecls->S_;
221 assert(GV->hasInitializer() && "A def without initializer?");
223 // First, if this is an automatic variable for a function, get the section
224 // name for it and return.
225 std::string name = GV->getName();
226 if (PAN::isLocalName(name))
227 return getSectionForAuto(GV);
229 // Record Exteranl Var Defs.
230 if (GV->hasExternalLinkage() || GV->hasCommonLinkage())
231 ExternalVarDefs->Items.push_back(GV);
233 // See if this is an uninitialized global.
234 const Constant *C = GV->getInitializer();
235 if (C->isNullValue())
236 return getBSSSectionForGlobal(GV);
238 // If this is initialized data in RAM. Put it in the correct IDATA section.
239 if (GV->getType()->getAddressSpace() == PIC16ISD::RAM_SPACE)
240 return getIDATASectionForGlobal(GV);
242 // This is initialized data in rom, put it in the readonly section.
243 if (GV->getType()->getAddressSpace() == PIC16ISD::ROM_SPACE)
244 return getROSectionForGlobal(GV);
246 // Else let the default implementation take care of it.
247 return TargetLoweringObjectFile::SelectSectionForGlobal(GV, Kind, Mang,TM);
250 PIC16TargetObjectFile::~PIC16TargetObjectFile() {
251 for (unsigned i = 0; i < BSSSections.size(); i++)
252 delete BSSSections[i];
253 for (unsigned i = 0; i < IDATASections.size(); i++)
254 delete IDATASections[i];
255 for (unsigned i = 0; i < AutosSections.size(); i++)
256 delete AutosSections[i];
257 for (unsigned i = 0; i < ROSections.size(); i++)
258 delete ROSections[i];
259 delete ExternalVarDecls;
260 delete ExternalVarDefs;
264 /// getSpecialCasedSectionGlobals - Allow the target to completely override
265 /// section assignment of a global.
266 const MCSection *PIC16TargetObjectFile::
267 getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
268 Mangler *Mang, const TargetMachine &TM) const {
269 assert(GV->hasSection());
271 if (const GlobalVariable *GVar = cast<GlobalVariable>(GV)) {
272 std::string SectName = GVar->getSection();
273 // If address for a variable is specified, get the address and create
275 std::string AddrStr = "Address=";
276 if (SectName.compare(0, AddrStr.length(), AddrStr) == 0) {
277 std::string SectAddr = SectName.substr(AddrStr.length());
278 return CreateSectionForGlobal(GVar, Mang, SectAddr);
281 // Create the section specified with section attribute.
282 return CreateSectionForGlobal(GVar, Mang);
285 return getPIC16Section(GV->getSection().c_str(), Kind);
288 // Create a new section for global variable. If Addr is given then create
289 // section at that address else create by name.
291 PIC16TargetObjectFile::CreateSectionForGlobal(const GlobalVariable *GV,
293 const std::string &Addr) const {
294 // See if this is an uninitialized global.
295 const Constant *C = GV->getInitializer();
296 if (C->isNullValue())
297 return CreateBSSSectionForGlobal(GV, Addr);
299 // If this is initialized data in RAM. Put it in the correct IDATA section.
300 if (GV->getType()->getAddressSpace() == PIC16ISD::RAM_SPACE)
301 return CreateIDATASectionForGlobal(GV, Addr);
303 // This is initialized data in rom, put it in the readonly section.
304 if (GV->getType()->getAddressSpace() == PIC16ISD::ROM_SPACE)
305 return CreateROSectionForGlobal(GV, Addr);
307 // Else let the default implementation take care of it.
308 return TargetLoweringObjectFile::SectionForGlobal(GV, Mang, *TM);
311 // Create uninitialized section for a variable.
313 PIC16TargetObjectFile::CreateBSSSectionForGlobal(const GlobalVariable *GV,
314 std::string Addr) const {
315 assert(GV->hasInitializer() && "This global doesn't need space");
316 assert(GV->getInitializer()->isNullValue() &&
317 "Unitialized global has non-zero initializer");
319 // If address is given then create a section at that address else create a
320 // section by section name specified in GV.
321 PIC16Section *FoundBSS = NULL;
323 Name = GV->getSection() + " UDATA";
324 for (unsigned i = 0; i < BSSSections.size(); i++) {
325 if (BSSSections[i]->S_->getName() == Name) {
326 FoundBSS = BSSSections[i];
331 std::string Prefix = GV->getNameStr() + "." + Addr + ".";
332 Name = PAN::getUdataSectionName(BSSSections.size(), Prefix) + " " + Addr;
335 PIC16Section *NewBSS = FoundBSS;
336 if (NewBSS == NULL) {
337 const MCSectionPIC16 *NewSection =
338 getPIC16Section(Name.c_str(), SectionKind::getBSS());
339 NewBSS = new PIC16Section(NewSection);
340 BSSSections.push_back(NewBSS);
343 // Insert the GV into this BSS.
344 NewBSS->Items.push_back(GV);
346 // We do not want to put any GV without explicit section into this section
347 // so set its size to DatabankSize.
348 NewBSS->Size = DataBankSize;
352 // Get rom section for a variable. Currently there can be only one rom section
353 // unless a variable explicitly requests a section.
355 PIC16TargetObjectFile::getROSectionForGlobal(const GlobalVariable *GV) const {
356 ROSections[0]->Items.push_back(GV);
357 return ROSections[0]->S_;
360 // Create initialized data section for a variable.
362 PIC16TargetObjectFile::CreateIDATASectionForGlobal(const GlobalVariable *GV,
363 std::string Addr) const {
364 assert(GV->hasInitializer() && "This global doesn't need space");
365 assert(!GV->getInitializer()->isNullValue() &&
366 "initialized global has zero initializer");
367 assert(GV->getType()->getAddressSpace() == PIC16ISD::RAM_SPACE &&
368 "can be used for initialized RAM data only");
371 // If address is given then create a section at that address else create a
372 // section by section name specified in GV.
373 PIC16Section *FoundIDATASec = NULL;
375 Name = GV->getSection() + " IDATA";
376 for (unsigned i = 0; i < IDATASections.size(); i++) {
377 if (IDATASections[i]->S_->getName() == Name) {
378 FoundIDATASec = IDATASections[i];
383 std::string Prefix = GV->getNameStr() + "." + Addr + ".";
384 Name = PAN::getIdataSectionName(IDATASections.size(), Prefix) + " " + Addr;
387 PIC16Section *NewIDATASec = FoundIDATASec;
388 if (NewIDATASec == NULL) {
389 const MCSectionPIC16 *NewSection =
390 getPIC16Section(Name.c_str(), /* FIXME */SectionKind::getMetadata());
391 NewIDATASec = new PIC16Section(NewSection);
392 IDATASections.push_back(NewIDATASec);
394 // Insert the GV into this IDATA Section.
395 NewIDATASec->Items.push_back(GV);
396 // We do not want to put any GV without explicit section into this section
397 // so set its size to DatabankSize.
398 NewIDATASec->Size = DataBankSize;
399 return NewIDATASec->S_;
402 // Create a section in rom for a variable.
404 PIC16TargetObjectFile::CreateROSectionForGlobal(const GlobalVariable *GV,
405 std::string Addr) const {
406 assert(GV->getType()->getAddressSpace() == PIC16ISD::ROM_SPACE &&
407 "can be used for ROM data only");
410 // If address is given then create a section at that address else create a
411 // section by section name specified in GV.
412 PIC16Section *FoundROSec = NULL;
414 Name = GV->getSection() + " ROMDATA";
415 for (unsigned i = 1; i < ROSections.size(); i++) {
416 if (ROSections[i]->S_->getName() == Name) {
417 FoundROSec = ROSections[i];
422 std::string Prefix = GV->getNameStr() + "." + Addr + ".";
423 Name = PAN::getRomdataSectionName(ROSections.size(), Prefix) + " " + Addr;
426 PIC16Section *NewRomSec = FoundROSec;
427 if (NewRomSec == NULL) {
428 const MCSectionPIC16 *NewSection =
429 getPIC16Section(Name.c_str(), SectionKind::getReadOnly());
430 NewRomSec = new PIC16Section(NewSection);
431 ROSections.push_back(NewRomSec);
434 // Insert the GV into this ROM Section.
435 NewRomSec->Items.push_back(GV);
436 return NewRomSec->S_;