Contents    Page-10    Prev    Next    Page+10    Index   

IF Statement Generation

Code for an intermediate code statement of the form (if c p1 p2) can be generated as follows:

  1. Generate code for the condition c using the arithmetic expression code generator. Note that a cmp instruction should be generated for all comparison operators, regardless of which comparison is used.

  2. Generate the appropriate jump-on-condition instruction, denoted jmp c below, by table lookup depending on the comparison operator.

    genarith(c)    # always generates cmp
    jmp c   .L1    # jump if c true = then
    genc(p2)             # "else"
    jmp     .L2          # jump to end
.L1:
    genc(p1)             # "then"
.L2:                     # end

The following jump table can be used:

op = < >
c je jne jl jle jge jg
-c jne je jge jg jl jle