如何在GCC插件中添加内置函数? [英] How to add a builtin function in a GCC plugin?

查看:175
本文介绍了如何在GCC插件中添加内置函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

GCC插件可以添加新的内置功能吗?如果是这样,如何正确执行呢?


GCC版本是5.3(或更高版本)。该代码由用C编写的插件编译和处理。


gcc-melt.org上的GCC插件的定价是可行的,但我看不到如何做。


据我所知GCC,内置文件是使用add_builtin_function()创建的/gcc/langhooks.c#L605 rel = noreferrer> gcc / langhooks.c :

 
add_builtin_function(const char * name,
树类型,
int function_code,
enum Built_in_class cl,
const char *库名,
树属性)

除了 function_code ,该函数的唯一数字ID。


类似(请参见 add_builtin_function_common()),该值来自 e预计会出现num Built_in_function ,但是GCC插件无法更改该枚举。


一个人不能传递任何大于 END_BUILTINS 似乎是 function_code 。在这种情况下, builtin_decl_implicit() builtin_decl_explicit()的声明将失败。


那么,在GCC插件中添加内置函数的正确方法是什么(不使用MELT等,仅使用GCC插件API)?


更新
我再次查看了 add_builtin_function_common() langhooks.builtin_function()对于C的实现。以及在GCC中如何使用它们。在某些情况下, function_code 似乎可以接受0。您不能使用 builtin_decl_implicit()然后可以保存 add_builtin_function()返回的DECL并在以后使用。 / p>

看起来像唯一可以尝试创建内置文件的事件是PLUGIN_START_UNIT(否则,由于 external_scope 变量,GCC可能会崩溃


我在该阶段尝试了以下操作( fntype 之前已创建):

  decl = add_builtin_function(
my_helper,fntype,
0 / * function_code * /,
BUILT_IN_NORMAL / *枚举built_in_class cl * / ,
NULL / * library_name * /,
NULL_TREE / * attrs * /)

my_helper 是在另一个C源文件中定义的,该C源文件已编译并与主源文件链接。然后在我的GIMPLE传递过程中,使用decl将对该函数的调用插入到其他函数( gimple_build_call )中。


GCC输出没有错误并确实插入了对 my_helper 的调用,但作为对普通函数的调用。实际上,我需要一个内置函数来避免调用,而是插入函数的主体。


另一方面, tsan0 传递,它会在我通过之后立即执行,插入内置函数的调用,就像人们期望的那样:没有显式调用,因此只插入了函数的主体。但是,它的内置函数是由GCC本身而不是由插件定义的。


因此,我想我的内置函数仍然需要一些东西才能成为有效的内置函数,但我不知道它是什么。

解决方案

我假设您要执行的操作(根据您的评论和链接文章)是插入C代码转换为函数。在那种情况下,我以为您不需要编写一个编译器插件就可以了。看看 Boost.Preprocessor ,仅使用预处理程序就可以对C代码进行非常高级的操作。


It is possible for a GCC plugin to add a new builtin function? If so, how to do it properly?

GCC version is 5.3 (or newer). The code is compiled and processed by the plugin written in C.

It is mentioned in the rationale for GCC plugins at gcc-melt.org that this is doable but I cannot see how.

As far as I can see in the sources of GCC, the builtins are created using add_builtin_function() from gcc/langhooks.c:

tree
add_builtin_function (const char *name,
      tree type,
      int function_code,
      enum built_in_class cl,
      const char *library_name,
      tree attrs)

It is more or less clear which values the arguments of this function should have, except for function_code, a unique numeric ID of the function.

Looks like (see add_builtin_function_common()), a value from enum built_in_function is expected there but a GCC plugin cannot change that enum.

One cannot pass any random value greater than END_BUILTINS as function_code either, it seems. builtin_decl_implicit() and builtin_decl_explicit() would have a failed assertion in that case.

So, what is the proper way to add a builtin in a GCC plugin (without using MELT and such, just GCC plugin API)?

Update I looked again at the implementation of add_builtin_function_common() and of langhooks.builtin_function() for C as well as at how these are used in GCC. It seems that 0 is acceptable as function_code in some cases. You cannot use builtin_decl_implicit() then but you can save the DECL returned by add_builtin_function() and use it later.

Looks like the only event when I can try to create built-ins that way is PLUGIN_START_UNIT (otherwise GCC may crash due to external_scope variable being NULL).

I tried the following at that stage (fntype was created before):

decl = add_builtin_function (
    "my_helper", fntype,
    0 /* function_code */,
    BUILT_IN_NORMAL /* enum built_in_class cl */,
    NULL /* library_name */,
    NULL_TREE /* attrs */)

my_helper was defined in a different C source file compiled and linked with the main source file. Then I used decl to insert the calls to that function into other functions (gimple_build_call) during my GIMPLE pass.

GCC output no errors and indeed inserted the call to my_helper but as a call to an ordinary function. I actually needed a builtin to avoid a call but rather insert the body of the function.

On the other hand, tsan0 pass, which executes right after my pass, inserts the calls of builtin functions just like one would expect: there is no explicit call as a result, just the body of the function is inserted. Its builtins, however, are defined by GCC itself rather than by the plugins.

So I suppose my builtin still needs something to be a valid builtin, but I do not know what it is. What could that be?

解决方案

I'm assuming what you want to do (from your comment and linked post) is insert C code into a function. In that case, I would have thought you wouldn't need to go so far as to write a compiler plugin. Have a look at Boost.Preprocessor, which can do very advanced manipulations of C code using only the preprocessor.

这篇关于如何在GCC插件中添加内置函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