OpenASIP  2.0
TPEFASpaceSectionWriter.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 TPEFASpaceSectionWriter.cc
26  *
27  * Definition of TPEFASpaceSectionWriter class.
28  *
29  * @author Jussi Nykänen 2003 (nykanen-no.spam-cs.tut.fi)
30  * @author Mikael Lepistö 2003 (tmlepist-no.spam-cs.tut.fi)
31  *
32  * @note rating: yellow
33  */
34 
35 #include <list>
36 
38 #include "SafePointer.hh"
39 #include "ReferenceKey.hh"
40 #include "SectionElement.hh"
41 #include "ASpaceSection.hh"
42 #include "ASpaceElement.hh"
43 #include "SectionSizeReplacer.hh"
44 #include "SectionOffsetReplacer.hh"
45 #include "BinaryStream.hh"
46 
47 namespace TPEF {
48 
49 using std::list;
50 using ReferenceManager::SafePointer;
51 using ReferenceManager::SectionKey;
52 using ReferenceManager::FileOffsetKey;
53 using ReferenceManager::SectionIndexKey;
54 
55 const TPEFASpaceSectionWriter TPEFASpaceSectionWriter::instance_;
57 
58 /**
59  * Constructor.
60  *
61  * Registers itself to SectionWriter.
62  */
65 }
66 
67 /**
68  * Destructor.
69  */
71 }
72 
73 /**
74  * Returns the section type this writer can write.
75  *
76  * @return The section type writer can write.
77  */
80  return Section::ST_ADDRSP;
81 }
82 
83 /**
84  * Writes the data of the section in to stream.
85  *
86  * @param stream The stream to be written to.
87  * @param sect The section to be written.
88  */
89 void
91  BinaryStream& stream,
92  const Section* sect) const {
93 
94  FileOffset startOffset = stream.writePosition();
95 
96  // file offset to data of section
98  FileOffsetKey(startOffset), sect->element(0));
99 
100  // resolve identification code of currently written section
101  SectionKey sectionKey = SafePointer::sectionKeyFor(sect);
102 
103  SectionId sectionId = sectionKey.sectionId();
104 
105  // index zero is reserved for undefined element.
106  Byte index = 1;
107  bool undefASpaceWritten = false;
108 
109  for (unsigned int i = 0; i < sect->elementCount(); i++) {
110 
111  ASpaceElement* elem = dynamic_cast<ASpaceElement*>(sect->element(i));
112  assert(elem != NULL);
113 
114  // references to undefined address space will be written out as
115  // identification code zero
116  if (dynamic_cast<const ASpaceSection*>(sect)->isUndefined(elem)) {
117  assert(elem->MAU() == 0);
118  assert(elem->wordSize() == 0);
119  assert(elem->align() == 0);
120  undefASpaceWritten = true;
121  writeElement(stream, elem, sectionId, 0);
122  } else {
123  writeElement(stream, elem, sectionId, index);
124  }
125 
126  index++;
127  }
128 
129  assert(undefASpaceWritten);
130 
131  SectionSizeReplacer::setSize(sect, stream.writePosition() - startOffset);
132 }
133 
134 /**
135  * Writes address space element to stream.
136  *
137  * @param stream Stream where to write.
138  * @param elem Element to write.
139  * @param id Section identification code of address space section.
140  * @param index Identification code of address space element.
141  */
142 void
144  BinaryStream &stream,
145  const ASpaceElement* elem,
146  SectionId id,
147  Word index) const {
148 
149  // add index of element for index replacer
151  SectionIndexKey(id, index), elem);
152 
153  stream.writeByte(index);
154  stream.writeByte(elem->MAU());
155  stream.writeByte(elem->align());
156  stream.writeByte(elem->wordSize());
157 
158  SectionOffsetReplacer replacer(elem->name());
159  replacer.resolve();
160 }
161 
162 /**
163  * Returns the fixed size length of section elements.
164  *
165  * @return The fixed size length of section elements.
166  */
167 Word
169  return elementSize_;
170 }
171 
172 }
TPEF::SectionId
HalfWord SectionId
Type for storing binary file section ids.
Definition: TPEFBaseType.hh:43
SectionSizeReplacer.hh
ASpaceElement.hh
TPEF::TPEFASpaceSectionWriter::~TPEFASpaceSectionWriter
virtual ~TPEFASpaceSectionWriter()
Definition: TPEFASpaceSectionWriter.cc:70
SectionOffsetReplacer.hh
TPEFASpaceSectionWriter.hh
TPEF::BinaryStream
Definition: BinaryStream.hh:59
TPEF::TPEFASpaceSectionWriter::TPEFASpaceSectionWriter
TPEFASpaceSectionWriter()
Definition: TPEFASpaceSectionWriter.cc:63
TPEF::BinaryStream::writePosition
unsigned int writePosition()
Definition: BinaryStream.cc:592
SafePointer.hh
TPEF::TPEFASpaceSectionWriter::instance_
static const TPEFASpaceSectionWriter instance_
An unique instance of class.
Definition: TPEFASpaceSectionWriter.hh:73
TPEF::ASpaceElement::wordSize
Byte wordSize() const
TPEF::ASpaceElement::MAU
Byte MAU() const
TPEF::ReferenceManager::SectionKey::sectionId
SectionId sectionId() const
Byte
unsigned char Byte
Definition: BaseType.hh:116
TPEF::ReferenceManager::SectionIndexKey
Definition: ReferenceKey.hh:65
TPEF::TPEFASpaceSectionWriter::type
virtual Section::SectionType type() const
Definition: TPEFASpaceSectionWriter.cc:79
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
assert
#define assert(condition)
Definition: Application.hh:86
TPEF::Section::element
SectionElement * element(Word index) const
TPEF::TPEFASpaceSectionWriter::writeElement
void writeElement(BinaryStream &stream, const ASpaceElement *elem, SectionId id, Word index) const
Definition: TPEFASpaceSectionWriter.cc:143
TPEF::SectionOffsetReplacer
Definition: SectionOffsetReplacer.hh:47
TPEF::SectionWriter::registerSectionWriter
static void registerSectionWriter(const SectionWriter *sWriter)
Definition: SectionWriter.cc:148
TPEF::ASpaceElement
Definition: ASpaceElement.hh:48
TPEF::TPEFASpaceSectionWriter::elementSize_
static const Word elementSize_
The fixed size address space element.
Definition: TPEFASpaceSectionWriter.hh:75
TPEF::ReferenceManager::SafePointer::sectionKeyFor
static SectionKey sectionKeyFor(const SafePointable *obj)
Definition: SafePointer.cc:408
TPEF::TPEFASpaceSectionWriter::actualWriteData
virtual void actualWriteData(BinaryStream &stream, const Section *section) const
Definition: TPEFASpaceSectionWriter.cc:90
TPEF::FileOffset
Word FileOffset
Type for storing absolute file offsets.
Definition: TPEFBaseType.hh:52
TPEF::ASpaceElement::name
Chunk * name() const
TPEF::BinaryStream::writeByte
void writeByte(Byte byte)
Definition: BinaryStream.cc:310
ASpaceSection.hh
TPEF::ReferenceManager::SectionKey
Definition: ReferenceKey.hh:145
TPEF::ReferenceManager::FileOffsetKey
Definition: ReferenceKey.hh:121
SectionElement.hh
TPEF::SectionSizeReplacer::setSize
static void setSize(const SafePointable *obj, Word size)
Definition: SectionSizeReplacer.cc:100
TPEF::TPEFASpaceSectionWriter::elementSize
virtual Word elementSize(const Section *section) const
Definition: TPEFASpaceSectionWriter.cc:168
BinaryStream.hh
TPEF::Section::SectionType
SectionType
Definition: Section.hh:69
ReferenceKey.hh
TPEF::ASpaceSection
Definition: ASpaceSection.hh:44
TPEF::TPEFSectionWriter
Definition: TPEFSectionWriter.hh:49
TPEF::ASpaceElement::align
Byte align() const
TPEF::Section::ST_ADDRSP
@ ST_ADDRSP
Address space section.
Definition: Section.hh:77
TPEF::Section::elementCount
Word elementCount() const
TPEF
Definition: Assembler.hh:43