Commit | Line | Data |
---|---|---|
6e8ad823 AW |
1 | ;;; Continuation-passing style (CPS) intermediate language (IL) |
2 | ||
3 | ;; Copyright (C) 2013 Free Software Foundation, Inc. | |
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 | ;;; | |
21 | ;;; Compiling CPS to RTL. The result is in the RTL language, which | |
22 | ;;; happens to be an ELF image as a bytecode. | |
23 | ;;; | |
24 | ;;; Code: | |
25 | ||
26 | (define-module (language cps compile-rtl) | |
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) |
6e8ad823 AW |
34 | #:use-module (language cps dfg) |
35 | #:use-module (language cps primitives) | |
36 | #:use-module (language cps reify-primitives) | |
37 | #:use-module (language cps slot-allocation) | |
38 | #:use-module (system vm assembler) | |
39 | #:export (compile-rtl)) | |
40 | ||
41 | ;; TODO: Source info, local var names. Needs work in the linker and the | |
42 | ;; debugger. | |
43 | ||
44 | (define (kw-arg-ref args kw default) | |
45 | (match (memq kw args) | |
46 | ((_ val . _) val) | |
47 | (_ default))) | |
48 | ||
49 | (define (optimize exp opts) | |
50 | (define (run-pass exp pass kw default) | |
51 | (if (kw-arg-ref opts kw default) | |
52 | (pass exp) | |
53 | exp)) | |
54 | ||
55 | ;; Calls to source-to-source optimization passes go here. | |
fa3b6e57 AW |
56 | (let* ((exp (run-pass exp contify #:contify? #t)) |
57 | (exp (run-pass exp inline-constructors #:inline-constructors? #t))) | |
6e8ad823 AW |
58 | ;; Passes that are needed: |
59 | ;; | |
6e8ad823 AW |
60 | ;; * Abort contification: turning abort primcalls into continuation |
61 | ;; calls, and eliding prompts if possible. | |
62 | ;; | |
63 | ;; * Common subexpression elimination. Desperately needed. Requires | |
64 | ;; effects analysis. | |
65 | ;; | |
66 | ;; * Loop peeling. Unrolls the first round through a loop if the | |
67 | ;; loop has effects that CSE can work on. Requires effects | |
68 | ;; analysis. When run before CSE, loop peeling is the equivalent | |
69 | ;; of loop-invariant code motion (LICM). | |
70 | ;; | |
71 | ;; * Generic simplification pass, to be run as needed. Used to | |
72 | ;; "clean up", both on the original raw input and after specific | |
73 | ;; optimization passes. | |
74 | ||
75 | exp)) | |
76 | ||
77 | (define (visit-funs proc exp) | |
78 | (match exp | |
79 | (($ $continue _ exp) | |
80 | (visit-funs proc exp)) | |
81 | ||
82 | (($ $fun meta free body) | |
83 | (proc exp) | |
84 | (visit-funs proc body)) | |
85 | ||
86 | (($ $letk conts body) | |
87 | (visit-funs proc body) | |
88 | (for-each (lambda (cont) (visit-funs proc cont)) conts)) | |
89 | ||
90 | (($ $cont sym src ($ $kargs names syms body)) | |
91 | (visit-funs proc body)) | |
92 | ||
93 | (($ $cont sym src ($ $kclause arity body)) | |
94 | (visit-funs proc body)) | |
95 | ||
96 | (($ $cont sym src ($ $kentry self tail clauses)) | |
97 | (for-each (lambda (clause) (visit-funs proc clause)) clauses)) | |
98 | ||
99 | (_ (values)))) | |
100 | ||
101 | (define (emit-rtl-sequence asm exp allocation nlocals cont-table) | |
607fe5a6 AW |
102 | (define (immediate-u8? val) |
103 | (and (integer? val) (exact? val) (<= 0 val 255))) | |
104 | ||
105 | (define (maybe-immediate-u8 sym) | |
106 | (call-with-values (lambda () | |
107 | (lookup-maybe-constant-value sym allocation)) | |
108 | (lambda (has-const? val) | |
109 | (and has-const? (immediate-u8? val) val)))) | |
110 | ||
6e8ad823 AW |
111 | (define (slot sym) |
112 | (lookup-slot sym allocation)) | |
113 | ||
114 | (define (constant sym) | |
115 | (lookup-constant-value sym allocation)) | |
116 | ||
117 | (define (emit-rtl label k exp next-label) | |
118 | (define (maybe-mov dst src) | |
119 | (unless (= dst src) | |
120 | (emit-mov asm dst src))) | |
121 | ||
122 | (define (maybe-jump label) | |
123 | (unless (eq? label next-label) | |
124 | (emit-br asm label))) | |
125 | ||
126 | (define (maybe-load-constant slot src) | |
127 | (call-with-values (lambda () | |
128 | (lookup-maybe-constant-value src allocation)) | |
129 | (lambda (has-const? val) | |
130 | (and has-const? | |
131 | (begin | |
132 | (emit-load-constant asm slot val) | |
133 | #t))))) | |
134 | ||
135 | (define (emit-tail) | |
136 | ;; There are only three kinds of expressions in tail position: | |
137 | ;; tail calls, multiple-value returns, and single-value returns. | |
138 | (match exp | |
139 | (($ $call proc args) | |
140 | (for-each (match-lambda | |
141 | ((src . dst) (emit-mov asm dst src))) | |
142 | (lookup-parallel-moves label allocation)) | |
143 | (let ((tail-slots (cdr (iota (1+ (length args)))))) | |
144 | (for-each maybe-load-constant tail-slots args)) | |
145 | (emit-tail-call asm (1+ (length args)))) | |
146 | (($ $values args) | |
147 | (let ((tail-slots (cdr (iota (1+ (length args)))))) | |
148 | (for-each (match-lambda | |
149 | ((src . dst) (emit-mov asm dst src))) | |
150 | (lookup-parallel-moves label allocation)) | |
151 | (for-each maybe-load-constant tail-slots args)) | |
152 | (emit-reset-frame asm (1+ (length args))) | |
153 | (emit-return-values asm)) | |
154 | (($ $primcall 'return (arg)) | |
155 | (emit-return asm (slot arg))))) | |
156 | ||
157 | (define (emit-val sym) | |
158 | (let ((dst (slot sym))) | |
159 | (match exp | |
160 | (($ $var sym) | |
161 | (maybe-mov dst (slot sym))) | |
162 | (($ $void) | |
163 | (when dst | |
164 | (emit-load-constant asm dst *unspecified*))) | |
165 | (($ $const exp) | |
166 | (when dst | |
167 | (emit-load-constant asm dst exp))) | |
168 | (($ $fun meta () ($ $cont k)) | |
169 | (emit-load-static-procedure asm dst k)) | |
170 | (($ $fun meta free ($ $cont k)) | |
171 | (emit-make-closure asm dst k (length free))) | |
172 | (($ $call proc args) | |
173 | (let ((proc-slot (lookup-call-proc-slot label allocation)) | |
174 | (nargs (length args))) | |
175 | (or (maybe-load-constant proc-slot proc) | |
176 | (maybe-mov proc-slot (slot proc))) | |
177 | (let lp ((n (1+ proc-slot)) (args args)) | |
178 | (match args | |
179 | (() | |
180 | (emit-call asm proc-slot (+ nargs 1)) | |
181 | (emit-receive asm dst proc-slot nlocals)) | |
182 | ((arg . args) | |
183 | (or (maybe-load-constant n arg) | |
184 | (maybe-mov n (slot arg))) | |
185 | (lp (1+ n) args)))))) | |
186 | (($ $primcall 'current-module) | |
187 | (emit-current-module asm dst)) | |
188 | (($ $primcall 'cached-toplevel-box (scope name bound?)) | |
189 | (emit-cached-toplevel-box asm dst (constant scope) (constant name) | |
190 | (constant bound?))) | |
191 | (($ $primcall 'cached-module-box (mod name public? bound?)) | |
192 | (emit-cached-module-box asm dst (constant mod) (constant name) | |
193 | (constant public?) (constant bound?))) | |
194 | (($ $primcall 'resolve (name bound?)) | |
195 | (emit-resolve asm dst (constant bound?) (slot name))) | |
196 | (($ $primcall 'free-ref (closure idx)) | |
197 | (emit-free-ref asm dst (slot closure) (constant idx))) | |
607fe5a6 AW |
198 | (($ $primcall 'make-vector (length init)) |
199 | (cond | |
200 | ((maybe-immediate-u8 length) | |
201 | => (lambda (length) | |
202 | (emit-constant-make-vector asm dst length (slot init)))) | |
203 | (else | |
204 | (emit-make-vector asm dst (slot length) (slot init))))) | |
8ba3f20c | 205 | (($ $primcall 'vector-ref (vector index)) |
607fe5a6 AW |
206 | (cond |
207 | ((maybe-immediate-u8 index) | |
208 | => (lambda (index) | |
209 | (emit-constant-vector-ref asm dst (slot vector) index))) | |
210 | (else | |
211 | (emit-vector-ref asm dst (slot vector) (slot index))))) | |
6e8ad823 AW |
212 | (($ $primcall name args) |
213 | ;; FIXME: Inline all the cases. | |
214 | (let ((inst (prim-rtl-instruction name))) | |
215 | (emit-text asm `((,inst ,dst ,@(map slot args)))))) | |
216 | (($ $values (arg)) | |
217 | (or (maybe-load-constant dst arg) | |
8d59d55e | 218 | (maybe-mov dst (slot arg))))) |
6e8ad823 AW |
219 | (maybe-jump k))) |
220 | ||
221 | (define (emit-vals syms) | |
222 | (match exp | |
223 | (($ $primcall name args) | |
224 | (error "unimplemented primcall in values context" name)) | |
225 | (($ $values args) | |
226 | (for-each (match-lambda | |
227 | ((src . dst) (emit-mov asm dst src))) | |
228 | (lookup-parallel-moves label allocation)) | |
229 | (for-each maybe-load-constant (map slot syms) args))) | |
230 | (maybe-jump k)) | |
231 | ||
232 | (define (emit-seq) | |
233 | (match exp | |
234 | (($ $primcall 'cache-current-module! (sym scope)) | |
235 | (emit-cache-current-module! asm (slot sym) (constant scope))) | |
236 | (($ $primcall 'free-set! (closure idx value)) | |
237 | (emit-free-set! asm (slot closure) (slot value) (constant idx))) | |
238 | (($ $primcall 'box-set! (box value)) | |
239 | (emit-box-set! asm (slot box) (slot value))) | |
240 | (($ $primcall 'struct-set! (struct index value)) | |
241 | (emit-struct-set! asm (slot struct) (slot index) (slot value))) | |
242 | (($ $primcall 'vector-set! (vector index value)) | |
8ba3f20c AW |
243 | (call-with-values (lambda () |
244 | (lookup-maybe-constant-value index allocation)) | |
245 | (lambda (has-const? index-val) | |
246 | (if (and has-const? (integer? index-val) (exact? index-val) | |
247 | (<= 0 index-val 255)) | |
248 | (emit-constant-vector-set! asm (slot vector) index-val | |
249 | (slot value)) | |
250 | (emit-vector-set! asm (slot vector) (slot index) | |
251 | (slot value)))))) | |
4f406fea AW |
252 | (($ $primcall 'variable-set! (var val)) |
253 | (emit-box-set! asm (slot var) (slot val))) | |
6e8ad823 AW |
254 | (($ $primcall 'set-car! (pair value)) |
255 | (emit-set-car! asm (slot pair) (slot value))) | |
256 | (($ $primcall 'set-cdr! (pair value)) | |
257 | (emit-set-cdr! asm (slot pair) (slot value))) | |
258 | (($ $primcall 'define! (sym value)) | |
259 | (emit-define asm (slot sym) (slot value))) | |
5db3e6bc AW |
260 | (($ $primcall 'push-fluid (fluid val)) |
261 | (emit-push-fluid asm (slot fluid) (slot val))) | |
262 | (($ $primcall 'pop-fluid ()) | |
263 | (emit-pop-fluid asm)) | |
6fb508da AW |
264 | (($ $primcall 'wind (winder unwinder)) |
265 | (emit-wind asm (slot winder) (slot unwinder))) | |
8d59d55e AW |
266 | (($ $primcall 'unwind ()) |
267 | (emit-unwind asm)) | |
6e8ad823 AW |
268 | (($ $primcall name args) |
269 | (error "unhandled primcall in seq context" name)) | |
8d59d55e AW |
270 | (($ $values ()) #f) |
271 | (($ $prompt escape? tag handler) | |
272 | (match (lookup-cont handler cont-table) | |
273 | (($ $ktrunc ($ $arity req () rest () #f) khandler-body) | |
274 | (let ((receive-args (gensym "handler")) | |
275 | (nreq (length req)) | |
276 | (proc-slot (lookup-call-proc-slot label allocation))) | |
277 | (emit-prompt asm (slot tag) escape? proc-slot receive-args) | |
278 | (emit-br asm k) | |
279 | (emit-label asm receive-args) | |
280 | (emit-receive-values asm proc-slot (->bool rest) nreq) | |
281 | (when rest | |
282 | (emit-bind-rest asm (+ proc-slot 1 nreq))) | |
283 | (for-each (match-lambda | |
284 | ((src . dst) (emit-mov asm dst src))) | |
285 | (lookup-parallel-moves handler allocation)) | |
286 | (emit-reset-frame asm nlocals) | |
287 | (emit-br asm khandler-body)))))) | |
6e8ad823 AW |
288 | (maybe-jump k)) |
289 | ||
290 | (define (emit-test kt kf) | |
291 | (define (unary op sym) | |
292 | (cond | |
293 | ((eq? kt next-label) | |
294 | (op asm (slot sym) #t kf)) | |
295 | (else | |
296 | (op asm (slot sym) #f kt) | |
297 | (maybe-jump kf)))) | |
298 | (define (binary op a b) | |
299 | (cond | |
300 | ((eq? kt next-label) | |
301 | (op asm (slot a) (slot b) #t kf)) | |
302 | (else | |
303 | (op asm (slot a) (slot b) #f kt) | |
304 | (maybe-jump kf)))) | |
305 | (match exp | |
306 | (($ $var sym) (unary emit-br-if-true sym)) | |
307 | (($ $primcall 'null? (a)) (unary emit-br-if-null a)) | |
308 | (($ $primcall 'nil? (a)) (unary emit-br-if-nil a)) | |
309 | (($ $primcall 'pair? (a)) (unary emit-br-if-pair a)) | |
310 | (($ $primcall 'struct? (a)) (unary emit-br-if-struct a)) | |
311 | (($ $primcall 'char? (a)) (unary emit-br-if-char a)) | |
be8b62ca AW |
312 | (($ $primcall 'symbol? (a)) (unary emit-br-if-symbol a)) |
313 | (($ $primcall 'variable? (a)) (unary emit-br-if-variable a)) | |
314 | (($ $primcall 'vector? (a)) (unary emit-br-if-vector a)) | |
315 | (($ $primcall 'string? (a)) (unary emit-br-if-string a)) | |
316 | ;; Add more TC7 tests here. Keep in sync with | |
317 | ;; *branching-primcall-arities* in (language cps primitives) and | |
318 | ;; the set of macro-instructions in assembly.scm. | |
6e8ad823 AW |
319 | (($ $primcall 'eq? (a b)) (binary emit-br-if-eq a b)) |
320 | (($ $primcall 'eqv? (a b)) (binary emit-br-if-eqv a b)) | |
321 | (($ $primcall 'equal? (a b)) (binary emit-br-if-equal a b)) | |
322 | (($ $primcall '< (a b)) (binary emit-br-if-< a b)) | |
323 | (($ $primcall '<= (a b)) (binary emit-br-if-<= a b)) | |
324 | (($ $primcall '= (a b)) (binary emit-br-if-= a b)) | |
325 | (($ $primcall '>= (a b)) (binary emit-br-if-<= b a)) | |
326 | (($ $primcall '> (a b)) (binary emit-br-if-< b a)))) | |
327 | ||
328 | (define (emit-trunc nreq rest? k) | |
329 | (match exp | |
330 | (($ $call proc args) | |
331 | (let ((proc-slot (lookup-call-proc-slot label allocation)) | |
332 | (nargs (length args))) | |
333 | (or (maybe-load-constant proc-slot proc) | |
334 | (maybe-mov proc-slot (slot proc))) | |
335 | (let lp ((n (1+ proc-slot)) (args args)) | |
336 | (match args | |
337 | (() | |
338 | (emit-call asm proc-slot (+ nargs 1)) | |
82f4bac4 AW |
339 | ;; FIXME: Only allow more values if there is a rest arg. |
340 | ;; Express values truncation by the presence of an | |
341 | ;; unused rest arg instead of implicitly. | |
342 | (emit-receive-values asm proc-slot #t nreq) | |
6e8ad823 AW |
343 | (when rest? |
344 | (emit-bind-rest asm (+ proc-slot 1 nreq))) | |
345 | (for-each (match-lambda | |
346 | ((src . dst) (emit-mov asm dst src))) | |
347 | (lookup-parallel-moves label allocation)) | |
348 | (emit-reset-frame asm nlocals)) | |
349 | ((arg . args) | |
350 | (or (maybe-load-constant n arg) | |
351 | (maybe-mov n (slot arg))) | |
352 | (lp (1+ n) args))))))) | |
353 | (maybe-jump k)) | |
354 | ||
355 | (match (lookup-cont k cont-table) | |
356 | (($ $ktail) (emit-tail)) | |
357 | (($ $kargs (name) (sym)) (emit-val sym)) | |
358 | (($ $kargs () ()) (emit-seq)) | |
359 | (($ $kargs names syms) (emit-vals syms)) | |
360 | (($ $kargs (name) (sym)) (emit-val sym)) | |
361 | (($ $kif kt kf) (emit-test kt kf)) | |
362 | (($ $ktrunc ($ $arity req () rest () #f) k) | |
363 | (emit-trunc (length req) (and rest #t) k)))) | |
364 | ||
365 | (define (collect-exps k src cont tail) | |
366 | (define (find-exp k src term) | |
367 | (match term | |
368 | (($ $continue exp-k exp) | |
369 | (cons (list k src exp-k exp) tail)) | |
370 | (($ $letk conts body) | |
371 | (find-exp k src body)))) | |
372 | (match cont | |
373 | (($ $kargs names syms body) | |
374 | (find-exp k src body)) | |
375 | (_ tail))) | |
376 | ||
377 | (let lp ((exps (reverse (fold-local-conts collect-exps '() exp)))) | |
378 | (match exps | |
379 | (() #t) | |
380 | (((k src exp-k exp) . exps) | |
381 | (let ((next-label (match exps | |
382 | (((k . _) . _) k) | |
383 | (() #f)))) | |
384 | (emit-label asm k) | |
e675e9bd AW |
385 | (when src |
386 | (emit-source asm src)) | |
6e8ad823 AW |
387 | (emit-rtl k exp-k exp next-label) |
388 | (lp exps)))))) | |
389 | ||
390 | (define (compile-fun f asm) | |
391 | (let ((allocation (allocate-slots f)) | |
392 | (cont-table (match f | |
393 | (($ $fun meta free body) | |
394 | (build-local-cont-table body))))) | |
395 | (define (emit-fun-clause clause alternate) | |
396 | (match clause | |
397 | (($ $cont k src | |
398 | ($ $kclause ($ $arity req opt rest kw allow-other-keys?) | |
399 | body)) | |
400 | (let ((kw-indices (map (match-lambda | |
401 | ((key name sym) | |
402 | (cons key (lookup-slot sym allocation)))) | |
403 | kw)) | |
404 | (nlocals (lookup-nlocals k allocation))) | |
405 | (emit-label asm k) | |
e675e9bd AW |
406 | (when src |
407 | (emit-source asm src)) | |
6e8ad823 AW |
408 | (emit-begin-kw-arity asm req opt rest kw-indices allow-other-keys? |
409 | nlocals alternate) | |
410 | (emit-rtl-sequence asm body allocation nlocals cont-table) | |
411 | (emit-end-arity asm))))) | |
412 | ||
413 | (define (emit-fun-clauses clauses) | |
414 | (match clauses | |
415 | ((clause . clauses) | |
416 | (let ((kalternate (match clauses | |
417 | (() #f) | |
418 | ((($ $cont k) . _) k)))) | |
419 | (emit-fun-clause clause kalternate) | |
420 | (when kalternate | |
421 | (emit-fun-clauses clauses)))))) | |
422 | ||
423 | (match f | |
424 | (($ $fun meta free ($ $cont k src ($ $kentry self tail clauses))) | |
425 | (emit-begin-program asm k (or meta '())) | |
e675e9bd AW |
426 | (when src |
427 | (emit-source asm src)) | |
6e8ad823 AW |
428 | (emit-fun-clauses clauses) |
429 | (emit-end-program asm))))) | |
430 | ||
431 | (define (compile-rtl exp env opts) | |
432 | (let* ((exp (fix-arities exp)) | |
433 | (exp (optimize exp opts)) | |
434 | (exp (convert-closures exp)) | |
435 | (exp (reify-primitives exp)) | |
436 | (asm (make-assembler))) | |
437 | (visit-funs (lambda (fun) | |
438 | (compile-fun fun asm)) | |
439 | exp) | |
440 | (values (link-assembly asm #:page-aligned? (kw-arg-ref opts #:to-file? #f)) | |
441 | env | |
442 | env))) |