Debian Codegraph @

src/fcml_gas_parser.c

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