RE: Inlining function pointers

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



So an example code would be:

#include 

inline void print_stuff() {
  printf("yavuz");
}

inline void call_func(void (*my_func)()) { 
  my_func();
}

int main() {
  call_func(print_stuff);
}


I tried it on Mac OS X gcc-4.0.1 (I guess). The compiler complained. 

Now I'm in the office and I just tried it with "gcc version 4.3.3 (Ubuntu 4.3.3-5ubuntu4)" and it compiled fine. So, it's probably an issue with the old compiler. But the question is, even though it compiles, can it inline both of the functions? The assembly output shows that it doesn't either of them, but I don't know if it is the right place to look:

trash$gcc  -finline-functions -S den.c
trash$more den.s
    .file    "den.c"
    .section    .rodata
.LC0:
    .string    "yavuz"
    .text
.globl print_stuff
    .type    print_stuff, @function
print_stuff:
    pushl    %ebp
    movl    %esp, %ebp
    subl    $8, %esp
    movl    $.LC0, (%esp)
    call    printf
    leave
    ret
    .size    print_stuff, .-print_stuff
.globl call_func
    .type    call_func, @function
call_func:
    pushl    %ebp
    movl    %esp, %ebp
    subl    $8, %esp
    movl    8(%ebp), %eax
    call    *%eax
    leave
    ret
    .size    call_func, .-call_func
.globl main
    .type    main, @function
main:
    leal    4(%esp), %ecx
    andl    $-16, %esp
    pushl    -4(%ecx)
    pushl    %ebp
    movl    %esp, %ebp
    pushl    %ecx
    subl    $20, %esp
    movl    $print_stuff, (%esp)
    call    call_func
    addl    $20, %esp
    popl    %ecx
    popl    %ebp
    leal    -4(%ecx), %esp
    ret
    .size    main, .-main
    .ident    "GCC: (Ubuntu 4.3.3-5ubuntu4) 4.3.3"
    .section    .note.GNU-stack,"",@progbits


> To: yavuzyetim@xxxxxxxxxxx
> CC: gcc-help@xxxxxxxxxxx
> Subject: Re: Inlining function pointers
> From: iant@xxxxxxxxxx
> Date: Tue, 25 Aug 2009 22:43:19 -0700
> 
> yavuz yetim  writes:
> 
>> I am using GTree in GLib and the g_tree_foreach() -shown below- function takes a function pointer to call a function (say comp()) for each node while traversing the tree. 
>> If I declare the function to be inlined and compile it, gcc comlains with following message:
>> /var/folders/eh/ehsWlP8bFe8W4KcsmZuVyk+++TI/-Tmp-//ccXexCH9.s:2592:non-relocatable subtraction expression, "_str_print" minus "L00000000006$pb"
>> /var/folders/eh/ehsWlP8bFe8W4KcsmZuVyk+++TI/-Tmp-//ccXexCH9.s:2592:symbol: "_str_print" can't be undefined in a subtraction expression
>>
>>
>>
>> somewhere in main:: g_tree_foreach(my_tree, comp, NULL);
>> functions::
>> inline gboolean comp(/*arguments*/) { /*do stuff*/ return FALSE;}
>> /*g_tree_foreach From GLib source*/void g_tree_foreach (GTree *tree, GTraverseFunc  func, gpointer user_data){    GTreeNode *node;
>>   g_return_if_fail (tree != NULL);      if (!tree->root)        return;
>>   node = g_tree_first_node (tree);      while (node)    {          if ((*func) (node->key, node->value, user_data))	       break;                node = g_tree_node_next (node);      }}
> 
> This looks like a compiler bug but it's hard to be certain.  I don't see
> anything named _str_print in your code sample.  Please give us a
> complete small test case.  Please also tell us what version of gcc you
> are using, and what target you are compiling for.
> 
> Ian

_________________________________________________________________
With Windows Live, you can organize, edit, and share your photos.
http://www.windowslive.com/Desktop/PhotoGallery


[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux