Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GCC fastcall function definition

Tags:

c

gcc

Okay, so I can call function as fastcall CC, by declaring it with __attribute__((fastcall)). How do I define the function itself as fastcall?

Like, I have caller code:

// caller.c

unsigned long func(unsigned long i) __attribute__((fastcall));

void caller() {
    register unsigned long i = 0;
    while ( i != 0xFFFFFFD0 ) {
        i = func(i);
    }
}

And the function:

// func.c

unsigned long func(unsigned long i) {
    return i++;
}

In this code, func() is being compiled as cdecl, it extracts i from stack, not from ecx(this is i386).

If I write unsigned long func(unsigned long i) __attribute__((fastcall)); in func.c it just won't compile, saying

error: expected ‘,’ or ‘;’ before ‘{’ token

If I declare it in func.c the same way I did in caller.c, it will complain the other way:

error: previous declaration of ‘func’ was here
like image 832
einclude Avatar asked Oct 13 '11 21:10

einclude


1 Answers

Attributes must be applied in the declaration, not in the definition.

Try:

__attribute__((fastcall)) unsigned long func(unsigned long i) ;
__attribute__((fastcall)) unsigned long func(unsigned long i) {
    return i++;
}

The standard way to do this is to put the declaration in a header and have both source files include the header

like image 149
Foo Bah Avatar answered Oct 18 '22 08:10

Foo Bah