VCall (Variadic Call) module

Share your advanced PureBasic knowledge/code with the community.
Justin
Addict
Addict
Posts: 948
Joined: Sat Apr 26, 2003 2:49 pm

Re: VCall (Variadic Call) module

Post by Justin »

Hi guys thanks for the help,

mk-soft, your solution does not work, i get an IMA

wilbert, the function is an object method defined as:

Code: Select all

public HRESULT put_Bounds(RECT bounds)
using mk-soft method i define the interface like

Code: Select all

put_Bounds(left_top.q, right_bottom.q)

and
obj\put_Bounds(RectByVal(rc))
but i get an IMA, using .i or .l or splitting the rect in four longs does not work either
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: VCall (Variadic Call) module

Post by wilbert »

Justin wrote:the function is an object method defined as:

Code: Select all

public HRESULT put_Bounds(RECT bounds)
Are you using the 32 or 64 bit version of PureBasic ?

If you are using x64, it might help to change the macro from mk-soft to

Code: Select all

CompilerIf #PB_Compiler_Processor = #PB_Processor_x64
  Macro RectByVal(_rect_)
    _rect_\top << 32 | (_rect_\left & $ffffffff) ,_rect_\bottom << 32 | (_rect_\right & $ffffffff)
  EndMacro
CompilerElse
  Macro RectByVal(_rect_)
    _rect_\left, _rect_\top, _rect_\right, _rect_\bottom
  EndMacro
CompilerEndIf
The original macro doesn't support negative values for left and right on x64.
Windows (x64)
Raspberry Pi OS (Arm64)
User avatar
mk-soft
Always Here
Always Here
Posts: 6202
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: VCall (Variadic Call) module

Post by mk-soft »

Change your Interface declaration

Code: Select all

Interface iXYZ
  ; ?
  ;put_Bounds(RECT bounds)
  CompilerIf #PB_Compiler_Processor = #PB_Processor_x64
    put_Bounds(left_top, right_bottom) ; X64
  CompilerElseIf
    put_Bounds(left, top, right, bottom) ; X86
  CompilerEndIf
  ; ?
EndInterface

CompilerIf #PB_Compiler_Processor = #PB_Processor_x64
  Macro RectByVal(_rect_)
    _rect_\top << 32 | _rect_\left ,_rect_\bottom << 32 | _rect_\right
  EndMacro
CompilerElse
  Macro RectByVal(_rect_)
    _rect_\left, _rect_\top, _rect_\right, _rect_\bottom
  EndMacro
CompilerEndIf
@wilbert

Left-shift (<<) is no problem. Only right-shift (>>) filled signed bit :wink:
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: VCall (Variadic Call) module

Post by wilbert »

mk-soft wrote:Left-shift (<<) is no problem. Only right-shift (>>) filled signed bit :wink:
In this case it is a problem.
If left or right are negative values like for example -50 and -10 and they are casted to 64 bit, the upper 32 bits of those 64 bit values are all 1.
Since the upper 32 bits are already all 1 In such a case, the OR operation will change nothing to that. No matter what value top or bottom is, they will always be -1 when you extract them if the value you combine them with is negative.
Windows (x64)
Raspberry Pi OS (Arm64)
Justin
Addict
Addict
Posts: 948
Joined: Sat Apr 26, 2003 2:49 pm

Re: VCall (Variadic Call) module

Post by Justin »

Hi wilbert,

i still get an IMA, maybe because it's an object method and not a standard dll api function.

anyways the weird thing is that passing it by reference seems to work.

i plan to post the project when it's finished, you'll be able to check it by yourself.
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: VCall (Variadic Call) module

Post by wilbert »

Justin wrote:anyways the weird thing is that passing it by reference seems to work.
I'm less familiar with the Windows calling convention compared to macOS.

I see now that the Windows x64 calling convention does mention "Any argument that doesn’t fit in 8 bytes, or isn't 1, 2, 4, or 8 bytes, must be passed by reference." .
https://docs.microsoft.com/en-us/cpp/bu ... ew=vs-2019
So maybe you are right and a C compiler will change it to a reference.
Windows (x64)
Raspberry Pi OS (Arm64)
Justin
Addict
Addict
Posts: 948
Joined: Sat Apr 26, 2003 2:49 pm

Re: VCall (Variadic Call) module

Post by Justin »

It finally works.
To sum up, as you said in 64 bit works by reference but crashes with the macros.
In 32 bit crashes by reference but works using the macro.

Big thanks to both.
infratec
Always Here
Always Here
Posts: 7576
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: VCall (Variadic Call) module

Post by infratec »

Can someone convert the asm backend assembler additionally to C backend assembler?

I will starting with it, but since I'm not familar with the gcc C asm instructions, it will takes time.

Or can we simulate the VCalls directly in C backend?
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: VCall (Variadic Call) module

Post by wilbert »

infratec wrote: Sun Aug 20, 2023 7:31 pm Or can we simulate the VCalls directly in C backend?
With the C backend it is faster and easier to call a C function like sprintf directly.

Code: Select all

*result = AllocateMemory(1024)
*format = UTF8("Half the value of PI is = %.3f")
half_pi.d = #PI / 2

!sprintf (p_result, p_format, v_half_pi);

Debug PeekS(*result, -1, #PB_UTF8)
Windows (x64)
Raspberry Pi OS (Arm64)
Post Reply