OpenASIP 2.2
Loading...
Searching...
No Matches
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"
40#include "Move.hh"
41#include "MathTools.hh"
43#include "Conversion.hh"
44
45#include <string>
46#include <ctime>
47
48using std::string;
49
50using namespace TTAMachine;
51using 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 */
72
73/**
74 * Sets a new prefix to be used for generated variable symbols
75 *
76 * @param prefix new prefix to be set
77 */
78void
79CompiledSimSymbolGenerator::enablePrefix(const std::string& prefix) {
80 prefix_ = prefix;
81}
82
83/**
84 * Disables the usage of prefix.
85 */
86void
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 */
100std::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 */
116std::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 */
149std::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 */
162std::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 */
177std::string
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 */
190std::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 */
204std::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 */
214std::string
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 */
227std::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 */
257std::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 */
269std::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 */
281std::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 */
293std::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 */
304std::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 */
319std::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 */
332std::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 */
345std::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 */
355std::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 */
366std::string
368 InstructionAddress address) const {
369 return "setJumpTargetFor_" + Conversion::toString(address);
370}
371
UInt32 InstructionAddress
Definition BaseType.hh:175
std::string basicBlockSymbol(InstructionAddress startAddress) const
std::string DAMemorySymbol(const TTAMachine::FunctionUnit &fu) const
std::string returnAddressSymbol(const TTAMachine::ControlUnit &gcu) const
std::string operationSymbol(const std::string &operationName, const TTAMachine::FunctionUnit &fu) const
std::string operationContextSymbol(const TTAMachine::FunctionUnit &fu) const
std::string busSymbol(const TTAMachine::Bus &bus) const
void enablePrefix(const std::string &prefix)
TCEString globalSymbolSuffix_
Suffix used for the generated global function symbols.
std::string portSymbol(const TTAMachine::Port &port) const
TCEString prefix_
Prefix used for generated variable symbols.
std::string moveOperandSymbol(const TTAProgram::Terminal &terminal, const TTAProgram::Move &move) const
std::string targetMemorySymbol(const TTAMachine::FunctionUnit &fu) const
std::string conflictDetectorSymbol(const TTAMachine::FunctionUnit &fu) const
CompiledSimSymbolGenerator(const TCEString &globalSymbolSuffix)
std::string registerSymbol(const TTAProgram::Terminal &terminal) const
std::string FUResultSymbol(const TTAMachine::Port &port) const
std::string immediateRegisterSymbol(const TTAProgram::Terminal &terminal) const
std::string jumpTargetSetterSymbol(InstructionAddress address) const
static std::string toString(const T &source)
static TCEString registerName(const TTAMachine::RegisterFile &rf, int index, char delim='.')
static SLongWord signExtendTo(SLongWord value, int width)
static ULongWord zeroExtendTo(ULongWord value, int width)
unsigned int unsignedValue() const
Definition SimValue.cc:919
int immediateWidth() const
Definition Bus.cc:160
bool signExtends() const
Definition Bus.cc:171
bool zeroExtends() const
Definition Bus.cc:182
virtual TCEString name() const
SpecialRegisterPort * returnAddressPort() const
Unit * parentUnit() const
virtual std::string name() const
Definition Port.cc:141
const TTAMachine::Bus & bus() const
Definition Move.cc:373
virtual SimValue value() const
Definition Terminal.cc:178
virtual int index() const
Definition Terminal.cc:274
virtual bool isGPR() const
Definition Terminal.cc:107
virtual bool isImmediateRegister() const
Definition Terminal.cc:97
virtual const TTAMachine::Port & port() const
Definition Terminal.cc:378
virtual bool isImmediate() const
Definition Terminal.cc:63
virtual const TTAMachine::ImmediateUnit & immediateUnit() const
Definition Terminal.cc:240
virtual const TTAMachine::RegisterFile & registerFile() const
Definition Terminal.cc:225