picobit icon indicating copy to clipboard operation
picobit copied to clipboard

Used global variable being optimized out

Open abbrev opened this issue 9 years ago • 0 comments

If the modify-global-b! function is defined in the code below, the compiler does not generate any assembly code to set *global-a*. The compiler still generates code to set *global-b* to the value of *global-a* (which is the default value of #f).

(define *global-a* (cons 1 2))
(define *global-b* *global-a*)

(define (modify-global-b!) (set! *global-b* 1))

(displayln *global-b*)

Expected output: (1 . 2) Actual output: #f

Part of the assembly code generated with the code above:

  (push-constant 1)
  (push-constant 2)
  (prim cons)
  (pop)
  (push-global *global-a*)
  (set-global *global-b*)

If *global-a* is set to some other value, such as the result of an expression, or a number, or #t, or a list, or any other constant value, *global-a* is optimized out completely and *global-b* is used in its place. This gives the expected results.

If *global-a* is explicitly referenced anywhere after its definition, the (pop) is replaced with (set-global *global-a*) in the assembly code. This also gives the expected results.

This is on commit bde85aa193964b7151f2d0eb39d47058d4298fc6.

abbrev avatar Oct 14 '15 18:10 abbrev