@@ -9,15 +9,15 @@ namespace pound::jit::decoder
99{
1010/* Increase value as more instructions get implemented */
1111#define INSTRUCTION_ARRAY_CAPACITY 4
12- arm32_decoder_t g_arm32_decoder = {};
12+
13+ #define HASH_TABLE_INVALID_INDEX 0xFFFF
1314
1415/*
1516 * ============================================================================
1617 * Foward Declarations
1718 * ============================================================================
1819 */
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);
20+ void arm32_add_instruction (arm32_decoder_t * decoder, const char * name, const char * bitstring);
2121
2222void arm32_parse_bitstring (const char * bitstring, uint32_t * mask, uint32_t * expected);
2323void arm32_grow_instructions_array (arm32_decoder_t * decoder, size_t new_capacity);
@@ -38,7 +38,7 @@ void arm32_init(pound::host::memory::arena_t allocator, arm32_decoder_t* decoder
3838 /* Setup Instructions array.*/
3939 size_t instructions_array_size = INSTRUCTION_ARRAY_CAPACITY * sizeof (arm32_instruction_info_t );
4040 PVM_ASSERT (instructions_array_size <= decoder->allocator .capacity );
41- LOG_TRACE (" Growing instructions array to %d bytes " , instructions_array_size);
41+ LOG_TRACE (" Allocated %d bytes to instructions array " , instructions_array_size);
4242
4343 void * new_ptr = pound::host::memory::arena_allocate (&decoder->allocator , instructions_array_size);
4444 PVM_ASSERT (nullptr != new_ptr);
@@ -47,57 +47,55 @@ void arm32_init(pound::host::memory::arena_t allocator, arm32_decoder_t* decoder
4747 decoder->instruction_capacity = INSTRUCTION_ARRAY_CAPACITY;
4848
4949 /* Add all Arm32 instructions */
50- #define INST (fn, name, bitstring ) arm32_add_instruction(decoder, name, bitstring, &arm32_##fn##_handler );
50+ #define INST (fn, name, bitstring ) arm32_add_instruction(decoder, name, bitstring);
5151#include " ./arm32.inc"
5252#undef INST
5353}
5454
55- void arm32_add_instruction (arm32_decoder_t * decoder, const char * name, const char * bitstring, arm32_handler_fn handler)
55+ arm32_instruction_info_t * arm32_decode (arm32_decoder_t * decoder, uint32_t instruction)
56+ {
57+ for (size_t i = 0 ; i < decoder->instruction_count ; ++i)
58+ {
59+ arm32_instruction_info_t * info = &decoder->instructions [i];
60+ if ((instruction & info->mask ) == info->expected )
61+ {
62+ LOG_TRACE (" Instruction found for 0x%08X: %s" , instruction, info->name );
63+ return info;
64+ }
65+ }
66+ PVM_ASSERT_MSG (false , " No instruction found for 0x%08X" , instruction);
67+ }
68+
69+ /*
70+ * ============================================================================
71+ * Private Functions
72+ * ============================================================================
73+ */
74+
75+ void arm32_add_instruction (arm32_decoder_t * decoder, const char * name, const char * bitstring)
5676{
5777 PVM_ASSERT (nullptr != decoder);
5878 PVM_ASSERT (nullptr != decoder->allocator .data );
5979 PVM_ASSERT (nullptr != name);
6080 PVM_ASSERT (nullptr != bitstring);
6181 PVM_ASSERT (decoder->instruction_count < decoder->instruction_capacity );
6282
63- arm32_opcode_t mask = 0 ;
64- arm32_opcode_t expected = 0 ;
83+ uint32_t mask = 0 ;
84+ uint32_t expected = 0 ;
6585 arm32_parse_bitstring (bitstring, &mask, &expected);
6686
6787 arm32_instruction_info_t * info = &decoder->instructions [decoder->instruction_count ];
88+ PVM_ASSERT (nullptr != info);
6889 info->name = name;
6990 info->mask = mask;
7091 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- }
8292
8393 ++decoder->instruction_count ;
84- LOG_TRACE ( " ======================================== " );
94+
8595 LOG_TRACE (" Instruction Registered: %s" , info->name );
8696 LOG_TRACE (" Mask: 0x%08X" , info->mask );
8797 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. */
9398}
94- void arm32_ADD_imm_handler (arm32_decoder_t * decoder, arm32_instruction_t instruction) {}
95-
96- /*
97- * ============================================================================
98- * Private Functions
99- * ============================================================================
100- */
10199
102100void arm32_parse_bitstring (const char * bitstring, uint32_t * mask, uint32_t * expected)
103101{
@@ -109,7 +107,7 @@ void arm32_parse_bitstring(const char* bitstring, uint32_t* mask, uint32_t* expe
109107 *mask = 0 ;
110108 *expected = 0 ;
111109 uint8_t instruction_size_bits = 32 ;
112- for (int i = 0 ; (i < instruction_size_bits) && (bitstring[i] != ' \0 ' ); ++i)
110+ for (unsigned int i = 0 ; (i < instruction_size_bits) && (bitstring[i] != ' \0 ' ); ++i)
113111 {
114112 uint32_t bit_position = 31 - i;
115113 switch (bitstring[i])
@@ -134,5 +132,4 @@ void arm32_parse_bitstring(const char* bitstring, uint32_t* mask, uint32_t* expe
134132 }
135133 }
136134}
137-
138135} // namespace pound::jit::decoder
0 commit comments