Update copyright notices to add year 2010.
[binutils.git] / sim / rx / main.c
1 /* main.c --- main function for stand-alone RX simulator.
2
3 Copyright (C) 2005, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
4 Contributed by Red Hat, Inc.
5
6 This file is part of the GNU simulators.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
20
21
22 #include <stdio.h>
23 #include <string.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <assert.h>
27 #include <setjmp.h>
28 #include <signal.h>
29
30 #include "bfd.h"
31
32 #include "cpu.h"
33 #include "mem.h"
34 #include "misc.h"
35 #include "load.h"
36 #include "trace.h"
37 #include "err.h"
38
39 static int disassemble = 0;
40
41 /* This must be higher than any other option index.  */
42 #define OPT_ACT 400
43
44 #define ACT(E,A) (OPT_ACT + SIM_ERR_##E * SIM_ERRACTION_NUM_ACTIONS + SIM_ERRACTION_##A)
45
46 static struct option sim_options[] =
47 {
48   { "end-sim-args", 0, NULL, 'E' },
49   { "exit-null-deref", 0, NULL, ACT(NULL_POINTER_DEREFERENCE,EXIT) },
50   { "warn-null-deref", 0, NULL, ACT(NULL_POINTER_DEREFERENCE,WARN) },
51   { "ignore-null-deref", 0, NULL, ACT(NULL_POINTER_DEREFERENCE,IGNORE) },
52   { "exit-unwritten-pages", 0, NULL, ACT(READ_UNWRITTEN_PAGES,EXIT) },
53   { "warn-unwritten-pages", 0, NULL, ACT(READ_UNWRITTEN_PAGES,WARN) },
54   { "ignore-unwritten-pages", 0, NULL, ACT(READ_UNWRITTEN_PAGES,IGNORE) },
55   { "exit-unwritten-bytes", 0, NULL, ACT(READ_UNWRITTEN_BYTES,EXIT) },
56   { "warn-unwritten-bytes", 0, NULL, ACT(READ_UNWRITTEN_BYTES,WARN) },
57   { "ignore-unwritten-bytes", 0, NULL, ACT(READ_UNWRITTEN_BYTES,IGNORE) },
58   { "exit-corrupt-stack", 0, NULL, ACT(CORRUPT_STACK,EXIT) },
59   { "warn-corrupt-stack", 0, NULL, ACT(CORRUPT_STACK,WARN) },
60   { "ignore-corrupt-stack", 0, NULL, ACT(CORRUPT_STACK,IGNORE) },
61   { 0, 0, 0, 0 }
62 };
63
64 static void
65 done (int exit_code)
66 {
67   if (verbose)
68     {
69       stack_heap_stats ();
70       mem_usage_stats ();
71       /* Only use comma separated numbers when being very verbose.
72          Comma separated numbers are hard to parse in awk scripts.  */
73       if (verbose > 1)
74         printf ("insns: %14s\n", comma (rx_cycles));
75       else
76         printf ("insns: %u\n", rx_cycles);
77     }
78   exit (exit_code);
79 }
80
81 int
82 main (int argc, char **argv)
83 {
84   int o;
85   int save_trace;
86   bfd *prog;
87
88   /* By default, we exit when an execution error occurs.  */
89   execution_error_init_standalone ();
90
91   while ((o = getopt_long (argc, argv, "tvdeEwi", sim_options, NULL)) != -1)
92     {
93       if (o == 'E')
94         /* Stop processing the command line. This is so that any remaining
95            words on the command line that look like arguments will be passed
96            on to the program being simulated.  */
97         break;
98
99       if (o >= OPT_ACT)
100         {
101           int e, a;
102
103           o -= OPT_ACT;
104           e = o / SIM_ERRACTION_NUM_ACTIONS;
105           a = o % SIM_ERRACTION_NUM_ACTIONS;
106           execution_error_set_action (e, a);
107         }
108       else switch (o)
109         {
110         case 't':
111           trace++;
112           break;
113         case 'v':
114           verbose++;
115           break;
116         case 'd':
117           disassemble++;
118           break;
119         case 'e':
120           execution_error_init_standalone ();
121           break;
122         case 'w':
123           execution_error_warn_all ();
124           break;
125         case 'i':
126           execution_error_ignore_all ();
127           break;
128         case '?':
129           {
130             int i;
131             fprintf (stderr,
132                      "usage: run [options] program [arguments]\n");
133             fprintf (stderr,
134                      "\t-v\t- increase verbosity.\n"
135                      "\t-t\t- trace.\n"
136                      "\t-d\t- disassemble.\n"
137                      "\t-E\t- stop processing sim args\n"
138                      "\t-e\t- exit on all execution errors.\n"
139                      "\t-w\t- warn (do not exit) on all execution errors.\n"
140                      "\t-i\t- ignore all execution errors.\n");
141             for (i=0; sim_options[i].name; i++)
142               fprintf (stderr, "\t--%s\n", sim_options[i].name);
143             exit (1);
144           }
145         }
146     }
147
148   prog = bfd_openr (argv[optind], 0);
149   if (!prog)
150     {
151       fprintf (stderr, "Can't read %s\n", argv[optind]);
152       exit (1);
153     }
154
155   if (!bfd_check_format (prog, bfd_object))
156     {
157       fprintf (stderr, "%s not a rx program\n", argv[optind]);
158       exit (1);
159     }
160
161   init_regs ();
162
163   rx_in_gdb = 0;
164   save_trace = trace;
165   trace = 0;
166   rx_load (prog);
167   trace = save_trace;
168
169   sim_disasm_init (prog);
170
171   while (1)
172     {
173       int rc;
174
175       if (trace)
176         printf ("\n");
177
178       if (disassemble)
179         sim_disasm_one ();
180
181       enable_counting = verbose;
182       rc = decode_opcode ();
183       enable_counting = 0;
184
185       if (RX_HIT_BREAK (rc))
186         done (1);
187       else if (RX_EXITED (rc))
188         done (RX_EXIT_STATUS (rc));
189       else if (RX_STOPPED (rc))
190         {
191           if (verbose)
192             printf("Stopped on signal %d\n", RX_STOP_SIG (rc));
193           exit(1);
194         }
195       else
196         assert (RX_STEPPED (rc));
197
198       trace_register_changes ();
199     }
200 }
This page took 0.035106 seconds and 4 git commands to generate.