OpenASIP  2.0
TPEFASpaceSectionReader.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 TPEFASpaceSectionReader.cc
26  *
27  * Definition of TPEFASpaceSectionReader class.
28  *
29  * @author Mikael Lepistö 2003 (tmlepist-no.spam-cs.tut.fi)
30  *
31  * @note rating: yellow
32  */
33 
35 #include "ASpaceSection.hh"
36 #include "SafePointer.hh"
37 #include "Exception.hh"
38 #include "ReferenceKey.hh"
39 #include "SectionReader.hh"
40 #include "TPEFBaseType.hh"
41 #include "ASpaceElement.hh"
42 #include "BinaryStream.hh"
43 
44 namespace TPEF {
45 
46 using ReferenceManager::SafePointer;
47 using ReferenceManager::SectionIndexKey;
48 using ReferenceManager::SectionOffsetKey;
49 
50 TPEFASpaceSectionReader TPEFASpaceSectionReader::proto_;
51 
52 /**
53  * Constructor.
54  *
55  * Registers itself to SectionReader.
56  */
59 }
60 
61 /**
62  * Destructor.
63  */
65 }
66 
67 /**
68  * Returns the type of section it is meant to read.
69  *
70  * @return The type of section it can read.
71  */
74  return Section::ST_ADDRSP;
75 }
76 
77 /**
78  * Reads section data from TPEF binary file.
79  *
80  * @param stream Stream to be read from.
81  * @param section Section where the information is to be stored.
82  * @exception UnreachableStream If reading of section fails.
83  * @exception KeyAlreadyExists Key was in use when trying to register object.
84  * @exception EndOfFile If end of file were reached while it shouldn't.
85  * @exception OutOfRange Some of read value were out of range.
86  * @exception WrongSubclass Some class couldn't do what it was asked for.
87  * @exception UnexpectedValue If there was unexpected value when reading.
88  */
89 void
91  BinaryStream& stream, Section* section) const {
92  // base classes implementation must be called with these.
93  TPEFSectionReader::readData(stream, section);
94 
95  // just being paranoid...
96  ASpaceSection* aSpaceSection =
97  dynamic_cast<ASpaceSection*>(section);
98  assert(aSpaceSection != NULL);
99 
100  std::set<Byte> alreadyUsedIds;
101 
102  bool undefFound = false;
103 
104  // check that link section is defined properly
105  assert(header().linkId != 0);
106 
107  if (!section->noBits()) {
108  // start of first element
109  SectionOffset elementStart = header().bodyOffset;
110 
111  while (elementStart + header().elementSize <=
112  header().bodyOffset + header().bodyLength) {
113 
114  ASpaceElement *elem = new ASpaceElement();
115 
116  // first byte of address space element is a unique identifier
117  Byte id = stream.readByte();
118 
119  // check that the identifier is really unique
120  if (alreadyUsedIds.find(id) != alreadyUsedIds.end()) {
121  bool twoASpaceElementsWithSameId = false;
122  assert(twoASpaceElementsWithSameId);
123  } else {
124  alreadyUsedIds.insert(id);
125  }
126 
127  SectionIndexKey sectionIndexKey(
128  dynamic_cast<TPEFReader*>(parent())->aSpaceId(), id);
129  SafePointer::addObjectReference(sectionIndexKey, elem);
130 
131  elem->setMAU(stream.readByte());
132  elem->setAlign(stream.readByte());
133  elem->setWordSize(stream.readByte());
134 
135  SectionOffsetKey nameSectionOffset(
136  header().linkId, stream.readWord());
137 
138  elem->setName(CREATE_SAFEPOINTER(nameSectionOffset));
139 
140  // check undef aspace
141  if (id == 0) {
142  assert(elem->MAU() == 0);
143  assert(elem->align() == 0);
144  assert(elem->wordSize() == 0);
145  assert(nameSectionOffset.offset() == 0);
146  aSpaceSection->setUndefinedASpace(elem);
147  undefFound = true;
148  }
149 
150  section->addElement(elem);
151 
152  elementStart += header().elementSize;
153  stream.setReadPosition(elementStart);
154  }
155  }
156 
157  // if there was no undef aspace create it (if nobits flag is set)
158  if (!undefFound) {
159  ASpaceElement *elem = new ASpaceElement();
160  elem->setMAU(0);
161  elem->setAlign(0);
162  elem->setWordSize(0);
163  section->addElement(elem);
164  aSpaceSection->setUndefinedASpace(elem);
165  }
166 }
167 }
ASpaceElement.hh
TPEF::ReferenceManager::SectionOffsetKey::offset
SectionOffset offset() const
TPEF::TPEFSectionReader::Header::bodyOffset
Word bodyOffset
Definition: TPEFSectionReader.hh:69
TPEF::BinaryStream::setReadPosition
void setReadPosition(unsigned int position)
Definition: BinaryStream.cc:629
Exception.hh
TPEF::ASpaceElement::setAlign
void setAlign(Byte aAlign)
TPEF::TPEFASpaceSectionReader::proto_
static TPEFASpaceSectionReader proto_
Prototype instance of TPEFASpaceSectionReader to be registered to SectionReader.
Definition: TPEFASpaceSectionReader.hh:63
TPEF::TPEFASpaceSectionReader::type
virtual Section::SectionType type() const
Definition: TPEFASpaceSectionReader.cc:73
TPEF::BinaryStream
Definition: BinaryStream.hh:59
SafePointer.hh
TPEF::ASpaceElement::wordSize
Byte wordSize() const
TPEF::ASpaceElement::MAU
Byte MAU() const
Byte
unsigned char Byte
Definition: BaseType.hh:116
TPEF::ReferenceManager::SectionIndexKey
Definition: ReferenceKey.hh:65
TPEF::ReferenceManager::SafePointer::addObjectReference
static void addObjectReference(SectionIndexKey key, const SafePointable *obj)
Definition: SafePointer.cc:306
TPEF::Section
Definition: Section.hh:64
TPEF::TPEFSectionReader::Header::elementSize
Word elementSize
Definition: TPEFSectionReader.hh:68
assert
#define assert(condition)
Definition: Application.hh:86
TPEF::Section::addElement
virtual void addElement(SectionElement *element)
Definition: Section.cc:133
TPEF::ASpaceElement::setWordSize
void setWordSize(Byte aWordSize)
TPEF::ASpaceElement
Definition: ASpaceElement.hh:48
SectionReader.hh
TPEF::SectionReader::registerSectionReader
static void registerSectionReader(const SectionReader *sReader)
Definition: SectionReader.cc:145
TPEF::TPEFSectionReader
Definition: TPEFSectionReader.hh:48
TPEF::BinaryStream::readByte
Byte readByte()
Definition: BinaryStream.cc:120
TPEF::TPEFASpaceSectionReader::TPEFASpaceSectionReader
TPEFASpaceSectionReader()
Definition: TPEFASpaceSectionReader.cc:57
ASpaceSection.hh
TPEF::Section::noBits
bool noBits() const
TPEF::TPEFSectionReader::parent
virtual BinaryReader * parent() const
Definition: TPEFSectionReader.cc:65
TPEF::SectionOffset
Word SectionOffset
Type for storing offsets relative to a given base offset value.
Definition: TPEFBaseType.hh:49
TPEF::ASpaceElement::setMAU
void setMAU(Byte aMAU)
TPEF::TPEFASpaceSectionReader::readData
virtual void readData(BinaryStream &stream, Section *section) const
Definition: TPEFASpaceSectionReader.cc:90
TPEF::TPEFSectionReader::readData
virtual void readData(BinaryStream &stream, Section *section) const
Definition: TPEFSectionReader.cc:86
TPEF::TPEFReader
Definition: TPEFReader.hh:52
TPEF::TPEFASpaceSectionReader::~TPEFASpaceSectionReader
virtual ~TPEFASpaceSectionReader()
Definition: TPEFASpaceSectionReader.cc:64
TPEFASpaceSectionReader.hh
TPEF::ASpaceElement::setName
void setName(const ReferenceManager::SafePointer *aName)
TPEF::ASpaceSection::setUndefinedASpace
void setUndefinedASpace(ASpaceElement *aSpace)
Definition: ASpaceSection.cc:109
TPEF::ReferenceManager::SectionOffsetKey
Definition: ReferenceKey.hh:93
BinaryStream.hh
TPEF::Section::SectionType
SectionType
Definition: Section.hh:69
TPEFBaseType.hh
ReferenceKey.hh
TPEF::BinaryStream::readWord
Word readWord()
Definition: BinaryStream.cc:187
TPEF::ASpaceSection
Definition: ASpaceSection.hh:44
TPEF::ASpaceElement::align
Byte align() const
TPEF::Section::ST_ADDRSP
@ ST_ADDRSP
Address space section.
Definition: Section.hh:77
TPEF::TPEFSectionReader::header
static const Header & header()
Definition: TPEFSectionReader.cc:174
TPEF
Definition: Assembler.hh:43