Skip to content

Commit bbcfd2c

Browse files
committed
Clean up GC code and add string functionality.
1 parent 4174503 commit bbcfd2c

File tree

4 files changed

+141
-121
lines changed

4 files changed

+141
-121
lines changed

langs/iniquity-gc/Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ objs = \
1111
main.o \
1212
values.o \
1313
print.o \
14-
io.o
14+
io.o \
15+
gc.o
1516

1617
default: runtime.o
1718

langs/iniquity-gc/gc.c

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
#include <inttypes.h>
2+
#include <stdlib.h>
3+
#include <string.h>
4+
#include <stdio.h>
5+
#include "values.h"
6+
#include "runtime.h"
7+
8+
const char* val_typeof_string(int64_t t) {
9+
switch (val_typeof(t)) {
10+
case T_INT: return "INT";
11+
case T_BOOL: return "BOOL";
12+
case T_CHAR: return "CHAR";
13+
case T_EOF: return "EOF";
14+
case T_VOID: return "VOID";
15+
case T_EMPTY: return "EMPTY";
16+
case T_BOX: return "BOX";
17+
case T_CONS: return "CONS";
18+
case T_VECT: return "VECT";
19+
case T_STR: return "STR";
20+
default: return "UNKNOWN";
21+
}
22+
}
23+
24+
void step(val_t* to, val_t* from, val_t** to_curr, val_t** to_next, int count, int* t_back) {
25+
type_t t;
26+
int i;
27+
int size;
28+
val_t v;
29+
val_t *ptr_v;
30+
for (i = 0; i < count; i++) {
31+
v = **to_curr;
32+
ptr_v = val_unwrap(v);
33+
t = val_typeof(v);
34+
switch (t) {
35+
case T_BOX:
36+
case T_CONS:
37+
case T_VECT:
38+
case T_STR:
39+
if (ptr_v >= from && ptr_v < from + heap_size) {
40+
// this is a pointer to from space so we need to deal with it
41+
if (val_unwrap(*ptr_v) >= to &&
42+
val_unwrap(*ptr_v) < to + heap_size) {
43+
// it points to a fwd pointer (points in to to-space), so just set
44+
// curr to what it points to.
45+
**to_curr = *ptr_v;
46+
*to_curr = *to_curr + 1;
47+
} else {
48+
// copy, fwd, update
49+
size = val_size(ptr_v, t);
50+
types[*t_back] = t; // enqueue type
51+
*t_back = *t_back + 1;
52+
memcpy(*to_next, ptr_v, 8 * size); // copy
53+
*ptr_v = val_wrap(*to_next, t); // fwd
54+
**to_curr = val_wrap(*to_next, t); // update
55+
*to_next = *to_next + size;
56+
*to_curr = *to_curr + 1;
57+
}
58+
} else {
59+
// looks like a pointer, but doesn't point to from-space
60+
// leave it alone
61+
*to_curr = *to_curr + 1;
62+
}
63+
break;
64+
default:
65+
// not a pointer
66+
*to_curr = *to_curr + 1;
67+
}
68+
}
69+
}
70+
71+
72+
int64_t* collect_garbage(int64_t* rsp, int64_t *rbp, int64_t* rbx) {
73+
int stack_count = rbp - rsp;
74+
75+
val_t *from = heap + ((rbx < (heap + heap_size)) ? 0 : heap_size);
76+
val_t *to = heap + ((rbx < (heap + heap_size)) ? heap_size : 0);
77+
78+
val_t *to_next = to;
79+
val_t *to_curr = to;
80+
81+
int t_back = 0;
82+
int t_front = 0;
83+
84+
// Step through everything on the stack
85+
val_t * rsp_curr = rsp;
86+
step(to, from, &rsp_curr, &to_next, stack_count, &t_back);
87+
88+
printf("DONE STACK\n");
89+
90+
int vi;
91+
// now play catch up between to_curr and to_next
92+
while (to_curr != to_next) {
93+
switch (types[t_front++]) {
94+
case T_VECT:
95+
vi = to_curr[0];
96+
to_curr++;
97+
step(to, from, &to_curr, &to_next, vi, &t_back);
98+
break;
99+
case T_BOX:
100+
step(to, from, &to_curr, &to_next, 1, &t_back);
101+
break;
102+
case T_CONS:
103+
step(to, from, &to_curr, &to_next, 2, &t_back);
104+
break;
105+
case T_STR:
106+
to_curr = to_curr + 1 + ((*to_curr + 1) / 2);
107+
break;
108+
default:
109+
to_curr++;
110+
break;
111+
}
112+
}
113+
return to_next;
114+
}
115+
116+
117+
void print_memory(int64_t* rsp, int64_t* rbp, int64_t* rbx) {
118+
119+
val_t* h = heap + ((rbx < (heap + heap_size)) ? 0 : heap_size);
120+
121+
int stack_count = rbp - rsp;
122+
int heap_count = rbx - h;
123+
124+
printf("----------------------------------------------------------------\n");
125+
int i;
126+
127+
printf("STACK:\n");
128+
for (i = 0; i < stack_count; i++) {
129+
printf("[%" PRIx64 "] = %016" PRIx64 ", %s\n",
130+
(int64_t)rsp + 8*i, rsp[i], val_typeof_string(rsp[i]));
131+
}
132+
printf("HEAP:\n");
133+
for (i = 0; i < heap_count; i++) {
134+
printf("[%" PRIx64 "] = %016" PRIx64 ", %s\n",
135+
(int64_t)h + 8*i, h[i], val_typeof_string(h[i]));
136+
}
137+
}

