Docsity

Code Generation Issues and Target - Compiler Construction - Lecture Slides, Slides of Compiler Construction

Partial preview of the text

Download Code Generation Issues and Target - Compiler Construction - Lecture Slides and more Slides Compiler Construction in PDF only on Docsity!

Compiler

Construction

Lecture 42

2

Code Generation

 The code generation problem

is the task of mapping intermediate code to machine code.

4

Issues:

 Input language: intermediate

code (optimized or not)

 Target: machine code,

assembly language

5

Issues:

 Memory management: mapping names to data objects in the run-time system.

 Instruction Selection

 Instruction Scheduling

 Register allocation

7

Issues:

 Memory management: mapping names to data objects in the run-time system.

 Instruction Selection

 Instruction Scheduling

 Register allocation

8

Issues:

 Memory management: mapping names to data objects in the run-time system.

 Instruction Selection

 Instruction Scheduling

 Register allocation

10

Target: Assembly Language

Operations:

  • MOV source, destination
  • ADD source, destination
  • SUB source, destination (dest = dest – source)
  • GOTO address
  • CJ conditional jump

11

Addressing Modes

MODE FORM ADDRESS ADDED COST absolute M M 1 register R R 0 indexed c(R) c + contents(R) 1 indirect register *R contents(R) 0 indirect indexed *c(R) contents(c+contents(R)) 1 literal #c c 1 stack SP SP 0 indexed stack c(SP) c + contents(SP) 1

13

Simple Code Generation

 Define a target code sequence for each intermediate code statement type.

14

Simple Code Generation

Intermediate becomes… a = b MOV b,a

a = b[c] MOV addr(b),R ADD c, R MOV *R0,a

16

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

17

Problems

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

  • t1 := c[j] MOV addr(c), R a[i] = b[c[j]];
    • ADD j, R
    • MOV *R0, t
  • t2 := b[t1] MOV addr(b), R
    • ADD t1, R
    • MOV *R0, t
  • a[i] := t2 MOV address(a), R
    • ADD i, R
    • MOV t2, *R
  • The cost of this code is - MOV addr(c), R - ADD j, R - MOV addr(b), R - ADD *R0, R - MOV addr(a), R - ADD i, R - MOV *R1, *R

20

Can optimize further …

MOV addr(c), R ADD j, R MOV addr(a), R ADD i, R MOV *addr(b)(R0), *R

The cost of this code is 10.