OpenASIP 2.2
Loading...
Searching...
No Matches
AOutTextSectionReader.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 AOutTextSectionReader.cc
26 *
27 * Definition of AOutTextSectionReader class.
28 *
29 * @author Jussi Nykänen 2003 (nykanen-no.spam-cs.tut.fi)
30 * @author Mikael Lepistö 2003 (tmlepist-no.spam-cs.tut.fi)
31 * @note reviewed 23 October 2003 by pj, am, ll, jn
32 *
33 * @note rating: yellow
34 */
35
37#include "SafePointer.hh"
39#include "ReferenceKey.hh"
40#include "SectionReader.hh"
41#include "Conversion.hh"
42#include "ResourceSection.hh"
43#include "MoveElement.hh"
44#include "ImmediateElement.hh"
45
46namespace TPEF {
47
48using std::string;
49using std::vector;
50using ReferenceManager::SafePointer;
51using ReferenceManager::SectionKey;
52using ReferenceManager::SectionOffsetKey;
53using ReferenceManager::SectionIndexKey;
54
56AOutTextSectionReader AOutTextSectionReader::proto_;
57
58
59/**
60 * Constructor.
61 *
62 * Registers itself to SectionReader.
63 */
67
68/**
69 * Destructor.
70 */
73
74/**
75 * Returns section type which reader can read.
76 *
77 * @return The type of the section that reader can read.
78 */
83
84/**
85 * Reads all Moves from stream and stores them in to Section.
86 *
87 * One move consists of 4 fields: guard (Byte), immediate (Byte),
88 * destination index (HalfWord), and source index (Word). If immediate
89 * is 1 then the source field of the move contains an immediate value.
90 *
91 * @param stream Stream to be read from.
92 * @param section Pointer to section in which information is stored.
93 * @exception UnreachableStream If reading of section fails.
94 * @exception KeyAlreadyExists Key was in use when trying to register object.
95 * @exception EndOfFile If end of file were reached while it shouldn't.
96 * @exception OutOfRange Some of read values were out of range.
97 * @exception WrongSubclass Some class couldn't do what it was asked for.
98 * @exception UnexpectedValue If there was unexpected value when reading.
99 */
100void
102 AOutReader* aOutReader = dynamic_cast<AOutReader*>(parent());
103
104 FileOffset offset = stream.readPosition();
105 Word length = aOutReader->header().sectionSizeText();
106
107 while (stream.readPosition() < offset + length) {
108
109 SectionOffset sectionOffset = stream.readPosition() - offset;
110
111 // we must first discover whether instruction is immediate or not
112 Byte guard = stream.readByte();
113 Byte imm = stream.readByte();
114
115 MoveElement* move = new MoveElement();
116 ImmediateElement* immediate = NULL;
117
118 // guard register is the only boolean register of a.out binaries
119 assert((guard == 0) || (guard == '!') || (guard == '?'));
121 move->setGuardIndex(0);
123 move->setGuarded(guard != 0);
124 move->setGuardInverted(guard == '!');
125
126 // always use universal bus
128
129 if (imm == 1) {
130 immediate = new ImmediateElement();
131 initializeImmediateMove(stream, move, immediate);
132 immediate->setBegin(true);
133 section->addElement(immediate);
134 section->addElement(move);
135
136 // set reference pointing immediate, since move is never
137 // referenced
138 setReference(immediate, sectionOffset, AOutReader::ST_TEXT);
139
140 } else {
141 initializeMove(stream, move);
142 move->setBegin(true);
143 section->addElement(move);
144 setReference(move, sectionOffset, AOutReader::ST_TEXT);
145 }
146 }
147}
148
149/**
150 * The attributes of move that has immediate in its source field are
151 * initialized.
152 *
153 * Immediate value of a move is modeled as a separate object.
154 *
155 * @param stream Stream to be read from.
156 * @param move Move to be initialized
157 * @param immediate Immediate to be initialized.
158 * @exception OutOfRange If destination register index is too large.
159 * @exception UnreachableStream If there occurs problems with stream.
160 */
161void
163 BinaryStream& stream, MoveElement* move,
164 ImmediateElement* immediate) const {
166
167 // mark every immediate to be inline encoded
169 immediate->setDestinationIndex(0);
170
171 // mark move source to match immediate
173 move->setSourceIndex(0);
174
175 HalfWord dest = stream.readHalfWord();
176 updateMoveDestination(move, dest);
177
178 immediate->setWord(stream.readWord());
179}
180
181/**
182 * Initializes an 'ordinary' Move.
183 *
184 * @param stream Stream to be read from.
185 * @param move Move to be initialized.
186 * @exception OutOfRange If destination or source register index is too big.
187 * @exception UnreachableStream If there occurs problems with stream.
188 */
189void
191 BinaryStream& stream, MoveElement* move) const {
192 HalfWord dest = stream.readHalfWord();
193 updateMoveDestination(move, dest);
194
195 Word src = stream.readWord();
196 updateMoveSource(move, src);
197}
198
199/**
200 * Convert an a.out register index into a TPEF register index.
201 *
202 * Service helper method. The callers are responsible for ensuring that the
203 * given register index is within the allowed range.
204 *
205 * @param reg A.out index of the register.
206 * @return The TPEF index of the same register.
207 */
208Word
210
211 // NOTE: This implementation depends on AOutSymbolSectionReader
212 // implementation (Where resource section is read).
213
214 // offset to first index of given register class
215 int regOffset = 0;
216
217 // int reg
220
221 // fp reg
222 } else if (reg < AOutReader::FIRST_BOOL_REGISTER) {
224
225 // bool register
226 } else if (reg < AOutReader::FIRST_FU_REGISTER) {
228
229 // fu or special register
230 } else {
231 regOffset = 0;
232 }
233
234 return reg - regOffset;
235}
236
237/**
238 * Convert an a.out register index to a TPEF register index and update the
239 * destination of the given move to reflect the given register index.
240 *
241 * Possible values of move destination field are indices of: integer
242 * registers, floating point registers, Boolean registers, or function unit
243 * registers. The type of the move destination is also set.
244 *
245 * @param move The Move to be updated.
246 * @param dest Index of the destination register.
247 * @exception OutOfRange If destination index is too large.
248 */
249void
251 MoveElement* move, const HalfWord dest) const {
255
256 } else if (dest < AOutReader::FIRST_BOOL_REGISTER) {
259
260 } else if (dest < AOutReader::FIRST_FU_REGISTER) {
263
264 } else {
265 // for special registers move type fields is same that for fu
268 }
269
271}
272
273/**
274 * Convert an a.out register index to a TPEF register index and update the
275 * source of the given move to reflect the given register index.
276 *
277 * Possible values of move source field are indices of: integer registers,
278 * floating point registers, Boolean registers, or function unit
279 * registers. The type of the move source is also set.
280 *
281 * @param move The Move to be updated.
282 * @param src Index of the source register.
283 * @exception OutOfRange If source index is too large.
284 */
285void
287 MoveElement* move, const Word src) const {
291
292 } else if (src < AOutReader::FIRST_BOOL_REGISTER) {
295
296 } else if (src < AOutReader::FIRST_FU_REGISTER) {
299
300 } else {
301 // for special registers move type fields is same that for fu
304 }
305
307}
308
309/**
310 * Sets references to ReferenceManager to enable referencing if
311 * it's needed later.
312 *
313 * @param elem Element to be registered to ReferenceManager.
314 * @param sectionOffset The offset of the element in the section.
315 * @param sectionID Identification code of the referenced section.
316 * @exception KeyAlreadyExists If registration fails because of existing key.
317 */
318void
320 InstructionElement* elem, SectionOffset sectionOffset,
321 SectionId sectionID) const {
322 SectionOffsetKey offKey = SectionOffsetKey(sectionID, sectionOffset);
324}
325}
#define assert(condition)
unsigned char Byte
Definition BaseType.hh:116
Word sectionSizeText() const
static const Word FIRST_INT_REGISTER
Index of the first integer register in a.out.
@ ST_TEXT
Text section.
static const Word FIRST_BOOL_REGISTER
Index of the first Boolean register.
static const Word FIRST_FU_REGISTER
Index of the first function unit register.
static const Word FIRST_FP_REGISTER
Index of the first floating-point register.
static const Header & header()
virtual BinaryReader * parent() const
Word convertAOutIndexToTPEF(const Word reg) const
void updateMoveSource(MoveElement *move, const Word src) const
void initializeMove(BinaryStream &stream, MoveElement *move) const
static AOutTextSectionReader proto_
Prototype to be registered to SectionReader.
virtual void readData(BinaryStream &stream, Section *section) const
void setReference(InstructionElement *elem, SectionOffset sectionOffset, SectionId sectionID) const
virtual Section::SectionType type() const
void initializeImmediateMove(BinaryStream &stream, MoveElement *move, ImmediateElement *immediate) const
void updateMoveDestination(MoveElement *move, const HalfWord dest) const
unsigned int readPosition()
HalfWord readHalfWord()
void setDestinationIndex(Byte aDestinationIndex)
void setDestinationUnit(Byte aDestinationUnit)
void setWord(Word aValue)
void setBegin(bool isBegin)
void setGuardIndex(HalfWord aGuardIndex)
void setSourceType(FieldType aType)
void setGuardInverted(bool flag)
void setSourceIndex(HalfWord aSourceIndex)
void setBus(HalfWord aBus)
void setDestinationUnit(HalfWord aDestinationUnit)
void setGuarded(bool flag)
void setSourceUnit(HalfWord aSourceUnit)
void setGuardUnit(HalfWord aGuardUnit)
void setDestinationIndex(HalfWord aDestinationIndex)
void setDestinationType(FieldType aType)
void setGuardType(FieldType gType)
@ MF_RF
Register file.
@ MF_IMM
Immediate.
@ MF_UNIT
Function unit.
static void addObjectReference(SectionIndexKey key, const SafePointable *obj)
@ UNIVERSAL_BUS
Universal bus.
@ UNIVERSAL_FU
Universal function unit.
@ INT_RF
Universal integer register file.
@ BOOL_RF
Universal boolean register file.
@ FP_RF
Universal floating point register file.
@ INLINE_IMM
Inline immediate unit id.
static void registerSectionReader(const SectionReader *sReader)
virtual void addElement(SectionElement *element)
Definition Section.cc:133
@ ST_CODE
Text section.
Definition Section.hh:79
HalfWord SectionId
Type for storing binary file section ids.
Word SectionOffset
Type for storing offsets relative to a given base offset value.
Word FileOffset
Type for storing absolute file offsets.