OpenASIP  2.0
InstructionReference.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 InstructionReference.cc
26  *
27  * Implementation of InstructionReference class.
28  *
29  * @author Ari Metsähalme 2005 (ari.metsahalme-no.spam-tut.fi)
30  * @author Heikki Kultala 2009 (heikki.kultala-no.spam-tut.fi)
31  * @note rating: red
32  */
33 
35 #include "InstructionReference.hh"
36 #include "NullInstruction.hh"
37 
38 namespace TTAProgram {
39 
40 /////////////////////////////////////////////////////////////////////////////
41 // InstructionReference
42 /////////////////////////////////////////////////////////////////////////////
43 
44 /**
45  * Constructor.
46  *
47  * @note Instruction references should be created using
48  * InstructionReferenceManager.
49  * @param ins Referred instruction.
50  */
52  impl_(impl) {
53  if (impl_ != NULL) {
54  impl_->addRef(*this);
55  }
56 }
57 
58 /**
59  * Constructor.
60  *
61  * @note Instruction references should be created using
62  * InstructionReferenceManager.
63  * @param ins Referred instruction.
64  */
66  impl_(ref.impl_) {
67  if (impl_ != NULL) {
68  impl_->addRef(*this);
69  }
70 }
71 
72 /**
73  * Assignment operator.
74  *
75  * Changes this reference to point to another instruction. Only changes
76  * this reference, not other references pointing to same instruction.
77  */
80  const InstructionReference& ref) {
81  // if both point to same instruction, no need to do anything.
82  if (ref.impl_ != impl_) {
83  // stop pointing to old instruction
84  if (impl_ != NULL) {
85  impl_->removeRef(*this);
86  }
87  // pointing to new instruction
88  impl_ = ref.impl_;
89  if (impl_ != NULL) {
90  impl_->addRef(*this);
91  }
92  }
93  return *this;
94 }
95 
96 
97 /**
98  * Destructor.
99  *
100  * Tells the impl that we are no longer pointing to it.
101  * It may get also deleted if this was the last reference to it.
102  */
104  if (impl_ != NULL) {
105  impl_->removeRef(*this);
106  }
107 }
108 
109 /**
110  * Sets a new referred instruction.
111  *
112  * The InstructionReferenceImpl is a "proxy object" that counts and
113  * keeps book of references
114  *
115  * @param newImpl New referred instruction.
116  * @return true if old impl stays alive, false if it is deleted.
117  */
118 bool
120  bool staysAlive = true;
121  assert(newImpl != impl_);
122  if (impl_ != NULL) {
123  staysAlive = impl_->removeRef(*this);
124  }
125  impl_ = newImpl;
126  if (impl_ != NULL) {
127  impl_->addRef(*this);
128  }
129  return staysAlive;
130 }
131 
132 /**
133  * Returns the referred instruction.
134  *
135  * @return Referred instruction.
136  */
139  if (impl_ == NULL) {
140  return NullInstruction::instance();
141  } else {
142  return impl_->instruction();
143  }
144 }
145 
146 bool
148  return impl_ == other.impl_;
149 }
150 
151 }
TTAProgram::InstructionReference::~InstructionReference
virtual ~InstructionReference()
Definition: InstructionReference.cc:103
TTAProgram
Definition: Estimator.hh:65
TTAProgram::InstructionReference::instruction
Instruction & instruction() const
Definition: InstructionReference.cc:138
TTAProgram::Instruction
Definition: Instruction.hh:57
TTAProgram::InstructionReferenceImpl::addRef
void addRef(InstructionReference &ref)
Definition: InstructionReferenceImpl.cc:100
assert
#define assert(condition)
Definition: Application.hh:86
TTAProgram::InstructionReference::setImpl
bool setImpl(InstructionReferenceImpl *impl)
Definition: InstructionReference.cc:119
InstructionReferenceImpl.hh
TTAProgram::InstructionReference::operator==
bool operator==(const InstructionReference &other) const
Definition: InstructionReference.cc:147
TTAProgram::InstructionReference::InstructionReference
InstructionReference(InstructionReferenceImpl *impl)
Definition: InstructionReference.cc:51
TTAProgram::InstructionReferenceImpl::instruction
Instruction & instruction()
NullInstruction.hh
TTAProgram::NullInstruction::instance
static NullInstruction & instance()
Definition: NullInstruction.cc:66
TTAProgram::InstructionReferenceImpl::removeRef
bool removeRef(InstructionReference &ref)
Definition: InstructionReferenceImpl.cc:114
InstructionReference.hh
TTAProgram::InstructionReference::impl_
InstructionReferenceImpl * impl_
Definition: InstructionReference.hh:64
TTAProgram::InstructionReference::operator=
InstructionReference & operator=(const InstructionReference &)
Definition: InstructionReference.cc:79
TTAProgram::InstructionReference
Definition: InstructionReference.hh:49
TTAProgram::InstructionReferenceImpl
Definition: InstructionReferenceImpl.hh:48