The program
--- snip ---
(bitc-version "0.10")
(import bitc.main as main)
(provide bitc.main main)

(define (get-setter byrefX:(by-ref 'a) v:word)
  (lambda ()
    (set! byrefX v)))

(define main.main:(fn (vector string) -> int32)
  (lambda (argv)
    (let ((localX 3450)) ((get-setter localX 3451)))
    0))
--- snip ---
generates invalid C code:
--- snip ---
siel...@fenchurch ~/working/bitc/code $ bitcc -I ~/include Main.bitc
bitc.out.c: In 
function '_17Main_SH0_DTget_HYsetter_SHFN2Z_4word_4wordFN0_4unit':
bitc.out.c:2329: warning: assignment makes integer from pointer without a cast
bitc.out.c:2331: warning: passing argument 1 of 'ct___clenv8982' makes pointer 
from integer without a cast
--- snip ---

I had a look at the generated code. To me the problem seems to be that the 
closure structure captures the lvalue as such but the initialization of the 
structure is wrong: It allocates storage on the heap for a copy but fails to 
dereference the pointer to the lvalue to actually copy its value. This causes 
the first error.
Next the constructor for the closure is called. It expects a lvalue as pointer 
but gets the dereferenced newly allocated heap location. This causes the 
second error.

I was interested in what happens when I create an invalid reference by 
capturing it in a closure. The specification was a bit unclear in that area 
(BitC Language Specification, Version 0.10+, 7.13.1):
  [...]
  By-reference parameters can only escape as part of a first class
  procedure, but the lifetime of a by-reference parameter cannot
  exceed the lifetime of its containing scope.
  [...]
If by-reference parameters are allowed to escape as such, this "cannot" is 
hard to enforce.
One way would be to not allow by-reference parameters to be captured at all 
(unless it has been proven that the closure will not outlive the referenced 
value). Another way would be to automatically capture a copy.

The compiler seems on the way to the second alternative.
But is it then necessary to allocate a dedicated heap cell for the captured 
value? It could directly be part of the closure structure.

But this automatic copying feels a bit awkward to me. I think I would prefer 
to have the capturing disallowed. That would force the user to explicitly 
copy. But then the copy could never be part of the closure structure...

-- 
MfG Marvin H. Sielenkemper
_______________________________________________
bitc-dev mailing list
[email protected]
http://www.coyotos.org/mailman/listinfo/bitc-dev

Reply via email to