OpenASIP  2.0
DataMemory.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 DataMemory.cc
26  *
27  * Implementation of DataMemory class.
28  *
29  * @author Mikael Lepistö 2006 (mikael.lepisto-no.spam-tut.fi)
30  * @note rating: red
31  */
32 
33 #include "DataMemory.hh"
34 #include "AddressSpace.hh"
35 #include "DataDefinition.hh"
36 #include "MapTools.hh"
37 #include "Conversion.hh"
38 
39 namespace TTAProgram {
40 
41 /**
42  * Sets address space of the memory.
43  *
44  * @param aSpace Address space of the memory.
45  */
47  ramSpace_(&aSpace) {
48 }
49 
50 /**
51  * Deletes all the added definitions.
52  */
55 }
56 
57 
58 /**
59  * Adds data to memory.
60  *
61  * Data definitions are sorted by starting address of defined area.
62  *
63  * @param dataDef Definition to add.
64  */
65 void
67  dataDefs_[dataDef->startAddress().location()] = dataDef;
68  // clear index cache since indices may change
69  indexCache_.clear();
70 }
71 
72 /**
73  * Finds the data definition, which contains the requested address.
74  *
75  * @param address Address whose data definition is requested.
76  * @return Data definition, which contains requested address.
77  */
80  std::map<AddressImage, DataDefinition*>::const_iterator iter =
81  dataDefs_.upper_bound(address.location());
82 
83  if (iter != dataDefs_.begin()) {
84  iter--;
85  DataDefinition* dataDef = (*iter).second;
86  // this data definition should contain the requested address
87  if (address.location() < dataDef->startAddress().location() +
88  dataDef->size()) {
89  return *dataDef;
90  }
91  }
92 
93  throw NotAvailable(
94  __FILE__, __LINE__, __func__,
95  "Address space: " + ramSpace_->name() +
96  " does not contain data definition for requested address:" +
97  Conversion::toString(address.location()));
98 }
99 
100 
101 /**
102  * Finds the data definition by index.
103  *
104  * Indices may change if new definitions are added to memory.
105  *
106  * @param index Index of the requested data definition.
107  * @return Data definition, which contains requested address.
108  */
110 DataMemory::dataDefinition(int index) const {
111  if (indexCache_.empty()) {
112  // create cache
113  for (std::map<AddressImage, DataDefinition*>::const_iterator iter =
114  dataDefs_.begin(); iter != dataDefs_.end(); iter++) {
115 
116  indexCache_.push_back((*iter).second);
117  }
118  }
119 
120  return *indexCache_[index];
121 }
122 
123 /**
124  * Returns the number of data definitions stored in memory.
125  *
126  * @return Number of data definitions.
127  */
128 int
130  return dataDefs_.size();
131 }
132 
133 
134 /**
135  * Returns the place where the data definitions end. Calculates it based
136  * only on the last data definition (since dataDefs_ is sorted).
137  *
138  * @return int end of data definitions
139  */
140 int
142  std::map<AddressImage, DataDefinition*>::const_reverse_iterator iter =
143  dataDefs_.rbegin();
144 
145  if (iter != dataDefs_.rend()) {
146  DataDefinition* dataDef = (*iter).second;
147  // this data definition should be the last one
148  return dataDef->startAddress().location() + dataDef->size();
149  }
150  // no data definitions added
151  return 0;
152 }
153 
154 /**
155  * Deletes the data definition at the given index.
156  *
157  * @param index Which data definition to delete.
158  */
159 void
161 
162  if (index < 0 || index >= dataDefinitionCount()) {
163  throw OutOfRange(__FILE__, __LINE__, __func__);
164  }
165 
166  DataDefMap::iterator iter = dataDefs_.begin();
167 
168  for (int i = 0; i < index; i++) {
169  iter++;
170  }
171 
172  dataDefs_.erase(iter);
173  indexCache_.clear();
174 }
175 
176 /**
177  * Returns the address space of the data memory.
178  *
179  * @return The address space of the data memory.
180  */
183  return *ramSpace_;
184 }
185 
186 /**
187  * Set new address space for the data memory.
188  *
189  * @param space New address space.
190  */
191 void
193  ramSpace_ = &space;
194  for (int i = 0; i < dataDefinitionCount(); i++) {
195  DataDefinition& def = dataDefinition(i);
196  def.setStartAddress(Address(def.startAddress().location(), space));
197  }
198 }
199 
200 /**
201  * POM style copy constructor, which supports dynamic binding.
202  *
203  * @return Copy of the object.
204  */
205 DataMemory*
207  DataMemory* memCopy = new DataMemory(*ramSpace_);
208 
209  for (int i = 0; i < dataDefinitionCount(); i++) {
210  memCopy->addDataDefinition(dataDefinition(i).copy());
211  }
212 
213  return memCopy;
214 }
215 
216 }
TTAProgram::DataMemory::setAddressSpace
void setAddressSpace(const TTAMachine::AddressSpace &space)
Definition: DataMemory.cc:192
TTAProgram
Definition: Estimator.hh:65
TTAProgram::DataDefinition
Definition: DataDefinition.hh:52
TTAMachine::Component::name
virtual TCEString name() const
Definition: MachinePart.cc:125
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
TTAMachine::AddressSpace
Definition: AddressSpace.hh:51
OutOfRange
Definition: Exception.hh:320
MapTools.hh
TTAProgram::DataMemory::addressSpace
const TTAMachine::AddressSpace & addressSpace() const
Definition: DataMemory.cc:182
AddressSpace.hh
MapTools::deleteAllValues
static void deleteAllValues(MapType &aMap)
TTAProgram::DataMemory::dataDefinitionCount
int dataDefinitionCount() const
Definition: DataMemory.cc:129
TTAProgram::DataMemory::indexCache_
std::vector< DataDefinition * > indexCache_
Cache for indexing dataDefinitions.
Definition: DataMemory.hh:85
Conversion::toString
static std::string toString(const T &source)
NotAvailable
Definition: Exception.hh:728
TTAProgram::DataMemory::~DataMemory
virtual ~DataMemory()
Definition: DataMemory.cc:53
TTAProgram::DataMemory::dataDefs_
DataDefMap dataDefs_
Data definitions for the address space.
Definition: DataMemory.hh:82
TTAProgram::DataMemory::addDataDefinition
void addDataDefinition(DataDefinition *dataDef)
Definition: DataMemory.cc:66
DataMemory.hh
TTAProgram::DataMemory::DataMemory
DataMemory(const TTAMachine::AddressSpace &aSpace)
Definition: DataMemory.cc:46
Conversion.hh
__func__
#define __func__
Definition: Application.hh:67
TTAProgram::DataMemory::dataDefinitionsEnd
int dataDefinitionsEnd() const
Definition: DataMemory.cc:141
TTAProgram::Address::location
InstructionAddress location() const
TTAProgram::DataMemory::dataDefinition
DataDefinition & dataDefinition(Address address) const
Definition: DataMemory.cc:79
TTAProgram::DataMemory::copy
DataMemory * copy() const
Definition: DataMemory.cc:206
TTAProgram::DataMemory
Definition: DataMemory.hh:56
TTAProgram::DataMemory::deleteDataDefinition
void deleteDataDefinition(int index)
Definition: DataMemory.cc:160
TTAProgram::DataDefinition::copy
virtual DataDefinition * copy() const
Definition: DataDefinition.cc:262
DataDefinition.hh
TTAProgram::DataMemory::ramSpace_
const TTAMachine::AddressSpace * ramSpace_
Address space of the memory.
Definition: DataMemory.hh:79
TTAProgram::DataDefinition::setStartAddress
virtual void setStartAddress(Address start)
Definition: DataDefinition.cc:134