从 gcc 函数树节点检索函数参数
Retrieve function arguments from gcc function tree node
这个问题的重点是 gcc 内部结构。我正在试验 gcc 泛型树。这个小项目是为了教育目的而编译一个硬编码的前端。我设法从外部调用 printf 并能够编译一个能够打印测试消息的可执行文件。后者证明我设法为函数准备参数。问题的本质是调用我自己的 function/method 并检索它的参数。
这是我准备通话的地方:
tree testFn;
tree testFndeclTypeParam[] = {
integer_type_node
};
tree testFndeclType = build_varargs_function_type_array(integer_type_node, 1, testFndeclTypeParam);
tree testFnDecl = build_fn_decl("testFunc", testFndeclType);
DECL_EXTERNAL(testFnDecl) = 1;
testFn = build1(ADDR_EXPR, build_pointer_type(testFndeclType), testFnDecl);
tree exprTest = build_int_cst_type(integer_type_node, 20);
tree testStmt = build_call_array_loc(UNKNOWN_LOCATION, integer_type_node, testFn, 1, testArgs);
append_to_statement_list(testStmt, &stackStmtList);
我可以确认 "testFunc" 函数确实被调用了。
现在另一边,这里是被调用的函数:
// Built type of main "int (int)"
tree mainFndeclTypeParam[] = {
integer_type_node, // int
};
tree mainFndeclType = build_function_type_array(integer_type_node, 1, mainFndeclTypeParam);
tree mainFndecl = build_fn_decl("testFunc", mainFndeclType);
tree stackStmtList = alloc_stmt_list();
tree parmList = build_decl(UNKNOWN_LOCATION, PARM_DECL, mainFndecl, integer_type_node);
我找不到显示如何检索参数的明确示例,但希望它位于参数树节点 parmList 中。
对于那些对 gcc 编译器设计感兴趣的人,这里是我的问题的解决方案。感谢 Google 或者维护 Java 前端的人。
应该是:
tree typelist = TYPE_ARG_TYPES(TREE_TYPE(mainFndecl));
tree typeOfList = TREE_VALUE(typelist);
tree parmList = build_decl(UNKNOWN_LOCATION, PARM_DECL, NULL_TREE, typeOfList);
tree *ptr = &DECL_ARGUMENTS(mainFndecl);
*ptr = parmList;
可以使用 TREE_CHAIN 检索下一个参数,如果有的话。
这个问题的重点是 gcc 内部结构。我正在试验 gcc 泛型树。这个小项目是为了教育目的而编译一个硬编码的前端。我设法从外部调用 printf 并能够编译一个能够打印测试消息的可执行文件。后者证明我设法为函数准备参数。问题的本质是调用我自己的 function/method 并检索它的参数。
这是我准备通话的地方:
tree testFn;
tree testFndeclTypeParam[] = {
integer_type_node
};
tree testFndeclType = build_varargs_function_type_array(integer_type_node, 1, testFndeclTypeParam);
tree testFnDecl = build_fn_decl("testFunc", testFndeclType);
DECL_EXTERNAL(testFnDecl) = 1;
testFn = build1(ADDR_EXPR, build_pointer_type(testFndeclType), testFnDecl);
tree exprTest = build_int_cst_type(integer_type_node, 20);
tree testStmt = build_call_array_loc(UNKNOWN_LOCATION, integer_type_node, testFn, 1, testArgs);
append_to_statement_list(testStmt, &stackStmtList);
我可以确认 "testFunc" 函数确实被调用了。
现在另一边,这里是被调用的函数:
// Built type of main "int (int)"
tree mainFndeclTypeParam[] = {
integer_type_node, // int
};
tree mainFndeclType = build_function_type_array(integer_type_node, 1, mainFndeclTypeParam);
tree mainFndecl = build_fn_decl("testFunc", mainFndeclType);
tree stackStmtList = alloc_stmt_list();
tree parmList = build_decl(UNKNOWN_LOCATION, PARM_DECL, mainFndecl, integer_type_node);
我找不到显示如何检索参数的明确示例,但希望它位于参数树节点 parmList 中。
对于那些对 gcc 编译器设计感兴趣的人,这里是我的问题的解决方案。感谢 Google 或者维护 Java 前端的人。
应该是:
tree typelist = TYPE_ARG_TYPES(TREE_TYPE(mainFndecl));
tree typeOfList = TREE_VALUE(typelist);
tree parmList = build_decl(UNKNOWN_LOCATION, PARM_DECL, NULL_TREE, typeOfList);
tree *ptr = &DECL_ARGUMENTS(mainFndecl);
*ptr = parmList;
可以使用 TREE_CHAIN 检索下一个参数,如果有的话。