GCC快速调用函数定义

发布于 2024-12-09 18:18:37 字数 904 浏览 0 评论 0原文

好的,我可以通过使用 __attribute__((fastcall)) 声明它,将函数作为 fastcall CC 来调用。 如何将函数本身定义为快速调用?

就像,我有调用者代码:

// caller.c

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

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

和函数:

// func.c

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

在此代码中,func() 被编译为 cdecl,它从堆栈中提取 i ,不是来自 ecx(这是 i386)。

如果我在 func.c 中编写 unsigned long func(unsigned long i) __attribute__((fastcall)); 它就不会编译,说

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

如果我像我一样在 func.c 中声明它在 caller.c 中,它会以另一种方式抱怨:

error: previous declaration of ‘func’ was here

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

葮薆情 2024-12-16 18:18:37

属性必须在声明中应用,而不是在定义中应用。

尝试:

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

执行此操作的标准方法是将声明放在标头中,并使两个源文件都包含该标头

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

妖妓 2024-12-16 18:18:37

问题是您在属性后面放置的分号。你需要

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

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

unsigned long func(unsigned long i) __attribute__((fastcall)) // no semicolon here
{
    ... function ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文