The ACK Pascal compiler is structured in such a way that all expressions are calculated at compile-time as far as possible. For expressions of type integer and type real the result of these parts of the expression are put in the constant_pool[] so that they can be loaded with the ldc or ldc_w instructions. For results of boolean expressions it is not necessary to put it in the constant_pool[] as these results can be loaded on the operand stack by the bipush instruction because the value can never exceed 255.
As an example of how the compiler calculates expressions in advance, take a look at the following expression:
Int1 := 1 + 2 + Three + Int2 + Four + Five;
The identifiers Int1 and Int2 are variables and the identifiers Three, Four and Five are constants with the respective values 3, 4 and 5. The compiler adds the first 3 elements of the expression as it already knows their values at compile-time. It stores the result of the addition of these three values, the value 6, in the constant_pool[]. It also adds the last two elements of the expression, the constants Four and Five, and stores the result in the constant_pool[]. This way the compiler only has to generate code for the addition of three values.
For every expression an expression tree is built, and the procedure in the compiler that generates the code for expressions starts at the root of the tree and generates the code in a depth-first manner. The expression tree has the operator with the highest precedence as root. When the code is generated this way, the instruction(s) performing the operation at the root of the tree will be generated after the code for all the subtrees is generated.
In the next subsections I will discuss how the code of expressions can be generated.