Skip to content

Instantly share code, notes, and snippets.

@aolo2
Created March 20, 2018 18:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aolo2/7c44135dbf46d4250fe358fa58bdbbf5 to your computer and use it in GitHub Desktop.
Save aolo2/7c44135dbf46d4250fe358fa58bdbbf5 to your computer and use it in GitHub Desktop.
#include <gcc-plugin.h>
#include <plugin-version.h>
#include <coretypes.h>
#include <tree.h>
#include <gimple.h>
#include <gimple-iterator.h>
#include <tree-pass.h>
#include <context.h>
#include <basic-block.h>
int plugin_is_GPL_compatible;
static struct plugin_info gimple_print_info =
{
version: "1.0",
help: "This plugin prints the GIMPLE/IR"
};
const pass_data gimple_print_data =
{
type: GIMPLE_PASS,
name: "gimple-print pass",
optinfo_flags: OPTGROUP_NONE,
tv_id: TV_NONE,
properties_required: PROP_gimple_any,
properties_provided: 0,
properties_destroyed: 0,
todo_flags_start: 0,
todo_flags_finish: 0
};
struct gimple_print_pass : gimple_opt_pass
{
gimple_print_pass(gcc::context *ctx)
: gimple_opt_pass(gimple_print_data, ctx)
{
}
virtual unsigned execute(function *fun) override
{
basic_block bb;
FOR_ALL_BB_FN(bb, fun)
{
gimple *stmt;
gimple_bb_info *bb_info = &bb->il.gimple;
switch (bb->index)
{
case 0:
printf("ENTRY block of function \'%s\'\n", function_name(fun));
break;
case 1:
printf("EXIT block of function \'%s\'\n", function_name(fun));
break;
default:
printf("INNER block of function \'%s\'\n", function_name(fun));
for (gimple_stmt_iterator gsi = gsi_start(bb_info->seq); !gsi_end_p(gsi); gsi_next(&gsi))
{
stmt = gsi_stmt(gsi);
}
}
}
return 0;
}
};
/* Return 0 on success or error code on failure */
int plugin_init(struct plugin_name_args *info, // Argument information */
struct plugin_gcc_version *ver) // Version info of GCC
{
/* Check that the GCC version is in line with the plugin requirements */
if(!plugin_default_version_check(ver, &gcc_version))
{
printf("This GCC plugin is for GCC version %d.%d\n", GCCPLUGIN_VERSION_MAJOR, GCCPLUGIN_VERSION_MINOR);
return 1;
}
/*
* Used to tell the plugin-framework about where we want to be called in the
* set of all passes. This is located in tree-pass.h
*/
struct register_pass_info pass;
/*
* Setup the info to register with GCC telling when we want to be called and
* to what GCC should call, when it's time to be called.
*/
pass.pass = new gimple_print_pass(g);
/*
* Get called after GCC has produced the SSA representation of the program.
* After the first SSA pass.
*/
pass.reference_pass_name = "ssa";
pass.ref_pass_instance_number = 1;
pass.pos_op = PASS_POS_INSERT_AFTER;
/* Tell GCC we want to be called after the first SSA pass */
register_callback("gimple-print", PLUGIN_PASS_MANAGER_SETUP, NULL, &pass);
/* Tell GCC some info about the plugin (used when --version or --help are called) */
register_callback("gimple-print", PLUGIN_INFO, NULL, &gimple_print_info);
/* Success */
printf("GIMPLE-PRINT plugin initialized...\n");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment