OpenASIP  2.0
TPEFResourceSectionReader.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 TPEFResourceSectionReader.cc
26  *
27  * Definition of TPEFResourceSectionReader class.
28  *
29  * @author Mikael Lepistö 2003 (tmlepist-no.spam-cs.tut.fi)
30  *
31  * @note rating: yellow
32  */
33 
35 #include "ResourceSection.hh"
36 #include "SafePointer.hh"
37 #include "Exception.hh"
38 #include "ReferenceKey.hh"
39 #include "SectionReader.hh"
40 #include "TPEFBaseType.hh"
41 #include "ResourceElement.hh"
42 #include "BinaryStream.hh"
43 
44 namespace TPEF {
45 
46 using ReferenceManager::SafePointer;
47 using ReferenceManager::SectionKey;
48 using ReferenceManager::SectionIndexKey;
49 using ReferenceManager::SectionOffsetKey;
50 
51 TPEFResourceSectionReader TPEFResourceSectionReader::proto_;
52 
53 /**
54  * Constructor.
55  *
56  * Registers itself to SectionReader.
57  */
61 }
62 
63 /**
64  * Destructor.
65  */
67 }
68 
69 /**
70  * Returns the type of section which reader can read.
71  *
72  * @return The type of section which reader can read.
73  */
76  return Section::ST_MR;
77 }
78 
79 /**
80  * Reads section data from TPEF binary file.
81  *
82  * @param stream Stream to be read from.
83  * @param section Section where the information is to be stored.
84  * @exception UnreachableStream If reading of section fails.
85  * @exception KeyAlreadyExists Key was in use when trying to register object.
86  * @exception EndOfFile If end of file were reached while it shouldn't.
87  * @exception OutOfRange Some of read value were out of range.
88  * @exception WrongSubclass Some class couldn't do what it was asked for.
89  * @exception UnexpectedValue If there was unexpected value when reading.
90  */
91 void
93  BinaryStream& stream, Section* section) const {
94  // base classes implementation must be called with these
95  TPEFSectionReader::readData(stream, section);
96 
97  ResourceSection* resourceSection =
98  dynamic_cast<ResourceSection*>(section);
99  assert(resourceSection != NULL);
100 
101  // check that link section is defined properly
102  assert(header().linkId != 0);
103 
104  if (!section->noBits()) {
105  // store start of first element
106  SectionOffset elementStart = header().bodyOffset;
107 
108  while (elementStart + header().elementSize <=
109  header().bodyOffset + header().bodyLength) {
110 
111  ResourceElement *elem = new ResourceElement();
112 
113  // TODO: check that OP, SR and PORT share the same id space and
114  // don't collide
115 
116  elem->setId(stream.readHalfWord());
117 
118  elem->setType(
119  static_cast<ResourceElement::ResourceType>
120  (stream.readByte()));
121 
123  nameSectOffset(header().linkId, stream.readWord());
124 
125  elem->setName(CREATE_SAFEPOINTER(nameSectOffset));
126 
127  elem->setInfo(stream.readWord());
128 
129  section->addElement(elem);
130 
131  elementStart += header().elementSize;
132  stream.setReadPosition(elementStart);
133  }
134  }
135 }
136 }
TPEF::ResourceSection
Definition: ResourceSection.hh:47
TPEFResourceSectionReader.hh
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::TPEFResourceSectionReader::proto_
static TPEFResourceSectionReader proto_
Prototype instance to be registered.
Definition: TPEFResourceSectionReader.hh:63
TPEF::TPEFResourceSectionReader::type
virtual Section::SectionType type() const
Definition: TPEFResourceSectionReader.cc:75
TPEF::BinaryStream
Definition: BinaryStream.hh:59
TPEF::ResourceElement
Definition: ResourceElement.hh:47
SafePointer.hh
ResourceSection.hh
TPEF::ResourceElement::setType
void setType(ResourceType aType)
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
ResourceElement.hh
SectionReader.hh
TPEF::TPEFResourceSectionReader::TPEFResourceSectionReader
TPEFResourceSectionReader()
Definition: TPEFResourceSectionReader.cc:58
TPEF::ResourceElement::setInfo
void setInfo(Word aInfo)
TPEF::SectionReader::registerSectionReader
static void registerSectionReader(const SectionReader *sReader)
Definition: SectionReader.cc:145
TPEF::BinaryStream::readHalfWord
HalfWord readHalfWord()
Definition: BinaryStream.cc:150
TPEF::ResourceElement::ResourceType
ResourceType
Resource types.
Definition: ResourceElement.hh:51
TPEF::TPEFSectionReader
Definition: TPEFSectionReader.hh:48
TPEF::BinaryStream::readByte
Byte readByte()
Definition: BinaryStream.cc:120
TPEF::Section::noBits
bool noBits() const
TPEF::SectionOffset
Word SectionOffset
Type for storing offsets relative to a given base offset value.
Definition: TPEFBaseType.hh:49
TPEF::TPEFResourceSectionReader::~TPEFResourceSectionReader
virtual ~TPEFResourceSectionReader()
Definition: TPEFResourceSectionReader.cc:66
TPEF::TPEFSectionReader::readData
virtual void readData(BinaryStream &stream, Section *section) const
Definition: TPEFSectionReader.cc:86
TPEF::ReferenceManager::SectionOffsetKey
Definition: ReferenceKey.hh:93
BinaryStream.hh
TPEF::Section::SectionType
SectionType
Definition: Section.hh:69
TPEF::ResourceElement::setId
void setId(HalfWord aId)
TPEF::TPEFResourceSectionReader::readData
virtual void readData(BinaryStream &stream, Section *section) const
Definition: TPEFResourceSectionReader.cc:92
TPEFBaseType.hh
TPEF::Section::ST_MR
@ ST_MR
Machine resources section.
Definition: Section.hh:78
ReferenceKey.hh
TPEF::BinaryStream::readWord
Word readWord()
Definition: BinaryStream.cc:187
TPEF::ResourceElement::setName
void setName(ReferenceManager::SafePointer *aName)
TPEF::TPEFSectionReader::header
static const Header & header()
Definition: TPEFSectionReader.cc:174
TPEF
Definition: Assembler.hh:43