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
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

From exit to set-does> A Story of Gforth Re-Implementation

  • M. Anton Ertl, TU Wien

Bernd Paysan, net2o

slide-2
SLIDE 2

[´] exit execute

  • Exit not tickable in Gforth 0.7
  • Standard-conforming, but
  • Several complaints over the years
  • Traditionally exit was a normal word
slide-3
SLIDE 3

Exit and locals

: x { a b c } ... ; : y x [’] x execute ;

  • ld

return stack y docol call lit execute ;s head code field body x docol >l >l >l . . . lp+!# 24 ;s head code field body locals stack a b c rp lp exit

slide-4
SLIDE 4

New implementation

: x { a b c } ... ; : y x [’] x execute ;

new

return stack y docol call-loc lit execute ;s head code field body x docolloc >l >l >l . . . ;s head code field body locals stack a b c lp-tramp rp lp

  • ld

return stack y docol call lit execute ;s head code field body x docol >l >l >l . . . lp+!# 24 ;s head code field body locals stack a b c rp lp exit

slide-5
SLIDE 5

Return-address manipulation

: foo r> drop unlocal exit ; : bar { a } foo ;

slide-6
SLIDE 6

Optimization

: x { a b c } ... ; : y x [’] x execute ;

new

return stack y docol call-loc lit execute ;s head code field body x docolloc >l >l >l . . . ;s head code field body locals stack a b c lp-tramp rp lp

  • ld

return stack y docol call lit execute ;s head code field body x docol >l >l >l . . . lp+!# 24 ;s head code field body locals stack a b c rp lp exit

new optimized

return stack y docol call-loc lit execute ;s head code field body x docolloc >l >l >l . . . unlocal ;s head code field body locals stack a b c lp-tramp rp lp

slide-7
SLIDE 7

What about does>?

: const create , does> @ ; 5 const b : c b ;

  • ld

head code field body const docol call create call , call (does) @ ;s b dodoes 5

c docol does-exec ;s

head code field body head code field body

: def create does> { a } [’] exit execute ; def x x

slide-8
SLIDE 8

New does> implementation

: const create , does> @ ; 5 const b : c b ;

new

  • ld

head code field body const docol call create call , call (does) @ ;s b dodoes 5

c docol does-exec ;s

head code field body head code field body const docol call create call , branch docol @ ;s lit call set-does> ;s

b dodoesxt 5

head code field body head code field body c docol lit call ;s head code field body

slide-9
SLIDE 9

Set-does>

: const create , does> @ ; 5 const b : c b ;

new

  • ld

head code field body const docol call create call , call (does) @ ;s b dodoes 5

c docol does-exec ;s

head code field body head code field body const docol call create call , branch docol @ ;s lit call set-does> ;s

b dodoesxt 5

head code field body head code field body c docol lit call ;s head code field body : const create , [’] @ set-does> ; 5 const b : c b ; const docol call create call , lit @ call set-does> ;s

b dodoesxt @ 5

head code field body head code field body c docol lit @ ;s head code field body

slide-10
SLIDE 10

Other set-does> benefits

: dofield ( -- ) does> ( name execution: addr1 -- addr2 ) @ + ; : dozerofield ( -- ) immediate does> ( name execution: -- ) drop ; : field ( align1 off1 align size "name"

  • align2 offset2 )

2 pick >r create-field r> if dofield else dozerofield then ; : field ( align1 off1 align size "name"

  • align2 offset2 )

2 pick >r create-field r> if [: @ + ;] set-does> else [: ;] set-does> immediate then ;

slide-11
SLIDE 11

Other set-does> benefits

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

slide-12
SLIDE 12

Performance

: baseline ; : 0-locals { } ;

  • ld

lp-trampoline unlocal : 3x0-locals { } ; : y { } ; : z { } ;

  • ld

lp-trampoline unlocal does> drop ;

  • ld

new [’] drop set-does> cycles from mispredictions (K8, Haswell) compile, execute compile, execute compile, execute compile, execute AMD64 instructions Haswell cycles K8 cycles ARM Cortex-A9 cycles 10 20 30 40 50 60

slide-13
SLIDE 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

slide-14
SLIDE 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