Function code size: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: Function code size
| Old | New | Line | |
|---|---|---|---|
| 1 | + | Advice from programmer [Rika](https://gitlab.com/runewalsh). | |
| 2 | + | ||
| 3 | + | Pascal function binary size can be measured (to within the procedure entry alignment... usually 16 bytes) with: | |
| 4 | + | ||
| 5 | + | ``` pascal | |
| 6 | + | procedure MyProc; | |
| 7 | + | begin | |
| 8 | + | // ... | |
| 9 | + | end; | |
| 10 | + | ||
| 11 | + | // Must immediately follow MyProc in the source! | |
| 12 | + | // Uses the implementation detail that the linker will lay them out in the same order (which is not guaranteed by any linker, so this may not work). | |
| 13 | + | procedure MyProcEnd; | |
| 14 | + | begin | |
| 15 | + | end; | |
| 16 | + | ||
| 17 | + | begin | |
| 18 | + | writeln('MyProc code size: ', pointer(@MyProcEnd) - pointer(@MyProc), ' b'); | |
| 19 | + | end. | |
| 20 | + | ``` | |
| 21 | + | ||
| 22 | + | 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): | |
| 23 | + | ||
| 24 | + | ``` pascal | |
| 25 | + | label MyAsmProcEnd; | |
| 26 | + | ||
| 27 | + | procedure MyAsmProc; assembler; | |
| 28 | + | asm | |
| 29 | + | // ... | |
| 30 | + | MyAsmProcEnd: | |
| 31 | + | end; | |
| 32 | + | ||
| 33 | + | begin | |
| 34 | + | writeln('MyAsmProc code size: ', pointer(@MyAsmProcEnd) - pointer(@MyAsmProc), ' b'); | |
| 35 | + | end. | |
| 36 | + | ``` | |
| 37 | + | ||
| 38 | + | This method misses the epilogue size, but the epilogue is negligible (1-digit) and constant; and if you have X bytes, after adding 17 more bytes of instructions you will have exactly X + 17. | |
| 39 | + | ||
| 40 | + | --- | |
| 41 | + | *From the Free Pascal wiki, page "Function code size", as it stood on 2023-09-17 after a change by Jonas, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).* |
