GEGELATI
programEngine.h
1
38#ifndef PROGRAMENGINE_H
39#define PROGRAMENGINE_H
40
41#include "data/primitiveTypeArray.h"
42#include "data/untypedSharedPtr.h"
43#include "program/program.h"
44
45namespace Program {
54 {
55 protected:
59
61 ProgramEngine() = delete;
62
65 registers; // If the type of registers attribute is
66 // changed one day
67 // make sure to update the Program::identifyIntrons()
68 // method as it create its own
69 // Data::PrimitiveTypeArray<double> to keep track of
70 // accessed addresses.
71
73 std::vector<std::reference_wrapper<const Data::DataHandler>>
75
77 std::vector<std::reference_wrapper<const Data::DataHandler>>
79
82
83 protected:
93 : programCounter{0}, registers{env.getNbRegisters()}, program{NULL},
95 {
96 // Setup the data sources
97 dataScsConstsAndRegs.push_back(this->registers);
98
99 if (env.getNbConstant() > 0) {
100 dataScsConstsAndRegs.push_back(env.getFakeDataSources().at(1));
101 }
102
103 // Cannot use insert here because it dataSourcesAndRegisters
104 // requires constnessand dataSrc data are not const...
105 for (auto data : env.getDataSources()) {
106 dataScsConstsAndRegs.push_back(data.get());
107 }
108 }
109
125 template <class T>
127 const std::vector<std::reference_wrapper<T>>& dataSrc)
128 : programCounter{0},
129 registers{prog.getEnvironment().getNbRegisters()}, program{NULL}
130 {
131 // Check that T is either convertible to a const DataHandler
132 static_assert(
133 std::is_convertible<T&, const Data::DataHandler&>::value);
134 // Setup the data sources
135 this->dataScsConstsAndRegs.push_back(this->registers);
136
137 if (prog.getEnvironment().getNbConstant() > 0) {
138 this->dataScsConstsAndRegs.push_back(
139 prog.cGetConstantHandler());
140 }
141
142 // Cannot use insert here because it dataSourcesAndRegisters
143 // requires constnessand dataSrc data are not const...
144 for (std::reference_wrapper<T> data : dataSrc) {
145 this->dataScsConstsAndRegs.push_back(data.get());
146 this->dataSources.push_back(data.get());
147 }
148
149 // Set the Program
150 this->setProgram(prog);
151 };
152
163 : ProgramEngine(prog, prog.getEnvironment().getDataSources()){};
164
169 virtual void processLine() = 0;
170
171 public:
181 void setProgram(const Program& prog);
182
192 template <class T>
193 void setDataSources(
194 const std::vector<std::reference_wrapper<T>>& dataSrc);
195
202 const std::vector<std::reference_wrapper<const Data::DataHandler>>&
203 getDataSources() const;
204
215 const bool next();
216
226 const Line& getCurrentLine() const;
227
240
256 const void fetchCurrentOperands(
257 std::vector<Data::UntypedSharedPtr>& operands) const;
258
274 uint64_t getOperandLocation(uint64_t idxOp) const;
275
284 virtual void iterateThroughtProgram(const bool ignoreException);
285 };
286
287 template <class T>
289 const std::vector<std::reference_wrapper<T>>& dataSrc)
290 {
291 // Check that T is either convertible to a const DataHandler
292 static_assert(std::is_convertible<T&, const Data::DataHandler&>::value);
293
294 // Replace the references in attributes
295 this->dataSources = dataSrc;
296 // we need this offset to push the constant at the first
297 size_t offset =
298 this->program->getEnvironment().getNbConstant() > 0 ? 2 : 1;
299 if (offset == 2) {
300 this->dataScsConstsAndRegs.at(1) =
302 }
303 for (size_t idx = 0; idx < this->dataSources.size(); idx++) {
304 this->dataScsConstsAndRegs.at(idx + offset) = dataSrc.at(idx);
305 }
306
307 // Set program to check compatibility with new data source
308 this->setProgram(*this->program);
309 }
310} // namespace Program
311
312#endif // PROGRAMENGINE_H
The Environment class contains all information needed to execute a Program.
Definition: environment.h:84
const std::vector< std::reference_wrapper< const Data::DataHandler > > & getDataSources() const
Get the DataHandler of the Environment.
Definition: environment.cpp:203
size_t getNbConstant() const
Get the number of constants used by programs.
Definition: environment.cpp:172
const std::vector< std::reference_wrapper< const Data::DataHandler > > & getFakeDataSources() const
Definition: environment.cpp:209
This abstract class is the base class for any instruction to be used in a Program.
Definition: instruction.h:59
Definition: line.h:48
This abstract class is the base class for any program engine (generation and execution)
Definition: programEngine.h:54
const Program * program
Definition: programEngine.h:58
virtual void processLine()=0
operator parenthesis used when iterating through the program with the function iterationThroughtProgr...
ProgramEngine(const Environment &env)
Constructor of the class.
Definition: programEngine.h:92
uint64_t programCounter
Program counter of the execution engine.
Definition: programEngine.h:81
const void fetchCurrentOperands(std::vector< Data::UntypedSharedPtr > &operands) const
Get the operands for the current Instruction.
Definition: programEngine.cpp:124
void setDataSources(const std::vector< std::reference_wrapper< T > > &dataSrc)
Method for changing the dataSources on which the Program will be executed.
Definition: programEngine.h:288
const bool next()
Increments the programCounter and checks for the end of the Program.
Definition: programEngine.cpp:96
const std::vector< std::reference_wrapper< const Data::DataHandler > > & getDataSources() const
Get the DataHandler of the ProgramExecutionEngine.
Definition: programEngine.cpp:91
virtual void iterateThroughtProgram(const bool ignoreException)
Function that iterates through the lines of the program and execute the function processLine().
Definition: programEngine.cpp:161
std::vector< std::reference_wrapper< const Data::DataHandler > > dataScsConstsAndRegs
Data sources (including registers) used in the Program.
Definition: programEngine.h:78
uint64_t getOperandLocation(uint64_t idxOp) const
Get the location for the current Instruction.
Definition: programEngine.cpp:144
void setProgram(const Program &prog)
Method for changing the Program executed by a ProgramExecutionEngin.
Definition: programEngine.cpp:41
std::vector< std::reference_wrapper< const Data::DataHandler > > dataSources
Data sources from the environment used for archiving a program.
Definition: programEngine.h:74
ProgramEngine()=delete
Default constructor is deleted.
const Line & getCurrentLine() const
Get the Program Line corresponding to the current programCounter.
Definition: programEngine.cpp:107
Data::PrimitiveTypeArray< double > registers
Registers used for the Program execution.
Definition: programEngine.h:65
const Instructions::Instruction & getCurrentInstruction() const
Get the Instruction corresponding to the current programCounter.
Definition: programEngine.cpp:112
ProgramEngine(const Program &prog, const std::vector< std::reference_wrapper< T > > &dataSrc)
Constructor of the class.
Definition: programEngine.h:126
ProgramEngine(const Program &prog)
Constructor of the class.
Definition: programEngine.h:162
const Data::ConstantHandler & cGetConstantHandler() const
get a const reference to the constantHandler object of the Program
Definition: program.cpp:203
const Environment & getEnvironment() const
Get the environment associated to the Program at construction.
Definition: program.cpp:110
Definition: line.h:43