24
COP4020 Programming Languages Compilation and Interpretation Prof. Xin Yuan

COP4020 Programming Languages Compilation and Interpretation Prof. Xin Yuan

Embed Size (px)

Citation preview

Page 1: COP4020 Programming Languages Compilation and Interpretation Prof. Xin Yuan

COP4020Programming Languages

Compilation and Interpretation

Prof. Xin Yuan

Page 2: COP4020 Programming Languages Compilation and Interpretation Prof. Xin Yuan

COP4020 Spring 2014 204/19/23

Overview

Compilation and interpretation Virtual machines Static linking and dynamic linking Compiler in action (g++) Integrated development environments

Page 3: COP4020 Programming Languages Compilation and Interpretation Prof. Xin Yuan

Compilation and interpretation

A program written in a high level language can run in two ways Compiled into a program in the native machine language and

then run on the target machine Directly interpreted and the execution is simulated within an

interpreter

Example: how can the following statement be executed? A[i][j] = 1;

COP4020 Spring 2014 304/19/23

Page 4: COP4020 Programming Languages Compilation and Interpretation Prof. Xin Yuan

Compilation and interpretation

Example: how can the following statement be executed? A[i][j] = 1;

Approach 1: You can create a software environment that understands 2-

dimensional array (and the language) To execute the statement, just put 1 in the array entry A[i][j]; This is interpretation since the software environment

understands the language and performs the operations specified by interpreting the statements.

COP4020 Spring 2014 404/19/23

Page 5: COP4020 Programming Languages Compilation and Interpretation Prof. Xin Yuan

Compilation and interpretation

Example: how can the following statement be executed? A[i][j] = 1;

Approach 2: Translate the statements into native machine (or assembly

language) and then run the program. This is compilation. g++ produces the following assembly for this

statement. salq $2, %rax

addq %rcx, %rax

leaq 0(,%rax,4), %rdx

addq %rdx, %rax

salq $2, %rax

addq %rsi, %rax

movl $1, A(,%rax,4)

COP4020 Spring 2014 504/19/23

Page 6: COP4020 Programming Languages Compilation and Interpretation Prof. Xin Yuan

Compilation and interpretation

How is a C++ program executed on linprog?g++ try.cpp compiling the program into machine code./a.out running the machine code

How is a python program executed?python try.pyThe program just runs, no compilation phaseThe program python is the software environment that understands python language. The program try.py is executed (interpreted) within the environment.

In general, which approach is more efficient?

COP4020 Spring 2014 604/19/23

Page 7: COP4020 Programming Languages Compilation and Interpretation Prof. Xin Yuan

Compilation and interpretation

In general, which approach is more efficient? A[i][j] = 1;

COP4020 Spring 2014 704/19/23

Compilation:salq $2, %raxaddq %rcx, %raxleaq 0(,%rax,4), %rdxaddq %rdx, %raxsalq $2, %raxaddq %rsi, %raxmovl $1, A(,%rax,4)

Interpretation:• create a software environment that

understand the language• put 1 in the array entry A[i][j];

Page 8: COP4020 Programming Languages Compilation and Interpretation Prof. Xin Yuan

Compilation and interpretation

In general, which approach is more efficient? A[i][j] = 1;

COP4020 Spring 2014 804/19/23

Compilation:salq $2, %raxaddq %rcx, %raxleaq 0(,%rax,4), %rdxaddq %rdx, %raxsalq $2, %raxaddq %rsi, %raxmovl $1, A(,%rax,4)

Interpretation:• create a software environment that

understand the language• put 1 in the array entry A[i][j];

• For the machine to put 1 in the array entry A[i][j], that code sequence still needs to be executed.

• Most interpreter does a little more than the barebone “real work.”

• Compilation is always more efficient!!• Interpretation provides more functionality. E.g. for debuggingOne can modify the value of a variable during execution.

Page 9: COP4020 Programming Languages Compilation and Interpretation Prof. Xin Yuan

COP4020 Spring 2014 904/19/23

Compilation

Compilation is the conceptual process of translating source code into a CPU-executable binary target code

Compiler runs on the same platform X as the target code

TargetProgram

CompilerSource

ProgramTarget

Program

Input Output

Run on X

Compile on X

Debug on X

Page 10: COP4020 Programming Languages Compilation and Interpretation Prof. Xin Yuan

COP4020 Spring 2014 1004/19/23

Cross Compilation

Compiler runs on platform X, target code runs on platform Y

TargetProgram

CrossCompiler

SourceProgram

TargetProgram

Input Output

Run on Y

Compile on X Copy to Y

Debug on X(= emulate Y)

Page 11: COP4020 Programming Languages Compilation and Interpretation Prof. Xin Yuan

COP4020 Spring 2014 1104/19/23

Interpretation

Interpretation is the conceptual process of running high-level code by an interpreter

Interpreter

SourceProgram

Input

Output

Page 12: COP4020 Programming Languages Compilation and Interpretation Prof. Xin Yuan

COP4020 Spring 2014 1204/19/23

Compilers versus Interpreters

Compilers “try to be as smart as possible” to fix decisions that can be taken at compile time to avoid to generate code that makes this decision at run time Type checking at compile time vs. runtime Static allocation Static linking Code optimization

Compilation leads to better performance in general Allocation of variables without variable lookup at run time Aggressive code optimization to exploit hardware features

Page 13: COP4020 Programming Languages Compilation and Interpretation Prof. Xin Yuan

