1 //===-- PIC16TargetAsmInfo.cpp - PIC16 asm properties ---------------------===//
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 // This file contains the declarations of the PIC16TargetAsmInfo properties.
12 //===----------------------------------------------------------------------===//
14 #include "PIC16TargetAsmInfo.h"
15 #include "PIC16TargetMachine.h"
16 #include "llvm/GlobalValue.h"
17 #include "llvm/GlobalVariable.h"
18 #include "llvm/DerivedTypes.h"
23 PIC16TargetAsmInfo(const PIC16TargetMachine &TM)
26 GlobalPrefix = PAN::getTagName(PAN::PREFIX_SYMBOL);
27 GlobalDirective = "\tglobal\t";
28 ExternDirective = "\textern\t";
30 Data8bitsDirective = " db ";
31 Data16bitsDirective = " dw ";
32 Data32bitsDirective = " dl ";
33 Data64bitsDirective = NULL;
34 RomData8bitsDirective = " dw ";
35 RomData16bitsDirective = " rom_di ";
36 RomData32bitsDirective = " rom_dl ";
38 AsciiDirective = " dt ";
39 AscizDirective = NULL;
40 BSSSection_ = getNamedSection("udata.# UDATA",
41 SectionFlags::Writable | SectionFlags::BSS);
42 ReadOnlySection = getNamedSection("romdata.# ROMDATA", SectionFlags::None);
43 DataSection = getNamedSection("idata.# IDATA", SectionFlags::Writable);
44 SwitchToSectionDirective = "";
45 // Need because otherwise a .text symbol is emitted by DwarfWriter
46 // in BeginModule, and gpasm cribbs for that .text symbol.
47 TextSection = getUnnamedSection("", SectionFlags::Code);
48 PIC16Section *ROSection = new PIC16Section(getReadOnlySection());
49 ROSections.push_back(ROSection);
50 ExternalVarDecls = new PIC16Section(getNamedSection("ExternalVarDecls"));
51 ExternalVarDefs = new PIC16Section(getNamedSection("ExternalVarDefs"));
52 // Set it to false because we weed to generate c file name and not bc file
54 HasSingleParameterDotFile = false;
57 const char *PIC16TargetAsmInfo::getRomDirective(unsigned Size) const {
59 case 8: return RomData8bitsDirective;
60 case 16: return RomData16bitsDirective;
61 case 32: return RomData32bitsDirective;
67 const char *PIC16TargetAsmInfo::
68 getDataASDirective(unsigned Size, unsigned AS) const {
69 if (AS == PIC16ISD::ROM_SPACE)
70 return getRomDirective(Size);
75 PIC16TargetAsmInfo::getBSSSectionForGlobal(const GlobalVariable *GV) const {
76 assert(GV->hasInitializer() && "This global doesn't need space");
77 Constant *C = GV->getInitializer();
78 assert(C->isNullValue() && "Unitialized globals has non-zero initializer");
80 // Find how much space this global needs.
81 const TargetData *TD = TM.getTargetData();
82 const Type *Ty = C->getType();
83 unsigned ValSize = TD->getTypeAllocSize(Ty);
85 // Go through all BSS Sections and assign this variable
86 // to the first available section having enough space.
87 PIC16Section *FoundBSS = NULL;
88 for (unsigned i = 0; i < BSSSections.size(); i++) {
89 if (DataBankSize - BSSSections[i]->Size >= ValSize) {
90 FoundBSS = BSSSections[i];
95 // No BSS section spacious enough was found. Crate a new one.
97 std::string name = PAN::getUdataSectionName(BSSSections.size());
98 const Section *NewSection = getNamedSection(name.c_str());
100 FoundBSS = new PIC16Section(NewSection);
102 // Add this newly created BSS section to the list of BSSSections.
103 BSSSections.push_back(FoundBSS);
106 // Insert the GV into this BSS.
107 FoundBSS->Items.push_back(GV);
108 FoundBSS->Size += ValSize;
113 PIC16TargetAsmInfo::getIDATASectionForGlobal(const GlobalVariable *GV) const {
114 assert(GV->hasInitializer() && "This global doesn't need space");
115 Constant *C = GV->getInitializer();
116 assert(!C->isNullValue() && "initialized globals has zero initializer");
117 assert(GV->getType()->getAddressSpace() == PIC16ISD::RAM_SPACE &&
118 "can split initialized RAM data only");
120 // Find how much space this global needs.
121 const TargetData *TD = TM.getTargetData();
122 const Type *Ty = C->getType();
123 unsigned ValSize = TD->getTypeAllocSize(Ty);
125 // Go through all IDATA Sections and assign this variable
126 // to the first available section having enough space.
127 PIC16Section *FoundIDATA = NULL;
128 for (unsigned i = 0; i < IDATASections.size(); i++) {
129 if (DataBankSize - IDATASections[i]->Size >= ValSize) {
130 FoundIDATA = IDATASections[i];
135 // No IDATA section spacious enough was found. Crate a new one.
137 std::string name = PAN::getIdataSectionName(IDATASections.size());
138 const Section *NewSection = getNamedSection(name.c_str());
140 FoundIDATA = new PIC16Section(NewSection);
142 // Add this newly created IDATA section to the list of IDATASections.
143 IDATASections.push_back(FoundIDATA);
146 // Insert the GV into this IDATA.
147 FoundIDATA->Items.push_back(GV);
148 FoundIDATA->Size += ValSize;
149 return FoundIDATA->S_;
152 // Get the section for an automatic variable of a function.
153 // For PIC16 they are globals only with mangled names.
155 PIC16TargetAsmInfo::getSectionForAuto(const GlobalVariable *GV) const {
157 const std::string name = PAN::getSectionNameForSym(GV->getName());
159 // Go through all Auto Sections and assign this variable
160 // to the appropriate section.
161 PIC16Section *FoundAutoSec = NULL;
162 for (unsigned i = 0; i < AutosSections.size(); i++) {
163 if (AutosSections[i]->S_->getName() == name) {
164 FoundAutoSec = AutosSections[i];
169 // No Auto section was found. Crate a new one.
171 const Section *NewSection = getNamedSection(name.c_str());
173 FoundAutoSec = new PIC16Section(NewSection);
175 // Add this newly created autos section to the list of AutosSections.
176 AutosSections.push_back(FoundAutoSec);
179 // Insert the auto into this section.
180 FoundAutoSec->Items.push_back(GV);
182 return FoundAutoSec->S_;
186 // Override default implementation to put the true globals into
187 // multiple data sections if required.
189 PIC16TargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV1,
190 SectionKind::Kind Kind) const {
191 // We select the section based on the initializer here, so it really
192 // has to be a GlobalVariable.
193 const GlobalVariable *GV = dyn_cast<GlobalVariable>(GV1);
195 return TargetAsmInfo::SelectSectionForGlobal(GV1, Kind);
197 // Record External Var Decls.
198 if (GV->isDeclaration()) {
199 ExternalVarDecls->Items.push_back(GV);
200 return ExternalVarDecls->S_;
203 assert(GV->hasInitializer() && "A def without initializer?");
205 // First, if this is an automatic variable for a function, get the section
206 // name for it and return.
207 std::string name = GV->getName();
208 if (PAN::isLocalName(name))
209 return getSectionForAuto(GV);
211 // Record Exteranl Var Defs.
212 if (GV->hasExternalLinkage() || GV->hasCommonLinkage())
213 ExternalVarDefs->Items.push_back(GV);
215 // See if this is an uninitialized global.
216 const Constant *C = GV->getInitializer();
217 if (C->isNullValue())
218 return getBSSSectionForGlobal(GV);
220 // If this is initialized data in RAM. Put it in the correct IDATA section.
221 if (GV->getType()->getAddressSpace() == PIC16ISD::RAM_SPACE)
222 return getIDATASectionForGlobal(GV);
224 // This is initialized data in rom, put it in the readonly section.
225 if (GV->getType()->getAddressSpace() == PIC16ISD::ROM_SPACE)
226 return getROSectionForGlobal(GV);
228 // Else let the default implementation take care of it.
229 return TargetAsmInfo::SelectSectionForGlobal(GV, Kind);
232 PIC16TargetAsmInfo::~PIC16TargetAsmInfo() {
233 for (unsigned i = 0; i < BSSSections.size(); i++)
234 delete BSSSections[i];
235 for (unsigned i = 0; i < IDATASections.size(); i++)
236 delete IDATASections[i];
237 for (unsigned i = 0; i < AutosSections.size(); i++)
238 delete AutosSections[i];
239 for (unsigned i = 0; i < ROSections.size(); i++)
240 delete ROSections[i];
241 delete ExternalVarDecls;
242 delete ExternalVarDefs;
246 /// getSpecialCasedSectionGlobals - Allow the target to completely override
247 /// section assignment of a global.
249 PIC16TargetAsmInfo::getSpecialCasedSectionGlobals(const GlobalValue *GV,
250 SectionKind::Kind Kind) const{
251 // If GV has a sectin name or section address create that section now.
252 if (GV->hasSection()) {
253 if (const GlobalVariable *GVar = cast<GlobalVariable>(GV)) {
254 std::string SectName = GVar->getSection();
255 // If address for a variable is specified, get the address and create
257 std::string AddrStr = "Address=";
258 if (SectName.compare(0, AddrStr.length(), AddrStr) == 0) {
259 std::string SectAddr = SectName.substr(AddrStr.length());
260 return CreateSectionForGlobal(GVar, SectAddr);
268 // Create a new section for global variable. If Addr is given then create
269 // section at that address else create by name.
271 PIC16TargetAsmInfo::CreateSectionForGlobal(const GlobalVariable *GV,
272 const std::string &Addr) const {
273 // See if this is an uninitialized global.
274 const Constant *C = GV->getInitializer();
275 if (C->isNullValue())
276 return CreateBSSSectionForGlobal(GV, Addr);
278 // If this is initialized data in RAM. Put it in the correct IDATA section.
279 if (GV->getType()->getAddressSpace() == PIC16ISD::RAM_SPACE)
280 return CreateIDATASectionForGlobal(GV, Addr);
282 // This is initialized data in rom, put it in the readonly section.
283 if (GV->getType()->getAddressSpace() == PIC16ISD::ROM_SPACE)
284 return CreateROSectionForGlobal(GV, Addr);
286 // Else let the default implementation take care of it.
287 return TargetAsmInfo::SectionForGlobal(GV);
290 // Create uninitialized section for a variable.
292 PIC16TargetAsmInfo::CreateBSSSectionForGlobal(const GlobalVariable *GV,
293 std::string Addr) const {
294 assert(GV->hasInitializer() && "This global doesn't need space");
295 assert(GV->getInitializer()->isNullValue() &&
296 "Unitialized global has non-zero initializer");
298 // If address is given then create a section at that address else create a
299 // section by section name specified in GV.
300 PIC16Section *FoundBSS = NULL;
302 Name = GV->getSection() + " UDATA";
303 for (unsigned i = 0; i < BSSSections.size(); i++) {
304 if (BSSSections[i]->S_->getName() == Name) {
305 FoundBSS = BSSSections[i];
310 std::string Prefix = GV->getNameStr() + "." + Addr + ".";
311 Name = PAN::getUdataSectionName(BSSSections.size(), Prefix) + " " + Addr;
314 PIC16Section *NewBSS = FoundBSS;
315 if (NewBSS == NULL) {
316 const Section *NewSection = getNamedSection(Name.c_str());
317 NewBSS = new PIC16Section(NewSection);
318 BSSSections.push_back(NewBSS);
321 // Insert the GV into this BSS.
322 NewBSS->Items.push_back(GV);
324 // We do not want to put any GV without explicit section into this section
325 // so set its size to DatabankSize.
326 NewBSS->Size = DataBankSize;
330 // Get rom section for a variable. Currently there can be only one rom section
331 // unless a variable explicitly requests a section.
333 PIC16TargetAsmInfo::getROSectionForGlobal(const GlobalVariable *GV) const {
334 ROSections[0]->Items.push_back(GV);
335 return ROSections[0]->S_;
338 // Create initialized data section for a variable.
340 PIC16TargetAsmInfo::CreateIDATASectionForGlobal(const GlobalVariable *GV,
341 std::string Addr) const {
342 assert(GV->hasInitializer() && "This global doesn't need space");
343 assert(!GV->getInitializer()->isNullValue() &&
344 "initialized global has zero initializer");
345 assert(GV->getType()->getAddressSpace() == PIC16ISD::RAM_SPACE &&
346 "can be used for initialized RAM data only");
349 // If address is given then create a section at that address else create a
350 // section by section name specified in GV.
351 PIC16Section *FoundIDATASec = NULL;
353 Name = GV->getSection() + " IDATA";
354 for (unsigned i = 0; i < IDATASections.size(); i++) {
355 if (IDATASections[i]->S_->getName() == Name) {
356 FoundIDATASec = IDATASections[i];
361 std::string Prefix = GV->getNameStr() + "." + Addr + ".";
362 Name = PAN::getIdataSectionName(IDATASections.size(), Prefix) + " " + Addr;
365 PIC16Section *NewIDATASec = FoundIDATASec;
366 if (NewIDATASec == NULL) {
367 const Section *NewSection = getNamedSection(Name.c_str());
368 NewIDATASec = new PIC16Section(NewSection);
369 IDATASections.push_back(NewIDATASec);
371 // Insert the GV into this IDATA Section.
372 NewIDATASec->Items.push_back(GV);
373 // We do not want to put any GV without explicit section into this section
374 // so set its size to DatabankSize.
375 NewIDATASec->Size = DataBankSize;
376 return NewIDATASec->S_;
379 // Create a section in rom for a variable.
381 PIC16TargetAsmInfo::CreateROSectionForGlobal(const GlobalVariable *GV,
382 std::string Addr) const {
383 assert(GV->getType()->getAddressSpace() == PIC16ISD::ROM_SPACE &&
384 "can be used for ROM data only");
387 // If address is given then create a section at that address else create a
388 // section by section name specified in GV.
389 PIC16Section *FoundROSec = NULL;
391 Name = GV->getSection() + " ROMDATA";
392 for (unsigned i = 1; i < ROSections.size(); i++) {
393 if (ROSections[i]->S_->getName() == Name) {
394 FoundROSec = ROSections[i];
399 std::string Prefix = GV->getNameStr() + "." + Addr + ".";
400 Name = PAN::getRomdataSectionName(ROSections.size(), Prefix) + " " + Addr;
403 PIC16Section *NewRomSec = FoundROSec;
404 if (NewRomSec == NULL) {
405 const Section *NewSection = getNamedSection(Name.c_str());
406 NewRomSec = new PIC16Section(NewSection);
407 ROSections.push_back(NewRomSec);
410 // Insert the GV into this ROM Section.
411 NewRomSec->Items.push_back(GV);
412 return NewRomSec->S_;