Sunteți pe pagina 1din 42

Nawroz University College Computer Science Department of Computer Science Stage Second

Programming Languages
Lecture 3

Topic
Review Compilation Interpretation Assembler Compilation Phases Machine-dependent and Machine-independent Summary

virtual machine (VM)


A virtual machine (VM) is an environment, usually a program or operating system. which does not physically exist but is created within another environment.

virtual machine (VM)


In this context, a VM is called a "guest" while the environment it runs within is called a "host.

virtual machine (VM)


A virtual machine (VM) is a software implementation of a machine (i.e. a computer) that executes programs like a physical machine. The virtual machine idea is itself one of the most elegant in the history of technology and is a step in the evolution of ideas about software.

virtual machine categories


Virtual machines are separated into two major categories. 1. system virtual machine. 2. process virtual machine.

System virtual machines


A system virtual machine provides a complete system platform which supports the execution of a complete operating system (OS). multiple OS environments can co-exist on the same computer, in strong isolation from each other

System virtual machine

Process virtual machines


A process VM, sometimes called an application virtual machine, runs as a normal application inside a host OS and supports a single process.

Process virtual machines


Its purpose is to provide a platformindependent programming environment that abstracts away details of the underlying hardware or operating system, and allows a program to execute in the same way on any platform.

Process virtual machines


VM has become popular with the Java programming language, which is implemented using the Java virtual machine. NET Framework, which runs on a VM called the Common Language Runtime (CLR).

Java Virtual Machine (JVM)


The Java programming language does not rely on platform specific instruction sets.

Java virtual machine

Compilation and Interpretation

Overview of Compilation
program gcd(input, output); var i, j: integer; begin

read(i, j);
while i <> j do if i > j then i := i j; else j := j i; writeln(i) end.

Compilation

Compilation
A Compiler is a program that reads a program written in one language (the source language) and translates it into an equivalent program in another language (the target language).
Machine code [a = a + b ;] [1000101001111]

Interpretation
Interpreters: A program that translates a users high-level program (Source Program) into machine program ( Target Program) at the same time.

Assembler
A program that converts a program written in assembly language into machine code.

Assembly Language

Assembler

Low Level Language Machine Code

#include<iostream.h> Void main() {int a; cin>>a; cout<<a;}

Source Program Preprocessor Modified Source Program Compiler Target Assembly Program Assembler Relocatable Machine Code Loader / Linker-editor Absolute Machine Code Library , Relocatable Object Files

Language
Processing System

Compilation Phases

Phase 1- Lexical Analysis:


In a compiler, linear analysis is called lexical analysis or scanning. Example

position = initial + rate * 60

would be grouped into the following tokens:

position = initial + rate * 60;


Lexeme Token

Position
= initial

The Identifier position.


The Assignment Symbol The Identifier Initial.

+
rate * 60 ;

The Plus Sign


The Identifier Rate. The Multiplication Sign The Number 60 The Semi-colon Symbol

Phase 2- Syntax Analysis:


Hierarchical analysis is called parsing or syntax analysis, it involves grouping the tokens of the source program into grammatical phrases that are used by the compiler to synthesize output. the grammatical phrases of the source program are represented by a parse tree .

Parse tree for: position =initial + rate*60;


Assignment Statement identifier position

Expression

Expression

+
identifier
rate

Expression

identifier Expression initial

Expression

Number
60

Phase 3- Semantic Analysis:


checks the source program for semantic errors and gathers type information for the subsequent codegeneration phase. I uses the hierarchical structure determined by the syntax-analysis phase to identify the operators and operands of expressions and statements. An important component of semantic analysis is type checking. Here the compiler checks that each operator has operands that are permitted by the source language specification.

For example, a real number is used to index an or when a binary arithmetic operator is applied to an integer and real.

Convert by semantic analysis

=
Position initial

= + *
rate
60 Position initial rate

+ * inttoreal
60.0

Phase 4- Intermediate Code Generator:


After syntax and semantic analysis, some compilers generate an explicit intermediate representation of the source program.

We can think of this intermediate representation as a program for an abstract machine. This intermediate representation should have two important properties;

it should be easy to produce, and easy to translate into the target program.

temp1 = inttoreal(60)
temp2 = id3 * temp1

temp3 = id2 + temp2


id1 =temp3

Phase 5- Code Optimization :


The code optimization phase attempts to improve the intermediate code, so that faster-running machine code will result.

temp1 = inttoreal(60) temp2 = id3 * temp1 temp3 = id2 + temp2 id1 =temp3 After perform Optimization temp1 = id3 * 60.0 id1 = id2+ temp1

Phase 6- Code Generation:


The final phase of the compiler is the generation of target code, consisting normally of relocatable machine code or assembly code. Memory locations are selected for each of the variables used by the program.

Then, intermediate instructions are each translated into a sequence of machine instructions that perform the same task.
MOVF MULF MOVF id3, R2 #60.0, R2 id2, R1

ADDF
MOVF

R2, R1
R1, id1

Additional Compilation Terminologies


Load module (executable image): the user and system code together Linking and loading: the process of collecting system program units and linking them to a user program

Machine-dependent
and Machine-independent

Machine-dependent
is a term for application software that runs only on a particular type of computer.

machine-independent
A term applied to software that is not dependent on the properties of a particular machine, and can therefore be used on any machine. Also called machine-independent, or cross-platform , portable.

end

S-ar putea să vă placă și