View Issue Details

IDProjectCategoryView StatusLast Update
0036389FPCCompilerpublic2019-12-09 23:01
ReporterDmitry Belkevich_2Assigned ToFlorian 
PrioritynormalSeverityminorReproducibilityalways
Status resolvedResolutionfixed 
Platform64xOSWindowsOS Version10
Product Version3.3.1Product Build62316M 
Target VersionFixed in Version3.3.1 
Summary0036389: compile code with error "Error: Asm: word value exceeds bounds 65536"
DescriptionTry to comile a code and get such error: unit1.pas(46,17) Error: Asm: word value exceeds bounds 65536
In some other builds and Delphi compiled without errors
Steps To ReproduceJust compile the code:

function Correct(TempInt: integer; Value: word): word; inline;
begin
 if TempInt = 32768 then
   Result := Value - TempInt
  else
   Result := 65536 - Value;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
 Arr: array of word;
begin
 Arr[0] := Correct(Temp, Arr[0]);
end;
TagsNo tags attached.
Fixed in Revision43670
FPCOldBugId
FPCTarget-
Attached Files
  • i36389-fix.patch (1,164 bytes)
    Index: compiler/x86/aoptx86.pas
    ===================================================================
    --- compiler/x86/aoptx86.pas	(revision 43626)
    +++ compiler/x86/aoptx86.pas	(working copy)
    @@ -2247,8 +2247,8 @@
                               debug_op2str(taicpu(hp2).opcode)+debug_opsize2str(taicpu(hp2).opsize)+')',p);
                         { limit size of constants as well to avoid assembler errors, but
                           check opsize to avoid overflow when left shifting the 1 }
    -                    if (taicpu(p).oper[0]^.typ=top_const) and (topsize2memsize[taicpu(hp2).opsize]<=4) then
    -                      taicpu(p).oper[0]^.val:=taicpu(p).oper[0]^.val and ((qword(1) shl (topsize2memsize[taicpu(hp2).opsize]*8))-1);
    +                    if (taicpu(p).oper[0]^.typ=top_const) and (topsize2memsize[taicpu(hp2).opsize]<=63) then
    +                      taicpu(p).oper[0]^.val:=taicpu(p).oper[0]^.val and ((qword(1) shl topsize2memsize[taicpu(hp2).opsize])-1);
                         taicpu(hp1).changeopsize(taicpu(hp2).opsize);
                         taicpu(p).changeopsize(taicpu(hp2).opsize);
                         if taicpu(p).oper[0]^.typ=top_reg then
    
    i36389-fix.patch (1,164 bytes)

Activities

Marģers

2019-12-02 12:18

reporter   ~0119579

unable reproduce on x86_64 linux
It's not clear what variable "Temp" is. Maybe there is hidden source of error.

J. Gareth Moreton

2019-12-02 12:31

developer   ~0119581

Can you change up the project slightly so it's obvious what Temp is? Thanks.

Martok

2019-12-02 14:05

reporter   ~0119582

Confirmed on i386-win32 using internal assembler. amd64 is indeed fine.

The type of Temp doesn't matter, can be any ordinal type, but not a literal. For example:

procedure TForm1.Button1Click(Sender: TObject);
var
 Arr: array of word;
 Temp: integer;
begin
 Temp:= 42;
 Arr[0] := Correct(Temp, Arr[0]);
end;

Marģers

2019-12-02 14:58

reporter   ~0119585

Last edited: 2019-12-02 15:00

View 2 revisions

confirm on x86_64 linux. Internal assembler and {$r-} {$q-}

error on lines:
Result := 65536 - Value;
Arr[0] := Correct(Temp, Arr[0]);

J. Gareth Moreton

2019-12-02 15:28

developer   ~0119586

Can you confirm if this patch fixes the problem? The bug was with the "MovOpMov2MovOp" optimisation. The check to see if the value was in range mixed up bits with bytes (topsize2memsize converts an operand size into a bit length) and so ultimately was never performed.

i36389-fix.patch (1,164 bytes)
Index: compiler/x86/aoptx86.pas
===================================================================
--- compiler/x86/aoptx86.pas	(revision 43626)
+++ compiler/x86/aoptx86.pas	(working copy)
@@ -2247,8 +2247,8 @@
                           debug_op2str(taicpu(hp2).opcode)+debug_opsize2str(taicpu(hp2).opsize)+')',p);
                     { limit size of constants as well to avoid assembler errors, but
                       check opsize to avoid overflow when left shifting the 1 }
-                    if (taicpu(p).oper[0]^.typ=top_const) and (topsize2memsize[taicpu(hp2).opsize]<=4) then
-                      taicpu(p).oper[0]^.val:=taicpu(p).oper[0]^.val and ((qword(1) shl (topsize2memsize[taicpu(hp2).opsize]*8))-1);
+                    if (taicpu(p).oper[0]^.typ=top_const) and (topsize2memsize[taicpu(hp2).opsize]<=63) then
+                      taicpu(p).oper[0]^.val:=taicpu(p).oper[0]^.val and ((qword(1) shl topsize2memsize[taicpu(hp2).opsize])-1);
                     taicpu(hp1).changeopsize(taicpu(hp2).opsize);
                     taicpu(p).changeopsize(taicpu(hp2).opsize);
                     if taicpu(p).oper[0]^.typ=top_reg then
i36389-fix.patch (1,164 bytes)

Marģers

2019-12-02 18:42

reporter   ~0119592

yep, patch fixes linux x86_64

J. Gareth Moreton

2019-12-02 18:55

developer   ~0119593

Last edited: 2019-12-02 18:55

View 2 revisions

Excellent - just need confirmation on the i386 platforms and I think we're good to merge.

Florian

2019-12-09 23:01

administrator   ~0119719

Thanks, applied (tested also on i386-linux).

Issue History

Date Modified Username Field Change
2019-12-02 09:49 Dmitry Belkevich_2 New Issue
2019-12-02 12:18 Marģers Note Added: 0119579
2019-12-02 12:31 J. Gareth Moreton Note Added: 0119581
2019-12-02 14:05 Martok Note Added: 0119582
2019-12-02 14:58 Marģers Note Added: 0119585
2019-12-02 15:00 Marģers Note Edited: 0119585 View Revisions
2019-12-02 15:28 J. Gareth Moreton File Added: i36389-fix.patch
2019-12-02 15:28 J. Gareth Moreton Note Added: 0119586
2019-12-02 15:29 J. Gareth Moreton Status new => feedback
2019-12-02 15:29 J. Gareth Moreton FPCTarget => -
2019-12-02 18:42 Marģers Note Added: 0119592
2019-12-02 18:55 J. Gareth Moreton Note Added: 0119593
2019-12-02 18:55 J. Gareth Moreton Note Edited: 0119593 View Revisions
2019-12-09 23:01 Florian Assigned To => Florian
2019-12-09 23:01 Florian Status feedback => resolved
2019-12-09 23:01 Florian Resolution open => fixed
2019-12-09 23:01 Florian Fixed in Version => 3.3.1
2019-12-09 23:01 Florian Fixed in Revision => 43670
2019-12-09 23:01 Florian Note Added: 0119719