COP4020 Programming Languages Compilation and Interpretation Prof. Xin Yuan
Overview Compilation and interpretation Virtual machines Static linking and dynamic linking Compiler in action (g++) Integrated development environments 4/19/2017 COP4020 Spring 2014 COP4020 Fall 2006
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; 4/19/2017 COP4020 Spring 2014
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. 4/19/2017 COP4020 Spring 2014
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 addq %rsi, %rax movl $1, A(,%rax,4) 4/19/2017 COP4020 Spring 2014
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.py The program just runs, no compilation phase The 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? 4/19/2017 COP4020 Spring 2014
Compilation and interpretation In general, which approach is more efficient? A[i][j] = 1; Compilation: salq $2, %rax addq %rcx, %rax leaq 0(,%rax,4), %rdx addq %rdx, %rax addq %rsi, %rax movl $1, A(,%rax,4) Interpretation: create a software environment that understand the language put 1 in the array entry A[i][j]; 4/19/2017 COP4020 Spring 2014
Compilation and interpretation In general, which approach is more efficient? A[i][j] = 1; 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: salq $2, %rax addq %rcx, %rax leaq 0(,%rax,4), %rdx addq %rdx, %rax addq %rsi, %rax movl $1, A(,%rax,4) Compilation is always more efficient!! Interpretation provides more functionality. E.g. for debugging One can modify the value of a variable during execution. 4/19/2017 COP4020 Spring 2014
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 Source Program Compiler Debug on X Target Program Compile on X Target Program Input Output Run on X 4/19/2017 COP4020 Spring 2014
Cross Compilation Compiler runs on platform X, target code runs on platform Y Debug on X (= emulate Y) Source Program Cross Compiler Target Program Compile on X Copy to Y Target Program Input Output Run on Y 4/19/2017 COP4020 Spring 2014
Interpretation Interpretation is the conceptual process of running high-level code by an interpreter Source Program Interpreter Output Input 4/19/2017 COP4020 Spring 2014
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 4/19/2017 COP4020 Spring 2014
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) 4/19/2017 COP4020 Spring 2014
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) 4/19/2017 COP4020 Spring 2014
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 4/19/2017 COP4020 Spring 2014
Compilation and Execution on Virtual Machines Compiler generates intermediate program Virtual machine interprets the intermediate program Source Program Compiler Intermediate Program Compile on X Run on VM Virtual Machine Input Output Run on X, Y, Z, … 4/19/2017 COP4020 Spring 2014
Pure Compilation and Static Linking Adopted by the typical Fortran systems Library routines are separately linked (merged) with the object code of the program Source Program Compiler Incomplete Object Code extern printf(); Linker _printf _fget _fscan … Static Library Object Code Binary Executable 4/19/2017 COP4020 Spring 2014
Compilation, Assembly, and Static Linking Facilitates debugging of the compiler Source Program Compiler Assembly Program extern printf(); Assembler _printf _fget _fscan … Linker Static Library Object Code Binary Executable 4/19/2017 COP4020 Spring 2014
Compilation, Assembly, and Dynamic Linking Dynamic libraries (DLL, .so, .dylib) are linked at run-time by the OS (via stubs in the executable) Source Program Compiler Assembly Program extern printf(); Assembler Shared Dynamic Libraries Incomplete Executable _printf, _fget, _fscan, … Output Input 4/19/2017 COP4020 Spring 2014
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? 4/19/2017 COP4020 Spring 2014
Preprocessing Most C and C++ compilers use a preprocessor to import header files and expand macros (‘cpp a.cpp’ and ‘cpp a1.cpp’ Source Program Preprocessor Modified Source Program #include <stdio.h> #define N 99 … for (i=0; i<N; i++) for (i=0; i<99; i++) Compiler Assembly or Object Code 4/19/2017 COP4020 Spring 2014
Assembly or Object Code The CPP Preprocessor Early C++ compilers used the CPP preprocessor to generated C code for compilation C++ Source Code C++ Preprocessor C Source Code C Compiler Assembly or Object Code 4/19/2017 COP4020 Spring 2014
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’ 4/19/2017 COP4020 Spring 2014
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 4/19/2017 COP4020 Spring 2014