* * *

Author Topic: Linux 64bit calling convention  (Read 609 times)

mikerabat

  • New member
  • *
  • Posts: 17
Linux 64bit calling convention
« on: July 04, 2017, 10:30:56 am »
I wrote quite a lot of assembler optimized functions for windows and now wanted to port it to Linux.
The biggest problem I now encounter is that I learned that Linux uses a different calling
convention for 64bit than windows does. Actually it uses different registers for
function calls and it would be a great deal of work to rename these registers or even clone and
rewrite the calls.

Is there a way I can force the freepascal compiler to use the windows calling convention for 64bit?
(I know for 32 bit I can force different conventions...)

kind regards
   Mike


Thaddy

  • Hero Member
  • *****
  • Posts: 4224
Re: Linux 64bit calling convention
« Reply #1 on: July 04, 2017, 11:54:33 am »
You do not need to. Win64 has only one (1) calling convention (four register _fastcall) and that is selected by default for that platform.
If you want you can select stdcall or winapi on linux too, I seem to remember stdcall on 64 bit is aliased to 4 register __fastcall, but imho makes not much sense, unless, indeed, it is for some of your own routines. So try stdcall on Linux. (which shouldn't be really stdcall,but the aliased __fastcall64). I am not absolutely sure, though.
And may be FastCall simply works.
« Last Edit: July 04, 2017, 11:56:22 am by Thaddy »
"Logically, no number of positive outcomes at the level of experimental testing can confirm a scientific theory, but a single counterexample is logically decisive."

mikerabat

  • New member
  • *
  • Posts: 17
Re: Linux 64bit calling convention
« Reply #2 on: July 04, 2017, 04:00:56 pm »
Yeah I know that - and that's the problem. My routines (40+) are implemented such that they depend on the
registers windows uses. The thing is that according to wiki there is also only one calling convention on Linux/Unix
which is similar to win64 calling conventions but uses different param registers. The thing is I rely on these....

Afaik on the x86 side (32bit) there are many calling conventions but they are completely ignored for 64bit (at least on the
windows compiler side is the case)... so ... is there something similar I can do on the 64bit side?

Remy Lebeau

  • Sr. Member
  • ****
  • Posts: 301
    • Lebeau Software
Re: Linux 64bit calling convention
« Reply #3 on: July 05, 2017, 11:20:16 pm »
The thing is that according to wiki there is also only one calling convention on Linux/Unix
which is similar to win64 calling conventions but uses different param registers. The thing is I rely on these....

Well, then you have to change that.  IFDEF the code to use one set of registers on Win64, and another set of registers on Linux64.  Consolidate them into a single set of common registers, or stack space, or whatever, for the rest of the code to use without caring about the underlying platform.
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) open source project - Admin, Developer

mikerabat

  • New member
  • *
  • Posts: 17
Re: Linux 64bit calling convention
« Reply #4 on: July 19, 2017, 09:20:46 am »
Alright... I managed to add a little asm prologue (interchanging registers) to all 64 bit routines so they now work properly.

avra

  • Hero Member
  • *****
  • Posts: 1063
    • Additional info
Re: Linux 64bit calling convention
« Reply #5 on: July 19, 2017, 01:18:39 pm »
Alright... I managed to add a little asm prologue (interchanging registers) to all 64 bit routines so they now work properly.
You might also want to take a look at macros. They could potentially add less overhead.
https://www.freepascal.org/docs-html/prog/progse5.html#x137-1380002.2
ct2laz - Easily convert components and projects between Lazarus and CodeTyphon

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus