Lab Assgn1
Lab Assgn1
Lab Assgn1
Lab Assignment 1 :
Objective : Get familiar with the working of a production quality open source compiler : gcc or g++
Ability to view and understand the internal dumps produced which shall be used as benchmark for the
experiments that you will conduct henceforth.
1.0 Use putty and the login credentials given by the technical staff to log into the Unix/Linux server.
Create a new directory with your roll-number. For every lab session, create a sub-directory, suitable
named such as, Lab1, Lab2 and save all your work records of a particular lab session there till end of
the course.
1.1 Learn a few useful options that can be used when compiling a source program and their
implications
1.2 Write a simple program in C / C++ and compile through the options one by one.
1.3 Generate the various intermediate dumps generated by the compiler and compare them with the
given source, particularly the original, gimple
1.4 Generate the assembly and try to correlate with the source.
1.5 Use the optimization switch and examine its effect on the source program. Compare the effects of
assembly code generated by gcc without and with use of the Optimization flag.
You are advised to write your own C / C++ program and run it through all the suggested experiments.
You may use the program given below as a default otherwise.
Read the Appendix for some useful Information about the gcc compiler.
SP2024/CS 334 : Compiler Design Lab / Lab 1 :Working wth gcc / Anup & Supratim / 1
APPENDIX : BASIC SWITCHES OF gcc / g++ compilers
1. Use of the compiler switch -c : this switch informs the compiler that the user is only
interested in compiling the submitted program but does not want the executable to be
generated.
2. Use of the compiler switch -o : this switch informs the compiler that the user is only
interested in giving her/his own name to the generated executable file and override the default
name “a.out” used by the compiler. It is better to give a name to each executable program,
instead of using the default “a.out”, because the latter gets overwritten after every compilation,
while the former remains saved with us.
3. Use of compiler switch -S : assembly code is generated by the compiler and saved in the file
“progname.s”. The assembly code generated directly by this switch is difficult to read.
$ gcc -S firstprog.c will generate assembly code in firstprog.s
A better human readable assembly code (annotated assembly) is generated using an additional
switch, “-fverbose-asm” , for example $ gcc -S -fverbose-asm firstprog.c will generate
annotated assembly in firstprog.s
4. Use of the verbose switch “-v” of gcc
In contrast to the common misconception that gcc (gnu C compiler) is the compiler proper, it
is in reality a shell script that calls various tools for performing compilation and related tasks.
The switch “-v” can be used to know the internal structure of gcc. The switch “-v” usually
stands for verbose description.
5. Generating intermediate compiler dumps in gcc
In order to generate the intermediate dumps produced by gcc, we use the switches given
below. Note that there may be small variations in the dumps and their formats depending on
the gcc version used. The verbose dump gives the version of gcc and version of ubuntu that is
used in my laptop. Earlier or later versions may give slightly different dumps, gcc version
9.4.0 (Ubuntu 9.4.0-1ubuntu1~20.04.1). Earlier or later versions would give slightly different
dumps. $ gcc --version will inform the version of the compiler.
Observations : Let the command be $ gcc fdump-tree-all firstprog.c
The internal files that are generated are named as “firstprog.c.dddt.name”, that is the source
program name suffixed with dot (.) followed by 3 digits followed by t; which in turn is
followed by a file-name. The naming convention used is the following.
Pass number
Source Intermediate
separator file name
program
SP2024/CS 334 : Compiler Design Lab / Lab 1 :Working wth gcc / Anup & Supratim / 2
6. Understanding the Intermediate Compiler Dumps
In the figure above, the file “gimple” is the dump produced by gcc after the 5 th pass. Also it
appears that gcc performed a large number of passes and the result are saved in an
appropriately named intermediate dump file.
• It may be noted that gimple is the name of intermediate language used by gcc (other compilers
may use AST, RTL, etc instead). The result of the 5 th pass is therefore the source program
translated into gimple language.
• The other passes use appropriate names to indicate the action performed by the pass. For
example, the 4th pass gives the original program after processing; the 12 th pass generates the
control flow graph (cfg); the 19th pass translated the intermediate code to static single
assignment (ssa); 231st pass produces an optimized version, and so on.
A small digression for those who are NOT familiar with shell programming. There are 3 operators used
by the shell for i/o redirection (redirecting the standard input, standard output and standard error files to
other files chosen by an user rather than their default binding to device files.
SP2024/CS 334 : Compiler Design Lab / Lab 1 :Working wth gcc / Anup & Supratim / 3