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 STATE_MODEL_NAME (sd
) = arg
;
72 sim_model_set (sd
, cpu
, model
);
76 case OPTION_MODEL_INFO
:
78 const SIM_MACH
* const *machp
;
79 const SIM_MODEL
*model
;
81 if (STATE_MACHS (sd
) == NULL
)
83 sim_io_printf (sd
, "This target does not support any models\n");
87 for (machp
= STATE_MACHS(sd
); *machp
!= NULL
; ++machp
)
89 sim_io_printf (sd
, "Models for architecture `%s':\n",
91 for (model
= MACH_MODELS (*machp
); MODEL_NAME (model
) != NULL
;
93 sim_io_printf (sd
, " %s", MODEL_NAME (model
));
94 sim_io_printf (sd
, "\n");
104 sim_model_install (SIM_DESC sd
)
106 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
108 sim_add_option_table (sd
, NULL
, model_options
);
109 sim_module_add_init_fn (sd
, sim_model_init
);
114 /* Subroutine of sim_model_set to set the model for one cpu. */
117 model_set (sim_cpu
*cpu
, const SIM_MODEL
*model
)
119 CPU_MACH (cpu
) = MODEL_MACH (model
);
120 CPU_MODEL (cpu
) = model
;
121 (* MACH_INIT_CPU (MODEL_MACH (model
))) (cpu
);
122 (* MODEL_INIT (model
)) (cpu
);
125 /* Set the current model of CPU to MODEL.
126 If CPU is NULL, all cpus are set to MODEL. */
129 sim_model_set (SIM_DESC sd
, sim_cpu
*cpu
, const SIM_MODEL
*model
)
135 for (c
= 0; c
< MAX_NR_PROCESSORS
; ++c
)
136 if (STATE_CPU (sd
, c
))
137 model_set (STATE_CPU (sd
, c
), model
);
141 model_set (cpu
, model
);
145 /* Look up model named NAME.
146 Result is pointer to MODEL entry or NULL if not found. */
149 sim_model_lookup (SIM_DESC sd
, const char *name
)
151 const SIM_MACH
* const *machp
;
152 const SIM_MODEL
*model
;
154 if (STATE_MACHS (sd
) == NULL
)
157 for (machp
= STATE_MACHS (sd
); *machp
!= NULL
; ++machp
)
159 for (model
= MACH_MODELS (*machp
); MODEL_NAME (model
) != NULL
; ++model
)
161 if (strcmp (MODEL_NAME (model
), name
) == 0)
168 /* Look up machine named NAME.
169 Result is pointer to MACH entry or NULL if not found. */
172 sim_mach_lookup (SIM_DESC sd
, const char *name
)
174 const SIM_MACH
* const *machp
;
176 if (STATE_MACHS (sd
) == NULL
)
179 for (machp
= STATE_MACHS (sd
); *machp
!= NULL
; ++machp
)
181 if (strcmp (MACH_NAME (*machp
), name
) == 0)
187 /* Look up a machine via its bfd name.
188 Result is pointer to MACH entry or NULL if not found. */
191 sim_mach_lookup_bfd_name (SIM_DESC sd
, const char *name
)
193 const SIM_MACH
* const *machp
;
195 if (STATE_MACHS (sd
) == NULL
)
198 for (machp
= STATE_MACHS (sd
); *machp
!= NULL
; ++machp
)
200 if (strcmp (MACH_BFD_NAME (*machp
), name
) == 0)
206 /* Initialize model support. */
209 sim_model_init (SIM_DESC sd
)
213 /* If both cpu model and state architecture are set, ensure they're
214 compatible. If only one is set, set the other. If neither are set,
215 use the default model. STATE_ARCHITECTURE is the bfd_arch_info data
216 for the selected "mach" (bfd terminology). */
218 /* Only check cpu 0. STATE_ARCHITECTURE is for that one only. */
219 /* ??? At present this only supports homogeneous multiprocessors. */
220 cpu
= STATE_CPU (sd
, 0);
222 if (! STATE_ARCHITECTURE (sd
)
224 && STATE_MODEL_NAME (sd
))
226 /* Set the default model. */
227 const SIM_MODEL
*model
= sim_model_lookup (sd
, STATE_MODEL_NAME (sd
));
228 SIM_ASSERT (model
!= NULL
);
229 sim_model_set (sd
, NULL
, model
);
232 if (STATE_ARCHITECTURE (sd
)
235 if (strcmp (STATE_ARCHITECTURE (sd
)->printable_name
,
236 MACH_BFD_NAME (CPU_MACH (cpu
))) != 0)
238 sim_io_eprintf (sd
, "invalid model `%s' for `%s'\n",
239 MODEL_NAME (CPU_MODEL (cpu
)),
240 STATE_ARCHITECTURE (sd
)->printable_name
);
244 else if (STATE_ARCHITECTURE (sd
) && STATE_MACHS (sd
))
246 /* Use the default model for the selected machine.
247 The default model is the first one in the list. */
248 const SIM_MACH
*mach
=
249 sim_mach_lookup_bfd_name (sd
, STATE_ARCHITECTURE (sd
)->printable_name
);
253 sim_io_eprintf (sd
, "unsupported machine `%s'\n",
254 STATE_ARCHITECTURE (sd
)->printable_name
);
257 sim_model_set (sd
, NULL
, MACH_MODELS (mach
));
259 else if (CPU_MACH (cpu
))
261 STATE_ARCHITECTURE (sd
) = bfd_scan_arch (MACH_BFD_NAME (CPU_MACH (cpu
)));