从GCC功能树节点中检索函数参数

Retrieve function arguments from gcc function tree node

本文关键字:检索 函数 参数 树节点 GCC 功能      更新时间:2023-10-16

这个问题的重点是GCC内部。我正在尝试使用GCC通用树。这个小型项目是为了教育目的编译硬编码的前端。我设法向外部调用printf,并能够编译能够打印测试消息的可执行文件。后者证明我设法为函数准备参数。问题的本质是调用我自己的功能/方法并检索其参数。

这是我准备电话的地方:

  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(如果有)来检索下一个参数。