-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathruntime.h
150 lines (125 loc) · 3.08 KB
/
runtime.h
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
#ifndef _RUNTIME_H_
#define _RUNTIME_H_
#ifdef __cplusplus
extern "C" {
#endif
#include <assert.h>
#include <stdint.h>
void *managed_alloc(size_t size);
typedef struct {
union {
uint8_t tag : 3;
void *vtable;
};
int64_t size;
// payload
} heapval_t;
#define VAL_INT_TAG (1 << 0)
#define MARKED_TAG (1 << 0)
#define VAL_ARRAY_TAG (1 << 1)
#define VAL_FRAME_TAG (1 << 2)
static inline int64_t *pith_field(heapval_t *val, size_t idx)
{
return (int64_t *)((uint8_t *)val + sizeof(heapval_t) + idx * sizeof(int64_t));
}
static inline int64_t *pith_field_arr(heapval_t *val, size_t idx)
{
return (int64_t *)((uint8_t *)val->vtable + idx * sizeof(int64_t));
}
static inline int64_t ptr_to_int(void *ptr)
{
int64_t iptr = (int64_t)ptr;
assert(iptr & VAL_INT_TAG);
return (iptr >> 1);
}
static inline void *int_to_ptr(int64_t i)
{
return (void *)((i << 1) | VAL_INT_TAG);
}
static inline heapval_t *ptr_to_hval(void *ptr)
{
assert(!((int64_t)ptr & VAL_INT_TAG));
return (heapval_t *)((int64_t)ptr);
}
heapval_t *alloc_heapval(void *vtable, size_t size);
heapval_t *alloc_arr(size_t size);
typedef struct {
void **buffer;
size_t size;
size_t capacity;
} vector_t;
static inline void vector_init(vector_t *vec, size_t capacity)
{
vec->buffer = (void **)malloc(capacity * sizeof(void *));
if (vec->buffer == NULL) {
fprintf(stderr, "vector_init: malloc failed\n");
exit(1);
}
vec->size = 0;
vec->capacity = capacity;
}
static inline void vector_push(vector_t *vec, void *elem)
{
if (vec->size == vec->capacity) {
vec->capacity *= 2;
vec->buffer = (void **)realloc(vec->buffer, vec->capacity * sizeof(void *));
if (vec->buffer == NULL) {
fprintf(stderr, "vector_push: realloc failed\n");
exit(1);
}
}
vec->buffer[vec->size++] = elem;
}
static inline void *vector_pop(vector_t *vec)
{
if (vec->size == 0) {
fprintf(stderr, "vector_pop: empty vector\n");
exit(1);
}
return vec->buffer[--vec->size];
}
static inline void vector_set(vector_t *vec, size_t idx, void *elem)
{
if (idx >= vec->size) {
fprintf(stderr, "vector_set: index out of bounds\n");
exit(1);
}
vec->buffer[idx] = elem;
}
static inline void *vector_get(vector_t *vec, size_t idx)
{
if (idx >= vec->size) {
fprintf(stderr, "vector_get: index out of bounds\n");
exit(1);
}
return vec->buffer[idx];
}
static inline void *vector_top(vector_t *vec)
{
if (vec->size == 0) {
fprintf(stderr, "vector_top: empty vector\n");
exit(1);
}
return vec->buffer[vec->size - 1];
}
static inline void vector_destroy(vector_t *vec)
{
free(vec->buffer);
vec->buffer = NULL;
vec->size = 0;
vec->capacity = 0;
}
typedef struct {
vector_t val_stack;
vector_t locals;
void *ip_start;
void *ip;
} frame_t;
frame_t *frame_create(void);
void frame_destroy(frame_t *frame);
extern vector_t frames;
extern frame_t *fp;
#ifdef __cplusplus
} // extern "C"
#endif
#endif // _RUNTIME_H_