英文:
How to get GCC Built-in Functions definition?
问题
我想获取GCC内置函数的定义。我已经下载了gcc-releases-gcc-8.5.0的源代码。在源代码中,我找到了与GCC内置函数定义相对应的源代码。但是,GCC内置函数定义中有几层嵌套,我想直接获取GCC内置函数定义。
例如,我如何获取__builtin_offsetof和__builtin_choose_expr函数的定义?
英文:
I want to get GCC Built-in Functions definition. I have downloaded gcc-releases-gcc-8.5.0 source code. In the source code, I have found the source code corresponding with GCC Built-in Functions definition. However, several layers of nesting in the GCC Built-in Functions definition, and I want to get the GCC Built-in Functions definition directly.
For example, how can I get the function definition of __builtin_offsetof and __builtin_choose_expr?
答案1
得分: 1
开始处理处理__builtin_offsetof的编译器代码在这里 https://github.com/gcc-mirror/gcc/blob/master/gcc/c/c-parser.cc#L9824 :
case RID_OFFSETOF:
{
c_parser_consume_token (parser);
matching_parens parens;
if (!parens.require_open (parser))
... 等等 ...
}
__builtin_choose_expr的处理从这里开始 https://github.com/gcc-mirror/gcc/blob/master/gcc/c/c-parser.cc#L9920 :
case RID_CHOOSE_EXPR:
{
vec<c_expr_t, va_gc> *cexpr_list;
c_expr_t *e1_p, *e2_p, *e3_p;
... 等等 ...
}
GCC中的内建函数被处理为C保留字。GCC中的C保留字列表在这里 https://github.com/gcc-mirror/gcc/blob/master/gcc/c-family/c-common.cc#L385 。带有关键字的词法分析器看起来是在这里初始化的 https://github.com/gcc-mirror/gcc/blob/master/gcc/cp/lex.cc#L252 。
英文:
Compiler code that handles __builtin_offsetof starts here https://github.com/gcc-mirror/gcc/blob/master/gcc/c/c-parser.cc#L9824 :
case RID_OFFSETOF:
{
c_parser_consume_token (parser);
matching_parens parens;
if (!parens.require_open (parser))
... etc ...
__builtin_choose_expr starts here https://github.com/gcc-mirror/gcc/blob/master/gcc/c/c-parser.cc#L9920 :
case RID_CHOOSE_EXPR:
{
vec<c_expr_t, va_gc> *cexpr_list;
c_expr_t *e1_p, *e2_p, *e3_p;
... etc ...
The built-in functions in GCC are handled as C reserved words. The list of C reserved words in GCC is in here https://github.com/gcc-mirror/gcc/blob/master/gcc/c-family/c-common.cc#L385 . Lexer with the keywords looks like is initialized here https://github.com/gcc-mirror/gcc/blob/master/gcc/cp/lex.cc#L252 .
答案2
得分: 0
内置函数 是那些不能被定义为(always_inline
)头文件中的函数的函数。 内置函数 不是 C 预处理器的 宏,它们不会扩展为 C/C++ 代码。这就是它们被称为 内置 的原因。
编译器将代码解析为中间表示形式。这个中间表示形式编码了表达式,例如对函数 __builtin_offsetof
的调用以及特定的参数。
机器代码从这个中间表示生成,这是函数内联以及替换对库函数的调用是否使用内置函数(取决于指定的编译器选项)的地方。
内置函数没有 C/C++ 的定义 - 它们作为特定 CPU/ISA 的汇编语言在编译器后端中生成,这取决于指定的编译器选项。
英文:
Built-in functions are those, which cannot be defined as (always_inline
) functions in header files. Built-in functions aren't C-preprocessor macros expanding into C/C++ code. That's the reason they are called built-in.
A compiler parses the code into an intermediate representation. That intermediate representation encodes expressions, like a call to function __builtin_offsetof
with these particular arguments.
Machine code gets generated from this intermediate representation and this is where function inlining and replacing calls to library functions with built-ins (or not) happens (depending on compiler options specified).
There are no C/C++ definitions for built-in functions - they get emitted as assembly language for a specific CPU/ISA in the compiler back-end, depending on compiler options specified.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论