gmp-utils: New API to simply use of GMP's integer/rational/float objects
[binutils-gdb.git] / sim / common / sim-model.c
blob1bf122baf988103f3cf4ffb9c180d40d90f49889
1 /* Model support.
2 Copyright (C) 1996-2020 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 #include "sim-main.h"
21 #include "sim-model.h"
22 #include "libiberty.h"
23 #include "sim-options.h"
24 #include "sim-io.h"
25 #include "sim-assert.h"
26 #include "bfd.h"
28 static void model_set (sim_cpu *, const SIM_MODEL *);
30 static DECLARE_OPTION_HANDLER (model_option_handler);
32 static MODULE_INIT_FN sim_model_init;
34 enum {
35 OPTION_MODEL = OPTION_START,
36 OPTION_MODEL_INFO,
39 static const OPTION model_options[] = {
40 { {"model", required_argument, NULL, OPTION_MODEL},
41 '\0', "MODEL", "Specify model to simulate",
42 model_option_handler, NULL },
44 { {"model-info", no_argument, NULL, OPTION_MODEL_INFO},
45 '\0', NULL, "List selectable models",
46 model_option_handler, NULL },
47 { {"info-model", no_argument, NULL, OPTION_MODEL_INFO},
48 '\0', NULL, NULL,
49 model_option_handler, NULL },
51 { {NULL, no_argument, NULL, 0}, '\0', NULL, NULL, NULL, NULL }
54 static SIM_RC
55 model_option_handler (SIM_DESC sd, sim_cpu *cpu, int opt,
56 char *arg, int is_command)
58 switch (opt)
60 case OPTION_MODEL :
62 const SIM_MODEL *model = sim_model_lookup (arg);
63 if (! model)
65 sim_io_eprintf (sd, "unknown model `%s'\n", arg);
66 return SIM_RC_FAIL;
68 sim_model_set (sd, cpu, model);
69 break;
72 case OPTION_MODEL_INFO :
74 const SIM_MACH **machp;
75 const SIM_MODEL *model;
76 for (machp = & sim_machs[0]; *machp != NULL; ++machp)
78 sim_io_printf (sd, "Models for architecture `%s':\n",
79 MACH_NAME (*machp));
80 for (model = MACH_MODELS (*machp); MODEL_NAME (model) != NULL;
81 ++model)
82 sim_io_printf (sd, " %s", MODEL_NAME (model));
83 sim_io_printf (sd, "\n");
85 break;
89 return SIM_RC_OK;
92 SIM_RC
93 sim_model_install (SIM_DESC sd)
95 SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
97 sim_add_option_table (sd, NULL, model_options);
98 sim_module_add_init_fn (sd, sim_model_init);
100 return SIM_RC_OK;
103 /* Subroutine of sim_model_set to set the model for one cpu. */
105 static void
106 model_set (sim_cpu *cpu, const SIM_MODEL *model)
108 CPU_MACH (cpu) = MODEL_MACH (model);
109 CPU_MODEL (cpu) = model;
110 (* MACH_INIT_CPU (MODEL_MACH (model))) (cpu);
111 (* MODEL_INIT (model)) (cpu);
114 /* Set the current model of CPU to MODEL.
115 If CPU is NULL, all cpus are set to MODEL. */
117 void
118 sim_model_set (SIM_DESC sd, sim_cpu *cpu, const SIM_MODEL *model)
120 if (! cpu)
122 int c;
124 for (c = 0; c < MAX_NR_PROCESSORS; ++c)
125 if (STATE_CPU (sd, c))
126 model_set (STATE_CPU (sd, c), model);
128 else
130 model_set (cpu, model);
134 /* Look up model named NAME.
135 Result is pointer to MODEL entry or NULL if not found. */
137 const SIM_MODEL *
138 sim_model_lookup (const char *name)
140 const SIM_MACH **machp;
141 const SIM_MODEL *model;
143 for (machp = & sim_machs[0]; *machp != NULL; ++machp)
145 for (model = MACH_MODELS (*machp); MODEL_NAME (model) != NULL; ++model)
147 if (strcmp (MODEL_NAME (model), name) == 0)
148 return model;
151 return NULL;
154 /* Look up machine named NAME.
155 Result is pointer to MACH entry or NULL if not found. */
157 const SIM_MACH *
158 sim_mach_lookup (const char *name)
160 const SIM_MACH **machp;
162 for (machp = & sim_machs[0]; *machp != NULL; ++machp)
164 if (strcmp (MACH_NAME (*machp), name) == 0)
165 return *machp;
167 return NULL;
170 /* Look up a machine via its bfd name.
171 Result is pointer to MACH entry or NULL if not found. */
173 const SIM_MACH *
174 sim_mach_lookup_bfd_name (const char *name)
176 const SIM_MACH **machp;
178 for (machp = & sim_machs[0]; *machp != NULL; ++machp)
180 if (strcmp (MACH_BFD_NAME (*machp), name) == 0)
181 return *machp;
183 return NULL;
186 /* Initialize model support. */
188 static SIM_RC
189 sim_model_init (SIM_DESC sd)
191 SIM_CPU *cpu;
193 if (!WITH_MODEL_P)
194 return SIM_RC_OK;
196 /* If both cpu model and state architecture are set, ensure they're
197 compatible. If only one is set, set the other. If neither are set,
198 use the default model. STATE_ARCHITECTURE is the bfd_arch_info data
199 for the selected "mach" (bfd terminology). */
201 /* Only check cpu 0. STATE_ARCHITECTURE is for that one only. */
202 /* ??? At present this only supports homogeneous multiprocessors. */
203 cpu = STATE_CPU (sd, 0);
205 if (! STATE_ARCHITECTURE (sd)
206 && ! CPU_MACH (cpu))
208 /* Set the default model. */
209 const SIM_MODEL *model = sim_model_lookup (WITH_DEFAULT_MODEL);
210 SIM_ASSERT (model != NULL);
211 sim_model_set (sd, NULL, model);
214 if (STATE_ARCHITECTURE (sd)
215 && CPU_MACH (cpu))
217 if (strcmp (STATE_ARCHITECTURE (sd)->printable_name,
218 MACH_BFD_NAME (CPU_MACH (cpu))) != 0)
220 sim_io_eprintf (sd, "invalid model `%s' for `%s'\n",
221 MODEL_NAME (CPU_MODEL (cpu)),
222 STATE_ARCHITECTURE (sd)->printable_name);
223 return SIM_RC_FAIL;
226 else if (STATE_ARCHITECTURE (sd))
228 /* Use the default model for the selected machine.
229 The default model is the first one in the list. */
230 const SIM_MACH *mach = sim_mach_lookup_bfd_name (STATE_ARCHITECTURE (sd)->printable_name);
232 if (mach == NULL)
234 sim_io_eprintf (sd, "unsupported machine `%s'\n",
235 STATE_ARCHITECTURE (sd)->printable_name);
236 return SIM_RC_FAIL;
238 sim_model_set (sd, NULL, MACH_MODELS (mach));
240 else
242 STATE_ARCHITECTURE (sd) = bfd_scan_arch (MACH_BFD_NAME (CPU_MACH (cpu)));
245 return SIM_RC_OK;
248 #if !WITH_MODEL_P
249 /* Set up basic model support. This is a stub for ports that do not define
250 models. See sim-model.h for more details. */
251 const SIM_MACH *sim_machs[] =
253 NULL
255 #endif