Debian Codegraph @

src/fcml_choosers.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 <stddef.h>
21
22 #include <fcml_assembler.h>
23 #include <fcml_choosers.h>
24
25 #include "fcml_coll.h"
26
27 struct instruction_holder {
28 fcml_ptr instruction;
29 fcml_usize code_length;
30 };
31
32 fcml_ptr LIB_CALL fcml_fn_asm_no_instruction_chooser(
33 fcml_st_chooser_context *context) {
34 return NULL;
35 }
36
37 static void fcml_ifn_replace_shortest(struct instruction_holder *shortest,
38 fcml_ptr instruction, fcml_st_instruction_code *instruction_code) {
39 if (!shortest->instruction ||
40 instruction_code->code_length < shortest->code_length) {
41 shortest->instruction = instruction;
42 shortest->code_length = instruction_code->code_length;
43 }
44 }
45
46 fcml_ptr LIB_CALL fcml_fn_asm_default_instruction_chooser(
47 fcml_st_chooser_context *context) {
48 fcml_st_instruction_code instruction_code;
49 fcml_ptr instruction = context->instruction;
50
51 struct instruction_holder shortest = {0};
52 struct instruction_holder shortest_no_override = {0};
53
54 while (instruction) {
55 context->extract(instruction, &instruction_code);
56
57 /* Keep the shortest GPI instruction which doesn't override anything. */
58 if ((instruction_code.details.instruction_group & FCML_AMT_GPI) != 0 &&
59 !instruction_code.details.osa_override &&
60 !instruction_code.details.asa_override) {
61 fcml_ifn_replace_shortest(&shortest_no_override, instruction,
62 &instruction_code);
63 }
64
65 fcml_ifn_replace_shortest(&shortest, instruction, &instruction_code);
66
67 instruction = context->next(instruction);
68 }
69
70 /* Return the shortest instruction which doesn't override ASA and OSA
71 if there is any. */
72 if (shortest_no_override.instruction != NULL) {
73 return shortest_no_override.instruction;
74 }
75
76 return shortest.instruction;
77 }