OpenASIP  2.0
ProcedureTransferTracker.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 ProcedureTransferTracker.cc
26  *
27  * Definition of ProcedureTransferTracker class.
28  *
29  * @author Pekka Jääskeläinen 2005 (pjaaskel-no.spam-cs.tut.fi)
30  * @note rating: red
31  */
32 
34 #include "ExecutionTrace.hh"
35 #include "Application.hh"
36 #include "SimulatorFrontend.hh"
37 #include "SimulatorToolbox.hh"
39 #include "Instruction.hh"
40 #include "ControlUnit.hh"
41 #include "Move.hh"
42 #include "Terminal.hh"
43 #include "TerminalFUPort.hh"
44 #include "SpecialRegisterPort.hh"
45 #include "StringTools.hh"
46 #include "Operation.hh"
47 #include "Machine.hh"
48 #include "Program.hh"
49 
50 /**
51  * Constructor for the default implementation of storing the transfers
52  * to a TraceDB.
53  *
54  * @param subject The SimulationController which is observed.
55  * @param traceDB The Execution Trace Database instance in which the
56  * trace is stored. Expects that the database is open
57  * for writing.
58  */
60  SimulatorFrontend& subject,
61  ExecutionTrace& traceDB) :
62  Listener(), subject_(subject), traceDB_(&traceDB),
63  previousInstruction_(NULL) {
66 }
67 
68 /**
69  * Constructors for the derived implementations.
70  *
71  * The implementation should override the addProcedureTransfer() to
72  * record the transfers as wished.
73  */
75  SimulatorFrontend& subject) :
76  Listener(), subject_(subject), traceDB_(NULL),
77  previousInstruction_(NULL) {
80 }
81 
82 /**
83  * Destructor.
84  */
88 }
89 
90 /**
91  * Stored procedure transfer data to execution database.
92  *
93  * Last executed instruction is saved and its procedure is compared to the
94  * procedure of the current instruction. If they differ, a procedure transfer
95  * has happened and data of it is stored in trace database.
96  */
97 void
99 
100  InstructionAddress lastExecutedInstrAddr =
102  // the instruction that WAS executed in the current cycle (this handles
103  // clock cycle *end* events)
104  const TTAProgram::Instruction& currentInstruction =
105  subject_.program().instructionAt(lastExecutedInstrAddr);
106 
107  if (!currentInstruction.isInProcedure() ||
108  (previousInstruction_ != NULL && &(currentInstruction.parent()) ==
109  &(previousInstruction_->parent()))) {
110  // the instrution is not in a procedure (probably hand coded
111  // assembly program without procedure info), or there was no
112  // procedure transfer
113  previousInstruction_ = &currentInstruction;
114  return;
115  }
116 
117  bool entry = true;
118  // the address of the call or the return
119  InstructionAddress lastControlFlowInstructionAddress = 0;
120  // in case this is the first instruction, consider this a procedure
121  // entry (to the first procedure)
122  if (previousInstruction_ != NULL) {
123  // find the instruction which is responsible for the procedure
124  // change: it should be the instruction at the last executed
125  // instruction at the source procedure minus the count of delay
126  // slots
127  lastControlFlowInstructionAddress =
130  const TTAProgram::Instruction& lastControlFlowInstruction =
132  lastControlFlowInstructionAddress);
133 
134  // check if the instruction contains a move to gcu.jump.
135  // if there's no such a move, we'll assume it's a call
136  for (int i = 0; i < lastControlFlowInstruction.moveCount(); ++i) {
137 
138  TTAProgram::Move& lastMove = lastControlFlowInstruction.move(i);
139  if (dynamic_cast<TTAProgram::TerminalFUPort*>(
140  &lastMove.destination()) == 0) {
141  continue;
142  }
143  if (lastMove.destination().isOpcodeSetting() && lastMove.isJump()) {
144  entry = false;
145  break;
146  }
147  }
148  }
149  previousInstruction_ = &currentInstruction;
151  subject_.cycleCount(), lastExecutedInstrAddr,
152  lastControlFlowInstructionAddress, entry);
153 }
154 
155 /**
156  * This function is called to record a procedure transfer (call or return).
157  *
158  * The default implementation stores the transfer to a TraceDB.
159  */
160 void
162  ClockCycleCount cycle,
163  InstructionAddress address,
164  InstructionAddress sourceAddress,
165  bool isEntry) {
166 
167  if (traceDB_ == NULL)
168  return;
169 
170  try {
172  cycle, address, sourceAddress,
174  } catch (const Exception& e) {
175  debugLog("Error while writing TraceDB: " + e.errorMessage());
176  }
177 }
178 
ProcedureTransferTracker::previousInstruction_
const TTAProgram::Instruction * previousInstruction_
the previously executed instruction
Definition: ProcedureTransferTracker.hh:75
InstructionAddress
UInt32 InstructionAddress
Definition: BaseType.hh:175
SimulatorFrontend::program
const TTAProgram::Program & program() const
Definition: SimulatorFrontend.cc:276
TTAProgram::Instruction::move
Move & move(int i) const
Definition: Instruction.cc:193
ProcedureTransferTracker::subject_
SimulatorFrontend & subject_
the tracked SimulatorFrontend instance
Definition: ProcedureTransferTracker.hh:71
ExecutionTrace::PT_EXIT
@ PT_EXIT
procedure exit
Definition: ExecutionTrace.hh:98
SimulatorFrontend::machine
const TTAMachine::Machine & machine() const
Definition: SimulatorFrontend.cc:263
TTAProgram::Instruction
Definition: Instruction.hh:57
Listener
Definition: Listener.hh:40
TTAProgram::Move::destination
Terminal & destination() const
Definition: Move.cc:323
ExecutionTrace::PT_ENTRY
@ PT_ENTRY
procedure entry
Definition: ExecutionTrace.hh:97
Terminal.hh
StringTools.hh
TTAProgram::Program::instructionAt
Instruction & instructionAt(InstructionAddress address) const
Definition: Program.cc:374
SimulationEventHandler.hh
ProcedureTransferTracker.hh
SimulatorToolbox.hh
TTAMachine::Machine::controlUnit
virtual ControlUnit * controlUnit() const
Definition: Machine.cc:345
Instruction.hh
Informer::unregisterListener
virtual bool unregisterListener(int event, Listener *listener)
Definition: Informer.cc:104
ExecutionTrace
Definition: ExecutionTrace.hh:56
SimulationEventHandler::SE_CYCLE_END
@ SE_CYCLE_END
Generated before advancing the simulator clock at the end of a simulation cycle.
Definition: SimulationEventHandler.hh:50
Application.hh
TTAProgram::Instruction::parent
CodeSnippet & parent() const
Definition: Instruction.cc:109
Informer::registerListener
virtual bool registerListener(int event, Listener *listener)
Definition: Informer.cc:87
Operation.hh
TerminalFUPort.hh
TTAProgram::Address::location
InstructionAddress location() const
TTAProgram::Move
Definition: Move.hh:55
TTAMachine::ControlUnit::delaySlots
int delaySlots() const
Machine.hh
Exception
Definition: Exception.hh:54
SimulatorFrontend::lastExecutedInstruction
virtual InstructionAddress lastExecutedInstruction(int coreId=-1) const
Definition: SimulatorFrontend.cc:1182
SimulatorFrontend.hh
Exception::errorMessage
std::string errorMessage() const
Definition: Exception.cc:123
TTAProgram::Terminal::isOpcodeSetting
virtual bool isOpcodeSetting() const
Definition: Terminal.cc:285
TTAProgram::TerminalFUPort
Definition: TerminalFUPort.hh:56
TTAProgram::Instruction::isInProcedure
bool isInProcedure() const
Definition: Instruction.cc:135
SimulatorFrontend::cycleCount
ClockCycleCount cycleCount() const
Definition: SimulatorFrontend.cc:1194
ProcedureTransferTracker::handleEvent
virtual void handleEvent()
Definition: ProcedureTransferTracker.cc:98
ProcedureTransferTracker::~ProcedureTransferTracker
virtual ~ProcedureTransferTracker()
Definition: ProcedureTransferTracker.cc:85
Program.hh
SimulatorFrontend::eventHandler
SimulationEventHandler & eventHandler()
Definition: SimulatorFrontend.cc:2260
ControlUnit.hh
SpecialRegisterPort.hh
ProcedureTransferTracker::ProcedureTransferTracker
ProcedureTransferTracker(SimulatorFrontend &subject, ExecutionTrace &traceDB)
Definition: ProcedureTransferTracker.cc:59
ExecutionTrace::addProcedureTransfer
void addProcedureTransfer(ClockCycleCount cycle, InstructionAddress address, InstructionAddress sourceAddress, ProcedureEntryType type)
Definition: ExecutionTrace.cc:575
ClockCycleCount
CycleCount ClockCycleCount
Alias for ClockCycleCount.
Definition: SimulatorConstants.hh:57
ProcedureTransferTracker::traceDB_
ExecutionTrace * traceDB_
the trace database to store the trace to
Definition: ProcedureTransferTracker.hh:73
TTAProgram::Move::isJump
bool isJump() const
Definition: Move.cc:164
Move.hh
debugLog
#define debugLog(text)
Definition: Application.hh:95
ProcedureTransferTracker::addProcedureTransfer
virtual void addProcedureTransfer(ClockCycleCount cycle, InstructionAddress address, InstructionAddress sourceAddress, bool isEntry)
Definition: ProcedureTransferTracker.cc:161
SimulatorFrontend
Definition: SimulatorFrontend.hh:89
TTAProgram::Instruction::moveCount
int moveCount() const
Definition: Instruction.cc:176
TTAProgram::Instruction::address
Address address() const
Definition: Instruction.cc:327
ExecutionTrace.hh