Compiled and Interpreted Languages CS 480/680 – Comparative Languages
Compiling and Interpreting2 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 Interpreting3 The Compilation Process (1) C++ int var1, var2; void main() { var1 = 5; var2 = 37; var2 /= var1; cout << var2 << endl; } Assembly SECTION MAIN,CODE XREFPRINTINT _MAINMOVE.W#5,VAR1 MOVE.W#7,VAR2 MOVE.WVAR1,D1 MOVE.WVAR2,D2 EXT.LD2 DIVS.WD1,D2 MOVE.WD2,VAR2 MOVE.WVAR2,-(SP) JSRPRINTINT MOVE.W#228,D7 TRAP#14 VAR1DS.L1 VAR2DS.L1 END
Compiling and Interpreting4 The Compilation Process (2) 1SECTION MAIN,CODE 2 XREFPRINTINT FC _MAINMOVE.W#5,VAR FC C 4 MOVE.W#7,VAR MOVE.WVAR1,D C 6 MOVE.WVAR2,D C 48C2 7 EXT.LD E 85C1 8 DIVS.WD1,D C C 9 MOVE.WD2,VAR F C 10 MOVE.WVAR2,-(SP) C 4EB JSRPRINTINT E3C 00E4 12 MOVE.W#228,D E4E 13 TRAP# VAR1DS.L C 15 VAR2DS.L END
Compiling and Interpreting5 The Compilation Process (3) #SECTION_CODE MAIN FC FC C C48C285C C C3F C4EB E3C00E44E4E #LOCAL C #XREF_32 PRINTINT E MOVE.W#5,VAR1 = loader = linker
Compiling and Interpreting6 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 Interpreting7 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 Interpreting8 Compiled and Interpreted Languages Compilation Executable Input Output Interpreter ProgramInput Output Source Compiled Interpreted
Compiling and Interpreting9 Example of Interpreted Code #!/usr/bin/perl $var1 = 7; $var2 = ”008”; $var3 = $var1 * $var2; print ”Result: $var3\n”; $string = ’print ”program ending now\n”’; eval $string; exit; $> perl myscript Result: 56 program ending now
Compiling and Interpreting10 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 Interpreting11 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 Interpreting12 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 Interpreting13 Programming Tradeoffs Efficiency Abstraction (Ease of use, specialization, paradigm enforcing, etc.) Assembly C C++ Java, Lisp Perl, Python, Ruby, etc. Fortran, Pascal
Compiling and Interpreting14 Some High Level Languages InterpretedCompiled Object-oriented Ruby, Python, Java C++ ImperativePerl C, C++, FORTRAN, Pascal FunctionalLISP, SchemeLISP, ML
Compiling and Interpreting15 A Brief History of Programming Languages