【发布时间】: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