blob: aa865bfb50965ea6a36f9eec0f80bdb06cd90003 [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"
Chris Lattnerf7427e52009-08-08 21:37:01 +000011#include "PIC16Section.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000012#include "PIC16ISelLowering.h"
13#include "PIC16TargetMachine.h"
14#include "llvm/DerivedTypes.h"
15#include "llvm/Module.h"
Chris Lattnera87dea42009-07-31 18:48:30 +000016#include "llvm/MC/MCSection.h"
Chris Lattnerfbf1d272009-08-08 20:14:13 +000017#include "llvm/MC/MCContext.h"
Chris Lattner93b6db32009-08-08 23:39:42 +000018#include "llvm/Support/raw_ostream.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000019using namespace llvm;
20
Chris Lattner93b6db32009-08-08 23:39:42 +000021
22MCSectionPIC16 *MCSectionPIC16::Create(const StringRef &Name,
23 SectionKind K, MCContext &Ctx) {
Chris Lattner873bc4c2009-08-13 00:26:52 +000024 return new (Ctx) MCSectionPIC16(Name, K);
Chris Lattner93b6db32009-08-08 23:39:42 +000025}
26
27
28void MCSectionPIC16::PrintSwitchToSection(const TargetAsmInfo &TAI,
29 raw_ostream &OS) const {
30 OS << getName() << '\n';
31}
32
33
34
35
Daniel Dunbar967ce7f2009-08-02 01:25:15 +000036PIC16TargetObjectFile::PIC16TargetObjectFile()
Chris Lattner759b8882009-08-06 16:27:28 +000037 : ExternalVarDecls(0), ExternalVarDefs(0) {
Daniel Dunbar967ce7f2009-08-02 01:25:15 +000038}
39
Chris Lattner93b6db32009-08-08 23:39:42 +000040const MCSectionPIC16 *PIC16TargetObjectFile::
41getPIC16Section(const char *Name, SectionKind Kind) const {
Chris Lattner873bc4c2009-08-13 00:26:52 +000042 MCSectionPIC16 *&Entry = SectionsByName[Name];
43 if (Entry)
44 return Entry;
45
46 return Entry = MCSectionPIC16::Create(Name, Kind, getContext());
Chris Lattnerfbf1d272009-08-08 20:14:13 +000047}
48
49
Chris Lattnera87dea42009-07-31 18:48:30 +000050void PIC16TargetObjectFile::Initialize(MCContext &Ctx, const TargetMachine &tm){
51 TargetLoweringObjectFile::Initialize(Ctx, tm);
52 TM = &tm;
53
Chris Lattner93b6db32009-08-08 23:39:42 +000054 BSSSection = getPIC16Section("udata.# UDATA", SectionKind::getBSS());
55 ReadOnlySection = getPIC16Section("romdata.# ROMDATA",
Chris Lattner9aee1812009-08-08 20:23:47 +000056 SectionKind::getReadOnly());
Chris Lattner93b6db32009-08-08 23:39:42 +000057 DataSection = getPIC16Section("idata.# IDATA", SectionKind::getDataRel());
Chris Lattnerf0144122009-07-28 03:13:23 +000058
59 // Need because otherwise a .text symbol is emitted by DwarfWriter
60 // in BeginModule, and gpasm cribbs for that .text symbol.
Chris Lattner93b6db32009-08-08 23:39:42 +000061 TextSection = getPIC16Section("", SectionKind::getText());
Chris Lattnerf0144122009-07-28 03:13:23 +000062
Chris Lattner93b6db32009-08-08 23:39:42 +000063 ROSections.push_back(new PIC16Section((MCSectionPIC16*)ReadOnlySection));
Chris Lattnerf0144122009-07-28 03:13:23 +000064
65 // FIXME: I don't know what the classification of these sections really is.
Chris Lattner9aee1812009-08-08 20:23:47 +000066 ExternalVarDecls = new PIC16Section(getPIC16Section("ExternalVarDecls",
Chris Lattner1ef9be22009-08-02 00:02:44 +000067 SectionKind::getMetadata()));
Chris Lattner9aee1812009-08-08 20:23:47 +000068 ExternalVarDefs = new PIC16Section(getPIC16Section("ExternalVarDefs",
Chris Lattner1ef9be22009-08-02 00:02:44 +000069 SectionKind::getMetadata()));
Chris Lattnerf0144122009-07-28 03:13:23 +000070}
71
Chris Lattner759b8882009-08-06 16:27:28 +000072const MCSection *PIC16TargetObjectFile::
73getSectionForFunction(const std::string &FnName) const {
74 std::string T = PAN::getCodeSectionName(FnName);
Chris Lattner93b6db32009-08-08 23:39:42 +000075 return getPIC16Section(T.c_str(), SectionKind::getText());
Chris Lattner759b8882009-08-06 16:27:28 +000076}
77
78
79const MCSection *PIC16TargetObjectFile::
80getSectionForFunctionFrame(const std::string &FnName) const {
81 std::string T = PAN::getFrameSectionName(FnName);
Chris Lattner93b6db32009-08-08 23:39:42 +000082 return getPIC16Section(T.c_str(), SectionKind::getDataRel());
Chris Lattner759b8882009-08-06 16:27:28 +000083}
Chris Lattnerf0144122009-07-28 03:13:23 +000084
Chris Lattnera87dea42009-07-31 18:48:30 +000085const MCSection *
Chris Lattnerf0144122009-07-28 03:13:23 +000086PIC16TargetObjectFile::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");
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 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];
102 break;
103 }
104 }
105
106 // No BSS section spacious enough was found. Crate a new one.
107 if (!FoundBSS) {
108 std::string name = PAN::getUdataSectionName(BSSSections.size());
Chris Lattner93b6db32009-08-08 23:39:42 +0000109 const MCSectionPIC16 *NewSection
110 = getPIC16Section(name.c_str(), /*FIXME*/ SectionKind::getMetadata());
Chris Lattnerf0144122009-07-28 03:13:23 +0000111
112 FoundBSS = new PIC16Section(NewSection);
113
114 // Add this newly created BSS section to the list of BSSSections.
115 BSSSections.push_back(FoundBSS);
116 }
117
118 // Insert the GV into this BSS.
119 FoundBSS->Items.push_back(GV);
120 FoundBSS->Size += ValSize;
121 return FoundBSS->S_;
122}
123
Chris Lattnera87dea42009-07-31 18:48:30 +0000124const MCSection *
Chris Lattnerf0144122009-07-28 03:13:23 +0000125PIC16TargetObjectFile::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");
131
132 // Find how much space this global needs.
Chris Lattnera87dea42009-07-31 18:48:30 +0000133 const TargetData *TD = TM->getTargetData();
Chris Lattnerf0144122009-07-28 03:13:23 +0000134 const Type *Ty = C->getType();
135 unsigned ValSize = TD->getTypeAllocSize(Ty);
136
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];
143 break;
144 }
145 }
146
147 // No IDATA section spacious enough was found. Crate a new one.
148 if (!FoundIDATA) {
149 std::string name = PAN::getIdataSectionName(IDATASections.size());
Chris Lattner93b6db32009-08-08 23:39:42 +0000150 const MCSectionPIC16 *NewSection =
151 getPIC16Section(name.c_str(), /*FIXME*/ SectionKind::getMetadata());
Chris Lattnerf0144122009-07-28 03:13:23 +0000152
153 FoundIDATA = new PIC16Section(NewSection);
154
155 // Add this newly created IDATA section to the list of IDATASections.
156 IDATASections.push_back(FoundIDATA);
157 }
158
159 // Insert the GV into this IDATA.
160 FoundIDATA->Items.push_back(GV);
161 FoundIDATA->Size += ValSize;
162 return FoundIDATA->S_;
163}
164
165// Get the section for an automatic variable of a function.
166// For PIC16 they are globals only with mangled names.
Chris Lattnera87dea42009-07-31 18:48:30 +0000167const MCSection *
Chris Lattnerf0144122009-07-28 03:13:23 +0000168PIC16TargetObjectFile::getSectionForAuto(const GlobalVariable *GV) const {
169
170 const std::string name = PAN::getSectionNameForSym(GV->getName());
171
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];
178 break;
179 }
180 }
181
182 // No Auto section was found. Crate a new one.
183 if (!FoundAutoSec) {
Chris Lattner93b6db32009-08-08 23:39:42 +0000184 const MCSectionPIC16 *NewSection =
185 getPIC16Section(name.c_str(), /*FIXME*/ SectionKind::getMetadata());
Chris Lattnerf0144122009-07-28 03:13:23 +0000186
187 FoundAutoSec = new PIC16Section(NewSection);
188
189 // Add this newly created autos section to the list of AutosSections.
190 AutosSections.push_back(FoundAutoSec);
191 }
192
193 // Insert the auto into this section.
194 FoundAutoSec->Items.push_back(GV);
195
196 return FoundAutoSec->S_;
197}
198
199
200// Override default implementation to put the true globals into
201// multiple data sections if required.
Chris Lattnera87dea42009-07-31 18:48:30 +0000202const MCSection *
Chris Lattnerf0144122009-07-28 03:13:23 +0000203PIC16TargetObjectFile::SelectSectionForGlobal(const GlobalValue *GV1,
Chris Lattnerf9650c02009-08-01 21:46:23 +0000204 SectionKind Kind,
Chris Lattnere53a6002009-07-29 05:09:30 +0000205 Mangler *Mang,
Chris Lattnerf0144122009-07-28 03:13:23 +0000206 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);
210 if (!GV)
Chris Lattnerf9650c02009-08-01 21:46:23 +0000211 return TargetLoweringObjectFile::SelectSectionForGlobal(GV1, Kind, Mang,TM);
Chris Lattnerf0144122009-07-28 03:13:23 +0000212
213 // Record External Var Decls.
214 if (GV->isDeclaration()) {
215 ExternalVarDecls->Items.push_back(GV);
216 return ExternalVarDecls->S_;
217 }
218
219 assert(GV->hasInitializer() && "A def without initializer?");
220
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);
226
227 // Record Exteranl Var Defs.
228 if (GV->hasExternalLinkage() || GV->hasCommonLinkage())
229 ExternalVarDefs->Items.push_back(GV);
230
231 // See if this is an uninitialized global.
232 const Constant *C = GV->getInitializer();
233 if (C->isNullValue())
234 return getBSSSectionForGlobal(GV);
235
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);
239
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);
243
244 // Else let the default implementation take care of it.
Chris Lattnerf9650c02009-08-01 21:46:23 +0000245 return TargetLoweringObjectFile::SelectSectionForGlobal(GV, Kind, Mang,TM);
Chris Lattnerf0144122009-07-28 03:13:23 +0000246}
247
248PIC16TargetObjectFile::~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;
259}
260
261
262/// getSpecialCasedSectionGlobals - Allow the target to completely override
263/// section assignment of a global.
Chris Lattner24f654c2009-08-06 16:39:58 +0000264const MCSection *PIC16TargetObjectFile::
265getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
266 Mangler *Mang, const TargetMachine &TM) const {
267 assert(GV->hasSection());
268
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
272 // section.
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);
Chris Lattnerf0144122009-07-28 03:13:23 +0000277 }
Chris Lattner24f654c2009-08-06 16:39:58 +0000278
279 // Create the section specified with section attribute.
280 return CreateSectionForGlobal(GVar, Mang);
Chris Lattnerf0144122009-07-28 03:13:23 +0000281 }
282
Chris Lattner93b6db32009-08-08 23:39:42 +0000283 return getPIC16Section(GV->getSection().c_str(), Kind);
Chris Lattnerf0144122009-07-28 03:13:23 +0000284}
285
286// Create a new section for global variable. If Addr is given then create
287// section at that address else create by name.
Chris Lattnera87dea42009-07-31 18:48:30 +0000288const MCSection *
Chris Lattnerf0144122009-07-28 03:13:23 +0000289PIC16TargetObjectFile::CreateSectionForGlobal(const GlobalVariable *GV,
Chris Lattnere53a6002009-07-29 05:09:30 +0000290 Mangler *Mang,
Chris Lattnerf0144122009-07-28 03:13:23 +0000291 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);
296
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);
300
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);
304
305 // Else let the default implementation take care of it.
Chris Lattnera87dea42009-07-31 18:48:30 +0000306 return TargetLoweringObjectFile::SectionForGlobal(GV, Mang, *TM);
Chris Lattnerf0144122009-07-28 03:13:23 +0000307}
308
309// Create uninitialized section for a variable.
Chris Lattnera87dea42009-07-31 18:48:30 +0000310const MCSection *
Chris Lattnerf0144122009-07-28 03:13:23 +0000311PIC16TargetObjectFile::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");
316 std::string Name;
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;
320 if (Addr.empty()) {
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];
325 break;
326 }
327 }
328 } else {
329 std::string Prefix = GV->getNameStr() + "." + Addr + ".";
330 Name = PAN::getUdataSectionName(BSSSections.size(), Prefix) + " " + Addr;
331 }
332
333 PIC16Section *NewBSS = FoundBSS;
334 if (NewBSS == NULL) {
Chris Lattner93b6db32009-08-08 23:39:42 +0000335 const MCSectionPIC16 *NewSection =
336 getPIC16Section(Name.c_str(), SectionKind::getBSS());
Chris Lattnerf0144122009-07-28 03:13:23 +0000337 NewBSS = new PIC16Section(NewSection);
338 BSSSections.push_back(NewBSS);
339 }
340
341 // Insert the GV into this BSS.
342 NewBSS->Items.push_back(GV);
343
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;
347 return NewBSS->S_;
348}
349
350// Get rom section for a variable. Currently there can be only one rom section
351// unless a variable explicitly requests a section.
Chris Lattnera87dea42009-07-31 18:48:30 +0000352const MCSection *
Chris Lattnerf0144122009-07-28 03:13:23 +0000353PIC16TargetObjectFile::getROSectionForGlobal(const GlobalVariable *GV) const {
354 ROSections[0]->Items.push_back(GV);
355 return ROSections[0]->S_;
356}
357
358// Create initialized data section for a variable.
Chris Lattnera87dea42009-07-31 18:48:30 +0000359const MCSection *
Chris Lattnerf0144122009-07-28 03:13:23 +0000360PIC16TargetObjectFile::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");
367
368 std::string Name;
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;
372 if (Addr.empty()) {
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];
377 break;
378 }
379 }
380 } else {
381 std::string Prefix = GV->getNameStr() + "." + Addr + ".";
382 Name = PAN::getIdataSectionName(IDATASections.size(), Prefix) + " " + Addr;
383 }
384
385 PIC16Section *NewIDATASec = FoundIDATASec;
386 if (NewIDATASec == NULL) {
Chris Lattner93b6db32009-08-08 23:39:42 +0000387 const MCSectionPIC16 *NewSection =
388 getPIC16Section(Name.c_str(), /* FIXME */SectionKind::getMetadata());
Chris Lattnerf0144122009-07-28 03:13:23 +0000389 NewIDATASec = new PIC16Section(NewSection);
390 IDATASections.push_back(NewIDATASec);
391 }
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_;
398}
399
400// Create a section in rom for a variable.
Chris Lattnera87dea42009-07-31 18:48:30 +0000401const MCSection *
Chris Lattnerf0144122009-07-28 03:13:23 +0000402PIC16TargetObjectFile::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");
406
407 std::string Name;
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;
411 if (Addr.empty()) {
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];
416 break;
417 }
418 }
419 } else {
420 std::string Prefix = GV->getNameStr() + "." + Addr + ".";
421 Name = PAN::getRomdataSectionName(ROSections.size(), Prefix) + " " + Addr;
422 }
423
424 PIC16Section *NewRomSec = FoundROSec;
425 if (NewRomSec == NULL) {
Chris Lattner93b6db32009-08-08 23:39:42 +0000426 const MCSectionPIC16 *NewSection =
427 getPIC16Section(Name.c_str(), SectionKind::getReadOnly());
Chris Lattnerf0144122009-07-28 03:13:23 +0000428 NewRomSec = new PIC16Section(NewSection);
429 ROSections.push_back(NewRomSec);
430 }
431
432 // Insert the GV into this ROM Section.
433 NewRomSec->Items.push_back(GV);
434 return NewRomSec->S_;
435}
436