OpenASIP 2.2
Loading...
Searching...
No Matches
OperationSimulator.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 OperationSimulator.cc
26 *
27 * Definition of OperationSimulator class.
28 *
29 * @author Jussi Nykänen 2004 (nykanen-no.spam-cs.tut.fi)
30 * @author Pekka Jääskeläinen 2005 (pjaaskel-no.spam-cs.tut.fi)
31 * @note rating: red
32 */
33
34#include "Application.hh"
35#include "OperationSimulator.hh"
36#include "SimValue.hh"
37#include "Conversion.hh"
38#include "Operation.hh"
39#include "Operand.hh"
40#include "TCEString.hh"
41
42using std::string;
43using std::vector;
44
46
47/**
48 * Constructor.
49 */
52
53/**
54 * Destructor.
55 */
58
59/**
60 * Returns the unique instance of OperationSimulator.
61 *
62 * @return The instance of OperationSimulator.
63 */
66 if (instance_ == NULL) {
68 }
69 return *instance_;
70}
71
72/**
73 * Executes the operation trigger command.
74 *
75 * @param op Operation to be simulated.
76 * @param inputs The inputs for the operation.
77 * @param outputs The outputs of the operation.
78 * @param context OperationContext used in simulation.
79 * @param result The possible error string.
80 * @return True if simulation is successful.
81 */
82bool
84 Operation& op,
85 std::vector<DataObject> inputs,
86 std::vector<SimValue*>& outputs,
87 OperationContext& context,
88 std::string& result) {
89
90 if (static_cast<unsigned>(op.numberOfInputs()) != inputs.size()) {
91 result = "wrong number of arguments";
92 return false;
93 }
94
95 if (!initializeOutputs(op, inputs, outputs, result)) {
96 return false;
97 }
98
99 SimValue** io_ = new SimValue*[outputs.size()];
100 for (std::size_t i = 0; i < outputs.size(); ++i) {
101 io_[i] = outputs.at(i);
102 }
103 try {
104 bool ready = op.simulateTrigger(io_, context);
105 delete[] io_;
106 io_ = NULL;
107 return ready;
108 } catch (const Exception& e) {
109 result = e.errorMessage();
110 return false;
111 }
112}
113
114/**
115 * Initializes one SimValue.
116 *
117 * @param value Value.
118 * @param sim SimValue to be initialized.
119 * @param result Possible error string.
120 */
121bool
123 std::string value,
124 SimValue* sim,
125 std::string& result) {
126
127 bool is_int = value.find(".") == string::npos &&
128 value.find('e') == string::npos;
129 bool is_float = !is_int &&
130 (value.find("f") != string::npos ||
131 value.find("F") != string::npos);
132 bool is_half = value.find(".") != string::npos &&
133 (value.find("h") != string::npos ||
134 value.find("H") != string::npos);
135
136 try {
137 if (value.size() > 2 && value[0] == '0' && value[1] == 'x') {
138 (*sim).setValue(TCEString(value));
139 } else if (is_int && !is_float) {
140 *sim = Conversion::toLong(value);
141 } else if (is_float) {
142 *sim = Conversion::toFloat(value.substr(0, value.length()-1));
143 } else if (is_half) {
144 *sim = HalfFloatWord(
145 Conversion::toFloat(value.substr(0, value.length()-1)));
146 } else {
147 *sim = Conversion::toDouble(value);
148 }
149 } catch (const NumberFormatException& n) {
150 result = "illegal operand value \"" + value + "\"";
151 return false;
152 }
153 return true;
154}
155
156/**
157 * Initializes the output values of the operation.
158 *
159 * @param op Operation in which outputs are initialized.
160 * @param inputs Input values.
161 * @param outputs Output values to be initialized.
162 * @param result Possible error message string.
163 * @return True if initialization is successful.
164 */
165bool
167 Operation& op,
168 std::vector<DataObject> inputs,
169 std::vector<SimValue*>& outputs,
170 std::string& result) {
171
172 for (size_t i = 0; i < inputs.size(); i++) {
173 SimValue* sim = new SimValue(op.input(i).width());
174
175 string value = inputs[i].stringValue();
176
177 if (!initializeSimValue(value, sim, result)) {
178 return false;
179 }
180
181 outputs.push_back(sim);
182 }
183
184 for (int i = 0; i < op.numberOfOutputs(); i++) {
185 SimValue* result = new SimValue(op.output(i).width());
186 outputs.push_back(result);
187 }
188 return true;
189}
static SLongWord toLong(const T &source)
static float toFloat(const T &source)
static double toDouble(const T &source)
std::string errorMessage() const
Definition Exception.cc:123
virtual int width() const
Definition Operand.cc:318
static OperationSimulator * instance_
Unique instance.
bool initializeOutputs(Operation &op, std::vector< DataObject > inputs, std::vector< SimValue * > &outputs, std::string &result)
static OperationSimulator & instance()
bool simulateTrigger(Operation &op, std::vector< DataObject > inputs, std::vector< SimValue * > &outputs, OperationContext &context, std::string &result)
bool initializeSimValue(std::string value, SimValue *sim, std::string &result)
virtual Operand & output(int index) const
Definition Operation.cc:526
virtual Operand & input(int index) const
Definition Operation.cc:503
virtual bool simulateTrigger(SimValue **, OperationContext &context) const
Definition Operation.cc:555
virtual int numberOfInputs() const
Definition Operation.cc:192
virtual int numberOfOutputs() const
Definition Operation.cc:202