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

Jonas Maebe jonas.maebe at elis.ugent.be
Tue Jul 30 22:41:05 CEST 2013


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


More information about the fpc-pascal mailing list