PB 5.31 - Wrong ASM generated for sign extension

Post bugreports for the Windows version here
User avatar
Posts: 3706
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


; 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

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