Debian Codegraph @

src/fcml_parser_int.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_symbols.h>
21
22 #include "fcml_parser_int.h"
23 #include "fcml_messages.h"
24 #include "fcml_env_int.h"
25 #include "fcml_dialect_int.h"
26
27 fcml_ceh_error fcml_fn_parser_add_symbol_to_symbol_table(
28 fcml_st_ceh_error_container *errors, fcml_st_symbol_table symbol_table,
29 fcml_st_symbol *symbol, fcml_bool allow_override) {
30
31 fcml_ceh_error error = FCML_COLL_ERROR_NO_ERROR;
32
33 if (!allow_override) {
34 if (fcml_fn_symbol_get(symbol_table, symbol->symbol)) {
35 /* Symbol already defined. */
36 fcml_fn_msg_add_error_message(errors,
37 FCML_MC_SEGMENT_SYMBOL_ALREADY_DEFINED,
38 FCML_CEH_MEC_ERROR_SYMBOL_ALREADY_DEFINED,
39 FCML_EN_CEH_EL_ERROR, symbol->symbol);
40 error = FCML_CEH_GEC_INVALID_INPUT;
41 }
42 }
43
44 if (!error) {
45 error = fcml_fn_symbol_add(symbol_table, symbol);
46 }
47
48 return error;
49 }
50
51 fcml_ceh_error fcml_fn_parse_to_cif(fcml_st_parser_context *context,
52 const fcml_string mnemonic, fcml_st_parser_result *result) {
53
54 if (!result || !mnemonic || !context) {
55 return FCML_CEH_GEC_INVALID_INPUT;
56 }
57
58 fcml_st_parser_ast ast = { 0 };
59
60 /* Free previous parser results. */
61 fcml_fn_parser_result_free(result);
62
63 fcml_ceh_error error = fcml_fn_parse_to_ast(context, mnemonic, &ast);
64
65 /* Copy errors from parser.*/
66 result->errors = ast.errors;
67
68 if (error) {
69
70 /* Just in case. */
71 fcml_fn_symbol_free(ast.symbol);
72 ast.symbol = NULL;
73 fcml_fn_ast_free_node(ast.tree);
74 ast.tree = NULL;
75
76 return error;
77 }
78
79 fcml_bool new_symbol_table = FCML_FALSE;
80
81 /* Symbol */
82 if (ast.symbol) {
83 if (!context->configuration.disable_symbols_declaration) {
84 if (context->symbol_table
85 || context->configuration.alloc_symbol_table_if_needed) {
86 /* If there is no symbol table yet, allocate it. */
87 if (!context->symbol_table) {
88 context->symbol_table = fcml_fn_symbol_table_alloc();
89 if (!context->symbol_table) {
90 fcml_fn_symbol_free(ast.symbol);
91 fcml_fn_ast_free_node(ast.tree);
92 return FCML_CEH_GEC_OUT_OF_MEMORY;
93 }
94 new_symbol_table = FCML_TRUE;
95 }
96
97 error = fcml_fn_parser_add_symbol_to_symbol_table(
98 &(result->errors), context->symbol_table, ast.symbol,
99 context->configuration.override_labels);
100 if (error) {
101 fcml_fn_symbol_free(ast.symbol);
102 fcml_fn_ast_free_node(ast.tree);
103 if (new_symbol_table) {
104 fcml_fn_symbol_table_free(context->symbol_table);
105 context->symbol_table = NULL;
106 }
107 return error;
108 }
109 result->symbol = ast.symbol;
110 } else {
111 /* Free found symbol. */
112 fcml_fn_symbol_free(ast.symbol);
113 ast.symbol = NULL;
114 }
115 } else {
116 /* We haven't expected any symbols so error has to be returned. */
117 fcml_fn_symbol_free(ast.symbol);
118 ast.symbol = NULL;
119 fcml_fn_ast_free_node(ast.tree);
120 ast.tree = NULL;
121
122 return FCML_CEH_GEC_UNSUPPORTED_LABEL_DECLARATION;
123 }
124 }
125
126 /* Instruction. */
127 if (!error && ast.tree) {
128
129 fcml_st_cif_converter_context cif_ctx;
130 cif_ctx.errors = &(result->errors);
131 cif_ctx.symbol_table = context->symbol_table;
132 cif_ctx.ignore_undefined_symbols =
133 context->configuration.ignore_undefined_symbols;
134
135 error = fcml_fn_ast_to_cif_converter(&cif_ctx, ast.tree,
136 &(result->instruction));
137 if (error) {
138
139 /* Free instruction, because it might haven't been fully parsed.*/
140 if (result->instruction) {
141 fcml_fn_ast_free_converted_cif(result->instruction);
142 result->instruction = NULL;
143 }
144
145 /* Defined symbol should also be freed, because we can not leave
146 * such symbols to be defined in the main symbols table.
147 */
148 if (result->symbol) {
149 fcml_fn_symbol_remove(context->symbol_table,
150 result->symbol->symbol);
151 result->symbol = NULL;
152 /* Free symbol table if it has been just allocated. */
153 if (new_symbol_table) {
154 fcml_fn_symbol_table_free(context->symbol_table);
155 context->symbol_table = NULL;
156 }
157 }
158 }
159 }
160
161 fcml_fn_ast_free_node(ast.tree);
162
163 return error;
164 }
165
166 fcml_ceh_error fcml_fn_parse_to_ast(fcml_st_parser_context *context,
167 const fcml_string mnemonic, fcml_st_parser_ast *ast) {
168 fcml_st_dialect_context_int *dialect =
169 (fcml_st_dialect_context_int*) context->dialect;
170 return dialect->instruction_parser(context->ip, mnemonic, ast);
171 }
172
173 void fcml_fn_parser_free_ast(fcml_st_parser_ast *ast) {
174
175 if (ast) {
176 /* Free symbol. */
177 if (ast->symbol) {
178 fcml_fn_env_str_strfree(ast->symbol->symbol);
179 fcml_fn_env_memory_free(ast->symbol);
180 ast->symbol = NULL;
181 }
182 /* Free errors and warnings. */
183 fcml_fn_ceh_free_errors_only(&(ast->errors));
184 /* Free AST tree. */
185 if (ast->tree) {
186 fcml_fn_ast_free_node(ast->tree);
187 }
188 ast->tree = NULL;
189 }
190 }
191