Due to the way pgen currently works, functions with side effects can be reexecuted
The problem
Pgen handles the dup instruction and some others by duplicating the entire expression tree under the operator. This is functionally valid, but wasteful. The plan was to have it cleaned up by optimization phases. The problem is that functions, as well as operands, can be reexecuted by such a step. This will cause functions with side effects to be reexecuted:
program test;
var i: integer;
function alpha: integer;
begin
i := i+1;
alpha := i
end;
begin
i := 1
end.
Would result in a counting sequence in variable i. With a double execution, the count would happen in 2's, that is 3, 5, 7, etc.
Functions with side effects are discouraged, but there is nothing in ISO 7185 that forbids them. Therefore this feature violates the standard.
The solution
The problem occurs because the tricks used in executing from a flat encoded virtual machine do not always translate into trees. Instructions like dup effectively create side trees. The idea of separating out the side trees and encoding them separately is valid, but results in re-execution.
An answer is provided by the temps mechanism recently added to pgen. That system registers temporary values as locals to the current procedure or function by tracking them as a block and allocating them during routine setup. A duplicated tree can be removed from the current expression tree under processing, encoded first, and then the result of that tree are carried to the place the tree was clipped out of in a temp. This:
Avoids the duplication waste.
Solves the function reexecution issue.
Sets up for the optimization of common subexpression elimination.
Due to the way pgen currently works, functions with side effects can be reexecuted
The problem
Pgen handles the dup instruction and some others by duplicating the entire expression tree under the operator. This is functionally valid, but wasteful. The plan was to have it cleaned up by optimization phases. The problem is that functions, as well as operands, can be reexecuted by such a step. This will cause functions with side effects to be reexecuted:
Would result in a counting sequence in variable i. With a double execution, the count would happen in 2's, that is 3, 5, 7, etc.
Functions with side effects are discouraged, but there is nothing in ISO 7185 that forbids them. Therefore this feature violates the standard.
The solution
The problem occurs because the tricks used in executing from a flat encoded virtual machine do not always translate into trees. Instructions like dup effectively create side trees. The idea of separating out the side trees and encoding them separately is valid, but results in re-execution.
An answer is provided by the temps mechanism recently added to pgen. That system registers temporary values as locals to the current procedure or function by tracking them as a block and allocating them during routine setup. A duplicated tree can be removed from the current expression tree under processing, encoded first, and then the result of that tree are carried to the place the tree was clipped out of in a temp. This: