introduction to compiler - skkuarcs.skku.edu/pmwiki/uploads/courses/compilers/01-intro.pdf ·...

17
Compiler Design Introduction to Compiler Hwansoo Han

Upload: others

Post on 20-Jun-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to Compiler - SKKUarcs.skku.edu/pmwiki/uploads/Courses/Compilers/01-Intro.pdf · 2011-08-31 · Traditional Two-pass Compiler High level functions Recognize legal program,

Compiler Design

Introduction to Compiler

Hwansoo Han

Page 2: Introduction to Compiler - SKKUarcs.skku.edu/pmwiki/uploads/Courses/Compilers/01-Intro.pdf · 2011-08-31 · Traditional Two-pass Compiler High level functions Recognize legal program,

Traditional Two-pass Compiler

High level functions

Recognize legal program, generate correct code (OS & linker can accept)

Manage the storage of all variables and code

Two passes

Use an intermediate representation (IR)

Front end maps legal source code into IR - O(n) or O(n log n)

Back end maps IR into target machine code - typically NP-complete

Admits multiple front ends & multiple passes (better code)

Sourcecode

Front

End

Errors

Machinecode

Back

End

IR

2

Page 3: Introduction to Compiler - SKKUarcs.skku.edu/pmwiki/uploads/Courses/Compilers/01-Intro.pdf · 2011-08-31 · Traditional Two-pass Compiler High level functions Recognize legal program,

Responsibilities

Recognize legal (& illegal) programs

Report errors in a useful way

Produce IR & preliminary storage map

Shape the code for the back end

Much of front end construction can be automated

Front End

Sourcecode

ScannerIR

Parser

Errors

tokens

3

Page 4: Introduction to Compiler - SKKUarcs.skku.edu/pmwiki/uploads/Courses/Compilers/01-Intro.pdf · 2011-08-31 · Traditional Two-pass Compiler High level functions Recognize legal program,

Front End - Scanner

Scanner

Maps character stream into words (basic units of syntax)

Produces tokens — a word & its part of speech x = x + y ; becomes <id,x> = <id,x> + <id,y> ;

word lexeme, part of speech token type

Typical tokens include number, identifier, +, –, new, while, if Scanner eliminates white space

Produced by automatic scanner generator

Sourcecode

ScannerIR

Parser

Errors

tokens

4

Page 5: Introduction to Compiler - SKKUarcs.skku.edu/pmwiki/uploads/Courses/Compilers/01-Intro.pdf · 2011-08-31 · Traditional Two-pass Compiler High level functions Recognize legal program,

Front End - Parser

Parser

Recognizes context-free syntax

Guides context-sensitive (―semantic‖) analysis E.g. type checking

Builds IR for source program

Produced by automatic parser generators

Sourcecode

ScannerIR

Parser

Errors

tokens

5

Page 6: Introduction to Compiler - SKKUarcs.skku.edu/pmwiki/uploads/Courses/Compilers/01-Intro.pdf · 2011-08-31 · Traditional Two-pass Compiler High level functions Recognize legal program,

Context-free syntax can be put to better use

This grammar defines simple expressions with addition & subtraction over ―number‖ and ―id‖

This grammar, like many, falls in a class called ―context-free grammars‖, abbreviated CFG

Front End – example (1)

1. goal expr

2. expr expr op term

3. | term

4. term number

5. | id

6. op +

7. | -

S = goal

T = { number, id, +, - }

N = { goal, expr, term, op }

P = { 1, 2, 3, 4, 5, 6, 7}

6

Page 7: Introduction to Compiler - SKKUarcs.skku.edu/pmwiki/uploads/Courses/Compilers/01-Intro.pdf · 2011-08-31 · Traditional Two-pass Compiler High level functions Recognize legal program,

Front End – example (2)

A parse can be represented by a tree (parse tree or syntax tree)

x + 2 - y

This contains a lot of

unneeded information.

term

op termexpr

termexpr

goal

expr

op

<id,x>

<number,2>

<id,y>

+

-

1. goal expr

2. expr expr op term

3. | term

4. term number

5. | id

6. op +

7. | -

7

Page 8: Introduction to Compiler - SKKUarcs.skku.edu/pmwiki/uploads/Courses/Compilers/01-Intro.pdf · 2011-08-31 · Traditional Two-pass Compiler High level functions Recognize legal program,

Front End – example (3)

Compilers often use an abstract syntax tree (AST)

This is much more concise

ASTs are one kind of intermediate representation (IR)

+

-

<id,x> <number,2>

<id,y>The AST summarizes

grammatical structure,

without including detail

about the derivation

8

Page 9: Introduction to Compiler - SKKUarcs.skku.edu/pmwiki/uploads/Courses/Compilers/01-Intro.pdf · 2011-08-31 · Traditional Two-pass Compiler High level functions Recognize legal program,

Back End

Responsibilities

Translate IR into target machine code

