From a1e0e6a913d6d3a27c264cd2172ff8045d5e91cd Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Sun, 25 Sep 2005 00:41:04 +0000 Subject: [PATCH] * arch.h (yasm_arch_syntax_flavor): Remove. (yasm_arch_create): Add parser and error parameters; now the arch is given the keyword of the parser in use. The error parameter allows the caller to find out whether it was the machine name or the parser name that was in error. (yasm_arch_module): Change create definition to match yasm_arch_create(). (yasm_arch_create_error): New error typedef for yasm_arch_create() errors. * lc3barch.c (lc3b_create): Update to match new yasm_arch_create(). * x86arch.c (x86_create): Likewise. * yasm.c (main): Use new yasm_arch_create() and handle the two kinds of errors it can now generate. Move parser creation up in the sequence so it happens before the arch is created. svn path=/trunk/yasm/; revision=1230 --- frontends/yasm/yasm.c | 49 ++++++++++++++++++++++++------------ libyasm/arch.h | 40 +++++++++++++---------------- modules/arch/lc3b/lc3barch.c | 14 +++++++++-- modules/arch/x86/x86arch.c | 15 +++++++++-- 4 files changed, 75 insertions(+), 43 deletions(-) diff --git a/frontends/yasm/yasm.c b/frontends/yasm/yasm.c index b7e8db7a..b338f0ce 100644 --- a/frontends/yasm/yasm.c +++ b/frontends/yasm/yasm.c @@ -231,6 +231,7 @@ main(int argc, char *argv[]) yasm_object *object = NULL; yasm_section *def_sect; size_t i; + yasm_arch_create_error arch_error; #if defined(HAVE_SETLOCALE) && defined(HAVE_LC_MESSAGES) setlocale(LC_MESSAGES, ""); @@ -417,11 +418,26 @@ main(int argc, char *argv[]) } } - /* Set up architecture using the selected (or default) machine */ + /* Default to NASM as the parser */ + if (!cur_parser_module) { + cur_parser_module = yasm_load_parser("nasm"); + if (!cur_parser_module) { + print_error(_("%s: could not load default %s"), _("FATAL"), + _("parser")); + cleanup(NULL); + return EXIT_FAILURE; + } + } + + /* Set up architecture using the selected (or default) machine and + * selected (or default nasm) parser. + */ if (!machine_name) machine_name = yasm__xstrdup(cur_arch_module->default_machine_keyword); - cur_arch = cur_arch_module->create(machine_name); + cur_arch = cur_arch_module->create(machine_name, + cur_parser_module->keyword, + &arch_error); if (!cur_arch) { if (strcmp(machine_name, "help") == 0) { yasm_arch_machine *m = cur_arch_module->machines; @@ -434,9 +450,21 @@ main(int argc, char *argv[]) return EXIT_SUCCESS; } - print_error(_("%s: `%s' is not a valid %s for %s `%s'"), - _("FATAL"), machine_name, _("machine"), - _("architecture"), cur_arch_module->keyword); + switch (arch_error) { + case YASM_ARCH_CREATE_BAD_MACHINE: + print_error(_("%s: `%s' is not a valid %s for %s `%s'"), + _("FATAL"), machine_name, _("machine"), + _("architecture"), cur_arch_module->keyword); + break; + case YASM_ARCH_CREATE_BAD_PARSER: + print_error(_("%s: `%s' is not a valid %s for %s `%s'"), + _("FATAL"), machine_name, _("machine"), + _("architecture"), cur_arch_module->keyword); + break; + default: + print_error(_("%s: unknown architecture error"), _("FATAL")); + } + return EXIT_FAILURE; } @@ -527,17 +555,6 @@ main(int argc, char *argv[]) return EXIT_FAILURE; } - /* Default to NASM as the parser */ - if (!cur_parser_module) { - cur_parser_module = yasm_load_parser("nasm"); - if (!cur_parser_module) { - print_error(_("%s: could not load default %s"), _("FATAL"), - _("parser")); - cleanup(NULL); - return EXIT_FAILURE; - } - } - /* If not already specified, default to the parser's default preproc. */ if (!cur_preproc_module) cur_preproc_module = diff --git a/libyasm/arch.h b/libyasm/arch.h index ae033a60..03f66f07 100644 --- a/libyasm/arch.h +++ b/libyasm/arch.h @@ -44,6 +44,13 @@ typedef enum { YASM_ARCH_CHECK_ID_TARGETMOD /**< A target modifier (for jumps) */ } yasm_arch_check_id_retval; +/** Errors that may be returned by yasm_arch_module::create(). */ +typedef enum { + YASM_ARCH_CREATE_OK = 0, /**< No error. */ + YASM_ARCH_CREATE_BAD_MACHINE, /**< Unrecognized machine name. */ + YASM_ARCH_CREATE_BAD_PARSER /**< Unrecognized parser name. */ +} yasm_arch_create_error; + /** An instruction operand (opaque type). */ typedef struct yasm_insn_operand yasm_insn_operand; #ifdef YASM_LIB_INTERNAL @@ -60,22 +67,6 @@ typedef struct yasm_arch_base { } yasm_arch_base; #endif -/** "Flavor" of the parser. - * Different assemblers order instruction operands differently. Also, some - * differ on how exactly various registers are specified. There's no great - * solution to this, as the parsers aren't supposed to have knowledge of the - * architectural internals, and the architecture is supposed to be parser - * independent. To make things work, as a rather hackish solution, we give the - * architecture a little knowledge about the general "flavor" of the parser, - * and let the architecture decide what to do with it. Most architectures will - * probably not even use this, but it's required for some (x86 in particular) - * for correct behavior on all parsers. - */ -typedef enum { - YASM_ARCH_SYNTAX_FLAVOR_NASM = 1, /**< Like NASM */ - YASM_ARCH_SYNTAX_FLAVOR_GAS /**< Like GAS */ -} yasm_arch_syntax_flavor; - /** YASM machine subtype. A number of different machine types may be * associated with a single architecture. These may be specific CPU's, but * the ABI used to interface with the architecture should be the primary @@ -110,11 +101,9 @@ typedef struct yasm_arch_module { /** Create architecture. * Module-level implementation of yasm_arch_create(). * Call yasm_arch_create() instead of calling this function. - * \param machine keyword of machine in use (must be one listed in - * #machines) - * \return NULL if machine not recognized. */ - /*@only@*/ yasm_arch * (*create) (const char *machine); + /*@only@*/ yasm_arch * (*create) (const char *machine, const char *parser, + yasm_arch_create_error *error); /** Module-level implementation of yasm_arch_destroy(). * Call yasm_arch_destroy() instead of calling this function. @@ -275,10 +264,14 @@ unsigned int yasm_arch_wordsize(const yasm_arch *arch); * \param module architecture module * \param machine keyword of machine in use (must be one listed in * #machines) - * \return NULL if machine not recognized, otherwise new architecture. + * \param parser keyword of parser in use + * \param error error return value + * \return NULL on error (error returned in error parameter), otherwise new + * architecture. */ /*@only@*/ yasm_arch *yasm_arch_create(const yasm_arch_module *module, - const char *machine); + const char *machine, const char *parser, + /*@out@*/ yasm_arch_create_error *error); /** Clean up, free any architecture-allocated memory. * \param arch architecture @@ -465,7 +458,8 @@ yasm_effaddr *yasm_arch_ea_create(yasm_arch *arch, /*@keep@*/ yasm_expr *e); #define yasm_arch_wordsize(arch) \ (((yasm_arch_base *)arch)->module->wordsize) -#define yasm_arch_create(module, machine) module->create(machine) +#define yasm_arch_create(module, machine, parser, error) \ + module->create(machine, parser, error) #define yasm_arch_destroy(arch) \ ((yasm_arch_base *)arch)->module->destroy(arch) diff --git a/modules/arch/lc3b/lc3barch.c b/modules/arch/lc3b/lc3barch.c index 3b274aa2..5c621db5 100644 --- a/modules/arch/lc3b/lc3barch.c +++ b/modules/arch/lc3b/lc3barch.c @@ -37,12 +37,22 @@ yasm_arch_module yasm_lc3b_LTX_arch; static /*@only@*/ yasm_arch * -lc3b_create(const char *machine) +lc3b_create(const char *machine, const char *parser, + /*@out@*/ yasm_arch_create_error *error) { yasm_arch_base *arch; - if (yasm__strcasecmp(machine, "lc3b") != 0) + *error = YASM_ARCH_CREATE_OK; + + if (yasm__strcasecmp(machine, "lc3b") != 0) { + *error = YASM_ARCH_CREATE_BAD_MACHINE; + return NULL; + } + + if (yasm__strcasecmp(parser, "nasm") != 0) { + *error = YASM_ARCH_CREATE_BAD_PARSER; return NULL; + } arch = yasm_xmalloc(sizeof(yasm_arch_base)); arch->module = &yasm_lc3b_LTX_arch; diff --git a/modules/arch/x86/x86arch.c b/modules/arch/x86/x86arch.c index f9cade13..6ace1e32 100644 --- a/modules/arch/x86/x86arch.c +++ b/modules/arch/x86/x86arch.c @@ -37,17 +37,22 @@ yasm_arch_module yasm_x86_LTX_arch; static /*@only@*/ yasm_arch * -x86_create(const char *machine) +x86_create(const char *machine, const char *parser, + /*@out@*/ yasm_arch_create_error *error) { yasm_arch_x86 *arch_x86; unsigned int amd64_machine; + *error = YASM_ARCH_CREATE_OK; + if (yasm__strcasecmp(machine, "x86") == 0) amd64_machine = 0; else if (yasm__strcasecmp(machine, "amd64") == 0) amd64_machine = 1; - else + else { + *error = YASM_ARCH_CREATE_BAD_MACHINE; return NULL; + } arch_x86 = yasm_xmalloc(sizeof(yasm_arch_x86)); @@ -57,6 +62,12 @@ x86_create(const char *machine) arch_x86->amd64_machine = amd64_machine; arch_x86->mode_bits = 0; + if (yasm__strcasecmp(parser, "nasm") != 0) { + yasm_xfree(arch_x86); + *error = YASM_ARCH_CREATE_BAD_PARSER; + return NULL; + } + return (yasm_arch *)arch_x86; } -- 2.40.0