diff options
author | Luc Van Oostenryck <luc.vanoostenryck@gmail.com> | 2019-12-15 09:00:36 +0100 |
---|---|---|
committer | Luc Van Oostenryck <luc.vanoostenryck@gmail.com> | 2019-12-16 10:27:47 +0100 |
commit | 47ed6c8134ea0b3459877f364969944426f7cdb5 (patch) | |
tree | eeb47775a7250c45f1d3a1176959f29fc1fdf28d | |
parent | d407146a779162d817c2d163c93c322d3bd07985 (diff) | |
download | sparse-47ed6c8134ea0b3459877f364969944426f7cdb5.tar.gz |
arch: move parsing of --arch=<ARCH> to target.c
So, the 2 tables indexed by arch are next to each other,
both in target.c, making easier to add a new arch.
Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
-rw-r--r-- | lib.c | 50 | ||||
-rw-r--r-- | target.c | 53 | ||||
-rw-r--r-- | target.h | 1 |
3 files changed, 58 insertions, 46 deletions
@@ -1137,56 +1137,14 @@ static char **handle_switch_x(char *arg, char **next) static char **handle_arch(char *arg, char **next) { - static const struct arch { - const char *name; - enum machine mach; - char bits; - } archs[] = { - { "aarch64", MACH_ARM64, 64, }, - { "arm64", MACH_ARM64, 64, }, - { "arm", MACH_ARM, 32, }, - { "i386", MACH_I386, 32, }, - { "m68k", MACH_M68K, 32, }, - { "mips", MACH_MIPS32, 0, }, - { "powerpc", MACH_PPC32, 0, }, - { "ppc", MACH_PPC32, 0, }, - { "riscv", MACH_RISCV32, 0, }, - { "s390x", MACH_S390X, 64, }, - { "s390", MACH_S390, 32, }, - { "sparc", MACH_SPARC32, 0, }, - { "x86_64", MACH_X86_64, 64, }, - { "x86-64", MACH_X86_64, 64, }, - { NULL }, - }; - const struct arch *p; + enum machine mach; if (*arg++ != '=') die("missing argument for --arch option"); - for (p = &archs[0]; p->name; p++) { - size_t len = strlen(p->name); - if (strncmp(p->name, arg, len) == 0) { - enum machine mach = p->mach; - const char *suf = arg + len; - int bits = p->bits; - - if (bits == 0) { - if (!strcmp(suf, "") || !strcmp(suf, "32")) { - ; - } else if (!strcmp(suf, "64")) { - mach += 1; - } else { - die("invalid architecture: %s", arg); - } - } else { - if (strcmp(suf, "")) - die("invalid architecture: %s", arg); - } - - target_config(mach); - break; - } - } + mach = target_parse(arg); + if (mach != MACH_UNKNOWN) + target_config(mach); return next; } @@ -1,4 +1,5 @@ #include <stdio.h> +#include <string.h> #include "symbol.h" #include "target.h" @@ -75,6 +76,58 @@ static const struct target *targets[] = { }; const struct target *arch_target = &target_default; +enum machine target_parse(const char *name) +{ + static const struct arch { + const char *name; + enum machine mach; + char bits; + } archs[] = { + { "aarch64", MACH_ARM64, 64, }, + { "arm64", MACH_ARM64, 64, }, + { "arm", MACH_ARM, 32, }, + { "i386", MACH_I386, 32, }, + { "m68k", MACH_M68K, 32, }, + { "mips", MACH_MIPS32, 0, }, + { "powerpc", MACH_PPC32, 0, }, + { "ppc", MACH_PPC32, 0, }, + { "riscv", MACH_RISCV32, 0, }, + { "s390x", MACH_S390X, 64, }, + { "s390", MACH_S390, 32, }, + { "sparc", MACH_SPARC32, 0, }, + { "x86_64", MACH_X86_64, 64, }, + { "x86-64", MACH_X86_64, 64, }, + { NULL }, + }; + const struct arch *p; + + for (p = &archs[0]; p->name; p++) { + size_t len = strlen(p->name); + if (strncmp(p->name, name, len) == 0) { + enum machine mach = p->mach; + const char *suf = name + len; + int bits = p->bits; + + if (bits == 0) { + if (!strcmp(suf, "") || !strcmp(suf, "32")) { + ; + } else if (!strcmp(suf, "64")) { + mach += 1; + } else { + die("invalid architecture: %s", name); + } + } else { + if (strcmp(suf, "")) + die("invalid architecture: %s", name); + } + + return mach; + } + } + + return MACH_UNKNOWN; +} + void target_config(enum machine mach) { @@ -92,6 +92,7 @@ extern const struct target target_x86_64; /* target.c */ extern const struct target *arch_target; +enum machine target_parse(const char *name); void target_config(enum machine mach); void target_init(void); |