A compiler is a computer program (or set of programs) that translates text written in a computer language (the source language) into another computer language (the target language). The original sequence is usually called the source code and the output called object code. Commonly the output has a form suitable for processing by other programs (e.g., a linker), but it may be a human readable text file.
The most common reason for wanting to translate source code is to create an executable program. The name "compiler" is primarily used for programs that translate source code from a high level language to a lower level language (e.g., assembly language or machine language). A program that translates from a low level language to a higher level one is a decompiler. A program that translates between high-level languages is usually called a language translator, source to source translator, or language converter. A language rewriter is usually a program that translates the form of expressions without a change of language.
A compiler is likely to perform many or all of the following operations: lexing, preprocessing, parsing, semantic analysis, code optimizations, and code generation.
Programmers soon began to create programs to help with the tedious task of entering machine code. Such a program which would translate an alphabetic letter (which were chosen to be mnemonic) into a corresponding numeric instruction which the machine could execute. In this manner assembly languages and the primitive compiler, the assembler, emerged.
Towards the end of the 1950s, machine-independent programming languages evolved. Subsequently, several experimental compilers were developed then (see, for example, the seminal work by Grace Hopper on the A-0 programming language), but the FORTRAN team led by John Backus at IBM is generally credited as having introduced the first complete compiler, in 1957. COBOL was an early language to be compiled on multiple architectures, in 1960. *
The idea of compilation quickly caught on, and most of the principles of compiler design were developed during the 1960s.
With the evolution of programming languages and the increasing power of computers, compilers are becoming more and more complex to bridge the gap between problem-solving modern programming languages and the various computer systems, aiming at getting the highest performance out of the target machines.
A compiler is itself a computer program written in some implementation language. Early compilers were written in assembly language. The first self-hosting compiler — capable of compiling its own source code in a high-level language — was created for Lisp by Hart and Levin at MIT in 1962 *. The use of high-level languages for writing compilers gained added impetus in the early 1970s when Pascal and C compilers were written in their own languages. Building a self-hosting compiler is a bootstrapping problem -- the first such compiler for a language must be compiled either by a compiler written in a different language, or (as in Hart and Levin's Lisp compiler) compiled by running the compiler in an interpreter.
Compiler construction and compiler optimization are taught at universities as part of the computer science curriculum. Such courses are usually supplemented with the implementation of a compiler for an educational programming language. A well documented example is the PL/0 compiler, which was originally used by Niklaus Wirth for teaching compiler construction in the 1970s. In spite of its simplicity, the PL/0 compiler introduced several concepts to the field which have since become established educational standards:
A compiler may produce binary output intended to run on the same type of computer and operating system ("platform") as the compiler itself runs on. This is sometimes called a native-code compiler. Alternatively, it might produce binary output designed to run on a different platform. This is known as a cross compiler. Cross compilers are very useful when bringing up a new hardware platform for the first time (see bootstrapping). Cross compilers are also necessary when developing software for microcontroller systems that have barely enough storage for the final machine code, much less a compiler. Compilers which are capable of producing both native and foreign binary output may be called either a cross compiler or a native compiler depending on a specific use, although it would be more correct to classify them as cross compilers.
Interpreters are never classified as native or cross compilers, because they do not output a binary representation of their input code.
Virtual machine (VM) compilers are typically not classified as either native or cross compilers. However, if need be, they can be classified as one or the other, especially in the less usual cases where a compiler is running inside the same VM (making it a native compiler), or where a compiler is capable of producing an output for several different platforms, including a VM (making it a cross compiler).
The ability to compile in a single pass is often seen as a benefit because it simplifies the job of writing a compiler and one pass compilers are generally faster than multi-pass compilers. Many languages were designed so that they could be compiled in a single pass (e.g., the Pascal programming language).
In some cases the design of a language feature may require a compiler to perform more than one pass over the source. For instance, when a declaration appearing on line 20 of the source affects the translation of the statement appearing on line 10; the first pass needs to gather information about declarations appearing after statements that they affect, with the actual translation happening during a second pass.
The disadvantage of compiling in a single pass is that it is not possible to perform many of the sophisticated optimizations needed to generate high quality code. It can be difficult to count exactly how many passes an optimizing compiler makes. For instance, different phases of optimization may analyse one expression many times but only analyse another expression once.
Splitting a compiler up into small programs is a technique used by researchers interested in producing provably correct compilers. Proving the correctness of a set of small programs often requiring less effort than proving the correctness of a larger, single, equivalent program.
While the typical multi-pass compiler outputs machine code from its final pass, there are several other types:
DOALL statements).
There are exceptions; some language specifications assume the use of a compiler (as with C), or spell out that implementations must include a compilation facility (as with Common Lisp). Some languages have features that are very easy to implement in an interpreter, but make writing a compiler much harder; for example, SNOBOL4, and many scripting languages are capable of constructing arbitrary source code at runtime with regular string operations, and then executing that code by passing it to a special evaluation function. To implement these features in a compiled language, programs must usually be shipped with a runtime environment that includes the compiler itself.
A compiler for a relatively simple language written by one person might be a single, monolithic, piece of software. When the source language is large and complex, and high quality output is required the design may be split into a number of relatively independent phases, or passes. Having separate phases means development can be parcelled up into small parts and given to different people. It also becomes much easier to replace a single phase by an improved one, or to insert new phases later (eg, additional optimizations).
The division of the compilation processes in phases (or passes) was championed by the Production Quality Compiler-Compiler Project (PQCC) at Carnegie Mellon University. This project introduced the terms front end, middle end (rarely heard today), and back end.
All but the smallest of compilers have more than two phases. However, these phases are usually regarded as being part of the front end or the back end. The point at where these two ends meet is always open to debate. The front end is generally considered to be where syntactic and semantic processing takes place, along with translation to a lower level of representation (than source code).
The middle (or 'analysis stage') performs optimizations on a more convenient form than either the source code (the input) or machine language (the output). Many optimizations can be performed without knowing or caring the exact architecture of the computer. In a compiler system, middle optimizations minimize the new programming needed to get a pretty good compiler for either a new computer or computer language.
The back end takes the output from the middle. It may perform more analysis, transformations and optimizations that are for a particular computer. Then, it generates code for a particular computer.
This front-end/analysis/back-end approach makes it possible to combine front ends for different languages with back ends for different CPUs.
This was actually done both in GCC and the Amsterdam compiler kit, which have multiple front-ends, shared analysis and multiple back-ends.
The work in back end is done in multiple steps:
Compiler analysis is the prerequisite for any compiler optimization and they tightly work together. For example, dependence analysis is crucial for loop transformation.
In addition, the scope of compiler analysis and optimization vary greatly, from as small as a basic block to the procedure/function level, or even over the whole program (interprocedural optimization). Obviously, a compiler can potentially do a better job using a broader view. But that broad view is not free: large scope analysis and optimizations are very costly in terms of compilation time and memory space; this is especially true for interprocedural analysis and optimizations.
The existence of interprocedural analysis and optimization is common in modern commercial compilers from IBM, SGI, Intel, Microsoft, and Sun Microsystems. The open source GCC was criticized for a long time for lacking powerful interprocedural optimizations, but it is changing in this respect. Another good open source compiler with full analysis and optimization infrastructure is Open64, which is used by many organizations for research and commercial purposes.
Due to the extra time and space needed for compiler analysis and optimization, some compilers skip them by default. Users have to use compilation options to explicitly tell the compiler which optimizations should be enabled.
#include
- include
- include
- define MODE_POSTFIX 0
- define MODE_ASSEMBLY 1
char lookahead; int pos; int compile_mode; char expression*;
void error() { printf("Syntax error!\n"); }
void match( char t ) { if( lookahead == t ) { pos++; lookahead = expression*; } else error(); }
void digit() { switch( lookahead ) { case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': if( compile_mode == MODE_POSTFIX ) printf("%c", lookahead); else printf("\tPUSH %c\n", lookahead); match( lookahead ); break; default: error(); break; } }
void term() { digit(); while(1) { switch( lookahead ) { case '*': match('*'); digit(); printf( "%s", compile_mode == MODE_POSTFIX ? "*" : "\tPOP B\n\tPOP A\n\tMUL A, B\n\tPUSH A\n"); break; case '/': match('/'); digit();
printf( "%s", compile_mode == MODE_POSTFIX ? "/" : "\tPOP B\n\tPOP A\n\tDIV A, B\n\tPUSH A\n"); break; default: return; } } }
void expr() { term(); while(1) { switch( lookahead ) { case '+': match('+'); term(); printf( "%s", compile_mode == MODE_POSTFIX ? "+" : "\tPOP B\n\tPOP A\n\tADD A, B\n\tPUSH A\n"); break; case '-': match('-'); term();
printf( "%s", compile_mode == MODE_POSTFIX ? "-" : "\tPOP B\n\tPOP A\n\tSUB A, B\n\tPUSH A\n"); break; default: return; } } }
int main ( int argc, char** argv ) { printf("Please enter an infix-notated expression with single digits:\n\n\t"); scanf("%20s", expression); printf("\nCompiling to postfix-notated expression:\n\n\t"); compile_mode = MODE_POSTFIX; pos = 0; lookahead = *expression; expr(); printf("\n\nCompiling to assembly-notated machine code:\n\n"); compile_mode = MODE_ASSEMBLY; pos = 0; lookahead = *expression; expr();
return 0; }
A possible execution of this simple compiler results in the following output:
Please enter an infix-notated expression with single digits:3-4*2+2
Compiling to postfix-notated expression:
342*-2+
Compiling to assembly-notated machine code:
PUSH 3 PUSH 4 PUSH 2 POP B POP A MUL A, B PUSH A POP B POP A SUB A, B PUSH A PUSH 2 POP B POP A ADD A, B PUSH A
Compilers | Programming language implementation
Vertalerkonstruksie | Compilador | Компилатор | Compilador | Překladač | Compiler | Compiler | Compilador | Kompilaator | Ohjelmointikielen kääntäjä | Compilateur | Compilador | מהדר | Program-prevodilac | Fordítóprogram | Kompilator | Compilatore | コンパイラ | 컴파일러 | Kompiliatorius | Compiler | Kompilator | Kompilator | Compilador | Компилятор | Compiler | Kompilator | ตัวแปลโปรแกรม | Derleyici | Trình biên dịch | 编译器 | நிரல்மொழிமாற்றி
This article is licensed under the GNU Free Documentation License.
It uses material from the
"Compiler".
Home Page • arts • business • computers • games • health • hospitals • home • kids & teens • news • physicians • recreation• reference • regional • science • shopping • society • sports • world