OpenASIP  2.0
TPEFProgramFactory.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 TPEFProgramFactory.hh
26  *
27  * Declaration of TPEFProgramFactory class.
28  *
29  * @author Mikael Lepistö 2005 (tmlepist-no.spam-cs.tut.fi)
30  * @author Pekka Jääskeläinen 2006 (pjaaskel-no.spam-cs.tut.fi)
31  * @note rating: yellow
32  */
33 
34 #ifndef TTA_TPEF_PROGRAM_FACTORY_HH
35 #define TTA_TPEF_PROGRAM_FACTORY_HH
36 
37 #include <memory>
38 #include <map>
39 #include <list>
40 #include <memory>
41 
42 #include "Binary.hh"
43 #include "MoveElement.hh"
44 #include "ResourceElement.hh"
45 #include "ResourceSection.hh"
46 #include "Socket.hh"
47 #include "TPEFTools.hh"
48 #include "ASpaceElement.hh"
49 
50 namespace TTAMachine {
51  class Machine;
52  class FunctionUnit;
53  class AddressSpace;
54  class Socket;
55  class RegisterFile;
56  class Guard;
57  class ImmediateUnit;
58  class InstructionTemplate;
59  class Port;
60  class Bus;
61  class Unit;
62 }
63 
64 namespace TPEF {
65  class ImmediateElement;
66 }
67 
68 class SimValue;
69 class UniversalMachine;
70 
71 namespace TTAProgram {
72 
73 class Move;
74 class Immediate;
75 class Terminal;
76 class Program;
77 class Instruction;
78 
79 /**
80  * Creates a new TTA program out of a description of a binary representation
81  * of the program and a model of the target architecture.
82  *
83  * This class builds only one program, even if the input binary contains many
84  * programs (the original TPEF file has multiple code sections with different
85  * address spaces).
86  */
88 public:
90  const TPEF::Binary& aBinary,
91  const TTAMachine::Machine& aMachine);
92 
94  const TPEF::Binary &aBinary,
95  const TTAMachine::Machine& aMachine,
96  UniversalMachine* aUniversalMachine);
97 
99  const TPEF::Binary &aBinary, UniversalMachine* aMachine);
100 
101  virtual ~TPEFProgramFactory();
102 
103  Program* build();
104 
105 protected:
106  typedef std::map<HalfWord, SimValue*> InlineValues;
107  typedef std::pair<Word, Word> ImmediateKey;
108  typedef std::map<ImmediateKey, TPEF::ImmediateElement*> ImmediateMap;
109  typedef std::vector<TPEF::MoveElement*> MoveVector;
110  typedef std::vector<TPEF::ImmediateElement*> ImmediateVector;
111 
113  const TPEF::ResourceSection &resources,
114  MoveVector& moveElements,
115  ImmediateVector& longImmediates,
116  ImmediateMap& immElements) const;
117 
118 
119  void addProcedures(
120  Program &program,
121  const TTAMachine::AddressSpace &programASpace) const;
122 
124  const TPEF::ResourceSection &resources,
125  const TTAMachine::Bus *aBus,
128  HalfWord unitId,
129  HalfWord index,
130  const ImmediateMap *immediateMap = NULL) const;
131 
133  const TPEF::ResourceSection &resources,
134  HalfWord busId) const;
135 
137  const TPEF::ResourceSection &resources,
138  HalfWord rfId) const;
139 
141  const TPEF::ResourceSection &resources,
142  Byte immUnitId) const;
143 
145  const TPEF::ResourceSection &resources,
146  HalfWord unitId, std::string tpefOpName = "") const;
147 
149  const TTAMachine::Bus &bus,
150  const TTAMachine::Unit &portParent,
151  std::string tpefOpName = "",
152  int tpefOpIndex = 0) const;
153 
155  const TPEF::ASpaceElement *aSpace) const;
156 
158  const TPEF::ResourceSection &resources,
159  TTAMachine::Bus &bus,
161  HalfWord unitId,
162  HalfWord index,
163  bool isInverted) const;
164 
166  const TPEF::ResourceSection &resources,
167  ImmediateVector& longImmediates,
168  MoveVector& moves) const;
169 
171 
172  void createLabels(Program &prog);
173 
174  void createDataRelocs(Program &prog);
175 
176  void createDataMemories(Program &prog);
177 
178  bool isFunctionStart(
179  const TPEF::InstructionElement &instructionElement) const;
180 
181  std::string functionName(
182  const TPEF::InstructionElement &instructionElement) const;
183 
184  std::string stringOfChunk(
185  const TPEF::Chunk *chunk, const TPEF::Section *chunkOwner) const;
186 
187 
188 private:
189 
190  /// Cache key for resources that are accessed from MOM(s)
191  class CacheKey {
192  public:
194  const TTAMachine::Bus &aBus,
197  HalfWord anUnitId, HalfWord anIndex) :
198  bus_(aBus), direction_(aDirection), type_(aType),
199  unitId_(anUnitId), index_(anIndex) { }
200 
201  bool operator<(const CacheKey &keyToCompare) const {
202 
203  if (direction_ < keyToCompare.direction_) return true;
204  else if (direction_ > keyToCompare.direction_) return false;
205 
206  if (type_ < keyToCompare.type_) return true;
207  else if (type_ > keyToCompare.type_) return false;
208 
209  if (unitId_ < keyToCompare.unitId_ ) return true;
210  else if (unitId_ > keyToCompare.unitId_ ) return false;
211 
212  if (index_ < keyToCompare.index_ ) return true;
213  else if (index_ > keyToCompare.index_) return false;
214 
215  if (&bus_ < &(keyToCompare.bus_)) return true;
216 
217  return false;
218  }
219 
220  private:
221  /// Bus that was used for transport.
223  /// Direction if the port was read or written.
225  /// Type of the accessed unit.
227  /// unit id of the corresponding TPEF resource element.
228  HalfWord unitId_;
229  /// Index of the corresponding TPEF resource element.
230  HalfWord index_;
231  };
232 
233  /**
234  * Helper struct for resolving socket allocations
235  * for register file access.
236  *
237  * This could be removed, if register file port storing for register
238  * references is implemented in TPEF.
239  */
241  SocketAllocation(std::shared_ptr<Move> m, unsigned int anIndex) :
242  index(anIndex) ,move(m), src(0), dst(0) { };
243 
244  unsigned int index;
245  std::shared_ptr<Move> move;
246  std::vector<TTAMachine::Socket*> srcSocks;
247  std::vector<TTAMachine::Socket*> dstSocks;
248  unsigned int src;
249  unsigned int dst;
250  };
251 
252  void clearResourceAllocations() const;
253 
254  bool canSourceBeAssigned(
255  SocketAllocation& alloc,
256  std::map<TTAMachine::Socket*,
257  std::vector<SocketAllocation*> >& fixedSockets) const;
258 
260  SocketAllocation& alloc,
261  std::map<TTAMachine::Socket*,
262  std::vector<SocketAllocation*> >& fixedSockets) const;
263 
265  std::vector<SocketAllocation>& allocs) const;
266 
267  Terminal* getFromCache(const CacheKey &key) const;
268  void addToCache(const CacheKey &key, Terminal *cachedTerm) const;
269  void clearCache() const;
270 
271  /// Binary that is used for creating program.
273  /// Target machine of program.
275  /// Universal machine of program.
277 
278  /// TPEFTools object for helper functions.
280  /// Instruction address space of machine.
282  /// Instruction address space element of TPEF.
284 
285  /// Stores information of start points of procedures that were found.
286  std::map<const TPEF::InstructionElement*,
288 
289  /// Moves whose source terminals are addresses referring to instructions.
290  mutable std::list<std::shared_ptr<Move> > instructionImmediates_;
291 
292  /// Long immediates whose value terminals refers to instructions.
293  mutable std::list<std::shared_ptr<Immediate> > longInstructionImmediates_;
294 
295  /// Cache map of terminals that are returned by different search
296  /// parameters.
297  mutable std::map<const CacheKey, Terminal *> cache_;
298 
299  /// Busses that are already allocated by moves of current instruction.
300  mutable std::set<HalfWord> allocatedBusses_;
301 
302  /// Program instruction by TPEF instruction element.
303  mutable std::map<TPEF::InstructionElement*, Instruction*> instructionMap_;
304 
305 };
306 
307 }
308 
309 #endif
TTAMachine::Guard
Definition: Guard.hh:55
TTAProgram::TPEFProgramFactory::findPort
TTAMachine::Port & findPort(const TTAMachine::Bus &bus, const TTAMachine::Unit &portParent, std::string tpefOpName="", int tpefOpIndex=0) const
Definition: TPEFProgramFactory.cc:1237
ASpaceElement.hh
TTAProgram::TPEFProgramFactory::SocketAllocation::index
unsigned int index
Definition: TPEFProgramFactory.hh:242
TTAProgram
Definition: Estimator.hh:65
TTAProgram::TPEFProgramFactory::ImmediateKey
std::pair< Word, Word > ImmediateKey
Definition: TPEFProgramFactory.hh:107
TTAProgram::Program
Definition: Program.hh:63
TTAProgram::TPEFProgramFactory::canDestinationBeAssigned
bool canDestinationBeAssigned(SocketAllocation &alloc, std::map< TTAMachine::Socket *, std::vector< SocketAllocation * > > &fixedSockets) const
Definition: TPEFProgramFactory.cc:1667
TPEF::ResourceSection
Definition: ResourceSection.hh:47
TTAProgram::TPEFProgramFactory::CacheKey
Cache key for resources that are accessed from MOM(s)
Definition: TPEFProgramFactory.hh:191
TTAProgram::TPEFProgramFactory::findInstrTemplate
TTAMachine::InstructionTemplate & findInstrTemplate(const TPEF::ResourceSection &resources, ImmediateVector &longImmediates, MoveVector &moves) const
Definition: TPEFProgramFactory.cc:1502
TTAProgram::TPEFProgramFactory::adfInstrASpace_
TTAMachine::AddressSpace * adfInstrASpace_
Instruction address space of machine.
Definition: TPEFProgramFactory.hh:281
TTAProgram::TPEFProgramFactory::allocatedBusses_
std::set< HalfWord > allocatedBusses_
Busses that are already allocated by moves of current instruction.
Definition: TPEFProgramFactory.hh:300
TTAMachine::AddressSpace
Definition: AddressSpace.hh:51
TPEF::InstructionElement
Definition: InstructionElement.hh:77
TTAProgram::TPEFProgramFactory::instructionMap_
std::map< TPEF::InstructionElement *, Instruction * > instructionMap_
Program instruction by TPEF instruction element.
Definition: TPEFProgramFactory.hh:303
TPEF::Binary
Definition: Binary.hh:49
TTAProgram::Instruction
Definition: Instruction.hh:57
TTAProgram::TPEFProgramFactory::longInstructionImmediates_
std::list< std::shared_ptr< Immediate > > longInstructionImmediates_
Long immediates whose value terminals refers to instructions.
Definition: TPEFProgramFactory.hh:293
TTAMachine::Bus
Definition: Bus.hh:53
TTAProgram::TPEFProgramFactory::~TPEFProgramFactory
virtual ~TPEFProgramFactory()
Definition: TPEFProgramFactory.cc:166
TPEF::MoveElement::FieldType
FieldType
Definition: MoveElement.hh:52
TTAProgram::TPEFProgramFactory::createLabels
void createLabels(Program &prog)
Definition: TPEFProgramFactory.cc:2036
TTAProgram::TPEFProgramFactory::TPEFProgramFactory
TPEFProgramFactory(const TPEF::Binary &aBinary, const TTAMachine::Machine &aMachine)
Definition: TPEFProgramFactory.cc:137
TTAProgram::TPEFProgramFactory::CacheKey::unitId_
HalfWord unitId_
unit id of the corresponding TPEF resource element.
Definition: TPEFProgramFactory.hh:228
TTAProgram::TPEFProgramFactory::CacheKey::operator<
bool operator<(const CacheKey &keyToCompare) const
Definition: TPEFProgramFactory.hh:201
TTAProgram::TPEFProgramFactory::SocketAllocation
Definition: TPEFProgramFactory.hh:240
TTAProgram::TPEFProgramFactory::createDataRelocs
void createDataRelocs(Program &prog)
TTAMachine::Socket::Direction
Direction
Definition: Socket.hh:58
TTAProgram::TPEFProgramFactory::addToCache
void addToCache(const CacheKey &key, Terminal *cachedTerm) const
Definition: TPEFProgramFactory.cc:1968
Byte
unsigned char Byte
Definition: BaseType.hh:116
TTAProgram::TPEFProgramFactory::createDataMemories
void createDataMemories(Program &prog)
Definition: TPEFProgramFactory.cc:2176
ResourceSection.hh
Socket.hh
TPEF::Section
Definition: Section.hh:64
TTAProgram::TPEFProgramFactory::createInstruction
Instruction * createInstruction(const TPEF::ResourceSection &resources, MoveVector &moveElements, ImmediateVector &longImmediates, ImmediateMap &immElements) const
Definition: TPEFProgramFactory.cc:512
TTAProgram::TPEFProgramFactory::getFromCache
Terminal * getFromCache(const CacheKey &key) const
Definition: TPEFProgramFactory.cc:1951
SimValue
Definition: SimValue.hh:96
TTAProgram::TPEFProgramFactory::clearResourceAllocations
void clearResourceAllocations() const
TTAProgram::TPEFProgramFactory::InlineValues
std::map< HalfWord, SimValue * > InlineValues
Definition: TPEFProgramFactory.hh:106
TTAProgram::TPEFProgramFactory::createTerminal
Terminal * createTerminal(const TPEF::ResourceSection &resources, const TTAMachine::Bus *aBus, TTAMachine::Socket::Direction direction, TPEF::MoveElement::FieldType type, HalfWord unitId, HalfWord index, const ImmediateMap *immediateMap=NULL) const
Definition: TPEFProgramFactory.cc:759
TTAMachine::InstructionTemplate
Definition: InstructionTemplate.hh:49
TTAProgram::TPEFProgramFactory
Definition: TPEFProgramFactory.hh:87
TTAMachine::FunctionUnit
Definition: FunctionUnit.hh:55
TTAProgram::TPEFProgramFactory::tpefTools_
TPEF::TPEFTools tpefTools_
TPEFTools object for helper functions.
Definition: TPEFProgramFactory.hh:279
TTAMachine::Unit
Definition: Unit.hh:51
TTAProgram::TPEFProgramFactory::findBus
TTAMachine::Bus & findBus(const TPEF::ResourceSection &resources, HalfWord busId) const
Definition: TPEFProgramFactory.cc:972
TTAProgram::TPEFProgramFactory::SocketAllocation::SocketAllocation
SocketAllocation(std::shared_ptr< Move > m, unsigned int anIndex)
Definition: TPEFProgramFactory.hh:241
TTAProgram::FunctionStart
Definition: TPEFProgramFactory.cc:97
TTAProgram::TPEFProgramFactory::findFunctionUnit
TTAMachine::FunctionUnit & findFunctionUnit(const TPEF::ResourceSection &resources, HalfWord unitId, std::string tpefOpName="") const
Definition: TPEFProgramFactory.cc:1143
TPEF::ASpaceElement
Definition: ASpaceElement.hh:48
TTAProgram::TPEFProgramFactory::CacheKey::CacheKey
CacheKey(const TTAMachine::Bus &aBus, TTAMachine::Socket::Direction aDirection, TPEF::MoveElement::FieldType aType, HalfWord anUnitId, HalfWord anIndex)
Definition: TPEFProgramFactory.hh:193
UniversalMachine
Definition: UniversalMachine.hh:56
ResourceElement.hh
TTAMachine::Port
Definition: Port.hh:54
TTAProgram::TPEFProgramFactory::findAddressSpace
TTAMachine::AddressSpace & findAddressSpace(const TPEF::ASpaceElement *aSpace) const
Definition: TPEFProgramFactory.cc:1333
TTAProgram::TPEFProgramFactory::tpefInstrASpace_
TPEF::ASpaceElement * tpefInstrASpace_
Instruction address space element of TPEF.
Definition: TPEFProgramFactory.hh:283
TTAProgram::TPEFProgramFactory::CacheKey::bus_
const TTAMachine::Bus & bus_
Bus that was used for transport.
Definition: TPEFProgramFactory.hh:222
TTAProgram::TPEFProgramFactory::build
Program * build()
Definition: TPEFProgramFactory.cc:200
TTAMachine::Socket
Definition: Socket.hh:53
TTAProgram::TPEFProgramFactory::ImmediateVector
std::vector< TPEF::ImmediateElement * > ImmediateVector
Definition: TPEFProgramFactory.hh:110
TTAProgram::TPEFProgramFactory::ImmediateMap
std::map< ImmediateKey, TPEF::ImmediateElement * > ImmediateMap
Definition: TPEFProgramFactory.hh:108
TTAProgram::TPEFProgramFactory::SocketAllocation::src
unsigned int src
Definition: TPEFProgramFactory.hh:248
TTAProgram::TPEFProgramFactory::canSourceBeAssigned
bool canSourceBeAssigned(SocketAllocation &alloc, std::map< TTAMachine::Socket *, std::vector< SocketAllocation * > > &fixedSockets) const
Definition: TPEFProgramFactory.cc:1624
TTAProgram::TPEFProgramFactory::SocketAllocation::move
std::shared_ptr< Move > move
Definition: TPEFProgramFactory.hh:245
TTAProgram::TPEFProgramFactory::isFunctionStart
bool isFunctionStart(const TPEF::InstructionElement &instructionElement) const
Definition: TPEFProgramFactory.cc:2458
TTAProgram::TPEFProgramFactory::functionStartPositions_
std::map< const TPEF::InstructionElement *, class FunctionStart * > functionStartPositions_
Stores information of start points of procedures that were found.
Definition: TPEFProgramFactory.hh:287
TTAProgram::TPEFProgramFactory::findGuard
TTAMachine::Guard & findGuard(const TPEF::ResourceSection &resources, TTAMachine::Bus &bus, TPEF::MoveElement::FieldType type, HalfWord unitId, HalfWord index, bool isInverted) const
Definition: TPEFProgramFactory.cc:1373
TTAProgram::TPEFProgramFactory::machine_
const TTAMachine::Machine * machine_
Target machine of program.
Definition: TPEFProgramFactory.hh:274
TTAProgram::TPEFProgramFactory::resolveSocketAllocations
void resolveSocketAllocations(std::vector< SocketAllocation > &allocs) const
Definition: TPEFProgramFactory.cc:1711
TTAProgram::TPEFProgramFactory::SocketAllocation::dstSocks
std::vector< TTAMachine::Socket * > dstSocks
Definition: TPEFProgramFactory.hh:247
TTAProgram::TPEFProgramFactory::universalMachine_
UniversalMachine * universalMachine_
Universal machine of program.
Definition: TPEFProgramFactory.hh:276
TTAProgram::TPEFProgramFactory::MoveVector
std::vector< TPEF::MoveElement * > MoveVector
Definition: TPEFProgramFactory.hh:109
TPEF::TPEFTools
Definition: TPEFTools.hh:62
TTAProgram::TPEFProgramFactory::findImmediateUnit
TTAMachine::ImmediateUnit & findImmediateUnit(const TPEF::ResourceSection &resources, Byte immUnitId) const
Definition: TPEFProgramFactory.cc:1099
TPEFTools.hh
TTAProgram::TPEFProgramFactory::seekFunctionStartPoints
void seekFunctionStartPoints()
Definition: TPEFProgramFactory.cc:1991
TTAProgram::Terminal
Definition: Terminal.hh:60
TTAProgram::TPEFProgramFactory::findRegisterFile
TTAMachine::RegisterFile & findRegisterFile(const TPEF::ResourceSection &resources, HalfWord rfId) const
Definition: TPEFProgramFactory.cc:1021
TTAProgram::TPEFProgramFactory::instructionImmediates_
std::list< std::shared_ptr< Move > > instructionImmediates_
Moves whose source terminals are addresses referring to instructions.
Definition: TPEFProgramFactory.hh:290
program
find Finds info of the inner loops in the program
Definition: InnerLoopFinder.cc:80
TTAProgram::TPEFProgramFactory::functionName
std::string functionName(const TPEF::InstructionElement &instructionElement) const
Definition: TPEFProgramFactory.cc:2471
TTAMachine::RegisterFile
Definition: RegisterFile.hh:47
TTAMachine
Definition: Assembler.hh:48
TTAProgram::TPEFProgramFactory::SocketAllocation::srcSocks
std::vector< TTAMachine::Socket * > srcSocks
Definition: TPEFProgramFactory.hh:246
TTAProgram::TPEFProgramFactory::binary_
const TPEF::Binary * binary_
Binary that is used for creating program.
Definition: TPEFProgramFactory.hh:272
TTAProgram::TPEFProgramFactory::CacheKey::type_
TPEF::MoveElement::FieldType type_
Type of the accessed unit.
Definition: TPEFProgramFactory.hh:226
TTAProgram::TPEFProgramFactory::SocketAllocation::dst
unsigned int dst
Definition: TPEFProgramFactory.hh:249
TTAProgram::TPEFProgramFactory::CacheKey::direction_
TTAMachine::Socket::Direction direction_
Direction if the port was read or written.
Definition: TPEFProgramFactory.hh:224
TTAProgram::TPEFProgramFactory::cache_
std::map< const CacheKey, Terminal * > cache_
Cache map of terminals that are returned by different search parameters.
Definition: TPEFProgramFactory.hh:297
MoveElement.hh
TTAProgram::TPEFProgramFactory::clearCache
void clearCache() const
Definition: TPEFProgramFactory.cc:1979
TPEF::Chunk
Definition: Chunk.hh:45
TTAProgram::TPEFProgramFactory::stringOfChunk
std::string stringOfChunk(const TPEF::Chunk *chunk, const TPEF::Section *chunkOwner) const
Definition: TPEFProgramFactory.cc:180
TTAProgram::TPEFProgramFactory::CacheKey::index_
HalfWord index_
Index of the corresponding TPEF resource element.
Definition: TPEFProgramFactory.hh:230
TTAProgram::TPEFProgramFactory::addProcedures
void addProcedures(Program &program, const TTAMachine::AddressSpace &programASpace) const
Definition: TPEFProgramFactory.cc:344
TPEF
Definition: Assembler.hh:43
TTAMachine::Machine
Definition: Machine.hh:73
Binary.hh
TTAMachine::ImmediateUnit
Definition: ImmediateUnit.hh:50