Debian Codegraph @

src/fcml_mnemonic_parser.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 /*
21 * mm - Mnemonic for addressing mode using memory addressing.
22 * mr - Mnemonic for addressing mode without memory addressing.
23 * sb - Sets memory data size to one byte.
24 * sf - Sets memory data size to "supported_osa" which can be set using "oX"
25 * mnemonic attributes.
26 * oX (X=dwq) - Sets supported operand size attribute to 16, 32 or 64 bits.
27 * aX (X=dwq) - Sets supported address size attribute to 16, 32 or 64 bits.
28 * ts - Sets "shortcut" flags for mnemonic.
29 * tb - Sets "shortcut/classic" flag for mnemonic.
30 * pXX - Sets pseudo opcode value for mnemonic.
31 * dXX - Sets memory data size to specified number of bytes. It has greater
32 * priority than "sb" and "sf".
33 * l0 - Mnemonic for L flag set to 0.
34 * l1 - Mnemonic for L flag set to 1.
35 * Sxx - One byte instruction suffix, used by AMD 3DNow for instance.
36 *
37 */
38
39 #include "fcml_mnemonic_parser.h"
40
41 #include <stdlib.h>
42
43 #include <fcml_common.h>
44 #include <fcml_types.h>
45
46 enum parser_state {
47 FCML_MP_PS_MNEMONIC,
48 FCML_MP_PS_BETWEEN_MNEMONICS,
49 FCML_MP_PS_ATTRIBUTE_KEY,
50 FCML_MP_PS_ATTRIBUTE_VALUE
51 };
52
53 #define FCML_IDF_MP_BUFF_LEN 256
54
55 static void clean_mnemonic(fcml_st_mp_mnemonic *mnemonic) {
56 mnemonic->supported_osa = FCML_DS_UNDEF;
57 mnemonic->supported_asa = FCML_DS_UNDEF;
58 mnemonic->mnemonic = NULL;
59 mnemonic->pseudo_op.is_not_null = FCML_FALSE;
60 mnemonic->pseudo_op.value = 0x00;
61 mnemonic->memory_data.is_not_null = FCML_FALSE;
62 mnemonic->memory_data.value = 0x00;
63 mnemonic->is_shortcut = FCML_FALSE;
64 mnemonic->is_classic = FCML_TRUE;
65 mnemonic->is_byte_ds = FCML_FALSE;
66 mnemonic->is_full_ds = FCML_FALSE;
67 mnemonic->is_mode_mem_only = FCML_FALSE;
68 mnemonic->is_mode_reg_only = FCML_FALSE;
69 mnemonic->is_default = FCML_TRUE;
70 mnemonic->l.value = 0;
71 mnemonic->l.is_not_null = FCML_FALSE;
72 mnemonic->suffix.is_not_null = FCML_FALSE;
73 mnemonic->suffix.value = 0x00;
74 }
75
76 static fcml_ceh_error dup_mnemonic(fcml_st_mp_mnemonic *parsed_mnemonic,
77 fcml_st_coll_list *mnemonics, fcml_string mnemonic_buff, fcml_usize len) {
78
79 fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR;
80
81 /* Allocate space for new mnemonic.*/
82 fcml_st_mp_mnemonic *new_mnemonic =
83 (fcml_st_mp_mnemonic*) fcml_fn_env_memory_alloc_clear(
84 sizeof(fcml_st_mp_mnemonic));
85 if (new_mnemonic == NULL) {
86 return FCML_CEH_GEC_OUT_OF_MEMORY;
87 }
88
89 /* Prepare mnemonic string.*/
90 *new_mnemonic = *parsed_mnemonic;
91
92 new_mnemonic->mnemonic = fcml_fn_env_str_stralloc(len + 1);
93 if (new_mnemonic->mnemonic == NULL) {
94 fcml_fn_env_memory_free(new_mnemonic);
95 return FCML_CEH_GEC_OUT_OF_MEMORY;
96 } else {
97 fcml_fn_env_str_strncpy(new_mnemonic->mnemonic, mnemonic_buff, len);
98 }
99
100 /* Add mnemonic to list of already parsed mnemonics.*/
101 if (!fcml_fn_coll_list_add_back(mnemonics, new_mnemonic)) {
102 fcml_fn_env_str_strfree(new_mnemonic->mnemonic);
103 fcml_fn_env_memory_free(new_mnemonic);
104 error = FCML_CEH_GEC_OUT_OF_MEMORY;
105 }
106
107 return error;
108 }
109
110 static fcml_ceh_error parse_attribute_size_flag(fcml_char flag_code,
111 fcml_usize *flags) {
112 fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR;
113 switch (flag_code) {
114 case 'w':
115 *flags = FCML_DS_16;
116 break;
117 case 'd':
118 *flags = FCML_DS_32;
119 break;
120 case 'q':
121 *flags = FCML_DS_64;
122 break;
123 case '*':
124 *flags = FCML_DS_UNDEF;
125 break;
126 default:
127 error = FCML_CEH_GEC_INVALID_INPUT;
128 }
129 return error;
130 }
131
132 static fcml_ceh_error handle_attribute_value(fcml_char attr_key,
133 fcml_char *attr_value, int attr_value_len,
134 fcml_st_mp_mnemonic *mnemonic) {
135 fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR;
136 /* Handle attributes. */
137 switch (attr_key) {
138 case 'l':
139 if (attr_value[0] == '0') {
140 mnemonic->l.value = 0;
141 } else if (attr_value[0] == '1') {
142 mnemonic->l.value = 1;
143 } else if (attr_value[0] == '2') {
144 mnemonic->l.value = 2;
145 }
146 mnemonic->l.is_not_null = FCML_TRUE;
147 break;
148 case 'm':
149 if (attr_value[0] == 'm') {
150 mnemonic->is_mode_mem_only = FCML_TRUE;
151 } else if (attr_value[0] == 'r') {
152 mnemonic->is_mode_reg_only = FCML_TRUE;
153 }
154 break;
155 case 's':
156 if (attr_value[0] == 'b') {
157 mnemonic->is_byte_ds = FCML_TRUE;
158 } else if (attr_value[0] == 'f') {
159 mnemonic->is_full_ds = FCML_TRUE;
160 }
161 break;
162 case 'S':
163 if (attr_value_len == 2) {
164 unsigned long int val = strtoul(attr_value, NULL, 16);
165 mnemonic->suffix.value = (fcml_uint8_t) val;
166 mnemonic->suffix.is_not_null = FCML_TRUE;
167 } else {
168 error = FCML_CEH_GEC_INVALID_INPUT;
169 }
170 break;
171 case 't':
172 if (attr_value[0] == 's') {
173 mnemonic->is_shortcut = FCML_TRUE;
174 mnemonic->is_classic = FCML_FALSE;
175 } else if (attr_value[0] == 'b') {
176 mnemonic->is_shortcut = FCML_TRUE;
177 mnemonic->is_classic = FCML_TRUE;
178 } else {
179 /* Currently this attribute can be set to 's' only.*/
180 error = FCML_CEH_GEC_INVALID_INPUT;
181 }
182 break;
183 case 'o':
184 error = parse_attribute_size_flag(attr_value[0],
185 &mnemonic->supported_osa);
186 break;
187 case 'a':
188 error = parse_attribute_size_flag(attr_value[0],
189 &mnemonic->supported_asa);
190 break;
191 case 'd':
192 if (attr_value_len == 2) {
193 unsigned long int val = strtoul(attr_value, NULL, 16);
194 mnemonic->memory_data.value = (fcml_uint8_t) val;
195 mnemonic->memory_data.is_not_null = FCML_TRUE;
196 } else {
197 error = FCML_CEH_GEC_INVALID_INPUT;
198 }
199 break;
200 case 'p':
201 if (attr_value_len == 2) {
202 unsigned long int val = strtoul(attr_value, NULL, 16);
203 mnemonic->pseudo_op.value = (fcml_uint8_t) val;
204 mnemonic->pseudo_op.is_not_null = FCML_TRUE;
205 } else {
206 error = FCML_CEH_GEC_INVALID_INPUT;
207 }
208 break;
209 default:
210 /* Unknown attribute key.*/
211 error = FCML_CEH_GEC_INVALID_INPUT;
212 }
213 return error;
214 }
215
216 static fcml_bool is_attribute_size_supported(
217 fcml_usize supported_attribute_size, fcml_usize attribute_size) {
218 if (supported_attribute_size == FCML_DS_UNDEF) {
219 return FCML_TRUE;
220 }
221 return supported_attribute_size == attribute_size;
222 }
223
224 static void coll_list_action_free_mnemonic(fcml_ptr item_value, fcml_ptr args) {
225 if (item_value) {
226 fcml_st_mp_mnemonic *parsed_mnemonic = (fcml_st_mp_mnemonic*) item_value;
227 if (parsed_mnemonic->mnemonic) {
228 fcml_fn_env_str_strfree(parsed_mnemonic->mnemonic);
229 }
230 fcml_fn_env_memory_free(parsed_mnemonic);
231 }
232 }
233
234 fcml_ceh_error fcml_fn_mp_parse_mnemonics(fcml_string mnemonics_pattern,
235 fcml_st_mp_mnemonic_set **mnemonics_set) {
236
237 fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR;
238 fcml_char mnemonic_buff[FCML_IDF_MP_BUFF_LEN];
239 int mnemonic_index = 0;
240
241 fcml_st_mp_mnemonic_set *mnemonics =
242 (fcml_st_mp_mnemonic_set*) fcml_fn_env_memory_alloc_clear(
243 sizeof(fcml_st_mp_mnemonic_set));
244 if (!mnemonics) {
245 return FCML_CEH_GEC_OUT_OF_MEMORY;
246 }
247
248 mnemonics->mnemonics = fcml_fn_coll_list_alloc();
249 if (!mnemonics->mnemonics) {
250 fcml_fn_env_memory_free(mnemonics);
251 return FCML_CEH_GEC_OUT_OF_MEMORY;
252 }
253
254 fcml_usize len = fcml_fn_env_str_strlen(mnemonics_pattern);
255
256 fcml_st_mp_mnemonic mnemonic;
257
258 clean_mnemonic(&mnemonic);
259
260 enum parser_state state = FCML_MP_PS_MNEMONIC;
261 fcml_char attr_key;
262 int attr_value_index = 0;
263 fcml_char attr_value[6];
264 fcml_usize i;
265
266 /* Main loop of simple finite-state machine parser.*/
267 for (i = 0; i < len && !error; i++) {
268
269 fcml_char c = mnemonics_pattern[i];
270
271 if (state == FCML_MP_PS_MNEMONIC) {
272 switch (c) {
273 case '[':
274 if (mnemonic_index > 0) {
275 state = FCML_MP_PS_ATTRIBUTE_KEY;
276 mnemonic.is_default = FCML_FALSE;
277 } else {
278 /* empty mnemonic.*/
279 error = FCML_CEH_GEC_INVALID_INPUT;
280 }
281 break;
282 case ';':
283 if (mnemonic_index > 0) {
284 error = dup_mnemonic(&mnemonic, mnemonics->mnemonics,
285 mnemonic_buff, mnemonic_index);
286 } else {
287 /* empty mnemonic.*/
288 error = FCML_CEH_GEC_INVALID_INPUT;
289 }
290 clean_mnemonic(&mnemonic);
291 mnemonic_index = 0;
292 break;
293 default:
294 if (mnemonic_index < FCML_IDF_MP_BUFF_LEN
295 && (fcml_fn_env_is_alpha(c) || fcml_fn_env_is_digit(c)
296 || c == '_')) {
297 mnemonic_buff[mnemonic_index++] = c;
298 } else {
299 error = FCML_CEH_GEC_INVALID_INPUT;
300 }
301 break;
302 }
303 } else if (state == FCML_MP_PS_BETWEEN_MNEMONICS) {
304
305 if (c != ';') {
306 error = FCML_CEH_GEC_INVALID_INPUT;
307 break;
308 }
309
310 error = dup_mnemonic(&mnemonic, mnemonics->mnemonics, mnemonic_buff,
311 mnemonic_index);
312
313 clean_mnemonic(&mnemonic);
314 mnemonic_index = 0;
315
316 state = FCML_MP_PS_MNEMONIC;
317
318 } else {
319 switch (c) {
320 case ']':
321 if (state != FCML_MP_PS_ATTRIBUTE_VALUE
322 || attr_value_index == 0) {
323 error = FCML_CEH_GEC_INVALID_INPUT;
324 break;
325 }
326 /* Handle last mnemonic attribute.*/
327 error = handle_attribute_value(attr_key, attr_value,
328 attr_value_index, &mnemonic);
329 attr_value_index = 0;
330 state = FCML_MP_PS_BETWEEN_MNEMONICS;
331 break;
332 case ',':
333 if (state != FCML_MP_PS_ATTRIBUTE_VALUE
334 || attr_value_index == 0) {
335 error = FCML_CEH_GEC_INVALID_INPUT;
336 break;
337 }
338 error = handle_attribute_value(attr_key, attr_value,
339 attr_value_index, &mnemonic);
340 attr_value_index = 0;
341 state = FCML_MP_PS_ATTRIBUTE_KEY;
342 break;
343 default:
344 if (state != FCML_MP_PS_ATTRIBUTE_KEY
345 && state != FCML_MP_PS_ATTRIBUTE_VALUE) {
346 error = FCML_CEH_GEC_INVALID_INPUT;
347 break;
348 }
349 if (state == FCML_MP_PS_ATTRIBUTE_KEY) {
350 if (fcml_fn_env_is_alpha(c)) {
351 attr_key = c;
352 state = FCML_MP_PS_ATTRIBUTE_VALUE;
353 } else {
354 error = FCML_CEH_GEC_INVALID_INPUT;
355 }
356 } else if (state == FCML_MP_PS_ATTRIBUTE_VALUE) {
357 if ((fcml_fn_env_is_alpha(c) || fcml_fn_env_is_digit(c)
358 || c == '*') && attr_value_index < 5) {
359 attr_value[attr_value_index++] = c;
360 attr_value[attr_value_index] = '\0';
361 } else {
362 error = FCML_CEH_GEC_INVALID_INPUT;
363 }
364 }
365 }
366 }
367 }
368
369 if (state != FCML_MP_PS_MNEMONIC && state != FCML_MP_PS_BETWEEN_MNEMONICS) {
370 error = FCML_CEH_GEC_INVALID_INPUT;
371 }
372
373 if (!error && mnemonic_index) {
374 error = dup_mnemonic(&mnemonic, mnemonics->mnemonics, mnemonic_buff,
375 mnemonic_index);
376 }
377
378 if (error) {
379 fcml_fn_mp_free_mnemonics(mnemonics);
380 } else {
381 if (mnemonics->mnemonics->size) {
382 *mnemonics_set = mnemonics;
383 } else {
384 /* There is no mnemonic in the list.*/
385 fcml_fn_mp_free_mnemonics(mnemonics);
386 error = FCML_CEH_GEC_INVALID_INPUT;
387 }
388 }
389
390 return error;
391 }
392
393 fcml_st_mp_mnemonic* fcml_fn_mp_choose_mnemonic(
394 fcml_st_mp_mnemonic_set *mnemonics, fcml_st_mp_config *config) {
395
396 fcml_st_mp_mnemonic *chosen_mnemonic = NULL;
397
398 if (mnemonics->mnemonics) {
399
400 fcml_st_coll_list_element *next = mnemonics->mnemonics->head;
401
402 while (next) {
403
404 fcml_st_mp_mnemonic *mnemonic = (fcml_st_mp_mnemonic*) next->item;
405
406 /* Set default mnemonic only if there is no mnemonic yet.
407 * Default mnemonics can not have any attributes defined,
408 * so we do not need to check anything.
409 */
410 if (!chosen_mnemonic && !mnemonic->is_shortcut
411 && mnemonic->is_default) {
412 chosen_mnemonic = mnemonic;
413 }
414
415 /* Size attributes.*/
416 if (is_attribute_size_supported(mnemonic->supported_asa,
417 config->effective_asa)
418 && is_attribute_size_supported(
419 mnemonic->supported_osa, config->effective_osa)) {
420
421 /* Memory data size.*/
422 if (!mnemonic->memory_data.is_not_null
423 || (mnemonic->memory_data.value
424 == (config->memory_data_size / 8))) {
425
426 /* Suffix.*/
427 if ((mnemonic->suffix.is_not_null
428 == config->suffix.is_not_null)
429 && (!config->suffix.is_not_null
430 || (config->suffix.value
431 == mnemonic->suffix.value))) {
432
433 /* L flag.*/
434 if ((!config->l.is_not_null && !mnemonic->l.is_not_null)
435 || (config->l.value == mnemonic->l.value)) {
436
437 /* Shortcuts. Pseudo opcode mnemonic is also
438 * treated as a shortcut by disassembler.
439 */
440 if ((config->use_shortcut
441 && ((mnemonic->pseudo_op.is_not_null
442 && mnemonic->pseudo_op.value
443 == config->pseudo_opcode.value)
444 || mnemonic->is_shortcut))
445 || (!config->use_shortcut
446 && !mnemonic->pseudo_op.is_not_null
447 && (!mnemonic->is_shortcut
448 || mnemonic->is_classic))) {
449
450 /* Addressing mode: See "mm","mr"
451 * mnemonic attribute.
452 */
453 fcml_bool is_mode_ok =
454 ((!mnemonic->is_mode_mem_only
455 && !mnemonic->is_mode_reg_only)
456 || (mnemonic->is_mode_mem_only
457 && config->is_memory)
458 || (mnemonic->is_mode_reg_only
459 && !config->is_memory));
460 if (is_mode_ok) {
461
462 /* Default mnemonic can not be overridden
463 * by another default.
464 */
465 if (!chosen_mnemonic
466 || !mnemonic->is_default) {
467 chosen_mnemonic = mnemonic;
468 break;
469 }
470
471 }
472 }
473 }
474 }
475 }
476 }
477
478 next = next->next;
479 }
480 }
481 return chosen_mnemonic;
482
483 }
484
485 void fcml_fn_mp_free_mnemonics(fcml_st_mp_mnemonic_set *mnemonics) {
486 if (mnemonics) {
487 if (mnemonics->mnemonics) {
488 fcml_fn_coll_list_free(mnemonics->mnemonics,
489 &coll_list_action_free_mnemonic, NULL);
490 }
491 fcml_fn_env_memory_free(mnemonics);
492 }
493 }
494
495 fcml_usize fcml_fn_mp_l_to_vector_length(fcml_uint8_t l) {
496 fcml_usize vector_length = FCML_DS_UNDEF;
497 switch (l) {
498 case 0x00:
499 vector_length = FCML_DS_128;
500 break;
501 case 0x01:
502 vector_length = FCML_DS_256;
503 break;
504 case 0x02:
505 vector_length = FCML_DS_512;
506 break;
507 }
508 return vector_length;
509 }