1 Introduction to Compilation Cheng-Chia Chen. 2 What is a compiler? l a program that translates an executable program in one language into an executable.

Slides:



Advertisements
Similar presentations
Lecture 01 - Introduction Eran Yahav 1. 2 Who? Eran Yahav Taub 734 Tel: Monday 13:30-14:30
Advertisements

Reference Book: Modern Compiler Design by Grune, Bal, Jacobs and Langendoen Wiley 2000.
Prof. Hilfinger CS 164 Lecture 11 Introduction to Programming Languages and Compilers CS164 11:00-12:00 MWF 10 Evans Notes by G. Necula, with additions.
From Cooper & Torczon1 Implications Must recognize legal (and illegal) programs Must generate correct code Must manage storage of all variables (and code)
BİL744 Derleyici Gerçekleştirimi (Compiler Design)1.
CPSC 388 – Compiler Design and Construction Lecture: MWF 11:00am-12:20pm, Room 106 Colton.
1 How are Languages Implemented? Two major strategies: –Interpreters (older, less studied) –Compilers (newer, more extensively studied) Interpreters run.
Invitation to Computer Science 5th Edition
INTRODUCTION TO COMPUTING CHAPTER NO. 06. Compilers and Language Translation Introduction The Compilation Process Phase 1 – Lexical Analysis Phase 2 –
COP4020 Programming Languages
Chapter 1. Introduction.
Chapter 1 Introduction Dr. Frank Lee. 1.1 Why Study Compiler? To write more efficient code in a high-level language To provide solid foundation in parsing.
Chapter 10: Compilers and Language Translation Invitation to Computer Science, Java Version, Third Edition.
Java Programming Introduction & Concepts. Introduction to Java Developed at Sun Microsystems by James Gosling in 1991 Object Oriented Free Compiled and.
CSC 338: Compiler design and implementation
Compiler course 1. Introduction. Outline Scope of the course Disciplines involved in it Abstract view for a compiler Front-end and back-end tasks Modules.
CST320 - Lec 11 Why study compilers? n n Ties lots of things you know together: –Theory (finite automata, grammars) –Data structures –Modularization –Utilization.
1 COMP 3438 – Part II-Lecture 1: Overview of Compiler Design Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
Lexical Analysis I Specifying Tokens Lecture 2 CS 4318/5531 Spring 2010 Apan Qasem Texas State University *some slides adopted from Cooper and Torczon.
D. M. Akbar Hussain: Department of Software & Media Technology 1 Compiler is tool: which translate notations from one system to another, usually from source.
Unit-1 Introduction Prepared by: Prof. Harish I Rathod
1.  10% Assignments/ class participation  10% Pop Quizzes  05% Attendance  25% Mid Term  50% Final Term 2.
Compilation in More Detail Asst. Prof. Dr. Ahmet Sayar Spring-2012 Kocaeli University Computer Engineering Department Principles of Programming Languages.
Chapter 1 Introduction. Chapter 1 - Introduction 2 The Goal of Chapter 1 Introduce different forms of language translators Give a high level overview.
1. 2 Preface In the time since the 1986 edition of this book, the world of compiler design has changed significantly 3.
CS 460/660 Compiler Construction. Class 01 2 Why Study Compilers? Compilers are important – –Responsible for many aspects of system performance Compilers.
Introduction to Compilers. Related Area Programming languages Machine architecture Language theory Algorithms Data structures Operating systems Software.
Topic #1: Introduction EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
1 Compiler Design (40-414)  Main Text Book: Compilers: Principles, Techniques & Tools, 2 nd ed., Aho, Lam, Sethi, and Ullman, 2007  Evaluation:  Midterm.
Chapter 1 Introduction Study Goals: Master: the phases of a compiler Understand: what is a compiler Know: interpreter,compiler structure.
Introduction CPSC 388 Ellen Walker Hiram College.
Prof. Necula CS 164 Lecture 11 Introduction to Programming Languages and Compilers CS164 11:00-12:30 TT 10 Evans.
Compiler Design Introduction 1. 2 Course Outline Introduction to Compiling Lexical Analysis Syntax Analysis –Context Free Grammars –Top-Down Parsing –Bottom-Up.
UPRM ICOM 4029 (Adapted from: Prof. Necula UCB CS 164) 1 Introduction to Programming Languages and Compilers CS164 11:00-12:30 TT 10 Evans.
Compiler Construction By: Muhammad Nadeem Edited By: M. Bilal Qureshi.
The Model of Compilation Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University.
1 Compiler & its Phases Krishan Kumar Asstt. Prof. (CSE) BPRCE, Gohana.
What is a compiler? –A program that reads a program written in one language (source language) and translates it into an equivalent program in another language.
Compilers Computer Symbol Table Output Scanner (lexical analysis)
Compiler Construction CPCS302 Dr. Manal Abdulaziz.
1 Asstt. Prof Navjot Kaur Computer Dept PRESENTED BY.
ICS312 Introduction to Compilers Set 23. What is a Compiler? A compiler is software (a program) that translates a high-level programming language to machine.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 1 Ahmed Ezzat.
Presented by : A best website designer company. Chapter 1 Introduction Prof Chung. 1.
CS416 Compiler Design1. 2 Course Information Instructor : Dr. Ilyas Cicekli –Office: EA504, –Phone: , – Course Web.
Chapter 1 Introduction Samuel College of Computer Science & Technology Harbin Engineering University.
CHAPTER 1 INTRODUCTION TO COMPILER SUNG-DONG KIM, DEPT. OF COMPUTER ENGINEERING, HANSUNG UNIVERSITY.
Chapter 1. Introduction.
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Advanced Computer Systems
Compiler Design (40-414) Main Text Book:
Chapter 1 Introduction.
Introduction to Compiler Construction
Compiler Construction (CS-636)
Chapter 1 Introduction.
课程名 编译原理 Compiling Techniques
Chapter 1: Introduction to Compiling (Cont.)
CS 536 / Fall 2017 Introduction to programming languages and compilers
Compiler Construction
CS416 Compiler Design lec00-outline September 19, 2018
Course supervisor: Lubna Siddiqui
Lecture 2: General Structure of a Compiler
Introduction CI612 Compiler Design CI612 Compiler Design.
Compiler 薛智文 TH 6 7 8, DTH Spring.
Compiler 薛智文 TH 6 7 8, DTH Spring.
CS416 Compiler Design lec00-outline February 23, 2019
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Chapter 10: Compilers and Language Translation
Lec00-outline May 18, 2019 Compiler Design CS416 Compiler Design.
Compiler 薛智文 M 2 3 4, DTH Spring.
Presentation transcript:

1 Introduction to Compilation Cheng-Chia Chen

2 What is a compiler? l a program that translates an executable program in one language into an executable program in another language l the compiler typically lowers the level of abstraction of the program l for “optimizing” compilers, we also expect the program produced to be better, in some way, than the original

3 Abstract view of compiler Implications: »recognize legal (and illegal) programs »generate correct code »manage storage of all variables and code »need format for object (or assembly) code

4 Traditional decomposition of a compiler Implications: intermediate language (il) front end maps legal code into il back end maps il onto target machine simplify retargeting allows multiple front ends multiple phases => better code Front end is O(n) or O(n log n) Back end is NP-Complete

5 Advantage of the decomposition

6 Components of a Compiler l Analysis »Lexical Analysis »Syntax Analysis »Semantic Analysis l Synthesis »Intermediate Code Generation »Code Optimization »Code Generation

7 The Structure of a Compiler l Front-end »Lexical Analysis »Parsing »Semantic Analysis »intermediate code generation l back-end »Optimization »Code Generation l The first 3, at least, can be understood by analogy to how humans comprehend a natural language.

8 Responsibilities of Frond End l recognize legal programs l report errors l produce il l preliminary storage map l shape the code for the back end Much of front end construction can be automated

9 Responsibilities of Back-end code optimization: [middle-end] »analyzes and changes il »goal is to reduce runtime »must preserve values code generation: »translate il into target machine code »choose instructions for each il operation »decide what to keep in registers at each point »ensure conformance with system interfaces

10 Lexical Analysis l First step: recognize words. »Smallest unit above letters Compiler is an interesting course. l Note the »Capital “ C ” (start of sentence symbol) »Blank “ “ (word separator) »Period “. ” (end of sentence symbol)

11 More Lexical Analysis l Lexical analysis is not trivial. Consider: 編譯器是一門有趣的課程。 l Programming languages are typically more cryptic than English: *h->j++ = e-5

12 And More Lexical Analysis Lexical analyzer divides program text into “ words ” or “ tokens ” if x == y then z = 1; else z = 2; l Units: if, x, ==, y, then, z, =, 1, ;, else, z, =, 2, ;

13 Parsing (syntax analysis) l Once words are understood, the next step is to understand sentence structure l Parsing = Diagramming Sentences »The diagram is a tree

14 Diagramming a Sentence Thislineisalongersentence verbarticlenounarticleadjectivenoun NP sentence VP

15 Parsing Programs l Parsing program expressions is the same l Consider: If x == y then z = 1; else z = 2; l Diagrammed: if-then-else xyz1z2== assignrelationassign predicateelse-stmtthen-stmt

16 Semantic Analysis Once sentence structure is understood, we can try to understand “ meaning ” »But meaning is too hard for compilers l Compilers perform limited analysis to catch inconsistencies l Some do more analysis to improve the performance of the program

17 Semantic Analysis in Natural Language l Example: 張三認為李四拿走他的課本. 誰的課本被拿走 ? 張三, 李四 or 第三者 ? l Even worse: Jack said Jack left his assignment at home? How many Jacks are there? Which one left the assignment?

18 Semantic Analysis in Programming l Programming languages define strict rules to avoid such ambiguities This C++ code prints “ 4 ” ; the inner definition is used l Illegal in Java. { int x = 3; { int x = 4; cout << x; }

19 More Semantic Analysis l Compilers perform many semantic checks besides variable bindings l Example: John loves her sister. A “ type mismatch ” between her and John; we know they are different people »Presumably John is male

20 Optimization l No strong counterpart in English, but akin to editing l Automatically modify programs so that they »Run faster »Use less memory »In general, conserve some resource

21 Optimization Example X = Y * 0 is the same as X = 0 X = Y * 2 is the same as X = Y + Y Assume X and Y are integers

22 Code Generation l Produces assembly code (usually) l A translation into another language »Analogous to human translation

23 Intermediate Languages l Many compilers perform translations between successive intermediate forms »All but first and last are intermediate languages internal to the compiler »Typically there is 1 IL IL ’ s generally ordered in descending level of abstraction »Highest is source »Lowest is assembly

24 Intermediate Languages (Cont.) IL ’ s are useful because lower levels require exposure of many features hidden by higher levels »registers »memory layout »etc. l It is hard to obtain all these hidden features directly from the source input.

25 Example l source line: a = bb+abs(c-7); »a sequence of ASCII characters in a text file. l The scanner groups characters into tokens: a = bb+abs(c-7); l After scanning, we have the token sequence: Id a Asg Id bb Plus Id abs Lparen Id c Minus IntLiteral 7 Rparen Semi

26 Example l The parser groups these tokens into parse tree: note: (, ) and ; disappear in the tree.

27 l The type checker resolves types and binds declarations within scopes:

28 l Finally, JVM code is generated for each node in the tree (leaves first, then roots): iload 3 // push local 3 (bb) iload 2 // push local 2 (c) ldc 7 // Push literal 7 isub // compute c-7 invokestatic java/lang/Math/abs(I)I iadd // compute bb+abs(c-7) istore 1 // store result into local 1(a)

29 Issues l Compiling is almost this simple, but there are many pitfalls. l Example: How are erroneous programs handled? l Language design has big impact on compiler »Determines what is easy and hard to compile »Course theme: many trade-offs in language design

30 Compilers Today l The overall structure of almost every compiler adheres to the outline l The proportions have changed since FORTRAN »Early: lexing, parsing most complex, expensive »Today: optimization dominates all other phases, lexing and parsing are cheap

31 Applications of Compilation Techniques l Editor l Interpreter l Debugger l Word Processing (Tex, Word) l VLSI Design (VHDL, Verilog) l Pattern Recognition

32 Trends in Compilation l Compilation for speed is less interesting. But: »scientific programs »advanced processors (Digital Signal Processors, advanced speculative architectures) l Ideas from compilation used for improving code reliability: »memory safety »detecting data races »...