1/* The common simulator framework for GDB, the GNU Debugger.
2
3   Copyright 2002, 2007, 2008, 2009, 2010, 2011 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
28int current_host_byte_order;
29int current_target_byte_order;
30int current_stdio;
31
32enum sim_alignments current_alignment;
33
34#if defined (WITH_FLOATING_POINT)
35int current_floating_point;
36#endif
37
38
39
40/* map a byte order onto a textual string */
41
42static const char *
43config_byte_order_to_a (int byte_order)
44{
45  switch (byte_order)
46    {
47    case LITTLE_ENDIAN:
48      return "LITTLE_ENDIAN";
49    case BIG_ENDIAN:
50      return "BIG_ENDIAN";
51    case 0:
52      return "0";
53    }
54  return "UNKNOWN";
55}
56
57
58static const char *
59config_stdio_to_a (int stdio)
60{
61  switch (stdio)
62    {
63    case DONT_USE_STDIO:
64      return "DONT_USE_STDIO";
65    case DO_USE_STDIO:
66      return "DO_USE_STDIO";
67    case 0:
68      return "0";
69    }
70  return "UNKNOWN";
71}
72
73
74static const char *
75config_environment_to_a (enum sim_environment environment)
76{
77  switch (environment)
78    {
79    case ALL_ENVIRONMENT:
80      return "ALL_ENVIRONMENT";
81    case USER_ENVIRONMENT:
82      return "USER_ENVIRONMENT";
83    case VIRTUAL_ENVIRONMENT:
84      return "VIRTUAL_ENVIRONMENT";
85    case OPERATING_ENVIRONMENT:
86      return "OPERATING_ENVIRONMENT";
87    }
88  return "UNKNOWN";
89}
90
91
92static const char *
93config_alignment_to_a (enum sim_alignments alignment)
94{
95  switch (alignment)
96    {
97    case MIXED_ALIGNMENT:
98      return "MIXED_ALIGNMENT";
99    case NONSTRICT_ALIGNMENT:
100      return "NONSTRICT_ALIGNMENT";
101    case STRICT_ALIGNMENT:
102      return "STRICT_ALIGNMENT";
103    case FORCED_ALIGNMENT:
104      return "FORCED_ALIGNMENT";
105    }
106  return "UNKNOWN";
107}
108
109
110#if defined (WITH_FLOATING_POINT)
111static const char *
112config_floating_point_to_a (int floating_point)
113{
114  switch (floating_point)
115    {
116    case SOFT_FLOATING_POINT:
117      return "SOFT_FLOATING_POINT";
118    case HARD_FLOATING_POINT:
119      return "HARD_FLOATING_POINT";
120    case 0:
121      return "0";
122    }
123  return "UNKNOWN";
124}
125#endif
126
127/* Set the default environment, prior to parsing argv.  */
128
129void
130sim_config_default (SIM_DESC sd)
131{
132   /* Set the current environment to ALL_ENVIRONMENT to indicate none has been
133      selected yet.  This is so that after parsing argv, we know whether the
134      environment was explicitly specified or not.  */
135  STATE_ENVIRONMENT (sd) = ALL_ENVIRONMENT;
136}
137
138/* Complete and verify the simulation environment.  */
139
140SIM_RC
141sim_config (SIM_DESC sd)
142{
143  int prefered_target_byte_order;
144  SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
145
146  /* extract all relevant information */
147  if (STATE_PROG_BFD (sd) == NULL
148      /* If we have a binary input file (presumably with specified
149	 "--architecture"), it'll have no endianness.  */
150      || (!bfd_little_endian (STATE_PROG_BFD (sd))
151	  && !bfd_big_endian (STATE_PROG_BFD (sd))))
152    prefered_target_byte_order = 0;
153  else
154    prefered_target_byte_order = (bfd_little_endian(STATE_PROG_BFD (sd))
155				  ? LITTLE_ENDIAN
156				  : BIG_ENDIAN);
157
158  /* set the host byte order */
159  current_host_byte_order = 1;
160  if (*(char*)(&current_host_byte_order))
161    current_host_byte_order = LITTLE_ENDIAN;
162  else
163    current_host_byte_order = BIG_ENDIAN;
164
165  /* verify the host byte order */
166  if (CURRENT_HOST_BYTE_ORDER != current_host_byte_order)
167    {
168      sim_io_eprintf (sd, "host (%s) and configured (%s) byte order in conflict",
169		      config_byte_order_to_a (current_host_byte_order),
170		      config_byte_order_to_a (CURRENT_HOST_BYTE_ORDER));
171      return SIM_RC_FAIL;
172    }
173
174
175  /* set the target byte order */
176#if (WITH_TREE_PROPERTIES)
177  if (current_target_byte_order == 0)
178    current_target_byte_order
179      = (tree_find_boolean_property (root, "/options/little-endian?")
180	 ? LITTLE_ENDIAN
181	 : BIG_ENDIAN);
182#endif
183  if (current_target_byte_order == 0
184      && prefered_target_byte_order != 0)
185    current_target_byte_order = prefered_target_byte_order;
186  if (current_target_byte_order == 0)
187    current_target_byte_order = WITH_TARGET_BYTE_ORDER;
188  if (current_target_byte_order == 0)
189    current_target_byte_order = WITH_DEFAULT_TARGET_BYTE_ORDER;
190
191  /* verify the target byte order */
192  if (CURRENT_TARGET_BYTE_ORDER == 0)
193    {
194      sim_io_eprintf (sd, "Target byte order unspecified\n");
195      return SIM_RC_FAIL;
196    }
197  if (CURRENT_TARGET_BYTE_ORDER != current_target_byte_order)
198    sim_io_eprintf (sd, "Target (%s) and configured (%s) byte order in conflict\n",
199		  config_byte_order_to_a (current_target_byte_order),
200		  config_byte_order_to_a (CURRENT_TARGET_BYTE_ORDER));
201  if (prefered_target_byte_order != 0
202      && CURRENT_TARGET_BYTE_ORDER != prefered_target_byte_order)
203    sim_io_eprintf (sd, "Target (%s) and specified (%s) byte order in conflict\n",
204		  config_byte_order_to_a (CURRENT_TARGET_BYTE_ORDER),
205		  config_byte_order_to_a (prefered_target_byte_order));
206
207
208  /* set the stdio */
209  if (current_stdio == 0)
210    current_stdio = WITH_STDIO;
211  if (current_stdio == 0)
212    current_stdio = DO_USE_STDIO;
213
214  /* verify the stdio */
215  if (CURRENT_STDIO == 0)
216    {
217      sim_io_eprintf (sd, "Target standard IO unspecified\n");
218      return SIM_RC_FAIL;
219    }
220  if (CURRENT_STDIO != current_stdio)
221    {
222      sim_io_eprintf (sd, "Target (%s) and configured (%s) standard IO in conflict\n",
223		      config_stdio_to_a (CURRENT_STDIO),
224		      config_stdio_to_a (current_stdio));
225      return SIM_RC_FAIL;
226    }
227
228
229  /* check the value of MSB */
230  if (WITH_TARGET_WORD_MSB != 0
231      && WITH_TARGET_WORD_MSB != (WITH_TARGET_WORD_BITSIZE - 1))
232    {
233      sim_io_eprintf (sd, "Target bitsize (%d) contradicts target most significant bit (%d)\n",
234		      WITH_TARGET_WORD_BITSIZE, WITH_TARGET_WORD_MSB);
235      return SIM_RC_FAIL;
236    }
237
238
239  /* set the environment */
240#if (WITH_TREE_PROPERTIES)
241  if (STATE_ENVIRONMENT (sd) == ALL_ENVIRONMENT)
242    {
243      const char *env =
244	tree_find_string_property(root, "/openprom/options/env");
245      STATE_ENVIRONMENT (sd) = ((strcmp(env, "user") == 0
246				 || strcmp(env, "uea") == 0)
247				? USER_ENVIRONMENT
248				: (strcmp(env, "virtual") == 0
249				   || strcmp(env, "vea") == 0)
250				? VIRTUAL_ENVIRONMENT
251				: (strcmp(env, "operating") == 0
252				   || strcmp(env, "oea") == 0)
253				? OPERATING_ENVIRONMENT
254				: ALL_ENVIRONMENT);
255    }
256#endif
257  if (STATE_ENVIRONMENT (sd) == ALL_ENVIRONMENT)
258    STATE_ENVIRONMENT (sd) = DEFAULT_ENVIRONMENT;
259
260
261  /* set the alignment */
262#if (WITH_TREE_PROPERTIES)
263  if (current_alignment == 0)
264    current_alignment =
265      (tree_find_boolean_property(root, "/openprom/options/strict-alignment?")
266       ? STRICT_ALIGNMENT
267       : NONSTRICT_ALIGNMENT);
268#endif
269  if (current_alignment == 0)
270    current_alignment = WITH_ALIGNMENT;
271  if (current_alignment == 0)
272    current_alignment = WITH_DEFAULT_ALIGNMENT;
273
274  /* verify the alignment */
275  if (CURRENT_ALIGNMENT == 0)
276    {
277      sim_io_eprintf (sd, "Target alignment unspecified\n");
278      return SIM_RC_FAIL;
279    }
280  if (CURRENT_ALIGNMENT != current_alignment)
281    {
282      sim_io_eprintf (sd, "Target (%s) and configured (%s) alignment in conflict\n",
283		      config_alignment_to_a (CURRENT_ALIGNMENT),
284		      config_alignment_to_a (current_alignment));
285      return SIM_RC_FAIL;
286    }
287
288#if defined (WITH_FLOATING_POINT)
289
290  /* set the floating point */
291  if (current_floating_point == 0)
292    current_floating_point = WITH_FLOATING_POINT;
293
294  /* verify the floating point */
295  if (CURRENT_FLOATING_POINT == 0)
296    {
297      sim_io_eprintf (sd, "Target floating-point unspecified\n");
298      return SIM_RC_FAIL;
299    }
300  if (CURRENT_FLOATING_POINT != current_floating_point)
301    {
302      sim_io_eprintf (sd, "Target (%s) and configured (%s) floating-point in conflict\n",
303		      config_alignment_to_a (CURRENT_FLOATING_POINT),
304		      config_alignment_to_a (current_floating_point));
305      return SIM_RC_FAIL;
306    }
307
308#endif
309  return SIM_RC_OK;
310}
311
312
313void
314print_sim_config (SIM_DESC sd)
315{
316#if defined (__GNUC__) && defined (__VERSION__)
317  sim_io_printf (sd, "Compiled by GCC %s on %s %s\n",
318			  __VERSION__, __DATE__, __TIME__);
319#else
320  sim_io_printf (sd, "Compiled on %s %s\n", __DATE__, __TIME__);
321#endif
322
323  sim_io_printf (sd, "WITH_TARGET_BYTE_ORDER   = %s\n",
324		 config_byte_order_to_a (WITH_TARGET_BYTE_ORDER));
325
326  sim_io_printf (sd, "WITH_DEFAULT_TARGET_BYTE_ORDER   = %s\n",
327		 config_byte_order_to_a (WITH_DEFAULT_TARGET_BYTE_ORDER));
328
329  sim_io_printf (sd, "WITH_HOST_BYTE_ORDER     = %s\n",
330		 config_byte_order_to_a (WITH_HOST_BYTE_ORDER));
331
332  sim_io_printf (sd, "WITH_STDIO               = %s\n",
333		 config_stdio_to_a (WITH_STDIO));
334
335  sim_io_printf (sd, "WITH_TARGET_WORD_MSB     = %d\n",
336		 WITH_TARGET_WORD_MSB);
337
338  sim_io_printf (sd, "WITH_TARGET_WORD_BITSIZE = %d\n",
339		 WITH_TARGET_WORD_BITSIZE);
340
341  sim_io_printf (sd, "WITH_TARGET_ADDRESS_BITSIZE = %d\n",
342		 WITH_TARGET_ADDRESS_BITSIZE);
343
344  sim_io_printf (sd, "WITH_TARGET_CELL_BITSIZE = %d\n",
345		 WITH_TARGET_CELL_BITSIZE);
346
347  sim_io_printf (sd, "WITH_TARGET_FLOATING_POINT_BITSIZE = %d\n",
348		 WITH_TARGET_FLOATING_POINT_BITSIZE);
349
350  sim_io_printf (sd, "WITH_ENVIRONMENT = %s\n",
351		 config_environment_to_a (WITH_ENVIRONMENT));
352
353  sim_io_printf (sd, "WITH_ALIGNMENT = %s\n",
354		 config_alignment_to_a (WITH_ALIGNMENT));
355
356#if defined (WITH_DEFAULT_ALIGNMENT)
357  sim_io_printf (sd, "WITH_DEFAULT_ALIGNMENT = %s\n",
358		 config_alignment_to_a (WITH_DEFAULT_ALIGNMENT));
359#endif
360
361#if defined (WITH_XOR_ENDIAN)
362  sim_io_printf (sd, "WITH_XOR_ENDIAN = %d\n", WITH_XOR_ENDIAN);
363#endif
364
365#if defined (WITH_FLOATING_POINT)
366  sim_io_printf (sd, "WITH_FLOATING_POINT = %s\n",
367		 config_floating_point_to_a (WITH_FLOATING_POINT));
368#endif
369
370#if defined (WITH_SMP)
371  sim_io_printf (sd, "WITH_SMP = %d\n", WITH_SMP);
372#endif
373
374#if defined (WITH_RESERVED_BITS)
375  sim_io_printf (sd, "WITH_RESERVED_BITS = %d\n", WITH_RESERVED_BITS);
376#endif
377
378#if defined (WITH_PROFILE)
379  sim_io_printf (sd, "WITH_PROFILE = %d\n", WITH_PROFILE);
380#endif
381
382}
383