2011-09-27 135 views
5

我我想要創建內部JIT LLVM代碼異常處理程序。關於exception handling in LLVM當前文檔是目前非常handwavy,所以我一直在試圖再使用大多數的片段,我從http://llvm.org/demo得到爲了得到工作的例子,但我不知道如果這些都是最新的與LLVM 2.9(我使用的版本)。 這是模塊在Module :: dump()之後的樣子。llvm異常; catch處理不處理,清理不叫

; ModuleID = 'testModule' 

declare i32 @myfunc() 

define i32 @test_function_that_invokes_another() { 
entryBlock: 
    %0 = alloca i8* 
    %1 = alloca i32 
    %someName = invoke i32 @myfunc() 
      to label %exitBlock unwind label %unwindBlock 

exitBlock:          ; preds = %entryBlock 
    ret i32 1 

unwindBlock:          ; preds = %entryBlock 
    %2 = call i8* @llvm.eh.exception() 
    store i8* %2, i8** %0 
    %3 = call i32 (i8*, i8*, ...)* @llvm.eh.selector(i8* %2, i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*), i8* null) 
    store i32 1, i32* %1 
    %4 = load i8** %0 
    %5 = call i32 (...)* @__cxa_begin_catch(i8* %4) nounwind 
    %cleanup_call = call i32 @myCleanup() 
    %6 = call i32 (...)* @__cxa_end_catch() 
    ret i32 1 
} 

declare i32 @__gxx_personality_v0(...) 

declare i32 @__cxa_begin_catch(...) 

declare i32 @__cxa_end_catch(...) 

declare i8* @llvm.eh.exception() nounwind readonly 

declare i32 @llvm.eh.selector(i8*, i8*, ...) nounwind 

declare i32 @myCleanup() 

,這是發生了什麼當我嘗試執行函數:

inside JIT calling C/C++ call 
terminate called after throwing an instance of 'int' 
Aborted 

這表明拋出該函數被調用,它拋出,但我在清理調用從未降落。 (我清理調用應該說「JIT調用C/C++清理內部」)

調用和功能(嘗試)來捕捉拋出的異常是:

const inline llvm::FunctionType* getTestFunctionSignature(llvm::LLVMContext& context) { 
      return llvm::TypeBuilder< unsigned int(), false > ::get(context); 
     } 

llvm::Function* createFunctionThatInvokesAnother(llvm::LLVMContext& ctx, llvm::Module* mod , llvm::Function* another) { 
    llvm::Function* result = llvm::Function::Create(getTestFunctionSignature(ctx), 
          llvm::GlobalValue::ExternalLinkage, 
          "test_function_that_invokes_another", 
          mod); 
    llvm::BasicBlock* entry_block = llvm::BasicBlock::Create(ctx, "entryBlock", result); 
    llvm::BasicBlock* exit_block = llvm::BasicBlock::Create(ctx, "exitBlock", result); 
    llvm::BasicBlock* unwind_block = llvm::BasicBlock::Create(ctx, "unwindBlock", result); 
    llvm::IRBuilder<> builder(entry_block); 
    llvm::ConstantInt* ci = llvm::ConstantInt::get(mod->getContext() , llvm::APInt(32 , llvm::StringRef("1"), 10)); 
    llvm::PointerType* pty3 = llvm::PointerType::get(llvm::IntegerType::get(mod->getContext(), 8), 0); 
    llvm::AllocaInst* ptr_24 = new llvm::AllocaInst(pty3, "", entry_block); 
    llvm::AllocaInst* ptr_25 = new llvm::AllocaInst(llvm::IntegerType::get(mod->getContext(), 32), "", entry_block); 
    llvm::Twine name("someName"); 
    builder.CreateInvoke(another , exit_block , unwind_block , "someName"); 

    builder.SetInsertPoint(exit_block); 
    builder.CreateRet(ci); 

    builder.SetInsertPoint(unwind_block); 
    llvm::Function* func___gxx_personality_v0 = func__gxx_personality_v0(mod); 
    llvm::Function* func___cxa_begin_catch = func__cxa_begin_catch(mod); 
    llvm::Function* func___cxa_end_catch = func__cxa_end_catch(mod); 
    llvm::Function* func_eh_ex = func_llvm_eh_exception(mod); 
    llvm::Function* func_eh_sel = func__llvm_eh_selector(mod); 
    llvm::Constant* const_ptr_17 = llvm::ConstantExpr::getCast(llvm::Instruction::BitCast, func___gxx_personality_v0, pty3); 
    llvm::ConstantPointerNull* const_ptr_18 = llvm::ConstantPointerNull::get(pty3); 

    llvm::CallInst* get_ex = llvm::CallInst::Create(func_eh_ex, "", unwind_block); 
    get_ex->setCallingConv(llvm::CallingConv::C); 
    get_ex->setTailCall(false); 
    new llvm::StoreInst(get_ex, ptr_24, false, unwind_block); 

    std::vector<llvm::Value*> int32_37_params; 
    int32_37_params.push_back(get_ex); 
    int32_37_params.push_back(const_ptr_17); 
    int32_37_params.push_back(const_ptr_18); 
    llvm::CallInst* eh_sel = llvm::CallInst::Create(func_eh_sel, int32_37_params.begin(), int32_37_params.end(), "", unwind_block); 
    eh_sel->setCallingConv(llvm::CallingConv::C); 
    eh_sel->setTailCall(false); 
    new llvm::StoreInst(ci, ptr_25, false, unwind_block); 

    llvm::LoadInst* ptr_29 = new llvm::LoadInst(ptr_24, "", false, unwind_block); 
    llvm::CallInst* ptr_30 = llvm::CallInst::Create(func___cxa_begin_catch, ptr_29, "", unwind_block); 
    ptr_30->setCallingConv(llvm::CallingConv::C); 
    ptr_30->setTailCall(false); 
    llvm::AttrListPtr ptr_30_PAL; 
    { 
     llvm::SmallVector<llvm::AttributeWithIndex, 4 > Attrs; 
     llvm::AttributeWithIndex PAWI; 
     PAWI.Index = 4294967295U; 
     PAWI.Attrs = 0 | llvm::Attribute::NoUnwind; 
     Attrs.push_back(PAWI); 
     ptr_30_PAL = llvm::AttrListPtr::get(Attrs.begin(), Attrs.end()); 

    } 
    ptr_30->setAttributes(ptr_30_PAL); 
    llvm::Function* cleanup = call_myCleanup(mod); 
    builder.CreateCall(cleanup , "cleanup_call"); 
    llvm::CallInst* end_catch = llvm::CallInst::Create(func___cxa_end_catch, "", unwind_block); 
    builder.CreateRet(ci); 
    //createCatchHandler(mod , unwind_block); 
    return result; 
} 

這被稱爲像往常一樣生意:

testMain() { 
llvm::LLVMContext ctx; 
    llvm::InitializeNativeTarget(); 
    llvm::StringRef idRef("testModule"); 
    llvm::Module* module = new llvm::Module(idRef, ctx); 
    std::string jitErrorString; 
    llvm::ExecutionEngine* execEngine = executionEngine(module , jitErrorString); 
    llvm::FunctionPassManager* OurFPM = new llvm::FunctionPassManager(module); 

llvm::Function *thr = call_my_func_that_throws(module); 
    llvm::Function* result = createFunctionThatInvokesAnother(ctx, module ,thr); 


    std::string errorInfo; 
    llvm::verifyModule(* module, llvm::PrintMessageAction, & errorInfo); 
    module->dump(); 


    void *fptr = execEngine->getPointerToFunction(result); 
    unsigned int (*fp)() = (unsigned int (*)())fptr; 
    try { 
    unsigned int value = fp(); 
    } catch (...) { 
     std::cout << " handled a throw from JIT function" << std::endl; 
    } 
} 

在我的函數拋出是:

int myfunc() { 
    std::cout << " inside JIT calling C/C++ call" << std::endl; 
    throw 0; 
}; 

llvm::Function* call_my_func_that_throws (llvm::Module* mod) { 
    std::vector< const llvm::Type* > FuncTy_ex_args; 
    llvm::FunctionType* FuncTy_ex = llvm::FunctionType::get(llvm::IntegerType::get(mod->getContext() , 32) , FuncTy_ex_args , false); 
    llvm::Function* result = llvm::Function::Create(FuncTy_ex, llvm::GlobalValue::ExternalLinkage, "myfunc", mod); 
    result->setCallingConv(llvm::CallingConv::C); 
    llvm::AttrListPtr PAL; 
    result->setAttributes(PAL); 
    llvm::sys::DynamicLibrary::AddSymbol("myfunc" , (void*) &myfunc); 
    return result; 
} 

和我清理功能以類似的方式定義:

int myCleanup() { 
    std::cout << " inside JIT calling C/C++ Cleanup" << std::endl; 
    return 18; 
}; 

llvm::Function* call_myCleanup (llvm::Module* mod) { 
    std::vector< const llvm::Type* > FuncTy_ex_args; 
    llvm::FunctionType* FuncTy_ex = llvm::FunctionType::get(llvm::IntegerType::get(mod->getContext() , 32) , FuncTy_ex_args , false); 
    llvm::Function* result = llvm::Function::Create(FuncTy_ex, llvm::GlobalValue::ExternalLinkage, "myCleanup", mod); 
    result->setCallingConv(llvm::CallingConv::C); 
    llvm::AttrListPtr PAL; 
    result->setAttributes(PAL); 
    llvm::sys::DynamicLibrary::AddSymbol("myCleanup" , (void*) &myCleanup); 
    return result; 
} 

我也看到了this document關於LLVM最近的異常處理的變化,但目前尚不清楚這些變化是如何轉化爲實際的,你知道,代碼

回答

2

現在EH代碼正在進行大量修改。如果我記得正確的話,演示版本不是2.9版本,但是當前的開發源代碼 - 意味着如果嘗試這種方式,試圖用2.9來做某件事將會是一個受傷的世界。

這就是說,EH表示,現在好多了,很多補丁都進去了就在本週,以提高文檔。如果您試圖編寫通過llvm使用異常的語言,我強烈建議您將代碼遷移到當前的開發源代碼中。

所有這一切說,我不知道異常是如何處理好中JIT在現在好了工作。它在名義上得到了支持,但您可能需要調試放入內存的展開表以確保它們是正確的。

+0

優秀的,可以使用新的方法,你指出一些例子中,我一直在閱讀這篇文章:http://lists.cs.uiuc.edu/pipermail/llvmdev/2011-July/041768.html但我米不知道我該怎麼辦 – lurscher

+0

呃文件已被重寫,所以應該是準確的。另一種選擇是查看clang輸出以瞭解它應該如何顯示。 – echristo