blob: 3837f9ef7b55305dd86db8604c662d83836d6e55 [file] [log] [blame]
Sanjiv Gupta0e687712008-05-13 09:02:57 +00001//===-- 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 Gohman8f092252008-11-03 18:22:42 +000015#include "PIC16TargetMachine.h"
Sanjiv Gupta1b046942009-01-13 19:18:47 +000016#include "llvm/GlobalValue.h"
Sanjiv Guptad8d27f42009-05-06 08:02:01 +000017#include "llvm/GlobalVariable.h"
18#include "llvm/DerivedTypes.h"
Sanjiv Gupta0e687712008-05-13 09:02:57 +000019
20using namespace llvm;
21
22PIC16TargetAsmInfo::
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000023PIC16TargetAsmInfo(const PIC16TargetMachine &TM)
Dan Gohman8f092252008-11-03 18:22:42 +000024 : TargetAsmInfo(TM) {
Sanjiv Gupta0e687712008-05-13 09:02:57 +000025 CommentString = ";";
Sanjiv Gupta211f3622009-05-10 05:23:47 +000026 GlobalPrefix = PAN::getTagName(PAN::PREFIX_SYMBOL);
27 GlobalDirective = "\tglobal\t";
28 ExternDirective = "\textern\t";
29
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000030 Data8bitsDirective = " db ";
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +000031 Data16bitsDirective = " dw ";
32 Data32bitsDirective = " dl ";
33 RomData8bitsDirective = " dw ";
34 RomData16bitsDirective = " rom_di ";
Sanjiv Gupta3b020fe2009-02-02 16:53:06 +000035 RomData32bitsDirective = " rom_dl ";
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000036 ZeroDirective = NULL;
Sanjiv Gupta1b046942009-01-13 19:18:47 +000037 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 Guptac1fa70c2009-04-08 06:24:04 +000044 // 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 Gupta0e687712008-05-13 09:02:57 +000047}
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +000048
Sanjiv Gupta3b020fe2009-02-02 16:53:06 +000049const 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 Guptac8d7bc82009-01-30 04:25:10 +000060
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +000061
Sanjiv Gupta3b020fe2009-02-02 16:53:06 +000062const 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 Guptac8d7bc82009-01-30 04:25:10 +000069
Sanjiv Guptad8d27f42009-05-06 08:02:01 +000070const Section *
71PIC16TargetAsmInfo::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 Sands777d2302009-05-09 07:06:46 +000079 unsigned ValSize = TD->getTypeAllocSize(Ty);
Sanjiv Guptad8d27f42009-05-06 08:02:01 +000080
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 Gupta211f3622009-05-10 05:23:47 +000093 std::string name = PAN::getUdataSectionName(BSSSections.size());
94 const Section *NewSection = getNamedSection (name.c_str());
Sanjiv Guptad8d27f42009-05-06 08:02:01 +000095
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
113const Section *
114PIC16TargetAsmInfo::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 Sands777d2302009-05-09 07:06:46 +0000124 unsigned ValSize = TD->getTypeAllocSize(Ty);
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000125
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 Gupta211f3622009-05-10 05:23:47 +0000138 std::string name = PAN::getIdataSectionName(IDATASections.size());
139 const Section *NewSection = getNamedSection (name.c_str());
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000140
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.
159const Section*
160PIC16TargetAsmInfo::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 Gupta211f3622009-05-10 05:23:47 +0000163 const GlobalVariable *GV = dyn_cast<GlobalVariable>(GV1);
164 if (!GV1 || ! GV->hasInitializer())
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000165 return TargetAsmInfo::SelectSectionForGlobal(GV1);
166
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000167 // 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 Guptad8d27f42009-05-06 08:02:01 +0000174
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000175 // See if this is an uninitialized global.
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000176 const Constant *C = GV->getInitializer();
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000177 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 Gupta211f3622009-05-10 05:23:47 +0000184
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000185 // Else let the default implementation take care of it.
186 return TargetAsmInfo::SelectSectionForGlobal(GV);
187}
188
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000189PIC16TargetAsmInfo::~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}