OpenASIP  2.0
TerminalRegister.cc
Go to the documentation of this file.
1 /*
2  Copyright (c) 2002-2011 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 TerminalRegister.cc
26  *
27  * Implementation of TerminalRegister class.
28  *
29  * @author Ari Metsähalme 2005 (ari.metsahalme-no.spam-tut.fi)
30  * @author Pekka Jääskeläinen 2011
31  * @note rating: red
32  */
33 
34 #include "TerminalRegister.hh"
35 #include "ImmediateUnit.hh"
36 #include "DisassemblyRegister.hh"
37 #include "UnboundedRegisterFile.hh"
38 
39 using namespace TTAMachine;
40 
41 namespace TTAProgram {
42 
43 /**
44  * The constructor.
45  *
46  * @param unit The register file.
47  * @param port The port of the register file.
48  * @param index Register identifier.
49  */
50 TerminalRegister::TerminalRegister(const Port& port, int index)
51  : unit_(*port.parentUnit()), port_(port), index_(index), isImmUnit_(false) {
52  if (dynamic_cast<const ImmediateUnit*>(&unit_) != NULL) {
53  isImmUnit_ = true;
54  } else if (dynamic_cast<const RegisterFile*>(&unit_) == NULL) {
55  throw InvalidData(
56  __FILE__, __LINE__, "TerminalRegister::TerminalRegister()",
57  "Unit of the terminal has invalid type");
58  }
59 }
60 
61 /**
62  * The destructor.
63  */
65 }
66 
67 /**
68  * Returns the register file of the general-purpose register.
69  *
70  * Applicable only if the unit of the terminal is an instance of
71  * RegisterFile.
72  *
73  * @return The register file of the general-purpose register.
74  * @exception WrongSubclass if the unit of the terminal is not an instance
75  * of RegisterFile.
76  */
77 const RegisterFile&
79  if (isImmUnit_ == false) {
80  return static_cast<const RegisterFile&>(unit_);
81  } else {
82  throw WrongSubclass(
83  __FILE__, __LINE__, "TerminalRegister::registerFile()",
84  "Unit of the terminal is not of type RegisterFile");
85  }
86 }
87 
88 /**
89  * Returns the immediate unit of the long immediate register.
90  *
91  * Applicable only if the unit of the terminal is an instance of
92  * ImmediateUnit.
93  *
94  * @return The immediate unit of the long immediate register.
95  * @exception WrongSubclass if the unit of the terminal is not an instance
96  * of ImmediateUnit.
97  */
98 const ImmediateUnit&
100  if (isImmUnit_ == true) {
101  return static_cast<const ImmediateUnit&>(unit_);
102  } else {
103  throw WrongSubclass(
104  __FILE__, __LINE__, "TerminalRegister::immediateUnit()",
105  "Unit of the terminal is not of type ImmediateUnit");
106  }
107 }
108 
109 /**
110  * Return the port.
111  *
112  * @return The port of the unit of the terminal.
113  * @exception WrongSubclass never.
114  */
115 const Port&
117  return port_;
118 }
119 
120 /**
121  * Change the register of the register file to the given index.
122  *
123  * @param index The new register index.
124  * @exception OutOfRange if index is not smaller than the size of the
125  * register file or immediate unit it belongs to.
126  */
127 void
129  const BaseRegisterFile& reg =
130  dynamic_cast<const BaseRegisterFile&>(unit_);
131 
132  if (index < reg.numberOfRegisters()) {
133  index_ = index;
134  } else {
135  throw OutOfRange(
136  __FILE__, __LINE__, "TerminalRegister::setIndex()",
137  "Index out of range.");
138  }
139 }
140 
141 /**
142  * Creates an exact copy of the terminal and returns it.
143  *
144  * @return A copy of the terminal.
145  */
146 Terminal*
148  return new TerminalRegister(port_, index_);
149 }
150 
151 /**
152  * Checks if terminals are equal.
153  *
154  * @param other Terminal to compare.
155  * @return true if terminals are equal.
156  */
157 bool
158 TerminalRegister::equals(const Terminal& other) const {
159  try {
160  if (isImmediateRegister()) {
161  return (other.isImmediateRegister() &&
162  index() == other.index() &&
163  immediateUnit().name() == other.immediateUnit().name());
164  }
165  if (isGPR()) {
166  return (other.isGPR() &&
167  index() == other.index() &&
168  registerFile().name() == other.registerFile().name());
169  }
170  return false;
171  } catch (const WrongSubclass&) {
172  // the method should throw this when called for wrong type,
173  // thus the objects cannot be equal
174  return false;
175  }
176 }
177 
178 TCEString
180  if (isImmUnit_ == false) {
181  DisassemblyRegister disasm(registerFile().name(), index());
182  return disasm.toString();
183  } else {
184  TCEString res = unit_.name();
185  res << "." << index_;
186  return res;
187  }
188 }
189 
190 /**
191  * Tells whether the terminal is a reg in UniversalMachine
192  *
193  * @return True if the terminal is a register in universalmachine,
194  * ie. not in any real machine. This practically means the
195  * register is unallocated and meant to be bypassed.
196  */
197 bool
199  return (dynamic_cast<const UnboundedRegisterFile*>(&unit_) != NULL);
200 }
201 
202 }
TTAProgram
Definition: Estimator.hh:65
TTAProgram::TerminalRegister::isGPR
virtual bool isGPR() const
DisassemblyRegister::toString
std::string toString() const
Definition: DisassemblyRegister.cc:66
TTAMachine::Component::name
virtual TCEString name() const
Definition: MachinePart.cc:125
TTAProgram::TerminalRegister::setIndex
virtual void setIndex(int index)
Definition: TerminalRegister.cc:128
TTAProgram::Terminal::index
virtual int index() const
Definition: Terminal.cc:274
UnboundedRegisterFile
Definition: UnboundedRegisterFile.hh:43
TTAProgram::Terminal::registerFile
virtual const TTAMachine::RegisterFile & registerFile() const
Definition: Terminal.cc:225
OutOfRange
Definition: Exception.hh:320
TTAProgram::TerminalRegister::isImmediateRegister
virtual bool isImmediateRegister() const
ImmediateUnit.hh
TTAProgram::TerminalRegister::isUniversalMachineRegister
virtual bool isUniversalMachineRegister() const
Definition: TerminalRegister.cc:198
TTAProgram::TerminalRegister::immediateUnit
virtual const TTAMachine::ImmediateUnit & immediateUnit() const
Definition: TerminalRegister.cc:99
TTAProgram::TerminalRegister::TerminalRegister
TerminalRegister(const TTAMachine::Port &port, int index)
Definition: TerminalRegister.cc:50
TTAProgram::TerminalRegister::index
virtual int index() const
TTAProgram::TerminalRegister::unit_
const TTAMachine::Unit & unit_
Unit of the terminal.
Definition: TerminalRegister.hh:82
TerminalRegister.hh
TTAMachine::BaseRegisterFile::numberOfRegisters
virtual int numberOfRegisters() const
TTAProgram::Terminal::isImmediateRegister
virtual bool isImmediateRegister() const
Definition: Terminal.cc:97
TTAMachine::BaseRegisterFile
Definition: BaseRegisterFile.hh:48
TTAProgram::TerminalRegister::~TerminalRegister
virtual ~TerminalRegister()
Copying is allowed.
Definition: TerminalRegister.cc:64
TTAProgram::TerminalRegister::port
virtual const TTAMachine::Port & port() const
Definition: TerminalRegister.cc:116
InvalidData
Definition: Exception.hh:149
WrongSubclass
Definition: Exception.hh:336
TTAMachine::Port
Definition: Port.hh:54
TTAProgram::TerminalRegister::isImmUnit_
bool isImmUnit_
Unit type flag: true if immediate unit, false if register file.
Definition: TerminalRegister.hh:88
UnboundedRegisterFile.hh
TTAProgram::Terminal::isGPR
virtual bool isGPR() const
Definition: Terminal.cc:107
TTAProgram::Terminal::immediateUnit
virtual const TTAMachine::ImmediateUnit & immediateUnit() const
Definition: Terminal.cc:240
TTAProgram::TerminalRegister::toString
virtual TCEString toString() const
Definition: TerminalRegister.cc:179
TTAProgram::TerminalRegister::port_
const TTAMachine::Port & port_
Port of the unit.
Definition: TerminalRegister.hh:84
DisassemblyRegister.hh
TTAProgram::TerminalRegister::registerFile
virtual const TTAMachine::RegisterFile & registerFile() const
Definition: TerminalRegister.cc:78
TTAProgram::TerminalRegister::equals
virtual bool equals(const Terminal &other) const
Definition: TerminalRegister.cc:158
false
find Finds info of the inner loops in the false
Definition: InnerLoopFinder.cc:81
TTAProgram::TerminalRegister::copy
virtual Terminal * copy() const
Definition: TerminalRegister.cc:147
TCEString
Definition: TCEString.hh:53
TTAProgram::Terminal
Definition: Terminal.hh:60
DisassemblyRegister
Definition: DisassemblyRegister.hh:51
TTAMachine::RegisterFile
Definition: RegisterFile.hh:47
TTAMachine
Definition: Assembler.hh:48
TTAProgram::TerminalRegister::index_
short index_
Index of the register of the register file or immediate unit.
Definition: TerminalRegister.hh:86
TTAMachine::ImmediateUnit
Definition: ImmediateUnit.hh:50