Sign in Join

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

Mailing lists / fpc-devel / 2010-03

cdecl and constant parameters

4 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

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

Seth Grover

The documentation
(http://www.freepascal.org/docs-html/ref/refsu56.html) says "A constant argument is passed by reference if its size is larger than a pointer. It is passed by value if the size is equal or is less then the size of a native pointer."

I have a packed record type defined which contains two 64-bit integers (so it's obviously bigger than a pointer). I wrote a routine that takes one of these records as a constant argument (eg., "procedure Doit(const rec : TMyRecord);")

I compiled it and looked at the assembly being generated, and sure enough it was being passed by reference, just as the documentation says. However, if the procedure is declared as "cdecl" the record itself (its contents) are copied onto the stack.

Should the documentation be updated? Or is this incorrect behavior?

Thanks,

-SG

This email is fiction. Any resemblance to actual events or persons living or dead is purely coincidental. Seth Grover sethdgrover[at]gmail[dot]com

Reply by email

Jonas Maebe
to Seth Grover
Seth Grover wrote 17 lines
The documentation (http://www.freepascal.org/docs-html/ref/refsu56.html) says "A constant argument is passed by reference if its size is larger than a pointer. It is passed by value if the size is equal or is less then the size of a native pointer." I have a packed record type defined which contains two 64-bit integers (so it's obviously bigger than a pointer). I wrote a routine that takes one of these records as a constant argument (eg., "procedure Doit(const rec : TMyRecord);") I compiled it and looked at the assembly being generated, and sure enough it was being passed by reference, just as the documentation says. However, if the procedure is declared as "cdecl" the record itself (its contents) are copied onto the stack. Should the documentation be updated? Or is this incorrect behavior?

I personally think that the documentation should not say anything at all about how "const" parameters are passed, as this should be an implementation detail that nobody should care about.

That said, with "cdecl", "const" has the same meaning as it has in C, which is "pass the argument exactly the same way as when there is no const modifier".

Jonas

Reply by email

Michael Van Canneyt
to Jonas Maebe
Jonas Maebe wrote 22 lines
On 31 Mar 2010, at 20:01, Seth Grover wrote: > The documentation > (http://www.freepascal.org/docs-html/ref/refsu56.html) says "A > constant argument is passed by reference if its size is larger than a > pointer. It is passed by value if the size is equal or is less then > the size of a native pointer." > > I have a packed record type defined which contains two 64-bit integers > (so it's obviously bigger than a pointer). I wrote a routine that > takes one of these records as a constant argument (eg., "procedure > Doit(const rec : TMyRecord);") > > I compiled it and looked at the assembly being generated, and sure > enough it was being passed by reference, just as the documentation > says. However, if the procedure is declared as "cdecl" the record > itself (its contents) are copied onto the stack. > > Should the documentation be updated? Or is this incorrect behavior? I personally think that the documentation should not say anything at all about how "const" parameters are passed, as this should be an implementation detail that nobody should care about.

While I principally agree with you:

It must, for Delphi compatibility. In Delphi the same is true, and is (unfortunately) documented.

We had this discussion already a couple of times.

Michael.

Show quoted text 8 lines
That said, with "cdecl", "const" has the same meaning as it has in C, which is "pass the argument exactly the same way as when there is no const modifier". Jonas_______________________________________________ fpc-devel maillist - http://lists.freepascal.org/mailman/listinfo/fpc-devel

Reply by email

Seth Grover

Jonas said:

Show quoted text 5 lines
I personally think that the documentation should not say anything at all about how "const" parameters are passed, as this should be an implementation detail that nobody should care about. That said, with "cdecl", "const" has the same meaning as it has in C, which is "pass the argument exactly the same way as when there is no const modifier".

In principle I do agree with you that most developers don't care about the underlying implementation of how parameters are passed. In my specific case, performance is extremely critical in the project we're working on, and knowing things like this are important to squeezing every last bit of performance out of our code (it's faster to pass a pointer to the record than put the whole thing on the stack).

Anyway, thanks for your insight. Sorry to bring up a discussion you've already had a few times (as Michael said), I guess I need to Google better next time. :)

Thanks again!

-SG

This email is fiction. Any resemblance to actual events or persons living or dead is purely coincidental. Seth Grover sethdgrover[at]gmail[dot]com

Reply by email