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" |
| 11 | #include "PIC16ISelLowering.h" |
| 12 | #include "PIC16TargetMachine.h" |
Sanjiv Gupta | 753ec15 | 2009-10-15 19:26:25 +0000 | [diff] [blame] | 13 | #include "PIC16Section.h" |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 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 | |
Sanjiv Gupta | 753ec15 | 2009-10-15 19:26:25 +0000 | [diff] [blame] | 22 | PIC16TargetObjectFile::PIC16TargetObjectFile() { |
Chris Lattner | 93b6db3 | 2009-08-08 23:39:42 +0000 | [diff] [blame] | 23 | } |
| 24 | |
Sanjiv Gupta | 5386a35 | 2009-10-16 08:58:34 +0000 | [diff] [blame] | 25 | PIC16TargetObjectFile::~PIC16TargetObjectFile() { |
| 26 | } |
| 27 | |
Sanjiv Gupta | 4e4bba5 | 2009-10-21 10:42:44 +0000 | [diff] [blame] | 28 | /// Find a pic16 section. Return null if not found. Do not create one. |
| 29 | PIC16Section *PIC16TargetObjectFile:: |
| 30 | findPIC16Section(const std::string &Name) { |
| 31 | /// Return if we have an already existing one. |
| 32 | PIC16Section *Entry = SectionsByName[Name]; |
| 33 | if (Entry) |
| 34 | return Entry; |
| 35 | |
| 36 | return NULL; |
| 37 | } |
| 38 | |
| 39 | |
Sanjiv Gupta | 753ec15 | 2009-10-15 19:26:25 +0000 | [diff] [blame] | 40 | /// Find a pic16 section. If not found, create one. |
| 41 | PIC16Section *PIC16TargetObjectFile:: |
| 42 | getPIC16Section(const std::string &Name, PIC16SectionType Ty, |
| 43 | const std::string &Address, int Color) const { |
Chris Lattner | 93b6db3 | 2009-08-08 23:39:42 +0000 | [diff] [blame] | 44 | |
Sanjiv Gupta | 753ec15 | 2009-10-15 19:26:25 +0000 | [diff] [blame] | 45 | /// Return if we have an already existing one. |
| 46 | PIC16Section *&Entry = SectionsByName[Name]; |
Chris Lattner | 873bc4c | 2009-08-13 00:26:52 +0000 | [diff] [blame] | 47 | if (Entry) |
| 48 | return Entry; |
| 49 | |
Sanjiv Gupta | 753ec15 | 2009-10-15 19:26:25 +0000 | [diff] [blame] | 50 | |
| 51 | Entry = PIC16Section::Create(Name, Ty, Address, Color, getContext()); |
| 52 | return Entry; |
Chris Lattner | fbf1d27 | 2009-08-08 20:14:13 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Sanjiv Gupta | 753ec15 | 2009-10-15 19:26:25 +0000 | [diff] [blame] | 55 | /// Find a standard pic16 data section. If not found, create one and keep |
| 56 | /// track of it by adding it to appropriate std section list. |
| 57 | PIC16Section *PIC16TargetObjectFile:: |
| 58 | getPIC16DataSection(const std::string &Name, PIC16SectionType Ty, |
| 59 | const std::string &Address, int Color) const { |
Chris Lattner | fbf1d27 | 2009-08-08 20:14:13 +0000 | [diff] [blame] | 60 | |
Sanjiv Gupta | 753ec15 | 2009-10-15 19:26:25 +0000 | [diff] [blame] | 61 | /// Return if we have an already existing one. |
| 62 | PIC16Section *&Entry = SectionsByName[Name]; |
| 63 | if (Entry) |
| 64 | return Entry; |
| 65 | |
| 66 | |
| 67 | /// Else create a new one and add it to appropriate section list. |
| 68 | Entry = PIC16Section::Create(Name, Ty, Address, Color, getContext()); |
| 69 | |
| 70 | switch (Ty) { |
| 71 | default: llvm_unreachable ("unknow standard section type."); |
| 72 | case UDATA: UDATASections_.push_back(Entry); break; |
| 73 | case IDATA: IDATASections_.push_back(Entry); break; |
| 74 | case ROMDATA: ROMDATASection_ = Entry; break; |
| 75 | } |
| 76 | |
| 77 | return Entry; |
| 78 | } |
| 79 | |
| 80 | |
| 81 | /// Find a standard pic16 autos section. If not found, create one and keep |
| 82 | /// track of it by adding it to appropriate std section list. |
| 83 | PIC16Section *PIC16TargetObjectFile:: |
| 84 | getPIC16AutoSection(const std::string &Name, PIC16SectionType Ty, |
| 85 | const std::string &Address, int Color) const { |
| 86 | |
| 87 | /// Return if we have an already existing one. |
| 88 | PIC16Section *&Entry = SectionsByName[Name]; |
| 89 | if (Entry) |
| 90 | return Entry; |
| 91 | |
| 92 | |
| 93 | /// Else create a new one and add it to appropriate section list. |
| 94 | Entry = PIC16Section::Create(Name, Ty, Address, Color, getContext()); |
| 95 | |
| 96 | assert (Ty == UDATA_OVR && "incorrect section type for autos"); |
| 97 | AUTOSections_.push_back(Entry); |
| 98 | |
| 99 | return Entry; |
| 100 | } |
| 101 | |
| 102 | /// Find a pic16 user section. If not found, create one and keep |
| 103 | /// track of it by adding it to appropriate std section list. |
| 104 | PIC16Section *PIC16TargetObjectFile:: |
| 105 | getPIC16UserSection(const std::string &Name, PIC16SectionType Ty, |
| 106 | const std::string &Address, int Color) const { |
| 107 | |
| 108 | /// Return if we have an already existing one. |
| 109 | PIC16Section *&Entry = SectionsByName[Name]; |
| 110 | if (Entry) |
| 111 | return Entry; |
| 112 | |
| 113 | |
| 114 | /// Else create a new one and add it to appropriate section list. |
| 115 | Entry = PIC16Section::Create(Name, Ty, Address, Color, getContext()); |
| 116 | |
| 117 | USERSections_.push_back(Entry); |
| 118 | |
| 119 | return Entry; |
| 120 | } |
| 121 | |
Sanjiv Gupta | 5386a35 | 2009-10-16 08:58:34 +0000 | [diff] [blame] | 122 | /// Do some standard initialization. |
Chris Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 123 | void PIC16TargetObjectFile::Initialize(MCContext &Ctx, const TargetMachine &tm){ |
| 124 | TargetLoweringObjectFile::Initialize(Ctx, tm); |
| 125 | TM = &tm; |
| 126 | |
Sanjiv Gupta | 753ec15 | 2009-10-15 19:26:25 +0000 | [diff] [blame] | 127 | ROMDATASection_ = NULL; |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 128 | } |
| 129 | |
Sanjiv Gupta | 753ec15 | 2009-10-15 19:26:25 +0000 | [diff] [blame] | 130 | /// allocateUDATA - Allocate a un-initialized global to an existing or new UDATA |
| 131 | /// section and return that section. |
Chris Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 132 | const MCSection * |
Sanjiv Gupta | 753ec15 | 2009-10-15 19:26:25 +0000 | [diff] [blame] | 133 | PIC16TargetObjectFile::allocateUDATA(const GlobalVariable *GV) const { |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 134 | assert(GV->hasInitializer() && "This global doesn't need space"); |
| 135 | Constant *C = GV->getInitializer(); |
| 136 | assert(C->isNullValue() && "Unitialized globals has non-zero initializer"); |
| 137 | |
| 138 | // Find how much space this global needs. |
Chris Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 139 | const TargetData *TD = TM->getTargetData(); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 140 | const Type *Ty = C->getType(); |
| 141 | unsigned ValSize = TD->getTypeAllocSize(Ty); |
| 142 | |
Sanjiv Gupta | 753ec15 | 2009-10-15 19:26:25 +0000 | [diff] [blame] | 143 | // Go through all UDATA Sections and assign this variable |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 144 | // to the first available section having enough space. |
Sanjiv Gupta | 753ec15 | 2009-10-15 19:26:25 +0000 | [diff] [blame] | 145 | PIC16Section *Found = NULL; |
| 146 | for (unsigned i = 0; i < UDATASections_.size(); i++) { |
| 147 | if (DataBankSize - UDATASections_[i]->getSize() >= ValSize) { |
| 148 | Found = UDATASections_[i]; |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 149 | break; |
| 150 | } |
| 151 | } |
| 152 | |
Sanjiv Gupta | 753ec15 | 2009-10-15 19:26:25 +0000 | [diff] [blame] | 153 | // No UDATA section spacious enough was found. Crate a new one. |
| 154 | if (!Found) { |
| 155 | std::string name = PAN::getUdataSectionName(UDATASections_.size()); |
| 156 | Found = getPIC16DataSection(name.c_str(), UDATA); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 157 | } |
| 158 | |
Sanjiv Gupta | 753ec15 | 2009-10-15 19:26:25 +0000 | [diff] [blame] | 159 | // Insert the GV into this UDATA section. |
| 160 | Found->Items.push_back(GV); |
| 161 | Found->setSize(Found->getSize() + ValSize); |
| 162 | return Found; |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 163 | } |
| 164 | |
Sanjiv Gupta | 753ec15 | 2009-10-15 19:26:25 +0000 | [diff] [blame] | 165 | /// allocateIDATA - allocate an initialized global into an existing |
| 166 | /// or new section and return that section. |
Chris Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 167 | const MCSection * |
Sanjiv Gupta | 753ec15 | 2009-10-15 19:26:25 +0000 | [diff] [blame] | 168 | PIC16TargetObjectFile::allocateIDATA(const GlobalVariable *GV) const{ |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 169 | assert(GV->hasInitializer() && "This global doesn't need space"); |
| 170 | Constant *C = GV->getInitializer(); |
| 171 | assert(!C->isNullValue() && "initialized globals has zero initializer"); |
| 172 | assert(GV->getType()->getAddressSpace() == PIC16ISD::RAM_SPACE && |
Sanjiv Gupta | 753ec15 | 2009-10-15 19:26:25 +0000 | [diff] [blame] | 173 | "can allocate initialized RAM data only"); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 174 | |
| 175 | // Find how much space this global needs. |
Chris Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 176 | const TargetData *TD = TM->getTargetData(); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 177 | const Type *Ty = C->getType(); |
| 178 | unsigned ValSize = TD->getTypeAllocSize(Ty); |
| 179 | |
| 180 | // Go through all IDATA Sections and assign this variable |
| 181 | // to the first available section having enough space. |
Sanjiv Gupta | 753ec15 | 2009-10-15 19:26:25 +0000 | [diff] [blame] | 182 | PIC16Section *Found = NULL; |
| 183 | for (unsigned i = 0; i < IDATASections_.size(); i++) { |
| 184 | if (DataBankSize - IDATASections_[i]->getSize() >= ValSize) { |
| 185 | Found = IDATASections_[i]; |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 186 | break; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | // No IDATA section spacious enough was found. Crate a new one. |
Sanjiv Gupta | 753ec15 | 2009-10-15 19:26:25 +0000 | [diff] [blame] | 191 | if (!Found) { |
| 192 | std::string name = PAN::getIdataSectionName(IDATASections_.size()); |
| 193 | Found = getPIC16DataSection(name.c_str(), IDATA); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | // Insert the GV into this IDATA. |
Sanjiv Gupta | 753ec15 | 2009-10-15 19:26:25 +0000 | [diff] [blame] | 197 | Found->Items.push_back(GV); |
| 198 | Found->setSize(Found->getSize() + ValSize); |
| 199 | return Found; |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 200 | } |
| 201 | |
Sanjiv Gupta | 753ec15 | 2009-10-15 19:26:25 +0000 | [diff] [blame] | 202 | // Allocate a program memory variable into ROMDATA section. |
| 203 | const MCSection * |
| 204 | PIC16TargetObjectFile::allocateROMDATA(const GlobalVariable *GV) const { |
| 205 | |
| 206 | std::string name = PAN::getRomdataSectionName(); |
| 207 | PIC16Section *S = getPIC16DataSection(name.c_str(), ROMDATA); |
| 208 | |
| 209 | S->Items.push_back(GV); |
| 210 | return S; |
| 211 | } |
| 212 | |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 213 | // Get the section for an automatic variable of a function. |
| 214 | // For PIC16 they are globals only with mangled names. |
Chris Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 215 | const MCSection * |
Sanjiv Gupta | 753ec15 | 2009-10-15 19:26:25 +0000 | [diff] [blame] | 216 | PIC16TargetObjectFile::allocateAUTO(const GlobalVariable *GV) const { |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 217 | |
| 218 | const std::string name = PAN::getSectionNameForSym(GV->getName()); |
Sanjiv Gupta | 753ec15 | 2009-10-15 19:26:25 +0000 | [diff] [blame] | 219 | PIC16Section *S = getPIC16AutoSection(name.c_str()); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 220 | |
Sanjiv Gupta | 753ec15 | 2009-10-15 19:26:25 +0000 | [diff] [blame] | 221 | S->Items.push_back(GV); |
| 222 | return S; |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | |
| 226 | // Override default implementation to put the true globals into |
| 227 | // multiple data sections if required. |
Chris Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 228 | const MCSection * |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 229 | PIC16TargetObjectFile::SelectSectionForGlobal(const GlobalValue *GV1, |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 230 | SectionKind Kind, |
Chris Lattner | e53a600 | 2009-07-29 05:09:30 +0000 | [diff] [blame] | 231 | Mangler *Mang, |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 232 | const TargetMachine &TM) const { |
| 233 | // We select the section based on the initializer here, so it really |
| 234 | // has to be a GlobalVariable. |
| 235 | const GlobalVariable *GV = dyn_cast<GlobalVariable>(GV1); |
| 236 | if (!GV) |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 237 | return TargetLoweringObjectFile::SelectSectionForGlobal(GV1, Kind, Mang,TM); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 238 | |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 239 | assert(GV->hasInitializer() && "A def without initializer?"); |
| 240 | |
| 241 | // First, if this is an automatic variable for a function, get the section |
| 242 | // name for it and return. |
| 243 | std::string name = GV->getName(); |
| 244 | if (PAN::isLocalName(name)) |
Sanjiv Gupta | 753ec15 | 2009-10-15 19:26:25 +0000 | [diff] [blame] | 245 | return allocateAUTO(GV); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 246 | |
| 247 | // See if this is an uninitialized global. |
| 248 | const Constant *C = GV->getInitializer(); |
| 249 | if (C->isNullValue()) |
Sanjiv Gupta | 753ec15 | 2009-10-15 19:26:25 +0000 | [diff] [blame] | 250 | return allocateUDATA(GV); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 251 | |
| 252 | // If this is initialized data in RAM. Put it in the correct IDATA section. |
| 253 | if (GV->getType()->getAddressSpace() == PIC16ISD::RAM_SPACE) |
Sanjiv Gupta | 753ec15 | 2009-10-15 19:26:25 +0000 | [diff] [blame] | 254 | return allocateIDATA(GV); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 255 | |
| 256 | // This is initialized data in rom, put it in the readonly section. |
| 257 | if (GV->getType()->getAddressSpace() == PIC16ISD::ROM_SPACE) |
Sanjiv Gupta | 753ec15 | 2009-10-15 19:26:25 +0000 | [diff] [blame] | 258 | return allocateROMDATA(GV); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 259 | |
| 260 | // Else let the default implementation take care of it. |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 261 | return TargetLoweringObjectFile::SelectSectionForGlobal(GV, Kind, Mang,TM); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 262 | } |
| 263 | |
Sanjiv Gupta | 753ec15 | 2009-10-15 19:26:25 +0000 | [diff] [blame] | 264 | |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 265 | |
| 266 | |
Sanjiv Gupta | 753ec15 | 2009-10-15 19:26:25 +0000 | [diff] [blame] | 267 | /// getExplicitSectionGlobal - Allow the target to completely override |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 268 | /// section assignment of a global. |
Chris Lattner | 24f654c | 2009-08-06 16:39:58 +0000 | [diff] [blame] | 269 | const MCSection *PIC16TargetObjectFile:: |
| 270 | getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind, |
| 271 | Mangler *Mang, const TargetMachine &TM) const { |
| 272 | assert(GV->hasSection()); |
| 273 | |
| 274 | if (const GlobalVariable *GVar = cast<GlobalVariable>(GV)) { |
| 275 | std::string SectName = GVar->getSection(); |
| 276 | // If address for a variable is specified, get the address and create |
| 277 | // section. |
Sanjiv Gupta | 753ec15 | 2009-10-15 19:26:25 +0000 | [diff] [blame] | 278 | // FIXME: move this attribute checking in PAN. |
Chris Lattner | 24f654c | 2009-08-06 16:39:58 +0000 | [diff] [blame] | 279 | std::string AddrStr = "Address="; |
| 280 | if (SectName.compare(0, AddrStr.length(), AddrStr) == 0) { |
| 281 | std::string SectAddr = SectName.substr(AddrStr.length()); |
Sanjiv Gupta | ecb28f2 | 2009-10-24 18:19:41 +0000 | [diff] [blame] | 282 | return allocateAtGivenAddress(GVar, SectAddr); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 283 | } |
Chris Lattner | 24f654c | 2009-08-06 16:39:58 +0000 | [diff] [blame] | 284 | |
| 285 | // Create the section specified with section attribute. |
Sanjiv Gupta | 753ec15 | 2009-10-15 19:26:25 +0000 | [diff] [blame] | 286 | return allocateInGivenSection(GVar); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 287 | } |
| 288 | |
Sanjiv Gupta | 753ec15 | 2009-10-15 19:26:25 +0000 | [diff] [blame] | 289 | return getPIC16DataSection(GV->getSection().c_str(), UDATA); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 290 | } |
| 291 | |
Sanjiv Gupta | 753ec15 | 2009-10-15 19:26:25 +0000 | [diff] [blame] | 292 | // Interface used by AsmPrinter to get a code section for a function. |
| 293 | const PIC16Section * |
| 294 | PIC16TargetObjectFile::SectionForCode(const std::string &FnName) const { |
| 295 | const std::string &sec_name = PAN::getCodeSectionName(FnName); |
| 296 | return getPIC16Section(sec_name, CODE); |
| 297 | } |
| 298 | |
| 299 | // Interface used by AsmPrinter to get a frame section for a function. |
| 300 | const PIC16Section * |
| 301 | PIC16TargetObjectFile::SectionForFrame(const std::string &FnName) const { |
| 302 | const std::string &sec_name = PAN::getFrameSectionName(FnName); |
| 303 | return getPIC16Section(sec_name, UDATA_OVR); |
| 304 | } |
| 305 | |
| 306 | // Allocate a global var in existing or new section of given name. |
Chris Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 307 | const MCSection * |
Sanjiv Gupta | 753ec15 | 2009-10-15 19:26:25 +0000 | [diff] [blame] | 308 | PIC16TargetObjectFile::allocateInGivenSection(const GlobalVariable *GV) const { |
| 309 | // Determine the type of section that we need to create. |
| 310 | PIC16SectionType SecTy; |
| 311 | |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 312 | // See if this is an uninitialized global. |
| 313 | const Constant *C = GV->getInitializer(); |
| 314 | if (C->isNullValue()) |
Sanjiv Gupta | 753ec15 | 2009-10-15 19:26:25 +0000 | [diff] [blame] | 315 | SecTy = UDATA; |
Daniel Dunbar | 1ead150 | 2009-10-15 15:02:14 +0000 | [diff] [blame] | 316 | // If this is initialized data in RAM. Put it in the correct IDATA section. |
Sanjiv Gupta | 753ec15 | 2009-10-15 19:26:25 +0000 | [diff] [blame] | 317 | else if (GV->getType()->getAddressSpace() == PIC16ISD::RAM_SPACE) |
| 318 | SecTy = IDATA; |
Daniel Dunbar | 1ead150 | 2009-10-15 15:02:14 +0000 | [diff] [blame] | 319 | // This is initialized data in rom, put it in the readonly section. |
Sanjiv Gupta | 753ec15 | 2009-10-15 19:26:25 +0000 | [diff] [blame] | 320 | else if (GV->getType()->getAddressSpace() == PIC16ISD::ROM_SPACE) |
| 321 | SecTy = ROMDATA; |
| 322 | else |
| 323 | llvm_unreachable ("Could not determine section type for global"); |
Daniel Dunbar | 1ead150 | 2009-10-15 15:02:14 +0000 | [diff] [blame] | 324 | |
Sanjiv Gupta | 753ec15 | 2009-10-15 19:26:25 +0000 | [diff] [blame] | 325 | PIC16Section *S = getPIC16UserSection(GV->getSection().c_str(), SecTy); |
| 326 | S->Items.push_back(GV); |
| 327 | return S; |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 328 | } |
| 329 | |
Sanjiv Gupta | 753ec15 | 2009-10-15 19:26:25 +0000 | [diff] [blame] | 330 | // Allocate a global var in a new absolute sections at given address. |
Chris Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 331 | const MCSection * |
Sanjiv Gupta | 753ec15 | 2009-10-15 19:26:25 +0000 | [diff] [blame] | 332 | PIC16TargetObjectFile::allocateAtGivenAddress(const GlobalVariable *GV, |
| 333 | const std::string &Addr) const { |
| 334 | // Determine the type of section that we need to create. |
| 335 | PIC16SectionType SecTy; |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 336 | |
Sanjiv Gupta | 753ec15 | 2009-10-15 19:26:25 +0000 | [diff] [blame] | 337 | // See if this is an uninitialized global. |
| 338 | const Constant *C = GV->getInitializer(); |
| 339 | if (C->isNullValue()) |
| 340 | SecTy = UDATA; |
| 341 | // If this is initialized data in RAM. Put it in the correct IDATA section. |
| 342 | else if (GV->getType()->getAddressSpace() == PIC16ISD::RAM_SPACE) |
| 343 | SecTy = IDATA; |
| 344 | // This is initialized data in rom, put it in the readonly section. |
| 345 | else if (GV->getType()->getAddressSpace() == PIC16ISD::ROM_SPACE) |
| 346 | SecTy = ROMDATA; |
| 347 | else |
| 348 | llvm_unreachable ("Could not determine section type for global"); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 349 | |
Sanjiv Gupta | 753ec15 | 2009-10-15 19:26:25 +0000 | [diff] [blame] | 350 | std::string Prefix = GV->getNameStr() + "." + Addr + "."; |
| 351 | std::string SName = PAN::getUserSectionName(Prefix); |
| 352 | PIC16Section *S = getPIC16UserSection(SName.c_str(), SecTy, Addr.c_str()); |
| 353 | S->Items.push_back(GV); |
| 354 | return S; |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 355 | } |
| 356 | |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 357 | |