-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path1.9.runtime.c
179 lines (163 loc) · 4.28 KB
/
1.9.runtime.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
#include <stdio.h>
#include <stdlib.h> // for exit
#include <sys/mman.h>
#ifndef FALSE
#define FALSE (0)
#define TRUE (1)
#endif
/* define all scheme constatns */
#define tag_mask 0x07
#define other_tag 0x07
#define bool_f 0x2f
#define bool_t 0x6f
#define fx_mask 0x03
#define fx_tag 0x00
#define fx_shift 2
#define nullval 0x3f
#define char_mask 0xff
#define char_shift 8
#define char_tag 0x0f
#define pair_tag 0x01
#define vector_tag 0x05
#define string_tag 0x06
/* all scheme values are of type ptrs */
typedef unsigned int ptr;
typedef struct {
void* eax; /* 0 scratch */
void* ebx; /* 4 preserve */
void* ecx; /* 8 scratch */
void* edx; /* 12 scratch */
void* esi; /* 16 preserve */
void* edi; /* 20 preserve */
void* ebp; /* 24 preserve */
void* esp; /* 28 preserve */
} context;
extern int scheme_entry(context* ctxt, void* stack, void* heap);
typedef struct {
ptr car;
ptr cdr;
} cell;
typedef struct {
unsigned int num;
ptr buf[1]; // num
} vector;
typedef struct {
unsigned int num;
char buf[1]; // num
} string;
static __inline ptr CAR(ptr x) { return ((cell*)(x-pair_tag))->car; }
static __inline ptr CDR(ptr x) { return ((cell*)(x-pair_tag))->cdr; }
static __inline int consp(ptr x) { return (x & tag_mask) == pair_tag; }
static __inline int nullp(ptr x) { return x == nullval; }
static void print_ptr_sub(ptr x) {
switch (x & tag_mask) {
default:
break;
case fx_tag: case fx_tag + 4:
printf("%d", ((int)x) >> fx_shift);
return;
case other_tag:
if ((x & char_mask) == char_tag) {
int c = x >> char_shift;
switch (c) {
default: printf("#\\%c", c); break;
case '\t': printf("#\\tab"); break;
case '\n': printf("#\\newline"); break;
case '\r': printf("#\\return"); break;
case ' ': printf("#\\space"); break;
}
return;
} else {
switch (x) {
default: break;
case bool_f: printf("#f"); return;
case bool_t: printf("#t"); return;
case nullval: printf("()"); return;
}
}
case pair_tag:
{
int first = TRUE;
printf("(");
for (; consp(x); x = CDR(x)) {
if (!first) printf(" ");
print_ptr_sub(CAR(x));
first = FALSE;
}
if (!nullp(x)) {
printf(" . ");
print_ptr_sub(x);
}
printf(")");
}
return;
case vector_tag:
{
vector* p = (vector*)(x - vector_tag);
int first = TRUE;
unsigned int n = p->num >> fx_shift;
unsigned int i;
printf("#(");
for (i=0; i<n; ++i) {
if (!first) printf(" ");
first = FALSE;
print_ptr_sub(p->buf[i]);
}
printf(")");
}
return;
case string_tag:
{
string* p = (string*)(x - string_tag);
unsigned int n = p->num;
unsigned int i;
printf("\"");
for (i=0; i<n; ++i) {
int c = p->buf[i];
switch (c) {
default: putchar(c); break;
case '"': fputs("\\\"", stdout); break;
case '\\': fputs("\\\\", stdout); break;
}
}
printf("\"");
}
return;
}
printf("#<unknown #0x%08x>", x);
}
static void print_ptr(ptr x) {
print_ptr_sub(x);
printf("\n");
}
static char* allocate_protected_space(int size) {
int page = getpagesize();
int status;
int aligned_size = ((size + page - 1) / page) * page;
char* p = mmap(0, aligned_size + 2 * page, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
if (p == MAP_FAILED) { exit(1); }
status = mprotect(p, page, PROT_NONE);
if (status != 0) { exit(1); }
status = mprotect(p + page + aligned_size, page, PROT_NONE);
if (status != 0) { exit(1); }
return p + page;
}
static void deallocate_protected_space(char* p, int size) {
int page = getpagesize();
int status;
int aligned_size = ((size + page - 1) / page) * page;
status = munmap(p - page, aligned_size + 2 * page);
if (status != 0) { exit(1); }
}
int main(int argc, char** argv) {
int stack_size = (16 * 4096); /* holds 16K cells */
char* stack_top = allocate_protected_space(stack_size);
char* stack_base = stack_top + stack_size;
int heap_size = (16 * 4096); /* holds 16K cells */
char* heap = allocate_protected_space(heap_size);
context ctxt;
print_ptr(scheme_entry(&ctxt, stack_base, heap));
deallocate_protected_space(heap, heap_size);
deallocate_protected_space(stack_top, stack_size);
return 0;
}