OpenASIP  2.0
TPEFResourceSectionWriter.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 TPEFResourceSectionWriter.cc
26  *
27  * Definition of TPEFResourceSectionWriter class.
28  *
29  * @author Mikael Lepistö 2004 (tmlepist-no.spam-cs.tut.fi)
30  *
31  * @note rating: yellow
32  */
33 
34 #include <list>
35 #include <set>
36 
38 #include "SafePointer.hh"
39 #include "ReferenceKey.hh"
40 #include "SectionElement.hh"
41 #include "ResourceElement.hh"
42 #include "SectionSizeReplacer.hh"
43 #include "SectionOffsetReplacer.hh"
44 #include "BinaryStream.hh"
45 
46 namespace TPEF {
47 
48 using std::list;
49 using ReferenceManager::SafePointer;
50 using ReferenceManager::SectionKey;
51 using ReferenceManager::FileOffsetKey;
52 using ReferenceManager::SectionIndexKey;
53 
54 const TPEFResourceSectionWriter TPEFResourceSectionWriter::instance_;
56 
57 /**
58  * Constructor.
59  *
60  * Registers itself to SectionWriter.
61  */
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_MR;
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 body of section
98  FileOffsetKey(startOffset), sect->element(0));
99 
100  // set for checking multiple items
101  std::multiset<
102  std::pair<HalfWord, ResourceElement::ResourceType> > checkMultiple;
103 
104  for (Word i = 0; i < sect->elementCount(); i++) {
105  ResourceElement* elem =
106  dynamic_cast<ResourceElement*>(sect->element(i));
107 
108  assert(elem != NULL);
109 
110  // identification codes of resources must be unique among all
111  // resources of the same type
112  std::pair<HalfWord, ResourceElement::ResourceType>
113  pairToFind(elem->id(), elem->type());
114 
115  // TODO: check that OP, SR and PORT share the same id space and don't
116  // collide
117 
118  if (checkMultiple.find(pairToFind) != checkMultiple.end()) {
119  bool multipleResourcesWithSameIdAndType = false;
120  assert(multipleResourcesWithSameIdAndType);
121  } else {
122  checkMultiple.insert(pairToFind);
123  }
124 
125  stream.writeHalfWord(elem->id());
126  stream.writeByte(static_cast<Byte>(elem->type()));
127 
128  assert(elem->name() != NULL);
129 
130  SectionOffsetReplacer replacer(elem->name());
131  replacer.resolve();
132 
133  stream.writeWord(elem->info());
134  }
135  SectionSizeReplacer::setSize(sect, stream.writePosition() - startOffset);
136 }
137 
138 /**
139  * Returns the fixed size length of section elements.
140  *
141  * @return The fixed size length of section elements.
142  */
143 Word
145  return elementSize_;
146 }
147 
148 }
TPEF::BinaryStream::writeHalfWord
void writeHalfWord(HalfWord halfword)
Definition: BinaryStream.cc:336
SectionSizeReplacer.hh
SectionOffsetReplacer.hh
TPEFResourceSectionWriter.hh
TPEF::TPEFResourceSectionWriter::TPEFResourceSectionWriter
TPEFResourceSectionWriter()
Definition: TPEFResourceSectionWriter.cc:62
TPEF::ResourceElement::id
HalfWord id() const
TPEF::BinaryStream::writeWord
void writeWord(Word word)
Definition: BinaryStream.cc:368
TPEF::ResourceElement::type
ResourceType type() const
TPEF::BinaryStream
Definition: BinaryStream.hh:59
TPEF::ResourceElement
Definition: ResourceElement.hh:47
TPEF::BinaryStream::writePosition
unsigned int writePosition()
Definition: BinaryStream.cc:592
SafePointer.hh
Byte
unsigned char Byte
Definition: BaseType.hh:116
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::ResourceElement::name
Chunk * name() const
assert
#define assert(condition)
Definition: Application.hh:86
TPEF::Section::element
SectionElement * element(Word index) const
TPEF::SectionOffsetReplacer
Definition: SectionOffsetReplacer.hh:47
TPEF::SectionWriter::registerSectionWriter
static void registerSectionWriter(const SectionWriter *sWriter)
Definition: SectionWriter.cc:148
ResourceElement.hh
TPEF::FileOffset
Word FileOffset
Type for storing absolute file offsets.
Definition: TPEFBaseType.hh:52
TPEF::ResourceElement::info
Word info() const
TPEF::TPEFResourceSectionWriter::actualWriteData
virtual void actualWriteData(BinaryStream &stream, const Section *section) const
Definition: TPEFResourceSectionWriter.cc:90
TPEF::BinaryStream::writeByte
void writeByte(Byte byte)
Definition: BinaryStream.cc:310
TPEF::TPEFResourceSectionWriter::instance_
static const TPEFResourceSectionWriter instance_
An unique instance of class.
Definition: TPEFResourceSectionWriter.hh:67
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::TPEFResourceSectionWriter::~TPEFResourceSectionWriter
virtual ~TPEFResourceSectionWriter()
Definition: TPEFResourceSectionWriter.cc:70
BinaryStream.hh
TPEF::Section::SectionType
SectionType
Definition: Section.hh:69
TPEF::TPEFResourceSectionWriter::type
virtual Section::SectionType type() const
Definition: TPEFResourceSectionWriter.cc:79
TPEF::TPEFResourceSectionWriter::elementSize_
static const Word elementSize_
The fixed size element.
Definition: TPEFResourceSectionWriter.hh:69
TPEF::Section::ST_MR
@ ST_MR
Machine resources section.
Definition: Section.hh:78
ReferenceKey.hh
TPEF::TPEFSectionWriter
Definition: TPEFSectionWriter.hh:49
TPEF::Section::elementCount
Word elementCount() const
TPEF
Definition: Assembler.hh:43
TPEF::TPEFResourceSectionWriter::elementSize
virtual Word elementSize(const Section *section) const
Definition: TPEFResourceSectionWriter.cc:144