| 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 "chooser_t.h" |
| 21 |
|
| 22 |
#include <stdio.h> |
| 23 |
#include <string.h> |
| 24 |
|
| 25 |
#include <fcml_assembler.h> |
| 26 |
#include <fcml_choosers.h> |
| 27 |
#include <fcml_common.h> |
| 28 |
#include <fcml_common_utils.h> |
| 29 |
#include <fcml_disassembler.h> |
| 30 |
#include <fcml_optimizers.h> |
| 31 |
#include <fcml_renderer.h> |
| 32 |
#include <fcml_types.h> |
| 33 |
|
| 34 |
#include "instructions_base_t.h" |
| 35 |
|
| 36 |
fcml_bool fcml_tf_chooser_suite_init(void) { |
| 37 |
return FCML_TRUE; |
| 38 |
} |
| 39 |
|
| 40 |
fcml_bool fcml_tf_chooser_suite_cleanup(void) { |
| 41 |
return FCML_TRUE; |
| 42 |
} |
| 43 |
|
| 44 |
void fcml_fn_chooser_default(void) { |
| 45 |
|
| 46 |
/* Instruction "adc ax,8042h" can be assembled in two ways.*/ |
| 47 |
/* 1: 0x66, 0x15, 0x42, 0x80*/ |
| 48 |
/* 2: 0x66, 0x81, 0xd0, 0x42, 0x80*/ |
| 49 |
/* Default instruction chooser is responsible for choosing the shortest form.*/ |
| 50 |
|
| 51 |
fcml_st_assembler_context context = {0}; |
| 52 |
context.assembler = assembler_intel; |
| 53 |
context.entry_point.op_mode = FCML_OM_32_BIT; |
| 54 |
context.entry_point.ip = 0x00401000; |
| 55 |
|
| 56 |
fcml_st_instruction instruction = {0}; |
| 57 |
instruction.mnemonic = "adc"; |
| 58 |
instruction.operands[0] = FCML_REG( fcml_reg_AX ); |
| 59 |
instruction.operands[1] = FCML_IMM16( 0x8042 ); |
| 60 |
|
| 61 |
fcml_st_assembler_result result; |
| 62 |
|
| 63 |
fcml_fn_assembler_result_prepare( &result ); |
| 64 |
|
| 65 |
if( !fcml_fn_assemble( &context, &instruction, &result ) ) { |
| 66 |
STF_ASSERT_PTR_NOT_NULL( result.chosen_instruction ); |
| 67 |
if( result.chosen_instruction ) { |
| 68 |
STF_ASSERT_EQUAL( 4, result.chosen_instruction->code_length ); |
| 69 |
if( result.chosen_instruction->code_length == 4 ) { |
| 70 |
STF_ASSERT_EQUAL( 0x66, result.chosen_instruction->code[0] ); |
| 71 |
STF_ASSERT_EQUAL( 0x15, result.chosen_instruction->code[1] ); |
| 72 |
STF_ASSERT_EQUAL( 0x42, result.chosen_instruction->code[2] ); |
| 73 |
STF_ASSERT_EQUAL( 0x80, result.chosen_instruction->code[3] ); |
| 74 |
} |
| 75 |
} |
| 76 |
fcml_fn_assembler_result_free( &result ); |
| 77 |
} else { |
| 78 |
STF_FAIL("Can not assemble instruction."); |
| 79 |
} |
| 80 |
|
| 81 |
} |
| 82 |
|
| 83 |
void fcml_fn_chooser_null_optimizer_all_forms(void) { |
| 84 |
|
| 85 |
/* Instruction "adc ax,8042h" can be assembled in two ways.*/ |
| 86 |
/* 1: 0x66, 0x15, 0x42, 0x80*/ |
| 87 |
/* 2: 0x66, 0x81, 0xd0, 0x42, 0x80*/ |
| 88 |
/* Null instruction chooser is responsible for choosing them all. Optimizer should also try all allowed ASA/OSA combinations.*/ |
| 89 |
|
| 90 |
fcml_ceh_error error; |
| 91 |
|
| 92 |
fcml_st_assembler_context context = {0}; |
| 93 |
context.configuration.optimizer_flags = FCML_OPTF_ALL_FORMS; |
| 94 |
context.configuration.chooser = &fcml_fn_asm_no_instruction_chooser; |
| 95 |
context.assembler = assembler_intel; |
| 96 |
context.entry_point.op_mode = FCML_OM_32_BIT; |
| 97 |
context.entry_point.ip = 0x00401000; |
| 98 |
|
| 99 |
fcml_st_instruction instruction = {0}; |
| 100 |
instruction.mnemonic = "adc"; |
| 101 |
instruction.operands[0] = FCML_REG( fcml_reg_AX ); |
| 102 |
instruction.operands[1] = FCML_IMM16( 0x8042 ); |
| 103 |
|
| 104 |
fcml_st_assembler_result result; |
| 105 |
|
| 106 |
fcml_fn_assembler_result_prepare( &result ); |
| 107 |
|
| 108 |
if( !fcml_fn_assemble( &context, &instruction, &result ) ) { |
| 109 |
|
| 110 |
STF_ASSERT_PTR_NULL( result.chosen_instruction ); |
| 111 |
|
| 112 |
/* Disassemble and render every instruction available.*/ |
| 113 |
|
| 114 |
fcml_st_disassembler_context d_context = {0}; |
| 115 |
d_context.entry_point.op_mode = FCML_OM_32_BIT; |
| 116 |
d_context.entry_point.ip = 0x00401000; |
| 117 |
d_context.disassembler = disassembler_intel; |
| 118 |
|
| 119 |
fcml_int flags = 0; |
| 120 |
|
| 121 |
/* Iterate over all instructions.*/ |
| 122 |
|
| 123 |
fcml_st_assembled_instruction *instruction = result.instructions; |
| 124 |
|
| 125 |
while( instruction ) { |
| 126 |
|
| 127 |
/* Disassemble instructions one by one.*/ |
| 128 |
|
| 129 |
d_context.code = instruction->code; |
| 130 |
d_context.code_length = instruction->code_length; |
| 131 |
|
| 132 |
fcml_st_disassembler_result dasm_result; |
| 133 |
|
| 134 |
fcml_fn_disassembler_result_prepare( &dasm_result ); |
| 135 |
|
| 136 |
error = fcml_fn_disassemble( &d_context, &dasm_result ); |
| 137 |
if( error ) { |
| 138 |
break; |
| 139 |
} |
| 140 |
|
| 141 |
/* Render instructions one by one.*/ |
| 142 |
|
| 143 |
fcml_char buffer[FCML_REND_MAX_BUFF_LEN]; |
| 144 |
|
| 145 |
fcml_st_render_config config = {0}; |
| 146 |
config.render_flags = FCML_REND_FLAG_RENDER_CODE; |
| 147 |
|
| 148 |
error = fcml_fn_render( dialect_intel, &config, buffer, sizeof( buffer ), &dasm_result ); |
| 149 |
if( error ) { |
| 150 |
/* Free disassembling result.*/ |
| 151 |
fcml_fn_disassembler_result_free( &dasm_result ); |
| 152 |
break; |
| 153 |
} |
| 154 |
|
| 155 |
fcml_fn_disassembler_result_free( &dasm_result ); |
| 156 |
|
| 157 |
if( strcmp( FCML_TEXT( "666781d04280 adc ax,-32702" ), buffer ) == 0 ) { |
| 158 |
flags |= 0x01; |
| 159 |
} |
| 160 |
|
| 161 |
if( strcmp( FCML_TEXT( "6681d04280 adc ax,-32702" ), buffer ) == 0 ) { |
| 162 |
flags |= 0x02; |
| 163 |
} |
| 164 |
|
| 165 |
if( strcmp( FCML_TEXT( "6667154280 adc ax,-32702" ), buffer ) == 0 ) { |
| 166 |
flags |= 0x04; |
| 167 |
} |
| 168 |
|
| 169 |
if( strcmp( FCML_TEXT( "66154280 adc ax,-32702" ), buffer ) == 0) { |
| 170 |
flags |= 0x08; |
| 171 |
} |
| 172 |
|
| 173 |
instruction = instruction->next; |
| 174 |
} |
| 175 |
|
| 176 |
fcml_fn_assembler_result_free( &result ); |
| 177 |
|
| 178 |
STF_ASSERT_EQUAL( error, 0 ); |
| 179 |
STF_ASSERT_EQUAL( flags, 0x0000000F ); |
| 180 |
|
| 181 |
} else { |
| 182 |
STF_FAIL("Can not assemble instruction."); |
| 183 |
} |
| 184 |
|
| 185 |
} |
| 186 |
|
| 187 |
fcml_stf_test_case fcml_ti_chooser[] = { |
| 188 |
{ "fcml_fn_chooser_default", fcml_fn_chooser_default }, |
| 189 |
{ "fcml_fn_chooser_null_optimizer_all_forms", fcml_fn_chooser_null_optimizer_all_forms }, |
| 190 |
FCML_STF_NULL_TEST |
| 191 |
}; |
| 192 |
|
| 193 |
fcml_stf_test_suite fcml_si_chooser = { |
| 194 |
"suite-fcml-chooser", fcml_tf_chooser_suite_init, fcml_tf_chooser_suite_cleanup, fcml_ti_chooser |
| 195 |
}; |
| 196 |
|