1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
|
%{
#include <getopt.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "coder.h"
#include "d2c_tok.h"
extern int yylex();
extern void free_flex_memory(void);
/* Affiche un message d'erreur suite à l'analyse en échec. */
static int d2c_error(rented_coder *, char *);
/* Affiche des indications sur l'utilisation du programme. */
static void show_usage(const char *);
%}
%code requires {
/* Pour la définition des expressions conditionnelles... */
#include "coder.h"
#include "conv.h"
#include "helpers.h"
struct action_tmp
{
CondActionType action;
const char *details;
};
#define register_named_field_in_coder(c, n, l) \
({ \
encoding_spec *__spec; \
coding_bits *__bits; \
__spec = get_current_encoding_spec(c); \
__bits = get_bits_in_encoding_spec(__spec); \
register_named_field_in_bits(__bits, n, l); \
})
#define register_bit_in_coder(c, v) \
({ \
encoding_spec *__spec; \
coding_bits *__bits; \
__spec = get_current_encoding_spec(c); \
__bits = get_bits_in_encoding_spec(__spec); \
register_bit_in_bits(__bits, v); \
})
#define count_coder_bits(c) \
({ \
encoding_spec *__spec; \
coding_bits *__bits; \
__spec = get_current_encoding_spec(c); \
__bits = get_bits_in_encoding_spec(__spec); \
count_coded_bits(__bits); \
})
#define register_syntax_item_in_coder(c, n, i) \
({ \
encoding_spec *__spec; \
asm_syntax *__syntax; \
__spec = get_current_encoding_spec(c); \
__syntax = get_syntax_in_encoding_spec(__spec); \
register_syntax_item(__syntax, n, i); \
})
#define register_conversion_in_coder(c, f) \
({ \
encoding_spec *__spec; \
conv_list *__list; \
__spec = get_current_encoding_spec(c); \
__list = get_conversions_in_encoding_spec(__spec); \
register_conversion(__list, f); \
})
#define register_hook_in_coder(c, t, f) \
({ \
encoding_spec *__spec; \
instr_hooks *__hooks;; \
__spec = get_current_encoding_spec(c); \
__hooks = get_hooks_in_encoding_spec(__spec); \
register_hook_function(__hooks, t, f); \
})
#define add_conditional_rule_to_coder(c, e, a) \
({ \
encoding_spec *__spec; \
decoding_rules *__rules; \
__spec = get_current_encoding_spec(c); \
__rules = get_rules_in_encoding_spec(__spec); \
register_conditional_rule(__rules, e, a); \
})
}
%union {
char *string; /* Chaîne de caractères #1 */
const char *cstring; /* Chaîne de caractères #2 */
int integer;
conv_func *subst; /* Fonction de conversion */
arg_list_t *args; /* Liste d'arguments */
arg_expr_t *arg; /* Argument multi-usages */
ConvUnaryOperation un_op; /* Opération unaire */
ConvBinaryOperation bin_op; /* Opération bianire */
cond_expr *expr; /* Expression de déclenchement */
rule_action raction; /* Action et éléments associés */
}
%parse-param { rented_coder *coder }
%token COPYRIGHT
%token TITLE
%token INS_NAME INS_DETAILS
%token ENCODING
%token TYPE NUMBER
%token ENC_START ENC_END
%token WORD HALF NAME SIZE BIT
%token SYNTAX OPERAND_NAME OPERAND_INTERNAL OPERAND_VISIBLE
%token CONV EQ OP COMMA CP NOT AND_LOG EOR COLON
%token HOOKS
%token RULES IF EXPR_START EQUAL BINVAL HEXVAL EXPR_END AND THEN SEE CALL CHK_CALL UNPREDICTABLE
%type <string> COPYRIGHT INS_NAME
%type <cstring> INS_DETAILS
%type <string> TYPE
%type <integer> NUMBER
%type <string> NAME
%type <integer> SIZE BIT
%type <string> OPERAND_NAME OPERAND_INTERNAL OPERAND_VISIBLE
%type <subst> substitution
%type <args> arg_list
%type <arg> arg_expr arg_composed
%type <un_op> arg_expr_un_op
%type <bin_op> arg_expr_bin_op
%type <string> arg_field
%type <expr> rule_cond
%type <string> BINVAL HEXVAL
%type <raction> action
%%
input : name encodings { if (!dump_all_routines_using_coder(coder)) YYABORT; }
name : COPYRIGHT TITLE INS_NAME { save_notes_for_coder(coder, $1, $3, NULL); }
| COPYRIGHT TITLE INS_NAME INS_DETAILS { save_notes_for_coder(coder, $1, $3, $4); }
encodings : /* empty */
| encoding encodings
encoding : ENCODING TYPE NUMBER content { push_encoding_spec(coder, $2, $3); }
content : /* empty */
| bitfield content
| syntax content
| conversions content
| hooks content
| rules content
bitfield : HALF bits {
if (count_coder_bits(coder) != 16)
{
fprintf(stderr, "Unexpected word size: %u vs 16\n", count_coder_bits(coder));
YYABORT;
}
}
| WORD bits {
if (count_coder_bits(coder) != 32)
{
fprintf(stderr, "Unexpected word size: %u vs 32\n", count_coder_bits(coder));
YYABORT;
}
}
bits : /* empty */
| NAME SIZE bits { register_named_field_in_coder(coder, $1, $2); }
| BIT bits { register_bit_in_coder(coder, $1); }
syntax : SYNTAX operands
operands : /* empty */
| operands OPERAND_NAME { register_syntax_item_in_coder(coder, $2, SIT_KEYWORD); }
| operands OPERAND_INTERNAL { register_syntax_item_in_coder(coder, $2, SIT_INT_OPERAND); }
| operands OPERAND_VISIBLE { register_syntax_item_in_coder(coder, $2, SIT_EXT_OPERAND); }
conversions : CONV substitutions
substitutions : /* empty */
| substitutions substitution { register_conversion_in_coder(coder, $2); }
substitution : NAME EQ arg_expr { $$ = make_conv_from_expr($1, $3); }
| NAME EQ NAME OP arg_list CP { $$ = make_conv_from_func($1, $3, $5); }
arg_list : arg_expr { $$ = build_arg_list($1); }
| arg_list COMMA arg_expr { $$ = extend_arg_list($1, $3); }
arg_expr : NAME { $$ = build_arg_expr_from_name($1); }
| NUMBER { $$ = build_arg_expr_from_number($1); }
| BINVAL { $$ = build_arg_expr_from_binval($1); }
| HEXVAL { $$ = build_arg_expr_from_hexval($1); }
| arg_composed { $$ = $1; }
| OP arg_expr CP { $$ = $2; }
| arg_expr_un_op arg_expr { $$ = build_unary_arg_expr($2, $1); }
| arg_expr arg_expr_bin_op arg_expr { $$ = build_binary_arg_expr($1, $3, $2); }
arg_expr_un_op : NOT { $$ = CUO_NOT; }
arg_expr_bin_op : AND_LOG { $$ = CBO_AND; }
| EOR { $$ = CBO_EOR; }
arg_composed : arg_field COLON arg_field { $$ = build_composed_arg_expr($1, $3); }
| arg_composed COLON arg_field { $$ = extend_composed_arg_expr($1, $3); }
arg_field : NAME { $$ = $1; }
| BINVAL { $$ = $1; }
hooks : HOOKS hookings
hookings : /* empty */
| hookings hooking
hooking : NAME EQ NAME { register_hook_in_coder(coder, $1, $3); }
rules : RULES rules_list
rules_list : /* empty */
| rules_list rule
rule : IF EXPR_START rule_cond EXPR_END THEN action { add_conditional_rule_to_coder(coder, $3, &$6); }
| action { add_conditional_rule_to_coder(coder, NULL, &$1); }
rule_cond : NAME EQUAL BINVAL { $$ = build_simple_cond_expression($1, CCT_EQUAL, $3, true); }
| NAME EQUAL HEXVAL { $$ = build_simple_cond_expression($1, CCT_EQUAL, $3, false); }
| NAME AND_LOG BINVAL { $$ = build_simple_cond_expression($1, CCT_AND, $3, true); }
| NAME AND_LOG HEXVAL { $$ = build_simple_cond_expression($1, CCT_AND, $3, false); }
| EXPR_START rule_cond EXPR_END AND EXPR_START rule_cond EXPR_END
{ $$ = build_composed_cond_expression($2, COT_AND, $6); }
action : SEE INS_DETAILS { $$.type = CAT_SEE; $$.details = make_callable($2, false); }
| UNPREDICTABLE { $$.type = CAT_UNPREDICTABLE; }
| CALL NAME OP arg_list CP { $$.type = CAT_CALL; $$.callee = $2; $$.args = $4; }
| CHK_CALL NAME OP arg_list CP { $$.type = CAT_CHECKED_CALL; $$.callee = $2; $$.args = $4; }
%%
/******************************************************************************
* *
* Paramètres : coder = codeur impliqué dans le processus. *
* msg = message d'erreur. *
* *
* Description : Affiche un message d'erreur suite à l'analyse en échec. *
* *
* Retour : 0 *
* *
* Remarques : - *
* *
******************************************************************************/
static int d2c_error(rented_coder *coder, char *msg)
{
printf("yyerror line %d: %s\n", d2c_get_lineno(), msg);
return 0;
}
/******************************************************************************
* *
* Paramètres : argv0 = nombre du programme exécuté. *
* *
* Description : Affiche des indications sur l'utilisation du programme. *
* *
* Retour : - *
* *
* Remarques : - *
* *
******************************************************************************/
static void show_usage(const char *argv0)
{
printf("\n");
printf("Usage: %s [options] < file\n", argv0);
printf("\n");
printf("Options:\n");
printf("\n");
printf("\t-h | --help\t\t\tDisplay this messsage.\n");
printf("\t-d | --dir <string>\t\tSpecify the main output directory.\n");
printf("\t-a | --arch <string>\t\tDefine the archicture to handle.\n");
printf("\t-H | --header <string>\t\tSet the base of the #ifndef / #define game.\n");
printf("\t-e | --encoding <string>\tDefine encoding prefixes for files.\n");
printf("\t-m | --macro <string>\t\tRegister some conversion functions.\n");
printf("\n");
}
/******************************************************************************
* *
* Paramètres : argc = nombre d'arguments dans la ligne de commande. *
* argv = arguments de la ligne de commande. *
* *
* Description : Point d'entrée du programme. *
* *
* Retour : EXIT_SUCCESS si le prgm s'est déroulé sans encombres. *
* *
* Remarques : - *
* *
******************************************************************************/
int main(int argc, char **argv)
{
int result; /* Bilan à retourner */
rented_coder *coder; /* Codeur à briffer & employer */
int index; /* Indice de fichier à traiter */
bool need_help; /* Affichage de l'aide ? */
bool has_error; /* Erreur dans la ligne de cmd.*/
int ret; /* Bilan d'une lecture d'arg. */
char *sep; /* Caratère '=' en coupure */
static struct option long_options[] = {
{ "help", no_argument, NULL, 'h' },
{ "input", required_argument, NULL, 'i' },
{ "dir", required_argument, NULL, 'd' },
{ "arch", required_argument, NULL, 'a' },
{ "header", required_argument, NULL, 'H' },
{ "encoding", required_argument, NULL, 'e' },
{ "macro", required_argument, NULL, 'M' },
{ NULL, 0, NULL, 0 }
};
result = EXIT_SUCCESS;
coder = create_coder();
index = 0;
need_help = false;
has_error = false;
while (!has_error)
{
ret = getopt_long(argc, argv, "hi:d:a:H:e:M:", long_options, &index);
if (ret == -1) break;
switch (ret)
{
case 'h':
need_help = true;
break;
case 'i':
set_coder_input_file(coder, optarg);
break;
case 'd':
set_coder_output_directory(coder, optarg);
break;
case 'a':
set_coder_arch(coder, optarg);
break;
case 'H':
set_coder_header_base(coder, optarg);
break;
case 'e':
sep = strchr(optarg, '=');
has_error = (sep == NULL);
if (!has_error)
{
*sep = '\0';
register_encoding(get_coder_pre_proc(coder), optarg, sep + 1);
}
break;
case 'M':
sep = strchr(optarg, '=');
has_error = (sep == NULL);
if (!has_error)
{
*sep = '\0';
define_macro(get_coder_pre_proc(coder), optarg, sep + 1);
}
break;
}
}
if (need_help || has_error || !do_basic_checks_with_coder(coder) || optind != argc)
{
show_usage(argv[0]);
result = (need_help ? EXIT_SUCCESS : EXIT_FAILURE);
goto exit;
}
result = yyparse(coder);
exit:
free_flex_memory();
delete_coder(coder);
return result;
}
|