OpenASIP  2.0
ImmediateUnit.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 ImmediateUnit.cc
26  *
27  * Implementation of ImmediateUnit class.
28  *
29  * @author Lasse Laasonen 2003 (lasse.laasonen-no.spam-tut.fi)
30  * @author Esa Määttä 2007 (esa.maatta-no.spam-tut.fi)
31  */
32 
33 #include "ImmediateUnit.hh"
34 #include "Port.hh"
35 #include "ObjectState.hh"
36 
37 using std::string;
38 
39 // constant latency value for immediate unit
40 const int LATENCY = 1;
41 
42 // constant max writes value for immediate unit
43 const int MAX_WRITES = 1;
44 
45 // initialization of static data members
47  "immediate-unit";
48 const string TTAMachine::ImmediateUnit::OSKEY_EXTENSION = "extension";
49 const string TTAMachine::ImmediateUnit::OSVALUE_SIGN = "sign";
50 const string TTAMachine::ImmediateUnit::OSVALUE_ZERO = "zero";
51 const string TTAMachine::ImmediateUnit::OSKEY_LATENCY = "latency";
52 
53 namespace TTAMachine {
54 
55 /**
56  * Constructor.
57  *
58  * @param name Name of the immediate unit.
59  * @param size Number of immediate registers in the immediate unit.
60  * @param width Bit width of the long immediate registers in the immediate
61  * unit.
62  * @param extension Extension mode applied to the long immediate when it is
63  * narrower than the immediate register,
64  * see Machine::Extension.
65  * @param latency Number of cycles needed between the encoding of a long
66  * immediate and its earliest transport on a data bus.
67  * The value "0" indicates that the long immediate can be
68  * read onto a bus in the same cycle in which the instruction
69  * that encodes it is executed.
70  * @exception OutOfRange If the given size or width is invalid.
71  * @exception InvalidName If the given name is not a valid name for a
72  component.
73  */
75  const string& name, unsigned int size, unsigned int width,
76  unsigned int maxReads, unsigned int guardLatency,
77  Machine::Extension extension)
78  : RegisterFile(
79  name, size, width, maxReads, MAX_WRITES, guardLatency,
81  extension_(extension),
82  latency_(LATENCY) {
84  setWidth(width);
85 }
86 
87 /**
88  * Constructor.
89  *
90  * Loads the state of the immediate unit from the given ObjectState instance.
91  * Does not load references to other components.
92  *
93  * @param state The ObjectState instance.
94  * @exception ObjectStateLoadingException If the given ObjectState instance
95  * is invalid or if connections to
96  * other machine parts cannot be made.
97  */
100 }
101 
102 /**
103  * Destructor.
104  */
106  unsetMachine();
107 }
108 
109 
110 /**
111  * Sets the name of the immediate unit.
112  *
113  * @param name Name of the immediate unit.
114  * @exception ComponentAlreadyExists If an immediate unit with the given
115  * name is already in the same machine.
116  * @exception InvalidName If the given name is not a valid name for a
117  * component.
118  */
119 void
120 ImmediateUnit::setName(const string& name) {
121  if (name == this->name()) {
122  return;
123  }
124 
125  if (machine() != NULL) {
126  if (machine()->immediateUnitNavigator().hasItem(name)) {
127  string procName = "ImmediateUnit::setName";
128  throw ComponentAlreadyExists(__FILE__, __LINE__, procName);
129  } else {
131  }
132  } else {
134  }
135 }
136 
137 /**
138  * Returns the extension mode of the immediate unit.
139  *
140  * @return The extension mode of the immediate unit.
141  */
144  return extension_;
145 }
146 
147 
148 /**
149  * Returns the minimum number of cycles needed between the encoding of a long
150  * immediate and its earliest transport on a data bus.
151  *
152  * @return Latency of the immediate unit.
153  */
154 int
156  return latency_;
157 }
158 
159 
160 /**
161  * Sets the maximum number of ports that can write a register all in the same
162  * cycle.
163  *
164  * For immediate unit the value is fixed to 1.
165  *
166  * @param writes Maximum number of ports.
167  * @exception OutOfRange If the given number of maximum writes is out of
168  * range.
169  */
170 void
172  if (maxWrites != MAX_WRITES) {
173  std::string procName = "ImmediateUnit::setMaxWrites";
174  throw OutOfRange(__FILE__, __LINE__, procName);
175  }
177 }
178 
179 /**
180  * Sets the extension mode for the immediate unit.
181  *
182  * @param mode The new extension mode.
183  */
184 void
186  extension_ = mode;
187 }
188 
189 
190 /**
191  * Sets the latency of the immediate unit.
192  *
193  * @param latency The new latency.
194  * @exception OutOfRange If the given latency is less than zero.
195  */
196 void
198  if (latency != LATENCY) {
199  string procName = "ImmediateUnit::setLatency";
200  throw OutOfRange(__FILE__, __LINE__, procName);
201  }
202  latency_ = latency;
203 }
204 
205 /**
206  * Removes the immediate unit from machine.
207  */
208 void
210  Machine* mach = machine();
211  if (mach == NULL) {
212  return;
213  }
214 
217  Machine::BusNavigator busNav = mach->busNavigator();
218  for (int itIndex = 0; itIndex < itNav.count(); itIndex++) {
219  InstructionTemplate* it = itNav.item(itIndex);
220  it->removeSlots(*this);
221  }
223  mach->removeImmediateUnit(*this);
224 }
225 
226 
227 /**
228  * Saves the contents to an ObjectState tree.
229  *
230  * @return The newly created ObjectState tree.
231  */
234 
237 
238  // set extension mode
239  if (extension_ == Machine::SIGN) {
241  } else {
243  }
244 
246 
247  return iUnit;
248 }
249 
250 
251 /**
252  * Loads its state from the given ObjectState instance.
253  *
254  * @param state The ObjectState instance.
255  * @exception ObjectStateLoadingException If the given ObjectState instance
256  * is invalid or if the connections
257  * to other machine parts cannot be
258  * made.
259  */
260 void
264 }
265 
266 /**
267  * Loads the state of the immediate unit without references to other
268  * components.
269  *
270  * @param state The ObjectState instance from which the state is loaded.
271  * @exception ObjectStateLoadingException If an error occurs while loading
272  * the state.
273  */
274 void
276  const string procName = "ImmediateUnit::loadStateWithoutReferences";
277 
278  if (state->name() != OSNAME_IMMEDIATE_UNIT) {
279  throw ObjectStateLoadingException(__FILE__, __LINE__, procName);
280  }
281 
282  try {
283  string extension = state->stringAttribute(OSKEY_EXTENSION);
284  if (extension == OSVALUE_SIGN) {
286  } else if (extension == OSVALUE_ZERO) {
288  } else {
289  throw ObjectStateLoadingException(__FILE__, __LINE__, procName);
290  }
291 
293  assert(latency_ >= 0);
294 
295  } catch (const Exception& e) {
297  __FILE__, __LINE__, procName, e.errorMessage());
298  }
299 }
300 }
TTAMachine::RegisterFile::setMaxWrites
virtual void setMaxWrites(int maxWrites)
Definition: RegisterFile.cc:247
TTAMachine::BaseRegisterFile::setWidth
virtual void setWidth(int width)
Definition: BaseRegisterFile.cc:112
TTAMachine::ImmediateUnit::OSVALUE_ZERO
static const std::string OSVALUE_ZERO
ObjectState attribute value for zero extension.
Definition: ImmediateUnit.hh:81
TTAMachine::Machine::ZERO
@ ZERO
Zero extension.
Definition: Machine.hh:81
TTAMachine::Component::setName
virtual void setName(const std::string &name)
Definition: MachinePart.cc:142
mode
mode
Definition: tceopgen.cc:45
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
ObjectStateLoadingException
Definition: Exception.hh:551
OutOfRange
Definition: Exception.hh:320
TTAMachine::ImmediateUnit::extension_
Machine::Extension extension_
Definition: ImmediateUnit.hh:93
ObjectState
Definition: ObjectState.hh:59
ImmediateUnit.hh
LATENCY
const int LATENCY
Definition: ImmediateUnit.cc:40
TTAMachine::Machine::Navigator::count
int count() const
ObjectState::setName
void setName(const std::string &name)
TTAMachine::RegisterFile::maxWrites
virtual int maxWrites() const
Definition: RegisterFile.cc:135
TTAMachine::ImmediateUnit::latency
virtual int latency() const
Definition: ImmediateUnit.cc:155
TTAMachine::ImmediateUnit::~ImmediateUnit
~ImmediateUnit()
Definition: ImmediateUnit.cc:105
TTAMachine::ImmediateUnit::loadStateWithoutReferences
void loadStateWithoutReferences(const ObjectState *state)
Definition: ImmediateUnit.cc:275
TTAMachine::InstructionTemplate
Definition: InstructionTemplate.hh:49
TTAMachine::RegisterFile::loadState
virtual void loadState(const ObjectState *state)
Definition: RegisterFile.cc:433
TTAMachine::RegisterFile::saveState
virtual ObjectState * saveState() const
Definition: RegisterFile.cc:392
assert
#define assert(condition)
Definition: Application.hh:86
TTAMachine::ImmediateUnit::unsetMachine
virtual void unsetMachine()
Definition: ImmediateUnit.cc:209
Port.hh
TTAMachine::ImmediateUnit::setName
virtual void setName(const std::string &name)
Definition: ImmediateUnit.cc:120
TTAMachine::ImmediateUnit::latency_
int latency_
Definition: ImmediateUnit.hh:99
TTAMachine::ImmediateUnit::ImmediateUnit
ImmediateUnit(const std::string &name, unsigned int size, unsigned int width, unsigned int maxReads, unsigned int guardLatency, Machine::Extension extension)
Definition: ImmediateUnit.cc:74
TTAMachine::ImmediateUnit::saveState
virtual ObjectState * saveState() const
Definition: ImmediateUnit.cc:233
ObjectState.hh
TTAMachine::Machine::removeImmediateUnit
virtual void removeImmediateUnit(ImmediateUnit &unit)
Definition: Machine.cc:542
TTAMachine::ImmediateUnit::OSNAME_IMMEDIATE_UNIT
static const std::string OSNAME_IMMEDIATE_UNIT
ObjectState name for ImmediateUnit.
Definition: ImmediateUnit.hh:75
TTAMachine::ImmediateUnit::OSKEY_EXTENSION
static const std::string OSKEY_EXTENSION
ObjectState attribute key for the extension mode.
Definition: ImmediateUnit.hh:77
NORMAL
@ NORMAL
Definition: tceopgen.cc:45
Exception
Definition: Exception.hh:54
TTAMachine::InstructionTemplate::removeSlots
virtual void removeSlots(const ImmediateUnit &dstUnit)
Definition: InstructionTemplate.cc:217
ObjectState::name
std::string name() const
TTAMachine::Machine::SIGN
@ SIGN
Sign extension.
Definition: Machine.hh:82
Exception::errorMessage
std::string errorMessage() const
Definition: Exception.cc:123
TTAMachine::ImmediateUnit::loadState
virtual void loadState(const ObjectState *state)
Definition: ImmediateUnit.cc:261
TTAMachine::Component::machine
virtual Machine * machine() const
TTAMachine::Machine::busNavigator
virtual BusNavigator busNavigator() const
Definition: Machine.cc:356
TTAMachine::ImmediateUnit::OSVALUE_SIGN
static const std::string OSVALUE_SIGN
ObjectState attribute value for sign extension.
Definition: ImmediateUnit.hh:79
ComponentAlreadyExists
Definition: Exception.hh:510
ObjectState::intAttribute
int intAttribute(const std::string &name) const
Definition: ObjectState.cc:276
TTAMachine::ImmediateUnit::OSKEY_LATENCY
static const std::string OSKEY_LATENCY
ObjectState attribute key for latency.
Definition: ImmediateUnit.hh:83
TTAMachine::Machine::Extension
Extension
Definition: Machine.hh:80
TTAMachine::Machine::Navigator::item
ComponentType * item(int index) const
TTAMachine::RegisterFile
Definition: RegisterFile.hh:47
TTAMachine::ImmediateUnit::extensionMode
virtual Machine::Extension extensionMode() const
Definition: ImmediateUnit.cc:143
MAX_WRITES
const int MAX_WRITES
Definition: ImmediateUnit.cc:43
TTAMachine::ImmediateUnit::setExtensionMode
virtual void setExtensionMode(Machine::Extension mode)
Definition: ImmediateUnit.cc:185
TTAMachine::ImmediateUnit::setLatency
virtual void setLatency(int latency)
Definition: ImmediateUnit.cc:197
TTAMachine
Definition: Assembler.hh:48
TTAMachine::BaseRegisterFile::size
virtual int size() const
TTAMachine::Machine::instructionTemplateNavigator
virtual InstructionTemplateNavigator instructionTemplateNavigator() const
Definition: Machine.cc:428
TTAMachine::BaseRegisterFile::width
virtual int width() const
TTAMachine::ImmediateUnit::setMaxWrites
virtual void setMaxWrites(int maxWrites)
Definition: ImmediateUnit.cc:171
TTAMachine::Unit::unsetMachine
virtual void unsetMachine()
Definition: Unit.cc:262
TTAMachine::Machine::Navigator
Definition: Machine.hh:186
TTAMachine::RegisterFile::setNumberOfRegisters
virtual void setNumberOfRegisters(int registers)
Definition: RegisterFile.cc:320
ObjectState::setAttribute
void setAttribute(const std::string &name, const std::string &value)
Definition: ObjectState.cc:100
TTAMachine::Machine
Definition: Machine.hh:73