engine: add constants array

This was tricky one to add to bal_engine_t because the struct was
already pretty packed. The struct only had 12 bytes to spare but to add
the constants array and its size required 16 bytes. I determined that
ssa_bit_widths_size was not required since ssa_bit_widths[] and
instructions[] will be parallel to each other, so one variable can keep
track of both arrays.

Signed-off-by: Ronald Caesar <github43132@proton.me>
This commit is contained in:
Ronald Caesar
2026-01-15 12:09:01 -04:00
parent 4e4e48e495
commit 7f792157f5
3 changed files with 35 additions and 13 deletions

View File

@@ -46,7 +46,8 @@ BAL_ALIGNED(64) typedef struct
bal_source_variable_t *source_variables; bal_source_variable_t *source_variables;
/*! /*!
* @brief The linear buffer of generated IR instructions. * @brief The linear buffer of generated IR instructions in a compilation
* unit.
*/ */
bal_instruction_t *instructions; bal_instruction_t *instructions;
@@ -55,6 +56,11 @@ BAL_ALIGNED(64) typedef struct
*/ */
bal_bit_width_t *ssa_bit_widths; bal_bit_width_t *ssa_bit_widths;
/*!
* @brief Linear buffer of constants generated in a compilation unit.
*/
bal_constant_t *constants;
/*! /*!
* @brief Size of source variable array. * @brief Size of source variable array.
*/ */
@@ -66,22 +72,29 @@ BAL_ALIGNED(64) typedef struct
size_t instructions_size; size_t instructions_size;
/*! /*!
* @brief Size of ssa bit width array. * @brief Size of the constants array.
*/ */
size_t ssa_bit_widths_size; size_t constants_size;
/*! /*!
* @brief The current number of instructions emitted. * @brief The current number of instructions emitted.
*
* @detials
* This keeps track of where we are in the instruction and ssa bit width
* arrays to make sure we dont cause an instruction overflow.
*/ */
bal_instruction_count_t instruction_count; bal_instruction_count_t instruction_count;
char _padding[2];
/*! /*!
* @brief The Engine's error state. * @brief The Engine's state.
* @details If an operation fails, this is set to a specific error code. *
* @details
* If an operation fails, this is set to a specific error code.
* Subsequent operations will silently fail until the engine is reset. * Subsequent operations will silently fail until the engine is reset.
*/ */
bal_error_t status; bal_error_t status;
char _pad[4];
/* Cold Data */ /* Cold Data */

View File

@@ -11,6 +11,7 @@
typedef uint64_t bal_guest_address_t; typedef uint64_t bal_guest_address_t;
typedef uint32_t bal_instruction_t; typedef uint32_t bal_instruction_t;
typedef uint32_t bal_constant_t;
typedef uint16_t bal_instruction_count_t; typedef uint16_t bal_instruction_count_t;
typedef uint16_t bal_ssa_id_t; typedef uint16_t bal_ssa_id_t;
typedef uint8_t bal_bit_width_t; typedef uint8_t bal_bit_width_t;

View File

