language levels and translation

32
1 Language Levels and Translation http://www.pds.ewi.tudelft.nl/~iosup/Courses/2011_ti1400_11.p See also: Blackboard >> Course Material >> Reader (Dictaat) http://www.pds.ewi.tudelft.nl/vakken/in1705/cos2007.pdf

Upload: jersey

Post on 29-Jan-2016

55 views

Category:

Documents


0 download

DESCRIPTION

Language Levels and Translation. http://www.pds.ewi.tudelft.nl/~iosup/Courses/2011_ti1400_11.ppt. See also: Blackboard >> Course Material >> Reader (Dictaat) http://www.pds.ewi.tudelft.nl/vakken/in1705/cos2007.pdf. IT Industry Competitiveness. Source: The Economist, Benchmarking IT industry - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Language Levels and Translation

1

Language Levelsand Translation

http://www.pds.ewi.tudelft.nl/~iosup/Courses/2011_ti1400_11.ppt

See also: Blackboard >> Course Material >> Reader (Dictaat)http://www.pds.ewi.tudelft.nl/vakken/in1705/cos2007.pdf

Page 2: Language Levels and Translation

TU-DelftTI1400/11-PDS

2

IT Industry Competitiveness

Source: The Economist, Benchmarking IT industry competitiveness 2009 http://portal.bsa.org/2009eiu/study/2009_eiu_global.pdf

Page 3: Language Levels and Translation

TU-DelftTI1400/11-PDS

3

The Netherlands: A Top IT Industry

Source: The Economist, Benchmarking IT industry competitiveness 2009 http://portal.bsa.org/2009eiu/study/2009_eiu_global.pdf

Page 4: Language Levels and Translation

TU-DelftTI1400/11-PDS

4

But … Where’s the Human Capital?

“A longer-term challenge for some European countries is encouraging more graduates to choose science-related subjects.”- NL is 27th in HC

Source: The Economist, Benchmarking IT industry competitiveness 2009

Q: Good/Bad news for you?

Page 5: Language Levels and Translation

TU-DelftTI1400/11-PDS

5

The Simplest(?) Problem: How to Program Computers?

• So far- Design them from scratch

- Assembly

• This lecture- Language levels

- Translation

- The compiler sequence

Page 6: Language Levels and Translation

TU-DelftTI1400/11-PDS

6

Language levels

• A computer has several language levels:

- machine language

- assembler language (e.g., Intel/Pentium

assembler)

- higher-level language (e.g., Java)

- application-specific language (e.g., MatLab)

• Close the gap between problem

description and machine program

Page 7: Language Levels and Translation

TU-DelftTI1400/11-PDS

7

Program transformations

• Need for program transformations

• Semantics of programs must remain the same

• Two ways of transformation:

1. compilation: first translate, then execute

2. interpretation: interleave translation and execution

Page 8: Language Levels and Translation

TU-DelftTI1400/11-PDS

8

Type of translators

Java

machineinstructions

Java

IA-32assembler

machineinstructions

Java

IA-32assembler

machineinstructions

JVMcompile tomachinelanguage

interpretbyte codecompile to

assembler

compile

assembleassemble

Page 9: Language Levels and Translation

TU-DelftTI1400/11-PDS

9

Types of interpreters

programPC

program

inter-preter

IPC

PC

program

run-timesystem/OS

(I)PC

PC

many steps of PC for one step of IPC

combination:interpretsystem calls

Page 10: Language Levels and Translation

TU-DelftTI1400/11-PDS

10

Compilation versus Interpretation

• Advantages interpretation- Direct edit/execution cycle

- Debugging on the level of interpreted language

- Less memory requirements for programs

• Advantage compilation- Faster execution (factor of 50-100)

- Semantic checks during compilation process

Page 11: Language Levels and Translation

TU-DelftTI1400/11-PDS

11

Simulation versus Emulation

• Mimicking of hardware or software

• Through program: simulation

• Through hardware: emulation

• Example:- PowerPC simulation on Intel Pentium

- Virtual machines such as KVM and VMware

Page 12: Language Levels and Translation

TU-DelftTI1400/11-PDS

12

Programs and machines

• In exchanging programs and machines three notions are relevant:

1. Compatibility

2. Portability

3. Conversion

Page 13: Language Levels and Translation

TU-DelftTI1400/11-PDS

13

Compatibility

• Compatibility: functionality of system is

independent of implementation

- machine versions with same instruction set

• Upward (FWD) compatibility: new version of

system incorporates functionality of old system

- Code compiled on old system will run on new system

- CDs are FWD-compatible with DVD readers

- (DVD readers are backwards-compatible with CDs)

Page 14: Language Levels and Translation

TU-DelftTI1400/11-PDS

14

Portability (1)

• Portability is the ease of transferring a program to different machines and operating systems.

• Problems:- Different machine instructions- Different OS calls- Other compiler with deviating

conventions

Page 15: Language Levels and Translation

TU-DelftTI1400/11-PDS

15

Portability (3)

• Steps:- making readable on new system

- adapt to new OS

- recompile

• Performance characteristics can change

Page 16: Language Levels and Translation

TU-DelftTI1400/11-PDS

16

Conversion

• Conversion is adaptation of applications to a different language or (operating) system- Faster code execution

• Conversion problems:- expressiveness of new language

