- Assignment Statements
A tree pattern of the form:
generates a Move instruction:
where aPlace represents the register, stack position or
memory location assigned to a.
- Arithmetic Operations
Suppose Op represents an arithmetic operation and
consider an abstract syntax tree for the statement
t = a Op b:
One possible code sequence is:
This is the method used in the example of the previous section. Of
course, some machines require that special registers be used for operations
such as multiplication and division.
Example 2
- IF Statements
IF statements can be represented by an abstract syntax tree such as:
A typical code sequence is:
Example 3 illustrates this
for the statement:
IF a < b THEN Max = b ELSE Max = a;
In Example 3, aPlace and bPlace mey refer to the variables
a and b themselves, if the machine allows two memory
operands. For machines such as the 86-family (PC-clones) which require that
one operand be in a register, the instruction:
can be replaced by:
MOVE Reg,aPlace
CMP Reg,bPlace
if neither a nor b have been previously assigned
to a register.
- Loops
Loops are just conditionals whose code is repeated. Consider the loop:
LOOP While condition DO
Statements
ENDLOOP
An abstract syntax tree is:
A reasonable code sequence is:
Example 4
shows such a loop: