Upload
egill-sigurdarson
View
226
Download
0
Tags:
Embed Size (px)
DESCRIPTION
Abstract Machines
Citation preview
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 bookProgramming Languages: Principles and Paradigmsby 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 isolatingimportant aspectsit is everywhere
in our livesin sciencein computer sciencein 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
Abstraction
mechanism to manage system complexity by isolatingimportant aspectsit is everywhere
in our livesin sciencein computer sciencein 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
Abstraction
mechanism to manage system complexity by isolatingimportant aspectsit is everywhere
in our livesin sciencein computer sciencein 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
Abstraction
mechanism to manage system complexity by isolatingimportant aspectsit is everywhere
in our livesin sciencein computer sciencein 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
Abstraction
mechanism to manage system complexity by isolatingimportant aspectsit is everywhere
in our livesin sciencein computer sciencein 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
Abstraction
mechanism to manage system complexity by isolatingimportant aspectsit is everywhere
in our livesin sciencein computer sciencein 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
Abstraction
mechanism to manage system complexity by isolatingimportant aspectsit is everywhere
in our livesin sciencein computer sciencein 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
Abstraction
mechanism to manage system complexity by isolatingimportant aspectsit is everywhere
in our livesin sciencein computer sciencein programming language
theory (formalisation of concepts)
practice (abstract data structure)
interpreters/compilers
Abstract machine(adding a level)
HierarchyDario 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 thatexecutes algorithms which are suitably formalised.
An abstract machine is nothing more than an abstraction ofthe concept of a physical computer.
An abstract machine permits step-by-step execution of aprogram.It is abstract because it omits the many details of real physicalmachines.
The algorithms we want to execute must be represented usingthe instructions of a programming language, L.The syntax of L allows us to use a given finite set ofconstructs, 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 datastructures and algorithms which can perform the storage andexecution of programs written in L.When we choose not to specify the language, L, we will simplytalk of the abstract machine, M, omitting the subscript.
Dario Della Monica Most of the slides are by Hrafn Loftsson
Abstract Machine: Structure
Memory
Program
DataSequencecontrol
Datacontrol
Memorymanagement
Operations
Interpreter
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 tothe language it is interpreting, L.Type of operations
1 Operations for processing primitive data;
2 Operations and data structures for controlling the sequence ofexecution of operations;
3 Operations and data structures for controlling data transfers;
4 Operations and data structures for memory management.
Dario Della Monica Most of the slides are by Hrafn Loftsson
Interpreter: Execution cycle
Store therisult
Execute OP1 Execute OP2 Execute OPn Execute HALT
Choose
Stop
Fetchoperands
Decode
Fetchnext instruction
Start
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 byMLs 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 electroniccomponents.
Let us call such a machine MHLH and let LH be its machinelanguage.
Parts:
MemoryLanguage LHInterpreter
Dario Della Monica Most of the slides are by Hrafn Loftsson
The Hardware Machine
Parts: Memory
Primary, secondary, cache, registers for storing data andprograms.
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 aparticular format.
The set of possible instructions depends on the particularphysical machine.
Dario Della Monica Most of the slides are by Hrafn Loftsson
The Hardware Machine
Parts: Interpreter
1 Operations: arithmetic and logical operations.
2 Sequence control: Program Counter (PC) register. Containsthe address of the next instruction to execute.
3 Data transfer/control: Specific registers interfacing with mainmemory.
4 Memory management depends on the specific architecture.
Simplest case: The program is loaded and immediately startsexecuting; it remains in memory until it terminates.Some form of multi-programming is almost alwaysimplemented. The execution of a program can be suspendedto 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 deviceswhich 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 thePC register) to be executed is retrieved from memory. Theinstruction an operation code and perhaps some operands is stored in the instruction register.
decode phase: the instruction stored in the instruction registeris decoded using special logic circuits. The operands areretrieved by data transfer operations using the address modesspecified 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 ofprograms written in L.An abstract machine therefore corresponds uniquely to alanguage, its machine language.
Conversely, given a programming language, L, there are manyabstract machines that have L as their machine language.
they differ from each other in the way in which the interpreteris implemented and in the data structures that they use;they all agree, though, on the language they interpretL.
To implement a programming language L meansimplementing an abstract machine which has L as its machinelanguage.
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 structuresand algorithms constituting ML.Advantage: the execution of programs in L will be fast.Disadvantages:
The constructs of a high-level language, L, are relativelycomplicated and very far from the elementary functionsprovided at the hardware level.Such a machine, once implemented, would be almostimpossible to modify. Modifications to L would be very costly.
When implementing ML in hardware, only low-levellanguages are used because their constructs are close to theoperations 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 byML using programs written in another language, L.Using Ls machine, ML , the machine ML can beimplemented using appropriate programs written in L
These programs interpret the constructs of L by simulating thefunctionality of ML.
Flexibility: The programs implementing the constructs of MLcan easily be changed.
Performance: Lower than the hardware implementation,because the implementation of ML uses another abstractmachine ML , 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 algorithmsfor ML in microcode(http://en.wikipedia.org/wiki/Microcode).
Similar to simulation in software ML is simulated usingprograms. In the case of firmware emulation, these programsare microprograms instead of programs in a high-levellanguage.
Microprograms use a special, very low-level language whichare stored in a special read-only memory instead of in mainmemory.
Performance: Can be executed at high speed.Flexibility: Modification of microcode is complicated andrequires 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 (thehost machine)
The implementation of L on the host machine MoLo takesplace using a translation from L to Lo.
Modes of implementations
1 purely interpreted implementation
2 purely compiled implementation
Dario Della Monica Most of the slides are by Hrafn Loftsson
Purely interpreted implementation
Program in L
Input data
Output datiInterpreter for Lwritten in LO
MO
Execution onMO
Figure: Purely interpreted implementation
A program is implemented in Lo which interprets all of Lsinstructions. This program is an interpreter, ILoL .
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 aprogram which implements a partial function:
ILoL : (ProgL D) D such that ILoL (PL, Input) = PL(Input)(1)
ProgL is the set of all possible programs that can be written in LPL is a program written in LD denotes the set of input and output dataD 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 adecoding procedure.
In order to execute an instruction of L, the interpreter ILoLuses a set of instructions in Lo which corresponds to aninstruction in language L.Not a real translation, because the code corresponding to aninstruction of L is executed, not output, by the interpreter.
Dario Della Monica Most of the slides are by Hrafn Loftsson
Purely compiled implementation
Programwritten in L
Compilerfrom L to LO
Programwritten in LO
Output data
Input data
Abstract macchineMA Host macchineMO
Execution onMA ExecutionMO
Figure: Purely compiled implementation
A program written in L is explicitly translated to a programwritten 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 afunction:
CL,Lo : ProgL ProgLo
such that, given a program PL, if
CL,Lo(PL) = PcLo , (2)
then, for every Input D:
PL(Input) = PcLo(Input) (3)
Dario Della Monica Most of the slides are by Hrafn Loftsson
Purely compiled implementation
Characteristics
L is called the source language, while Lo is called the objectlanguage.
To execute a program PL (written in L) on input data D,CL,Lo is executed with PL as input.This produces a compiled program PcLo as its output(written in Lo).Then PcLo can be executed on the machine MoLo supplyingit with input data D to obtain the desired result.
The translation phase (called compilation) is separate fromthe 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 ILoL must perform a decoding of Ls constructswhile it executes.As part of the time required for the execution of PL, it is alsonecessary to add in the time required to perform decoding.
Advantage: flexibility. Debugging tools can be developedeasily.
Advantage: An interpreter is simpler to develop than acompiler.
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 R2 then goto L2t r a n s l a t i o n o f C. . .R1 = R1 + 1goto L1
L2 : . . .
The interpreter does not generate the code starting at P2.The code describes the operations that the interpreter mustexecute 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 PcLo is more efficient than an interpretiveimplementation because the former does not have theoverhead of the instruction decoding phase.Decoding an instruction of language L is performed once bythe compiler, independent of the number of times thisinstruction occurs at runtime.
Disadvantage: The compilation approach loses all informationabout the structure of the source program.
When a runtime-error occurs, it can be difficult to determinewhich 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?
InterpretationAn interpreter often operates on an internal representation of aprogram which is different from the external one.The translation from the external notation of L to its internalrepresentation is performed using compilation from L to anintermediate language.The intermediate language is the one that is interpreted.
CompilationSome instructions for input/output are often translated intooperating system calls, which simulate at runtime (andtherefore interprets) the high-level instructions.
Dario Della Monica Most of the slides are by Hrafn Loftsson
The Real Case
Programwritten in L
Compilerfrom L to Li
Programwritten in Li
Programwritten in L
Compilerfrom L to Li
Programwritten in Li
Interpreter for Liwritten
in Lo or SRTOutput data
Input data
MA MO
Compilation onMAExecution onMO
Figure: The real case with an intermediate machine
The compiler CL,Li translates L to the intermediate language LiThe interpreter ILoLi runs on the machine MoLo (which simulatesthe machine MiLi )
Dario Della Monica Most of the slides are by Hrafn Loftsson
The Real Case
Different cases:
1 ML =MiLi : purely interpreted implementation.2 ML 6=MiLi 6=MoLo .
(a) If the interpreter of the intermediate machine is substantiallydifferent from the interpreter for MoLo , we have animplementation of an interpretative type.
(b) If the interpreter of the intermediate machine is similar as theinterpreter for MoLo (of which it extends some of itsfunctionality), we have a implementation of a compiled type.
3 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 arefurthest from the host machine language and to compile therest.
Compiled solutions are preferred when execution efficiency isdesired.
The interpreted approach is preferred when greater flexibilityis 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.
2 Implement (interpret) the intermediate language on thevarious platforms.
Portability
First time adopted by the Pascal language, using P-code asintermediate machine http://en.wikipedia.org/wiki/P-code_machine.
Used by Java, whose abstract (intermediate) machine is calledthe Java Virtual Machine and its corresponding machinelanguage 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 acompiled language
Why not?
One should, instead, talk of interpretative or compiledimplementations of a language.
Dario Della Monica Most of the slides are by Hrafn Loftsson
Abstract machines hierarchy in a micro-programmedcomputer
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
The Concepts of Abstract Machine and InterpreterImplementation of a Language