cs1q computer systems lecture 14 simon gay. lecture 14cs1q computer systems - simon gay2 where we...

27
CS1Q Computer Systems Lecture 14 Simon Gay

Upload: calvin-mckenzie

Post on 12-Jan-2016

214 views

Category:

Documents


1 download

TRANSCRIPT

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

CS1Q Computer SystemsLecture 14

Simon Gay

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

Lecture 14 CS1Q Computer Systems - Simon Gay 2

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

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

Lecture 14 CS1Q Computer Systems - Simon Gay 3

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?

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

Lecture 14 CS1Q Computer Systems - Simon Gay 4

Compilers and InterpretersThere 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 ormachine language for a particular CPU (for example, Pentium).

This translation produces an independent, self-sufficient machinelanguage program, which can be executed without reference to theoriginal program or to the compiler.

We have seen how to systematically translate some simple aspects ofhigh level language into assembly language.

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

Lecture 14 CS1Q Computer Systems - Simon Gay 5

Compilers and InterpretersAn interpreter analyses a high-level language program one statementat a time, and for each statement, carries out operations which producethe 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, andput 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 bedisplayed on the screen.

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

Lecture 14 CS1Q Computer Systems - Simon Gay 6

Compilers and Interpreters: AnalogyOn holiday in Norway, I want to buy a sandwich for lunch. I do notspeak Norwegian. I have two possible strategies:

1. Find someone who speaks English, and ask them to teach me how toask 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 shopand buy a sandwich for me. Every day I need to find another Englishspeaker and repeat the process. I never find out how to ask for asandwich in Norwegian. This is like using an interpreter.

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

Lecture 14 CS1Q Computer Systems - Simon Gay 7

Compilers and Interpreters: ComparisonThe compilation process takes time, but the compiled program runsquickly. With an interpreter, the overhead of compilation is avoided,but the program runs more slowly.

Traditionally, compilers are used for development of software whichwill be used many times and which must be efficient. Interpreters areused 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.

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

Lecture 14 CS1Q Computer Systems - Simon Gay 8

Compilers and InterpretersThe 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 youget error messages; these are errors that can be detected withoutrunning the program.

“Link” combines your program with any libraries that it uses, toproduce a single executable file.

“Run” executes (the machine language form of) your program. Youhave to compile and build first.

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

Lecture 14 CS1Q Computer Systems - Simon Gay 9

Compilers and InterpretersThe IT Machine system in the lab combines a compiler and aninterpreter.

“Assemble” translates your assembly language program intomachine language. This is a (very simple) compilation process.

“Run” executes the machine language program, one instruction ata time. This is interpretation. The IT Machine system is a programwhich looks at the machine language program and simulates theeffect of each instruction.

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

Lecture 14 CS1Q Computer Systems - Simon Gay 10

Tombstone DiagramsWe can use tombstone diagrams to represent programs, compilers,and interpreters, and to keep track of the fact that three languagesare involved in the compilation process:

The source language is the language being compiled: e.g. in anC 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 compileris written (remember that a compiler is just a program). This is notrelevant to a user of the compiler, but is significant to compilerdevelopers.

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

Lecture 14 CS1Q Computer Systems - Simon Gay 11

Tombstone DiagramsThe following diagram represents a program called P, expressed in alanguage 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 machinelanguage (i.e. for Intel CPUs).

A program called graph,expressed in Ada.

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

Lecture 14 CS1Q Computer Systems - Simon Gay 12

Tombstone Diagrams: MachinesA machine that executes machine language M is represented like this:

M

Examples:

x86 PPC SPARC

An x86 machine(e.g. Pentium 4 PC)

A Power PC (PPC)machine (e.g. Mac)

A SPARC machine(e.g. Sun workstation)

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

Lecture 14 CS1Q Computer Systems - Simon Gay 13

Tombstone Diagrams: ExecutionA program can run on a machine only if it is expressed in the correctmachine language. Here’s how we represent this:

P

M

Mmust match

Examples:

sort

x86

x86

sort

PPC

PPC

sort

PPC

x86

sort

Java

x86OK OK NO NO

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

Lecture 14 CS1Q Computer Systems - Simon Gay 14

Tombstone Diagrams: CompilersA compiler or translator is represented by this diagram:

S T

L

sourcelanguage

targetlanguage

implementationlanguageExamples:

