sim: unify bfd library dependency testing logic
[deliverable/binutils-gdb.git] / sim / common / sim-model.c
CommitLineData
c906108c 1/* Model support.
3666a048 2 Copyright (C) 1996-2021 Free Software Foundation, Inc.
c906108c
SS
3 Contributed by Cygnus Support.
4
5This file is part of GDB, the GNU debugger.
6
7This program is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
4744ac1b
JB
9the Free Software Foundation; either version 3 of the License, or
10(at your option) any later version.
c906108c
SS
11
12This program is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
4744ac1b
JB
17You should have received a copy of the GNU General Public License
18along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c 19
6df01ab8
MF
20/* This must come before any other includes. */
21#include "defs.h"
22
c906108c 23#include "sim-main.h"
fd87baa9 24#include "sim-model.h"
c906108c
SS
25#include "libiberty.h"
26#include "sim-options.h"
27#include "sim-io.h"
28#include "sim-assert.h"
29#include "bfd.h"
30
8a0ebee6 31static void model_set (sim_cpu *, const SIM_MODEL *);
c906108c
SS
32
33static DECLARE_OPTION_HANDLER (model_option_handler);
34
35static MODULE_INIT_FN sim_model_init;
36
bd0bd508
MF
37enum {
38 OPTION_MODEL = OPTION_START,
39 OPTION_MODEL_INFO,
40};
c906108c
SS
41
42static const OPTION model_options[] = {
43 { {"model", required_argument, NULL, OPTION_MODEL},
44 '\0', "MODEL", "Specify model to simulate",
21cf617c 45 model_option_handler, NULL },
bd0bd508
MF
46
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},
51 '\0', NULL, NULL,
52 model_option_handler, NULL },
53
21cf617c 54 { {NULL, no_argument, NULL, 0}, '\0', NULL, NULL, NULL, NULL }
c906108c
SS
55};
56
57static SIM_RC
58model_option_handler (SIM_DESC sd, sim_cpu *cpu, int opt,
59 char *arg, int is_command)
60{
61 switch (opt)
62 {
63 case OPTION_MODEL :
64 {
8a0ebee6 65 const SIM_MODEL *model = sim_model_lookup (arg);
c906108c
SS
66 if (! model)
67 {
53a5351d 68 sim_io_eprintf (sd, "unknown model `%s'\n", arg);
c906108c
SS
69 return SIM_RC_FAIL;
70 }
71 sim_model_set (sd, cpu, model);
72 break;
73 }
bd0bd508
MF
74
75 case OPTION_MODEL_INFO :
76 {
8a0ebee6
MF
77 const SIM_MACH **machp;
78 const SIM_MODEL *model;
bd0bd508
MF
79 for (machp = & sim_machs[0]; *machp != NULL; ++machp)
80 {
81 sim_io_printf (sd, "Models for architecture `%s':\n",
82 MACH_NAME (*machp));
83 for (model = MACH_MODELS (*machp); MODEL_NAME (model) != NULL;
84 ++model)
85 sim_io_printf (sd, " %s", MODEL_NAME (model));
86 sim_io_printf (sd, "\n");
87 }
88 break;
89 }
c906108c
SS
90 }
91
92 return SIM_RC_OK;
93}
94
95SIM_RC
96sim_model_install (SIM_DESC sd)
97{
98 SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
99
100 sim_add_option_table (sd, NULL, model_options);
101 sim_module_add_init_fn (sd, sim_model_init);
102
103 return SIM_RC_OK;
104}
105
106/* Subroutine of sim_model_set to set the model for one cpu. */
107
108static void
8a0ebee6 109model_set (sim_cpu *cpu, const SIM_MODEL *model)
c906108c
SS
110{
111 CPU_MACH (cpu) = MODEL_MACH (model);
112 CPU_MODEL (cpu) = model;
113 (* MACH_INIT_CPU (MODEL_MACH (model))) (cpu);
114 (* MODEL_INIT (model)) (cpu);
115}
116
117/* Set the current model of CPU to MODEL.
118 If CPU is NULL, all cpus are set to MODEL. */
119
120void
8a0ebee6 121sim_model_set (SIM_DESC sd, sim_cpu *cpu, const SIM_MODEL *model)
c906108c
SS
122{
123 if (! cpu)
124 {
125 int c;
126
127 for (c = 0; c < MAX_NR_PROCESSORS; ++c)
128 if (STATE_CPU (sd, c))
129 model_set (STATE_CPU (sd, c), model);
130 }
131 else
132 {
133 model_set (cpu, model);
134 }
135}
136
137/* Look up model named NAME.
138 Result is pointer to MODEL entry or NULL if not found. */
139
8a0ebee6 140const SIM_MODEL *
c906108c
SS
141sim_model_lookup (const char *name)
142{
8a0ebee6
MF
143 const SIM_MACH **machp;
144 const SIM_MODEL *model;
c906108c
SS
145
146 for (machp = & sim_machs[0]; *machp != NULL; ++machp)
147 {
148 for (model = MACH_MODELS (*machp); MODEL_NAME (model) != NULL; ++model)
149 {
150 if (strcmp (MODEL_NAME (model), name) == 0)
151 return model;
152 }
153 }
154 return NULL;
155}
156
157/* Look up machine named NAME.
158 Result is pointer to MACH entry or NULL if not found. */
159
8a0ebee6 160const SIM_MACH *
c906108c
SS
161sim_mach_lookup (const char *name)
162{
8a0ebee6 163 const SIM_MACH **machp;
c906108c
SS
164
165 for (machp = & sim_machs[0]; *machp != NULL; ++machp)
166 {
167 if (strcmp (MACH_NAME (*machp), name) == 0)
168 return *machp;
169 }
170 return NULL;
171}
172
173/* Look up a machine via its bfd name.
174 Result is pointer to MACH entry or NULL if not found. */
175
8a0ebee6 176const SIM_MACH *
c906108c
SS
177sim_mach_lookup_bfd_name (const char *name)
178{
8a0ebee6 179 const SIM_MACH **machp;
c906108c
SS
180
181 for (machp = & sim_machs[0]; *machp != NULL; ++machp)
182 {
183 if (strcmp (MACH_BFD_NAME (*machp), name) == 0)
184 return *machp;
185 }
186 return NULL;
187}
188
189/* Initialize model support. */
190
191static SIM_RC
192sim_model_init (SIM_DESC sd)
193{
194 SIM_CPU *cpu;
195
0d585950
MF
196 if (!WITH_MODEL_P)
197 return SIM_RC_OK;
198
c906108c
SS
199 /* If both cpu model and state architecture are set, ensure they're
200 compatible. If only one is set, set the other. If neither are set,
201 use the default model. STATE_ARCHITECTURE is the bfd_arch_info data
202 for the selected "mach" (bfd terminology). */
203
204 /* Only check cpu 0. STATE_ARCHITECTURE is for that one only. */
205 /* ??? At present this only supports homogeneous multiprocessors. */
206 cpu = STATE_CPU (sd, 0);
207
208 if (! STATE_ARCHITECTURE (sd)
209 && ! CPU_MACH (cpu))
210 {
211 /* Set the default model. */
8a0ebee6 212 const SIM_MODEL *model = sim_model_lookup (WITH_DEFAULT_MODEL);
ccd4b295 213 SIM_ASSERT (model != NULL);
c906108c
SS
214 sim_model_set (sd, NULL, model);
215 }
216
217 if (STATE_ARCHITECTURE (sd)
218 && CPU_MACH (cpu))
219 {
220 if (strcmp (STATE_ARCHITECTURE (sd)->printable_name,
221 MACH_BFD_NAME (CPU_MACH (cpu))) != 0)
222 {
223 sim_io_eprintf (sd, "invalid model `%s' for `%s'\n",
224 MODEL_NAME (CPU_MODEL (cpu)),
225 STATE_ARCHITECTURE (sd)->printable_name);
226 return SIM_RC_FAIL;
227 }
228 }
229 else if (STATE_ARCHITECTURE (sd))
230 {
231 /* Use the default model for the selected machine.
232 The default model is the first one in the list. */
8a0ebee6 233 const SIM_MACH *mach = sim_mach_lookup_bfd_name (STATE_ARCHITECTURE (sd)->printable_name);
9846de1b
JM
234
235 if (mach == NULL)
236 {
237 sim_io_eprintf (sd, "unsupported machine `%s'\n",
238 STATE_ARCHITECTURE (sd)->printable_name);
239 return SIM_RC_FAIL;
240 }
c906108c
SS
241 sim_model_set (sd, NULL, MACH_MODELS (mach));
242 }
243 else
244 {
245 STATE_ARCHITECTURE (sd) = bfd_scan_arch (MACH_BFD_NAME (CPU_MACH (cpu)));
246 }
247
248 return SIM_RC_OK;
249}
0d585950
MF
250\f
251#if !WITH_MODEL_P
252/* Set up basic model support. This is a stub for ports that do not define
253 models. See sim-model.h for more details. */
254const SIM_MACH *sim_machs[] =
255{
256 NULL
257};
258#endif
This page took 0.964944 seconds and 4 git commands to generate.