Debian Codegraph @

src/fcml_optimizers.c

raw
1 /*
2 * FCML - Free Code Manipulation Library.
3 * Copyright (C) 2010-2024 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 "fcml_optimizers.h"
21
22 #include <fcml_types.h>
23 #include <fcml_errors.h>
24 #include <fcml_common.h>
25
26 #include "fcml_trace.h"
27
28 typedef struct opt_path_element {
29 fcml_flags flags;
30 fcml_usize attribute_size;
31 } opt_path_element;
32
33 static opt_path_element opt_16_32_bit_path_16[] = {
34 { FCML_EN_ASF_16, FCML_DS_16 },
35 { FCML_EN_ASF_32, FCML_DS_32 }
36 };
37
38 static opt_path_element opt_16_32_bit_path_32[] = {
39 { FCML_EN_ASF_32, FCML_DS_32 },
40 { FCML_EN_ASF_16, FCML_DS_16 }
41 };
42
43 static opt_path_element opt_64_bit_path_for_asa_64[] = {
44 { FCML_EN_ASF_64, FCML_DS_64 },
45 { FCML_EN_ASF_32, FCML_DS_32 }
46 };
47
48 static opt_path_element opt_64_bit_path_for_asa_32[] = {
49 { FCML_EN_ASF_32, FCML_DS_32 },
50 { FCML_EN_ASF_64, FCML_DS_64 }
51 };
52
53 static opt_path_element opt_64_bit_path_for_osa_16[] = {
54 { FCML_EN_ASF_16, FCML_DS_16 },
55 { FCML_EN_ASF_64, FCML_DS_64 },
56 { FCML_EN_ASF_32, FCML_DS_32 }
57 };
58
59 static opt_path_element opt_64_bit_path_for_osa_32[] = {
60 { FCML_EN_ASF_32, FCML_DS_32 },
61 { FCML_EN_ASF_64, FCML_DS_64 },
62 { FCML_EN_ASF_16, FCML_DS_16 }
63 };
64
65 static opt_path_element opt_64_bit_path_for_osa_64[] = {
66 { FCML_EN_ASF_64, FCML_DS_64 },
67 { FCML_EN_ASF_32, FCML_DS_32 },
68 { FCML_EN_ASF_16, FCML_DS_16 }
69 };
70
71 static fcml_int go_through_opt_path(fcml_flags flags, fcml_int path_length,
72 opt_path_element *path, fcml_usize *attribute_dest) {
73 int i, count = 0;
74 for (i = 0; i < path_length; i++) {
75 opt_path_element *element = &(path[i]);
76 if ((flags & element->flags) || (flags == FCML_EN_ASF_ANY)) {
77 attribute_dest[count++] = element->attribute_size;
78 }
79 }
80 return count;
81 }
82
83 static fcml_int try_set_attribute_size(fcml_flags flags,
84 fcml_uint16_t opt_flags, fcml_usize attribute_size,
85 fcml_usize *attribute_dest) {
86 if ((flags & opt_flags) || (flags == FCML_EN_ASF_ANY)) {
87 *attribute_dest = attribute_size;
88 return 1;
89 }
90 return 0;
91 }
92
93 fcml_ceh_error LIB_CALL fcml_fn_asm_default_optimizer(
94 fcml_st_asm_optimizer_context *context,
95 fcml_st_asm_optimizer_processing_details *ds_flags,
96 fcml_fnp_asm_optimizer_callback callback, fcml_ptr callback_args) {
97
98 fcml_ceh_error error = FCML_CEH_GEC_INVALID_ADDRESS_SIZE;
99
100 const fcml_st_entry_point *entry_point = context->entry_point;
101
102 fcml_usize ep_asa = entry_point->address_size_attribute;
103 fcml_usize ep_osa = entry_point->operand_size_attribute;
104
105 /* TODO: Maybe we should also try different vector lengths if there is
106 such a possibility. */
107 fcml_usize easa[2];
108 fcml_usize eosa[3];
109
110 fcml_int easa_count = 0;
111 fcml_int eosa_count = 0;
112
113 opt_path_element *path;
114
115 fcml_uint16_t opt_flags = context->optimizer_flags;
116
117 /* Just in case. */
118 if (ds_flags == NULL) {
119 FCML_TRACE_MSG("Optimizer details flags haven't been set.");
120 return FCML_CEH_GEC_INTERNAL_ERROR;
121 }
122
123 FCML_TRACE("Allowed ASA set: %d, flags: 0x%02X.",
124 ds_flags->allowed_easa.is_set,
125 ds_flags->allowed_easa.flags);
126
127 FCML_TRACE("Allowed OSA set: %d, flags: 0x%02X.",
128 ds_flags->allowed_eosa.is_set,
129 ds_flags->allowed_eosa.flags);
130
131 /* Address size attribute. */
132
133 if (ds_flags->easa) {
134 /* Effective address size has been already chosen and cannot
135 * be changed.
136 */
137 FCML_TRACE("ASA forced to %d bits by addr mode acceptors.",
138 ds_flags->easa);
139 easa[easa_count++] = ds_flags->easa;
140 } else {
141 /* Maybe the user has already asked for a specific ASA? */
142 fcml_usize attr_size = 0;
143 fcml_flags attr_size_flag = 0;
144 switch (opt_flags & 0x000F) {
145 case FCML_OPTF_ASA_16:
146 FCML_TRACE("ASA forced to 16 bits by optimizer_flags (config).");
147 attr_size = FCML_DS_16;
148 attr_size_flag = FCML_EN_ASF_16;
149 break;
150 case FCML_OPTF_ASA_32:
151 FCML_TRACE("ASA forced to 32 bits by optimizer_flags (config).");
152 attr_size = FCML_DS_32;
153 attr_size_flag = FCML_EN_ASF_32;
154 break;
155 case FCML_OPTF_ASA_64:
156 FCML_TRACE("ASA forced to 64 bits by optimizer_flags (config).");
157 attr_size = FCML_DS_64;
158 attr_size_flag = FCML_EN_ASF_64;
159 break;
160 }
161 if (attr_size) {
162 easa_count = try_set_attribute_size(
163 ds_flags->allowed_easa.flags, attr_size_flag, attr_size,
164 &(easa[easa_count]));
165 }
166 }
167
168 /* Operand size attribute. */
169
170 if (ds_flags->eosa) {
171 /* Effective address size has been already chosen and cannot
172 * be changed.
173 */
174 FCML_TRACE("OSA forced to %d bits by addr mode acceptors.",
175 ds_flags->eosa);
176 eosa[eosa_count++] = ds_flags->eosa;
177 } else {
178 /* Maybe the user has already asked for a specific OSA? */
179 fcml_usize attr_size = 0;
180 fcml_flags attr_size_flag = 0;
181 switch (opt_flags & 0x00F0) {
182 case FCML_OPTF_OSA_16:
183 FCML_TRACE("OSA forced to 16 bits by optimizer_flags (config).");
184 attr_size = FCML_DS_16;
185 attr_size_flag = FCML_EN_ASF_16;
186 break;
187 case FCML_OPTF_OSA_32:
188 FCML_TRACE("OSA forced to 32 bits by optimizer_flags (config).");
189 attr_size = FCML_DS_32;
190 attr_size_flag = FCML_EN_ASF_32;
191 break;
192 case FCML_OPTF_OSA_64:
193 FCML_TRACE("OSA forced to 64 bits by optimizer_flags (config).");
194 attr_size = FCML_DS_64;
195 attr_size_flag = FCML_EN_ASF_64;
196 break;
197 }
198 if (attr_size) {
199 eosa_count = try_set_attribute_size(
200 ds_flags->allowed_eosa.flags, attr_size_flag, attr_size,
201 &(eosa[eosa_count]));
202 }
203 }
204
205 /* Choosing the best optimization path for ASA.*/
206
207 if (!easa_count) {
208 switch (entry_point->op_mode) {
209 case FCML_OM_16_BIT:
210 case FCML_OM_32_BIT:
211 path = (ep_asa == FCML_DS_16) ? opt_16_32_bit_path_16 :
212 opt_16_32_bit_path_32;
213 easa_count = go_through_opt_path(
214 ds_flags->allowed_easa.flags, 2, path, easa);
215 break;
216 case FCML_OM_64_BIT:
217 path = (ep_asa == FCML_DS_32) ? opt_64_bit_path_for_asa_32 :
218 opt_64_bit_path_for_asa_64;
219 easa_count = go_through_opt_path(
220 ds_flags->allowed_easa.flags, 2, path, easa);
221 break;
222 }
223 }
224
225 /* Choosing the best optimization path for OSA.*/
226
227 if (!eosa_count) {
228 switch (entry_point->op_mode) {
229 case FCML_OM_16_BIT:
230 case FCML_OM_32_BIT:
231 path = (ep_osa == FCML_DS_16) ? opt_16_32_bit_path_16 :
232 opt_16_32_bit_path_32;
233 eosa_count = go_through_opt_path(
234 ds_flags->allowed_eosa.flags, 2, path, eosa);
235 break;
236 case FCML_OM_64_BIT:
237 switch (ep_osa) {
238 case FCML_DS_16:
239 path = &opt_64_bit_path_for_osa_16[0];
240 break;
241 case FCML_DS_32:
242 path = &opt_64_bit_path_for_osa_32[0];
243 break;
244 case FCML_DS_64:
245 path = &opt_64_bit_path_for_osa_64[0];
246 break;
247 }
248 eosa_count = go_through_opt_path(
249 ds_flags->allowed_eosa.flags, 3, path, eosa);
250 break;
251 }
252 }
253
254 ds_flags->break_optimization = FCML_FALSE;
255
256 /* If true all addressing modes available for a given instruction will
257 * be encoded not only the optimal one.
258 */
259 fcml_bool all_forms = opt_flags == FCML_OPTF_ALL_FORMS;
260
261 int i, j;
262 for (i = 0; i < easa_count && (all_forms || error) &&
263 !ds_flags->break_optimization; i++) {
264 ds_flags->easa = easa[i];
265 for (j = 0; j < eosa_count && (all_forms || error) &&
266 !ds_flags->break_optimization; j++) {
267 ds_flags->eosa = eosa[j];
268 FCML_TRACE("Optimizing for (EASA: %d, EOSA: %d).",
269 ds_flags->easa, ds_flags->eosa);
270 error = callback(callback_args);
271 }
272 }
273
274 return error;
275 }