Next: , Previous: Mode Switching, Up: Target Macros


17.25 定义目标机特定的__attribute__用法

可以为函数,数据和类型定义target特定的属性。 这些使用下列target钩子来描述;它们还需要在extend.texi中被记述。

— Target Hook: const struct attribute_spec * TARGET_ATTRIBUTE_TABLE

如果定义, 该目标钩子指向一个‘struct attribute_spec’ (在tree.h中定义) 数组, 用来指定该目标的机器特定的属性, 以及这些属性被应用到的实体和它们接受的参数的一些限制。

— Target Hook: bool TARGET_ATTRIBUTE_TAKES_IDENTIFIER_P (const_tree name)
— Target Hook: int TARGET_COMP_TYPE_ATTRIBUTES (tree type1, tree type2)

如果定义,该目标钩子为一个函数, 如果type1type2的属性不匹配则返回0,匹配则返回1, 几乎匹配则返回2(这将产生一个warning)。如果没有被定义, 则机器特定的属性总被假定为匹配的。

— Target Hook: void TARGET_SET_DEFAULT_TYPE_ATTRIBUTES (tree type)

如果定义,该目标钩子为一个函数,其将缺省属性赋予新定义的类型type

— Target Hook: tree TARGET_MERGE_TYPE_ATTRIBUTES (tree type1, tree type2)

定义该target钩子,如果合并类型属性需要进行特殊的处理。 如果定义,则结果为type1type2的组合TYPE_ATTRIBUTES列表。 其假设comptypes总是被调用并返回1。 该函数可以调用merge_attributes来处理机器无关的合并。

— Target Hook: tree TARGET_MERGE_DECL_ATTRIBUTES (tree olddecl, tree newdecl)

定义该target钩子,如果合并decl属性需要进行特殊的处理。如果定义, 则结果为olddeclnewdecl的组合DECL_ATTRIBUTES列表。 newdeclolddecl的拷贝。这样的例子是当一个属性覆盖另一个, 或者当一个属性被后续的属性置空的情况。 该函数可以调用merge_attributes来处理机器无关的合并。

如果唯一需要target特定的处理是Microsoft Windows target的‘dllimport’, 则你应该定义宏TARGET_DLLIMPORT_DECL_ATTRIBUTES1。 然后编译器将会定义一个叫做merge_dllimport_decl_attributes的函数, 其可以被定义为TARGET_MERGE_DECL_ATTRIBUTES的扩展。 你还可以为你的port在属性表中增加handle_dll_attribute, 来执行‘dllimport’和‘dllexport’属性的初始化处理。 例如,在i386/cygwin.hi386/i386.c中。

— Target Hook: bool TARGET_VALID_DLLIMPORT_ATTRIBUTE_P (tree decl)

decl为一个指定为__attribute__((dllimport))的变量或者函数。 使用该钩子,如果target需要给handle_dll_attribute增加额外的有效性检查。

— Macro: TARGET_DECLSPEC

定义该宏为非零, 如果你想将__declspec(X)__attribute((X))等同对待。 缺省下,只有在定义了TARGET_DLLIMPORT_DECL_ATTRIBUTES的target上才可以。 目前对于__declspec的实现是通过一个内建的宏,但是你不应该依赖于实现细节。

— Target Hook: void TARGET_INSERT_ATTRIBUTES (tree node, tree *attr_ptr)

定义该target钩子,如果你想在decl被创建时,能够为其增加属性。 这在后端想要实现一个pragma,并且用到与pragma相关的属性的时候, 通常很有用。参数node是正在创建的decl。 参数attr_ptr是指向该decl的属性列表的指针。不要修改列表本身, 因为其可能与其它decl共享,但是可以将属性链接到列表的头部, 并且修改*attr_ptr以指向新的属性,或者如果需要进一步的修改, 创建一个列表的拷贝。

— Target Hook: bool TARGET_FUNCTION_ATTRIBUTE_INLINABLE_P (tree fndecl)

该target钩子返回true,如果可以将fndecl内联到当前函数中, 而不管它具有的target特定属性,否则为false。 缺省下,如果函数具有一个target特定属性,则不会被内联。

— Target Hook: bool TARGET_OPTION_VALID_ATTRIBUTE_P (tree fndecl, tree name, tree args, int flags)

This hook is called to parse the attribute(option("...")), and it allows the function to set different target machine compile time options for the current function that might be different than the options specified on the command line. The hook should return true if the options are valid.

The hook should set the DECL_FUNCTION_SPECIFIC_TARGET field in the function declaration to hold a pointer to a target specific struct cl_target_option structure.

— Target Hook: void TARGET_OPTION_SAVE (struct cl_target_option *ptr)

This hook is called to save any additional target specific information in the struct cl_target_option structure for function specific options. 参见Option file format.

— Target Hook: void TARGET_OPTION_RESTORE (struct cl_target_option *ptr)

This hook is called to restore any additional target specific information in the struct cl_target_option structure for function specific options.

— Target Hook: void TARGET_OPTION_PRINT (FILE *file, int indent, struct cl_target_option *ptr)

This hook is called to print any additional target specific information in the struct cl_target_option structure for function specific options.

— Target Hook: bool TARGET_OPTION_PRAGMA_PARSE (tree args, tree pop_target)

This target hook parses the options for #pragma GCC option to set the machine specific options for functions that occur later in the input stream. The options should be the same as handled by the TARGET_OPTION_VALID_ATTRIBUTE_P hook.

— Target Hook: void TARGET_OPTION_OVERRIDE (void)

Sometimes certain combinations of command options do not make sense on a particular target machine. You can override the hook TARGET_OPTION_OVERRIDE to take account of this. This hooks is called once just after all the command options have been parsed.

Don't use this hook to turn on various extra optimizations for -O. That is what OPTIMIZATION_OPTIONS is for.

If you need to do something whenever the optimization level is changed via the optimize attribute or pragma, see TARGET_OVERRIDE_OPTIONS_AFTER_CHANGE

— Target Hook: bool TARGET_CAN_INLINE_P (tree caller, tree callee)

This target hook returns false if the caller function cannot inline callee, based on target specific information. By default, inlining is not allowed if the callee function has function specific target options and the caller does not use the same options.