Debian Codegraph @

scheme/ikarus.compiler.ss

raw
1 ;;; Ikarus Scheme -- A compiler for R6RS Scheme.
2 ;;; Copyright (C) 2006,2007,2008 Abdulaziz Ghuloum
3 ;;;
4 ;;; This program is free software: you can redistribute it and/or modify
5 ;;; it under the terms of the GNU General Public License version 3 as
6 ;;; published by the Free Software Foundation.
7 ;;;
8 ;;; This program is distributed in the hope that it will be useful, but
9 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
10 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 ;;; General Public License for more details.
12 ;;;
13 ;;; You should have received a copy of the GNU General Public License
14 ;;; along with this program. If not, see <http://www.gnu.org/licenses/>.
15
16
17 (library (ikarus.compiler)
18 (export compile-core-expr-to-port
19 assembler-output optimize-cp
20 current-primitive-locations eval-core
21 current-core-eval compile-core-expr
22 expand expand/optimize expand/scc-letrec optimizer-output
23 cp0-effort-limit cp0-size-limit optimize-level
24 perform-tag-analysis tag-analysis-output
25 strip-source-info generate-debug-calls current-letrec-pass)
26 (import
27 (rnrs hashtables)
28 (ikarus system $fx)
29 (ikarus system $pairs)
30 (only (ikarus system $codes) $code->closure)
31 (only (ikarus system $structs) $struct-ref $struct/rtd?)
32 (except (ikarus)
33 optimize-level debug-optimizer
34 fasl-write optimize-cp
35 compile-core-expr-to-port assembler-output
36 current-primitive-locations eval-core
37 cp0-size-limit cp0-effort-limit
38 expand/optimize expand/scc-letrec expand optimizer-output
39 tag-analysis-output perform-tag-analysis
40 current-core-eval current-letrec-pass)
41 (ikarus include)
42 (ikarus.fasl.write)
43 (ikarus.intel-assembler))
44
45
46 (define strip-source-info (make-parameter #f))
47 (define generate-debug-calls (make-parameter #f))
48
49 (define-syntax struct-case
50 (lambda (x)
51 (define (enumerate fld* i)
52 (syntax-case fld* ()
53 [() #'()]
54 [(x . x*)
55 (with-syntax ([i i] [i* (enumerate #'x* (fx+ i 1))])
56 #'(i . i*))]))
57 (define (generate-body ctxt cls*)
58 (syntax-case cls* (else)
59 [() (with-syntax ([x x]) #'(error #f "unmatched " v 'x))]
60 [([else b b* ...]) #'(begin b b* ...)]
61 [([(rec-name rec-field* ...) b b* ...] . rest) (identifier? #'rec-name)
62 (with-syntax ([altern (generate-body ctxt #'rest)]
63 [(id* ...) (enumerate #'(rec-field* ...) 0)]
64 [rtd #'(type-descriptor rec-name)])
65 #'(if ($struct/rtd? v rtd)
66 (let ([rec-field* ($struct-ref v id*)] ...)
67 b b* ...)
68 altern))]))
69 (syntax-case x ()
70 [(_ expr cls* ...)
71 (with-syntax ([body (generate-body #'_ #'(cls* ...))])
72 #'(let ([v expr]) body))])))
73
74
75
76
77 (define (remq1 x ls)
78 (cond
79 [(null? ls) '()]
80 [(eq? x (car ls)) (cdr ls)]
81 [else
82 (let ([t (remq1 x (cdr ls))])
83 (cond
84 [(eq? t (cdr ls)) ls]
85 [else (cons (car ls) t)]))]))
86
87 (define (singleton x) (list x))
88
89 (define (union s1 s2)
90 (define (add* s1 s2)
91 (cond
92 [(null? s1) s2]
93 [else (add (car s1) (add* (cdr s1) s2))]))
94 (define (add x s)
95 (cond
96 [(memq x s) s]
97 [else (cons x s)]))
98 (cond
99 [(null? s1) s2]
100 [(null? s2) s1]
101 [else (add* s1 s2)]))
102
103 (define (difference s1 s2)
104 (define (rem* s1 s2)
105 (cond
106 [(null? s1) s2]
107 [else (remq1 (car s1) (rem* (cdr s1) s2))]))
108 (cond
109 [(null? s1) '()]
110 [(null? s2) s1]
111 [else (rem* s2 s1)]))
112
113
114
115 (define-struct constant (value))
116 (define-struct code-loc (label))
117 (define-struct foreign-label (label))
118 (define-struct var
119 (name reg-conf frm-conf var-conf reg-move frm-move var-move
120 loc index referenced global-loc))
121 (define-struct cp-var (idx))
122 (define-struct frame-var (idx))
123 (define-struct new-frame (base-idx size body))
124 (define-struct save-cp (loc))
125 (define-struct eval-cp (check body))
126 (define-struct return (value))
127 (define-struct call-cp
128 (call-convention label save-cp? rp-convention base-idx arg-count live-mask))
129 (define-struct tailcall-cp (convention label arg-count))
130 (define-struct primcall (op arg*))
131 (define-struct primref (name))
132 (define-struct conditional (test conseq altern))
133 (define-struct interrupt-call (test handler))
134 (define-struct bind (lhs* rhs* body))
135 (define-struct recbind (lhs* rhs* body))
136 (define-struct rec*bind (lhs* rhs* body))
137 (define-struct fix (lhs* rhs* body))
138
139 (define-struct seq (e0 e1))
140 (define-struct case-info (label args proper))
141 (define-struct clambda-case (info body))
142 (define-struct clambda (label cases cp free name))
143 (define-struct closure (code free* well-known?))
144 (define-struct funcall (op rand*))
145 (define-struct jmpcall (label op rand*))
146 (define-struct forcall (op rand*))
147 (define-struct codes (list body))
148 (define-struct assign (lhs rhs))
149 (define-struct mvcall (producer consumer))
150
151 (define-struct known (expr type))
152
153 (define-struct shortcut (body handler))
154
155 (define-struct fvar (idx))
156 (define-struct object (val))
157 (define-struct locals (vars body))
158 (define-struct nframe (vars live body))
159 (define-struct nfv (conf loc var-conf frm-conf nfv-conf))
160 (define-struct ntcall (target value args mask size))
161 (define-struct asm-instr (op dst src))
162 (define-struct disp (s0 s1))
163
164 ;;; this define-structure definition for compatibility with the
165 ;;; notation used in Oscar's thesis.
166 (define-syntax define-structure
167 (lambda (stx)
168 (define (fmt ctxt)
169 (lambda (str . args)
170 (datum->syntax ctxt
171 (string->symbol
172 (apply format str (map syntax->datum args))))))
173 (syntax-case stx ()
174 [(_ (name fields ...))
175 #'(define-struct name (fields ...))]
176 [(_ (name fields ...) ([others defaults] ...))
177 (with-syntax ([(pred maker (getters ...) (setters ...))
178 (let ([fmt (fmt #'name)])
179 (list (fmt "~s?" #'name)
180 (fmt "make-~s" #'name)
181 (map (lambda (x) (fmt "~s-~s" #'name x))
182 #'(fields ... others ...))
183 (map (lambda (x) (fmt "set-~s-~s!" #'name x))
184 #'(fields ... others ...))))])
185 #'(module (name pred getters ... setters ... maker)
186 (module P (name pred getters ... setters ... maker)
187 (define-struct name (fields ... others ...)))
188 (module (maker)
189 (define (maker fields ...)
190 (import P)
191 (maker fields ... defaults ...)))
192 (module (name pred getters ... setters ...)
193 (import P))))])))
194 ;;;
195 (define-structure (prelex name operand)
196 ([source-referenced? #f]
197 [source-assigned? #f]
198 [residual-referenced? #f]
199 [residual-assigned? #f]
200 [global-location #f]))
201
202 (define mkfvar
203 (let ([cache '()])
204 (lambda (i)
205 (cond
206 [(fixnum? i)
207 (cond
208 [(assv i cache) => cdr]
209 [else
210 (let ([fv (make-fvar i)])
211 (set! cache (cons (cons i fv) cache))
212 fv)])]
213 [else (error 'mkfvar "not a fixnum" i)]))))
214
215 (define (unique-var name)
216 (make-var name #f #f #f #f #f #f #f #f #f #f))
217
218 (define (recordize x)
219 (define *cookie* (gensym))
220 (define (gen-fml* fml*)
221 (cond
222 [(pair? fml*)
223 (let ([v (make-prelex (car fml*) #f)])
224 (putprop (car fml*) *cookie* v)
225 (cons v (gen-fml* (cdr fml*))))]
226 [(symbol? fml*)
227 (let ([v (make-prelex fml* #f)])
228 (putprop fml* *cookie* v)
229 v)]
230 [else '()]))
231 (define (ungen-fml* fml*)
232 (cond
233 [(pair? fml*)
234 (remprop (car fml*) *cookie*)
235 (ungen-fml* (cdr fml*))]
236 [(symbol? fml*)
237 (remprop fml* *cookie*)]))
238 (define (properize fml*)
239 (cond
240 [(pair? fml*)
241 (cons (car fml*) (properize (cdr fml*)))]
242 [(null? fml*) '()]
243 [else (list fml*)]))
244 (define (quoted-sym x)
245 (if (and (list? x)
246 (fx= (length x) 2)
247 (eq? 'quote (car x))
248 (symbol? (cadr x)))
249 (cadr x)
250 (error 'quoted-sym "not a quoted symbol" x)))
251 (define (quoted-string x)
252 (if (and (list? x)
253 (fx= (length x) 2)
254 (eq? 'quote (car x))
255 (string? (cadr x)))
256 (cadr x)
257 (error 'quoted-string "not a quoted string" x)))
258 (define (lexical x)
259 (getprop x *cookie*))
260 (define (get-fmls x args)
261 (define (matching? fmls args)
262 (cond
263 [(null? fmls) (null? args)]
264 [(pair? fmls) (and (pair? args) (matching? (cdr fmls) (cdr args)))]
265 [else #t]))
266 (define (get-cls* x)
267 (if (pair? x)
268 (case (car x)
269 [(case-lambda) (cdr x)]
270 [(annotated-case-lambda) (cddr x)]
271 [else '()])
272 '()))
273 (let f ([cls* (get-cls* x)])
274 (cond
275 [(null? cls*) '()]
276 [(matching? (caar cls*) args)
277 (caar cls*)]
278 [else (f (cdr cls*))])))
279 (define (make-global-set! lhs rhs)
280 (make-funcall (make-primref '$init-symbol-value!)
281 (list (make-constant lhs) rhs)))
282 (define-syntax equal-case
283 (lambda (x)
284 (syntax-case x ()
285 [(_ val clause* ...)
286 (with-syntax ([body
287 (let f ([clause* #'(clause* ...)])
288 (syntax-case clause* (else)
289 [([else e e* ...])
290 #'(begin e e* ...)]
291 [([(datum* ...) e e* ...] . rest)
292 (with-syntax ([rest (f #'rest)])
293 #'(if (member t '(datum* ...))
294 (begin e e* ...)
295 rest))]))])
296 #'(let ([t val]) body))])))
297
298 (define (E-clambda-clause* cls* ctxt)
299 (map
300 (let ([ctxt (if (pair? ctxt) (car ctxt) #f)])
301 (lambda (cls)
302 (let ([fml* (car cls)] [body (cadr cls)])
303 (let ([nfml* (gen-fml* fml*)])
304 (let ([body (E body ctxt)])
305 (ungen-fml* fml*)
306 (make-clambda-case
307 (make-case-info
308 (gensym)
309 (properize nfml*)
310 (list? fml*))
311 body))))))
312 cls*))
313 (define (E-make-parameter mk-call args ctxt)
314 (case (length args)
315 [(1)
316 (let ([val-expr (car args)]
317 [t (gensym 't)]
318 [x (gensym 'x)])
319 (E `((lambda (,t)
320 (case-lambda
321 [() ,t]
322 [(,x) (set! ,t ,x)]))
323 ,val-expr)
324 ctxt))]
325 [(2)
326 (let ([val-expr (car args)]
327 [guard-expr (cadr args)]
328 [f (gensym 'f)]
329 [t (gensym 't)]
330 [t0 (gensym 't)]
331 [x (gensym 'x)])
332 (E `((case-lambda
333 [(,t ,f)
334 (if ((primitive procedure?) ,f)
335 ((case-lambda
336 [(,t0)
337 (case-lambda
338 [() ,t0]
339 [(,x) (set! ,t0 (,f ,x))])])
340 (,f ,t))
341 ((primitive die)
342 'make-parameter
343 '"not a procedure"
344 ,f))])
345 ,val-expr
346 ,guard-expr)
347 ctxt))]
348 [else
349 (mk-call
350 (make-primref 'make-parameter)
351 (map (lambda (x) (E x #f)) args))]))
352 (define (E-app mk-call rator args ctxt)
353 (equal-case rator
354 [((primitive make-parameter)) (E-make-parameter mk-call args ctxt)]
355 [else
356 (let ([names (get-fmls rator args)])
357 (mk-call
358 (E rator (list ctxt))
359 (let f ([args args] [names names])
360 (cond
361 [(pair? names)
362 (cons
363 (E (car args) (car names))
364 (f (cdr args) (cdr names)))]
365 [else
366 (map (lambda (x) (E x #f)) args)]))))]))
367 (define (E x ctxt)
368 (cond
369 [(pair? x)
370 (equal-case (car x)
371 [(quote) (make-constant (cadr x))]
372 [(if)
373 (make-conditional
374 (E (cadr x) #f)
375 (E (caddr x) ctxt)
376 (E (cadddr x) ctxt))]
377 [(set!)
378 (let ([lhs (cadr x)] [rhs (caddr x)])
379 (cond
380 [(lexical lhs) =>
381 (lambda (var)
382 (set-prelex-source-assigned?! var #t)
383 (make-assign var (E rhs lhs)))]
384 [else (make-global-set! lhs (E rhs lhs))]))]
385 [(begin)
386 (let f ([a (cadr x)] [d (cddr x)])
387 (cond
388 [(null? d) (E a ctxt)]
389 [else
390 (make-seq (E a #f) (f (car d) (cdr d)))]))]
391 [(letrec)
392 (let ([bind* (cadr x)] [body (caddr x)])
393 (let ([lhs* (map car bind*)]
394 [rhs* (map cadr bind*)])
395 (let ([nlhs* (gen-fml* lhs*)])
396 (let ([expr (make-recbind nlhs* (map E rhs* lhs*) (E body ctxt))])
397 (ungen-fml* lhs*)
398 expr))))]
399 [(letrec*)
400 (let ([bind* (cadr x)] [body (caddr x)])
401 (let ([lhs* (map car bind*)]
402 [rhs* (map cadr bind*)])
403 (let ([nlhs* (gen-fml* lhs*)])
404 (let ([expr (make-rec*bind nlhs* (map E rhs* lhs*) (E body ctxt))])
405 (ungen-fml* lhs*)
406 expr))))]
407 [(library-letrec*)
408 (let ([bind* (cadr x)] [body (caddr x)])
409 (let ([lhs* (map car bind*)]
410 [loc* (map cadr bind*)]
411 [rhs* (map caddr bind*)])
412 (let ([nlhs* (gen-fml* lhs*)])
413 (for-each
414 (lambda (lhs loc)
415 (set-prelex-global-location! lhs loc))
416 nlhs* loc*)
417 (let ([expr (make-rec*bind nlhs* (map E rhs* lhs*)
418 (let f ([lhs* nlhs*] [loc* loc*])
419 (cond
420 [(null? lhs*) (E body ctxt)]
421 [(not (car loc*)) (f (cdr lhs*) (cdr loc*))]
422 [else (f (cdr lhs*) (cdr loc*))])))])
423 (ungen-fml* lhs*)
424 expr))))]
425 [(case-lambda)
426 (let ([cls* (E-clambda-clause* (cdr x) ctxt)])
427 (make-clambda (gensym) cls* #f #f
428 (and (symbol? ctxt) ctxt)))]
429 [(annotated-case-lambda)
430 (let ([ae (cadr x)])
431 (let ([cls* (E-clambda-clause* (cddr x) ctxt)])
432 (make-clambda (gensym) cls* #f #f
433 (cons
434 (and (symbol? ctxt) ctxt)
435 (and (not (strip-source-info))
436 (annotation? ae)
437 (annotation-source ae))))))]
438 [(lambda)
439 (E `(case-lambda ,(cdr x)) ctxt)]
440 [(foreign-call)
441 (let ([name (quoted-string (cadr x))] [arg* (cddr x)])
442 (make-forcall name (map (lambda (x) (E x #f)) arg*)))]
443 [(primitive)
444 (let ([var (cadr x)])
445 (make-primref var))]
446 [(annotated-call)
447 (E-app
448 (if (generate-debug-calls)
449 (lambda (op rands)
450 (define (operator? x)
451 (struct-case x
452 [(primref x)
453 (guard (con [(assertion-violation? con) #t])
454 (system-value x)
455 #f)]
456 [else #f]))
457 (define (get-src/expr ae)
458 (if (annotation? ae)
459 (cons (annotation-source ae) (annotation-stripped ae))
460 (cons #f (syntax->datum ae))))
461 (define src/expr
462 (make-constant (get-src/expr (cadr x))))
463 (if (operator? op)
464 (make-funcall op rands)
465 (make-funcall (make-primref 'debug-call)
466 (cons* src/expr op rands))))
467 make-funcall)
468 (caddr x) (cdddr x) ctxt)]
469 [else (E-app make-funcall (car x) (cdr x) ctxt)])]
470 [(symbol? x)
471 (cond
472 [(lexical x) =>
473 (lambda (var)
474 (set-prelex-source-referenced?! var #t)
475 var)]
476 [else
477 (make-funcall
478 (make-primref 'top-level-value)
479 (list (make-constant x)))])]
480 [else (error 'recordize "invalid expression" x)]))
481 (E x #f))
482
483 (define (unparse x)
484 (define (E-args proper x)
485 (if proper
486 (map E x)
487 (let f ([a (car x)] [d (cdr x)])
488 (cond
489 [(null? d) (E a)]
490 [else (cons (E a) (f (car d) (cdr d)))]))))
491 (define (E x)
492 (struct-case x
493 [(constant c) `(quote ,c)]
494 [(known x t) `(known ,(E x) ,(T:description t))]
495 [(code-loc x) `(code-loc ,x)]
496 [(var x) (string->symbol (format ":~a" x))]
497 [(prelex name) (string->symbol (format ":~a" name))]
498 [(primref x) x]
499 [(conditional test conseq altern)
500 `(if ,(E test) ,(E conseq) ,(E altern))]
501 [(interrupt-call e0 e1)
502 `(interrupt-call ,(E e0) ,(E e1))]
503 [(primcall op arg*) `(,op . ,(map E arg*))]
504 [(bind lhs* rhs* body)
505 `(let ,(map (lambda (lhs rhs) (list (E lhs) (E rhs))) lhs* rhs*)
506 ,(E body))]
507 [(recbind lhs* rhs* body)
508 `(letrec ,(map (lambda (lhs rhs) (list (E lhs) (E rhs))) lhs* rhs*)
509 ,(E body))]
510 [(rec*bind lhs* rhs* body)
511 `(letrec* ,(map (lambda (lhs rhs) (list (E lhs) (E rhs))) lhs* rhs*)
512 ,(E body))]
513 ;[(library-recbind lhs* loc* rhs* body)
514 ; `(letrec ,(map (lambda (lhs loc rhs) (list (E lhs) loc (E rhs)))
515 ; lhs* loc* rhs*)
516 ; ,(E body))]
517 [(fix lhs* rhs* body)
518 `(fix ,(map (lambda (lhs rhs) (list (E lhs) (E rhs))) lhs* rhs*)
519 ,(E body))]
520 [(seq e0 e1)
521 (let ()
522 (define (f x ac)
523 (struct-case x
524 [(seq e0 e1) (f e0 (f e1 ac))]
525 [else (cons (E x) ac)]))
526 (cons 'begin (f e0 (f e1 '()))))]
527 [(clambda-case info body)
528 `( ; label: ,(case-info-label info)
529 ,(E-args (case-info-proper info) (case-info-args info))
530 ,(E body))]
531 [(clambda g cls* cp free)
532 `(clambda (label: ,g) ; cp: ,(E cp) ) ;free: ,(map E free))
533 ,@(map E cls*))]
534 [(clambda label clauses free)
535 `(code ,label . ,(map E clauses))]
536 [(closure code free* wk?)
537 `(closure ,@(if wk? '(wk) '()) ,(E code) ,(map E free*))]
538 [(codes list body)
539 `(codes ,(map E list)
540 ,(E body))]
541 [(funcall rator rand*) `(funcall ,(E rator) . ,(map E rand*))]
542 [(jmpcall label rator rand*)
543 `(jmpcall ,label ,(E rator) . ,(map E rand*))]
544 [(forcall rator rand*) `(foreign-call ,rator . ,(map E rand*))]
545 [(assign lhs rhs) `(set! ,(E lhs) ,(E rhs))]
546 [(return x) `(return ,(E x))]
547 [(new-frame base-idx size body)
548 `(new-frame [base: ,base-idx]
549 [size: ,size]
550 ,(E body))]
551 [(frame-var idx)
552 (string->symbol (format "fv.~a" idx))]
553 [(cp-var idx)
554 (string->symbol (format "cp.~a" idx))]
555 [(save-cp expr)
556 `(save-cp ,(E expr))]
557 [(eval-cp check body)
558 `(eval-cp ,check ,(E body))]
559 [(call-cp call-convention label save-cp? rp-convention base-idx arg-count live-mask)
560 `(call-cp [conv: ,call-convention]
561 [label: ,label]
562 [rpconv: ,(if (symbol? rp-convention)
563 rp-convention
564 (E rp-convention))]
565 [base-idx: ,base-idx]
566 [arg-count: ,arg-count]
567 [live-mask: ,live-mask])]
568 [(tailcall-cp convention label arg-count)
569 `(tailcall-cp ,convention ,label ,arg-count)]
570 [(foreign-label x) `(foreign-label ,x)]
571 [(mvcall prod cons) `(mvcall ,(E prod) ,(E cons))]
572 [(fvar idx) (string->symbol (format "fv.~a" idx))]
573 [(nfv idx) 'nfv]
574 [(locals vars body) `(locals ,(map E vars) ,(E body))]
575 [(asm-instr op d s)
576 `(asm ,op ,(E d) ,(E s))]
577 [(disp s0 s1)
578 `(disp ,(E s0) ,(E s1))]
579 [(nframe vars live body) `(nframe ;[vars: ,(map E vars)]
580 ;[live: ,(map E live)]
581 ,(E body))]
582 [(shortcut body handler)
583 `(shortcut ,(E body) ,(E handler))]
584 [(ntcall target valuw args mask size)
585 `(ntcall ,target ,size)]
586 [else
587 (if (symbol? x)
588 x
589 "#<unknown>")]))
590 (E x))
591
592
593 (define (unparse-pretty x)
594 (define n 0)
595 (define h (make-eq-hashtable))
596 (define (Var x)
597 (or (hashtable-ref h x #f)
598 (let ([v (string->symbol (format "~a_~a" (prelex-name x) n))])
599 (hashtable-set! h x v)
600 (set! n (+ n 1))
601 v)))
602 (define (map f ls)
603 (cond
604 [(null? ls) '()]
605 [else
606 (let ([a (f (car ls))])
607 (cons a (map f (cdr ls))))]))
608 (define (E-args proper x)
609 (if proper
610 (map Var x)
611 (let f ([a (car x)] [d (cdr x)])
612 (cond
613 [(null? d) (Var a)]
614 [else
615 (let ([a (Var a)])
616 (cons a (f (car d) (cdr d))))]))))
617 (define (clambda-clause x)
618 (struct-case x
619 [(clambda-case info body)
620 (let ([args (E-args (case-info-proper info) (case-info-args info)) ])
621 (list args (E body)))]))
622 (define (build-let b* body)
623 (cond
624 [(and (= (length b*) 1)
625 (pair? body)
626 (or (eq? (car body) 'let*)
627 (and (eq? (car body) 'let)
628 (= (length (cadr body)) 1))))
629 (list 'let* (append b* (cadr body)) (caddr body))]
630 [else
631 (list 'let b* body)]))
632 (define (E x)
633 (struct-case x
634 [(constant c) `(quote ,c)]
635 [(prelex) (Var x)]
636 [(primref x) x]
637 [(known x t) `(known ,(E x) ,(T:description t))]
638 [(conditional test conseq altern)
639 (cons 'if (map E (list test conseq altern)))]
640 [(primcall op arg*) (cons op (map E arg*))]
641 [(bind lhs* rhs* body)
642 (let* ([lhs* (map Var lhs*)]
643 [rhs* (map E rhs*)]
644 [body (E body)])
645 (import (only (ikarus) map))
646 (build-let (map list lhs* rhs*) body))]
647 [(fix lhs* rhs* body)
648 (let* ([lhs* (map Var lhs*)]
649 [rhs* (map E rhs*)]
650 [body (E body)])
651 (import (only (ikarus) map))
652 (list 'letrec (map list lhs* rhs*) body))]
653 [(recbind lhs* rhs* body)
654 (let* ([lhs* (map Var lhs*)]
655 [rhs* (map E rhs*)]
656 [body (E body)])
657 (import (only (ikarus) map))
658 (list 'letrec (map list lhs* rhs*) body))]
659 [(rec*bind lhs* rhs* body)
660 (let* ([lhs* (map Var lhs*)]
661 [rhs* (map E rhs*)]
662 [body (E body)])
663 (import (only (ikarus) map))
664 (list 'letrec* (map list lhs* rhs*) body))]
665 [(seq e0 e1)
666 (cons 'begin
667 (let f ([e0 e0] [e* (list e1)])
668 (struct-case e0
669 [(seq e00 e01)
670 (f e00 (cons e01 e*))]
671 [else
672 (let ([x (E e0)])
673 (if (null? e*)
674 (list x)
675 (cons x (f (car e*) (cdr e*)))))])))]
676 [(clambda g cls* cp free)
677 (let ([cls* (map clambda-clause cls*)])
678 (cond
679 [(= (length cls*) 1) (cons 'lambda (car cls*))]
680 [else (cons 'case-lambda cls*)]))]
681 [(funcall rator rand*)
682 (let ([rator (E rator)])
683 (cons rator (map E rand*)))]
684 [(forcall rator rand*) `(foreign-call ,rator . ,(map E rand*))]
685 [(assign lhs rhs) `(set! ,(E lhs) ,(E rhs))]
686 [(foreign-label x) `(foreign-label ,x)]
687 [else x]))
688 (E x))
689
690 (define open-mvcalls (make-parameter #t))
691
692 (define (optimize-direct-calls x)
693 (define who 'optimize-direct-calls)
694 (define (make-conses ls)
695 (cond
696 [(null? ls) (make-constant '())]
697 [else
698 (make-funcall (make-primref 'cons)
699 (list (car ls) (make-conses (cdr ls))))]))
700 (define (properize lhs* rhs*)
701 (cond
702 [(null? lhs*) (error who "improper improper")]
703 [(null? (cdr lhs*))
704 (list (make-conses rhs*))]
705 [else (cons (car rhs*) (properize (cdr lhs*) (cdr rhs*)))]))
706 (define (inline-case cls rand*)
707 (struct-case cls
708 [(clambda-case info body)
709 (struct-case info
710 [(case-info label fml* proper)
711 (if proper
712 (and (fx= (length fml*) (length rand*))
713 (make-bind fml* rand* body))
714 (and (fx<= (length fml*) (length rand*))
715 (make-bind fml* (properize fml* rand*) body)))])]))
716 (define (try-inline cls* rand* default)
717 (cond
718 [(null? cls*) default]
719 [(inline-case (car cls*) rand*)]
720 [else (try-inline (cdr cls*) rand* default)]))
721 (define (inline mk rator rand*)
722 (define (valid-mv-consumer? x)
723 (struct-case x
724 [(clambda L cases F)
725 (and (fx= (length cases) 1)
726 (struct-case (car cases)
727 [(clambda-case info body)
728 (struct-case info
729 [(case-info L args proper) proper])]))]
730 [else #f]))
731 (define (single-value-consumer? x)
732 (struct-case x
733 [(clambda L cases F)
734 (and (fx= (length cases) 1)
735 (struct-case (car cases)
736 [(clambda-case info body)
737 (struct-case info
738 [(case-info L args proper)
739 (and proper (fx= (length args) 1))])]))]
740 [else #f]))
741 (define (valid-mv-producer? x)
742 (struct-case x
743 [(funcall) #t]
744 [(conditional) #f]
745 [(bind lhs* rhs* body) (valid-mv-producer? body)]
746 [else #f] ;; FIXME BUG
747 ))
748 (struct-case rator
749 [(clambda g cls*)
750 (try-inline cls* rand*
751 (mk rator rand*))]
752 [(primref op)
753 (case op
754 ;;; FIXME HERE
755 [(call-with-values)
756 (cond
757 [(and (open-mvcalls) (fx= (length rand*) 2))
758 (let ([producer (inline (car rand*) '())]
759 [consumer (cadr rand*)])
760 (cond
761 [(single-value-consumer? consumer)
762 (inline consumer (list producer))]
763 [(and (valid-mv-consumer? consumer)
764 (valid-mv-producer? producer))
765 (make-mvcall producer consumer)]
766 [else
767 (make-funcall rator rand*)]))]
768 [else
769 (mk rator rand*)])]
770 [(debug-call)
771 (inline
772 (lambda (op^ rand*^)
773 (mk rator (cons* (car rand*) op^ rand*^)))
774 (cadr rand*)
775 (cddr rand*))]
776 [else
777 (mk rator rand*)])]
778 [(bind lhs* rhs* body)
779 (if (null? lhs*)
780 (inline mk body rand*)
781 (make-bind lhs* rhs*
782 (call-expr mk body rand*)))]
783 [(recbind lhs* rhs* body)
784 (if (null? lhs*)
785 (inline mk body rand*)
786 (make-recbind lhs* rhs*
787 (call-expr mk body rand*)))]
788 [(rec*bind lhs* rhs* body)
789 (if (null? lhs*)
790 (inline mk body rand*)
791 (make-rec*bind lhs* rhs*
792 (call-expr mk body rand*)))]
793 [else (mk rator rand*)]))
794 (define (call-expr mk x rand*)
795 (cond
796 [(clambda? x) (inline mk x rand*)]
797 [(and (prelex? x) (not (prelex-source-assigned? x)))
798 ;;; FIXME: did we do the analysis yet?
799 (mk x rand*)]
800 [else
801 (let ([t (make-prelex 'tmp #f)])
802 (set-prelex-source-referenced?! t #t)
803 (make-bind (list t) (list x)
804 (mk t rand*)))]))
805 (define (Expr x)
806 (struct-case x
807 [(constant) x]
808 [(prelex) (assert (prelex-source-referenced? x)) x]
809 [(primref) x]
810 [(bind lhs* rhs* body)
811 (make-bind lhs* (map Expr rhs*) (Expr body))]
812 [(recbind lhs* rhs* body)
813 (make-recbind lhs* (map Expr rhs*) (Expr body))]
814 [(rec*bind lhs* rhs* body)
815 (make-rec*bind lhs* (map Expr rhs*) (Expr body))]
816 [(conditional test conseq altern)
817 (make-conditional
818 (Expr test)
819 (Expr conseq)
820 (Expr altern))]
821 [(seq e0 e1)
822 (make-seq (Expr e0) (Expr e1))]
823 [(clambda g cls* cp free name)
824 (make-clambda g
825 (map (lambda (x)
826 (struct-case x
827 [(clambda-case info body)
828 (make-clambda-case info (Expr body))]))
829 cls*)
830 cp free name)]
831 [(funcall rator rand*)
832 (inline make-funcall (Expr rator) (map Expr rand*))]
833 [(forcall rator rand*)
834 (make-forcall rator (map Expr rand*))]
835 [(assign lhs rhs)
836 (assert (prelex-source-assigned? lhs))
837 (make-assign lhs (Expr rhs))]
838 [else (error who "invalid expression" (unparse x))]))
839 (Expr x))
840
841
842 #|
843 (letrec* (bi ...
844 [x (let ([lhs* rhs*] ...) body)]
845 bj ...)
846 body)
847 ===?
848 (letrec* (bi ...
849 [tmp* rhs*] ...
850 [lhs* tmp*] ...
851 [x body]
852 bj ...)
853 body)
854 |#
855
856
857 (include "ikarus.compiler.optimize-letrec.ss")
858 (include "ikarus.compiler.source-optimizer.ss")
859
860 (define (rewrite-assignments x)
861 (define who 'rewrite-assignments)
862 (define (fix-lhs* lhs*)
863 (cond
864 [(null? lhs*) (values '() '() '())]
865 [else
866 (let ([x (car lhs*)])
867 (let-values ([(lhs* a-lhs* a-rhs*) (fix-lhs* (cdr lhs*))])
868 (cond
869 [(and (prelex-source-assigned? x) (not (prelex-global-location x)))
870 (let ([t (make-prelex 'assignment-tmp #f)])
871 (set-prelex-source-referenced?! t #t)
872 (values (cons t lhs*) (cons x a-lhs*) (cons t a-rhs*)))]
873 [else
874 (values (cons x lhs*) a-lhs* a-rhs*)])))]))
875 (define (bind-assigned lhs* rhs* body)
876 (cond
877 [(null? lhs*) body]
878 [else
879 (make-bind lhs*
880 (map (lambda (rhs) (make-funcall (make-primref 'vector) (list rhs))) rhs*)
881 body)]))
882 (define (Expr x)
883 (struct-case x
884 [(constant) x]
885 [(prelex)
886 (cond
887 [(prelex-source-assigned? x)
888 (cond
889 [(prelex-global-location x) =>
890 (lambda (loc)
891 (make-funcall
892 (make-primref '$symbol-value)
893 (list (make-constant loc))))]
894 [else
895 (make-funcall (make-primref '$vector-ref)
896 (list x (make-constant 0)))])]
897 [else x])]
898 [(primref) x]
899 [(bind lhs* rhs* body)
900 (let-values ([(lhs* a-lhs* a-rhs*) (fix-lhs* lhs*)])
901 (make-bind lhs* (map Expr rhs*)
902 (bind-assigned a-lhs* a-rhs* (Expr body))))]
903 [(fix lhs* rhs* body)
904 (make-fix lhs* (map Expr rhs*) (Expr body))]
905 [(conditional test conseq altern)
906 (make-conditional (Expr test) (Expr conseq) (Expr altern))]
907 [(seq e0 e1) (make-seq (Expr e0) (Expr e1))]
908 [(clambda g cls* cp free name)
909 (make-clambda g
910 (map (lambda (cls)
911 (struct-case cls
912 [(clambda-case info body)
913 (struct-case info
914 [(case-info label fml* proper)
915 (let-values ([(fml* a-lhs* a-rhs*) (fix-lhs* fml*)])
916 (make-clambda-case
917 (make-case-info label fml* proper)
918 (bind-assigned a-lhs* a-rhs* (Expr body))))])]))
919 cls*)
920 cp free name)]
921 [(forcall op rand*)
922 (make-forcall op (map Expr rand*))]
923 [(funcall rator rand*)
924 (make-funcall (Expr rator) (map Expr rand*))]
925 [(assign lhs rhs)
926 (cond
927 [(prelex-source-assigned? lhs) =>
928 (lambda (where)
929 (cond
930 [(symbol? where)
931 (make-funcall (make-primref '$init-symbol-value!)
932 (list (make-constant where) (Expr rhs)))]
933 [(prelex-global-location lhs) =>
934 (lambda (loc)
935 (make-funcall (make-primref '$set-symbol-value!)
936 (list (make-constant loc) (Expr rhs))))]
937 [else
938 (make-funcall (make-primref '$vector-set!)
939 (list lhs (make-constant 0) (Expr rhs)))]))]
940 [else
941 (error 'rewrite-assignments "not assigned" lhs x)])]
942 [(mvcall p c) (make-mvcall (Expr p) (Expr c))]
943 [else (error who "invalid expression" (unparse x))]))
944 (Expr x))
945
946 (include "ikarus.compiler.tag-annotation-analysis.ss")
947
948 (define (introduce-vars x)
949 (define who 'introduce-vars)
950 (define (lookup x)
951 (let ([v (prelex-operand x)])
952 (assert (var? v))
953 v))
954 (define (convert-prelex x)
955 (assert (not (var? (prelex-operand x))))
956 (let ([v (unique-var (prelex-name x))])
957 (set-var-referenced! v (prelex-source-referenced? x))
958 (set-var-global-loc! v (prelex-global-location x))
959 (set-prelex-operand! x v)
960 v))
961 (define (A x)
962 (struct-case x
963 [(known x t) (make-known (E x) t)]
964 [else (E x)]))
965 (define (E x)
966 (struct-case x
967 [(constant) x]
968 [(prelex) (lookup x)]
969 [(primref) x]
970 [(bind lhs* rhs* body)
971 (let ([lhs* (map convert-prelex lhs*)])
972 (make-bind lhs* (map E rhs*) (E body)))]
973 [(fix lhs* rhs* body)
974 (let ([lhs* (map convert-prelex lhs*)])
975 (make-fix lhs* (map E rhs*) (E body)))]
976 [(conditional e0 e1 e2)
977 (make-conditional (E e0) (E e1) (E e2))]
978 [(seq e0 e1) (make-seq (E e0) (E e1))]
979 [(clambda g cls* cp free name)
980 (make-clambda g
981 (map
982 (lambda (cls)
983 (struct-case cls
984 [(clambda-case info body)
985 (struct-case info
986 [(case-info label args proper)
987 (let ([args (map convert-prelex args)])
988 (make-clambda-case
989 (make-case-info label args proper)
990 (E body)))])]))
991 cls*)
992 cp free name)]
993 [(primcall rator rand*)
994 (make-primcall rator (map A rand*))]
995 [(funcall rator rand*)
996 (make-funcall (A rator) (map A rand*))]
997 [(forcall rator rand*) (make-forcall rator (map E rand*))]
998 [(assign lhs rhs)
999 (make-assign (lookup lhs) (E rhs))]
1000 [else (error who "invalid expression" (unparse x))]))
1001 (E x))
1002
1003 (define (sanitize-bindings x)
1004 (define who 'sanitize-bindings)
1005 (define (CLambda x)
1006 (struct-case x
1007 [(clambda g cls* cp free name)
1008 (make-clambda g
1009 (map (lambda (cls)
1010 (struct-case cls
1011 [(clambda-case info body)
1012 (struct-case info
1013 [(case-info label fml* proper)
1014 (make-clambda-case
1015 (make-case-info label fml* proper)
1016 (Expr body))])]))
1017 cls*)
1018 cp free name)]))
1019 (define (do-fix lhs* rhs* body)
1020 (if (null? lhs*)
1021 (Expr body)
1022 (make-fix lhs* (map CLambda rhs*) (Expr body))))
1023 (define (A x)
1024 (struct-case x
1025 [(known x t) (make-known (Expr x) t)]
1026 [else (Expr x)]))
1027 (define (Expr x)
1028 (struct-case x
1029 [(constant) x]
1030 [(var) x]
1031 [(primref) x]
1032 [(bind lhs* rhs* body)
1033 (let-values ([(lambda* other*)
1034 (partition
1035 (lambda (x) (clambda? (cdr x)))
1036 (map cons lhs* rhs*))])
1037 (make-bind (map car other*)
1038 (map Expr (map cdr other*))
1039 (do-fix (map car lambda*) (map cdr lambda*)
1040 body)))]
1041 [(fix lhs* rhs* body)
1042 (do-fix lhs* rhs* body)]
1043 [(conditional test conseq altern)
1044 (make-conditional (Expr test) (Expr conseq) (Expr altern))]
1045 [(seq e0 e1) (make-seq (Expr e0) (Expr e1))]
1046 [(clambda g cls* cp free name)
1047 (let ([t (unique-var 'anon)])
1048 (make-fix (list t) (list (CLambda x)) t))]
1049 [(forcall op rand*)
1050 (make-forcall op (map Expr rand*))]
1051 [(funcall rator rand*)
1052 (make-funcall (A rator) (map A rand*))]
1053 [(mvcall p c) (make-mvcall (Expr p) (Expr c))]
1054 [else (error who "invalid expression" (unparse x))]))
1055 (Expr x))
1056
1057
1058 (define (untag x)
1059 (struct-case x
1060 [(known x t) (values x t)]
1061 [else (values x #f)]))
1062
1063 (define (tag x t)
1064 (if t
1065 (make-known x t)
1066 x))
1067
1068 (define (optimize-for-direct-jumps x)
1069 (define who 'optimize-for-direct-jumps)
1070 (define (init-var x)
1071 (set-var-referenced! x #f))
1072 (define (set-var x v)
1073 (struct-case v
1074 [(clambda) (set-var-referenced! x v)]
1075 [(var)
1076 (cond
1077 [(bound-var v) => (lambda (v) (set-var-referenced! x v))]
1078 [else (void)])]
1079 [else (void)]))
1080 (define (bound-var x)
1081 (var-referenced x))
1082 (define (optimize c rator rand*)
1083 (let ([n (length rand*)])
1084 (struct-case c
1085 [(clambda main-label cls*)
1086 (let f ([cls* cls*])
1087 (cond
1088 [(null? cls*)
1089 ;;; none matching?
1090 (make-funcall rator rand*)]
1091 [else
1092 (struct-case (clambda-case-info (car cls*))
1093 [(case-info label fml* proper)
1094 (cond
1095 [proper
1096 (if (fx= n (length fml*))
1097 (make-jmpcall label (strip rator) (map strip rand*))
1098 (f (cdr cls*)))]
1099 [else
1100 (if (fx<= (length (cdr fml*)) n)
1101 (make-jmpcall label (strip rator)
1102 (let f ([fml* (cdr fml*)] [rand* rand*])
1103 (cond
1104 [(null? fml*)
1105 ;;; FIXME: construct list afterwards
1106 (list (make-funcall (make-primref 'list) rand*))]
1107 [else
1108 (cons (strip (car rand*))
1109 (f (cdr fml*) (cdr rand*)))])))
1110 (f (cdr cls*)))])])]))])))
1111 (define (strip x)
1112 (struct-case x
1113 [(known x t) x]
1114 [else x]))
1115 (define (CLambda x)
1116 (struct-case x
1117 [(clambda g cls* cp free name)
1118 (make-clambda g
1119 (map (lambda (cls)
1120 (struct-case cls
1121 [(clambda-case info body)
1122 (for-each init-var (case-info-args info))
1123 (make-clambda-case info (Expr body))]))
1124 cls*)
1125 cp free name)]))
1126 (define (A x)
1127 (struct-case x
1128 [(known x t) (make-known (Expr x) t)]
1129 [else (Expr x)]))
1130 (define (A- x)
1131 (struct-case x
1132 [(known x t) (Expr x)]
1133 [else (Expr x)]))
1134 (define (Expr x)
1135 (struct-case x
1136 [(constant) x]
1137 [(var) x]
1138 [(primref) x]
1139 [(bind lhs* rhs* body)
1140 (for-each init-var lhs*)
1141 (let ([rhs* (map Expr rhs*)])
1142 (for-each set-var lhs* rhs*)
1143 (make-bind lhs* rhs* (Expr body)))]
1144 [(fix lhs* rhs* body)
1145 (for-each set-var lhs* rhs*)
1146 (make-fix lhs* (map CLambda rhs*) (Expr body))]
1147 [(conditional test conseq altern)
1148 (make-conditional (Expr test) (Expr conseq) (Expr altern))]
1149 [(seq e0 e1) (make-seq (Expr e0) (Expr e1))]
1150 [(forcall op rand*)
1151 (make-forcall op (map Expr rand*))]
1152 [(funcall rator rand*)
1153 (let-values ([(rator t) (untag (A rator))])
1154 (cond
1155 [(and (var? rator) (bound-var rator)) =>
1156 (lambda (c)
1157 (optimize c rator (map A rand*)))]
1158 [(and (primref? rator)
1159 (eq? (primref-name rator) '$$apply))
1160 (make-jmpcall (sl-apply-label)
1161 (A- (car rand*))
1162 (map A- (cdr rand*)))]
1163 [else
1164 (make-funcall (tag rator t) (map A rand*))]))]
1165 [else (error who "invalid expression" (unparse x))]))
1166 (Expr x))
1167
1168
1169 (define (insert-global-assignments x)
1170 (define who 'insert-global-assignments)
1171 (define (global-assign lhs* body)
1172 (cond
1173 [(null? lhs*) body]
1174 [(var-global-loc (car lhs*)) =>
1175 (lambda (loc)
1176 (make-seq
1177 (make-funcall (make-primref '$init-symbol-value!)
1178 (list (make-constant loc) (car lhs*)))
1179 (global-assign (cdr lhs*) body)))]
1180 [else (global-assign (cdr lhs*) body)]))
1181 (define (global-fix lhs* body)
1182 (cond
1183 [(null? lhs*) body]
1184 [(var-global-loc (car lhs*)) =>
1185 (lambda (loc)
1186 (make-seq
1187 (make-funcall (make-primref '$set-symbol-value/proc!)
1188 (list (make-constant loc) (car lhs*)))
1189 (global-assign (cdr lhs*) body)))]
1190 [else (global-assign (cdr lhs*) body)]))
1191 (define (A x)
1192 (struct-case x
1193 [(known x t) (make-known (Expr x) t)]
1194 [else (Expr x)]))
1195 (define (Expr x)
1196 (struct-case x
1197 [(constant) x]
1198 [(var)
1199 (cond
1200 [(var-global-loc x) =>
1201 (lambda (loc)
1202 (make-funcall
1203 (make-primref '$symbol-value)
1204 (list (make-constant loc))))]
1205 [else x])]
1206 [(primref) x]
1207 [(bind lhs* rhs* body)
1208 (make-bind lhs* (map Expr rhs*)
1209 (global-assign lhs* (Expr body)))]
1210 [(fix lhs* rhs* body)
1211 (make-fix lhs* (map Expr rhs*)
1212 (global-fix lhs* (Expr body)))]
1213 [(conditional test conseq altern)
1214 (make-conditional (Expr test) (Expr conseq) (Expr altern))]
1215 [(seq e0 e1) (make-seq (Expr e0) (Expr e1))]
1216 [(clambda g cls* cp free name)
1217 (make-clambda g
1218 (map (lambda (cls)
1219 (struct-case cls
1220 [(clambda-case info body)
1221 (make-clambda-case info (Expr body))]))
1222 cls*)
1223 cp free name)]
1224 [(forcall op rand*)
1225 (make-forcall op (map Expr rand*))]
1226 [(funcall rator rand*)
1227 (make-funcall (A rator) (map A rand*))]
1228 [(jmpcall label rator rand*)
1229 (make-jmpcall label (Expr rator) (map Expr rand*))]
1230 [else (error who "invalid expression" (unparse x))]))
1231 (define (AM x)
1232 (struct-case x
1233 [(known x t) (make-known (Main x) t)]
1234 [else (Main x)]))
1235 (define (Main x)
1236 (struct-case x
1237 [(constant) x]
1238 [(var) x]
1239 [(primref) x]
1240 [(bind lhs* rhs* body)
1241 (make-bind lhs* (map Main rhs*)
1242 (global-assign lhs* (Main body)))]
1243 [(fix lhs* rhs* body)
1244 (make-fix lhs* (map Main rhs*)
1245 (global-fix lhs* (Main body)))]
1246 [(conditional test conseq altern)
1247 (make-conditional (Main test) (Main conseq) (Main altern))]
1248 [(seq e0 e1) (make-seq (Main e0) (Main e1))]
1249 [(clambda g cls* cp free name)
1250 (make-clambda g
1251 (map (lambda (cls)
1252 (struct-case cls
1253 [(clambda-case info body)
1254 (make-clambda-case info (Expr body))]))
1255 cls*)
1256 cp free name)]
1257 [(forcall op rand*)
1258 (make-forcall op (map Main rand*))]
1259 [(funcall rator rand*)
1260 (make-funcall (AM rator) (map AM rand*))]
1261 [(jmpcall label rator rand*)
1262 (make-jmpcall label (Main rator) (map Main rand*))]
1263 [else (error who "invalid expression" (unparse x))]))
1264 (let ([x (Main x)])
1265 ;(pretty-print x)
1266 x))
1267
1268
1269
1270 (define optimize-cp (make-parameter #t))
1271
1272 (define (convert-closures prog)
1273 (define who 'convert-closures)
1274 (define (Expr* x*)
1275 (cond
1276 [(null? x*) (values '() '())]
1277 [else
1278 (let-values ([(a a-free) (Expr (car x*))]
1279 [(d d-free) (Expr* (cdr x*))])
1280 (values (cons a d) (union a-free d-free)))]))
1281 (define (do-clambda* lhs* x*)
1282 (cond
1283 [(null? x*) (values '() '())]
1284 [else
1285 (let-values ([(a a-free) (do-clambda (car lhs*) (car x*))]
1286 [(d d-free) (do-clambda* (cdr lhs*) (cdr x*))])
1287 (values (cons a d) (union a-free d-free)))]))
1288 (define (do-clambda lhs x)
1289 (struct-case x
1290 [(clambda g cls* _cp _free name)
1291 (let-values ([(cls* free)
1292 (let f ([cls* cls*])
1293 (cond
1294 [(null? cls*) (values '() '())]
1295 [else
1296 (struct-case (car cls*)
1297 [(clambda-case info body)
1298 (let-values ([(body body-free) (Expr body)]
1299 [(cls* cls*-free) (f (cdr cls*))])
1300 (values
1301 (cons (make-clambda-case info body) cls*)
1302 (union (difference body-free (case-info-args info))
1303 cls*-free)))])]))])
1304 (values
1305 (make-closure
1306 (make-clambda g cls* lhs free name)
1307 free
1308 #f)
1309 free))]))
1310 (define (A x)
1311 (struct-case x
1312 [(known x t)
1313 (let-values ([(x free) (Expr x)])
1314 (values (make-known x t) free))]
1315 [else (Expr x)]))
1316 (define (A* x*)
1317 (cond
1318 [(null? x*) (values '() '())]
1319 [else
1320 (let-values ([(a a-free) (A (car x*))]
1321 [(d d-free) (A* (cdr x*))])
1322 (values (cons a d) (union a-free d-free)))]))
1323 (define (Expr ex)
1324 (struct-case ex
1325 [(constant) (values ex '())]
1326 [(var)
1327 (set-var-index! ex #f)
1328 (values ex (singleton ex))]
1329 [(primref) (values ex '())]
1330 [(bind lhs* rhs* body)
1331 (let-values ([(rhs* rhs-free) (Expr* rhs*)]
1332 [(body body-free) (Expr body)])
1333 (values (make-bind lhs* rhs* body)
1334 (union rhs-free (difference body-free lhs*))))]
1335 [(fix lhs* rhs* body)
1336 (for-each (lambda (x) (set-var-index! x #t)) lhs*)
1337 (let-values ([(rhs* rfree) (do-clambda* lhs* rhs*)]
1338 [(body bfree) (Expr body)])
1339 (for-each
1340 (lambda (lhs rhs)
1341 (when (var-index lhs)
1342 (set-closure-well-known?! rhs #t)
1343 (set-var-index! lhs #f)))
1344 lhs* rhs*)
1345 (values (make-fix lhs* rhs* body)
1346 (difference (union bfree rfree) lhs*)))]
1347 [(conditional test conseq altern)
1348 (let-values ([(test test-free) (Expr test)]
1349 [(conseq conseq-free) (Expr conseq)]
1350 [(altern altern-free) (Expr altern)])
1351 (values (make-conditional test conseq altern)
1352 (union test-free (union conseq-free altern-free))))]
1353 [(seq e0 e1)
1354 (let-values ([(e0 e0-free) (Expr e0)]
1355 [(e1 e1-free) (Expr e1)])
1356 (values (make-seq e0 e1) (union e0-free e1-free)))]
1357 [(forcall op rand*)
1358 (let-values ([(rand* rand*-free) (Expr* rand*)])
1359 (values (make-forcall op rand*) rand*-free))]
1360 [(funcall rator rand*)
1361 (let-values ([(rator rat-free) (A rator)]
1362 [(rand* rand*-free) (A* rand*)])
1363 (values (make-funcall rator rand*)
1364 (union rat-free rand*-free)))]
1365 [(jmpcall label rator rand*)
1366 (let-values ([(rator rat-free)
1367 (if (optimize-cp) (Rator rator) (Expr rator))]
1368 [(rand* rand*-free)
1369 (A* rand*)])
1370 (values (make-jmpcall label rator rand*)
1371 (union rat-free rand*-free)))]
1372 [else (error who "invalid expression" ex)]))
1373 (define (Rator x)
1374 (struct-case x
1375 [(var) (values x (singleton x))]
1376 ;[(known x t)
1377 ; (let-values ([(x free) (Rator x)])
1378 ; (values (make-known x t) free))]
1379 [else (Expr x)]))
1380 (let-values ([(prog free) (Expr prog)])
1381 (unless (null? free)
1382 (error 'convert-closures "free vars encountered in program"
1383 (map unparse free)))
1384 prog))
1385
1386
1387
1388 (define (optimize-closures/lift-codes x)
1389 (define who 'optimize-closures/lift-codes)
1390 (define all-codes '())
1391 (module (unset! set-subst! get-subst copy-subst!)
1392 (define-struct prop (val))
1393 (define (unset! x)
1394 (unless (var? x) (error 'unset! "not a var" x))
1395 (set-var-index! x #f))
1396 (define (set-subst! x v)
1397 (unless (var? x) (error 'set-subst! "not a var" x))
1398 (set-var-index! x (make-prop v)))
1399 (define (copy-subst! lhs rhs)
1400 (unless (var? lhs) (error 'copy-subst! "not a var" lhs))
1401 (cond
1402 [(and (var? rhs) (var-index rhs)) =>
1403 (lambda (v)
1404 (cond
1405 [(prop? v) (set-var-index! lhs v)]
1406 [else (set-var-index! lhs #f)]))]
1407 [else (set-var-index! lhs #f)]))
1408 (define (get-subst x)
1409 (unless (var? x) (error 'get-subst "not a var" x))
1410 (struct-case (var-index x)
1411 [(prop v) v]
1412 [else #f])))
1413 (define (combinator? x)
1414 (struct-case x
1415 [(closure code free*)
1416 (null? free*)]
1417 [else #f]))
1418 (define (lift-code cp code free*)
1419 (struct-case code
1420 [(clambda label cls* cp/dropped free*/dropped name)
1421 (let ([cls* (map
1422 (lambda (x)
1423 (struct-case x
1424 [(clambda-case info body)
1425 (for-each unset! (case-info-args info))
1426 (make-clambda-case info (E body))]))
1427 cls*)])
1428 (let ([g (make-code-loc label)])
1429 (set! all-codes
1430 (cons (make-clambda label cls* cp free* name)
1431 all-codes))
1432 g))]))
1433 (define (trim p? ls)
1434 (cond
1435 [(null? ls) '()]
1436 [(p? (car ls)) (trim p? (cdr ls))]
1437 [else
1438 (cons (car ls) (trim p? (cdr ls)))]))
1439 (define (do-bind lhs* rhs* body)
1440 (for-each unset! lhs*)
1441 (let ([rhs* (map E rhs*)])
1442 (for-each copy-subst! lhs* rhs*)
1443 (let ([body (E body)])
1444 (for-each unset! lhs*)
1445 (make-bind lhs* rhs* body))))
1446 (define (trim-free ls)
1447 (cond
1448 [(null? ls) '()]
1449 [(get-forward! (car ls)) =>
1450 (lambda (what)
1451 (let ([rest (trim-free (cdr ls))])
1452 (struct-case what
1453 [(closure) rest]
1454 [(var) (if (memq what rest) rest (cons what rest))]
1455 [else (error who "invalid value in trim-free" what)])))]
1456 [else (cons (car ls) (trim-free (cdr ls)))]))
1457 (define (do-fix lhs* rhs* body)
1458 (for-each unset! lhs*)
1459 (let ([free** ;;; trim the free lists first; after init.
1460 (map (lambda (lhs rhs)
1461 ;;; remove self also
1462 (remq lhs (trim-free (closure-free* rhs))))
1463 lhs* rhs*)])
1464 (define-struct node (name code deps whacked free wk?))
1465 (let ([node*
1466 (map (lambda (lhs rhs)
1467 (let ([n (make-node lhs (closure-code rhs) '() #f '()
1468 (closure-well-known? rhs))])
1469 (set-subst! lhs n)
1470 n))
1471 lhs* rhs*)])
1472 ;;; if x is free in y, then whenever x becomes a non-combinator,
1473 ;;; y also becomes a non-combinator. Here, we mark these
1474 ;;; dependencies.
1475 (for-each
1476 (lambda (my-node free*)
1477 (for-each (lambda (fvar)
1478 (cond
1479 [(get-subst fvar) => ;;; one of ours
1480 (lambda (her-node)
1481 (set-node-deps! her-node
1482 (cons my-node (node-deps her-node))))]
1483 [else ;;; not one of ours
1484 (set-node-free! my-node
1485 (cons fvar (node-free my-node)))]))
1486 free*))
1487 node* free**)
1488 ;;; Next, we go over the list of nodes, and if we find one
1489 ;;; that has any free variables, we know it's a non-combinator,
1490 ;;; so we whack it and add it to all of its dependents.
1491 (let ()
1492 (define (process-node x)
1493 (when (cond
1494 [(null? (node-free x)) #f]
1495 ;[(and (node-wk? x) (null? (cdr (node-free x)))) #f]
1496 [else #t])
1497 (unless (node-whacked x)
1498 (set-node-whacked! x #t)
1499 (for-each
1500 (lambda (y)
1501 (set-node-free! y
1502 (cons (node-name x) (node-free y)))
1503 (process-node y))
1504 (node-deps x)))))
1505 (for-each process-node node*))
1506 ;;; Now those that have free variables are actual closures.
1507 ;;; Those with no free variables are actual combinators.
1508 (let ([rhs*
1509 (map
1510 (lambda (node)
1511 (let ([wk? (node-wk? node)]
1512 [name (node-name node)]
1513 [free (node-free node)])
1514 (let ([closure
1515 (make-closure (node-code node) free wk?)])
1516 (cond
1517 [(null? free)
1518 (set-subst! name closure)]
1519 [(and (null? (cdr free)) wk?)
1520 (set-subst! name closure)]
1521 [else
1522 (unset! name)])
1523 closure)))
1524 node*)])
1525 (for-each
1526 (lambda (lhs^ closure)
1527 (let* ([lhs (get-forward! lhs^)]
1528 [free
1529 (filter var?
1530 (remq lhs (trim-free (closure-free* closure))))])
1531 (set-closure-free*! closure free)
1532 (set-closure-code! closure
1533 (lift-code
1534 lhs
1535 (closure-code closure)
1536 (closure-free* closure)))))
1537 lhs*
1538 rhs*)
1539 (let ([body (E body)])
1540 (let f ([lhs* lhs*] [rhs* rhs*] [l* '()] [r* '()])
1541 (cond
1542 [(null? lhs*)
1543 (if (null? l*)
1544 body
1545 (make-fix l* r* body))]
1546 [else
1547 (let ([lhs (car lhs*)] [rhs (car rhs*)])
1548 (cond
1549 [(get-subst lhs)
1550 (unset! lhs)
1551 (f (cdr lhs*) (cdr rhs*) l* r*)]
1552 [else
1553 (f (cdr lhs*) (cdr rhs*)
1554 (cons lhs l*) (cons rhs r*))]))])))))))
1555 (define (get-forward! x)
1556 (when (eq? x 'q)
1557 (error who "BUG: circular dep"))
1558 (let ([y (get-subst x)])
1559 (cond
1560 [(not y) x]
1561 [(var? y)
1562 (set-subst! x 'q)
1563 (let ([y (get-forward! y)])
1564 (set-subst! x y)
1565 y)]
1566 [(closure? y)
1567 (let ([free (closure-free* y)])
1568 (cond
1569 [(null? free) y]
1570 [(null? (cdr free))
1571 (set-subst! x 'q)
1572 (let ([y (get-forward! (car free))])
1573 (set-subst! x y)
1574 y)]
1575 [else y]))]
1576 [else x])))
1577 (define (A x)
1578 (struct-case x
1579 [(known x t) (make-known (E x) t)]
1580 [else (E x)]))
1581 (define (E x)
1582 (struct-case x
1583 [(constant) x]
1584 [(var) (get-forward! x)]
1585 [(primref) x]
1586 [(bind lhs* rhs* body) (do-bind lhs* rhs* body)]
1587 [(fix lhs* rhs* body) (do-fix lhs* rhs* body)]
1588 [(conditional test conseq altern)
1589 (make-conditional (E test) (E conseq) (E altern))]
1590 [(seq e0 e1) (make-seq (E e0) (E e1))]
1591 [(forcall op rand*) (make-forcall op (map E rand*))]
1592 [(funcall rator rand*) (make-funcall (A rator) (map A rand*))]
1593 [(jmpcall label rator rand*)
1594 (make-jmpcall label (E rator) (map E rand*))]
1595 [else (error who "invalid expression" (unparse x))]))
1596 ;(when (optimize-cp)
1597 ; (printf "BEFORE\n")
1598 ; (parameterize ([pretty-width 200])
1599 ; (pretty-print (unparse x))))
1600 (let ([x (E x)])
1601 (let ([v (make-codes all-codes x)])
1602 ;(when (optimize-cp)
1603 ; (printf "AFTER\n")
1604 ; (parameterize ([pretty-width 200])
1605 ; (pretty-print (unparse v))))
1606 v)))
1607
1608
1609
1610 (begin ;;; DEFINITIONS
1611 (module (wordsize)
1612 (import (ikarus include))
1613 (include "ikarus.config.ss"))
1614 (define wordshift
1615 (case wordsize
1616 [(4) 2]
1617 [(8) 3]
1618 [else
1619 (error 'ikarus "wordsize is neither 4 nor 8" wordsize)]))
1620 (define fx-scale wordsize)
1621 (define object-alignment (* 2 wordsize))
1622 (define align-shift (+ wordshift 1))
1623 (define fx-shift wordshift)
1624 (define fx-mask (- wordsize 1))
1625 (define fx-tag 0)
1626 (define bool-f #x2F)
1627 (define bool-t #x3F)
1628 (define bool-mask #xEF)
1629 (define bool-tag #x2F)
1630 (define bool-shift 4)
1631 (define nil #x4F)
1632 (define eof #x5F) ; double check
1633 (define unbound #x6F) ; double check
1634 (define void-object #x7F) ; double check
1635 (define bwp-object #x8F) ; double check
1636 (define char-size 4)
1637 (define char-shift 8)
1638 (define char-tag #x0F)
1639 (define char-mask #xFF)
1640 (define pair-mask 7)
1641 (define pair-tag 1)
1642 (define disp-car 0)
1643 (define disp-cdr wordsize)
1644 (define pair-size (* 2 wordsize))
1645
1646 (define flonum-tag #x17)
1647 (define flonum-size 16)
1648 (define disp-flonum-data 8)
1649
1650 (define ratnum-tag #x27)
1651 (define disp-ratnum-num (* 1 wordsize))
1652 (define disp-ratnum-den (* 2 wordsize))
1653 (define ratnum-size (* 4 wordsize))
1654
1655 (define compnum-tag #x37)
1656 (define disp-compnum-real (* 1 wordsize))
1657 (define disp-compnum-imag (* 2 wordsize))
1658 (define compnum-size (* 4 wordsize))
1659
1660 (define cflonum-tag #x47)
1661 (define disp-cflonum-real (* 1 wordsize))
1662 (define disp-cflonum-imag (* 2 wordsize))
1663 (define cflonum-size (* 4 wordsize))
1664
1665 (define bignum-mask #b111)
1666 (define bignum-tag #b011)
1667 (define bignum-sign-mask #b1000)
1668 (define bignum-sign-shift 3)
1669 (define bignum-length-shift 4)
1670 (define disp-bignum-data wordsize)
1671
1672 (define pagesize 4096)
1673 (define pageshift 12)
1674
1675 (define bytevector-mask 7)
1676 (define bytevector-tag 2)
1677 (define disp-bytevector-length 0)
1678 (define disp-bytevector-data 8)
1679
1680 (define ptag-mask 7)
1681 (define symbol-ptag 5)
1682 (define symbol-record-tag #x5F)
1683 (define disp-symbol-record-string (* 1 wordsize))
1684 (define disp-symbol-record-ustring (* 2 wordsize))
1685 (define disp-symbol-record-value (* 3 wordsize))
1686 (define disp-symbol-record-proc (* 4 wordsize))
1687 (define disp-symbol-record-plist (* 5 wordsize))
1688 (define symbol-record-size (* 6 wordsize))
1689
1690 (define record-tag 5)
1691 (define record-mask 7)
1692
1693 (define vector-tag 5)
1694 (define vector-mask 7)
1695 (define disp-vector-length 0)
1696 (define disp-vector-data wordsize)
1697 (define string-mask 7)
1698 (define string-tag 6)
1699 (define disp-string-length 0)
1700 (define disp-string-data wordsize)
1701 (define closure-mask 7)
1702 (define closure-tag 3)
1703 (define disp-closure-code 0)
1704 (define disp-closure-data wordsize)
1705 (define continuation-tag #x1F)
1706 (define disp-continuation-top (* 1 wordsize))
1707 (define disp-continuation-size (* 2 wordsize))
1708 (define disp-continuation-next (* 3 wordsize))
1709 (define continuation-size (* 4 wordsize))
1710 (define code-tag #x2F)
1711 (define disp-code-instrsize (* 1 wordsize))
1712 (define disp-code-relocsize (* 2 wordsize))
1713 (define disp-code-freevars (* 3 wordsize))
1714 (define disp-code-annotation (* 4 wordsize))
1715 (define disp-code-unused (* 5 wordsize))
1716 (define disp-code-data (* 6 wordsize))
1717
1718 (define transcoder-mask #xFF) ;;; 0011
1719 (define transcoder-tag #x7F) ;;; 0011
1720 (define transcoder-payload-shift 10)
1721
1722 (define transcoder-write-utf8-mask #x1000)
1723 (define transcoder-write-byte-mask #x2000)
1724 (define transcoder-read-utf8-mask #x4000)
1725 (define transcoder-read-byte-mask #x8000)
1726 (define transcoder-handling-mode-shift 16)
1727 (define transcoder-handling-mode-bits 2)
1728 (define transcoder-eol-style-shift 18)
1729 (define transcoder-eol-style-bits 3)
1730 (define transcoder-codec-shift 21)
1731 (define transcoder-codec-bits 3)
1732
1733 (define transcoder-handling-mode:none #b00)
1734 (define transcoder-handling-mode:ignore #b01)
1735 (define transcoder-handling-mode:raise #b10)
1736 (define transcoder-handling-mode:replace #b11)
1737
1738 (define transcoder-eol-style:none #b000)
1739 (define transcoder-eol-style:lf #b001)
1740 (define transcoder-eol-style:cr #b010)
1741 (define transcoder-eol-style:crlf #b011)
1742 (define transcoder-eol-style:nel #b100)
1743 (define transcoder-eol-style:crnel #b101)
1744 (define transcoder-eol-style:ls #b110)
1745
1746 (define transcoder-codec:none #b000)
1747 (define transcoder-codec:latin-1 #b001)
1748 (define transcoder-codec:utf-8 #b010)
1749 (define transcoder-codec:utf-16 #b011)
1750
1751 (define port-tag #x3F)
1752 (define port-mask #x3F)
1753 (define disp-port-attrs 0)
1754 (define disp-port-index (* 1 wordsize))
1755 (define disp-port-size (* 2 wordsize))
1756 (define disp-port-buffer (* 3 wordsize))
1757 (define disp-port-transcoder (* 4 wordsize))
1758 (define disp-port-id (* 5 wordsize))
1759 (define disp-port-read! (* 6 wordsize))
1760 (define disp-port-write! (* 7 wordsize))
1761 (define disp-port-get-position (* 8 wordsize))
1762 (define disp-port-set-position! (* 9 wordsize))
1763 (define disp-port-close (* 10 wordsize))
1764 (define disp-port-cookie (* 11 wordsize))
1765 (define disp-port-unused1 (* 12 wordsize))
1766 (define disp-port-unused2 (* 13 wordsize))
1767 (define port-size (* 14 wordsize))
1768
1769 (define disp-tcbucket-tconc 0)
1770 (define disp-tcbucket-key (* 1 wordsize))
1771 (define disp-tcbucket-val (* 2 wordsize))
1772 (define disp-tcbucket-next (* 3 wordsize))
1773 (define tcbucket-size (* 4 wordsize))
1774 (define record-ptag 5)
1775 (define record-pmask 7)
1776 (define disp-struct-rtd 0)
1777 (define disp-struct-data wordsize)
1778
1779 ;;; refer to the picture in src/ikarus-collect.c for details
1780 ;;; on how call-frames are laid out. (search for livemask)
1781 (define call-instruction-size
1782 (case wordsize
1783 [(4) 5]
1784 [(8) 10]
1785 [else (die 'call-instruction-size "invalid" wordsize)]))
1786 (define disp-frame-size (- (+ call-instruction-size (* 3 wordsize))))
1787 (define disp-frame-offset (- (+ call-instruction-size (* 2 wordsize))))
1788 (define disp-multivalue-rp (- (+ call-instruction-size (* 1 wordsize))))
1789
1790 (define dirty-word -1))
1791
1792 ;(define pcb-allocation-pointer (* 0 wordsize)) NOT USED
1793 (define pcb-allocation-redline (* 1 wordsize))
1794 ;(define pcb-frame-pointer (* 2 wordsize)) NOT USED
1795 (define pcb-frame-base (* 3 wordsize))
1796 (define pcb-frame-redline (* 4 wordsize))
1797 (define pcb-next-continuation (* 5 wordsize))
1798 ;(define pcb-system-stack (* 6 wordsize)) NOT USED
1799 (define pcb-dirty-vector (* 7 wordsize))
1800 (define pcb-arg-list (* 8 wordsize))
1801 (define pcb-engine-counter (* 9 wordsize))
1802 (define pcb-interrupted (* 10 wordsize))
1803 (define pcb-base-rtd (* 11 wordsize))
1804 (define pcb-collect-key (* 12 wordsize))
1805
1806
1807 (define (fx? x)
1808 (let* ([intbits (* wordsize 8)]
1809 [fxbits (- intbits fx-shift)])
1810 (and (or (fixnum? x) (bignum? x))
1811 (<= (- (expt 2 (- fxbits 1)))
1812 x
1813 (- (expt 2 (- fxbits 1)) 1)))))
1814
1815
1816 (module ()
1817 ;;; initialize the cogen
1818 (code-entry-adjustment (- disp-code-data vector-tag)))
1819
1820 (begin ;;; COGEN HELERS
1821 (define (align n)
1822 (fxsll (fxsra (fx+ n (fxsub1 object-alignment)) align-shift) align-shift))
1823 (define (mem off val)
1824 (cond
1825 [(fixnum? off) (list 'disp (int off) val)]
1826 [(register? off) (list 'disp off val)]
1827 [else (error 'mem "invalid disp" off)]))
1828 (define-syntax int
1829 (syntax-rules ()
1830 [(_ x) x]))
1831 (define (obj x) (list 'obj x))
1832 (define (byte x) (list 'byte x))
1833 (define (byte-vector x) (list 'byte-vector x))
1834 (define (movzbl src targ) (list 'movzbl src targ))
1835 (define (sall src targ) (list 'sall src targ))
1836 (define (sarl src targ) (list 'sarl src targ))
1837 (define (shrl src targ) (list 'shrl src targ))
1838 (define (notl src) (list 'notl src))
1839 (define (pushl src) (list 'pushl src))
1840 (define (popl src) (list 'popl src))
1841 (define (orl src targ) (list 'orl src targ))
1842 (define (xorl src targ) (list 'xorl src targ))
1843 (define (andl src targ) (list 'andl src targ))
1844 (define (movl src targ) (list 'movl src targ))
1845 (define (leal src targ) (list 'leal src targ))
1846 (define (movb src targ) (list 'movb src targ))
1847 (define (addl src targ) (list 'addl src targ))
1848 (define (imull src targ) (list 'imull src targ))
1849 (define (idivl src) (list 'idivl src))
1850 (define (subl src targ) (list 'subl src targ))
1851 (define (push src) (list 'push src))
1852 (define (pop targ) (list 'pop targ))
1853 (define (sete targ) (list 'sete targ))
1854 (define (call targ) (list 'call targ))
1855 (define (tail-indirect-cpr-call)
1856 (jmp (mem (fx- disp-closure-code closure-tag) cpr)))
1857 (define (indirect-cpr-call)
1858 (call (mem (fx- disp-closure-code closure-tag) cpr)))
1859 (define (negl targ) (list 'negl targ))
1860 (define (label x) (list 'label x))
1861 (define (label-address x) (list 'label-address x))
1862 (define (ret) '(ret))
1863 (define (cltd) '(cltd))
1864 (define (cmpl arg1 arg2) (list 'cmpl arg1 arg2))
1865 (define (je label) (list 'je label))
1866 (define (jne label) (list 'jne label))
1867 (define (jle label) (list 'jle label))
1868 (define (jge label) (list 'jge label))
1869 (define (jg label) (list 'jg label))
1870 (define (jl label) (list 'jl label))
1871 (define (jb label) (list 'jb label))
1872 (define (ja label) (list 'ja label))
1873 (define (jo label) (list 'jo label))
1874 (define (jmp label) (list 'jmp label))
1875 (define esp '%esp) ; stack base pointer
1876 (define al '%al)
1877 (define ah '%ah)
1878 (define bh '%bh)
1879 (define cl '%cl)
1880 (define eax '%eax)
1881 (define ebx '%ebx)
1882 (define ecx '%ecx)
1883 (define edx '%edx)
1884 (define apr '%ebp) ; allocation pointer
1885 (define fpr '%esp) ; frame pointer
1886 (define cpr '%edi) ; closure pointer
1887 (define pcr '%esi) ; pcb pointer
1888 (define register? symbol?)
1889 (define (argc-convention n)
1890 (fx- 0 (fxsll n fx-shift))))
1891
1892
1893 (define (primref->symbol op)
1894 (unless (symbol? op) (error 'primref->symbol "not a symbol" op))
1895 (cond
1896 [((current-primitive-locations) op) =>
1897 (lambda (x)
1898 (unless (symbol? x)
1899 (error 'primitive-location
1900 "not a valid location for ~s" x op))
1901 x)]
1902 [else
1903 (let ()
1904 (define-condition-type &url &condition
1905 make-url-condition
1906 url-condition?
1907 (url condition-url))
1908 (raise
1909 (condition
1910 (make-error)
1911 (make-who-condition 'ikarus)
1912 (make-message-condition "primitive not supported yet")
1913 (make-message-condition
1914 "please file a bug report to help us prioritize our goals")
1915 (make-url-condition
1916 "https://bugs.launchpad.net/ikarus/+filebug")
1917 (make-irritants-condition (list op)))))]))
1918
1919 ;(define (primref-loc op)
1920 ; (mem (fx- disp-symbol-record-proc record-tag)
1921 ; (obj (primref->symbol op))))
1922
1923
1924
1925 (module ;assembly-labels
1926 (refresh-cached-labels!
1927 sl-annotated-procedure-label
1928 sl-apply-label
1929 sl-continuation-code-label
1930 sl-invalid-args-label
1931 sl-mv-ignore-rp-label
1932 sl-mv-error-rp-label
1933 sl-values-label
1934 sl-cwv-label)
1935 (define-syntax define-cached
1936 (lambda (x)
1937 (syntax-case x ()
1938 [(_ refresh [(name*) b* b** ...] ...)
1939 (with-syntax ([(v* ...) (generate-temporaries #'(name* ...))])
1940 #'(begin
1941 (define v* #f) ...
1942 (define (name*)
1943 (or v* (error 'name* "uninitialized label"))) ...
1944 (define (refresh)
1945 (define-syntax name*
1946 (lambda (stx)
1947 (syntax-error stx
1948 "cannot use label before it is defined")))
1949 ...
1950 (let* ([name* (let ([label (let () b* b** ...)])
1951 (set! v* label)
1952 (lambda () label))] ...)
1953 (void)))))])))
1954 (define-cached refresh-cached-labels!
1955 [(sl-annotated-procedure-label)
1956 (import (ikarus.code-objects))
1957 (define SL_annotated (gensym "SL_annotated"))
1958 (assemble-sources (lambda (x) #f)
1959 (list
1960 (list 2
1961 `(name ,(make-annotation-indirect))
1962 (label SL_annotated)
1963 (movl (mem (fx- (fx+ disp-closure-data wordsize) closure-tag) cpr) cpr)
1964 (tail-indirect-cpr-call))))
1965 SL_annotated]
1966 [(sl-apply-label)
1967 (let ([SL_apply (gensym "SL_apply")]
1968 [L_apply_done (gensym)]
1969 [L_apply_loop (gensym)])
1970 (assemble-sources (lambda (x) #f)
1971 (list
1972 (list 0
1973 (label SL_apply)
1974 (movl (mem fpr eax) ebx)
1975 (cmpl (int nil) ebx)
1976 (je (label L_apply_done))
1977 (label L_apply_loop)
1978 (movl (mem (fx- disp-car pair-tag) ebx) ecx)
1979 (movl (mem (fx- disp-cdr pair-tag) ebx) ebx)
1980 (movl ecx (mem fpr eax))
1981 (subl (int wordsize) eax)
1982 (cmpl (int nil) ebx)
1983 (jne (label L_apply_loop))
1984 (label L_apply_done)
1985 (addl (int wordsize) eax)
1986 (tail-indirect-cpr-call))))
1987 SL_apply)]
1988 [(sl-continuation-code-label)
1989 (define SL_continuation_code (gensym "SL_continuation_code"))
1990 (assemble-sources (lambda (x) #f)
1991 (list
1992 (let ([L_cont_zero_args (gensym)]
1993 [L_cont_mult_args (gensym)]
1994 [L_cont_one_arg (gensym)]
1995 [L_cont_mult_move_args (gensym)]
1996 [L_cont_mult_copy_loop (gensym)])
1997 (list 1 ; freevars
1998 (label SL_continuation_code)
1999 (movl (mem (fx- disp-closure-data closure-tag) cpr) ebx) ; captured-k
2000 (movl ebx (mem pcb-next-continuation pcr)) ; set
2001 (movl (mem pcb-frame-base pcr) ebx)
2002 (cmpl (int (argc-convention 1)) eax)
2003 (jg (label L_cont_zero_args))
2004 (jl (label L_cont_mult_args))
2005 (label L_cont_one_arg)
2006 (movl (mem (fx- 0 wordsize) fpr) eax)
2007 (movl ebx fpr)
2008 (subl (int wordsize) fpr)
2009 (ret)
2010 (label L_cont_zero_args)
2011 (subl (int wordsize) ebx)
2012 (movl ebx fpr)
2013 (movl (mem 0 ebx) ebx) ; return point
2014 (jmp (mem disp-multivalue-rp ebx)) ; go
2015 (label L_cont_mult_args)
2016 (subl (int wordsize) ebx)
2017 (cmpl ebx fpr)
2018 (jne (label L_cont_mult_move_args))
2019 (movl (mem 0 ebx) ebx)
2020 (jmp (mem disp-multivalue-rp ebx))
2021 (label L_cont_mult_move_args)
2022 ; move args from fpr to ebx
2023 (movl (int 0) ecx)
2024 (label L_cont_mult_copy_loop)
2025 (subl (int wordsize) ecx)
2026 (movl (mem fpr ecx) edx)
2027 (movl edx (mem ebx ecx))
2028 (cmpl ecx eax)
2029 (jne (label L_cont_mult_copy_loop))
2030 (movl ebx fpr)
2031 (movl (mem 0 ebx) ebx)
2032 (jmp (mem disp-multivalue-rp ebx))))))
2033 SL_continuation_code]
2034 [(sl-invalid-args-label)
2035 (define SL_invalid_args (gensym "SL_invalid_args"))
2036 (assemble-sources (lambda (x) #f)
2037 (list
2038 (list 0
2039 (label SL_invalid_args)
2040 ;;;
2041 (movl cpr (mem (fx- 0 wordsize) fpr)) ; first arg
2042 (negl eax)
2043 (movl eax (mem (fx- 0 (fx* 2 wordsize)) fpr))
2044 (movl (obj (primref->symbol '$incorrect-args-error-handler)) cpr)
2045 (movl (mem (- disp-symbol-record-proc record-tag) cpr) cpr)
2046 ;(movl (primref-loc '$incorrect-args-error-handler) cpr)
2047 (movl (int (argc-convention 2)) eax)
2048 (tail-indirect-cpr-call))))
2049 SL_invalid_args]
2050 [(sl-mv-ignore-rp-label)
2051 (define SL_multiple_values_ignore_rp (gensym "SL_multiple_ignore_error_rp"))
2052 (assemble-sources (lambda (x) #f)
2053 (list
2054 (list 0
2055 (label SL_multiple_values_ignore_rp)
2056 (ret))))
2057 SL_multiple_values_ignore_rp]
2058 [(sl-mv-error-rp-label)
2059 (define SL_multiple_values_error_rp (gensym "SL_multiple_values_error_rp"))
2060 (assemble-sources (lambda (x) #f)
2061 (list
2062 (list 0
2063 (label SL_multiple_values_error_rp)
2064 (movl (obj (primref->symbol '$multiple-values-error)) cpr)
2065 (movl (mem (- disp-symbol-record-proc record-tag) cpr) cpr)
2066 ;(movl (primref-loc '$multiple-values-error) cpr)
2067 (tail-indirect-cpr-call))))
2068 SL_multiple_values_error_rp]
2069 [(sl-values-label)
2070 (define SL_values (gensym "SL_values"))
2071 (assemble-sources (lambda (x) #f)
2072 (list
2073 (let ([L_values_one_value (gensym)]
2074 [L_values_many_values (gensym)])
2075 (list 0 ; no freevars
2076 '(name values)
2077 (label SL_values)
2078 (cmpl (int (argc-convention 1)) eax)
2079 (je (label L_values_one_value))
2080 (label L_values_many_values)
2081 (movl (mem 0 fpr) ebx) ; return point
2082 (jmp (mem disp-multivalue-rp ebx)) ; go
2083 (label L_values_one_value)
2084 (movl (mem (fx- 0 wordsize) fpr) eax)
2085 (ret)))))
2086 SL_values]
2087 [(sl-cwv-label)
2088 (define SL_call_with_values (gensym "SL_call_with_values"))
2089 (assemble-sources (lambda (x) #f)
2090 (list
2091 (let ([L_cwv_done (gensym)]
2092 [L_cwv_loop (gensym)]
2093 [L_cwv_multi_rp (gensym)]
2094 [L_cwv_call (gensym)]
2095 [SL_nonprocedure (gensym "SL_nonprocedure")]
2096 [SL_invalid_args (gensym "SL_invalid_args")])
2097 (list
2098 0 ; no free vars
2099 '(name call-with-values)
2100 (label SL_call_with_values)
2101 (cmpl (int (argc-convention 2)) eax)
2102 (jne (label SL_invalid_args))
2103 (movl (mem (fx- 0 wordsize) fpr) ebx) ; producer
2104 (movl ebx cpr)
2105 (andl (int closure-mask) ebx)
2106 (cmpl (int closure-tag) ebx)
2107 (jne (label SL_nonprocedure))
2108 (movl (int (argc-convention 0)) eax)
2109 (compile-call-frame
2110 3
2111 '#(#b110)
2112 (label-address L_cwv_multi_rp)
2113 (indirect-cpr-call))
2114 ;;; one value returned
2115 (movl (mem (fx* -2 wordsize) fpr) ebx) ; consumer
2116 (movl ebx cpr)
2117 (movl eax (mem (fx- 0 wordsize) fpr))
2118 (movl (int (argc-convention 1)) eax)
2119 (andl (int closure-mask) ebx)
2120 (cmpl (int closure-tag) ebx)
2121 (jne (label SL_nonprocedure))
2122 (tail-indirect-cpr-call)
2123 ;;; multiple values returned
2124 (label L_cwv_multi_rp)
2125 ; because values does not pop the return point
2126 ; we have to adjust fp one more word here
2127 (addl (int (fx* wordsize 3)) fpr)
2128 (movl (mem (fx* -2 wordsize) fpr) cpr) ; consumer
2129 (cmpl (int (argc-convention 0)) eax)
2130 (je (label L_cwv_done))
2131 (movl (int (fx* -4 wordsize)) ebx)
2132 (addl fpr ebx) ; ebx points to first value
2133 (movl ebx ecx)
2134 (addl eax ecx) ; ecx points to the last value
2135 (label L_cwv_loop)
2136 (movl (mem 0 ebx) edx)
2137 (movl edx (mem (fx* 3 wordsize) ebx))
2138 (subl (int wordsize) ebx)
2139 (cmpl ecx ebx)
2140 (jge (label L_cwv_loop))
2141 (label L_cwv_done)
2142 (movl cpr ebx)
2143 (andl (int closure-mask) ebx)
2144 (cmpl (int closure-tag) ebx)
2145 (jne (label SL_nonprocedure))
2146 (tail-indirect-cpr-call)
2147
2148 (label SL_nonprocedure)
2149 (movl cpr (mem (fx- 0 wordsize) fpr)) ; first arg
2150 (movl (obj (primref->symbol '$apply-nonprocedure-error-handler)) cpr)
2151 (movl (mem (- disp-symbol-record-proc record-tag) cpr) cpr)
2152 (movl (int (argc-convention 1)) eax)
2153 (tail-indirect-cpr-call)
2154
2155 (label SL_invalid_args)
2156 ;;;
2157 (movl cpr (mem (fx- 0 wordsize) fpr)) ; first arg
2158 (negl eax)
2159 (movl eax (mem (fx- 0 (fx* 2 wordsize)) fpr))
2160 (movl (obj (primref->symbol '$incorrect-args-error-handler)) cpr)
2161 (movl (mem (- disp-symbol-record-proc record-tag) cpr) cpr)
2162 (movl (int (argc-convention 2)) eax)
2163 (tail-indirect-cpr-call)
2164
2165 ))))
2166 SL_call_with_values]
2167 ))
2168
2169 (define (print-instr x)
2170 (cond
2171 [(and (pair? x) (eq? (car x) 'seq))
2172 (for-each print-instr (cdr x))]
2173 [else
2174 (printf " ~s\n" x)]))
2175
2176 (define optimizer-output (make-parameter #f))
2177 (define perform-tag-analysis (make-parameter #t))
2178
2179 (define (compile-core-expr->code p)
2180 (let* ([p (recordize p)]
2181 [p (parameterize ([open-mvcalls #f])
2182 (optimize-direct-calls p))]
2183 [p (optimize-letrec p)]
2184 [p (source-optimize p)]
2185 [dummy
2186 (begin
2187 (when (optimizer-output)
2188 (pretty-print (unparse-pretty p)))
2189 #f)]
2190 [p (rewrite-assignments p)]
2191 [p (if (perform-tag-analysis)
2192 (introduce-tags p)
2193 p)]
2194 [p (introduce-vars p)]
2195 [p (sanitize-bindings p)]
2196 [p (optimize-for-direct-jumps p)]
2197 [p (insert-global-assignments p)]
2198 [p (convert-closures p)]
2199 [p (optimize-closures/lift-codes p)])
2200 (let ([ls* (alt-cogen p)])
2201 (when (assembler-output)
2202 (parameterize ([gensym-prefix "L"]
2203 [print-gensym #f])
2204 (for-each
2205 (lambda (ls)
2206 (newline)
2207 (for-each print-instr ls))
2208 ls*)))
2209 (let ([code*
2210 (assemble-sources
2211 (lambda (x)
2212 (if (closure? x)
2213 (if (null? (closure-free* x))
2214 (code-loc-label (closure-code x))
2215 (error 'compile "BUG: non-thunk escaped" x))
2216 #f))
2217 ls*)])
2218 (car code*)))))
2219
2220 (define compile-core-expr-to-port
2221 (lambda (expr port)
2222 (fasl-write (compile-core-expr->code expr) port)))
2223
2224
2225 (define (compile-core-expr x)
2226 (let ([code (compile-core-expr->code x)])
2227 ($code->closure code)))
2228
2229 (define assembler-output (make-parameter #f))
2230
2231 (define current-core-eval
2232 (make-parameter
2233 (lambda (x) ((compile-core-expr x)))
2234 (lambda (x)
2235 (if (procedure? x)
2236 x
2237 (die 'current-core-eval "not a procedure" x)))))
2238
2239 (define eval-core
2240 (lambda (x)
2241 ((current-core-eval) x)))
2242
2243 (include "ikarus.compiler.altcogen.ss")
2244
2245 (define current-primitive-locations
2246 (let ([plocs (lambda (x) #f)])
2247 (case-lambda
2248 [() plocs]
2249 [(p)
2250 (if (procedure? p)
2251 (begin
2252 (set! plocs p)
2253 (refresh-cached-labels!))
2254 (error 'current-primitive-locations "not a procedure" p))])))
2255
2256 (define (expand/pretty x env who . passes)
2257 (unless (environment? env)
2258 (die who "not an environment" env))
2259 (let-values ([(x libs) (core-expand x env)])
2260 (let f ([x (recordize x)] [passes passes])
2261 (if (null? passes)
2262 (unparse-pretty x)
2263 (f ((car passes) x) (cdr passes))))))
2264
2265
2266 (define expand/scc-letrec
2267 (case-lambda
2268 [(x) (expand/scc-letrec x (interaction-environment))]
2269 [(x env)
2270 (expand/pretty x env 'expand/scc-letrec
2271 (lambda (x)
2272 (parameterize ([open-mvcalls #f])
2273 (optimize-direct-calls x)))
2274 (lambda (x)
2275 (parameterize ([debug-scc #t])
2276 (optimize-letrec x))))]))
2277
2278 (define expand/optimize
2279 (case-lambda
2280 [(x) (expand/optimize x (interaction-environment))]
2281 [(x env)
2282 (expand/pretty x env 'expand/optimize
2283 (lambda (x)
2284 (parameterize ([open-mvcalls #f])
2285 (optimize-direct-calls x)))
2286 optimize-letrec
2287 source-optimize)]))
2288
2289 (define expand
2290 (case-lambda
2291 [(x) (expand x (interaction-environment))]
2292 [(x env) (expand/pretty x env 'expand)]))
2293
2294 )
2295
2296