2 Copyright (C) 1996-2021 Free Software Foundation, Inc.
3 Contributed by Cygnus Support.
5 This file is part of GDB, the GNU debugger.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 /* This must come before any other includes. */
24 #include "sim-model.h"
25 #include "libiberty.h"
26 #include "sim-options.h"
28 #include "sim-assert.h"
31 static void model_set (sim_cpu
*, const SIM_MODEL
*);
33 static DECLARE_OPTION_HANDLER (model_option_handler
);
35 static MODULE_INIT_FN sim_model_init
;
38 OPTION_MODEL
= OPTION_START
,
42 static const OPTION model_options
[] = {
43 { {"model", required_argument
, NULL
, OPTION_MODEL
},
44 '\0', "MODEL", "Specify model to simulate",
45 model_option_handler
, NULL
},
47 { {"model-info", no_argument
, NULL
, OPTION_MODEL_INFO
},
48 '\0', NULL
, "List selectable models",
49 model_option_handler
, NULL
},
50 { {"info-model", no_argument
, NULL
, OPTION_MODEL_INFO
},
52 model_option_handler
, NULL
},
54 { {NULL
, no_argument
, NULL
, 0}, '\0', NULL
, NULL
, NULL
, NULL
}
58 model_option_handler (SIM_DESC sd
, sim_cpu
*cpu
, int opt
,
59 char *arg
, int is_command
)
65 const SIM_MODEL
*model
= sim_model_lookup (sd
, arg
);
68 sim_io_eprintf (sd
, "unknown model `%s'\n", arg
);
71 sim_model_set (sd
, cpu
, model
);
75 case OPTION_MODEL_INFO
:
77 const SIM_MACH
* const *machp
;
78 const SIM_MODEL
*model
;
80 if (STATE_MACHS (sd
) == NULL
)
82 sim_io_printf (sd
, "This target does not support any models\n");
86 for (machp
= STATE_MACHS(sd
); *machp
!= NULL
; ++machp
)
88 sim_io_printf (sd
, "Models for architecture `%s':\n",
90 for (model
= MACH_MODELS (*machp
); MODEL_NAME (model
) != NULL
;
92 sim_io_printf (sd
, " %s", MODEL_NAME (model
));
93 sim_io_printf (sd
, "\n");
103 sim_model_install (SIM_DESC sd
)
105 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
107 sim_add_option_table (sd
, NULL
, model_options
);
108 sim_module_add_init_fn (sd
, sim_model_init
);
113 /* Subroutine of sim_model_set to set the model for one cpu. */
116 model_set (sim_cpu
*cpu
, const SIM_MODEL
*model
)
118 CPU_MACH (cpu
) = MODEL_MACH (model
);
119 CPU_MODEL (cpu
) = model
;
120 (* MACH_INIT_CPU (MODEL_MACH (model
))) (cpu
);
121 (* MODEL_INIT (model
)) (cpu
);
124 /* Set the current model of CPU to MODEL.
125 If CPU is NULL, all cpus are set to MODEL. */
128 sim_model_set (SIM_DESC sd
, sim_cpu
*cpu
, const SIM_MODEL
*model
)
134 for (c
= 0; c
< MAX_NR_PROCESSORS
; ++c
)
135 if (STATE_CPU (sd
, c
))
136 model_set (STATE_CPU (sd
, c
), model
);
140 model_set (cpu
, model
);
144 /* Look up model named NAME.
145 Result is pointer to MODEL entry or NULL if not found. */
148 sim_model_lookup (SIM_DESC sd
, const char *name
)
150 const SIM_MACH
* const *machp
;
151 const SIM_MODEL
*model
;
153 if (STATE_MACHS (sd
) == NULL
)
156 for (machp
= STATE_MACHS (sd
); *machp
!= NULL
; ++machp
)
158 for (model
= MACH_MODELS (*machp
); MODEL_NAME (model
) != NULL
; ++model
)
160 if (strcmp (MODEL_NAME (model
), name
) == 0)
167 /* Look up machine named NAME.
168 Result is pointer to MACH entry or NULL if not found. */
171 sim_mach_lookup (SIM_DESC sd
, const char *name
)
173 const SIM_MACH
* const *machp
;
175 if (STATE_MACHS (sd
) == NULL
)
178 for (machp
= STATE_MACHS (sd
); *machp
!= NULL
; ++machp
)
180 if (strcmp (MACH_NAME (*machp
), name
) == 0)
186 /* Look up a machine via its bfd name.
187 Result is pointer to MACH entry or NULL if not found. */
190 sim_mach_lookup_bfd_name (SIM_DESC sd
, const char *name
)
192 const SIM_MACH
* const *machp
;
194 if (STATE_MACHS (sd
) == NULL
)
197 for (machp
= STATE_MACHS (sd
); *machp
!= NULL
; ++machp
)
199 if (strcmp (MACH_BFD_NAME (*machp
), name
) == 0)
205 /* Initialize model support. */
208 sim_model_init (SIM_DESC sd
)
215 /* If both cpu model and state architecture are set, ensure they're
216 compatible. If only one is set, set the other. If neither are set,
217 use the default model. STATE_ARCHITECTURE is the bfd_arch_info data
218 for the selected "mach" (bfd terminology). */
220 /* Only check cpu 0. STATE_ARCHITECTURE is for that one only. */
221 /* ??? At present this only supports homogeneous multiprocessors. */
222 cpu
= STATE_CPU (sd
, 0);
224 if (! STATE_ARCHITECTURE (sd
)
227 /* Set the default model. */
228 const SIM_MODEL
*model
= sim_model_lookup (sd
, WITH_DEFAULT_MODEL
);
229 SIM_ASSERT (model
!= NULL
);
230 sim_model_set (sd
, NULL
, model
);
233 if (STATE_ARCHITECTURE (sd
)
236 if (strcmp (STATE_ARCHITECTURE (sd
)->printable_name
,
237 MACH_BFD_NAME (CPU_MACH (cpu
))) != 0)
239 sim_io_eprintf (sd
, "invalid model `%s' for `%s'\n",
240 MODEL_NAME (CPU_MODEL (cpu
)),
241 STATE_ARCHITECTURE (sd
)->printable_name
);
245 else if (STATE_ARCHITECTURE (sd
))
247 /* Use the default model for the selected machine.
248 The default model is the first one in the list. */
249 const SIM_MACH
*mach
=
250 sim_mach_lookup_bfd_name (sd
, STATE_ARCHITECTURE (sd
)->printable_name
);
254 sim_io_eprintf (sd
, "unsupported machine `%s'\n",
255 STATE_ARCHITECTURE (sd
)->printable_name
);
258 sim_model_set (sd
, NULL
, MACH_MODELS (mach
));
262 STATE_ARCHITECTURE (sd
) = bfd_scan_arch (MACH_BFD_NAME (CPU_MACH (cpu
)));