I am in the middle of compiler coding for a C-like language.
I got past syntax and semantic validation, and I'm starting the code generation phase.
The final code that I have to generate should be in a 3 address loading / storage architecture. I can accept an unlimited set of registers and 32 M memory for the stack and system memory.
Right now, when I start generating code, I start with the assumption that a really large array of int int R[2000000]denotes a set of registers. And when and when I come across a variable declaration (through an analyzer / semantic analyzer), I allocate case for this particular variable.
Now that the program encounters this variable again, I am returning it from this register. I save the register number of each variable in a character table.
Now my question is this: suppose we have such an expression -
a := b + c + e / f *h;
And I saved a, b, c, e, f, h in R1, R2, R3, R4, R5, R6 respectively, the final generated code will be (assuming the following available regs start with R7 ...)
R9 = R5 * R6;
R8 = R4 / R9;
R7 = R3 + R8;
R1 = R2 + R7;
What is the approach to "remembering" what was in the previous register and operation?
If this is not the right way to do this, can someone please give me some guidance on how to implement it?
Any suggestion would be great. Thanks