Please do not redistribute these slides without prior written permission
1
CS 3650
Computer Systems
Dr. Alden Jackson
2
Course Logistics
http://www.mshah.io/comp/Fall18/Systems/index.html
3
Last Class
4
Lecture 4 - Compilers, Linkers
Dr. Alden Jackson
Question: “Any advice for the future?
Answer: Yes, I do have one thing. Students aren't joining our field, computer science, and I don't know why. It's just such an amazing field, and it's changed the world, and we're just at the beginning of the change. We have to find a way to get our excitement out to be more publicly visible. It is exciting—in the 50 years that I've been involved, the change has been astounding.
” - Frances Allen
6
Frances Allen
7
Compilers
8
Compilers have their hands everywhere!
9
Compilation Pipeline
10
The C preprocessor [More info]
11
preprocessor.c example
12
preprocessor.c example | gcc -E preprocessor.c
13
preprocessor.c example | gcc -E preprocessor.c
14
preprocessor.c example | gcc -E preprocessor.c
15
preprocessor2.c example | gcc -E preprocessor.c
16
preprocessor2.c example | gcc -E preprocessor.c
17
preprocessor3.c example | gcc -E preprocessor.c
18
Already a few ideas in stage 1 with the preprocessor
19
Stage 1!
Stage 2: The compiler
20
Stage 2!
21
Let’s now take a look at the stages of compiling a program
22
Consider this a mini-crash course--take a full compilers course for more
A Compiler has two phases
23
A Compiler has two phases
24
The front ends main responsibility is to build an intermediate representation from which code can be generated.
A Compiler has two phases
25
The front ends main responsibility is to build an intermediate representation (i.e. not C code) from which code can be generated.
A Compiler has two phases
26
Job is to generate code (10010101...) on our machine
[Front End]
Scanning (Lexical Analysis)
27
Scanning (Lexical analysis)
28
Scanning (Lexical analysis)
29
Scanning (Lexical analysis)
30
Now each individual lexeme is categorized into tokens.
Scanning (Lexical analysis)
31
Now each individual lexeme is categorized into tokens.
However, we do not know if we have a valid program yet!
[Front End]
Parsing (Syntactic Analysis)
32
Parsing (Syntactic Analysis)
33
S = Sentence
NP = Noun Phrase
VP = Verb Phrase
V = Verb
Adv = Adverb
A = Adjective
Parsing (Syntactic Analysis)
34
A syntactically correct sentence (but semantically does not make sense) [source]
Context-free Grammar (CFG)
35
Context-free Grammar (CFG) Example
36
Context-free Grammar (CFG) Definitions
37
Context-free Grammar (CFG) Definitions
38
Production is some rule or sequence of rules. It also happens to be our ‘start rule’ listed at the top.
Context-free Grammar (CFG) Definitions
39
Terminal symbol for the production ‘Program’. It consists of 1 Statement token ‘Stmt’
Context-free Grammar (CFG) Definitions
40
The right hand side here are terminal symbols
Context-free Grammar (CFG) Definitions
41
The left hand side here are nonterminal symbols. (That is, they consist of other things after them)
Parsing
42
Abstract Syntax Tree (AST) and Concrete Trees
43
E-> E*E (Concrete Tree)
In English:Starting from the bottom left, I find E, then traverse up the tree, then down to the next child, and then up, and then to the rightmost E.
44
E-> E*E (Follow the red arrows)
In English:Starting from the bottom left, I find E from the root, then traverse up the tree, then down to the next child, and then up, and then to the rightmost E.
45
E-> identifier + E * E (A slightly larger example)
46
A Note on Abstract Syntax Trees/Concrete Trees
47
Example AST | x=1 y=2 3* x+y;
(We can derive the full parse tree from this--AST is more condensed)�(Full parse tree may be x=1; y=2; 3*(x+y);
48
[Front End]
Semantic Analysis
49
Semantic Analysis
50
Symbol Table
51
Symbol Table
52
Using symbol information for Type-checking
53
Type-checking
54
So how might we verify types? (Part of semantic analysis)
55
So how might we verify types? (Part of semantic analysis)
56
If our symbol table says ‘x’ is an int, then the right side better also be an integer value
Generation of intermediate Language
57
AST-> Intermediate representation (IR)
58
GCC, and Clang compiler IR
59
Clang’s LLVM framework has an intermediate form called bitcode
60
Intermediate Representations
61
[Typically the Middle End if it exists]
Code Optimization
62
Code Optimization
63
Code Optimization
64
We will revisit some topics in code optimization throughout the course
[Back End]
Code Generation
65
GCC and Clang compilers
66
Looking closer at Clang as a compiler
67
Telephone game example [showing information loss]
68
Short 5 minute break
69
70
Okay, we made it!
Now we can generate assembly code for our target architecture
71
Linkers
72
Linking
73
Linking
74
Example with the linker
Here are two different c files (main.c on the left, and sum.c on the right).
75
Example with the linker
In our main.c file, sum is declared but not defined. The body of code for sum is in sum.c.
76
The Linkers job is to combine two (or more) files
Note that main.o and sum.o are generated from our compiler.
77
The Linkers job is to combine two (or more) files
Note that main.o and sum.o are generated from our compiler.
78
Compiler does this part
The Linkers job is to combine two (or more) files
Note that main.o and sum.o are generated from our compiler.
79
Linker combines output
Open Question: Why use a Linker? Your thoughts?
80
Why use a Linker? Modularity
81
Why use a Linker? Modularity
82
Why use a Linker? Time Efficiency
83
Why use a Linker? Space Efficiency
84
The Linker has 2 jobs
85
Reminder: Linker from a 40,000 foot view
86
What does the linker do? (Job 1) |Symbol Resolution
87
What does the linker do? (Job 1) |Symbol Resolution
88
Example (Finding the symbols)
89
Remember objdump? objdump -t sum
This gives us the symbol table of an executable or .o file
90
What does the linker do? (Job 2)| Relocation
91
Relocation example from: http://www.bravegnu.org/gnu-eprog/linker.html
92
Linker works with the three kind of object files to perform its job
93
Future Reading
94
In-Class Activity
95
In-Class Activity
96