國立台灣大學 資訊工程學系 薛智文 98 Spring Compiler TH 234, DTH 103.

Slides:



Advertisements
Similar presentations
Introduction to Compiler Construction
Advertisements

CS 31003: Compilers Introduction to Phases of Compiler.
Compiler Chang Chi-Chung Textbook Compilers: Principles, Techniques, and Tools, 2/E.  Alfred V. Aho, Columbia University  Monica S. Lam,
CPSC Compiler Tutorial 9 Review of Compiler.
Yu-Chen Kuo1 Chapter 1 Introduction to Compiling.
Chapter 1: Introduction to Compiling
Reference Book: Modern Compiler Design by Grune, Bal, Jacobs and Langendoen Wiley 2000.
Compiler Construction1 A Compulsory Module for Students in Computer Science Department Faculty of IT / Al – Al Bayt University First Semester 2009/2010.
1.3 Executing Programs. How is Computer Code Transformed into an Executable? Interpreters Compilers Hybrid systems.
Lecture 2 Phases of Compiler. Preprocessors, Compilers, Assemblers, and Linkers Preprocessor Compiler Assembler Linker Skeletal Source Program Source.
CPSC 388 – Compiler Design and Construction Lecture: MWF 11:00am-12:20pm, Room 106 Colton.
September 7, September 7, 2015September 7, 2015September 7, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University.
Course Revision Contents  Compilers  Compilers Vs Interpreters  Structure of Compiler  Compilation Phases  Compiler Construction Tools  A Simple.
COP4020 Programming Languages
Chapter 1. Introduction.
Introduction For some compiler, the intermediate code is a pseudo code of a virtual machine. Interpreter of the virtual machine is invoked to execute the.
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.
Introduction to Compiler Construction Robert van Engelen COP5621 Compiler Construction Copyright Robert.
CST320 - Lec 11 Why study compilers? n n Ties lots of things you know together: –Theory (finite automata, grammars) –Data structures –Modularization –Utilization.
RIT 08/11/47Chapter 11 Chapter 1: Introduction to Compiling Dr. Winai Wichaipanitch Rajamangala Institute of Technology Klong 6 Thanyaburi Pathumthani.
1 COMP 3438 – Part II-Lecture 1: Overview of Compiler Design Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
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.
1 Chapter 1 Introduction. 2 Outlines 1.1 Overview and History 1.2 What Do Compilers Do? 1.3 The Structure of a Compiler 1.4 The Syntax and Semantics of.
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 Lecture 1 Wed, Jan 12, The Stages of Compilation Lexical analysis. Syntactic analysis. Semantic analysis. Intermediate code generation.
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.
Overview of Previous Lesson(s) Over View  A program must be translated into a form in which it can be executed by a computer.  The software systems.
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 to Compiling
Compiler Design Introduction 1. 2 Course Outline Introduction to Compiling Lexical Analysis Syntax Analysis –Context Free Grammars –Top-Down Parsing –Bottom-Up.
The Model of Compilation Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University.
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.
CH1.1 CSE244 Chapter 1: Introduction to Compiling Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University of Connecticut.
Dr. Mohamed Ramadan Saady 314ALL CH1.1 Chapter 1: Introduction to Compiling.
Compiler Construction CPCS302 Dr. Manal Abdulaziz.
©SoftMoore ConsultingSlide 1 Structure of Compilers.
1 Asstt. Prof Navjot Kaur Computer Dept PRESENTED BY.
Overview of Compilation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 2.
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.
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Advanced Computer Systems
Compiler Design (40-414) Main Text Book:
PRINCIPLES OF COMPILER DESIGN
Chapter 1 Introduction.
Introduction to Compiler Construction
Chapter 1 Introduction.
课程名 编译原理 Compiling Techniques
Chapter 1: Introduction to Compiling (Cont.)
Compiler Construction
CS416 Compiler Design lec00-outline September 19, 2018
Introduction to Compiler Construction
Course supervisor: Lubna Siddiqui
Introduction CI612 Compiler Design CI612 Compiler Design.
Compiler 薛智文 TH 6 7 8, DTH Spring.
Compiler 薛智文 TH 6 7 8, DTH Spring.
Compiler design.
CS416 Compiler Design lec00-outline February 23, 2019
Introduction to Compiler Construction
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Lec00-outline May 18, 2019 Compiler Design CS416 Compiler Design.
Compiler 薛智文 M 2 3 4, DTH Spring.
Introduction to Compiler Construction
Presentation transcript:

國立台灣大學 資訊工程學系 薛智文 98 Spring Compiler TH 234, DTH 103

