Compiled and Interpreted Languages

underlingbuddhaΒιοτεχνολογία

2 Οκτ 2013 (πριν από 4 χρόνια και 9 μέρες)

87 εμφανίσεις

Compiled and Interpreted Languages

CS
480
/
680


Comparative Languages

Compiling and Interpreting

2

Compiling High Level Languages

Source Code (text)

Compiler

Assembly (text)

Assembler

Relocatable Object Code (machine code without addresses)

Linker/Loader

Executable Code (machine code)

Compiling and Interpreting

3

The Compilation Process (
1
)

C++


int var
1
, var
2
;

void main() {


var
1
=
5
;


var
2
=
37
;


var
2
/= var
1
;


cout << var
2
<< endl;

}

Assembly


SECTION MAIN,CODE


XREF

PRINTINT

_MAIN

MOVE.W

#
5
,VAR
1


MOVE.W

#
7
,VAR
2


MOVE.W

VAR
1
,D
1


MOVE.W

VAR
2
,D
2


EXT.L

D
2


DIVS.W

D
1
,D
2


MOVE.W

D
2
,VAR
2


MOVE.W

VAR
2
,
-
(SP)


JSR

PRINTINT


MOVE.W

#
228
,D
7


TRAP

#
14

VAR
1

DS.L

1

VAR
2

DS.L

1


END

Compiling and Interpreting

4

The Compilation Process (
2
)


1


SECTION MAIN,CODE


2


XREF

PRINTINT

00000000 33
FC
0005 00000038 3
_MAIN

MOVE.W

#
5
,VAR
1

00000008 33
FC
0007 0000003
C
4


MOVE.W

#
7
,VAR
2

00000010 3239 00000038 5


MOVE.W

VAR
1
,D
1

00000016 3439 0000003
C
6


MOVE.W

VAR
2
,D
2

0000001
C
48
C
2 7


EXT.L

D
2

0000001
E
85
C
1 8


DIVS.W

D
1
,D
2

00000020 33
C
2 0000003
C
9


MOVE.W

D
2
,VAR
2

00000026 3
F
39 0000003
C
10


MOVE.W

VAR
2
,
-
(SP)

0000002
C
4
EB
9 00000000 11


JSR

PRINTINT

00000032 3
E
3
C
00
E
4 12


MOVE.W

#
228
,D
7

00000036 4
E
4
E
13


TRAP

#
14

00000038 14
VAR
1

DS.L

1

0000003
C
15
VAR
2

DS.L

1

00000040 16


END


Compiling and Interpreting

5

The Compilation Process (
3
)

#SECTION_CODE
00000040
MAIN



000000 33
FC
00050000003833
FC
00070000003
C


000010 32390000003834390000003
C
48
C
285
C
1


000020 33
C
20000003
C
3
F
390000003
C
4
EB
90000


000030 00003
E
3
C
00
E
44
E
4
E
0000000000000000


#LOCAL


00000004


0000000
C


00000012


00000018


00000022


00000028


#XREF_
32
PRINTINT


0000002
E

MOVE.W

#
5
,VAR
1

= loader

= linker

Compiling and Interpreting

6

Advantages of Compiled Higher Languages


Abstraction of hardware details


Allow a program to more easily be ported to
other hardware platforms


Can be tailored for specific types of
applications


Web programming


Logic programming


Compiling and Interpreting

7

Interpreted Languages


Interpreted languages are not compiled


The source code is run directly


Often compiled into a more efficient pseudo
-
language just before running

Only machine code can run on the CPU, so how does an
interpreted language run?


The
interpreter

is an executable program which interprets
the source code and runs the appropriate machine code.

Compiling and Interpreting

8

Compiled and Interpreted Languages

Compilation

Executable

Input

Output

Interpreter

Program

Input

Output

Source

Compiled

Interpreted

Compiling and Interpreting

9

Example of Interpreted Code

#!/usr/bin/perl


$var
1
=
7
;

$var
2
= ”
008
”;


$var
3
= $var
1
* $var
2
;


print ”Result: $var
3
\
n”;


$string = ’print ”program ending now
\
n”’;

eval $string;


exit;


$> perl myscript


Result:
56

program ending now

Compiling and Interpreting

10

Eval()


A key advantage of interpreted languages is that
they can build and execute code
on
-
the
-
fly


Essentially, the “compiler” is built into the
interpreter program



How hard would this be for a compiled
language?

Compiling and Interpreting

11

Pros

and Cons

of Interpreted Languages


Advantages of interpreted languages


Fast (program development) and messy


Untyped variables


On
-
the
-
fly variable creation


Eval()


Extremely

portable code


Powerful


Lean (only the interpreter is fat)


Compiling and Interpreting

12

Pros and
Cons of Interpreted Languages


Disadvantages of interpreted languages


Much slower

to execute


Can be very hard to debug:








Linking is an issue


may not be appropriate for
large
-
scale software development

$count =
0
;

while ($counnt <
100
) {








$count++;

}

Compiling and Interpreting

13

Programming Tradeoffs

Efficiency

Abstraction

(Ease of use, specialization, paradigm enforcing, etc.)

Assembly

C

C++

Java, Lisp

Perl, Python, Ruby, etc.

Fortran, Pascal

Compiling and Interpreting

14

Some High Level Languages

Interpreted

Compiled

Object
-
oriented

Ruby, Python,
Java

C++

Imperative

Perl

C, C++,
FORTRAN,
Pascal

Functional

LISP, Scheme

LISP, ML

Compiling and Interpreting

15

A Brief History of Programming Languages