OpenASIP  2.0
CodeSection.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 CodeSection.cc
26  *
27  * Definition of CodeSection class.
28  *
29  * @author Mikael Lepistö 2003 (tmlepist-no.spam-cs.tut.fi)
30  *
31  * @note rating: yellow
32  */
33 
34 #include "CodeSection.hh"
35 #include "InstructionElement.hh"
36 
37 namespace TPEF {
38 
39 CodeSection CodeSection::proto_(true);
40 
41 /**
42  * Constructor.
43  *
44  * @param init True if registeration is wanted.
45  */
47  Section() {
48  if (init) {
50  }
51  setFlagVLen();
53 }
54 
55 /**
56  * Destructor.
57  */
59 }
60 
61 /**
62  * Returns section's type.
63  *
64  * @return Type of section.
65  */
68  return ST_CODE;
69 }
70 
71 /**
72  * Creates an instance of CodeSection.
73  *
74  * @return Newly created section.
75  */
76 Section*
78  return new CodeSection(false);
79 }
80 
81 /**
82  * Returns requested instruction element.
83  *
84  * @param index Index of requested element.
85  * @return Requested element.
86  */
88 CodeSection::element(Word index) const {
89  return dynamic_cast<InstructionElement*>(Section::element(index));
90 }
91 
92 /**
93  * Clears instruction startpoint cache.
94  */
95 void
97  instructionStartCache_.clear();
98  elementIndexCache_.clear();
99 }
100 
101 /**
102  * Initializes instruction cache.
103  *
104  * Scans through section and marks element indexes of those
105  * instruction elements that starts a new instruction.
106  *
107  * Also sets section element index for every element.
108  */
109 void
111 
112  // both caches must be initialized or uninitilized
114 
115  if (instructionStartCache_.empty()) {
116  for (Word i = 0; i < elementCount(); i++) {
117  InstructionElement* elem = element(i);
118 
119  // cache start index of instruction
120  if (elem->begin()) {
121  instructionStartCache_.push_back(i);
122  }
123 
124  // cache element index of instruction element
125  elementIndexCache_[elem] = i;
126 
127  }
128  }
129 
130  // there must be at least one instruction start
131  assert(!instructionStartCache_.empty());
132  assert(!elementIndexCache_.empty());
133 
134 }
135 
136 /**
137  * Returns number of instructions stored in section.
138  *
139  * Each instruction elements that starts new instruction are marked
140  * with begin() flag.
141  *
142  * If section elements are changed, cache must be cleared before
143  * calling this function.
144  *
145  * @return Number of instructions in code section.
146  */
147 Word
150  return instructionStartCache_.size();
151 }
152 
153 /**
154  * Returns starting element of requested instruction.
155  *
156  * See. instructionCount() for more information.
157  *
158  * If section elements are changed, cache must be cleared before
159  * calling this function.
160  *
161  * @param index Index of requested instruction.
162  * @return Starting element of requested instruction.
163  */
165 CodeSection::instruction(Word index) const {
166  return *element(instructionToSectionIndex(index));
167 }
168 
169 /**
170  * Returns element index of starting element of requested instruction.
171  *
172  * I.e. converts instruction index to instruction element index.
173  *
174  * See. instructionCount() for more information.
175  *
176  * If section elements are changed, cache must be cleared before
177  * calling this function.
178  *
179  * @param index Index of instruction
180  * @return Instruction element index of requested instruction.
181  */
182 Word
185  return instructionStartCache_[index];
186 }
187 
188 /**
189  * Returns index of instruction element.
190  *
191  * If section elements are changed, cache must be cleared before
192  * calling this function.
193  *
194  * @param elem Element whose index is returned.
195  * @return index of instruction element.
196  */
197 Word
200  return MapTools::valueForKey<Word>(elementIndexCache_, &elem);
201 
202 }
203 
204 /**
205  * Returns true if element is found from section.
206  *
207  * If section elements are changed, cache must be cleared before
208  * calling this function.
209  *
210  * @param elem Element to check.
211  * @return True if element is found from section.
212  */
213 bool
217 }
218 
219 /**
220  * Returns index of instruction of requested element.
221  *
222  * If requested element is in middle of instruction, then index of that
223  * instruction is returned.
224  *
225  * If section elements are changed, cache must be cleared before
226  * calling this function.
227  *
228  * @param elem Element whose instruction index is returned.
229  * @return index of instruction element.
230  */
231 Word
234 
235  // get instruction element index
236  Word elementIndex =
237  MapTools::valueForKey<Word>(elementIndexCache_, &elem);
238 
239  Word instrCount = instructionCount();
240  int first = 0;
241  int last = instrCount - 1;
242  Word key = elementIndex;
243 
244  int middle = 0;
245 
246  // binary search from instructionStartCache for finding instruction index
247  while (first <= last) {
248  middle = (first + last) / 2;
249  if (key > instructionStartCache_[middle]) {
250  first = middle + 1;
251  } else if (key < instructionStartCache_[middle]) {
252  last = middle - 1;
253  } else {
254  break;
255  }
256  }
257 
258  return middle;
259 }
260 
261 /**
262  * Adds an element to section and clears internal caches.
263  *
264  * @param element Element that is added to section.
265  */
266 void
270 }
271 
272 /**
273  * Sets replaces an element with another and clears internal caches.
274  *
275  * @param index Index of element that is replaced.
276  * @param element Element that is set to given index.
277  */
278 void
282 }
283 
284 }
TPEF::CodeSection::element
virtual InstructionElement * element(Word index) const
Definition: CodeSection.cc:88
TPEF::CodeSection::initInstructionCache
void initInstructionCache() const
Definition: CodeSection.cc:110
TPEF::CodeSection::addElement
virtual void addElement(SectionElement *element)
Definition: CodeSection.cc:267
TPEF::InstructionElement
Definition: InstructionElement.hh:77
TPEF::Section::setElement
virtual void setElement(Word index, SectionElement *element)
Definition: Section.cc:145
TPEF::CodeSection::~CodeSection
virtual ~CodeSection()
Definition: CodeSection.cc:58
TPEF::Section::setFlagVLen
void setFlagVLen()
InstructionElement.hh
TPEF::CodeSection::setElement
virtual void setElement(Word index, SectionElement *element)
Definition: CodeSection.cc:279
TPEF::Section
Definition: Section.hh:64
TPEF::CodeSection::indexOfElement
Word indexOfElement(const InstructionElement &elem) const
Definition: CodeSection.cc:198
TPEF::CodeSection::instructionStartCache_
std::vector< Word > instructionStartCache_
Cache vector of element indexes of those instruction elements, which begins a new instruction.
Definition: CodeSection.hh:78
assert
#define assert(condition)
Definition: Application.hh:86
TPEF::Section::element
SectionElement * element(Word index) const
TPEF::Section::addElement
virtual void addElement(SectionElement *element)
Definition: Section.cc:133
TPEF::SectionElement
Definition: SectionElement.hh:44
TPEF::CodeSection::instruction
InstructionElement & instruction(Word index) const
Definition: CodeSection.cc:165
TPEF::InstructionElement::begin
bool begin() const
TPEF::CodeSection::isInSection
bool isInSection(const InstructionElement &elem) const
Definition: CodeSection.cc:214
TPEF::Section::unsetFlagNoBits
void unsetFlagNoBits()
TPEF::Section::registerSection
static void registerSection(const Section *section)
Definition: Section.cc:114
MapTools::containsKey
static bool containsKey(const MapType &aMap, const KeyType &aKey)
TPEF::CodeSection::instructionCount
Word instructionCount() const
Definition: CodeSection.cc:148
TPEF::CodeSection::instructionToSectionIndex
Word instructionToSectionIndex(Word index) const
Definition: CodeSection.cc:183
TPEF::CodeSection::clone
virtual Section * clone() const
Definition: CodeSection.cc:77
TPEF::Section::SectionType
SectionType
Definition: Section.hh:69
TPEF::Section::ST_CODE
@ ST_CODE
Text section.
Definition: Section.hh:79
TPEF::CodeSection::proto_
static CodeSection proto_
Prototype instance of section.
Definition: CodeSection.hh:74
TPEF::CodeSection::clearInstructionCache
void clearInstructionCache() const
Definition: CodeSection.cc:96
TPEF::CodeSection::elementIndexCache_
std::map< const InstructionElement *, Word > elementIndexCache_
Cache of indexes of instruction elemenets.
Definition: CodeSection.hh:81
TPEF::CodeSection::indexOfInstruction
Word indexOfInstruction(const InstructionElement &elem) const
Definition: CodeSection.cc:232
CodeSection.hh
TPEF::CodeSection::type
virtual SectionType type() const
Definition: CodeSection.cc:67
TPEF::Section::elementCount
Word elementCount() const
TPEF
Definition: Assembler.hh:43
TPEF::CodeSection::CodeSection
CodeSection(bool init)
Definition: CodeSection.cc:46