-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
160 lines (147 loc) · 4.16 KB
/
main.c
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
/* Asp applicative language
* J.Cupitt, November 86
* Main section */
#include <sys/time.h> /* Used for finding execution time. */
#include <sys/resource.h>
#include "asphdr.h" /* Get main header */
#include "evaltypes.h" /* Needed by symboltypes.h */
#include "lextypes.h" /* Needed by parsetypes.h */
#include "parsetypes.h" /* Needed by symboltypes.h */
#include "symboltypes.h" /* Needed by symbol.h */
#include "symbol.h" /* Get symbol handler */
#include "memory.h" /* Get memory handler */
#include "dump.h" /* Need dumper for debugging */
#include "subexpr.h" /* Need subexpr remover */
#include "declare.h" /* Need to test for UNRESOLVED */
#include "parser.h" /* Need to see ST */
#include "compile.h" /* Get compiler */
#include "evaluate.h" /* Need evaluator */
#include "io.h" /* Need to init input */
#include "error.h" /* Error handler */
#define DASTACKSIZE 500 /* Obviously modest! */
#define DFSTACKSIZE 100 /* Fence stack */
#define DPSTACKSIZE 1000 /* Also modest */
#define DEFAULTEVAL 60000 /* Modest size for default */
#define DEFAULTPARSE 20000 /* Also modest */
/* Command line stuff. Available globally in main.h */
enum Boolean Warnings = TRUE; /* -w not issued */
enum Boolean Extras = TRUE; /* - not issued */
enum Boolean Counting = FALSE; /* -c issued */
int EvalHeapSize = DEFAULTEVAL; /* Size of eval heap */
int ParseHeapSize = DEFAULTPARSE; /* And size of parse heap */
int AStackSize = DASTACKSIZE; /* Application stack size */
int FStackSize = DFSTACKSIZE; /* Fence stack */
int PStackSize = DPSTACKSIZE; /* Pointer stack */
char *OurName; /* Name we were called by */
/* Print a usage message & exit */
static void
usage()
{ errorstart();
errorstr( "usage: " );
errorstr( OurName );
errorstr( " [-w -c - -h<n> -p<n> -a<n> -g<n> -f<n>] [{filename}]" );
errorend();
quit();
}
/* Get an uint from a string. Used for getting heap sizes */
static int
GetUInt( str )
char *str;
{ int res;
if( sscanf( str, "%u", &res) != 1 ) {
errorstart();
errorstr( "heap size should be unsigned int" );
errorend();
quit();
}
return( res );
}
/* Read elapsed time for this job. Return a long with the bottom
* two digits fractions of a sec. */
static long
dotime()
{ long r;
struct rusage useblock;
if( getrusage( RUSAGE_SELF, &useblock ) != 0 ) {
errorstart();
errorstr( "call to getrusage fails!" );
errorend();
quit();
}
r = (useblock.ru_utime.tv_usec/10000L) +
(useblock.ru_utime.tv_sec*100L);
return( r );
}
/* Start here ... */
void
main( argc, argv )
int argc;
char *argv[];
{ long time; /* Used for timing execution */
OurName = *argv; /* Note name we were called by */
while( --argc )
if( argv[argc][0] == '-' )
switch( argv[argc][1] ) {
case 'w':
Warnings = FALSE;
break;
case 'c':
Counting = TRUE;
break;
case '\0':
Extras = FALSE;
break;
case 'h':
EvalHeapSize = GetUInt( argv[argc]+2 );
break;
case 'p':
ParseHeapSize = GetUInt( argv[argc]+2 );
break;
case 'a':
AStackSize = GetUInt( argv[argc]+2 );
break;
case 'g':
PStackSize = GetUInt( argv[argc]+2 );
break;
case 'f':
FStackSize = GetUInt( argv[argc]+2 );
break;
default:
usage();
break;
}
else /* It's a file name */
AddFile( argv[ argc ] );
InitMemory();
InitParseTree();
InitEvalStacks();
ParseFiles();
TestDeclared();
CommonSub();
#ifdef DEBUG
printf( "Dumping ST:\n" );
DumpST( CurrentTable );
#endif
CompileTable();
if( Counting )
printf( "# Generated %d nodes\n", UsedSpace() );
if( Extras )
printf( "Starting evaluation\n" );
time = dotime(); /* Read clock */
EvaluateScript();
time = dotime() - time; /* Elasped CPU */
if( Extras )
printf( "Finished\n" );
#ifdef TREES
printf( "Graph after reduction is:\n" );
DumpGraph( 1, &ScriptRoot->details.func.code );
#endif
if( Counting ) {
printf( "# %d compactions, %d reductions\n",
NumberCompacts, NumberReductions );
printf( "# Graph after reduction contains %d nodes\n",
CountGraph( &ScriptRoot->details.func.code ) );
printf( "# %d nodes in use\n", UsedSpace() );
printf( "# %.2f secs CPU\n", (float) time/100L );
}
}