OpenASIP 2.2
Loading...
Searching...
No Matches
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
47using std::string;
48using boost::format;
49
50namespace TTAMachine {
51
52// initialization of static data members
53const string AddressSpace::OSNAME_ADDRESS_SPACE = "adress_space";
54const string AddressSpace::OSKEY_WIDTH = "width";
55const string AddressSpace::OSKEY_MIN_ADDRESS = "min_a";
56const string AddressSpace::OSKEY_MAX_ADDRESS = "max_a";
57const string AddressSpace::OSKEY_SHARED_MEMORY = "shared-memory";
58const 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 */
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 */
154int
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 */
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 */
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 */
190void
191AddressSpace::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 */
214void
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 */
231void
233 if (start >= end) {
234 string procName = "AddressSpace::setAddressBounds";
235 throw OutOfRange(__FILE__, __LINE__, procName);
236 }
237
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 */
249void
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 */
260void
262
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
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) {
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 */
333void
335 const string procName = "AddressSpace::loadState";
336
337 if (state->name() != OSNAME_ADDRESS_SPACE) {
338 throw ObjectStateLoadingException(__FILE__, __LINE__, procName);
339 }
340
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 */
377void
379 numericalIds_.insert(id);
380}
381
382bool
385}
386
387/**
388 * Returns ids that are assigned to this address space.
389 *
390 * @return Address space ids.
391 */
392std::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 */
403bool
404AddressSpace::setNumericalIds(const std::set<unsigned>& ids) {
405 assert (machine() != NULL);
406
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
431bool
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
440bool
442 return !this->operator==(other);
443}
444
445}
446
#define __func__
#define assert(condition)
unsigned long ULongWord
Definition BaseType.hh:51
static bool containsKey(const ContainerType &aContainer, const KeyType &aKey)
std::string errorMessage() const
Definition Exception.cc:123
bool hasAttribute(const std::string &name) const
void setAttribute(const std::string &name, const std::string &value)
void setValue(const std::string &value)
ObjectState * child(int index) const
int intValue() const
bool boolAttribute(const std::string &name) const
int intAttribute(const std::string &name) const
ULongWord uLongAttribute(const std::string &name) const
std::string name() const
int childCount() const
static const std::string OSKEY_NUMERICAL_ID
static const std::string OSNAME_ADDRESS_SPACE
ObjectState name for AddressSpace.
bool shared_
True in case this address space maps to a memory that is shared across all the cores in the multicore...
virtual void loadState(const ObjectState *state)
ULongWord minAddress_
Lowest address in the address space.
static const std::string OSKEY_SHARED_MEMORY
virtual bool hasNumericalId(unsigned id) const
ULongWord maxAddress_
Highest address in the address space.
bool operator!=(const AddressSpace &other) const
bool operator==(const AddressSpace &other) const
static const std::string OSKEY_MAX_ADDRESS
ObjectState attribute key for maximum address.
virtual ULongWord end() const
virtual void setAddressBounds(ULongWord start, ULongWord end)
AddressSpace(const std::string &name, int width, ULongWord minAddress, ULongWord maxAddress, Machine &owner)
static const std::string OSKEY_MIN_ADDRESS
ObjectState attribute key for minimum address.
virtual void setMachine(Machine &mach)
std::set< unsigned > numericalIds() const
virtual ObjectState * saveState() const
virtual void setShared(bool shared)
virtual void setName(const std::string &name)
bool setNumericalIds(const std::set< unsigned > &ids)
virtual void addNumericalId(unsigned id)
virtual void unsetMachine()
virtual int width() const
IDSet numericalIds_
The numerical ids mapped to this address space.
virtual ULongWord start() const
static const std::string OSKEY_WIDTH
ObjectState attribute key for the bit width.
int width_
Bit width of the minimum addressable word.
std::set< unsigned > IDSet
virtual void setWidth(int width)
virtual void setName(const std::string &name)
virtual Machine * machine() const
virtual void loadState(const ObjectState *state)
static const std::string OSKEY_NAME
ObjectState attribute key for the name of the component.
void internalSetMachine(Machine &machine)
virtual bool isRegistered() const
virtual TCEString name() const
virtual AddressSpace * addressSpace() const
virtual void setAddressSpace(AddressSpace *as)
ComponentType * item(int index) const
virtual FunctionUnitNavigator functionUnitNavigator() const
Definition Machine.cc:380
virtual AddressSpaceNavigator addressSpaceNavigator() const
Definition Machine.cc:392
virtual ControlUnit * controlUnit() const
Definition Machine.cc:345
virtual void deleteAddressSpace(AddressSpace &as)
Definition Machine.cc:623
virtual void addAddressSpace(AddressSpace &as)
Definition Machine.cc:248
virtual boost::format text(int textId)