Difference between revisions of "Function code size"
From Lazarus wiki
Jump to navigationJump to searchLine 19: | Line 19: | ||
</syntaxhighlight> | </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 | + | 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): |
<syntaxhighlight lang="pascal"> | <syntaxhighlight lang="pascal"> | ||
Line 34: | Line 34: | ||
end. | end. | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | |||
+ | This method misses the epilogue size. | ||
+ | But epilogue is negligible (1-digit) and constant; and if you got X bytes, after adding 17 more bytes of instructions you’ll see exactly X + 17. |
Revision as of 21:08, 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):
label MyAsmProcEnd;
procedure MyAsmProc; assembler;
asm
// ...
MyAsmProcEnd:
end;
begin
writeln('MyAsmProc code size: ', pointer(@MyAsmProcEnd) - pointer(@MyAsmProc), ' b');
end.
This method misses the epilogue size. But epilogue is negligible (1-digit) and constant; and if you got X bytes, after adding 17 more bytes of instructions you’ll see exactly X + 17.