OpenASIP 2.2
Loading...
Searching...
No Matches
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"
45#include "BinaryStream.hh"
46
47namespace TPEF {
48
49using std::list;
50using ReferenceManager::SafePointer;
51using ReferenceManager::SectionKey;
52using ReferenceManager::FileOffsetKey;
53using ReferenceManager::SectionIndexKey;
54
55const TPEFASpaceSectionWriter TPEFASpaceSectionWriter::instance_;
57
58/**
59 * Constructor.
60 *
61 * Registers itself to SectionWriter.
62 */
66
67/**
68 * Destructor.
69 */
72
73/**
74 * Returns the section type this writer can write.
75 *
76 * @return The section type writer can write.
77 */
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 */
89void
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 */
142void
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 */
167Word
169 return elementSize_;
170}
171
172}
#define assert(condition)
unsigned char Byte
Definition BaseType.hh:116
Byte wordSize() const
Chunk * name() const
Byte align() const
Byte MAU() const
void writeByte(Byte byte)
unsigned int writePosition()
static void addObjectReference(SectionIndexKey key, const SafePointable *obj)
static SectionKey sectionKeyFor(const SafePointable *obj)
static void setSize(const SafePointable *obj, Word size)
static void registerSectionWriter(const SectionWriter *sWriter)
@ ST_ADDRSP
Address space section.
Definition Section.hh:77
SectionElement * element(Word index) const
Word elementCount() const
virtual Word elementSize(const Section *section) const
static const Word elementSize_
The fixed size address space element.
static const TPEFASpaceSectionWriter instance_
An unique instance of class.
void writeElement(BinaryStream &stream, const ASpaceElement *elem, SectionId id, Word index) const
virtual void actualWriteData(BinaryStream &stream, const Section *section) const
virtual Section::SectionType type() const
HalfWord SectionId
Type for storing binary file section ids.
Word FileOffset
Type for storing absolute file offsets.