Commit | Line | Data |
---|---|---|
6e8ad823 AW |
1 | ;;; Continuation-passing style (CPS) intermediate language (IL) |
2 | ||
7ab76a83 | 3 | ;; Copyright (C) 2013, 2014 Free Software Foundation, Inc. |
6e8ad823 AW |
4 | |
5 | ;;;; This library is free software; you can redistribute it and/or | |
6 | ;;;; modify it under the terms of the GNU Lesser General Public | |
7 | ;;;; License as published by the Free Software Foundation; either | |
8 | ;;;; version 3 of the License, or (at your option) any later version. | |
9 | ;;;; | |
10 | ;;;; This library is distributed in the hope that it will be useful, | |
11 | ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | ;;;; Lesser General Public License for more details. | |
14 | ;;;; | |
15 | ;;;; You should have received a copy of the GNU Lesser General Public | |
16 | ;;;; License along with this library; if not, write to the Free Software | |
17 | ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
18 | ||
19 | ;;; Commentary: | |
20 | ;;; | |
691697de AW |
21 | ;;; Compiling CPS to bytecode. The result is in the bytecode language, |
22 | ;;; which happens to be an ELF image as a bytecode. | |
6e8ad823 AW |
23 | ;;; |
24 | ;;; Code: | |
25 | ||
691697de | 26 | (define-module (language cps compile-bytecode) |
6e8ad823 AW |
27 | #:use-module (ice-9 match) |
28 | #:use-module (srfi srfi-1) | |
29 | #:use-module (language cps) | |
30 | #:use-module (language cps arities) | |
31 | #:use-module (language cps closure-conversion) | |
8ac8e2df | 32 | #:use-module (language cps contification) |
fa3b6e57 | 33 | #:use-module (language cps constructors) |
7a08e479 | 34 | #:use-module (language cps cse) |
305cccb4 | 35 | #:use-module (language cps dce) |
6e8ad823 | 36 | #:use-module (language cps dfg) |
7e273b7a | 37 | #:use-module (language cps elide-values) |
6e8ad823 | 38 | #:use-module (language cps primitives) |
63463880 | 39 | #:use-module (language cps prune-bailouts) |
dd692618 | 40 | #:use-module (language cps prune-top-level-scopes) |
6e8ad823 | 41 | #:use-module (language cps reify-primitives) |
62b7180b | 42 | #:use-module (language cps renumber) |
c4a209b9 | 43 | #:use-module (language cps self-references) |
22a79b55 | 44 | #:use-module (language cps simplify) |
6e8ad823 | 45 | #:use-module (language cps slot-allocation) |
4c906ad5 | 46 | #:use-module (language cps specialize-primcalls) |
8bc65d2d | 47 | #:use-module (language cps type-fold) |
6e8ad823 | 48 | #:use-module (system vm assembler) |
691697de | 49 | #:export (compile-bytecode)) |
6e8ad823 | 50 | |
d258fccc | 51 | ;; TODO: Local var names. |
6e8ad823 AW |
52 | |
53 | (define (kw-arg-ref args kw default) | |
54 | (match (memq kw args) | |
55 | ((_ val . _) val) | |
56 | (_ default))) | |
57 | ||
58 | (define (optimize exp opts) | |
59 | (define (run-pass exp pass kw default) | |
60 | (if (kw-arg-ref opts kw default) | |
1d15832f | 61 | (pass exp) |
6e8ad823 AW |
62 | exp)) |
63 | ||
305cccb4 AW |
64 | ;; The first DCE pass is mainly to eliminate functions that aren't |
65 | ;; called. The last is mainly to eliminate rest parameters that | |
66 | ;; aren't used, and thus shouldn't be consed. | |
67 | ||
9e94cd9b | 68 | (let* ((exp (run-pass exp eliminate-dead-code #:eliminate-dead-code? #t)) |
dd692618 | 69 | (exp (run-pass exp prune-top-level-scopes #:prune-top-level-scopes? #t)) |
22a79b55 | 70 | (exp (run-pass exp simplify #:simplify? #t)) |
305cccb4 | 71 | (exp (run-pass exp contify #:contify? #t)) |
7e273b7a | 72 | (exp (run-pass exp inline-constructors #:inline-constructors? #t)) |
4c906ad5 | 73 | (exp (run-pass exp specialize-primcalls #:specialize-primcalls? #t)) |
305cccb4 | 74 | (exp (run-pass exp elide-values #:elide-values? #t)) |
9e94cd9b | 75 | (exp (run-pass exp prune-bailouts #:prune-bailouts? #t)) |
85270a8c | 76 | (exp (run-pass exp eliminate-common-subexpressions #:cse? #t)) |
6129faa0 | 77 | (exp (run-pass exp type-fold #:type-fold? #t)) |
c4a209b9 | 78 | (exp (run-pass exp resolve-self-references #:resolve-self-references? #t)) |
22a79b55 AW |
79 | (exp (run-pass exp eliminate-dead-code #:eliminate-dead-code? #t)) |
80 | (exp (run-pass exp simplify #:simplify? #t))) | |
6e8ad823 AW |
81 | ;; Passes that are needed: |
82 | ;; | |
6e8ad823 AW |
83 | ;; * Abort contification: turning abort primcalls into continuation |
84 | ;; calls, and eliding prompts if possible. | |
85 | ;; | |
6e8ad823 AW |
86 | ;; * Loop peeling. Unrolls the first round through a loop if the |
87 | ;; loop has effects that CSE can work on. Requires effects | |
88 | ;; analysis. When run before CSE, loop peeling is the equivalent | |
89 | ;; of loop-invariant code motion (LICM). | |
6e8ad823 AW |
90 | |
91 | exp)) | |
92 | ||
d258fccc | 93 | (define (compile-fun f asm) |
6bc36ca5 | 94 | (let* ((dfg (compute-dfg f #:global? #f)) |
a8430ab1 | 95 | (allocation (allocate-slots f dfg))) |
987c1f5f AW |
96 | (define (maybe-slot sym) |
97 | (lookup-maybe-slot sym allocation)) | |
98 | ||
6e422a35 AW |
99 | (define (slot sym) |
100 | (lookup-slot sym allocation)) | |
101 | ||
102 | (define (constant sym) | |
103 | (lookup-constant-value sym allocation)) | |
104 | ||
105 | (define (maybe-mov dst src) | |
106 | (unless (= dst src) | |
107 | (emit-mov asm dst src))) | |
108 | ||
109 | (define (maybe-load-constant slot src) | |
110 | (call-with-values (lambda () | |
111 | (lookup-maybe-constant-value src allocation)) | |
112 | (lambda (has-const? val) | |
113 | (and has-const? | |
114 | (begin | |
115 | (emit-load-constant asm slot val) | |
116 | #t))))) | |
117 | ||
24b611e8 | 118 | (define (compile-entry) |
a8430ab1 AW |
119 | (let ((label (dfg-min-label dfg))) |
120 | (match (lookup-cont label dfg) | |
8320f504 | 121 | (($ $kfun src meta self tail clause) |
24b611e8 AW |
122 | (when src |
123 | (emit-source asm src)) | |
a8430ab1 AW |
124 | (emit-begin-program asm label meta) |
125 | (compile-clause (1+ label)) | |
126 | (emit-end-program asm))))) | |
90dce16d | 127 | |
a8430ab1 AW |
128 | (define (compile-clause label) |
129 | (match (lookup-cont label dfg) | |
90dce16d AW |
130 | (($ $kclause ($ $arity req opt rest kw allow-other-keys?) |
131 | body alternate) | |
6e422a35 AW |
132 | (let* ((kw-indices (map (match-lambda |
133 | ((key name sym) | |
134 | (cons key (lookup-slot sym allocation)))) | |
135 | kw)) | |
a8430ab1 AW |
136 | (nlocals (lookup-nlocals label allocation))) |
137 | (emit-label asm label) | |
90dce16d AW |
138 | (emit-begin-kw-arity asm req opt rest kw-indices allow-other-keys? |
139 | nlocals | |
140 | (match alternate (#f #f) (($ $cont alt) alt))) | |
a8430ab1 | 141 | (let ((next (compile-body (1+ label) nlocals))) |
6e422a35 | 142 | (emit-end-arity asm) |
90dce16d AW |
143 | (match alternate |
144 | (($ $cont alt) | |
a8430ab1 | 145 | (unless (eq? next alt) |
90dce16d AW |
146 | (error "unexpected k" alt)) |
147 | (compile-clause next)) | |
148 | (#f | |
a8430ab1 | 149 | (unless (= next (+ (dfg-min-label dfg) (dfg-label-count dfg))) |
90dce16d | 150 | (error "unexpected end of clauses"))))))))) |
6e422a35 | 151 | |
a8430ab1 AW |
152 | (define (compile-body label nlocals) |
153 | (let compile-cont ((label label)) | |
154 | (if (eq? label (+ (dfg-min-label dfg) (dfg-label-count dfg))) | |
155 | label | |
156 | (match (lookup-cont label dfg) | |
157 | (($ $kclause) label) | |
78351d10 | 158 | (($ $kargs names vars term) |
a8430ab1 | 159 | (emit-label asm label) |
78351d10 AW |
160 | (for-each (lambda (name var) |
161 | (let ((slot (maybe-slot var))) | |
162 | (when slot | |
163 | (emit-definition asm name slot)))) | |
164 | names vars) | |
6e422a35 AW |
165 | (let find-exp ((term term)) |
166 | (match term | |
167 | (($ $letk conts term) | |
168 | (find-exp term)) | |
169 | (($ $continue k src exp) | |
170 | (when src | |
171 | (emit-source asm src)) | |
a8430ab1 AW |
172 | (compile-expression label k exp nlocals) |
173 | (compile-cont (1+ label)))))) | |
6e422a35 | 174 | (_ |
a8430ab1 AW |
175 | (emit-label asm label) |
176 | (compile-cont (1+ label))))))) | |
6e422a35 | 177 | |
a8430ab1 AW |
178 | (define (compile-expression label k exp nlocals) |
179 | (let* ((fallthrough? (= k (1+ label)))) | |
6e422a35 | 180 | (define (maybe-emit-jump) |
a8430ab1 | 181 | (unless fallthrough? |
6e422a35 | 182 | (emit-br asm k))) |
a8430ab1 | 183 | (match (lookup-cont k dfg) |
6e422a35 AW |
184 | (($ $ktail) |
185 | (compile-tail label exp)) | |
186 | (($ $kargs (name) (sym)) | |
987c1f5f | 187 | (let ((dst (maybe-slot sym))) |
6e422a35 AW |
188 | (when dst |
189 | (compile-value label exp dst nlocals))) | |
190 | (maybe-emit-jump)) | |
191 | (($ $kargs () ()) | |
92805e21 AW |
192 | (match exp |
193 | (($ $branch kt exp) | |
194 | (compile-test label exp kt k (1+ label))) | |
195 | (_ | |
196 | (compile-effect label exp k nlocals) | |
197 | (maybe-emit-jump)))) | |
6e422a35 AW |
198 | (($ $kargs names syms) |
199 | (compile-values label exp syms) | |
200 | (maybe-emit-jump)) | |
36527695 | 201 | (($ $kreceive ($ $arity req () rest () #f) kargs) |
fa48a2f7 AW |
202 | (compile-trunc label k exp (length req) |
203 | (and rest | |
a8430ab1 | 204 | (match (lookup-cont kargs dfg) |
fa48a2f7 AW |
205 | (($ $kargs names (_ ... rest)) rest))) |
206 | nlocals) | |
a8430ab1 | 207 | (unless (and fallthrough? (= kargs (1+ k))) |
987c1f5f | 208 | (emit-br asm kargs)))))) |
6e422a35 AW |
209 | |
210 | (define (compile-tail label exp) | |
211 | ;; There are only three kinds of expressions in tail position: | |
212 | ;; tail calls, multiple-value returns, and single-value returns. | |
213 | (match exp | |
214 | (($ $call proc args) | |
215 | (for-each (match-lambda | |
216 | ((src . dst) (emit-mov asm dst src))) | |
217 | (lookup-parallel-moves label allocation)) | |
218 | (let ((tail-slots (cdr (iota (1+ (length args)))))) | |
219 | (for-each maybe-load-constant tail-slots args)) | |
220 | (emit-tail-call asm (1+ (length args)))) | |
b3ae2b50 AW |
221 | (($ $callk k proc args) |
222 | (for-each (match-lambda | |
223 | ((src . dst) (emit-mov asm dst src))) | |
224 | (lookup-parallel-moves label allocation)) | |
225 | (let ((tail-slots (cdr (iota (1+ (length args)))))) | |
226 | (for-each maybe-load-constant tail-slots args)) | |
227 | (emit-tail-call-label asm (1+ (length args)) k)) | |
987c1f5f AW |
228 | (($ $values ()) |
229 | (emit-reset-frame asm 1) | |
230 | (emit-return-values asm)) | |
13085a82 | 231 | (($ $values (arg)) |
987c1f5f | 232 | (if (maybe-slot arg) |
13085a82 AW |
233 | (emit-return asm (slot arg)) |
234 | (begin | |
235 | (emit-load-constant asm 1 (constant arg)) | |
236 | (emit-return asm 1)))) | |
6e422a35 | 237 | (($ $values args) |
13085a82 AW |
238 | (for-each (match-lambda |
239 | ((src . dst) (emit-mov asm dst src))) | |
240 | (lookup-parallel-moves label allocation)) | |
6e422a35 | 241 | (let ((tail-slots (cdr (iota (1+ (length args)))))) |
6e422a35 AW |
242 | (for-each maybe-load-constant tail-slots args)) |
243 | (emit-reset-frame asm (1+ (length args))) | |
244 | (emit-return-values asm)) | |
245 | (($ $primcall 'return (arg)) | |
246 | (emit-return asm (slot arg))))) | |
247 | ||
248 | (define (compile-value label exp dst nlocals) | |
249 | (match exp | |
6e422a35 AW |
250 | (($ $values (arg)) |
251 | (or (maybe-load-constant dst arg) | |
252 | (maybe-mov dst (slot arg)))) | |
253 | (($ $void) | |
254 | (emit-load-constant asm dst *unspecified*)) | |
255 | (($ $const exp) | |
256 | (emit-load-constant asm dst exp)) | |
cf8bb037 | 257 | (($ $closure k 0) |
6e422a35 | 258 | (emit-load-static-procedure asm dst k)) |
cf8bb037 AW |
259 | (($ $closure k nfree) |
260 | (emit-make-closure asm dst k nfree)) | |
6e422a35 AW |
261 | (($ $primcall 'current-module) |
262 | (emit-current-module asm dst)) | |
263 | (($ $primcall 'cached-toplevel-box (scope name bound?)) | |
264 | (emit-cached-toplevel-box asm dst (constant scope) (constant name) | |
265 | (constant bound?))) | |
266 | (($ $primcall 'cached-module-box (mod name public? bound?)) | |
267 | (emit-cached-module-box asm dst (constant mod) (constant name) | |
268 | (constant public?) (constant bound?))) | |
269 | (($ $primcall 'resolve (name bound?)) | |
270 | (emit-resolve asm dst (constant bound?) (slot name))) | |
271 | (($ $primcall 'free-ref (closure idx)) | |
272 | (emit-free-ref asm dst (slot closure) (constant idx))) | |
6e422a35 | 273 | (($ $primcall 'vector-ref (vector index)) |
4c906ad5 | 274 | (emit-vector-ref asm dst (slot vector) (slot index))) |
d38ca16e AW |
275 | (($ $primcall 'make-vector (length init)) |
276 | (emit-make-vector asm dst (slot length) (slot init))) | |
4c906ad5 AW |
277 | (($ $primcall 'make-vector/immediate (length init)) |
278 | (emit-make-vector/immediate asm dst (constant length) (slot init))) | |
279 | (($ $primcall 'vector-ref/immediate (vector index)) | |
280 | (emit-vector-ref/immediate asm dst (slot vector) (constant index))) | |
281 | (($ $primcall 'allocate-struct/immediate (vtable nfields)) | |
282 | (emit-allocate-struct/immediate asm dst (slot vtable) (constant nfields))) | |
283 | (($ $primcall 'struct-ref/immediate (struct n)) | |
284 | (emit-struct-ref/immediate asm dst (slot struct) (constant n))) | |
6e422a35 AW |
285 | (($ $primcall 'builtin-ref (name)) |
286 | (emit-builtin-ref asm dst (constant name))) | |
287 | (($ $primcall 'bv-u8-ref (bv idx)) | |
288 | (emit-bv-u8-ref asm dst (slot bv) (slot idx))) | |
d59060ce AW |
289 | (($ $primcall 'bv-s8-ref (bv idx)) |
290 | (emit-bv-s8-ref asm dst (slot bv) (slot idx))) | |
6e422a35 AW |
291 | (($ $primcall 'bv-u16-ref (bv idx)) |
292 | (emit-bv-u16-ref asm dst (slot bv) (slot idx))) | |
293 | (($ $primcall 'bv-s16-ref (bv idx)) | |
294 | (emit-bv-s16-ref asm dst (slot bv) (slot idx))) | |
295 | (($ $primcall 'bv-u32-ref (bv idx val)) | |
296 | (emit-bv-u32-ref asm dst (slot bv) (slot idx))) | |
297 | (($ $primcall 'bv-s32-ref (bv idx val)) | |
298 | (emit-bv-s32-ref asm dst (slot bv) (slot idx))) | |
299 | (($ $primcall 'bv-u64-ref (bv idx val)) | |
300 | (emit-bv-u64-ref asm dst (slot bv) (slot idx))) | |
301 | (($ $primcall 'bv-s64-ref (bv idx val)) | |
302 | (emit-bv-s64-ref asm dst (slot bv) (slot idx))) | |
303 | (($ $primcall 'bv-f32-ref (bv idx val)) | |
304 | (emit-bv-f32-ref asm dst (slot bv) (slot idx))) | |
305 | (($ $primcall 'bv-f64-ref (bv idx val)) | |
306 | (emit-bv-f64-ref asm dst (slot bv) (slot idx))) | |
307 | (($ $primcall name args) | |
308 | ;; FIXME: Inline all the cases. | |
691697de | 309 | (let ((inst (prim-instruction name))) |
6e422a35 AW |
310 | (emit-text asm `((,inst ,dst ,@(map slot args)))))))) |
311 | ||
312 | (define (compile-effect label exp k nlocals) | |
313 | (match exp | |
314 | (($ $values ()) #f) | |
7ab76a83 | 315 | (($ $prompt escape? tag handler) |
a8430ab1 | 316 | (match (lookup-cont handler dfg) |
36527695 | 317 | (($ $kreceive ($ $arity req () rest () #f) khandler-body) |
6e422a35 AW |
318 | (let ((receive-args (gensym "handler")) |
319 | (nreq (length req)) | |
987c1f5f | 320 | (proc-slot (lookup-call-proc-slot handler allocation))) |
6e422a35 AW |
321 | (emit-prompt asm (slot tag) escape? proc-slot receive-args) |
322 | (emit-br asm k) | |
323 | (emit-label asm receive-args) | |
4dfcb360 AW |
324 | (unless (and rest (zero? nreq)) |
325 | (emit-receive-values asm proc-slot (->bool rest) nreq)) | |
fa48a2f7 | 326 | (when (and rest |
a8430ab1 | 327 | (match (lookup-cont khandler-body dfg) |
fa48a2f7 AW |
328 | (($ $kargs names (_ ... rest)) |
329 | (maybe-slot rest)))) | |
6e422a35 AW |
330 | (emit-bind-rest asm (+ proc-slot 1 nreq))) |
331 | (for-each (match-lambda | |
332 | ((src . dst) (emit-mov asm dst src))) | |
333 | (lookup-parallel-moves handler allocation)) | |
334 | (emit-reset-frame asm nlocals) | |
335 | (emit-br asm khandler-body))))) | |
336 | (($ $primcall 'cache-current-module! (sym scope)) | |
337 | (emit-cache-current-module! asm (slot sym) (constant scope))) | |
338 | (($ $primcall 'free-set! (closure idx value)) | |
339 | (emit-free-set! asm (slot closure) (slot value) (constant idx))) | |
340 | (($ $primcall 'box-set! (box value)) | |
341 | (emit-box-set! asm (slot box) (slot value))) | |
4c906ad5 AW |
342 | (($ $primcall 'struct-set!/immediate (struct index value)) |
343 | (emit-struct-set!/immediate asm (slot struct) (constant index) (slot value))) | |
6e422a35 | 344 | (($ $primcall 'vector-set! (vector index value)) |
4c906ad5 AW |
345 | (emit-vector-set! asm (slot vector) (slot index) (slot value))) |
346 | (($ $primcall 'vector-set!/immediate (vector index value)) | |
347 | (emit-vector-set!/immediate asm (slot vector) (constant index) | |
348 | (slot value))) | |
6e422a35 AW |
349 | (($ $primcall 'set-car! (pair value)) |
350 | (emit-set-car! asm (slot pair) (slot value))) | |
351 | (($ $primcall 'set-cdr! (pair value)) | |
352 | (emit-set-cdr! asm (slot pair) (slot value))) | |
353 | (($ $primcall 'define! (sym value)) | |
354 | (emit-define! asm (slot sym) (slot value))) | |
355 | (($ $primcall 'push-fluid (fluid val)) | |
356 | (emit-push-fluid asm (slot fluid) (slot val))) | |
357 | (($ $primcall 'pop-fluid ()) | |
358 | (emit-pop-fluid asm)) | |
359 | (($ $primcall 'wind (winder unwinder)) | |
360 | (emit-wind asm (slot winder) (slot unwinder))) | |
361 | (($ $primcall 'bv-u8-set! (bv idx val)) | |
362 | (emit-bv-u8-set! asm (slot bv) (slot idx) (slot val))) | |
d59060ce AW |
363 | (($ $primcall 'bv-s8-set! (bv idx val)) |
364 | (emit-bv-s8-set! asm (slot bv) (slot idx) (slot val))) | |
6e422a35 AW |
365 | (($ $primcall 'bv-u16-set! (bv idx val)) |
366 | (emit-bv-u16-set! asm (slot bv) (slot idx) (slot val))) | |
367 | (($ $primcall 'bv-s16-set! (bv idx val)) | |
368 | (emit-bv-s16-set! asm (slot bv) (slot idx) (slot val))) | |
369 | (($ $primcall 'bv-u32-set! (bv idx val)) | |
370 | (emit-bv-u32-set! asm (slot bv) (slot idx) (slot val))) | |
371 | (($ $primcall 'bv-s32-set! (bv idx val)) | |
372 | (emit-bv-s32-set! asm (slot bv) (slot idx) (slot val))) | |
373 | (($ $primcall 'bv-u64-set! (bv idx val)) | |
374 | (emit-bv-u64-set! asm (slot bv) (slot idx) (slot val))) | |
375 | (($ $primcall 'bv-s64-set! (bv idx val)) | |
376 | (emit-bv-s64-set! asm (slot bv) (slot idx) (slot val))) | |
377 | (($ $primcall 'bv-f32-set! (bv idx val)) | |
378 | (emit-bv-f32-set! asm (slot bv) (slot idx) (slot val))) | |
379 | (($ $primcall 'bv-f64-set! (bv idx val)) | |
380 | (emit-bv-f64-set! asm (slot bv) (slot idx) (slot val))) | |
381 | (($ $primcall 'unwind ()) | |
382 | (emit-unwind asm)))) | |
383 | ||
384 | (define (compile-values label exp syms) | |
385 | (match exp | |
386 | (($ $values args) | |
387 | (for-each (match-lambda | |
388 | ((src . dst) (emit-mov asm dst src))) | |
389 | (lookup-parallel-moves label allocation)) | |
390 | (for-each maybe-load-constant (map slot syms) args)))) | |
391 | ||
392 | (define (compile-test label exp kt kf next-label) | |
393 | (define (unary op sym) | |
394 | (cond | |
395 | ((eq? kt next-label) | |
396 | (op asm (slot sym) #t kf)) | |
397 | (else | |
398 | (op asm (slot sym) #f kt) | |
399 | (unless (eq? kf next-label) | |
400 | (emit-br asm kf))))) | |
401 | (define (binary op a b) | |
402 | (cond | |
403 | ((eq? kt next-label) | |
404 | (op asm (slot a) (slot b) #t kf)) | |
405 | (else | |
406 | (op asm (slot a) (slot b) #f kt) | |
407 | (unless (eq? kf next-label) | |
408 | (emit-br asm kf))))) | |
409 | (match exp | |
58ef5f07 AW |
410 | (($ $values (sym)) |
411 | (call-with-values (lambda () | |
412 | (lookup-maybe-constant-value sym allocation)) | |
413 | (lambda (has-const? val) | |
414 | (if has-const? | |
415 | (if val | |
416 | (unless (eq? kt next-label) | |
417 | (emit-br asm kt)) | |
418 | (unless (eq? kf next-label) | |
419 | (emit-br asm kf))) | |
420 | (unary emit-br-if-true sym))))) | |
6e422a35 AW |
421 | (($ $primcall 'null? (a)) (unary emit-br-if-null a)) |
422 | (($ $primcall 'nil? (a)) (unary emit-br-if-nil a)) | |
423 | (($ $primcall 'pair? (a)) (unary emit-br-if-pair a)) | |
424 | (($ $primcall 'struct? (a)) (unary emit-br-if-struct a)) | |
425 | (($ $primcall 'char? (a)) (unary emit-br-if-char a)) | |
426 | (($ $primcall 'symbol? (a)) (unary emit-br-if-symbol a)) | |
427 | (($ $primcall 'variable? (a)) (unary emit-br-if-variable a)) | |
428 | (($ $primcall 'vector? (a)) (unary emit-br-if-vector a)) | |
429 | (($ $primcall 'string? (a)) (unary emit-br-if-string a)) | |
430 | (($ $primcall 'bytevector? (a)) (unary emit-br-if-bytevector a)) | |
431 | ;; Add more TC7 tests here. Keep in sync with | |
432 | ;; *branching-primcall-arities* in (language cps primitives) and | |
433 | ;; the set of macro-instructions in assembly.scm. | |
434 | (($ $primcall 'eq? (a b)) (binary emit-br-if-eq a b)) | |
435 | (($ $primcall 'eqv? (a b)) (binary emit-br-if-eqv a b)) | |
436 | (($ $primcall 'equal? (a b)) (binary emit-br-if-equal a b)) | |
437 | (($ $primcall '< (a b)) (binary emit-br-if-< a b)) | |
438 | (($ $primcall '<= (a b)) (binary emit-br-if-<= a b)) | |
439 | (($ $primcall '= (a b)) (binary emit-br-if-= a b)) | |
440 | (($ $primcall '>= (a b)) (binary emit-br-if-<= b a)) | |
441 | (($ $primcall '> (a b)) (binary emit-br-if-< b a)))) | |
442 | ||
fa48a2f7 | 443 | (define (compile-trunc label k exp nreq rest-var nlocals) |
b3ae2b50 AW |
444 | (define (do-call proc args emit-call) |
445 | (let* ((proc-slot (lookup-call-proc-slot label allocation)) | |
446 | (nargs (1+ (length args))) | |
447 | (arg-slots (map (lambda (x) (+ x proc-slot)) (iota nargs)))) | |
448 | (for-each (match-lambda | |
449 | ((src . dst) (emit-mov asm dst src))) | |
450 | (lookup-parallel-moves label allocation)) | |
451 | (for-each maybe-load-constant arg-slots (cons proc args)) | |
452 | (emit-call asm proc-slot nargs) | |
453 | (emit-dead-slot-map asm proc-slot | |
454 | (lookup-dead-slot-map label allocation)) | |
455 | (cond | |
456 | ((and (= 1 nreq) (and rest-var) (not (maybe-slot rest-var)) | |
457 | (match (lookup-parallel-moves k allocation) | |
458 | ((((? (lambda (src) (= src (1+ proc-slot))) src) | |
459 | . dst)) dst) | |
460 | (_ #f))) | |
461 | ;; The usual case: one required live return value, ignoring | |
462 | ;; any additional values. | |
463 | => (lambda (dst) | |
464 | (emit-receive asm dst proc-slot nlocals))) | |
465 | (else | |
466 | (unless (and (zero? nreq) rest-var) | |
467 | (emit-receive-values asm proc-slot (->bool rest-var) nreq)) | |
468 | (when (and rest-var (maybe-slot rest-var)) | |
469 | (emit-bind-rest asm (+ proc-slot 1 nreq))) | |
470 | (for-each (match-lambda | |
471 | ((src . dst) (emit-mov asm dst src))) | |
472 | (lookup-parallel-moves k allocation)) | |
473 | (emit-reset-frame asm nlocals))))) | |
6e422a35 AW |
474 | (match exp |
475 | (($ $call proc args) | |
b3ae2b50 AW |
476 | (do-call proc args |
477 | (lambda (asm proc-slot nargs) | |
478 | (emit-call asm proc-slot nargs)))) | |
479 | (($ $callk k proc args) | |
480 | (do-call proc args | |
481 | (lambda (asm proc-slot nargs) | |
482 | (emit-call-label asm proc-slot nargs k)))))) | |
6e422a35 AW |
483 | |
484 | (match f | |
6bc36ca5 | 485 | (($ $cont k ($ $kfun src meta self tail clause)) |
24b611e8 | 486 | (compile-entry))))) |
d258fccc | 487 | |
691697de | 488 | (define (compile-bytecode exp env opts) |
6e8ad823 AW |
489 | (let* ((exp (fix-arities exp)) |
490 | (exp (optimize exp opts)) | |
491 | (exp (convert-closures exp)) | |
cf8bb037 | 492 | ;; first-order optimization should go here |
6e8ad823 | 493 | (exp (reify-primitives exp)) |
a0329d01 | 494 | (exp (renumber exp)) |
6e8ad823 | 495 | (asm (make-assembler))) |
cf8bb037 AW |
496 | (match exp |
497 | (($ $program funs) | |
498 | (for-each (lambda (fun) (compile-fun fun asm)) | |
499 | funs))) | |
6e8ad823 AW |
500 | (values (link-assembly asm #:page-aligned? (kw-arg-ref opts #:to-file? #f)) |
501 | env | |
502 | env))) |