blob: 5a4387ae253afc05b945137a7e2a6a2b5553171b [file] [log] [blame]
Sanjiv Gupta09bb4202008-05-13 09:02:57 +00001//===-- PIC16TargetAsmInfo.cpp - PIC16 asm properties ---------------------===//
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// This file contains the declarations of the PIC16TargetAsmInfo properties.
11//
12//===----------------------------------------------------------------------===//
13
14#include "PIC16TargetAsmInfo.h"
Dan Gohmana004d7b2008-11-03 18:22:42 +000015#include "PIC16TargetMachine.h"
Sanjiv Gupta4affaea2009-01-13 19:18:47 +000016#include "llvm/GlobalValue.h"
Sanjiv Guptabb4a5c72009-05-06 08:02:01 +000017#include "llvm/GlobalVariable.h"
18#include "llvm/DerivedTypes.h"
Sanjiv Gupta09bb4202008-05-13 09:02:57 +000019
20using namespace llvm;
21
22PIC16TargetAsmInfo::
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +000023PIC16TargetAsmInfo(const PIC16TargetMachine &TM)
Dan Gohmana004d7b2008-11-03 18:22:42 +000024 : TargetAsmInfo(TM) {
Sanjiv Gupta09bb4202008-05-13 09:02:57 +000025 CommentString = ";";
Sanjiv Guptaca549b72009-05-10 05:23:47 +000026 GlobalPrefix = PAN::getTagName(PAN::PREFIX_SYMBOL);
27 GlobalDirective = "\tglobal\t";
28 ExternDirective = "\textern\t";
29
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +000030 Data8bitsDirective = " db ";
Sanjiv Guptadc2943d2009-01-30 04:25:10 +000031 Data16bitsDirective = " dw ";
32 Data32bitsDirective = " dl ";
Sanjiv Gupta9197df22009-07-06 18:09:11 +000033 Data64bitsDirective = NULL;
Sanjiv Guptadc2943d2009-01-30 04:25:10 +000034 RomData8bitsDirective = " dw ";
35 RomData16bitsDirective = " rom_di ";
Sanjiv Gupta46fc9fe2009-02-02 16:53:06 +000036 RomData32bitsDirective = " rom_dl ";
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +000037 ZeroDirective = NULL;
Sanjiv Gupta4affaea2009-01-13 19:18:47 +000038 AsciiDirective = " dt ";
39 AscizDirective = NULL;
Chris Lattner64630562009-07-27 06:17:14 +000040 BSSSection_ = getOrCreateSection("udata.# UDATA", false, SectionKind::BSS);
41 ReadOnlySection = getOrCreateSection("romdata.# ROMDATA", false,
42 SectionKind::ReadOnly);
43 DataSection = getOrCreateSection("idata.# IDATA", false,SectionKind::DataRel);
Sanjiv Gupta4affaea2009-01-13 19:18:47 +000044 SwitchToSectionDirective = "";
Sanjiv Gupta1a94cdc2009-04-08 06:24:04 +000045 // Need because otherwise a .text symbol is emitted by DwarfWriter
46 // in BeginModule, and gpasm cribbs for that .text symbol.
Chris Lattner64630562009-07-27 06:17:14 +000047 TextSection = getOrCreateSection("", true, SectionKind::Text);
Sanjiv Guptabb399cf2009-07-06 10:18:37 +000048 PIC16Section *ROSection = new PIC16Section(getReadOnlySection());
49 ROSections.push_back(ROSection);
Chris Lattnerd8310522009-07-27 05:32:16 +000050
51 // FIXME: I don't know what the classification of these sections really is.
Chris Lattner64630562009-07-27 06:17:14 +000052 ExternalVarDecls = new PIC16Section(getOrCreateSection("ExternalVarDecls",
53 false,
54 SectionKind::Metadata));
55 ExternalVarDefs = new PIC16Section(getOrCreateSection("ExternalVarDefs",
56 false,
57 SectionKind::Metadata));
Sanjiv Gupta14eba042009-05-28 18:24:11 +000058 // Set it to false because we weed to generate c file name and not bc file
59 // name.
60 HasSingleParameterDotFile = false;
Sanjiv Gupta09bb4202008-05-13 09:02:57 +000061}
Sanjiv Guptadc2943d2009-01-30 04:25:10 +000062
Chris Lattnerc8859c52009-07-20 17:12:46 +000063const char *PIC16TargetAsmInfo::getRomDirective(unsigned Size) const {
64 switch (Size) {
65 case 8: return RomData8bitsDirective;
66 case 16: return RomData16bitsDirective;
67 case 32: return RomData32bitsDirective;
68 default: return NULL;
69 }
Sanjiv Gupta46fc9fe2009-02-02 16:53:06 +000070}
Sanjiv Guptadc2943d2009-01-30 04:25:10 +000071
Sanjiv Guptadc2943d2009-01-30 04:25:10 +000072
Chris Lattnerc8859c52009-07-20 17:12:46 +000073const char *PIC16TargetAsmInfo::
74getDataASDirective(unsigned Size, unsigned AS) const {
Sanjiv Gupta46fc9fe2009-02-02 16:53:06 +000075 if (AS == PIC16ISD::ROM_SPACE)
Chris Lattnerc8859c52009-07-20 17:12:46 +000076 return getRomDirective(Size);
77 return NULL;
Sanjiv Gupta46fc9fe2009-02-02 16:53:06 +000078}
Sanjiv Guptadc2943d2009-01-30 04:25:10 +000079
Sanjiv Guptabb4a5c72009-05-06 08:02:01 +000080const Section *
81PIC16TargetAsmInfo::getBSSSectionForGlobal(const GlobalVariable *GV) const {
Chris Lattner4b4c52d2009-07-21 17:20:18 +000082 assert(GV->hasInitializer() && "This global doesn't need space");
Sanjiv Guptabb4a5c72009-05-06 08:02:01 +000083 Constant *C = GV->getInitializer();
Chris Lattner4b4c52d2009-07-21 17:20:18 +000084 assert(C->isNullValue() && "Unitialized globals has non-zero initializer");
Sanjiv Guptabb4a5c72009-05-06 08:02:01 +000085
86 // Find how much space this global needs.
87 const TargetData *TD = TM.getTargetData();
88 const Type *Ty = C->getType();
Duncan Sandsec4f97d2009-05-09 07:06:46 +000089 unsigned ValSize = TD->getTypeAllocSize(Ty);
Sanjiv Guptabb4a5c72009-05-06 08:02:01 +000090
91 // Go through all BSS Sections and assign this variable
92 // to the first available section having enough space.
93 PIC16Section *FoundBSS = NULL;
94 for (unsigned i = 0; i < BSSSections.size(); i++) {
95 if (DataBankSize - BSSSections[i]->Size >= ValSize) {
96 FoundBSS = BSSSections[i];
97 break;
98 }
99 }
100
101 // No BSS section spacious enough was found. Crate a new one.
Chris Lattner4b4c52d2009-07-21 17:20:18 +0000102 if (!FoundBSS) {
Sanjiv Guptaca549b72009-05-10 05:23:47 +0000103 std::string name = PAN::getUdataSectionName(BSSSections.size());
Chris Lattner64630562009-07-27 06:17:14 +0000104 const Section *NewSection = getOrCreateSection(name.c_str(), false,
105 // FIXME.
106 SectionKind::Metadata);
Sanjiv Guptabb4a5c72009-05-06 08:02:01 +0000107
108 FoundBSS = new PIC16Section(NewSection);
109
110 // Add this newly created BSS section to the list of BSSSections.
111 BSSSections.push_back(FoundBSS);
112 }
113
114 // Insert the GV into this BSS.
115 FoundBSS->Items.push_back(GV);
116 FoundBSS->Size += ValSize;
Sanjiv Guptabb4a5c72009-05-06 08:02:01 +0000117 return FoundBSS->S_;
118}
119
120const Section *
121PIC16TargetAsmInfo::getIDATASectionForGlobal(const GlobalVariable *GV) const {
Chris Lattner4b4c52d2009-07-21 17:20:18 +0000122 assert(GV->hasInitializer() && "This global doesn't need space");
Sanjiv Guptabb4a5c72009-05-06 08:02:01 +0000123 Constant *C = GV->getInitializer();
Chris Lattner4b4c52d2009-07-21 17:20:18 +0000124 assert(!C->isNullValue() && "initialized globals has zero initializer");
125 assert(GV->getType()->getAddressSpace() == PIC16ISD::RAM_SPACE &&
126 "can split initialized RAM data only");
Sanjiv Guptabb4a5c72009-05-06 08:02:01 +0000127
128 // Find how much space this global needs.
129 const TargetData *TD = TM.getTargetData();
130 const Type *Ty = C->getType();
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000131 unsigned ValSize = TD->getTypeAllocSize(Ty);
Sanjiv Guptabb4a5c72009-05-06 08:02:01 +0000132
133 // Go through all IDATA Sections and assign this variable
134 // to the first available section having enough space.
135 PIC16Section *FoundIDATA = NULL;
136 for (unsigned i = 0; i < IDATASections.size(); i++) {
Chris Lattner4b4c52d2009-07-21 17:20:18 +0000137 if (DataBankSize - IDATASections[i]->Size >= ValSize) {
Sanjiv Guptabb4a5c72009-05-06 08:02:01 +0000138 FoundIDATA = IDATASections[i];
139 break;
140 }
141 }
142
143 // No IDATA section spacious enough was found. Crate a new one.
Chris Lattner4b4c52d2009-07-21 17:20:18 +0000144 if (!FoundIDATA) {
Sanjiv Guptaca549b72009-05-10 05:23:47 +0000145 std::string name = PAN::getIdataSectionName(IDATASections.size());
Chris Lattner64630562009-07-27 06:17:14 +0000146 const Section *NewSection = getOrCreateSection(name.c_str(),
147 false,
148 // FIXME.
149 SectionKind::Metadata);
Sanjiv Guptabb4a5c72009-05-06 08:02:01 +0000150
151 FoundIDATA = new PIC16Section(NewSection);
152
153 // Add this newly created IDATA section to the list of IDATASections.
154 IDATASections.push_back(FoundIDATA);
155 }
156
157 // Insert the GV into this IDATA.
158 FoundIDATA->Items.push_back(GV);
159 FoundIDATA->Size += ValSize;
Sanjiv Guptabb4a5c72009-05-06 08:02:01 +0000160 return FoundIDATA->S_;
161}
162
Sanjiv Guptadbe79112009-05-12 17:07:27 +0000163// Get the section for an automatic variable of a function.
164// For PIC16 they are globals only with mangled names.
165const Section *
166PIC16TargetAsmInfo::getSectionForAuto(const GlobalVariable *GV) const {
167
168 const std::string name = PAN::getSectionNameForSym(GV->getName());
169
170 // Go through all Auto Sections and assign this variable
171 // to the appropriate section.
172 PIC16Section *FoundAutoSec = NULL;
173 for (unsigned i = 0; i < AutosSections.size(); i++) {
Chris Lattner4b4c52d2009-07-21 17:20:18 +0000174 if (AutosSections[i]->S_->getName() == name) {
Sanjiv Guptadbe79112009-05-12 17:07:27 +0000175 FoundAutoSec = AutosSections[i];
176 break;
177 }
178 }
179
180 // No Auto section was found. Crate a new one.
Chris Lattner4b4c52d2009-07-21 17:20:18 +0000181 if (!FoundAutoSec) {
Chris Lattner64630562009-07-27 06:17:14 +0000182 const Section *NewSection = getOrCreateSection(name.c_str(),
183 // FIXME.
184 false,
185 SectionKind::Metadata);
Sanjiv Guptadbe79112009-05-12 17:07:27 +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
Sanjiv Guptabb4a5c72009-05-06 08:02:01 +0000200// Override default implementation to put the true globals into
201// multiple data sections if required.
202const Section*
Chris Lattnerbb0c9bf2009-07-24 18:42:53 +0000203PIC16TargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV1,
Chris Lattnercc195212009-07-25 23:21:55 +0000204 SectionKind Kind) const {
Sanjiv Guptabb4a5c72009-05-06 08:02:01 +0000205 // We select the section based on the initializer here, so it really
206 // has to be a GlobalVariable.
Sanjiv Guptaca549b72009-05-10 05:23:47 +0000207 const GlobalVariable *GV = dyn_cast<GlobalVariable>(GV1);
Sanjiv Gupta5ade9e82009-05-13 15:13:17 +0000208 if (!GV)
Chris Lattnerbb0c9bf2009-07-24 18:42:53 +0000209 return TargetAsmInfo::SelectSectionForGlobal(GV1, Kind);
Sanjiv Guptabb4a5c72009-05-06 08:02:01 +0000210
Chris Lattnerbb0c9bf2009-07-24 18:42:53 +0000211 // Record External Var Decls.
Sanjiv Gupta5ade9e82009-05-13 15:13:17 +0000212 if (GV->isDeclaration()) {
213 ExternalVarDecls->Items.push_back(GV);
214 return ExternalVarDecls->S_;
215 }
216
Chris Lattner4b4c52d2009-07-21 17:20:18 +0000217 assert(GV->hasInitializer() && "A def without initializer?");
Sanjiv Gupta5ade9e82009-05-13 15:13:17 +0000218
Sanjiv Guptaca549b72009-05-10 05:23:47 +0000219 // First, if this is an automatic variable for a function, get the section
220 // name for it and return.
Chris Lattner0ad34562009-07-24 18:34:27 +0000221 std::string name = GV->getName();
222 if (PAN::isLocalName(name))
Sanjiv Guptadbe79112009-05-12 17:07:27 +0000223 return getSectionForAuto(GV);
Sanjiv Guptabb4a5c72009-05-06 08:02:01 +0000224
Sanjiv Gupta5ade9e82009-05-13 15:13:17 +0000225 // Record Exteranl Var Defs.
Chris Lattner0ad34562009-07-24 18:34:27 +0000226 if (GV->hasExternalLinkage() || GV->hasCommonLinkage())
Sanjiv Gupta5ade9e82009-05-13 15:13:17 +0000227 ExternalVarDefs->Items.push_back(GV);
Sanjiv Gupta5ade9e82009-05-13 15:13:17 +0000228
Sanjiv Guptabb4a5c72009-05-06 08:02:01 +0000229 // See if this is an uninitialized global.
Sanjiv Guptaca549b72009-05-10 05:23:47 +0000230 const Constant *C = GV->getInitializer();
Sanjiv Guptabb4a5c72009-05-06 08:02:01 +0000231 if (C->isNullValue())
232 return getBSSSectionForGlobal(GV);
233
Sanjiv Guptadbe79112009-05-12 17:07:27 +0000234 // If this is initialized data in RAM. Put it in the correct IDATA section.
Sanjiv Guptabb4a5c72009-05-06 08:02:01 +0000235 if (GV->getType()->getAddressSpace() == PIC16ISD::RAM_SPACE)
236 return getIDATASectionForGlobal(GV);
237
Sanjiv Guptadbe79112009-05-12 17:07:27 +0000238 // This is initialized data in rom, put it in the readonly section.
Sanjiv Guptabb399cf2009-07-06 10:18:37 +0000239 if (GV->getType()->getAddressSpace() == PIC16ISD::ROM_SPACE)
240 return getROSectionForGlobal(GV);
Sanjiv Guptadbe79112009-05-12 17:07:27 +0000241
Sanjiv Guptabb4a5c72009-05-06 08:02:01 +0000242 // Else let the default implementation take care of it.
Chris Lattnerbb0c9bf2009-07-24 18:42:53 +0000243 return TargetAsmInfo::SelectSectionForGlobal(GV, Kind);
Sanjiv Guptabb4a5c72009-05-06 08:02:01 +0000244}
245
Sanjiv Guptabb4a5c72009-05-06 08:02:01 +0000246PIC16TargetAsmInfo::~PIC16TargetAsmInfo() {
Chris Lattner523f3bc2009-07-20 22:23:48 +0000247 for (unsigned i = 0; i < BSSSections.size(); i++)
248 delete BSSSections[i];
249 for (unsigned i = 0; i < IDATASections.size(); i++)
250 delete IDATASections[i];
251 for (unsigned i = 0; i < AutosSections.size(); i++)
252 delete AutosSections[i];
253 for (unsigned i = 0; i < ROSections.size(); i++)
254 delete ROSections[i];
Sanjiv Gupta5ade9e82009-05-13 15:13:17 +0000255 delete ExternalVarDecls;
256 delete ExternalVarDefs;
Sanjiv Guptabb4a5c72009-05-06 08:02:01 +0000257}
Sanjiv Guptabb399cf2009-07-06 10:18:37 +0000258
Chris Lattner0ad34562009-07-24 18:34:27 +0000259
260/// getSpecialCasedSectionGlobals - Allow the target to completely override
261/// section assignment of a global.
262const Section *
263PIC16TargetAsmInfo::getSpecialCasedSectionGlobals(const GlobalValue *GV,
Chris Lattnercc195212009-07-25 23:21:55 +0000264 SectionKind Kind) const {
Sanjiv Guptabb399cf2009-07-06 10:18:37 +0000265 // If GV has a sectin name or section address create that section now.
266 if (GV->hasSection()) {
Chris Lattnerb3889492009-07-24 17:13:27 +0000267 if (const GlobalVariable *GVar = cast<GlobalVariable>(GV)) {
268 std::string SectName = GVar->getSection();
269 // If address for a variable is specified, get the address and create
270 // section.
271 std::string AddrStr = "Address=";
272 if (SectName.compare(0, AddrStr.length(), AddrStr) == 0) {
273 std::string SectAddr = SectName.substr(AddrStr.length());
274 return CreateSectionForGlobal(GVar, SectAddr);
275 }
Chris Lattner63acd9d2009-07-26 18:08:15 +0000276
277 // Create the section specified with section attribute.
278 return CreateSectionForGlobal(GVar);
Chris Lattner4fe5e142009-07-24 04:59:43 +0000279 }
Sanjiv Guptabb399cf2009-07-06 10:18:37 +0000280 }
Chris Lattner0ad34562009-07-24 18:34:27 +0000281
282 return 0;
Sanjiv Guptabb399cf2009-07-06 10:18:37 +0000283}
284
285// Create a new section for global variable. If Addr is given then create
286// section at that address else create by name.
287const Section *
Chris Lattnerb3889492009-07-24 17:13:27 +0000288PIC16TargetAsmInfo::CreateSectionForGlobal(const GlobalVariable *GV,
289 const std::string &Addr) const {
Sanjiv Guptabb399cf2009-07-06 10:18:37 +0000290 // See if this is an uninitialized global.
291 const Constant *C = GV->getInitializer();
292 if (C->isNullValue())
293 return CreateBSSSectionForGlobal(GV, Addr);
294
295 // If this is initialized data in RAM. Put it in the correct IDATA section.
296 if (GV->getType()->getAddressSpace() == PIC16ISD::RAM_SPACE)
297 return CreateIDATASectionForGlobal(GV, Addr);
298
299 // This is initialized data in rom, put it in the readonly section.
300 if (GV->getType()->getAddressSpace() == PIC16ISD::ROM_SPACE)
301 return CreateROSectionForGlobal(GV, Addr);
302
303 // Else let the default implementation take care of it.
304 return TargetAsmInfo::SectionForGlobal(GV);
305}
306
307// Create uninitialized section for a variable.
308const Section *
309PIC16TargetAsmInfo::CreateBSSSectionForGlobal(const GlobalVariable *GV,
310 std::string Addr) const {
Chris Lattner4b4c52d2009-07-21 17:20:18 +0000311 assert(GV->hasInitializer() && "This global doesn't need space");
312 assert(GV->getInitializer()->isNullValue() &&
313 "Unitialized global has non-zero initializer");
Sanjiv Guptabb399cf2009-07-06 10:18:37 +0000314 std::string Name;
315 // If address is given then create a section at that address else create a
316 // section by section name specified in GV.
317 PIC16Section *FoundBSS = NULL;
318 if (Addr.empty()) {
319 Name = GV->getSection() + " UDATA";
320 for (unsigned i = 0; i < BSSSections.size(); i++) {
321 if (BSSSections[i]->S_->getName() == Name) {
322 FoundBSS = BSSSections[i];
323 break;
324 }
325 }
Chris Lattner4b4c52d2009-07-21 17:20:18 +0000326 } else {
Daniel Dunbar1e13b972009-07-24 08:24:36 +0000327 std::string Prefix = GV->getNameStr() + "." + Addr + ".";
Sanjiv Guptabb399cf2009-07-06 10:18:37 +0000328 Name = PAN::getUdataSectionName(BSSSections.size(), Prefix) + " " + Addr;
329 }
330
331 PIC16Section *NewBSS = FoundBSS;
332 if (NewBSS == NULL) {
Chris Lattner64630562009-07-27 06:17:14 +0000333 const Section *NewSection = getOrCreateSection(Name.c_str(),
334 false, SectionKind::BSS);
Sanjiv Guptabb399cf2009-07-06 10:18:37 +0000335 NewBSS = new PIC16Section(NewSection);
336 BSSSections.push_back(NewBSS);
337 }
338
339 // Insert the GV into this BSS.
340 NewBSS->Items.push_back(GV);
341
342 // We do not want to put any GV without explicit section into this section
343 // so set its size to DatabankSize.
344 NewBSS->Size = DataBankSize;
345 return NewBSS->S_;
346}
347
348// Get rom section for a variable. Currently there can be only one rom section
349// unless a variable explicitly requests a section.
350const Section *
351PIC16TargetAsmInfo::getROSectionForGlobal(const GlobalVariable *GV) const {
352 ROSections[0]->Items.push_back(GV);
353 return ROSections[0]->S_;
354}
355
356// Create initialized data section for a variable.
357const Section *
358PIC16TargetAsmInfo::CreateIDATASectionForGlobal(const GlobalVariable *GV,
359 std::string Addr) const {
Chris Lattner4b4c52d2009-07-21 17:20:18 +0000360 assert(GV->hasInitializer() && "This global doesn't need space");
361 assert(!GV->getInitializer()->isNullValue() &&
362 "initialized global has zero initializer");
363 assert(GV->getType()->getAddressSpace() == PIC16ISD::RAM_SPACE &&
364 "can be used for initialized RAM data only");
Sanjiv Guptabb399cf2009-07-06 10:18:37 +0000365
366 std::string Name;
367 // If address is given then create a section at that address else create a
368 // section by section name specified in GV.
369 PIC16Section *FoundIDATASec = NULL;
370 if (Addr.empty()) {
371 Name = GV->getSection() + " IDATA";
372 for (unsigned i = 0; i < IDATASections.size(); i++) {
373 if (IDATASections[i]->S_->getName() == Name) {
374 FoundIDATASec = IDATASections[i];
375 break;
376 }
377 }
Chris Lattner4b4c52d2009-07-21 17:20:18 +0000378 } else {
Daniel Dunbar1e13b972009-07-24 08:24:36 +0000379 std::string Prefix = GV->getNameStr() + "." + Addr + ".";
Sanjiv Guptabb399cf2009-07-06 10:18:37 +0000380 Name = PAN::getIdataSectionName(IDATASections.size(), Prefix) + " " + Addr;
381 }
382
383 PIC16Section *NewIDATASec = FoundIDATASec;
384 if (NewIDATASec == NULL) {
Chris Lattner64630562009-07-27 06:17:14 +0000385 const Section *NewSection = getOrCreateSection(Name.c_str(),
386 false,
387 // FIXME:
388 SectionKind::Metadata);
Sanjiv Guptabb399cf2009-07-06 10:18:37 +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.
401const Section *
402PIC16TargetAsmInfo::CreateROSectionForGlobal(const GlobalVariable *GV,
403 std::string Addr) const {
Chris Lattner4b4c52d2009-07-21 17:20:18 +0000404 assert(GV->getType()->getAddressSpace() == PIC16ISD::ROM_SPACE &&
405 "can be used for ROM data only");
Sanjiv Guptabb399cf2009-07-06 10:18:37 +0000406
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 }
Chris Lattner4b4c52d2009-07-21 17:20:18 +0000419 } else {
Daniel Dunbar1e13b972009-07-24 08:24:36 +0000420 std::string Prefix = GV->getNameStr() + "." + Addr + ".";
Sanjiv Guptabb399cf2009-07-06 10:18:37 +0000421 Name = PAN::getRomdataSectionName(ROSections.size(), Prefix) + " " + Addr;
422 }
423
424 PIC16Section *NewRomSec = FoundROSec;
425 if (NewRomSec == NULL) {
Chris Lattner64630562009-07-27 06:17:14 +0000426 const Section *NewSection = getOrCreateSection(Name.c_str(),
427 false,
428 SectionKind::ReadOnly);
Sanjiv Guptabb399cf2009-07-06 10:18:37 +0000429 NewRomSec = new PIC16Section(NewSection);
430 ROSections.push_back(NewRomSec);
431 }
432
433 // Insert the GV into this ROM Section.
434 NewRomSec->Items.push_back(GV);
435 return NewRomSec->S_;
436}
437