Generating Code for REAL Control StructuresTopAnouncementsGenerating Code for expressions used as conditions (cont.)

Generating Code for expressions used as conditions (cont.)

  1. gen-cond-expr must be able to generate code for any expression, including simple arithmetic operations. The easiest way to do this is to count on the genexpr function we discussed in the last class to calculate the value of the expression and then compare it to 0.

    gencondarithmetic(node *expr, int sense, codelabel *target)
    {  oprndesc *valdesc;
    
          valdesc = genexpr(expr)
          output "CMP #0,valdesc"
          if ( sense )   
             output "BNE target"
          else  
             output "BEQ target"
    }
    

  2. Genexpr can use a similar trick to generate code for logical and relational operators. That is, genexpr will call gen-cond-expr for such operators.

Computer Science 434
Department of Computer Science
Williams College

Generating Code for REAL Control StructuresTopAnouncementsGenerating Code for expressions used as conditions (cont.)