You are on page 1of 49

Programming Languages:

Abstract Machines
Dario Della Monica
School of Computer Science, Reykjavik University, Iceland

Most of the slides are by Hrafn Loftsson


based on the book
Programming Languages: Principles and Paradigms
by M. Gabbrielli and S. Martini (Springer 2010)

Dario Della Monica

Most of the slides are by Hrafn Loftsson

Abstraction
mechanism to manage system complexity by isolating
important aspects
it is everywhere
in
in
in
in

our lives
science
computer science
programming language

theory (formalisation of concepts)


practice (abstract data structure)

Dario Della Monica

Most of the slides are by Hrafn Loftsson

Abstraction
mechanism to manage system complexity by isolating
important aspects
it is everywhere
in
in
in
in

our lives
science
computer science
programming language

theory (formalisation of concepts)


practice (abstract data structure)

Dario Della Monica

Most of the slides are by Hrafn Loftsson

Abstraction
mechanism to manage system complexity by isolating
important aspects
it is everywhere
in
in
in
in

our lives
science
computer science
programming language

theory (formalisation of concepts)


practice (abstract data structure)

Dario Della Monica

Most of the slides are by Hrafn Loftsson

Abstraction
mechanism to manage system complexity by isolating
important aspects
it is everywhere
in
in
in
in

our lives
science
computer science
programming language

theory (formalisation of concepts)


practice (abstract data structure)

Dario Della Monica

Most of the slides are by Hrafn Loftsson

Abstraction
mechanism to manage system complexity by isolating
important aspects
it is everywhere
in
in
in
in

our lives
science
computer science
programming language

theory (formalisation of concepts)


practice (abstract data structure)

Dario Della Monica

Most of the slides are by Hrafn Loftsson

Abstraction
mechanism to manage system complexity by isolating
important aspects
it is everywhere
in
in
in
in

our lives
science
computer science
programming language

theory (formalisation of concepts)


practice (abstract data structure)

interpreters/compilers

Dario Della Monica

Most of the slides are by Hrafn Loftsson

Abstraction
mechanism to manage system complexity by isolating
important aspects
it is everywhere
in
in
in
in

our lives
science
computer science
programming language

theory (formalisation of concepts)


practice (abstract data structure)

interpreters/compilers
Abstract machine
(adding a level)

Dario Della Monica

Most of the slides are by Hrafn Loftsson

Abstraction
mechanism to manage system complexity by isolating
important aspects
it is everywhere
in
in
in
in

our lives
science
computer science
programming language

theory (formalisation of concepts)


practice (abstract data structure)

interpreters/compilers
Abstract machine
(adding a level)

Hierarchy
Dario Della Monica

Most of the slides are by Hrafn Loftsson

Outline

1 The Concepts of Abstract Machine and Interpreter

2 Implementation of a Language

Dario Della Monica

Most of the slides are by Hrafn Loftsson

Outline

1 The Concepts of Abstract Machine and Interpreter

2 Implementation of a Language

Dario Della Monica

Most of the slides are by Hrafn Loftsson

Abstract Machine
The Concept
An electronic, digital computer is a physical machine that
executes algorithms which are suitably formalised.
An abstract machine is nothing more than an abstraction of
the concept of a physical computer.
An abstract machine permits step-by-step execution of a
program.
It is abstract because it omits the many details of real physical
machines.

The algorithms we want to execute must be represented using


the instructions of a programming language, L.
The syntax of L allows us to use a given finite set of
constructs, called instructions, to construct programs.
Dario Della Monica

Most of the slides are by Hrafn Loftsson

Abstract Machine

Definition (Abstract Machine)


Assume that we are given a programming language, L.
An abstract machine for L, denoted by ML , is any set of data
structures and algorithms which can perform the storage and
execution of programs written in L.
When we choose not to specify the language, L, we will simply
talk of the abstract machine, M, omitting the subscript.

Dario Della Monica

Most of the slides are by Hrafn Loftsson

Abstract Machine: Structure

Interpreter

Data

Sequence
control

Memory

Operations

Data
control

Program
Memory
management

Figure: The structure of an abstract machine

Dario Della Monica

Most of the slides are by Hrafn Loftsson

The Interpreter

The interpreter must perform the operations that are specific to


the language it is interpreting, L.
Type of operations
1

Operations for processing primitive data;

