engine: add translation loop switch statement

Also made the decoder function name shorter.

Signed-off-by: Ronald Caesar <github43132@proton.me>
This commit is contained in:
Ronald Caesar
2026-01-24 00:34:41 -04:00
parent b81c721feb
commit 5dada6c7a4
7 changed files with 43 additions and 12 deletions

View File

@@ -19,6 +19,7 @@ extern "C"
{
#endif
#define BAL_OPERANDS_SIZE 4
#define BAL_OPERAND_BIT_WIDTH 5
/// The type of an instruction operand.
@@ -69,7 +70,7 @@ extern "C"
bal_opcode_t ir_opcode;
/// Descriptors for up to 4 operands.
bal_decoder_operand_t operand[4];
bal_decoder_operand_t operands[BAL_OPERANDS_SIZE];
} bal_decoder_instruction_metadata_t;
static_assert(32 == sizeof(bal_decoder_instruction_metadata_t), "Expected decoder metadata struct to be 32 bytes.");
@@ -84,7 +85,7 @@ extern "C"
///
/// The pointer refers to static readonly memory. It is valid for the
/// lifetime of the program and must not be freed.
BAL_HOT const bal_decoder_instruction_metadata_t *bal_decoder_arm64_decode(
BAL_HOT const bal_decoder_instruction_metadata_t *bal_decode_arm64(
const uint32_t instruction);
#ifdef __cplusplus

View File

@@ -14,7 +14,6 @@ typedef uint64_t bal_instruction_t;
typedef uint32_t bal_constant_t;
typedef uint16_t bal_instruction_count_t;
typedef uint16_t bal_constant_count_t;
typedef uint16_t bal_ssa_id_t;
typedef uint8_t bal_bit_width_t;
typedef enum

View File

@@ -27,6 +27,14 @@
#define BAL_ALIGN_UP(x, memory_alignment) \
(((x) + ((memory_alignment) - 1)) & ~((memory_alignment) - 1))
static uint32_t extract_operand_value(const uint32_t,
const bal_decoder_operand_t *);
static uint32_t intern_constant(bal_constant_t,
bal_constant_t *,
bal_constant_count_t *,
size_t,
bal_error_t *);
bal_error_t
bal_engine_init (bal_allocator_t *allocator, bal_engine_t *engine)
{
@@ -122,9 +130,32 @@ bal_engine_translate (bal_engine_t *BAL_RESTRICT engine,
bal_constant_t constant_count = engine->constant_count;
bal_instruction_count_t instruction_count = engine->instruction_count;
const uint32_t *BAL_RESTRICT arm_instruction_cursor = arm_entry_point;
uint32_t arm_register_values[BAL_OPERANDS_SIZE] = { 0 };
while (ir_instruction_cursor < ir_instruction_end)
{
const bal_decoder_instruction_metadata_t *metadata
= bal_decode_arm64(*arm_instruction_cursor);
const bal_decoder_operand_t *BAL_RESTRICT operands = metadata->operands;
for (size_t i = 0; i < BAL_OPERANDS_SIZE; ++i)
{
arm_register_values[i]
= extract_operand_value(*arm_instruction_cursor, &operands[i]);
}
switch (metadata->ir_opcode)
{
case OPCODE_ADD: {
uint32_t rd = arm_register_values[0];
uint32_t rn = arm_register_values[1];
uint32_t rm = arm_register_values[2];
// TODO
}
default:
break;
}
ir_instruction_cursor++;
bit_width_cursor++;
arm_instruction_cursor++;
@@ -167,16 +198,16 @@ bal_engine_destroy (bal_allocator_t *allocator, bal_engine_t *engine)
}
BAL_HOT static uint32_t
extract_operand_value (bal_instruction_t instruction,
bal_decoder_operand_t *operand)
extract_operand_value (const uint32_t instruction,
const bal_decoder_operand_t *operand)
{
if (BAL_OPERAND_TYPE_NONE == operand->type)
{
return 0;
}
uint32_t mask = (1U << (uint32_t)operand->bit_width) - 1;
uint32_t bits = (instruction >> (uint32_t)operand->bit_position) & mask;
uint32_t mask = (1U << operand->bit_width) - 1;
uint32_t bits = (instruction >> operand->bit_position) & mask;
return bits;
}
@@ -199,7 +230,7 @@ intern_constant (bal_constant_t constant,
}
constants[*count] = constant;
uint32_t index = *count | BAL_IS_CONSTANT_BIT_POSITION;
uint32_t index = *count | BAL_IS_CONSTANT_BIT_POSITION;
(*count)++;
return index;
}

View File

@@ -4,7 +4,7 @@
#include <stdio.h>
const bal_decoder_instruction_metadata_t *
bal_decoder_arm64_decode (const uint32_t instruction)
bal_decode_arm64(const uint32_t instruction)
{
// Index is top 11 bits
const uint16_t index = instruction >> 21;

View File

@@ -71,7 +71,7 @@ main (void)
// Device Under Test.
//
const bal_decoder_instruction_metadata_t *dut_result
= bal_decoder_arm64_decode(instruction);
= bal_decode_arm64(instruction);
const bal_decoder_instruction_metadata_t *ref_result = NULL;

View File

@@ -63,7 +63,7 @@ main (int argc, char *argv[])
}
const bal_decoder_instruction_metadata_t *metadata
= bal_decoder_arm64_decode(instruction);
= bal_decode_arm64(instruction);
if (NULL == metadata)
{

View File

@@ -49,7 +49,7 @@ main (int argc, char *argv[])
uint32_t instruction = (uint32_t)value;
const bal_decoder_instruction_metadata_t* metadata
= bal_decoder_arm64_decode(instruction);
= bal_decode_arm64(instruction);
if (NULL == metadata)
{
printf("UNDEFINED\n");