-
Notifications
You must be signed in to change notification settings - Fork 0
/
compile.scm
215 lines (201 loc) · 6.45 KB
/
compile.scm
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
(use-modules (ice-9 match)
(ice-9 binary-ports)
(ice-9 format)
(rnrs bytevectors)
((rnrs io ports) #:select (call-with-bytevector-output-port))
(srfi srfi-1)
(ice-9 pretty-print))
(define (compile port)
(define types '())
(define funcs '())
(define exports '())
(define (intern-type! params results)
(define type `(type (param ,@params)
(result ,@results)))
(match (member type types)
((_ . tail) (length tail))
(#f
(let ((idx (length types)))
(set! types (cons type types))
idx))))
(define (arg-type _) 'i32)
(define (lookup-func name)
(match funcs
((('func name* . _) . funcs)
(if (eq? name name*)
(length funcs)
(lp funcs)))))
(define (add-func! name typeidx body)
(set! funcs (cons `(func ,name ,typeidx ,body) funcs)))
(define (add-export! name id)
(set! exports (cons `(export ,name (func ,(lookup-func id))) exports)))
(define (compile-def def)
(define (args->env args) (reverse args))
(match def
(('define (f arg ...) exp)
(add-func! f (intern-type! (map arg-type arg) '(i32))
(lambda () (compile-exp exp (args->env arg))))
(add-export! (symbol->string f) f))))
(define (compile-func func)
(match func
(('func name type compile-body)
`(func ,type (local) ,@(compile-body)))))
(define (compile-exp exp env)
(let compile ((exp exp))
(match exp
((? symbol? id)
(match (memq id env)
((_ . tail) `((local.get ,(length tail))))))
((? exact-integer? n)
`((i32.const ,n)))
(('if test then else)
`((block i32
(block ,@(compile test)
(br_if 0)
,@(compile else)
(br 1))
,@(compile then))))
(('zero? exp)
`(,@(compile exp)
(i32.eqz)))
(('- a b)
`(,@(compile a)
,@(compile b)
(i32.sub)))
(('* a b)
`(,@(compile a)
,@(compile b)
(i32.mul)))
((f arg ...)
(let ((idx (lookup-func f)))
`(,@(append-map compile arg)
(call ,idx)))))))
(let lp ()
(let ((datum (read port)))
(unless (eof-object? datum)
(compile-def datum)
(lp))))
(let ((funcs (map compile-func funcs)))
(let ((types (reverse types))
(funcs (reverse funcs))
(exports (reverse exports)))
(format #t "Assembling:\n~y\n" `(module ,@types ,@funcs ,@exports))
(assemble types funcs exports))))
(define (assemble types funcs exports)
(define (put-sleb out n)
(if (let ((n* (+ n 64)))
(eq? n* (logand n* #x7f)))
(put-u8 out (logand n #x7f))
(begin
(put-u8 out (logior #x80 (logand n #x7f)))
(put-sleb out (ash n -7)))))
(define (put-uleb out n)
(let ((next (ash n -7)))
(if (zero? next)
(put-u8 out n)
(begin
(put-u8 out (logior #x80 (logand n #x7f)))
(put-uleb out next)))))
(define (put-bytes out bytes)
(put-uleb out (bytevector-length bytes))
(put-bytevector out bytes))
(define (put-section out id contents)
(put-u8 out id)
(put-bytes out contents))
(define (put-vector out f elts)
(put-uleb out (length elts))
(for-each (lambda (elt) (f out elt)) elts))
(define (put-vector-section out id f elts)
(unless (null? elts)
(put-section out id
(call-with-bytevector-output-port
(lambda (out)
(put-vector out f elts))))))
(define (put-type out type)
(match type
('i32 (put-u8 out #x7f))
('i64 (put-u8 out #x7e))
('f32 (put-u8 out #x7d))
('f64 (put-u8 out #x7c))
(('type ('param p ...) ('result r ...))
(put-u8 out #x60)
(put-vector out put-type p)
(put-vector out put-type r))))
(define (put-func out func)
(match func
(('func typeidx . code)
(put-uleb out typeidx))))
(define (put-name out name)
(put-bytes out (string->utf8 name)))
(define (put-export out export)
(match export
(('export name ('func idx))
(put-name out name)
(put-u8 out #x00)
(put-uleb out idx))))
(define (put-block-body out body)
(match body
(((? symbol? type) body ...)
(put-type out type)
(put-expr out body))
((('type typeidx) body ...)
(put-sleb out typeidx)
(put-expr out body))
(body
(put-u8 out #x40)
(put-expr out body))))
(define (put-locals out locals)
(match locals ; Only implemented for no locals.
(() (put-vector out error '()))))
(define (put-instruction out instr)
(match instr
(('block body ...)
(put-u8 out #x02)
(put-block-body out body))
(('br_if labelidx)
(put-u8 out #x0d)
(put-uleb out labelidx))
(('br labelidx)
(put-u8 out #x0c)
(put-uleb out labelidx))
(('i32.eqz) (put-u8 out #x45))
(('i32.sub) (put-u8 out #x6b))
(('i32.mul) (put-u8 out #x6c))
(('i32.const c)
(put-u8 out #x41)
(put-sleb out c))
(('return)
(put-u8 out #x0f))
(('local.get idx)
(put-u8 out #x20)
(put-uleb out idx))
(('call idx)
(put-u8 out #x10)
(put-uleb out idx))))
(define (put-expr out instrs)
(for-each (lambda (instr) (put-instruction out instr)) instrs)
(put-u8 out #x0b))
(define (put-code out func)
(match func
(('func typeidx ('local t ...) . code)
(put-bytes out
(call-with-bytevector-output-port
(lambda (out)
(put-locals out t)
(put-expr out code)))))))
(call-with-bytevector-output-port
(lambda (out)
(put-bytevector out #vu8(#x00 #x61 #x73 #x6D)) ; magic
(put-bytevector out #vu8(#x01 #x00 #x00 #x00)) ; version
(put-vector-section out 1 put-type types)
(put-vector-section out 3 put-func funcs)
(put-vector-section out 7 put-export exports)
(put-vector-section out 10 put-code funcs))))
(when (batch-mode?)
(match (command-line)
((_ in out)
(let ((bv (call-with-input-file in compile)))
(call-with-output-file out (lambda (port) (put-bytevector port bv)))))
((prog . _)
(format (current-error-port) "usage: ~a in.scm out.wasm\n" prog)
(exit 1))))