-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.c
273 lines (265 loc) · 6.59 KB
/
utils.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
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*
module : utils.c
version : 1.1.1.3
date : 12/16/24
*/
#include <stdio.h>
#include <time.h>
#include "globals.h"
#define CORRECT_GARBAGE_COLLECTOR
/* PUBLIC int clock(); */ /* file time.h */
PUBLIC void getsym(); /* file scan.c */
PUBLIC void error(char *message);
PUBLIC void execerror(char *message, char *op); /* file main.c */
PUBLIC void lookup();
/* PUBLIC void exit(); */ /* file interp.c */
static Node
memory[MEMORYMAX],
*memoryindex = memory,
*mem_low = memory,
*mem_mid;
#define MEM_HIGH (MEMORYMAX-1)
static int direction = +1;
static int nodesinspected, nodescopied;
static int start_gc_clock;
PUBLIC void inimem1()
{
stk = conts = dump = dump1 = dump2 = dump3 = dump4 = dump5 = NULL;
direction = +1;
memoryindex = mem_low;
}
PUBLIC void inimem2()
{
mem_low = memoryindex;
#ifdef CORRECT_GARBAGE_COLLECTOR
mem_mid = mem_low + (&memory[MEM_HIGH] - mem_low) / 2;
#else
mem_mid = mem_low + (MEM_HIGH)/2;
#endif
if (tracegc > 1)
{ printf("memory = %ld : %ld\n",
(long)memory,MEM2INT(memory));
printf("memoryindex = %ld : %ld\n",
(long)memoryindex,MEM2INT(memoryindex));
printf("mem_low = %ld : %ld\n",
(long)mem_low,MEM2INT(mem_low));
printf("top of mem = %ld : %ld\n",
(long)(&memory[MEM_HIGH]),MEM2INT((&memory[MEM_HIGH])));
printf("mem_mid = %ld : %ld\n",
(long)mem_mid,MEM2INT(mem_mid)); }
}
PUBLIC void printnode(p)
Node *p;
{
printf("%10ld: %-10s %10ld %10ld\n",
MEM2INT(p),
symtab[(short) p->op].name,
p->op == LIST_ ? MEM2INT(p->u.lis) : p->u.num,
MEM2INT(p->next));
}
PRIVATE Node *copy(n)
Node *n;
{
Node *temp;
nodesinspected++;
if (tracegc > 4) printf("copy ..\n");
if (n == NULL) return NULL;
if (n < mem_low) return n; /* later: combine with previous line */
if (n->op == ILLEGAL_)
{ printf("copy: illegal node "); printnode(n); return(NULL); }
if (n->op == COPIED_) return n->u.lis;
temp = memoryindex; memoryindex += direction;
temp->op = n->op;
temp->u.num = n->op == LIST_ ? (long)copy(n->u.lis) : n->u.num;
temp->next = copy(n->next);
n->op = COPIED_;
n->u.lis = temp;
nodescopied++;
if (tracegc > 3)
{ printf("%5d - ",nodescopied); printnode(temp); }
return temp;
}
PUBLIC void writeterm(Node *n);
PUBLIC void gc1(mess)
char * mess;
{
start_gc_clock = clock();
if (tracegc > 1)
printf("begin %s garbage collection\n",mess);
direction = - direction;
memoryindex = (direction == 1) ? mem_low : &memory[MEM_HIGH];
/*
if (tracegc > 1)
{ printf("direction = %d\n",direction);
printf("memoryindex = %d : %d\n",
(long)memoryindex,MEM2INT(memoryindex)); }
*/
nodesinspected = nodescopied = 0;
#define COP(X,NAME) \
if (X != NULL) \
{ if (tracegc > 2) \
{ printf("old %s = ",NAME); \
writeterm(X); printf("\n"); } \
X = copy(X); \
if (tracegc > 2) \
{ printf("new %s = ",NAME); \
writeterm(X); printf("\n"); } }
COP(stk,"stk"); COP(prog,"prog"); COP(conts,"conts");
COP(dump,"dump"); COP(dump1,"dump1"); COP(dump2,"dump2");
COP(dump3,"dump3"); COP(dump4,"dump4"); COP(dump5,"dump5");
}
PRIVATE void gc2(mess)
char * mess;
{
int this_gc_clock;
this_gc_clock = clock() - start_gc_clock;
if (this_gc_clock == 0) this_gc_clock = 1; /* correction */
if (tracegc > 0)
printf("gc - %d nodes inspected, %d nodes copied, clock: %d\n",
nodesinspected,nodescopied,this_gc_clock);
if (tracegc > 1)
printf("end %s garbage collection\n",mess);
gc_clock += this_gc_clock;
}
PUBLIC void gc_()
{
gc1("user requested");
gc2("user requested");
}
PUBLIC Node *newnode(o,l,r)
Operator o;
long l;
Node *r;
{
Node *p;
if (memoryindex == mem_mid)
{ gc1("automatic");
if (o == LIST_) l = (long)copy(l);
r = copy(r);
#ifdef CORRECT_GARBAGE_COLLECTOR
gc2("automatic");
if ((direction == +1 && memoryindex >= mem_mid) ||
(direction == -1 && memoryindex <= mem_mid))
execerror("memory", "copying"); }
#else
gc2("automatic"); }
#endif
p = memoryindex;
memoryindex += direction;
p->op = o;
p->u.num = l;
p->next = r;
D( printnode(p); )
return p;
}
PUBLIC void memoryindex_()
{
stk = newnode(INTEGER_,MEM2INT(memoryindex),stk);
}
PUBLIC void readfactor() /* read a JOY factor */
{
switch (sym)
{ case ATOM:
lookup();
D( printf("readfactor: location = %ld\n",(long) location); )
if (location < firstlibra)
stk = newnode(LOC2INT(location),location->u.proc,stk);
else stk = newnode(USR_,location,stk);
return;
case INTEGER_: case CHAR_: case STRING_:
stk = newnode(sym,num,stk);
return;
case LBRACE:
{ int set = 0; getsym();
while (sym != RBRACE)
{ if (sym == CHAR_ || sym == INTEGER_)
set = set | (1 << num);
else error("numeric expected in set");
getsym(); }
stk = newnode(SET_,set,stk); }
return;
case LBRACK:
{ void readterm();
getsym();
readterm();
if (sym != RBRACK)
error("']' expected");
return; }
/* MU - MU x IN --x--x-- .
stk = newnode(MU,
newnode(IN, entry to x, body), stk);
return;
*/
default:
error("a factor cannot begin with this symbol");
return; }
}
PUBLIC void readterm()
{
stk = newnode(LIST_,NULL,stk);
if (sym <= ATOM)
{ readfactor();
stk->next->u.lis = stk;
stk = stk->next;
stk->u.lis->next = NULL;
dump = newnode(LIST_,stk->u.lis,dump);
getsym();
while (sym <= ATOM)
{ readfactor();
dump->u.lis->next = stk;
stk = stk->next;
dump->u.lis->next->next = NULL;
dump->u.lis = dump->u.lis->next;
getsym(); }
dump = dump->next; }
}
PUBLIC void writefactor(n)
Node *n;
{
if (n == NULL)
#ifdef DEBUG
return;
#else
execerror("non-empty stack","print");
#endif
switch (n->op)
{ case BOOLEAN_:
printf("%s", n->u.num ? "true" : "false"); return;
case INTEGER_:
printf("%ld",n->u.num); return;
case SET_:
{ int i; long set = n->u.set;
printf("{");
for (i = 0; i < SETSIZE; i++)
if (set & (1 << i))
{ printf("%d",i);
set = set & ~(1 << i);
if (set != 0)
printf(" "); }
printf("}");
return; }
case CHAR_:
printf("'%c", (char) n->u.num); return;
case STRING_:
printf("\"%s\"",n->u.str); return;
case LIST_:
printf("%s","[");
writeterm(n->u.lis);
printf("%s","]");
return;
case USR_:
printf("%s", n->u.ent->name ); return;
default:
printf("%s",symtab[(int) n->op].name); return; }
}
PUBLIC void writeterm(n)
Node *n;
{
while (n != NULL)
{
writefactor(n);
n = n->next;
if (n != NULL)
printf(" ");
}
}