OpenASIP 2.2
Loading...
Searching...
No Matches
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
34#include "OperationContext.hh"
35#include "Application.hh"
36#include "Memory.hh"
37#include "OperationState.hh"
38#include "SimValue.hh"
40#include "Exception.hh"
41#include <string>
42
43using 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 */
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 */
112bool
113OperationContextPimpl::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 */
125void
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 */
139OperationContextPimpl::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 */
154void
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 */
177void
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 */
196void
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 */
213Memory&
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 */
226void
230
231/**
232 * Returns the unique id of the OperationContext instance.
233 *
234 * @return The unique id for the OperationContext instance.
235 */
236int
240
241/**
242 * Returns the FU name of the OperationContext instance.
243 *
244 * @return The FU name for the OperationContext instance.
245 */
246const TCEString&
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 */
264
265/**
266 * TODO
267 */
268void
272
273/**
274 * TODO
275 */
276bool
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 */
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 */
302void
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 */
313bool
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 */
325bool
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 */
335bool
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 */
359int
#define __func__
#define assert(condition)
long long CycleCount
Type for storing simulation cycle counts.
Definition BaseType.hh:187
UInt32 InstructionAddress
Definition BaseType.hh:175
find Finds info of the inner loops in the false
InstructionAddress dummyInstructionAddress
void setMemory(Memory *memory)
void setUpdateProgramCounter(bool value)
InstructionAddress & programCounter()
CycleCount cycleCount_
Number of times advanceClock() has been called since the creation.
StateRegistry * stateRegistry_
The state registry.
bool updateProgramCounter_
Should program counter be updated?
static int nextContextId_
Context id for the next created context instance.
int branchDelayCycles_
Amount of delay cycles caused by pipeline.
CycleCount * cycleCountVar_
The external variable that contains the current simulation cycle count.
void advanceClock(OperationContext &)
const TCEString FUName_
Name of the FU instance – passed down from MachineStateBuilder.
const TCEString & functionUnitName()
Memory * memory_
The Memory model instance.
bool hasState(const char *name) const
std::map< std::string, OperationState * > StateRegistry
Type of state registry.
void registerState(OperationState *state)
InstructionAddress & programCounter_
Simulates the program counter value.
void setSaveReturnAddress(bool value)
SimValue & returnAddress_
Simulates the procedure return address.
void unregisterState(const char *name)
int contextId_
Unique number that identifies a context instance.
bool saveReturnAddress_
Should the return address be saved?
OperationContextPimpl(const TCEString &name)
OperationState & state(const char *name) const
virtual const char * name()=0