Skip to content

Commit a0ed438

Browse files
committed
jit: Implement arm32 instruction decoding logic
Introduces the core Arm32 decoder, including the instruction parsing mechanism and an initial set of defined instructions. Signed-off-by: Ronald Caesar <github43132@proton.me>
1 parent 2e2b5df commit a0ed438

File tree

5 files changed

+308
-337
lines changed

5 files changed

+308
-337
lines changed

CMakeLists.txt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ add_executable(Pound
9797

9898
add_subdirectory(3rd_Party)
9999
add_subdirectory(src/common)
100-
add_subdirectory(src/frontend)
101100
add_subdirectory(src/host)
102101
add_subdirectory(src/jit)
103102
add_subdirectory(src/pvm)
@@ -110,7 +109,7 @@ add_subdirectory(src/targets/switch1/hardware)
110109
include(TestBigEndian)
111110
TEST_BIG_ENDIAN(WORDS_BIGENDIAN)
112111

113-
list(APPEND POUND_PROJECT_TARGETS common frontend host pvm)
112+
list(APPEND POUND_PROJECT_TARGETS common host pvm)
114113
foreach(TARGET ${POUND_PROJECT_TARGETS})
115114
# Apply Endianness definitions to all our targets.
116115
if(WORDS_BIGENDIAN)
@@ -149,7 +148,6 @@ set_property(TARGET Pound PROPERTY CMAKE_INTERPROCEDURAL_OPTIMIZATION_RELEASE TR
149148

150149
target_link_libraries(Pound PRIVATE
151150
common
152-
frontend
153151
host
154152
jit
155153
pvm

src/jit/decoder/arm32.cpp

Lines changed: 33 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,16 @@
77

88
namespace pound::jit::decoder
99
{
10-
/* Increase value as more instructions get implemented */
11-
#define INSTRUCTION_ARRAY_CAPACITY 4
12-
arm32_decoder_t g_arm32_decoder = {};
10+
#define INSTRUCTION_ARRAY_CAPACITY 261
11+
12+
#define HASH_TABLE_INVALID_INDEX 0xFFFF
1313

1414
/*
1515
* ============================================================================
1616
* Foward Declarations
1717
* ============================================================================
1818
*/
19-
void arm32_add_instruction(arm32_decoder_t* decoder, const char* name, const char* bitstring, arm32_handler_fn handler);
20-
void arm32_ADD_imm_handler(arm32_decoder_t* decoder, arm32_instruction_t instruction);
19+
void arm32_add_instruction(arm32_decoder_t* decoder, const char* name, const char* bitstring);
2120

2221
void arm32_parse_bitstring(const char* bitstring, uint32_t* mask, uint32_t* expected);
2322
void arm32_grow_instructions_array(arm32_decoder_t* decoder, size_t new_capacity);
@@ -38,7 +37,7 @@ void arm32_init(pound::host::memory::arena_t allocator, arm32_decoder_t* decoder
3837
/* Setup Instructions array.*/
3938
size_t instructions_array_size = INSTRUCTION_ARRAY_CAPACITY * sizeof(arm32_instruction_info_t);
4039
PVM_ASSERT(instructions_array_size <= decoder->allocator.capacity);
41-
LOG_TRACE("Growing instructions array to %d bytes", instructions_array_size);
40+
LOG_TRACE("Allocated %d bytes to instructions array", instructions_array_size);
4241

4342
void* new_ptr = pound::host::memory::arena_allocate(&decoder->allocator, instructions_array_size);
4443
PVM_ASSERT(nullptr != new_ptr);
@@ -47,57 +46,55 @@ void arm32_init(pound::host::memory::arena_t allocator, arm32_decoder_t* decoder
4746
decoder->instruction_capacity = INSTRUCTION_ARRAY_CAPACITY;
4847

4948
/* Add all Arm32 instructions */
50-
#define INST(fn, name, bitstring) arm32_add_instruction(decoder, name, bitstring, &arm32_##fn##_handler);
49+
#define INST(fn, name, bitstring) arm32_add_instruction(decoder, name, bitstring);
5150
#include "./arm32.inc"
5251
#undef INST
5352
}
5453

55-
void arm32_add_instruction(arm32_decoder_t* decoder, const char* name, const char* bitstring, arm32_handler_fn handler)
54+
arm32_instruction_info_t* arm32_decode(arm32_decoder_t* decoder, uint32_t instruction)
55+
{
56+
for (size_t i = 0; i < decoder->instruction_count; ++i)
57+
{
58+
arm32_instruction_info_t* info = &decoder->instructions[i];
59+
if ((instruction & info->mask) == info->expected)
60+
{
61+
LOG_TRACE("Instruction found for 0x%08X: %s", instruction, info->name);
62+
return info;
63+
}
64+
}
65+
PVM_ASSERT_MSG(false, "No instruction found for 0x%08X", instruction);
66+
}
67+
68+
/*
69+
* ============================================================================
70+
* Private Functions
71+
* ============================================================================
72+
*/
73+
74+
void arm32_add_instruction(arm32_decoder_t* decoder, const char* name, const char* bitstring)
5675
{
5776
PVM_ASSERT(nullptr != decoder);
5877
PVM_ASSERT(nullptr != decoder->allocator.data);
5978
PVM_ASSERT(nullptr != name);
6079
PVM_ASSERT(nullptr != bitstring);
6180
PVM_ASSERT(decoder->instruction_count < decoder->instruction_capacity);
6281

63-
arm32_opcode_t mask = 0;
64-
arm32_opcode_t expected = 0;
82+
uint32_t mask = 0;
83+
uint32_t expected = 0;
6584
arm32_parse_bitstring(bitstring, &mask, &expected);
6685

6786
arm32_instruction_info_t* info = &decoder->instructions[decoder->instruction_count];
87+
PVM_ASSERT(nullptr != info);
6888
info->name = name;
6989
info->mask = mask;
7090
info->expected = expected;
71-
info->handler = handler;
72-
73-
/* Calculate priority based on number of fixed bits. */
74-
info->priority = 0;
75-
for (int i = 0; i < 32; ++i)
76-
{
77-
if ((mask >> i) & 1)
78-
{
79-
++info->priority;
80-
}
81-
}
8291

8392
++decoder->instruction_count;
84-
LOG_TRACE("========================================");
93+
8594
LOG_TRACE("Instruction Registered: %s", info->name);
8695
LOG_TRACE("Mask: 0x%08X", info->mask);
8796
LOG_TRACE("Expected: 0x%08X", info->expected);
88-
LOG_TRACE("Priority: %d", info->priority);
89-
LOG_TRACE("========================================");
90-
91-
92-
/* TODO(GloriousTacoo:jit): Add instruction to lookup table. */
9397
}
94-
void arm32_ADD_imm_handler(arm32_decoder_t* decoder, arm32_instruction_t instruction) {}
95-
96-
/*
97-
* ============================================================================
98-
* Private Functions
99-
* ============================================================================
100-
*/
10198

10299
void arm32_parse_bitstring(const char* bitstring, uint32_t* mask, uint32_t* expected)
103100
{
@@ -109,7 +106,7 @@ void arm32_parse_bitstring(const char* bitstring, uint32_t* mask, uint32_t* expe
109106
*mask = 0;
110107
*expected = 0;
111108
uint8_t instruction_size_bits = 32;
112-
for (int i = 0; (i < instruction_size_bits) && (bitstring[i] != '\0'); ++i)
109+
for (unsigned int i = 0; (i < instruction_size_bits) && (bitstring[i] != '\0'); ++i)
113110
{
114111
uint32_t bit_position = 31 - i;
115112
switch (bitstring[i])
@@ -120,19 +117,9 @@ void arm32_parse_bitstring(const char* bitstring, uint32_t* mask, uint32_t* expe
120117
case '1':
121118
*mask |= (1U << bit_position);
122119
*expected |= (1U << bit_position);
123-
break;
124-
case 'c':
125-
case 'n':
126-
case 'd':
127-
case 'r':
128-
case 'v':
129-
case 's':
130-
case 'S':
131-
break;
132120
default:
133-
PVM_ASSERT_MSG(false, "Invalid bitstring character: %c", bitstring[i]);
121+
break;
134122
}
135123
}
136124
}
137-
138125
} // namespace pound::jit::decoder

src/jit/decoder/arm32.h

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,14 @@
77

88
namespace pound::jit::decoder
99
{
10-
typedef uint32_t arm32_opcode_t;
11-
typedef uint32_t arm32_instruction_t;
12-
1310
typedef struct arm32_decoder arm32_decoder_t;
14-
typedef void (*arm32_handler_fn)(arm32_decoder_t* decoder, arm32_instruction_t instruction);
11+
typedef void (*arm32_handler_fn)(arm32_decoder_t* decoder, uint32_t instruction);
1512

1613
typedef struct
1714
{
1815
const char* name;
19-
arm32_opcode_t mask;
20-
arm32_opcode_t expected;
21-
arm32_handler_fn handler;
22-
23-
/* Use to order instructions in the lookup table. The more specific
24-
* instructions are checked first */
25-
uint8_t priority;
16+
uint32_t mask;
17+
uint32_t expected;
2618
} arm32_instruction_info_t;
2719

2820
struct arm32_decoder
@@ -31,23 +23,11 @@ struct arm32_decoder
3123
arm32_instruction_info_t* instructions;
3224
size_t instruction_count;
3325
size_t instruction_capacity;
34-
35-
struct
36-
{
37-
arm32_instruction_info_t** bucket;
38-
size_t count;
39-
size_t capacity;
40-
} lookup_table[4096]; /* 2^12 entries. */
4126
};
4227

4328
extern arm32_decoder_t g_arm32_decoder;
4429

4530
void arm32_init(pound::host::memory::arena_t allocator, arm32_decoder_t* decoder);
46-
47-
void arm32_add_instruction(arm32_decoder_t* decoder, const char* name, arm32_opcode_t mask, arm32_opcode_t expected,
48-
arm32_handler_fn handler);
49-
void arm32_ADD_imm_handler(arm32_decoder_t* decoder, arm32_instruction_t instruction);
50-
51-
52-
} // namespace pound::jit::decoder
53-
#endif // POUND_JIT_DECODER_ARM32_H
31+
arm32_instruction_info_t* arm32_decode(arm32_decoder_t* decoder, uint32_t instruction);
32+
}
33+
#endif // POUND_JIT_DECODER_ARM32_H

0 commit comments

Comments
 (0)