fuzion icon indicating copy to clipboard operation
fuzion copied to clipboard

C backend: escape analysis fails for fields, works for functions

Open fridis opened this issue 8 months ago • 0 comments

Take this example (simplified from Cons, list and Sequence.reduce):

C ref is
  tail l => abstract

l : choice nil C is
  red =>
    say "hi"
    match l.this
      nil =>
      c C => c.tail.red

l1 => (ref : C is redef tail l := nil)
l2 l => (ref : C is redef tail l := l1)

l2.red

which works with the JVM backend:

 > ./build/bin/fz tt2.fz
hi
hi
hi

and crashes with the C backend:

 > ./build/bin/fz -c -o=tt2 tt2.fz; ./tt2
hi
hi
*** /tmp/fuzion_tt2_1479586643205897048.c:251: unhandled dynamic target 71 in access of `C.tail` within l.red(0 args) at /home/fridi/fuzion/clean/fuzion/tt2.fz:9:16:
      c C => c.tail.red

The problem is apparently related to tail being implemented as a field. If this is changed to a function for l2 as follows

C ref is
  tail l => abstract

l : choice nil C is
  red =>
    say "hi"
    match l.this
      nil =>
      c C => c.tail.red

l1 => (ref : C is redef tail l := nil)
l2 l => (ref : C is redef tail l => l1)

l2.red

it works both with JVM as well as with the C backend:

 > ./build/bin/fz tt2a.fz
hi
hi
hi
 > ./build/bin/fz -c -o=tt2a tt2a.fz; ./tt2a
hi
hi
hi

A slightly more complex example

  Cs ref is
    tail l => abstract

  S ref is
    al l => abstract
    red =>
      say "hi"
      match al
        nil    =>
        c Cs => c.tail.red

  l : choice nil Cs, S is
    redef al => l.this

  l1 l :=
    id l (ref : Cs is
            public redef tail l := nil)
  s1 S => l1
  l2 S =>
    id l (ref : Cs is
            public redef tail l := l1)

  l2.red

works with JVM backend

 > ./build/bin/fz tt.fz
hi
hi
hi

but stops printing early with the C backend:

 > ./build/bin/fz -c -o=tt tt.fz; ./tt
hi
hi

fridis avatar Apr 10 '25 14:04 fridis