lm32: cleanup cpu type name composition

introduce LM32_CPU_TYPE_NAME macro and consistently use it
to construct cpu type names. While at it replace dynamic
cpu type name composition with static data.

Signed-off-by: Igor Mammedov <imammedo@redhat.com>
Reviewed-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
Acked-by: Michael Walle <michael@walle.cc>
Message-Id: <1507211474-188400-9-git-send-email-imammedo@redhat.com>
Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
This commit is contained in:
Igor Mammedov 2017-10-05 15:50:42 +02:00 committed by Eduardo Habkost
parent 5eab493d7a
commit c6678108ba
2 changed files with 25 additions and 52 deletions

View File

@ -51,7 +51,7 @@ static void lm32_cpu_list_entry(gpointer data, gpointer user_data)
const char *typename = object_class_get_name(oc); const char *typename = object_class_get_name(oc);
char *name; char *name;
name = g_strndup(typename, strlen(typename) - strlen("-" TYPE_LM32_CPU)); name = g_strndup(typename, strlen(typename) - strlen(LM32_CPU_TYPE_SUFFIX));
(*s->cpu_fprintf)(s->file, " %s\n", name); (*s->cpu_fprintf)(s->file, " %s\n", name);
g_free(name); g_free(name);
} }
@ -215,32 +215,12 @@ static void lm32_full_cpu_initfn(Object *obj)
| LM32_FEATURE_CYCLE_COUNT; | LM32_FEATURE_CYCLE_COUNT;
} }
typedef struct LM32CPUInfo {
const char *name;
void (*initfn)(Object *obj);
} LM32CPUInfo;
static const LM32CPUInfo lm32_cpus[] = {
{
.name = "lm32-basic",
.initfn = lm32_basic_cpu_initfn,
},
{
.name = "lm32-standard",
.initfn = lm32_standard_cpu_initfn,
},
{
.name = "lm32-full",
.initfn = lm32_full_cpu_initfn,
},
};
static ObjectClass *lm32_cpu_class_by_name(const char *cpu_model) static ObjectClass *lm32_cpu_class_by_name(const char *cpu_model)
{ {
ObjectClass *oc; ObjectClass *oc;
char *typename; char *typename;
typename = g_strdup_printf("%s-" TYPE_LM32_CPU, cpu_model); typename = g_strdup_printf(LM32_CPU_TYPE_NAME("%s"), cpu_model);
oc = object_class_by_name(typename); oc = object_class_by_name(typename);
g_free(typename); g_free(typename);
if (oc != NULL && (!object_class_dynamic_cast(oc, TYPE_LM32_CPU) || if (oc != NULL && (!object_class_dynamic_cast(oc, TYPE_LM32_CPU) ||
@ -283,36 +263,26 @@ static void lm32_cpu_class_init(ObjectClass *oc, void *data)
cc->tcg_initialize = lm32_translate_init; cc->tcg_initialize = lm32_translate_init;
} }
static void lm32_register_cpu_type(const LM32CPUInfo *info) #define DEFINE_LM32_CPU_TYPE(cpu_model, initfn) \
{ { \
TypeInfo type_info = { .parent = TYPE_LM32_CPU, \
.parent = TYPE_LM32_CPU, .name = LM32_CPU_TYPE_NAME(cpu_model), \
.instance_init = info->initfn, .instance_init = initfn, \
}; }
type_info.name = g_strdup_printf("%s-" TYPE_LM32_CPU, info->name); static const TypeInfo lm32_cpus_type_infos[] = {
type_register(&type_info); { /* base class should be registered first */
g_free((void *)type_info.name); .name = TYPE_LM32_CPU,
} .parent = TYPE_CPU,
.instance_size = sizeof(LM32CPU),
static const TypeInfo lm32_cpu_type_info = { .instance_init = lm32_cpu_initfn,
.name = TYPE_LM32_CPU, .abstract = true,
.parent = TYPE_CPU, .class_size = sizeof(LM32CPUClass),
.instance_size = sizeof(LM32CPU), .class_init = lm32_cpu_class_init,
.instance_init = lm32_cpu_initfn, },
.abstract = true, DEFINE_LM32_CPU_TYPE("lm32-basic", lm32_basic_cpu_initfn),
.class_size = sizeof(LM32CPUClass), DEFINE_LM32_CPU_TYPE("lm32-standard", lm32_standard_cpu_initfn),
.class_init = lm32_cpu_class_init, DEFINE_LM32_CPU_TYPE("lm32-full", lm32_full_cpu_initfn),
}; };
static void lm32_cpu_register_types(void) DEFINE_TYPES(lm32_cpus_type_infos)
{
int i;
type_register_static(&lm32_cpu_type_info);
for (i = 0; i < ARRAY_SIZE(lm32_cpus); i++) {
lm32_register_cpu_type(&lm32_cpus[i]);
}
}
type_init(lm32_cpu_register_types)

View File

@ -257,6 +257,9 @@ bool lm32_cpu_do_semihosting(CPUState *cs);
#define cpu_init(cpu_model) cpu_generic_init(TYPE_LM32_CPU, cpu_model) #define cpu_init(cpu_model) cpu_generic_init(TYPE_LM32_CPU, cpu_model)
#define LM32_CPU_TYPE_SUFFIX "-" TYPE_LM32_CPU
#define LM32_CPU_TYPE_NAME(model) model LM32_CPU_TYPE_SUFFIX
#define cpu_list lm32_cpu_list #define cpu_list lm32_cpu_list
#define cpu_signal_handler cpu_lm32_signal_handler #define cpu_signal_handler cpu_lm32_signal_handler