diff --git a/include/bal_decoder.h b/include/bal_decoder.h index ff44c73..0a2d56a 100644 --- a/include/bal_decoder.h +++ b/include/bal_decoder.h @@ -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 diff --git a/include/bal_types.h b/include/bal_types.h index 1e72101..b7ca4b9 100644 --- a/include/bal_types.h +++ b/include/bal_types.h @@ -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 diff --git a/src/bal_engine.c b/src/bal_engine.c index d0c172b..3535cca 100644 --- a/src/bal_engine.c +++ b/src/bal_engine.c @@ -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; } diff --git a/src/decoder.c b/src/decoder.c index d16830e..5b5b2a2 100644 --- a/src/decoder.c +++ b/src/decoder.c @@ -4,7 +4,7 @@ #include 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; diff --git a/tests/test_decoder.c b/tests/test_decoder.c index 77dbe3c..dfc96b3 100644 --- a/tests/test_decoder.c +++ b/tests/test_decoder.c @@ -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; diff --git a/tools/coverage_cli.c b/tools/coverage_cli.c index 977c901..6bafa26 100644 --- a/tools/coverage_cli.c +++ b/tools/coverage_cli.c @@ -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) { diff --git a/tools/decoder_cli.c b/tools/decoder_cli.c index 0cf6c35..472848f 100644 --- a/tools/decoder_cli.c +++ b/tools/decoder_cli.c @@ -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");