Choose instructions to implement each IR operation

Decide which values to keep in registers

Find optimal order of instruction execution

Ensure conformance with system interfaces

Automation has been less successful in the back end

Errors

IR Instruction

Scheduling

Instruction

Selection

Machinecode

Register

Allocation

IR IR

9

Page 10: Introduction to Compiler - SKKUarcs.skku.edu/pmwiki/uploads/Courses/Compilers/01-Intro.pdf · 2011-08-31 · Traditional Two-pass Compiler High level functions Recognize legal program,

Back End – Instruction selection

Instruction Selection

Produce fast, compact code

Take advantage of target features such as addressing modes

Usually viewed as a pattern matching problem

ad hoc methods, pattern matching, dynamic programming

Errors

IR Instruction

Scheduling

Instruction

Selection

Machinecode

Register

Allocation

IR IR

10

Page 11: Introduction to Compiler - SKKUarcs.skku.edu/pmwiki/uploads/Courses/Compilers/01-Intro.pdf · 2011-08-31 · Traditional Two-pass Compiler High level functions Recognize legal program,

Back End – Instruction scheduling

Instruction Scheduling

Avoid hardware stalls and interlocks

Use all functional units productively

Can increase lifetime of variables (changing the allocation)

Optimal scheduling is NP-Complete in nearly all cases

Heuristic techniques are well developed

Errors

IR Instruction

Selection

Machinecode

Instruction

Scheduling

IR IR Register

Allocation

11

Page 12: Introduction to Compiler - SKKUarcs.skku.edu/pmwiki/uploads/Courses/Compilers/01-Intro.pdf · 2011-08-31 · Traditional Two-pass Compiler High level functions Recognize legal program,

Back End – Register allocation

Register Allocation

Have each value in a register when it is used

Manage a limited set of resources

Can change instruction choices & insert LOADs & STOREs

Optimal allocation is NP-Complete

Compilers approximate solutions to NP-Complete problems

Errors

IR Register

AllocationInstruction

Selection

Machinecode

Instruction

Scheduling

IR IR

12

Page 13: Introduction to Compiler - SKKUarcs.skku.edu/pmwiki/uploads/Courses/Compilers/01-Intro.pdf · 2011-08-31 · Traditional Two-pass Compiler High level functions Recognize legal program,

Optimizing Compiler

Code Optimizations

Analyzes IR and rewrites (or transforms) IR

Primary goal is to reduce

Execution time,

Space usage,

Power consumption, …

Must preserve ―meaning‖ of the code

Errors

SourceCode

Middle

End

Front

End

Machinecode

Back

End

IR IR

13

Page 14: Introduction to Compiler - SKKUarcs.skku.edu/pmwiki/uploads/Courses/Compilers/01-Intro.pdf · 2011-08-31 · Traditional Two-pass Compiler High level functions Recognize legal program,

Instruction Selection Example

Simple Treewalk for initial code

Peephole matching for desired code

x

IDENT<a,ARP,4>

IDENT<b,ARP,8>

loadI 4 r5loadAO r0,r5 r6loadI 8 r7loadAO r0,r7 r8mult r6,r8 r9

loadAI r0,4 r5loadAI r0,8 r6mult r5,r6 r7

Tree Treewalk Code Desired Code

14

Page 15: Introduction to Compiler - SKKUarcs.skku.edu/pmwiki/uploads/Courses/Compilers/01-Intro.pdf · 2011-08-31 · Traditional Two-pass Compiler High level functions Recognize legal program,

Instruction Scheduling Example

a

b c

d

The Precedence GraphThe Code

a: loadAI r0,@w r1

b: add r1,r1 r1

c: loadAI r0,@x r2

d: mult r1,r2 r1

Schedule Instructions considering

Latency

Dependences

Generate fast executing code

15

Page 16: Introduction to Compiler - SKKUarcs.skku.edu/pmwiki/uploads/Courses/Compilers/01-Intro.pdf · 2011-08-31 · Traditional Two-pass Compiler High level functions Recognize legal program,

Register Allocation Example

Instruction selection assume infinite number of registers (virtual registers)

Mapping virtual registers to physical registers

Sometime need register spill/fill code

loadI 4 r1loadAO r0,r1 r2loadI 8 r3loadAO r0,r3 r4mult r2,r4 r5

6 virtual registers

loadI 4 r1loadAO r0,r1 r1loadI 8 r2loadAO r0,r2 r2mult r1,r2 r2

3 physical registers

(only r5 is used later) (r5 is renamed with r2)

16

Page 17: Introduction to Compiler - SKKUarcs.skku.edu/pmwiki/uploads/Courses/Compilers/01-Intro.pdf · 2011-08-31 · Traditional Two-pass Compiler High level functions Recognize legal program,

Summary

Front End

Process high-level programming language

Middle End

Apply optimization for speed, power, space, …

Back End

Produce machine-level assembly code (or binary code)

17