0

This question is focusing on gcc internals. I am experimenting with gcc generic trees. This small project is about compiling a hard coded front end just for education purpose. I managed to call printf externally and was able to compile an executable which is able to print a test message. The latter is proof that I manage to prepare arguments for a function. The essence of the problem is to call my own function/method and retrieve its arguments.

This is where I prepare the call:

  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);

I can confirm that the function "testFunc" is definitely called.

Now the other side, here is the function being called:

  // 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);

I could not find an explicit example showing how to retrieve the arguments, but expected it to be in parmList, the argument tree node.

2
  • 1
    Could you, please, provide a minimal, complete, self-standing source code example? The appropriate compile/link command for console would be appreciated. Commented Mar 3, 2017 at 12:16
  • @Scheff a self-standing example will be to large for this, but thank you for paying interest. I use command line "gccsample main.exp testFunc.exp". The two files are just dummies to invoke the compiler. Commented Mar 6, 2017 at 8:43

1 Answer 1

1

Here is the solution to my problem for those interested in gcc compiler design. Thanks Google or who ever maintains the Java front end.

It should be:

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;

The next argument can be retrieved by using TREE_CHAIN, if any.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.