280 likes | 377 Views
Chapter 8. Intermediate Code Basic Code Generation Techniques. Gang S. Liu College of Computer Science & Technology Harbin Engineering University. Introduction.
E N D
Chapter 8 Intermediate Code Basic Code Generation Techniques Gang S. Liu College of Computer Science & Technology Harbin Engineering University
Introduction • Final task of the compiler is to generate executable code for a target machine that is a representation of a semantics of the source code. • This is the most complex phase of a compiler. • It depends on detailed information about • the target architecture, • the structure of the runtime • OS • There is an attempt to optimize the speed and the size of the target code to take advantages of special features of the target machine (registers, addressing modes, pipelining, and cache memory) Samuel2005@126.com
Introduction (cont) • The code generation is typically broken into several steps, often including an abstract code called intermediate code. • Two popular forms are • Three address code • P-code Samuel2005@126.com
Intermediate Code • A data structure that represents the source program during translation is called an intermediate representation (IR). • An abstract syntax tree was used as the principal IR. • An abstract syntax tree does not resemble target code. • Example: control flow constructs. • A new form of IR is necessary. • Such intermediate representation that closely resembles target code is called intermediate code. Samuel2005@126.com
Form of Intermediate Code • Intermediate code is a linearization of the syntax tree. • Intermediate code • Can be very high level, representing operations almost as abstractly as the syntax tree or can closely resemble target code. • May use or not used detailed information about the target machine and runtime environment. Samuel2005@126.com
Use of Intermediate Code • Intermediate code is useful • For producing extremely efficient code • In making a compiler more easily retargetable (if intermediate code is relatively target independent). Source Language 1 Target Language 1 Intermediate Code Source Language 2 Target Language 2 Samuel2005@126.com
Three-Address Code • The most basic instruction of three address code x = y op z • The use of the address x differs from the addresses of y and z. • y and z can represent constants and literal values. Samuel2005@126.com
Example + 2*a+(b-3) * - 2 a b 3 t1=2*a t2=b-3 t3=t1+t2 t1=b-3 t2=2*a t3=t2+t1 Right-to-left linearization Left-to-right linearization Samuel2005@126.com
Three-Address Code (cont) • It is necessary to vary form of the three-address code to express all constructs (e.g. t2=-t1) • No standard form exists. Samuel2005@126.com
Implementation of Three-Address Code • Each three-address instruction is implemented as a record structure containing several fields. • The entire sequence is an array or a linked list . • The most common implementation requires four fields – quadruple • One for operation and three for addresses. • For instructions that need fewer number of addresses, one or more addresses fields is given null or “empty” values. Samuel2005@126.com
Factorial Program • { Sample program • in TINY language - • computes factorial } • read x; { input an integer } • if 0 < x then { don't compute if x <= 0 } • fact := 1; • repeat • fact := fact * x; • x := x - 1 • until x = 0; • write fact { output factorial of x } • end Samuel2005@126.com
Syntax Tree for Factorial Program Samuel2005@126.com
Example (rd, x, _, _) (gt, x, 0, t1) (if_f, t1, L1, _) (asn, 1, fact, _) (lab, L2, _, _) (mul, fact, x, t2) (asn, t2, fact, _) (sub, x, 1, t3) (asn, t3, x, _) (eq, x, 0, t4) (if_f, t4, L2, _) (wri, fact, _, _) (lab, L1, _, _) (halt, _, _, _) { Sample program in TINY language - computes factorial } read x; { input an integer } if 0 < x then { don't compute if x <= 0 } fact := 1; repeat fact := fact * x; x := x - 1 until x = 0; write fact { output factorial of x } end Samuel2005@126.com
Different Representation • Instructions themselves represent temporaries. • This reduces the number of address fields from three to two. • Such representation is called a triple. • Amount of space is reduced. • Major drawback: any movement becomes difficult for array representation. Samuel2005@126.com
Example (rd, x, _, _) (gt, x, 0, t1) (if_f, t1, L1, _) (asn, 1, fact, _) (lab, L2, _, _) (mul, fact, x, t2) (ans, t2, fact, _) (sub, x, 1, t3) (asn, t3, x, _) (eq, x, 0, t4) (if_f, t4, L2, _) (wri, fact, _, _) (lab, L1, _, _) (halt, _, _, _) (0) (rd, x, _) (1) (gt, x, 0) (2) (if_f, (1), (11)) (3) (asn, 1, fact) (4) (mul, fact, x) (5) (asn, (4), fact) (6) (sub, x, 1) (7) (asn, (6), x) (8) (eq, x, 0) (9) (if_f, (8), (4)) (10) (wri, fact, _) (11) (halt, _, _) Samuel2005@126.com
P-Code • Standard assembly language code produced by Pascal compilers in 1970/80. • Designed for hypothetical stack machine, called P-machine. • Interpreters were written for actual machines. • This made Pascal compilers easy portable. • Only interpreter must be rewritten for a new platform. • Modifications of P-code are used in a number of compilers, mostly for Pascal-like languages. Samuel2005@126.com
P-Machine • Consists of • A code memory • An unspecified data memory for named variables • A stack for temporary data • Registers needed to maintain the stack and support execution. Samuel2005@126.com
Example 1 2*a+(b-3) ldc 2 ; load constant 2 lod a ; load value of variable a mpi ; integer multiplication lod b ; load value of variable b ldc 3 ; load constant 3 sbi ; integer subtraction adi ; integer addition Samuel2005@126.com
Example 2 x:=y+1 lda x ; load address of x lod y ; load value of y ldc 1 ; load constant 1 adi ; add sto ; store top to address ; bellow top & pop both Samuel2005@126.com
Factorial Program lod fact ;load value of fact lod x ;load value of x mpi ;multiply sto ;store top to ;address of second & ;pop lda x ;load address of x lod x ;load a value of x ldc 1 ;load constant 1 Sbi ;subtract sto; lod x ldc 0 equ ;test for equality fjp L2 ;jump to L2 of false lod fact; wri lab L1 stp lda x ; load address of x rdi ; read an integer, store to ; address on top of the stack ; (& pop it) lod x ; load the value of x ldc 0 ; load constant 0 grt ;pop an compare top two ;values push the Boolean ;result fjp L1 ;pop Boolean value, ;jump to L1 if false lda fact ;load address of fact ldc 1 ;load constant 1 sto ;pop two values, storing ;the first to address ;represented by second lab L2 ; definition of label 2 lda fact ; load address of fact Samuel2005@126.com
P-Code and Three-Address Code • P-code • is closer to actual machine. • Instructions require fewer addresses. • “One-address” or “zero-address” • Less compact in terms of instructions. • Not “self-contained” • Instructions operate implicitly on a stack • All temporary values are on stack, no need for temporary names. Samuel2005@126.com
Generation of Target Code • Involves two standard techniques • Macro expansion • Replaces each intermediate code instruction with an equivalent sequence of target code instructions. • Static simulation • Straight-line simulation of the effects of the intermediate code and generating target code to match these effects. Samuel2005@126.com
Example exp → id = exp | aexp aexp → aexp + factor | factor factor → (exp) | num | id (x=x+3)+4 lda x lod x ldc 3 adi stn ldc 4 adi t1 = x+3 x = t1 t2 = t1+4 Samuel2005@126.com
Static Simulation lda x lod x ldc 3 adi stn ldc 4 adi top of stack 3 x address of x t1=x+3 top of stack t1 address of x Samuel2005@126.com
Static Simulation lda x lod x ldc 3 adi stn ldc 4 adi top of stack t1 address of x x=t1 top of stack t1 Samuel2005@126.com
Static Simulation lda x lod x ldc 3 adi stn ldc 4 adi t1 = x+3 x = t1 t2 = t1+4 top of stack 4 t2=t1+4 t1 top of stack t2 Samuel2005@126.com
Example exp → id = exp | aexp aexp → aexp + factor | factor factor → (exp) | num | id (x=x+3)+4 lda x lod x ldc 3 adi stn ldc 4 adi t1 = x+3 x = t1 t2 = t1+4 Samuel2005@126.com
Macro Expansion lda t1 lod x ldc 3 adi sto lda t1 lod x ldc 3 adi sto lda x lod t1 sto lda t2 lod t1 ldc 4 adi sto t1 = x+3 x = t1 t2 = t1+4 t1 = x+3 lda x lod t1 sto x = t1 lda t2 lod t1 ldc 4 adi sto t2 = t1+4 Samuel2005@126.com