diff options
Diffstat (limited to 'tools')
-rw-r--r-- | tools/d2c/coder.c | 241 | ||||
-rw-r--r-- | tools/d2c/coder.h | 5 | ||||
-rw-r--r-- | tools/d2c/d2c.c | 40 |
3 files changed, 175 insertions, 111 deletions
diff --git a/tools/d2c/coder.c b/tools/d2c/coder.c index 2e02e47..cd64d88 100644 --- a/tools/d2c/coder.c +++ b/tools/d2c/coder.c @@ -26,6 +26,7 @@ #include <assert.h> #include <fcntl.h> +#include <libgen.h> #include <malloc.h> #include <stdio.h> #include <string.h> @@ -99,7 +100,7 @@ static void init_coder_opcodes_file(int, const output_info *, const char *); static void init_coder_code_file(int, const char *); /* Centralise l'impression du nom de fonction de désassemblage. */ -static void write_read_function_name(int fd, const char *, const string_exch *, const char *); +static void write_read_function_name(const rented_coder *, const output_info *, int, const string_exch *); /* Génère ou complète un fichier contenant le code C principal. */ static bool output_coder_raw(const rented_coder *, const output_info *, const string_exch *, const encoding_spec *, int, int); @@ -523,6 +524,55 @@ void mark_coder_as_useless(rented_coder *coder) /****************************************************************************** * * +* Paramètres : coder = gestion par la machine en remplacement de l'humain. * +* info = précisions quant à la génération. * +* * +* Description : Crée la désignation principale d'une instruction. * +* * +* Retour : Identifiant à libérer après usage. * +* * +* Remarques : - * +* * +******************************************************************************/ + +char *build_coder_main_identifier(const rented_coder *coder, const output_info *info) +{ + char *result; /* Chaîne construite à renvoyer*/ + char *filename; /* Nom de fichier modifiable */ + char *sub; /* Compartimentage à insérer */ + char *name; /* Désignation à manipuler */ + + if (info->filename_reuse > 0) + { + filename = strdup(coder->input); + + sub = basename(filename); + + if (info->filename_reuse < strlen(sub)) + sub[info->filename_reuse] = '\0'; + + } + + name = get_coder_code_name(coder); + make_string_upper(name); + + if (info->filename_reuse > 0) + asprintf(&result, "%s_%s_%s", info->id_prefix, sub, name); + else + asprintf(&result, "%s_%s", info->id_prefix, name); + + free(name); + + if (info->filename_reuse > 0) + free(filename); + + return result; + +} + + +/****************************************************************************** +* * * Paramètres : coder = gestion par la machine en remplacement de l'humain. * * info = précisions quant à la génération. * * prefix = type d'encodage à répercuter sur le nom de fichier. * @@ -994,10 +1044,10 @@ bool output_coder_body(const rented_coder *coder, const output_info *info) /****************************************************************************** * * -* Paramètres : fd = flux ouvert en écriture. * -* arch = architecture en cours de traitement. * -* sub = encodage choisi comme sous-ensemble d'architecture. * -* name = désignation complète d'une instruction. * +* Paramètres : coder = gestion automatique de l'écriture de code. * +* info = précisions quant à la génération. * +* fd = flux ouvert en écriture. * +* enc = encodage choisi comme sous-ensemble d'architecture. * * * * Description : Centralise l'impression du nom de fonction de désassemblage. * * * @@ -1007,12 +1057,53 @@ bool output_coder_body(const rented_coder *coder, const output_info *info) * * ******************************************************************************/ -static void write_read_function_name(int fd, const char *arch, const string_exch *sub, const char *name) +static void write_read_function_name(const rented_coder *coder, const output_info *info, int fd, const string_exch *enc) { - if (sub->dest == NULL) - dprintf(fd, "%s_read_instr_%s", arch, name); + char *arch; /* Architecture à traiter */ + char *filename; /* Nom de fichier modifiable */ + char *sub; /* Compartimentage à insérer */ + char *name; /* Désignation à manipuler */ + + /* Préparations */ + + arch = strdup(info->arch_cn); + make_string_lower(arch); + + if (info->filename_reuse > 0) + { + filename = strdup(coder->input); + + sub = basename(filename); + + if (info->filename_reuse < strlen(sub)) + sub[info->filename_reuse] = '\0'; + + make_string_lower(sub); + + } + + name = get_coder_code_name(coder); + + /* Impressions */ + + if (enc->dest == NULL) + dprintf(fd, "%s_read_instr", arch); else - dprintf(fd, "%s_read_%s_instr_%s", arch, sub->dest, name); + dprintf(fd, "%s_read_%s_instr", arch, enc->dest); + + if (info->filename_reuse > 0) + dprintf(fd, "_%s", sub); + + dprintf(fd, "_%s", name); + + /* Sortie propre */ + + free(name); + + if (info->filename_reuse > 0) + free(filename); + + free(arch); } @@ -1038,18 +1129,15 @@ static bool output_coder_raw(const rented_coder *coder, const output_info *info, { bool result; /* Bilan à retourner */ char *arch; /* Architecture à traiter */ - char *name; /* Désignation à manipuler */ char *prefix; /* Préfixe employé en suffixe */ coding_bits *bits; /* Gestionnaire de bits */ unsigned int wide; /* Taille des mots */ size_t maxlen; /* Taille à compléter */ - char *fullname; /* Désignation complète */ + char *constant; /* Définition d'une constante */ arch = strdup(info->arch_cn); make_string_lower(arch); - name = get_coder_code_name(coder); - prefix = build_encoding_spec_prefix(encoding); bits = get_bits_in_encoding_spec(encoding); @@ -1062,7 +1150,7 @@ static bool output_coder_raw(const rented_coder *coder, const output_info *info, dprintf(hfd, "/* Décode une forme d'instruction de type '%s'. */\n", coder->ins); dprintf(hfd, "GArchInstruction *"); - write_read_function_name(hfd, arch, enc_name, name); + write_read_function_name(coder, info, hfd, enc_name); dprintf(hfd, "_%s", prefix); dprintf(hfd, "("); @@ -1101,7 +1189,7 @@ static bool output_coder_raw(const rented_coder *coder, const output_info *info, dprintf(cfd, "static "); dprintf(cfd, "GArchInstruction *"); - write_read_function_name(cfd, arch, enc_name, name); + write_read_function_name(coder, info, cfd, enc_name); dprintf(cfd, "_%s", prefix); dprintf(cfd, "("); @@ -1112,13 +1200,11 @@ static bool output_coder_raw(const rented_coder *coder, const output_info *info, dprintf(cfd, "{"); dprintf(cfd, "\n"); - make_string_upper(name); - - asprintf(&fullname, "%s_%s", info->id_prefix, name); + constant = build_coder_main_identifier(coder, info); - result = write_encoding_spec_raw_disass(encoding, cfd, arch, fullname, coder->pp); + result = write_encoding_spec_raw_disass(encoding, cfd, arch, constant, coder->pp); - free(fullname); + free(constant); dprintf(cfd, "}\n"); dprintf(cfd, "\n"); @@ -1127,8 +1213,6 @@ static bool output_coder_raw(const rented_coder *coder, const output_info *info, free(prefix); - free(name); - free(arch); return result; @@ -1155,8 +1239,6 @@ static bool output_coder_raw(const rented_coder *coder, const output_info *info, static bool output_coder_main_raw(const rented_coder *coder, const output_info *info, const string_exch *enc_name, int hfd, int cfd) { bool result; /* Bilan à retourner */ - char *arch; /* Architecture à traiter */ - char *name; /* Désignation à manipuler */ unsigned int wide; /* Taille des mots */ size_t maxlen; /* Taille à compléter */ bool first; /* Note un premier appel */ @@ -1165,11 +1247,6 @@ static bool output_coder_main_raw(const rented_coder *coder, const output_info * result = false; - arch = strdup(info->arch_cn); - make_string_lower(arch); - - name = get_coder_code_name(coder); - wide = get_bit_width_for_encoding_spec(coder, enc_name); /* Désassemblage : déclaration */ @@ -1177,7 +1254,7 @@ static bool output_coder_main_raw(const rented_coder *coder, const output_info * dprintf(hfd, "/* Décode une instruction de type '%s'. */\n", coder->ins); dprintf(hfd, "GArchInstruction *"); - write_read_function_name(hfd, arch, enc_name, name); + write_read_function_name(coder, info, hfd, enc_name); dprintf(hfd, "("); dprintf(hfd, "uint%u_t raw", wide); @@ -1210,7 +1287,7 @@ static bool output_coder_main_raw(const rented_coder *coder, const output_info * dprintf(cfd, "\n"); dprintf(cfd, "GArchInstruction *"); - write_read_function_name(cfd, arch, enc_name, name); + write_read_function_name(coder, info, cfd, enc_name); dprintf(cfd, "("); dprintf(cfd, "uint%u_t raw", wide); @@ -1237,7 +1314,7 @@ static bool output_coder_main_raw(const rented_coder *coder, const output_info * if (first) { dprintf(cfd, "\tresult = "); - write_read_function_name(cfd, arch, enc_name, name); + write_read_function_name(coder, info, cfd, enc_name); dprintf(cfd, "_%s(raw);\n", prefix); dprintf(cfd, "\n"); @@ -1250,7 +1327,7 @@ static bool output_coder_main_raw(const rented_coder *coder, const output_info * dprintf(cfd, "\tif (result == NULL)\n"); dprintf(cfd, "\t\tresult = "); - write_read_function_name(cfd, arch, enc_name, name); + write_read_function_name(coder, info, cfd, enc_name); dprintf(cfd, "_%s(raw);\n", prefix); dprintf(cfd, "\n"); @@ -1267,12 +1344,6 @@ static bool output_coder_main_raw(const rented_coder *coder, const output_info * dprintf(cfd, "}\n"); dprintf(cfd, "\n"); - /* Conclusion */ - - free(name); - - free(arch); - return result; } @@ -1299,21 +1370,18 @@ static bool output_coder_format(const rented_coder *coder, const output_info *in { bool result; /* Bilan à retourner */ char *arch; /* Architecture à traiter */ - char *name; /* Désignation à manipuler */ size_t maxlen; /* Taille à compléter */ - char *fullname; /* Désignation complète */ + char *constant; /* Définition d'une constante */ arch = strdup(info->arch_cn); make_string_lower(arch); - name = get_coder_code_name(coder); - /* Désassemblage : déclaration */ dprintf(hfd, "/* Décode une instruction de type '%s'. */\n", coder->ins); dprintf(hfd, "GArchInstruction *"); - write_read_function_name(hfd, arch, enc_name, name); + write_read_function_name(coder, info, hfd, enc_name); dprintf(hfd, "("); dprintf(hfd, "const GArchProcessor *, GProcContext *, const GBinContent *, "); @@ -1351,7 +1419,7 @@ static bool output_coder_format(const rented_coder *coder, const output_info *in dprintf(cfd, "\n"); dprintf(cfd, "GArchInstruction *"); - write_read_function_name(cfd, arch, enc_name, name); + write_read_function_name(coder, info, cfd, enc_name); dprintf(cfd, "("); dprintf(cfd, "const GArchProcessor *proc, GProcContext *ctx, const GBinContent *content, "); @@ -1362,21 +1430,17 @@ static bool output_coder_format(const rented_coder *coder, const output_info *in dprintf(cfd, "{"); dprintf(cfd, "\n"); - make_string_upper(name); - - asprintf(&fullname, "%s_%s", info->id_prefix, name); + constant = build_coder_main_identifier(coder, info); - result = write_encoding_spec_format_disass(encoding, cfd, arch, fullname, info->fmt_prefix); + result = write_encoding_spec_format_disass(encoding, cfd, arch, constant, info->fmt_prefix); - free(fullname); + free(constant); dprintf(cfd, "}\n"); dprintf(cfd, "\n"); /* Conclusion */ - free(name); - free(arch); return result; @@ -1482,11 +1546,12 @@ bool output_coder_identifier(const rented_coder *coder, const output_info *info) bool result; /* Bilan à retourner */ bool created; /* Note une création */ int fd; /* Flux ouvert en écriture */ + char *constant; /* Définition d'une constante */ char *name; /* Désignation à manipuler */ instr_id *id; /* Gestionnaire d'identifiant */ unsigned int iid; /* Identifiant unique attribué */ - char *constant; /* Définition d'une constante */ char *comment; /* Contenu du commentaire */ + char *aligned; /* Adaptation pour l'alignement*/ result = false; @@ -1503,12 +1568,7 @@ bool output_coder_identifier(const rented_coder *coder, const output_info *info) /* Constitution de la constante */ - name = get_coder_code_name(coder); - make_string_upper(name); - - asprintf(&constant, "%s_%s,", info->id_prefix, name); - - free(name); + constant = build_coder_main_identifier(coder, info); /* Définition du commentaire */ @@ -1523,7 +1583,11 @@ bool output_coder_identifier(const rented_coder *coder, const output_info *info) /* Impression de la ligne */ - dprintf(fd, " %-40s/* %-28s*/\n", constant, comment); + asprintf(&aligned, "%s,", constant); + + dprintf(fd, " %-40s/* %-28s*/\n", aligned, comment); + + free(aligned); free(constant); free(comment); @@ -1631,8 +1695,7 @@ bool output_coder_sub_identifier(const rented_coder *coder, const output_info *i bool result; /* Bilan à retourner */ bool created; /* Note une création */ int fd; /* Flux ouvert en écriture */ - char *name; /* Désignation à manipuler */ - char *fullname; /* Désignation complète */ + char *constant; /* Définition d'une constante */ size_t i; /* Boucle de parcours */ result = false; @@ -1650,21 +1713,14 @@ bool output_coder_sub_identifier(const rented_coder *coder, const output_info *i /* Impression des sous-identifiants */ - name = get_coder_code_name(coder); - make_string_upper(name); - - asprintf(&fullname, "%s_%s", info->id_prefix, name); - - free(name); + constant = build_coder_main_identifier(coder, info); result = true; for (i = 0; i < coder->specs_count && result; i++) - result = write_encoding_spec_subid(coder->specs[i], fd, fullname); - - free(fullname); + result = write_encoding_spec_subid(coder->specs[i], fd, constant); - result = true; + free(constant); ocsi_exit: @@ -1795,9 +1851,9 @@ bool output_coder_keyword(const rented_coder *coder, const output_info *info) bool result; /* Bilan à retourner */ bool created; /* Note une création */ int fd; /* Flux ouvert en écriture */ - char *name; /* Désignation à manipuler */ - char *fullname; /* Désignation complète */ + char *constant; /* Définition d'une constante */ size_t i; /* Boucle de parcours */ + char *name; /* Désignation à manipuler */ result = false; @@ -1822,12 +1878,7 @@ bool output_coder_keyword(const rented_coder *coder, const output_info *info) /* Lancement des impressions */ - name = get_coder_code_name(coder); - make_string_upper(name); - - asprintf(&fullname, "%s_%s", info->id_prefix, name); - - free(name); + constant = build_coder_main_identifier(coder, info); result = true; @@ -1840,7 +1891,7 @@ bool output_coder_keyword(const rented_coder *coder, const output_info *info) break; case IOT_RAW: - result = write_encoding_spec_keywords(coder->specs[i], fd, fullname); + result = write_encoding_spec_keywords(coder->specs[i], fd, constant); break; case IOT_FORMAT: @@ -1848,7 +1899,7 @@ bool output_coder_keyword(const rented_coder *coder, const output_info *info) /* Impression de la colonne */ - dprintf(fd, "\t[%s] = ", fullname); + dprintf(fd, "\t[%s] = ", constant); /* Impression du mot clef */ @@ -1862,7 +1913,7 @@ bool output_coder_keyword(const rented_coder *coder, const output_info *info) } - free(fullname); + free(constant); ock_exit: @@ -1995,8 +2046,7 @@ bool output_coder_hooks(const rented_coder *coder, const output_info *info) bool result; /* Bilan à retourner */ bool created; /* Note une création */ int fd; /* Flux ouvert en écriture */ - char *name; /* Désignation à manipuler */ - char *fullname; /* Désignation complète */ + char *constant; /* Définition d'une constante */ size_t i; /* Boucle de parcours */ result = false; @@ -2022,12 +2072,7 @@ bool output_coder_hooks(const rented_coder *coder, const output_info *info) /* Lancement des impressions */ - name = get_coder_code_name(coder); - make_string_upper(name); - - asprintf(&fullname, "%s_%s", info->id_prefix, name); - - free(name); + constant = build_coder_main_identifier(coder, info); result = true; @@ -2040,17 +2085,17 @@ bool output_coder_hooks(const rented_coder *coder, const output_info *info) break; case IOT_RAW: - result = write_encoding_spec_hooks(coder->specs[i], fd, fullname, true); + result = write_encoding_spec_hooks(coder->specs[i], fd, constant, true); break; case IOT_FORMAT: assert(i == 0); - result = write_encoding_spec_hooks(coder->specs[i], fd, fullname, false); + result = write_encoding_spec_hooks(coder->specs[i], fd, constant, false); break; } - free(fullname); + free(constant); och_exit: @@ -2163,6 +2208,7 @@ bool output_coder_description(const rented_coder *coder, const output_info *info bool result; /* Bilan à retourner */ bool created; /* Note une création */ int fd; /* Flux ouvert en écriture */ + char *constant; /* Définition d'une constante */ char *name; /* Désignation à manipuler */ result = false; @@ -2188,12 +2234,11 @@ bool output_coder_description(const rented_coder *coder, const output_info *info /* Impression de la colonne */ - name = get_coder_code_name(coder); - make_string_upper(name); + constant = build_coder_main_identifier(coder, info); - dprintf(fd, "\t[%s_%s] = ", info->id_prefix, name); + dprintf(fd, "\t[%s] = ", constant); - free(name); + free(constant); /* Impression du mot clef */ diff --git a/tools/d2c/coder.h b/tools/d2c/coder.h index 218ab92..143b969 100644 --- a/tools/d2c/coder.h +++ b/tools/d2c/coder.h @@ -117,9 +117,14 @@ typedef struct _output_info const char *id_prefix; /* Préfixe pour les constantes */ int id_len; /* Largeur des identifiants */ + size_t filename_reuse; /* Taille d'une extention */ + } output_info; +/* Crée la désignation principale d'une instruction. */ +char *build_coder_main_identifier(const rented_coder *, const output_info *); + /* Génère ou complète un fichier contenant le code C principal. */ bool output_coder_body(const rented_coder *, const output_info *); diff --git a/tools/d2c/d2c.c b/tools/d2c/d2c.c index 1d9b5b6..9cb2df4 100644 --- a/tools/d2c/d2c.c +++ b/tools/d2c/d2c.c @@ -89,6 +89,14 @@ static void show_usage(const char *argv0) printf("\n"); + printf("Raw specific options:\n"); + + printf("\n"); + + printf("\t--filename-reuse <length>\t\tSet the length of filename to include in identifiers (default = 0).\n"); + + printf("\n"); + printf("Format specific options:\n"); printf("\n"); @@ -133,21 +141,23 @@ int main(int argc, char **argv) static struct option long_options[] = { - { "help", no_argument, NULL, 'h' }, - { "exec", required_argument, NULL, 'x' }, - { "outdir", required_argument, NULL, 'o' }, - { "type", required_argument, NULL, 't' }, - { "arch", required_argument, NULL, 'a' }, - { "name", required_argument, NULL, 'n' }, - { "guard", required_argument, NULL, 'G' }, - { "encoding", required_argument, NULL, 'e' }, + { "help", no_argument, NULL, 'h' }, + { "exec", required_argument, NULL, 'x' }, + { "outdir", required_argument, NULL, 'o' }, + { "type", required_argument, NULL, 't' }, + { "arch", required_argument, NULL, 'a' }, + { "name", required_argument, NULL, 'n' }, + { "guard", required_argument, NULL, 'G' }, + { "encoding", required_argument, NULL, 'e' }, + + { "id-prefix", required_argument, NULL, 0x100 }, + { "id-expected", required_argument, NULL, 0x101 }, - { "id-prefix", required_argument, NULL, 0x100 }, - { "id-expected",required_argument, NULL, 0x101 }, + { "filename-reuse", required_argument, NULL, 0x200 }, - { "op-prefix", required_argument, NULL, 0x200 }, + { "op-prefix", required_argument, NULL, 0x300 }, - { NULL, 0, NULL, 0 } + { NULL, 0, NULL, 0 } }; @@ -163,7 +173,7 @@ int main(int argc, char **argv) while (!has_error) { - ret = getopt_long(argc, argv, "hx:o:t:a:n:e:G:", long_options, &index); + ret = getopt_long(argc, argv, "hx:o:t:a:n:G:e:", long_options, &index); if (ret == -1) break; switch (ret) @@ -244,6 +254,10 @@ int main(int argc, char **argv) break; case 0x200: + info.filename_reuse = strtoul(optarg, NULL, 10); + break; + + case 0x300: info.fmt_prefix = optarg; break; |