-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgc.c
More file actions
250 lines (211 loc) · 6.15 KB
/
gc.c
File metadata and controls
250 lines (211 loc) · 6.15 KB
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
#include <inttypes.h>
#include <stdio.h>
#include <stdint.h>
#include <sys/mman.h>
#include <string.h>
#include <stdlib.h>
static int64_t _DEBUG = 1;
void show_freeptr ();
static void initialize_space (const char* label, int64_t** begin, int64_t** end, int64_t size);
void initialize (int64_t rs, int64_t hs);
void scan ();
void queue (int64_t** node_ref);
void print_void ();
void print_int_vector (int64_t* x);
void print_int (int64_t x);
void print_bool (int64_t x);
void print_bool_vector (int64_t* x);
void print_vector (int64_t* x);
void print_value(int64_t* tag, int64_t x);
void print_result(int64_t* tag, int64_t x);
// types
int64_t ty_void = 0;
int64_t ty_bool = 1;
int64_t ty_int = 2;
int64_t ty_vector = 3;
int64_t ty_function = 4;
int64_t rootstack_size = 0;
int64_t heap_size = 0;
int64_t* free_ptr = NULL;
int64_t* queue_head = NULL;
int64_t* queue_tail = NULL;
int64_t* tospace_begin = NULL;
int64_t* tospace_end = NULL;
int64_t* fromspace_begin = NULL;
int64_t* fromspace_end = NULL;
int64_t* rootstack_ptr = NULL;
int64_t* rootstack_begin = NULL;
int64_t* rootstack_end = NULL;
void show_freeptr () {
if (_DEBUG) {
fprintf(stderr, "\x1b[90m[DEBUG] show_freeptr\nfree_ptr = %p\x1b[0m\n", free_ptr);
}
}
static void initialize_space (const char* label, int64_t** begin, int64_t** end, int64_t size) {
*begin = malloc(size);
if (!*begin) {
fprintf(stderr, "Unable to initialize space for %s\n", label);
exit(1);
}
*(uint8_t**)end = *(uint8_t**)begin + size;
if (_DEBUG) {
fprintf(stderr, "\x1b[90m[DEBUG] initialize_space\n%s = [%p, %p] (%lld)\x1b[0m\n\n", label, begin, end, size);
}
memset(*begin, 0, size);
}
void initialize (int64_t rs, int64_t hs) {
rootstack_size = rs;
heap_size = hs;
if (_DEBUG) {
fprintf(stderr, "\x1b[90m[DEBUG] initialize\nrs: %llu, hs: %llu\x1b[0m\n\n", rs, hs);
}
initialize_space("fromspace", &fromspace_begin, &fromspace_end, heap_size);
initialize_space("tospace", &tospace_begin, &tospace_end, heap_size);
initialize_space("rootstack", &rootstack_begin, &rootstack_end, rootstack_size);
free_ptr = fromspace_begin;
rootstack_ptr = rootstack_begin;
}
void scan () {
int64_t* node = queue_head;
if (_DEBUG) {
fprintf(stderr, "\x1b[90m[DEBUG] scan\nqueue_head = %p\x1b[0m\n\n", node);
}
int64_t size = 0;
int64_t* tag = (int64_t*)node[0];
// Make sure node is a vector.
if (tag[0] != ty_vector) {
fprintf(stderr, "\x1b[31m[ERROR] scan\nnode is not a vector, it was type %d\x1b[0m\n\n", tag[0]);
exit(1);
}
for (int64_t i = 0; i < tag[1]; i++) {
int64_t* e = (int64_t*)node[1 + i];
int64_t* e_tag = (int64_t*)tag[2 + i];
// Skip if its not a vector
if (e_tag[0] == ty_vector) {
queue((int64_t**)(&node[1 + i]));
}
}
// Calculate new size and update queue.
size = tag[1] + 1;
queue_head += size;
}
void queue (int64_t** node_ref) {
if (_DEBUG) {
fprintf(stderr, "\x1b[90m[DEBUG] queue\nnode_ref = %p\x1b[0m\n\n", node_ref);
}
int64_t* node = *node_ref;
int64_t* new_tail = queue_tail;
int64_t size = 0;
int64_t* tag = (int64_t*)node[0];
if (tospace_begin <= tag && tag < tospace_end) {
new_tail = tag;
if (_DEBUG) {
fprintf(stderr, "\x1b[90m[DEBUG] queue\nnew_tail = %p\x1b[0m\n\n", (int64_t*)*tag);
}
} else {
size = tag[1] + 1;
if (_DEBUG) {
fprintf(stderr, "\x1b[90m[DEBUG] queue\nsize = %llu\x1b[0m\n\n", size);
}
}
// Update queue and its tail.
queue_tail += size;
*node_ref = new_tail;
*(int64_t**)node = new_tail;
}
void collect (int64_t* updated_rootstack_ptr) {
if (_DEBUG) {
fprintf(stderr, "\x1b[90m[DEBUG] collect\x1b[0m\n\n");
}
rootstack_ptr = updated_rootstack_ptr;
queue_head = tospace_begin;
queue_tail = tospace_begin;
int64_t* root = rootstack_ptr;
while (root != rootstack_end) {
if (*root) {
queue((int16_t**)root);
}
root++;
}
while (queue_head != queue_tail) {
scan();
}
free_ptr = queue_tail;
// Swap the active and inactive memory blocks in the heap.
int64_t* tospace_begin_copy = tospace_begin;
int64_t* tospace_end_copy = tospace_end;
tospace_begin = fromspace_begin;
tospace_end = fromspace_end;
fromspace_begin = tospace_begin_copy;
fromspace_end = tospace_end_copy;
// Reset memory.
memset(*tospace_begin, 0, heap_size);
}
void print_void () {
printf("void");
}
void print_int (int64_t x) {
printf("%lld", x);
}
void print_int_vector (int64_t* x) {
printf("%lld", x[1]);
}
void print_bool (int64_t x) {
if (x == 1) {
printf("true");
} else if (x == 0) {
printf("false");
} else {
printf("BAD BOOL TYPE");
}
}
void print_bool_vector (int64_t* x) {
if (x[1] == 1) {
printf("true");
} else if (x[1] == 0) {
printf("false");
} else {
printf("BAD BOOL TYPE");
}
}
void print_vector (int64_t* x) {
int64_t* tag;
for (int64_t i = 0; i < x[1]; i++) {
tag = x[i + 2];
if (tag[0] == ty_int) { print_int(tag[1]); }
else if (tag[0] == ty_bool) { print_bool(tag[1]); }
else if (tag[0] == ty_void) { print_void(tag[1]); }
else {
printf("(");
print_vector(tag);
printf(")");
}
if (i + 1 < x[1]) {
printf(", ");
}
}
}
void print_value(int64_t* tag, int64_t x) {
if (tag[0] == ty_void) { print_void(); }
else if (tag[0] == ty_int && x > 10000000) { print_int_vector(x); }
else if (tag[0] == ty_bool && x > 10000000) { print_bool_vector(x); }
else if (tag[0] == ty_int) { print_int(x); }
else if (tag[0] == ty_bool) { print_bool(x); }
else if (tag[0] == ty_vector) { printf("("); print_vector(x); }
else { printf("\x1b[31mUnknown Tag Type\n\x1b[0m%lld <%p>", tag[0], tag); }
}
void print_result(int64_t* tag, int64_t x) {
print_value(tag, x);
if (tag[0] == ty_void) { printf("\x1b[90m : void\x1b[0m"); }
else if (tag[0] == ty_int) { printf("\x1b[90m : int\x1b[0m"); }
else if (tag[0] == ty_int) { printf("\x1b[90m : int\x1b[0m"); }
else if (tag[0] == ty_bool) { printf("\x1b[90m : bool \x1b[0m"); }
else if (tag[0] == ty_vector) { printf(")\x1b[90m : vector\x1b[0m"); }
else { printf("\x1b[90m : undefined\x1b[0m"); }
printf("\n");
}
int64_t read_int() {
int64_t x = 0;
scanf("%lld", &x);
return x;
}