CS 460/660 Compiler Construction. Class 01 2 Why Study Compilers? Compilers are important –...

19
CS 460/660 CS 460/660 Compiler Construction Compiler Construction

Transcript of CS 460/660 Compiler Construction. Class 01 2 Why Study Compilers? Compilers are important –...

Page 1: CS 460/660 Compiler Construction. Class 01 2 Why Study Compilers? Compilers are important – –Responsible for many aspects of system performance Compilers.

CS 460/660 CS 460/660 Compiler ConstructionCompiler Construction

Page 2: CS 460/660 Compiler Construction. Class 01 2 Why Study Compilers? Compilers are important – –Responsible for many aspects of system performance Compilers.

Class 01Class 01 22

Why Study Compilers?Why Study Compilers?

Compilers are important– Responsible for many aspects of system performance

Compilers are interesting– Compilers include many applications of theory to

practice– Writing a compiler exposes practical algorithmic &

engineering issues

Compilers are everywhere– Many practical applications have embedded

languages– Commands, macros, formatting tags …

Many applications have input formats that look like languages

Page 3: CS 460/660 Compiler Construction. Class 01 2 Why Study Compilers? Compilers are important – –Responsible for many aspects of system performance Compilers.

Class 01Class 01 33

Intrinsic MeritIntrinsic Merit

Compiler Construction poses some challenging and interesting problems:– Compilers must do a lot but also run quickly– Compilers have primary responsibility for run-time performance

Computer architects perpetually create new challenges for the compiler by building more complex machines – Compilers must hide that complexity from the programmer

A successful compiler requires mastery of the many complex interactions between its constituent parts

Page 4: CS 460/660 Compiler Construction. Class 01 2 Why Study Compilers? Compilers are important – –Responsible for many aspects of system performance Compilers.

Class 01Class 01 44

A True CapstoneA True Capstone

Compiler construction involves ideas from many different parts of computer science – Data Structures

Greedy algorithms, Heuristic search techniques

– AlgorithmsGraph algorithms, union-find Dynamic programming

– TheoryDFAs & PDAs, pattern matching, Fixed-point algorithms

– SystemsAllocation & naming, Synchronization, locality

– ArchitecturePipeline & hierarchy management, Instruction set use

Page 5: CS 460/660 Compiler Construction. Class 01 2 Why Study Compilers? Compilers are important – –Responsible for many aspects of system performance Compilers.

Class 01Class 01 55

Why does it matter today?Why does it matter today?

In the last 2 years, most processors have gone multicore– The era of clock-speed improvements seems to be

drawing to an endFaster clock speeds mean higher power (n2 effect)Smaller wires mean higher resistance for on-chip wires

– For the near term, performance improvement will come from placing multiple copies of the processor (core) on a single die

Classic programs, written in old languages, are not well suited to capitalize on this kind of multiprocessor parallelism

– Parallel programs require sophisticated compilers

Page 6: CS 460/660 Compiler Construction. Class 01 2 Why Study Compilers? Compilers are important – –Responsible for many aspects of system performance Compilers.

Class 01Class 01 66

TextbookTextbook

Compilers: Principles, Compilers: Principles, Techniques, and ToolsTechniques, and Tools– The Dragon BookThe Dragon Book

You should read it, You should read it, It is a good book It is a good book I will cover some, but not all.I will cover some, but not all.– Quizzes/Tests come from Quizzes/Tests come from

lectures and readingslectures and readings

Page 7: CS 460/660 Compiler Construction. Class 01 2 Why Study Compilers? Compilers are important – –Responsible for many aspects of system performance Compilers.

Class 01Class 01 77

The Goal of Chapter 1The Goal of Chapter 1

Introduce different forms of language Introduce different forms of language translatorstranslators

Give a high level overview of the structure Give a high level overview of the structure of a typical compilerof a typical compiler

Discuss trends in programming languages Discuss trends in programming languages and machine architecture that are shaping and machine architecture that are shaping compilers. compilers.

Page 8: CS 460/660 Compiler Construction. Class 01 2 Why Study Compilers? Compilers are important – –Responsible for many aspects of system performance Compilers.

Class 01Class 01 88

What is a Compiler?What is a Compiler?

Def:Def: CompilerCompiler -- -- – a program that translates a program written in a program that translates a program written in

a language like Pascal, C, PL/I, FORTRAN, or a language like Pascal, C, PL/I, FORTRAN, or COBOL into another language.COBOL into another language.

An important role of the compiler is to An important role of the compiler is to report any errors that it detects.report any errors that it detects.

Page 9: CS 460/660 Compiler Construction. Class 01 2 Why Study Compilers? Compilers are important – –Responsible for many aspects of system performance Compilers.

Class 01Class 01 99

What is an Interpreter?What is an Interpreter?

Def:Def: InterpreterInterpreter -- -- – Instead of producing a target program at Instead of producing a target program at

