Difference between revisions of "Function code size"

From Free Pascal wiki
Jump to navigationJump to search
(Created page with "Advice from programmer [https://gitlab.com/runewalsh Rika]. Pascal function binary size can be measured (to within the procedure entry alignment... usually 16 bytes) with: <s...")
 
Line 16: Line 16:
 
begin
 
begin
 
   writeln('MyProc code size: ', pointer(@MyProcEnd) - pointer(@MyProc), ' b');
 
   writeln('MyProc code size: ', pointer(@MyProcEnd) - pointer(@MyProc), ' b');
 +
end.
 +
</syntaxhighlight>
 +
 +
For '''assembler''' routines, either do the same but make MyProcEnd '''assembler''' too (still not sure if it works, I use the method below... but it definitely doesn’t work when mixing Pascal and assembler procedures), or do the following (finer and more future-compatible way that sadly does not compile with Pascal routines... and would miss the prologue and epilogue if it did):
 +
 +
<syntaxhighlight lang="pascal">
 +
label MyAsmProcStart, MyAsmProcEnd;
 +
 +
procedure MyAsmProc; assembler;
 +
asm
 +
MyAsmProcStart:
 +
  ...
 +
MyAsmProcEnd:
 +
end;
 +
 +
begin
 +
  writeln('MyAsmProc code size: ', pointer(@MyAsmProcEnd) - pointer(@MyAsmProcStart), ' b');
 
end.
 
end.
 
</syntaxhighlight>
 
</syntaxhighlight>

Revision as of 20:06, 6 September 2023

Advice from programmer Rika.

Pascal function binary size can be measured (to within the procedure entry alignment... usually 16 bytes) with:

procedure MyProc;
begin
  // ...
end;

// Must immediately follow MyProc in the source!
// Uses the implementation detail that the linker will lay them out in the same order.
procedure MyProcEnd; 
begin
end;

begin
  writeln('MyProc code size: ', pointer(@MyProcEnd) - pointer(@MyProc), ' b');
end.

For assembler routines, either do the same but make MyProcEnd assembler too (still not sure if it works, I use the method below... but it definitely doesn’t work when mixing Pascal and assembler procedures), or do the following (finer and more future-compatible way that sadly does not compile with Pascal routines... and would miss the prologue and epilogue if it did):

label MyAsmProcStart, MyAsmProcEnd;

procedure MyAsmProc; assembler;
asm
MyAsmProcStart:
  ...
MyAsmProcEnd:
end;

begin
  writeln('MyAsmProc code size: ', pointer(@MyAsmProcEnd) - pointer(@MyAsmProcStart), ' b');
end.