Debian Codegraph @

scheme/ikarus.compiler.altcogen.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 (module (alt-cogen compile-call-frame)
18 ;;; input to cogen is <Program>:
19 ;;; <Expr> ::= (constant x)
20 ;;; | (var)
21 ;;; | (primref name)
22 ;;; | (bind var* <Expr>* <Expr>)
23 ;;; | (fix var* <FixRhs>* <Expr>)
24 ;;; | (conditional <Expr> <Expr> <Expr>)
25 ;;; | (seq <Expr> <Expr>)
26 ;;; | (closure <codeloc> <var>*) ; thunk special case
27 ;;; | (forcall "name" <Expr>*)
28 ;;; | (funcall <Expr> <Expr>*)
29 ;;; | (jmpcall <label> <Expr> <Expr>*)
30 ;;; | (mvcall <Expr> <clambda>)
31 ;;; <codeloc> ::= (code-loc <label>)
32 ;;; <clambda> ::= (clambda <label> <case>* <cp> <free var>*)
33 ;;; <case> ::= (clambda-case <info> <body>)
34 ;;; <info> ::= (clambda-info label <arg var>* proper)
35 ;;; <Program> ::= (codes <clambda>* <Expr>)
36
37
38
39
40
41 (define (introduce-primcalls x)
42 ;;;
43 (define who 'introduce-primcalls)
44 ;;;
45 (define (check-gensym x)
46 (unless (gensym? x)
47 (error who "invalid gensym" x)))
48 ;;;
49 (define (check-label x)
50 (struct-case x
51 [(code-loc label)
52 (check-gensym label)]
53 [else (error who "invalid label" x)]))
54 ;;;
55 (define (check-var x)
56 (struct-case x
57 [(var) (void)]
58 [else (error who "invalid var" x)]))
59 ;;;
60 (define (check-closure x)
61 (struct-case x
62 [(closure label free*)
63 (check-label label)
64 (for-each check-var free*)]
65 [else (error who "invalid closure" x)]))
66 ;;;
67 (define (mkfuncall op arg*)
68 (define (primop? x)
69 (import primops)
70 (or (eq? x 'debug-call) (primop? x)))
71 (struct-case op
72 [(known x t)
73 (struct-case x
74 [(primref name)
75 (if (primop? name)
76 (make-primcall name arg*)
77 (make-funcall op arg*))]
78 [else (make-funcall op arg*)])]
79 [(primref name)
80 (cond
81 [(primop? name)
82 (make-primcall name arg*)]
83 [else (make-funcall op arg*)])]
84 [else (make-funcall op arg*)]))
85 ;;;
86 (define (A x)
87 (struct-case x
88 [(known x t) (make-known (Expr x) t)]
89 [else (Expr x)]))
90 (define (Expr x)
91 (struct-case x
92 [(constant) x]
93 [(var) x]
94 [(primref) x]
95 [(bind lhs* rhs* body)
96 (make-bind lhs* (map Expr rhs*) (Expr body))]
97 [(fix lhs* rhs* body)
98 (make-fix lhs* rhs* (Expr body))]
99 [(conditional e0 e1 e2)
100 (make-conditional (Expr e0) (Expr e1) (Expr e2))]
101 [(seq e0 e1)
102 (make-seq (Expr e0) (Expr e1))]
103 [(closure) x]
104 [(forcall op arg*)
105 (make-forcall op (map Expr arg*))]
106 [(funcall rator arg*)
107 (mkfuncall (A rator) (map A arg*))]
108 [(jmpcall label rator arg*)
109 (make-jmpcall label (Expr rator) (map Expr arg*))]
110 [else (error who "invalid expr" x)]))
111 ;;;
112 (define (ClambdaCase x)
113 (struct-case x
114 [(clambda-case info body)
115 (make-clambda-case info (Expr body))]
116 [else (error who "invalid clambda-case" x)]))
117 ;;;
118 (define (Clambda x)
119 (struct-case x
120 [(clambda label case* cp free* name)
121 (make-clambda label (map ClambdaCase case*) cp free* name)]
122 [else (error who "invalid clambda" x)]))
123 ;;;
124 (define (Program x)
125 (struct-case x
126 [(codes code* body)
127 (make-codes (map Clambda code*) (Expr body))]
128 [else (error who "invalid program" x)]))
129 ;;;
130 (Program x))
131
132
133
134 (define (eliminate-fix x)
135 ;;;
136 (define who 'eliminate-fix)
137 ;;;
138 (define (Expr main-cpvar cpvar free*)
139 ;;;
140 (define (Var x)
141 (cond
142 [(eq? x main-cpvar) cpvar]
143 [else
144 (let f ([free* free*] [i 0])
145 (cond
146 [(null? free*) x]
147 [(eq? x (car free*))
148 (make-primcall '$cpref (list cpvar (make-constant i)))]
149 [else (f (cdr free*) (fxadd1 i))]))]))
150 (define (do-fix lhs* rhs* body)
151 (define (handle-closure x)
152 (struct-case x
153 [(closure code free* well-known?)
154 (make-closure code (map Var free*) well-known?)]))
155 (make-fix lhs* (map handle-closure rhs*) body))
156 (define (A x)
157 (struct-case x
158 [(known x t) (make-known (Expr x) t)]
159 [else (Expr x)]))
160 (define (Expr x)
161 (struct-case x
162 [(constant) x]
163 [(var) (Var x)]
164 [(primref) x]
165 [(bind lhs* rhs* body)
166 (make-bind lhs* (map Expr rhs*) (Expr body))]
167 [(fix lhs* rhs* body)
168 (do-fix lhs* rhs* (Expr body))]
169 [(conditional e0 e1 e2)
170 (make-conditional (Expr e0) (Expr e1) (Expr e2))]
171 [(seq e0 e1)
172 (make-seq (Expr e0) (Expr e1))]
173 [(closure)
174 (let ([t (unique-var 'tmp)])
175 (Expr (make-fix (list t) (list x) t)))]
176 [(primcall op arg*)
177 (make-primcall op (map A arg*))]
178 [(forcall op arg*)
179 (make-forcall op (map Expr arg*))]
180 [(funcall rator arg*)
181 (make-funcall (A rator) (map A arg*))]
182 [(jmpcall label rator arg*)
183 (make-jmpcall label (Expr rator) (map Expr arg*))]
184 [else (error who "invalid expr" x)]))
185 Expr)
186 ;;;
187 (define (ClambdaCase main-cp free*)
188 (lambda (x)
189 (struct-case x
190 [(clambda-case info body)
191 (struct-case info
192 [(case-info label args proper)
193 (let ([cp (unique-var 'cp)])
194 (make-clambda-case
195 (make-case-info label (cons cp args) proper)
196 ((Expr main-cp cp free*) body)))])]
197 [else (error who "invalid clambda-case" x)])))
198 ;;;
199 (define (Clambda x)
200 (struct-case x
201 [(clambda label case* cp free* name)
202 (make-clambda label (map (ClambdaCase cp free*) case*)
203 #f free* name)]
204 [else (error who "invalid clambda" x)]))
205 ;;;
206 (define (Program x)
207 (struct-case x
208 [(codes code* body)
209 (make-codes (map Clambda code*) ((Expr #f #f '()) body))]
210 [else (error who "invalid program" x)]))
211 ;;;
212 (Program x))
213
214
215
216 (define-syntax seq*
217 (syntax-rules ()
218 [(_ e) e]
219 [(_ e* ... e)
220 (make-seq (seq* e* ...) e)]))
221
222 (define (insert-engine-checks x)
223 (define who 'insert-engine-checks)
224 (define (known-primref? x)
225 (struct-case x
226 [(known x t) (known-primref? x)]
227 [(primref) #t]
228 [else #f]))
229 (define (A x)
230 (struct-case x
231 [(known x t) (Expr x)]
232 [else (Expr x)]))
233 (define (Expr x)
234 (struct-case x
235 [(constant) #f]
236 [(var) #f]
237 [(primref) #f]
238 [(jmpcall label rator arg*) #t]
239 [(funcall rator arg*)
240 (if (known-primref? rator) (ormap A arg*) #t)]
241 [(bind lhs* rhs* body) (or (ormap Expr rhs*) (Expr body))]
242 [(fix lhs* rhs* body) (Expr body)]
243 [(conditional e0 e1 e2) (or (Expr e0) (Expr e1) (Expr e2))]
244 [(seq e0 e1) (or (Expr e0) (Expr e1))]
245 [(primcall op arg*) (ormap A arg*)]
246 [(forcall op arg*) (ormap Expr arg*)]
247 [else (error who "invalid expr" x)]))
248 (define (Main x)
249 (if (Expr x)
250 (make-seq (make-primcall '$do-event '()) x)
251 x))
252 (define (CaseExpr x)
253 (struct-case x
254 [(clambda-case info body)
255 (make-clambda-case info (Main body))]))
256 (define (CodeExpr x)
257 (struct-case x
258 [(clambda L cases cp free name)
259 (make-clambda L (map CaseExpr cases) cp free name)]))
260 (define (CodesExpr x)
261 (struct-case x
262 [(codes list body)
263 (make-codes (map CodeExpr list) (Main body))]))
264 (CodesExpr x))
265
266
267 (define (insert-stack-overflow-check x)
268 (define who 'insert-stack-overflow-check)
269 (define (A x)
270 (struct-case x
271 [(known x t) (NonTail x)]
272 [else (NonTail x)]))
273 (define (NonTail x)
274 (struct-case x
275 [(constant) #f]
276 [(var) #f]
277 [(primref) #f]
278 [(funcall rator arg*) #t]
279 [(jmpcall label rator arg*) #t]
280 [(mvcall rator k) #t]
281 [(primcall op arg*) (ormap A arg*)] ;PUNT!!! FIXME!
282 [(bind lhs* rhs* body) (or (ormap NonTail rhs*) (NonTail body))]
283 [(fix lhs* rhs* body) (NonTail body)]
284 [(conditional e0 e1 e2) (or (NonTail e0) (NonTail e1) (NonTail e2))]
285 [(seq e0 e1) (or (NonTail e0) (NonTail e1))]
286 [(forcall op arg*) (ormap NonTail arg*)]
287 [(known x t) (NonTail x)]
288 [else (error who "invalid expr" x)]))
289 (define (Tail x)
290 (struct-case x
291 [(constant) #f]
292 [(var) #f]
293 [(primref) #f]
294 [(bind lhs* rhs* body) (or (ormap NonTail rhs*) (Tail body))]
295 [(fix lhs* rhs* body) (Tail body)]
296 [(conditional e0 e1 e2) (or (NonTail e0) (Tail e1) (Tail e2))]
297 [(seq e0 e1) (or (NonTail e0) (Tail e1))]
298 [(primcall op arg*) (ormap NonTail arg*)]
299 [(forcall op arg*) (ormap NonTail arg*)]
300 [(funcall rator arg*) (or (NonTail rator) (ormap NonTail arg*))]
301 [(jmpcall label rator arg*) (or (NonTail rator) (ormap NonTail arg*))]
302 [(mvcall rator k) #t] ; punt
303 [else (error who "invalid expr" x)]))
304 (define (insert-check x)
305 (make-seq (make-primcall '$stack-overflow-check '()) x))
306 (define (ClambdaCase x)
307 (struct-case x
308 [(clambda-case info body)
309 (make-clambda-case info (Main body))]))
310 (define (Clambda x)
311 (struct-case x
312 [(clambda label case* cp free* name)
313 (make-clambda label (map ClambdaCase case*) cp free* name)]))
314 (define (Main x)
315 (if (Tail x)
316 (insert-check x)
317 x))
318 (define (Program x)
319 (struct-case x
320 [(codes code* body)
321 (make-codes (map Clambda code*) (Main body))]))
322 (Program x))
323
324 (include "pass-specify-rep.ss")
325
326 (define parameter-registers '(%edi))
327 (define return-value-register '%eax)
328 (define cp-register '%edi)
329 (define all-registers
330 (case wordsize
331 [(4) '(%eax %edi %ebx %edx %ecx)]
332 [else '(%eax %edi %ebx %edx %ecx %r8 %r9 %r10 %r11 %r14 %r15)]))
333
334 (define non-8bit-registers
335 (case wordsize
336 [(4) '(%edi)]
337 [else '(%edi)]))
338
339 (define argc-register '%eax)
340
341 ;;; apr = %ebp
342 ;;; esp = %esp
343 ;;; pcr = %esi
344 ;;; cpr = %edi
345
346 (define (register-index x)
347 (cond
348 [(assq x '([%eax 0] [%edi 1] [%ebx 2] [%edx 3]
349 [%ecx 4] [%esi 5] [%esp 6] [%ebp 7]))
350 => cadr]
351 [else (error 'register-index "not a register" x)]))
352
353 (define (impose-calling-convention/evaluation-order x)
354 (define who 'impose-calling-convention/evaluation-order)
355 ;;;
356 ;;;
357 (define (S* x* k)
358 (cond
359 [(null? x*) (k '())]
360 [else
361 (S (car x*)
362 (lambda (a)
363 (S* (cdr x*)
364 (lambda (d)
365 (k (cons a d))))))]))
366 ;;;
367 (define (S x k)
368 (struct-case x
369 [(bind lhs* rhs* body)
370 (do-bind lhs* rhs* (S body k))]
371 [(seq e0 e1)
372 (make-seq (E e0) (S e1 k))]
373 [(known x) (S x k)]
374 [else
375 (cond
376 [(or (constant? x) (symbol? x)) (k x)]
377 [(var? x)
378 (cond
379 [(var-loc x) => k]
380 [else (k x)])]
381 [(or (funcall? x) (primcall? x) (jmpcall? x)
382 (forcall? x) (shortcut? x)
383 (conditional? x))
384 (let ([t (unique-var 'tmp)])
385 (do-bind (list t) (list x)
386 (k t)))]
387 [else (error who "invalid S" x)])]))
388 (define (Mem x k)
389 (struct-case x
390 [(primcall op arg*)
391 (if (eq? op 'mref)
392 (S* arg*
393 (lambda (arg*)
394 (k (make-disp (car arg*) (cadr arg*)))))
395 (S x k))]
396 [else (S x k)]))
397 ;;;
398 (define (do-bind lhs* rhs* body)
399 (cond
400 [(null? lhs*) body]
401 [else
402 (set! locals (cons (car lhs*) locals))
403 (make-seq
404 (V (car lhs*) (car rhs*))
405 (do-bind (cdr lhs*) (cdr rhs*) body))]))
406 ;;;
407 (define (nontail-locations args)
408 (let f ([regs parameter-registers] [args args])
409 (cond
410 [(null? args) (values '() '() '())]
411 [(null? regs) (values '() '() args)]
412 [else
413 (let-values ([(r* rl* f*) (f (cdr regs) (cdr args))])
414 (values (cons (car regs) r*)
415 (cons (car args) rl*)
416 f*))])))
417 (define (make-set lhs rhs)
418 (make-asm-instr 'move lhs rhs))
419 (define (do-bind-frmt* nf* v* ac)
420 (cond
421 [(null? nf*) ac]
422 [else
423 (make-seq
424 (V (car nf*) (car v*))
425 (do-bind-frmt* (cdr nf*) (cdr v*) ac))]))
426 ;;;
427 (define (handle-nontail-call rator rands value-dest call-targ)
428 (let-values ([(reg-locs reg-args frm-args)
429 (nontail-locations (cons rator rands))])
430 (let ([regt* (map (lambda (x) (unique-var 'rt)) reg-args)]
431 [frmt* (map (lambda (x) (make-nfv 'unset-conflicts #f #f #f #f))
432 frm-args)])
433 (let* ([call
434 (make-ntcall call-targ value-dest
435 (cons* argc-register
436 pcr esp apr
437 (append reg-locs frmt*))
438 #f #f)]
439 [body
440 (make-nframe frmt* #f
441 (do-bind-frmt* frmt* frm-args
442 (do-bind (cdr regt*) (cdr reg-args)
443 ;;; evaluate cpt last
444 (do-bind (list (car regt*)) (list (car reg-args))
445 (assign* reg-locs regt*
446 (make-seq
447 (make-set argc-register
448 (make-constant
449 (argc-convention (length rands))))
450 call))))))])
451 (if value-dest
452 (make-seq body (make-set value-dest return-value-register))
453 body)))))
454 ;;; (define (alloc-check size)
455 ;;; (E (make-conditional ;;; PCB ALLOC-REDLINE
456 ;;; (make-primcall '<=
457 ;;; (list (make-primcall 'int+ (list apr size))
458 ;;; (make-primcall 'mref (list pcr (make-constant 4)))))
459 ;;; (make-primcall 'nop '())
460 ;;; (make-funcall
461 ;;; (make-primcall 'mref
462 ;;; (list
463 ;;; (make-constant (make-object (primref->symbol 'do-overflow)))
464 ;;; (make-constant (- disp-symbol-record-proc symbol-ptag))))
465 ;;; (list size)))))
466 (define (alloc-check size)
467 (define (test size)
468 (if (struct-case size
469 [(constant i) (<= i 4096)]
470 [else #f])
471 (make-primcall '<=
472 (list
473 apr
474 (make-primcall 'mref
475 (list pcr (make-constant pcb-allocation-redline)))))
476 (make-primcall '>=
477 (list (make-primcall 'int-
478 (list
479 (make-primcall 'mref
480 (list pcr (make-constant pcb-allocation-redline)))
481 apr))
482 size))))
483 (E (make-shortcut
484 (make-conditional ;;; PCB ALLOC-REDLINE
485 (test size)
486 (make-primcall 'nop '())
487 (make-primcall 'interrupt '()))
488 (make-funcall
489 (make-primcall 'mref
490 (list
491 (make-constant (make-object (primref->symbol 'do-overflow)))
492 (make-constant (- disp-symbol-record-proc symbol-ptag))))
493 (list size)))))
494 ;;; impose value
495 (define (V d x)
496 (struct-case x
497 [(constant) (make-set d x)]
498 [(var)
499 (cond
500 [(var-loc x) => (lambda (loc) (make-set d loc))]
501 [else (make-set d x)])]
502 [(bind lhs* rhs* e)
503 (do-bind lhs* rhs* (V d e))]
504 [(seq e0 e1)
505 (make-seq (E e0) (V d e1))]
506 [(conditional e0 e1 e2)
507 (make-conditional (P e0) (V d e1) (V d e2))]
508 [(primcall op rands)
509 (case op
510 [(alloc)
511 (S (car rands)
512 (lambda (size)
513 (make-seq
514 (alloc-check size)
515 (S (cadr rands)
516 (lambda (tag)
517 (make-seq
518 (make-seq
519 (make-set d apr)
520 (make-asm-instr 'logor d tag))
521 (make-asm-instr 'int+ apr size)))))))]
522 [(mref)
523 (S* rands
524 (lambda (rands)
525 (make-set d (make-disp (car rands) (cadr rands)))))]
526 [(mref32)
527 (S* rands
528 (lambda (rands)
529 (make-asm-instr 'load32 d
530 (make-disp (car rands) (cadr rands)))))]
531 [(bref)
532 (S* rands
533 (lambda (rands)
534 (make-asm-instr 'load8 d
535 (make-disp (car rands) (cadr rands)))))]
536 [(logand logxor logor int+ int- int*
537 int-/overflow int+/overflow int*/overflow)
538 (make-seq
539 (V d (car rands))
540 (S (cadr rands)
541 (lambda (s)
542 (make-asm-instr op d s))))]
543 [(int-quotient)
544 (S* rands
545 (lambda (rands)
546 (seq*
547 (make-set eax (car rands))
548 (make-asm-instr 'cltd edx eax)
549 (make-asm-instr 'idiv eax (cadr rands))
550 (make-set d eax))))]
551 [(int-remainder)
552 (S* rands
553 (lambda (rands)
554 (seq*
555 (make-set eax (car rands))
556 (make-asm-instr 'cltd edx eax)
557 (make-asm-instr 'idiv edx (cadr rands))
558 (make-set d edx))))]
559 [(sll sra srl sll/overflow)
560 (let ([a (car rands)] [b (cadr rands)])
561 (cond
562 [(constant? b)
563 (make-seq
564 (V d a)
565 (make-asm-instr op d b))]
566 [else
567 (S b
568 (lambda (b)
569 (seq*
570 (V d a)
571 (make-set ecx b)
572 (make-asm-instr op d ecx))))]))]
573 [else (error who "invalid value op" op)])]
574 [(funcall rator rands)
575 (handle-nontail-call rator rands d #f)]
576 [(jmpcall label rator rands)
577 (handle-nontail-call rator rands d label)]
578 [(forcall op rands)
579 (handle-nontail-call
580 (make-constant (make-foreign-label op))
581 rands d op)]
582 [(shortcut body handler)
583 (make-shortcut
584 (V d body)
585 (V d handler))]
586 [(known x) (V d x)]
587 [else
588 (if (symbol? x)
589 (make-set d x)
590 (error who "invalid value" (unparse x)))]))
591 ;;;
592 (define (assign* lhs* rhs* ac)
593 (cond
594 [(null? lhs*) ac]
595 [else
596 (make-seq
597 (make-set (car lhs*) (car rhs*))
598 (assign* (cdr lhs*) (cdr rhs*) ac))]))
599 ;;;
600 (define (VT x)
601 (S x
602 (lambda (x)
603 (make-seq
604 (make-set return-value-register x)
605 (make-primcall 'return
606 (list pcr esp apr return-value-register))))))
607 ;;; impose effect
608 (define (E x)
609 (struct-case x
610 [(seq e0 e1) (make-seq (E e0) (E e1))]
611 [(conditional e0 e1 e2)
612 (make-conditional (P e0) (E e1) (E e2))]
613 [(bind lhs* rhs* e)
614 (do-bind lhs* rhs* (E e))]
615 [(primcall op rands)
616 (case op
617 [(mset bset mset32)
618 (S* rands
619 (lambda (s*)
620 (make-asm-instr op
621 (make-disp (car s*) (cadr s*))
622 (caddr s*))))]
623 [(fl:load fl:store fl:add! fl:sub! fl:mul! fl:div!
624 fl:from-int fl:shuffle bswap!
625 fl:store-single fl:load-single)
626 (S* rands
627 (lambda (s*)
628 (make-asm-instr op (car s*) (cadr s*))))]
629 [(nop interrupt incr/zero? fl:double->single
630 fl:single->double) x]
631 [else (error 'impose-effect "invalid instr" x)])]
632 [(funcall rator rands)
633 (handle-nontail-call rator rands #f #f)]
634 [(jmpcall label rator rands)
635 (handle-nontail-call rator rands #f label)]
636 [(forcall op rands)
637 (handle-nontail-call
638 (make-constant (make-foreign-label op))
639 rands #f op)]
640 [(shortcut body handler)
641 (make-shortcut (E body) (E handler))]
642 [else (error who "invalid effect" x)]))
643 ;;; impose pred
644 (define (P x)
645 (struct-case x
646 [(constant) x]
647 [(seq e0 e1) (make-seq (E e0) (P e1))]
648 [(conditional e0 e1 e2)
649 (make-conditional (P e0) (P e1) (P e2))]
650 [(bind lhs* rhs* e)
651 (do-bind lhs* rhs* (P e))]
652 [(primcall op rands)
653 (let ([a (car rands)] [b (cadr rands)])
654 (cond
655 [(and (constant? a) (constant? b))
656 (let ([t (unique-var 'tmp)])
657 (P (make-bind (list t) (list a)
658 (make-primcall op (list t b)))))]
659 [else
660 (Mem a
661 (lambda (a)
662 (Mem b
663 (lambda (b)
664 (make-asm-instr op a b)))))]))]
665 ;(cond
666 ; [(and (constant? a) (constant? b))
667 ; (let ([t (unique-var 'tmp)])
668 ; (P (make-bind (list t) (list a)
669 ; (make-primcall op (list t b)))))]
670 ; [(constant? a)
671 ; (Mem b (lambda (b) (make-asm-instr op a b)))]
672 ; [(constant? b)
673 ; (Mem a (lambda (a) (make-asm-instr op a b)))]
674 ; [else
675 ; (S* rands
676 ; (lambda (rands)
677 ; (let ([a (car rands)] [b (cadr rands)])
678 ; (make-asm-instr op a b))))]))]
679 [(shortcut body handler)
680 (make-shortcut (P body) (P handler))]
681 [else (error who "invalid pred" x)]))
682 ;;;
683 (define (handle-tail-call target rator rands)
684 (let* ([args (cons rator rands)]
685 [locs (formals-locations args)]
686 [rest
687 (make-seq
688 (make-set argc-register
689 (make-constant
690 (argc-convention (length rands))))
691 (cond
692 [target
693 (make-primcall 'direct-jump
694 (cons target
695 (cons* argc-register
696 pcr esp apr
697 locs)))]
698 [else
699 (make-primcall 'indirect-jump
700 (cons* argc-register
701 pcr esp apr
702 locs))]))])
703 (let f ([args (reverse args)]
704 [locs (reverse locs)]
705 [targs '()]
706 [tlocs '()])
707 (cond
708 [(null? args) (assign* tlocs targs rest)]
709 [(constant? (car args))
710 (f (cdr args) (cdr locs)
711 (cons (car args) targs)
712 (cons (car locs) tlocs))]
713 [(and (fvar? (car locs))
714 (var? (car args))
715 (eq? (car locs) (var-loc (car args))))
716 (f (cdr args) (cdr locs) targs tlocs)]
717 [else
718 (let ([t (unique-var 'tmp)])
719 (set! locals (cons t locals))
720 (make-seq
721 (V t (car args))
722 (f (cdr args) (cdr locs)
723 (cons t targs) (cons (car locs) tlocs))))]))))
724 (define (Tail x)
725 (struct-case x
726 [(constant) (VT x)]
727 [(var) (VT x)]
728 [(primcall op rands)
729 (case op
730 [($call-with-underflow-handler)
731 (let ([t0 (unique-var 't)]
732 [t1 (unique-var 't)]
733 [t2 (unique-var 't)]
734 [handler (car rands)]
735 [proc (cadr rands)]
736 [k (caddr rands)])
737 (set! locals (cons* t0 t1 t2 locals))
738 (seq*
739 (V t0 handler)
740 (V t1 k)
741 (V t2 proc)
742 (make-set (mkfvar 1) t0)
743 (make-set (mkfvar 2) t1)
744 (make-set cpr t2)
745 (make-set argc-register (make-constant (argc-convention 1)))
746 (make-asm-instr 'int- fpr (make-constant wordsize))
747 (make-primcall 'indirect-jump
748 (list argc-register cpr pcr esp apr
749 (mkfvar 1) (mkfvar 2)))))]
750 [else (VT x)])]
751 [(bind lhs* rhs* e)
752 (do-bind lhs* rhs* (Tail e))]
753 [(seq e0 e1)
754 (make-seq (E e0) (Tail e1))]
755 [(conditional e0 e1 e2)
756 (make-conditional (P e0) (Tail e1) (Tail e2))]
757 [(funcall rator rands)
758 (handle-tail-call #f rator rands)]
759 [(jmpcall label rator rands)
760 (handle-tail-call (make-code-loc label) rator rands)]
761 [(forcall) (VT x)]
762 [(shortcut body handler)
763 (make-shortcut (Tail body) (Tail handler))]
764 [(known x) (Tail x)]
765 [else (error who "invalid tail" x)]))
766 ;;;
767 (define (formals-locations args)
768 (let f ([regs parameter-registers] [args args])
769 (cond
770 [(null? args) '()]
771 [(null? regs)
772 (let f ([i 1] [args args])
773 (cond
774 [(null? args) '()]
775 [else
776 (cons (mkfvar i)
777 (f (fxadd1 i) (cdr args)))]))]
778 [else
779 (cons (car regs) (f (cdr regs) (cdr args)))])))
780 ;;;
781 (define locals '())
782 (define (partition-formals ls)
783 (let f ([regs parameter-registers] [ls ls])
784 (cond
785 [(null? regs)
786 (let ([flocs
787 (let f ([i 1] [ls ls])
788 (cond
789 [(null? ls) '()]
790 [else (cons (mkfvar i) (f (fxadd1 i) (cdr ls)))]))])
791 (values '() '() ls flocs))]
792 [(null? ls)
793 (values '() '() '() '())]
794 [else
795 (let-values ([(rargs rlocs fargs flocs)
796 (f (cdr regs) (cdr ls))])
797 (values (cons (car ls) rargs)
798 (cons (car regs) rlocs)
799 fargs flocs))])))
800 ;;;
801 (define (ClambdaCase x)
802 (struct-case x
803 [(clambda-case info body)
804 (struct-case info
805 [(case-info label args proper)
806 (let-values ([(rargs rlocs fargs flocs)
807 (partition-formals args)])
808 (set! locals rargs)
809 (for-each set-var-loc! fargs flocs)
810 (let ([body (let f ([args rargs] [locs rlocs])
811 (cond
812 [(null? args) (Tail body)]
813 [else
814 (make-seq
815 (make-set (car args) (car locs))
816 (f (cdr args) (cdr locs)))]))])
817 (make-clambda-case
818 (make-case-info label (append rlocs flocs) proper)
819 (make-locals locals body))))])]))
820 ;;;
821 (define (Clambda x)
822 (struct-case x
823 [(clambda label case* cp free* name)
824 (make-clambda label (map ClambdaCase case*) cp free* name)]))
825 ;;;
826 (define (Main x)
827 (set! locals '())
828 (let ([x (Tail x)])
829 (make-locals locals x)))
830 ;;;
831 (define (Program x)
832 (struct-case x
833 [(codes code* body)
834 (make-codes (map Clambda code*) (Main body))]))
835 ;;;
836 ; (print-code x)
837 (Program x))
838
839 (module ListySet
840 (make-empty-set set-member? set-add set-rem set-difference set-union
841 empty-set? singleton
842 set->list list->set)
843 (define-struct set (v))
844 (define (make-empty-set) (make-set '()))
845 (define (set-member? x s)
846 ;(unless (fixnum? x) (error 'set-member? "not a fixnum" x))
847 (unless (set? s) (error 'set-member? "not a set" s))
848 (memq x (set-v s)))
849 (define (empty-set? s)
850 (unless (set? s) (error 'empty-set? "not a set" s))
851 (null? (set-v s)))
852 (define (set->list s)
853 (unless (set? s) (error 'set->list "not a set" s))
854 (set-v s))
855 (define (singleton x)
856 (make-set (list x)))
857 (define (set-add x s)
858 ;(unless (fixnum? x) (error 'set-add "not a fixnum" x))
859 (unless (set? s) (error 'set-add "not a set" s))
860 (cond
861 [(memq x (set-v s)) s]
862 [else (make-set (cons x (set-v s)))]))
863 (define (rem x s)
864 (cond
865 [(null? s) '()]
866 [(eq? x (car s)) (cdr s)]
867 [else (cons (car s) (rem x (cdr s)))]))
868 (define (set-rem x s)
869 ;(unless (fixnum? x) (error 'set-rem "not a fixnum" x))
870 (unless (set? s) (error 'set-rem "not a set" s))
871 (make-set (rem x (set-v s))))
872 (define (difference s1 s2)
873 (cond
874 [(null? s2) s1]
875 [else (difference (rem (car s2) s1) (cdr s2))]))
876 (define (set-difference s1 s2)
877 (unless (set? s1) (error 'set-difference "not a set" s1))
878 (unless (set? s2) (error 'set-difference "not a set" s2))
879 (make-set (difference (set-v s1) (set-v s2))))
880 (define (set-union s1 s2)
881 (unless (set? s1) (error 'set-union "not a set" s1))
882 (unless (set? s2) (error 'set-union "not a set" s2))
883 (make-set (union (set-v s1) (set-v s2))))
884 (define (list->set ls)
885 ;(unless (andmap fixnum? ls) (error 'set-rem "not a list of fixnum" ls))
886 (make-set ls))
887 (define (union s1 s2)
888 (cond
889 [(null? s1) s2]
890 [(memq (car s1) s2) (union (cdr s1) s2)]
891 [else (cons (car s1) (union (cdr s1) s2))])))
892
893 (module IntegerSet
894 (make-empty-set set-member? set-add singleton set-rem set-difference
895 set-union empty-set? set->list list->set)
896 ;;;
897 (begin
898 (define-syntax car (identifier-syntax $car))
899 (define-syntax cdr (identifier-syntax $cdr))
900 (define-syntax fxsll (identifier-syntax $fxsll))
901 (define-syntax fxsra (identifier-syntax $fxsra))
902 (define-syntax fxlogor (identifier-syntax $fxlogor))
903 (define-syntax fxlogand (identifier-syntax $fxlogand))
904 (define-syntax fxlognot (identifier-syntax $fxlognot))
905 (define-syntax fx+ (identifier-syntax $fx+))
906 (define-syntax fxzero? (identifier-syntax $fxzero?))
907 (define-syntax fxeven?
908 (syntax-rules ()
909 [(_ x) ($fxzero? ($fxlogand x 1))])))
910 ;;;
911 (define bits 28)
912 (define (index-of n) (fxquotient n bits))
913 (define (mask-of n) (fxsll 1 (fxremainder n bits)))
914 ;;;
915 (define (make-empty-set) 0)
916 (define (empty-set? s) (eqv? s 0))
917
918 (define (set-member? n s)
919 (unless (fixnum? n) (error 'set-member? "not a fixnum" n))
920 (let f ([s s] [i (index-of n)] [j (mask-of n)])
921 (cond
922 [(pair? s)
923 (if (fxeven? i)
924 (f (car s) (fxsra i 1) j)
925 (f (cdr s) (fxsra i 1) j))]
926 [(eq? i 0) (eq? j (fxlogand s j))]
927 [else #f])))
928 ;;;
929 (define (singleton n)
930 (set-add n (make-empty-set)))
931 ;;;
932 (define (set-add n s)
933 (unless (fixnum? n) (error 'set-add "not a fixnum" n))
934 (let f ([s s] [i (index-of n)] [j (mask-of n)])
935 (cond
936 [(pair? s)
937 (if (fxeven? i)
938 (let ([a0 (car s)])
939 (let ([a1 (f a0 (fxsra i 1) j)])
940 (if (eq? a0 a1) s (cons a1 (cdr s)))))
941 (let ([d0 (cdr s)])
942 (let ([d1 (f d0 (fxsra i 1) j)])
943 (if (eq? d0 d1) s (cons (car s) d1)))))]
944 [(eq? i 0) (fxlogor s j)]
945 [else
946 (if (fxeven? i)
947 (cons (f s (fxsra i 1) j) 0)
948 (cons s (f 0 (fxsra i 1) j)))])))
949 ;;;
950 (define (cons^ a d)
951 (if (and (eq? d 0) (fixnum? a))
952 a
953 (cons a d)))
954 ;;;
955 (define (set-rem n s)
956 (unless (fixnum? n) (error 'set-rem "not a fixnum" n))
957 (let f ([s s] [i (index-of n)] [j (mask-of n)])
958 (cond
959 [(pair? s)
960 (if (fxeven? i)
961 (let ([a0 (car s)])
962 (let ([a1 (f a0 (fxsra i 1) j)])
963 (if (eq? a0 a1) s (cons^ a1 (cdr s)))))
964 (let ([d0 (cdr s)])
965 (let ([d1 (f d0 (fxsra i 1) j)])
966 (if (eq? d0 d1) s (cons^ (car s) d1)))))]
967 [(eq? i 0) (fxlogand s (fxlognot j))]
968 [else s])))
969
970 (define (set-union^ s1 m2)
971 (if (pair? s1)
972 (let ([a0 (car s1)])
973 (let ([a1 (set-union^ a0 m2)])
974 (if (eq? a0 a1) s1 (cons a1 (cdr s1)))))
975 (fxlogor s1 m2)))
976 ;;;
977 (define (set-union s1 s2)
978 (if (pair? s1)
979 (if (pair? s2)
980 (if (eq? s1 s2)
981 s1
982 (cons (set-union (car s1) (car s2))
983 (set-union (cdr s1) (cdr s2))))
984 (let ([a0 (car s1)])
985 (let ([a1 (set-union^ a0 s2)])
986 (if (eq? a0 a1) s1 (cons a1 (cdr s1))))))
987 (if (pair? s2)
988 (let ([a0 (car s2)])
989 (let ([a1 (set-union^ a0 s1)])
990 (if (eq? a0 a1) s2 (cons a1 (cdr s2)))))
991 (fxlogor s1 s2))))
992 ;;;
993 (define (set-difference^ s1 m2)
994 (if (pair? s1)
995 (let ([a0 (car s1)])
996 (let ([a1 (set-difference^ a0 m2)])
997 (if (eq? a0 a1) s1 (cons^ a1 (cdr s1)))))
998 (fxlogand s1 (fxlognot m2))))
999
1000 (define (set-difference^^ m1 s2)
1001 (if (pair? s2)
1002 (set-difference^^ m1 (car s2))
1003 (fxlogand m1 (fxlognot s2))))
1004 ;;;
1005 (define (set-difference s1 s2)
1006 (if (pair? s1)
1007 (if (pair? s2)
1008 (if (eq? s1 s2)
1009 0
1010 (cons^ (set-difference (car s1) (car s2))
1011 (set-difference (cdr s1) (cdr s2))))
1012 (let ([a0 (car s1)])
1013 (let ([a1 (set-difference^ a0 s2)])
1014 (if (eq? a0 a1) s1 (cons^ a1 (cdr s1))))))
1015 (if (pair? s2)
1016 (set-difference^^ s1 (car s2))
1017 (fxlogand s1 (fxlognot s2)))))
1018 ;;;
1019 (define (list->set ls)
1020 (unless (andmap fixnum? ls) (error 'list->set "not a list of fixnum" ls))
1021 (let f ([ls ls] [s 0])
1022 (cond
1023 [(null? ls) s]
1024 [else (f (cdr ls) (set-add (car ls) s))])))
1025 ;;;
1026 (define (set->list s)
1027 (let f ([i 0] [j 1] [s s] [ac '()])
1028 (cond
1029 [(pair? s)
1030 (f i (fxsll j 1) (car s)
1031 (f (fxlogor i j) (fxsll j 1) (cdr s) ac))]
1032 [else
1033 (let f ([i (fx* i bits)] [m s] [ac ac])
1034 (cond
1035 [(fxeven? m)
1036 (if (fxzero? m)
1037 ac
1038 (f (fx+ i 1) (fxsra m 1) ac))]
1039 [else
1040 (f (fx+ i 1) (fxsra m 1) (cons i ac))]))])))
1041 #|IntegerSet|#)
1042
1043 (module ListyGraphs
1044 (empty-graph add-edge! empty-graph? print-graph node-neighbors
1045 delete-node!)
1046 (import ListySet)
1047 ;;;
1048 (define-struct graph (ls))
1049 ;;;
1050 (define (empty-graph) (make-graph '()))
1051 ;;;
1052 (define (empty-graph? g)
1053 (andmap (lambda (x) (empty-set? (cdr x))) (graph-ls g)))
1054 ;;;
1055 (define (single x)
1056 (set-add x (make-empty-set)))
1057 ;;;
1058 (define (add-edge! g x y)
1059 (let ([ls (graph-ls g)])
1060 (cond
1061 [(assq x ls) =>
1062 (lambda (p0)
1063 (unless (set-member? y (cdr p0))
1064 (set-cdr! p0 (set-add y (cdr p0)))
1065 (cond
1066 [(assq y ls) =>
1067 (lambda (p1)
1068 (set-cdr! p1 (set-add x (cdr p1))))]
1069 [else
1070 (set-graph-ls! g
1071 (cons (cons y (single x)) ls))])))]
1072 [(assq y ls) =>
1073 (lambda (p1)
1074 (set-cdr! p1 (set-add x (cdr p1)))
1075 (set-graph-ls! g (cons (cons x (single y)) ls)))]
1076 [else
1077 (set-graph-ls! g
1078 (cons* (cons x (single y))
1079 (cons y (single x))
1080 ls))])))
1081 (define (print-graph g)
1082 (printf "G={\n")
1083 (parameterize ([print-gensym 'pretty])
1084 (for-each (lambda (x)
1085 (let ([lhs (car x)] [rhs* (cdr x)])
1086 (printf " ~s => ~s\n"
1087 (unparse lhs)
1088 (map unparse (set->list rhs*)))))
1089 (graph-ls g)))
1090 (printf "}\n"))
1091 (define (node-neighbors x g)
1092 (cond
1093 [(assq x (graph-ls g)) => cdr]
1094 [else (make-empty-set)]))
1095 ;;;
1096 (define (delete-node! x g)
1097 (let ([ls (graph-ls g)])
1098 (cond
1099 [(assq x ls) =>
1100 (lambda (p)
1101 (for-each (lambda (y)
1102 (let ([p (assq y ls)])
1103 (set-cdr! p (set-rem x (cdr p)))))
1104 (set->list (cdr p)))
1105 (set-cdr! p (make-empty-set)))]
1106 [else (void)])))
1107 ;;;
1108 #|ListyGraphs|#)
1109
1110
1111 (module IntegerGraphs
1112 (empty-graph add-edge! empty-graph? print-graph node-neighbors
1113 delete-node!)
1114 (import IntegerSet)
1115 ;;;
1116 (define-struct graph (ls))
1117 ;;;
1118 (define (empty-graph) (make-graph '()))
1119 ;;;
1120 (define (empty-graph? g)
1121 (andmap (lambda (x) (empty-set? (cdr x))) (graph-ls g)))
1122 ;;;
1123 (define (single x)
1124 (set-add x (make-empty-set)))
1125 ;;;
1126 (define (add-edge! g x y)
1127 (let ([ls (graph-ls g)])
1128 (cond
1129 [(assq x ls) =>
1130 (lambda (p0)
1131 (unless (set-member? y (cdr p0))
1132 (set-cdr! p0 (set-add y (cdr p0)))
1133 (cond
1134 [(assq y ls) =>
1135 (lambda (p1)
1136 (set-cdr! p1 (set-add x (cdr p1))))]
1137 [else
1138 (set-graph-ls! g
1139 (cons (cons y (single x)) ls))])))]
1140 [(assq y ls) =>
1141 (lambda (p1)
1142 (set-cdr! p1 (set-add x (cdr p1)))
1143 (set-graph-ls! g (cons (cons x (single y)) ls)))]
1144 [else
1145 (set-graph-ls! g
1146 (cons* (cons x (single y))
1147 (cons y (single x))
1148 ls))])))
1149 (define (print-graph g)
1150 (printf "G={\n")
1151 (parameterize ([print-gensym 'pretty])
1152 (for-each (lambda (x)
1153 (let ([lhs (car x)] [rhs* (cdr x)])
1154 (printf " ~s => ~s\n"
1155 (unparse lhs)
1156 (map unparse (set->list rhs*)))))
1157 (graph-ls g)))
1158 (printf "}\n"))
1159 (define (node-neighbors x g)
1160 (cond
1161 [(assq x (graph-ls g)) => cdr]
1162 [else (make-empty-set)]))
1163 ;;;
1164 (define (delete-node! x g)
1165 (let ([ls (graph-ls g)])
1166 (cond
1167 [(assq x ls) =>
1168 (lambda (p)
1169 (for-each (lambda (y)
1170 (let ([p (assq y ls)])
1171 (set-cdr! p (set-rem x (cdr p)))))
1172 (set->list (cdr p)))
1173 (set-cdr! p (make-empty-set)))]
1174 [else (void)])))
1175 ;;;
1176 #|IntegerGraphs|#)
1177
1178 (module conflict-helpers
1179 (empty-var-set rem-var add-var union-vars mem-var? for-each-var init-vars!
1180 empty-nfv-set rem-nfv add-nfv union-nfvs mem-nfv? for-each-nfv init-nfv!
1181 empty-frm-set rem-frm add-frm union-frms mem-frm?
1182 empty-reg-set rem-reg add-reg union-regs mem-reg?
1183 reg?)
1184 (import IntegerSet)
1185 (begin
1186 (define (add-frm x s) (set-add (fvar-idx x) s))
1187 (define (rem-nfv x s)
1188 (remq1 x s))
1189 (define (init-var! x i)
1190 (set-var-index! x i)
1191 (set-var-var-move! x (empty-var-set))
1192 (set-var-reg-move! x (empty-reg-set))
1193 (set-var-frm-move! x (empty-frm-set))
1194 (set-var-var-conf! x (empty-var-set))
1195 (set-var-reg-conf! x (empty-reg-set))
1196 (set-var-frm-conf! x (empty-frm-set)))
1197 (define (init-vars! ls)
1198 (let f ([ls ls] [i 0])
1199 (unless (null? ls)
1200 (init-var! (car ls) i)
1201 (f (cdr ls) (fxadd1 i)))))
1202 (define (init-nfv! x)
1203 (set-nfv-frm-conf! x (empty-frm-set))
1204 (set-nfv-nfv-conf! x (empty-nfv-set))
1205 (set-nfv-var-conf! x (empty-var-set)))
1206 (define (reg? x) (symbol? x))
1207 (define (empty-var-set) (make-empty-set))
1208 (define (add-var x s) (set-add (var-index x) s))
1209 (define (mem-var? x s) (set-member? (var-index x) s))
1210 (define (rem-var x s) (set-rem (var-index x) s))
1211 (define (union-vars s1 s2) (set-union s1 s2))
1212 (define (for-each-var s varvec f)
1213 (for-each (lambda (i) (f (vector-ref varvec i)))
1214 (set->list s)))
1215 (define (empty-reg-set) (make-empty-set))
1216 (define (add-reg x s) (set-add (register-index x) s))
1217 (define (rem-reg x s) (set-rem (register-index x) s))
1218 (define (mem-reg? x s) (set-member? (register-index x) s))
1219 (define (union-regs s1 s2) (set-union s1 s2))
1220 (define (empty-frm-set) (make-empty-set))
1221 (define (mem-frm? x s) (set-member? (fvar-idx x) s))
1222 (define (rem-frm x s) (set-rem (fvar-idx x) s))
1223 (define (union-frms s1 s2) (set-union s1 s2))
1224 (define (empty-nfv-set) '())
1225 (define (add-nfv x s)
1226 (cond
1227 [(memq x s) s]
1228 [else (cons x s)]))
1229 (define (mem-nfv? x s)
1230 (memq x s))
1231 (define (union-nfvs s1 s2)
1232 (let f ([s1 s1] [s2 s2])
1233 (cond
1234 [(null? s1) s2]
1235 [(memq (car s1) s2) (f (cdr s1) s2)]
1236 [else (cons (car s1) (f (cdr s1) s2))])))
1237 (define (for-each-nfv s f)
1238 (for-each f s))))
1239
1240 (define (uncover-frame-conflicts x varvec)
1241 (import IntegerSet)
1242 (import conflict-helpers)
1243 (define who 'uncover-frame-conflicts)
1244 (define spill-set (make-empty-set))
1245 (define (mark-reg/vars-conf! r vs)
1246 (for-each-var vs varvec
1247 (lambda (v)
1248 (set-var-reg-conf! v
1249 (add-reg r (var-reg-conf v))))))
1250 (define (mark-frm/vars-conf! f vs)
1251 (for-each-var vs varvec
1252 (lambda (v)
1253 (set-var-frm-conf! v
1254 (add-frm f (var-frm-conf v))))))
1255 (define (mark-frm/nfvs-conf! f ns)
1256 (for-each-nfv ns
1257 (lambda (n)
1258 (set-nfv-frm-conf! n
1259 (add-frm f (nfv-frm-conf n))))))
1260 (define (mark-var/vars-conf! v vs)
1261 (for-each-var vs varvec
1262 (lambda (w)
1263 (set-var-var-conf! w
1264 (add-var v (var-var-conf w)))))
1265 (set-var-var-conf! v
1266 (union-vars vs (var-var-conf v))))
1267 (define (mark-var/frms-conf! v fs)
1268 (set-var-frm-conf! v
1269 (union-frms fs (var-frm-conf v))))
1270 (define (mark-var/regs-conf! v rs)
1271 (set-var-reg-conf! v
1272 (union-regs rs (var-reg-conf v))))
1273 (define (mark-var/nfvs-conf! v ns)
1274 (for-each-nfv ns
1275 (lambda (n)
1276 (set-nfv-var-conf! n
1277 (add-var v (nfv-var-conf n))))))
1278 (define (mark-nfv/vars-conf! n vs)
1279 (set-nfv-var-conf! n
1280 (union-vars vs (nfv-var-conf n))))
1281 (define (mark-nfv/frms-conf! n fs)
1282 (set-nfv-frm-conf! n
1283 (union-frms fs (nfv-frm-conf n))))
1284 (define (mark-nfv/nfvs-conf! n ns)
1285 (set-nfv-nfv-conf! n
1286 (union-nfvs ns (nfv-nfv-conf n)))
1287 (for-each-nfv ns
1288 (lambda (m)
1289 (set-nfv-nfv-conf! m
1290 (add-nfv n (nfv-nfv-conf m))))))
1291 (define (mark-var/var-move! x y)
1292 (set-var-var-move! x
1293 (add-var y (var-var-move x)))
1294 (set-var-var-move! y
1295 (add-var x (var-var-move y))))
1296 (define (mark-var/frm-move! x y)
1297 (set-var-frm-move! x
1298 (add-frm y (var-frm-move x))))
1299 (define (mark-var/reg-move! x y)
1300 (set-var-reg-move! x
1301 (add-reg y (var-reg-move x))))
1302 (define (const? x)
1303 (or (constant? x)
1304 (code-loc? x)))
1305 (define (R x vs rs fs ns)
1306 (cond
1307 [(const? x) (values vs rs fs ns)]
1308 [(reg? x)
1309 (values vs (add-reg x rs) fs ns)]
1310 [(fvar? x)
1311 (values vs rs (add-frm x fs) ns)]
1312 [(var? x)
1313 (values (add-var x vs) rs fs ns)]
1314 [(nfv? x)
1315 (values vs rs fs (add-nfv x ns))]
1316 [(disp? x)
1317 (let-values ([(vs rs fs ns) (R (disp-s0 x) vs rs fs ns)])
1318 (R (disp-s1 x) vs rs fs ns))]
1319 [else (error who "invalid R" x)]))
1320 (define (R* ls vs rs fs ns)
1321 (cond
1322 [(null? ls) (values vs rs fs ns)]
1323 [else
1324 (let-values ([(vs rs fs ns) (R (car ls) vs rs fs ns)])
1325 (R* (cdr ls) vs rs fs ns))]))
1326 (define (E x vs rs fs ns)
1327 (struct-case x
1328 [(seq e0 e1)
1329 (let-values ([(vs rs fs ns) (E e1 vs rs fs ns)])
1330 (E e0 vs rs fs ns))]
1331 [(conditional e0 e1 e2)
1332 (let-values ([(vs1 rs1 fs1 ns1) (E e1 vs rs fs ns)]
1333 [(vs2 rs2 fs2 ns2) (E e2 vs rs fs ns)])
1334 (P e0
1335 vs1 rs1 fs1 ns1
1336 vs2 rs2 fs2 ns2
1337 (union-vars vs1 vs2)
1338 (union-regs rs1 rs2)
1339 (union-frms fs1 fs2)
1340 (union-nfvs ns1 ns2)))]
1341 [(asm-instr op d s)
1342 (case op
1343 [(move load8 load32)
1344 (cond
1345 [(reg? d)
1346 (cond
1347 [(not (mem-reg? d rs))
1348 (set-asm-instr-op! x 'nop)
1349 (values vs rs fs ns)]
1350 [(or (const? s) (disp? s) (reg? s))
1351 (let ([rs (rem-reg d rs)])
1352 (mark-reg/vars-conf! d vs)
1353 (R s vs rs fs ns))]
1354 [(var? s)
1355 (let ([rs (rem-reg d rs)]
1356 [vs (rem-var s vs)])
1357 (mark-var/reg-move! s d)
1358 (mark-reg/vars-conf! d vs)
1359 (values (add-var s vs) rs fs ns))]
1360 [(fvar? s)
1361 (let ([rs (rem-reg d rs)])
1362 (mark-reg/vars-conf! d vs)
1363 (values vs rs (add-frm s fs) ns))]
1364 [else (error who "invalid rs" (unparse x))])]
1365 [(fvar? d)
1366 (cond
1367 [(not (mem-frm? d fs))
1368 (set-asm-instr-op! x 'nop)
1369 (values vs rs fs ns)]
1370 [(or (const? s) (disp? s) (reg? s))
1371 (let ([fs (rem-frm d fs)])
1372 (mark-frm/vars-conf! d vs)
1373 (mark-frm/nfvs-conf! d ns)
1374 (R s vs rs fs ns))]
1375 [(var? s)
1376 (let ([fs (rem-frm d fs)]
1377 [vs (rem-var s vs)])
1378 (mark-var/frm-move! s d)
1379 (mark-frm/vars-conf! d vs)
1380 (mark-frm/nfvs-conf! d ns)
1381 (values (add-var s vs) rs fs ns))]
1382 [else (error who "invalid fs" s)])]
1383 [(var? d)
1384 (cond
1385 [(not (mem-var? d vs))
1386 (set-asm-instr-op! x 'nop)
1387 (values vs rs fs ns)]
1388 [(or (disp? s) (constant? s))
1389 (let ([vs (rem-var d vs)])
1390 (mark-var/vars-conf! d vs)
1391 (mark-var/frms-conf! d fs)
1392 (mark-var/regs-conf! d rs)
1393 (mark-var/nfvs-conf! d ns)
1394 (R s vs rs fs ns))]
1395 [(reg? s)
1396 (let ([vs (rem-var d vs)]
1397 [rs (rem-reg s rs)])
1398 (mark-var/reg-move! d s)
1399 (mark-var/vars-conf! d vs)
1400 (mark-var/frms-conf! d fs)
1401 (mark-var/regs-conf! d rs)
1402 (mark-var/nfvs-conf! d ns)
1403 (values vs (add-reg s rs) fs ns))]
1404 [(var? s)
1405 (let ([vs (rem-var d (rem-var s vs))])
1406 (mark-var/var-move! d s)
1407 (mark-var/vars-conf! d vs)
1408 (mark-var/frms-conf! d fs)
1409 (mark-var/regs-conf! d rs)
1410 (mark-var/nfvs-conf! d ns)
1411 (values (add-var s vs) rs fs ns))]
1412 [(fvar? s)
1413 (let ([vs (rem-var d vs)]
1414 [fs (rem-frm s fs)])
1415 (mark-var/frm-move! d s)
1416 (mark-var/vars-conf! d vs)
1417 (mark-var/frms-conf! d fs)
1418 (mark-var/regs-conf! d rs)
1419 (mark-var/nfvs-conf! d ns)
1420 (values vs rs (add-frm s fs) ns))]
1421 [else (error who "invalid vs" s)])]
1422 [(nfv? d)
1423 (cond
1424 [(not (mem-nfv? d ns)) (error who "dead nfv")]
1425 [(or (disp? s) (constant? s) (reg? s))
1426 (let ([ns (rem-nfv d ns)])
1427 (mark-nfv/vars-conf! d vs)
1428 (mark-nfv/frms-conf! d fs)
1429 (R s vs rs fs ns))]
1430 [(var? s)
1431 (let ([ns (rem-nfv d ns)]
1432 [vs (rem-var s vs)])
1433 (mark-nfv/vars-conf! d vs)
1434 (mark-nfv/frms-conf! d fs)
1435 (values (add-var s vs) rs fs ns))]
1436 [(fvar? s)
1437 (let ([ns (rem-nfv d ns)]
1438 [fs (rem-frm s fs)])
1439 (mark-nfv/vars-conf! d vs)
1440 (mark-nfv/frms-conf! d fs)
1441 (values vs rs (add-frm s fs) ns))]
1442 [else (error who "invalid ns" s)])]
1443 [else (error who "invalid d" d)])]
1444 [(int-/overflow int+/overflow int*/overflow)
1445 (let ([v (exception-live-set)])
1446 (unless (vector? v)
1447 (error who "unbound exception" x v))
1448 (let ([vs (union-vars vs (vector-ref v 0))]
1449 [rs (union-regs rs (vector-ref v 1))]
1450 [fs (union-frms fs (vector-ref v 2))]
1451 [ns (union-nfvs ns (vector-ref v 3))])
1452 (cond
1453 [(var? d)
1454 (cond
1455 [(not (mem-var? d vs))
1456 (set-asm-instr-op! x 'nop)
1457 (values vs rs fs ns)]
1458 [else
1459 (let ([vs (rem-var d vs)])
1460 (mark-var/vars-conf! d vs)
1461 (mark-var/frms-conf! d fs)
1462 (mark-var/nfvs-conf! d ns)
1463 (mark-var/regs-conf! d rs)
1464 (R s (add-var d vs) rs fs ns))])]
1465 [(reg? d)
1466 (cond
1467 [(not (mem-reg? d rs))
1468 (values vs rs fs ns)]
1469 [else
1470 (let ([rs (rem-reg d rs)])
1471 (mark-reg/vars-conf! d vs)
1472 (R s vs (add-reg d rs) fs ns))])]
1473 [(nfv? d)
1474 (cond
1475 [(not (mem-nfv? d ns)) (error who "dead nfv")]
1476 [else
1477 (let ([ns (rem-nfv d ns)])
1478 (mark-nfv/vars-conf! d vs)
1479 (mark-nfv/frms-conf! d fs)
1480 (R s vs rs fs (add-nfv d ns)))])]
1481 [else (error who "invalid op d" (unparse x))])))]
1482 [(nop) (values vs rs fs ns)]
1483 [(logand logor logxor sll sra srl int+ int- int* bswap!
1484 sll/overflow)
1485 (cond
1486 [(var? d)
1487 (cond
1488 [(not (mem-var? d vs))
1489 (set-asm-instr-op! x 'nop)
1490 (values vs rs fs ns)]
1491 [else
1492 (let ([vs (rem-var d vs)])
1493 (mark-var/vars-conf! d vs)
1494 (mark-var/frms-conf! d fs)
1495 (mark-var/nfvs-conf! d ns)
1496 (mark-var/regs-conf! d rs)
1497 (R s (add-var d vs) rs fs ns))])]
1498 [(reg? d)
1499 (cond
1500 [(not (mem-reg? d rs))
1501 (set-asm-instr-op! x 'nop)
1502 (values vs rs fs ns)]
1503 [else
1504 (let ([rs (rem-reg d rs)])
1505 (mark-reg/vars-conf! d vs)
1506 (R s vs (add-reg d rs) fs ns))])]
1507 [(nfv? d)
1508 (cond
1509 [(not (mem-nfv? d ns)) (error who "dead nfv")]
1510 [else
1511 (let ([ns (rem-nfv d ns)])
1512 (mark-nfv/vars-conf! d vs)
1513 (mark-nfv/frms-conf! d fs)
1514 (R s vs rs fs (add-nfv d ns)))])]
1515 [else (error who "invalid op d" (unparse x))])]
1516 [(idiv)
1517 (mark-reg/vars-conf! eax vs)
1518 (mark-reg/vars-conf! edx vs)
1519 (R s vs (add-reg eax (add-reg edx rs)) fs ns)]
1520 [(cltd)
1521 (mark-reg/vars-conf! edx vs)
1522 (R s vs (rem-reg edx rs) fs ns)]
1523 [(mset mset32 bset
1524 fl:load fl:store fl:add! fl:sub! fl:mul! fl:div! fl:from-int
1525 fl:shuffle fl:load-single fl:store-single)
1526 (R* (list s d) vs rs fs ns)]
1527 [else (error who "invalid effect op" (unparse x))])]
1528 [(ntcall target value args mask size)
1529 (set! spill-set (union-vars vs spill-set))
1530 (for-each-var vs varvec (lambda (x) (set-var-loc! x #t)))
1531 (R* args vs (empty-reg-set) fs ns)]
1532 [(nframe nfvs live body)
1533 (for-each init-nfv! nfvs)
1534 (set-nframe-live! x (vector vs fs ns))
1535 (E body vs rs fs ns)]
1536 [(primcall op args)
1537 (case op
1538 [(nop fl:double->single fl:single->double) (values vs rs fs ns)]
1539 [(interrupt incr/zero?)
1540 (let ([v (exception-live-set)])
1541 (unless (vector? v)
1542 (error who "unbound exception2"))
1543 (values (vector-ref v 0)
1544 (vector-ref v 1)
1545 (vector-ref v 2)
1546 (vector-ref v 3)))]
1547 [else (error who "invalid effect op" op)])]
1548 [(shortcut body handler)
1549 (let-values ([(vsh rsh fsh nsh) (E handler vs rs fs ns)])
1550 (parameterize ([exception-live-set
1551 (vector vsh rsh fsh nsh)])
1552 (E body vs rs fs ns)))]
1553 [else (error who "invalid effect" (unparse x))]))
1554 (define (P x vst rst fst nst
1555 vsf rsf fsf nsf
1556 vsu rsu fsu nsu)
1557 (struct-case x
1558 [(seq e0 e1)
1559 (let-values ([(vs rs fs ns)
1560 (P e1 vst rst fst nst
1561 vsf rsf fsf nsf
1562 vsu rsu fsu nsu)])
1563 (E e0 vs rs fs ns))]
1564 [(conditional e0 e1 e2)
1565 (let-values ([(vs1 rs1 fs1 ns1)
1566 (P e1 vst rst fst nst
1567 vsf rsf fsf nsf
1568 vsu rsu fsu nsu)]
1569 [(vs2 rs2 fs2 ns2)
1570 (P e2 vst rst fst nst
1571 vsf rsf fsf nsf
1572 vsu rsu fsu nsu)])
1573 (P e0
1574 vs1 rs1 fs1 ns1
1575 vs2 rs2 fs2 ns2
1576 (union-vars vs1 vs2)
1577 (union-regs rs1 rs2)
1578 (union-frms fs1 fs2)
1579 (union-nfvs ns1 ns2)))]
1580 [(constant t)
1581 (if t
1582 (values vst rst fst nst)
1583 (values vsf rsf fsf nsf))]
1584 [(asm-instr op d s)
1585 (R* (list d s) vsu rsu fsu nsu)]
1586 [(shortcut body handler)
1587 (let-values ([(vsh rsh fsh nsh)
1588 (P handler vst rst fst nst
1589 vsf rsf fsf nsf
1590 vsu rsu fsu nsu)])
1591 (parameterize ([exception-live-set
1592 (vector vsh rsh fsh nsh)])
1593 (P body vst rst fst nst
1594 vsf rsf fsf nsf
1595 vsu rsu fsu nsu)))]
1596 [else (error who "invalid pred" (unparse x))]))
1597 (define (T x)
1598 (struct-case x
1599 [(seq e0 e1)
1600 (let-values ([(vs rs fs ns) (T e1)])
1601 (E e0 vs rs fs ns))]
1602 [(conditional e0 e1 e2)
1603 (let-values ([(vs1 rs1 fs1 ns1) (T e1)]
1604 [(vs2 rs2 fs2 ns2) (T e2)])
1605 (P e0
1606 vs1 rs1 fs1 ns1
1607 vs2 rs2 fs2 ns2
1608 (union-vars vs1 vs2)
1609 (union-regs rs1 rs2)
1610 (union-frms fs1 fs2)
1611 (union-nfvs ns1 ns2)))]
1612 [(primcall op arg*)
1613 (case op
1614 [(return indirect-jump direct-jump)
1615 (R* arg* (empty-var-set)
1616 (empty-reg-set)
1617 (empty-frm-set)
1618 (empty-nfv-set))]
1619 [else (error who "invalid tail op" x)])]
1620 [(shortcut body handler)
1621 (let-values ([(vsh rsh fsh nsh) (T handler)])
1622 (parameterize ([exception-live-set
1623 (vector vsh rsh fsh nsh)])
1624 (T body)))]
1625 [else (error who "invalid tail" x)]))
1626 (define exception-live-set
1627 (make-parameter #f))
1628 (T x)
1629 spill-set)
1630
1631
1632
1633 (module (assign-frame-sizes)
1634 ;;; assign-frame-sizes module
1635 (define indent (make-parameter 0))
1636 (import IntegerSet)
1637 (import conflict-helpers)
1638 (define (rewrite x varvec)
1639 (define who 'rewrite)
1640 (define (assign x)
1641 (let ()
1642 (define (assign-any)
1643 (let ([frms (var-frm-conf x)]
1644 [vars (var-var-conf x)])
1645 (let f ([i 1])
1646 (cond
1647 [(set-member? i frms) (f (fxadd1 i))]
1648 [else
1649 (let ([fv (mkfvar i)])
1650 (set-var-loc! x fv)
1651 (for-each-var vars varvec
1652 (lambda (var)
1653 (set-var-frm-conf! var
1654 (add-frm fv (var-frm-conf var)))))
1655 fv)]))))
1656 (define (assign-move x)
1657 (let ([mr (set->list
1658 (set-difference
1659 (var-frm-move x)
1660 (var-frm-conf x)))])
1661 (cond
1662 [(null? mr) #f]
1663 [else
1664 (let ([fv (mkfvar (car mr))])
1665 (set-var-loc! x fv)
1666 (for-each-var (var-var-conf x) varvec
1667 (lambda (var)
1668 (set-var-frm-conf! var
1669 (add-frm fv (var-frm-conf var)))))
1670 (for-each-var (var-var-move x) varvec
1671 (lambda (var)
1672 (set-var-frm-move! var
1673 (add-frm fv (var-frm-move var)))))
1674 fv)])))
1675 (or (assign-move x)
1676 (assign-any))))
1677 (define (NFE idx mask x)
1678 (struct-case x
1679 [(seq e0 e1)
1680 (let ([e0 (E e0)])
1681 (make-seq e0 (NFE idx mask e1)))]
1682 [(ntcall target value args mask^ size)
1683 (make-ntcall target value
1684 (map (lambda (x)
1685 (cond
1686 [(symbol? x) x]
1687 [(nfv? x) (nfv-loc x)]
1688 [else (error who "invalid arg")]))
1689 args)
1690 mask idx)]
1691 [else (error who "invalid NF effect" x)]))
1692 (define (Var x)
1693 (cond
1694 [(var-loc x) =>
1695 (lambda (loc)
1696 (if (fvar? loc)
1697 loc
1698 (assign x)))]
1699 [else x]))
1700 (define (R x)
1701 (cond
1702 [(or (constant? x) (reg? x) (fvar? x)) x]
1703 [(nfv? x)
1704 (or (nfv-loc x)
1705 (error who "unassigned nfv"))]
1706 [(var? x) (Var x)]
1707 [(disp? x)
1708 (make-disp (R (disp-s0 x)) (R (disp-s1 x)))]
1709 [else (error who "invalid R" (unparse x))]))
1710 (define (E x)
1711 (struct-case x
1712 [(seq e0 e1)
1713 (let ([e0 (E e0)])
1714 (make-seq e0 (E e1)))]
1715 [(conditional e0 e1 e2)
1716 (make-conditional (P e0) (E e1) (E e2))]
1717 [(asm-instr op d s)
1718 (case op
1719 [(move load8 load32)
1720 (let ([d (R d)] [s (R s)])
1721 (cond
1722 [(eq? d s)
1723 (make-primcall 'nop '())]
1724 [else
1725 (make-asm-instr op d s)]))]
1726 [(logand logor logxor int+ int- int* mset bset mset32
1727 sll sra srl bswap!
1728 cltd idiv int-/overflow int+/overflow int*/overflow
1729 fl:load fl:store fl:add! fl:sub! fl:mul! fl:div!
1730 fl:from-int fl:shuffle fl:load-single fl:store-single
1731 sll/overflow)
1732 (make-asm-instr op (R d) (R s))]
1733 [(nop) (make-primcall 'nop '())]
1734 [else (error who "invalid op" op)])]
1735 [(nframe vars live body)
1736 (let ([live-frms1
1737 (map (lambda (i) (Var (vector-ref varvec i)))
1738 (set->list (vector-ref live 0)))]
1739 [live-frms2 (set->list (vector-ref live 1))]
1740 [live-nfvs (vector-ref live 2)])
1741 (define (max-frm ls i)
1742 (cond
1743 [(null? ls) i]
1744 [else
1745 (max-frm (cdr ls)
1746 (max i (fvar-idx (car ls))))]))
1747 (define (max-ls ls i)
1748 (cond
1749 [(null? ls) i]
1750 [else
1751 (max-ls (cdr ls) (max i (car ls)))]))
1752 (define (max-nfv ls i)
1753 (cond
1754 [(null? ls) i]
1755 [else
1756 (let ([loc (nfv-loc (car ls))])
1757 (unless (fvar? loc) (error 'max-nfv "not assigned"))
1758 (max-nfv (cdr ls) (max i (fvar-idx loc))))]))
1759 (define (actual-frame-size vars i)
1760 (define (var-conflict? i vs)
1761 (ormap (lambda (xi)
1762 (let ([loc (var-loc (vector-ref varvec xi))])
1763 (and (fvar? loc)
1764 (fx= i (fvar-idx loc)))))
1765 (set->list vs)))
1766 (define (frame-size-ok? i vars)
1767 (or (null? vars)
1768 (let ([x (car vars)])
1769 (and (not (set-member? i (nfv-frm-conf x)))
1770 (not (var-conflict? i (nfv-var-conf x)))
1771 (frame-size-ok? (fxadd1 i) (cdr vars))))))
1772 (cond
1773 [(frame-size-ok? i vars) i]
1774 [else (actual-frame-size vars (fxadd1 i))]))
1775 (define (assign-frame-vars! vars i)
1776 (unless (null? vars)
1777 (let ([v (car vars)] [fv (mkfvar i)])
1778 (set-nfv-loc! v fv)
1779 ;(for-each
1780 ; (lambda (j)
1781 ; (when (fx= j i)
1782 ; (error who "invalid assignment")))
1783 ; (set->list (nfv-frm-conf v)))
1784 (for-each
1785 (lambda (x)
1786 (let ([loc (nfv-loc x)])
1787 (cond
1788 [loc
1789 (when (fx= (fvar-idx loc) i)
1790 (error who "invalid assignment"))]
1791 [else
1792 (set-nfv-nfv-conf! x
1793 (rem-nfv v (nfv-nfv-conf x)))
1794 (set-nfv-frm-conf! x
1795 (add-frm fv (nfv-frm-conf x)))])))
1796 (nfv-nfv-conf v))
1797 (for-each-var (nfv-var-conf v) varvec
1798 (lambda (x)
1799 (let ([loc (var-loc x)])
1800 (cond
1801 [(fvar? loc)
1802 (when (fx= (fvar-idx loc) i)
1803 (error who "invalid assignment"))]
1804 [else
1805 (set-var-frm-conf! x
1806 (add-frm fv (var-frm-conf x)))])))))
1807 (assign-frame-vars! (cdr vars) (fxadd1 i))))
1808 (define (make-mask n)
1809 (let ([v (make-vector (fxsra (fx+ n 7) 3) 0)])
1810 (define (set-bit idx)
1811 (let ([q (fxsra idx 3)]
1812 [r (fxlogand idx 7)])
1813 (vector-set! v q
1814 (fxlogor (vector-ref v q) (fxsll 1 r)))))
1815 (for-each (lambda (x) (set-bit (fvar-idx x))) live-frms1)
1816 (for-each set-bit live-frms2)
1817 (for-each (lambda (x)
1818 (let ([loc (nfv-loc x)])
1819 (when loc
1820 (set-bit (fvar-idx loc)))))
1821 live-nfvs) v))
1822 (let ([i (actual-frame-size vars
1823 (fx+ 2
1824 (max-frm live-frms1
1825 (max-nfv live-nfvs
1826 (max-ls live-frms2 0)))))])
1827 (assign-frame-vars! vars i)
1828 (NFE (fxsub1 i) (make-mask (fxsub1 i)) body)))]
1829 [(primcall op args)
1830 (case op
1831 [(nop interrupt incr/zero? fl:double->single
1832 fl:single->double) x]
1833 [else (error who "invalid effect prim" op)])]
1834 [(shortcut body handler)
1835 (make-shortcut (E body) (E handler))]
1836 [else (error who "invalid effect" (unparse x))]))
1837 (define (P x)
1838 (struct-case x
1839 [(seq e0 e1)
1840 (let ([e0 (E e0)])
1841 (make-seq e0 (P e1)))]
1842 [(conditional e0 e1 e2)
1843 (make-conditional (P e0) (P e1) (P e2))]
1844 [(asm-instr op d s) (make-asm-instr op (R d) (R s))]
1845 [(constant) x]
1846 [(shortcut body handler)
1847 (make-shortcut (P body) (P handler))]
1848 [else (error who "invalid pred" (unparse x))]))
1849 (define (T x)
1850 (struct-case x
1851 [(seq e0 e1)
1852 (let ([e0 (E e0)])
1853 (make-seq e0 (T e1)))]
1854 [(conditional e0 e1 e2)
1855 (make-conditional (P e0) (T e1) (T e2))]
1856 [(primcall op args) x]
1857 [(shortcut body handler)
1858 (make-shortcut (T body) (T handler))]
1859 [else (error who "invalid tail" (unparse x))]))
1860 (T x))
1861 ;;;
1862 (define (Main x)
1863 (struct-case x
1864 [(locals vars body)
1865 (init-vars! vars)
1866 (let ([varvec (list->vector vars)])
1867 (let ([call-live* (uncover-frame-conflicts body varvec)])
1868 (let ([body (rewrite body varvec)])
1869 (make-locals
1870 (cons varvec
1871 (let f ([vars vars])
1872 (cond
1873 [(null? vars) '()]
1874 [(var-loc (car vars)) (f (cdr vars))]
1875 [else (cons (car vars) (f (cdr vars)))])))
1876 body))))]
1877 [else (error 'assign-frame-sizes "invalid main" x)]))
1878 ;;;
1879 (define (ClambdaCase x)
1880 (struct-case x
1881 [(clambda-case info body)
1882 (make-clambda-case info (Main body))]))
1883 ;;;
1884 (define (Clambda x)
1885 (struct-case x
1886 [(clambda label case* cp free* name)
1887 (make-clambda label (map ClambdaCase case*) cp free* name)]))
1888 ;;;
1889 (define (Program x)
1890 (struct-case x
1891 [(codes code* body)
1892 (make-codes (map Clambda code*) (Main body))]))
1893 ;;;
1894 (define (assign-frame-sizes x)
1895 (let ([v (Program x)])
1896 v)))
1897
1898
1899
1900
1901 (module (color-by-chaitin)
1902 (import ListySet)
1903 (import ListyGraphs)
1904 ;(import IntegerSet)
1905 ;(import IntegerGraphs)
1906 ;;;
1907 (define (set-for-each f s)
1908 (for-each f (set->list s)))
1909 ;;;
1910 (define (build-graph x)
1911 (define who 'build-graph)
1912 (define g (empty-graph))
1913 (define (R* ls)
1914 (cond
1915 [(null? ls) (make-empty-set)]
1916 [else (set-union (R (car ls)) (R* (cdr ls)))]))
1917 (define (R x)
1918 (struct-case x
1919 [(constant) (make-empty-set)]
1920 [(var) (singleton x)]
1921 [(disp s0 s1) (set-union (R s0) (R s1))]
1922 [(fvar) (make-empty-set)]
1923 [(code-loc) (make-empty-set)]
1924 [else
1925 (cond
1926 [(symbol? x)
1927 (if (memq x all-registers)
1928 (set-add x (make-empty-set))
1929 (make-empty-set))]
1930 [else (error who "invalid R" x)])]))
1931 ;;; build effect
1932 (define (E x s)
1933 (struct-case x
1934 [(asm-instr op d v)
1935 (case op
1936 [(move load32)
1937 (let ([s (set-rem d s)])
1938 (set-for-each (lambda (y) (add-edge! g d y)) s)
1939 (set-union (R v) s))]
1940 [(load8)
1941 (let ([s (set-rem d s)])
1942 (set-for-each (lambda (y) (add-edge! g d y)) s)
1943 (when (var? d)
1944 (for-each (lambda (r) (add-edge! g d r)) non-8bit-registers))
1945 (when (var? v)
1946 (for-each (lambda (r) (add-edge! g v r)) non-8bit-registers))
1947 (set-union (R v) s))]
1948 [(int-/overflow int+/overflow int*/overflow)
1949 (unless (exception-live-set)
1950 (error who "uninitialized live set"))
1951 (let ([s (set-rem d (set-union s (exception-live-set)))])
1952 (set-for-each (lambda (y) (add-edge! g d y)) s)
1953 (set-union (set-union (R v) (R d)) s))]
1954 [(logand logxor int+ int- int* logor sll sra srl bswap!
1955 sll/overflow)
1956 (let ([s (set-rem d s)])
1957 (set-for-each (lambda (y) (add-edge! g d y)) s)
1958 (set-union (set-union (R v) (R d)) s))]
1959 [(bset)
1960 (when (var? v)
1961 (for-each (lambda (r) (add-edge! g v r))
1962 non-8bit-registers))
1963 (set-union (set-union (R v) (R d)) s)]
1964 [(cltd)
1965 (let ([s (set-rem edx s)])
1966 (when (register? edx)
1967 (set-for-each (lambda (y) (add-edge! g edx y)) s))
1968 (set-union (R eax) s))]
1969 [(idiv)
1970 (let ([s (set-rem eax (set-rem edx s))])
1971 (when (register? eax)
1972 (set-for-each
1973 (lambda (y) (add-edge! g eax y) (add-edge! g edx y))
1974 s))
1975 (set-union (set-union (R eax) (R edx))
1976 (set-union (R v) s)))]
1977 [(mset mset32 fl:load fl:store fl:add! fl:sub! fl:mul! fl:div!
1978 fl:from-int fl:shuffle fl:store-single
1979 fl:load-single)
1980 (set-union (R v) (set-union (R d) s))]
1981 [else (error who "invalid effect" x)])]
1982 [(seq e0 e1) (E e0 (E e1 s))]
1983 [(conditional e0 e1 e2)
1984 (let ([s1 (E e1 s)] [s2 (E e2 s)])
1985 (P e0 s1 s2 (set-union s1 s2)))]
1986 [(ntcall targ value args mask size)
1987 (set-union (R* args) s)]
1988 [(primcall op arg*)
1989 (case op
1990 [(nop fl:single->double fl:double->single) s]
1991 [(interrupt incr/zero?)
1992 (or (exception-live-set) (error who "uninitialized exception"))]
1993 [else (error who "invalid effect primcall" op)])]
1994 [(shortcut body handler)
1995 (let ([s2 (E handler s)])
1996 (parameterize ([exception-live-set s2])
1997 (E body s)))]
1998 [else (error who "invalid effect" (unparse x))]))
1999 (define (P x st sf su)
2000 (struct-case x
2001 [(constant c) (if c st sf)]
2002 [(seq e0 e1)
2003 (E e0 (P e1 st sf su))]
2004 [(conditional e0 e1 e2)
2005 (let ([s1 (P e1 st sf su)] [s2 (P e2 st sf su)])
2006 (P e0 s1 s2 (set-union s1 s2)))]
2007 [(asm-instr op s0 s1)
2008 (set-union (set-union (R s0) (R s1)) su)]
2009 [(shortcut body handler)
2010 (let ([s2 (P handler st sf su)])
2011 (parameterize ([exception-live-set s2])
2012 (P body st sf su)))]
2013 [else (error who "invalid pred" (unparse x))]))
2014 (define (T x)
2015 (struct-case x
2016 [(conditional e0 e1 e2)
2017 (let ([s1 (T e1)] [s2 (T e2)])
2018 (P e0 s1 s2 (set-union s1 s2)))]
2019 [(primcall op rands)
2020 (R* rands)]
2021 [(seq e0 e1) (E e0 (T e1))]
2022 [(shortcut body handler)
2023 (let ([s2 (T handler)])
2024 (parameterize ([exception-live-set s2])
2025 (T body)))]
2026 [else (error who "invalid tail" (unparse x))]))
2027 (define exception-live-set (make-parameter #f))
2028 (let ([s (T x)])
2029 ;(pretty-print (unparse x))
2030 ;(print-graph g)
2031 g))
2032 ;;;
2033 (define (color-graph sp* un* g)
2034 (define (find-low-degree ls g)
2035 (cond
2036 [(null? ls) #f]
2037 [(fx< (length (set->list (node-neighbors (car ls) g)))
2038 (length all-registers))
2039 (car ls)]
2040 [else (find-low-degree (cdr ls) g)]))
2041 (define (find-color/maybe x confs env)
2042 (let ([cr (map (lambda (x)
2043 (cond
2044 [(symbol? x) x]
2045 [(assq x env) => cdr]
2046 [else #f]))
2047 (set->list confs))])
2048 (let ([r* (set->list
2049 (set-difference
2050 (list->set all-registers)
2051 (list->set cr)))])
2052 (if (null? r*)
2053 #f
2054 (car r*)))))
2055 (define (find-color x confs env)
2056 (or (find-color/maybe x confs env)
2057 (error 'find-color "cannot find color for" x)))
2058 (cond
2059 [(and (empty-set? sp*) (empty-set? un*))
2060 (values '() (make-empty-set) '())]
2061 [(find-low-degree (set->list un*) g) =>
2062 (lambda (un)
2063 (let ([n* (node-neighbors un g)])
2064 (delete-node! un g)
2065 (let-values ([(spills sp* env)
2066 (color-graph sp* (set-rem un un*) g)])
2067 (let ([r (find-color un n* env)])
2068 (values spills sp*
2069 (cons (cons un r) env))))))]
2070 [(find-low-degree (set->list sp*) g) =>
2071 (lambda (sp)
2072 (let ([n* (node-neighbors sp g)])
2073 (delete-node! sp g)
2074 (let-values ([(spills sp* env)
2075 (color-graph (set-rem sp sp*) un* g)])
2076 (let ([r (find-color sp n* env)])
2077 (values spills (set-add sp sp*)
2078 (cons (cons sp r) env))))))]
2079 [(pair? (set->list sp*))
2080 (let ([sp (car (set->list sp*))])
2081 (let ([n* (node-neighbors sp g)])
2082 (delete-node! sp g)
2083 (let-values ([(spills sp* env)
2084 (color-graph (set-rem sp sp*) un* g)])
2085 (let ([r (find-color/maybe sp n* env)])
2086 (if r
2087 (values spills (set-add sp sp*)
2088 (cons (cons sp r) env))
2089 (values (cons sp spills) sp* env))))))]
2090 [else (error 'color-graph "whoaaa")]))
2091 ;;;
2092 (define (substitute env x)
2093 (define who 'substitute)
2094 (define (Var x)
2095 (cond
2096 [(assq x env) => cdr]
2097 [else x]))
2098 (define (Rhs x)
2099 (struct-case x
2100 [(var) (Var x)]
2101 [(primcall op rand*)
2102 (make-primcall op (map Rand rand*))]
2103 [else x]))
2104 (define (Rand x)
2105 (struct-case x
2106 [(var) (Var x)]
2107 [else x]))
2108 (define (Lhs x)
2109 (struct-case x
2110 [(var) (Var x)]
2111 [(nfv confs loc)
2112 (or loc (error who "LHS not set" x))]
2113 [else x]))
2114 (define (D x)
2115 (struct-case x
2116 [(constant) x]
2117 [(var) (Var x)]
2118 [(fvar) x]
2119 [else
2120 (if (symbol? x) x (error who "invalid D" x))]))
2121 (define (R x)
2122 (struct-case x
2123 [(constant) x]
2124 [(var) (Var x)]
2125 [(fvar) x]
2126 [(nfv c loc)
2127 (or loc (error who "unset nfv in R" x))]
2128 [(disp s0 s1) (make-disp (D s0) (D s1))]
2129 [else
2130 (if (symbol? x) x (error who "invalid R" x))]))
2131 ;;; substitute effect
2132 (define (E x)
2133 (struct-case x
2134 [(seq e0 e1) (make-seq (E e0) (E e1))]
2135 [(conditional e0 e1 e2)
2136 (make-conditional (P e0) (E e1) (E e2))]
2137 [(asm-instr op x v)
2138 (make-asm-instr op (R x) (R v))]
2139 [(primcall op rands)
2140 (make-primcall op (map R rands))]
2141 [(ntcall) x]
2142 [(shortcut body handler)
2143 (make-shortcut (E body) (E handler))]
2144 [else (error who "invalid effect" (unparse x))]))
2145 (define (P x)
2146 (struct-case x
2147 [(constant) x]
2148 [(asm-instr op x v)
2149 (make-asm-instr op (R x) (R v))]
2150 [(conditional e0 e1 e2)
2151 (make-conditional (P e0) (P e1) (P e2))]
2152 [(seq e0 e1) (make-seq (E e0) (P e1))]
2153 [(shortcut body handler)
2154 (make-shortcut (P body) (P handler))]
2155 [else (error who "invalid pred" (unparse x))]))
2156 (define (T x)
2157 (struct-case x
2158 [(primcall op rands) x]
2159 [(conditional e0 e1 e2)
2160 (make-conditional (P e0) (T e1) (T e2))]
2161 [(seq e0 e1) (make-seq (E e0) (T e1))]
2162 [(shortcut body handler)
2163 (make-shortcut (T body) (T handler))]
2164 [else (error who "invalid tail" (unparse x))]))
2165 ;(print-code x)
2166 (T x))
2167 ;;;
2168 (define (do-spill sp* varvec)
2169 (import conflict-helpers)
2170 (define (find/set-loc x)
2171 (let f ([i 1] [conf (var-frm-conf x)])
2172 (let ([fv (mkfvar i)])
2173 (cond
2174 [(mem-frm? fv conf) (f (fxadd1 i) conf)]
2175 [else
2176 (for-each-var (var-var-conf x) varvec
2177 (lambda (y)
2178 (set-var-var-conf! y
2179 (rem-var x (var-var-conf y)))
2180 (set-var-frm-conf! y
2181 (add-frm fv (var-frm-conf y)))))
2182 (set-var-loc! x fv)
2183 (cons x fv)]))))
2184 (map find/set-loc sp*))
2185 ;;;
2186 (define (add-unspillables un* x)
2187 (define who 'add-unspillables)
2188 (define (mku)
2189 (let ([u (unique-var 'u)])
2190 (set! un* (set-add u un*))
2191 u))
2192 (define (S x k)
2193 (cond
2194 [(or (constant? x) (var? x) (symbol? x))
2195 (k x)]
2196 [else
2197 (let ([u (mku)])
2198 (make-seq (E (make-asm-instr 'move u x)) (k u)))]))
2199 (define (S* ls k)
2200 (cond
2201 [(null? ls) (k '())]
2202 [else
2203 (S (car ls)
2204 (lambda (a)
2205 (S* (cdr ls)
2206 (lambda (d)
2207 (k (cons a d))))))]))
2208 (define (long-imm? x)
2209 (struct-case x
2210 [(constant n)
2211 (cond
2212 [(integer? n)
2213 (not (<= (- (expt 2 31)) n (- (expt 2 31) 1)))]
2214 [else #t])]
2215 [else #f]))
2216 (define (small-operand? x)
2217 (case wordsize
2218 [(4) (not (mem? x))]
2219 [(8)
2220 (struct-case x
2221 [(constant n)
2222 (cond
2223 [(integer? n)
2224 (<= (- (expt 2 31)) n (- (expt 2 31) 1))]
2225 [else #f])]
2226 [else (or (register? x) (var? x))])]
2227 [else (error 'small-operand? "huh?")]))
2228 (define (mem? x)
2229 (or (disp? x) (fvar? x)))
2230 (define (fix-address x k)
2231 (cond
2232 [(disp? x)
2233 (let ([s0 (disp-s0 x)] [s1 (disp-s1 x)])
2234 (cond
2235 [(not (small-operand? s0))
2236 (let ([u (mku)])
2237 (make-seq
2238 (E (make-asm-instr 'move u s0))
2239 (fix-address (make-disp u s1) k)))]
2240 [(not (small-operand? s1))
2241 (let ([u (mku)])
2242 (make-seq
2243 (E (make-asm-instr 'move u s1))
2244 (fix-address (make-disp s0 u) k)))]
2245 [else (k x)]))]
2246 [else (k x)]))
2247 ;;; unspillable effect
2248 (define (E x)
2249 (struct-case x
2250 [(seq e0 e1) (make-seq (E e0) (E e1))]
2251 [(conditional e0 e1 e2)
2252 (make-conditional (P e0) (E e1) (E e2))]
2253 [(asm-instr op a b)
2254 (case op
2255 [(load8 load32)
2256 (fix-address b
2257 (lambda (b)
2258 (cond
2259 [(or (register? a) (var? a))
2260 (make-asm-instr op a b)]
2261 [else
2262 (let ([u (mku)])
2263 (make-seq
2264 (make-asm-instr op u b)
2265 (E (make-asm-instr 'move a u))))])))]
2266 [(logor logxor logand int+ int- int* move
2267 int-/overflow int+/overflow int*/overflow)
2268 (cond
2269 [(and (eq? op 'move) (eq? a b))
2270 (make-primcall 'nop '())]
2271 [(and (= wordsize 8)
2272 (not (eq? op 'move))
2273 (long-imm? b))
2274 (let ([u (mku)])
2275 (make-seq
2276 (E (make-asm-instr 'move u b))
2277 (E (make-asm-instr op a u))))]
2278 [(and (memq op '(int* int*/overflow)) (mem? a))
2279 (let ([u (mku)])
2280 (make-seq
2281 (make-seq
2282 (E (make-asm-instr 'move u a))
2283 (E (make-asm-instr op u b)))
2284 (E (make-asm-instr 'move a u))))]
2285 [(and (mem? a) (not (small-operand? b)))
2286 (let ([u (mku)])
2287 (make-seq
2288 (E (make-asm-instr 'move u b))
2289 (E (make-asm-instr op a u))))]
2290 [(disp? a)
2291 (let ([s0 (disp-s0 a)] [s1 (disp-s1 a)])
2292 (cond
2293 [(not (small-operand? s0))
2294 (let ([u (mku)])
2295 (make-seq
2296 (E (make-asm-instr 'move u s0))
2297 (E (make-asm-instr op (make-disp u s1) b))))]
2298 [(not (small-operand? s1))
2299 (let ([u (mku)])
2300 (make-seq
2301 (E (make-asm-instr 'move u s1))
2302 (E (make-asm-instr op (make-disp s0 u) b))))]
2303 [(small-operand? b) x]
2304 [else
2305 (let ([u (mku)])
2306 (make-seq
2307 (E (make-asm-instr 'move u b))
2308 (E (make-asm-instr op a u))))]))]
2309 [(disp? b)
2310 (let ([s0 (disp-s0 b)] [s1 (disp-s1 b)])
2311 (cond
2312 [(not (small-operand? s0))
2313 (let ([u (mku)])
2314 (make-seq
2315 (E (make-asm-instr 'move u s0))
2316 (E (make-asm-instr op a (make-disp u s1)))))]
2317 [(not (small-operand? s1))
2318 (let ([u (mku)])
2319 (make-seq
2320 (E (make-asm-instr 'move u s1))
2321 (E (make-asm-instr op a (make-disp s0 u)))))]
2322 [else x]))]
2323 [else x])]
2324 [(bswap!)
2325 (cond
2326 [(mem? b)
2327 (let ([u (mku)])
2328 (make-seq
2329 (E (make-asm-instr 'move u a))
2330 (E (make-asm-instr 'bswap! u u))
2331 (E (make-asm-instr 'move b u))))]
2332 [else x])]
2333 [(cltd)
2334 (unless (and (symbol? a) (symbol? b))
2335 (error who "invalid args to cltd"))
2336 x]
2337 [(idiv)
2338 (unless (symbol? a)
2339 (error who "invalid arg to idiv"))
2340 (cond
2341 [(or (var? b) (symbol? b)) x]
2342 [else
2343 (let ([u (mku)])
2344 (make-seq
2345 (E (make-asm-instr 'move u b))
2346 (E (make-asm-instr 'idiv a u))))])]
2347 [(sll sra srl sll/overflow)
2348 (unless (or (constant? b)
2349 (eq? b ecx))
2350 (error who "invalid shift" b))
2351 x]
2352 [(mset mset32 bset)
2353 (cond
2354 [(not (small-operand? b))
2355 (let ([u (mku)])
2356 (make-seq
2357 (E (make-asm-instr 'move u b))
2358 (E (make-asm-instr op a u))))]
2359 [else
2360 (check-disp a
2361 (lambda (a)
2362 (let ([s0 (disp-s0 a)] [s1 (disp-s1 a)])
2363 (cond
2364 [(and (constant? s0) (constant? s1))
2365 (let ([u (mku)])
2366 (make-seq
2367 (make-seq
2368 (E (make-asm-instr 'move u s0))
2369 (E (make-asm-instr 'int+ u s1)))
2370 (make-asm-instr op
2371 (make-disp u (make-constant 0))
2372 b)))]
2373 [else (make-asm-instr op a b)]))))])]
2374 [(fl:load fl:store fl:add! fl:sub! fl:mul! fl:div!
2375 fl:load-single fl:store-single)
2376 (check-disp-arg a
2377 (lambda (a)
2378 (check-disp-arg b
2379 (lambda (b)
2380 (make-asm-instr op a b)))))]
2381 [(fl:from-int fl:shuffle) x]
2382 [else (error who "invalid effect op" op)])]
2383 [(primcall op rands)
2384 (case op
2385 [(nop interrupt incr/zero? fl:single->double
2386 fl:double->single) x]
2387 [else (error who "invalid op in" (unparse x))])]
2388 [(ntcall) x]
2389 [(shortcut body handler)
2390 (let ([body (E body)])
2391 (make-shortcut body (E handler)))]
2392 [else (error who "invalid effect" (unparse x))]))
2393 (define (check-disp-arg x k)
2394 (cond
2395 [(small-operand? x)
2396 (k x)]
2397 [else
2398 (let ([u (mku)])
2399 (make-seq
2400 (E (make-asm-instr 'move u x))
2401 (k u)))]))
2402 (define (check-disp x k)
2403 (struct-case x
2404 [(disp a b)
2405 (check-disp-arg a
2406 (lambda (a)
2407 (check-disp-arg b
2408 (lambda (b)
2409 (k (make-disp a b))))))]
2410 [else (k x)]))
2411 (define (P x)
2412 (struct-case x
2413 [(constant) x]
2414 [(conditional e0 e1 e2)
2415 (make-conditional (P e0) (P e1) (P e2))]
2416 [(seq e0 e1) (make-seq (E e0) (P e1))]
2417 [(asm-instr op a b)
2418 (cond
2419 [(memq op '(fl:= fl:< fl:<= fl:> fl:>=))
2420 (if (mem? a)
2421 (let ([u (mku)])
2422 (make-seq
2423 (E (make-asm-instr 'move u a))
2424 (make-asm-instr op u b)))
2425 x)]
2426 [(and (not (mem? a)) (not (small-operand? a)))
2427 (let ([u (mku)])
2428 (make-seq
2429 (E (make-asm-instr 'move u a))
2430 (P (make-asm-instr op u b))))]
2431 [(and (not (mem? b)) (not (small-operand? b)))
2432 (let ([u (mku)])
2433 (make-seq
2434 (E (make-asm-instr 'move u b))
2435 (P (make-asm-instr op a u))))]
2436 [(and (mem? a) (mem? b))
2437 (let ([u (mku)])
2438 (make-seq
2439 (E (make-asm-instr 'move u b))
2440 (P (make-asm-instr op a u))))]
2441 [else
2442 (check-disp a
2443 (lambda (a)
2444 (check-disp b
2445 (lambda (b)
2446 (make-asm-instr op a b)))))])]
2447 [(shortcut body handler)
2448 (let ([body (P body)])
2449 (make-shortcut body (P handler)))]
2450 [else (error who "invalid pred" (unparse x))]))
2451 (define (T x)
2452 (struct-case x
2453 [(primcall op rands) x]
2454 [(conditional e0 e1 e2)
2455 (make-conditional (P e0) (T e1) (T e2))]
2456 [(seq e0 e1) (make-seq (E e0) (T e1))]
2457 [(shortcut body handler)
2458 (make-shortcut (T body) (T handler))]
2459 [else (error who "invalid tail" (unparse x))]))
2460 (let ([x (T x)])
2461 (values un* x)))
2462 ;;;
2463 (define (color-program x)
2464 (define who 'color-program)
2465 (struct-case x
2466 [(locals vars body)
2467 (let ([varvec (car vars)] [sp* (cdr vars)])
2468 (let loop ([sp* (list->set sp*)] [un* (make-empty-set)] [body body])
2469 (let-values ([(un* body) (add-unspillables un* body)])
2470 (let ([g (build-graph body)])
2471 (let-values ([(spills sp* env) (color-graph sp* un* g)])
2472 (cond
2473 [(null? spills) (substitute env body)]
2474 [else
2475 (let* ([env (do-spill spills varvec)]
2476 [body (substitute env body)])
2477 (loop sp* un* body))]))))))]))
2478 ;;;
2479 (define (color-by-chaitin x)
2480 ;;;
2481 (define (ClambdaCase x)
2482 (struct-case x
2483 [(clambda-case info body)
2484 (make-clambda-case info (color-program body))]))
2485 ;;;
2486 (define (Clambda x)
2487 (struct-case x
2488 [(clambda label case* cp free* name)
2489 (make-clambda label (map ClambdaCase case*) cp free* name)]))
2490 ;;;
2491 (define (Program x)
2492 (struct-case x
2493 [(codes code* body)
2494 (make-codes (map Clambda code*) (color-program body))]))
2495 ;;;
2496 (Program x))
2497 #|chaitin module|#)
2498
2499
2500
2501
2502 (define (compile-call-frame framesize livemask-vec multiarg-rp call-sequence)
2503 (let ([L_CALL (label (gensym))])
2504 (list 'seq
2505 (if (or (= framesize 0) (= framesize 1))
2506 '(seq)
2507 `(subl ,(* (fxsub1 framesize) wordsize) ,fpr))
2508 (jmp L_CALL)
2509 `(byte-vector ,livemask-vec)
2510 `(int ,(* framesize wordsize))
2511 '(current-frame-offset)
2512 multiarg-rp
2513 `(pad ,call-instruction-size
2514 ,L_CALL
2515 ,call-sequence)
2516 (if (or (= framesize 0) (= framesize 1))
2517 '(seq)
2518 `(addl ,(* (fxsub1 framesize) wordsize) ,fpr)))))
2519
2520
2521
2522 (define (flatten-codes x)
2523 (define who 'flatten-codes)
2524 ;;;
2525 (define (FVar i)
2526 `(disp ,(* i (- wordsize)) ,fpr))
2527 ;;;
2528 (define (C x)
2529 (struct-case x
2530 [(code-loc label) (label-address label)]
2531 [(foreign-label L) `(foreign-label ,L)]
2532 [(closure label free*)
2533 (unless (null? free*) (error who "nonempty closure"))
2534 `(obj ,x)]
2535 [(object o)
2536 `(obj ,o)]
2537 [else
2538 (if (integer? x)
2539 x
2540 (error who "invalid constant C" x))]))
2541 (define (BYTE x)
2542 (struct-case x
2543 [(constant x)
2544 (unless (and (integer? x) (fx<= x 255) (fx<= -128 x))
2545 (error who "invalid byte" x))
2546 x]
2547 [else (error who "invalid byte" x)]))
2548 (define (D x)
2549 (struct-case x
2550 [(constant c) (C c)]
2551 [else
2552 (if (symbol? x) x (error who "invalid D" x))]))
2553 (define (R x)
2554 (struct-case x
2555 [(constant c) (C c)]
2556 [(fvar i) (FVar i)]
2557 [(disp s0 s1)
2558 (let ([s0 (D s0)] [s1 (D s1)])
2559 `(disp ,s0 ,s1))]
2560 [else
2561 (if (symbol? x) x (error who "invalid R" x))]))
2562 (define (R/l x)
2563 (struct-case x
2564 [(constant c) (C c)]
2565 [(fvar i) (FVar i)]
2566 [(disp s0 s1)
2567 (let ([s0 (D s0)] [s1 (D s1)])
2568 `(disp ,s0 ,s1))]
2569 [else
2570 (if (symbol? x) (reg/l x) (error who "invalid R/l" x))]))
2571 (define (reg/h x)
2572 (cond
2573 [(assq x '([%eax %ah] [%ebx %bh] [%ecx %ch] [%edx %dh]))
2574 => cadr]
2575 [else (error who "invalid reg/h" x)]))
2576 (define (reg/l x)
2577 (cond
2578 [(assq x '([%eax %al] [%ebx %bl] [%ecx %cl] [%edx %dl]
2579 [%r8 %r8l] [%r9 %r9l] [%r10 %r10l] [%r11 %r11l]
2580 [%r12 %r12l] [%r13 %r13l] [%r14 %r14l] [%r15 %r15l]))
2581 => cadr]
2582 [else (error who "invalid reg/l" x)]))
2583 (define (R/cl x)
2584 (struct-case x
2585 [(constant i)
2586 (unless (fixnum? i)
2587 (error who "invalid R/cl" x))
2588 (fxlogand i (- (* wordsize 8) 1))]
2589 [else
2590 (if (eq? x ecx)
2591 '%cl
2592 (error who "invalid R/cl" x))]))
2593 (define (interrupt? x)
2594 (struct-case x
2595 [(primcall op args) (eq? op 'interrupt)]
2596 [else #f]))
2597 ;;; flatten effect
2598 (define (E x ac)
2599 (struct-case x
2600 [(seq e0 e1) (E e0 (E e1 ac))]
2601 [(conditional e0 e1 e2)
2602 (cond
2603 [(interrupt? e1)
2604 (let ([L (or (exception-label)
2605 (error who "no exception label"))])
2606 (P e0 L #f (E e2 ac)))]
2607 [(interrupt? e2)
2608 (let ([L (or (exception-label)
2609 (error who "no exception label"))])
2610 (P e0 #f L (E e1 ac)))]
2611 [else
2612 (let ([lf (unique-label)] [le (unique-label)])
2613 (P e0 #f lf
2614 (E e1
2615 (cons* `(jmp ,le) lf
2616 (E e2 (cons le ac))))))])]
2617 [(ntcall target value args mask size)
2618 (let ([LCALL (unique-label)])
2619 (define (rp-label value)
2620 (if value
2621 (label-address (sl-mv-error-rp-label))
2622 (label-address (sl-mv-ignore-rp-label))))
2623 (cond
2624 [(string? target) ;; foreign call
2625 (cons* `(movl (foreign-label "ik_foreign_call") %ebx)
2626 (compile-call-frame
2627 size
2628 mask
2629 (rp-label value)
2630 `(call %ebx))
2631 ac)]
2632 [target ;;; known call
2633 (cons* (compile-call-frame
2634 size
2635 mask
2636 (rp-label value)
2637 `(call (label ,target)))
2638 ac)]
2639 [else
2640 (cons* (compile-call-frame
2641 size
2642 mask
2643 (rp-label value)
2644 `(call (disp ,(fx- disp-closure-code closure-tag)
2645 ,cp-register)))
2646 ac)]))]
2647 [(asm-instr op d s)
2648 (case op
2649 [(logand) (cons `(andl ,(R s) ,(R d)) ac)]
2650 [(int+) (cons `(addl ,(R s) ,(R d)) ac)]
2651 [(int*) (cons `(imull ,(R s) ,(R d)) ac)]
2652 [(int-) (cons `(subl ,(R s) ,(R d)) ac)]
2653 [(logor) (cons `(orl ,(R s) ,(R d)) ac)]
2654 [(logxor) (cons `(xorl ,(R s) ,(R d)) ac)]
2655 [(mset) (cons `(movl ,(R s) ,(R d)) ac)]
2656 [(move)
2657 (if (eq? d s)
2658 ac
2659 (cons `(movl ,(R s) ,(R d)) ac))]
2660 [(load8)
2661 (if (eq? d s)
2662 ac
2663 (cons `(movb ,(R/l s) ,(R/l d)) ac))]
2664 [(bset) (cons `(movb ,(R/l s) ,(R d)) ac)]
2665 [(sll) (cons `(sall ,(R/cl s) ,(R d)) ac)]
2666 [(sra) (cons `(sarl ,(R/cl s) ,(R d)) ac)]
2667 [(srl) (cons `(shrl ,(R/cl s) ,(R d)) ac)]
2668 [(idiv) (cons `(idivl ,(R s)) ac)]
2669 [(cltd) (cons `(cltd) ac)]
2670 [(bswap!)
2671 (let ([s (R s)] [d (R d)])
2672 (unless (eq? s d) (error who "invalid instr" x))
2673 (cons `(bswap ,s) ac))]
2674 [(mset32) (cons `(mov32 ,(R s) ,(R d)) ac)]
2675 [(load32) (cons `(mov32 ,(R s) ,(R d)) ac)]
2676 [(int-/overflow)
2677 (let ([L (or (exception-label)
2678 (error who "no exception label"))])
2679 (cons* `(subl ,(R s) ,(R d))
2680 `(jo ,L)
2681 ac))]
2682 [(sll/overflow)
2683 (let ([L (or (exception-label)
2684 (error who "no exception label"))])
2685 (cons* `(sall ,(R/cl s) ,(R d))
2686 `(jo ,L)
2687 ac))]
2688 [(int*/overflow)
2689 (let ([L (or (exception-label)
2690 (error who "no exception label"))])
2691 (cons* `(imull ,(R s) ,(R d))
2692 `(jo ,L)
2693 ac))]
2694 [(int+/overflow)
2695 (let ([L (or (exception-label)
2696 (error who "no exception label"))])
2697 (cons* `(addl ,(R s) ,(R d))
2698 `(jo ,L)
2699 ac))]
2700 [(fl:store)
2701 (cons `(movsd xmm0 ,(R (make-disp s d))) ac)]
2702 [(fl:store-single)
2703 (cons `(movss xmm0 ,(R (make-disp s d))) ac)]
2704 [(fl:load)
2705 (cons `(movsd ,(R (make-disp s d)) xmm0) ac)]
2706 [(fl:load-single)
2707 (cons `(movss ,(R (make-disp s d)) xmm0) ac)]
2708 [(fl:from-int)
2709 (cons `(cvtsi2sd ,(R s) xmm0) ac)]
2710 [(fl:shuffle)
2711 (cons `(pshufb ,(R (make-disp s d)) xmm0) ac)]
2712 [(fl:add!)
2713 (cons `(addsd ,(R (make-disp s d)) xmm0) ac)]
2714 [(fl:sub!)
2715 (cons `(subsd ,(R (make-disp s d)) xmm0) ac)]
2716 [(fl:mul!)
2717 (cons `(mulsd ,(R (make-disp s d)) xmm0) ac)]
2718 [(fl:div!)
2719 (cons `(divsd ,(R (make-disp s d)) xmm0) ac)]
2720 [else (error who "invalid instr" x)])]
2721 [(primcall op rands)
2722 (case op
2723 [(nop) ac]
2724 [(interrupt)
2725 (let ([l (or (exception-label)
2726 (error who "no exception label"))])
2727 (cons `(jmp ,l) ac))]
2728 [(incr/zero?)
2729 (let ([l (or (exception-label)
2730 (error who "no exception label"))])
2731 (cons*
2732 `(addl ,(D (caddr rands)) ,(R (make-disp (car rands) (cadr rands))))
2733 `(je ,l)
2734 ac))]
2735 [(fl:double->single)
2736 (cons '(cvtsd2ss xmm0 xmm0) ac)]
2737 [(fl:single->double)
2738 (cons '(cvtss2sd xmm0 xmm0) ac)]
2739 [else (error who "invalid effect" (unparse x))])]
2740 [(shortcut body handler)
2741 (let ([L (unique-interrupt-label)] [L2 (unique-label)])
2742 (let ([hand (cons L (E handler `((jmp ,L2))))])
2743 (let ([tc (exceptions-conc)])
2744 (set-cdr! tc (append hand (cdr tc)))))
2745 (parameterize ([exception-label L])
2746 (E body (cons L2 ac))))]
2747 [else (error who "invalid effect" (unparse x))]))
2748 ;;;
2749 (define (unique-interrupt-label)
2750 (label (gensym "ERROR")))
2751 (define (unique-label)
2752 (label (gensym)))
2753 ;;;
2754 (define (constant=? x k)
2755 (struct-case x
2756 [(constant k0) (equal? k0 k)]
2757 [else #f]))
2758 ;;;
2759 (define (P x lt lf ac)
2760 (struct-case x
2761 [(constant c)
2762 (if c
2763 (if lt (cons `(jmp ,lt) ac) ac)
2764 (if lf (cons `(jmp ,lf) ac) ac))]
2765 [(seq e0 e1)
2766 (E e0 (P e1 lt lf ac))]
2767 [(conditional e0 e1 e2)
2768 (cond
2769 [(and (constant=? e1 #t) (constant=? e2 #f))
2770 (P e0 lt lf ac)]
2771 [(and (constant=? e1 #f) (constant=? e2 #t))
2772 (P e0 lf lt ac)]
2773 [(and lt lf)
2774 (let ([l (unique-label)])
2775 (P e0 #f l
2776 (P e1 lt lf
2777 (cons l (P e2 lt lf ac)))))]
2778 [lt
2779 (let ([lf (unique-label)] [l (unique-label)])
2780 (P e0 #f l
2781 (P e1 lt lf
2782 (cons l (P e2 lt #f (cons lf ac))))))]
2783 [lf
2784 (let ([lt (unique-label)] [l (unique-label)])
2785 (P e0 #f l
2786 (P e1 lt lf
2787 (cons l (P e2 #f lf (cons lt ac))))))]
2788 [else
2789 (let ([lf (unique-label)] [l (unique-label)])
2790 (P e0 #f l
2791 (P e1 #f #f
2792 (cons `(jmp ,lf)
2793 (cons l (P e2 #f #f (cons lf ac)))))))])]
2794 [(asm-instr op a0 a1)
2795 (let ()
2796 (define (notop x)
2797 (cond
2798 [(assq x '([= !=] [!= =] [< >=] [<= >] [> <=] [>= <]
2799 [u< u>=] [u<= u>] [u> u<=] [u>= u<]
2800 [fl:= fl:o!=] [fl:!= fl:o=]
2801 [fl:< fl:o>=] [fl:<= fl:o>]
2802 [fl:> fl:o<=] [fl:>= fl:o<]
2803 ))
2804 => cadr]
2805 [else (error who "invalid notop" x)]))
2806 (define (jmpname x)
2807 (cond
2808 [(assq x '([= je] [!= jne] [< jl] [<= jle] [> jg] [>= jge]
2809 [u< jb] [u<= jbe] [u> ja] [u>= jae]
2810 [fl:= je] [fl:!= jne]
2811 [fl:< jb] [fl:> ja] [fl:<= jbe] [fl:>= jae]
2812 [fl:o= je] [fl:o!= jne]
2813 [fl:o< jb] [fl:o> ja] [fl:o<= jbe] [fl:o>= jae]
2814 ))
2815 => cadr]
2816 [else (error who "invalid jmpname" x)]))
2817 (define (revjmpname x)
2818 (cond
2819 [(assq x '([= je] [!= jne] [< jg] [<= jge] [> jl] [>= jle]
2820 [u< ja] [u<= jae] [u> jb] [u>= jbe]))
2821 => cadr]
2822 [else (error who "invalid jmpname" x)]))
2823 (define (cmp op a0 a1 lab ac)
2824 (cond
2825 [(memq op '(fl:= fl:!= fl:< fl:<= fl:> fl:>=))
2826 (cons* `(ucomisd ,(R (make-disp a0 a1)) xmm0)
2827 `(,(jmpname op) ,lab)
2828 ;;; BOGUS!
2829 ac)]
2830 [(memq op '(fl:o= fl:o!= fl:o< fl:o<= fl:o> fl:o>=))
2831 (cons* `(ucomisd ,(R (make-disp a0 a1)) xmm0)
2832 `(jp ,lab)
2833 `(,(jmpname op) ,lab)
2834 ac)]
2835 [(or (symbol? a0) (constant? a1))
2836 (cons* `(cmpl ,(R a1) ,(R a0))
2837 `(,(jmpname op) ,lab)
2838 ac)]
2839 [(or (symbol? a1) (constant? a0))
2840 (cons* `(cmpl ,(R a0) ,(R a1))
2841 `(,(revjmpname op) ,lab)
2842 ac)]
2843 [else (error who "invalid cmpops" a0 a1)]))
2844 (cond
2845 [(and lt lf)
2846 (cmp op a0 a1 lt
2847 (cons `(jmp ,lf) ac))]
2848 [lt
2849 (cmp op a0 a1 lt ac)]
2850 [lf
2851 (cmp (notop op) a0 a1 lf ac)]
2852 [else ac]))]
2853 [(shortcut body handler)
2854 (let ([L (unique-interrupt-label)] [lj (unique-label)])
2855 (let ([ac (if (and lt lf) ac (cons lj ac))])
2856 (let ([hand (cons L (P handler (or lt lj) (or lf lj) '()))])
2857 (let ([tc (exceptions-conc)])
2858 (set-cdr! tc (append hand (cdr tc)))))
2859 (parameterize ([exception-label L])
2860 (P body lt lf ac))))]
2861 [else (error who "invalid pred" x)]))
2862 ;;;
2863 (define (T x ac)
2864 (struct-case x
2865 [(seq e0 e1) (E e0 (T e1 ac))]
2866 [(conditional e0 e1 e2)
2867 (let ([L (unique-label)])
2868 (P e0 #f L (T e1 (cons L (T e2 ac)))))]
2869 [(primcall op rands)
2870 (case op
2871 [(return) (cons '(ret) ac)]
2872 [(indirect-jump)
2873 (cons `(jmp (disp ,(fx- disp-closure-code closure-tag) ,cp-register))
2874 ac)]
2875 [(direct-jump)
2876 (cons `(jmp (label ,(code-loc-label (car rands)))) ac)]
2877 [else (error who "invalid tail" x)])]
2878 [(shortcut body handler)
2879 (let ([L (unique-interrupt-label)])
2880 (let ([hand (cons L (T handler '()))])
2881 (let ([tc (exceptions-conc)])
2882 (set-cdr! tc (append hand (cdr tc)))))
2883 (parameterize ([exception-label L])
2884 (T body ac)))]
2885 [else (error who "invalid tail" x)]))
2886 (define exception-label (make-parameter #f))
2887 ;;;
2888 (define (handle-vararg fml-count ac)
2889 (define CONTINUE_LABEL (unique-label))
2890 (define DONE_LABEL (unique-label))
2891 (define CONS_LABEL (unique-label))
2892 (define LOOP_HEAD (unique-label))
2893 (define L_CALL (unique-label))
2894 (cons* (cmpl (int (argc-convention (fxsub1 fml-count))) eax)
2895 (jl CONS_LABEL)
2896 (movl (int nil) ebx)
2897 (jmp DONE_LABEL)
2898 CONS_LABEL
2899 (movl (mem pcb-allocation-redline pcr) ebx)
2900 (addl eax ebx)
2901 (addl eax ebx)
2902 (cmpl ebx apr)
2903 (jle LOOP_HEAD)
2904 ; overflow
2905 (addl eax esp) ; advance esp to cover args
2906 (pushl cpr) ; push current cp
2907 (pushl eax) ; push argc
2908 (negl eax) ; make argc positive
2909 (addl (int (fx* 4 wordsize)) eax) ; add 4 words to adjust frame size
2910 (pushl eax) ; push frame size
2911 (addl eax eax) ; double the number of args
2912 (movl eax (mem (fx* -2 wordsize) fpr)) ; pass it as first arg
2913 (movl (int (argc-convention 1)) eax) ; setup argc
2914 (movl (obj (primref->symbol 'do-vararg-overflow)) cpr)
2915 (movl (mem (- disp-symbol-record-proc record-tag) cpr) cpr)
2916 ;(movl (primref-loc 'do-vararg-overflow) cpr) ; load handler
2917 (compile-call-frame 0 '#() '(int 0) (indirect-cpr-call))
2918 (popl eax) ; pop framesize and drop it
2919 (popl eax) ; reload argc
2920 (popl cpr) ; reload cp
2921 (subl eax fpr) ; readjust fp
2922 LOOP_HEAD
2923 (movl (int nil) ebx)
2924 CONTINUE_LABEL
2925 (movl ebx (mem disp-cdr apr))
2926 (movl (mem fpr eax) ebx)
2927 (movl ebx (mem disp-car apr))
2928 (movl apr ebx)
2929 (addl (int pair-tag) ebx)
2930 (addl (int pair-size) apr)
2931 (addl (int (fxsll 1 fx-shift)) eax)
2932 (cmpl (int (fx- 0 (fxsll fml-count fx-shift))) eax)
2933 (jle CONTINUE_LABEL)
2934 DONE_LABEL
2935 (movl ebx (mem (fx- 0 (fxsll fml-count fx-shift)) fpr))
2936 ac))
2937 ;;;
2938 (define (properize args proper ac)
2939 (cond
2940 [proper ac]
2941 [else
2942 (handle-vararg (length (cdr args)) ac)]))
2943 ;;;
2944 (define (ClambdaCase x ac)
2945 (struct-case x
2946 [(clambda-case info body)
2947 (struct-case info
2948 [(case-info L args proper)
2949 (let ([lothers (unique-label)])
2950 (cons* `(cmpl ,(argc-convention
2951 (if proper
2952 (length (cdr args))
2953 (length (cddr args))))
2954 ,argc-register)
2955 (cond
2956 [proper `(jne ,lothers)]
2957 [(> (argc-convention 0) (argc-convention 1))
2958 `(jg ,lothers)]
2959 [else
2960 `(jl ,lothers)])
2961 (properize args proper
2962 (cons (label L)
2963 (T body (cons lothers ac))))))])]))
2964 ;;;
2965 (define (Clambda x)
2966 (struct-case x
2967 [(clambda L case* cp free* name)
2968 (cons* (length free*)
2969 `(name ,name)
2970 (label L)
2971 (let ([ac (list '(nop))])
2972 (parameterize ([exceptions-conc ac])
2973 (let f ([case* case*])
2974 (cond
2975 [(null? case*)
2976 (cons `(jmp (label ,(sl-invalid-args-label))) ac)]
2977 [else
2978 (ClambdaCase (car case*) (f (cdr case*)))])))))]))
2979 ;;;
2980 (define exceptions-conc (make-parameter #f))
2981 ;;;
2982 (define (Program x)
2983 (struct-case x
2984 [(codes code* body)
2985 (cons (cons* 0
2986 (label (gensym))
2987 (let ([ac (list '(nop))])
2988 (parameterize ([exceptions-conc ac])
2989 (T body ac))))
2990 (map Clambda code*))]))
2991 (Program x))
2992
2993 (define (print-code x)
2994 (parameterize ([print-gensym '#t])
2995 (pretty-print (unparse x))))
2996
2997 (define (alt-cogen x)
2998 (define (time-it name proc)
2999 (proc))
3000 (let* ([x (introduce-primcalls x)]
3001 [x (eliminate-fix x)]
3002 [x (insert-engine-checks x)]
3003 [x (insert-stack-overflow-check x)]
3004 [x (specify-representation x)]
3005 [x (impose-calling-convention/evaluation-order x)]
3006 [x (time-it "frame" (lambda () (assign-frame-sizes x)))]
3007 [x (time-it "register" (lambda () (color-by-chaitin x)))]
3008 [ls (flatten-codes x)])
3009 ls))
3010
3011
3012 #|module alt-cogen|#)
3013
3014