blob: 9d7592a2e12b160a44a5dc771dcdf55cb5c5a412 [file] [log] [blame]
Sanjiv Gupta0e687712008-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 Gohman8f092252008-11-03 18:22:42 +000015#include "PIC16TargetMachine.h"
Sanjiv Gupta1b046942009-01-13 19:18:47 +000016#include "llvm/GlobalValue.h"
Sanjiv Guptad8d27f42009-05-06 08:02:01 +000017#include "llvm/GlobalVariable.h"
18#include "llvm/DerivedTypes.h"
Sanjiv Gupta0e687712008-05-13 09:02:57 +000019
20using namespace llvm;
21
22PIC16TargetAsmInfo::
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000023PIC16TargetAsmInfo(const PIC16TargetMachine &TM)
Dan Gohman8f092252008-11-03 18:22:42 +000024 : TargetAsmInfo(TM) {
Sanjiv Gupta0e687712008-05-13 09:02:57 +000025 CommentString = ";";
Sanjiv Gupta211f3622009-05-10 05:23:47 +000026 GlobalPrefix = PAN::getTagName(PAN::PREFIX_SYMBOL);
27 GlobalDirective = "\tglobal\t";
28 ExternDirective = "\textern\t";
29
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000030 Data8bitsDirective = " db ";
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +000031 Data16bitsDirective = " dw ";
32 Data32bitsDirective = " dl ";
33 RomData8bitsDirective = " dw ";
34 RomData16bitsDirective = " rom_di ";
Sanjiv Gupta3b020fe2009-02-02 16:53:06 +000035 RomData32bitsDirective = " rom_dl ";
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000036 ZeroDirective = NULL;
Sanjiv Gupta1b046942009-01-13 19:18:47 +000037 AsciiDirective = " dt ";
38 AscizDirective = NULL;
39 BSSSection_ = getNamedSection("udata.# UDATA",
40 SectionFlags::Writeable | SectionFlags::BSS);
41 ReadOnlySection = getNamedSection("romdata.# ROMDATA", SectionFlags::None);
42 DataSection = getNamedSection("idata.# IDATA", SectionFlags::Writeable);
43 SwitchToSectionDirective = "";
Sanjiv Guptac1fa70c2009-04-08 06:24:04 +000044 // Need because otherwise a .text symbol is emitted by DwarfWriter
45 // in BeginModule, and gpasm cribbs for that .text symbol.
46 TextSection = getUnnamedSection("", SectionFlags::Code);
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +000047 ROSection = new PIC16Section(getReadOnlySection());
Sanjiv Guptaad6585b2009-05-13 15:13:17 +000048 ExternalVarDecls = new PIC16Section(getNamedSection("ExternalVarDecls"));
49 ExternalVarDefs = new PIC16Section(getNamedSection("ExternalVarDefs"));
Sanjiv Gupta0e687712008-05-13 09:02:57 +000050}
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +000051
Sanjiv Gupta3b020fe2009-02-02 16:53:06 +000052const char *PIC16TargetAsmInfo::getRomDirective(unsigned size) const
53{
54 if (size == 8)
55 return RomData8bitsDirective;
56 else if (size == 16)
57 return RomData16bitsDirective;
58 else if (size == 32)
59 return RomData32bitsDirective;
60 else
61 return NULL;
62}
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +000063
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +000064
Sanjiv Gupta3b020fe2009-02-02 16:53:06 +000065const char *PIC16TargetAsmInfo::getASDirective(unsigned size,
66 unsigned AS) const {
67 if (AS == PIC16ISD::ROM_SPACE)
68 return getRomDirective(size);
69 else
70 return NULL;
71}
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +000072
Sanjiv Guptad8d27f42009-05-06 08:02:01 +000073const Section *
74PIC16TargetAsmInfo::getBSSSectionForGlobal(const GlobalVariable *GV) const {
75 assert (GV->hasInitializer() && "This global doesn't need space");
76 Constant *C = GV->getInitializer();
77 assert (C->isNullValue() && "Unitialized globals has non-zero initializer");
78
79 // Find how much space this global needs.
80 const TargetData *TD = TM.getTargetData();
81 const Type *Ty = C->getType();
Duncan Sands777d2302009-05-09 07:06:46 +000082 unsigned ValSize = TD->getTypeAllocSize(Ty);
Sanjiv Guptad8d27f42009-05-06 08:02:01 +000083
84 // Go through all BSS Sections and assign this variable
85 // to the first available section having enough space.
86 PIC16Section *FoundBSS = NULL;
87 for (unsigned i = 0; i < BSSSections.size(); i++) {
88 if (DataBankSize - BSSSections[i]->Size >= ValSize) {
89 FoundBSS = BSSSections[i];
90 break;
91 }
92 }
93
94 // No BSS section spacious enough was found. Crate a new one.
95 if (! FoundBSS) {
Sanjiv Gupta211f3622009-05-10 05:23:47 +000096 std::string name = PAN::getUdataSectionName(BSSSections.size());
97 const Section *NewSection = getNamedSection (name.c_str());
Sanjiv Guptad8d27f42009-05-06 08:02:01 +000098
99 FoundBSS = new PIC16Section(NewSection);
100
101 // Add this newly created BSS section to the list of BSSSections.
102 BSSSections.push_back(FoundBSS);
103 }
104
105 // Insert the GV into this BSS.
106 FoundBSS->Items.push_back(GV);
107 FoundBSS->Size += ValSize;
108
109 // We can't do this here because GV is const .
110 // const std::string SName = FoundBSS->S_->getName();
111 // GV->setSection(SName);
112
113 return FoundBSS->S_;
114}
115
116const Section *
117PIC16TargetAsmInfo::getIDATASectionForGlobal(const GlobalVariable *GV) const {
118 assert (GV->hasInitializer() && "This global doesn't need space");
119 Constant *C = GV->getInitializer();
120 assert (!C->isNullValue() && "initialized globals has zero initializer");
121 assert (GV->getType()->getAddressSpace() == PIC16ISD::RAM_SPACE &&
122 "can split initialized RAM data only");
123
124 // Find how much space this global needs.
125 const TargetData *TD = TM.getTargetData();
126 const Type *Ty = C->getType();
Duncan Sands777d2302009-05-09 07:06:46 +0000127 unsigned ValSize = TD->getTypeAllocSize(Ty);
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000128
129 // Go through all IDATA Sections and assign this variable
130 // to the first available section having enough space.
131 PIC16Section *FoundIDATA = NULL;
132 for (unsigned i = 0; i < IDATASections.size(); i++) {
133 if ( DataBankSize - IDATASections[i]->Size >= ValSize) {
134 FoundIDATA = IDATASections[i];
135 break;
136 }
137 }
138
139 // No IDATA section spacious enough was found. Crate a new one.
140 if (! FoundIDATA) {
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000141 std::string name = PAN::getIdataSectionName(IDATASections.size());
142 const Section *NewSection = getNamedSection (name.c_str());
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000143
144 FoundIDATA = new PIC16Section(NewSection);
145
146 // Add this newly created IDATA section to the list of IDATASections.
147 IDATASections.push_back(FoundIDATA);
148 }
149
150 // Insert the GV into this IDATA.
151 FoundIDATA->Items.push_back(GV);
152 FoundIDATA->Size += ValSize;
153
154 // We can't do this here because GV is const .
155 // GV->setSection(FoundIDATA->S->getName());
156
157 return FoundIDATA->S_;
158}
159
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000160// Get the section for an automatic variable of a function.
161// For PIC16 they are globals only with mangled names.
162const Section *
163PIC16TargetAsmInfo::getSectionForAuto(const GlobalVariable *GV) const {
164
165 const std::string name = PAN::getSectionNameForSym(GV->getName());
166
167 // Go through all Auto Sections and assign this variable
168 // to the appropriate section.
169 PIC16Section *FoundAutoSec = NULL;
170 for (unsigned i = 0; i < AutosSections.size(); i++) {
171 if ( AutosSections[i]->S_->getName() == name) {
172 FoundAutoSec = AutosSections[i];
173 break;
174 }
175 }
176
177 // No Auto section was found. Crate a new one.
178 if (! FoundAutoSec) {
179 const Section *NewSection = getNamedSection (name.c_str());
180
181 FoundAutoSec = new PIC16Section(NewSection);
182
183 // Add this newly created autos section to the list of AutosSections.
184 AutosSections.push_back(FoundAutoSec);
185 }
186
187 // Insert the auto into this section.
188 FoundAutoSec->Items.push_back(GV);
189
190 return FoundAutoSec->S_;
191}
192
193
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000194// Override default implementation to put the true globals into
195// multiple data sections if required.
196const Section*
197PIC16TargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV1) const {
198 // We select the section based on the initializer here, so it really
199 // has to be a GlobalVariable.
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000200 const GlobalVariable *GV = dyn_cast<GlobalVariable>(GV1);
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000201
202 if (!GV)
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000203 return TargetAsmInfo::SelectSectionForGlobal(GV1);
204
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000205 // Record Exteranl Var Decls.
206 if (GV->isDeclaration()) {
207 ExternalVarDecls->Items.push_back(GV);
208 return ExternalVarDecls->S_;
209 }
210
211 assert (GV->hasInitializer() && "A def without initializer?");
212
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000213 // First, if this is an automatic variable for a function, get the section
214 // name for it and return.
215 const std::string name = GV->getName();
216 if (PAN::isLocalName(name)) {
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000217 return getSectionForAuto(GV);
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000218 }
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000219
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000220 // Record Exteranl Var Defs.
221 if (GV->hasExternalLinkage() || GV->hasCommonLinkage()) {
222 ExternalVarDefs->Items.push_back(GV);
223 }
224
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000225 // See if this is an uninitialized global.
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000226 const Constant *C = GV->getInitializer();
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000227 if (C->isNullValue())
228 return getBSSSectionForGlobal(GV);
229
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000230 // If this is initialized data in RAM. Put it in the correct IDATA section.
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000231 if (GV->getType()->getAddressSpace() == PIC16ISD::RAM_SPACE)
232 return getIDATASectionForGlobal(GV);
233
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000234 // This is initialized data in rom, put it in the readonly section.
235 if (GV->getType()->getAddressSpace() == PIC16ISD::ROM_SPACE) {
236 ROSection->Items.push_back(GV);
237 return ROSection->S_;
238 }
239
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000240 // Else let the default implementation take care of it.
241 return TargetAsmInfo::SelectSectionForGlobal(GV);
242}
243
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000244PIC16TargetAsmInfo::~PIC16TargetAsmInfo() {
245
246 for (unsigned i = 0; i < BSSSections.size(); i++) {
247 delete BSSSections[i];
248 }
249
250 for (unsigned i = 0; i < IDATASections.size(); i++) {
251 delete IDATASections[i];
252 }
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000253
254 for (unsigned i = 0; i < AutosSections.size(); i++) {
255 delete AutosSections[i];
256 }
257
258 delete ROSection;
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000259 delete ExternalVarDecls;
260 delete ExternalVarDefs;
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000261}