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 <github43132@proton.me>
This commit is contained in:
Ronald Caesar
2026-01-14 18:15:35 -04:00
parent 1a1ce44c87
commit 02501ae7a4
3 changed files with 42 additions and 39 deletions

View File

@@ -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.

View File

@@ -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)
{

View File

@@ -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 ***/