77
88namespace 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
2221void arm32_parse_bitstring (const char * bitstring, uint32_t * mask, uint32_t * expected);
2322void 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
10299void 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
0 commit comments