Download - Today ’ s Topic
TodayToday’’s Topics Topic
Breakdown of CC scriptBreakdown of CC script
TodayToday’’s Objectivess Objectives
Within context of Within context of ““compilingcompiling”” C program C program Define actions of linker, compiler, assembler, Define actions of linker, compiler, assembler, OS, macro preprocessor, loaderOS, macro preprocessor, loader
Draw block diagram showing order of linker, Draw block diagram showing order of linker, compiler, assembler, OS, macro preprocessor, compiler, assembler, OS, macro preprocessor, loaderloader
Explain why runtime stack needed for CExplain why runtime stack needed for C Draw logical division of memory into Draw logical division of memory into stack, heapstack, heap
Compare and contrast stack and heapCompare and contrast stack and heap For C program, indicate which variables For C program, indicate which variables are stored in heap; in stack; in neitherare stored in heap; in stack; in neither
Breaking Down CCBreaking Down CC
source
Preprocessed
SourceASM
O
B
J
E
C
T
.s
.o
Preprocessor
Compiler
Assembler
LinkerLoader
Executablea.out
Executable
Program
In Memory
Breaking Down CCBreaking Down CC
source
Preprocessed
SourceASM
O
B
J
E
C
T
.s
.o
Preprocessor
Compiler
Assembler
LinkerLoader
Executablea.out
Executable
Program
In Memory
C Preprocessor (cpp)C Preprocessor (cpp)
Pass over sourcePass over source Insert included filesInsert included files Perform macro substitutionsPerform macro substitutions
Define macrosDefine macros #define NUM 100#define NUM 100 #define xx(v,name,op,metrics) \ #define xx(v,name,op,metrics) \ v=xxinit(op,name,IR->metrics)v=xxinit(op,name,IR->metrics)
gcc –E example.c sends preprocessor gcc –E example.c sends preprocessor output to stdoutoutput to stdout
Breaking Down CCBreaking Down CC
source
Preprocessed
SourceASM
O
B
J
E
C
T
.s
.o
Preprocessor
Compiler
Assembler
LinkerLoader
Executablea.out
Executable
Program
In Memory
CompilerCompiler
gcc actually name of a scriptgcc actually name of a script Compiler translates one language to Compiler translates one language to another (or the same???)another (or the same???)
gcc compiler translates C to gcc compiler translates C to assemblerassembler
gcc –S example.c gcc –S example.c ““savessaves”” example.s example.s Compiler consists ofCompiler consists of
ParserParser Code generationCode generation MysticismMysticism
Breaking Down CCBreaking Down CC
source
Preprocessed
SourceASM
O
B
J
E
C
T
.s
.o
Preprocessor
Compiler
Assembler
LinkerLoader
Executablea.out
Executable
Program
In Memory
AssemblerAssembler
Another translator ??? (Another translator ??? (asas example.s)example.s)
Assembler to (binary) objectAssembler to (binary) object Why not compile straight to Why not compile straight to binary?binary?
gcc –c example.c to gcc –c example.c to ““savesave”” object object NM to look at object (nm NM to look at object (nm example.o)example.o)
Breaking Down CCBreaking Down CC
source
Preprocessed
SourceASM
O
B
J
E
C
T
.s
.o
Preprocessor
Compiler
Assembler
LinkerLoader
Executablea.out
Executable
Program
In Memory
LinkerLinker
Combine objects, both user .o Combine objects, both user .o files and libraries, make an files and libraries, make an executableexecutable
gcc *.o –lm yields a.outgcc *.o –lm yields a.out gcc –o myExec *.o –lm gcc –o myExec *.o –lm Use nm to look at objectUse nm to look at object
Breaking Down CCBreaking Down CC
source
Preprocessed
SourceASM
O
B
J
E
C
T
.s
.o
Preprocessor
Compiler
Assembler
LinkerLoader
Executablea.out
Executable
Program
In Memory
LoaderLoader
Gets an address to place Gets an address to place programprogram
Changes necessary addresses (if Changes necessary addresses (if any)any)
Places code into memory Places code into memory
Operating SystemOperating System
Oversees whole processOversees whole process ““RecognizesRecognizes”” gcc example.c command gcc example.c command Parses flags and argumentsParses flags and arguments Invokes gcc scriptInvokes gcc script Performs memory mangagement Performs memory mangagement (malloc)(malloc)
Chooses Chooses ““addressaddress”” to place program to place program