OpenASIP  2.0
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 
44 namespace TPEF {
45 
46 using std::map;
47 using std::list;
48 
49 using ReferenceManager::SafePointer;
50 using ReferenceManager::SectionOffsetKey;
51 
52 /////////////////////////////////////////////////////////////////////////////
53 // Section
54 /////////////////////////////////////////////////////////////////////////////
55 
57 
58 // initializes instance of static memeber variable
60 
61 /**
62  * Constructor.
63  */
65  SafePointable(),
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  */
90 Section*
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  */
113 void
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  */
132 void
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  */
144 void
145 Section::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  */
156 bool
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  */
168 Chunk*
169 Section::chunk(SectionOffset /*offset*/) const {
170  throw NotChunkable(__FILE__, __LINE__, "Section::chunk");
171 }
172 
173 /////////////////////////////////////////////////////////////////////////////
174 // RawSection
175 /////////////////////////////////////////////////////////////////////////////
176 
177 /**
178  * Constructor.
179  */
180 RawSection::RawSection() : Section(), length_(0) {
181 }
182 
183 /**
184  * Destructor.
185  */
188 }
189 
190 /**
191  * Returns true if section is chunkable.
192  *
193  * @return True if section is chunkable.
194  */
195 bool
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  */
211 Chunk*
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  */
237 bool
238 RawSection::belongsToSection(const Chunk* chunk) const {
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  */
252 void
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  */
264 void
267 }
268 
269 /**
270  * Returns length of data inside section.
271  *
272  * @return Length of data inside section.
273  */
274 Word
276  return length_;
277 }
278 
279 /**
280  * Returns length of data section in MAUs.
281  *
282  * @return Length of data inside sect.
283  */
284 Word
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  */
295 Word
296 RawSection::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  */
319 Word
320 RawSection::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  */
340 Word
341 RawSection::chunkToMAUIndex(const Chunk* chunk) const {
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  */
353 void
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  */
365 Word
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  */
377 Chunk*
378 RawSection::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 }
TPEF::Section::isChunkable
virtual bool isChunkable() const
Definition: Section.cc:157
TPEF::Section::prototypes_
static SectionPrototypeMap * prototypes_
Container for registere section prototypes.
Definition: Section.hh:168
TPEF::Section::chunk
virtual Chunk * chunk(SectionOffset offset) const
Definition: Section.cc:169
TPEF::RawSection::lengthInMAUs
virtual Word lengthInMAUs() const
Definition: Section.cc:285
TPEF::Section::aSpace
ASpaceElement * aSpace() const
TPEF::RawSection::referredChunkCount
Word referredChunkCount() const
Definition: Section.cc:366
TPEF::RawSection::setDataLength
virtual void setDataLength(Word length)
Definition: Section.cc:253
TPEF::Section::~Section
virtual ~Section()
Definition: Section.cc:76
TPEF::Section::SectionPrototypeMap
std::map< SectionType, const Section * > SectionPrototypeMap
Type of map that contains section prototypes.
Definition: Section.hh:165
OutOfRange
Definition: Exception.hh:320
MapTools.hh
TPEF::Section::setElement
virtual void setElement(Word index, SectionElement *element)
Definition: Section.cc:145
TPEF::Section::type
virtual SectionType type() const =0
Returns SectioType of actual section instance.
MapTools::deleteAllValues
static void deleteAllValues(MapType &aMap)
SafePointer.hh
TPEF::RawSection::isChunkable
virtual bool isChunkable() const
Definition: Section.cc:196
Byte
unsigned char Byte
Definition: BaseType.hh:116
TPEF::RawSection::MAUsToBytes
virtual Word MAUsToBytes(Word mauCount) const
Definition: Section.cc:320
Conversion::toString
static std::string toString(const T &source)
TPEF::Section
Definition: Section.hh:64
TPEF::RawSection::referredChunksCache_
std::vector< Chunk * > referredChunksCache_
Table containing all the chunks that are requested from section.
Definition: Section.hh:240
TPEF::RawSection::belongsToSection
bool belongsToSection(const Chunk *chunk) const
Definition: Section.cc:238
TPEF::RawSection::dataChunks_
ChunkMap dataChunks_
Container for already created chunks.
Definition: Section.hh:237
TCEString.hh
TPEF::Section::element
SectionElement * element(Word index) const
assert
#define assert(condition)
Definition: Application.hh:86
TPEF::Section::addElement
virtual void addElement(SectionElement *element)
Definition: Section.cc:133
TPEF::RawSection::assureInSection
virtual void assureInSection(SectionOffset offset) const
Definition: Section.cc:354
TPEF::Section::Section
Section()
Definition: Section.cc:64
abortWithError
#define abortWithError(message)
Definition: Application.hh:72
TPEF::ReferenceManager::SafePointer
Definition: SafePointer.hh:188
TPEF::RawSection::length_
Word length_
The length of raw data section.
Definition: Section.hh:231
TPEF::Section::createSection
static Section * createSection(SectionType type)
Definition: Section.cc:91
TPEF::RawSection::referredChunk
Chunk * referredChunk(Word index) const
Definition: Section.cc:378
TPEF::SectionElement
Definition: SectionElement.hh:44
__func__
#define __func__
Definition: Application.hh:67
TPEF::RawSection::bytesToMAUs
virtual Word bytesToMAUs(Word byteCount) const
Definition: Section.cc:296
MAU
MinimumAddressableUnit MAU
Definition: CustomDBGController.cc:44
TPEF::SafePointable
Definition: SafePointable.hh:48
TPEF::Section::elements_
std::vector< SectionElement * > elements_
Contain elements.
Definition: Section.hh:170
Exception::errorMessage
std::string errorMessage() const
Definition: Exception.cc:123
TPEF::Section::PROGRAM_SECTION_MASK
static const Byte PROGRAM_SECTION_MASK
Mask for checking if section is auxiliary or program section.
Definition: Section.hh:183
TPEF::Section::registerSection
static void registerSection(const Section *section)
Definition: Section.cc:114
TPEF::SectionOffset
Word SectionOffset
Type for storing offsets relative to a given base offset value.
Definition: TPEFBaseType.hh:49
MapTools::containsKey
static bool containsKey(const MapType &aMap, const KeyType &aKey)
TPEF::Chunk::offset
SectionOffset offset() const
BYTE_BITWIDTH
const Byte BYTE_BITWIDTH
Definition: BaseType.hh:136
TPEF::RawSection::setLengthInMAUs
virtual void setLengthInMAUs(Word length)
Definition: Section.cc:265
TCEString
Definition: TCEString.hh:53
Section.hh
TPEF::RawSection::~RawSection
virtual ~RawSection()
Definition: Section.cc:186
TPEF::Section::SectionType
SectionType
Definition: Section.hh:69
TPEF::RawSection::length
virtual Word length() const
Definition: Section.cc:275
NotChunkable
Definition: Exception.hh:353
TPEF::RawSection::chunk
virtual Chunk * chunk(SectionOffset offset) const
Definition: Section.cc:212
TPEF::RawSection::chunkToMAUIndex
virtual Word chunkToMAUIndex(const Chunk *chunk) const
Definition: Section.cc:341
TPEF::Section::clone
virtual Section * clone() const =0
Creates clone of instance.
TPEF::Chunk
Definition: Chunk.hh:45
InstanceNotFound
Definition: Exception.hh:304
TPEF::Section::elementCount
Word elementCount() const
TPEF
Definition: Assembler.hh:43
TPEF::RawSection::RawSection
RawSection()
Definition: Section.cc:180