Fault compilation

Create issue
Issue #46 resolved
Takashi Kato repo owner created an issue

This type of code is compiled wrongly;

(define (prob k l)
  ((k (let loop ((l l) (r '()))
    (if (null? l)
        (reverse! r)
        (loop (cdr l) (cons (car l) r)))))
   (cons 'a 'b)))
(prob (lambda (l) (lambda (p) p)) '(1 2 3))
;; -> should be '(a . b) but SEGV

The compiled code is

;; size: 33
;;    0: CONST_PUSH a
;;    2: CONST b
;;    4: CONS_PUSH
;;    5: FRAME 25
;;    7: LREF_PUSH(1)
;;    8: CONST_PUSH ()
;;   10: LREF(8) ** <-- should be LREF(9) **
;;   11: BNNULL 8                  ; (if (null? l) (reverse! r) (lo ...
;;   13: FRAME 4
;;   15: LREF_PUSH(9)
;;   16: GREF_CALL(1) #<identifier reverse!#user (0x804027e0):0>; (reverse! r)
;;   18: JUMP 8
;;   20: LREF_CDR_PUSH(8)
;;   21: LREF_CAR_PUSH(8)
;;   22: LREF(9)
;;   23: CONS_PUSH
;;   24: SHIFTJ(2 8)
;;   25: JUMP -16
;;   27: LEAVE(2)
;;   28: PUSH
;;   29: LREF(0)
;;   30: CALL(1)
;;   31: TAIL_CALL(1)
;;   32: RET

The index 10 is wrong. This is because compiler doesn't consider how many argument already pushed to calculate offset of frame.

Comments (1)

  1. Log in to comment