OpenASIP  2.0
ControlUnit.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 ControlUnit.cc
26  *
27  * Implementation of ControlUnit class.
28  *
29  * @author Lasse Laasonen 2004 (lasse.laasonen-no.spam-tut.fi)
30  * @author Pekka J��skel�inen 2006 (pekka.jaaskelainen-no.spam-tut.fi)
31  */
32 
33 #include "ControlUnit.hh"
34 #include "SpecialRegisterPort.hh"
35 #include "Guard.hh"
36 #include "MOMTextGenerator.hh"
37 #include "Application.hh"
38 #include "Machine.hh"
39 #include "ObjectState.hh"
40 #include "Conversion.hh"
41 
42 using std::string;
43 
44 namespace TTAMachine {
45 
46 // initialization of static data members
47 const string ControlUnit::OSNAME_CONTROL_UNIT = "control_unit";
48 const string ControlUnit::OSKEY_DELAY_SLOTS = "d_slots";
49 const string ControlUnit::OSKEY_GUARD_LATENCY = "g_latency";
50 const string ControlUnit::OSKEY_RA_PORT = "ra_port";
51 
52 /**
53  * Constructor.
54  *
55  * @param name Name of the control unit.
56  * @param delaySlots Number of delay instruction slots of the transport pipeline.
57  * @param globalGuardLatency The global guard latency.
58  * @exception OutOfRange If some of the given values is out of valid range.
59  * @exception InvalidName If the given name is not a valid component name.
60  */
62  const string& name, int delaySlots, int globalGuardLatency)
63  : FunctionUnit(name),
64  delaySlots_(delaySlots),
65  globalGuardLatency_(0),
66  raPort_(NULL) {
69 }
70 
71 /**
72  * Constructor.
73  *
74  * Loads the state of the control unit from the given ObjectState instance.
75  * Does not load references to other components.
76  *
77  * @param state The ObjectState instance from which the name is taken.
78  * @exception ObjectStateLoadingException If the given ObjectState instance
79  * is invalid.
80  */
82  : FunctionUnit(state),
83  delaySlots_(0),
84  globalGuardLatency_(0),
85  raPort_(NULL) {
87 }
88 
89 /**
90  * Destructor.
91  */
93  unsetMachine();
94 }
95 
96 
97 /**
98  * Copies the instance.
99  *
100  * Current FunctionUnit state is copied to a new FunctionUnit object.
101  *
102  * @return Copy of the instance.
103  */
106 
107  return new ControlUnit(saveState());
108 }
109 
110 
111 /**
112  * Attaches the control unit to machine.
113  *
114  * @param mach The machine.
115  * @exception ComponentAlreadyExists If the given machine already has a
116  * global control unit.
117  */
118 void
120  // if global guard latency is zero, there cannot be a register guard
121  // that reads a register of local guard latency zero
122  if (globalGuardLatency() == 0 && hasLocalGuardLatencyOfZero(mach)) {
124  }
125 
126  internalSetMachine(mach);
127  mach.setGlobalControl(*this);
128 }
129 
130 /**
131  * Detaches the control unit from machine.
132  */
133 void
135  if (!isRegistered()) {
136  return;
137  } else {
138  Machine* mach = machine();
140  mach->unsetGlobalControl();
141  }
142 }
143 
144 void
145 ControlUnit::setDelaySlots(int delaySlots) {
146  if (delaySlots < 0) {
147  throw OutOfRange(__FILE__, __LINE__, __func__);
148  }
150 }
151 
152 /**
153  * Sets the global guard latency.
154  *
155  * @param latency The new latency.
156  * @exception OutOfRange If the given latency is negative.
157  */
158 void
160  if (latency < 0) {
161  throw OutOfRange(__FILE__, __LINE__, __func__);
162  }
163 
164  // if latency is zero, there must not be a guard term that reads a
165  // registr of a register file with local guard latency of zero
166  if (latency == 0 && isRegistered()) {
168  MOMTextGenerator textGen;
169  boost::format text = textGen.text(
171  throw OutOfRange(
172  __FILE__, __LINE__, __func__, text.str());
173  }
174  }
175 
176  globalGuardLatency_ = latency;
177 }
178 
179 /**
180  * Returns the number of special register ports in the control unit.
181  *
182  * @return The number of special register ports.
183  */
184 int
186  return portCount() - operationPortCount();
187 }
188 
189 
190 /**
191  * Tells whether the control unit has a special register port of the given
192  * name.
193  *
194  * @return True if the control unit has the port, otherwise false.
195  */
196 bool
197 ControlUnit::hasSpecialRegisterPort(const std::string& name) const {
198  return hasPort(name) && !hasOperationPort(name);
199 }
200 
201 
202 /**
203  * Returns a special register port by the given index.
204  *
205  * @param index The index.
206  * @return A special register port.
207  * @exception OutOfRange If the given index is less than 0 or greater or
208  * equal to the number of special register ports.
209  */
212  const string procName = "ControlUnit::specialRegisterPort";
213 
214  if (index < 0) {
215  throw OutOfRange(__FILE__, __LINE__, procName);
216  }
217 
218  int portCount = this->portCount();
219  int srPortCount(-1);
220 
221  for (int i = 0; i < portCount; i++) {
222  BaseFUPort* port = this->port(i);
223  SpecialRegisterPort* srPort =
224  dynamic_cast<SpecialRegisterPort*>(port);
225  if (srPort != NULL) {
226  srPortCount++;
227  if (srPortCount == index) {
228  return srPort;
229  }
230  }
231  }
232 
233  throw OutOfRange(__FILE__, __LINE__, procName);
234 }
235 
236 /**
237  * Returns a special register port by the given name.
238  *
239  * @param name Name of the port.
240  * @return A special register port.
241  * @exception InstanceNotFound If a special register port does not exist by
242  * the given name.
243  */
245 ControlUnit::specialRegisterPort(const std::string& name) const {
246  const string procName = "ControlUnit::specialRegisterPort";
247 
248  if (!hasPort(name)) {
249  throw InstanceNotFound(
250  __FILE__, __LINE__, procName, "Port not found: " + name);
251  }
252 
253  Port* port = this->port(name);
254  SpecialRegisterPort* srPort = dynamic_cast<SpecialRegisterPort*>(port);
255  if (srPort == NULL) {
256  throw InstanceNotFound(__FILE__, __LINE__, procName);
257  }
258  return srPort;
259 }
260 
261 /**
262  * Binds the given port to return address port.
263  *
264  * If there is another port bound to return address port already, unbinds it.
265  *
266  * @param port The port to be bound.
267  * @exception IllegalRegistration If the given port is not a port of this
268  * control unit.
269  */
270 void
272  if (port.parentUnit() != this) {
273  const string procName = "ControlUnit::setReturnAddressPort";
274  throw IllegalRegistration(__FILE__, __LINE__, procName);
275  }
276 
277  raPort_ = const_cast<SpecialRegisterPort*>(&port);
278 }
279 
280 /**
281  * Unbinds the return address port if one exists.
282  */
283 void
285  raPort_ = NULL;
286 }
287 
288 
289 /**
290  * Tells whether the control unit has a return address port.
291  *
292  * @return True if a return address port exists, otherwise false.
293  */
294 bool
296  return raPort_ != NULL;
297 }
298 
299 
300 /**
301  * Returns the return address port.
302  *
303  * @return The return address port.
304  * @exception InstanceNotFound If there is no return address port.
305  */
308  if (raPort_ != NULL) {
309  return raPort_;
310  } else {
311  const string procName = "ControlUnit::returnAddressPort";
312  throw InstanceNotFound(__FILE__, __LINE__, procName);
313  }
314 }
315 
316 /**
317  * Saves the contents to an ObjectState tree.
318  *
319  * @return The newly created ObjectState tree.
320  */
323 
326 
327  // set delay slots
329 
330  // set global guard latency
332 
333  // set return address port
334  if (hasReturnAddressPort()) {
336  }
337 
338  return cUnit;
339 }
340 
341 
342 /**
343  * Loads its state from the given ObjectState instance.
344  *
345  * @param state The ObjectState instance.
346  * @exception ObjectStateLoadingException If the given ObjectState instance
347  * invalid or if connections to other
348  * machine parts cannot be made.
349  */
350 void
354 }
355 
356 /**
357  * Removes the given port from the control unit and unsets the binding of
358  * return address port if necessary.
359  *
360  * @param port The port to be removed.
361  */
362 void
364  if (&port == raPort_) {
366  }
368 }
369 
370 
371 /**
372  * Loads the state of the control unit without references to other
373  * components.
374  *
375  * @param state The ObjectState instance from which the state is loaded.
376  * @exception ObjectStateLoadingException If an error occurs while loading
377  * the state.
378  */
379 void
381  const string procName = "ControlUnit::loadStateWithoutReferences";
382 
383  if (state->name() != OSNAME_CONTROL_UNIT) {
384  throw ObjectStateLoadingException(__FILE__, __LINE__, procName);
385  }
386 
387  try {
390  if (state->hasAttribute(OSKEY_RA_PORT)) {
391  string portName = state->stringAttribute(OSKEY_RA_PORT);
393  } else {
395  }
396 
397  } catch (Exception& e) {
398  throw ObjectStateLoadingException(__FILE__, __LINE__, procName,
399  e.errorMessage());
400  }
401 }
402 
403 /**
404  * Tells whether the given machine has a guard that reads a register with
405  * local guard latency of zero.
406  *
407  * @param machine The machine.
408  * @return True if the machine has the guard, otherwise false.
409  */
410 bool
413  for (int i = 0; i < busNav.count(); i++) {
414  Bus* bus = busNav.item(i);
415  for (int i = 0; i < bus->guardCount(); i++) {
416  Guard* guard = bus->guard(i);
417  RegisterGuard* regGuard =
418  dynamic_cast<RegisterGuard*>(guard);
419  if (regGuard != NULL) {
420  const RegisterFile* rf = regGuard->registerFile();
421  if (rf->guardLatency() == 0) {
422  return true;
423  }
424  }
425  }
426  }
427  return false;
428 }
429 }
TTAMachine::Guard
Definition: Guard.hh:55
ObjectState::hasAttribute
bool hasAttribute(const std::string &name) const
Definition: ObjectState.cc:205
MOMTextGenerator::TXT_INVALID_GUARD_LATENCY
@ TXT_INVALID_GUARD_LATENCY
Definition: MOMTextGenerator.hh:87
TTAMachine::ControlUnit::unsetMachine
virtual void unsetMachine()
Definition: ControlUnit.cc:134
TTAMachine::ControlUnit::loadStateWithoutReferences
void loadStateWithoutReferences(const ObjectState *state)
Definition: ControlUnit.cc:380
TTAMachine::Component::name
virtual TCEString name() const
Definition: MachinePart.cc:125
ObjectState::stringAttribute
std::string stringAttribute(const std::string &name) const
Definition: ObjectState.cc:249
TTAMachine::Component::isRegistered
virtual bool isRegistered() const
Definition: MachinePart.cc:177
machine
TTAMachine::Machine * machine
the architecture definition of the estimated processor
Definition: EstimatorCmdLineUI.cc:59
TTAMachine::ControlUnit::delaySlots_
int delaySlots_
Number of delay instruction slots on the transport pipeline.
Definition: ControlUnit.hh:98
ObjectStateLoadingException
Definition: Exception.hh:551
TTAMachine::BaseFUPort::parentUnit
FunctionUnit * parentUnit() const
Definition: BaseFUPort.cc:96
TTAMachine::Unit::removePort
virtual void removePort(Port &port)
Definition: Unit.cc:235
TTAMachine::ControlUnit::globalGuardLatency_
int globalGuardLatency_
The global guard latency.
Definition: ControlUnit.hh:101
OutOfRange
Definition: Exception.hh:320
TTAMachine::Bus
Definition: Bus.hh:53
TTAMachine::BaseFUPort
Definition: BaseFUPort.hh:44
TTAMachine::ControlUnit::saveState
virtual ObjectState * saveState() const
Definition: ControlUnit.cc:322
TTAMachine::ControlUnit::OSKEY_RA_PORT
static const std::string OSKEY_RA_PORT
ObjectState attribute key for the name of the return address port.
Definition: ControlUnit.hh:88
ObjectState
Definition: ObjectState.hh:59
TTAMachine::FunctionUnit::port
virtual BaseFUPort * port(const std::string &name) const
Definition: FunctionUnit.cc:145
TTAMachine::Machine::unsetGlobalControl
virtual void unsetGlobalControl()
Definition: Machine.cc:565
TTAMachine::Machine::Navigator::count
int count() const
Texts::TextGenerator::text
virtual boost::format text(int textId)
Definition: TextGenerator.cc:94
ObjectState::setName
void setName(const std::string &name)
TTAMachine::Component::internalSetMachine
void internalSetMachine(Machine &machine)
TTAMachine::ControlUnit::setGlobalGuardLatency
void setGlobalGuardLatency(int latency)
Definition: ControlUnit.cc:159
TTAMachine::ControlUnit::OSKEY_GUARD_LATENCY
static const std::string OSKEY_GUARD_LATENCY
ObjectState attribute key for the global guard latency.
Definition: ControlUnit.hh:86
TTAMachine::ControlUnit::~ControlUnit
virtual ~ControlUnit()
Definition: ControlUnit.cc:92
TTAMachine::FunctionUnit
Definition: FunctionUnit.hh:55
TTAMachine::ControlUnit::copy
virtual ControlUnit * copy() const
Definition: ControlUnit.cc:105
TTAMachine::SpecialRegisterPort
Definition: SpecialRegisterPort.hh:48
TTAMachine::ControlUnit::raPort_
SpecialRegisterPort * raPort_
The return address port.
Definition: ControlUnit.hh:103
TTAMachine::ControlUnit
Definition: ControlUnit.hh:50
Conversion.hh
TTAMachine::RegisterGuard
Definition: Guard.hh:137
TTAMachine::Port
Definition: Port.hh:54
TTAMachine::ControlUnit::specialRegisterPort
SpecialRegisterPort * specialRegisterPort(int index) const
Definition: ControlUnit.cc:211
Application.hh
TTAMachine::ControlUnit::specialRegisterPortCount
int specialRegisterPortCount() const
Definition: ControlUnit.cc:185
__func__
#define __func__
Definition: Application.hh:67
ObjectState.hh
TTAMachine::ControlUnit::ControlUnit
ControlUnit(const std::string &name, int delaySlots, int globalGuardLatency)
Definition: ControlUnit.cc:61
Guard.hh
TTAMachine::ControlUnit::loadState
virtual void loadState(const ObjectState *state)
Definition: ControlUnit.cc:351
TTAMachine::Unit::hasPort
virtual bool hasPort(const std::string &name) const
Definition: Unit.cc:96
TTAMachine::ControlUnit::removePort
virtual void removePort(Port &port)
Definition: ControlUnit.cc:363
TTAMachine::ControlUnit::unsetReturnAddressPort
void unsetReturnAddressPort()
Definition: ControlUnit.cc:284
TTAMachine::ControlUnit::delaySlots
int delaySlots() const
Machine.hh
Exception
Definition: Exception.hh:54
TTAMachine::ControlUnit::setDelaySlots
void setDelaySlots(int delaySlots)
Definition: ControlUnit.cc:145
TTAMachine::FunctionUnit::operationPortCount
virtual int operationPortCount() const
Definition: FunctionUnit.cc:182
TTAMachine::FunctionUnit::saveState
virtual ObjectState * saveState() const
Definition: FunctionUnit.cc:677
ObjectState::name
std::string name() const
TTAMachine::Bus::guardCount
int guardCount() const
Definition: Bus.cc:441
TTAMachine::Bus::guard
Guard * guard(int index) const
Definition: Bus.cc:456
TTAMachine::ControlUnit::hasSpecialRegisterPort
bool hasSpecialRegisterPort(const std::string &name) const
Definition: ControlUnit.cc:197
TTAMachine::ControlUnit::OSKEY_DELAY_SLOTS
static const std::string OSKEY_DELAY_SLOTS
ObjectState attribute key for the number of delay slots.
Definition: ControlUnit.hh:84
TTAMachine::Unit::portCount
virtual int portCount() const
Definition: Unit.cc:135
Exception::errorMessage
std::string errorMessage() const
Definition: Exception.cc:123
TTAMachine::Machine::setGlobalControl
virtual void setGlobalControl(ControlUnit &unit)
Definition: Machine.cc:317
IllegalRegistration
Definition: Exception.hh:532
TTAMachine::FunctionUnit::hasOperationPort
virtual bool hasOperationPort(const std::string &name) const
Definition: FunctionUnit.cc:204
TTAMachine::ControlUnit::setMachine
virtual void setMachine(Machine &mach)
Definition: ControlUnit.cc:119
TTAMachine::ControlUnit::hasLocalGuardLatencyOfZero
static bool hasLocalGuardLatencyOfZero(const Machine &machine)
Definition: ControlUnit.cc:411
TTAMachine::Component::machine
virtual Machine * machine() const
MOMTextGenerator
Definition: MOMTextGenerator.hh:40
ControlUnit.hh
TTAMachine::Machine::busNavigator
virtual BusNavigator busNavigator() const
Definition: Machine.cc:356
SpecialRegisterPort.hh
TTAMachine::ControlUnit::setReturnAddressPort
void setReturnAddressPort(const SpecialRegisterPort &port)
Definition: ControlUnit.cc:271
MOMTextGenerator.hh
TTAMachine::FunctionUnit::loadState
virtual void loadState(const ObjectState *state)
Definition: FunctionUnit.cc:706
ObjectState::intAttribute
int intAttribute(const std::string &name) const
Definition: ObjectState.cc:276
TTAMachine::FunctionUnit::unsetMachineDerived
void unsetMachineDerived()
Definition: FunctionUnit.cc:665
TTAMachine::Machine::Navigator::item
ComponentType * item(int index) const
TTAMachine::RegisterFile::guardLatency
virtual int guardLatency() const
Definition: RegisterFile.cc:333
TTAMachine::RegisterFile
Definition: RegisterFile.hh:47
TTAMachine::ControlUnit::OSNAME_CONTROL_UNIT
static const std::string OSNAME_CONTROL_UNIT
ObjectState name for ControlUnit.
Definition: ControlUnit.hh:82
TTAMachine
Definition: Assembler.hh:48
TTAMachine::ControlUnit::globalGuardLatency
int globalGuardLatency() const
TTAMachine::ControlUnit::returnAddressPort
SpecialRegisterPort * returnAddressPort() const
Definition: ControlUnit.cc:307
TTAMachine::RegisterGuard::registerFile
const RegisterFile * registerFile() const
TTAMachine::Machine::Navigator
Definition: Machine.hh:186
TTAMachine::ControlUnit::hasReturnAddressPort
bool hasReturnAddressPort() const
Definition: ControlUnit.cc:295
ObjectState::setAttribute
void setAttribute(const std::string &name, const std::string &value)
Definition: ObjectState.cc:100
InstanceNotFound
Definition: Exception.hh:304
TTAMachine::Machine
Definition: Machine.hh:73