OpenASIP 2.2
Loading...
Searching...
No Matches
Section.cc
Go to the documentation of this file.
1/*
2 Copyright (c) 2002-2009 Tampere University.
3
4 This file is part of TTA-Based Codesign Environment (TCE).
5
6 Permission is hereby granted, free of charge, to any person obtaining a
7 copy of this software and associated documentation files (the "Software"),
8 to deal in the Software without restriction, including without limitation
9 the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 and/or sell copies of the Software, and to permit persons to whom the
11 Software is furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 DEALINGS IN THE SOFTWARE.
23 */
24/**
25 * @file Section.cc
26 *
27 * Non-inline definitions of Section class.
28 *
29 * @author Mikael Lepistö 2003 (tmlepist-no.spam-cs.tut.fi)
30 *
31 * @note rating: yellow
32 */
33
34#include "Section.hh"
35
36#include <map>
37#include <list>
38#include <cmath>
39
40#include "MapTools.hh"
41#include "SafePointer.hh"
42#include "TCEString.hh"
43
44namespace TPEF {
45
46using std::map;
47using std::list;
48
49using ReferenceManager::SafePointer;
50using ReferenceManager::SectionOffsetKey;
51
52/////////////////////////////////////////////////////////////////////////////
53// Section
54/////////////////////////////////////////////////////////////////////////////
55
57
58// initializes instance of static memeber variable
60
61/**
62 * Constructor.
63 */
66 link_(&SafePointer::null),
67 aSpace_(&SafePointer::null),
68 name_(&SafePointer::null),
69 flags_(0),
70 startingAddress_(0) {
71}
72
73/**
74 * Destructor.
75 */
77 while (elements_.size() != 0) {
78 delete elements_[elements_.size() - 1];
79 elements_.pop_back();
80 }
81}
82
83/**
84 * Creates instance of concrete section type.
85 *
86 * @param type Type of section instance that is to be created.
87 * @return Pointer to newly created section.
88 * @exception InstanceNotFound If there is no registered instance for type.
89 */
92 if (prototypes_ == NULL ||
94
95 throw InstanceNotFound(
96 __FILE__, __LINE__, __func__,
97 TCEString("No prototype for section type: ") +
98 Conversion::toString(static_cast<int>(type)));
99 }
100
101 Section* section = (*prototypes_)[type]->clone();
102 return section;
103}
104
105/**
106 * Registers section instance that implements some section type.
107 *
108 * Every registered prototype implements one of used sections. These
109 * registered sections are used to clone section instances by SectionType.
110 *
111 * @param section Section instance that will be registered.
112 */
113void
115
116 // We can't create prototypes_ map statically, because we don't know
117 //if it is initialized before this method is called.
118 if (prototypes_ == NULL) {
120 }
121
123
124 (*prototypes_)[section->type()] = section;
125}
126
127/**
128 * Adds an element to section.
129 *
130 * @param element Element that is added to section.
131 */
132void
134 assert(element != NULL);
135 elements_.push_back(element);
136}
137
138/**
139 * Sets replaces an element in given index with another.
140 *
141 * @param index Index of element that is replaced.
142 * @param element Element that is set to given index.
143 */
144void
145Section::setElement(Word index, SectionElement* element) {
146 assert(element != NULL);
147 assert(index < elementCount());
148 elements_[index] = element;
149}
150
151/**
152 * Returns true if section is chunkable.
153 *
154 * @return True if section is chunkable.
155 */
156bool
158 return false;
159}
160
161/**
162 * Returns a chunk that has offset to a data of section.
163 *
164 * @param offset Offset to a data that is wanted to be referenced.
165 * @return Chunk for raw data section.
166 * @exception NotChunkable If Section is not derived from RawSection.
167 */
168Chunk*
169Section::chunk(SectionOffset /*offset*/) const {
170 throw NotChunkable(__FILE__, __LINE__, "Section::chunk");
171}
172
173/////////////////////////////////////////////////////////////////////////////
174// RawSection
175/////////////////////////////////////////////////////////////////////////////
176
177/**
178 * Constructor.
179 */
181}
182
183/**
184 * Destructor.
185 */
189
190/**
191 * Returns true if section is chunkable.
192 *
193 * @return True if section is chunkable.
194 */
195bool
197 return true;
198}
199
200/**
201 * Returns a chunk object to data in the section at a given offset.
202 *
203 * Checks that there is something in the section before returning chunk.
204 * In the other words, Chunks can not be given from outside of
205 * section's data.
206 *
207 * @param offset The offset of wanted data chunk.
208 * @return Chunk pointing data.
209 * @exception NotChunkable If called on a section that is not RawSection.
210 */
211Chunk*
213 Chunk* newChunk = NULL;
214
215 try {
216 assureInSection(offset);
217 } catch (const OutOfRange& e) {
218 throw NotChunkable(__FILE__, __LINE__, __func__, e.errorMessage());
219 }
220 if (!MapTools::containsKey(dataChunks_, offset)) {
221 newChunk = new Chunk(offset);
222 dataChunks_[offset] = newChunk;
223 referredChunksCache_.clear();
224 } else {
225 newChunk = dataChunks_[offset];
226 }
227
228 return newChunk;
229}
230
231/**
232 * Returns true if the chunk in parameter belongs to this section.
233 *
234 * @param chunk Chunk that is checked.
235 * @return True if the chunk in parameter belongs to this section.
236 */
237bool
239
241 return dataChunks_[chunk->offset()] == chunk;
242 }
243
244 return false;
245}
246
247/**
248 * Sets the length of section's data.
249 *
250 * @param length bytes of data in section.
251 */
252void
254 // should this method be overrided to throw exception if
255 // called with DataSection or StringSection classes
256 length_ = length;
257}
258
259/**
260 * Sets the length of section's data.
261 *
262 * @param length Section length in MAUs.
263 */
264void
268
269/**
270 * Returns length of data inside section.
271 *
272 * @return Length of data inside section.
273 */
274Word
276 return length_;
277}
278
279/**
280 * Returns length of data section in MAUs.
281 *
282 * @return Length of data inside sect.
283 */
284Word
286 return bytesToMAUs(length());
287}
288
289/**
290 * Returns how many MAUs is amount of bytes described in parameter.
291 *
292 * @param bytes Number of bytes to convert.
293 * @return Number of MAUs.
294 */
295Word
296RawSection::bytesToMAUs(Word bytes) const {
297 assert(aSpace() != NULL);
298
299 if (aSpace()->MAU() == 0) {
300 abortWithError("MAU of address space can't be zero.");
301 }
302
303 Word mauSize = static_cast<Word>(
304 ceil(static_cast<double>(aSpace()->MAU()) /
305 static_cast<double>(BYTE_BITWIDTH)));
306
307 // must be even
308 assert ((bytes % mauSize) == 0);
309
310 return bytes / mauSize;
311}
312
313/**
314 * Returns how many bytes some amount of MAUs are.
315 *
316 * @param maus Number of MAUs to convert.
317 * @return Number of bytes that are needed for storing some amount of MAUs.
318 */
319Word
320RawSection::MAUsToBytes(Word maus) const {
321 assert(aSpace() != NULL);
322
323 if (aSpace()->MAU() == 0) {
324 abortWithError("MAU of address space can't be zero for this method.");
325 }
326
327 Word mauSize = static_cast<Word>(
328 ceil(static_cast<double>(aSpace()->MAU()) /
329 static_cast<double>(BYTE_BITWIDTH)));
330
331 return maus * mauSize;
332}
333
334/**
335 * Returns MAU index to given chunk.
336 *
337 * @param chunk Chunk whose MAU index should be found.
338 * @return MAU index to data referred by the chunk.
339 */
340Word
342 return bytesToMAUs(chunk->offset());
343}
344
345/**
346 * Checks that section is not indexed out of bounds.
347 *
348 * If section is indexed out of bounds, exception is thrown.
349 *
350 * @param offset The offset being checked that it is in the area of section.
351 * @exception OutOfRange if not in area of section.
352 */
353void
355 SectionOffset offset) const {
356 if (offset >= length())
357 throw OutOfRange(__FILE__, __LINE__, __func__);
358}
359
360/**
361 * Returns number of chunks that are requested from section.
362 *
363 * @return Number of requested chunks.
364 */
365Word
367 return dataChunks_.size();
368}
369
370
371/**
372 * Returns chunks that is requested from section before.
373 *
374 * @param Index of chunk to return.
375 * @return Chunk to return.
376 */
377Chunk*
378RawSection::referredChunk(Word index) const {
379 if (referredChunksCache_.empty()) {
380 ChunkMap::iterator iter = dataChunks_.begin();
381
382 while (iter != dataChunks_.end()) {
383 referredChunksCache_.push_back((*iter).second);
384 iter++;
385 }
386 }
387
388 return referredChunksCache_[index];
389}
390
391}
#define __func__
#define abortWithError(message)
#define assert(condition)
const Byte BYTE_BITWIDTH
Definition BaseType.hh:136
unsigned char Byte
Definition BaseType.hh:116
MinimumAddressableUnit MAU
static std::string toString(const T &source)
std::string errorMessage() const
Definition Exception.cc:123
static void deleteAllValues(MapType &aMap)
static bool containsKey(const MapType &aMap, const KeyType &aKey)
SectionOffset offset() const
virtual Word bytesToMAUs(Word byteCount) const
Definition Section.cc:296
virtual bool isChunkable() const
Definition Section.cc:196
ChunkMap dataChunks_
Container for already created chunks.
Definition Section.hh:237
virtual void assureInSection(SectionOffset offset) const
Definition Section.cc:354
virtual void setLengthInMAUs(Word length)
Definition Section.cc:265
Chunk * referredChunk(Word index) const
Definition Section.cc:378
virtual Chunk * chunk(SectionOffset offset) const
Definition Section.cc:212
virtual Word chunkToMAUIndex(const Chunk *chunk) const
Definition Section.cc:341
virtual Word length() const
Definition Section.cc:275
std::vector< Chunk * > referredChunksCache_
Table containing all the chunks that are requested from section.
Definition Section.hh:240
Word length_
The length of raw data section.
Definition Section.hh:231
virtual void setDataLength(Word length)
Definition Section.cc:253
Word referredChunkCount() const
Definition Section.cc:366
virtual Word MAUsToBytes(Word mauCount) const
Definition Section.cc:320
virtual ~RawSection()
Definition Section.cc:186
virtual Word lengthInMAUs() const
Definition Section.cc:285
bool belongsToSection(const Chunk *chunk) const
Definition Section.cc:238
static Section * createSection(SectionType type)
Definition Section.cc:91
virtual void setElement(Word index, SectionElement *element)
Definition Section.cc:145
static void registerSection(const Section *section)
Definition Section.cc:114
virtual void addElement(SectionElement *element)
Definition Section.cc:133
virtual Chunk * chunk(SectionOffset offset) const
Definition Section.cc:169
std::map< SectionType, const Section * > SectionPrototypeMap
Type of map that contains section prototypes.
Definition Section.hh:165
static SectionPrototypeMap * prototypes_
Container for registere section prototypes.
Definition Section.hh:168
SectionElement * element(Word index) const
virtual Section * clone() const =0
Creates clone of instance.
Word elementCount() const
static const Byte PROGRAM_SECTION_MASK
Mask for checking if section is auxiliary or program section.
Definition Section.hh:183
virtual bool isChunkable() const
Definition Section.cc:157
virtual ~Section()
Definition Section.cc:76
virtual SectionType type() const =0
Returns SectioType of actual section instance.
std::vector< SectionElement * > elements_
Contain elements.
Definition Section.hh:170
ASpaceElement * aSpace() const
Word SectionOffset
Type for storing offsets relative to a given base offset value.