make inline model use static when inlining.
[external/binutils.git] / sim / ppc / main.c
1 /*  This file is part of the program psim.
2
3     Copyright (C) 1994-1995, 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
25 #include "psim.h"
26 #include "function_unit.h"
27 #include "options.h"
28
29 #ifdef HAVE_STDLIB_H
30 #include <stdlib.h>
31 #endif
32
33 #ifdef HAVE_UNISTD_H
34 #include <unistd.h>
35 #endif
36
37 #ifdef HAVE_STRING_H
38 #include <string.h>
39 #else
40 #ifdef HAVE_STRINGS_H
41 #include <strings.h>
42 #endif
43 #endif
44
45 extern char **environ;
46 extern char *optarg;
47 extern int optind;
48 extern int optopt;
49 extern int opterr;
50
51 void
52 printf_filtered(const char *msg, ...)
53 {
54   va_list ap;
55   va_start(ap, msg);
56   vprintf(msg, ap);
57   va_end(ap);
58 }
59
60 void
61 error (char *msg, ...)
62 {
63   va_list ap;
64   va_start(ap, msg);
65   vprintf(msg, ap);
66   va_end(ap);
67   exit (1);
68 }
69
70 void *
71 zalloc(long size)
72 {
73   void *memory = malloc(size);
74   if (memory == NULL)
75     error("zmalloc failed\n");
76   memset(memory, 0, size);
77   return memory;
78 }
79
80 void
81 zfree(void *chunk)
82 {
83   free(chunk);
84 }
85
86 static void
87 usage(void)
88 {
89   printf_filtered("Usage:\n\tpsim [ -t <trace-option> ] [-m model] [-i] [-I] <image> [ <image-args> ... ]\n");
90   trace_usage();
91   error("");
92 }
93
94 int
95 main(int argc, char **argv)
96 {
97   psim *system;
98   const char *name_of_file;
99   char *arg_;
100   psim_status status;
101   int letter;
102   int print_info = 0;
103
104   /* check for arguments -- note sim_calls.c also contains argument processing
105      code for the simulator linked within gdb.  */
106   while ((letter = getopt (argc, argv, "Iim:t:")) != EOF)
107     {
108       switch (letter) {
109       case 't':
110         trace_option(optarg);
111         break;
112       case 'm':
113         model_set(optarg);
114         break;
115       case 'i':
116         print_info = 1;
117         break;
118       case 'I':
119         print_info = 2;
120         break;
121       default:
122         usage();
123       }
124     }
125   if (optind >= argc)
126     usage();
127   name_of_file = argv[optind];
128
129   if (ppc_trace[trace_opts])
130     print_options ();
131
132   /* create the simulator */
133   system = psim_create(name_of_file);
134
135   /* fudge the environment so that _=prog-name */
136   arg_ = (char*)zalloc(strlen(argv[optind]) + strlen("_=") + 1);
137   strcpy(arg_, "_=");
138   strcat(arg_, argv[optind]);
139   putenv(arg_);
140
141   /* initialize it */
142   psim_init(system);
143   psim_stack(system, &argv[optind], environ);
144
145   psim_run(system);
146
147   /* any final clean up */
148   if (print_info)
149     psim_print_info (system, print_info);
150
151   /* why did we stop */
152   status = psim_get_status(system);
153   switch (status.reason) {
154   case was_continuing:
155     error("psim: continuing while stoped!\n");
156     return 0;
157   case was_trap:
158     error("psim: no trap insn\n");
159     return 0;
160   case was_exited:
161     return status.signal;
162   case was_signalled:
163     printf ("%s: Caught signal %d at address 0x%lx\n",
164             name_of_file, (int)status.signal,
165             (long)status.program_counter);
166     return status.signal;
167   default:
168     error("unknown halt condition\n");
169     return 0;
170   }
171 }