OpenASIP  2.0
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 
36 #include "AOutTextSectionReader.hh"
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 
46 namespace TPEF {
47 
48 using std::string;
49 using std::vector;
50 using ReferenceManager::SafePointer;
51 using ReferenceManager::SectionKey;
52 using ReferenceManager::SectionOffsetKey;
53 using ReferenceManager::SectionIndexKey;
54 
56 AOutTextSectionReader AOutTextSectionReader::proto_;
57 
58 
59 /**
60  * Constructor.
61  *
62  * Registers itself to SectionReader.
63  */
66 }
67 
68 /**
69  * Destructor.
70  */
72 }
73 
74 /**
75  * Returns section type which reader can read.
76  *
77  * @return The type of the section that reader can read.
78  */
81  return Section::ST_CODE;
82 }
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  */
100 void
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  */
161 void
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  */
189 void
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  */
208 Word
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
218  if (reg < AOutReader::FIRST_FP_REGISTER) {
219  regOffset = AOutReader::FIRST_INT_REGISTER;
220 
221  // fp reg
222  } else if (reg < AOutReader::FIRST_BOOL_REGISTER) {
223  regOffset = AOutReader::FIRST_FP_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  */
249 void
251  MoveElement* move, const HalfWord dest) const {
252  if (dest < AOutReader::FIRST_FP_REGISTER) {
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  */
285 void
287  MoveElement* move, const Word src) const {
288  if (src < AOutReader::FIRST_FP_REGISTER) {
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  */
318 void
320  InstructionElement* elem, SectionOffset sectionOffset,
321  SectionId sectionID) const {
322  SectionOffsetKey offKey = SectionOffsetKey(sectionID, sectionOffset);
323  SafePointer::addObjectReference(offKey, elem);
324 }
325 }
TPEF::SectionId
HalfWord SectionId
Type for storing binary file section ids.
Definition: TPEFBaseType.hh:43
TPEF::AOutTextSectionReader::initializeMove
void initializeMove(BinaryStream &stream, MoveElement *move) const
Definition: AOutTextSectionReader.cc:190
TPEF::MoveElement::setGuardInverted
void setGuardInverted(bool flag)
TPEF::AOutTextSectionReader::updateMoveDestination
void updateMoveDestination(MoveElement *move, const HalfWord dest) const
Definition: AOutTextSectionReader.cc:250
TPEF::AOutTextSectionReader::initializeImmediateMove
void initializeImmediateMove(BinaryStream &stream, MoveElement *move, ImmediateElement *immediate) const
Definition: AOutTextSectionReader.cc:162
TPEF::BinaryStream::readPosition
unsigned int readPosition()
Definition: BinaryStream.cc:561
TPEF::AOutTextSectionReader::OFFSET_TO_IMMEDIATE_VALUE
static const int OFFSET_TO_IMMEDIATE_VALUE
Definition: AOutTextSectionReader.hh:51
TPEF::AOutTextSectionReader::~AOutTextSectionReader
virtual ~AOutTextSectionReader()
Definition: AOutTextSectionReader.cc:71
TPEF::ResourceElement::BOOL_RF
@ BOOL_RF
Universal boolean register file.
Definition: ResourceElement.hh:68
TPEF::InstructionElement
Definition: InstructionElement.hh:77
TPEF::MoveElement::setDestinationUnit
void setDestinationUnit(HalfWord aDestinationUnit)
TPEF::MoveElement::MF_UNIT
@ MF_UNIT
Function unit.
Definition: MoveElement.hh:56
TPEF::BinaryStream
Definition: BinaryStream.hh:59
TPEF::ImmediateElement::setDestinationIndex
void setDestinationIndex(Byte aDestinationIndex)
TPEF::ImmediateElement::setWord
void setWord(Word aValue)
TPEF::MoveElement::setGuarded
void setGuarded(bool flag)
TPEF::MoveElement::MF_IMM
@ MF_IMM
Immediate.
Definition: MoveElement.hh:55
SafePointer.hh
TPEF::AOutTextSectionReader::convertAOutIndexToTPEF
Word convertAOutIndexToTPEF(const Word reg) const
Definition: AOutTextSectionReader.cc:209
TPEF::ImmediateElement
Definition: ImmediateElement.hh:49
TPEF::MoveElement::setDestinationType
void setDestinationType(FieldType aType)
Byte
unsigned char Byte
Definition: BaseType.hh:116
ResourceSection.hh
TPEF::ReferenceManager::SafePointer::addObjectReference
static void addObjectReference(SectionIndexKey key, const SafePointable *obj)
Definition: SafePointer.cc:306
TPEF::AOutTextSectionReader::readData
virtual void readData(BinaryStream &stream, Section *section) const
Definition: AOutTextSectionReader.cc:101
ImmediateElement.hh
TPEF::MoveElement::setBus
void setBus(HalfWord aBus)
TPEF::Section
Definition: Section.hh:64
TPEF::MoveElement::MF_RF
@ MF_RF
Register file.
Definition: MoveElement.hh:54
AOutSymbolSectionReader.hh
TPEF::ImmediateElement::setDestinationUnit
void setDestinationUnit(Byte aDestinationUnit)
assert
#define assert(condition)
Definition: Application.hh:86
TPEF::Section::addElement
virtual void addElement(SectionElement *element)
Definition: Section.cc:133
TPEF::AOutReader::FIRST_BOOL_REGISTER
static const Word FIRST_BOOL_REGISTER
Index of the first Boolean register.
Definition: AOutReader.hh:135
TPEF::AOutTextSectionReader::proto_
static AOutTextSectionReader proto_
Prototype to be registered to SectionReader.
Definition: AOutTextSectionReader.hh:84
TPEF::AOutReader
Definition: AOutReader.hh:54
TPEF::MoveElement::setGuardIndex
void setGuardIndex(HalfWord aGuardIndex)
TPEF::ResourceElement::INLINE_IMM
@ INLINE_IMM
Inline immediate unit id.
Definition: ResourceElement.hh:72
TPEF::AOutSectionReader::parent
virtual BinaryReader * parent() const
Definition: AOutSectionReader.cc:57
Conversion.hh
TPEF::MoveElement
Definition: MoveElement.hh:47
SectionReader.hh
TPEF::FileOffset
Word FileOffset
Type for storing absolute file offsets.
Definition: TPEFBaseType.hh:52
TPEF::SectionReader::registerSectionReader
static void registerSectionReader(const SectionReader *sReader)
Definition: SectionReader.cc:145
TPEF::MoveElement::setDestinationIndex
void setDestinationIndex(HalfWord aDestinationIndex)
TPEF::BinaryStream::readHalfWord
HalfWord readHalfWord()
Definition: BinaryStream.cc:150
TPEF::BinaryStream::readByte
Byte readByte()
Definition: BinaryStream.cc:120
TPEF::AOutReader::ST_TEXT
@ ST_TEXT
Text section.
Definition: AOutReader.hh:116
TPEF::AOutTextSectionReader::setReference
void setReference(InstructionElement *elem, SectionOffset sectionOffset, SectionId sectionID) const
Definition: AOutTextSectionReader.cc:319
TPEF::MoveElement::setGuardType
void setGuardType(FieldType gType)
TPEF::InstructionElement::setBegin
void setBegin(bool isBegin)
TPEF::SectionOffset
Word SectionOffset
Type for storing offsets relative to a given base offset value.
Definition: TPEFBaseType.hh:49
TPEF::AOutTextSectionReader::updateMoveSource
void updateMoveSource(MoveElement *move, const Word src) const
Definition: AOutTextSectionReader.cc:286
TPEF::AOutReader::header
static const Header & header()
TPEF::MoveElement::setSourceUnit
void setSourceUnit(HalfWord aSourceUnit)
TPEF::AOutReader::FIRST_FU_REGISTER
static const Word FIRST_FU_REGISTER
Index of the first function unit register.
Definition: AOutReader.hh:137
TPEF::AOutSectionReader
Definition: AOutSectionReader.hh:45
AOutTextSectionReader.hh
TPEF::MoveElement::setSourceIndex
void setSourceIndex(HalfWord aSourceIndex)
TPEF::ReferenceManager::SectionOffsetKey
Definition: ReferenceKey.hh:93
TPEF::Section::SectionType
SectionType
Definition: Section.hh:69
TPEF::ResourceElement::UNIVERSAL_BUS
@ UNIVERSAL_BUS
Universal bus.
Definition: ResourceElement.hh:64
TPEF::ResourceElement::FP_RF
@ FP_RF
Universal floating point register file.
Definition: ResourceElement.hh:69
TPEF::Section::ST_CODE
@ ST_CODE
Text section.
Definition: Section.hh:79
TPEF::ResourceElement::INT_RF
@ INT_RF
Universal integer register file.
Definition: ResourceElement.hh:67
TPEF::AOutReader::FIRST_FP_REGISTER
static const Word FIRST_FP_REGISTER
Index of the first floating-point register.
Definition: AOutReader.hh:133
ReferenceKey.hh
TPEF::BinaryStream::readWord
Word readWord()
Definition: BinaryStream.cc:187
TPEF::AOutTextSectionReader::AOutTextSectionReader
AOutTextSectionReader()
Definition: AOutTextSectionReader.cc:64
MoveElement.hh
TPEF::AOutReader::Header::sectionSizeText
Word sectionSizeText() const
TPEF::AOutTextSectionReader::type
virtual Section::SectionType type() const
Definition: AOutTextSectionReader.cc:80
TPEF::AOutReader::FIRST_INT_REGISTER
static const Word FIRST_INT_REGISTER
Index of the first integer register in a.out.
Definition: AOutReader.hh:131
TPEF::MoveElement::setSourceType
void setSourceType(FieldType aType)
TPEF
Definition: Assembler.hh:43
TPEF::MoveElement::setGuardUnit
void setGuardUnit(HalfWord aGuardUnit)
TPEF::ResourceElement::UNIVERSAL_FU
@ UNIVERSAL_FU
Universal function unit.
Definition: ResourceElement.hh:65