Creating an inline build function in x64 Visual Studio

I know that the MSVC compiler in x64 mode does not support inline code snippets, and to use the assembly code you need to define your function in some external my_asm_funcs.asm file as follows:

my_asm_func PROC mov rax, rcx ret my_asm_func ENDP 

And then in your .c or .h file, you define a header for such a function:

 int my_asm_func(int x); 

Although this solution answers many problems, Iโ€™m still interested in having the assembly function of the assemblies built-in, in other words - after compilation I donโ€™t want any โ€œcallsโ€ my_asm_func I just want this piece of assembly to be glued to my final compiled code. I tried declaring a function with inline and __forceinline keywords, but nothing seems to help. Is there a way to do what I want?

+5
source share
1 answer

No, there is no way to do what you want.

The Microsoft compiler does not support native builds for x86-64 targets, as you said. This forces you to define your build functions in an external code module (* .asm), assemble them using MASM, and link the result with your compiled C / C ++ code.

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

Even if you enable the time code (LTCG), your build module will not be inserted because the linker simply does not support this.

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

It is not possible for the inline or __forceinline to do anything. In fact, you cannot use them without a compiler error (or at least a warning). These annotations should include a function definition (which for an inline function matches its declaration), but you cannot put it in a function definition, as it is defined in a separate .asm file. These are not MASM keywords, so trying to add them to the definition will necessarily result in an error. And including them in a direct declaration of the assembly function in the C header will be similarly unsuccessful, since there is no code for the built-in - just a prototype.

This is why Microsoft recommends using internal features. You can use them directly in your C or C ++ code, and the compiler automatically issues the appropriate assembler code. This not only makes the inlay desired, but also internal functions allow the optimizer to function, improving results. No, intrinsics do not lead to perfect code, and there are no built-in functions for all this, but this is best suited for the Microsoft compiler.

Your only alternative is to sit down and play with various permutations of the C / C ++ code until you get a compiler to create the desired object code. This can be very powerful in cases where internal functions are not available for the instructions that you want to generate, but it takes a lot of time spent fidgeting, and you have to go back to it to make sure that it continues to do what you when You are updating the compiler version.

+10
source

Source: https://habr.com/ru/post/1261453/


All Articles