170 likes | 193 Views
This article explores the generation of intermediate code for MIPS assembly by creating a custom IR package, including quadruples and examples like memory access and function calls. Learn about stack frames and Single Static Assignment (SSA) for efficient code optimization and debugging.
E N D
Intermediate-Code Generation Jin Tianxing 5110309085
Purpose • Theoretically, we can directly generate the MIPS assemble code after semantic analysis. • Intermediate-code give us opportunities to register allocation, code optimization and easiness when debugging. • After this, the input code and AST can be thrown away.
Where is the data? • There are only two possible place containing data: and . • That means the intermediate-code get data only with and • For simplicity, we assume the number of is unlimited.
Quadruple • A quadruple consists of an , which can possibly be null. • You should design your own IR package. • Here is a viable example.
Examples • while () ; • ; • for (;;);
Example for jump stmt • Maintain stacks of and of loops. • break • Jump to the peek of • continue • Jump to the peek of
What to do when calling a function? • Save in memory. • Change , • (jump and link) to the of the function. • When from a function: • address(value) stores in a . • (jump register) • Restore from memory.
Single Static Assignment • It is a intuitive approach in intermediate-code generation. • One can be only assigned once.
Example • Non-SSA Version: • always represents (not store in memory) • SSA Version( is stored in memory):