OpenASIP 2.2
Loading...
Searching...
No Matches
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
37namespace TPEF {
38
39CodeSection CodeSection::proto_(true);
40
41/**
42 * Constructor.
43 *
44 * @param init True if registeration is wanted.
45 */
47 Section() {
48 if (init) {
50 }
53}
54
55/**
56 * Destructor.
57 */
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 */
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 */
88CodeSection::element(Word index) const {
89 return dynamic_cast<InstructionElement*>(Section::element(index));
90}
91
92/**
93 * Clears instruction startpoint cache.
94 */
95void
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 */
109void
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
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 */
147Word
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 */
165CodeSection::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 */
182Word
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 */
197Word
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 */
213bool
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 */
231Word
234
235 // get instruction element index
236 Word elementIndex =
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 */
266void
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 */
278void
283
284}
#define assert(condition)
static KeyType keyForValue(const MapType &aMap, const ValueType &aValue)
static bool containsKey(const MapType &aMap, const KeyType &aKey)
bool isInSection(const InstructionElement &elem) const
virtual ~CodeSection()
virtual SectionType type() const
virtual InstructionElement * element(Word index) const
static CodeSection proto_
Prototype instance of section.
InstructionElement & instruction(Word index) const
virtual Section * clone() const
Word instructionToSectionIndex(Word index) const
virtual void addElement(SectionElement *element)
Word instructionCount() const
void initInstructionCache() const
Word indexOfInstruction(const InstructionElement &elem) const
virtual void setElement(Word index, SectionElement *element)
std::vector< Word > instructionStartCache_
Cache vector of element indexes of those instruction elements, which begins a new instruction.
Word indexOfElement(const InstructionElement &elem) const
void clearInstructionCache() const
std::map< const InstructionElement *, Word > elementIndexCache_
Cache of indexes of instruction elemenets.
CodeSection(bool init)
virtual void setElement(Word index, SectionElement *element)
Definition Section.cc:145
static void registerSection(const Section *section)
Definition Section.cc:114
virtual void addElement(SectionElement *element)
Definition Section.cc:133
@ ST_CODE
Text section.
Definition Section.hh:79
void unsetFlagNoBits()
SectionElement * element(Word index) const
Word elementCount() const
void setFlagVLen()