CS502: Compiler Design Introduction and Logistics Manas Thakur - - PowerPoint PPT Presentation

cs502 compiler design introduction and logistics manas
SMART_READER_LITE
LIVE PREVIEW

CS502: Compiler Design Introduction and Logistics Manas Thakur - - PowerPoint PPT Presentation

CS502: Compiler Design Introduction and Logistics Manas Thakur Fall 2020 Credits Content V. Krishna Nandivada (IIT Madras) Rupesh Nasre (IIT Madras) Yannis Smaragdakis (University of Athens) Media images.google.com


slide-1
SLIDE 1

Manas Thakur

Fall 2020

CS502: Compiler Design Introduction and Logistics

slide-2
SLIDE 2

Manas Thakur CS502: Compiler Design 2

Credits

  • Content

– V. Krishna Nandivada (IIT Madras) – Rupesh Nasre (IIT Madras) – Yannis Smaragdakis (University of Athens)

  • Media

– images.google.com – unsplash.com

slide-3
SLIDE 3

Manas Thakur CS502: Compiler Design 3

Dedication

To the curiosity in all of us that naturally leads to innovation

slide-4
SLIDE 4

Manas Thakur CS502: Compiler Design 4

Evolution

It all started with communication!

slide-5
SLIDE 5

Manas Thakur CS502: Compiler Design 5

Language explosion

How many languages do you know?

slide-6
SLIDE 6

Manas Thakur CS502: Compiler Design 6

Why need a translator?

  • Communication is a

precursor to evolution.

  • Unfortunately, not

everyone understands the other person’s language(s).

  • A translator is needed to

facilitate correct communication.

slide-7
SLIDE 7

Manas Thakur CS502: Compiler Design 7

Programming languages

  • Facilitate communication between humans and computers
  • Is the communication one way?

– How does a computer communicate to you?

slide-8
SLIDE 8

Manas Thakur CS502: Compiler Design 8

Why so many of them?

Source: codingdojo.com

  • Task-specific

– HTML for web pages – LaTeX for document formatting – Scripting for automation – Makefile’s language? – Google’s language??

  • Educational

– We will use some in this course.

  • For fun!

– Google (or DDG if you prefer)

“esoteric programming languages”

slide-9
SLIDE 9

Manas Thakur CS502: Compiler Design 9

Translators for programming languages

  • What if all your assignments were to be written in binary?
  • Which ones do you know of?

– Preprocessor – Compiler – Interpreter – Assembler

Translator

Source program Object program

slide-10
SLIDE 10

Manas Thakur CS502: Compiler Design 10

Translation of C Programs

Preprocessor (cpp) Preprocessor (cpp) Compiler (gcc) Compiler (gcc) Assembler (as) Assembler (as) Linker / Loader (ld) Linker / Loader (ld) Source program (fjle.c) Modifjed source program (fjle.i) Target assembly program (fjle.s) Relocatable machine code (fjle.o) Target machine code (a.out) library fjles, relocatable

  • bject fjles
  • cpp fjle.c > fjle.i
  • gcc -S fjle.i
  • as fjle.s -o fjle.o
  • ld -o a.out fjle.o ...libraries...
slide-11
SLIDE 11

Manas Thakur CS502: Compiler Design 11

Compilers – A Sangam of Theory and Practice

Compilers

This course offers a healthy mix of multiple areas of computer science and engineering!

Theory of Computation Programming languages Algorithms Software Engineering Computer Architecture Machine learning?

slide-12
SLIDE 12

Manas Thakur CS502: Compiler Design 12

Learnings as part of this course

  • The working of a compiler

– Obviously!

  • See the applications of several CS areas

– Theory is indeed useful – Practice is also required

  • Work with (slightly) large software systems

– Design is important – Improve programming skills

  • Learn to think about tradeoffs

– Precision vs efficiency – Difficult to always get a win-win

slide-13
SLIDE 13

Manas Thakur CS502: Compiler Design 13

Logistics

  • Classes:

– Where: Zoom (at least for now) – When: F Slot (Mon 3pm, Tue 4pm, Thu 9am, Fri 10am, Sat 11am)

  • Resource locators:

– Course web page (deadlines, slides, other relevant material)

  • https://manas.gitlab.io/courses/compilers/fall20/

– Moodle (assignment specs, submissions, marks)

  • Get yourself enrolled

– Microsoft Teams (discussions, activities, fun)

  • Registered students will get a link today
slide-14
SLIDE 14

Manas Thakur CS502: Compiler Design 14

Evaluation

  • Theory (50%)

– Quiz (20 marks) – End sem (30* marks)

  • Assignments (50%)

– A1 (10 marks) – A2 (8 marks) – A3 (12 marks) – A4 (10 marks) – A5 (10* marks)

* A5 marks can optionally be added to end-sem.

  • All assignments are individual
  • Plagiarism will not be tolerated

(read about MOSS from Stanford)

  • No copying of code
  • No “just saw my friend’s code”
  • No code sharing on GitHub et al.
  • Grading based on testcases

(public, private, contributed)

  • Deadlines at 11:55 pm

– 25% less per late day

slide-15
SLIDE 15

Manas Thakur CS502: Compiler Design 15

Your friends for assignments

  • Language:

– Java – Won’t be difficult to catch up if you know C/C++ instead

  • Tools (tutorials will be given):

– Java Tree Builder (JTB) – JavaCC – Using an IDE (such as Eclipse or IntelliJIdea) for code completion

and debugging would help tremendously

  • Instructions regarding format and naming (automated!)
slide-16
SLIDE 16

Manas Thakur CS502: Compiler Design 16

