PB 5.31 - Wrong ASM generated for sign extension

Post bugreports for the Windows version here
User avatar
luis
Addict
Addict
Posts: 3722
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

PB 5.31 - Wrong ASM generated for sign extension

Post by luis »

Well not always, in this specific case at least, probably in other cases too.

This should really be fixed, it's unnerving every time you mix different data sizes in an expression not knowing if you are about to activate this kind of bug !

I'm reporting this because I was asked to, but probably it would be too hard to fix this (rigid/fragile compiler) and it will stay as it is, like other similar compiler bugs :cry:

Code: Select all

EnableExplicit

; for x86 the bug is present for .b, .w 
; for x64 the bug is present for .b, .w, .l

; so when b2 is smaller than the architecture's native data type 

Define a = 0
Define b1 = 1
Define b2.w = 2

Procedure MyProcedure (x)
 Debug "x = " + x
EndProcedure

MyProcedure (a - (b1 - b2)) ; ok

a = a + (a - MyProcedure (a - (b1 - b2))) ; same params passed to MyProcedure(), yet this time the debug statement shows they are getting destroyed
debug wrote: [00:32:35] x = 1
[00:32:35] x = 0

Code: Select all

; Define a = 0
  MOV    dword [v_a],0
; Define b1 = 1
  MOV    dword [v_b1],1
; Define b2.w = 2
  MOV    word [v_b2],2
; 
; 
; MyProcedure (a - (b1 - b2))
  MOV    ebx,dword [v_a]
  MOV    edi,dword [v_b1]
  MOVSX  eax,word [v_b2]
  SUB    edi,eax
  SUB    ebx,edi
  PUSH   ebx
  CALL  _Procedure0
; 
; a = a + (a - MyProcedure (a - (b1 - b2)))
  MOV    ebx,dword [v_a]
  MOV    edi,dword [v_a]
  MOV    esi,dword [v_a]
  MOV    eax,dword [v_b1]
  MOVSX  eax,word [v_b2] ; <--- extends overwriting b1 (!!)
  SUB    eax,eax ; <--- same register (!!!), this wipes away (zero) the sub expression result (b1 - b2) 
  SUB    esi,eax
  PUSH   esi
  CALL  _Procedure0
  SUB    edi,eax
  ADD    ebx,edi
  MOV    dword [v_a],ebx
; 

Original thread -> http://www.purebasic.fr/english/viewtop ... =7&t=61214
User avatar
luis
Addict
Addict
Posts: 3722
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: PB 5.31 - Wrong ASM generated for sign extension

Post by luis »

This looks like one of the cases where leaving the upcoming C compiler backend generate the ASM code can solve a PB compiler bug. I've tried with the x86 C backend in Alpha 5 and it works correctly.
This is good news and one of the nice side effect of having a mainstream C compiler generating code!
A less good one is the bug remained undisturbed for 7 years, and it will probably continue to be there in the ASM backend...
Comfort
User
User
Posts: 17
Joined: Thu Jul 05, 2018 11:52 pm

Re: PB 5.31 - Wrong ASM generated for sign extension

Post by Comfort »

Code: Select all

Procedure MyProcedure (x)
 Debug "x = " + x
EndProcedure
Could it be that your old (we were all 6 years younger then) procedure has no return value?
User avatar
luis
Addict
Addict
Posts: 3722
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: PB 5.31 - Wrong ASM generated for sign extension

Post by luis »

Hi, I don't think so because in PB omitting ProcedureReturn is equivalent to ProcedureReturn 0.
Anyway I tried adding a ProcedureReturn 0 and the behaviour is the same.
Post Reply