OpenASIP 2.2
Loading...
Searching...
No Matches
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
44namespace TPEF {
45
46using ReferenceManager::SafePointer;
47using ReferenceManager::SectionIndexKey;
48using ReferenceManager::SectionOffsetKey;
49
50TPEFASpaceSectionReader TPEFASpaceSectionReader::proto_;
51
52/**
53 * Constructor.
54 *
55 * Registers itself to SectionReader.
56 */
60
61/**
62 * Destructor.
63 */
66
67/**
68 * Returns the type of section it is meant to read.
69 *
70 * @return The type of section it can read.
71 */
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 */
89void
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}
#define assert(condition)
unsigned char Byte
Definition BaseType.hh:116
void setName(const ReferenceManager::SafePointer *aName)
void setAlign(Byte aAlign)
Byte wordSize() const
void setMAU(Byte aMAU)
Byte align() const
void setWordSize(Byte aWordSize)
Byte MAU() const
void setUndefinedASpace(ASpaceElement *aSpace)
void setReadPosition(unsigned int position)
static void addObjectReference(SectionIndexKey key, const SafePointable *obj)
static void registerSectionReader(const SectionReader *sReader)
virtual void addElement(SectionElement *element)
Definition Section.cc:133
@ ST_ADDRSP
Address space section.
Definition Section.hh:77
bool noBits() const
virtual void readData(BinaryStream &stream, Section *section) const
virtual Section::SectionType type() const
static TPEFASpaceSectionReader proto_
Prototype instance of TPEFASpaceSectionReader to be registered to SectionReader.
virtual BinaryReader * parent() const
virtual void readData(BinaryStream &stream, Section *section) const
static const Header & header()
Word SectionOffset
Type for storing offsets relative to a given base offset value.