Changeset 31324 for lang/scheme

Show
Ignore:
Timestamp:
03/17/09 23:33:05 (4 years ago)
Author:
mokehehe
Message:

命令の名前を短くしてみる。
吐かれた命令のオプティマイザ追加。

Location:
lang/scheme/3imp
Files:
3 added
2 modified

Legend:

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

    r31243 r31324  
    2222 
    2323(defops HALT 
    24         REFER-LOCAL 
    25         REFER-FREE 
    26         REFER-GLOBAL 
    27         INDIRECT 
    28         CONSTANT 
     24        LREF 
     25        FREF 
     26        GREF 
     27        UNBOX 
     28        CONST 
    2929        CLOSE 
    3030        BOX 
    3131        TEST 
    32         ASSIGN-LOCAL 
    33         ASSIGN-FREE 
    34         ASSIGN-GLOBAL 
     32        LSET 
     33        FSET 
     34        GSET 
    3535        CONTI 
    3636        NUATE 
    3737        FRAME 
    38         ARGUMENT 
     38        ARG 
    3939        SHIFT 
    4040        APPLY 
    41         RETURN 
     41        RET 
    4242         
    43         DIRECT-INVOKE 
    44         RETURN-DIRECT 
     43        EXPAND 
     44        SHRINK 
    4545        ) 
    4646 
     
    5151    (compile-refer x e 
    5252                   (if (set-member? x s) 
    53                        (list* INDIRECT next) 
     53                       (list* UNBOX next) 
    5454                     next))) 
    5555   ((pair? x) 
    5656    (record-case x 
    57                  (quote (obj) (list* CONSTANT obj next)) 
     57                 (quote (obj) (list* CONST obj next)) 
    5858                 (lambda (vars . bodies) 
    5959                   (compile-lambda vars bodies e s next)) 
     
    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* LSET n next))) 
     70                                       (lambda (n)   (compile x e s (list* FSET n next))) 
     71                                       (lambda (sym) (compile x e s (list* GSET sym next))))) 
    7272                 (call/cc (x) 
    7373                          (let ((c (list* CONTI (tail? next) 
    74                                          (list* ARGUMENT 
     74                                         (list* ARG 
    7575                                               (compile x e s 
    7676                                                        (if (tail? next) 
     
    8686                        (args (cdr x))) 
    8787                    (compile-apply func args e s next))))) 
    88    (else (list* CONSTANT x next)))) 
     88   (else (list* CONST x next)))) 
    8989 
    9090(define (compile-apply func args e s next) 
     
    110110           (loop (cdr ap) 
    111111                 (compile (car ap) e s 
    112                           (list* ARGUMENT c)))))) 
     112                          (list* ARG c)))))) 
    113113 
    114114(define (direct-invoke? func) 
     
    147147        (let ((args2 (check-argnum varnum args))) 
    148148          (if (null? proper-vars) 
    149               ;; 変数なし(lambda () ...):DIRECT-INVOKE~RETURN-DIRECT 自体必要ない 
     149              ;; 変数なし(lambda () ...):EXPAND~SHRINK 自体必要ない 
    150150              (make-boxes sets proper-vars 
    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* EXPAND (length args2) 
    154154                           (make-boxes sets proper-vars 
    155155                                       (compile-lambda-bodies ext-vars bodies (cdr e) sets s 
    156156                                                              (cond ((tail? next) next) 
    157                                                                     ((eq? (car next) RETURN-DIRECT) 
     157                                                                    ((eq? (car next) SHRINK) 
    158158                                                                     (let ((argnum2 (cadr next)) 
    159159                                                                           (nnext (cddr next))) 
    160                                                                        (list* RETURN-DIRECT 
     160                                                                       (list* SHRINK 
    161161                                                                             (+ (length args2) argnum2) 
    162162                                                                             nnext))) 
    163163                                                                    (else 
    164                                                                      (list* RETURN-DIRECT (length args2) 
     164                                                                     (list* SHRINK (length args2) 
    165165                                                                           next)))))))) 
    166166              (compile-apply-args args2 c e s)))))))) 
     
    181181                          (length free) 
    182182                          (make-boxes sets proper-vars 
    183                                       (compile-lambda-bodies proper-vars bodies free sets s (list RETURN))) 
     183                                      (compile-lambda-bodies proper-vars bodies free sets s (list RET))) 
    184184                          next))))) 
    185185 
     
    221221    (collect-free (cdr vars) e 
    222222                  (compile-refer (car vars) e 
    223                                  (list* ARGUMENT next))))) 
     223                                 (list* ARG next))))) 
    224224 
    225225(define (find-setses xs v) 
     
    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* LREF n next)) 
     258                  (lambda (n)   (list* FREF n next)) 
     259                  (lambda (sym) (list* GREF sym next)))) 
    260260 
    261261(define (find-index x ls) 
     
    274274 
    275275(define (tail? next) 
    276   (eq? (car next) RETURN)) 
     276  (eq? (car next) RET)) 
    277277 
    278278 
     
    332332;                        (error "stack error")) 
    333333                      a) 
    334                 (REFER-LOCAL (n . x) 
     334                (LREF (n . x) 
    335335                             (VM (index f n) x f c s)) 
    336                 (REFER-FREE (n . x) 
     336                (FREF (n . x) 
    337337                            (VM (index-closure c n) x f c s)) 
    338                 (REFER-GLOBAL (sym . x) 
     338                (GREF (sym . x) 
    339339                              (VM (refer-global sym (lambda () (error "unbound variable:" sym))) x f c s)) 
    340                 (INDIRECT x 
     340                (UNBOX x 
    341341                          (VM (unbox a) x f c s)) 
    342                 (CONSTANT (obj . x) 
     342                (CONST (obj . x) 
    343343                          (VM obj x f c s)) 
    344344                (CLOSE (argnum n body . x) 
     
    349349                (TEST (then . else) 
    350350                      (VM a (if a then else) f c s)) 
    351                 (ASSIGN-LOCAL (n . x) 
     351                (LSET (n . x) 
    352352                              (set-box! (index f n) a) 
    353353                              (VM a x f c s)) 
    354                 (ASSIGN-FREE (n . x) 
     354                (FSET (n . x) 
    355355                             (set-box! (index-closure c n) a) 
    356356                             (VM a x f c s)) 
    357                 (ASSIGN-GLOBAL (sym . x) 
     357                (GSET (sym . x) 
    358358                               (assign-global! sym a) 
    359359                               (VM a x f c s)) 
     
    364364                (FRAME (x . ret) 
    365365                       (VM a x f c (push ret (push f (push c s))))) 
    366                 (ARGUMENT x 
     366                (ARG x 
    367367                          (VM a x f c (push a s))) 
    368368                (SHIFT (n . x) 
     
    370370                         (VM a x f c (shift-args n m s)))) 
    371371                (APPLY (argnum) 
    372                                            (do-apply argnum a s)) 
    373                 (RETURN () 
     372                       (do-apply argnum a s)) 
     373                (RET () 
    374374                        (do-return a s)) 
    375                 (DIRECT-INVOKE (argnum . x) 
     375                (EXPAND (argnum . x) 
    376376                               (expand-frame argnum f s) 
    377377                               (VM a x (+ f argnum) c s)) 
    378                 (RETURN-DIRECT (n . x) 
     378                (SHRINK (n . x) 
    379379                               (shrink-frame n f s) 
    380380                               (VM a x (- f n) c (- s n))) 
     
    489489    (closure 
    490490     '(1 . 1)  ; argnum 
    491      (list* REFER-LOCAL 0 
     491     (list* LREF 0 
    492492           (list* NUATE (save-stack ss) 
    493                  (list RETURN))) 
     493                 (list RET))) 
    494494     0 
    495495     0))) 
     
    587587  (vrecord-case x 
    588588                (HALT () (my-write `(HALT))) 
    589                 (REFER-LOCAL (n . x) (my-write `(REFER-LOCAL ,n : ,(index f n)))) 
    590                 (REFER-FREE (n . x) (my-write `(REFER-FREE ,n : ,(index-closure c n)))) 
    591                 (REFER-GLOBAL (sym . x) (my-write `(REFER-GLOBAL ,sym : ,(refer-global sym error)))) 
    592                 (INDIRECT x (my-write `(INDIRECT : ,(unbox a)))) 
    593                 (CONSTANT (obj . x) (my-write `(CONSTANT ,obj))) 
     589                (LREF (n . x) (my-write `(LREF ,n : ,(index f n)))) 
     590                (FREF (n . x) (my-write `(FREF ,n : ,(index-closure c n)))) 
     591                (GREF (sym . x) (my-write `(GREF ,sym : ,(refer-global sym error)))) 
     592                (UNBOX x (my-write `(UNBOX : ,(unbox a)))) 
     593                (CONST (obj . x) (my-write `(CONST ,obj))) 
    594594                (CLOSE (argnum n body . x) (my-write `(CLOSE ,argnum ,n))) 
    595595                (BOX (n . x) (my-write `(BOX ,n : ,(index f n)))) 
    596596                (TEST (then . else) (my-write `(TEST : ,a))) 
    597                 (ASSIGN-LOCAL (n . x) (my-write `(ASSIGN-LOCAL ,n : ,a))) 
    598                 (ASSIGN-FREE (n . x) (my-write `(ASSIGN-FREE ,n : ,a))) 
    599                 (ASSIGN-GLOBAL (sym . x) (my-write `(ASSIGN-GLOBAL ,sym : ,a))) 
     597                (LSET (n . x) (my-write `(LSET ,n : ,a))) 
     598                (FSET (n . x) (my-write `(FSET ,n : ,a))) 
     599                (GSET (sym . x) (my-write `(GSET ,sym : ,a))) 
    600600                (CONTI (tail? . x) (my-write `(CONTI ,tail?))) 
    601601                (NUATE (stack . x) (my-write `(NUATE))) 
    602602                (FRAME (x . ret) (my-write `(FRAME))) 
    603                 (ARGUMENT x (my-write `(ARGUMENT))) 
     603                (ARG x (my-write `(ARG))) 
    604604                (SHIFT (n . x) (my-write `(SHIFT ,n))) 
    605605                (APPLY (argnum) (my-write `(APPLY ,argnum))) 
    606                 (RETURN () (my-write `(RETURN))) 
    607                 (DIRECT-INVOKE (argnum . x) (my-write `(DIRECT-INVOKE ,argnum))) 
    608                 (RETURN-DIRECT (n . x) (my-write `(RETURN-DIRECT ,n))) 
     606                (RET () (my-write `(RET))) 
     607                (EXPAND (argnum . x) (my-write `(EXPAND ,argnum))) 
     608                (SHRINK (n . x) (my-write `(SHRINK ,n))) 
    609609                (else 
    610610                 (error "unknown opcode" (car x)))) 
     
    639639 
    640640(define (my-write s) 
     641  (define (abbrev? s sym) 
     642    (and (eq? (car s) sym) 
     643         (not (null? (cdr s))) 
     644         (null? (cddr s)))) 
    641645  (cond ((my-procedure? s) 
    642646         (display "#<proc>")) 
    643647        ((pair? s) 
    644          (display "(") 
    645          (let loop ((first #t) 
    646                     (ls s)) 
    647            (if (pair? ls) 
    648                (begin 
    649                  (unless first (display " ")) 
    650                  (my-write (car ls)) 
    651                  (loop #f (cdr ls))) 
    652              (when (not (null? ls)) 
    653                (display " . ") 
    654                (my-write ls)))) 
    655          (display ")")) 
     648         (cond ((abbrev? s 'quote) (display #\') (my-write (cadr s))) 
     649               ((abbrev? s 'quasiquote) (display #\`) (my-write (cadr s))) 
     650               (else 
     651                (display "(") 
     652                (let loop ((first #t) 
     653                           (ls s)) 
     654                  (if (pair? ls) 
     655                      (begin 
     656                        (unless first (display " ")) 
     657                        (my-write (car ls)) 
     658                        (loop #f (cdr ls))) 
     659                    (when (not (null? ls)) 
     660                      (display " . ") 
     661                      (my-write ls)))) 
     662                (display ")")))) 
    656663        (else 
    657664         (write s)))) 
    658665 
    659666(define (comp x s) 
    660   (compile (comp-macroexpand-from-stack x s) '(()) '() (list RETURN))) 
     667  (compile (comp-macroexpand-from-stack x s) '(()) '() (list RET))) 
    661668 
    662669(define (run code s) 
     
    787794  (define-primitive-function2 (vm-apply args s) 
    788795                              (apply vm-apply s (cadr args) (cddr args))) 
     796 
     797  (define-primitive-function2 (macroexpand args s) 
     798                              (comp-macroexpand-from-stack (car args) s)) 
     799  (define-primitive-function2 (macroexpand-1 args s) 
     800                              (comp-macroexpand-1-from-stack (car args) s)) 
     801  (define-primitive-function2 (disasm args s) 
     802                              (let ((f (car args))) 
     803                                (if (and (not (primitive-function? f)) 
     804                                         (my-procedure? f)) 
     805                                    (closure-body f) 
     806                                  #f))) 
    789807  ) 
    790808 
     
    806824(define-special-form (quote . all) 
    807825  `(quote ,@all)) 
     826 
     827(define-special-form (lambda vars . bodies) 
     828  `(lambda ,vars ,@(map comp-macroexpand-1 bodies))) 
    808829 
    809830(define-special-form (if . all) 
     
    904925                       (error "illegal case exp" clause)) 
    905926                      ((pair? (car clause)) 
    906                        `(((eq? ,g ',(caar clause)) ,@(cdr clause)) 
     927                       `((,(let loop ((p (car clause))) 
     928                             (cond ((not (pair? p)) #f) 
     929                                   ((null? (cdr p)) `(eq? ,g ',(car p))) 
     930                                   (else `(if (eq? ,g ',(car p)) 
     931                                              #t 
     932                                            ,(loop (cdr p)))))) 
     933                          ,@(cdr clause)) 
    907934                         ,@(loop (cdr clauses)))) 
    908935                      ;; else 
  • lang/scheme/3imp/opsym2code.scm

    r31243 r31324  
    33(define optbl 
    44  '((HALT ()) 
    5     (REFER-LOCAL (n . $x)) 
    6     (REFER-FREE (n . $x)) 
    7     (REFER-GLOBAL (sym . $x)) 
    8     (INDIRECT $x) 
    9     (CONSTANT (obj . $x)) 
     5    (LREF (n . $x)) 
     6    (FREF (n . $x)) 
     7    (GREF (sym . $x)) 
     8    (UNBOX $x) 
     9    (CONST (obj . $x)) 
    1010    (CLOSE (argnum n $body . $x)) 
    1111    (BOX (n . $x)) 
    1212    (TEST ($then . $else)) 
    13     (ASSIGN-LOCAL (n . $x)) 
    14     (ASSIGN-FREE (n . $x)) 
    15     (ASSIGN-GLOBAL (sym . $x)) 
     13    (LSET (n . $x)) 
     14    (FSET (n . $x)) 
     15    (GSET (sym . $x)) 
    1616    (CONTI (tail$ . $x)) 
    1717    (NUATE (stack . $x)) 
    1818    (FRAME ($x . $ret)) 
    19     (ARGUMENT $x) 
     19    (ARG $x) 
    2020    (SHIFT (n . $x)) 
    2121    (APPLY (argnum)) 
    22     (RETURN ()) 
    23     (DIRECT-INVOKE (argnum . $x)) 
    24     (RETURN-DIRECT (n . $x)) 
     22    (RET ()) 
     23    (EXPAND (argnum . $x)) 
     24    (SHRINK (n . $x)) 
     25     
     26    (GREF-APPLY (sym n)) 
     27    (GREF-SHIFT-APPLY (sym n)) 
     28    (SHIFT-APPLY (n)) 
     29    (CONST-ARG (obj . $x)) 
    2530    )) 
    2631