Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2013-01

Inline methods

7 messages · 4 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Ludo Brands

Hi,

I'm running into a strange problem with inline methods.

   TCircularBuffer=class
   ...
     Pin:pointer;
     Pout:pointer;
     function IsFull:boolean;inline;
   end;
function TCircularBuffer.IsFull: boolean;inline;
begin
   result:=PIn=POut;
end;

When the method is called from outside the class then it is inlined (x64 linux):

dbcacheservercore.pas:197 if FBuffer[currbuf].IsFull then 0x000000000048e4bf <+47>: mov -0x18(%rbp),%rax 0x000000000048e4c3 <+51>: mov 0x8(%rax),%rdx 0x000000000048e4c7 <+55>: movslq -0x24(%rbp),%rax 0x000000000048e4cb <+59>: mov (%rdx,%rax,8),%rax 0x000000000048e4cf <+63>: mov %rax,-0x30(%rbp) 0x000000000048e4d3 <+67>: mov -0x30(%rbp),%rax 0x000000000048e4d7 <+71>: mov -0x30(%rbp),%rdx 0x000000000048e4db <+75>: mov 0x28(%rax),%rax 0x000000000048e4df <+79>: cmp 0x30(%rdx),%rax 0x000000000048e4e3 <+83>: sete %al
0x000000000048e4e6 <+86>: test %al,%al
0x000000000048e4e8 <+88>: je 0x48e585 <ADD+245>

But when the method is called from inside the class then a call is used:

dbcacheservercore.pas:109 if IsFull then
0x000000000048dfc8 <+24>: mov -0x18(%rbp),%rdi 0x000000000048dfcc <+28>: callq 0x48e170 <ISFULL> 0x000000000048dfd1 <+33>: test %al,%al
0x000000000048dfd3 <+35>: jne 0x48e042 <ADD+146>

Using Self.IsFull results in the same code.

FPC 2.6.0 and 2.7.1 from today.

A bug?

Thanks, Ludo

Reply by email

Jonas Maebe
to Ludo Brands
Ludo Brands wrote
When the method is called from outside the class then it is inlined (x64 linux):

[snip]

But when the method is called from inside the class then a call is used:

[snip]

Show quoted text 5 lines
Using Self.IsFull results in the same code. FPC 2.6.0 and 2.7.1 from today. A bug?

Are those calls before or after the method body appears in the source code? The compiler cannot inline anything for which it hasn't parsed the source code yet.

Jonas

Reply by email

Michael Van Canneyt
to Jonas Maebe
Jonas Maebe wrote 17 lines
On 16 Jan 2013, at 12:16, Ludo Brands wrote: When the method is called from outside the class then it is inlined (x64 linux): [snip] But when the method is called from inside the class then a call is used: [snip] Using Self.IsFull results in the same code. FPC 2.6.0 and 2.7.1 from today. A bug? Are those calls before or after the method body appears in the source code? The compiler cannot inline anything for which it hasn't parsed the source code yet.

Does that differ ? If so, then I should mention this in the documentation !

Michael.

Reply by email

Jonas Maebe
to Michael Van Canneyt
Michael Van Canneyt wrote 21 lines
On Wed, 16 Jan 2013, Jonas Maebe wrote: > On 16 Jan 2013, at 12:16, Ludo Brands wrote: > >> When the method is called from outside the class then it is >> inlined (x64 linux): >> [snip] >> But when the method is called from inside the class then a >> call is used: >> [snip] >> Using Self.IsFull results in the same code. >> >> FPC 2.6.0 and 2.7.1 from today. >> >> A bug? > Are those calls before or after the method body appears in the > source code? The compiler cannot inline anything for which it > hasn't parsed the source code yet. Does that differ ? If so, then I should mention this in the documentation !

Someone else also thought so in the past: http://lists.freepascal.org/lists/fpc-pascal/2009-November/023321.html :)

Jonas

Reply by email

Ludo Brands
to Jonas Maebe: [snip] [snip] Are those calls before or after the method body appears in the source code?…
Jonas Maebe wrote 7 lines
Are those calls before or after the method body appears in the source code? The compiler cannot inline anything for which it hasn't parsed the source code yet.

The method was implemented below the code that used it. Moving it up solved it.

Actually the "inline;' in the declaration is not really used. Removing it results in the same code. Perhaps a Hint or Warning when inline is used in the declaration could avoid these mistakes.

Thanks a lot.

Ludo

Reply by email

Jonas Maebe
to Ludo Brands
Ludo Brands wrote 12 lines
On 16/01/2013 13:17, Jonas Maebe wrote: > > Are those calls before or after the method body appears in the > source code? The compiler cannot inline anything for which it > hasn't parsed the source code yet. > The method was implemented below the code that used it. Moving it up solved it. Actually the "inline;' in the declaration is not really used. Removing it results in the same code. Perhaps a Hint or Warning when inline is used in the declaration could avoid these mistakes.

Actually, the correct behaviour would be that the compiler requires that it is only specified in the interface, or both in the interface and in the implementation (I'm not sure yet what's most appropriate). Inlining is always also enabled across units and hence should have to be specified in the interface.

Jonas

Reply by email