| 1 |
;;; Ikarus Scheme -- A compiler for R6RS Scheme. |
| 2 |
;;; Copyright (C) 2006,2007,2008 Abdulaziz Ghuloum |
| 3 |
;;; |
| 4 |
;;; This program is free software: you can redistribute it and/or modify |
| 5 |
;;; it under the terms of the GNU General Public License version 3 as |
| 6 |
;;; published by the Free Software Foundation. |
| 7 |
;;; |
| 8 |
;;; This program is distributed in the hope that it will be useful, but |
| 9 |
;;; WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 |
;;; General Public License for more details. |
| 12 |
;;; |
| 13 |
;;; You should have received a copy of the GNU General Public License |
| 14 |
;;; along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
|
| 16 |
|
| 17 |
(define-syntax section |
| 18 |
(syntax-rules (/section) |
| 19 |
[(section e* ... /section) (begin e* ...)])) |
| 20 |
|
| 21 |
(section ;;; helpers |
| 22 |
|
| 23 |
(define (prm op . arg*) |
| 24 |
(make-primcall op arg*)) |
| 25 |
|
| 26 |
(define (nop) (make-primcall 'nop '())) |
| 27 |
|
| 28 |
(define (K x) (make-constant x)) |
| 29 |
|
| 30 |
|
| 31 |
(define (tag-test x mask tag) |
| 32 |
(if mask |
| 33 |
(prm '= (prm 'logand x (K mask)) (K tag)) |
| 34 |
(prm '= x (K tag)))) |
| 35 |
|
| 36 |
(define (sec-tag-test x pmask ptag smask stag) |
| 37 |
(make-conditional |
| 38 |
(tag-test x pmask ptag) |
| 39 |
(tag-test (prm 'mref x (K (- ptag))) smask stag) |
| 40 |
(make-constant #f))) |
| 41 |
|
| 42 |
(define (dirty-vector-set address) |
| 43 |
(define shift-bits 2) |
| 44 |
(prm 'mset32 |
| 45 |
(prm 'mref pcr (K pcb-dirty-vector)) |
| 46 |
(prm 'sll (prm 'srl address (K pageshift)) (K shift-bits)) |
| 47 |
(K dirty-word))) |
| 48 |
|
| 49 |
(define (smart-dirty-vector-set addr what) |
| 50 |
(struct-case what |
| 51 |
[(constant t) |
| 52 |
(if (or (fx? t) (immediate? t)) |
| 53 |
(prm 'nop) |
| 54 |
(dirty-vector-set addr))] |
| 55 |
[(known x t) |
| 56 |
(cond |
| 57 |
[(eq? (T:immediate? t) 'yes) |
| 58 |
(record-optimization 'smart-dirty-vec t) |
| 59 |
(nop)] |
| 60 |
[else (smart-dirty-vector-set addr x)])] |
| 61 |
[else (dirty-vector-set addr)])) |
| 62 |
|
| 63 |
(define (slow-mem-assign v x i) |
| 64 |
(with-tmp ([t (prm 'int+ x (K i))]) |
| 65 |
(make-seq |
| 66 |
(prm 'mset t (K 0) (T v)) |
| 67 |
(dirty-vector-set t)))) |
| 68 |
|
| 69 |
(define (mem-assign v x i) |
| 70 |
(struct-case v |
| 71 |
[(constant t) |
| 72 |
(if (or (fx? t) (immediate? t)) |
| 73 |
(prm 'mset x (K i) (T v)) |
| 74 |
(slow-mem-assign v x i))] |
| 75 |
[(known expr t) |
| 76 |
(cond |
| 77 |
[(eq? (T:immediate? t) 'yes) |
| 78 |
(record-optimization 'mem-assign v) |
| 79 |
(prm 'mset x (K i) (T expr))] |
| 80 |
[else (slow-mem-assign expr x i)])] |
| 81 |
[else (slow-mem-assign v x i)])) |
| 82 |
|
| 83 |
(define (align-code unknown-amt known-amt) |
| 84 |
(prm 'sll |
| 85 |
(prm 'sra |
| 86 |
(prm 'int+ unknown-amt |
| 87 |
(K (+ known-amt (sub1 object-alignment)))) |
| 88 |
(K align-shift)) |
| 89 |
(K align-shift))) |
| 90 |
/section) |
| 91 |
|
| 92 |
(section ;;; simple objects section |
| 93 |
|
| 94 |
(define-primop base-rtd safe |
| 95 |
[(V) (prm 'mref pcr (K pcb-base-rtd))] |
| 96 |
[(P) (K #t)] |
| 97 |
[(E) (prm 'nop)]) |
| 98 |
|
| 99 |
(define-primop void safe |
| 100 |
[(V) (K void-object)] |
| 101 |
[(P) (K #t)] |
| 102 |
[(E) (prm 'nop)]) |
| 103 |
|
| 104 |
(define-primop nop unsafe |
| 105 |
[(E) (prm 'nop)]) |
| 106 |
|
| 107 |
(define-primop neq? unsafe |
| 108 |
[(P x y) (prm '!= (T x) (T y))] |
| 109 |
[(E x y) (nop)]) |
| 110 |
|
| 111 |
(define-primop eq? safe |
| 112 |
[(P x y) (prm '= (T x) (T y))] |
| 113 |
[(E x y) (nop)]) |
| 114 |
|
| 115 |
(define (equable-constant? x) |
| 116 |
(struct-case x |
| 117 |
[(constant xv) (equable? xv)] |
| 118 |
[(known x t) (equable-constant? x)] |
| 119 |
[else #f])) |
| 120 |
|
| 121 |
(define-primop eqv? safe |
| 122 |
[(P x y) |
| 123 |
(if (or (equable-constant? x) |
| 124 |
(equable-constant? y)) |
| 125 |
(prm '= (T x) (T y)) |
| 126 |
(interrupt))] |
| 127 |
[(E x y) (nop)]) |
| 128 |
|
| 129 |
(define-primop null? safe |
| 130 |
[(P x) (prm '= (T x) (K nil))] |
| 131 |
[(E x) (nop)]) |
| 132 |
|
| 133 |
(define-primop not safe |
| 134 |
[(P x) (prm '= (T x) (K bool-f))] |
| 135 |
[(E x) (nop)]) |
| 136 |
|
| 137 |
(define-primop eof-object safe |
| 138 |
[(V) (K eof)] |
| 139 |
[(P) (K #t)] |
| 140 |
[(E) (nop)]) |
| 141 |
|
| 142 |
(define-primop eof-object? safe |
| 143 |
[(P x) (prm '= (T x) (K eof))] |
| 144 |
[(E x) (nop)]) |
| 145 |
|
| 146 |
(define-primop $unbound-object? unsafe |
| 147 |
[(P x) (prm '= (T x) (K unbound))] |
| 148 |
[(E x) (nop)]) |
| 149 |
|
| 150 |
(define-primop immediate? safe |
| 151 |
[(P x) |
| 152 |
(make-conditional |
| 153 |
(tag-test (T x) fx-mask fx-tag) |
| 154 |
(make-constant #t) |
| 155 |
(tag-test (T x) 7 7))] |
| 156 |
[(E x) (nop)]) |
| 157 |
|
| 158 |
(define-primop boolean? safe |
| 159 |
[(P x) |
| 160 |
(tag-test (T x) bool-mask bool-tag)] |
| 161 |
[(E x) (nop)]) |
| 162 |
|
| 163 |
(define-primop bwp-object? safe |
| 164 |
[(P x) (prm '= (T x) (K bwp-object))] |
| 165 |
[(E x) (nop)]) |
| 166 |
|
| 167 |
(define-primop $forward-ptr? unsafe |
| 168 |
[(P x) (prm '= (T x) (K -1))] |
| 169 |
[(E x) (nop)]) |
| 170 |
|
| 171 |
(define-primop pointer-value unsafe |
| 172 |
[(V x) (prm 'logand |
| 173 |
(prm 'srl (T x) (K 1)) |
| 174 |
(K (* -1 fx-scale)))] |
| 175 |
[(P x) (K #t)] |
| 176 |
[(E x) (nop)]) |
| 177 |
|
| 178 |
(define-primop $arg-list unsafe |
| 179 |
[(V) (prm 'mref pcr (K pcb-arg-list))] |
| 180 |
[(P) (K #t)] |
| 181 |
[(E) (nop)]) |
| 182 |
|
| 183 |
(define-primop $collect-key unsafe |
| 184 |
[(V) (prm 'mref pcr (K pcb-collect-key))] |
| 185 |
[(E x) (prm 'mset pcr (K pcb-collect-key) (T x))]) |
| 186 |
|
| 187 |
(define-primop $memq safe |
| 188 |
[(P x ls) |
| 189 |
(struct-case ls |
| 190 |
[(constant ls) |
| 191 |
(cond |
| 192 |
[(not (list? ls)) (interrupt)] |
| 193 |
[else |
| 194 |
(with-tmp ([x (T x)]) |
| 195 |
(let f ([ls ls]) |
| 196 |
(cond |
| 197 |
[(null? ls) (K #f)] |
| 198 |
[(null? (cdr ls)) (prm '= x (T (K (car ls))))] |
| 199 |
[else |
| 200 |
(make-conditional |
| 201 |
(prm '= x (T (K (car ls)))) |
| 202 |
(K #t) |
| 203 |
(f (cdr ls)))])))])] |
| 204 |
[(known expr t) |
| 205 |
(cogen-pred-$memq x expr)] |
| 206 |
[else (interrupt)])] |
| 207 |
[(V x ls) |
| 208 |
(struct-case ls |
| 209 |
[(constant ls) |
| 210 |
(cond |
| 211 |
[(not (list? ls)) (interrupt)] |
| 212 |
[else |
| 213 |
(with-tmp ([x (T x)]) |
| 214 |
(let f ([ls ls]) |
| 215 |
(cond |
| 216 |
[(null? ls) (K bool-f)] |
| 217 |
[else |
| 218 |
(make-conditional |
| 219 |
(prm '= x (T (K (car ls)))) |
| 220 |
(T (K ls)) |
| 221 |
(f (cdr ls)))])))])] |
| 222 |
[(known expr t) |
| 223 |
(cogen-value-$memq x expr)] |
| 224 |
[else (interrupt)])] |
| 225 |
[(E x ls) (nop)]) |
| 226 |
|
| 227 |
(define-primop memq safe |
| 228 |
[(P x ls) (cogen-pred-$memq x ls)] |
| 229 |
[(V x ls) (cogen-value-$memq x ls)] |
| 230 |
[(E x ls) |
| 231 |
(struct-case ls |
| 232 |
[(constant ls) |
| 233 |
(cond |
| 234 |
[(list? ls) (nop)] |
| 235 |
[else (interrupt)])] |
| 236 |
[(known expr t) |
| 237 |
(cogen-effect-memq x expr)] |
| 238 |
[else (interrupt)])]) |
| 239 |
|
| 240 |
(define (equable? x) |
| 241 |
(or (fx? x) (not (number? x)))) |
| 242 |
|
| 243 |
(define-primop memv safe |
| 244 |
[(V x ls) |
| 245 |
(struct-case ls |
| 246 |
[(constant lsv) |
| 247 |
(cond |
| 248 |
[(and (list? lsv) (andmap equable? lsv)) |
| 249 |
(cogen-value-$memq x ls)] |
| 250 |
[else (interrupt)])] |
| 251 |
[(known expr t) |
| 252 |
(cogen-value-memv x expr)] |
| 253 |
[else (interrupt)])] |
| 254 |
[(P x ls) |
| 255 |
(struct-case ls |
| 256 |
[(constant lsv) |
| 257 |
(cond |
| 258 |
[(and (list? lsv) (andmap equable? lsv)) |
| 259 |
(cogen-pred-$memq x ls)] |
| 260 |
[else (interrupt)])] |
| 261 |
[(known expr t) |
| 262 |
(cogen-pred-memv x expr)] |
| 263 |
[else (interrupt)])] |
| 264 |
[(E x ls) |
| 265 |
(struct-case ls |
| 266 |
[(constant lsv) |
| 267 |
(cond |
| 268 |
[(list? lsv) (nop)] |
| 269 |
[else (interrupt)])] |
| 270 |
[(known expr t) |
| 271 |
(cogen-effect-memv x expr)] |
| 272 |
[else (interrupt)])]) |
| 273 |
|
| 274 |
/section) |
| 275 |
|
| 276 |
(section ;;; pairs |
| 277 |
|
| 278 |
(define-primop pair? safe |
| 279 |
[(P x) |
| 280 |
(tag-test (T x) pair-mask pair-tag)] |
| 281 |
[(E x) (nop)]) |
| 282 |
|
| 283 |
(define-primop cons safe |
| 284 |
[(V a d) |
| 285 |
(with-tmp ([t (prm 'alloc (K pair-size) (K pair-tag))]) |
| 286 |
(prm 'mset t (K (- disp-car pair-tag)) (T a)) |
| 287 |
(prm 'mset t (K (- disp-cdr pair-tag)) (T d)) |
| 288 |
t)] |
| 289 |
[(P a d) (K #t)] |
| 290 |
[(E a d) (prm 'nop)]) |
| 291 |
|
| 292 |
(define-primop $car unsafe |
| 293 |
[(V x) (prm 'mref (T x) (K (- disp-car pair-tag)))] |
| 294 |
[(E x) (nop)]) |
| 295 |
|
| 296 |
(define-primop $cdr unsafe |
| 297 |
[(V x) (prm 'mref (T x) (K (- disp-cdr pair-tag)))] |
| 298 |
[(E x) (nop)]) |
| 299 |
|
| 300 |
(define-primop $set-car! unsafe |
| 301 |
[(E x v) |
| 302 |
(with-tmp ([x (T x)]) |
| 303 |
(prm 'mset x (K (- disp-car pair-tag)) (T v)) |
| 304 |
(smart-dirty-vector-set x v))]) |
| 305 |
|
| 306 |
(define-primop $set-cdr! unsafe |
| 307 |
[(E x v) |
| 308 |
(with-tmp ([x (T x)]) |
| 309 |
(prm 'mset x (K (- disp-cdr pair-tag)) (T v)) |
| 310 |
(smart-dirty-vector-set x v))]) |
| 311 |
|
| 312 |
(define (assert-pair x) |
| 313 |
(struct-case x |
| 314 |
[(known x t) |
| 315 |
(case (T:pair? t) |
| 316 |
[(yes) (record-optimization 'assert-pair x) (nop)] |
| 317 |
[(no) (interrupt)] |
| 318 |
[else (assert-pair x)])] |
| 319 |
[else |
| 320 |
(interrupt-unless (tag-test x pair-mask pair-tag))])) |
| 321 |
|
| 322 |
(define-primop car safe |
| 323 |
[(V x) |
| 324 |
(with-tmp ([x (T x)]) |
| 325 |
(assert-pair x) |
| 326 |
(prm 'mref x (K (- disp-car pair-tag))))] |
| 327 |
[(E x) (assert-pair (T x))]) |
| 328 |
|
| 329 |
(define-primop cdr safe |
| 330 |
[(V x) |
| 331 |
(with-tmp ([x (T x)]) |
| 332 |
(assert-pair x) |
| 333 |
(prm 'mref x (K (- disp-cdr pair-tag))))] |
| 334 |
[(E x) (assert-pair (T x))]) |
| 335 |
|
| 336 |
(define-primop set-car! safe |
| 337 |
[(E x v) |
| 338 |
(with-tmp ([x (T x)]) |
| 339 |
(assert-pair x) |
| 340 |
(prm 'mset x (K (- disp-car pair-tag)) (T v)) |
| 341 |
(smart-dirty-vector-set x v))]) |
| 342 |
|
| 343 |
(define-primop set-cdr! safe |
| 344 |
[(E x v) |
| 345 |
(with-tmp ([x (T x)]) |
| 346 |
(assert-pair x) |
| 347 |
(prm 'mset x (K (- disp-cdr pair-tag)) (T v)) |
| 348 |
(smart-dirty-vector-set x v))]) |
| 349 |
|
| 350 |
|
| 351 |
(define (expand-cxr val ls) |
| 352 |
(cond |
| 353 |
[(null? ls) (T val)] |
| 354 |
[else |
| 355 |
(with-tmp ([x (expand-cxr val (cdr ls))]) |
| 356 |
(assert-pair x) |
| 357 |
(prm 'mref x |
| 358 |
(case (car ls) |
| 359 |
[(a) (K (- disp-car pair-tag))] |
| 360 |
[else (K (- disp-cdr pair-tag))])))])) |
| 361 |
|
| 362 |
(define-primop caar safe [(V x) (expand-cxr x '(a a))]) |
| 363 |
(define-primop cadr safe [(V x) (expand-cxr x '(a d))]) |
| 364 |
(define-primop cdar safe [(V x) (expand-cxr x '(d a))]) |
| 365 |
(define-primop cddr safe [(V x) (expand-cxr x '(d d))]) |
| 366 |
(define-primop caaar safe [(V x) (expand-cxr x '(a a a))]) |
| 367 |
(define-primop caadr safe [(V x) (expand-cxr x '(a a d))]) |
| 368 |
(define-primop cadar safe [(V x) (expand-cxr x '(a d a))]) |
| 369 |
(define-primop caddr safe [(V x) (expand-cxr x '(a d d))]) |
| 370 |
(define-primop cdaar safe [(V x) (expand-cxr x '(d a a))]) |
| 371 |
(define-primop cdadr safe [(V x) (expand-cxr x '(d a d))]) |
| 372 |
(define-primop cddar safe [(V x) (expand-cxr x '(d d a))]) |
| 373 |
(define-primop cdddr safe [(V x) (expand-cxr x '(d d d))]) |
| 374 |
;(define-primop caaaar safe [(V x) (expand-cxr x '(a a a a))]) |
| 375 |
;(define-primop caaadr safe [(V x) (expand-cxr x '(a a a d))]) |
| 376 |
;(define-primop caadar safe [(V x) (expand-cxr x '(a a d a))]) |
| 377 |
;(define-primop caaddr safe [(V x) (expand-cxr x '(a a d d))]) |
| 378 |
;(define-primop cadaar safe [(V x) (expand-cxr x '(a d a a))]) |
| 379 |
;(define-primop cadadr safe [(V x) (expand-cxr x '(a d a d))]) |
| 380 |
;(define-primop caddar safe [(V x) (expand-cxr x '(a d d a))]) |
| 381 |
(define-primop cadddr safe [(V x) (expand-cxr x '(a d d d))]) |
| 382 |
;(define-primop cdaaar safe [(V x) (expand-cxr x '(d a a a))]) |
| 383 |
;(define-primop cdaadr safe [(V x) (expand-cxr x '(d a a d))]) |
| 384 |
;(define-primop cdadar safe [(V x) (expand-cxr x '(d a d a))]) |
| 385 |
;(define-primop cdaddr safe [(V x) (expand-cxr x '(d a d d))]) |
| 386 |
;(define-primop cddaar safe [(V x) (expand-cxr x '(d d a a))]) |
| 387 |
;(define-primop cddadr safe [(V x) (expand-cxr x '(d d a d))]) |
| 388 |
;(define-primop cdddar safe [(V x) (expand-cxr x '(d d d a))]) |
| 389 |
;(define-primop cddddr safe [(V x) (expand-cxr x '(d d d d))]) |
| 390 |
|
| 391 |
|
| 392 |
(define-primop list safe |
| 393 |
[(V) (K nil)] |
| 394 |
[(V . arg*) |
| 395 |
(let ([n (length arg*)] [t* (map T arg*)]) |
| 396 |
(with-tmp ([v (prm 'alloc (K (align (* n pair-size))) (K pair-tag))]) |
| 397 |
(prm 'mset v (K (- disp-car pair-tag)) (car t*)) |
| 398 |
(prm 'mset v |
| 399 |
(K (- (+ disp-cdr (* (sub1 n) pair-size)) pair-tag)) |
| 400 |
(K nil)) |
| 401 |
(let f ([t* (cdr t*)] [i pair-size]) |
| 402 |
(cond |
| 403 |
[(null? t*) v] |
| 404 |
[else |
| 405 |
(with-tmp ([tmp (prm 'int+ v (K i))]) |
| 406 |
(prm 'mset tmp (K (- disp-car pair-tag)) (car t*)) |
| 407 |
(prm 'mset tmp (K (+ disp-cdr (- pair-size) (- pair-tag))) tmp) |
| 408 |
(f (cdr t*) (+ i pair-size)))]))))] |
| 409 |
[(P . arg*) (K #t)] |
| 410 |
[(E . arg*) (nop)]) |
| 411 |
|
| 412 |
(define-primop cons* safe |
| 413 |
[(V) (interrupt)] |
| 414 |
[(V x) (T x)] |
| 415 |
[(V a . a*) |
| 416 |
(let ([t* (map T a*)] [n (length a*)]) |
| 417 |
(with-tmp ([v (prm 'alloc (K (* n pair-size)) (K pair-tag))]) |
| 418 |
(prm 'mset v (K (- disp-car pair-tag)) (T a)) |
| 419 |
(let f ([t* t*] [i pair-size]) |
| 420 |
(cond |
| 421 |
[(null? (cdr t*)) |
| 422 |
(seq* (prm 'mset v (K (- i disp-cdr pair-tag)) (car t*)) v)] |
| 423 |
[else |
| 424 |
(with-tmp ([tmp (prm 'int+ v (K i))]) |
| 425 |
(prm 'mset tmp (K (- disp-car pair-tag)) (car t*)) |
| 426 |
(prm 'mset tmp (K (- (- disp-cdr pair-tag) pair-size)) tmp) |
| 427 |
(f (cdr t*) (+ i pair-size)))]))))] |
| 428 |
[(P) (interrupt)] |
| 429 |
[(P x) (P x)] |
| 430 |
[(P a . a*) (K #t)] |
| 431 |
[(E) (interrupt)] |
| 432 |
[(E . a*) (nop)]) |
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
/section) |
| 438 |
|
| 439 |
(section ;;; vectors |
| 440 |
(section ;;; helpers |
| 441 |
(define (vector-range-check x idx) |
| 442 |
(define (check-non-vector x idx) |
| 443 |
(define (check-fx idx) |
| 444 |
(seq* |
| 445 |
(interrupt-unless (tag-test (T x) vector-mask vector-tag)) |
| 446 |
(with-tmp ([len (cogen-value-$vector-length x)]) |
| 447 |
(interrupt-unless (prm 'u< (T idx) len)) |
| 448 |
(interrupt-unless-fixnum len)))) |
| 449 |
(define (check-? idx) |
| 450 |
(seq* |
| 451 |
(interrupt-unless (tag-test (T x) vector-mask vector-tag)) |
| 452 |
(with-tmp ([len (cogen-value-$vector-length x)]) |
| 453 |
(interrupt-unless (prm 'u< (T idx) len)) |
| 454 |
(with-tmp ([t (prm 'logor len (T idx))]) |
| 455 |
(interrupt-unless-fixnum t))))) |
| 456 |
(struct-case idx |
| 457 |
[(constant i) |
| 458 |
(if (and (fx? i) (>= i 0)) |
| 459 |
(check-fx idx) |
| 460 |
(check-? idx))] |
| 461 |
[(known idx idx-t) |
| 462 |
(case (T:fixnum? idx-t) |
| 463 |
[(yes) (check-fx idx)] |
| 464 |
[(maybe) (vector-range-check x idx)] |
| 465 |
[else |
| 466 |
(printf "vector check with mismatch index tag ~s" idx-t) |
| 467 |
(vector-range-check x idx)])] |
| 468 |
[else (check-? idx)])) |
| 469 |
(define (check-vector x idx) |
| 470 |
(define (check-fx idx) |
| 471 |
(with-tmp ([len (cogen-value-$vector-length x)]) |
| 472 |
(interrupt-unless (prm 'u< (T idx) len)))) |
| 473 |
(define (check-? idx) |
| 474 |
(seq* |
| 475 |
(interrupt-unless-fixnum (T idx)) |
| 476 |
(with-tmp ([len (cogen-value-$vector-length x)]) |
| 477 |
(interrupt-unless (prm 'u< (T idx) len))))) |
| 478 |
(struct-case idx |
| 479 |
[(constant i) |
| 480 |
(if (and (fx? i) (>= i 0)) |
| 481 |
(check-fx idx) |
| 482 |
(interrupt))] |
| 483 |
[(known idx idx-t) |
| 484 |
(case (T:fixnum? idx-t) |
| 485 |
[(yes) (check-fx idx)] |
| 486 |
[(no) (interrupt)] |
| 487 |
[else (check-vector x idx)])] |
| 488 |
[else (check-? idx)])) |
| 489 |
(struct-case x |
| 490 |
[(known x t) |
| 491 |
(case (T:vector? t) |
| 492 |
[(yes) (record-optimization 'check-vector x) (check-vector x idx)] |
| 493 |
[(no) (interrupt)] |
| 494 |
[else (check-non-vector x idx)])] |
| 495 |
[else (check-non-vector x idx)])) |
| 496 |
/section) |
| 497 |
|
| 498 |
(define-primop vector? safe |
| 499 |
[(P x) (sec-tag-test (T x) vector-mask vector-tag fx-mask fx-tag)] |
| 500 |
[(E x) (nop)]) |
| 501 |
|
| 502 |
(define-primop $make-vector unsafe |
| 503 |
[(V len) |
| 504 |
(struct-case len |
| 505 |
[(constant i) |
| 506 |
(if (and (fx? i) #f) |
| 507 |
(interrupt) |
| 508 |
(with-tmp ([v (prm 'alloc |
| 509 |
(K (align (+ (* i wordsize) disp-vector-data))) |
| 510 |
(K vector-tag))]) |
| 511 |
(prm 'mset v |
| 512 |
(K (- disp-vector-length vector-tag)) |
| 513 |
(K (* i fx-scale))) |
| 514 |
v))] |
| 515 |
[(known expr t) |
| 516 |
(cogen-value-$make-vector expr)] |
| 517 |
[else |
| 518 |
(with-tmp ([alen (align-code (T len) disp-vector-data)]) |
| 519 |
(with-tmp ([v (prm 'alloc alen (K vector-tag))]) |
| 520 |
(prm 'mset v (K (- disp-vector-length vector-tag)) (T len)) |
| 521 |
v))])] |
| 522 |
[(P len) (K #t)] |
| 523 |
[(E len) (nop)]) |
| 524 |
|
| 525 |
(define-primop make-vector safe |
| 526 |
[(V len) |
| 527 |
(with-tmp ([x (make-forcall "ikrt_make_vector1" (list (T len)))]) |
| 528 |
(interrupt-when (prm '= x (K 0))) |
| 529 |
x)]) |
| 530 |
|
| 531 |
(define-primop $vector-ref unsafe |
| 532 |
[(V x i) |
| 533 |
(or |
| 534 |
(struct-case i |
| 535 |
[(constant i) |
| 536 |
(and (fx? i) |
| 537 |
(fx>= i 0) |
| 538 |
(prm 'mref (T x) |
| 539 |
(K (+ (* i wordsize) (- disp-vector-data vector-tag)))))] |
| 540 |
[(known i t) |
| 541 |
(cogen-value-$vector-ref x i)] |
| 542 |
[else #f]) |
| 543 |
(prm 'mref (T x) |
| 544 |
(prm 'int+ (T i) (K (- disp-vector-data vector-tag)))))] |
| 545 |
[(E x i) (nop)]) |
| 546 |
|
| 547 |
(define-primop $vector-length unsafe |
| 548 |
[(V x) (prm 'mref (T x) (K (- disp-vector-length vector-tag)))] |
| 549 |
[(E x) (prm 'nop)] |
| 550 |
[(P x) (K #t)]) |
| 551 |
|
| 552 |
(define-primop vector-length safe |
| 553 |
[(V x) |
| 554 |
(struct-case x |
| 555 |
[(known x t) |
| 556 |
(case (T:vector? t) |
| 557 |
[(yes) (record-optimization 'vector-length x) (cogen-value-$vector-length x)] |
| 558 |
[(no) (interrupt)] |
| 559 |
[else (cogen-value-vector-length x)])] |
| 560 |
[else |
| 561 |
(seq* |
| 562 |
(interrupt-unless (tag-test (T x) vector-mask vector-tag)) |
| 563 |
(with-tmp ([t (cogen-value-$vector-length x)]) |
| 564 |
(interrupt-unless-fixnum t) |
| 565 |
t))])] |
| 566 |
[(E x) |
| 567 |
(struct-case x |
| 568 |
[(known x t) |
| 569 |
(case (T:vector? t) |
| 570 |
[(yes) (record-optimization 'vector-length x) (nop)] |
| 571 |
[(no) (interrupt)] |
| 572 |
[else (cogen-effect-vector-length x)])] |
| 573 |
[else |
| 574 |
(seq* |
| 575 |
(interrupt-unless (tag-test (T x) vector-mask vector-tag)) |
| 576 |
(with-tmp ([t (cogen-value-$vector-length x)]) |
| 577 |
(interrupt-unless-fixnum t)))])] |
| 578 |
[(P x) |
| 579 |
(seq* (cogen-effect-vector-length x) (K #t))]) |
| 580 |
|
| 581 |
(define-primop vector-ref safe |
| 582 |
[(V x i) |
| 583 |
(seq* |
| 584 |
(vector-range-check x i) |
| 585 |
(cogen-value-$vector-ref x i))] |
| 586 |
[(E x i) |
| 587 |
(vector-range-check x i)]) |
| 588 |
|
| 589 |
|
| 590 |
(define-primop $vector-set! unsafe |
| 591 |
[(E x i v) |
| 592 |
(struct-case i |
| 593 |
[(constant i) |
| 594 |
(if (not (fx? i)) |
| 595 |
(interrupt) |
| 596 |
(mem-assign v (T x) |
| 597 |
(+ (* i wordsize) |
| 598 |
(- disp-vector-data vector-tag))))] |
| 599 |
[(known i t) |
| 600 |
(cogen-effect-$vector-set! x i v)] |
| 601 |
[else |
| 602 |
(mem-assign v |
| 603 |
(prm 'int+ (T x) (T i)) |
| 604 |
(- disp-vector-data vector-tag))])]) |
| 605 |
|
| 606 |
(define-primop vector-set! safe |
| 607 |
[(E x i v) |
| 608 |
(seq* |
| 609 |
(vector-range-check x i) |
| 610 |
(cogen-effect-$vector-set! x i v))]) |
| 611 |
|
| 612 |
(define-primop vector safe |
| 613 |
[(V . arg*) |
| 614 |
(with-tmp ([v (prm 'alloc |
| 615 |
(K (align (+ disp-vector-data |
| 616 |
(* (length arg*) wordsize)))) |
| 617 |
(K vector-tag))]) |
| 618 |
(seq* |
| 619 |
(prm 'mset v (K (- disp-vector-length vector-tag)) |
| 620 |
(K (* (length arg*) wordsize))) |
| 621 |
(let f ([t* (map T arg*)] |
| 622 |
[i (- disp-vector-data vector-tag)]) |
| 623 |
(cond |
| 624 |
[(null? t*) v] |
| 625 |
[else |
| 626 |
(make-seq |
| 627 |
(prm 'mset v (K i) (car t*)) |
| 628 |
(f (cdr t*) (+ i wordsize)))]))))] |
| 629 |
[(E . arg*) (prm 'nop)] |
| 630 |
[(P . arg*) (K #t)]) |
| 631 |
|
| 632 |
/section) |
| 633 |
|
| 634 |
(section ;;; closures |
| 635 |
|
| 636 |
(define-primop procedure? safe |
| 637 |
[(P x) (tag-test (T x) closure-mask closure-tag)]) |
| 638 |
|
| 639 |
(define-primop $cpref unsafe |
| 640 |
[(V x i) |
| 641 |
(struct-case i |
| 642 |
[(constant i) |
| 643 |
(unless (fx? i) (interrupt)) |
| 644 |
(prm 'mref (T x) |
| 645 |
(K (+ (- disp-closure-data closure-tag) |
| 646 |
(* i wordsize))))] |
| 647 |
[(known expr t) |
| 648 |
(cogen-value-$cpref x expr)] |
| 649 |
[else (interrupt)])]) |
| 650 |
|
| 651 |
/section) |
| 652 |
|
| 653 |
(section ;;; symbols |
| 654 |
|
| 655 |
(define-primop symbol? safe |
| 656 |
[(P x) |
| 657 |
(sec-tag-test (T x) vector-mask vector-tag #f symbol-record-tag)] |
| 658 |
[(E x) (nop)]) |
| 659 |
|
| 660 |
(define-primop $make-symbol unsafe |
| 661 |
[(V str) |
| 662 |
(with-tmp ([x (prm 'alloc (K (align symbol-record-size)) (K symbol-ptag))]) |
| 663 |
(prm 'mset x (K (- symbol-ptag)) (K symbol-record-tag)) |
| 664 |
(prm 'mset x (K (- disp-symbol-record-string symbol-ptag)) (T str)) |
| 665 |
(prm 'mset x (K (- disp-symbol-record-ustring symbol-ptag)) (K 0)) |
| 666 |
(prm 'mset x (K (- disp-symbol-record-value symbol-ptag)) (K unbound)) |
| 667 |
(prm 'mset x (K (- disp-symbol-record-proc symbol-ptag)) (K unbound)) |
| 668 |
(prm 'mset x (K (- disp-symbol-record-plist symbol-ptag)) (K nil)) |
| 669 |
x)] |
| 670 |
[(P str) (K #t)] |
| 671 |
[(E str) (nop)]) |
| 672 |
|
| 673 |
(define-primop $symbol-string unsafe |
| 674 |
[(V x) (prm 'mref (T x) (K (- disp-symbol-record-string symbol-ptag)))] |
| 675 |
[(E x) (nop)]) |
| 676 |
|
| 677 |
(define-primop $set-symbol-string! unsafe |
| 678 |
[(E x v) (mem-assign v (T x) (- disp-symbol-record-string symbol-ptag))]) |
| 679 |
|
| 680 |
(define-primop $symbol-unique-string unsafe |
| 681 |
[(V x) (prm 'mref (T x) (K (- disp-symbol-record-ustring symbol-ptag)))] |
| 682 |
[(E x) (nop)]) |
| 683 |
|
| 684 |
(define-primop $set-symbol-unique-string! unsafe |
| 685 |
[(E x v) (mem-assign v (T x) (- disp-symbol-record-ustring symbol-ptag))]) |
| 686 |
|
| 687 |
(define-primop $symbol-plist unsafe |
| 688 |
[(V x) (prm 'mref (T x) (K (- disp-symbol-record-plist symbol-ptag)))] |
| 689 |
[(E x) (nop)]) |
| 690 |
|
| 691 |
(define-primop $set-symbol-plist! unsafe |
| 692 |
[(E x v) (mem-assign v (T x) (- disp-symbol-record-plist symbol-ptag))]) |
| 693 |
|
| 694 |
(define-primop $symbol-value unsafe |
| 695 |
[(V x) (prm 'mref (T x) (K (- disp-symbol-record-value symbol-ptag)))] |
| 696 |
[(E x) (nop)]) |
| 697 |
|
| 698 |
(define-primop $set-symbol-value! unsafe |
| 699 |
[(E x v) |
| 700 |
(with-tmp ([x (T x)]) |
| 701 |
(prm 'mset x (K (- disp-symbol-record-value symbol-ptag)) (T v)) |
| 702 |
(dirty-vector-set x))]) |
| 703 |
|
| 704 |
(define-primop $set-symbol-proc! unsafe |
| 705 |
[(E x v) |
| 706 |
(with-tmp ([x (T x)]) |
| 707 |
(prm 'mset x (K (- disp-symbol-record-proc symbol-ptag)) (T v)) |
| 708 |
(dirty-vector-set x))]) |
| 709 |
|
| 710 |
(define-primop $set-symbol-value/proc! unsafe |
| 711 |
[(E x v) |
| 712 |
(with-tmp ([x (T x)] [v (T v)]) |
| 713 |
(prm 'mset x (K (- disp-symbol-record-value symbol-ptag)) v) |
| 714 |
(prm 'mset x (K (- disp-symbol-record-proc symbol-ptag)) v) |
| 715 |
(dirty-vector-set x))]) |
| 716 |
|
| 717 |
(define-primop top-level-value safe |
| 718 |
[(V x) |
| 719 |
(struct-case x |
| 720 |
[(constant s) |
| 721 |
(if (symbol? s) |
| 722 |
(with-tmp ([v (cogen-value-$symbol-value x)]) |
| 723 |
(interrupt-when (cogen-pred-$unbound-object? v)) |
| 724 |
v) |
| 725 |
(interrupt))] |
| 726 |
[(known expr t) |
| 727 |
(cogen-value-top-level-value expr)] |
| 728 |
[else |
| 729 |
(with-tmp ([x (T x)]) |
| 730 |
(interrupt-unless (cogen-pred-symbol? x)) |
| 731 |
(with-tmp ([v (cogen-value-$symbol-value x)]) |
| 732 |
(interrupt-when (cogen-pred-$unbound-object? v)) |
| 733 |
v))])] |
| 734 |
[(E x) |
| 735 |
(struct-case x |
| 736 |
[(constant s) |
| 737 |
(if (symbol? s) |
| 738 |
(with-tmp ([v (cogen-value-$symbol-value x)]) |
| 739 |
(interrupt-when (cogen-pred-$unbound-object? v))) |
| 740 |
(interrupt))] |
| 741 |
[(known expr t) |
| 742 |
(cogen-effect-top-level-value expr)] |
| 743 |
[else |
| 744 |
(with-tmp ([x (T x)]) |
| 745 |
(interrupt-unless (cogen-pred-symbol? x)) |
| 746 |
(with-tmp ([v (cogen-value-$symbol-value x)]) |
| 747 |
(interrupt-when (cogen-pred-$unbound-object? v))))])]) |
| 748 |
|
| 749 |
|
| 750 |
(define-primop $init-symbol-function! unsafe |
| 751 |
[(E x v) |
| 752 |
(with-tmp ([x (T x)] [v (T v)]) |
| 753 |
(prm 'mset x (K (- disp-symbol-record-proc symbol-ptag)) v) |
| 754 |
(dirty-vector-set x))]) |
| 755 |
|
| 756 |
|
| 757 |
/section) |
| 758 |
|
| 759 |
(section ;;; fixnums |
| 760 |
|
| 761 |
(define-primop fixnum? safe |
| 762 |
[(P x) (tag-test (T x) fx-mask fx-tag)] |
| 763 |
[(E x) (nop)]) |
| 764 |
|
| 765 |
|
| 766 |
(define-primop fixnum-width safe |
| 767 |
[(V) (K (fxsll (- (* wordsize 8) fx-shift) fx-shift))] |
| 768 |
[(E) (nop)] |
| 769 |
[(P) (K #t)]) |
| 770 |
|
| 771 |
(define-primop least-fixnum safe |
| 772 |
[(V) (K (sll (- (expt 2 (- (- (* wordsize 8) fx-shift) 1))) |
| 773 |
fx-shift))] |
| 774 |
[(E) (nop)] |
| 775 |
[(P) (K #t)]) |
| 776 |
|
| 777 |
(define-primop greatest-fixnum safe |
| 778 |
[(V) (K (sll (- (expt 2 (- (- (* wordsize 8) fx-shift) 1)) 1) |
| 779 |
fx-shift))] |
| 780 |
[(E) (nop)] |
| 781 |
[(P) (K #t)]) |
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
(define-primop $fxzero? unsafe |
| 787 |
[(P x) (prm '= (T x) (K 0))] |
| 788 |
[(E x) (nop)]) |
| 789 |
|
| 790 |
(define-primop $fx= unsafe |
| 791 |
[(P x y) (prm '= (T x) (T y))] |
| 792 |
[(E x y) (nop)]) |
| 793 |
|
| 794 |
(define-primop $fx< unsafe |
| 795 |
[(P x y) (prm '< (T x) (T y))] |
| 796 |
[(E x y) (nop)]) |
| 797 |
|
| 798 |
(define-primop $fx<= unsafe |
| 799 |
[(P x y) (prm '<= (T x) (T y))] |
| 800 |
[(E x y) (nop)]) |
| 801 |
|
| 802 |
(define-primop $fx> unsafe |
| 803 |
[(P x y) (prm '> (T x) (T y))] |
| 804 |
[(E x y) (nop)]) |
| 805 |
|
| 806 |
(define-primop $fx>= unsafe |
| 807 |
[(P x y) (prm '>= (T x) (T y))] |
| 808 |
[(E x y) (nop)]) |
| 809 |
|
| 810 |
(define-primop $fxadd1 unsafe |
| 811 |
[(V x) (cogen-value-$fx+ x (K 1))] |
| 812 |
[(P x) (K #t)] |
| 813 |
[(E x) (nop)]) |
| 814 |
|
| 815 |
(define-primop $fxsub1 unsafe |
| 816 |
[(V x) (cogen-value-$fx+ x (K -1))] |
| 817 |
[(P x) (K #t)] |
| 818 |
[(E x) (nop)]) |
| 819 |
|
| 820 |
(define-primop $fx+ unsafe |
| 821 |
[(V x y) (prm 'int+ (T x) (T y))] |
| 822 |
[(P x y) (K #t)] |
| 823 |
[(E x y) (nop)]) |
| 824 |
|
| 825 |
(define-primop $fx* unsafe |
| 826 |
[(V a b) |
| 827 |
(struct-case a |
| 828 |
[(constant a) |
| 829 |
(unless (fx? a) (interrupt)) |
| 830 |
(prm 'int* (T b) (K a))] |
| 831 |
[(known a t) (cogen-value-$fx* a b)] |
| 832 |
[else |
| 833 |
(struct-case b |
| 834 |
[(constant b) |
| 835 |
(unless (fx? b) (interrupt)) |
| 836 |
(prm 'int* (T a) (K b))] |
| 837 |
[(known b t) (cogen-value-$fx* a b)] |
| 838 |
[else |
| 839 |
(prm 'int* (T a) (prm 'sra (T b) (K fx-shift)))])])] |
| 840 |
[(P x y) (K #t)] |
| 841 |
[(E x y) (nop)]) |
| 842 |
|
| 843 |
(define-primop $fxlognot unsafe |
| 844 |
[(V x) (cogen-value-$fxlogxor x (K -1))] |
| 845 |
[(P x) (K #t)] |
| 846 |
[(E x) (nop)]) |
| 847 |
|
| 848 |
(define-primop $fxlogand unsafe |
| 849 |
[(V x y) (prm 'logand (T x) (T y))] |
| 850 |
[(P x y) (K #t)] |
| 851 |
[(E x y) (nop)]) |
| 852 |
|
| 853 |
(define-primop $fxlogor unsafe |
| 854 |
[(V x y) (prm 'logor (T x) (T y))] |
| 855 |
[(P x y) (K #t)] |
| 856 |
[(E x y) (nop)]) |
| 857 |
|
| 858 |
(define-primop $fxlogxor unsafe |
| 859 |
[(V x y) (prm 'logxor (T x) (T y))] |
| 860 |
[(P x y) (K #t)] |
| 861 |
[(E x y) (nop)]) |
| 862 |
|
| 863 |
(define-primop $fx- unsafe |
| 864 |
[(V x y) (prm 'int- (T x) (T y))] |
| 865 |
[(P x y) (K #t)] |
| 866 |
[(E x y) (nop)]) |
| 867 |
|
| 868 |
(define-primop $fxsll unsafe |
| 869 |
[(V x i) |
| 870 |
(struct-case i |
| 871 |
[(constant i) |
| 872 |
(unless (fx? i) (interrupt)) |
| 873 |
(prm 'sll (T x) (K i))] |
| 874 |
[(known i t) (cogen-value-$fxsll x i)] |
| 875 |
[else |
| 876 |
(prm 'sll (T x) (prm 'sra (T i) (K fx-shift)))])] |
| 877 |
[(P x i) (K #t)] |
| 878 |
[(E x i) (nop)]) |
| 879 |
|
| 880 |
(define-primop $fxsra unsafe |
| 881 |
[(V x i) |
| 882 |
(struct-case i |
| 883 |
[(constant i) |
| 884 |
(unless (fx? i) (interrupt)) |
| 885 |
(prm 'logand |
| 886 |
(prm 'sra (T x) |
| 887 |
(K (if (< i (* wordsize 8)) |
| 888 |
i |
| 889 |
(- (* wordsize 8) 1)))) |
| 890 |
(K (* -1 fx-scale)))] |
| 891 |
[(known i t) (cogen-value-$fxsra x i)] |
| 892 |
[else |
| 893 |
(with-tmp ([i (prm 'sra (T i) (K fx-shift))]) |
| 894 |
(with-tmp ([i (make-conditional |
| 895 |
(prm '< i (K (* 8 wordsize))) |
| 896 |
i |
| 897 |
(K (- (* 8 wordsize) 1)))]) |
| 898 |
(prm 'logand |
| 899 |
(prm 'sra (T x) i) |
| 900 |
(K (* -1 fx-scale)))))])] |
| 901 |
[(P x i) (K #t)] |
| 902 |
[(E x i) (nop)]) |
| 903 |
|
| 904 |
(define-primop $fxquotient unsafe |
| 905 |
[(V a b) |
| 906 |
(with-tmp ([b (T b)]) ;;; FIXME: why is quotient called remainder? |
| 907 |
(prm 'sll (prm 'int-quotient (T a) b) (K fx-shift)))] |
| 908 |
[(P a b) (K #t)] |
| 909 |
[(E a b) (nop)]) |
| 910 |
|
| 911 |
(define-primop $int-quotient unsafe |
| 912 |
[(V a b) |
| 913 |
(prm 'sll (prm 'int-quotient (T a) (T b)) (K fx-shift))]) |
| 914 |
|
| 915 |
(define-primop $int-remainder unsafe |
| 916 |
[(V a b) (prm 'int-remainder (T a))]) |
| 917 |
|
| 918 |
(define-primop $fxmodulo unsafe |
| 919 |
[(V a b) |
| 920 |
(with-tmp ([b (T b)]) |
| 921 |
(with-tmp ([c (prm 'logand b |
| 922 |
(prm 'sra (prm 'logxor b (T a)) |
| 923 |
(K (sub1 (* 8 wordsize)))))]) |
| 924 |
(prm 'int+ c (prm 'int-remainder (T a) b))))] |
| 925 |
[(P a b) (K #t)] |
| 926 |
[(E a b) (nop)]) |
| 927 |
|
| 928 |
(define-primop $fxinthash unsafe |
| 929 |
[(V key) |
| 930 |
(with-tmp ([k (T key)]) |
| 931 |
(with-tmp ([k (prm 'int+ k (prm 'logxor (prm 'sll k (K 15)) (K -1)))]) |
| 932 |
(with-tmp ([k (prm 'logxor k (prm 'sra k (K 10)))]) |
| 933 |
(with-tmp ([k (prm 'int+ k (prm 'sll k (K 3)))]) |
| 934 |
(with-tmp ([k (prm 'logxor k (prm 'sra k (K 6)))]) |
| 935 |
(with-tmp ([k (prm 'int+ k (prm 'logxor (prm 'sll k (K 11)) (K -1)))]) |
| 936 |
(with-tmp ([k (prm 'logxor k (prm 'sra k (K 16)))]) |
| 937 |
(prm 'sll k (K fx-shift)))))))))]) |
| 938 |
|
| 939 |
|
| 940 |
;(define inthash |
| 941 |
; (lambda (key) |
| 942 |
; ;static int inthash(int key) { /* from Bob Jenkin's */ |
| 943 |
; ; key += ~(key << 15); |
| 944 |
; ; key ^= (key >> 10); |
| 945 |
; ; key += (key << 3); |
| 946 |
; ; key ^= (key >> 6); |
| 947 |
; ; key += ~(key << 11); |
| 948 |
; ; key ^= (key >> 16); |
| 949 |
; ; return key; |
| 950 |
; ;} |
| 951 |
; (let* ([key ($fx+ key ($fxlognot ($fxsll key 15)))] |
| 952 |
; [key ($fxlogxor key ($fxsra key 10))] |
| 953 |
; [key ($fx+ key ($fxsll key 3))] |
| 954 |
; [key ($fxlogxor key ($fxsra key 6))] |
| 955 |
; [key ($fx+ key ($fxlognot ($fxsll key 11)))] |
| 956 |
; [key ($fxlogxor key ($fxsra key 16))]) |
| 957 |
; key))) |
| 958 |
|
| 959 |
|
| 960 |
/section) |
| 961 |
|
| 962 |
(section ;;; bignums |
| 963 |
|
| 964 |
(define-primop bignum? safe |
| 965 |
[(P x) (sec-tag-test (T x) vector-mask vector-tag bignum-mask bignum-tag)] |
| 966 |
[(E x) (nop)]) |
| 967 |
|
| 968 |
(define-primop $bignum-positive? unsafe |
| 969 |
[(P x) |
| 970 |
(prm '= (prm 'logand |
| 971 |
(prm 'mref (T x) (K (- vector-tag))) |
| 972 |
(K bignum-sign-mask)) |
| 973 |
(K 0))] |
| 974 |
[(E x) (nop)]) |
| 975 |
|
| 976 |
(define-primop $bignum-byte-ref unsafe |
| 977 |
[(V s i) |
| 978 |
(struct-case i |
| 979 |
[(constant i) |
| 980 |
(unless (fx? i) (interrupt)) |
| 981 |
(prm 'sll |
| 982 |
(prm 'logand |
| 983 |
(prm 'mref (T s) |
| 984 |
(K (+ i (- disp-bignum-data record-tag)))) |
| 985 |
(K 255)) |
| 986 |
(K fx-shift))] |
| 987 |
[(known i t) (cogen-value-$bignum-byte-ref s i)] |
| 988 |
[else |
| 989 |
(prm 'sll |
| 990 |
(prm 'srl ;;; FIXME: bref |
| 991 |
(prm 'mref (T s) |
| 992 |
(prm 'int+ |
| 993 |
(prm 'sra (T i) (K fx-shift)) |
| 994 |
;;; ENDIANNESS DEPENDENCY |
| 995 |
(K (- disp-bignum-data |
| 996 |
(- wordsize 1) |
| 997 |
record-tag)))) |
| 998 |
(K (* (- wordsize 1) 8))) |
| 999 |
(K fx-shift))])] |
| 1000 |
[(P s i) (K #t)] |
| 1001 |
[(E s i) (nop)]) |
| 1002 |
|
| 1003 |
(define-primop $bignum-size unsafe |
| 1004 |
[(V x) |
| 1005 |
(prm 'sll |
| 1006 |
(prm 'sra |
| 1007 |
(prm 'mref (T x) (K (- record-tag))) |
| 1008 |
(K bignum-length-shift)) |
| 1009 |
(K (* 2 fx-shift)))]) |
| 1010 |
|
| 1011 |
/section) |
| 1012 |
|
| 1013 |
(section ;;; flonums |
| 1014 |
|
| 1015 |
(define ($flop-aux op fl0 fl1) |
| 1016 |
(with-tmp ([x (prm 'alloc (K (align flonum-size)) (K vector-tag))]) |
| 1017 |
(prm 'mset x (K (- vector-tag)) (K flonum-tag)) |
| 1018 |
(prm 'fl:load (T fl0) (K (- disp-flonum-data vector-tag))) |
| 1019 |
(prm op (T fl1) (K (- disp-flonum-data vector-tag))) |
| 1020 |
(prm 'fl:store x (K (- disp-flonum-data vector-tag))) |
| 1021 |
x)) |
| 1022 |
|
| 1023 |
(define ($flop-aux* op fl fl*) |
| 1024 |
(with-tmp ([x (prm 'alloc (K (align flonum-size)) (K vector-tag))]) |
| 1025 |
(prm 'mset x (K (- vector-tag)) (K flonum-tag)) |
| 1026 |
(prm 'fl:load (T fl) (K (- disp-flonum-data vector-tag))) |
| 1027 |
(let f ([fl* fl*]) |
| 1028 |
(cond |
| 1029 |
[(null? fl*) (prm 'nop)] |
| 1030 |
[else |
| 1031 |
(make-seq |
| 1032 |
(prm op (T (car fl*)) (K (- disp-flonum-data vector-tag))) |
| 1033 |
(f (cdr fl*)))])) |
| 1034 |
(prm 'fl:store x (K (- disp-flonum-data vector-tag))) |
| 1035 |
x)) |
| 1036 |
|
| 1037 |
(define ($flcmp-aux op fl0 fl1) |
| 1038 |
(make-seq |
| 1039 |
(prm 'fl:load (T fl0) (K (- disp-flonum-data vector-tag))) |
| 1040 |
(prm op (T fl1) (K (- disp-flonum-data vector-tag))))) |
| 1041 |
|
| 1042 |
(define-primop flonum? safe |
| 1043 |
[(P x) (sec-tag-test (T x) vector-mask vector-tag #f flonum-tag)] |
| 1044 |
[(E x) (nop)]) |
| 1045 |
|
| 1046 |
(define-primop $flonum-u8-ref unsafe |
| 1047 |
[(V s i) |
| 1048 |
(struct-case i |
| 1049 |
[(constant i) |
| 1050 |
(unless (and (fx? i) (fx<= 0 i) (fx<= i 7)) |
| 1051 |
(interrupt)) |
| 1052 |
(prm 'sll |
| 1053 |
(prm 'logand |
| 1054 |
(prm 'bref (T s) |
| 1055 |
(K (+ (- 7 i) (- disp-flonum-data record-tag)))) |
| 1056 |
(K 255)) |
| 1057 |
(K fx-shift))] |
| 1058 |
[(known expr t) |
| 1059 |
(cogen-value-$flonum-u8-ref s expr)] |
| 1060 |
[else (interrupt)])] |
| 1061 |
[(P s i) (K #t)] |
| 1062 |
[(E s i) (nop)]) |
| 1063 |
|
| 1064 |
(define-primop $make-flonum unsafe |
| 1065 |
[(V) |
| 1066 |
(with-tmp ([x (prm 'alloc (K (align flonum-size)) (K vector-tag))]) |
| 1067 |
(prm 'mset x (K (- vector-tag)) (K flonum-tag)) |
| 1068 |
x)] |
| 1069 |
[(P str) (K #t)] |
| 1070 |
[(E str) (nop)]) |
| 1071 |
|
| 1072 |
(define-primop $flonum-set! unsafe |
| 1073 |
[(E x i v) |
| 1074 |
(struct-case i |
| 1075 |
[(constant i) |
| 1076 |
(unless (and (fx? i) (fx<= 0 i) (fx<= i 7)) |
| 1077 |
(interrupt)) |
| 1078 |
(prm 'bset |
| 1079 |
(T x) |
| 1080 |
(K (+ (- 7 i) (- disp-flonum-data vector-tag))) |
| 1081 |
(prm 'sra (T v) (K fx-shift)))] |
| 1082 |
[(known expr t) |
| 1083 |
(cogen-effect-$flonum-set! x expr v)] |
| 1084 |
[else (interrupt)])]) |
| 1085 |
|
| 1086 |
(define-primop $fixnum->flonum unsafe |
| 1087 |
[(V fx) |
| 1088 |
(case wordsize |
| 1089 |
[(4) |
| 1090 |
(with-tmp ([x (prm 'alloc (K (align flonum-size)) (K vector-tag))]) |
| 1091 |
(prm 'mset x (K (- vector-tag)) (K flonum-tag)) |
| 1092 |
(prm 'fl:from-int |
| 1093 |
(K 0) ; dummy |
| 1094 |
(prm 'sra (T fx) (K fx-shift))) |
| 1095 |
(prm 'fl:store x (K (- disp-flonum-data vector-tag))) |
| 1096 |
x)] |
| 1097 |
[else |
| 1098 |
(with-tmp ([f (cogen-value-$make-flonum)]) |
| 1099 |
(make-forcall "ikrt_fixnum_to_flonum" (list (T fx) f)))])]) |
| 1100 |
|
| 1101 |
|
| 1102 |
(define (check-flonums ls code) |
| 1103 |
(cond |
| 1104 |
[(null? ls) code] |
| 1105 |
[else |
| 1106 |
(struct-case (car ls) |
| 1107 |
[(constant v) |
| 1108 |
(if (flonum? v) |
| 1109 |
(check-flonums (cdr ls) code) |
| 1110 |
(interrupt))] |
| 1111 |
[(known x t) |
| 1112 |
(case (T:flonum? t) |
| 1113 |
[(yes) |
| 1114 |
(record-optimization 'check-flonum x) |
| 1115 |
(check-flonums (cdr ls) code)] |
| 1116 |
[(no) (interrupt)] |
| 1117 |
[else (check-flonums (cons x (cdr ls)) code)])] |
| 1118 |
[else |
| 1119 |
(check-flonums (cdr ls) |
| 1120 |
(with-tmp ([x (T (car ls))]) |
| 1121 |
(interrupt-unless |
| 1122 |
(tag-test x vector-mask vector-tag)) |
| 1123 |
(interrupt-unless |
| 1124 |
(prm '= (prm 'mref x (K (- vector-tag))) |
| 1125 |
(K flonum-tag))) |
| 1126 |
code))])])) |
| 1127 |
|
| 1128 |
; (define (primary-tag-tests ls) |
| 1129 |
; (cond |
| 1130 |
; [(null? ls) (prm 'nop)] |
| 1131 |
; [else |
| 1132 |
; (seq* |
| 1133 |
; (interrupt-unless |
| 1134 |
; (tag-test (car ls) vector-mask vector-tag)) |
| 1135 |
; (primary-tag-tests (cdr ls)))])) |
| 1136 |
; (define (secondary-tag-tests ls) |
| 1137 |
; (define (or* a*) |
| 1138 |
; (cond |
| 1139 |
; [(null? (cdr a*)) (car a*)] |
| 1140 |
; [else (prm 'logor (car a*) (or* (cdr a*)))])) |
| 1141 |
; (interrupt-unless |
| 1142 |
; (prm '= (or* (map (lambda (x) |
| 1143 |
; (prm 'mref x (K (- vector-tag)))) |
| 1144 |
; ls)) |
| 1145 |
; (K flonum-tag)))) |
| 1146 |
; (let ([check |
| 1147 |
; (let f ([ls ls] [ac '()]) |
| 1148 |
; (cond |
| 1149 |
; [(null? ls) ac] |
| 1150 |
; [else |
| 1151 |
; (struct-case (car ls) |
| 1152 |
; [(constant v) |
| 1153 |
; (if (flonum? v) |
| 1154 |
; (f (cdr ls) ac) |
| 1155 |
; #f)] |
| 1156 |
; [else (f (cdr ls) (cons (T (car ls)) ac))])]))]) |
| 1157 |
; (cond |
| 1158 |
; [(not check) (interrupt)] |
| 1159 |
; [(null? check) code] |
| 1160 |
; [else |
| 1161 |
; (seq* |
| 1162 |
; (primary-tag-tests check) |
| 1163 |
; (secondary-tag-tests check) |
| 1164 |
; code)]))) |
| 1165 |
|
| 1166 |
(define-primop $fl+ unsafe |
| 1167 |
[(V x y) ($flop-aux 'fl:add! x y)]) |
| 1168 |
(define-primop $fl- unsafe |
| 1169 |
[(V x y) ($flop-aux 'fl:sub! x y)]) |
| 1170 |
(define-primop $fl* unsafe |
| 1171 |
[(V x y) ($flop-aux 'fl:mul! x y)]) |
| 1172 |
(define-primop $fl/ unsafe |
| 1173 |
[(V x y) ($flop-aux 'fl:div! x y)]) |
| 1174 |
|
| 1175 |
(define-primop fl+ safe |
| 1176 |
[(V) (K (make-object 0.0))] |
| 1177 |
[(V x) (check-flonums (list x) (T x))] |
| 1178 |
[(V x . x*) (check-flonums (cons x x*) ($flop-aux* 'fl:add! x x*))] |
| 1179 |
[(P . x*) (check-flonums x* (K #t))] |
| 1180 |
[(E . x*) (check-flonums x* (nop))]) |
| 1181 |
(define-primop fl* safe |
| 1182 |
[(V) (K (make-object 1.0))] |
| 1183 |
[(V x) (check-flonums (list x) (T x))] |
| 1184 |
[(V x . x*) (check-flonums (cons x x*) ($flop-aux* 'fl:mul! x x*))] |
| 1185 |
[(P . x*) (check-flonums x* (K #t))] |
| 1186 |
[(E . x*) (check-flonums x* (nop))]) |
| 1187 |
(define-primop fl- safe |
| 1188 |
[(V x) (check-flonums (list x) ($flop-aux 'fl:sub! (K 0.0) x))] |
| 1189 |
[(V x . x*) (check-flonums (cons x x*) ($flop-aux* 'fl:sub! x x*))] |
| 1190 |
[(P x . x*) (check-flonums (cons x x*) (K #t))] |
| 1191 |
[(E x . x*) (check-flonums (cons x x*) (nop))]) |
| 1192 |
(define-primop fl/ safe |
| 1193 |
[(V x) (check-flonums (list x) ($flop-aux 'fl:div! (K 1.0) x))] |
| 1194 |
[(V x . x*) (check-flonums (cons x x*) ($flop-aux* 'fl:div! x x*))] |
| 1195 |
[(P x . x*) (check-flonums (cons x x*) (K #t))] |
| 1196 |
[(E x . x*) (check-flonums (cons x x*) (nop))]) |
| 1197 |
|
| 1198 |
(define-primop $fl= unsafe |
| 1199 |
[(P x y) ($flcmp-aux 'fl:= x y)]) |
| 1200 |
(define-primop $fl< unsafe |
| 1201 |
[(P x y) ($flcmp-aux 'fl:< x y)]) |
| 1202 |
(define-primop $fl<= unsafe |
| 1203 |
[(P x y) ($flcmp-aux 'fl:<= x y)]) |
| 1204 |
(define-primop $fl> unsafe |
| 1205 |
[(P x y) ($flcmp-aux 'fl:> x y)]) |
| 1206 |
(define-primop $fl>= unsafe |
| 1207 |
[(P x y) ($flcmp-aux 'fl:>= x y)]) |
| 1208 |
|
| 1209 |
(define-primop fl=? safe |
| 1210 |
[(P x y) (check-flonums (list x y) ($flcmp-aux 'fl:= x y))] |
| 1211 |
[(E x y) (check-flonums (list x y) (nop))]) |
| 1212 |
(define-primop fl<? safe |
| 1213 |
[(P x y) (check-flonums (list x y) ($flcmp-aux 'fl:< x y))] |
| 1214 |
[(E x y) (check-flonums (list x y) (nop))]) |
| 1215 |
(define-primop fl<=? safe |
| 1216 |
[(P x y) (check-flonums (list x y) ($flcmp-aux 'fl:<= x y))] |
| 1217 |
[(E x y) (check-flonums (list x y) (nop))]) |
| 1218 |
(define-primop fl>? safe |
| 1219 |
[(P x y) (check-flonums (list x y) ($flcmp-aux 'fl:> x y))] |
| 1220 |
[(E x y) (check-flonums (list x y) (nop))]) |
| 1221 |
(define-primop fl>=? safe |
| 1222 |
[(P x y) (check-flonums (list x y) ($flcmp-aux 'fl:>= x y))] |
| 1223 |
[(E x y) (check-flonums (list x y) (nop))]) |
| 1224 |
|
| 1225 |
(define-primop $flonum-sbe unsafe |
| 1226 |
[(V x) |
| 1227 |
(prm 'sll |
| 1228 |
(prm 'srl |
| 1229 |
(prm 'mref32 (T x) |
| 1230 |
(K (- (+ disp-flonum-data 4) vector-tag))) |
| 1231 |
(K 20)) |
| 1232 |
(K fx-shift))]) |
| 1233 |
|
| 1234 |
/section) |
| 1235 |
|
| 1236 |
(section ;;; ratnums |
| 1237 |
|
| 1238 |
(define-primop ratnum? safe |
| 1239 |
[(P x) (sec-tag-test (T x) vector-mask vector-tag #f ratnum-tag)] |
| 1240 |
[(E x) (nop)]) |
| 1241 |
|
| 1242 |
(define-primop $make-ratnum unsafe |
| 1243 |
[(V num den) |
| 1244 |
(with-tmp ([x (prm 'alloc (K (align ratnum-size)) (K vector-tag))]) |
| 1245 |
(prm 'mset x (K (- vector-tag)) (K ratnum-tag)) |
| 1246 |
(prm 'mset x (K (- disp-ratnum-num vector-tag)) (T num)) |
| 1247 |
(prm 'mset x (K (- disp-ratnum-den vector-tag)) (T den)) |
| 1248 |
x)] |
| 1249 |
[(P str) (K #t)] |
| 1250 |
[(E str) (nop)]) |
| 1251 |
|
| 1252 |
|
| 1253 |
(define-primop $ratnum-n unsafe |
| 1254 |
[(V x) (prm 'mref (T x) (K (- disp-ratnum-num vector-tag)))]) |
| 1255 |
|
| 1256 |
(define-primop $ratnum-d unsafe |
| 1257 |
[(V x) (prm 'mref (T x) (K (- disp-ratnum-den vector-tag)))]) |
| 1258 |
|
| 1259 |
/section) |
| 1260 |
|
| 1261 |
(section ;;; complnums |
| 1262 |
|
| 1263 |
(define-primop compnum? safe |
| 1264 |
[(P x) (sec-tag-test (T x) vector-mask vector-tag #f compnum-tag)] |
| 1265 |
[(E x) (nop)]) |
| 1266 |
|
| 1267 |
(define-primop $make-compnum unsafe |
| 1268 |
[(V real imag) |
| 1269 |
(with-tmp ([x (prm 'alloc (K (align compnum-size)) (K vector-tag))]) |
| 1270 |
(prm 'mset x (K (- vector-tag)) (K compnum-tag)) |
| 1271 |
(prm 'mset x (K (- disp-compnum-real vector-tag)) (T real)) |
| 1272 |
(prm 'mset x (K (- disp-compnum-imag vector-tag)) (T imag)) |
| 1273 |
x)] |
| 1274 |
[(P str) (K #t)] |
| 1275 |
[(E str) (nop)]) |
| 1276 |
|
| 1277 |
|
| 1278 |
(define-primop $compnum-real unsafe |
| 1279 |
[(V x) (prm 'mref (T x) (K (- disp-compnum-real vector-tag)))]) |
| 1280 |
|
| 1281 |
(define-primop $compnum-imag unsafe |
| 1282 |
[(V x) (prm 'mref (T x) (K (- disp-compnum-imag vector-tag)))]) |
| 1283 |
|
| 1284 |
/section) |
| 1285 |
|
| 1286 |
|
| 1287 |
(section ;;; cflonums |
| 1288 |
|
| 1289 |
(define-primop cflonum? safe |
| 1290 |
[(P x) (sec-tag-test (T x) vector-mask vector-tag #f cflonum-tag)] |
| 1291 |
[(E x) (nop)]) |
| 1292 |
|
| 1293 |
(define-primop $make-cflonum unsafe |
| 1294 |
[(V real imag) |
| 1295 |
(with-tmp ([x (prm 'alloc (K (align cflonum-size)) (K vector-tag))]) |
| 1296 |
(prm 'mset x (K (- vector-tag)) (K cflonum-tag)) |
| 1297 |
(prm 'mset x (K (- disp-cflonum-real vector-tag)) (T real)) |
| 1298 |
(prm 'mset x (K (- disp-cflonum-imag vector-tag)) (T imag)) |
| 1299 |
x)] |
| 1300 |
[(P str) (K #t)] |
| 1301 |
[(E str) (nop)]) |
| 1302 |
|
| 1303 |
|
| 1304 |
(define-primop $cflonum-real unsafe |
| 1305 |
[(V x) (prm 'mref (T x) (K (- disp-cflonum-real vector-tag)))]) |
| 1306 |
|
| 1307 |
(define-primop $cflonum-imag unsafe |
| 1308 |
[(V x) (prm 'mref (T x) (K (- disp-cflonum-imag vector-tag)))]) |
| 1309 |
|
| 1310 |
/section) |
| 1311 |
|
| 1312 |
(section ;;; generic arithmetic |
| 1313 |
|
| 1314 |
|
| 1315 |
|
| 1316 |
(define (assert-fixnums a a*) |
| 1317 |
(define (or* a a*) |
| 1318 |
(cond |
| 1319 |
[(null? a*) a] |
| 1320 |
[else (or* (prm 'logor a (T (car a*))) (cdr a*))])) |
| 1321 |
(define (known-fixnum? x) |
| 1322 |
(struct-case x |
| 1323 |
[(constant i) (fx? i)] |
| 1324 |
[(known x t) |
| 1325 |
(case (T:fixnum? t) |
| 1326 |
[(yes) (record-optimization 'assert-fixnum x) #t] |
| 1327 |
[else #f])] |
| 1328 |
[else #f])) |
| 1329 |
(define (known-non-fixnum? x) |
| 1330 |
(struct-case x |
| 1331 |
[(constant i) (not (fx? i))] |
| 1332 |
[(known x t) (eq? (T:fixnum? t) 'no)] |
| 1333 |
[else #f])) |
| 1334 |
(let-values ([(fx* others) (partition known-fixnum? (cons a a*))]) |
| 1335 |
(let-values ([(nfx* others) (partition known-non-fixnum? others)]) |
| 1336 |
(cond |
| 1337 |
[(not (null? nfx*)) (interrupt)] |
| 1338 |
[(null? others) (nop)] |
| 1339 |
[else |
| 1340 |
(interrupt-unless |
| 1341 |
(tag-test (or* (T (car others)) (cdr others)) fx-mask fx-tag))])))) |
| 1342 |
|
| 1343 |
(define (fixnum-fold-p op a a*) |
| 1344 |
(seq* |
| 1345 |
(assert-fixnums a a*) |
| 1346 |
(let f ([a a] [a* a*]) |
| 1347 |
(cond |
| 1348 |
[(null? a*) (K #t)] |
| 1349 |
[else |
| 1350 |
(let ([b (car a*)]) |
| 1351 |
(make-conditional |
| 1352 |
(prm op (T a) (T b)) |
| 1353 |
(f b (cdr a*)) |
| 1354 |
(K #f)))])))) |
| 1355 |
|
| 1356 |
(define-primop = safe |
| 1357 |
[(P) (interrupt)] |
| 1358 |
[(P a . a*) (fixnum-fold-p '= a a*)] |
| 1359 |
[(E) (interrupt)] |
| 1360 |
[(E a . a*) (assert-fixnums a a*)]) |
| 1361 |
|
| 1362 |
(define-primop < safe |
| 1363 |
[(P) (interrupt)] |
| 1364 |
[(P a . a*) (fixnum-fold-p '< a a*)] |
| 1365 |
[(E) (interrupt)] |
| 1366 |
[(E a . a*) (assert-fixnums a a*)]) |
| 1367 |
|
| 1368 |
(define-primop <= safe |
| 1369 |
[(P) (interrupt)] |
| 1370 |
[(P a . a*) (fixnum-fold-p '<= a a*)] |
| 1371 |
[(E) (interrupt)] |
| 1372 |
[(E a . a*) (assert-fixnums a a*)]) |
| 1373 |
|
| 1374 |
(define-primop > safe |
| 1375 |
[(P) (interrupt)] |
| 1376 |
[(P a . a*) (fixnum-fold-p '> a a*)] |
| 1377 |
[(E) (interrupt)] |
| 1378 |
[(E a . a*) (assert-fixnums a a*)]) |
| 1379 |
|
| 1380 |
(define-primop >= safe |
| 1381 |
[(P) (interrupt)] |
| 1382 |
[(P a . a*) (fixnum-fold-p '>= a a*)] |
| 1383 |
[(E) (interrupt)] |
| 1384 |
[(E a . a*) (assert-fixnums a a*)]) |
| 1385 |
|
| 1386 |
(define-primop fx= safe |
| 1387 |
[(P) (interrupt)] |
| 1388 |
[(P a . a*) (fixnum-fold-p '= a a*)] |
| 1389 |
[(E) (interrupt)] |
| 1390 |
[(E a . a*) (assert-fixnums a a*)]) |
| 1391 |
|
| 1392 |
(define-primop fx< safe |
| 1393 |
[(P) (interrupt)] |
| 1394 |
[(P a . a*) (fixnum-fold-p '< a a*)] |
| 1395 |
[(E) (interrupt)] |
| 1396 |
[(E a . a*) (assert-fixnums a a*)]) |
| 1397 |
|
| 1398 |
(define-primop fx<= safe |
| 1399 |
[(P) (interrupt)] |
| 1400 |
[(P a . a*) (fixnum-fold-p '<= a a*)] |
| 1401 |
[(E) (interrupt)] |
| 1402 |
[(E a . a*) (assert-fixnums a a*)]) |
| 1403 |
|
| 1404 |
(define-primop fx> safe |
| 1405 |
[(P) (interrupt)] |
| 1406 |
[(P a . a*) (fixnum-fold-p '> a a*)] |
| 1407 |
[(E) (interrupt)] |
| 1408 |
[(E a . a*) (assert-fixnums a a*)]) |
| 1409 |
|
| 1410 |
(define-primop fx>= safe |
| 1411 |
[(P) (interrupt)] |
| 1412 |
[(P a . a*) (fixnum-fold-p '>= a a*)] |
| 1413 |
[(E) (interrupt)] |
| 1414 |
[(E a . a*) (assert-fixnums a a*)]) |
| 1415 |
|
| 1416 |
(define-primop fx=? safe |
| 1417 |
[(P) (interrupt)] |
| 1418 |
[(P a . a*) (fixnum-fold-p '= a a*)] |
| 1419 |
[(E) (interrupt)] |
| 1420 |
[(E a . a*) (assert-fixnums a a*)]) |
| 1421 |
|
| 1422 |
(define-primop fx<? safe |
| 1423 |
[(P) (interrupt)] |
| 1424 |
[(P a . a*) (fixnum-fold-p '< a a*)] |
| 1425 |
[(E) (interrupt)] |
| 1426 |
[(E a . a*) (assert-fixnums a a*)]) |
| 1427 |
|
| 1428 |
(define-primop fx<=? safe |
| 1429 |
[(P) (interrupt)] |
| 1430 |
[(P a . a*) (fixnum-fold-p '<= a a*)] |
| 1431 |
[(E) (interrupt)] |
| 1432 |
[(E a . a*) (assert-fixnums a a*)]) |
| 1433 |
|
| 1434 |
(define-primop fx>? safe |
| 1435 |
[(P) (interrupt)] |
| 1436 |
[(P a . a*) (fixnum-fold-p '> a a*)] |
| 1437 |
[(E) (interrupt)] |
| 1438 |
[(E a . a*) (assert-fixnums a a*)]) |
| 1439 |
|
| 1440 |
(define-primop fx>=? safe |
| 1441 |
[(P) (interrupt)] |
| 1442 |
[(P a . a*) (fixnum-fold-p '>= a a*)] |
| 1443 |
[(E) (interrupt)] |
| 1444 |
[(E a . a*) (assert-fixnums a a*)]) |
| 1445 |
|
| 1446 |
(define-primop - safe |
| 1447 |
[(V a) |
| 1448 |
(interrupt) |
| 1449 |
(seq* |
| 1450 |
(assert-fixnums a '()) |
| 1451 |
(prm 'int-/overflow (K 0) (T a)))] |
| 1452 |
[(V a . a*) |
| 1453 |
(interrupt) |
| 1454 |
(seq* |
| 1455 |
(assert-fixnums a a*) |
| 1456 |
(let f ([a (T a)] [a* a*]) |
| 1457 |
(cond |
| 1458 |
[(null? a*) a] |
| 1459 |
[else |
| 1460 |
(f (prm 'int-/overflow a (T (car a*))) (cdr a*))])))] |
| 1461 |
[(P a . a*) (seq* (assert-fixnums a a*) (K #t))] |
| 1462 |
[(E a . a*) (assert-fixnums a a*)]) |
| 1463 |
|
| 1464 |
(define-primop + safe |
| 1465 |
[(V) (K 0)] |
| 1466 |
[(V a . a*) |
| 1467 |
(interrupt) |
| 1468 |
(seq* |
| 1469 |
(assert-fixnums a a*) |
| 1470 |
(let f ([a (T a)] [a* a*]) |
| 1471 |
(cond |
| 1472 |
[(null? a*) a] |
| 1473 |
[else |
| 1474 |
(f (prm 'int+/overflow a (T (car a*))) (cdr a*))])))] |
| 1475 |
[(P) (K #t)] |
| 1476 |
[(P a . a*) (seq* (assert-fixnums a a*) (K #t))] |
| 1477 |
[(E) (nop)] |
| 1478 |
[(E a . a*) (assert-fixnums a a*)]) |
| 1479 |
|
| 1480 |
(define-primop add1 safe |
| 1481 |
[(V x) (cogen-value-+ x (K 1))]) |
| 1482 |
(define-primop sub1 safe |
| 1483 |
[(V x) (cogen-value-+ x (K -1))]) |
| 1484 |
|
| 1485 |
(define-primop fxadd1 safe |
| 1486 |
[(V x) (cogen-value-+ x (K 1))]) |
| 1487 |
(define-primop fxsub1 safe |
| 1488 |
[(V x) (cogen-value-+ x (K -1))]) |
| 1489 |
|
| 1490 |
|
| 1491 |
(define (cogen-binary-* a b) |
| 1492 |
(define (cogen-*-non-constants a b) |
| 1493 |
(interrupt) |
| 1494 |
(with-tmp ([a (T a)] [b (T b)]) |
| 1495 |
(assert-fixnum a) |
| 1496 |
(assert-fixnum b) |
| 1497 |
(prm 'int*/overflow a |
| 1498 |
(prm 'sra b (K fx-shift))))) |
| 1499 |
(define (cogen-*-constant a b) |
| 1500 |
(struct-case a |
| 1501 |
[(constant ak) |
| 1502 |
(if (fx? ak) |
| 1503 |
(begin |
| 1504 |
(interrupt) |
| 1505 |
(with-tmp ([b (T b)]) |
| 1506 |
(assert-fixnum b) |
| 1507 |
(prm 'int*/overflow a b))) |
| 1508 |
(interrupt))] |
| 1509 |
[(known x t) (cogen-*-constant x b)] |
| 1510 |
[else #f])) |
| 1511 |
(or (cogen-*-constant a b) |
| 1512 |
(cogen-*-constant b a) |
| 1513 |
(cogen-*-non-constants a b))) |
| 1514 |
|
| 1515 |
|
| 1516 |
(define-primop * safe |
| 1517 |
[(V) (K (fxsll 1 fx-shift))] |
| 1518 |
[(V a b) (cogen-binary-* a b)] |
| 1519 |
[(P) (K #t)] |
| 1520 |
[(P a . a*) (seq* (assert-fixnums a a*) (K #t))] |
| 1521 |
[(E) (nop)] |
| 1522 |
[(E a . a*) (assert-fixnums a a*)]) |
| 1523 |
|
| 1524 |
(define-primop bitwise-and safe |
| 1525 |
[(V) (K (fxsll -1 fx-shift))] |
| 1526 |
[(V a . a*) |
| 1527 |
(interrupt) |
| 1528 |
(seq* |
| 1529 |
(assert-fixnums a a*) |
| 1530 |
(let f ([a (T a)] [a* a*]) |
| 1531 |
(cond |
| 1532 |
[(null? a*) a] |
| 1533 |
[else |
| 1534 |
(f (prm 'logand a (T (car a*))) (cdr a*))])))] |
| 1535 |
[(P) (K #t)] |
| 1536 |
[(P a . a*) (seq* (assert-fixnums a a*) (K #t))] |
| 1537 |
[(E) (nop)] |
| 1538 |
[(E a . a*) (assert-fixnums a a*)]) |
| 1539 |
|
| 1540 |
(define-primop fx+ safe |
| 1541 |
[(V x y) (cogen-value-+ x y)]) |
| 1542 |
|
| 1543 |
(define-primop fx- safe |
| 1544 |
[(V x) (cogen-value-- (K 0) x)] |
| 1545 |
[(V x y) (cogen-value-- x y)]) |
| 1546 |
|
| 1547 |
(define-primop fx* safe |
| 1548 |
[(V a b) (cogen-binary-* a b)]) |
| 1549 |
|
| 1550 |
(define-primop zero? safe |
| 1551 |
[(P x) |
| 1552 |
(seq* |
| 1553 |
(assert-fixnum x) |
| 1554 |
(cogen-pred-$fxzero? x))] |
| 1555 |
[(E x) (assert-fixnum x)]) |
| 1556 |
|
| 1557 |
|
| 1558 |
(define-primop fxarithmetic-shift-left safe |
| 1559 |
[(V x n) |
| 1560 |
(struct-case n |
| 1561 |
[(constant i) |
| 1562 |
(cond |
| 1563 |
[(and (fx? i) |
| 1564 |
(>= i 0) |
| 1565 |
(< i (- (* wordsize 8) fx-shift))) |
| 1566 |
(with-tmp ([x (T x)]) |
| 1567 |
(assert-fixnum x) |
| 1568 |
(cond |
| 1569 |
[(< i 6) |
| 1570 |
(let f ([i i]) |
| 1571 |
(cond |
| 1572 |
[(zero? i) x] |
| 1573 |
[else |
| 1574 |
(interrupt) |
| 1575 |
(prm 'sll/overflow (f (- i 1)) (K 1))]))] |
| 1576 |
[else |
| 1577 |
(with-tmp ([x2 (prm 'sll x (K i))]) |
| 1578 |
(interrupt-unless (prm '= (prm 'sra x2 (K i)) x)) |
| 1579 |
x2)]))] |
| 1580 |
[else |
| 1581 |
(interrupt)])] |
| 1582 |
[else |
| 1583 |
(with-tmp ([x (T x)] [n (T n)]) |
| 1584 |
(assert-fixnums x (list n)) |
| 1585 |
(with-tmp ([n (prm 'sra n (K fx-shift))]) |
| 1586 |
(interrupt-when |
| 1587 |
(prm '< n (K 0))) |
| 1588 |
(interrupt-when |
| 1589 |
(prm '>= n (K (- (* wordsize 8) fx-shift)))) |
| 1590 |
(with-tmp ([x2 (prm 'sll x n)]) |
| 1591 |
(interrupt-unless (prm '= (prm 'sra x2 n) x)) |
| 1592 |
x2)))])]) |
| 1593 |
|
| 1594 |
|
| 1595 |
|
| 1596 |
(define-primop fxarithmetic-shift-right safe |
| 1597 |
[(V x n) |
| 1598 |
(struct-case n |
| 1599 |
;;; FIXME: check for known types |
| 1600 |
[(constant i) |
| 1601 |
(cond |
| 1602 |
[(and (fx? i) |
| 1603 |
(>= i 0) |
| 1604 |
(< i (- (* wordsize 8) fx-shift))) |
| 1605 |
(prm 'sll |
| 1606 |
(prm 'sra (T x) (K (+ i fx-shift))) |
| 1607 |
(K fx-shift))] |
| 1608 |
[else |
| 1609 |
(interrupt)])] |
| 1610 |
[else |
| 1611 |
(with-tmp ([x (T x)] [n (T n)]) |
| 1612 |
(assert-fixnums x (list n)) |
| 1613 |
(with-tmp ([n (prm 'sra n (K fx-shift))]) |
| 1614 |
(interrupt-when |
| 1615 |
(prm '< n (K 0))) |
| 1616 |
(interrupt-when |
| 1617 |
(prm '>= n (K (- (* wordsize 8) fx-shift)))) |
| 1618 |
(prm 'sll |
| 1619 |
(prm 'sra (prm 'sra x n) (K fx-shift)) |
| 1620 |
(K fx-shift))))])]) |
| 1621 |
|
| 1622 |
|
| 1623 |
(define (log2 n) |
| 1624 |
(let f ([n n] [i 0]) |
| 1625 |
(cond |
| 1626 |
[(zero? (fxand n 1)) |
| 1627 |
(f (fxsra n 1) (+ i 1))] |
| 1628 |
[(= n 1) i] |
| 1629 |
[else #f]))) |
| 1630 |
|
| 1631 |
|
| 1632 |
(define-primop div safe |
| 1633 |
[(V x n) |
| 1634 |
(struct-case n |
| 1635 |
[(constant i) |
| 1636 |
(cond |
| 1637 |
[(and (fx? i) (> i 0) (log2 i)) => |
| 1638 |
(lambda (bits) |
| 1639 |
(seq* |
| 1640 |
(interrupt-unless (cogen-pred-fixnum? x)) |
| 1641 |
(prm 'sll |
| 1642 |
(prm 'sra (T x) (K (+ bits fx-shift))) |
| 1643 |
(K fx-shift))))] |
| 1644 |
[else |
| 1645 |
(interrupt)])] |
| 1646 |
[(known expr t) |
| 1647 |
(cogen-value-div x expr)] |
| 1648 |
[else (interrupt)])]) |
| 1649 |
|
| 1650 |
(define-primop quotient safe |
| 1651 |
[(V x n) |
| 1652 |
(struct-case n |
| 1653 |
[(constant i) |
| 1654 |
(if (eqv? i 2) |
| 1655 |
(seq* |
| 1656 |
(interrupt-unless (cogen-pred-fixnum? x)) |
| 1657 |
(make-conditional |
| 1658 |
(prm '< (T x) (K 0)) |
| 1659 |
(prm 'logand |
| 1660 |
(prm 'int+ |
| 1661 |
(prm 'sra (T x) (K 1)) |
| 1662 |
(K (fxsll 1 (sub1 fx-shift)))) |
| 1663 |
(K (fxsll -1 fx-shift))) |
| 1664 |
(prm 'logand |
| 1665 |
(prm 'sra (T x) (K 1)) |
| 1666 |
(K (fxsll -1 fx-shift))))) |
| 1667 |
(interrupt))] |
| 1668 |
[(known expr t) (cogen-value-quotient x expr)] |
| 1669 |
[else (interrupt)])]) |
| 1670 |
|
| 1671 |
/section) |
| 1672 |
|
| 1673 |
(section ;;; structs |
| 1674 |
|
| 1675 |
(define-primop $struct? unsafe |
| 1676 |
[(P x) (sec-tag-test (T x) vector-mask vector-tag vector-mask vector-tag)] |
| 1677 |
[(E x) (nop)]) |
| 1678 |
|
| 1679 |
(define-primop $struct/rtd? unsafe |
| 1680 |
[(P x rtd) |
| 1681 |
(make-conditional |
| 1682 |
(tag-test (T x) vector-mask vector-tag) |
| 1683 |
(prm '= (prm 'mref (T x) (K (- vector-tag))) (T rtd)) |
| 1684 |
(make-constant #f))] |
| 1685 |
[(E x rtd) (nop)]) |
| 1686 |
|
| 1687 |
(define-primop $make-struct unsafe |
| 1688 |
[(V rtd len) |
| 1689 |
(struct-case len |
| 1690 |
[(constant i) |
| 1691 |
(unless (fx? i) (interrupt)) |
| 1692 |
(with-tmp ([t (prm 'alloc |
| 1693 |
(K (align (+ (* i wordsize) disp-struct-data))) |
| 1694 |
(K vector-tag))]) |
| 1695 |
(prm 'mset t (K (- disp-struct-rtd vector-tag)) (T rtd)) |
| 1696 |
t)] |
| 1697 |
[(known expr t) |
| 1698 |
(cogen-value-$make-struct rtd expr)] |
| 1699 |
[else |
| 1700 |
(with-tmp ([ln (align-code len disp-struct-data)]) |
| 1701 |
(with-tmp ([t (prm 'alloc ln (K vector-tag))]) |
| 1702 |
(prm 'mset t (K (- disp-struct-rtd vector-tag)) (T rtd)) |
| 1703 |
t))])] |
| 1704 |
[(P rtd len) (K #t)] |
| 1705 |
[(E rtd len) (nop)]) |
| 1706 |
|
| 1707 |
(define-primop $struct-rtd unsafe |
| 1708 |
[(V x) |
| 1709 |
(prm 'mref (T x) (K (- disp-struct-rtd vector-tag)))] |
| 1710 |
[(E x) (nop)] |
| 1711 |
[(P x) #t]) |
| 1712 |
|
| 1713 |
(define-primop $struct-ref unsafe |
| 1714 |
[(V x i) (cogen-value-$vector-ref x i)] |
| 1715 |
[(E x i) (nop)]) |
| 1716 |
|
| 1717 |
(define-primop $struct-set! unsafe |
| 1718 |
[(V x i v) |
| 1719 |
(seq* (cogen-effect-$vector-set! x i v) |
| 1720 |
(K void-object))] |
| 1721 |
[(E x i v) (cogen-effect-$vector-set! x i v)] |
| 1722 |
[(P x i v) |
| 1723 |
(seq* (cogen-effect-$vector-set! x i v) |
| 1724 |
(K #t))]) |
| 1725 |
|
| 1726 |
(define-primop $struct unsafe |
| 1727 |
[(V rtd . v*) |
| 1728 |
(with-tmp ([t (prm 'alloc |
| 1729 |
(K (align |
| 1730 |
(+ disp-struct-data |
| 1731 |
(* (length v*) wordsize)))) |
| 1732 |
(K vector-tag))]) |
| 1733 |
(prm 'mset t (K (- disp-struct-rtd vector-tag)) (T rtd)) |
| 1734 |
(let f ([v* v*] |
| 1735 |
[i (- disp-struct-data vector-tag)]) |
| 1736 |
(cond |
| 1737 |
[(null? v*) t] |
| 1738 |
[else |
| 1739 |
(make-seq |
| 1740 |
(prm 'mset t (K i) (T (car v*))) |
| 1741 |
(f (cdr v*) (+ i wordsize)))])))] |
| 1742 |
[(P rtd . v*) (K #t)] |
| 1743 |
[(E rtd . v*) (nop)]) |
| 1744 |
|
| 1745 |
/section) |
| 1746 |
|
| 1747 |
(section ;;; characters |
| 1748 |
|
| 1749 |
(define-primop char? safe |
| 1750 |
[(P x) (tag-test (T x) char-mask char-tag)] |
| 1751 |
[(E x) (nop)]) |
| 1752 |
|
| 1753 |
(define-primop $char= unsafe |
| 1754 |
[(P x y) (prm '= (T x) (T y))] |
| 1755 |
[(E x y) (nop)]) |
| 1756 |
|
| 1757 |
(define-primop $char< unsafe |
| 1758 |
[(P x y) (prm '< (T x) (T y))] |
| 1759 |
[(E x y) (nop)]) |
| 1760 |
|
| 1761 |
(define-primop $char<= unsafe |
| 1762 |
[(P x y) (prm '<= (T x) (T y))] |
| 1763 |
[(E x y) (nop)]) |
| 1764 |
|
| 1765 |
(define-primop $char> unsafe |
| 1766 |
[(P x y) (prm '> (T x) (T y))] |
| 1767 |
[(E x y) (nop)]) |
| 1768 |
|
| 1769 |
(define-primop $char>= unsafe |
| 1770 |
[(P x y) (prm '>= (T x) (T y))] |
| 1771 |
[(E x y) (nop)]) |
| 1772 |
|
| 1773 |
(define-primop $fixnum->char unsafe |
| 1774 |
[(V x) |
| 1775 |
(prm 'logor |
| 1776 |
(prm 'sll (T x) (K (- char-shift fx-shift))) |
| 1777 |
(K char-tag))] |
| 1778 |
[(P x) (K #t)] |
| 1779 |
[(E x) (nop)]) |
| 1780 |
|
| 1781 |
(define-primop $char->fixnum unsafe |
| 1782 |
[(V x) (prm 'sra (T x) (K (- char-shift fx-shift)))] |
| 1783 |
[(P x) (K #t)] |
| 1784 |
[(E x) (nop)]) |
| 1785 |
|
| 1786 |
|
| 1787 |
(define (assert-chars a a*) |
| 1788 |
(define (or* a a*) |
| 1789 |
(cond |
| 1790 |
[(null? a*) a] |
| 1791 |
[else (or* (prm 'logor a (T (car a*))) (cdr a*))])) |
| 1792 |
(define (known-char? x) |
| 1793 |
(struct-case x |
| 1794 |
[(constant i) (char? i)] |
| 1795 |
[(known x t) (eq? (T:char? t) 'yes)] |
| 1796 |
[else #f])) |
| 1797 |
(define (known-non-char? x) |
| 1798 |
(struct-case x |
| 1799 |
[(constant i) (not (char? i))] |
| 1800 |
[(known x t) (eq? (T:char? t) 'no)] |
| 1801 |
[else #f])) |
| 1802 |
(let-values ([(fx* others) (partition known-char? (cons a a*))]) |
| 1803 |
(let-values ([(nfx* others) (partition known-non-char? others)]) |
| 1804 |
(cond |
| 1805 |
[(not (null? nfx*)) (interrupt)] |
| 1806 |
[(null? others) (nop)] |
| 1807 |
[else |
| 1808 |
(interrupt-unless |
| 1809 |
(tag-test (or* (T (car others)) (cdr others)) char-mask char-tag))])))) |
| 1810 |
|
| 1811 |
(define (char-fold-p op a a*) |
| 1812 |
(seq* |
| 1813 |
(assert-chars a a*) |
| 1814 |
(let f ([a a] [a* a*]) |
| 1815 |
(cond |
| 1816 |
[(null? a*) (K #t)] |
| 1817 |
[else |
| 1818 |
(let ([b (car a*)]) |
| 1819 |
(make-conditional |
| 1820 |
(prm op (T a) (T b)) |
| 1821 |
(f b (cdr a*)) |
| 1822 |
(K #f)))])))) |
| 1823 |
|
| 1824 |
|
| 1825 |
(define-primop char=? safe |
| 1826 |
[(P) (interrupt)] |
| 1827 |
[(P a . a*) (char-fold-p '= a a*)] |
| 1828 |
[(E) (interrupt)] |
| 1829 |
[(E a . a*) (assert-chars a a*)]) |
| 1830 |
|
| 1831 |
(define-primop char<? safe |
| 1832 |
[(P) (interrupt)] |
| 1833 |
[(P a . a*) (char-fold-p '< a a*)] |
| 1834 |
[(E) (interrupt)] |
| 1835 |
[(E a . a*) (assert-chars a a*)]) |
| 1836 |
|
| 1837 |
(define-primop char<=? safe |
| 1838 |
[(P) (interrupt)] |
| 1839 |
[(P a . a*) (char-fold-p '<= a a*)] |
| 1840 |
[(E) (interrupt)] |
| 1841 |
[(E a . a*) (assert-chars a a*)]) |
| 1842 |
|
| 1843 |
(define-primop char>? safe |
| 1844 |
[(P) (interrupt)] |
| 1845 |
[(P a . a*) (char-fold-p '> a a*)] |
| 1846 |
[(E) (interrupt)] |
| 1847 |
[(E a . a*) (assert-chars a a*)]) |
| 1848 |
|
| 1849 |
(define-primop char>=? safe |
| 1850 |
[(P) (interrupt)] |
| 1851 |
[(P a . a*) (char-fold-p '>= a a*)] |
| 1852 |
[(E) (interrupt)] |
| 1853 |
[(E a . a*) (assert-chars a a*)]) |
| 1854 |
|
| 1855 |
/section) |
| 1856 |
|
| 1857 |
(section ;;; bytevectors |
| 1858 |
|
| 1859 |
(define-primop bytevector? safe |
| 1860 |
[(P x) (tag-test (T x) bytevector-mask bytevector-tag)] |
| 1861 |
[(E x) (nop)]) |
| 1862 |
|
| 1863 |
(define-primop $make-bytevector unsafe |
| 1864 |
[(V n) |
| 1865 |
(struct-case n |
| 1866 |
[(constant n) |
| 1867 |
(unless (fx? n) (interrupt)) |
| 1868 |
(with-tmp ([s (prm 'alloc |
| 1869 |
(K (align (+ n 1 disp-bytevector-data))) |
| 1870 |
(K bytevector-tag))]) |
| 1871 |
(prm 'mset s |
| 1872 |
(K (- disp-bytevector-length bytevector-tag)) |
| 1873 |
(K (* n fx-scale))) |
| 1874 |
(prm 'bset s |
| 1875 |
(K (+ n (- disp-bytevector-data bytevector-tag))) |
| 1876 |
(K 0)) |
| 1877 |
s)] |
| 1878 |
[(known expr t) |
| 1879 |
(cogen-value-$make-bytevector expr)] |
| 1880 |
[else |
| 1881 |
(with-tmp ([s (prm 'alloc |
| 1882 |
(align-code |
| 1883 |
(prm 'sra (T n) (K fx-shift)) |
| 1884 |
(+ disp-bytevector-data 1)) |
| 1885 |
(K bytevector-tag))]) |
| 1886 |
(prm 'mset s |
| 1887 |
(K (- disp-bytevector-length bytevector-tag)) |
| 1888 |
(T n)) |
| 1889 |
(prm 'bset s |
| 1890 |
(prm 'int+ |
| 1891 |
(prm 'sra (T n) (K fx-shift)) |
| 1892 |
(K (- disp-bytevector-data bytevector-tag))) |
| 1893 |
(K 0)) |
| 1894 |
s)])] |
| 1895 |
[(P n) (K #t)] |
| 1896 |
[(E n) (nop)]) |
| 1897 |
|
| 1898 |
(define-primop $bytevector-length unsafe |
| 1899 |
[(V x) (prm 'mref (T x) (K (- disp-bytevector-length bytevector-tag)))] |
| 1900 |
[(P x) (K #t)] |
| 1901 |
[(E x) (nop)]) |
| 1902 |
|
| 1903 |
(define-primop $bytevector-u8-ref unsafe |
| 1904 |
[(V s i) |
| 1905 |
(struct-case i |
| 1906 |
[(constant i) |
| 1907 |
(unless (fx? i) (interrupt)) |
| 1908 |
(prm 'sll |
| 1909 |
(prm 'logand |
| 1910 |
(prm 'bref (T s) |
| 1911 |
(K (+ i (- disp-bytevector-data bytevector-tag)))) |
| 1912 |
(K 255)) |
| 1913 |
(K fx-shift))] |
| 1914 |
[else |
| 1915 |
(prm 'sll |
| 1916 |
(prm 'logand |
| 1917 |
(prm 'bref (T s) |
| 1918 |
(prm 'int+ |
| 1919 |
(prm 'sra (T i) (K fx-shift)) |
| 1920 |
(K (- disp-bytevector-data bytevector-tag)))) |
| 1921 |
(K 255)) |
| 1922 |
(K fx-shift))])] |
| 1923 |
[(P s i) (K #t)] |
| 1924 |
[(E s i) (nop)]) |
| 1925 |
|
| 1926 |
(define-primop $bytevector-s8-ref unsafe |
| 1927 |
[(V s i) |
| 1928 |
(struct-case i |
| 1929 |
[(constant i) |
| 1930 |
(unless (fx? i) (interrupt)) |
| 1931 |
(prm 'sra |
| 1932 |
(prm 'sll |
| 1933 |
(prm 'logand |
| 1934 |
(prm 'bref (T s) |
| 1935 |
(K (+ i (- disp-bytevector-data bytevector-tag)))) |
| 1936 |
(K 255)) |
| 1937 |
(K (- (* wordsize 8) 8))) |
| 1938 |
(K (- (* wordsize 8) (+ 8 fx-shift))))] |
| 1939 |
[else |
| 1940 |
(prm 'sra |
| 1941 |
(prm 'sll |
| 1942 |
(prm 'bref (T s) |
| 1943 |
(prm 'int+ |
| 1944 |
(prm 'sra (T i) (K fx-shift)) |
| 1945 |
(K (- disp-bytevector-data bytevector-tag)))) |
| 1946 |
(K (- (* wordsize 8) 8))) |
| 1947 |
(K (- (* wordsize 8) (+ 8 fx-shift))))])] |
| 1948 |
[(P s i) (K #t)] |
| 1949 |
[(E s i) (nop)]) |
| 1950 |
|
| 1951 |
|
| 1952 |
(define-primop $bytevector-set! unsafe |
| 1953 |
[(E x i c) |
| 1954 |
(struct-case i |
| 1955 |
[(constant i) |
| 1956 |
(unless (fx? i) (interrupt)) |
| 1957 |
(struct-case c |
| 1958 |
[(constant c) |
| 1959 |
(unless (fx? c) (interrupt)) |
| 1960 |
(prm 'bset (T x) |
| 1961 |
(K (+ i (- disp-bytevector-data bytevector-tag))) |
| 1962 |
(K (cond |
| 1963 |
[(<= -128 c 127) c] |
| 1964 |
[(<= 128 c 255) (- c 256)] |
| 1965 |
[else (interrupt)])))] |
| 1966 |
[else |
| 1967 |
(prm 'bset (T x) |
| 1968 |
(K (+ i (- disp-bytevector-data bytevector-tag))) |
| 1969 |
(prm 'sra (T c) (K fx-shift)))])] |
| 1970 |
[else |
| 1971 |
(struct-case c |
| 1972 |
[(constant c) |
| 1973 |
(unless (fx? c) (interrupt)) |
| 1974 |
(prm 'bset (T x) |
| 1975 |
(prm 'int+ |
| 1976 |
(prm 'sra (T i) (K fx-shift)) |
| 1977 |
(K (- disp-bytevector-data bytevector-tag))) |
| 1978 |
(K (cond |
| 1979 |
[(<= -128 c 127) c] |
| 1980 |
[(<= 128 c 255) (- c 256)] |
| 1981 |
[else (interrupt)])))] |
| 1982 |
[else |
| 1983 |
(prm 'bset (T x) |
| 1984 |
(prm 'int+ |
| 1985 |
(prm 'sra (T i) (K fx-shift)) |
| 1986 |
(K (- disp-bytevector-data bytevector-tag))) |
| 1987 |
(prm 'sra (T c) (K fx-shift)))])])]) |
| 1988 |
|
| 1989 |
(define-primop $bytevector-ieee-double-native-ref unsafe |
| 1990 |
[(V bv i) |
| 1991 |
(with-tmp ([x (prm 'alloc (K (align flonum-size)) (K vector-tag))]) |
| 1992 |
(prm 'mset x (K (- vector-tag)) (K flonum-tag)) |
| 1993 |
(prm 'fl:load |
| 1994 |
(prm 'int+ (T bv) (prm 'sra (T i) (K fx-shift))) |
| 1995 |
(K (- disp-bytevector-data bytevector-tag))) |
| 1996 |
(prm 'fl:store x (K (- disp-flonum-data vector-tag))) |
| 1997 |
x)]) |
| 1998 |
|
| 1999 |
|
| 2000 |
;;; the following uses unsupported sse3 instructions |
| 2001 |
;(define-primop $bytevector-ieee-double-nonnative-ref unsafe |
| 2002 |
; [(V bv i) |
| 2003 |
; (with-tmp ([x (prm 'alloc (K (align flonum-size)) (K vector-tag))]) |
| 2004 |
; (prm 'mset x (K (- vector-tag)) (K flonum-tag)) |
| 2005 |
; (prm 'fl:load |
| 2006 |
; (prm 'int+ (T bv) (prm 'sra (T i) (K fx-shift))) |
| 2007 |
; (K (- disp-bytevector-data bytevector-tag))) |
| 2008 |
; (prm 'fl:shuffle |
| 2009 |
; (K (make-object '#vu8(7 6 2 3 4 5 1 0))) |
| 2010 |
; (K (- disp-bytevector-data bytevector-tag))) |
| 2011 |
; (prm 'fl:store x (K (- disp-flonum-data vector-tag))) |
| 2012 |
; x)]) |
| 2013 |
|
| 2014 |
(define-primop $bytevector-ieee-double-nonnative-ref unsafe |
| 2015 |
[(V bv i) |
| 2016 |
(case wordsize |
| 2017 |
[(4) |
| 2018 |
(let ([bvoff (- disp-bytevector-data bytevector-tag)] |
| 2019 |
[floff (- disp-flonum-data vector-tag)]) |
| 2020 |
(with-tmp ([x (prm 'alloc (K (align flonum-size)) (K vector-tag))]) |
| 2021 |
(prm 'mset x (K (- vector-tag)) (K flonum-tag)) |
| 2022 |
(with-tmp ([t (prm 'int+ (T bv) |
| 2023 |
(prm 'sra (T i) (K fx-shift)))]) |
| 2024 |
(with-tmp ([x0 (prm 'mref t (K bvoff))]) |
| 2025 |
(prm 'bswap! x0 x0) |
| 2026 |
(prm 'mset x (K (+ floff wordsize)) x0)) |
| 2027 |
(with-tmp ([x0 (prm 'mref t (K (+ bvoff wordsize)))]) |
| 2028 |
(prm 'bswap! x0 x0) |
| 2029 |
(prm 'mset x (K floff) x0))) |
| 2030 |
x))] |
| 2031 |
[else |
| 2032 |
(let ([bvoff (- disp-bytevector-data bytevector-tag)] |
| 2033 |
[floff (- disp-flonum-data vector-tag)]) |
| 2034 |
(with-tmp ([x (prm 'alloc (K (align flonum-size)) (K vector-tag))]) |
| 2035 |
(prm 'mset x (K (- vector-tag)) (K flonum-tag)) |
| 2036 |
(with-tmp ([t (prm 'int+ (T bv) |
| 2037 |
(prm 'sra (T i) (K fx-shift)))]) |
| 2038 |
(with-tmp ([x0 (prm 'mref t (K bvoff))]) |
| 2039 |
(prm 'bswap! x0 x0) |
| 2040 |
(prm 'mset x (K floff) x0))) |
| 2041 |
x))])]) |
| 2042 |
|
| 2043 |
|
| 2044 |
(define-primop $bytevector-ieee-double-native-set! unsafe |
| 2045 |
[(E bv i x) |
| 2046 |
(seq* |
| 2047 |
(prm 'fl:load (T x) (K (- disp-flonum-data vector-tag))) |
| 2048 |
(prm 'fl:store |
| 2049 |
(prm 'int+ (T bv) (prm 'sra (T i) (K fx-shift))) |
| 2050 |
(K (- disp-bytevector-data bytevector-tag))))]) |
| 2051 |
|
| 2052 |
|
| 2053 |
(define-primop $bytevector-ieee-single-native-ref unsafe |
| 2054 |
[(V bv i) |
| 2055 |
(with-tmp ([x (prm 'alloc (K (align flonum-size)) (K vector-tag))]) |
| 2056 |
(prm 'mset x (K (- vector-tag)) (K flonum-tag)) |
| 2057 |
(prm 'fl:load-single |
| 2058 |
(prm 'int+ (T bv) (prm 'sra (T i) (K fx-shift))) |
| 2059 |
(K (- disp-bytevector-data bytevector-tag))) |
| 2060 |
(prm 'fl:single->double) |
| 2061 |
(prm 'fl:store x (K (- disp-flonum-data vector-tag))) |
| 2062 |
x)]) |
| 2063 |
|
| 2064 |
(define-primop $bytevector-ieee-single-native-set! unsafe |
| 2065 |
[(E bv i x) |
| 2066 |
(seq* |
| 2067 |
(prm 'fl:load (T x) (K (- disp-flonum-data vector-tag))) |
| 2068 |
(prm 'fl:double->single) |
| 2069 |
(prm 'fl:store-single |
| 2070 |
(prm 'int+ (T bv) (prm 'sra (T i) (K fx-shift))) |
| 2071 |
(K (- disp-bytevector-data bytevector-tag))))]) |
| 2072 |
|
| 2073 |
(define-primop $bytevector-ieee-single-nonnative-ref unsafe |
| 2074 |
[(V bv i) |
| 2075 |
(let ([bvoff (- disp-bytevector-data bytevector-tag)] |
| 2076 |
[floff (- disp-flonum-data vector-tag)]) |
| 2077 |
(with-tmp ([x (prm 'alloc (K (align flonum-size)) (K vector-tag))]) |
| 2078 |
(prm 'mset x (K (- vector-tag)) (K flonum-tag)) |
| 2079 |
(with-tmp ([t (prm 'int+ (T bv) (prm 'sra (T i) (K fx-shift)))]) |
| 2080 |
(with-tmp ([x0 (prm 'mref t (K bvoff))]) |
| 2081 |
(prm 'bswap! x0 x0) |
| 2082 |
(prm 'mset x (K floff) x0))) |
| 2083 |
(prm 'fl:load-single x (K (+ floff (- wordsize 4)))) |
| 2084 |
(prm 'fl:single->double) |
| 2085 |
(prm 'fl:store x (K floff)) |
| 2086 |
x))]) |
| 2087 |
|
| 2088 |
|
| 2089 |
;;; the following uses unsupported sse3 instructions |
| 2090 |
;(define-primop $bytevector-ieee-double-nonnative-set! unsafe |
| 2091 |
; [(E bv i x) |
| 2092 |
; (seq* |
| 2093 |
; (prm 'fl:load (T x) (K (- disp-flonum-data vector-tag))) |
| 2094 |
; (prm 'fl:shuffle |
| 2095 |
; (K (make-object '#vu8(7 6 2 3 4 5 1 0))) |
| 2096 |
; (K (- disp-bytevector-data bytevector-tag))) |
| 2097 |
; (prm 'fl:store |
| 2098 |
; (prm 'int+ (T bv) (prm 'sra (T i) (K fx-shift))) |
| 2099 |
; (K (- disp-bytevector-data bytevector-tag))))]) |
| 2100 |
|
| 2101 |
(define-primop $bytevector-ieee-double-nonnative-set! unsafe |
| 2102 |
[(E bv i x) |
| 2103 |
(case wordsize |
| 2104 |
[(4) |
| 2105 |
(let ([bvoff (- disp-bytevector-data bytevector-tag)] |
| 2106 |
[floff (- disp-flonum-data vector-tag)]) |
| 2107 |
(with-tmp ([t (prm 'int+ (T bv) |
| 2108 |
(prm 'sra (T i) (K fx-shift)))]) |
| 2109 |
(with-tmp ([x0 (prm 'mref (T x) (K floff))]) |
| 2110 |
(prm 'bswap! x0 x0) |
| 2111 |
(prm 'mset t (K (+ bvoff wordsize)) x0)) |
| 2112 |
(with-tmp ([x0 (prm 'mref (T x) (K (+ floff wordsize)))]) |
| 2113 |
(prm 'bswap! x0 x0) |
| 2114 |
(prm 'mset t (K bvoff) x0))))] |
| 2115 |
[else |
| 2116 |
(let ([bvoff (- disp-bytevector-data bytevector-tag)] |
| 2117 |
[floff (- disp-flonum-data vector-tag)]) |
| 2118 |
(with-tmp ([t (prm 'int+ (T bv) |
| 2119 |
(prm 'sra (T i) (K fx-shift)))]) |
| 2120 |
(with-tmp ([x0 (prm 'mref (T x) (K floff))]) |
| 2121 |
(prm 'bswap! x0 x0) |
| 2122 |
(prm 'mset t (K bvoff) x0))))])]) |
| 2123 |
|
| 2124 |
(define-primop $bytevector-ieee-single-nonnative-set! unsafe |
| 2125 |
[(E bv i x) |
| 2126 |
(let ([bvoff (- disp-bytevector-data bytevector-tag)] |
| 2127 |
[floff (- disp-flonum-data vector-tag)]) |
| 2128 |
(seq* |
| 2129 |
(prm 'fl:load (T x) (K floff)) |
| 2130 |
(prm 'fl:double->single) |
| 2131 |
(with-tmp ([t (prm 'int+ (T bv) |
| 2132 |
(prm 'sra (T i) (K fx-shift)))]) |
| 2133 |
(prm 'fl:store-single t (K bvoff)) |
| 2134 |
(case wordsize |
| 2135 |
[(4) |
| 2136 |
(with-tmp ([x0 (prm 'mref t (K bvoff))]) |
| 2137 |
(prm 'bswap! x0 x0) |
| 2138 |
(prm 'mset t (K bvoff) x0))] |
| 2139 |
[else |
| 2140 |
(with-tmp ([x0 (prm 'mref32 t (K bvoff))]) |
| 2141 |
(prm 'bswap! x0 x0) |
| 2142 |
(prm 'mset32 t (K bvoff) (prm 'sra x0 (K 32))))]))))]) |
| 2143 |
/section) |
| 2144 |
|
| 2145 |
(section ;;; strings |
| 2146 |
|
| 2147 |
(define-primop string? safe |
| 2148 |
[(P x) (tag-test (T x) string-mask string-tag)] |
| 2149 |
[(E x) (nop)]) |
| 2150 |
|
| 2151 |
(define-primop $make-string unsafe |
| 2152 |
[(V n) |
| 2153 |
(struct-case n |
| 2154 |
[(constant n) |
| 2155 |
(unless (fx? n) (interrupt)) |
| 2156 |
(with-tmp ([s (prm 'alloc |
| 2157 |
(K (align (+ (* n wordsize) disp-string-data))) |
| 2158 |
(K string-tag))]) |
| 2159 |
(prm 'mset s |
| 2160 |
(K (- disp-string-length string-tag)) |
| 2161 |
(K (* n fx-scale))) |
| 2162 |
s)] |
| 2163 |
[(known expr) |
| 2164 |
(cogen-value-$make-string expr)] |
| 2165 |
[else |
| 2166 |
(with-tmp ([s (prm 'alloc |
| 2167 |
(align-code (T n) disp-string-data) |
| 2168 |
(K string-tag))]) |
| 2169 |
(prm 'mset s |
| 2170 |
(K (- disp-string-length string-tag)) |
| 2171 |
(T n)) |
| 2172 |
s)])] |
| 2173 |
[(P n) (K #t)] |
| 2174 |
[(E n) (nop)]) |
| 2175 |
|
| 2176 |
(define-primop $string-length unsafe |
| 2177 |
[(V x) (prm 'mref (T x) (K (- disp-string-length string-tag)))] |
| 2178 |
[(P x) (K #t)] |
| 2179 |
[(E x) (nop)]) |
| 2180 |
|
| 2181 |
|
| 2182 |
(define-primop $string-ref unsafe |
| 2183 |
[(V s i) |
| 2184 |
(struct-case i |
| 2185 |
[(constant i) |
| 2186 |
(unless (fx? i) (interrupt)) |
| 2187 |
(prm 'mref32 (T s) |
| 2188 |
(K (+ (* i char-size) |
| 2189 |
(- disp-string-data string-tag))))] |
| 2190 |
[else |
| 2191 |
(prm 'mref32 (T s) |
| 2192 |
(prm 'int+ |
| 2193 |
(cond |
| 2194 |
[(= wordsize char-size) (T i)] |
| 2195 |
[(= wordsize 8) (prm 'sra (T i) (K 1))] |
| 2196 |
[else (error '$string-ref "invalid operand")]) |
| 2197 |
(K (- disp-string-data string-tag))))])] |
| 2198 |
[(P s i) (K #t)] |
| 2199 |
[(E s i) (nop)]) |
| 2200 |
|
| 2201 |
(define assert-fixnum |
| 2202 |
(case-lambda |
| 2203 |
[(x) |
| 2204 |
(struct-case x |
| 2205 |
[(constant i) |
| 2206 |
(if (fx? i) (nop) (interrupt))] |
| 2207 |
[(known expr t) |
| 2208 |
(case (T:fixnum? t) |
| 2209 |
[(yes) (nop)] |
| 2210 |
[(no) (interrupt)] |
| 2211 |
[else (assert-fixnum expr)])] |
| 2212 |
[else (interrupt-unless (cogen-pred-fixnum? x))])])) |
| 2213 |
|
| 2214 |
(define (assert-string x) |
| 2215 |
(struct-case x |
| 2216 |
[(constant s) (if (string? s) (nop) (interrupt))] |
| 2217 |
[(known expr t) |
| 2218 |
(case (T:string? t) |
| 2219 |
[(yes) (record-optimization 'assert-string x) (nop)] |
| 2220 |
[(no) (interrupt)] |
| 2221 |
[else (assert-string expr)])] |
| 2222 |
[else (interrupt-unless (cogen-pred-string? x))])) |
| 2223 |
|
| 2224 |
(define-primop string-ref safe |
| 2225 |
[(V s i) |
| 2226 |
(seq* |
| 2227 |
(assert-fixnum i) |
| 2228 |
(assert-string s) |
| 2229 |
(interrupt-unless (prm 'u< (T i) (cogen-value-$string-length s))) |
| 2230 |
(cogen-value-$string-ref s i))] |
| 2231 |
[(P s i) |
| 2232 |
(seq* |
| 2233 |
(assert-fixnum i) |
| 2234 |
(assert-string s) |
| 2235 |
(interrupt-unless (prm 'u< (T i) (cogen-value-$string-length s))) |
| 2236 |
(K #t))] |
| 2237 |
[(E s i) |
| 2238 |
(seq* |
| 2239 |
(assert-fixnum i) |
| 2240 |
(assert-string s) |
| 2241 |
(interrupt-unless (prm 'u< (T i) (cogen-value-$string-length s))))]) |
| 2242 |
|
| 2243 |
(define-primop $string-set! unsafe |
| 2244 |
[(E x i c) |
| 2245 |
(struct-case i |
| 2246 |
[(constant i) |
| 2247 |
(unless (fx? i) (interrupt)) |
| 2248 |
(prm 'mset32 (T x) |
| 2249 |
(K (+ (* i char-size) |
| 2250 |
(- disp-string-data string-tag))) |
| 2251 |
(T c))] |
| 2252 |
[else |
| 2253 |
(prm 'mset32 (T x) |
| 2254 |
(prm 'int+ |
| 2255 |
(cond |
| 2256 |
[(= wordsize char-size) (T i)] |
| 2257 |
[(= wordsize 8) (prm 'sra (T i) (K 1))] |
| 2258 |
[else (error '$string-set! "invalid operand")]) |
| 2259 |
(K (- disp-string-data string-tag))) |
| 2260 |
(T c))])]) |
| 2261 |
|
| 2262 |
/section) |
| 2263 |
|
| 2264 |
(section ;;; ports |
| 2265 |
|
| 2266 |
(define-primop port? safe |
| 2267 |
[(P x) (sec-tag-test (T x) vector-mask vector-tag port-mask port-tag)] |
| 2268 |
[(E x) (nop)]) |
| 2269 |
|
| 2270 |
;(define-primop input-port? safe |
| 2271 |
; [(P x) (sec-tag-test (T x) vector-mask vector-tag #f input-port-tag)] |
| 2272 |
; [(E x) (nop)]) |
| 2273 |
; |
| 2274 |
;(define-primop output-port? safe |
| 2275 |
; [(P x) (sec-tag-test (T x) vector-mask vector-tag #f output-port-tag)] |
| 2276 |
; [(E x) (nop)]) |
| 2277 |
(define port-attrs-shift 6) |
| 2278 |
|
| 2279 |
(define-primop $make-port unsafe |
| 2280 |
[(V attrs idx sz buf tr id read write getp setp cl cookie) |
| 2281 |
(with-tmp ([p (prm 'alloc (K (align port-size)) (K vector-tag))]) |
| 2282 |
(prm 'mset p (K (- vector-tag)) |
| 2283 |
(prm 'logor (prm 'sll (T attrs) (K port-attrs-shift)) (K port-tag))) |
| 2284 |
(prm 'mset p (K (- disp-port-index vector-tag)) (T idx)) |
| 2285 |
(prm 'mset p (K (- disp-port-size vector-tag)) (T sz)) |
| 2286 |
(prm 'mset p (K (- disp-port-buffer vector-tag)) (T buf)) |
| 2287 |
(prm 'mset p (K (- disp-port-transcoder vector-tag)) (T tr)) |
| 2288 |
(prm 'mset p (K (- disp-port-id vector-tag)) (T id)) |
| 2289 |
(prm 'mset p (K (- disp-port-read! vector-tag)) (T read)) |
| 2290 |
(prm 'mset p (K (- disp-port-write! vector-tag)) (T write)) |
| 2291 |
(prm 'mset p (K (- disp-port-get-position vector-tag)) (T getp)) |
| 2292 |
(prm 'mset p (K (- disp-port-set-position! vector-tag)) (T setp)) |
| 2293 |
(prm 'mset p (K (- disp-port-close vector-tag)) (T cl)) |
| 2294 |
(prm 'mset p (K (- disp-port-cookie vector-tag)) (T cookie)) |
| 2295 |
(prm 'mset p (K (- disp-port-unused1 vector-tag)) (K 0)) |
| 2296 |
(prm 'mset p (K (- disp-port-unused2 vector-tag)) (K 0)) |
| 2297 |
p)]) |
| 2298 |
|
| 2299 |
(define-primop $port-index unsafe |
| 2300 |
[(V x) (prm 'mref (T x) (K (- disp-port-index vector-tag)))]) |
| 2301 |
(define-primop $port-size unsafe |
| 2302 |
[(V x) (prm 'mref (T x) (K (- disp-port-size vector-tag)))]) |
| 2303 |
(define-primop $port-buffer unsafe |
| 2304 |
[(V x) (prm 'mref (T x) (K (- disp-port-buffer vector-tag)))]) |
| 2305 |
(define-primop $port-transcoder unsafe |
| 2306 |
[(V x) (prm 'mref (T x) (K (- disp-port-transcoder vector-tag)))]) |
| 2307 |
(define-primop $port-id unsafe |
| 2308 |
[(V x) (prm 'mref (T x) (K (- disp-port-id vector-tag)))]) |
| 2309 |
(define-primop $port-read! unsafe |
| 2310 |
[(V x) (prm 'mref (T x) (K (- disp-port-read! vector-tag)))]) |
| 2311 |
(define-primop $port-write! unsafe |
| 2312 |
[(V x) (prm 'mref (T x) (K (- disp-port-write! vector-tag)))]) |
| 2313 |
(define-primop $port-get-position unsafe |
| 2314 |
[(V x) (prm 'mref (T x) (K (- disp-port-get-position vector-tag)))]) |
| 2315 |
(define-primop $port-set-position! unsafe |
| 2316 |
[(V x) (prm 'mref (T x) (K (- disp-port-set-position! vector-tag)))]) |
| 2317 |
(define-primop $port-close unsafe |
| 2318 |
[(V x) (prm 'mref (T x) (K (- disp-port-close vector-tag)))]) |
| 2319 |
(define-primop $port-cookie unsafe |
| 2320 |
[(V x) (prm 'mref (T x) (K (- disp-port-cookie vector-tag)))]) |
| 2321 |
(define-primop $port-attrs unsafe |
| 2322 |
[(V x) |
| 2323 |
(prm 'sra |
| 2324 |
(prm 'mref (T x) (K (- disp-port-attrs vector-tag))) |
| 2325 |
(K port-attrs-shift))]) |
| 2326 |
(define-primop $port-tag unsafe |
| 2327 |
[(V x) |
| 2328 |
(make-conditional |
| 2329 |
(tag-test (T x) vector-mask vector-tag) |
| 2330 |
(with-tmp ([tag |
| 2331 |
(prm 'mref (T x) (K (- disp-port-attrs vector-tag)))]) |
| 2332 |
(make-conditional |
| 2333 |
(tag-test tag port-mask port-tag) |
| 2334 |
(prm 'sra tag (K port-attrs-shift)) |
| 2335 |
(K 0))) |
| 2336 |
(K 0))]) |
| 2337 |
|
| 2338 |
(define-primop $set-port-index! unsafe |
| 2339 |
[(E x i) (prm 'mset (T x) (K (- disp-port-index vector-tag)) (T i))]) |
| 2340 |
(define-primop $set-port-size! unsafe |
| 2341 |
[(E x i) (prm 'mset (T x) (K (- disp-port-size vector-tag)) (T i))]) |
| 2342 |
(define-primop $set-port-attrs! unsafe |
| 2343 |
[(E x i) |
| 2344 |
(prm 'mset (T x) |
| 2345 |
(K (- disp-port-attrs vector-tag)) |
| 2346 |
(prm 'logor (prm 'sll (T i) (K port-attrs-shift)) (K port-tag)))]) |
| 2347 |
|
| 2348 |
|
| 2349 |
/section) |
| 2350 |
|
| 2351 |
(section ;;; interrupts-and-engines |
| 2352 |
|
| 2353 |
(define-primop $interrupted? unsafe |
| 2354 |
[(P) (prm '!= (prm 'mref pcr (K pcb-interrupted)) (K 0))]) |
| 2355 |
|
| 2356 |
(define-primop $unset-interrupted! unsafe |
| 2357 |
[(E) (prm 'mset pcr (K pcb-interrupted) (K 0))]) |
| 2358 |
|
| 2359 |
(define-primop $do-event safe |
| 2360 |
[(E) |
| 2361 |
(begin |
| 2362 |
(interrupt) |
| 2363 |
(prm 'incr/zero? pcr (K pcb-engine-counter) |
| 2364 |
(K (fxsll 1 fx-shift))))]) |
| 2365 |
|
| 2366 |
(define-primop $swap-engine-counter! unsafe |
| 2367 |
[(V x) |
| 2368 |
;;; FIXME: should be atomic swap instead of load and set! |
| 2369 |
(with-tmp ([x0 (T x)]) |
| 2370 |
(with-tmp ([t (prm 'mref pcr (K pcb-engine-counter))]) |
| 2371 |
(prm 'mset pcr (K pcb-engine-counter) x0) |
| 2372 |
t))]) |
| 2373 |
|
| 2374 |
(define-primop $stack-overflow-check unsafe |
| 2375 |
[(E) |
| 2376 |
(make-shortcut |
| 2377 |
(make-conditional |
| 2378 |
(make-primcall 'u< |
| 2379 |
(list esp (make-primcall 'mref |
| 2380 |
(list pcr (make-constant pcb-frame-redline))))) |
| 2381 |
(make-primcall 'interrupt '()) |
| 2382 |
(make-primcall 'nop '())) |
| 2383 |
(make-forcall "ik_stack_overflow" '()))]) |
| 2384 |
|
| 2385 |
/section) |
| 2386 |
|
| 2387 |
(section ;;; control operations |
| 2388 |
|
| 2389 |
(define-primop $fp-at-base unsafe |
| 2390 |
[(P) |
| 2391 |
(prm '= (prm 'int+ |
| 2392 |
(prm 'mref pcr (K pcb-frame-base)) |
| 2393 |
(K (- wordsize))) |
| 2394 |
fpr)]) |
| 2395 |
|
| 2396 |
(define-primop $current-frame unsafe |
| 2397 |
[(V) (prm 'mref pcr (K pcb-next-continuation))]) |
| 2398 |
|
| 2399 |
|
| 2400 |
(define-primop $seal-frame-and-call unsafe |
| 2401 |
[(V x) ;;; PCB NEXT CONT;;; PCB BASE |
| 2402 |
(with-tmp ([k (prm 'alloc (K continuation-size) (K vector-tag))]) |
| 2403 |
(with-tmp ([base (prm 'int+ |
| 2404 |
(prm 'mref pcr (K pcb-frame-base)) |
| 2405 |
(K (- wordsize)))]) |
| 2406 |
(with-tmp ([underflow-handler (prm 'mref base (K 0))]) |
| 2407 |
(prm 'mset k (K (- vector-tag)) (K continuation-tag)) |
| 2408 |
(prm 'mset k (K (- disp-continuation-top vector-tag)) fpr) |
| 2409 |
(prm 'mset k (K (- disp-continuation-next vector-tag)) |
| 2410 |
(prm 'mref pcr (K pcb-next-continuation))) |
| 2411 |
(prm 'mset k (K (- disp-continuation-size vector-tag)) (prm 'int- base fpr)) |
| 2412 |
(prm 'mset pcr (K pcb-next-continuation) k) |
| 2413 |
(prm 'mset pcr (K pcb-frame-base) fpr) |
| 2414 |
(prm '$call-with-underflow-handler underflow-handler (T x) k))))] |
| 2415 |
[(E . args) (interrupt)] |
| 2416 |
[(P . args) (interrupt)]) |
| 2417 |
|
| 2418 |
(define-primop $frame->continuation unsafe |
| 2419 |
[(V x) |
| 2420 |
(with-tmp ([t (prm 'alloc |
| 2421 |
(K (align (+ disp-closure-data wordsize))) |
| 2422 |
(K closure-tag))]) |
| 2423 |
(prm 'mset t (K (- disp-closure-code closure-tag)) |
| 2424 |
(K (make-code-loc (sl-continuation-code-label)))) |
| 2425 |
(prm 'mset t (K (- disp-closure-data closure-tag)) |
| 2426 |
(T x)) |
| 2427 |
t)] |
| 2428 |
[(P x) (K #t)] |
| 2429 |
[(E x) (nop)]) |
| 2430 |
|
| 2431 |
(define-primop $make-call-with-values-procedure unsafe |
| 2432 |
[(V) (K (make-closure |
| 2433 |
(make-code-loc (sl-cwv-label)) |
| 2434 |
'() #f))] |
| 2435 |
[(P) (interrupt)] |
| 2436 |
[(E) (interrupt)]) |
| 2437 |
|
| 2438 |
(define-primop $make-values-procedure unsafe |
| 2439 |
[(V) (K (make-closure |
| 2440 |
(make-code-loc (sl-values-label)) |
| 2441 |
'() #f))] |
| 2442 |
[(P) (interrupt)] |
| 2443 |
[(E) (interrupt)]) |
| 2444 |
|
| 2445 |
(define-primop $make-annotated-procedure unsafe |
| 2446 |
[(V annotation proc) |
| 2447 |
(let ([off (- disp-closure-code closure-tag)]) |
| 2448 |
(with-tmp ([t (prm 'alloc |
| 2449 |
(K (align (+ disp-closure-data (* 2 wordsize)))) |
| 2450 |
(K closure-tag))]) |
| 2451 |
(prm 'mset t (K (- disp-closure-code closure-tag)) |
| 2452 |
(K (make-code-loc (sl-annotated-procedure-label)))) |
| 2453 |
(prm 'mset t (K (- disp-closure-data closure-tag)) |
| 2454 |
(T annotation)) |
| 2455 |
(prm 'mset t (K (- (+ disp-closure-data wordsize) closure-tag)) |
| 2456 |
(T proc)) |
| 2457 |
t))] |
| 2458 |
[(P) (interrupt)] |
| 2459 |
[(E) (interrupt)]) |
| 2460 |
|
| 2461 |
(define-primop $annotated-procedure-annotation unsafe |
| 2462 |
[(V proc) |
| 2463 |
(prm 'mref (T proc) |
| 2464 |
(K (- disp-closure-data closure-tag)))]) |
| 2465 |
|
| 2466 |
|
| 2467 |
/section) |
| 2468 |
|
| 2469 |
(section ;;; hash table tcbuckets |
| 2470 |
|
| 2471 |
(define-primop $make-tcbucket unsafe |
| 2472 |
[(V tconc key val next) |
| 2473 |
(with-tmp ([x (prm 'alloc (K (align tcbucket-size)) (K vector-tag))]) |
| 2474 |
(prm 'mset x (K (- disp-tcbucket-tconc vector-tag)) (T tconc)) |
| 2475 |
(prm 'mset x (K (- disp-tcbucket-key vector-tag)) (T key)) |
| 2476 |
(prm 'mset x (K (- disp-tcbucket-val vector-tag)) (T val)) |
| 2477 |
(prm 'mset x (K (- disp-tcbucket-next vector-tag)) (T next)) |
| 2478 |
x)]) |
| 2479 |
|
| 2480 |
(define-primop $tcbucket-key unsafe |
| 2481 |
[(V x) (prm 'mref (T x) (K (- disp-tcbucket-key vector-tag)))]) |
| 2482 |
(define-primop $tcbucket-val unsafe |
| 2483 |
[(V x) (prm 'mref (T x) (K (- disp-tcbucket-val vector-tag)))]) |
| 2484 |
(define-primop $tcbucket-next unsafe |
| 2485 |
[(V x) (prm 'mref (T x) (K (- disp-tcbucket-next vector-tag)))]) |
| 2486 |
|
| 2487 |
(define-primop $set-tcbucket-key! unsafe |
| 2488 |
[(E x v) (mem-assign v (T x) (- disp-tcbucket-key vector-tag))]) |
| 2489 |
(define-primop $set-tcbucket-val! unsafe |
| 2490 |
[(E x v) (mem-assign v (T x) (- disp-tcbucket-val vector-tag))]) |
| 2491 |
(define-primop $set-tcbucket-next! unsafe |
| 2492 |
[(E x v) (mem-assign v (T x) (- disp-tcbucket-next vector-tag))]) |
| 2493 |
(define-primop $set-tcbucket-tconc! unsafe |
| 2494 |
[(E x v) (mem-assign v (T x) (- disp-tcbucket-tconc vector-tag))]) |
| 2495 |
|
| 2496 |
|
| 2497 |
/section) |
| 2498 |
|
| 2499 |
(section ;;; codes |
| 2500 |
|
| 2501 |
(define-primop code? unsafe |
| 2502 |
[(P x) (sec-tag-test (T x) vector-mask vector-tag #f code-tag)]) |
| 2503 |
|
| 2504 |
(define-primop $closure-code unsafe |
| 2505 |
[(V x) |
| 2506 |
(prm 'int+ |
| 2507 |
(prm 'mref (T x) (K (- disp-closure-code closure-tag))) |
| 2508 |
(K (- vector-tag disp-code-data)))]) |
| 2509 |
|
| 2510 |
(define-primop $code-freevars unsafe |
| 2511 |
[(V x) (prm 'mref (T x) (K (- disp-code-freevars vector-tag)))]) |
| 2512 |
|
| 2513 |
(define-primop $code-reloc-vector unsafe |
| 2514 |
[(V x) (prm 'mref (T x) (K (- disp-code-relocsize vector-tag)))]) |
| 2515 |
|
| 2516 |
(define-primop $code-size unsafe |
| 2517 |
[(V x) (prm 'mref (T x) (K (- disp-code-instrsize vector-tag)))]) |
| 2518 |
|
| 2519 |
(define-primop $code-annotation unsafe |
| 2520 |
[(V x) (prm 'mref (T x) (K (- disp-code-annotation vector-tag)))]) |
| 2521 |
|
| 2522 |
(define-primop $code->closure unsafe |
| 2523 |
[(V x) |
| 2524 |
(with-tmp ([v (prm 'alloc |
| 2525 |
(K (align (+ 0 disp-closure-data))) |
| 2526 |
(K closure-tag))]) |
| 2527 |
(prm 'mset v |
| 2528 |
(K (- disp-closure-code closure-tag)) |
| 2529 |
(prm 'int+ (T x) |
| 2530 |
(K (- disp-code-data vector-tag)))) |
| 2531 |
v)]) |
| 2532 |
|
| 2533 |
(define-primop $code-ref unsafe |
| 2534 |
[(V x i) |
| 2535 |
(prm 'sll |
| 2536 |
(prm 'logand |
| 2537 |
(prm 'bref (T x) |
| 2538 |
(prm 'int+ |
| 2539 |
(prm 'sra (T i) (K fx-shift)) |
| 2540 |
(K (- disp-code-data vector-tag)))) |
| 2541 |
(K 255)) |
| 2542 |
(K fx-shift))]) |
| 2543 |
|
| 2544 |
(define-primop $code-set! unsafe |
| 2545 |
[(E x i v) |
| 2546 |
(prm 'bset (T x) |
| 2547 |
(prm 'int+ |
| 2548 |
(prm 'sra (T i) (K fx-shift)) |
| 2549 |
(K (- disp-code-data vector-tag))) |
| 2550 |
(prm 'sra (T v) (K fx-shift)))]) |
| 2551 |
|
| 2552 |
(define-primop $set-code-annotation! unsafe |
| 2553 |
[(E x v) (mem-assign v (T x) (- disp-code-annotation vector-tag))]) |
| 2554 |
|
| 2555 |
/section) |
| 2556 |
|
| 2557 |
(section ; transcoders |
| 2558 |
|
| 2559 |
(define-primop transcoder? unsafe |
| 2560 |
[(P x) (tag-test (T x) transcoder-mask transcoder-tag)]) |
| 2561 |
|
| 2562 |
(define-primop $data->transcoder unsafe |
| 2563 |
[(V x) (prm 'logor |
| 2564 |
(prm 'sll (T x) (K (- transcoder-payload-shift |
| 2565 |
fx-shift))) |
| 2566 |
(K transcoder-tag))]) |
| 2567 |
(define-primop $transcoder->data unsafe |
| 2568 |
[(V x) (prm 'sra (T x) (K (- transcoder-payload-shift fx-shift)))]) |
| 2569 |
/section) |
| 2570 |
|