UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 531 Compiler Construction Ch.2 Spring 2007 Marco Valtorta

Slides:



Advertisements
Similar presentations
CS1Q Computer Systems Lecture 14
Advertisements

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 531 Compiler Construction Final Lecture of the Semester Spring 2010 Marco.
Compiler Construction by Muhammad Bilal Zafar (AP)
Object Oriented Programming in Java George Mason University Fall 2011
1 Languages and Compilers (SProg og Oversættere) Lecture 2 Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 531 Compiler Construction Ch.2: Language Processors Spring 2010 Marco.
T-diagrams “Mommy, where do compilers come from?” Adapted from:
Introduction to Computers and Programming. Some definitions Algorithm: –A procedure for solving a problem –A sequence of discrete steps that defines such.
Reference Book: Modern Compiler Design by Grune, Bal, Jacobs and Langendoen Wiley 2000.
1 Programming Languages Translation  Lecture Objectives:  Be able to list and explain five features of the Java programming language.  Be able to explain.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 531 Compiler Construction Ch.1 Spring 2010 Marco Valtorta
Introduction to Computers and Programming. Some definitions Algorithm: Algorithm: A procedure for solving a problem A procedure for solving a problem.
3/24/2004COSC4301 assignment 31 Compiler, Interpreter, and Bootstrapping Motivation: When we are asked to write a  Compiler for a complex source language.
3-1 3 Compilers and interpreters  Compilers and other translators  Interpreters  Tombstone diagrams  Real vs virtual machines  Interpretive compilers.
Source Code Basics. Code For a computer to execute instructions, it needs to be in binary Each instruction is given a number Known as “operation code”
Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, Introduction: Hello, World Below slide 1www.idc.ac.il/tecs Introduction:
P51UST: Unix and Software Tools Unix and Software Tools (P51UST) Compilers, Interpreters and Debuggers Ruibin Bai (Room AB326) Division of Computer Science.
CSCI 224 Introduction to Java Programming. Course Objectives  Learn the Java programming language: Syntax, Idioms Patterns, Styles  Become comfortable.
M1G Introduction to Programming 2 4. Enhancing a class:Room.
1 Languages and Compilers (SProg og Oversættere) Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm Hutchinson.
High-level Languages.
High level & Low level language High level programming languages are more structured, are closer to spoken language and are more intuitive than low level.
CMSC 202 Computer Science II for Majors Object-Oriented Programming.
An intro to programming. The purpose of writing a program is to solve a problem or take advantage of an opportunity Consists of multiple steps:  Understanding.
Introduction to Programming Languages. Problem Solving in Programming.
Introduction CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
CS1Q Computer Systems Lecture 14 Simon Gay. Lecture 14CS1Q Computer Systems - Simon Gay2 Where we are Global computing: the Internet Networks and distributed.
C++ Programming Language Lecture 1 Introduction By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Language processors (Chapter 2) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.
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.
1 CSC204 – Programming I Lecture 2 Intro to OOP with Java.
Fundamental Programming: Fundamental Programming K.Chinnasarn, Ph.D.
Getting started with Programming using IDE. JAVA JAVA IS A PROGRAMMING LANGUAGE AND A PLATFORM. IT CAN BE USED TO DELIVER AND RUN HIGHLY INTERACTIVE DYNAMIC.
CS 614: Theory and Construction of Compilers Lecture 7 Fall 2002 Department of Computer Science University of Alabama Joel Jones.
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.
Ted Pedersen – CS 3011 – Chapter 10 1 A brief history of computer architectures CISC – complex instruction set computing –Intel x86, VAX –Evolved from.
1 Compiler Design (40-414)  Main Text Book: Compilers: Principles, Techniques & Tools, 2 nd ed., Aho, Lam, Sethi, and Ullman, 2007  Evaluation:  Midterm.
Data Structures and Algorithms Dr. Tehseen Zia Assistant Professor Dept. Computer Science and IT University of Sargodha Lecture 1.
Chapter 1 Introduction. Chapter 1 -- Introduction2  Def: Compiler --  a program that translates a program written in a language like Pascal, C, PL/I,
A compiler is a computer program that translate written code (source code) into another computer language Associated with high level languages A well.
 Chapter 2 Language Processors Fall Chart 2  Translators and Compilers  Interpreters  Real and Abstract Machines  Interpretive Compilers 
 Computer Languages Computer Languages  Machine Language Machine Language  Assembly Language Assembly Language  High Level Language High Level Language.
