Debian Codegraph @

src/fcml_intel_parser_def.y

raw
1 /*
2 * FCML - Free Code Manipulation Library.
3 * Copyright (C) 2010-2024 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 %locations
21 %define api.pure
22 %parse-param { struct fcml_st_parser_data *pd }
23 %lex-param { yyscan_t yyscanner }
24 %define api.prefix {intel_}
25
26 %code provides
27 {
28 #define YYSTYPE INTEL_STYPE
29 #define YYLTYPE INTEL_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, "Out of memory."); \
38 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 tokens 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. */
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 /* Single-precision floating-point in IEEE 754. */
81 %token <float_value> FCML_TK_FLOAT
82
83 /* Size operand (BYTE PTR etc.) */
84 %token <size_operator> FCML_TK_SIZE_OPERATOR
85
86 /* Literal symbol. It's used mainly for mnemonics.*/
87 %token <symbol> FCML_TK_SYMBOL
88
89 /* Instruction addressing mode hints. */
90 %token <hints> FCML_TK_HINTS
91 %token <hints> FCML_TK_EAO_HINTS
92 %token <hints> FCML_TK_SIB_HINT
93
94 /* Prefixes */
95 %token <prefixes> FCML_TK_PREFIX
96
97 /* Non-terminal symbols */
98 %type <reg_value> reg
99 %type <reg_value> segment_selector
100 %type <hints> hint_list
101 %type <ast> operand
102 %type <ast> operand_list
103 %type <ast> instruction
104 %type <ast> exp
105 %type <ast> far_pointer
106 %type <ast> effective_address
107 %type <ast> effective_address_components
108 %type <symbol> mnemonic
109 %type <prefixes> inst_prefixes
110
111 /* Precedence levels and associativity. */
112 %left '-' '+'
113 %left '*' '/'
114 %left FCML_OP_UMINUS
115
116 /* Destructor for leafs of AST. */
117 %destructor { fcml_fn_ast_free_node($$); } <ast>
118
119 /* Printers goes here. */
120 %printer { YYFPRINTF(yyoutput, "Integer: %d Overflow: %d", $$.value, $$.overflow); } <integer_value>
121 %printer { YYFPRINTF(yyoutput, "Float: %f Overflow: %d", $$.value, $$.overflow); } <float_value>
122 %printer { YYFPRINTF(yyoutput, "AST-Type: %d", $$->type); } <ast>
123 %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>
124
125 %union {
126 /*Complex values.*/
127 fcml_st_ast_val_integer integer_value;
128 fcml_st_ast_val_float float_value;
129 fcml_st_register reg_value;
130 fcml_st_ast_node *ast;
131 struct {
132 fcml_string text;
133 int length;
134 } symbol;
135 fcml_st_size_operator size_operator;
136 fcml_hints hints;
137 fcml_prefixes prefixes;
138 fcml_bool bool_value;
139 fcml_int int_value;
140 }
141
142 %{
143 #include "fcml_intel_lexer.h"
144 #define yyscanner ((yyscan_t)pd->scannerInfo)
145 void intel_error(YYLTYPE *yyloc, struct fcml_st_parser_data *pd,
146 const char *error );
147 %}
148
149 %initial-action {
150 /* Uncomment it in order to enable debugging */
151 /* yydebug = 1; */
152 }
153
154 %%
155
156 start:
157 | instruction { pd->tree = $1; pd->symbol = NULL; }
158 | 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 ); }
159 | 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 ); }
160 ;
161
162 instruction: mnemonic { $$ = fcml_fn_ast_alloc_node_instruction( 0, $1.text, $1.length, 0, NULL ); HANDLE_ERRORS($$); }
163 | mnemonic operand_list { $$ = fcml_fn_ast_alloc_node_instruction( 0, $1.text, $1.length, 0, $2 ); HANDLE_ERRORS($$); }
164 | mnemonic hint_list operand_list { $$ = fcml_fn_ast_alloc_node_instruction( 0, $1.text, $1.length, $2, $3 ); HANDLE_ERRORS($$); }
165 | inst_prefixes mnemonic { $$ = fcml_fn_ast_alloc_node_instruction( $1, $2.text, $2.length, 0, NULL ); HANDLE_ERRORS($$); }
166 | inst_prefixes mnemonic operand_list { $$ = fcml_fn_ast_alloc_node_instruction( $1, $2.text, $2.length, 0, $3 ); HANDLE_ERRORS($$); }
167 | inst_prefixes mnemonic hint_list operand_list { $$ = fcml_fn_ast_alloc_node_instruction( $1, $2.text, $2.length, $3, $4 ); HANDLE_ERRORS($$); }
168 ;
169
170 inst_prefixes: FCML_TK_PREFIX
171 | inst_prefixes FCML_TK_PREFIX { if( $1 & $2 ) { ADD_ERROR_MSG( "Doubled prefixes." ); YYERROR; } else { $$ = $1 | $2; } }
172 ;
173
174 mnemonic: FCML_TK_SYMBOL
175 ;
176
177 hint_list: FCML_TK_HINTS
178 | hint_list FCML_TK_HINTS { $$ = $1 | $2; }
179 ;
180
181 operand_list: operand { $$ = fcml_fn_ast_alloc_node_operand_list( $1, NULL ); HANDLE_ERRORS($$); }
182 | operand_list ',' operand { $$ = fcml_fn_ast_alloc_node_operand_list( $1, $3 ); HANDLE_ERRORS($$); }
183 ;
184
185 /* TODO: Maybe we should decorate every operand regardless of the type?
186 It's probably a better idea as returning a nice descriptive error about unsupported decorator
187 is probably a better approach that context less syntax error.
188 */
189
190 operand: exp
191 | far_pointer
192 | effective_address
193 | effective_address FCML_TK_OPMASK_REG_DECORATOR { $$ = fcml_fn_ast_decorate_effective_address($1, &$2, FCML_FALSE); }
194 | effective_address FCML_TK_OPMASK_REG_DECORATOR FCML_TK_DECORATOR_Z { $$ = fcml_fn_ast_decorate_effective_address($1, &$2, FCML_TRUE); }
195 | effective_address FCML_TK_DECORATOR_Z { $$ = fcml_fn_ast_decorate_effective_address($1, NULL, FCML_TRUE); }
196 | reg { $$ = fcml_fn_ast_alloc_node_register( &$1, NULL, FCML_FALSE ); HANDLE_ERRORS($$); }
197 | reg FCML_TK_OPMASK_REG_DECORATOR { $$ = fcml_fn_ast_alloc_node_register( &$1, &$2, FCML_FALSE ); HANDLE_ERRORS($$); }
198 | reg FCML_TK_OPMASK_REG_DECORATOR FCML_TK_DECORATOR_Z { $$ = fcml_fn_ast_alloc_node_register( &$1, &$2, FCML_TRUE ); HANDLE_ERRORS($$); }
199 | reg FCML_TK_DECORATOR_Z { $$ = fcml_fn_ast_alloc_node_register( &$1, NULL, FCML_TRUE ); HANDLE_ERRORS($$); }
200 | FCML_TK_DECORATOR_SAE { $$ = fcml_fn_ast_alloc_node_virtual( FCML_TRUE, FCML_FALSE, 0 ); HANDLE_ERRORS($$); }
201 | FCML_TK_DECORATOR_ER { $$ = fcml_fn_ast_alloc_node_virtual( FCML_FALSE, FCML_TRUE, $1 ); HANDLE_ERRORS($$); }
202 ;
203
204 effective_address: '[' effective_address_components ']' { $$ = $2; }
205 | FCML_TK_SIZE_OPERATOR '[' effective_address_components ']' { $$ = fcml_fn_ast_set_effective_address_details( NULL, &$1, 0, $3 ); }
206 | segment_selector '[' effective_address_components ']' { $$ = fcml_fn_ast_set_effective_address_details( &$1, FCML_OS_UNDEFINED, 0, $3 ); }
207 | FCML_TK_SIZE_OPERATOR segment_selector '[' effective_address_components ']' { $$ = fcml_fn_ast_set_effective_address_details( &$2, &$1, 0, $4 ); }
208 | effective_address FCML_TK_DECORATOR_BCAST { $$ = fcml_fn_ast_set_effective_address_details( NULL, FCML_OS_UNDEFINED, $2, $1 ); }
209 ;
210
211 effective_address_components: reg { $$ = fcml_fn_ast_alloc_node_effective_address( &$1, NULL, NULL, NULL, FCML_FALSE, 0 ); }
212 | FCML_TK_SIB_HINT reg { $$ = fcml_fn_ast_alloc_node_effective_address( &$2, NULL, NULL, NULL, FCML_FALSE, $1 ); }
213 | FCML_TK_REG_RIP { $$ = fcml_fn_ast_alloc_node_effective_address( &$1, NULL, NULL, NULL, FCML_FALSE, 0 ); }
214 | reg '+' reg { $$ = fcml_fn_ast_alloc_node_effective_address( &$1, &$3, NULL, NULL, FCML_FALSE, 0 ); }
215 | reg '+' reg '*' FCML_TK_INTEGER { $$ = fcml_fn_ast_alloc_node_effective_address( &$1, &$3, &$5, NULL, FCML_FALSE, 0 ); }
216 | reg '+' reg '*' FCML_TK_INTEGER '+' exp { $$ = fcml_fn_ast_alloc_node_effective_address( &$1, &$3, &$5, $7, FCML_FALSE, 0 ); }
217 | reg '+' reg '*' FCML_TK_INTEGER '-' exp { $$ = fcml_fn_ast_alloc_node_effective_address( &$1, &$3, &$5, $7, FCML_TRUE, 0 ); }
218 | reg '*' FCML_TK_INTEGER '+' exp { $$ = fcml_fn_ast_alloc_node_effective_address( NULL, &$1, &$3, $5, FCML_FALSE, 0 ); }
219 | reg '*' FCML_TK_INTEGER '-' exp { $$ = fcml_fn_ast_alloc_node_effective_address( NULL, &$1, &$3, $5, FCML_TRUE, 0 ); }
220 | reg '+' reg '+' exp { $$ = fcml_fn_ast_alloc_node_effective_address( &$1, &$3, NULL, $5, FCML_FALSE, 0 ); }
221 | reg '+' reg '-' exp { $$ = fcml_fn_ast_alloc_node_effective_address( &$1, &$3, NULL, $5, FCML_TRUE, 0 ); }
222 | reg '+' exp { $$ = fcml_fn_ast_alloc_node_effective_address( &$1, NULL, NULL, $3, FCML_FALSE, 0 ); }
223 | FCML_TK_REG_RIP '+' exp { $$ = fcml_fn_ast_alloc_node_effective_address( &$1, NULL, NULL, $3, FCML_FALSE, 0 ); }
224 | FCML_TK_SIB_HINT reg '+' exp { $$ = fcml_fn_ast_alloc_node_effective_address( &$2, NULL, NULL, $4, FCML_FALSE, $1 ); }
225 | reg '-' exp { $$ = fcml_fn_ast_alloc_node_effective_address( &$1, NULL, NULL, $3, FCML_TRUE, 0 ); }
226 | FCML_TK_SIB_HINT reg '-' exp { $$ = fcml_fn_ast_alloc_node_effective_address( &$2, NULL, NULL, $4, FCML_TRUE, $1 ); }
227 | exp { $$ = fcml_fn_ast_alloc_node_effective_address( NULL, NULL, NULL, $1, FCML_FALSE, 0); }
228 | FCML_TK_EAO_HINTS exp { $$ = fcml_fn_ast_alloc_node_effective_address( NULL, NULL, NULL, $2, FCML_FALSE, $1); }
229 | FCML_TK_SIB_HINT exp { $$ = fcml_fn_ast_alloc_node_effective_address( NULL, NULL, NULL, $2, FCML_FALSE, $1); }
230 ;
231
232 segment_selector: FCML_TK_REG_SEG ':' { $$ = $1; }
233 ;
234
235 far_pointer: exp ':' exp { $$ = fcml_fn_ast_alloc_node_far_pointer( $1, $3 ); HANDLE_ERRORS($$); }
236 ;
237
238 exp: FCML_TK_INTEGER { $$ = fcml_fn_ast_alloc_node_integer( &$1 ); HANDLE_ERRORS($$); }
239 | FCML_TK_FLOAT { $$ = fcml_fn_ast_alloc_node_float( &$1 ); HANDLE_ERRORS($$); }
240 | exp '-' exp { $$ = fcml_fn_ast_alloc_node_exp( FCML_EN_EXN_SUB, $1, $3 ); HANDLE_ERRORS($$); }
241 | exp '+' exp { $$ = fcml_fn_ast_alloc_node_exp( FCML_EN_EXN_ADD, $1, $3 ); HANDLE_ERRORS($$); }
242 | exp '/' exp { $$ = fcml_fn_ast_alloc_node_exp( FCML_EN_EXN_DIV, $1, $3 ); HANDLE_ERRORS($$); }
243 | exp '*' exp { $$ = fcml_fn_ast_alloc_node_exp( FCML_EN_EXN_MUL, $1, $3 ); HANDLE_ERRORS($$); }
244 | FCML_TK_SYMBOL { $$ = fcml_fn_ast_alloc_node_use_symbol( $1.text, $1.length ); HANDLE_ERRORS($$); }
245 | '-' exp %prec FCML_OP_UMINUS { $$ = fcml_fn_ast_alloc_node_uminus( $2 ); HANDLE_ERRORS($$); }
246 | '(' exp ')' { $$ = $2; }
247 ;
248
249 /* Every register possible. */
250 reg: FCML_TK_REG_GPR
251 | FCML_TK_REG_SIMD
252 | FCML_TK_REG_FPU
253 | FCML_TK_REG_SEG
254 | FCML_TK_REG_OPMASK
255 | FCML_TK_REG_CR
256 | FCML_TK_REG_DR
257 ;
258
259 %%