Recent

Author Topic: [Solved] Meaning of 'const' for externally linked functions / procedures  (Read 1215 times)

MathMan

  • Sr. Member
  • ****
  • Posts: 325
Hi all,

On const the manual(s) state "this is a contract between the programmer and the compiler, that the value will not be changed by the called function / procedure".

I was wondering what this means wrt externally linked functions / procedures - especially if these are implemented in assembler. I.e. for the following definition

Code: [Select]
function abc( const Val: QWORD ):QWORD;register;external name 'abc';

do I have to make sure that (Linux 64 bit ABI exemplary) RDI is equal on entry and exit? That would somehow contradict the ABI definition, that the registers RDI, RSI, RCX, RDX, R8-R11 and RAX have to be considered 'volatile'? Or is the compiler silently ignoring the 'const' declaration in this case?

Kind regards,
MathMan
« Last Edit: June 29, 2020, 01:33:12 pm by MathMan »

PascalDragon

  • Hero Member
  • *****
  • Posts: 5481
  • Compiler Developer
Re: Meaning of 'const' for externally linked functions / procedures
« Reply #1 on: June 29, 2020, 01:07:02 pm »
You don't need to care about any parameters. However especially if you're dealing with records you shouldn't change any fields, because the compiler assumes that the value doesn't change and might do certain optimizations on the caller side.

Also depending on the platform const might lead to parameters being passed differently (e.g. a ShortString might be passed as a reference instead of a copy on the stack).

For external functions (especially if you're dealing with non-Pascal code) you should use constref, var or out depending on your usecase. For assembly functions you can follow that advice as well or you'll need to check how a specific parameter is passed.

MathMan

  • Sr. Member
  • ****
  • Posts: 325
Re: Meaning of 'const' for externally linked functions / procedures
« Reply #2 on: June 29, 2020, 01:32:32 pm »
...

For external functions (especially if you're dealing with non-Pascal code) you should use constref, var or out depending on your usecase. For assembly functions you can follow that advice as well or you'll need to check how a specific parameter is passed.

Thanks for the clarifcation - solves it for me.

 

TinyPortal © 2005-2018