HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Do "compiler" and "assembler" correspond to frontend and backend phases of a compiler?

Submitted by: @import:stackexchange-cs··
0
Viewed 0 times
backendphasescorrespondassemblercompilerandfrontend

Problem

In Compilers: Principles, Techniques, and Tools (2nd Edition) 2nd Edition
by Alfred V. Aho (Author), Monica S. Lam (Author), Ravi Sethi (Author), Jeffrey D. Ullman (Author) , Figure 1.5 gives a language processing system, and Figure 1.6 shows the phases of a compiler. In Figure 1.6, if I am correct, a compiler has a front end (the first four phasesin Figure 1.6) and a backend (the last three phases "machine independent code optimizer", "code generator" and "machine dependent code optimizer" in Figure 1.6).

Literally by the names, the "compiler" in Figure 1.5 should correspond to the all 7 phases of a compiler in Figure 1.6.

But when I try to understand the individual phases of a compiler in Figure 1.6, I found that the book says:


In App endix A, we put the techniques in this chapter together to build a
compiler front end in Java. The front end translates statements into assembly-
level instructions.

I was wondering:

Does the "backend" of the compiler in Figure 1.6 correspond to the "assembler" in Figure 1.5, and does the "frontend" of a compiler in Figure 1.6 correspond to the "compiler" in Figure 1.5?

Is the "intermediate repesentation" in Figure 1.6 the "assembly program" in Figure 1.5?

I guess the answers are all yes, but Yuval's reply mentions that an assembler does its job in only one way, while the books seems to say that each of the three steps in the backend can be done in more than one ways.

For Figure 1.5 in the book, let's take GCC as an example. In the following figure from https://medium.com/@andrewlindburg/gcc-main-c-explained-1287d91b12a7, what phases does the compiler perform, and what does the assembler perform? Does "compiler CC1" perform the first four phases in Figure 1.6, and does the "assembler AS" perform the "assembler" in Figure 1.5 and to the backend (i.e. the last three phases) in Figure 1.6 in the book above?

Thanks.

Solution

Some compilers translate source code to assembler code in one or more phases, then use an assembler which will produce bit patterns of the instructions needed, and store them in a file that the linker understands.

Many compilers don’t do this. Producing code in assembler (human readable) form takes time. Parsing the human readable form, which is the first thing the assembler has to do, takes time again. Substantial time, actually. By generating the output directly that the linker needs, substantial time can be saved.

So usually there is no assembler involved.

And please note that (1) all these charts are just examples of how you could. The UCSD Pascal compiler had one single phase for compiling. And (2) gcc is more than a compiler. There is no iron rule how a compiler has to be organized. It's just a program, and the developers of that program figure out a good way to make the software work.

PS. Do “compiler” and “assembler” in Figure 1.5 and in the GCC figure respectively correspond to frontend and backend phases of a compiler in Figure 1.6? Absolutely not. The whole Figure 1.6 is just a more detailed view of the "Compiler" box in Figure 1.5.

Context

StackExchange Computer Science Q#98854, answer score: 5

Revisions (0)

No revisions yet.