| 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 <string.h> |
| 21 |
#include <fcml_errors.h> |
| 22 |
#include <fcml_symbols.h> |
| 23 |
|
| 24 |
#include "fcml_env_int.h" |
| 25 |
#include "fcml_apc_ast.h" |
| 26 |
#include "fcml_utils.h" |
| 27 |
#include "fcml_trace.h" |
| 28 |
#include "fcml_messages.h" |
| 29 |
|
| 30 |
struct fcml_ist_ast_extracted_symbols { |
| 31 |
/* List of extracted symbols. */ |
| 32 |
fcml_st_coll_list *symbols; |
| 33 |
}; |
| 34 |
|
| 35 |
fcml_st_ast_node *fcml_fn_ast_alloc_node_integer( |
| 36 |
fcml_st_ast_val_integer *integer_value) { |
| 37 |
|
| 38 |
FCML_ENV_ALLOC_CLEAR(node, fcml_st_ast_node); |
| 39 |
if (!node) { |
| 40 |
return NULL; |
| 41 |
} |
| 42 |
|
| 43 |
FCML_ENV_ALLOC_CLEAR(value_node, fcml_st_ast_node_value); |
| 44 |
if (!value_node) { |
| 45 |
fcml_fn_env_memory_free(node); |
| 46 |
return NULL; |
| 47 |
} |
| 48 |
|
| 49 |
value_node->type = FCML_EN_ET_INTEGER; |
| 50 |
value_node->integer_value = *integer_value; |
| 51 |
|
| 52 |
node->type = FCML_EN_TN_VALUE; |
| 53 |
node->node = value_node; |
| 54 |
|
| 55 |
return node; |
| 56 |
} |
| 57 |
|
| 58 |
fcml_st_ast_node *fcml_fn_ast_alloc_node_float( |
| 59 |
fcml_st_ast_val_float *float_value) { |
| 60 |
|
| 61 |
FCML_ENV_ALLOC_CLEAR(node, fcml_st_ast_node); |
| 62 |
if (!node) { |
| 63 |
return NULL; |
| 64 |
} |
| 65 |
|
| 66 |
FCML_ENV_ALLOC_CLEAR(exp_node, fcml_st_ast_node_value); |
| 67 |
if (!exp_node) { |
| 68 |
fcml_fn_env_memory_free(node); |
| 69 |
return NULL; |
| 70 |
} |
| 71 |
|
| 72 |
exp_node->type = FCML_EN_ET_FLOAT; |
| 73 |
exp_node->float_value = *float_value; |
| 74 |
|
| 75 |
node->type = FCML_EN_TN_VALUE; |
| 76 |
node->node = exp_node; |
| 77 |
|
| 78 |
return node; |
| 79 |
} |
| 80 |
|
| 81 |
fcml_st_ast_node *fcml_fn_ast_alloc_node_pseudo_operation( |
| 82 |
fcml_string mnemonic, fcml_usize length, fcml_st_ast_node *exp |
| 83 |
) { |
| 84 |
|
| 85 |
fcml_st_ast_node *operand_list = |
| 86 |
fcml_fn_ast_alloc_node_operand_list(exp, NULL); |
| 87 |
|
| 88 |
if (!operand_list) { |
| 89 |
return NULL; |
| 90 |
} |
| 91 |
|
| 92 |
fcml_st_ast_node *pseudo_operation = fcml_fn_ast_alloc_node_instruction(0, |
| 93 |
mnemonic, length, 0, operand_list); |
| 94 |
if (!pseudo_operation) { |
| 95 |
fcml_fn_ast_free_node(operand_list); |
| 96 |
return NULL; |
| 97 |
} |
| 98 |
|
| 99 |
return pseudo_operation; |
| 100 |
} |
| 101 |
|
| 102 |
fcml_st_ast_node *fcml_fn_ast_alloc_node_instruction(fcml_prefixes prefixes, |
| 103 |
fcml_string mnemonic, fcml_usize length, fcml_hints hints, |
| 104 |
fcml_st_ast_node *operands_list) { |
| 105 |
|
| 106 |
FCML_ENV_ALLOC_CLEAR(node, fcml_st_ast_node); |
| 107 |
if (!node) { |
| 108 |
return NULL; |
| 109 |
} |
| 110 |
|
| 111 |
FCML_ENV_ALLOC_CLEAR(instruction_node, fcml_st_ast_node_instruction); |
| 112 |
if (!instruction_node) { |
| 113 |
fcml_fn_env_memory_free(node); |
| 114 |
return NULL; |
| 115 |
} |
| 116 |
|
| 117 |
instruction_node->mnemonic = (fcml_string)fcml_fn_env_str_strldup(mnemonic, |
| 118 |
length); |
| 119 |
|
| 120 |
if (!instruction_node->mnemonic) { |
| 121 |
fcml_fn_env_memory_free(node); |
| 122 |
fcml_fn_env_memory_free(instruction_node); |
| 123 |
return NULL; |
| 124 |
} |
| 125 |
|
| 126 |
instruction_node->operands = operands_list; |
| 127 |
instruction_node->hints = hints; |
| 128 |
instruction_node->prefixes = prefixes; |
| 129 |
|
| 130 |
node->type = FCML_EN_TN_INSTRUCTION; |
| 131 |
node->node = instruction_node; |
| 132 |
|
| 133 |
return node; |
| 134 |
} |
| 135 |
|
| 136 |
fcml_st_ast_node *fcml_fn_ast_alloc_node_virtual(fcml_bool sae, |
| 137 |
fcml_bool er_enabled, fcml_uint8_t er_value) { |
| 138 |
|
| 139 |
FCML_ENV_ALLOC_CLEAR(node, fcml_st_ast_node); |
| 140 |
if (!node) { |
| 141 |
return NULL; |
| 142 |
} |
| 143 |
|
| 144 |
FCML_ENV_ALLOC_CLEAR(virtual_node, fcml_st_ast_node_virtual); |
| 145 |
if (!virtual_node) { |
| 146 |
fcml_fn_env_memory_free(node); |
| 147 |
return NULL; |
| 148 |
} |
| 149 |
|
| 150 |
if (er_enabled) { |
| 151 |
virtual_node->decorators.er.is_not_null = FCML_TRUE; |
| 152 |
virtual_node->decorators.er.value = er_value; |
| 153 |
} |
| 154 |
|
| 155 |
virtual_node->decorators.sae = sae; |
| 156 |
|
| 157 |
node->type = FCML_EN_TN_VIRTUAL; |
| 158 |
node->node = virtual_node; |
| 159 |
|
| 160 |
return node; |
| 161 |
} |
| 162 |
|
| 163 |
fcml_st_ast_node *fcml_fn_ast_decorate_effective_address( |
| 164 |
fcml_st_ast_node *node, |
| 165 |
fcml_st_register *opmask_reg_decorator, |
| 166 |
fcml_bool zero_decorator) { |
| 167 |
|
| 168 |
fcml_st_operand_decorators *decorators = |
| 169 |
&((fcml_st_ast_node_effective_address*)node->node)->decorators; |
| 170 |
|
| 171 |
if (opmask_reg_decorator) { |
| 172 |
decorators->operand_mask_reg = *opmask_reg_decorator; |
| 173 |
} |
| 174 |
|
| 175 |
decorators->z = zero_decorator; |
| 176 |
return node; |
| 177 |
} |
| 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_ENV_ALLOC_CLEAR(node, fcml_st_ast_node); |
| 183 |
if (!node) { |
| 184 |
return NULL; |
| 185 |
} |
| 186 |
|
| 187 |
FCML_ENV_ALLOC_CLEAR(reg_node, fcml_st_ast_node_register); |
| 188 |
if (!reg_node) { |
| 189 |
fcml_fn_env_memory_free(node); |
| 190 |
return NULL; |
| 191 |
} |
| 192 |
|
| 193 |
/* Decorators. */ |
| 194 |
if (opmask_reg_decorator) { |
| 195 |
reg_node->decorators.operand_mask_reg = *opmask_reg_decorator; |
| 196 |
} |
| 197 |
|
| 198 |
reg_node->decorators.z = zero_decorator; |
| 199 |
reg_node->reg = *reg; |
| 200 |
|
| 201 |
node->type = FCML_EN_TN_REG; |
| 202 |
node->node = reg_node; |
| 203 |
|
| 204 |
return node; |
| 205 |
} |
| 206 |
|
| 207 |
fcml_st_ast_node *fcml_fn_ast_alloc_node_operand_list( |
| 208 |
fcml_st_ast_node *operand_l, |
| 209 |
fcml_st_ast_node *operand_r) |
| 210 |
{ |
| 211 |
|
| 212 |
fcml_st_ast_node *operand_list_node = NULL; |
| 213 |
fcml_st_ast_node_operand_list *operand_list = NULL; |
| 214 |
|
| 215 |
if (operand_l->type != FCML_EN_TN_OPERAND_LIST) { |
| 216 |
|
| 217 |
operand_list_node = (fcml_st_ast_node*)fcml_fn_env_memory_alloc_clear( |
| 218 |
sizeof(fcml_st_ast_node)); |
| 219 |
|
| 220 |
if (!operand_list_node) { |
| 221 |
return NULL; |
| 222 |
} |
| 223 |
|
| 224 |
operand_list = (fcml_st_ast_node_operand_list*) |
| 225 |
fcml_fn_env_memory_alloc_clear( |
| 226 |
sizeof(fcml_st_ast_node_operand_list)); |
| 227 |
|
| 228 |
if (!operand_list) { |
| 229 |
fcml_fn_env_memory_free(operand_list_node); |
| 230 |
return NULL; |
| 231 |
} |
| 232 |
|
| 233 |
operand_list_node->type = FCML_EN_TN_OPERAND_LIST; |
| 234 |
operand_list_node->node = operand_list; |
| 235 |
operand_list->operands = fcml_fn_coll_list_alloc(); |
| 236 |
|
| 237 |
if (!operand_list->operands) { |
| 238 |
fcml_fn_env_memory_free(operand_list_node); |
| 239 |
fcml_fn_env_memory_free(operand_list); |
| 240 |
return NULL; |
| 241 |
} |
| 242 |
|
| 243 |
if (!fcml_fn_coll_list_add_back(operand_list->operands, operand_l) |
| 244 |
|| (operand_r != NULL && !fcml_fn_coll_list_add_back( |
| 245 |
operand_list->operands, operand_r))) { |
| 246 |
|
| 247 |
fcml_fn_env_memory_free(operand_list_node); |
| 248 |
fcml_fn_env_memory_free(operand_list); |
| 249 |
fcml_fn_coll_list_free(operand_list->operands, NULL, NULL); |
| 250 |
return NULL; |
| 251 |
} |
| 252 |
|
| 253 |
} else { |
| 254 |
|
| 255 |
operand_list_node = operand_l; |
| 256 |
operand_list = (fcml_st_ast_node_operand_list*)operand_l->node; |
| 257 |
|
| 258 |
if (!fcml_fn_coll_list_add_back(operand_list->operands, operand_r)) { |
| 259 |
return NULL; |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
return operand_list_node; |
| 264 |
} |
| 265 |
|
| 266 |
fcml_st_symbol *fcml_fn_ast_alloc_node_define_symbol(fcml_ip ip, |
| 267 |
fcml_string symbol_name, fcml_usize length) { |
| 268 |
|
| 269 |
FCML_ENV_ALLOC_CLEAR(symbol, fcml_st_symbol); |
| 270 |
if (!symbol) { |
| 271 |
return FCML_FALSE; |
| 272 |
} |
| 273 |
|
| 274 |
/* Allocate symbol. */ |
| 275 |
symbol->symbol = (fcml_string)fcml_fn_env_str_strldup(symbol_name, length); |
| 276 |
if (!symbol->symbol) { |
| 277 |
fcml_fn_env_memory_free(symbol); |
| 278 |
return NULL; |
| 279 |
} |
| 280 |
|
| 281 |
symbol->value = (fcml_int64_t)ip; |
| 282 |
|
| 283 |
return symbol; |
| 284 |
} |
| 285 |
|
| 286 |
fcml_st_ast_node *fcml_fn_ast_alloc_node_use_symbol(fcml_string symbol_name, |
| 287 |
fcml_usize length) { |
| 288 |
|
| 289 |
FCML_ENV_ALLOC_CLEAR(node, fcml_st_ast_node); |
| 290 |
if (!node) { |
| 291 |
return NULL; |
| 292 |
} |
| 293 |
|
| 294 |
FCML_ENV_ALLOC_CLEAR(symbol_node, fcml_st_ast_node_use_symbol); |
| 295 |
if (!symbol_node){ |
| 296 |
fcml_fn_env_memory_free(node); |
| 297 |
return NULL; |
| 298 |
} |
| 299 |
|
| 300 |
/* Allocate symbol name. */ |
| 301 |
symbol_node->symbol = (fcml_string)fcml_fn_env_str_strldup(symbol_name, |
| 302 |
length); |
| 303 |
|
| 304 |
if (!symbol_node->symbol) { |
| 305 |
fcml_fn_env_memory_free(symbol_node); |
| 306 |
fcml_fn_env_memory_free(node); |
| 307 |
return NULL; |
| 308 |
} |
| 309 |
|
| 310 |
node->node = symbol_node; |
| 311 |
node->type = FCML_EN_TN_USE_SYMBOL; |
| 312 |
|
| 313 |
return node; |
| 314 |
} |
| 315 |
|
| 316 |
fcml_st_ast_node *fcml_fn_ast_alloc_node_exp(fcml_en_ast_exp_node_operator op, |
| 317 |
fcml_st_ast_node *exp_left, fcml_st_ast_node *exp_right) { |
| 318 |
|
| 319 |
FCML_ENV_ALLOC_CLEAR(node, fcml_st_ast_node); |
| 320 |
if (!node) { |
| 321 |
return NULL; |
| 322 |
} |
| 323 |
|
| 324 |
FCML_ENV_ALLOC_CLEAR(exp_node, fcml_st_ast_node_exp); |
| 325 |
if (!exp_node) { |
| 326 |
fcml_fn_env_memory_free(node); |
| 327 |
return NULL; |
| 328 |
} |
| 329 |
|
| 330 |
exp_node->l = exp_left; |
| 331 |
exp_node->r = exp_right; |
| 332 |
exp_node->op = op; |
| 333 |
|
| 334 |
node->type = FCML_EN_TN_EXP; |
| 335 |
node->node = exp_node; |
| 336 |
|
| 337 |
return node; |
| 338 |
} |
| 339 |
|
| 340 |
fcml_st_ast_node *fcml_fn_ast_alloc_node_far_pointer( |
| 341 |
fcml_st_ast_node *segment_selector, |
| 342 |
fcml_st_ast_node *offset) { |
| 343 |
|
| 344 |
FCML_ENV_ALLOC_CLEAR(node, fcml_st_ast_node); |
| 345 |
if (!node) { |
| 346 |
return NULL; |
| 347 |
} |
| 348 |
|
| 349 |
FCML_ENV_ALLOC_CLEAR(fp_node, fcml_st_ast_node_far_pointer); |
| 350 |
if (!fp_node) { |
| 351 |
fcml_fn_env_memory_free(node); |
| 352 |
return NULL; |
| 353 |
} |
| 354 |
|
| 355 |
fp_node->segment_selector = segment_selector; |
| 356 |
fp_node->offset = offset; |
| 357 |
|
| 358 |
node->type = FCML_EN_TN_FAR_POINTER; |
| 359 |
node->node = fp_node; |
| 360 |
|
| 361 |
return node; |
| 362 |
} |
| 363 |
|
| 364 |
fcml_st_ast_node *fcml_fn_ast_set_effective_address_hins( |
| 365 |
fcml_st_ast_node *effective_address_node, |
| 366 |
fcml_hints hints) { |
| 367 |
|
| 368 |
fcml_st_ast_node_effective_address *effective_address = |
| 369 |
(fcml_st_ast_node_effective_address *) effective_address_node->node; |
| 370 |
|
| 371 |
effective_address->instruction_hints |= hints; |
| 372 |
return effective_address_node; |
| 373 |
} |
| 374 |
|
| 375 |
fcml_st_ast_node *fcml_fn_ast_set_effective_address_details( |
| 376 |
fcml_st_register *segment_selector, fcml_st_size_operator *size_operator, |
| 377 |
fcml_uint8_t bcast_decorator, fcml_st_ast_node *effective_address_node) { |
| 378 |
|
| 379 |
fcml_st_ast_node_effective_address *effective_address = |
| 380 |
(fcml_st_ast_node_effective_address *)effective_address_node->node; |
| 381 |
|
| 382 |
if (segment_selector) { |
| 383 |
effective_address->segment_selector = *segment_selector; |
| 384 |
} |
| 385 |
|
| 386 |
if (bcast_decorator > 0) { |
| 387 |
effective_address->decorators.bcast.is_not_null = FCML_TRUE; |
| 388 |
effective_address->decorators.bcast.value = bcast_decorator; |
| 389 |
} |
| 390 |
|
| 391 |
/* Size operator is optional because not all assembler dialects allows |
| 392 |
* to specify it here. |
| 393 |
*/ |
| 394 |
if (size_operator) { |
| 395 |
effective_address->size_operator = size_operator->size; |
| 396 |
if (size_operator->multimedia) { |
| 397 |
effective_address->addressing_hints |= |
| 398 |
FCML_OP_HINT_MULTIMEDIA_INSTRUCTION; |
| 399 |
} |
| 400 |
} |
| 401 |
|
| 402 |
return effective_address_node; |
| 403 |
} |
| 404 |
|
| 405 |
fcml_st_ast_node *fcml_fn_ast_set_displacemnt( |
| 406 |
fcml_st_ast_node *displacement, fcml_st_ast_node *effective_address_node) { |
| 407 |
|
| 408 |
fcml_st_ast_node_effective_address *effective_address = |
| 409 |
(fcml_st_ast_node_effective_address *)effective_address_node->node; |
| 410 |
|
| 411 |
if (displacement) { |
| 412 |
effective_address->displacement = displacement; |
| 413 |
} |
| 414 |
|
| 415 |
return effective_address_node; |
| 416 |
} |
| 417 |
|
| 418 |
fcml_st_ast_node *fcml_fn_ast_alloc_node_effective_address( |
| 419 |
fcml_st_register *base, fcml_st_register *index, |
| 420 |
fcml_st_ast_val_integer *scale_factor, fcml_st_ast_node *displacement, |
| 421 |
fcml_bool uminus_displacement, fcml_hints hints) { |
| 422 |
|
| 423 |
FCML_ENV_ALLOC_CLEAR(node, fcml_st_ast_node); |
| 424 |
if (!node) { |
| 425 |
return NULL; |
| 426 |
} |
| 427 |
|
| 428 |
FCML_ENV_ALLOC_CLEAR(ea_node, fcml_st_ast_node_effective_address); |
| 429 |
if (!ea_node) { |
| 430 |
fcml_fn_env_memory_free(node); |
| 431 |
return NULL; |
| 432 |
} |
| 433 |
|
| 434 |
ea_node->addressing_hints = hints; |
| 435 |
|
| 436 |
if (base) { |
| 437 |
ea_node->base = *base; |
| 438 |
} |
| 439 |
|
| 440 |
if (index) { |
| 441 |
ea_node->index = *index; |
| 442 |
} |
| 443 |
|
| 444 |
if (scale_factor) { |
| 445 |
ea_node->scale_factor = *scale_factor; |
| 446 |
} |
| 447 |
|
| 448 |
if (displacement) { |
| 449 |
ea_node->displacement = (uminus_displacement) ? |
| 450 |
fcml_fn_ast_alloc_node_uminus(displacement) : displacement; |
| 451 |
|
| 452 |
if (!ea_node->displacement) { |
| 453 |
/* Out of memory. */ |
| 454 |
fcml_fn_env_memory_free(node); |
| 455 |
fcml_fn_env_memory_free(ea_node); |
| 456 |
return NULL; |
| 457 |
} |
| 458 |
} |
| 459 |
|
| 460 |
node->node = ea_node; |
| 461 |
node->type = FCML_EN_TN_EFFECTIVE_ADDRESS; |
| 462 |
|
| 463 |
return node; |
| 464 |
} |
| 465 |
|
| 466 |
fcml_st_ast_node *fcml_fn_ast_alloc_node_uminus(fcml_st_ast_node *exp) { |
| 467 |
|
| 468 |
fcml_st_ast_node *node; |
| 469 |
fcml_st_ast_node_uminus *uminus_node; |
| 470 |
|
| 471 |
/* Two unary minuses, so they can be reduced here. */ |
| 472 |
if (exp->type == FCML_EN_TN_UMINUS) { |
| 473 |
uminus_node = (fcml_st_ast_node_uminus*)exp->node; |
| 474 |
node = uminus_node->exp; |
| 475 |
fcml_fn_env_memory_free(exp); |
| 476 |
fcml_fn_env_memory_free(uminus_node); |
| 477 |
return node; |
| 478 |
} |
| 479 |
|
| 480 |
node = (fcml_st_ast_node*)fcml_fn_env_memory_alloc_clear( |
| 481 |
sizeof(fcml_st_ast_node)); |
| 482 |
|
| 483 |
if (!node) { |
| 484 |
return NULL; |
| 485 |
} |
| 486 |
|
| 487 |
uminus_node = (fcml_st_ast_node_uminus*)fcml_fn_env_memory_alloc_clear( |
| 488 |
sizeof(fcml_st_ast_node_uminus)); |
| 489 |
|
| 490 |
if (!uminus_node) { |
| 491 |
fcml_fn_env_memory_free(node); |
| 492 |
return NULL; |
| 493 |
} |
| 494 |
|
| 495 |
uminus_node->exp = exp; |
| 496 |
|
| 497 |
node->node = uminus_node; |
| 498 |
node->type = FCML_EN_TN_UMINUS; |
| 499 |
|
| 500 |
return node; |
| 501 |
|
| 502 |
} |
| 503 |
|
| 504 |
static void free_operand_node_list_action(fcml_ptr item_value, fcml_ptr args) { |
| 505 |
fcml_st_ast_node *node = (fcml_st_ast_node*) item_value; |
| 506 |
fcml_fn_ast_free_node(node); |
| 507 |
} |
| 508 |
|
| 509 |
void fcml_fn_ast_free_node(fcml_st_ast_node *exp) { |
| 510 |
if (!exp) { |
| 511 |
return; |
| 512 |
} else { |
| 513 |
switch (exp->type) { |
| 514 |
case FCML_EN_TN_INSTRUCTION: { |
| 515 |
fcml_st_ast_node_instruction *instruction = |
| 516 |
(fcml_st_ast_node_instruction*) exp->node; |
| 517 |
if (instruction->mnemonic) { |
| 518 |
fcml_fn_env_str_strfree(instruction->mnemonic); |
| 519 |
} |
| 520 |
fcml_fn_ast_free_node(instruction->operands); |
| 521 |
} |
| 522 |
break; |
| 523 |
case FCML_EN_TN_OPERAND_LIST: { |
| 524 |
fcml_st_ast_node_operand_list *operand_list = |
| 525 |
(fcml_st_ast_node_operand_list*) exp->node; |
| 526 |
fcml_fn_coll_list_free(operand_list->operands, |
| 527 |
free_operand_node_list_action, NULL); |
| 528 |
} |
| 529 |
break; |
| 530 |
case FCML_EN_TN_EXP: { |
| 531 |
fcml_st_ast_node_exp *expr = (fcml_st_ast_node_exp*) exp->node; |
| 532 |
fcml_fn_ast_free_node(expr->l); |
| 533 |
fcml_fn_ast_free_node(expr->r); |
| 534 |
} |
| 535 |
break; |
| 536 |
case FCML_EN_TN_UMINUS: { |
| 537 |
fcml_st_ast_node_uminus *uminus = |
| 538 |
(fcml_st_ast_node_uminus*) exp->node; |
| 539 |
fcml_fn_ast_free_node(uminus->exp); |
| 540 |
} |
| 541 |
break; |
| 542 |
case FCML_EN_TN_FAR_POINTER: { |
| 543 |
fcml_st_ast_node_far_pointer *far_pointer = |
| 544 |
(fcml_st_ast_node_far_pointer*) exp->node; |
| 545 |
fcml_fn_ast_free_node(far_pointer->offset); |
| 546 |
fcml_fn_ast_free_node(far_pointer->segment_selector); |
| 547 |
} |
| 548 |
break; |
| 549 |
case FCML_EN_TN_EFFECTIVE_ADDRESS: { |
| 550 |
fcml_st_ast_node_effective_address *effective_address = |
| 551 |
(fcml_st_ast_node_effective_address*) exp->node; |
| 552 |
fcml_fn_ast_free_node(effective_address->displacement); |
| 553 |
} |
| 554 |
break; |
| 555 |
case FCML_EN_TN_USE_SYMBOL: { |
| 556 |
fcml_st_ast_node_use_symbol *symbol_node = |
| 557 |
(fcml_st_ast_node_use_symbol*) exp->node; |
| 558 |
if (symbol_node->symbol) { |
| 559 |
fcml_fn_env_str_strfree(symbol_node->symbol); |
| 560 |
} |
| 561 |
} |
| 562 |
break; |
| 563 |
default: |
| 564 |
break; |
| 565 |
} |
| 566 |
|
| 567 |
if (exp->node) { |
| 568 |
fcml_fn_env_memory_free(exp->node); |
| 569 |
} |
| 570 |
|
| 571 |
fcml_fn_env_memory_free(exp); |
| 572 |
} |
| 573 |
} |
| 574 |
|
| 575 |
fcml_ceh_error fcml_fn_ast_visit_nodes(fcml_st_ast_node *ast_node, |
| 576 |
fcml_fnp_ast_node_visitor visitor, fcml_ptr visitor_args) { |
| 577 |
|
| 578 |
fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR; |
| 579 |
|
| 580 |
if (ast_node) { |
| 581 |
switch (ast_node->type) { |
| 582 |
case FCML_EN_TN_INSTRUCTION: { |
| 583 |
fcml_st_ast_node_instruction *node_instruction = |
| 584 |
(fcml_st_ast_node_instruction*) ast_node->node; |
| 585 |
error = fcml_fn_ast_visit_nodes(node_instruction->operands, visitor, |
| 586 |
visitor_args); |
| 587 |
if (error) |
| 588 |
break; |
| 589 |
break; |
| 590 |
} |
| 591 |
case FCML_EN_TN_OPERAND_LIST: { |
| 592 |
fcml_st_ast_node_operand_list *operand_list = |
| 593 |
(fcml_st_ast_node_operand_list*) ast_node->node; |
| 594 |
fcml_st_coll_list_element *current = operand_list->operands->head; |
| 595 |
int i = 0; |
| 596 |
while (current && i < FCML_OPERANDS_COUNT) { |
| 597 |
error = fcml_fn_ast_visit_nodes( |
| 598 |
(fcml_st_ast_node*) current->item, visitor, |
| 599 |
visitor_args); |
| 600 |
if (error) |
| 601 |
break; |
| 602 |
current = current->next; |
| 603 |
} |
| 604 |
break; |
| 605 |
} |
| 606 |
case FCML_EN_TN_EXP: { |
| 607 |
fcml_st_ast_node_exp *exp_node = |
| 608 |
(fcml_st_ast_node_exp*) ast_node->node; |
| 609 |
error = fcml_fn_ast_visit_nodes(exp_node->l, visitor, visitor_args); |
| 610 |
if (error) |
| 611 |
break; |
| 612 |
error = fcml_fn_ast_visit_nodes(exp_node->r, visitor, visitor_args); |
| 613 |
if (error) |
| 614 |
break; |
| 615 |
break; |
| 616 |
} |
| 617 |
case FCML_EN_TN_UMINUS: { |
| 618 |
fcml_st_ast_node_uminus *uminus_node = |
| 619 |
(fcml_st_ast_node_uminus*) ast_node->node; |
| 620 |
error = fcml_fn_ast_visit_nodes(uminus_node->exp, visitor, |
| 621 |
visitor_args); |
| 622 |
if (error) |
| 623 |
break; |
| 624 |
break; |
| 625 |
} |
| 626 |
case FCML_EN_TN_FAR_POINTER: { |
| 627 |
fcml_st_ast_node_far_pointer *far_pointer_node = |
| 628 |
(fcml_st_ast_node_far_pointer*) ast_node->node; |
| 629 |
error = fcml_fn_ast_visit_nodes(far_pointer_node->offset, visitor, |
| 630 |
visitor_args); |
| 631 |
if (error) |
| 632 |
break; |
| 633 |
error = fcml_fn_ast_visit_nodes(far_pointer_node->segment_selector, |
| 634 |
visitor, visitor_args); |
| 635 |
if (error) |
| 636 |
break; |
| 637 |
break; |
| 638 |
} |
| 639 |
case FCML_EN_TN_EFFECTIVE_ADDRESS: { |
| 640 |
fcml_st_ast_node_effective_address *eff_addr_node = |
| 641 |
(fcml_st_ast_node_effective_address*) ast_node->node; |
| 642 |
error = fcml_fn_ast_visit_nodes(eff_addr_node->displacement, |
| 643 |
visitor, visitor_args); |
| 644 |
if (error) |
| 645 |
break; |
| 646 |
break; |
| 647 |
} |
| 648 |
default: |
| 649 |
/* Just visit current node. */ |
| 650 |
break; |
| 651 |
} |
| 652 |
/* Visit original node. */ |
| 653 |
error = visitor(ast_node, visitor_args); |
| 654 |
} |
| 655 |
return error; |
| 656 |
} |
| 657 |
|
| 658 |
/* Visitor responsible for extracting used symbols. */ |
| 659 |
static fcml_ceh_error extract_symbols_visitor(fcml_st_ast_node *node, |
| 660 |
fcml_ptr visitor_args) { |
| 661 |
|
| 662 |
fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR; |
| 663 |
|
| 664 |
if (node->type == FCML_EN_TN_USE_SYMBOL) { |
| 665 |
fcml_st_ast_node_use_symbol *symbol_node = |
| 666 |
(fcml_st_ast_node_use_symbol*) node->node; |
| 667 |
if (symbol_node->symbol) { |
| 668 |
struct fcml_ist_ast_extracted_symbols *extracted_symbols = |
| 669 |
(struct fcml_ist_ast_extracted_symbols*) visitor_args; |
| 670 |
if (!extracted_symbols->symbols) { |
| 671 |
extracted_symbols->symbols = fcml_fn_coll_list_alloc(); |
| 672 |
if (!extracted_symbols->symbols) { |
| 673 |
return FCML_CEH_GEC_OUT_OF_MEMORY; |
| 674 |
} |
| 675 |
} |
| 676 |
if (!fcml_fn_coll_list_add_front(extracted_symbols->symbols, |
| 677 |
symbol_node->symbol)) { |
| 678 |
return FCML_CEH_GEC_OUT_OF_MEMORY; |
| 679 |
} |
| 680 |
} |
| 681 |
} |
| 682 |
|
| 683 |
return error; |
| 684 |
} |
| 685 |
|
| 686 |
/* Extracts all symbols used by whole instruction AST tree. */ |
| 687 |
fcml_ceh_error fcml_fn_ast_extract_used_symbols(fcml_st_ast_node *ast_tree, |
| 688 |
fcml_st_coll_list **extracted_symbols) { |
| 689 |
|
| 690 |
fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR; |
| 691 |
|
| 692 |
struct fcml_ist_ast_extracted_symbols symbols; |
| 693 |
symbols.symbols = NULL; |
| 694 |
|
| 695 |
error = fcml_fn_ast_visit_nodes(ast_tree, |
| 696 |
&extract_symbols_visitor, &symbols); |
| 697 |
if (error) { |
| 698 |
if (symbols.symbols) { |
| 699 |
fcml_fn_coll_list_free(symbols.symbols, NULL, NULL); |
| 700 |
symbols.symbols = NULL; |
| 701 |
} |
| 702 |
} |
| 703 |
|
| 704 |
*extracted_symbols = symbols.symbols; |
| 705 |
|
| 706 |
return error; |
| 707 |
} |
| 708 |
|
| 709 |
void fcml_fn_ast_free_converted_cif(fcml_st_instruction *cif_instruction) { |
| 710 |
if (cif_instruction) { |
| 711 |
if (cif_instruction->mnemonic) { |
| 712 |
fcml_fn_env_str_strfree(cif_instruction->mnemonic); |
| 713 |
} |
| 714 |
fcml_fn_env_memory_free(cif_instruction); |
| 715 |
} |
| 716 |
} |
| 717 |
|
| 718 |
static inline float get_float_value(fcml_st_ast_node_value *value) { |
| 719 |
return value->float_value.value; |
| 720 |
} |
| 721 |
|
| 722 |
static inline fcml_int64_t get_integer_value(fcml_st_ast_node_value *value) { |
| 723 |
return (fcml_int64_t) value->integer_value.value; |
| 724 |
} |
| 725 |
|
| 726 |
static fcml_st_ast_val_integer convert_to_val_integer(fcml_int64_t val) { |
| 727 |
fcml_st_ast_val_integer int_value; |
| 728 |
int_value.value = (fcml_uint64_t) val; |
| 729 |
int_value.is_signed = val < 0; |
| 730 |
int_value.overflow = FCML_FALSE; |
| 731 |
return int_value; |
| 732 |
} |
| 733 |
|
| 734 |
static fcml_ceh_error eval_symbol(fcml_st_cif_converter_context *context, |
| 735 |
fcml_st_ast_node_use_symbol *symbol_node, |
| 736 |
fcml_st_ast_node_value *result) { |
| 737 |
|
| 738 |
fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR; |
| 739 |
|
| 740 |
/* Get symbol definition from symbol table. */ |
| 741 |
fcml_st_symbol *symbol_def = fcml_fn_symbol_get(context->symbol_table, |
| 742 |
symbol_node->symbol); |
| 743 |
|
| 744 |
if (!symbol_def && !context->ignore_undefined_symbols) { |
| 745 |
|
| 746 |
/* Symbol is undefined. */ |
| 747 |
fcml_fn_msg_add_error_message(context->errors, |
| 748 |
FCML_MC_SEGMENT_UNDEFINED_SYMBOL, |
| 749 |
FCML_CEH_MEC_ERROR_UNDEFINED_SYMBOL, FCML_EN_CEH_EL_ERROR, |
| 750 |
symbol_node->symbol); |
| 751 |
|
| 752 |
error = FCML_CEH_GEC_UNDEFINED_SYMBOL; |
| 753 |
|
| 754 |
} else { |
| 755 |
|
| 756 |
/* Ignored symbols are treated as 0. Possibility to ignore |
| 757 |
* symbols is used in case of load-and-go assembler. |
| 758 |
*/ |
| 759 |
|
| 760 |
if (!symbol_def) { |
| 761 |
context->ignored_symbols++; |
| 762 |
} |
| 763 |
|
| 764 |
context->evaluated_symbols++; |
| 765 |
|
| 766 |
result->type = FCML_EN_ET_INTEGER; |
| 767 |
result->integer_value.is_signed = FCML_TRUE; |
| 768 |
result->integer_value.overflow = FCML_FALSE; |
| 769 |
result->integer_value.value = symbol_def ? symbol_def->value : 0L; |
| 770 |
|
| 771 |
} |
| 772 |
|
| 773 |
return error; |
| 774 |
} |
| 775 |
|
| 776 |
fcml_ceh_error fcml_ifn_ast_eval_exp(fcml_st_cif_converter_context *context, |
| 777 |
fcml_st_ast_node *exp, fcml_st_ast_node_value *result) { |
| 778 |
fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR; |
| 779 |
switch (exp->type) { |
| 780 |
case FCML_EN_TN_VALUE: { |
| 781 |
*result = *((fcml_st_ast_node_value*) exp->node); |
| 782 |
if (result->integer_value.overflow) { |
| 783 |
fcml_fn_ceh_add_error(context->errors, |
| 784 |
fcml_fn_msg_get_message(FCML_MC_CEH_GEC_VALUE_OUT_OF_RANGE), |
| 785 |
FCML_CEH_MEW_WARN_VALUE_OUT_OF_RANGE, FCML_EN_CEH_EL_WARN); |
| 786 |
} |
| 787 |
break; |
| 788 |
} |
| 789 |
case FCML_EN_TN_UMINUS: { |
| 790 |
error = fcml_ifn_ast_eval_exp(context, |
| 791 |
((fcml_st_ast_node_uminus*) exp->node)->exp, result); |
| 792 |
if (error) { |
| 793 |
return error; |
| 794 |
} |
| 795 |
if (result->type == FCML_EN_ET_INTEGER) { |
| 796 |
result->integer_value = convert_to_val_integer( |
| 797 |
-((fcml_int64_t) result->integer_value.value)); |
| 798 |
} else { |
| 799 |
result->float_value.value = -result->float_value.value; |
| 800 |
} |
| 801 |
break; |
| 802 |
} |
| 803 |
case FCML_EN_TN_USE_SYMBOL: { |
| 804 |
fcml_st_ast_node_use_symbol *symbol_node = |
| 805 |
(fcml_st_ast_node_use_symbol*) exp->node; |
| 806 |
error = eval_symbol(context, symbol_node, result); |
| 807 |
break; |
| 808 |
} |
| 809 |
case FCML_EN_TN_EXP: { |
| 810 |
fcml_st_ast_node_exp *exp_node = (fcml_st_ast_node_exp*) exp->node; |
| 811 |
fcml_st_ast_node_value lval = { (fcml_st_ast_value_node_type) 0 }; |
| 812 |
fcml_st_ast_node_value rval = { (fcml_st_ast_value_node_type) 0 }; |
| 813 |
fcml_ceh_error error = fcml_ifn_ast_eval_exp(context, exp_node->l, |
| 814 |
&lval); |
| 815 |
if (error) { |
| 816 |
return error; |
| 817 |
} |
| 818 |
error = fcml_ifn_ast_eval_exp(context, exp_node->r, &rval); |
| 819 |
if (error) { |
| 820 |
return error; |
| 821 |
} |
| 822 |
fcml_bool is_float = FCML_FALSE; |
| 823 |
result->type = FCML_EN_ET_INTEGER; |
| 824 |
if (lval.type == FCML_EN_ET_FLOAT || rval.type == FCML_EN_ET_FLOAT) { |
| 825 |
result->type = FCML_EN_ET_FLOAT; |
| 826 |
is_float = FCML_TRUE; |
| 827 |
} |
| 828 |
switch (exp_node->op) { |
| 829 |
case FCML_EN_EXN_SUB: |
| 830 |
if (is_float) { |
| 831 |
result->float_value.value = |
| 832 |
get_float_value(&lval) - get_float_value(&rval); |
| 833 |
} else { |
| 834 |
result->integer_value = convert_to_val_integer( |
| 835 |
get_integer_value(&lval) - get_integer_value(&rval)); |
| 836 |
} |
| 837 |
break; |
| 838 |
case FCML_EN_EXN_ADD: |
| 839 |
if (is_float) { |
| 840 |
result->float_value.value = |
| 841 |
get_float_value(&lval) + get_float_value(&rval); |
| 842 |
} else { |
| 843 |
result->integer_value = convert_to_val_integer( |
| 844 |
get_integer_value(&lval) + get_integer_value(&rval)); |
| 845 |
} |
| 846 |
break; |
| 847 |
case FCML_EN_EXN_MUL: |
| 848 |
if (is_float) { |
| 849 |
result->float_value.value = |
| 850 |
get_float_value(&lval) * get_float_value(&rval); |
| 851 |
} else { |
| 852 |
result->integer_value = convert_to_val_integer( |
| 853 |
get_integer_value(&lval) * get_integer_value(&rval)); |
| 854 |
} |
| 855 |
break; |
| 856 |
case FCML_EN_EXN_DIV: |
| 857 |
if (is_float) { |
| 858 |
result->float_value.value = |
| 859 |
get_float_value(&lval) / get_float_value(&rval); |
| 860 |
} else { |
| 861 |
fcml_int64_t divisor = get_integer_value(&rval); |
| 862 |
if (divisor == 0) { |
| 863 |
fcml_fn_ceh_add_error(context->errors, "Division by zero.", |
| 864 |
FCML_CEH_MEC_ERROR_DIVISION_BY_0, |
| 865 |
FCML_EN_CEH_EL_ERROR); |
| 866 |
error = FCML_CEH_GEC_INVALID_INPUT; |
| 867 |
} else { |
| 868 |
result->integer_value = convert_to_val_integer( |
| 869 |
get_integer_value(&lval) / divisor); |
| 870 |
} |
| 871 |
} |
| 872 |
break; |
| 873 |
} |
| 874 |
break; |
| 875 |
} |
| 876 |
default: |
| 877 |
FCML_TRACE("Unsupported AST node %d.", exp->type); |
| 878 |
error = FCML_CEH_GEC_INTERNAL_ERROR; |
| 879 |
break; |
| 880 |
} |
| 881 |
|
| 882 |
return error; |
| 883 |
} |
| 884 |
|
| 885 |
static fcml_ceh_error convert_value_to_immediate(fcml_st_ast_node_value *value, |
| 886 |
fcml_st_integer *immediate) { |
| 887 |
fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR; |
| 888 |
immediate->is_signed = FCML_FALSE; |
| 889 |
if (value->type == FCML_EN_ET_FLOAT) { |
| 890 |
/* Floats are not supported yet. */ |
| 891 |
error = FCML_CEH_GEC_FEATURE_NOT_SUPPORTED; |
| 892 |
} else if (value->type == FCML_EN_ET_INTEGER) { |
| 893 |
error = fcml_fn_utils_conv_int64_to_int(value->integer_value.value, |
| 894 |
value->integer_value.is_signed, immediate, FCML_ENUSF_ALL); |
| 895 |
} else { |
| 896 |
/* Unknown value type.*/ |
| 897 |
FCML_TRACE("Unknown value type: %d.", value->type); |
| 898 |
error = FCML_CEH_GEC_INTERNAL_ERROR; |
| 899 |
} |
| 900 |
return error; |
| 901 |
} |
| 902 |
|
| 903 |
static fcml_ceh_error convert_far_pointer_node_to_operand( |
| 904 |
fcml_st_cif_converter_context *context, |
| 905 |
fcml_st_ast_node_far_pointer *far_pointer_node, |
| 906 |
fcml_st_far_pointer *far_pointer_operand) { |
| 907 |
|
| 908 |
fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR; |
| 909 |
fcml_st_ast_node *segment_selector_node = |
| 910 |
far_pointer_node->segment_selector; |
| 911 |
fcml_st_ast_node *offset_node = far_pointer_node->offset; |
| 912 |
fcml_st_ast_node_value segment_selector_value; |
| 913 |
fcml_st_ast_node_value offset_value; |
| 914 |
|
| 915 |
/* Evaluate expressions.*/ |
| 916 |
if ((error = fcml_ifn_ast_eval_exp(context, segment_selector_node, |
| 917 |
&segment_selector_value))) { |
| 918 |
return error; |
| 919 |
} |
| 920 |
|
| 921 |
if ((error = fcml_ifn_ast_eval_exp(context, offset_node, &offset_value))) { |
| 922 |
return error; |
| 923 |
} |
| 924 |
|
| 925 |
/* Segment selector and offset have to be integers.*/ |
| 926 |
if (segment_selector_value.type != FCML_EN_ET_INTEGER |
| 927 |
|| offset_value.type != FCML_EN_ET_INTEGER) { |
| 928 |
/* Only integer values are supported here.*/ |
| 929 |
if (!fcml_fn_ceh_add_error(context->errors, |
| 930 |
"Segment selector and offset have to be an integer values.", |
| 931 |
FCML_CEH_MEC_ERROR_WRONG_VALUE_FORMAT, FCML_EN_CEH_EL_ERROR)) { |
| 932 |
error = FCML_CEH_GEC_OUT_OF_MEMORY; |
| 933 |
} else { |
| 934 |
error = FCML_CEH_GEC_INVALID_INPUT; |
| 935 |
} |
| 936 |
return error; |
| 937 |
} |
| 938 |
|
| 939 |
/* Set segment selector.*/ |
| 940 |
fcml_bool overflow = FCML_FALSE; |
| 941 |
|
| 942 |
if (segment_selector_value.integer_value.is_signed) { |
| 943 |
fcml_int64_t segment = |
| 944 |
(fcml_int64_t) segment_selector_value.integer_value.value; |
| 945 |
if (segment <= FCML_INT16_MAX && segment >= FCML_INT16_MIN) { |
| 946 |
far_pointer_operand->segment = (fcml_int16_t) segment; |
| 947 |
} else { |
| 948 |
overflow = FCML_TRUE; |
| 949 |
} |
| 950 |
} else { |
| 951 |
if (segment_selector_value.integer_value.value <= FCML_UINT16_MAX) { |
| 952 |
far_pointer_operand->segment = |
| 953 |
(fcml_uint16_t) segment_selector_value.integer_value.value; |
| 954 |
} else { |
| 955 |
overflow = FCML_TRUE; |
| 956 |
} |
| 957 |
} |
| 958 |
|
| 959 |
/* In case of far pointer calculation such problem is |
| 960 |
* reported as an error. |
| 961 |
*/ |
| 962 |
if (overflow) { |
| 963 |
if (!fcml_fn_ceh_add_error(context->errors, |
| 964 |
"Segment selector out of range.", |
| 965 |
FCML_CEH_MEC_ERROR_VALUE_OUT_OF_RANGE, FCML_EN_CEH_EL_ERROR)) { |
| 966 |
error = FCML_CEH_GEC_OUT_OF_MEMORY; |
| 967 |
} else { |
| 968 |
error = FCML_CEH_GEC_VALUE_OUT_OF_RANGE; |
| 969 |
} |
| 970 |
return error; |
| 971 |
} |
| 972 |
|
| 973 |
/* Set offset.*/ |
| 974 |
if (offset_value.integer_value.is_signed) { |
| 975 |
fcml_int64_t offset = (fcml_int64_t) offset_value.integer_value.value; |
| 976 |
if (offset <= FCML_INT16_MAX && offset >= FCML_INT16_MIN) { |
| 977 |
far_pointer_operand->offset16 = (fcml_int16_t) offset; |
| 978 |
far_pointer_operand->offset_size = FCML_DS_16; |
| 979 |
} else if (offset <= FCML_INT32_MAX && offset >= FCML_INT32_MIN) { |
| 980 |
far_pointer_operand->offset32 = (fcml_int32_t) offset; |
| 981 |
far_pointer_operand->offset_size = FCML_DS_32; |
| 982 |
} else { |
| 983 |
overflow = FCML_TRUE; |
| 984 |
} |
| 985 |
} else { |
| 986 |
fcml_uint64_t offset = offset_value.integer_value.value; |
| 987 |
if (offset <= FCML_UINT16_MAX) { |
| 988 |
far_pointer_operand->offset16 = |
| 989 |
(fcml_int16_t) offset_value.integer_value.value; |
| 990 |
far_pointer_operand->offset_size = FCML_DS_16; |
| 991 |
} else if (offset <= FCML_UINT32_MAX) { |
| 992 |
far_pointer_operand->offset32 = |
| 993 |
(fcml_uint32_t) offset_value.integer_value.value; |
| 994 |
far_pointer_operand->offset_size = FCML_DS_32; |
| 995 |
} else { |
| 996 |
overflow = FCML_TRUE; |
| 997 |
} |
| 998 |
} |
| 999 |
|
| 1000 |
if (overflow) { |
| 1001 |
if (!fcml_fn_ceh_add_error(context->errors, "Offset out of range.", |
| 1002 |
FCML_CEH_MEC_ERROR_VALUE_OUT_OF_RANGE, FCML_EN_CEH_EL_ERROR)) { |
| 1003 |
error = FCML_CEH_GEC_OUT_OF_MEMORY; |
| 1004 |
} else { |
| 1005 |
error = FCML_CEH_GEC_VALUE_OUT_OF_RANGE; |
| 1006 |
} |
| 1007 |
} |
| 1008 |
|
| 1009 |
return error; |
| 1010 |
} |
| 1011 |
|
| 1012 |
static fcml_ceh_error convert_effective_address_node_to_operand( |
| 1013 |
fcml_st_cif_converter_context *context, |
| 1014 |
fcml_st_instruction *cif_instruction, |
| 1015 |
fcml_st_ast_node_effective_address *effective_address_node, |
| 1016 |
fcml_en_effective_address_form address_form, fcml_st_operand *operand) { |
| 1017 |
|
| 1018 |
fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR; |
| 1019 |
|
| 1020 |
fcml_st_address *address = &(operand->address); |
| 1021 |
|
| 1022 |
/* Copy operands and instruction hints.*/ |
| 1023 |
operand->hints |= effective_address_node->addressing_hints; |
| 1024 |
cif_instruction->hints |= effective_address_node->instruction_hints; |
| 1025 |
|
| 1026 |
operand->decorators = effective_address_node->decorators; |
| 1027 |
|
| 1028 |
fcml_st_effective_address *effective_address = |
| 1029 |
&(address->effective_address); |
| 1030 |
|
| 1031 |
fcml_bool is_combined = !is_reg_undef(&(effective_address_node->base)) |
| 1032 |
|| !is_reg_undef(&(effective_address_node->index)); |
| 1033 |
|
| 1034 |
address->address_form = is_combined ? FCML_AF_COMBINED : FCML_AF_OFFSET; |
| 1035 |
|
| 1036 |
/* Evaluate displacement.*/ |
| 1037 |
if (effective_address_node->displacement) { |
| 1038 |
|
| 1039 |
fcml_st_ast_node_value value; |
| 1040 |
|
| 1041 |
if ((error = fcml_ifn_ast_eval_exp(context, |
| 1042 |
effective_address_node->displacement, &value))) { |
| 1043 |
return error; |
| 1044 |
} |
| 1045 |
|
| 1046 |
if (value.type != FCML_EN_ET_INTEGER) { |
| 1047 |
fcml_fn_ceh_add_error(context->errors, |
| 1048 |
"Displacement has to be an integer value.", |
| 1049 |
FCML_CEH_MEC_ERROR_WRONG_VALUE_FORMAT, |
| 1050 |
FCML_EN_CEH_EL_ERROR); |
| 1051 |
error = FCML_CEH_GEC_INVALID_INPUT; |
| 1052 |
return error; |
| 1053 |
} |
| 1054 |
|
| 1055 |
fcml_st_integer disp_value; |
| 1056 |
|
| 1057 |
error = fcml_fn_utils_conv_int64_to_int(value.integer_value.value, |
| 1058 |
value.integer_value.is_signed, &disp_value, |
| 1059 |
is_combined ? |
| 1060 |
FCML_ENUSF_ALL : |
| 1061 |
(fcml_en_utils_size_flags) (FCML_ENUSF_16 |
| 1062 |
| FCML_ENUSF_32 | FCML_ENUSF_64)); |
| 1063 |
if (error) { |
| 1064 |
return error; |
| 1065 |
} |
| 1066 |
|
| 1067 |
if (is_combined) { |
| 1068 |
effective_address->displacement = disp_value; |
| 1069 |
} else { |
| 1070 |
error = fcml_fn_utils_integer_to_offset(&disp_value, |
| 1071 |
&(address->offset)); |
| 1072 |
if (error) { |
| 1073 |
return error; |
| 1074 |
} |
| 1075 |
} |
| 1076 |
|
| 1077 |
} |
| 1078 |
|
| 1079 |
if (is_combined) { |
| 1080 |
|
| 1081 |
effective_address->base = effective_address_node->base; |
| 1082 |
effective_address->index = effective_address_node->index; |
| 1083 |
|
| 1084 |
fcml_uint32_t scale_factor = |
| 1085 |
(fcml_uint32_t) effective_address_node->scale_factor.value; |
| 1086 |
if ((scale_factor == 0 || scale_factor == 1 || scale_factor == 2 |
| 1087 |
|| scale_factor == 4 || scale_factor == 8)) { |
| 1088 |
effective_address->scale_factor = (fcml_uint8_t) scale_factor; |
| 1089 |
} else { |
| 1090 |
fcml_fn_ceh_add_error(context->errors, "Wrong scale factor value.", |
| 1091 |
FCML_CEH_MEC_ERROR_WRONG_VALUE_FORMAT, |
| 1092 |
FCML_EN_CEH_EL_ERROR); |
| 1093 |
return FCML_CEH_GEC_INVALID_INPUT; |
| 1094 |
} |
| 1095 |
|
| 1096 |
} |
| 1097 |
|
| 1098 |
address->segment_selector.segment_selector = |
| 1099 |
effective_address_node->segment_selector; |
| 1100 |
address->size_operator = effective_address_node->size_operator; |
| 1101 |
|
| 1102 |
return error; |
| 1103 |
} |
| 1104 |
|
| 1105 |
static fcml_ceh_error handle_ast_node(fcml_st_cif_converter_context *context, |
| 1106 |
fcml_st_instruction *cif_instruction, fcml_st_operand *current_operand, |
| 1107 |
fcml_st_ast_node *ast_node) { |
| 1108 |
|
| 1109 |
fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR; |
| 1110 |
|
| 1111 |
switch (ast_node->type) { |
| 1112 |
case FCML_EN_TN_INSTRUCTION: { |
| 1113 |
fcml_st_ast_node_instruction *node_instruction = |
| 1114 |
(fcml_st_ast_node_instruction*) ast_node->node; |
| 1115 |
/* Handle mnemonic.*/ |
| 1116 |
cif_instruction->prefixes = node_instruction->prefixes; |
| 1117 |
cif_instruction->hints |= node_instruction->hints; |
| 1118 |
cif_instruction->mnemonic = fcml_fn_env_str_strdup( |
| 1119 |
node_instruction->mnemonic); |
| 1120 |
if (cif_instruction->mnemonic) { |
| 1121 |
/* Handle operands.*/ |
| 1122 |
if (node_instruction->operands) { |
| 1123 |
error = handle_ast_node(context, cif_instruction, |
| 1124 |
NULL, node_instruction->operands); |
| 1125 |
} |
| 1126 |
} else { |
| 1127 |
error = FCML_CEH_GEC_OUT_OF_MEMORY; |
| 1128 |
} |
| 1129 |
break; |
| 1130 |
} |
| 1131 |
case FCML_EN_TN_OPERAND_LIST: { |
| 1132 |
fcml_st_ast_node_operand_list *operand_list = |
| 1133 |
(fcml_st_ast_node_operand_list*) ast_node->node; |
| 1134 |
if (operand_list->operands->size > FCML_OPERANDS_COUNT) { |
| 1135 |
fcml_fn_ceh_add_error(context->errors, "Too many operands.", |
| 1136 |
FCML_CEH_MEC_ERROR_TO_MANY_OPERANDS, FCML_EN_CEH_EL_ERROR); |
| 1137 |
error = FCML_CEH_GEC_INVALID_INPUT; |
| 1138 |
} else { |
| 1139 |
fcml_st_coll_list_element *current = operand_list->operands->head; |
| 1140 |
int i = 0; |
| 1141 |
while (current && i < FCML_OPERANDS_COUNT) { |
| 1142 |
error = handle_ast_node(context, cif_instruction, |
| 1143 |
&(cif_instruction->operands[i++]), |
| 1144 |
(fcml_st_ast_node*) current->item); |
| 1145 |
if (error) { |
| 1146 |
return error; |
| 1147 |
} |
| 1148 |
current = current->next; |
| 1149 |
} |
| 1150 |
cif_instruction->operands_count = operand_list->operands->size; |
| 1151 |
} |
| 1152 |
break; |
| 1153 |
} |
| 1154 |
case FCML_EN_TN_USE_SYMBOL: |
| 1155 |
case FCML_EN_TN_EXP: |
| 1156 |
case FCML_EN_TN_VALUE: |
| 1157 |
case FCML_EN_TN_UMINUS: { |
| 1158 |
fcml_st_ast_node_value value; |
| 1159 |
error = fcml_ifn_ast_eval_exp(context, ast_node, &value); |
| 1160 |
if (!error) { |
| 1161 |
if (current_operand != NULL) { |
| 1162 |
/* Convert value to immediate operand.*/ |
| 1163 |
error = convert_value_to_immediate(&value, |
| 1164 |
&(current_operand->immediate)); |
| 1165 |
if (!error) { |
| 1166 |
current_operand->type = FCML_OT_IMMEDIATE; |
| 1167 |
} |
| 1168 |
} else { |
| 1169 |
FCML_TRACE_MSG("Operand is mandatory here."); |
| 1170 |
error = FCML_CEH_GEC_INTERNAL_ERROR; |
| 1171 |
} |
| 1172 |
} |
| 1173 |
break; |
| 1174 |
} |
| 1175 |
case FCML_EN_TN_VIRTUAL: { |
| 1176 |
fcml_st_ast_node_virtual *node_virtual = |
| 1177 |
(fcml_st_ast_node_virtual*) ast_node->node; |
| 1178 |
current_operand->type = FCML_OT_VIRTUAL; |
| 1179 |
current_operand->decorators = node_virtual->decorators; |
| 1180 |
break; |
| 1181 |
} |
| 1182 |
case FCML_EN_TN_REG: { |
| 1183 |
fcml_st_ast_node_register *node_register = |
| 1184 |
(fcml_st_ast_node_register*) ast_node->node; |
| 1185 |
current_operand->reg = node_register->reg; |
| 1186 |
current_operand->type = FCML_OT_REGISTER; |
| 1187 |
current_operand->decorators = node_register->decorators; |
| 1188 |
break; |
| 1189 |
} |
| 1190 |
case FCML_EN_TN_FAR_POINTER: { |
| 1191 |
fcml_st_ast_node_far_pointer *far_pointer_node = |
| 1192 |
(fcml_st_ast_node_far_pointer*) ast_node->node; |
| 1193 |
error = convert_far_pointer_node_to_operand(context, |
| 1194 |
far_pointer_node, &(current_operand->far_pointer)); |
| 1195 |
current_operand->type = FCML_OT_FAR_POINTER; |
| 1196 |
break; |
| 1197 |
} |
| 1198 |
case FCML_EN_TN_EFFECTIVE_ADDRESS: { |
| 1199 |
fcml_st_ast_node_effective_address *effective_address_node = |
| 1200 |
(fcml_st_ast_node_effective_address*) ast_node->node; |
| 1201 |
error = convert_effective_address_node_to_operand( |
| 1202 |
context, cif_instruction, effective_address_node, |
| 1203 |
effective_address_node->address_form, current_operand); |
| 1204 |
current_operand->type = FCML_OT_ADDRESS; |
| 1205 |
break; |
| 1206 |
} |
| 1207 |
default: |
| 1208 |
/* Unsupported node type. */ |
| 1209 |
error = FCML_CEH_GEC_INTERNAL_ERROR; |
| 1210 |
break; |
| 1211 |
} |
| 1212 |
|
| 1213 |
return error; |
| 1214 |
} |
| 1215 |
|
| 1216 |
fcml_ceh_error fcml_fn_ast_to_cif_converter( |
| 1217 |
fcml_st_cif_converter_context *context, |
| 1218 |
fcml_st_ast_node *ast_instruction_node, |
| 1219 |
fcml_st_instruction **out_instruction) { |
| 1220 |
fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR; |
| 1221 |
context->evaluated_symbols = 0; |
| 1222 |
fcml_st_instruction *instruction = |
| 1223 |
(fcml_st_instruction*) fcml_fn_env_memory_alloc_clear( |
| 1224 |
sizeof(fcml_st_instruction)); |
| 1225 |
if (instruction) { |
| 1226 |
error = handle_ast_node(context, instruction, NULL, |
| 1227 |
ast_instruction_node); |
| 1228 |
if (error) { |
| 1229 |
fcml_fn_ast_free_converted_cif(instruction); |
| 1230 |
} else { |
| 1231 |
*out_instruction = instruction; |
| 1232 |
} |
| 1233 |
} |
| 1234 |
return error; |
| 1235 |
} |