Changeset 31151

Show
Ignore:
Timestamp:
03/13/09 22:20:29 (4 years ago)
Author:
mokehehe
Message:

バイトコードのリストを一段減らしてみた

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • lang/scheme/3imp/4.7.scm

    r31147 r31151  
    5151    (compile-refer x e 
    5252                   (if (set-member? x s) 
    53                        (list INDIRECT next) 
     53                       (list* INDIRECT next) 
    5454                     next))) 
    5555   ((pair? x) 
    5656    (record-case x 
    57                  (quote (obj) (list CONSTANT obj next)) 
     57                 (quote (obj) (list* CONSTANT obj next)) 
    5858                 (lambda (vars . bodies) 
    5959                   (compile-lambda vars bodies e s next)) 
     
    6464                       (let ((thenc (compile then e s next)) 
    6565                             (elsec (compile else e s next))) 
    66                          (compile test e s (list TEST thenc elsec))))) 
     66                         (compile test e s (list* TEST thenc elsec))))) 
    6767                 (set! (var x) 
    6868                       (compile-lookup var e 
    69                                        (lambda (n)   (compile x e s (list ASSIGN-LOCAL n next))) 
    70                                        (lambda (n)   (compile x e s (list ASSIGN-FREE n next))) 
    71                                        (lambda (sym) (compile x e s (list ASSIGN-GLOBAL sym next))))) 
     69                                       (lambda (n)   (compile x e s (list* ASSIGN-LOCAL n next))) 
     70                                       (lambda (n)   (compile x e s (list* ASSIGN-FREE n next))) 
     71                                       (lambda (sym) (compile x e s (list* ASSIGN-GLOBAL sym next))))) 
    7272                 (call/cc (x) 
    73                           (let ((c (list CONTI (tail? next) 
    74                                          (list ARGUMENT 
     73                          (let ((c (list* CONTI (tail? next) 
     74                                         (list* ARGUMENT 
    7575                                               (compile x e s 
    7676                                                        (if (tail? next) 
    77                                                             (list SHIFT 
     77                                                            (list* SHIFT 
    7878                                                                  1 
    7979                                                                  (list APPLY 1)) 
     
    8181                            (if (tail? next) 
    8282                                c 
    83                               (list FRAME c next)))) 
     83                              (list* FRAME c next)))) 
    8484                 (else 
    8585                  (let ((func (car x)) 
    8686                        (args (cdr x))) 
    8787                    (compile-apply func args e s next))))) 
    88    (else (list CONSTANT x next)))) 
     88   (else (list* CONSTANT x next)))) 
    8989 
    9090(define (compile-apply func args e s next) 
     
    9494      (let ((c (compile func e s 
    9595                        (if (tail? next) 
    96                             (list SHIFT 
     96                            (list* SHIFT 
    9797                                  argnum 
    9898                                  (list APPLY argnum)) 
     
    101101          (if (tail? next) 
    102102              bc 
    103             (list FRAME bc next))))))) 
     103            (list* FRAME bc next))))))) 
    104104 
    105105(define (compile-apply-args ap c e s) 
     
    110110           (loop (cdr ap) 
    111111                 (compile (car ap) e s 
    112                           (list ARGUMENT c)))))) 
     112                          (list* ARGUMENT c)))))) 
    113113 
    114114(define (direct-invoke? func) 
     
    151151                          (compile-lambda-bodies (car e) bodies (cdr e) sets s 
    152152                                                 next)) 
    153             (let ((c (list DIRECT-INVOKE (length args2) 
     153            (let ((c (list* DIRECT-INVOKE (length args2) 
    154154                           (make-boxes sets proper-vars 
    155155                                       (compile-lambda-bodies ext-vars bodies (cdr e) sets s 
     
    157157                                                                    ((eq? (car next) RETURN-DIRECT) 
    158158                                                                     (let ((argnum2 (cadr next)) 
    159                                                                            (nnext (caddr next))) 
    160                                                                        (list RETURN-DIRECT 
     159                                                                           (nnext (cddr next))) 
     160                                                                       (list* RETURN-DIRECT 
    161161                                                                             (+ (length args2) argnum2) 
    162162                                                                             nnext))) 
    163163                                                                    (else 
    164                                                                      (list RETURN-DIRECT (length args2) 
     164                                                                     (list* RETURN-DIRECT (length args2) 
    165165                                                                           next)))))))) 
    166166              (compile-apply-args args2 c e s)))))))) 
     
    177177                          -1)))) 
    178178      (collect-free free e 
    179                     (list CLOSE 
     179                    (list* CLOSE 
    180180                          varnum 
    181181                          (length free) 
     
    221221    (collect-free (cdr vars) e 
    222222                  (compile-refer (car vars) e 
    223                                  (list ARGUMENT next))))) 
     223                                 (list* ARGUMENT next))))) 
    224224 
    225225(define (find-setses xs v) 
     
    250250             next 
    251251           (if (set-member? (car vars) sets) 
    252                (list BOX n (f (cdr vars) (+ n 1))) 
     252               (list* BOX n (f (cdr vars) (+ n 1))) 
    253253             (f (cdr vars) (+ n 1)))))) 
    254254 
    255255(define (compile-refer x e next) 
    256256  (compile-lookup x e 
    257                   (lambda (n)   (list REFER-LOCAL n next)) 
    258                   (lambda (n)   (list REFER-FREE n next)) 
    259                   (lambda (sym) (list REFER-GLOBAL sym next)))) 
     257                  (lambda (n)   (list* REFER-LOCAL n next)) 
     258                  (lambda (n)   (list* REFER-FREE n next)) 
     259                  (lambda (sym) (list* REFER-GLOBAL sym next)))) 
    260260 
    261261(define (find-index x ls) 
     
    332332;                        (error "stack error")) 
    333333                      a) 
    334                 (REFER-LOCAL (n x) 
     334                (REFER-LOCAL (n . x) 
    335335                             (VM (index f n) x f c s)) 
    336                 (REFER-FREE (n x) 
     336                (REFER-FREE (n . x) 
    337337                            (VM (index-closure c n) x f c s)) 
    338                 (REFER-GLOBAL (sym x) 
     338                (REFER-GLOBAL (sym . x) 
    339339                              (VM (refer-global sym (lambda () (error "unbound variable:" sym))) x f c s)) 
    340                 (INDIRECT (x) 
     340                (INDIRECT x 
    341341                          (VM (unbox a) x f c s)) 
    342                 (CONSTANT (obj x) 
     342                (CONSTANT (obj . x) 
    343343                          (VM obj x f c s)) 
    344                 (CLOSE (argnum n body x) 
     344                (CLOSE (argnum n body . x) 
    345345                       (VM (closure argnum body n s) x f c (- s n))) 
    346                 (BOX (n x) 
     346                (BOX (n . x) 
    347347                     (index-set! f n (box (index f n))) 
    348348                     (VM a x f c s)) 
    349                 (TEST (then else) 
     349                (TEST (then . else) 
    350350                      (VM a (if a then else) f c s)) 
    351                 (ASSIGN-LOCAL (n x) 
     351                (ASSIGN-LOCAL (n . x) 
    352352                              (set-box! (index f n) a) 
    353353                              (VM a x f c s)) 
    354                 (ASSIGN-FREE (n x) 
     354                (ASSIGN-FREE (n . x) 
    355355                             (set-box! (index-closure c n) a) 
    356356                             (VM a x f c s)) 
    357                 (ASSIGN-GLOBAL (sym x) 
     357                (ASSIGN-GLOBAL (sym . x) 
    358358                               (assign-global! sym a) 
    359359                               (VM a x f c s)) 
    360                 (CONTI (tail? x) 
     360                (CONTI (tail? . x) 
    361361                       (VM (continuation s tail?) x f c s)) 
    362                 (NUATE (stack x) 
     362                (NUATE (stack . x) 
    363363                       (VM a x f c (restore-stack stack))) 
    364                 (FRAME (x ret) 
     364                (FRAME (x . ret) 
    365365                       (VM a x f c (push ret (push f (push c s))))) 
    366                 (ARGUMENT (x) 
     366                (ARGUMENT x 
    367367                          (VM a x f c (push a s))) 
    368                 (SHIFT (n x) 
     368                (SHIFT (n . x) 
    369369                       (let ((m (index s (- n 1))))  ; 一つ前の呼び出しの引数の数 
    370370                         (VM a x f c (shift-args n m s)))) 
     
    379379                          (let ((s (- s argnum))) 
    380380                            (VM a (index s 0) (index s 1) (index s 2) (- s 4))))) 
    381                 (DIRECT-INVOKE (argnum x) 
     381                (DIRECT-INVOKE (argnum . x) 
    382382                               (expand-frame argnum f s) 
    383383                               (VM a x (+ f argnum) c s)) 
    384                 (RETURN-DIRECT (n x) 
     384                (RETURN-DIRECT (n . x) 
    385385                               (shrink-frame n f s) 
    386386                               (VM a x (- f n) c (- s n))) 
     
    479479    (closure 
    480480     '(1 . 1)  ; argnum 
    481      (list REFER-LOCAL 0 
    482            (list NUATE (save-stack ss) 
     481     (list* REFER-LOCAL 0 
     482           (list* NUATE (save-stack ss) 
    483483                 (list RETURN))) 
    484484     0 
     
    577577  (vrecord-case x 
    578578                (HALT () (my-write `(HALT))) 
    579                 (REFER-LOCAL (n x) (my-write `(REFER-LOCAL ,n : ,(index f n)))) 
    580                 (REFER-FREE (n x) (my-write `(REFER-FREE ,n : ,(index-closure c n)))) 
    581                 (REFER-GLOBAL (sym x) (my-write `(REFER-GLOBAL ,sym : ,(refer-global sym error)))) 
    582                 (INDIRECT (x) (my-write `(INDIRECT : ,(unbox a)))) 
    583                 (CONSTANT (obj x) (my-write `(CONSTANT ,obj))) 
    584                 (CLOSE (argnum n body x) (my-write `(CLOSE ,argnum ,n))) 
    585                 (BOX (n x) (my-write `(BOX ,n : ,(index f n)))) 
    586                 (TEST (then else) (my-write `(TEST : ,a))) 
    587                 (ASSIGN-LOCAL (n x) (my-write `(ASSIGN-LOCAL ,n : ,a))) 
    588                 (ASSIGN-FREE (n x) (my-write `(ASSIGN-FREE ,n : ,a))) 
    589                 (ASSIGN-GLOBAL (sym x) (my-write `(ASSIGN-GLOBAL ,sym : ,a))) 
    590                 (CONTI (tail? x) (my-write `(CONTI ,tail?))) 
    591                 (NUATE (stack x) (my-write `(NUATE))) 
    592                 (FRAME (x ret) (my-write `(FRAME))) 
    593                 (ARGUMENT (x) (my-write `(ARGUMENT))) 
    594                 (SHIFT (n x) (my-write `(SHIFT ,n))) 
     579                (REFER-LOCAL (n . x) (my-write `(REFER-LOCAL ,n : ,(index f n)))) 
     580                (REFER-FREE (n . x) (my-write `(REFER-FREE ,n : ,(index-closure c n)))) 
     581                (REFER-GLOBAL (sym . x) (my-write `(REFER-GLOBAL ,sym : ,(refer-global sym error)))) 
     582                (INDIRECT x (my-write `(INDIRECT : ,(unbox a)))) 
     583                (CONSTANT (obj . x) (my-write `(CONSTANT ,obj))) 
     584                (CLOSE (argnum n body . x) (my-write `(CLOSE ,argnum ,n))) 
     585                (BOX (n . x) (my-write `(BOX ,n : ,(index f n)))) 
     586                (TEST (then . else) (my-write `(TEST : ,a))) 
     587                (ASSIGN-LOCAL (n . x) (my-write `(ASSIGN-LOCAL ,n : ,a))) 
     588                (ASSIGN-FREE (n . x) (my-write `(ASSIGN-FREE ,n : ,a))) 
     589                (ASSIGN-GLOBAL (sym . x) (my-write `(ASSIGN-GLOBAL ,sym : ,a))) 
     590                (CONTI (tail? . x) (my-write `(CONTI ,tail?))) 
     591                (NUATE (stack . x) (my-write `(NUATE))) 
     592                (FRAME (x . ret) (my-write `(FRAME))) 
     593                (ARGUMENT x (my-write `(ARGUMENT))) 
     594                (SHIFT (n . x) (my-write `(SHIFT ,n))) 
    595595                (APPLY (argnum) (my-write `(APPLY ,argnum))) 
    596596                (RETURN () (my-write `(RETURN))) 
    597                 (DIRECT-INVOKE (argnum x) (my-write `(DIRECT-INVOKE ,argnum))) 
    598                 (RETURN-DIRECT (n x) (my-write `(RETURN-DIRECT ,n))) 
     597                (DIRECT-INVOKE (argnum . x) (my-write `(DIRECT-INVOKE ,argnum))) 
     598                (RETURN-DIRECT (n . x) (my-write `(RETURN-DIRECT ,n))) 
    599599                (else 
    600600                 (error "unknown opcode" (car x))))