OpenASIP  2.0
OperationContextPimpl.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 OperationContextPimpl.cc
26  *
27  * Definition of OperationContextPimpl (private implementation) class.
28  *
29  * @author Viljami Korhonen 2008 (viljami.korhonen-no.spam-tut.fi)
30  * @note rating: red
31  */
32 
33 #include "OperationContextPimpl.hh"
34 #include "OperationContext.hh"
35 #include "Application.hh"
36 #include "Memory.hh"
37 #include "OperationState.hh"
38 #include "SimValue.hh"
39 #include "OperationContextPimpl.hh"
40 #include "Exception.hh"
41 #include <string>
42 
43 using std::string;
44 
45 /// Id given for the next created OperationContext instance.
47 
50 
51 /**
52  * Constructor for contexts suitable for basic operations.
53  */
55  memory_(NULL),
56  programCounter_(dummyInstructionAddress),
57  returnAddress_(NullSimValue::instance()),
58  saveReturnAddress_(false),
59  updateProgramCounter_(false),
60  cycleCount_(0),
61  cycleCountVar_(NULL),
62  FUName_(name),
63  branchDelayCycles_(dummyInt) {
66 }
67 
68 /**
69  * Constructor for contexts suitable for any kinds of operations.
70  *
71  * @param memory The memory model instance.
72  * @param nww The natural word width of the memory model.
73  * @param programCounter The program counter register.
74  * @param returnAddress The return address register.
75  * @param syscallHandler The syscall handler register.
76  * @param syscallNumber The syscall code register.
77  *
78  */
80  const TCEString& name,
81  Memory* memory,
82  InstructionAddress& programCounter,
83  SimValue& returnAddress,
84  int delayCycles) :
85  memory_(memory),
86  programCounter_(programCounter),
87  returnAddress_(returnAddress),
88  saveReturnAddress_(false),
89  updateProgramCounter_(false),
90  cycleCount_(0),
91  cycleCountVar_(NULL),
92  FUName_(name),
93  branchDelayCycles_(delayCycles) {
96 }
97 
98 /**
99  * Default destructor
100  */
102  delete stateRegistry_;
103  stateRegistry_ = NULL;
104 }
105 
106 /**
107  * Checks if state with given name can be found in the context.
108  *
109  * @param name Name of the state.
110  * @return True if the state is found.
111  */
112 bool
113 OperationContextPimpl::hasState(const char* name) const {
114  try {
115  state(name);
116  } catch (const KeyNotFound&) {
117  return false;
118  }
119  return true;
120 }
121 
122 /**
123  * Generates an unique context id for the current OperationContext instance.
124  */
125 void
128  nextContextId_++;
129 }
130 
131 /**
132  * Looks up the (concrete) operation state identified by the string name.
133  *
134  * @param name The state identifier.
135  * @return The found operation state instance.
136  * @exception KeyNotFound If state by name couldn't be found.
137  */
139 OperationContextPimpl::state(const char* name) const {
140 
141  StateRegistry::const_iterator i = stateRegistry_->find(name);
142 
143  if (i == stateRegistry_->end()) {
144  throw KeyNotFound(__FILE__, __LINE__, __func__,
145  "OperationContextPimpl: State not found.");
146  }
147 
148  return *(*i).second;
149 }
150 
151 /**
152  * Advances the internal clock of each registered operation state object.
153  */
154 void
156 
157  StateRegistry::iterator i = stateRegistry_->begin();
158 
159  while (i != stateRegistry_->end()) {
160  (*i).second->advanceClock(context);
161  ++i;
162  }
163  ++cycleCount_;
164 }
165 
166 /**
167  * Registers the operation state for given name.
168  *
169  * Called in the createState() of the custom OperationBehavior classes.
170  * If state with given identifier is already found in the context, program
171  * is aborted. This method is only for internal use. Used by the macro
172  * definitions of OSAL.hh.
173  *
174  * @param name The identifier for the registered state instance.
175  * @param stateToRegister The state instance.
176  */
177 void
179  string stateName = stateToRegister->name();
180  // RegisterState() and unregisterState() are supposed to be used
181  // only internally, they are not part of the "client IF", therefore
182  // it's reasonable to assert.
183  assert(!hasState(stateName.c_str()));
184  (*stateRegistry_)[stateName] = stateToRegister;
185 }
186 
187 /**
188  * Unregisters the operation state of given name.
189  *
190  * This method is only for internal use. Used by the macro
191  * definitions of OSAL.hh.
192  *
193  * Called in deleteState() of the custom OperationBehavior classes.
194  * Aborts if there's no state with given identifier in the context.
195  */
196 void
198  // RegisterState() and unregisterState() are supposed to be used
199  // only internally, they are not part of the "client IF", therefore
200  // it's reasonable to assert.
201  assert(hasState(name));
202  stateRegistry_->erase(name);
203 }
204 
205 /**
206  * Returns a reference to a Memory Module wrapper instance.
207  *
208  * This instance can be accessed by the function unit and behavior
209  * simulation methods to simulate memory access.
210  *
211  * @return The Memory instance.
212  */
213 Memory&
215  return *memory_;
216 }
217 
218 /**
219  * Sets the reference to a Memory Module instance.
220  *
221  * This instance can be accessed by the function unit and behavior
222  * simulation methods to simulate memory access.
223  *
224  * @param memory The Memory instance.
225  */
226 void
228  memory_ = memory;
229 }
230 
231 /**
232  * Returns the unique id of the OperationContext instance.
233  *
234  * @return The unique id for the OperationContext instance.
235  */
236 int
238  return contextId_;
239 }
240 
241 /**
242  * Returns the FU name of the OperationContext instance.
243  *
244  * @return The FU name for the OperationContext instance.
245  */
246 const TCEString&
248  return FUName_;
249 }
250 
251 /**
252  * Returns a reference to the current value of the program counter register.
253  *
254  * The value of the program counter can be changed through this reference.
255  * This is used to implement control transfer operations like jumps and calls
256  * to subroutines.
257  *
258  * @return The program counter value as a reference.
259  */
262  return programCounter_;
263 }
264 
265 /**
266  * TODO
267  */
268 void
270  updateProgramCounter_ = value;
271 }
272 
273 /**
274  * TODO
275  */
276 bool
278  return updateProgramCounter_;
279 }
280 
281 /**
282  * Returns a reference to the current value of the return address register.
283  *
284  * The value of the return address can be changed through this reference.
285  * This is used in implementing calls to subroutines.
286  *
287  * @return The return address value as a reference.
288  */
289 SimValue&
291  return returnAddress_;
292 }
293 
294 /**
295  * Makes the return address to be saved in the RA register.
296  *
297  * This is used by CALL instruction to save the RA before jumping.
298  *
299  * @param value Value to set to.
300  * @return The return address value as a reference.
301  */
302 void
304  saveReturnAddress_ = value;
305 }
306 
307 /**
308  * Returns true if RA should saved before executing next control flow
309  * operation.
310  *
311  * @return The return address value as a reference.
312  */
313 bool
315  return saveReturnAddress_;
316 }
317 
318 /**
319  * Returns true if there are no operation state objects stored in the
320  * context.
321  *
322  * @return True if there are no operation state objects stored in the
323  * context.
324  */
325 bool
327  return stateRegistry_->size() == 0;
328 }
329 
330 /**
331  * Returns true if the context has memory model associated with it.
332  *
333  * @return True if the context has memory model associated with it.
334  */
335 bool
337  return (memory_ != NULL);
338 }
339 
340 /**
341  * Returns the count of cycles simulated.
342  *
343  * Can be used to implement real time timer operations, etc. Uses
344  * the simulator's cycle count, if available, otherwise uses internal
345  * cycle counter that is incremented with the advanceClock();
346  */
349  if (cycleCountVar_ != NULL) {
350  return *cycleCountVar_;
351  } else {
352  return cycleCount_;
353  }
354 }
355 
356 /**
357  * Returns the amount of pipeline delay cycles.
358  */
359 int
361  return branchDelayCycles_;
362 }
OperationContextPimpl::unregisterState
void unregisterState(const char *name)
Definition: OperationContextPimpl.cc:197
OperationContextPimpl::registerState
void registerState(OperationState *state)
Definition: OperationContextPimpl.cc:178
OperationState
Definition: OperationState.hh:46
InstructionAddress
UInt32 InstructionAddress
Definition: BaseType.hh:175
OperationContextPimpl::returnAddress_
SimValue & returnAddress_
Simulates the procedure return address.
Definition: OperationContextPimpl.hh:111
OperationContextPimpl.hh
Exception.hh
Memory.hh
OperationContext
Definition: OperationContext.hh:56
OperationContextPimpl::StateRegistry
std::map< std::string, OperationState * > StateRegistry
Type of state registry.
Definition: OperationContextPimpl.hh:54
OperationContextPimpl::setUpdateProgramCounter
void setUpdateProgramCounter(bool value)
Definition: OperationContextPimpl.cc:269
OperationContextPimpl::contextId_
int contextId_
Unique number that identifies a context instance.
Definition: OperationContextPimpl.hh:105
dummyInt
int dummyInt
Definition: OperationContextPimpl.cc:49
OperationContextPimpl::saveReturnAddress_
bool saveReturnAddress_
Should the return address be saved?
Definition: OperationContextPimpl.hh:115
OperationState.hh
OperationContextPimpl::advanceClock
void advanceClock(OperationContext &)
Definition: OperationContextPimpl.cc:155
OperationContextPimpl::cycleCountVar_
CycleCount * cycleCountVar_
The external variable that contains the current simulation cycle count.
Definition: OperationContextPimpl.hh:123
OperationContextPimpl::state
OperationState & state(const char *name) const
Definition: OperationContextPimpl.cc:139
SimValue
Definition: SimValue.hh:96
assert
#define assert(condition)
Definition: Application.hh:86
OperationContextPimpl::cycleCount_
CycleCount cycleCount_
Number of times advanceClock() has been called since the creation.
Definition: OperationContextPimpl.hh:120
OperationContextPimpl::hasMemoryModel
bool hasMemoryModel() const
Definition: OperationContextPimpl.cc:336
OperationContextPimpl::setMemory
void setMemory(Memory *memory)
Definition: OperationContextPimpl.cc:227
OperationContextPimpl::memory_
Memory * memory_
The Memory model instance.
Definition: OperationContextPimpl.hh:103
OperationContextPimpl::branchDelayCycles
int branchDelayCycles()
Definition: OperationContextPimpl.cc:360
OperationContextPimpl::OperationContextPimpl
OperationContextPimpl(const TCEString &name)
Definition: OperationContextPimpl.cc:54
OperationContextPimpl::returnAddress
SimValue & returnAddress()
Definition: OperationContextPimpl.cc:290
Application.hh
OperationContextPimpl::FUName_
const TCEString FUName_
Name of the FU instance – passed down from MachineStateBuilder.
Definition: OperationContextPimpl.hh:125
OperationContextPimpl::updateProgramCounter_
bool updateProgramCounter_
Should program counter be updated?
Definition: OperationContextPimpl.hh:117
__func__
#define __func__
Definition: Application.hh:67
OperationContextPimpl::memory
Memory & memory()
Definition: OperationContextPimpl.cc:214
OperationState::name
virtual const char * name()=0
OperationContextPimpl::programCounter
InstructionAddress & programCounter()
Definition: OperationContextPimpl.cc:261
OperationContextPimpl::contextId
int contextId() const
Definition: OperationContextPimpl.cc:237
OperationContextPimpl::cycleCount
CycleCount cycleCount() const
Definition: OperationContextPimpl.cc:348
OperationContextPimpl::initializeContextId
void initializeContextId()
Definition: OperationContextPimpl.cc:126
OperationContextPimpl::~OperationContextPimpl
~OperationContextPimpl()
Definition: OperationContextPimpl.cc:101
OperationContextPimpl::updateProgramCounter
bool updateProgramCounter() const
Definition: OperationContextPimpl.cc:277
OperationContextPimpl::nextContextId_
static int nextContextId_
Context id for the next created context instance.
Definition: OperationContextPimpl.hh:107
false
find Finds info of the inner loops in the false
Definition: InnerLoopFinder.cc:81
CycleCount
long long CycleCount
Type for storing simulation cycle counts.
Definition: BaseType.hh:187
SimValue.hh
TCEString
Definition: TCEString.hh:53
OperationContextPimpl::saveReturnAddress
bool saveReturnAddress()
Definition: OperationContextPimpl.cc:314
OperationContextPimpl::setSaveReturnAddress
void setSaveReturnAddress(bool value)
Definition: OperationContextPimpl.cc:303
NullSimValue
Definition: SimValue.hh:237
KeyNotFound
Definition: Exception.hh:285
OperationContextPimpl::branchDelayCycles_
int branchDelayCycles_
Amount of delay cycles caused by pipeline.
Definition: OperationContextPimpl.hh:127
dummyInstructionAddress
InstructionAddress dummyInstructionAddress
Definition: OperationContextPimpl.cc:48
OperationContextPimpl::functionUnitName
const TCEString & functionUnitName()
Definition: OperationContextPimpl.cc:247
OperationContextPimpl::hasState
bool hasState(const char *name) const
Definition: OperationContextPimpl.cc:113
OperationContextPimpl::programCounter_
InstructionAddress & programCounter_
Simulates the program counter value.
Definition: OperationContextPimpl.hh:109
OperationContext.hh
OperationContextPimpl::stateRegistry_
StateRegistry * stateRegistry_
The state registry.
Definition: OperationContextPimpl.hh:113
Memory
Definition: Memory.hh:74
OperationContextPimpl::isEmpty
bool isEmpty() const
Definition: OperationContextPimpl.cc:326