Operations and data structures for controlling the sequence of


execution of operations;

Operations and data structures for controlling data transfers;

Operations and data structures for memory management.

Dario Della Monica

Most of the slides are by Hrafn Loftsson

Interpreter: Execution cycle


Start

Fetch
next instruction

Decode

Fetch
operands

Choose

Execute OP1

Execute OP2

Store the
risult

Execute OPn

Execute HALT

Stop

Figure: The execution cycle of a generic interpreter


Dario Della Monica

Most of the slides are by Hrafn Loftsson

Machine Language

Definition (Machine language)


Given an abstract machine, ML , the language L understood by
ML s interpreter is called the machine language of ML .

Dario Della Monica

Most of the slides are by Hrafn Loftsson

An Example of an Abstract Machine

Dario Della Monica

Most of the slides are by Hrafn Loftsson

An Example of an Abstract Machine

The Hardware Machine


Physically implemented using logic circuits and electronic
components.
Let us call such a machine MHLH and let LH be its machine
language.
Parts:
Memory
Language LH
Interpreter
Dario Della Monica

Most of the slides are by Hrafn Loftsson

The Hardware Machine

Parts: Memory
Primary, secondary, cache, registers for storing data and
programs.
Data divided into primitive types: integers, reals, chars.
All data represented as bits.

Dario Della Monica

Most of the slides are by Hrafn Loftsson

The Hardware Machine

Parts: Language LH
Simple instructions:
OpCode Operand1 Operand2
ADD
R5,
R0
ADD
(R5),
(R0)
Internal representation: Instructions are data stored in a
particular format.
The set of possible instructions depends on the particular
physical machine.

Dario Della Monica

Most of the slides are by Hrafn Loftsson

The Hardware Machine

Parts: Interpreter
1

Operations: arithmetic and logical operations.

Sequence control: Program Counter (PC) register. Contains


the address of the next instruction to execute.

Data transfer/control: Specific registers interfacing with main


memory.
Memory management depends on the specific architecture.

Simplest case: The program is loaded and immediately starts


executing; it remains in memory until it terminates.
Some form of multi-programming is almost always
implemented. The execution of a program can be suspended
to give the CPU to other programs.

Dario Della Monica

Most of the slides are by Hrafn Loftsson

The Hardware Machine

Parts: Interpreter
The interpreter is implemented as a set of physical devices
which comprise the Control Unit (CU)
Supports the execution of the fetch-decode-execute cycle.

Dario Della Monica

Most of the slides are by Hrafn Loftsson

The Hardware Machine

fetch-decode-execute cycle
fetch phase: the next instruction (whose address is kept in the
PC register) to be executed is retrieved from memory. The
instruction an operation code and perhaps some operands
is stored in the instruction register.
decode phase: the instruction stored in the instruction register
is decoded using special logic circuits. The operands are
retrieved by data transfer operations using the address modes
specified in the instruction.
execute phase: the primitive hardware operation is executed.
Storage is performed by means of data-transfer operations.

Dario Della Monica

Most of the slides are by Hrafn Loftsson

An Example of an Abstract Machine

Dario Della Monica

Most of the slides are by Hrafn Loftsson

Outline

1 The Concepts of Abstract Machine and Interpreter

2 Implementation of a Language

Dario Della Monica

Most of the slides are by Hrafn Loftsson

Implementation of a Language

ML is, by definition, a device which allows the execution of


programs written in L.
An abstract machine therefore corresponds uniquely to a
language, its machine language.
Conversely, given a programming language, L, there are many
abstract machines that have L as their machine language.
they differ from each other in the way in which the interpreter
is implemented and in the data structures that they use;
they all agree, though, on the language they interpretL.

To implement a programming language L means


implementing an abstract machine which has L as its machine
language.

Dario Della Monica

Most of the slides are by Hrafn Loftsson

Implementation of an abstract machine

Three options:
implementation in hardware;
simulation using software;
simulation (emulation) using firmware.

Dario Della Monica

Most of the slides are by Hrafn Loftsson

Implementation of an abstract machine


Implementation of ML in hardware
Sufficient to implement in the hardware the data structures
and algorithms constituting ML .
Advantage: the execution of programs in L will be fast.
Disadvantages:
The constructs of a high-level language, L, are relatively
complicated and very far from the elementary functions
provided at the hardware level.
Such a machine, once implemented, would be almost
impossible to modify. Modifications to L would be very costly.

