【问题标题】:Retrieve function arguments from gcc function tree node从 gcc 函数树节点检索函数参数
【发布时间】:2017-07-23 10:38:32
【问题描述】:

这个问题的重点是 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 中。

【问题讨论】:

  • 能否提供一个最小的、完整的、独立的源代码示例?控制台的适当编译/链接命令将不胜感激。
  • @Scheff 一个独立的例子对于这个来说会很大,但感谢您支付利息。我使用命令行“gccsample main.exp testFunc.exp”。这两个文件只是用来调用编译器的虚拟文件。

标签: c++ c gcc compiler-construction


【解决方案1】:

对于那些对 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 检索下一个参数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-12-12
    • 2012-01-17
    • 2013-06-27
    • 1970-01-01
    • 2017-04-01
    • 2011-08-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多