简体   繁体   中英

How can gcc generate assembly which can set up stack frame with ENTER instruction

I generate assembly code like this with gcc, Can gcc generate code which has ENTER for stack frame?

.file   "temp.c"
.text
.globl  main
.type   main, @function
main:
pushq   %rbp
movq    %rsp, %rbp
movl    $0, -12(%rbp)
movl    $0, -8(%rbp)
movl    $0, -4(%rbp)
movl    $0, %eax
popq    %rbp
ret
.size   main, .-main
.ident  "GCC: (Ubuntu 7.4.0-1ubuntu1~18.04) 7.4.0"
.section    .note.GNU-stack,"",@progbits

Here 's original code:

 #include <stdio.h>
  int main(){
   int a;
   int b;
   int c;
   a = 0;
   b = 0;
   c = 0;
}

GCC will never emit enter because it's super-slow compared to its normal frame-pointer setup of 2 or 3 single-uop instructions.

(If it makes a frame pointer at all; gcc -O1 and higher enables -fomit-frame-pointer . Except when optimizing for size, because x(%rsp) addressing modes use an extra byte vs. x(%rbp) modes.)

# equivalent to  enter $24, $0  (4 bytes)
    push   %rbp               # 1 byte
    mov    %rsp, %rbp         # 3 bytes
    sub    $24, %rsp          # 4 bytes only for a non-zero immediate

Specifically, on Skylake enter is 12 uops, and one per 8-cycle throughput for enter a, 0 ( Agner Fog's instruction tables ). With a non-zero nesting level, it's insanely slow, like 87 cycles + 7 * nesting level .

On Ryzen, enter is 12 uops, with one per 16-cycle throughput.

leave is fine, though: it's only 3 uops on Intel CPUs. (That's still one more than mov %rbp, %rsp / pop %rbp though. The 3 uops doesn't include a stack-sync uop; it's 3 even if the stack engine was in sync before leave .)


The only reason to use enter would be optimizing for code-size at the expense of speed. But even gcc -Os doesn't care enough about code-size to have an option for that.

Even clang -Oz (which will use push $1 / pop %rax to save 2 bytes vs. mov $1, %eax ) doesn't use enter . ( Godbolt compiler explorer )

But enter 0,0 doesn't even save code-size so it's just pure bad.

And I follow a manual that said program start entry with ENTER

That's one (obsolete and not recommended) option.

If you want to write your own compiler that makes slow code, go ahead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM