blob: efc03acce93849585a5c08059e8daf862ba07a23 [file] [log] [blame]
Chris Lattnerf0144122009-07-28 03:13:23 +00001//===-- PIC16TargetObjectFile.cpp - PIC16 object files --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "PIC16TargetObjectFile.h"
11#include "PIC16ISelLowering.h"
12#include "PIC16TargetMachine.h"
13#include "llvm/DerivedTypes.h"
14#include "llvm/Module.h"
15using namespace llvm;
16
17
18PIC16TargetObjectFile::PIC16TargetObjectFile(const PIC16TargetMachine &tm)
19: TM (tm) {
20 BSSSection_ = getOrCreateSection("udata.# UDATA", false, SectionKind::BSS);
21 ReadOnlySection = getOrCreateSection("romdata.# ROMDATA", false,
22 SectionKind::ReadOnly);
23 DataSection = getOrCreateSection("idata.# IDATA", false,SectionKind::DataRel);
24
25 // Need because otherwise a .text symbol is emitted by DwarfWriter
26 // in BeginModule, and gpasm cribbs for that .text symbol.
27 TextSection = getOrCreateSection("", true, SectionKind::Text);
28
29
30 PIC16Section *ROSection = new PIC16Section(ReadOnlySection);
31 ROSections.push_back(ROSection);
32
33 // FIXME: I don't know what the classification of these sections really is.
34 ExternalVarDecls = new PIC16Section(getOrCreateSection("ExternalVarDecls",
35 false,
36 SectionKind::Metadata));
37 ExternalVarDefs = new PIC16Section(getOrCreateSection("ExternalVarDefs",
38 false,
39 SectionKind::Metadata));
40}
41
42
43const Section *
44PIC16TargetObjectFile::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");
48
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);
53
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];
60 break;
61 }
62 }
63
64 // No BSS section spacious enough was found. Crate a new one.
65 if (!FoundBSS) {
66 std::string name = PAN::getUdataSectionName(BSSSections.size());
67 const Section *NewSection = getOrCreateSection(name.c_str(), false,
68 // FIXME.
69 SectionKind::Metadata);
70
71 FoundBSS = new PIC16Section(NewSection);
72
73 // Add this newly created BSS section to the list of BSSSections.
74 BSSSections.push_back(FoundBSS);
75 }
76
77 // Insert the GV into this BSS.
78 FoundBSS->Items.push_back(GV);
79 FoundBSS->Size += ValSize;
80 return FoundBSS->S_;
81}
82
83const Section *
84PIC16TargetObjectFile::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");
90
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);
95
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];
102 break;
103 }
104 }
105
106 // No IDATA section spacious enough was found. Crate a new one.
107 if (!FoundIDATA) {
108 std::string name = PAN::getIdataSectionName(IDATASections.size());
109 const Section *NewSection = getOrCreateSection(name.c_str(),
110 false,
111 // FIXME.
112 SectionKind::Metadata);
113
114 FoundIDATA = new PIC16Section(NewSection);
115
116 // Add this newly created IDATA section to the list of IDATASections.
117 IDATASections.push_back(FoundIDATA);
118 }
119
120 // Insert the GV into this IDATA.
121 FoundIDATA->Items.push_back(GV);
122 FoundIDATA->Size += ValSize;
123 return FoundIDATA->S_;
124}
125
126// Get the section for an automatic variable of a function.
127// For PIC16 they are globals only with mangled names.
128const Section *
129PIC16TargetObjectFile::getSectionForAuto(const GlobalVariable *GV) const {
130
131 const std::string name = PAN::getSectionNameForSym(GV->getName());
132
133 // Go through all Auto Sections and assign this variable
134 // to the appropriate section.
135 PIC16Section *FoundAutoSec = NULL;
136 for (unsigned i = 0; i < AutosSections.size(); i++) {
137 if (AutosSections[i]->S_->getName() == name) {
138 FoundAutoSec = AutosSections[i];
139 break;
140 }
141 }
142
143 // No Auto section was found. Crate a new one.
144 if (!FoundAutoSec) {
145 const Section *NewSection = getOrCreateSection(name.c_str(),
146 // FIXME.
147 false,
148 SectionKind::Metadata);
149
150 FoundAutoSec = new PIC16Section(NewSection);
151
152 // Add this newly created autos section to the list of AutosSections.
153 AutosSections.push_back(FoundAutoSec);
154 }
155
156 // Insert the auto into this section.
157 FoundAutoSec->Items.push_back(GV);
158
159 return FoundAutoSec->S_;
160}
161
162
163// Override default implementation to put the true globals into
164// multiple data sections if required.
165const Section*
166PIC16TargetObjectFile::SelectSectionForGlobal(const GlobalValue *GV1,
167 SectionKind Kind,
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);
172 if (!GV)
173 return TargetLoweringObjectFile::SelectSectionForGlobal(GV1, Kind, TM);
174
175 // Record External Var Decls.
176 if (GV->isDeclaration()) {
177 ExternalVarDecls->Items.push_back(GV);
178 return ExternalVarDecls->S_;
179 }
180
181 assert(GV->hasInitializer() && "A def without initializer?");
182
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);
188
189 // Record Exteranl Var Defs.
190 if (GV->hasExternalLinkage() || GV->hasCommonLinkage())
191 ExternalVarDefs->Items.push_back(GV);
192
193 // See if this is an uninitialized global.
194 const Constant *C = GV->getInitializer();
195 if (C->isNullValue())
196 return getBSSSectionForGlobal(GV);
197
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);
201
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);
205
206 // Else let the default implementation take care of it.
207 return TargetLoweringObjectFile::SelectSectionForGlobal(GV, Kind, TM);
208}
209
210PIC16TargetObjectFile::~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;
221}
222
223
224/// getSpecialCasedSectionGlobals - Allow the target to completely override
225/// section assignment of a global.
226const Section *
227PIC16TargetObjectFile::getSpecialCasedSectionGlobals(const GlobalValue *GV,
228 SectionKind Kind) const {
229 // If GV has a sectin name or section address create that section now.
230 if (GV->hasSection()) {
231 if (const GlobalVariable *GVar = cast<GlobalVariable>(GV)) {
232 std::string SectName = GVar->getSection();
233 // If address for a variable is specified, get the address and create
234 // section.
235 std::string AddrStr = "Address=";
236 if (SectName.compare(0, AddrStr.length(), AddrStr) == 0) {
237 std::string SectAddr = SectName.substr(AddrStr.length());
238 return CreateSectionForGlobal(GVar, SectAddr);
239 }
240
241 // Create the section specified with section attribute.
242 return CreateSectionForGlobal(GVar);
243 }
244 }
245
246 return 0;
247}
248
249// Create a new section for global variable. If Addr is given then create
250// section at that address else create by name.
251const Section *
252PIC16TargetObjectFile::CreateSectionForGlobal(const GlobalVariable *GV,
253 const std::string &Addr) const {
254 // See if this is an uninitialized global.
255 const Constant *C = GV->getInitializer();
256 if (C->isNullValue())
257 return CreateBSSSectionForGlobal(GV, Addr);
258
259 // If this is initialized data in RAM. Put it in the correct IDATA section.
260 if (GV->getType()->getAddressSpace() == PIC16ISD::RAM_SPACE)
261 return CreateIDATASectionForGlobal(GV, Addr);
262
263 // This is initialized data in rom, put it in the readonly section.
264 if (GV->getType()->getAddressSpace() == PIC16ISD::ROM_SPACE)
265 return CreateROSectionForGlobal(GV, Addr);
266
267 // Else let the default implementation take care of it.
268 return TargetLoweringObjectFile::SectionForGlobal(GV, TM);
269}
270
271// Create uninitialized section for a variable.
272const Section *
273PIC16TargetObjectFile::CreateBSSSectionForGlobal(const GlobalVariable *GV,
274 std::string Addr) const {
275 assert(GV->hasInitializer() && "This global doesn't need space");
276 assert(GV->getInitializer()->isNullValue() &&
277 "Unitialized global has non-zero initializer");
278 std::string Name;
279 // If address is given then create a section at that address else create a
280 // section by section name specified in GV.
281 PIC16Section *FoundBSS = NULL;
282 if (Addr.empty()) {
283 Name = GV->getSection() + " UDATA";
284 for (unsigned i = 0; i < BSSSections.size(); i++) {
285 if (BSSSections[i]->S_->getName() == Name) {
286 FoundBSS = BSSSections[i];
287 break;
288 }
289 }
290 } else {
291 std::string Prefix = GV->getNameStr() + "." + Addr + ".";
292 Name = PAN::getUdataSectionName(BSSSections.size(), Prefix) + " " + Addr;
293 }
294
295 PIC16Section *NewBSS = FoundBSS;
296 if (NewBSS == NULL) {
297 const Section *NewSection = getOrCreateSection(Name.c_str(),
298 false, SectionKind::BSS);
299 NewBSS = new PIC16Section(NewSection);
300 BSSSections.push_back(NewBSS);
301 }
302
303 // Insert the GV into this BSS.
304 NewBSS->Items.push_back(GV);
305
306 // We do not want to put any GV without explicit section into this section
307 // so set its size to DatabankSize.
308 NewBSS->Size = DataBankSize;
309 return NewBSS->S_;
310}
311
312// Get rom section for a variable. Currently there can be only one rom section
313// unless a variable explicitly requests a section.
314const Section *
315PIC16TargetObjectFile::getROSectionForGlobal(const GlobalVariable *GV) const {
316 ROSections[0]->Items.push_back(GV);
317 return ROSections[0]->S_;
318}
319
320// Create initialized data section for a variable.
321const Section *
322PIC16TargetObjectFile::CreateIDATASectionForGlobal(const GlobalVariable *GV,
323 std::string Addr) const {
324 assert(GV->hasInitializer() && "This global doesn't need space");
325 assert(!GV->getInitializer()->isNullValue() &&
326 "initialized global has zero initializer");
327 assert(GV->getType()->getAddressSpace() == PIC16ISD::RAM_SPACE &&
328 "can be used for initialized RAM data only");
329
330 std::string Name;
331 // If address is given then create a section at that address else create a
332 // section by section name specified in GV.
333 PIC16Section *FoundIDATASec = NULL;
334 if (Addr.empty()) {
335 Name = GV->getSection() + " IDATA";
336 for (unsigned i = 0; i < IDATASections.size(); i++) {
337 if (IDATASections[i]->S_->getName() == Name) {
338 FoundIDATASec = IDATASections[i];
339 break;
340 }
341 }
342 } else {
343 std::string Prefix = GV->getNameStr() + "." + Addr + ".";
344 Name = PAN::getIdataSectionName(IDATASections.size(), Prefix) + " " + Addr;
345 }
346
347 PIC16Section *NewIDATASec = FoundIDATASec;
348 if (NewIDATASec == NULL) {
349 const Section *NewSection = getOrCreateSection(Name.c_str(),
350 false,
351 // FIXME:
352 SectionKind::Metadata);
353 NewIDATASec = new PIC16Section(NewSection);
354 IDATASections.push_back(NewIDATASec);
355 }
356 // Insert the GV into this IDATA Section.
357 NewIDATASec->Items.push_back(GV);
358 // We do not want to put any GV without explicit section into this section
359 // so set its size to DatabankSize.
360 NewIDATASec->Size = DataBankSize;
361 return NewIDATASec->S_;
362}
363
364// Create a section in rom for a variable.
365const Section *
366PIC16TargetObjectFile::CreateROSectionForGlobal(const GlobalVariable *GV,
367 std::string Addr) const {
368 assert(GV->getType()->getAddressSpace() == PIC16ISD::ROM_SPACE &&
369 "can be used for ROM data only");
370
371 std::string Name;
372 // If address is given then create a section at that address else create a
373 // section by section name specified in GV.
374 PIC16Section *FoundROSec = NULL;
375 if (Addr.empty()) {
376 Name = GV->getSection() + " ROMDATA";
377 for (unsigned i = 1; i < ROSections.size(); i++) {
378 if (ROSections[i]->S_->getName() == Name) {
379 FoundROSec = ROSections[i];
380 break;
381 }
382 }
383 } else {
384 std::string Prefix = GV->getNameStr() + "." + Addr + ".";
385 Name = PAN::getRomdataSectionName(ROSections.size(), Prefix) + " " + Addr;
386 }
387
388 PIC16Section *NewRomSec = FoundROSec;
389 if (NewRomSec == NULL) {
390 const Section *NewSection = getOrCreateSection(Name.c_str(),
391 false,
392 SectionKind::ReadOnly);
393 NewRomSec = new PIC16Section(NewSection);
394 ROSections.push_back(NewRomSec);
395 }
396
397 // Insert the GV into this ROM Section.
398 NewRomSec->Items.push_back(GV);
399 return NewRomSec->S_;
400}
401