OpenASIP  2.0
AnnotatedInstructionElement.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 AnnotatedInstructionElement.cc
26  *
27  * Implementation of AnnotatedInstructionElement class.
28  *
29  * @author Pekka Jääskeläinen 2006 (pekka.jaaskelainen-no.spam-tut.fi)
30  * @note rating: red
31  */
32 
33 #include <utility> // std::pair
34 
36 #include "MapTools.hh"
37 #include "TCEString.hh"
38 
39 namespace TTAProgram {
40 
41 /////////////////////////////////////////////////////////////////////////////
42 // AnnotatedInstructionElement
43 /////////////////////////////////////////////////////////////////////////////
44 
45 /**
46  * Constructor.
47  */
49 }
50 
51 /**
52  * Destructor.
53  */
55 }
56 
57 /**
58  * Adds an annotation to the instruction element.
59  *
60  * @param annotation The annotation to add.
61  */
62 void
64  const ProgramAnnotation& annotation) {
65  annotations_.insert(
66  std::pair<ProgramAnnotation::Id, ProgramAnnotation>(
68 }
69 
70 /**
71  * Sets an annotation to the instruction element as the sole annotation of
72  * that type.
73  *
74  * Removes all annotations with the same type before adding the annotation.
75  *
76  * @param annotation The annotation to set.
77  */
78 void
80  const ProgramAnnotation& annotation) {
81 
82  // hmm.. does multimap::erase(key) remove *all* elements with the given
83  // key, or just first found?
84  // multimap::erase(key) returns the number of elements removed as
85  // size_type, so it erases all matching elements
86  annotations_.erase(annotation.id());
87  annotations_.insert(
88  std::pair<ProgramAnnotation::Id, ProgramAnnotation>(
90 }
91 
92 /**
93  * Returns the annotation at the given index (and id).
94  *
95  * @param index The index of the annotation.
96  * @param id The id of the annotation (optional).
97  * @exception OutOfRange If the index is out of range.
98  */
101  int index, ProgramAnnotation::Id id) const {
102  std::pair<AnnotationIndex::const_iterator,
103  AnnotationIndex::const_iterator> range(
104  annotations_.end(), annotations_.end());
105 
107  // set the iterators to point to the set of elements with the given id
108  range = annotations_.equal_range(id);
109  else
110  // go through all elements
111  range = std::make_pair(annotations_.begin(), annotations_.end());
112 
113  AnnotationIndex::const_iterator i = range.first;
114  for (int counter = 0; counter < index; ++counter) {
115  ++i;
116  if (i == annotations_.end())
117  break;
118  }
119 
120  if (i == annotations_.end())
121  throw OutOfRange(__FILE__, __LINE__, __func__);
122 
123  return (*i).second;
124 }
125 
126 /**
127  * Returns the count of annotations (with the given id).
128  *
129  * @param id The id of the annotations to count (optional).
130  * @return The count of annotations (with the given id).
131  */
132 int
135  return static_cast<int>(annotations_.count(id));
136  else
137  return static_cast<int>(annotations_.size());
138 }
139 
140 /**
141  * Removes all annotations (with the given id).
142  *
143  * @param id The id of the annotations to remove (optional).
144  */
145 void
147 
149  annotations_.clear();
150  }
151  AnnotationIndex::iterator i = annotations_.find(id);
152  while (i != annotations_.end()) {
153  annotations_.erase(i);
154  i = annotations_.find(id);
155  }
156 }
157 
158 /**
159  * Returns true in case there's at least one annotation with the given id.
160  *
161  * @param id An annotation id.
162  * @return True in case there's at least one annotation with the given id.
163  */
164 bool
166  return annotationCount(id) > 0;
167 }
168 
169 /**
170  * Returns true in case there's at least one annotation with the given id
171  * and the given data.
172  */
173 bool
175  ProgramAnnotation::Id id, const TCEString& data) const {
176 
177  auto range = annotations_.equal_range(id);
178  for (auto i = range.first; i != range.second; i++) {
179  if (i->second.stringValue() == data) {
180  return true;
181  }
182  }
183  return false;
184 }
185 
186 
187 /**
188  * Copies annotations from another annotated element.
189  *
190  * Possible old annotations are deleted.
191  */
192 void
194  const AnnotatedInstructionElement& other) {
195  annotations_ = other.annotations_;
196 }
197 
198 } // namespace TTAProgram
TTAProgram::AnnotatedInstructionElement
Definition: AnnotatedInstructionElement.hh:53
TTAProgram
Definition: Estimator.hh:65
TTAProgram::AnnotatedInstructionElement::AnnotatedInstructionElement
AnnotatedInstructionElement()
Definition: AnnotatedInstructionElement.cc:48
OutOfRange
Definition: Exception.hh:320
MapTools.hh
TTAProgram::AnnotatedInstructionElement::setAnnotation
void setAnnotation(const ProgramAnnotation &annotation)
Definition: AnnotatedInstructionElement.cc:79
TTAProgram::ProgramAnnotation::ANN_UNDEF_ID
@ ANN_UNDEF_ID
an illegal annotation ID (the id is only 24 bits, this has more meaningful bits)
Definition: ProgramAnnotation.hh:202
TTAProgram::AnnotatedInstructionElement::copyAnnotationsFrom
void copyAnnotationsFrom(const AnnotatedInstructionElement &other)
Definition: AnnotatedInstructionElement.cc:193
TTAProgram::AnnotatedInstructionElement::annotations_
AnnotationIndex annotations_
container for annotations
Definition: AnnotatedInstructionElement.hh:79
TCEString.hh
TTAProgram::ProgramAnnotation::Id
Id
the ID in TPEF is 24 bits, here enum
Definition: ProgramAnnotation.hh:52
TTAProgram::ProgramAnnotation::id
ProgramAnnotation::Id id() const
Definition: ProgramAnnotation.cc:111
TTAProgram::AnnotatedInstructionElement::hasAnnotation
bool hasAnnotation(ProgramAnnotation::Id id, const TCEString &data) const
Definition: AnnotatedInstructionElement.cc:174
__func__
#define __func__
Definition: Application.hh:67
TTAProgram::AnnotatedInstructionElement::~AnnotatedInstructionElement
~AnnotatedInstructionElement()
Definition: AnnotatedInstructionElement.cc:54
TTAProgram::AnnotatedInstructionElement::hasAnnotations
bool hasAnnotations(ProgramAnnotation::Id id=ProgramAnnotation::ANN_UNDEF_ID) const
Definition: AnnotatedInstructionElement.cc:165
TTAProgram::AnnotatedInstructionElement::annotation
ProgramAnnotation annotation(int index, ProgramAnnotation::Id id=ProgramAnnotation::ANN_UNDEF_ID) const
Definition: AnnotatedInstructionElement.cc:100
TTAProgram::AnnotatedInstructionElement::addAnnotation
void addAnnotation(const ProgramAnnotation &annotation)
Definition: AnnotatedInstructionElement.cc:63
AnnotatedInstructionElement.hh
TCEString
Definition: TCEString.hh:53
TTAProgram::ProgramAnnotation
Definition: ProgramAnnotation.hh:49
TTAProgram::AnnotatedInstructionElement::removeAnnotations
void removeAnnotations(ProgramAnnotation::Id id=ProgramAnnotation::ANN_UNDEF_ID)
Definition: AnnotatedInstructionElement.cc:146
TTAProgram::AnnotatedInstructionElement::annotationCount
int annotationCount(ProgramAnnotation::Id id=ProgramAnnotation::ANN_UNDEF_ID) const
Definition: AnnotatedInstructionElement.cc:133