public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Inlining function pointers
@ 2009-08-26 14:38 yavuz yetim
  2009-08-26 14:47 ` Ian Lance Taylor
  0 siblings, 1 reply; 6+ messages in thread
From: yavuz yetim @ 2009-08-26 14:38 UTC (permalink / raw)
  To: gcc-help


Hi,
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);      }}

Thanks,Yavuz

_________________________________________________________________
Hotmail® is up to 70% faster. Now good news travels really fast. 
http://windowslive.com/online/hotmail?ocid=PID23391::T:WLMTAGL:ON:WL:en-US:WM_HYGN_faster:082009

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Inlining function pointers
  2009-08-26 14:38 Inlining function pointers yavuz yetim
@ 2009-08-26 14:47 ` Ian Lance Taylor
  2009-08-26 16:31   ` yavuz yetim
  0 siblings, 1 reply; 6+ messages in thread
From: Ian Lance Taylor @ 2009-08-26 14:47 UTC (permalink / raw)
  To: yavuz yetim; +Cc: gcc-help

yavuz yetim <yavuzyetim@hotmail.com> 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

^ permalink raw reply	[flat|nested] 6+ messages in thread

* RE: Inlining function pointers
  2009-08-26 14:47 ` Ian Lance Taylor
@ 2009-08-26 16:31   ` yavuz yetim
       [not found]     ` <C6BAB13B.1219F%eljay@adobe.com>
  2009-08-27  8:17     ` Ian Lance Taylor
  0 siblings, 2 replies; 6+ messages in thread
From: yavuz yetim @ 2009-08-26 16:31 UTC (permalink / raw)
  To: iant; +Cc: gcc-help


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@hotmail.com
> CC: gcc-help@gcc.gnu.org
> Subject: Re: Inlining function pointers
> From: iant@google.com
> 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

^ permalink raw reply	[flat|nested] 6+ messages in thread

* FW: Inlining function pointers
       [not found]     ` <C6BAB13B.1219F%eljay@adobe.com>
@ 2009-08-27  0:18       ` yavuz yetim
  2009-08-27  0:20         ` yavuz yetim
  0 siblings, 1 reply; 6+ messages in thread
From: yavuz yetim @ 2009-08-27  0:18 UTC (permalink / raw)
  To: gcc help




----------------------------------------
> From: yavuzyetim@hotmail.com
> To: eljay@adobe.com
> Subject: RE: Inlining function pointers
> Date: Wed, 26 Aug 2009 14:34:09 +0000
>
>
> Sorry, my mistake, so I tried the below: It does inline the regular call but why can't it inline the function pointer? It knows which function this pointer is pointing to beforehand. So shouldn't it inline it?
>
> #include
>
> static inline void print_stuff() __attribute__((always_inline));
> static inline void print_stuff()
> {
> printf("yavuz");
> }
> static inline void call_func(void (*my_func)()) __attribute__((always_inline));
> static inline void call_func(void (*my_func)()) {
> my_func();
> }
>
> int main() {
> call_func(print_stuff);
> }
>
> and the result is:
> trash$gcc -S den.c
> trash$more den.s
> .file "den.c"
> .text
> .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, -8(%ebp)
> movl -8(%ebp), %eax
> call *%eax
> addl $20, %esp
> popl %ecx
> popl %ebp
> leal -4(%ecx), %esp
> ret
> .size main, .-main
> .section .rodata
> .LC0:
> .string "yavuz"
> .text
> .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
> .ident "GCC: (Ubuntu 4.3.3-5ubuntu4) 4.3.3"
> .section .note.GNU-stack,"",@progbits
>
>
>
> ----------------------------------------
>> From: eljay@adobe.com
>> To: yavuzyetim@hotmail.com
>> Date: Wed, 26 Aug 2009 07:30:35 -0700
>> Subject: Re: Inlining function pointers
>>
>> Hi yavuz,
>>
>>> trash$gcc -finline-functions -S den.c
>>
>> That won't result in the functions to be inlined.
>>
>> You have to enable optimization as well: -O1 -finline-functions
>>
>> Sincerely,
>> --Eljay
>>
>
> _________________________________________________________________
> Windows Live: Make it easier for your friends to see what you’re up to on Facebook.
> http://windowslive.com/Campaign/SocialNetworking?ocid=PID23285::T:WLMTAGL:ON:WL:en-US:SI_SB_facebook:082009

_________________________________________________________________
Get back to school stuff for them and cashback for you.
http://www.bing.com/cashback?form=MSHYCB&publ=WLHMTAG&crea=TEXT_MSHYCB_BackToSchool_Cashback_BTSCashback_1x1

^ permalink raw reply	[flat|nested] 6+ messages in thread

