-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathruntime.zest
More file actions
216 lines (199 loc) · 6.9 KB
/
Copy pathruntime.zest
File metadata and controls
216 lines (199 loc) · 6.9 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
string-innards = struct[
ptr: u32,
len: u32,
]
print = (x) {
if %only(%repr-of(x) == u32) {
%print(x)
%print('/u32')
} else if %only(%repr-of(x) == i64) {
%print(x)
} else if %only(%repr-of(x) == string) {
%print('\'')
// TODO escape
%print(x)
%print('\'')
} else if %only(%repr-of(x) == any) {
// TODO Need to be able to jump back into interpreter for unknown types.
%print('TODO/any')
} else {
unmade = %only(%unmake(%repr-of(x)))
head = %only(%from-only(unmade).0)
args = %only(%from-only(unmade).1)
if %only(%from-only(head) == struct) {
%print('[')
positional mut = 1
%each(x, (ix, key, value) {
if %only(%from-only(ix) > 0) %print(', ')
if %only(%from-only(key) != %from-only(ix)) { positional@ = 0 }
if {positional == 0} {
print-key(%from-only(key))
%print(': ')
}
print(value)
})
%print(']')
} else if %only(%from-only(head) == union) {
%print('[')
%each(x, (ix, key, value) {
if %only(%from-only(key) != 0) {
print-key(%from-only(key))
%print(': ')
}
print(value)
})
%print(']/')
print-repr(%only(%repr-of(x)))
} else if %only(%from-only(head) == list) {
%print('[TODO]/')
print-repr(%only(%repr-of(x)))
} else if %only(%from-only(head) == fun) {
print(%closure(x))
%print('/')
print-repr(%only(%repr-of(x)))
} else if %only(%from-only(head) == namespace) {
print(%closure(x))
%print('/')
print-repr(%only(%repr-of(x)))
} else if %only(%from-only(head) == only) {
%print('%only(')
print(%from-only(x))
%print(')')
} else {
%print('TODO/')
print-repr(%only(%repr-of(x)))
}
}
}
print-repr = (x) {
if %only(%from-only(x) == u32) {
%print('u32')
} else if %only(%from-only(x) == i64) {
%print('i64')
} else if %only(%from-only(x) == string) {
%print('string')
} else if %only(%from-only(x) == any) {
%print('any')
} else if %only(%from-only(x) == repr) {
%print('repr')
} else if %only(%repr-of(x) == repr-kind) {
print-repr-kind(%only(x))
} else {
unmade = %only(%unmake(%from-only(x)))
head = %only(%from-only(unmade).0)
args = %only(%from-only(unmade).1)
print-repr-kind(head)
%print('[')
positional mut = 1
%each(args, (ix, key, value) {
if %only(%from-only(ix) > 0) %print(', ')
if %only(%from-only(key) != %from-only(ix)) { positional@ = 0 }
if {positional == 0} {
print-key(%from-only(key))
%print(': ')
}
if %only(%repr-of(%from-only(value)) == repr) {
print-repr(value)
} else {
print(%from-only(value))
}
})
%print(']')
}
}
print-repr-kind = (x) {
if %only(%from-only(x) == struct) {
%print('struct')
} else if %only(%from-only(x) == union) {
%print('union')
} else if %only(%from-only(x) == list) {
%print('list')
} else if %only(%from-only(x) == fun) {
%print('fun')
} else if %only(%from-only(x) == namespace) {
%print('namespace')
} else if %only(%from-only(x) == only) {
%print('only')
} else {
panic('Unreachable')
}
}
print-key = (key) {
// TODO If key is a valid name then omit the quotes.
print(key)
}
println = (x) {
print(x)
%print('\n')
}
println-main = () {
println(%main())
}
panic = (message) {
print(message)
%panic()
}
wasm-page-len-log = u32[16] // log2(64 * 1024)
wasm-page-len = u32[1] << wasm-page-len-log
// Allocations are divided into `class-count` size classes, with sizes ranging from `2 ^ class-min-len-log` to `2 ^ class-max-len-log` bytes.
class-min-len-log = u32[2] // smallest size class that can fit a freelist pointer
class-max-len-log = u32[32] // enough for entire wasm32 address space
class-count = class-max-len-log - class-min-len-log
class-small-count = wasm-page-len-log - class-min-len-log
// Each wasm page is dedicated to one size class and is never reused in a different size class.
// For each size class we maintain a freelist.
// A pointer to 0 indicates that there are no free slots.
alloc-free-ptr-ptr = %heap-start() - class-count
// For size classes smaller than `wasm-page-len` we allocate an entire page and track the next free slot within the page.
// If the next slot points to the start of a wasm page then we need to allocate a new page.
alloc-next-ptr-ptr = alloc-free-ptr-ptr - class-small-count
alloc-pages = (page-count/u32) /u32 {
page-count-prev = %memory-grow(page-count)
// %memory-grow returns i32[-1] on oom
if {page-count-prev == u32[4294967295]} {
panic('Out of memory')
u32[0] // TODO Fix type inference for functions that don't return.
} else {
page-count-prev * wasm-page-len
}
}
alloc = (:class/u32) /u32 {
// if {class >= class-count} panic('Class is too large')
free-ptr-ptr = alloc-free-ptr-ptr + {class * %size-of(u32)}
free-ptr = %load(free-ptr-ptr, u32)
if {free-ptr != u32[0]} {
%store(free-ptr-ptr, %load(free-ptr, u32))
free-ptr
} else if {class < class-small-count} {
next-ptr-ptr = alloc-next-ptr-ptr + {class * %size-of(u32)}
next-ptr = %load(next-ptr-ptr, u32)
alloc-ptr = if {{next-ptr % wasm-page-len} == u32[0]} alloc-pages(1) else next-ptr
len-log = class + class-min-len-log
len = u32[1] << len-log
%store(next-ptr-ptr, alloc-ptr + len)
alloc-ptr
} else {
page-count = u32[1] << {class - class-small-count}
alloc-pages(page-count)
}
}
free = (:class/u32, :ptr/u32) /struct[] {
// if {class >= class-count} panic('Class is too large')
free-ptr-ptr = alloc-free-ptr-ptr + {class * %size-of(u32)}
free-ptr = %load(free-ptr-ptr, u32)
%store(ptr, free-ptr)
%store(free-ptr-ptr, ptr)
len-log = class + class-min-len-log
len = u32[1] << len-log
%memory-fill(ptr + %size-of(u32), u32[0], len - %size-of(u32))
}
len-to-class = (len/u32) /u32 {
// if {len == u32[0]} panic('Cannot alloc zero bytes')
lower-len-log = u32[31] - %clz(len)
upper-len-log = if {{u32[1] << lower-len-log} == len} lower-len-log else lower-len-log + u32[1]
if {upper-len-log > class-min-len-log} upper-len-log - class-min-len-log else u32[0]
}
class-to-len = (class/u32) /u32 {
if {class >= class-count} panic('Class is too large')
u32[1] << {class + class-min-len-log}
}