6 Program Translation CS 100 The World of

  • Slides: 15
Download presentation
6. Program Translation CS 100: The World of Computing John Dougherty Haverford College

6. Program Translation CS 100: The World of Computing John Dougherty Haverford College

Overview w The Problem w The “Source” – high-level code w The Target –

Overview w The Problem w The “Source” – high-level code w The Target – low-level (machine) code w Types of translation w The translation algorithm/process w PIPPIN

The Problem w People communication in ambiguous, highlevel languages, using experience, context, and can

The Problem w People communication in ambiguous, highlevel languages, using experience, context, and can ask for clarity interactively n e. g. , Thoreau threw through the tunnel. w Machines have no “sense” of context or experience, and need unambiguous instructions

The Source w High-level programming languages close to natural language (but not quite) n

The Source w High-level programming languages close to natural language (but not quite) n Alice, Javascript, C++, Java, C# w Known as Source code w Each instructions implies many lower-level instructions (as we’ll see …)

The Target w Low-level instructions that are clear and simple – typically fixed in

The Target w Low-level instructions that are clear and simple – typically fixed in size, with a command some reference to data n n Opcode Operand w Known as … n Machine code || binary code || executable

Program Translation w From High- to Low-Level w Recall “divide and conquer” in programming

Program Translation w From High- to Low-Level w Recall “divide and conquer” in programming n n Input Process Output … then details of Process, then details of … w Typically many low-level operations per high-level instruction w From source code to machine/binary code w Two ways to translate …

Interpretation w Works with the source always w Translates and executes “on the fly”

Interpretation w Works with the source always w Translates and executes “on the fly” w Like a language translator at the UN w Easier to debug w Executes slower

Compilation w Works with executable w Translates the entire program from source to machine

Compilation w Works with executable w Translates the entire program from source to machine code once w Executes the machine code as many times as needed w Recompile often during development w Executes substantially faster w Most software is distributed (except open source) w Hides algorithm

Phases of Translation w Scanning – breaking text sequence into tokens (i. e. ,

Phases of Translation w Scanning – breaking text sequence into tokens (i. e. , meaningful chunks) n “while”, “=”, “For all together” w Parsing – organizing the tokens to discover the meaning of the program w Code Generation – writing the sequence of machine level operations n Opcodes, operands

Language Levels w High-level: one-to-many relation to machine language (e. g. , z =

Language Levels w High-level: one-to-many relation to machine language (e. g. , z = x + y is 4 PIPPIN ops) w Assembly language: one-to-one (roughly) relation to machine language (PIPPIN) w Low-level: machine, or binary, language of 0 s and 1 s

Arithmetic Instructions w To demonstrate this process, we’ll look at standard arithmetic expressions and

Arithmetic Instructions w To demonstrate this process, we’ll look at standard arithmetic expressions and statements in a high-level language w Expressions have a pattern, or (recursivelydefined) form Var = exp Where exp = value | exp + exp | exp – exp | … (demonstration of Rosetta)

PIPPIN instruction layout opcode operand Each box contains a byte

PIPPIN instruction layout opcode operand Each box contains a byte

Sample PIPPIN Opcodes 00000100 LOD (load from X) 00000101 STO (store to X) 00001111

Sample PIPPIN Opcodes 00000100 LOD (load from X) 00000101 STO (store to X) 00001111 HLT (halt execution) 0000 ADD (acc = acc + X)

Example PIPPIN program ; PIPPIN code for Z = X + Y [1] [2]

Example PIPPIN program ; PIPPIN code for Z = X + Y [1] [2] [3] [4] LOD X ADD Y STO Z HLT ; acc <= X ; acc <= acc + Y ; acc => Z ; halt ; other examples AE pp. 252 -4

Programming Paradigms w Imperative: procedures as abstractions, details of how to do a task

Programming Paradigms w Imperative: procedures as abstractions, details of how to do a task (e. g. , FORTRAN, Pascal) w Functional: mathematical approach of inputprocess-return value – functions can be composed of other functions (including themselves), and can be evaluated (e. g. , LISP) w Declarative: describe the information, but not the way it is processed (e. g. , Prolog) w Object-Oriented: interacting objects (e. g. , Java, C++, C#, Smalltalk, Javascript, Alice)