Overview of Compilers and Language Translation

Slides:



Advertisements
Similar presentations
CS1Q Computer Systems Lecture 14
Advertisements

Systems Software.
T-diagrams “Mommy, where do compilers come from?” Adapted from:
Reference Book: Modern Compiler Design by Grune, Bal, Jacobs and Langendoen Wiley 2000.
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 1, Lab.
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.
1.3 Executing Programs. How is Computer Code Transformed into an Executable? Interpreters Compilers Hybrid systems.
CCSA 221 Programming in C CHAPTER 2 SOME FUNDAMENTALS 1 ALHANOUF ALAMR.
Chapter 17 Programming Tools The Architecture of Computer Hardware and Systems Software: An Information Technology Approach 3rd Edition, Irv Englander.
CS 355 – Programming 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.
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.
CS1Q Computer Systems Lecture 14 Simon Gay. Lecture 14CS1Q Computer Systems - Simon Gay2 Where we are Global computing: the Internet Networks and distributed.
Language processors (Chapter 2) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 2.
© 2012 Pearson Education, Inc. All rights reserved. 1-1 Why Java? Needed program portability – Program written in a language that would run on various.
1 COMP 3438 – Part II-Lecture 1: Overview of Compiler Design Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
Copyright © 2007 Addison-Wesley. All rights reserved.1-1 Reasons for Studying Concepts of Programming Languages Increased ability to express ideas Improved.
Unit-1 Introduction Prepared by: Prof. Harish I Rathod
Chapter 1 Introduction. Chapter 1 - Introduction 2 The Goal of Chapter 1 Introduce different forms of language translators Give a high level overview.
CS 460/660 Compiler Construction. Class 01 2 Why Study Compilers? Compilers are important – –Responsible for many aspects of system performance Compilers.
 Chapter 2 Language Processors Fall Chart 2  Translators and Compilers  Interpreters  Real and Abstract Machines  Interpretive Compilers 
 Programming - the process of creating computer programs.
© 2012 Pearson Education, Inc. All rights reserved types of Java programs Application – Stand-alone program (run without a web browser) – Relaxed.
1 Languages and Compilers (SProg og Oversættere) Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm Hutchinson.
1 Asstt. Prof Navjot Kaur Computer Dept PRESENTED BY.
Chapter 1 An Overview of Computers and Programming Languages.
Representation of Data - Instructions Start of the lesson: Open this PowerPoint from the A451 page – Representation of Data/ Instructions How confident.
Programming Languages Concepts Chapter 1: Programming Languages Concepts Lecture # 4.
Some of the utilities associated with the development of programs. These program development tools allow users to write and construct programs that the.
1 The user’s view  A user is a person employing the computer to do useful work  Examples of useful work include spreadsheets word processing developing.
Introduction to Algorithm. What is Algorithm? an algorithm is any well-defined computational procedure that takes some value, or set of values, as input.
Installing Java on a Home machine
DDC 2223 SYSTEM SOFTWARE DDC2223 SYSTEM SOFTWARE.
Lecture 1b- Introduction
Advanced Computer Systems
01 – Overview of Compilers
Assembler, Compiler, MIPS simulator
Compiler, Interpreter, and Bootstrapping
Chapter 1 Introduction.
Introduction to Compiler Construction
Operating System Interface between a user and the computer hardware
Programming languages
CS 326 Programming Languages, Concepts and Implementation
PROGRAMMING LANGUAGES
Programming Language Hierarchy, Phases of a Java Program
CSCI-235 Micro-Computer Applications
Topic: Difference b/w JDK, JRE, JIT, JVM
Microprocessor and Assembly Language
Chapter 1 Introduction.
课程名 编译原理 Compiling Techniques
Want to Write a Compiler?
Assembler, Compiler, Interpreter
Installing Java on a Home machine
Compiler Construction
CSC128 FUNDAMENTALS OF COMPUTER PROBLEM SOLVING
Chapter 11 Introduction to Programming in C
CSCE 531 Compiler Construction Ch.2
COP4020 Programming Languages
Introduction CSC 111.
Chapter 11 Introduction to Programming in C
CMP 131 Introduction to Computer Programming
Assembler, Compiler, Interpreter
Chapter 11 Introduction to Programming in C
Introduction to Microprocessor Programming
(Computer fundamental Lab)
Lecture 1 Runtime environments.
Programming language translators
Presentation transcript:

