05-3-compilers-1
05-3-compilers-1
UNIX Environment
Week 05, Segment 3:
Unix Development Tools:
The Compiler Chain, Part I
Jan Schaumann
[email protected]
https://stevens.netmeister.org/631/
CS631 - Advanced Programming in the UNIX Environment
The UNIX Userland is an IDE – essential tools that follow the paradigm of “Do one
thing, and do it right” can be combined.
Compilers
00101001011100110101110011010101101011010
110011010110101100110010101101101101010011
#include <stdio.h>
int
main(int argc, char **argv) {
printf(“Hello, World!\n”);
}
Compilers
A compiler translates source code from a high-level programming language into machine code
for a given architecture by performing a number of steps:
• preprocessing
…
void perror(const char *);
/usr/include/stdio.h int printf(const char * __restrict, …)
__print ike(1, 2);
int putc(int, FILE *);
#include <stdio.h> int putchar(int);
#de ne NUM 42 …
int int
main(int argc, char **argv) { main(int argc, char **argv) {
printf(“%d\n”, NUM); printf(“%d\n”, 42);
} }
Jan Schaumann 2021-09-06
fi
fl
CS631 - Advanced Programming in the UNIX Environment
Compilers
A compiler translates source code from a high-level programming language into machine code
for a given architecture by performing a number of steps:
• preprocessing
• lexical analysis
if (num
num > 42) msg = “Don’t panic!”;
panic!”
Keyword: Token: Identi er: Operator: Number: Token: Identi er: Token: String: Token:
“if” “(“ “num” “>” 42 “)“ “msg” “=“ “Don’t panic!” “;“
Compilers
A compiler translates source code from a high-level programming language into machine code
for a given architecture by performing a number of steps:
• preprocessing
• lexical analysis
if (num > 42) msg = “Don’t panic!”;
• syntax analysis if
Compilers
A compiler translates source code from a high-level programming language into machine code
for a given architecture by performing a number of steps:
• preprocessing
• lexical analysis
if (num > 42) msg = “Don’t panic!”;
• syntax analysis if
• semantic analysis
Compilers
A compiler translates source code from a high-level programming language into machine code
for a given architecture by performing a number of steps:
• preprocessing
• lexical analysis
if (msg > 42) num = “Don’t panic!”;
• semantic analysis
Compilers
A compiler translates source code from a high-level programming language into machine code
for a given architecture by performing a number of steps:
• preprocessing
• lexical analysis int i;
unused
• syntax analysis void func() { redundant int i;
———
int j;
• semantic analysis ———
j = 42; void func() {
———
i = 5; i = 10;
• code generation i = 10; return;
return; }
• code optimization ———
i = 20; dead code
}
Compilers
A compiler translates source code from a high-level programming language into machine code
for a given architecture by performing a number of steps: .file “hw.c"
.text
• preprocessing .LC0:
.section .rodata
•
.cfi_startproc
semantic analysis int pushq %rbp
.cfi_def_cfa_offset 16
main(int argc, char **argv) {
• code generation printf(“Hello, World!\n”);
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
}
• code optimization movl
call
$.LC0, %edi
puts
movl $0, %eax
popq %rbp
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE3:
.size main, .-main
Jan Schaumann .ident 2021-09-06
"GCC: (nb4 20200810) 7.5.0”
CS631 - Advanced Programming in the UNIX Environment
Compilers
A compiler translates source code from a high-level programming language into machine code
for a given architecture by performing
.file
.text
a number
“hw.c" of steps: 00000000 7f 45 4c 46 02 01 01 00 | .ELF....|
00000010 00 00 00 00 00 00 00 00 | ........|
•
.section .rodata 00000020 01 00 3e 00 01 00 00 00 | ..>.....|
preprocessing .LC0: 00000030 00 00 00 00 00 00 00 00 | ........|
.string "Hello world" *
• lexical analysis
.text
.globl main
00000050
00000060
58
00
02
00
00
00
00
00
00
40
00
00
00
00
00
00
|
|
X.......|
....@...|
.type main, @function 00000070 00 00 40 00 0c 00 0b 00 | ..@.....|
• syntax analysis main:
.LFB3:
00000100
00000110
55
00
48
e8
89
00
e5
00
bf
00
00
00
00
b8
00
00
|
|
UH......|
........|
.cfi_startproc 00000120 00 00 00 5d c3 48 65 6c | ...].Hel|
• semantic analysis pushq %rbp
.cfi_def_cfa_offset 16
00000130
00000140
6c
00
6f
00
20
47
77
43
6f
43
72
3a
6c
20
64
28
|
|
lo world|
..GCC: (|
•
.cfi_offset 6, -16 00000150 6e 62 34 20 32 30 32 30 | nb4 2020|
code generation movq %rsp, %rbp 00000160 30 38 31 30 29 20 37 2e | 0810) 7.|
.cfi_def_cfa_register 6 00000170 35 2e 30 00 00 00 00 00 | 5.0.....|
• code optimization
movl
call
$.LC0, %edi
puts
00000200
00000210
14
01
00
7a
00
52
00
00
00
01
00
78
00
10
00
01
|
|
........|
.zR..x..|
movl $0, %eax 00000220 1b 0c 07 08 90 01 00 00 | ........|
• assembly popq %rbp
.cfi_def_cfa 7, 8
00000230
00000240
1c
00
00
00
00
00
00
00
1c
15
00
00
00
00
00
00
|
|
........|
........|
ret 00000250 00 41 0e 10 86 02 43 0d | .A....C.|
.cfi_endproc 00000260 06 50 0c 07 08 00 00 00 | .P......|
.LFE3: 00000270 00 00 00 00 00 00 00 00 | ........|
.size main, .-main *
Jan Schaumann 00000320 01 00 00 00 04 00 f1 ff 2021-09-06
| ........|
CS631 - Advanced Programming in the UNIX Environment
Compilers
A compiler translates source code from a high-level programming language into machine code
for a given architecture by performing a number of steps:
• preprocessing
• lexical analysis
• syntax analysis
• semantic analysis
• code generation
• code optimization
• assembly
• linking
Jan Schaumann 2021-09-06
CS631 - Advanced Programming in the UNIX Environment
Compilers
A compiler translates source code from a high-level programming language into machine code
for a given architecture by performing a number of steps:
• preprocessing
• lexical analysis
programming language speci c
• syntax analysis
• semantic analysis
• code generation
compiler speci c
• code optimization
• assembly platform speci c
• linking
Jan Schaumann 2021-09-06
fi
fi
fi
CS631 - Advanced Programming in the UNIX Environment
Compilers
A compiler translates source code from a high-level programming language into machine code
for a given architecture by performing a number of steps:
Compilers
GCC is the default on many Unix platforms, although some have recently switched to
clang(1) / LLVM.
The compiler chain or driver usually performs preprocessing (e.g. via cpp(1)),
compilation (cc(1)), assembly (as(1)) and linking (ld(1)).
To be continued…