Array enumeration bug with opmisation level 2 for target 80386 ? lazarus / 64-FPC development ---------------------------------------------------------------------- Nitorami · 2018-07-29 11:48 I have tracked down an obstinate bug in my program, and isolated it in a small piece of code, see below. Can someone please confirm with FPC3.0.4, win32, and the parameters -O2 -Op80386 -Mdelphi (or -Mobjfpc) Procedure TMyClass.TestModes runs a loop over the 4 array elements of fOutputs and prints their length. The correct result should be Quote10 10 10 10 But with optimization level 2, and optimization target processor 80386, I get this Quote10 10 0 Runtime error 204 at $004034A0   $004034A0   $00401651 The issue is rather sensitive to small changes elsewhere in the code, and may be somewhat random. I did not manage to reproduce it with optimization off, nor when optimising for a different target processor than 80386, so I guess it is a compiler bug. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i type TPoint = record x,y: integer; end; type  TOutput = object    OName: Ansistring;    OConns  : integer;    TargetList    : array of TPoint;    procedure Init;  end;   TMyClass = Class     fOutputs: array [0..3] of TOutput;     procedure TestModes;    constructor create;  end; //-------------------------------procedure TOutput.Init;begin  OName := '';  OConns := 0;  SetLength (TargetList,10);end; //-------------------------------constructor TMyClass.Create;var n: integer;begin  for n := 0 to high (fOutputs) do fOutputs[n].Init;end; procedure TMyClass.TestModes;var  OP: TOutput;begin  for OP in fOutputs do begin  //Enumeration seems to go wrong    writeln (length(OP.Targetlist));  end;end; //-------------------------------var C1: TMyClass;begin  C1 := TMyClass.Create;  C1.TestModes;  C1.Free;end. ---------------------------------------------------------------------- marcov · 2018-07-29 12:46 Yes. Trunk seems to have it also, please file it as bug on bugs.freepascal.org ---------------------------------------------------------------------- ASerge · 2018-07-29 13:13 Confirm. Win32. Found the difference No optimization, without -Op80386. OK Code: ASM  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i No optimization, without -Op80386:# [31] for n := 0 to high (fOutputs) do fOutputs[n].Init;        movl    $0,-12(%ebp)        subl    $1,-12(%ebp)        .balign 4,0x90.Lj40:        addl    $1,-12(%ebp)      movl    -8(%ebp),%edx        movl    -12(%ebp),%eax ; This OK      imull   $12,%eax,%eax        leal    4(%edx,%eax),%eax        call    P$PROGRAM$_$TOUTPUT_$__$$_INIT No optimization, with -Op80386. OK Code: ASM  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i # [31] for n := 0 to high (fOutputs) do fOutputs[n].Init;        movl    $0,-12(%ebp)        subl    $1,-12(%ebp)        .balign 4,0x90.Lj40:      addl    $1,-12(%ebp)        movl    -8(%ebp),%edx        movl    -12(%ebp),%eax        imull   $12,%eax,%eax  ; This OK        leal    4(%edx,%eax),%eax        call    P$PROGRAM$_$TOUTPUT_$__$$_INIT Optimization, without -Op80386. OK Code: ASM  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i # [31] for n := 0 to high (fOutputs) do fOutputs[n].Init;        movl    $0,-12(%ebp)        subl    $1,-12(%ebp)        .balign 4,0x90.Lj40:      addl    $1,-12(%ebp)        movl    -8(%ebp),%edx        movl    -12(%ebp),%eax        imull   $12,%eax,%eax  ; This OK        leal    4(%edx,%eax),%eax        call    P$PROGRAM$_$TOUTPUT_$__$$_INIT Optimization, -Op80386. Error Code: ASM  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i # [31] for n := 0 to high (fOutputs) do fOutputs[n].Init;        movl    $0,-12(%ebp)        subl    $1,-12(%ebp)        .balign 4,0x90.Lj40:      addl    $1,-12(%ebp)        movl    -8(%ebp),%edx        movl    -12(%ebp),%eax        leal    (,%eax,4),%eax  ; This eax*4        leal    (%eax,%eax,8),%eax; This eax*4 + (eax*4)*8 = eax*36        leal    4(%edx,%eax),%eax        call    P$PROGRAM$_$TOUTPUT_$__$$_INIT With optimization and -Op80386 line 9,10 do multiplication by 36, not by 12. ---------------------------------------------------------------------- Nitorami · 2018-07-29 13:23 Thanks. I'll file a bug report. https://bugs.freepascal.org/view.php?id=34051