3
$\begingroup$

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).

enter image description here

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.

enter image description here

$\endgroup$
4
$\begingroup$

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.

$\endgroup$
  • $\begingroup$ Thanks. In GCC, what phases does the compiler perform, and what does the assembler perform? $\endgroup$ – Tim Oct 20 '18 at 23:55
  • $\begingroup$ 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? $\endgroup$ – Tim Oct 21 '18 at 12:56
1
$\begingroup$

The "front end" of the compiler is the part that is intimately related to the source language, the "back end" depends on the target. Many back ends emit assembly language for convenience (the object file contains not only binary instructions, but also places to fill in with addresses of external variables and functions, data for patching up to be placed at different addresses in memory, ...).

This separation (and the interpolation of the ridiculously named "middle end") was fueled by the desire of getting compilers for lots of languages for a plethora of targets. Less pressing today, but there are still differences between CPU makes and models. The "middle end" collects lots of language- and target-independent transformations that aim at better code (get rid of code that never runs, use knowledge that e.g. a variable can't be zero to avoid to check for this, compute common expressions just once, handle tail recursion, the list is long).

$\endgroup$
  • $\begingroup$ Thanks. 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? $\endgroup$ – Tim Oct 22 '18 at 2:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.