Ada x86

C

Ada x86

x86

Java C

C++

x86 ass. x86

x86

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

Lecture 14 CS1Q Computer Systems - Simon Gay 15

The Compilation Process

S T

M

P

S

M

P

T

must match must match

generated

Example:

sort sort

C C x86

x86

x86

x86

sort

x86

x86

source programobject program

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

Lecture 14 CS1Q Computer Systems - Simon Gay 16

ExerciseImagine 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.

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

Lecture 14 CS1Q Computer Systems - Simon Gay 17

Solution

TSP TSP

Pascal Pascal G4

G4

G4

G4

TSP

G4

G4

compile execute

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

Lecture 14 CS1Q Computer Systems - Simon Gay 18

Cross-CompilationA cross-compiler runs on one machine (the host machine) but generatescode for a different machine (the target machine).

Example:

sort sort

C C PS2

x86

x86

PS2

sort

PS2

PS2download

Examples: software development for video games or mobile phones;embedded computing applications (e.g. software for a car enginemanagement computer).

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

Lecture 14 CS1Q Computer Systems - Simon Gay 19

Two-Stage CompilationAn S-into-T translator and a T-into-U translator can be composed tomake a two-stage S-into-U translator.

Example:

sort sort

C C Java

x86

x86

Java Java x86

x86

x86

sort

x86

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

Lecture 14 CS1Q Computer Systems - Simon Gay 20

Compiling a CompilerSuppose we have an Java-into-x86 compiler, expressed in C. We cannotrun this compiler, because it is not expressed in machine language. Butwe can treat it as an ordinary source program to be translated by aC-into-x86 compiler:

Java x86

C C x86

x86

Java x86

x86

x86

The object program is an Ada-into-x86 compiler expressed in x86machine language, so it can be used as in the example on Slide 13.

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

Lecture 14 CS1Q Computer Systems - Simon Gay 21

InterpretersTo represent an interpreter, we show the source language S at the topand the implementation language L at the bottom.

S

L

Examples:

Basic

x86

Basic

Java

SQL

x86

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

Lecture 14 CS1Q Computer Systems - Simon Gay 22

Using an InterpreterP

S

S

M

M

must match

must match

The combination of an interpreterexpressed in M machine language,and a machine M, behaves like amachine which can execute thesource language S. It is anabstract or virtual machine.

Examples: chess

Basic

Basic

x86

x86

chess

Lisp

Basic

x86

x86

NO

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

Lecture 14 CS1Q Computer Systems - Simon Gay 23

Interpretive Compilers:A HybridCombine compilation and interpretation:• compile a high-level language into an intermediate language, often known as bytecodes• interpret the bytecode programThis is the approach used by Java, to achieve portability.

Javaprogram

Java bytecodeprogram

JVM onmachine X

JVM onmachine Y

JVM onmachine Z

Javacompiler

interpreted by

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

Lecture 14 CS1Q Computer Systems - Simon Gay 24

Interpretive Compilation of Java

P P

Java Java JVM

x86

x86

JVM

P

JVM

x86

JVM

x86

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

Lecture 14 CS1Q Computer Systems - Simon Gay 25

Virtual MachinesJava bytecode programs consist of instructions for the JVM (Java virtual machine). The JVM is a CPU which is implemented insoftware 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 atruly compiled program.

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

Lecture 14 CS1Q Computer Systems - Simon Gay 26

RefinementsExecuting Java programs via the JVM leads to relatively poorperformance, so some refinements of the idea have been developed.

Native code compilers compile Java into real machine language, forgreater speed; of course, a different Java compiler is needed for eachCPU.

Just in time (JIT) compilers interpret JVM instructions, but also compilethem into native code. If a section of a program is executed more thanonce (e.g. in a loop) then the compiled version is used. Some of theefficiency of compiled code is achieved, without the initial cost. (But itis difficult for a JIT compiler to do as much optimisation as aconventional compiler.)

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

Lecture 14 CS1Q Computer Systems - Simon Gay 27

Levels of CompilationIt’s not always straightforward to say whether a language is beingcompiled or interpreted.

Java is compiled into JVM instructions which are then interpreted.

If a language is compiled into machine language, the individualmachine language instructions are interpreted by the CPU.

In a sense, true compilation consists of translating a high level languageprogram 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 ahardware interpreter is available.