Overview of Compilers and Language Translation ©SoftMoore Consulting

Programming Languages Overview Programming Languages Serve as a means of communication among people as well as between people and machines Provide a framework for formulating the software solution to a problem Can enhance or inhibit creativity Influence the ways we think about software design by making some program structures easier to describe than others (e.g., recursion in FORTRAN) ©SoftMoore Consulting

Programming Languages (continued) Overview Programming Languages (continued) “Language is an instrument of human reason, and not merely a medium for the expression of thought.” – George Boole “By relieving the brain of all unnecessary work, a good notation sets it free to concentrate on more advanced problems.” – Bertrand Russell ©SoftMoore Consulting

Role of Programming Languages Overview Role of Programming Languages Machine independence Portability Reuse Abstraction Communication of ideas Productivity Reliability (error detection) ©SoftMoore Consulting

Translators and Compilers Overview Translators and Compilers In the context of programming languages, a translator is a program that accepts as input text written in one language (called the source language) and converts it into a semantically equivalent representation in a second language (called the target or object language). If the source language is a high-level language (HLL) and the target language is a low-level language (LLL), then the translator is called a compiler. ©SoftMoore Consulting

Simplified View of Compile/Execute Cycle Overview Simplified View of Compile/Execute Cycle Compiler Source program Object Program input Program Results Compile Execute ©SoftMoore Consulting

Language Versus Its Implementation Overview Language Versus Its Implementation Language Identifier may have an arbitrary number of characters Integer types with arbitrary number of digits Precision of floating-point types is not specified Implementation May restrict the number of significant characters Can restrict valid range of integer types Precision of floating point types is (usually) determined by the machine ©SoftMoore Consulting

Overview Role of a Compiler A compiler must first verify that the source program is valid with respect to the source language definition. If the source program is valid, the compiler must produce a semantically equivalent and reasonably efficient machine language program for the target computer. If the source program is not valid, the compiler must provide reasonable feedback to the programmer as to the nature and location of any errors. Feedback on possible multiple errors is usually desirable. ©SoftMoore Consulting

Other Language Processors Overview Other Language Processors Assembler (translates symbolic assembly language to machine code) High-level language translator (e.g., C++ to C) Interpreter (more on this topic in subsequent slides) Syntax-directed editors Source code formatters/pretty printers Testing/Re-engineering tools Macro preprocessors Linker/Loader ©SoftMoore Consulting

Diagnostic Tools Error reports Cross reference maps Run time profilers Overview Diagnostic Tools Error reports Cross reference maps Run time profilers Source level debuggers Disassemblers Decompilers ©SoftMoore Consulting

Overview Interpreter Translates/executes source program instructions immediately (e.g., one line at a time) Does not analyze and translate the entire program before starting to run – translation is performed every time the program is run Source program is basically treated as another form of input data to the interpreter Control resides in interpreter, not in user program. User program is passive rather than active. Some interpreters perform elementary syntactic translation (e.g., compress keywords into single byte operation codes). ©SoftMoore Consulting

Simplified View of an Interpreter Overview Simplified View of an Interpreter Interpreter Source program Program input Execute Program Results ©SoftMoore Consulting

Compilers Versus Interpreters Overview Compilers Versus Interpreters Compilation two step process (compile, execute) better error detection compiled program runs faster Interpretation one step process (execute) provides rapid feedback to user good for prototyping and highly interactive systems performance penalty ©SoftMoore Consulting

