Linux Kernel Architecture

(Jacob Rumans) #1
Mauerer app03.tex V1 - 09/04/2008 6:11pm Page 1185

Appendix C: Notes on C


values are stored at the beginning of the active activation record. The variables are therefore accessed
by de-referencingebp+8andebp+12.addis used to add both values, and theeaxregister is used as the
workspace. The value is left in the register so that the result can be passed to the calling function.

The following two actions are needed to return to the calling function:

❑ The stored frame pointer value is removed from the stack usingpopand is written to theebp
register. The top end of the stack is therefore reconstructed formain.
❑ retpops the return address from the stack and goes there.

Because a further local variable (ret), in which the return value ofaddis stored, was created inmain,the
value in theeaxregister must still be copied to the appropriate position in the stack.

The rest of the assembler code (lines 14 through 24) is concerned with calling theprintfandexitlibrary
functions in the usual way.

The use of frame pointers is not mandatory. They can just as easily be omitted, because func-
tionally equivalent code can be generated without them. This is the purpose of thegccoption
-omit-frame-pointer. Because each procedure then has two fewer assembler operations, the resulting
code is a little bit faster — this is why the kernel normally dispenses with frame pointers.

However, the downside is that it is no longer possible to create stack backtraces to reconstruct the call
sequence of functions. Because backtraces yield very useful information when debugging or decoding
kernel oopses (i.e., the emergency message generated when the kernel encounters a serious problem),
the option of adding frame pointers to code was introduced when 2.5 was developed. It is advisable to
enable this option unless maximum system performance is required.

C.1.4 Optimization


Optimization is an important functionality of compilers that enables fast code to be generated with-
out modifying the effect of the program. It frees programmers of the burden of performing micro-
optimization on their programs. Instead, they are able to concentrate on writing informative, easy-to-read
C code, which the compiler then automatically translates into the best possible assembler code. Unfortu-
nately, optimization is a very complex topic that requires not only a great deal of programming skill in
C and Assembler, but also a profound knowledge of mathematics and formal logics. For this reason, the
following sections give only a brief overview of the optimization features of the GCC.

Constant Simplification


Constant simplification is one of the most basic optimization techniques — which is why much faster and
much more compact code should not be expected. The name itself suggests the direction that optimiza-
tion takes, but what does simplification actually achieve? The best way to answer this is by examining a
short C example in which values are supplied to a number of variables.

int x,y;
x = 10;
y=x+42;
const int z = y * 23;
printf("x, y, z: %d, %d, %d\n", x,y,z);
Free download pdf