PIC16TargetObjectFile.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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. #include "PIC16TargetObjectFile.h"
  10. #include "MCSectionPIC16.h"
  11. #include "PIC16ISelLowering.h"
  12. #include "PIC16TargetMachine.h"
  13. #include "llvm/DerivedTypes.h"
  14. #include "llvm/Module.h"
  15. #include "llvm/MC/MCSection.h"
  16. #include "llvm/MC/MCContext.h"
  17. #include "llvm/Support/raw_ostream.h"
  18. using namespace llvm;
  19. MCSectionPIC16 *MCSectionPIC16::Create(const StringRef &Name,
  20. SectionKind K, MCContext &Ctx) {
  21. return new (Ctx) MCSectionPIC16(Name, K);
  22. }
  23. void MCSectionPIC16::PrintSwitchToSection(const MCAsmInfo &TAI,
  24. raw_ostream &OS) const {
  25. OS << getName() << '\n';
  26. }
  27. PIC16TargetObjectFile::PIC16TargetObjectFile()
  28. : ExternalVarDecls(0), ExternalVarDefs(0) {
  29. }
  30. const MCSectionPIC16 *PIC16TargetObjectFile::
  31. getPIC16Section(const char *Name, SectionKind Kind) const {
  32. MCSectionPIC16 *&Entry = SectionsByName[Name];
  33. if (Entry)
  34. return Entry;
  35. return Entry = MCSectionPIC16::Create(Name, Kind, getContext());
  36. }
  37. void PIC16TargetObjectFile::Initialize(MCContext &Ctx, const TargetMachine &tm){
  38. TargetLoweringObjectFile::Initialize(Ctx, tm);
  39. TM = &tm;
  40. BSSSection = getPIC16Section("udata.# UDATA", SectionKind::getBSS());
  41. ReadOnlySection = getPIC16Section("romdata.# ROMDATA",
  42. SectionKind::getReadOnly());
  43. DataSection = getPIC16Section("idata.# IDATA", SectionKind::getDataRel());
  44. // Need because otherwise a .text symbol is emitted by DwarfWriter
  45. // in BeginModule, and gpasm cribbs for that .text symbol.
  46. TextSection = getPIC16Section("", SectionKind::getText());
  47. ROSections.push_back(new PIC16Section((MCSectionPIC16*)ReadOnlySection));
  48. // FIXME: I don't know what the classification of these sections really is.
  49. ExternalVarDecls = new PIC16Section(getPIC16Section("ExternalVarDecls",
  50. SectionKind::getMetadata()));
  51. ExternalVarDefs = new PIC16Section(getPIC16Section("ExternalVarDefs",
  52. SectionKind::getMetadata()));
  53. }
  54. const MCSection *PIC16TargetObjectFile::
  55. getSectionForFunction(const std::string &FnName) const {
  56. std::string T = PAN::getCodeSectionName(FnName);
  57. return getPIC16Section(T.c_str(), SectionKind::getText());
  58. }
  59. const MCSection *PIC16TargetObjectFile::
  60. getSectionForFunctionFrame(const std::string &FnName) const {
  61. std::string T = PAN::getFrameSectionName(FnName);
  62. return getPIC16Section(T.c_str(), SectionKind::getDataRel());
  63. }
  64. const MCSection *
  65. PIC16TargetObjectFile::getBSSSectionForGlobal(const GlobalVariable *GV) const {
  66. assert(GV->hasInitializer() && "This global doesn't need space");
  67. Constant *C = GV->getInitializer();
  68. assert(C->isNullValue() && "Unitialized globals has non-zero initializer");
  69. // Find how much space this global needs.
  70. const TargetData *TD = TM->getTargetData();
  71. const Type *Ty = C->getType();
  72. unsigned ValSize = TD->getTypeAllocSize(Ty);
  73. // Go through all BSS Sections and assign this variable
  74. // to the first available section having enough space.
  75. PIC16Section *FoundBSS = NULL;
  76. for (unsigned i = 0; i < BSSSections.size(); i++) {
  77. if (DataBankSize - BSSSections[i]->Size >= ValSize) {
  78. FoundBSS = BSSSections[i];
  79. break;
  80. }
  81. }
  82. // No BSS section spacious enough was found. Crate a new one.
  83. if (!FoundBSS) {
  84. std::string name = PAN::getUdataSectionName(BSSSections.size());
  85. const MCSectionPIC16 *NewSection
  86. = getPIC16Section(name.c_str(), /*FIXME*/ SectionKind::getMetadata());
  87. FoundBSS = new PIC16Section(NewSection);
  88. // Add this newly created BSS section to the list of BSSSections.
  89. BSSSections.push_back(FoundBSS);
  90. }
  91. // Insert the GV into this BSS.
  92. FoundBSS->Items.push_back(GV);
  93. FoundBSS->Size += ValSize;
  94. return FoundBSS->S_;
  95. }
  96. const MCSection *
  97. PIC16TargetObjectFile::getIDATASectionForGlobal(const GlobalVariable *GV) const{
  98. assert(GV->hasInitializer() && "This global doesn't need space");
  99. Constant *C = GV->getInitializer();
  100. assert(!C->isNullValue() && "initialized globals has zero initializer");
  101. assert(GV->getType()->getAddressSpace() == PIC16ISD::RAM_SPACE &&
  102. "can split initialized RAM data only");
  103. // Find how much space this global needs.
  104. const TargetData *TD = TM->getTargetData();
  105. const Type *Ty = C->getType();
  106. unsigned ValSize = TD->getTypeAllocSize(Ty);
  107. // Go through all IDATA Sections and assign this variable
  108. // to the first available section having enough space.
  109. PIC16Section *FoundIDATA = NULL;
  110. for (unsigned i = 0; i < IDATASections.size(); i++) {
  111. if (DataBankSize - IDATASections[i]->Size >= ValSize) {
  112. FoundIDATA = IDATASections[i];
  113. break;
  114. }
  115. }
  116. // No IDATA section spacious enough was found. Crate a new one.
  117. if (!FoundIDATA) {
  118. std::string name = PAN::getIdataSectionName(IDATASections.size());
  119. const MCSectionPIC16 *NewSection =
  120. getPIC16Section(name.c_str(), /*FIXME*/ SectionKind::getMetadata());
  121. FoundIDATA = new PIC16Section(NewSection);
  122. // Add this newly created IDATA section to the list of IDATASections.
  123. IDATASections.push_back(FoundIDATA);
  124. }
  125. // Insert the GV into this IDATA.
  126. FoundIDATA->Items.push_back(GV);
  127. FoundIDATA->Size += ValSize;
  128. return FoundIDATA->S_;
  129. }
  130. // Get the section for an automatic variable of a function.
  131. // For PIC16 they are globals only with mangled names.
  132. const MCSection *
  133. PIC16TargetObjectFile::getSectionForAuto(const GlobalVariable *GV) const {
  134. const std::string name = PAN::getSectionNameForSym(GV->getName());
  135. // Go through all Auto Sections and assign this variable
  136. // to the appropriate section.
  137. PIC16Section *FoundAutoSec = NULL;
  138. for (unsigned i = 0; i < AutosSections.size(); i++) {
  139. if (AutosSections[i]->S_->getName() == name) {
  140. FoundAutoSec = AutosSections[i];
  141. break;
  142. }
  143. }
  144. // No Auto section was found. Crate a new one.
  145. if (!FoundAutoSec) {
  146. const MCSectionPIC16 *NewSection =
  147. getPIC16Section(name.c_str(), /*FIXME*/ SectionKind::getMetadata());
  148. FoundAutoSec = new PIC16Section(NewSection);
  149. // Add this newly created autos section to the list of AutosSections.
  150. AutosSections.push_back(FoundAutoSec);
  151. }
  152. // Insert the auto into this section.
  153. FoundAutoSec->Items.push_back(GV);
  154. return FoundAutoSec->S_;
  155. }
  156. // Override default implementation to put the true globals into
  157. // multiple data sections if required.
  158. const MCSection *
  159. PIC16TargetObjectFile::SelectSectionForGlobal(const GlobalValue *GV1,
  160. SectionKind Kind,
  161. Mangler *Mang,
  162. const TargetMachine &TM) const {
  163. // We select the section based on the initializer here, so it really
  164. // has to be a GlobalVariable.
  165. const GlobalVariable *GV = dyn_cast<GlobalVariable>(GV1);
  166. if (!GV)
  167. return TargetLoweringObjectFile::SelectSectionForGlobal(GV1, Kind, Mang,TM);
  168. // Record External Var Decls.
  169. if (GV->isDeclaration()) {
  170. ExternalVarDecls->Items.push_back(GV);
  171. return ExternalVarDecls->S_;
  172. }
  173. assert(GV->hasInitializer() && "A def without initializer?");
  174. // First, if this is an automatic variable for a function, get the section
  175. // name for it and return.
  176. std::string name = GV->getName();
  177. if (PAN::isLocalName(name))
  178. return getSectionForAuto(GV);
  179. // Record Exteranl Var Defs.
  180. if (GV->hasExternalLinkage() || GV->hasCommonLinkage())
  181. ExternalVarDefs->Items.push_back(GV);
  182. // See if this is an uninitialized global.
  183. const Constant *C = GV->getInitializer();
  184. if (C->isNullValue())
  185. return getBSSSectionForGlobal(GV);
  186. // If this is initialized data in RAM. Put it in the correct IDATA section.
  187. if (GV->getType()->getAddressSpace() == PIC16ISD::RAM_SPACE)
  188. return getIDATASectionForGlobal(GV);
  189. // This is initialized data in rom, put it in the readonly section.
  190. if (GV->getType()->getAddressSpace() == PIC16ISD::ROM_SPACE)
  191. return getROSectionForGlobal(GV);
  192. // Else let the default implementation take care of it.
  193. return TargetLoweringObjectFile::SelectSectionForGlobal(GV, Kind, Mang,TM);
  194. }
  195. PIC16TargetObjectFile::~PIC16TargetObjectFile() {
  196. for (unsigned i = 0; i < BSSSections.size(); i++)
  197. delete BSSSections[i];
  198. for (unsigned i = 0; i < IDATASections.size(); i++)
  199. delete IDATASections[i];
  200. for (unsigned i = 0; i < AutosSections.size(); i++)
  201. delete AutosSections[i];
  202. for (unsigned i = 0; i < ROSections.size(); i++)
  203. delete ROSections[i];
  204. delete ExternalVarDecls;
  205. delete ExternalVarDefs;
  206. }
  207. /// getSpecialCasedSectionGlobals - Allow the target to completely override
  208. /// section assignment of a global.
  209. const MCSection *PIC16TargetObjectFile::
  210. getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
  211. Mangler *Mang, const TargetMachine &TM) const {
  212. assert(GV->hasSection());
  213. if (const GlobalVariable *GVar = cast<GlobalVariable>(GV)) {
  214. std::string SectName = GVar->getSection();
  215. // If address for a variable is specified, get the address and create
  216. // section.
  217. std::string AddrStr = "Address=";
  218. if (SectName.compare(0, AddrStr.length(), AddrStr) == 0) {
  219. std::string SectAddr = SectName.substr(AddrStr.length());
  220. return CreateSectionForGlobal(GVar, Mang, SectAddr);
  221. }
  222. // Create the section specified with section attribute.
  223. return CreateSectionForGlobal(GVar, Mang);
  224. }
  225. return getPIC16Section(GV->getSection().c_str(), Kind);
  226. }
  227. // Create a new section for global variable. If Addr is given then create
  228. // section at that address else create by name.
  229. const MCSection *
  230. PIC16TargetObjectFile::CreateSectionForGlobal(const GlobalVariable *GV,
  231. Mangler *Mang,
  232. const std::string &Addr) const {
  233. // See if this is an uninitialized global.
  234. const Constant *C = GV->getInitializer();
  235. if (C->isNullValue())
  236. return CreateBSSSectionForGlobal(GV, Addr);
  237. // If this is initialized data in RAM. Put it in the correct IDATA section.
  238. if (GV->getType()->getAddressSpace() == PIC16ISD::RAM_SPACE)
  239. return CreateIDATASectionForGlobal(GV, Addr);
  240. // This is initialized data in rom, put it in the readonly section.
  241. if (GV->getType()->getAddressSpace() == PIC16ISD::ROM_SPACE)
  242. return CreateROSectionForGlobal(GV, Addr);
  243. // Else let the default implementation take care of it.
  244. return TargetLoweringObjectFile::SectionForGlobal(GV, Mang, *TM);
  245. }
  246. // Create uninitialized section for a variable.
  247. const MCSection *
  248. PIC16TargetObjectFile::CreateBSSSectionForGlobal(const GlobalVariable *GV,
  249. std::string Addr) const {
  250. assert(GV->hasInitializer() && "This global doesn't need space");
  251. assert(GV->getInitializer()->isNullValue() &&
  252. "Unitialized global has non-zero initializer");
  253. std::string Name;
  254. // If address is given then create a section at that address else create a
  255. // section by section name specified in GV.
  256. PIC16Section *FoundBSS = NULL;
  257. if (Addr.empty()) {
  258. Name = GV->getSection() + " UDATA";
  259. for (unsigned i = 0; i < BSSSections.size(); i++) {
  260. if (BSSSections[i]->S_->getName() == Name) {
  261. FoundBSS = BSSSections[i];
  262. break;
  263. }
  264. }
  265. } else {
  266. std::string Prefix = GV->getNameStr() + "." + Addr + ".";
  267. Name = PAN::getUdataSectionName(BSSSections.size(), Prefix) + " " + Addr;
  268. }
  269. PIC16Section *NewBSS = FoundBSS;
  270. if (NewBSS == NULL) {
  271. const MCSectionPIC16 *NewSection =
  272. getPIC16Section(Name.c_str(), SectionKind::getBSS());
  273. NewBSS = new PIC16Section(NewSection);
  274. BSSSections.push_back(NewBSS);
  275. }
  276. // Insert the GV into this BSS.
  277. NewBSS->Items.push_back(GV);
  278. // We do not want to put any GV without explicit section into this section
  279. // so set its size to DatabankSize.
  280. NewBSS->Size = DataBankSize;
  281. return NewBSS->S_;
  282. }
  283. // Get rom section for a variable. Currently there can be only one rom section
  284. // unless a variable explicitly requests a section.
  285. const MCSection *
  286. PIC16TargetObjectFile::getROSectionForGlobal(const GlobalVariable *GV) const {
  287. ROSections[0]->Items.push_back(GV);
  288. return ROSections[0]->S_;
  289. }
  290. // Create initialized data section for a variable.
  291. const MCSection *
  292. PIC16TargetObjectFile::CreateIDATASectionForGlobal(const GlobalVariable *GV,
  293. std::string Addr) const {
  294. assert(GV->hasInitializer() && "This global doesn't need space");
  295. assert(!GV->getInitializer()->isNullValue() &&
  296. "initialized global has zero initializer");
  297. assert(GV->getType()->getAddressSpace() == PIC16ISD::RAM_SPACE &&
  298. "can be used for initialized RAM data only");
  299. std::string Name;
  300. // If address is given then create a section at that address else create a
  301. // section by section name specified in GV.
  302. PIC16Section *FoundIDATASec = NULL;
  303. if (Addr.empty()) {
  304. Name = GV->getSection() + " IDATA";
  305. for (unsigned i = 0; i < IDATASections.size(); i++) {
  306. if (IDATASections[i]->S_->getName() == Name) {
  307. FoundIDATASec = IDATASections[i];
  308. break;
  309. }
  310. }
  311. } else {
  312. std::string Prefix = GV->getNameStr() + "." + Addr + ".";
  313. Name = PAN::getIdataSectionName(IDATASections.size(), Prefix) + " " + Addr;
  314. }
  315. PIC16Section *NewIDATASec = FoundIDATASec;
  316. if (NewIDATASec == NULL) {
  317. const MCSectionPIC16 *NewSection =
  318. getPIC16Section(Name.c_str(), /* FIXME */SectionKind::getMetadata());
  319. NewIDATASec = new PIC16Section(NewSection);
  320. IDATASections.push_back(NewIDATASec);
  321. }
  322. // Insert the GV into this IDATA Section.
  323. NewIDATASec->Items.push_back(GV);
  324. // We do not want to put any GV without explicit section into this section
  325. // so set its size to DatabankSize.
  326. NewIDATASec->Size = DataBankSize;
  327. return NewIDATASec->S_;
  328. }
  329. // Create a section in rom for a variable.
  330. const MCSection *
  331. PIC16TargetObjectFile::CreateROSectionForGlobal(const GlobalVariable *GV,
  332. std::string Addr) const {
  333. assert(GV->getType()->getAddressSpace() == PIC16ISD::ROM_SPACE &&
  334. "can be used for ROM data only");
  335. std::string Name;
  336. // If address is given then create a section at that address else create a
  337. // section by section name specified in GV.
  338. PIC16Section *FoundROSec = NULL;
  339. if (Addr.empty()) {
  340. Name = GV->getSection() + " ROMDATA";
  341. for (unsigned i = 1; i < ROSections.size(); i++) {
  342. if (ROSections[i]->S_->getName() == Name) {
  343. FoundROSec = ROSections[i];
  344. break;
  345. }
  346. }
  347. } else {
  348. std::string Prefix = GV->getNameStr() + "." + Addr + ".";
  349. Name = PAN::getRomdataSectionName(ROSections.size(), Prefix) + " " + Addr;
  350. }
  351. PIC16Section *NewRomSec = FoundROSec;
  352. if (NewRomSec == NULL) {
  353. const MCSectionPIC16 *NewSection =
  354. getPIC16Section(Name.c_str(), SectionKind::getReadOnly());
  355. NewRomSec = new PIC16Section(NewSection);
  356. ROSections.push_back(NewRomSec);
  357. }
  358. // Insert the GV into this ROM Section.
  359. NewRomSec->Items.push_back(GV);
  360. return NewRomSec->S_;
  361. }