vishapoberon / compiler

vishap oberon compiler
http://oberon.vishap.am
GNU General Public License v3.0
186 stars 25 forks source link

Segmentation fault when handle array in nested procedure #40

Closed Oleg-N-Cher closed 7 years ago

Oleg-N-Cher commented 7 years ago

Arthur has found this bug. It present in Ofront, Ofront+ and voc. Yet I do not have a solution. Maybe you look, Dave?

MODULE SpeedTest;
IMPORT Out := Console;

(* VAR m: ARRAY 50 OF INTEGER; - If array is declared here - all is ok *)

PROCEDURE F3(n: INTEGER): INTEGER;
VAR i: INTEGER;
    m: ARRAY 50 OF INTEGER; (* Segmentation fault when handle m[45] *)

  PROCEDURE f(n: INTEGER): INTEGER;
  BEGIN
    Out.Ln; Out.String('DEBUG: f('); Out.Int(n, 0); Out.String(') = ...'); Out.Ln;
    Out.Int(m[n], 0); Out.Ln;
    IF m[n] = -1 THEN m[n] := f(n - 1) + f(n - 2) END;
    RETURN m[n]
  END f;

BEGIN
  FOR i := 3 TO LEN(m) - 1 DO m[i] := -1 END;
  m[1] := 1; m[2] := 1;
  RETURN f(n)
END F3;

BEGIN
  Out.String('START.'); Out.Ln;
  Out.String('F3(45) = '); Out.Int(F3(45), 0); Out.Ln
END SpeedTest.
dcwbrown commented 7 years ago

An inner procedure accessing the local variable of an outer procedure.

That's different address calculation from either global or local variables.

Was it working in OFront before you integrated my changes?

On 2016-08-17 23:13, Oleg N. Cher wrote:

Arthur has found this bug. It present in Ofront, Ofront+ and voc. Yet I do not have a solution. Maybe you look, Dave?

MODULE SpeedTest; IMPORT Out := Console;

(* VAR m: ARRAY 50 OF INTEGER; - If array is declared here - all is ok *)

PROCEDURE F3(n: INTEGER): INTEGER; VAR i: INTEGER; m: ARRAY 50 OF INTEGER; (* Segmentation fault when handle m[45] *)

PROCEDURE f(n: INTEGER): INTEGER; BEGIN Out.Ln; Out.String('DEBUG: f('); Out.Int(n, 0); Out.String(') = ...'); Out.Ln; Out.Int(m[n], 0); Out.Ln; IF m[n] = -1 THEN m[n] := f(n - 1) + f(n - 2) END; RETURN m[n] END f;

BEGIN FOR i := 3 TO LEN(m) - 1 DO m[i] := -1 END; m[1] := 1; m[2] := 1; RETURN f(n) END F3;

BEGIN Out.String('START.'); Out.Ln; Out.String('F3(45) = '); Out.Int(F3(45), 0); Out.Ln END SpeedTest.

You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub [1], or mute the thread [2].

Links:

[1] https://github.com/vishaps/voc/issues/40 [2] https://github.com/notifications/unsubscribe-auth/ADChoHFD8vaPtpr62ne_Maa8Owg3cVuLks5qg4d8gaJpZM4Jm8V1

Oleg-N-Cher commented 7 years ago

Dave, it's not your bug and not mine. It exists in original Ofront v1.3

I suppose it went unnoticed because of the fact that Josef and other Ofront users use nested procedures (as type-bound procedures) very little.