@@ -26,6 +26,7 @@ bal_engine_init (bal_allocator_t *allocator, bal_engine_t *engine)
size_t ssa_bit_widths_size = MAX_INSTRUCTIONS * sizeof(bal_bit_width_t); size_t ssa_bit_widths_size = MAX_INSTRUCTIONS * sizeof(bal_bit_width_t);
size_t instructions_size = MAX_INSTRUCTIONS * sizeof(bal_instruction_t); size_t instructions_size = MAX_INSTRUCTIONS * sizeof(bal_instruction_t);
size_t constants_size = MAX_INSTRUCTIONS * sizeof(bal_instruction_t);
// Calculate amount of memory needed for all arrays in engine. // Calculate amount of memory needed for all arrays in engine.
// //
@@ -37,9 +38,12 @@ bal_engine_init (bal_allocator_t *allocator, bal_engine_t *engine)
size_t offset_ssa_bit_widths = BAL_ALIGN_UP( size_t offset_ssa_bit_widths = BAL_ALIGN_UP(
(offset_instructions + instructions_size), memory_alignment); (offset_instructions + instructions_size), memory_alignment);
size_t total_size_with_padding = BAL_ALIGN_UP( size_t offset_constants = BAL_ALIGN_UP(
(offset_ssa_bit_widths + ssa_bit_widths_size), memory_alignment); (offset_ssa_bit_widths + ssa_bit_widths_size), memory_alignment);
size_t total_size_with_padding
= BAL_ALIGN_UP((offset_constants + constants_size), memory_alignment);
uint8_t *data = (uint8_t *)allocator->allocate( uint8_t *data = (uint8_t *)allocator->allocate(
allocator, memory_alignment, total_size_with_padding); allocator, memory_alignment, total_size_with_padding);
@@ -52,9 +56,10 @@ bal_engine_init (bal_allocator_t *allocator, bal_engine_t *engine)
engine->source_variables = (bal_source_variable_t *)data; engine->source_variables = (bal_source_variable_t *)data;
engine->instructions = (bal_instruction_t *)(data + offset_instructions); engine->instructions = (bal_instruction_t *)(data + offset_instructions);
engine->ssa_bit_widths = (bal_bit_width_t *)(data + offset_ssa_bit_widths); engine->ssa_bit_widths = (bal_bit_width_t *)(data + offset_ssa_bit_widths);
engine->constants = (bal_constant_t *)(data + offset_constants);
engine->source_variables_size = source_variables_size; engine->source_variables_size = source_variables_size;
engine->instructions_size = instructions_size; engine->instructions_size = instructions_size;
engine->ssa_bit_widths_size = ssa_bit_widths_size; engine->constants_size = constants_size;
engine->instruction_count = 0; engine->instruction_count = 0;
engine->status = BAL_SUCCESS; engine->status = BAL_SUCCESS;
engine->arena_base = (void *)data; engine->arena_base = (void *)data;
@@ -71,6 +76,9 @@ bal_engine_init (bal_allocator_t *allocator, bal_engine_t *engine)
POISON_UNINITIALIZED_MEMORY, POISON_UNINITIALIZED_MEMORY,
ssa_bit_widths_size); ssa_bit_widths_size);
(void)memset(
engine->constants, POISON_UNINITIALIZED_MEMORY, constants_size);
return engine->status; return engine->status;
} }
@@ -79,7 +87,7 @@ bal_engine_translate (bal_engine_t *BAL_RESTRICT engine,
bal_memory_interface_t *BAL_RESTRICT interface, bal_memory_interface_t *BAL_RESTRICT interface,
const uint32_t *BAL_RESTRICT arm_entry_point) const uint32_t *BAL_RESTRICT arm_entry_point)
{ {
if (NULL == engine || NULL == arm_entry_point) if (BAL_UNLIKELY(NULL == engine || NULL == arm_entry_point))
{ {
return BAL_ERROR_ENGINE_STATE_INVALID; return BAL_ERROR_ENGINE_STATE_INVALID;
} }
@@ -121,12 +129,12 @@ bal_engine_reset (bal_engine_t *engine)
engine->instruction_count = 0; engine->instruction_count = 0;
engine->status = BAL_SUCCESS; engine->status = BAL_SUCCESS;
size_t source_variables_size
= MAX_GUEST_REGISTERS * sizeof(bal_source_variable_t);
(void)memset(engine->source_variables, (void)memset(engine->source_variables,
POISON_UNINITIALIZED_MEMORY, POISON_UNINITIALIZED_MEMORY,
source_variables_size); engine->source_variables_size);
(void)memset(
engine->constants, POISON_UNINITIALIZED_MEMORY, engine->constants_size);
return engine->status; return engine->status;
} }