SPO600 Project Stage 1 (Pt.1) - Create a GCC Pass

Create a GCC Pass

I would like to create a GCC Pass. I expect the pass will be used to analyze the GIMPLE files.

Step 1. Get to the gcc subdirectory

I go to the directory of my gcc source code. Then, I get to the gcc subdirectory inside the sourcecode of gcc.

Step 2. Create a very simple pass

I create a Pass file modeled on the pass_nrv. I just hope the pass works first. So, the pass is as simple as possible.

code:

/* Test Pass
        Jeff Yau, Seneca Polytechnic College
        Modelled on tree-nrv.cc and tree-ctyler.cc by Chris Tyler, Seneca Polytechnic College, 2024-11

This file is part of GCC.

GCC is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.

GCC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3.  If not see
<http://www.gnu.org/licenses/>.  */

//#define INCLUDE_MEMORY
//#include <stdlib.h>
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "backend.h"
#include "tree.h"
#include "gimple.h"
#include "tree-pass.h"
#include "ssa.h"
#include "tree-pretty-print.h"
#include "gimple-iterator.h"
#include "gimple-walk.h"
#include "internal-fn.h"
#include "gimple-pretty-print.h"

//added headers
#include "cgraph.h"
#include "gimple-ssa.h"
#include "attribs.h"
#include "pretty-print.h"
#include "tree-inline.h"
#include "intl.h"
#include "function.h"
#include "basic-block.h"


namespace {

const pass_data pass_data_jeff =
{
  GIMPLE_PASS, /* type */
  "jeff", /* name */
  OPTGROUP_NONE, /* optinfo_flags */
  TV_NONE, /* tv_id */
  PROP_cfg, /* properties_required */
  0, /* properties_provided */
  0, /* properties_destroyed */
  0, /* todo_flags_start */
  0, /* todo_flags_finish */
};

class pass_jeff : public gimple_opt_pass
{
public:
  pass_jeff (gcc::context *ctxt)
    : gimple_opt_pass (pass_data_jeff, ctxt)
  {}

  /* opt_pass methods: */
  bool gate (function *) final override { return 1; }

  unsigned int execute (function *) final override;

}; // class pass_jeff

  unsigned int pass_jeff::execute (function *)
  {
    struct cgraph_node *node;
    int counter = 0;
    FOR_EACH_FUNCTION (node){
      if (dump_file)
        {
          fprintf(dump_file, "Function %d's Name is '%s' \n", ++counter, node->name ());

        }
    }

    if (dump_file){
          fprintf(dump_file, "\n\n End diagnostics.\n");
    }
    return 0;

  }


} // anon namespace

gimple_opt_pass *
make_pass_jeff (gcc::context *ctxt)
{
  return new pass_jeff (ctxt);
}


Step 3. Update gcc/tree-pass.h

I put added a line in the gcc/tree-pass.h. So that the pass manager will be able to find my pass.

Step 4. Update passes.def

In the passes.def, I added my pass with instruction "NEXT_PASS (pass_jeff)" as below:-

Step 5. Update Makefile.in

In the gcc/Makefile.in, add the object file for my pass.

Step 6. Re-make the Makefile

Since the Makefile will not be updated correctly by the make Command. we have to remove the Makefile manually. Then, rebuild the makefile by:-

../gccsource/configure --prefix=$HOME/spo600/lab4/gcc-test-003

Unfortunately, there are still some error after rebuilding the gcc with the make command. 

Step 7. Delete the build file test file and rebuild everything

I decided to remove the build file and the test file to reinstall it. 
Script:
rm -rf gcc-build-003/
rm -rf gcc-test-003/
mkdir gcc-build-003
cd gcc-build-003
../gccsource/configure --prefix=$HOME/spo600/lab4/gcc-test-003
 time make -j 24 |& tee build.log
make install

Seems like it is fixed now. Output:

Step 8. test the pass with a c file

mkdir test
cd test
../gcc-test-003/bin/gcc -fdump-tree-jeff ../jeff.c -o output
cat output-jeff.c.265t.jeff

the C file:
#include <stdio.h>

int main(){
        printf("I am Jeff\n");
        return 0;
}

output:


I will modify the pass to add functionality in the next part.

Comments

Popular posts from this blog

SPO600 Project Stage 2 (Pt.1) - GCC pass locating clone function

SPO600 Project Stage 2 (Pt.2) - GCC pass locating clone function -modified version