- llvm::IRBuilder<>& builder,
- llvm::FunctionPassManager& fpm,
- llvm::Function& toInvoke,
- std::string ourId,
- unsigned numExceptionsToCatch,
- unsigned exceptionTypesToCatch[]) {
-
- llvm::LLVMContext& context = module.getContext();
- llvm::Function *toPrint32Int = module.getFunction("print32Int");
-
- ArgTypes argTypes;
- argTypes.push_back(builder.getInt32Ty());
-
- ArgNames argNames;
- argNames.push_back("exceptTypeToThrow");
-
- llvm::Function* ret = createFunction(module,
- builder.getVoidTy(),
- argTypes,
- argNames,
- ourId,
- llvm::Function::ExternalLinkage,
- false,
- false);
-
- // Block which calls invoke
- llvm::BasicBlock *entryBlock = llvm::BasicBlock::Create(context,
- "entry",
- ret);
- // Normal block for invoke
- llvm::BasicBlock *normalBlock = llvm::BasicBlock::Create(context,
- "normal",
- ret);
- // Unwind block for invoke
- llvm::BasicBlock *exceptionBlock =
- llvm::BasicBlock::Create(context, "exception", ret);
-
- // Block which routes exception to correct catch handler block
- llvm::BasicBlock *exceptionRouteBlock =
- llvm::BasicBlock::Create(context, "exceptionRoute", ret);
-
- // Foreign exception handler
- llvm::BasicBlock *externalExceptionBlock =
- llvm::BasicBlock::Create(context, "externalException", ret);
-
- // Block which calls _Unwind_Resume
- llvm::BasicBlock *unwindResumeBlock =
- llvm::BasicBlock::Create(context, "unwindResume", ret);
-
- // Clean up block which delete exception if needed
- llvm::BasicBlock *endBlock =
- llvm::BasicBlock::Create(context, "end", ret);
-
- std::string nextName;
- std::vector<llvm::BasicBlock*> catchBlocks(numExceptionsToCatch);
- llvm::Value* exceptionCaughtFlag = NULL;
- llvm::Value* exceptionStorage = NULL;
-
- // Finally block which will branch to unwindResumeBlock if
- // exception is not caught. Initializes/allocates stack locations.
- llvm::BasicBlock* finallyBlock = createFinallyBlock(context,
- module,
- builder,
- *ret,
- nextName = "finally",
- ourId,
- *endBlock,
- *unwindResumeBlock,
- &exceptionCaughtFlag,
- &exceptionStorage);
-
- for (unsigned i = 0; i < numExceptionsToCatch; ++i) {
- nextName = ourTypeInfoNames[exceptionTypesToCatch[i]];
-
- // One catch block per type info to be caught
- catchBlocks[i] = createCatchBlock(context,
- module,
- builder,
- *ret,
- nextName,
- ourId,
- *finallyBlock,
- *exceptionCaughtFlag);
- }
-
- // Entry Block
-
- builder.SetInsertPoint(entryBlock);
-
- std::vector<llvm::Value*> args;
- args.push_back(namedValues["exceptTypeToThrow"]);
- builder.CreateInvoke(&toInvoke,
- normalBlock,
- exceptionBlock,
- args.begin(),
- args.end());
-
- // End Block
-
- builder.SetInsertPoint(endBlock);
-
- generateStringPrint(context,
- module,
- builder,
- "Gen: In end block: exiting in " + ourId + ".\n",
- USE_GLOBAL_STR_CONSTS);
- llvm::Function *deleteOurException =
- module.getFunction("deleteOurException");
-
- // Note: function handles NULL exceptions
- builder.CreateCall(deleteOurException,
- builder.CreateLoad(exceptionStorage));
- builder.CreateRetVoid();
-
- // Normal Block
-
- builder.SetInsertPoint(normalBlock);
-
- generateStringPrint(context,
- module,
- builder,
- "Gen: No exception in " + ourId + "!\n",
- USE_GLOBAL_STR_CONSTS);
-
- // Finally block is always called
- builder.CreateBr(finallyBlock);
-
- // Unwind Resume Block
-
- builder.SetInsertPoint(unwindResumeBlock);
-
- llvm::Function *resumeOurException =
- module.getFunction("_Unwind_Resume");
- builder.CreateCall(resumeOurException,
- builder.CreateLoad(exceptionStorage));
- builder.CreateUnreachable();
-
- // Exception Block
-
- builder.SetInsertPoint(exceptionBlock);
-
- llvm::Function *ehException = module.getFunction("llvm.eh.exception");
-
- // Retrieve thrown exception
- llvm::Value* unwindException = builder.CreateCall(ehException);
-
- // Store exception and flag
- builder.CreateStore(unwindException, exceptionStorage);
- builder.CreateStore(ourExceptionThrownState, exceptionCaughtFlag);
- llvm::Function *personality = module.getFunction("ourPersonality");
- llvm::Value* functPtr =
- builder.CreatePointerCast(personality,
- builder.getInt8Ty()->getPointerTo());
-
- args.clear();
- args.push_back(unwindException);
- args.push_back(functPtr);
-
- // Note: Skipping index 0
- for (unsigned i = 0; i < numExceptionsToCatch; ++i) {
- // Set up type infos to be caught
- args.push_back(
- module.getGlobalVariable(
- ourTypeInfoNames[exceptionTypesToCatch[i]]));
- }
-
- args.push_back(llvm::ConstantInt::get(builder.getInt32Ty(), 0));
-
- llvm::Function *ehSelector = module.getFunction("llvm.eh.selector");
-
- // Set up this exeption block as the landing pad which will handle
- // given type infos. See case Intrinsic::eh_selector in
- // SelectionDAGBuilder::visitIntrinsicCall(...) and AddCatchInfo(...)
- // implemented in FunctionLoweringInfo.cpp to see how the implementation
- // handles this call. This landing pad (this exception block), will be
- // called either because it nees to cleanup (call finally) or a type
- // info was found which matched the thrown exception.
- llvm::Value* retTypeInfoIndex = builder.CreateCall(ehSelector,
- args.begin(),
- args.end());
-
- // Retrieve exception_class member from thrown exception
- // (_Unwind_Exception instance). This member tells us whether or not
- // the exception is foreign.
- llvm::Value* unwindExceptionClass =
- builder.CreateLoad(
- builder.CreateStructGEP(
- builder.CreatePointerCast(
- unwindException,
- ourUnwindExceptionType->getPointerTo()),
- 0));
-
- // Branch to the externalExceptionBlock if the exception is foreign or
- // to a catch router if not. Either way the finally block will be run.
- builder.CreateCondBr(
- builder.CreateICmpEQ(unwindExceptionClass,
- llvm::ConstantInt::get(builder.getInt64Ty(),
- ourBaseExceptionClass)),
- exceptionRouteBlock,
- externalExceptionBlock);
-
- // External Exception Block
-
- builder.SetInsertPoint(externalExceptionBlock);
-
- generateStringPrint(context,
- module,
- builder,
- "Gen: Foreign exception received.\n",
- USE_GLOBAL_STR_CONSTS);
-
- // Branch to the finally block
- builder.CreateBr(finallyBlock);
-
- // Exception Route Block
-
- builder.SetInsertPoint(exceptionRouteBlock);
-
- // Casts exception pointer (_Unwind_Exception instance) to parent
- // (OurException instance).
- //
- // Note: ourBaseFromUnwindOffset is usually negative
- llvm::Value* typeInfoThrown =
- builder.CreatePointerCast(
- builder.CreateConstGEP1_64(unwindException,
- ourBaseFromUnwindOffset),
- ourExceptionType->getPointerTo());
-
- // Retrieve thrown exception type info type
- //
- // Note: Index is not relative to pointer but instead to structure
- // unlike a true getelementptr (GEP) instruction
- typeInfoThrown = builder.CreateStructGEP(typeInfoThrown, 0);
-
- llvm::Value* typeInfoThrownType =
- builder.CreateStructGEP(typeInfoThrown, 0);
-
- generateIntegerPrint(context,
- module,
- builder,
- *toPrint32Int,
- *(builder.CreateLoad(typeInfoThrownType)),
- "Gen: Exception type <%d> received (stack unwound) "
- " in " +
- ourId +
- ".\n",
- USE_GLOBAL_STR_CONSTS);
-
- // Route to matched type info catch block or run cleanup finally block
- llvm::SwitchInst* switchToCatchBlock =
- builder.CreateSwitch(retTypeInfoIndex,
- finallyBlock,
- numExceptionsToCatch);
-
- unsigned nextTypeToCatch;
-
- for (unsigned i = 1; i <= numExceptionsToCatch; ++i) {
- nextTypeToCatch = i - 1;
- switchToCatchBlock->addCase(llvm::ConstantInt::get(
- llvm::Type::getInt32Ty(context),
- i),
- catchBlocks[nextTypeToCatch]);
- }
-
- llvm::verifyFunction(*ret);
- fpm.run(*ret);
-
- return(ret);
-}
-
-
-/// Generates function which throws either an exception matched to a runtime
-/// determined type info type (argument to generated function), or if this
-/// runtime value matches nativeThrowType, throws a foreign exception by
-/// calling nativeThrowFunct.
-/// @param module code for module instance
-/// @param builder builder instance
-/// @param fpm a function pass manager holding optional IR to IR
+ llvm::IRBuilder<>& builder,
+ llvm::FunctionPassManager& fpm,
+ llvm::Function& toInvoke,
+ std::string ourId,
+ unsigned numExceptionsToCatch,
+ unsigned exceptionTypesToCatch[]) {
+
+ llvm::LLVMContext& context = module.getContext();
+ llvm::Function *toPrint32Int = module.getFunction("print32Int");
+
+ ArgTypes argTypes;
+ argTypes.push_back(builder.getInt32Ty());
+
+ ArgNames argNames;
+ argNames.push_back("exceptTypeToThrow");
+
+ llvm::Function* ret = createFunction(module,
+ builder.getVoidTy(),
+ argTypes,
+ argNames,
+ ourId,
+ llvm::Function::ExternalLinkage,
+ false,
+ false);
+
+ // Block which calls invoke
+ llvm::BasicBlock *entryBlock = llvm::BasicBlock::Create(context,
+ "entry",
+ ret);
+ // Normal block for invoke
+ llvm::BasicBlock *normalBlock = llvm::BasicBlock::Create(context,
+ "normal",
+ ret);
+ // Unwind block for invoke
+ llvm::BasicBlock *exceptionBlock =
+ llvm::BasicBlock::Create(context, "exception", ret);
+
+ // Block which routes exception to correct catch handler block
+ llvm::BasicBlock *exceptionRouteBlock =
+ llvm::BasicBlock::Create(context, "exceptionRoute", ret);
+
+ // Foreign exception handler
+ llvm::BasicBlock *externalExceptionBlock =
+ llvm::BasicBlock::Create(context, "externalException", ret);
+
+ // Block which calls _Unwind_Resume
+ llvm::BasicBlock *unwindResumeBlock =
+ llvm::BasicBlock::Create(context, "unwindResume", ret);
+
+ // Clean up block which delete exception if needed
+ llvm::BasicBlock *endBlock =
+ llvm::BasicBlock::Create(context, "end", ret);
+
+ std::string nextName;
+ std::vector<llvm::BasicBlock*> catchBlocks(numExceptionsToCatch);
+ llvm::Value* exceptionCaughtFlag = NULL;
+ llvm::Value* exceptionStorage = NULL;
+
+ // Finally block which will branch to unwindResumeBlock if
+ // exception is not caught. Initializes/allocates stack locations.
+ llvm::BasicBlock* finallyBlock = createFinallyBlock(context,
+ module,
+ builder,
+ *ret,
+ nextName = "finally",
+ ourId,
+ *endBlock,
+ *unwindResumeBlock,
+ &exceptionCaughtFlag,
+ &exceptionStorage);
+
+ for (unsigned i = 0; i < numExceptionsToCatch; ++i) {
+ nextName = ourTypeInfoNames[exceptionTypesToCatch[i]];
+
+ // One catch block per type info to be caught
+ catchBlocks[i] = createCatchBlock(context,
+ module,
+ builder,
+ *ret,
+ nextName,
+ ourId,
+ *finallyBlock,
+ *exceptionCaughtFlag);
+ }
+
+ // Entry Block
+
+ builder.SetInsertPoint(entryBlock);
+
+ std::vector<llvm::Value*> args;
+ args.push_back(namedValues["exceptTypeToThrow"]);
+ builder.CreateInvoke(&toInvoke,
+ normalBlock,
+ exceptionBlock,
+ args.begin(),
+ args.end());
+
+ // End Block
+
+ builder.SetInsertPoint(endBlock);
+
+ generateStringPrint(context,
+ module,
+ builder,
+ "Gen: In end block: exiting in " + ourId + ".\n",
+ USE_GLOBAL_STR_CONSTS);
+ llvm::Function *deleteOurException =
+ module.getFunction("deleteOurException");
+
+ // Note: function handles NULL exceptions
+ builder.CreateCall(deleteOurException,
+ builder.CreateLoad(exceptionStorage));
+ builder.CreateRetVoid();
+
+ // Normal Block
+
+ builder.SetInsertPoint(normalBlock);
+
+ generateStringPrint(context,
+ module,
+ builder,
+ "Gen: No exception in " + ourId + "!\n",
+ USE_GLOBAL_STR_CONSTS);
+
+ // Finally block is always called
+ builder.CreateBr(finallyBlock);
+
+ // Unwind Resume Block
+
+ builder.SetInsertPoint(unwindResumeBlock);
+
+ llvm::Function *resumeOurException =
+ module.getFunction("_Unwind_Resume");
+ builder.CreateCall(resumeOurException,
+ builder.CreateLoad(exceptionStorage));
+ builder.CreateUnreachable();
+
+ // Exception Block
+
+ builder.SetInsertPoint(exceptionBlock);
+
+ llvm::Function *ehException = module.getFunction("llvm.eh.exception");
+
+ // Retrieve thrown exception
+ llvm::Value* unwindException = builder.CreateCall(ehException);
+
+ // Store exception and flag
+ builder.CreateStore(unwindException, exceptionStorage);
+ builder.CreateStore(ourExceptionThrownState, exceptionCaughtFlag);
+ llvm::Function *personality = module.getFunction("ourPersonality");
+ llvm::Value* functPtr =
+ builder.CreatePointerCast(personality,
+ builder.getInt8Ty()->getPointerTo());
+
+ args.clear();
+ args.push_back(unwindException);
+ args.push_back(functPtr);
+
+ // Note: Skipping index 0
+ for (unsigned i = 0; i < numExceptionsToCatch; ++i) {
+ // Set up type infos to be caught
+ args.push_back(module.getGlobalVariable(
+ ourTypeInfoNames[exceptionTypesToCatch[i]]));
+ }
+
+ args.push_back(llvm::ConstantInt::get(builder.getInt32Ty(), 0));
+
+ llvm::Function *ehSelector = module.getFunction("llvm.eh.selector");
+
+ // Set up this exeption block as the landing pad which will handle
+ // given type infos. See case Intrinsic::eh_selector in
+ // SelectionDAGBuilder::visitIntrinsicCall(...) and AddCatchInfo(...)
+ // implemented in FunctionLoweringInfo.cpp to see how the implementation
+ // handles this call. This landing pad (this exception block), will be
+ // called either because it nees to cleanup (call finally) or a type
+ // info was found which matched the thrown exception.
+ llvm::Value* retTypeInfoIndex = builder.CreateCall(ehSelector,
+ args.begin(),
+ args.end());
+
+ // Retrieve exception_class member from thrown exception
+ // (_Unwind_Exception instance). This member tells us whether or not
+ // the exception is foreign.
+ llvm::Value* unwindExceptionClass =
+ builder.CreateLoad(builder.CreateStructGEP(
+ builder.CreatePointerCast(unwindException,
+ ourUnwindExceptionType->getPointerTo()),
+ 0));
+
+ // Branch to the externalExceptionBlock if the exception is foreign or
+ // to a catch router if not. Either way the finally block will be run.
+ builder.CreateCondBr(builder.CreateICmpEQ(unwindExceptionClass,
+ llvm::ConstantInt::get(builder.getInt64Ty(),
+ ourBaseExceptionClass)),
+ exceptionRouteBlock,
+ externalExceptionBlock);
+
+ // External Exception Block
+
+ builder.SetInsertPoint(externalExceptionBlock);
+
+ generateStringPrint(context,
+ module,
+ builder,
+ "Gen: Foreign exception received.\n",
+ USE_GLOBAL_STR_CONSTS);
+
+ // Branch to the finally block
+ builder.CreateBr(finallyBlock);
+
+ // Exception Route Block
+
+ builder.SetInsertPoint(exceptionRouteBlock);
+
+ // Casts exception pointer (_Unwind_Exception instance) to parent
+ // (OurException instance).
+ //
+ // Note: ourBaseFromUnwindOffset is usually negative
+ llvm::Value* typeInfoThrown =
+ builder.CreatePointerCast(builder.CreateConstGEP1_64(unwindException,
+ ourBaseFromUnwindOffset),
+ ourExceptionType->getPointerTo());
+
+ // Retrieve thrown exception type info type
+ //
+ // Note: Index is not relative to pointer but instead to structure
+ // unlike a true getelementptr (GEP) instruction
+ typeInfoThrown = builder.CreateStructGEP(typeInfoThrown, 0);
+
+ llvm::Value* typeInfoThrownType =
+ builder.CreateStructGEP(typeInfoThrown, 0);
+
+ generateIntegerPrint(context,
+ module,
+ builder,
+ *toPrint32Int,
+ *(builder.CreateLoad(typeInfoThrownType)),
+ "Gen: Exception type <%d> received (stack unwound) "
+ " in " +
+ ourId +
+ ".\n",
+ USE_GLOBAL_STR_CONSTS);
+
+ // Route to matched type info catch block or run cleanup finally block
+ llvm::SwitchInst* switchToCatchBlock =
+ builder.CreateSwitch(retTypeInfoIndex,
+ finallyBlock,
+ numExceptionsToCatch);
+
+ unsigned nextTypeToCatch;
+
+ for (unsigned i = 1; i <= numExceptionsToCatch; ++i) {
+ nextTypeToCatch = i - 1;
+ switchToCatchBlock->addCase(llvm::ConstantInt::get(
+ llvm::Type::getInt32Ty(context), i),
+ catchBlocks[nextTypeToCatch]);
+ }
+
+ llvm::verifyFunction(*ret);
+ fpm.run(*ret);
+
+ return(ret);
+}
+
+
+/// Generates function which throws either an exception matched to a runtime
+/// determined type info type (argument to generated function), or if this
+/// runtime value matches nativeThrowType, throws a foreign exception by
+/// calling nativeThrowFunct.
+/// @param module code for module instance
+/// @param builder builder instance
+/// @param fpm a function pass manager holding optional IR to IR