* RE: Inlining function pointers
  2009-08-27  0:18       ` FW: " yavuz yetim
@ 2009-08-27  0:20         ` yavuz yetim
  0 siblings, 0 replies; 6+ messages in thread
From: yavuz yetim @ 2009-08-27  0:20 UTC (permalink / raw)
  To: gcc help


When I try it with -O1, it does inline the pointer as well...
Sorry for my spams.

Thanks,
Yavuz

----------------------------------------
> From: yavuzyetim@hotmail.com
> To: gcc-help@gcc.gnu.org
> Subject: FW: Inlining function pointers
> Date: Wed, 26 Aug 2009 14:34:40 +0000
>
>
>
>
> ----------------------------------------
>> From: yavuzyetim@hotmail.com
>> To: eljay@adobe.com
>> Subject: RE: Inlining function pointers
>> Date: Wed, 26 Aug 2009 14:34:09 +0000
>>
>>
>> Sorry, my mistake, so I tried the below: It does inline the regular call but why can't it inline the function pointer? It knows which function this pointer is pointing to beforehand. So shouldn't it inline it?
>>
>> #include
>>
>> static inline void print_stuff() __attribute__((always_inline));
>> static inline void print_stuff()
>> {
>> printf("yavuz");
>> }
>> static inline void call_func(void (*my_func)()) __attribute__((always_inline));
>> static inline void call_func(void (*my_func)()) {
>> my_func();
>> }
>>
>> int main() {
>> call_func(print_stuff);
>> }
>>
>> and the result is:
>> trash$gcc -S den.c
>> trash$more den.s
>> .file "den.c"
>> .text
>> .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, -8(%ebp)
>> movl -8(%ebp), %eax
>> call *%eax
>> addl $20, %esp
>> popl %ecx
>> popl %ebp
>> leal -4(%ecx), %esp
>> ret
>> .size main, .-main
>> .section .rodata
>> .LC0:
>> .string "yavuz"
>> .text
>> .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
>> .ident "GCC: (Ubuntu 4.3.3-5ubuntu4) 4.3.3"
>> .section .note.GNU-stack,"",@progbits
>>
>>
>>
>> ----------------------------------------
>>> From: eljay@adobe.com
>>> To: yavuzyetim@hotmail.com
>>> Date: Wed, 26 Aug 2009 07:30:35 -0700
>>> Subject: Re: Inlining function pointers
>>>
>>> Hi yavuz,
>>>
>>>> trash$gcc -finline-functions -S den.c
>>>
>>> That won't result in the functions to be inlined.
>>>
>>> You have to enable optimization as well: -O1 -finline-functions
>>>
>>> Sincerely,
>>> --Eljay
>>>
>>
>> _________________________________________________________________
>> Windows Live: Make it easier for your friends to see what you’re up to on Facebook.
>> http://windowslive.com/Campaign/SocialNetworking?ocid=PID23285::T:WLMTAGL:ON:WL:en-US:SI_SB_facebook:082009
>
> _________________________________________________________________
> Get back to school stuff for them and cashback for you.
> http://www.bing.com/cashback?form=MSHYCB&publ=WLHMTAG&crea=TEXT_MSHYCB_BackToSchool_Cashback_BTSCashback_1x1

_________________________________________________________________
Windows Live: Keep your friends up to date with what you do online.
http://windowslive.com/Campaign/SocialNetworking?ocid=PID23285::T:WLMTAGL:ON:WL:en-US:SI_SB_online:082009

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Inlining function pointers
  2009-08-26 16:31   ` yavuz yetim
       [not found]     ` <C6BAB13B.1219F%eljay@adobe.com>
@ 2009-08-27  8:17     ` Ian Lance Taylor
  1 sibling, 0 replies; 6+ messages in thread
From: Ian Lance Taylor @ 2009-08-27  8:17 UTC (permalink / raw)
  To: yavuz yetim; +Cc: gcc-help

yavuz yetim <yavuzyetim@hotmail.com> writes:

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

Sounds like a bug in that relatively old compiler.

> 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:

When I compile your test case with gcc mainline, it inlines both
functions into main.

> trash$gcc  -finline-functions -S den.c

You need to compile with optimization (the -O option)--simply specifying
-finline-functions is insufficient.

Ian

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2009-08-26 14:49 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-08-26 14:38 Inlining function pointers yavuz yetim
2009-08-26 14:47 ` Ian Lance Taylor
2009-08-26 16:31   ` yavuz yetim
     [not found]     ` <C6BAB13B.1219F%eljay@adobe.com>
2009-08-27  0:18       ` FW: " yavuz yetim
2009-08-27  0:20         ` yavuz yetim
2009-08-27  8:17     ` Ian Lance Taylor

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).