Overview of Compiler Design CIS 631, CSE 691, CIS400, CSE 400 Compiler Design Dr. Nancy McCracken January 15, 2008.

Slides:



Advertisements
Similar presentations
CPSC 388 – Compiler Design and Construction
Advertisements

1 CIS 461 Compiler Design and Construction Fall 2014 Instructor: Hugh McGuire slides derived from Tevfik Bultan, Keith Cooper, and Linda Torczon Lecture-Module.
Software & Services Group, Developer Products Division Copyright© 2010, Intel Corporation. All rights reserved. *Other brands and names are the property.
Compilers and Language Translation
Lecture 01 - Introduction Eran Yahav 1. 2 Who? Eran Yahav Taub 734 Tel: Monday 13:30-14:30
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.
CS 153: Concepts of Compiler Design August 25 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
UMBC Introduction to Compilers CMSC 431 Shon Vick 01/28/02.
1.3 Executing Programs. How is Computer Code Transformed into an Executable? Interpreters Compilers Hybrid systems.
CPSC 388 – Compiler Design and Construction Lecture: MWF 11:00am-12:20pm, Room 106 Colton.
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.
CSC 338: Compiler design and implementation
Compiler Construction1 COMP Compiler Construction Lecturer: Dr. Arthur Cater Teaching Assistant:
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.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 2.
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.
Compiler design Lecture 1: Compiler Overview Sulaimany University 2 Oct
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.
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.
. n COMPILERS n n AND n n INTERPRETERS. -Compilers nA compiler is a program thatt reads a program written in one language - the source language- and translates.
Introduction CPSC 388 Ellen Walker Hiram College.
Chapter 1 Introduction Major Data Structures in Compiler
Programming Languages
Compiler Introduction 1 Kavita Patel. Outlines 2  1.1 What Do Compilers Do?  1.2 The Structure of a Compiler  1.3 Compilation Process  1.4 Phases.
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.
Compiler Construction CPCS302 Dr. Manal Abdulaziz.
Programming Language Concepts (CIS 635) Elsa L Gunter 4303 GITC NJIT,
CSC 4181 Compiler Construction
LECTURE 3 Compiler Phases. COMPILER PHASES Compilation of a program proceeds through a fixed series of phases.  Each phase uses an (intermediate) form.
Language Implementation Overview John Keyser Spring 2016.
©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.
Prologue Sung-Dong Kim, Dept. of Computer Engineering, Hansung University.
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
Compiler Construction (CS-636)
Chapter 1 Introduction.
课程名 编译原理 Compiling Techniques
Compiler Lecture 1 CS510.
Compiler Construction
Introduction to Compiler Construction
Course supervisor: Lubna Siddiqui
CSE401 Introduction to Compiler Construction
Introduction to Compiler Construction
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Chapter 10: Compilers and Language Translation
Introduction to Compiler Construction
Presentation transcript:

Overview of Compiler Design CIS 631, CSE 691, CIS400, CSE 400 Compiler Design Dr. Nancy McCracken January 15, 2008

2 Compilers Compilers translate from a source language (typically a high level language) to a functionally equivalent target language (typically the machine code of a particular machine or a machine-independent virtual machine). Compilers for high level programming languages are among the larger and more complex pieces of software –Original languages included Fortran and Cobol Often multi-pass compilers (to facilitate memory reuse) –Compiler development helped in better programming language design Early development focused on syntactic analysis –Commercially, compilers are developed by very large software groups Current focus is on optimization and smart use of resources for modern RISC architectures

3 Motivation to learn about compilers General background of good software engineer –Increases understanding of language semantics –Understanding what machine code is generated for language constructs helps to under performance issues for languages –Helps to understand good language design –Opportunity for non-trivial programming project Can apply compiler techniques to many other language translation applications: –Translating JavaDoc comments to HTML –Collating responses from an survey –Implementing a server that uses a protocol like http or imap –Printers use parsing to render PostScript files –Translating from a hardware description language to the schematic of a circuit –Some aspects of natural language processing, such as a spam filter

4 Dynamic Structure of a Compiler character stream val=01*val+i lexical analysis (scanning) token stream 1 (ident) "val" 3 (assign) - 2 (number) 10 4 (times) - 1 (ident) "val" 5 (plus) - 1 (ident) "i" token number token value syntax analysis (parsing) syntax tree ident = number * ident + ident Term Expression Statement Front end (analysis)

5 Dynamic Structure of a Compiler semantic analysis (type checking,...) syntax tree ident = number * ident + ident Term Expression Statement intermediate representation syntax tree, symbol table, or three address code (TAC)... optimization code generation const10 load1 mul... machine code Front end Back end (synthesis)

