1/* The common simulator framework for GDB, the GNU Debugger.
2
3   Copyright 2002-2020 Free Software Foundation, Inc.
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
11   the Free Software Foundation; either version 3 of the License, or
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
20   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
21
22
23#include "sim-main.h"
24#include "sim-assert.h"
25#include "bfd.h"
26
27
28enum bfd_endian current_target_byte_order = BFD_ENDIAN_UNKNOWN;
29int current_stdio;
30
31enum sim_alignments current_alignment;
32
33#if defined (WITH_FLOATING_POINT)
34int current_floating_point;
35#endif
36
37
38
39/* map a byte order onto a textual string */
40
41static const char *
42config_byte_order_to_a (enum bfd_endian byte_order)
43{
44  switch (byte_order)
45    {
46    case BFD_ENDIAN_LITTLE:
47      return "LITTLE_ENDIAN";
48    case BFD_ENDIAN_BIG:
49      return "BIG_ENDIAN";
50    case BFD_ENDIAN_UNKNOWN:
51      return "UNKNOWN_ENDIAN";
52    }
53  return "UNKNOWN";
54}
55
56
57static const char *
58config_stdio_to_a (int stdio)
59{
60  switch (stdio)
61    {
62    case DONT_USE_STDIO:
63      return "DONT_USE_STDIO";
64    case DO_USE_STDIO:
65      return "DO_USE_STDIO";
66    case 0:
67      return "0";
68    }
69  return "UNKNOWN";
70}
71
72
73static const char *
74config_environment_to_a (enum sim_environment environment)
75{
76  switch (environment)
77    {
78    case ALL_ENVIRONMENT:
79      return "ALL_ENVIRONMENT";
80    case USER_ENVIRONMENT:
81      return "USER_ENVIRONMENT";
82    case VIRTUAL_ENVIRONMENT:
83      return "VIRTUAL_ENVIRONMENT";
84    case OPERATING_ENVIRONMENT:
85      return "OPERATING_ENVIRONMENT";
86    }
87  return "UNKNOWN";
88}
89
90
91static const char *
92config_alignment_to_a (enum sim_alignments alignment)
93{
94  switch (alignment)
95    {
96    case MIXED_ALIGNMENT:
97      return "MIXED_ALIGNMENT";
98    case NONSTRICT_ALIGNMENT:
99      return "NONSTRICT_ALIGNMENT";
100    case STRICT_ALIGNMENT:
101      return "STRICT_ALIGNMENT";
102    case FORCED_ALIGNMENT:
103      return "FORCED_ALIGNMENT";
104    }
105  return "UNKNOWN";
106}
107
108
109#if defined (WITH_FLOATING_POINT)
110static const char *
111config_floating_point_to_a (int floating_point)
112{
113  switch (floating_point)
114    {
115    case SOFT_FLOATING_POINT:
116      return "SOFT_FLOATING_POINT";
117    case HARD_FLOATING_POINT:
118      return "HARD_FLOATING_POINT";
119    case 0:
120      return "0";
121    }
122  return "UNKNOWN";
123}
124#endif
125
126/* Set the default environment, prior to parsing argv.  */
127
128void
129sim_config_default (SIM_DESC sd)
130{
131   /* Set the current environment to ALL_ENVIRONMENT to indicate none has been
132      selected yet.  This is so that after parsing argv, we know whether the
133      environment was explicitly specified or not.  */
134  STATE_ENVIRONMENT (sd) = ALL_ENVIRONMENT;
135}
136
137/* Complete and verify the simulation environment.  */
138
139SIM_RC
140sim_config (SIM_DESC sd)
141{
142  enum bfd_endian prefered_target_byte_order;
143  SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
144
145  /* extract all relevant information */
146  if (STATE_PROG_BFD (sd) == NULL
147      /* If we have a binary input file (presumably with specified
148	 "--architecture"), it'll have no endianness.  */
149      || (!bfd_little_endian (STATE_PROG_BFD (sd))
150	  && !bfd_big_endian (STATE_PROG_BFD (sd))))
151    prefered_target_byte_order = BFD_ENDIAN_UNKNOWN;
152  else
153    prefered_target_byte_order = (bfd_little_endian (STATE_PROG_BFD (sd))
154				  ? BFD_ENDIAN_LITTLE
155				  : BFD_ENDIAN_BIG);
156
157  /* set the target byte order */
158#if (WITH_TREE_PROPERTIES)
159  if (current_target_byte_order == BFD_ENDIAN_UNKNOWN)
160    current_target_byte_order
161      = (tree_find_boolean_property (root, "/options/little-endian?")
162	 ? BFD_ENDIAN_LITTLE
163	 : BFD_ENDIAN_BIG);
164#endif
165  if (current_target_byte_order == BFD_ENDIAN_UNKNOWN
166      && prefered_target_byte_order != BFD_ENDIAN_UNKNOWN)
167    current_target_byte_order = prefered_target_byte_order;
168  if (current_target_byte_order == BFD_ENDIAN_UNKNOWN)
169    current_target_byte_order = WITH_TARGET_BYTE_ORDER;
170  if (current_target_byte_order == BFD_ENDIAN_UNKNOWN)
171    current_target_byte_order = WITH_DEFAULT_TARGET_BYTE_ORDER;
172
173  /* verify the target byte order */
174  if (CURRENT_TARGET_BYTE_ORDER == BFD_ENDIAN_UNKNOWN)
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));
183  if (prefered_target_byte_order != BFD_ENDIAN_UNKNOWN
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    }
209
210
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    }
219
220
221  /* set the environment */
222#if (WITH_TREE_PROPERTIES)
223  if (STATE_ENVIRONMENT (sd) == ALL_ENVIRONMENT)
224    {
225      const char *env =
226	tree_find_string_property (root, "/openprom/options/env");
227      STATE_ENVIRONMENT (sd) = ((strcmp (env, "user") == 0
228				 || strcmp (env, "uea") == 0)
229				? USER_ENVIRONMENT
230				: (strcmp (env, "virtual") == 0
231				   || strcmp (env, "vea") == 0)
232				? VIRTUAL_ENVIRONMENT
233				: (strcmp (env, "operating") == 0
234				   || strcmp (env, "oea") == 0)
235				? OPERATING_ENVIRONMENT
236				: ALL_ENVIRONMENT);
237    }
238#endif
239  if (STATE_ENVIRONMENT (sd) == ALL_ENVIRONMENT)
240    STATE_ENVIRONMENT (sd) = (WITH_ENVIRONMENT != ALL_ENVIRONMENT ?
241			      WITH_ENVIRONMENT : USER_ENVIRONMENT);
242
243
244  /* set the alignment */
245#if (WITH_TREE_PROPERTIES)
246  if (current_alignment == 0)
247    current_alignment =
248      (tree_find_boolean_property (root, "/openprom/options/strict-alignment?")
249       ? STRICT_ALIGNMENT
250       : NONSTRICT_ALIGNMENT);
251#endif
252  if (current_alignment == 0)
253    current_alignment = WITH_ALIGNMENT;
254  if (current_alignment == 0)
255    current_alignment = WITH_DEFAULT_ALIGNMENT;
256
257  /* verify the alignment */
258  if (CURRENT_ALIGNMENT == 0)
259    {
260      sim_io_eprintf (sd, "Target alignment unspecified\n");
261      return SIM_RC_FAIL;
262    }
263  if (CURRENT_ALIGNMENT != current_alignment)
264    {
265      sim_io_eprintf (sd, "Target (%s) and configured (%s) alignment in conflict\n",
266		      config_alignment_to_a (CURRENT_ALIGNMENT),
267		      config_alignment_to_a (current_alignment));
268      return SIM_RC_FAIL;
269    }
270
271#if defined (WITH_FLOATING_POINT)
272
273  /* set the floating point */
274  if (current_floating_point == 0)
275    current_floating_point = WITH_FLOATING_POINT;
276
277  /* verify the floating point */
278  if (CURRENT_FLOATING_POINT == 0)
279    {
280      sim_io_eprintf (sd, "Target floating-point unspecified\n");
281      return SIM_RC_FAIL;
282    }
283  if (CURRENT_FLOATING_POINT != current_floating_point)
284    {
285      sim_io_eprintf (sd, "Target (%s) and configured (%s) floating-point in conflict\n",
286		      config_alignment_to_a (CURRENT_FLOATING_POINT),
287		      config_alignment_to_a (current_floating_point));
288      return SIM_RC_FAIL;
289    }
290
291#endif
292  return SIM_RC_OK;
293}
294
295
296void
297print_sim_config (SIM_DESC sd)
298{
299  sim_io_printf (sd, "WITH_TARGET_BYTE_ORDER = %s\n",
300		 config_byte_order_to_a (WITH_TARGET_BYTE_ORDER));
301
302  sim_io_printf (sd, "WITH_DEFAULT_TARGET_BYTE_ORDER = %s\n",
303		 config_byte_order_to_a (WITH_DEFAULT_TARGET_BYTE_ORDER));
304
305  sim_io_printf (sd, "HOST_BYTE_ORDER = %s\n",
306		 config_byte_order_to_a (HOST_BYTE_ORDER));
307
308  sim_io_printf (sd, "WITH_STDIO = %s\n",
309		 config_stdio_to_a (WITH_STDIO));
310
311  sim_io_printf (sd, "WITH_TARGET_WORD_MSB = %d\n",
312		 WITH_TARGET_WORD_MSB);
313
314  sim_io_printf (sd, "WITH_TARGET_WORD_BITSIZE = %d\n",
315		 WITH_TARGET_WORD_BITSIZE);
316
317  sim_io_printf (sd, "WITH_TARGET_ADDRESS_BITSIZE = %d\n",
318		 WITH_TARGET_ADDRESS_BITSIZE);
319
320  sim_io_printf (sd, "WITH_TARGET_CELL_BITSIZE = %d\n",
321		 WITH_TARGET_CELL_BITSIZE);
322
323  sim_io_printf (sd, "WITH_TARGET_FLOATING_POINT_BITSIZE = %d\n",
324		 WITH_TARGET_FLOATING_POINT_BITSIZE);
325
326  sim_io_printf (sd, "WITH_ENVIRONMENT = %s\n",
327		 config_environment_to_a (WITH_ENVIRONMENT));
328
329  sim_io_printf (sd, "WITH_ALIGNMENT = %s\n",
330		 config_alignment_to_a (WITH_ALIGNMENT));
331
332#if defined (WITH_DEFAULT_ALIGNMENT)
333  sim_io_printf (sd, "WITH_DEFAULT_ALIGNMENT = %s\n",
334		 config_alignment_to_a (WITH_DEFAULT_ALIGNMENT));
335#endif
336
337#if defined (WITH_XOR_ENDIAN)
338  sim_io_printf (sd, "WITH_XOR_ENDIAN = %d\n", WITH_XOR_ENDIAN);
339#endif
340
341#if defined (WITH_FLOATING_POINT)
342  sim_io_printf (sd, "WITH_FLOATING_POINT = %s\n",
343		 config_floating_point_to_a (WITH_FLOATING_POINT));
344#endif
345
346#if defined (WITH_SMP)
347  sim_io_printf (sd, "WITH_SMP = %d\n", WITH_SMP);
348#endif
349
350#if defined (WITH_RESERVED_BITS)
351  sim_io_printf (sd, "WITH_RESERVED_BITS = %d\n", WITH_RESERVED_BITS);
352#endif
353
354#if defined (WITH_PROFILE)
355  sim_io_printf (sd, "WITH_PROFILE = %d\n", WITH_PROFILE);
356#endif
357
358}
359