diff mbox series

[v2,22/40] sh4: simplify superh_cpu_class_by_name()

Message ID 1507211474-188400-23-git-send-email-imammedo@redhat.com
State New
Headers show
Series generalize parsing of cpu_model (part 2) | expand

Commit Message

Igor Mammedov Oct. 5, 2017, 1:50 p.m. UTC
currently for sh4 cpu_model argument for '-cpu' option
could be either 'cpu model' name or cpu_typename.

however typically '-cpu' takes 'cpu model' name and
cpu type for sh4 target isn't advertised publicly
('-cpu help' prints only 'cpu model' names) so we
shouldn't care about this use case (it's more of a bug).

1. Drop '-cpu cpu_typename' to align with the rest of
   targets.
2. Compose searched for typename from cpu model and use
   it with object_class_by_name() directly instead of
   over-complicated
       object_class_get_list()
       g_slist_find_custom() + superh_cpu_name_compare()

With #1 droped, #2 could be used for both lookups which
simplifies superh_cpu_class_by_name() quite a bit.

Signed-off-by: Igor Mammedov <imammedo@redhat.com>
Acked-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
---
v2:
  - there is no need to NULL-initialize 'oc' in superh_cpu_class_by_name()
    also drop extra ';' (Philippe Mathieu-Daudé)

CC: aurelien@aurel32.net
---
 target/sh4/cpu.c | 34 ++++++++++++----------------------
 1 file changed, 12 insertions(+), 22 deletions(-)

Comments

Philippe Mathieu-Daudé Oct. 5, 2017, 3:16 p.m. UTC | #1
On 10/05/2017 10:50 AM, Igor Mammedov wrote:
> currently for sh4 cpu_model argument for '-cpu' option
> could be either 'cpu model' name or cpu_typename.
> 
> however typically '-cpu' takes 'cpu model' name and
> cpu type for sh4 target isn't advertised publicly
> ('-cpu help' prints only 'cpu model' names) so we
> shouldn't care about this use case (it's more of a bug).
> 
> 1. Drop '-cpu cpu_typename' to align with the rest of
>    targets.
> 2. Compose searched for typename from cpu model and use
>    it with object_class_by_name() directly instead of
>    over-complicated
>        object_class_get_list()
>        g_slist_find_custom() + superh_cpu_name_compare()
> 
> With #1 droped, #2 could be used for both lookups which
> simplifies superh_cpu_class_by_name() quite a bit.
> 
> Signed-off-by: Igor Mammedov <imammedo@redhat.com>

Reviewed-by: Philippe Mathieu-Daudé <f4bug@amsat.org>

> ---
> v2:
>   - there is no need to NULL-initialize 'oc' in superh_cpu_class_by_name()
>     also drop extra ';' (Philippe Mathieu-Daudé)
> 
> CC: aurelien@aurel32.net
> ---
>  target/sh4/cpu.c | 34 ++++++++++++----------------------
>  1 file changed, 12 insertions(+), 22 deletions(-)
> 
> diff --git a/target/sh4/cpu.c b/target/sh4/cpu.c
> index 72f1dec..e715560 100644
> --- a/target/sh4/cpu.c
> +++ b/target/sh4/cpu.c
> @@ -120,36 +120,26 @@ void sh4_cpu_list(FILE *f, fprintf_function cpu_fprintf)
>      g_slist_free(list);
>  }
>  
> -static gint superh_cpu_name_compare(gconstpointer a, gconstpointer b)
> -{
> -    const SuperHCPUClass *scc = SUPERH_CPU_CLASS(a);
> -    const char *name = b;
> -
> -    return strcasecmp(scc->name, name);
> -}
> -
>  static ObjectClass *superh_cpu_class_by_name(const char *cpu_model)
>  {
>      ObjectClass *oc;
> -    GSList *list, *item;
> +    char *s, *typename = NULL;
>  
> -    if (strcasecmp(cpu_model, "any") == 0) {
> -        return object_class_by_name(TYPE_SH7750R_CPU);
> +    s = g_ascii_strdown(cpu_model, -1);
> +    if (strcmp(s, "any") == 0) {
> +        oc = object_class_by_name(TYPE_SH7750R_CPU);
> +        goto out;
>      }
>  
> -    oc = object_class_by_name(cpu_model);
> -    if (oc != NULL && object_class_dynamic_cast(oc, TYPE_SUPERH_CPU) != NULL
> -        && !object_class_is_abstract(oc)) {
> -        return oc;
> +    typename = g_strdup_printf(SUPERH_CPU_TYPE_NAME("%s"), s);
> +    oc = object_class_by_name(typename);
> +    if (oc != NULL && object_class_is_abstract(oc)) {
> +        oc = NULL;
>      }
>  
> -    oc = NULL;
> -    list = object_class_get_list(TYPE_SUPERH_CPU, false);
> -    item = g_slist_find_custom(list, cpu_model, superh_cpu_name_compare);
> -    if (item != NULL) {
> -        oc = item->data;
> -    }
> -    g_slist_free(list);
> +out:
> +    g_free(s);
> +    g_free(typename);
>      return oc;
>  }
>  
>
diff mbox series

Patch

diff --git a/target/sh4/cpu.c b/target/sh4/cpu.c
index 72f1dec..e715560 100644
--- a/target/sh4/cpu.c
+++ b/target/sh4/cpu.c
@@ -120,36 +120,26 @@  void sh4_cpu_list(FILE *f, fprintf_function cpu_fprintf)
     g_slist_free(list);
 }
 
-static gint superh_cpu_name_compare(gconstpointer a, gconstpointer b)
-{
-    const SuperHCPUClass *scc = SUPERH_CPU_CLASS(a);
-    const char *name = b;
-
-    return strcasecmp(scc->name, name);
-}
-
 static ObjectClass *superh_cpu_class_by_name(const char *cpu_model)
 {
     ObjectClass *oc;
-    GSList *list, *item;
+    char *s, *typename = NULL;
 
-    if (strcasecmp(cpu_model, "any") == 0) {
-        return object_class_by_name(TYPE_SH7750R_CPU);
+    s = g_ascii_strdown(cpu_model, -1);
+    if (strcmp(s, "any") == 0) {
+        oc = object_class_by_name(TYPE_SH7750R_CPU);
+        goto out;
     }
 
-    oc = object_class_by_name(cpu_model);
-    if (oc != NULL && object_class_dynamic_cast(oc, TYPE_SUPERH_CPU) != NULL
-        && !object_class_is_abstract(oc)) {
-        return oc;
+    typename = g_strdup_printf(SUPERH_CPU_TYPE_NAME("%s"), s);
+    oc = object_class_by_name(typename);
+    if (oc != NULL && object_class_is_abstract(oc)) {
+        oc = NULL;
     }
 
-    oc = NULL;
-    list = object_class_get_list(TYPE_SUPERH_CPU, false);
-    item = g_slist_find_custom(list, cpu_model, superh_cpu_name_compare);
-    if (item != NULL) {
-        oc = item->data;
-    }
-    g_slist_free(list);
+out:
+    g_free(s);
+    g_free(typename);
     return oc;
 }