Presentation is loading. Please wait.

Presentation is loading. Please wait.

Effect-driven CPS transformation in interpreters

Similar presentations


Presentation on theme: "Effect-driven CPS transformation in interpreters"— Presentation transcript:

1 Effect-driven CPS transformation in interpreters
Dries Harnie

2 Table assignment struct { EXP tab,idx,val; } TAB;
VAL tbl_assign(TAB tAB) { VAL tab = eval(tAB->tab); VAL idx = eval(tAB->idx); VAL val = eval(tAB->val); tab[idx] = val; }

3 Tpico: thunks

4 Table assignment in Tpico
tab_thunk(TAB tAB) { TMP tmp = new tmp; tmp->tAB = tAB; thr_poke(ta1_thunk, tmp); eval(tAB->tab); } ta1_thunk(TMP_TAB tmp, VAL tab) { tmp->tab = tab; thr_poke(ta2_thunk, tmp); eval(tmp->tAB->idx); ta2_thunk(TMP_TAB tmp, VAL idx) { tmp->idx = idx; thr_poke(ta3_thunk, tmp); eval(tmp->tAB->val); } ta3_thunk(TMP_TAB tmp, VAL val) { tmp->val = val; thr_zap(); (tmp->tab)[tmp->idx] = tmp->val;

5 The perils of garbage collection
struct { EXP tab,idx,val; } TAB; VAL tbl_assign(TAB tAB) { VAL tab = eval(tAB->tab); VAL idx = eval(tAB->idx); VAL val = eval(tAB->val); tab[idx] = val; } GC can happen at any of these points!

6 Thunkmaster Looks like Scheme: (define (map f l) (case l
((cons x xs) (cons (f x) (map f xs))) ((nil) (nil)))) (define (fac x) (if (< x 1) 1 (* x (fac (- x 1))))) (define (eval-tbl-assign TAB) (let* ((tab (eval (get-tab TAB))) (idx (eval (get-idx TAB))) (val (eval (get-val TAB)))) (set-tab! tab idx val))) (define (thr-loop val) (let ((thk (current-thunk))) (thr-loop (thk val))))

7 Thunkmaster Has effects:
(define (map f l) (case l ((cons x xs) (cons (f x) (map f xs))) ((nil) (nil)))) Effects associate computations with side effects. If f ! print, then (map f l) ! print.

8 Thunkifying code (define (eval-tbl-assign TAB)
(let* ((tab (eval (get-tab TAB))) (idx (eval (get-idx TAB))) (val (eval (get-val TAB)))) (set-tab! tab idx val))) (define (eval-tbl-assign TAB) (eval (get-tab TAB) (lambda (tab) (eval (get-idx TAB) (lambda (idx) (eval (get-val TAB) (lambda (val) (set-tab! tab idx val))))))))

9 Thunkifying code (define (eval-tbl-assign TAB)
(eval (get-tab TAB) (lambda (tab) (eval (get-idx TAB) (lambda (idx) (eval (get-val TAB) (lambda (val) (set-tab! tab idx val)))))))) (define (eval-tbl-assign TAB) (defstruct (tmp tab idx)) (define (ta1 tmp tab) (thr-poke! ta2 (tmp-set-tab! Tmp tab)) (eval (get-idx TAB))) (define (ta2 tmp idx) (thr-poke! ta3 (tmp-set-idx! Tmp idx)) (eval (get-val TAB))) (define (ta3 tmp val) (thr-zap!) (set-tab! (tmp-get-tab tmp) (tmp-get-idx tmp) val))) (thr-poke! ta1 (new tmp)) (eval (get-tab TAB)))

10 Thunkifying code (defstruct (tmp TAB tab idx)) (define (ta1 tmp tab)
(thr-poke! ta2 (tmp-set-tab! Tmp tab)) (eval (get-idx (tmp-get-TAB tmp)))) (define (ta2 tmp idx) (thr-poke! ta3 (tmp-set-idx! Tmp idx)) (eval (get-val (tmp-get-TAB tmp)))) (define (ta3 tmp val) (thr-zap!) (set-tab! (tmp-get-tab tmp) (tmp-get-idx tmp) val))) (define (eval-tbl-assign TAB) (thr-poke! ta1 (tmp-set-TAB! (new tmp) TAB)) (eval (get-tab TAB)))

11 Thesis: goals Use effect system to determine where CPS is needed.
Find other places where CPS could help Tpico.


Download ppt "Effect-driven CPS transformation in interpreters"

Similar presentations


Ads by Google