title: Code generation for if statements
body: <p>I am in the middle of writing the code generation section of a compiler for a simple (C-like) language. I am having a problem with "if" statements. I would like to generate corresponding jump instructions for each part of the condition, like GCC would do. The structure which holds the structure of boolean expressions is an Abstract Syntax Tree.</p>
  
  <p>So far, I wrote a function who would generate corresponding labels for comparisons and also for logical and and logical negation. However, I have a huge problem when trying to make this function work for logical or as well.</p>
  
  <p>For a simple statement, such as</p>
  
  <pre><code>if (25 &lt; 19) then
    print 99
  statements
  </code></pre>
  
  <p>I would like my code to look like this:</p>
  
  <pre><code>cmp 25, 19
  jge label0
  label1:
  print 99
  label0:
  statements
  </code></pre>
  
  <p>(and my function does display something similar for this simple example).</p>
  
  <p>When handling a negation, you just have to negate the tests for these conditions (transform jge into jl above). For logical and, you just generate the corresponding code for the left and right conjuncts sequentially, in a straightforward manner. But for OR, I have reached a number of challenging problems. Instead of jumping to the statements after the if (or the next else branch) if the condition <i>fails</i>, as for all the other cases, I would have to jump to the actual place where the code resides if either of the OR's operands hold. Then when you have two ORs, the situation gets even worse, as the AST represents ORs using binary nodes and the function who <del>attempts to translate</del> translates to our Intermediate Representation of choice uses this AST.</p>
  
  <p>Could anyone please offer their help on this issue? I have also tried consulting 'Engineering a Compiler', but I haven't managed to find out anything useful from there.</p>