OpenASIP  2.0
TPEFLineNumSectionWriter.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 TPEFLineNumSectionWriter.cc
26  *
27  * Definition of TPEFLineNumSectionWriter class.
28  *
29  * @author Mikael Lepistö 2004 (tmlepist-no.spam-cs.tut.fi)
30  *
31  * @note rating: yellow
32  */
33 
34 #include <list>
35 
37 #include "SafePointer.hh"
38 #include "ReferenceKey.hh"
39 #include "SectionElement.hh"
40 #include "LineNumSection.hh"
41 #include "LineNumProcedure.hh"
42 #include "LineNumElement.hh"
43 #include "SectionSizeReplacer.hh"
44 #include "SectionOffsetReplacer.hh"
45 #include "SectionIndexReplacer.hh"
46 #include "SectionIdReplacer.hh"
47 #include "BinaryStream.hh"
48 
49 namespace TPEF {
50 
51 using std::list;
52 
53 using ReferenceManager::SafePointer;
54 using ReferenceManager::SectionKey;
55 using ReferenceManager::FileOffsetKey;
56 using ReferenceManager::SectionIndexKey;
57 
58 const TPEFLineNumSectionWriter TPEFLineNumSectionWriter::instance_;
60 
61 /**
62  * Constructor.
63  *
64  * Registers itself to SectionWriter.
65  */
68 }
69 
70 /**
71  * Destructor.
72  */
74 }
75 
76 /**
77  * Returns the section type this writer can write.
78  *
79  * @return The section type writer can write.
80  */
83  return Section::ST_LINENO;
84 }
85 
86 /**
87  * Writes the data of the section in to stream.
88  *
89  * @param stream The stream to be written to.
90  * @param sect The section to be written.
91  */
92 void
94  BinaryStream& stream,
95  const Section* sect) const {
96 
97  FileOffset startOffset = stream.writePosition();
98  // file offset to data of section
100  FileOffsetKey(startOffset), sect->element(0));
101 
102  for (Word i = 0; i < sect->elementCount(); i++) {
103  LineNumProcedure* elem =
104  dynamic_cast<LineNumProcedure*>(sect->element(i));
105  assert(elem != NULL);
106 
107  if (elem->procedureSymbol() != NULL) {
109  replacer(elem->procedureSymbol(), sizeof(Word));
110  replacer.resolve();
111  } else {
112  bool procedureSymbolMustBeSet = false;
113  assert(procedureSymbolMustBeSet);
114  }
115 
116  // line number zero represents procedure
117  stream.writeHalfWord(0);
118 
119  // write line nums...
120  for(Word lineIndex = 0; lineIndex < elem->lineCount(); lineIndex++) {
121 
122  const LineNumElement* lineElem = elem->line(lineIndex);
123 
124  // write each line
125  SectionOffsetReplacer replacer(lineElem->instruction());
126  replacer.resolve();
127  stream.writeHalfWord(lineElem->lineNumber());
128  }
129  }
130  SectionSizeReplacer::setSize(sect, stream.writePosition() - startOffset);
131 }
132 
133 /**
134  * Returns the fixed size length of section elements.
135  *
136  * @return The fixed size length of section elements.
137  */
138 Word
140  return elementSize_;
141 }
142 
143 /**
144  * Writes Info field of section header.
145  *
146  * @param stream The stream where to write.
147  * @param sect Section to write.
148  */
149 void
151  BinaryStream& stream, const Section* sect) const {
152 
153  const Section* codeSection =
154  dynamic_cast<const LineNumSection*>(sect)->codeSection();
155 
156  SectionIdReplacer idReplacer(codeSection);
157  idReplacer.resolve();
158 
159  stream.writeHalfWord(0);
160 }
161 
162 }
TPEF::TPEFLineNumSectionWriter::actualWriteData
virtual void actualWriteData(BinaryStream &stream, const Section *section) const
Definition: TPEFLineNumSectionWriter.cc:93
TPEF::BinaryStream::writeHalfWord
void writeHalfWord(HalfWord halfword)
Definition: BinaryStream.cc:336
SectionSizeReplacer.hh
TPEF::TPEFLineNumSectionWriter::~TPEFLineNumSectionWriter
virtual ~TPEFLineNumSectionWriter()
Definition: TPEFLineNumSectionWriter.cc:73
SectionOffsetReplacer.hh
SectionIdReplacer.hh
TPEF::TPEFLineNumSectionWriter::TPEFLineNumSectionWriter
TPEFLineNumSectionWriter()
Definition: TPEFLineNumSectionWriter.cc:66
TPEF::BinaryStream
Definition: BinaryStream.hh:59
TPEF::LineNumProcedure::line
const LineNumElement * line(HalfWord index) const
TPEF::BinaryStream::writePosition
unsigned int writePosition()
Definition: BinaryStream.cc:592
SafePointer.hh
LineNumSection.hh
TPEF::TPEFLineNumSectionWriter::instance_
static const TPEFLineNumSectionWriter instance_
An unique instance of class.
Definition: TPEFLineNumSectionWriter.hh:68
LineNumElement.hh
TPEF::ReferenceManager::SafePointer::addObjectReference
static void addObjectReference(SectionIndexKey key, const SafePointable *obj)
Definition: SafePointer.cc:306
TPEF::ValueReplacer::resolve
void resolve()
Definition: ValueReplacer.cc:90
TPEF::Section
Definition: Section.hh:64
TPEF::TPEFLineNumSectionWriter::elementSize
virtual Word elementSize(const Section *section) const
Definition: TPEFLineNumSectionWriter.cc:139
assert
#define assert(condition)
Definition: Application.hh:86
TPEF::Section::element
SectionElement * element(Word index) const
TPEF::SectionOffsetReplacer
Definition: SectionOffsetReplacer.hh:47
TPEF::LineNumElement
Definition: LineNumElement.hh:48
TPEF::SectionWriter::registerSectionWriter
static void registerSectionWriter(const SectionWriter *sWriter)
Definition: SectionWriter.cc:148
LineNumProcedure.hh
TPEFLineNumSectionWriter.hh
TPEF::FileOffset
Word FileOffset
Type for storing absolute file offsets.
Definition: TPEFBaseType.hh:52
TPEF::TPEFLineNumSectionWriter::elementSize_
static const Word elementSize_
The fixed size address space element.
Definition: TPEFLineNumSectionWriter.hh:70
TPEF::LineNumElement::instruction
InstructionElement * instruction() const
TPEF::LineNumProcedure::procedureSymbol
SymbolElement * procedureSymbol() const
TPEF::SectionIdReplacer
Definition: SectionIdReplacer.hh:47
TPEF::Section::ST_LINENO
@ ST_LINENO
Line number section.
Definition: Section.hh:75
TPEF::ReferenceManager::FileOffsetKey
Definition: ReferenceKey.hh:121
SectionElement.hh
TPEF::LineNumSection
Definition: LineNumSection.hh:47
TPEF::SectionSizeReplacer::setSize
static void setSize(const SafePointable *obj, Word size)
Definition: SectionSizeReplacer.cc:100
SectionIndexReplacer.hh
TPEF::LineNumElement::lineNumber
HalfWord lineNumber() const
BinaryStream.hh
TPEF::LineNumProcedure::lineCount
HalfWord lineCount() const
TPEF::Section::SectionType
SectionType
Definition: Section.hh:69
TPEF::SectionIndexReplacer
Definition: SectionIndexReplacer.hh:48
TPEF::LineNumProcedure
Definition: LineNumProcedure.hh:53
TPEF::TPEFLineNumSectionWriter::writeInfo
virtual void writeInfo(BinaryStream &stream, const Section *sect) const
Definition: TPEFLineNumSectionWriter.cc:150
ReferenceKey.hh
TPEF::TPEFLineNumSectionWriter::type
virtual Section::SectionType type() const
Definition: TPEFLineNumSectionWriter.cc:82
TPEF::TPEFSectionWriter
Definition: TPEFSectionWriter.hh:49
TPEF::Section::elementCount
Word elementCount() const
TPEF
Definition: Assembler.hh:43