www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Inner functions vs inlining

reply quetzal <quetzal_member pathlink.com> writes:
Let's take following code

===== cut here =====
int baz2(int a) { return a + 1; }
int bar2(int a) { return baz2(a) + 2; }
int foo2(int a) { return bar2(a) + 4; }

int foo(int a) {
int bar(int a) {
int baz(int a) { return a + 1; }
return baz(a) + 2;
}
return bar(a) + 4;
}
int main()
{
return foo(5) + foo2(5);
}
===== cut here =====
and compile with flags -O -inline (dmd 0.95 windows version)
what we see in assembly listing?
_TEXT:00402044                 push    eax
_TEXT:00402045                 mov     eax, 5
_TEXT:0040204A                 call    402034
_TEXT:0040204F                 add     eax, 0Ch
_TEXT:00402052                 pop     ecx
_TEXT:00402053                 retn
as we can clearly see, foo() call was not properly inlined, while
foo2() along with calls to bar2() and baz2() was inlined perfectly.
if we take a look at foo() assembly listing..
_TEXT:00402034                 push    eax
_TEXT:00402035                 push    eax
_TEXT:00402036                 lea     eax, [esp+8+var_4]
_TEXT:0040203A                 call    402010 ; bar()
_TEXT:0040203F                 add     eax, 4
_TEXT:00402042                 pop     ecx
_TEXT:00402043                 retn
. we can see that bar() call was not inlined also.
and bar() is..
_TEXT:00402010                 mov     eax, [esp+arg_0]
_TEXT:00402014                 add     eax, 3
_TEXT:00402017                 retn    4
finally we got some inlining happened here, baz() was properly inlined after
all.

somehow inlining for inner functions is not working properly, or maybe i'm
missing something?

also there's issue with calling convention.. there's no fastcall (or similar) in
D? value is being passed via stack all the time, even for such trivial functions
(which is no good)

also i'd like to ask Walter once again to think about adding assembly listing
generation to D compiler. its so damn convenient in msvc/icc to have mixed
source/assembly listing generated by compiler where each source is followed by
assembly code produced.
i was told that compiler generates instructions directly, skipping assembly
phase, but i dont find it to be a real problem.

p.s. if there's no convenient disassembly library i could even code one <g>
Jul 09 2004
parent "Walter" <newshound digitalmars.com> writes:
"quetzal" <quetzal_member pathlink.com> wrote in message
news:ccnian$1dg1$1 digitaldaemon.com...
 somehow inlining for inner functions is not working properly, or maybe i'm
 missing something?
I'll look at it.
 also there's issue with calling convention.. there's no fastcall (or
similar) in
 D? value is being passed via stack all the time, even for such trivial
functions
 (which is no good)
It's not really needed. And for functions with one argument, the argument is passed in EAX.
 also i'd like to ask Walter once again to think about adding assembly
listing
 generation to D compiler. its so damn convenient in msvc/icc to have mixed
 source/assembly listing generated by compiler where each source is
followed by
 assembly code produced.
 i was told that compiler generates instructions directly, skipping
assembly
 phase, but i dont find it to be a real problem.

 p.s. if there's no convenient disassembly library i could even code one
<g> All you need to do for mixed assembly/source is to compile with -g and run obj2asm over the output.
Jul 10 2004