首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何注册GIMPLE_PASS?

如何注册GIMPLE_PASS?
EN

Stack Overflow用户
提问于 2014-09-02 14:41:57
回答 1查看 370关注 0票数 2

我试着做一个简单的插件例子,如:

代码语言:javascript
复制
#include "gcc-plugin.h"
#include "tree.h"
#include "gimple.h"
#include "tree-pass.h"
#include <stdio.h>

extern void
test(void*gcc_data, void*b) {
    printf("Hellow world\n");
}

extern int plugin_init (struct plugin_name_args *plugin_info,
                        struct plugin_gcc_version *version)
{
    const char * nombre = "Hello world";
    register_callback(nombre, GIMPLE_PASS, &test, NULL);

    return 0;
}

但是GIMPLE_PASSgcc-plugin.h中不是一个预定义的事件,我知道我必须在tree-pass.h中使用PLUGIN_PASS_MANAGER_SETUPstruct pass_data来做一些事情,但是我不知道具体的方法,也没有找到任何例子。

有人会教我怎么做?iThanks。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-04-01 13:40:53

我知道你可能不再需要这个了,但它可能对其他人很有用,我最近一直在做这个工作,而且这方面有0种文档.你所能做的就是找到一些例子(只有很少的例子),试着理解他们想要做什么(如果注释得很好的话),我正在编写一个插件--解析gimple代码,下面是我的plugin_init

代码语言:javascript
复制
int plugin_init (plugin_name_args* plugin_info,
             plugin_gcc_version* ver)
{
  // You can get the plugin name
  const char * const plugin_name = plugin_info->base_name;
  cerr << "starting ";
  // here you can get the plugin args and number of args, 
  // look at https://gcc.gnu.org/onlinedocs/gccint/Plugins-loading.html#Plugins-loading
  // for more information how to give your plugin some args
  const int argc = plugin_info->argc;   
  const struct plugin_argument * const argv = plugin_info->argv;
  struct register_pass_info calls_printer_info;

  // Here you say where you need to put your plugin, mine is called after ssa
  // This is my pass
  calls_printer_info.pass                         = new calls_printer_pass();
  calls_printer_info.reference_pass_name          = "ssa" ;
  calls_printer_info.ref_pass_instance_number     = 1;
  calls_printer_info.pos_op                       = PASS_POS_INSERT_AFTER;
  register_callback (plugin_name,
                     PLUGIN_PASS_MANAGER_SETUP,
                     NULL,
                     &calls_printer_info);
  return 0;
}

您需要在以下之前创建您的通行证:

代码语言:javascript
复制
static const struct pass_data calls_printer_pass_data = {
                .type                   = GIMPLE_PASS,
                .name                   = "calls_printer",
                .optinfo_flags          = OPTGROUP_NONE,
                .has_gate               = false,
                .has_execute            = true,
                .tv_id                  = TV_NONE,
                .properties_required    = PROP_cfg,
                .properties_provided    = 0,
                .properties_destroyed   = 0,
                .todo_flags_start       = 0,
                .todo_flags_finish      = 0
};

class calls_printer_pass : public gimple_opt_pass {
public:
        calls_printer_pass() : gimple_opt_pass(calls_printer_pass_data, g) {}
        // put the function you want to execute when the pass is executed
        unsigned int execute() { return my_function(); }
};

别忘了这是许可证:

代码语言:javascript
复制
int plugin_is_GPL_compatible;
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25626124

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档