Lecture1 introduction compilers

16
Compilers CS 346 3 – 0 – 0 – 6 Monday – Tuesday – Wednesday 9.00 --- 9.55 10.00 --- 10.55 11.00 --- 11.55 Arijit Sur Office: H-103 Department of Computer Science and Engineering Email: [email protected] Ph: 2361 Instructors: Dr. A. Sur and Dr. A. Sarkar

Transcript of Lecture1 introduction compilers

Page 1: Lecture1 introduction compilers

CompilersCS 346

3 – 0 – 0 – 6

Monday – Tuesday – Wednesday9.00 --- 9.55 10.00 --- 10.55 11.00 --- 11.55

Arijit Sur

Office: H-103

Department of Computer Science and Engineering

Email: [email protected]

Ph: 2361

Instructors: Dr. A. Sur and Dr. A. Sarkar

Page 2: Lecture1 introduction compilers

Syllabus• Overview of different phases of a compiler: front-end; back-end;

• Lexical analysis: specification of tokens, recognition of tokens, input buffering, automatic tools;

• Syntax analysis: context free grammars, top down and bottom up parsing techniques, construction of efficient parsers, syntax-directed translation, automatic tools;

• Semantic analysis: declaration processing, type checking, symbol tables, • Semantic analysis: declaration processing, type checking, symbol tables, error recovery;

• Intermediate code generation: run-time environments, translation of language constructs;

• Code generation: flow-graphs, register allocation, code-generation algorithms;

• Introduction to code optimization techniques.

8-Jan-15 CS346 Lecture 1 2

Page 3: Lecture1 introduction compilers

Books

• Text Book

– Alfred V. Aho, Monica S. Lam, Ravi Sethi, Jeffrey D. Ullman, Compilers: Principles, Techniques, and Tools, 2nd Edition, Prentice Hall, 2009

• Reference Books

– V. Raghavan, Principles of Compiler Design, McGrawHill, 2010

– C.N. Fischer, R.J. Le Blanc, Crafting a Compiler with C, Pearson Education, 2009

– K. D. Cooper, L. Torczon, Engineering a Compiler, Morgan Kaufmann Publishers, 2004

– Allen Holub, Compiler Design in C, Prentice-Hall software series, 1990

8-Jan-15 CS346 Lecture 1 3

Page 4: Lecture1 introduction compilers

Grades

• 2 Quiz s– 20% of grade

• Mid-Semester – 30% of grade

• End-Semester – 50% of grade

• Attendance below 75% --- F Grade

• Random Attendance

8-Jan-15 CS346 Lecture 1 4

Page 5: Lecture1 introduction compilers
Page 6: Lecture1 introduction compilers

8-Jan-15 CS346 Lecture 1 6

Page 7: Lecture1 introduction compilers

Basic Blocks of Computing System

8-Jan-15 CS346 Lecture 1 7

Page 8: Lecture1 introduction compilers

Program Execution: Layered Architecture

High Level Language Program

Assembly Language Program

Compiler

Assembler +

temp = v[k];

v[k] = v[k+1];

v[k+1] = temp;

lw $15, 0($2)

lw $16, 4($2)

sw $16, 0($2)

sw $15, 4($2)

Machine Language Program

Control Signal Specification

Assembler + Linker + Loader

Machine Interpretation

sw $15, 4($2)

0000 1001 1100 0110 1010 1111 0101 1000

1010 1111 0101 1000 0000 1001 1100 0110

1100 0110 1010 1111 0101 1000 0000 1001

0101 1000 0000 1001 1100 0110 1010 1111

°

°

ALUOP[0:3] <= InstReg[9:11] & MASK

8-Jan-15 CS346 Lecture 1 8

Page 9: Lecture1 introduction compilers

Architecture of Computing System

Compiler

Operating

System

Application

Instruction SetArchitecture

Firmware

I/O systemInstr. Set Proc.

Digital Design

Circuit Design

Architecture

Datapath & Control

Layout

8-Jan-15 CS346 Lecture 1 9

Page 10: Lecture1 introduction compilers

Definitions

• What is a compiler?– A program that accepts as input a program text in a certain language and

produces as output a program text in another language, while preserving themeaning of that text (Grune et al, 2000).

– A program that reads a program written in one language (source language)and translates it into an equivalent program in another language (targetlanguage) (Aho et al)

– Report errors of source language which are detected during the

8-Jan-15 CS346 Lecture 1 10

– Report errors of source language which are detected during thetranslation process

• What is an interpreter?– A program that reads a source program and produces the results of executing

this source.

• We deal with compilers! Many of these issues arise with interpreters!

Page 11: Lecture1 introduction compilers

Compiler Vs. Interpreter

Compiler

Source Program

Interpreter

Source Program

Input

Output

Target Program

Target ProgramInput Output

Translator

Source Program

Intermediate

Program

Input

Virtual

Machine Output

A Hybrid Compiler

Page 12: Lecture1 introduction compilers

Examples

• C is typically compiled

• Lisp is typically interpreted

• Java is compiled to bytecodes, which are then interpreted

• source-to-source compiler, transcompiler, or transpiler :– C++ to C

– High Performance Fortran (HPF) to Fortran (parallelising compiler)

8-Jan-15 12

– High Performance Fortran (HPF) to Fortran (parallelising compiler)

• Cross Compilers (wiki)– A cross compiler is a compiler capable of

reading executable code for a platform other than the one onwhich the compiler is running. For example, a compiler that runson a Windows 7 PC but generates code that runson Android Smartphone is a cross compiler.

CS346 Lecture 1

Page 13: Lecture1 introduction compilers

Qualities of a Good Compiler

What qualities would you want in a compiler?

– generates correct code (first and foremost!)

– generates fast code

– conforms to the specifications of the input language

– copes with essentially arbitrary input size, variables,

8-Jan-15 13

– copes with essentially arbitrary input size, variables, etc.

– compilation time (linearly)proportional to size of source

– good diagnostics

– consistent optimisations

– works well with the debugger

CS346 Lecture 1

Page 14: Lecture1 introduction compilers

Principles of Compilation

The compiler must:

• preserve the meaning of the program being compiled.

• “improve” the source code in some way.

Other issues (depending on the setting):

8-Jan-15 14

Other issues (depending on the setting):

• Speed (of compiled code)

• Space (size of compiled code)

• Feedback (information provided to the user)

• Debugging (transformations obscure the relationship source code vs target)

• Compilation time efficiency (fast or slow compiler?)

CS346 Lecture 1

Page 15: Lecture1 introduction compilers

Applications

• Most common use: translate a high-level program to object code– Program Translation: binary translation, hardware synthesis, …

• Optimizations for computer architectures:– Improve program performance, take into account hardware parallelism, etc…

• Interpreters: e.g., BASIC, Lisp etc.

8-Jan-15 15

• Software productivity tools– Debugging aids: e.g., purify

• Text formatters, just-in-time compilation for Java, power management, global distributed computing, …

Key: Ability to extract properties of a source program (analysis) and transform it to construct a target program (synthesis)

CS346 Lecture 1

Page 16: Lecture1 introduction compilers