OpenASIP  2.0
ProgramWriter.hh
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 ProgramWriter.hh
26  *
27  * Declaration of ProgramWriter class.
28  *
29  * @author Mikael Lepistö 2005 (tmlepist-no.spam-cs.tut.fi)
30  * @note rating: yellow
31  */
32 
33 #ifndef TTA_PROGRAM_WRITER_HH
34 #define TTA_PROGRAM_WRITER_HH
35 
36 #include <map>
37 
38 #include "BaseType.hh"
39 #include "MoveElement.hh"
40 #include "Binary.hh"
41 #include "Address.hh"
42 
43 namespace TPEF {
44  class Binary;
45  class CodeSection;
46  class RelocSection;
47  class SymbolSection;
48  class ResourceSection;
49  class ResourceElement;
50  class Section;
51  class ASpaceElement;
52  class SectionElement;
53 }
54 
55 namespace TTAMachine {
56  class Machine;
57  class Bus;
58  class RegisterFile;
59  class ImmediateUnit;
60  class FunctionUnit;
61  class Port;
62  class HWOperation;
63  class MachinePart;
64  class AddressSpace;
65 }
66 
67 namespace TTAProgram {
68 
69 class Program;
70 class Terminal;
71 
72 /**
73  * Returns requested Machine parts as a TPEF resources.
74  *
75  * Also adds new resources to resource section if resource is not
76  * already there.
77  *
78  * NOTE: If resource section is modified externally, cache should be cleared.
79  *
80  */
82 public:
84  TTAMachine::Machine& mach,
85  TPEF::ResourceSection& resources);
86 
87  void clearCache();
88 
92  const TTAMachine::HWOperation& oper,
93  int operandIndex);
97  const TTAMachine::ImmediateUnit& immUnit);
98 
99 private:
100  void initCache();
101 
102  /// The model of the target processor architecture.
104  /// The TPEF input section that contains all machine resource entries.
106 
107  // Key type for caching ResourceElements for machine components.
108  // For example (HWOperation, index) pair
109  typedef std::pair<const TTAMachine::MachinePart*, int> CacheKey;
110  /// Mapping from parts of the target processor model to TPEF machine
111  /// resource entries.
112  typedef std::map<CacheKey, TPEF::ResourceElement*> ResourceCache;
113 
114  /// Aggregate of all mappings between machine resource entries and
115  /// target processor parts (machine parts).
117 
118  /// Last TPEF bus id that was generated.
119  HalfWord lastBusId_;
120  /// Last TPEF unit port id that was generated.
121  HalfWord lastUnitPortId_;
122  /// Last TPEF function unit id that was generated.
124  /// Last TPEF register file id that was generated.
126  /// Last TPEF immediate unit id that was generated.
128 };
129 
130 /**
131  * Writes new TPEF out of POM and original TPEF.
132  *
133  * Writer creates creates whole new code section, code relocation section,
134  * resource section and symbol section for code symbols. Data sections,
135  * uninitialized data sections and data to data relocations are also copied
136  * from original TPEF.
137  *
138  * Client of POM has to write data to code relocations and all sections
139  * that client wants to preserve from original TPEF.
140  */
142 public:
143  ProgramWriter(const Program& prog);
144 
145  TPEF::Binary* createBinary() const;
146 
147  static TPEF::Binary* createBinary(const Program& prog);
148 
149 private:
150  /**
151  * TPEF resource id:s for getting corresponding resource elements.
152  */
153  struct ResourceID {
154  TPEF::MoveElement::FieldType type; ///< RF, UNIT or IMM.
155  HalfWord unit; ///< Unit id.
156  HalfWord index; ///< Operand or register file index.
157  };
158 
159  /**
160  * Info about relocation of immediate or data chunk.
161  */
162  struct RelocInfo {
164  TPEF::Section* aSrcSect, TPEF::SectionElement* aSrcElem,
165  const Address& aDstAddress, int bitWidth) :
166  srcSect(aSrcSect), srcElem(aSrcElem),
167  destination(aDstAddress), bits(bitWidth) {
168  }
169 
170  /// Location section.
172  /// Location element.
174  /// Destination address.
176  /// Relocation width.
177  int bits;
178  };
179 
180  void createCodeSection(
181  TPEF::CodeSection* code,
182  TPEFResourceUpdater& updater) const;
183 
184  void createDataSections(TPEF::Binary* bin, bool littleEndian) const;
185 
186  void createRelocSections(TPEF::Binary* bin) const;
187 
188  TPEF::Section& findSection(TPEF::Binary& bin, Address address) const;
189 
191  const Terminal& term,
192  TPEFResourceUpdater& updater) const;
193 
195  TPEF::ResourceSection& resources) const;
196 
198  const TTAMachine::AddressSpace& addressSpace,
199  TPEF::Binary& bin) const;
200 
201 
202  /// Program that is written to TPEF.
203  const Program& prog_;
204  /// Created TPEF binary.
205  // const TPEF::Binary& tpef_;
206 
207  /// Map that contains created address spaces
208  mutable std::map<const TTAMachine::AddressSpace*,
210 
211  mutable std::vector<RelocInfo> relocInfos_;
212 
213  /// Default widt that is used as address width of relocations of immediate
214  /// elements. This should be fixed by checking width from bus or template.
215  static const HalfWord IMMEDIATE_ADDRESS_WIDTH;
216 
217  /// Maximum width for short immediates
218  static const int MAX_SIMM_WIDTH;
219 };
220 
221 }
222 
223 #endif
TTAProgram::ProgramWriter::aSpaceMap_
std::map< const TTAMachine::AddressSpace *, TPEF::ASpaceElement * > aSpaceMap_
Created TPEF binary.
Definition: ProgramWriter.hh:209
TTAProgram
Definition: Estimator.hh:65
TTAProgram::TPEFResourceUpdater::mach_
TTAMachine::Machine & mach_
The model of the target processor architecture.
Definition: ProgramWriter.hh:103
TTAProgram::Program
Definition: Program.hh:63
TPEF::ResourceSection
Definition: ResourceSection.hh:47
BaseType.hh
TTAProgram::Address
Definition: Address.hh:51
TTAProgram::TPEFResourceUpdater::lastRegisterFileId_
HalfWord lastRegisterFileId_
Last TPEF register file id that was generated.
Definition: ProgramWriter.hh:125
TTAProgram::TPEFResourceUpdater::functionUnit
TPEF::ResourceElement & functionUnit(const TTAMachine::FunctionUnit &fu)
Definition: ProgramWriter.cc:512
TTAMachine::HWOperation
Definition: HWOperation.hh:52
TTAMachine::AddressSpace
Definition: AddressSpace.hh:51
TTAProgram::TPEFResourceUpdater
Definition: ProgramWriter.hh:81
TTAProgram::ProgramWriter::createDataSections
void createDataSections(TPEF::Binary *bin, bool littleEndian) const
Definition: ProgramWriter.cc:1403
TTAProgram::TPEFResourceUpdater::CacheKey
std::pair< const TTAMachine::MachinePart *, int > CacheKey
Definition: ProgramWriter.hh:109
TTAProgram::ProgramWriter::createRelocSections
void createRelocSections(TPEF::Binary *bin) const
Definition: ProgramWriter.cc:1578
TTAProgram::ProgramWriter::resolveFileType
TPEF::Binary::FileType resolveFileType(TPEF::ResourceSection &resources) const
Definition: ProgramWriter.cc:1711
TTAProgram::TPEFResourceUpdater::immediateUnit
TPEF::ResourceElement & immediateUnit(const TTAMachine::ImmediateUnit &immUnit)
Definition: ProgramWriter.cc:646
TTAProgram::TPEFResourceUpdater::operand
TPEF::ResourceElement & operand(const TTAMachine::HWOperation &oper, int operandIndex)
Definition: ProgramWriter.cc:464
TPEF::Binary
Definition: Binary.hh:49
TTAMachine::Bus
Definition: Bus.hh:53
TTAProgram::ProgramWriter
Definition: ProgramWriter.hh:141
TTAProgram::ProgramWriter::createCodeSection
void createCodeSection(TPEF::CodeSection *code, TPEFResourceUpdater &updater) const
Definition: ProgramWriter.cc:954
TTAProgram::ProgramWriter::IMMEDIATE_ADDRESS_WIDTH
static const HalfWord IMMEDIATE_ADDRESS_WIDTH
Default widt that is used as address width of relocations of immediate elements. This should be fixed...
Definition: ProgramWriter.hh:215
TPEF::MoveElement::FieldType
FieldType
Definition: MoveElement.hh:52
TTAProgram::ProgramWriter::terminalResource
ResourceID terminalResource(const Terminal &term, TPEFResourceUpdater &updater) const
Definition: ProgramWriter.cc:1310
TPEF::ResourceElement
Definition: ResourceElement.hh:47
TTAProgram::ProgramWriter::ResourceID::index
HalfWord index
Operand or register file index.
Definition: ProgramWriter.hh:156
TTAProgram::TPEFResourceUpdater::TPEFResourceUpdater
TPEFResourceUpdater(TTAMachine::Machine &mach, TPEF::ResourceSection &resources)
Definition: ProgramWriter.cc:151
TTAProgram::TPEFResourceUpdater::ResourceCache
std::map< CacheKey, TPEF::ResourceElement * > ResourceCache
Mapping from parts of the target processor model to TPEF machine resource entries.
Definition: ProgramWriter.hh:112
TTAProgram::TPEFResourceUpdater::cache_
ResourceCache cache_
Aggregate of all mappings between machine resource entries and target processor parts (machine parts)...
Definition: ProgramWriter.hh:116
TTAProgram::ProgramWriter::ResourceID::type
TPEF::MoveElement::FieldType type
RF, UNIT or IMM.
Definition: ProgramWriter.hh:154
TPEF::Section
Definition: Section.hh:64
TTAProgram::TPEFResourceUpdater::lastFunctionUnitId_
HalfWord lastFunctionUnitId_
Last TPEF function unit id that was generated.
Definition: ProgramWriter.hh:123
TTAMachine::FunctionUnit
Definition: FunctionUnit.hh:55
TTAProgram::TPEFResourceUpdater::resources_
TPEF::ResourceSection & resources_
The TPEF input section that contains all machine resource entries.
Definition: ProgramWriter.hh:105
TTAProgram::ProgramWriter::findSection
TPEF::Section & findSection(TPEF::Binary &bin, Address address) const
Definition: ProgramWriter.cc:1660
TPEF::ASpaceElement
Definition: ASpaceElement.hh:48
TTAProgram::TPEFResourceUpdater::lastImmediateUnitId_
HalfWord lastImmediateUnitId_
Last TPEF immediate unit id that was generated.
Definition: ProgramWriter.hh:127
TTAMachine::Port
Definition: Port.hh:54
TTAProgram::ProgramWriter::prog_
const Program & prog_
Program that is written to TPEF.
Definition: ProgramWriter.hh:203
TPEF::Binary::FileType
FileType
Definition: Binary.hh:54
TTAProgram::ProgramWriter::MAX_SIMM_WIDTH
static const int MAX_SIMM_WIDTH
Maximum width for short immediates.
Definition: ProgramWriter.hh:218
TPEF::SectionElement
Definition: SectionElement.hh:44
TTAProgram::TPEFResourceUpdater::bus
TPEF::ResourceElement & bus(const TTAMachine::Bus &bus)
Definition: ProgramWriter.cc:317
TTAProgram::ProgramWriter::createASpaceElement
TPEF::ASpaceElement & createASpaceElement(const TTAMachine::AddressSpace &addressSpace, TPEF::Binary &bin) const
Definition: ProgramWriter.cc:1544
TTAProgram::ProgramWriter::RelocInfo::bits
int bits
Relocation width.
Definition: ProgramWriter.hh:177
TTAProgram::ProgramWriter::RelocInfo::srcElem
TPEF::SectionElement * srcElem
Location element.
Definition: ProgramWriter.hh:173
TTAProgram::TPEFResourceUpdater::registerFile
TPEF::ResourceElement & registerFile(const TTAMachine::RegisterFile &rf)
Definition: ProgramWriter.cc:586
TTAProgram::TPEFResourceUpdater::initCache
void initCache()
Definition: ProgramWriter.cc:169
TPEF::CodeSection
Definition: CodeSection.hh:44
TTAProgram::TPEFResourceUpdater::functionUnitPort
TPEF::ResourceElement & functionUnitPort(const TTAMachine::Port &port)
Definition: ProgramWriter.cc:373
TTAProgram::ProgramWriter::ResourceID::unit
HalfWord unit
Unit id.
Definition: ProgramWriter.hh:155
TTAProgram::ProgramWriter::relocInfos_
std::vector< RelocInfo > relocInfos_
Definition: ProgramWriter.hh:211
TTAProgram::ProgramWriter::RelocInfo::destination
Address destination
Destination address.
Definition: ProgramWriter.hh:175
Address.hh
TTAProgram::ProgramWriter::ResourceID
Definition: ProgramWriter.hh:153
TTAProgram::ProgramWriter::RelocInfo::srcSect
TPEF::Section * srcSect
Location section.
Definition: ProgramWriter.hh:171
TTAProgram::TPEFResourceUpdater::lastBusId_
HalfWord lastBusId_
Last TPEF bus id that was generated.
Definition: ProgramWriter.hh:119
TTAProgram::TPEFResourceUpdater::clearCache
void clearCache()
Definition: ProgramWriter.cc:296
TTAProgram::Terminal
Definition: Terminal.hh:60
TTAMachine::RegisterFile
Definition: RegisterFile.hh:47
TTAProgram::ProgramWriter::RelocInfo
Definition: ProgramWriter.hh:162
TTAProgram::ProgramWriter::RelocInfo::RelocInfo
RelocInfo(TPEF::Section *aSrcSect, TPEF::SectionElement *aSrcElem, const Address &aDstAddress, int bitWidth)
Definition: ProgramWriter.hh:163
TTAMachine
Definition: Assembler.hh:48
TTAProgram::TPEFResourceUpdater::lastUnitPortId_
HalfWord lastUnitPortId_
Last TPEF unit port id that was generated.
Definition: ProgramWriter.hh:121
TTAProgram::ProgramWriter::createBinary
TPEF::Binary * createBinary() const
Definition: ProgramWriter.cc:700
MoveElement.hh
TPEF
Definition: Assembler.hh:43
TTAMachine::Machine
Definition: Machine.hh:73
Binary.hh
TTAProgram::ProgramWriter::ProgramWriter
ProgramWriter(const Program &prog)
Definition: ProgramWriter.cc:688
TTAMachine::ImmediateUnit
Definition: ImmediateUnit.hh:50