Intermediate Code Generating machineindependent intermediate form Decouple backend
• Intermediate Code • Generating machine-independent intermediate form. – Decouple backend from frontend, facilitate retargeting – Machine independent code optimizer can be applied here. • Position of intermediate code generator: parser Static semantic analysis Machine-specific Code optimization Target code generation Intermediate Code generation Machine-independent Code optimization
• Intermediate languages, many kinds for different purposes • High-level representation for source to source translation to keep the program structure: – Abstract syntax tree • Low-level representation for compiling for target machine. – An intermediate form that is close to low level machine language. – Three address code (more later) » gcc uses RTL, a variation of the three address code. • Other commonly used intermediate language – Control flow graph, Program dependence graph (PDG), DAG (direct acyclic graph)
• Three address code: • A sequence of statement of the form x: =y op z • Example: a: =b*-c + b * -c t 1 : = -c t 2 : = b * t 1 t 3 : = -c t 4 : = b * t 3 t 5 = t 2 + t 4 a = t 5 t 1 : = -c t 2 : = b * t 1 t 3 = t 2 + t 2 a = t 3 • Three address statements are close to the assembly statements (OP src 1 src 2 dst)
• Some three-address statements that will be used later: – Assignment statements: • With a binary operation: • With a unary operation: • With no operation(copy) : x : = y op z x: = op y x : = y – Branch statements • Unconditional jump: • Conditional jumps: goto L if x relop y goto L – Statement for procedure calls • Param x, set a parameter for a procedure call • Call p, n call procedure p with n parameters • Return y return from a procedure with return value y
– Example: instructions for procedure call: p(x 1, x 2, x 3, …, xn): param x 1 param x 2 … param xn call p, n – Indexed assignments: • x : = y[i] and x[i] : = y – Address and pointer assignments • x : = &y, x : = *y
- Slides: 5