1.没有内联的情况
#include <stdio.h>
void f() {
printf("hello\n");
}
int main() {
f();
// void (*g)() = f;
// g();
}
.file "inline.c"
.section .rodata
.LC0:
.string "hello"
.text
.globl f
.type f, @function
f:
.LFB0:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
movq %rsp, %rbp
.cfi_offset 6, -16
.cfi_def_cfa_register 6
movl $.LC0, %edi
call puts
leave
ret
.cfi_endproc
.LFE0:
.size f, .-f
.globl main
.type main, @function
main:
.LFB1:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
movq %rsp, %rbp
.cfi_offset 6, -16
.cfi_def_cfa_register 6
movl $0, %eax
call f
leave
ret
.cfi_endproc
.LFE1:
.size main, .-main
.ident "GCC: (Ubuntu/Linaro 4.4.4-14ubuntu5.1) 4.4.5"
.section .note.GNU-stack,"",@progbits
惊奇的是不管我在f加上inline还是没有,结果都是一样的,google一下,有人说是默认情况是不会优化的,加上-O后编译器会自动优化!
让后我加上-O,结果
.file "inline.c"
.section .rodata.str1.1,"aMS",@progbits,1
.LC0:
.string "hello\n"
.text
.globl f
.type f, @function
f:
.LFB22:
.cfi_startproc
subq $8, %rsp
.cfi_def_cfa_offset 16
movl $.LC0, %esi
movl $1, %edi
movl $0, %eax
call __printf_chk
addq $8, %rsp
ret
.cfi_endproc
.LFE22:
.size f, .-f
.globl main
.type main, @function
main:
.LFB23:
.cfi_startproc
subq $8, %rsp
.cfi_def_cfa_offset 16
movl $.LC0, %esi
movl $1, %edi
movl $0, %eax
call __printf_chk
addq $8, %rsp
ret
.cfi_endproc
.LFE23:
.size main, .-main
.ident "GCC: (Ubuntu/Linaro 4.4.4-14ubuntu5.1) 4.4.5"
.section .note.GNU-stack,"",@progbits
结果确实会优化,其实我的目的是要看看inline传给一个指针函数事,是否被优化(inline 可以被优化,是不是inline没有地址?,其实inline是有地址的!看看上面第一个汇编就知道了!)
我们试一试(把所有的注释去掉)
.file "inline.c"
.section .rodata.str1.1,"aMS",@progbits,1
.LC0:
.string "hello\n"
.text
.globl f
.type f, @function
f:
.LFB22:
.cfi_startproc
subq $8, %rsp
.cfi_def_cfa_offset 16
movl $.LC0, %esi
movl $1, %edi
movl $0, %eax
call __printf_chk
addq $8, %rsp
ret
.cfi_endproc
.LFE22:
.size f, .-f
.globl main
.type main, @function
main:
.LFB23:
.cfi_startproc
subq $8, %rsp
.cfi_def_cfa_offset 16
movl $.LC0, %esi
movl $1, %edi
movl $0, %eax
call __printf_chk
movl $.LC0, %esi
movl $1, %edi
movl $0, %eax
call __printf_chk
addq $8, %rsp
ret
.cfi_endproc
.LFE23:
.size main, .-main
.ident "GCC: (Ubuntu/Linaro 4.4.4-14ubuntu5.1) 4.4.5"
.section .note.GNU-stack,"",@progbits
被优化了!
这里是由于我在编写C语言的排序代码时遇到的问题
#include <stdio.h>
#include <string.h>
//get the length of array
#define len(array) \
(sizeof(array) / sizeof(array[0]))
//define a array and a size of its
typedef struct{
int *array;
int length;
}DArray;
//define a func pointer
typedef void (* ArrSort)(DArray,int,int);
typedef void (*ArrPrintf)(int *,int);
//define a struct to contain functions which can sort array
typedef struct{
ArrSort quick2Sort;
ArrSort bubbleSort;
ArrPrintf arrPrint;
DArray arr;
}ArrFuc;
//void APrintf(int *arr,int count);
//define a quick sort
void Quick2Sort(DArray arr,int left,int right)
{
// printf("data is %d",arr.length);
// APrintf(arr.array,arr.length);
if(left >= right)return;//one sort end,and next
int aleft = left;
int aright = right;
int *arrData = arr.array;
int key = arrData[aleft];//get a num to sort
while(aleft < aright)
{
while(aleft < aright && arrData[aright] >= key)
{
--aright;
}
arrData[aleft] = arrData[aright];//move data to left
while(aleft < aright && arrData[aleft] <= key)
{
++aleft;
}
arrData[aright] = arrData[aleft];
}
arrData[aleft] = key;
// APrintf(arrData,arr.length);
Quick2Sort(arr,left,aleft-1);
Quick2Sort(arr,aleft+1,right);
}
void APrintf(int *arr,int count);
// bubble sort
void BubbleSort(DArray arr,int count,int none)
{
int i=0,j=0,temp=0;
for(;i < count - 1;i++)
{
for(j=0;j < (count - i - 1);j++)
{
if(arr.array[j] > arr.array[j+1])
{
temp = arr.array[j];
arr.array[j] = arr.array[j+1];
arr.array[j + 1] = temp;
//APrintf(arr.array,10);
}
}
// APrintf(arr.array,10);
}
}
//print array
inline void APrintf(int *arr,int count)
{
int i = 0;
for(;i<count;i++)
{
printf("%d ",arr[i]);
}
printf("\n");
}
int main()
{
ArrFuc func;
DArray arrp;
//init func var
memset((void *)&func,0,sizeof(func));
func.quick2Sort = &Quick2Sort;
func.bubbleSort = &BubbleSort;
func.arrPrint = &APrintf;
int arr[10] = {2,11,22,34,5,56,3,2,1,62};
arrp.array = arr;
arrp.length=len(arr);
func.arr = arrp;
//call function in func and use array in func
// func.quick2Sort(func.arr,0,9);
// func.arrPrint(func.arr.array,func.arr.length);
#define NONE 0
func.bubbleSort(func.arr,func.arr.length,NONE);
func.arrPrint(func.arr.array,func.arr.length);
return 0;
}