OpenASIP  2.0
AddressSpace.cc
Go to the documentation of this file.
1 /*
2  Copyright (c) 2002-2010 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 AddressSpace.cc
26  *
27  * Implementation of AddressSpace class.
28  *
29  * @author Lasse Laasonen 2003 (lasse.laasonen-no.spam-tut.fi)
30  * @author Pekka Jääskeläinen 2010
31  * @note reviewed 10 Jun 2004 by vpj, am, tr, ll
32  * @note rating: red
33  */
34 
35 #include <string>
36 #include <tuple>
37 
38 #include "AddressSpace.hh"
39 #include "ControlUnit.hh"
40 #include "MOMTextGenerator.hh"
41 #include "Application.hh"
42 #include "AssocTools.hh"
43 #include "Machine.hh"
44 #include "ObjectState.hh"
45 #include "AssocTools.hh"
46 
47 using std::string;
48 using boost::format;
49 
50 namespace TTAMachine {
51 
52 // initialization of static data members
53 const string AddressSpace::OSNAME_ADDRESS_SPACE = "adress_space";
54 const string AddressSpace::OSKEY_WIDTH = "width";
55 const string AddressSpace::OSKEY_MIN_ADDRESS = "min_a";
56 const string AddressSpace::OSKEY_MAX_ADDRESS = "max_a";
57 const string AddressSpace::OSKEY_SHARED_MEMORY = "shared-memory";
58 const string AddressSpace::OSKEY_NUMERICAL_ID = "numerical-id";
59 
60 /**
61  * Constructor.
62  *
63  * @param name Name of the address space.
64  * @param width Bit width of the minimum addressable word.
65  * @param minAddress Lowest address in the address space.
66  * @param maxAddress Highest address in the address space.
67  * @param owner The machine which owns the address space.
68  * @exception ComponentAlreadyExists If another address space by the same
69  * name is already registered to the
70  * machine.
71  * @exception OutOfRange If the some of the given parameters is out of range.
72  * @exception InvalidName If the given name is not valid for a component.
73  */
75  const string& name, int width, ULongWord minAddress,
76  ULongWord maxAddress, Machine& owner)
77  : Component(name) {
78  if (width <= 0 || minAddress >= maxAddress) {
79  string procName = "AddressSpace::AddressSpace";
80  throw OutOfRange(__FILE__, __LINE__, procName);
81  }
82 
83  width_ = width;
84  minAddress_ = minAddress;
85  maxAddress_ = maxAddress;
86 
87  setMachine(owner);
88 }
89 
90 /**
91  * Constructor.
92  *
93  * Loads its state from the given ObjectState instance.
94  *
95  * @param state The ObjectState from which the name is taken.
96  * @param owner The machine which owns the address space.
97  * @exception ObjectStateLoadingException If the machine already has an
98  * address space by the same name as
99  * the coming name of this or if
100  * the ObjectState instance is
101  * invalid.
102  */
104  : Component(state),
105  width_(0),
106  minAddress_(0),
107  maxAddress_(0),
108  shared_(true) {
109  loadState(state);
110 
111  for (IDSet::const_iterator i = numericalIds_.begin();
112  i != numericalIds_.end(); ++i) {
113  unsigned id = (*i);
115  for (int asi = 0; asi < nav.count(); ++asi) {
116  AddressSpace& otherAS = *nav.item(asi);
117  if (otherAS.hasNumericalId(id)) {
119  __FILE__, __LINE__, __func__,
120  (boost::format(
121  "Address space '%s' has the same numerical "
122  "id %d as '%s'.") %
123  otherAS.name() % id % name()).str());
124  }
125  }
126  }
127 
128  try {
129  setMachine(owner);
130  } catch (ComponentAlreadyExists&) {
131  MOMTextGenerator textGenerator;
132  format errorMsg = textGenerator.text(MOMTextGenerator::
133  TXT_AS_EXISTS_BY_NAME);
134  errorMsg % name();
135  string procName = "AddressSpace::AddressSpace";
136  throw ObjectStateLoadingException(__FILE__, __LINE__, procName,
137  errorMsg.str());
138  }
139 }
140 
141 /**
142  * Destructor.
143  */
145  unsetMachine();
146 }
147 
148 
149 /**
150  * Returns the bit width of the minimum addressable word.
151  *
152  * @return The bit width of the minimum addressable word.
153  */
154 int
156  return width_;
157 }
158 
159 
160 /**
161  * Returns the lowest memory address of the address space.
162  *
163  * @return The lowest memory address of the address space.
164  */
165 ULongWord
167  return minAddress_;
168 }
169 
170 
171 /**
172  * Returns the highest memory address of the address space.
173  *
174  * @return The highest memory address of the address space.
175  */
176 ULongWord
178  return maxAddress_;
179 }
180 
181 
182 /**
183  * Sets the name of the address space.
184  *
185  * @param name Name of the address space.
186  * @exception ComponentAlreadyExists If an address space with the given name
187  * is already in the same machine.
188  * @exception InvalidName If the given name is not valid for a component.
189  */
190 void
191 AddressSpace::setName(const string& name) {
192  if (name == this->name()) {
193  return;
194  }
195 
196  if (machine() != NULL) {
197  if (machine()->addressSpaceNavigator().hasItem(name)) {
198  string procName = "AddressSpace::setName";
199  throw ComponentAlreadyExists(__FILE__, __LINE__, procName);
200  } else {
202  }
203  } else {
205  }
206 }
207 
208 /**
209  * Sets the bit width of the minimum addressable word.
210  *
211  * @param width The bit width of the minimum addressable word.
212  * @exception OutOfRange If the given width is illegal (<=0).
213  */
214 void
216  if (width <= 0) {
217  string procName = "AddressSpace::setWidth";
218  throw OutOfRange(__FILE__, __LINE__, procName);
219  }
220 
221  width_ = width;
222 }
223 
224 /**
225  * Sets the memory address bounds of the address space.
226  *
227  * @param start The lowest memory address.
228  * @param end The highest memory address.
229  * @exception OutOfRange If the given start and end addresses are illegal.
230  */
231 void
233  if (start >= end) {
234  string procName = "AddressSpace::setAddressBounds";
235  throw OutOfRange(__FILE__, __LINE__, procName);
236  }
237 
238  minAddress_ = start;
239  maxAddress_ = end;
240 }
241 
242 /**
243  * Registers the address space to a machine. Do not use this method.
244  *
245  * @param mach Machine to which the address space is going to be registered.
246  * @exception ComponentAlreadyExists If there is another address space by the
247  * same name in the given machine.
248  */
249 void
251  assert(!isRegistered());
252  mach.addAddressSpace(*this);
253  internalSetMachine(mach);
254 }
255 
256 /**
257  * Removes registration of the address space from its current machine. The
258  * address space is deleted too because it cannot be unregistered.
259  */
260 void
262 
263  assert(isRegistered());
264  Machine* mach = machine();
265 
266  // remove dangling pointers in function units
268  int units = fuNav.count();
269  for (int i = 0; i < units; i++) {
270  FunctionUnit* fu = fuNav.item(i);
271  if (fu->addressSpace() == this) {
272  fu->setAddressSpace(NULL);
273  }
274  }
275 
276  // remove dangling pointer in GCU
277  ControlUnit* cu = mach->controlUnit();
278  if (cu != NULL) {
279  if (cu->addressSpace() == this) {
280  cu->setAddressSpace(NULL);
281  }
282  }
283 
285  mach->deleteAddressSpace(*this);
286 }
287 
288 
289 /**
290  * Saves the contents to an ObjectState object.
291  *
292  * @return The newly created ObjectState object.
293  */
296 
298  as->setAttribute(OSKEY_NAME, name());
299 
300  // set width
302 
303  // set min address
305 
306  // set max address
308 
309  if (!shared_) {
311  }
312 
313  for (IDSet::const_iterator i = numericalIds_.begin();
314  i != numericalIds_.end(); ++i) {
315  ObjectState* child = new ObjectState(OSKEY_NUMERICAL_ID, as);
316  child->setValue((int)(*i));
317  }
318 
319  return as;
320 }
321 
322 
323 /**
324  * Loads its state from the given ObjectState instance.
325  *
326  * @param state The ObjectState instance.
327  * @exception ObjectStateLoadingException If the given ObjectState instance
328  *  is invalid or if the machine
329  * already has an address space by
330  * the same name as the coming name
331  * of this address space.
332  */
333 void
335  const string procName = "AddressSpace::loadState";
336 
337  if (state->name() != OSNAME_ADDRESS_SPACE) {
338  throw ObjectStateLoadingException(__FILE__, __LINE__, procName);
339  }
340 
341  Component::loadState(state);
342 
343  try {
345  ULongWord minAddress = state->uLongAttribute(
347  ULongWord maxAddress = state->uLongAttribute(
349  setAddressBounds(minAddress, maxAddress);
350 
351  if (state->hasAttribute(OSKEY_SHARED_MEMORY)) {
352  setShared(
354  }
355 
356  for (int i = 0; i < state->childCount(); i++) {
357  ObjectState* child = state->child(i);
358  if (child->name() == OSKEY_NUMERICAL_ID) {
359  addNumericalId(child->intValue());
360  }
361  }
362 
363  } catch (Exception& e) {
364  throw ObjectStateLoadingException(__FILE__, __LINE__, procName,
365  e.errorMessage());
366  }
367 }
368 
369 /**
370  * Adds a numerical address space id that should be mapped to this
371  * address space.
372  *
373  * Numerical IDs are referred to from programs, i.e., by means of the
374  * __attribute__((address_space(N)). Single ADF address space can map
375  * multiple program address spaces.
376  */
377 void
379  numericalIds_.insert(id);
380 }
381 
382 bool
383 AddressSpace::hasNumericalId(unsigned id) const {
385 }
386 
387 /**
388  * Returns ids that are assigned to this address space.
389  *
390  * @return Address space ids.
391  */
392 std::set<unsigned>
394  return numericalIds_;
395 }
396 
397 /**
398  * Sets the ids for the address space.
399  *
400  * @param ids Contains new ids for the address space.
401  * @return True if setting the new address space ids was successful.
402  */
403 bool
404 AddressSpace::setNumericalIds(const std::set<unsigned>& ids) {
405  assert (machine() != NULL);
406 
407  Machine::AddressSpaceNavigator asNavigator =
409 
410  // loop through all other address spaces and check if input parameter
411  // violates any existing ids
412  for (int i = 0; i < asNavigator.count(); i++) {
413  AddressSpace* as = asNavigator.item(i);
414 
415  if (as != this) {
416  IDSet otherIds = as->numericalIds();
417 
418  for (IDSet::iterator id = ids.begin(); id != ids.end(); ++id) {
419  if (AssocTools::containsKey(otherIds, *id)) {
420  return false;
421  }
422  }
423  }
424  }
425 
426  // no violating ids found, overwrite ids for this address space
427  numericalIds_ = ids;
428  return true;
429 }
430 
431 bool
433  return (this->width_ == other.width_
434  && this->minAddress_ == other.minAddress_
435  && this->maxAddress_ == other.maxAddress_
436  && this->numericalIds_ == other.numericalIds_
437  && this->shared_ == other.shared_);
438 }
439 
440 bool
442  return !this->operator==(other);
443 }
444 
445 }
446 
TTAMachine::Component::internalUnsetMachine
void internalUnsetMachine()
ObjectState::hasAttribute
bool hasAttribute(const std::string &name) const
Definition: ObjectState.cc:205
TTAMachine::Component::setName
virtual void setName(const std::string &name)
Definition: MachinePart.cc:142
TTAMachine::AddressSpace::unsetMachine
virtual void unsetMachine()
Definition: AddressSpace.cc:261
TTAMachine::Component::name
virtual TCEString name() const
Definition: MachinePart.cc:125
TTAMachine::AddressSpace::addNumericalId
virtual void addNumericalId(unsigned id)
Definition: AddressSpace.cc:378
TTAMachine::Component::isRegistered
virtual bool isRegistered() const
Definition: MachinePart.cc:177
TTAMachine::AddressSpace::numericalIds
std::set< unsigned > numericalIds() const
Definition: AddressSpace.cc:393
TTAMachine::AddressSpace
Definition: AddressSpace.hh:51
ObjectStateLoadingException
Definition: Exception.hh:551
ObjectState::intValue
int intValue() const
TTAMachine::AddressSpace::hasNumericalId
virtual bool hasNumericalId(unsigned id) const
Definition: AddressSpace.cc:383
AssocTools::containsKey
static bool containsKey(const ContainerType &aContainer, const KeyType &aKey)
ObjectState::uLongAttribute
ULongWord uLongAttribute(const std::string &name) const
Definition: ObjectState.cc:291
TTAMachine::AddressSpace::width_
int width_
Bit width of the minimum addressable word.
Definition: AddressSpace.hh:97
OutOfRange
Definition: Exception.hh:320
TTAMachine::AddressSpace::OSKEY_NUMERICAL_ID
static const std::string OSKEY_NUMERICAL_ID
Definition: AddressSpace.hh:92
AddressSpace.hh
TTAMachine::AddressSpace::operator!=
bool operator!=(const AddressSpace &other) const
Definition: AddressSpace.cc:441
ObjectState
Definition: ObjectState.hh:59
TTAMachine::FunctionUnit::addressSpace
virtual AddressSpace * addressSpace() const
Definition: FunctionUnit.cc:580
TTAMachine::Machine::Navigator::count
int count() const
Texts::TextGenerator::text
virtual boost::format text(int textId)
Definition: TextGenerator.cc:94
TTAMachine::Component::internalSetMachine
void internalSetMachine(Machine &machine)
TTAMachine::AddressSpace::setShared
virtual void setShared(bool shared)
Definition: AddressSpace.hh:71
TTAMachine::AddressSpace::OSNAME_ADDRESS_SPACE
static const std::string OSNAME_ADDRESS_SPACE
ObjectState name for AddressSpace.
Definition: AddressSpace.hh:84
TTAMachine::AddressSpace::OSKEY_MAX_ADDRESS
static const std::string OSKEY_MAX_ADDRESS
ObjectState attribute key for maximum address.
Definition: AddressSpace.hh:90
TTAMachine::Machine::deleteAddressSpace
virtual void deleteAddressSpace(AddressSpace &as)
Definition: Machine.cc:623
assert
#define assert(condition)
Definition: Application.hh:86
TTAMachine::FunctionUnit
Definition: FunctionUnit.hh:55
TTAMachine::AddressSpace::~AddressSpace
virtual ~AddressSpace()
Definition: AddressSpace.cc:144
TTAMachine::AddressSpace::setWidth
virtual void setWidth(int width)
Definition: AddressSpace.cc:215
TTAMachine::AddressSpace::OSKEY_WIDTH
static const std::string OSKEY_WIDTH
ObjectState attribute key for the bit width.
Definition: AddressSpace.hh:86
TTAMachine::AddressSpace::AddressSpace
AddressSpace(const std::string &name, int width, ULongWord minAddress, ULongWord maxAddress, Machine &owner)
Definition: AddressSpace.cc:74
TTAMachine::AddressSpace::setAddressBounds
virtual void setAddressBounds(ULongWord start, ULongWord end)
Definition: AddressSpace.cc:232
TTAMachine::Machine::controlUnit
virtual ControlUnit * controlUnit() const
Definition: Machine.cc:345
TTAMachine::ControlUnit
Definition: ControlUnit.hh:50
TTAMachine::Component::loadState
virtual void loadState(const ObjectState *state)
Definition: MachinePart.cc:205
Application.hh
__func__
#define __func__
Definition: Application.hh:67
ObjectState.hh
TTAMachine::Machine::functionUnitNavigator
virtual FunctionUnitNavigator functionUnitNavigator() const
Definition: Machine.cc:380
TTAMachine::Component
Definition: MachinePart.hh:90
TTAMachine::Component::OSKEY_NAME
static const std::string OSKEY_NAME
ObjectState attribute key for the name of the component.
Definition: MachinePart.hh:137
ObjectState::child
ObjectState * child(int index) const
Definition: ObjectState.cc:471
TTAMachine::AddressSpace::OSKEY_SHARED_MEMORY
static const std::string OSKEY_SHARED_MEMORY
Definition: AddressSpace.hh:91
ObjectState::childCount
int childCount() const
TTAMachine::AddressSpace::saveState
virtual ObjectState * saveState() const
Definition: AddressSpace.cc:295
TTAMachine::Machine::addAddressSpace
virtual void addAddressSpace(AddressSpace &as)
Definition: Machine.cc:248
Machine.hh
Exception
Definition: Exception.hh:54
TTAMachine::FunctionUnit::setAddressSpace
virtual void setAddressSpace(AddressSpace *as)
Definition: FunctionUnit.cc:594
TTAMachine::Machine::addressSpaceNavigator
virtual AddressSpaceNavigator addressSpaceNavigator() const
Definition: Machine.cc:392
ObjectState::name
std::string name() const
Exception::errorMessage
std::string errorMessage() const
Definition: Exception.cc:123
TTAMachine::AddressSpace::setNumericalIds
bool setNumericalIds(const std::set< unsigned > &ids)
Definition: AddressSpace.cc:404
TTAMachine::AddressSpace::width
virtual int width() const
Definition: AddressSpace.cc:155
TTAMachine::AddressSpace::IDSet
std::set< unsigned > IDSet
Definition: AddressSpace.hh:95
TTAMachine::AddressSpace::OSKEY_MIN_ADDRESS
static const std::string OSKEY_MIN_ADDRESS
ObjectState attribute key for minimum address.
Definition: AddressSpace.hh:88
TTAMachine::AddressSpace::setMachine
virtual void setMachine(Machine &mach)
Definition: AddressSpace.cc:250
TTAMachine::Component::machine
virtual Machine * machine() const
AssocTools.hh
MOMTextGenerator
Definition: MOMTextGenerator.hh:40
ObjectState::boolAttribute
bool boolAttribute(const std::string &name) const
Definition: ObjectState.cc:338
ControlUnit.hh
ComponentAlreadyExists
Definition: Exception.hh:510
TTAMachine::AddressSpace::loadState
virtual void loadState(const ObjectState *state)
Definition: AddressSpace.cc:334
MOMTextGenerator.hh
ObjectState::intAttribute
int intAttribute(const std::string &name) const
Definition: ObjectState.cc:276
ULongWord
unsigned long ULongWord
Definition: BaseType.hh:51
TTAMachine::Machine::Navigator::item
ComponentType * item(int index) const
TTAMachine::AddressSpace::operator==
bool operator==(const AddressSpace &other) const
Definition: AddressSpace.cc:432
TTAMachine
Definition: Assembler.hh:48
TTAMachine::AddressSpace::shared_
bool shared_
True in case this address space maps to a memory that is shared across all the cores in the multicore...
Definition: AddressSpace.hh:106
TTAMachine::AddressSpace::minAddress_
ULongWord minAddress_
Lowest address in the address space.
Definition: AddressSpace.hh:99
TTAMachine::AddressSpace::numericalIds_
IDSet numericalIds_
The numerical ids mapped to this address space.
Definition: AddressSpace.hh:103
TTAMachine::AddressSpace::start
virtual ULongWord start() const
Definition: AddressSpace.cc:166
TTAMachine::Machine::Navigator
Definition: Machine.hh:186
TTAMachine::AddressSpace::end
virtual ULongWord end() const
Definition: AddressSpace.cc:177
TTAMachine::AddressSpace::maxAddress_
ULongWord maxAddress_
Highest address in the address space.
Definition: AddressSpace.hh:101
ObjectState::setValue
void setValue(const std::string &value)
TTAMachine::AddressSpace::setName
virtual void setName(const std::string &name)
Definition: AddressSpace.cc:191
ObjectState::setAttribute
void setAttribute(const std::string &name, const std::string &value)
Definition: ObjectState.cc:100
TTAMachine::Machine
Definition: Machine.hh:73