title: Compiler design- code generation for expressions with multiple operands
body: <p>I am in the middle of coding up a compiler for a C like language. </p>
  
  <p>I have gotten past syntax and semantic checking and I am beginning the code generation phase.</p>
  
  <p>The final code that I have to generate has to be in 3 address load/store architecture. I'm allowed to assume an unbounded set of registers and a 32 M memory space for stack and system memory.</p>
  
  <p>Right now, when I start generating the code, I begin by assuming a really large int array <code>int R[2000000]</code> to denote the register set. And as and when I encounter a variable declaration (through the parser/semantic analyzer) I allocate a register for that particular variable.</p>
  
  <p>Now in the course of the program when I encounter this variable again, I retrieve it back from that register number. I save the register number of every variable in the symbol table.</p>
  
  <p>My question now is this: Suppose, we have a statement like this -</p>
  
  <pre><code>a := b + c + e / f *h;
  </code></pre>
  
  <p>And I have saved a,b,c,e,f,h in R1, R2, R3, R4, R5, R6 respectively, the final generated code would be (assuming that the next available regs start from R7...)</p>
  
  <pre><code>R9 = R5 * R6;
  R8 = R4 / R9;
  R7 = R3 + R8;
  R1 = R2 + R7;
  </code></pre>
  
  <p>What is the approach to "remember" what was the previous register and the operation? </p>
  
  <p>If this is not the correct way of doing it, can anyone please give me some pointers on how to implement it? </p>
  
  <p>Any suggestion would be great. Thanks</p>