Lecture 23 Chapter 8 Intermediate Code Generation Intermediate













- Slides: 13
Lecture # 23 Chapter 8: Intermediate Code Generation
Intermediate Code Generation • Facilitates retargeting: enables attaching a back end for the new machine to an existing front end Front end Intermediate code Back end Target machine code • Enables machine-independent code optimization 2
Intermediate Representations • Graphical representations (e. g. AST) • Postfix notation: operations on values stored on operand stack (similar to JVM bytecode) • Three-address code: (e. g. triples and quads) x : = y op z • Two-address code: x : = op y which is the same as x : = x op y 3
Syntax-Directed Translation of Abstract Syntax Trees Production S id : = E E E 1 + E 2 E E 1 * E 2 E - E 1 E ( E 1 ) E id Semantic Rule S. nptr : = mknode(‘: =’, mkleaf(id, id. entry), E. nptr) E. nptr : = mknode(‘+’, E 1. nptr, E 2. nptr) E. nptr : = mknode(‘*’, E 1. nptr, E 2. nptr) E. nptr : = mknode(‘uminus’, E 1. nptr) E. nptr : = E 1. nptr E. nptr : = mkleaf(id, id. entry) 4
Abstract Syntax Trees E. nptr a * (b + c) E. nptr a * E. nptr ( E. nptr b ) + E. nptr c * Pro: Cons: easy restructuring of code and/or expressions for intermediate code optimization memory intensive a + b c 5
Abstract Syntax Trees versus DAGs a : = b * -c + b * -c : = a : = + a * b + * uminus b c * uminus b uminus c Tree c DAG 6
Postfix Notation a : = b * -c + b * -c a b c uminus * + assign Postfix notation represents operations on a stack Pro: Cons: easy to generate stack operations are more difficult to optimize Bytecode (for example) iload 2 iload 3 ineg imul iadd istore 1 // // // push b push c uminus * + store 7 a
Three-Address Code a : = b * -c + b * -c t 1 t 2 t 3 t 4 t 5 a : = : = : = - c b * t 1 - c b * t 3 t 2 + t 4 t 5 Linearized representation of a syntax tree t 1 t 2 t 5 a : = : = - c b * t 1 t 2 + t 2 t 5 Linearized representation of a syntax DAG 8
Three-Address Statements • • Assignment statements: x : = y op z, x : = op y Indexed assignments: x : = y[i], x[i] : = y Pointer assignments: x : = &y, x : = *y, *x : = y Copy statements: x : = y Unconditional jumps: goto lab Conditional jumps: if x relop y goto lab Function calls: param x… call p, n return y 9
Syntax-Directed Translation into Three -Address Code Productions S id : = E | while E do S E E+E |E*E |-E |(E) | id | num Synthesized attributes: S. code three-address code for S S. begin label to start of S or nil S. after label to end of S or nil E. code three-address code for E E. place a name holding the value of E gen(E. place ‘: =’ E 1. place ‘+’ E 2. place) Code generation t 3 : = t 1 + t 2 10
Syntax-Directed Translation into Three -Address Code (cont’d) Productions S id : = E S while E do S 1 E E 1 + E 2 E E 1 * E 2 E - E 1 E ( E 1 ) E id E num Semantic rules S. code : = E. code || gen(id. place ‘: =’ E. place); S. begin : = S. after : = nil (see next slide) E. place : = newtemp(); E. code : = E 1. code || E 2. code || gen(E. place ‘: =’ E 1. place ‘+’ E 2. place) E. place : = newtemp(); E. code : = E 1. code || E 2. code || gen(E. place ‘: =’ E 1. place ‘*’ E 2. place) E. place : = newtemp(); E. code : = E 1. code || gen(E. place ‘: =’ ‘uminus’ E 1. place) E. place : = E 1. place E. code : = E 1. code E. place : = id. name E. code : = ‘’ E. place : = newtemp(); 11 E. code : = gen(E. place ‘: =’ num. value)
Syntax-Directed Translation into Three -Address Code (cont’d) Production S while E do S 1 S. begin: E. code if E. place = 0 goto S. after S. code Semantic rule goto S. begin : = newlabel() S. after: … S. code : = gen(S. begin ‘: ’) || E. code || gen(‘if’ E. place ‘=‘ ‘ 0’ ‘goto’ S. after) || S 1. code || gen(‘goto’ S. begin) || gen(S. after ‘: ’) 12
Example i : = 2 * n + k while i do i : = i - k t 1 : = 2 t 2 : = t 1 * n t 3 : = t 2 + k i : = t 3 L 1: if i = 0 goto L 2 t 4 : = i - k i : = t 4 goto L 1 L 2: 13