decoder: Implement linear search decoder

This will be replace with a hash table but I need a working
decoding implementation right now.

Signed-off-by: Ronald Caesar <github43132@proton.me>
This commit is contained in:
Ronald Caesar
2025-12-08 18:24:18 -04:00
parent 7c62fb2d21
commit e0c76b5361
5 changed files with 20 additions and 29 deletions

View File

@@ -1,19 +1,19 @@
#include "decoder.h"
#include "decoder_table_gen.h"
#include <stddef.h>
bal_decoder_instruction_metadata_t *
const bal_decoder_instruction_metadata_t *
bal_decoder_arm64_decode (const uint32_t instruction)
{
#if 0
/* Extract hash key: Bits [27:20] and [7:4] */
const uint32_t major = (instruction >> 20U) & 0xFFU;
const uint32_t minor = (instruction >> 4U) & 0xFU;
for (size_t i = 0; i < BAL_DECODER_ARM64_INSTRUCTIONS_SIZE; ++i)
{
const bal_decoder_instruction_metadata_t *metadata
= &g_bal_decoder_arm64_instructions[i];
const uint16_t index = (uint16_t)((major << 4U) | minor);
/*TODO: Implement hash table */
return NULL;
#endif
(void)instruction; // Bypass unused aprameter warning.
if ((instruction & metadata->mask) == metadata->expected)
{
return metadata;
}
}
return NULL;
}

View File

@@ -32,13 +32,13 @@ extern "C"
* @details 1 = significant bit, 0 = variable field (register,
* immediate, etc.).
*/
uint64_t mask;
uint32_t mask;
/*!
* @brief The expected value of the instruction after applying the mask.
* @details (instruction & mask) == expected.
*/
uint64_t expected;
uint32_t expected;
} bal_decoder_instruction_metadata_t;
/*!
@@ -56,7 +56,7 @@ extern "C"
* @post The returned pointer (if not null) points to static read-only
* memory.
*/
bal_decoder_instruction_metadata_t *bal_decoder_arm64_decode(
const bal_decoder_instruction_metadata_t *bal_decoder_arm64_decode(
const uint32_t instruction);
#ifdef __cplusplus

View File

@@ -1,7 +1,7 @@
/* Generated 2807 instructions */
#include "decoder_table_gen.h"
const bal_decoder_entry_t g_bal_decoder_arm64_instructions[BAL_DECODER_ARM64_INSTRUCTIONS_SIZE] = {
const bal_decoder_instruction_metadata_t g_bal_decoder_arm64_instructions[BAL_DECODER_ARM64_INSTRUCTIONS_SIZE] = {
{ "AUTIAZ", 0xFFFFFFFF, 0xD503239F },
{ "AUTIBZ", 0xFFFFFFFF, 0xD50323DF },
{ "DRPS", 0xFFFFFFFF, 0xD6BF03E0 },

View File

@@ -1,13 +1,7 @@
/* Generated header file */
#include "decoder.h"
#include <stdint.h>
#define BAL_DECODER_ARM64_INSTRUCTIONS_SIZE 2807
typedef struct
{
const char* mnemonic;
uint32_t mask;
uint32_t value;
} bal_decoder_entry_t;
extern const bal_decoder_entry_t g_bal_decoder_arm64_instructions[BAL_DECODER_ARM64_INSTRUCTIONS_SIZE];
extern const bal_decoder_instruction_metadata_t g_bal_decoder_arm64_instructions[BAL_DECODER_ARM64_INSTRUCTIONS_SIZE];

View File

@@ -215,14 +215,11 @@ if __name__ == "__main__":
with open(out_file_header, "w") as f:
f.write(f"/* Generated header file */\n")
f.write(f'#include "decoder.h"\n')
f.write("#include <stdint.h>\n\n")
f.write(f"#define {arm64_instructions_size_name} {len(all_instructions)}\n\n")
f.write(
"typedef struct \n{\n const char* mnemonic; \n uint32_t mask;\n uint32_t value;\n} bal_decoder_entry_t;\n\n"
)
f.write(
f"extern const bal_decoder_entry_t {arm64_global_instructions_array_name}[{arm64_instructions_size_name}];\n"
f"extern const bal_decoder_instruction_metadata_t {arm64_global_instructions_array_name}[{arm64_instructions_size_name}];\n"
)
print(f"Generated ARM decoder table header file -> {out_file_header}")
@@ -235,7 +232,7 @@ if __name__ == "__main__":
f.write(f"/* Generated {len(all_instructions)} instructions */\n")
f.write(f'#include "decoder_table_gen.h"\n\n')
f.write(
f"const bal_decoder_entry_t {arm64_global_instructions_array_name}[{arm64_instructions_size_name}] = {{\n"
f"const bal_decoder_instruction_metadata_t {arm64_global_instructions_array_name}[{arm64_instructions_size_name}] = {{\n"
)
for inst in all_instructions:
f.write(