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