jit: Add Barr C code standard

Signed-off-by: Ronald Caesar <github43132@proton.me>
This commit is contained in:
Ronald Caesar 2025-10-26 13:59:48 -04:00
parent a543f78b77
commit 1c3b730899
No known key found for this signature in database
GPG key ID: 04307C401999C596
5 changed files with 1628 additions and 136 deletions

View file

@ -5,59 +5,69 @@
#define LOG_MODULE "jit"
#include "common/logging.h"
namespace pound::jit::decoder
{
namespace pound::jit::decoder {
#define INSTRUCTION_ARRAY_CAPACITY 261
#define HASH_TABLE_INVALID_INDEX 0xFFFF
#define INSTRUCTION_BITSTRING_LENGTH 32
/*
* ============================================================================
* Foward Declarations
* ============================================================================
*/
static void arm32_add_instruction(arm32_decoder_t* decoder, const char* name, const char* bitstring);
static void arm32_parse_bitstring(const char* bitstring, uint32_t* mask, uint32_t* expected);
static void arm32_add_instruction(arm32_decoder_t *p_decoder,
const char *p_name,
const char *p_bitstring);
static void arm32_parse_bitstring(const char *p_bitstring,
uint32_t *p_mask,
uint32_t *p_expected);
/*
* ============================================================================
* Public Functions
* ============================================================================
*/
void arm32_init(pound::host::memory::arena_t allocator, arm32_decoder_t* decoder)
void
arm32_init (pound::host::memory::arena_t allocator, arm32_decoder_t *p_decoder)
{
PVM_ASSERT(nullptr != decoder);
PVM_ASSERT(nullptr != p_decoder);
PVM_ASSERT(nullptr != allocator.data);
(void)memset(decoder, 0, sizeof(arm32_decoder_t));
decoder->allocator = allocator;
(void)memset(p_decoder, 0, sizeof(arm32_decoder_t));
p_decoder->allocator = allocator;
/* Setup Instructions array.*/
size_t instructions_array_size = INSTRUCTION_ARRAY_CAPACITY * sizeof(arm32_instruction_info_t);
PVM_ASSERT(instructions_array_size <= decoder->allocator.capacity);
LOG_TRACE("Allocated %d bytes to instructions array", instructions_array_size);
size_t instructions_array_size
= INSTRUCTION_ARRAY_CAPACITY * sizeof(arm32_instruction_info_t);
PVM_ASSERT(instructions_array_size <= p_decoder->allocator.capacity);
LOG_TRACE("Allocated %d bytes to instructions array",
instructions_array_size);
void* new_ptr = pound::host::memory::arena_allocate(&decoder->allocator, instructions_array_size);
PVM_ASSERT(nullptr != new_ptr);
void *p_memory = pound::host::memory::arena_allocate(
&p_decoder->allocator, instructions_array_size);
PVM_ASSERT(nullptr != p_memory);
decoder->instructions = (arm32_instruction_info_t*)new_ptr;
decoder->instruction_capacity = INSTRUCTION_ARRAY_CAPACITY;
p_decoder->p_instructions = (arm32_instruction_info_t *)p_memory;
p_decoder->instruction_capacity = INSTRUCTION_ARRAY_CAPACITY;
/* Add all Arm32 instructions */
#define INST(fn, name, bitstring) arm32_add_instruction(decoder, name, bitstring);
#define INST(fn, name, bitstring) \
arm32_add_instruction(p_decoder, name, bitstring);
#include "./arm32.inc"
#undef INST
}
arm32_instruction_info_t* arm32_decode(arm32_decoder_t* decoder, uint32_t instruction)
arm32_instruction_info_t *
arm32_decode (arm32_decoder_t *p_decoder, uint32_t instruction)
{
for (size_t i = 0; i < decoder->instruction_count; ++i)
for (size_t i = 0; i < p_decoder->instruction_count; ++i)
{
arm32_instruction_info_t* info = &decoder->instructions[i];
if ((instruction & info->mask) == info->expected)
arm32_instruction_info_t *p_info = &p_decoder->p_instructions[i];
if ((instruction & p_info->mask) == p_info->expected)
{
LOG_TRACE("Instruction found for 0x%08X: %s", instruction, info->name);
return info;
LOG_TRACE("Instruction found for 0x%08X: %s",
instruction,
p_info->p_name);
return p_info;
}
}
PVM_ASSERT_MSG(false, "No instruction found for 0x%08X", instruction);
@ -69,55 +79,64 @@ arm32_instruction_info_t* arm32_decode(arm32_decoder_t* decoder, uint32_t instru
* ============================================================================
*/
static void arm32_add_instruction(arm32_decoder_t* decoder, const char* name, const char* bitstring)
static void
arm32_add_instruction (arm32_decoder_t *p_decoder,
const char *p_name,
const char *p_bitstring)
{
PVM_ASSERT(nullptr != decoder);
PVM_ASSERT(nullptr != decoder->allocator.data);
PVM_ASSERT(nullptr != name);
PVM_ASSERT(nullptr != bitstring);
PVM_ASSERT(decoder->instruction_count < decoder->instruction_capacity);
PVM_ASSERT(nullptr != p_decoder);
PVM_ASSERT(nullptr != p_decoder->allocator.data);
PVM_ASSERT(nullptr != p_name);
PVM_ASSERT(nullptr != p_bitstring);
PVM_ASSERT(p_decoder->instruction_count < p_decoder->instruction_capacity);
uint32_t mask = 0;
uint32_t mask = 0;
uint32_t expected = 0;
arm32_parse_bitstring(bitstring, &mask, &expected);
arm32_parse_bitstring(p_bitstring, &mask, &expected);
arm32_instruction_info_t* info = &decoder->instructions[decoder->instruction_count];
PVM_ASSERT(nullptr != info);
info->name = name;
info->mask = mask;
info->expected = expected;
arm32_instruction_info_t *p_info
= &p_decoder->p_instructions[p_decoder->instruction_count];
PVM_ASSERT(nullptr != p_info);
p_info->p_name = p_name;
p_info->mask = mask;
p_info->expected = expected;
++decoder->instruction_count;
++p_decoder->instruction_count;
LOG_TRACE("Instruction Registered: %s", info->name);
LOG_TRACE("Mask: 0x%08X", info->mask);
LOG_TRACE("Expected: 0x%08X", info->expected);
LOG_TRACE("Instruction Registered: %s", p_info->p_name);
LOG_TRACE("Mask: 0x%08X", p_info->mask);
LOG_TRACE("Expected: 0x%08X", p_info->expected);
}
static void arm32_parse_bitstring(const char* bitstring, uint32_t* mask, uint32_t* expected)
static void
arm32_parse_bitstring (const char *p_bitstring,
uint32_t *p_mask,
uint32_t *p_expected)
{
PVM_ASSERT(nullptr != bitstring);
PVM_ASSERT(nullptr != mask);
PVM_ASSERT(nullptr != expected);
PVM_ASSERT(32 == strlen(bitstring));
PVM_ASSERT(nullptr != p_bitstring);
PVM_ASSERT(nullptr != p_mask);
PVM_ASSERT(nullptr != p_expected);
PVM_ASSERT(INSTRUCTION_BITSTRING_LENGTH == strlen(p_bitstring));
*mask = 0;
*expected = 0;
*p_mask = 0;
*p_expected = 0;
uint8_t instruction_size_bits = 32;
for (unsigned int i = 0; (i < instruction_size_bits) && (bitstring[i] != '\0'); ++i)
for (unsigned int i = 0;
(i < instruction_size_bits) && (p_bitstring[i] != '\0');
++i)
{
uint32_t bit_position = 31 - i;
switch (bitstring[i])
switch (p_bitstring[i])
{
case '0':
*mask |= (1U << bit_position);
*p_mask |= (1U << bit_position);
break;
case '1':
*mask |= (1U << bit_position);
*expected |= (1U << bit_position);
*p_mask |= (1U << bit_position);
*p_expected |= (1U << bit_position);
default:
break;
}
}
}
} // namespace pound::jit::decoder
} // namespace pound::jit::p_decoder