0% found this document useful (0 votes)
2 views

05-3-compilers-1

Uploaded by

JohnKevinStanley
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

05-3-compilers-1

Uploaded by

JohnKevinStanley
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Advanced Programming in the

UNIX Environment
Week 05, Segment 3:
Unix Development Tools:
The Compiler Chain, Part I

Department of Computer Science


Stevens Institute of Technology

Jan Schaumann
[email protected]
https://stevens.netmeister.org/631/
CS631 - Advanced Programming in the UNIX Environment

Software Development Tools

The UNIX Userland is an IDE – essential tools that follow the paradigm of “Do one
thing, and do it right” can be combined.

The most important tools are: You are here.


• $EDITOR ✅
• the compiler toolchain
• gdb(1) – debugging your code
• make(1) – project build management, maintain program dependencies
• diff(1) and patch(1) – report and apply di erences between les
• cvs(1), svn(1), git(1) etc. – revision control, distributed project management

Jan Schaumann 2021-09-06


ff
fi
CS631 - Advanced Programming in the UNIX Environment

Compilers

00101001011100110101110011010101101011010
110011010110101100110010101101101101010011

#include <stdio.h>

int
main(int argc, char **argv) {
printf(“Hello, World!\n”);
}

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

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!” “;“

Jan Schaumann 2021-09-06


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:
• preprocessing
• lexical analysis
if (num > 42) msg = “Don’t panic!”;

• syntax analysis if

greater than assign

num 42 msg “Don’t panic!”

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
if (num > 42) msg = “Don’t panic!”;

• syntax analysis if
• semantic analysis

greater than assign

num 42 msg “Don’t panic!”

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
if (msg > 42) num = “Don’t panic!”;

• syntax analysis if “Ok, this part makes sense.”

• semantic analysis

greater than assign


“Wait a second, you “Assigning a string to
can’t compare a string a number makes no
to a number!” sense!”
msg 42 num “Don’t panic!”

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 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
}

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: .file “hw.c"
.text

• preprocessing .LC0:
.section .rodata

.string "Hello world"


• lexical analysis .text
.globl main
.type main, @function
• syntax analysis #include <stdio.h> main:
.LFB3:


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

C Code C++ Code Objective-C Code Fortran Code Ada Code

C speci c C++ speci c Objective-C speci c Fortran speci c Ada speci c


- preprocessing - preprocessing - preprocessing - preprocessing - preprocessing
- lexical analysis - lexical analysis - lexical analysis - lexical analysis - lexical analysis
- syntax analysis - syntax analysis - syntax analysis - syntax analysis - syntax analysis
- semantic analysis - semantic analysis - semantic analysis - semantic analysis - semantic analysis

Intermediate Code Generation and Optimization

i386 object code x86-64 object code ARM64 object code


& executable & executable & executable
Jan Schaumann 2021-09-06
fi
fi
fi
fi
fi
CS631 - Advanced Programming in the UNIX Environment

Compilers

There are many di erent closed- and open-source compiler chains:


• Intel C/C++ Compiler (or icc)
• Turbo C / Turbo C++ / C++ Builder (Borland)
• Microsoft Visual C++
• Clang (a frontend to LLVM)
• GNU Compiler Collection (or gcc)
• Portable C Compiler (or pcc)
• …

Jan Schaumann 2021-09-06


ff
CS631 - Advanced Programming in the UNIX Environment

The GNU Compiler Collection

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…

Jan Schaumann 2021-09-06

You might also like