[SOLVED] Possible bug in code generated for shifts in 64bit mode lazarus / 62-General ---------------------------------------------------------------------- 440bx · 2021-05-30 00:31 Hello, consider the following test programCode: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$APPTYPE CONSOLE} program _bits; uses  math,  sysutils  ; var  i : ptruint;  v : ptruint;  s : qword; function IsDebuggerPresent()         : wordbool; stdcall external 'kernel32'; begin  { this loop produces the expected result                                    }   v := 1;  for i := 1 to 64 do  begin    v := v * 2;    writeln('  i : ',i:2, '  v : ', (v - 1):23, '  ', IntToHex(v - 1, 0):16);  end;   writeln;  writeln;   { this loop should produce the same result but does not                   }   s := 0;  for i := 1 to 63 do  begin    if IsDebuggerPresent() and (i = 30) then asm int 3 end;     { it works with i = 30 but not 31 and above                               }     s := (1 shl i);    writeln('  i : ', i:2, '  s : ', s - 1:23, '  ', IntToHex(s - 1, 0):16);  end;   writeln;  writeln('press ENTER/RETURN to end this program.');  readln;end.                 In theory, the result of both loops should be the same but, the first loop generates the correct values (2^n - 1) whereas the second does not. Inspecting the code generated (FPC v3.0.4 in 64bit mode), it uses an SHL instruction instead of an SHLD instruction.  This causes the resulting values to be incorrect. 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 BIts.lpr:47                               s := (1 shl i);0000000100001745 b801000000               mov    $0x1,%eax000000010000174A 8b0db0e80100             mov    0x1e8b0(%rip),%ecx        # 0x1000200000000000100001750 4863c9                   movslq %ecx,%rcx0000000100001753 d3e0                     shl    %cl,%eax0000000100001755 4863c0                   movslq %eax,%rax0000000100001758 488905c1e80100           mov    %rax,0x1e8c1(%rip)        # 0x100020020  All comments welcome. ---------------------------------------------------------------------- 440bx · 2021-05-30 01:31 In this case the problem isn't related to type promotion and demotion.  The problem is that in 64 bit and for a 64 bit type, it should be using SHLD not SHL. ---------------------------------------------------------------------- 440bx · 2021-05-30 03:11 Quote from: jamie on May 30, 2021, 04:48:16 amThere is your problem.. In this case, the compiler may have actually worked correctly this time.. 1 being a constant normally results in a integer and seeing its on the left side the net results was a 32 bit . casting the constant to a QWord then forces this type to be a 64 bit. You're right!.  Just tried it and it works when the typecast to qword is present.  if not present, it uses eax instead rax even though the destination is declared as a 64bit magnitude. You're very likely right again when you stated the behavior won't be considered a bug even though I would still consider it as one. Thank you for pointing that out.  That's very helpful.