Debian Codegraph @

src/fcml_gas_parser_def.y

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 %define api.pure
21 %locations
22 %parse-param { struct fcml_st_parser_data *pd }
23 %lex-param { yyscan_t yyscanner }
24 %define api.prefix {gas_}
25
26 %code provides
27 {
28 #define YYSTYPE GAS_STYPE
29 #define YYLTYPE GAS_LTYPE
30 }
31
32 %{
33 #include <stdio.h>
34 #include "fcml_parser_utils.h"
35
36 /* Macro responsible for handling 'Out of memory' errors. */
37 #define HANDLE_ERRORS(x) if( !x ) { yyerror(&yylloc, pd, \
38 "Out of memory."); YYERROR; }
39
40 /* Macro responsible for adding error messages to result. */
41 #define ADD_ERROR_MSG(x) { yyerror(&yylloc, pd, x); }
42 %}
43
44 /* Uncomment it in order to enable debugging */
45 /* %debug */
46
47 /*Terminal symbols.*/
48
49 /* Uexpected tokeens goes here. */
50 %token FCML_TK_UNEXPECTED
51
52 /* Registers. */
53 %token <reg_value> FCML_TK_REG_GPR
54 %token <reg_value> FCML_TK_REG_SIMD
55 %token <reg_value> FCML_TK_REG_FPU
56 %token <reg_value> FCML_TK_REG_SEG
57 %token <reg_value> FCML_TK_REG_OPMASK
58 %token <reg_value> FCML_TK_REG_CR
59 %token <reg_value> FCML_TK_REG_DR
60 %token <reg_value> FCML_TK_REG_RIP
61
62 /* AVX-512 broadcast. */
63 %token <int_value> FCML_TK_DECORATOR_BCAST
64
65 /* AVX-512 Embedded rounding. */
66 %token <int_value> FCML_TK_DECORATOR_ER
67
68 /* AVX-512 zeroying. TODO: We should consider changing it to bool_value in order to simplify decorators handling a bit. */
69 %token FCML_TK_DECORATOR_Z
70
71 /* AVX-512 Suppress all exceptions. */
72 %token FCML_TK_DECORATOR_SAE
73
74 /* AVX-512 Opmask registers decorators. */
75 %token <reg_value> FCML_TK_OPMASK_REG_DECORATOR
76
77 /* Integer values. Encoding doesn't matters. */
78 %token <integer_value> FCML_TK_INTEGER
79
80 /* Literal symbol. It's used mainly for mnemonics.*/
81 %token <symbol> FCML_TK_SYMBOL
82
83 /* Prefixes */
84 %token <prefixes> FCML_TK_PREFIX
85
86 /* Pseudo operations */
87
88 %token <symbol> FCML_TK_PO_BYTE
89
90 /*Non-terminal symbols.*/
91 %type <reg_value> reg
92 %type <reg_value> segment_selector
93 %type <ast> operand
94 %type <ast> operand_list
95 %type <ast> instruction
96 %type <ast> exp
97 %type <ast> effective_address
98 %type <ast> effective_address_components_without_dis
99 %type <ast> effective_address_components
100 %type <ast> pseudo_operation
101 %type <symbol> mnemonic
102 %type <prefixes> inst_prefixes
103
104 /* Precedence levels and associativity. */
105 %left '-' '+'
106 %left '*' '/'
107 %left FCML_OP_UMINUS
108
109 /* Destructor for leafs of AST. */
110 %destructor { fcml_fn_ast_free_node($$); } <ast>
111
112 /* Printers goes here. */
113 %printer { YYFPRINTF(yyoutput, "Integer: %ld Overflow: %d", $$.value, $$.overflow); } <integer_value>
114 %printer { YYFPRINTF(yyoutput, "AST-Type: %d", $$->type); } <ast>
115 %printer { YYFPRINTF(yyoutput, "Type: %s Size: %d Reg: %d x64_exp: %d", fcml_fn_pu_reg_type_to_string( $$.type ), $$.size, $$.reg, $$.x64_exp); } <reg_value>
116
117 %union {
118 /*Complex values.*/
119 fcml_st_ast_val_integer integer_value;
120 fcml_st_register reg_value;
121 fcml_st_ast_node *ast;
122 struct {
123 fcml_string text;
124 int length;
125 } symbol;
126 fcml_hints hints;
127 fcml_prefixes prefixes;
128 fcml_bool bool_value;
129 fcml_int int_value;
130 }
131
132 %{
133 #include "fcml_gas_lexer.h"
134 #define yyscanner ((yyscan_t)pd->scannerInfo)
135 void gas_error(YYLTYPE *yyloc, struct fcml_st_parser_data *pd,
136 const char *error );
137 %}
138
139 %initial-action {
140 /* Uncomment it in order to enable debugging */
141 /* gas_debug = 1; */
142 }
143
144 %%
145
146 start:
147 | instruction { pd->tree = $1; pd->symbol = NULL; }
148 | pseudo_operation { pd->tree = $1; pd->symbol = NULL; }
149 | FCML_TK_SYMBOL ':' { pd->tree = NULL; pd->symbol = fcml_fn_ast_alloc_node_define_symbol( pd->ip, $1.text, $1.length ); HANDLE_ERRORS( pd->symbol ); }
150 | FCML_TK_SYMBOL ':' instruction { pd->tree = $3; pd->symbol = fcml_fn_ast_alloc_node_define_symbol( pd->ip, $1.text, $1.length ); HANDLE_ERRORS( pd->symbol ); }
151 | FCML_TK_SYMBOL ':' pseudo_operation { pd->tree = $3; pd->symbol = fcml_fn_ast_alloc_node_define_symbol( pd->ip, $1.text, $1.length ); HANDLE_ERRORS( pd->symbol ); }
152 ;
153
154 instruction: mnemonic { $$ = fcml_fn_ast_alloc_node_instruction( 0, $1.text, $1.length, 0, NULL ); HANDLE_ERRORS($$); }
155 | mnemonic operand_list { $$ = fcml_fn_ast_alloc_node_instruction( 0, $1.text, $1.length, 0, $2 ); HANDLE_ERRORS($$); }
156 | inst_prefixes mnemonic { $$ = fcml_fn_ast_alloc_node_instruction( $1, $2.text, $2.length, 0, NULL ); HANDLE_ERRORS($$); }
157 | inst_prefixes mnemonic operand_list { $$ = fcml_fn_ast_alloc_node_instruction( $1, $2.text, $2.length, 0, $3 ); HANDLE_ERRORS($$); }
158 ;
159
160 inst_prefixes: FCML_TK_PREFIX
161 | inst_prefixes FCML_TK_PREFIX { if( $1 & $2 ) { ADD_ERROR_MSG( "Doubled prefixes." ); YYERROR; } else { $$ = $1 | $2; } }
162 ;
163
164 mnemonic: FCML_TK_SYMBOL
165 ;
166
167 operand_list: operand { $$ = fcml_fn_ast_alloc_node_operand_list( $1, NULL ); HANDLE_ERRORS($$); }
168 | operand_list ',' operand { $$ = fcml_fn_ast_alloc_node_operand_list( $1, $3 ); HANDLE_ERRORS($$); }
169 ;
170
171 operand: '$' exp { $$ = $2; }
172 | effective_address
173 | effective_address FCML_TK_OPMASK_REG_DECORATOR { $$ = fcml_fn_ast_decorate_effective_address($1, &$2, FCML_FALSE); }
174 | effective_address FCML_TK_OPMASK_REG_DECORATOR FCML_TK_DECORATOR_Z { $$ = fcml_fn_ast_decorate_effective_address($1, &$2, FCML_TRUE); }
175 | effective_address FCML_TK_DECORATOR_Z { $$ = fcml_fn_ast_decorate_effective_address($1, NULL, FCML_TRUE); }
176 | '*' effective_address { $$ = fcml_fn_ast_set_effective_address_hins( $2, FCML_HINT_INDIRECT_POINTER ); }
177 | reg { $$ = fcml_fn_ast_alloc_node_register( &$1, NULL, FCML_FALSE ); HANDLE_ERRORS($$); }
178 | reg FCML_TK_OPMASK_REG_DECORATOR { $$ = fcml_fn_ast_alloc_node_register( &$1, &$2, FCML_FALSE ); HANDLE_ERRORS($$); }
179 | reg FCML_TK_OPMASK_REG_DECORATOR FCML_TK_DECORATOR_Z { $$ = fcml_fn_ast_alloc_node_register( &$1, &$2, FCML_TRUE ); HANDLE_ERRORS($$); }
180 | reg FCML_TK_DECORATOR_Z { $$ = fcml_fn_ast_alloc_node_register( &$1, NULL, FCML_TRUE ); HANDLE_ERRORS($$); }
181 | FCML_TK_DECORATOR_SAE { $$ = fcml_fn_ast_alloc_node_virtual( FCML_TRUE, FCML_FALSE, 0 ); HANDLE_ERRORS($$); }
182 | FCML_TK_DECORATOR_ER { $$ = fcml_fn_ast_alloc_node_virtual( FCML_FALSE, FCML_TRUE, $1 ); HANDLE_ERRORS($$); }
183 ;
184
185 effective_address: effective_address_components
186 | segment_selector effective_address_components { $$ = fcml_fn_ast_set_effective_address_details( &$1, FCML_OS_UNDEFINED, 0, $2 ); }
187 | effective_address FCML_TK_DECORATOR_BCAST { $$ = fcml_fn_ast_set_effective_address_details( NULL, FCML_OS_UNDEFINED, $2, $1 ); }
188 ;
189
190 effective_address_components:
191 effective_address_components_without_dis
192 | exp effective_address_components_without_dis { $$ = fcml_fn_ast_set_displacemnt( $1, $2 ); }
193 | exp { $$ = fcml_fn_ast_alloc_node_effective_address( NULL, NULL, NULL, $1, FCML_FALSE, 0); }
194 ;
195
196 effective_address_components_without_dis:
197 '(' FCML_TK_REG_RIP ')' { $$ = fcml_fn_ast_alloc_node_effective_address( &$2, NULL, NULL, NULL, FCML_FALSE, 0 ); }
198 | '(' reg ')' { $$ = fcml_fn_ast_alloc_node_effective_address( &$2, NULL, NULL, NULL, FCML_FALSE, 0 ); }
199 | '(' reg ',' reg ')' { $$ = fcml_fn_ast_alloc_node_effective_address( &$2, &$4, NULL, NULL, FCML_FALSE, 0 ); }
200 | '(' ',' reg ')' { $$ = fcml_fn_ast_alloc_node_effective_address( NULL, &$3, NULL, NULL, FCML_FALSE, 0 ); }
201 | '(' reg ',' reg ',' FCML_TK_INTEGER ')' { $$ = fcml_fn_ast_alloc_node_effective_address( &$2, &$4, &$6, NULL, FCML_FALSE, 0 ); }
202 | '(' ',' reg ',' FCML_TK_INTEGER ')' { $$ = fcml_fn_ast_alloc_node_effective_address( NULL, &$3, &$5, NULL, FCML_FALSE, 0 ); }
203 ;
204
205 segment_selector: FCML_TK_REG_SEG ':' { $$ = $1; }
206 ;
207
208 exp:
209 FCML_TK_INTEGER { $$ = fcml_fn_ast_alloc_node_integer( &$1 ); HANDLE_ERRORS($$); }
210 | exp '-' exp { $$ = fcml_fn_ast_alloc_node_exp( FCML_EN_EXN_SUB, $1, $3 ); HANDLE_ERRORS($$); }
211 | exp '+' exp { $$ = fcml_fn_ast_alloc_node_exp( FCML_EN_EXN_ADD, $1, $3 ); HANDLE_ERRORS($$); }
212 | exp '/' exp { $$ = fcml_fn_ast_alloc_node_exp( FCML_EN_EXN_DIV, $1, $3 ); HANDLE_ERRORS($$); }
213 | exp '*' exp { $$ = fcml_fn_ast_alloc_node_exp( FCML_EN_EXN_MUL, $1, $3 ); HANDLE_ERRORS($$); }
214 | FCML_TK_SYMBOL { $$ = fcml_fn_ast_alloc_node_use_symbol( $1.text, $1.length ); HANDLE_ERRORS($$); }
215 | '-' exp %prec FCML_OP_UMINUS { $$ = fcml_fn_ast_alloc_node_uminus( $2 ); HANDLE_ERRORS($$); }
216 | '(' exp ')' { $$ = $2; }
217 ;
218
219 /* Every register possible. */
220 reg: FCML_TK_REG_GPR
221 | FCML_TK_REG_SIMD
222 | FCML_TK_REG_FPU
223 | FCML_TK_REG_SEG
224 | FCML_TK_REG_OPMASK
225 | FCML_TK_REG_CR
226 | FCML_TK_REG_DR
227 ;
228
229 /* Pseudo operations. */
230
231 pseudo_operation: FCML_TK_PO_BYTE exp { $$ = fcml_fn_ast_alloc_node_pseudo_operation( $1.text, $1.length, $2 ); HANDLE_ERRORS($$); }
232 ;
233
234 %%