OpenASIP 2.2
Loading...
Searching...
No Matches
Functions
RISCVInstructionExecutor.hh File Reference
#include <stdint.h>
Include dependency graph for RISCVInstructionExecutor.hh:

Go to the source code of this file.

Functions

int initializeMachine (const char *machinePath, char **error)
 
int resetMachine ()
 
int unpackInstruction (uint32_t instruction, char **output, char **error)
 
int executeInstruction32 (const char *opName, const uint32_t *inputs, uint32_t inputsCount, uint32_t *output, char **error)
 
int executeInstruction64 (const char *opName, const uint64_t *inputs, uint32_t inputsCount, uint64_t *output, char **error)
 

Detailed Description

Declaration of RISCVInstructionExecutor class.

Author
Eetu Soronen 2025 (eetu..nosp@m.soro.nosp@m.nen@t.nosp@m.uni..nosp@m.fi)
Note
rating: red

Definition in file RISCVInstructionExecutor.hh.

Function Documentation

◆ executeInstruction32()

int executeInstruction32 ( const char *  opName,
const uint32_t *  inputs,
uint32_t  inputsCount,
uint32_t *  output,
char **  error 
)

Executes a custom 32-wide instruction.

The instruction behavior is searched automatically by OperationPool from the OSAL search paths. See chapter 4.4 in the manual.

Parameters
opNameThe operation name as it is in the machine file.
inputsInput value(s) of the operation. Can contain more values than the operation needs, in that case only the first values will be used.
inputsCountnumber of inputs
outputThe result of the operation.
errorWill not be touched in case of success. Must be freed by the client.
Returns
0 on success, -1 on failure.

Definition at line 286 of file RISCVInstructionExecutor.cc.

288 {
289 if (output == nullptr) {
290 if (error != nullptr) {
291 *error = strdup(
292 "ExecuteInstruction32 error: Output parameter is null");
293 }
294 return -1;
295 }
296
297 try {
298 std::vector<uint64_t> inputs64(inputsCount);
299 for (uint32_t i = 0; i < inputsCount; i++) {
300 inputs64[i] = static_cast<uint64_t>(inputs[i]);
301 }
302
303 std::vector<SimValue> results =
305 opName, 32, inputs64.data(), inputsCount);
306 for (size_t i = 0; i < results.size(); i++) {
307 output[i] = results.at(i).uIntWordValue();
308 }
309 return 0;
310 } catch (Exception& e) {
311 if (error != nullptr) {
312 *error = strdup(e.errorMessage().c_str());
313 }
314 return -1;
315 }
316}
std::string errorMessage() const
Definition Exception.cc:123
std::vector< SimValue > executeInstructionHelper(const char *opName, uint8_t width, const uint64_t *inputs, int inputsCount)

References Exception::errorMessage(), and RISCVInstructionExecutor::executeInstructionHelper().

Here is the call graph for this function:

◆ executeInstruction64()

int executeInstruction64 ( const char *  opName,
const uint64_t *  inputs,
uint32_t  inputsCount,
uint64_t *  output,
char **  error 
)

Executes a custom 64-wide instruction.

The instruction behavior is searched automatically by OperationPool from the OSAL search paths. See chapter 4.4 in the manual.

Parameters
opNameThe operation name as it is in the machine file.
inputsInput value(s) of the operation. Can contain more values than the operation needs, in that case only the first values will be used.
inputsCountnumber of inputs
outputThe result of the operation.
errorWill not be touched in case of success. Must be freed by the client.
Returns
0 on success, -1 on failure.

Definition at line 335 of file RISCVInstructionExecutor.cc.

337 {
338 if (output == nullptr) {
339 if (error != nullptr) {
340 *error = strdup(
341 "ExecuteInstruction64 error: Output parameter is null");
342 }
343 return -1;
344 }
345
346 try {
347 std::vector<SimValue> results =
349 opName, 64, inputs, inputsCount);
350 for (size_t i = 0; i < results.size(); i++) {
351 output[i] = results.at(i).uLongWordValue();
352 }
353 return 0;
354 } catch (Exception& e) {
355 if (error != nullptr) {
356 *error = strdup(e.errorMessage().c_str());
357 }
358 return -1;
359 }
360}

References Exception::errorMessage(), and RISCVInstructionExecutor::executeInstructionHelper().

Here is the call graph for this function:

◆ initializeMachine()

int initializeMachine ( const char *  machinePath,
char **  error 
)

Initializes the OpenASIP Machine.

Should be called before other functions. Creates the <instruction name, opcode> map for the custom operations, and the OperationPool instruction cache object.

Parameters
machinePathpath to the .adf machine file.
errorerror messages in case of failure. Can also be a nullptr if desired. Must be freed by the client.
Returns
0 on success, -1 on failure.

Definition at line 147 of file RISCVInstructionExecutor.cc.

