You are on page 1of 37

BITS Pilani

BITS Pilani
Hyderabad Campus

Dr.Aruna Malapati Asst Professor Department of CSIS

BITS Pilani
Hyderabad Campus

COMPLIER CONSTRUCTION / COMPILER DESIGN (CS F363 / IS F342)

Todays Agenda
General course information Objectives Reasons to take this course Course Logistics

Course outline
Overview of compiler phases
CS C362 Second Semester 2013-14 BITS Pilani, Hyderabad Campus

General Course Information


Instructor: Dr. Aruna Malapati

Office: B226
Email: arunam@hyderabad.bits-pilani.ac.in Chamber Consultation : Monday 4:00-5:00pm

CS C362 Second Semester 2013-14

BITS Pilani, Hyderabad Campus

Objectives
The goal of this course is to give a working knowledge of the basic techniques used in the implementation of modern programming languages. The course is centered around a substantial programming project: implementing front end compiler for a realistic language. Students successfully completing this course will be able to apply the theory and methods learned during the course to design and implement optimizing compilers for most programming languages.
CS C362 Second Semester 2013-14 BITS Pilani, Hyderabad Campus

Reasons to Take this Course


To understand better

programming languages (principles & semantics)


computer architecture and machine code structure the relation between source programs and generated machine code To get a good balance of theory & practice

To complete a substantial programming project (a compiler


for a realistic language)
CS C362 Second Semester 2013-14 BITS Pilani, Hyderabad Campus

Course Motivation
Why build compilers? Why study Compiler Design / Construction?

Why attend class?

CS C362 Second Semester 2013-14

BITS Pilani, Hyderabad Campus

Books

CS C362 Second Semester 2013-14

BITS Pilani, Hyderabad Campus

Course Logistics

Grading Policy
5%

Quiz
Project

35%

20%

Test 1 Test 2 Compehensive Exam(PARTLY OPEN)

20% 20%

*Prescribed Text book, References and only Hand-Written Notes Permitted. Timeliness Assignments are to be completed in time with no postponements. Submissions 24 hours from deadline will have a penalty of 2 Marks per day.
CS C362 Second Semester 2013-14 BITS Pilani, Hyderabad Campus

Course Outline
Introduction to Compilers

Lexical Analysis
Syntax Analysis Semantic Analysis / Syntax Directed Translation Intermediate Code Generation Special aspects of compilers and runtime Code Optimization Code Generation
CS C362 Second Semester 2013-14 BITS Pilani, Hyderabad Campus

Why Compilers ?

#include<stdio.h> main() { printf(WELCOME TO COMPILER DESIGN); }

Translator

CS C362 Second Semester 2013-14

BITS Pilani, Hyderabad Campus

Isn't this a solved problem?

Changes in architecture influence changes in compilers

CS C362 Second Semester 2013-14

BITS Pilani, Hyderabad Campus

Interest
Compiler construction is a microcosm of computer science.
Artificial intelligence Greedy algorithms Learning algorithms Algorithms Theory Graph algorithms Union-find dynamic programming DFAs for scanning Parser generators Lattice theory For analysis Inside a compiler, all these things come together

Systems

Allocation and naming Locality Synchronization


Pipeline management Hierarchy management Instruction set use
CS C362 Second Semester 2013-14 BITS Pilani, Hyderabad Campus

Architecture

What is your experience with compilers ?


You have used several compilers. What qualities are important in a compiler?

CS C362 Second Semester 2013-14

BITS Pilani, Hyderabad Campus

Challenges
Building a compiler requires knowledge of

programming languages (parameter passing, variable


scoping, memory allocation, etc) theory (automata, context-free languages, etc) algorithms and data structures (hash tables, graph algorithms, dynamic programming, etc)

computer architecture (assembly programming)


software engineering
CS C362 Second Semester 2013-14 BITS Pilani, Hyderabad Campus

Abstract view
Source Code Compiler Errors

Target Code

Functions:

Recognize legal (and illegal) programs


Generate correct code Manage storage of all variables and Code agreement on format for object (or assembly) code
CS C362 Second Semester 2013-14 BITS Pilani, Hyderabad Campus

Compiler phases VS Hierarchical translations

CS C362 Second Semester 2013-14

BITS Pilani, Hyderabad Campus

Program Translation

Program in L1 Translation

Program in L2

L1 Source Language L2 Target Language

CS C362 Second Semester 2013-14

BITS Pilani, Hyderabad Campus

Compilation
Program in L1 Program in M2

Compilation
L1 Source Language M2 Target Language (machine lang. Or assembly lang.)

Note: A compiler may or may not include an assembler. End of Note.

The compiled program M2 can be executed (if it is in machine language) this duration is referred to as run-time
The compilation duration is referred to as compile-time.
CS C362 Second Semester 2013-14 BITS Pilani, Hyderabad Campus

Separate Compilation
Module 1 in L1 Compilation Module 1 in A2 Assembly Module 2 in A2 Compilation Assembly Linking L1 Source Language Module 1 in M3

Module 2 in L1

Module 2 in M3

A2 Assembly Language
M3 Machine Language

Execution

Program in M3

CS C362 Second Semester 2013-14

BITS Pilani, Hyderabad Campus

Compilation & Execution


