2017-03-03 46 views
0

這個問題關注於gcc內部。我正在試驗gcc通用樹。這個小型項目是爲了教育目的而編寫一個硬編碼的前端。我設法從外部調用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」。這兩個文件只是用來調用編譯器的傻瓜。 – 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,如果任何被檢索。