Information Instructor cnliyingzju edu cn 13335880866 Teacher Assistant

  • Slides: 33
Download presentation

Information Instructor 李莹 cnliying@zju. edu. cn 13335880866 Teacher Assistant 孙煦雪 sunxuxue@zju. edu. cn 15858138880

Information Instructor 李莹 cnliying@zju. edu. cn 13335880866 Teacher Assistant 孙煦雪 sunxuxue@zju. edu. cn 15858138880

Text Books The Dragon Book: Compilers Principles, Techniques, & Tools, Second Edition, Alfred V.

Text Books The Dragon Book: Compilers Principles, Techniques, & Tools, Second Edition, Alfred V. Aho The Tiger Book: Modern Compiler Implementation in C, Andrew Appel

Grade Distribution attend class 10% Homework 10% Class test or Mid-Term Exam 15% Final

Grade Distribution attend class 10% Homework 10% Class test or Mid-Term Exam 15% Final during exam period: 65%

Course Structure Course has theoretical and practical aspects Compiling Techniques Compiler Design

Course Structure Course has theoretical and practical aspects Compiling Techniques Compiler Design

Compiler: why? 1940’s The first electronic computers Programming in machine language Slow , tedious

Compiler: why? 1940’s The first electronic computers Programming in machine language Slow , tedious and error prone 1954 IBM develops the 704 All programming done in assembly Software costs exceeded hardware costs!

Compiler: why? Emerge of higher-level languages with higher-level notations more easily programming Fortran Scientific

Compiler: why? Emerge of higher-level languages with higher-level notations more easily programming Fortran Scientific computation Cobol Business data processing Lisp Symbolic computation

A compiler Higher-level Language For human Compile r Machine Language For machine

A compiler Higher-level Language For human Compile r Machine Language For machine

Why Study Compilers 1. 2. 3. 4. 5. 6. Basic idea for constructing translators.

Why Study Compilers 1. 2. 3. 4. 5. 6. Basic idea for constructing translators. Principle and Techniques reusable in other CS domains. Influences hardware design, e. g. , RISC, VLIW. Tools (mostly “optimization”) for enhancing software reliability and security. Excellent software-engineering example --theory meets practice. Essential software tool. 9

A compiler is a program that can read a program in one language and

A compiler is a program that can read a program in one language and translate it into an equivalent program in another language Source Program Compiler Input Target Program Output

A Interpreter An interpreter directly execute the operations of source program on inputs Source

A Interpreter An interpreter directly execute the operations of source program on inputs Source program Input Interpreter Output

A language-processing system

A language-processing system

The structure Two parts of a compiler Analysis, front end Synthesis, back end operate

The structure Two parts of a compiler Analysis, front end Synthesis, back end operate as a sequence of phases

Lexical Analysis Reads stream of characters Group the characters into lexemes For each lexemes,

Lexical Analysis Reads stream of characters Group the characters into lexemes For each lexemes, output a Token <token-name, attribute-value>

Syntax Analysis Reads Token stream Create tree-like intermediate representation (syntax tree) The grammatical structure

Syntax Analysis Reads Token stream Create tree-like intermediate representation (syntax tree) The grammatical structure of the token stream

Semantic Analysis Uses syntax tree information in Symbol table understand the meaning of program

Semantic Analysis Uses syntax tree information in Symbol table understand the meaning of program Too hard for compilers only check Semantic consistency Type checking Type conversions (Coercions)

Intermediate Code Geneartion For portability and reuse Before generate target program Generate intermediate representations

Intermediate Code Geneartion For portability and reuse Before generate target program Generate intermediate representations first Three address code Code sequence One operator Three operands (max)

Code Optimization To improve the intermediate code Run faster Use less memory Consume less

Code Optimization To improve the intermediate code Run faster Use less memory Consume less power

Code Generation Takes an intermediate representation as input Maps it to target language Registers

Code Generation Takes an intermediate representation as input Maps it to target language Registers and memory allocation Translate IR instructions into machine instructions

Symbol-Table Management An essential function of a compiler Records variable names various attributes of

Symbol-Table Management An essential function of a compiler Records variable names various attributes of them

Passes Several phases may be grouped into a pass A pass reads an input

Passes Several phases may be grouped into a pass A pass reads an input file and writes an output file An example of a pass including: Lexical analysis, parsing, semantic analysis IR generation.

Compiler construction tools-1 Some tools could be used to help compiler construction Parser Generators.

Compiler construction tools-1 Some tools could be used to help compiler construction Parser Generators. automatically produce syntax analyzers from a grammatical description of a programming language. Yacc Scanner Generators. produce lexical analyzers from a regular-expression description of the tokens of a language. Lex Syntax-directed translation engines. produce collections of routines for walking a parse tree and generating intermediate code.

Compiler construction tools-2 Code-generators. produce a code generator from a collection of rules for

Compiler construction tools-2 Code-generators. produce a code generator from a collection of rules for translating each operation of the intermediate language into the machine language for a target machine. Data-flow analysis engines. facilitate the gathering of information about how values are transmitted from one part of a program to each other part. Compiler-construction toolkit. provide an integrated set of routines for constructing various phases of a compiler.

The move to Higher-level Languages First generation languages: machine languages. Second generation: assembly languages.

The move to Higher-level Languages First generation languages: machine languages. Second generation: assembly languages. Third generation: higher-level languages, Fortran, Cobol, Lisp, C…. Forth generation: languages designed for specific applications, NOMAD, SQL Fifth generation: logic- and constraint-based languages, Prolog and OPS 5

Another classification of languages Imperative how a computation is to be done C, C++,

Another classification of languages Imperative how a computation is to be done C, C++, C#, Java Declarative what computation is to be done ML, Haskell, Prolog Von neumann languages Object-oriented languages Scripting languages

Impacts on Compilers Can help promote the use of high-level languages Compiler writing is

Impacts on Compilers Can help promote the use of high-level languages Compiler writing is challenging. A compiler must translate correctly the potentially infinite set of programs

Application of Compiler Technology Implementation of higher-level programming languages Optimizations for computer architectures Design

Application of Compiler Technology Implementation of higher-level programming languages Optimizations for computer architectures Design of new computer architectures Program translations Software productivity tools

Optimizations for computer architecture Parallelism Multi-core, many-core VLIW (very long instruction word), issue multiple

Optimizations for computer architecture Parallelism Multi-core, many-core VLIW (very long instruction word), issue multiple operations in parallel Memory hierarchies Several levels of storage The closer, the faster Registers, CPU cache Main memory disks

Design of new computer architecture RISC Specialized architecture VLIW SIMD

Design of new computer architecture RISC Specialized architecture VLIW SIMD

Program translation Binary translation x 86 →Sparc x 86 →arm Hardware synthesis VHDL RTL

Program translation Binary translation x 86 →Sparc x 86 →arm Hardware synthesis VHDL RTL Database query interpreters Compiled simulation

Software Productivity Tools Type checking Bounds Checking Memory-Management tools Code generators

Software Productivity Tools Type checking Bounds Checking Memory-Management tools Code generators

Summary Language Processors Compiler Phases Machine and Assembly languages Modeling in Compiler Design Code

Summary Language Processors Compiler Phases Machine and Assembly languages Modeling in Compiler Design Code Optimization Higher-level Languages Compilers and Computer Architecture Software Productivity and Software Security Programming Language Basics: Scope Rules, Environments, Block Structure, Parameter Passing, Aliasing

The end of Lecture 01

The end of Lecture 01