开发者

GCC fastcall function definition

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


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


The problem is the semicolon you put after the attribute. You need

unsigned long func(unsigned long i) __attribute__((fastcall)) // no semicolon here
{
    ... function ...
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