6 Compiler versus Interpreter Compiler translates to machine code scannerparser...code generatorloader source codemachine code Variant: interpretation of intermediate code... compiler... source codeintermediate code (e.g. Java bytecode) VM source code is translated into the code of a virtual machine (VM) VM interprets the code simulating the physical machine Interpreter executes source code "directly" scannerparser source code interpretation statements in a loop are scanned and parsed again and again

7 Static Structure of a Compiler parser & sem. analysis scanner symbol table code generation provides tokens from the source code maintains information about declared names and types generates machine code "main program" directs the whole compilation uses data flow

8 Lexical Analysis Stream of characters is grouped into tokens Examples of tokens are identifiers, reserved words, integers, doubles or floats, delimiters, operators and special symbols int a; a = a + 2; int reserved word aidentifier ;special symbol aidentifier =operator aidentifier +operator 2integer constant ;special symbol

9 Syntax Analysis or Parsing Parsing uses a context-free grammar of valid programming language structures to find the structure of the input Result of parsing usually represented by a syntax tree Example of grammar rules: expression -> expression + expression | … variable | constant variable -> identifier constant -> intconstant | doubleconstant | … Example parse tree: =a+a2=a+a2

10 Semantic Analysis Parse tree is checked for things that violates the semantic rules of the language –Semantic rules may be written with an attribute grammar Examples: –Using undeclared variables –Function called with improper arguments Number and type of arguments –Array variables used without array syntax –Type checking of operator arguments –Left hand side of an assignment must be a variable (sometimes called an L-value) –...

11 Intermediate Code Generation An intermediate code representation is used to further break the program into something that is easy to generate machine code from Typical choices include –Annotated parse trees –Three Address Code (TAC), and abstract machine language Example statements:Resulting TAC: if (a b { a = a – c; }if _t1 goto L0 _t2 = a – c a = _t2 c = b * c L0: _t3 = b * c c = _t3

12 Code Optimization Compiler converts the intermediate representation to another one that attempts to be smaller and faster. Typical optimizations: –Inhibit code generation for unreachable segments –Getting rid of unused variables –Eliminating multiplication by 1 and addition by 0 –Loop optimization: e.g. removing statements not modified in the loop –Common sub-expression elimination –...

13 Object Code Generation The target program is generated in the machine language of the target architecture. –Memory locations are selected for each variable –Instructions are chosen for each operation –Individual tree nodes or TAC is translated into a sequence of machine language instructions that perform the same task Typical machine language instructions include things like –Load register –Add register to memory location –Store register to memory –...

14 Object Code Optimization It is possible to have another code optimization phase that transforms the object code into more efficient object code. These optimizations use features of the hardware itself to make efficient use of processors and registers. –Specialized instructions –Pipelining –Branch prediction and other peephole optimizations

15 Symbol Table Symbol table management is a part of the compiler that interacts with several of the phases –Identifiers are found in lexical analysis and placed in the symbol table –During syntactical and semantical analysis, type and scope information is added –During code generation, type information is used to determine what instructions to use –During optimization, the “live analysis” may be kept in the symbol table

16 Error Handling Error handling and reporting also occurs across many phases –Lexical analyzer reports invalid character sequences –Syntactic analyzer reports invalid token sequences –Semantic analyzer reports type and scope errors, and the like The compiler may be able to continue with some errors, but other errors may stop the process

17 Compiler Project Choose a source language –Needs to be large enough to have many of the interesting language features/problems for compiling –Needs to be small enough to implement in a semester –Tentatively choosing a subset called MicroJava Choose a target language –Needs to be either a real assembly language for a machine with an assembler or a virtual machine language with an interpreter –Tentatively choosing a slightly modified Java VM with an interpreter

18 Example MicroJava Program program P final int size = 10; class Table { int[] pos; int[] neg; } Table val; { void main() int x, i; {// initialize val val = new Table; val.pos = new int[size]; val.neg = new int[size]; i = 0; while (i < size) { val.pos[i] = 0; val.neg[i] = 0; i = i + 1; } // read values read(x); while (x != 0) { if (x > 0) val.pos[x] = val.pos[x] + 1; else if (x < 0) val.neg[-x] = val.neg[-x] + 1; read(x); } main program; no separate compilation classes (without methods) global variables local variables

19 References for these slides Wirth, Compiler Construction, chapters 1 and 2 Course notes from H. Mossenback, System Specification and Compiler Construction, –Also notes on MicroJava Course notes from Jerry Cain, Compilers, General references: –Aho, A., Sethi, R., Ullman, J., Compilers: Principles, Techniques and Tools, Addison-Wesley, (also new edition 2006) –Steven Muchnik, Advanced Compiler Design and Implementation, Morgan-Kaufmann, –Keith Cooper and Linda Torczon, Engineering a Compiler, Morgan-Kaufmann, 2003.