2017-03-03 17 views
0

この質問はgccの内部構造に焦点を当てています。私はgcc genericツリーを試しています。この小さなプロジェクトは、教育目的のためだけにハードコードされたフロントエンドをコンパイルすることです。私はprintfを外部から呼び出すことができ、テストメッセージを出力できる実行可能ファイルをコンパイルすることができました。後者は、私が関数の引数を用意することができるという証拠です。この問題の本質は、自分の関数/メソッドを呼び出して引数を取り出すことです。gcc関数ツリーノードから関数の引数を取得する

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、引数のツリーノードであることが予想できませんでした。

+1

最小限で完全な自立型ソースコードの例を提供してください。コンソール用の適切なコンパイル/リンクコマンドは高く評価されます。 – Scheff

+0

@Scheff自立的な例がこれには大きくなるだろうが、興味を持ってくれてありがとう。私はコマンドライン "gccsample main.exp testFunc.exp"を使います。 2つのファイルは、コンパイラを起動するための単なるダミーです。 – Tanyong

答えて

0

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を使用して取得することができます。

関連する問題