iwillspeak / feersum

A Scheme compiler that targets .NET written in F#
http://playlist.feersum-scheme.net
MIT License
24 stars 1 forks source link

Stop Allocating Shadow Locals for Captured Values #16

Closed iwillspeak closed 3 years ago

iwillspeak commented 3 years ago

When a value is captured into the environment in Lower.rewriteExpression we don't update the locals count. This means we will still allocate a local slot for the captured value even though we have re-written any references to it to now point to the environment slot instead.

This is a bit of an in-depth change however as if we remove shaddow locals then local indicies would no longer match up with the indices in EmitCtx.Locals.

The solution to this might be to have Lambda values own the entire list of locals and filter that down as we move storage into the environment. Another option might be to re-write local indices when we have moved some items into the environment to get rid of any 'holes'.

(define (foo)
  (define (bar x)
    (if (< 10 x)
      x
      (bar (- 1 x))))
  (bar 10))

In the above example the local for bar captures itself, but we would still allocate local 0 for it.