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 "MCSectionPIC16.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"
22 MCSectionPIC16
*MCSectionPIC16::Create(const StringRef
&Name
,
23 SectionKind K
, MCContext
&Ctx
) {
24 return new (Ctx
) MCSectionPIC16(Name
, K
);
28 void MCSectionPIC16::PrintSwitchToSection(const TargetAsmInfo
&TAI
,
29 raw_ostream
&OS
) const {
30 OS
<< getName() << '\n';
36 PIC16TargetObjectFile::PIC16TargetObjectFile()
37 : ExternalVarDecls(0), ExternalVarDefs(0) {
40 const MCSectionPIC16
*PIC16TargetObjectFile::
41 getPIC16Section(const char *Name
, SectionKind Kind
) const {
42 MCSectionPIC16
*&Entry
= SectionsByName
[Name
];
46 return Entry
= MCSectionPIC16::Create(Name
, Kind
, getContext());
50 void PIC16TargetObjectFile::Initialize(MCContext
&Ctx
, const TargetMachine
&tm
){
51 TargetLoweringObjectFile::Initialize(Ctx
, tm
);
54 BSSSection
= getPIC16Section("udata.# UDATA", SectionKind::getBSS());
55 ReadOnlySection
= getPIC16Section("romdata.# ROMDATA",
56 SectionKind::getReadOnly());
57 DataSection
= getPIC16Section("idata.# IDATA", SectionKind::getDataRel());
59 // Need because otherwise a .text symbol is emitted by DwarfWriter
60 // in BeginModule, and gpasm cribbs for that .text symbol.
61 TextSection
= getPIC16Section("", SectionKind::getText());
63 ROSections
.push_back(new PIC16Section((MCSectionPIC16
*)ReadOnlySection
));
65 // FIXME: I don't know what the classification of these sections really is.
66 ExternalVarDecls
= new PIC16Section(getPIC16Section("ExternalVarDecls",
67 SectionKind::getMetadata()));
68 ExternalVarDefs
= new PIC16Section(getPIC16Section("ExternalVarDefs",
69 SectionKind::getMetadata()));
72 const MCSection
*PIC16TargetObjectFile::
73 getSectionForFunction(const std::string
&FnName
) const {
74 std::string T
= PAN::getCodeSectionName(FnName
);
75 return getPIC16Section(T
.c_str(), SectionKind::getText());
79 const MCSection
*PIC16TargetObjectFile::
80 getSectionForFunctionFrame(const std::string
&FnName
) const {
81 std::string T
= PAN::getFrameSectionName(FnName
);
82 return getPIC16Section(T
.c_str(), SectionKind::getDataRel());
86 PIC16TargetObjectFile::getBSSSectionForGlobal(const GlobalVariable
*GV
) const {
87 assert(GV
->hasInitializer() && "This global doesn't need space");
88 Constant
*C
= GV
->getInitializer();
89 assert(C
->isNullValue() && "Unitialized globals has non-zero initializer");
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 BSS Sections and assign this variable
97 // to the first available section having enough space.
98 PIC16Section
*FoundBSS
= NULL
;
99 for (unsigned i
= 0; i
< BSSSections
.size(); i
++) {
100 if (DataBankSize
- BSSSections
[i
]->Size
>= ValSize
) {
101 FoundBSS
= BSSSections
[i
];
106 // No BSS section spacious enough was found. Crate a new one.
108 std::string name
= PAN::getUdataSectionName(BSSSections
.size());
109 const MCSectionPIC16
*NewSection
110 = getPIC16Section(name
.c_str(), /*FIXME*/ SectionKind::getMetadata());
112 FoundBSS
= new PIC16Section(NewSection
);
114 // Add this newly created BSS section to the list of BSSSections.
115 BSSSections
.push_back(FoundBSS
);
118 // Insert the GV into this BSS.
119 FoundBSS
->Items
.push_back(GV
);
120 FoundBSS
->Size
+= ValSize
;
125 PIC16TargetObjectFile::getIDATASectionForGlobal(const GlobalVariable
*GV
) const{
126 assert(GV
->hasInitializer() && "This global doesn't need space");
127 Constant
*C
= GV
->getInitializer();
128 assert(!C
->isNullValue() && "initialized globals has zero initializer");
129 assert(GV
->getType()->getAddressSpace() == PIC16ISD::RAM_SPACE
&&
130 "can split initialized RAM data only");
132 // Find how much space this global needs.
133 const TargetData
*TD
= TM
->getTargetData();
134 const Type
*Ty
= C
->getType();
135 unsigned ValSize
= TD
->getTypeAllocSize(Ty
);
137 // Go through all IDATA Sections and assign this variable
138 // to the first available section having enough space.
139 PIC16Section
*FoundIDATA
= NULL
;
140 for (unsigned i
= 0; i
< IDATASections
.size(); i
++) {
141 if (DataBankSize
- IDATASections
[i
]->Size
>= ValSize
) {
142 FoundIDATA
= IDATASections
[i
];
147 // No IDATA section spacious enough was found. Crate a new one.
149 std::string name
= PAN::getIdataSectionName(IDATASections
.size());
150 const MCSectionPIC16
*NewSection
=
151 getPIC16Section(name
.c_str(), /*FIXME*/ SectionKind::getMetadata());
153 FoundIDATA
= new PIC16Section(NewSection
);
155 // Add this newly created IDATA section to the list of IDATASections.
156 IDATASections
.push_back(FoundIDATA
);
159 // Insert the GV into this IDATA.
160 FoundIDATA
->Items
.push_back(GV
);
161 FoundIDATA
->Size
+= ValSize
;
162 return FoundIDATA
->S_
;
165 // Get the section for an automatic variable of a function.
166 // For PIC16 they are globals only with mangled names.
168 PIC16TargetObjectFile::getSectionForAuto(const GlobalVariable
*GV
) const {
170 const std::string name
= PAN::getSectionNameForSym(GV
->getName());
172 // Go through all Auto Sections and assign this variable
173 // to the appropriate section.
174 PIC16Section
*FoundAutoSec
= NULL
;
175 for (unsigned i
= 0; i
< AutosSections
.size(); i
++) {
176 if (AutosSections
[i
]->S_
->getName() == name
) {
177 FoundAutoSec
= AutosSections
[i
];
182 // No Auto section was found. Crate a new one.
184 const MCSectionPIC16
*NewSection
=
185 getPIC16Section(name
.c_str(), /*FIXME*/ SectionKind::getMetadata());
187 FoundAutoSec
= new PIC16Section(NewSection
);
189 // Add this newly created autos section to the list of AutosSections.
190 AutosSections
.push_back(FoundAutoSec
);
193 // Insert the auto into this section.
194 FoundAutoSec
->Items
.push_back(GV
);
196 return FoundAutoSec
->S_
;
200 // Override default implementation to put the true globals into
201 // multiple data sections if required.
203 PIC16TargetObjectFile::SelectSectionForGlobal(const GlobalValue
*GV1
,
206 const TargetMachine
&TM
) const {
207 // We select the section based on the initializer here, so it really
208 // has to be a GlobalVariable.
209 const GlobalVariable
*GV
= dyn_cast
<GlobalVariable
>(GV1
);
211 return TargetLoweringObjectFile::SelectSectionForGlobal(GV1
, Kind
, Mang
,TM
);
213 // Record External Var Decls.
214 if (GV
->isDeclaration()) {
215 ExternalVarDecls
->Items
.push_back(GV
);
216 return ExternalVarDecls
->S_
;
219 assert(GV
->hasInitializer() && "A def without initializer?");
221 // First, if this is an automatic variable for a function, get the section
222 // name for it and return.
223 std::string name
= GV
->getName();
224 if (PAN::isLocalName(name
))
225 return getSectionForAuto(GV
);
227 // Record Exteranl Var Defs.
228 if (GV
->hasExternalLinkage() || GV
->hasCommonLinkage())
229 ExternalVarDefs
->Items
.push_back(GV
);
231 // See if this is an uninitialized global.
232 const Constant
*C
= GV
->getInitializer();
233 if (C
->isNullValue())
234 return getBSSSectionForGlobal(GV
);
236 // If this is initialized data in RAM. Put it in the correct IDATA section.
237 if (GV
->getType()->getAddressSpace() == PIC16ISD::RAM_SPACE
)
238 return getIDATASectionForGlobal(GV
);
240 // This is initialized data in rom, put it in the readonly section.
241 if (GV
->getType()->getAddressSpace() == PIC16ISD::ROM_SPACE
)
242 return getROSectionForGlobal(GV
);
244 // Else let the default implementation take care of it.
245 return TargetLoweringObjectFile::SelectSectionForGlobal(GV
, Kind
, Mang
,TM
);
248 PIC16TargetObjectFile::~PIC16TargetObjectFile() {
249 for (unsigned i
= 0; i
< BSSSections
.size(); i
++)
250 delete BSSSections
[i
];
251 for (unsigned i
= 0; i
< IDATASections
.size(); i
++)
252 delete IDATASections
[i
];
253 for (unsigned i
= 0; i
< AutosSections
.size(); i
++)
254 delete AutosSections
[i
];
255 for (unsigned i
= 0; i
< ROSections
.size(); i
++)
256 delete ROSections
[i
];
257 delete ExternalVarDecls
;
258 delete ExternalVarDefs
;
262 /// getSpecialCasedSectionGlobals - Allow the target to completely override
263 /// section assignment of a global.
264 const MCSection
*PIC16TargetObjectFile::
265 getExplicitSectionGlobal(const GlobalValue
*GV
, SectionKind Kind
,
266 Mangler
*Mang
, const TargetMachine
&TM
) const {
267 assert(GV
->hasSection());
269 if (const GlobalVariable
*GVar
= cast
<GlobalVariable
>(GV
)) {
270 std::string SectName
= GVar
->getSection();
271 // If address for a variable is specified, get the address and create
273 std::string AddrStr
= "Address=";
274 if (SectName
.compare(0, AddrStr
.length(), AddrStr
) == 0) {
275 std::string SectAddr
= SectName
.substr(AddrStr
.length());
276 return CreateSectionForGlobal(GVar
, Mang
, SectAddr
);
279 // Create the section specified with section attribute.
280 return CreateSectionForGlobal(GVar
, Mang
);
283 return getPIC16Section(GV
->getSection().c_str(), Kind
);
286 // Create a new section for global variable. If Addr is given then create
287 // section at that address else create by name.
289 PIC16TargetObjectFile::CreateSectionForGlobal(const GlobalVariable
*GV
,
291 const std::string
&Addr
) const {
292 // See if this is an uninitialized global.
293 const Constant
*C
= GV
->getInitializer();
294 if (C
->isNullValue())
295 return CreateBSSSectionForGlobal(GV
, Addr
);
297 // If this is initialized data in RAM. Put it in the correct IDATA section.
298 if (GV
->getType()->getAddressSpace() == PIC16ISD::RAM_SPACE
)
299 return CreateIDATASectionForGlobal(GV
, Addr
);
301 // This is initialized data in rom, put it in the readonly section.
302 if (GV
->getType()->getAddressSpace() == PIC16ISD::ROM_SPACE
)
303 return CreateROSectionForGlobal(GV
, Addr
);
305 // Else let the default implementation take care of it.
306 return TargetLoweringObjectFile::SectionForGlobal(GV
, Mang
, *TM
);
309 // Create uninitialized section for a variable.
311 PIC16TargetObjectFile::CreateBSSSectionForGlobal(const GlobalVariable
*GV
,
312 std::string Addr
) const {
313 assert(GV
->hasInitializer() && "This global doesn't need space");
314 assert(GV
->getInitializer()->isNullValue() &&
315 "Unitialized global has non-zero initializer");
317 // If address is given then create a section at that address else create a
318 // section by section name specified in GV.
319 PIC16Section
*FoundBSS
= NULL
;
321 Name
= GV
->getSection() + " UDATA";
322 for (unsigned i
= 0; i
< BSSSections
.size(); i
++) {
323 if (BSSSections
[i
]->S_
->getName() == Name
) {
324 FoundBSS
= BSSSections
[i
];
329 std::string Prefix
= GV
->getNameStr() + "." + Addr
+ ".";
330 Name
= PAN::getUdataSectionName(BSSSections
.size(), Prefix
) + " " + Addr
;
333 PIC16Section
*NewBSS
= FoundBSS
;
334 if (NewBSS
== NULL
) {
335 const MCSectionPIC16
*NewSection
=
336 getPIC16Section(Name
.c_str(), SectionKind::getBSS());
337 NewBSS
= new PIC16Section(NewSection
);
338 BSSSections
.push_back(NewBSS
);
341 // Insert the GV into this BSS.
342 NewBSS
->Items
.push_back(GV
);
344 // We do not want to put any GV without explicit section into this section
345 // so set its size to DatabankSize.
346 NewBSS
->Size
= DataBankSize
;
350 // Get rom section for a variable. Currently there can be only one rom section
351 // unless a variable explicitly requests a section.
353 PIC16TargetObjectFile::getROSectionForGlobal(const GlobalVariable
*GV
) const {
354 ROSections
[0]->Items
.push_back(GV
);
355 return ROSections
[0]->S_
;
358 // Create initialized data section for a variable.
360 PIC16TargetObjectFile::CreateIDATASectionForGlobal(const GlobalVariable
*GV
,
361 std::string Addr
) const {
362 assert(GV
->hasInitializer() && "This global doesn't need space");
363 assert(!GV
->getInitializer()->isNullValue() &&
364 "initialized global has zero initializer");
365 assert(GV
->getType()->getAddressSpace() == PIC16ISD::RAM_SPACE
&&
366 "can be used for initialized RAM data only");
369 // If address is given then create a section at that address else create a
370 // section by section name specified in GV.
371 PIC16Section
*FoundIDATASec
= NULL
;
373 Name
= GV
->getSection() + " IDATA";
374 for (unsigned i
= 0; i
< IDATASections
.size(); i
++) {
375 if (IDATASections
[i
]->S_
->getName() == Name
) {
376 FoundIDATASec
= IDATASections
[i
];
381 std::string Prefix
= GV
->getNameStr() + "." + Addr
+ ".";
382 Name
= PAN::getIdataSectionName(IDATASections
.size(), Prefix
) + " " + Addr
;
385 PIC16Section
*NewIDATASec
= FoundIDATASec
;
386 if (NewIDATASec
== NULL
) {
387 const MCSectionPIC16
*NewSection
=
388 getPIC16Section(Name
.c_str(), /* FIXME */SectionKind::getMetadata());
389 NewIDATASec
= new PIC16Section(NewSection
);
390 IDATASections
.push_back(NewIDATASec
);
392 // Insert the GV into this IDATA Section.
393 NewIDATASec
->Items
.push_back(GV
);
394 // We do not want to put any GV without explicit section into this section
395 // so set its size to DatabankSize.
396 NewIDATASec
->Size
= DataBankSize
;
397 return NewIDATASec
->S_
;
400 // Create a section in rom for a variable.
402 PIC16TargetObjectFile::CreateROSectionForGlobal(const GlobalVariable
*GV
,
403 std::string Addr
) const {
404 assert(GV
->getType()->getAddressSpace() == PIC16ISD::ROM_SPACE
&&
405 "can be used for ROM data only");
408 // If address is given then create a section at that address else create a
409 // section by section name specified in GV.
410 PIC16Section
*FoundROSec
= NULL
;
412 Name
= GV
->getSection() + " ROMDATA";
413 for (unsigned i
= 1; i
< ROSections
.size(); i
++) {
414 if (ROSections
[i
]->S_
->getName() == Name
) {
415 FoundROSec
= ROSections
[i
];
420 std::string Prefix
= GV
->getNameStr() + "." + Addr
+ ".";
421 Name
= PAN::getRomdataSectionName(ROSections
.size(), Prefix
) + " " + Addr
;
424 PIC16Section
*NewRomSec
= FoundROSec
;
425 if (NewRomSec
== NULL
) {
426 const MCSectionPIC16
*NewSection
=
427 getPIC16Section(Name
.c_str(), SectionKind::getReadOnly());
428 NewRomSec
= new PIC16Section(NewSection
);
429 ROSections
.push_back(NewRomSec
);
432 // Insert the GV into this ROM Section.
433 NewRomSec
->Items
.push_back(GV
);
434 return NewRomSec
->S_
;