2012-10-07 76 views
0

只需在LLVM彙編代碼中輸入用戶輸入即可。它會在主要功能的參數中嗎?目前,我的主要功能如下:在LLVM彙編代碼中輸入用戶輸入

define i32 @main() nounwind { 
factorial.exit: 
    %0 = tail call i32 @factorial(i32 3) nounwind ; <i32> [#uses=1] 
    %1 = tail call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([4 x i8]* @.str, i32  0, i32 0), i32 %0) nounwind ; <i32> [#uses0] 
    ret i32 0 
} 

它計算的3階乘的那一刻,或什麼都編號我把3.我目前從終端執行這個點,但我會make一個makefile來最終執行它。我怎麼讓它從終端接受用戶輸入,我想我會執行它像「lli factorial.bc 5」之類的東西,在我編譯成字節碼之後,它會給我5的階乘。

感謝您的幫助!

+0

請訪問http://llvm.org/demo/。用C鍵入你的代碼。分析輸出的LLVM程序集。 –

+0

是的,我一直在嘗試。這是很難解釋,當我嘗試實現它時,我得到了一大堆錯誤。我收到了很多關於i8和i32的錯誤。 – intA

回答

0

編譯上llvm.org/demo代碼產生如下:

define i32 @main(i32 %argc, i8** nocapture %argv) nounwind uwtable { 
    %1 = getelementptr inbounds i8** %argv, i64 1 
    %2 = load i8** %1, align 8, !tbaa !0 
    %3 = tail call i64 @strtol(i8* nocapture %2, i8** null, i32 10) nounwind 
    %4 = trunc i64 %3 to i32 
    %5 = icmp eq i32 %4, 0 
    br i1 %5, label %factorial.exit, label %tailrecurse.i 

tailrecurse.i:         ; preds = %tailrecurse.i, %0 
    %X.tr2.i = phi i32 [ %6, %tailrecurse.i ], [ %4, %0 ] 
    %accumulator.tr1.i = phi i32 [ %7, %tailrecurse.i ], [ 1, %0 ] 
    %6 = add nsw i32 %X.tr2.i, -1 
    %7 = mul nsw i32 %accumulator.tr1.i, %X.tr2.i 
    %8 = icmp eq i32 %6, 0 
    br i1 %8, label %factorial.exit, label %tailrecurse.i 

factorial.exit:         ; preds = %tailrecurse.i, %0 
    %accumulator.tr.lcssa.i = phi i32 [ 1, %0 ], [ %7, %tailrecurse.i ] 
    %9 = tail call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([4 x i8]* @.str, i64 0, i64 0), i32 %accumulator.tr.lcssa.i) nounwind 
    ret i32 0 
} 

尤其是你應該看看%1(即argv+1)和%2(即*(argv+1)):當你調用lli factorial.bc 5%2將包含指針字符串5

+0

謝謝,這有助於。雖然我能夠以另一種方式做到這一點,但請使用「cin」語句並以此方式生成程序集。 – intA

0

所以我能夠使用「cin」語句而不是在主要方法中使用參數。我用C編碼並使用LLVM網站生成程序集。