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 "; |
| 33 | RomData8bitsDirective = " dw "; |
| 34 | RomData16bitsDirective = " rom_di "; |
Sanjiv Gupta | 3b020fe | 2009-02-02 16:53:06 +0000 | [diff] [blame] | 35 | RomData32bitsDirective = " rom_dl "; |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 36 | ZeroDirective = NULL; |
Sanjiv Gupta | 1b04694 | 2009-01-13 19:18:47 +0000 | [diff] [blame] | 37 | AsciiDirective = " dt "; |
| 38 | AscizDirective = NULL; |
| 39 | BSSSection_ = getNamedSection("udata.# UDATA", |
| 40 | SectionFlags::Writeable | SectionFlags::BSS); |
| 41 | ReadOnlySection = getNamedSection("romdata.# ROMDATA", SectionFlags::None); |
| 42 | DataSection = getNamedSection("idata.# IDATA", SectionFlags::Writeable); |
| 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. |
| 46 | TextSection = getUnnamedSection("", SectionFlags::Code); |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 47 | } |
Sanjiv Gupta | c8d7bc8 | 2009-01-30 04:25:10 +0000 | [diff] [blame] | 48 | |
Sanjiv Gupta | 3b020fe | 2009-02-02 16:53:06 +0000 | [diff] [blame] | 49 | const char *PIC16TargetAsmInfo::getRomDirective(unsigned size) const |
| 50 | { |
| 51 | if (size == 8) |
| 52 | return RomData8bitsDirective; |
| 53 | else if (size == 16) |
| 54 | return RomData16bitsDirective; |
| 55 | else if (size == 32) |
| 56 | return RomData32bitsDirective; |
| 57 | else |
| 58 | return NULL; |
| 59 | } |
Sanjiv Gupta | c8d7bc8 | 2009-01-30 04:25:10 +0000 | [diff] [blame] | 60 | |
Sanjiv Gupta | c8d7bc8 | 2009-01-30 04:25:10 +0000 | [diff] [blame] | 61 | |
Sanjiv Gupta | 3b020fe | 2009-02-02 16:53:06 +0000 | [diff] [blame] | 62 | const char *PIC16TargetAsmInfo::getASDirective(unsigned size, |
| 63 | unsigned AS) const { |
| 64 | if (AS == PIC16ISD::ROM_SPACE) |
| 65 | return getRomDirective(size); |
| 66 | else |
| 67 | return NULL; |
| 68 | } |
Sanjiv Gupta | c8d7bc8 | 2009-01-30 04:25:10 +0000 | [diff] [blame] | 69 | |
Sanjiv Gupta | d8d27f4 | 2009-05-06 08:02:01 +0000 | [diff] [blame] | 70 | const Section * |
| 71 | PIC16TargetAsmInfo::getBSSSectionForGlobal(const GlobalVariable *GV) const { |
| 72 | assert (GV->hasInitializer() && "This global doesn't need space"); |
| 73 | Constant *C = GV->getInitializer(); |
| 74 | assert (C->isNullValue() && "Unitialized globals has non-zero initializer"); |
| 75 | |
| 76 | // Find how much space this global needs. |
| 77 | const TargetData *TD = TM.getTargetData(); |
| 78 | const Type *Ty = C->getType(); |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 79 | unsigned ValSize = TD->getTypeAllocSize(Ty); |
Sanjiv Gupta | d8d27f4 | 2009-05-06 08:02:01 +0000 | [diff] [blame] | 80 | |
| 81 | // Go through all BSS Sections and assign this variable |
| 82 | // to the first available section having enough space. |
| 83 | PIC16Section *FoundBSS = NULL; |
| 84 | for (unsigned i = 0; i < BSSSections.size(); i++) { |
| 85 | if (DataBankSize - BSSSections[i]->Size >= ValSize) { |
| 86 | FoundBSS = BSSSections[i]; |
| 87 | break; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | // No BSS section spacious enough was found. Crate a new one. |
| 92 | if (! FoundBSS) { |
Sanjiv Gupta | 211f362 | 2009-05-10 05:23:47 +0000 | [diff] [blame^] | 93 | std::string name = PAN::getUdataSectionName(BSSSections.size()); |
| 94 | const Section *NewSection = getNamedSection (name.c_str()); |
Sanjiv Gupta | d8d27f4 | 2009-05-06 08:02:01 +0000 | [diff] [blame] | 95 | |
| 96 | FoundBSS = new PIC16Section(NewSection); |
| 97 | |
| 98 | // Add this newly created BSS section to the list of BSSSections. |
| 99 | BSSSections.push_back(FoundBSS); |
| 100 | } |
| 101 | |
| 102 | // Insert the GV into this BSS. |
| 103 | FoundBSS->Items.push_back(GV); |
| 104 | FoundBSS->Size += ValSize; |
| 105 | |
| 106 | // We can't do this here because GV is const . |
| 107 | // const std::string SName = FoundBSS->S_->getName(); |
| 108 | // GV->setSection(SName); |
| 109 | |
| 110 | return FoundBSS->S_; |
| 111 | } |
| 112 | |
| 113 | const Section * |
| 114 | PIC16TargetAsmInfo::getIDATASectionForGlobal(const GlobalVariable *GV) const { |
| 115 | assert (GV->hasInitializer() && "This global doesn't need space"); |
| 116 | Constant *C = GV->getInitializer(); |
| 117 | assert (!C->isNullValue() && "initialized globals has zero initializer"); |
| 118 | assert (GV->getType()->getAddressSpace() == PIC16ISD::RAM_SPACE && |
| 119 | "can split initialized RAM data only"); |
| 120 | |
| 121 | // Find how much space this global needs. |
| 122 | const TargetData *TD = TM.getTargetData(); |
| 123 | const Type *Ty = C->getType(); |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 124 | unsigned ValSize = TD->getTypeAllocSize(Ty); |
Sanjiv Gupta | d8d27f4 | 2009-05-06 08:02:01 +0000 | [diff] [blame] | 125 | |
| 126 | // Go through all IDATA Sections and assign this variable |
| 127 | // to the first available section having enough space. |
| 128 | PIC16Section *FoundIDATA = NULL; |
| 129 | for (unsigned i = 0; i < IDATASections.size(); i++) { |
| 130 | if ( DataBankSize - IDATASections[i]->Size >= ValSize) { |
| 131 | FoundIDATA = IDATASections[i]; |
| 132 | break; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | // No IDATA section spacious enough was found. Crate a new one. |
| 137 | if (! FoundIDATA) { |
Sanjiv Gupta | 211f362 | 2009-05-10 05:23:47 +0000 | [diff] [blame^] | 138 | std::string name = PAN::getIdataSectionName(IDATASections.size()); |
| 139 | const Section *NewSection = getNamedSection (name.c_str()); |
Sanjiv Gupta | d8d27f4 | 2009-05-06 08:02:01 +0000 | [diff] [blame] | 140 | |
| 141 | FoundIDATA = new PIC16Section(NewSection); |
| 142 | |
| 143 | // Add this newly created IDATA section to the list of IDATASections. |
| 144 | IDATASections.push_back(FoundIDATA); |
| 145 | } |
| 146 | |
| 147 | // Insert the GV into this IDATA. |
| 148 | FoundIDATA->Items.push_back(GV); |
| 149 | FoundIDATA->Size += ValSize; |
| 150 | |
| 151 | // We can't do this here because GV is const . |
| 152 | // GV->setSection(FoundIDATA->S->getName()); |
| 153 | |
| 154 | return FoundIDATA->S_; |
| 155 | } |
| 156 | |
| 157 | // Override default implementation to put the true globals into |
| 158 | // multiple data sections if required. |
| 159 | const Section* |
| 160 | PIC16TargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV1) const { |
| 161 | // We select the section based on the initializer here, so it really |
| 162 | // has to be a GlobalVariable. |
Sanjiv Gupta | 211f362 | 2009-05-10 05:23:47 +0000 | [diff] [blame^] | 163 | const GlobalVariable *GV = dyn_cast<GlobalVariable>(GV1); |
| 164 | if (!GV1 || ! GV->hasInitializer()) |
Sanjiv Gupta | d8d27f4 | 2009-05-06 08:02:01 +0000 | [diff] [blame] | 165 | return TargetAsmInfo::SelectSectionForGlobal(GV1); |
| 166 | |
Sanjiv Gupta | 211f362 | 2009-05-10 05:23:47 +0000 | [diff] [blame^] | 167 | // First, if this is an automatic variable for a function, get the section |
| 168 | // name for it and return. |
| 169 | const std::string name = GV->getName(); |
| 170 | if (PAN::isLocalName(name)) { |
| 171 | const std::string Sec_Name = PAN::getSectionNameForSym(name); |
| 172 | return getNamedSection(Sec_Name.c_str()); |
| 173 | } |
Sanjiv Gupta | d8d27f4 | 2009-05-06 08:02:01 +0000 | [diff] [blame] | 174 | |
Sanjiv Gupta | d8d27f4 | 2009-05-06 08:02:01 +0000 | [diff] [blame] | 175 | // See if this is an uninitialized global. |
Sanjiv Gupta | 211f362 | 2009-05-10 05:23:47 +0000 | [diff] [blame^] | 176 | const Constant *C = GV->getInitializer(); |
Sanjiv Gupta | d8d27f4 | 2009-05-06 08:02:01 +0000 | [diff] [blame] | 177 | if (C->isNullValue()) |
| 178 | return getBSSSectionForGlobal(GV); |
| 179 | |
| 180 | // This is initialized data. We only deal with initialized data in RAM. |
| 181 | if (GV->getType()->getAddressSpace() == PIC16ISD::RAM_SPACE) |
| 182 | return getIDATASectionForGlobal(GV); |
| 183 | |
Sanjiv Gupta | 211f362 | 2009-05-10 05:23:47 +0000 | [diff] [blame^] | 184 | |
Sanjiv Gupta | d8d27f4 | 2009-05-06 08:02:01 +0000 | [diff] [blame] | 185 | // Else let the default implementation take care of it. |
| 186 | return TargetAsmInfo::SelectSectionForGlobal(GV); |
| 187 | } |
| 188 | |
Sanjiv Gupta | d8d27f4 | 2009-05-06 08:02:01 +0000 | [diff] [blame] | 189 | PIC16TargetAsmInfo::~PIC16TargetAsmInfo() { |
| 190 | |
| 191 | for (unsigned i = 0; i < BSSSections.size(); i++) { |
| 192 | delete BSSSections[i]; |
| 193 | } |
| 194 | |
| 195 | for (unsigned i = 0; i < IDATASections.size(); i++) { |
| 196 | delete IDATASections[i]; |
| 197 | } |
| 198 | } |