Search code examples
c++assemblyinlininginline-functions

Why does C++ inline function has call instructions?


I read that with inline functions where ever the function call is made we replace the function call with the body of the function definition.

According to the above explanation there should not be any function call when inline is user.

If that is the case Why do I see three call instructions in the assembly code ?

#include <iostream>                                                                  
                                                                                     
inline int add(int x, int y)                                                         
{                                                                                    
        return x+ y;                                                                 
}                                                                                    
                                                                                     
int main()                                                                           
{                                                                                    
        add(8,9);                                                                    
        add(20,10);                                                                  
        add(100,233);                                                                
}

meow@vikkyhacks ~/Arena/c/temp $ g++ -c a.cpp
meow@vikkyhacks ~/Arena/c/temp $ objdump -M intel -d a.o
0000000000000000 <main>:
   0:   55                      push   rbp
   1:   48 89 e5                mov    rbp,rsp
   4:   be 09 00 00 00          mov    esi,0x9
   9:   bf 08 00 00 00          mov    edi,0x8
   e:   e8 00 00 00 00          call   13 <main+0x13>
  13:   be 0a 00 00 00          mov    esi,0xa
  18:   bf 14 00 00 00          mov    edi,0x14
  1d:   e8 00 00 00 00          call   22 <main+0x22>
  22:   be e9 00 00 00          mov    esi,0xe9
  27:   bf 64 00 00 00          mov    edi,0x64
  2c:   e8 00 00 00 00          call   31 <main+0x31>
  31:   b8 00 00 00 00          mov    eax,0x0
  36:   5d                      pop    rbp
  37:   c3                      ret  

NOTE

Complete dump of the object file is here


Solution

    • You did not optimize so the calls are not inlined
    • You produced an object file (not a .exe) so the calls are not resolved. What you see is a dummy call whose address will be filled by the linker
    • If you compile a full executable you will see the correct addresses for the jumps

    See page 28 of: http://www.cs.princeton.edu/courses/archive/spr04/cos217/lectures/Assembler.pdf