Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 1 | //===-- 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 Lattner | c077621 | 2009-08-15 06:13:40 +0000 | [diff] [blame^] | 11 | #include "MCSectionPIC16.h" |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 12 | #include "PIC16ISelLowering.h" |
| 13 | #include "PIC16TargetMachine.h" |
| 14 | #include "llvm/DerivedTypes.h" |
| 15 | #include "llvm/Module.h" |
Chris Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 16 | #include "llvm/MC/MCSection.h" |
Chris Lattner | fbf1d27 | 2009-08-08 20:14:13 +0000 | [diff] [blame] | 17 | #include "llvm/MC/MCContext.h" |
Chris Lattner | 93b6db3 | 2009-08-08 23:39:42 +0000 | [diff] [blame] | 18 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 19 | using namespace llvm; |
| 20 | |
Chris Lattner | 93b6db3 | 2009-08-08 23:39:42 +0000 | [diff] [blame] | 21 | |
| 22 | MCSectionPIC16 *MCSectionPIC16::Create(const StringRef &Name, |
| 23 | SectionKind K, MCContext &Ctx) { |
Chris Lattner | 873bc4c | 2009-08-13 00:26:52 +0000 | [diff] [blame] | 24 | return new (Ctx) MCSectionPIC16(Name, K); |
Chris Lattner | 93b6db3 | 2009-08-08 23:39:42 +0000 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | |
| 28 | void MCSectionPIC16::PrintSwitchToSection(const TargetAsmInfo &TAI, |
| 29 | raw_ostream &OS) const { |
| 30 | OS << getName() << '\n'; |
| 31 | } |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
Daniel Dunbar | 967ce7f | 2009-08-02 01:25:15 +0000 | [diff] [blame] | 36 | PIC16TargetObjectFile::PIC16TargetObjectFile() |
Chris Lattner | 759b888 | 2009-08-06 16:27:28 +0000 | [diff] [blame] | 37 | : ExternalVarDecls(0), ExternalVarDefs(0) { |
Daniel Dunbar | 967ce7f | 2009-08-02 01:25:15 +0000 | [diff] [blame] | 38 | } |
| 39 | |
Chris Lattner | 93b6db3 | 2009-08-08 23:39:42 +0000 | [diff] [blame] | 40 | const MCSectionPIC16 *PIC16TargetObjectFile:: |
| 41 | getPIC16Section(const char *Name, SectionKind Kind) const { |
Chris Lattner | 873bc4c | 2009-08-13 00:26:52 +0000 | [diff] [blame] | 42 | MCSectionPIC16 *&Entry = SectionsByName[Name]; |
| 43 | if (Entry) |
| 44 | return Entry; |
| 45 | |
| 46 | return Entry = MCSectionPIC16::Create(Name, Kind, getContext()); |
Chris Lattner | fbf1d27 | 2009-08-08 20:14:13 +0000 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | |
Chris Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 50 | void PIC16TargetObjectFile::Initialize(MCContext &Ctx, const TargetMachine &tm){ |
| 51 | TargetLoweringObjectFile::Initialize(Ctx, tm); |
| 52 | TM = &tm; |
| 53 | |
Chris Lattner | 93b6db3 | 2009-08-08 23:39:42 +0000 | [diff] [blame] | 54 | BSSSection = getPIC16Section("udata.# UDATA", SectionKind::getBSS()); |
| 55 | ReadOnlySection = getPIC16Section("romdata.# ROMDATA", |
Chris Lattner | 9aee181 | 2009-08-08 20:23:47 +0000 | [diff] [blame] | 56 | SectionKind::getReadOnly()); |
Chris Lattner | 93b6db3 | 2009-08-08 23:39:42 +0000 | [diff] [blame] | 57 | DataSection = getPIC16Section("idata.# IDATA", SectionKind::getDataRel()); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 58 | |
| 59 | // Need because otherwise a .text symbol is emitted by DwarfWriter |
| 60 | // in BeginModule, and gpasm cribbs for that .text symbol. |
Chris Lattner | 93b6db3 | 2009-08-08 23:39:42 +0000 | [diff] [blame] | 61 | TextSection = getPIC16Section("", SectionKind::getText()); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 62 | |
Chris Lattner | 93b6db3 | 2009-08-08 23:39:42 +0000 | [diff] [blame] | 63 | ROSections.push_back(new PIC16Section((MCSectionPIC16*)ReadOnlySection)); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 64 | |
| 65 | // FIXME: I don't know what the classification of these sections really is. |
Chris Lattner | 9aee181 | 2009-08-08 20:23:47 +0000 | [diff] [blame] | 66 | ExternalVarDecls = new PIC16Section(getPIC16Section("ExternalVarDecls", |
Chris Lattner | 1ef9be2 | 2009-08-02 00:02:44 +0000 | [diff] [blame] | 67 | SectionKind::getMetadata())); |
Chris Lattner | 9aee181 | 2009-08-08 20:23:47 +0000 | [diff] [blame] | 68 | ExternalVarDefs = new PIC16Section(getPIC16Section("ExternalVarDefs", |
Chris Lattner | 1ef9be2 | 2009-08-02 00:02:44 +0000 | [diff] [blame] | 69 | SectionKind::getMetadata())); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Chris Lattner | 759b888 | 2009-08-06 16:27:28 +0000 | [diff] [blame] | 72 | const MCSection *PIC16TargetObjectFile:: |
| 73 | getSectionForFunction(const std::string &FnName) const { |
| 74 | std::string T = PAN::getCodeSectionName(FnName); |
Chris Lattner | 93b6db3 | 2009-08-08 23:39:42 +0000 | [diff] [blame] | 75 | return getPIC16Section(T.c_str(), SectionKind::getText()); |
Chris Lattner | 759b888 | 2009-08-06 16:27:28 +0000 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | |
| 79 | const MCSection *PIC16TargetObjectFile:: |
| 80 | getSectionForFunctionFrame(const std::string &FnName) const { |
| 81 | std::string T = PAN::getFrameSectionName(FnName); |
Chris Lattner | 93b6db3 | 2009-08-08 23:39:42 +0000 | [diff] [blame] | 82 | return getPIC16Section(T.c_str(), SectionKind::getDataRel()); |
Chris Lattner | 759b888 | 2009-08-06 16:27:28 +0000 | [diff] [blame] | 83 | } |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 84 | |
Chris Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 85 | const MCSection * |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 86 | PIC16TargetObjectFile::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 Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 92 | const TargetData *TD = TM->getTargetData(); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 93 | 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 Lattner | 93b6db3 | 2009-08-08 23:39:42 +0000 | [diff] [blame] | 109 | const MCSectionPIC16 *NewSection |
| 110 | = getPIC16Section(name.c_str(), /*FIXME*/ SectionKind::getMetadata()); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 111 | |
| 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 Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 124 | const MCSection * |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 125 | PIC16TargetObjectFile::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 Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 133 | const TargetData *TD = TM->getTargetData(); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 134 | 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 Lattner | 93b6db3 | 2009-08-08 23:39:42 +0000 | [diff] [blame] | 150 | const MCSectionPIC16 *NewSection = |
| 151 | getPIC16Section(name.c_str(), /*FIXME*/ SectionKind::getMetadata()); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 152 | |
| 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 Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 167 | const MCSection * |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 168 | PIC16TargetObjectFile::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 Lattner | 93b6db3 | 2009-08-08 23:39:42 +0000 | [diff] [blame] | 184 | const MCSectionPIC16 *NewSection = |
| 185 | getPIC16Section(name.c_str(), /*FIXME*/ SectionKind::getMetadata()); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 186 | |
| 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 Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 202 | const MCSection * |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 203 | PIC16TargetObjectFile::SelectSectionForGlobal(const GlobalValue *GV1, |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 204 | SectionKind Kind, |
Chris Lattner | e53a600 | 2009-07-29 05:09:30 +0000 | [diff] [blame] | 205 | Mangler *Mang, |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 206 | 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 Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 211 | return TargetLoweringObjectFile::SelectSectionForGlobal(GV1, Kind, Mang,TM); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 212 | |
| 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 Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 245 | return TargetLoweringObjectFile::SelectSectionForGlobal(GV, Kind, Mang,TM); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | PIC16TargetObjectFile::~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 Lattner | 24f654c | 2009-08-06 16:39:58 +0000 | [diff] [blame] | 264 | const MCSection *PIC16TargetObjectFile:: |
| 265 | getExplicitSectionGlobal(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 Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 277 | } |
Chris Lattner | 24f654c | 2009-08-06 16:39:58 +0000 | [diff] [blame] | 278 | |
| 279 | // Create the section specified with section attribute. |
| 280 | return CreateSectionForGlobal(GVar, Mang); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 281 | } |
| 282 | |
Chris Lattner | 93b6db3 | 2009-08-08 23:39:42 +0000 | [diff] [blame] | 283 | return getPIC16Section(GV->getSection().c_str(), Kind); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 284 | } |
| 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 Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 288 | const MCSection * |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 289 | PIC16TargetObjectFile::CreateSectionForGlobal(const GlobalVariable *GV, |
Chris Lattner | e53a600 | 2009-07-29 05:09:30 +0000 | [diff] [blame] | 290 | Mangler *Mang, |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 291 | 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 Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 306 | return TargetLoweringObjectFile::SectionForGlobal(GV, Mang, *TM); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | // Create uninitialized section for a variable. |
Chris Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 310 | const MCSection * |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 311 | PIC16TargetObjectFile::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 Lattner | 93b6db3 | 2009-08-08 23:39:42 +0000 | [diff] [blame] | 335 | const MCSectionPIC16 *NewSection = |
| 336 | getPIC16Section(Name.c_str(), SectionKind::getBSS()); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 337 | 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 Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 352 | const MCSection * |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 353 | PIC16TargetObjectFile::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 Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 359 | const MCSection * |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 360 | PIC16TargetObjectFile::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 Lattner | 93b6db3 | 2009-08-08 23:39:42 +0000 | [diff] [blame] | 387 | const MCSectionPIC16 *NewSection = |
| 388 | getPIC16Section(Name.c_str(), /* FIXME */SectionKind::getMetadata()); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 389 | 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 Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 401 | const MCSection * |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 402 | PIC16TargetObjectFile::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 Lattner | 93b6db3 | 2009-08-08 23:39:42 +0000 | [diff] [blame] | 426 | const MCSectionPIC16 *NewSection = |
| 427 | getPIC16Section(Name.c_str(), SectionKind::getReadOnly()); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 428 | 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 | |