One-pass compiler
In computer programming, a one-pass compiler is a compiler that processes each compilation unit only once, sequentially translating each source statement or declaration into something close to its final machine code. This is in contrast to a multi-pass compiler which converts the program into one or more intermediate representations in steps between source code and machine code, and which reprocesses the entire compilation unit in each sequential pass.
A one-pass compiler initially has to leave addresses for forward jumps unresolved. These can be handled in various ways without needing to have another complete pass. Some nominally one-pass compilers effectively 'pass the buck' by generating assembly language and letting the assembler sort out the forward references, but this requires one or more passes in the assembler.
One-pass compilers are smaller and faster than multi-pass compilers.
One-pass compilers are unable to generate as efficient programs as multi-pass compilers due to the limited scope of available information. Many effective compiler optimizations require multiple passes over a basic block, loop, subroutine, or entire module. Some require passes over an entire program.
Difficulties
A core requirement for any programming language intended for one-pass compilation is that all user-defined identifiers, except possibly labels, must be declared before use:- Pascal contains a forward specification to allow routines to be mutually recursive.
- PL/I allows data declarations to be placed anywhere within a program, specifically, after some references to the not-yet-declared items, so one pass is needed to resolve data declarations, followed by one or more passes to generate code.
Languages which rely on context for statement identification rather than using reserved or stropped keywords may also cause problems. The following examples are from Fortran 77:
IF l1,l2 ! two-way branch, where B is a boolean/logical expression
IF l1,l2,l3 ! three-way branch, where N is a numeric expression
IF THEN ! start conditional block
IF THEN = 3.1 ! conditional assignment to variable THEN
IF X = 10 ! single conditional statement
IF GOTO l4 ! conditional jump
IF = 2 ! assignment to a subscripted variable named IF
DO 12 I = 1,15 ! start of a count-controlled loop
DO 12 I = 1.15 ! assignment of the value 1.15 to a variable called
DO12IThe entire statement must be scanned to determine what sort of statement it is; only then can translation take place. Hence any potentially ambiguous statements must be processed at least twice.