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:: |
Sanjiv Gupta | eb01aba | 2009-08-20 19:28:24 +0000 | [diff] [blame] | 73 | getSectionForFunction(const std::string &FnName, bool isInterrupt) const { |
| 74 | std::string T = PAN::getCodeSectionName(FnName, isInterrupt); |
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 | |
Sanjiv Gupta | eb01aba | 2009-08-20 19:28:24 +0000 | [diff] [blame] | 199 | void PIC16TargetObjectFile::createClonedSectionForAutos(const std::string &SecName) { |
| 200 | |
| 201 | // If the function is cloned then it will have ".IL" in its name |
| 202 | // If this function is not cloned then return; |
| 203 | if (SecName.find(".IL") == std::string::npos) |
| 204 | return; |
| 205 | |
| 206 | // Come here if the function is cloned. |
| 207 | // Get the name of the original section from which it has been cloned. |
| 208 | std::string OrigName = SecName; |
| 209 | OrigName.replace(SecName.find(".IL"),3,""); |
| 210 | |
| 211 | // Find original section |
| 212 | PIC16Section *FoundAutoSec = NULL; |
| 213 | for (unsigned i = 0; i < AutosSections.size(); i++) { |
| 214 | if (AutosSections[i]->S_->getName() == OrigName) { |
| 215 | FoundAutoSec = AutosSections[i]; |
| 216 | break; |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | // No auto section exists for the original function. |
| 221 | if (!FoundAutoSec) |
| 222 | return; |
| 223 | |
| 224 | // Create new section for the cloned function |
| 225 | const MCSectionPIC16 *NewSection = |
| 226 | getPIC16Section(SecName.c_str(), SectionKind::getMetadata()); |
| 227 | |
| 228 | PIC16Section *NewAutoSec = new PIC16Section(NewSection); |
| 229 | // Add this newly created autos section to the list of AutosSections. |
| 230 | AutosSections.push_back(NewAutoSec); |
| 231 | |
| 232 | // Add the items from the original section to the new section |
| 233 | // Donot mangle them here. Because mangling them here will distort |
| 234 | // the original names. |
| 235 | // These names will be mangled them at the time of printing only |
| 236 | const std::vector<const GlobalVariable*> &Items = FoundAutoSec->Items; |
| 237 | for (unsigned j = 0; j < Items.size(); j++) { |
| 238 | NewAutoSec->Items.push_back(Items[j]); |
| 239 | } |
| 240 | } |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 241 | |
| 242 | // Override default implementation to put the true globals into |
| 243 | // multiple data sections if required. |
Chris Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 244 | const MCSection * |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 245 | PIC16TargetObjectFile::SelectSectionForGlobal(const GlobalValue *GV1, |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 246 | SectionKind Kind, |
Chris Lattner | e53a600 | 2009-07-29 05:09:30 +0000 | [diff] [blame] | 247 | Mangler *Mang, |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 248 | const TargetMachine &TM) const { |
| 249 | // We select the section based on the initializer here, so it really |
| 250 | // has to be a GlobalVariable. |
| 251 | const GlobalVariable *GV = dyn_cast<GlobalVariable>(GV1); |
| 252 | if (!GV) |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 253 | return TargetLoweringObjectFile::SelectSectionForGlobal(GV1, Kind, Mang,TM); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 254 | |
| 255 | // Record External Var Decls. |
| 256 | if (GV->isDeclaration()) { |
| 257 | ExternalVarDecls->Items.push_back(GV); |
| 258 | return ExternalVarDecls->S_; |
| 259 | } |
| 260 | |
| 261 | assert(GV->hasInitializer() && "A def without initializer?"); |
| 262 | |
| 263 | // First, if this is an automatic variable for a function, get the section |
| 264 | // name for it and return. |
| 265 | std::string name = GV->getName(); |
| 266 | if (PAN::isLocalName(name)) |
| 267 | return getSectionForAuto(GV); |
| 268 | |
| 269 | // Record Exteranl Var Defs. |
| 270 | if (GV->hasExternalLinkage() || GV->hasCommonLinkage()) |
| 271 | ExternalVarDefs->Items.push_back(GV); |
| 272 | |
| 273 | // See if this is an uninitialized global. |
| 274 | const Constant *C = GV->getInitializer(); |
| 275 | if (C->isNullValue()) |
| 276 | return getBSSSectionForGlobal(GV); |
| 277 | |
| 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 getIDATASectionForGlobal(GV); |
| 281 | |
| 282 | // This is initialized data in rom, put it in the readonly section. |
| 283 | if (GV->getType()->getAddressSpace() == PIC16ISD::ROM_SPACE) |
| 284 | return getROSectionForGlobal(GV); |
| 285 | |
| 286 | // Else let the default implementation take care of it. |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 287 | return TargetLoweringObjectFile::SelectSectionForGlobal(GV, Kind, Mang,TM); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | PIC16TargetObjectFile::~PIC16TargetObjectFile() { |
| 291 | for (unsigned i = 0; i < BSSSections.size(); i++) |
| 292 | delete BSSSections[i]; |
| 293 | for (unsigned i = 0; i < IDATASections.size(); i++) |
| 294 | delete IDATASections[i]; |
| 295 | for (unsigned i = 0; i < AutosSections.size(); i++) |
| 296 | delete AutosSections[i]; |
| 297 | for (unsigned i = 0; i < ROSections.size(); i++) |
| 298 | delete ROSections[i]; |
| 299 | delete ExternalVarDecls; |
| 300 | delete ExternalVarDefs; |
| 301 | } |
| 302 | |
| 303 | |
| 304 | /// getSpecialCasedSectionGlobals - Allow the target to completely override |
| 305 | /// section assignment of a global. |
Chris Lattner | 24f654c | 2009-08-06 16:39:58 +0000 | [diff] [blame] | 306 | const MCSection *PIC16TargetObjectFile:: |
| 307 | getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind, |
| 308 | Mangler *Mang, const TargetMachine &TM) const { |
| 309 | assert(GV->hasSection()); |
| 310 | |
| 311 | if (const GlobalVariable *GVar = cast<GlobalVariable>(GV)) { |
| 312 | std::string SectName = GVar->getSection(); |
| 313 | // If address for a variable is specified, get the address and create |
| 314 | // section. |
| 315 | std::string AddrStr = "Address="; |
| 316 | if (SectName.compare(0, AddrStr.length(), AddrStr) == 0) { |
| 317 | std::string SectAddr = SectName.substr(AddrStr.length()); |
| 318 | return CreateSectionForGlobal(GVar, Mang, SectAddr); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 319 | } |
Chris Lattner | 24f654c | 2009-08-06 16:39:58 +0000 | [diff] [blame] | 320 | |
| 321 | // Create the section specified with section attribute. |
| 322 | return CreateSectionForGlobal(GVar, Mang); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 323 | } |
| 324 | |
Chris Lattner | 93b6db3 | 2009-08-08 23:39:42 +0000 | [diff] [blame] | 325 | return getPIC16Section(GV->getSection().c_str(), Kind); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | // Create a new section for global variable. If Addr is given then create |
| 329 | // section at that address else create by name. |
Chris Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 330 | const MCSection * |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 331 | PIC16TargetObjectFile::CreateSectionForGlobal(const GlobalVariable *GV, |
Chris Lattner | e53a600 | 2009-07-29 05:09:30 +0000 | [diff] [blame] | 332 | Mangler *Mang, |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 333 | const std::string &Addr) const { |
| 334 | // See if this is an uninitialized global. |
| 335 | const Constant *C = GV->getInitializer(); |
| 336 | if (C->isNullValue()) |
| 337 | return CreateBSSSectionForGlobal(GV, Addr); |
| 338 | |
| 339 | // If this is initialized data in RAM. Put it in the correct IDATA section. |
| 340 | if (GV->getType()->getAddressSpace() == PIC16ISD::RAM_SPACE) |
| 341 | return CreateIDATASectionForGlobal(GV, Addr); |
| 342 | |
| 343 | // This is initialized data in rom, put it in the readonly section. |
| 344 | if (GV->getType()->getAddressSpace() == PIC16ISD::ROM_SPACE) |
| 345 | return CreateROSectionForGlobal(GV, Addr); |
| 346 | |
| 347 | // Else let the default implementation take care of it. |
Chris Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 348 | return TargetLoweringObjectFile::SectionForGlobal(GV, Mang, *TM); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | // Create uninitialized section for a variable. |
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::CreateBSSSectionForGlobal(const GlobalVariable *GV, |
| 354 | std::string Addr) const { |
| 355 | assert(GV->hasInitializer() && "This global doesn't need space"); |
| 356 | assert(GV->getInitializer()->isNullValue() && |
| 357 | "Unitialized global has non-zero initializer"); |
| 358 | std::string Name; |
| 359 | // If address is given then create a section at that address else create a |
| 360 | // section by section name specified in GV. |
| 361 | PIC16Section *FoundBSS = NULL; |
| 362 | if (Addr.empty()) { |
| 363 | Name = GV->getSection() + " UDATA"; |
| 364 | for (unsigned i = 0; i < BSSSections.size(); i++) { |
| 365 | if (BSSSections[i]->S_->getName() == Name) { |
| 366 | FoundBSS = BSSSections[i]; |
| 367 | break; |
| 368 | } |
| 369 | } |
| 370 | } else { |
| 371 | std::string Prefix = GV->getNameStr() + "." + Addr + "."; |
| 372 | Name = PAN::getUdataSectionName(BSSSections.size(), Prefix) + " " + Addr; |
| 373 | } |
| 374 | |
| 375 | PIC16Section *NewBSS = FoundBSS; |
| 376 | if (NewBSS == NULL) { |
Chris Lattner | 93b6db3 | 2009-08-08 23:39:42 +0000 | [diff] [blame] | 377 | const MCSectionPIC16 *NewSection = |
| 378 | getPIC16Section(Name.c_str(), SectionKind::getBSS()); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 379 | NewBSS = new PIC16Section(NewSection); |
| 380 | BSSSections.push_back(NewBSS); |
| 381 | } |
| 382 | |
| 383 | // Insert the GV into this BSS. |
| 384 | NewBSS->Items.push_back(GV); |
| 385 | |
| 386 | // We do not want to put any GV without explicit section into this section |
| 387 | // so set its size to DatabankSize. |
| 388 | NewBSS->Size = DataBankSize; |
| 389 | return NewBSS->S_; |
| 390 | } |
| 391 | |
| 392 | // Get rom section for a variable. Currently there can be only one rom section |
| 393 | // unless a variable explicitly requests a section. |
Chris Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 394 | const MCSection * |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 395 | PIC16TargetObjectFile::getROSectionForGlobal(const GlobalVariable *GV) const { |
| 396 | ROSections[0]->Items.push_back(GV); |
| 397 | return ROSections[0]->S_; |
| 398 | } |
| 399 | |
| 400 | // Create initialized data section 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::CreateIDATASectionForGlobal(const GlobalVariable *GV, |
| 403 | std::string Addr) const { |
| 404 | assert(GV->hasInitializer() && "This global doesn't need space"); |
| 405 | assert(!GV->getInitializer()->isNullValue() && |
| 406 | "initialized global has zero initializer"); |
| 407 | assert(GV->getType()->getAddressSpace() == PIC16ISD::RAM_SPACE && |
| 408 | "can be used for initialized RAM data only"); |
| 409 | |
| 410 | std::string Name; |
| 411 | // If address is given then create a section at that address else create a |
| 412 | // section by section name specified in GV. |
| 413 | PIC16Section *FoundIDATASec = NULL; |
| 414 | if (Addr.empty()) { |
| 415 | Name = GV->getSection() + " IDATA"; |
| 416 | for (unsigned i = 0; i < IDATASections.size(); i++) { |
| 417 | if (IDATASections[i]->S_->getName() == Name) { |
| 418 | FoundIDATASec = IDATASections[i]; |
| 419 | break; |
| 420 | } |
| 421 | } |
| 422 | } else { |
| 423 | std::string Prefix = GV->getNameStr() + "." + Addr + "."; |
| 424 | Name = PAN::getIdataSectionName(IDATASections.size(), Prefix) + " " + Addr; |
| 425 | } |
| 426 | |
| 427 | PIC16Section *NewIDATASec = FoundIDATASec; |
| 428 | if (NewIDATASec == NULL) { |
Chris Lattner | 93b6db3 | 2009-08-08 23:39:42 +0000 | [diff] [blame] | 429 | const MCSectionPIC16 *NewSection = |
| 430 | getPIC16Section(Name.c_str(), /* FIXME */SectionKind::getMetadata()); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 431 | NewIDATASec = new PIC16Section(NewSection); |
| 432 | IDATASections.push_back(NewIDATASec); |
| 433 | } |
| 434 | // Insert the GV into this IDATA Section. |
| 435 | NewIDATASec->Items.push_back(GV); |
| 436 | // We do not want to put any GV without explicit section into this section |
| 437 | // so set its size to DatabankSize. |
| 438 | NewIDATASec->Size = DataBankSize; |
| 439 | return NewIDATASec->S_; |
| 440 | } |
| 441 | |
| 442 | // Create a section in rom for a variable. |
Chris Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 443 | const MCSection * |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 444 | PIC16TargetObjectFile::CreateROSectionForGlobal(const GlobalVariable *GV, |
| 445 | std::string Addr) const { |
| 446 | assert(GV->getType()->getAddressSpace() == PIC16ISD::ROM_SPACE && |
| 447 | "can be used for ROM data only"); |
| 448 | |
| 449 | std::string Name; |
| 450 | // If address is given then create a section at that address else create a |
| 451 | // section by section name specified in GV. |
| 452 | PIC16Section *FoundROSec = NULL; |
| 453 | if (Addr.empty()) { |
| 454 | Name = GV->getSection() + " ROMDATA"; |
| 455 | for (unsigned i = 1; i < ROSections.size(); i++) { |
| 456 | if (ROSections[i]->S_->getName() == Name) { |
| 457 | FoundROSec = ROSections[i]; |
| 458 | break; |
| 459 | } |
| 460 | } |
| 461 | } else { |
| 462 | std::string Prefix = GV->getNameStr() + "." + Addr + "."; |
| 463 | Name = PAN::getRomdataSectionName(ROSections.size(), Prefix) + " " + Addr; |
| 464 | } |
| 465 | |
| 466 | PIC16Section *NewRomSec = FoundROSec; |
| 467 | if (NewRomSec == NULL) { |
Chris Lattner | 93b6db3 | 2009-08-08 23:39:42 +0000 | [diff] [blame] | 468 | const MCSectionPIC16 *NewSection = |
| 469 | getPIC16Section(Name.c_str(), SectionKind::getReadOnly()); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 470 | NewRomSec = new PIC16Section(NewSection); |
| 471 | ROSections.push_back(NewRomSec); |
| 472 | } |
| 473 | |
| 474 | // Insert the GV into this ROM Section. |
| 475 | NewRomSec->Items.push_back(GV); |
| 476 | return NewRomSec->S_; |
| 477 | } |
| 478 | |