mirror of
https://github.com/pound-emu/ballistic.git
synced 2026-01-31 01:15:21 +01:00
engine: add engine init function
Signed-off-by: Ronald Caesar <github43132@proton.me>
This commit is contained in:
@@ -35,6 +35,7 @@ add_custom_command(
|
||||
add_library(Ballistic STATIC
|
||||
src/decoder.c
|
||||
src/decoder_table_gen.c
|
||||
src/bal_engine.c
|
||||
)
|
||||
|
||||
target_include_directories(Ballistic PUBLIC include)
|
||||
|
||||
72
src/bal_engine.c
Normal file
72
src/bal_engine.c
Normal file
@@ -0,0 +1,72 @@
|
||||
#include "bal_engine.h"
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
|
||||
#define MAX_INSTRUCTIONS 65536
|
||||
|
||||
// Not sure what exact value to put here.
|
||||
//
|
||||
#define MAX_GUEST_REGISTERS 128
|
||||
|
||||
// Helper macro to align `x` UP to the nearest memory alignment.
|
||||
//
|
||||
#define BAL_ALIGN_UP(x, memory_alignment) \
|
||||
(((x) + ((memory_alignment) - 1)) & ~((memory_alignment) - 1))
|
||||
|
||||
bal_error_t
|
||||
bal_engine_init (bal_allocator_t *allocator, bal_engine_t *engine)
|
||||
{
|
||||
if (NULL == allocator || NULL == engine)
|
||||
{
|
||||
return BAL_ERROR_INVALID_ARGUMENT;
|
||||
}
|
||||
|
||||
size_t source_variable_size
|
||||
= MAX_GUEST_REGISTERS * sizeof(bal_source_variable_t);
|
||||
|
||||
size_t ssa_bit_width_size = MAX_INSTRUCTIONS * sizeof(bal_bit_width_t);
|
||||
size_t instruction_size = MAX_INSTRUCTIONS * sizeof(bal_instruction_t);
|
||||
|
||||
// Calculate amount of memory needed for all arrays in engine.
|
||||
//
|
||||
size_t memory_alignment = 64U;
|
||||
|
||||
size_t offset_instructions
|
||||
= BAL_ALIGN_UP(source_variable_size, memory_alignment);
|
||||
|
||||
size_t offset_ssa_bit_widths = BAL_ALIGN_UP(
|
||||
(offset_instructions + instruction_size), memory_alignment);
|
||||
|
||||
size_t total_size_with_padding = BAL_ALIGN_UP(
|
||||
(offset_ssa_bit_widths + ssa_bit_width_size), memory_alignment);
|
||||
|
||||
uint8_t *data = (uint8_t *)allocator->allocate(
|
||||
allocator, memory_alignment, total_size_with_padding);
|
||||
|
||||
if (NULL == data)
|
||||
{
|
||||
engine->status = BAL_ERROR_ALLOCATION_FAILED;
|
||||
return engine->status;
|
||||
}
|
||||
|
||||
engine->source_variables = (bal_source_variable_t *)data;
|
||||
engine->instructions = (bal_instruction_t *)(data + offset_instructions);
|
||||
engine->ssa_bit_widths = (bal_bit_width_t *)(data + offset_ssa_bit_widths);
|
||||
engine->instruction_count = 0;
|
||||
engine->status = BAL_SUCCESS;
|
||||
engine->arena_base = (void*)data;
|
||||
engine->arena_size = total_size_with_padding;
|
||||
|
||||
(void)memset(engine->source_variables,
|
||||
POISON_UNINITIALIZED_MEMORY,
|
||||
source_variable_size);
|
||||
|
||||
(void)memset(
|
||||
engine->instructions, POISON_UNINITIALIZED_MEMORY, instruction_size);
|
||||
|
||||
(void)memset(engine->ssa_bit_widths,
|
||||
POISON_UNINITIALIZED_MEMORY,
|
||||
ssa_bit_width_size);
|
||||
|
||||
return engine->status;
|
||||
}
|
||||
@@ -2,18 +2,48 @@
|
||||
*
|
||||
* @brief Manages resources while translating ARM blocks to Itermediate
|
||||
* Representation.
|
||||
*/
|
||||
*/
|
||||
|
||||
#ifndef BALLISTIC_ENGINE_H
|
||||
#define BALLISTIC_ENGINE_H
|
||||
#define BALLISTIC_ENGINE_H
|
||||
|
||||
#include "bal_types.h"
|
||||
#include "bal_memory.h"
|
||||
#include "bal_errors.h"
|
||||
#include <stdint.h>
|
||||
|
||||
#define POISON_UNINITIALIZED_MEMORY 0x5a
|
||||
#define POISON_FREED_MEMORY 0x6b
|
||||
|
||||
typedef struct
|
||||
{
|
||||
instruction_t *instructions;
|
||||
uint8_t *ssa_bit_widths;
|
||||
uint16_t instruction_count;
|
||||
uint32_t current_ssa_index;
|
||||
uint32_t original_variable_index;
|
||||
} bal_source_variable_t;
|
||||
|
||||
__attribute__((aligned(64))) typedef struct
|
||||
{
|
||||
// Hot Data
|
||||
//
|
||||
bal_source_variable_t *source_variables;
|
||||
bal_instruction_t *instructions;
|
||||
bal_bit_width_t *ssa_bit_widths;
|
||||
uint16_t instruction_count;
|
||||
bal_error_t status;
|
||||
char _pad[4];
|
||||
|
||||
// Cold Data
|
||||
|
||||
// The pointer returned during heap initialization.
|
||||
// We need this to free the engine's allocated arrays.
|
||||
//
|
||||
void *arena_base;
|
||||
size_t arena_size;
|
||||
|
||||
} bal_engine_t;
|
||||
|
||||
#endif /* BALLISTIC_ENGINE_H */
|
||||
bal_error_t bal_engine_init (bal_allocator_t *allocator, bal_engine_t *engine);
|
||||
|
||||
#endif /* BALLISTIC_ENGINE_H */
|
||||
|
||||
/*** end of file ***/
|
||||
|
||||
18
src/bal_errors.h
Normal file
18
src/bal_errors.h
Normal file
@@ -0,0 +1,18 @@
|
||||
/** @file bal_errors.h
|
||||
*
|
||||
* @brief Defines Ballistic error types.
|
||||
*/
|
||||
|
||||
#ifndef BAL_ERRORS_H
|
||||
#define BAL_ERRORS_H
|
||||
|
||||
typedef enum
|
||||
{
|
||||
BAL_SUCCESS = 0,
|
||||
BAL_ERROR_INVALID_ARGUMENT = -1,
|
||||
BAL_ERROR_ALLOCATION_FAILED = -2,
|
||||
} bal_error_t;
|
||||
|
||||
#endif /* BAL_ERRORS_H */
|
||||
|
||||
/*** end of file ***/
|
||||
@@ -10,6 +10,9 @@
|
||||
#ifndef BALLISTIC_MEMORY_H
|
||||
#define BALLISTIC_MEMORY_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
|
||||
/*!
|
||||
* @brief Function signature for memory allocation.
|
||||
*
|
||||
|
||||
@@ -9,12 +9,14 @@
|
||||
#include <stdint.h>
|
||||
|
||||
typedef uint64_t bal_instruction_t;
|
||||
typedef uint8_t bal_bit_width_t;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
OPCODE_CONST,
|
||||
OPCODE_MOV,
|
||||
OPCODE_ADD,
|
||||
OPCODE_EMUM_END = 0x7FF, // Force enum to 2 bytes.
|
||||
} bal_opcode_t;
|
||||
|
||||
#endif /* BALLISTIC_TYPES_H */
|
||||
|
||||
Reference in New Issue
Block a user