aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorLuc Van Oostenryck <luc.vanoostenryck@gmail.com>2019-12-15 09:00:36 +0100
committerLuc Van Oostenryck <luc.vanoostenryck@gmail.com>2019-12-16 10:27:47 +0100
commit47ed6c8134ea0b3459877f364969944426f7cdb5 (patch)
treeeeb47775a7250c45f1d3a1176959f29fc1fdf28d
parentd407146a779162d817c2d163c93c322d3bd07985 (diff)
downloadsparse-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.c50
-rw-r--r--target.c53
-rw-r--r--target.h1
3 files changed, 58 insertions, 46 deletions
diff --git a/lib.c b/lib.c
index d759648a..82354af7 100644
--- a/lib.c
+++ b/lib.c
@@ -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;
}
diff --git a/target.c b/target.c
index f23ed308..8d2d1058 100644
--- a/target.c
+++ b/target.c
@@ -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)
{
diff --git a/target.h b/target.h
index 0c45ecf4..c6aef959 100644
--- a/target.h
+++ b/target.h
@@ -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);