Examples of Interpreters Overview Examples of Interpreters BASIC and Lisp language interpreters Java Virtual Machine (JVM) Java is compiled to an intermediate, low-level form (Java byte code) that gets interpreted by the JVM Operating system command interpreter various Unix shells (sh, csh, bash, etc.) Windows command prompt SQL interpreter (interactive database query) ©SoftMoore Consulting

Overview Emulators An emulator or virtual machine is an interpreter for a machine instruction set. The machine being “emulated” may be real or hypothetical. Similar to real machines, emulators typically use an instruction pointer (program counter) and a fetch-decode-execute cycle. Running a program on an emulator is functionally equivalent to running the program directly on the machine, but the program will experience some performance degradation on the emulator. ©SoftMoore Consulting

Emulators (continued) Overview Emulators (continued) A real machine can be viewed as an interpreter implemented in hardware. Conversely, an emulator can be viewed as a machine implemented in software. ©SoftMoore Consulting

Interpretive Compilers Overview Interpretive Compilers An interpretive compiler is a combination of a compiler and a low-level interpreter (emulator). The compiler translates programs to the instruction set interpreted by the emulator, and the emulator is used to run the compiled program. Example – Oracle/Sun Java Development Kit javac is a compiler java is an emulator for the Java Virtual Machine (JVM) An interpretive compiler usually provides fast compilation with reasonable performance. ©SoftMoore Consulting

Just-In-Time Compiler Overview Just-In-Time Compiler A Just-In-Time (JIT) Compiler is a compiler that converts program source code into native machine code just before the program is run. Java provides a just-in-time compiler with the JVM that translates Java bytecode into native machine code. Use of the JIT compiler is optional. The translation for a method is performed when the method is first called. Performance improvements can be significant for methods that are executed repeatedly. ©SoftMoore Consulting

Writing a Compiler Writing a compiler involves 3 languages Overview Writing a Compiler Writing a compiler involves 3 languages Source language input to the compiler e.g., C++, Java, or CPRL Implementation language the language that the compiler is written in e.g, C++ or Java Target language output of the compiler e.g., assembly language or machine language (possibly for a virtual computer) ©SoftMoore Consulting

Overview Tombstone Diagrams Program P expressed in language L (may be a machine language) Machine M S-to-T translator expressed in language L (may be a machine language) L P M L S  T ©SoftMoore Consulting

Examples: Tombstone Diagrams Overview Examples: Tombstone Diagrams Java C++  x86 Python sort C++ x86 MIPS ARM Power PC Java  JVM ©SoftMoore Consulting

Running Program P on Machine M Overview Running Program P on Machine M must match M P  x86 sort  SPARC ©SoftMoore Consulting

Compiling a Program must match M S  T S P T Overview ©SoftMoore Consulting

Example: Compiling and Executing a Program Overview Example: Compiling and Executing a Program Compile: Execute: x86 C++  x86 C++ sort ©SoftMoore Consulting

Overview Cross-Compiler A cross-compiler runs on one machine and produces target code for a different machine. The output of a cross-compiler must be downloaded to the target machine for execution. Commonly used for embedded systems download Power PC C++  ARM C++ sort ARM ©SoftMoore Consulting

Functionally equivalent to a C++-to-x86 compiler Overview Two-stage Compiler Functionally equivalent to a C++-to-x86 compiler x86 C++  C C++ sort C C  x86 ©SoftMoore Consulting

Using the Source Language as the Implementation Language It is common to write a compiler in the language being compiled; e.g., writing a C++ compiler in C++. Advantages The compiler itself provides a non-trivial test of the language being compiled. Only one language needs to be learned by compiler developers. Only one compiler needs to be maintained. If changes are made in the compiler to improve performance, then recompiling the compiler will improve compiler performance. For a new programming language, how do we write a compiler in that language? (chicken and egg problem) ©SoftMoore Consulting

