[04/10] arch: move parsing of --arch=<ARCH> to target.c
diff mbox series

Message ID 20191216223756.2428-5-luc.vanoostenryck@gmail.com
State Mainlined, archived
Headers show
Series
  • move arch specificities in their own files
Related show

Commit Message

Luc Van Oostenryck Dec. 16, 2019, 10:37 p.m. UTC
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>
---
 lib.c    | 50 ++++----------------------------------------------
 target.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 target.h |  1 +
 3 files changed, 58 insertions(+), 46 deletions(-)

Patch
diff mbox series

diff --git a/lib.c b/lib.c
index d759648a504a..82354af7eafa 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 f23ed30881bd..8d2d10588d60 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 0c45ecf41ea2..c6aef9592080 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);