[fpc-pascal] ++ and -- ( and +=, -=, ...)

Gerhard Scholz gs at g--s.de
Wed Jul 31 04:46:58 CEST 2013


I tried it now with locals and globals, but with the same results.

The code:

program plusas ;

  type
    t = array [ 1..10 ] of longint ;

  var
    i : longint ;
    a : t ;

  function incg ( var i : longint ) : longint ;
//inc(i),return the incremented i

    begin
      inc ( i ) ;
      result := i ;
     end ;

  procedure l ;

    var
      i : longint ;
      a : t ;

    function incl ( var i : longint ) : longint ;
//inc(i),return the incremented i

      begin
        inc ( i ) ;
        result := i ;
       end ;

    begin
      i := 1 ;
      a[i] := a[i] + 3 ;
      a[i] += 3 ;
      a[incg ( i )] += 3 ;
      a[incl ( i )] += 3 ;
     end ;

  begin
    i := 1 ;
    a[i] := a[i] + 3 ;
    a[i] += 3 ;
    a[incg ( i )] += 3 ;
   end.

Very obviously the += is implemented as a kind of macro, if the index is a 
function, it is called twice with surprising result.

According to the FPC manual (I just looked it up), which says:

    a += b Adds b to a, and stores the result in a.
    Remark: These constructions are just for typing convenience, they don't 
generate different code.

this is the expected result, I must admit.

>From my common sense thinking, i would have expected something like:

    a[incg ( i )] += 3 ;

translates to

    P := @ a[incg ( i )] ;
    P^ := P^ + 3 ;

I have no C compiler since I don't like C, but I would like to know how C 
compilers handle +=.

The GNU C manual says:
   +=  Adds the two operands together, and then assign the result of the 
addition to the left operand.

Could be understood ambiguous also.

I know the += from Algol68, where it is defined very exact:

op (hplusab, +:=)  =(ref  int a,  int b) ref  int :a := a + b;

or, in pascal-like terms

operator += ( var a : longint ; b : longint ) z : longint ; begin a := a + b 
; z := a ; end ;

Exact definitions normally give less surprising results.
So I think I will forget += etc.

----- Original Message ----- 
From: "Jonas Maebe" <jonas.maebe at elis.ugent.be>
To: "FPC-Pascal users discussions" <fpc-pascal at lists.freepascal.org>
Sent: Tuesday, July 30, 2013 10:41 PM
Subject: Re: [fpc-pascal] ++ and -- ( and +=, -=, ...)



On 30 Jul 2013, at 22:17, Gerhard Scholz wrote:

> Beside of the question, if ++,--,+=,etc. fit into Pascal or not, there 
> stays the question: is it a plus?
> I expected that
> a[i] += 3
> compiles better than
> a[i] := a[i] + 3
> I expected that the computation of a[i] is done only once, but the 
> produced code is the same, the address of a[i] is computed twice.
> So the whole construct is only a typing saving.
> Compilation done with FP 2.6.2, winxp, 32-bit)

Syntax and generated code are in principle unrelated. The reason you don't 
get the optimised version of the code, is probably because you used global 
variables. Make the array and i local, and you will see that in both cases 
the address of a[i] is calculated only once. Constructs involving global 
variables are harder to analyse for side-effects, so they are simply not 
optimised at all in many cases by FPC.

> Constructs like I++, ++I are nice shortcuts (and sometimes the code can be 
> better readable), but have only a real value, if the produced code is a 
> bit optimized.

That was true in the eighties when C statements were pretty much directly 
mapped to assembler. Nowadays, they make code actually harder to optimise 
because they introduce side-effects in the middle of expressions.

Adding a particular syntax to a programming language in order to work around 
a (realistically solvable) weakness of the optimiser is an extremely bad 
approach to language design.


Jonas_______________________________________________
fpc-pascal maillist  -  fpc-pascal at lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal 




More information about the fpc-pascal mailing list