You will get PCs!

  • Points for answering tricky questions and bringing out insightful
  • bservations:

– Sometimes during the class – Sometimes on Teams

  • Redemption policy:

– Deadline extensions – Coffees after you come back – More interesting ideas as they come!

Participation Credits

slide-17
SLIDE 17

Manas Thakur CS502: Compiler Design 17

Homework

Find out the difgerences between a compiler and an interpreter.

slide-18
SLIDE 18

Manas Thakur

Fall 2020

CS502: Compiler Design Introduction (Cont.)

slide-19
SLIDE 19

Manas Thakur CS502: Compiler Design 19

Recap: Translators

  • Typical PL translators:

– Interpreter – Compiler

  • Name some languages that are

– interpreted

  • JavaScript, Shellscript, Perl, ...

– compiled

  • C, C++, Pascal, Rust, ...
  • What about Java?

Translator

Source program Target program

slide-20
SLIDE 20

Manas Thakur CS502: Compiler Design 20

Compilers vs Interpreters

Image source: https://stackoverflow.com/a/31551282

slide-21
SLIDE 21

Manas Thakur CS502: Compiler Design 21

Compilers vs Interpreters: Demo

slide-22
SLIDE 22

Manas Thakur CS502: Compiler Design 22

Implications of the differences between an interpreter and a compiler

  • Compilers get a lot of time to translate input programs!
  • What do they do in this time?
  • That’s what we would learn in this course :-)
  • Let’s now find out what they must do.
slide-23
SLIDE 23

Manas Thakur CS502: Compiler Design 23

Duties of a compiler

  • Translate: input language, output language
  • Maintain correctness

पीताजी अजमेर गए | Father died today.

  • Be efficient

– Why are you laughing? – I understood yesterday's joke.

  • Generate a good language

– I got books but more than that I got your letter.

मैः पीताबेः, लेपीन मैः अने त्ऱ मीमला है पी अमी से अमी मीमला है।

slide-24
SLIDE 24

Manas Thakur CS502: Compiler Design 24

Additional expectations from a compiler

  • Improve code

– Need for speed – My computer is cleaner than my almirah – Save energy, save environment

  • Help in programming

– Find bugs – Suggest/perform refactorings

  • Integrate with the development ecosystem

– Did you install Eclipse/IntelliJ? – How do IDEs differ from command prompt?

slide-25
SLIDE 25

Manas Thakur CS502: Compiler Design 25

Compilers work with strings

  • Letters Characters

– ‘a’, ‘2’, ‘=’, ‘;’

  • Words Tokens

– “a”, “int”, “while”, “foo”

  • Sentences Instructions

– “int a = 2;”

  • Documents Programs
slide-26
SLIDE 26

Manas Thakur CS502: Compiler Design 26

How many phases should we have?

Compiler Compiler Source program Target program Analyze source code Analyze source code Source program Target program Generate target code Generate target code Analyze source code Analyze source code Source program Target program Improve code Improve code Generate target code Generate target code

slide-27
SLIDE 27

Manas Thakur CS502: Compiler Design 27

Typical stages in a compiler

Lexical Analyzer Lexical Analyzer Syntax Analyzer Syntax Analyzer Semantic Analyzer Semantic Analyzer Intermediate Code Generator Intermediate Code Generator Character stream Token stream Syntax tree Syntax tree Intermediate representation Machine-Independent Code Optimizer Machine-Independent Code Optimizer Code Generator Code Generator Target machine code Intermediate representation Machine-Dependent Code Optimizer Machine-Dependent Code Optimizer Target machine code Symbol Table

F r o n t e n d B a c k e n d

slide-28
SLIDE 28

Manas Thakur CS502: Compiler Design 28

Typical stages in a compiler

Lexical Analyzer Lexical Analyzer Syntax Analyzer Syntax Analyzer Semantic Analyzer Semantic Analyzer Code Generator Code Generator z = x + y * 32 <id,1> <=> <id,2> <+> <id,3> <*> <32> Intermediate Code Generator Intermediate Code Generator Machine-Independent Code Optimizer Machine-Independent Code Optimizer

t1 = id3 * 32 t2 = inttofloat(t1) t3 = id2 + t2 id1 = t3

Machine-Dependent Code Optimizer Machine-Dependent Code Optimizer <id,1> <id,2> <id,3> 32 * + = <id,1> <id,2> <id,3> * inttofloat + = 32

t1 = id3 * 32.0 id1 = id2 + t1 LD R1, id3 MUL R1, R1, #32.0 LD R2, id2 ADD R1, R1, R2 ST id1, R1 LD R1, id3 SHL R1, #5 LD R2, id2 ADD R1, R1, R2 ST id1, R1

z ... x ... y ... 1 2 3 Symbol Table

slide-29
SLIDE 29

Manas Thakur

Fall 2020

CS502: Compiler Design Introduction (Cont.)

slide-30
SLIDE 30

Manas Thakur CS502: Compiler Design 30

Ideal vs Reality

One Compiler One Compiler Fortran C Java Future Language ARM x86 PowerPC Future Target

Ideal World

gcc gcc f90 f90 Fortran C Java Future Language ARM x86 PowerPC Future Target javac javac futurec futurec ?

Reality

slide-31
SLIDE 31

Manas Thakur CS502: Compiler Design 31

Our staged model offers a middle ground

  • Can we build n*m compilers with n+m components?
  • Example: Scala and Java are both translated to Bytecode and

use the same virtual machine.

  • A rough statement:

– Most problems in the front-end are polynomial. – Most problems in the back-end are NP-complete.

  • Consequently:

– Front-end needs patience – Back-end makes us impatient

Source code Target code Intermediate code Front end Back end