| 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 |
/** @file fcml_optimizers.h |
| 21 |
* API for assembler optimizers. For more details about optimizers see |
| 22 |
* FCML manual. |
| 23 |
* |
| 24 |
* @copyright Copyright (C) 2010-2020 Slawomir Wojtasiak. All rights reserved. |
| 25 |
* This project is released under the GNU Lesser General Public License. |
| 26 |
*/ |
| 27 |
#ifndef FCML_OPTIMIZERS_H_ |
| 28 |
#define FCML_OPTIMIZERS_H_ |
| 29 |
|
| 30 |
#include "fcml_lib_export.h" |
| 31 |
|
| 32 |
#include "fcml_types.h" |
| 33 |
#include "fcml_errors.h" |
| 34 |
#include "fcml_common.h" |
| 35 |
|
| 36 |
#ifdef __cplusplus |
| 37 |
extern "C" { |
| 38 |
#endif |
| 39 |
|
| 40 |
/** Optimizer context used as a connector with the environment. */ |
| 41 |
typedef struct fcml_st_asm_optimizer_context { |
| 42 |
/** Instruction's entry point provided by the caller. The |
| 43 |
* same which is available in the assembler context. */ |
| 44 |
const fcml_st_entry_point *entry_point; |
| 45 |
/** Optimizer flags passed through the assembler context. |
| 46 |
* @see fcml_st_assembler_conf |
| 47 |
*/ |
| 48 |
fcml_uint16_t optimizer_flags; |
| 49 |
} fcml_st_asm_optimizer_context; |
| 50 |
|
| 51 |
/** Processing details for optimizers. */ |
| 52 |
typedef struct fcml_st_asm_optimizer_processing_details { |
| 53 |
/** Allowed values of the operand size attribute calculated by the |
| 54 |
* assembler engine. |
| 55 |
* It is the optimizer who decides which one should be finally used. |
| 56 |
*/ |
| 57 |
fcml_st_nullable_size_flags allowed_eosa; |
| 58 |
/** Allowed values of the address size attribute calculated by the |
| 59 |
* assembler engine. |
| 60 |
* It is the optimizer who decides which one should be finally used. |
| 61 |
*/ |
| 62 |
fcml_st_nullable_size_flags allowed_easa; |
| 63 |
/** Effective address size attribute chosen for currently processed |
| 64 |
* instruction form. |
| 65 |
* If it is set it can not be changed anymore. It has |
| 66 |
* higher priority than flags above. Take into account that it is |
| 67 |
* effective attribute size attribute so it can be forced using instruction |
| 68 |
* prefixes to override the default attribute size. |
| 69 |
*/ |
| 70 |
fcml_usize easa; |
| 71 |
/** Effective operand size attribute chosen for currently processed |
| 72 |
* instruction form. |
| 73 |
* If it is set it can not be changed anymore. It has higher priority than |
| 74 |
* flags above. Take into account that it is the effective operand size |
| 75 |
* attribute so it can be forced using instruction prefixes to override |
| 76 |
* the default attribute size. |
| 77 |
*/ |
| 78 |
fcml_usize eosa; |
| 79 |
/** Vector length of AVX instructions. Set to FCML_DS_UNDEF if not used. */ |
| 80 |
fcml_usize vector_length; |
| 81 |
/** Set to true in order to break optimization process immediately. */ |
| 82 |
fcml_bool break_optimization; |
| 83 |
} fcml_st_asm_optimizer_processing_details; |
| 84 |
|
| 85 |
/** |
| 86 |
* Callback used to invoke encoding process for given processing details |
| 87 |
* configuration. |
| 88 |
* @param args Arguments from optimizer. |
| 89 |
* @return Error code or FCML_CEH_GEC_NO_ERROR. |
| 90 |
*/ |
| 91 |
typedef fcml_ceh_error (*fcml_fnp_asm_optimizer_callback)(fcml_ptr args); |
| 92 |
|
| 93 |
/** |
| 94 |
* Function pointer declaration for optimizers. |
| 95 |
* @param context Optimizer context. |
| 96 |
* @param ds_flags Current instruction processing details. |
| 97 |
* @param callback Callback used to continue processing for configuration |
| 98 |
* prepared by optimizer. |
| 99 |
* @param args Arguments that should be passed to the callback. |
| 100 |
* @return Error code or FCML_CEH_GEC_NO_ERROR. |
| 101 |
*/ |
| 102 |
typedef fcml_ceh_error (LIB_CALL *fcml_fnp_asm_optimizer)( |
| 103 |
fcml_st_asm_optimizer_context *context, |
| 104 |
fcml_st_asm_optimizer_processing_details *ds_flags, |
| 105 |
fcml_fnp_asm_optimizer_callback callback, fcml_ptr args); |
| 106 |
|
| 107 |
/* Optimizers flags that can be used to configure optimization process. */ |
| 108 |
#define FCML_OPTF_ASA_16 0x01 |
| 109 |
#define FCML_OPTF_ASA_32 0x02 |
| 110 |
#define FCML_OPTF_ASA_64 0x03 |
| 111 |
#define FCML_OPTF_OSA_16 0x10 |
| 112 |
#define FCML_OPTF_OSA_32 0x20 |
| 113 |
#define FCML_OPTF_OSA_64 0x30 |
| 114 |
/* Do not choose the optimal form, but return all possibilities. */ |
| 115 |
#define FCML_OPTF_ALL_FORMS 0xFF |
| 116 |
|
| 117 |
/** |
| 118 |
* Default optimizer implementation. |
| 119 |
* This implementation chooses the best combination of attributes for |
| 120 |
* current processor operating mode. |
| 121 |
* @param context Optimizer context. |
| 122 |
* @param ds_flags Current instruction processing details. |
| 123 |
* @param callback Callback used to continue processing for configuration |
| 124 |
* prepared by optimizer. |
| 125 |
* @param callback_args Arguments that should be passed to the callback. |
| 126 |
* @return Error code or FCML_CEH_GEC_NO_ERROR. |
| 127 |
*/ |
| 128 |
LIB_EXPORT fcml_ceh_error LIB_CALL fcml_fn_asm_default_optimizer( |
| 129 |
fcml_st_asm_optimizer_context *context, |
| 130 |
fcml_st_asm_optimizer_processing_details *ds_flags, |
| 131 |
fcml_fnp_asm_optimizer_callback callback, fcml_ptr callback_args); |
| 132 |
|
| 133 |
#ifdef __cplusplus |
| 134 |
} |
| 135 |
#endif |
| 136 |
|
| 137 |
#endif /* FCML_OPTIMIZERS_H_ */ |