When implementing ML in hardware, only low-level


languages are used because their constructs are close to the
operations of physical devices.
Dario Della Monica

Most of the slides are by Hrafn Loftsson

Implementation of an abstract machine


Simulation of ML using software
Implementing the data structures and algorithms required by
ML using programs written in another language, L0 .
Using L0 s machine, M0L0 , the machine ML can be
implemented using appropriate programs written in L0
These programs interpret the constructs of L by simulating the
functionality of ML .

Flexibility: The programs implementing the constructs of ML


can easily be changed.
Performance: Lower than the hardware implementation,
because the implementation of ML uses another abstract
machine M0L0 , which must be implemented!
Dario Della Monica

Most of the slides are by Hrafn Loftsson

Implementation of an abstract machine


Emulation of ML using firmware
Simulation/emulation of the data structures and algorithms
for ML in microcode
(http://en.wikipedia.org/wiki/Microcode).
Similar to simulation in software ML is simulated using
programs. In the case of firmware emulation, these programs
are microprograms instead of programs in a high-level
language.
Microprograms use a special, very low-level language which
are stored in a special read-only memory instead of in main
memory.
Performance: Can be executed at high speed.
Flexibility: Modification of microcode is complicated and
requires special hardware to re-write the memory.
Dario Della Monica

Most of the slides are by Hrafn Loftsson

Implementation: The Ideal case


Assumptions
We want to implement L i.e. an abstract machine, ML .
We exclude direct implementation in hardware.
For the implementation of ML , we have available MoLo (the
host machine)
The implementation of L on the host machine MoLo takes
place using a translation from L to Lo.
Modes of implementations
1

purely interpreted implementation

purely compiled implementation


Dario Della Monica

Most of the slides are by Hrafn Loftsson

Purely interpreted implementation

Program in L
Interpreter for L
written in LO

Output dati

Execution on M O

Input data

MO

Figure: Purely interpreted implementation

A program is implemented in Lo which interprets all of Ls


instructions. This program is an interpreter, ILLo .
Dario Della Monica

Most of the slides are by Hrafn Loftsson

Purely interpreted implementation

Definition (Interpreter)
An interpreter for language L, written in language Lo, is a
program which implements a partial function:
ILLo : (Prog L D) D

such that ILLo (P L , Input) = P L (Input)


(1)

Prog L is the set of all possible programs that can be written in L


P L is a program written in L
D denotes the set of input and output data
D D is input data

Dario Della Monica

Most of the slides are by Hrafn Loftsson

Purely interpreted implementation

Characteristics
Programs in L are not explicitly translated there is only a
decoding procedure.
In order to execute an instruction of L, the interpreter ILLo
uses a set of instructions in Lo which corresponds to an
instruction in language L.
Not a real translation, because the code corresponding to an
instruction of L is executed, not output, by the interpreter.

Dario Della Monica

Most of the slides are by Hrafn Loftsson

Purely compiled implementation

Input data

Program
written in L

Program
written in LO

Compiler
from L to LO
Execution on M A
Abstract macchine M A

Output data

Execution M O
Host macchine M O

Figure: Purely compiled implementation

A program written in L is explicitly translated to a program


written in Lo.
The translation is performed by a program called compiler, CL,Lo
Dario Della Monica

Most of the slides are by Hrafn Loftsson

Purely compiled implementation

Definition (Compiler)
A compiler from L to Lo is a program which implements a
function:
CL,Lo : Prog L Prog Lo
such that, given a program P L , if
CL,Lo (P L ) = Pc Lo ,

(2)

then, for every Input D:


P L (Input) = Pc Lo (Input)

Dario Della Monica

Most of the slides are by Hrafn Loftsson

(3)

Purely compiled implementation

Characteristics
L is called the source language, while Lo is called the object
language.
To execute a program P L (written in L) on input data D,
CL,Lo is executed with P L as input.
This produces a compiled program Pc Lo as its output
(written in Lo).
Then Pc Lo can be executed on the machine MoLo supplying
it with input data D to obtain the desired result.
The translation phase (called compilation) is separate from
the execution phase.

Dario Della Monica

Most of the slides are by Hrafn Loftsson

Comparing the two modes of implementations

Interpreted implementation
Disadvantage: low efficiency.
The interpreter ILLo must perform a decoding of Ls constructs
while it executes.
As part of the time required for the execution of P L , it is also
necessary to add in the time required to perform decoding.

Advantage: flexibility. Debugging tools can be developed


easily.
Advantage: An interpreter is simpler to develop than a
compiler.

Dario Della Monica

Most of the slides are by Hrafn Loftsson

Comparing the two modes of implementations


Example
P1 : f o r ( i = 1 , i <=n , i=i +1) C ;
P2 :
R1 = 1
R2 = n
L1 : i f R1 > R2 t h e n g o t o L2
t r a n s l a t i o n of C
...
R1 = R1 + 1
g o t o L1
L2 : . . .

The interpreter does not generate the code starting at P2.


The code describes the operations that the interpreter must
execute at runtime once it has decoded the for command.
Dario Della Monica

Most of the slides are by Hrafn Loftsson

Comparing the two modes of implementations

Compiled implementation
Advantage: high efficiency.
The execution of Pc Lo is more efficient than an interpretive
implementation because the former does not have the
overhead of the instruction decoding phase.
Decoding an instruction of language L is performed once by
the compiler, independent of the number of times this
instruction occurs at runtime.

Disadvantage: The compilation approach loses all information


about the structure of the source program.
When a runtime-error occurs, it can be difficult to determine
which source-program command caused it.
More difficult to implement debugging tools.

Dario Della Monica

Most of the slides are by Hrafn Loftsson

The Real Case

What happens in practice?


Interpretation
An interpreter often operates on an internal representation of a
program which is different from the external one.
The translation from the external notation of L to its internal
representation is performed using compilation from L to an
intermediate language.
The intermediate language is the one that is interpreted.

Compilation
Some instructions for input/output are often translated into
operating system calls, which simulate at runtime (and
therefore interprets) the high-level instructions.

Dario Della Monica

Most of the slides are by Hrafn Loftsson

The Real Case

Input data

Program
written in L

Compiler
from L to Li

Program
written in Li
Interpreter for Li
written
in Lo or SRT

Program
written in L

Compiler
from L to Li

Program
written in Li

Output data

Execution on M O

Compilation on M A
MA

MO

Figure: The real case with an intermediate machine

The compiler CL,Li translates L to the intermediate language Li


Lo runs on the machine Mo
The interpreter ILi
Lo (which simulates
the machine MiLi )
Dario Della Monica

Most of the slides are by Hrafn Loftsson

The Real Case

Different cases:
1
2

ML = MiLi : purely interpreted implementation.


ML 6= MiLi 6= MoLo .
(a) If the interpreter of the intermediate machine is substantially
different from the interpreter for MoLo , we have an
implementation of an interpretative type.
(b) If the interpreter of the intermediate machine is similar as the
interpreter for MoLo (of which it extends some of its
functionality), we have a implementation of a compiled type.

MiLi = MoLo : purely compiled implementation.

Dario Della Monica

Most of the slides are by Hrafn Loftsson

The Real Case

In practice:
We tend to interpret those language constructs which are
furthest from the host machine language and to compile the
rest.
Compiled solutions are preferred when execution efficiency is
desired.
The interpreted approach is preferred when greater flexibility
is required.

Dario Della Monica

Most of the slides are by Hrafn Loftsson

The Real Case Interpretive Compilers


Implementing a language on many platforms
1

Compile the programs to an intermediate language.

Implement (interpret) the intermediate language on the


various platforms.

Portability
First time adopted by the Pascal language, using P-code as
intermediate machine
http://en.wikipedia.org/wiki/P-code_machine.
Used by Java, whose abstract (intermediate) machine is called
the Java Virtual Machine and its corresponding machine
language is Java Byte Code.
http://en.wikipedia.org/wiki/Java_bytecode
Dario Della Monica

Most of the slides are by Hrafn Loftsson

The Real Case

Important note
One should not talk about an interpreted language or a
compiled language
Why not?
One should, instead, talk of interpretative or compiled
implementations of a language.

Dario Della Monica

Most of the slides are by Hrafn Loftsson

Abstract machines hierarchy in a micro-programmed


computer

Software
Firmware
Hardware

Dario Della Monica

Most of the slides are by Hrafn Loftsson

A more complex hierarchy

Dario Della Monica

Most of the slides are by Hrafn Loftsson

You might also like