使汇编函数在x64 Visual Studio中内联 [英] Making assembly function inline in x64 Visual Studio

查看:715
本文介绍了使汇编函数在x64 Visual Studio中内联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道x64模式下的MSVC编译器不支持内联汇编代码段,并且要使用汇编代码,您必须在某些外部my_asm_funcs.asm文件中定义函数,如下所示:

I know that MSVC compiler in x64 mode does not support inline assembly snippets of code, and in order to use assembly code you have to define your function in some external my_asm_funcs.asm file like that:

my_asm_func PROC
    mov rax, rcx
    ret
my_asm_func ENDP

然后在.c或.h文件中,为该函数定义一个标题,如下所示:

And then in your .c or .h file you define a header for the function like that:

int my_asm_func(int x);

尽管该解决方案解决了许多问题,但是我仍然希望使该汇编代码函数内联,换句话说-编译后,我不想对 my_asm_func 进行任何调用",我只希望将这段程序集粘贴到我的最终编译代码中.我尝试使用 inline __ forceinline 关键字声明该函数,但似乎无济于事.还有什么方法可以做我想要的吗?

Although that solution answers many concerns, but I am still interested in making that assembly code function to be inline, in other words - after compilation I don't want any "calls" to my_asm_func, I just want this piece of assembly to be glued into my final compiled code. I tried declaring the function with inline and __forceinline keywords, but nothing seems to be helping. Is there still any way to do what I want?

推荐答案

不,您无法做自己想做的事情.

正如您所说,Microsoft的编译器不支持x86-64目标的内联汇编.这迫使您在外部代码模块(* .asm)中定义汇编函数,将其与MASM进行汇编,并将结果与​​单独编译的C/C ++代码链接在一起.

Microsoft's compiler doesn't support inline assembly for x86-64 targets, as you said. This forces you to define your assembly functions in an external code module (*.asm), assemble them with MASM, and link the result together with your separately-compiled C/C++ code.

所需的步骤分隔意味着C/C ++编译器无法内联汇编函数,因为在编译时它们对汇编函数不可见.

The required separation of steps means that the C/C++ compiler cannot inline your assembly functions because they are not visible to it at the time of compilation.

即使启用了链接时代码生成(LTCG),您的汇编模块也不会内联,因为链接器根本不支持此功能.

Even with link-time code generation (LTCG) enabled, your assembly module(s) will not get inlined because the linker simply doesn't support this.

绝对没有办法在直接插入C或C ++代码的单独模块中编写汇编函数.

There is absolutely no way to get assembly functions written in a separate module inlined directly into C or C++ code.

inline__forceinline关键字无法执行任何操作.实际上,没有编译器错误(或至少是警告),您将无法使用它们.这些注释必须放在函数的 definition 上(对于内联函数,其定义与其声明相同),但是您不能将其放在函数的定义上,因为它是在单独的地方定义的* .asm文件.这些不是MASM关键字,因此尝试将它们添加到定义中必然会导致错误.将它们放在C头文件中的汇编函数的前向声明中同样会失败,因为那里没有内联代码,只是一个原型.

There is no way that the inline or __forceinline keywords could do anything. In fact, there's no way that you could use them without a compiler error (or at least a warning). These annotations have to go on the function's definition (which, for an inline function, is the same as its declaration), but you can't put it on the function's definition, since that's defined in a separate *.asm file. These aren't MASM keywords, so trying to add them to the definition would necessarily result in an error. And putting them on the forward declaration of the assembly function in the C header is going to be similarly unsuccessful, since there's no code there to inline—just a prototype.

这就是Microsoft建议使用 intrinsics 的原因..您可以直接在C或C ++代码中使用它们,编译器将自动发出相应的汇编代码.这不仅可以实现所需的内联,而且内在函数甚至可以使优化器发挥作用,从而进一步改善结果.不,内在函数不能带来完美的代码,并不是所有事物都可以使用内在函数,但这是使用Microsoft编译器可以做到的最好的选择.

This is why Microsoft recommends using intrinsics. You can use these directly in your C or C++ code, and the compiler will emit the corresponding assembly code automatically. Not only does this accomplish the desired inlining, but intrinsics even allow the optimizer to function, further improving the results. No, intrinsics do not lead to perfect code, and there aren't intrinsics for everything, but it's the best you can do with Microsoft's compiler.

您唯一的其他选择是坐下来玩C/C ++代码的各种排列,直到让编译器生成所需的目标代码为止.如果内在函数无法用于您希望生成的指令,但这种功能可能非常强大,但是确实花费了很多时间坐立不安,您必须重新访问它以确保它可以继续执行您的操作升级编译器版本时需要.

Your only other alternative is to sit down and play with various permutations of C/C++ code until you get the compiler to generate the desired object code. This can be very powerful in cases where intrinsics are not available for the instructions that you wish to be generated, but it does take a lot of time spent fidgeting, and you'll have to revisit it to make sure it continues to do what you want when you upgrade compiler versions.

这篇关于使汇编函数在x64 Visual Studio中内联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