langs/iniquity-gc/main.c

Lines changed: 1 addition & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#include <stdio.h>
22
#include <stdlib.h>
33
#include <inttypes.h>
4-
#include <string.h>
54
#include "values.h"
65
#include "print.h"
76
#include "runtime.h"
@@ -30,7 +29,7 @@ int main(int argc, char** argv)
3029
error_handler = &error_exit;
3130
heap = malloc(2 * 8 * heap_size);
3231
types = malloc(sizeof(type_t) * heap_size);
33-
32+
3433
val_t result;
3534

3635
result = entry(heap);
@@ -42,121 +41,3 @@ int main(int argc, char** argv)
4241
free(heap);
4342
return 0;
4443
}
45-
46-
const char* val_typeof_string(int64_t t) {
47-
switch (val_typeof(t)) {
48-
case T_INT: return "INT";
49-
case T_BOOL: return "BOOL";
50-
case T_CHAR: return "CHAR";
51-
case T_EOF: return "EOF";
52-
case T_VOID: return "VOID";
53-
case T_EMPTY: return "EMPTY";
54-
case T_BOX: return "BOX";
55-
case T_CONS: return "CONS";
56-
case T_VECT: return "VECT";
57-
case T_STR: return "STR";
58-
default: return "UNKNOWN";
59-
}
60-
}
61-
62-
void step(val_t* to, val_t** to_curr, val_t** to_next, int count, int* t_back) {
63-
type_t t;
64-
int i;
65-
int size;
66-
for (i = 0; i < count; i++) {
67-
t = val_typeof(**to_curr);
68-
switch (t) {
69-
case T_BOX:
70-
case T_CONS:
71-
case T_VECT:
72-
if (val_unwrap(*val_unwrap(**to_curr)) >= to &&
73-
val_unwrap(*val_unwrap(**to_curr)) < to + heap_size) {
74-
// this is a fwd pointer (points in to to-space), so just set
75-
// curr to what it points to.
76-
**to_curr = *val_unwrap(**to_curr);
77-
*to_curr = *to_curr + 1;
78-
} else {
79-
// not a fwd pointer, copy to to_next
80-
size = val_size(val_unwrap(**to_curr), t);
81-
types[*t_back] = t; // enqueue type
82-
*t_back = *t_back + 1;
83-
memcpy(*to_next, val_unwrap(**to_curr), 8 * size); // copy
84-
*val_unwrap(**to_curr) = val_wrap(*to_next, t); // fwd
85-
**to_curr = val_wrap(*to_next, t); // update
86-
*to_next = *to_next + size;
87-
*to_curr = *to_curr + 1;
88-
};
89-
break;
90-
case T_STR:
91-
printf("STRING STEP\n");
92-
break;
93-
default:
94-
*to_curr = *to_curr + 1;
95-
};
96-
};
97-
}
98-
99-
100-
int64_t* collect_garbage(int64_t* rsp, int64_t *rbp, int64_t* rbx) {
101-
int stack_count = rbp - rsp;
102-
103-
val_t *from = heap + ((rbx < (heap + heap_size)) ? 0 : heap_size);
104-
val_t *to = heap + ((rbx < (heap + heap_size)) ? heap_size : 0);
105-
106-
val_t *to_next = to;
107-
val_t *to_curr = to;
108-
109-
int t_back = 0;
110-
int t_front = 0;
111-
112-
// Step through everything on the stack
113-
val_t * rsp_curr = rsp;
114-
step(to, &rsp_curr, &to_next, stack_count, &t_back);
115-
116-
int vi;
117-
// now play catch up between to_curr and to_next
118-
while (to_curr != to_next) {
119-
switch (types[t_front++]) {
120-
case T_VECT:
121-
vi = to_curr[0];
122-
to_curr++;
123-
step(to, &to_curr, &to_next, vi, &t_back);
124-
break;
125-
case T_BOX:
126-
step(to, &to_curr, &to_next, 1, &t_back);
127-
break;
128-
case T_CONS:
129-
step(to, &to_curr, &to_next, 2, &t_back);
130-
break;
131-
case T_STR:
132-
printf("STRING!!!\n");
133-
printf("size: %lld\n", 1 + ((*to_curr + 1) / 2));
134-
to_curr = to_curr + 3;
135-
default:
136-
to_curr++;
137-
break;
138-
}
139-
}
140-
return to_next;
141-
}
142-
143-
144-
void print_memory(int64_t* rsp, int64_t* rbp, int64_t* rbx) {
145-
146-
val_t* h = heap + ((rbx < (heap + heap_size)) ? 0 : heap_size);
147-
148-
int stack_count = rbp - rsp;
149-
int heap_count = rbx - h;
150-
151-
printf("----------------------------------------------------------------\n");
152-
int i;
153-
154-
printf("STACK:\n");
155-
for (i = 0; i < stack_count; i++) {
156-
printf("[%" PRIx64 "] = %016" PRIx64 ", %s\n", (int64_t)rsp + 8*i, rsp[i], val_typeof_string(rsp[i]));
157-
}
158-
printf("HEAP:\n");
159-
for (i = 0; i < heap_count; i++) {
160-
printf("[%" PRIx64 "] = %016" PRIx64 ", %s\n", (int64_t)h + 8*i, h[i], val_typeof_string(h[i]));
161-
}
162-
}

langs/iniquity-gc/runtime.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ extern void (*error_handler)();
88
// in words
99
#define heap_size 10000
1010
extern int64_t *heap;
11+
extern type_t *types;
1112
#endif /* RUNTIME_H */

0 commit comments

Comments
 (0)