engine: add reset() function

Also added the size of each array to bal_engine_t to make finding the
end of the array in memory simple and easy.

Signed-off-by: Ronald Caesar <github43132@proton.me>
This commit is contained in:
Ronald Caesar
2026-01-10 13:19:24 -04:00
parent a4dde266f0
commit 6160510de7
2 changed files with 65 additions and 13 deletions

View File

@@ -21,24 +21,24 @@ bal_engine_init (bal_allocator_t *allocator, bal_engine_t *engine)
return BAL_ERROR_INVALID_ARGUMENT;
}
size_t source_variable_size
size_t source_variables_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);
size_t ssa_bit_widths_size = MAX_INSTRUCTIONS * sizeof(bal_bit_width_t);
size_t instructions_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);
= BAL_ALIGN_UP(source_variables_size, memory_alignment);
size_t offset_ssa_bit_widths = BAL_ALIGN_UP(
(offset_instructions + instruction_size), memory_alignment);
(offset_instructions + instructions_size), memory_alignment);
size_t total_size_with_padding = BAL_ALIGN_UP(
(offset_ssa_bit_widths + ssa_bit_width_size), memory_alignment);
(offset_ssa_bit_widths + ssa_bit_widths_size), memory_alignment);
uint8_t *data = (uint8_t *)allocator->allocate(
allocator, memory_alignment, total_size_with_padding);
@@ -52,21 +52,45 @@ bal_engine_init (bal_allocator_t *allocator, bal_engine_t *engine)
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;
engine->source_variables_size = source_variables_size;
engine->instructions_size = instructions_size;
engine->ssa_bit_widths_size = ssa_bit_widths_size;
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);
source_variables_size);
(void)memset(
engine->instructions, POISON_UNINITIALIZED_MEMORY, instruction_size);
engine->instructions, POISON_UNINITIALIZED_MEMORY, instructions_size);
(void)memset(engine->ssa_bit_widths,
POISON_UNINITIALIZED_MEMORY,
ssa_bit_width_size);
ssa_bit_widths_size);
return engine->status;
}
bal_error_t
bal_engine_reset (bal_engine_t *engine)
{
if (NULL == engine)
{
return BAL_ERROR_INVALID_ARGUMENT;
}
engine->instruction_count = 0;
engine->status = BAL_SUCCESS;
size_t source_variables_size
= MAX_GUEST_REGISTERS * sizeof(bal_source_variable_t);
(void)memset(engine->source_variables,
POISON_UNINITIALIZED_MEMORY,
source_variables_size);
return engine->status;
}

View File

@@ -60,6 +60,21 @@ __attribute__((aligned(64))) typedef struct
*/
bal_bit_width_t *ssa_bit_widths;
/*!
* @brief Size of source variable array.
*/
size_t source_variables_size;
/*!
* @brief Size of instruction array.
*/
size_t instructions_size;
/*!
* @brief Size of ssa bit width array.
*/
size_t ssa_bit_widths_size;
/*!
* @brief The current number of instructions emitted.
*/
@@ -99,6 +114,19 @@ __attribute__((aligned(64))) typedef struct
*/
bal_error_t bal_engine_init (bal_allocator_t *allocator, bal_engine_t *engine);
/*!
* @brief Resets the engine for the next compilation unit.
*
* @details
* This is a low cost memory operation.
*
* @param[in,out] engine The engine to reset.
*
* @return BAL_SUCCESS on success.
* @return BAL_ERROR_INVALID_ARG if arguments are NULL.
*/
bal_error_t bal_engine_reset(bal_engine_t *engine);
#endif /* BALLISTIC_ENGINE_H */
/*** end of file ***/