Multi-pass compiler

Multi-pass compiler

A multi-pass compiler is a type of compiler that processes the source code or abstract syntax tree of a program several times. This is in contrast to a one-pass compiler, which traverses the program only once. Each pass takes the result of the previous pass as the input, and creates an intermediate output. In this way, the (intermediate) code is improved pass by pass, until the final pass emits the final code.

Multi-pass compilers are sometimes called wide compilers,[citation needed] referring to the greater scope of the passes: they can "see" the entire program being compiled, instead of just a small portion of it. The wider scope thus available to these compilers allows better code generation (e.g. smaller code size, faster code) compared to the output of one-pass compilers, at the cost of higher compiler time and memory consumption. In addition, some languages cannot be compiled in a single pass, as a result of their design.

Contents

Typical multi-pass compiler

Multi-passcompiler.png

Lexical analysis

This stage of a multi-pass compiler is to remove irrelevant information from the source program that syntax analysis will not be able to use or interpret. Irrelevant information could include things like comments and white space. In addition to removing the irrelevant information, the lexical analysis determines the lexical tokens of the language.

Syntax analysis

Syntax analysis is responsible for looking at syntax rules of the language (often as a context-free grammar), and building some intermediate representation of the language. An example of this intermediate representation could be something like a Abstract Syntax Tree or a Directed Acyclic Graph.

Semantic analysis

Semantic analysis takes the representation made from syntax analysis and applies semantic rules to the representation to make sure that the program meets the semantic rules requirements of the language. For example, in the example below at the stage of semantic analysis if the language required that conditions on if statements were boolean expressions the cond would be type-checked to make sure it would be a valid boolean expression.

if(cond) {
 ... 
} else {
 ...
}

In addition to performing semantic analysis at this stage of compilation, often symbol tables are created in order to assist in code generation.

Code generation

This final stage of a typical compiler converts the intermediate representation of program into an executable set of instructions (often assembly). This last stage in the only stage in compilation that is machine dependent. There can also be optimization done at this stage of compilation that make the program more efficient.

Advantages of Multi-pass compilers

Machine Independent: Since the multiple passes include a modular structure, and the code generation decoupled from the other steps of the compiler, the passes can be reused for different hardware/machines.

More Expressive Languages: Many programming languages cannot be represented with a single pass compilers, for example Pascal can be implemented with a single pass compiler since it requires that all definitions come before their use.

var x:Integer;
procedure inc;
begin
        x:=x+1;
end;
x:=0;
inc;

This in the single pass compiler when x is referenced, the semantic analysis and code generation can be done since the compiler already knows from the declaration of x:

  1. where the value of x has been stored
  2. x's type

Languages like Java require a multi-pass compiler since the definition of x would not be required to come before the use.

public class Example { 
        public static void main(String [] args) {
                assert(x==0);                     
                x++;
                assert(x==1);
        }
        static int x=0;
}

The multi-pass compiler would allocate the memory location for x during the semantic analysis phase of compilation and would have processed the declaration of x before its use.

References

  • Bent Thomsen, Languages and Compilers SProg og Overseattere, Department of Computer Science, Aalborg University

Wikimedia Foundation. 2010.

Игры ⚽ Нужна курсовая?

Look at other dictionaries:

  • multi-pass — adjective Involving several passes over something. The multi pass compiler skims the source code looking for directives on the first pass …   Wiktionary

  • One-pass compiler — In computer programming, a one pass compiler is a compiler that passes through the source code of each compilation unit only once. In other words, a one pass compiler does not look back at code it previously processed. Another term sometimes used …   Wikipedia

  • Compiler — This article is about the computing term. For the anime, see Compiler (anime). A diagram of the operation of a typical multi language, multi target compiler A compiler is a computer program (or set of programs) that transforms source code written …   Wikipedia

  • Compiler — Historisches Beispiel anhand von CBASIC Ein Compiler (auch Übersetzer oder Kompilierer genannt) ist ein Computerprogramm, das ein in einer Quellsprache geschriebenes Programm (Quelltext/Quellprogramm, meist von einem Programmierer in einer… …   Deutsch Wikipedia

  • Compiler-Front-End — Ein Compiler (auch Übersetzer oder Kompilierer genannt) ist ein Computerprogramm, das ein in einer Quellsprache geschriebenes Programm – genannt Quellprogramm – in ein semantisch äquivalentes Programm einer Zielsprache (Zielprogramm) umwandelt.… …   Deutsch Wikipedia

  • Compiler optimization — is the process of tuning the output of a compiler to minimize or maximize some attributes of an executable computer program. The most common requirement is to minimize the time taken to execute a program; a less common one is to minimize the… …   Wikipedia

  • Portable C Compiler — Entwickler PCC Team Aktuelle Version 1.0 (1. April 2011) Betriebssystem UNIX, OpenBSD, NetBSD, GNU/Linux, u. a …   Deutsch Wikipedia

  • Cross compiler — A cross compiler is a compiler capable of creating executable code for a platform other than the one on which the compiler is run. Cross compiler tools are used to generate executables for embedded system or multiple platforms. It is used to… …   Wikipedia

  • Code generation (compiler) — In computer science, code generation is the process by which a compiler s code generator converts some intermediate representation of source code into a form (e.g., machine code) that can be readily executed by a machine (often a computer).… …   Wikipedia

  • Kompilierer — Ein Compiler (auch Übersetzer oder Kompilierer genannt) ist ein Computerprogramm, das ein in einer Quellsprache geschriebenes Programm – genannt Quellprogramm – in ein semantisch äquivalentes Programm einer Zielsprache (Zielprogramm) umwandelt.… …   Deutsch Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”