from exit to set does a story of gforth re implementation
play

From exit to set-does> A Story of Gforth Re-Implementation M. - PowerPoint PPT Presentation

From exit to set-does> A Story of Gforth Re-Implementation M. Anton Ertl, TU Wien Bernd Paysan, net2o [] exit execute Exit not tickable in Gforth 0.7 Standard-conforming, but Several complaints over the years Traditionally


  1. From exit to set-does> A Story of Gforth Re-Implementation M. Anton Ertl, TU Wien Bernd Paysan, net2o

  2. [´] exit execute • Exit not tickable in Gforth 0.7 • Standard-conforming, but • Several complaints over the years • Traditionally exit was a normal word

  3. Exit and locals : x { a b c } ... ; : y x [’] x execute ; old x head docol code field >l body >l locals >l stack . lp a . b . c lp+!# 24 exit ;s y head return docol code field stack call body rp lit execute ;s

  4. New implementation : x { a b c } ... ; : y x [’] x execute ; old new x head x head docol docolloc code field code field >l >l body body >l >l locals locals >l >l stack stack . . lp lp a a . . b b . . c c lp+!# ;s 24 exit ;s y head y head return return docol docol code field code field stack stack call call-loc body body rp rp lit lit execute execute ;s ;s lp-tramp

  5. Return-address manipulation : foo r> drop unlocal exit ; : bar { a } foo ;

  6. Optimization : x { a b c } ... ; : y x [’] x execute ; old new new optimized x head x head x head docol docolloc docolloc code field code field code field >l >l >l body body body >l >l >l locals locals locals >l >l >l stack stack stack . . . lp lp lp a a a . . . b b b . . . c c c lp+!# ;s unlocal 24 exit ;s ;s y head y head y head return return return docol docol docol code field code field code field stack stack stack call call-loc call-loc body body body rp rp rp lit lit lit execute execute execute ;s ;s ;s lp-tramp lp-tramp

  7. What about does> ? : const create , does> @ ; 5 const b : c b ; old const head docol code field call body create call , call : def (does) @ create ;s does> { a } [’] exit execute ; def x b head x dodoes code field 5 body c head docol code field does-exec body ;s

  8. New does> implementation : const create , does> @ ; 5 const b : c b ; old new const head const head docol docol code field code field call call body body create create call call , , call branch (does) @ docol ;s @ ;s lit call set-does> ;s b head b head dodoes dodoesxt code field code field 5 5 body body c head c head docol docol code field code field does-exec lit body body ;s call ;s

  9. Set-does> : const create , does> @ ; : const create , [’] @ set-does> ; 5 const b 5 const b : c b ; : c b ; old new const head const head const head docol docol docol code field code field code field call call call body body body create create create call call call , , , call branch lit (does) @ @ docol call ;s @ set-does> ;s ;s lit call set-does> ;s b head b head b head dodoes dodoesxt dodoesxt code field code field code field @ 5 5 5 body body body c head c head c head docol docol docol code field code field code field does-exec lit lit body body body ;s call @ ;s ;s

  10. Other set-does> benefits : dofield ( -- ) does> ( name execution: addr1 -- addr2 ) @ + ; : dozerofield ( -- ) immediate does> ( name execution: -- ) drop ; : field ( align1 off1 : field ( align1 off1 align size "name" align size "name" -- align2 offset2 ) -- align2 offset2 ) 2 pick >r create-field r> if 2 pick >r create-field r> if dofield [: @ + ;] set-does> else else dozerofield [: ;] set-does> immediate then ; then ;

  11. Other set-does> benefits : doconst does> @ ; : const ( n "name" -- ) : const ( n "name" -- ) create , doconst create , [’] @ set-does> [: >body @ postpone literal ;] [: >body @ postpone literal ;] set-opt ; set-opt ;

  12. Performance : baseline ; cycles from mispredictions (K8, Haswell) : 0-locals { } ; old lp-trampoline unlocal : 3x0-locals { } ; : y { } ; : z { } ; old lp-trampoline unlocal 60 does> drop ; old new [’] drop set-does> 50 40 30 20 10 0 compile, execute compile, execute compile, execute compile, execute AMD64 instructions Haswell cycles K8 cycles ARM Cortex-A9 cycles

  13. Native-code caveats • Return has to match call • otherwise misprediction (20 cycles penalty) push data needed for cleaning up locals call rest-of-definition clean up locals ret rest-of-definition: ... ret

  14. Conclusion • [’] exit execute ⇒ new locals cleanup ⇒ unlocal ⇒ optimization ⇒ new does> implementation ⇒ set-does> • Helpful: intelligent compile, quotations • set-does> : more flexibility • Performance similar with optimizations set-does> can save a call level

Recommend


More recommend