-
Notifications
You must be signed in to change notification settings - Fork 16
/
startup.c
342 lines (307 loc) · 8.63 KB
/
startup.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dlfcn.h>
typedef long Ptr;
struct RootSet
{
unsigned int usedregs;
Ptr *fp;
Ptr cp;
Ptr ac;
Ptr t1;
Ptr t2;
Ptr t3;
};
#define ws 4
#define number_tag 0
#define immed_tag 1
#define pair_tag 2
#define string_tag 3 /* It is a variety of string if tag is string and object tag is number or immediate. */
#define symbol_tag 4
#define vector_tag 5
#define proc_tag 6
#define float_tag 7
#define mask 7
#define tag_len 3
#define bool_tag 0x01
#define null_tag 0x09
#define char_tag 0x11
#define imm_tag_len 8
#define imm_mask 0xFF
#define attr_len (tag_len + 1)
#define ac_bit (1<<0)
#define t1_bit (1<<1)
#define t2_bit (1<<2)
#define t3_bit (1<<3)
#define DEFAULT_HEAP_SIZE (4*10000)
#define DEFAULT_STACK_SIZE (4*10000)
#define PTR(ptr) ((Ptr)(ptr))
#define TAG(ptr) (PTR(ptr) & mask)
#define UNTAG(ptr) (PTR(ptr) & (~mask))
#define IMMTAG(ptr) (PTR(ptr) & imm_mask)
#define OBJ(ptr) ((Ptr *)UNTAG(ptr))
#define OBJLENGTH(ptr) (*OBJ(ptr) >> attr_len)
#define OBJTAG(ptr) TAG(*OBJ(ptr) >> 1)
#define CAR(ptr) (*(OBJ(ptr) + 1))
#define CDR(ptr) (*(OBJ(ptr) + 2))
#define STRINGDATA(ptr) ((char *)(OBJ(ptr) + 1))
#define SYMBOLNAME(ptr) (*(OBJ(ptr) + 1))
#define VECTORDATA(ptr) (OBJ(ptr) + 1)
/*#define VERBOSE*/
#ifdef VERBOSE
# define LOG(...) fprintf(stderr, __VA_ARGS__)
#else
# define LOG(...)
#endif
extern Ptr call_scheme();
extern char *stack_bottom;
extern char *gc_free;
extern char *heap_end;
extern Ptr global_refs;
static unsigned int gc_space_size;
static char *gc_cur_space;
static char *gc_to_space;
static char *gc_scan;
static void *rtldDefault = 0;
static void error_exit(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
exit(1);
}
static unsigned int align(unsigned int n) { return (n+1) & ~1; } /* 2n alignment */
static unsigned int object_size(Ptr *obj)
{
unsigned int tag = OBJTAG(obj);
unsigned int len = OBJLENGTH(obj);
unsigned int full_len = 1;
if (tag == string_tag || tag == number_tag || tag == immed_tag) { /* string or box or bytevector */
full_len += ((len+3) / 4);
} else if (tag == proc_tag) {
full_len += 1+len;
} else { /* pair or symbol or vector */
full_len += len;
}
return align(full_len) * ws;
}
static unsigned int content_offset(Ptr *obj)
{
unsigned int tag = OBJTAG(obj);
unsigned int offset = 1;
if (tag == proc_tag) offset++;
return offset;
}
static void gc_initialize(unsigned int heap_size)
{
gc_space_size = heap_size;
gc_free = gc_cur_space = malloc(gc_space_size);
memset(gc_cur_space, 0xcd, gc_space_size);
gc_to_space = malloc(gc_space_size);
memset(gc_to_space, 0xfd, gc_space_size);
heap_end = gc_cur_space + gc_space_size;
if (TAG(gc_cur_space) != 0 || TAG(gc_to_space) != 0) {
error_exit("memory not aligned\n");
}
}
static void set_forward(Ptr *obj, Ptr to) { *obj = to | 0x1; }
static int forwarded(Ptr *obj) { return *obj & 0x1; }
/* Cheney collector */
static void gc_copy_forward(Ptr *p)
{
unsigned int tag = TAG(*p);
if (tag == number_tag || tag == immed_tag || tag == float_tag) { return; } /* number or immediate or float -> ignore */
Ptr *obj = OBJ(*p);
if (obj == 0) { return; } /* null pointer -> ignore */
if (forwarded(obj)) { /* update pointer with forwarding info */
LOG("U%x", tag);
*p = UNTAG(*obj) | tag;
} else { /* copy and forward object */
LOG("C%x", tag);
unsigned int size = object_size(obj);
memcpy(gc_free, obj, size);
set_forward(obj, PTR(gc_free));
*p = PTR(gc_free) | tag;
gc_free += size;
}
}
static void gc_walk_roots(struct RootSet *root)
{
LOG("gc_walk_roots\n");
LOG("walk registers\n");
root->cp |= proc_tag; /* cp is untagged. tag it temporarily. */
gc_copy_forward(&root->cp);
root->cp = UNTAG(root->cp);
if (root->usedregs & ac_bit) gc_copy_forward(&root->ac);
if (root->usedregs & t1_bit) gc_copy_forward(&root->t1);
if (root->usedregs & t2_bit) gc_copy_forward(&root->t2);
if (root->usedregs & t3_bit) gc_copy_forward(&root->t3);
LOG("\n");
LOG("walk stack\n");
Ptr *fp = (Ptr *)stack_bottom+1;
while (fp <= root->fp) {
unsigned int size = *(fp-1) / ws;
Ptr *p = fp+1; /* skip return code pointer */
for (; p<fp+size; p++)
gc_copy_forward(p);
fp = fp+size+1;
}
LOG("\n");
LOG("walk global references\n");
gc_copy_forward(&global_refs);
LOG("\n");
LOG("end gc_walk_roots\n");
}
void gc_collect(struct RootSet *root)
{
LOG(";; gc_collect called\n");
memset(gc_to_space, 0xcd, gc_space_size);
gc_scan = gc_free = gc_to_space;
gc_walk_roots(root);
while (gc_scan < gc_free) {
Ptr *obj = OBJ(gc_scan);
switch (OBJTAG(obj)) {
case float_tag:
error_exit("scan incorrect, unboxed type.\n");
break;
case pair_tag:
case symbol_tag:
case vector_tag:
case proc_tag:
{
unsigned int i, len = OBJLENGTH(obj);
unsigned int offset = content_offset(obj);
for (i=0; i<len; i++)
gc_copy_forward(&obj[i+offset]);
LOG("\n");
gc_scan += align(offset + len) * ws;
break;
}
case number_tag: /* box */
case immed_tag: /* bytevector */
case string_tag:
gc_scan += object_size(obj);
break;
}
}
LOG("switch roles of gc spaces\n");
char *temp = gc_to_space;
gc_to_space = gc_cur_space;
gc_cur_space = temp;
heap_end = gc_cur_space + gc_space_size;
memset(gc_to_space, 0xfd, gc_space_size);
}
void *dlsym_subr(const char* symbol) { return dlsym(rtldDefault, symbol); }
static void print_string(Ptr ptr)
{
int n;
char *s;
n = OBJLENGTH(ptr);
s = STRINGDATA(ptr);
while (n--) {
if (*s == 0) {
printf("\\0");
} else {
if (*s == '"' || *s == '\\') printf("\\");
printf("%c", *s);
}
s++;
}
}
static void print(Ptr ptr)
{
switch (TAG(ptr)) {
case number_tag:
printf("%ld", ptr/(mask+1)); break;
case immed_tag:
switch (IMMTAG(ptr)) {
case bool_tag:
printf((ptr>>imm_tag_len) ? "#t" : "#f"); break;
case null_tag:
printf("()"); break;
case char_tag:
switch (ptr>>imm_tag_len) {
case '\n':
printf("#\\newline"); break;
case ' ':
printf("#\\space"); break;
case 9:
printf("#\\tab"); break;
default:
printf("#\\%c", (char)(ptr>>imm_tag_len)); break;
}
break;
}
break;
case pair_tag:
printf("(");
print(CAR(ptr));
ptr = CDR(ptr);
while (TAG(ptr) == pair_tag) {
printf(" ");
print(CAR(ptr));
ptr = CDR(ptr);
}
if (IMMTAG(ptr) != null_tag) {
printf(" . ");
print(ptr);
}
printf(")");
break;
case string_tag:
printf("\"");
print_string(ptr);
printf("\"");
break;
case symbol_tag:
print_string(SYMBOLNAME(ptr));
break;
case vector_tag:
{
int n;
Ptr *p;
printf("#(");
n = OBJLENGTH(ptr);
p = VECTORDATA(ptr);
if (n != 0) {
print(*p);
while (--n) {
printf(" ");
print(*++p);
}
}
printf(")");
break;
}
case proc_tag:
printf("<procedure>");
break;
case float_tag:
{
Ptr x = UNTAG(ptr);
printf("%f", *(float *)&x);
break;
}
default:
printf("#<garbage %x>", (unsigned int)ptr);
break;
}
}
int main(int argc, char *argv[])
{
stack_bottom = malloc(DEFAULT_STACK_SIZE);
memset(stack_bottom, 0xcc, DEFAULT_STACK_SIZE);
#ifdef HEAP_SIZE
unsigned int heap_size = HEAP_SIZE;
#else
unsigned int heap_size = DEFAULT_HEAP_SIZE;
#endif
gc_initialize(heap_size);
rtldDefault = dlopen(0, RTLD_NOW | RTLD_GLOBAL);
print(call_scheme());
printf("\n");
return 0;
}