COP4020 Spring 2014 1304/19/23

Compilers versus Interpreters

Benefit of interpretation? Interpretation facilitates interactive debugging and testing

Interpretation leads to better diagnostics of a programming problem Procedures can be invoked from command line by a user Variable values can be inspected and modified by a user

Some programming languages cannot be purely compiled into machine code alone

Some languages allow programs to rewrite/add code to the code base dynamically Some languages allow programs to translate data to code for execution

(interpretation)

Page 14: COP4020 Programming Languages Compilation and Interpretation Prof. Xin Yuan

COP4020 Spring 2014 1404/19/23

Compilers versus Interpreters

The compiler versus interpreter implementation is often fuzzy One can view an interpreter as a virtual machine that executes high-

level code Java is compiled to bytecode Java bytecode is interpreted by the Java virtual machine (JVM) or

translated to machine code by a just-in-time compiler (JIT) A processor (CPU) can be viewed as an implementation in hardware of

a virtual machine (e.g. bytecode can be executed in hardware)

Page 15: COP4020 Programming Languages Compilation and Interpretation Prof. Xin Yuan

COP4020 Spring 2014 1504/19/23

Virtual Machines

A virtual machine executes an instruction stream in software

Adopted by Pascal, Java, Smalltalk-80, C#, functional and logic languages, and some scripting languages Pascal compilers generate P-code that can be interpreted or

compiled into object code Java compilers generate bytecode that is interpreted by the Java

virtual machine (JVM) The JVM may translate bytecode into machine code by just-in-

time (JIT) compilation

Page 16: COP4020 Programming Languages Compilation and Interpretation Prof. Xin Yuan

COP4020 Spring 2014 1604/19/23

Compilation and Execution on Virtual Machines Compiler generates intermediate program Virtual machine interprets the intermediate program

VirtualMachine

CompilerSource

ProgramIntermediate

Program

Input Output

Run on VMCompile on X

Run on X, Y, Z, …

Page 17: COP4020 Programming Languages Compilation and Interpretation Prof. Xin Yuan

COP4020 Spring 2014 1704/19/23

Pure Compilation and Static Linking Adopted by the typical Fortran systems Library routines are separately linked (merged) with the

object code of the program

CompilerSource

ProgramIncomplete

Object Code

LinkerStatic LibraryObject Code

_printf_fget_fscan…

extern printf();

BinaryExecutable

Page 18: COP4020 Programming Languages Compilation and Interpretation Prof. Xin Yuan

COP4020 Spring 2014 1804/19/23

Compilation, Assembly, and Static Linking Facilitates debugging of the compiler

CompilerSource

ProgramAssemblyProgram

LinkerStatic LibraryObject Code

BinaryExecutable

Assembler

_printf_fget_fscan…

extern printf();

Page 19: COP4020 Programming Languages Compilation and Interpretation Prof. Xin Yuan

COP4020 Spring 2014 1904/19/23

Compilation, Assembly, and Dynamic Linking Dynamic libraries (DLL, .so, .dylib) are linked at run-time

by the OS (via stubs in the executable)

CompilerSource

ProgramAssemblyProgram

IncompleteExecutable

Input

Output

Assembler

Shared Dynamic Libraries_printf, _fget, _fscan, …

extern printf();

Page 20: COP4020 Programming Languages Compilation and Interpretation Prof. Xin Yuan

Static linking and dynamic linking in action Try ‘g++ –static a1.cpp’ and ‘g++ a1.cpp’ Try to run the program on linprog and cetus

What are the relative executable file sizes for static and dynamic linking?

Why dynamic linking is now much more common? Which linking mechanism is more portable?

COP4020 Spring 2014 2004/19/23

Page 21: COP4020 Programming Languages Compilation and Interpretation Prof. Xin Yuan

COP4020 Spring 2014 2104/19/23

Preprocessing

Most C and C++ compilers use a preprocessor to import header files and expand macros (‘cpp a.cpp’ and ‘cpp a1.cpp’

Compiler

PreprocessorSource

ProgramModified Source

Program

Assembly orObject Code

#include <stdio.h>#define N 99…for (i=0; i<N; i++)

for (i=0; i<99; i++)

Page 22: COP4020 Programming Languages Compilation and Interpretation Prof. Xin Yuan

COP4020 Spring 2014 2204/19/23

The CPP Preprocessor

Early C++ compilers used the CPP preprocessor to generated C code for compilation

C Compiler

C++Preprocessor

C++SourceCode

C SourceCode

Assembly orObject Code

Page 23: COP4020 Programming Languages Compilation and Interpretation Prof. Xin Yuan

g++ phases:

When running g++, it invokes preprocessor, compiler, assembler, and linker Try ‘g++ -v a.cpp’ to see the commands executed.

Stop after preprocess ‘g++ -v -E a1.cpp’ Stop after compiler (assembly code) ‘ g++ -v –S a1.cpp’

COP4020 Spring 2014 2304/19/23

Page 24: COP4020 Programming Languages Compilation and Interpretation Prof. Xin Yuan

COP4020 Spring 2014 2404/19/23

Integrated Development Environments Programming tools function together in concert

Editors Compilers/preprocessors/interpreters Debuggers Emulators Assemblers Linkers

Advantages Tools and compilation stages are hidden Automatic source-code dependency checking Debugging made simpler Editor with search facilities

Examples Smalltalk-80, Eclipse, MS VisualStudio, Borland