Constant parameters and double types

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Constant parameters and double types

Luiz Americo Pereira Camara-2
I'm evaluating the effect of passing function parameters by value or by
reference.

Like stated in the documentation, passing as reference (const) or by
value types with size equal to pointer size like Integer has no
difference in the code output (they are passed by value).

Also as expected i noticed a difference in the generated assembler for
types TRect (16bytes) or TPoint (8 bytes).

But for double (8 bytes) the generated assembler are equal for both
types of parameters. Is this intentional? For double types, there's no
difference in using const modifier?

I'm using a 32bit cpu.

Luiz

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Constant parameters and double types

Luiz Americo Pereira Camara-2
Luiz Americo Pereira Camara wrote:

> I'm evaluating the effect of passing function parameters by value or
> by reference.
>
> Like stated in the documentation, passing as reference (const) or by
> value types with size equal to pointer size like Integer has no
> difference in the code output (they are passed by value).
>
> Also as expected i noticed a difference in the generated assembler for
> types TRect (16bytes) or TPoint (8 bytes).
>
> But for double (8 bytes) the generated assembler are equal for both
> types of parameters. Is this intentional? For double types, there's no
> difference in using const modifier?
>
> I'm using a 32bit cpu.
>
I attached the source code of a small app and the asm output of the
functions.

It seems that for double fpc pass by reference if the parameter is not
assigned regardless of const modifier. Can someone confirm this?

.section .text.n_p$asmconstparameter_byvalue$double
        .balign 16,0x90
.globl P$ASMCONSTPARAMETER_BYVALUE$DOUBLE
P$ASMCONSTPARAMETER_BYVALUE$DOUBLE:
# Temps allocated between ebp-8 and ebp-8
# [17] begin
        pushl %ebp
        movl %esp,%ebp
        subl $8,%esp
# Var V located at ebp+8
# Var i located at ebp-8
# [18] i := V;
        movl 8(%ebp),%eax
        movl %eax,-8(%ebp)
        movl 12(%ebp),%eax
        movl %eax,-4(%ebp)
# [19] DoIt(i);
        subl $8,%esp
        movl -8(%ebp),%eax
        movl %eax,(%esp)
        movl -4(%ebp),%eax
        movl %eax,4(%esp)
        call P$ASMCONSTPARAMETER_DOIT$DOUBLE
# [20] end;
        leave
        ret $8

.section .text.n_p$asmconstparameter_byreference$double
        .balign 16,0x90
.globl P$ASMCONSTPARAMETER_BYREFERENCE$DOUBLE
P$ASMCONSTPARAMETER_BYREFERENCE$DOUBLE:
# Temps allocated between ebp-8 and ebp-8
# [25] begin
        pushl %ebp
        movl %esp,%ebp
        subl $8,%esp
# Var V located at ebp+8
# Var i located at ebp-8
# [26] i := V;
        movl 8(%ebp),%eax
        movl %eax,-8(%ebp)
        movl 12(%ebp),%eax
        movl %eax,-4(%ebp)
# [27] DoIt(i);
        subl $8,%esp
        movl -8(%ebp),%eax
        movl %eax,(%esp)
        movl -4(%ebp),%eax
        movl %eax,4(%esp)
        call P$ASMCONSTPARAMETER_DOIT$DOUBLE
# [28] end;
        leave
        ret $8

program asmConstParameter;

{$Mode ObjFpc}
{$H+}

uses
  SysUtils, Types;

procedure DoIt(V: Double);
begin
  Write(V);
end;  
 
procedure ByValue(V: Double);
var
  i: Double;
begin
  i := V;  
  DoIt(i);
end;

procedure ByReference(const V: Double);
var
  i: Double;
begin
  i := V;
  DoIt(i);
end;

var
  x: Double;

begin  
  x := 1.1;
  ByValue(X);
  ByReference(X);
end.

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal