OpenASIP  2.0
DataDefinition.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 DataDefinition.cc
26  *
27  * Implementation of DataDefinition class.
28  *
29  * @author Mikael Lepistö 2006 (mikael.lepisto-no.spam-tut.fi)
30  * @author Pekka Jääskeläinen 2006 (pekka.jaaskelainen-no.spam-tut.fi)
31  * @note rating: red
32  */
33 
34 #include <vector>
35 #include "Address.hh"
36 #include "AddressSpace.hh"
37 #include "DataDefinition.hh"
38 #include "MathTools.hh"
39 
40 namespace TTAProgram {
41 
42 /**
43  * Creates data area definition.
44  *
45  * @param start Starting address of definition.
46  * @param size Number of MAUs that area represents.
47  * @param initData Table of initialization data if initialized definition.
48  * @exception OutOfRange In case the initialization data contains data that
49  * do not fit in a MAU.
50  */
51 
53  Address start, int size, bool littleEndian,
54  MinimumAddressableUnit* initData, bool allZeros)
55  : start_(start),
56  size_(size),
57  allZeros_(allZeros),
58  littleEndian_(littleEndian) {
59  if (initData != NULL) {
60  data_ = new std::vector<MinimumAddressableUnit>(size);
61  for (int i = 0 ; i < size; i++) {
62  const int mauBits = start.space().width();
63  const MinimumAddressableUnit mau = initData[i];
64  if (MathTools::requiredBits(mau) > mauBits) {
65  if (MathTools::requiredBitsSigned(mau) > mauBits) {
66  throw OutOfRange(
67  __FILE__, __LINE__, __func__,
68  "The given value does not fit to the MAU of address "
69  "space.");
70  }
71  }
72 
73 
74  (*data_)[i] = mau;
75  }
76  } else {
77  data_ = NULL;
78  }
79 }
80 
81 /**
82  * Creates initialized data area definition.
83  *
84  * @param start Starting address of definition.
85  * @param initData Initialization data.
86  * @exception OutOfRange In case the initialization data contains data that
87  * do not fit in a MAU.
88  */
90  Address start, const std::vector<MinimumAddressableUnit>& initData,
91  bool littleEndian)
92  : start_(start), allZeros_(false), littleEndian_(littleEndian) {
93  // check that the MAUs are not too large for the address space
94  for (std::size_t i = 0 ; i < initData.size(); i++) {
95  const int mauBits = start.space().width();
96  const MinimumAddressableUnit mau = initData[i];
97 
98  if (MathTools::requiredBits(mau) > mauBits) {
99 
100  if (MathTools::requiredBitsSigned(mau) > mauBits) {
101  throw OutOfRange(
102  __FILE__, __LINE__, __func__,
103  "The given value does not fit to the MAU of address "
104  "space.");
105  }
106  }
107  }
108 
109  data_ = new std::vector<MinimumAddressableUnit>(initData);
110  size_ = data_->size();
111 }
112 
113 /**
114  * Frees all the init data if there is any.
115  */
117  if (data_ != NULL) {
118  delete data_;
119  data_ = NULL;
120  }
121 }
122 
123 /**
124  * Returns start address of data definition.
125  *
126  * @return Start address of data definition.
127  */
128 Address
130  return start_;
131 }
132 
133 void
135  start_ = start;
136 }
137 
138 /**
139  * Returns true if data definition contains initialization data.
140  *
141  * @return True if data definition contains initialization data.
142  */
143 bool
145  return allZeros_ || (data_ != NULL);
146 }
147 
148 /**
149  * Returns one MAU of initialization values from requested index.
150  *
151  * @param index Index of the MAU whic is returned.
152  * @return One MAU of initialization values from requested index.
153  */
155 DataDefinition::MAU(int index) const {
156  assert(index < size());
157 
158  if (isAddress()) {
159  if (littleEndian_) {
160  index = size() - index -1;
161  }
162 
164 
165  // how many bits should be masked out
166  int maskBits = index * startAddress().space().width();
167 
168  // how many bits is the whole area definition
169  int totalAreaDefBits = size() * startAddress().space().width();
170 
171  // how many bits of the data definition should not be masked out
172  int remainingBits = totalAreaDefBits - maskBits;
173 
174  // how many bits is value that is stored as a address
175  int addressBits = sizeof(retVal) * BYTE_BITWIDTH;
176 
177  // how many bits will be masked from start of address
178  int addressMaskBits = addressBits - remainingBits;
179 
180  // mask out bits if needed
181  if (addressMaskBits >= 0) {
182  retVal = retVal << addressMaskBits;
183  } else {
184  retVal = retVal >> (-addressMaskBits);
185  }
186 
187  // align MAU bits to right
188  retVal = retVal >> (addressBits - startAddress().space().width());
189 
190  // return MAU
191  return retVal;
192 
193  } else if(allZeros_) {
194  return 0;
195  } else if (isInitialized()) {
196  return (*data_)[index];
197 
198  } else {
199  throw NotAvailable(
200  __FILE__, __LINE__, __func__,
201  "Definition does not contain initialisation data.");
202  }
203 }
204 
205 /**
206  * Returns the number of MAUs represented by definition.
207  *
208  * @return The number of MAUs represented by definition.
209  */
210 int
212  return size_;
213 }
214 
215 /**
216  * Returns true if init data of definition contains an address.
217  *
218  * @return True if init data of definition contains an address.
219  */
220 bool
222  return false;
223 }
224 
225 /**
226  * Returns true if init data of definition contains an instruction address.
227  *
228  * @return True if init data of definition contains an instruction address.
229  */
230 bool
232  return false;
233 }
234 
235 /**
236  * Returns address where to initialization data refers.
237  *
238  * @return Address where to initialization data refers.
239  */
240 Address
242  throw WrongSubclass(
243  __FILE__, __LINE__, __func__,
244  "Data definition does not contain an address reference.");
245 }
246 
247 /**
248  * Set new destination address for the data definition.
249  */
250 void
252  throw WrongSubclass(
253  __FILE__, __LINE__, __func__,
254  "Data definition does not refer to a data address.");
255 }
256 
257 /**
258  * POM style copy constructor, which supports dynamic binding.
259  *
260  * @return Copy of the object.
261  */
263  DataDefinition* newDef = NULL;
264 
265  if (allZeros_) {
266  newDef = new DataDefinition(start_, size_, littleEndian_, NULL, true);
267  } else if (isInitialized()) {
268  newDef = new DataDefinition(start_, *data_, littleEndian_);
269  } else {
270  newDef = new DataDefinition(start_, size_, littleEndian_);
271  }
272 
273  return newDef;
274 }
275 
276 }
TTAProgram::DataDefinition::DataDefinition
DataDefinition(Address start, int size, bool littleEndian, MinimumAddressableUnit *initData=NULL, bool allZeros=false)
Definition: DataDefinition.cc:52
TTAProgram
Definition: Estimator.hh:65
TTAProgram::DataDefinition::size_
int size_
Size of uninitialized data definition.
Definition: DataDefinition.hh:85
TTAProgram::DataDefinition
Definition: DataDefinition.hh:52
TTAProgram::Address
Definition: Address.hh:51
TTAProgram::DataDefinition::startAddress
virtual Address startAddress() const
Definition: DataDefinition.cc:129
TTAProgram::DataDefinition::size
virtual int size() const
Definition: DataDefinition.cc:211
OutOfRange
Definition: Exception.hh:320
AddressSpace.hh
TTAProgram::DataDefinition::isAddress
virtual bool isAddress() const
Definition: DataDefinition.cc:221
TTAProgram::Address::space
const TTAMachine::AddressSpace & space() const
TTAProgram::DataDefinition::start_
Address start_
Start address of definition.
Definition: DataDefinition.hh:82
NotAvailable
Definition: Exception.hh:728
assert
#define assert(condition)
Definition: Application.hh:86
WrongSubclass
Definition: Exception.hh:336
TTAProgram::DataDefinition::destinationAddress
virtual Address destinationAddress() const
Definition: DataDefinition.cc:241
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
TTAProgram::DataDefinition::isInitialized
virtual bool isInitialized() const
Definition: DataDefinition.cc:144
__func__
#define __func__
Definition: Application.hh:67
TTAProgram::DataDefinition::allZeros_
bool allZeros_
Is all the data zeros? (In this case data_ is null)
Definition: DataDefinition.hh:91
TTAProgram::DataDefinition::setDestinationAddress
virtual void setDestinationAddress(Address dest)
Definition: DataDefinition.cc:251
MathTools::requiredBits
static int requiredBits(unsigned long int number)
TTAProgram::Address::location
InstructionAddress location() const
TTAMachine::AddressSpace::width
virtual int width() const
Definition: AddressSpace.cc:155
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
Address.hh
TTAProgram::DataDefinition::~DataDefinition
virtual ~DataDefinition()
Definition: DataDefinition.cc:116
MathTools.hh
TTAProgram::DataDefinition::littleEndian_
bool littleEndian_
Definition: DataDefinition.hh:93
TTAProgram::DataDefinition::copy
virtual DataDefinition * copy() const
Definition: DataDefinition.cc:262
TTAProgram::DataDefinition::isInstructionAddress
virtual bool isInstructionAddress() const
Definition: DataDefinition.cc:231
DataDefinition.hh
MathTools::requiredBitsSigned
static int requiredBitsSigned(SLongWord number)
TTAProgram::DataDefinition::setStartAddress
virtual void setStartAddress(Address start)
Definition: DataDefinition.cc:134
TTAProgram::DataDefinition::data_
std::vector< MinimumAddressableUnit > * data_
Init data of definition.
Definition: DataDefinition.hh:88
TTAProgram::DataDefinition::MAU
virtual MinimumAddressableUnit MAU(int index) const
Definition: DataDefinition.cc:155