14.3. Statements and Control Flow:
Naive Approach
(Dragon Book, Section 6.6)

The following actions generate code for statements and boolean expressions in a simple but naive way.

Production and action
S → id = E
  gen(id.name = E.addr);
S → { SL }
  Do nothing
SL → SL1 S
  Do nothing
SL → ε
  Do nothing
S → if ( E )
  L1 = newLabel();
  L2 = newLabel();
  gen(ifFalse E.addr goto L1);
S1 else
  gen(goto L2);
  gen(label L1);
S2
  gen(label L2);
S → if ( E )
  L1 = newLabel();
  gen(ifFalse E.addr goto L1);
S1
  gen(label L1);
S → while
  L1 = newLabel();
  L2 = newLabel();
  gen(label L1)
( E )
  gen(ifFalse E.addr goto L2); S1
  gen(goto L1);
  gen(label L2);
E → E1 == E2
  E.addr = newTemporary();
  L1 = newLabel();
  L2 = newLabel();
  gen(if E1.addr = E2.addr goto L1);
  gen(E.addr = 0);
  gen(goto L2);
  gen(label L1);
  gen(E.addr = 1);
  gen(label L2);
E → E1 < E2
  E.addr = newTemporary();
  L1 = newLabel();
  L2 = newLabel();
  gen(if E1.addr < E2.addr goto L1);
  gen(E.addr = 0);
  gen(goto L2);
  gen(label L1);
  gen(E.addr = 1);
  gen(label L2);

Sample code generation for statements

Let's use the above rules to translate

  count = 0;
  while(x < y) {
    count = 2*count;
    x = x + 1;
  }
into intermediate code. Here is the result.

t1 = 0               // count = 0
count = t1
label L1             // start while loop
if x < y goto L2     // x < y
t2 = 0
goto L3
label L2
t2 = 1
label L3
ifFalse t2 goto L4
t3 = 2               // count = 2*count
count = t3 * count
t4 = 1               // x = x + 1
x = x + t4
goto L1              // end of while loop
label L4

Notice that there are a lot more lines than necessary. A better translation would be

count = 0
label L1
if x ≥ y goto L2
count = 2 * count
x = x + 1
goto L1 
label L2