[bitc-dev] Capture problem with by-ref

Swaroop Sridhar swaroop at cs.jhu.edu
Tue Mar 11 11:50:53 EDT 2008


Jonathan S. Shapiro wrote:
> I am inclined to prohibit this by declaring that no by-ref argument is
> permitted to be captured by a closure. The alternatives are:
> 
>   1. Remove BY-REF, but I don't want to do that because it is extremely
>      useful.
> 
>   2. Force the compiler to heapify any stack-based argument value that
>      is passed to a BY-REF formal parameter, effectively re-writing the
>      BY-REF as REF.
> 
>      Note that there is no issue here for objects that are already
>      heap-based. It is only stack frame capture that is the source of
>      concern.

I see the problem. There is actually another solution we can consider,
which is to not permit capture of by-ref arguments since this is a
form of escape.

First, in all of the examples in this thread, there is still no problem.
This is because, the by-ref argument is only used as a r-value, which 
induces an implicit de-reference.

>   (define (outer x:(by-ref 'a))
>     (lambda (y)
>       (pair y, x)))
> 
> The problem is that any invocation of OUTER captures x.

Here, the inner-lambda only needs to capture the r-value of x. So, we
only need to store the de-reference of x in the closure of the inner
lambda.

The question now is, what if the example were written as:

(define (outer x1:(by-ref 'a) x2:(by-ref 'a))
   (lambda (y)
      (set! x1 x2)))

Now, x1 is used as an l-value, and this will lead to the escape of the
pointer corresponding to the by-ref argument.

So, I think we can say that the by-ref arguments cannot be used in
an inner-lambda at an lvalue position.

Swaroop.



More information about the bitc-dev mailing list