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