LLVM API Documentation
00001 //===-- PIC16TargetObjectFile.cpp - PIC16 object files --------------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 00010 #include "PIC16TargetObjectFile.h" 00011 #include "MCSectionPIC16.h" 00012 #include "PIC16ISelLowering.h" 00013 #include "PIC16TargetMachine.h" 00014 #include "llvm/DerivedTypes.h" 00015 #include "llvm/Module.h" 00016 #include "llvm/MC/MCSection.h" 00017 #include "llvm/MC/MCContext.h" 00018 #include "llvm/Support/raw_ostream.h" 00019 using namespace llvm; 00020 00021 00022 MCSectionPIC16 *MCSectionPIC16::Create(const StringRef &Name, 00023 SectionKind K, MCContext &Ctx) { 00024 return new (Ctx) MCSectionPIC16(Name, K); 00025 } 00026 00027 00028 void MCSectionPIC16::PrintSwitchToSection(const TargetAsmInfo &TAI, 00029 raw_ostream &OS) const { 00030 OS << getName() << '\n'; 00031 } 00032 00033 00034 00035 00036 PIC16TargetObjectFile::PIC16TargetObjectFile() 00037 : ExternalVarDecls(0), ExternalVarDefs(0) { 00038 } 00039 00040 const MCSectionPIC16 *PIC16TargetObjectFile:: 00041 getPIC16Section(const char *Name, SectionKind Kind) const { 00042 MCSectionPIC16 *&Entry = SectionsByName[Name]; 00043 if (Entry) 00044 return Entry; 00045 00046 return Entry = MCSectionPIC16::Create(Name, Kind, getContext()); 00047 } 00048 00049 00050 void PIC16TargetObjectFile::Initialize(MCContext &Ctx, const TargetMachine &tm){ 00051 TargetLoweringObjectFile::Initialize(Ctx, tm); 00052 TM = &tm; 00053 00054 BSSSection = getPIC16Section("udata.# UDATA", SectionKind::getBSS()); 00055 ReadOnlySection = getPIC16Section("romdata.# ROMDATA", 00056 SectionKind::getReadOnly()); 00057 DataSection = getPIC16Section("idata.# IDATA", SectionKind::getDataRel()); 00058 00059 // Need because otherwise a .text symbol is emitted by DwarfWriter 00060 // in BeginModule, and gpasm cribbs for that .text symbol. 00061 TextSection = getPIC16Section("", SectionKind::getText()); 00062 00063 ROSections.push_back(new PIC16Section((MCSectionPIC16*)ReadOnlySection)); 00064 00065 // FIXME: I don't know what the classification of these sections really is. 00066 ExternalVarDecls = new PIC16Section(getPIC16Section("ExternalVarDecls", 00067 SectionKind::getMetadata())); 00068 ExternalVarDefs = new PIC16Section(getPIC16Section("ExternalVarDefs", 00069 SectionKind::getMetadata())); 00070 } 00071 00072 const MCSection *PIC16TargetObjectFile:: 00073 getSectionForFunction(const std::string &FnName) const { 00074 std::string T = PAN::getCodeSectionName(FnName); 00075 return getPIC16Section(T.c_str(), SectionKind::getText()); 00076 } 00077 00078 00079 const MCSection *PIC16TargetObjectFile:: 00080 getSectionForFunctionFrame(const std::string &FnName) const { 00081 std::string T = PAN::getFrameSectionName(FnName); 00082 return getPIC16Section(T.c_str(), SectionKind::getDataRel()); 00083 } 00084 00085 const MCSection * 00086 PIC16TargetObjectFile::getBSSSectionForGlobal(const GlobalVariable *GV) const { 00087 assert(GV->hasInitializer() && "This global doesn't need space"); 00088 Constant *C = GV->getInitializer(); 00089 assert(C->isNullValue() && "Unitialized globals has non-zero initializer"); 00090 00091 // Find how much space this global needs. 00092 const TargetData *TD = TM->getTargetData(); 00093 const Type *Ty = C->getType(); 00094 unsigned ValSize = TD->getTypeAllocSize(Ty); 00095 00096 // Go through all BSS Sections and assign this variable 00097 // to the first available section having enough space. 00098 PIC16Section *FoundBSS = NULL; 00099 for (unsigned i = 0; i < BSSSections.size(); i++) { 00100 if (DataBankSize - BSSSections[i]->Size >= ValSize) { 00101 FoundBSS = BSSSections[i]; 00102 break; 00103 } 00104 } 00105 00106 // No BSS section spacious enough was found. Crate a new one. 00107 if (!FoundBSS) { 00108 std::string name = PAN::getUdataSectionName(BSSSections.size()); 00109 const MCSectionPIC16 *NewSection 00110 = getPIC16Section(name.c_str(), /*FIXME*/ SectionKind::getMetadata()); 00111 00112 FoundBSS = new PIC16Section(NewSection); 00113 00114 // Add this newly created BSS section to the list of BSSSections. 00115 BSSSections.push_back(FoundBSS); 00116 } 00117 00118 // Insert the GV into this BSS. 00119 FoundBSS->Items.push_back(GV); 00120 FoundBSS->Size += ValSize; 00121 return FoundBSS->S_; 00122 } 00123 00124 const MCSection * 00125 PIC16TargetObjectFile::getIDATASectionForGlobal(const GlobalVariable *GV) const{ 00126 assert(GV->hasInitializer() && "This global doesn't need space"); 00127 Constant *C = GV->getInitializer(); 00128 assert(!C->isNullValue() && "initialized globals has zero initializer"); 00129 assert(GV->getType()->getAddressSpace() == PIC16ISD::RAM_SPACE && 00130 "can split initialized RAM data only"); 00131 00132 // Find how much space this global needs. 00133 const TargetData *TD = TM->getTargetData(); 00134 const Type *Ty = C->getType(); 00135 unsigned ValSize = TD->getTypeAllocSize(Ty); 00136 00137 // Go through all IDATA Sections and assign this variable 00138 // to the first available section having enough space. 00139 PIC16Section *FoundIDATA = NULL; 00140 for (unsigned i = 0; i < IDATASections.size(); i++) { 00141 if (DataBankSize - IDATASections[i]->Size >= ValSize) { 00142 FoundIDATA = IDATASections[i]; 00143 break; 00144 } 00145 } 00146 00147 // No IDATA section spacious enough was found. Crate a new one. 00148 if (!FoundIDATA) { 00149 std::string name = PAN::getIdataSectionName(IDATASections.size()); 00150 const MCSectionPIC16 *NewSection = 00151 getPIC16Section(name.c_str(), /*FIXME*/ SectionKind::getMetadata()); 00152 00153 FoundIDATA = new PIC16Section(NewSection); 00154 00155 // Add this newly created IDATA section to the list of IDATASections. 00156 IDATASections.push_back(FoundIDATA); 00157 } 00158 00159 // Insert the GV into this IDATA. 00160 FoundIDATA->Items.push_back(GV); 00161 FoundIDATA->Size += ValSize; 00162 return FoundIDATA->S_; 00163 } 00164 00165 // Get the section for an automatic variable of a function. 00166 // For PIC16 they are globals only with mangled names. 00167 const MCSection * 00168 PIC16TargetObjectFile::getSectionForAuto(const GlobalVariable *GV) const { 00169 00170 const std::string name = PAN::getSectionNameForSym(GV->getName()); 00171 00172 // Go through all Auto Sections and assign this variable 00173 // to the appropriate section. 00174 PIC16Section *FoundAutoSec = NULL; 00175 for (unsigned i = 0; i < AutosSections.size(); i++) { 00176 if (AutosSections[i]->S_->getName() == name) { 00177 FoundAutoSec = AutosSections[i]; 00178 break; 00179 } 00180 } 00181 00182 // No Auto section was found. Crate a new one. 00183 if (!FoundAutoSec) { 00184 const MCSectionPIC16 *NewSection = 00185 getPIC16Section(name.c_str(), /*FIXME*/ SectionKind::getMetadata()); 00186 00187 FoundAutoSec = new PIC16Section(NewSection); 00188 00189 // Add this newly created autos section to the list of AutosSections. 00190 AutosSections.push_back(FoundAutoSec); 00191 } 00192 00193 // Insert the auto into this section. 00194 FoundAutoSec->Items.push_back(GV); 00195 00196 return FoundAutoSec->S_; 00197 } 00198 00199 00200 // Override default implementation to put the true globals into 00201 // multiple data sections if required. 00202 const MCSection * 00203 PIC16TargetObjectFile::SelectSectionForGlobal(const GlobalValue *GV1, 00204 SectionKind Kind, 00205 Mangler *Mang, 00206 const TargetMachine &TM) const { 00207 // We select the section based on the initializer here, so it really 00208 // has to be a GlobalVariable. 00209 const GlobalVariable *GV = dyn_cast<GlobalVariable>(GV1); 00210 if (!GV) 00211 return TargetLoweringObjectFile::SelectSectionForGlobal(GV1, Kind, Mang,TM); 00212 00213 // Record External Var Decls. 00214 if (GV->isDeclaration()) { 00215 ExternalVarDecls->Items.push_back(GV); 00216 return ExternalVarDecls->S_; 00217 } 00218 00219 assert(GV->hasInitializer() && "A def without initializer?"); 00220 00221 // First, if this is an automatic variable for a function, get the section 00222 // name for it and return. 00223 std::string name = GV->getName(); 00224 if (PAN::isLocalName(name)) 00225 return getSectionForAuto(GV); 00226 00227 // Record Exteranl Var Defs. 00228 if (GV->hasExternalLinkage() || GV->hasCommonLinkage()) 00229 ExternalVarDefs->Items.push_back(GV); 00230 00231 // See if this is an uninitialized global. 00232 const Constant *C = GV->getInitializer(); 00233 if (C->isNullValue()) 00234 return getBSSSectionForGlobal(GV); 00235 00236 // If this is initialized data in RAM. Put it in the correct IDATA section. 00237 if (GV->getType()->getAddressSpace() == PIC16ISD::RAM_SPACE) 00238 return getIDATASectionForGlobal(GV); 00239 00240 // This is initialized data in rom, put it in the readonly section. 00241 if (GV->getType()->getAddressSpace() == PIC16ISD::ROM_SPACE) 00242 return getROSectionForGlobal(GV); 00243 00244 // Else let the default implementation take care of it. 00245 return TargetLoweringObjectFile::SelectSectionForGlobal(GV, Kind, Mang,TM); 00246 } 00247 00248 PIC16TargetObjectFile::~PIC16TargetObjectFile() { 00249 for (unsigned i = 0; i < BSSSections.size(); i++) 00250 delete BSSSections[i]; 00251 for (unsigned i = 0; i < IDATASections.size(); i++) 00252 delete IDATASections[i]; 00253 for (unsigned i = 0; i < AutosSections.size(); i++) 00254 delete AutosSections[i]; 00255 for (unsigned i = 0; i < ROSections.size(); i++) 00256 delete ROSections[i]; 00257 delete ExternalVarDecls; 00258 delete ExternalVarDefs; 00259 } 00260 00261 00262 /// getSpecialCasedSectionGlobals - Allow the target to completely override 00263 /// section assignment of a global. 00264 const MCSection *PIC16TargetObjectFile:: 00265 getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind, 00266 Mangler *Mang, const TargetMachine &TM) const { 00267 assert(GV->hasSection()); 00268 00269 if (const GlobalVariable *GVar = cast<GlobalVariable>(GV)) { 00270 std::string SectName = GVar->getSection(); 00271 // If address for a variable is specified, get the address and create 00272 // section. 00273 std::string AddrStr = "Address="; 00274 if (SectName.compare(0, AddrStr.length(), AddrStr) == 0) { 00275 std::string SectAddr = SectName.substr(AddrStr.length()); 00276 return CreateSectionForGlobal(GVar, Mang, SectAddr); 00277 } 00278 00279 // Create the section specified with section attribute. 00280 return CreateSectionForGlobal(GVar, Mang); 00281 } 00282 00283 return getPIC16Section(GV->getSection().c_str(), Kind); 00284 } 00285 00286 // Create a new section for global variable. If Addr is given then create 00287 // section at that address else create by name. 00288 const MCSection * 00289 PIC16TargetObjectFile::CreateSectionForGlobal(const GlobalVariable *GV, 00290 Mangler *Mang, 00291 const std::string &Addr) const { 00292 // See if this is an uninitialized global. 00293 const Constant *C = GV->getInitializer(); 00294 if (C->isNullValue()) 00295 return CreateBSSSectionForGlobal(GV, Addr); 00296 00297 // If this is initialized data in RAM. Put it in the correct IDATA section. 00298 if (GV->getType()->getAddressSpace() == PIC16ISD::RAM_SPACE) 00299 return CreateIDATASectionForGlobal(GV, Addr); 00300 00301 // This is initialized data in rom, put it in the readonly section. 00302 if (GV->getType()->getAddressSpace() == PIC16ISD::ROM_SPACE) 00303 return CreateROSectionForGlobal(GV, Addr); 00304 00305 // Else let the default implementation take care of it. 00306 return TargetLoweringObjectFile::SectionForGlobal(GV, Mang, *TM); 00307 } 00308 00309 // Create uninitialized section for a variable. 00310 const MCSection * 00311 PIC16TargetObjectFile::CreateBSSSectionForGlobal(const GlobalVariable *GV, 00312 std::string Addr) const { 00313 assert(GV->hasInitializer() && "This global doesn't need space"); 00314 assert(GV->getInitializer()->isNullValue() && 00315 "Unitialized global has non-zero initializer"); 00316 std::string Name; 00317 // If address is given then create a section at that address else create a 00318 // section by section name specified in GV. 00319 PIC16Section *FoundBSS = NULL; 00320 if (Addr.empty()) { 00321 Name = GV->getSection() + " UDATA"; 00322 for (unsigned i = 0; i < BSSSections.size(); i++) { 00323 if (BSSSections[i]->S_->getName() == Name) { 00324 FoundBSS = BSSSections[i]; 00325 break; 00326 } 00327 } 00328 } else { 00329 std::string Prefix = GV->getNameStr() + "." + Addr + "."; 00330 Name = PAN::getUdataSectionName(BSSSections.size(), Prefix) + " " + Addr; 00331 } 00332 00333 PIC16Section *NewBSS = FoundBSS; 00334 if (NewBSS == NULL) { 00335 const MCSectionPIC16 *NewSection = 00336 getPIC16Section(Name.c_str(), SectionKind::getBSS()); 00337 NewBSS = new PIC16Section(NewSection); 00338 BSSSections.push_back(NewBSS); 00339 } 00340 00341 // Insert the GV into this BSS. 00342 NewBSS->Items.push_back(GV); 00343 00344 // We do not want to put any GV without explicit section into this section 00345 // so set its size to DatabankSize. 00346 NewBSS->Size = DataBankSize; 00347 return NewBSS->S_; 00348 } 00349 00350 // Get rom section for a variable. Currently there can be only one rom section 00351 // unless a variable explicitly requests a section. 00352 const MCSection * 00353 PIC16TargetObjectFile::getROSectionForGlobal(const GlobalVariable *GV) const { 00354 ROSections[0]->Items.push_back(GV); 00355 return ROSections[0]->S_; 00356 } 00357 00358 // Create initialized data section for a variable. 00359 const MCSection * 00360 PIC16TargetObjectFile::CreateIDATASectionForGlobal(const GlobalVariable *GV, 00361 std::string Addr) const { 00362 assert(GV->hasInitializer() && "This global doesn't need space"); 00363 assert(!GV->getInitializer()->isNullValue() && 00364 "initialized global has zero initializer"); 00365 assert(GV->getType()->getAddressSpace() == PIC16ISD::RAM_SPACE && 00366 "can be used for initialized RAM data only"); 00367 00368 std::string Name; 00369 // If address is given then create a section at that address else create a 00370 // section by section name specified in GV. 00371 PIC16Section *FoundIDATASec = NULL; 00372 if (Addr.empty()) { 00373 Name = GV->getSection() + " IDATA"; 00374 for (unsigned i = 0; i < IDATASections.size(); i++) { 00375 if (IDATASections[i]->S_->getName() == Name) { 00376 FoundIDATASec = IDATASections[i]; 00377 break; 00378 } 00379 } 00380 } else { 00381 std::string Prefix = GV->getNameStr() + "." + Addr + "."; 00382 Name = PAN::getIdataSectionName(IDATASections.size(), Prefix) + " " + Addr; 00383 } 00384 00385 PIC16Section *NewIDATASec = FoundIDATASec; 00386 if (NewIDATASec == NULL) { 00387 const MCSectionPIC16 *NewSection = 00388 getPIC16Section(Name.c_str(), /* FIXME */SectionKind::getMetadata()); 00389 NewIDATASec = new PIC16Section(NewSection); 00390 IDATASections.push_back(NewIDATASec); 00391 } 00392 // Insert the GV into this IDATA Section. 00393 NewIDATASec->Items.push_back(GV); 00394 // We do not want to put any GV without explicit section into this section 00395 // so set its size to DatabankSize. 00396 NewIDATASec->Size = DataBankSize; 00397 return NewIDATASec->S_; 00398 } 00399 00400 // Create a section in rom for a variable. 00401 const MCSection * 00402 PIC16TargetObjectFile::CreateROSectionForGlobal(const GlobalVariable *GV, 00403 std::string Addr) const { 00404 assert(GV->getType()->getAddressSpace() == PIC16ISD::ROM_SPACE && 00405 "can be used for ROM data only"); 00406 00407 std::string Name; 00408 // If address is given then create a section at that address else create a 00409 // section by section name specified in GV. 00410 PIC16Section *FoundROSec = NULL; 00411 if (Addr.empty()) { 00412 Name = GV->getSection() + " ROMDATA"; 00413 for (unsigned i = 1; i < ROSections.size(); i++) { 00414 if (ROSections[i]->S_->getName() == Name) { 00415 FoundROSec = ROSections[i]; 00416 break; 00417 } 00418 } 00419 } else { 00420 std::string Prefix = GV->getNameStr() + "." + Addr + "."; 00421 Name = PAN::getRomdataSectionName(ROSections.size(), Prefix) + " " + Addr; 00422 } 00423 00424 PIC16Section *NewRomSec = FoundROSec; 00425 if (NewRomSec == NULL) { 00426 const MCSectionPIC16 *NewSection = 00427 getPIC16Section(Name.c_str(), SectionKind::getReadOnly()); 00428 NewRomSec = new PIC16Section(NewSection); 00429 ROSections.push_back(NewRomSec); 00430 } 00431 00432 // Insert the GV into this ROM Section. 00433 NewRomSec->Items.push_back(GV); 00434 return NewRomSec->S_; 00435 } 00436