May 11, 2018

[C++] inline function address

Do inline functions have addresses?

Although it's well known, just, since C++17
has inline variable, the implement should be the same.

https://godbolt.org/g/6ZbmYa

#include <stdio.h>

inline void f() {
    printf("hello\n");
}

int main() {
    f();
    void (*g)() = f;
    g();
}
Assembly:
.LC0:
  .string "hello"
f():
  push rbp
  mov rbp, rsp
  mov edi, OFFSET FLAT:.LC0
  call puts
  nop
  pop rbp
  ret
main:
  push rbp
  mov rbp, rsp
  sub rsp, 16
  call f()
  mov QWORD PTR [rbp-8], OFFSET FLAT:f()
  mov rax, QWORD PTR [rbp-8]
  call rax
  mov eax, 0
  leave
  ret
Assembly: -O3
.LC0:
  .string "hello"
main:
  sub rsp, 8
  mov edi, OFFSET FLAT:.LC0
  call puts
  mov edi, OFFSET FLAT:.LC0
  call puts
  xor eax, eax
  add rsp, 8
  ret

https://godbolt.org/g/k8m11U

#include <stdio.h>

inline int a{42};

inline void f() {
    printf("%i\n",a);
}

int main() {

    auto pa = &a;
    printf("%i", *pa);
}
Assembly:
a:
  .long 42
.LC0:
  .string "%i"
main:
  push rbp
  mov rbp, rsp
  sub rsp, 16
  mov QWORD PTR [rbp-8], OFFSET FLAT:a
  mov rax, QWORD PTR [rbp-8]
  mov eax, DWORD PTR [rax]
  mov esi, eax
  mov edi, OFFSET FLAT:.LC0
  mov eax, 0
  call printf
  mov eax, 0
  leave
  ret
Assembly: -O3
.LC0:
  .string "%i"
main:
  sub rsp, 8
  mov esi, DWORD PTR a[rip]
  mov edi, OFFSET FLAT:.LC0
  xor eax, eax
  call printf
  xor eax, eax
  add rsp, 8
  ret
a:
  .long 42

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.