147 {
148 std::unique_ptr<TTAMachine::Machine> machine = nullptr;
149
150 try {
151 machine = std::unique_ptr<TTAMachine::Machine>(
153 } catch (const SerializerException& e) {
154 if (error != nullptr) {
155 std::string errorStr =
156 std::string("InitializeMachine error: ") + e.errorMessage();
157 *error = strdup(errorStr.c_str());
158 }
159 return -1;
160 }
161 BinaryEncoding* bem = BEMGenerator(*machine).generate();
162 if (bem == nullptr) {
163 *error = strdup("InitializeMachine error: failed to generate bem");
164 return -1;
165 }
166
169 *error = strdup(
170 "InitializeMachine error: failed to generate custom ops map");
171 return -1;
172 }
173
174 if (RISCVInstructionExecutor::pool == nullptr) {
175 RISCVInstructionExecutor::pool = std::make_unique<OperationPool>();
176 }
177
178 delete bem;
179 return 0;
180}
TTAMachine::Machine * machine
the architecture definition of the estimated processor
BinaryEncoding * generate()
static void findCustomOps(std::map< std::string, int > &customOps_, BinaryEncoding *bem_)
static Machine * loadFromADF(const std::string &adfFileName)
Definition Machine.cc:899
std::unique_ptr< OperationPool > pool
std::map< std::string, int > customOps

References RISCVInstructionExecutor::customOps, Exception::errorMessage(), RISCVTools::findCustomOps(), BEMGenerator::generate(), TTAMachine::Machine::loadFromADF(), machine, and RISCVInstructionExecutor::pool.

Here is the call graph for this function:

◆ resetMachine()

int resetMachine ( )

Resets the machine to uninitialized state.

Deletes the customOps map loaded and deletes the OperationPool instruction cache.

Definition at line 189 of file RISCVInstructionExecutor.cc.

189 {
192 return 0;
193}

References RISCVInstructionExecutor::customOps, and RISCVInstructionExecutor::pool.

◆ unpackInstruction()

int unpackInstruction ( uint32_t  instruction,
char **  output,
char **  error 
)

Unpacks a RISC-V R4-type instruction and returns its string representation if found from the machine file.

Remember to call Initialize machine first.

Parameters
opcodefull RISC-V opcode. Register values are ignored.
outputThe char* representation of the opcode, if it is found. Must be freed by the client.
errorError messages in case of failure. Must be freed by the client.
Returns
0 on success, -1 on failure.

Definition at line 209 of file RISCVInstructionExecutor.cc.

209 {
211 *error = strdup(
212 "UnpackInstruction error: customOps map is empty. Did you "
213 "initialize the machine first?");
214 return -1;
215 }
216
217 if (output == nullptr) {
218 if (error != nullptr) {
219 *error =
220 strdup("UnpackInstruction error: Output parameter is null");
221 }
222 return -1;
223 }
224
225 R4Instruction decodedInstruction =
227
228 bool isCustom0 =
229 (decodedInstruction.baseopcode ==
231 bool isCustom1 =
232 (decodedInstruction.baseopcode ==
234
235 if (!isCustom0 && !isCustom1) {
236 if (error != nullptr) {
237 *error = strdup("UnpackInstruction error: Unknown base opcode");
238 }
239 return -1;
240 }
241
242 std::string opName = "";
243 for (const auto& op : RISCVInstructionExecutor::customOps) {
244 uint32_t encoding = op.second;
245 bool isMatch = false;
246
247 if (isCustom1 &&
248 RISCVTools::getFunc2Int(encoding) == decodedInstruction.funct2 &&
249 RISCVTools::getFunc3Int(encoding) == decodedInstruction.funct3) {
250 isMatch = true;
251 } else if (
252 isCustom0 &&
253 RISCVTools::getFunc7Int(encoding) == decodedInstruction.funct7 &&
254 RISCVTools::getFunc3Int(encoding) == decodedInstruction.funct3) {
255 isMatch = true;
256 }
257
258 if (isMatch) {
259 *output = strdup(op.first.c_str());
260 return 0;
261 }
262 }
263
264 if (error != nullptr) {
265 *error = strdup("UnpackInstruction error: Operation not found");
266 }
267 return -1;
268}
static int getFunc2Int(const int encoding)
static R4Instruction decodeR4Instruction(const uint32_t opcode)
static int getFunc7Int(const int encoding)
static int getFunc3Int(const int encoding)

References R4Instruction::baseopcode, RISCVInstructionExecutor::customOps, RISCVTools::decodeR4Instruction(), R4Instruction::funct2, R4Instruction::funct3, R4Instruction::funct7, RISCVTools::getFunc2Int(), RISCVTools::getFunc3Int(), RISCVTools::getFunc7Int(), RISCVInstructionExecutor::OPC_CUSTOM_0, and RISCVInstructionExecutor::OPC_CUSTOM_1.

Here is the call graph for this function: