免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3571 | 回复: 7
打印 上一主题 下一主题

gcc奇怪的宏定义 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-11-21 23:48 |只看该作者 |倒序浏览
在看gcc源代码的时候发现一种很奇怪的宏定义方式
#define DEFTREECODE(SYM, STRING, TYPE, NARGS)   SYM,
  enum tree_code {
       #include "tree.def"
  };
tree.def文件里面的内容如下:
DEFTREECODE (ERROR_MARK, "error_mark", "x", 0)
DEFTREECODE (IDENTIFIER_NODE, "identifier_node", "x", -1)
DEFTREECODE (OP_IDENTIFIER, "op_identifier", "x", 2)
DEFTREECODE (TREE_LIST, "tree_list", "x", 2)
....
等很多类似的东西,
不知道这样做有什么作用,这样定义出来的tree_code代表什么意思呢?

论坛徽章:
0
2 [报告]
发表于 2008-11-22 00:39 |只看该作者
没看明白

论坛徽章:
0
3 [报告]
发表于 2008-11-22 07:23 |只看该作者
应该是用来定义和树的节点相关的信息的

论坛徽章:
0
4 [报告]
发表于 2008-11-22 08:54 |只看该作者
gcc-implementation-details(见附件) 4.1节中有如下说明:

The tree.def file contains various node names defined using a C preprocessor macro
DEFTREECODE. The macro is used in different ways depending on the information required.
We illustrate the use with an example. Consider the following macro that is represents the
C void type:
DEFTREECODE (VOID_TYPE, "void_type", ’t’, 0)
Defining the DEFTREECODE macro as:
#define DEFTREECODE(arg1, arg2, arg3, arg4) arg2
yields the second argument which gives the name as a string. The definitions of the
DEFTREECODE are changed as needed in the GCC sources. For instance, the various nodes
are enumerated simply as:
#define DEFTREECODE(arg1, arg2, arg3, arg4) arg1
enum tree_node_list {
#include "tree.def"
};
#undef DEFTREECODE
The technique is used at many places in the source, for example for the RTL definitions
too.
The first argument of the DEFTREECODE macro is the symbolic name of the node typically
used to create an enumerated data type of nodes. The second argument is the identifier used
to refer to that node. The nodes in the GCC AST are of different kinds as listed in [kinds of
AST nodes in GCC], page 16. These kinds are encoded via a set of character codes which
are listed in tree.def. These codes are the third argument of the DEFTREECODE macro.
The fourth argument in most node definitions is the number of operands of that node. For
other nodes, the use of the fourth argument is dependent on the node being described. The
collection of the DEFTREECODE macros define the database of nodes that GCC uses for it’s
AST.

gcc-implementation-details.pdf (366.26 KB, 下载次数: 77)

[ 本帖最后由 heixia108 于 2008-11-22 08:55 编辑 ]

论坛徽章:
0
5 [报告]
发表于 2008-11-22 10:11 |只看该作者
恩,差不多明白了。不知这个pdf在哪里下的?有没有gcc2.0的?

论坛徽章:
0
6 [报告]
发表于 2008-11-22 18:24 |只看该作者

论坛徽章:
0
7 [报告]
发表于 2008-11-22 23:50 |只看该作者
楼上,你QQ号或msn是?看了你的博客,想一起交流。

论坛徽章:
0
8 [报告]
发表于 2008-11-23 22:57 |只看该作者

回复 #7 kenby 的帖子

给你发消息了,呵,希望有机会和你交流
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP