"I am a person who works hard and plays hard."

Yuan Wei
Second Year Graduate Student Department of Computer Science
University of Virginia Charlottesville, VA 22903
Email: yw3f@cs.virginia.edu


Source Code Analysis

Main Page   Compound List   File List   Compound Members   File Members  

main.c

Go to the documentation of this file.
00001 /*
00002  * main.c - main line routines
00003  *
00004  * This file is a part of the SimpleScalar tool suite written by
00005  * Todd M. Austin as a part of the Multiscalar Research Project.
00006  *
00007  * The tool suite is currently maintained by Doug Burger and Todd M. Austin.
00008  *
00009  * Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000 by Todd M. Austin
00010  *
00011  * This source file is distributed "as is" in the hope that it will be
00012  * useful.  The tool set comes with no warranty, and no author or
00013  * distributor accepts any responsibility for the consequences of its
00014  * use. 
00015  *
00016  * Everyone is granted permission to copy, modify and redistribute
00017  * this tool set under the following conditions:
00018  *
00019  *    This source code is distributed for non-commercial use only. 
00020  *    Please contact the maintainer for restrictions applying to 
00021  *    commercial use.
00022  *
00023  *    Permission is granted to anyone to make or distribute copies
00024  *    of this source code, either as received or modified, in any
00025  *    medium, provided that all copyright notices, permission and
00026  *    nonwarranty notices are preserved, and that the distributor
00027  *    grants the recipient permission for further redistribution as
00028  *    permitted by this document.
00029  *
00030  *    Permission is granted to distribute this file in compiled
00031  *    or executable form under the same conditions that apply for
00032  *    source code, provided that either:
00033  *
00034  *    A. it is accompanied by the corresponding machine-readable
00035  *       source code,
00036  *    B. it is accompanied by a written offer, with no time limit,
00037  *       to give anyone a machine-readable copy of the corresponding
00038  *       source code in return for reimbursement of the cost of
00039  *       distribution.  This written offer must permit verbatim
00040  *       duplication by anyone, or
00041  *    C. it is distributed by someone who received only the
00042  *       executable form, and is accompanied by a copy of the
00043  *       written offer of source code that they received concurrently.
00044  *
00045  * In other words, you are welcome to use, share and improve this
00046  * source file.  You are forbidden to forbid anyone else to use, share
00047  * and improve what you give them.
00048  *
00049  * INTERNET: dburger@cs.wisc.edu
00050  * US Mail:  1210 W. Dayton Street, Madison, WI 53706
00051  *
00052  * $Id: main.c,v 1.1.1.1 2000/05/26 15:19:03 taustin Exp $
00053  *
00054  * $Log: main.c,v $
00055  * Revision 1.1.1.1  2000/05/26 15:19:03  taustin
00056  * SimpleScalar Tool Set
00057  *
00058  *
00059  * Revision 1.9  1999/12/31 18:37:40  taustin
00060  * renice support no longer interferes with extern renice mechanisms
00061  *
00062  * Revision 1.8  1998/08/31 17:09:49  taustin
00063  * dumbed down parts for MS VC++
00064  * added unistd.h include to remove warnings
00065  *
00066  * Revision 1.7  1998/08/27 08:39:28  taustin
00067  * added support for MS VC++ compilation
00068  * implemented host interface description in host.h
00069  * added target interface support
00070  * added simulator and program output redirection (via "-redir:sim"
00071  *       and "redir:prog" options, respectively)
00072  * added "-nice" option (available on all simulator) that resets
00073  *       simulator scheduling priority to specified level
00074  * added checkpoint support
00075  * implemented a more portable random() interface
00076  * all simulators now emit command line used to invoke them
00077  *
00078  * Revision 1.6  1997/04/16  22:09:20  taustin
00079  * added standalone loader support
00080  *
00081  * Revision 1.5  1997/03/11  01:13:36  taustin
00082  * updated copyright
00083  * random number generator seed option added
00084  * versioning format simplified (X.Y)
00085  * fast terminate (-q) option added
00086  * RUNNING flag now helps stats routine determine when to spew stats
00087  *
00088  * Revision 1.3  1996/12/27  15:52:20  taustin
00089  * updated comments
00090  * integrated support for options and stats packages
00091  *
00092  * Revision 1.1  1996/12/05  18:52:32  taustin
00093  * Initial revision
00094  *
00095  *
00096  */
00097 
00098 #include <stdio.h>
00099 #include <stdlib.h>
00100 #include <string.h>
00101 #include <time.h>
00102 #include <setjmp.h>
00103 #include <signal.h>
00104 #include <sys/types.h>
00105 #ifndef _MSC_VER
00106 #include <unistd.h>
00107 #include <sys/time.h>
00108 #endif
00109 #ifdef BFD_LOADER
00110 #include <bfd.h>
00111 #endif /* BFD_LOADER */
00112 
00113 #include "host.h"
00114 #include "misc.h"
00115 #include "machine.h"
00116 #include "endian.h"
00117 #include "version.h"
00118 #include "dlite.h"
00119 #include "options.h"
00120 #include "stats.h"
00121 #include "loader.h"
00122 #include "sim.h"
00123 
00124 /* stats signal handler */
00125 static void
00126 signal_sim_stats(int sigtype)
00127 {
00128   sim_dump_stats = TRUE;
00129 }
00130 
00131 /* exit signal handler */
00132 static void
00133 signal_exit_now(int sigtype)
00134 {
00135   sim_exit_now = TRUE;
00136 }
00137 
00138 /* execution instruction counter */
00139 counter_t sim_num_insn = 0;
00140 
00141 #if 0 /* not portable... :-( */
00142 /* total simulator (data) memory usage */
00143 unsigned int sim_mem_usage = 0;
00144 #endif
00145 
00146 /* execution start/end times */
00147 time_t sim_start_time;
00148 time_t sim_end_time;
00149 int sim_elapsed_time;
00150 
00151 /* byte/word swapping required to execute target executable on this host */
00152 int sim_swap_bytes;
00153 int sim_swap_words;
00154 
00155 /* exit when this becomes non-zero */
00156 int sim_exit_now = FALSE;
00157 
00158 /* longjmp here when simulation is completed */
00159 jmp_buf sim_exit_buf;
00160 
00161 /* set to non-zero when simulator should dump statistics */
00162 int sim_dump_stats = FALSE;
00163 
00164 /* options database */
00165 struct opt_odb_t *sim_odb;
00166 
00167 /* stats database */
00168 struct stat_sdb_t *sim_sdb;
00169 
00170 /* EIO interfaces */
00171 char *sim_eio_fname = NULL;
00172 char *sim_chkpt_fname = NULL;
00173 FILE *sim_eio_fd = NULL;
00174 
00175 /* redirected program/simulator output file names */
00176 static char *sim_simout = NULL;
00177 static char *sim_progout = NULL;
00178 FILE *sim_progfd = NULL;
00179 
00180 /* track first argument orphan, this is the program to execute */
00181 static int exec_index = -1;
00182 
00183 /* dump help information */
00184 static int help_me;
00185 
00186 /* random number generator seed */
00187 static int rand_seed;
00188 
00189 /* initialize and quit immediately */
00190 static int init_quit;
00191 
00192 #ifndef _MSC_VER
00193 /* simulator scheduling priority */
00194 static int nice_priority;
00195 #endif
00196 
00197 /* default simulator scheduling priority */
00198 #define NICE_DEFAULT_VALUE              0
00199 
00200 static int
00201 orphan_fn(int i, int argc, char **argv)
00202 {
00203   exec_index = i;
00204   return /* done */FALSE;
00205 }
00206 
00207 static void
00208 banner(FILE *fd, int argc, char **argv)
00209 {
00210   char *s;
00211 
00212   fprintf(fd,
00213           "%s: SimpleScalar/%s Tool Set version %d.%d of %s.\n"
00214           "Copyright (c) 1994-2000 by Todd M. Austin.  All Rights Reserved.\n"
00215           "This version of SimpleScalar is licensed for academic non-commercial use only.\n"
00216           "\n",
00217           ((s = strrchr(argv[0], '/')) ? s+1 : argv[0]),
00218           VER_TARGET, VER_MAJOR, VER_MINOR, VER_UPDATE);
00219 }
00220 
00221 static void
00222 usage(FILE *fd, int argc, char **argv)
00223 {
00224   fprintf(fd, "Usage: %s {-options} executable {arguments}\n", argv[0]);
00225   opt_print_help(sim_odb, fd);
00226 }
00227 
00228 static int running = FALSE;
00229 
00230 /* print all simulator stats */
00231 void
00232 sim_print_stats(FILE *fd)               /* output stream */
00233 {
00234 #if 0 /* not portable... :-( */
00235   extern char etext, *sbrk(int);
00236 #endif
00237 
00238   if (!running)
00239     return;
00240 
00241   /* get stats time */
00242   sim_end_time = time((time_t *)NULL);
00243   sim_elapsed_time = MAX(sim_end_time - sim_start_time, 1);
00244 
00245 #if 0 /* not portable... :-( */
00246   /* compute simulator memory usage */
00247   sim_mem_usage = (sbrk(0) - &etext) / 1024;
00248 #endif
00249 
00250   /* print simulation stats */
00251   fprintf(fd, "\nsim: ** simulation statistics **\n");
00252   stat_print_stats(sim_sdb, fd);
00253   sim_aux_stats(fd);
00254   fprintf(fd, "\n");
00255 }
00256 
00257 /* print stats, uninitialize simulator components, and exit w/ exitcode */
00258 static void
00259 exit_now(int exit_code)
00260 {
00261   /* print simulation stats */
00262   sim_print_stats(stderr);
00263 
00264   /* un-initialize the simulator */
00265   sim_uninit();
00266 
00267   /* all done! */
00268   exit(exit_code);
00269 }
00270 
00271 int
00272 main(int argc, char **argv, char **envp)
00273 {
00274   char *s;
00275   int i, exit_code;
00276 
00277 #ifndef _MSC_VER
00278   /* catch SIGUSR1 and dump intermediate stats */
00279   signal(SIGUSR1, signal_sim_stats);
00280 
00281   /* catch SIGUSR2 and dump final stats and exit */
00282   signal(SIGUSR2, signal_exit_now);
00283 #endif /* _MSC_VER */
00284 
00285   /* register an error handler */
00286   fatal_hook(sim_print_stats);
00287 
00288   /* set up a non-local exit point */
00289   if ((exit_code = setjmp(sim_exit_buf)) != 0)
00290     {
00291       /* special handling as longjmp cannot pass 0 */
00292       exit_now(exit_code-1);
00293     }
00294 
00295   /* register global options */
00296   sim_odb = opt_new(orphan_fn);
00297   opt_reg_flag(sim_odb, "-h", "print help message",
00298                &help_me, /* default */FALSE, /* !print */FALSE, NULL);
00299   opt_reg_flag(sim_odb, "-v", "verbose operation",
00300                &verbose, /* default */FALSE, /* !print */FALSE, NULL);
00301 #ifdef DEBUG
00302   opt_reg_flag(sim_odb, "-d", "enable debug message",
00303                &debugging, /* default */FALSE, /* !print */FALSE, NULL);
00304 #endif /* DEBUG */
00305   opt_reg_flag(sim_odb, "-i", "start in Dlite debugger",
00306                &dlite_active, /* default */FALSE, /* !print */FALSE, NULL);
00307   opt_reg_int(sim_odb, "-seed",
00308               "random number generator seed (0 for timer seed)",
00309               &rand_seed, /* default */1, /* print */TRUE, NULL);
00310   opt_reg_flag(sim_odb, "-q", "initialize and terminate immediately",
00311                &init_quit, /* default */FALSE, /* !print */FALSE, NULL);
00312   opt_reg_string(sim_odb, "-chkpt", "restore EIO trace execution from <fname>",
00313                  &sim_chkpt_fname, /* default */NULL, /* !print */FALSE, NULL);
00314 
00315   /* stdio redirection options */
00316   opt_reg_string(sim_odb, "-redir:sim",
00317                  "redirect simulator output to file (non-interactive only)",
00318                  &sim_simout,
00319                  /* default */NULL, /* !print */FALSE, NULL);
00320   opt_reg_string(sim_odb, "-redir:prog",
00321                  "redirect simulated program output to file",
00322                  &sim_progout, /* default */NULL, /* !print */FALSE, NULL);
00323 
00324 #ifndef _MSC_VER
00325   /* scheduling priority option */
00326   opt_reg_int(sim_odb, "-nice",
00327               "simulator scheduling priority", &nice_priority,
00328               /* default */NICE_DEFAULT_VALUE, /* print */TRUE, NULL);
00329 #endif
00330 
00331   /* FIXME: add stats intervals and max insts... */
00332 
00333   /* register all simulator-specific options */
00334   sim_reg_options(sim_odb);
00335 
00336   /* parse simulator options */
00337   exec_index = -1;
00338   opt_process_options(sim_odb, argc, argv);
00339 
00340   /* redirect I/O? */
00341   if (sim_simout != NULL)
00342     {
00343       /* send simulator non-interactive output (STDERR) to file SIM_SIMOUT */
00344       fflush(stderr);
00345       if (!freopen(sim_simout, "w", stderr))
00346         fatal("unable to redirect simulator output to file `%s'", sim_simout);
00347     }
00348 
00349   if (sim_progout != NULL)
00350     {
00351       /* redirect simulated program output to file SIM_PROGOUT */
00352       sim_progfd = fopen(sim_progout, "w");
00353       if (!sim_progfd)
00354         fatal("unable to redirect program output to file `%s'", sim_progout);
00355     }
00356 
00357   /* need at least two argv values to run */
00358   if (argc < 2)
00359     {
00360       banner(stderr, argc, argv);
00361       usage(stderr, argc, argv);
00362       exit(1);
00363     }
00364 
00365   /* opening banner */
00366   banner(stderr, argc, argv);
00367 
00368   if (help_me)
00369     {
00370       /* print help message and exit */
00371       usage(stderr, argc, argv);
00372       exit(1);
00373     }
00374 
00375   /* seed the random number generator */
00376   if (rand_seed == 0)
00377     {
00378       /* seed with the timer value, true random */
00379       mysrand(time((time_t *)NULL));
00380     }
00381   else
00382     {
00383       /* seed with default or user-specified random number generator seed */
00384       mysrand(rand_seed);
00385     }
00386 
00387   /* exec_index is set in orphan_fn() */
00388   if (exec_index == -1)
00389     {
00390       /* executable was not found */
00391       fprintf(stderr, "error: no executable specified\n");
00392       usage(stderr, argc, argv);
00393       exit(1);
00394     }
00395   /* else, exec_index points to simulated program arguments */
00396 
00397   /* check simulator-specific options */
00398   sim_check_options(sim_odb, argc, argv);
00399 
00400 #ifndef _MSC_VER
00401   /* set simulator scheduling priority */
00402   if (nice(0) < nice_priority)
00403     {
00404       if (nice(nice_priority - nice(0)) < 0)
00405         fatal("could not renice simulator process");
00406     }
00407 #endif
00408 
00409   /* default architected value... */
00410   sim_num_insn = 0;
00411 
00412 #ifdef BFD_LOADER
00413   /* initialize the bfd library */
00414   bfd_init();
00415 #endif /* BFD_LOADER */
00416 
00417   /* initialize the instruction decoder */
00418   md_init_decoder();
00419 
00420   /* initialize all simulation modules */
00421   sim_init();
00422 
00423   /* initialize architected state */
00424   sim_load_prog(argv[exec_index], argc-exec_index, argv+exec_index, envp);
00425 
00426   /* register all simulator stats */
00427   sim_sdb = stat_new();
00428   sim_reg_stats(sim_sdb);
00429 #if 0 /* not portable... :-( */
00430   stat_reg_uint(sim_sdb, "sim_mem_usage",
00431                 "total simulator (data) memory usage",
00432                 &sim_mem_usage, sim_mem_usage, "%11dk");
00433 #endif
00434 
00435   /* record start of execution time, used in rate stats */
00436   sim_start_time = time((time_t *)NULL);
00437 
00438   /* emit the command line for later reuse */
00439   fprintf(stderr, "sim: command line: ");
00440   for (i=0; i < argc; i++)
00441     fprintf(stderr, "%s ", argv[i]);
00442   fprintf(stderr, "\n");
00443 
00444   /* output simulation conditions */
00445   s = ctime(&sim_start_time);
00446   if (s[strlen(s)-1] == '\n')
00447     s[strlen(s)-1] = '\0';
00448   fprintf(stderr, "\nsim: simulation started @ %s, options follow:\n", s);
00449   opt_print_options(sim_odb, stderr, /* short */TRUE, /* notes */TRUE);
00450   sim_aux_config(stderr);
00451   fprintf(stderr, "\n");
00452 
00453   /* omit option dump time from rate stats */
00454   sim_start_time = time((time_t *)NULL);
00455 
00456   if (init_quit)
00457     exit_now(0);
00458 
00459   running = TRUE;
00460   sim_main();
00461 
00462   /* simulation finished early */
00463   exit_now(0);
00464 
00465   return 0;
00466 }


UVa CS Department of Computer Science
School of Engineering, University of Virginia
151 Engineer's Way, P.O. Box 400740
Charlottesville, Virginia 22904-4740

(434) 982-2200  Fax: (434) 982-2214