blob: 5a2d4d8896c86aaa052d22b25b09f3635d2be104 [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"
Chris Lattnera87dea42009-07-31 18:48:30 +000015#include "llvm/MC/MCSection.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000016using namespace llvm;
17
Chris Lattnera87dea42009-07-31 18:48:30 +000018void PIC16TargetObjectFile::Initialize(MCContext &Ctx, const TargetMachine &tm){
19 TargetLoweringObjectFile::Initialize(Ctx, tm);
20 TM = &tm;
21
Chris Lattnerf0144122009-07-28 03:13:23 +000022 BSSSection_ = getOrCreateSection("udata.# UDATA", false, SectionKind::BSS);
23 ReadOnlySection = getOrCreateSection("romdata.# ROMDATA", false,
24 SectionKind::ReadOnly);
25 DataSection = getOrCreateSection("idata.# IDATA", false,SectionKind::DataRel);
26
27 // Need because otherwise a .text symbol is emitted by DwarfWriter
28 // in BeginModule, and gpasm cribbs for that .text symbol.
29 TextSection = getOrCreateSection("", true, SectionKind::Text);
30
Chris Lattnera87dea42009-07-31 18:48:30 +000031 ROSections.push_back(new PIC16Section(ReadOnlySection));
Chris Lattnerf0144122009-07-28 03:13:23 +000032
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
Chris Lattnera87dea42009-07-31 18:48:30 +000043const MCSection *
Chris Lattnerf0144122009-07-28 03:13:23 +000044PIC16TargetObjectFile::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.
Chris Lattnera87dea42009-07-31 18:48:30 +000050 const TargetData *TD = TM->getTargetData();
Chris Lattnerf0144122009-07-28 03:13:23 +000051 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());
Chris Lattnera87dea42009-07-31 18:48:30 +000067 const MCSection *NewSection = getOrCreateSection(name.c_str(), false,
68 // FIXME.
69 SectionKind::Metadata);
Chris Lattnerf0144122009-07-28 03:13:23 +000070
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
Chris Lattnera87dea42009-07-31 18:48:30 +000083const MCSection *
Chris Lattnerf0144122009-07-28 03:13:23 +000084PIC16TargetObjectFile::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.
Chris Lattnera87dea42009-07-31 18:48:30 +000092 const TargetData *TD = TM->getTargetData();
Chris Lattnerf0144122009-07-28 03:13:23 +000093 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());
Chris Lattnera87dea42009-07-31 18:48:30 +0000109 const MCSection *NewSection = getOrCreateSection(name.c_str(), false,
Chris Lattnerf0144122009-07-28 03:13:23 +0000110 // FIXME.
111 SectionKind::Metadata);
112
113 FoundIDATA = new PIC16Section(NewSection);
114
115 // Add this newly created IDATA section to the list of IDATASections.
116 IDATASections.push_back(FoundIDATA);
117 }
118
119 // Insert the GV into this IDATA.
120 FoundIDATA->Items.push_back(GV);
121 FoundIDATA->Size += ValSize;
122 return FoundIDATA->S_;
123}
124
125// Get the section for an automatic variable of a function.
126// For PIC16 they are globals only with mangled names.
Chris Lattnera87dea42009-07-31 18:48:30 +0000127const MCSection *
Chris Lattnerf0144122009-07-28 03:13:23 +0000128PIC16TargetObjectFile::getSectionForAuto(const GlobalVariable *GV) const {
129
130 const std::string name = PAN::getSectionNameForSym(GV->getName());
131
132 // Go through all Auto Sections and assign this variable
133 // to the appropriate section.
134 PIC16Section *FoundAutoSec = NULL;
135 for (unsigned i = 0; i < AutosSections.size(); i++) {
136 if (AutosSections[i]->S_->getName() == name) {
137 FoundAutoSec = AutosSections[i];
138 break;
139 }
140 }
141
142 // No Auto section was found. Crate a new one.
143 if (!FoundAutoSec) {
Chris Lattnera87dea42009-07-31 18:48:30 +0000144 const MCSection *NewSection = getOrCreateSection(name.c_str(),
145 // FIXME.
146 false,
147 SectionKind::Metadata);
Chris Lattnerf0144122009-07-28 03:13:23 +0000148
149 FoundAutoSec = new PIC16Section(NewSection);
150
151 // Add this newly created autos section to the list of AutosSections.
152 AutosSections.push_back(FoundAutoSec);
153 }
154
155 // Insert the auto into this section.
156 FoundAutoSec->Items.push_back(GV);
157
158 return FoundAutoSec->S_;
159}
160
161
162// Override default implementation to put the true globals into
163// multiple data sections if required.
Chris Lattnera87dea42009-07-31 18:48:30 +0000164const MCSection *
Chris Lattnerf0144122009-07-28 03:13:23 +0000165PIC16TargetObjectFile::SelectSectionForGlobal(const GlobalValue *GV1,
166 SectionKind Kind,
Chris Lattnere53a6002009-07-29 05:09:30 +0000167 Mangler *Mang,
Chris Lattnerf0144122009-07-28 03:13:23 +0000168 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)
Chris Lattnere53a6002009-07-29 05:09:30 +0000173 return TargetLoweringObjectFile::SelectSectionForGlobal(GV1, Kind, Mang,TM);
Chris Lattnerf0144122009-07-28 03:13:23 +0000174
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.
Chris Lattnere53a6002009-07-29 05:09:30 +0000207 return TargetLoweringObjectFile::SelectSectionForGlobal(GV, Kind, Mang,TM);
Chris Lattnerf0144122009-07-28 03:13:23 +0000208}
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.
Chris Lattnera87dea42009-07-31 18:48:30 +0000226const MCSection *
Chris Lattnerf0144122009-07-28 03:13:23 +0000227PIC16TargetObjectFile::getSpecialCasedSectionGlobals(const GlobalValue *GV,
Chris Lattnere53a6002009-07-29 05:09:30 +0000228 Mangler *Mang,
Chris Lattnerf0144122009-07-28 03:13:23 +0000229 SectionKind Kind) const {
230 // If GV has a sectin name or section address create that section now.
231 if (GV->hasSection()) {
232 if (const GlobalVariable *GVar = cast<GlobalVariable>(GV)) {
233 std::string SectName = GVar->getSection();
234 // If address for a variable is specified, get the address and create
235 // section.
236 std::string AddrStr = "Address=";
237 if (SectName.compare(0, AddrStr.length(), AddrStr) == 0) {
238 std::string SectAddr = SectName.substr(AddrStr.length());
Chris Lattnere53a6002009-07-29 05:09:30 +0000239 return CreateSectionForGlobal(GVar, Mang, SectAddr);
Chris Lattnerf0144122009-07-28 03:13:23 +0000240 }
241
242 // Create the section specified with section attribute.
Chris Lattnere53a6002009-07-29 05:09:30 +0000243 return CreateSectionForGlobal(GVar, Mang);
Chris Lattnerf0144122009-07-28 03:13:23 +0000244 }
245 }
246
247 return 0;
248}
249
250// Create a new section for global variable. If Addr is given then create
251// section at that address else create by name.
Chris Lattnera87dea42009-07-31 18:48:30 +0000252const MCSection *
Chris Lattnerf0144122009-07-28 03:13:23 +0000253PIC16TargetObjectFile::CreateSectionForGlobal(const GlobalVariable *GV,
Chris Lattnere53a6002009-07-29 05:09:30 +0000254 Mangler *Mang,
Chris Lattnerf0144122009-07-28 03:13:23 +0000255 const std::string &Addr) const {
256 // See if this is an uninitialized global.
257 const Constant *C = GV->getInitializer();
258 if (C->isNullValue())
259 return CreateBSSSectionForGlobal(GV, Addr);
260
261 // If this is initialized data in RAM. Put it in the correct IDATA section.
262 if (GV->getType()->getAddressSpace() == PIC16ISD::RAM_SPACE)
263 return CreateIDATASectionForGlobal(GV, Addr);
264
265 // This is initialized data in rom, put it in the readonly section.
266 if (GV->getType()->getAddressSpace() == PIC16ISD::ROM_SPACE)
267 return CreateROSectionForGlobal(GV, Addr);
268
269 // Else let the default implementation take care of it.
Chris Lattnera87dea42009-07-31 18:48:30 +0000270 return TargetLoweringObjectFile::SectionForGlobal(GV, Mang, *TM);
Chris Lattnerf0144122009-07-28 03:13:23 +0000271}
272
273// Create uninitialized section for a variable.
Chris Lattnera87dea42009-07-31 18:48:30 +0000274const MCSection *
Chris Lattnerf0144122009-07-28 03:13:23 +0000275PIC16TargetObjectFile::CreateBSSSectionForGlobal(const GlobalVariable *GV,
276 std::string Addr) const {
277 assert(GV->hasInitializer() && "This global doesn't need space");
278 assert(GV->getInitializer()->isNullValue() &&
279 "Unitialized global has non-zero initializer");
280 std::string Name;
281 // If address is given then create a section at that address else create a
282 // section by section name specified in GV.
283 PIC16Section *FoundBSS = NULL;
284 if (Addr.empty()) {
285 Name = GV->getSection() + " UDATA";
286 for (unsigned i = 0; i < BSSSections.size(); i++) {
287 if (BSSSections[i]->S_->getName() == Name) {
288 FoundBSS = BSSSections[i];
289 break;
290 }
291 }
292 } else {
293 std::string Prefix = GV->getNameStr() + "." + Addr + ".";
294 Name = PAN::getUdataSectionName(BSSSections.size(), Prefix) + " " + Addr;
295 }
296
297 PIC16Section *NewBSS = FoundBSS;
298 if (NewBSS == NULL) {
Chris Lattnera87dea42009-07-31 18:48:30 +0000299 const MCSection *NewSection = getOrCreateSection(Name.c_str(), false,
300 SectionKind::BSS);
Chris Lattnerf0144122009-07-28 03:13:23 +0000301 NewBSS = new PIC16Section(NewSection);
302 BSSSections.push_back(NewBSS);
303 }
304
305 // Insert the GV into this BSS.
306 NewBSS->Items.push_back(GV);
307
308 // We do not want to put any GV without explicit section into this section
309 // so set its size to DatabankSize.
310 NewBSS->Size = DataBankSize;
311 return NewBSS->S_;
312}
313
314// Get rom section for a variable. Currently there can be only one rom section
315// unless a variable explicitly requests a section.
Chris Lattnera87dea42009-07-31 18:48:30 +0000316const MCSection *
Chris Lattnerf0144122009-07-28 03:13:23 +0000317PIC16TargetObjectFile::getROSectionForGlobal(const GlobalVariable *GV) const {
318 ROSections[0]->Items.push_back(GV);
319 return ROSections[0]->S_;
320}
321
322// Create initialized data section for a variable.
Chris Lattnera87dea42009-07-31 18:48:30 +0000323const MCSection *
Chris Lattnerf0144122009-07-28 03:13:23 +0000324PIC16TargetObjectFile::CreateIDATASectionForGlobal(const GlobalVariable *GV,
325 std::string Addr) const {
326 assert(GV->hasInitializer() && "This global doesn't need space");
327 assert(!GV->getInitializer()->isNullValue() &&
328 "initialized global has zero initializer");
329 assert(GV->getType()->getAddressSpace() == PIC16ISD::RAM_SPACE &&
330 "can be used for initialized RAM data only");
331
332 std::string Name;
333 // If address is given then create a section at that address else create a
334 // section by section name specified in GV.
335 PIC16Section *FoundIDATASec = NULL;
336 if (Addr.empty()) {
337 Name = GV->getSection() + " IDATA";
338 for (unsigned i = 0; i < IDATASections.size(); i++) {
339 if (IDATASections[i]->S_->getName() == Name) {
340 FoundIDATASec = IDATASections[i];
341 break;
342 }
343 }
344 } else {
345 std::string Prefix = GV->getNameStr() + "." + Addr + ".";
346 Name = PAN::getIdataSectionName(IDATASections.size(), Prefix) + " " + Addr;
347 }
348
349 PIC16Section *NewIDATASec = FoundIDATASec;
350 if (NewIDATASec == NULL) {
Chris Lattnera87dea42009-07-31 18:48:30 +0000351 const MCSection *NewSection = getOrCreateSection(Name.c_str(), false,
Chris Lattnerf0144122009-07-28 03:13:23 +0000352 // FIXME:
353 SectionKind::Metadata);
354 NewIDATASec = new PIC16Section(NewSection);
355 IDATASections.push_back(NewIDATASec);
356 }
357 // Insert the GV into this IDATA Section.
358 NewIDATASec->Items.push_back(GV);
359 // We do not want to put any GV without explicit section into this section
360 // so set its size to DatabankSize.
361 NewIDATASec->Size = DataBankSize;
362 return NewIDATASec->S_;
363}
364
365// Create a section in rom for a variable.
Chris Lattnera87dea42009-07-31 18:48:30 +0000366const MCSection *
Chris Lattnerf0144122009-07-28 03:13:23 +0000367PIC16TargetObjectFile::CreateROSectionForGlobal(const GlobalVariable *GV,
368 std::string Addr) const {
369 assert(GV->getType()->getAddressSpace() == PIC16ISD::ROM_SPACE &&
370 "can be used for ROM data only");
371
372 std::string Name;
373 // If address is given then create a section at that address else create a
374 // section by section name specified in GV.
375 PIC16Section *FoundROSec = NULL;
376 if (Addr.empty()) {
377 Name = GV->getSection() + " ROMDATA";
378 for (unsigned i = 1; i < ROSections.size(); i++) {
379 if (ROSections[i]->S_->getName() == Name) {
380 FoundROSec = ROSections[i];
381 break;
382 }
383 }
384 } else {
385 std::string Prefix = GV->getNameStr() + "." + Addr + ".";
386 Name = PAN::getRomdataSectionName(ROSections.size(), Prefix) + " " + Addr;
387 }
388
389 PIC16Section *NewRomSec = FoundROSec;
390 if (NewRomSec == NULL) {
Chris Lattnera87dea42009-07-31 18:48:30 +0000391 const MCSection *NewSection = getOrCreateSection(Name.c_str(), false,
392 SectionKind::ReadOnly);
Chris Lattnerf0144122009-07-28 03:13:23 +0000393 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