]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - sim/common/sim-config.c
sim: overhaul & unify endian settings management
[thirdparty/binutils-gdb.git] / sim / common / sim-config.c
CommitLineData
b85e4829
AC
1/* The common simulator framework for GDB, the GNU Debugger.
2
3666a048 3 Copyright 2002-2021 Free Software Foundation, Inc.
b85e4829
AC
4
5 Contributed by Andrew Cagney and Red Hat.
6
7 This file is part of GDB.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
4744ac1b 11 the Free Software Foundation; either version 3 of the License, or
b85e4829
AC
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
4744ac1b 20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c 21
6df01ab8
MF
22/* This must come before any other includes. */
23#include "defs.h"
c906108c
SS
24
25#include "sim-main.h"
26#include "sim-assert.h"
27#include "bfd.h"
28
29
1ac72f06 30enum bfd_endian current_target_byte_order = BFD_ENDIAN_UNKNOWN;
c906108c
SS
31int current_stdio;
32
33enum sim_alignments current_alignment;
34
35#if defined (WITH_FLOATING_POINT)
36int current_floating_point;
37#endif
38
39
40
41/* map a byte order onto a textual string */
42
43static const char *
1ac72f06 44config_byte_order_to_a (enum bfd_endian byte_order)
c906108c
SS
45{
46 switch (byte_order)
47 {
1ac72f06 48 case BFD_ENDIAN_LITTLE:
c906108c 49 return "LITTLE_ENDIAN";
1ac72f06 50 case BFD_ENDIAN_BIG:
c906108c 51 return "BIG_ENDIAN";
1ac72f06
MF
52 case BFD_ENDIAN_UNKNOWN:
53 return "UNKNOWN_ENDIAN";
c906108c
SS
54 }
55 return "UNKNOWN";
56}
57
58
59static const char *
60config_stdio_to_a (int stdio)
61{
62 switch (stdio)
63 {
64 case DONT_USE_STDIO:
65 return "DONT_USE_STDIO";
66 case DO_USE_STDIO:
67 return "DO_USE_STDIO";
68 case 0:
69 return "0";
70 }
71 return "UNKNOWN";
72}
73
74
75static const char *
76config_environment_to_a (enum sim_environment environment)
77{
78 switch (environment)
79 {
80 case ALL_ENVIRONMENT:
81 return "ALL_ENVIRONMENT";
82 case USER_ENVIRONMENT:
83 return "USER_ENVIRONMENT";
84 case VIRTUAL_ENVIRONMENT:
85 return "VIRTUAL_ENVIRONMENT";
86 case OPERATING_ENVIRONMENT:
87 return "OPERATING_ENVIRONMENT";
88 }
89 return "UNKNOWN";
90}
91
92
93static const char *
94config_alignment_to_a (enum sim_alignments alignment)
95{
96 switch (alignment)
97 {
98 case MIXED_ALIGNMENT:
99 return "MIXED_ALIGNMENT";
100 case NONSTRICT_ALIGNMENT:
101 return "NONSTRICT_ALIGNMENT";
102 case STRICT_ALIGNMENT:
103 return "STRICT_ALIGNMENT";
104 case FORCED_ALIGNMENT:
105 return "FORCED_ALIGNMENT";
106 }
107 return "UNKNOWN";
108}
109
110
111#if defined (WITH_FLOATING_POINT)
112static const char *
113config_floating_point_to_a (int floating_point)
114{
115 switch (floating_point)
116 {
117 case SOFT_FLOATING_POINT:
118 return "SOFT_FLOATING_POINT";
119 case HARD_FLOATING_POINT:
120 return "HARD_FLOATING_POINT";
121 case 0:
122 return "0";
123 }
124 return "UNKNOWN";
125}
126#endif
127
128/* Set the default environment, prior to parsing argv. */
129
130void
131sim_config_default (SIM_DESC sd)
132{
133 /* Set the current environment to ALL_ENVIRONMENT to indicate none has been
134 selected yet. This is so that after parsing argv, we know whether the
135 environment was explicitly specified or not. */
136 STATE_ENVIRONMENT (sd) = ALL_ENVIRONMENT;
137}
138
139/* Complete and verify the simulation environment. */
140
141SIM_RC
142sim_config (SIM_DESC sd)
143{
1ac72f06 144 enum bfd_endian prefered_target_byte_order;
c906108c
SS
145 SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
146
147 /* extract all relevant information */
1d72487d
HPN
148 if (STATE_PROG_BFD (sd) == NULL
149 /* If we have a binary input file (presumably with specified
150 "--architecture"), it'll have no endianness. */
151 || (!bfd_little_endian (STATE_PROG_BFD (sd))
152 && !bfd_big_endian (STATE_PROG_BFD (sd))))
1ac72f06 153 prefered_target_byte_order = BFD_ENDIAN_UNKNOWN;
c906108c 154 else
34b47c38 155 prefered_target_byte_order = (bfd_little_endian (STATE_PROG_BFD (sd))
1ac72f06
MF
156 ? BFD_ENDIAN_LITTLE
157 : BFD_ENDIAN_BIG);
c906108c 158
c906108c
SS
159 /* set the target byte order */
160#if (WITH_TREE_PROPERTIES)
1ac72f06 161 if (current_target_byte_order == BFD_ENDIAN_UNKNOWN)
c906108c
SS
162 current_target_byte_order
163 = (tree_find_boolean_property (root, "/options/little-endian?")
1ac72f06
MF
164 ? BFD_ENDIAN_LITTLE
165 : BFD_ENDIAN_BIG);
c906108c 166#endif
1ac72f06
MF
167 if (current_target_byte_order == BFD_ENDIAN_UNKNOWN
168 && prefered_target_byte_order != BFD_ENDIAN_UNKNOWN)
c906108c 169 current_target_byte_order = prefered_target_byte_order;
1ac72f06 170 if (current_target_byte_order == BFD_ENDIAN_UNKNOWN)
c906108c 171 current_target_byte_order = WITH_TARGET_BYTE_ORDER;
c906108c
SS
172
173 /* verify the target byte order */
1ac72f06 174 if (CURRENT_TARGET_BYTE_ORDER == BFD_ENDIAN_UNKNOWN)
c906108c
SS
175 {
176 sim_io_eprintf (sd, "Target byte order unspecified\n");
177 return SIM_RC_FAIL;
178 }
179 if (CURRENT_TARGET_BYTE_ORDER != current_target_byte_order)
180 sim_io_eprintf (sd, "Target (%s) and configured (%s) byte order in conflict\n",
181 config_byte_order_to_a (current_target_byte_order),
182 config_byte_order_to_a (CURRENT_TARGET_BYTE_ORDER));
1ac72f06 183 if (prefered_target_byte_order != BFD_ENDIAN_UNKNOWN
c906108c
SS
184 && CURRENT_TARGET_BYTE_ORDER != prefered_target_byte_order)
185 sim_io_eprintf (sd, "Target (%s) and specified (%s) byte order in conflict\n",
186 config_byte_order_to_a (CURRENT_TARGET_BYTE_ORDER),
187 config_byte_order_to_a (prefered_target_byte_order));
188
189
190 /* set the stdio */
191 if (current_stdio == 0)
192 current_stdio = WITH_STDIO;
193 if (current_stdio == 0)
194 current_stdio = DO_USE_STDIO;
195
196 /* verify the stdio */
197 if (CURRENT_STDIO == 0)
198 {
199 sim_io_eprintf (sd, "Target standard IO unspecified\n");
200 return SIM_RC_FAIL;
201 }
202 if (CURRENT_STDIO != current_stdio)
203 {
204 sim_io_eprintf (sd, "Target (%s) and configured (%s) standard IO in conflict\n",
205 config_stdio_to_a (CURRENT_STDIO),
206 config_stdio_to_a (current_stdio));
207 return SIM_RC_FAIL;
208 }
028f6515
MF
209
210
c906108c
SS
211 /* check the value of MSB */
212 if (WITH_TARGET_WORD_MSB != 0
213 && WITH_TARGET_WORD_MSB != (WITH_TARGET_WORD_BITSIZE - 1))
214 {
215 sim_io_eprintf (sd, "Target bitsize (%d) contradicts target most significant bit (%d)\n",
216 WITH_TARGET_WORD_BITSIZE, WITH_TARGET_WORD_MSB);
217 return SIM_RC_FAIL;
218 }
028f6515
MF
219
220
c906108c
SS
221 /* set the environment */
222#if (WITH_TREE_PROPERTIES)
223 if (STATE_ENVIRONMENT (sd) == ALL_ENVIRONMENT)
224 {
225 const char *env =
34b47c38
MF
226 tree_find_string_property (root, "/openprom/options/env");
227 STATE_ENVIRONMENT (sd) = ((strcmp (env, "user") == 0
228 || strcmp (env, "uea") == 0)
c906108c 229 ? USER_ENVIRONMENT
34b47c38
MF
230 : (strcmp (env, "virtual") == 0
231 || strcmp (env, "vea") == 0)
c906108c 232 ? VIRTUAL_ENVIRONMENT
34b47c38
MF
233 : (strcmp (env, "operating") == 0
234 || strcmp (env, "oea") == 0)
c906108c
SS
235 ? OPERATING_ENVIRONMENT
236 : ALL_ENVIRONMENT);
237 }
238#endif
239 if (STATE_ENVIRONMENT (sd) == ALL_ENVIRONMENT)
ce39bd38
MF
240 STATE_ENVIRONMENT (sd) = (WITH_ENVIRONMENT != ALL_ENVIRONMENT ?
241 WITH_ENVIRONMENT : USER_ENVIRONMENT);
028f6515
MF
242
243
c906108c
SS
244 /* set the alignment */
245#if (WITH_TREE_PROPERTIES)
246 if (current_alignment == 0)
247 current_alignment =
34b47c38 248 (tree_find_boolean_property (root, "/openprom/options/strict-alignment?")
c906108c
SS
249 ? STRICT_ALIGNMENT
250 : NONSTRICT_ALIGNMENT);
251#endif
252 if (current_alignment == 0)
253 current_alignment = WITH_ALIGNMENT;
ba307cdd 254 /* If the port hasn't specified an alignment, default to not enforcing. */
c906108c 255 if (current_alignment == 0)
ba307cdd 256 current_alignment = NONSTRICT_ALIGNMENT;
028f6515 257
c906108c
SS
258 /* verify the alignment */
259 if (CURRENT_ALIGNMENT == 0)
260 {
261 sim_io_eprintf (sd, "Target alignment unspecified\n");
262 return SIM_RC_FAIL;
263 }
264 if (CURRENT_ALIGNMENT != current_alignment)
265 {
266 sim_io_eprintf (sd, "Target (%s) and configured (%s) alignment in conflict\n",
267 config_alignment_to_a (CURRENT_ALIGNMENT),
268 config_alignment_to_a (current_alignment));
269 return SIM_RC_FAIL;
270 }
028f6515 271
c906108c 272#if defined (WITH_FLOATING_POINT)
028f6515 273
c906108c
SS
274 /* set the floating point */
275 if (current_floating_point == 0)
276 current_floating_point = WITH_FLOATING_POINT;
028f6515 277
c906108c
SS
278 /* verify the floating point */
279 if (CURRENT_FLOATING_POINT == 0)
280 {
281 sim_io_eprintf (sd, "Target floating-point unspecified\n");
282 return SIM_RC_FAIL;
283 }
284 if (CURRENT_FLOATING_POINT != current_floating_point)
285 {
286 sim_io_eprintf (sd, "Target (%s) and configured (%s) floating-point in conflict\n",
287 config_alignment_to_a (CURRENT_FLOATING_POINT),
288 config_alignment_to_a (current_floating_point));
289 return SIM_RC_FAIL;
290 }
028f6515 291
c906108c
SS
292#endif
293 return SIM_RC_OK;
294}
295
296
297void
2b667e32 298sim_config_print (SIM_DESC sd)
c906108c 299{
34ac507d 300 sim_io_printf (sd, "WITH_TARGET_BYTE_ORDER = %s\n",
c906108c
SS
301 config_byte_order_to_a (WITH_TARGET_BYTE_ORDER));
302
34ac507d 303 sim_io_printf (sd, "HOST_BYTE_ORDER = %s\n",
0cb8d851 304 config_byte_order_to_a (HOST_BYTE_ORDER));
c906108c 305
34ac507d 306 sim_io_printf (sd, "WITH_STDIO = %s\n",
c906108c
SS
307 config_stdio_to_a (WITH_STDIO));
308
34ac507d 309 sim_io_printf (sd, "WITH_TARGET_WORD_MSB = %d\n",
c906108c
SS
310 WITH_TARGET_WORD_MSB);
311
312 sim_io_printf (sd, "WITH_TARGET_WORD_BITSIZE = %d\n",
313 WITH_TARGET_WORD_BITSIZE);
314
315 sim_io_printf (sd, "WITH_TARGET_ADDRESS_BITSIZE = %d\n",
316 WITH_TARGET_ADDRESS_BITSIZE);
317
318 sim_io_printf (sd, "WITH_TARGET_CELL_BITSIZE = %d\n",
319 WITH_TARGET_CELL_BITSIZE);
320
321 sim_io_printf (sd, "WITH_TARGET_FLOATING_POINT_BITSIZE = %d\n",
322 WITH_TARGET_FLOATING_POINT_BITSIZE);
323
324 sim_io_printf (sd, "WITH_ENVIRONMENT = %s\n",
325 config_environment_to_a (WITH_ENVIRONMENT));
326
327 sim_io_printf (sd, "WITH_ALIGNMENT = %s\n",
328 config_alignment_to_a (WITH_ALIGNMENT));
329
c906108c
SS
330#if defined (WITH_XOR_ENDIAN)
331 sim_io_printf (sd, "WITH_XOR_ENDIAN = %d\n", WITH_XOR_ENDIAN);
332#endif
333
334#if defined (WITH_FLOATING_POINT)
335 sim_io_printf (sd, "WITH_FLOATING_POINT = %s\n",
336 config_floating_point_to_a (WITH_FLOATING_POINT));
337#endif
338
339#if defined (WITH_SMP)
340 sim_io_printf (sd, "WITH_SMP = %d\n", WITH_SMP);
341#endif
342
343#if defined (WITH_RESERVED_BITS)
344 sim_io_printf (sd, "WITH_RESERVED_BITS = %d\n", WITH_RESERVED_BITS);
345#endif
028f6515 346
c906108c
SS
347#if defined (WITH_PROFILE)
348 sim_io_printf (sd, "WITH_PROFILE = %d\n", WITH_PROFILE);
349#endif
028f6515 350
c906108c 351}