Presentation is loading. Please wait.

Presentation is loading. Please wait.

Compiler Construction Sohail Aslam Lecture 42. 2 Code Generation  The code generation problem is the task of mapping intermediate code to machine code.

Similar presentations


Presentation on theme: "Compiler Construction Sohail Aslam Lecture 42. 2 Code Generation  The code generation problem is the task of mapping intermediate code to machine code."— Presentation transcript:

1 Compiler Construction Sohail Aslam Lecture 42

2 2 Code Generation  The code generation problem is the task of mapping intermediate code to machine code.

3 3 Code Generation Requirements:  Correctness  Efficiency

4 4 Issues:Issues:  Input language: intermediate code (optimized or not)  Target: machine code, assembly language

5 5 Issues:Issues:  Memory management: mapping names to data objects in the run-time system.  Instruction Selection  Instruction Scheduling  Register allocation

6 6 Issues:Issues:  Memory management: mapping names to data objects in the run-time system.  Instruction Selection  Instruction Scheduling  Register allocation

7 7 Issues:Issues:  Memory management: mapping names to data objects in the run-time system.  Instruction Selection  Instruction Scheduling  Register allocation

8 8 Issues:Issues:  Memory management: mapping names to data objects in the run-time system.  Instruction Selection  Instruction Scheduling  Register allocation

9 9 Target: Assembly Language General Characteristics  Byte-addressable with 4-byte words  N general -purpose registers  Two-address instructions: op source, destination

10 10 Target: Assembly Language Operations: MOV source, destination ADD source, destination SUB source, destination (dest = dest – source) GOTO address CJ conditional jump

11 11 Addressing Modes MODEFORMADDRESSADDED COST absoluteMM1 registerRR0 indexedc(R)c + contents(R)1 indirect register*Rcontents(R)0 indirect indexed*c(R)contents(c+contents(R))1 literal#cc1 stackSP 0 indexed stackc(SP)c + contents(SP)1

12 12 Target: Assembly Language Instruction costs: The cost corresponds to length of instruction MOV R0,R1 ; R0 = c(R1) has cost 1 MOV R5,M ; M = c(R5) has cost 2: 1 for instruction, 1 additional for memory address

13 13 Simple Code Generation  Define a target code sequence for each intermediate code statement type.

14 14 Simple Code Generation Intermediatebecomes… a = bMOV b,a a = b[c]MOV addr(b),R0 ADD c, R0 MOV *R0,a

15 15 Simple Code Generation Intermediatebecomes… a = b + cMOV b,a ADD c,a a[b] = cMOV addr(a),R0 ADD b,R0 MOV c,*R0

16 16  Consider the C statement: a[i] = b[c[j]]; t1 := c[j] MOV addr(c), R0 ADD j, R0 MOV *R0, t1 t2 := b[t1] MOV addr(b), R0 ADD t1, R0 MOV *R0, t2 a[i] := t2 MOV address(a), R0 ADD i, R0 MOV t2, *R0  The cost of this code is 18 and we are forced to allocate space for two temporaries.

17 17 ProblemsProblems  Local decisions do not produce good code.  Do not take temporary variables into account

18 18 OptimizeOptimize  Get rid of the temporaries: MOV addr(c), R0 ADD j, R0 MOV addr(b), R1 ADD *R0, R1 MOV addr(a), R2 ADD i, R2 MOV *R1, *R2 The cost of this code is 12.

19 19 a[i] = b[c[j]]; t1 := c[j] MOV addr(c), R0 ADD j, R0 MOV *R0, t1 t2 := b[t1] MOV addr(b), R0 ADD t1, R0 MOV *R0, t2 a[i] := t2 MOV address(a), R0 ADD i, R0 MOV t2, *R0 The cost of this code is 18 MOV addr(c), R0 ADD j, R0 MOV addr(b), R1 ADD *R0, R1 MOV addr(a), R2 ADD i, R2 MOV *R1, *R2 The cost of this code is 12.

20 20 Can optimize further … MOV addr(c), R0 ADD j, R0 MOV addr(a), R2 ADD i, R2 MOV *addr(b)(R0), *R2 The cost of this code is 10.

21 21 Can optimize further …  What is needed is a way to generate machine code based on past and future use of the data.


Download ppt "Compiler Construction Sohail Aslam Lecture 42. 2 Code Generation  The code generation problem is the task of mapping intermediate code to machine code."

Similar presentations


Ads by Google