- different operating systems

- different network environment

- different machine precision

Page 17: Language Levels and Translation

TU-DelftTI1400/11-PDS

17

Portability/Conversion Cost

• An example- Mid-size game studio has 25+ game titles

- 4 languages, 25 x 4 = 100 releases

- 100 platforms (mobiles etc.)

- $1,000/release x 100 x 100 = $10,000,000 all

Q: How would you address this situation?

Page 18: Language Levels and Translation

TU-DelftTI1400/11-PDS

18

Outsourcing: The Dutch Market Story

0-49k

50-199k

200-499k

500k+Outsourcing

Web

Java Ent.

Mobile

Services

Embedded

Stop0-1yr

1-2yr2+yr

Source: European IT Outsourcing Intelligence Report 2011: The Netherlands http://www.itsourcing-europe.com/uploads/Dutch_ITO_Intelligence_Report_2011.pdf

Q: Good/Bad news for you?

Page 19: Language Levels and Translation

TU-DelftTI1400/11-PDS

19

Levels of abstraction

Language level

Machine-program level

Microprogram level

Assembler level

Digital logic level

Operating System

.......

interpret

translate

Page 20: Language Levels and Translation

TU-DelftTI1400/11-PDS

20

Virtual machines (1)

Virtual machine Mn with machine language Ln

Virtual machine M3 with machine language L3

Virtual machine M2 with machine language L2

Real machine M1 with machine language L1

Page 21: Language Levels and Translation

TU-DelftTI1400/11-PDS

21

Virtual machines (2)

• A language defines a virtual machine

• The machines M2,...,Mn are virtual

• Machine M1 is real

• At Mi we need an interpreter or a compiler

to translate programs written in Li+1 to Li

• Hardware and software are equivalent

Page 22: Language Levels and Translation

TU-DelftTI1400/11-PDS

22

Compiler structure

Source program

Lexicographical analysis

Syntactic analysis

Semantic analysis

Intermediate-codegeneration

Code optimization

Code generation

Target program

Page 23: Language Levels and Translation

TU-DelftTI1400/11-PDS

23

Lexicographical analysis (1)

Goal: reading program text and group characters into tokens

int SUM = 0;

i n t S U M = 0 ;

int SUM = 0 ;

10 characters

5 tokens

Page 24: Language Levels and Translation

TU-DelftTI1400/11-PDS

24

Lexicographical analysis (2)

Tokens are classified:• Keywords (for, if,....)• Identifiers (SUM, ...)• Constants (0, 3.14, “char”)• Delimiters ({,;)• Operators (+, =, ...)

Page 25: Language Levels and Translation

TU-DelftTI1400/11-PDS

25

Lexicographical analysis (3)

• Identifiers and constants are stored in the symbol table:

entry name kind type value.. .. .. .. ..100 SUM ident int .... .. .. .. ..200 PI const real 3.1415

Page 26: Language Levels and Translation

TU-DelftTI1400/11-PDS

26

Syntactic analysis

• Check of correctness of program with respect to the grammar of the language

• Also called parsing• Building of so called parse tree

total = 3 + (2*5)

=

+

*

52

3

total

Page 27: Language Levels and Translation

TU-DelftTI1400/11-PDS

27

Semantic analysis

• Static semantics- (part of) type checking

- illegal statements

• Dynamic semantics- done at run-time

- remainder of type checking

- operator exceptions (e.g., division by 0)

Page 28: Language Levels and Translation

TU-DelftTI1400/11-PDS

28

Intermediate code (1)

• Reasons for intermediate code level:1. simplify compilation process

2. reuse parts of compiler for different architectures

• In intermediate code:- a single operation at a time

- test on only one condition at a time

- while loops replaced by test and branch instructions, and labels

Page 29: Language Levels and Translation

TU-DelftTI1400/11-PDS

29

Intermediate code (2): Example

while ( (a>b) && (a <= c+d) )

a = a*b;

translated into:

L1: if (a>b) goto L2

goto L3L2: h1 = c+d

if (a <= h1) goto L4goto L3

L4: a = a*bgoto L1

L3:

Page 30: Language Levels and Translation

TU-DelftTI1400/11-PDS

30

Code generation (1)

MOV EAX, <RHS >(0)MOV <LHS >, EAXMOV EAX, 3ADD EAX, <2*5 >

MOV EAX, 2MOV EBX, 5IMUL EAX, EBX

=

3 + <2*5>

2*5

<LHS> = “total”

Page 31: Language Levels and Translation

TU-DelftTI1400/11-PDS

31

Code generation (2)

MOV EAX, 2 load 2 in EAXMOV EBX, 5 load 5 in EBXIMUL EAX,EBX multiplyPUSH EAX push result on stack

MOV EAX, 3 load 3 in EAXPOP EBX pop from stackADD EAX,EBX additionPUSH EAX push result on stack

POP EAX pop from stackMOV total(0),EAX do final assignment

communication via stack

Page 32: Language Levels and Translation

TU-DelftTI1400/11-PDS

32

Code optimization

MOV EAX, 2 load 2 in EAXMOV EBX, 5 load 5 in EBXIMUL EAX,EBX multiply

MOV EBX, 3 load 3 in EBXADD EAX, EBX addition

STW total(0), EAX do final assignment

omit communication via stack