olliej
olliej

Reputation: 36763

Is it possible to convince GCC to mimic the fastcall calling convention?

So i have a piece of assembly that needs to call a function with the fastcall calling convention on windows, but gcc doesn't (afaict) support it. GCC does provide the regparm attribute but that expects the first 3 parameters to be passed in eax, edx and ecx, whereas fastcall expects the first two parameters to be passed in ecx and edx.

I'm merely trying to avoid effectively duplicating a few code paths, so this isn't exactly critical, but it would be great if it were avoidable.

Upvotes: 3

Views: 6951

Answers (3)

ephemient
ephemient

Reputation: 204678

GCC does support fastcall, via __attribute__((fastcall)). It appears to have been introduced in GCC 3.4.

Upvotes: 12

user865927
user865927

Reputation:

I know I'm kind of late to the party on this one but if any of you stumble across this, just remember that you can define a macro to mimic it. For example:

#if defined(__GNUC__)
    #define MSFASTCALL __fastcall
    #define GCCFASTCALL 
#elif defined(_MSC_VER)
    #define MSFASTCALL
    #define GCCFASTCALL __attribute__((fastcall))
#endif

int MSFASTCALL magic() GCCFASTCALL;

Obviously, this looks kind of ugly, so you could just define 2 prototypes (which is what I do) to make it a little easier to read but there are people who prefer the route that requires less typing. I generally don't use calling conventions except for special cases. I just let the compiler optimize the rest away.

Now, there are some quirks to remember. For example, when you're targeting a 64 bit platforms, all functions utilize the fastcall convention in order to take advantage of the extra registers and improve speed and reduce indirection cost. Likewise, fastcall is implemented differently by different platforms as it is not standardized. There are some others but that's all I can pull off the top of my head.

Upvotes: 5

Mike F
Mike F

Reputation:

If you're calling the function from asm then surely you have complete control over how you call the function. What's stopping you from just loading up the registers and issuing a CALL?

Upvotes: 1

Related Questions