OpenASIP  2.0
CompiledSimSymbolGenerator.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 CompiledSimSymbolGenerator.hh
26  *
27  * Definition of CompiledSimSymbolGenerator class.
28  *
29  * @author Viljami Korhonen 2008 (viljami.korhonen-no.spam-tut.fi)
30  * @note rating: red
31  */
32 
34 #include "FunctionUnit.hh"
35 #include "RegisterFile.hh"
36 #include "ImmediateUnit.hh"
37 #include "ControlUnit.hh"
38 #include "Terminal.hh"
39 #include "SpecialRegisterPort.hh"
40 #include "Move.hh"
41 #include "MathTools.hh"
42 #include "DisassemblyRegister.hh"
43 #include "Conversion.hh"
44 
45 #include <string>
46 #include <ctime>
47 
48 using std::string;
49 
50 using namespace TTAMachine;
51 using namespace TTAProgram;
52 
53 
54 /**
55  * Default constructor.
56  *
57  * @param globalSymbolSuffix Can be used to produce unique
58  * symbol names for the global BB simulation functions.
59  * This has to be used in case using multiple simulation
60  * engines in the same process.
61  */
63  const TCEString& globalSymbolSuffix) :
64  globalSymbolSuffix_(globalSymbolSuffix) {
65 }
66 
67 /**
68  * Default destructor
69  */
71 }
72 
73 /**
74  * Sets a new prefix to be used for generated variable symbols
75  *
76  * @param prefix new prefix to be set
77  */
78 void
79 CompiledSimSymbolGenerator::enablePrefix(const std::string& prefix) {
80  prefix_ = prefix;
81 }
82 
83 /**
84  * Disables the usage of prefix.
85  */
86 void
88  prefix_ = "";
89 }
90 
91 /**
92  * Generates a timestamp
93  *
94  * @note Should not be printed to the simulation sources as this makes
95  * ccache useless. There's always a cache miss due to the changing
96  * timestamp.
97  *
98  * @return a string containing the timestamp
99  */
100 std::string
102  time_t rawTime;
103  time(&rawTime);
104  return string(ctime(&rawTime));
105 }
106 
107 /**
108  * Generates an unique symbol name for the given port
109  *
110  * Converts a TTA unit's port to a unique symbol name that can be later
111  * referred as a SimValue variable in the generated C/C++ code.
112  *
113  * @param port A port of the TTA unit
114  * @return A string containing the generated symbol name
115  */
116 std::string
118  string symbolName;
119  // just speed opt. no need to reserve more at every append.
120  symbolName = prefix_;
121  symbolName.reserve(40);
122  Unit* unit = port.parentUnit();
123  if (dynamic_cast<const FunctionUnit*>(unit) != NULL) {
124  symbolName += "FU_";
125  if (dynamic_cast<const ControlUnit*>(unit) != NULL) {
126  symbolName += "GCU_";
127  }
128  } else if (dynamic_cast<const RegisterFile*>(unit) != NULL) {
129  symbolName += "RF_";
130  if (dynamic_cast<const ImmediateUnit*>(unit) != NULL) {
131  symbolName += "IU_";
132  }
133  } else {
134  symbolName += "unknown_";
135  }
136 
137  symbolName += unit->name();
138  symbolName += "_";
139  symbolName += port.name();
140  return symbolName;
141 }
142 
143 /**
144  * Generates an unique symbol name for a RF register of the terminal
145  *
146  * @param terminal Given terminal
147  * @return A string containing the generated symbol name
148  */
149 std::string
151  const TTAProgram::Terminal& terminal) const {
152  return registerSymbol(terminal.registerFile(), terminal.index());
153 }
154 
155 /**
156  * Generates an unique symbol name for a RF register
157  *
158  * @param rf The given register file
159  * @param index The index of the register
160  * @return A string containing the generated symbol name
161  */
162 std::string
164  const TTAMachine::RegisterFile& rf,
165  int index) const {
166  return prefix_ + "RF_" +
167  DisassemblyRegister::registerName(rf, index, '_');
168 }
169 
170 
171 /**
172  * Generates an unique symbol name for a IU register of the terminal
173  *
174  * @param terminal Given terminal
175  * @return A string containing the generated symbol name
176  */
177 std::string
179  const TTAProgram::Terminal& terminal) const {
180  return immediateRegisterSymbol(terminal.immediateUnit(), terminal.index());
181 }
182 
183 /**
184  * Generates an unique symbol name for a IU register
185  *
186  * @param iu The given IU
187  * @param index Index of the immediate register
188  * @return A string containing the generated symbol name
189  */
190 std::string
192  const TTAMachine::ImmediateUnit& iu,
193  int index) const {
194  return prefix_ + "IU_" + DisassemblyRegister::registerName(iu, index, '_');
195  //.name() + "_" + Conversion::toString(index);
196 }
197 
198 /**
199  * Generates an unique symbol name for a TTA machine bus
200  *
201  * @param bus The given bus
202  * @return A string containing the generated symbol name
203  */
204 std::string
206  return prefix_ + "bus_" + bus.name();
207 }
208 
209 /**
210  * Generates a symbol name for the GCU's return address
211  *
212  * @return A symbol name for the GCU's return address
213  */
214 std::string
216  const TTAMachine::ControlUnit& gcu) const {
217  return portSymbol(*gcu.returnAddressPort());
218 }
219 
220 /**
221  * Generates an unique symbol name for a given move operand (terminal)
222  *
223  * @param terminal The operand terminal
224  * @param move The move
225  * @return a string containing the generated symbol name
226  */
227 std::string
229  const TTAProgram::Terminal& terminal,
230  const TTAProgram::Move & move) const {
231 
232  if (terminal.isGPR()) {
233  return registerSymbol(terminal);
234  } else if (terminal.isImmediateRegister()) {
235  return immediateRegisterSymbol(terminal);
236  } else if (terminal.isImmediate()) {
237  int value = terminal.value().unsignedValue();
238  const Bus& bus = move.bus();
239  if (bus.signExtends()) {
240  value = MathTools::signExtendTo(value, bus.immediateWidth());
241  }
242  else if (bus.zeroExtends()) {
243  value = MathTools::zeroExtendTo(value, bus.immediateWidth());
244  }
245  return "(int)" + Conversion::toString(value) + "u";
246  } else {
247  return portSymbol(terminal.port());
248  }
249 }
250 
251 /**
252  * Generates an unique symbol name for a given operation context
253  *
254  * @param fu The given function unit
255  * @return a string containing the generated symbol name
256  */
257 std::string
259  const TTAMachine::FunctionUnit& fu) const {
260  return prefix_ + "FU_" + fu.name() + "_context";
261 }
262 
263 /**
264  * Generates an unique symbol name for a given FU conflict detector.
265  *
266  *@param fu The given function unit
267  * @return std::string containing the generated symbol name
268  */
269 std::string
271  const TTAMachine::FunctionUnit& fu) const {
272  return prefix_ + "FU_" + fu.name() + "_conflict_detector";
273 }
274 
275 /**
276  * Generates an unique symbol name for the given FU's memory space
277  *
278  * @param fuName given function unit
279  * @return A string containing the generated symbol name
280  */
281 std::string
283  const TTAMachine::FunctionUnit& fu) const {
284  return prefix_ + fu.name() + "_target_memory";
285 }
286 
287 /**
288  * Generates an unique symbol name for the given FU's DirectAccessMemory&
289  *
290  * @param fuName Given function unit
291  * @return A string containing the generated symbol name
292  */
293 std::string
295  const {
296  return prefix_ + fu.name() + "_direct_access_memory";
297 }
298 
299 /**
300  * Generates an unique symbol name for a temporary guard variable
301  *
302  * @return A string containing the generated symbol name
303  */
304 std::string
306  static int guardNumber = 0;
307  return "guard_" + Conversion::toString(guardNumber++);
308 }
309 
310 /**
311  * Generates an unique symbol name for a basic block function
312  *
313  * A new C/C++ function is created for each basic block in the program, so the
314  * generated symbol will also be used as the generated function name.
315  *
316  * @param startAddress The start address of the basic block
317  * @return A string containing the generated symbol name
318  */
319 std::string
321  const {
322  return "simulate_" + globalSymbolSuffix_ + "_" + Conversion::toString(startAddress);
323 }
324 
325 /**
326  * Generates an unique symbol name for a FU's operation
327  *
328  * @param operationName Name of the operation
329  * @param fu The function unit containing the operation
330  * @return The generated operation symbol string
331  */
332 std::string
334  const std::string& operationName,
335  const TTAMachine::FunctionUnit& fu) const {
336  return prefix_ + "op_" + fu.name() + "_" + operationName;
337 }
338 
339 /**
340  * Generates an unique symbol name for FU's results.
341  *
342  * @param port The FU port
343  * @return A string containing the generated FU results symbol
344  */
345 std::string
347  return portSymbol(port) + "_results";
348 }
349 
350 /**
351  * Generates name for temporary variables using incremental numbers
352  *
353  * @return The generated temporary variable name
354  */
355 std::string
357  static int counter = 0;
358  return "_tmp_variable_" + Conversion::toString(counter++);
359 }
360 
361 /**
362  * Generates name for jump target setting function
363  *
364  * @param address address of the simulate function to be set
365  */
366 std::string
368  InstructionAddress address) const {
369  return "setJumpTargetFor_" + Conversion::toString(address);
370 }
371 
TTAMachine::Bus::immediateWidth
int immediateWidth() const
Definition: Bus.cc:160
CompiledSimSymbolGenerator::immediateRegisterSymbol
std::string immediateRegisterSymbol(const TTAProgram::Terminal &terminal) const
Definition: CompiledSimSymbolGenerator.cc:178
TTAProgram
Definition: Estimator.hh:65
CompiledSimSymbolGenerator::prefix_
TCEString prefix_
Prefix used for generated variable symbols.
Definition: CompiledSimSymbolGenerator.hh:130
InstructionAddress
UInt32 InstructionAddress
Definition: BaseType.hh:175
CompiledSimSymbolGenerator::disablePrefix
void disablePrefix()
Definition: CompiledSimSymbolGenerator.cc:87
TTAMachine::Component::name
virtual TCEString name() const
Definition: MachinePart.cc:125
TTAProgram::Terminal::index
virtual int index() const
Definition: Terminal.cc:274
TTAMachine::Bus::zeroExtends
bool zeroExtends() const
Definition: Bus.cc:182
TTAProgram::Terminal::registerFile
virtual const TTAMachine::RegisterFile & registerFile() const
Definition: Terminal.cc:225
CompiledSimSymbolGenerator::portSymbol
std::string portSymbol(const TTAMachine::Port &port) const
Definition: CompiledSimSymbolGenerator.cc:117
CompiledSimSymbolGenerator::operationSymbol
std::string operationSymbol(const std::string &operationName, const TTAMachine::FunctionUnit &fu) const
Definition: CompiledSimSymbolGenerator.cc:333
TTAMachine::Bus
Definition: Bus.hh:53
CompiledSimSymbolGenerator::busSymbol
std::string busSymbol(const TTAMachine::Bus &bus) const
Definition: CompiledSimSymbolGenerator.cc:205
CompiledSimSymbolGenerator::DAMemorySymbol
std::string DAMemorySymbol(const TTAMachine::FunctionUnit &fu) const
Definition: CompiledSimSymbolGenerator.cc:294
TTAProgram::Move::bus
const TTAMachine::Bus & bus() const
Definition: Move.cc:373
CompiledSimSymbolGenerator::returnAddressSymbol
std::string returnAddressSymbol(const TTAMachine::ControlUnit &gcu) const
Definition: CompiledSimSymbolGenerator.cc:215
ImmediateUnit.hh
CompiledSimSymbolGenerator::operationContextSymbol
std::string operationContextSymbol(const TTAMachine::FunctionUnit &fu) const
Definition: CompiledSimSymbolGenerator.cc:258
CompiledSimSymbolGenerator::globalSymbolSuffix_
TCEString globalSymbolSuffix_
Suffix used for the generated global function symbols.
Definition: CompiledSimSymbolGenerator.hh:133
Terminal.hh
CompiledSimSymbolGenerator::CompiledSimSymbolGenerator
CompiledSimSymbolGenerator(const TCEString &globalSymbolSuffix)
Definition: CompiledSimSymbolGenerator.cc:62
CompiledSimSymbolGenerator::jumpTargetSetterSymbol
std::string jumpTargetSetterSymbol(InstructionAddress address) const
Definition: CompiledSimSymbolGenerator.cc:367
Conversion::toString
static std::string toString(const T &source)
TTAMachine::Bus::signExtends
bool signExtends() const
Definition: Bus.cc:171
TTAMachine::FunctionUnit
Definition: FunctionUnit.hh:55
CompiledSimSymbolGenerator::guardBoolSymbol
std::string guardBoolSymbol() const
Definition: CompiledSimSymbolGenerator.cc:305
TTAProgram::Terminal::isImmediateRegister
virtual bool isImmediateRegister() const
Definition: Terminal.cc:97
TTAMachine::Unit
Definition: Unit.hh:51
MathTools::zeroExtendTo
static ULongWord zeroExtendTo(ULongWord value, int width)
CompiledSimSymbolGenerator::basicBlockSymbol
std::string basicBlockSymbol(InstructionAddress startAddress) const
Definition: CompiledSimSymbolGenerator.cc:320
TTAMachine::ControlUnit
Definition: ControlUnit.hh:50
Conversion.hh
TTAMachine::Port
Definition: Port.hh:54
CompiledSimSymbolGenerator::registerSymbol
std::string registerSymbol(const TTAProgram::Terminal &terminal) const
Definition: CompiledSimSymbolGenerator.cc:150
TTAProgram::Terminal::isGPR
virtual bool isGPR() const
Definition: Terminal.cc:107
TTAProgram::Terminal::value
virtual SimValue value() const
Definition: Terminal.cc:178
TTAProgram::Terminal::immediateUnit
virtual const TTAMachine::ImmediateUnit & immediateUnit() const
Definition: Terminal.cc:240
CompiledSimSymbolGenerator::enablePrefix
void enablePrefix(const std::string &prefix)
Definition: CompiledSimSymbolGenerator.cc:79
CompiledSimSymbolGenerator::FUResultSymbol
std::string FUResultSymbol(const TTAMachine::Port &port) const
Definition: CompiledSimSymbolGenerator.cc:346
TTAProgram::Move
Definition: Move.hh:55
SimValue::unsignedValue
unsigned int unsignedValue() const
Definition: SimValue.cc:919
CompiledSimSymbolGenerator::generateTempVariable
std::string generateTempVariable() const
Definition: CompiledSimSymbolGenerator.cc:356
DisassemblyRegister.hh
CompiledSimSymbolGenerator::targetMemorySymbol
std::string targetMemorySymbol(const TTAMachine::FunctionUnit &fu) const
Definition: CompiledSimSymbolGenerator.cc:282
RegisterFile.hh
TTAMachine::Port::name
virtual std::string name() const
Definition: Port.cc:141
TCEString
Definition: TCEString.hh:53
ControlUnit.hh
SpecialRegisterPort.hh
TTAProgram::Terminal
Definition: Terminal.hh:60
TTAProgram::Terminal::port
virtual const TTAMachine::Port & port() const
Definition: Terminal.cc:378
DisassemblyRegister::registerName
static TCEString registerName(const TTAMachine::RegisterFile &rf, int index, char delim='.')
Definition: DisassemblyRegister.cc:95
TTAMachine::RegisterFile
Definition: RegisterFile.hh:47
MathTools.hh
Move.hh
TTAMachine
Definition: Assembler.hh:48
TTAMachine::ControlUnit::returnAddressPort
SpecialRegisterPort * returnAddressPort() const
Definition: ControlUnit.cc:307
TTAProgram::Terminal::isImmediate
virtual bool isImmediate() const
Definition: Terminal.cc:63
CompiledSimSymbolGenerator::moveOperandSymbol
std::string moveOperandSymbol(const TTAProgram::Terminal &terminal, const TTAProgram::Move &move) const
Definition: CompiledSimSymbolGenerator.cc:228
CompiledSimSymbolGenerator::conflictDetectorSymbol
std::string conflictDetectorSymbol(const TTAMachine::FunctionUnit &fu) const
Definition: CompiledSimSymbolGenerator.cc:270
MathTools::signExtendTo
static SLongWord signExtendTo(SLongWord value, int width)
CompiledSimSymbolGenerator::timeStamp
std::string timeStamp() const
Definition: CompiledSimSymbolGenerator.cc:101
CompiledSimSymbolGenerator.hh
FunctionUnit.hh
CompiledSimSymbolGenerator::~CompiledSimSymbolGenerator
virtual ~CompiledSimSymbolGenerator()
Definition: CompiledSimSymbolGenerator.cc:70
TTAMachine::Port::parentUnit
Unit * parentUnit() const
TTAMachine::ImmediateUnit
Definition: ImmediateUnit.hh:50