GCC keyword Inline Explore __GCC

Source: Internet
Author: User
First, inline introduction
Look at the shape first:
inline int Test () {... return 0}
int main () {test (); return 0; }
We know that if the test function does not have the inline keyword modifier, the program executes to the call to test, and jumps from the main function to the test function execution. In order to return from the test function to the Mian function, it can be executed from the next instruction that invokes the test function, before the test function is invoked, we must protect the field (the value of some registers will be pressed stack).
So if you add the inline keyword, then the compiler will treat the test function as a macro, that is, directly in the main function. It is important to note here that not all functions plus the inline keyword will certainly be expanded in the main function, which generally requires the inline keyword to be as simple as possible.
Second, the reverse assembly look at the effect
Source:
#include <stdio.h>
static inline int test1 () {int a = 1;
printf ("Hello test1.\n");
return A; }
static inline int test2 () {int b = 2;
printf ("Hello test2.\n");
return b; }
int main () {test1 (); Test2 ();
return 0; }
First compilation: ARM-NONE-LINUX-GNUEABI-GCC Inline.c-o Inline
Disassembly is as follows: arm-none-linux-gnueabi-objdump-d inline > Log
View Log file Cat log:
Partial assembler Code 000083d4 <main>: 83d4:e1a0c00d mov ip, SP 83d8:e92dd800 Push {fp, IP, LR, PC} 83dc:e24cb004 Sub fp, IP, #4; 0x4 83e0:eb000005 bl 83fc <test1> 83e4:eb000012 bl 8434 <test2> 83e8:e3a03000 mov r3, #0; 0x0 83ec:e1a00003 mov r0, R3 83f0:e24bd00c Sub sp, FP, #12 0xc 83f4:e89d6800 LDM sp, {fp, SP, LR} 83F8:E12FFF1E bx LR
000083FC <test1>: 83fc:e1a0c00d mov ip, SP 8400:e92dd800 Push {fp, IP, LR, PC} 8404:e24cb004 Sub fp, IP, #4; 0x4 8408:e24dd008 Sub sp, SP, #8; 0x8 840c:e3a03001 mov r3, #1; 0x1 8410:e50b3010 str R3, [FP, #-16] 8414:e59f0014 ldr r0, [pc, #20]; 8430 <test1+0x34> 8418:ebffffb9 bl 8304 <_init+0x50> 841c:e51b3010 LDR R3, [FP, #-16] 8420:e1a00003 mov r0, R3 8424:e24bd00c Sub sp, FP, #12 0xc 8428:e89d6800 LDM sp, {fp, SP, LR} 842C:E12FFF1E bx LR 8430:00008504. Word 0x00008504
00008434 <test2>: 8434:e1a0c00d mov ip, SP 8438:e92dd800 Push {fp, IP, LR, PC} 843c:e24cb004 Sub fp, IP, #4; 0x4 8440:e24dd008 Sub sp, SP, #8; 0x8 8444:e3a03002 mov r3, #2; 0x2 8448:e50b3010 str R3, [FP, #-16] 844c:e59f0014 ldr r0, [pc, #20]; 8468 <test2+0x34> 8450:ebffffab bl 8304 <_init+0x50> 8454:e51b3010 LDR R3, [FP, #-16] 8458:e1a00003 mov r0, R3 845c:e24bd00c Sub sp, FP, #12 0xc 8460:e89d6800 LDM sp, {fp, SP, LR} 8464:E12FFF1E bx LR 8468:00008514. Word 0x00008514
Second compilation
Arm-none-linux-gnueabi-gcc-o Inline.c-o Inline
Disassembly is as follows: arm-none-linux-gnueabi-objdump-d inline > Log
View Log file Cat log:
000083d4 <main>: 83d4:e52de004 push {LR}; (str lr, [SP, #-4]!) 83d8:e24dd004 Sub sp, SP, #4; 0x4 83dc:e59f0018 ldr r0, [pc, #24]; 83FC <main+0x28> 83e0:ebffffc7 bl 8304 <_init+0x50> 83e4:e59f0014 ldr r0, [pc, #20]; 8400 <main+0x2c> 83e8:ebffffc5 bl 8304 <_init+0x50> 83ec:e3a00000 mov r0, #0; 0x0 83f0:e28dd004 Add SP, SP, #4; 0x4 83f4:e49de004 pop {LR}; (Ldr LR, [sp], #4) 83F8:E12FFF1E bx LR 83fc:0000849c. Word 0x0000849c 8400:000084ac. Word 0x000084ac
Summarized as follows: For the Inine keyword, if the compiler does not optimize the option (-O or-o2), the compilation system does not expand the function that it modifies in the Mian function. However, the function code modified by inline is placed behind main in the order in which it is called in the main function. If the optimization option is added at compile time, the function modified by inline will most likely be expanded in the main function.
Note: Inline is a suggested keyword, specifically whether the disassembly of the disassembly is known.


Original address: http://blog.chinaunix.net/uid-26833883-id-3351281.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.