Bootstrapping a Compiler Overview Bootstrapping a Compiler Problem: Suppose that we want to build a compiler for a programming language, say C#, that will run on machine M, and assume that we already have a compiler for a different language, say C, that runs on M. Furthermore, we desire that the source code for the C# compiler be C#. C# C#  M M C  M Want Have ©SoftMoore Consulting

Bootstrapping a Compiler: Step 1 Overview Bootstrapping a Compiler: Step 1 Start by selecting a subset of C# (C#/0) that is sufficiently complete for writing a compiler. Write a compiler for C#/0 in C and compile it. C C#/0  M M C  M Write this Compile it To get this ©SoftMoore Consulting

Bootstrapping a Compiler: Step 2 Overview Bootstrapping a Compiler: Step 2 Write another compiler for C#/0 in the language C#/0. Compile it using the compiler obtained from step 1. (At this point we no longer need for the C compiler.) C#/0 C#/0  M M Write this Compile using the compiler from step 1 To get this ©SoftMoore Consulting

Bootstrapping a Compiler: Step 3 Overview Bootstrapping a Compiler: Step 3 Write the full compiler for C# in C#/0. Compile it using the compiler obtained from step 2. C#/0 C#  M M C#/0  M Write this Compile it To get this ©SoftMoore Consulting

Efficiency Efficiency of a program Efficiency of a compiler speed Overview Efficiency Efficiency of a program speed use of memory Efficiency of a compiler efficiency of the compiler itself efficiency of the object code that it generates ©SoftMoore Consulting

Improving Efficiency of a Compiler Overview Improving Efficiency of a Compiler Suppose you have a compiler for a language (say C++) written in that language. If you modify the compiler to improve efficiency of the generated object code, then you can recompile the compiler to obtain a more efficient compiler. C++ C++  M M Rewritten to improve efficiency This version is more efficient than this version Compile it using existing compiler ©SoftMoore Consulting

Tombstone Diagram for an Interpreter Overview Tombstone Diagram for an Interpreter An interpreter for S expressed in language L (may be a machine language) Examples S L Basic Java Lisp x86 JVM x86-64 ©SoftMoore Consulting

Running an Interpreter Overview Running an Interpreter Functionally equivalent to a Basic machine; i.e., a machine that executes Basic commands in hardware Basic x86 Basic Basic x86 sort Example: ©SoftMoore Consulting

Writing/Executing a Java Program Overview Writing/Executing a Java Program Java P JVM P x86 Java  JVM Compile: Java Compiler x86 JVM P JVM x86 Execute: Java Virtual Machine (JVM) x86 ©SoftMoore Consulting

Compiler Project Source language: CPRL Overview Compiler Project Source language: CPRL Target language: CPRLVM/A, assembly language for the CPRL Virtual Machine (CPRLVM) You will write a CPRL-to-CPRLVM/A compiler in Java. I will provide a CPRLVM assembler. When you compile your compiler, you will have a CPRL-to-CPRLVM/A compiler that runs on a Java virtual machine. ©SoftMoore Consulting

Compiler Project (continued) Overview Compiler Project (continued) JVM CPRL  CPRLVM/A Java x86 Java  JVM You will write this compiler. Compiled version of your compiler Use the Java compiler to compile your CPRL compiler. ©SoftMoore Consulting

Compiler Project (continued) Overview Compiler Project (continued) Once your compiler is working, you can write test programs in CPRL and compile/assemble them. JVM x86 CRPL Hello CPRL  CPRLVM/A CPRLVM/A CPRLVM/A  CPRLVM CPRLVM HelloWorld in CPRL HelloWorld in CPRL assembly language HelloWorld in CPRLVM machine language Compiled version of your compiler Provided assembler ©SoftMoore Consulting

Compiler Project (continued) Overview Compiler Project (continued) I will provide a CPRLVM interpreter (emulator) that runs on the JVM (since I wrote it in Java). You can use the CPRLVM interpreter to execute programs compiled using your compiler and assembled using the CPRLVM assembler. CPRLVM JVM x86 Provided emulator Hello ©SoftMoore Consulting