From 02501ae7a437ba460bed7ef13dd7ff9e23aa70f2 Mon Sep 17 00:00:00 2001 From: Ronald Caesar Date: Wed, 14 Jan 2026 18:15:35 -0400 Subject: [PATCH] engine: add main jit compilation loop Also remove bal_translate_block() body. It will need to be redesigned to be used by bal_engine_run(). Signed-off-by: Ronald Caesar --- include/bal_engine.h | 13 +++++++++++++ src/bal_engine.c | 27 +++++++++++++++++++++++++++ src/bal_translator.c | 41 ++--------------------------------------- 3 files changed, 42 insertions(+), 39 deletions(-) diff --git a/include/bal_engine.h b/include/bal_engine.h index 0f308de..3458ad7 100644 --- a/include/bal_engine.h +++ b/include/bal_engine.h @@ -109,6 +109,19 @@ BAL_ALIGNED(64) typedef struct */ BAL_COLD bal_error_t bal_engine_init(bal_allocator_t *allocator, bal_engine_t *engine); +/*! + * @brief Executes the main JIT compilation loop. + * + * @param[in,out] engine The engine context. Must be initialized. + * @param[in] arm_entry_point Pointer to the start of the ARM machine code + * to translate. + * + * @return BAL_SUCCESS on successfull translation of arm_entry_point. + * @return BAL_ERROR_ENGINE_STATE_INVALID if any function parameters are NULL. + */ +BAL_HOT bal_error_t +bal_engine_run(bal_engine_t *BAL_RESTRICT engine, + const uint32_t *BAL_RESTRICT arm_entry_point); /*! * @brief Resets the engine for the next compilation unit. diff --git a/src/bal_engine.c b/src/bal_engine.c index 6a3949c..59b6fe4 100644 --- a/src/bal_engine.c +++ b/src/bal_engine.c @@ -74,6 +74,33 @@ bal_engine_init (bal_allocator_t *allocator, bal_engine_t *engine) return engine->status; } +bal_error_t +bal_engine_run(bal_engine_t *BAL_RESTRICT engine, const uint32_t *BAL_RESTRICT arm_entry_point) +{ + if (NULL == engine || NULL == arm_entry_point) + { + return BAL_ERROR_ENGINE_STATE_INVALID; + } + + // Load state to registers. + // + bal_instruction_t *BAL_RESTRICT ir_instruction_cursor = engine->instructions + engine->instruction_count; + bal_instruction_t *BAL_RESTRICT ir_instruction_end = engine->instructions + engine->instructions_size; + bal_bit_width_t *BAL_RESTRICT bit_width_cursor = engine->ssa_bit_widths + engine->instruction_count; + bal_source_variable_t *BAL_RESTRICT source_variables_base = engine->source_variables; + + const uint32_t *BAL_RESTRICT arm_instruction_cursor = arm_entry_point; + + while (ir_instruction_cursor < ir_instruction_end) + { + ir_instruction_cursor++; + bit_width_cursor++; + arm_instruction_cursor++; + } + + return engine->status; +} + bal_error_t bal_engine_reset (bal_engine_t *engine) { diff --git a/src/bal_translator.c b/src/bal_translator.c index d5c13b1..f007a76 100644 --- a/src/bal_translator.c +++ b/src/bal_translator.c @@ -1,46 +1,9 @@ #include "bal_translator.h" -typedef struct -{ - bal_instruction_t *BAL_RESTRICT instructions; - bal_bit_width_t *BAL_RESTRICT ssa_bit_widths; - bal_source_variable_t *BAL_RESTRICT source_variables; - size_t instruction_count; - uint32_t max_instructions; - bal_error_t status; -} bal_translation_context_t; - bal_error_t -bal_translate_block (bal_engine_t *BAL_RESTRICT engine, - const uint32_t *BAL_RESTRICT arm_code) +bal_translate_block () { - // Copy pointers to the stack to deal with Double Indirection and Aliasing - // Fear. - // - bal_translation_context_t context; - context.instructions = engine->instructions; - context.ssa_bit_widths = engine->ssa_bit_widths; - context.source_variables = engine->source_variables; - - // Copy other important data to `context` to keep register pressure low. - // - context.instruction_count = engine->instruction_count; - context.max_instructions - = engine->instructions_size / sizeof(bal_instruction_t); - context.status = BAL_SUCCESS; - - for (size_t i = 0; i < context.max_instructions; ++i) - { - // TODO - } - - // Sync back to memory. We should only write to engine once in this - // function. - // - engine->instruction_count = context.instruction_count; - engine->status = context.status; - - return context.status; + // TODO } /*** end of file ***/