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 – unsplash.com Manas Thakur CS502: Compiler Design 2
Dedication To the curiosity in all of us that naturally leads to innovation Manas Thakur CS502: Compiler Design 3
Evolution It all started with communication! Manas Thakur CS502: Compiler Design 4
Language explosion How many languages do you know? Manas Thakur CS502: Compiler Design 5
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. Manas Thakur CS502: Compiler Design 6
Programming languages ● Facilitate communication between humans and computers ● Is the communication one way? – How does a computer communicate to you? Manas Thakur CS502: Compiler Design 7
Why so many of them? ● 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” Source: codingdojo.com Manas Thakur CS502: Compiler Design 8
Translators for programming languages ● What if all your assignments were to be written in binary? Source program ● Which ones do you know of? – Preprocessor – Compiler Translator – Interpreter – Assembler Object program Manas Thakur CS502: Compiler Design 9
Translation of C Programs Source program (fjle.c) Preprocessor (cpp) Preprocessor (cpp) ● cpp fjle.c > fjle.i Modifjed source program (fjle.i) ● gcc -S fjle.i ● as fjle.s -o fjle.o Compiler (gcc) Compiler (gcc) ● ld -o a.out fjle.o ... libraries ... Target assembly program (fjle.s) Assembler (as) Assembler (as) Relocatable machine code (fjle.o) library fjles, Linker / Loader (ld) Linker / Loader (ld) relocatable object fjles Target machine code (a.out) Manas Thakur CS502: Compiler Design 10
Compilers – A Sangam of Theory and Practice Theory of Computation Programming Algorithms languages Compilers Machine Software learning? Engineering Computer Architecture This course offers a healthy mix of multiple areas of computer science and engineering! Manas Thakur CS502: Compiler Design 11
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 Manas Thakur CS502: Compiler Design 12
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 Manas Thakur CS502: Compiler Design 13
Evaluation ● Theory (50%) All assignments are individual ● – Quiz (20 marks) Plagiarism will not be tolerated ● (read about MOSS from Stanford ) – End sem (30* marks) No copying of code ● No “just saw my friend’s code” ● Assignments (50%) ● No code sharing on GitHub et al. ● – A1 (10 marks) Grading based on testcases ● (public, private, contributed) – A2 (8 marks) – A3 (12 marks) Deadlines at 11:55 pm ● – A4 (10 marks) – 25% less per late day – A5 (10* marks) * A5 marks can optionally be added to end-sem. Manas Thakur CS502: Compiler Design 14
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!) Manas Thakur CS502: Compiler Design 15
You will get PCs! ● Points for answering tricky questions and bringing out insightful observations: – Sometimes during the class Participation Credits – Sometimes on Teams ● Redemption policy: – Deadline extensions – Coffees after you come back – More interesting ideas as they come! Manas Thakur CS502: Compiler Design 16
Homework Find out the difgerences between a compiler and an interpreter. Manas Thakur CS502: Compiler Design 17
CS502: Compiler Design Introduction (Cont.) Manas Thakur Fall 2020
Recap: Translators ● Typical PL translators: Source program – Interpreter – Compiler Translator ● Name some languages that are – interpreted ● JavaScript, Shellscript, Perl, ... – compiled Target program ● C, C++, Pascal, Rust, ... ● What about Java? Manas Thakur CS502: Compiler Design 19
Compilers vs Interpreters Image source: https://stackoverflow.com/a/31551282 Manas Thakur CS502: Compiler Design 20
Compilers vs Interpreters: Demo Manas Thakur CS502: Compiler Design 21
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. Manas Thakur CS502: Compiler Design 22
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. मैः पी�ताबेः , लेपी�न मैः अ�ने �त्ऱ मीमला है पी� अमी�� से अमी�� मीमला है। Manas Thakur CS502: Compiler Design 23
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? Manas Thakur CS502: Compiler Design 24
Compilers work with strings ● Letters Characters – ‘a’, ‘2’, ‘=’, ‘;’ ● Words Tokens – “a”, “int”, “while”, “foo” ● Sentences Instructions – “int a = 2;” ● Documents Programs Manas Thakur CS502: Compiler Design 25
How many phases should we have? Source program Source program Analyze Analyze Compiler source code Compiler source code Generate Generate Target program target code target code Source program Target program Analyze Analyze source code source code Improve Improve code code Generate Generate target code target code Target program Manas Thakur CS502: Compiler Design 26
Typical stages in a compiler Character stream Machine-Independent Machine-Independent Lexical Analyzer Lexical Analyzer Code Optimizer Code Optimizer B a c k e n d Intermediate representation Token stream F r o n t e n d Syntax Analyzer Code Generator Syntax Analyzer Code Generator Syntax tree Target machine code Machine-Dependent Machine-Dependent Semantic Analyzer Semantic Analyzer Code Optimizer Code Optimizer Syntax tree Target machine code Intermediate Intermediate Symbol Code Generator Code Generator Table Intermediate representation Manas Thakur CS502: Compiler Design 27
Typical stages in a compiler z = x + y * 32 Intermediate Symbol Table Intermediate Lexical Analyzer Lexical Analyzer Code Generator Code Generator 1 z ... <id,1> <=> <id,2> <+> <id,3> <*> <32> t1 = id3 * 32 x ... 2 t2 = inttofloat(t1) y ... 3 Syntax Analyzer t3 = id2 + t2 Syntax Analyzer id1 = t3 = + Machine-Independent Machine-Independent <id,1> * Code Optimizer Code Optimizer <id,2> 32 <id,3> t1 = id3 * 32.0 LD R1, id3 id1 = id2 + t1 Semantic Analyzer Semantic Analyzer SHL R1, #5 LD R2, id2 Code Generator ADD R1, R1, R2 Code Generator = + ST id1, R1 <id,1> inttofloat LD R1, id3 <id,2> MUL R1, R1, #32.0 Machine-Dependent Machine-Dependent * LD R2, id2 Code Optimizer Code Optimizer ADD R1, R1, R2 <id,3> 32 ST id1, R1 Manas Thakur CS502: Compiler Design 28
CS502: Compiler Design Introduction (Cont.) Manas Thakur Fall 2020
Ideal vs Reality Ideal World ARM Fortran C x86 One Compiler One Compiler Java PowerPC Future Future Target Language Reality ARM f90 Fortran f90 gcc C x86 gcc javac Java PowerPC javac futurec Future futurec ? Future Language Target Manas Thakur CS502: Compiler Design 30
Recommend
More recommend