Author Topic: A trick to remove some x64 ABI limitations  (Read 701 times)

HSE

  • Member
  • *****
  • Posts: 2151
  • AMD 7-32 / i3 10-64
A trick to remove some x64 ABI limitations
« on: January 20, 2022, 04:18:02 AM »
Hi all!

Translating from 32 bits to 64 bits a big problem is that ecx and/or edx can be arguments in other order than x64 ABI allow. And that complicate everything.

Fortunatelly this little macro remove all the problem:
Code: [Select]
@reg32_64 macro reg_32, reg_64
    if @WordSize eq 4
      @CatStr(<_>,&reg_32) textequ <reg_32>
      @CatStr(<__>,&reg_32) textequ <reg_32>
    else
      @CatStr(<_>,&reg_32) textequ <reg_64>
      @CatStr(<__>,&reg_32) textequ <@CatStr(&reg_64,<d>)>
    endif
endm

And in your code:
Code: [Select]
@reg32_64 ecx, r10
@reg32_64 edx, r11
 
This generate wildcard registers _ecx and _edx that became DWORD ecx and edx in 32 bits, and QWORD r10 an r11 in 64 bits.

Because some x64 graphical API still requiere DWORD, also are generated wildcard registers __ecx and __edx that always became DWORD, ecx and edx in 32 bits, and r10d an r11d in 64 bits.

Then you don't need to create solutions for dual code:
Code: [Select]
Method ComboBox.Init, uses xbx xsi, pOwner:POINTER, hParent:HWND, pDefStruc:PDEF_COMBOBOX
  SetObject xsi
  mov xbx, pDefStruc
  assume xbx:PDEF_COMBOBOX
  mov eax, [xbx].dStyle
  or eax, WS_CHILD or WS_VISIBLE
  invoke CreateWindowEx, [xbx].dExStyle, $OfsCStr("ComboBox"), [xbx].pTitle, eax, \
                         [xbx].sdPosX, [xbx].sdPosY, [xbx].dWidth, [xbx].dHeight, \
                         hParent, [xbx].xCtlID, hInstance, 0
  assume xbx:NOTHING
  ACall xsi.Init, pOwner, xax
  Subclass ComboBox                             ;Uses xsi
MethodEnd


Instead you just use almost the pure 32 bits code:
Code: [Select]
Method ComboBox.Init, uses xsi, pOwner:POINTER, hParent:HWND, pDefStruc:PDEF_COMBOBOX
  SetObject xsi
  mov _ecx, pDefStruc
  assume _ecx:PDEF_COMBOBOX
  mov eax, [_ecx].dStyle
  or eax, WS_CHILD or WS_VISIBLE
  invoke CreateWindowEx, [_ecx].dExStyle, $OfsCStr("ComboBox"), [_ecx].pTitle, eax, \
                         [_ecx].sdPosX, [_ecx].sdPosY, [_ecx].dWidth, [_ecx].dHeight, \
                         hParent, [_ecx].xCtlID, hInstance, 0
  assume _ecx:NOTHING
  ACall xsi.Init, pOwner, xax
  Subclass ComboBox                             ;Uses xsi
MethodEnd

Indeed, in 32 bits, is exactly the original code.

Naturally this is not limited to OOP, this work in plain assembly.
 
Regards, HSE
« Last Edit: January 20, 2022, 11:55:15 PM by HSE »
Equations in Assembly: SmplMath

Biterider

  • Moderator
  • Member
  • *****
  • Posts: 941
  • ObjAsm Developer
    • ObjAsm
Re: A trick to remove some x64 ABI limitations
« Reply #1 on: January 20, 2022, 08:29:02 AM »
Hi HSE
It is a very good idea  :thumbsup:
Perhaps we can elaborate on this concept a bit further by creating aliases for the GP registers in a more general way.

I've also taken some steps in this direction by creating some macros to switch between registers and local or arguments (LocReg, ArgReg, etc.).
Turns out I don't use them that often. Only in situations where I needed heavy optimization.

Biterider

nidud

  • Member
  • *****
  • Posts: 2388
    • https://github.com/nidud/asmc
Re: A trick to remove some x64 ABI limitations
« Reply #2 on: January 20, 2022, 09:24:52 AM »
deleted
« Last Edit: February 26, 2022, 05:34:05 AM by nidud »

HSE

  • Member
  • *****
  • Posts: 2151
  • AMD 7-32 / i3 10-64
Re: A trick to remove some x64 ABI limitations
« Reply #3 on: January 20, 2022, 12:23:19 PM »
Perhaps we can elaborate on this concept a bit further by creating aliases for the GP registers in a more general way.
Of course you see a bigger picture than I do, and collisions must be avoided. But I hit first, ugly wildcard names are yours  :biggrin: :biggrin: :biggrin:

Only in situations where I needed heavy optimization.
This is not exactly same priority. With pseudo push/pop and this little macro, 32 bits code become 64 bits code. Perhaps You don't even need to know 64 bits programming so much. Optimization is a further step, that involve SIMD, AVX, amount of timings and is a lot more complex. I need precision, not efficiency. Under this terms optimization usually is not so easy  :thumbsup:

A simple method for this is to use 64-bit registers.
Thanks for effort nidud  :thumbsup:. But you are missing the most important part, that is not my 32 bit code.

:biggrin: Example is a little elemental but is my real 32 bits code that now is working in 64 bits. 8000 SLOC in minutes (a lot of minutes obviously :biggrin: :biggrin: :biggrin:). [not in the count thousands of SLOC maked dual by Biterider]

A note aside, this is the code that work with AsmC but don't work with AsmC64  :undecided:
Equations in Assembly: SmplMath