| 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 primops (primop? cogen-primop) |
| 18 |
; (define (primop? x) #f) |
| 19 |
; (define cogen-primop (lambda args (error 'cogen-primop "not yet")))) |
| 20 |
; |
| 21 |
;#!eof |
| 22 |
|
| 23 |
;(define-syntax export-all-module |
| 24 |
; (syntax-rules (define) |
| 25 |
; [(_ M (define name* v*) ...) |
| 26 |
; (module M (name* ...) |
| 27 |
; (define name* v*) ...)])) |
| 28 |
; |
| 29 |
;(export-all-module object-representation |
| 30 |
; (define fixnum-scale 4) |
| 31 |
; (define fixnum-shift 2) |
| 32 |
; (define fixnum-tag 0) |
| 33 |
; (define fixnum-mask 3)) |
| 34 |
|
| 35 |
(module primops (primop? get-primop set-primop!) |
| 36 |
(define cookie (gensym)) |
| 37 |
(define (primop? x) |
| 38 |
(and (getprop x cookie) #t)) |
| 39 |
(define (get-primop x) |
| 40 |
(or (getprop x cookie) |
| 41 |
(error 'getprimop "not a primitive" x))) |
| 42 |
(define (set-primop! x v) |
| 43 |
(putprop x cookie v)) |
| 44 |
) |
| 45 |
|
| 46 |
(module (specify-representation) |
| 47 |
(import primops) |
| 48 |
(define-struct PH |
| 49 |
(interruptable? p-handler p-handled? v-handler v-handled? e-handler e-handled?)) |
| 50 |
(define interrupt-handler |
| 51 |
(make-parameter (lambda () (error 'interrupt-handler "uninitialized")))) |
| 52 |
(define (interrupt) |
| 53 |
((interrupt-handler)) |
| 54 |
(prm 'interrupt)) |
| 55 |
(define (with-interrupt-handler p x ctxt args |
| 56 |
make-interrupt-call make-no-interrupt-call |
| 57 |
k) |
| 58 |
(cond |
| 59 |
[(not (PH-interruptable? p)) |
| 60 |
(parameterize ([interrupt-handler |
| 61 |
(lambda () |
| 62 |
(error 'cogen "uninterruptable" |
| 63 |
x args ctxt))]) |
| 64 |
(k))] |
| 65 |
[else |
| 66 |
(let ([interrupted? #f]) |
| 67 |
(let ([body |
| 68 |
(parameterize ([interrupt-handler |
| 69 |
(lambda () (set! interrupted? #t))]) |
| 70 |
(k))]) |
| 71 |
(cond |
| 72 |
[(not interrupted?) body] |
| 73 |
[(eq? ctxt 'V) |
| 74 |
(let ([h (make-interrupt-call x args)]) |
| 75 |
(if (struct-case body |
| 76 |
[(primcall op) (eq? op 'interrupt)] |
| 77 |
[else #f]) |
| 78 |
(make-no-interrupt-call x args) |
| 79 |
(make-shortcut body h)))] |
| 80 |
[(eq? ctxt 'E) |
| 81 |
(let ([h (make-interrupt-call x args)]) |
| 82 |
(if (struct-case body |
| 83 |
[(primcall op) (eq? op 'interrupt)] |
| 84 |
[else #f]) |
| 85 |
(make-no-interrupt-call x args) |
| 86 |
(make-shortcut body h)))] |
| 87 |
[(eq? ctxt 'P) |
| 88 |
(let ([h (prm '!= (make-interrupt-call x args) (K bool-f))]) |
| 89 |
(if (struct-case body |
| 90 |
[(primcall op) (eq? op 'interrupt)] |
| 91 |
[else #f]) |
| 92 |
(prm '!= (make-no-interrupt-call x args) (K bool-f)) |
| 93 |
(make-shortcut body h)))] |
| 94 |
[else (error 'with-interrupt-handler "invalid context" ctxt)])))])) |
| 95 |
(define (copy-tag orig new) |
| 96 |
(struct-case orig |
| 97 |
[(known _ t) (make-known new t)] |
| 98 |
[else new])) |
| 99 |
(define (remove-tag x) |
| 100 |
(struct-case x |
| 101 |
[(known expr t) expr] |
| 102 |
[else x])) |
| 103 |
(define-syntax with-tmp |
| 104 |
(lambda (x) |
| 105 |
(syntax-case x () |
| 106 |
[(_ ([lhs* rhs*] ...) b b* ...) |
| 107 |
(with-syntax ([(n* ...) (generate-temporaries #'(lhs* ...))]) |
| 108 |
#'(let ([lhs* rhs*] ...) |
| 109 |
(let ([n* (unique-var 'lhs*)] ...) |
| 110 |
(make-bind (list n* ...) (list lhs* ...) |
| 111 |
(let ([lhs* (copy-tag lhs* n*)] ...) |
| 112 |
(seq* b b* ...))))))]))) |
| 113 |
;;; if ctxt is V: |
| 114 |
;;; if cogen-value, then V |
| 115 |
;;; if cogen-pred, then (if P #f #t) |
| 116 |
;;; if cogen-effect, then (seq E (void)) |
| 117 |
;;; |
| 118 |
;;; if ctxt is P: |
| 119 |
;;; if cogen-pred, then P |
| 120 |
;;; if cogen-value, then (!= V #f) |
| 121 |
;;; if cogen-effect, then (seq E #t) |
| 122 |
;;; |
| 123 |
;;; if ctxt is E: |
| 124 |
;;; if cogen-effect, then E |
| 125 |
;;; if cogen-value, then (let ([tmp V]) (nop)) |
| 126 |
;;; if cogen-pred, then (if P (nop) (nop)) |
| 127 |
(define (simplify* args k) |
| 128 |
(define (S* ls) |
| 129 |
(cond |
| 130 |
[(null? ls) (values '() '() '())] |
| 131 |
[else |
| 132 |
(let-values ([(lhs* rhs* arg*) (S* (cdr ls))]) |
| 133 |
(let ([a (car ls)]) |
| 134 |
(struct-case a |
| 135 |
[(known expr type) |
| 136 |
(struct-case expr |
| 137 |
[(constant i) |
| 138 |
;;; erase known tag |
| 139 |
(values lhs* rhs* (cons expr arg*))] |
| 140 |
[else |
| 141 |
;(printf "known ~s ~s\n" type expr) |
| 142 |
(let ([tmp (unique-var 'tmp)]) |
| 143 |
(values (cons tmp lhs*) |
| 144 |
(cons (V expr) rhs*) |
| 145 |
(cons (make-known tmp type) arg*)))])] |
| 146 |
[(constant i) |
| 147 |
(values lhs* rhs* (cons a arg*))] |
| 148 |
[else |
| 149 |
(let ([t (unique-var 'tmp)]) |
| 150 |
(values (cons t lhs*) (cons (V a) rhs*) (cons t arg*)))])))])) |
| 151 |
(let-values ([(lhs* rhs* args) (S* args)]) |
| 152 |
(cond |
| 153 |
[(null? lhs*) (k args)] |
| 154 |
[else |
| 155 |
(make-bind lhs* rhs* (k args))]))) |
| 156 |
;;; |
| 157 |
(define (make-cogen-handler make-interrupt-call make-no-interrupt-call) |
| 158 |
(define (cogen-primop x ctxt args) |
| 159 |
(define (interrupt? x) |
| 160 |
(struct-case x |
| 161 |
[(primcall x) (eq? x 'interrupt)] |
| 162 |
[else #f])) |
| 163 |
(let ([p (get-primop x)]) |
| 164 |
(simplify* args |
| 165 |
(lambda (args) |
| 166 |
(with-interrupt-handler p x ctxt (map T args) |
| 167 |
make-interrupt-call make-no-interrupt-call |
| 168 |
(lambda () |
| 169 |
(case ctxt |
| 170 |
[(P) |
| 171 |
(cond |
| 172 |
[(PH-p-handled? p) |
| 173 |
(apply (PH-p-handler p) args)] |
| 174 |
[(PH-v-handled? p) |
| 175 |
(let ([e (apply (PH-v-handler p) args)]) |
| 176 |
(if (interrupt? e) e (prm '!= e (K bool-f))))] |
| 177 |
[(PH-e-handled? p) |
| 178 |
(let ([e (apply (PH-e-handler p) args)]) |
| 179 |
(if (interrupt? e) e (make-seq e (K #t))))] |
| 180 |
[else (error 'cogen-primop "not handled" x)])] |
| 181 |
[(V) |
| 182 |
(cond |
| 183 |
[(PH-v-handled? p) |
| 184 |
(apply (PH-v-handler p) args)] |
| 185 |
[(PH-p-handled? p) |
| 186 |
(let ([e (apply (PH-p-handler p) args)]) |
| 187 |
(if (interrupt? e) |
| 188 |
e |
| 189 |
(make-conditional e (K bool-t) (K bool-f))))] |
| 190 |
[(PH-e-handled? p) |
| 191 |
(let ([e (apply (PH-e-handler p) args)]) |
| 192 |
(if (interrupt? e) e (make-seq e (K void-object))))] |
| 193 |
[else (error 'cogen-primop "not handled" x)])] |
| 194 |
[(E) |
| 195 |
(cond |
| 196 |
[(PH-e-handled? p) |
| 197 |
(apply (PH-e-handler p) args)] |
| 198 |
[(PH-p-handled? p) |
| 199 |
(let ([e (apply (PH-p-handler p) args)]) |
| 200 |
(if (interrupt? e) |
| 201 |
e |
| 202 |
(make-conditional e (prm 'nop) (prm 'nop))))] |
| 203 |
[(PH-v-handled? p) |
| 204 |
(let ([e (apply (PH-v-handler p) args)]) |
| 205 |
(if (interrupt? e) |
| 206 |
e |
| 207 |
(with-tmp ([t e]) (prm 'nop))))] |
| 208 |
[else (error 'cogen-primop "not handled" x)])] |
| 209 |
[else |
| 210 |
(error 'cogen-primop "invalid context" ctxt)]))))))) |
| 211 |
cogen-primop) |
| 212 |
(module (cogen-primop cogen-debug-primop) |
| 213 |
(define (primop-interrupt-handler x) |
| 214 |
(case x |
| 215 |
[(fx+) 'error@fx+] |
| 216 |
[(fx-) 'error@fx-] |
| 217 |
[(fx*) 'error@fx*] |
| 218 |
[(add1) 'error@add1] |
| 219 |
[(sub1) 'error@sub1] |
| 220 |
[(fxadd1) 'error@fxadd1] |
| 221 |
[(fxsub1) 'error@fxsub1] |
| 222 |
[(fxarithmetic-shift-left) 'error@fxarithmetic-shift-left] |
| 223 |
[(fxarithmetic-shift-right) 'error@fxarithmetic-shift-right] |
| 224 |
[else x])) |
| 225 |
(define (make-interrupt-call op args) |
| 226 |
(make-funcall |
| 227 |
(V (make-primref (primop-interrupt-handler op))) |
| 228 |
args)) |
| 229 |
(define (make-no-interrupt-call op args) |
| 230 |
(make-funcall (V (make-primref op)) args)) |
| 231 |
(define cogen-primop |
| 232 |
(make-cogen-handler make-interrupt-call make-no-interrupt-call)) |
| 233 |
(define (cogen-debug-primop op src/loc ctxt args) |
| 234 |
(define (make-call op args) |
| 235 |
(make-funcall |
| 236 |
(V (make-primref 'debug-call)) |
| 237 |
(cons* (V src/loc) (V (make-primref op)) args))) |
| 238 |
((make-cogen-handler make-call make-call) |
| 239 |
op ctxt args))) |
| 240 |
|
| 241 |
|
| 242 |
(define-syntax define-primop |
| 243 |
(lambda (x) |
| 244 |
(define (cogen-name stx name suffix) |
| 245 |
(datum->syntax stx |
| 246 |
(string->symbol |
| 247 |
(format "cogen-~a-~a" |
| 248 |
suffix |
| 249 |
(syntax->datum name))))) |
| 250 |
(define (generate-handler name ctxt case*) |
| 251 |
(define (filter-cases case*) |
| 252 |
(syntax-case case* () |
| 253 |
[() '()] |
| 254 |
[([(c . arg*) b b* ...] . rest) |
| 255 |
(free-identifier=? #'c ctxt) |
| 256 |
(cons #'[arg* b b* ...] (filter-cases #'rest))] |
| 257 |
[(c . rest) (filter-cases #'rest)])) |
| 258 |
(let ([case* (filter-cases case*)]) |
| 259 |
(with-syntax ([ctxt ctxt] [name name] |
| 260 |
[(case* ...) case*] |
| 261 |
[handled? (not (null? case*))]) |
| 262 |
#'[(case-lambda |
| 263 |
case* ... |
| 264 |
[args (interrupt)]) |
| 265 |
handled?]))) |
| 266 |
(syntax-case x () |
| 267 |
[(stx name int? case* ...) |
| 268 |
(with-syntax ([cogen-p (cogen-name #'stx #'name "pred")] |
| 269 |
[cogen-e (cogen-name #'stx #'name "effect")] |
| 270 |
[cogen-v (cogen-name #'stx #'name "value")] |
| 271 |
[interruptable? |
| 272 |
(syntax-case #'int? (safe unsafe) |
| 273 |
[safe #t] [unsafe #f])] |
| 274 |
[(p-handler phandled?) |
| 275 |
(generate-handler #'name #'P #'(case* ...))] |
| 276 |
[(v-handler vhandled?) |
| 277 |
(generate-handler #'name #'V #'(case* ...))] |
| 278 |
[(e-handler ehandled?) |
| 279 |
(generate-handler #'name #'E #'(case* ...))]) |
| 280 |
#'(begin |
| 281 |
(define cogen-p p-handler) |
| 282 |
(define cogen-v v-handler) |
| 283 |
(define cogen-e e-handler) |
| 284 |
(module () |
| 285 |
(set-primop! 'name |
| 286 |
(make-PH interruptable? |
| 287 |
cogen-p phandled? |
| 288 |
cogen-v vhandled? |
| 289 |
cogen-e ehandled?)))))]))) |
| 290 |
|
| 291 |
|
| 292 |
(define (handle-fix lhs* rhs* body) |
| 293 |
(define (closure-size x) |
| 294 |
(struct-case x |
| 295 |
[(closure code free*) |
| 296 |
(if (null? free*) |
| 297 |
0 |
| 298 |
(align (+ disp-closure-data |
| 299 |
(* (length free*) wordsize))))])) |
| 300 |
(define (partition p? lhs* rhs*) |
| 301 |
(cond |
| 302 |
[(null? lhs*) (values '() '() '() '())] |
| 303 |
[else |
| 304 |
(let-values ([(a* b* c* d*) |
| 305 |
(partition p? (cdr lhs*) (cdr rhs*))] |
| 306 |
[(x y) (values (car lhs*) (car rhs*))]) |
| 307 |
(cond |
| 308 |
[(p? x y) |
| 309 |
(values (cons x a*) (cons y b*) c* d*)] |
| 310 |
[else |
| 311 |
(values a* b* (cons x c*) (cons y d*))]))])) |
| 312 |
(define (combinator? lhs rhs) |
| 313 |
(struct-case rhs |
| 314 |
[(closure code free*) (null? free*)])) |
| 315 |
(define (sum n* n) |
| 316 |
(cond |
| 317 |
[(null? n*) n] |
| 318 |
[else (sum (cdr n*) (+ n (car n*)))])) |
| 319 |
(define (adders lhs n n*) |
| 320 |
(cond |
| 321 |
[(null? n*) '()] |
| 322 |
[else |
| 323 |
(cons (prm 'int+ lhs (K n)) |
| 324 |
(adders lhs (+ n (car n*)) (cdr n*)))])) |
| 325 |
(define (build-closures lhs* rhs* body) |
| 326 |
(let ([lhs (car lhs*)] [rhs (car rhs*)] |
| 327 |
[lhs* (cdr lhs*)] [rhs* (cdr rhs*)]) |
| 328 |
(let ([n (closure-size rhs)] |
| 329 |
[n* (map closure-size rhs*)]) |
| 330 |
(make-bind (list lhs) |
| 331 |
(list (prm 'alloc |
| 332 |
(K (sum n* n)) |
| 333 |
(K closure-tag))) |
| 334 |
(make-bind lhs* (adders lhs n n*) |
| 335 |
body))))) |
| 336 |
(define (build-setters lhs* rhs* body) |
| 337 |
(define (build-setter lhs rhs body) |
| 338 |
(struct-case rhs |
| 339 |
[(closure code free*) |
| 340 |
(make-seq |
| 341 |
(prm 'mset lhs |
| 342 |
(K (- disp-closure-code closure-tag)) |
| 343 |
(V code)) |
| 344 |
(let f ([ls free*] |
| 345 |
[i (- disp-closure-data closure-tag)]) |
| 346 |
(cond |
| 347 |
[(null? ls) body] |
| 348 |
[else |
| 349 |
(make-seq |
| 350 |
(prm 'mset lhs (K i) (V (car ls))) |
| 351 |
(f (cdr ls) (+ i wordsize)))])))])) |
| 352 |
(cond |
| 353 |
[(null? lhs*) body] |
| 354 |
[else |
| 355 |
(build-setter (car lhs*) (car rhs*) |
| 356 |
(build-setters (cdr lhs*) (cdr rhs*) body))])) |
| 357 |
(let-values ([(flhs* frhs* clhs* crhs*) |
| 358 |
(partition combinator? lhs* rhs*)]) |
| 359 |
(cond |
| 360 |
[(null? clhs*) (make-bind flhs* (map V frhs*) body)] |
| 361 |
[(null? flhs*) |
| 362 |
(build-closures clhs* crhs* |
| 363 |
(build-setters clhs* crhs* body))] |
| 364 |
[else |
| 365 |
(make-bind flhs* (map V frhs*) |
| 366 |
(build-closures clhs* crhs* |
| 367 |
(build-setters clhs* crhs* body)))]))) |
| 368 |
|
| 369 |
|
| 370 |
(define (constant-rep x) |
| 371 |
(let ([c (constant-value x)]) |
| 372 |
(cond |
| 373 |
[(fx? c) (make-constant (* c fx-scale))] |
| 374 |
[(boolean? c) (make-constant (if c bool-t bool-f))] |
| 375 |
[(eq? c (void)) (make-constant void-object)] |
| 376 |
[(bwp-object? c) (make-constant bwp-object)] |
| 377 |
[(char? c) (make-constant |
| 378 |
(fxlogor char-tag |
| 379 |
(fxsll (char->integer c) char-shift)))] |
| 380 |
[(null? c) (make-constant nil)] |
| 381 |
[(eof-object? c) (make-constant eof)] |
| 382 |
[(object? c) (error 'constant-rep "double-wrap")] |
| 383 |
[else (make-constant (make-object c))]))) |
| 384 |
|
| 385 |
(define (V x) ;;; erase known values |
| 386 |
(struct-case x |
| 387 |
[(known x t) |
| 388 |
(unknown-V x)] |
| 389 |
[else (unknown-V x)])) |
| 390 |
|
| 391 |
(define (unknown-V x) |
| 392 |
(struct-case x |
| 393 |
[(constant) (constant-rep x)] |
| 394 |
[(var) x] |
| 395 |
[(primref name) |
| 396 |
(prm 'mref |
| 397 |
(K (make-object (primref->symbol name))) |
| 398 |
(K (- disp-symbol-record-value symbol-ptag)))] |
| 399 |
[(code-loc) (make-constant x)] |
| 400 |
[(closure) (make-constant x)] |
| 401 |
[(bind lhs* rhs* body) |
| 402 |
(make-bind lhs* (map V rhs*) (V body))] |
| 403 |
[(fix lhs* rhs* body) |
| 404 |
(handle-fix lhs* rhs* (V body))] |
| 405 |
[(conditional e0 e1 e2) |
| 406 |
(make-conditional (P e0) (V e1) (V e2))] |
| 407 |
[(seq e0 e1) |
| 408 |
(make-seq (E e0) (V e1))] |
| 409 |
[(primcall op arg*) |
| 410 |
(case op |
| 411 |
[(debug-call) |
| 412 |
(cogen-debug-call op 'V arg* V)] |
| 413 |
[else (cogen-primop op 'V arg*)])] |
| 414 |
[(forcall op arg*) |
| 415 |
(make-forcall op (map V arg*))] |
| 416 |
[(funcall rator arg*) |
| 417 |
(make-funcall (Function rator) (map V arg*))] |
| 418 |
[(jmpcall label rator arg*) |
| 419 |
(make-jmpcall label (V rator) (map V arg*))] |
| 420 |
[else (error 'cogen-V "invalid value expr" x)])) |
| 421 |
|
| 422 |
(define (cogen-debug-call op ctxt arg* k) |
| 423 |
(define (fail) |
| 424 |
(k (make-funcall (make-primref 'debug-call) arg*))) |
| 425 |
(assert (>= (length arg*) 2)) |
| 426 |
(let ([src/expr (car arg*)] |
| 427 |
[op (cadr arg*)] |
| 428 |
[args (cddr arg*)]) |
| 429 |
(struct-case (remove-tag op) |
| 430 |
[(primref name) |
| 431 |
(if (primop? name) |
| 432 |
(cogen-debug-primop name src/expr ctxt args) |
| 433 |
(fail))] |
| 434 |
[else (fail)]))) |
| 435 |
|
| 436 |
(define (P x) |
| 437 |
(struct-case x |
| 438 |
[(constant c) (if c (K #t) (K #f))] |
| 439 |
[(primref) (K #t)] |
| 440 |
[(code-loc) (K #t)] |
| 441 |
[(closure) (K #t)] |
| 442 |
[(bind lhs* rhs* body) |
| 443 |
(make-bind lhs* (map V rhs*) (P body))] |
| 444 |
[(conditional e0 e1 e2) |
| 445 |
(make-conditional (P e0) (P e1) (P e2))] |
| 446 |
[(seq e0 e1) |
| 447 |
(make-seq (E e0) (P e1))] |
| 448 |
[(fix lhs* rhs* body) |
| 449 |
(handle-fix lhs* rhs* (P body))] |
| 450 |
[(primcall op arg*) |
| 451 |
(case op |
| 452 |
[(debug-call) |
| 453 |
(cogen-debug-call op 'P arg* P)] |
| 454 |
[else (cogen-primop op 'P arg*)])] |
| 455 |
[(var) (prm '!= (V x) (V (K #f)))] |
| 456 |
[(funcall) (prm '!= (V x) (V (K #f)))] |
| 457 |
[(jmpcall) (prm '!= (V x) (V (K #f)))] |
| 458 |
[(forcall) (prm '!= (V x) (V (K #f)))] |
| 459 |
[(known expr type) |
| 460 |
;;; FIXME: suboptimal |
| 461 |
(P expr)] |
| 462 |
[else (error 'cogen-P "invalid pred expr" x)])) |
| 463 |
|
| 464 |
(define (E x) |
| 465 |
(struct-case x |
| 466 |
[(constant) (nop)] |
| 467 |
[(var) (nop)] |
| 468 |
[(primref) (nop)] |
| 469 |
[(code-loc) (nop)] |
| 470 |
[(closure) (nop)] |
| 471 |
[(bind lhs* rhs* body) |
| 472 |
(make-bind lhs* (map V rhs*) (E body))] |
| 473 |
[(conditional e0 e1 e2) |
| 474 |
(make-conditional (P e0) (E e1) (E e2))] |
| 475 |
[(seq e0 e1) |
| 476 |
(make-seq (E e0) (E e1))] |
| 477 |
[(fix lhs* rhs* body) |
| 478 |
(handle-fix lhs* rhs* (E body))] |
| 479 |
[(primcall op arg*) |
| 480 |
(case op |
| 481 |
[(debug-call) |
| 482 |
(cogen-debug-call op 'E arg* E)] |
| 483 |
[else (cogen-primop op 'E arg*)])] |
| 484 |
[(forcall op arg*) |
| 485 |
(make-forcall op (map V arg*))] |
| 486 |
[(funcall rator arg*) |
| 487 |
(make-funcall (Function rator) (map V arg*))] |
| 488 |
[(jmpcall label rator arg*) |
| 489 |
(make-jmpcall label (V rator) (map V arg*))] |
| 490 |
[(known expr type) |
| 491 |
;;; FIXME: suboptimal |
| 492 |
(E expr)] |
| 493 |
[else (error 'cogen-E "invalid effect expr" x)])) |
| 494 |
|
| 495 |
(define (Function x) |
| 496 |
(define (Function x check?) |
| 497 |
(define (nonproc x check?) |
| 498 |
(cond |
| 499 |
[check? |
| 500 |
(with-tmp ([x (V x)]) |
| 501 |
(make-shortcut |
| 502 |
(make-seq |
| 503 |
(make-conditional |
| 504 |
(tag-test x closure-mask closure-tag) |
| 505 |
(prm 'nop) |
| 506 |
(prm 'interrupt)) |
| 507 |
x) |
| 508 |
(V (make-funcall (make-primref 'error) |
| 509 |
(list (K 'apply) (K "not a procedure") x)))))] |
| 510 |
[else |
| 511 |
(V x)])) |
| 512 |
(struct-case x |
| 513 |
[(primcall op args) |
| 514 |
(cond |
| 515 |
[(and (eq? op 'top-level-value) |
| 516 |
(= (length args) 1) |
| 517 |
(let f ([x (car args)]) |
| 518 |
(struct-case x |
| 519 |
[(constant x) |
| 520 |
(and (symbol? x) x)] |
| 521 |
[(known x t) (f x)] |
| 522 |
[else #f]))) => |
| 523 |
(lambda (sym) |
| 524 |
(reset-symbol-proc! sym) |
| 525 |
(prm 'mref (T (K sym)) |
| 526 |
(K (- disp-symbol-record-proc symbol-ptag))))] |
| 527 |
[else (nonproc x check?)])] |
| 528 |
[(primref op) (V x)] |
| 529 |
[(known x t) |
| 530 |
(cond |
| 531 |
[(eq? (T:procedure? t) 'yes) |
| 532 |
;(record-optimization 'procedure x) |
| 533 |
(Function x #f)] |
| 534 |
[else (Function x check?)])] |
| 535 |
[else (nonproc x check?)])) |
| 536 |
(Function x #t)) |
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
(define record-optimization^ |
| 541 |
(let ([h (make-eq-hashtable)]) |
| 542 |
(lambda (what expr) |
| 543 |
(let ([n (hashtable-ref h what 0)]) |
| 544 |
(hashtable-set! h what (+ n 1)) |
| 545 |
(printf "optimize ~a[~s]: ~s\n" what n (unparse expr)))))) |
| 546 |
(define-syntax record-optimization |
| 547 |
(syntax-rules () |
| 548 |
[(_ what expr) (void)])) |
| 549 |
|
| 550 |
;;;======================================================================== |
| 551 |
;;; |
| 552 |
(define (interrupt-unless x) |
| 553 |
(make-conditional x (prm 'nop) (interrupt))) |
| 554 |
(define (interrupt-when x) |
| 555 |
(make-conditional x (interrupt) (prm 'nop))) |
| 556 |
(define (interrupt-unless-fixnum x) |
| 557 |
(interrupt-unless (tag-test x fx-mask fx-tag))) |
| 558 |
|
| 559 |
|
| 560 |
(define (T x) |
| 561 |
(struct-case x |
| 562 |
[(var) x] |
| 563 |
[(constant i) (constant-rep x)] |
| 564 |
[(known expr type) |
| 565 |
(make-known (T expr) type)] |
| 566 |
[else (error 'cogen-T "invalid" (unparse x))])) |
| 567 |
|
| 568 |
(define (ClambdaCase x) |
| 569 |
(struct-case x |
| 570 |
[(clambda-case info body) |
| 571 |
(make-clambda-case info (V body))] |
| 572 |
[else (error 'specify-rep "invalid clambda-case" x)])) |
| 573 |
;;; |
| 574 |
(define (Clambda x) |
| 575 |
(struct-case x |
| 576 |
[(clambda label case* cp free* name) |
| 577 |
(make-clambda label |
| 578 |
(map ClambdaCase case*) |
| 579 |
cp free* name)] |
| 580 |
[else (error 'specify-rep "invalid clambda" x)])) |
| 581 |
;;; |
| 582 |
(define (Program x) |
| 583 |
(struct-case x |
| 584 |
[(codes code* body) |
| 585 |
(let ([code* (map Clambda code*)] |
| 586 |
[body (V body)]) |
| 587 |
(make-codes code* body))] |
| 588 |
[else (error 'specify-rep "invalid program" x)])) |
| 589 |
|
| 590 |
(define (specify-representation x) |
| 591 |
(let ([x (Program x)]) |
| 592 |
x)) |
| 593 |
|
| 594 |
(include "pass-specify-rep-primops.ss")) |