Debian Codegraph @

src/fcml_decoding_tree.c

raw
1 /*
2 * FCML - Free Code Manipulation Library.
3 * Copyright (C) 2010-2020 Slawomir Wojtasiak
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 #include <assert.h>
21
22 #include "fcml_env_int.h"
23 #include "fcml_decoding_tree.h"
24 #include "fcml_def.h"
25
26 /*********************************
27 * Disassembling tree support. *
28 *********************************/
29
30 #define FCML_IDF_REG_FIELD_SIZE 3
31 #define FCML_IDF_CONDITION_FIELD_SIZE 4
32 #define FCML_IDF_REG_FIELD_POS 0
33
34 typedef struct fcml_ist_dt_dts_opcodes {
35 fcml_uint8_t opcode_bytes[FCML_OPCODES_NUM];
36 } fcml_ist_dt_dts_opcodes;
37
38 typedef fcml_ceh_error (*fcml_ifp_dt_dts_opcode_callback)(
39 fcml_st_dt_decoding_tree *dec_tree,
40 fcml_st_def_instruction_desc *instruction_desc,
41 fcml_st_def_addr_mode_desc *opcode_desc,
42 fcml_ist_dt_dts_opcodes *opcodes);
43
44 /* Sets opcode field into a opcode byte. */
45 static fcml_uint8_t set_opcode_byte_field(fcml_uint8_t opcode_byte,
46 int opcode_flags_pos, int field_size, fcml_uint8_t field_value) {
47
48 /* Mask for opcode field.*/
49 fcml_uint8_t bit_mask = 0x00;
50 int i = 0;
51 for (i = 0; i < field_size; i++) {
52 bit_mask <<= 1;
53 bit_mask |= 0x01;
54 }
55 bit_mask = ~bit_mask;
56
57 /* Preparing opcode with opcode field set. */
58 return (opcode_byte & bit_mask) | (field_value << opcode_flags_pos);
59 }
60
61 static fcml_ceh_error default_opcode_callback(
62 fcml_st_dt_decoding_tree *dec_tree,
63 fcml_st_def_instruction_desc *instruction_desc,
64 fcml_st_def_addr_mode_desc *opcode_desc,
65 fcml_ist_dt_dts_opcodes *opcodes) {
66
67 fcml_st_dt_diss_tree_element **current_elements = &(dec_tree->opcode[0]);
68 fcml_st_dt_diss_tree_element *element = NULL;
69
70 /* Looking for a node in the instruction tree where new
71 * instruction should be placed. If there is no appropriate
72 * node a new one is allocated.
73 */
74 int opcode_num = FCML_DEF_OPCODE_FLAGS_OPCODE_NUM(
75 opcode_desc->opcode_flags);
76 for (int i = 0; i < opcode_num; i++) {
77 fcml_uint8_t opcode = opcodes->opcode_bytes[i];
78 element = current_elements[opcode];
79 if (!element) {
80 element = FCML_ENV_ALLOC_ST(fcml_st_dt_diss_tree_element);
81 if (!element) {
82 return FCML_CEH_GEC_OUT_OF_MEMORY;
83 }
84 current_elements[opcode] = element;
85 }
86 /* Get next level of opcodes for the next loop.*/
87 current_elements = &(element->opcodes[0]);
88 }
89
90 if (!element->instruction_decoding_defs) {
91 element->instruction_decoding_defs = fcml_fn_coll_list_alloc();
92 if (!element->instruction_decoding_defs) {
93 return FCML_CEH_GEC_OUT_OF_MEMORY;
94 }
95 }
96
97 /* Prepare instruction decoding.*/
98 return dec_tree->prep_callback(dec_tree->dialect_context, element,
99 instruction_desc, opcode_desc);
100 }
101
102 /* Function used to calculate all instruction opcode bytes using recursion. */
103 static fcml_ceh_error handle_next_opcode_byte(
104 fcml_st_dt_decoding_tree *dec_tree,
105 fcml_st_def_instruction_desc *instruction_desc,
106 fcml_st_def_addr_mode_desc *opcode_desc,
107 fcml_ist_dt_dts_opcodes *opcodes,
108 fcml_ifp_dt_dts_opcode_callback callback, int opcode_bytes_count,
109 int opcode_byte_num, int primary_opcode_byte_num) {
110
111 fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR;
112
113 /* Get next opcode byte from instruction.*/
114 if (opcode_byte_num == opcode_bytes_count) {
115 /* There are no more opcode bytes available.*/
116 return callback(dec_tree, instruction_desc, opcode_desc, opcodes);
117 }
118
119 fcml_uint8_t opcode_byte = opcode_desc->opcode[opcode_byte_num];
120
121 /* Check if this is an opcode with opcode fields.*/
122 if (opcode_byte_num == primary_opcode_byte_num) {
123
124 fcml_uint32_t opcode_flags = opcode_desc->opcode_flags;
125
126 int opcode_flags_pos = FCML_DEF_OPCODE_FLAGS_POS(opcode_flags);
127
128 /* Opcode field: REG and TTTN.*/
129 if ( FCML_DEF_OPCODE_FLAGS_OPCODE_FIELD_REG(
130 opcode_flags) || FCML_DEF_OPCODE_FLAGS_OPCODE_FIELD_TTTN( opcode_flags )) {
131
132 int number_of_opcodes = 0;
133 int field_size = 0;
134 if (FCML_DEF_OPCODE_FLAGS_OPCODE_FIELD_REG(opcode_flags)) {
135 number_of_opcodes = FCML_REG_FIELD_NUMBER_OF_REGISTERS;
136 field_size = FCML_IDF_REG_FIELD_SIZE;
137 } else if (FCML_DEF_OPCODE_FLAGS_OPCODE_FIELD_TTTN(opcode_flags)) {
138 number_of_opcodes = FCML_REG_FIELD_NUMBER_OF_CONDITIONS;
139 field_size = FCML_IDF_CONDITION_FIELD_SIZE;
140 }
141
142 int i;
143 for (i = 0; i < number_of_opcodes && !error; i++) {
144 /* Prepare opcode byte with register/condition field.*/
145 fcml_uint8_t encoded_opcode_byte =
146 set_opcode_byte_field(opcode_byte,
147 opcode_flags_pos, field_size, (fcml_uint8_t) i);
148 opcodes->opcode_bytes[opcode_byte_num] = encoded_opcode_byte;
149 /* Handle next opcode byte.*/
150 error = handle_next_opcode_byte(dec_tree,
151 instruction_desc, opcode_desc, opcodes, callback,
152 opcode_bytes_count, opcode_byte_num + 1,
153 primary_opcode_byte_num);
154 }
155
156 return error;
157 }
158 }
159
160 /* This is a plain opcode byte that shouldn't be modified in any way.*/
161 opcodes->opcode_bytes[opcode_byte_num] = opcode_byte;
162 /* Handle next opcode byte.*/
163 return handle_next_opcode_byte(dec_tree, instruction_desc,
164 opcode_desc, opcodes, callback, opcode_bytes_count,
165 opcode_byte_num + 1, primary_opcode_byte_num);
166 }
167
168 static fcml_ceh_error iterate_through_all_opcodes(
169 fcml_st_dt_decoding_tree *dec_tree,
170 fcml_st_def_instruction_desc *instruction_desc,
171 fcml_st_def_addr_mode_desc *opcode_desc,
172 fcml_ifp_dt_dts_opcode_callback callback) {
173
174 /* Number of opcode bytes.*/
175 int opcode_bytes_count = FCML_DEF_OPCODE_FLAGS_OPCODE_NUM(
176 opcode_desc->opcode_flags);
177
178 /* Opcode byte with potential flags.*/
179 int primary_opcode_byte_num = FCML_DEF_OPCODE_FLAGS_PRIMARY_OPCODE(
180 opcode_desc->opcode_flags);
181
182 assert(primary_opcode_byte_num < opcode_bytes_count);
183
184 fcml_ist_dt_dts_opcodes opcode = { { 0 } };
185
186 /* Handle all opcode bytes.*/
187 return handle_next_opcode_byte(dec_tree, instruction_desc,
188 opcode_desc, &opcode, callback, opcode_bytes_count, 0,
189 primary_opcode_byte_num);
190 }
191
192 static int update_disassembling_tree(
193 fcml_st_def_instruction_desc *instruction_desc_src,
194 fcml_st_dt_decoding_tree *dec_tree) {
195
196 fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR;
197
198 /* Get instruction description.*/
199 int instruction_index = 0;
200 while (instruction_desc_src[instruction_index].mnemonic != NULL && !error) {
201 struct fcml_st_def_instruction_desc *instruction_desc =
202 &instruction_desc_src[instruction_index++];
203
204 /* Get description of the specific instruction form.*/
205 int opcode_index = 0;
206 for (opcode_index = 0;
207 opcode_index < instruction_desc->opcode_desc_count;
208 opcode_index++) {
209 struct fcml_st_def_addr_mode_desc *opcode_desc =
210 &(instruction_desc->addr_modes[opcode_index]);
211
212 /* Iterate through all opcode combinations for this instruction form.*/
213 int error = iterate_through_all_opcodes(dec_tree,
214 instruction_desc, opcode_desc,
215 &default_opcode_callback);
216 if (error) {
217 break;
218 }
219 }
220 }
221
222 return error;
223 }
224
225 static void free_inst_def_element_handler(fcml_ptr item_value, fcml_ptr args) {
226 fcml_st_dt_decoding_tree *tree = (fcml_st_dt_decoding_tree*) args;
227 if (tree->disp_callback) {
228 tree->disp_callback(tree->dialect_context, item_value);
229 }
230 }
231
232 static void tree_element_free(fcml_st_dialect_context_int *dialect_context,
233 fcml_st_dt_decoding_tree *tree, fcml_st_dt_diss_tree_element *element) {
234
235 /* Free all child elements.*/
236 for (int i = 0; i < FCML_DT_TREE_OPCODE_ARRAY_SIZE; i++) {
237 fcml_st_dt_diss_tree_element *child_element = element->opcodes[i];
238 if (child_element) {
239 tree_element_free(dialect_context, tree, child_element);
240 }
241 }
242 /* Free element itself.*/
243 fcml_st_coll_list *instruction_decoding_defs =
244 element->instruction_decoding_defs;
245 if (instruction_decoding_defs) {
246 fcml_fn_coll_list_free(instruction_decoding_defs,
247 &free_inst_def_element_handler,
248 tree);
249 }
250 fcml_fn_env_memory_free(element);
251 }
252
253 fcml_ceh_error fcml_fn_dt_dts_tree_init(
254 fcml_st_dialect_context_int *dialect_context,
255 fcml_st_dt_decoding_tree **tree,
256 fcml_fp_dt_prep_inst_def_callback prepare_callback,
257 fcml_fp_dt_disp_inst_def_callback dispose_callback) {
258 fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR;
259
260 FCML_ENV_ALLOC_CLEAR(dec_tree, fcml_st_dt_decoding_tree);
261 if (dec_tree) {
262 dec_tree->dialect_context = dialect_context;
263 dec_tree->prep_callback = prepare_callback;
264 dec_tree->disp_callback = dispose_callback;
265 error = update_disassembling_tree(
266 fcml_ext_instructions_def, dec_tree);
267 if (!error) {
268 *tree = dec_tree;
269 } else {
270 fcml_fn_dt_dts_tree_free(dialect_context, dec_tree);
271 }
272 } else {
273 error = FCML_CEH_GEC_OUT_OF_MEMORY;
274 }
275
276 return error;
277 }
278
279 void fcml_fn_dt_dts_tree_free(fcml_st_dialect_context_int *dialect_context,
280 fcml_st_dt_decoding_tree *tree) {
281 int i = 0;
282 /* Free all child elements.*/
283 for (i = 0; i < FCML_DT_TREE_OPCODE_ARRAY_SIZE; i++) {
284 fcml_st_dt_diss_tree_element *element = tree->opcode[i];
285 if (element) {
286 tree_element_free(dialect_context, tree, element);
287 }
288 }
289 /* Free tree.*/
290 fcml_fn_env_memory_free(tree);
291 }
292