11.4 Standard Code Generation Strategies

A code generator can be written to recognize standard "templates":

  1. 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.

  2. 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

  3. 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:

    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:

    if neither a nor b have been previously assigned to a register.

  4. 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:

    Send questions and comments to: Karen Lemone