OpenASIP  2.0
SectionIndexReplacer.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 SectionIndexReplacer.cc
26  *
27  * Definition of SectionIndexReplacer class.
28  *
29  * @author Mikael Lepistö 2003 (tmlepist-no.spam-cs.tut.fi)
30  *
31  * @note rating: yellow
32  */
33 
34 #include "SectionIndexReplacer.hh"
35 #include "SafePointer.hh"
36 #include "BinaryStream.hh"
37 
38 namespace TPEF {
39 
40 using ReferenceManager::SafePointer;
41 using ReferenceManager::SectionIndexKey;
42 
43 /**
44  * Constructor.
45  */
47  const SafePointable* obj, Byte size) :
48  ValueReplacer(obj), fieldSize_(size) {
49 }
50 
51 /**
52  * Copy constructor.
53  */
55  const SectionIndexReplacer& replacer) :
56  ValueReplacer(replacer), fieldSize_(replacer.fieldSize_) {
57 }
58 
59 /**
60  * Destructor.
61  */
63 }
64 
65 /**
66  * Tries to do replacement in to the stream.
67  *
68  * @return True if replacement were done.
69  */
70 bool
72  try {
73  SectionIndexKey key =
75 
77  writeReplacement(key.index());
78  return true;
79 
80  } catch (KeyNotFound& e) {
82  return false;
83  }
84 }
85 
86 /**
87  * Creates dynamically allocated copy of replacer.
88  *
89  * @return Dynamically allocated copy of replacer.
90  */
93  return new SectionIndexReplacer(*this);
94 }
95 
96 /**
97  * Writes value with one, two or four bytes wide.
98  *
99  * @param value Value to be written.
100  */
101 void
103  switch (fieldSize_) {
104  case 1: {
105  stream().writeByte(value);
106  break;
107  }
108  case 2: {
109  stream().writeHalfWord(value);
110  break;
111  }
112  case 3: {
113  Byte firstByte = (value >> HALFWORD_BITWIDTH);
114  HalfWord lastBytes = (value << HALFWORD_BITWIDTH)
116 
117  // writing three bytes in big endian format
118  stream().writeHalfWord(lastBytes);
119  stream().writeByte(firstByte);
120  break;
121  }
122  case 4: {
123  stream().writeWord(value);
124  break;
125  }
126  default:
127  bool unsupportedFieldSizeOfReplacement = false;
128  assert(unsupportedFieldSizeOfReplacement);
129  }
130 }
131 
132 }
TPEF::SectionIndexReplacer::SectionIndexReplacer
SectionIndexReplacer(const SafePointable *obj, Byte fieldSize)
Definition: SectionIndexReplacer.cc:46
TPEF::BinaryStream::writeHalfWord
void writeHalfWord(HalfWord halfword)
Definition: BinaryStream.cc:336
TPEF::ReferenceManager::SectionIndexKey::index
SectionIndex index() const
TPEF::BinaryStream::writeWord
void writeWord(Word word)
Definition: BinaryStream.cc:368
SafePointer.hh
Byte
unsigned char Byte
Definition: BaseType.hh:116
TPEF::ReferenceManager::SectionIndexKey
Definition: ReferenceKey.hh:65
TPEF::SectionIndexReplacer::fieldSize_
const Byte fieldSize_
Field how many bytes of stream are replaced with section index.
Definition: SectionIndexReplacer.hh:65
assert
#define assert(condition)
Definition: Application.hh:86
TPEF::ValueReplacer::stream
static BinaryStream & stream()
Definition: ValueReplacer.cc:160
TPEF::SectionIndexReplacer::writeReplacement
void writeReplacement(Word value)
Definition: SectionIndexReplacer.cc:102
TPEF::BinaryStream::setWritePosition
void setWritePosition(unsigned int position)
Definition: BinaryStream.cc:689
TPEF::SectionIndexReplacer::tryToReplace
virtual bool tryToReplace()
Definition: SectionIndexReplacer.cc:71
TPEF::SectionIndexReplacer::~SectionIndexReplacer
virtual ~SectionIndexReplacer()
Definition: SectionIndexReplacer.cc:62
TPEF::BinaryStream::writeByte
void writeByte(Byte byte)
Definition: BinaryStream.cc:310
TPEF::SafePointable
Definition: SafePointable.hh:48
TPEF::ValueReplacer::streamPosition
unsigned int streamPosition() const
Definition: ValueReplacer.cc:180
SectionIndexReplacer.hh
TPEF::ReferenceManager::SafePointer::sectionIndexKeyFor
static SectionIndexKey sectionIndexKeyFor(const SafePointable *obj)
Definition: SafePointer.cc:366
BinaryStream.hh
TPEF::SectionIndexReplacer
Definition: SectionIndexReplacer.hh:48
TPEF::SectionIndexReplacer::clone
virtual ValueReplacer * clone()
Definition: SectionIndexReplacer.cc:92
KeyNotFound
Definition: Exception.hh:285
HALFWORD_BITWIDTH
const Byte HALFWORD_BITWIDTH
Definition: BaseType.hh:137
TPEF::ValueReplacer
Definition: ValueReplacer.hh:71
TPEF
Definition: Assembler.hh:43
TPEF::ValueReplacer::reference
const SafePointable * reference() const
Definition: ValueReplacer.cc:170