From 7f792157f53b64a0814c1439ecde7c2333065d6c Mon Sep 17 00:00:00 2001 From: Ronald Caesar Date: Thu, 15 Jan 2026 12:09:01 -0400 Subject: [PATCH] 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 --- include/bal_engine.h | 25 +++++++++++++++++++------ include/bal_types.h | 1 + src/bal_engine.c | 22 +++++++++++++++------- 3 files changed, 35 insertions(+), 13 deletions(-) diff --git a/include/bal_engine.h b/include/bal_engine.h index 95e0f32..f8eb98f 100644 --- a/include/bal_engine.h +++ b/include/bal_engine.h @@ -46,7 +46,8 @@ BAL_ALIGNED(64) typedef struct 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; @@ -55,6 +56,11 @@ BAL_ALIGNED(64) typedef struct */ 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. */ @@ -66,22 +72,29 @@ BAL_ALIGNED(64) typedef struct 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. + * + * @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; + char _padding[2]; + /*! - * @brief The Engine's error state. - * @details If an operation fails, this is set to a specific error code. + * @brief The Engine's state. + * + * @details + * If an operation fails, this is set to a specific error code. * Subsequent operations will silently fail until the engine is reset. */ bal_error_t status; - char _pad[4]; /* Cold Data */ diff --git a/include/bal_types.h b/include/bal_types.h index 0d84ce7..4dc160b 100644 --- a/include/bal_types.h +++ b/include/bal_types.h @@ -11,6 +11,7 @@ typedef uint64_t bal_guest_address_t; typedef uint32_t bal_instruction_t; +typedef uint32_t bal_constant_t; typedef uint16_t bal_instruction_count_t; typedef uint16_t bal_ssa_id_t; typedef uint8_t bal_bit_width_t; diff --git a/src/bal_engine.c b/src/bal_engine.c index ce9082e..75d69a3 100644 --- a/src/bal_engine.c +++ b/src/bal_engine.c @@ -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 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. // @@ -37,9 +38,12 @@ bal_engine_init (bal_allocator_t *allocator, bal_engine_t *engine) size_t offset_ssa_bit_widths = BAL_ALIGN_UP( (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); + size_t total_size_with_padding + = BAL_ALIGN_UP((offset_constants + constants_size), memory_alignment); + uint8_t *data = (uint8_t *)allocator->allocate( 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->instructions = (bal_instruction_t *)(data + offset_instructions); 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->instructions_size = instructions_size; - engine->ssa_bit_widths_size = ssa_bit_widths_size; + engine->constants_size = constants_size; engine->instruction_count = 0; engine->status = BAL_SUCCESS; engine->arena_base = (void *)data; @@ -71,6 +76,9 @@ bal_engine_init (bal_allocator_t *allocator, bal_engine_t *engine) POISON_UNINITIALIZED_MEMORY, ssa_bit_widths_size); + (void)memset( + engine->constants, POISON_UNINITIALIZED_MEMORY, constants_size); + return engine->status; } @@ -79,7 +87,7 @@ bal_engine_translate (bal_engine_t *BAL_RESTRICT engine, bal_memory_interface_t *BAL_RESTRICT interface, 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; } @@ -121,12 +129,12 @@ bal_engine_reset (bal_engine_t *engine) 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); + engine->source_variables_size); + + (void)memset( + engine->constants, POISON_UNINITIALIZED_MEMORY, engine->constants_size); return engine->status; }