Commit | Line | Data |
---|---|---|
4f8d4a38 DD |
1 | /* main.c --- main function for stand-alone RX simulator. |
2 | ||
dc3cf14f | 3 | Copyright (C) 2005, 2007, 2008, 2009, 2010 Free Software Foundation, Inc. |
4f8d4a38 DD |
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 | ||
2388a152 | 22 | #include "config.h" |
4f8d4a38 DD |
23 | #include <stdio.h> |
24 | #include <string.h> | |
2388a152 | 25 | #ifdef HAVE_STDLIB_H |
4f8d4a38 | 26 | #include <stdlib.h> |
2388a152 MM |
27 | #endif |
28 | #ifdef HAVE_UNISTD_H | |
4f8d4a38 | 29 | #include <unistd.h> |
2388a152 | 30 | #endif |
4f8d4a38 DD |
31 | #include <assert.h> |
32 | #include <setjmp.h> | |
33 | #include <signal.h> | |
2388a152 MM |
34 | #ifdef HAVE_GETOPT_H |
35 | #include <getopt.h> | |
36 | #endif | |
4f8d4a38 DD |
37 | |
38 | #include "bfd.h" | |
39 | ||
40 | #include "cpu.h" | |
41 | #include "mem.h" | |
42 | #include "misc.h" | |
43 | #include "load.h" | |
44 | #include "trace.h" | |
45 | #include "err.h" | |
46 | ||
47 | static int disassemble = 0; | |
48 | ||
49 | /* This must be higher than any other option index. */ | |
50 | #define OPT_ACT 400 | |
51 | ||
52 | #define ACT(E,A) (OPT_ACT + SIM_ERR_##E * SIM_ERRACTION_NUM_ACTIONS + SIM_ERRACTION_##A) | |
53 | ||
54 | static struct option sim_options[] = | |
55 | { | |
56 | { "end-sim-args", 0, NULL, 'E' }, | |
57 | { "exit-null-deref", 0, NULL, ACT(NULL_POINTER_DEREFERENCE,EXIT) }, | |
58 | { "warn-null-deref", 0, NULL, ACT(NULL_POINTER_DEREFERENCE,WARN) }, | |
59 | { "ignore-null-deref", 0, NULL, ACT(NULL_POINTER_DEREFERENCE,IGNORE) }, | |
60 | { "exit-unwritten-pages", 0, NULL, ACT(READ_UNWRITTEN_PAGES,EXIT) }, | |
61 | { "warn-unwritten-pages", 0, NULL, ACT(READ_UNWRITTEN_PAGES,WARN) }, | |
62 | { "ignore-unwritten-pages", 0, NULL, ACT(READ_UNWRITTEN_PAGES,IGNORE) }, | |
63 | { "exit-unwritten-bytes", 0, NULL, ACT(READ_UNWRITTEN_BYTES,EXIT) }, | |
64 | { "warn-unwritten-bytes", 0, NULL, ACT(READ_UNWRITTEN_BYTES,WARN) }, | |
65 | { "ignore-unwritten-bytes", 0, NULL, ACT(READ_UNWRITTEN_BYTES,IGNORE) }, | |
66 | { "exit-corrupt-stack", 0, NULL, ACT(CORRUPT_STACK,EXIT) }, | |
67 | { "warn-corrupt-stack", 0, NULL, ACT(CORRUPT_STACK,WARN) }, | |
68 | { "ignore-corrupt-stack", 0, NULL, ACT(CORRUPT_STACK,IGNORE) }, | |
69 | { 0, 0, 0, 0 } | |
70 | }; | |
71 | ||
72 | static void | |
73 | done (int exit_code) | |
74 | { | |
75 | if (verbose) | |
76 | { | |
77 | stack_heap_stats (); | |
78 | mem_usage_stats (); | |
79 | /* Only use comma separated numbers when being very verbose. | |
80 | Comma separated numbers are hard to parse in awk scripts. */ | |
81 | if (verbose > 1) | |
82 | printf ("insns: %14s\n", comma (rx_cycles)); | |
83 | else | |
84 | printf ("insns: %u\n", rx_cycles); | |
93378652 DD |
85 | |
86 | pipeline_stats (); | |
4f8d4a38 DD |
87 | } |
88 | exit (exit_code); | |
89 | } | |
90 | ||
91 | int | |
92 | main (int argc, char **argv) | |
93 | { | |
94 | int o; | |
95 | int save_trace; | |
96 | bfd *prog; | |
97 | ||
98 | /* By default, we exit when an execution error occurs. */ | |
99 | execution_error_init_standalone (); | |
100 | ||
101 | while ((o = getopt_long (argc, argv, "tvdeEwi", sim_options, NULL)) != -1) | |
102 | { | |
103 | if (o == 'E') | |
104 | /* Stop processing the command line. This is so that any remaining | |
105 | words on the command line that look like arguments will be passed | |
106 | on to the program being simulated. */ | |
107 | break; | |
108 | ||
109 | if (o >= OPT_ACT) | |
110 | { | |
111 | int e, a; | |
112 | ||
113 | o -= OPT_ACT; | |
114 | e = o / SIM_ERRACTION_NUM_ACTIONS; | |
115 | a = o % SIM_ERRACTION_NUM_ACTIONS; | |
116 | execution_error_set_action (e, a); | |
117 | } | |
118 | else switch (o) | |
119 | { | |
120 | case 't': | |
121 | trace++; | |
122 | break; | |
123 | case 'v': | |
124 | verbose++; | |
125 | break; | |
126 | case 'd': | |
127 | disassemble++; | |
128 | break; | |
129 | case 'e': | |
130 | execution_error_init_standalone (); | |
131 | break; | |
132 | case 'w': | |
133 | execution_error_warn_all (); | |
134 | break; | |
135 | case 'i': | |
136 | execution_error_ignore_all (); | |
137 | break; | |
138 | case '?': | |
139 | { | |
140 | int i; | |
141 | fprintf (stderr, | |
142 | "usage: run [options] program [arguments]\n"); | |
143 | fprintf (stderr, | |
144 | "\t-v\t- increase verbosity.\n" | |
145 | "\t-t\t- trace.\n" | |
146 | "\t-d\t- disassemble.\n" | |
147 | "\t-E\t- stop processing sim args\n" | |
148 | "\t-e\t- exit on all execution errors.\n" | |
149 | "\t-w\t- warn (do not exit) on all execution errors.\n" | |
150 | "\t-i\t- ignore all execution errors.\n"); | |
151 | for (i=0; sim_options[i].name; i++) | |
152 | fprintf (stderr, "\t--%s\n", sim_options[i].name); | |
153 | exit (1); | |
154 | } | |
155 | } | |
156 | } | |
157 | ||
158 | prog = bfd_openr (argv[optind], 0); | |
159 | if (!prog) | |
160 | { | |
161 | fprintf (stderr, "Can't read %s\n", argv[optind]); | |
162 | exit (1); | |
163 | } | |
164 | ||
165 | if (!bfd_check_format (prog, bfd_object)) | |
166 | { | |
167 | fprintf (stderr, "%s not a rx program\n", argv[optind]); | |
168 | exit (1); | |
169 | } | |
170 | ||
171 | init_regs (); | |
172 | ||
173 | rx_in_gdb = 0; | |
174 | save_trace = trace; | |
175 | trace = 0; | |
176 | rx_load (prog); | |
177 | trace = save_trace; | |
178 | ||
179 | sim_disasm_init (prog); | |
180 | ||
181 | while (1) | |
182 | { | |
183 | int rc; | |
184 | ||
185 | if (trace) | |
186 | printf ("\n"); | |
187 | ||
188 | if (disassemble) | |
189 | sim_disasm_one (); | |
190 | ||
191 | enable_counting = verbose; | |
192 | rc = decode_opcode (); | |
193 | enable_counting = 0; | |
194 | ||
195 | if (RX_HIT_BREAK (rc)) | |
196 | done (1); | |
197 | else if (RX_EXITED (rc)) | |
198 | done (RX_EXIT_STATUS (rc)); | |
199 | else if (RX_STOPPED (rc)) | |
200 | { | |
201 | if (verbose) | |
202 | printf("Stopped on signal %d\n", RX_STOP_SIG (rc)); | |
203 | exit(1); | |
204 | } | |
205 | else | |
206 | assert (RX_STEPPED (rc)); | |
207 | ||
208 | trace_register_changes (); | |
209 | } | |
210 | } |