strange code generation with float (x64)

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
xorc1zt
Enthusiast
Enthusiast
Posts: 276
Joined: Sat Jul 09, 2011 7:57 am

strange code generation with float (x64)

Post by xorc1zt »

hi

please take a look at the following generated code from pb x64

Code: Select all

Declare.f retFloat (*this, valFloat.f)

Interface FloatTest
    retFloat.f (valFloat.f)
EndInterface

DataSection
    vmt:
    Data.i @retFloat()
EndDataSection

Structure FLOATTEST_STR
    *vmt
EndStructure


Procedure.i FloatTest ()
    Define *newFloatTest.FLOATTEST_STR = AllocateMemory(SizeOf(FLOATTEST_STR))
    
    *newFloatTest\vmt = ?vmt
    ProcedureReturn *newFloatTest
EndProcedure

Procedure.f retFloat (*this, valFloat.f)
    ProcedureReturn valFloat + 0.458
EndProcedure

Define a.FloatTest = FloatTest()

Define f1.f
Define f2.f

f1 = retFloat(0, 10.0)

f2 = a\retFloat(10.0)
direct call with retFloat(0, 10.0)

Code: Select all

; f1 = retFloat(0, 10.0)
  FLD    dword [F2]         ; load 10.000000 to ST(0)
  SUB    rsp,8
  FSTP   dword [rsp]        ; push ST(0) onto the stack
  PUSH   qword 0
  POP    rcx
  MOVSS  xmm1,dword [rsp]   ; move 10.000000 from stack to XMM(1)
  ADD    rsp,8
  CALL  _Procedure0         ; direct call
  FSTP   dword [v_f1]       ; move result to f1
indirect call with a\retFloat(10.0)

Code: Select all

; f2 = a\retFloat(10.0)
  MOV    eax,1092616192     ; move 10.000000 to EAX (not from a address)
  PUSH   rax                ; push on stack
  MOVSS  xmm1,dword [rsp]   ; move 10.000000 from stack to XMM1
  ADD    rsp,8
  MOV    rax,qword [v_a]
  SUB    rsp,32
  MOV    rcx,rax
  MOV    rax,[rax]
  CALL   qword [rax]        ; indirect call
  ADD    rsp,32
  FSTP   dword [v_f2]       ; move result to f2
now the retFloat proc

Code: Select all

; Procedure.f retFloat (*this, valFloat.f)
macro MP0{
_Procedure0:
  MOV    qword [rsp+8],rcx
  MOVSS  dword [rsp+16],xmm1          ; move xmm1 to stack
  PS0=48
  SUB    rsp,40                                                                                                                                                                                                                                                                                         
; ProcedureReturn valFloat + 0.458
  FLD    dword [rsp+PS0+8]            ; load from stack
  FADD   dword [F1]                   
  MOVSXD rax,eax                      ; why ?
  JMP   _EndProcedure1
; EndProcedure
  FLDZ                                ; why ?
_EndProcedure1:
  ADD    rsp,40
  RET
}
both direct call and indirect have useless operations like the sse ones but the direct call got the most. actually using interface is faster than a direct call oO
is this a intentional behavior ?