| 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 <fcml_lag_assembler.h> |
| 21 |
#include <fcml_parser.h> |
| 22 |
#include <fcml_errors.h> |
| 23 |
|
| 24 |
#include "fcml_parser_int.h" |
| 25 |
#include "fcml_assembler_int.h" |
| 26 |
#include "fcml_env_int.h" |
| 27 |
#include "fcml_coll.h" |
| 28 |
#include "fcml_apc_ast.h" |
| 29 |
#include "fcml_messages.h" |
| 30 |
#include "fcml_utils.h" |
| 31 |
|
| 32 |
typedef struct lag_pass_holder { |
| 33 |
/* Pass number. */ |
| 34 |
fcml_int pass; |
| 35 |
} lag_pass_holder; |
| 36 |
|
| 37 |
/* Holds information about last symbol value modification. */ |
| 38 |
typedef struct symbol_state { |
| 39 |
/* Name of the symbol. */ |
| 40 |
fcml_string symbol_name; |
| 41 |
/* Pass which modified symbol. */ |
| 42 |
fcml_int pass; |
| 43 |
} symbol_state; |
| 44 |
|
| 45 |
typedef struct lag_instruction { |
| 46 |
fcml_ip ip; |
| 47 |
/* Next instruction. */ |
| 48 |
struct lag_instruction *next; |
| 49 |
/* If instruction has symbols, we have its AST just to reassemble it when needed. */ |
| 50 |
fcml_st_parser_ast ast; |
| 51 |
/* List of symbols used by instruction. Every symbol is represented by its name (list of fcml_string). */ |
| 52 |
fcml_st_coll_list *used_symbols; |
| 53 |
/* Number of undefined symbols when instruction was assembled last time. */ |
| 54 |
fcml_int undefined_symbols; |
| 55 |
/* Assembled instruction. */ |
| 56 |
fcml_st_assembled_instruction *instruction; |
| 57 |
/* Line number where instruction is defined. */ |
| 58 |
fcml_int line; |
| 59 |
/* Generated instruction can not be shorter than */ |
| 60 |
fcml_int size_guard; |
| 61 |
} lag_instruction; |
| 62 |
|
| 63 |
/* Details about processing errors. */ |
| 64 |
typedef struct lag_error_details { |
| 65 |
/* Errors. */ |
| 66 |
fcml_st_ceh_error_container errors; |
| 67 |
/* Line where assembling failed. */ |
| 68 |
fcml_int line; |
| 69 |
} lag_error_details; |
| 70 |
|
| 71 |
/* Context shared between multiple passes. */ |
| 72 |
typedef struct lag_asseblation_context { |
| 73 |
/* Parsed/Converted/Assembled instructions. */ |
| 74 |
lag_instruction *first_instruction; |
| 75 |
lag_instruction *last_instruction; |
| 76 |
/* One line assembler used to assemble code. */ |
| 77 |
fcml_st_assembler_context assembler_context; |
| 78 |
/* Global errors going here. */ |
| 79 |
lag_error_details error_details; |
| 80 |
} lag_asseblation_context; |
| 81 |
|
| 82 |
/* Returns true if any of the symbols from given list exists in the state map. */ |
| 83 |
static fcml_bool is_any_symbol_modified(fcml_coll_map symbol_state_map, |
| 84 |
fcml_st_coll_list *symbol_names) { |
| 85 |
if (symbol_names) { |
| 86 |
fcml_st_coll_list_element *element = symbol_names->head; |
| 87 |
while (element) { |
| 88 |
fcml_string symbol_name = (fcml_string) element->item; |
| 89 |
if (fcml_fn_coll_map_get(symbol_state_map, symbol_name)) { |
| 90 |
return FCML_TRUE; |
| 91 |
} |
| 92 |
element = element->next; |
| 93 |
} |
| 94 |
} |
| 95 |
return FCML_FALSE; |
| 96 |
} |
| 97 |
|
| 98 |
/* Frees symbol state structure used by second pass and later. */ |
| 99 |
static void map_symbol_state_free(fcml_ptr key, fcml_ptr value, |
| 100 |
fcml_ptr args) { |
| 101 |
if (value) { |
| 102 |
/* Symbol name is managed by parser context, so it can not |
| 103 |
* be free'd here. */ |
| 104 |
symbol_state *symbol = (symbol_state*) value; |
| 105 |
fcml_fn_env_memory_free(symbol); |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
/* Decides if symbol state has to be removed from state stable. */ |
| 110 |
static fcml_bool map_prev_pass_symbol_if(fcml_ptr key, fcml_ptr value, |
| 111 |
fcml_ptr args) { |
| 112 |
fcml_bool remove = FCML_FALSE; |
| 113 |
lag_pass_holder *pass_holder = (lag_pass_holder*) args; |
| 114 |
if (value && pass_holder) { |
| 115 |
symbol_state *symbol = (symbol_state*) value; |
| 116 |
if (symbol->pass < pass_holder->pass) { |
| 117 |
remove = FCML_TRUE; |
| 118 |
} |
| 119 |
} |
| 120 |
return remove; |
| 121 |
} |
| 122 |
|
| 123 |
/* Adds symbol status to the map. */ |
| 124 |
static fcml_ceh_error add_symbol_state(fcml_coll_map symbol_state_map, |
| 125 |
fcml_string symbol, fcml_int pass) { |
| 126 |
symbol_state *state = |
| 127 |
(symbol_state*) fcml_fn_coll_map_get(symbol_state_map, |
| 128 |
symbol); |
| 129 |
if (state) { |
| 130 |
state->pass = pass; |
| 131 |
} else { |
| 132 |
state = (symbol_state*) fcml_fn_env_memory_alloc( |
| 133 |
sizeof(symbol_state)); |
| 134 |
if (!state) { |
| 135 |
return FCML_CEH_GEC_OUT_OF_MEMORY; |
| 136 |
} |
| 137 |
state->pass = pass; |
| 138 |
state->symbol_name = symbol; |
| 139 |
fcml_int map_error = 0; |
| 140 |
fcml_fn_coll_map_put(symbol_state_map, symbol, state, &map_error); |
| 141 |
if (map_error) { |
| 142 |
/* Can not add symbol to the map. */ |
| 143 |
fcml_fn_env_memory_free(state); |
| 144 |
return FCML_CEH_GEC_OUT_OF_MEMORY; |
| 145 |
} |
| 146 |
} |
| 147 |
return FCML_CEH_GEC_NO_ERROR; |
| 148 |
} |
| 149 |
|
| 150 |
/* Allocates map used as fast-access storage for symbols statuses. */ |
| 151 |
static fcml_coll_map alloc_symbol_state_map() { |
| 152 |
fcml_st_coll_map_descriptor map_descriptor = fcml_coll_map_descriptor_string; |
| 153 |
map_descriptor.entry_free_function = &map_symbol_state_free; |
| 154 |
fcml_int map_error = 0; |
| 155 |
return fcml_fn_coll_map_alloc(&map_descriptor, 10, &map_error); |
| 156 |
} |
| 157 |
|
| 158 |
/* Removes all symbols from previous passes. */ |
| 159 |
static void clean_symbol_state_map(fcml_coll_map symbol_state_map, |
| 160 |
fcml_int pass) { |
| 161 |
lag_pass_holder holder; |
| 162 |
holder.pass = pass; |
| 163 |
fcml_fn_coll_map_remove_if(symbol_state_map, |
| 164 |
&map_prev_pass_symbol_if, &holder); |
| 165 |
} |
| 166 |
|
| 167 |
/* Adds new instruction to the processing context. */ |
| 168 |
static lag_instruction* instruction_to_context( |
| 169 |
lag_asseblation_context *processing_ctx) { |
| 170 |
FCML_ENV_ALLOC_CLEAR(instruction, lag_instruction); |
| 171 |
if (!instruction) { |
| 172 |
return NULL; |
| 173 |
} |
| 174 |
if (!processing_ctx->first_instruction) { |
| 175 |
processing_ctx->first_instruction = instruction; |
| 176 |
processing_ctx->last_instruction = instruction; |
| 177 |
} else { |
| 178 |
processing_ctx->last_instruction->next = instruction; |
| 179 |
processing_ctx->last_instruction = instruction; |
| 180 |
} |
| 181 |
return instruction; |
| 182 |
} |
| 183 |
|
| 184 |
/* Free lag instruction. */ |
| 185 |
static void free_instruction(lag_instruction *instruction, |
| 186 |
fcml_st_symbol_table symbol_table) { |
| 187 |
if (instruction) { |
| 188 |
lag_instruction *current = instruction; |
| 189 |
while (current) { |
| 190 |
|
| 191 |
lag_instruction *next = current->next; |
| 192 |
|
| 193 |
/* It is really important! Remember that the same symbol is stored |
| 194 |
* in the internal symbol table, and it's symbol table that is |
| 195 |
* responsible for freeing it. |
| 196 |
*/ |
| 197 |
if (current->ast.symbol && symbol_table) { |
| 198 |
/* Anyway, if symbol table is given, remove symbol from it here. */ |
| 199 |
fcml_fn_symbol_remove(symbol_table, |
| 200 |
current->ast.symbol->symbol); |
| 201 |
} |
| 202 |
current->ast.symbol = NULL; |
| 203 |
/* Free everything else. */ |
| 204 |
fcml_fn_parser_free_ast(&(current->ast)); |
| 205 |
/* Free assembled instruction. */ |
| 206 |
if (current->instruction) { |
| 207 |
fcml_fn_assembler_instruction_free(current->instruction); |
| 208 |
} |
| 209 |
/* Free symbols list but leave symbols themselves alone. */ |
| 210 |
if (current->used_symbols) { |
| 211 |
fcml_fn_coll_list_free(current->used_symbols, NULL, NULL); |
| 212 |
} |
| 213 |
fcml_fn_env_memory_free(current); |
| 214 |
|
| 215 |
current = next; |
| 216 |
} |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
static fcml_st_assembled_instruction* choose_second_pass_best_inst( |
| 221 |
lag_instruction *lag_instruction, |
| 222 |
fcml_st_assembler_result *assembler_result) { |
| 223 |
|
| 224 |
fcml_st_assembled_instruction **instructions = |
| 225 |
&(assembler_result->instructions); |
| 226 |
fcml_st_assembled_instruction *choosen_instruction = |
| 227 |
assembler_result->chosen_instruction; |
| 228 |
|
| 229 |
fcml_int guard = lag_instruction->size_guard; |
| 230 |
|
| 231 |
if (!lag_instruction->undefined_symbols && guard > 0) { |
| 232 |
fcml_st_assembled_instruction *chosen_instruction = NULL; |
| 233 |
/* Choose the longest form. */ |
| 234 |
fcml_st_assembled_instruction *current = *instructions; |
| 235 |
while (current) { |
| 236 |
if (!chosen_instruction && guard <= current->code_length) { |
| 237 |
chosen_instruction = current; |
| 238 |
} else if (current->code_length > chosen_instruction->code_length |
| 239 |
&& guard <= current->code_length) { |
| 240 |
chosen_instruction = current; |
| 241 |
} |
| 242 |
current = current->next; |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
if (!lag_instruction->undefined_symbols) { |
| 247 |
if (choosen_instruction->code_length |
| 248 |
> lag_instruction->instruction->code_length) { |
| 249 |
lag_instruction->size_guard = choosen_instruction->code_length; |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
/* Detach chosen instruction. */ |
| 254 |
fcml_fn_assembler_instruction_detach(instructions, choosen_instruction); |
| 255 |
|
| 256 |
assembler_result->number_of_instructions--; |
| 257 |
|
| 258 |
return choosen_instruction; |
| 259 |
} |
| 260 |
|
| 261 |
/* Chooses best instruction as a result for the first pass. */ |
| 262 |
static fcml_st_assembled_instruction* choose_first_pass_best_instruction( |
| 263 |
fcml_int ignored_symbols, fcml_st_assembler_result *assembler_result) { |
| 264 |
|
| 265 |
fcml_st_assembled_instruction *chosen_instruction = NULL; |
| 266 |
|
| 267 |
if (ignored_symbols > 0) { |
| 268 |
/* Choose the longest form. */ |
| 269 |
fcml_st_assembled_instruction *current = assembler_result->instructions; |
| 270 |
while (current) { |
| 271 |
if (!chosen_instruction) { |
| 272 |
chosen_instruction = current; |
| 273 |
} else if (current->code_length > chosen_instruction->code_length) { |
| 274 |
chosen_instruction = current; |
| 275 |
} |
| 276 |
current = current->next; |
| 277 |
} |
| 278 |
} else { |
| 279 |
/* Default chooser chooses the shortest form for us. */ |
| 280 |
chosen_instruction = assembler_result->chosen_instruction; |
| 281 |
} |
| 282 |
|
| 283 |
/* Detach instruction to avoid its deallocation. */ |
| 284 |
fcml_fn_assembler_instruction_detach(&(assembler_result->instructions), |
| 285 |
chosen_instruction); |
| 286 |
|
| 287 |
assembler_result->number_of_instructions--; |
| 288 |
|
| 289 |
return chosen_instruction; |
| 290 |
} |
| 291 |
|
| 292 |
/* First pass of the load-and-go assembler. */ |
| 293 |
static fcml_ceh_error assembler_pass_1( |
| 294 |
fcml_st_lag_assembler_context *context, const fcml_string *source_code, |
| 295 |
lag_asseblation_context *processing_ctx, |
| 296 |
fcml_bool *invoke_next_pass) { |
| 297 |
|
| 298 |
fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR; |
| 299 |
|
| 300 |
fcml_st_assembler_context *assembler_context = |
| 301 |
&(processing_ctx->assembler_context); |
| 302 |
|
| 303 |
/* Parses instructions one by one. Every parsed instruction is |
| 304 |
* then converted to CIF and assembled. Assembler should return |
| 305 |
* all allowed forms of assembled instruction. If all symbols |
| 306 |
* were known, the shortest instruction for is chosen; otherwise |
| 307 |
* the longest one. |
| 308 |
*/ |
| 309 |
fcml_st_parser_context parser_context = { 0 }; |
| 310 |
parser_context.configuration.ignore_undefined_symbols = FCML_TRUE; |
| 311 |
parser_context.configuration.override_labels = FCML_FALSE; |
| 312 |
parser_context.configuration.disable_symbols_declaration = FCML_FALSE; |
| 313 |
parser_context.configuration.alloc_symbol_table_if_needed = FCML_FALSE; |
| 314 |
parser_context.ip = assembler_context->entry_point.ip; |
| 315 |
parser_context.dialect = fcml_fn_assembler_extract_dialect( |
| 316 |
assembler_context->assembler); |
| 317 |
|
| 318 |
fcml_st_assembler_result assembler_result; |
| 319 |
fcml_fn_assembler_result_prepare(&assembler_result); |
| 320 |
|
| 321 |
fcml_st_parser_result parser_result; |
| 322 |
fcml_fn_parser_result_prepare(&parser_result); |
| 323 |
|
| 324 |
fcml_int line = 0; |
| 325 |
|
| 326 |
const fcml_string instruction; |
| 327 |
|
| 328 |
/* Parse source code, line by line. */ |
| 329 |
while ((instruction = source_code[line])) { |
| 330 |
|
| 331 |
processing_ctx->error_details.line = line; |
| 332 |
|
| 333 |
/* Check if line is blank. */ |
| 334 |
|
| 335 |
fcml_int length = fcml_fn_env_str_strlen(instruction); |
| 336 |
if (length > FCML_PARSER_MAX_INSTRUCTION_LEN) { |
| 337 |
fcml_fn_ceh_add_error(&(processing_ctx->error_details.errors), |
| 338 |
fcml_fn_msg_get_message( |
| 339 |
FCML_MC_SEGMENT_PARSED_LINE_TO_LONG), |
| 340 |
FCML_CEH_MEC_ERROR_PARSED_LINE_TOO_LONG, |
| 341 |
FCML_EN_CEH_EL_ERROR); |
| 342 |
error = FCML_CEH_GEC_INVALID_INPUT; |
| 343 |
break; |
| 344 |
} |
| 345 |
|
| 346 |
/* Empty lines should be ignored. */ |
| 347 |
if (!fcml_fn_env_str_strblank(instruction)) { |
| 348 |
|
| 349 |
/* Allocate space for current instruction. */ |
| 350 |
lag_instruction *lag_instruction = |
| 351 |
instruction_to_context(processing_ctx); |
| 352 |
if (!lag_instruction) { |
| 353 |
break; |
| 354 |
} |
| 355 |
|
| 356 |
fcml_st_parser_ast *ast = &(lag_instruction->ast); |
| 357 |
|
| 358 |
/* Parse instruction to AST. */ |
| 359 |
error = fcml_fn_parse_to_ast(&parser_context, instruction, ast); |
| 360 |
if (error) { |
| 361 |
fcml_fn_ceh_move_errors(&(processing_ctx->error_details.errors), |
| 362 |
&(ast->errors)); |
| 363 |
break; |
| 364 |
} |
| 365 |
|
| 366 |
/* There is a symbol definition, so store it in the symbol table. */ |
| 367 |
if (ast->symbol) { |
| 368 |
|
| 369 |
/* Check if the symbol is already defined. */ |
| 370 |
if (fcml_fn_symbol_get(context->symbol_table, |
| 371 |
ast->symbol->symbol)) { |
| 372 |
fcml_fn_msg_add_error_message( |
| 373 |
&(processing_ctx->error_details.errors), |
| 374 |
FCML_MC_SEGMENT_SYMBOL_ALREADY_DEFINED, |
| 375 |
FCML_CEH_MEC_ERROR_SYMBOL_ALREADY_DEFINED, |
| 376 |
FCML_EN_CEH_EL_ERROR, ast->symbol->symbol); |
| 377 |
/* Already defined. It doesn't finish up inside symbol table, which should free it later, so |
| 378 |
* we have to free it manually here. |
| 379 |
*/ |
| 380 |
fcml_fn_symbol_free(ast->symbol); |
| 381 |
ast->symbol = NULL; |
| 382 |
error = FCML_CEH_GEC_INVALID_INPUT; |
| 383 |
break; |
| 384 |
} |
| 385 |
|
| 386 |
/* Take into account that the same symbol is hold by AST and symbol table. |
| 387 |
* It is detached from AST when AST is free'd and in result symbol table |
| 388 |
* is responsible for freeing it. |
| 389 |
*/ |
| 390 |
error = fcml_fn_symbol_add(context->symbol_table, ast->symbol); |
| 391 |
if (error) { |
| 392 |
/* It doesn't finish up inside symbol table, which should free |
| 393 |
* it later, so we have to free it manually here. |
| 394 |
*/ |
| 395 |
fcml_fn_symbol_free(ast->symbol); |
| 396 |
ast->symbol = NULL; |
| 397 |
break; |
| 398 |
} |
| 399 |
|
| 400 |
} |
| 401 |
|
| 402 |
lag_instruction->line = line; |
| 403 |
|
| 404 |
fcml_int code_length = 0; |
| 405 |
|
| 406 |
/* Check if there is any instruction to assemble. */ |
| 407 |
if (ast->tree) { |
| 408 |
|
| 409 |
/* Convert AST to common instruction format. */ |
| 410 |
|
| 411 |
fcml_st_cif_converter_context cif_context = { 0 }; |
| 412 |
cif_context.ignore_undefined_symbols = FCML_TRUE; |
| 413 |
cif_context.symbol_table = context->symbol_table; |
| 414 |
|
| 415 |
fcml_st_instruction *cif_instruction; |
| 416 |
|
| 417 |
error = fcml_fn_ast_to_cif_converter(&cif_context, ast->tree, |
| 418 |
&cif_instruction); |
| 419 |
if (error) { |
| 420 |
/* Should never happened, because we ignore undefined symbols. */ |
| 421 |
break; |
| 422 |
} |
| 423 |
|
| 424 |
/* Instruction uses symbols, so extract it just in order to make |
| 425 |
* them visible later in second stage, because we have to reassemble |
| 426 |
* instructions which use modified symbols. |
| 427 |
*/ |
| 428 |
if (cif_context.evaluated_symbols > 0) { |
| 429 |
error = fcml_fn_ast_extract_used_symbols(ast->tree, |
| 430 |
&(lag_instruction->used_symbols)); |
| 431 |
if (error) { |
| 432 |
fcml_fn_ast_free_converted_cif(cif_instruction); |
| 433 |
break; |
| 434 |
} |
| 435 |
} |
| 436 |
|
| 437 |
lag_instruction->undefined_symbols = |
| 438 |
cif_context.ignored_symbols; |
| 439 |
|
| 440 |
/* There are undefined symbols, so next pass is definitely needed. */ |
| 441 |
if (cif_context.ignored_symbols > 0) { |
| 442 |
*invoke_next_pass = FCML_TRUE; |
| 443 |
} |
| 444 |
|
| 445 |
/* Assemble instruction. */ |
| 446 |
assembler_context->entry_point.ip = parser_context.ip; |
| 447 |
|
| 448 |
/* In case of first pass even if instruction fails, we will try again in the |
| 449 |
* next pass, because it might fail because of some calculations that take |
| 450 |
* place after choosing default value for undefined symbols. |
| 451 |
*/ |
| 452 |
error = fcml_fn_assemble(assembler_context, cif_instruction, |
| 453 |
&assembler_result); |
| 454 |
if (!error) { |
| 455 |
|
| 456 |
/* Choose the best instruction. */ |
| 457 |
fcml_st_assembled_instruction *chosen_instruction = |
| 458 |
choose_first_pass_best_instruction( |
| 459 |
cif_context.ignored_symbols, |
| 460 |
&assembler_result); |
| 461 |
|
| 462 |
code_length += chosen_instruction->code_length; |
| 463 |
|
| 464 |
lag_instruction->instruction = chosen_instruction; |
| 465 |
|
| 466 |
} else { |
| 467 |
error = FCML_CEH_GEC_NO_ERROR; |
| 468 |
} |
| 469 |
|
| 470 |
/* Free instruction model. */ |
| 471 |
fcml_fn_ast_free_converted_cif(cif_instruction); |
| 472 |
|
| 473 |
} |
| 474 |
|
| 475 |
lag_instruction->ip = parser_context.ip; |
| 476 |
|
| 477 |
/* Increment instruction pointer. */ |
| 478 |
|
| 479 |
parser_context.ip += code_length; |
| 480 |
} |
| 481 |
|
| 482 |
line++; |
| 483 |
} |
| 484 |
|
| 485 |
fcml_fn_assembler_result_free(&assembler_result); |
| 486 |
|
| 487 |
fcml_fn_parser_result_free(&parser_result); |
| 488 |
|
| 489 |
return error; |
| 490 |
|
| 491 |
} |
| 492 |
|
| 493 |
static fcml_ceh_error assembler_pass_2_to_n( |
| 494 |
fcml_st_lag_assembler_context *context, |
| 495 |
lag_asseblation_context *processing_ctx) { |
| 496 |
|
| 497 |
fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR; |
| 498 |
|
| 499 |
fcml_st_assembler_context *assembler_context = |
| 500 |
&(processing_ctx->assembler_context); |
| 501 |
|
| 502 |
fcml_st_assembler_result assembler_result; |
| 503 |
fcml_fn_assembler_result_prepare(&assembler_result); |
| 504 |
|
| 505 |
/* Allocate map for information about symbols modifications. */ |
| 506 |
fcml_coll_map symbol_state_map = alloc_symbol_state_map(); |
| 507 |
if (!symbol_state_map) { |
| 508 |
return FCML_CEH_GEC_OUT_OF_MEMORY; |
| 509 |
} |
| 510 |
|
| 511 |
/* Passes counter. */ |
| 512 |
fcml_int pass = 2; |
| 513 |
|
| 514 |
fcml_bool invoke_next_pass = FCML_TRUE; |
| 515 |
|
| 516 |
/* Main loop of second and further passes. */ |
| 517 |
while (!error && invoke_next_pass) { |
| 518 |
|
| 519 |
lag_instruction *lag_instruction = |
| 520 |
processing_ctx->first_instruction; |
| 521 |
|
| 522 |
fcml_ip ip_disp = 0; |
| 523 |
|
| 524 |
while (lag_instruction) { |
| 525 |
|
| 526 |
processing_ctx->error_details.line = lag_instruction->line; |
| 527 |
|
| 528 |
lag_instruction->ip += ip_disp; |
| 529 |
|
| 530 |
fcml_st_symbol *def_symbol = lag_instruction->ast.symbol; |
| 531 |
|
| 532 |
/* Update defined symbol if there is such need. */ |
| 533 |
|
| 534 |
if (def_symbol && ip_disp != 0) { |
| 535 |
|
| 536 |
def_symbol->value += ip_disp; |
| 537 |
|
| 538 |
/* Adds symbol to the map of recently modified symbols. */ |
| 539 |
error = add_symbol_state(symbol_state_map, |
| 540 |
def_symbol->symbol, pass); |
| 541 |
if (error) { |
| 542 |
break; |
| 543 |
} |
| 544 |
|
| 545 |
} |
| 546 |
|
| 547 |
/* Instruction has to be reassembled in three cases. If it ignored |
| 548 |
* any undefined symbol in the first phase, if it uses any symbol |
| 549 |
* which is marked as recently modified and the last case if instruction |
| 550 |
* IP has been modified and instruction uses symbols. |
| 551 |
*/ |
| 552 |
if (lag_instruction->ast.tree |
| 553 |
&& (!lag_instruction->instruction |
| 554 |
|| lag_instruction->undefined_symbols |
| 555 |
|| (lag_instruction->used_symbols && ip_disp) |
| 556 |
|| is_any_symbol_modified( |
| 557 |
symbol_state_map, |
| 558 |
lag_instruction->used_symbols))) { |
| 559 |
|
| 560 |
fcml_st_instruction *cif_instruction; |
| 561 |
|
| 562 |
fcml_st_cif_converter_context cif_context = { 0 }; |
| 563 |
cif_context.ignore_undefined_symbols = FCML_TRUE; |
| 564 |
cif_context.symbol_table = context->symbol_table; |
| 565 |
|
| 566 |
/* Regenerate instruction with new/modified symbols. */ |
| 567 |
|
| 568 |
error = fcml_fn_ast_to_cif_converter(&cif_context, |
| 569 |
lag_instruction->ast.tree, &cif_instruction); |
| 570 |
if (error) { |
| 571 |
break; |
| 572 |
} |
| 573 |
|
| 574 |
lag_instruction->undefined_symbols = |
| 575 |
cif_context.ignored_symbols; |
| 576 |
if (lag_instruction->undefined_symbols) { |
| 577 |
fcml_fn_ast_free_converted_cif(cif_instruction); |
| 578 |
/* Find undefined symbol. */ |
| 579 |
fcml_st_coll_list_element *element = |
| 580 |
lag_instruction->used_symbols->head; |
| 581 |
while (element) { |
| 582 |
fcml_string symbol = (fcml_string) element->item; |
| 583 |
if (fcml_fn_symbol_get(context->symbol_table, |
| 584 |
symbol) == NULL) { |
| 585 |
fcml_fn_msg_add_error_message( |
| 586 |
&(processing_ctx->error_details.errors), |
| 587 |
FCML_MC_SEGMENT_UNDEFINED_SYMBOL, |
| 588 |
FCML_CEH_MEC_ERROR_UNDEFINED_SYMBOL, |
| 589 |
FCML_EN_CEH_EL_ERROR, symbol); |
| 590 |
} |
| 591 |
element = element->next; |
| 592 |
} |
| 593 |
/* Undefined symbol. */ |
| 594 |
error = FCML_CEH_GEC_UNDEFINED_SYMBOL; |
| 595 |
break; |
| 596 |
} |
| 597 |
|
| 598 |
/* Assemble instruction. */ |
| 599 |
assembler_context->entry_point.ip = |
| 600 |
(fcml_ip) lag_instruction->ip; |
| 601 |
|
| 602 |
error = fcml_fn_assemble(assembler_context, cif_instruction, |
| 603 |
&assembler_result); |
| 604 |
if (error) { |
| 605 |
/* Copy potential error messages. */ |
| 606 |
fcml_fn_ceh_move_errors( |
| 607 |
&(processing_ctx->error_details.errors), |
| 608 |
&(assembler_result.errors)); |
| 609 |
fcml_fn_ast_free_converted_cif(cif_instruction); |
| 610 |
break; |
| 611 |
} |
| 612 |
|
| 613 |
fcml_fn_ast_free_converted_cif(cif_instruction); |
| 614 |
|
| 615 |
fcml_int code_length = lag_instruction->instruction->code_length; |
| 616 |
|
| 617 |
/* Choose the best instruction form. */ |
| 618 |
|
| 619 |
fcml_st_assembled_instruction *choosen_instruction = |
| 620 |
choose_second_pass_best_inst( |
| 621 |
lag_instruction, &assembler_result); |
| 622 |
|
| 623 |
if (lag_instruction->instruction) { |
| 624 |
/* Free previous instruction. */ |
| 625 |
fcml_fn_assembler_instruction_free( |
| 626 |
lag_instruction->instruction); |
| 627 |
} |
| 628 |
|
| 629 |
lag_instruction->instruction = choosen_instruction; |
| 630 |
|
| 631 |
ip_disp += ((fcml_int) lag_instruction->instruction->code_length |
| 632 |
- code_length); |
| 633 |
|
| 634 |
} |
| 635 |
|
| 636 |
lag_instruction = lag_instruction->next; |
| 637 |
} |
| 638 |
|
| 639 |
/* Remove symbols from previous passes. */ |
| 640 |
clean_symbol_state_map(symbol_state_map, pass); |
| 641 |
|
| 642 |
/* Invoke next phase if and only if current phase modified any symbols. */ |
| 643 |
invoke_next_pass = fcml_fn_coll_map_size(symbol_state_map) > 0; |
| 644 |
|
| 645 |
pass++; |
| 646 |
} |
| 647 |
|
| 648 |
/* Free all symbol states stored in the map. Take into |
| 649 |
* account that map should be already empty here if |
| 650 |
* assemblation succeeded. |
| 651 |
*/ |
| 652 |
fcml_fn_coll_map_free(symbol_state_map); |
| 653 |
|
| 654 |
fcml_fn_assembler_result_free(&assembler_result); |
| 655 |
|
| 656 |
return error; |
| 657 |
} |
| 658 |
|
| 659 |
/* Copies assembled instructions from LAG context to the result. */ |
| 660 |
static void convert_instructions(lag_asseblation_context *processing_ctx, |
| 661 |
fcml_st_lag_assembler_result *result) { |
| 662 |
lag_instruction *current_lag = processing_ctx->first_instruction; |
| 663 |
fcml_st_assembled_instruction *first = NULL, *current = NULL; |
| 664 |
while (current_lag) { |
| 665 |
lag_instruction *next = current_lag->next; |
| 666 |
if (current_lag->instruction && !first) { |
| 667 |
first = current_lag->instruction; |
| 668 |
current = first; |
| 669 |
} else if (current_lag->instruction) { |
| 670 |
current->next = current_lag->instruction; |
| 671 |
current = current_lag->instruction; |
| 672 |
} |
| 673 |
current_lag->instruction = NULL; |
| 674 |
current_lag = next; |
| 675 |
} |
| 676 |
result->instructions = first; |
| 677 |
} |
| 678 |
|
| 679 |
/* Assembles code available in "source_code" array. */ |
| 680 |
fcml_ceh_error LIB_CALL fcml_ifn_lag_assemble_core( |
| 681 |
fcml_st_lag_assembler_context *context, const fcml_string *source_code, |
| 682 |
fcml_st_lag_assembler_result *result) { |
| 683 |
|
| 684 |
fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR; |
| 685 |
|
| 686 |
lag_asseblation_context processing_ctx = { 0 }; |
| 687 |
|
| 688 |
/* Free result of the previous assemblation. */ |
| 689 |
fcml_fn_lag_assembler_result_free(result); |
| 690 |
|
| 691 |
/* Allocate symbol table, if it hasn't been defined yet. */ |
| 692 |
fcml_bool local_st = FCML_FALSE; |
| 693 |
if (!context->symbol_table) { |
| 694 |
context->symbol_table = fcml_fn_symbol_table_alloc(); |
| 695 |
if (!context->symbol_table) { |
| 696 |
return FCML_CEH_GEC_OUT_OF_MEMORY; |
| 697 |
} |
| 698 |
local_st = FCML_TRUE; |
| 699 |
} |
| 700 |
|
| 701 |
fcml_st_assembler_context *asm_ctx = &(processing_ctx.assembler_context); |
| 702 |
|
| 703 |
/* It's really important to use default implementations of optimizer |
| 704 |
* and chooser here. */ |
| 705 |
asm_ctx->assembler = context->assembler; |
| 706 |
asm_ctx->configuration = context->configuration; |
| 707 |
asm_ctx->configuration.optimizer = &fcml_fn_asm_default_optimizer; |
| 708 |
asm_ctx->configuration.chooser = &fcml_fn_asm_default_instruction_chooser; |
| 709 |
asm_ctx->entry_point = context->entry_point; |
| 710 |
|
| 711 |
/* Pass 1 */ |
| 712 |
fcml_bool invoke_next_pass = FCML_FALSE; |
| 713 |
|
| 714 |
error = assembler_pass_1(context, source_code, &processing_ctx, |
| 715 |
&invoke_next_pass); |
| 716 |
|
| 717 |
/* Pass 2 */ |
| 718 |
if (!error && invoke_next_pass) { |
| 719 |
error = assembler_pass_2_to_n(context, &processing_ctx); |
| 720 |
} |
| 721 |
|
| 722 |
/* Prepare result. */ |
| 723 |
if (!error) { |
| 724 |
convert_instructions(&processing_ctx, result); |
| 725 |
} |
| 726 |
|
| 727 |
/* Copy line number in case of errors. */ |
| 728 |
if (error) { |
| 729 |
result->error_line = processing_ctx.error_details.line; |
| 730 |
} |
| 731 |
|
| 732 |
/* Free all assembled instructions. In case of error clean symbol table as well. */ |
| 733 |
free_instruction(processing_ctx.first_instruction, |
| 734 |
error ? context->symbol_table : NULL); |
| 735 |
|
| 736 |
/* It's our own symbol table so free it. Remember that user's symbol table is populated |
| 737 |
* with symbols defined while assembling. It can be useful to get these information |
| 738 |
* when code is already assembled, but it has also disadvantages, because user have |
| 739 |
* to free them. Fortunately it is completely transparent functionality realized by |
| 740 |
* "fcml_fn_symbol_table_free" function which user is obliged to call anyway. |
| 741 |
*/ |
| 742 |
if (local_st) { |
| 743 |
fcml_fn_symbol_table_free(context->symbol_table); |
| 744 |
context->symbol_table = NULL; |
| 745 |
} |
| 746 |
|
| 747 |
fcml_fn_ceh_move_errors(&(result->errors), |
| 748 |
&(processing_ctx.error_details.errors)); |
| 749 |
|
| 750 |
return error; |
| 751 |
|
| 752 |
} |
| 753 |
|
| 754 |
/* Load-and-go assembler. */ |
| 755 |
fcml_ceh_error LIB_CALL fcml_fn_lag_assemble( |
| 756 |
fcml_st_lag_assembler_context *context, const fcml_string *source_code, |
| 757 |
fcml_st_lag_assembler_result *result) { |
| 758 |
|
| 759 |
fcml_ceh_error error = fcml_ifn_lag_assemble_core(context, source_code, |
| 760 |
result); |
| 761 |
|
| 762 |
if (error) { |
| 763 |
/* Try to convert error code to error message if there is such need. */ |
| 764 |
fcml_fn_utils_conv_gec_to_error_info( |
| 765 |
context->configuration.enable_error_messages, &(result->errors), |
| 766 |
error); |
| 767 |
} |
| 768 |
|
| 769 |
return error; |
| 770 |
} |
| 771 |
|
| 772 |
/* Free result of the load-and-go assemblation process. */ |
| 773 |
void LIB_CALL fcml_fn_lag_assembler_result_free( |
| 774 |
fcml_st_lag_assembler_result *result) { |
| 775 |
if (result) { |
| 776 |
fcml_fn_ceh_free_errors_only(&(result->errors)); |
| 777 |
fcml_st_assembled_instruction *instruction = result->instructions; |
| 778 |
while (instruction) { |
| 779 |
fcml_st_assembled_instruction *next = instruction->next; |
| 780 |
fcml_fn_assembler_instruction_free(instruction); |
| 781 |
instruction = next; |
| 782 |
} |
| 783 |
result->error_line = 0; |
| 784 |
result->instructions = NULL; |
| 785 |
} |
| 786 |
} |
| 787 |
|
| 788 |
void LIB_CALL fcml_fn_lag_assembler_result_prepare( |
| 789 |
fcml_st_lag_assembler_result *result) { |
| 790 |
if (result) { |
| 791 |
fcml_fn_env_memory_clear(result, sizeof(fcml_st_lag_assembler_result)); |
| 792 |
} |
| 793 |
} |