diff options
Diffstat (limited to 'tools/d2c/grammar.y')
-rw-r--r-- | tools/d2c/grammar.y | 397 |
1 files changed, 154 insertions, 243 deletions
diff --git a/tools/d2c/grammar.y b/tools/d2c/grammar.y index 063d22b..0c79f04 100644 --- a/tools/d2c/grammar.y +++ b/tools/d2c/grammar.y @@ -1,13 +1,14 @@ %{ -#include <getopt.h> +#include <getopt.h>////// #include <fcntl.h> #include <stdio.h> #include <stdlib.h> #include <sys/mman.h> #include <sys/stat.h> +#include "decl.h" #include "tokens.h" @@ -27,79 +28,124 @@ static void *map_input_data(const char *, size_t *); #include "coder.h" #include "helpers.h" +#include "syntax.h" #include "args/decl.h" +#include "assert/decl.h" #include "bits/decl.h" #include "conv/decl.h" #include "format/decl.h" #include "hooks/decl.h" +#include "id/decl.h" +#include "pattern/decl.h" #include "rules/decl.h" -#include "syntax/decl.h" - - -#define handle_coder_format(c, r) \ - ({ \ - encoding_spec *__spec; \ - operands_format *__format; \ - bool __status; \ - __spec = get_current_encoding_spec(c); \ - __format = get_format_in_encoding_spec(__spec); \ - __status = load_format_from_raw_line(__format, r); \ - if (!__status) YYABORT; \ + + +#define handle_coder_id(c, r) \ + ({ \ + instr_id *__id; \ + bool __status; \ + __id = get_coder_instruction_id(c); \ + __status = load_id_from_raw_line(__id, r); \ + if (!__status) YYABORT; \ + }) + +#define handle_coder_desc(c, r) \ + ({ \ + instr_desc *__desc; \ + __desc = get_coder_instruction_desc(c); \ + set_instruction_description(__desc, r); \ + }) + +#define handle_coder_format(c, r) \ + ({ \ + encoding_spec *__spec; \ + operands_format *__format; \ + bool __status; \ + __spec = get_current_encoding_spec(c); \ + __format = get_format_in_encoding_spec(__spec); \ + __status = load_format_from_raw_line(__format, r); \ + if (!__status) YYABORT; \ }) -#define handle_coder_bits(c, e, r) \ - ({ \ - encoding_spec *__spec; \ - coding_bits *__bits; \ - bool __status; \ - __spec = get_current_encoding_spec(c); \ - __bits = get_bits_in_encoding_spec(__spec); \ - __status = load_bits_from_raw_line(__bits, e, r); \ - if (!__status) YYABORT; \ +#define handle_coder_bits(c, e, r) \ + ({ \ + encoding_spec *__spec; \ + coding_bits *__bits; \ + bool __status; \ + __spec = get_current_encoding_spec(c); \ + __bits = get_bits_in_encoding_spec(__spec); \ + __status = load_bits_from_raw_line(__bits, e, r); \ + if (!__status) YYABORT; \ }) -#define handle_coder_syntax(c, r) \ - ({ \ - encoding_spec *__spec; \ - asm_syntax *__syntax; \ - bool __status; \ - __spec = get_current_encoding_spec(c); \ - __syntax = get_syntax_in_encoding_spec(__spec); \ - __status = load_syntax_from_raw_line(__syntax, r); \ - if (!__status) YYABORT; \ +#define push_coder_new_syntax(c) \ + ({ \ + encoding_spec *__spec; \ + __spec = get_current_encoding_spec(c); \ + push_new_encoding_syntax(__spec); \ }) -#define handle_coder_conversions(c, r) \ - ({ \ - encoding_spec *__spec; \ - conv_list *__list; \ - bool __status; \ - __spec = get_current_encoding_spec(c); \ - __list = get_conversions_in_encoding_spec(__spec); \ - __status = load_convs_from_raw_block(__list, r); \ - if (!__status) YYABORT; \ +#define handle_coder_asm(c, r) \ + ({ \ + encoding_spec *__spec; \ + encoding_syntax *__syntax; \ + asm_pattern *__pattern; \ + bool __status; \ + __spec = get_current_encoding_spec(c); \ + __syntax = get_current_encoding_syntax(__spec); \ + __pattern = get_asm_pattern_in_encoding_syntax(__syntax); \ + __status = load_asm_pattern_from_raw_line(__pattern, r); \ + if (!__status) YYABORT; \ }) -#define handle_coder_hooks(c, r) \ - ({ \ - encoding_spec *__spec; \ - instr_hooks *__hooks;; \ - bool __status; \ - __spec = get_current_encoding_spec(c); \ - __hooks = get_hooks_in_encoding_spec(__spec); \ - __status = load_hooks_from_raw_line(__hooks, r); \ - if (!__status) YYABORT; \ +#define handle_coder_conversions(c, r) \ + ({ \ + encoding_spec *__spec; \ + encoding_syntax *__syntax; \ + conv_list *__list; \ + bool __status; \ + __spec = get_current_encoding_spec(c); \ + __syntax = get_current_encoding_syntax(__spec); \ + __list = get_conversions_in_encoding_syntax(__syntax); \ + __status = load_convs_from_raw_block(__list, r); \ + if (!__status) YYABORT; \ }) -#define handle_coder_rules(c, r) \ - ({ \ - encoding_spec *__spec; \ - decoding_rules *__rules; \ - bool __status; \ - __spec = get_current_encoding_spec(c); \ - __rules = get_rules_in_encoding_spec(__spec); \ - __status = load_rules_from_raw_block(__rules, r); \ - if (!__status) YYABORT; \ +#define handle_coder_assertions(c, r) \ + ({ \ + encoding_spec *__spec; \ + encoding_syntax *__syntax; \ + disass_assert *__dassert; \ + bool __status; \ + __spec = get_current_encoding_spec(c); \ + __syntax = get_current_encoding_syntax(__spec); \ + __dassert = get_assertions_for_encoding_syntax(__syntax); \ + __status = load_assertions_from_raw_block(__dassert, r); \ + if (!__status) YYABORT; \ + }) + +#define handle_coder_hooks(c, r) \ + ({ \ + encoding_spec *__spec; \ + instr_hooks *__hooks;; \ + bool __status; \ + __spec = get_current_encoding_spec(c); \ + __hooks = get_hooks_in_encoding_spec(__spec); \ + __status = load_hooks_from_raw_line(__hooks, r); \ + if (!__status) YYABORT; \ + }) + +#define handle_coder_rules(c, r) \ + ({ \ + encoding_spec *__spec; \ + encoding_syntax *__syntax; \ + decoding_rules *__rules; \ + bool __status; \ + __spec = get_current_encoding_spec(c); \ + __syntax = get_current_encoding_syntax(__spec); \ + __rules = get_rules_in_encoding_syntax(__syntax); \ + __status = load_rules_from_raw_block(__rules, r); \ + if (!__status) YYABORT; \ }) } @@ -138,16 +184,17 @@ YY_DECL; %token COPYRIGHT %token TITLE %token INS_NAME INS_SEP INS_DETAILS +%token ID %token DESC %token ENCODING %token TYPE NUMBER %token ENC_START ENC_END -%token FORMAT +%token FORMAT UNUSED %token WORD HALF %token SYNTAX -%token CONV +%token ASSERT CONV ASM %token HOOKS %token RULES @@ -167,13 +214,15 @@ YY_DECL; %% -input : name encodings { if (!dump_all_routines_using_coder(coder)) YYABORT; } - | name desc encodings { if (!dump_all_routines_using_coder(coder)) YYABORT; } +input : name id desc encodings + | name id encodings name : COPYRIGHT TITLE INS_NAME { save_notes_for_coder(coder, $1, $3, '\0', NULL); } | COPYRIGHT TITLE INS_NAME INS_SEP INS_DETAILS { save_notes_for_coder(coder, $1, $3, $4, $5); } -desc : DESC RAW_LINE +id : ID RAW_LINE { handle_coder_id(coder, $2); } + +desc : DESC RAW_BLOCK { handle_coder_desc(coder, $2); } encodings : /* empty */ | encoding encodings @@ -186,38 +235,50 @@ encoding : ENCODING TYPE NUMBER format_encoding { push_encoding_spec(coder, $2, /* Définitions à l'aide d'un format défini */ format_encoding : format format_content + | unused_format format : FORMAT RAW_LINE { handle_coder_format(coder, $2); } +unused_format : UNUSED RAW_LINE { handle_coder_format(coder, $2); mark_coder_as_useless(coder); } + format_content : /* empty */ - | hooks format_content - | rules format_content + | SYNTAX { push_coder_new_syntax(coder); } format_syntax + | hooks format_syntax + +format_syntax : /* empty */ + | rules format_syntax /* Définitions à l'aide de données brutes */ raw_encoding : bitfield raw_content +bitfield : HALF RAW_LINE { handle_coder_bits(coder, 16, $2); } + | WORD RAW_LINE { handle_coder_bits(coder, 32, $2); } + raw_content : /* empty */ - | syntax raw_content - | conversions raw_content + | SYNTAX { push_coder_new_syntax(coder); } raw_syntax raw_content | hooks raw_content - | rules raw_content -bitfield : HALF RAW_LINE { handle_coder_bits(coder, 16, $2); } - | WORD RAW_LINE { handle_coder_bits(coder, 32, $2); } +raw_syntax : /* empty */ + | assertions raw_syntax + | conversions raw_syntax + | asm raw_syntax + | rules raw_syntax -syntax : SYNTAX RAW_LINE { handle_coder_syntax(coder, $2); } +assertions : ASSERT RAW_BLOCK { handle_coder_assertions(coder, $2); } conversions : CONV RAW_BLOCK { handle_coder_conversions(coder, $2); } +asm : ASM RAW_LINE { handle_coder_asm(coder, $2); } -/* Définitions communes */ -hooks : HOOKS RAW_BLOCK { handle_coder_hooks(coder, $2); } +/* Définitions communes */ rules : RULES RAW_BLOCK { handle_coder_rules(coder, $2); } +hooks : HOOKS RAW_BLOCK { handle_coder_hooks(coder, $2); } + %% @@ -238,7 +299,7 @@ rules : RULES RAW_BLOCK { handle_coder_rules(coder, $2); } static int yyerror(rented_coder *coder, char *temp, char *msg) { - printf("yyerror line %d: %s\n", yyget_lineno(), msg); + printf("YYERROR line %d: %s\n", yyget_lineno(), msg); return 0; @@ -247,46 +308,6 @@ static int yyerror(rented_coder *coder, char *temp, char *msg) /****************************************************************************** * * -* 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]\n", argv0); - - printf("\n"); - - printf("Options:\n"); - - printf("\n"); - - printf("\t-h | --help\t\t\tDisplay this messsage.\n"); - printf("\t-i | --input <file>\t\tProvide the input file containing the description.\n"); - printf("\t-t | --type <raw|format>\tSet the type of the input file.\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 <none|string>\tDefine encoding prefixes for files.\n"); - printf("\t-M | --macro <string>\t\tRegister some conversion functions.\n"); - printf("\t-n | --operand <string>\t\tRegister a function producing final operands.\n"); - printf("\t-p | --prefix <string>\t\tDefine a prefix to format operand type constants (see -t).\n"); - - printf("\n"); - -} - - -/****************************************************************************** -* * * Paramètres : filename = chemin du fichier à charger en mémoire. * * length = taille de l'espace mémoie à mettre en place. [OUT]* * * @@ -344,155 +365,47 @@ static void *map_input_data(const char *filename, size_t *length) /****************************************************************************** * * -* Paramètres : argc = nombre d'arguments dans la ligne de commande. * -* argv = arguments de la ligne de commande. * +* Paramètres : filename = chemin d'accès à un fichier à traiter. * +* pp = préprocesseur déjà chargé à intégrer. * * * -* Description : Point d'entrée du programme. * +* Description : Charge en mémoire la définition contenue dans un fichier. * * * -* Retour : EXIT_SUCCESS si le prgm s'est déroulé sans encombres. * +* Retour : Définition chargée ou NULL en cas d'erreur. * * * * Remarques : - * * * ******************************************************************************/ -int main(int argc, char **argv) +rented_coder *process_definition_file(const char *filename, pre_processor *pp) { - 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 */ + rented_coder *result; /* Codeur à briffer et renvoyer*/ size_t length; /* Nombre d'octets à traiter */ char *content; /* Contenu brut à analyser */ char *temp; /* Zone de travail temporaire */ YY_BUFFER_STATE state; /* Contexte d'analyse */ + int status; /* Bilan d'une analyse */ - static struct option long_options[] = { - - { "help", no_argument, NULL, 'h' }, - { "input", required_argument, NULL, 'i' }, - { "type", required_argument, NULL, 't' }, - { "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' }, - { "operand", required_argument, NULL, 'n' }, - { "prefix", required_argument, NULL, 'p' }, - { 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:t:d:a:H:e:M:n:p:", 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 't': - if (strcmp(optarg, "raw") == 0) - set_coder_input_type(coder, IOT_RAW); - else if (strcmp(optarg, "format") == 0) - set_coder_input_type(coder, IOT_FORMAT); - 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': - - if (strcmp(optarg, "none") == 0) - register_empty_encoding(get_coder_pre_proc(coder)); - - else - { - 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; - - case 'n': - register_as_operand_producer(get_coder_pre_proc(coder), optarg); - break; - - case 'p': - set_coder_const_prefix(coder, optarg); - 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); - printf("need help ? %d - result = %d vs %d\n", need_help, result, EXIT_SUCCESS); - goto exit; - } - - content = map_input_data(get_coder_input_file(coder), &length); + content = map_input_data(filename, &length); if (content == MAP_FAILED) { - result = EXIT_FAILURE; + result = NULL; goto exit; } + result = create_coder(pp); + set_coder_input_file(result, filename); + temp = (char *)calloc(length, sizeof(char)); state = d2c__scan_bytes(content, length); - result = yyparse(coder, temp); + status = yyparse(result, temp); + + if (status == EXIT_FAILURE) + { + delete_coder(result); + result = NULL; + } yy_delete_buffer(state); @@ -502,8 +415,6 @@ int main(int argc, char **argv) exit: - delete_coder(coder); - return result; } |