Introduction to OOP CPS235: Introduction.
JavaScript 101 Introduction to Programming. Topics What is programming? The common elements found in most programming languages Introduction to JavaScript.
Lesson 1 1 LESSON 1 l Background information l Introduction to Java Introduction and a Taste of Java.
1 Languages and Compilers (SProg og Oversættere) Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm Hutchinson.
Programming Language Concepts (CIS 635) Elsa L Gunter 4303 GITC NJIT,
1 Languages and Compilers (SProg og Oversættere) Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm Hutchinson.
ITP 109 Week 2 Trina Gregory Introduction to Java.
Lecture #1: Introduction to Algorithms and Problem Solving Dr. Hmood Al-Dossari King Saud University Department of Computer Science 6 February 2012.
Evolution and History of Programming Languages
Computer Systems Nat 5 Computing Science
COMP Compilers Lecture 1: Introduction
Lecture 1b- Introduction
Visit for more Learning Resources
Overview of Compilers and Language Translation
Compiler, Interpreter, and Bootstrapping
Programming Language Hierarchy, Phases of a Java Program
CSCI-235 Micro-Computer Applications
Computer Systems Nat 5 Computing Science
Topic: Difference b/w JDK, JRE, JIT, JVM
课程名 编译原理 Compiling Techniques
Want to Write a Compiler?
Course supervisor: Lubna Siddiqui
COMP Compilers Lecture 1: Introduction
CSCE 531 Compiler Construction Ch.2
Virtual Machines (Introduction to Virtual Machines)
Programming language translators
Presentation transcript:

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 531 Compiler Construction Ch.2 Spring 2007 Marco Valtorta

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Acknowledgment The slides are based on the textbook and other sources, including slides from Bent Thomsen’s course at the University of Aalborg in Denmark and several other fine textbooks The three main other compiler textbooks I considered are: –Aho, Alfred V., Monica S. Lam, Ravi Sethi, and Jeffrey D. Ullman. Compilers: Principles, Techniques, & Tools, 2 nd ed. Addison-Welsey, (The “dragon book”) –Appel, Andrew W. Modern Compiler Implementation in Java, 2 nd ed. Cambridge, (Editions in ML and C also available; the “tiger books”) –Grune, Dick, Henri E. Bal, Ceriel J.H. Jacobs, and Koen G. Langendoen. Modern Compiler Design. Wiley, 2000

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Today’s lecture Three topics –Treating compilers and interpreters as black- boxes Tombstone- or T- diagrams –A first look inside the black-box Your guided tour –Some language design Issues

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Terminology Translatorinputoutput source program object program is expressed in the source language is expressed in the implementation language is expressed in the target language Q: Which programming languages play a role in this picture? A: All of them!

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Tombstone Diagrams What are they? –diagrams consisting out of a set of “puzzle pieces” we can use to reason about language processors and programs –different kinds of pieces –combination rules (not all diagrams are “well formed”) M Machine implemented in hardware S -> T L Translator implemented in L MLML Language interpreter in L Program P implemented in L L P

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Tombstone diagrams: Combination rules S PP T S -> T M M L P M WRONG!OK! M M P M L P WRONG!

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Tetris x86C Tetris Compilation x86 Example: Compilation of C programs on an x86 machine C -> x86 x86 Tetris x86

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering What is Tetris? Tetris® The World's Most Popular Video Game Since its commercial introduction in 1987, Tetris® has been established as the largest selling and most recognized global brand in the history of the interactive game software industry. Simple, entertaining, and yet challenging, Tetris® can be found on more than 60 platforms. Over 65 million Tetris® units have been sold worldwide to date.

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Tetris PPCC Tetris Cross compilation x86 Example: A C “cross compiler” from x86 to PPC C -> PPC x86 A cross compiler is a compiler which runs on one machine (the host machine) but emits code for another machine (the target machine). Host ≠ Target Q: Are cross compilers useful? Why would/could we use them? PPC Tetris PPC download

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Tetris x86 Tetris JVMJava Tetris Two Stage Compilation x86 Java->JVM x86 A two-stage translator is a composition of two translators. The output of the first translator is provided as input to the second translator. x86 JVM->x86 x86

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering x86 Java->x86 Compiling a Compiler Observation: A compiler is a program! Therefore it can be provided as input to a language processor. Example: compiling a compiler. Java->x86 C x86 C -> x86 x86

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Interpreters An interpreter is a language processor implemented in software, i.e. as a program. Terminology: abstract (or virtual) machine versus real machine Example: The Java Virtual Machine JVM x86 JVM Tetris Q: Why are abstract machines useful?

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Interpreters Q: Why are abstract machines useful? 1) Abstract machines provide better platform independence JVM x86 PPC JVM Tetris JVM PPC JVM Tetris

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Interpreters Q: Why are abstract machines useful? 2) Abstract machines are useful for testing and debugging. Example: Testing the “Ultima” processor using hardware emulation Ultima x86 Ultima  P P Functional equivalence Note: we don’t have to implement Ultima emulator in x86 we can use a high-level language and compile it.

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Interpreters versus Compilers Q: What are the tradeoffs between compilation and interpretation? Compilers typically offer more advantages when –programs are deployed in a production setting –programs are “repetitive” –the instructions of the programming language are complex Interpreters typically are a better choice when –we are in a development/testing/debugging stage –programs are run once and then discarded –the instructions of the language are simple –the execution speed is overshadowed by other factors e.g. on a web server where communications costs are much higher than execution speed

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Interpretive Compilers Why? A tradeoff between fast(er) compilation and a reasonable runtime performance. How? Use an “intermediate language” more high-level than machine code => easier to compile to more low-level than source language => easy to implement as an interpreter Example: A “Java Development Kit” for machine M Java->JVM M JVM M

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering P JVMJava P Interpretive Compilers Example: Here is how we use our “Java Development Kit” to run a Java program P Java->JVM M JVM M M JVM P M javac java

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Portable Compilers Example: Two different “Java Development Kits” Java->JVM JVM M Kit 2: Java->JVM M JVM M Kit 1: Q: Which one is “more portable”?

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Portable Compilers In the previous example we have seen that portability is not an “all or nothing” kind of deal. It is useful to talk about a “degree of portability” as the percentage of code that needs to be re-written when moving to a dissimilar machine. In practice 100% portability is impossible.

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Example: a “portable” compiler kit Java->JVM Java JVM Java Java->JVM JVM Q: Suppose we want to run this kit on some machine M. How could we go about realizing that goal? (with the least amount of effort) Portable Compiler Kit:

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Example: a “portable” compiler kit Java->JVM Java JVM Java Java->JVM JVM Q: Suppose we want to run this kit on some machine M. How could we go about realizing that goal? (with the least amount of effort) JVM Java JVM C reimplement C->M M JVM M M

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Example: a “portable” compiler kit Java->JVM Java JVM Java Java->JVM JVM M This is what we have now: Now, how do we run our Tetris program? Tetris JVMJava Tetris M Java->JVM JVM M JVM Tetris JVM M M

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Bootstrapping Java->JVM Java JVM Java Java->JVM JVM Remember our “portable compiler kit”: We haven’t used this yet! Java->JVM Java Same language! Q: What can we do with a compiler written in itself? Is that useful at all? JVM M

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Bootstrapping Java->JVM Java Same language! Q: What can we do with a compiler written in itself? Is that useful at all? By implementing the compiler in (a subset of) its own language, we become less dependent on the target platform => more portable implementation. But… “chicken and egg problem”? How do to get around that? => BOOTSTRAPPING: requires some work to make the first “egg”. There are many possible variations on how to bootstrap a compiler written in its own language.

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Bootstrapping an Interpretive Compiler to Generate M code Java->JVM Java JVM Java Java->JVM JVM Our “portable compiler kit”: P M Java P Goal we want to get a “completely native” Java compiler on machine M Java->M M JVM M M

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Bootstrapping an Interpretive Compiler to Generate M code (first approach) Step 1: implement Java->M JavaJVM Java ->M Java->JVM JVM M M Java ->M Java Step 2: compile it Step 3: Use this to compile again by rewriting Java ->JVM Java

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Bootstrapping an Interpretive Compiler to Generate M code (first approach) Step 3: “Self compile” the Java (in Java) compiler M Java->M JVM M M Java->M Java Java->M JVMThis is our desired compiler! Step 4: use this to compile the P program P M Java P Java->M M

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Bootstrapping an Interpretive Compiler to Generate M code (second approach) Idea: we will build a two-stage Java -> M compiler. P M P M Java P P JVM M Java->JVM M M JVM->M M We will make this by compiling To get this we implement JVM->M Java Java->JVM JVM and compile it

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Bootstrapping an Interpretive Compiler to Generate M code (second approach) Step 1: implement JVM->M JavaJVM JVM->M Java->JVM JVM M M JVM->M Java Step 2: compile it Step 3: compile this

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Bootstrapping an Interpretive Compiler to Generate M code (second approach) Step 3: “Self compile” the JVM (in JVM) compiler M JVM->M JVM M M JVM->M JVM JVM->M JVMThis is the second stage of our compiler! Step 4: use this to compile the Java compiler

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Bootstrapping an Interpretive Compiler to Generate M code Step 4: Compile the Java->JVM compiler into machine code M Java->JVM M JVM JVM->M MThe first stage of our compiler! We are DONE! P M P M Java P P JVM M Java->JVM M M JVM->M M

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Full Bootstrap A full bootstrap is necessary when we are building a new compiler from scratch. Example: We want to implement an Ada compiler for machine M. We don’t currently have access to any Ada compiler (not on M, nor on any other machine). Idea: Ada is very large, we will implement the compiler in a subset of Ada and bootstrap it from a subset of Ada compiler in another language. (e.g. C) Ada-S ->M C v1 Step 1: build a compiler for Ada-S in another language

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Full Bootstrap Ada-S ->M C v1 Step 1a: build a compiler (v1) for Ada-S in another language. Ada-S ->M C v1 M Ada-S->M v1 Step 1b: Compile v1 compiler on M M C->M M This compiler can be used for bootstrapping on machine M but we do not want to rely on it permanently!

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Full Bootstrap Ada-S ->M Ada-S v2 Step 2a: Implement v2 of Ada-S compiler in Ada-S Ada-S ->M Ada-S v2 M M Ada-S->M v2 Step 2b: Compile v2 compiler with v1 compiler Ada-S ->M M v1 Q: Is it hard to rewrite the compiler in Ada-S? We are now no longer dependent on the availability of a C compiler!

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Full Bootstrap Step 3a: Build a full Ada compiler in Ada-S Ada->M Ada-S v3 M M Ada->M v3 Ada-S ->M M v2 Step 3b: Compile with v2 compiler Ada->M Ada-S v3 From this point on we can maintain the compiler in Ada. Subsequent versions v4,v5,... of the compiler in Ada and compile each with the the previous version.

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Half Bootstrap We discussed full bootstrap which is required when we have no access to a compiler for our language at all. Q: What if we have access to an compiler for our language on a different machine HM but want to develop one for TM ? Ada->HM HM We have: Ada->TM TM We want: Idea: We can use cross compilation from HM to TM to bootstrap the TM compiler. Ada->HM Ada

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering HM Ada->TM Half Bootstrap Idea: We can use cross compilation from HM to M to bootstrap the M compiler. Step 1: Implement Ada->TM compiler in Ada Ada->TM Ada Step 2: Compile on HM Ada->TM Ada Ada->HM HM Cross compiler: running on HM but emits TM code

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering TM Ada->TM Half Bootstrap Step 3: Cross compile our TM compiler. Ada->TM Ada Ada->TM HM DONE! From now on we can develop subsequent versions of the compiler completely on TM

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Bootstrapping to Improve Efficiency The efficiency of programs and compilers: Efficiency of programs: - memory usage - runtime Efficiency of compilers: - Efficiency of the compiler itself - Efficiency of the emitted code Idea: We start from a simple compiler (generating inefficient code) and develop more sophisticated version of it. We can then use bootstrapping to improve performance of the compiler.

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Bootstrapping to Improve Efficiency We have: Ada->M slow Ada Ada-> M slow M slow We implement: Ada->M fast Ada Ada->M fast Ada M Ada->M fast M slow Step 1 Ada-> M slow M slow Step 2 Ada->M fast Ada M Ada->M fast M fast Ada-> M fast M slow Fast compiler that emits fast code!

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Conclusion To write a good compiler you may be writing several simpler ones first You have to think about the source language, the target language and the implementation language. Strategies for implementing a compiler 1.Write it in machine code 2.Write it in a lower level language and compile it using an existing compiler 3.Write it in the same language that it compiles and bootstrap The work of a compiler writer is never finished, there is always version 1.x and version 2.0 and …