Difference Between Phase and Pass in Compiler

In general, compiler is a computer program that reads a program written in one language, which is called the source language, and translates it in to another language, which is called the target language. Traditionally, source language was a high level language such as C++ and target language was a low level language such as Assembly language. So, in general compilers can be seen as translators that translate from one language to another. Pass and Phase are two terms often used with compilers. Number of passes of a compiler is the number of times it goes over the source (or some form of representation of it). A compiler is broken down in to parts for the convenience of construction. Phase is often used to call such a single independent part of a compiler.

What is a Pass in a Compiler?

A standard way to classify compilers is by the number of “passes”. Usually, compiling is a relatively resource intensive process and initially computers did not have enough memory to hold such a program that did the complete job. Due to this limitation of hardware resources in early computers, compilers were broken down in to smaller sub programs that did its partial job by going over the source code (made a “pass” over the source or some other form of it) and performed analysis, transformations and translation tasks separately. So, depending on this classification, compilers are indentified as one-pass or multi-pass compilers.

As the name suggests, one-pass compilers compiles in a single pass. It is easier to write a one-pass compiler and also they perform faster than multi-pass compilers. Therefore, even at the time when you had resource limitations, languages were designed so that they could be compiled in a one-pass (e.g. Pascal). On the other hand, a typical multi-pass compiler is made up of several main stages. The first stage is the scanner (also known as the lexical analyzer). Scanner reads the program and converts it to a string of tokens. The second stage is the parser. It converts the string of tokens in to a parse tree (or an abstract syntax tree), which captures the syntactic structure of the program. Next stage is the  that interpret the semantics of the syntactic structure. The code optimizations stages and final code generation stage follow this.

What is a Phase in a Compiler?

The term phase often comes up when you talk about compiler construction. Initially, compilers were every simple pieces of single, monolithic software written by one person for the compilation of a simple language. But when the source code of the language to be translated becomes complex and large, the compiler was broken down in to multiple (relatively independent) phases. The advantage of having different phases is that the development of the compiler can be distributed among a team of developers. Furthermore, it improves the modularity and reuse by allowing phases to be replaced by improved ones or additional phases (such as further optimizations) to be added to the compiler. The process of dividing the compilation to phases was introduced by the PQCC (Production Quality Compiler-Compiler Project) at Carnegie Melon University. They introduced the terms front end, middle end and back end. Most compilers have at least two phases. But usually, back end and front end encapsulate these phases.

What is the difference between Phase and Pass in Compiler?

Phase and Pass are two terms used in the area of compilers. A pass is a single time the compiler passes over (goes through) the sources code or some other representation of it. Typically, most compilers have at least two phases called front end and back end, while they could be either one-pass or multi-pass. Phase is used to classify compilers according to the construction, while pass is used to classify compilers according to how they operate.