Docsity

Compilers and Interpreters: A Comparison of Translation Approaches in Programming - Prof. , Study notes of Programming Languages

Toggle sidebar
bg1
CS 3723: Supplemental Notes on Compilers and Interpreters,2/14/2008
Compilers and Interpreters
Most programs a written in high-level programming languages that are not di-
rectly implemented by any real hardware machine. So it is not possible to simply
dump the source code onto a computer and expect it to run. There are two basic
approaches to bridge the gap between high-level programming languages and
machine code: compilation and interpretation.
Traditional Compilation. With compilation, some source program Pwritten
in some source language Lis translated into an equivalent program P0in some
target language L0. The program that does this translation is called a compiler.
Typically, the source language Lis some high-level programming language (e.g.,
C), and the target language L0is the machine code of some hardware machine.
The output program P0would then be an executable that can run directly on
the hardware. In this way, P0is both the output of the compiler program and
a program itself. After compilation P0can be executed (independently of the
compiler) to process input and produce output:
Machine Code
Program (P’)
Source Program (P)
Input
Output
Traditional
Compiler
Interpretation. An alternative approach to program execution is interpretation.
An interpreter is a program that executes another program Pwritten in some
programming language Lby simulating Land directly performing the tasks
required by the statements in Pwhile Pis running. With an interpreter, there is
no independent executable output P0. The interpreter itself is the only program
that runs directly on L0. It knows how to simulate the execution each kind
of statement in L, and so it can be used to perform the tasks for each of the
statements in Pas they would be performed on L.
1
pf3
pf4
pf5

Partial preview of the text

Download Compilers and Interpreters: A Comparison of Translation Approaches in Programming - Prof. and more Study notes Programming Languages in PDF only on Docsity!

CS 3723 : Supplemental Notes on Compilers and Interpreters, 2 / 14 / 2008

Compilers and Interpreters

Most programs a written in high-level programming languages that are not di- rectly implemented by any real hardware machine. So it is not possible to simply dump the source code onto a computer and expect it to run. There are two basic approaches to bridge the gap between high-level programming languages and machine code: compilation and interpretation.

Traditional Compilation. With compilation, some source program P written in some source language L is translated into an equivalent program P′^ in some target language L′. The program that does this translation is called a compiler. Typically, the source language L is some high-level programming language (e.g., C), and the target language L′^ is the machine code of some hardware machine. The output program P′^ would then be an executable that can run directly on the hardware. In this way, P′^ is both the output of the compiler program and a program itself. After compilation P′^ can be executed (independently of the compiler) to process input and produce output:

Machine Code Source Program (P) Program (P’)

Input

Output

T r a d i t i o n a l C o m p i l e r

Interpretation. An alternative approach to program execution is interpretation. An interpreter is a program that executes another program P written in some programming language L by simulating L and directly performing the tasks required by the statements in P while P is running. With an interpreter, there is no independent executable output P′. The interpreter itself is the only program that runs directly on L′. It knows how to simulate the execution each kind of statement in L, and so it can be used to perform the tasks for each of the statements in P as they would be performed on L.

For a non-interactive program P, the interpreter can be seen as taking two inputs (P and the input for P) and directly producing an output. This output must be the same as P would produce if it were executing directly on L:

Source Program (P) I n t e r p r e t e r

Input

Output

Comparison of Compilation and Interpretation. Compilations happens once (each time the source is modified) whereas interpretation continues throughout every execution of a program. So for long running programs, compilation is almost always faster. In addition, many compilers expend effort to analyze pro- grams and rearrange and ‘optimize’ the code so that it runs even faster. (A good interpreter might execute a program about 20× slower than the compiled and optimized machine code for the same program.)

On the other hand, this translation and optimization takes time, so if you are developing a program, you might get faster turnaround time with a interpreter. In addition, with a optimizing compiler the program that gets executed does not always perform operations in the order that they appear in the program, whereas interpretation executes each statement as it appears in the program. Thus, interpreted programs are often easier to debug.

Another issue is the amount of runtime overhead required to implement various features. Languages like Lisp, SmallTalk, and most ‘scripting languages’ tend to require a lot of decisions and actions to take place in the background that are not explicitly mentioned in the code. One example of this we’ve already dis- cussed is garbage collection, but other example include the way variable names are mapped to their contents, complex built-in data types, dynamic loading of new program code, and reflection. In general the runtime performance overhead of these features is the same whether the program is compiled or interpreted, so in more ‘dynamic’ languages where lots of these decisions occur at runtime, in- terpretation will not be significantly slower than compilation. These languages are typically implemented with an interpreter and are sometimes called “inter- preted languages” even though a compiler could be written for them. On the other hand, languages that can be analyzed easily and for which many decisions can be decided during compilation (e.g., Fortran, C, C++) benefit from compi- lation; these are often called “compiled languages” even though an interpreter could be written for them.

This process of compiling parts of the program as needed while the program is running is known as “Just-in-time” (JIT) compilation:

of a Method^ Bytecode JIT Compiler

Input

Output

Machine Code for a Method

Java Virtual Machine

Interpreter

Class Loader

Garbage Collection

Reflection

Java Class File

In addition to a compiler and/or interpreter, a Java Virtual Machine also includes other runtime facilities that support features such as dynamic class loading, re- flection, and garbage collection.

Dynamic Loading and Eval

One of the more interesting facilities provided by the Java Virtual Machine is the ability to load class files into the virtual machine and link them dynamically to a running program This dynamic class loading happens automatically the first time each class is used, but can also be explicitly requested by the running program to, for example, download a new plugin over the internet and integrate into the running program.

Slightly less flexible versions of dynamic loading and linking are provided for traditional compiled languages (e.g., C) by most modern operating systems (e.g., Linux) and through calls to system libraries (e.g., dlopen).

An even more flexible version of dynamic loading is available in many ‘inter- preted languages.’ Lisp/Scheme provides an eval function. This function takes a list containing Lisp/Scheme code as an argument

( l e t ( ( code ( cons ’+ ( cons 1 ( cons 1 ’ ( ) ) ) ) ) ) ( cons code ( e v a l code ) ) )

will evaluate to: ((+ 1 1). 2 ).

Similar statements/operators are provided by most Unix Shells (‘.. .‘ and $(.. .)) and by scripting languages such as Perl (eval ".. .";). These tend to use pro- gram source code stored in strings rather than as lists.

This facility is incredibly powerful and can be used in many of the same ways higher-order functions can be used. But it is also dangerous, since the code being incorporated by eval can be anything. Letting user input from an untrustworthy source into the an eval string is an easy way to introduce security vulnerability.

For more information: Mitchell, 4. 1 ; Scott, Programming Language Pragmatics, 2 nd ed.; Lindholm and Yellin, Java Virtual Machine Specification, 2 nd ed.; dlopen man page