blob: 295188f0b414d2cd52216ce960ae1b372ac0ca1c [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 Gupta0e687712008-05-13 09:02:57 +000048}
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +000049
Sanjiv Gupta3b020fe2009-02-02 16:53:06 +000050const char *PIC16TargetAsmInfo::getRomDirective(unsigned size) const
51{
52 if (size == 8)
53 return RomData8bitsDirective;
54 else if (size == 16)
55 return RomData16bitsDirective;
56 else if (size == 32)
57 return RomData32bitsDirective;
58 else
59 return NULL;
60}
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +000061
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +000062
Sanjiv Gupta3b020fe2009-02-02 16:53:06 +000063const char *PIC16TargetAsmInfo::getASDirective(unsigned size,
64 unsigned AS) const {
65 if (AS == PIC16ISD::ROM_SPACE)
66 return getRomDirective(size);
67 else
68 return NULL;
69}
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +000070
Sanjiv Guptad8d27f42009-05-06 08:02:01 +000071const Section *
72PIC16TargetAsmInfo::getBSSSectionForGlobal(const GlobalVariable *GV) const {
73 assert (GV->hasInitializer() && "This global doesn't need space");
74 Constant *C = GV->getInitializer();
75 assert (C->isNullValue() && "Unitialized globals has non-zero initializer");
76
77 // Find how much space this global needs.
78 const TargetData *TD = TM.getTargetData();
79 const Type *Ty = C->getType();
Duncan Sands777d2302009-05-09 07:06:46 +000080 unsigned ValSize = TD->getTypeAllocSize(Ty);
Sanjiv Guptad8d27f42009-05-06 08:02:01 +000081
82 // Go through all BSS Sections and assign this variable
83 // to the first available section having enough space.
84 PIC16Section *FoundBSS = NULL;
85 for (unsigned i = 0; i < BSSSections.size(); i++) {
86 if (DataBankSize - BSSSections[i]->Size >= ValSize) {
87 FoundBSS = BSSSections[i];
88 break;
89 }
90 }
91
92 // No BSS section spacious enough was found. Crate a new one.
93 if (! FoundBSS) {
Sanjiv Gupta211f3622009-05-10 05:23:47 +000094 std::string name = PAN::getUdataSectionName(BSSSections.size());
95 const Section *NewSection = getNamedSection (name.c_str());
Sanjiv Guptad8d27f42009-05-06 08:02:01 +000096
97 FoundBSS = new PIC16Section(NewSection);
98
99 // Add this newly created BSS section to the list of BSSSections.
100 BSSSections.push_back(FoundBSS);
101 }
102
103 // Insert the GV into this BSS.
104 FoundBSS->Items.push_back(GV);
105 FoundBSS->Size += ValSize;
106
107 // We can't do this here because GV is const .
108 // const std::string SName = FoundBSS->S_->getName();
109 // GV->setSection(SName);
110
111 return FoundBSS->S_;
112}
113
114const Section *
115PIC16TargetAsmInfo::getIDATASectionForGlobal(const GlobalVariable *GV) const {
116 assert (GV->hasInitializer() && "This global doesn't need space");
117 Constant *C = GV->getInitializer();
118 assert (!C->isNullValue() && "initialized globals has zero initializer");
119 assert (GV->getType()->getAddressSpace() == PIC16ISD::RAM_SPACE &&
120 "can split initialized RAM data only");
121
122 // Find how much space this global needs.
123 const TargetData *TD = TM.getTargetData();
124 const Type *Ty = C->getType();
Duncan Sands777d2302009-05-09 07:06:46 +0000125 unsigned ValSize = TD->getTypeAllocSize(Ty);
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000126
127 // Go through all IDATA Sections and assign this variable
128 // to the first available section having enough space.
129 PIC16Section *FoundIDATA = NULL;
130 for (unsigned i = 0; i < IDATASections.size(); i++) {
131 if ( DataBankSize - IDATASections[i]->Size >= ValSize) {
132 FoundIDATA = IDATASections[i];
133 break;
134 }
135 }
136
137 // No IDATA section spacious enough was found. Crate a new one.
138 if (! FoundIDATA) {
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000139 std::string name = PAN::getIdataSectionName(IDATASections.size());
140 const Section *NewSection = getNamedSection (name.c_str());
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000141
142 FoundIDATA = new PIC16Section(NewSection);
143
144 // Add this newly created IDATA section to the list of IDATASections.
145 IDATASections.push_back(FoundIDATA);
146 }
147
148 // Insert the GV into this IDATA.
149 FoundIDATA->Items.push_back(GV);
150 FoundIDATA->Size += ValSize;
151
152 // We can't do this here because GV is const .
153 // GV->setSection(FoundIDATA->S->getName());
154
155 return FoundIDATA->S_;
156}
157
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000158// Get the section for an automatic variable of a function.
159// For PIC16 they are globals only with mangled names.
160const Section *
161PIC16TargetAsmInfo::getSectionForAuto(const GlobalVariable *GV) const {
162
163 const std::string name = PAN::getSectionNameForSym(GV->getName());
164
165 // Go through all Auto Sections and assign this variable
166 // to the appropriate section.
167 PIC16Section *FoundAutoSec = NULL;
168 for (unsigned i = 0; i < AutosSections.size(); i++) {
169 if ( AutosSections[i]->S_->getName() == name) {
170 FoundAutoSec = AutosSections[i];
171 break;
172 }
173 }
174
175 // No Auto section was found. Crate a new one.
176 if (! FoundAutoSec) {
177 const Section *NewSection = getNamedSection (name.c_str());
178
179 FoundAutoSec = new PIC16Section(NewSection);
180
181 // Add this newly created autos section to the list of AutosSections.
182 AutosSections.push_back(FoundAutoSec);
183 }
184
185 // Insert the auto into this section.
186 FoundAutoSec->Items.push_back(GV);
187
188 return FoundAutoSec->S_;
189}
190
191
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000192// Override default implementation to put the true globals into
193// multiple data sections if required.
194const Section*
195PIC16TargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV1) const {
196 // We select the section based on the initializer here, so it really
197 // has to be a GlobalVariable.
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000198 const GlobalVariable *GV = dyn_cast<GlobalVariable>(GV1);
199 if (!GV1 || ! GV->hasInitializer())
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000200 return TargetAsmInfo::SelectSectionForGlobal(GV1);
201
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000202 // First, if this is an automatic variable for a function, get the section
203 // name for it and return.
204 const std::string name = GV->getName();
205 if (PAN::isLocalName(name)) {
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000206 return getSectionForAuto(GV);
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000207 }
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000208
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000209 // See if this is an uninitialized global.
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000210 const Constant *C = GV->getInitializer();
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000211 if (C->isNullValue())
212 return getBSSSectionForGlobal(GV);
213
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000214 // If this is initialized data in RAM. Put it in the correct IDATA section.
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000215 if (GV->getType()->getAddressSpace() == PIC16ISD::RAM_SPACE)
216 return getIDATASectionForGlobal(GV);
217
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000218 // This is initialized data in rom, put it in the readonly section.
219 if (GV->getType()->getAddressSpace() == PIC16ISD::ROM_SPACE) {
220 ROSection->Items.push_back(GV);
221 return ROSection->S_;
222 }
223
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000224 // Else let the default implementation take care of it.
225 return TargetAsmInfo::SelectSectionForGlobal(GV);
226}
227
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000228PIC16TargetAsmInfo::~PIC16TargetAsmInfo() {
229
230 for (unsigned i = 0; i < BSSSections.size(); i++) {
231 delete BSSSections[i];
232 }
233
234 for (unsigned i = 0; i < IDATASections.size(); i++) {
235 delete IDATASections[i];
236 }
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000237
238 for (unsigned i = 0; i < AutosSections.size(); i++) {
239 delete AutosSections[i];
240 }
241
242 delete ROSection;
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000243}