]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - sim/ppc/main.c
* config/sh/tm-sh.h (BELIEVE_PCC_PROMOTION): Define, so that
[thirdparty/binutils-gdb.git] / sim / ppc / main.c
1 /* This file is part of the program psim.
2
3 Copyright (C) 1994-1996, Andrew Cagney <cagney@highland.com.au>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18
19 */
20
21
22 #include <stdarg.h>
23 #include <stdio.h>
24 #include <fcntl.h>
25
26 #include <signal.h>
27
28 #include "psim.h"
29 #include "options.h"
30 #include "device.h" /* FIXME: psim should provide the interface */
31 #include "events.h" /* FIXME: psim should provide the interface */
32
33 #ifdef HAVE_STDLIB_H
34 #include <stdlib.h>
35 #endif
36
37 #ifdef HAVE_UNISTD_H
38 #include <unistd.h>
39 #endif
40
41 #ifdef HAVE_STRING_H
42 #include <string.h>
43 #else
44 #ifdef HAVE_STRINGS_H
45 #include <strings.h>
46 #endif
47 #endif
48
49 #include <errno.h>
50
51 #if !defined(O_NDELAY) || !defined(F_GETFL) || !defined(F_SETFL)
52 #undef WITH_STDIO
53 #define WITH_STDIO DO_USE_STDIO
54 #endif
55
56
57 extern char **environ;
58
59 static psim *simulation = NULL;
60
61
62 void
63 sim_io_printf_filtered(const char *msg, ...)
64 {
65 va_list ap;
66 va_start(ap, msg);
67 vprintf(msg, ap);
68 va_end(ap);
69 }
70
71 void
72 error (char *msg, ...)
73 {
74 va_list ap;
75 va_start(ap, msg);
76 vprintf(msg, ap);
77 printf("\n");
78 va_end(ap);
79
80 /* any final clean up */
81 if (ppc_trace[trace_print_info] && simulation != NULL)
82 psim_print_info (simulation, ppc_trace[trace_print_info]);
83
84 exit (1);
85 }
86
87 int
88 sim_io_write_stdout(const char *buf,
89 int sizeof_buf)
90 {
91 switch (CURRENT_STDIO) {
92 case DO_USE_STDIO:
93 {
94 int i;
95 for (i = 0; i < sizeof_buf; i++) {
96 putchar(buf[i]);
97 }
98 return i;
99 }
100 break;
101 case DONT_USE_STDIO:
102 return write(1, buf, sizeof_buf);
103 break;
104 default:
105 error("sim_io_write_stdout: invalid switch\n");
106 }
107 return 0;
108 }
109
110 int
111 sim_io_write_stderr(const char *buf,
112 int sizeof_buf)
113 {
114 switch (CURRENT_STDIO) {
115 case DO_USE_STDIO:
116 {
117 int i;
118 for (i = 0; i < sizeof_buf; i++) {
119 fputc(buf[i], stderr);
120 }
121 return i;
122 }
123 break;
124 case DONT_USE_STDIO:
125 return write(2, buf, sizeof_buf);
126 break;
127 default:
128 error("sim_io_write_stdout: invalid switch\n");
129 }
130 return 0;
131 }
132
133 int
134 sim_io_read_stdin(char *buf,
135 int sizeof_buf)
136 {
137 switch (CURRENT_STDIO) {
138 case DO_USE_STDIO:
139 if (sizeof_buf > 1) {
140 if (fgets(buf, sizeof_buf, stdin) != NULL)
141 return strlen(buf);
142 }
143 else if (sizeof_buf == 1) {
144 char b[2];
145 if (fgets(b, sizeof(b), stdin) != NULL) {
146 memcpy(buf, b, strlen(b));
147 return strlen(b);
148 }
149 }
150 else if (sizeof_buf == 0)
151 return 0;
152 return sim_io_eof;
153 break;
154 case DONT_USE_STDIO:
155 #if defined(O_NDELAY) && defined(F_GETFL) && defined(F_SETFL)
156 {
157 /* check for input */
158 int flags;
159 int status;
160 int nr_read;
161 int result;
162 /* get the old status */
163 flags = fcntl(0, F_GETFL, 0);
164 if (flags == -1) {
165 perror("sim_io_read_stdin");
166 return sim_io_eof;
167 }
168 /* temp, disable blocking IO */
169 status = fcntl(0, F_SETFL, flags | O_NDELAY);
170 if (status == -1) {
171 perror("sim_io_read_stdin");
172 return sim_io_eof;
173 }
174 /* try for input */
175 nr_read = read(0, buf, sizeof_buf);
176 if (nr_read > 0
177 || (nr_read == 0 && sizeof_buf == 0))
178 result = nr_read;
179 else if (nr_read == 0)
180 result = sim_io_eof;
181 else { /* nr_read < 0 */
182 if (errno == EAGAIN)
183 result = sim_io_not_ready;
184 else
185 result = sim_io_eof;
186 }
187 /* return to regular vewing */
188 status = fcntl(0, F_SETFL, flags);
189 if (status == -1) {
190 perror("sim_io_read_stdin");
191 return sim_io_eof;
192 }
193 return result;
194 }
195 break;
196 #endif
197 default:
198 error("sim_io_read_stdin: invalid switch\n");
199 break;
200 }
201 return 0;
202 }
203
204 void
205 sim_io_flush_stdoutput(void)
206 {
207 switch (CURRENT_STDIO) {
208 case DO_USE_STDIO:
209 fflush (stdout);
210 break;
211 case DONT_USE_STDIO:
212 break;
213 default:
214 error("sim_io_flush_stdoutput: invalid switch\n");
215 break;
216 }
217 }
218
219
220 void *
221 zalloc(long size)
222 {
223 void *memory = malloc(size);
224 if (memory == NULL)
225 error("zmalloc failed\n");
226 memset(memory, 0, size);
227 return memory;
228 }
229
230 void
231 zfree(void *chunk)
232 {
233 free(chunk);
234 }
235
236 /* When a CNTRL-C occures, queue an event to shut down the simulation */
237
238 static RETSIGTYPE
239 cntrl_c(int sig)
240 {
241 psim_stop (simulation);
242 }
243
244
245 int
246 main(int argc, char **argv)
247 {
248 const char *name_of_file;
249 char *arg_;
250 psim_status status;
251 device *root = psim_tree();
252
253 /* parse the arguments */
254 argv = psim_options(root, argv + 1);
255 if (argv[0] == NULL) {
256 if (ppc_trace[trace_opts]) {
257 print_options ();
258 return 0;
259 } else {
260 psim_usage(0);
261 }
262 }
263 name_of_file = argv[0];
264
265 if (ppc_trace[trace_opts])
266 print_options ();
267
268 /* create the simulator */
269 simulation = psim_create(name_of_file, root);
270
271 /* fudge the environment so that _=prog-name */
272 arg_ = (char*)zalloc(strlen(argv[0]) + strlen("_=") + 1);
273 strcpy(arg_, "_=");
274 strcat(arg_, argv[0]);
275 putenv(arg_);
276
277 /* initialize it */
278 psim_init(simulation);
279 psim_stack(simulation, argv, environ);
280
281 {
282 RETSIGTYPE (*prev) ();
283 prev = signal(SIGINT, cntrl_c);
284 psim_run(simulation);
285 signal(SIGINT, prev);
286 }
287
288 /* any final clean up */
289 if (ppc_trace[trace_print_info])
290 psim_print_info (simulation, ppc_trace[trace_print_info]);
291
292 /* why did we stop */
293 status = psim_get_status(simulation);
294 switch (status.reason) {
295 case was_continuing:
296 error("psim: continuing while stoped!\n");
297 return 0;
298 case was_trap:
299 error("psim: no trap insn\n");
300 return 0;
301 case was_exited:
302 return status.signal;
303 case was_signalled:
304 printf ("%s: Caught signal %d at address 0x%lx\n",
305 name_of_file, (int)status.signal,
306 (long)status.program_counter);
307 return status.signal;
308 default:
309 error("unknown halt condition\n");
310 return 0;
311 }
312 }