[fpc-pascal] How does inline work?
Jonas Maebe
jonas.maebe at elis.ugent.be
Sun Nov 22 12:25:32 CET 2009
On 22 Nov 2009, at 10:10, Thierry Coq wrote:
> I'm trying to use inline to remove unnecessary debugging code, such as the following code, with FPC 2.3.1 on Windows, when compiling for production.
> However when I compile the code, at O1 optimisation level (my default), the call to DebugInfo is still present in the generated assembler.
Always provide a compilable sample with the full compiler invocation when reporting problems. After guessing how your the full source code looks and compiling it, the call is removed here:
$ cat tt.pp
unit tt;
interface
{$Inline on}
type
TForm1 = class
private
procedure DebugInfo; inline;
public
procedure DoSomething;
end;
Implementation
procedure TForm1.DebugInfo;
begin
{$ifdef debug}
DebugInfo
{$endif}
end;
procedure TForm1.DoSomething;
begin
DebugInfo;
end;
end.
$ fpc -O1 -al tt -Sd
Free Pascal Compiler version 2.4.0rc1 [2009/10/24] for i386
Copyright (c) 1993-2009 by Florian Klaempfl
Target OS: Darwin for i386
Compiling tt.pp
Assembling tt
28 lines compiled, 0.0 sec
$ cat tt.s
...
.text
.align 4
.globl _TT_TFORM1_$__DOSOMETHING
_TT_TFORM1_$__DOSOMETHING:
# Temps allocated between ebp-4 and ebp-4
# [24] begin
pushl %ebp
movl %esp,%ebp
subl $8,%esp
# Var $self located at ebp-4
movl %eax,-4(%ebp)
# [26] end;
leave
ret
...
I guess the real case in which you observe that behaviour is more complicated, with circular dependencies between implementation units, or whereby the implementation of the "inline" routine appears only after it is called. In the former case you have to recompile several times before all inline statements can take effect, since the compiler can only methods for which it knows the implementation at the point when these methods are called. In the latter case, the compiler will not be able to inline the routine in the unit before its implementation is parsed.
Jonas
More information about the fpc-pascal
mailing list