Inexplicable stack distribution in native C code built for x64 in Visual Studio 2010

I am trying to use CL 16.0 for x64 (VS 2010) to create some readable 64-bit ASM code for an example, but CL insists on predetermining a ton of stack space (28 bytes) with the following line:

sub rsp, 40 ; 00000028H (actual value depends on number of local vars of course) 

The question is, how can I disable this behavior? It's hard to explain to the class, and I like to show them clean, explainable code ... My assumption is that "sub rsp, XXX" should allocate the exact space required by local variables in the function.

Of course, this does not require additional space. On x86, this behavior is apparently controlled by the edit-and-continue switches (/ Zi vs. / ZI), but they have no effect in the case of x64. Any idea how to make x64 CL allocate as many stack as required?

Thanks in advance!

+4
source share
1 answer

It would be easier if you showed the source code that produced this, but 40 bytes is not so much for a 64-bit machine. These are just five long or pointers. Another thing to consider is alignment for local variables β€” the compiler probably imposes them on optimal access.

Edit:

Hmm, this is really a little puzzling. Doing this through GCC does not result in such a stack grab. My suspicion is that this is a requirement for the procedure for handling exceptional requirements for the compiler (do you see any strange labels before and after allocating a stack in the generated code?).

Here are some MSDN links you might find useful:

I don't have a windows window for testing, but try /favor:??? and see if this level of optimization eliminates a bit. This is the function of the sheet after all.

Also, formatting comments on SO sucks, put the code in the question itself.

+1
source

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


All Articles