| 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 |
#ifndef FCML_UTILS_H_ |
| 21 |
#define FCML_UTILS_H_ |
| 22 |
|
| 23 |
#include <fcml_types.h> |
| 24 |
#include <fcml_common.h> |
| 25 |
#include <fcml_errors.h> |
| 26 |
|
| 27 |
#include "fcml_ceh.h" |
| 28 |
#include "fcml_stream.h" |
| 29 |
|
| 30 |
typedef enum fcml_en_utils_size_flags { |
| 31 |
FCML_ENUSF_8 = 0x01, |
| 32 |
FCML_ENUSF_16 = 0x02, |
| 33 |
FCML_ENUSF_32 = 0x04, |
| 34 |
FCML_ENUSF_64 = 0x08, |
| 35 |
FCML_ENUSF_ALL = ( FCML_ENUSF_8 | FCML_ENUSF_16 | FCML_ENUSF_32 | FCML_ENUSF_64 ) |
| 36 |
} fcml_en_utils_size_flags; |
| 37 |
|
| 38 |
fcml_ceh_error fcml_fn_utils_conv_int64_to_int( |
| 39 |
const fcml_uint64_t src, fcml_bool is_src_signed, |
| 40 |
fcml_st_integer *dest, fcml_en_utils_size_flags filter); |
| 41 |
|
| 42 |
fcml_ceh_error fcml_fn_utils_conv_int_to_int(const fcml_st_integer *src, |
| 43 |
fcml_st_integer *dest, fcml_usize expected_source_size, |
| 44 |
fcml_usize destination_size); |
| 45 |
|
| 46 |
fcml_ceh_error fcml_fn_utils_conv_int_to_uint8(const fcml_st_integer *src, |
| 47 |
fcml_uint8_t *dest); |
| 48 |
|
| 49 |
fcml_ceh_error fcml_fn_utils_conv_int_to_uint16(const fcml_st_integer *src, |
| 50 |
fcml_uint16_t *dest); |
| 51 |
|
| 52 |
fcml_ceh_error fcml_fn_utils_conv_int_to_uint32(const fcml_st_integer *src, |
| 53 |
fcml_uint32_t *dest); |
| 54 |
|
| 55 |
fcml_ceh_error fcml_fn_utils_conv_int_to_uint64(const fcml_st_integer *src, |
| 56 |
fcml_uint64_t *dest); |
| 57 |
|
| 58 |
fcml_ceh_error fcml_fn_utils_conv_int_to_int8(const fcml_st_integer *src, |
| 59 |
fcml_int8_t *dest); |
| 60 |
|
| 61 |
fcml_ceh_error fcml_fn_utils_conv_int_to_int16(const fcml_st_integer *src, |
| 62 |
fcml_int16_t *dest); |
| 63 |
|
| 64 |
fcml_ceh_error fcml_fn_utils_conv_int_to_int32(const fcml_st_integer *src, |
| 65 |
fcml_int32_t *dest); |
| 66 |
|
| 67 |
fcml_ceh_error fcml_fn_utils_conv_int_to_int64(const fcml_st_integer *src, |
| 68 |
fcml_int64_t *dest); |
| 69 |
|
| 70 |
fcml_ceh_error fcml_fn_utils_decode_integer(fcml_st_memory_stream *stream, |
| 71 |
fcml_st_integer *src, fcml_usize size); |
| 72 |
|
| 73 |
fcml_ceh_error fcml_fn_utils_encode_integer(fcml_st_memory_stream *stream, |
| 74 |
const fcml_st_integer *src); |
| 75 |
|
| 76 |
fcml_ceh_error fcml_fn_utils_offset_to_integer(const fcml_st_offset *offset, |
| 77 |
fcml_st_integer *src); |
| 78 |
|
| 79 |
fcml_ceh_error fcml_fn_utils_integer_to_offset(const fcml_st_integer *src, |
| 80 |
fcml_st_offset *offset); |
| 81 |
|
| 82 |
fcml_ceh_error fcml_fn_utils_extend_int(fcml_st_integer *src, |
| 83 |
fcml_usize ext); |
| 84 |
|
| 85 |
fcml_ceh_error fcml_fn_utils_conv_map_error(fcml_int error); |
| 86 |
|
| 87 |
void fcml_fn_utils_conv_gec_to_error_info(fcml_bool enabled, |
| 88 |
fcml_st_ceh_error_container *error_container, fcml_ceh_error code ); |
| 89 |
|
| 90 |
fcml_ceh_error fcml_fn_utils_prepare_entry_point(fcml_st_entry_point *entry_point); |
| 91 |
|
| 92 |
fcml_bool fcml_fn_utils_is_disp_neg(const fcml_st_integer *displacement); |
| 93 |
|
| 94 |
fcml_bool fcml_fn_utils_validate_vector_len(fcml_usize vector_length); |
| 95 |
|
| 96 |
fcml_int64_t fcml_fn_utils_divide_integer(const fcml_st_integer *dividend, |
| 97 |
fcml_st_integer *quotient, fcml_int32_t divisor); |
| 98 |
|
| 99 |
/** |
| 100 |
* Returns TRUE if value can be safely cast to int32. |
| 101 |
*/ |
| 102 |
static inline fcml_bool safe_cast_i6432(fcml_int64_t value) { |
| 103 |
return value <= FCML_INT32_MAX && value >= FCML_INT32_MIN; |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Returns true if operand is SIMD register. |
| 108 |
*/ |
| 109 |
static inline fcml_bool is_simd_reg(const fcml_st_operand *operand) { |
| 110 |
return operand->type == FCML_OT_REGISTER && |
| 111 |
operand->reg.type == FCML_REG_SIMD; |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Returns true if operand is IMM. |
| 116 |
*/ |
| 117 |
static inline fcml_bool is_imm(const fcml_st_operand *operand) { |
| 118 |
return operand->type == FCML_OT_IMMEDIATE; |
| 119 |
} |
| 120 |
|
| 121 |
/* Returns true if register type is undefined. */ |
| 122 |
static inline fcml_bool is_reg_undef(const fcml_st_register *reg) { |
| 123 |
return reg->type == FCML_REG_UNDEFINED; |
| 124 |
} |
| 125 |
|
| 126 |
/* Returns true if integer value is 0. */ |
| 127 |
static inline fcml_bool is_integer_zero(const fcml_st_integer *imm) { |
| 128 |
switch(imm->size) { |
| 129 |
case FCML_DS_8: |
| 130 |
return imm->int8 == 0; |
| 131 |
case FCML_DS_16: |
| 132 |
return imm->int16 == 0; |
| 133 |
case FCML_DS_32: |
| 134 |
return imm->int32 == 0; |
| 135 |
case FCML_DS_64: |
| 136 |
return imm->int64 == 0; |
| 137 |
} |
| 138 |
return FCML_FALSE; |
| 139 |
} |
| 140 |
|
| 141 |
#endif /* FCML_UTILS_H_ */ |