1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
|
/* main.c
*
* COPYRIGHT (c) 1993 by AT&T Bell Laboratories.
*
* This is the main routine for the interactive version of SML/NJ.
*/
#include <stdio.h>
#include <ctype.h>
#include "ml-base.h"
#include "ml-options.h"
#include "ml-limits.h"
#include "ml-globals.h"
#ifdef COLLECT_STATS
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "stats-data.h"
#endif
FILE *DebugF = NULL;
#ifdef TARGET_BYTECODE
FILE *BC_stdout = NULL;
#endif
/* Runtime globals */
bool_t SilentLoad = TRUE;
bool_t DumpObjectStrings = FALSE;
bool_t GCMessages = FALSE;
char **RawArgs;
char **CmdLineArgs; /* does not include the command name (argv[0]) */
char *MLCmdName; /* the command name used to invoke the runtime */
#ifdef HACKED_STANDALONE
bool_t StandAlone = TRUE;
#endif
/* local variables */
PVT bool_t isBoot = FALSE; /* true if we should bootstrap a system */
PVT char *LoadImage /* the path name of the image file to load */
= DFLT_IMAGE;
PVT char *BootFrom /* the boot source (bin file list file). */
= NULL;
# ifdef MP_SUPPORT
PVT int NumProcs = 1; /* not used */
# endif
PVT void ParseOptions (int argc, char **argv, heap_params_t **heapParams);
int main (int argc, char **argv)
{
heap_params_t *heapParams;
DebugF = stderr;
/* process the command-line options */
ParseOptions (argc, argv, &heapParams);
#ifdef TARGET_BYTECODE
if (BC_stdout == NULL)
BC_stdout = fdopen(dup(1), "w");
#endif
InitTimers ();
RecordGlobals ();
InitCFunList ();
#ifdef MP_SUPPORT
MP_Init();
#endif
/* start ML */
if (isBoot) {
BootML (BootFrom, heapParams);
}
else { /* load an image */
LoadML (LoadImage, heapParams);
}
Exit (0);
} /* end of main. */
/* ParseOptions:
*
* Parse the command-line options.
*/
PVT void ParseOptions (int argc, char **argv, heap_params_t **heapParams)
{
char option[MAX_OPT_LEN], *optionArg, **nextArg;
bool_t errFlg = FALSE;
/* first scan for any heap/GC parameters */
if ((*heapParams = ParseHeapParams(argv)) == NIL(heap_params_t *))
errFlg = TRUE;
RawArgs = argv;
CmdLineArgs = NEW_VEC(char *, argc);
MLCmdName = *argv++;
#ifdef HACKED_STANDALONE
LoadImage = MLCmdName;
#endif
nextArg = CmdLineArgs;
while (--argc > 0) {
char *arg = *argv++;
#define MATCH(opt) (strcmp(opt, option) == 0)
#define CHECK(opt) { \
if (optionArg[0] == '\0') { \
errFlg = TRUE; \
Error("missing argument for \"%s\" option\n", opt); \
continue; \
} \
} /* CHECK */
if (isRuntimeOption(arg, option, &optionArg)) {
if (MATCH("boot")) {
CHECK("boot");
isBoot = TRUE;
BootFrom = optionArg;
}
else if (MATCH("load")) {
CHECK("load");
LoadImage = optionArg;
#ifdef HACKED_STANDALONE
LoadImage = MLCmdName;
#endif
}
else if (MATCH("cmdname")) {
CHECK("cmdname");
MLCmdName = optionArg;
#ifdef HACKED_STANDALONE
StandAlone = FALSE;
#endif
}
#ifdef MP_SUPPORT
else if (MATCH("nprocs")) {
CHECK("nprocs");
NumProcs = atoi(optionArg);
if (NumProcs < 0)
NumProcs = 0;
else if (NumProcs > MAX_NUM_PROCS)
NumProcs = MAX_NUM_PROCS;
}
#endif
else if (MATCH("quiet")) {
SilentLoad = TRUE;
}
else if (MATCH("verbose")) {
SilentLoad = FALSE;
}
else if (MATCH("objects")) {
DumpObjectStrings = TRUE;
}
else if (MATCH("debug")) {
CHECK("debug");
if ((DebugF = fopen(optionArg, "w")) == NULL) {
DebugF = stderr; /* restore the file pointer */
errFlg = TRUE;
Error("unable to open debug output file \"%s\"\n", *(argv[-1]));
continue;
}
}
#ifdef COLLECT_STATS
else if (MATCH("stats")) {
CHECK("stats");
StatsFD = open (optionArg, O_WRONLY|O_TRUNC|O_CREAT, 0666);
if (StatsFD == -1) {
errFlg = TRUE;
Error("unable to open statistics file \"%s\"\n", *(argv[-1]));
continue;
}
}
#endif
#ifdef TARGET_BYTECODE
else if (MATCH("dump")) {
CHECK("dump");
BC_stdout = fopen (optionArg, "w");
}
#ifdef INSTR_TRACE
else if (MATCH("trace")) {
extern bool_t traceOn;
traceOn = TRUE;
}
#endif
#endif
}
else {
*nextArg++ = arg;
}
} /* end while */
*nextArg = NIL(char *);
if (errFlg)
Exit (1);
} /* end of ParseOptions */
/* Exit:
* Exit from the ML system.
*/
void Exit (int code)
{
#if COUNT_REG_MASKS
DumpMasks();
#endif
#ifdef COLLECT_STATS
if (StatsFD >= 0) {
STATS_FLUSH_BUF();
close (StatsFD);
}
#endif
#if (defined(TARGET_BYTECODE) && defined(INSTR_COUNT))
{
extern void PrintInstrCount (FILE *f);
PrintInstrCount (DebugF);
}
#endif
exit (code);
} /* end of Exit */
|