OpenASIP  2.0
TPEFLineNumSectionReader.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 TPEFLineNumSectionReader.cc
26  *
27  * Definition of TPEFLineNumSectionReader class.
28  *
29  * @author Mikael Lepistö 2003 (tmlepist-no.spam-cs.tut.fi)
30  *
31  * @note rating: yellow
32  */
33 
35 #include "LineNumSection.hh"
36 #include "SafePointer.hh"
37 #include "Exception.hh"
38 #include "ReferenceKey.hh"
39 #include "SectionReader.hh"
40 #include "TPEFBaseType.hh"
41 #include "LineNumProcedure.hh"
42 #include "BinaryStream.hh"
43 
44 namespace TPEF {
45 
46 using ReferenceManager::SafePointer;
47 using ReferenceManager::SectionOffsetKey;
48 using ReferenceManager::SectionIndexKey;
49 using ReferenceManager::SectionKey;
50 
51 TPEFLineNumSectionReader TPEFLineNumSectionReader::proto_;
53 
54 /**
55  * Constructor.
56  *
57  * Registers itself to SectionReader.
58  */
61 }
62 
63 /**
64  * Destructor.
65  */
67 }
68 
69 /**
70  * Returns the type of section it is meant to read.
71  *
72  * @return The type of section it can read.
73  */
76  return Section::ST_LINENO;
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  LineNumSection* lineNumSection = dynamic_cast<LineNumSection*>(section);
98  assert(lineNumSection != NULL);
99 
100  // check that link section is defined properly
101  assert(header().linkId != 0);
102 
103  if (!section->noBits()) {
104  // mark start of first element
105  SectionOffset elementStart = header().bodyOffset;
106 
107  LineNumProcedure *procedure = NULL;
108 
109  while (elementStart + header().elementSize <=
110  header().bodyOffset + header().bodyLength) {
111 
112  Word elementOffset = stream.readWord();
113  HalfWord lineNum = stream.readHalfWord();
114 
115  // Let's create new procedure for our line numbers if needed.
116  if (lineNum == 0) {
117  procedure = new LineNumProcedure();
118 
119  SectionIndexKey indexKey(header().linkId, elementOffset);
120 
121  if (elementOffset != 0) {
122  procedure->setProcedureSymbol(
123  CREATE_SAFEPOINTER(indexKey));
124  }
125 
126  section->addElement(procedure);
127 
128  } else if (procedure != NULL) {
129  LineNumElement *elem = new LineNumElement();
130 
131  // where is section id off data section stored
132  SectionOffsetKey instrKey(codeSectionId_, elementOffset);
133  elem->setInstruction(CREATE_SAFEPOINTER(instrKey));
134  elem->setLineNumber(lineNum);
135 
136  // add line to last created procedure
137  procedure->addLine(elem);
138 
139  } else {
140  bool sectionShouldNotHaveLineNumbersBeforeProcedure = false;
141  assert(sectionShouldNotHaveLineNumbersBeforeProcedure);
142  }
143 
144  elementStart += header().elementSize;
145  stream.setReadPosition(elementStart);
146  }
147  }
148 }
149 
150 /**
151  * Reads info field of section header, which contains the identification
152  * code of the code section to which the line number section applies.
153  *
154  * Read position of stream will be moved 4 bytes forward.
155  *
156  * @param stream Stream where from info word is read.
157  * @param sect Section whose info field is interpreted.
158  */
159 void
161  Section* sect) const {
162 
163  // identification code of referenced section is in first 2 bytes of info
164  // field
165  codeSectionId_ = stream.readHalfWord();
166 
167  if (codeSectionId_ != 0) {
169  dynamic_cast<LineNumSection*>(sect)->setCodeSection(
170  CREATE_SAFEPOINTER(sKey));
171  } else {
172  dynamic_cast<LineNumSection*>(sect)->setCodeSection(
174  }
175 
176  // skip rest 2 of 4 bytes
177  stream.readHalfWord();
178 }
179 
180 }
TPEF::TPEFLineNumSectionReader::readInfo
virtual void readInfo(BinaryStream &stream, Section *sect) const
Definition: TPEFLineNumSectionReader.cc:160
TPEF::TPEFLineNumSectionReader::TPEFLineNumSectionReader
TPEFLineNumSectionReader()
Definition: TPEFLineNumSectionReader.cc:59
TPEF::SectionId
HalfWord SectionId
Type for storing binary file section ids.
Definition: TPEFBaseType.hh:43
TPEF::LineNumProcedure::setProcedureSymbol
void setProcedureSymbol(const ReferenceManager::SafePointer *aRef)
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::BinaryStream
Definition: BinaryStream.hh:59
TPEF::ReferenceManager::SafePointer::null
static const SafePointer null
The default SafePointer that is used in null references.
Definition: SafePointer.hh:229
TPEF::TPEFLineNumSectionReader::codeSectionId_
static SectionId codeSectionId_
Identification code of text section which is referenced from currently read section.
Definition: TPEFLineNumSectionReader.hh:65
SafePointer.hh
TPEF::TPEFLineNumSectionReader::type
virtual Section::SectionType type() const
Definition: TPEFLineNumSectionReader.cc:75
LineNumSection.hh
TPEF::ReferenceManager::SectionIndexKey
Definition: ReferenceKey.hh:65
TPEF::LineNumProcedure::addLine
void addLine(const LineNumElement *elem)
TPEF::TPEFLineNumSectionReader::readData
virtual void readData(BinaryStream &stream, Section *section) const
Definition: TPEFLineNumSectionReader.cc:92
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::LineNumElement::setLineNumber
void setLineNumber(HalfWord lineNum)
TPEF::LineNumElement
Definition: LineNumElement.hh:48
TPEF::TPEFLineNumSectionReader::proto_
static TPEFLineNumSectionReader proto_
Prototype instance of TPEFLineNumSectionReader to be registered to SectionReader.
Definition: TPEFLineNumSectionReader.hh:61
LineNumProcedure.hh
TPEFLineNumSectionReader.hh
SectionReader.hh
TPEF::SectionReader::registerSectionReader
static void registerSectionReader(const SectionReader *sReader)
Definition: SectionReader.cc:145
TPEF::BinaryStream::readHalfWord
HalfWord readHalfWord()
Definition: BinaryStream.cc:150
TPEF::TPEFSectionReader
Definition: TPEFSectionReader.hh:48
TPEF::TPEFLineNumSectionReader::~TPEFLineNumSectionReader
virtual ~TPEFLineNumSectionReader()
Definition: TPEFLineNumSectionReader.cc:66
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::Section::ST_LINENO
@ ST_LINENO
Line number section.
Definition: Section.hh:75
TPEF::ReferenceManager::SectionKey
Definition: ReferenceKey.hh:145
TPEF::TPEFSectionReader::readData
virtual void readData(BinaryStream &stream, Section *section) const
Definition: TPEFSectionReader.cc:86
TPEF::LineNumSection
Definition: LineNumSection.hh:47
TPEF::LineNumElement::setInstruction
void setInstruction(ReferenceManager::SafePointer *aRef)
TPEF::ReferenceManager::SectionOffsetKey
Definition: ReferenceKey.hh:93
BinaryStream.hh
TPEF::Section::SectionType
SectionType
Definition: Section.hh:69
TPEF::LineNumProcedure
Definition: LineNumProcedure.hh:53
TPEFBaseType.hh
ReferenceKey.hh
TPEF::BinaryStream::readWord
Word readWord()
Definition: BinaryStream.cc:187
TPEF::TPEFSectionReader::header
static const Header & header()
Definition: TPEFSectionReader.cc:174
TPEF
Definition: Assembler.hh:43