Sign in Join

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

Mailing lists / fpc-devel / 2015-04

Bug or Feature? Inline of code from Units does not work when types uses in code are defined in implementation

4 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

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

Michael Ring

Take the following unit, my goal is to have the code called in the unit inlined in the main code.

This works fine when

type
   pLongWord = ^LongWord;

is defined in the interface of the unit.

if this is defined in Implementation then Inlining of the code does not work.

Is this correct behaviour? Or should both ways work?

Thank you,

Michael

Platform is arm-embedded, Compiler Commandline was:

/usr/local/lib/fpc/3.1.1/ppcrossarm -MObjFPC -Tembedded -Parm -Cparmv6m -WpSTM32L053R8 -XParm-none-eabi- test.pas -a -g -B -Si

unit utest2;
{$inline on}
{$modeswitch advancedrecords}
interface
type
   TBits_16 = 0..65535;
   // Independent watchdog
   TIWDG_KR_bits = record
   private
     function getKEY      : TBits_16; inline;
     procedure setKEY(value : TBits_16); inline;
   public
     property KEY         : TBits_16 read getKEY write setKEY;// [0:15]
Key value (write only, read 0x0000)
   end;
   TIWDG_Registers = record
     KR_bits              : TIWDG_KR_bits;
     KR                   : longWord;                   // 0x00 Key register
   end;
const
     IWDG_BASE              = $40003000;
var
     IWDG                   : TIWDG_Registers absolute IWDG_BASE;
//type
//  pLongWord = ^LongWord;
implementation
type
   pLongWord = ^LongWord;
function TIWDG_KR_bits.getKEY : TBits_16; inline;
begin
   getKEY := pLongWord(@self)^ shr 0 and %1111111111111111;
end;
procedure TIWDG_KR_bits.setKEY(value : TBits_16); inline;
begin
   pLongWord(@self)^ := pLongWord(@self)^ and
%11111111111111110000000000000000 or (value shl 0);
end;
end.

Calling Prog:

program test;
{$INLINE ON}
{$modeswitch advancedrecords}
uses
   utest2;
var
   a : longWord;
begin
   IWDG.KR_bits.KEY := 1;
   a := IWDG.KR_bits.KEY;
end.

Reply by email

Florian Klaempfl
to Michael Ring
Michael Ring wrote 14 lines
Take the following unit, my goal is to have the code called in the unit inlined in the main code. This works fine when type pLongWord = ^LongWord; is defined in the interface of the unit. if this is defined in Implementation then Inlining of the code does not work. Is this correct behaviour? Or should both ways work?

Inline is only a recommendation to the compiler. Such a definition in the implementation part prevents indeed inlining.

In this particular case I recommend that you just leave the definition away so compiler uses that one of the system unit.

Reply by email

Michael Ring
to Florian Klaempfl

Thanks,

So do you think it is worth to extend documentation for inline? Or perhaps the compiler could give a warning when code is not suitable for inlining or is this decision done when a caller requests inlining and not when the inline function gets compiled?

Michael

Florian Klaempfl wrote 23 lines
Am 06.04.2015 um 17:53 schrieb Michael Ring: > Take the following unit, my goal is to have the code called in the unit > inlined in the main code. > > This works fine when > > type > pLongWord = ^LongWord; > > is defined in the interface of the unit. > > if this is defined in Implementation then Inlining of the code does not > work. > > Is this correct behaviour? Or should both ways work? Inline is only a recommendation to the compiler. Such a definition in the implementation part prevents indeed inlining. In this particular case I recommend that you just leave the definition away so compiler uses that one of the system unit. _______________________________________________ fpc-devel maillist - http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel

Reply by email

Florian Klämpfl
to Michael Ring
Michael Ring wrote 5 lines
Thanks, So do you think it is worth to extend documentation for inline? Or perhaps the compiler could give a warning when code is not suitable for inlining or is this decision done when a caller requests inlining and not when the inline function gets compiled?

If the situation is clear, the compiler gives a hint. In this case, the procedure is inlined if it is called in the same unit, otherwise not.

The decision, if a procedure can be inlined is pretty difficult and changes also from time to time, so I prefer neither to document (situation might change and documentation gets wrong) it nor increase the hints regarding this (sometimes it is really hard to explain why something is not inlined).

Reply by email