| 1 |
;(define integer->char ascii->char) |
| 2 |
;(define char->integer char->ascii) |
| 3 |
(library (rnrs-benchmarks compiler) |
| 4 |
(export main) |
| 5 |
(import (rnrs) |
| 6 |
(rnrs r5rs) |
| 7 |
(rnrs mutable-pairs) |
| 8 |
(rnrs mutable-strings) |
| 9 |
(rnrs-benchmarks)) |
| 10 |
|
| 11 |
(define open-input-file* open-input-file) |
| 12 |
(define (pp-expression expr port) (write expr port) (newline port)) |
| 13 |
(define (write-returning-len obj port) (write obj port) 1) |
| 14 |
(define (display-returning-len obj port) (display obj port) 1) |
| 15 |
(define (write-word w port) |
| 16 |
(write-char (integer->char (quotient w 256)) port) |
| 17 |
(write-char (integer->char (modulo w 256)) port)) |
| 18 |
(define char-nul (integer->char 0)) |
| 19 |
(define char-tab (integer->char 9)) |
| 20 |
(define char-newline (integer->char 10)) |
| 21 |
(define character-encoding char->integer) |
| 22 |
(define max-character-encoding 255) |
| 23 |
(define (fatal-err msg arg) (fatal-error msg arg)) |
| 24 |
(define (scheme-global-var name) name) |
| 25 |
(define (scheme-global-var-ref var) (scheme-global-eval var)) |
| 26 |
(define (scheme-global-var-set! var val) |
| 27 |
(scheme-global-eval (list 'set! var (list 'quote val)) fatal-err)) |
| 28 |
(define (scheme-global-eval expr err) |
| 29 |
;(eval expr) |
| 30 |
(fatal-error "scheme-global-eval is no more")) |
| 31 |
(define (pinpoint-error filename line char) #t) |
| 32 |
(define file-path-sep #\:) |
| 33 |
(define file-ext-sep #\.) |
| 34 |
(define (path-absolute? x) |
| 35 |
(and (> (string-length x) 0) |
| 36 |
(let ((c (string-ref x 0))) (or (char=? c #\/) (char=? c #\~))))) |
| 37 |
(define (file-path x) |
| 38 |
(let loop1 ((i (string-length x))) |
| 39 |
(if (and (> i 0) (not (char=? (string-ref x (- i 1)) file-path-sep))) |
| 40 |
(loop1 (- i 1)) |
| 41 |
(let ((result (make-string i))) |
| 42 |
(let loop2 ((j (- i 1))) |
| 43 |
(if (< j 0) |
| 44 |
result |
| 45 |
(begin |
| 46 |
(string-set! result j (string-ref x j)) |
| 47 |
(loop2 (- j 1))))))))) |
| 48 |
(define (file-name x) |
| 49 |
(let loop1 ((i (string-length x))) |
| 50 |
(if (and (> i 0) (not (char=? (string-ref x (- i 1)) file-path-sep))) |
| 51 |
(loop1 (- i 1)) |
| 52 |
(let ((result (make-string (- (string-length x) i)))) |
| 53 |
(let loop2 ((j (- (string-length x) 1))) |
| 54 |
(if (< j i) |
| 55 |
result |
| 56 |
(begin |
| 57 |
(string-set! result (- j i) (string-ref x j)) |
| 58 |
(loop2 (- j 1))))))))) |
| 59 |
(define (file-ext x) |
| 60 |
(let loop1 ((i (string-length x))) |
| 61 |
(if (or (= i 0) (char=? (string-ref x (- i 1)) file-path-sep)) |
| 62 |
#f |
| 63 |
(if (not (char=? (string-ref x (- i 1)) file-ext-sep)) |
| 64 |
(loop1 (- i 1)) |
| 65 |
(let ((result (make-string (- (string-length x) i)))) |
| 66 |
(let loop2 ((j (- (string-length x) 1))) |
| 67 |
(if (< j i) |
| 68 |
result |
| 69 |
(begin |
| 70 |
(string-set! result (- j i) (string-ref x j)) |
| 71 |
(loop2 (- j 1)))))))))) |
| 72 |
(define (file-root x) |
| 73 |
(let loop1 ((i (string-length x))) |
| 74 |
(if (or (= i 0) (char=? (string-ref x (- i 1)) file-path-sep)) |
| 75 |
x |
| 76 |
(if (not (char=? (string-ref x (- i 1)) file-ext-sep)) |
| 77 |
(loop1 (- i 1)) |
| 78 |
(let ((result (make-string (- i 1)))) |
| 79 |
(let loop2 ((j (- i 2))) |
| 80 |
(if (< j 0) |
| 81 |
result |
| 82 |
(begin |
| 83 |
(string-set! result j (string-ref x j)) |
| 84 |
(loop2 (- j 1)))))))))) |
| 85 |
(define (make-counter next limit limit-error) |
| 86 |
(lambda () |
| 87 |
(if (< next limit) |
| 88 |
(let ((result next)) (set! next (+ next 1)) result) |
| 89 |
(limit-error)))) |
| 90 |
(define (pos-in-list x l) |
| 91 |
(let loop ((l l) (i 0)) |
| 92 |
(cond ((not (pair? l)) #f) |
| 93 |
((eq? (car l) x) i) |
| 94 |
(else (loop (cdr l) (+ i 1)))))) |
| 95 |
(define (string-pos-in-list x l) |
| 96 |
(let loop ((l l) (i 0)) |
| 97 |
(cond ((not (pair? l)) #f) |
| 98 |
((string=? (car l) x) i) |
| 99 |
(else (loop (cdr l) (+ i 1)))))) |
| 100 |
(define (nth-after l n) |
| 101 |
(let loop ((l l) (n n)) (if (> n 0) (loop (cdr l) (- n 1)) l))) |
| 102 |
(define (pair-up l1 l2) |
| 103 |
(define (pair l1 l2) |
| 104 |
(if (pair? l1) |
| 105 |
(cons (cons (car l1) (car l2)) (pair (cdr l1) (cdr l2))) |
| 106 |
'())) |
| 107 |
(pair l1 l2)) |
| 108 |
(define (my-last-pair l) |
| 109 |
(let loop ((l l)) (if (pair? (cdr l)) (loop (cdr l)) l))) |
| 110 |
(define (sort-list l <?) |
| 111 |
(define (mergesort l) |
| 112 |
(define (merge l1 l2) |
| 113 |
(cond ((null? l1) l2) |
| 114 |
((null? l2) l1) |
| 115 |
(else |
| 116 |
(let ((e1 (car l1)) (e2 (car l2))) |
| 117 |
(if (<? e1 e2) |
| 118 |
(cons e1 (merge (cdr l1) l2)) |
| 119 |
(cons e2 (merge l1 (cdr l2)))))))) |
| 120 |
(define (split l) |
| 121 |
(if (or (null? l) (null? (cdr l))) l (cons (car l) (split (cddr l))))) |
| 122 |
(if (or (null? l) (null? (cdr l))) |
| 123 |
l |
| 124 |
(let* ((l1 (mergesort (split l))) (l2 (mergesort (split (cdr l))))) |
| 125 |
(merge l1 l2)))) |
| 126 |
(mergesort l)) |
| 127 |
(define (lst->vector l) |
| 128 |
(let* ((n (length l)) (v (make-vector n))) |
| 129 |
(let loop ((l l) (i 0)) |
| 130 |
(if (pair? l) |
| 131 |
(begin (vector-set! v i (car l)) (loop (cdr l) (+ i 1))) |
| 132 |
v)))) |
| 133 |
(define (vector->lst v) |
| 134 |
(let loop ((l '()) (i (- (vector-length v) 1))) |
| 135 |
(if (< i 0) l (loop (cons (vector-ref v i) l) (- i 1))))) |
| 136 |
(define (lst->string l) |
| 137 |
(let* ((n (length l)) (s (make-string n))) |
| 138 |
(let loop ((l l) (i 0)) |
| 139 |
(if (pair? l) |
| 140 |
(begin (string-set! s i (car l)) (loop (cdr l) (+ i 1))) |
| 141 |
s)))) |
| 142 |
(define (string->lst s) |
| 143 |
(let loop ((l '()) (i (- (string-length s) 1))) |
| 144 |
(if (< i 0) l (loop (cons (string-ref s i) l) (- i 1))))) |
| 145 |
(define (with-exception-handling proc) |
| 146 |
(let ((old-exception-handler throw-to-exception-handler)) |
| 147 |
(let ((val (call-with-current-continuation |
| 148 |
(lambda (cont) |
| 149 |
(set! throw-to-exception-handler cont) |
| 150 |
(proc))))) |
| 151 |
(set! throw-to-exception-handler old-exception-handler) |
| 152 |
val))) |
| 153 |
(define (throw-to-exception-handler val) |
| 154 |
(fatal-err "Internal error, no exception handler at this point" val)) |
| 155 |
(define (compiler-error msg . args) |
| 156 |
(newline) |
| 157 |
(display "*** ERROR -- ") |
| 158 |
(display msg) |
| 159 |
(for-each (lambda (x) (display " ") (write x)) args) |
| 160 |
(newline) |
| 161 |
(compiler-abort)) |
| 162 |
(define (compiler-user-error loc msg . args) |
| 163 |
(newline) |
| 164 |
(display "*** ERROR -- In ") |
| 165 |
(locat-show loc) |
| 166 |
(newline) |
| 167 |
(display "*** ") |
| 168 |
(display msg) |
| 169 |
(for-each (lambda (x) (display " ") (write x)) args) |
| 170 |
(newline) |
| 171 |
(compiler-abort)) |
| 172 |
(define (compiler-internal-error msg . args) |
| 173 |
(newline) |
| 174 |
(display "*** ERROR -- Compiler internal error detected") |
| 175 |
(newline) |
| 176 |
(display "*** in procedure ") |
| 177 |
(display msg) |
| 178 |
(for-each (lambda (x) (display " ") (write x)) args) |
| 179 |
(newline) |
| 180 |
(compiler-abort)) |
| 181 |
(define (compiler-limitation-error msg . args) |
| 182 |
(newline) |
| 183 |
(display "*** ERROR -- Compiler limit reached") |
| 184 |
(newline) |
| 185 |
(display "*** ") |
| 186 |
(display msg) |
| 187 |
(for-each (lambda (x) (display " ") (write x)) args) |
| 188 |
(newline) |
| 189 |
(compiler-abort)) |
| 190 |
(define (compiler-abort) (throw-to-exception-handler #f)) |
| 191 |
(define (make-gnode label edges) (vector label edges)) |
| 192 |
(define (gnode-label x) (vector-ref x 0)) |
| 193 |
(define (gnode-edges x) (vector-ref x 1)) |
| 194 |
(define (transitive-closure graph) |
| 195 |
(define changed? #f) |
| 196 |
(define (closure edges) |
| 197 |
(list->set |
| 198 |
(set-union |
| 199 |
edges |
| 200 |
(apply set-union |
| 201 |
(map (lambda (label) (gnode-edges (gnode-find label graph))) |
| 202 |
(set->list edges)))))) |
| 203 |
(let ((new-graph |
| 204 |
(set-map (lambda (x) |
| 205 |
(let ((new-edges (closure (gnode-edges x)))) |
| 206 |
(if (not (set-equal? new-edges (gnode-edges x))) |
| 207 |
(set! changed? #t)) |
| 208 |
(make-gnode (gnode-label x) new-edges))) |
| 209 |
graph))) |
| 210 |
(if changed? (transitive-closure new-graph) new-graph))) |
| 211 |
(define (gnode-find label graph) |
| 212 |
(define (find label l) |
| 213 |
(cond ((null? l) #f) |
| 214 |
((eq? (gnode-label (car l)) label) (car l)) |
| 215 |
(else (find label (cdr l))))) |
| 216 |
(find label (set->list graph))) |
| 217 |
(define (topological-sort graph) |
| 218 |
(if (set-empty? graph) |
| 219 |
'() |
| 220 |
(let ((to-remove (or (remove-no-edges graph) (remove-cycle graph)))) |
| 221 |
(let ((labels (set-map gnode-label to-remove))) |
| 222 |
(cons labels |
| 223 |
(topological-sort |
| 224 |
(set-map (lambda (x) |
| 225 |
(make-gnode |
| 226 |
(gnode-label x) |
| 227 |
(set-difference (gnode-edges x) labels))) |
| 228 |
(set-difference graph to-remove)))))))) |
| 229 |
(define (remove-no-edges graph) |
| 230 |
(let ((nodes-with-no-edges |
| 231 |
(set-keep (lambda (x) (set-empty? (gnode-edges x))) graph))) |
| 232 |
(if (set-empty? nodes-with-no-edges) #f nodes-with-no-edges))) |
| 233 |
(define (remove-cycle graph) |
| 234 |
(define (remove l) |
| 235 |
(let ((edges (gnode-edges (car l)))) |
| 236 |
(define (equal-edges? x) (set-equal? (gnode-edges x) edges)) |
| 237 |
(define (member-edges? x) (set-member? (gnode-label x) edges)) |
| 238 |
(if (set-member? (gnode-label (car l)) edges) |
| 239 |
(let ((edge-graph (set-keep member-edges? graph))) |
| 240 |
(if (set-every? equal-edges? edge-graph) |
| 241 |
edge-graph |
| 242 |
(remove (cdr l)))) |
| 243 |
(remove (cdr l))))) |
| 244 |
(remove (set->list graph))) |
| 245 |
(define (list->set list) list) |
| 246 |
(define (set->list set) set) |
| 247 |
(define (set-empty) '()) |
| 248 |
(define (set-empty? set) (null? set)) |
| 249 |
(define (set-member? x set) (memq x set)) |
| 250 |
(define (set-singleton x) (list x)) |
| 251 |
(define (set-adjoin set x) (if (memq x set) set (cons x set))) |
| 252 |
(define (set-remove set x) |
| 253 |
(cond ((null? set) '()) |
| 254 |
((eq? (car set) x) (cdr set)) |
| 255 |
(else (cons (car set) (set-remove (cdr set) x))))) |
| 256 |
(define (set-equal? s1 s2) |
| 257 |
(cond ((null? s1) (null? s2)) |
| 258 |
((memq (car s1) s2) (set-equal? (cdr s1) (set-remove s2 (car s1)))) |
| 259 |
(else #f))) |
| 260 |
(define (set-difference set . other-sets) |
| 261 |
(define (difference s1 s2) |
| 262 |
(cond ((null? s1) '()) |
| 263 |
((memq (car s1) s2) (difference (cdr s1) s2)) |
| 264 |
(else (cons (car s1) (difference (cdr s1) s2))))) |
| 265 |
(n-ary difference set other-sets)) |
| 266 |
(define (set-union . sets) |
| 267 |
(define (union s1 s2) |
| 268 |
(cond ((null? s1) s2) |
| 269 |
((memq (car s1) s2) (union (cdr s1) s2)) |
| 270 |
(else (cons (car s1) (union (cdr s1) s2))))) |
| 271 |
(n-ary union '() sets)) |
| 272 |
(define (set-intersection set . other-sets) |
| 273 |
(define (intersection s1 s2) |
| 274 |
(cond ((null? s1) '()) |
| 275 |
((memq (car s1) s2) (cons (car s1) (intersection (cdr s1) s2))) |
| 276 |
(else (intersection (cdr s1) s2)))) |
| 277 |
(n-ary intersection set other-sets)) |
| 278 |
(define (n-ary function first rest) |
| 279 |
(if (null? rest) |
| 280 |
first |
| 281 |
(n-ary function (function first (car rest)) (cdr rest)))) |
| 282 |
(define (set-keep keep? set) |
| 283 |
(cond ((null? set) '()) |
| 284 |
((keep? (car set)) (cons (car set) (set-keep keep? (cdr set)))) |
| 285 |
(else (set-keep keep? (cdr set))))) |
| 286 |
(define (set-every? pred? set) |
| 287 |
(or (null? set) (and (pred? (car set)) (set-every? pred? (cdr set))))) |
| 288 |
(define (set-map proc set) |
| 289 |
(if (null? set) '() (cons (proc (car set)) (set-map proc (cdr set))))) |
| 290 |
(define (list->queue list) |
| 291 |
(cons list (if (pair? list) (my-last-pair list) '()))) |
| 292 |
(define (queue->list queue) (car queue)) |
| 293 |
(define (queue-empty) (cons '() '())) |
| 294 |
(define (queue-empty? queue) (null? (car queue))) |
| 295 |
(define (queue-get! queue) |
| 296 |
(if (null? (car queue)) |
| 297 |
(compiler-internal-error "queue-get!, queue is empty") |
| 298 |
(let ((x (caar queue))) |
| 299 |
(set-car! queue (cdar queue)) |
| 300 |
(if (null? (car queue)) (set-cdr! queue '())) |
| 301 |
x))) |
| 302 |
(define (queue-put! queue x) |
| 303 |
(let ((entry (cons x '()))) |
| 304 |
(if (null? (car queue)) |
| 305 |
(set-car! queue entry) |
| 306 |
(set-cdr! (cdr queue) entry)) |
| 307 |
(set-cdr! queue entry) |
| 308 |
x)) |
| 309 |
(define (string->canonical-symbol str) |
| 310 |
(let ((len (string-length str))) |
| 311 |
(let loop ((str str) (s (make-string len)) (i (- len 1))) |
| 312 |
(if (>= i 0) |
| 313 |
(begin |
| 314 |
(string-set! s i (char-downcase (string-ref str i))) |
| 315 |
(loop str s (- i 1))) |
| 316 |
(string->symbol s))))) |
| 317 |
(define quote-sym (string->canonical-symbol "QUOTE")) |
| 318 |
(define quasiquote-sym (string->canonical-symbol "QUASIQUOTE")) |
| 319 |
(define unquote-sym (string->canonical-symbol "UNQUOTE")) |
| 320 |
(define unquote-splicing-sym (string->canonical-symbol "UNQUOTE-SPLICING")) |
| 321 |
(define lambda-sym (string->canonical-symbol "LAMBDA")) |
| 322 |
(define if-sym (string->canonical-symbol "IF")) |
| 323 |
(define set!-sym (string->canonical-symbol "SET!")) |
| 324 |
(define cond-sym (string->canonical-symbol "COND")) |
| 325 |
(define =>-sym (string->canonical-symbol "=>")) |
| 326 |
(define else-sym (string->canonical-symbol "ELSE")) |
| 327 |
(define and-sym (string->canonical-symbol "AND")) |
| 328 |
(define or-sym (string->canonical-symbol "OR")) |
| 329 |
(define case-sym (string->canonical-symbol "CASE")) |
| 330 |
(define let-sym (string->canonical-symbol "LET")) |
| 331 |
(define let*-sym (string->canonical-symbol "LET*")) |
| 332 |
(define letrec-sym (string->canonical-symbol "LETREC")) |
| 333 |
(define begin-sym (string->canonical-symbol "BEGIN")) |
| 334 |
(define do-sym (string->canonical-symbol "DO")) |
| 335 |
(define define-sym (string->canonical-symbol "DEFINE")) |
| 336 |
(define delay-sym (string->canonical-symbol "DELAY")) |
| 337 |
(define future-sym (string->canonical-symbol "FUTURE")) |
| 338 |
(define **define-macro-sym (string->canonical-symbol "DEFINE-MACRO")) |
| 339 |
(define **declare-sym (string->canonical-symbol "DECLARE")) |
| 340 |
(define **include-sym (string->canonical-symbol "INCLUDE")) |
| 341 |
(define not-sym (string->canonical-symbol "NOT")) |
| 342 |
(define **c-declaration-sym (string->canonical-symbol "C-DECLARATION")) |
| 343 |
(define **c-init-sym (string->canonical-symbol "C-INIT")) |
| 344 |
(define **c-procedure-sym (string->canonical-symbol "C-PROCEDURE")) |
| 345 |
(define void-sym (string->canonical-symbol "VOID")) |
| 346 |
(define char-sym (string->canonical-symbol "CHAR")) |
| 347 |
(define signed-char-sym (string->canonical-symbol "SIGNED-CHAR")) |
| 348 |
(define unsigned-char-sym (string->canonical-symbol "UNSIGNED-CHAR")) |
| 349 |
(define short-sym (string->canonical-symbol "SHORT")) |
| 350 |
(define unsigned-short-sym (string->canonical-symbol "UNSIGNED-SHORT")) |
| 351 |
(define int-sym (string->canonical-symbol "INT")) |
| 352 |
(define unsigned-int-sym (string->canonical-symbol "UNSIGNED-INT")) |
| 353 |
(define long-sym (string->canonical-symbol "LONG")) |
| 354 |
(define unsigned-long-sym (string->canonical-symbol "UNSIGNED-LONG")) |
| 355 |
(define float-sym (string->canonical-symbol "FLOAT")) |
| 356 |
(define double-sym (string->canonical-symbol "DOUBLE")) |
| 357 |
(define pointer-sym (string->canonical-symbol "POINTER")) |
| 358 |
(define boolean-sym (string->canonical-symbol "BOOLEAN")) |
| 359 |
(define string-sym (string->canonical-symbol "STRING")) |
| 360 |
(define scheme-object-sym (string->canonical-symbol "SCHEME-OBJECT")) |
| 361 |
(define c-id-prefix "___") |
| 362 |
(define false-object (if (eq? '() #f) (string->symbol "#f") #f)) |
| 363 |
(define (false-object? obj) (eq? obj false-object)) |
| 364 |
(define undef-object (string->symbol "#[undefined]")) |
| 365 |
(define (undef-object? obj) (eq? obj undef-object)) |
| 366 |
(define (symbol-object? obj) |
| 367 |
(and (not (false-object? obj)) (not (undef-object? obj)) (symbol? obj))) |
| 368 |
(define scm-file-exts '("scm" #f)) |
| 369 |
(define compiler-version "2.2.2") |
| 370 |
(define (open-sf filename) |
| 371 |
(define (open-err) (compiler-error "Can't find file" filename)) |
| 372 |
(if (not (file-ext filename)) |
| 373 |
(let loop ((exts scm-file-exts)) |
| 374 |
(if (pair? exts) |
| 375 |
(let* ((ext (car exts)) |
| 376 |
(full-name |
| 377 |
(if ext (string-append filename "." ext) filename)) |
| 378 |
(port (open-input-file* full-name))) |
| 379 |
(if port (vector port full-name 0 1 0) (loop (cdr exts)))) |
| 380 |
(open-err))) |
| 381 |
(let ((port (open-input-file* filename))) |
| 382 |
(if port (vector port filename 0 1 0) (open-err))))) |
| 383 |
(define (close-sf sf) (close-input-port (vector-ref sf 0))) |
| 384 |
(define (sf-read-char sf) |
| 385 |
(let ((c (read-char (vector-ref sf 0)))) |
| 386 |
(cond ((eof-object? c)) |
| 387 |
((char=? c char-newline) |
| 388 |
(vector-set! sf 3 (+ (vector-ref sf 3) 1)) |
| 389 |
(vector-set! sf 4 0)) |
| 390 |
(else (vector-set! sf 4 (+ (vector-ref sf 4) 1)))) |
| 391 |
c)) |
| 392 |
(define (sf-peek-char sf) (peek-char (vector-ref sf 0))) |
| 393 |
(define (sf-read-error sf msg . args) |
| 394 |
(apply compiler-user-error |
| 395 |
(cons (sf->locat sf) |
| 396 |
(cons (string-append "Read error -- " msg) args)))) |
| 397 |
(define (sf->locat sf) |
| 398 |
(vector 'file |
| 399 |
(vector-ref sf 1) |
| 400 |
(vector-ref sf 2) |
| 401 |
(vector-ref sf 3) |
| 402 |
(vector-ref sf 4))) |
| 403 |
(define (expr->locat expr source) (vector 'expr expr source)) |
| 404 |
(define (locat-show loc) |
| 405 |
(if loc |
| 406 |
(case (vector-ref loc 0) |
| 407 |
((file) |
| 408 |
(if (pinpoint-error |
| 409 |
(vector-ref loc 1) |
| 410 |
(vector-ref loc 3) |
| 411 |
(vector-ref loc 4)) |
| 412 |
(begin |
| 413 |
(display "file \"") |
| 414 |
(display (vector-ref loc 1)) |
| 415 |
(display "\", line ") |
| 416 |
(display (vector-ref loc 3)) |
| 417 |
(display ", character ") |
| 418 |
(display (vector-ref loc 4))))) |
| 419 |
((expr) |
| 420 |
(display "expression ") |
| 421 |
(write (vector-ref loc 1)) |
| 422 |
(if (vector-ref loc 2) |
| 423 |
(begin |
| 424 |
(display " ") |
| 425 |
(locat-show (source-locat (vector-ref loc 2)))))) |
| 426 |
(else (compiler-internal-error "locat-show, unknown location tag"))) |
| 427 |
(display "unknown location"))) |
| 428 |
(define (locat-filename loc) |
| 429 |
(if loc |
| 430 |
(case (vector-ref loc 0) |
| 431 |
((file) (vector-ref loc 1)) |
| 432 |
((expr) |
| 433 |
(let ((source (vector-ref loc 2))) |
| 434 |
(if source (locat-filename (source-locat source)) ""))) |
| 435 |
(else |
| 436 |
(compiler-internal-error "locat-filename, unknown location tag"))) |
| 437 |
"")) |
| 438 |
(define (make-source code locat) (vector code locat)) |
| 439 |
(define (source-code x) (vector-ref x 0)) |
| 440 |
(define (source-code-set! x y) (vector-set! x 0 y) x) |
| 441 |
(define (source-locat x) (vector-ref x 1)) |
| 442 |
(define (expression->source expr source) |
| 443 |
(define (expr->source x) |
| 444 |
(make-source |
| 445 |
(cond ((pair? x) (list->source x)) |
| 446 |
((vector? x) (vector->source x)) |
| 447 |
((symbol-object? x) (string->canonical-symbol (symbol->string x))) |
| 448 |
(else x)) |
| 449 |
(expr->locat x source))) |
| 450 |
(define (list->source l) |
| 451 |
(cond ((pair? l) (cons (expr->source (car l)) (list->source (cdr l)))) |
| 452 |
((null? l) '()) |
| 453 |
(else (expr->source l)))) |
| 454 |
(define (vector->source v) |
| 455 |
(let* ((len (vector-length v)) (x (make-vector len))) |
| 456 |
(let loop ((i (- len 1))) |
| 457 |
(if (>= i 0) |
| 458 |
(begin |
| 459 |
(vector-set! x i (expr->source (vector-ref v i))) |
| 460 |
(loop (- i 1))))) |
| 461 |
x)) |
| 462 |
(expr->source expr)) |
| 463 |
(define (source->expression source) |
| 464 |
(define (list->expression l) |
| 465 |
(cond ((pair? l) |
| 466 |
(cons (source->expression (car l)) (list->expression (cdr l)))) |
| 467 |
((null? l) '()) |
| 468 |
(else (source->expression l)))) |
| 469 |
(define (vector->expression v) |
| 470 |
(let* ((len (vector-length v)) (x (make-vector len))) |
| 471 |
(let loop ((i (- len 1))) |
| 472 |
(if (>= i 0) |
| 473 |
(begin |
| 474 |
(vector-set! x i (source->expression (vector-ref v i))) |
| 475 |
(loop (- i 1))))) |
| 476 |
x)) |
| 477 |
(let ((code (source-code source))) |
| 478 |
(cond ((pair? code) (list->expression code)) |
| 479 |
((vector? code) (vector->expression code)) |
| 480 |
(else code)))) |
| 481 |
(define (file->sources filename info-port) |
| 482 |
(if info-port |
| 483 |
(begin |
| 484 |
(display "(reading \"" info-port) |
| 485 |
(display filename info-port) |
| 486 |
(display "\"" info-port))) |
| 487 |
(let ((sf (open-sf filename))) |
| 488 |
(define (read-sources) |
| 489 |
(let ((source (read-source sf))) |
| 490 |
(if (not (eof-object? source)) |
| 491 |
(begin |
| 492 |
(if info-port (display "." info-port)) |
| 493 |
(cons source (read-sources))) |
| 494 |
'()))) |
| 495 |
(let ((sources (read-sources))) |
| 496 |
(if info-port (display ")" info-port)) |
| 497 |
(close-sf sf) |
| 498 |
sources))) |
| 499 |
(define (file->sources* filename info-port loc) |
| 500 |
(file->sources |
| 501 |
(if (path-absolute? filename) |
| 502 |
filename |
| 503 |
(string-append (file-path (locat-filename loc)) filename)) |
| 504 |
info-port)) |
| 505 |
(define (read-source sf) |
| 506 |
(define (read-char*) |
| 507 |
(let ((c (sf-read-char sf))) |
| 508 |
(if (eof-object? c) |
| 509 |
(sf-read-error sf "Premature end of file encountered") |
| 510 |
c))) |
| 511 |
(define (read-non-whitespace-char) |
| 512 |
(let ((c (read-char*))) |
| 513 |
(cond ((< 0 (vector-ref read-table (char->integer c))) |
| 514 |
(read-non-whitespace-char)) |
| 515 |
((char=? c #\;) |
| 516 |
(let loop () |
| 517 |
(if (not (char=? (read-char*) char-newline)) |
| 518 |
(loop) |
| 519 |
(read-non-whitespace-char)))) |
| 520 |
(else c)))) |
| 521 |
(define (delimiter? c) |
| 522 |
(or (eof-object? c) (not (= (vector-ref read-table (char->integer c)) 0)))) |
| 523 |
(define (read-list first) |
| 524 |
(let ((result (cons first '()))) |
| 525 |
(let loop ((end result)) |
| 526 |
(let ((c (read-non-whitespace-char))) |
| 527 |
(cond ((char=? c #\))) |
| 528 |
((and (char=? c #\.) (delimiter? (sf-peek-char sf))) |
| 529 |
(let ((x (read-source sf))) |
| 530 |
(if (char=? (read-non-whitespace-char) #\)) |
| 531 |
(set-cdr! end x) |
| 532 |
(sf-read-error sf "')' expected")))) |
| 533 |
(else |
| 534 |
(let ((tail (cons (rd* c) '()))) |
| 535 |
(set-cdr! end tail) |
| 536 |
(loop tail)))))) |
| 537 |
result)) |
| 538 |
(define (read-vector) |
| 539 |
(define (loop i) |
| 540 |
(let ((c (read-non-whitespace-char))) |
| 541 |
(if (char=? c #\)) |
| 542 |
(make-vector i '()) |
| 543 |
(let* ((x (rd* c)) (v (loop (+ i 1)))) (vector-set! v i x) v)))) |
| 544 |
(loop 0)) |
| 545 |
(define (read-string) |
| 546 |
(define (loop i) |
| 547 |
(let ((c (read-char*))) |
| 548 |
(cond ((char=? c #\") (make-string i #\space)) |
| 549 |
((char=? c #\\) |
| 550 |
(let* ((c (read-char*)) (s (loop (+ i 1)))) |
| 551 |
(string-set! s i c) |
| 552 |
s)) |
| 553 |
(else (let ((s (loop (+ i 1)))) (string-set! s i c) s))))) |
| 554 |
(loop 0)) |
| 555 |
(define (read-symbol/number-string i) |
| 556 |
(if (delimiter? (sf-peek-char sf)) |
| 557 |
(make-string i #\space) |
| 558 |
(let* ((c (sf-read-char sf)) (s (read-symbol/number-string (+ i 1)))) |
| 559 |
(string-set! s i (char-downcase c)) |
| 560 |
s))) |
| 561 |
(define (read-symbol/number c) |
| 562 |
(let ((s (read-symbol/number-string 1))) |
| 563 |
(string-set! s 0 (char-downcase c)) |
| 564 |
(or (string->number s 10) (string->canonical-symbol s)))) |
| 565 |
(define (read-prefixed-number c) |
| 566 |
(let ((s (read-symbol/number-string 2))) |
| 567 |
(string-set! s 0 #\#) |
| 568 |
(string-set! s 1 c) |
| 569 |
(string->number s 10))) |
| 570 |
(define (read-special-symbol) |
| 571 |
(let ((s (read-symbol/number-string 2))) |
| 572 |
(string-set! s 0 #\#) |
| 573 |
(string-set! s 1 #\#) |
| 574 |
(string->canonical-symbol s))) |
| 575 |
(define (rd c) |
| 576 |
(cond ((eof-object? c) c) |
| 577 |
((< 0 (vector-ref read-table (char->integer c))) |
| 578 |
(rd (sf-read-char sf))) |
| 579 |
((char=? c #\;) |
| 580 |
(let loop () |
| 581 |
(let ((c (sf-read-char sf))) |
| 582 |
(cond ((eof-object? c) c) |
| 583 |
((char=? c char-newline) (rd (sf-read-char sf))) |
| 584 |
(else (loop)))))) |
| 585 |
(else (rd* c)))) |
| 586 |
(define (rd* c) |
| 587 |
(let ((source (make-source #f (sf->locat sf)))) |
| 588 |
(source-code-set! |
| 589 |
source |
| 590 |
(cond ((char=? c #\() |
| 591 |
(let ((x (read-non-whitespace-char))) |
| 592 |
(if (char=? x #\)) '() (read-list (rd* x))))) |
| 593 |
((char=? c #\#) |
| 594 |
(let ((c (char-downcase (sf-read-char sf)))) |
| 595 |
(cond ((char=? c #\() (read-vector)) |
| 596 |
((char=? c #\f) false-object) |
| 597 |
((char=? c #\t) #t) |
| 598 |
((char=? c #\\) |
| 599 |
(let ((c (read-char*))) |
| 600 |
(if (or (not (char-alphabetic? c)) |
| 601 |
(delimiter? (sf-peek-char sf))) |
| 602 |
c |
| 603 |
(let ((name (read-symbol/number c))) |
| 604 |
(let ((x (assq name named-char-table))) |
| 605 |
(if x |
| 606 |
(cdr x) |
| 607 |
(sf-read-error |
| 608 |
sf |
| 609 |
"Unknown character name" |
| 610 |
name))))))) |
| 611 |
((char=? c #\#) (read-special-symbol)) |
| 612 |
(else |
| 613 |
(let ((num (read-prefixed-number c))) |
| 614 |
(or num |
| 615 |
(sf-read-error |
| 616 |
sf |
| 617 |
"Unknown '#' read macro" |
| 618 |
c))))))) |
| 619 |
((char=? c #\") (read-string)) |
| 620 |
((char=? c #\') |
| 621 |
(list (make-source quote-sym (sf->locat sf)) (read-source sf))) |
| 622 |
((char=? c #\`) |
| 623 |
(list (make-source quasiquote-sym (sf->locat sf)) |
| 624 |
(read-source sf))) |
| 625 |
((char=? c #\,) |
| 626 |
(if (char=? (sf-peek-char sf) #\@) |
| 627 |
(let ((x (make-source unquote-splicing-sym (sf->locat sf)))) |
| 628 |
(sf-read-char sf) |
| 629 |
(list x (read-source sf))) |
| 630 |
(list (make-source unquote-sym (sf->locat sf)) |
| 631 |
(read-source sf)))) |
| 632 |
((char=? c #\)) (sf-read-error sf "Misplaced ')'")) |
| 633 |
((or (char=? c #\[) (char=? c #\]) (char=? c #\{) (char=? c #\})) |
| 634 |
(sf-read-error sf "Illegal character" c)) |
| 635 |
(else |
| 636 |
(if (char=? c #\.) |
| 637 |
(if (delimiter? (sf-peek-char sf)) |
| 638 |
(sf-read-error sf "Misplaced '.'"))) |
| 639 |
(read-symbol/number c)))))) |
| 640 |
(rd (sf-read-char sf))) |
| 641 |
(define named-char-table |
| 642 |
(list (cons (string->canonical-symbol "NUL") char-nul) |
| 643 |
(cons (string->canonical-symbol "TAB") char-tab) |
| 644 |
(cons (string->canonical-symbol "NEWLINE") char-newline) |
| 645 |
(cons (string->canonical-symbol "SPACE") #\space))) |
| 646 |
(define read-table |
| 647 |
(let ((rt (make-vector (+ max-character-encoding 1) 0))) |
| 648 |
(vector-set! rt (char->integer char-tab) 1) |
| 649 |
(vector-set! rt (char->integer char-newline) 1) |
| 650 |
(vector-set! rt (char->integer #\space) 1) |
| 651 |
(vector-set! rt (char->integer #\;) -1) |
| 652 |
(vector-set! rt (char->integer #\() -1) |
| 653 |
(vector-set! rt (char->integer #\)) -1) |
| 654 |
(vector-set! rt (char->integer #\") -1) |
| 655 |
(vector-set! rt (char->integer #\') -1) |
| 656 |
(vector-set! rt (char->integer #\`) -1) |
| 657 |
rt)) |
| 658 |
(define (make-var name bound refs sets source) |
| 659 |
(vector var-tag name bound refs sets source #f)) |
| 660 |
(define (var? x) |
| 661 |
(and (vector? x) (> (vector-length x) 0) (eq? (vector-ref x 0) var-tag))) |
| 662 |
(define (var-name x) (vector-ref x 1)) |
| 663 |
(define (var-bound x) (vector-ref x 2)) |
| 664 |
(define (var-refs x) (vector-ref x 3)) |
| 665 |
(define (var-sets x) (vector-ref x 4)) |
| 666 |
(define (var-source x) (vector-ref x 5)) |
| 667 |
(define (var-info x) (vector-ref x 6)) |
| 668 |
(define (var-name-set! x y) (vector-set! x 1 y)) |
| 669 |
(define (var-bound-set! x y) (vector-set! x 2 y)) |
| 670 |
(define (var-refs-set! x y) (vector-set! x 3 y)) |
| 671 |
(define (var-sets-set! x y) (vector-set! x 4 y)) |
| 672 |
(define (var-source-set! x y) (vector-set! x 5 y)) |
| 673 |
(define (var-info-set! x y) (vector-set! x 6 y)) |
| 674 |
(define var-tag (list 'var-tag)) |
| 675 |
(define (var-copy var) |
| 676 |
(make-var (var-name var) #t (set-empty) (set-empty) (var-source var))) |
| 677 |
(define (make-temp-var name) (make-var name #t (set-empty) (set-empty) #f)) |
| 678 |
(define (temp-var? var) (eq? (var-bound var) #t)) |
| 679 |
(define ret-var (make-temp-var 'ret)) |
| 680 |
(define ret-var-set (set-singleton ret-var)) |
| 681 |
(define closure-env-var (make-temp-var 'closure-env)) |
| 682 |
(define empty-var (make-temp-var #f)) |
| 683 |
(define make-global-environment (lambda () (env-frame #f '()))) |
| 684 |
(define (env-frame env vars) (vector (cons vars #f) '() '() env)) |
| 685 |
(define (env-new-var! env name source) |
| 686 |
(let* ((glob (not (env-parent-ref env))) |
| 687 |
(var (make-var name (not glob) (set-empty) (set-empty) source))) |
| 688 |
(env-vars-set! env (cons var (env-vars-ref env))) |
| 689 |
var)) |
| 690 |
(define (env-macro env name def) |
| 691 |
(let ((name* (if (full-name? name) |
| 692 |
name |
| 693 |
(let ((prefix (env-namespace-prefix env name))) |
| 694 |
(if prefix (make-full-name prefix name) name))))) |
| 695 |
(vector (vector-ref env 0) |
| 696 |
(cons (cons name* def) (env-macros-ref env)) |
| 697 |
(env-decls-ref env) |
| 698 |
(env-parent-ref env)))) |
| 699 |
(define (env-declare env decl) |
| 700 |
(vector (vector-ref env 0) |
| 701 |
(env-macros-ref env) |
| 702 |
(cons decl (env-decls-ref env)) |
| 703 |
(env-parent-ref env))) |
| 704 |
(define (env-vars-ref env) (car (vector-ref env 0))) |
| 705 |
(define (env-vars-set! env vars) (set-car! (vector-ref env 0) vars)) |
| 706 |
(define (env-macros-ref env) (vector-ref env 1)) |
| 707 |
(define (env-decls-ref env) (vector-ref env 2)) |
| 708 |
(define (env-parent-ref env) (vector-ref env 3)) |
| 709 |
(define (env-namespace-prefix env name) |
| 710 |
(let loop ((decls (env-decls-ref env))) |
| 711 |
(if (pair? decls) |
| 712 |
(let ((decl (car decls))) |
| 713 |
(if (eq? (car decl) namespace-sym) |
| 714 |
(let ((syms (cddr decl))) |
| 715 |
(if (or (null? syms) (memq name syms)) |
| 716 |
(cadr decl) |
| 717 |
(loop (cdr decls)))) |
| 718 |
(loop (cdr decls)))) |
| 719 |
#f))) |
| 720 |
(define (env-lookup env name stop-at-first-frame? proc) |
| 721 |
(define (search env name full?) |
| 722 |
(if full? |
| 723 |
(search* env name full?) |
| 724 |
(let ((prefix (env-namespace-prefix env name))) |
| 725 |
(if prefix |
| 726 |
(search* env (make-full-name prefix name) #t) |
| 727 |
(search* env name full?))))) |
| 728 |
(define (search* env name full?) |
| 729 |
(define (search-macros macros) |
| 730 |
(if (pair? macros) |
| 731 |
(let ((m (car macros))) |
| 732 |
(if (eq? (car m) name) |
| 733 |
(proc env name (cdr m)) |
| 734 |
(search-macros (cdr macros)))) |
| 735 |
(search-vars (env-vars-ref env)))) |
| 736 |
(define (search-vars vars) |
| 737 |
(if (pair? vars) |
| 738 |
(let ((v (car vars))) |
| 739 |
(if (eq? (var-name v) name) |
| 740 |
(proc env name v) |
| 741 |
(search-vars (cdr vars)))) |
| 742 |
(let ((env* (env-parent-ref env))) |
| 743 |
(if (or stop-at-first-frame? (not env*)) |
| 744 |
(proc env name #f) |
| 745 |
(search env* name full?))))) |
| 746 |
(search-macros (env-macros-ref env))) |
| 747 |
(search env name (full-name? name))) |
| 748 |
(define (valid-prefix? str) |
| 749 |
(let ((l (string-length str))) |
| 750 |
(or (= l 0) (and (>= l 2) (char=? (string-ref str (- l 1)) #\#))))) |
| 751 |
(define (full-name? sym) |
| 752 |
(let ((str (symbol->string sym))) |
| 753 |
(let loop ((i (- (string-length str) 1))) |
| 754 |
(if (< i 0) #f (if (char=? (string-ref str i) #\#) #t (loop (- i 1))))))) |
| 755 |
(define (make-full-name prefix sym) |
| 756 |
(if (= (string-length prefix) 0) |
| 757 |
sym |
| 758 |
(string->canonical-symbol (string-append prefix (symbol->string sym))))) |
| 759 |
(define (env-lookup-var env name source) |
| 760 |
(env-lookup |
| 761 |
env |
| 762 |
name |
| 763 |
#f |
| 764 |
(lambda (env name x) |
| 765 |
(if x |
| 766 |
(if (var? x) |
| 767 |
x |
| 768 |
(compiler-internal-error |
| 769 |
"env-lookup-var, name is that of a macro" |
| 770 |
name)) |
| 771 |
(env-new-var! env name source))))) |
| 772 |
(define (env-define-var env name source) |
| 773 |
(env-lookup |
| 774 |
env |
| 775 |
name |
| 776 |
#t |
| 777 |
(lambda (env name x) |
| 778 |
(if x |
| 779 |
(if (var? x) |
| 780 |
(pt-syntax-error source "Duplicate definition of a variable") |
| 781 |
(compiler-internal-error |
| 782 |
"env-define-var, name is that of a macro" |
| 783 |
name)) |
| 784 |
(env-new-var! env name source))))) |
| 785 |
(define (env-lookup-global-var env name) |
| 786 |
(let ((env* (env-global-env env))) |
| 787 |
(define (search-vars vars) |
| 788 |
(if (pair? vars) |
| 789 |
(let ((v (car vars))) |
| 790 |
(if (eq? (var-name v) name) v (search-vars (cdr vars)))) |
| 791 |
(env-new-var! env* name #f))) |
| 792 |
(search-vars (env-vars-ref env*)))) |
| 793 |
(define (env-global-variables env) (env-vars-ref (env-global-env env))) |
| 794 |
(define (env-global-env env) |
| 795 |
(let loop ((env env)) |
| 796 |
(let ((env* (env-parent-ref env))) (if env* (loop env*) env)))) |
| 797 |
(define (env-lookup-macro env name) |
| 798 |
(env-lookup |
| 799 |
env |
| 800 |
name |
| 801 |
#f |
| 802 |
(lambda (env name x) (if (or (not x) (var? x)) #f x)))) |
| 803 |
(define (env-declarations env) env) |
| 804 |
(define flag-declarations '()) |
| 805 |
(define parameterized-declarations '()) |
| 806 |
(define boolean-declarations '()) |
| 807 |
(define namable-declarations '()) |
| 808 |
(define namable-boolean-declarations '()) |
| 809 |
(define namable-string-declarations '()) |
| 810 |
(define (define-flag-decl name type) |
| 811 |
(set! flag-declarations (cons (cons name type) flag-declarations)) |
| 812 |
'()) |
| 813 |
(define (define-parameterized-decl name) |
| 814 |
(set! parameterized-declarations (cons name parameterized-declarations)) |
| 815 |
'()) |
| 816 |
(define (define-boolean-decl name) |
| 817 |
(set! boolean-declarations (cons name boolean-declarations)) |
| 818 |
'()) |
| 819 |
(define (define-namable-decl name type) |
| 820 |
(set! namable-declarations (cons (cons name type) namable-declarations)) |
| 821 |
'()) |
| 822 |
(define (define-namable-boolean-decl name) |
| 823 |
(set! namable-boolean-declarations (cons name namable-boolean-declarations)) |
| 824 |
'()) |
| 825 |
(define (define-namable-string-decl name) |
| 826 |
(set! namable-string-declarations (cons name namable-string-declarations)) |
| 827 |
'()) |
| 828 |
(define (flag-decl source type val) (list type val)) |
| 829 |
(define (parameterized-decl source id parm) (list id parm)) |
| 830 |
(define (boolean-decl source id pos) (list id pos)) |
| 831 |
(define (namable-decl source type val names) (cons type (cons val names))) |
| 832 |
(define (namable-boolean-decl source id pos names) (cons id (cons pos names))) |
| 833 |
(define (namable-string-decl source id str names) |
| 834 |
(if (and (eq? id namespace-sym) (not (valid-prefix? str))) |
| 835 |
(pt-syntax-error source "Illegal namespace")) |
| 836 |
(cons id (cons str names))) |
| 837 |
(define (declaration-value name element default decls) |
| 838 |
(if (not decls) |
| 839 |
default |
| 840 |
(let loop ((l (env-decls-ref decls))) |
| 841 |
(if (pair? l) |
| 842 |
(let ((d (car l))) |
| 843 |
(if (and (eq? (car d) name) |
| 844 |
(or (null? (cddr d)) (memq element (cddr d)))) |
| 845 |
(cadr d) |
| 846 |
(loop (cdr l)))) |
| 847 |
(declaration-value name element default (env-parent-ref decls)))))) |
| 848 |
(define namespace-sym |
| 849 |
(let ([s (string->canonical-symbol "NAMESPACE")]) |
| 850 |
(define-namable-string-decl s) |
| 851 |
s)) |
| 852 |
(define (node-parent x) (vector-ref x 1)) |
| 853 |
(define (node-children x) (vector-ref x 2)) |
| 854 |
(define (node-fv x) (vector-ref x 3)) |
| 855 |
(define (node-decl x) (vector-ref x 4)) |
| 856 |
(define (node-source x) (vector-ref x 5)) |
| 857 |
(define (node-parent-set! x y) (vector-set! x 1 y)) |
| 858 |
(define (node-fv-set! x y) (vector-set! x 3 y)) |
| 859 |
(define (node-decl-set! x y) (vector-set! x 4 y)) |
| 860 |
(define (node-source-set! x y) (vector-set! x 5 y)) |
| 861 |
(define (node-children-set! x y) |
| 862 |
(vector-set! x 2 y) |
| 863 |
(for-each (lambda (child) (node-parent-set! child x)) y) |
| 864 |
(node-fv-invalidate! x)) |
| 865 |
(define (node-fv-invalidate! x) |
| 866 |
(let loop ((node x)) |
| 867 |
(if node (begin (node-fv-set! node #t) (loop (node-parent node)))))) |
| 868 |
(define (make-cst parent children fv decl source val) |
| 869 |
(vector cst-tag parent children fv decl source val)) |
| 870 |
(define (cst? x) |
| 871 |
(and (vector? x) (> (vector-length x) 0) (eq? (vector-ref x 0) cst-tag))) |
| 872 |
(define (cst-val x) (vector-ref x 6)) |
| 873 |
(define (cst-val-set! x y) (vector-set! x 6 y)) |
| 874 |
(define cst-tag (list 'cst-tag)) |
| 875 |
(define (make-ref parent children fv decl source var) |
| 876 |
(vector ref-tag parent children fv decl source var)) |
| 877 |
(define (ref? x) |
| 878 |
(and (vector? x) (> (vector-length x) 0) (eq? (vector-ref x 0) ref-tag))) |
| 879 |
(define (ref-var x) (vector-ref x 6)) |
| 880 |
(define (ref-var-set! x y) (vector-set! x 6 y)) |
| 881 |
(define ref-tag (list 'ref-tag)) |
| 882 |
(define (make-set parent children fv decl source var) |
| 883 |
(vector set-tag parent children fv decl source var)) |
| 884 |
(define (set? x) |
| 885 |
(and (vector? x) (> (vector-length x) 0) (eq? (vector-ref x 0) set-tag))) |
| 886 |
(define (set-var x) (vector-ref x 6)) |
| 887 |
(define (set-var-set! x y) (vector-set! x 6 y)) |
| 888 |
(define set-tag (list 'set-tag)) |
| 889 |
(define (make-def parent children fv decl source var) |
| 890 |
(vector def-tag parent children fv decl source var)) |
| 891 |
(define (def? x) |
| 892 |
(and (vector? x) (> (vector-length x) 0) (eq? (vector-ref x 0) def-tag))) |
| 893 |
(define (def-var x) (vector-ref x 6)) |
| 894 |
(define (def-var-set! x y) (vector-set! x 6 y)) |
| 895 |
(define def-tag (list 'def-tag)) |
| 896 |
(define (make-tst parent children fv decl source) |
| 897 |
(vector tst-tag parent children fv decl source)) |
| 898 |
(define (tst? x) |
| 899 |
(and (vector? x) (> (vector-length x) 0) (eq? (vector-ref x 0) tst-tag))) |
| 900 |
(define tst-tag (list 'tst-tag)) |
| 901 |
(define (make-conj parent children fv decl source) |
| 902 |
(vector conj-tag parent children fv decl source)) |
| 903 |
(define (conj? x) |
| 904 |
(and (vector? x) (> (vector-length x) 0) (eq? (vector-ref x 0) conj-tag))) |
| 905 |
(define conj-tag (list 'conj-tag)) |
| 906 |
(define (make-disj parent children fv decl source) |
| 907 |
(vector disj-tag parent children fv decl source)) |
| 908 |
(define (disj? x) |
| 909 |
(and (vector? x) (> (vector-length x) 0) (eq? (vector-ref x 0) disj-tag))) |
| 910 |
(define disj-tag (list 'disj-tag)) |
| 911 |
(define (make-prc parent children fv decl source name min rest parms) |
| 912 |
(vector prc-tag parent children fv decl source name min rest parms)) |
| 913 |
(define (prc? x) |
| 914 |
(and (vector? x) (> (vector-length x) 0) (eq? (vector-ref x 0) prc-tag))) |
| 915 |
(define (prc-name x) (vector-ref x 6)) |
| 916 |
(define (prc-min x) (vector-ref x 7)) |
| 917 |
(define (prc-rest x) (vector-ref x 8)) |
| 918 |
(define (prc-parms x) (vector-ref x 9)) |
| 919 |
(define (prc-name-set! x y) (vector-set! x 6 y)) |
| 920 |
(define (prc-min-set! x y) (vector-set! x 7 y)) |
| 921 |
(define (prc-rest-set! x y) (vector-set! x 8 y)) |
| 922 |
(define (prc-parms-set! x y) (vector-set! x 9 y)) |
| 923 |
(define prc-tag (list 'prc-tag)) |
| 924 |
(define (make-app parent children fv decl source) |
| 925 |
(vector app-tag parent children fv decl source)) |
| 926 |
(define (app? x) |
| 927 |
(and (vector? x) (> (vector-length x) 0) (eq? (vector-ref x 0) app-tag))) |
| 928 |
(define app-tag (list 'app-tag)) |
| 929 |
(define (make-fut parent children fv decl source) |
| 930 |
(vector fut-tag parent children fv decl source)) |
| 931 |
(define (fut? x) |
| 932 |
(and (vector? x) (> (vector-length x) 0) (eq? (vector-ref x 0) fut-tag))) |
| 933 |
(define fut-tag (list 'fut-tag)) |
| 934 |
(define (new-cst source decl val) (make-cst #f '() #t decl source val)) |
| 935 |
(define (new-ref source decl var) |
| 936 |
(let ((node (make-ref #f '() #t decl source var))) |
| 937 |
(var-refs-set! var (set-adjoin (var-refs var) node)) |
| 938 |
node)) |
| 939 |
(define (new-ref-extended-bindings source name env) |
| 940 |
(new-ref source |
| 941 |
(add-extended-bindings (env-declarations env)) |
| 942 |
(env-lookup-global-var env name))) |
| 943 |
(define (new-set source decl var val) |
| 944 |
(let ((node (make-set #f (list val) #t decl source var))) |
| 945 |
(var-sets-set! var (set-adjoin (var-sets var) node)) |
| 946 |
(node-parent-set! val node) |
| 947 |
node)) |
| 948 |
(define (set-val x) |
| 949 |
(if (set? x) |
| 950 |
(car (node-children x)) |
| 951 |
(compiler-internal-error "set-val, 'set' node expected" x))) |
| 952 |
(define (new-def source decl var val) |
| 953 |
(let ((node (make-def #f (list val) #t decl source var))) |
| 954 |
(var-sets-set! var (set-adjoin (var-sets var) node)) |
| 955 |
(node-parent-set! val node) |
| 956 |
node)) |
| 957 |
(define (def-val x) |
| 958 |
(if (def? x) |
| 959 |
(car (node-children x)) |
| 960 |
(compiler-internal-error "def-val, 'def' node expected" x))) |
| 961 |
(define (new-tst source decl pre con alt) |
| 962 |
(let ((node (make-tst #f (list pre con alt) #t decl source))) |
| 963 |
(node-parent-set! pre node) |
| 964 |
(node-parent-set! con node) |
| 965 |
(node-parent-set! alt node) |
| 966 |
node)) |
| 967 |
(define (tst-pre x) |
| 968 |
(if (tst? x) |
| 969 |
(car (node-children x)) |
| 970 |
(compiler-internal-error "tst-pre, 'tst' node expected" x))) |
| 971 |
(define (tst-con x) |
| 972 |
(if (tst? x) |
| 973 |
(cadr (node-children x)) |
| 974 |
(compiler-internal-error "tst-con, 'tst' node expected" x))) |
| 975 |
(define (tst-alt x) |
| 976 |
(if (tst? x) |
| 977 |
(caddr (node-children x)) |
| 978 |
(compiler-internal-error "tst-alt, 'tst' node expected" x))) |
| 979 |
(define (new-conj source decl pre alt) |
| 980 |
(let ((node (make-conj #f (list pre alt) #t decl source))) |
| 981 |
(node-parent-set! pre node) |
| 982 |
(node-parent-set! alt node) |
| 983 |
node)) |
| 984 |
(define (conj-pre x) |
| 985 |
(if (conj? x) |
| 986 |
(car (node-children x)) |
| 987 |
(compiler-internal-error "conj-pre, 'conj' node expected" x))) |
| 988 |
(define (conj-alt x) |
| 989 |
(if (conj? x) |
| 990 |
(cadr (node-children x)) |
| 991 |
(compiler-internal-error "conj-alt, 'conj' node expected" x))) |
| 992 |
(define (new-disj source decl pre alt) |
| 993 |
(let ((node (make-disj #f (list pre alt) #t decl source))) |
| 994 |
(node-parent-set! pre node) |
| 995 |
(node-parent-set! alt node) |
| 996 |
node)) |
| 997 |
(define (disj-pre x) |
| 998 |
(if (disj? x) |
| 999 |
(car (node-children x)) |
| 1000 |
(compiler-internal-error "disj-pre, 'disj' node expected" x))) |
| 1001 |
(define (disj-alt x) |
| 1002 |
(if (disj? x) |
| 1003 |
(cadr (node-children x)) |
| 1004 |
(compiler-internal-error "disj-alt, 'disj' node expected" x))) |
| 1005 |
(define (new-prc source decl name min rest parms body) |
| 1006 |
(let ((node (make-prc #f (list body) #t decl source name min rest parms))) |
| 1007 |
(for-each (lambda (x) (var-bound-set! x node)) parms) |
| 1008 |
(node-parent-set! body node) |
| 1009 |
node)) |
| 1010 |
(define (prc-body x) |
| 1011 |
(if (prc? x) |
| 1012 |
(car (node-children x)) |
| 1013 |
(compiler-internal-error "prc-body, 'proc' node expected" x))) |
| 1014 |
(define (new-call source decl oper args) |
| 1015 |
(let ((node (make-app #f (cons oper args) #t decl source))) |
| 1016 |
(node-parent-set! oper node) |
| 1017 |
(for-each (lambda (x) (node-parent-set! x node)) args) |
| 1018 |
node)) |
| 1019 |
(define (new-call* source decl oper args) |
| 1020 |
(if *ptree-port* |
| 1021 |
(if (ref? oper) |
| 1022 |
(let ((var (ref-var oper))) |
| 1023 |
(if (global? var) |
| 1024 |
(let ((proc (standard-procedure |
| 1025 |
(var-name var) |
| 1026 |
(node-decl oper)))) |
| 1027 |
(if (and proc |
| 1028 |
(not (nb-args-conforms? |
| 1029 |
(length args) |
| 1030 |
(standard-procedure-call-pattern proc)))) |
| 1031 |
(begin |
| 1032 |
(display "*** WARNING -- \"" *ptree-port*) |
| 1033 |
(display (var-name var) *ptree-port*) |
| 1034 |
(display "\" is called with " *ptree-port*) |
| 1035 |
(display (length args) *ptree-port*) |
| 1036 |
(display " argument(s)." *ptree-port*) |
| 1037 |
(newline *ptree-port*)))))))) |
| 1038 |
(new-call source decl oper args)) |
| 1039 |
(define (app-oper x) |
| 1040 |
(if (app? x) |
| 1041 |
(car (node-children x)) |
| 1042 |
(compiler-internal-error "app-oper, 'call' node expected" x))) |
| 1043 |
(define (app-args x) |
| 1044 |
(if (app? x) |
| 1045 |
(cdr (node-children x)) |
| 1046 |
(compiler-internal-error "app-args, 'call' node expected" x))) |
| 1047 |
(define (oper-pos? node) |
| 1048 |
(let ((parent (node-parent node))) |
| 1049 |
(if parent (and (app? parent) (eq? (app-oper parent) node)) #f))) |
| 1050 |
(define (new-fut source decl val) |
| 1051 |
(let ((node (make-fut #f (list val) #t decl source))) |
| 1052 |
(node-parent-set! val node) |
| 1053 |
node)) |
| 1054 |
(define (fut-val x) |
| 1055 |
(if (fut? x) |
| 1056 |
(car (node-children x)) |
| 1057 |
(compiler-internal-error "fut-val, 'fut' node expected" x))) |
| 1058 |
(define (new-disj-call source decl pre oper alt) |
| 1059 |
(new-call* |
| 1060 |
source |
| 1061 |
decl |
| 1062 |
(let* ((parms (new-temps source '(temp))) (temp (car parms))) |
| 1063 |
(new-prc source |
| 1064 |
decl |
| 1065 |
#f |
| 1066 |
1 |
| 1067 |
#f |
| 1068 |
parms |
| 1069 |
(new-tst source |
| 1070 |
decl |
| 1071 |
(new-ref source decl temp) |
| 1072 |
(new-call* |
| 1073 |
source |
| 1074 |
decl |
| 1075 |
oper |
| 1076 |
(list (new-ref source decl temp))) |
| 1077 |
alt))) |
| 1078 |
(list pre))) |
| 1079 |
(define (new-seq source decl before after) |
| 1080 |
(new-call* |
| 1081 |
source |
| 1082 |
decl |
| 1083 |
(new-prc source decl #f 1 #f (new-temps source '(temp)) after) |
| 1084 |
(list before))) |
| 1085 |
(define (new-let ptree proc vars vals body) |
| 1086 |
(if (pair? vars) |
| 1087 |
(new-call |
| 1088 |
(node-source ptree) |
| 1089 |
(node-decl ptree) |
| 1090 |
(new-prc (node-source proc) |
| 1091 |
(node-decl proc) |
| 1092 |
(prc-name proc) |
| 1093 |
(length vars) |
| 1094 |
#f |
| 1095 |
(reverse vars) |
| 1096 |
body) |
| 1097 |
(reverse vals)) |
| 1098 |
body)) |
| 1099 |
(define (new-temps source names) |
| 1100 |
(if (null? names) |
| 1101 |
'() |
| 1102 |
(cons (make-var (car names) #t (set-empty) (set-empty) source) |
| 1103 |
(new-temps source (cdr names))))) |
| 1104 |
(define (new-variables vars) |
| 1105 |
(if (null? vars) |
| 1106 |
'() |
| 1107 |
(cons (make-var |
| 1108 |
(source-code (car vars)) |
| 1109 |
#t |
| 1110 |
(set-empty) |
| 1111 |
(set-empty) |
| 1112 |
(car vars)) |
| 1113 |
(new-variables (cdr vars))))) |
| 1114 |
(define (set-prc-names! vars vals) |
| 1115 |
(let loop ((vars vars) (vals vals)) |
| 1116 |
(if (not (null? vars)) |
| 1117 |
(let ((var (car vars)) (val (car vals))) |
| 1118 |
(if (prc? val) (prc-name-set! val (symbol->string (var-name var)))) |
| 1119 |
(loop (cdr vars) (cdr vals)))))) |
| 1120 |
(define (free-variables node) |
| 1121 |
(if (eq? (node-fv node) #t) |
| 1122 |
(let ((x (apply set-union (map free-variables (node-children node))))) |
| 1123 |
(node-fv-set! |
| 1124 |
node |
| 1125 |
(cond ((ref? node) |
| 1126 |
(if (global? (ref-var node)) x (set-adjoin x (ref-var node)))) |
| 1127 |
((set? node) |
| 1128 |
(if (global? (set-var node)) x (set-adjoin x (set-var node)))) |
| 1129 |
((prc? node) (set-difference x (list->set (prc-parms node)))) |
| 1130 |
((and (app? node) (prc? (app-oper node))) |
| 1131 |
(set-difference x (list->set (prc-parms (app-oper node))))) |
| 1132 |
(else x))))) |
| 1133 |
(node-fv node)) |
| 1134 |
(define (bound-variables node) (list->set (prc-parms node))) |
| 1135 |
(define (not-mutable? var) (set-empty? (var-sets var))) |
| 1136 |
(define (mutable? var) (not (not-mutable? var))) |
| 1137 |
(define (bound? var) (var-bound var)) |
| 1138 |
(define (global? var) (not (bound? var))) |
| 1139 |
(define (global-val var) |
| 1140 |
(and (global? var) |
| 1141 |
(let ((sets (set->list (var-sets var)))) |
| 1142 |
(and (pair? sets) |
| 1143 |
(null? (cdr sets)) |
| 1144 |
(def? (car sets)) |
| 1145 |
(eq? (compilation-strategy (node-decl (car sets))) block-sym) |
| 1146 |
(def-val (car sets)))))) |
| 1147 |
(define **not-sym (string->canonical-symbol "##NOT")) |
| 1148 |
(define **quasi-append-sym (string->canonical-symbol "##QUASI-APPEND")) |
| 1149 |
(define **quasi-list-sym (string->canonical-symbol "##QUASI-LIST")) |
| 1150 |
(define **quasi-cons-sym (string->canonical-symbol "##QUASI-CONS")) |
| 1151 |
(define **quasi-list->vector-sym |
| 1152 |
(string->canonical-symbol "##QUASI-LIST->VECTOR")) |
| 1153 |
(define **case-memv-sym (string->canonical-symbol "##CASE-MEMV")) |
| 1154 |
(define **unassigned?-sym (string->canonical-symbol "##UNASSIGNED?")) |
| 1155 |
(define **make-cell-sym (string->canonical-symbol "##MAKE-CELL")) |
| 1156 |
(define **cell-ref-sym (string->canonical-symbol "##CELL-REF")) |
| 1157 |
(define **cell-set!-sym (string->canonical-symbol "##CELL-SET!")) |
| 1158 |
(define **make-placeholder-sym (string->canonical-symbol "##MAKE-PLACEHOLDER")) |
| 1159 |
(define ieee-scheme-sym (string->canonical-symbol "IEEE-SCHEME")) |
| 1160 |
(define r4rs-scheme-sym (string->canonical-symbol "R4RS-SCHEME")) |
| 1161 |
(define multilisp-sym (string->canonical-symbol "MULTILISP")) |
| 1162 |
(define lambda-lift-sym (string->canonical-symbol "LAMBDA-LIFT")) |
| 1163 |
(define block-sym (string->canonical-symbol "BLOCK")) |
| 1164 |
(define separate-sym (string->canonical-symbol "SEPARATE")) |
| 1165 |
(define standard-bindings-sym (string->canonical-symbol "STANDARD-BINDINGS")) |
| 1166 |
(define extended-bindings-sym (string->canonical-symbol "EXTENDED-BINDINGS")) |
| 1167 |
(define safe-sym (string->canonical-symbol "SAFE")) |
| 1168 |
(define interrupts-enabled-sym (string->canonical-symbol "INTERRUPTS-ENABLED")) |
| 1169 |
|
| 1170 |
(define dummy1 |
| 1171 |
(begin |
| 1172 |
(define-flag-decl ieee-scheme-sym 'dialect) |
| 1173 |
(define-flag-decl r4rs-scheme-sym 'dialect) |
| 1174 |
(define-flag-decl multilisp-sym 'dialect) |
| 1175 |
(define-boolean-decl lambda-lift-sym) |
| 1176 |
(define-flag-decl block-sym 'compilation-strategy) |
| 1177 |
(define-flag-decl separate-sym 'compilation-strategy) |
| 1178 |
(define-namable-boolean-decl standard-bindings-sym) |
| 1179 |
(define-namable-boolean-decl extended-bindings-sym) |
| 1180 |
(define-boolean-decl safe-sym) |
| 1181 |
(define-boolean-decl interrupts-enabled-sym) |
| 1182 |
#f)) |
| 1183 |
|
| 1184 |
(define (scheme-dialect decl) |
| 1185 |
(declaration-value 'dialect #f ieee-scheme-sym decl)) |
| 1186 |
(define (lambda-lift? decl) (declaration-value lambda-lift-sym #f #t decl)) |
| 1187 |
(define (compilation-strategy decl) |
| 1188 |
(declaration-value 'compilation-strategy #f separate-sym decl)) |
| 1189 |
(define (standard-binding? name decl) |
| 1190 |
(declaration-value standard-bindings-sym name #f decl)) |
| 1191 |
(define (extended-binding? name decl) |
| 1192 |
(declaration-value extended-bindings-sym name #f decl)) |
| 1193 |
(define (add-extended-bindings decl) |
| 1194 |
(add-decl (list extended-bindings-sym #t) decl)) |
| 1195 |
(define (intrs-enabled? decl) |
| 1196 |
(declaration-value interrupts-enabled-sym #f #t decl)) |
| 1197 |
(define (add-not-interrupts-enabled decl) |
| 1198 |
(add-decl (list interrupts-enabled-sym #f) decl)) |
| 1199 |
(define (safe? decl) (declaration-value safe-sym #f #f decl)) |
| 1200 |
(define (add-not-safe decl) (add-decl (list safe-sym #f) decl)) |
| 1201 |
(define (dialect-specific-keywords dialect) |
| 1202 |
(cond ((eq? dialect ieee-scheme-sym) ieee-scheme-specific-keywords) |
| 1203 |
((eq? dialect r4rs-scheme-sym) r4rs-scheme-specific-keywords) |
| 1204 |
((eq? dialect multilisp-sym) multilisp-specific-keywords) |
| 1205 |
(else |
| 1206 |
(compiler-internal-error |
| 1207 |
"dialect-specific-keywords, unknown dialect" |
| 1208 |
dialect)))) |
| 1209 |
(define (dialect-specific-procedures dialect) |
| 1210 |
(cond ((eq? dialect ieee-scheme-sym) ieee-scheme-specific-procedures) |
| 1211 |
((eq? dialect r4rs-scheme-sym) r4rs-scheme-specific-procedures) |
| 1212 |
((eq? dialect multilisp-sym) multilisp-specific-procedures) |
| 1213 |
(else |
| 1214 |
(compiler-internal-error |
| 1215 |
"dialect-specific-procedures, unknown dialect" |
| 1216 |
dialect)))) |
| 1217 |
(define (make-standard-procedure x) |
| 1218 |
(cons (string->canonical-symbol (car x)) (cdr x))) |
| 1219 |
(define (standard-procedure name decl) |
| 1220 |
(or (assq name (dialect-specific-procedures (scheme-dialect decl))) |
| 1221 |
(assq name common-procedures))) |
| 1222 |
(define (standard-procedure-call-pattern proc) (cdr proc)) |
| 1223 |
(define ieee-scheme-specific-keywords '()) |
| 1224 |
(define ieee-scheme-specific-procedures (map make-standard-procedure '())) |
| 1225 |
(define r4rs-scheme-specific-keywords (list delay-sym)) |
| 1226 |
(define r4rs-scheme-specific-procedures |
| 1227 |
(map make-standard-procedure |
| 1228 |
'(("LIST-TAIL" 2) |
| 1229 |
("-" . 1) |
| 1230 |
("/" . 1) |
| 1231 |
("STRING->LIST" 1) |
| 1232 |
("LIST->STRING" 1) |
| 1233 |
("STRING-COPY" 1) |
| 1234 |
("STRING-FILL!" 2) |
| 1235 |
("VECTOR->LIST" 1) |
| 1236 |
("LIST->VECTOR" 1) |
| 1237 |
("VECTOR-FILL!" 2) |
| 1238 |
("FORCE" 1) |
| 1239 |
("WITH-INPUT-FROM-FILE" 2) |
| 1240 |
("WITH-OUTPUT-TO-FILE" 2) |
| 1241 |
("CHAR-READY?" 0 1) |
| 1242 |
("LOAD" 1) |
| 1243 |
("TRANSCRIPT-ON" 1) |
| 1244 |
("TRANSCRIPT-OFF" 0)))) |
| 1245 |
(define multilisp-specific-keywords (list delay-sym future-sym)) |
| 1246 |
(define multilisp-specific-procedures |
| 1247 |
(map make-standard-procedure '(("FORCE" 1) ("TOUCH" 1)))) |
| 1248 |
(define common-keywords |
| 1249 |
(list quote-sym |
| 1250 |
quasiquote-sym |
| 1251 |
unquote-sym |
| 1252 |
unquote-splicing-sym |
| 1253 |
lambda-sym |
| 1254 |
if-sym |
| 1255 |
set!-sym |
| 1256 |
cond-sym |
| 1257 |
=>-sym |
| 1258 |
else-sym |
| 1259 |
and-sym |
| 1260 |
or-sym |
| 1261 |
case-sym |
| 1262 |
let-sym |
| 1263 |
let*-sym |
| 1264 |
letrec-sym |
| 1265 |
begin-sym |
| 1266 |
do-sym |
| 1267 |
define-sym |
| 1268 |
**define-macro-sym |
| 1269 |
**declare-sym |
| 1270 |
**include-sym)) |
| 1271 |
(define common-procedures |
| 1272 |
(map make-standard-procedure |
| 1273 |
'(("NOT" 1) |
| 1274 |
("BOOLEAN?" 1) |
| 1275 |
("EQV?" 2) |
| 1276 |
("EQ?" 2) |
| 1277 |
("EQUAL?" 2) |
| 1278 |
("PAIR?" 1) |
| 1279 |
("CONS" 2) |
| 1280 |
("CAR" 1) |
| 1281 |
("CDR" 1) |
| 1282 |
("SET-CAR!" 2) |
| 1283 |
("SET-CDR!" 2) |
| 1284 |
("CAAR" 1) |
| 1285 |
("CADR" 1) |
| 1286 |
("CDAR" 1) |
| 1287 |
("CDDR" 1) |
| 1288 |
("CAAAR" 1) |
| 1289 |
("CAADR" 1) |
| 1290 |
("CADAR" 1) |
| 1291 |
("CADDR" 1) |
| 1292 |
("CDAAR" 1) |
| 1293 |
("CDADR" 1) |
| 1294 |
("CDDAR" 1) |
| 1295 |
("CDDDR" 1) |
| 1296 |
("CAAAAR" 1) |
| 1297 |
("CAAADR" 1) |
| 1298 |
("CAADAR" 1) |
| 1299 |
("CAADDR" 1) |
| 1300 |
("CADAAR" 1) |
| 1301 |
("CADADR" 1) |
| 1302 |
("CADDAR" 1) |
| 1303 |
("CADDDR" 1) |
| 1304 |
("CDAAAR" 1) |
| 1305 |
("CDAADR" 1) |
| 1306 |
("CDADAR" 1) |
| 1307 |
("CDADDR" 1) |
| 1308 |
("CDDAAR" 1) |
| 1309 |
("CDDADR" 1) |
| 1310 |
("CDDDAR" 1) |
| 1311 |
("CDDDDR" 1) |
| 1312 |
("NULL?" 1) |
| 1313 |
("LIST?" 1) |
| 1314 |
("LIST" . 0) |
| 1315 |
("LENGTH" 1) |
| 1316 |
("APPEND" . 0) |
| 1317 |
("REVERSE" 1) |
| 1318 |
("LIST-REF" 2) |
| 1319 |
("MEMQ" 2) |
| 1320 |
("MEMV" 2) |
| 1321 |
("MEMBER" 2) |
| 1322 |
("ASSQ" 2) |
| 1323 |
("ASSV" 2) |
| 1324 |
("ASSOC" 2) |
| 1325 |
("SYMBOL?" 1) |
| 1326 |
("SYMBOL->STRING" 1) |
| 1327 |
("STRING->SYMBOL" 1) |
| 1328 |
("NUMBER?" 1) |
| 1329 |
("COMPLEX?" 1) |
| 1330 |
("REAL?" 1) |
| 1331 |
("RATIONAL?" 1) |
| 1332 |
("INTEGER?" 1) |
| 1333 |
("EXACT?" 1) |
| 1334 |
("INEXACT?" 1) |
| 1335 |
("=" . 2) |
| 1336 |
("<" . 2) |
| 1337 |
(">" . 2) |
| 1338 |
("<=" . 2) |
| 1339 |
(">=" . 2) |
| 1340 |
("ZERO?" 1) |
| 1341 |
("POSITIVE?" 1) |
| 1342 |
("NEGATIVE?" 1) |
| 1343 |
("ODD?" 1) |
| 1344 |
("EVEN?" 1) |
| 1345 |
("MAX" . 1) |
| 1346 |
("MIN" . 1) |
| 1347 |
("+" . 0) |
| 1348 |
("*" . 0) |
| 1349 |
("-" 1 2) |
| 1350 |
("/" 1 2) |
| 1351 |
("ABS" 1) |
| 1352 |
("QUOTIENT" 2) |
| 1353 |
("REMAINDER" 2) |
| 1354 |
("MODULO" 2) |
| 1355 |
("GCD" . 0) |
| 1356 |
("LCM" . 0) |
| 1357 |
("NUMERATOR" 1) |
| 1358 |
("DENOMINATOR" 1) |
| 1359 |
("FLOOR" 1) |
| 1360 |
("CEILING" 1) |
| 1361 |
("TRUNCATE" 1) |
| 1362 |
("ROUND" 1) |
| 1363 |
("RATIONALIZE" 2) |
| 1364 |
("EXP" 1) |
| 1365 |
("LOG" 1) |
| 1366 |
("SIN" 1) |
| 1367 |
("COS" 1) |
| 1368 |
("TAN" 1) |
| 1369 |
("ASIN" 1) |
| 1370 |
("ACOS" 1) |
| 1371 |
("ATAN" 1 2) |
| 1372 |
("SQRT" 1) |
| 1373 |
("EXPT" 2) |
| 1374 |
("MAKE-RECTANGULAR" 2) |
| 1375 |
("MAKE-POLAR" 2) |
| 1376 |
("REAL-PART" 1) |
| 1377 |
("IMAG-PART" 1) |
| 1378 |
("MAGNITUDE" 1) |
| 1379 |
("ANGLE" 1) |
| 1380 |
("EXACT->INEXACT" 1) |
| 1381 |
("INEXACT->EXACT" 1) |
| 1382 |
("NUMBER->STRING" 1 2) |
| 1383 |
("STRING->NUMBER" 1 2) |
| 1384 |
("CHAR?" 1) |
| 1385 |
("CHAR=?" 2) |
| 1386 |
("CHAR<?" 2) |
| 1387 |
("CHAR>?" 2) |
| 1388 |
("CHAR<=?" 2) |
| 1389 |
("CHAR>=?" 2) |
| 1390 |
("CHAR-CI=?" 2) |
| 1391 |
("CHAR-CI<?" 2) |
| 1392 |
("CHAR-CI>?" 2) |
| 1393 |
("CHAR-CI<=?" 2) |
| 1394 |
("CHAR-CI>=?" 2) |
| 1395 |
("CHAR-ALPHABETIC?" 1) |
| 1396 |
("CHAR-NUMERIC?" 1) |
| 1397 |
("CHAR-WHITESPACE?" 1) |
| 1398 |
("CHAR-UPPER-CASE?" 1) |
| 1399 |
("CHAR-LOWER-CASE?" 1) |
| 1400 |
("CHAR->INTEGER" 1) |
| 1401 |
("INTEGER->CHAR" 1) |
| 1402 |
("CHAR-UPCASE" 1) |
| 1403 |
("CHAR-DOWNCASE" 1) |
| 1404 |
("STRING?" 1) |
| 1405 |
("MAKE-STRING" 1 2) |
| 1406 |
("STRING" . 0) |
| 1407 |
("STRING-LENGTH" 1) |
| 1408 |
("STRING-REF" 2) |
| 1409 |
("STRING-SET!" 3) |
| 1410 |
("STRING=?" 2) |
| 1411 |
("STRING<?" 2) |
| 1412 |
("STRING>?" 2) |
| 1413 |
("STRING<=?" 2) |
| 1414 |
("STRING>=?" 2) |
| 1415 |
("STRING-CI=?" 2) |
| 1416 |
("STRING-CI<?" 2) |
| 1417 |
("STRING-CI>?" 2) |
| 1418 |
("STRING-CI<=?" 2) |
| 1419 |
("STRING-CI>=?" 2) |
| 1420 |
("SUBSTRING" 3) |
| 1421 |
("STRING-APPEND" . 0) |
| 1422 |
("VECTOR?" 1) |
| 1423 |
("MAKE-VECTOR" 1 2) |
| 1424 |
("VECTOR" . 0) |
| 1425 |
("VECTOR-LENGTH" 1) |
| 1426 |
("VECTOR-REF" 2) |
| 1427 |
("VECTOR-SET!" 3) |
| 1428 |
("PROCEDURE?" 1) |
| 1429 |
("APPLY" . 2) |
| 1430 |
("MAP" . 2) |
| 1431 |
("FOR-EACH" . 2) |
| 1432 |
("CALL-WITH-CURRENT-CONTINUATION" 1) |
| 1433 |
("CALL-WITH-INPUT-FILE" 2) |
| 1434 |
("CALL-WITH-OUTPUT-FILE" 2) |
| 1435 |
("INPUT-PORT?" 1) |
| 1436 |
("OUTPUT-PORT?" 1) |
| 1437 |
("CURRENT-INPUT-PORT" 0) |
| 1438 |
("CURRENT-OUTPUT-PORT" 0) |
| 1439 |
("OPEN-INPUT-FILE" 1) |
| 1440 |
("OPEN-OUTPUT-FILE" 1) |
| 1441 |
("CLOSE-INPUT-PORT" 1) |
| 1442 |
("CLOSE-OUTPUT-PORT" 1) |
| 1443 |
("EOF-OBJECT?" 1) |
| 1444 |
("READ" 0 1) |
| 1445 |
("READ-CHAR" 0 1) |
| 1446 |
("PEEK-CHAR" 0 1) |
| 1447 |
("WRITE" 1 2) |
| 1448 |
("DISPLAY" 1 2) |
| 1449 |
("NEWLINE" 0 1) |
| 1450 |
("WRITE-CHAR" 1 2)))) |
| 1451 |
(define (parse-program program env module-name proc) |
| 1452 |
(define (parse-prog program env lst proc) |
| 1453 |
(if (null? program) |
| 1454 |
(proc (reverse lst) env) |
| 1455 |
(let ((source (car program))) |
| 1456 |
(cond ((macro-expr? source env) |
| 1457 |
(parse-prog |
| 1458 |
(cons (macro-expand source env) (cdr program)) |
| 1459 |
env |
| 1460 |
lst |
| 1461 |
proc)) |
| 1462 |
((begin-defs-expr? source) |
| 1463 |
(parse-prog |
| 1464 |
(append (begin-defs-body source) (cdr program)) |
| 1465 |
env |
| 1466 |
lst |
| 1467 |
proc)) |
| 1468 |
((include-expr? source) |
| 1469 |
(if *ptree-port* (display " " *ptree-port*)) |
| 1470 |
(let ((x (file->sources* |
| 1471 |
(include-filename source) |
| 1472 |
*ptree-port* |
| 1473 |
(source-locat source)))) |
| 1474 |
(if *ptree-port* (newline *ptree-port*)) |
| 1475 |
(parse-prog (append x (cdr program)) env lst proc))) |
| 1476 |
((define-macro-expr? source env) |
| 1477 |
(if *ptree-port* |
| 1478 |
(begin |
| 1479 |
(display " \"macro\"" *ptree-port*) |
| 1480 |
(newline *ptree-port*))) |
| 1481 |
(parse-prog (cdr program) (add-macro source env) lst proc)) |
| 1482 |
((declare-expr? source) |
| 1483 |
(if *ptree-port* |
| 1484 |
(begin |
| 1485 |
(display " \"decl\"" *ptree-port*) |
| 1486 |
(newline *ptree-port*))) |
| 1487 |
(parse-prog |
| 1488 |
(cdr program) |
| 1489 |
(add-declarations source env) |
| 1490 |
lst |
| 1491 |
proc)) |
| 1492 |
((define-expr? source env) |
| 1493 |
(let* ((var** (definition-variable source)) |
| 1494 |
(var* (source-code var**)) |
| 1495 |
(var (env-lookup-var env var* var**))) |
| 1496 |
(if *ptree-port* |
| 1497 |
(begin |
| 1498 |
(display " " *ptree-port*) |
| 1499 |
(display (var-name var) *ptree-port*) |
| 1500 |
(newline *ptree-port*))) |
| 1501 |
(let ((node (pt (definition-value source) env 'true))) |
| 1502 |
(set-prc-names! (list var) (list node)) |
| 1503 |
(parse-prog |
| 1504 |
(cdr program) |
| 1505 |
env |
| 1506 |
(cons (cons (new-def source |
| 1507 |
(env-declarations env) |
| 1508 |
var |
| 1509 |
node) |
| 1510 |
env) |
| 1511 |
lst) |
| 1512 |
proc)))) |
| 1513 |
((c-declaration-expr? source) |
| 1514 |
(if *ptree-port* |
| 1515 |
(begin |
| 1516 |
(display " \"c-decl\"" *ptree-port*) |
| 1517 |
(newline *ptree-port*))) |
| 1518 |
(add-c-declaration (source-code (cadr (source-code source)))) |
| 1519 |
(parse-prog (cdr program) env lst proc)) |
| 1520 |
((c-init-expr? source) |
| 1521 |
(if *ptree-port* |
| 1522 |
(begin |
| 1523 |
(display " \"c-init\"" *ptree-port*) |
| 1524 |
(newline *ptree-port*))) |
| 1525 |
(add-c-init (source-code (cadr (source-code source)))) |
| 1526 |
(parse-prog (cdr program) env lst proc)) |
| 1527 |
(else |
| 1528 |
(if *ptree-port* |
| 1529 |
(begin |
| 1530 |
(display " \"expr\"" *ptree-port*) |
| 1531 |
(newline *ptree-port*))) |
| 1532 |
(parse-prog |
| 1533 |
(cdr program) |
| 1534 |
env |
| 1535 |
(cons (cons (pt source env 'true) env) lst) |
| 1536 |
proc)))))) |
| 1537 |
(if *ptree-port* |
| 1538 |
(begin (display "Parsing:" *ptree-port*) (newline *ptree-port*))) |
| 1539 |
(c-interface-begin module-name) |
| 1540 |
(parse-prog |
| 1541 |
program |
| 1542 |
env |
| 1543 |
'() |
| 1544 |
(lambda (lst env) |
| 1545 |
(if *ptree-port* (newline *ptree-port*)) |
| 1546 |
(proc lst env (c-interface-end))))) |
| 1547 |
(define (c-interface-begin module-name) |
| 1548 |
(set! c-interface-module-name module-name) |
| 1549 |
(set! c-interface-proc-count 0) |
| 1550 |
(set! c-interface-decls '()) |
| 1551 |
(set! c-interface-procs '()) |
| 1552 |
(set! c-interface-inits '()) |
| 1553 |
#f) |
| 1554 |
(define (c-interface-end) |
| 1555 |
(let ((i (make-c-intf |
| 1556 |
(reverse c-interface-decls) |
| 1557 |
(reverse c-interface-procs) |
| 1558 |
(reverse c-interface-inits)))) |
| 1559 |
(set! c-interface-module-name #f) |
| 1560 |
(set! c-interface-proc-count #f) |
| 1561 |
(set! c-interface-decls #f) |
| 1562 |
(set! c-interface-procs #f) |
| 1563 |
(set! c-interface-inits #f) |
| 1564 |
i)) |
| 1565 |
(define c-interface-module-name #f) |
| 1566 |
(define c-interface-proc-count #f) |
| 1567 |
(define c-interface-decls #f) |
| 1568 |
(define c-interface-procs #f) |
| 1569 |
(define c-interface-inits #f) |
| 1570 |
(define (make-c-intf decls procs inits) (vector decls procs inits)) |
| 1571 |
(define (c-intf-decls c-intf) (vector-ref c-intf 0)) |
| 1572 |
(define (c-intf-decls-set! c-intf x) (vector-set! c-intf 0 x)) |
| 1573 |
(define (c-intf-procs c-intf) (vector-ref c-intf 1)) |
| 1574 |
(define (c-intf-procs-set! c-intf x) (vector-set! c-intf 1 x)) |
| 1575 |
(define (c-intf-inits c-intf) (vector-ref c-intf 2)) |
| 1576 |
(define (c-intf-inits-set! c-intf x) (vector-set! c-intf 2 x)) |
| 1577 |
(define (c-declaration-expr? source) |
| 1578 |
(and (mymatch **c-declaration-sym 1 source) |
| 1579 |
(let ((code (source-code source))) |
| 1580 |
(or (string? (source-code (cadr code))) |
| 1581 |
(pt-syntax-error |
| 1582 |
source |
| 1583 |
"Argument to '##c-declaration' must be a string"))))) |
| 1584 |
(define (c-init-expr? source) |
| 1585 |
(and (mymatch **c-init-sym 1 source) |
| 1586 |
(let ((code (source-code source))) |
| 1587 |
(or (string? (source-code (cadr code))) |
| 1588 |
(pt-syntax-error |
| 1589 |
source |
| 1590 |
"Argument to '##c-init' must be a string"))))) |
| 1591 |
(define (c-procedure-expr? source) |
| 1592 |
(and (mymatch **c-procedure-sym 3 source) |
| 1593 |
(let ((code (source-code source))) |
| 1594 |
(if (not (string? (source-code (cadddr code)))) |
| 1595 |
(pt-syntax-error |
| 1596 |
source |
| 1597 |
"Last argument to '##c-procedure' must be a string") |
| 1598 |
(check-arg-and-result-types source (cadr code) (caddr code)))))) |
| 1599 |
(define scheme-to-c-notation |
| 1600 |
(list (list void-sym "VOID" "void") |
| 1601 |
(list char-sym "CHAR" "char") |
| 1602 |
(list signed-char-sym "SCHAR" "signed char") |
| 1603 |
(list unsigned-char-sym "UCHAR" "unsigned char") |
| 1604 |
(list short-sym "SHORT" "short") |
| 1605 |
(list unsigned-short-sym "USHORT" "unsigned short") |
| 1606 |
(list int-sym "INT" "int") |
| 1607 |
(list unsigned-int-sym "UINT" "unsigned int") |
| 1608 |
(list long-sym "LONG" "long") |
| 1609 |
(list unsigned-long-sym "ULONG" "unsigned long") |
| 1610 |
(list float-sym "FLOAT" "float") |
| 1611 |
(list double-sym "DOUBLE" "double") |
| 1612 |
(list pointer-sym "POINTER" "void*") |
| 1613 |
(list boolean-sym "BOOLEAN" "int") |
| 1614 |
(list string-sym "STRING" "char*") |
| 1615 |
(list scheme-object-sym "SCMOBJ" "long"))) |
| 1616 |
(define (convert-type typ) (if (assq typ scheme-to-c-notation) typ #f)) |
| 1617 |
(define (check-arg-and-result-types source arg-typs-source res-typ-source) |
| 1618 |
(let ((arg-typs (source-code arg-typs-source)) |
| 1619 |
(res-typ (source-code res-typ-source))) |
| 1620 |
(let ((res-type (convert-type res-typ))) |
| 1621 |
(if (not res-type) |
| 1622 |
(pt-syntax-error res-typ-source "Invalid result type") |
| 1623 |
(if (not (proper-length arg-typs)) |
| 1624 |
(pt-syntax-error |
| 1625 |
arg-typs-source |
| 1626 |
"Ill-terminated argument type list") |
| 1627 |
(let loop ((lst arg-typs)) |
| 1628 |
(if (pair? lst) |
| 1629 |
(let* ((arg-typ (source-code (car lst))) |
| 1630 |
(arg-type (convert-type arg-typ))) |
| 1631 |
(if (or (not arg-type) (eq? arg-type void-sym)) |
| 1632 |
(pt-syntax-error (car lst) "Invalid argument type") |
| 1633 |
(loop (cdr lst)))) |
| 1634 |
#t))))))) |
| 1635 |
(define (add-c-declaration declaration-string) |
| 1636 |
(set! c-interface-decls (cons declaration-string c-interface-decls)) |
| 1637 |
#f) |
| 1638 |
(define (add-c-init initialization-code-string) |
| 1639 |
(set! c-interface-inits (cons initialization-code-string c-interface-inits)) |
| 1640 |
#f) |
| 1641 |
(define (add-c-proc scheme-name c-name arity def) |
| 1642 |
(set! c-interface-procs |
| 1643 |
(cons (vector scheme-name c-name arity def) c-interface-procs)) |
| 1644 |
#f) |
| 1645 |
(define (pt-c-procedure source env use) |
| 1646 |
(let* ((code (source-code source)) |
| 1647 |
(name (build-c-procedure |
| 1648 |
(map source-code (source-code (cadr code))) |
| 1649 |
(source-code (caddr code)) |
| 1650 |
(source-code (cadddr code)))) |
| 1651 |
(decl (env-declarations env))) |
| 1652 |
(new-ref source decl (env-lookup-global-var env (string->symbol name))))) |
| 1653 |
(define (build-c-procedure argument-types result-type proc-name-or-code) |
| 1654 |
(define proc-name? |
| 1655 |
(let loop ((i (- (string-length proc-name-or-code) 1))) |
| 1656 |
(if (>= i 0) |
| 1657 |
(let ((c (string-ref proc-name-or-code i))) |
| 1658 |
(if (or (char-alphabetic? c) (char=? c #\_)) (loop (- i 1)) #f)) |
| 1659 |
#t))) |
| 1660 |
(define nl (string #\newline)) |
| 1661 |
(define undefined-value "UND") |
| 1662 |
(define scheme-arg-prefix "ARG") |
| 1663 |
(define scheme-result-name "RESULT") |
| 1664 |
(define c-arg-prefix "arg") |
| 1665 |
(define c-result-name "result") |
| 1666 |
(define scheme-to-c-prefix "SCMOBJ_TO_") |
| 1667 |
(define c-to-scheme-suffix "_TO_SCMOBJ") |
| 1668 |
(define (c-type-name typ) (cadr (assq typ scheme-to-c-notation))) |
| 1669 |
(define (c-type-decl typ) (caddr (assq typ scheme-to-c-notation))) |
| 1670 |
(define (listify strings) |
| 1671 |
(if (null? strings) |
| 1672 |
"" |
| 1673 |
(string-append |
| 1674 |
(car strings) |
| 1675 |
(apply string-append |
| 1676 |
(map (lambda (s) (string-append "," s)) (cdr strings)))))) |
| 1677 |
(define (scheme-arg-var t) |
| 1678 |
(string-append c-id-prefix scheme-arg-prefix (number->string (cdr t)))) |
| 1679 |
(define (c-arg-var t) |
| 1680 |
(string-append c-id-prefix c-arg-prefix (number->string (cdr t)))) |
| 1681 |
(define (make-c-procedure arg-types res-type) |
| 1682 |
(define (make-arg-decl) |
| 1683 |
(apply string-append |
| 1684 |
(map (lambda (t) |
| 1685 |
(string-append |
| 1686 |
(c-type-decl (car t)) |
| 1687 |
" " |
| 1688 |
(c-arg-var t) |
| 1689 |
";" |
| 1690 |
nl)) |
| 1691 |
arg-types))) |
| 1692 |
(define (make-conversions) |
| 1693 |
(if (not (null? arg-types)) |
| 1694 |
(let loop ((lst arg-types) (str (string-append "if (" nl))) |
| 1695 |
(if (null? lst) |
| 1696 |
(string-append str " )" nl) |
| 1697 |
(let ((t (car lst)) (rest (cdr lst))) |
| 1698 |
(loop rest |
| 1699 |
(string-append |
| 1700 |
str |
| 1701 |
" " |
| 1702 |
c-id-prefix |
| 1703 |
scheme-to-c-prefix |
| 1704 |
(c-type-name (car t)) |
| 1705 |
"(" |
| 1706 |
(scheme-arg-var t) |
| 1707 |
"," |
| 1708 |
(c-arg-var t) |
| 1709 |
")" |
| 1710 |
(if (null? rest) "" " &&") |
| 1711 |
nl))))) |
| 1712 |
"")) |
| 1713 |
(define (make-body) |
| 1714 |
(if proc-name? |
| 1715 |
(let* ((param-list (listify (map c-arg-var arg-types))) |
| 1716 |
(call (string-append proc-name-or-code "(" param-list ")"))) |
| 1717 |
(if (eq? res-type void-sym) |
| 1718 |
(string-append |
| 1719 |
"{" |
| 1720 |
nl |
| 1721 |
call |
| 1722 |
";" |
| 1723 |
nl |
| 1724 |
c-id-prefix |
| 1725 |
scheme-result-name |
| 1726 |
" = " |
| 1727 |
c-id-prefix |
| 1728 |
undefined-value |
| 1729 |
";" |
| 1730 |
nl |
| 1731 |
"}" |
| 1732 |
nl) |
| 1733 |
(string-append |
| 1734 |
c-id-prefix |
| 1735 |
(c-type-name res-type) |
| 1736 |
c-to-scheme-suffix |
| 1737 |
"(" |
| 1738 |
call |
| 1739 |
"," |
| 1740 |
c-id-prefix |
| 1741 |
scheme-result-name |
| 1742 |
");" |
| 1743 |
nl))) |
| 1744 |
(if (eq? res-type void-sym) |
| 1745 |
(string-append |
| 1746 |
"{" |
| 1747 |
nl |
| 1748 |
proc-name-or-code |
| 1749 |
nl |
| 1750 |
c-id-prefix |
| 1751 |
scheme-result-name |
| 1752 |
" = " |
| 1753 |
c-id-prefix |
| 1754 |
undefined-value |
| 1755 |
";" |
| 1756 |
nl |
| 1757 |
"}" |
| 1758 |
nl) |
| 1759 |
(string-append |
| 1760 |
"{" |
| 1761 |
nl |
| 1762 |
proc-name-or-code |
| 1763 |
nl |
| 1764 |
c-id-prefix |
| 1765 |
(c-type-name res-type) |
| 1766 |
c-to-scheme-suffix |
| 1767 |
"(" |
| 1768 |
c-id-prefix |
| 1769 |
c-result-name |
| 1770 |
"," |
| 1771 |
c-id-prefix |
| 1772 |
scheme-result-name |
| 1773 |
");" |
| 1774 |
nl |
| 1775 |
"}" |
| 1776 |
nl)))) |
| 1777 |
(let* ((index (number->string c-interface-proc-count)) |
| 1778 |
(scheme-name (string-append "#!" c-interface-module-name "#" index)) |
| 1779 |
(c-name (string-append c-id-prefix (scheme-id->c-id scheme-name))) |
| 1780 |
(arity (length argument-types)) |
| 1781 |
(def (string-append |
| 1782 |
(if (or proc-name? (eq? res-type void-sym)) |
| 1783 |
"" |
| 1784 |
(string-append |
| 1785 |
(c-type-decl res-type) |
| 1786 |
" " |
| 1787 |
c-id-prefix |
| 1788 |
c-result-name |
| 1789 |
";" |
| 1790 |
nl)) |
| 1791 |
(make-arg-decl) |
| 1792 |
(make-conversions) |
| 1793 |
(make-body)))) |
| 1794 |
(set! c-interface-proc-count (+ c-interface-proc-count 1)) |
| 1795 |
(add-c-proc scheme-name c-name arity def) |
| 1796 |
scheme-name)) |
| 1797 |
(let loop ((i 1) (lst1 argument-types) (lst2 '())) |
| 1798 |
(if (pair? lst1) |
| 1799 |
(loop (+ i 1) (cdr lst1) (cons (cons (car lst1) i) lst2)) |
| 1800 |
(make-c-procedure (reverse lst2) result-type)))) |
| 1801 |
(define (scheme-id->c-id s) |
| 1802 |
(define (hex->char i) (string-ref "0123456789abcdef" i)) |
| 1803 |
(let loop ((i (- (string-length s) 1)) (l '())) |
| 1804 |
(if (>= i 0) |
| 1805 |
(let ((c (string-ref s i))) |
| 1806 |
(cond ((or (char-alphabetic? c) (char-numeric? c)) |
| 1807 |
(loop (- i 1) (cons c l))) |
| 1808 |
((char=? c #\_) (loop (- i 1) (cons c (cons c l)))) |
| 1809 |
(else |
| 1810 |
(let ((n (character-encoding c))) |
| 1811 |
(loop (- i 1) |
| 1812 |
(cons #\_ |
| 1813 |
(cons (hex->char (quotient n 16)) |
| 1814 |
(cons (hex->char (modulo n 16)) l)))))))) |
| 1815 |
(lst->string l)))) |
| 1816 |
(define (pt-syntax-error source msg . args) |
| 1817 |
(apply compiler-user-error |
| 1818 |
(cons (source-locat source) |
| 1819 |
(cons (string-append "Syntax error -- " msg) args)))) |
| 1820 |
(define (pt source env use) |
| 1821 |
(cond ((macro-expr? source env) (pt (macro-expand source env) env use)) |
| 1822 |
((self-eval-expr? source) (pt-self-eval source env use)) |
| 1823 |
((quote-expr? source) (pt-quote source env use)) |
| 1824 |
((quasiquote-expr? source) (pt-quasiquote source env use)) |
| 1825 |
((unquote-expr? source) |
| 1826 |
(pt-syntax-error source "Ill-placed 'unquote'")) |
| 1827 |
((unquote-splicing-expr? source) |
| 1828 |
(pt-syntax-error source "Ill-placed 'unquote-splicing'")) |
| 1829 |
((var-expr? source env) (pt-var source env use)) |
| 1830 |
((set!-expr? source env) (pt-set! source env use)) |
| 1831 |
((lambda-expr? source env) (pt-lambda source env use)) |
| 1832 |
((if-expr? source) (pt-if source env use)) |
| 1833 |
((cond-expr? source) (pt-cond source env use)) |
| 1834 |
((and-expr? source) (pt-and source env use)) |
| 1835 |
((or-expr? source) (pt-or source env use)) |
| 1836 |
((case-expr? source) (pt-case source env use)) |
| 1837 |
((let-expr? source env) (pt-let source env use)) |
| 1838 |
((let*-expr? source env) (pt-let* source env use)) |
| 1839 |
((letrec-expr? source env) (pt-letrec source env use)) |
| 1840 |
((begin-expr? source) (pt-begin source env use)) |
| 1841 |
((do-expr? source env) (pt-do source env use)) |
| 1842 |
((define-expr? source env) |
| 1843 |
(pt-syntax-error source "Ill-placed 'define'")) |
| 1844 |
((delay-expr? source env) (pt-delay source env use)) |
| 1845 |
((future-expr? source env) (pt-future source env use)) |
| 1846 |
((define-macro-expr? source env) |
| 1847 |
(pt-syntax-error source "Ill-placed '##define-macro'")) |
| 1848 |
((begin-defs-expr? source) |
| 1849 |
(pt-syntax-error source "Ill-placed 'begin' style definitions")) |
| 1850 |
((declare-expr? source) |
| 1851 |
(pt-syntax-error source "Ill-placed '##declare'")) |
| 1852 |
((c-declaration-expr? source) |
| 1853 |
(pt-syntax-error source "Ill-placed '##c-declaration'")) |
| 1854 |
((c-init-expr? source) |
| 1855 |
(pt-syntax-error source "Ill-placed '##c-init'")) |
| 1856 |
((c-procedure-expr? source) (pt-c-procedure source env use)) |
| 1857 |
((combination-expr? source) (pt-combination source env use)) |
| 1858 |
(else (compiler-internal-error "pt, unknown expression type" source)))) |
| 1859 |
(define (macro-expand source env) |
| 1860 |
(let ((code (source-code source))) |
| 1861 |
(expression->source |
| 1862 |
(apply (cdr (env-lookup-macro env (source-code (car code)))) |
| 1863 |
(cdr (source->expression source))) |
| 1864 |
source))) |
| 1865 |
(define (pt-self-eval source env use) |
| 1866 |
(let ((val (source->expression source))) |
| 1867 |
(if (eq? use 'none) |
| 1868 |
(new-cst source (env-declarations env) undef-object) |
| 1869 |
(new-cst source (env-declarations env) val)))) |
| 1870 |
(define (pt-quote source env use) |
| 1871 |
(let ((code (source-code source))) |
| 1872 |
(if (eq? use 'none) |
| 1873 |
(new-cst source (env-declarations env) undef-object) |
| 1874 |
(new-cst source |
| 1875 |
(env-declarations env) |
| 1876 |
(source->expression (cadr code)))))) |
| 1877 |
(define (pt-quasiquote source env use) |
| 1878 |
(let ((code (source-code source))) (pt-quasiquotation (cadr code) 1 env))) |
| 1879 |
(define (pt-quasiquotation form level env) |
| 1880 |
(cond ((= level 0) (pt form env 'true)) |
| 1881 |
((quasiquote-expr? form) |
| 1882 |
(pt-quasiquotation-list form (source-code form) (+ level 1) env)) |
| 1883 |
((unquote-expr? form) |
| 1884 |
(if (= level 1) |
| 1885 |
(pt (cadr (source-code form)) env 'true) |
| 1886 |
(pt-quasiquotation-list form (source-code form) (- level 1) env))) |
| 1887 |
((unquote-splicing-expr? form) |
| 1888 |
(if (= level 1) |
| 1889 |
(pt-syntax-error form "Ill-placed 'unquote-splicing'") |
| 1890 |
(pt-quasiquotation-list form (source-code form) (- level 1) env))) |
| 1891 |
((pair? (source-code form)) |
| 1892 |
(pt-quasiquotation-list form (source-code form) level env)) |
| 1893 |
((vector? (source-code form)) |
| 1894 |
(vector-form |
| 1895 |
form |
| 1896 |
(pt-quasiquotation-list |
| 1897 |
form |
| 1898 |
(vector->lst (source-code form)) |
| 1899 |
level |
| 1900 |
env) |
| 1901 |
env)) |
| 1902 |
(else |
| 1903 |
(new-cst form (env-declarations env) (source->expression form))))) |
| 1904 |
(define (pt-quasiquotation-list form l level env) |
| 1905 |
(cond ((pair? l) |
| 1906 |
(if (and (unquote-splicing-expr? (car l)) (= level 1)) |
| 1907 |
(let ((x (pt (cadr (source-code (car l))) env 'true))) |
| 1908 |
(if (null? (cdr l)) |
| 1909 |
x |
| 1910 |
(append-form |
| 1911 |
(car l) |
| 1912 |
x |
| 1913 |
(pt-quasiquotation-list form (cdr l) 1 env) |
| 1914 |
env))) |
| 1915 |
(cons-form |
| 1916 |
form |
| 1917 |
(pt-quasiquotation (car l) level env) |
| 1918 |
(pt-quasiquotation-list form (cdr l) level env) |
| 1919 |
env))) |
| 1920 |
((null? l) (new-cst form (env-declarations env) '())) |
| 1921 |
(else (pt-quasiquotation l level env)))) |
| 1922 |
(define (append-form source ptree1 ptree2 env) |
| 1923 |
(cond ((and (cst? ptree1) (cst? ptree2)) |
| 1924 |
(new-cst source |
| 1925 |
(env-declarations env) |
| 1926 |
(append (cst-val ptree1) (cst-val ptree2)))) |
| 1927 |
((and (cst? ptree2) (null? (cst-val ptree2))) ptree1) |
| 1928 |
(else |
| 1929 |
(new-call* |
| 1930 |
source |
| 1931 |
(add-not-safe (env-declarations env)) |
| 1932 |
(new-ref-extended-bindings source **quasi-append-sym env) |
| 1933 |
(list ptree1 ptree2))))) |
| 1934 |
(define (cons-form source ptree1 ptree2 env) |
| 1935 |
(cond ((and (cst? ptree1) (cst? ptree2)) |
| 1936 |
(new-cst source |
| 1937 |
(env-declarations env) |
| 1938 |
(cons (cst-val ptree1) (cst-val ptree2)))) |
| 1939 |
((and (cst? ptree2) (null? (cst-val ptree2))) |
| 1940 |
(new-call* |
| 1941 |
source |
| 1942 |
(add-not-safe (env-declarations env)) |
| 1943 |
(new-ref-extended-bindings source **quasi-list-sym env) |
| 1944 |
(list ptree1))) |
| 1945 |
(else |
| 1946 |
(new-call* |
| 1947 |
source |
| 1948 |
(add-not-safe (env-declarations env)) |
| 1949 |
(new-ref-extended-bindings source **quasi-cons-sym env) |
| 1950 |
(list ptree1 ptree2))))) |
| 1951 |
(define (vector-form source ptree env) |
| 1952 |
(if (cst? ptree) |
| 1953 |
(new-cst source (env-declarations env) (lst->vector (cst-val ptree))) |
| 1954 |
(new-call* |
| 1955 |
source |
| 1956 |
(add-not-safe (env-declarations env)) |
| 1957 |
(new-ref-extended-bindings source **quasi-list->vector-sym env) |
| 1958 |
(list ptree)))) |
| 1959 |
(define (pt-var source env use) |
| 1960 |
(if (eq? use 'none) |
| 1961 |
(new-cst source (env-declarations env) undef-object) |
| 1962 |
(new-ref source |
| 1963 |
(env-declarations env) |
| 1964 |
(env-lookup-var env (source-code source) source)))) |
| 1965 |
(define (pt-set! source env use) |
| 1966 |
(let ((code (source-code source))) |
| 1967 |
(new-set source |
| 1968 |
(env-declarations env) |
| 1969 |
(env-lookup-var env (source-code (cadr code)) (cadr code)) |
| 1970 |
(pt (caddr code) env 'true)))) |
| 1971 |
(define (pt-lambda source env use) |
| 1972 |
(let ((code (source-code source))) |
| 1973 |
(define (new-params parms) |
| 1974 |
(cond ((pair? parms) |
| 1975 |
(let* ((parm* (car parms)) |
| 1976 |
(parm (source-code parm*)) |
| 1977 |
(p* (if (pair? parm) (car parm) parm*))) |
| 1978 |
(cons (make-var (source-code p*) #t (set-empty) (set-empty) p*) |
| 1979 |
(new-params (cdr parms))))) |
| 1980 |
((null? parms) '()) |
| 1981 |
(else |
| 1982 |
(list (make-var |
| 1983 |
(source-code parms) |
| 1984 |
#t |
| 1985 |
(set-empty) |
| 1986 |
(set-empty) |
| 1987 |
parms))))) |
| 1988 |
(define (min-params parms) |
| 1989 |
(let loop ((l parms) (n 0)) |
| 1990 |
(if (pair? l) |
| 1991 |
(if (pair? (source-code (car l))) n (loop (cdr l) (+ n 1))) |
| 1992 |
n))) |
| 1993 |
(define (rest-param? parms) |
| 1994 |
(if (pair? parms) (rest-param? (cdr parms)) (not (null? parms)))) |
| 1995 |
(define (optionals parms source body env) |
| 1996 |
(if (pair? parms) |
| 1997 |
(let* ((parm* (car parms)) (parm (source-code parm*))) |
| 1998 |
(if (and (pair? parm) (length? parm 2)) |
| 1999 |
(let* ((var (car parm)) |
| 2000 |
(vars (new-variables (list var))) |
| 2001 |
(decl (env-declarations env))) |
| 2002 |
(new-call* |
| 2003 |
parm* |
| 2004 |
decl |
| 2005 |
(new-prc parm* |
| 2006 |
decl |
| 2007 |
#f |
| 2008 |
1 |
| 2009 |
#f |
| 2010 |
vars |
| 2011 |
(optionals |
| 2012 |
(cdr parms) |
| 2013 |
source |
| 2014 |
body |
| 2015 |
(env-frame env vars))) |
| 2016 |
(list (new-tst parm* |
| 2017 |
decl |
| 2018 |
(new-call* |
| 2019 |
parm* |
| 2020 |
decl |
| 2021 |
(new-ref-extended-bindings |
| 2022 |
parm* |
| 2023 |
**unassigned?-sym |
| 2024 |
env) |
| 2025 |
(list (new-ref parm* |
| 2026 |
decl |
| 2027 |
(env-lookup-var |
| 2028 |
env |
| 2029 |
(source-code var) |
| 2030 |
var)))) |
| 2031 |
(pt (cadr parm) env 'true) |
| 2032 |
(new-ref parm* |
| 2033 |
decl |
| 2034 |
(env-lookup-var |
| 2035 |
env |
| 2036 |
(source-code var) |
| 2037 |
var)))))) |
| 2038 |
(optionals (cdr parms) source body env))) |
| 2039 |
(pt-body source body env 'true))) |
| 2040 |
(if (eq? use 'none) |
| 2041 |
(new-cst source (env-declarations env) undef-object) |
| 2042 |
(let* ((parms (source->parms (cadr code))) (frame (new-params parms))) |
| 2043 |
(new-prc source |
| 2044 |
(env-declarations env) |
| 2045 |
#f |
| 2046 |
(min-params parms) |
| 2047 |
(rest-param? parms) |
| 2048 |
frame |
| 2049 |
(optionals |
| 2050 |
parms |
| 2051 |
source |
| 2052 |
(cddr code) |
| 2053 |
(env-frame env frame))))))) |
| 2054 |
(define (source->parms source) |
| 2055 |
(let ((x (source-code source))) (if (or (pair? x) (null? x)) x source))) |
| 2056 |
(define (pt-body source body env use) |
| 2057 |
(define (letrec-defines vars vals envs body env) |
| 2058 |
(cond ((null? body) |
| 2059 |
(pt-syntax-error |
| 2060 |
source |
| 2061 |
"Body must contain at least one evaluable expression")) |
| 2062 |
((macro-expr? (car body) env) |
| 2063 |
(letrec-defines |
| 2064 |
vars |
| 2065 |
vals |
| 2066 |
envs |
| 2067 |
(cons (macro-expand (car body) env) (cdr body)) |
| 2068 |
env)) |
| 2069 |
((begin-defs-expr? (car body)) |
| 2070 |
(letrec-defines |
| 2071 |
vars |
| 2072 |
vals |
| 2073 |
envs |
| 2074 |
(append (begin-defs-body (car body)) (cdr body)) |
| 2075 |
env)) |
| 2076 |
((include-expr? (car body)) |
| 2077 |
(if *ptree-port* (display " " *ptree-port*)) |
| 2078 |
(let ((x (file->sources* |
| 2079 |
(include-filename (car body)) |
| 2080 |
*ptree-port* |
| 2081 |
(source-locat (car body))))) |
| 2082 |
(if *ptree-port* (newline *ptree-port*)) |
| 2083 |
(letrec-defines vars vals envs (append x (cdr body)) env))) |
| 2084 |
((define-expr? (car body) env) |
| 2085 |
(let* ((var** (definition-variable (car body))) |
| 2086 |
(var* (source-code var**)) |
| 2087 |
(var (env-define-var env var* var**))) |
| 2088 |
(letrec-defines |
| 2089 |
(cons var vars) |
| 2090 |
(cons (definition-value (car body)) vals) |
| 2091 |
(cons env envs) |
| 2092 |
(cdr body) |
| 2093 |
env))) |
| 2094 |
((declare-expr? (car body)) |
| 2095 |
(letrec-defines |
| 2096 |
vars |
| 2097 |
vals |
| 2098 |
envs |
| 2099 |
(cdr body) |
| 2100 |
(add-declarations (car body) env))) |
| 2101 |
((define-macro-expr? (car body) env) |
| 2102 |
(letrec-defines |
| 2103 |
vars |
| 2104 |
vals |
| 2105 |
envs |
| 2106 |
(cdr body) |
| 2107 |
(add-macro (car body) env))) |
| 2108 |
((c-declaration-expr? (car body)) |
| 2109 |
(add-c-declaration (source-code (cadr (source-code (car body))))) |
| 2110 |
(letrec-defines vars vals envs (cdr body) env)) |
| 2111 |
((c-init-expr? (car body)) |
| 2112 |
(add-c-init (source-code (cadr (source-code (car body))))) |
| 2113 |
(letrec-defines vars vals envs (cdr body) env)) |
| 2114 |
((null? vars) (pt-sequence source body env use)) |
| 2115 |
(else |
| 2116 |
(let ((vars* (reverse vars))) |
| 2117 |
(let loop ((vals* '()) (l1 vals) (l2 envs)) |
| 2118 |
(if (not (null? l1)) |
| 2119 |
(loop (cons (pt (car l1) (car l2) 'true) vals*) |
| 2120 |
(cdr l1) |
| 2121 |
(cdr l2)) |
| 2122 |
(pt-recursive-let source vars* vals* body env use))))))) |
| 2123 |
(letrec-defines '() '() '() body (env-frame env '()))) |
| 2124 |
(define (pt-sequence source seq env use) |
| 2125 |
(if (length? seq 1) |
| 2126 |
(pt (car seq) env use) |
| 2127 |
(new-seq source |
| 2128 |
(env-declarations env) |
| 2129 |
(pt (car seq) env 'none) |
| 2130 |
(pt-sequence source (cdr seq) env use)))) |
| 2131 |
(define (pt-if source env use) |
| 2132 |
(let ((code (source-code source))) |
| 2133 |
(new-tst source |
| 2134 |
(env-declarations env) |
| 2135 |
(pt (cadr code) env 'pred) |
| 2136 |
(pt (caddr code) env use) |
| 2137 |
(if (length? code 3) |
| 2138 |
(new-cst source (env-declarations env) undef-object) |
| 2139 |
(pt (cadddr code) env use))))) |
| 2140 |
(define (pt-cond source env use) |
| 2141 |
(define (pt-clauses clauses) |
| 2142 |
(if (length? clauses 0) |
| 2143 |
(new-cst source (env-declarations env) undef-object) |
| 2144 |
(let* ((clause* (car clauses)) (clause (source-code clause*))) |
| 2145 |
(cond ((eq? (source-code (car clause)) else-sym) |
| 2146 |
(pt-sequence clause* (cdr clause) env use)) |
| 2147 |
((length? clause 1) |
| 2148 |
(new-disj |
| 2149 |
clause* |
| 2150 |
(env-declarations env) |
| 2151 |
(pt (car clause) env (if (eq? use 'true) 'true 'pred)) |
| 2152 |
(pt-clauses (cdr clauses)))) |
| 2153 |
((eq? (source-code (cadr clause)) =>-sym) |
| 2154 |
(new-disj-call |
| 2155 |
clause* |
| 2156 |
(env-declarations env) |
| 2157 |
(pt (car clause) env 'true) |
| 2158 |
(pt (caddr clause) env 'true) |
| 2159 |
(pt-clauses (cdr clauses)))) |
| 2160 |
(else |
| 2161 |
(new-tst clause* |
| 2162 |
(env-declarations env) |
| 2163 |
(pt (car clause) env 'pred) |
| 2164 |
(pt-sequence clause* (cdr clause) env use) |
| 2165 |
(pt-clauses (cdr clauses)))))))) |
| 2166 |
(pt-clauses (cdr (source-code source)))) |
| 2167 |
(define (pt-and source env use) |
| 2168 |
(define (pt-exprs exprs) |
| 2169 |
(cond ((length? exprs 0) (new-cst source (env-declarations env) #t)) |
| 2170 |
((length? exprs 1) (pt (car exprs) env use)) |
| 2171 |
(else |
| 2172 |
(new-conj |
| 2173 |
(car exprs) |
| 2174 |
(env-declarations env) |
| 2175 |
(pt (car exprs) env (if (eq? use 'true) 'true 'pred)) |
| 2176 |
(pt-exprs (cdr exprs)))))) |
| 2177 |
(pt-exprs (cdr (source-code source)))) |
| 2178 |
(define (pt-or source env use) |
| 2179 |
(define (pt-exprs exprs) |
| 2180 |
(cond ((length? exprs 0) |
| 2181 |
(new-cst source (env-declarations env) false-object)) |
| 2182 |
((length? exprs 1) (pt (car exprs) env use)) |
| 2183 |
(else |
| 2184 |
(new-disj |
| 2185 |
(car exprs) |
| 2186 |
(env-declarations env) |
| 2187 |
(pt (car exprs) env (if (eq? use 'true) 'true 'pred)) |
| 2188 |
(pt-exprs (cdr exprs)))))) |
| 2189 |
(pt-exprs (cdr (source-code source)))) |
| 2190 |
(define (pt-case source env use) |
| 2191 |
(let ((code (source-code source)) (temp (new-temps source '(temp)))) |
| 2192 |
(define (pt-clauses clauses) |
| 2193 |
(if (length? clauses 0) |
| 2194 |
(new-cst source (env-declarations env) undef-object) |
| 2195 |
(let* ((clause* (car clauses)) (clause (source-code clause*))) |
| 2196 |
(if (eq? (source-code (car clause)) else-sym) |
| 2197 |
(pt-sequence clause* (cdr clause) env use) |
| 2198 |
(new-tst clause* |
| 2199 |
(env-declarations env) |
| 2200 |
(new-call* |
| 2201 |
clause* |
| 2202 |
(add-not-safe (env-declarations env)) |
| 2203 |
(new-ref-extended-bindings |
| 2204 |
clause* |
| 2205 |
**case-memv-sym |
| 2206 |
env) |
| 2207 |
(list (new-ref clause* |
| 2208 |
(env-declarations env) |
| 2209 |
(car temp)) |
| 2210 |
(new-cst (car clause) |
| 2211 |
(env-declarations env) |
| 2212 |
(source->expression (car clause))))) |
| 2213 |
(pt-sequence clause* (cdr clause) env use) |
| 2214 |
(pt-clauses (cdr clauses))))))) |
| 2215 |
(new-call* |
| 2216 |
source |
| 2217 |
(env-declarations env) |
| 2218 |
(new-prc source |
| 2219 |
(env-declarations env) |
| 2220 |
#f |
| 2221 |
1 |
| 2222 |
#f |
| 2223 |
temp |
| 2224 |
(pt-clauses (cddr code))) |
| 2225 |
(list (pt (cadr code) env 'true))))) |
| 2226 |
(define (pt-let source env use) |
| 2227 |
(let ((code (source-code source))) |
| 2228 |
(if (bindable-var? (cadr code) env) |
| 2229 |
(let* ((self (new-variables (list (cadr code)))) |
| 2230 |
(bindings (map source-code (source-code (caddr code)))) |
| 2231 |
(vars (new-variables (map car bindings))) |
| 2232 |
(vals (map (lambda (x) (pt (cadr x) env 'true)) bindings)) |
| 2233 |
(env (env-frame (env-frame env vars) self)) |
| 2234 |
(self-proc |
| 2235 |
(list (new-prc source |
| 2236 |
(env-declarations env) |
| 2237 |
#f |
| 2238 |
(length vars) |
| 2239 |
#f |
| 2240 |
vars |
| 2241 |
(pt-body source (cdddr code) env use))))) |
| 2242 |
(set-prc-names! self self-proc) |
| 2243 |
(set-prc-names! vars vals) |
| 2244 |
(new-call* |
| 2245 |
source |
| 2246 |
(env-declarations env) |
| 2247 |
(new-prc source |
| 2248 |
(env-declarations env) |
| 2249 |
#f |
| 2250 |
1 |
| 2251 |
#f |
| 2252 |
self |
| 2253 |
(new-call* |
| 2254 |
source |
| 2255 |
(env-declarations env) |
| 2256 |
(new-ref source (env-declarations env) (car self)) |
| 2257 |
vals)) |
| 2258 |
self-proc)) |
| 2259 |
(if (null? (source-code (cadr code))) |
| 2260 |
(pt-body source (cddr code) env use) |
| 2261 |
(let* ((bindings (map source-code (source-code (cadr code)))) |
| 2262 |
(vars (new-variables (map car bindings))) |
| 2263 |
(vals (map (lambda (x) (pt (cadr x) env 'true)) bindings)) |
| 2264 |
(env (env-frame env vars))) |
| 2265 |
(set-prc-names! vars vals) |
| 2266 |
(new-call* |
| 2267 |
source |
| 2268 |
(env-declarations env) |
| 2269 |
(new-prc source |
| 2270 |
(env-declarations env) |
| 2271 |
#f |
| 2272 |
(length vars) |
| 2273 |
#f |
| 2274 |
vars |
| 2275 |
(pt-body source (cddr code) env use)) |
| 2276 |
vals)))))) |
| 2277 |
(define (pt-let* source env use) |
| 2278 |
(let ((code (source-code source))) |
| 2279 |
(define (pt-bindings bindings env use) |
| 2280 |
(if (null? bindings) |
| 2281 |
(pt-body source (cddr code) env use) |
| 2282 |
(let* ((binding* (car bindings)) |
| 2283 |
(binding (source-code binding*)) |
| 2284 |
(vars (new-variables (list (car binding)))) |
| 2285 |
(vals (list (pt (cadr binding) env 'true))) |
| 2286 |
(env (env-frame env vars))) |
| 2287 |
(set-prc-names! vars vals) |
| 2288 |
(new-call* |
| 2289 |
binding* |
| 2290 |
(env-declarations env) |
| 2291 |
(new-prc binding* |
| 2292 |
(env-declarations env) |
| 2293 |
#f |
| 2294 |
1 |
| 2295 |
#f |
| 2296 |
vars |
| 2297 |
(pt-bindings (cdr bindings) env use)) |
| 2298 |
vals)))) |
| 2299 |
(pt-bindings (source-code (cadr code)) env use))) |
| 2300 |
(define (pt-letrec source env use) |
| 2301 |
(let* ((code (source-code source)) |
| 2302 |
(bindings (map source-code (source-code (cadr code)))) |
| 2303 |
(vars* (new-variables (map car bindings))) |
| 2304 |
(env* (env-frame env vars*))) |
| 2305 |
(pt-recursive-let |
| 2306 |
source |
| 2307 |
vars* |
| 2308 |
(map (lambda (x) (pt (cadr x) env* 'true)) bindings) |
| 2309 |
(cddr code) |
| 2310 |
env* |
| 2311 |
use))) |
| 2312 |
(define (pt-recursive-let source vars vals body env use) |
| 2313 |
(define (dependency-graph vars vals) |
| 2314 |
(define (dgraph vars* vals*) |
| 2315 |
(if (null? vars*) |
| 2316 |
(set-empty) |
| 2317 |
(let ((var (car vars*)) (val (car vals*))) |
| 2318 |
(set-adjoin |
| 2319 |
(dgraph (cdr vars*) (cdr vals*)) |
| 2320 |
(make-gnode |
| 2321 |
var |
| 2322 |
(set-intersection (list->set vars) (free-variables val))))))) |
| 2323 |
(dgraph vars vals)) |
| 2324 |
(define (val-of var) |
| 2325 |
(list-ref vals (- (length vars) (length (memq var vars))))) |
| 2326 |
(define (bind-in-order order) |
| 2327 |
(if (null? order) |
| 2328 |
(pt-body source body env use) |
| 2329 |
(let* ((vars-set (car order)) (vars (set->list vars-set))) |
| 2330 |
(let loop1 ((l (reverse vars)) |
| 2331 |
(vars-b '()) |
| 2332 |
(vals-b '()) |
| 2333 |
(vars-a '())) |
| 2334 |
(if (not (null? l)) |
| 2335 |
(let* ((var (car l)) (val (val-of var))) |
| 2336 |
(if (or (prc? val) |
| 2337 |
(set-empty? |
| 2338 |
(set-intersection (free-variables val) vars-set))) |
| 2339 |
(loop1 (cdr l) |
| 2340 |
(cons var vars-b) |
| 2341 |
(cons val vals-b) |
| 2342 |
vars-a) |
| 2343 |
(loop1 (cdr l) vars-b vals-b (cons var vars-a)))) |
| 2344 |
(let* ((result1 (let loop2 ((l vars-a)) |
| 2345 |
(if (not (null? l)) |
| 2346 |
(let* ((var (car l)) (val (val-of var))) |
| 2347 |
(new-seq source |
| 2348 |
(env-declarations env) |
| 2349 |
(new-set source |
| 2350 |
(env-declarations |
| 2351 |
env) |
| 2352 |
var |
| 2353 |
val) |
| 2354 |
(loop2 (cdr l)))) |
| 2355 |
(bind-in-order (cdr order))))) |
| 2356 |
(result2 (if (null? vars-b) |
| 2357 |
result1 |
| 2358 |
(new-call* |
| 2359 |
source |
| 2360 |
(env-declarations env) |
| 2361 |
(new-prc source |
| 2362 |
(env-declarations env) |
| 2363 |
#f |
| 2364 |
(length vars-b) |
| 2365 |
#f |
| 2366 |
vars-b |
| 2367 |
result1) |
| 2368 |
vals-b))) |
| 2369 |
(result3 (if (null? vars-a) |
| 2370 |
result2 |
| 2371 |
(new-call* |
| 2372 |
source |
| 2373 |
(env-declarations env) |
| 2374 |
(new-prc source |
| 2375 |
(env-declarations env) |
| 2376 |
#f |
| 2377 |
(length vars-a) |
| 2378 |
#f |
| 2379 |
vars-a |
| 2380 |
result2) |
| 2381 |
(map (lambda (var) |
| 2382 |
(new-cst source |
| 2383 |
(env-declarations env) |
| 2384 |
undef-object)) |
| 2385 |
vars-a))))) |
| 2386 |
result3)))))) |
| 2387 |
(set-prc-names! vars vals) |
| 2388 |
(bind-in-order |
| 2389 |
(topological-sort (transitive-closure (dependency-graph vars vals))))) |
| 2390 |
(define (pt-begin source env use) |
| 2391 |
(pt-sequence source (cdr (source-code source)) env use)) |
| 2392 |
(define (pt-do source env use) |
| 2393 |
(let* ((code (source-code source)) |
| 2394 |
(loop (new-temps source '(loop))) |
| 2395 |
(bindings (map source-code (source-code (cadr code)))) |
| 2396 |
(vars (new-variables (map car bindings))) |
| 2397 |
(init (map (lambda (x) (pt (cadr x) env 'true)) bindings)) |
| 2398 |
(env (env-frame env vars)) |
| 2399 |
(step (map (lambda (x) |
| 2400 |
(pt (if (length? x 2) (car x) (caddr x)) env 'true)) |
| 2401 |
bindings)) |
| 2402 |
(exit (source-code (caddr code)))) |
| 2403 |
(set-prc-names! vars init) |
| 2404 |
(new-call* |
| 2405 |
source |
| 2406 |
(env-declarations env) |
| 2407 |
(new-prc source |
| 2408 |
(env-declarations env) |
| 2409 |
#f |
| 2410 |
1 |
| 2411 |
#f |
| 2412 |
loop |
| 2413 |
(new-call* |
| 2414 |
source |
| 2415 |
(env-declarations env) |
| 2416 |
(new-ref source (env-declarations env) (car loop)) |
| 2417 |
init)) |
| 2418 |
(list (new-prc source |
| 2419 |
(env-declarations env) |
| 2420 |
#f |
| 2421 |
(length vars) |
| 2422 |
#f |
| 2423 |
vars |
| 2424 |
(new-tst source |
| 2425 |
(env-declarations env) |
| 2426 |
(pt (car exit) env 'pred) |
| 2427 |
(if (length? exit 1) |
| 2428 |
(new-cst (caddr code) |
| 2429 |
(env-declarations env) |
| 2430 |
undef-object) |
| 2431 |
(pt-sequence (caddr code) (cdr exit) env use)) |
| 2432 |
(if (length? code 3) |
| 2433 |
(new-call* |
| 2434 |
source |
| 2435 |
(env-declarations env) |
| 2436 |
(new-ref source |
| 2437 |
(env-declarations env) |
| 2438 |
(car loop)) |
| 2439 |
step) |
| 2440 |
(new-seq source |
| 2441 |
(env-declarations env) |
| 2442 |
(pt-sequence |
| 2443 |
source |
| 2444 |
(cdddr code) |
| 2445 |
env |
| 2446 |
'none) |
| 2447 |
(new-call* |
| 2448 |
source |
| 2449 |
(env-declarations env) |
| 2450 |
(new-ref source |
| 2451 |
(env-declarations env) |
| 2452 |
(car loop)) |
| 2453 |
step))))))))) |
| 2454 |
(define (pt-combination source env use) |
| 2455 |
(let* ((code (source-code source)) |
| 2456 |
(oper (pt (car code) env 'true)) |
| 2457 |
(decl (node-decl oper))) |
| 2458 |
(new-call* |
| 2459 |
source |
| 2460 |
(env-declarations env) |
| 2461 |
oper |
| 2462 |
(map (lambda (x) (pt x env 'true)) (cdr code))))) |
| 2463 |
(define (pt-delay source env use) |
| 2464 |
(let ((code (source-code source))) |
| 2465 |
(new-call* |
| 2466 |
source |
| 2467 |
(add-not-safe (env-declarations env)) |
| 2468 |
(new-ref-extended-bindings source **make-placeholder-sym env) |
| 2469 |
(list (new-prc source |
| 2470 |
(env-declarations env) |
| 2471 |
#f |
| 2472 |
0 |
| 2473 |
#f |
| 2474 |
'() |
| 2475 |
(pt (cadr code) env 'true)))))) |
| 2476 |
(define (pt-future source env use) |
| 2477 |
(let ((decl (env-declarations env)) (code (source-code source))) |
| 2478 |
(new-fut source decl (pt (cadr code) env 'true)))) |
| 2479 |
(define (self-eval-expr? source) |
| 2480 |
(let ((code (source-code source))) |
| 2481 |
(and (not (pair? code)) (not (symbol-object? code))))) |
| 2482 |
(define (quote-expr? source) (mymatch quote-sym 1 source)) |
| 2483 |
(define (quasiquote-expr? source) (mymatch quasiquote-sym 1 source)) |
| 2484 |
(define (unquote-expr? source) (mymatch unquote-sym 1 source)) |
| 2485 |
(define (unquote-splicing-expr? source) |
| 2486 |
(mymatch unquote-splicing-sym 1 source)) |
| 2487 |
(define (var-expr? source env) |
| 2488 |
(let ((code (source-code source))) |
| 2489 |
(and (symbol-object? code) |
| 2490 |
(not-keyword source env code) |
| 2491 |
(not-macro source env code)))) |
| 2492 |
(define (not-macro source env name) |
| 2493 |
(if (env-lookup-macro env name) |
| 2494 |
(pt-syntax-error source "Macro name can't be used as a variable:" name) |
| 2495 |
#t)) |
| 2496 |
(define (bindable-var? source env) |
| 2497 |
(let ((code (source-code source))) |
| 2498 |
(and (symbol-object? code) (not-keyword source env code)))) |
| 2499 |
(define (not-keyword source env name) |
| 2500 |
(if (or (memq name common-keywords) |
| 2501 |
(memq name |
| 2502 |
(dialect-specific-keywords |
| 2503 |
(scheme-dialect (env-declarations env))))) |
| 2504 |
(pt-syntax-error |
| 2505 |
source |
| 2506 |
"Predefined keyword can't be used as a variable:" |
| 2507 |
name) |
| 2508 |
#t)) |
| 2509 |
(define (set!-expr? source env) |
| 2510 |
(and (mymatch set!-sym 2 source) |
| 2511 |
(var-expr? (cadr (source-code source)) env))) |
| 2512 |
(define (lambda-expr? source env) |
| 2513 |
(and (mymatch lambda-sym -2 source) |
| 2514 |
(proper-parms? (source->parms (cadr (source-code source))) env))) |
| 2515 |
(define (if-expr? source) |
| 2516 |
(and (mymatch if-sym -2 source) |
| 2517 |
(or (<= (length (source-code source)) 4) |
| 2518 |
(pt-syntax-error source "Ill-formed special form" if-sym)))) |
| 2519 |
(define (cond-expr? source) |
| 2520 |
(and (mymatch cond-sym -1 source) (proper-clauses? source))) |
| 2521 |
(define (and-expr? source) (mymatch and-sym 0 source)) |
| 2522 |
(define (or-expr? source) (mymatch or-sym 0 source)) |
| 2523 |
(define (case-expr? source) |
| 2524 |
(and (mymatch case-sym -2 source) (proper-case-clauses? source))) |
| 2525 |
(define (let-expr? source env) |
| 2526 |
(and (mymatch let-sym -2 source) |
| 2527 |
(let ((code (source-code source))) |
| 2528 |
(if (bindable-var? (cadr code) env) |
| 2529 |
(and (proper-bindings? (caddr code) #t env) |
| 2530 |
(or (> (length code) 3) |
| 2531 |
(pt-syntax-error source "Ill-formed named 'let'"))) |
| 2532 |
(proper-bindings? (cadr code) #t env))))) |
| 2533 |
(define (let*-expr? source env) |
| 2534 |
(and (mymatch let*-sym -2 source) |
| 2535 |
(proper-bindings? (cadr (source-code source)) #f env))) |
| 2536 |
(define (letrec-expr? source env) |
| 2537 |
(and (mymatch letrec-sym -2 source) |
| 2538 |
(proper-bindings? (cadr (source-code source)) #t env))) |
| 2539 |
(define (begin-expr? source) (mymatch begin-sym -1 source)) |
| 2540 |
(define (do-expr? source env) |
| 2541 |
(and (mymatch do-sym -2 source) |
| 2542 |
(proper-do-bindings? source env) |
| 2543 |
(proper-do-exit? source))) |
| 2544 |
(define (define-expr? source env) |
| 2545 |
(and (mymatch define-sym -1 source) |
| 2546 |
(proper-definition? source env) |
| 2547 |
(let ((v (definition-variable source))) |
| 2548 |
(not-macro v env (source-code v))))) |
| 2549 |
(define (combination-expr? source) |
| 2550 |
(let ((length (proper-length (source-code source)))) |
| 2551 |
(if length |
| 2552 |
(or (> length 0) (pt-syntax-error source "Ill-formed procedure call")) |
| 2553 |
(pt-syntax-error source "Ill-terminated procedure call")))) |
| 2554 |
(define (delay-expr? source env) |
| 2555 |
(and (not (eq? (scheme-dialect (env-declarations env)) ieee-scheme-sym)) |
| 2556 |
(mymatch delay-sym 1 source))) |
| 2557 |
(define (future-expr? source env) |
| 2558 |
(and (eq? (scheme-dialect (env-declarations env)) multilisp-sym) |
| 2559 |
(mymatch future-sym 1 source))) |
| 2560 |
(define (macro-expr? source env) |
| 2561 |
(let ((code (source-code source))) |
| 2562 |
(and (pair? code) |
| 2563 |
(symbol-object? (source-code (car code))) |
| 2564 |
(let ((macr (env-lookup-macro env (source-code (car code))))) |
| 2565 |
(and macr |
| 2566 |
(let ((len (proper-length (cdr code)))) |
| 2567 |
(if len |
| 2568 |
(let ((len* (+ len 1)) (size (car macr))) |
| 2569 |
(or (if (> size 0) (= len* size) (>= len* (- size))) |
| 2570 |
(pt-syntax-error source "Ill-formed macro form"))) |
| 2571 |
(pt-syntax-error |
| 2572 |
source |
| 2573 |
"Ill-terminated macro form")))))))) |
| 2574 |
(define (define-macro-expr? source env) |
| 2575 |
(and (mymatch **define-macro-sym -1 source) (proper-definition? source env))) |
| 2576 |
(define (declare-expr? source) (mymatch **declare-sym -1 source)) |
| 2577 |
(define (include-expr? source) (mymatch **include-sym 1 source)) |
| 2578 |
(define (begin-defs-expr? source) (mymatch begin-sym 0 source)) |
| 2579 |
(define (mymatch keyword size source) |
| 2580 |
(let ((code (source-code source))) |
| 2581 |
(and (pair? code) |
| 2582 |
(eq? (source-code (car code)) keyword) |
| 2583 |
(let ((length (proper-length (cdr code)))) |
| 2584 |
(if length |
| 2585 |
(or (if (> size 0) (= length size) (>= length (- size))) |
| 2586 |
(pt-syntax-error source "Ill-formed special form" keyword)) |
| 2587 |
(pt-syntax-error |
| 2588 |
source |
| 2589 |
"Ill-terminated special form" |
| 2590 |
keyword)))))) |
| 2591 |
(define (proper-length l) |
| 2592 |
(define (length l n) |
| 2593 |
(cond ((pair? l) (length (cdr l) (+ n 1))) ((null? l) n) (else #f))) |
| 2594 |
(length l 0)) |
| 2595 |
(define (proper-definition? source env) |
| 2596 |
(let* ((code (source-code source)) |
| 2597 |
(pattern* (cadr code)) |
| 2598 |
(pattern (source-code pattern*)) |
| 2599 |
(body (cddr code))) |
| 2600 |
(cond ((bindable-var? pattern* env) |
| 2601 |
(cond ((length? body 0) #t) |
| 2602 |
((length? body 1) #t) |
| 2603 |
(else (pt-syntax-error source "Ill-formed definition body")))) |
| 2604 |
((pair? pattern) |
| 2605 |
(if (length? body 0) |
| 2606 |
(pt-syntax-error |
| 2607 |
source |
| 2608 |
"Body of a definition must have at least one expression")) |
| 2609 |
(if (bindable-var? (car pattern) env) |
| 2610 |
(proper-parms? (cdr pattern) env) |
| 2611 |
(pt-syntax-error |
| 2612 |
(car pattern) |
| 2613 |
"Procedure name must be an identifier"))) |
| 2614 |
(else (pt-syntax-error pattern* "Ill-formed definition pattern"))))) |
| 2615 |
(define (definition-variable def) |
| 2616 |
(let* ((code (source-code def)) (pattern (cadr code))) |
| 2617 |
(if (pair? (source-code pattern)) (car (source-code pattern)) pattern))) |
| 2618 |
(define (definition-value def) |
| 2619 |
(let ((code (source-code def)) (loc (source-locat def))) |
| 2620 |
(cond ((pair? (source-code (cadr code))) |
| 2621 |
(make-source |
| 2622 |
(cons (make-source lambda-sym loc) |
| 2623 |
(cons (parms->source (cdr (source-code (cadr code))) loc) |
| 2624 |
(cddr code))) |
| 2625 |
loc)) |
| 2626 |
((null? (cddr code)) |
| 2627 |
(make-source |
| 2628 |
(list (make-source quote-sym loc) (make-source undef-object loc)) |
| 2629 |
loc)) |
| 2630 |
(else (caddr code))))) |
| 2631 |
(define (parms->source parms loc) |
| 2632 |
(if (or (pair? parms) (null? parms)) (make-source parms loc) parms)) |
| 2633 |
(define (proper-parms? parms env) |
| 2634 |
(define (proper-parms parms seen optional-seen) |
| 2635 |
(cond ((pair? parms) |
| 2636 |
(let* ((parm* (car parms)) (parm (source-code parm*))) |
| 2637 |
(cond ((pair? parm) |
| 2638 |
(if (eq? (scheme-dialect (env-declarations env)) |
| 2639 |
multilisp-sym) |
| 2640 |
(let ((length (proper-length parm))) |
| 2641 |
(if (or (eqv? length 1) (eqv? length 2)) |
| 2642 |
(let ((var (car parm))) |
| 2643 |
(if (bindable-var? var env) |
| 2644 |
(if (memq (source-code var) seen) |
| 2645 |
(pt-syntax-error |
| 2646 |
var |
| 2647 |
"Duplicate parameter in parameter list") |
| 2648 |
(proper-parms |
| 2649 |
(cdr parms) |
| 2650 |
(cons (source-code var) seen) |
| 2651 |
#t)) |
| 2652 |
(pt-syntax-error |
| 2653 |
var |
| 2654 |
"Parameter must be an identifier"))) |
| 2655 |
(pt-syntax-error |
| 2656 |
parm* |
| 2657 |
"Ill-formed optional parameter"))) |
| 2658 |
(pt-syntax-error |
| 2659 |
parm* |
| 2660 |
"optional parameters illegal in this dialect"))) |
| 2661 |
(optional-seen |
| 2662 |
(pt-syntax-error parm* "Optional parameter expected")) |
| 2663 |
((bindable-var? parm* env) |
| 2664 |
(if (memq parm seen) |
| 2665 |
(pt-syntax-error |
| 2666 |
parm* |
| 2667 |
"Duplicate parameter in parameter list")) |
| 2668 |
(proper-parms (cdr parms) (cons parm seen) #f)) |
| 2669 |
(else |
| 2670 |
(pt-syntax-error |
| 2671 |
parm* |
| 2672 |
"Parameter must be an identifier"))))) |
| 2673 |
((null? parms) #t) |
| 2674 |
((bindable-var? parms env) |
| 2675 |
(if (memq (source-code parms) seen) |
| 2676 |
(pt-syntax-error parms "Duplicate parameter in parameter list") |
| 2677 |
#t)) |
| 2678 |
(else |
| 2679 |
(pt-syntax-error parms "Rest parameter must be an identifier")))) |
| 2680 |
(proper-parms parms '() #f)) |
| 2681 |
(define (proper-clauses? source) |
| 2682 |
(define (proper-clauses clauses) |
| 2683 |
(or (null? clauses) |
| 2684 |
(let* ((clause* (car clauses)) |
| 2685 |
(clause (source-code clause*)) |
| 2686 |
(length (proper-length clause))) |
| 2687 |
(if length |
| 2688 |
(if (>= length 1) |
| 2689 |
(if (eq? (source-code (car clause)) else-sym) |
| 2690 |
(cond ((= length 1) |
| 2691 |
(pt-syntax-error |
| 2692 |
clause* |
| 2693 |
"Else clause must have a body")) |
| 2694 |
((not (null? (cdr clauses))) |
| 2695 |
(pt-syntax-error |
| 2696 |
clause* |
| 2697 |
"Else clause must be the last clause")) |
| 2698 |
(else (proper-clauses (cdr clauses)))) |
| 2699 |
(if (and (>= length 2) |
| 2700 |
(eq? (source-code (cadr clause)) =>-sym) |
| 2701 |
(not (= length 3))) |
| 2702 |
(pt-syntax-error |
| 2703 |
(cadr clause) |
| 2704 |
"'=>' must be followed by a single expression") |
| 2705 |
(proper-clauses (cdr clauses)))) |
| 2706 |
(pt-syntax-error clause* "Ill-formed 'cond' clause")) |
| 2707 |
(pt-syntax-error clause* "Ill-terminated 'cond' clause"))))) |
| 2708 |
(proper-clauses (cdr (source-code source)))) |
| 2709 |
(define (proper-case-clauses? source) |
| 2710 |
(define (proper-case-clauses clauses) |
| 2711 |
(or (null? clauses) |
| 2712 |
(let* ((clause* (car clauses)) |
| 2713 |
(clause (source-code clause*)) |
| 2714 |
(length (proper-length clause))) |
| 2715 |
(if length |
| 2716 |
(if (>= length 2) |
| 2717 |
(if (eq? (source-code (car clause)) else-sym) |
| 2718 |
(if (not (null? (cdr clauses))) |
| 2719 |
(pt-syntax-error |
| 2720 |
clause* |
| 2721 |
"Else clause must be the last clause") |
| 2722 |
(proper-case-clauses (cdr clauses))) |
| 2723 |
(begin |
| 2724 |
(proper-selector-list? (car clause)) |
| 2725 |
(proper-case-clauses (cdr clauses)))) |
| 2726 |
(pt-syntax-error |
| 2727 |
clause* |
| 2728 |
"A 'case' clause must have a selector list and a body")) |
| 2729 |
(pt-syntax-error clause* "Ill-terminated 'case' clause"))))) |
| 2730 |
(proper-case-clauses (cddr (source-code source)))) |
| 2731 |
(define (proper-selector-list? source) |
| 2732 |
(let* ((code (source-code source)) (length (proper-length code))) |
| 2733 |
(if length |
| 2734 |
(or (>= length 1) |
| 2735 |
(pt-syntax-error |
| 2736 |
source |
| 2737 |
"Selector list must contain at least one element")) |
| 2738 |
(pt-syntax-error source "Ill-terminated selector list")))) |
| 2739 |
(define (proper-bindings? bindings check-dupl? env) |
| 2740 |
(define (proper-bindings l seen) |
| 2741 |
(cond ((pair? l) |
| 2742 |
(let* ((binding* (car l)) (binding (source-code binding*))) |
| 2743 |
(if (eqv? (proper-length binding) 2) |
| 2744 |
(let ((var (car binding))) |
| 2745 |
(if (bindable-var? var env) |
| 2746 |
(if (and check-dupl? (memq (source-code var) seen)) |
| 2747 |
(pt-syntax-error |
| 2748 |
var |
| 2749 |
"Duplicate variable in bindings") |
| 2750 |
(proper-bindings |
| 2751 |
(cdr l) |
| 2752 |
(cons (source-code var) seen))) |
| 2753 |
(pt-syntax-error |
| 2754 |
var |
| 2755 |
"Binding variable must be an identifier"))) |
| 2756 |
(pt-syntax-error binding* "Ill-formed binding")))) |
| 2757 |
((null? l) #t) |
| 2758 |
(else (pt-syntax-error bindings "Ill-terminated binding list")))) |
| 2759 |
(proper-bindings (source-code bindings) '())) |
| 2760 |
(define (proper-do-bindings? source env) |
| 2761 |
(let ((bindings (cadr (source-code source)))) |
| 2762 |
(define (proper-bindings l seen) |
| 2763 |
(cond ((pair? l) |
| 2764 |
(let* ((binding* (car l)) |
| 2765 |
(binding (source-code binding*)) |
| 2766 |
(length (proper-length binding))) |
| 2767 |
(if (or (eqv? length 2) (eqv? length 3)) |
| 2768 |
(let ((var (car binding))) |
| 2769 |
(if (bindable-var? var env) |
| 2770 |
(if (memq (source-code var) seen) |
| 2771 |
(pt-syntax-error |
| 2772 |
var |
| 2773 |
"Duplicate variable in bindings") |
| 2774 |
(proper-bindings |
| 2775 |
(cdr l) |
| 2776 |
(cons (source-code var) seen))) |
| 2777 |
(pt-syntax-error |
| 2778 |
var |
| 2779 |
"Binding variable must be an identifier"))) |
| 2780 |
(pt-syntax-error binding* "Ill-formed binding")))) |
| 2781 |
((null? l) #t) |
| 2782 |
(else (pt-syntax-error bindings "Ill-terminated binding list")))) |
| 2783 |
(proper-bindings (source-code bindings) '()))) |
| 2784 |
(define (proper-do-exit? source) |
| 2785 |
(let* ((code (source-code (caddr (source-code source)))) |
| 2786 |
(length (proper-length code))) |
| 2787 |
(if length |
| 2788 |
(or (> length 0) (pt-syntax-error source "Ill-formed exit clause")) |
| 2789 |
(pt-syntax-error source "Ill-terminated exit clause")))) |
| 2790 |
(define (include-filename source) (source-code (cadr (source-code source)))) |
| 2791 |
(define (begin-defs-body source) (cdr (source-code source))) |
| 2792 |
(define (length? l n) |
| 2793 |
(cond ((null? l) (= n 0)) ((> n 0) (length? (cdr l) (- n 1))) (else #f))) |
| 2794 |
(define (transform-declaration source) |
| 2795 |
(let ((code (source-code source))) |
| 2796 |
(if (not (pair? code)) |
| 2797 |
(pt-syntax-error source "Ill-formed declaration") |
| 2798 |
(let* ((pos (not (eq? (source-code (car code)) not-sym))) |
| 2799 |
(x (if pos code (cdr code)))) |
| 2800 |
(if (not (pair? x)) |
| 2801 |
(pt-syntax-error source "Ill-formed declaration") |
| 2802 |
(let* ((id* (car x)) (id (source-code id*))) |
| 2803 |
(cond ((not (symbol-object? id)) |
| 2804 |
(pt-syntax-error |
| 2805 |
id* |
| 2806 |
"Declaration name must be an identifier")) |
| 2807 |
((assq id flag-declarations) |
| 2808 |
(cond ((not pos) |
| 2809 |
(pt-syntax-error |
| 2810 |
id* |
| 2811 |
"Declaration can't be negated")) |
| 2812 |
((null? (cdr x)) |
| 2813 |
(flag-decl |
| 2814 |
source |
| 2815 |
(cdr (assq id flag-declarations)) |
| 2816 |
id)) |
| 2817 |
(else |
| 2818 |
(pt-syntax-error |
| 2819 |
source |
| 2820 |
"Ill-formed declaration")))) |
| 2821 |
((memq id parameterized-declarations) |
| 2822 |
(cond ((not pos) |
| 2823 |
(pt-syntax-error |
| 2824 |
id* |
| 2825 |
"Declaration can't be negated")) |
| 2826 |
((eqv? (proper-length x) 2) |
| 2827 |
(parameterized-decl |
| 2828 |
source |
| 2829 |
id |
| 2830 |
(source->expression (cadr x)))) |
| 2831 |
(else |
| 2832 |
(pt-syntax-error |
| 2833 |
source |
| 2834 |
"Ill-formed declaration")))) |
| 2835 |
((memq id boolean-declarations) |
| 2836 |
(if (null? (cdr x)) |
| 2837 |
(boolean-decl source id pos) |
| 2838 |
(pt-syntax-error source "Ill-formed declaration"))) |
| 2839 |
((assq id namable-declarations) |
| 2840 |
(cond ((not pos) |
| 2841 |
(pt-syntax-error |
| 2842 |
id* |
| 2843 |
"Declaration can't be negated")) |
| 2844 |
(else |
| 2845 |
(namable-decl |
| 2846 |
source |
| 2847 |
(cdr (assq id namable-declarations)) |
| 2848 |
id |
| 2849 |
(map source->expression (cdr x)))))) |
| 2850 |
((memq id namable-boolean-declarations) |
| 2851 |
(namable-boolean-decl |
| 2852 |
source |
| 2853 |
id |
| 2854 |
pos |
| 2855 |
(map source->expression (cdr x)))) |
| 2856 |
((memq id namable-string-declarations) |
| 2857 |
(if (not (pair? (cdr x))) |
| 2858 |
(pt-syntax-error source "Ill-formed declaration") |
| 2859 |
(let* ((str* (cadr x)) (str (source-code str*))) |
| 2860 |
(cond ((not pos) |
| 2861 |
(pt-syntax-error |
| 2862 |
id* |
| 2863 |
"Declaration can't be negated")) |
| 2864 |
((not (string? str)) |
| 2865 |
(pt-syntax-error str* "String expected")) |
| 2866 |
(else |
| 2867 |
(namable-string-decl |
| 2868 |
source |
| 2869 |
id |
| 2870 |
str |
| 2871 |
(map source->expression (cddr x)))))))) |
| 2872 |
(else (pt-syntax-error id* "Unknown declaration"))))))))) |
| 2873 |
(define (add-declarations source env) |
| 2874 |
(let loop ((l (cdr (source-code source))) (env env)) |
| 2875 |
(if (pair? l) |
| 2876 |
(loop (cdr l) (env-declare env (transform-declaration (car l)))) |
| 2877 |
env))) |
| 2878 |
(define (add-decl d decl) (env-declare decl d)) |
| 2879 |
(define (add-macro source env) |
| 2880 |
(define (form-size parms) |
| 2881 |
(let loop ((l parms) (n 1)) |
| 2882 |
(if (pair? l) (loop (cdr l) (+ n 1)) (if (null? l) n (- n))))) |
| 2883 |
(define (error-proc . msgs) |
| 2884 |
(apply compiler-user-error |
| 2885 |
(cons (source-locat source) (cons "(in macro body)" msgs)))) |
| 2886 |
(let ((var (definition-variable source)) (proc (definition-value source))) |
| 2887 |
(if (lambda-expr? proc env) |
| 2888 |
(env-macro |
| 2889 |
env |
| 2890 |
(source-code var) |
| 2891 |
(cons (form-size (source->parms (cadr (source-code proc)))) |
| 2892 |
(scheme-global-eval (source->expression proc) error-proc))) |
| 2893 |
(pt-syntax-error source "Macro value must be a lambda expression")))) |
| 2894 |
(define (ptree.begin! info-port) (set! *ptree-port* info-port) '()) |
| 2895 |
(define (ptree.end!) '()) |
| 2896 |
(define *ptree-port* '()) |
| 2897 |
(define (normalize-parse-tree ptree env) |
| 2898 |
(define (normalize ptree) |
| 2899 |
(let ((tree (assignment-convert (partial-evaluate ptree) env))) |
| 2900 |
(lambda-lift! tree) |
| 2901 |
tree)) |
| 2902 |
(if (def? ptree) |
| 2903 |
(begin |
| 2904 |
(node-children-set! ptree (list (normalize (def-val ptree)))) |
| 2905 |
ptree) |
| 2906 |
(normalize ptree))) |
| 2907 |
(define (partial-evaluate ptree) (pe ptree '())) |
| 2908 |
(define (pe ptree consts) |
| 2909 |
(cond ((cst? ptree) |
| 2910 |
(new-cst (node-source ptree) (node-decl ptree) (cst-val ptree))) |
| 2911 |
((ref? ptree) |
| 2912 |
(let ((var (ref-var ptree))) |
| 2913 |
(var-refs-set! var (set-remove (var-refs var) ptree)) |
| 2914 |
(let ((x (assq var consts))) |
| 2915 |
(if x |
| 2916 |
(new-cst (node-source ptree) (node-decl ptree) (cdr x)) |
| 2917 |
(let ((y (global-val var))) |
| 2918 |
(if (and y (cst? y)) |
| 2919 |
(new-cst (node-source ptree) |
| 2920 |
(node-decl ptree) |
| 2921 |
(cst-val y)) |
| 2922 |
(new-ref (node-source ptree) |
| 2923 |
(node-decl ptree) |
| 2924 |
var))))))) |
| 2925 |
((set? ptree) |
| 2926 |
(let ((var (set-var ptree)) (val (pe (set-val ptree) consts))) |
| 2927 |
(var-sets-set! var (set-remove (var-sets var) ptree)) |
| 2928 |
(new-set (node-source ptree) (node-decl ptree) var val))) |
| 2929 |
((tst? ptree) |
| 2930 |
(let ((pre (pe (tst-pre ptree) consts))) |
| 2931 |
(if (cst? pre) |
| 2932 |
(let ((val (cst-val pre))) |
| 2933 |
(if (false-object? val) |
| 2934 |
(pe (tst-alt ptree) consts) |
| 2935 |
(pe (tst-con ptree) consts))) |
| 2936 |
(new-tst (node-source ptree) |
| 2937 |
(node-decl ptree) |
| 2938 |
pre |
| 2939 |
(pe (tst-con ptree) consts) |
| 2940 |
(pe (tst-alt ptree) consts))))) |
| 2941 |
((conj? ptree) |
| 2942 |
(let ((pre (pe (conj-pre ptree) consts))) |
| 2943 |
(if (cst? pre) |
| 2944 |
(let ((val (cst-val pre))) |
| 2945 |
(if (false-object? val) pre (pe (conj-alt ptree) consts))) |
| 2946 |
(new-conj |
| 2947 |
(node-source ptree) |
| 2948 |
(node-decl ptree) |
| 2949 |
pre |
| 2950 |
(pe (conj-alt ptree) consts))))) |
| 2951 |
((disj? ptree) |
| 2952 |
(let ((pre (pe (disj-pre ptree) consts))) |
| 2953 |
(if (cst? pre) |
| 2954 |
(let ((val (cst-val pre))) |
| 2955 |
(if (false-object? val) (pe (disj-alt ptree) consts) pre)) |
| 2956 |
(new-disj |
| 2957 |
(node-source ptree) |
| 2958 |
(node-decl ptree) |
| 2959 |
pre |
| 2960 |
(pe (disj-alt ptree) consts))))) |
| 2961 |
((prc? ptree) |
| 2962 |
(new-prc (node-source ptree) |
| 2963 |
(node-decl ptree) |
| 2964 |
(prc-name ptree) |
| 2965 |
(prc-min ptree) |
| 2966 |
(prc-rest ptree) |
| 2967 |
(prc-parms ptree) |
| 2968 |
(pe (prc-body ptree) consts))) |
| 2969 |
((app? ptree) |
| 2970 |
(let ((oper (app-oper ptree)) (args (app-args ptree))) |
| 2971 |
(if (and (prc? oper) |
| 2972 |
(not (prc-rest oper)) |
| 2973 |
(= (length (prc-parms oper)) (length args))) |
| 2974 |
(pe-let ptree consts) |
| 2975 |
(new-call |
| 2976 |
(node-source ptree) |
| 2977 |
(node-decl ptree) |
| 2978 |
(pe oper consts) |
| 2979 |
(map (lambda (x) (pe x consts)) args))))) |
| 2980 |
((fut? ptree) |
| 2981 |
(new-fut (node-source ptree) |
| 2982 |
(node-decl ptree) |
| 2983 |
(pe (fut-val ptree) consts))) |
| 2984 |
(else (compiler-internal-error "pe, unknown parse tree node type")))) |
| 2985 |
(define (pe-let ptree consts) |
| 2986 |
(let* ((proc (app-oper ptree)) |
| 2987 |
(vals (app-args ptree)) |
| 2988 |
(vars (prc-parms proc)) |
| 2989 |
(non-mut-vars (set-keep not-mutable? (list->set vars)))) |
| 2990 |
(for-each |
| 2991 |
(lambda (var) |
| 2992 |
(var-refs-set! var (set-empty)) |
| 2993 |
(var-sets-set! var (set-empty))) |
| 2994 |
vars) |
| 2995 |
(let loop ((l vars) |
| 2996 |
(v vals) |
| 2997 |
(new-vars '()) |
| 2998 |
(new-vals '()) |
| 2999 |
(new-consts consts)) |
| 3000 |
(if (null? l) |
| 3001 |
(if (null? new-vars) |
| 3002 |
(pe (prc-body proc) new-consts) |
| 3003 |
(new-call |
| 3004 |
(node-source ptree) |
| 3005 |
(node-decl ptree) |
| 3006 |
(new-prc (node-source proc) |
| 3007 |
(node-decl proc) |
| 3008 |
#f |
| 3009 |
(length new-vars) |
| 3010 |
#f |
| 3011 |
(reverse new-vars) |
| 3012 |
(pe (prc-body proc) new-consts)) |
| 3013 |
(reverse new-vals))) |
| 3014 |
(let ((var (car l)) (val (pe (car v) consts))) |
| 3015 |
(if (and (set-member? var non-mut-vars) (cst? val)) |
| 3016 |
(loop (cdr l) |
| 3017 |
(cdr v) |
| 3018 |
new-vars |
| 3019 |
new-vals |
| 3020 |
(cons (cons var (cst-val val)) new-consts)) |
| 3021 |
(loop (cdr l) |
| 3022 |
(cdr v) |
| 3023 |
(cons var new-vars) |
| 3024 |
(cons val new-vals) |
| 3025 |
new-consts))))))) |
| 3026 |
(define (assignment-convert ptree env) |
| 3027 |
(ac ptree (env-declare env (list safe-sym #f)) '())) |
| 3028 |
(define (ac ptree env mut) |
| 3029 |
(cond ((cst? ptree) ptree) |
| 3030 |
((ref? ptree) |
| 3031 |
(let ((var (ref-var ptree))) |
| 3032 |
(if (global? var) |
| 3033 |
ptree |
| 3034 |
(let ((x (assq var mut))) |
| 3035 |
(if x |
| 3036 |
(let ((source (node-source ptree))) |
| 3037 |
(var-refs-set! var (set-remove (var-refs var) ptree)) |
| 3038 |
(new-call |
| 3039 |
source |
| 3040 |
(node-decl ptree) |
| 3041 |
(new-ref-extended-bindings source **cell-ref-sym env) |
| 3042 |
(list (new-ref source (node-decl ptree) (cdr x))))) |
| 3043 |
ptree))))) |
| 3044 |
((set? ptree) |
| 3045 |
(let ((var (set-var ptree)) |
| 3046 |
(source (node-source ptree)) |
| 3047 |
(val (ac (set-val ptree) env mut))) |
| 3048 |
(var-sets-set! var (set-remove (var-sets var) ptree)) |
| 3049 |
(if (global? var) |
| 3050 |
(new-set source (node-decl ptree) var val) |
| 3051 |
(new-call |
| 3052 |
source |
| 3053 |
(node-decl ptree) |
| 3054 |
(new-ref-extended-bindings source **cell-set!-sym env) |
| 3055 |
(list (new-ref source (node-decl ptree) (cdr (assq var mut))) |
| 3056 |
val))))) |
| 3057 |
((tst? ptree) |
| 3058 |
(new-tst (node-source ptree) |
| 3059 |
(node-decl ptree) |
| 3060 |
(ac (tst-pre ptree) env mut) |
| 3061 |
(ac (tst-con ptree) env mut) |
| 3062 |
(ac (tst-alt ptree) env mut))) |
| 3063 |
((conj? ptree) |
| 3064 |
(new-conj |
| 3065 |
(node-source ptree) |
| 3066 |
(node-decl ptree) |
| 3067 |
(ac (conj-pre ptree) env mut) |
| 3068 |
(ac (conj-alt ptree) env mut))) |
| 3069 |
((disj? ptree) |
| 3070 |
(new-disj |
| 3071 |
(node-source ptree) |
| 3072 |
(node-decl ptree) |
| 3073 |
(ac (disj-pre ptree) env mut) |
| 3074 |
(ac (disj-alt ptree) env mut))) |
| 3075 |
((prc? ptree) (ac-proc ptree env mut)) |
| 3076 |
((app? ptree) |
| 3077 |
(let ((oper (app-oper ptree)) (args (app-args ptree))) |
| 3078 |
(if (and (prc? oper) |
| 3079 |
(not (prc-rest oper)) |
| 3080 |
(= (length (prc-parms oper)) (length args))) |
| 3081 |
(ac-let ptree env mut) |
| 3082 |
(new-call |
| 3083 |
(node-source ptree) |
| 3084 |
(node-decl ptree) |
| 3085 |
(ac oper env mut) |
| 3086 |
(map (lambda (x) (ac x env mut)) args))))) |
| 3087 |
((fut? ptree) |
| 3088 |
(new-fut (node-source ptree) |
| 3089 |
(node-decl ptree) |
| 3090 |
(ac (fut-val ptree) env mut))) |
| 3091 |
(else (compiler-internal-error "ac, unknown parse tree node type")))) |
| 3092 |
(define (ac-proc ptree env mut) |
| 3093 |
(let* ((mut-parms (ac-mutables (prc-parms ptree))) |
| 3094 |
(mut-parms-copies (map var-copy mut-parms)) |
| 3095 |
(mut (append (pair-up mut-parms mut-parms-copies) mut)) |
| 3096 |
(new-body (ac (prc-body ptree) env mut))) |
| 3097 |
(new-prc (node-source ptree) |
| 3098 |
(node-decl ptree) |
| 3099 |
(prc-name ptree) |
| 3100 |
(prc-min ptree) |
| 3101 |
(prc-rest ptree) |
| 3102 |
(prc-parms ptree) |
| 3103 |
(if (null? mut-parms) |
| 3104 |
new-body |
| 3105 |
(new-call |
| 3106 |
(node-source ptree) |
| 3107 |
(node-decl ptree) |
| 3108 |
(new-prc (node-source ptree) |
| 3109 |
(node-decl ptree) |
| 3110 |
#f |
| 3111 |
(length mut-parms-copies) |
| 3112 |
#f |
| 3113 |
mut-parms-copies |
| 3114 |
new-body) |
| 3115 |
(map (lambda (var) |
| 3116 |
(new-call |
| 3117 |
(var-source var) |
| 3118 |
(node-decl ptree) |
| 3119 |
(new-ref-extended-bindings |
| 3120 |
(var-source var) |
| 3121 |
**make-cell-sym |
| 3122 |
env) |
| 3123 |
(list (new-ref (var-source var) |
| 3124 |
(node-decl ptree) |
| 3125 |
var)))) |
| 3126 |
mut-parms)))))) |
| 3127 |
(define (ac-let ptree env mut) |
| 3128 |
(let* ((proc (app-oper ptree)) |
| 3129 |
(vals (app-args ptree)) |
| 3130 |
(vars (prc-parms proc)) |
| 3131 |
(vals-fv (apply set-union (map free-variables vals))) |
| 3132 |
(mut-parms (ac-mutables vars)) |
| 3133 |
(mut-parms-copies (map var-copy mut-parms)) |
| 3134 |
(mut (append (pair-up mut-parms mut-parms-copies) mut))) |
| 3135 |
(let loop ((l vars) |
| 3136 |
(v vals) |
| 3137 |
(new-vars '()) |
| 3138 |
(new-vals '()) |
| 3139 |
(new-body (ac (prc-body proc) env mut))) |
| 3140 |
(if (null? l) |
| 3141 |
(new-let ptree proc new-vars new-vals new-body) |
| 3142 |
(let ((var (car l)) (val (car v))) |
| 3143 |
(if (memq var mut-parms) |
| 3144 |
(let ((src (node-source val)) |
| 3145 |
(decl (node-decl val)) |
| 3146 |
(var* (cdr (assq var mut)))) |
| 3147 |
(if (set-member? var vals-fv) |
| 3148 |
(loop (cdr l) |
| 3149 |
(cdr v) |
| 3150 |
(cons var* new-vars) |
| 3151 |
(cons (new-call |
| 3152 |
src |
| 3153 |
decl |
| 3154 |
(new-ref-extended-bindings |
| 3155 |
src |
| 3156 |
**make-cell-sym |
| 3157 |
env) |
| 3158 |
(list (new-cst src decl undef-object))) |
| 3159 |
new-vals) |
| 3160 |
(new-seq src |
| 3161 |
decl |
| 3162 |
(new-call |
| 3163 |
src |
| 3164 |
decl |
| 3165 |
(new-ref-extended-bindings |
| 3166 |
src |
| 3167 |
**cell-set!-sym |
| 3168 |
env) |
| 3169 |
(list (new-ref src decl var*) |
| 3170 |
(ac val env mut))) |
| 3171 |
new-body)) |
| 3172 |
(loop (cdr l) |
| 3173 |
(cdr v) |
| 3174 |
(cons var* new-vars) |
| 3175 |
(cons (new-call |
| 3176 |
src |
| 3177 |
decl |
| 3178 |
(new-ref-extended-bindings |
| 3179 |
src |
| 3180 |
**make-cell-sym |
| 3181 |
env) |
| 3182 |
(list (ac val env mut))) |
| 3183 |
new-vals) |
| 3184 |
new-body))) |
| 3185 |
(loop (cdr l) |
| 3186 |
(cdr v) |
| 3187 |
(cons var new-vars) |
| 3188 |
(cons (ac val env mut) new-vals) |
| 3189 |
new-body))))))) |
| 3190 |
(define (ac-mutables l) |
| 3191 |
(if (pair? l) |
| 3192 |
(let ((var (car l)) (rest (ac-mutables (cdr l)))) |
| 3193 |
(if (mutable? var) (cons var rest) rest)) |
| 3194 |
'())) |
| 3195 |
(define (lambda-lift! ptree) (ll! ptree (set-empty) '())) |
| 3196 |
(define (ll! ptree cst-procs env) |
| 3197 |
(define (new-env env vars) |
| 3198 |
(define (loop i l) |
| 3199 |
(if (pair? l) |
| 3200 |
(let ((var (car l))) |
| 3201 |
(cons (cons var (cons (length (set->list (var-refs var))) i)) |
| 3202 |
(loop (+ i 1) (cdr l)))) |
| 3203 |
env)) |
| 3204 |
(loop (length env) vars)) |
| 3205 |
(cond ((or (cst? ptree) |
| 3206 |
(ref? ptree) |
| 3207 |
(set? ptree) |
| 3208 |
(tst? ptree) |
| 3209 |
(conj? ptree) |
| 3210 |
(disj? ptree) |
| 3211 |
(fut? ptree)) |
| 3212 |
(for-each |
| 3213 |
(lambda (child) (ll! child cst-procs env)) |
| 3214 |
(node-children ptree))) |
| 3215 |
((prc? ptree) |
| 3216 |
(ll! (prc-body ptree) cst-procs (new-env env (prc-parms ptree)))) |
| 3217 |
((app? ptree) |
| 3218 |
(let ((oper (app-oper ptree)) (args (app-args ptree))) |
| 3219 |
(if (and (prc? oper) |
| 3220 |
(not (prc-rest oper)) |
| 3221 |
(= (length (prc-parms oper)) (length args))) |
| 3222 |
(ll!-let ptree cst-procs (new-env env (prc-parms oper))) |
| 3223 |
(for-each |
| 3224 |
(lambda (child) (ll! child cst-procs env)) |
| 3225 |
(node-children ptree))))) |
| 3226 |
(else (compiler-internal-error "ll!, unknown parse tree node type")))) |
| 3227 |
(define (ll!-let ptree cst-procs env) |
| 3228 |
(let* ((proc (app-oper ptree)) |
| 3229 |
(vals (app-args ptree)) |
| 3230 |
(vars (prc-parms proc)) |
| 3231 |
(var-val-map (pair-up vars vals))) |
| 3232 |
(define (var->val var) (cdr (assq var var-val-map))) |
| 3233 |
(define (liftable-proc-vars vars) |
| 3234 |
(let loop ((cst-proc-vars |
| 3235 |
(set-keep |
| 3236 |
(lambda (var) |
| 3237 |
(let ((val (var->val var))) |
| 3238 |
(and (prc? val) |
| 3239 |
(lambda-lift? (node-decl val)) |
| 3240 |
(set-every? oper-pos? (var-refs var))))) |
| 3241 |
(list->set vars)))) |
| 3242 |
(let* ((non-cst-proc-vars |
| 3243 |
(set-keep |
| 3244 |
(lambda (var) |
| 3245 |
(let ((val (var->val var))) |
| 3246 |
(and (prc? val) (not (set-member? var cst-proc-vars))))) |
| 3247 |
(list->set vars))) |
| 3248 |
(cst-proc-vars* |
| 3249 |
(set-keep |
| 3250 |
(lambda (var) |
| 3251 |
(let ((val (var->val var))) |
| 3252 |
(set-empty? |
| 3253 |
(set-intersection |
| 3254 |
(free-variables val) |
| 3255 |
non-cst-proc-vars)))) |
| 3256 |
cst-proc-vars))) |
| 3257 |
(if (set-equal? cst-proc-vars cst-proc-vars*) |
| 3258 |
cst-proc-vars |
| 3259 |
(loop cst-proc-vars*))))) |
| 3260 |
(define (transitively-closed-free-variables vars) |
| 3261 |
(let ((tcfv-map |
| 3262 |
(map (lambda (var) (cons var (free-variables (var->val var)))) |
| 3263 |
vars))) |
| 3264 |
(let loop ((changed? #f)) |
| 3265 |
(for-each |
| 3266 |
(lambda (var-tcfv) |
| 3267 |
(let loop2 ((l (set->list (cdr var-tcfv))) (fv (cdr var-tcfv))) |
| 3268 |
(if (null? l) |
| 3269 |
(if (not (set-equal? fv (cdr var-tcfv))) |
| 3270 |
(begin (set-cdr! var-tcfv fv) (set! changed? #t))) |
| 3271 |
(let ((x (assq (car l) tcfv-map))) |
| 3272 |
(loop2 (cdr l) (if x (set-union fv (cdr x)) fv)))))) |
| 3273 |
tcfv-map) |
| 3274 |
(if changed? (loop #f) tcfv-map)))) |
| 3275 |
(let* ((tcfv-map |
| 3276 |
(transitively-closed-free-variables (liftable-proc-vars vars))) |
| 3277 |
(cst-proc-vars-list (map car tcfv-map)) |
| 3278 |
(cst-procs* (set-union (list->set cst-proc-vars-list) cst-procs))) |
| 3279 |
(define (var->tcfv var) (cdr (assq var tcfv-map))) |
| 3280 |
(define (order-vars vars) |
| 3281 |
(map car |
| 3282 |
(sort-list |
| 3283 |
(map (lambda (var) (assq var env)) vars) |
| 3284 |
(lambda (x y) |
| 3285 |
(if (= (cadr x) (cadr y)) |
| 3286 |
(< (cddr x) (cddr y)) |
| 3287 |
(< (cadr x) (cadr y))))))) |
| 3288 |
(define (lifted-vars var) |
| 3289 |
(order-vars (set->list (set-difference (var->tcfv var) cst-procs*)))) |
| 3290 |
(define (lift-app! var) |
| 3291 |
(let* ((val (var->val var)) (vars (lifted-vars var))) |
| 3292 |
(define (new-ref* var) |
| 3293 |
(new-ref (var-source var) (node-decl val) var)) |
| 3294 |
(if (not (null? vars)) |
| 3295 |
(for-each |
| 3296 |
(lambda (oper) |
| 3297 |
(let ((node (node-parent oper))) |
| 3298 |
(node-children-set! |
| 3299 |
node |
| 3300 |
(cons (app-oper node) |
| 3301 |
(append (map new-ref* vars) (app-args node)))))) |
| 3302 |
(set->list (var-refs var)))))) |
| 3303 |
(define (lift-prc! var) |
| 3304 |
(let* ((val (var->val var)) (vars (lifted-vars var))) |
| 3305 |
(if (not (null? vars)) |
| 3306 |
(let ((var-copies (map var-copy vars))) |
| 3307 |
(prc-parms-set! val (append var-copies (prc-parms val))) |
| 3308 |
(for-each (lambda (x) (var-bound-set! x val)) var-copies) |
| 3309 |
(node-fv-invalidate! val) |
| 3310 |
(prc-min-set! val (+ (prc-min val) (length vars))) |
| 3311 |
(ll-rename! val (pair-up vars var-copies)))))) |
| 3312 |
(for-each lift-app! cst-proc-vars-list) |
| 3313 |
(for-each lift-prc! cst-proc-vars-list) |
| 3314 |
(for-each (lambda (node) (ll! node cst-procs* env)) vals) |
| 3315 |
(ll! (prc-body proc) cst-procs* env)))) |
| 3316 |
(define (ll-rename! ptree var-map) |
| 3317 |
(cond ((ref? ptree) |
| 3318 |
(let* ((var (ref-var ptree)) (x (assq var var-map))) |
| 3319 |
(if x |
| 3320 |
(begin |
| 3321 |
(var-refs-set! var (set-remove (var-refs var) ptree)) |
| 3322 |
(var-refs-set! (cdr x) (set-adjoin (var-refs (cdr x)) ptree)) |
| 3323 |
(ref-var-set! ptree (cdr x)))))) |
| 3324 |
((set? ptree) |
| 3325 |
(let* ((var (set-var ptree)) (x (assq var var-map))) |
| 3326 |
(if x |
| 3327 |
(begin |
| 3328 |
(var-sets-set! var (set-remove (var-sets var) ptree)) |
| 3329 |
(var-sets-set! (cdr x) (set-adjoin (var-sets (cdr x)) ptree)) |
| 3330 |
(set-var-set! ptree (cdr x))))))) |
| 3331 |
(node-fv-set! ptree #t) |
| 3332 |
(for-each (lambda (child) (ll-rename! child var-map)) (node-children ptree))) |
| 3333 |
(define (parse-tree->expression ptree) (se ptree '() (list 0))) |
| 3334 |
(define (se ptree env num) |
| 3335 |
(cond ((cst? ptree) (list quote-sym (cst-val ptree))) |
| 3336 |
((ref? ptree) |
| 3337 |
(let ((x (assq (ref-var ptree) env))) |
| 3338 |
(if x (cdr x) (var-name (ref-var ptree))))) |
| 3339 |
((set? ptree) |
| 3340 |
(list set!-sym |
| 3341 |
(let ((x (assq (set-var ptree) env))) |
| 3342 |
(if x (cdr x) (var-name (set-var ptree)))) |
| 3343 |
(se (set-val ptree) env num))) |
| 3344 |
((def? ptree) |
| 3345 |
(list define-sym |
| 3346 |
(let ((x (assq (def-var ptree) env))) |
| 3347 |
(if x (cdr x) (var-name (def-var ptree)))) |
| 3348 |
(se (def-val ptree) env num))) |
| 3349 |
((tst? ptree) |
| 3350 |
(list if-sym |
| 3351 |
(se (tst-pre ptree) env num) |
| 3352 |
(se (tst-con ptree) env num) |
| 3353 |
(se (tst-alt ptree) env num))) |
| 3354 |
((conj? ptree) |
| 3355 |
(list and-sym |
| 3356 |
(se (conj-pre ptree) env num) |
| 3357 |
(se (conj-alt ptree) env num))) |
| 3358 |
((disj? ptree) |
| 3359 |
(list or-sym |
| 3360 |
(se (disj-pre ptree) env num) |
| 3361 |
(se (disj-alt ptree) env num))) |
| 3362 |
((prc? ptree) |
| 3363 |
(let ((new-env (se-rename (prc-parms ptree) env num))) |
| 3364 |
(list lambda-sym |
| 3365 |
(se-parameters |
| 3366 |
(prc-parms ptree) |
| 3367 |
(prc-rest ptree) |
| 3368 |
(prc-min ptree) |
| 3369 |
new-env) |
| 3370 |
(se (prc-body ptree) new-env num)))) |
| 3371 |
((app? ptree) |
| 3372 |
(let ((oper (app-oper ptree)) (args (app-args ptree))) |
| 3373 |
(if (and (prc? oper) |
| 3374 |
(not (prc-rest oper)) |
| 3375 |
(= (length (prc-parms oper)) (length args))) |
| 3376 |
(let ((new-env (se-rename (prc-parms oper) env num))) |
| 3377 |
(list (if (set-empty? |
| 3378 |
(set-intersection |
| 3379 |
(list->set (prc-parms oper)) |
| 3380 |
(apply set-union (map free-variables args)))) |
| 3381 |
let-sym |
| 3382 |
letrec-sym) |
| 3383 |
(se-bindings (prc-parms oper) args new-env num) |
| 3384 |
(se (prc-body oper) new-env num))) |
| 3385 |
(map (lambda (x) (se x env num)) (cons oper args))))) |
| 3386 |
((fut? ptree) (list future-sym (se (fut-val ptree) env num))) |
| 3387 |
(else (compiler-internal-error "se, unknown parse tree node type")))) |
| 3388 |
(define (se-parameters parms rest min env) |
| 3389 |
(define (se-parms parms rest n env) |
| 3390 |
(cond ((null? parms) '()) |
| 3391 |
((and rest (null? (cdr parms))) (cdr (assq (car parms) env))) |
| 3392 |
(else |
| 3393 |
(let ((parm (cdr (assq (car parms) env)))) |
| 3394 |
(cons (if (> n 0) parm (list parm)) |
| 3395 |
(se-parms (cdr parms) rest (- n 1) env)))))) |
| 3396 |
(se-parms parms rest min env)) |
| 3397 |
(define (se-bindings vars vals env num) |
| 3398 |
(if (null? vars) |
| 3399 |
'() |
| 3400 |
(cons (list (cdr (assq (car vars) env)) (se (car vals) env num)) |
| 3401 |
(se-bindings (cdr vars) (cdr vals) env num)))) |
| 3402 |
(define (se-rename vars env num) |
| 3403 |
(define (rename vars) |
| 3404 |
(if (null? vars) |
| 3405 |
env |
| 3406 |
(cons (cons (car vars) |
| 3407 |
(string->canonical-symbol |
| 3408 |
(string-append |
| 3409 |
(symbol->string (var-name (car vars))) |
| 3410 |
"#" |
| 3411 |
(number->string (car num))))) |
| 3412 |
(rename (cdr vars))))) |
| 3413 |
(set-car! num (+ (car num) 1)) |
| 3414 |
(rename vars)) |
| 3415 |
(define *opnd-table* '()) |
| 3416 |
(define *opnd-table-alloc* '()) |
| 3417 |
(define opnd-table-size 10000) |
| 3418 |
(define (enter-opnd arg1 arg2) |
| 3419 |
(let loop ((i 0)) |
| 3420 |
(if (< i *opnd-table-alloc*) |
| 3421 |
(let ((x (vector-ref *opnd-table* i))) |
| 3422 |
(if (and (eqv? (car x) arg1) (eqv? (cdr x) arg2)) i (loop (+ i 1)))) |
| 3423 |
(if (< *opnd-table-alloc* opnd-table-size) |
| 3424 |
(begin |
| 3425 |
(set! *opnd-table-alloc* (+ *opnd-table-alloc* 1)) |
| 3426 |
(vector-set! *opnd-table* i (cons arg1 arg2)) |
| 3427 |
i) |
| 3428 |
(compiler-limitation-error |
| 3429 |
"program is too long [virtual machine operand table overflow]"))))) |
| 3430 |
(define (contains-opnd? opnd1 opnd2) |
| 3431 |
(cond ((eqv? opnd1 opnd2) #t) |
| 3432 |
((clo? opnd2) (contains-opnd? opnd1 (clo-base opnd2))) |
| 3433 |
(else #f))) |
| 3434 |
(define (any-contains-opnd? opnd opnds) |
| 3435 |
(if (null? opnds) |
| 3436 |
#f |
| 3437 |
(or (contains-opnd? opnd (car opnds)) |
| 3438 |
(any-contains-opnd? opnd (cdr opnds))))) |
| 3439 |
(define (make-reg num) num) |
| 3440 |
(define (reg? x) (< x 10000)) |
| 3441 |
(define (reg-num x) (modulo x 10000)) |
| 3442 |
(define (make-stk num) (+ num 10000)) |
| 3443 |
(define (stk? x) (= (quotient x 10000) 1)) |
| 3444 |
(define (stk-num x) (modulo x 10000)) |
| 3445 |
(define (make-glo name) (+ (enter-opnd name #t) 30000)) |
| 3446 |
(define (glo? x) (= (quotient x 10000) 3)) |
| 3447 |
(define (glo-name x) (car (vector-ref *opnd-table* (modulo x 10000)))) |
| 3448 |
(define (make-clo base index) (+ (enter-opnd base index) 40000)) |
| 3449 |
(define (clo? x) (= (quotient x 10000) 4)) |
| 3450 |
(define (clo-base x) (car (vector-ref *opnd-table* (modulo x 10000)))) |
| 3451 |
(define (clo-index x) (cdr (vector-ref *opnd-table* (modulo x 10000)))) |
| 3452 |
(define (make-lbl num) (+ num 20000)) |
| 3453 |
(define (lbl? x) (= (quotient x 10000) 2)) |
| 3454 |
(define (lbl-num x) (modulo x 10000)) |
| 3455 |
(define label-limit 9999) |
| 3456 |
(define (make-obj val) (+ (enter-opnd val #f) 50000)) |
| 3457 |
(define (obj? x) (= (quotient x 10000) 5)) |
| 3458 |
(define (obj-val x) (car (vector-ref *opnd-table* (modulo x 10000)))) |
| 3459 |
(define (make-pcontext fs map) (vector fs map)) |
| 3460 |
(define (pcontext-fs x) (vector-ref x 0)) |
| 3461 |
(define (pcontext-map x) (vector-ref x 1)) |
| 3462 |
(define (make-frame size slots regs closed live) |
| 3463 |
(vector size slots regs closed live)) |
| 3464 |
(define (frame-size x) (vector-ref x 0)) |
| 3465 |
(define (frame-slots x) (vector-ref x 1)) |
| 3466 |
(define (frame-regs x) (vector-ref x 2)) |
| 3467 |
(define (frame-closed x) (vector-ref x 3)) |
| 3468 |
(define (frame-live x) (vector-ref x 4)) |
| 3469 |
(define (frame-eq? x y) (= (frame-size x) (frame-size y))) |
| 3470 |
(define (frame-truncate frame nb-slots) |
| 3471 |
(let ((fs (frame-size frame))) |
| 3472 |
(make-frame |
| 3473 |
nb-slots |
| 3474 |
(nth-after (frame-slots frame) (- fs nb-slots)) |
| 3475 |
(frame-regs frame) |
| 3476 |
(frame-closed frame) |
| 3477 |
(frame-live frame)))) |
| 3478 |
(define (frame-live? var frame) |
| 3479 |
(let ((live (frame-live frame))) |
| 3480 |
(if (eq? var closure-env-var) |
| 3481 |
(let ((closed (frame-closed frame))) |
| 3482 |
(if (or (set-member? var live) |
| 3483 |
(not (set-empty? |
| 3484 |
(set-intersection live (list->set closed))))) |
| 3485 |
closed |
| 3486 |
#f)) |
| 3487 |
(if (set-member? var live) var #f)))) |
| 3488 |
(define (frame-first-empty-slot frame) |
| 3489 |
(let loop ((i 1) (s (reverse (frame-slots frame)))) |
| 3490 |
(if (pair? s) |
| 3491 |
(if (frame-live? (car s) frame) (loop (+ i 1) (cdr s)) i) |
| 3492 |
i))) |
| 3493 |
(define (make-proc-obj |
| 3494 |
name |
| 3495 |
primitive? |
| 3496 |
code |
| 3497 |
call-pat |
| 3498 |
side-effects? |
| 3499 |
strict-pat |
| 3500 |
type) |
| 3501 |
(let ((proc-obj |
| 3502 |
(vector proc-obj-tag |
| 3503 |
name |
| 3504 |
primitive? |
| 3505 |
code |
| 3506 |
call-pat |
| 3507 |
#f |
| 3508 |
#f |
| 3509 |
#f |
| 3510 |
side-effects? |
| 3511 |
strict-pat |
| 3512 |
type))) |
| 3513 |
(proc-obj-specialize-set! proc-obj (lambda (decls) proc-obj)) |
| 3514 |
proc-obj)) |
| 3515 |
(define proc-obj-tag (list 'proc-obj)) |
| 3516 |
(define (proc-obj? x) |
| 3517 |
(and (vector? x) |
| 3518 |
(> (vector-length x) 0) |
| 3519 |
(eq? (vector-ref x 0) proc-obj-tag))) |
| 3520 |
(define (proc-obj-name obj) (vector-ref obj 1)) |
| 3521 |
(define (proc-obj-primitive? obj) (vector-ref obj 2)) |
| 3522 |
(define (proc-obj-code obj) (vector-ref obj 3)) |
| 3523 |
(define (proc-obj-call-pat obj) (vector-ref obj 4)) |
| 3524 |
(define (proc-obj-test obj) (vector-ref obj 5)) |
| 3525 |
(define (proc-obj-inlinable obj) (vector-ref obj 6)) |
| 3526 |
(define (proc-obj-specialize obj) (vector-ref obj 7)) |
| 3527 |
(define (proc-obj-side-effects? obj) (vector-ref obj 8)) |
| 3528 |
(define (proc-obj-strict-pat obj) (vector-ref obj 9)) |
| 3529 |
(define (proc-obj-type obj) (vector-ref obj 10)) |
| 3530 |
(define (proc-obj-code-set! obj x) (vector-set! obj 3 x)) |
| 3531 |
(define (proc-obj-test-set! obj x) (vector-set! obj 5 x)) |
| 3532 |
(define (proc-obj-inlinable-set! obj x) (vector-set! obj 6 x)) |
| 3533 |
(define (proc-obj-specialize-set! obj x) (vector-set! obj 7 x)) |
| 3534 |
(define (make-pattern min-args nb-parms rest?) |
| 3535 |
(let loop ((x (if rest? (- nb-parms 1) (list nb-parms))) |
| 3536 |
(y (if rest? (- nb-parms 1) nb-parms))) |
| 3537 |
(let ((z (- y 1))) (if (< z min-args) x (loop (cons z x) z))))) |
| 3538 |
(define (pattern-member? n pat) |
| 3539 |
(cond ((pair? pat) (if (= (car pat) n) #t (pattern-member? n (cdr pat)))) |
| 3540 |
((null? pat) #f) |
| 3541 |
(else (<= pat n)))) |
| 3542 |
(define (type-name type) (if (pair? type) (car type) type)) |
| 3543 |
(define (type-pot-fut? type) (pair? type)) |
| 3544 |
(define (make-bbs) |
| 3545 |
(vector (make-counter 1 label-limit bbs-limit-err) (queue-empty) '())) |
| 3546 |
(define (bbs-limit-err) |
| 3547 |
(compiler-limitation-error "procedure is too long [too many labels]")) |
| 3548 |
(define (bbs-lbl-counter bbs) (vector-ref bbs 0)) |
| 3549 |
(define (bbs-lbl-counter-set! bbs cntr) (vector-set! bbs 0 cntr)) |
| 3550 |
(define (bbs-bb-queue bbs) (vector-ref bbs 1)) |
| 3551 |
(define (bbs-bb-queue-set! bbs bbq) (vector-set! bbs 1 bbq)) |
| 3552 |
(define (bbs-entry-lbl-num bbs) (vector-ref bbs 2)) |
| 3553 |
(define (bbs-entry-lbl-num-set! bbs lbl-num) (vector-set! bbs 2 lbl-num)) |
| 3554 |
(define (bbs-new-lbl! bbs) ((bbs-lbl-counter bbs))) |
| 3555 |
(define (lbl-num->bb lbl-num bbs) |
| 3556 |
(let loop ((bb-list (queue->list (bbs-bb-queue bbs)))) |
| 3557 |
(if (= (bb-lbl-num (car bb-list)) lbl-num) |
| 3558 |
(car bb-list) |
| 3559 |
(loop (cdr bb-list))))) |
| 3560 |
(define (make-bb label-instr bbs) |
| 3561 |
(let ((bb (vector label-instr (queue-empty) '() '() '()))) |
| 3562 |
(queue-put! (vector-ref bbs 1) bb) |
| 3563 |
bb)) |
| 3564 |
(define (bb-lbl-num bb) (label-lbl-num (vector-ref bb 0))) |
| 3565 |
(define (bb-label-type bb) (label-type (vector-ref bb 0))) |
| 3566 |
(define (bb-label-instr bb) (vector-ref bb 0)) |
| 3567 |
(define (bb-label-instr-set! bb l) (vector-set! bb 0 l)) |
| 3568 |
(define (bb-non-branch-instrs bb) (queue->list (vector-ref bb 1))) |
| 3569 |
(define (bb-non-branch-instrs-set! bb l) (vector-set! bb 1 (list->queue l))) |
| 3570 |
(define (bb-branch-instr bb) (vector-ref bb 2)) |
| 3571 |
(define (bb-branch-instr-set! bb b) (vector-set! bb 2 b)) |
| 3572 |
(define (bb-references bb) (vector-ref bb 3)) |
| 3573 |
(define (bb-references-set! bb l) (vector-set! bb 3 l)) |
| 3574 |
(define (bb-precedents bb) (vector-ref bb 4)) |
| 3575 |
(define (bb-precedents-set! bb l) (vector-set! bb 4 l)) |
| 3576 |
(define (bb-entry-frame-size bb) |
| 3577 |
(frame-size (gvm-instr-frame (bb-label-instr bb)))) |
| 3578 |
(define (bb-exit-frame-size bb) |
| 3579 |
(frame-size (gvm-instr-frame (bb-branch-instr bb)))) |
| 3580 |
(define (bb-slots-gained bb) |
| 3581 |
(- (bb-exit-frame-size bb) (bb-entry-frame-size bb))) |
| 3582 |
(define (bb-put-non-branch! bb gvm-instr) |
| 3583 |
(queue-put! (vector-ref bb 1) gvm-instr)) |
| 3584 |
(define (bb-put-branch! bb gvm-instr) (vector-set! bb 2 gvm-instr)) |
| 3585 |
(define (bb-add-reference! bb ref) |
| 3586 |
(if (not (memq ref (vector-ref bb 3))) |
| 3587 |
(vector-set! bb 3 (cons ref (vector-ref bb 3))))) |
| 3588 |
(define (bb-add-precedent! bb prec) |
| 3589 |
(if (not (memq prec (vector-ref bb 4))) |
| 3590 |
(vector-set! bb 4 (cons prec (vector-ref bb 4))))) |
| 3591 |
(define (bb-last-non-branch-instr bb) |
| 3592 |
(let ((non-branch-instrs (bb-non-branch-instrs bb))) |
| 3593 |
(if (null? non-branch-instrs) |
| 3594 |
(bb-label-instr bb) |
| 3595 |
(let loop ((l non-branch-instrs)) |
| 3596 |
(if (pair? (cdr l)) (loop (cdr l)) (car l)))))) |
| 3597 |
(define (gvm-instr-type gvm-instr) (vector-ref gvm-instr 0)) |
| 3598 |
(define (gvm-instr-frame gvm-instr) (vector-ref gvm-instr 1)) |
| 3599 |
(define (gvm-instr-comment gvm-instr) (vector-ref gvm-instr 2)) |
| 3600 |
(define (make-label-simple lbl-num frame comment) |
| 3601 |
(vector 'label frame comment lbl-num 'simple)) |
| 3602 |
(define (make-label-entry lbl-num nb-parms min rest? closed? frame comment) |
| 3603 |
(vector 'label frame comment lbl-num 'entry nb-parms min rest? closed?)) |
| 3604 |
(define (make-label-return lbl-num frame comment) |
| 3605 |
(vector 'label frame comment lbl-num 'return)) |
| 3606 |
(define (make-label-task-entry lbl-num frame comment) |
| 3607 |
(vector 'label frame comment lbl-num 'task-entry)) |
| 3608 |
(define (make-label-task-return lbl-num frame comment) |
| 3609 |
(vector 'label frame comment lbl-num 'task-return)) |
| 3610 |
(define (label-lbl-num gvm-instr) (vector-ref gvm-instr 3)) |
| 3611 |
(define (label-lbl-num-set! gvm-instr n) (vector-set! gvm-instr 3 n)) |
| 3612 |
(define (label-type gvm-instr) (vector-ref gvm-instr 4)) |
| 3613 |
(define (label-entry-nb-parms gvm-instr) (vector-ref gvm-instr 5)) |
| 3614 |
(define (label-entry-min gvm-instr) (vector-ref gvm-instr 6)) |
| 3615 |
(define (label-entry-rest? gvm-instr) (vector-ref gvm-instr 7)) |
| 3616 |
(define (label-entry-closed? gvm-instr) (vector-ref gvm-instr 8)) |
| 3617 |
(define (make-apply prim opnds loc frame comment) |
| 3618 |
(vector 'apply frame comment prim opnds loc)) |
| 3619 |
(define (apply-prim gvm-instr) (vector-ref gvm-instr 3)) |
| 3620 |
(define (apply-opnds gvm-instr) (vector-ref gvm-instr 4)) |
| 3621 |
(define (apply-loc gvm-instr) (vector-ref gvm-instr 5)) |
| 3622 |
(define (make-copy opnd loc frame comment) |
| 3623 |
(vector 'copy frame comment opnd loc)) |
| 3624 |
(define (copy-opnd gvm-instr) (vector-ref gvm-instr 3)) |
| 3625 |
(define (copy-loc gvm-instr) (vector-ref gvm-instr 4)) |
| 3626 |
(define (make-close parms frame comment) (vector 'close frame comment parms)) |
| 3627 |
(define (close-parms gvm-instr) (vector-ref gvm-instr 3)) |
| 3628 |
(define (make-closure-parms loc lbl opnds) (vector loc lbl opnds)) |
| 3629 |
(define (closure-parms-loc x) (vector-ref x 0)) |
| 3630 |
(define (closure-parms-lbl x) (vector-ref x 1)) |
| 3631 |
(define (closure-parms-opnds x) (vector-ref x 2)) |
| 3632 |
(define (make-ifjump test opnds true false poll? frame comment) |
| 3633 |
(vector 'ifjump frame comment test opnds true false poll?)) |
| 3634 |
(define (ifjump-test gvm-instr) (vector-ref gvm-instr 3)) |
| 3635 |
(define (ifjump-opnds gvm-instr) (vector-ref gvm-instr 4)) |
| 3636 |
(define (ifjump-true gvm-instr) (vector-ref gvm-instr 5)) |
| 3637 |
(define (ifjump-false gvm-instr) (vector-ref gvm-instr 6)) |
| 3638 |
(define (ifjump-poll? gvm-instr) (vector-ref gvm-instr 7)) |
| 3639 |
(define (make-jump opnd nb-args poll? frame comment) |
| 3640 |
(vector 'jump frame comment opnd nb-args poll?)) |
| 3641 |
(define (jump-opnd gvm-instr) (vector-ref gvm-instr 3)) |
| 3642 |
(define (jump-nb-args gvm-instr) (vector-ref gvm-instr 4)) |
| 3643 |
(define (jump-poll? gvm-instr) (vector-ref gvm-instr 5)) |
| 3644 |
(define (first-class-jump? gvm-instr) (jump-nb-args gvm-instr)) |
| 3645 |
(define (make-comment) (cons 'comment '())) |
| 3646 |
(define (comment-put! comment name val) |
| 3647 |
(set-cdr! comment (cons (cons name val) (cdr comment)))) |
| 3648 |
(define (comment-get comment name) |
| 3649 |
(and comment (let ((x (assq name (cdr comment)))) (if x (cdr x) #f)))) |
| 3650 |
(define (bbs-purify! bbs) |
| 3651 |
(let loop () |
| 3652 |
(bbs-remove-jump-cascades! bbs) |
| 3653 |
(bbs-remove-dead-code! bbs) |
| 3654 |
(let* ((changed1? (bbs-remove-common-code! bbs)) |
| 3655 |
(changed2? (bbs-remove-useless-jumps! bbs))) |
| 3656 |
(if (or changed1? changed2?) (loop) (bbs-order! bbs))))) |
| 3657 |
(define (bbs-remove-jump-cascades! bbs) |
| 3658 |
(define (empty-bb? bb) |
| 3659 |
(and (eq? (bb-label-type bb) 'simple) (null? (bb-non-branch-instrs bb)))) |
| 3660 |
(define (jump-to-non-entry-lbl? branch) |
| 3661 |
(and (eq? (gvm-instr-type branch) 'jump) |
| 3662 |
(not (first-class-jump? branch)) |
| 3663 |
(jump-lbl? branch))) |
| 3664 |
(define (jump-cascade-to lbl-num fs poll? seen thunk) |
| 3665 |
(if (memq lbl-num seen) |
| 3666 |
(thunk lbl-num fs poll?) |
| 3667 |
(let ((bb (lbl-num->bb lbl-num bbs))) |
| 3668 |
(if (and (empty-bb? bb) (<= (bb-slots-gained bb) 0)) |
| 3669 |
(let ((jump-lbl-num |
| 3670 |
(jump-to-non-entry-lbl? (bb-branch-instr bb)))) |
| 3671 |
(if jump-lbl-num |
| 3672 |
(jump-cascade-to |
| 3673 |
jump-lbl-num |
| 3674 |
(+ fs (bb-slots-gained bb)) |
| 3675 |
(or poll? (jump-poll? (bb-branch-instr bb))) |
| 3676 |
(cons lbl-num seen) |
| 3677 |
thunk) |
| 3678 |
(thunk lbl-num fs poll?))) |
| 3679 |
(thunk lbl-num fs poll?))))) |
| 3680 |
(define (equiv-lbl lbl-num seen) |
| 3681 |
(if (memq lbl-num seen) |
| 3682 |
lbl-num |
| 3683 |
(let ((bb (lbl-num->bb lbl-num bbs))) |
| 3684 |
(if (empty-bb? bb) |
| 3685 |
(let ((jump-lbl-num |
| 3686 |
(jump-to-non-entry-lbl? (bb-branch-instr bb)))) |
| 3687 |
(if (and jump-lbl-num |
| 3688 |
(not (jump-poll? (bb-branch-instr bb))) |
| 3689 |
(= (bb-slots-gained bb) 0)) |
| 3690 |
(equiv-lbl jump-lbl-num (cons lbl-num seen)) |
| 3691 |
lbl-num)) |
| 3692 |
lbl-num)))) |
| 3693 |
(define (remove-cascade! bb) |
| 3694 |
(let ((branch (bb-branch-instr bb))) |
| 3695 |
(case (gvm-instr-type branch) |
| 3696 |
((ifjump) |
| 3697 |
(bb-put-branch! |
| 3698 |
bb |
| 3699 |
(make-ifjump |
| 3700 |
(ifjump-test branch) |
| 3701 |
(ifjump-opnds branch) |
| 3702 |
(equiv-lbl (ifjump-true branch) '()) |
| 3703 |
(equiv-lbl (ifjump-false branch) '()) |
| 3704 |
(ifjump-poll? branch) |
| 3705 |
(gvm-instr-frame branch) |
| 3706 |
(gvm-instr-comment branch)))) |
| 3707 |
((jump) |
| 3708 |
(if (not (first-class-jump? branch)) |
| 3709 |
(let ((dest-lbl-num (jump-lbl? branch))) |
| 3710 |
(if dest-lbl-num |
| 3711 |
(jump-cascade-to |
| 3712 |
dest-lbl-num |
| 3713 |
(frame-size (gvm-instr-frame branch)) |
| 3714 |
(jump-poll? branch) |
| 3715 |
'() |
| 3716 |
(lambda (lbl-num fs poll?) |
| 3717 |
(let* ((dest-bb (lbl-num->bb lbl-num bbs)) |
| 3718 |
(last-branch (bb-branch-instr dest-bb))) |
| 3719 |
(if (and (empty-bb? dest-bb) |
| 3720 |
(or (not poll?) |
| 3721 |
put-poll-on-ifjump? |
| 3722 |
(not (eq? (gvm-instr-type last-branch) |
| 3723 |
'ifjump)))) |
| 3724 |
(let* ((new-fs (+ fs (bb-slots-gained dest-bb))) |
| 3725 |
(new-frame |
| 3726 |
(frame-truncate |
| 3727 |
(gvm-instr-frame branch) |
| 3728 |
new-fs))) |
| 3729 |
(define (adjust-opnd opnd) |
| 3730 |
(cond ((stk? opnd) |
| 3731 |
(make-stk |
| 3732 |
(+ (- fs (bb-entry-frame-size dest-bb)) |
| 3733 |
(stk-num opnd)))) |
| 3734 |
((clo? opnd) |
| 3735 |
(make-clo |
| 3736 |
(adjust-opnd (clo-base opnd)) |
| 3737 |
(clo-index opnd))) |
| 3738 |
(else opnd))) |
| 3739 |
(case (gvm-instr-type last-branch) |
| 3740 |
((ifjump) |
| 3741 |
(bb-put-branch! |
| 3742 |
bb |
| 3743 |
(make-ifjump |
| 3744 |
(ifjump-test last-branch) |
| 3745 |
(map adjust-opnd (ifjump-opnds last-branch)) |
| 3746 |
(equiv-lbl (ifjump-true last-branch) '()) |
| 3747 |
(equiv-lbl (ifjump-false last-branch) '()) |
| 3748 |
(or poll? (ifjump-poll? last-branch)) |
| 3749 |
new-frame |
| 3750 |
(gvm-instr-comment last-branch)))) |
| 3751 |
((jump) |
| 3752 |
(bb-put-branch! |
| 3753 |
bb |
| 3754 |
(make-jump |
| 3755 |
(adjust-opnd (jump-opnd last-branch)) |
| 3756 |
(jump-nb-args last-branch) |
| 3757 |
(or poll? (jump-poll? last-branch)) |
| 3758 |
new-frame |
| 3759 |
(gvm-instr-comment last-branch)))) |
| 3760 |
(else |
| 3761 |
(compiler-internal-error |
| 3762 |
"bbs-remove-jump-cascades!, unknown branch type")))) |
| 3763 |
(bb-put-branch! |
| 3764 |
bb |
| 3765 |
(make-jump |
| 3766 |
(make-lbl lbl-num) |
| 3767 |
(jump-nb-args branch) |
| 3768 |
(or poll? (jump-poll? branch)) |
| 3769 |
(frame-truncate (gvm-instr-frame branch) fs) |
| 3770 |
(gvm-instr-comment branch))))))))))) |
| 3771 |
(else |
| 3772 |
(compiler-internal-error |
| 3773 |
"bbs-remove-jump-cascades!, unknown branch type"))))) |
| 3774 |
(for-each remove-cascade! (queue->list (bbs-bb-queue bbs)))) |
| 3775 |
(define (jump-lbl? branch) |
| 3776 |
(let ((opnd (jump-opnd branch))) (if (lbl? opnd) (lbl-num opnd) #f))) |
| 3777 |
(define put-poll-on-ifjump? #t) |
| 3778 |
(define (bbs-remove-dead-code! bbs) |
| 3779 |
(let ((new-bb-queue (queue-empty)) (scan-queue (queue-empty))) |
| 3780 |
(define (reachable ref bb) |
| 3781 |
(if bb (bb-add-reference! bb ref)) |
| 3782 |
(if (not (memq ref (queue->list new-bb-queue))) |
| 3783 |
(begin |
| 3784 |
(bb-references-set! ref '()) |
| 3785 |
(bb-precedents-set! ref '()) |
| 3786 |
(queue-put! new-bb-queue ref) |
| 3787 |
(queue-put! scan-queue ref)))) |
| 3788 |
(define (direct-jump to-bb from-bb) |
| 3789 |
(reachable to-bb from-bb) |
| 3790 |
(bb-add-precedent! to-bb from-bb)) |
| 3791 |
(define (scan-instr gvm-instr bb) |
| 3792 |
(define (scan-opnd gvm-opnd) |
| 3793 |
(cond ((lbl? gvm-opnd) |
| 3794 |
(reachable (lbl-num->bb (lbl-num gvm-opnd) bbs) bb)) |
| 3795 |
((clo? gvm-opnd) (scan-opnd (clo-base gvm-opnd))))) |
| 3796 |
(case (gvm-instr-type gvm-instr) |
| 3797 |
((label) '()) |
| 3798 |
((apply) |
| 3799 |
(for-each scan-opnd (apply-opnds gvm-instr)) |
| 3800 |
(if (apply-loc gvm-instr) (scan-opnd (apply-loc gvm-instr)))) |
| 3801 |
((copy) |
| 3802 |
(scan-opnd (copy-opnd gvm-instr)) |
| 3803 |
(scan-opnd (copy-loc gvm-instr))) |
| 3804 |
((close) |
| 3805 |
(for-each |
| 3806 |
(lambda (parm) |
| 3807 |
(reachable (lbl-num->bb (closure-parms-lbl parm) bbs) bb) |
| 3808 |
(scan-opnd (closure-parms-loc parm)) |
| 3809 |
(for-each scan-opnd (closure-parms-opnds parm))) |
| 3810 |
(close-parms gvm-instr))) |
| 3811 |
((ifjump) |
| 3812 |
(for-each scan-opnd (ifjump-opnds gvm-instr)) |
| 3813 |
(direct-jump (lbl-num->bb (ifjump-true gvm-instr) bbs) bb) |
| 3814 |
(direct-jump (lbl-num->bb (ifjump-false gvm-instr) bbs) bb)) |
| 3815 |
((jump) |
| 3816 |
(let ((opnd (jump-opnd gvm-instr))) |
| 3817 |
(if (lbl? opnd) |
| 3818 |
(direct-jump (lbl-num->bb (lbl-num opnd) bbs) bb) |
| 3819 |
(scan-opnd (jump-opnd gvm-instr))))) |
| 3820 |
(else |
| 3821 |
(compiler-internal-error |
| 3822 |
"bbs-remove-dead-code!, unknown GVM instruction type")))) |
| 3823 |
(reachable (lbl-num->bb (bbs-entry-lbl-num bbs) bbs) #f) |
| 3824 |
(let loop () |
| 3825 |
(if (not (queue-empty? scan-queue)) |
| 3826 |
(let ((bb (queue-get! scan-queue))) |
| 3827 |
(begin |
| 3828 |
(scan-instr (bb-label-instr bb) bb) |
| 3829 |
(for-each |
| 3830 |
(lambda (gvm-instr) (scan-instr gvm-instr bb)) |
| 3831 |
(bb-non-branch-instrs bb)) |
| 3832 |
(scan-instr (bb-branch-instr bb) bb) |
| 3833 |
(loop))))) |
| 3834 |
(bbs-bb-queue-set! bbs new-bb-queue))) |
| 3835 |
(define (bbs-remove-useless-jumps! bbs) |
| 3836 |
(let ((changed? #f)) |
| 3837 |
(define (remove-useless-jump bb) |
| 3838 |
(let ((branch (bb-branch-instr bb))) |
| 3839 |
(if (and (eq? (gvm-instr-type branch) 'jump) |
| 3840 |
(not (first-class-jump? branch)) |
| 3841 |
(not (jump-poll? branch)) |
| 3842 |
(jump-lbl? branch)) |
| 3843 |
(let* ((dest-bb (lbl-num->bb (jump-lbl? branch) bbs)) |
| 3844 |
(frame1 (gvm-instr-frame (bb-last-non-branch-instr bb))) |
| 3845 |
(frame2 (gvm-instr-frame (bb-label-instr dest-bb)))) |
| 3846 |
(if (and (eq? (bb-label-type dest-bb) 'simple) |
| 3847 |
(frame-eq? frame1 frame2) |
| 3848 |
(= (length (bb-precedents dest-bb)) 1)) |
| 3849 |
(begin |
| 3850 |
(set! changed? #t) |
| 3851 |
(bb-non-branch-instrs-set! |
| 3852 |
bb |
| 3853 |
(append (bb-non-branch-instrs bb) |
| 3854 |
(bb-non-branch-instrs dest-bb) |
| 3855 |
'())) |
| 3856 |
(bb-branch-instr-set! bb (bb-branch-instr dest-bb)) |
| 3857 |
(remove-useless-jump bb))))))) |
| 3858 |
(for-each remove-useless-jump (queue->list (bbs-bb-queue bbs))) |
| 3859 |
changed?)) |
| 3860 |
(define (bbs-remove-common-code! bbs) |
| 3861 |
(let* ((bb-list (queue->list (bbs-bb-queue bbs))) |
| 3862 |
(n (length bb-list)) |
| 3863 |
(hash-table-length (cond ((< n 50) 43) ((< n 500) 403) (else 4003))) |
| 3864 |
(hash-table (make-vector hash-table-length '())) |
| 3865 |
(prim-table '()) |
| 3866 |
(block-map '()) |
| 3867 |
(changed? #f)) |
| 3868 |
(define (hash-prim prim) |
| 3869 |
(let ((n (length prim-table)) (i (pos-in-list prim prim-table))) |
| 3870 |
(if i |
| 3871 |
(- n i) |
| 3872 |
(begin (set! prim-table (cons prim prim-table)) (+ n 1))))) |
| 3873 |
(define (hash-opnds l) |
| 3874 |
(let loop ((l l) (n 0)) |
| 3875 |
(if (pair? l) |
| 3876 |
(loop (cdr l) |
| 3877 |
(let ((x (car l))) |
| 3878 |
(if (lbl? x) |
| 3879 |
n |
| 3880 |
(modulo (+ (* n 10000) x) hash-table-length)))) |
| 3881 |
n))) |
| 3882 |
(define (hash-bb bb) |
| 3883 |
(let ((branch (bb-branch-instr bb))) |
| 3884 |
(modulo (case (gvm-instr-type branch) |
| 3885 |
((ifjump) |
| 3886 |
(+ (hash-opnds (ifjump-opnds branch)) |
| 3887 |
(* 10 (hash-prim (ifjump-test branch))) |
| 3888 |
(* 100 (frame-size (gvm-instr-frame branch))))) |
| 3889 |
((jump) |
| 3890 |
(+ (hash-opnds (list (jump-opnd branch))) |
| 3891 |
(* 10 (or (jump-nb-args branch) -1)) |
| 3892 |
(* 100 (frame-size (gvm-instr-frame branch))))) |
| 3893 |
(else 0)) |
| 3894 |
hash-table-length))) |
| 3895 |
(define (replacement-lbl-num lbl) |
| 3896 |
(let ((x (assv lbl block-map))) (if x (cdr x) lbl))) |
| 3897 |
(define (fix-map! bb1 bb2) |
| 3898 |
(let loop ((l block-map)) |
| 3899 |
(if (pair? l) |
| 3900 |
(let ((x (car l))) |
| 3901 |
(if (= bb1 (cdr x)) (set-cdr! x bb2)) |
| 3902 |
(loop (cdr l)))))) |
| 3903 |
(define (enter-bb! bb) |
| 3904 |
(let ((h (hash-bb bb))) |
| 3905 |
(vector-set! hash-table h (add-bb bb (vector-ref hash-table h))))) |
| 3906 |
(define (add-bb bb l) |
| 3907 |
(if (pair? l) |
| 3908 |
(let ((bb* (car l))) |
| 3909 |
(set! block-map |
| 3910 |
(cons (cons (bb-lbl-num bb) (bb-lbl-num bb*)) block-map)) |
| 3911 |
(if (eqv-bb? bb bb*) |
| 3912 |
(begin |
| 3913 |
(fix-map! (bb-lbl-num bb) (bb-lbl-num bb*)) |
| 3914 |
(set! changed? #t) |
| 3915 |
l) |
| 3916 |
(begin |
| 3917 |
(set! block-map (cdr block-map)) |
| 3918 |
(if (eqv-gvm-instr? |
| 3919 |
(bb-branch-instr bb) |
| 3920 |
(bb-branch-instr bb*)) |
| 3921 |
(extract-common-tail |
| 3922 |
bb |
| 3923 |
bb* |
| 3924 |
(lambda (head head* tail) |
| 3925 |
(if (null? tail) |
| 3926 |
(cons bb* (add-bb bb (cdr l))) |
| 3927 |
(let* ((lbl (bbs-new-lbl! bbs)) |
| 3928 |
(branch (bb-branch-instr bb)) |
| 3929 |
(fs** (need-gvm-instrs tail branch)) |
| 3930 |
(frame (frame-truncate |
| 3931 |
(gvm-instr-frame |
| 3932 |
(if (null? head) |
| 3933 |
(bb-label-instr bb) |
| 3934 |
(car head))) |
| 3935 |
fs**)) |
| 3936 |
(bb** (make-bb (make-label-simple |
| 3937 |
lbl |
| 3938 |
frame |
| 3939 |
#f) |
| 3940 |
bbs))) |
| 3941 |
(bb-non-branch-instrs-set! bb** tail) |
| 3942 |
(bb-branch-instr-set! bb** branch) |
| 3943 |
(bb-non-branch-instrs-set! bb* (reverse head*)) |
| 3944 |
(bb-branch-instr-set! |
| 3945 |
bb* |
| 3946 |
(make-jump (make-lbl lbl) #f #f frame #f)) |
| 3947 |
(bb-non-branch-instrs-set! bb (reverse head)) |
| 3948 |
(bb-branch-instr-set! |
| 3949 |
bb |
| 3950 |
(make-jump (make-lbl lbl) #f #f frame #f)) |
| 3951 |
(set! changed? #t) |
| 3952 |
(cons bb (cons bb* (add-bb bb** (cdr l)))))))) |
| 3953 |
(cons bb* (add-bb bb (cdr l))))))) |
| 3954 |
(list bb))) |
| 3955 |
(define (extract-common-tail bb1 bb2 cont) |
| 3956 |
(let loop ((l1 (reverse (bb-non-branch-instrs bb1))) |
| 3957 |
(l2 (reverse (bb-non-branch-instrs bb2))) |
| 3958 |
(tail '())) |
| 3959 |
(if (and (pair? l1) (pair? l2)) |
| 3960 |
(let ((i1 (car l1)) (i2 (car l2))) |
| 3961 |
(if (eqv-gvm-instr? i1 i2) |
| 3962 |
(loop (cdr l1) (cdr l2) (cons i1 tail)) |
| 3963 |
(cont l1 l2 tail))) |
| 3964 |
(cont l1 l2 tail)))) |
| 3965 |
(define (eqv-bb? bb1 bb2) |
| 3966 |
(let ((bb1-non-branch (bb-non-branch-instrs bb1)) |
| 3967 |
(bb2-non-branch (bb-non-branch-instrs bb2))) |
| 3968 |
(and (= (length bb1-non-branch) (length bb2-non-branch)) |
| 3969 |
(eqv-gvm-instr? (bb-label-instr bb1) (bb-label-instr bb2)) |
| 3970 |
(eqv-gvm-instr? (bb-branch-instr bb1) (bb-branch-instr bb2)) |
| 3971 |
(eqv-list? eqv-gvm-instr? bb1-non-branch bb2-non-branch)))) |
| 3972 |
(define (eqv-list? pred? l1 l2) |
| 3973 |
(if (pair? l1) |
| 3974 |
(and (pair? l2) |
| 3975 |
(pred? (car l1) (car l2)) |
| 3976 |
(eqv-list? pred? (cdr l1) (cdr l2))) |
| 3977 |
(not (pair? l2)))) |
| 3978 |
(define (eqv-lbl-num? lbl1 lbl2) |
| 3979 |
(= (replacement-lbl-num lbl1) (replacement-lbl-num lbl2))) |
| 3980 |
(define (eqv-gvm-opnd? opnd1 opnd2) |
| 3981 |
(if (not opnd1) |
| 3982 |
(not opnd2) |
| 3983 |
(and opnd2 |
| 3984 |
(cond ((lbl? opnd1) |
| 3985 |
(and (lbl? opnd2) |
| 3986 |
(eqv-lbl-num? (lbl-num opnd1) (lbl-num opnd2)))) |
| 3987 |
((clo? opnd1) |
| 3988 |
(and (clo? opnd2) |
| 3989 |
(= (clo-index opnd1) (clo-index opnd2)) |
| 3990 |
(eqv-gvm-opnd? (clo-base opnd1) (clo-base opnd2)))) |
| 3991 |
(else (eqv? opnd1 opnd2)))))) |
| 3992 |
(define (eqv-gvm-instr? instr1 instr2) |
| 3993 |
(define (eqv-closure-parms? p1 p2) |
| 3994 |
(and (eqv-gvm-opnd? (closure-parms-loc p1) (closure-parms-loc p2)) |
| 3995 |
(eqv-lbl-num? (closure-parms-lbl p1) (closure-parms-lbl p2)) |
| 3996 |
(eqv-list? |
| 3997 |
eqv-gvm-opnd? |
| 3998 |
(closure-parms-opnds p1) |
| 3999 |
(closure-parms-opnds p2)))) |
| 4000 |
(let ((type1 (gvm-instr-type instr1)) (type2 (gvm-instr-type instr2))) |
| 4001 |
(and (eq? type1 type2) |
| 4002 |
(frame-eq? (gvm-instr-frame instr1) (gvm-instr-frame instr2)) |
| 4003 |
(case type1 |
| 4004 |
((label) |
| 4005 |
(let ((ltype1 (label-type instr1)) |
| 4006 |
(ltype2 (label-type instr2))) |
| 4007 |
(and (eq? ltype1 ltype2) |
| 4008 |
(case ltype1 |
| 4009 |
((simple return task-entry task-return) #t) |
| 4010 |
((entry) |
| 4011 |
(and (= (label-entry-min instr1) |
| 4012 |
(label-entry-min instr2)) |
| 4013 |
(= (label-entry-nb-parms instr1) |
| 4014 |
(label-entry-nb-parms instr2)) |
| 4015 |
(eq? (label-entry-rest? instr1) |
| 4016 |
(label-entry-rest? instr2)) |
| 4017 |
(eq? (label-entry-closed? instr1) |
| 4018 |
(label-entry-closed? instr2)))) |
| 4019 |
(else |
| 4020 |
(compiler-internal-error |
| 4021 |
"eqv-gvm-instr?, unknown label type")))))) |
| 4022 |
((apply) |
| 4023 |
(and (eq? (apply-prim instr1) (apply-prim instr2)) |
| 4024 |
(eqv-list? |
| 4025 |
eqv-gvm-opnd? |
| 4026 |
(apply-opnds instr1) |
| 4027 |
(apply-opnds instr2)) |
| 4028 |
(eqv-gvm-opnd? (apply-loc instr1) (apply-loc instr2)))) |
| 4029 |
((copy) |
| 4030 |
(and (eqv-gvm-opnd? (copy-opnd instr1) (copy-opnd instr2)) |
| 4031 |
(eqv-gvm-opnd? (copy-loc instr1) (copy-loc instr2)))) |
| 4032 |
((close) |
| 4033 |
(eqv-list? |
| 4034 |
eqv-closure-parms? |
| 4035 |
(close-parms instr1) |
| 4036 |
(close-parms instr2))) |
| 4037 |
((ifjump) |
| 4038 |
(and (eq? (ifjump-test instr1) (ifjump-test instr2)) |
| 4039 |
(eqv-list? |
| 4040 |
eqv-gvm-opnd? |
| 4041 |
(ifjump-opnds instr1) |
| 4042 |
(ifjump-opnds instr2)) |
| 4043 |
(eqv-lbl-num? (ifjump-true instr1) (ifjump-true instr2)) |
| 4044 |
(eqv-lbl-num? (ifjump-false instr1) (ifjump-false instr2)) |
| 4045 |
(eq? (ifjump-poll? instr1) (ifjump-poll? instr2)))) |
| 4046 |
((jump) |
| 4047 |
(and (eqv-gvm-opnd? (jump-opnd instr1) (jump-opnd instr2)) |
| 4048 |
(eqv? (jump-nb-args instr1) (jump-nb-args instr2)) |
| 4049 |
(eq? (jump-poll? instr1) (jump-poll? instr2)))) |
| 4050 |
(else |
| 4051 |
(compiler-internal-error |
| 4052 |
"eqv-gvm-instr?, unknown 'gvm-instr':" |
| 4053 |
instr1)))))) |
| 4054 |
(define (update-bb! bb) (replace-label-references! bb replacement-lbl-num)) |
| 4055 |
(for-each enter-bb! bb-list) |
| 4056 |
(bbs-entry-lbl-num-set! bbs (replacement-lbl-num (bbs-entry-lbl-num bbs))) |
| 4057 |
(let loop ((i 0) (result '())) |
| 4058 |
(if (< i hash-table-length) |
| 4059 |
(let ((bb-kept (vector-ref hash-table i))) |
| 4060 |
(for-each update-bb! bb-kept) |
| 4061 |
(loop (+ i 1) (append bb-kept result))) |
| 4062 |
(bbs-bb-queue-set! bbs (list->queue result)))) |
| 4063 |
changed?)) |
| 4064 |
(define (replace-label-references! bb replacement-lbl-num) |
| 4065 |
(define (update-gvm-opnd opnd) |
| 4066 |
(if opnd |
| 4067 |
(cond ((lbl? opnd) (make-lbl (replacement-lbl-num (lbl-num opnd)))) |
| 4068 |
((clo? opnd) |
| 4069 |
(make-clo (update-gvm-opnd (clo-base opnd)) (clo-index opnd))) |
| 4070 |
(else opnd)) |
| 4071 |
opnd)) |
| 4072 |
(define (update-gvm-instr instr) |
| 4073 |
(define (update-closure-parms p) |
| 4074 |
(make-closure-parms |
| 4075 |
(update-gvm-opnd (closure-parms-loc p)) |
| 4076 |
(replacement-lbl-num (closure-parms-lbl p)) |
| 4077 |
(map update-gvm-opnd (closure-parms-opnds p)))) |
| 4078 |
(case (gvm-instr-type instr) |
| 4079 |
((apply) |
| 4080 |
(make-apply |
| 4081 |
(apply-prim instr) |
| 4082 |
(map update-gvm-opnd (apply-opnds instr)) |
| 4083 |
(update-gvm-opnd (apply-loc instr)) |
| 4084 |
(gvm-instr-frame instr) |
| 4085 |
(gvm-instr-comment instr))) |
| 4086 |
((copy) |
| 4087 |
(make-copy |
| 4088 |
(update-gvm-opnd (copy-opnd instr)) |
| 4089 |
(update-gvm-opnd (copy-loc instr)) |
| 4090 |
(gvm-instr-frame instr) |
| 4091 |
(gvm-instr-comment instr))) |
| 4092 |
((close) |
| 4093 |
(make-close |
| 4094 |
(map update-closure-parms (close-parms instr)) |
| 4095 |
(gvm-instr-frame instr) |
| 4096 |
(gvm-instr-comment instr))) |
| 4097 |
((ifjump) |
| 4098 |
(make-ifjump |
| 4099 |
(ifjump-test instr) |
| 4100 |
(map update-gvm-opnd (ifjump-opnds instr)) |
| 4101 |
(replacement-lbl-num (ifjump-true instr)) |
| 4102 |
(replacement-lbl-num (ifjump-false instr)) |
| 4103 |
(ifjump-poll? instr) |
| 4104 |
(gvm-instr-frame instr) |
| 4105 |
(gvm-instr-comment instr))) |
| 4106 |
((jump) |
| 4107 |
(make-jump |
| 4108 |
(update-gvm-opnd (jump-opnd instr)) |
| 4109 |
(jump-nb-args instr) |
| 4110 |
(jump-poll? instr) |
| 4111 |
(gvm-instr-frame instr) |
| 4112 |
(gvm-instr-comment instr))) |
| 4113 |
(else |
| 4114 |
(compiler-internal-error "update-gvm-instr, unknown 'instr':" instr)))) |
| 4115 |
(bb-non-branch-instrs-set! |
| 4116 |
bb |
| 4117 |
(map update-gvm-instr (bb-non-branch-instrs bb))) |
| 4118 |
(bb-branch-instr-set! bb (update-gvm-instr (bb-branch-instr bb)))) |
| 4119 |
(define (bbs-order! bbs) |
| 4120 |
(let ((new-bb-queue (queue-empty)) |
| 4121 |
(left-to-schedule (queue->list (bbs-bb-queue bbs)))) |
| 4122 |
(define (remove x l) |
| 4123 |
(if (eq? (car l) x) (cdr l) (cons (car l) (remove x (cdr l))))) |
| 4124 |
(define (remove-bb! bb) |
| 4125 |
(set! left-to-schedule (remove bb left-to-schedule)) |
| 4126 |
bb) |
| 4127 |
(define (prec-bb bb) |
| 4128 |
(let loop ((l (bb-precedents bb)) (best #f) (best-fs #f)) |
| 4129 |
(if (null? l) |
| 4130 |
best |
| 4131 |
(let* ((x (car l)) (x-fs (bb-exit-frame-size x))) |
| 4132 |
(if (and (memq x left-to-schedule) |
| 4133 |
(or (not best) (< x-fs best-fs))) |
| 4134 |
(loop (cdr l) x x-fs) |
| 4135 |
(loop (cdr l) best best-fs)))))) |
| 4136 |
(define (succ-bb bb) |
| 4137 |
(define (branches-to-lbl? bb) |
| 4138 |
(let ((branch (bb-branch-instr bb))) |
| 4139 |
(case (gvm-instr-type branch) |
| 4140 |
((ifjump) #t) |
| 4141 |
((jump) (lbl? (jump-opnd branch))) |
| 4142 |
(else |
| 4143 |
(compiler-internal-error "bbs-order!, unknown branch type"))))) |
| 4144 |
(define (best-succ bb1 bb2) |
| 4145 |
(if (branches-to-lbl? bb1) |
| 4146 |
bb1 |
| 4147 |
(if (branches-to-lbl? bb2) |
| 4148 |
bb2 |
| 4149 |
(if (< (bb-exit-frame-size bb1) (bb-exit-frame-size bb2)) |
| 4150 |
bb2 |
| 4151 |
bb1)))) |
| 4152 |
(let ((branch (bb-branch-instr bb))) |
| 4153 |
(case (gvm-instr-type branch) |
| 4154 |
((ifjump) |
| 4155 |
(let* ((true-bb (lbl-num->bb (ifjump-true branch) bbs)) |
| 4156 |
(true-bb* (and (memq true-bb left-to-schedule) true-bb)) |
| 4157 |
(false-bb (lbl-num->bb (ifjump-false branch) bbs)) |
| 4158 |
(false-bb* (and (memq false-bb left-to-schedule) false-bb))) |
| 4159 |
(if (and true-bb* false-bb*) |
| 4160 |
(best-succ true-bb* false-bb*) |
| 4161 |
(or true-bb* false-bb*)))) |
| 4162 |
((jump) |
| 4163 |
(let ((opnd (jump-opnd branch))) |
| 4164 |
(and (lbl? opnd) |
| 4165 |
(let ((bb (lbl-num->bb (lbl-num opnd) bbs))) |
| 4166 |
(and (memq bb left-to-schedule) bb))))) |
| 4167 |
(else (compiler-internal-error "bbs-order!, unknown branch type"))))) |
| 4168 |
(define (schedule-from bb) |
| 4169 |
(queue-put! new-bb-queue bb) |
| 4170 |
(let ((x (succ-bb bb))) |
| 4171 |
(if x |
| 4172 |
(begin |
| 4173 |
(schedule-around (remove-bb! x)) |
| 4174 |
(let ((y (succ-bb bb))) |
| 4175 |
(if y (schedule-around (remove-bb! y))))))) |
| 4176 |
(schedule-refs bb)) |
| 4177 |
(define (schedule-around bb) |
| 4178 |
(let ((x (prec-bb bb))) |
| 4179 |
(if x |
| 4180 |
(let ((bb-list (schedule-back (remove-bb! x) '()))) |
| 4181 |
(queue-put! new-bb-queue x) |
| 4182 |
(schedule-forw bb) |
| 4183 |
(for-each schedule-refs bb-list)) |
| 4184 |
(schedule-from bb)))) |
| 4185 |
(define (schedule-back bb bb-list) |
| 4186 |
(let ((bb-list* (cons bb bb-list)) (x (prec-bb bb))) |
| 4187 |
(if x |
| 4188 |
(let ((bb-list (schedule-back (remove-bb! x) bb-list*))) |
| 4189 |
(queue-put! new-bb-queue x) |
| 4190 |
bb-list) |
| 4191 |
bb-list*))) |
| 4192 |
(define (schedule-forw bb) |
| 4193 |
(queue-put! new-bb-queue bb) |
| 4194 |
(let ((x (succ-bb bb))) |
| 4195 |
(if x |
| 4196 |
(begin |
| 4197 |
(schedule-forw (remove-bb! x)) |
| 4198 |
(let ((y (succ-bb bb))) |
| 4199 |
(if y (schedule-around (remove-bb! y))))))) |
| 4200 |
(schedule-refs bb)) |
| 4201 |
(define (schedule-refs bb) |
| 4202 |
(for-each |
| 4203 |
(lambda (x) |
| 4204 |
(if (memq x left-to-schedule) (schedule-around (remove-bb! x)))) |
| 4205 |
(bb-references bb))) |
| 4206 |
(schedule-from (remove-bb! (lbl-num->bb (bbs-entry-lbl-num bbs) bbs))) |
| 4207 |
(bbs-bb-queue-set! bbs new-bb-queue) |
| 4208 |
(let ((bb-list (queue->list new-bb-queue))) |
| 4209 |
(let loop ((l bb-list) (i 1) (lbl-map '())) |
| 4210 |
(if (pair? l) |
| 4211 |
(let* ((label-instr (bb-label-instr (car l))) |
| 4212 |
(old-lbl-num (label-lbl-num label-instr))) |
| 4213 |
(label-lbl-num-set! label-instr i) |
| 4214 |
(loop (cdr l) (+ i 1) (cons (cons old-lbl-num i) lbl-map))) |
| 4215 |
(let () |
| 4216 |
(define (replacement-lbl-num x) (cdr (assv x lbl-map))) |
| 4217 |
(define (update-bb! bb) |
| 4218 |
(replace-label-references! bb replacement-lbl-num)) |
| 4219 |
(for-each update-bb! bb-list) |
| 4220 |
(bbs-lbl-counter-set! |
| 4221 |
bbs |
| 4222 |
(make-counter |
| 4223 |
(* (+ 1 (quotient (bbs-new-lbl! bbs) 1000)) 1000) |
| 4224 |
label-limit |
| 4225 |
bbs-limit-err)))))))) |
| 4226 |
(define (make-code bb gvm-instr sn) (vector bb gvm-instr sn)) |
| 4227 |
(define (code-bb code) (vector-ref code 0)) |
| 4228 |
(define (code-gvm-instr code) (vector-ref code 1)) |
| 4229 |
(define (code-slots-needed code) (vector-ref code 2)) |
| 4230 |
(define (code-slots-needed-set! code n) (vector-set! code 2 n)) |
| 4231 |
(define (bbs->code-list bbs) |
| 4232 |
(let ((code-list (linearize bbs))) |
| 4233 |
(setup-slots-needed! code-list) |
| 4234 |
code-list)) |
| 4235 |
(define (linearize bbs) |
| 4236 |
(let ((code-queue (queue-empty))) |
| 4237 |
(define (put-bb bb) |
| 4238 |
(define (put-instr gvm-instr) |
| 4239 |
(queue-put! code-queue (make-code bb gvm-instr #f))) |
| 4240 |
(put-instr (bb-label-instr bb)) |
| 4241 |
(for-each put-instr (bb-non-branch-instrs bb)) |
| 4242 |
(put-instr (bb-branch-instr bb))) |
| 4243 |
(for-each put-bb (queue->list (bbs-bb-queue bbs))) |
| 4244 |
(queue->list code-queue))) |
| 4245 |
(define (setup-slots-needed! code-list) |
| 4246 |
(if (null? code-list) |
| 4247 |
#f |
| 4248 |
(let* ((code (car code-list)) |
| 4249 |
(gvm-instr (code-gvm-instr code)) |
| 4250 |
(sn-rest (setup-slots-needed! (cdr code-list)))) |
| 4251 |
(case (gvm-instr-type gvm-instr) |
| 4252 |
((label) |
| 4253 |
(if (> sn-rest (frame-size (gvm-instr-frame gvm-instr))) |
| 4254 |
(compiler-internal-error |
| 4255 |
"setup-slots-needed!, incoherent slots needed for LABEL")) |
| 4256 |
(code-slots-needed-set! code sn-rest) |
| 4257 |
#f) |
| 4258 |
((ifjump jump) |
| 4259 |
(let ((sn (frame-size (gvm-instr-frame gvm-instr)))) |
| 4260 |
(code-slots-needed-set! code sn) |
| 4261 |
(need-gvm-instr gvm-instr sn))) |
| 4262 |
(else |
| 4263 |
(code-slots-needed-set! code sn-rest) |
| 4264 |
(need-gvm-instr gvm-instr sn-rest)))))) |
| 4265 |
(define (need-gvm-instrs non-branch branch) |
| 4266 |
(if (pair? non-branch) |
| 4267 |
(need-gvm-instr |
| 4268 |
(car non-branch) |
| 4269 |
(need-gvm-instrs (cdr non-branch) branch)) |
| 4270 |
(need-gvm-instr branch (frame-size (gvm-instr-frame branch))))) |
| 4271 |
(define (need-gvm-instr gvm-instr sn-rest) |
| 4272 |
(case (gvm-instr-type gvm-instr) |
| 4273 |
((label) sn-rest) |
| 4274 |
((apply) |
| 4275 |
(let ((loc (apply-loc gvm-instr))) |
| 4276 |
(need-gvm-opnds |
| 4277 |
(apply-opnds gvm-instr) |
| 4278 |
(need-gvm-loc-opnd loc (need-gvm-loc loc sn-rest))))) |
| 4279 |
((copy) |
| 4280 |
(let ((loc (copy-loc gvm-instr))) |
| 4281 |
(need-gvm-opnd |
| 4282 |
(copy-opnd gvm-instr) |
| 4283 |
(need-gvm-loc-opnd loc (need-gvm-loc loc sn-rest))))) |
| 4284 |
((close) |
| 4285 |
(let ((parms (close-parms gvm-instr))) |
| 4286 |
(define (need-parms-opnds p) |
| 4287 |
(if (null? p) |
| 4288 |
sn-rest |
| 4289 |
(need-gvm-opnds |
| 4290 |
(closure-parms-opnds (car p)) |
| 4291 |
(need-parms-opnds (cdr p))))) |
| 4292 |
(define (need-parms-loc p) |
| 4293 |
(if (null? p) |
| 4294 |
(need-parms-opnds parms) |
| 4295 |
(let ((loc (closure-parms-loc (car p)))) |
| 4296 |
(need-gvm-loc-opnd |
| 4297 |
loc |
| 4298 |
(need-gvm-loc loc (need-parms-loc (cdr p))))))) |
| 4299 |
(need-parms-loc parms))) |
| 4300 |
((ifjump) (need-gvm-opnds (ifjump-opnds gvm-instr) sn-rest)) |
| 4301 |
((jump) (need-gvm-opnd (jump-opnd gvm-instr) sn-rest)) |
| 4302 |
(else |
| 4303 |
(compiler-internal-error |
| 4304 |
"need-gvm-instr, unknown 'gvm-instr':" |
| 4305 |
gvm-instr)))) |
| 4306 |
(define (need-gvm-loc loc sn-rest) |
| 4307 |
(if (and loc (stk? loc) (>= (stk-num loc) sn-rest)) |
| 4308 |
(- (stk-num loc) 1) |
| 4309 |
sn-rest)) |
| 4310 |
(define (need-gvm-loc-opnd gvm-loc slots-needed) |
| 4311 |
(if (and gvm-loc (clo? gvm-loc)) |
| 4312 |
(need-gvm-opnd (clo-base gvm-loc) slots-needed) |
| 4313 |
slots-needed)) |
| 4314 |
(define (need-gvm-opnd gvm-opnd slots-needed) |
| 4315 |
(cond ((stk? gvm-opnd) (max (stk-num gvm-opnd) slots-needed)) |
| 4316 |
((clo? gvm-opnd) (need-gvm-opnd (clo-base gvm-opnd) slots-needed)) |
| 4317 |
(else slots-needed))) |
| 4318 |
(define (need-gvm-opnds gvm-opnds slots-needed) |
| 4319 |
(if (null? gvm-opnds) |
| 4320 |
slots-needed |
| 4321 |
(need-gvm-opnd |
| 4322 |
(car gvm-opnds) |
| 4323 |
(need-gvm-opnds (cdr gvm-opnds) slots-needed)))) |
| 4324 |
(define (write-bb bb port) |
| 4325 |
(write-gvm-instr (bb-label-instr bb) port) |
| 4326 |
(display " [precedents=" port) |
| 4327 |
(write (map bb-lbl-num (bb-precedents bb)) port) |
| 4328 |
(display "]" port) |
| 4329 |
(newline port) |
| 4330 |
(for-each |
| 4331 |
(lambda (x) (write-gvm-instr x port) (newline port)) |
| 4332 |
(bb-non-branch-instrs bb)) |
| 4333 |
(write-gvm-instr (bb-branch-instr bb) port)) |
| 4334 |
(define (write-bbs bbs port) |
| 4335 |
(for-each |
| 4336 |
(lambda (bb) |
| 4337 |
(if (= (bb-lbl-num bb) (bbs-entry-lbl-num bbs)) |
| 4338 |
(begin (display "**** Entry block:" port) (newline port))) |
| 4339 |
(write-bb bb port) |
| 4340 |
(newline port)) |
| 4341 |
(queue->list (bbs-bb-queue bbs)))) |
| 4342 |
(define (virtual.dump proc port) |
| 4343 |
(let ((proc-seen (queue-empty)) (proc-left (queue-empty))) |
| 4344 |
(define (scan-opnd gvm-opnd) |
| 4345 |
(cond ((obj? gvm-opnd) |
| 4346 |
(let ((val (obj-val gvm-opnd))) |
| 4347 |
(if (and (proc-obj? val) |
| 4348 |
(proc-obj-code val) |
| 4349 |
(not (memq val (queue->list proc-seen)))) |
| 4350 |
(begin |
| 4351 |
(queue-put! proc-seen val) |
| 4352 |
(queue-put! proc-left val))))) |
| 4353 |
((clo? gvm-opnd) (scan-opnd (clo-base gvm-opnd))))) |
| 4354 |
(define (dump-proc p) |
| 4355 |
(define (scan-code code) |
| 4356 |
(let ((gvm-instr (code-gvm-instr code))) |
| 4357 |
(write-gvm-instr gvm-instr port) |
| 4358 |
(newline port) |
| 4359 |
(case (gvm-instr-type gvm-instr) |
| 4360 |
((apply) |
| 4361 |
(for-each scan-opnd (apply-opnds gvm-instr)) |
| 4362 |
(if (apply-loc gvm-instr) (scan-opnd (apply-loc gvm-instr)))) |
| 4363 |
((copy) |
| 4364 |
(scan-opnd (copy-opnd gvm-instr)) |
| 4365 |
(scan-opnd (copy-loc gvm-instr))) |
| 4366 |
((close) |
| 4367 |
(for-each |
| 4368 |
(lambda (parms) |
| 4369 |
(scan-opnd (closure-parms-loc parms)) |
| 4370 |
(for-each scan-opnd (closure-parms-opnds parms))) |
| 4371 |
(close-parms gvm-instr))) |
| 4372 |
((ifjump) (for-each scan-opnd (ifjump-opnds gvm-instr))) |
| 4373 |
((jump) (scan-opnd (jump-opnd gvm-instr))) |
| 4374 |
(else '())))) |
| 4375 |
(if (proc-obj-primitive? p) |
| 4376 |
(display "**** #[primitive " port) |
| 4377 |
(display "**** #[procedure " port)) |
| 4378 |
(display (proc-obj-name p) port) |
| 4379 |
(display "] =" port) |
| 4380 |
(newline port) |
| 4381 |
(let loop ((l (bbs->code-list (proc-obj-code p))) |
| 4382 |
(prev-filename "") |
| 4383 |
(prev-line 0)) |
| 4384 |
(if (pair? l) |
| 4385 |
(let* ((code (car l)) |
| 4386 |
(instr (code-gvm-instr code)) |
| 4387 |
(src (comment-get (gvm-instr-comment instr) 'source)) |
| 4388 |
(loc (and src (source-locat src))) |
| 4389 |
(filename |
| 4390 |
(if (and loc (eq? (vector-ref loc 0) 'file)) |
| 4391 |
(vector-ref loc 1) |
| 4392 |
prev-filename)) |
| 4393 |
(line (if (and loc (eq? (vector-ref loc 0) 'file)) |
| 4394 |
(vector-ref loc 3) |
| 4395 |
prev-line))) |
| 4396 |
(if (or (not (string=? filename prev-filename)) |
| 4397 |
(not (= line prev-line))) |
| 4398 |
(begin |
| 4399 |
(display "#line " port) |
| 4400 |
(display line port) |
| 4401 |
(if (not (string=? filename prev-filename)) |
| 4402 |
(begin (display " " port) (write filename port))) |
| 4403 |
(newline port))) |
| 4404 |
(scan-code code) |
| 4405 |
(loop (cdr l) filename line)) |
| 4406 |
(newline port)))) |
| 4407 |
(scan-opnd (make-obj proc)) |
| 4408 |
(let loop () |
| 4409 |
(if (not (queue-empty? proc-left)) |
| 4410 |
(begin (dump-proc (queue-get! proc-left)) (loop)))))) |
| 4411 |
(define (write-gvm-instr gvm-instr port) |
| 4412 |
(define (write-closure-parms parms) |
| 4413 |
(display " " port) |
| 4414 |
(let ((len (+ 1 (write-gvm-opnd (closure-parms-loc parms) port)))) |
| 4415 |
(display " = (" port) |
| 4416 |
(let ((len (+ len (+ 4 (write-gvm-lbl (closure-parms-lbl parms) port))))) |
| 4417 |
(+ len |
| 4418 |
(write-terminated-opnd-list (closure-parms-opnds parms) port))))) |
| 4419 |
(define (write-terminated-opnd-list l port) |
| 4420 |
(let loop ((l l) (len 0)) |
| 4421 |
(if (pair? l) |
| 4422 |
(let ((opnd (car l))) |
| 4423 |
(display " " port) |
| 4424 |
(loop (cdr l) (+ len (+ 1 (write-gvm-opnd opnd port))))) |
| 4425 |
(begin (display ")" port) (+ len 1))))) |
| 4426 |
(define (write-param-pattern gvm-instr port) |
| 4427 |
(let ((len (if (not (= (label-entry-min gvm-instr) |
| 4428 |
(label-entry-nb-parms gvm-instr))) |
| 4429 |
(let ((len (write-returning-len |
| 4430 |
(label-entry-min gvm-instr) |
| 4431 |
port))) |
| 4432 |
(display "-" port) |
| 4433 |
(+ len 1)) |
| 4434 |
0))) |
| 4435 |
(let ((len (+ len |
| 4436 |
(write-returning-len |
| 4437 |
(label-entry-nb-parms gvm-instr) |
| 4438 |
port)))) |
| 4439 |
(if (label-entry-rest? gvm-instr) |
| 4440 |
(begin (display "+" port) (+ len 1)) |
| 4441 |
len)))) |
| 4442 |
(define (write-prim-applic prim opnds port) |
| 4443 |
(display "(" port) |
| 4444 |
(let ((len (+ 1 (display-returning-len (proc-obj-name prim) port)))) |
| 4445 |
(+ len (write-terminated-opnd-list opnds port)))) |
| 4446 |
(define (write-instr gvm-instr) |
| 4447 |
(case (gvm-instr-type gvm-instr) |
| 4448 |
((label) |
| 4449 |
(let ((len (write-gvm-lbl (label-lbl-num gvm-instr) port))) |
| 4450 |
(display " " port) |
| 4451 |
(let ((len (+ len |
| 4452 |
(+ 1 |
| 4453 |
(write-returning-len |
| 4454 |
(frame-size (gvm-instr-frame gvm-instr)) |
| 4455 |
port))))) |
| 4456 |
(case (label-type gvm-instr) |
| 4457 |
((simple) len) |
| 4458 |
((entry) |
| 4459 |
(if (label-entry-closed? gvm-instr) |
| 4460 |
(begin |
| 4461 |
(display " closure-entry-point " port) |
| 4462 |
(+ len (+ 21 (write-param-pattern gvm-instr port)))) |
| 4463 |
(begin |
| 4464 |
(display " entry-point " port) |
| 4465 |
(+ len (+ 13 (write-param-pattern gvm-instr port)))))) |
| 4466 |
((return) (display " return-point" port) (+ len 13)) |
| 4467 |
((task-entry) (display " task-entry-point" port) (+ len 17)) |
| 4468 |
((task-return) (display " task-return-point" port) (+ len 18)) |
| 4469 |
(else |
| 4470 |
(compiler-internal-error |
| 4471 |
"write-gvm-instr, unknown label type")))))) |
| 4472 |
((apply) |
| 4473 |
(display " " port) |
| 4474 |
(let ((len (+ 2 |
| 4475 |
(if (apply-loc gvm-instr) |
| 4476 |
(let ((len (write-gvm-opnd |
| 4477 |
(apply-loc gvm-instr) |
| 4478 |
port))) |
| 4479 |
(display " = " port) |
| 4480 |
(+ len 3)) |
| 4481 |
0)))) |
| 4482 |
(+ len |
| 4483 |
(write-prim-applic |
| 4484 |
(apply-prim gvm-instr) |
| 4485 |
(apply-opnds gvm-instr) |
| 4486 |
port)))) |
| 4487 |
((copy) |
| 4488 |
(display " " port) |
| 4489 |
(let ((len (+ 2 (write-gvm-opnd (copy-loc gvm-instr) port)))) |
| 4490 |
(display " = " port) |
| 4491 |
(+ len (+ 3 (write-gvm-opnd (copy-opnd gvm-instr) port))))) |
| 4492 |
((close) |
| 4493 |
(display " close" port) |
| 4494 |
(let ((len (+ 7 (write-closure-parms (car (close-parms gvm-instr)))))) |
| 4495 |
(let loop ((l (cdr (close-parms gvm-instr))) (len len)) |
| 4496 |
(if (pair? l) |
| 4497 |
(let ((x (car l))) |
| 4498 |
(display "," port) |
| 4499 |
(loop (cdr l) (+ len (+ 1 (write-closure-parms x))))) |
| 4500 |
len)))) |
| 4501 |
((ifjump) |
| 4502 |
(display " if " port) |
| 4503 |
(let ((len (+ 5 |
| 4504 |
(write-prim-applic |
| 4505 |
(ifjump-test gvm-instr) |
| 4506 |
(ifjump-opnds gvm-instr) |
| 4507 |
port)))) |
| 4508 |
(let ((len (+ len |
| 4509 |
(if (ifjump-poll? gvm-instr) |
| 4510 |
(begin (display " jump* " port) 7) |
| 4511 |
(begin (display " jump " port) 6))))) |
| 4512 |
(let ((len (+ len |
| 4513 |
(write-returning-len |
| 4514 |
(frame-size (gvm-instr-frame gvm-instr)) |
| 4515 |
port)))) |
| 4516 |
(display " " port) |
| 4517 |
(let ((len (+ len |
| 4518 |
(+ 1 |
| 4519 |
(write-gvm-lbl (ifjump-true gvm-instr) port))))) |
| 4520 |
(display " else " port) |
| 4521 |
(+ len (+ 6 (write-gvm-lbl (ifjump-false gvm-instr) port)))))))) |
| 4522 |
((jump) |
| 4523 |
(display " " port) |
| 4524 |
(let ((len (+ 2 |
| 4525 |
(if (jump-poll? gvm-instr) |
| 4526 |
(begin (display "jump* " port) 6) |
| 4527 |
(begin (display "jump " port) 5))))) |
| 4528 |
(let ((len (+ len |
| 4529 |
(write-returning-len |
| 4530 |
(frame-size (gvm-instr-frame gvm-instr)) |
| 4531 |
port)))) |
| 4532 |
(display " " port) |
| 4533 |
(let ((len (+ len |
| 4534 |
(+ 1 (write-gvm-opnd (jump-opnd gvm-instr) port))))) |
| 4535 |
(+ len |
| 4536 |
(if (jump-nb-args gvm-instr) |
| 4537 |
(begin |
| 4538 |
(display " " port) |
| 4539 |
(+ 1 |
| 4540 |
(write-returning-len (jump-nb-args gvm-instr) port))) |
| 4541 |
0)))))) |
| 4542 |
(else |
| 4543 |
(compiler-internal-error |
| 4544 |
"write-gvm-instr, unknown 'gvm-instr':" |
| 4545 |
gvm-instr)))) |
| 4546 |
(define (spaces n) |
| 4547 |
(if (> n 0) |
| 4548 |
(if (> n 7) |
| 4549 |
(begin (display " " port) (spaces (- n 8))) |
| 4550 |
(begin (display " " port) (spaces (- n 1)))))) |
| 4551 |
(let ((len (write-instr gvm-instr))) |
| 4552 |
(spaces (- 40 len)) |
| 4553 |
(display " " port) |
| 4554 |
(write-frame (gvm-instr-frame gvm-instr) port)) |
| 4555 |
(let ((x (gvm-instr-comment gvm-instr))) |
| 4556 |
(if x |
| 4557 |
(let ((y (comment-get x 'text))) |
| 4558 |
(if y (begin (display " ; " port) (display y port))))))) |
| 4559 |
(define (write-frame frame port) |
| 4560 |
(define (write-var var opnd sep) |
| 4561 |
(display sep port) |
| 4562 |
(write-gvm-opnd opnd port) |
| 4563 |
(if var |
| 4564 |
(begin |
| 4565 |
(display "=" port) |
| 4566 |
(cond ((eq? var closure-env-var) |
| 4567 |
(write (map (lambda (var) (var-name var)) |
| 4568 |
(frame-closed frame)) |
| 4569 |
port)) |
| 4570 |
((eq? var ret-var) (display "#" port)) |
| 4571 |
((temp-var? var) (display "." port)) |
| 4572 |
(else (write (var-name var) port)))))) |
| 4573 |
(define (live? var) |
| 4574 |
(let ((live (frame-live frame))) |
| 4575 |
(or (set-member? var live) |
| 4576 |
(and (eq? var closure-env-var) |
| 4577 |
(not (set-empty? |
| 4578 |
(set-intersection |
| 4579 |
live |
| 4580 |
(list->set (frame-closed frame))))))))) |
| 4581 |
(let loop1 ((i 1) (l (reverse (frame-slots frame))) (sep "; ")) |
| 4582 |
(if (pair? l) |
| 4583 |
(let ((var (car l))) |
| 4584 |
(write-var (if (live? var) var #f) (make-stk i) sep) |
| 4585 |
(loop1 (+ i 1) (cdr l) " ")) |
| 4586 |
(let loop2 ((i 0) (l (frame-regs frame)) (sep sep)) |
| 4587 |
(if (pair? l) |
| 4588 |
(let ((var (car l))) |
| 4589 |
(if (live? var) |
| 4590 |
(begin |
| 4591 |
(write-var var (make-reg i) sep) |
| 4592 |
(loop2 (+ i 1) (cdr l) " ")) |
| 4593 |
(loop2 (+ i 1) (cdr l) sep)))))))) |
| 4594 |
(define (write-gvm-opnd gvm-opnd port) |
| 4595 |
(define (write-opnd) |
| 4596 |
(cond ((reg? gvm-opnd) |
| 4597 |
(display "+" port) |
| 4598 |
(+ 1 (write-returning-len (reg-num gvm-opnd) port))) |
| 4599 |
((stk? gvm-opnd) |
| 4600 |
(display "-" port) |
| 4601 |
(+ 1 (write-returning-len (stk-num gvm-opnd) port))) |
| 4602 |
((glo? gvm-opnd) (write-returning-len (glo-name gvm-opnd) port)) |
| 4603 |
((clo? gvm-opnd) |
| 4604 |
(let ((len (write-gvm-opnd (clo-base gvm-opnd) port))) |
| 4605 |
(display "(" port) |
| 4606 |
(let ((len (+ len |
| 4607 |
(+ 1 |
| 4608 |
(write-returning-len |
| 4609 |
(clo-index gvm-opnd) |
| 4610 |
port))))) |
| 4611 |
(display ")" port) |
| 4612 |
(+ len 1)))) |
| 4613 |
((lbl? gvm-opnd) (write-gvm-lbl (lbl-num gvm-opnd) port)) |
| 4614 |
((obj? gvm-opnd) |
| 4615 |
(display "'" port) |
| 4616 |
(+ (write-gvm-obj (obj-val gvm-opnd) port) 1)) |
| 4617 |
(else |
| 4618 |
(compiler-internal-error |
| 4619 |
"write-gvm-opnd, unknown 'gvm-opnd':" |
| 4620 |
gvm-opnd)))) |
| 4621 |
(write-opnd)) |
| 4622 |
(define (write-gvm-lbl lbl port) |
| 4623 |
(display "#" port) |
| 4624 |
(+ (write-returning-len lbl port) 1)) |
| 4625 |
(define (write-gvm-obj val port) |
| 4626 |
(cond ((false-object? val) (display "#f" port) 2) |
| 4627 |
((undef-object? val) (display "#[undefined]" port) 12) |
| 4628 |
((proc-obj? val) |
| 4629 |
(if (proc-obj-primitive? val) |
| 4630 |
(display "#[primitive " port) |
| 4631 |
(display "#[procedure " port)) |
| 4632 |
(let ((len (display-returning-len (proc-obj-name val) port))) |
| 4633 |
(display "]" port) |
| 4634 |
(+ len 13))) |
| 4635 |
(else (write-returning-len val port)))) |
| 4636 |
(define (virtual.begin!) |
| 4637 |
(set! *opnd-table* (make-vector opnd-table-size)) |
| 4638 |
(set! *opnd-table-alloc* 0) |
| 4639 |
'()) |
| 4640 |
(define (virtual.end!) (set! *opnd-table* '()) '()) |
| 4641 |
(define (make-target version name) |
| 4642 |
(define current-target-version 4) |
| 4643 |
(if (not (= version current-target-version)) |
| 4644 |
(compiler-internal-error |
| 4645 |
"make-target, version of target package is not current" |
| 4646 |
name)) |
| 4647 |
(let ((x (make-vector 11))) (vector-set! x 1 name) x)) |
| 4648 |
(define (target-name x) (vector-ref x 1)) |
| 4649 |
(define (target-begin! x) (vector-ref x 2)) |
| 4650 |
(define (target-begin!-set! x y) (vector-set! x 2 y)) |
| 4651 |
(define (target-end! x) (vector-ref x 3)) |
| 4652 |
(define (target-end!-set! x y) (vector-set! x 3 y)) |
| 4653 |
(define (target-dump x) (vector-ref x 4)) |
| 4654 |
(define (target-dump-set! x y) (vector-set! x 4 y)) |
| 4655 |
(define (target-nb-regs x) (vector-ref x 5)) |
| 4656 |
(define (target-nb-regs-set! x y) (vector-set! x 5 y)) |
| 4657 |
(define (target-prim-info x) (vector-ref x 6)) |
| 4658 |
(define (target-prim-info-set! x y) (vector-set! x 6 y)) |
| 4659 |
(define (target-label-info x) (vector-ref x 7)) |
| 4660 |
(define (target-label-info-set! x y) (vector-set! x 7 y)) |
| 4661 |
(define (target-jump-info x) (vector-ref x 8)) |
| 4662 |
(define (target-jump-info-set! x y) (vector-set! x 8 y)) |
| 4663 |
(define (target-proc-result x) (vector-ref x 9)) |
| 4664 |
(define (target-proc-result-set! x y) (vector-set! x 9 y)) |
| 4665 |
(define (target-task-return x) (vector-ref x 10)) |
| 4666 |
(define (target-task-return-set! x y) (vector-set! x 10 y)) |
| 4667 |
(define targets-loaded '()) |
| 4668 |
(define (get-target name) |
| 4669 |
(let ((x (assq name targets-loaded))) |
| 4670 |
(if x (cdr x) (compiler-error "Target package is not available" name)))) |
| 4671 |
(define (put-target targ) |
| 4672 |
(let* ((name (target-name targ)) (x (assq name targets-loaded))) |
| 4673 |
(if x |
| 4674 |
(set-cdr! x targ) |
| 4675 |
(set! targets-loaded (cons (cons name targ) targets-loaded))) |
| 4676 |
'())) |
| 4677 |
(define (default-target) |
| 4678 |
(if (null? targets-loaded) |
| 4679 |
(compiler-error "No target package is available") |
| 4680 |
(car (car targets-loaded)))) |
| 4681 |
(define (select-target! name info-port) |
| 4682 |
(set! target (get-target name)) |
| 4683 |
((target-begin! target) info-port) |
| 4684 |
(set! target.dump (target-dump target)) |
| 4685 |
(set! target.nb-regs (target-nb-regs target)) |
| 4686 |
(set! target.prim-info (target-prim-info target)) |
| 4687 |
(set! target.label-info (target-label-info target)) |
| 4688 |
(set! target.jump-info (target-jump-info target)) |
| 4689 |
(set! target.proc-result (target-proc-result target)) |
| 4690 |
(set! target.task-return (target-task-return target)) |
| 4691 |
(set! **not-proc-obj (target.prim-info **not-sym)) |
| 4692 |
'()) |
| 4693 |
(define (unselect-target!) ((target-end! target)) '()) |
| 4694 |
(define target '()) |
| 4695 |
(define target.dump '()) |
| 4696 |
(define target.nb-regs '()) |
| 4697 |
(define target.prim-info '()) |
| 4698 |
(define target.label-info '()) |
| 4699 |
(define target.jump-info '()) |
| 4700 |
(define target.proc-result '()) |
| 4701 |
(define target.task-return '()) |
| 4702 |
(define **not-proc-obj '()) |
| 4703 |
(define (target.specialized-prim-info* name decl) |
| 4704 |
(let ((x (target.prim-info* name decl))) |
| 4705 |
(and x ((proc-obj-specialize x) decl)))) |
| 4706 |
(define (target.prim-info* name decl) |
| 4707 |
(and (if (standard-procedure name decl) |
| 4708 |
(standard-binding? name decl) |
| 4709 |
(extended-binding? name decl)) |
| 4710 |
(target.prim-info name))) |
| 4711 |
(define generic-sym (string->canonical-symbol "GENERIC")) |
| 4712 |
(define fixnum-sym (string->canonical-symbol "FIXNUM")) |
| 4713 |
(define flonum-sym (string->canonical-symbol "FLONUM")) |
| 4714 |
(define dummy2 |
| 4715 |
(begin |
| 4716 |
(define-namable-decl generic-sym 'arith) |
| 4717 |
(define-namable-decl fixnum-sym 'arith) |
| 4718 |
(define-namable-decl flonum-sym 'arith) |
| 4719 |
#f)) |
| 4720 |
(define (arith-implementation name decls) |
| 4721 |
(declaration-value 'arith name generic-sym decls)) |
| 4722 |
(define (cf source target-name . opts) |
| 4723 |
(let* ((dest (file-root source)) |
| 4724 |
(module-name (file-name dest)) |
| 4725 |
(info-port (if (memq 'verbose opts) (current-output-port) #f)) |
| 4726 |
(result (compile-program |
| 4727 |
(list **include-sym source) |
| 4728 |
(if target-name target-name (default-target)) |
| 4729 |
opts |
| 4730 |
module-name |
| 4731 |
dest |
| 4732 |
info-port))) |
| 4733 |
(if (and info-port (not (eq? info-port (current-output-port)))) |
| 4734 |
(close-output-port info-port)) |
| 4735 |
result)) |
| 4736 |
(define (ce source target-name . opts) |
| 4737 |
(let* ((dest "program") |
| 4738 |
(module-name "program") |
| 4739 |
(info-port (if (memq 'verbose opts) (current-output-port) #f)) |
| 4740 |
(result (compile-program |
| 4741 |
source |
| 4742 |
(if target-name target-name (default-target)) |
| 4743 |
opts |
| 4744 |
module-name |
| 4745 |
dest |
| 4746 |
info-port))) |
| 4747 |
(if (and info-port (not (eq? info-port (current-output-port)))) |
| 4748 |
(close-output-port info-port)) |
| 4749 |
result)) |
| 4750 |
(define wrap-program (lambda (program) program)) |
| 4751 |
(define (compile-program program target-name opts module-name dest info-port) |
| 4752 |
(define (compiler-body) |
| 4753 |
(if (not (valid-module-name? module-name)) |
| 4754 |
(compiler-error |
| 4755 |
"Invalid characters in file name (must be a symbol with no \"#\")") |
| 4756 |
(begin |
| 4757 |
(ptree.begin! info-port) |
| 4758 |
(virtual.begin!) |
| 4759 |
(select-target! target-name info-port) |
| 4760 |
(parse-program |
| 4761 |
(list (expression->source (wrap-program program) #f)) |
| 4762 |
(make-global-environment) |
| 4763 |
module-name |
| 4764 |
(lambda (lst env c-intf) |
| 4765 |
(let ((parsed-program |
| 4766 |
(map (lambda (x) (normalize-parse-tree (car x) (cdr x))) |
| 4767 |
lst))) |
| 4768 |
(if (memq 'expansion opts) |
| 4769 |
(let ((port (current-output-port))) |
| 4770 |
(display "Expansion:" port) |
| 4771 |
(newline port) |
| 4772 |
(let loop ((l parsed-program)) |
| 4773 |
(if (pair? l) |
| 4774 |
(let ((ptree (car l))) |
| 4775 |
(pp-expression |
| 4776 |
(parse-tree->expression ptree) |
| 4777 |
port) |
| 4778 |
(loop (cdr l))))) |
| 4779 |
(newline port))) |
| 4780 |
(let ((module-init-proc |
| 4781 |
(compile-parsed-program |
| 4782 |
module-name |
| 4783 |
parsed-program |
| 4784 |
env |
| 4785 |
c-intf |
| 4786 |
info-port))) |
| 4787 |
(if (memq 'report opts) (generate-report env)) |
| 4788 |
(if (memq 'gvm opts) |
| 4789 |
(let ((gvm-port |
| 4790 |
(open-output-file (string-append dest ".gvm")))) |
| 4791 |
(virtual.dump module-init-proc gvm-port) |
| 4792 |
(close-output-port gvm-port))) |
| 4793 |
(target.dump module-init-proc dest c-intf opts) |
| 4794 |
(dump-c-intf module-init-proc dest c-intf))))) |
| 4795 |
(unselect-target!) |
| 4796 |
(virtual.end!) |
| 4797 |
(ptree.end!) |
| 4798 |
#t))) |
| 4799 |
(let ((successful (with-exception-handling compiler-body))) |
| 4800 |
(if info-port |
| 4801 |
(if successful |
| 4802 |
(begin |
| 4803 |
(display "Compilation finished." info-port) |
| 4804 |
(newline info-port)) |
| 4805 |
(begin |
| 4806 |
(display "Compilation terminated abnormally." info-port) |
| 4807 |
(newline info-port)))) |
| 4808 |
successful)) |
| 4809 |
(define (valid-module-name? module-name) |
| 4810 |
(define (valid-char? c) |
| 4811 |
(and (not (memv c |
| 4812 |
'(#\# |
| 4813 |
#\; |
| 4814 |
#\( |
| 4815 |
#\) |
| 4816 |
#\space |
| 4817 |
#\[ |
| 4818 |
#\] |
| 4819 |
#\{ |
| 4820 |
#\} |
| 4821 |
#\" |
| 4822 |
#\' |
| 4823 |
#\` |
| 4824 |
#\,))) |
| 4825 |
(not (char-whitespace? c)))) |
| 4826 |
(let ((n (string-length module-name))) |
| 4827 |
(and (> n 0) |
| 4828 |
(not (string=? module-name ".")) |
| 4829 |
(not (string->number module-name 10)) |
| 4830 |
(let loop ((i 0)) |
| 4831 |
(if (< i n) |
| 4832 |
(if (valid-char? (string-ref module-name i)) (loop (+ i 1)) #f) |
| 4833 |
#t))))) |
| 4834 |
(define (dump-c-intf module-init-proc dest c-intf) |
| 4835 |
(let ((decls (c-intf-decls c-intf)) |
| 4836 |
(procs (c-intf-procs c-intf)) |
| 4837 |
(inits (c-intf-inits c-intf))) |
| 4838 |
(if (or (not (null? decls)) (not (null? procs)) (not (null? inits))) |
| 4839 |
(let* ((module-name (proc-obj-name module-init-proc)) |
| 4840 |
(filename (string-append dest ".c")) |
| 4841 |
(port (open-output-file filename))) |
| 4842 |
(display "/* File: \"" port) |
| 4843 |
(display filename port) |
| 4844 |
(display "\", C-interface file produced by Gambit " port) |
| 4845 |
(display compiler-version port) |
| 4846 |
(display " */" port) |
| 4847 |
(newline port) |
| 4848 |
(display "#define " port) |
| 4849 |
(display c-id-prefix port) |
| 4850 |
(display "MODULE_NAME \"" port) |
| 4851 |
(display module-name port) |
| 4852 |
(display "\"" port) |
| 4853 |
(newline port) |
| 4854 |
(display "#define " port) |
| 4855 |
(display c-id-prefix port) |
| 4856 |
(display "MODULE_LINKER " port) |
| 4857 |
(display c-id-prefix port) |
| 4858 |
(display (scheme-id->c-id module-name) port) |
| 4859 |
(newline port) |
| 4860 |
(display "#define " port) |
| 4861 |
(display c-id-prefix port) |
| 4862 |
(display "VERSION \"" port) |
| 4863 |
(display compiler-version port) |
| 4864 |
(display "\"" port) |
| 4865 |
(newline port) |
| 4866 |
(if (not (null? procs)) |
| 4867 |
(begin |
| 4868 |
(display "#define " port) |
| 4869 |
(display c-id-prefix port) |
| 4870 |
(display "C_PRC_COUNT " port) |
| 4871 |
(display (length procs) port) |
| 4872 |
(newline port))) |
| 4873 |
(display "#include \"gambit.h\"" port) |
| 4874 |
(newline port) |
| 4875 |
(display c-id-prefix port) |
| 4876 |
(display "BEGIN_MODULE" port) |
| 4877 |
(newline port) |
| 4878 |
(for-each |
| 4879 |
(lambda (x) |
| 4880 |
(let ((scheme-name (vector-ref x 0))) |
| 4881 |
(display c-id-prefix port) |
| 4882 |
(display "SUPPLY_PRM(" port) |
| 4883 |
(display c-id-prefix port) |
| 4884 |
(display "P_" port) |
| 4885 |
(display (scheme-id->c-id scheme-name) port) |
| 4886 |
(display ")" port) |
| 4887 |
(newline port))) |
| 4888 |
procs) |
| 4889 |
(newline port) |
| 4890 |
(for-each (lambda (x) (display x port) (newline port)) decls) |
| 4891 |
(if (not (null? procs)) |
| 4892 |
(begin |
| 4893 |
(for-each |
| 4894 |
(lambda (x) |
| 4895 |
(let ((scheme-name (vector-ref x 0)) |
| 4896 |
(c-name (vector-ref x 1)) |
| 4897 |
(arity (vector-ref x 2)) |
| 4898 |
(def (vector-ref x 3))) |
| 4899 |
(display c-id-prefix port) |
| 4900 |
(display "BEGIN_C_COD(" port) |
| 4901 |
(display c-name port) |
| 4902 |
(display "," port) |
| 4903 |
(display c-id-prefix port) |
| 4904 |
(display "P_" port) |
| 4905 |
(display (scheme-id->c-id scheme-name) port) |
| 4906 |
(display "," port) |
| 4907 |
(display arity port) |
| 4908 |
(display ")" port) |
| 4909 |
(newline port) |
| 4910 |
(display "#undef ___ARG1" port) |
| 4911 |
(newline port) |
| 4912 |
(display "#define ___ARG1 ___R1" port) |
| 4913 |
(newline port) |
| 4914 |
(display "#undef ___ARG2" port) |
| 4915 |
(newline port) |
| 4916 |
(display "#define ___ARG2 ___R2" port) |
| 4917 |
(newline port) |
| 4918 |
(display "#undef ___ARG3" port) |
| 4919 |
(newline port) |
| 4920 |
(display "#define ___ARG3 ___R3" port) |
| 4921 |
(newline port) |
| 4922 |
(display "#undef ___RESULT" port) |
| 4923 |
(newline port) |
| 4924 |
(display "#define ___RESULT ___R1" port) |
| 4925 |
(newline port) |
| 4926 |
(display def port) |
| 4927 |
(display c-id-prefix port) |
| 4928 |
(display "END_C_COD" port) |
| 4929 |
(newline port))) |
| 4930 |
procs) |
| 4931 |
(newline port) |
| 4932 |
(display c-id-prefix port) |
| 4933 |
(display "BEGIN_C_PRC" port) |
| 4934 |
(newline port) |
| 4935 |
(let loop ((i 0) (lst procs)) |
| 4936 |
(if (not (null? lst)) |
| 4937 |
(let* ((x (car lst)) |
| 4938 |
(scheme-name (vector-ref x 0)) |
| 4939 |
(c-name (vector-ref x 1)) |
| 4940 |
(arity (vector-ref x 2))) |
| 4941 |
(if (= i 0) (display " " port) (display "," port)) |
| 4942 |
(display c-id-prefix port) |
| 4943 |
(display "DEF_C_PRC(" port) |
| 4944 |
(display c-name port) |
| 4945 |
(display "," port) |
| 4946 |
(display c-id-prefix port) |
| 4947 |
(display "P_" port) |
| 4948 |
(display (scheme-id->c-id scheme-name) port) |
| 4949 |
(display "," port) |
| 4950 |
(display arity port) |
| 4951 |
(display ")" port) |
| 4952 |
(newline port) |
| 4953 |
(loop (+ i 1) (cdr lst))))) |
| 4954 |
(display c-id-prefix port) |
| 4955 |
(display "END_C_PRC" port) |
| 4956 |
(newline port))) |
| 4957 |
(newline port) |
| 4958 |
(display c-id-prefix port) |
| 4959 |
(display "BEGIN_PRM" port) |
| 4960 |
(newline port) |
| 4961 |
(for-each (lambda (x) (display x port) (newline port)) inits) |
| 4962 |
(display c-id-prefix port) |
| 4963 |
(display "END_PRM" port) |
| 4964 |
(newline port) |
| 4965 |
(close-output-port port))))) |
| 4966 |
(define (generate-report env) |
| 4967 |
(let ((vars (sort-variables (env-global-variables env))) |
| 4968 |
(decl (env-declarations env))) |
| 4969 |
(define (report title pred? vars wrote-something?) |
| 4970 |
(if (pair? vars) |
| 4971 |
(let ((var (car vars))) |
| 4972 |
(if (pred? var) |
| 4973 |
(begin |
| 4974 |
(if (not wrote-something?) |
| 4975 |
(begin (display " ") (display title) (newline))) |
| 4976 |
(let loop1 ((l (var-refs var)) (r? #f) (c? #f)) |
| 4977 |
(if (pair? l) |
| 4978 |
(let* ((x (car l)) (y (node-parent x))) |
| 4979 |
(if (and y (app? y) (eq? x (app-oper y))) |
| 4980 |
(loop1 (cdr l) r? #t) |
| 4981 |
(loop1 (cdr l) #t c?))) |
| 4982 |
(let loop2 ((l (var-sets var)) (d? #f) (a? #f)) |
| 4983 |
(if (pair? l) |
| 4984 |
(if (set? (car l)) |
| 4985 |
(loop2 (cdr l) d? #t) |
| 4986 |
(loop2 (cdr l) #t a?)) |
| 4987 |
(begin |
| 4988 |
(display " [") |
| 4989 |
(if d? (display "D") (display " ")) |
| 4990 |
(if a? (display "A") (display " ")) |
| 4991 |
(if r? (display "R") (display " ")) |
| 4992 |
(if c? (display "C") (display " ")) |
| 4993 |
(display "] ") |
| 4994 |
(display (var-name var)) |
| 4995 |
(newline)))))) |
| 4996 |
(report title pred? (cdr vars) #t)) |
| 4997 |
(cons (car vars) |
| 4998 |
(report title pred? (cdr vars) wrote-something?)))) |
| 4999 |
(begin (if wrote-something? (newline)) '()))) |
| 5000 |
(display "Global variable usage:") |
| 5001 |
(newline) |
| 5002 |
(newline) |
| 5003 |
(report "OTHERS" |
| 5004 |
(lambda (x) #t) |
| 5005 |
(report "EXTENDED" |
| 5006 |
(lambda (x) (target.prim-info (var-name x))) |
| 5007 |
(report "STANDARD" |
| 5008 |
(lambda (x) (standard-procedure (var-name x) decl)) |
| 5009 |
vars |
| 5010 |
#f) |
| 5011 |
#f) |
| 5012 |
#f))) |
| 5013 |
(define (compile-parsed-program module-name program env c-intf info-port) |
| 5014 |
(if info-port (display "Compiling:" info-port)) |
| 5015 |
(set! trace-indentation 0) |
| 5016 |
(set! *bbs* (make-bbs)) |
| 5017 |
(set! *global-env* env) |
| 5018 |
(set! proc-queue '()) |
| 5019 |
(set! constant-vars '()) |
| 5020 |
(set! known-procs '()) |
| 5021 |
(restore-context (make-context 0 '() (list ret-var) '() (entry-poll) #f)) |
| 5022 |
(let* ((entry-lbl (bbs-new-lbl! *bbs*)) |
| 5023 |
(body-lbl (bbs-new-lbl! *bbs*)) |
| 5024 |
(frame (current-frame ret-var-set)) |
| 5025 |
(comment (if (null? program) #f (source-comment (car program))))) |
| 5026 |
(bbs-entry-lbl-num-set! *bbs* entry-lbl) |
| 5027 |
(set! entry-bb |
| 5028 |
(make-bb (make-label-entry entry-lbl 0 0 #f #f frame comment) *bbs*)) |
| 5029 |
(bb-put-branch! entry-bb (make-jump (make-lbl body-lbl) #f #f frame #f)) |
| 5030 |
(set! *bb* (make-bb (make-label-simple body-lbl frame comment) *bbs*)) |
| 5031 |
(let loop1 ((l (c-intf-procs c-intf))) |
| 5032 |
(if (not (null? l)) |
| 5033 |
(let* ((x (car l)) |
| 5034 |
(name (vector-ref x 0)) |
| 5035 |
(sym (string->canonical-symbol name)) |
| 5036 |
(var (env-lookup-global-var *global-env* sym))) |
| 5037 |
(add-constant-var |
| 5038 |
var |
| 5039 |
(make-obj (make-proc-obj name #t #f 0 #t '() '(#f)))) |
| 5040 |
(loop1 (cdr l))))) |
| 5041 |
(let loop2 ((l program)) |
| 5042 |
(if (not (null? l)) |
| 5043 |
(let ((node (car l))) |
| 5044 |
(if (def? node) |
| 5045 |
(let* ((var (def-var node)) (val (global-val var))) |
| 5046 |
(if (and val (prc? val)) |
| 5047 |
(add-constant-var |
| 5048 |
var |
| 5049 |
(make-obj |
| 5050 |
(make-proc-obj |
| 5051 |
(symbol->string (var-name var)) |
| 5052 |
#t |
| 5053 |
#f |
| 5054 |
(call-pattern val) |
| 5055 |
#t |
| 5056 |
'() |
| 5057 |
'(#f))))))) |
| 5058 |
(loop2 (cdr l))))) |
| 5059 |
(let loop3 ((l program)) |
| 5060 |
(if (null? l) |
| 5061 |
(let ((ret-opnd (var->opnd ret-var))) |
| 5062 |
(seal-bb #t 'return) |
| 5063 |
(dealloc-slots nb-slots) |
| 5064 |
(bb-put-branch! |
| 5065 |
*bb* |
| 5066 |
(make-jump ret-opnd #f #f (current-frame (set-empty)) #f))) |
| 5067 |
(let ((node (car l))) |
| 5068 |
(if (def? node) |
| 5069 |
(begin |
| 5070 |
(gen-define (def-var node) (def-val node) info-port) |
| 5071 |
(loop3 (cdr l))) |
| 5072 |
(if (null? (cdr l)) |
| 5073 |
(gen-node node ret-var-set 'tail) |
| 5074 |
(begin |
| 5075 |
(gen-node node ret-var-set 'need) |
| 5076 |
(loop3 (cdr l)))))))) |
| 5077 |
(let loop4 () |
| 5078 |
(if (pair? proc-queue) |
| 5079 |
(let ((x (car proc-queue))) |
| 5080 |
(set! proc-queue (cdr proc-queue)) |
| 5081 |
(gen-proc (car x) (cadr x) (caddr x) info-port) |
| 5082 |
(trace-unindent info-port) |
| 5083 |
(loop4)))) |
| 5084 |
(if info-port (begin (newline info-port) (newline info-port))) |
| 5085 |
(bbs-purify! *bbs*) |
| 5086 |
(let ((proc (make-proc-obj |
| 5087 |
(string-append "#!" module-name) |
| 5088 |
#t |
| 5089 |
*bbs* |
| 5090 |
'(0) |
| 5091 |
#t |
| 5092 |
'() |
| 5093 |
'(#f)))) |
| 5094 |
(set! *bb* '()) |
| 5095 |
(set! *bbs* '()) |
| 5096 |
(set! *global-env* '()) |
| 5097 |
(set! proc-queue '()) |
| 5098 |
(set! constant-vars '()) |
| 5099 |
(set! known-procs '()) |
| 5100 |
(clear-context) |
| 5101 |
proc))) |
| 5102 |
(define *bb* '()) |
| 5103 |
(define *bbs* '()) |
| 5104 |
(define *global-env* '()) |
| 5105 |
(define proc-queue '()) |
| 5106 |
(define constant-vars '()) |
| 5107 |
(define known-procs '()) |
| 5108 |
(define trace-indentation '()) |
| 5109 |
(define (trace-indent info-port) |
| 5110 |
(set! trace-indentation (+ trace-indentation 1)) |
| 5111 |
(if info-port |
| 5112 |
(begin |
| 5113 |
(newline info-port) |
| 5114 |
(let loop ((i trace-indentation)) |
| 5115 |
(if (> i 0) (begin (display " " info-port) (loop (- i 1)))))))) |
| 5116 |
(define (trace-unindent info-port) |
| 5117 |
(set! trace-indentation (- trace-indentation 1))) |
| 5118 |
(define (gen-define var node info-port) |
| 5119 |
(if (prc? node) |
| 5120 |
(let* ((p-bbs *bbs*) |
| 5121 |
(p-bb *bb*) |
| 5122 |
(p-proc-queue proc-queue) |
| 5123 |
(p-known-procs known-procs) |
| 5124 |
(p-context (current-context)) |
| 5125 |
(bbs (make-bbs)) |
| 5126 |
(lbl1 (bbs-new-lbl! bbs)) |
| 5127 |
(lbl2 (bbs-new-lbl! bbs)) |
| 5128 |
(context (entry-context node '())) |
| 5129 |
(frame (context->frame |
| 5130 |
context |
| 5131 |
(set-union (free-variables (prc-body node)) ret-var-set))) |
| 5132 |
(bb1 (make-bb (make-label-entry |
| 5133 |
lbl1 |
| 5134 |
(length (prc-parms node)) |
| 5135 |
(prc-min node) |
| 5136 |
(prc-rest node) |
| 5137 |
#f |
| 5138 |
frame |
| 5139 |
(source-comment node)) |
| 5140 |
bbs)) |
| 5141 |
(bb2 (make-bb (make-label-simple lbl2 frame (source-comment node)) |
| 5142 |
bbs))) |
| 5143 |
(define (do-body) |
| 5144 |
(gen-proc node bb2 context info-port) |
| 5145 |
(let loop () |
| 5146 |
(if (pair? proc-queue) |
| 5147 |
(let ((x (car proc-queue))) |
| 5148 |
(set! proc-queue (cdr proc-queue)) |
| 5149 |
(gen-proc (car x) (cadr x) (caddr x) info-port) |
| 5150 |
(trace-unindent info-port) |
| 5151 |
(loop)))) |
| 5152 |
(trace-unindent info-port) |
| 5153 |
(bbs-purify! *bbs*)) |
| 5154 |
(context-entry-bb-set! context bb1) |
| 5155 |
(bbs-entry-lbl-num-set! bbs lbl1) |
| 5156 |
(bb-put-branch! bb1 (make-jump (make-lbl lbl2) #f #f frame #f)) |
| 5157 |
(set! *bbs* bbs) |
| 5158 |
(set! proc-queue '()) |
| 5159 |
(set! known-procs '()) |
| 5160 |
(if (constant-var? var) |
| 5161 |
(let-constant-var |
| 5162 |
var |
| 5163 |
(make-lbl lbl1) |
| 5164 |
(lambda () (add-known-proc lbl1 node) (do-body))) |
| 5165 |
(do-body)) |
| 5166 |
(set! *bbs* p-bbs) |
| 5167 |
(set! *bb* p-bb) |
| 5168 |
(set! proc-queue p-proc-queue) |
| 5169 |
(set! known-procs p-known-procs) |
| 5170 |
(restore-context p-context) |
| 5171 |
(let* ((x (assq var constant-vars)) |
| 5172 |
(proc (if x |
| 5173 |
(let ((p (cdr x))) |
| 5174 |
(proc-obj-code-set! (obj-val p) bbs) |
| 5175 |
p) |
| 5176 |
(make-obj |
| 5177 |
(make-proc-obj |
| 5178 |
(symbol->string (var-name var)) |
| 5179 |
#f |
| 5180 |
bbs |
| 5181 |
(call-pattern node) |
| 5182 |
#t |
| 5183 |
'() |
| 5184 |
'(#f)))))) |
| 5185 |
(put-copy |
| 5186 |
proc |
| 5187 |
(make-glo (var-name var)) |
| 5188 |
#f |
| 5189 |
ret-var-set |
| 5190 |
(source-comment node)))) |
| 5191 |
(put-copy |
| 5192 |
(gen-node node ret-var-set 'need) |
| 5193 |
(make-glo (var-name var)) |
| 5194 |
#f |
| 5195 |
ret-var-set |
| 5196 |
(source-comment node)))) |
| 5197 |
(define (call-pattern node) |
| 5198 |
(make-pattern (prc-min node) (length (prc-parms node)) (prc-rest node))) |
| 5199 |
(define (make-context nb-slots slots regs closed poll entry-bb) |
| 5200 |
(vector nb-slots slots regs closed poll entry-bb)) |
| 5201 |
(define (context-nb-slots x) (vector-ref x 0)) |
| 5202 |
(define (context-slots x) (vector-ref x 1)) |
| 5203 |
(define (context-regs x) (vector-ref x 2)) |
| 5204 |
(define (context-closed x) (vector-ref x 3)) |
| 5205 |
(define (context-poll x) (vector-ref x 4)) |
| 5206 |
(define (context-entry-bb x) (vector-ref x 5)) |
| 5207 |
(define (context-entry-bb-set! x y) (vector-set! x 5 y)) |
| 5208 |
(define nb-slots '()) |
| 5209 |
(define slots '()) |
| 5210 |
(define regs '()) |
| 5211 |
(define closed '()) |
| 5212 |
(define poll '()) |
| 5213 |
(define entry-bb '()) |
| 5214 |
(define (restore-context context) |
| 5215 |
(set! nb-slots (context-nb-slots context)) |
| 5216 |
(set! slots (context-slots context)) |
| 5217 |
(set! regs (context-regs context)) |
| 5218 |
(set! closed (context-closed context)) |
| 5219 |
(set! poll (context-poll context)) |
| 5220 |
(set! entry-bb (context-entry-bb context))) |
| 5221 |
(define (clear-context) |
| 5222 |
(restore-context (make-context '() '() '() '() '() '()))) |
| 5223 |
(define (current-context) |
| 5224 |
(make-context nb-slots slots regs closed poll entry-bb)) |
| 5225 |
(define (current-frame live) (make-frame nb-slots slots regs closed live)) |
| 5226 |
(define (context->frame context live) |
| 5227 |
(make-frame |
| 5228 |
(context-nb-slots context) |
| 5229 |
(context-slots context) |
| 5230 |
(context-regs context) |
| 5231 |
(context-closed context) |
| 5232 |
live)) |
| 5233 |
(define (make-poll since-entry? delta) (cons since-entry? delta)) |
| 5234 |
(define (poll-since-entry? x) (car x)) |
| 5235 |
(define (poll-delta x) (cdr x)) |
| 5236 |
(define (entry-poll) (make-poll #f (- poll-period poll-head))) |
| 5237 |
(define (return-poll poll) |
| 5238 |
(let ((delta (poll-delta poll))) |
| 5239 |
(make-poll (poll-since-entry? poll) (+ poll-head (max delta poll-tail))))) |
| 5240 |
(define (poll-merge poll other-poll) |
| 5241 |
(make-poll |
| 5242 |
(or (poll-since-entry? poll) (poll-since-entry? other-poll)) |
| 5243 |
(max (poll-delta poll) (poll-delta other-poll)))) |
| 5244 |
(define poll-period 90) |
| 5245 |
(define poll-head 15) |
| 5246 |
(define poll-tail 15) |
| 5247 |
(define (entry-context proc closed) |
| 5248 |
(define (empty-vars-list n) |
| 5249 |
(if (> n 0) (cons empty-var (empty-vars-list (- n 1))) '())) |
| 5250 |
(let* ((parms (prc-parms proc)) |
| 5251 |
(pc (target.label-info |
| 5252 |
(prc-min proc) |
| 5253 |
(length parms) |
| 5254 |
(prc-rest proc) |
| 5255 |
(not (null? closed)))) |
| 5256 |
(fs (pcontext-fs pc)) |
| 5257 |
(slots-list (empty-vars-list fs)) |
| 5258 |
(regs-list (empty-vars-list target.nb-regs))) |
| 5259 |
(define (assign-var-to-loc var loc) |
| 5260 |
(let ((x (cond ((reg? loc) |
| 5261 |
(let ((i (reg-num loc))) |
| 5262 |
(if (<= i target.nb-regs) |
| 5263 |
(nth-after regs-list i) |
| 5264 |
(compiler-internal-error |
| 5265 |
"entry-context, reg out of bound in back-end's pcontext")))) |
| 5266 |
((stk? loc) |
| 5267 |
(let ((i (stk-num loc))) |
| 5268 |
(if (<= i fs) |
| 5269 |
(nth-after slots-list (- fs i)) |
| 5270 |
(compiler-internal-error |
| 5271 |
"entry-context, stk out of bound in back-end's pcontext")))) |
| 5272 |
(else |
| 5273 |
(compiler-internal-error |
| 5274 |
"entry-context, loc other than reg or stk in back-end's pcontext"))))) |
| 5275 |
(if (eq? (car x) empty-var) |
| 5276 |
(set-car! x var) |
| 5277 |
(compiler-internal-error |
| 5278 |
"entry-context, duplicate location in back-end's pcontext")))) |
| 5279 |
(let loop ((l (pcontext-map pc))) |
| 5280 |
(if (not (null? l)) |
| 5281 |
(let* ((couple (car l)) (name (car couple)) (loc (cdr couple))) |
| 5282 |
(cond ((eq? name 'return) (assign-var-to-loc ret-var loc)) |
| 5283 |
((eq? name 'closure-env) |
| 5284 |
(assign-var-to-loc closure-env-var loc)) |
| 5285 |
(else (assign-var-to-loc (list-ref parms (- name 1)) loc))) |
| 5286 |
(loop (cdr l))))) |
| 5287 |
(make-context fs slots-list regs-list closed (entry-poll) #f))) |
| 5288 |
(define (get-var opnd) |
| 5289 |
(cond ((glo? opnd) (env-lookup-global-var *global-env* (glo-name opnd))) |
| 5290 |
((reg? opnd) (list-ref regs (reg-num opnd))) |
| 5291 |
((stk? opnd) (list-ref slots (- nb-slots (stk-num opnd)))) |
| 5292 |
(else |
| 5293 |
(compiler-internal-error |
| 5294 |
"get-var, location must be global, register or stack slot")))) |
| 5295 |
(define (put-var opnd new) |
| 5296 |
(define (put-v opnd new) |
| 5297 |
(cond ((reg? opnd) (set! regs (replace-nth regs (reg-num opnd) new))) |
| 5298 |
((stk? opnd) |
| 5299 |
(set! slots (replace-nth slots (- nb-slots (stk-num opnd)) new))) |
| 5300 |
(else |
| 5301 |
(compiler-internal-error |
| 5302 |
"put-var, location must be register or stack slot, for var:" |
| 5303 |
(var-name new))))) |
| 5304 |
(if (eq? new ret-var) |
| 5305 |
(let ((x (var->opnd ret-var))) (and x (put-v x empty-var)))) |
| 5306 |
(put-v opnd new)) |
| 5307 |
(define (flush-regs) (set! regs '())) |
| 5308 |
(define (push-slot) |
| 5309 |
(set! nb-slots (+ nb-slots 1)) |
| 5310 |
(set! slots (cons empty-var slots))) |
| 5311 |
(define (dealloc-slots n) |
| 5312 |
(set! nb-slots (- nb-slots n)) |
| 5313 |
(set! slots (nth-after slots n))) |
| 5314 |
(define (pop-slot) (dealloc-slots 1)) |
| 5315 |
(define (replace-nth l i v) |
| 5316 |
(if (null? l) |
| 5317 |
(if (= i 0) (list v) (cons empty-var (replace-nth l (- i 1) v))) |
| 5318 |
(if (= i 0) |
| 5319 |
(cons v (cdr l)) |
| 5320 |
(cons (car l) (replace-nth (cdr l) (- i 1) v))))) |
| 5321 |
(define (live-vars live) |
| 5322 |
(if (not (set-empty? (set-intersection live (list->set closed)))) |
| 5323 |
(set-adjoin live closure-env-var) |
| 5324 |
live)) |
| 5325 |
(define (dead-slots live) |
| 5326 |
(let ((live-v (live-vars live))) |
| 5327 |
(define (loop s l i) |
| 5328 |
(cond ((null? l) (list->set (reverse s))) |
| 5329 |
((set-member? (car l) live-v) (loop s (cdr l) (- i 1))) |
| 5330 |
(else (loop (cons i s) (cdr l) (- i 1))))) |
| 5331 |
(loop '() slots nb-slots))) |
| 5332 |
(define (live-slots live) |
| 5333 |
(let ((live-v (live-vars live))) |
| 5334 |
(define (loop s l i) |
| 5335 |
(cond ((null? l) (list->set (reverse s))) |
| 5336 |
((set-member? (car l) live-v) (loop (cons i s) (cdr l) (- i 1))) |
| 5337 |
(else (loop s (cdr l) (- i 1))))) |
| 5338 |
(loop '() slots nb-slots))) |
| 5339 |
(define (dead-regs live) |
| 5340 |
(let ((live-v (live-vars live))) |
| 5341 |
(define (loop s l i) |
| 5342 |
(cond ((>= i target.nb-regs) (list->set (reverse s))) |
| 5343 |
((null? l) (loop (cons i s) l (+ i 1))) |
| 5344 |
((and (set-member? (car l) live-v) (not (memq (car l) slots))) |
| 5345 |
(loop s (cdr l) (+ i 1))) |
| 5346 |
(else (loop (cons i s) (cdr l) (+ i 1))))) |
| 5347 |
(loop '() regs 0))) |
| 5348 |
(define (live-regs live) |
| 5349 |
(let ((live-v (live-vars live))) |
| 5350 |
(define (loop s l i) |
| 5351 |
(cond ((null? l) (list->set (reverse s))) |
| 5352 |
((and (set-member? (car l) live-v) (not (memq (car l) slots))) |
| 5353 |
(loop (cons i s) (cdr l) (+ i 1))) |
| 5354 |
(else (loop s (cdr l) (+ i 1))))) |
| 5355 |
(loop '() regs 0))) |
| 5356 |
(define (lowest-dead-slot live) |
| 5357 |
(make-stk (or (lowest (dead-slots live)) (+ nb-slots 1)))) |
| 5358 |
(define (highest-live-slot live) (make-stk (or (highest (live-slots live)) 0))) |
| 5359 |
(define (lowest-dead-reg live) |
| 5360 |
(let ((x (lowest (set-remove (dead-regs live) 0)))) (if x (make-reg x) #f))) |
| 5361 |
(define (highest-dead-reg live) |
| 5362 |
(let ((x (highest (dead-regs live)))) (if x (make-reg x) #f))) |
| 5363 |
(define (highest set) (if (set-empty? set) #f (apply max (set->list set)))) |
| 5364 |
(define (lowest set) (if (set-empty? set) #f (apply min (set->list set)))) |
| 5365 |
(define (above set n) (set-keep (lambda (x) (> x n)) set)) |
| 5366 |
(define (below set n) (set-keep (lambda (x) (< x n)) set)) |
| 5367 |
(define (var->opnd var) |
| 5368 |
(let ((x (assq var constant-vars))) |
| 5369 |
(if x |
| 5370 |
(cdr x) |
| 5371 |
(if (global? var) |
| 5372 |
(make-glo (var-name var)) |
| 5373 |
(let ((n (pos-in-list var regs))) |
| 5374 |
(if n |
| 5375 |
(make-reg n) |
| 5376 |
(let ((n (pos-in-list var slots))) |
| 5377 |
(if n |
| 5378 |
(make-stk (- nb-slots n)) |
| 5379 |
(let ((n (pos-in-list var closed))) |
| 5380 |
(if n |
| 5381 |
(make-clo (var->opnd closure-env-var) (+ n 1)) |
| 5382 |
(compiler-internal-error |
| 5383 |
"var->opnd, variable is not accessible:" |
| 5384 |
(var-name var)))))))))))) |
| 5385 |
(define (source-comment node) |
| 5386 |
(let ((x (make-comment))) (comment-put! x 'source (node-source node)) x)) |
| 5387 |
(define (sort-variables lst) |
| 5388 |
(sort-list |
| 5389 |
lst |
| 5390 |
(lambda (x y) |
| 5391 |
(string<? (symbol->string (var-name x)) (symbol->string (var-name y)))))) |
| 5392 |
(define (add-constant-var var opnd) |
| 5393 |
(set! constant-vars (cons (cons var opnd) constant-vars))) |
| 5394 |
(define (let-constant-var var opnd thunk) |
| 5395 |
(let* ((x (assq var constant-vars)) (temp (cdr x))) |
| 5396 |
(set-cdr! x opnd) |
| 5397 |
(thunk) |
| 5398 |
(set-cdr! x temp))) |
| 5399 |
(define (constant-var? var) (assq var constant-vars)) |
| 5400 |
(define (not-constant-var? var) (not (constant-var? var))) |
| 5401 |
(define (add-known-proc label proc) |
| 5402 |
(set! known-procs (cons (cons label proc) known-procs))) |
| 5403 |
(define (gen-proc proc bb context info-port) |
| 5404 |
(trace-indent info-port) |
| 5405 |
(if info-port |
| 5406 |
(if (prc-name proc) |
| 5407 |
(display (prc-name proc) info-port) |
| 5408 |
(display "\"unknown\"" info-port))) |
| 5409 |
(let ((lbl (bb-lbl-num bb)) |
| 5410 |
(live (set-union (free-variables (prc-body proc)) ret-var-set))) |
| 5411 |
(set! *bb* bb) |
| 5412 |
(restore-context context) |
| 5413 |
(gen-node (prc-body proc) ret-var-set 'tail))) |
| 5414 |
(define (schedule-gen-proc proc closed-list) |
| 5415 |
(let* ((lbl1 (bbs-new-lbl! *bbs*)) |
| 5416 |
(lbl2 (bbs-new-lbl! *bbs*)) |
| 5417 |
(context (entry-context proc closed-list)) |
| 5418 |
(frame (context->frame |
| 5419 |
context |
| 5420 |
(set-union (free-variables (prc-body proc)) ret-var-set))) |
| 5421 |
(bb1 (make-bb (make-label-entry |
| 5422 |
lbl1 |
| 5423 |
(length (prc-parms proc)) |
| 5424 |
(prc-min proc) |
| 5425 |
(prc-rest proc) |
| 5426 |
(not (null? closed-list)) |
| 5427 |
frame |
| 5428 |
(source-comment proc)) |
| 5429 |
*bbs*)) |
| 5430 |
(bb2 (make-bb (make-label-simple lbl2 frame (source-comment proc)) |
| 5431 |
*bbs*))) |
| 5432 |
(context-entry-bb-set! context bb1) |
| 5433 |
(bb-put-branch! bb1 (make-jump (make-lbl lbl2) #f #f frame #f)) |
| 5434 |
(set! proc-queue (cons (list proc bb2 context) proc-queue)) |
| 5435 |
(make-lbl lbl1))) |
| 5436 |
(define (gen-node node live why) |
| 5437 |
(cond ((cst? node) (gen-return (make-obj (cst-val node)) why node)) |
| 5438 |
((ref? node) |
| 5439 |
(let* ((var (ref-var node)) (name (var-name var))) |
| 5440 |
(gen-return |
| 5441 |
(cond ((eq? why 'side) (make-obj undef-object)) |
| 5442 |
((global? var) |
| 5443 |
(let ((prim (target.prim-info* name (node-decl node)))) |
| 5444 |
(if prim (make-obj prim) (var->opnd var)))) |
| 5445 |
(else (var->opnd var))) |
| 5446 |
why |
| 5447 |
node))) |
| 5448 |
((set? node) |
| 5449 |
(let* ((src (gen-node |
| 5450 |
(set-val node) |
| 5451 |
(set-adjoin live (set-var node)) |
| 5452 |
'keep)) |
| 5453 |
(dst (var->opnd (set-var node)))) |
| 5454 |
(put-copy src dst #f live (source-comment node)) |
| 5455 |
(gen-return (make-obj undef-object) why node))) |
| 5456 |
((def? node) |
| 5457 |
(compiler-internal-error |
| 5458 |
"gen-node, 'def' node not at root of parse tree")) |
| 5459 |
((tst? node) (gen-tst node live why)) |
| 5460 |
((conj? node) (gen-conj/disj node live why)) |
| 5461 |
((disj? node) (gen-conj/disj node live why)) |
| 5462 |
((prc? node) |
| 5463 |
(let* ((closed (not-constant-closed-vars node)) |
| 5464 |
(closed-list (sort-variables (set->list closed))) |
| 5465 |
(proc-lbl (schedule-gen-proc node closed-list))) |
| 5466 |
(let ((opnd (if (null? closed-list) |
| 5467 |
(begin |
| 5468 |
(add-known-proc (lbl-num proc-lbl) node) |
| 5469 |
proc-lbl) |
| 5470 |
(begin |
| 5471 |
(dealloc-slots |
| 5472 |
(- nb-slots |
| 5473 |
(stk-num (highest-live-slot |
| 5474 |
(set-union closed live))))) |
| 5475 |
(push-slot) |
| 5476 |
(let ((slot (make-stk nb-slots)) |
| 5477 |
(var (make-temp-var 'closure))) |
| 5478 |
(put-var slot var) |
| 5479 |
(bb-put-non-branch! |
| 5480 |
*bb* |
| 5481 |
(make-close |
| 5482 |
(list (make-closure-parms |
| 5483 |
slot |
| 5484 |
(lbl-num proc-lbl) |
| 5485 |
(map var->opnd closed-list))) |
| 5486 |
(current-frame (set-adjoin live var)) |
| 5487 |
(source-comment node))) |
| 5488 |
slot))))) |
| 5489 |
(gen-return opnd why node)))) |
| 5490 |
((app? node) (gen-call node live why)) |
| 5491 |
((fut? node) (gen-fut node live why)) |
| 5492 |
(else |
| 5493 |
(compiler-internal-error |
| 5494 |
"gen-node, unknown parse tree node type:" |
| 5495 |
node)))) |
| 5496 |
(define (gen-return opnd why node) |
| 5497 |
(cond ((eq? why 'tail) |
| 5498 |
(let ((var (make-temp-var 'result))) |
| 5499 |
(put-copy |
| 5500 |
opnd |
| 5501 |
target.proc-result |
| 5502 |
var |
| 5503 |
ret-var-set |
| 5504 |
(source-comment node)) |
| 5505 |
(let ((ret-opnd (var->opnd ret-var))) |
| 5506 |
(seal-bb (intrs-enabled? (node-decl node)) 'return) |
| 5507 |
(dealloc-slots nb-slots) |
| 5508 |
(bb-put-branch! |
| 5509 |
*bb* |
| 5510 |
(make-jump |
| 5511 |
ret-opnd |
| 5512 |
#f |
| 5513 |
#f |
| 5514 |
(current-frame (set-singleton var)) |
| 5515 |
#f))))) |
| 5516 |
(else opnd))) |
| 5517 |
(define (not-constant-closed-vars val) |
| 5518 |
(set-keep not-constant-var? (free-variables val))) |
| 5519 |
(define (predicate node live cont) |
| 5520 |
(define (cont* true-lbl false-lbl) (cont false-lbl true-lbl)) |
| 5521 |
(define (generic-true-test) |
| 5522 |
(predicate-test node live **not-proc-obj '0 (list node) cont*)) |
| 5523 |
(cond ((or (conj? node) (disj? node)) (predicate-conj/disj node live cont)) |
| 5524 |
((app? node) |
| 5525 |
(let ((proc (node->proc (app-oper node)))) |
| 5526 |
(if proc |
| 5527 |
(let ((spec (specialize-for-call proc (node-decl node)))) |
| 5528 |
(if (and (proc-obj-test spec) |
| 5529 |
(nb-args-conforms? |
| 5530 |
(length (app-args node)) |
| 5531 |
(proc-obj-call-pat spec))) |
| 5532 |
(if (eq? spec **not-proc-obj) |
| 5533 |
(predicate (car (app-args node)) live cont*) |
| 5534 |
(predicate-test |
| 5535 |
node |
| 5536 |
live |
| 5537 |
spec |
| 5538 |
(proc-obj-strict-pat proc) |
| 5539 |
(app-args node) |
| 5540 |
cont)) |
| 5541 |
(generic-true-test))) |
| 5542 |
(generic-true-test)))) |
| 5543 |
(else (generic-true-test)))) |
| 5544 |
(define (predicate-conj/disj node live cont) |
| 5545 |
(let* ((pre (if (conj? node) (conj-pre node) (disj-pre node))) |
| 5546 |
(alt (if (conj? node) (conj-alt node) (disj-alt node))) |
| 5547 |
(alt-live (set-union live (free-variables alt)))) |
| 5548 |
(predicate |
| 5549 |
pre |
| 5550 |
alt-live |
| 5551 |
(lambda (true-lbl false-lbl) |
| 5552 |
(let ((pre-context (current-context))) |
| 5553 |
(set! *bb* |
| 5554 |
(make-bb (make-label-simple |
| 5555 |
(if (conj? node) true-lbl false-lbl) |
| 5556 |
(current-frame alt-live) |
| 5557 |
(source-comment alt)) |
| 5558 |
*bbs*)) |
| 5559 |
(predicate |
| 5560 |
alt |
| 5561 |
live |
| 5562 |
(lambda (true-lbl2 false-lbl2) |
| 5563 |
(let ((alt-context (current-context))) |
| 5564 |
(restore-context pre-context) |
| 5565 |
(set! *bb* |
| 5566 |
(make-bb (make-label-simple |
| 5567 |
(if (conj? node) false-lbl true-lbl) |
| 5568 |
(current-frame live) |
| 5569 |
(source-comment alt)) |
| 5570 |
*bbs*)) |
| 5571 |
(merge-contexts-and-seal-bb |
| 5572 |
alt-context |
| 5573 |
live |
| 5574 |
(intrs-enabled? (node-decl node)) |
| 5575 |
'internal |
| 5576 |
(source-comment node)) |
| 5577 |
(bb-put-branch! |
| 5578 |
*bb* |
| 5579 |
(make-jump |
| 5580 |
(make-lbl (if (conj? node) false-lbl2 true-lbl2)) |
| 5581 |
#f |
| 5582 |
#f |
| 5583 |
(current-frame live) |
| 5584 |
#f)) |
| 5585 |
(cont true-lbl2 false-lbl2))))))))) |
| 5586 |
(define (predicate-test node live test strict-pat args cont) |
| 5587 |
(let loop ((args* args) (liv live) (vars* '())) |
| 5588 |
(if (not (null? args*)) |
| 5589 |
(let* ((needed (vals-live-vars liv (cdr args*))) |
| 5590 |
(var (save-var |
| 5591 |
(gen-node (car args*) needed 'need) |
| 5592 |
(make-temp-var 'predicate) |
| 5593 |
needed |
| 5594 |
(source-comment (car args*))))) |
| 5595 |
(loop (cdr args*) (set-adjoin liv var) (cons var vars*))) |
| 5596 |
(let* ((true-lbl (bbs-new-lbl! *bbs*)) |
| 5597 |
(false-lbl (bbs-new-lbl! *bbs*))) |
| 5598 |
(seal-bb (intrs-enabled? (node-decl node)) 'internal) |
| 5599 |
(bb-put-branch! |
| 5600 |
*bb* |
| 5601 |
(make-ifjump |
| 5602 |
test |
| 5603 |
(map var->opnd (reverse vars*)) |
| 5604 |
true-lbl |
| 5605 |
false-lbl |
| 5606 |
#f |
| 5607 |
(current-frame live) |
| 5608 |
(source-comment node))) |
| 5609 |
(cont true-lbl false-lbl))))) |
| 5610 |
(define (gen-tst node live why) |
| 5611 |
(let ((pre (tst-pre node)) (con (tst-con node)) (alt (tst-alt node))) |
| 5612 |
(predicate |
| 5613 |
pre |
| 5614 |
(set-union live (free-variables con) (free-variables alt)) |
| 5615 |
(lambda (true-lbl false-lbl) |
| 5616 |
(let ((pre-context (current-context)) |
| 5617 |
(true-bb (make-bb (make-label-simple |
| 5618 |
true-lbl |
| 5619 |
(current-frame |
| 5620 |
(set-union live (free-variables con))) |
| 5621 |
(source-comment con)) |
| 5622 |
*bbs*)) |
| 5623 |
(false-bb |
| 5624 |
(make-bb (make-label-simple |
| 5625 |
false-lbl |
| 5626 |
(current-frame (set-union live (free-variables alt))) |
| 5627 |
(source-comment alt)) |
| 5628 |
*bbs*))) |
| 5629 |
(set! *bb* true-bb) |
| 5630 |
(let ((con-opnd (gen-node con live why))) |
| 5631 |
(if (eq? why 'tail) |
| 5632 |
(begin |
| 5633 |
(restore-context pre-context) |
| 5634 |
(set! *bb* false-bb) |
| 5635 |
(gen-node alt live why)) |
| 5636 |
(let* ((result-var (make-temp-var 'result)) |
| 5637 |
(live-after (set-adjoin live result-var))) |
| 5638 |
(save-opnd-to-reg |
| 5639 |
con-opnd |
| 5640 |
target.proc-result |
| 5641 |
result-var |
| 5642 |
live |
| 5643 |
(source-comment con)) |
| 5644 |
(let ((con-context (current-context)) (con-bb *bb*)) |
| 5645 |
(restore-context pre-context) |
| 5646 |
(set! *bb* false-bb) |
| 5647 |
(save-opnd-to-reg |
| 5648 |
(gen-node alt live why) |
| 5649 |
target.proc-result |
| 5650 |
result-var |
| 5651 |
live |
| 5652 |
(source-comment alt)) |
| 5653 |
(let ((next-lbl (bbs-new-lbl! *bbs*)) (alt-bb *bb*)) |
| 5654 |
(if (> (context-nb-slots con-context) nb-slots) |
| 5655 |
(begin |
| 5656 |
(seal-bb (intrs-enabled? (node-decl node)) |
| 5657 |
'internal) |
| 5658 |
(let ((alt-context (current-context))) |
| 5659 |
(restore-context con-context) |
| 5660 |
(set! *bb* con-bb) |
| 5661 |
(merge-contexts-and-seal-bb |
| 5662 |
alt-context |
| 5663 |
live-after |
| 5664 |
(intrs-enabled? (node-decl node)) |
| 5665 |
'internal |
| 5666 |
(source-comment node)))) |
| 5667 |
(let ((alt-context (current-context))) |
| 5668 |
(restore-context con-context) |
| 5669 |
(set! *bb* con-bb) |
| 5670 |
(seal-bb (intrs-enabled? (node-decl node)) |
| 5671 |
'internal) |
| 5672 |
(let ((con-context* (current-context))) |
| 5673 |
(restore-context alt-context) |
| 5674 |
(set! *bb* alt-bb) |
| 5675 |
(merge-contexts-and-seal-bb |
| 5676 |
con-context* |
| 5677 |
live-after |
| 5678 |
(intrs-enabled? (node-decl node)) |
| 5679 |
'internal |
| 5680 |
(source-comment node))))) |
| 5681 |
(let ((frame (current-frame live-after))) |
| 5682 |
(bb-put-branch! |
| 5683 |
con-bb |
| 5684 |
(make-jump (make-lbl next-lbl) #f #f frame #f)) |
| 5685 |
(bb-put-branch! |
| 5686 |
alt-bb |
| 5687 |
(make-jump (make-lbl next-lbl) #f #f frame #f)) |
| 5688 |
(set! *bb* |
| 5689 |
(make-bb (make-label-simple |
| 5690 |
next-lbl |
| 5691 |
frame |
| 5692 |
(source-comment node)) |
| 5693 |
*bbs*)) |
| 5694 |
target.proc-result))))))))))) |
| 5695 |
(define (nb-args-conforms? n call-pat) (pattern-member? n call-pat)) |
| 5696 |
(define (merge-contexts-and-seal-bb other-context live poll? where comment) |
| 5697 |
(let ((live-v (live-vars live)) |
| 5698 |
(other-nb-slots (context-nb-slots other-context)) |
| 5699 |
(other-regs (context-regs other-context)) |
| 5700 |
(other-slots (context-slots other-context)) |
| 5701 |
(other-poll (context-poll other-context)) |
| 5702 |
(other-entry-bb (context-entry-bb other-context))) |
| 5703 |
(let loop1 ((i (- target.nb-regs 1))) |
| 5704 |
(if (>= i 0) |
| 5705 |
(let ((other-var (reg->var other-regs i)) (var (reg->var regs i))) |
| 5706 |
(if (and (not (eq? var other-var)) (set-member? other-var live-v)) |
| 5707 |
(let ((r (make-reg i))) |
| 5708 |
(put-var r empty-var) |
| 5709 |
(if (not (or (not (set-member? var live-v)) |
| 5710 |
(memq var regs) |
| 5711 |
(memq var slots))) |
| 5712 |
(let ((top (make-stk (+ nb-slots 1)))) |
| 5713 |
(put-copy r top var live-v comment))) |
| 5714 |
(put-copy (var->opnd other-var) r other-var live-v comment))) |
| 5715 |
(loop1 (- i 1))))) |
| 5716 |
(let loop2 ((i 1)) |
| 5717 |
(if (<= i other-nb-slots) |
| 5718 |
(let ((other-var (stk->var other-slots i)) (var (stk->var slots i))) |
| 5719 |
(if (and (not (eq? var other-var)) (set-member? other-var live-v)) |
| 5720 |
(let ((s (make-stk i))) |
| 5721 |
(if (<= i nb-slots) (put-var s empty-var)) |
| 5722 |
(if (not (or (not (set-member? var live-v)) |
| 5723 |
(memq var regs) |
| 5724 |
(memq var slots))) |
| 5725 |
(let ((top (make-stk (+ nb-slots 1)))) |
| 5726 |
(put-copy s top var live-v comment))) |
| 5727 |
(put-copy (var->opnd other-var) s other-var live-v comment)) |
| 5728 |
(if (> i nb-slots) |
| 5729 |
(let ((top (make-stk (+ nb-slots 1)))) |
| 5730 |
(put-copy |
| 5731 |
(make-obj undef-object) |
| 5732 |
top |
| 5733 |
empty-var |
| 5734 |
live-v |
| 5735 |
comment)))) |
| 5736 |
(loop2 (+ i 1))))) |
| 5737 |
(dealloc-slots (- nb-slots other-nb-slots)) |
| 5738 |
(let loop3 ((i (- target.nb-regs 1))) |
| 5739 |
(if (>= i 0) |
| 5740 |
(let ((other-var (reg->var other-regs i)) (var (reg->var regs i))) |
| 5741 |
(if (not (eq? var other-var)) (put-var (make-reg i) empty-var)) |
| 5742 |
(loop3 (- i 1))))) |
| 5743 |
(let loop4 ((i 1)) |
| 5744 |
(if (<= i other-nb-slots) |
| 5745 |
(let ((other-var (stk->var other-slots i)) (var (stk->var slots i))) |
| 5746 |
(if (not (eq? var other-var)) (put-var (make-stk i) empty-var)) |
| 5747 |
(loop4 (+ i 1))))) |
| 5748 |
(seal-bb poll? where) |
| 5749 |
(set! poll (poll-merge poll other-poll)) |
| 5750 |
(if (not (eq? entry-bb other-entry-bb)) |
| 5751 |
(compiler-internal-error |
| 5752 |
"merge-contexts-and-seal-bb, entry-bb's do not agree")))) |
| 5753 |
(define (seal-bb poll? where) |
| 5754 |
(define (my-last-pair l) (if (pair? (cdr l)) (my-last-pair (cdr l)) l)) |
| 5755 |
(define (poll-at split-point) |
| 5756 |
(let loop ((i 0) (l1 (bb-non-branch-instrs *bb*)) (l2 '())) |
| 5757 |
(if (< i split-point) |
| 5758 |
(loop (+ i 1) (cdr l1) (cons (car l1) l2)) |
| 5759 |
(let* ((label-instr (bb-label-instr *bb*)) |
| 5760 |
(non-branch-instrs1 (reverse l2)) |
| 5761 |
(non-branch-instrs2 l1) |
| 5762 |
(frame (gvm-instr-frame |
| 5763 |
(car (my-last-pair |
| 5764 |
(cons label-instr non-branch-instrs1))))) |
| 5765 |
(prec-bb (make-bb label-instr *bbs*)) |
| 5766 |
(new-lbl (bbs-new-lbl! *bbs*))) |
| 5767 |
(bb-non-branch-instrs-set! prec-bb non-branch-instrs1) |
| 5768 |
(bb-put-branch! |
| 5769 |
prec-bb |
| 5770 |
(make-jump (make-lbl new-lbl) #f #t frame #f)) |
| 5771 |
(bb-label-instr-set! *bb* (make-label-simple new-lbl frame #f)) |
| 5772 |
(bb-non-branch-instrs-set! *bb* non-branch-instrs2) |
| 5773 |
(set! poll (make-poll #t 0)))))) |
| 5774 |
(define (poll-at-end) (poll-at (length (bb-non-branch-instrs *bb*)))) |
| 5775 |
(define (impose-polling-constraints) |
| 5776 |
(let ((n (+ (length (bb-non-branch-instrs *bb*)) 1)) |
| 5777 |
(delta (poll-delta poll))) |
| 5778 |
(if (> (+ delta n) poll-period) |
| 5779 |
(begin |
| 5780 |
(poll-at (max (- poll-period delta) 0)) |
| 5781 |
(impose-polling-constraints))))) |
| 5782 |
(if poll? (impose-polling-constraints)) |
| 5783 |
(let* ((n (+ (length (bb-non-branch-instrs *bb*)) 1)) |
| 5784 |
(delta (+ (poll-delta poll) n)) |
| 5785 |
(since-entry? (poll-since-entry? poll))) |
| 5786 |
(if (and poll? |
| 5787 |
(case where |
| 5788 |
((call) (> delta (- poll-period poll-head))) |
| 5789 |
((tail-call) (> delta poll-tail)) |
| 5790 |
((return) (and since-entry? (> delta (+ poll-head poll-tail)))) |
| 5791 |
((internal) #f) |
| 5792 |
(else |
| 5793 |
(compiler-internal-error "seal-bb, unknown 'where':" where)))) |
| 5794 |
(poll-at-end) |
| 5795 |
(set! poll (make-poll since-entry? delta))))) |
| 5796 |
(define (reg->var regs i) |
| 5797 |
(cond ((null? regs) '()) |
| 5798 |
((> i 0) (reg->var (cdr regs) (- i 1))) |
| 5799 |
(else (car regs)))) |
| 5800 |
(define (stk->var slots i) |
| 5801 |
(let ((j (- (length slots) i))) (if (< j 0) '() (list-ref slots j)))) |
| 5802 |
(define (gen-conj/disj node live why) |
| 5803 |
(let ((pre (if (conj? node) (conj-pre node) (disj-pre node))) |
| 5804 |
(alt (if (conj? node) (conj-alt node) (disj-alt node)))) |
| 5805 |
(let ((needed (set-union live (free-variables alt))) |
| 5806 |
(bool? (boolean-value? pre)) |
| 5807 |
(predicate-var (make-temp-var 'predicate))) |
| 5808 |
(define (general-predicate node live cont) |
| 5809 |
(let* ((con-lbl (bbs-new-lbl! *bbs*)) (alt-lbl (bbs-new-lbl! *bbs*))) |
| 5810 |
(save-opnd-to-reg |
| 5811 |
(gen-node pre live 'need) |
| 5812 |
target.proc-result |
| 5813 |
predicate-var |
| 5814 |
live |
| 5815 |
(source-comment pre)) |
| 5816 |
(seal-bb (intrs-enabled? (node-decl node)) 'internal) |
| 5817 |
(bb-put-branch! |
| 5818 |
*bb* |
| 5819 |
(make-ifjump |
| 5820 |
**not-proc-obj |
| 5821 |
(list target.proc-result) |
| 5822 |
alt-lbl |
| 5823 |
con-lbl |
| 5824 |
#f |
| 5825 |
(current-frame (set-adjoin live predicate-var)) |
| 5826 |
(source-comment node))) |
| 5827 |
(cont con-lbl alt-lbl))) |
| 5828 |
(define (alternative con-lbl alt-lbl) |
| 5829 |
(let* ((pre-context (current-context)) |
| 5830 |
(result-var (make-temp-var 'result)) |
| 5831 |
(con-live (if bool? live (set-adjoin live predicate-var))) |
| 5832 |
(alt-live (set-union live (free-variables alt))) |
| 5833 |
(con-bb (make-bb (make-label-simple |
| 5834 |
con-lbl |
| 5835 |
(current-frame con-live) |
| 5836 |
(source-comment alt)) |
| 5837 |
*bbs*)) |
| 5838 |
(alt-bb (make-bb (make-label-simple |
| 5839 |
alt-lbl |
| 5840 |
(current-frame alt-live) |
| 5841 |
(source-comment alt)) |
| 5842 |
*bbs*))) |
| 5843 |
(if bool? |
| 5844 |
(begin |
| 5845 |
(set! *bb* con-bb) |
| 5846 |
(save-opnd-to-reg |
| 5847 |
(make-obj (if (conj? node) false-object #t)) |
| 5848 |
target.proc-result |
| 5849 |
result-var |
| 5850 |
live |
| 5851 |
(source-comment node))) |
| 5852 |
(put-var (var->opnd predicate-var) result-var)) |
| 5853 |
(let ((con-context (current-context))) |
| 5854 |
(set! *bb* alt-bb) |
| 5855 |
(restore-context pre-context) |
| 5856 |
(let ((alt-opnd (gen-node alt live why))) |
| 5857 |
(if (eq? why 'tail) |
| 5858 |
(begin |
| 5859 |
(restore-context con-context) |
| 5860 |
(set! *bb* con-bb) |
| 5861 |
(let ((ret-opnd (var->opnd ret-var)) |
| 5862 |
(result-set (set-singleton result-var))) |
| 5863 |
(seal-bb (intrs-enabled? (node-decl node)) 'return) |
| 5864 |
(dealloc-slots nb-slots) |
| 5865 |
(bb-put-branch! |
| 5866 |
*bb* |
| 5867 |
(make-jump |
| 5868 |
ret-opnd |
| 5869 |
#f |
| 5870 |
#f |
| 5871 |
(current-frame result-set) |
| 5872 |
#f)))) |
| 5873 |
(let ((alt-context* (current-context)) (alt-bb* *bb*)) |
| 5874 |
(restore-context con-context) |
| 5875 |
(set! *bb* con-bb) |
| 5876 |
(seal-bb (intrs-enabled? (node-decl node)) 'internal) |
| 5877 |
(let ((con-context* (current-context)) |
| 5878 |
(next-lbl (bbs-new-lbl! *bbs*))) |
| 5879 |
(restore-context alt-context*) |
| 5880 |
(set! *bb* alt-bb*) |
| 5881 |
(save-opnd-to-reg |
| 5882 |
alt-opnd |
| 5883 |
target.proc-result |
| 5884 |
result-var |
| 5885 |
live |
| 5886 |
(source-comment alt)) |
| 5887 |
(merge-contexts-and-seal-bb |
| 5888 |
con-context* |
| 5889 |
(set-adjoin live result-var) |
| 5890 |
(intrs-enabled? (node-decl node)) |
| 5891 |
'internal |
| 5892 |
(source-comment node)) |
| 5893 |
(let ((frame (current-frame |
| 5894 |
(set-adjoin live result-var)))) |
| 5895 |
(bb-put-branch! |
| 5896 |
*bb* |
| 5897 |
(make-jump (make-lbl next-lbl) #f #f frame #f)) |
| 5898 |
(bb-put-branch! |
| 5899 |
con-bb |
| 5900 |
(make-jump (make-lbl next-lbl) #f #f frame #f)) |
| 5901 |
(set! *bb* |
| 5902 |
(make-bb (make-label-simple |
| 5903 |
next-lbl |
| 5904 |
frame |
| 5905 |
(source-comment node)) |
| 5906 |
*bbs*)) |
| 5907 |
target.proc-result)))))))) |
| 5908 |
((if bool? predicate general-predicate) |
| 5909 |
pre |
| 5910 |
needed |
| 5911 |
(lambda (true-lbl false-lbl) |
| 5912 |
(if (conj? node) |
| 5913 |
(alternative false-lbl true-lbl) |
| 5914 |
(alternative true-lbl false-lbl))))))) |
| 5915 |
(define (gen-call node live why) |
| 5916 |
(let* ((oper (app-oper node)) (args (app-args node)) (nb-args (length args))) |
| 5917 |
(if (and (prc? oper) |
| 5918 |
(not (prc-rest oper)) |
| 5919 |
(= (length (prc-parms oper)) nb-args)) |
| 5920 |
(gen-let (prc-parms oper) args (prc-body oper) live why) |
| 5921 |
(if (inlinable-app? node) |
| 5922 |
(let ((eval-order (arg-eval-order #f args)) |
| 5923 |
(vars (map (lambda (x) (cons x #f)) args))) |
| 5924 |
(let loop ((l eval-order) (liv live)) |
| 5925 |
(if (not (null? l)) |
| 5926 |
(let* ((needed (vals-live-vars liv (map car (cdr l)))) |
| 5927 |
(arg (car (car l))) |
| 5928 |
(pos (cdr (car l))) |
| 5929 |
(var (save-var |
| 5930 |
(gen-node arg needed 'need) |
| 5931 |
(make-temp-var pos) |
| 5932 |
needed |
| 5933 |
(source-comment arg)))) |
| 5934 |
(set-cdr! (assq arg vars) var) |
| 5935 |
(loop (cdr l) (set-adjoin liv var))) |
| 5936 |
(let ((loc (if (eq? why 'side) |
| 5937 |
(make-reg 0) |
| 5938 |
(or (lowest-dead-reg live) |
| 5939 |
(lowest-dead-slot live))))) |
| 5940 |
(if (and (stk? loc) (> (stk-num loc) nb-slots)) |
| 5941 |
(push-slot)) |
| 5942 |
(let* ((args (map var->opnd (map cdr vars))) |
| 5943 |
(var (make-temp-var 'result)) |
| 5944 |
(proc (node->proc oper)) |
| 5945 |
(strict-pat (proc-obj-strict-pat proc))) |
| 5946 |
(if (not (eq? why 'side)) (put-var loc var)) |
| 5947 |
(bb-put-non-branch! |
| 5948 |
*bb* |
| 5949 |
(make-apply |
| 5950 |
(specialize-for-call proc (node-decl node)) |
| 5951 |
args |
| 5952 |
(if (eq? why 'side) #f loc) |
| 5953 |
(current-frame |
| 5954 |
(if (eq? why 'side) live (set-adjoin live var))) |
| 5955 |
(source-comment node))) |
| 5956 |
(gen-return loc why node)))))) |
| 5957 |
(let* ((calling-local-proc? |
| 5958 |
(and (ref? oper) |
| 5959 |
(let ((opnd (var->opnd (ref-var oper)))) |
| 5960 |
(and (lbl? opnd) |
| 5961 |
(let ((x (assq (lbl-num opnd) known-procs))) |
| 5962 |
(and x |
| 5963 |
(let ((proc (cdr x))) |
| 5964 |
(and (not (prc-rest proc)) |
| 5965 |
(= (prc-min proc) nb-args) |
| 5966 |
(= (length (prc-parms proc)) |
| 5967 |
nb-args) |
| 5968 |
(lbl-num opnd))))))))) |
| 5969 |
(jstate (get-jump-state |
| 5970 |
args |
| 5971 |
(if calling-local-proc? |
| 5972 |
(target.label-info nb-args nb-args #f #f) |
| 5973 |
(target.jump-info nb-args)))) |
| 5974 |
(in-stk (jump-state-in-stk jstate)) |
| 5975 |
(in-reg (jump-state-in-reg jstate)) |
| 5976 |
(eval-order |
| 5977 |
(arg-eval-order (if calling-local-proc? #f oper) in-reg)) |
| 5978 |
(live-after |
| 5979 |
(if (eq? why 'tail) (set-remove live ret-var) live)) |
| 5980 |
(live-for-regs (args-live-vars live eval-order)) |
| 5981 |
(return-lbl (if (eq? why 'tail) #f (bbs-new-lbl! *bbs*)))) |
| 5982 |
(save-regs |
| 5983 |
(live-regs live-after) |
| 5984 |
(stk-live-vars live-for-regs in-stk why) |
| 5985 |
(source-comment node)) |
| 5986 |
(let ((frame-start (stk-num (highest-live-slot live-after)))) |
| 5987 |
(let loop1 ((l in-stk) (liv live-after) (i (+ frame-start 1))) |
| 5988 |
(if (not (null? l)) |
| 5989 |
(let ((arg (car l)) |
| 5990 |
(slot (make-stk i)) |
| 5991 |
(needed (set-union |
| 5992 |
(stk-live-vars liv (cdr l) why) |
| 5993 |
live-for-regs))) |
| 5994 |
(if arg |
| 5995 |
(let ((var (if (and (eq? arg 'return) |
| 5996 |
(eq? why 'tail)) |
| 5997 |
ret-var |
| 5998 |
(make-temp-var (- frame-start i))))) |
| 5999 |
(save-opnd-to-stk |
| 6000 |
(if (eq? arg 'return) |
| 6001 |
(if (eq? why 'tail) |
| 6002 |
(var->opnd ret-var) |
| 6003 |
(make-lbl return-lbl)) |
| 6004 |
(gen-node arg needed 'need)) |
| 6005 |
slot |
| 6006 |
var |
| 6007 |
needed |
| 6008 |
(source-comment |
| 6009 |
(if (eq? arg 'return) node arg))) |
| 6010 |
(loop1 (cdr l) (set-adjoin liv var) (+ i 1))) |
| 6011 |
(begin |
| 6012 |
(if (> i nb-slots) |
| 6013 |
(put-copy |
| 6014 |
(make-obj undef-object) |
| 6015 |
slot |
| 6016 |
empty-var |
| 6017 |
liv |
| 6018 |
(source-comment node))) |
| 6019 |
(loop1 (cdr l) liv (+ i 1))))) |
| 6020 |
(let loop2 ((l eval-order) |
| 6021 |
(liv liv) |
| 6022 |
(reg-map '()) |
| 6023 |
(oper-var '())) |
| 6024 |
(if (not (null? l)) |
| 6025 |
(let* ((arg (car (car l))) |
| 6026 |
(pos (cdr (car l))) |
| 6027 |
(needed (args-live-vars liv (cdr l))) |
| 6028 |
(var (if (and (eq? arg 'return) |
| 6029 |
(eq? why 'tail)) |
| 6030 |
ret-var |
| 6031 |
(make-temp-var pos))) |
| 6032 |
(opnd (if (eq? arg 'return) |
| 6033 |
(if (eq? why 'tail) |
| 6034 |
(var->opnd ret-var) |
| 6035 |
(make-lbl return-lbl)) |
| 6036 |
(gen-node arg needed 'need)))) |
| 6037 |
(if (eq? pos 'operator) |
| 6038 |
(if (and (ref? arg) |
| 6039 |
(not (or (obj? opnd) (lbl? opnd)))) |
| 6040 |
(loop2 (cdr l) |
| 6041 |
(set-adjoin liv (ref-var arg)) |
| 6042 |
reg-map |
| 6043 |
(ref-var arg)) |
| 6044 |
(begin |
| 6045 |
(save-arg |
| 6046 |
opnd |
| 6047 |
var |
| 6048 |
needed |
| 6049 |
(source-comment |
| 6050 |
(if (eq? arg 'return) node arg))) |
| 6051 |
(loop2 (cdr l) |
| 6052 |
(set-adjoin liv var) |
| 6053 |
reg-map |
| 6054 |
var))) |
| 6055 |
(let ((reg (make-reg pos))) |
| 6056 |
(if (all-args-trivial? (cdr l)) |
| 6057 |
(save-opnd-to-reg |
| 6058 |
opnd |
| 6059 |
reg |
| 6060 |
var |
| 6061 |
needed |
| 6062 |
(source-comment |
| 6063 |
(if (eq? arg 'return) node arg))) |
| 6064 |
(save-in-slot |
| 6065 |
opnd |
| 6066 |
var |
| 6067 |
needed |
| 6068 |
(source-comment |
| 6069 |
(if (eq? arg 'return) node arg)))) |
| 6070 |
(loop2 (cdr l) |
| 6071 |
(set-adjoin liv var) |
| 6072 |
(cons (cons pos var) reg-map) |
| 6073 |
oper-var)))) |
| 6074 |
(let loop3 ((i (- target.nb-regs 1))) |
| 6075 |
(if (>= i 0) |
| 6076 |
(let ((couple (assq i reg-map))) |
| 6077 |
(if couple |
| 6078 |
(let ((var (cdr couple))) |
| 6079 |
(if (not (eq? (reg->var regs i) var)) |
| 6080 |
(save-opnd-to-reg |
| 6081 |
(var->opnd var) |
| 6082 |
(make-reg i) |
| 6083 |
var |
| 6084 |
liv |
| 6085 |
(source-comment node))))) |
| 6086 |
(loop3 (- i 1))) |
| 6087 |
(let ((opnd (if calling-local-proc? |
| 6088 |
(make-lbl |
| 6089 |
(+ calling-local-proc? 1)) |
| 6090 |
(var->opnd oper-var)))) |
| 6091 |
(seal-bb (intrs-enabled? (node-decl node)) |
| 6092 |
(if return-lbl 'call 'tail-call)) |
| 6093 |
(dealloc-slots |
| 6094 |
(- nb-slots |
| 6095 |
(+ frame-start (length in-stk)))) |
| 6096 |
(bb-put-branch! |
| 6097 |
*bb* |
| 6098 |
(make-jump |
| 6099 |
opnd |
| 6100 |
(if calling-local-proc? #f nb-args) |
| 6101 |
#f |
| 6102 |
(current-frame liv) |
| 6103 |
(source-comment node))) |
| 6104 |
(let ((result-var (make-temp-var 'result))) |
| 6105 |
(dealloc-slots (- nb-slots frame-start)) |
| 6106 |
(flush-regs) |
| 6107 |
(put-var target.proc-result result-var) |
| 6108 |
(if return-lbl |
| 6109 |
(begin |
| 6110 |
(set! poll (return-poll poll)) |
| 6111 |
(set! *bb* |
| 6112 |
(make-bb (make-label-return |
| 6113 |
return-lbl |
| 6114 |
(current-frame |
| 6115 |
(set-adjoin |
| 6116 |
live |
| 6117 |
result-var)) |
| 6118 |
(source-comment |
| 6119 |
node)) |
| 6120 |
*bbs*)))) |
| 6121 |
target.proc-result)))))))))))))) |
| 6122 |
(define (contained-reg/slot opnd) |
| 6123 |
(cond ((reg? opnd) opnd) |
| 6124 |
((stk? opnd) opnd) |
| 6125 |
((clo? opnd) (contained-reg/slot (clo-base opnd))) |
| 6126 |
(else #f))) |
| 6127 |
(define (opnd-needed opnd needed) |
| 6128 |
(let ((x (contained-reg/slot opnd))) |
| 6129 |
(if x (set-adjoin needed (get-var x)) needed))) |
| 6130 |
(define (save-opnd opnd live comment) |
| 6131 |
(let ((slot (lowest-dead-slot live))) |
| 6132 |
(put-copy opnd slot (get-var opnd) live comment))) |
| 6133 |
(define (save-regs regs live comment) |
| 6134 |
(for-each |
| 6135 |
(lambda (i) (save-opnd (make-reg i) live comment)) |
| 6136 |
(set->list regs))) |
| 6137 |
(define (save-opnd-to-reg opnd reg var live comment) |
| 6138 |
(if (set-member? (reg-num reg) (live-regs live)) |
| 6139 |
(save-opnd reg (opnd-needed opnd live) comment)) |
| 6140 |
(put-copy opnd reg var live comment)) |
| 6141 |
(define (save-opnd-to-stk opnd stk var live comment) |
| 6142 |
(if (set-member? (stk-num stk) (live-slots live)) |
| 6143 |
(save-opnd stk (opnd-needed opnd live) comment)) |
| 6144 |
(put-copy opnd stk var live comment)) |
| 6145 |
(define (all-args-trivial? l) |
| 6146 |
(if (null? l) |
| 6147 |
#t |
| 6148 |
(let ((arg (car (car l)))) |
| 6149 |
(or (eq? arg 'return) |
| 6150 |
(and (trivial? arg) (all-args-trivial? (cdr l))))))) |
| 6151 |
(define (every-trivial? l) |
| 6152 |
(or (null? l) (and (trivial? (car l)) (every-trivial? (cdr l))))) |
| 6153 |
(define (trivial? node) |
| 6154 |
(or (cst? node) |
| 6155 |
(ref? node) |
| 6156 |
(and (set? node) (trivial? (set-val node))) |
| 6157 |
(and (inlinable-app? node) (every-trivial? (app-args node))))) |
| 6158 |
(define (inlinable-app? node) |
| 6159 |
(if (app? node) |
| 6160 |
(let ((proc (node->proc (app-oper node)))) |
| 6161 |
(and proc |
| 6162 |
(let ((spec (specialize-for-call proc (node-decl node)))) |
| 6163 |
(and (proc-obj-inlinable spec) |
| 6164 |
(nb-args-conforms? |
| 6165 |
(length (app-args node)) |
| 6166 |
(proc-obj-call-pat spec)))))) |
| 6167 |
#f)) |
| 6168 |
(define (boolean-value? node) |
| 6169 |
(or (and (conj? node) |
| 6170 |
(boolean-value? (conj-pre node)) |
| 6171 |
(boolean-value? (conj-alt node))) |
| 6172 |
(and (disj? node) |
| 6173 |
(boolean-value? (disj-pre node)) |
| 6174 |
(boolean-value? (disj-alt node))) |
| 6175 |
(boolean-app? node))) |
| 6176 |
(define (boolean-app? node) |
| 6177 |
(if (app? node) |
| 6178 |
(let ((proc (node->proc (app-oper node)))) |
| 6179 |
(if proc (eq? (type-name (proc-obj-type proc)) 'boolean) #f)) |
| 6180 |
#f)) |
| 6181 |
(define (node->proc node) |
| 6182 |
(cond ((cst? node) (if (proc-obj? (cst-val node)) (cst-val node) #f)) |
| 6183 |
((ref? node) |
| 6184 |
(if (global? (ref-var node)) |
| 6185 |
(target.prim-info* (var-name (ref-var node)) (node-decl node)) |
| 6186 |
#f)) |
| 6187 |
(else #f))) |
| 6188 |
(define (specialize-for-call proc decl) ((proc-obj-specialize proc) decl)) |
| 6189 |
(define (get-jump-state args pc) |
| 6190 |
(define (empty-node-list n) |
| 6191 |
(if (> n 0) (cons #f (empty-node-list (- n 1))) '())) |
| 6192 |
(let* ((fs (pcontext-fs pc)) |
| 6193 |
(slots-list (empty-node-list fs)) |
| 6194 |
(regs-list (empty-node-list target.nb-regs))) |
| 6195 |
(define (assign-node-to-loc var loc) |
| 6196 |
(let ((x (cond ((reg? loc) |
| 6197 |
(let ((i (reg-num loc))) |
| 6198 |
(if (<= i target.nb-regs) |
| 6199 |
(nth-after regs-list i) |
| 6200 |
(compiler-internal-error |
| 6201 |
"jump-state, reg out of bound in back-end's pcontext")))) |
| 6202 |
((stk? loc) |
| 6203 |
(let ((i (stk-num loc))) |
| 6204 |
(if (<= i fs) |
| 6205 |
(nth-after slots-list (- i 1)) |
| 6206 |
(compiler-internal-error |
| 6207 |
"jump-state, stk out of bound in back-end's pcontext")))) |
| 6208 |
(else |
| 6209 |
(compiler-internal-error |
| 6210 |
"jump-state, loc other than reg or stk in back-end's pcontext"))))) |
| 6211 |
(if (not (car x)) |
| 6212 |
(set-car! x var) |
| 6213 |
(compiler-internal-error |
| 6214 |
"jump-state, duplicate location in back-end's pcontext")))) |
| 6215 |
(let loop ((l (pcontext-map pc))) |
| 6216 |
(if (not (null? l)) |
| 6217 |
(let* ((couple (car l)) (name (car couple)) (loc (cdr couple))) |
| 6218 |
(cond ((eq? name 'return) (assign-node-to-loc 'return loc)) |
| 6219 |
(else (assign-node-to-loc (list-ref args (- name 1)) loc))) |
| 6220 |
(loop (cdr l))))) |
| 6221 |
(vector slots-list regs-list))) |
| 6222 |
(define (jump-state-in-stk x) (vector-ref x 0)) |
| 6223 |
(define (jump-state-in-reg x) (vector-ref x 1)) |
| 6224 |
(define (arg-eval-order oper nodes) |
| 6225 |
(define (loop nodes pos part1 part2) |
| 6226 |
(cond ((null? nodes) |
| 6227 |
(let ((p1 (reverse part1)) (p2 (free-vars-order part2))) |
| 6228 |
(cond ((not oper) (append p1 p2)) |
| 6229 |
((trivial? oper) |
| 6230 |
(append p1 p2 (list (cons oper 'operator)))) |
| 6231 |
(else (append (cons (cons oper 'operator) p1) p2))))) |
| 6232 |
((not (car nodes)) (loop (cdr nodes) (+ pos 1) part1 part2)) |
| 6233 |
((or (eq? (car nodes) 'return) (trivial? (car nodes))) |
| 6234 |
(loop (cdr nodes) |
| 6235 |
(+ pos 1) |
| 6236 |
part1 |
| 6237 |
(cons (cons (car nodes) pos) part2))) |
| 6238 |
(else |
| 6239 |
(loop (cdr nodes) |
| 6240 |
(+ pos 1) |
| 6241 |
(cons (cons (car nodes) pos) part1) |
| 6242 |
part2)))) |
| 6243 |
(loop nodes 0 '() '())) |
| 6244 |
(define (free-vars-order l) |
| 6245 |
(let ((bins '()) (ordered-args '())) |
| 6246 |
(define (free-v x) (if (eq? x 'return) (set-empty) (free-variables x))) |
| 6247 |
(define (add-to-bin! x) |
| 6248 |
(let ((y (assq x bins))) |
| 6249 |
(if y (set-cdr! y (+ (cdr y) 1)) (set! bins (cons (cons x 1) bins))))) |
| 6250 |
(define (payoff-if-removed node) |
| 6251 |
(let ((x (free-v node))) |
| 6252 |
(let loop ((l (set->list x)) (r 0)) |
| 6253 |
(if (null? l) |
| 6254 |
r |
| 6255 |
(let ((y (cdr (assq (car l) bins)))) |
| 6256 |
(loop (cdr l) (+ r (quotient 1000 (* y y))))))))) |
| 6257 |
(define (remove-free-vars! x) |
| 6258 |
(let loop ((l (set->list x))) |
| 6259 |
(if (not (null? l)) |
| 6260 |
(let ((y (assq (car l) bins))) |
| 6261 |
(set-cdr! y (- (cdr y) 1)) |
| 6262 |
(loop (cdr l)))))) |
| 6263 |
(define (find-max-payoff l thunk) |
| 6264 |
(if (null? l) |
| 6265 |
(thunk '() -1) |
| 6266 |
(find-max-payoff |
| 6267 |
(cdr l) |
| 6268 |
(lambda (best-arg best-payoff) |
| 6269 |
(let ((payoff (payoff-if-removed (car (car l))))) |
| 6270 |
(if (>= payoff best-payoff) |
| 6271 |
(thunk (car l) payoff) |
| 6272 |
(thunk best-arg best-payoff))))))) |
| 6273 |
(define (remove x l) |
| 6274 |
(cond ((null? l) '()) |
| 6275 |
((eq? x (car l)) (cdr l)) |
| 6276 |
(else (cons (car l) (remove x (cdr l)))))) |
| 6277 |
(for-each |
| 6278 |
(lambda (x) (for-each add-to-bin! (set->list (free-v (car x))))) |
| 6279 |
l) |
| 6280 |
(let loop ((args l) (ordered-args '())) |
| 6281 |
(if (null? args) |
| 6282 |
(reverse ordered-args) |
| 6283 |
(find-max-payoff |
| 6284 |
args |
| 6285 |
(lambda (best-arg best-payoff) |
| 6286 |
(remove-free-vars! (free-v (car best-arg))) |
| 6287 |
(loop (remove best-arg args) (cons best-arg ordered-args)))))))) |
| 6288 |
(define (args-live-vars live order) |
| 6289 |
(cond ((null? order) live) |
| 6290 |
((eq? (car (car order)) 'return) |
| 6291 |
(args-live-vars (set-adjoin live ret-var) (cdr order))) |
| 6292 |
(else |
| 6293 |
(args-live-vars |
| 6294 |
(set-union live (free-variables (car (car order)))) |
| 6295 |
(cdr order))))) |
| 6296 |
(define (stk-live-vars live slots why) |
| 6297 |
(cond ((null? slots) live) |
| 6298 |
((not (car slots)) (stk-live-vars live (cdr slots) why)) |
| 6299 |
((eq? (car slots) 'return) |
| 6300 |
(stk-live-vars |
| 6301 |
(if (eq? why 'tail) (set-adjoin live ret-var) live) |
| 6302 |
(cdr slots) |
| 6303 |
why)) |
| 6304 |
(else |
| 6305 |
(stk-live-vars |
| 6306 |
(set-union live (free-variables (car slots))) |
| 6307 |
(cdr slots) |
| 6308 |
why)))) |
| 6309 |
(define (gen-let vars vals node live why) |
| 6310 |
(let ((var-val-map (pair-up vars vals)) |
| 6311 |
(var-set (list->set vars)) |
| 6312 |
(all-live |
| 6313 |
(set-union |
| 6314 |
live |
| 6315 |
(free-variables node) |
| 6316 |
(apply set-union (map free-variables vals))))) |
| 6317 |
(define (var->val var) (cdr (assq var var-val-map))) |
| 6318 |
(define (proc-var? var) (prc? (var->val var))) |
| 6319 |
(define (closed-vars var const-proc-vars) |
| 6320 |
(set-difference |
| 6321 |
(not-constant-closed-vars (var->val var)) |
| 6322 |
const-proc-vars)) |
| 6323 |
(define (no-closed-vars? var const-proc-vars) |
| 6324 |
(set-empty? (closed-vars var const-proc-vars))) |
| 6325 |
(define (closed-vars? var const-proc-vars) |
| 6326 |
(not (no-closed-vars? var const-proc-vars))) |
| 6327 |
(define (compute-const-proc-vars proc-vars) |
| 6328 |
(let loop1 ((const-proc-vars proc-vars)) |
| 6329 |
(let ((new-const-proc-vars |
| 6330 |
(set-keep |
| 6331 |
(lambda (x) (no-closed-vars? x const-proc-vars)) |
| 6332 |
const-proc-vars))) |
| 6333 |
(if (not (set-equal? new-const-proc-vars const-proc-vars)) |
| 6334 |
(loop1 new-const-proc-vars) |
| 6335 |
const-proc-vars)))) |
| 6336 |
(let* ((proc-vars (set-keep proc-var? var-set)) |
| 6337 |
(const-proc-vars (compute-const-proc-vars proc-vars)) |
| 6338 |
(clo-vars |
| 6339 |
(set-keep (lambda (x) (closed-vars? x const-proc-vars)) proc-vars)) |
| 6340 |
(clo-vars-list (set->list clo-vars))) |
| 6341 |
(for-each |
| 6342 |
(lambda (proc-var) |
| 6343 |
(let ((label (schedule-gen-proc (var->val proc-var) '()))) |
| 6344 |
(add-known-proc (lbl-num label) (var->val proc-var)) |
| 6345 |
(add-constant-var proc-var label))) |
| 6346 |
(set->list const-proc-vars)) |
| 6347 |
(let ((non-clo-vars-list |
| 6348 |
(set->list |
| 6349 |
(set-keep |
| 6350 |
(lambda (var) |
| 6351 |
(and (not (set-member? var const-proc-vars)) |
| 6352 |
(not (set-member? var clo-vars)))) |
| 6353 |
vars))) |
| 6354 |
(liv (set-union |
| 6355 |
live |
| 6356 |
(apply set-union |
| 6357 |
(map (lambda (x) (closed-vars x const-proc-vars)) |
| 6358 |
clo-vars-list)) |
| 6359 |
(free-variables node)))) |
| 6360 |
(let loop2 ((vars* non-clo-vars-list)) |
| 6361 |
(if (not (null? vars*)) |
| 6362 |
(let* ((var (car vars*)) |
| 6363 |
(val (var->val var)) |
| 6364 |
(needed (vals-live-vars liv (map var->val (cdr vars*))))) |
| 6365 |
(if (var-useless? var) |
| 6366 |
(gen-node val needed 'side) |
| 6367 |
(save-val |
| 6368 |
(gen-node val needed 'need) |
| 6369 |
var |
| 6370 |
needed |
| 6371 |
(source-comment val))) |
| 6372 |
(loop2 (cdr vars*))))) |
| 6373 |
(if (pair? clo-vars-list) |
| 6374 |
(begin |
| 6375 |
(dealloc-slots (- nb-slots (stk-num (highest-live-slot liv)))) |
| 6376 |
(let loop3 ((l clo-vars-list)) |
| 6377 |
(if (not (null? l)) |
| 6378 |
(begin |
| 6379 |
(push-slot) |
| 6380 |
(let ((var (car l)) (slot (make-stk nb-slots))) |
| 6381 |
(put-var slot var) |
| 6382 |
(loop3 (cdr l)))))) |
| 6383 |
(bb-put-non-branch! |
| 6384 |
*bb* |
| 6385 |
(make-close |
| 6386 |
(map (lambda (var) |
| 6387 |
(let ((closed-list |
| 6388 |
(sort-variables |
| 6389 |
(set->list (closed-vars var const-proc-vars))))) |
| 6390 |
(if (null? closed-list) |
| 6391 |
(compiler-internal-error |
| 6392 |
"gen-let, no closed variables:" |
| 6393 |
(var-name var)) |
| 6394 |
(make-closure-parms |
| 6395 |
(var->opnd var) |
| 6396 |
(lbl-num (schedule-gen-proc |
| 6397 |
(var->val var) |
| 6398 |
closed-list)) |
| 6399 |
(map var->opnd closed-list))))) |
| 6400 |
clo-vars-list) |
| 6401 |
(current-frame liv) |
| 6402 |
(source-comment node))))) |
| 6403 |
(gen-node node live why))))) |
| 6404 |
(define (save-arg opnd var live comment) |
| 6405 |
(if (glo? opnd) |
| 6406 |
(add-constant-var var opnd) |
| 6407 |
(save-val opnd var live comment))) |
| 6408 |
(define (save-val opnd var live comment) |
| 6409 |
(cond ((or (obj? opnd) (lbl? opnd)) (add-constant-var var opnd)) |
| 6410 |
((and (reg? opnd) (not (set-member? (reg-num opnd) (live-regs live)))) |
| 6411 |
(put-var opnd var)) |
| 6412 |
((and (stk? opnd) (not (set-member? (stk-num opnd) (live-slots live)))) |
| 6413 |
(put-var opnd var)) |
| 6414 |
(else (save-in-slot opnd var live comment)))) |
| 6415 |
(define (save-in-slot opnd var live comment) |
| 6416 |
(let ((slot (lowest-dead-slot live))) (put-copy opnd slot var live comment))) |
| 6417 |
(define (save-var opnd var live comment) |
| 6418 |
(cond ((or (obj? opnd) (lbl? opnd)) (add-constant-var var opnd) var) |
| 6419 |
((or (glo? opnd) (reg? opnd) (stk? opnd)) (get-var opnd)) |
| 6420 |
(else |
| 6421 |
(let ((dest (or (highest-dead-reg live) (lowest-dead-slot live)))) |
| 6422 |
(put-copy opnd dest var live comment) |
| 6423 |
var)))) |
| 6424 |
(define (put-copy opnd loc var live comment) |
| 6425 |
(if (and (stk? loc) (> (stk-num loc) nb-slots)) (push-slot)) |
| 6426 |
(if var (put-var loc var)) |
| 6427 |
(if (not (eq? opnd loc)) |
| 6428 |
(bb-put-non-branch! |
| 6429 |
*bb* |
| 6430 |
(make-copy |
| 6431 |
opnd |
| 6432 |
loc |
| 6433 |
(current-frame (if var (set-adjoin live var) live)) |
| 6434 |
comment)))) |
| 6435 |
(define (var-useless? var) |
| 6436 |
(and (set-empty? (var-refs var)) (set-empty? (var-sets var)))) |
| 6437 |
(define (vals-live-vars live vals) |
| 6438 |
(if (null? vals) |
| 6439 |
live |
| 6440 |
(vals-live-vars |
| 6441 |
(set-union live (free-variables (car vals))) |
| 6442 |
(cdr vals)))) |
| 6443 |
(define (gen-fut node live why) |
| 6444 |
(let* ((val (fut-val node)) |
| 6445 |
(clo-vars (not-constant-closed-vars val)) |
| 6446 |
(clo-vars-list (set->list clo-vars)) |
| 6447 |
(ret-var* (make-temp-var 0)) |
| 6448 |
(live-after live) |
| 6449 |
(live-starting-task |
| 6450 |
(set-adjoin (set-union live-after clo-vars) ret-var*)) |
| 6451 |
(task-lbl (bbs-new-lbl! *bbs*)) |
| 6452 |
(return-lbl (bbs-new-lbl! *bbs*))) |
| 6453 |
(save-regs (live-regs live-after) live-starting-task (source-comment node)) |
| 6454 |
(let ((frame-start (stk-num (highest-live-slot live-after)))) |
| 6455 |
(save-opnd-to-reg |
| 6456 |
(make-lbl return-lbl) |
| 6457 |
target.task-return |
| 6458 |
ret-var* |
| 6459 |
(set-remove live-starting-task ret-var*) |
| 6460 |
(source-comment node)) |
| 6461 |
(let loop1 ((l clo-vars-list) (i 0)) |
| 6462 |
(if (null? l) |
| 6463 |
(dealloc-slots (- nb-slots (+ frame-start i))) |
| 6464 |
(let ((var (car l)) (rest (cdr l))) |
| 6465 |
(if (memq var regs) |
| 6466 |
(loop1 rest i) |
| 6467 |
(let loop2 ((j (- target.nb-regs 1))) |
| 6468 |
(if (>= j 0) |
| 6469 |
(if (or (>= j (length regs)) |
| 6470 |
(not (set-member? |
| 6471 |
(list-ref regs j) |
| 6472 |
live-starting-task))) |
| 6473 |
(let ((reg (make-reg j))) |
| 6474 |
(put-copy |
| 6475 |
(var->opnd var) |
| 6476 |
reg |
| 6477 |
var |
| 6478 |
live-starting-task |
| 6479 |
(source-comment node)) |
| 6480 |
(loop1 rest i)) |
| 6481 |
(loop2 (- j 1))) |
| 6482 |
(let ((slot (make-stk (+ frame-start (+ i 1)))) |
| 6483 |
(needed (list->set rest))) |
| 6484 |
(if (and (or (> (stk-num slot) nb-slots) |
| 6485 |
(not (memq (list-ref |
| 6486 |
slots |
| 6487 |
(- nb-slots (stk-num slot))) |
| 6488 |
regs))) |
| 6489 |
(set-member? |
| 6490 |
(stk-num slot) |
| 6491 |
(live-slots needed))) |
| 6492 |
(save-opnd |
| 6493 |
slot |
| 6494 |
live-starting-task |
| 6495 |
(source-comment node))) |
| 6496 |
(put-copy |
| 6497 |
(var->opnd var) |
| 6498 |
slot |
| 6499 |
var |
| 6500 |
live-starting-task |
| 6501 |
(source-comment node)) |
| 6502 |
(loop1 rest (+ i 1))))))))) |
| 6503 |
(seal-bb (intrs-enabled? (node-decl node)) 'call) |
| 6504 |
(bb-put-branch! |
| 6505 |
*bb* |
| 6506 |
(make-jump |
| 6507 |
(make-lbl task-lbl) |
| 6508 |
#f |
| 6509 |
#f |
| 6510 |
(current-frame live-starting-task) |
| 6511 |
#f)) |
| 6512 |
(let ((task-context |
| 6513 |
(make-context |
| 6514 |
(- nb-slots frame-start) |
| 6515 |
(reverse (nth-after (reverse slots) frame-start)) |
| 6516 |
(cons ret-var (cdr regs)) |
| 6517 |
'() |
| 6518 |
poll |
| 6519 |
entry-bb)) |
| 6520 |
(return-context |
| 6521 |
(make-context |
| 6522 |
frame-start |
| 6523 |
(nth-after slots (- nb-slots frame-start)) |
| 6524 |
'() |
| 6525 |
closed |
| 6526 |
(return-poll poll) |
| 6527 |
entry-bb))) |
| 6528 |
(restore-context task-context) |
| 6529 |
(set! *bb* |
| 6530 |
(make-bb (make-label-task-entry |
| 6531 |
task-lbl |
| 6532 |
(current-frame live-starting-task) |
| 6533 |
(source-comment node)) |
| 6534 |
*bbs*)) |
| 6535 |
(gen-node val ret-var-set 'tail) |
| 6536 |
(let ((result-var (make-temp-var 'future))) |
| 6537 |
(restore-context return-context) |
| 6538 |
(put-var target.proc-result result-var) |
| 6539 |
(set! *bb* |
| 6540 |
(make-bb (make-label-task-return |
| 6541 |
return-lbl |
| 6542 |
(current-frame (set-adjoin live result-var)) |
| 6543 |
(source-comment node)) |
| 6544 |
*bbs*)) |
| 6545 |
(gen-return target.proc-result why node)))))) |
| 6546 |
(define prim-procs |
| 6547 |
'(("not" (1) #f 0 boolean) |
| 6548 |
("boolean?" (1) #f 0 boolean) |
| 6549 |
("eqv?" (2) #f 0 boolean) |
| 6550 |
("eq?" (2) #f 0 boolean) |
| 6551 |
("equal?" (2) #f 0 boolean) |
| 6552 |
("pair?" (1) #f 0 boolean) |
| 6553 |
("cons" (2) #f () pair) |
| 6554 |
("car" (1) #f 0 (#f)) |
| 6555 |
("cdr" (1) #f 0 (#f)) |
| 6556 |
("set-car!" (2) #t (1) pair) |
| 6557 |
("set-cdr!" (2) #t (1) pair) |
| 6558 |
("caar" (1) #f 0 (#f)) |
| 6559 |
("cadr" (1) #f 0 (#f)) |
| 6560 |
("cdar" (1) #f 0 (#f)) |
| 6561 |
("cddr" (1) #f 0 (#f)) |
| 6562 |
("caaar" (1) #f 0 (#f)) |
| 6563 |
("caadr" (1) #f 0 (#f)) |
| 6564 |
("cadar" (1) #f 0 (#f)) |
| 6565 |
("caddr" (1) #f 0 (#f)) |
| 6566 |
("cdaar" (1) #f 0 (#f)) |
| 6567 |
("cdadr" (1) #f 0 (#f)) |
| 6568 |
("cddar" (1) #f 0 (#f)) |
| 6569 |
("cdddr" (1) #f 0 (#f)) |
| 6570 |
("caaaar" (1) #f 0 (#f)) |
| 6571 |
("caaadr" (1) #f 0 (#f)) |
| 6572 |
("caadar" (1) #f 0 (#f)) |
| 6573 |
("caaddr" (1) #f 0 (#f)) |
| 6574 |
("cadaar" (1) #f 0 (#f)) |
| 6575 |
("cadadr" (1) #f 0 (#f)) |
| 6576 |
("caddar" (1) #f 0 (#f)) |
| 6577 |
("cadddr" (1) #f 0 (#f)) |
| 6578 |
("cdaaar" (1) #f 0 (#f)) |
| 6579 |
("cdaadr" (1) #f 0 (#f)) |
| 6580 |
("cdadar" (1) #f 0 (#f)) |
| 6581 |
("cdaddr" (1) #f 0 (#f)) |
| 6582 |
("cddaar" (1) #f 0 (#f)) |
| 6583 |
("cddadr" (1) #f 0 (#f)) |
| 6584 |
("cdddar" (1) #f 0 (#f)) |
| 6585 |
("cddddr" (1) #f 0 (#f)) |
| 6586 |
("null?" (1) #f 0 boolean) |
| 6587 |
("list?" (1) #f 0 boolean) |
| 6588 |
("list" 0 #f () list) |
| 6589 |
("length" (1) #f 0 integer) |
| 6590 |
("append" 0 #f 0 list) |
| 6591 |
("reverse" (1) #f 0 list) |
| 6592 |
("list-ref" (2) #f 0 (#f)) |
| 6593 |
("memq" (2) #f 0 list) |
| 6594 |
("memv" (2) #f 0 list) |
| 6595 |
("member" (2) #f 0 list) |
| 6596 |
("assq" (2) #f 0 #f) |
| 6597 |
("assv" (2) #f 0 #f) |
| 6598 |
("assoc" (2) #f 0 #f) |
| 6599 |
("symbol?" (1) #f 0 boolean) |
| 6600 |
("symbol->string" (1) #f 0 string) |
| 6601 |
("string->symbol" (1) #f 0 symbol) |
| 6602 |
("number?" (1) #f 0 boolean) |
| 6603 |
("complex?" (1) #f 0 boolean) |
| 6604 |
("real?" (1) #f 0 boolean) |
| 6605 |
("rational?" (1) #f 0 boolean) |
| 6606 |
("integer?" (1) #f 0 boolean) |
| 6607 |
("exact?" (1) #f 0 boolean) |
| 6608 |
("inexact?" (1) #f 0 boolean) |
| 6609 |
("=" 0 #f 0 boolean) |
| 6610 |
("<" 0 #f 0 boolean) |
| 6611 |
(">" 0 #f 0 boolean) |
| 6612 |
("<=" 0 #f 0 boolean) |
| 6613 |
(">=" 0 #f 0 boolean) |
| 6614 |
("zero?" (1) #f 0 boolean) |
| 6615 |
("positive?" (1) #f 0 boolean) |
| 6616 |
("negative?" (1) #f 0 boolean) |
| 6617 |
("odd?" (1) #f 0 boolean) |
| 6618 |
("even?" (1) #f 0 boolean) |
| 6619 |
("max" 1 #f 0 number) |
| 6620 |
("min" 1 #f 0 number) |
| 6621 |
("+" 0 #f 0 number) |
| 6622 |
("*" 0 #f 0 number) |
| 6623 |
("-" 1 #f 0 number) |
| 6624 |
("/" 1 #f 0 number) |
| 6625 |
("abs" (1) #f 0 number) |
| 6626 |
("quotient" 1 #f 0 integer) |
| 6627 |
("remainder" (2) #f 0 integer) |
| 6628 |
("modulo" (2) #f 0 integer) |
| 6629 |
("gcd" 1 #f 0 integer) |
| 6630 |
("lcm" 1 #f 0 integer) |
| 6631 |
("numerator" (1) #f 0 integer) |
| 6632 |
("denominator" (1) #f 0 integer) |
| 6633 |
("floor" (1) #f 0 integer) |
| 6634 |
("ceiling" (1) #f 0 integer) |
| 6635 |
("truncate" (1) #f 0 integer) |
| 6636 |
("round" (1) #f 0 integer) |
| 6637 |
("rationalize" (2) #f 0 number) |
| 6638 |
("exp" (1) #f 0 number) |
| 6639 |
("log" (1) #f 0 number) |
| 6640 |
("sin" (1) #f 0 number) |
| 6641 |
("cos" (1) #f 0 number) |
| 6642 |
("tan" (1) #f 0 number) |
| 6643 |
("asin" (1) #f 0 number) |
| 6644 |
("acos" (1) #f 0 number) |
| 6645 |
("atan" (1 2) #f 0 number) |
| 6646 |
("sqrt" (1) #f 0 number) |
| 6647 |
("expt" (2) #f 0 number) |
| 6648 |
("make-rectangular" (2) #f 0 number) |
| 6649 |
("make-polar" (2) #f 0 number) |
| 6650 |
("real-part" (1) #f 0 real) |
| 6651 |
("imag-part" (1) #f 0 real) |
| 6652 |
("magnitude" (1) #f 0 real) |
| 6653 |
("angle" (1) #f 0 real) |
| 6654 |
("exact->inexact" (1) #f 0 number) |
| 6655 |
("inexact->exact" (1) #f 0 number) |
| 6656 |
("number->string" (1 2) #f 0 string) |
| 6657 |
("string->number" (1 2) #f 0 number) |
| 6658 |
("char?" (1) #f 0 boolean) |
| 6659 |
("char=?" 0 #f 0 boolean) |
| 6660 |
("char<?" 0 #f 0 boolean) |
| 6661 |
("char>?" 0 #f 0 boolean) |
| 6662 |
("char<=?" 0 #f 0 boolean) |
| 6663 |
("char>=?" 0 #f 0 boolean) |
| 6664 |
("char-ci=?" 0 #f 0 boolean) |
| 6665 |
("char-ci<?" 0 #f 0 boolean) |
| 6666 |
("char-ci>?" 0 #f 0 boolean) |
| 6667 |
("char-ci<=?" 0 #f 0 boolean) |
| 6668 |
("char-ci>=?" 0 #f 0 boolean) |
| 6669 |
("char-alphabetic?" (1) #f 0 boolean) |
| 6670 |
("char-numeric?" (1) #f 0 boolean) |
| 6671 |
("char-whitespace?" (1) #f 0 boolean) |
| 6672 |
("char-upper-case?" (1) #f 0 boolean) |
| 6673 |
("char-lower-case?" (1) #f 0 boolean) |
| 6674 |
("char->integer" (1) #f 0 integer) |
| 6675 |
("integer->char" (1) #f 0 char) |
| 6676 |
("char-upcase" (1) #f 0 char) |
| 6677 |
("char-downcase" (1) #f 0 char) |
| 6678 |
("string?" (1) #f 0 boolean) |
| 6679 |
("make-string" (1 2) #f 0 string) |
| 6680 |
("string" 0 #f 0 string) |
| 6681 |
("string-length" (1) #f 0 integer) |
| 6682 |
("string-ref" (2) #f 0 char) |
| 6683 |
("string-set!" (3) #t 0 string) |
| 6684 |
("string=?" 0 #f 0 boolean) |
| 6685 |
("string<?" 0 #f 0 boolean) |
| 6686 |
("string>?" 0 #f 0 boolean) |
| 6687 |
("string<=?" 0 #f 0 boolean) |
| 6688 |
("string>=?" 0 #f 0 boolean) |
| 6689 |
("string-ci=?" 0 #f 0 boolean) |
| 6690 |
("string-ci<?" 0 #f 0 boolean) |
| 6691 |
("string-ci>?" 0 #f 0 boolean) |
| 6692 |
("string-ci<=?" 0 #f 0 boolean) |
| 6693 |
("string-ci>=?" 0 #f 0 boolean) |
| 6694 |
("substring" (3) #f 0 string) |
| 6695 |
("string-append" 0 #f 0 string) |
| 6696 |
("vector?" (1) #f 0 boolean) |
| 6697 |
("make-vector" (1 2) #f (1) vector) |
| 6698 |
("vector" 0 #f () vector) |
| 6699 |
("vector-length" (1) #f 0 integer) |
| 6700 |
("vector-ref" (2) #f 0 (#f)) |
| 6701 |
("vector-set!" (3) #t (1 2) vector) |
| 6702 |
("procedure?" (1) #f 0 boolean) |
| 6703 |
("apply" 2 #t 0 (#f)) |
| 6704 |
("map" 2 #t 0 list) |
| 6705 |
("for-each" 2 #t 0 #f) |
| 6706 |
("call-with-current-continuation" (1) #t 0 (#f)) |
| 6707 |
("call-with-input-file" (2) #t 0 (#f)) |
| 6708 |
("call-with-output-file" (2) #t 0 (#f)) |
| 6709 |
("input-port?" (1) #f 0 boolean) |
| 6710 |
("output-port?" (1) #f 0 boolean) |
| 6711 |
("current-input-port" (0) #f 0 port) |
| 6712 |
("current-output-port" (0) #f 0 port) |
| 6713 |
("open-input-file" (1) #t 0 port) |
| 6714 |
("open-output-file" (1) #t 0 port) |
| 6715 |
("close-input-port" (1) #t 0 #f) |
| 6716 |
("close-output-port" (1) #t 0 #f) |
| 6717 |
("eof-object?" (1) #f 0 boolean) |
| 6718 |
("read" (0 1) #t 0 #f) |
| 6719 |
("read-char" (0 1) #t 0 #f) |
| 6720 |
("peek-char" (0 1) #t 0 #f) |
| 6721 |
("write" (0 1) #t 0 #f) |
| 6722 |
("display" (0 1) #t 0 #f) |
| 6723 |
("newline" (0 1) #t 0 #f) |
| 6724 |
("write-char" (1 2) #t 0 #f) |
| 6725 |
("list-tail" (2) #f 0 (#f)) |
| 6726 |
("string->list" (1) #f 0 list) |
| 6727 |
("list->string" (1) #f 0 string) |
| 6728 |
("string-copy" (1) #f 0 string) |
| 6729 |
("string-fill!" (2) #t 0 string) |
| 6730 |
("vector->list" (1) #f 0 list) |
| 6731 |
("list->vector" (1) #f 0 vector) |
| 6732 |
("vector-fill!" (2) #t 0 vector) |
| 6733 |
("force" (1) #t 0 #f) |
| 6734 |
("with-input-from-file" (2) #t 0 (#f)) |
| 6735 |
("with-output-to-file" (2) #t 0 (#f)) |
| 6736 |
("char-ready?" (0 1) #f 0 boolean) |
| 6737 |
("load" (1) #t 0 (#f)) |
| 6738 |
("transcript-on" (1) #t 0 #f) |
| 6739 |
("transcript-off" (0) #t 0 #f) |
| 6740 |
("touch" (1) #t 0 #f) |
| 6741 |
("##type" (1) #f () integer) |
| 6742 |
("##type-cast" (2) #f () (#f)) |
| 6743 |
("##subtype" (1) #f () integer) |
| 6744 |
("##subtype-set!" (2) #t () #f) |
| 6745 |
("##not" (1) #f () boolean) |
| 6746 |
("##null?" (1) #f () boolean) |
| 6747 |
("##unassigned?" (1) #f () boolean) |
| 6748 |
("##unbound?" (1) #f () boolean) |
| 6749 |
("##eq?" (2) #f () boolean) |
| 6750 |
("##fixnum?" (1) #f () boolean) |
| 6751 |
("##flonum?" (1) #f () boolean) |
| 6752 |
("##special?" (1) #f () boolean) |
| 6753 |
("##pair?" (1) #f () boolean) |
| 6754 |
("##subtyped?" (1) #f () boolean) |
| 6755 |
("##procedure?" (1) #f () boolean) |
| 6756 |
("##placeholder?" (1) #f () boolean) |
| 6757 |
("##vector?" (1) #f () boolean) |
| 6758 |
("##symbol?" (1) #f () boolean) |
| 6759 |
("##ratnum?" (1) #f () boolean) |
| 6760 |
("##cpxnum?" (1) #f () boolean) |
| 6761 |
("##string?" (1) #f () boolean) |
| 6762 |
("##bignum?" (1) #f () boolean) |
| 6763 |
("##char?" (1) #f () boolean) |
| 6764 |
("##closure?" (1) #f () boolean) |
| 6765 |
("##subprocedure?" (1) #f () boolean) |
| 6766 |
("##return-dynamic-env-bind?" (1) #f () boolean) |
| 6767 |
("##fixnum.+" 0 #f () integer) |
| 6768 |
("##fixnum.*" 0 #f () integer) |
| 6769 |
("##fixnum.-" 1 #f () integer) |
| 6770 |
("##fixnum.quotient" (2) #f () integer) |
| 6771 |
("##fixnum.remainder" (2) #f () integer) |
| 6772 |
("##fixnum.modulo" (2) #f () integer) |
| 6773 |
("##fixnum.logior" 0 #f () integer) |
| 6774 |
("##fixnum.logxor" 0 #f () integer) |
| 6775 |
("##fixnum.logand" 0 #f () integer) |
| 6776 |
("##fixnum.lognot" (1) #f () integer) |
| 6777 |
("##fixnum.ash" (2) #f () integer) |
| 6778 |
("##fixnum.lsh" (2) #f () integer) |
| 6779 |
("##fixnum.zero?" (1) #f () boolean) |
| 6780 |
("##fixnum.positive?" (1) #f () boolean) |
| 6781 |
("##fixnum.negative?" (1) #f () boolean) |
| 6782 |
("##fixnum.odd?" (1) #f () boolean) |
| 6783 |
("##fixnum.even?" (1) #f () boolean) |
| 6784 |
("##fixnum.=" 0 #f () boolean) |
| 6785 |
("##fixnum.<" 0 #f () boolean) |
| 6786 |
("##fixnum.>" 0 #f () boolean) |
| 6787 |
("##fixnum.<=" 0 #f () boolean) |
| 6788 |
("##fixnum.>=" 0 #f () boolean) |
| 6789 |
("##flonum.->fixnum" (1) #f () integer) |
| 6790 |
("##flonum.<-fixnum" (1) #f () real) |
| 6791 |
("##flonum.+" 0 #f () real) |
| 6792 |
("##flonum.*" 0 #f () real) |
| 6793 |
("##flonum.-" 1 #f () real) |
| 6794 |
("##flonum./" 1 #f () real) |
| 6795 |
("##flonum.abs" (1) #f () real) |
| 6796 |
("##flonum.truncate" (1) #f () real) |
| 6797 |
("##flonum.round" (1) #f () real) |
| 6798 |
("##flonum.exp" (1) #f () real) |
| 6799 |
("##flonum.log" (1) #f () real) |
| 6800 |
("##flonum.sin" (1) #f () real) |
| 6801 |
("##flonum.cos" (1) #f () real) |
| 6802 |
("##flonum.tan" (1) #f () real) |
| 6803 |
("##flonum.asin" (1) #f () real) |
| 6804 |
("##flonum.acos" (1) #f () real) |
| 6805 |
("##flonum.atan" (1) #f () real) |
| 6806 |
("##flonum.sqrt" (1) #f () real) |
| 6807 |
("##flonum.zero?" (1) #f () boolean) |
| 6808 |
("##flonum.positive?" (1) #f () boolean) |
| 6809 |
("##flonum.negative?" (1) #f () boolean) |
| 6810 |
("##flonum.=" 0 #f () boolean) |
| 6811 |
("##flonum.<" 0 #f () boolean) |
| 6812 |
("##flonum.>" 0 #f () boolean) |
| 6813 |
("##flonum.<=" 0 #f () boolean) |
| 6814 |
("##flonum.>=" 0 #f () boolean) |
| 6815 |
("##char=?" 0 #f () boolean) |
| 6816 |
("##char<?" 0 #f () boolean) |
| 6817 |
("##char>?" 0 #f () boolean) |
| 6818 |
("##char<=?" 0 #f () boolean) |
| 6819 |
("##char>=?" 0 #f () boolean) |
| 6820 |
("##cons" (2) #f () pair) |
| 6821 |
("##set-car!" (2) #t () pair) |
| 6822 |
("##set-cdr!" (2) #t () pair) |
| 6823 |
("##car" (1) #f () (#f)) |
| 6824 |
("##cdr" (1) #f () (#f)) |
| 6825 |
("##caar" (1) #f () (#f)) |
| 6826 |
("##cadr" (1) #f () (#f)) |
| 6827 |
("##cdar" (1) #f () (#f)) |
| 6828 |
("##cddr" (1) #f () (#f)) |
| 6829 |
("##caaar" (1) #f () (#f)) |
| 6830 |
("##caadr" (1) #f () (#f)) |
| 6831 |
("##cadar" (1) #f () (#f)) |
| 6832 |
("##caddr" (1) #f () (#f)) |
| 6833 |
("##cdaar" (1) #f () (#f)) |
| 6834 |
("##cdadr" (1) #f () (#f)) |
| 6835 |
("##cddar" (1) #f () (#f)) |
| 6836 |
("##cdddr" (1) #f () (#f)) |
| 6837 |
("##caaaar" (1) #f () (#f)) |
| 6838 |
("##caaadr" (1) #f () (#f)) |
| 6839 |
("##caadar" (1) #f () (#f)) |
| 6840 |
("##caaddr" (1) #f () (#f)) |
| 6841 |
("##cadaar" (1) #f () (#f)) |
| 6842 |
("##cadadr" (1) #f () (#f)) |
| 6843 |
("##caddar" (1) #f () (#f)) |
| 6844 |
("##cadddr" (1) #f () (#f)) |
| 6845 |
("##cdaaar" (1) #f () (#f)) |
| 6846 |
("##cdaadr" (1) #f () (#f)) |
| 6847 |
("##cdadar" (1) #f () (#f)) |
| 6848 |
("##cdaddr" (1) #f () (#f)) |
| 6849 |
("##cddaar" (1) #f () (#f)) |
| 6850 |
("##cddadr" (1) #f () (#f)) |
| 6851 |
("##cdddar" (1) #f () (#f)) |
| 6852 |
("##cddddr" (1) #f () (#f)) |
| 6853 |
("##make-cell" (1) #f () pair) |
| 6854 |
("##cell-ref" (1) #f () (#f)) |
| 6855 |
("##cell-set!" (2) #t () pair) |
| 6856 |
("##vector" 0 #f () vector) |
| 6857 |
("##make-vector" (2) #f () vector) |
| 6858 |
("##vector-length" (1) #f () integer) |
| 6859 |
("##vector-ref" (2) #f () (#f)) |
| 6860 |
("##vector-set!" (3) #t () vector) |
| 6861 |
("##vector-shrink!" (2) #t () vector) |
| 6862 |
("##string" 0 #f () string) |
| 6863 |
("##make-string" (2) #f () string) |
| 6864 |
("##string-length" (1) #f () integer) |
| 6865 |
("##string-ref" (2) #f () char) |
| 6866 |
("##string-set!" (3) #t () string) |
| 6867 |
("##string-shrink!" (2) #t () string) |
| 6868 |
("##vector8" 0 #f () string) |
| 6869 |
("##make-vector8" (2) #f () string) |
| 6870 |
("##vector8-length" (1) #f () integer) |
| 6871 |
("##vector8-ref" (2) #f () integer) |
| 6872 |
("##vector8-set!" (3) #t () string) |
| 6873 |
("##vector8-shrink!" (2) #t () string) |
| 6874 |
("##vector16" 0 #f () string) |
| 6875 |
("##make-vector16" (2) #f () string) |
| 6876 |
("##vector16-length" (1) #f () integer) |
| 6877 |
("##vector16-ref" (2) #f () integer) |
| 6878 |
("##vector16-set!" (3) #t () string) |
| 6879 |
("##vector16-shrink!" (2) #t () string) |
| 6880 |
("##closure-code" (1) #f () #f) |
| 6881 |
("##closure-ref" (2) #f () (#f)) |
| 6882 |
("##closure-set!" (3) #t () #f) |
| 6883 |
("##subprocedure-id" (1) #f () #f) |
| 6884 |
("##subprocedure-parent" (1) #f () #f) |
| 6885 |
("##return-fs" (1) #f () #f) |
| 6886 |
("##return-link" (1) #f () #f) |
| 6887 |
("##procedure-info" (1) #f () #f) |
| 6888 |
("##pstate" (0) #f () #f) |
| 6889 |
("##make-placeholder" (1) #f 0 (#f)) |
| 6890 |
("##touch" (1) #t 0 #f) |
| 6891 |
("##apply" (2) #t () (#f)) |
| 6892 |
("##call-with-current-continuation" (1) #t () (#f)) |
| 6893 |
("##global-var" (1) #t () #f) |
| 6894 |
("##global-var-ref" (1) #f () (#f)) |
| 6895 |
("##global-var-set!" (2) #t () #f) |
| 6896 |
("##atomic-car" (1) #f () (#f)) |
| 6897 |
("##atomic-cdr" (1) #f () (#f)) |
| 6898 |
("##atomic-set-car!" (2) #t () pair) |
| 6899 |
("##atomic-set-cdr!" (2) #t () pair) |
| 6900 |
("##atomic-set-car-if-eq?!" (3) #t () boolean) |
| 6901 |
("##atomic-set-cdr-if-eq?!" (3) #t () boolean) |
| 6902 |
("##quasi-append" 0 #f 0 list) |
| 6903 |
("##quasi-list" 0 #f () list) |
| 6904 |
("##quasi-cons" (2) #f () pair) |
| 6905 |
("##quasi-list->vector" (1) #f 0 vector) |
| 6906 |
("##case-memv" (2) #f 0 list))) |
| 6907 |
(define ofile-version-major 5) |
| 6908 |
(define ofile-version-minor 0) |
| 6909 |
(define prim-proc-prefix 1) |
| 6910 |
(define user-proc-prefix 2) |
| 6911 |
(define pair-prefix 3) |
| 6912 |
(define flonum-prefix 4) |
| 6913 |
(define local-object-bits -524281) |
| 6914 |
(define symbol-object-bits -393209) |
| 6915 |
(define prim-proc-object-bits -262137) |
| 6916 |
(define padding-tag 0) |
| 6917 |
(define end-of-code-tag 32768) |
| 6918 |
(define m68020-proc-code-tag 32769) |
| 6919 |
(define m68881-proc-code-tag 32770) |
| 6920 |
(define stat-tag 32771) |
| 6921 |
(define global-var-ref-tag 34816) |
| 6922 |
(define global-var-set-tag 36864) |
| 6923 |
(define global-var-ref-jump-tag 38912) |
| 6924 |
(define prim-proc-ref-tag 40960) |
| 6925 |
(define local-proc-ref-tag 49152) |
| 6926 |
(define long-index-mask 16383) |
| 6927 |
(define word-index-mask 2047) |
| 6928 |
(define (ofile.begin! filename add-obj) |
| 6929 |
(set! ofile-add-obj add-obj) |
| 6930 |
(set! ofile-syms (queue-empty)) |
| 6931 |
; (set! *ofile-port1* (open-output-file (string-append filename ".O"))) |
| 6932 |
(if ofile-asm? |
| 6933 |
(begin |
| 6934 |
(set! *ofile-port2* |
| 6935 |
(asm-open-output-file (string-append filename ".asm"))) |
| 6936 |
(set! *ofile-pos* 0))) |
| 6937 |
(ofile-word ofile-version-major) |
| 6938 |
(ofile-word ofile-version-minor) |
| 6939 |
'()) |
| 6940 |
(define (ofile.end!) |
| 6941 |
(ofile-line "") |
| 6942 |
; (close-output-port *ofile-port1*) |
| 6943 |
(if ofile-asm? (asm-close-output-port *ofile-port2*)) |
| 6944 |
'()) |
| 6945 |
(define asm-output '()) |
| 6946 |
(define asm-line '()) |
| 6947 |
(define (asm-open-output-file filename) |
| 6948 |
(set! asm-output '()) |
| 6949 |
(set! asm-line '())) |
| 6950 |
(define (asm-close-output-port asm-port) #f) |
| 6951 |
(define (asm-newline asm-port) (asm-display char-newline asm-port)) |
| 6952 |
(define (asm-display obj asm-port) |
| 6953 |
(if (eqv? obj char-newline) |
| 6954 |
(begin |
| 6955 |
(set! asm-output |
| 6956 |
(cons (apply string-append (reverse asm-line)) asm-output)) |
| 6957 |
(set! asm-line '())) |
| 6958 |
(set! asm-line |
| 6959 |
(cons (cond ((string? obj) obj) |
| 6960 |
((char? obj) (if (eqv? obj char-tab) " " (string obj))) |
| 6961 |
((number? obj) (number->string obj)) |
| 6962 |
(else (compiler-internal-error "asm-display" obj))) |
| 6963 |
asm-line)))) |
| 6964 |
(define (asm-output-get) (reverse asm-output)) |
| 6965 |
(define *ofile-port1* '()) |
| 6966 |
(define *ofile-port2* '()) |
| 6967 |
(define *ofile-pos* '()) |
| 6968 |
(define ofile-nl char-newline) |
| 6969 |
(define ofile-tab char-tab) |
| 6970 |
(define ofile-asm? '()) |
| 6971 |
(define ofile-asm-bits? #f) |
| 6972 |
(define ofile-asm-gvm? #f) |
| 6973 |
(define ofile-stats? '()) |
| 6974 |
(define ofile-add-obj '()) |
| 6975 |
(define ofile-syms '()) |
| 6976 |
(define (ofile-word n) |
| 6977 |
(let ((n (modulo n 65536))) |
| 6978 |
(if (and ofile-asm? ofile-asm-bits?) |
| 6979 |
(let () |
| 6980 |
(define (ofile-display x) |
| 6981 |
(asm-display x *ofile-port2*) |
| 6982 |
(cond ((eq? x ofile-nl) (set! *ofile-pos* 0)) |
| 6983 |
((eq? x ofile-tab) |
| 6984 |
(set! *ofile-pos* (* (quotient (+ *ofile-pos* 8) 8) 8))) |
| 6985 |
(else (set! *ofile-pos* (+ *ofile-pos* (string-length x)))))) |
| 6986 |
(if (> *ofile-pos* 64) (ofile-display ofile-nl)) |
| 6987 |
(if (= *ofile-pos* 0) (ofile-display " .word") (ofile-display ",")) |
| 6988 |
(ofile-display ofile-tab) |
| 6989 |
(let ((s (make-string 6 #\0))) |
| 6990 |
(string-set! s 1 #\x) |
| 6991 |
(let loop ((i 5) (n n)) |
| 6992 |
(if (> n 0) |
| 6993 |
(begin |
| 6994 |
(string-set! |
| 6995 |
s |
| 6996 |
i |
| 6997 |
(string-ref "0123456789ABCDEF" (remainder n 16))) |
| 6998 |
(loop (- i 1) (quotient n 16))))) |
| 6999 |
(ofile-display s)))) |
| 7000 |
' (write-word n *ofile-port1*))) |
| 7001 |
(define (ofile-long x) (ofile-word (upper-16bits x)) (ofile-word x)) |
| 7002 |
(define (ofile-string s) |
| 7003 |
(let ((len (string-length s))) |
| 7004 |
(define (ref i) (if (>= i len) 0 (character-encoding (string-ref s i)))) |
| 7005 |
(let loop ((i 0)) |
| 7006 |
(if (< i len) |
| 7007 |
(begin |
| 7008 |
(ofile-word (+ (* (ref i) 256) (ref (+ i 1)))) |
| 7009 |
(loop (+ i 2))))) |
| 7010 |
(if (= (remainder len 2) 0) (ofile-word 0)))) |
| 7011 |
(define (ofile-wsym tag name) |
| 7012 |
(let ((n (string-pos-in-list name (queue->list ofile-syms)))) |
| 7013 |
(if n |
| 7014 |
(ofile-word (+ tag n)) |
| 7015 |
(let ((m (length (queue->list ofile-syms)))) |
| 7016 |
(queue-put! ofile-syms name) |
| 7017 |
(ofile-word (+ tag word-index-mask)) |
| 7018 |
(ofile-string name))))) |
| 7019 |
(define (ofile-lsym tag name) |
| 7020 |
(let ((n (string-pos-in-list name (queue->list ofile-syms)))) |
| 7021 |
(if n |
| 7022 |
(ofile-long (+ tag (* n 8))) |
| 7023 |
(let ((m (length (queue->list ofile-syms)))) |
| 7024 |
(queue-put! ofile-syms name) |
| 7025 |
(ofile-long (+ tag (* long-index-mask 8))) |
| 7026 |
(ofile-string name))))) |
| 7027 |
(define (ofile-ref obj) |
| 7028 |
(let ((n (obj-encoding obj))) |
| 7029 |
(if n |
| 7030 |
(ofile-long n) |
| 7031 |
(if (symbol-object? obj) |
| 7032 |
(begin (ofile-lsym symbol-object-bits (symbol->string obj))) |
| 7033 |
(let ((m (ofile-add-obj obj))) |
| 7034 |
(if m |
| 7035 |
(ofile-long (+ local-object-bits (* m 8))) |
| 7036 |
(begin |
| 7037 |
(ofile-lsym |
| 7038 |
prim-proc-object-bits |
| 7039 |
(proc-obj-name obj))))))))) |
| 7040 |
(define (ofile-prim-proc s) |
| 7041 |
(ofile-long prim-proc-prefix) |
| 7042 |
(ofile-wsym 0 s) |
| 7043 |
(ofile-comment (list "| #[primitive " s "] ="))) |
| 7044 |
(define (ofile-user-proc) (ofile-long user-proc-prefix)) |
| 7045 |
(define (ofile-line s) |
| 7046 |
(if ofile-asm? |
| 7047 |
(begin |
| 7048 |
(if (> *ofile-pos* 0) (asm-newline *ofile-port2*)) |
| 7049 |
(asm-display s *ofile-port2*) |
| 7050 |
(asm-newline *ofile-port2*) |
| 7051 |
(set! *ofile-pos* 0)))) |
| 7052 |
(define (ofile-tabs-to n) |
| 7053 |
(let loop () |
| 7054 |
(if (< *ofile-pos* n) |
| 7055 |
(begin |
| 7056 |
(asm-display ofile-tab *ofile-port2*) |
| 7057 |
(set! *ofile-pos* (* (quotient (+ *ofile-pos* 8) 8) 8)) |
| 7058 |
(loop))))) |
| 7059 |
(define (ofile-comment l) |
| 7060 |
(if ofile-asm? |
| 7061 |
(let () |
| 7062 |
(if ofile-asm-bits? |
| 7063 |
(begin (ofile-tabs-to 32) (asm-display "|" *ofile-port2*))) |
| 7064 |
(for-each (lambda (x) (asm-display x *ofile-port2*)) l) |
| 7065 |
(asm-newline *ofile-port2*) |
| 7066 |
(set! *ofile-pos* 0)))) |
| 7067 |
(define (ofile-gvm-instr code) |
| 7068 |
(if (and ofile-asm? ofile-asm-gvm?) |
| 7069 |
(let ((gvm-instr (code-gvm-instr code)) (sn (code-slots-needed code))) |
| 7070 |
(if (> *ofile-pos* 0) |
| 7071 |
(begin (asm-newline *ofile-port2*) (set! *ofile-pos* 0))) |
| 7072 |
(if ofile-asm-bits? (ofile-tabs-to 32)) |
| 7073 |
(asm-display "| GVM: [" *ofile-port2*) |
| 7074 |
(asm-display sn *ofile-port2*) |
| 7075 |
(asm-display "] " *ofile-port2*) |
| 7076 |
(asm-newline *ofile-port2*) |
| 7077 |
(set! *ofile-pos* 0)))) |
| 7078 |
(define (ofile-stat stat) |
| 7079 |
(define (obj->string x) |
| 7080 |
(cond ((string? x) x) |
| 7081 |
((symbol-object? x) (symbol->string x)) |
| 7082 |
((number? x) (number->string x)) |
| 7083 |
((false-object? x) "#f") |
| 7084 |
((eq? x #t) "#t") |
| 7085 |
((null? x) "()") |
| 7086 |
((pair? x) |
| 7087 |
(let loop ((l1 (cdr x)) (l2 (list (obj->string (car x)) "("))) |
| 7088 |
(cond ((pair? l1) |
| 7089 |
(loop (cdr l1) |
| 7090 |
(cons (obj->string (car l1)) (cons " " l2)))) |
| 7091 |
((null? l1) (apply string-append (reverse (cons ")" l2)))) |
| 7092 |
(else |
| 7093 |
(apply string-append |
| 7094 |
(reverse (cons ")" |
| 7095 |
(cons (obj->string l1) |
| 7096 |
(cons " . " l2))))))))) |
| 7097 |
(else |
| 7098 |
(compiler-internal-error |
| 7099 |
"ofile-stat, can't convert to string 'x'" |
| 7100 |
x)))) |
| 7101 |
(ofile-string (obj->string stat))) |
| 7102 |
(define (upper-16bits x) |
| 7103 |
(cond ((>= x 0) (quotient x 65536)) |
| 7104 |
((>= x (- 65536)) -1) |
| 7105 |
(else (- (quotient (+ x 65537) 65536) 2)))) |
| 7106 |
(define type-fixnum 0) |
| 7107 |
(define type-flonum 1) |
| 7108 |
(define type-special 7) |
| 7109 |
(define type-pair 4) |
| 7110 |
(define type-placeholder 5) |
| 7111 |
(define type-subtyped 3) |
| 7112 |
(define type-procedure 2) |
| 7113 |
(define subtype-vector 0) |
| 7114 |
(define subtype-symbol 1) |
| 7115 |
(define subtype-port 2) |
| 7116 |
(define subtype-ratnum 3) |
| 7117 |
(define subtype-cpxnum 4) |
| 7118 |
(define subtype-string 16) |
| 7119 |
(define subtype-bignum 17) |
| 7120 |
(define data-false (- 33686019)) |
| 7121 |
(define data-null (- 67372037)) |
| 7122 |
(define data-true -2) |
| 7123 |
(define data-undef -3) |
| 7124 |
(define data-unass -4) |
| 7125 |
(define data-unbound -5) |
| 7126 |
(define data-eof -6) |
| 7127 |
(define data-max-fixnum 268435455) |
| 7128 |
(define data-min-fixnum (- 268435456)) |
| 7129 |
(define (make-encoding data type) (+ (* data 8) type)) |
| 7130 |
(define (obj-type obj) |
| 7131 |
(cond ((false-object? obj) 'special) |
| 7132 |
((undef-object? obj) 'special) |
| 7133 |
((symbol-object? obj) 'subtyped) |
| 7134 |
((proc-obj? obj) 'procedure) |
| 7135 |
((eq? obj #t) 'special) |
| 7136 |
((null? obj) 'special) |
| 7137 |
((pair? obj) 'pair) |
| 7138 |
((number? obj) |
| 7139 |
(cond ((and (integer? obj) |
| 7140 |
(exact? obj) |
| 7141 |
(>= obj data-min-fixnum) |
| 7142 |
(<= obj data-max-fixnum)) |
| 7143 |
'fixnum) |
| 7144 |
((and (inexact? (real-part obj)) |
| 7145 |
(zero? (imag-part obj)) |
| 7146 |
;;; AZIZ: test looks wrong |
| 7147 |
(exact? (imag-part obj))) |
| 7148 |
'flonum) |
| 7149 |
(else 'subtyped))) |
| 7150 |
((char? obj) 'special) |
| 7151 |
(else 'subtyped))) |
| 7152 |
(define (obj-subtype obj) |
| 7153 |
(cond ((symbol-object? obj) 'symbol) |
| 7154 |
((number? obj) |
| 7155 |
(cond ((and (integer? obj) (exact? obj)) 'bignum) |
| 7156 |
((and (rational? obj) (exact? obj)) 'ratnum) |
| 7157 |
(else 'cpxnum))) |
| 7158 |
((vector? obj) 'vector) |
| 7159 |
((string? obj) 'string) |
| 7160 |
(else |
| 7161 |
(compiler-internal-error "obj-subtype, unknown object 'obj'" obj)))) |
| 7162 |
(define (obj-type-tag obj) |
| 7163 |
(case (obj-type obj) |
| 7164 |
((fixnum) type-fixnum) |
| 7165 |
((flonum) type-flonum) |
| 7166 |
((special) type-special) |
| 7167 |
((pair) type-pair) |
| 7168 |
((subtyped) type-subtyped) |
| 7169 |
((procedure) type-procedure) |
| 7170 |
(else (compiler-internal-error "obj-type-tag, unknown object 'obj'" obj)))) |
| 7171 |
(define (obj-encoding obj) |
| 7172 |
(case (obj-type obj) |
| 7173 |
((fixnum) (make-encoding obj type-fixnum)) |
| 7174 |
((special) |
| 7175 |
(make-encoding |
| 7176 |
(cond ((false-object? obj) data-false) |
| 7177 |
((undef-object? obj) data-undef) |
| 7178 |
((eq? obj #t) data-true) |
| 7179 |
((null? obj) data-null) |
| 7180 |
((char? obj) (character-encoding obj)) |
| 7181 |
(else |
| 7182 |
(compiler-internal-error |
| 7183 |
"obj-encoding, unknown SPECIAL object 'obj'" |
| 7184 |
obj))) |
| 7185 |
type-special)) |
| 7186 |
(else #f))) |
| 7187 |
(define bits-false (make-encoding data-false type-special)) |
| 7188 |
(define bits-null (make-encoding data-null type-special)) |
| 7189 |
(define bits-true (make-encoding data-true type-special)) |
| 7190 |
(define bits-unass (make-encoding data-unass type-special)) |
| 7191 |
(define bits-unbound (make-encoding data-unbound type-special)) |
| 7192 |
(define (asm.begin!) |
| 7193 |
(set! asm-code-queue (queue-empty)) |
| 7194 |
(set! asm-const-queue (queue-empty)) |
| 7195 |
'()) |
| 7196 |
(define (asm.end! debug-info) |
| 7197 |
(asm-assemble! debug-info) |
| 7198 |
(set! asm-code-queue '()) |
| 7199 |
(set! asm-const-queue '()) |
| 7200 |
'()) |
| 7201 |
(define asm-code-queue '()) |
| 7202 |
(define asm-const-queue '()) |
| 7203 |
(define (asm-word x) (queue-put! asm-code-queue (modulo x 65536))) |
| 7204 |
(define (asm-long x) (asm-word (upper-16bits x)) (asm-word x)) |
| 7205 |
(define (asm-label lbl label-descr) |
| 7206 |
(queue-put! asm-code-queue (cons 'label (cons lbl label-descr)))) |
| 7207 |
(define (asm-comment x) (queue-put! asm-code-queue (cons 'comment x))) |
| 7208 |
(define (asm-align n offset) |
| 7209 |
(queue-put! asm-code-queue (cons 'align (cons n offset)))) |
| 7210 |
(define (asm-ref-glob glob) |
| 7211 |
(queue-put! |
| 7212 |
asm-code-queue |
| 7213 |
(cons 'ref-glob (symbol->string (glob-name glob))))) |
| 7214 |
(define (asm-set-glob glob) |
| 7215 |
(queue-put! |
| 7216 |
asm-code-queue |
| 7217 |
(cons 'set-glob (symbol->string (glob-name glob))))) |
| 7218 |
(define (asm-ref-glob-jump glob) |
| 7219 |
(queue-put! |
| 7220 |
asm-code-queue |
| 7221 |
(cons 'ref-glob-jump (symbol->string (glob-name glob))))) |
| 7222 |
(define (asm-proc-ref num offset) |
| 7223 |
(queue-put! asm-code-queue (cons 'proc-ref (cons num offset)))) |
| 7224 |
(define (asm-prim-ref proc offset) |
| 7225 |
(queue-put! |
| 7226 |
asm-code-queue |
| 7227 |
(cons 'prim-ref (cons (proc-obj-name proc) offset)))) |
| 7228 |
(define (asm-m68020-proc) (queue-put! asm-code-queue '(m68020-proc))) |
| 7229 |
(define (asm-m68881-proc) (queue-put! asm-code-queue '(m68881-proc))) |
| 7230 |
(define (asm-stat x) (queue-put! asm-code-queue (cons 'stat x))) |
| 7231 |
(define (asm-brel type lbl) |
| 7232 |
(queue-put! asm-code-queue (cons 'brab (cons type lbl)))) |
| 7233 |
(define (asm-wrel lbl offs) |
| 7234 |
(queue-put! asm-code-queue (cons 'wrel (cons lbl offs)))) |
| 7235 |
(define (asm-lrel lbl offs n) |
| 7236 |
(queue-put! asm-code-queue (cons 'lrel (cons lbl (cons offs n))))) |
| 7237 |
(define (asm-assemble! debug-info) |
| 7238 |
(define header-offset 2) |
| 7239 |
(define ref-glob-len 2) |
| 7240 |
(define set-glob-len 10) |
| 7241 |
(define ref-glob-jump-len 2) |
| 7242 |
(define proc-ref-len 4) |
| 7243 |
(define prim-ref-len 4) |
| 7244 |
(define stat-len 4) |
| 7245 |
(define (padding loc n offset) (modulo (- offset loc) n)) |
| 7246 |
(queue-put! asm-const-queue debug-info) |
| 7247 |
(asm-align 4 0) |
| 7248 |
(emit-label const-lbl) |
| 7249 |
(let ((code-list (queue->list asm-code-queue)) |
| 7250 |
(const-list (queue->list asm-const-queue))) |
| 7251 |
(let* ((fix-list |
| 7252 |
(let loop ((l code-list) (len header-offset) (x '())) |
| 7253 |
(if (null? l) |
| 7254 |
(reverse x) |
| 7255 |
(let ((part (car l)) (rest (cdr l))) |
| 7256 |
(if (pair? part) |
| 7257 |
(case (car part) |
| 7258 |
((label align brab) |
| 7259 |
(loop rest 0 (cons (cons len part) x))) |
| 7260 |
((wrel) (loop rest (+ len 2) x)) |
| 7261 |
((lrel) (loop rest (+ len 4) x)) |
| 7262 |
((ref-glob) (loop rest (+ len ref-glob-len) x)) |
| 7263 |
((set-glob) (loop rest (+ len set-glob-len) x)) |
| 7264 |
((ref-glob-jump) |
| 7265 |
(loop rest (+ len ref-glob-jump-len) x)) |
| 7266 |
((proc-ref) (loop rest (+ len proc-ref-len) x)) |
| 7267 |
((prim-ref) (loop rest (+ len prim-ref-len) x)) |
| 7268 |
((stat) (loop rest (+ len stat-len) x)) |
| 7269 |
((comment m68020-proc m68881-proc) (loop rest len x)) |
| 7270 |
(else |
| 7271 |
(compiler-internal-error |
| 7272 |
"asm-assemble!, unknown code list element" |
| 7273 |
part))) |
| 7274 |
(loop rest (+ len 2) x)))))) |
| 7275 |
(lbl-list |
| 7276 |
(let loop ((l fix-list) (x '())) |
| 7277 |
(if (null? l) |
| 7278 |
x |
| 7279 |
(let ((part (cdar l)) (rest (cdr l))) |
| 7280 |
(if (eq? (car part) 'label) |
| 7281 |
(loop rest (cons (cons (cadr part) part) x)) |
| 7282 |
(loop rest x))))))) |
| 7283 |
(define (replace-lbl-refs-by-pointer-to-label) |
| 7284 |
(let loop ((l code-list)) |
| 7285 |
(if (not (null? l)) |
| 7286 |
(let ((part (car l)) (rest (cdr l))) |
| 7287 |
(if (pair? part) |
| 7288 |
(case (car part) |
| 7289 |
((brab) |
| 7290 |
(set-cdr! (cdr part) (cdr (assq (cddr part) lbl-list)))) |
| 7291 |
((wrel) |
| 7292 |
(set-car! (cdr part) (cdr (assq (cadr part) lbl-list)))) |
| 7293 |
((lrel) |
| 7294 |
(set-car! |
| 7295 |
(cdr part) |
| 7296 |
(cdr (assq (cadr part) lbl-list)))))) |
| 7297 |
(loop rest))))) |
| 7298 |
(define (assign-loc-to-labels) |
| 7299 |
(let loop ((l fix-list) (loc 0)) |
| 7300 |
(if (not (null? l)) |
| 7301 |
(let* ((first (car l)) |
| 7302 |
(rest (cdr l)) |
| 7303 |
(len (car first)) |
| 7304 |
(cur-loc (+ loc len)) |
| 7305 |
(part (cdr first))) |
| 7306 |
(case (car part) |
| 7307 |
((label) |
| 7308 |
(if (cddr part) |
| 7309 |
(vector-set! |
| 7310 |
(cddr part) |
| 7311 |
0 |
| 7312 |
(quotient (- cur-loc header-offset) 8))) |
| 7313 |
(set-car! (cdr part) cur-loc) |
| 7314 |
(loop rest cur-loc)) |
| 7315 |
((align) |
| 7316 |
(loop rest |
| 7317 |
(+ cur-loc |
| 7318 |
(padding cur-loc (cadr part) (cddr part))))) |
| 7319 |
((brab) (loop rest (+ cur-loc 2))) |
| 7320 |
((braw) (loop rest (+ cur-loc 4))) |
| 7321 |
(else |
| 7322 |
(compiler-internal-error |
| 7323 |
"assign-loc-to-labels, unknown code list element" |
| 7324 |
part))))))) |
| 7325 |
(define (branch-tensioning-pass) |
| 7326 |
(assign-loc-to-labels) |
| 7327 |
(let loop ((changed? #f) (l fix-list) (loc 0)) |
| 7328 |
(if (null? l) |
| 7329 |
(if changed? (branch-tensioning-pass)) |
| 7330 |
(let* ((first (car l)) |
| 7331 |
(rest (cdr l)) |
| 7332 |
(len (car first)) |
| 7333 |
(cur-loc (+ loc len)) |
| 7334 |
(part (cdr first))) |
| 7335 |
(case (car part) |
| 7336 |
((label) (loop changed? rest cur-loc)) |
| 7337 |
((align) |
| 7338 |
(loop changed? |
| 7339 |
rest |
| 7340 |
(+ cur-loc |
| 7341 |
(padding cur-loc (cadr part) (cddr part))))) |
| 7342 |
((brab) |
| 7343 |
(let ((dist (- (cadr (cddr part)) (+ cur-loc 2)))) |
| 7344 |
(if (or (< dist -128) (> dist 127) (= dist 0)) |
| 7345 |
(begin |
| 7346 |
(set-car! part 'braw) |
| 7347 |
(loop #t rest (+ cur-loc 2))) |
| 7348 |
(loop changed? rest (+ cur-loc 2))))) |
| 7349 |
((braw) (loop changed? rest (+ cur-loc 4))) |
| 7350 |
(else |
| 7351 |
(compiler-internal-error |
| 7352 |
"branch-tensioning-pass, unknown code list element" |
| 7353 |
part))))))) |
| 7354 |
(define (write-block start-loc end-loc start end) |
| 7355 |
(if (> end-loc start-loc) |
| 7356 |
(ofile-word (quotient (- end-loc start-loc) 2))) |
| 7357 |
(let loop ((loc start-loc) (l start)) |
| 7358 |
(if (not (eq? l end)) |
| 7359 |
(let ((part (car l)) (rest (cdr l))) |
| 7360 |
(if (pair? part) |
| 7361 |
(case (car part) |
| 7362 |
((label) (loop loc rest)) |
| 7363 |
((align) |
| 7364 |
(let ((n (padding loc (cadr part) (cddr part)))) |
| 7365 |
(let pad ((i 0)) |
| 7366 |
(if (< i n) |
| 7367 |
(begin (ofile-word 0) (pad (+ i 2))) |
| 7368 |
(loop (+ loc n) rest))))) |
| 7369 |
((brab) |
| 7370 |
(let ((dist (- (cadr (cddr part)) (+ loc 2)))) |
| 7371 |
(ofile-word (+ (cadr part) (modulo dist 256))) |
| 7372 |
(loop (+ loc 2) rest))) |
| 7373 |
((braw) |
| 7374 |
(let ((dist (- (cadr (cddr part)) (+ loc 2)))) |
| 7375 |
(ofile-word (cadr part)) |
| 7376 |
(ofile-word (modulo dist 65536)) |
| 7377 |
(loop (+ loc 4) rest))) |
| 7378 |
((wrel) |
| 7379 |
(let ((dist (+ (- (cadr (cadr part)) loc) (cddr part)))) |
| 7380 |
(ofile-word (modulo dist 65536)) |
| 7381 |
(loop (+ loc 2) rest))) |
| 7382 |
((lrel) |
| 7383 |
(let ((dist (+ (- (cadr (cadr part)) loc) |
| 7384 |
(caddr part)))) |
| 7385 |
(ofile-long (+ (* dist 65536) (cdddr part))) |
| 7386 |
(loop (+ loc 4) rest))) |
| 7387 |
((comment) |
| 7388 |
(let ((x (cdr part))) |
| 7389 |
(if (pair? x) (ofile-comment x) (ofile-gvm-instr x)) |
| 7390 |
(loop loc rest)))) |
| 7391 |
(begin (ofile-word part) (loop (+ loc 2) rest))))))) |
| 7392 |
(define (write-code) |
| 7393 |
(let ((proc-len |
| 7394 |
(+ (cadr (cdr (assq const-lbl lbl-list))) |
| 7395 |
(* (length const-list) 4)))) |
| 7396 |
(if (>= proc-len 32768) |
| 7397 |
(compiler-limitation-error |
| 7398 |
"procedure is too big (32K bytes limit per procedure)")) |
| 7399 |
(ofile-word (+ 32768 proc-len))) |
| 7400 |
(let loop1 ((start code-list) (start-loc header-offset)) |
| 7401 |
(let loop2 ((end start) (loc start-loc)) |
| 7402 |
(if (null? end) |
| 7403 |
(write-block start-loc loc start end) |
| 7404 |
(let ((part (car end)) (rest (cdr end))) |
| 7405 |
(if (pair? part) |
| 7406 |
(case (car part) |
| 7407 |
((label comment) (loop2 rest loc)) |
| 7408 |
((align) |
| 7409 |
(loop2 rest |
| 7410 |
(+ loc (padding loc (cadr part) (cddr part))))) |
| 7411 |
((brab wrel) (loop2 rest (+ loc 2))) |
| 7412 |
((braw) (loop2 rest (+ loc 4))) |
| 7413 |
((lrel) (loop2 rest (+ loc 4))) |
| 7414 |
(else |
| 7415 |
(write-block start-loc loc start end) |
| 7416 |
(case (car part) |
| 7417 |
((ref-glob) |
| 7418 |
(ofile-wsym global-var-ref-tag (cdr part)) |
| 7419 |
(loop1 rest (+ loc ref-glob-len))) |
| 7420 |
((set-glob) |
| 7421 |
(ofile-wsym global-var-set-tag (cdr part)) |
| 7422 |
(loop1 rest (+ loc set-glob-len))) |
| 7423 |
((ref-glob-jump) |
| 7424 |
(ofile-wsym global-var-ref-jump-tag (cdr part)) |
| 7425 |
(loop1 rest (+ loc ref-glob-jump-len))) |
| 7426 |
((proc-ref) |
| 7427 |
(ofile-word (+ local-proc-ref-tag (cadr part))) |
| 7428 |
(ofile-word (cddr part)) |
| 7429 |
(loop1 rest (+ loc proc-ref-len))) |
| 7430 |
((prim-ref) |
| 7431 |
(ofile-wsym prim-proc-ref-tag (cadr part)) |
| 7432 |
(ofile-word (cddr part)) |
| 7433 |
(loop1 rest (+ loc prim-ref-len))) |
| 7434 |
((m68020-proc) |
| 7435 |
(ofile-word m68020-proc-code-tag) |
| 7436 |
(loop1 rest loc)) |
| 7437 |
((m68881-proc) |
| 7438 |
(ofile-word m68881-proc-code-tag) |
| 7439 |
(loop1 rest loc)) |
| 7440 |
((stat) |
| 7441 |
(ofile-word stat-tag) |
| 7442 |
(ofile-stat (cdr part)) |
| 7443 |
(loop1 rest (+ loc stat-len)))))) |
| 7444 |
(loop2 rest (+ loc 2))))))) |
| 7445 |
(ofile-word end-of-code-tag) |
| 7446 |
(for-each ofile-ref const-list) |
| 7447 |
(ofile-long (obj-encoding (+ (length const-list) 1)))) |
| 7448 |
(replace-lbl-refs-by-pointer-to-label) |
| 7449 |
(branch-tensioning-pass) |
| 7450 |
(write-code)))) |
| 7451 |
(define const-lbl 0) |
| 7452 |
(define (identical-opnd68? opnd1 opnd2) (eqv? opnd1 opnd2)) |
| 7453 |
(define (reg68? x) (or (dreg? x) (areg? x))) |
| 7454 |
(define (make-dreg num) num) |
| 7455 |
(define (dreg? x) (and (integer? x) (>= x 0) (< x 8))) |
| 7456 |
(define (dreg-num x) x) |
| 7457 |
(define (make-areg num) (+ num 8)) |
| 7458 |
(define (areg? x) (and (integer? x) (>= x 8) (< x 16))) |
| 7459 |
(define (areg-num x) (- x 8)) |
| 7460 |
(define (make-ind areg) (+ areg 8)) |
| 7461 |
(define (ind? x) (and (integer? x) (>= x 16) (< x 24))) |
| 7462 |
(define (ind-areg x) (- x 8)) |
| 7463 |
(define (make-pinc areg) (+ areg 16)) |
| 7464 |
(define (pinc? x) (and (integer? x) (>= x 24) (< x 32))) |
| 7465 |
(define (pinc-areg x) (- x 16)) |
| 7466 |
(define (make-pdec areg) (+ areg 24)) |
| 7467 |
(define (pdec? x) (and (integer? x) (>= x 32) (< x 40))) |
| 7468 |
(define (pdec-areg x) (- x 24)) |
| 7469 |
(define (make-disp areg offset) (+ (+ areg 32) (* (modulo offset 65536) 8))) |
| 7470 |
(define (disp? x) (and (integer? x) (>= x 40) (< x 524328))) |
| 7471 |
(define (disp-areg x) (+ (remainder x 8) 8)) |
| 7472 |
(define (disp-offset x) |
| 7473 |
(- (modulo (+ (quotient (- x 40) 8) 32768) 65536) 32768)) |
| 7474 |
(define (make-disp* areg offset) |
| 7475 |
(if (= offset 0) (make-ind areg) (make-disp areg offset))) |
| 7476 |
(define (disp*? x) (or (ind? x) (disp? x))) |
| 7477 |
(define (disp*-areg x) (if (ind? x) (ind-areg x) (disp-areg x))) |
| 7478 |
(define (disp*-offset x) (if (ind? x) 0 (disp-offset x))) |
| 7479 |
(define (make-inx areg ireg offset) |
| 7480 |
(+ (+ areg 524320) (* ireg 8) (* (modulo offset 256) 128))) |
| 7481 |
(define (inx? x) (and (integer? x) (>= x 524328) (< x 557096))) |
| 7482 |
(define (inx-areg x) (+ (remainder (- x 524328) 8) 8)) |
| 7483 |
(define (inx-ireg x) (quotient (remainder (- x 524328) 128) 8)) |
| 7484 |
(define (inx-offset x) |
| 7485 |
(- (modulo (+ (quotient (- x 524328) 128) 128) 256) 128)) |
| 7486 |
(define (make-freg num) (+ 557096 num)) |
| 7487 |
(define (freg? x) (and (integer? x) (>= x 557096) (< x 557104))) |
| 7488 |
(define (freg-num x) (- x 557096)) |
| 7489 |
(define (make-pcr lbl offset) |
| 7490 |
(+ 557104 (+ (modulo offset 65536) (* lbl 65536)))) |
| 7491 |
(define (pcr? x) (and (integer? x) (>= x 557104))) |
| 7492 |
(define (pcr-lbl x) (quotient (- x 557104) 65536)) |
| 7493 |
(define (pcr-offset x) (- (modulo (- x 524336) 65536) 32768)) |
| 7494 |
(define (make-imm val) (if (< val 0) (* val 2) (- -1 (* val 2)))) |
| 7495 |
(define (imm? x) (and (integer? x) (< x 0))) |
| 7496 |
(define (imm-val x) (if (even? x) (quotient x 2) (- (quotient x 2)))) |
| 7497 |
(define (make-glob name) name) |
| 7498 |
(define (glob? x) (symbol? x)) |
| 7499 |
(define (glob-name x) x) |
| 7500 |
(define (make-frame-base-rel slot) (make-disp sp-reg slot)) |
| 7501 |
(define (frame-base-rel? x) |
| 7502 |
(and (disp? x) (identical-opnd68? sp-reg (disp-areg x)))) |
| 7503 |
(define (frame-base-rel-slot x) (disp-offset x)) |
| 7504 |
(define (make-reg-list regs) regs) |
| 7505 |
(define (reg-list? x) (or (pair? x) (null? x))) |
| 7506 |
(define (reg-list-regs x) x) |
| 7507 |
(define first-dtemp 0) |
| 7508 |
(define gvm-reg1 1) |
| 7509 |
(define poll-timer-reg (make-dreg 5)) |
| 7510 |
(define null-reg (make-dreg 6)) |
| 7511 |
(define placeholder-reg (make-dreg 6)) |
| 7512 |
(define false-reg (make-dreg 7)) |
| 7513 |
(define pair-reg (make-dreg 7)) |
| 7514 |
(define gvm-reg0 0) |
| 7515 |
(define first-atemp 1) |
| 7516 |
(define heap-reg (make-areg 3)) |
| 7517 |
(define ltq-tail-reg (make-areg 4)) |
| 7518 |
(define pstate-reg (make-areg 5)) |
| 7519 |
(define table-reg (make-areg 6)) |
| 7520 |
(define sp-reg (make-areg 7)) |
| 7521 |
(define pdec-sp (make-pdec sp-reg)) |
| 7522 |
(define pinc-sp (make-pinc sp-reg)) |
| 7523 |
(define dtemp1 (make-dreg first-dtemp)) |
| 7524 |
(define atemp1 (make-areg first-atemp)) |
| 7525 |
(define atemp2 (make-areg (+ first-atemp 1))) |
| 7526 |
(define ftemp1 (make-freg 0)) |
| 7527 |
(define arg-count-reg dtemp1) |
| 7528 |
(define (trap-offset n) (+ 32768 (* (- n 32) 8))) |
| 7529 |
(define (emit-move.l opnd1 opnd2) |
| 7530 |
(let ((src (opnd->mode/reg opnd1)) (dst (opnd->reg/mode opnd2))) |
| 7531 |
(asm-word (+ 8192 (+ dst src))) |
| 7532 |
(opnd-ext-rd-long opnd1) |
| 7533 |
(opnd-ext-wr-long opnd2) |
| 7534 |
(if ofile-asm? |
| 7535 |
(emit-asm "movl" ofile-tab (opnd-str opnd1) "," (opnd-str opnd2))))) |
| 7536 |
(define (emit-move.w opnd1 opnd2) |
| 7537 |
(let ((src (opnd->mode/reg opnd1)) (dst (opnd->reg/mode opnd2))) |
| 7538 |
(asm-word (+ 12288 (+ dst src))) |
| 7539 |
(opnd-ext-rd-word opnd1) |
| 7540 |
(opnd-ext-wr-word opnd2) |
| 7541 |
(if ofile-asm? |
| 7542 |
(emit-asm "movw" ofile-tab (opnd-str opnd1) "," (opnd-str opnd2))))) |
| 7543 |
(define (emit-move.b opnd1 opnd2) |
| 7544 |
(let ((src (opnd->mode/reg opnd1)) (dst (opnd->reg/mode opnd2))) |
| 7545 |
(asm-word (+ 4096 (+ dst src))) |
| 7546 |
(opnd-ext-rd-word opnd1) |
| 7547 |
(opnd-ext-wr-word opnd2) |
| 7548 |
(if ofile-asm? |
| 7549 |
(emit-asm "movb" ofile-tab (opnd-str opnd1) "," (opnd-str opnd2))))) |
| 7550 |
(define (emit-moveq n opnd) |
| 7551 |
(asm-word (+ 28672 (+ (* (dreg-num opnd) 512) (modulo n 256)))) |
| 7552 |
(if ofile-asm? (emit-asm "moveq" ofile-tab "#" n "," (opnd-str opnd)))) |
| 7553 |
(define (emit-movem.l opnd1 opnd2) |
| 7554 |
(define (reg-mask reg-list flip-bits?) |
| 7555 |
(let loop ((i 15) (bit 32768) (mask 0)) |
| 7556 |
(if (>= i 0) |
| 7557 |
(loop (- i 1) |
| 7558 |
(quotient bit 2) |
| 7559 |
(if (memq i reg-list) |
| 7560 |
(+ mask (if flip-bits? (quotient 32768 bit) bit)) |
| 7561 |
mask)) |
| 7562 |
mask))) |
| 7563 |
(define (movem op reg-list opnd) |
| 7564 |
(asm-word (+ op (opnd->mode/reg opnd))) |
| 7565 |
(asm-word (reg-mask reg-list (pdec? opnd)))) |
| 7566 |
(if (reg-list? opnd1) |
| 7567 |
(begin (movem 18624 opnd1 opnd2) (opnd-ext-wr-long opnd2)) |
| 7568 |
(begin (movem 19648 opnd2 opnd1) (opnd-ext-rd-long opnd1))) |
| 7569 |
(if ofile-asm? |
| 7570 |
(emit-asm "moveml" ofile-tab (opnd-str opnd1) "," (opnd-str opnd2)))) |
| 7571 |
(define (emit-exg opnd1 opnd2) |
| 7572 |
(define (exg r1 r2) |
| 7573 |
(let ((mode (if (dreg? r2) 49472 (if (dreg? r1) 49544 49480))) |
| 7574 |
(num1 (if (dreg? r1) (dreg-num r1) (areg-num r1))) |
| 7575 |
(num2 (if (dreg? r2) (dreg-num r2) (areg-num r2)))) |
| 7576 |
(asm-word (+ mode (+ (* num1 512) num2))))) |
| 7577 |
(if (dreg? opnd2) (exg opnd2 opnd1) (exg opnd1 opnd2)) |
| 7578 |
(if ofile-asm? |
| 7579 |
(emit-asm "exg" ofile-tab (opnd-str opnd1) "," (opnd-str opnd2)))) |
| 7580 |
(define (emit-eor.l opnd1 opnd2) |
| 7581 |
(cond ((imm? opnd1) |
| 7582 |
(asm-word (+ 2688 (opnd->mode/reg opnd2))) |
| 7583 |
(opnd-ext-rd-long opnd1) |
| 7584 |
(opnd-ext-wr-long opnd2)) |
| 7585 |
(else |
| 7586 |
(asm-word |
| 7587 |
(+ 45440 (+ (* (dreg-num opnd1) 512) (opnd->mode/reg opnd2)))) |
| 7588 |
(opnd-ext-wr-long opnd2))) |
| 7589 |
(if ofile-asm? |
| 7590 |
(emit-asm "eorl" ofile-tab (opnd-str opnd1) "," (opnd-str opnd2)))) |
| 7591 |
(define (emit-and.l opnd1 opnd2) |
| 7592 |
(cond ((imm? opnd1) |
| 7593 |
(asm-word (+ 640 (opnd->mode/reg opnd2))) |
| 7594 |
(opnd-ext-rd-long opnd1) |
| 7595 |
(opnd-ext-wr-long opnd2)) |
| 7596 |
(else |
| 7597 |
(let ((mode (if (dreg? opnd2) 49280 49536)) |
| 7598 |
(reg (if (dreg? opnd2) (dreg-num opnd2) (dreg-num opnd1))) |
| 7599 |
(other (if (dreg? opnd2) opnd1 opnd2))) |
| 7600 |
(asm-word (+ mode (+ (* reg 512) (opnd->mode/reg other)))) |
| 7601 |
(if (dreg? opnd2) |
| 7602 |
(opnd-ext-rd-long other) |
| 7603 |
(opnd-ext-wr-long other))))) |
| 7604 |
(if ofile-asm? |
| 7605 |
(emit-asm "andl" ofile-tab (opnd-str opnd1) "," (opnd-str opnd2)))) |
| 7606 |
(define (emit-and.w opnd1 opnd2) |
| 7607 |
(cond ((imm? opnd1) |
| 7608 |
(asm-word (+ 576 (opnd->mode/reg opnd2))) |
| 7609 |
(opnd-ext-rd-word opnd1) |
| 7610 |
(opnd-ext-wr-word opnd2)) |
| 7611 |
(else |
| 7612 |
(let ((mode (if (dreg? opnd2) 49216 49472)) |
| 7613 |
(reg (if (dreg? opnd2) (dreg-num opnd2) (dreg-num opnd1))) |
| 7614 |
(other (if (dreg? opnd2) opnd1 opnd2))) |
| 7615 |
(asm-word (+ mode (+ (* reg 512) (opnd->mode/reg other)))) |
| 7616 |
(if (dreg? opnd2) |
| 7617 |
(opnd-ext-rd-word other) |
| 7618 |
(opnd-ext-wr-word other))))) |
| 7619 |
(if ofile-asm? |
| 7620 |
(emit-asm "andw" ofile-tab (opnd-str opnd1) "," (opnd-str opnd2)))) |
| 7621 |
(define (emit-or.l opnd1 opnd2) |
| 7622 |
(cond ((imm? opnd1) |
| 7623 |
(asm-word (+ 128 (opnd->mode/reg opnd2))) |
| 7624 |
(opnd-ext-rd-long opnd1) |
| 7625 |
(opnd-ext-wr-long opnd2)) |
| 7626 |
(else |
| 7627 |
(let ((mode (if (dreg? opnd2) 32896 33152)) |
| 7628 |
(reg (if (dreg? opnd2) (dreg-num opnd2) (dreg-num opnd1))) |
| 7629 |
(other (if (dreg? opnd2) opnd1 opnd2))) |
| 7630 |
(asm-word (+ mode (+ (* reg 512) (opnd->mode/reg other)))) |
| 7631 |
(if (dreg? opnd2) |
| 7632 |
(opnd-ext-rd-long other) |
| 7633 |
(opnd-ext-wr-long other))))) |
| 7634 |
(if ofile-asm? |
| 7635 |
(emit-asm "orl" ofile-tab (opnd-str opnd1) "," (opnd-str opnd2)))) |
| 7636 |
(define (emit-addq.l n opnd) |
| 7637 |
(let ((m (if (= n 8) 0 n))) |
| 7638 |
(asm-word (+ 20608 (* m 512) (opnd->mode/reg opnd))) |
| 7639 |
(opnd-ext-wr-long opnd) |
| 7640 |
(if ofile-asm? (emit-asm "addql" ofile-tab "#" n "," (opnd-str opnd))))) |
| 7641 |
(define (emit-addq.w n opnd) |
| 7642 |
(let ((m (if (= n 8) 0 n))) |
| 7643 |
(asm-word (+ 20544 (* m 512) (opnd->mode/reg opnd))) |
| 7644 |
(opnd-ext-wr-word opnd) |
| 7645 |
(if ofile-asm? (emit-asm "addqw" ofile-tab "#" n "," (opnd-str opnd))))) |
| 7646 |
(define (emit-add.l opnd1 opnd2) |
| 7647 |
(cond ((areg? opnd2) |
| 7648 |
(asm-word |
| 7649 |
(+ 53696 (+ (* (areg-num opnd2) 512) (opnd->mode/reg opnd1)))) |
| 7650 |
(opnd-ext-rd-long opnd1)) |
| 7651 |
((imm? opnd1) |
| 7652 |
(asm-word (+ 1664 (opnd->mode/reg opnd2))) |
| 7653 |
(opnd-ext-rd-long opnd1) |
| 7654 |
(opnd-ext-wr-long opnd2)) |
| 7655 |
(else |
| 7656 |
(let ((mode (if (dreg? opnd2) 53376 53632)) |
| 7657 |
(reg (if (dreg? opnd2) (dreg-num opnd2) (dreg-num opnd1))) |
| 7658 |
(other (if (dreg? opnd2) opnd1 opnd2))) |
| 7659 |
(asm-word (+ mode (+ (* reg 512) (opnd->mode/reg other)))) |
| 7660 |
(if (dreg? opnd2) |
| 7661 |
(opnd-ext-rd-long other) |
| 7662 |
(opnd-ext-wr-long other))))) |
| 7663 |
(if ofile-asm? |
| 7664 |
(emit-asm "addl" ofile-tab (opnd-str opnd1) "," (opnd-str opnd2)))) |
| 7665 |
(define (emit-add.w opnd1 opnd2) |
| 7666 |
(cond ((areg? opnd2) |
| 7667 |
(asm-word |
| 7668 |
(+ 53440 (+ (* (areg-num opnd2) 512) (opnd->mode/reg opnd1)))) |
| 7669 |
(opnd-ext-rd-word opnd1)) |
| 7670 |
((imm? opnd1) |
| 7671 |
(asm-word (+ 1600 (opnd->mode/reg opnd2))) |
| 7672 |
(opnd-ext-rd-word opnd1) |
| 7673 |
(opnd-ext-wr-word opnd2)) |
| 7674 |
(else |
| 7675 |
(let ((mode (if (dreg? opnd2) 53312 53568)) |
| 7676 |
(reg (if (dreg? opnd2) (dreg-num opnd2) (dreg-num opnd1))) |
| 7677 |
(other (if (dreg? opnd2) opnd1 opnd2))) |
| 7678 |
(asm-word (+ mode (+ (* reg 512) (opnd->mode/reg other)))) |
| 7679 |
(if (dreg? opnd2) |
| 7680 |
(opnd-ext-rd-word other) |
| 7681 |
(opnd-ext-wr-word other))))) |
| 7682 |
(if ofile-asm? |
| 7683 |
(emit-asm "addw" ofile-tab (opnd-str opnd1) "," (opnd-str opnd2)))) |
| 7684 |
(define (emit-addx.w opnd1 opnd2) |
| 7685 |
(if (dreg? opnd1) |
| 7686 |
(asm-word (+ 53568 (+ (* (dreg-num opnd2) 512) (dreg-num opnd1)))) |
| 7687 |
(asm-word |
| 7688 |
(+ 53576 |
| 7689 |
(+ (* (areg-num (pdec-areg opnd2)) 512) |
| 7690 |
(areg-num (pdec-areg opnd1)))))) |
| 7691 |
(if ofile-asm? |
| 7692 |
(emit-asm "addxw" ofile-tab (opnd-str opnd1) "," (opnd-str opnd2)))) |
| 7693 |
(define (emit-subq.l n opnd) |
| 7694 |
(let ((m (if (= n 8) 0 n))) |
| 7695 |
(asm-word (+ 20864 (* m 512) (opnd->mode/reg opnd))) |
| 7696 |
(opnd-ext-wr-long opnd) |
| 7697 |
(if ofile-asm? (emit-asm "subql" ofile-tab "#" n "," (opnd-str opnd))))) |
| 7698 |
(define (emit-subq.w n opnd) |
| 7699 |
(let ((m (if (= n 8) 0 n))) |
| 7700 |
(asm-word (+ 20800 (* m 512) (opnd->mode/reg opnd))) |
| 7701 |
(opnd-ext-wr-word opnd) |
| 7702 |
(if ofile-asm? (emit-asm "subqw" ofile-tab "#" n "," (opnd-str opnd))))) |
| 7703 |
(define (emit-sub.l opnd1 opnd2) |
| 7704 |
(cond ((areg? opnd2) |
| 7705 |
(asm-word |
| 7706 |
(+ 37312 (+ (* (areg-num opnd2) 512) (opnd->mode/reg opnd1)))) |
| 7707 |
(opnd-ext-rd-long opnd1)) |
| 7708 |
((imm? opnd1) |
| 7709 |
(asm-word (+ 1152 (opnd->mode/reg opnd2))) |
| 7710 |
(opnd-ext-rd-long opnd1) |
| 7711 |
(opnd-ext-wr-long opnd2)) |
| 7712 |
(else |
| 7713 |
(let ((mode (if (dreg? opnd2) 36992 37248)) |
| 7714 |
(reg (if (dreg? opnd2) (dreg-num opnd2) (dreg-num opnd1))) |
| 7715 |
(other (if (dreg? opnd2) opnd1 opnd2))) |
| 7716 |
(asm-word (+ mode (+ (* reg 512) (opnd->mode/reg other)))) |
| 7717 |
(if (dreg? opnd2) |
| 7718 |
(opnd-ext-rd-long other) |
| 7719 |
(opnd-ext-wr-long other))))) |
| 7720 |
(if ofile-asm? |
| 7721 |
(emit-asm "subl" ofile-tab (opnd-str opnd1) "," (opnd-str opnd2)))) |
| 7722 |
(define (emit-sub.w opnd1 opnd2) |
| 7723 |
(cond ((areg? opnd2) |
| 7724 |
(asm-word |
| 7725 |
(+ 37056 (+ (* (areg-num opnd2) 512) (opnd->mode/reg opnd1)))) |
| 7726 |
(opnd-ext-rd-word opnd1)) |
| 7727 |
((imm? opnd1) |
| 7728 |
(asm-word (+ 1088 (opnd->mode/reg opnd2))) |
| 7729 |
(opnd-ext-rd-word opnd1) |
| 7730 |
(opnd-ext-wr-word opnd2)) |
| 7731 |
(else |
| 7732 |
(let ((mode (if (dreg? opnd2) 36928 37184)) |
| 7733 |
(reg (if (dreg? opnd2) (dreg-num opnd2) (dreg-num opnd1))) |
| 7734 |
(other (if (dreg? opnd2) opnd1 opnd2))) |
| 7735 |
(asm-word (+ mode (+ (* reg 512) (opnd->mode/reg other)))) |
| 7736 |
(if (dreg? opnd2) |
| 7737 |
(opnd-ext-rd-word other) |
| 7738 |
(opnd-ext-wr-word other))))) |
| 7739 |
(if ofile-asm? |
| 7740 |
(emit-asm "subw" ofile-tab (opnd-str opnd1) "," (opnd-str opnd2)))) |
| 7741 |
(define (emit-asl.l opnd1 opnd2) |
| 7742 |
(if (dreg? opnd1) |
| 7743 |
(asm-word (+ 57760 (+ (* (dreg-num opnd1) 512) (dreg-num opnd2)))) |
| 7744 |
(let ((n (imm-val opnd1))) |
| 7745 |
(asm-word (+ 57728 (+ (* (if (= n 8) 0 n) 512) (dreg-num opnd2)))))) |
| 7746 |
(if ofile-asm? |
| 7747 |
(emit-asm "asll" ofile-tab (opnd-str opnd1) "," (opnd-str opnd2)))) |
| 7748 |
(define (emit-asl.w opnd1 opnd2) |
| 7749 |
(if (dreg? opnd1) |
| 7750 |
(asm-word (+ 57696 (+ (* (dreg-num opnd1) 512) (dreg-num opnd2)))) |
| 7751 |
(let ((n (imm-val opnd1))) |
| 7752 |
(asm-word (+ 57664 (+ (* (if (= n 8) 0 n) 512) (dreg-num opnd2)))))) |
| 7753 |
(if ofile-asm? |
| 7754 |
(emit-asm "aslw" ofile-tab (opnd-str opnd1) "," (opnd-str opnd2)))) |
| 7755 |
(define (emit-asr.l opnd1 opnd2) |
| 7756 |
(if (dreg? opnd1) |
| 7757 |
(asm-word (+ 57504 (+ (* (dreg-num opnd1) 512) (dreg-num opnd2)))) |
| 7758 |
(let ((n (imm-val opnd1))) |
| 7759 |
(asm-word (+ 57472 (+ (* (if (= n 8) 0 n) 512) (dreg-num opnd2)))))) |
| 7760 |
(if ofile-asm? |
| 7761 |
(emit-asm "asrl" ofile-tab (opnd-str opnd1) "," (opnd-str opnd2)))) |
| 7762 |
(define (emit-asr.w opnd1 opnd2) |
| 7763 |
(if (dreg? opnd1) |
| 7764 |
(asm-word (+ 57440 (+ (* (dreg-num opnd1) 512) (dreg-num opnd2)))) |
| 7765 |
(let ((n (imm-val opnd1))) |
| 7766 |
(asm-word (+ 57408 (+ (* (if (= n 8) 0 n) 512) (dreg-num opnd2)))))) |
| 7767 |
(if ofile-asm? |
| 7768 |
(emit-asm "asrw" ofile-tab (opnd-str opnd1) "," (opnd-str opnd2)))) |
| 7769 |
(define (emit-lsl.l opnd1 opnd2) |
| 7770 |
(if (dreg? opnd1) |
| 7771 |
(asm-word (+ 57768 (+ (* (dreg-num opnd1) 512) (dreg-num opnd2)))) |
| 7772 |
(let ((n (imm-val opnd1))) |
| 7773 |
(asm-word (+ 57736 (+ (* (if (= n 8) 0 n) 512) (dreg-num opnd2)))))) |
| 7774 |
(if ofile-asm? |
| 7775 |
(emit-asm "lsll" ofile-tab (opnd-str opnd1) "," (opnd-str opnd2)))) |
| 7776 |
(define (emit-lsr.l opnd1 opnd2) |
| 7777 |
(if (dreg? opnd1) |
| 7778 |
(asm-word (+ 57512 (+ (* (dreg-num opnd1) 512) (dreg-num opnd2)))) |
| 7779 |
(let ((n (imm-val opnd1))) |
| 7780 |
(asm-word (+ 57480 (+ (* (if (= n 8) 0 n) 512) (dreg-num opnd2)))))) |
| 7781 |
(if ofile-asm? |
| 7782 |
(emit-asm "lsrl" ofile-tab (opnd-str opnd1) "," (opnd-str opnd2)))) |
| 7783 |
(define (emit-lsr.w opnd1 opnd2) |
| 7784 |
(if (dreg? opnd1) |
| 7785 |
(asm-word (+ 57448 (+ (* (dreg-num opnd1) 512) (dreg-num opnd2)))) |
| 7786 |
(let ((n (imm-val opnd1))) |
| 7787 |
(asm-word (+ 57416 (+ (* (if (= n 8) 0 n) 512) (dreg-num opnd2)))))) |
| 7788 |
(if ofile-asm? |
| 7789 |
(emit-asm "lsrw" ofile-tab (opnd-str opnd1) "," (opnd-str opnd2)))) |
| 7790 |
(define (emit-clr.l opnd) |
| 7791 |
(asm-word (+ 17024 (opnd->mode/reg opnd))) |
| 7792 |
(opnd-ext-wr-long opnd) |
| 7793 |
(if ofile-asm? (emit-asm "clrl" ofile-tab (opnd-str opnd)))) |
| 7794 |
(define (emit-neg.l opnd) |
| 7795 |
(asm-word (+ 17536 (opnd->mode/reg opnd))) |
| 7796 |
(opnd-ext-wr-long opnd) |
| 7797 |
(if ofile-asm? (emit-asm "negl" ofile-tab (opnd-str opnd)))) |
| 7798 |
(define (emit-not.l opnd) |
| 7799 |
(asm-word (+ 18048 (opnd->mode/reg opnd))) |
| 7800 |
(opnd-ext-wr-long opnd) |
| 7801 |
(if ofile-asm? (emit-asm "notl" ofile-tab (opnd-str opnd)))) |
| 7802 |
(define (emit-ext.l opnd) |
| 7803 |
(asm-word (+ 18624 (dreg-num opnd))) |
| 7804 |
(if ofile-asm? (emit-asm "extl" ofile-tab (opnd-str opnd)))) |
| 7805 |
(define (emit-ext.w opnd) |
| 7806 |
(asm-word (+ 18560 (dreg-num opnd))) |
| 7807 |
(if ofile-asm? (emit-asm "extw" ofile-tab (opnd-str opnd)))) |
| 7808 |
(define (emit-swap opnd) |
| 7809 |
(asm-word (+ 18496 (dreg-num opnd))) |
| 7810 |
(if ofile-asm? (emit-asm "swap" ofile-tab (opnd-str opnd)))) |
| 7811 |
(define (emit-cmp.l opnd1 opnd2) |
| 7812 |
(cond ((areg? opnd2) |
| 7813 |
(asm-word |
| 7814 |
(+ 45504 (+ (* (areg-num opnd2) 512) (opnd->mode/reg opnd1)))) |
| 7815 |
(opnd-ext-rd-long opnd1)) |
| 7816 |
((imm? opnd1) |
| 7817 |
(asm-word (+ 3200 (opnd->mode/reg opnd2))) |
| 7818 |
(opnd-ext-rd-long opnd1) |
| 7819 |
(opnd-ext-rd-long opnd2)) |
| 7820 |
(else |
| 7821 |
(asm-word |
| 7822 |
(+ 45184 (+ (* (dreg-num opnd2) 512) (opnd->mode/reg opnd1)))) |
| 7823 |
(opnd-ext-rd-long opnd1))) |
| 7824 |
(if ofile-asm? |
| 7825 |
(emit-asm "cmpl" ofile-tab (opnd-str opnd1) "," (opnd-str opnd2)))) |
| 7826 |
(define (emit-cmp.w opnd1 opnd2) |
| 7827 |
(cond ((areg? opnd2) |
| 7828 |
(asm-word |
| 7829 |
(+ 45248 (+ (* (areg-num opnd2) 512) (opnd->mode/reg opnd1)))) |
| 7830 |
(opnd-ext-rd-word opnd1)) |
| 7831 |
((imm? opnd1) |
| 7832 |
(asm-word (+ 3136 (opnd->mode/reg opnd2))) |
| 7833 |
(opnd-ext-rd-word opnd1) |
| 7834 |
(opnd-ext-rd-word opnd2)) |
| 7835 |
(else |
| 7836 |
(asm-word |
| 7837 |
(+ 45120 (+ (* (dreg-num opnd2) 512) (opnd->mode/reg opnd1)))) |
| 7838 |
(opnd-ext-rd-word opnd1))) |
| 7839 |
(if ofile-asm? |
| 7840 |
(emit-asm "cmpw" ofile-tab (opnd-str opnd1) "," (opnd-str opnd2)))) |
| 7841 |
(define (emit-cmp.b opnd1 opnd2) |
| 7842 |
(cond ((imm? opnd1) |
| 7843 |
(asm-word (+ 3072 (opnd->mode/reg opnd2))) |
| 7844 |
(opnd-ext-rd-word opnd1) |
| 7845 |
(opnd-ext-rd-word opnd2)) |
| 7846 |
(else |
| 7847 |
(asm-word |
| 7848 |
(+ 45056 (+ (* (dreg-num opnd2) 512) (opnd->mode/reg opnd1)))) |
| 7849 |
(opnd-ext-rd-word opnd1))) |
| 7850 |
(if ofile-asm? |
| 7851 |
(emit-asm "cmpb" ofile-tab (opnd-str opnd1) "," (opnd-str opnd2)))) |
| 7852 |
(define (emit-tst.l opnd) |
| 7853 |
(asm-word (+ 19072 (opnd->mode/reg opnd))) |
| 7854 |
(opnd-ext-rd-long opnd) |
| 7855 |
(if ofile-asm? (emit-asm "tstl" ofile-tab (opnd-str opnd)))) |
| 7856 |
(define (emit-tst.w opnd) |
| 7857 |
(asm-word (+ 19008 (opnd->mode/reg opnd))) |
| 7858 |
(opnd-ext-rd-word opnd) |
| 7859 |
(if ofile-asm? (emit-asm "tstw" ofile-tab (opnd-str opnd)))) |
| 7860 |
(define (emit-lea opnd areg) |
| 7861 |
(asm-word (+ 16832 (+ (* (areg-num areg) 512) (opnd->mode/reg opnd)))) |
| 7862 |
(opnd-ext-rd-long opnd) |
| 7863 |
(if ofile-asm? |
| 7864 |
(emit-asm "lea" ofile-tab (opnd-str opnd) "," (opnd-str areg)))) |
| 7865 |
(define (emit-unlk areg) |
| 7866 |
(asm-word (+ 20056 (areg-num areg))) |
| 7867 |
(if ofile-asm? (emit-asm "unlk" ofile-tab (opnd-str areg)))) |
| 7868 |
(define (emit-move-proc num opnd) |
| 7869 |
(let ((dst (opnd->reg/mode opnd))) |
| 7870 |
(asm-word (+ 8192 (+ dst 60))) |
| 7871 |
(asm-proc-ref num 0) |
| 7872 |
(opnd-ext-wr-long opnd) |
| 7873 |
(if ofile-asm? (emit-asm "MOVE_PROC(" num "," (opnd-str opnd) ")")))) |
| 7874 |
(define (emit-move-prim val opnd) |
| 7875 |
(let ((dst (opnd->reg/mode opnd))) |
| 7876 |
(asm-word (+ 8192 (+ dst 60))) |
| 7877 |
(asm-prim-ref val 0) |
| 7878 |
(opnd-ext-wr-long opnd) |
| 7879 |
(if ofile-asm? |
| 7880 |
(emit-asm "MOVE_PRIM(" (proc-obj-name val) "," (opnd-str opnd) ")")))) |
| 7881 |
(define (emit-pea opnd) |
| 7882 |
(asm-word (+ 18496 (opnd->mode/reg opnd))) |
| 7883 |
(opnd-ext-rd-long opnd) |
| 7884 |
(if ofile-asm? (emit-asm "pea" ofile-tab (opnd-str opnd)))) |
| 7885 |
(define (emit-pea* n) |
| 7886 |
(asm-word 18552) |
| 7887 |
(asm-word n) |
| 7888 |
(if ofile-asm? (emit-asm "pea" ofile-tab n))) |
| 7889 |
(define (emit-btst opnd1 opnd2) |
| 7890 |
(asm-word (+ 256 (+ (* (dreg-num opnd1) 512) (opnd->mode/reg opnd2)))) |
| 7891 |
(opnd-ext-rd-word opnd2) |
| 7892 |
(if ofile-asm? |
| 7893 |
(emit-asm "btst" ofile-tab (opnd-str opnd1) "," (opnd-str opnd2)))) |
| 7894 |
(define (emit-bra lbl) |
| 7895 |
(asm-brel 24576 lbl) |
| 7896 |
(if ofile-asm? (emit-asm "bra" ofile-tab "L" lbl))) |
| 7897 |
(define (emit-bcc lbl) |
| 7898 |
(asm-brel 25600 lbl) |
| 7899 |
(if ofile-asm? (emit-asm "bcc" ofile-tab "L" lbl))) |
| 7900 |
(define (emit-bcs lbl) |
| 7901 |
(asm-brel 25856 lbl) |
| 7902 |
(if ofile-asm? (emit-asm "bcs" ofile-tab "L" lbl))) |
| 7903 |
(define (emit-bhi lbl) |
| 7904 |
(asm-brel 25088 lbl) |
| 7905 |
(if ofile-asm? (emit-asm "bhi" ofile-tab "L" lbl))) |
| 7906 |
(define (emit-bls lbl) |
| 7907 |
(asm-brel 25344 lbl) |
| 7908 |
(if ofile-asm? (emit-asm "bls" ofile-tab "L" lbl))) |
| 7909 |
(define (emit-bmi lbl) |
| 7910 |
(asm-brel 27392 lbl) |
| 7911 |
(if ofile-asm? (emit-asm "bmi" ofile-tab "L" lbl))) |
| 7912 |
(define (emit-bpl lbl) |
| 7913 |
(asm-brel 27136 lbl) |
| 7914 |
(if ofile-asm? (emit-asm "bpl" ofile-tab "L" lbl))) |
| 7915 |
(define (emit-beq lbl) |
| 7916 |
(asm-brel 26368 lbl) |
| 7917 |
(if ofile-asm? (emit-asm "beq" ofile-tab "L" lbl))) |
| 7918 |
(define (emit-bne lbl) |
| 7919 |
(asm-brel 26112 lbl) |
| 7920 |
(if ofile-asm? (emit-asm "bne" ofile-tab "L" lbl))) |
| 7921 |
(define (emit-blt lbl) |
| 7922 |
(asm-brel 27904 lbl) |
| 7923 |
(if ofile-asm? (emit-asm "blt" ofile-tab "L" lbl))) |
| 7924 |
(define (emit-bgt lbl) |
| 7925 |
(asm-brel 28160 lbl) |
| 7926 |
(if ofile-asm? (emit-asm "bgt" ofile-tab "L" lbl))) |
| 7927 |
(define (emit-ble lbl) |
| 7928 |
(asm-brel 28416 lbl) |
| 7929 |
(if ofile-asm? (emit-asm "ble" ofile-tab "L" lbl))) |
| 7930 |
(define (emit-bge lbl) |
| 7931 |
(asm-brel 27648 lbl) |
| 7932 |
(if ofile-asm? (emit-asm "bge" ofile-tab "L" lbl))) |
| 7933 |
(define (emit-dbra dreg lbl) |
| 7934 |
(asm-word (+ 20936 dreg)) |
| 7935 |
(asm-wrel lbl 0) |
| 7936 |
(if ofile-asm? (emit-asm "dbra" ofile-tab (opnd-str dreg) ",L" lbl))) |
| 7937 |
(define (emit-trap num) |
| 7938 |
(asm-word (+ 20032 num)) |
| 7939 |
(if ofile-asm? (emit-asm "trap" ofile-tab "#" num))) |
| 7940 |
(define (emit-trap1 num args) |
| 7941 |
(asm-word (+ 20136 (areg-num table-reg))) |
| 7942 |
(asm-word (trap-offset num)) |
| 7943 |
(let loop ((args args)) |
| 7944 |
(if (not (null? args)) (begin (asm-word (car args)) (loop (cdr args))))) |
| 7945 |
(if ofile-asm? |
| 7946 |
(let () |
| 7947 |
(define (words l) |
| 7948 |
(if (null? l) (list ")") (cons "," (cons (car l) (words (cdr l)))))) |
| 7949 |
(apply emit-asm (cons "TRAP1(" (cons num (words args))))))) |
| 7950 |
(define (emit-trap2 num args) |
| 7951 |
(asm-word (+ 20136 (areg-num table-reg))) |
| 7952 |
(asm-word (trap-offset num)) |
| 7953 |
(asm-align 8 (modulo (- 4 (* (length args) 2)) 8)) |
| 7954 |
(let loop ((args args)) |
| 7955 |
(if (not (null? args)) (begin (asm-word (car args)) (loop (cdr args))))) |
| 7956 |
(if ofile-asm? |
| 7957 |
(let () |
| 7958 |
(define (words l) |
| 7959 |
(if (null? l) (list ")") (cons "," (cons (car l) (words (cdr l)))))) |
| 7960 |
(apply emit-asm (cons "TRAP2(" (cons num (words args))))))) |
| 7961 |
(define (emit-trap3 num) |
| 7962 |
(asm-word (+ 20200 (areg-num table-reg))) |
| 7963 |
(asm-word (trap-offset num)) |
| 7964 |
(if ofile-asm? (emit-asm "TRAP3(" num ")"))) |
| 7965 |
(define (emit-rts) (asm-word 20085) (if ofile-asm? (emit-asm "rts"))) |
| 7966 |
(define (emit-nop) (asm-word 20081) (if ofile-asm? (emit-asm "nop"))) |
| 7967 |
(define (emit-jmp opnd) |
| 7968 |
(asm-word (+ 20160 (opnd->mode/reg opnd))) |
| 7969 |
(opnd-ext-rd-long opnd) |
| 7970 |
(if ofile-asm? (emit-asm "jmp" ofile-tab (opnd-str opnd)))) |
| 7971 |
(define (emit-jmp-glob glob) |
| 7972 |
(asm-word 8814) |
| 7973 |
(asm-ref-glob-jump glob) |
| 7974 |
(asm-word 20177) |
| 7975 |
(if ofile-asm? (emit-asm "JMP_GLOB(" (glob-name glob) ")"))) |
| 7976 |
(define (emit-jmp-proc num offset) |
| 7977 |
(asm-word 20217) |
| 7978 |
(asm-proc-ref num offset) |
| 7979 |
(if ofile-asm? (emit-asm "JMP_PROC(" num "," offset ")"))) |
| 7980 |
(define (emit-jmp-prim val offset) |
| 7981 |
(asm-word 20217) |
| 7982 |
(asm-prim-ref val offset) |
| 7983 |
(if ofile-asm? (emit-asm "JMP_PRIM(" (proc-obj-name val) "," offset ")"))) |
| 7984 |
(define (emit-jsr opnd) |
| 7985 |
(asm-word (+ 20096 (opnd->mode/reg opnd))) |
| 7986 |
(opnd-ext-rd-long opnd) |
| 7987 |
(if ofile-asm? (emit-asm "jsr" ofile-tab (opnd-str opnd)))) |
| 7988 |
(define (emit-word n) |
| 7989 |
(asm-word n) |
| 7990 |
(if ofile-asm? (emit-asm ".word" ofile-tab n))) |
| 7991 |
(define (emit-label lbl) |
| 7992 |
(asm-label lbl #f) |
| 7993 |
(if ofile-asm? (emit-asm* "L" lbl ":"))) |
| 7994 |
(define (emit-label-subproc lbl parent-lbl label-descr) |
| 7995 |
(asm-align 8 0) |
| 7996 |
(asm-wrel parent-lbl (- 32768 type-procedure)) |
| 7997 |
(asm-label lbl label-descr) |
| 7998 |
(if ofile-asm? |
| 7999 |
(begin (emit-asm "SUBPROC(L" parent-lbl ")") (emit-asm* "L" lbl ":")))) |
| 8000 |
(define (emit-label-return lbl parent-lbl fs link label-descr) |
| 8001 |
(asm-align 8 4) |
| 8002 |
(asm-word (* fs 4)) |
| 8003 |
(asm-word (* (- fs link) 4)) |
| 8004 |
(asm-wrel parent-lbl (- 32768 type-procedure)) |
| 8005 |
(asm-label lbl label-descr) |
| 8006 |
(if ofile-asm? |
| 8007 |
(begin |
| 8008 |
(emit-asm "RETURN(L" parent-lbl "," fs "," link ")") |
| 8009 |
(emit-asm* "L" lbl ":")))) |
| 8010 |
(define (emit-label-task-return lbl parent-lbl fs link label-descr) |
| 8011 |
(asm-align 8 4) |
| 8012 |
(asm-word (+ 32768 (* fs 4))) |
| 8013 |
(asm-word (* (- fs link) 4)) |
| 8014 |
(asm-wrel parent-lbl (- 32768 type-procedure)) |
| 8015 |
(asm-label lbl label-descr) |
| 8016 |
(if ofile-asm? |
| 8017 |
(begin |
| 8018 |
(emit-asm "TASK_RETURN(L" parent-lbl "," fs "," link ")") |
| 8019 |
(emit-asm* "L" lbl ":")))) |
| 8020 |
(define (emit-lbl-ptr lbl) |
| 8021 |
(asm-wrel lbl 0) |
| 8022 |
(if ofile-asm? (emit-asm "LBL_PTR(L" lbl ")"))) |
| 8023 |
(define (emit-set-glob glob) |
| 8024 |
(asm-set-glob glob) |
| 8025 |
(if ofile-asm? (emit-asm "SET_GLOB(" (glob-name glob) ")"))) |
| 8026 |
(define (emit-const obj) |
| 8027 |
(let ((n (pos-in-list obj (queue->list asm-const-queue)))) |
| 8028 |
(if n |
| 8029 |
(make-pcr const-lbl (* n 4)) |
| 8030 |
(let ((m (length (queue->list asm-const-queue)))) |
| 8031 |
(queue-put! asm-const-queue obj) |
| 8032 |
(make-pcr const-lbl (* m 4)))))) |
| 8033 |
(define (emit-stat stat) |
| 8034 |
(asm-word 21177) |
| 8035 |
(asm-stat stat) |
| 8036 |
(if ofile-asm? (emit-asm "STAT(" stat ")"))) |
| 8037 |
(define (emit-asm . l) (asm-comment (cons ofile-tab l))) |
| 8038 |
(define (emit-asm* . l) (asm-comment l)) |
| 8039 |
(define (emit-muls.l opnd1 opnd2) |
| 8040 |
(asm-m68020-proc) |
| 8041 |
(asm-word (+ 19456 (opnd->mode/reg opnd1))) |
| 8042 |
(asm-word (+ 2048 (* (dreg-num opnd2) 4096))) |
| 8043 |
(opnd-ext-rd-long opnd1) |
| 8044 |
(if ofile-asm? |
| 8045 |
(emit-asm "mulsl" ofile-tab (opnd-str opnd1) "," (opnd-str opnd2)))) |
| 8046 |
(define (emit-divsl.l opnd1 opnd2 opnd3) |
| 8047 |
(asm-m68020-proc) |
| 8048 |
(asm-word (+ 19520 (opnd->mode/reg opnd1))) |
| 8049 |
(asm-word (+ 2048 (* (dreg-num opnd3) 4096) (dreg-num opnd2))) |
| 8050 |
(opnd-ext-rd-long opnd1) |
| 8051 |
(if ofile-asm? |
| 8052 |
(emit-asm |
| 8053 |
"divsll" |
| 8054 |
ofile-tab |
| 8055 |
(opnd-str opnd1) |
| 8056 |
"," |
| 8057 |
(opnd-str opnd2) |
| 8058 |
":" |
| 8059 |
(opnd-str opnd3)))) |
| 8060 |
(define (emit-fint.dx opnd1 opnd2) (emit-fop.dx "int" 1 opnd1 opnd2)) |
| 8061 |
(define (emit-fsinh.dx opnd1 opnd2) (emit-fop.dx "sinh" 2 opnd1 opnd2)) |
| 8062 |
(define (emit-fintrz.dx opnd1 opnd2) (emit-fop.dx "intrz" 3 opnd1 opnd2)) |
| 8063 |
(define (emit-fsqrt.dx opnd1 opnd2) (emit-fop.dx "sqrt" 4 opnd1 opnd2)) |
| 8064 |
(define (emit-flognp1.dx opnd1 opnd2) (emit-fop.dx "lognp1" 6 opnd1 opnd2)) |
| 8065 |
(define (emit-fetoxm1.dx opnd1 opnd2) (emit-fop.dx "etoxm1" 8 opnd1 opnd2)) |
| 8066 |
(define (emit-ftanh.dx opnd1 opnd2) (emit-fop.dx "tanh" 9 opnd1 opnd2)) |
| 8067 |
(define (emit-fatan.dx opnd1 opnd2) (emit-fop.dx "atan" 10 opnd1 opnd2)) |
| 8068 |
(define (emit-fasin.dx opnd1 opnd2) (emit-fop.dx "asin" 12 opnd1 opnd2)) |
| 8069 |
(define (emit-fatanh.dx opnd1 opnd2) (emit-fop.dx "atanh" 13 opnd1 opnd2)) |
| 8070 |
(define (emit-fsin.dx opnd1 opnd2) (emit-fop.dx "sin" 14 opnd1 opnd2)) |
| 8071 |
(define (emit-ftan.dx opnd1 opnd2) (emit-fop.dx "tan" 15 opnd1 opnd2)) |
| 8072 |
(define (emit-fetox.dx opnd1 opnd2) (emit-fop.dx "etox" 16 opnd1 opnd2)) |
| 8073 |
(define (emit-ftwotox.dx opnd1 opnd2) (emit-fop.dx "twotox" 17 opnd1 opnd2)) |
| 8074 |
(define (emit-ftentox.dx opnd1 opnd2) (emit-fop.dx "tentox" 18 opnd1 opnd2)) |
| 8075 |
(define (emit-flogn.dx opnd1 opnd2) (emit-fop.dx "logn" 20 opnd1 opnd2)) |
| 8076 |
(define (emit-flog10.dx opnd1 opnd2) (emit-fop.dx "log10" 21 opnd1 opnd2)) |
| 8077 |
(define (emit-flog2.dx opnd1 opnd2) (emit-fop.dx "log2" 22 opnd1 opnd2)) |
| 8078 |
(define (emit-fabs.dx opnd1 opnd2) (emit-fop.dx "abs" 24 opnd1 opnd2)) |
| 8079 |
(define (emit-fcosh.dx opnd1 opnd2) (emit-fop.dx "cosh" 25 opnd1 opnd2)) |
| 8080 |
(define (emit-fneg.dx opnd1 opnd2) (emit-fop.dx "neg" 26 opnd1 opnd2)) |
| 8081 |
(define (emit-facos.dx opnd1 opnd2) (emit-fop.dx "acos" 28 opnd1 opnd2)) |
| 8082 |
(define (emit-fcos.dx opnd1 opnd2) (emit-fop.dx "cos" 29 opnd1 opnd2)) |
| 8083 |
(define (emit-fgetexp.dx opnd1 opnd2) (emit-fop.dx "getexp" 30 opnd1 opnd2)) |
| 8084 |
(define (emit-fgetman.dx opnd1 opnd2) (emit-fop.dx "getman" 31 opnd1 opnd2)) |
| 8085 |
(define (emit-fdiv.dx opnd1 opnd2) (emit-fop.dx "div" 32 opnd1 opnd2)) |
| 8086 |
(define (emit-fmod.dx opnd1 opnd2) (emit-fop.dx "mod" 33 opnd1 opnd2)) |
| 8087 |
(define (emit-fadd.dx opnd1 opnd2) (emit-fop.dx "add" 34 opnd1 opnd2)) |
| 8088 |
(define (emit-fmul.dx opnd1 opnd2) (emit-fop.dx "mul" 35 opnd1 opnd2)) |
| 8089 |
(define (emit-fsgldiv.dx opnd1 opnd2) (emit-fop.dx "sgldiv" 36 opnd1 opnd2)) |
| 8090 |
(define (emit-frem.dx opnd1 opnd2) (emit-fop.dx "rem" 37 opnd1 opnd2)) |
| 8091 |
(define (emit-fscale.dx opnd1 opnd2) (emit-fop.dx "scale" 38 opnd1 opnd2)) |
| 8092 |
(define (emit-fsglmul.dx opnd1 opnd2) (emit-fop.dx "sglmul" 39 opnd1 opnd2)) |
| 8093 |
(define (emit-fsub.dx opnd1 opnd2) (emit-fop.dx "sub" 40 opnd1 opnd2)) |
| 8094 |
(define (emit-fcmp.dx opnd1 opnd2) (emit-fop.dx "cmp" 56 opnd1 opnd2)) |
| 8095 |
(define (emit-fop.dx name code opnd1 opnd2) |
| 8096 |
(asm-m68881-proc) |
| 8097 |
(asm-word (+ 61952 (opnd->mode/reg opnd1))) |
| 8098 |
(asm-word |
| 8099 |
(+ (if (freg? opnd1) (* (freg-num opnd1) 1024) 21504) |
| 8100 |
(* (freg-num opnd2) 128) |
| 8101 |
code)) |
| 8102 |
(opnd-ext-rd-long opnd1) |
| 8103 |
(if ofile-asm? |
| 8104 |
(emit-asm |
| 8105 |
"f" |
| 8106 |
name |
| 8107 |
(if (freg? opnd1) "x" "d") |
| 8108 |
ofile-tab |
| 8109 |
(opnd-str opnd1) |
| 8110 |
"," |
| 8111 |
(opnd-str opnd2)))) |
| 8112 |
(define (emit-fmov.dx opnd1 opnd2) |
| 8113 |
(emit-fmov |
| 8114 |
(if (and (freg? opnd1) (freg? opnd2)) (* (freg-num opnd1) 1024) 21504) |
| 8115 |
opnd1 |
| 8116 |
opnd2) |
| 8117 |
(if ofile-asm? |
| 8118 |
(emit-asm |
| 8119 |
(if (and (freg? opnd1) (freg? opnd2)) "fmovex" "fmoved") |
| 8120 |
ofile-tab |
| 8121 |
(opnd-str opnd1) |
| 8122 |
"," |
| 8123 |
(opnd-str opnd2)))) |
| 8124 |
(define (emit-fmov.l opnd1 opnd2) |
| 8125 |
(emit-fmov 16384 opnd1 opnd2) |
| 8126 |
(if ofile-asm? |
| 8127 |
(emit-asm "fmovel" ofile-tab (opnd-str opnd1) "," (opnd-str opnd2)))) |
| 8128 |
(define (emit-fmov code opnd1 opnd2) |
| 8129 |
(define (fmov code opnd1 opnd2) |
| 8130 |
(asm-m68881-proc) |
| 8131 |
(asm-word (+ 61952 (opnd->mode/reg opnd1))) |
| 8132 |
(asm-word (+ (* (freg-num opnd2) 128) code)) |
| 8133 |
(opnd-ext-rd-long opnd1)) |
| 8134 |
(if (freg? opnd2) (fmov code opnd1 opnd2) (fmov (+ code 8192) opnd2 opnd1))) |
| 8135 |
(define (emit-fbeq lbl) |
| 8136 |
(asm-m68881-proc) |
| 8137 |
(asm-word 62081) |
| 8138 |
(asm-wrel lbl 0) |
| 8139 |
(if ofile-asm? (emit-asm "fbeq" ofile-tab "L" lbl))) |
| 8140 |
(define (emit-fbne lbl) |
| 8141 |
(asm-m68881-proc) |
| 8142 |
(asm-word 62094) |
| 8143 |
(asm-wrel lbl 0) |
| 8144 |
(if ofile-asm? (emit-asm "fbne" ofile-tab "L" lbl))) |
| 8145 |
(define (emit-fblt lbl) |
| 8146 |
(asm-m68881-proc) |
| 8147 |
(asm-word 62100) |
| 8148 |
(asm-wrel lbl 0) |
| 8149 |
(if ofile-asm? (emit-asm "fblt" ofile-tab "L" lbl))) |
| 8150 |
(define (emit-fbgt lbl) |
| 8151 |
(asm-m68881-proc) |
| 8152 |
(asm-word 62098) |
| 8153 |
(asm-wrel lbl 0) |
| 8154 |
(if ofile-asm? (emit-asm "fbgt" ofile-tab "L" lbl))) |
| 8155 |
(define (emit-fble lbl) |
| 8156 |
(asm-m68881-proc) |
| 8157 |
(asm-word 62101) |
| 8158 |
(asm-wrel lbl 0) |
| 8159 |
(if ofile-asm? (emit-asm "fble" ofile-tab "L" lbl))) |
| 8160 |
(define (emit-fbge lbl) |
| 8161 |
(asm-m68881-proc) |
| 8162 |
(asm-word 62099) |
| 8163 |
(asm-wrel lbl 0) |
| 8164 |
(if ofile-asm? (emit-asm "fbge" ofile-tab "L" lbl))) |
| 8165 |
(define (opnd->mode/reg opnd) |
| 8166 |
(cond ((disp? opnd) (+ 32 (disp-areg opnd))) |
| 8167 |
((inx? opnd) (+ 40 (inx-areg opnd))) |
| 8168 |
((pcr? opnd) 58) |
| 8169 |
((imm? opnd) 60) |
| 8170 |
((glob? opnd) (+ 32 table-reg)) |
| 8171 |
((freg? opnd) 0) |
| 8172 |
(else opnd))) |
| 8173 |
(define (opnd->reg/mode opnd) |
| 8174 |
(let ((x (opnd->mode/reg opnd))) |
| 8175 |
(* (+ (* 8 (remainder x 8)) (quotient x 8)) 64))) |
| 8176 |
(define (opnd-ext-rd-long opnd) (opnd-extension opnd #f #f)) |
| 8177 |
(define (opnd-ext-rd-word opnd) (opnd-extension opnd #f #t)) |
| 8178 |
(define (opnd-ext-wr-long opnd) (opnd-extension opnd #t #f)) |
| 8179 |
(define (opnd-ext-wr-word opnd) (opnd-extension opnd #t #t)) |
| 8180 |
(define (opnd-extension opnd write? word?) |
| 8181 |
(cond ((disp? opnd) (asm-word (disp-offset opnd))) |
| 8182 |
((inx? opnd) |
| 8183 |
(asm-word |
| 8184 |
(+ (+ (* (inx-ireg opnd) 4096) 2048) |
| 8185 |
(modulo (inx-offset opnd) 256)))) |
| 8186 |
((pcr? opnd) (asm-wrel (pcr-lbl opnd) (pcr-offset opnd))) |
| 8187 |
((imm? opnd) |
| 8188 |
(if word? (asm-word (imm-val opnd)) (asm-long (imm-val opnd)))) |
| 8189 |
((glob? opnd) (if write? (asm-set-glob opnd) (asm-ref-glob opnd))))) |
| 8190 |
(define (opnd-str opnd) |
| 8191 |
(cond ((dreg? opnd) |
| 8192 |
(vector-ref |
| 8193 |
'#("d0" "d1" "d2" "d3" "d4" "d5" "d6" "d7") |
| 8194 |
(dreg-num opnd))) |
| 8195 |
((areg? opnd) |
| 8196 |
(vector-ref |
| 8197 |
'#("a0" "a1" "a2" "a3" "a4" "a5" "a6" "sp") |
| 8198 |
(areg-num opnd))) |
| 8199 |
((ind? opnd) |
| 8200 |
(vector-ref |
| 8201 |
'#("a0@" "a1@" "a2@" "a3@" "a4@" "a5@" "a6@" "sp@") |
| 8202 |
(areg-num (ind-areg opnd)))) |
| 8203 |
((pinc? opnd) |
| 8204 |
(vector-ref |
| 8205 |
'#("a0@+" "a1@+" "a2@+" "a3@+" "a4@+" "a5@+" "a6@+" "sp@+") |
| 8206 |
(areg-num (pinc-areg opnd)))) |
| 8207 |
((pdec? opnd) |
| 8208 |
(vector-ref |
| 8209 |
'#("a0@-" "a1@-" "a2@-" "a3@-" "a4@-" "a5@-" "a6@-" "sp@-") |
| 8210 |
(areg-num (pdec-areg opnd)))) |
| 8211 |
((disp? opnd) |
| 8212 |
(string-append |
| 8213 |
(opnd-str (disp-areg opnd)) |
| 8214 |
"@(" |
| 8215 |
(number->string (disp-offset opnd)) |
| 8216 |
")")) |
| 8217 |
((inx? opnd) |
| 8218 |
(string-append |
| 8219 |
(opnd-str (inx-areg opnd)) |
| 8220 |
"@(" |
| 8221 |
(number->string (inx-offset opnd)) |
| 8222 |
"," |
| 8223 |
(opnd-str (inx-ireg opnd)) |
| 8224 |
":l)")) |
| 8225 |
((pcr? opnd) |
| 8226 |
(let ((lbl (pcr-lbl opnd)) (offs (pcr-offset opnd))) |
| 8227 |
(if (= offs 0) |
| 8228 |
(string-append "L" (number->string lbl)) |
| 8229 |
(string-append |
| 8230 |
"L" |
| 8231 |
(number->string lbl) |
| 8232 |
"+" |
| 8233 |
(number->string offs))))) |
| 8234 |
((imm? opnd) (string-append "#" (number->string (imm-val opnd)))) |
| 8235 |
((glob? opnd) |
| 8236 |
(string-append "GLOB(" (symbol->string (glob-name opnd)) ")")) |
| 8237 |
((freg? opnd) |
| 8238 |
(vector-ref |
| 8239 |
'#("fp0" "fp1" "fp2" "fp3" "fp4" "fp5" "fp6" "fp7") |
| 8240 |
(freg-num opnd))) |
| 8241 |
((reg-list? opnd) |
| 8242 |
(let loop ((l (reg-list-regs opnd)) (result "[") (sep "")) |
| 8243 |
(if (pair? l) |
| 8244 |
(loop (cdr l) (string-append result sep (opnd-str (car l))) "/") |
| 8245 |
(string-append result "]")))) |
| 8246 |
(else (compiler-internal-error "opnd-str, unknown 'opnd'" opnd)))) |
| 8247 |
(define (begin! info-port targ) |
| 8248 |
(set! return-reg (make-reg 0)) |
| 8249 |
(target-end!-set! targ end!) |
| 8250 |
(target-dump-set! targ dump) |
| 8251 |
(target-nb-regs-set! targ nb-gvm-regs) |
| 8252 |
(target-prim-info-set! targ prim-info) |
| 8253 |
(target-label-info-set! targ label-info) |
| 8254 |
(target-jump-info-set! targ jump-info) |
| 8255 |
(target-proc-result-set! targ (make-reg 1)) |
| 8256 |
(target-task-return-set! targ return-reg) |
| 8257 |
(set! *info-port* info-port) |
| 8258 |
'()) |
| 8259 |
(define (end!) '()) |
| 8260 |
(define *info-port* '()) |
| 8261 |
(define nb-gvm-regs 5) |
| 8262 |
(define nb-arg-regs 3) |
| 8263 |
(define pointer-size 4) |
| 8264 |
(define prim-proc-table |
| 8265 |
(map (lambda (x) |
| 8266 |
(cons (string->canonical-symbol (car x)) |
| 8267 |
(apply make-proc-obj (car x) #t #f (cdr x)))) |
| 8268 |
prim-procs)) |
| 8269 |
(define (prim-info name) |
| 8270 |
(let ((x (assq name prim-proc-table))) (if x (cdr x) #f))) |
| 8271 |
(define (get-prim-info name) |
| 8272 |
(let ((proc (prim-info (string->canonical-symbol name)))) |
| 8273 |
(if proc |
| 8274 |
proc |
| 8275 |
(compiler-internal-error "get-prim-info, unknown primitive:" name)))) |
| 8276 |
(define (label-info min-args nb-parms rest? closed?) |
| 8277 |
(let ((nb-stacked (max 0 (- nb-parms nb-arg-regs)))) |
| 8278 |
(define (location-of-parms i) |
| 8279 |
(if (> i nb-parms) |
| 8280 |
'() |
| 8281 |
(cons (cons i |
| 8282 |
(if (> i nb-stacked) |
| 8283 |
(make-reg (- i nb-stacked)) |
| 8284 |
(make-stk i))) |
| 8285 |
(location-of-parms (+ i 1))))) |
| 8286 |
(let ((x (cons (cons 'return 0) (location-of-parms 1)))) |
| 8287 |
(make-pcontext |
| 8288 |
nb-stacked |
| 8289 |
(if closed? |
| 8290 |
(cons (cons 'closure-env (make-reg (+ nb-arg-regs 1))) x) |
| 8291 |
x))))) |
| 8292 |
(define (jump-info nb-args) |
| 8293 |
(let ((nb-stacked (max 0 (- nb-args nb-arg-regs)))) |
| 8294 |
(define (location-of-args i) |
| 8295 |
(if (> i nb-args) |
| 8296 |
'() |
| 8297 |
(cons (cons i |
| 8298 |
(if (> i nb-stacked) |
| 8299 |
(make-reg (- i nb-stacked)) |
| 8300 |
(make-stk i))) |
| 8301 |
(location-of-args (+ i 1))))) |
| 8302 |
(make-pcontext |
| 8303 |
nb-stacked |
| 8304 |
(cons (cons 'return (make-reg 0)) (location-of-args 1))))) |
| 8305 |
(define (closed-var-offset i) (+ (* i pointer-size) 2)) |
| 8306 |
(define (dump proc filename c-intf options) |
| 8307 |
(if *info-port* |
| 8308 |
(begin (display "Dumping:" *info-port*) (newline *info-port*))) |
| 8309 |
(set! ofile-asm? (memq 'asm options)) |
| 8310 |
(set! ofile-stats? (memq 'stats options)) |
| 8311 |
(set! debug-info? (memq 'debug options)) |
| 8312 |
(set! object-queue (queue-empty)) |
| 8313 |
(set! objects-dumped (queue-empty)) |
| 8314 |
(ofile.begin! filename add-object) |
| 8315 |
(queue-put! object-queue proc) |
| 8316 |
(queue-put! objects-dumped proc) |
| 8317 |
(let loop ((index 0)) |
| 8318 |
(if (not (queue-empty? object-queue)) |
| 8319 |
(let ((obj (queue-get! object-queue))) |
| 8320 |
(dump-object obj index) |
| 8321 |
(loop (+ index 1))))) |
| 8322 |
(ofile.end!) |
| 8323 |
(if *info-port* (newline *info-port*)) |
| 8324 |
(set! object-queue '()) |
| 8325 |
(set! objects-dumped '())) |
| 8326 |
(define debug-info? '()) |
| 8327 |
(define object-queue '()) |
| 8328 |
(define objects-dumped '()) |
| 8329 |
(define (add-object obj) |
| 8330 |
(if (and (proc-obj? obj) (not (proc-obj-code obj))) |
| 8331 |
#f |
| 8332 |
(let ((n (pos-in-list obj (queue->list objects-dumped)))) |
| 8333 |
(if n |
| 8334 |
n |
| 8335 |
(let ((m (length (queue->list objects-dumped)))) |
| 8336 |
(queue-put! objects-dumped obj) |
| 8337 |
(queue-put! object-queue obj) |
| 8338 |
m))))) |
| 8339 |
(define (dump-object obj index) |
| 8340 |
(ofile-line "|------------------------------------------------------") |
| 8341 |
(case (obj-type obj) |
| 8342 |
((pair) (dump-pair obj)) |
| 8343 |
((flonum) (dump-flonum obj)) |
| 8344 |
((subtyped) |
| 8345 |
(case (obj-subtype obj) |
| 8346 |
((vector) (dump-vector obj)) |
| 8347 |
((symbol) (dump-symbol obj)) |
| 8348 |
((ratnum) (dump-ratnum obj)) |
| 8349 |
((cpxnum) (dump-cpxnum obj)) |
| 8350 |
((string) (dump-string obj)) |
| 8351 |
((bignum) (dump-bignum obj)) |
| 8352 |
(else |
| 8353 |
(compiler-internal-error |
| 8354 |
"dump-object, can't dump object 'obj':" |
| 8355 |
obj)))) |
| 8356 |
((procedure) (dump-procedure obj)) |
| 8357 |
(else |
| 8358 |
(compiler-internal-error "dump-object, can't dump object 'obj':" obj)))) |
| 8359 |
(define (dump-pair pair) |
| 8360 |
(ofile-long pair-prefix) |
| 8361 |
(ofile-ref (cdr pair)) |
| 8362 |
(ofile-ref (car pair))) |
| 8363 |
(define (dump-vector v) |
| 8364 |
(ofile-long (+ (* (vector-length v) 1024) (* subtype-vector 8))) |
| 8365 |
(let ((len (vector-length v))) |
| 8366 |
(let loop ((i 0)) |
| 8367 |
(if (< i len) (begin (ofile-ref (vector-ref v i)) (loop (+ i 1))))))) |
| 8368 |
(define (dump-symbol sym) |
| 8369 |
(compiler-internal-error "dump-symbol, can't dump SYMBOL type")) |
| 8370 |
(define (dump-ratnum x) |
| 8371 |
(ofile-long (+ (* 2 1024) (* subtype-ratnum 8))) |
| 8372 |
(ofile-ref (numerator x)) |
| 8373 |
(ofile-ref (denominator x))) |
| 8374 |
(define (dump-cpxnum x) |
| 8375 |
(ofile-long (+ (* 2 1024) (* subtype-cpxnum 8))) |
| 8376 |
(ofile-ref (real-part x)) |
| 8377 |
(ofile-ref (imag-part x))) |
| 8378 |
(define (dump-string s) |
| 8379 |
(ofile-long (+ (* (+ (string-length s) 1) 256) (* subtype-string 8))) |
| 8380 |
(let ((len (string-length s))) |
| 8381 |
(define (ref i) (if (>= i len) 0 (character-encoding (string-ref s i)))) |
| 8382 |
(let loop ((i 0)) |
| 8383 |
(if (<= i len) |
| 8384 |
(begin |
| 8385 |
(ofile-word (+ (* (ref i) 256) (ref (+ i 1)))) |
| 8386 |
(loop (+ i 2))))))) |
| 8387 |
(define (dump-flonum x) |
| 8388 |
(let ((bits (flonum->bits x))) |
| 8389 |
(ofile-long flonum-prefix) |
| 8390 |
(ofile-long (quotient bits 4294967296)) |
| 8391 |
(ofile-long (modulo bits 4294967296)))) |
| 8392 |
(define (flonum->inexact-exponential-format x) |
| 8393 |
(define (exp-form-pos x y i) |
| 8394 |
(let ((i*2 (+ i i))) |
| 8395 |
(let ((z (if (and (not (< flonum-e-bias i*2)) (not (< x y))) |
| 8396 |
(exp-form-pos x (* y y) i*2) |
| 8397 |
(cons x 0)))) |
| 8398 |
(let ((a (car z)) (b (cdr z))) |
| 8399 |
(let ((i+b (+ i b))) |
| 8400 |
(if (and (not (< flonum-e-bias i+b)) (not (< a y))) |
| 8401 |
(begin (set-car! z (/ a y)) (set-cdr! z i+b))) |
| 8402 |
z))))) |
| 8403 |
(define (exp-form-neg x y i) |
| 8404 |
(let ((i*2 (+ i i))) |
| 8405 |
(let ((z (if (and (< i*2 flonum-e-bias-minus-1) (< x y)) |
| 8406 |
(exp-form-neg x (* y y) i*2) |
| 8407 |
(cons x 0)))) |
| 8408 |
(let ((a (car z)) (b (cdr z))) |
| 8409 |
(let ((i+b (+ i b))) |
| 8410 |
(if (and (< i+b flonum-e-bias-minus-1) (< a y)) |
| 8411 |
(begin (set-car! z (/ a y)) (set-cdr! z i+b))) |
| 8412 |
z))))) |
| 8413 |
(define (exp-form x) |
| 8414 |
(if (< x inexact-+1) |
| 8415 |
(let ((z (exp-form-neg x inexact-+1/2 1))) |
| 8416 |
(set-car! z (* inexact-+2 (car z))) |
| 8417 |
(set-cdr! z (- -1 (cdr z))) |
| 8418 |
z) |
| 8419 |
(exp-form-pos x inexact-+2 1))) |
| 8420 |
(if (negative? x) |
| 8421 |
(let ((z (exp-form (- inexact-0 x)))) |
| 8422 |
(set-car! z (- inexact-0 (car z))) |
| 8423 |
z) |
| 8424 |
(exp-form x))) |
| 8425 |
(define (flonum->exact-exponential-format x) |
| 8426 |
(let ((z (flonum->inexact-exponential-format x))) |
| 8427 |
(let ((y (car z))) |
| 8428 |
(cond ((not (< y inexact-+2)) |
| 8429 |
(set-car! z flonum-+m-min) |
| 8430 |
(set-cdr! z flonum-e-bias-plus-1)) |
| 8431 |
((not (< inexact--2 y)) |
| 8432 |
(set-car! z flonum--m-min) |
| 8433 |
(set-cdr! z flonum-e-bias-plus-1)) |
| 8434 |
(else |
| 8435 |
(set-car! |
| 8436 |
z |
| 8437 |
(truncate (inexact->exact (* (car z) inexact-m-min)))))) |
| 8438 |
(set-cdr! z (- (cdr z) flonum-m-bits)) |
| 8439 |
z))) |
| 8440 |
(define (flonum->bits x) |
| 8441 |
(define (bits a b) |
| 8442 |
(if (< a flonum-+m-min) |
| 8443 |
a |
| 8444 |
(+ (- a flonum-+m-min) |
| 8445 |
(* (+ (+ b flonum-m-bits) flonum-e-bias) flonum-+m-min)))) |
| 8446 |
(let ((z (flonum->exact-exponential-format x))) |
| 8447 |
(let ((a (car z)) (b (cdr z))) |
| 8448 |
(if (negative? a) (+ flonum-sign-bit (bits (- 0 a) b)) (bits a b))))) |
| 8449 |
(define flonum-m-bits 52) |
| 8450 |
(define flonum-e-bits 11) |
| 8451 |
(define flonum-sign-bit 9223372036854775808) |
| 8452 |
(define flonum-+m-min 4503599627370496) |
| 8453 |
(define flonum--m-min -4503599627370496) |
| 8454 |
(define flonum-e-bias 1023) |
| 8455 |
(define flonum-e-bias-plus-1 1024) |
| 8456 |
(define flonum-e-bias-minus-1 1022) |
| 8457 |
(define inexact-m-min (exact->inexact flonum-+m-min)) |
| 8458 |
(define inexact-+2 (exact->inexact 2)) |
| 8459 |
(define inexact--2 (exact->inexact -2)) |
| 8460 |
(define inexact-+1 (exact->inexact 1)) |
| 8461 |
(define inexact-+1/2 (exact->inexact (/ 1 2))) |
| 8462 |
(define inexact-0 (exact->inexact 0)) |
| 8463 |
(define (dump-bignum x) |
| 8464 |
(define radix 16384) |
| 8465 |
(define (integer->digits n) |
| 8466 |
(if (= n 0) |
| 8467 |
'() |
| 8468 |
(cons (remainder n radix) (integer->digits (quotient n radix))))) |
| 8469 |
(let ((l (integer->digits (abs x)))) |
| 8470 |
(ofile-long (+ (* (+ (length l) 1) 512) (* subtype-bignum 8))) |
| 8471 |
(if (< x 0) (ofile-word 0) (ofile-word 1)) |
| 8472 |
(for-each ofile-word l))) |
| 8473 |
(define (dump-procedure proc) |
| 8474 |
(let ((bbs (proc-obj-code proc))) |
| 8475 |
(set! entry-lbl-num (bbs-entry-lbl-num bbs)) |
| 8476 |
(set! label-counter (bbs-lbl-counter bbs)) |
| 8477 |
(set! var-descr-queue (queue-empty)) |
| 8478 |
(set! first-class-label-queue (queue-empty)) |
| 8479 |
(set! deferred-code-queue (queue-empty)) |
| 8480 |
(if *info-port* |
| 8481 |
(begin |
| 8482 |
(display " #[" *info-port*) |
| 8483 |
(if (proc-obj-primitive? proc) |
| 8484 |
(display "primitive " *info-port*) |
| 8485 |
(display "procedure " *info-port*)) |
| 8486 |
(display (proc-obj-name proc) *info-port*) |
| 8487 |
(display "]" *info-port*))) |
| 8488 |
(if (proc-obj-primitive? proc) |
| 8489 |
(ofile-prim-proc (proc-obj-name proc)) |
| 8490 |
(ofile-user-proc)) |
| 8491 |
(asm.begin!) |
| 8492 |
(let loop ((prev-bb #f) (prev-gvm-instr #f) (l (bbs->code-list bbs))) |
| 8493 |
(if (not (null? l)) |
| 8494 |
(let ((pres-bb (code-bb (car l))) |
| 8495 |
(pres-gvm-instr (code-gvm-instr (car l))) |
| 8496 |
(pres-slots-needed (code-slots-needed (car l))) |
| 8497 |
(next-gvm-instr |
| 8498 |
(if (null? (cdr l)) #f (code-gvm-instr (cadr l))))) |
| 8499 |
(if ofile-asm? (asm-comment (car l))) |
| 8500 |
(gen-gvm-instr |
| 8501 |
prev-gvm-instr |
| 8502 |
pres-gvm-instr |
| 8503 |
next-gvm-instr |
| 8504 |
pres-slots-needed) |
| 8505 |
(loop pres-bb pres-gvm-instr (cdr l))))) |
| 8506 |
(asm.end! |
| 8507 |
(if debug-info? |
| 8508 |
(vector (lst->vector (queue->list first-class-label-queue)) |
| 8509 |
(lst->vector (queue->list var-descr-queue))) |
| 8510 |
#f)) |
| 8511 |
(if *info-port* (newline *info-port*)) |
| 8512 |
(set! var-descr-queue '()) |
| 8513 |
(set! first-class-label-queue '()) |
| 8514 |
(set! deferred-code-queue '()) |
| 8515 |
(set! instr-source '()) |
| 8516 |
(set! entry-frame '()) |
| 8517 |
(set! exit-frame '()))) |
| 8518 |
(define label-counter '()) |
| 8519 |
(define entry-lbl-num '()) |
| 8520 |
(define var-descr-queue '()) |
| 8521 |
(define first-class-label-queue '()) |
| 8522 |
(define deferred-code-queue '()) |
| 8523 |
(define instr-source '()) |
| 8524 |
(define entry-frame '()) |
| 8525 |
(define exit-frame '()) |
| 8526 |
(define (defer-code! thunk) (queue-put! deferred-code-queue thunk)) |
| 8527 |
(define (gen-deferred-code!) |
| 8528 |
(let loop () |
| 8529 |
(if (not (queue-empty? deferred-code-queue)) |
| 8530 |
(let ((thunk (queue-get! deferred-code-queue))) (thunk) (loop))))) |
| 8531 |
(define (add-var-descr! descr) |
| 8532 |
(define (index x l) |
| 8533 |
(let loop ((l l) (i 0)) |
| 8534 |
(cond ((not (pair? l)) #f) |
| 8535 |
((equal? (car l) x) i) |
| 8536 |
(else (loop (cdr l) (+ i 1)))))) |
| 8537 |
(let ((n (index descr (queue->list var-descr-queue)))) |
| 8538 |
(if n |
| 8539 |
n |
| 8540 |
(let ((m (length (queue->list var-descr-queue)))) |
| 8541 |
(queue-put! var-descr-queue descr) |
| 8542 |
m)))) |
| 8543 |
(define (add-first-class-label! source slots frame) |
| 8544 |
(let loop ((i 0) (l1 slots) (l2 '())) |
| 8545 |
(if (pair? l1) |
| 8546 |
(let ((var (car l1))) |
| 8547 |
(let ((x (frame-live? var frame))) |
| 8548 |
(if (and x (or (pair? x) (not (temp-var? x)))) |
| 8549 |
(let ((descr-index |
| 8550 |
(add-var-descr! |
| 8551 |
(if (pair? x) |
| 8552 |
(map (lambda (y) (add-var-descr! (var-name y))) x) |
| 8553 |
(var-name x))))) |
| 8554 |
(loop (+ i 1) |
| 8555 |
(cdr l1) |
| 8556 |
(cons (+ (* i 16384) descr-index) l2))) |
| 8557 |
(loop (+ i 1) (cdr l1) l2)))) |
| 8558 |
(let ((label-descr (lst->vector (cons 0 (cons source l2))))) |
| 8559 |
(queue-put! first-class-label-queue label-descr) |
| 8560 |
label-descr)))) |
| 8561 |
(define (gen-gvm-instr prev-gvm-instr gvm-instr next-gvm-instr sn) |
| 8562 |
(set! instr-source (comment-get (gvm-instr-comment gvm-instr) 'source)) |
| 8563 |
(set! exit-frame (gvm-instr-frame gvm-instr)) |
| 8564 |
(set! entry-frame (and prev-gvm-instr (gvm-instr-frame prev-gvm-instr))) |
| 8565 |
(case (gvm-instr-type gvm-instr) |
| 8566 |
((label) |
| 8567 |
(set! entry-frame exit-frame) |
| 8568 |
(set! current-fs (frame-size exit-frame)) |
| 8569 |
(case (label-type gvm-instr) |
| 8570 |
((simple) (gen-label-simple (label-lbl-num gvm-instr) sn)) |
| 8571 |
((entry) |
| 8572 |
(gen-label-entry |
| 8573 |
(label-lbl-num gvm-instr) |
| 8574 |
(label-entry-nb-parms gvm-instr) |
| 8575 |
(label-entry-min gvm-instr) |
| 8576 |
(label-entry-rest? gvm-instr) |
| 8577 |
(label-entry-closed? gvm-instr) |
| 8578 |
sn)) |
| 8579 |
((return) (gen-label-return (label-lbl-num gvm-instr) sn)) |
| 8580 |
((task-entry) (gen-label-task-entry (label-lbl-num gvm-instr) sn)) |
| 8581 |
((task-return) (gen-label-task-return (label-lbl-num gvm-instr) sn)) |
| 8582 |
(else (compiler-internal-error "gen-gvm-instr, unknown label type")))) |
| 8583 |
((apply) |
| 8584 |
(gen-apply |
| 8585 |
(apply-prim gvm-instr) |
| 8586 |
(apply-opnds gvm-instr) |
| 8587 |
(apply-loc gvm-instr) |
| 8588 |
sn)) |
| 8589 |
((copy) (gen-copy (copy-opnd gvm-instr) (copy-loc gvm-instr) sn)) |
| 8590 |
((close) (gen-close (close-parms gvm-instr) sn)) |
| 8591 |
((ifjump) |
| 8592 |
(gen-ifjump |
| 8593 |
(ifjump-test gvm-instr) |
| 8594 |
(ifjump-opnds gvm-instr) |
| 8595 |
(ifjump-true gvm-instr) |
| 8596 |
(ifjump-false gvm-instr) |
| 8597 |
(ifjump-poll? gvm-instr) |
| 8598 |
(if (and next-gvm-instr |
| 8599 |
(memq (label-type next-gvm-instr) '(simple task-entry))) |
| 8600 |
(label-lbl-num next-gvm-instr) |
| 8601 |
#f))) |
| 8602 |
((jump) |
| 8603 |
(gen-jump |
| 8604 |
(jump-opnd gvm-instr) |
| 8605 |
(jump-nb-args gvm-instr) |
| 8606 |
(jump-poll? gvm-instr) |
| 8607 |
(if (and next-gvm-instr |
| 8608 |
(memq (label-type next-gvm-instr) '(simple task-entry))) |
| 8609 |
(label-lbl-num next-gvm-instr) |
| 8610 |
#f))) |
| 8611 |
(else |
| 8612 |
(compiler-internal-error |
| 8613 |
"gen-gvm-instr, unknown 'gvm-instr':" |
| 8614 |
gvm-instr)))) |
| 8615 |
(define (reg-in-opnd68 opnd) |
| 8616 |
(cond ((dreg? opnd) opnd) |
| 8617 |
((areg? opnd) opnd) |
| 8618 |
((ind? opnd) (ind-areg opnd)) |
| 8619 |
((pinc? opnd) (pinc-areg opnd)) |
| 8620 |
((pdec? opnd) (pdec-areg opnd)) |
| 8621 |
((disp? opnd) (disp-areg opnd)) |
| 8622 |
((inx? opnd) (inx-ireg opnd)) |
| 8623 |
(else #f))) |
| 8624 |
(define (temp-in-opnd68 opnd) |
| 8625 |
(let ((reg (reg-in-opnd68 opnd))) |
| 8626 |
(if reg |
| 8627 |
(cond ((identical-opnd68? reg dtemp1) reg) |
| 8628 |
((identical-opnd68? reg atemp1) reg) |
| 8629 |
((identical-opnd68? reg atemp2) reg) |
| 8630 |
(else #f)) |
| 8631 |
#f))) |
| 8632 |
(define (pick-atemp keep) |
| 8633 |
(if (and keep (identical-opnd68? keep atemp1)) atemp2 atemp1)) |
| 8634 |
(define return-reg '()) |
| 8635 |
(define max-nb-args 1024) |
| 8636 |
(define heap-allocation-fudge (* pointer-size (+ (* 2 max-nb-args) 1024))) |
| 8637 |
(define intr-flag 0) |
| 8638 |
(define ltq-tail 1) |
| 8639 |
(define ltq-head 2) |
| 8640 |
(define heap-lim 12) |
| 8641 |
(define closure-lim 17) |
| 8642 |
(define closure-ptr 18) |
| 8643 |
(define intr-flag-slot (make-disp* pstate-reg (* pointer-size intr-flag))) |
| 8644 |
(define ltq-tail-slot (make-disp* pstate-reg (* pointer-size ltq-tail))) |
| 8645 |
(define ltq-head-slot (make-disp* pstate-reg (* pointer-size ltq-head))) |
| 8646 |
(define heap-lim-slot (make-disp* pstate-reg (* pointer-size heap-lim))) |
| 8647 |
(define closure-lim-slot (make-disp* pstate-reg (* pointer-size closure-lim))) |
| 8648 |
(define closure-ptr-slot (make-disp* pstate-reg (* pointer-size closure-ptr))) |
| 8649 |
(define touch-trap 1) |
| 8650 |
(define non-proc-jump-trap 6) |
| 8651 |
(define rest-params-trap 7) |
| 8652 |
(define rest-params-closed-trap 8) |
| 8653 |
(define wrong-nb-arg1-trap 9) |
| 8654 |
(define wrong-nb-arg1-closed-trap 10) |
| 8655 |
(define wrong-nb-arg2-trap 11) |
| 8656 |
(define wrong-nb-arg2-closed-trap 12) |
| 8657 |
(define heap-alloc1-trap 13) |
| 8658 |
(define heap-alloc2-trap 14) |
| 8659 |
(define closure-alloc-trap 15) |
| 8660 |
(define intr-trap 24) |
| 8661 |
(define cache-line-length 16) |
| 8662 |
(define polling-intermittency 10) |
| 8663 |
(define (stat-clear!) (set! *stats* (cons 0 '()))) |
| 8664 |
(define (stat-dump!) (emit-stat (cdr *stats*))) |
| 8665 |
(define (stat-add! bin count) |
| 8666 |
(define (add! stats bin count) |
| 8667 |
(set-car! stats (+ (car stats) count)) |
| 8668 |
(if (not (null? bin)) |
| 8669 |
(let ((x (assoc (car bin) (cdr stats)))) |
| 8670 |
(if x |
| 8671 |
(add! (cdr x) (cdr bin) count) |
| 8672 |
(begin |
| 8673 |
(set-cdr! stats (cons (list (car bin) 0) (cdr stats))) |
| 8674 |
(add! (cdadr stats) (cdr bin) count)))))) |
| 8675 |
(add! *stats* bin count)) |
| 8676 |
(define (fetch-stat-add! gvm-opnd) (opnd-stat-add! 'fetch gvm-opnd)) |
| 8677 |
(define (store-stat-add! gvm-opnd) (opnd-stat-add! 'store gvm-opnd)) |
| 8678 |
(define (jump-stat-add! gvm-opnd) (opnd-stat-add! 'jump gvm-opnd)) |
| 8679 |
(define (opnd-stat-add! type opnd) |
| 8680 |
(cond ((reg? opnd) (stat-add! (list 'gvm-opnd 'reg type (reg-num opnd)) 1)) |
| 8681 |
((stk? opnd) (stat-add! (list 'gvm-opnd 'stk type) 1)) |
| 8682 |
((glo? opnd) (stat-add! (list 'gvm-opnd 'glo type (glo-name opnd)) 1)) |
| 8683 |
((clo? opnd) |
| 8684 |
(stat-add! (list 'gvm-opnd 'clo type) 1) |
| 8685 |
(fetch-stat-add! (clo-base opnd))) |
| 8686 |
((lbl? opnd) (stat-add! (list 'gvm-opnd 'lbl type) 1)) |
| 8687 |
((obj? opnd) |
| 8688 |
(let ((val (obj-val opnd))) |
| 8689 |
(if (number? val) |
| 8690 |
(stat-add! (list 'gvm-opnd 'obj type val) 1) |
| 8691 |
(stat-add! (list 'gvm-opnd 'obj type (obj-type val)) 1)))) |
| 8692 |
(else |
| 8693 |
(compiler-internal-error "opnd-stat-add!, unknown 'opnd':" opnd)))) |
| 8694 |
(define (opnd-stat opnd) |
| 8695 |
(cond ((reg? opnd) 'reg) |
| 8696 |
((stk? opnd) 'stk) |
| 8697 |
((glo? opnd) 'glo) |
| 8698 |
((clo? opnd) 'clo) |
| 8699 |
((lbl? opnd) 'lbl) |
| 8700 |
((obj? opnd) 'obj) |
| 8701 |
(else (compiler-internal-error "opnd-stat, unknown 'opnd':" opnd)))) |
| 8702 |
(define *stats* '()) |
| 8703 |
(define (move-opnd68-to-loc68 opnd loc) |
| 8704 |
(if (not (identical-opnd68? opnd loc)) |
| 8705 |
(if (imm? opnd) |
| 8706 |
(move-n-to-loc68 (imm-val opnd) loc) |
| 8707 |
(emit-move.l opnd loc)))) |
| 8708 |
(define (move-obj-to-loc68 obj loc) |
| 8709 |
(let ((n (obj-encoding obj))) |
| 8710 |
(if n (move-n-to-loc68 n loc) (emit-move.l (emit-const obj) loc)))) |
| 8711 |
(define (move-n-to-loc68 n loc) |
| 8712 |
(cond ((= n bits-null) (emit-move.l null-reg loc)) |
| 8713 |
((= n bits-false) (emit-move.l false-reg loc)) |
| 8714 |
((and (dreg? loc) (>= n -128) (<= n 127)) (emit-moveq n loc)) |
| 8715 |
((and (areg? loc) (>= n -32768) (<= n 32767)) |
| 8716 |
(emit-move.w (make-imm n) loc)) |
| 8717 |
((and (identical-opnd68? loc pdec-sp) (>= n -32768) (<= n 32767)) |
| 8718 |
(emit-pea* n)) |
| 8719 |
((= n 0) (emit-clr.l loc)) |
| 8720 |
((and (not (and (inx? loc) (= (inx-ireg loc) dtemp1))) |
| 8721 |
(>= n -128) |
| 8722 |
(<= n 127)) |
| 8723 |
(emit-moveq n dtemp1) |
| 8724 |
(emit-move.l dtemp1 loc)) |
| 8725 |
(else (emit-move.l (make-imm n) loc)))) |
| 8726 |
(define (add-n-to-loc68 n loc) |
| 8727 |
(if (not (= n 0)) |
| 8728 |
(cond ((and (>= n -8) (<= n 8)) |
| 8729 |
(if (> n 0) (emit-addq.l n loc) (emit-subq.l (- n) loc))) |
| 8730 |
((and (areg? loc) (>= n -32768) (<= n 32767)) |
| 8731 |
(emit-lea (make-disp loc n) loc)) |
| 8732 |
((and (not (identical-opnd68? loc dtemp1)) (>= n -128) (<= n 128)) |
| 8733 |
(emit-moveq (- (abs n)) dtemp1) |
| 8734 |
(if (> n 0) (emit-sub.l dtemp1 loc) (emit-add.l dtemp1 loc))) |
| 8735 |
(else (emit-add.l (make-imm n) loc))))) |
| 8736 |
(define (power-of-2 n) |
| 8737 |
(let loop ((i 0) (k 1)) |
| 8738 |
(cond ((= k n) i) ((> k n) #f) (else (loop (+ i 1) (* k 2)))))) |
| 8739 |
(define (mul-n-to-reg68 n reg) |
| 8740 |
(if (= n 0) |
| 8741 |
(emit-moveq 0 reg) |
| 8742 |
(let ((abs-n (abs n))) |
| 8743 |
(if (= abs-n 1) |
| 8744 |
(if (< n 0) (emit-neg.l reg)) |
| 8745 |
(let ((shift (power-of-2 abs-n))) |
| 8746 |
(if shift |
| 8747 |
(let ((m (min shift 32))) |
| 8748 |
(if (or (<= m 8) (identical-opnd68? reg dtemp1)) |
| 8749 |
(let loop ((i m)) |
| 8750 |
(if (> i 0) |
| 8751 |
(begin |
| 8752 |
(emit-asl.l (make-imm (min i 8)) reg) |
| 8753 |
(loop (- i 8))))) |
| 8754 |
(begin (emit-moveq m dtemp1) (emit-asl.l dtemp1 reg))) |
| 8755 |
(if (< n 0) (emit-neg.l reg))) |
| 8756 |
(emit-muls.l (make-imm n) reg))))))) |
| 8757 |
(define (div-n-to-reg68 n reg) |
| 8758 |
(let ((abs-n (abs n))) |
| 8759 |
(if (= abs-n 1) |
| 8760 |
(if (< n 0) (emit-neg.l reg)) |
| 8761 |
(let ((shift (power-of-2 abs-n))) |
| 8762 |
(if shift |
| 8763 |
(let ((m (min shift 32)) (lbl (new-lbl!))) |
| 8764 |
(emit-move.l reg reg) |
| 8765 |
(emit-bpl lbl) |
| 8766 |
(add-n-to-loc68 (* (- abs-n 1) 8) reg) |
| 8767 |
(emit-label lbl) |
| 8768 |
(if (or (<= m 8) (identical-opnd68? reg dtemp1)) |
| 8769 |
(let loop ((i m)) |
| 8770 |
(if (> i 0) |
| 8771 |
(begin |
| 8772 |
(emit-asr.l (make-imm (min i 8)) reg) |
| 8773 |
(loop (- i 8))))) |
| 8774 |
(begin (emit-moveq m dtemp1) (emit-asr.l dtemp1 reg))) |
| 8775 |
(if (< n 0) (emit-neg.l reg))) |
| 8776 |
(emit-divsl.l (make-imm n) reg reg)))))) |
| 8777 |
(define (cmp-n-to-opnd68 n opnd) |
| 8778 |
(cond ((= n bits-null) (emit-cmp.l opnd null-reg) #f) |
| 8779 |
((= n bits-false) (emit-cmp.l opnd false-reg) #f) |
| 8780 |
((or (pcr? opnd) (imm? opnd)) |
| 8781 |
(if (= n 0) |
| 8782 |
(begin (emit-move.l opnd dtemp1) #t) |
| 8783 |
(begin |
| 8784 |
(move-opnd68-to-loc68 opnd atemp1) |
| 8785 |
(if (and (>= n -32768) (<= n 32767)) |
| 8786 |
(emit-cmp.w (make-imm n) atemp1) |
| 8787 |
(emit-cmp.l (make-imm n) atemp1)) |
| 8788 |
#t))) |
| 8789 |
((= n 0) (emit-move.l opnd dtemp1) #t) |
| 8790 |
((and (>= n -128) (<= n 127) (not (identical-opnd68? opnd dtemp1))) |
| 8791 |
(emit-moveq n dtemp1) |
| 8792 |
(emit-cmp.l opnd dtemp1) |
| 8793 |
#f) |
| 8794 |
(else (emit-cmp.l (make-imm n) opnd) #t))) |
| 8795 |
(define current-fs '()) |
| 8796 |
(define (adjust-current-fs n) (set! current-fs (+ current-fs n))) |
| 8797 |
(define (new-lbl!) (label-counter)) |
| 8798 |
(define (needed? loc sn) (and loc (if (stk? loc) (<= (stk-num loc) sn) #t))) |
| 8799 |
(define (sn-opnd opnd sn) |
| 8800 |
(cond ((stk? opnd) (max (stk-num opnd) sn)) |
| 8801 |
((clo? opnd) (sn-opnd (clo-base opnd) sn)) |
| 8802 |
(else sn))) |
| 8803 |
(define (sn-opnds opnds sn) |
| 8804 |
(if (null? opnds) sn (sn-opnd (car opnds) (sn-opnds (cdr opnds) sn)))) |
| 8805 |
(define (sn-opnd68 opnd sn) |
| 8806 |
(cond ((and (disp*? opnd) (identical-opnd68? (disp*-areg opnd) sp-reg)) |
| 8807 |
(max (disp*-offset opnd) sn)) |
| 8808 |
((identical-opnd68? opnd pdec-sp) (max (+ current-fs 1) sn)) |
| 8809 |
((identical-opnd68? opnd pinc-sp) (max current-fs sn)) |
| 8810 |
(else sn))) |
| 8811 |
(define (resize-frame n) |
| 8812 |
(let ((x (- n current-fs))) |
| 8813 |
(adjust-current-fs x) |
| 8814 |
(add-n-to-loc68 (* (- pointer-size) x) sp-reg))) |
| 8815 |
(define (shrink-frame n) |
| 8816 |
(cond ((< n current-fs) (resize-frame n)) |
| 8817 |
((> n current-fs) |
| 8818 |
(compiler-internal-error "shrink-frame, can't increase frame size")))) |
| 8819 |
(define (make-top-of-frame n sn) |
| 8820 |
(if (and (< n current-fs) (>= n sn)) (resize-frame n))) |
| 8821 |
(define (make-top-of-frame-if-stk-opnd68 opnd sn) |
| 8822 |
(if (frame-base-rel? opnd) |
| 8823 |
(make-top-of-frame (frame-base-rel-slot opnd) sn))) |
| 8824 |
(define (make-top-of-frame-if-stk-opnds68 opnd1 opnd2 sn) |
| 8825 |
(if (frame-base-rel? opnd1) |
| 8826 |
(let ((slot1 (frame-base-rel-slot opnd1))) |
| 8827 |
(if (frame-base-rel? opnd2) |
| 8828 |
(make-top-of-frame (max (frame-base-rel-slot opnd2) slot1) sn) |
| 8829 |
(make-top-of-frame slot1 sn))) |
| 8830 |
(if (frame-base-rel? opnd2) |
| 8831 |
(make-top-of-frame (frame-base-rel-slot opnd2) sn)))) |
| 8832 |
(define (opnd68->true-opnd68 opnd sn) |
| 8833 |
(if (frame-base-rel? opnd) |
| 8834 |
(let ((slot (frame-base-rel-slot opnd))) |
| 8835 |
(cond ((> slot current-fs) (adjust-current-fs 1) pdec-sp) |
| 8836 |
((and (= slot current-fs) (< sn current-fs)) |
| 8837 |
(adjust-current-fs -1) |
| 8838 |
pinc-sp) |
| 8839 |
(else (make-disp* sp-reg (* pointer-size (- current-fs slot)))))) |
| 8840 |
opnd)) |
| 8841 |
(define (move-opnd68-to-any-areg opnd keep sn) |
| 8842 |
(if (areg? opnd) |
| 8843 |
opnd |
| 8844 |
(let ((areg (pick-atemp keep))) |
| 8845 |
(make-top-of-frame-if-stk-opnd68 opnd sn) |
| 8846 |
(move-opnd68-to-loc68 (opnd68->true-opnd68 opnd sn) areg) |
| 8847 |
areg))) |
| 8848 |
(define (clo->opnd68 opnd keep sn) |
| 8849 |
(let ((base (clo-base opnd)) (offs (closed-var-offset (clo-index opnd)))) |
| 8850 |
(if (lbl? base) (make-pcr (lbl-num base) offs) (clo->loc68 opnd keep sn)))) |
| 8851 |
(define (clo->loc68 opnd keep sn) |
| 8852 |
(let ((base (clo-base opnd)) (offs (closed-var-offset (clo-index opnd)))) |
| 8853 |
(cond ((eq? base return-reg) (make-disp* (reg->reg68 base) offs)) |
| 8854 |
((obj? base) |
| 8855 |
(let ((areg (pick-atemp keep))) |
| 8856 |
(move-obj-to-loc68 (obj-val base) areg) |
| 8857 |
(make-disp* areg offs))) |
| 8858 |
(else |
| 8859 |
(let ((areg (pick-atemp keep))) |
| 8860 |
(move-opnd-to-loc68 base areg sn) |
| 8861 |
(make-disp* areg offs)))))) |
| 8862 |
(define (reg->reg68 reg) (reg-num->reg68 (reg-num reg))) |
| 8863 |
(define (reg-num->reg68 num) |
| 8864 |
(if (= num 0) (make-areg gvm-reg0) (make-dreg (+ (- num 1) gvm-reg1)))) |
| 8865 |
(define (opnd->opnd68 opnd keep sn) |
| 8866 |
(cond ((lbl? opnd) |
| 8867 |
(let ((areg (pick-atemp keep))) |
| 8868 |
(emit-lea (make-pcr (lbl-num opnd) 0) areg) |
| 8869 |
areg)) |
| 8870 |
((obj? opnd) |
| 8871 |
(let ((val (obj-val opnd))) |
| 8872 |
(if (proc-obj? val) |
| 8873 |
(let ((num (add-object val)) (areg (pick-atemp keep))) |
| 8874 |
(if num (emit-move-proc num areg) (emit-move-prim val areg)) |
| 8875 |
areg) |
| 8876 |
(let ((n (obj-encoding val))) |
| 8877 |
(if n (make-imm n) (emit-const val)))))) |
| 8878 |
((clo? opnd) (clo->opnd68 opnd keep sn)) |
| 8879 |
(else (loc->loc68 opnd keep sn)))) |
| 8880 |
(define (loc->loc68 loc keep sn) |
| 8881 |
(cond ((reg? loc) (reg->reg68 loc)) |
| 8882 |
((stk? loc) (make-frame-base-rel (stk-num loc))) |
| 8883 |
((glo? loc) (make-glob (glo-name loc))) |
| 8884 |
((clo? loc) (clo->loc68 loc keep sn)) |
| 8885 |
(else (compiler-internal-error "loc->loc68, unknown 'loc':" loc)))) |
| 8886 |
(define (move-opnd68-to-loc opnd loc sn) |
| 8887 |
(cond ((reg? loc) |
| 8888 |
(make-top-of-frame-if-stk-opnd68 opnd sn) |
| 8889 |
(move-opnd68-to-loc68 (opnd68->true-opnd68 opnd sn) (reg->reg68 loc))) |
| 8890 |
((stk? loc) |
| 8891 |
(let* ((loc-slot (stk-num loc)) |
| 8892 |
(sn-after-opnd1 (if (< loc-slot sn) sn (- loc-slot 1)))) |
| 8893 |
(if (> current-fs loc-slot) |
| 8894 |
(make-top-of-frame |
| 8895 |
(if (frame-base-rel? opnd) |
| 8896 |
(let ((opnd-slot (frame-base-rel-slot opnd))) |
| 8897 |
(if (>= opnd-slot (- loc-slot 1)) opnd-slot loc-slot)) |
| 8898 |
loc-slot) |
| 8899 |
sn-after-opnd1)) |
| 8900 |
(let* ((opnd1 (opnd68->true-opnd68 opnd sn-after-opnd1)) |
| 8901 |
(opnd2 (opnd68->true-opnd68 |
| 8902 |
(make-frame-base-rel loc-slot) |
| 8903 |
sn))) |
| 8904 |
(move-opnd68-to-loc68 opnd1 opnd2)))) |
| 8905 |
((glo? loc) |
| 8906 |
(make-top-of-frame-if-stk-opnd68 opnd sn) |
| 8907 |
(move-opnd68-to-loc68 |
| 8908 |
(opnd68->true-opnd68 opnd sn) |
| 8909 |
(make-glob (glo-name loc)))) |
| 8910 |
((clo? loc) |
| 8911 |
(let ((clo (clo->loc68 |
| 8912 |
loc |
| 8913 |
(temp-in-opnd68 opnd) |
| 8914 |
(sn-opnd68 opnd sn)))) |
| 8915 |
(make-top-of-frame-if-stk-opnd68 opnd sn) |
| 8916 |
(move-opnd68-to-loc68 (opnd68->true-opnd68 opnd sn) clo))) |
| 8917 |
(else |
| 8918 |
(compiler-internal-error "move-opnd68-to-loc, unknown 'loc':" loc)))) |
| 8919 |
(define (move-opnd-to-loc68 opnd loc68 sn) |
| 8920 |
(if (and (lbl? opnd) (areg? loc68)) |
| 8921 |
(emit-lea (make-pcr (lbl-num opnd) 0) loc68) |
| 8922 |
(let* ((sn-after-opnd68 (sn-opnd68 loc68 sn)) |
| 8923 |
(opnd68 (opnd->opnd68 |
| 8924 |
opnd |
| 8925 |
(temp-in-opnd68 loc68) |
| 8926 |
sn-after-opnd68))) |
| 8927 |
(make-top-of-frame-if-stk-opnds68 opnd68 loc68 sn) |
| 8928 |
(let* ((opnd68* (opnd68->true-opnd68 opnd68 sn-after-opnd68)) |
| 8929 |
(loc68* (opnd68->true-opnd68 loc68 sn))) |
| 8930 |
(move-opnd68-to-loc68 opnd68* loc68*))))) |
| 8931 |
(define (copy-opnd-to-loc opnd loc sn) |
| 8932 |
(if (and (lbl? opnd) (eq? loc return-reg)) |
| 8933 |
(emit-lea (make-pcr (lbl-num opnd) 0) (reg->reg68 loc)) |
| 8934 |
(move-opnd68-to-loc (opnd->opnd68 opnd #f (sn-opnd loc sn)) loc sn))) |
| 8935 |
(define (touch-reg68-to-reg68 src dst) |
| 8936 |
(define (trap-to-touch-handler dreg lbl) |
| 8937 |
(if ofile-stats? |
| 8938 |
(emit-stat |
| 8939 |
'((touch 0 |
| 8940 |
(determined-placeholder -1) |
| 8941 |
(undetermined-placeholder 1))))) |
| 8942 |
(gen-trap |
| 8943 |
instr-source |
| 8944 |
entry-frame |
| 8945 |
#t |
| 8946 |
dreg |
| 8947 |
(+ touch-trap (dreg-num dreg)) |
| 8948 |
lbl)) |
| 8949 |
(define (touch-dreg-to-reg src dst) |
| 8950 |
(let ((lbl1 (new-lbl!))) |
| 8951 |
(emit-btst src placeholder-reg) |
| 8952 |
(emit-bne lbl1) |
| 8953 |
(if ofile-stats? |
| 8954 |
(emit-stat |
| 8955 |
'((touch 0 (non-placeholder -1) (determined-placeholder 1))))) |
| 8956 |
(trap-to-touch-handler src lbl1) |
| 8957 |
(move-opnd68-to-loc68 src dst))) |
| 8958 |
(define (touch-areg-to-dreg src dst) |
| 8959 |
(let ((lbl1 (new-lbl!))) |
| 8960 |
(emit-move.l src dst) |
| 8961 |
(emit-btst dst placeholder-reg) |
| 8962 |
(emit-bne lbl1) |
| 8963 |
(if ofile-stats? |
| 8964 |
(emit-stat |
| 8965 |
'((touch 0 (non-placeholder -1) (determined-placeholder 1))))) |
| 8966 |
(trap-to-touch-handler dst lbl1))) |
| 8967 |
(if ofile-stats? (emit-stat '((touch 1 (non-placeholder 1))))) |
| 8968 |
(cond ((dreg? src) (touch-dreg-to-reg src dst)) |
| 8969 |
((dreg? dst) (touch-areg-to-dreg src dst)) |
| 8970 |
(else (emit-move.l src dtemp1) (touch-dreg-to-reg dtemp1 dst)))) |
| 8971 |
(define (touch-opnd-to-any-reg68 opnd sn) |
| 8972 |
(if (reg? opnd) |
| 8973 |
(let ((reg (reg->reg68 opnd))) (touch-reg68-to-reg68 reg reg) reg) |
| 8974 |
(let ((opnd68 (opnd->opnd68 opnd #f sn))) |
| 8975 |
(make-top-of-frame-if-stk-opnd68 opnd68 sn) |
| 8976 |
(move-opnd68-to-loc68 (opnd68->true-opnd68 opnd68 sn) dtemp1) |
| 8977 |
(touch-reg68-to-reg68 dtemp1 dtemp1) |
| 8978 |
dtemp1))) |
| 8979 |
(define (touch-opnd-to-loc opnd loc sn) |
| 8980 |
(if (reg? opnd) |
| 8981 |
(let ((reg68 (reg->reg68 opnd))) |
| 8982 |
(if (reg? loc) |
| 8983 |
(touch-reg68-to-reg68 reg68 (reg->reg68 loc)) |
| 8984 |
(begin |
| 8985 |
(touch-reg68-to-reg68 reg68 reg68) |
| 8986 |
(move-opnd68-to-loc reg68 loc sn)))) |
| 8987 |
(if (reg? loc) |
| 8988 |
(let ((reg68 (reg->reg68 loc))) |
| 8989 |
(move-opnd-to-loc68 opnd reg68 sn) |
| 8990 |
(touch-reg68-to-reg68 reg68 reg68)) |
| 8991 |
(let ((reg68 (touch-opnd-to-any-reg68 opnd sn))) |
| 8992 |
(move-opnd68-to-loc reg68 loc sn))))) |
| 8993 |
(define (gen-trap source frame save-live? not-save-reg num lbl) |
| 8994 |
(define (adjust-slots l n) |
| 8995 |
(cond ((= n 0) (append l '())) |
| 8996 |
((< n 0) (adjust-slots (cdr l) (+ n 1))) |
| 8997 |
(else (adjust-slots (cons empty-var l) (- n 1))))) |
| 8998 |
(define (set-slot! slots i x) |
| 8999 |
(let loop ((l slots) (n (- (length slots) i))) |
| 9000 |
(if (> n 0) (loop (cdr l) (- n 1)) (set-car! l x)))) |
| 9001 |
(let ((ret-slot (frame-first-empty-slot frame))) |
| 9002 |
(let loop1 ((save1 '()) (save2 #f) (regs (frame-regs frame)) (i 0)) |
| 9003 |
(if (pair? regs) |
| 9004 |
(let ((var (car regs))) |
| 9005 |
(if (eq? var ret-var) |
| 9006 |
(let ((x (cons (reg->reg68 (make-reg i)) var))) |
| 9007 |
(if (> ret-slot current-fs) |
| 9008 |
(loop1 (cons x save1) save2 (cdr regs) (+ i 1)) |
| 9009 |
(loop1 save1 x (cdr regs) (+ i 1)))) |
| 9010 |
(if (and save-live? |
| 9011 |
(frame-live? var frame) |
| 9012 |
(not (eqv? not-save-reg (reg->reg68 (make-reg i))))) |
| 9013 |
(loop1 (cons (cons (reg->reg68 (make-reg i)) var) save1) |
| 9014 |
save2 |
| 9015 |
(cdr regs) |
| 9016 |
(+ i 1)) |
| 9017 |
(loop1 save1 save2 (cdr regs) (+ i 1))))) |
| 9018 |
(let ((order (sort-list save1 (lambda (x y) (< (car x) (car y)))))) |
| 9019 |
(let ((slots (append (map cdr order) |
| 9020 |
(adjust-slots |
| 9021 |
(frame-slots frame) |
| 9022 |
(- current-fs (frame-size frame))))) |
| 9023 |
(reg-list (map car order)) |
| 9024 |
(nb-regs (length order))) |
| 9025 |
(define (trap) |
| 9026 |
(emit-trap2 num '()) |
| 9027 |
(gen-label-return* |
| 9028 |
(new-lbl!) |
| 9029 |
(add-first-class-label! source slots frame) |
| 9030 |
slots |
| 9031 |
0)) |
| 9032 |
(if save2 |
| 9033 |
(begin |
| 9034 |
(emit-move.l |
| 9035 |
(car save2) |
| 9036 |
(make-disp* |
| 9037 |
sp-reg |
| 9038 |
(* pointer-size (- current-fs ret-slot)))) |
| 9039 |
(set-slot! slots ret-slot (cdr save2)))) |
| 9040 |
(if (> (length order) 2) |
| 9041 |
(begin |
| 9042 |
(emit-movem.l reg-list pdec-sp) |
| 9043 |
(trap) |
| 9044 |
(emit-movem.l pinc-sp reg-list)) |
| 9045 |
(let loop2 ((l (reverse reg-list))) |
| 9046 |
(if (pair? l) |
| 9047 |
(let ((reg (car l))) |
| 9048 |
(emit-move.l reg pdec-sp) |
| 9049 |
(loop2 (cdr l)) |
| 9050 |
(emit-move.l pinc-sp reg)) |
| 9051 |
(trap)))) |
| 9052 |
(if save2 |
| 9053 |
(emit-move.l |
| 9054 |
(make-disp* sp-reg (* pointer-size (- current-fs ret-slot))) |
| 9055 |
(car save2))) |
| 9056 |
(emit-label lbl))))))) |
| 9057 |
(define (gen-label-simple lbl sn) |
| 9058 |
(if ofile-stats? |
| 9059 |
(begin (stat-clear!) (stat-add! '(gvm-instr label simple) 1))) |
| 9060 |
(set! pointers-allocated 0) |
| 9061 |
(emit-label lbl)) |
| 9062 |
(define (gen-label-entry lbl nb-parms min rest? closed? sn) |
| 9063 |
(if ofile-stats? |
| 9064 |
(begin |
| 9065 |
(stat-clear!) |
| 9066 |
(stat-add! |
| 9067 |
(list 'gvm-instr |
| 9068 |
'label |
| 9069 |
'entry |
| 9070 |
nb-parms |
| 9071 |
min |
| 9072 |
(if rest? 'rest 'not-rest) |
| 9073 |
(if closed? 'closed 'not-closed)) |
| 9074 |
1))) |
| 9075 |
(set! pointers-allocated 0) |
| 9076 |
(let ((label-descr (add-first-class-label! instr-source '() exit-frame))) |
| 9077 |
(if (= lbl entry-lbl-num) |
| 9078 |
(emit-label lbl) |
| 9079 |
(emit-label-subproc lbl entry-lbl-num label-descr))) |
| 9080 |
(let* ((nb-parms* (if rest? (- nb-parms 1) nb-parms)) |
| 9081 |
(dispatch-lbls (make-vector (+ (- nb-parms min) 1))) |
| 9082 |
(optional-lbls (make-vector (+ (- nb-parms min) 1)))) |
| 9083 |
(let loop ((i min)) |
| 9084 |
(if (<= i nb-parms) |
| 9085 |
(let ((lbl (new-lbl!))) |
| 9086 |
(vector-set! optional-lbls (- nb-parms i) lbl) |
| 9087 |
(vector-set! |
| 9088 |
dispatch-lbls |
| 9089 |
(- nb-parms i) |
| 9090 |
(if (or (>= i nb-parms) (<= nb-parms nb-arg-regs)) |
| 9091 |
lbl |
| 9092 |
(new-lbl!))) |
| 9093 |
(loop (+ i 1))))) |
| 9094 |
(if closed? |
| 9095 |
(let ((closure-reg (reg-num->reg68 (+ nb-arg-regs 1)))) |
| 9096 |
(emit-move.l pinc-sp closure-reg) |
| 9097 |
(emit-subq.l 6 closure-reg) |
| 9098 |
(if (or (and (<= min 1) (<= 1 nb-parms*)) |
| 9099 |
(and (<= min 2) (<= 2 nb-parms*))) |
| 9100 |
(emit-move.w dtemp1 dtemp1)))) |
| 9101 |
(if (and (<= min 2) (<= 2 nb-parms*)) |
| 9102 |
(emit-beq (vector-ref dispatch-lbls (- nb-parms 2)))) |
| 9103 |
(if (and (<= min 1) (<= 1 nb-parms*)) |
| 9104 |
(emit-bmi (vector-ref dispatch-lbls (- nb-parms 1)))) |
| 9105 |
(let loop ((i min)) |
| 9106 |
(if (<= i nb-parms*) |
| 9107 |
(begin |
| 9108 |
(if (not (or (= i 1) (= i 2))) |
| 9109 |
(begin |
| 9110 |
(emit-cmp.w (make-imm (encode-arg-count i)) arg-count-reg) |
| 9111 |
(emit-beq (vector-ref dispatch-lbls (- nb-parms i))))) |
| 9112 |
(loop (+ i 1))))) |
| 9113 |
(cond (rest? |
| 9114 |
(emit-trap1 |
| 9115 |
(if closed? rest-params-closed-trap rest-params-trap) |
| 9116 |
(list min nb-parms*)) |
| 9117 |
(if (not closed?) (emit-lbl-ptr lbl)) |
| 9118 |
(set! pointers-allocated 1) |
| 9119 |
(gen-guarantee-fudge) |
| 9120 |
(emit-bra (vector-ref optional-lbls 0))) |
| 9121 |
((= min nb-parms*) |
| 9122 |
(emit-trap1 |
| 9123 |
(if closed? wrong-nb-arg1-closed-trap wrong-nb-arg1-trap) |
| 9124 |
(list nb-parms*)) |
| 9125 |
(if (not closed?) (emit-lbl-ptr lbl))) |
| 9126 |
(else |
| 9127 |
(emit-trap1 |
| 9128 |
(if closed? wrong-nb-arg2-closed-trap wrong-nb-arg2-trap) |
| 9129 |
(list min nb-parms*)) |
| 9130 |
(if (not closed?) (emit-lbl-ptr lbl)))) |
| 9131 |
(if (> nb-parms nb-arg-regs) |
| 9132 |
(let loop1 ((i (- nb-parms 1))) |
| 9133 |
(if (>= i min) |
| 9134 |
(let ((nb-stacked (if (<= i nb-arg-regs) 0 (- i nb-arg-regs)))) |
| 9135 |
(emit-label (vector-ref dispatch-lbls (- nb-parms i))) |
| 9136 |
(let loop2 ((j 1)) |
| 9137 |
(if (and (<= j nb-arg-regs) |
| 9138 |
(<= j i) |
| 9139 |
(<= j (- (- nb-parms nb-arg-regs) nb-stacked))) |
| 9140 |
(begin |
| 9141 |
(emit-move.l (reg-num->reg68 j) pdec-sp) |
| 9142 |
(loop2 (+ j 1))) |
| 9143 |
(let loop3 ((k j)) |
| 9144 |
(if (and (<= k nb-arg-regs) (<= k i)) |
| 9145 |
(begin |
| 9146 |
(emit-move.l |
| 9147 |
(reg-num->reg68 k) |
| 9148 |
(reg-num->reg68 (+ (- k j) 1))) |
| 9149 |
(loop3 (+ k 1))))))) |
| 9150 |
(if (> i min) |
| 9151 |
(emit-bra (vector-ref optional-lbls (- nb-parms i)))) |
| 9152 |
(loop1 (- i 1)))))) |
| 9153 |
(let loop ((i min)) |
| 9154 |
(if (<= i nb-parms) |
| 9155 |
(let ((val (if (= i nb-parms*) bits-null bits-unass))) |
| 9156 |
(emit-label (vector-ref optional-lbls (- nb-parms i))) |
| 9157 |
(cond ((> (- nb-parms i) nb-arg-regs) |
| 9158 |
(move-n-to-loc68 val pdec-sp)) |
| 9159 |
((< i nb-parms) |
| 9160 |
(move-n-to-loc68 |
| 9161 |
val |
| 9162 |
(reg-num->reg68 (parm->reg-num (+ i 1) nb-parms))))) |
| 9163 |
(loop (+ i 1))))))) |
| 9164 |
(define (encode-arg-count n) (cond ((= n 1) -1) ((= n 2) 0) (else (+ n 1)))) |
| 9165 |
(define (parm->reg-num i nb-parms) |
| 9166 |
(if (<= nb-parms nb-arg-regs) i (+ i (- nb-arg-regs nb-parms)))) |
| 9167 |
(define (no-arg-check-entry-offset proc nb-args) |
| 9168 |
(let ((x (proc-obj-call-pat proc))) |
| 9169 |
(if (and (pair? x) (null? (cdr x))) |
| 9170 |
(let ((arg-count (car x))) |
| 9171 |
(if (= arg-count nb-args) |
| 9172 |
(if (or (= arg-count 1) (= arg-count 2)) 10 14) |
| 9173 |
0)) |
| 9174 |
0))) |
| 9175 |
(define (gen-label-return lbl sn) |
| 9176 |
(if ofile-stats? |
| 9177 |
(begin (stat-clear!) (stat-add! '(gvm-instr label return) 1))) |
| 9178 |
(set! pointers-allocated 0) |
| 9179 |
(let ((slots (frame-slots exit-frame))) |
| 9180 |
(gen-label-return* |
| 9181 |
lbl |
| 9182 |
(add-first-class-label! instr-source slots exit-frame) |
| 9183 |
slots |
| 9184 |
0))) |
| 9185 |
(define (gen-label-return* lbl label-descr slots extra) |
| 9186 |
(let ((i (pos-in-list ret-var slots))) |
| 9187 |
(if i |
| 9188 |
(let* ((fs (length slots)) (link (- fs i))) |
| 9189 |
(emit-label-return lbl entry-lbl-num (+ fs extra) link label-descr)) |
| 9190 |
(compiler-internal-error |
| 9191 |
"gen-label-return*, no return address in frame")))) |
| 9192 |
(define (gen-label-task-entry lbl sn) |
| 9193 |
(if ofile-stats? |
| 9194 |
(begin (stat-clear!) (stat-add! '(gvm-instr label task-entry) 1))) |
| 9195 |
(set! pointers-allocated 0) |
| 9196 |
(emit-label lbl) |
| 9197 |
(if (= current-fs 0) |
| 9198 |
(begin |
| 9199 |
(emit-move.l (reg->reg68 return-reg) pdec-sp) |
| 9200 |
(emit-move.l sp-reg (make-pinc ltq-tail-reg))) |
| 9201 |
(begin |
| 9202 |
(emit-move.l sp-reg atemp1) |
| 9203 |
(emit-move.l (make-pinc atemp1) pdec-sp) |
| 9204 |
(let loop ((i (- current-fs 1))) |
| 9205 |
(if (> i 0) |
| 9206 |
(begin |
| 9207 |
(emit-move.l (make-pinc atemp1) (make-disp atemp1 -8)) |
| 9208 |
(loop (- i 1))))) |
| 9209 |
(emit-move.l (reg->reg68 return-reg) (make-pdec atemp1)) |
| 9210 |
(emit-move.l atemp1 (make-pinc ltq-tail-reg)))) |
| 9211 |
(emit-move.l ltq-tail-reg ltq-tail-slot)) |
| 9212 |
(define (gen-label-task-return lbl sn) |
| 9213 |
(if ofile-stats? |
| 9214 |
(begin (stat-clear!) (stat-add! '(gvm-instr label task-return) 1))) |
| 9215 |
(set! pointers-allocated 0) |
| 9216 |
(let ((slots (frame-slots exit-frame))) |
| 9217 |
(set! current-fs (+ current-fs 1)) |
| 9218 |
(let ((dummy-lbl (new-lbl!)) (skip-lbl (new-lbl!))) |
| 9219 |
(gen-label-return* |
| 9220 |
dummy-lbl |
| 9221 |
(add-first-class-label! instr-source slots exit-frame) |
| 9222 |
slots |
| 9223 |
1) |
| 9224 |
(emit-bra skip-lbl) |
| 9225 |
(gen-label-task-return* |
| 9226 |
lbl |
| 9227 |
(add-first-class-label! instr-source slots exit-frame) |
| 9228 |
slots |
| 9229 |
1) |
| 9230 |
(emit-subq.l pointer-size ltq-tail-reg) |
| 9231 |
(emit-label skip-lbl)))) |
| 9232 |
(define (gen-label-task-return* lbl label-descr slots extra) |
| 9233 |
(let ((i (pos-in-list ret-var slots))) |
| 9234 |
(if i |
| 9235 |
(let* ((fs (length slots)) (link (- fs i))) |
| 9236 |
(emit-label-task-return |
| 9237 |
lbl |
| 9238 |
entry-lbl-num |
| 9239 |
(+ fs extra) |
| 9240 |
link |
| 9241 |
label-descr)) |
| 9242 |
(compiler-internal-error |
| 9243 |
"gen-label-task-return*, no return address in frame")))) |
| 9244 |
(define (gen-apply prim opnds loc sn) |
| 9245 |
(if ofile-stats? |
| 9246 |
(begin |
| 9247 |
(stat-add! |
| 9248 |
(list 'gvm-instr |
| 9249 |
'apply |
| 9250 |
(string->canonical-symbol (proc-obj-name prim)) |
| 9251 |
(map opnd-stat opnds) |
| 9252 |
(if loc (opnd-stat loc) #f)) |
| 9253 |
1) |
| 9254 |
(for-each fetch-stat-add! opnds) |
| 9255 |
(if loc (store-stat-add! loc)))) |
| 9256 |
(let ((x (proc-obj-inlinable prim))) |
| 9257 |
(if (not x) |
| 9258 |
(compiler-internal-error "gen-APPLY, unknown 'prim':" prim) |
| 9259 |
(if (or (needed? loc sn) (car x)) ((cdr x) opnds loc sn))))) |
| 9260 |
(define (define-apply name side-effects? proc) |
| 9261 |
(let ((prim (get-prim-info name))) |
| 9262 |
(proc-obj-inlinable-set! prim (cons side-effects? proc)))) |
| 9263 |
(define (gen-copy opnd loc sn) |
| 9264 |
(if ofile-stats? |
| 9265 |
(begin |
| 9266 |
(stat-add! (list 'gvm-instr 'copy (opnd-stat opnd) (opnd-stat loc)) 1) |
| 9267 |
(fetch-stat-add! opnd) |
| 9268 |
(store-stat-add! loc))) |
| 9269 |
(if (needed? loc sn) (copy-opnd-to-loc opnd loc sn))) |
| 9270 |
(define (gen-close parms sn) |
| 9271 |
(define (size->bytes size) |
| 9272 |
(* (quotient |
| 9273 |
(+ (* (+ size 2) pointer-size) (- cache-line-length 1)) |
| 9274 |
cache-line-length) |
| 9275 |
cache-line-length)) |
| 9276 |
(define (parms->bytes parms) |
| 9277 |
(if (null? parms) |
| 9278 |
0 |
| 9279 |
(+ (size->bytes (length (closure-parms-opnds (car parms)))) |
| 9280 |
(parms->bytes (cdr parms))))) |
| 9281 |
(if ofile-stats? |
| 9282 |
(begin |
| 9283 |
(for-each |
| 9284 |
(lambda (x) |
| 9285 |
(stat-add! |
| 9286 |
(list 'gvm-instr |
| 9287 |
'close |
| 9288 |
(opnd-stat (closure-parms-loc x)) |
| 9289 |
(map opnd-stat (closure-parms-opnds x))) |
| 9290 |
1) |
| 9291 |
(store-stat-add! (closure-parms-loc x)) |
| 9292 |
(fetch-stat-add! (make-lbl (closure-parms-lbl x))) |
| 9293 |
(for-each fetch-stat-add! (closure-parms-opnds x))) |
| 9294 |
parms))) |
| 9295 |
(let ((total-space-needed (parms->bytes parms)) (lbl1 (new-lbl!))) |
| 9296 |
(emit-move.l closure-ptr-slot atemp2) |
| 9297 |
(move-n-to-loc68 total-space-needed dtemp1) |
| 9298 |
(emit-sub.l dtemp1 atemp2) |
| 9299 |
(emit-cmp.l closure-lim-slot atemp2) |
| 9300 |
(emit-bcc lbl1) |
| 9301 |
(gen-trap instr-source entry-frame #f #f closure-alloc-trap lbl1) |
| 9302 |
(emit-move.l atemp2 closure-ptr-slot) |
| 9303 |
(let* ((opnds* (apply append (map closure-parms-opnds parms))) |
| 9304 |
(sn* (sn-opnds opnds* sn))) |
| 9305 |
(let loop1 ((parms parms)) |
| 9306 |
(let ((loc (closure-parms-loc (car parms))) |
| 9307 |
(size (length (closure-parms-opnds (car parms)))) |
| 9308 |
(rest (cdr parms))) |
| 9309 |
(if (= size 1) |
| 9310 |
(emit-addq.l type-procedure atemp2) |
| 9311 |
(emit-move.w |
| 9312 |
(make-imm (+ 32768 (* (+ size 1) 4))) |
| 9313 |
(make-pinc atemp2))) |
| 9314 |
(move-opnd68-to-loc |
| 9315 |
atemp2 |
| 9316 |
loc |
| 9317 |
(sn-opnds (map closure-parms-loc rest) sn*)) |
| 9318 |
(if (null? rest) |
| 9319 |
(add-n-to-loc68 |
| 9320 |
(+ (- (size->bytes size) total-space-needed) 2) |
| 9321 |
atemp2) |
| 9322 |
(begin |
| 9323 |
(add-n-to-loc68 (- (size->bytes size) type-procedure) atemp2) |
| 9324 |
(loop1 rest))))) |
| 9325 |
(let loop2 ((parms parms)) |
| 9326 |
(let* ((opnds (closure-parms-opnds (car parms))) |
| 9327 |
(lbl (closure-parms-lbl (car parms))) |
| 9328 |
(size (length opnds)) |
| 9329 |
(rest (cdr parms))) |
| 9330 |
(emit-lea (make-pcr lbl 0) atemp1) |
| 9331 |
(emit-move.l atemp1 (make-pinc atemp2)) |
| 9332 |
(let loop3 ((opnds opnds)) |
| 9333 |
(if (not (null? opnds)) |
| 9334 |
(let ((sn** (sn-opnds |
| 9335 |
(apply append (map closure-parms-opnds rest)) |
| 9336 |
sn))) |
| 9337 |
(move-opnd-to-loc68 |
| 9338 |
(car opnds) |
| 9339 |
(make-pinc atemp2) |
| 9340 |
(sn-opnds (cdr opnds) sn**)) |
| 9341 |
(loop3 (cdr opnds))))) |
| 9342 |
(if (not (null? rest)) |
| 9343 |
(begin |
| 9344 |
(add-n-to-loc68 |
| 9345 |
(- (size->bytes size) (* (+ size 1) pointer-size)) |
| 9346 |
atemp2) |
| 9347 |
(loop2 rest)))))))) |
| 9348 |
(define (gen-ifjump test opnds true-lbl false-lbl poll? next-lbl) |
| 9349 |
(if ofile-stats? |
| 9350 |
(begin |
| 9351 |
(stat-add! |
| 9352 |
(list 'gvm-instr |
| 9353 |
'ifjump |
| 9354 |
(string->canonical-symbol (proc-obj-name test)) |
| 9355 |
(map opnd-stat opnds) |
| 9356 |
(if poll? 'poll 'not-poll)) |
| 9357 |
1) |
| 9358 |
(for-each fetch-stat-add! opnds) |
| 9359 |
(stat-dump!))) |
| 9360 |
(let ((proc (proc-obj-test test))) |
| 9361 |
(if proc |
| 9362 |
(gen-ifjump* proc opnds true-lbl false-lbl poll? next-lbl) |
| 9363 |
(compiler-internal-error "gen-IFJUMP, unknown 'test':" test)))) |
| 9364 |
(define (gen-ifjump* proc opnds true-lbl false-lbl poll? next-lbl) |
| 9365 |
(let ((fs (frame-size exit-frame))) |
| 9366 |
(define (double-branch) |
| 9367 |
(proc #t opnds false-lbl fs) |
| 9368 |
(if ofile-stats? |
| 9369 |
(emit-stat |
| 9370 |
'((gvm-instr.ifjump.fall-through 1) |
| 9371 |
(gvm-instr.ifjump.double-branch 1)))) |
| 9372 |
(emit-bra true-lbl) |
| 9373 |
(gen-deferred-code!)) |
| 9374 |
(gen-guarantee-fudge) |
| 9375 |
(if poll? (gen-poll)) |
| 9376 |
(if next-lbl |
| 9377 |
(cond ((= true-lbl next-lbl) |
| 9378 |
(proc #t opnds false-lbl fs) |
| 9379 |
(if ofile-stats? |
| 9380 |
(emit-stat '((gvm-instr.ifjump.fall-through 1))))) |
| 9381 |
((= false-lbl next-lbl) |
| 9382 |
(proc #f opnds true-lbl fs) |
| 9383 |
(if ofile-stats? |
| 9384 |
(emit-stat '((gvm-instr.ifjump.fall-through 1))))) |
| 9385 |
(else (double-branch))) |
| 9386 |
(double-branch)))) |
| 9387 |
(define (define-ifjump name proc) |
| 9388 |
(define-apply |
| 9389 |
name |
| 9390 |
#f |
| 9391 |
(lambda (opnds loc sn) |
| 9392 |
(let ((true-lbl (new-lbl!)) |
| 9393 |
(cont-lbl (new-lbl!)) |
| 9394 |
(reg68 (if (and (reg? loc) (not (eq? loc return-reg))) |
| 9395 |
(reg->reg68 loc) |
| 9396 |
dtemp1))) |
| 9397 |
(proc #f opnds true-lbl current-fs) |
| 9398 |
(move-n-to-loc68 bits-false reg68) |
| 9399 |
(emit-bra cont-lbl) |
| 9400 |
(emit-label true-lbl) |
| 9401 |
(move-n-to-loc68 bits-true reg68) |
| 9402 |
(emit-label cont-lbl) |
| 9403 |
(move-opnd68-to-loc reg68 loc sn)))) |
| 9404 |
(proc-obj-test-set! (get-prim-info name) proc)) |
| 9405 |
(define (gen-jump opnd nb-args poll? next-lbl) |
| 9406 |
(let ((fs (frame-size exit-frame))) |
| 9407 |
(if ofile-stats? |
| 9408 |
(begin |
| 9409 |
(stat-add! |
| 9410 |
(list 'gvm-instr |
| 9411 |
'jump |
| 9412 |
(opnd-stat opnd) |
| 9413 |
nb-args |
| 9414 |
(if poll? 'poll 'not-poll)) |
| 9415 |
1) |
| 9416 |
(jump-stat-add! opnd) |
| 9417 |
(if (and (lbl? opnd) next-lbl (= next-lbl (lbl-num opnd))) |
| 9418 |
(stat-add! '(gvm-instr.jump.fall-through) 1)) |
| 9419 |
(stat-dump!))) |
| 9420 |
(gen-guarantee-fudge) |
| 9421 |
(cond ((glo? opnd) |
| 9422 |
(if poll? (gen-poll)) |
| 9423 |
(setup-jump fs nb-args) |
| 9424 |
(emit-jmp-glob (make-glob (glo-name opnd))) |
| 9425 |
(gen-deferred-code!)) |
| 9426 |
((and (stk? opnd) (= (stk-num opnd) (+ fs 1)) (not nb-args)) |
| 9427 |
(if poll? (gen-poll)) |
| 9428 |
(setup-jump (+ fs 1) nb-args) |
| 9429 |
(emit-rts) |
| 9430 |
(gen-deferred-code!)) |
| 9431 |
((lbl? opnd) |
| 9432 |
(if (and poll? |
| 9433 |
(= fs current-fs) |
| 9434 |
(not nb-args) |
| 9435 |
(not (and next-lbl (= next-lbl (lbl-num opnd))))) |
| 9436 |
(gen-poll-branch (lbl-num opnd)) |
| 9437 |
(begin |
| 9438 |
(if poll? (gen-poll)) |
| 9439 |
(setup-jump fs nb-args) |
| 9440 |
(if (not (and next-lbl (= next-lbl (lbl-num opnd)))) |
| 9441 |
(emit-bra (lbl-num opnd)))))) |
| 9442 |
((obj? opnd) |
| 9443 |
(if poll? (gen-poll)) |
| 9444 |
(let ((val (obj-val opnd))) |
| 9445 |
(if (proc-obj? val) |
| 9446 |
(let ((num (add-object val)) |
| 9447 |
(offset (no-arg-check-entry-offset val nb-args))) |
| 9448 |
(setup-jump fs (if (<= offset 0) nb-args #f)) |
| 9449 |
(if num |
| 9450 |
(emit-jmp-proc num offset) |
| 9451 |
(emit-jmp-prim val offset)) |
| 9452 |
(gen-deferred-code!)) |
| 9453 |
(gen-jump* (opnd->opnd68 opnd #f fs) fs nb-args)))) |
| 9454 |
(else |
| 9455 |
(if poll? (gen-poll)) |
| 9456 |
(gen-jump* (opnd->opnd68 opnd #f fs) fs nb-args))))) |
| 9457 |
(define (gen-jump* opnd fs nb-args) |
| 9458 |
(if nb-args |
| 9459 |
(let ((lbl (new-lbl!))) |
| 9460 |
(make-top-of-frame-if-stk-opnd68 opnd fs) |
| 9461 |
(move-opnd68-to-loc68 (opnd68->true-opnd68 opnd fs) atemp1) |
| 9462 |
(shrink-frame fs) |
| 9463 |
(emit-move.l atemp1 dtemp1) |
| 9464 |
(emit-addq.w (modulo (- type-pair type-procedure) 8) dtemp1) |
| 9465 |
(emit-btst dtemp1 pair-reg) |
| 9466 |
(emit-beq lbl) |
| 9467 |
(move-n-to-loc68 (encode-arg-count nb-args) arg-count-reg) |
| 9468 |
(emit-trap3 non-proc-jump-trap) |
| 9469 |
(emit-label lbl) |
| 9470 |
(move-n-to-loc68 (encode-arg-count nb-args) arg-count-reg) |
| 9471 |
(emit-jmp (make-ind atemp1))) |
| 9472 |
(let ((areg (move-opnd68-to-any-areg opnd #f fs))) |
| 9473 |
(setup-jump fs nb-args) |
| 9474 |
(emit-jmp (make-ind areg)))) |
| 9475 |
(gen-deferred-code!)) |
| 9476 |
(define (setup-jump fs nb-args) |
| 9477 |
(shrink-frame fs) |
| 9478 |
(if nb-args (move-n-to-loc68 (encode-arg-count nb-args) arg-count-reg))) |
| 9479 |
(define (gen-poll) |
| 9480 |
(let ((lbl (new-lbl!))) |
| 9481 |
(emit-dbra poll-timer-reg lbl) |
| 9482 |
(emit-moveq (- polling-intermittency 1) poll-timer-reg) |
| 9483 |
(emit-cmp.l intr-flag-slot sp-reg) |
| 9484 |
(emit-bcc lbl) |
| 9485 |
(gen-trap instr-source entry-frame #f #f intr-trap lbl))) |
| 9486 |
(define (gen-poll-branch lbl) |
| 9487 |
(emit-dbra poll-timer-reg lbl) |
| 9488 |
(emit-moveq (- polling-intermittency 1) poll-timer-reg) |
| 9489 |
(emit-cmp.l intr-flag-slot sp-reg) |
| 9490 |
(emit-bcc lbl) |
| 9491 |
(gen-trap instr-source entry-frame #f #f intr-trap (new-lbl!)) |
| 9492 |
(emit-bra lbl)) |
| 9493 |
(define (make-gen-slot-ref slot type) |
| 9494 |
(lambda (opnds loc sn) |
| 9495 |
(let ((sn-loc (sn-opnd loc sn)) (opnd (car opnds))) |
| 9496 |
(move-opnd-to-loc68 opnd atemp1 sn-loc) |
| 9497 |
(move-opnd68-to-loc |
| 9498 |
(make-disp* atemp1 (- (* slot pointer-size) type)) |
| 9499 |
loc |
| 9500 |
sn)))) |
| 9501 |
(define (make-gen-slot-set! slot type) |
| 9502 |
(lambda (opnds loc sn) |
| 9503 |
(let ((sn-loc (if loc (sn-opnd loc sn) sn))) |
| 9504 |
(let* ((first-opnd (car opnds)) |
| 9505 |
(second-opnd (cadr opnds)) |
| 9506 |
(sn-second-opnd (sn-opnd second-opnd sn-loc))) |
| 9507 |
(move-opnd-to-loc68 first-opnd atemp1 sn-second-opnd) |
| 9508 |
(move-opnd-to-loc68 |
| 9509 |
second-opnd |
| 9510 |
(make-disp* atemp1 (- (* slot pointer-size) type)) |
| 9511 |
sn-loc) |
| 9512 |
(if loc |
| 9513 |
(if (not (eq? first-opnd loc)) |
| 9514 |
(move-opnd68-to-loc atemp1 loc sn))))))) |
| 9515 |
(define (gen-cons opnds loc sn) |
| 9516 |
(let ((sn-loc (sn-opnd loc sn))) |
| 9517 |
(let ((first-opnd (car opnds)) (second-opnd (cadr opnds))) |
| 9518 |
(gen-guarantee-space 2) |
| 9519 |
(if (contains-opnd? loc second-opnd) |
| 9520 |
(let ((sn-second-opnd (sn-opnd second-opnd sn-loc))) |
| 9521 |
(move-opnd-to-loc68 first-opnd (make-pdec heap-reg) sn-second-opnd) |
| 9522 |
(move-opnd68-to-loc68 heap-reg atemp2) |
| 9523 |
(move-opnd-to-loc68 second-opnd (make-pdec heap-reg) sn-loc) |
| 9524 |
(move-opnd68-to-loc atemp2 loc sn)) |
| 9525 |
(let* ((sn-second-opnd (sn-opnd second-opnd sn)) |
| 9526 |
(sn-loc (sn-opnd loc sn-second-opnd))) |
| 9527 |
(move-opnd-to-loc68 first-opnd (make-pdec heap-reg) sn-loc) |
| 9528 |
(move-opnd68-to-loc heap-reg loc sn-second-opnd) |
| 9529 |
(move-opnd-to-loc68 second-opnd (make-pdec heap-reg) sn)))))) |
| 9530 |
(define (make-gen-apply-c...r pattern) |
| 9531 |
(lambda (opnds loc sn) |
| 9532 |
(let ((sn-loc (sn-opnd loc sn)) (opnd (car opnds))) |
| 9533 |
(move-opnd-to-loc68 opnd atemp1 sn-loc) |
| 9534 |
(let loop ((pattern pattern)) |
| 9535 |
(if (<= pattern 3) |
| 9536 |
(if (= pattern 3) |
| 9537 |
(move-opnd68-to-loc (make-pdec atemp1) loc sn) |
| 9538 |
(move-opnd68-to-loc (make-ind atemp1) loc sn)) |
| 9539 |
(begin |
| 9540 |
(if (odd? pattern) |
| 9541 |
(emit-move.l (make-pdec atemp1) atemp1) |
| 9542 |
(emit-move.l (make-ind atemp1) atemp1)) |
| 9543 |
(loop (quotient pattern 2)))))))) |
| 9544 |
(define (gen-set-car! opnds loc sn) |
| 9545 |
(let ((sn-loc (if loc (sn-opnd loc sn) sn))) |
| 9546 |
(let* ((first-opnd (car opnds)) |
| 9547 |
(second-opnd (cadr opnds)) |
| 9548 |
(sn-second-opnd (sn-opnd second-opnd sn-loc))) |
| 9549 |
(move-opnd-to-loc68 first-opnd atemp1 sn-second-opnd) |
| 9550 |
(move-opnd-to-loc68 second-opnd (make-ind atemp1) sn-loc) |
| 9551 |
(if (and loc (not (eq? first-opnd loc))) |
| 9552 |
(move-opnd68-to-loc atemp1 loc sn))))) |
| 9553 |
(define (gen-set-cdr! opnds loc sn) |
| 9554 |
(let ((sn-loc (if loc (sn-opnd loc sn) sn))) |
| 9555 |
(let* ((first-opnd (car opnds)) |
| 9556 |
(second-opnd (cadr opnds)) |
| 9557 |
(sn-second-opnd (sn-opnd second-opnd sn-loc))) |
| 9558 |
(move-opnd-to-loc68 first-opnd atemp1 sn-second-opnd) |
| 9559 |
(if (and loc (not (eq? first-opnd loc))) |
| 9560 |
(move-opnd-to-loc68 |
| 9561 |
second-opnd |
| 9562 |
(make-disp atemp1 (- pointer-size)) |
| 9563 |
sn-loc) |
| 9564 |
(move-opnd-to-loc68 second-opnd (make-pdec atemp1) sn-loc)) |
| 9565 |
(if (and loc (not (eq? first-opnd loc))) |
| 9566 |
(move-opnd68-to-loc atemp1 loc sn))))) |
| 9567 |
(define (commut-oper gen opnds loc sn self? accum-self accum-other) |
| 9568 |
(if (null? opnds) |
| 9569 |
(gen (reverse accum-self) (reverse accum-other) loc sn self?) |
| 9570 |
(let ((opnd (car opnds)) (rest (cdr opnds))) |
| 9571 |
(cond ((and (not self?) (eq? opnd loc)) |
| 9572 |
(commut-oper gen rest loc sn #t accum-self accum-other)) |
| 9573 |
((contains-opnd? loc opnd) |
| 9574 |
(commut-oper |
| 9575 |
gen |
| 9576 |
rest |
| 9577 |
loc |
| 9578 |
sn |
| 9579 |
self? |
| 9580 |
(cons opnd accum-self) |
| 9581 |
accum-other)) |
| 9582 |
(else |
| 9583 |
(commut-oper |
| 9584 |
gen |
| 9585 |
rest |
| 9586 |
loc |
| 9587 |
sn |
| 9588 |
self? |
| 9589 |
accum-self |
| 9590 |
(cons opnd accum-other))))))) |
| 9591 |
(define (gen-add-in-place opnds loc68 sn) |
| 9592 |
(if (not (null? opnds)) |
| 9593 |
(let* ((first-opnd (car opnds)) |
| 9594 |
(other-opnds (cdr opnds)) |
| 9595 |
(sn-other-opnds (sn-opnds other-opnds sn)) |
| 9596 |
(sn-first-opnd (sn-opnd first-opnd sn-other-opnds)) |
| 9597 |
(opnd68 (opnd->opnd68 |
| 9598 |
first-opnd |
| 9599 |
(temp-in-opnd68 loc68) |
| 9600 |
(sn-opnd68 loc68 sn)))) |
| 9601 |
(make-top-of-frame-if-stk-opnds68 opnd68 loc68 sn-other-opnds) |
| 9602 |
(if (imm? opnd68) |
| 9603 |
(add-n-to-loc68 |
| 9604 |
(imm-val opnd68) |
| 9605 |
(opnd68->true-opnd68 loc68 sn-other-opnds)) |
| 9606 |
(let ((opnd68* (opnd68->true-opnd68 opnd68 sn-other-opnds))) |
| 9607 |
(if (or (dreg? opnd68) (reg68? loc68)) |
| 9608 |
(emit-add.l |
| 9609 |
opnd68* |
| 9610 |
(opnd68->true-opnd68 loc68 sn-other-opnds)) |
| 9611 |
(begin |
| 9612 |
(move-opnd68-to-loc68 opnd68* dtemp1) |
| 9613 |
(emit-add.l |
| 9614 |
dtemp1 |
| 9615 |
(opnd68->true-opnd68 loc68 sn-other-opnds)))))) |
| 9616 |
(gen-add-in-place other-opnds loc68 sn)))) |
| 9617 |
(define (gen-add self-opnds other-opnds loc sn self?) |
| 9618 |
(let* ((opnds (append self-opnds other-opnds)) |
| 9619 |
(first-opnd (car opnds)) |
| 9620 |
(other-opnds (cdr opnds)) |
| 9621 |
(sn-other-opnds (sn-opnds other-opnds sn)) |
| 9622 |
(sn-first-opnd (sn-opnd first-opnd sn-other-opnds))) |
| 9623 |
(if (<= (length self-opnds) 1) |
| 9624 |
(let ((loc68 (loc->loc68 loc #f sn-first-opnd))) |
| 9625 |
(if self? |
| 9626 |
(gen-add-in-place opnds loc68 sn) |
| 9627 |
(begin |
| 9628 |
(move-opnd-to-loc68 first-opnd loc68 sn-other-opnds) |
| 9629 |
(gen-add-in-place other-opnds loc68 sn)))) |
| 9630 |
(begin |
| 9631 |
(move-opnd-to-loc68 first-opnd dtemp1 (sn-opnd loc sn-other-opnds)) |
| 9632 |
(gen-add-in-place other-opnds dtemp1 (sn-opnd loc sn)) |
| 9633 |
(if self? |
| 9634 |
(let ((loc68 (loc->loc68 loc dtemp1 sn))) |
| 9635 |
(make-top-of-frame-if-stk-opnd68 loc68 sn) |
| 9636 |
(emit-add.l dtemp1 (opnd68->true-opnd68 loc68 sn))) |
| 9637 |
(move-opnd68-to-loc dtemp1 loc sn)))))) |
| 9638 |
(define (gen-sub-in-place opnds loc68 sn) |
| 9639 |
(if (not (null? opnds)) |
| 9640 |
(let* ((first-opnd (car opnds)) |
| 9641 |
(other-opnds (cdr opnds)) |
| 9642 |
(sn-other-opnds (sn-opnds other-opnds sn)) |
| 9643 |
(sn-first-opnd (sn-opnd first-opnd sn-other-opnds)) |
| 9644 |
(opnd68 (opnd->opnd68 |
| 9645 |
first-opnd |
| 9646 |
(temp-in-opnd68 loc68) |
| 9647 |
(sn-opnd68 loc68 sn)))) |
| 9648 |
(make-top-of-frame-if-stk-opnds68 opnd68 loc68 sn-other-opnds) |
| 9649 |
(if (imm? opnd68) |
| 9650 |
(add-n-to-loc68 |
| 9651 |
(- (imm-val opnd68)) |
| 9652 |
(opnd68->true-opnd68 loc68 sn-other-opnds)) |
| 9653 |
(let ((opnd68* (opnd68->true-opnd68 opnd68 sn-other-opnds))) |
| 9654 |
(if (or (dreg? opnd68) (reg68? loc68)) |
| 9655 |
(emit-sub.l |
| 9656 |
opnd68* |
| 9657 |
(opnd68->true-opnd68 loc68 sn-other-opnds)) |
| 9658 |
(begin |
| 9659 |
(move-opnd68-to-loc68 opnd68* dtemp1) |
| 9660 |
(emit-sub.l |
| 9661 |
dtemp1 |
| 9662 |
(opnd68->true-opnd68 loc68 sn-other-opnds)))))) |
| 9663 |
(gen-sub-in-place other-opnds loc68 sn)))) |
| 9664 |
(define (gen-sub first-opnd other-opnds loc sn self-opnds?) |
| 9665 |
(if (null? other-opnds) |
| 9666 |
(if (and (or (reg? loc) (stk? loc)) (not (eq? loc return-reg))) |
| 9667 |
(begin |
| 9668 |
(copy-opnd-to-loc first-opnd loc (sn-opnd loc sn)) |
| 9669 |
(let ((loc68 (loc->loc68 loc #f sn))) |
| 9670 |
(make-top-of-frame-if-stk-opnd68 loc68 sn) |
| 9671 |
(emit-neg.l (opnd68->true-opnd68 loc68 sn)))) |
| 9672 |
(begin |
| 9673 |
(move-opnd-to-loc68 first-opnd dtemp1 (sn-opnd loc sn)) |
| 9674 |
(emit-neg.l dtemp1) |
| 9675 |
(move-opnd68-to-loc dtemp1 loc sn))) |
| 9676 |
(let* ((sn-other-opnds (sn-opnds other-opnds sn)) |
| 9677 |
(sn-first-opnd (sn-opnd first-opnd sn-other-opnds))) |
| 9678 |
(if (and (not self-opnds?) (or (reg? loc) (stk? loc))) |
| 9679 |
(let ((loc68 (loc->loc68 loc #f sn-first-opnd))) |
| 9680 |
(if (not (eq? first-opnd loc)) |
| 9681 |
(move-opnd-to-loc68 first-opnd loc68 sn-other-opnds)) |
| 9682 |
(gen-sub-in-place other-opnds loc68 sn)) |
| 9683 |
(begin |
| 9684 |
(move-opnd-to-loc68 |
| 9685 |
first-opnd |
| 9686 |
dtemp1 |
| 9687 |
(sn-opnd loc sn-other-opnds)) |
| 9688 |
(gen-sub-in-place other-opnds dtemp1 (sn-opnd loc sn)) |
| 9689 |
(move-opnd68-to-loc dtemp1 loc sn)))))) |
| 9690 |
(define (gen-mul-in-place opnds reg68 sn) |
| 9691 |
(if (not (null? opnds)) |
| 9692 |
(let* ((first-opnd (car opnds)) |
| 9693 |
(other-opnds (cdr opnds)) |
| 9694 |
(sn-other-opnds (sn-opnds other-opnds sn)) |
| 9695 |
(opnd68 (opnd->opnd68 first-opnd (temp-in-opnd68 reg68) sn))) |
| 9696 |
(make-top-of-frame-if-stk-opnd68 opnd68 sn-other-opnds) |
| 9697 |
(if (imm? opnd68) |
| 9698 |
(mul-n-to-reg68 (quotient (imm-val opnd68) 8) reg68) |
| 9699 |
(begin |
| 9700 |
(emit-asr.l (make-imm 3) reg68) |
| 9701 |
(emit-muls.l (opnd68->true-opnd68 opnd68 sn-other-opnds) reg68))) |
| 9702 |
(gen-mul-in-place other-opnds reg68 sn)))) |
| 9703 |
(define (gen-mul self-opnds other-opnds loc sn self?) |
| 9704 |
(let* ((opnds (append self-opnds other-opnds)) |
| 9705 |
(first-opnd (car opnds)) |
| 9706 |
(other-opnds (cdr opnds)) |
| 9707 |
(sn-other-opnds (sn-opnds other-opnds sn)) |
| 9708 |
(sn-first-opnd (sn-opnd first-opnd sn-other-opnds))) |
| 9709 |
(if (null? self-opnds) |
| 9710 |
(let ((loc68 (loc->loc68 loc #f sn-first-opnd))) |
| 9711 |
(if self? |
| 9712 |
(gen-mul-in-place opnds loc68 sn) |
| 9713 |
(begin |
| 9714 |
(move-opnd-to-loc68 first-opnd loc68 sn-other-opnds) |
| 9715 |
(gen-mul-in-place other-opnds loc68 sn)))) |
| 9716 |
(begin |
| 9717 |
(move-opnd-to-loc68 first-opnd dtemp1 (sn-opnd loc sn-other-opnds)) |
| 9718 |
(gen-mul-in-place other-opnds dtemp1 (sn-opnd loc sn)) |
| 9719 |
(if self? |
| 9720 |
(let ((loc68 (loc->loc68 loc dtemp1 sn))) |
| 9721 |
(make-top-of-frame-if-stk-opnd68 loc68 sn) |
| 9722 |
(emit-asr.l (make-imm 3) dtemp1) |
| 9723 |
(emit-muls.l dtemp1 (opnd68->true-opnd68 loc68 sn))) |
| 9724 |
(move-opnd68-to-loc dtemp1 loc sn)))))) |
| 9725 |
(define (gen-div-in-place opnds reg68 sn) |
| 9726 |
(if (not (null? opnds)) |
| 9727 |
(let* ((first-opnd (car opnds)) |
| 9728 |
(other-opnds (cdr opnds)) |
| 9729 |
(sn-other-opnds (sn-opnds other-opnds sn)) |
| 9730 |
(sn-first-opnd (sn-opnd first-opnd sn-other-opnds)) |
| 9731 |
(opnd68 (opnd->opnd68 first-opnd (temp-in-opnd68 reg68) sn))) |
| 9732 |
(make-top-of-frame-if-stk-opnd68 opnd68 sn-other-opnds) |
| 9733 |
(if (imm? opnd68) |
| 9734 |
(let ((n (quotient (imm-val opnd68) 8))) |
| 9735 |
(div-n-to-reg68 n reg68) |
| 9736 |
(if (> (abs n) 1) (emit-and.w (make-imm -8) reg68))) |
| 9737 |
(let ((opnd68* (opnd68->true-opnd68 opnd68 sn-other-opnds))) |
| 9738 |
(emit-divsl.l opnd68* reg68 reg68) |
| 9739 |
(emit-asl.l (make-imm 3) reg68))) |
| 9740 |
(gen-div-in-place other-opnds reg68 sn)))) |
| 9741 |
(define (gen-div first-opnd other-opnds loc sn self-opnds?) |
| 9742 |
(if (null? other-opnds) |
| 9743 |
(begin |
| 9744 |
(move-opnd-to-loc68 first-opnd pdec-sp (sn-opnd loc sn)) |
| 9745 |
(emit-moveq 8 dtemp1) |
| 9746 |
(emit-divsl.l pinc-sp dtemp1 dtemp1) |
| 9747 |
(emit-asl.l (make-imm 3) dtemp1) |
| 9748 |
(emit-and.w (make-imm -8) dtemp1) |
| 9749 |
(move-opnd68-to-loc dtemp1 loc sn)) |
| 9750 |
(let* ((sn-other-opnds (sn-opnds other-opnds sn)) |
| 9751 |
(sn-first-opnd (sn-opnd first-opnd sn-other-opnds))) |
| 9752 |
(if (and (reg? loc) (not self-opnds?) (not (eq? loc return-reg))) |
| 9753 |
(let ((reg68 (reg->reg68 loc))) |
| 9754 |
(if (not (eq? first-opnd loc)) |
| 9755 |
(move-opnd-to-loc68 first-opnd reg68 sn-other-opnds)) |
| 9756 |
(gen-div-in-place other-opnds reg68 sn)) |
| 9757 |
(begin |
| 9758 |
(move-opnd-to-loc68 |
| 9759 |
first-opnd |
| 9760 |
dtemp1 |
| 9761 |
(sn-opnd loc sn-other-opnds)) |
| 9762 |
(gen-div-in-place other-opnds dtemp1 (sn-opnd loc sn)) |
| 9763 |
(move-opnd68-to-loc dtemp1 loc sn)))))) |
| 9764 |
(define (gen-rem first-opnd second-opnd loc sn) |
| 9765 |
(let* ((sn-loc (sn-opnd loc sn)) |
| 9766 |
(sn-second-opnd (sn-opnd second-opnd sn-loc))) |
| 9767 |
(move-opnd-to-loc68 first-opnd dtemp1 sn-second-opnd) |
| 9768 |
(let ((opnd68 (opnd->opnd68 second-opnd #f sn-loc)) |
| 9769 |
(reg68 (if (and (reg? loc) (not (eq? loc return-reg))) |
| 9770 |
(reg->reg68 loc) |
| 9771 |
false-reg))) |
| 9772 |
(make-top-of-frame-if-stk-opnd68 opnd68 sn-loc) |
| 9773 |
(let ((opnd68* (if (areg? opnd68) |
| 9774 |
(begin (emit-move.l opnd68 reg68) reg68) |
| 9775 |
(opnd68->true-opnd68 opnd68 sn-loc)))) |
| 9776 |
(emit-divsl.l opnd68* reg68 dtemp1)) |
| 9777 |
(move-opnd68-to-loc reg68 loc sn) |
| 9778 |
(if (not (and (reg? loc) (not (eq? loc return-reg)))) |
| 9779 |
(emit-move.l (make-imm bits-false) false-reg))))) |
| 9780 |
(define (gen-mod first-opnd second-opnd loc sn) |
| 9781 |
(let* ((sn-loc (sn-opnd loc sn)) |
| 9782 |
(sn-first-opnd (sn-opnd first-opnd sn-loc)) |
| 9783 |
(sn-second-opnd (sn-opnd second-opnd sn-first-opnd)) |
| 9784 |
(opnd68 (opnd->opnd68 second-opnd #f sn-second-opnd))) |
| 9785 |
(define (general-case) |
| 9786 |
(let ((lbl1 (new-lbl!)) |
| 9787 |
(lbl2 (new-lbl!)) |
| 9788 |
(lbl3 (new-lbl!)) |
| 9789 |
(opnd68** (opnd68->true-opnd68 opnd68 sn-second-opnd)) |
| 9790 |
(opnd68* (opnd68->true-opnd68 |
| 9791 |
(opnd->opnd68 first-opnd #f sn-second-opnd) |
| 9792 |
sn-second-opnd))) |
| 9793 |
(move-opnd68-to-loc68 opnd68* dtemp1) |
| 9794 |
(move-opnd68-to-loc68 opnd68** false-reg) |
| 9795 |
(emit-divsl.l false-reg false-reg dtemp1) |
| 9796 |
(emit-move.l false-reg false-reg) |
| 9797 |
(emit-beq lbl3) |
| 9798 |
(move-opnd68-to-loc68 opnd68* dtemp1) |
| 9799 |
(emit-bmi lbl1) |
| 9800 |
(move-opnd68-to-loc68 opnd68** dtemp1) |
| 9801 |
(emit-bpl lbl3) |
| 9802 |
(emit-bra lbl2) |
| 9803 |
(emit-label lbl1) |
| 9804 |
(move-opnd68-to-loc68 opnd68** dtemp1) |
| 9805 |
(emit-bmi lbl3) |
| 9806 |
(emit-label lbl2) |
| 9807 |
(emit-add.l dtemp1 false-reg) |
| 9808 |
(emit-label lbl3) |
| 9809 |
(move-opnd68-to-loc false-reg loc sn) |
| 9810 |
(emit-move.l (make-imm bits-false) false-reg))) |
| 9811 |
(make-top-of-frame-if-stk-opnd68 opnd68 sn-first-opnd) |
| 9812 |
(if (imm? opnd68) |
| 9813 |
(let ((n (quotient (imm-val opnd68) 8))) |
| 9814 |
(if (> n 0) |
| 9815 |
(let ((shift (power-of-2 n))) |
| 9816 |
(if shift |
| 9817 |
(let ((reg68 (if (and (reg? loc) |
| 9818 |
(not (eq? loc return-reg))) |
| 9819 |
(reg->reg68 loc) |
| 9820 |
dtemp1))) |
| 9821 |
(move-opnd-to-loc68 first-opnd reg68 sn-loc) |
| 9822 |
(emit-and.l (make-imm (* (- n 1) 8)) reg68) |
| 9823 |
(move-opnd68-to-loc reg68 loc sn)) |
| 9824 |
(general-case))) |
| 9825 |
(general-case))) |
| 9826 |
(general-case)))) |
| 9827 |
(define (gen-op emit-op dst-ok?) |
| 9828 |
(define (gen-op-in-place opnds loc68 sn) |
| 9829 |
(if (not (null? opnds)) |
| 9830 |
(let* ((first-opnd (car opnds)) |
| 9831 |
(other-opnds (cdr opnds)) |
| 9832 |
(sn-other-opnds (sn-opnds other-opnds sn)) |
| 9833 |
(sn-first-opnd (sn-opnd first-opnd sn-other-opnds)) |
| 9834 |
(opnd68 (opnd->opnd68 |
| 9835 |
first-opnd |
| 9836 |
(temp-in-opnd68 loc68) |
| 9837 |
(sn-opnd68 loc68 sn)))) |
| 9838 |
(make-top-of-frame-if-stk-opnds68 opnd68 loc68 sn-other-opnds) |
| 9839 |
(if (imm? opnd68) |
| 9840 |
(emit-op opnd68 (opnd68->true-opnd68 loc68 sn-other-opnds)) |
| 9841 |
(let ((opnd68* (opnd68->true-opnd68 opnd68 sn-other-opnds))) |
| 9842 |
(if (or (dreg? opnd68) (dst-ok? loc68)) |
| 9843 |
(emit-op opnd68* |
| 9844 |
(opnd68->true-opnd68 loc68 sn-other-opnds)) |
| 9845 |
(begin |
| 9846 |
(move-opnd68-to-loc68 opnd68* dtemp1) |
| 9847 |
(emit-op dtemp1 |
| 9848 |
(opnd68->true-opnd68 loc68 sn-other-opnds)))))) |
| 9849 |
(gen-op-in-place other-opnds loc68 sn)))) |
| 9850 |
(lambda (self-opnds other-opnds loc sn self?) |
| 9851 |
(let* ((opnds (append self-opnds other-opnds)) |
| 9852 |
(first-opnd (car opnds)) |
| 9853 |
(other-opnds (cdr opnds)) |
| 9854 |
(sn-other-opnds (sn-opnds other-opnds sn)) |
| 9855 |
(sn-first-opnd (sn-opnd first-opnd sn-other-opnds))) |
| 9856 |
(if (<= (length self-opnds) 1) |
| 9857 |
(let ((loc68 (loc->loc68 loc #f sn-first-opnd))) |
| 9858 |
(if self? |
| 9859 |
(gen-op-in-place opnds loc68 sn) |
| 9860 |
(begin |
| 9861 |
(move-opnd-to-loc68 first-opnd loc68 sn-other-opnds) |
| 9862 |
(gen-op-in-place other-opnds loc68 sn)))) |
| 9863 |
(begin |
| 9864 |
(move-opnd-to-loc68 first-opnd dtemp1 (sn-opnd loc sn-other-opnds)) |
| 9865 |
(gen-op-in-place other-opnds dtemp1 (sn-opnd loc sn)) |
| 9866 |
(if self? |
| 9867 |
(let ((loc68 (loc->loc68 loc dtemp1 sn))) |
| 9868 |
(make-top-of-frame-if-stk-opnd68 loc68 sn) |
| 9869 |
(emit-op dtemp1 (opnd68->true-opnd68 loc68 sn))) |
| 9870 |
(move-opnd68-to-loc dtemp1 loc sn))))))) |
| 9871 |
(define gen-logior (gen-op emit-or.l dreg?)) |
| 9872 |
(define gen-logxor (gen-op emit-eor.l (lambda (x) #f))) |
| 9873 |
(define gen-logand (gen-op emit-and.l dreg?)) |
| 9874 |
(define (gen-shift right-shift) |
| 9875 |
(lambda (opnds loc sn) |
| 9876 |
(let ((sn-loc (sn-opnd loc sn))) |
| 9877 |
(let* ((opnd1 (car opnds)) |
| 9878 |
(opnd2 (cadr opnds)) |
| 9879 |
(sn-opnd1 (sn-opnd opnd1 sn-loc)) |
| 9880 |
(o2 (opnd->opnd68 opnd2 #f sn-opnd1))) |
| 9881 |
(make-top-of-frame-if-stk-opnd68 o2 sn-opnd1) |
| 9882 |
(if (imm? o2) |
| 9883 |
(let* ((reg68 (if (and (reg? loc) (not (eq? loc return-reg))) |
| 9884 |
(reg->reg68 loc) |
| 9885 |
dtemp1)) |
| 9886 |
(n (quotient (imm-val o2) 8)) |
| 9887 |
(emit-shft (if (> n 0) emit-lsl.l right-shift))) |
| 9888 |
(move-opnd-to-loc68 opnd1 reg68 sn-loc) |
| 9889 |
(let loop ((i (min (abs n) 29))) |
| 9890 |
(if (> i 0) |
| 9891 |
(begin |
| 9892 |
(emit-shft (make-imm (min i 8)) reg68) |
| 9893 |
(loop (- i 8))))) |
| 9894 |
(if (< n 0) (emit-and.w (make-imm -8) reg68)) |
| 9895 |
(move-opnd68-to-loc reg68 loc sn)) |
| 9896 |
(let* ((reg68 (if (and (reg? loc) (not (eq? loc return-reg))) |
| 9897 |
(reg->reg68 loc) |
| 9898 |
dtemp1)) |
| 9899 |
(reg68* (if (and (reg? loc) (not (eq? loc return-reg))) |
| 9900 |
dtemp1 |
| 9901 |
false-reg)) |
| 9902 |
(lbl1 (new-lbl!)) |
| 9903 |
(lbl2 (new-lbl!))) |
| 9904 |
(emit-move.l (opnd68->true-opnd68 o2 sn-opnd1) reg68*) |
| 9905 |
(move-opnd-to-loc68 opnd1 reg68 sn-loc) |
| 9906 |
(emit-asr.l (make-imm 3) reg68*) |
| 9907 |
(emit-bmi lbl1) |
| 9908 |
(emit-lsl.l reg68* reg68) |
| 9909 |
(emit-bra lbl2) |
| 9910 |
(emit-label lbl1) |
| 9911 |
(emit-neg.l reg68*) |
| 9912 |
(right-shift reg68* reg68) |
| 9913 |
(emit-and.w (make-imm -8) reg68) |
| 9914 |
(emit-label lbl2) |
| 9915 |
(move-opnd68-to-loc reg68 loc sn) |
| 9916 |
(if (not (and (reg? loc) (not (eq? loc return-reg)))) |
| 9917 |
(emit-move.l (make-imm bits-false) false-reg)))))))) |
| 9918 |
(define (flo-oper oper1 oper2 opnds loc sn) |
| 9919 |
(gen-guarantee-space 2) |
| 9920 |
(move-opnd-to-loc68 |
| 9921 |
(car opnds) |
| 9922 |
atemp1 |
| 9923 |
(sn-opnds (cdr opnds) (sn-opnd loc sn))) |
| 9924 |
(oper1 (make-disp* atemp1 (- type-flonum)) ftemp1) |
| 9925 |
(let loop ((opnds (cdr opnds))) |
| 9926 |
(if (not (null? opnds)) |
| 9927 |
(let* ((opnd (car opnds)) |
| 9928 |
(other-opnds (cdr opnds)) |
| 9929 |
(sn-other-opnds (sn-opnds other-opnds sn))) |
| 9930 |
(move-opnd-to-loc68 opnd atemp1 sn-other-opnds) |
| 9931 |
(oper2 (make-disp* atemp1 (- type-flonum)) ftemp1) |
| 9932 |
(loop (cdr opnds))))) |
| 9933 |
(add-n-to-loc68 (* -2 pointer-size) heap-reg) |
| 9934 |
(emit-fmov.dx ftemp1 (make-ind heap-reg)) |
| 9935 |
(let ((reg68 (if (reg? loc) (reg->reg68 loc) atemp1))) |
| 9936 |
(emit-move.l heap-reg reg68) |
| 9937 |
(emit-addq.l type-flonum reg68)) |
| 9938 |
(if (not (reg? loc)) (move-opnd68-to-loc atemp1 loc sn))) |
| 9939 |
(define (gen-make-placeholder opnds loc sn) |
| 9940 |
(let ((sn-loc (sn-opnd loc sn))) |
| 9941 |
(let ((opnd (car opnds))) |
| 9942 |
(gen-guarantee-space 4) |
| 9943 |
(emit-clr.l (make-pdec heap-reg)) |
| 9944 |
(move-opnd-to-loc68 opnd (make-pdec heap-reg) sn-loc) |
| 9945 |
(emit-move.l null-reg (make-pdec heap-reg)) |
| 9946 |
(move-opnd68-to-loc68 heap-reg atemp2) |
| 9947 |
(emit-addq.l (modulo (- type-placeholder type-pair) 8) atemp2) |
| 9948 |
(emit-move.l atemp2 (make-pdec heap-reg)) |
| 9949 |
(move-opnd68-to-loc atemp2 loc sn)))) |
| 9950 |
(define (gen-subprocedure-id opnds loc sn) |
| 9951 |
(let ((sn-loc (sn-opnd loc sn)) |
| 9952 |
(opnd (car opnds)) |
| 9953 |
(reg68 (if (and (reg? loc) (not (eq? loc return-reg))) |
| 9954 |
(reg->reg68 loc) |
| 9955 |
dtemp1))) |
| 9956 |
(move-opnd-to-loc68 opnd atemp1 sn-loc) |
| 9957 |
(move-n-to-loc68 32768 reg68) |
| 9958 |
(emit-sub.w (make-disp* atemp1 -2) reg68) |
| 9959 |
(move-opnd68-to-loc reg68 loc sn))) |
| 9960 |
(define (gen-subprocedure-parent opnds loc sn) |
| 9961 |
(let ((sn-loc (sn-opnd loc sn)) (opnd (car opnds))) |
| 9962 |
(move-opnd-to-loc68 opnd atemp1 sn-loc) |
| 9963 |
(emit-add.w (make-disp* atemp1 -2) atemp1) |
| 9964 |
(add-n-to-loc68 -32768 atemp1) |
| 9965 |
(move-opnd68-to-loc atemp1 loc sn))) |
| 9966 |
(define (gen-return-fs opnds loc sn) |
| 9967 |
(let ((sn-loc (sn-opnd loc sn)) |
| 9968 |
(opnd (car opnds)) |
| 9969 |
(reg68 (if (and (reg? loc) (not (eq? loc return-reg))) |
| 9970 |
(reg->reg68 loc) |
| 9971 |
dtemp1)) |
| 9972 |
(lbl (new-lbl!))) |
| 9973 |
(move-opnd-to-loc68 opnd atemp1 sn-loc) |
| 9974 |
(emit-moveq 0 reg68) |
| 9975 |
(emit-move.w (make-disp* atemp1 -6) reg68) |
| 9976 |
(emit-beq lbl) |
| 9977 |
(emit-and.w (make-imm 32767) reg68) |
| 9978 |
(emit-subq.l 8 reg68) |
| 9979 |
(emit-label lbl) |
| 9980 |
(emit-addq.l 8 reg68) |
| 9981 |
(emit-asl.l (make-imm 1) reg68) |
| 9982 |
(move-opnd68-to-loc reg68 loc sn))) |
| 9983 |
(define (gen-return-link opnds loc sn) |
| 9984 |
(let ((sn-loc (sn-opnd loc sn)) |
| 9985 |
(opnd (car opnds)) |
| 9986 |
(reg68 (if (and (reg? loc) (not (eq? loc return-reg))) |
| 9987 |
(reg->reg68 loc) |
| 9988 |
dtemp1)) |
| 9989 |
(lbl (new-lbl!))) |
| 9990 |
(move-opnd-to-loc68 opnd atemp1 sn-loc) |
| 9991 |
(emit-moveq 0 reg68) |
| 9992 |
(emit-move.w (make-disp* atemp1 -6) reg68) |
| 9993 |
(emit-beq lbl) |
| 9994 |
(emit-and.w (make-imm 32767) reg68) |
| 9995 |
(emit-subq.l 8 reg68) |
| 9996 |
(emit-label lbl) |
| 9997 |
(emit-addq.l 8 reg68) |
| 9998 |
(emit-sub.w (make-disp* atemp1 -4) reg68) |
| 9999 |
(emit-asl.l (make-imm 1) reg68) |
| 10000 |
(move-opnd68-to-loc reg68 loc sn))) |
| 10001 |
(define (gen-procedure-info opnds loc sn) |
| 10002 |
(let ((sn-loc (sn-opnd loc sn)) (opnd (car opnds))) |
| 10003 |
(move-opnd-to-loc68 opnd atemp1 sn-loc) |
| 10004 |
(emit-add.w (make-disp* atemp1 -2) atemp1) |
| 10005 |
(move-opnd68-to-loc (make-disp* atemp1 (- 32768 6)) loc sn))) |
| 10006 |
(define (gen-guarantee-space n) |
| 10007 |
(set! pointers-allocated (+ pointers-allocated n)) |
| 10008 |
(if (> pointers-allocated heap-allocation-fudge) |
| 10009 |
(begin (gen-guarantee-fudge) (set! pointers-allocated n)))) |
| 10010 |
(define (gen-guarantee-fudge) |
| 10011 |
(if (> pointers-allocated 0) |
| 10012 |
(let ((lbl (new-lbl!))) |
| 10013 |
(emit-cmp.l heap-lim-slot heap-reg) |
| 10014 |
(emit-bcc lbl) |
| 10015 |
(gen-trap instr-source entry-frame #f #f heap-alloc1-trap lbl) |
| 10016 |
(set! pointers-allocated 0)))) |
| 10017 |
(define pointers-allocated '()) |
| 10018 |
(define (gen-type opnds loc sn) |
| 10019 |
(let* ((sn-loc (sn-opnd loc sn)) |
| 10020 |
(opnd (car opnds)) |
| 10021 |
(reg68 (if (and (reg? loc) (not (eq? loc return-reg))) |
| 10022 |
(reg->reg68 loc) |
| 10023 |
dtemp1))) |
| 10024 |
(move-opnd-to-loc68 opnd reg68 sn-loc) |
| 10025 |
(emit-and.l (make-imm 7) reg68) |
| 10026 |
(emit-asl.l (make-imm 3) reg68) |
| 10027 |
(move-opnd68-to-loc reg68 loc sn))) |
| 10028 |
(define (gen-type-cast opnds loc sn) |
| 10029 |
(let ((sn-loc (if loc (sn-opnd loc sn) sn))) |
| 10030 |
(let ((first-opnd (car opnds)) (second-opnd (cadr opnds))) |
| 10031 |
(let* ((sn-loc (if (and loc (not (eq? first-opnd loc))) sn-loc sn)) |
| 10032 |
(o1 (opnd->opnd68 first-opnd #f (sn-opnd second-opnd sn-loc))) |
| 10033 |
(o2 (opnd->opnd68 second-opnd (temp-in-opnd68 o1) sn-loc)) |
| 10034 |
(reg68 (if (and (reg? loc) (not (eq? loc return-reg))) |
| 10035 |
(reg->reg68 loc) |
| 10036 |
dtemp1))) |
| 10037 |
(make-top-of-frame-if-stk-opnds68 o1 o2 sn-loc) |
| 10038 |
(move-opnd68-to-loc68 |
| 10039 |
(opnd68->true-opnd68 o1 (sn-opnd68 o2 sn-loc)) |
| 10040 |
reg68) |
| 10041 |
(emit-and.w (make-imm -8) reg68) |
| 10042 |
(if (imm? o2) |
| 10043 |
(let ((n (quotient (imm-val o2) 8))) |
| 10044 |
(if (> n 0) (emit-addq.w n reg68))) |
| 10045 |
(begin |
| 10046 |
(move-opnd68-to-loc68 (opnd68->true-opnd68 o2 sn-loc) atemp1) |
| 10047 |
(emit-exg atemp1 reg68) |
| 10048 |
(emit-asr.l (make-imm 3) reg68) |
| 10049 |
(emit-add.l atemp1 reg68))) |
| 10050 |
(move-opnd68-to-loc reg68 loc sn))))) |
| 10051 |
(define (gen-subtype opnds loc sn) |
| 10052 |
(let ((sn-loc (sn-opnd loc sn)) |
| 10053 |
(opnd (car opnds)) |
| 10054 |
(reg68 (if (and (reg? loc) (not (eq? loc return-reg))) |
| 10055 |
(reg->reg68 loc) |
| 10056 |
dtemp1))) |
| 10057 |
(move-opnd-to-loc68 opnd atemp1 sn-loc) |
| 10058 |
(emit-moveq 0 reg68) |
| 10059 |
(emit-move.b (make-ind atemp1) reg68) |
| 10060 |
(move-opnd68-to-loc reg68 loc sn))) |
| 10061 |
(define (gen-subtype-set! opnds loc sn) |
| 10062 |
(let ((sn-loc (if loc (sn-opnd loc sn) sn))) |
| 10063 |
(let ((first-opnd (car opnds)) (second-opnd (cadr opnds))) |
| 10064 |
(let* ((sn-loc (if (and loc (not (eq? first-opnd loc))) sn-loc sn)) |
| 10065 |
(o1 (opnd->opnd68 first-opnd #f (sn-opnd second-opnd sn-loc))) |
| 10066 |
(o2 (opnd->opnd68 second-opnd (temp-in-opnd68 o1) sn-loc))) |
| 10067 |
(make-top-of-frame-if-stk-opnds68 o1 o2 sn-loc) |
| 10068 |
(move-opnd68-to-loc68 |
| 10069 |
(opnd68->true-opnd68 o1 (sn-opnd68 o2 sn-loc)) |
| 10070 |
atemp1) |
| 10071 |
(if (imm? o2) |
| 10072 |
(emit-move.b (make-imm (imm-val o2)) (make-ind atemp1)) |
| 10073 |
(begin |
| 10074 |
(move-opnd68-to-loc68 (opnd68->true-opnd68 o2 sn-loc) dtemp1) |
| 10075 |
(emit-move.b dtemp1 (make-ind atemp1)))) |
| 10076 |
(if (and loc (not (eq? first-opnd loc))) |
| 10077 |
(move-opnd68-to-loc atemp1 loc sn)))))) |
| 10078 |
(define (vector-select kind vector string vector8 vector16) |
| 10079 |
(case kind |
| 10080 |
((string) string) |
| 10081 |
((vector8) vector8) |
| 10082 |
((vector16) vector16) |
| 10083 |
(else vector))) |
| 10084 |
(define (obj-vector? kind) (vector-select kind #t #f #f #f)) |
| 10085 |
(define (make-gen-vector kind) |
| 10086 |
(lambda (opnds loc sn) |
| 10087 |
(let ((sn-loc (if loc (sn-opnd loc sn) sn))) |
| 10088 |
(let* ((n (length opnds)) |
| 10089 |
(bytes (+ pointer-size |
| 10090 |
(* (vector-select kind 4 1 1 2) |
| 10091 |
(+ n (if (eq? kind 'string) 1 0))))) |
| 10092 |
(adjust (modulo (- bytes) 8))) |
| 10093 |
(gen-guarantee-space |
| 10094 |
(quotient (* (quotient (+ bytes (- 8 1)) 8) 8) pointer-size)) |
| 10095 |
(if (not (= adjust 0)) (emit-subq.l adjust heap-reg)) |
| 10096 |
(if (eq? kind 'string) (emit-move.b (make-imm 0) (make-pdec heap-reg))) |
| 10097 |
(let loop ((opnds (reverse opnds))) |
| 10098 |
(if (pair? opnds) |
| 10099 |
(let* ((o (car opnds)) (sn-o (sn-opnds (cdr opnds) sn-loc))) |
| 10100 |
(if (eq? kind 'vector) |
| 10101 |
(move-opnd-to-loc68 o (make-pdec heap-reg) sn-o) |
| 10102 |
(begin |
| 10103 |
(move-opnd-to-loc68 o dtemp1 sn-o) |
| 10104 |
(emit-asr.l (make-imm 3) dtemp1) |
| 10105 |
(if (eq? kind 'vector16) |
| 10106 |
(emit-move.w dtemp1 (make-pdec heap-reg)) |
| 10107 |
(emit-move.b dtemp1 (make-pdec heap-reg))))) |
| 10108 |
(loop (cdr opnds))))) |
| 10109 |
(emit-move.l |
| 10110 |
(make-imm |
| 10111 |
(+ (* 256 (- bytes pointer-size)) |
| 10112 |
(* 8 (if (eq? kind 'vector) subtype-vector subtype-string)))) |
| 10113 |
(make-pdec heap-reg)) |
| 10114 |
(if loc |
| 10115 |
(begin |
| 10116 |
(emit-lea (make-disp* heap-reg type-subtyped) atemp2) |
| 10117 |
(move-opnd68-to-loc atemp2 loc sn))))))) |
| 10118 |
(define (make-gen-vector-length kind) |
| 10119 |
(lambda (opnds loc sn) |
| 10120 |
(let ((sn-loc (sn-opnd loc sn)) |
| 10121 |
(opnd (car opnds)) |
| 10122 |
(reg68 (if (and (reg? loc) (not (eq? loc return-reg))) |
| 10123 |
(reg->reg68 loc) |
| 10124 |
dtemp1))) |
| 10125 |
(move-opnd-to-loc68 opnd atemp1 sn-loc) |
| 10126 |
(move-opnd68-to-loc68 (make-disp* atemp1 (- type-subtyped)) reg68) |
| 10127 |
(emit-lsr.l (make-imm (vector-select kind 7 5 5 6)) reg68) |
| 10128 |
(if (not (eq? kind 'vector)) |
| 10129 |
(begin |
| 10130 |
(emit-and.w (make-imm -8) reg68) |
| 10131 |
(if (eq? kind 'string) (emit-subq.l 8 reg68)))) |
| 10132 |
(move-opnd68-to-loc reg68 loc sn)))) |
| 10133 |
(define (make-gen-vector-ref kind) |
| 10134 |
(lambda (opnds loc sn) |
| 10135 |
(let ((sn-loc (sn-opnd loc sn))) |
| 10136 |
(let ((first-opnd (car opnds)) |
| 10137 |
(second-opnd (cadr opnds)) |
| 10138 |
(reg68 (if (and (reg? loc) (not (eq? loc return-reg))) |
| 10139 |
(reg->reg68 loc) |
| 10140 |
dtemp1))) |
| 10141 |
(let* ((o2 (opnd->opnd68 second-opnd #f (sn-opnd first-opnd sn-loc))) |
| 10142 |
(o1 (opnd->opnd68 first-opnd (temp-in-opnd68 o2) sn-loc))) |
| 10143 |
(make-top-of-frame-if-stk-opnds68 o1 o2 sn-loc) |
| 10144 |
(let* ((offset (if (eq? kind 'closure) |
| 10145 |
(- pointer-size type-procedure) |
| 10146 |
(- pointer-size type-subtyped))) |
| 10147 |
(loc68 (if (imm? o2) |
| 10148 |
(begin |
| 10149 |
(move-opnd68-to-loc68 |
| 10150 |
(opnd68->true-opnd68 o1 sn-loc) |
| 10151 |
atemp1) |
| 10152 |
(make-disp* |
| 10153 |
atemp1 |
| 10154 |
(+ (quotient |
| 10155 |
(imm-val o2) |
| 10156 |
(vector-select kind 2 8 8 4)) |
| 10157 |
offset))) |
| 10158 |
(begin |
| 10159 |
(move-opnd68-to-loc68 |
| 10160 |
(opnd68->true-opnd68 o2 (sn-opnd68 o1 sn-loc)) |
| 10161 |
dtemp1) |
| 10162 |
(emit-asr.l |
| 10163 |
(make-imm (vector-select kind 1 3 3 2)) |
| 10164 |
dtemp1) |
| 10165 |
(move-opnd68-to-loc68 |
| 10166 |
(opnd68->true-opnd68 o1 sn-loc) |
| 10167 |
atemp1) |
| 10168 |
(if (and (identical-opnd68? reg68 dtemp1) |
| 10169 |
(not (obj-vector? kind))) |
| 10170 |
(begin |
| 10171 |
(emit-move.l dtemp1 atemp2) |
| 10172 |
(make-inx atemp1 atemp2 offset)) |
| 10173 |
(make-inx atemp1 dtemp1 offset)))))) |
| 10174 |
(if (not (obj-vector? kind)) (emit-moveq 0 reg68)) |
| 10175 |
(case kind |
| 10176 |
((string vector8) (emit-move.b loc68 reg68)) |
| 10177 |
((vector16) (emit-move.w loc68 reg68)) |
| 10178 |
(else (emit-move.l loc68 reg68))) |
| 10179 |
(if (not (obj-vector? kind)) |
| 10180 |
(begin |
| 10181 |
(emit-asl.l (make-imm 3) reg68) |
| 10182 |
(if (eq? kind 'string) (emit-addq.w type-special reg68)))) |
| 10183 |
(move-opnd68-to-loc reg68 loc sn))))))) |
| 10184 |
(define (make-gen-vector-set! kind) |
| 10185 |
(lambda (opnds loc sn) |
| 10186 |
(let ((sn-loc (if loc (sn-opnd loc sn) sn))) |
| 10187 |
(let ((first-opnd (car opnds)) |
| 10188 |
(second-opnd (cadr opnds)) |
| 10189 |
(third-opnd (caddr opnds))) |
| 10190 |
(let* ((sn-loc (if (and loc (not (eq? first-opnd loc))) |
| 10191 |
(sn-opnd first-opnd sn-loc) |
| 10192 |
sn)) |
| 10193 |
(sn-third-opnd (sn-opnd third-opnd sn-loc)) |
| 10194 |
(o2 (opnd->opnd68 |
| 10195 |
second-opnd |
| 10196 |
#f |
| 10197 |
(sn-opnd first-opnd sn-third-opnd))) |
| 10198 |
(o1 (opnd->opnd68 |
| 10199 |
first-opnd |
| 10200 |
(temp-in-opnd68 o2) |
| 10201 |
sn-third-opnd))) |
| 10202 |
(make-top-of-frame-if-stk-opnds68 o1 o2 sn-third-opnd) |
| 10203 |
(let* ((offset (if (eq? kind 'closure) |
| 10204 |
(- pointer-size type-procedure) |
| 10205 |
(- pointer-size type-subtyped))) |
| 10206 |
(loc68 (if (imm? o2) |
| 10207 |
(begin |
| 10208 |
(move-opnd68-to-loc68 |
| 10209 |
(opnd68->true-opnd68 o1 sn-third-opnd) |
| 10210 |
atemp1) |
| 10211 |
(make-disp* |
| 10212 |
atemp1 |
| 10213 |
(+ (quotient |
| 10214 |
(imm-val o2) |
| 10215 |
(vector-select kind 2 8 8 4)) |
| 10216 |
offset))) |
| 10217 |
(begin |
| 10218 |
(move-opnd68-to-loc68 |
| 10219 |
(opnd68->true-opnd68 o2 (sn-opnd68 o1 sn-loc)) |
| 10220 |
dtemp1) |
| 10221 |
(emit-asr.l |
| 10222 |
(make-imm (vector-select kind 1 3 3 2)) |
| 10223 |
dtemp1) |
| 10224 |
(move-opnd68-to-loc68 |
| 10225 |
(opnd68->true-opnd68 o1 sn-loc) |
| 10226 |
atemp1) |
| 10227 |
(if (obj-vector? kind) |
| 10228 |
(make-inx atemp1 dtemp1 offset) |
| 10229 |
(begin |
| 10230 |
(emit-move.l dtemp1 atemp2) |
| 10231 |
(make-inx atemp1 atemp2 offset))))))) |
| 10232 |
(if (obj-vector? kind) |
| 10233 |
(move-opnd-to-loc68 third-opnd loc68 sn-loc) |
| 10234 |
(begin |
| 10235 |
(move-opnd-to-loc68 third-opnd dtemp1 sn-loc) |
| 10236 |
(emit-asr.l (make-imm 3) dtemp1) |
| 10237 |
(if (eq? kind 'vector16) |
| 10238 |
(emit-move.w dtemp1 loc68) |
| 10239 |
(emit-move.b dtemp1 loc68)))) |
| 10240 |
(if (and loc (not (eq? first-opnd loc))) |
| 10241 |
(copy-opnd-to-loc first-opnd loc sn)))))))) |
| 10242 |
(define (make-gen-vector-shrink! kind) |
| 10243 |
(lambda (opnds loc sn) |
| 10244 |
(let ((sn-loc (if loc (sn-opnd loc sn) sn))) |
| 10245 |
(let ((first-opnd (car opnds)) (second-opnd (cadr opnds))) |
| 10246 |
(let* ((sn-loc (if (and loc (not (eq? first-opnd loc))) |
| 10247 |
(sn-opnd first-opnd sn-loc) |
| 10248 |
sn)) |
| 10249 |
(o2 (opnd->opnd68 second-opnd #f (sn-opnd first-opnd sn-loc))) |
| 10250 |
(o1 (opnd->opnd68 first-opnd (temp-in-opnd68 o2) sn-loc))) |
| 10251 |
(make-top-of-frame-if-stk-opnds68 o1 o2 sn-loc) |
| 10252 |
(move-opnd68-to-loc68 |
| 10253 |
(opnd68->true-opnd68 o2 (sn-opnd68 o1 sn-loc)) |
| 10254 |
dtemp1) |
| 10255 |
(emit-move.l (opnd68->true-opnd68 o1 sn-loc) atemp1) |
| 10256 |
(if (eq? kind 'string) |
| 10257 |
(begin |
| 10258 |
(emit-asr.l (make-imm 3) dtemp1) |
| 10259 |
(emit-move.b |
| 10260 |
(make-imm 0) |
| 10261 |
(make-inx atemp1 dtemp1 (- pointer-size type-subtyped))) |
| 10262 |
(emit-addq.l 1 dtemp1) |
| 10263 |
(emit-asl.l (make-imm 8) dtemp1)) |
| 10264 |
(emit-asl.l (make-imm (vector-select kind 7 5 5 6)) dtemp1)) |
| 10265 |
(emit-move.b (make-ind atemp1) dtemp1) |
| 10266 |
(emit-move.l dtemp1 (make-disp* atemp1 (- type-subtyped))) |
| 10267 |
(if (and loc (not (eq? first-opnd loc))) |
| 10268 |
(move-opnd68-to-loc atemp1 loc sn))))))) |
| 10269 |
(define (gen-eq-test bits not? opnds lbl fs) |
| 10270 |
(gen-compare* (opnd->opnd68 (car opnds) #f fs) (make-imm bits) fs) |
| 10271 |
(if not? (emit-bne lbl) (emit-beq lbl))) |
| 10272 |
(define (gen-compare opnd1 opnd2 fs) |
| 10273 |
(let* ((o1 (opnd->opnd68 opnd1 #f (sn-opnd opnd2 fs))) |
| 10274 |
(o2 (opnd->opnd68 opnd2 (temp-in-opnd68 o1) fs))) |
| 10275 |
(gen-compare* o1 o2 fs))) |
| 10276 |
(define (gen-compare* o1 o2 fs) |
| 10277 |
(make-top-of-frame-if-stk-opnds68 o1 o2 fs) |
| 10278 |
(let ((order-1-2 |
| 10279 |
(cond ((imm? o1) |
| 10280 |
(cmp-n-to-opnd68 (imm-val o1) (opnd68->true-opnd68 o2 fs))) |
| 10281 |
((imm? o2) |
| 10282 |
(not (cmp-n-to-opnd68 |
| 10283 |
(imm-val o2) |
| 10284 |
(opnd68->true-opnd68 o1 fs)))) |
| 10285 |
((reg68? o1) (emit-cmp.l (opnd68->true-opnd68 o2 fs) o1) #f) |
| 10286 |
((reg68? o2) (emit-cmp.l (opnd68->true-opnd68 o1 fs) o2) #t) |
| 10287 |
(else |
| 10288 |
(emit-move.l (opnd68->true-opnd68 o1 (sn-opnd68 o2 fs)) dtemp1) |
| 10289 |
(emit-cmp.l (opnd68->true-opnd68 o2 fs) dtemp1) |
| 10290 |
#f)))) |
| 10291 |
(shrink-frame fs) |
| 10292 |
order-1-2)) |
| 10293 |
(define (gen-compares branch< branch>= branch> branch<= not? opnds lbl fs) |
| 10294 |
(gen-compares* |
| 10295 |
gen-compare |
| 10296 |
branch< |
| 10297 |
branch>= |
| 10298 |
branch> |
| 10299 |
branch<= |
| 10300 |
not? |
| 10301 |
opnds |
| 10302 |
lbl |
| 10303 |
fs)) |
| 10304 |
(define (gen-compares* |
| 10305 |
gen-comp |
| 10306 |
branch< |
| 10307 |
branch>= |
| 10308 |
branch> |
| 10309 |
branch<= |
| 10310 |
not? |
| 10311 |
opnds |
| 10312 |
lbl |
| 10313 |
fs) |
| 10314 |
(define (gen-compare-sequence opnd1 opnd2 rest) |
| 10315 |
(if (null? rest) |
| 10316 |
(if (gen-comp opnd1 opnd2 fs) |
| 10317 |
(if not? (branch<= lbl) (branch> lbl)) |
| 10318 |
(if not? (branch>= lbl) (branch< lbl))) |
| 10319 |
(let ((order-1-2 |
| 10320 |
(gen-comp opnd1 opnd2 (sn-opnd opnd2 (sn-opnds rest fs))))) |
| 10321 |
(if (= current-fs fs) |
| 10322 |
(if not? |
| 10323 |
(begin |
| 10324 |
(if order-1-2 (branch<= lbl) (branch>= lbl)) |
| 10325 |
(gen-compare-sequence opnd2 (car rest) (cdr rest))) |
| 10326 |
(let ((exit-lbl (new-lbl!))) |
| 10327 |
(if order-1-2 (branch<= exit-lbl) (branch>= exit-lbl)) |
| 10328 |
(gen-compare-sequence opnd2 (car rest) (cdr rest)) |
| 10329 |
(emit-label exit-lbl))) |
| 10330 |
(if not? |
| 10331 |
(let ((next-lbl (new-lbl!))) |
| 10332 |
(if order-1-2 (branch> next-lbl) (branch< next-lbl)) |
| 10333 |
(shrink-frame fs) |
| 10334 |
(emit-bra lbl) |
| 10335 |
(emit-label next-lbl) |
| 10336 |
(gen-compare-sequence opnd2 (car rest) (cdr rest))) |
| 10337 |
(let* ((next-lbl (new-lbl!)) (exit-lbl (new-lbl!))) |
| 10338 |
(if order-1-2 (branch> next-lbl) (branch< next-lbl)) |
| 10339 |
(shrink-frame fs) |
| 10340 |
(emit-bra exit-lbl) |
| 10341 |
(emit-label next-lbl) |
| 10342 |
(gen-compare-sequence opnd2 (car rest) (cdr rest)) |
| 10343 |
(emit-label exit-lbl))))))) |
| 10344 |
(if (or (null? opnds) (null? (cdr opnds))) |
| 10345 |
(begin (shrink-frame fs) (if (not not?) (emit-bra lbl))) |
| 10346 |
(gen-compare-sequence (car opnds) (cadr opnds) (cddr opnds)))) |
| 10347 |
(define (gen-compare-flo opnd1 opnd2 fs) |
| 10348 |
(let* ((o1 (opnd->opnd68 opnd1 #f (sn-opnd opnd2 fs))) |
| 10349 |
(o2 (opnd->opnd68 opnd2 (temp-in-opnd68 o1) fs))) |
| 10350 |
(make-top-of-frame-if-stk-opnds68 o1 o2 fs) |
| 10351 |
(emit-move.l (opnd68->true-opnd68 o1 (sn-opnd68 o2 fs)) atemp1) |
| 10352 |
(emit-move.l (opnd68->true-opnd68 o2 fs) atemp2) |
| 10353 |
(emit-fmov.dx (make-disp* atemp2 (- type-flonum)) ftemp1) |
| 10354 |
(emit-fcmp.dx (make-disp* atemp1 (- type-flonum)) ftemp1) |
| 10355 |
#t)) |
| 10356 |
(define (gen-compares-flo branch< branch>= branch> branch<= not? opnds lbl fs) |
| 10357 |
(gen-compares* |
| 10358 |
gen-compare-flo |
| 10359 |
branch< |
| 10360 |
branch>= |
| 10361 |
branch> |
| 10362 |
branch<= |
| 10363 |
not? |
| 10364 |
opnds |
| 10365 |
lbl |
| 10366 |
fs)) |
| 10367 |
(define (gen-type-test tag not? opnds lbl fs) |
| 10368 |
(let ((opnd (car opnds))) |
| 10369 |
(let ((o (opnd->opnd68 opnd #f fs))) |
| 10370 |
(define (mask-test set-reg correction) |
| 10371 |
(emit-btst |
| 10372 |
(if (= correction 0) |
| 10373 |
(if (dreg? o) |
| 10374 |
o |
| 10375 |
(begin |
| 10376 |
(emit-move.l (opnd68->true-opnd68 o fs) dtemp1) |
| 10377 |
dtemp1)) |
| 10378 |
(begin |
| 10379 |
(if (not (eq? o dtemp1)) |
| 10380 |
(emit-move.l (opnd68->true-opnd68 o fs) dtemp1)) |
| 10381 |
(emit-addq.w correction dtemp1) |
| 10382 |
dtemp1)) |
| 10383 |
set-reg)) |
| 10384 |
(make-top-of-frame-if-stk-opnd68 o fs) |
| 10385 |
(cond ((= tag 0) |
| 10386 |
(if (eq? o dtemp1) |
| 10387 |
(emit-and.w (make-imm 7) dtemp1) |
| 10388 |
(begin |
| 10389 |
(emit-move.l (opnd68->true-opnd68 o fs) dtemp1) |
| 10390 |
(emit-and.w (make-imm 7) dtemp1)))) |
| 10391 |
((= tag type-placeholder) (mask-test placeholder-reg 0)) |
| 10392 |
(else (mask-test pair-reg (modulo (- type-pair tag) 8)))) |
| 10393 |
(shrink-frame fs) |
| 10394 |
(if not? (emit-bne lbl) (emit-beq lbl))))) |
| 10395 |
(define (gen-subtype-test type not? opnds lbl fs) |
| 10396 |
(let ((opnd (car opnds))) |
| 10397 |
(let ((o (opnd->opnd68 opnd #f fs)) (cont-lbl (new-lbl!))) |
| 10398 |
(make-top-of-frame-if-stk-opnd68 o fs) |
| 10399 |
(if (not (eq? o dtemp1)) (emit-move.l (opnd68->true-opnd68 o fs) dtemp1)) |
| 10400 |
(emit-move.l dtemp1 atemp1) |
| 10401 |
(emit-addq.w (modulo (- type-pair type-subtyped) 8) dtemp1) |
| 10402 |
(emit-btst dtemp1 pair-reg) |
| 10403 |
(shrink-frame fs) |
| 10404 |
(if not? (emit-bne lbl) (emit-bne cont-lbl)) |
| 10405 |
(emit-cmp.b (make-imm (* type 8)) (make-ind atemp1)) |
| 10406 |
(if not? (emit-bne lbl) (emit-beq lbl)) |
| 10407 |
(emit-label cont-lbl)))) |
| 10408 |
(define (gen-even-test not? opnds lbl fs) |
| 10409 |
(move-opnd-to-loc68 (car opnds) dtemp1 fs) |
| 10410 |
(emit-and.w (make-imm 8) dtemp1) |
| 10411 |
(shrink-frame fs) |
| 10412 |
(if not? (emit-bne lbl) (emit-beq lbl))) |
| 10413 |
(define (def-spec name specializer-maker) |
| 10414 |
(let ((proc-name (string->canonical-symbol name))) |
| 10415 |
(let ((proc (prim-info proc-name))) |
| 10416 |
(if proc |
| 10417 |
(proc-obj-specialize-set! proc (specializer-maker proc proc-name)) |
| 10418 |
(compiler-internal-error "def-spec, unknown primitive:" name))))) |
| 10419 |
(define (safe name) |
| 10420 |
(lambda (proc proc-name) |
| 10421 |
(let ((spec (get-prim-info name))) (lambda (decls) spec)))) |
| 10422 |
(define (unsafe name) |
| 10423 |
(lambda (proc proc-name) |
| 10424 |
(let ((spec (get-prim-info name))) |
| 10425 |
(lambda (decls) (if (not (safe? decls)) spec proc))))) |
| 10426 |
(define (safe-arith fix-name flo-name) (arith #t fix-name flo-name)) |
| 10427 |
(define (unsafe-arith fix-name flo-name) (arith #f fix-name flo-name)) |
| 10428 |
(define (arith fix-safe? fix-name flo-name) |
| 10429 |
(lambda (proc proc-name) |
| 10430 |
(let ((fix-spec (if fix-name (get-prim-info fix-name) proc)) |
| 10431 |
(flo-spec (if flo-name (get-prim-info flo-name) proc))) |
| 10432 |
(lambda (decls) |
| 10433 |
(let ((arith (arith-implementation proc-name decls))) |
| 10434 |
(cond ((eq? arith fixnum-sym) |
| 10435 |
(if (or fix-safe? (not (safe? decls))) fix-spec proc)) |
| 10436 |
((eq? arith flonum-sym) (if (not (safe? decls)) flo-spec proc)) |
| 10437 |
(else proc))))))) |
| 10438 |
(define dummy3 |
| 10439 |
(begin |
| 10440 |
(define-apply "##TYPE" #f (lambda (opnds loc sn) (gen-type opnds loc sn))) |
| 10441 |
(define-apply |
| 10442 |
"##TYPE-CAST" |
| 10443 |
#f |
| 10444 |
(lambda (opnds loc sn) (gen-type-cast opnds loc sn))) |
| 10445 |
(define-apply |
| 10446 |
"##SUBTYPE" |
| 10447 |
#f |
| 10448 |
(lambda (opnds loc sn) (gen-subtype opnds loc sn))) |
| 10449 |
(define-apply |
| 10450 |
"##SUBTYPE-SET!" |
| 10451 |
#t |
| 10452 |
(lambda (opnds loc sn) (gen-subtype-set! opnds loc sn))) |
| 10453 |
(define-ifjump |
| 10454 |
"##NOT" |
| 10455 |
(lambda (not? opnds lbl fs) (gen-eq-test bits-false not? opnds lbl fs))) |
| 10456 |
(define-ifjump |
| 10457 |
"##NULL?" |
| 10458 |
(lambda (not? opnds lbl fs) (gen-eq-test bits-null not? opnds lbl fs))) |
| 10459 |
(define-ifjump |
| 10460 |
"##UNASSIGNED?" |
| 10461 |
(lambda (not? opnds lbl fs) (gen-eq-test bits-unass not? opnds lbl fs))) |
| 10462 |
(define-ifjump |
| 10463 |
"##UNBOUND?" |
| 10464 |
(lambda (not? opnds lbl fs) (gen-eq-test bits-unbound not? opnds lbl fs))) |
| 10465 |
(define-ifjump |
| 10466 |
"##EQ?" |
| 10467 |
(lambda (not? opnds lbl fs) |
| 10468 |
(gen-compares emit-beq emit-bne emit-beq emit-bne not? opnds lbl fs))) |
| 10469 |
(define-ifjump |
| 10470 |
"##FIXNUM?" |
| 10471 |
(lambda (not? opnds lbl fs) (gen-type-test type-fixnum not? opnds lbl fs))) |
| 10472 |
(define-ifjump |
| 10473 |
"##FLONUM?" |
| 10474 |
(lambda (not? opnds lbl fs) (gen-type-test type-flonum not? opnds lbl fs))) |
| 10475 |
(define-ifjump |
| 10476 |
"##SPECIAL?" |
| 10477 |
(lambda (not? opnds lbl fs) (gen-type-test type-special not? opnds lbl fs))) |
| 10478 |
(define-ifjump |
| 10479 |
"##PAIR?" |
| 10480 |
(lambda (not? opnds lbl fs) (gen-type-test type-pair not? opnds lbl fs))) |
| 10481 |
(define-ifjump |
| 10482 |
"##SUBTYPED?" |
| 10483 |
(lambda (not? opnds lbl fs) (gen-type-test type-subtyped not? opnds lbl fs))) |
| 10484 |
(define-ifjump |
| 10485 |
"##PROCEDURE?" |
| 10486 |
(lambda (not? opnds lbl fs) (gen-type-test type-procedure not? opnds lbl fs))) |
| 10487 |
(define-ifjump |
| 10488 |
"##PLACEHOLDER?" |
| 10489 |
(lambda (not? opnds lbl fs) |
| 10490 |
(gen-type-test type-placeholder not? opnds lbl fs))) |
| 10491 |
(define-ifjump |
| 10492 |
"##VECTOR?" |
| 10493 |
(lambda (not? opnds lbl fs) |
| 10494 |
(gen-subtype-test subtype-vector not? opnds lbl fs))) |
| 10495 |
(define-ifjump |
| 10496 |
"##SYMBOL?" |
| 10497 |
(lambda (not? opnds lbl fs) |
| 10498 |
(gen-subtype-test subtype-symbol not? opnds lbl fs))) |
| 10499 |
(define-ifjump |
| 10500 |
"##RATNUM?" |
| 10501 |
(lambda (not? opnds lbl fs) |
| 10502 |
(gen-subtype-test subtype-ratnum not? opnds lbl fs))) |
| 10503 |
(define-ifjump |
| 10504 |
"##CPXNUM?" |
| 10505 |
(lambda (not? opnds lbl fs) |
| 10506 |
(gen-subtype-test subtype-cpxnum not? opnds lbl fs))) |
| 10507 |
(define-ifjump |
| 10508 |
"##STRING?" |
| 10509 |
(lambda (not? opnds lbl fs) |
| 10510 |
(gen-subtype-test subtype-string not? opnds lbl fs))) |
| 10511 |
(define-ifjump |
| 10512 |
"##BIGNUM?" |
| 10513 |
(lambda (not? opnds lbl fs) |
| 10514 |
(gen-subtype-test subtype-bignum not? opnds lbl fs))) |
| 10515 |
(define-ifjump |
| 10516 |
"##CHAR?" |
| 10517 |
(lambda (not? opnds lbl fs) |
| 10518 |
(let ((opnd (car opnds))) |
| 10519 |
(let ((o (opnd->opnd68 opnd #f fs)) (cont-lbl (new-lbl!))) |
| 10520 |
(make-top-of-frame-if-stk-opnd68 o fs) |
| 10521 |
(emit-move.l (opnd68->true-opnd68 o fs) dtemp1) |
| 10522 |
(if not? (emit-bmi lbl) (emit-bmi cont-lbl)) |
| 10523 |
(emit-addq.w (modulo (- type-pair type-special) 8) dtemp1) |
| 10524 |
(emit-btst dtemp1 pair-reg) |
| 10525 |
(shrink-frame fs) |
| 10526 |
(if not? (emit-bne lbl) (emit-beq lbl)) |
| 10527 |
(emit-label cont-lbl))))) |
| 10528 |
(define-ifjump |
| 10529 |
"##CLOSURE?" |
| 10530 |
(lambda (not? opnds lbl fs) |
| 10531 |
(move-opnd-to-loc68 (car opnds) atemp1 fs) |
| 10532 |
(shrink-frame fs) |
| 10533 |
(emit-cmp.w (make-imm 20153) (make-ind atemp1)) |
| 10534 |
(if not? (emit-bne lbl) (emit-beq lbl)))) |
| 10535 |
(define-ifjump |
| 10536 |
"##SUBPROCEDURE?" |
| 10537 |
(lambda (not? opnds lbl fs) |
| 10538 |
(move-opnd-to-loc68 (car opnds) atemp1 fs) |
| 10539 |
(shrink-frame fs) |
| 10540 |
(emit-move.w (make-pdec atemp1) dtemp1) |
| 10541 |
(if not? (emit-bmi lbl) (emit-bpl lbl)))) |
| 10542 |
(define-ifjump |
| 10543 |
"##RETURN-DYNAMIC-ENV-BIND?" |
| 10544 |
(lambda (not? opnds lbl fs) |
| 10545 |
(move-opnd-to-loc68 (car opnds) atemp1 fs) |
| 10546 |
(shrink-frame fs) |
| 10547 |
(emit-move.w (make-disp* atemp1 -6) dtemp1) |
| 10548 |
(if not? (emit-bne lbl) (emit-beq lbl)))) |
| 10549 |
(define-apply |
| 10550 |
"##FIXNUM.+" |
| 10551 |
#f |
| 10552 |
(lambda (opnds loc sn) |
| 10553 |
(let ((sn-loc (sn-opnd loc sn))) |
| 10554 |
(cond ((null? opnds) (copy-opnd-to-loc (make-obj '0) loc sn)) |
| 10555 |
((null? (cdr opnds)) (copy-opnd-to-loc (car opnds) loc sn)) |
| 10556 |
((or (reg? loc) (stk? loc)) |
| 10557 |
(commut-oper gen-add opnds loc sn #f '() '())) |
| 10558 |
(else (gen-add opnds '() loc sn #f)))))) |
| 10559 |
(define-apply |
| 10560 |
"##FIXNUM.-" |
| 10561 |
#f |
| 10562 |
(lambda (opnds loc sn) |
| 10563 |
(let ((sn-loc (sn-opnd loc sn))) |
| 10564 |
(gen-sub (car opnds) |
| 10565 |
(cdr opnds) |
| 10566 |
loc |
| 10567 |
sn |
| 10568 |
(any-contains-opnd? loc (cdr opnds)))))) |
| 10569 |
(define-apply |
| 10570 |
"##FIXNUM.*" |
| 10571 |
#f |
| 10572 |
(lambda (opnds loc sn) |
| 10573 |
(let ((sn-loc (sn-opnd loc sn))) |
| 10574 |
(cond ((null? opnds) (copy-opnd-to-loc (make-obj '1) loc sn)) |
| 10575 |
((null? (cdr opnds)) (copy-opnd-to-loc (car opnds) loc sn)) |
| 10576 |
((and (reg? loc) (not (eq? loc return-reg))) |
| 10577 |
(commut-oper gen-mul opnds loc sn #f '() '())) |
| 10578 |
(else (gen-mul opnds '() loc sn #f)))))) |
| 10579 |
(define-apply |
| 10580 |
"##FIXNUM.QUOTIENT" |
| 10581 |
#f |
| 10582 |
(lambda (opnds loc sn) |
| 10583 |
(let ((sn-loc (sn-opnd loc sn))) |
| 10584 |
(gen-div (car opnds) |
| 10585 |
(cdr opnds) |
| 10586 |
loc |
| 10587 |
sn |
| 10588 |
(any-contains-opnd? loc (cdr opnds)))))) |
| 10589 |
(define-apply |
| 10590 |
"##FIXNUM.REMAINDER" |
| 10591 |
#f |
| 10592 |
(lambda (opnds loc sn) |
| 10593 |
(let ((sn-loc (sn-opnd loc sn))) |
| 10594 |
(gen-rem (car opnds) (cadr opnds) loc sn)))) |
| 10595 |
(define-apply |
| 10596 |
"##FIXNUM.MODULO" |
| 10597 |
#f |
| 10598 |
(lambda (opnds loc sn) |
| 10599 |
(let ((sn-loc (sn-opnd loc sn))) |
| 10600 |
(gen-mod (car opnds) (cadr opnds) loc sn)))) |
| 10601 |
(define-apply |
| 10602 |
"##FIXNUM.LOGIOR" |
| 10603 |
#f |
| 10604 |
(lambda (opnds loc sn) |
| 10605 |
(let ((sn-loc (sn-opnd loc sn))) |
| 10606 |
(cond ((null? opnds) (copy-opnd-to-loc (make-obj '0) loc sn)) |
| 10607 |
((null? (cdr opnds)) (copy-opnd-to-loc (car opnds) loc sn)) |
| 10608 |
((or (reg? loc) (stk? loc)) |
| 10609 |
(commut-oper gen-logior opnds loc sn #f '() '())) |
| 10610 |
(else (gen-logior opnds '() loc sn #f)))))) |
| 10611 |
(define-apply |
| 10612 |
"##FIXNUM.LOGXOR" |
| 10613 |
#f |
| 10614 |
(lambda (opnds loc sn) |
| 10615 |
(let ((sn-loc (sn-opnd loc sn))) |
| 10616 |
(cond ((null? opnds) (copy-opnd-to-loc (make-obj '0) loc sn)) |
| 10617 |
((null? (cdr opnds)) (copy-opnd-to-loc (car opnds) loc sn)) |
| 10618 |
((or (reg? loc) (stk? loc)) |
| 10619 |
(commut-oper gen-logxor opnds loc sn #f '() '())) |
| 10620 |
(else (gen-logxor opnds '() loc sn #f)))))) |
| 10621 |
(define-apply |
| 10622 |
"##FIXNUM.LOGAND" |
| 10623 |
#f |
| 10624 |
(lambda (opnds loc sn) |
| 10625 |
(let ((sn-loc (sn-opnd loc sn))) |
| 10626 |
(cond ((null? opnds) (copy-opnd-to-loc (make-obj '-1) loc sn)) |
| 10627 |
((null? (cdr opnds)) (copy-opnd-to-loc (car opnds) loc sn)) |
| 10628 |
((or (reg? loc) (stk? loc)) |
| 10629 |
(commut-oper gen-logand opnds loc sn #f '() '())) |
| 10630 |
(else (gen-logand opnds '() loc sn #f)))))) |
| 10631 |
(define-apply |
| 10632 |
"##FIXNUM.LOGNOT" |
| 10633 |
#f |
| 10634 |
(lambda (opnds loc sn) |
| 10635 |
(let ((sn-loc (sn-opnd loc sn)) (opnd (car opnds))) |
| 10636 |
(if (and (or (reg? loc) (stk? loc)) (not (eq? loc return-reg))) |
| 10637 |
(begin |
| 10638 |
(copy-opnd-to-loc opnd loc sn-loc) |
| 10639 |
(let ((loc68 (loc->loc68 loc #f sn))) |
| 10640 |
(make-top-of-frame-if-stk-opnd68 loc68 sn) |
| 10641 |
(emit-not.l (opnd68->true-opnd68 loc68 sn)) |
| 10642 |
(emit-and.w (make-imm -8) (opnd68->true-opnd68 loc68 sn)))) |
| 10643 |
(begin |
| 10644 |
(move-opnd-to-loc68 opnd dtemp1 (sn-opnd loc sn)) |
| 10645 |
(emit-not.l dtemp1) |
| 10646 |
(emit-and.w (make-imm -8) dtemp1) |
| 10647 |
(move-opnd68-to-loc dtemp1 loc sn)))))) |
| 10648 |
(define-apply "##FIXNUM.ASH" #f (gen-shift emit-asr.l)) |
| 10649 |
(define-apply "##FIXNUM.LSH" #f (gen-shift emit-lsr.l)) |
| 10650 |
(define-ifjump |
| 10651 |
"##FIXNUM.ZERO?" |
| 10652 |
(lambda (not? opnds lbl fs) (gen-eq-test 0 not? opnds lbl fs))) |
| 10653 |
(define-ifjump |
| 10654 |
"##FIXNUM.POSITIVE?" |
| 10655 |
(lambda (not? opnds lbl fs) |
| 10656 |
(gen-compares |
| 10657 |
emit-bgt |
| 10658 |
emit-ble |
| 10659 |
emit-blt |
| 10660 |
emit-bge |
| 10661 |
not? |
| 10662 |
(list (car opnds) (make-obj '0)) |
| 10663 |
lbl |
| 10664 |
fs))) |
| 10665 |
(define-ifjump |
| 10666 |
"##FIXNUM.NEGATIVE?" |
| 10667 |
(lambda (not? opnds lbl fs) |
| 10668 |
(gen-compares |
| 10669 |
emit-blt |
| 10670 |
emit-bge |
| 10671 |
emit-bgt |
| 10672 |
emit-ble |
| 10673 |
not? |
| 10674 |
(list (car opnds) (make-obj '0)) |
| 10675 |
lbl |
| 10676 |
fs))) |
| 10677 |
(define-ifjump |
| 10678 |
"##FIXNUM.ODD?" |
| 10679 |
(lambda (not? opnds lbl fs) (gen-even-test (not not?) opnds lbl fs))) |
| 10680 |
(define-ifjump |
| 10681 |
"##FIXNUM.EVEN?" |
| 10682 |
(lambda (not? opnds lbl fs) (gen-even-test not? opnds lbl fs))) |
| 10683 |
(define-ifjump |
| 10684 |
"##FIXNUM.=" |
| 10685 |
(lambda (not? opnds lbl fs) |
| 10686 |
(gen-compares emit-beq emit-bne emit-beq emit-bne not? opnds lbl fs))) |
| 10687 |
(define-ifjump |
| 10688 |
"##FIXNUM.<" |
| 10689 |
(lambda (not? opnds lbl fs) |
| 10690 |
(gen-compares emit-blt emit-bge emit-bgt emit-ble not? opnds lbl fs))) |
| 10691 |
(define-ifjump |
| 10692 |
"##FIXNUM.>" |
| 10693 |
(lambda (not? opnds lbl fs) |
| 10694 |
(gen-compares emit-bgt emit-ble emit-blt emit-bge not? opnds lbl fs))) |
| 10695 |
(define-ifjump |
| 10696 |
"##FIXNUM.<=" |
| 10697 |
(lambda (not? opnds lbl fs) |
| 10698 |
(gen-compares emit-ble emit-bgt emit-bge emit-blt not? opnds lbl fs))) |
| 10699 |
(define-ifjump |
| 10700 |
"##FIXNUM.>=" |
| 10701 |
(lambda (not? opnds lbl fs) |
| 10702 |
(gen-compares emit-bge emit-blt emit-ble emit-bgt not? opnds lbl fs))) |
| 10703 |
(define-apply |
| 10704 |
"##FLONUM.->FIXNUM" |
| 10705 |
#f |
| 10706 |
(lambda (opnds loc sn) |
| 10707 |
(let ((sn-loc (sn-opnd loc sn))) |
| 10708 |
(move-opnd-to-loc68 (car opnds) atemp1 sn-loc) |
| 10709 |
(let ((reg68 (if (and (reg? loc) (not (eq? loc return-reg))) |
| 10710 |
(reg->reg68 loc) |
| 10711 |
dtemp1))) |
| 10712 |
(emit-fmov.dx (make-disp* atemp1 (- type-flonum)) ftemp1) |
| 10713 |
(emit-fmov.l ftemp1 reg68) |
| 10714 |
(emit-asl.l (make-imm 3) reg68) |
| 10715 |
(if (not (and (reg? loc) (not (eq? loc return-reg)))) |
| 10716 |
(move-opnd68-to-loc reg68 loc sn)))))) |
| 10717 |
(define-apply |
| 10718 |
"##FLONUM.<-FIXNUM" |
| 10719 |
#f |
| 10720 |
(lambda (opnds loc sn) |
| 10721 |
(gen-guarantee-space 2) |
| 10722 |
(move-opnd-to-loc68 |
| 10723 |
(car opnds) |
| 10724 |
dtemp1 |
| 10725 |
(sn-opnds (cdr opnds) (sn-opnd loc sn))) |
| 10726 |
(emit-asr.l (make-imm 3) dtemp1) |
| 10727 |
(emit-fmov.l dtemp1 ftemp1) |
| 10728 |
(add-n-to-loc68 (* -2 pointer-size) heap-reg) |
| 10729 |
(emit-fmov.dx ftemp1 (make-ind heap-reg)) |
| 10730 |
(let ((reg68 (if (reg? loc) (reg->reg68 loc) atemp1))) |
| 10731 |
(emit-move.l heap-reg reg68) |
| 10732 |
(emit-addq.l type-flonum reg68)) |
| 10733 |
(if (not (reg? loc)) (move-opnd68-to-loc atemp1 loc sn)))) |
| 10734 |
(define-apply |
| 10735 |
"##FLONUM.+" |
| 10736 |
#f |
| 10737 |
(lambda (opnds loc sn) |
| 10738 |
(let ((sn-loc (sn-opnd loc sn))) |
| 10739 |
(cond ((null? opnds) (copy-opnd-to-loc (make-obj inexact-0) loc sn)) |
| 10740 |
((null? (cdr opnds)) (copy-opnd-to-loc (car opnds) loc sn)) |
| 10741 |
(else (flo-oper emit-fmov.dx emit-fadd.dx opnds loc sn)))))) |
| 10742 |
(define-apply |
| 10743 |
"##FLONUM.*" |
| 10744 |
#f |
| 10745 |
(lambda (opnds loc sn) |
| 10746 |
(let ((sn-loc (sn-opnd loc sn))) |
| 10747 |
(cond ((null? opnds) (copy-opnd-to-loc (make-obj inexact-+1) loc sn)) |
| 10748 |
((null? (cdr opnds)) (copy-opnd-to-loc (car opnds) loc sn)) |
| 10749 |
(else (flo-oper emit-fmov.dx emit-fmul.dx opnds loc sn)))))) |
| 10750 |
(define-apply |
| 10751 |
"##FLONUM.-" |
| 10752 |
#f |
| 10753 |
(lambda (opnds loc sn) |
| 10754 |
(let ((sn-loc (sn-opnd loc sn))) |
| 10755 |
(if (null? (cdr opnds)) |
| 10756 |
(flo-oper emit-fneg.dx #f opnds loc sn) |
| 10757 |
(flo-oper emit-fmov.dx emit-fsub.dx opnds loc sn))))) |
| 10758 |
(define-apply |
| 10759 |
"##FLONUM./" |
| 10760 |
#f |
| 10761 |
(lambda (opnds loc sn) |
| 10762 |
(let ((sn-loc (sn-opnd loc sn))) |
| 10763 |
(if (null? (cdr opnds)) |
| 10764 |
(flo-oper |
| 10765 |
emit-fmov.dx |
| 10766 |
emit-fdiv.dx |
| 10767 |
(cons (make-obj inexact-+1) opnds) |
| 10768 |
loc |
| 10769 |
sn) |
| 10770 |
(flo-oper emit-fmov.dx emit-fdiv.dx opnds loc sn))))) |
| 10771 |
(define-apply |
| 10772 |
"##FLONUM.ABS" |
| 10773 |
#f |
| 10774 |
(lambda (opnds loc sn) |
| 10775 |
(let ((sn-loc (sn-opnd loc sn))) (flo-oper emit-fabs.dx #f opnds loc sn)))) |
| 10776 |
(define-apply |
| 10777 |
"##FLONUM.TRUNCATE" |
| 10778 |
#f |
| 10779 |
(lambda (opnds loc sn) |
| 10780 |
(let ((sn-loc (sn-opnd loc sn))) |
| 10781 |
(flo-oper emit-fintrz.dx #f opnds loc sn)))) |
| 10782 |
(define-apply |
| 10783 |
"##FLONUM.ROUND" |
| 10784 |
#f |
| 10785 |
(lambda (opnds loc sn) |
| 10786 |
(let ((sn-loc (sn-opnd loc sn))) (flo-oper emit-fint.dx #f opnds loc sn)))) |
| 10787 |
(define-apply |
| 10788 |
"##FLONUM.EXP" |
| 10789 |
#f |
| 10790 |
(lambda (opnds loc sn) |
| 10791 |
(let ((sn-loc (sn-opnd loc sn))) (flo-oper emit-fetox.dx #f opnds loc sn)))) |
| 10792 |
(define-apply |
| 10793 |
"##FLONUM.LOG" |
| 10794 |
#f |
| 10795 |
(lambda (opnds loc sn) |
| 10796 |
(let ((sn-loc (sn-opnd loc sn))) (flo-oper emit-flogn.dx #f opnds loc sn)))) |
| 10797 |
(define-apply |
| 10798 |
"##FLONUM.SIN" |
| 10799 |
#f |
| 10800 |
(lambda (opnds loc sn) |
| 10801 |
(let ((sn-loc (sn-opnd loc sn))) (flo-oper emit-fsin.dx #f opnds loc sn)))) |
| 10802 |
(define-apply |
| 10803 |
"##FLONUM.COS" |
| 10804 |
#f |
| 10805 |
(lambda (opnds loc sn) |
| 10806 |
(let ((sn-loc (sn-opnd loc sn))) (flo-oper emit-fcos.dx #f opnds loc sn)))) |
| 10807 |
(define-apply |
| 10808 |
"##FLONUM.TAN" |
| 10809 |
#f |
| 10810 |
(lambda (opnds loc sn) |
| 10811 |
(let ((sn-loc (sn-opnd loc sn))) (flo-oper emit-ftan.dx #f opnds loc sn)))) |
| 10812 |
(define-apply |
| 10813 |
"##FLONUM.ASIN" |
| 10814 |
#f |
| 10815 |
(lambda (opnds loc sn) |
| 10816 |
(let ((sn-loc (sn-opnd loc sn))) (flo-oper emit-fasin.dx #f opnds loc sn)))) |
| 10817 |
(define-apply |
| 10818 |
"##FLONUM.ACOS" |
| 10819 |
#f |
| 10820 |
(lambda (opnds loc sn) |
| 10821 |
(let ((sn-loc (sn-opnd loc sn))) (flo-oper emit-facos.dx #f opnds loc sn)))) |
| 10822 |
(define-apply |
| 10823 |
"##FLONUM.ATAN" |
| 10824 |
#f |
| 10825 |
(lambda (opnds loc sn) |
| 10826 |
(let ((sn-loc (sn-opnd loc sn))) (flo-oper emit-fatan.dx #f opnds loc sn)))) |
| 10827 |
(define-apply |
| 10828 |
"##FLONUM.SQRT" |
| 10829 |
#f |
| 10830 |
(lambda (opnds loc sn) |
| 10831 |
(let ((sn-loc (sn-opnd loc sn))) (flo-oper emit-fsqrt.dx #f opnds loc sn)))) |
| 10832 |
(define-ifjump |
| 10833 |
"##FLONUM.ZERO?" |
| 10834 |
(lambda (not? opnds lbl fs) |
| 10835 |
(gen-compares-flo |
| 10836 |
emit-fbeq |
| 10837 |
emit-fbne |
| 10838 |
emit-fbeq |
| 10839 |
emit-fbne |
| 10840 |
not? |
| 10841 |
(list (car opnds) (make-obj inexact-0)) |
| 10842 |
lbl |
| 10843 |
fs))) |
| 10844 |
(define-ifjump |
| 10845 |
"##FLONUM.NEGATIVE?" |
| 10846 |
(lambda (not? opnds lbl fs) |
| 10847 |
(gen-compares-flo |
| 10848 |
emit-fblt |
| 10849 |
emit-fbge |
| 10850 |
emit-fbgt |
| 10851 |
emit-fble |
| 10852 |
not? |
| 10853 |
(list (car opnds) (make-obj inexact-0)) |
| 10854 |
lbl |
| 10855 |
fs))) |
| 10856 |
(define-ifjump |
| 10857 |
"##FLONUM.POSITIVE?" |
| 10858 |
(lambda (not? opnds lbl fs) |
| 10859 |
(gen-compares-flo |
| 10860 |
emit-fbgt |
| 10861 |
emit-fble |
| 10862 |
emit-fblt |
| 10863 |
emit-fbge |
| 10864 |
not? |
| 10865 |
(list (car opnds) (make-obj inexact-0)) |
| 10866 |
lbl |
| 10867 |
fs))) |
| 10868 |
(define-ifjump |
| 10869 |
"##FLONUM.=" |
| 10870 |
(lambda (not? opnds lbl fs) |
| 10871 |
(gen-compares-flo |
| 10872 |
emit-fbeq |
| 10873 |
emit-fbne |
| 10874 |
emit-fbeq |
| 10875 |
emit-fbne |
| 10876 |
not? |
| 10877 |
opnds |
| 10878 |
lbl |
| 10879 |
fs))) |
| 10880 |
(define-ifjump |
| 10881 |
"##FLONUM.<" |
| 10882 |
(lambda (not? opnds lbl fs) |
| 10883 |
(gen-compares-flo |
| 10884 |
emit-fblt |
| 10885 |
emit-fbge |
| 10886 |
emit-fbgt |
| 10887 |
emit-fble |
| 10888 |
not? |
| 10889 |
opnds |
| 10890 |
lbl |
| 10891 |
fs))) |
| 10892 |
(define-ifjump |
| 10893 |
"##FLONUM.>" |
| 10894 |
(lambda (not? opnds lbl fs) |
| 10895 |
(gen-compares-flo |
| 10896 |
emit-fbgt |
| 10897 |
emit-fble |
| 10898 |
emit-fblt |
| 10899 |
emit-fbge |
| 10900 |
not? |
| 10901 |
opnds |
| 10902 |
lbl |
| 10903 |
fs))) |
| 10904 |
(define-ifjump |
| 10905 |
"##FLONUM.<=" |
| 10906 |
(lambda (not? opnds lbl fs) |
| 10907 |
(gen-compares-flo |
| 10908 |
emit-fble |
| 10909 |
emit-fbgt |
| 10910 |
emit-fbge |
| 10911 |
emit-fblt |
| 10912 |
not? |
| 10913 |
opnds |
| 10914 |
lbl |
| 10915 |
fs))) |
| 10916 |
(define-ifjump |
| 10917 |
"##FLONUM.>=" |
| 10918 |
(lambda (not? opnds lbl fs) |
| 10919 |
(gen-compares-flo |
| 10920 |
emit-fbge |
| 10921 |
emit-fblt |
| 10922 |
emit-fble |
| 10923 |
emit-fbgt |
| 10924 |
not? |
| 10925 |
opnds |
| 10926 |
lbl |
| 10927 |
fs))) |
| 10928 |
(define-ifjump |
| 10929 |
"##CHAR=?" |
| 10930 |
(lambda (not? opnds lbl fs) |
| 10931 |
(gen-compares emit-beq emit-bne emit-beq emit-bne not? opnds lbl fs))) |
| 10932 |
(define-ifjump |
| 10933 |
"##CHAR<?" |
| 10934 |
(lambda (not? opnds lbl fs) |
| 10935 |
(gen-compares emit-blt emit-bge emit-bgt emit-ble not? opnds lbl fs))) |
| 10936 |
(define-ifjump |
| 10937 |
"##CHAR>?" |
| 10938 |
(lambda (not? opnds lbl fs) |
| 10939 |
(gen-compares emit-bgt emit-ble emit-blt emit-bge not? opnds lbl fs))) |
| 10940 |
(define-ifjump |
| 10941 |
"##CHAR<=?" |
| 10942 |
(lambda (not? opnds lbl fs) |
| 10943 |
(gen-compares emit-ble emit-bgt emit-bge emit-blt not? opnds lbl fs))) |
| 10944 |
(define-ifjump |
| 10945 |
"##CHAR>=?" |
| 10946 |
(lambda (not? opnds lbl fs) |
| 10947 |
(gen-compares emit-bge emit-blt emit-ble emit-bgt not? opnds lbl fs))) |
| 10948 |
(define-apply "##CONS" #f (lambda (opnds loc sn) (gen-cons opnds loc sn))) |
| 10949 |
(define-apply |
| 10950 |
"##SET-CAR!" |
| 10951 |
#t |
| 10952 |
(lambda (opnds loc sn) (gen-set-car! opnds loc sn))) |
| 10953 |
(define-apply |
| 10954 |
"##SET-CDR!" |
| 10955 |
#t |
| 10956 |
(lambda (opnds loc sn) (gen-set-cdr! opnds loc sn))) |
| 10957 |
(define-apply "##CAR" #f (make-gen-apply-c...r 2)) |
| 10958 |
(define-apply "##CDR" #f (make-gen-apply-c...r 3)) |
| 10959 |
(define-apply "##CAAR" #f (make-gen-apply-c...r 4)) |
| 10960 |
(define-apply "##CADR" #f (make-gen-apply-c...r 5)) |
| 10961 |
(define-apply "##CDAR" #f (make-gen-apply-c...r 6)) |
| 10962 |
(define-apply "##CDDR" #f (make-gen-apply-c...r 7)) |
| 10963 |
(define-apply "##CAAAR" #f (make-gen-apply-c...r 8)) |
| 10964 |
(define-apply "##CAADR" #f (make-gen-apply-c...r 9)) |
| 10965 |
(define-apply "##CADAR" #f (make-gen-apply-c...r 10)) |
| 10966 |
(define-apply "##CADDR" #f (make-gen-apply-c...r 11)) |
| 10967 |
(define-apply "##CDAAR" #f (make-gen-apply-c...r 12)) |
| 10968 |
(define-apply "##CDADR" #f (make-gen-apply-c...r 13)) |
| 10969 |
(define-apply "##CDDAR" #f (make-gen-apply-c...r 14)) |
| 10970 |
(define-apply "##CDDDR" #f (make-gen-apply-c...r 15)) |
| 10971 |
(define-apply "##CAAAAR" #f (make-gen-apply-c...r 16)) |
| 10972 |
(define-apply "##CAAADR" #f (make-gen-apply-c...r 17)) |
| 10973 |
(define-apply "##CAADAR" #f (make-gen-apply-c...r 18)) |
| 10974 |
(define-apply "##CAADDR" #f (make-gen-apply-c...r 19)) |
| 10975 |
(define-apply "##CADAAR" #f (make-gen-apply-c...r 20)) |
| 10976 |
(define-apply "##CADADR" #f (make-gen-apply-c...r 21)) |
| 10977 |
(define-apply "##CADDAR" #f (make-gen-apply-c...r 22)) |
| 10978 |
(define-apply "##CADDDR" #f (make-gen-apply-c...r 23)) |
| 10979 |
(define-apply "##CDAAAR" #f (make-gen-apply-c...r 24)) |
| 10980 |
(define-apply "##CDAADR" #f (make-gen-apply-c...r 25)) |
| 10981 |
(define-apply "##CDADAR" #f (make-gen-apply-c...r 26)) |
| 10982 |
(define-apply "##CDADDR" #f (make-gen-apply-c...r 27)) |
| 10983 |
(define-apply "##CDDAAR" #f (make-gen-apply-c...r 28)) |
| 10984 |
(define-apply "##CDDADR" #f (make-gen-apply-c...r 29)) |
| 10985 |
(define-apply "##CDDDAR" #f (make-gen-apply-c...r 30)) |
| 10986 |
(define-apply "##CDDDDR" #f (make-gen-apply-c...r 31)) |
| 10987 |
(define-apply |
| 10988 |
"##MAKE-CELL" |
| 10989 |
#f |
| 10990 |
(lambda (opnds loc sn) (gen-cons (list (car opnds) (make-obj '())) loc sn))) |
| 10991 |
(define-apply "##CELL-REF" #f (make-gen-apply-c...r 2)) |
| 10992 |
(define-apply |
| 10993 |
"##CELL-SET!" |
| 10994 |
#t |
| 10995 |
(lambda (opnds loc sn) (gen-set-car! opnds loc sn))) |
| 10996 |
(define-apply "##VECTOR" #f (make-gen-vector 'vector)) |
| 10997 |
(define-apply "##VECTOR-LENGTH" #f (make-gen-vector-length 'vector)) |
| 10998 |
(define-apply "##VECTOR-REF" #f (make-gen-vector-ref 'vector)) |
| 10999 |
(define-apply "##VECTOR-SET!" #t (make-gen-vector-set! 'vector)) |
| 11000 |
(define-apply "##VECTOR-SHRINK!" #t (make-gen-vector-shrink! 'vector)) |
| 11001 |
(define-apply "##STRING" #f (make-gen-vector 'string)) |
| 11002 |
(define-apply "##STRING-LENGTH" #f (make-gen-vector-length 'string)) |
| 11003 |
(define-apply "##STRING-REF" #f (make-gen-vector-ref 'string)) |
| 11004 |
(define-apply "##STRING-SET!" #t (make-gen-vector-set! 'string)) |
| 11005 |
(define-apply "##STRING-SHRINK!" #t (make-gen-vector-shrink! 'string)) |
| 11006 |
(define-apply "##VECTOR8" #f (make-gen-vector 'vector8)) |
| 11007 |
(define-apply "##VECTOR8-LENGTH" #f (make-gen-vector-length 'vector8)) |
| 11008 |
(define-apply "##VECTOR8-REF" #f (make-gen-vector-ref 'vector8)) |
| 11009 |
(define-apply "##VECTOR8-SET!" #t (make-gen-vector-set! 'vector8)) |
| 11010 |
(define-apply "##VECTOR8-SHRINK!" #t (make-gen-vector-shrink! 'vector8)) |
| 11011 |
(define-apply "##VECTOR16" #f (make-gen-vector 'vector16)) |
| 11012 |
(define-apply "##VECTOR16-LENGTH" #f (make-gen-vector-length 'vector16)) |
| 11013 |
(define-apply "##VECTOR16-REF" #f (make-gen-vector-ref 'vector16)) |
| 11014 |
(define-apply "##VECTOR16-SET!" #t (make-gen-vector-set! 'vector16)) |
| 11015 |
(define-apply "##VECTOR16-SHRINK!" #t (make-gen-vector-shrink! 'vector16)) |
| 11016 |
(define-apply "##CLOSURE-CODE" #f (make-gen-slot-ref 1 type-procedure)) |
| 11017 |
(define-apply "##CLOSURE-REF" #f (make-gen-vector-ref 'closure)) |
| 11018 |
(define-apply "##CLOSURE-SET!" #t (make-gen-vector-set! 'closure)) |
| 11019 |
(define-apply |
| 11020 |
"##SUBPROCEDURE-ID" |
| 11021 |
#f |
| 11022 |
(lambda (opnds loc sn) (gen-subprocedure-id opnds loc sn))) |
| 11023 |
(define-apply |
| 11024 |
"##SUBPROCEDURE-PARENT" |
| 11025 |
#f |
| 11026 |
(lambda (opnds loc sn) (gen-subprocedure-parent opnds loc sn))) |
| 11027 |
(define-apply |
| 11028 |
"##RETURN-FS" |
| 11029 |
#f |
| 11030 |
(lambda (opnds loc sn) (gen-return-fs opnds loc sn))) |
| 11031 |
(define-apply |
| 11032 |
"##RETURN-LINK" |
| 11033 |
#f |
| 11034 |
(lambda (opnds loc sn) (gen-return-link opnds loc sn))) |
| 11035 |
(define-apply |
| 11036 |
"##PROCEDURE-INFO" |
| 11037 |
#f |
| 11038 |
(lambda (opnds loc sn) (gen-procedure-info opnds loc sn))) |
| 11039 |
(define-apply |
| 11040 |
"##PSTATE" |
| 11041 |
#f |
| 11042 |
(lambda (opnds loc sn) (move-opnd68-to-loc pstate-reg loc sn))) |
| 11043 |
(define-apply |
| 11044 |
"##MAKE-PLACEHOLDER" |
| 11045 |
#f |
| 11046 |
(lambda (opnds loc sn) (gen-make-placeholder opnds loc sn))) |
| 11047 |
(define-apply |
| 11048 |
"##TOUCH" |
| 11049 |
#t |
| 11050 |
(lambda (opnds loc sn) |
| 11051 |
(let ((opnd (car opnds))) |
| 11052 |
(if loc |
| 11053 |
(touch-opnd-to-loc opnd loc sn) |
| 11054 |
(touch-opnd-to-any-reg68 opnd sn))))) |
| 11055 |
(def-spec "NOT" (safe "##NOT")) |
| 11056 |
(def-spec "NULL?" (safe "##NULL?")) |
| 11057 |
(def-spec "EQ?" (safe "##EQ?")) |
| 11058 |
(def-spec "PAIR?" (safe "##PAIR?")) |
| 11059 |
(def-spec "PROCEDURE?" (safe "##PROCEDURE?")) |
| 11060 |
(def-spec "VECTOR?" (safe "##VECTOR?")) |
| 11061 |
(def-spec "SYMBOL?" (safe "##SYMBOL?")) |
| 11062 |
(def-spec "STRING?" (safe "##STRING?")) |
| 11063 |
(def-spec "CHAR?" (safe "##CHAR?")) |
| 11064 |
(def-spec "ZERO?" (safe-arith "##FIXNUM.ZERO?" "##FLONUM.ZERO?")) |
| 11065 |
(def-spec "POSITIVE?" (safe-arith "##FIXNUM.POSITIVE?" "##FLONUM.POSITIVE?")) |
| 11066 |
(def-spec "NEGATIVE?" (safe-arith "##FIXNUM.NEGATIVE?" "##FLONUM.NEGATIVE?")) |
| 11067 |
(def-spec "ODD?" (safe-arith "##FIXNUM.ODD?" #f)) |
| 11068 |
(def-spec "EVEN?" (safe-arith "##FIXNUM.EVEN?" #f)) |
| 11069 |
(def-spec "+" (unsafe-arith "##FIXNUM.+" "##FLONUM.+")) |
| 11070 |
(def-spec "*" (unsafe-arith "##FIXNUM.*" "##FLONUM.*")) |
| 11071 |
(def-spec "-" (unsafe-arith "##FIXNUM.-" "##FLONUM.-")) |
| 11072 |
(def-spec "/" (unsafe-arith #f "##FLONUM./")) |
| 11073 |
(def-spec "QUOTIENT" (unsafe-arith "##FIXNUM.QUOTIENT" #f)) |
| 11074 |
(def-spec "REMAINDER" (unsafe-arith "##FIXNUM.REMAINDER" #f)) |
| 11075 |
(def-spec "MODULO" (unsafe-arith "##FIXNUM.MODULO" #f)) |
| 11076 |
(def-spec "=" (safe-arith "##FIXNUM.=" "##FLONUM.=")) |
| 11077 |
(def-spec "<" (safe-arith "##FIXNUM.<" "##FLONUM.<")) |
| 11078 |
(def-spec ">" (safe-arith "##FIXNUM.>" "##FLONUM.>")) |
| 11079 |
(def-spec "<=" (safe-arith "##FIXNUM.<=" "##FLONUM.<=")) |
| 11080 |
(def-spec ">=" (safe-arith "##FIXNUM.>=" "##FLONUM.>=")) |
| 11081 |
(def-spec "ABS" (unsafe-arith #f "##FLONUM.ABS")) |
| 11082 |
(def-spec "TRUNCATE" (unsafe-arith #f "##FLONUM.TRUNCATE")) |
| 11083 |
(def-spec "EXP" (unsafe-arith #f "##FLONUM.EXP")) |
| 11084 |
(def-spec "LOG" (unsafe-arith #f "##FLONUM.LOG")) |
| 11085 |
(def-spec "SIN" (unsafe-arith #f "##FLONUM.SIN")) |
| 11086 |
(def-spec "COS" (unsafe-arith #f "##FLONUM.COS")) |
| 11087 |
(def-spec "TAN" (unsafe-arith #f "##FLONUM.TAN")) |
| 11088 |
(def-spec "ASIN" (unsafe-arith #f "##FLONUM.ASIN")) |
| 11089 |
(def-spec "ACOS" (unsafe-arith #f "##FLONUM.ACOS")) |
| 11090 |
(def-spec "ATAN" (unsafe-arith #f "##FLONUM.ATAN")) |
| 11091 |
(def-spec "SQRT" (unsafe-arith #f "##FLONUM.SQRT")) |
| 11092 |
(def-spec "CHAR=?" (safe "##CHAR=?")) |
| 11093 |
(def-spec "CHAR<?" (safe "##CHAR<?")) |
| 11094 |
(def-spec "CHAR>?" (safe "##CHAR>?")) |
| 11095 |
(def-spec "CHAR<=?" (safe "##CHAR<=?")) |
| 11096 |
(def-spec "CHAR>=?" (safe "##CHAR>=?")) |
| 11097 |
(def-spec "CONS" (safe "##CONS")) |
| 11098 |
(def-spec "SET-CAR!" (unsafe "##SET-CAR!")) |
| 11099 |
(def-spec "SET-CDR!" (unsafe "##SET-CDR!")) |
| 11100 |
(def-spec "CAR" (unsafe "##CAR")) |
| 11101 |
(def-spec "CDR" (unsafe "##CDR")) |
| 11102 |
(def-spec "CAAR" (unsafe "##CAAR")) |
| 11103 |
(def-spec "CADR" (unsafe "##CADR")) |
| 11104 |
(def-spec "CDAR" (unsafe "##CDAR")) |
| 11105 |
(def-spec "CDDR" (unsafe "##CDDR")) |
| 11106 |
(def-spec "CAAAR" (unsafe "##CAAAR")) |
| 11107 |
(def-spec "CAADR" (unsafe "##CAADR")) |
| 11108 |
(def-spec "CADAR" (unsafe "##CADAR")) |
| 11109 |
(def-spec "CADDR" (unsafe "##CADDR")) |
| 11110 |
(def-spec "CDAAR" (unsafe "##CDAAR")) |
| 11111 |
(def-spec "CDADR" (unsafe "##CDADR")) |
| 11112 |
(def-spec "CDDAR" (unsafe "##CDDAR")) |
| 11113 |
(def-spec "CDDDR" (unsafe "##CDDDR")) |
| 11114 |
(def-spec "CAAAAR" (unsafe "##CAAAAR")) |
| 11115 |
(def-spec "CAAADR" (unsafe "##CAAADR")) |
| 11116 |
(def-spec "CAADAR" (unsafe "##CAADAR")) |
| 11117 |
(def-spec "CAADDR" (unsafe "##CAADDR")) |
| 11118 |
(def-spec "CADAAR" (unsafe "##CADAAR")) |
| 11119 |
(def-spec "CADADR" (unsafe "##CADADR")) |
| 11120 |
(def-spec "CADDAR" (unsafe "##CADDAR")) |
| 11121 |
(def-spec "CADDDR" (unsafe "##CADDDR")) |
| 11122 |
(def-spec "CDAAAR" (unsafe "##CDAAAR")) |
| 11123 |
(def-spec "CDAADR" (unsafe "##CDAADR")) |
| 11124 |
(def-spec "CDADAR" (unsafe "##CDADAR")) |
| 11125 |
(def-spec "CDADDR" (unsafe "##CDADDR")) |
| 11126 |
(def-spec "CDDAAR" (unsafe "##CDDAAR")) |
| 11127 |
(def-spec "CDDADR" (unsafe "##CDDADR")) |
| 11128 |
(def-spec "CDDDAR" (unsafe "##CDDDAR")) |
| 11129 |
(def-spec "CDDDDR" (unsafe "##CDDDDR")) |
| 11130 |
(def-spec "VECTOR" (safe "##VECTOR")) |
| 11131 |
(def-spec "VECTOR-LENGTH" (unsafe "##VECTOR-LENGTH")) |
| 11132 |
(def-spec "VECTOR-REF" (unsafe "##VECTOR-REF")) |
| 11133 |
(def-spec "VECTOR-SET!" (unsafe "##VECTOR-SET!")) |
| 11134 |
(def-spec "STRING" (safe "##STRING")) |
| 11135 |
(def-spec "STRING-LENGTH" (unsafe "##STRING-LENGTH")) |
| 11136 |
(def-spec "STRING-REF" (unsafe "##STRING-REF")) |
| 11137 |
(def-spec "STRING-SET!" (unsafe "##STRING-SET!")) |
| 11138 |
(def-spec "TOUCH" (safe "##TOUCH")) |
| 11139 |
(let ((targ (make-target 4 'm68000))) |
| 11140 |
(target-begin!-set! targ (lambda (info-port) (begin! info-port targ))) |
| 11141 |
(put-target targ)) |
| 11142 |
)) ; dummy3 |
| 11143 |
|
| 11144 |
(define input-source-code ' |
| 11145 |
(begin |
| 11146 |
(declare (standard-bindings) (fixnum) (not safe) (block)) |
| 11147 |
(define (fib n) |
| 11148 |
(if (< n 2) |
| 11149 |
n |
| 11150 |
(+ (fib (- n 1)) |
| 11151 |
(fib (- n 2))))) |
| 11152 |
|
| 11153 |
(define (tak x y z) |
| 11154 |
(if (not (< y x)) |
| 11155 |
z |
| 11156 |
(tak (tak (- x 1) y z) |
| 11157 |
(tak (- y 1) z x) |
| 11158 |
(tak (- z 1) x y)))) |
| 11159 |
|
| 11160 |
(define (ack m n) |
| 11161 |
(cond ((= m 0) (+ n 1)) |
| 11162 |
((= n 0) (ack (- m 1) 1)) |
| 11163 |
(else (ack (- m 1) (ack m (- n 1)))))) |
| 11164 |
|
| 11165 |
(define (create-x n) |
| 11166 |
(define result (make-vector n)) |
| 11167 |
(do ((i 0 (+ i 1))) |
| 11168 |
((>= i n) result) |
| 11169 |
(vector-set! result i i))) |
| 11170 |
|
| 11171 |
(define (create-y x) |
| 11172 |
(let* ((n (vector-length x)) |
| 11173 |
(result (make-vector n))) |
| 11174 |
(do ((i (- n 1) (- i 1))) |
| 11175 |
((< i 0) result) |
| 11176 |
(vector-set! result i (vector-ref x i))))) |
| 11177 |
|
| 11178 |
(define (my-try n) |
| 11179 |
(vector-length (create-y (create-x n)))) |
| 11180 |
|
| 11181 |
(define (go n) |
| 11182 |
(let loop ((repeat 100) |
| 11183 |
(result 0)) |
| 11184 |
(if (> repeat 0) |
| 11185 |
(loop (- repeat 1) (my-try n)) |
| 11186 |
result))) |
| 11187 |
|
| 11188 |
(+ (fib 20) |
| 11189 |
(tak 18 12 6) |
| 11190 |
(ack 3 9) |
| 11191 |
(go 200000)) |
| 11192 |
)) |
| 11193 |
|
| 11194 |
(define output-expected '( |
| 11195 |
"|------------------------------------------------------" |
| 11196 |
"| #[primitive #!program] =" |
| 11197 |
"L1:" |
| 11198 |
" cmpw #1,d0" |
| 11199 |
" beq L1000" |
| 11200 |
" TRAP1(9,0)" |
| 11201 |
" LBL_PTR(L1)" |
| 11202 |
"L1000:" |
| 11203 |
" MOVE_PROC(1,a1)" |
| 11204 |
" movl a1,GLOB(fib)" |
| 11205 |
" MOVE_PROC(2,a1)" |
| 11206 |
" movl a1,GLOB(tak)" |
| 11207 |
" MOVE_PROC(3,a1)" |
| 11208 |
" movl a1,GLOB(ack)" |
| 11209 |
" MOVE_PROC(4,a1)" |
| 11210 |
" movl a1,GLOB(create-x)" |
| 11211 |
" MOVE_PROC(5,a1)" |
| 11212 |
" movl a1,GLOB(create-y)" |
| 11213 |
" MOVE_PROC(6,a1)" |
| 11214 |
" movl a1,GLOB(my-try)" |
| 11215 |
" MOVE_PROC(7,a1)" |
| 11216 |
" movl a1,GLOB(go)" |
| 11217 |
" movl a0,sp@-" |
| 11218 |
" movl #160,d1" |
| 11219 |
" lea L2,a0" |
| 11220 |
" dbra d5,L1001" |
| 11221 |
" moveq #9,d5" |
| 11222 |
" cmpl a5@,sp" |
| 11223 |
" bcc L1001" |
| 11224 |
" TRAP2(24)" |
| 11225 |
" RETURN(L1,1,1)" |
| 11226 |
"L1002:" |
| 11227 |
"L1001:" |
| 11228 |
" JMP_PROC(1,10)" |
| 11229 |
" RETURN(L1,1,1)" |
| 11230 |
"L2:" |
| 11231 |
" movl d1,sp@-" |
| 11232 |
" moveq #48,d3" |
| 11233 |
" moveq #96,d2" |
| 11234 |
" movl #144,d1" |
| 11235 |
" lea L3,a0" |
| 11236 |
" JMP_PROC(2,14)" |
| 11237 |
" RETURN(L1,2,1)" |
| 11238 |
"L3:" |
| 11239 |
" movl d1,sp@-" |
| 11240 |
" moveq #72,d2" |
| 11241 |
" moveq #24,d1" |
| 11242 |
" lea L4,a0" |
| 11243 |
" JMP_PROC(3,10)" |
| 11244 |
" RETURN(L1,3,1)" |
| 11245 |
"L4:" |
| 11246 |
" movl d1,sp@-" |
| 11247 |
" movl #1600000,d1" |
| 11248 |
" lea L5,a0" |
| 11249 |
" JMP_PROC(7,10)" |
| 11250 |
" RETURN(L1,4,1)" |
| 11251 |
"L5:" |
| 11252 |
" dbra d5,L1003" |
| 11253 |
" moveq #9,d5" |
| 11254 |
" cmpl a5@,sp" |
| 11255 |
" bcc L1003" |
| 11256 |
" TRAP2(24)" |
| 11257 |
" RETURN(L1,4,1)" |
| 11258 |
"L1004:" |
| 11259 |
"L1003:" |
| 11260 |
"L6:" |
| 11261 |
" addl sp@(8),d1" |
| 11262 |
" addl sp@(4),d1" |
| 11263 |
" addl sp@+,d1" |
| 11264 |
" addql #8,sp" |
| 11265 |
" rts" |
| 11266 |
"L0:" |
| 11267 |
"|------------------------------------------------------" |
| 11268 |
"| #[primitive fib] =" |
| 11269 |
"L1:" |
| 11270 |
" bmi L1000" |
| 11271 |
" TRAP1(9,1)" |
| 11272 |
" LBL_PTR(L1)" |
| 11273 |
"L1000:" |
| 11274 |
" moveq #16,d0" |
| 11275 |
" cmpl d1,d0" |
| 11276 |
" ble L3" |
| 11277 |
" bra L4" |
| 11278 |
" RETURN(L1,2,1)" |
| 11279 |
"L2:" |
| 11280 |
" movl d1,sp@-" |
| 11281 |
" movl sp@(4),d1" |
| 11282 |
" moveq #-16,d0" |
| 11283 |
" addl d0,d1" |
| 11284 |
" lea L5,a0" |
| 11285 |
" moveq #16,d0" |
| 11286 |
" cmpl d1,d0" |
| 11287 |
" bgt L4" |
| 11288 |
"L3:" |
| 11289 |
" movl a0,sp@-" |
| 11290 |
" movl d1,sp@-" |
| 11291 |
" subql #8,d1" |
| 11292 |
" lea L2,a0" |
| 11293 |
" dbra d5,L1001" |
| 11294 |
" moveq #9,d5" |
| 11295 |
" cmpl a5@,sp" |
| 11296 |
" bcc L1001" |
| 11297 |
" TRAP2(24)" |
| 11298 |
" RETURN(L1,2,1)" |
| 11299 |
"L1002:" |
| 11300 |
"L1001:" |
| 11301 |
" moveq #16,d0" |
| 11302 |
" cmpl d1,d0" |
| 11303 |
" ble L3" |
| 11304 |
"L4:" |
| 11305 |
" jmp a0@" |
| 11306 |
" RETURN(L1,3,1)" |
| 11307 |
"L5:" |
| 11308 |
" addl sp@+,d1" |
| 11309 |
" dbra d5,L1003" |
| 11310 |
" moveq #9,d5" |
| 11311 |
" cmpl a5@,sp" |
| 11312 |
" bcc L1003" |
| 11313 |
" TRAP2(24)" |
| 11314 |
" RETURN(L1,2,1)" |
| 11315 |
"L1004:" |
| 11316 |
"L1003:" |
| 11317 |
" addql #4,sp" |
| 11318 |
" rts" |
| 11319 |
"L0:" |
| 11320 |
"|------------------------------------------------------" |
| 11321 |
"| #[primitive tak] =" |
| 11322 |
"L1:" |
| 11323 |
" cmpw #4,d0" |
| 11324 |
" beq L1000" |
| 11325 |
" TRAP1(9,3)" |
| 11326 |
" LBL_PTR(L1)" |
| 11327 |
"L1000:" |
| 11328 |
" cmpl d1,d2" |
| 11329 |
" bge L4" |
| 11330 |
" bra L3" |
| 11331 |
" RETURN(L1,6,1)" |
| 11332 |
"L2:" |
| 11333 |
" movl d1,d3" |
| 11334 |
" movl sp@(20),a0" |
| 11335 |
" movl sp@+,d2" |
| 11336 |
" movl sp@+,d1" |
| 11337 |
" dbra d5,L1001" |
| 11338 |
" moveq #9,d5" |
| 11339 |
" cmpl a5@,sp" |
| 11340 |
" bcc L1001" |
| 11341 |
" movl a0,sp@(12)" |
| 11342 |
" TRAP2(24)" |
| 11343 |
" RETURN(L1,4,1)" |
| 11344 |
"L1002:" |
| 11345 |
" movl sp@(12),a0" |
| 11346 |
"L1001:" |
| 11347 |
" cmpl d1,d2" |
| 11348 |
" lea sp@(16),sp" |
| 11349 |
" bge L4" |
| 11350 |
"L3:" |
| 11351 |
" movl a0,sp@-" |
| 11352 |
" movl d1,sp@-" |
| 11353 |
" movl d2,sp@-" |
| 11354 |
" movl d3,sp@-" |
| 11355 |
" subql #8,d1" |
| 11356 |
" lea L5,a0" |
| 11357 |
" dbra d5,L1003" |
| 11358 |
" moveq #9,d5" |
| 11359 |
" cmpl a5@,sp" |
| 11360 |
" bcc L1003" |
| 11361 |
" TRAP2(24)" |
| 11362 |
" RETURN(L1,4,1)" |
| 11363 |
"L1004:" |
| 11364 |
"L1003:" |
| 11365 |
" cmpl d1,d2" |
| 11366 |
" blt L3" |
| 11367 |
"L4:" |
| 11368 |
" movl d3,d1" |
| 11369 |
" jmp a0@" |
| 11370 |
" RETURN(L1,4,1)" |
| 11371 |
"L5:" |
| 11372 |
" movl d1,sp@-" |
| 11373 |
" movl sp@(12),d3" |
| 11374 |
" movl sp@(4),d2" |
| 11375 |
" movl sp@(8),d1" |
| 11376 |
" subql #8,d1" |
| 11377 |
" lea L6,a0" |
| 11378 |
" cmpl d1,d2" |
| 11379 |
" bge L4" |
| 11380 |
" bra L3" |
| 11381 |
" RETURN(L1,5,1)" |
| 11382 |
"L6:" |
| 11383 |
" movl d1,sp@-" |
| 11384 |
" movl sp@(12),d3" |
| 11385 |
" movl sp@(16),d2" |
| 11386 |
" movl sp@(8),d1" |
| 11387 |
" subql #8,d1" |
| 11388 |
" lea L2,a0" |
| 11389 |
" cmpl d1,d2" |
| 11390 |
" bge L4" |
| 11391 |
" bra L3" |
| 11392 |
"L0:" |
| 11393 |
"|------------------------------------------------------" |
| 11394 |
"| #[primitive ack] =" |
| 11395 |
"L1:" |
| 11396 |
" beq L1000" |
| 11397 |
" TRAP1(9,2)" |
| 11398 |
" LBL_PTR(L1)" |
| 11399 |
"L1000:" |
| 11400 |
" movl d1,d0" |
| 11401 |
" bne L3" |
| 11402 |
" bra L5" |
| 11403 |
" RETURN(L1,2,1)" |
| 11404 |
"L2:" |
| 11405 |
" movl d1,d2" |
| 11406 |
" movl sp@+,d1" |
| 11407 |
" subql #8,d1" |
| 11408 |
" movl sp@+,a0" |
| 11409 |
" dbra d5,L1001" |
| 11410 |
" moveq #9,d5" |
| 11411 |
" cmpl a5@,sp" |
| 11412 |
" bcc L1001" |
| 11413 |
" movl a0,sp@-" |
| 11414 |
" TRAP2(24)" |
| 11415 |
" RETURN(L1,1,1)" |
| 11416 |
"L1002:" |
| 11417 |
" movl sp@+,a0" |
| 11418 |
"L1001:" |
| 11419 |
" movl d1,d0" |
| 11420 |
" beq L5" |
| 11421 |
"L3:" |
| 11422 |
" movl d2,d0" |
| 11423 |
" bne L6" |
| 11424 |
"L4:" |
| 11425 |
" subql #8,d1" |
| 11426 |
" moveq #8,d2" |
| 11427 |
" dbra d5,L1003" |
| 11428 |
" moveq #9,d5" |
| 11429 |
" cmpl a5@,sp" |
| 11430 |
" bcc L1003" |
| 11431 |
" movl a0,sp@-" |
| 11432 |
" TRAP2(24)" |
| 11433 |
" RETURN(L1,1,1)" |
| 11434 |
"L1004:" |
| 11435 |
" movl sp@+,a0" |
| 11436 |
"L1003:" |
| 11437 |
" movl d1,d0" |
| 11438 |
" bne L3" |
| 11439 |
"L5:" |
| 11440 |
" movl d2,d1" |
| 11441 |
" addql #8,d1" |
| 11442 |
" jmp a0@" |
| 11443 |
"L6:" |
| 11444 |
" movl a0,sp@-" |
| 11445 |
" movl d1,sp@-" |
| 11446 |
" movl d2,d1" |
| 11447 |
" subql #8,d1" |
| 11448 |
" movl d1,d2" |
| 11449 |
" movl sp@,d1" |
| 11450 |
" lea L2,a0" |
| 11451 |
" dbra d5,L1005" |
| 11452 |
" moveq #9,d5" |
| 11453 |
" cmpl a5@,sp" |
| 11454 |
" bcc L1005" |
| 11455 |
" TRAP2(24)" |
| 11456 |
" RETURN(L1,2,1)" |
| 11457 |
"L1006:" |
| 11458 |
"L1005:" |
| 11459 |
" movl d1,d0" |
| 11460 |
" bne L3" |
| 11461 |
" bra L5" |
| 11462 |
"L0:" |
| 11463 |
"|------------------------------------------------------" |
| 11464 |
"| #[primitive create-x] =" |
| 11465 |
"L1:" |
| 11466 |
" bmi L1000" |
| 11467 |
" TRAP1(9,1)" |
| 11468 |
" LBL_PTR(L1)" |
| 11469 |
"L1000:" |
| 11470 |
" movl a0,sp@-" |
| 11471 |
" movl d1,sp@-" |
| 11472 |
" lea L2,a0" |
| 11473 |
" dbra d5,L1001" |
| 11474 |
" moveq #9,d5" |
| 11475 |
" cmpl a5@,sp" |
| 11476 |
" bcc L1001" |
| 11477 |
" TRAP2(24)" |
| 11478 |
" RETURN(L1,2,1)" |
| 11479 |
"L1002:" |
| 11480 |
"L1001:" |
| 11481 |
" moveq #-1,d0" |
| 11482 |
" JMP_PRIM(make-vector,0)" |
| 11483 |
" RETURN(L1,2,1)" |
| 11484 |
"L2:" |
| 11485 |
" movl d1,d2" |
| 11486 |
" movl sp@+,d1" |
| 11487 |
" moveq #0,d3" |
| 11488 |
" movl sp@+,a0" |
| 11489 |
" dbra d5,L1003" |
| 11490 |
" moveq #9,d5" |
| 11491 |
" cmpl a5@,sp" |
| 11492 |
" bcc L1003" |
| 11493 |
" movl a0,sp@-" |
| 11494 |
" TRAP2(24)" |
| 11495 |
" RETURN(L1,1,1)" |
| 11496 |
"L1004:" |
| 11497 |
" movl sp@+,a0" |
| 11498 |
"L1003:" |
| 11499 |
" cmpl d1,d3" |
| 11500 |
" bge L4" |
| 11501 |
"L3:" |
| 11502 |
" movl d3,d0" |
| 11503 |
" asrl #1,d0" |
| 11504 |
" movl d2,a1" |
| 11505 |
" movl d3,a1@(1,d0:l)" |
| 11506 |
" addql #8,d3" |
| 11507 |
" dbra d5,L1005" |
| 11508 |
" moveq #9,d5" |
| 11509 |
" cmpl a5@,sp" |
| 11510 |
" bcc L1005" |
| 11511 |
" movl a0,sp@-" |
| 11512 |
" TRAP2(24)" |
| 11513 |
" RETURN(L1,1,1)" |
| 11514 |
"L1006:" |
| 11515 |
" movl sp@+,a0" |
| 11516 |
"L1005:" |
| 11517 |
" cmpl d1,d3" |
| 11518 |
" blt L3" |
| 11519 |
"L4:" |
| 11520 |
" movl d2,d1" |
| 11521 |
" jmp a0@" |
| 11522 |
"L0:" |
| 11523 |
"|------------------------------------------------------" |
| 11524 |
"| #[primitive create-y] =" |
| 11525 |
"L1:" |
| 11526 |
" bmi L1000" |
| 11527 |
" TRAP1(9,1)" |
| 11528 |
" LBL_PTR(L1)" |
| 11529 |
"L1000:" |
| 11530 |
" movl d1,a1" |
| 11531 |
" movl a1@(-3),d2" |
| 11532 |
" lsrl #7,d2" |
| 11533 |
" movl a0,sp@-" |
| 11534 |
" movl d1,sp@-" |
| 11535 |
" movl d2,sp@-" |
| 11536 |
" movl d2,d1" |
| 11537 |
" lea L2,a0" |
| 11538 |
" dbra d5,L1001" |
| 11539 |
" moveq #9,d5" |
| 11540 |
" cmpl a5@,sp" |
| 11541 |
" bcc L1001" |
| 11542 |
" TRAP2(24)" |
| 11543 |
" RETURN(L1,3,1)" |
| 11544 |
"L1002:" |
| 11545 |
"L1001:" |
| 11546 |
" moveq #-1,d0" |
| 11547 |
" JMP_PRIM(make-vector,0)" |
| 11548 |
" RETURN(L1,3,1)" |
| 11549 |
"L2:" |
| 11550 |
" movl sp@+,d2" |
| 11551 |
" subql #8,d2" |
| 11552 |
" movl d2,d3" |
| 11553 |
" movl d1,d2" |
| 11554 |
" movl sp@+,d1" |
| 11555 |
" movl sp@+,a0" |
| 11556 |
" dbra d5,L1003" |
| 11557 |
" moveq #9,d5" |
| 11558 |
" cmpl a5@,sp" |
| 11559 |
" bcc L1003" |
| 11560 |
" movl a0,sp@-" |
| 11561 |
" TRAP2(24)" |
| 11562 |
" RETURN(L1,1,1)" |
| 11563 |
"L1004:" |
| 11564 |
" movl sp@+,a0" |
| 11565 |
"L1003:" |
| 11566 |
" movl d3,d0" |
| 11567 |
" blt L4" |
| 11568 |
"L3:" |
| 11569 |
" movl d3,d0" |
| 11570 |
" asrl #1,d0" |
| 11571 |
" movl d1,a1" |
| 11572 |
" movl a1@(1,d0:l),d4" |
| 11573 |
" movl d3,d0" |
| 11574 |
" asrl #1,d0" |
| 11575 |
" movl d2,a1" |
| 11576 |
" movl d4,a1@(1,d0:l)" |
| 11577 |
" subql #8,d3" |
| 11578 |
" dbra d5,L1005" |
| 11579 |
" moveq #9,d5" |
| 11580 |
" cmpl a5@,sp" |
| 11581 |
" bcc L1005" |
| 11582 |
" movl a0,sp@-" |
| 11583 |
" TRAP2(24)" |
| 11584 |
" RETURN(L1,1,1)" |
| 11585 |
"L1006:" |
| 11586 |
" movl sp@+,a0" |
| 11587 |
"L1005:" |
| 11588 |
" movl d3,d0" |
| 11589 |
" bge L3" |
| 11590 |
"L4:" |
| 11591 |
" movl d2,d1" |
| 11592 |
" jmp a0@" |
| 11593 |
"L0:" |
| 11594 |
"|------------------------------------------------------" |
| 11595 |
"| #[primitive my-try] =" |
| 11596 |
"L1:" |
| 11597 |
" bmi L1000" |
| 11598 |
" TRAP1(9,1)" |
| 11599 |
" LBL_PTR(L1)" |
| 11600 |
"L1000:" |
| 11601 |
" movl a0,sp@-" |
| 11602 |
" lea L2,a0" |
| 11603 |
" dbra d5,L1001" |
| 11604 |
" moveq #9,d5" |
| 11605 |
" cmpl a5@,sp" |
| 11606 |
" bcc L1001" |
| 11607 |
" TRAP2(24)" |
| 11608 |
" RETURN(L1,1,1)" |
| 11609 |
"L1002:" |
| 11610 |
"L1001:" |
| 11611 |
" JMP_PROC(4,10)" |
| 11612 |
" RETURN(L1,1,1)" |
| 11613 |
"L2:" |
| 11614 |
" lea L3,a0" |
| 11615 |
" JMP_PROC(5,10)" |
| 11616 |
" RETURN(L1,1,1)" |
| 11617 |
"L3:" |
| 11618 |
" movl d1,a1" |
| 11619 |
" movl a1@(-3),d1" |
| 11620 |
" lsrl #7,d1" |
| 11621 |
" dbra d5,L1003" |
| 11622 |
" moveq #9,d5" |
| 11623 |
" cmpl a5@,sp" |
| 11624 |
" bcc L1003" |
| 11625 |
" TRAP2(24)" |
| 11626 |
" RETURN(L1,1,1)" |
| 11627 |
"L1004:" |
| 11628 |
"L1003:" |
| 11629 |
" rts" |
| 11630 |
"L0:" |
| 11631 |
"|------------------------------------------------------" |
| 11632 |
"| #[primitive go] =" |
| 11633 |
"L1:" |
| 11634 |
" bmi L1000" |
| 11635 |
" TRAP1(9,1)" |
| 11636 |
" LBL_PTR(L1)" |
| 11637 |
"L1000:" |
| 11638 |
" moveq #0,d3" |
| 11639 |
" movl #800,d2" |
| 11640 |
" dbra d5,L1001" |
| 11641 |
" moveq #9,d5" |
| 11642 |
" cmpl a5@,sp" |
| 11643 |
" bcc L1001" |
| 11644 |
" movl a0,sp@-" |
| 11645 |
" TRAP2(24)" |
| 11646 |
" RETURN(L1,1,1)" |
| 11647 |
"L1002:" |
| 11648 |
" movl sp@+,a0" |
| 11649 |
"L1001:" |
| 11650 |
" movl d2,d0" |
| 11651 |
" ble L4" |
| 11652 |
" bra L3" |
| 11653 |
" RETURN(L1,3,1)" |
| 11654 |
"L2:" |
| 11655 |
" movl d1,d3" |
| 11656 |
" movl sp@+,d1" |
| 11657 |
" subql #8,d1" |
| 11658 |
" movl d1,d2" |
| 11659 |
" movl sp@+,d1" |
| 11660 |
" movl sp@+,a0" |
| 11661 |
" dbra d5,L1003" |
| 11662 |
" moveq #9,d5" |
| 11663 |
" cmpl a5@,sp" |
| 11664 |
" bcc L1003" |
| 11665 |
" movl a0,sp@-" |
| 11666 |
" TRAP2(24)" |
| 11667 |
" RETURN(L1,1,1)" |
| 11668 |
"L1004:" |
| 11669 |
" movl sp@+,a0" |
| 11670 |
"L1003:" |
| 11671 |
" movl d2,d0" |
| 11672 |
" ble L4" |
| 11673 |
"L3:" |
| 11674 |
" movl a0,sp@-" |
| 11675 |
" movl d1,sp@-" |
| 11676 |
" movl d2,sp@-" |
| 11677 |
" lea L2,a0" |
| 11678 |
" dbra d5,L1005" |
| 11679 |
" moveq #9,d5" |
| 11680 |
" cmpl a5@,sp" |
| 11681 |
" bcc L1005" |
| 11682 |
" TRAP2(24)" |
| 11683 |
" RETURN(L1,3,1)" |
| 11684 |
"L1006:" |
| 11685 |
"L1005:" |
| 11686 |
" JMP_PROC(6,10)" |
| 11687 |
"L4:" |
| 11688 |
" movl d3,d1" |
| 11689 |
" jmp a0@" |
| 11690 |
"L0:" |
| 11691 |
"")) |
| 11692 |
|
| 11693 |
(define (main . args) |
| 11694 |
(run-benchmark |
| 11695 |
"compiler" |
| 11696 |
compiler-iters |
| 11697 |
(lambda (result) |
| 11698 |
(equal? result output-expected)) |
| 11699 |
(lambda (expr target opt) (lambda () (ce expr target opt) (asm-output-get))) |
| 11700 |
input-source-code |
| 11701 |
'm68000 |
| 11702 |
'asm))) |