create new tag
view all tags

Project for CSC 255/455, Spring 2011

Project 1. Trivial: Adding a pass in GCC

10%, Individual Project, Due: Wed. Jan. 26 11:59pm


For a taste of the most widely used compiler, you will implement a simple pass inside GCC. If you follow the instructions below, the project should not take more than four hours. The purpose is to learn the process more than to learn the compiler. You will use a different compiler in later projects but the basic three-phase structure --- a front-end, a set of optimization passes, and a back-end ---is the same.

Install GCC

Here we use GCC-4.2.2. It is not the most recent GCC, but should be sufficient for our project. Check out the code from GCC read-only repository, following the commands below. For more information about GCC debugging and data structure, check out the instructions, pointers, and discussions from 2009 project page.

**step #1 Check out gcc source code. It will over 700MB disk space, let's not to store it in your home directory in your CS account. Use either your personal machine or the local disk on one of the public use machines in the CS department. The local disk in these machines can be accessed at /localdisk. Create your own directory first. The local disk is not shared. You have to log into the machine to access its local disk. If you use your personal machine, please note that for testing purpose, you will need to have your compiler and the class repository accessible from your machine, and the machine should be able to run Unix commands and Ruby scripts.

    cd  /localdisk/your_dir
    svn co svn://gcc.gnu.org/svn/gcc/tags/gcc_4_2_2_release gcc-4.2.2

**step #2 Apply the cs255 patch file and copy in cs255.c. They are in the class repository under projects/1_gcc/base/.

   cd gcc-4.2.2
   cd gcc
   patch -i [class_repos]/projects/1_gcc/base/cs255.patch
   cp [class_repos]/projects/1_gcc/base/cs255.c .
   cd ../..

**step #3 Install GCC

    mkdir obj
    cd obj
    ../gcc-4.2.2/configure --prefix=$INSTALL_DIR 
        // $INSTALL_DIR is the directory you want to install your compiler, such as $HOME/gcc-build
        // If you encounter the error "error: Building GCC requires GMP 4.1+ and MPFR 2.3.0+", 
        // please download gmp or mpfr from ftp://gcc.gnu.org/pub/gcc/infrastructure 
        // and put the extracted directory under $HOME/gcc/gcc-4.2.2/
    make install

* If you are not familiar with subversion, take a look at the VersionStuff page.

* Gnu's wiki page on the generic installation procedure for GCC.

Add the cs255 pass

You will add a pass that will insert code into a program to count and output the number of executed statements when the program executes. In the cs255.patch, you are provided with a file cs255.c, which has code to first add a call init() at the beginning of the main function, which has a global counter initialized to 0; then add a call before each (gimple---GCC intermediate form) statement, which increments the counter; and finally add a call at the exit, which reports the run-time instruction counts. The three functions are provided in 1_gcc/base/cs255-lib.[ch].

* Put your own code into gcc-4.2.2/gcc/cs255.c

* Initialization: you'll need to add it at the entry of the main function

* Increment: to be added before each real gimple statements (all except for labels)

* Report: to be added at all possible exits of the program

* Remember to add #include "cs255-lib.h" in your testing programs.

* Invoke your pass: use gcc -O2 -fcs255 your_test_prog cs255-lib.c

This part should take at most tens of lines of code and no more than a day (including half an hour compiling the compiler for the first time). The purpose is to get you familiar with GCC and the basic GCC functions that you'll need for adding your optimization passes. Reading the relevant, existing code (in gcc-4.2.2/gcc/cs255.c) is the best way to help you implementing the required pass. For instance, the code for counting the number of basic blocks and statements statically is already in the file. The code for getting the dynamic count of the basic blocks is also given.

For testing, we use the test cases in directory projects/test_cases. You may use the results from them to check your code with your classmates. And they are very useful in following parts and final competition.

Automated testing

You are provided with a testing harness at projects/1_gcc/base/test.rb. Suppose your current working directory is projects/1_gcc/paul, you can use

../base/test.rb ab.c /usr/bin/gcc

to test the default gcc on the test ab.c (or any other file in test_cases), or you can use

../base/test.rb all /user/bin/gcc

to test all C programs in the test directory. Some of the programs require inputs. The test harness provides the default inputs. You can test your cs255 compiler similarly by modifying test.rb, in particular, uncommenting the two lines marked in the file. In addition, supply your compiler in place of /user/bin/gcc as the second argument when running test.rb.

Submit your project

Create a directory in the repository as projects/1_gcc/your_cs_user_id. Submit the source code (only the modified cs255.c for project #1), along with a report document, either in text or pdf, describing the design and implementation.

Your report should contain the instruction counts generated by your compiler for all test programs with the standard inputs. Use the updated projects/1_gcc/base/test.rb (as of revision 28), which generates a summary of results when it is run with "all" as the second parameter. You should include the summary in the report.

Edit | Attach | Watch | Print version | History: r5 < r4 < r3 < r2 < r1 | Backlinks | Raw View | Raw edit | More topic actions
Topic revision: r5 - 2011-01-24 - ChenDing
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2020 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding URCS? Send feedback