Program parts in L1 Compile-Time Program parts in A2 Assembly-Time Link-Time Program parts in M3

Run-Time

Program in M3

L1 Source Language

A2 Assembly Language
M3 Machine Language

CS C362 Second Semester 2013-14

BITS Pilani, Hyderabad Campus

Interpretation vs. Compilation


Consider a C program Pc. Let t(Pc) be the time taken for executing Pc (i.e. interpreting Pc). Let comp(Pc) be the time taken for compiling Pc to Pm for some machine language m. Let t(Pm) be the time taken for executing Pm (on machine m) Fact: t(Pm) << t(Pc) Why Compile? comp(Pc) + N * t(Pm) < large N.

N * t(Pc) for sufficiently


BITS Pilani, Hyderabad Campus

CS C362 Second Semester 2013-14

Program Compilation
Program in IL Program in L1 Compilation L1 Source Language M2 Target Language (of a machine architecture) IL Intermediate Language (Intermediate Representation) Example: gcc compiles C programs to (say) 80x86 programs. It uses trees as IL. Program in M2

Note: Having an IL stage modularizes the compiler; improves testing of compiler. End of Note.
CS C362 Second Semester 2013-14 BITS Pilani, Hyderabad Campus

The Structure of a Modern Compiler (Phases)

CS C362 Second Semester 2013-14

BITS Pilani, Hyderabad Campus

The Structure of a Modern Compiler (front end)


A Lexer groups input characters into tokens. A parser recognizes sequences of tokens based on grammar and generates ASTs. Performs type checking and translates ASTs into IRs

CS C362 Second Semester 2013-14

BITS Pilani, Hyderabad Campus

The Structure of a Modern Compiler (Back end)

Optimizes IRs Generates machine code from assembly code Optimizes the code using control-flow and data-flow analyses, register allocation, etc
CS C362 Second Semester 2013-14 BITS Pilani, Hyderabad Campus

Source Program Analysis


Compiler cousins

CS C362 Second Semester 2013-14

BITS Pilani, Hyderabad Campus

Addressing Portability
Suppose you want to write compilers from m source languages to n computer platforms.

CS C362 Second Semester 2013-14

BITS Pilani, Hyderabad Campus

CS C362 Second Semester 2013-14

BITS Pilani, Hyderabad Campus

Virtual Machines
Sometimes IL is useful for other purposes:
It may be used as a target for many source languages; It may be used as a source for many target machines.

Example: Suns JVM (Java Virtual machine)


Used as an IL for Java Used as a source language for many target machines (Intel 80x86, Sun Sparc, etc.) Advantage: Java programs are architecture-independent.

Example: Microsofts .NET CLR (Common Language Runtime)


Used as an IL for many source languages Advantage: .NET applications are language-independent
CS C362 Second Semester 2013-14 BITS Pilani, Hyderabad Campus

Virtual Machines
Interpretation

Program in VM Program in L1 Compilation Program in M2 Compilation

Note: VM program can be interpreted or compiled.

L1 Source Language M2 Target Language (of a machine architecture) VM Virtual machine (a machine capable of understanding an Intermediate Language program)

CS C362 Second Semester 2013-14

BITS Pilani, Hyderabad Campus

Virtual Machines
Virtual machines save compiler development effort:

Given M different sources languages and N different


target machines, one would need M * N compilers. If a VM can be designed to accommodate all these languages, then one would need M compilers (from source languages to VM) and N interpreters or

compilers (VM to target machines)


i.e. M+N effort instead of M*N effort
CS C362 Second Semester 2013-14 BITS Pilani, Hyderabad Campus

Compilation
Program P coded in L1 Comp(L1,M2,M2) Program P coded in M2

Comp(X, Y, Z) is Compiler for X coded in Y and generating output in Z.

Program P coded in C

gcc(x86,x86) = Comp(C,x86,x86)

Program P coded in x86

CS C362 Second Semester 2013-14

BITS Pilani, Hyderabad Campus

Problem: C compiler for Sparc


Need a C compiler generating Sparc output, given gcc(x86,x86):
Write a new C compiler to generate Sparc : Code it in C (to avoid coding in x86) Feed this C program (I.e. the new C compiler) to gcc(x86, x86) as input:

C compiler for Sparc output coded in C

gcc(x86,x86) = Comp(C,x86,x86)

C compiler for Sparc output coded in x86

CS C362 Second Semester 2013-14

BITS Pilani, Hyderabad Campus

Cross Compilation / Retargeting


Program P coded in L1 Program P coded in M3

Comp(L1,M2,M3)

L1 Source Language M3 Target Language (of a machine architecture) M2 compilation platform

CS C362 Second Semester 2013-14

BITS Pilani, Hyderabad Campus

Bootstrapping
A compiler is characterized by three languages:
Source Language (s) Target Language (T) Implementation Language (I)

A Compiler may run on one machine and produce code for another machine. (Cross Compiler) Suppose we write a cross compiler in S in implementation language I to generate code for machine N i.e we create SI N.

CS C362 Second Semester 2013-14

BITS Pilani, Hyderabad Campus

Summary
Course related stuff(Logistics, Motivation , Overview) Why Compilers? Compiler phases

CS C362 Second Semester 2013-14

BITS Pilani, Hyderabad Campus

You might also like