| 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_APC_AST_H_ |
| 21 |
#define FCML_APC_AST_H_ |
| 22 |
|
| 23 |
#include <stdlib.h> |
| 24 |
#include <fcml_symbols.h> |
| 25 |
#include <fcml_parser.h> |
| 26 |
#include "fcml_common.h" |
| 27 |
#include "fcml_coll.h" |
| 28 |
|
| 29 |
#define FCML_AST_INT_FLAG_IMMEDIATE 0x0001 |
| 30 |
|
| 31 |
typedef enum fcml_en_ast_node_type { |
| 32 |
FCML_EN_TN_INSTRUCTION, |
| 33 |
FCML_EN_TN_OPERAND_LIST, |
| 34 |
FCML_EN_TN_EXP, |
| 35 |
FCML_EN_TN_UMINUS, |
| 36 |
FCML_EN_TN_VALUE, |
| 37 |
FCML_EN_TN_REG, |
| 38 |
FCML_EN_TN_VIRTUAL, |
| 39 |
FCML_EN_TN_FAR_POINTER, |
| 40 |
FCML_EN_TN_EFFECTIVE_ADDRESS, |
| 41 |
FCML_EN_TN_USE_SYMBOL |
| 42 |
} fcml_en_ast_node_type; |
| 43 |
|
| 44 |
typedef struct fcml_st_ast_size_operator { |
| 45 |
fcml_uint16_t size; |
| 46 |
fcml_bool multimedia; |
| 47 |
} fcml_st_size_operator; |
| 48 |
|
| 49 |
typedef struct fcml_st_ast_val_integer { |
| 50 |
fcml_uint64_t value; |
| 51 |
fcml_bool overflow; |
| 52 |
fcml_bool is_signed; |
| 53 |
} fcml_st_ast_val_integer; |
| 54 |
|
| 55 |
typedef struct fcml_st_ast_val_float { |
| 56 |
fcml_float value; |
| 57 |
fcml_bool overflow; |
| 58 |
} fcml_st_ast_val_float; |
| 59 |
|
| 60 |
typedef struct fcml_st_ast_node { |
| 61 |
fcml_en_ast_node_type type; |
| 62 |
fcml_ptr node; |
| 63 |
} fcml_st_ast_node; |
| 64 |
|
| 65 |
typedef enum fcml_st_ast_value_node_type { |
| 66 |
FCML_EN_ET_INTEGER, |
| 67 |
FCML_EN_ET_FLOAT |
| 68 |
} fcml_st_ast_value_node_type; |
| 69 |
|
| 70 |
typedef struct fcml_st_ast_node_value { |
| 71 |
fcml_st_ast_value_node_type type; |
| 72 |
fcml_st_ast_val_integer integer_value; |
| 73 |
fcml_st_ast_val_float float_value; |
| 74 |
} fcml_st_ast_node_value; |
| 75 |
|
| 76 |
typedef struct fcml_st_ast_node_register { |
| 77 |
fcml_st_register reg; |
| 78 |
fcml_st_operand_decorators decorators; |
| 79 |
} fcml_st_ast_node_register; |
| 80 |
|
| 81 |
typedef struct fcml_st_ast_node_virtual { |
| 82 |
fcml_st_operand_decorators decorators; |
| 83 |
} fcml_st_ast_node_virtual; |
| 84 |
|
| 85 |
typedef enum fcml_en_ast_exp_node_operator { |
| 86 |
FCML_EN_EXN_SUB, |
| 87 |
FCML_EN_EXN_ADD, |
| 88 |
FCML_EN_EXN_MUL, |
| 89 |
FCML_EN_EXN_DIV |
| 90 |
} fcml_en_ast_exp_node_operator; |
| 91 |
|
| 92 |
typedef struct fcml_st_ast_node_exp { |
| 93 |
fcml_st_ast_node *l; |
| 94 |
fcml_st_ast_node *r; |
| 95 |
fcml_en_ast_exp_node_operator op; |
| 96 |
} fcml_st_ast_node_exp; |
| 97 |
|
| 98 |
typedef struct fcml_st_ast_node_uminus { |
| 99 |
fcml_st_ast_node *exp; |
| 100 |
} fcml_st_ast_node_uminus; |
| 101 |
|
| 102 |
typedef struct fcml_st_ast_node_define_symbol { |
| 103 |
fcml_st_symbol *symbol; |
| 104 |
} fcml_st_ast_node_define_symbol; |
| 105 |
|
| 106 |
typedef struct fcml_st_ast_node_use_symbol { |
| 107 |
fcml_string symbol; |
| 108 |
} fcml_st_ast_node_use_symbol; |
| 109 |
|
| 110 |
typedef struct fcml_st_ast_node_far_pointer { |
| 111 |
fcml_st_ast_node *segment_selector; |
| 112 |
fcml_st_ast_node *offset; |
| 113 |
} fcml_st_ast_node_far_pointer; |
| 114 |
|
| 115 |
typedef struct fcml_st_ast_node_effective_address { |
| 116 |
fcml_hints instruction_hints; |
| 117 |
fcml_hints addressing_hints; |
| 118 |
fcml_uint16_t size_operator; |
| 119 |
fcml_st_register segment_selector; |
| 120 |
fcml_st_register base; |
| 121 |
fcml_st_register index; |
| 122 |
fcml_st_ast_val_integer scale_factor; |
| 123 |
fcml_st_ast_node *displacement; |
| 124 |
fcml_en_effective_address_form address_form; |
| 125 |
fcml_st_operand_decorators decorators; |
| 126 |
} fcml_st_ast_node_effective_address; |
| 127 |
|
| 128 |
typedef struct fcml_st_ast_node_operand_list { |
| 129 |
fcml_st_coll_list *operands; |
| 130 |
} fcml_st_ast_node_operand_list; |
| 131 |
|
| 132 |
typedef struct fcml_st_ast_node_instruction { |
| 133 |
fcml_uint16_t prefixes; |
| 134 |
fcml_hints hints; |
| 135 |
fcml_string mnemonic; |
| 136 |
fcml_st_ast_node *operands; |
| 137 |
} fcml_st_ast_node_instruction; |
| 138 |
|
| 139 |
typedef struct fcml_st_cif_converter_context { |
| 140 |
/* Set to true in order to ignore all undefined symbols. |
| 141 |
* In such case every unknown symbol is treated as 0. |
| 142 |
*/ |
| 143 |
fcml_bool ignore_undefined_symbols; |
| 144 |
/* Container for errors. */ |
| 145 |
fcml_st_ceh_error_container *errors; |
| 146 |
/* Symbols table. */ |
| 147 |
fcml_st_symbol_table symbol_table; |
| 148 |
/* Number of evaluated symbols. */ |
| 149 |
fcml_int evaluated_symbols; |
| 150 |
/* Number of ignored undefined symbols. */ |
| 151 |
fcml_int ignored_symbols; |
| 152 |
} fcml_st_cif_converter_context; |
| 153 |
|
| 154 |
/* Function pointer for node visitor. */ |
| 155 |
typedef fcml_ceh_error (*fcml_fnp_ast_node_visitor)(fcml_st_ast_node *node, |
| 156 |
fcml_ptr visitor_args ); |
| 157 |
|
| 158 |
/* Operations */ |
| 159 |
|
| 160 |
fcml_st_ast_node *fcml_fn_ast_alloc_node_integer( |
| 161 |
fcml_st_ast_val_integer *integer_value); |
| 162 |
|
| 163 |
fcml_st_ast_node *fcml_fn_ast_alloc_node_float( |
| 164 |
fcml_st_ast_val_float *float_value); |
| 165 |
|
| 166 |
fcml_st_ast_node *fcml_fn_ast_alloc_node_operand_list( |
| 167 |
fcml_st_ast_node *operand_l, fcml_st_ast_node *operand_r); |
| 168 |
|
| 169 |
fcml_st_ast_node *fcml_fn_ast_alloc_node_instruction(fcml_prefixes prefixes, |
| 170 |
fcml_string mnemonic, fcml_usize length, fcml_hints hints, |
| 171 |
fcml_st_ast_node *operand_list); |
| 172 |
|
| 173 |
fcml_st_ast_node *fcml_fn_ast_alloc_node_pseudo_operation(fcml_string mnemonic, |
| 174 |
fcml_usize length, fcml_st_ast_node *exp); |
| 175 |
|
| 176 |
fcml_st_ast_node *fcml_fn_ast_alloc_node_virtual(fcml_bool sae, |
| 177 |
fcml_bool er_enabled, fcml_uint8_t er_value); |
| 178 |
|
| 179 |
fcml_st_ast_node *fcml_fn_ast_alloc_node_register(fcml_st_register *reg, |
| 180 |
fcml_st_register *opmask_reg_decorator, fcml_bool zero_decorator); |
| 181 |
|
| 182 |
fcml_st_symbol *fcml_fn_ast_alloc_node_define_symbol(fcml_ip ip, |
| 183 |
fcml_string symbol, fcml_usize length); |
| 184 |
|
| 185 |
fcml_st_ast_node *fcml_fn_ast_alloc_node_use_symbol(fcml_string symbol_name, |
| 186 |
fcml_usize length); |
| 187 |
|
| 188 |
fcml_st_ast_node *fcml_fn_ast_alloc_node_exp(fcml_en_ast_exp_node_operator op, |
| 189 |
fcml_st_ast_node *exp_left, fcml_st_ast_node *exp_right); |
| 190 |
|
| 191 |
fcml_st_ast_node *fcml_fn_ast_alloc_node_far_pointer( |
| 192 |
fcml_st_ast_node *segment_selector, fcml_st_ast_node *offset); |
| 193 |
|
| 194 |
fcml_st_ast_node *fcml_fn_ast_set_effective_address_hins( |
| 195 |
fcml_st_ast_node *effective_address_node, |
| 196 |
fcml_hints hints); |
| 197 |
|
| 198 |
fcml_st_ast_node *fcml_fn_ast_decorate_effective_address( |
| 199 |
fcml_st_ast_node *node, fcml_st_register *opmask_reg_decorator, |
| 200 |
fcml_bool zero_decorator); |
| 201 |
|
| 202 |
fcml_st_ast_node *fcml_fn_ast_set_effective_address_details( |
| 203 |
fcml_st_register *segment_selector, fcml_st_size_operator *size_operator, |
| 204 |
fcml_uint8_t bcast_decorator, fcml_st_ast_node *effective_address ); |
| 205 |
|
| 206 |
fcml_st_ast_node *fcml_fn_ast_set_displacemnt(fcml_st_ast_node *displacement, |
| 207 |
fcml_st_ast_node *effective_address); |
| 208 |
|
| 209 |
fcml_st_ast_node *fcml_fn_ast_alloc_node_effective_address(fcml_st_register *base, |
| 210 |
fcml_st_register *index, fcml_st_ast_val_integer *scale_factor, |
| 211 |
fcml_st_ast_node *displacement, fcml_bool uminus_displacement, fcml_hints hints); |
| 212 |
|
| 213 |
fcml_st_ast_node *fcml_fn_ast_alloc_node_uminus(fcml_st_ast_node *exp); |
| 214 |
|
| 215 |
void fcml_fn_ast_free_node(fcml_st_ast_node *exp); |
| 216 |
|
| 217 |
fcml_ceh_error visit_nodes(fcml_st_ast_node *ast_tree, |
| 218 |
fcml_fnp_ast_node_visitor visitor, fcml_ptr visitor_args); |
| 219 |
|
| 220 |
fcml_ceh_error fcml_fn_ast_extract_used_symbols(fcml_st_ast_node *ast_tree, |
| 221 |
fcml_st_coll_list **extracted_symbols); |
| 222 |
|
| 223 |
fcml_ceh_error fcml_fn_ast_to_cif_converter( |
| 224 |
fcml_st_cif_converter_context *context, |
| 225 |
fcml_st_ast_node *ast_tree, fcml_st_instruction **instruction); |
| 226 |
|
| 227 |
void fcml_fn_ast_free_converted_cif(fcml_st_instruction *cif_instruction); |
| 228 |
|
| 229 |
#endif /* FCML_APC_AST_H_ */ |