Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 1 | //===-- PIC16TargetAsmInfo.cpp - PIC16 asm properties ---------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file contains the declarations of the PIC16TargetAsmInfo properties. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "PIC16TargetAsmInfo.h" |
Dan Gohman | 8f09225 | 2008-11-03 18:22:42 +0000 | [diff] [blame] | 15 | #include "PIC16TargetMachine.h" |
Sanjiv Gupta | 1b04694 | 2009-01-13 19:18:47 +0000 | [diff] [blame] | 16 | #include "llvm/GlobalValue.h" |
Sanjiv Gupta | d8d27f4 | 2009-05-06 08:02:01 +0000 | [diff] [blame] | 17 | #include "llvm/GlobalVariable.h" |
| 18 | #include "llvm/DerivedTypes.h" |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 19 | |
| 20 | using namespace llvm; |
| 21 | |
| 22 | PIC16TargetAsmInfo:: |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 23 | PIC16TargetAsmInfo(const PIC16TargetMachine &TM) |
Dan Gohman | 8f09225 | 2008-11-03 18:22:42 +0000 | [diff] [blame] | 24 | : TargetAsmInfo(TM) { |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 25 | CommentString = ";"; |
Sanjiv Gupta | 211f362 | 2009-05-10 05:23:47 +0000 | [diff] [blame] | 26 | GlobalPrefix = PAN::getTagName(PAN::PREFIX_SYMBOL); |
| 27 | GlobalDirective = "\tglobal\t"; |
| 28 | ExternDirective = "\textern\t"; |
| 29 | |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 30 | Data8bitsDirective = " db "; |
Sanjiv Gupta | c8d7bc8 | 2009-01-30 04:25:10 +0000 | [diff] [blame] | 31 | Data16bitsDirective = " dw "; |
| 32 | Data32bitsDirective = " dl "; |
Sanjiv Gupta | 53cf829 | 2009-07-06 18:09:11 +0000 | [diff] [blame] | 33 | Data64bitsDirective = NULL; |
Sanjiv Gupta | c8d7bc8 | 2009-01-30 04:25:10 +0000 | [diff] [blame] | 34 | RomData8bitsDirective = " dw "; |
| 35 | RomData16bitsDirective = " rom_di "; |
Sanjiv Gupta | 3b020fe | 2009-02-02 16:53:06 +0000 | [diff] [blame] | 36 | RomData32bitsDirective = " rom_dl "; |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 37 | ZeroDirective = NULL; |
Sanjiv Gupta | 1b04694 | 2009-01-13 19:18:47 +0000 | [diff] [blame] | 38 | AsciiDirective = " dt "; |
| 39 | AscizDirective = NULL; |
| 40 | BSSSection_ = getNamedSection("udata.# UDATA", |
Chris Lattner | b4fc419 | 2009-07-25 18:57:34 +0000 | [diff] [blame] | 41 | SectionFlags::Writable | SectionFlags::BSS); |
Sanjiv Gupta | 1b04694 | 2009-01-13 19:18:47 +0000 | [diff] [blame] | 42 | ReadOnlySection = getNamedSection("romdata.# ROMDATA", SectionFlags::None); |
Chris Lattner | b4fc419 | 2009-07-25 18:57:34 +0000 | [diff] [blame] | 43 | DataSection = getNamedSection("idata.# IDATA", SectionFlags::Writable); |
Sanjiv Gupta | 1b04694 | 2009-01-13 19:18:47 +0000 | [diff] [blame] | 44 | SwitchToSectionDirective = ""; |
Sanjiv Gupta | c1fa70c | 2009-04-08 06:24:04 +0000 | [diff] [blame] | 45 | // Need because otherwise a .text symbol is emitted by DwarfWriter |
| 46 | // in BeginModule, and gpasm cribbs for that .text symbol. |
| 47 | TextSection = getUnnamedSection("", SectionFlags::Code); |
Sanjiv Gupta | 505996f | 2009-07-06 10:18:37 +0000 | [diff] [blame] | 48 | PIC16Section *ROSection = new PIC16Section(getReadOnlySection()); |
| 49 | ROSections.push_back(ROSection); |
Sanjiv Gupta | ad6585b | 2009-05-13 15:13:17 +0000 | [diff] [blame] | 50 | ExternalVarDecls = new PIC16Section(getNamedSection("ExternalVarDecls")); |
| 51 | ExternalVarDefs = new PIC16Section(getNamedSection("ExternalVarDefs")); |
Sanjiv Gupta | dd4694b | 2009-05-28 18:24:11 +0000 | [diff] [blame] | 52 | // Set it to false because we weed to generate c file name and not bc file |
| 53 | // name. |
| 54 | HasSingleParameterDotFile = false; |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 55 | } |
Sanjiv Gupta | c8d7bc8 | 2009-01-30 04:25:10 +0000 | [diff] [blame] | 56 | |
Chris Lattner | 83757c7 | 2009-07-20 17:12:46 +0000 | [diff] [blame] | 57 | const char *PIC16TargetAsmInfo::getRomDirective(unsigned Size) const { |
| 58 | switch (Size) { |
| 59 | case 8: return RomData8bitsDirective; |
| 60 | case 16: return RomData16bitsDirective; |
| 61 | case 32: return RomData32bitsDirective; |
| 62 | default: return NULL; |
| 63 | } |
Sanjiv Gupta | 3b020fe | 2009-02-02 16:53:06 +0000 | [diff] [blame] | 64 | } |
Sanjiv Gupta | c8d7bc8 | 2009-01-30 04:25:10 +0000 | [diff] [blame] | 65 | |
Sanjiv Gupta | c8d7bc8 | 2009-01-30 04:25:10 +0000 | [diff] [blame] | 66 | |
Chris Lattner | 83757c7 | 2009-07-20 17:12:46 +0000 | [diff] [blame] | 67 | const char *PIC16TargetAsmInfo:: |
| 68 | getDataASDirective(unsigned Size, unsigned AS) const { |
Sanjiv Gupta | 3b020fe | 2009-02-02 16:53:06 +0000 | [diff] [blame] | 69 | if (AS == PIC16ISD::ROM_SPACE) |
Chris Lattner | 83757c7 | 2009-07-20 17:12:46 +0000 | [diff] [blame] | 70 | return getRomDirective(Size); |
| 71 | return NULL; |
Sanjiv Gupta | 3b020fe | 2009-02-02 16:53:06 +0000 | [diff] [blame] | 72 | } |
Sanjiv Gupta | c8d7bc8 | 2009-01-30 04:25:10 +0000 | [diff] [blame] | 73 | |
Sanjiv Gupta | d8d27f4 | 2009-05-06 08:02:01 +0000 | [diff] [blame] | 74 | const Section * |
| 75 | PIC16TargetAsmInfo::getBSSSectionForGlobal(const GlobalVariable *GV) const { |
Chris Lattner | a46cb52 | 2009-07-21 17:20:18 +0000 | [diff] [blame] | 76 | assert(GV->hasInitializer() && "This global doesn't need space"); |
Sanjiv Gupta | d8d27f4 | 2009-05-06 08:02:01 +0000 | [diff] [blame] | 77 | Constant *C = GV->getInitializer(); |
Chris Lattner | a46cb52 | 2009-07-21 17:20:18 +0000 | [diff] [blame] | 78 | assert(C->isNullValue() && "Unitialized globals has non-zero initializer"); |
Sanjiv Gupta | d8d27f4 | 2009-05-06 08:02:01 +0000 | [diff] [blame] | 79 | |
| 80 | // Find how much space this global needs. |
| 81 | const TargetData *TD = TM.getTargetData(); |
| 82 | const Type *Ty = C->getType(); |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 83 | unsigned ValSize = TD->getTypeAllocSize(Ty); |
Sanjiv Gupta | d8d27f4 | 2009-05-06 08:02:01 +0000 | [diff] [blame] | 84 | |
| 85 | // Go through all BSS Sections and assign this variable |
| 86 | // to the first available section having enough space. |
| 87 | PIC16Section *FoundBSS = NULL; |
| 88 | for (unsigned i = 0; i < BSSSections.size(); i++) { |
| 89 | if (DataBankSize - BSSSections[i]->Size >= ValSize) { |
| 90 | FoundBSS = BSSSections[i]; |
| 91 | break; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | // No BSS section spacious enough was found. Crate a new one. |
Chris Lattner | a46cb52 | 2009-07-21 17:20:18 +0000 | [diff] [blame] | 96 | if (!FoundBSS) { |
Sanjiv Gupta | 211f362 | 2009-05-10 05:23:47 +0000 | [diff] [blame] | 97 | std::string name = PAN::getUdataSectionName(BSSSections.size()); |
Chris Lattner | a46cb52 | 2009-07-21 17:20:18 +0000 | [diff] [blame] | 98 | const Section *NewSection = getNamedSection(name.c_str()); |
Sanjiv Gupta | d8d27f4 | 2009-05-06 08:02:01 +0000 | [diff] [blame] | 99 | |
| 100 | FoundBSS = new PIC16Section(NewSection); |
| 101 | |
| 102 | // Add this newly created BSS section to the list of BSSSections. |
| 103 | BSSSections.push_back(FoundBSS); |
| 104 | } |
| 105 | |
| 106 | // Insert the GV into this BSS. |
| 107 | FoundBSS->Items.push_back(GV); |
| 108 | FoundBSS->Size += ValSize; |
Sanjiv Gupta | d8d27f4 | 2009-05-06 08:02:01 +0000 | [diff] [blame] | 109 | return FoundBSS->S_; |
| 110 | } |
| 111 | |
| 112 | const Section * |
| 113 | PIC16TargetAsmInfo::getIDATASectionForGlobal(const GlobalVariable *GV) const { |
Chris Lattner | a46cb52 | 2009-07-21 17:20:18 +0000 | [diff] [blame] | 114 | assert(GV->hasInitializer() && "This global doesn't need space"); |
Sanjiv Gupta | d8d27f4 | 2009-05-06 08:02:01 +0000 | [diff] [blame] | 115 | Constant *C = GV->getInitializer(); |
Chris Lattner | a46cb52 | 2009-07-21 17:20:18 +0000 | [diff] [blame] | 116 | assert(!C->isNullValue() && "initialized globals has zero initializer"); |
| 117 | assert(GV->getType()->getAddressSpace() == PIC16ISD::RAM_SPACE && |
| 118 | "can split initialized RAM data only"); |
Sanjiv Gupta | d8d27f4 | 2009-05-06 08:02:01 +0000 | [diff] [blame] | 119 | |
| 120 | // Find how much space this global needs. |
| 121 | const TargetData *TD = TM.getTargetData(); |
| 122 | const Type *Ty = C->getType(); |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 123 | unsigned ValSize = TD->getTypeAllocSize(Ty); |
Sanjiv Gupta | d8d27f4 | 2009-05-06 08:02:01 +0000 | [diff] [blame] | 124 | |
| 125 | // Go through all IDATA Sections and assign this variable |
| 126 | // to the first available section having enough space. |
| 127 | PIC16Section *FoundIDATA = NULL; |
| 128 | for (unsigned i = 0; i < IDATASections.size(); i++) { |
Chris Lattner | a46cb52 | 2009-07-21 17:20:18 +0000 | [diff] [blame] | 129 | if (DataBankSize - IDATASections[i]->Size >= ValSize) { |
Sanjiv Gupta | d8d27f4 | 2009-05-06 08:02:01 +0000 | [diff] [blame] | 130 | FoundIDATA = IDATASections[i]; |
| 131 | break; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | // No IDATA section spacious enough was found. Crate a new one. |
Chris Lattner | a46cb52 | 2009-07-21 17:20:18 +0000 | [diff] [blame] | 136 | if (!FoundIDATA) { |
Sanjiv Gupta | 211f362 | 2009-05-10 05:23:47 +0000 | [diff] [blame] | 137 | std::string name = PAN::getIdataSectionName(IDATASections.size()); |
Chris Lattner | a46cb52 | 2009-07-21 17:20:18 +0000 | [diff] [blame] | 138 | const Section *NewSection = getNamedSection(name.c_str()); |
Sanjiv Gupta | d8d27f4 | 2009-05-06 08:02:01 +0000 | [diff] [blame] | 139 | |
| 140 | FoundIDATA = new PIC16Section(NewSection); |
| 141 | |
| 142 | // Add this newly created IDATA section to the list of IDATASections. |
| 143 | IDATASections.push_back(FoundIDATA); |
| 144 | } |
| 145 | |
| 146 | // Insert the GV into this IDATA. |
| 147 | FoundIDATA->Items.push_back(GV); |
| 148 | FoundIDATA->Size += ValSize; |
Sanjiv Gupta | d8d27f4 | 2009-05-06 08:02:01 +0000 | [diff] [blame] | 149 | return FoundIDATA->S_; |
| 150 | } |
| 151 | |
Sanjiv Gupta | 2364cfe | 2009-05-12 17:07:27 +0000 | [diff] [blame] | 152 | // Get the section for an automatic variable of a function. |
| 153 | // For PIC16 they are globals only with mangled names. |
| 154 | const Section * |
| 155 | PIC16TargetAsmInfo::getSectionForAuto(const GlobalVariable *GV) const { |
| 156 | |
| 157 | const std::string name = PAN::getSectionNameForSym(GV->getName()); |
| 158 | |
| 159 | // Go through all Auto Sections and assign this variable |
| 160 | // to the appropriate section. |
| 161 | PIC16Section *FoundAutoSec = NULL; |
| 162 | for (unsigned i = 0; i < AutosSections.size(); i++) { |
Chris Lattner | a46cb52 | 2009-07-21 17:20:18 +0000 | [diff] [blame] | 163 | if (AutosSections[i]->S_->getName() == name) { |
Sanjiv Gupta | 2364cfe | 2009-05-12 17:07:27 +0000 | [diff] [blame] | 164 | FoundAutoSec = AutosSections[i]; |
| 165 | break; |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | // No Auto section was found. Crate a new one. |
Chris Lattner | a46cb52 | 2009-07-21 17:20:18 +0000 | [diff] [blame] | 170 | if (!FoundAutoSec) { |
| 171 | const Section *NewSection = getNamedSection(name.c_str()); |
Sanjiv Gupta | 2364cfe | 2009-05-12 17:07:27 +0000 | [diff] [blame] | 172 | |
| 173 | FoundAutoSec = new PIC16Section(NewSection); |
| 174 | |
| 175 | // Add this newly created autos section to the list of AutosSections. |
| 176 | AutosSections.push_back(FoundAutoSec); |
| 177 | } |
| 178 | |
| 179 | // Insert the auto into this section. |
| 180 | FoundAutoSec->Items.push_back(GV); |
| 181 | |
| 182 | return FoundAutoSec->S_; |
| 183 | } |
| 184 | |
| 185 | |
Sanjiv Gupta | d8d27f4 | 2009-05-06 08:02:01 +0000 | [diff] [blame] | 186 | // Override default implementation to put the true globals into |
| 187 | // multiple data sections if required. |
| 188 | const Section* |
Chris Lattner | f20f250 | 2009-07-24 18:42:53 +0000 | [diff] [blame] | 189 | PIC16TargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV1, |
Chris Lattner | 460d51e | 2009-07-25 23:21:55 +0000 | [diff] [blame] | 190 | SectionKind Kind) const { |
Sanjiv Gupta | d8d27f4 | 2009-05-06 08:02:01 +0000 | [diff] [blame] | 191 | // We select the section based on the initializer here, so it really |
| 192 | // has to be a GlobalVariable. |
Sanjiv Gupta | 211f362 | 2009-05-10 05:23:47 +0000 | [diff] [blame] | 193 | const GlobalVariable *GV = dyn_cast<GlobalVariable>(GV1); |
Sanjiv Gupta | ad6585b | 2009-05-13 15:13:17 +0000 | [diff] [blame] | 194 | if (!GV) |
Chris Lattner | f20f250 | 2009-07-24 18:42:53 +0000 | [diff] [blame] | 195 | return TargetAsmInfo::SelectSectionForGlobal(GV1, Kind); |
Sanjiv Gupta | d8d27f4 | 2009-05-06 08:02:01 +0000 | [diff] [blame] | 196 | |
Chris Lattner | f20f250 | 2009-07-24 18:42:53 +0000 | [diff] [blame] | 197 | // Record External Var Decls. |
Sanjiv Gupta | ad6585b | 2009-05-13 15:13:17 +0000 | [diff] [blame] | 198 | if (GV->isDeclaration()) { |
| 199 | ExternalVarDecls->Items.push_back(GV); |
| 200 | return ExternalVarDecls->S_; |
| 201 | } |
| 202 | |
Chris Lattner | a46cb52 | 2009-07-21 17:20:18 +0000 | [diff] [blame] | 203 | assert(GV->hasInitializer() && "A def without initializer?"); |
Sanjiv Gupta | ad6585b | 2009-05-13 15:13:17 +0000 | [diff] [blame] | 204 | |
Sanjiv Gupta | 211f362 | 2009-05-10 05:23:47 +0000 | [diff] [blame] | 205 | // First, if this is an automatic variable for a function, get the section |
| 206 | // name for it and return. |
Chris Lattner | 7420ab2 | 2009-07-24 18:34:27 +0000 | [diff] [blame] | 207 | std::string name = GV->getName(); |
| 208 | if (PAN::isLocalName(name)) |
Sanjiv Gupta | 2364cfe | 2009-05-12 17:07:27 +0000 | [diff] [blame] | 209 | return getSectionForAuto(GV); |
Sanjiv Gupta | d8d27f4 | 2009-05-06 08:02:01 +0000 | [diff] [blame] | 210 | |
Sanjiv Gupta | ad6585b | 2009-05-13 15:13:17 +0000 | [diff] [blame] | 211 | // Record Exteranl Var Defs. |
Chris Lattner | 7420ab2 | 2009-07-24 18:34:27 +0000 | [diff] [blame] | 212 | if (GV->hasExternalLinkage() || GV->hasCommonLinkage()) |
Sanjiv Gupta | ad6585b | 2009-05-13 15:13:17 +0000 | [diff] [blame] | 213 | ExternalVarDefs->Items.push_back(GV); |
Sanjiv Gupta | ad6585b | 2009-05-13 15:13:17 +0000 | [diff] [blame] | 214 | |
Sanjiv Gupta | d8d27f4 | 2009-05-06 08:02:01 +0000 | [diff] [blame] | 215 | // See if this is an uninitialized global. |
Sanjiv Gupta | 211f362 | 2009-05-10 05:23:47 +0000 | [diff] [blame] | 216 | const Constant *C = GV->getInitializer(); |
Sanjiv Gupta | d8d27f4 | 2009-05-06 08:02:01 +0000 | [diff] [blame] | 217 | if (C->isNullValue()) |
| 218 | return getBSSSectionForGlobal(GV); |
| 219 | |
Sanjiv Gupta | 2364cfe | 2009-05-12 17:07:27 +0000 | [diff] [blame] | 220 | // If this is initialized data in RAM. Put it in the correct IDATA section. |
Sanjiv Gupta | d8d27f4 | 2009-05-06 08:02:01 +0000 | [diff] [blame] | 221 | if (GV->getType()->getAddressSpace() == PIC16ISD::RAM_SPACE) |
| 222 | return getIDATASectionForGlobal(GV); |
| 223 | |
Sanjiv Gupta | 2364cfe | 2009-05-12 17:07:27 +0000 | [diff] [blame] | 224 | // This is initialized data in rom, put it in the readonly section. |
Sanjiv Gupta | 505996f | 2009-07-06 10:18:37 +0000 | [diff] [blame] | 225 | if (GV->getType()->getAddressSpace() == PIC16ISD::ROM_SPACE) |
| 226 | return getROSectionForGlobal(GV); |
Sanjiv Gupta | 2364cfe | 2009-05-12 17:07:27 +0000 | [diff] [blame] | 227 | |
Sanjiv Gupta | d8d27f4 | 2009-05-06 08:02:01 +0000 | [diff] [blame] | 228 | // Else let the default implementation take care of it. |
Chris Lattner | f20f250 | 2009-07-24 18:42:53 +0000 | [diff] [blame] | 229 | return TargetAsmInfo::SelectSectionForGlobal(GV, Kind); |
Sanjiv Gupta | d8d27f4 | 2009-05-06 08:02:01 +0000 | [diff] [blame] | 230 | } |
| 231 | |
Sanjiv Gupta | d8d27f4 | 2009-05-06 08:02:01 +0000 | [diff] [blame] | 232 | PIC16TargetAsmInfo::~PIC16TargetAsmInfo() { |
Chris Lattner | b3e7189 | 2009-07-20 22:23:48 +0000 | [diff] [blame] | 233 | for (unsigned i = 0; i < BSSSections.size(); i++) |
| 234 | delete BSSSections[i]; |
| 235 | for (unsigned i = 0; i < IDATASections.size(); i++) |
| 236 | delete IDATASections[i]; |
| 237 | for (unsigned i = 0; i < AutosSections.size(); i++) |
| 238 | delete AutosSections[i]; |
| 239 | for (unsigned i = 0; i < ROSections.size(); i++) |
| 240 | delete ROSections[i]; |
Sanjiv Gupta | ad6585b | 2009-05-13 15:13:17 +0000 | [diff] [blame] | 241 | delete ExternalVarDecls; |
| 242 | delete ExternalVarDefs; |
Sanjiv Gupta | d8d27f4 | 2009-05-06 08:02:01 +0000 | [diff] [blame] | 243 | } |
Sanjiv Gupta | 505996f | 2009-07-06 10:18:37 +0000 | [diff] [blame] | 244 | |
Chris Lattner | 7420ab2 | 2009-07-24 18:34:27 +0000 | [diff] [blame] | 245 | |
| 246 | /// getSpecialCasedSectionGlobals - Allow the target to completely override |
| 247 | /// section assignment of a global. |
| 248 | const Section * |
| 249 | PIC16TargetAsmInfo::getSpecialCasedSectionGlobals(const GlobalValue *GV, |
Chris Lattner | 460d51e | 2009-07-25 23:21:55 +0000 | [diff] [blame] | 250 | SectionKind Kind) const { |
Sanjiv Gupta | 505996f | 2009-07-06 10:18:37 +0000 | [diff] [blame] | 251 | // If GV has a sectin name or section address create that section now. |
| 252 | if (GV->hasSection()) { |
Chris Lattner | 7c023d6 | 2009-07-24 17:13:27 +0000 | [diff] [blame] | 253 | if (const GlobalVariable *GVar = cast<GlobalVariable>(GV)) { |
| 254 | std::string SectName = GVar->getSection(); |
| 255 | // If address for a variable is specified, get the address and create |
| 256 | // section. |
| 257 | std::string AddrStr = "Address="; |
| 258 | if (SectName.compare(0, AddrStr.length(), AddrStr) == 0) { |
| 259 | std::string SectAddr = SectName.substr(AddrStr.length()); |
| 260 | return CreateSectionForGlobal(GVar, SectAddr); |
| 261 | } |
Chris Lattner | 74fb545 | 2009-07-26 18:08:15 +0000 | [diff] [blame] | 262 | |
| 263 | // Create the section specified with section attribute. |
| 264 | return CreateSectionForGlobal(GVar); |
Chris Lattner | 796131e | 2009-07-24 04:59:43 +0000 | [diff] [blame] | 265 | } |
Sanjiv Gupta | 505996f | 2009-07-06 10:18:37 +0000 | [diff] [blame] | 266 | } |
Chris Lattner | 7420ab2 | 2009-07-24 18:34:27 +0000 | [diff] [blame] | 267 | |
| 268 | return 0; |
Sanjiv Gupta | 505996f | 2009-07-06 10:18:37 +0000 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | // Create a new section for global variable. If Addr is given then create |
| 272 | // section at that address else create by name. |
| 273 | const Section * |
Chris Lattner | 7c023d6 | 2009-07-24 17:13:27 +0000 | [diff] [blame] | 274 | PIC16TargetAsmInfo::CreateSectionForGlobal(const GlobalVariable *GV, |
| 275 | const std::string &Addr) const { |
Sanjiv Gupta | 505996f | 2009-07-06 10:18:37 +0000 | [diff] [blame] | 276 | // See if this is an uninitialized global. |
| 277 | const Constant *C = GV->getInitializer(); |
| 278 | if (C->isNullValue()) |
| 279 | return CreateBSSSectionForGlobal(GV, Addr); |
| 280 | |
| 281 | // If this is initialized data in RAM. Put it in the correct IDATA section. |
| 282 | if (GV->getType()->getAddressSpace() == PIC16ISD::RAM_SPACE) |
| 283 | return CreateIDATASectionForGlobal(GV, Addr); |
| 284 | |
| 285 | // This is initialized data in rom, put it in the readonly section. |
| 286 | if (GV->getType()->getAddressSpace() == PIC16ISD::ROM_SPACE) |
| 287 | return CreateROSectionForGlobal(GV, Addr); |
| 288 | |
| 289 | // Else let the default implementation take care of it. |
| 290 | return TargetAsmInfo::SectionForGlobal(GV); |
| 291 | } |
| 292 | |
| 293 | // Create uninitialized section for a variable. |
| 294 | const Section * |
| 295 | PIC16TargetAsmInfo::CreateBSSSectionForGlobal(const GlobalVariable *GV, |
| 296 | std::string Addr) const { |
Chris Lattner | a46cb52 | 2009-07-21 17:20:18 +0000 | [diff] [blame] | 297 | assert(GV->hasInitializer() && "This global doesn't need space"); |
| 298 | assert(GV->getInitializer()->isNullValue() && |
| 299 | "Unitialized global has non-zero initializer"); |
Sanjiv Gupta | 505996f | 2009-07-06 10:18:37 +0000 | [diff] [blame] | 300 | std::string Name; |
| 301 | // If address is given then create a section at that address else create a |
| 302 | // section by section name specified in GV. |
| 303 | PIC16Section *FoundBSS = NULL; |
| 304 | if (Addr.empty()) { |
| 305 | Name = GV->getSection() + " UDATA"; |
| 306 | for (unsigned i = 0; i < BSSSections.size(); i++) { |
| 307 | if (BSSSections[i]->S_->getName() == Name) { |
| 308 | FoundBSS = BSSSections[i]; |
| 309 | break; |
| 310 | } |
| 311 | } |
Chris Lattner | a46cb52 | 2009-07-21 17:20:18 +0000 | [diff] [blame] | 312 | } else { |
Daniel Dunbar | f6ccee5 | 2009-07-24 08:24:36 +0000 | [diff] [blame] | 313 | std::string Prefix = GV->getNameStr() + "." + Addr + "."; |
Sanjiv Gupta | 505996f | 2009-07-06 10:18:37 +0000 | [diff] [blame] | 314 | Name = PAN::getUdataSectionName(BSSSections.size(), Prefix) + " " + Addr; |
| 315 | } |
| 316 | |
| 317 | PIC16Section *NewBSS = FoundBSS; |
| 318 | if (NewBSS == NULL) { |
Chris Lattner | a46cb52 | 2009-07-21 17:20:18 +0000 | [diff] [blame] | 319 | const Section *NewSection = getNamedSection(Name.c_str()); |
Sanjiv Gupta | 505996f | 2009-07-06 10:18:37 +0000 | [diff] [blame] | 320 | NewBSS = new PIC16Section(NewSection); |
| 321 | BSSSections.push_back(NewBSS); |
| 322 | } |
| 323 | |
| 324 | // Insert the GV into this BSS. |
| 325 | NewBSS->Items.push_back(GV); |
| 326 | |
| 327 | // We do not want to put any GV without explicit section into this section |
| 328 | // so set its size to DatabankSize. |
| 329 | NewBSS->Size = DataBankSize; |
| 330 | return NewBSS->S_; |
| 331 | } |
| 332 | |
| 333 | // Get rom section for a variable. Currently there can be only one rom section |
| 334 | // unless a variable explicitly requests a section. |
| 335 | const Section * |
| 336 | PIC16TargetAsmInfo::getROSectionForGlobal(const GlobalVariable *GV) const { |
| 337 | ROSections[0]->Items.push_back(GV); |
| 338 | return ROSections[0]->S_; |
| 339 | } |
| 340 | |
| 341 | // Create initialized data section for a variable. |
| 342 | const Section * |
| 343 | PIC16TargetAsmInfo::CreateIDATASectionForGlobal(const GlobalVariable *GV, |
| 344 | std::string Addr) const { |
Chris Lattner | a46cb52 | 2009-07-21 17:20:18 +0000 | [diff] [blame] | 345 | assert(GV->hasInitializer() && "This global doesn't need space"); |
| 346 | assert(!GV->getInitializer()->isNullValue() && |
| 347 | "initialized global has zero initializer"); |
| 348 | assert(GV->getType()->getAddressSpace() == PIC16ISD::RAM_SPACE && |
| 349 | "can be used for initialized RAM data only"); |
Sanjiv Gupta | 505996f | 2009-07-06 10:18:37 +0000 | [diff] [blame] | 350 | |
| 351 | std::string Name; |
| 352 | // If address is given then create a section at that address else create a |
| 353 | // section by section name specified in GV. |
| 354 | PIC16Section *FoundIDATASec = NULL; |
| 355 | if (Addr.empty()) { |
| 356 | Name = GV->getSection() + " IDATA"; |
| 357 | for (unsigned i = 0; i < IDATASections.size(); i++) { |
| 358 | if (IDATASections[i]->S_->getName() == Name) { |
| 359 | FoundIDATASec = IDATASections[i]; |
| 360 | break; |
| 361 | } |
| 362 | } |
Chris Lattner | a46cb52 | 2009-07-21 17:20:18 +0000 | [diff] [blame] | 363 | } else { |
Daniel Dunbar | f6ccee5 | 2009-07-24 08:24:36 +0000 | [diff] [blame] | 364 | std::string Prefix = GV->getNameStr() + "." + Addr + "."; |
Sanjiv Gupta | 505996f | 2009-07-06 10:18:37 +0000 | [diff] [blame] | 365 | Name = PAN::getIdataSectionName(IDATASections.size(), Prefix) + " " + Addr; |
| 366 | } |
| 367 | |
| 368 | PIC16Section *NewIDATASec = FoundIDATASec; |
| 369 | if (NewIDATASec == NULL) { |
Chris Lattner | a46cb52 | 2009-07-21 17:20:18 +0000 | [diff] [blame] | 370 | const Section *NewSection = getNamedSection(Name.c_str()); |
Sanjiv Gupta | 505996f | 2009-07-06 10:18:37 +0000 | [diff] [blame] | 371 | NewIDATASec = new PIC16Section(NewSection); |
| 372 | IDATASections.push_back(NewIDATASec); |
| 373 | } |
| 374 | // Insert the GV into this IDATA Section. |
| 375 | NewIDATASec->Items.push_back(GV); |
| 376 | // We do not want to put any GV without explicit section into this section |
| 377 | // so set its size to DatabankSize. |
| 378 | NewIDATASec->Size = DataBankSize; |
| 379 | return NewIDATASec->S_; |
| 380 | } |
| 381 | |
| 382 | // Create a section in rom for a variable. |
| 383 | const Section * |
| 384 | PIC16TargetAsmInfo::CreateROSectionForGlobal(const GlobalVariable *GV, |
| 385 | std::string Addr) const { |
Chris Lattner | a46cb52 | 2009-07-21 17:20:18 +0000 | [diff] [blame] | 386 | assert(GV->getType()->getAddressSpace() == PIC16ISD::ROM_SPACE && |
| 387 | "can be used for ROM data only"); |
Sanjiv Gupta | 505996f | 2009-07-06 10:18:37 +0000 | [diff] [blame] | 388 | |
| 389 | std::string Name; |
| 390 | // If address is given then create a section at that address else create a |
| 391 | // section by section name specified in GV. |
| 392 | PIC16Section *FoundROSec = NULL; |
| 393 | if (Addr.empty()) { |
| 394 | Name = GV->getSection() + " ROMDATA"; |
| 395 | for (unsigned i = 1; i < ROSections.size(); i++) { |
| 396 | if (ROSections[i]->S_->getName() == Name) { |
| 397 | FoundROSec = ROSections[i]; |
| 398 | break; |
| 399 | } |
| 400 | } |
Chris Lattner | a46cb52 | 2009-07-21 17:20:18 +0000 | [diff] [blame] | 401 | } else { |
Daniel Dunbar | f6ccee5 | 2009-07-24 08:24:36 +0000 | [diff] [blame] | 402 | std::string Prefix = GV->getNameStr() + "." + Addr + "."; |
Sanjiv Gupta | 505996f | 2009-07-06 10:18:37 +0000 | [diff] [blame] | 403 | Name = PAN::getRomdataSectionName(ROSections.size(), Prefix) + " " + Addr; |
| 404 | } |
| 405 | |
| 406 | PIC16Section *NewRomSec = FoundROSec; |
| 407 | if (NewRomSec == NULL) { |
Chris Lattner | a46cb52 | 2009-07-21 17:20:18 +0000 | [diff] [blame] | 408 | const Section *NewSection = getNamedSection(Name.c_str()); |
Sanjiv Gupta | 505996f | 2009-07-06 10:18:37 +0000 | [diff] [blame] | 409 | NewRomSec = new PIC16Section(NewSection); |
| 410 | ROSections.push_back(NewRomSec); |
| 411 | } |
| 412 | |
| 413 | // Insert the GV into this ROM Section. |
| 414 | NewRomSec->Items.push_back(GV); |
| 415 | return NewRomSec->S_; |
| 416 | } |
| 417 | |