CS1Q Computer Systems Lecture 14 Simon Gay. Lecture 14CS1Q Computer Systems - Simon Gay2 Where we are Global computing: the Internet Networks and distributed.

Slides:



Advertisements
Similar presentations
CS1Q Computer Systems Lecture 14
Advertisements

compilers and interpreters
Computer Concepts 5th Edition Parsons/Oja Page 546 CHAPTER 11 Software Engineering Section A PARSONS/OJA Computer Programming.
1 Lecture 1  Getting ready to program  Hardware Model  Software Model  Programming Languages  The C Language  Software Engineering  Programming.
T-diagrams “Mommy, where do compilers come from?” Adapted from:
Introduction to Computer Programming CSC 1401: Introduction to Programming with Java Lecture 2 Wanda M. Kunkle.
1 Programming Languages Translation  Lecture Objectives:  Be able to list and explain five features of the Java programming language.  Be able to explain.
1 Programming & Programming Languages Overview l Machine operations and machine language. l Example of machine language. l Different types of processor.
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 1, Lab.
CS 104 Introduction to Computer Science and Graphics Problems Software and Programming Language (2) Programming Languages 09/26/2008 Yang Song (Prepared.
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.
CEN 226: Computer Organization & Assembly Language :CSC 225 (Lec#1) By Dr. Syed Noman.
CSE 1301 J Lecture 2 Intro to Java Programming Richard Gesick.
COMP Computer Basics Yi Hong May 13, 2015.
CHAPTER 4: INTRODUCTION TO COMPUTER ORGANIZATION AND PROGRAMMING DESIGN Lec. Ghader Kurdi.
P51UST: Unix and Software Tools Unix and Software Tools (P51UST) Compilers, Interpreters and Debuggers Ruibin Bai (Room AB326) Division of Computer Science.
ITEC 352 Lecture 11 ISA - CPU. ISA (2) Review Questions? HW 2 due on Friday ISA –Machine language –Buses –Memory.
CS 355 – Programming Languages
CS1Q Computer Systems Lecture 3 Simon Gay. Lecture 3CS1Q Computer Systems - Simon Gay2 Where we are Global computing: the Internet Networks and distributed.
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.
How Java Programs Work MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation ©Akhilesh.
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 Computers and Programming – Computer Programming.
Cosc 2150: Computer Organization
Computer Programming A program is a set of instructions a computer follows in order to perform a task. solve a problem Collectively, these instructions.
Language processors (Chapter 2) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.
Copyright © 2007 Addison-Wesley. All rights reserved.1-1 Reasons for Studying Concepts of Programming Languages Increased ability to express ideas Improved.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 7 OS System Structure.
1 Text Reference: Warford. 2 Computer Architecture: The design of those aspects of a computer which are visible to the programmer. Architecture Organization.
CS 614: Theory and Construction of Compilers Lecture 7 Fall 2002 Department of Computer Science University of Alabama Joel Jones.
CS 614: Theory and Construction of Compilers Lecture 8 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
CS1Q Computer Systems Lecture 3 Simon Gay. Lecture 3CS1Q Computer Systems - Simon Gay2 Where we are Global computing: the Internet Networks and distributed.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
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 
 Programming - the process of creating computer programs.
By: Cheryl Mok & Sarah Tan. Java is partially interpreted. 1. Programmer writes a program in textual form 2. Runs the compiler, which converts the textual.
Introduction to OOP CPS235: Introduction.
1 Languages and Compilers (SProg og Oversættere) Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm Hutchinson.
OCR A Level F453: The function and purpose of translators Translators a. describe the need for, and use of, translators to convert source code.
Computer Programming Week 1: The Basics of CP 1 st semester 2012 School of Information Technology Website:
Programming 2 Intro to Java Machine code Assembly languages Fortran Basic Pascal Scheme CC++ Java LISP Smalltalk Smalltalk-80.
Introduction to Computer Programming Concepts M. Uyguroğlu R. Uyguroğlu.
Software Engineering Algorithms, Compilers, & Lifecycle.
Evolution and History of Programming Languages
Advanced Computer Systems
Assembler, Compiler, MIPS simulator
14 Compilers, Interpreters and Debuggers
Overview of Compilers and Language Translation
Compiler, Interpreter, and Bootstrapping
Computational Thinking, Problem-solving and Programming: General Principals IB Computer Science.
PROGRAMMING LANGUAGES
Programming Language Hierarchy, Phases of a Java Program
CSCI-235 Micro-Computer Applications
Lecture 1: Introduction to JAVA
Why to use the assembly and why we need this course at all?
Microprocessor and Assembly Language
Java programming lecture one
2.1. Compilers and Interpreters
Teaching Computing to GCSE
Programming Languages
Lesson Objectives Aims Key Words Compiler, interpreter, assembler
Introduction CSC 111.
CMP 131 Introduction to Computer Programming
Programming Languages
Lecture 4: Instruction Set Design/Pipelining
Programming language translators
Presentation transcript:

CS1Q Computer Systems Lecture 14 Simon Gay

Lecture 14CS1Q Computer Systems - Simon Gay2 Where we are Global computing: the Internet Networks and distributed computing Application on a single computer Operating System Architecture Digital Logic Electronics Physics compilers and interpreters

Lecture 14CS1Q Computer Systems - Simon Gay3 High Level Languages Most programming is done in high-level languages, such as Python: their syntax is easier for humans to read and write the programming style is close to the way we think about problems, rather than close to the structure of the CPU we don’t need to think about details such as register allocation and memory allocation programs are not specific to a particular design of CPU How do we bridge the gap between high-level languages and the CPU?

Lecture 14CS1Q Computer Systems - Simon Gay4 Compilers and Interpreters There are two basic approaches: compilation and interpretation. A compiler translates a program written in a high-level language (for example, C) into a program written in assembly language or machine language for a particular CPU (for example, Pentium). This translation produces an independent, self-sufficient machine language program, which can be executed without reference to the original program or to the compiler. We have seen how to systematically translate some simple aspects of high level language into assembly language.

Lecture 14CS1Q Computer Systems - Simon Gay5 Compilers and Interpreters An interpreter analyses a high-level language program one statement at a time, and for each statement, carries out operations which produce the desired effect. For example, if an Ada interpreter encounters the statement x := 5; then it must work out where in memory the variable x is stored, and put the value 5 in that location. If it encounters the statement write(x); then it must find the value of x in memory, and cause that value to be displayed on the screen.

Lecture 14CS1Q Computer Systems - Simon Gay6 Compilers and Interpreters: Analogy On holiday in Norway, I want to buy a sandwich for lunch. I do not speak Norwegian. I have two possible strategies: 1. Find someone who speaks English, and ask them to teach me how to ask for a sandwich in Norwegian. I can then buy a sandwich every day. This is like using a compiler. 2. Find someone who speaks English, and ask them to go into the shop and buy a sandwich for me. Every day I need to find another English speaker and repeat the process. I never find out how to ask for a sandwich in Norwegian. This is like using an interpreter.

Lecture 14CS1Q Computer Systems - Simon Gay7 Compilers and Interpreters: Comparison The compilation process takes time, but the compiled program runs quickly. With an interpreter, the overhead of compilation is avoided, but the program runs more slowly. Traditionally, compilers are used for development of software which will be used many times and which must be efficient. Interpreters are used for simpler languages, more suited for quick programming tasks (e.g. scripting languages). An interpreter supports a more flexible style of program development, e.g. for prototyping.

Lecture 14CS1Q Computer Systems - Simon Gay8 Compilers and Interpreters The C programming language has a compiler. Typically, there are three parts to the compilatoins process: “Compile”, “Link” and “Run”. “Compile” translates your program into machine language. Often you get error messages; these are errors that can be detected without running the program. “Link” combines your program with any libraries that it uses, to produce a single executable file. “Run” executes (the machine language form of) your program. You have to compile and build first.

Lecture 14CS1Q Computer Systems - Simon Gay9 Compilers and Interpreters The IT Machine system in the lab combines a compiler and an interpreter. “Assemble” translates your assembly language program into machine language. This is a (very simple) compilation process. “Run” executes the machine language program, one instruction at a time. This is interpretation. The IT Machine system is a program which looks at the machine language program and simulates the effect of each instruction.

Lecture 14CS1Q Computer Systems - Simon Gay10 Tombstone Diagrams We can use tombstone diagrams to represent programs, compilers, and interpreters, and to keep track of the fact that three languages are involved in the compilation process: The source language is the language being compiled: e.g. in an C compiler, the source language is C. The target language is the language produced by the compiler: e.g. Pentium machine language, or JVM instructions. The implementation language is the language in which the compiler is written (remember that a compiler is just a program). This is not relevant to a user of the compiler, but is significant to compiler developers.

Lecture 14CS1Q Computer Systems - Simon Gay11 Tombstone Diagrams The following diagram represents a program called P, expressed in a language called L (the implementation language). P L Examples: sort Java sort x86 graph C A program called sort, expressed in Java. A program called sort, expressed in x86 machine language (i.e. for Intel CPUs). A program called graph, expressed in Ada.

Lecture 14CS1Q Computer Systems - Simon Gay12 Tombstone Diagrams: Machines A machine that executes machine language M is represented like this: M Examples: x86PPC SPARC An x86 machine (e.g. Pentium 4 PC) A Power PC (PPC) machine (e.g. Mac) A SPARC machine (e.g. Sun workstation)

Lecture 14CS1Q Computer Systems - Simon Gay13 Tombstone Diagrams: Execution A program can run on a machine only if it is expressed in the correct machine language. Here’s how we represent this: P M M must match Examples: sort x86 sort PPC sort PPC x86 sort Java x86 OK NO

Lecture 14CS1Q Computer Systems - Simon Gay14 Tombstone Diagrams: Compilers A compiler or translator is represented by this diagram: ST L source language target language implementation language Examples: Adax86 C Adax86 JavaC C++ x86 ass.x86

Lecture 14CS1Q Computer Systems - Simon Gay15 The Compilation Process ST M P S M P T must match generated Example: sort C Cx86 sort x86 source program object program

Lecture 14CS1Q Computer Systems - Simon Gay16 Exercise Imagine that we have: a program called TSP, written in the high-level language Pascal a Pascal compiler, running on a G4 microprocessor (e.g. in an iMac), which produces object code for the G4 Draw diagrams showing how TSP can be compiled and executed.

Lecture 14CS1Q Computer Systems - Simon Gay17 Solution TSP Pascal G4 TSP G4 compile execute

Lecture 14CS1Q Computer Systems - Simon Gay18 Cross-Compilation A cross-compiler runs on one machine (the host machine) but generates code for a different machine (the target machine). Example: sort C CPS2 x86 PS2 sort PS2 download Examples: software development for video games or mobile phones; embedded computing applications (e.g. software for a car engine management computer).

Lecture 14CS1Q Computer Systems - Simon Gay19 Two-Stage Compilation An S-into-T translator and a T-into-U translator can be composed to make a two-stage S-into-U translator. Example: sort C C Java x86 Java x86 sort x86

Lecture 14CS1Q Computer Systems - Simon Gay20 Compiling a Compiler Suppose we have an Java-into-x86 compiler, expressed in C. We cannot run this compiler, because it is not expressed in machine language. But we can treat it as an ordinary source program to be translated by a C-into-x86 compiler: Javax86 C C Javax86 The object program is an Ada-into-x86 compiler expressed in x86 machine language, so it can be used as in the example on Slide 13.

Lecture 14CS1Q Computer Systems - Simon Gay21 Interpreters To represent an interpreter, we show the source language S at the top and the implementation language L at the bottom. S L Examples: Basic x86 Basic Java SQL x86

Lecture 14CS1Q Computer Systems - Simon Gay22 Using an Interpreter P S S M M must match The combination of an interpreter expressed in M machine language, and a machine M, behaves like a machine which can execute the source language S. It is an abstract or virtual machine. Examples: chess Basic x86 chess Lisp Basic x86 NO

Lecture 14CS1Q Computer Systems - Simon Gay23 Interpretive Compilers:A Hybrid Combine compilation and interpretation: compile a high-level language into an intermediate language, often known as bytecodes interpret the bytecode program This is the approach used by Java, to achieve portability. Java program Java bytecode program JVM on machine X JVM on machine Y JVM on machine Z Java compiler interpreted by

Lecture 14CS1Q Computer Systems - Simon Gay24 Interpretive Compilation of Java PP Java JVM x86 JVM P x86 JVM x86

Lecture 14CS1Q Computer Systems - Simon Gay25 Virtual Machines Java bytecode programs consist of instructions for the JVM (Java virtual machine). The JVM is a CPU which is implemented in software rather than hardware. Advantages: “write once, run anywhere” - just need a JVM implementation for each type of computer, and a standard Java compiler. possibility of compiling other languages to JVM code Disadvantages: interpreting a JVM program is slower than executing a truly compiled program.

Lecture 14CS1Q Computer Systems - Simon Gay26 Refinements Executing Java programs via the JVM leads to relatively poor performance, so some refinements of the idea have been developed. Native code compilers compile Java into real machine language, for greater speed; of course, a different Java compiler is needed for each CPU. Just in time (JIT) compilers interpret JVM instructions, but also compile them into native code. If a section of a program is executed more than once (e.g. in a loop) then the compiled version is used. Some of the efficiency of compiled code is achieved, without the initial cost. (But it is difficult for a JIT compiler to do as much optimisation as a conventional compiler.)

Lecture 14CS1Q Computer Systems - Simon Gay27 Levels of Compilation It’s not always straightforward to say whether a language is being compiled or interpreted. Java is compiled into JVM instructions which are then interpreted. If a language is compiled into machine language, the individual machine language instructions are interpreted by the CPU. In a sense, true compilation consists of translating a high level language program into a circuit, which directly carries out the desired function. Compilation to hardware is an active research area. What we mean by a machine language is a language for which a hardware interpreter is available.