compiler - History, Types of compilers, Compiler design, Front end, A compiler example
A computer program which translates (compiles) the source code of a high-level computer language program, such as BASIC, into a set of machine-code instructions which can be understood by the central processing unit. Compilers are very large programs, and contain error-checking and other facilities.
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 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.
History
Early computers did not use compilers. 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. In spite of its simplicity, the PL/0 compiler introduced several concepts to the field which have since become established educational standards:
The use of Program Development by Stepwise Refinement The use of a Recursive descent parser The use of EBNF to specify the syntax of a language The use of P-Code during generation of portable
output code The use of T-diagrams for the formal description of the bootstrapping problem
Types of compilers
There are many ways to classify compilers according to the input and output, internal structure, and runtime behavior. For example,
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, language converter, or language rewriter (this last term is usually applied to translations that do not involve a change of language)Native versus cross compiler
Most compilers are classified as either native (or hosted) compilers or cross compilers.
A native or host compiler is one whose output is intended to run on the same type of computer and operating system ("platform") as the compiler itself runs on.
Interpreters and Virtual machine (VM) compilers are not usually classified as native or cross compilers.
One-pass versus multi-pass compilers
Classifying compilers by number of passes has its background in the hardware resource limitations of computers.
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., Pascal).
In some cases the design of a language feature may require a compiler to perform more than one pass over the source.
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.
Splitting a compiler up into small programs is a technique used by researchers interested in producing provably correct compilers.
While the typical multi-pass compiler outputs machine code from its final pass, there are several other types:
A "source-to-source compiler" is a type of compiler that takes a high level language as its input and outputs a high level language. For example, an automatic parallelizing compiler will frequently take in a high level language program as an input and then transform the code and annotate it with parallel code annotations (e.g. Stage compiler that compiles to assembly language of a theoretical machine, like some Prolog implementations This Prolog machine is also known as the Warren Abstract Machine (or WAM). Just-in-time compiler, used by Smalltalk and Java systems, and also by Microsoft .Net's Common Intermediate Language (CIL) Applications are delivered in bytecode, which is compiled to native machine code just prior to execution.Compiled versus interpreted languages
Many people divide higher-level programming languages into compiled languages and interpreted languages. However, there is rarely anything about a language that requires it to be compiled or interpreted. Compilers and interpreters are implementations of languages, not languages themselves. The categorization usually reflects the most popular or widespread implementations of a language -- for instance, BASIC is thought of as an interpreted language, and C a compiled one, despite the existence of BASIC compilers and C interpreters. some language specifications spell out that implementations must include a compilation facility (eg, Common Lisp), while other 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.
Hardware compilation
The output of some compilers may target hardware at a very low level, eg a Field Programmable Gate Array (FPGA). Such compilers are said to be hardware compilers because the programs they compile effectively control the final configuration of the hardware and how it operates;
Compiler design
The approach taken to compiler design is affected by the complexity of the processing that needs to be done, the experience of the person(s) designing it, and the resources (eg, people and tools) available.
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.
The division of the compilation processes in phases (or passes) was championed by the Production Quality Compiler-Compiler Project (PQCC) at Carnegie Mellon University.
The middle end is usually designed to perform optimizations on a form other than the source code or machine code. This source code/machine code independence is intended to enable generic optimizations to be shared between versions of the compiler supporting different languages and target processors.
This front-end/middle/back-end approach makes it possible to combine front ends for different languages with back ends for different CPUs.
Front end
The front end analyses the source code to build an internal representation of the program, called the intermediate representation or IR. Languages which strop their keywords (and allow arbitrary spaces within identifiers) require a phase before parsing, which is to take the input source and convert it to a canonical form ready for the parser. Algol, Coral66, Atlas Autocode, and Imp are examples of stropped languages whose compilers would have a Line Reconstruction phase. Some languages, e.g., C, require a preprocessing phase to do things such as conditional compilation and macro substitution. Semantic analysis is the phase that checks the meaning of the program to ensure it obeys the rules of the language.
The work in the back end is done in multiple steps:
Compiler analysis - This is the process to gather program information from the intermediate representation of the input source files. Optimization - the intermediate language representation is transformed into functionally equivalent but faster (or smaller) forms. Code generation - the transformed intermediate language is translated into the output language, usually the native machine language of the system.Compiler analysis is the prerequisite for any compiler optimization, and they tightly work together.
In addition, the scope of compiler analysis and optimizations vary greatly, from as small as a basic block to the procedure/function level, or even over the whole program (interprocedural optimization).
The existence of interprocedural analysis and optimizations is common in modern commercial compilers from IBM, SGI, Intel, Microsoft, and Sun Microsystems.
Due to the extra time and space needed for compiler analysis and optimizations, some compilers skip them by default.
A compiler example
The following program represents a very simple one-pass compiler, written in C. This compiler compiles an expression defined in infix notation to postfix notation and also into an assembly-like machine language. } 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); }
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
User Comments Add a comment…