OpenASIP  2.0
DataSection.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 DataSection.cc
26  *
27  * Non-inline definitions of DataSection class.
28  *
29  * @author Mikael Lepistö 2003 (tmlepist-no.spam-cs.tut.fi)
30  * @author Pekka Jääskeläinen 2006 (pjaaskel-no.spam-cs.tut.fi)
31  *
32  * @note rating: yellow
33  */
34 
35 #include <cmath>
36 
37 #include "DataSection.hh"
38 #include "Application.hh"
39 #include "Swapper.hh"
40 #include "MathTools.hh"
41 
42 namespace TPEF {
43 
44 DataSection DataSection::proto_(true);
45 
46 /**
47  * Constructor.
48  *
49  * @param init true if registeration is wanted
50  */
52 
53  if (init) {
55  }
56 
57  unsetFlagVLen();
59 }
60 
61 /**
62  * Destructor.
63  */
65 }
66 
67 /**
68  * Returns section's type.
69  *
70  * @return Type of section.
71  */
74  return ST_DATA;
75 }
76 
77 /**
78  * Sets value of one byte of data section.
79  *
80  * NOTE: Byte here is tpef file byte. (8bits).
81  *
82  * @param offset Offset where to write the byte.
83  * @param aByte Value to write.
84  */
85 void
86 DataSection::setByte(Word offset, Byte aByte) {
87  if (offset >= data_.size()) {
89  "Can't set byte " +
90  Conversion::toString(static_cast<int>(aByte)) +
91  " to offset: " + Conversion::toString(offset) +
92  "\t data section size: " + Conversion::toString(data_.size()));
93  }
94  data_[offset] = aByte;
95 }
96 
97 /**
98  * Creates an instance of DataSection.
99  *
100  * @return Newly created section.
101  */
102 Section*
104  return new DataSection(false);
105 }
106 
107 /**
108  * Returns byte from chunk's offset.
109  *
110  * @param chunk Chunk containing offset of data.
111  * @return Byte from chunk offset.
112  */
113 Byte
114 DataSection::byte(const Chunk* chunk) const {
115  return data_[chunk->offset()];
116 }
117 
118 /**
119  * Returns one MAU from section by gived MAU index.
120  *
121  * @param index Index of MAU to return.
122  * @return One MAU of data section.
123  */
125 DataSection::MAU(Word index) const {
126 
127  int byteOffset = MAUsToBytes(index);
128  int mauInBytes = MAUsToBytes(1);
129 
130  MinimumAddressableUnit retVal = 0;
131 
132  // read all the bytes to retval
133  for(int i = 0; i < mauInBytes; i++) {
134  retVal = (retVal << BYTE_BITWIDTH) |
135  static_cast<MinimumAddressableUnit>(byte(byteOffset+i));
136  }
137 
138  return retVal;
139 }
140 
141 /**
142  * Adds MAU to the end of section.
143  *
144  * @param aMAU MAU that will be copied into the section.
145  * @exception OutOfRange If the given value could not fit to the bit count
146  * defined by the address space's MAU width (when interpreted as unsigned or
147  * signed integer).
148  */
149 void
151  int mauBits = aSpace()->MAU();
152 
153  if (MathTools::requiredBits(aMAU) > mauBits) {
154  if (MathTools::requiredBitsSigned(aMAU) > mauBits) {
155  throw OutOfRange(
156  __FILE__, __LINE__, __func__,
157  "The given value does not fit to the MAU of address space.");
158  }
159  }
160 
161  // how many bits are left to last byte
162  int modulo = mauBits % BYTE_BITWIDTH;
163 
164  // write first bits
165  if (modulo != 0) {
166  mauBits -= modulo;
167  addByte(static_cast<Byte>(aMAU >> mauBits));
168  }
169 
170  // move shifting to right for the next MAU
171  mauBits -= BYTE_BITWIDTH;
172 
173  // write the rest
174  for (; mauBits >= 0;
175  mauBits = mauBits - BYTE_BITWIDTH) {
176 
177  addByte(static_cast<Byte>(aMAU >> mauBits));
178  }
179 }
180 
181 /**
182  * Returns byte from byte offset.
183  *
184  * @param offset Offset of data.
185  * @return Byte from offset.
186  */
187 Byte
188 DataSection::byte(Word offset) const {
189  return data_[offset];
190 }
191 
192 /**
193  * Adds byte of data into section.
194  *
195  * Method copies data that is given in parameter.
196  *
197  * @param aByte Byte that will be copied into the section.
198  */
199 void
201  data_.push_back(aByte);
202 }
203 
204 /**
205  * Returns the size of the data section in bytes.
206  *
207  * @return Length of data in bytes.
208  */
209 Word
211  return data_.size();
212 }
213 
214 /**
215  * Tries to set data length.
216 
217  * Can be used only for making section bigger. Asserts if method is
218  * tried to use for making section smaller.
219  *
220  * Initializes data with zeros.
221  *
222  * @param length Length to which section is expanded.
223  */
224 void
226  assert(length() <= aLength);
227  while (length() < aLength) {
228  addByte(0);
229  }
230 }
231 
232 /**
233  * Writes unsigned value to data section.
234  *
235  * Value is aligned to field as normal big endian value
236  * least significant bit is stored to last bit of last MAU.
237  *
238  * If we call writeValue(0, 4, 3) and MAU is 2bit
239  * start of data section will be 00 00 00 11.
240  *
241  * However the MAUs are stored in data section like this:
242  * 00000000|000000000|00000000|00000011 (one MAU per byte)
243  * MAU1 | MAU2 | MAU3 | MAU4
244  *
245  * @param index MAU offset to section where to we write value.
246  * @param numOfMAUs Number of MAUs that we use for storing value.
247  * @param value Value to write.
248  */
249 void
250 DataSection::writeValue(Word index, Word numOfMAUs, unsigned long value) {
251 
252  int mauInBytes = MAUsToBytes(1);
253 
254  MinimumAddressableUnit mauMask =
255  static_cast<unsigned int>(-1) >>
256  (sizeof(mauMask)*BYTE_BITWIDTH - aSpace()->MAU());
257 
258  int shiftCount = 0;
259 
260  // start writing from end of area..
261  for (int i = numOfMAUs - 1; i >= 0; i--) {
262  int byteOffset = MAUsToBytes(index + i);
263 
264  MinimumAddressableUnit currentMAU = 0;
265 
266  if (shiftCount < static_cast<int>(sizeof(value)*BYTE_BITWIDTH)) {
267  // I tried math tools... system tests went broken :(
268  // ssooo... if it's not broken....
269  currentMAU = (value >> shiftCount) & mauMask;
270  }
271 
272  shiftCount += aSpace()->MAU();
273 
274  // write current MAU :)
275  for (int j = mauInBytes-1; j >= 0; j--) {
276  Byte currentByte = static_cast<Byte>(currentMAU);
277  setByte(byteOffset + j, currentByte);
278  currentMAU = currentMAU >> BYTE_BITWIDTH;
279  }
280  }
281 }
282 
283 /**
284  * Writes signed value to data section.
285  *
286  * For example, when we call writeValue(0, 4, -3) and MAU is 2bit
287  * start of data section will be 11 11 11 01.
288  *
289  * MAUs are stored in data section like this:
290  * 00000011 000000011 00000011 00000001 (one MAU per byte)
291  *
292  * If we call writeValue(0, 4, -3) and MAU is 10bit
293  * start of data section will be 1111111111 1111111111 1111111111 1111111101.
294  *
295  * However the MAUs are stored in data section like this:
296  *
297  * 00000011 11111111|00000011 11111111|00000011 11111111|00000011 11111101|
298  * MAU1 | MAU2 | MAU3 | MAU4 |
299  *
300  * @param index MAU offset to section where to we write value.
301  * @param numOfMAUs Number of MAUs that we use for storing value.
302  * @param value Value to write.
303  */
304 void
305 DataSection::writeValue(Word index, Word numOfMAUs, signed long value) {
306 
307  int mauInBytes = MAUsToBytes(1);
308 
309  MinimumAddressableUnit mauMask =
310  static_cast<unsigned int>(-1) >>
311  (sizeof(mauMask)*BYTE_BITWIDTH - aSpace()->MAU());
312 
313  int shiftCount = 0;
314 
315  // start writing from end of area..
316  for (int i = numOfMAUs - 1; i >= 0; i--) {
317  int byteOffset = MAUsToBytes(index + i);
318 
319  MinimumAddressableUnit currentMAU =
320  static_cast<MinimumAddressableUnit>(~0);
321 
322  if (shiftCount < static_cast<int>(sizeof(value)*BYTE_BITWIDTH)) {
323  // I tried math tools... system tests went broken :(
324  // ssooo... if it's not broken....
325  currentMAU = (value >> shiftCount) & mauMask;
326  shiftCount += aSpace()->MAU();
327  }
328 
329  // write current MAU :)
330  for (int j = mauInBytes-1; j >= 0; j--) {
331  Byte currentByte = static_cast<Byte>(currentMAU);
332  setByte(byteOffset + j,currentByte);
333  currentMAU = currentMAU >> BYTE_BITWIDTH;
334  }
335  }
336 }
337 
338 }
TPEF::DataSection::proto_
static DataSection proto_
Prototype instance of section.
Definition: DataSection.hh:80
TPEF::DataSection::byte
virtual Byte byte(const Chunk *chunk) const
Definition: DataSection.cc:114
TPEF::Section::aSpace
ASpaceElement * aSpace() const
TPEF::DataSection::MAU
virtual MinimumAddressableUnit MAU(Word index) const
Definition: DataSection.cc:125
TPEF::Section::unsetFlagVLen
void unsetFlagVLen()
TPEF::DataSection::addMAU
virtual void addMAU(MinimumAddressableUnit aMAU)
Definition: DataSection.cc:150
TPEF::DataSection::clone
virtual Section * clone() const
Definition: DataSection.cc:103
OutOfRange
Definition: Exception.hh:320
TPEF::DataSection::setByte
virtual void setByte(Word offset, Byte aByte)
Definition: DataSection.cc:86
TPEF::ASpaceElement::MAU
Byte MAU() const
Byte
unsigned char Byte
Definition: BaseType.hh:116
TPEF::RawSection::MAUsToBytes
virtual Word MAUsToBytes(Word mauCount) const
Definition: Section.cc:320
TPEF::DataSection::addByte
virtual void addByte(Byte aByte)
Definition: DataSection.cc:200
Conversion::toString
static std::string toString(const T &source)
TPEF::Section
Definition: Section.hh:64
TPEF::DataSection::data_
std::vector< Byte > data_
Contains the data of data section.
Definition: DataSection.hh:83
assert
#define assert(condition)
Definition: Application.hh:86
abortWithError
#define abortWithError(message)
Definition: Application.hh:72
TPEF::Section::ST_DATA
@ ST_DATA
Initialized data section.
Definition: Section.hh:80
MinimumAddressableUnit
Word MinimumAddressableUnit
Type for storing a MAU (must be unsigned type!). This limits the maximum size of the simulated minimu...
Definition: BaseType.hh:184
Application.hh
TPEF::UDataSection
Definition: UDataSection.hh:47
__func__
#define __func__
Definition: Application.hh:67
DataSection.hh
MathTools::requiredBits
static int requiredBits(unsigned long int number)
TPEF::Section::unsetFlagNoBits
void unsetFlagNoBits()
TPEF::DataSection::~DataSection
virtual ~DataSection()
Definition: DataSection.cc:64
TPEF::Section::registerSection
static void registerSection(const Section *section)
Definition: Section.cc:114
TPEF::DataSection::type
virtual SectionType type() const
Definition: DataSection.cc:73
TPEF::Chunk::offset
SectionOffset offset() const
TPEF::DataSection::setDataLength
virtual void setDataLength(Word length)
Definition: DataSection.cc:225
Swapper.hh
BYTE_BITWIDTH
const Byte BYTE_BITWIDTH
Definition: BaseType.hh:136
false
find Finds info of the inner loops in the false
Definition: InnerLoopFinder.cc:81
TPEF::Section::SectionType
SectionType
Definition: Section.hh:69
TPEF::DataSection::DataSection
DataSection(bool init)
Definition: DataSection.cc:51
TPEF::DataSection::length
virtual Word length() const
Definition: DataSection.cc:210
MathTools.hh
TPEF::RawSection::chunk
virtual Chunk * chunk(SectionOffset offset) const
Definition: Section.cc:212
MathTools::requiredBitsSigned
static int requiredBitsSigned(SLongWord number)
TPEF::Chunk
Definition: Chunk.hh:45
TPEF::DataSection::writeValue
virtual void writeValue(Word index, Word numOfMAUs, unsigned long value)
Definition: DataSection.cc:250
TPEF
Definition: Assembler.hh:43