| 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_errors.h> |
| 21 |
#include <fcml_common.h> |
| 22 |
#include <fcml_parser.h> |
| 23 |
|
| 24 |
#include "fcml_ceh.h" |
| 25 |
#include "fcml_env_int.h" |
| 26 |
#include "fcml_parser_utils.h" |
| 27 |
#include "fcml_intel_parser_def.h" |
| 28 |
#include "fcml_intel_lexer.h" |
| 29 |
#include "fcml_intel_parser.h" |
| 30 |
#include "fcml_parser_int.h" |
| 31 |
|
| 32 |
void intel_error(YYLTYPE *yylloc, struct fcml_st_parser_data *pd, |
| 33 |
const char *error) { |
| 34 |
/* Stores parser error into standard container. */ |
| 35 |
fcml_fn_ceh_add_error(&(pd->errors), (const fcml_string) error, |
| 36 |
FCML_CEH_MEC_ERROR_INVALID_SYNTAX, FCML_EN_CEH_EL_ERROR); |
| 37 |
} |
| 38 |
|
| 39 |
void* intel_alloc(yy_size_t size, yyscan_t yyscanner) { |
| 40 |
return fcml_fn_env_memory_alloc((fcml_usize) size); |
| 41 |
} |
| 42 |
|
| 43 |
void* intel_realloc(void *ptr, yy_size_t size, yyscan_t yyscanner) { |
| 44 |
return fcml_fn_env_memory_realloc(ptr, (fcml_usize) size); |
| 45 |
} |
| 46 |
|
| 47 |
void intel_free(void *ptr, yyscan_t yyscanner) { |
| 48 |
return fcml_fn_env_memory_free(ptr); |
| 49 |
} |
| 50 |
|
| 51 |
fcml_ceh_error fcml_fn_intel_parse_instruction_to_ast(fcml_ip ip, |
| 52 |
const fcml_string mnemonic, fcml_st_parser_ast *ast) { |
| 53 |
|
| 54 |
fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR; |
| 55 |
|
| 56 |
/* Instruction size is limited to prevent from parser's stack and buffer overflow. */ |
| 57 |
if (!mnemonic || !ast |
| 58 |
|| fcml_fn_env_str_strlen(mnemonic) |
| 59 |
> FCML_PARSER_MAX_INSTRUCTION_LEN) { |
| 60 |
return FCML_CEH_GEC_INVALID_INPUT; |
| 61 |
} |
| 62 |
|
| 63 |
/* Fill instruction pointer. */ |
| 64 |
fcml_st_parser_data parser = { 0 }; |
| 65 |
parser.ip = ip; |
| 66 |
|
| 67 |
/* Set up scanner. */ |
| 68 |
if (intel_lex_init_extra(&parser, &(parser.scannerInfo))) { |
| 69 |
return FCML_CEH_GEC_OUT_OF_MEMORY; |
| 70 |
} |
| 71 |
|
| 72 |
intel__scan_string(mnemonic, parser.scannerInfo); |
| 73 |
|
| 74 |
int yyresult = intel_parse(&parser); |
| 75 |
|
| 76 |
intel_lex_destroy(parser.scannerInfo); |
| 77 |
|
| 78 |
ast->errors = parser.errors; |
| 79 |
ast->symbol = parser.symbol; |
| 80 |
ast->tree = parser.tree; |
| 81 |
|
| 82 |
if (yyresult) { |
| 83 |
switch (yyresult) { |
| 84 |
case 1: /*Syntax error.*/ |
| 85 |
error = FCML_CEH_GEC_INVALID_INPUT; |
| 86 |
break; |
| 87 |
case 2: /*Out of memory*/ |
| 88 |
error = FCML_CEH_GEC_OUT_OF_MEMORY; |
| 89 |
break; |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
return error; |
| 94 |
} |
| 95 |
|