translation, it appears to directly execute the translation, it appears to directly execute the operations specified in the source program on operations specified in the source program on the inputs specified by the user.the inputs specified by the user.

Page 10: CS 460/660 Compiler Construction. Class 01 2 Why Study Compilers? Compilers are important – –Responsible for many aspects of system performance Compilers.

Class 01Class 01 1010

What are the Differences?What are the Differences?

Compiler is usually much faster.Compiler is usually much faster.

Interpreter usually gives better Interpreter usually gives better diagnostics.diagnostics.

Page 11: CS 460/660 Compiler Construction. Class 01 2 Why Study Compilers? Compilers are important – –Responsible for many aspects of system performance Compilers.

Class 01Class 01 1111

2. Structure of a Compiler2. Structure of a Compiler

So far we have So far we have viewed the viewed the compiler as a compiler as a single black box. single black box.

Page 12: CS 460/660 Compiler Construction. Class 01 2 Why Study Compilers? Compilers are important – –Responsible for many aspects of system performance Compilers.

Class 01Class 01 1212

2. Structure of a Compiler2. Structure of a Compiler

Now we want to Now we want to open that box.open that box.

Page 13: CS 460/660 Compiler Construction. Class 01 2 Why Study Compilers? Compilers are important – –Responsible for many aspects of system performance Compilers.

Class 01Class 01 1313

Page 14: CS 460/660 Compiler Construction. Class 01 2 Why Study Compilers? Compilers are important – –Responsible for many aspects of system performance Compilers.

Class 01Class 01 1414

2. Structure of a Compiler2. Structure of a Compiler

1.1. The Lexical Analyzer reads the stream The Lexical Analyzer reads the stream of characters and groups them into of characters and groups them into meaningful sequences called lexemes.meaningful sequences called lexemes.

2.2. The Syntax Analyzer depicts the The Syntax Analyzer depicts the grammatical structure of the lexemes in grammatical structure of the lexemes in a syntax treea syntax tree

3.3. The Semantic Analyzer checks the tree The Semantic Analyzer checks the tree for semantic consistency (type for semantic consistency (type checking)checking)

Page 15: CS 460/660 Compiler Construction. Class 01 2 Why Study Compilers? Compilers are important – –Responsible for many aspects of system performance Compilers.

Class 01Class 01 1515

2. Structure of a Compiler2. Structure of a Compiler

4.4. Intermediate Code Generation – Intermediate Code Generation – produces assembly like instructions produces assembly like instructions (three address code) (three address code)

5.5. Code Optimization – seeks to improve Code Optimization – seeks to improve intermediate code so better target code intermediate code so better target code can be generatedcan be generated

6.6. Code Generation – takes the Code Generation – takes the intermediate code and maps to the intermediate code and maps to the target code.target code.

Page 16: CS 460/660 Compiler Construction. Class 01 2 Why Study Compilers? Compilers are important – –Responsible for many aspects of system performance Compilers.

Class 01Class 01 1616

2. Structure of a Compiler2. Structure of a Compiler

7.7. Symbol Table Management - An Symbol Table Management - An essential function of the compiler is to essential function of the compiler is to record the variable names used in the record the variable names used in the source program and collect attributes source program and collect attributes of each name. (type, scope,…)of each name. (type, scope,…)

Page 17: CS 460/660 Compiler Construction. Class 01 2 Why Study Compilers? Compilers are important – –Responsible for many aspects of system performance Compilers.

Class 01Class 01 1717

2.8 The Grouping of the Phases 2.8 The Grouping of the Phases into Passesinto Passes

Pass – several phases may be grouped Pass – several phases may be grouped together into a pass that reads an input file together into a pass that reads an input file and writes an output file.and writes an output file.– Front End Front End

Lexical Analysis, Syntax Analysis, Semantic Lexical Analysis, Syntax Analysis, Semantic Analysis, and Intermediate Code GenerationAnalysis, and Intermediate Code Generation

– Code Optimization Code Optimization Might be an optional pass.Might be an optional pass.

– Back EndBack EndCode Generation for a specific target machine.Code Generation for a specific target machine.

Page 18: CS 460/660 Compiler Construction. Class 01 2 Why Study Compilers? Compilers are important – –Responsible for many aspects of system performance Compilers.

Class 01Class 01 1818

2.9 Compiler-Construction Tools2.9 Compiler-Construction Tools

The compiler writer, like any software The compiler writer, like any software developer, can profitably use modern developer, can profitably use modern software development environments software development environments containing toolscontaining tools– Editor, debugger, version managers, profilers, Editor, debugger, version managers, profilers,

test harnesses, …test harnesses, …

There are also tools to help implement There are also tools to help implement various phases of a compiler.various phases of a compiler.– Scanner Generator, Parser generator, …Scanner Generator, Parser generator, …

Page 19: CS 460/660 Compiler Construction. Class 01 2 Why Study Compilers? Compilers are important – –Responsible for many aspects of system performance Compilers.

Class 01Class 01 1919