資工系網媒所 NEWS 實驗室 08:37 /271

資工系網媒所 NEWS 實驗室 08:37 /272 Why Study Compilers? 1.Excellent software-engineering example --- theory meets practice. 2.Essential software tool. 3.Influences hardware design, e.g., RISC, VLIW. 4.Tools (mostly “ optimization ” ) for enhancing software reliability and security.

資工系網媒所 NEWS 實驗室 08:37 /273 Compilers & Architecture Modern architectures have very complex structures, especially opportunities for parallel execution. Sequential programs can only make effective use of these features via an optimizing compiler. Hardware question: If we implemented this, could a compiler use it?

資工系網媒所 NEWS 實驗室 08:37 /274 Software Reliability Optimization technology (data-flow analysis) used in: Lock/unlock errors. Buffers not range-checked. Memory Leaks. SQL injection bugs..

資工系網媒所 NEWS 實驗室 08:37 /275 What this Course Offers? Compiler methodology for both compiler implementation and related applications. Theoretical framework. Key algorithms. Hands-on experience. Nongoal: build a complete optimizing compiler.

資工系網媒所 NEWS 實驗室 08:37 /276 Course Outline Part Introduction. Part Scanner. Part Parser. Part Syntax-Directed Translation. Part Symbol Table. Part Intermediate Code Generation. Part Run Time Storage Organization. Part Optimization. Part How to Write a Compiler. Part A Simple Code Generation (PSEUDO) Example.

資工系網媒所 NEWS 實驗室 08:37 /277 Introduction Compiler is one of language processors. input Compiler source program target program output

資工系網媒所 NEWS 實驗室 08:37 /278 What is a Compiler? Definitions: The software system translates description of computations into a program executable by a computer. Source and target must be equivalent! Compiler writing spans: programming languages; machine architecture; language theory; algorithms and data structures; software engineering. History: 1950: the first FORTRAN compiler took 18 man-years; now: using software tools, can be done in a few months as a student’s project. input Compiler source program target program output

資工系網媒所 NEWS 實驗室 08:37 /279 An Interpreter input Interpreter source program output

資工系網媒所 NEWS 實驗室 08:37 /2710 A Hybrid Compiler Translator source program input Virtual Machine intermediate program output

資工系網媒所 NEWS 實驗室 08:37 /2711 A Language-Processing System source program library files relocatable object files Preprocessor modified source program Compiler target assembly program Assembler relocatable machine code Linker/Loader target machine code

資工系網媒所 NEWS 實驗室 08:37 /2712 Applications Computer language compilers. Translator: from one format to another. query interpreter text formatter silicon compiler infix notation  postfix notation: pretty printers · · · Software productivity tools – 6 * * –

資工系網媒所 NEWS 實驗室 08:37 /2713 Relations with Computational Theory Computational theory: a set of grammar rules ≡ the definition of a particular machine. also equivalent to a set of languages recognized by this machine. a type of machines: a family of machines with a given set of operations, or capabilities; power of a type of machines ≡ the set of languages that can be recognized by this type of machines.

資工系網媒所 NEWS 實驗室 08:37 /2714 A Language-Processing System source program library files relocatable object files Preprocessor modified source program Compiler target assembly program Assembler relocatable machine code Linker/Loader target machine code

資工系網媒所 NEWS 實驗室 08:37 /2715 Phases of a Compiler character stream Lexical Analyzer (scanner) token stream Machine-Independent Code Optimizer optimized intermediate representation Code Generator relocatable machine code Machine-Dependent Code Optimizer target-machine code Semantic Analyzer annotated abstract-syntax tree Syntax Analyzer (parser) abstract-syntax tree Intermediate Code Generator intermediate representation Symbol Table Error Handling front-end back-end

資工系網媒所 NEWS 實驗室 08:37 /2716 Lexical Analyzer (Scanner) Actions: Reads characters from the source program; lexemes pattern Groups characters into lexemes, i.e., sequences of characters that “go together”, following a given pattern ; token Each lexeme corresponds to a token. the scanner returns the next token, plus maybe some additional information, to the parser; The scanner may also discover lexical errors, i.e., erroneous characters. lexemetokenbad character The definitions of what a lexeme, token or bad character is depend on the definition of the source language.

資工系網媒所 NEWS 實驗室 08:37 /2717 Scanner Example for C Lexeme: C statement position = initial + rate * 60; (Lexeme) position = initial + rate * 60 ; (Token) ID ASSIGN ID PLUS ID TIME INT SEMI-COL Arbitrary number of blanks (white spaces) between lexemes. Erroneous sequence of characters, that are not parts of comments, for the C language: control 2abc 1position… 2initial… 3rate… Symbol Table

資工系網媒所 NEWS 實驗室 08:37 /2718 Syntax Analyzer (Parser) Actions: grammatical phrases Group tokens into grammatical phrases, to discover the underlying structure of the source syntax errors Find syntax errors, e.g., the following C source line: (Lexeme) index = 12 * ; (Token) ID ASSIGN INT TIMES SEMI-COL Every token is legal, but the sequence is erroneous! static semantic errors May find some static semantic errors, e.g., use of undeclared variables or multiple declared variables. May generate code, or build some intermediate representation of the source program, such as an abstract-syntax tree.

資工系網媒所 NEWS 實驗室 08:37 /2719 Source code: position = initial + rate * 60 Abstract-syntax tree: interior nodes of the tree are OPERATORS; a node’s children are its OPERANDS; logical unit each subtree forms a logical unit. the subtree with * at its root shows that * has higher precedence than +, the operation “rate * 60” must be performed as a unit, not “initial + rate”. Where is ”;”? Parser Example for C = + * 60 1position… 2initial… 3rate… Symbol Table

資工系網媒所 NEWS 實驗室 08:37 /2720 Semantic Analyzer Actions: type errors Check for more static semantic errors, e.g., type errors. May annotate and/or change the abstract syntax tree. = + * 60 = + * int_to_float 60 1position… 2initial… 3rate… Symbol Table

資工系網媒所 NEWS 實驗室 08:37 /2721 Intermediate Code Generator Actions: translate from abstract-syntax trees to intermediate codes. One choice for intermediate code is 3-address code : Each statement contains at most 3 operands; in addition to “=”, i.e., assignment, at most one operator. An ”easy” and “universal” format that can be translated into most assembly languages. t 1 = int_to_float(60) t 2 = id 3 * t 1 t 3 = id 2 + t 2 id 1 = t 3 = + * int_to_float 60

資工系網媒所 NEWS 實驗室 08:37 /2722 Optimizer Improve the efficiency of intermediate code. faster Goal may be to make code run faster, and/or to use the least number of registers · · · Current trends: to obtain smaller, but maybe slower, equivalent code for embedded systems; to reduce power consumption. t 1 = id 3 * 60.0 id 1 = id 2 + t 1 t 1 = int_to_float(60) t 2 = id 3 * t 1 t 3 = id 2 + t 2 id 1 = t 3

資工系網媒所 NEWS 實驗室 08:37 /2723 Code Generation A compiler may generate which is rare pure machine codes (machine dependent assembly language) directly, which is rare now ; virtual machine code. Example: PASCAL  compiler  P-code  interpreter  execution Speed is roughly 4 times slower than running directly generated machine codes. Advantages: simplify the job of a compiler; 1/3 for P-code decrease the size of the generated code: 1/3 for P-code ; can be run easily on a variety of platforms P-machine is an ideal general machine whose interpreter can be written easily; divide and conquer; recent example: JAVA and Byte-code.

資工系網媒所 NEWS 實驗室 08:37 /2724 Code Generation Example LDF R 2, id 3 MULF R 2, R 2, #60.0 LDF R 1, id 2 ADDF R 1, R 1, R 2 STF id 1, R 1 t 1 = id 3 * 60.0 id 1 = id 2 + t 1

資工系網媒所 NEWS 實驗室 08:37 /2725 Practical Considerations (1/2) Preprocessing phase: macro substitution: #define MAXC 10 rational preprocessing: add new features for old languages. BASIC C  C ++ compiler directives: #include non-standard language extensions. adding parallel primitives

資工系網媒所 NEWS 實驗室 08:37 /2726 Practical Considerations (2/2) Passes of compiling A pass reads an input file and writes an output file. First pass reads the text file once. May need to read the text one more time for any forward addressed objects, i.e., anything that is used before its declaration. Example: C language goto error_handling; · · · error_handling: · · ·

資工系網媒所 NEWS 實驗室 08:37 /2727 Reduce Number of Passes Each pass takes I/O time. Back-patching Back-patching : leave a blank slot for missing information, and fill in the empty slot when the information becomes available. Example: C language when a label is used trace if it is not defined before, save a trace into the to-be-processed table label name corresponds to LABEL TABLE[i] code generated: GOTO LABEL TABLE[i] when a label is defined check known labels for redefined labels if it is not used before, save a trace into the to-be-processed table if it is used before, then find its trace and fill the current address into the trace Time and space trade-off !