Compiler produces wrong code when using option -gt

Summary

When I compile a code using -O3 optimization level together with the option -gt, the compiler produces a wrong code for functions that does a for-loop using Result as index.

System Information

  • Operating system: Linux Ubuntu 20.04 LTS
  • Processor architecture: x86-64
  • Compiler version: 3.3.1 revision 4f9acc10
  • Device: Computer

Steps to reproduce

build the example code using the command fpc -O3 -gt project1

Example Project

program project1;

{$mode objfpc}

function CharPos(const S: AnsiString; const C: AnsiChar; const Index: SizeInt): SizeInt;
begin
  for Result := Index to Length(S) do
    if S[Result] = C then
      Exit;
  Result := 0;
end;

begin
  Writeln(CharPos('Hello!', '!', 1));
end.

What is the current bug behavior?

Runtime error 216 at $00000000004010BE
  $00000000004010BE

What is the expected (correct) behavior?

6

Assignee Loading
Time tracking Loading