llvm.org GIT mirror llvm / 8550071
Converted Exception demo over to using new 3.0 landingpad instruction. This was compiled and tested on OS X 10.7.1. It was not tested on LINUX. In addition the defined OLD_EXC_SYSTEM was not tested with this version. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@140303 91177308-0d34-0410-b5e6-96231b3b80d8 Garrison Venn 7 years ago
1 changed file(s) with 60 addition(s) and 23 deletion(s). Raw diff Collapse all Expand all
6161 #include "llvm/Support/Dwarf.h"
6262 #include "llvm/Support/TargetSelect.h"
6363
64 // FIXME: See use of UpgradeExceptionHandling(...) below
64 #ifdef OLD_EXC_SYSTEM
65 // See use of UpgradeExceptionHandling(...) below
6566 #include "llvm/AutoUpgrade.h"
67 #endif
6668
6769 // FIXME: Although all systems tested with (Linux, OS X), do not need this
6870 // header file included. A user on ubuntu reported, undefined symbols
184186 static std::map ourTypeInfoNamesIndex;
185187
186188 static llvm::StructType *ourTypeInfoType;
189 #ifndef OLD_EXC_SYSTEM
190 static llvm::StructType *ourCaughtResultType;
191 #endif
187192 static llvm::StructType *ourExceptionType;
188193 static llvm::StructType *ourUnwindExceptionType;
189194
12181223
12191224 builder.SetInsertPoint(unwindResumeBlock);
12201225
1221 llvm::Function *resumeOurException =
1222 module.getFunction("_Unwind_Resume");
1226 llvm::Function *resumeOurException = module.getFunction("_Unwind_Resume");
12231227 builder.CreateCall(resumeOurException,
12241228 builder.CreateLoad(exceptionStorage));
12251229 builder.CreateUnreachable();
12281232
12291233 builder.SetInsertPoint(exceptionBlock);
12301234
1235 llvm::Function *personality = module.getFunction("ourPersonality");
1236
1237 #ifndef OLD_EXC_SYSTEM
1238 llvm::LandingPadInst *caughtResult =
1239 builder.CreateLandingPad(ourCaughtResultType,
1240 personality,
1241 numExceptionsToCatch,
1242 "landingPad");
1243
1244 caughtResult->setCleanup(true);
1245
1246 for (unsigned i = 0; i < numExceptionsToCatch; ++i) {
1247 // Set up type infos to be caught
1248 caughtResult->addClause(module.getGlobalVariable(
1249 ourTypeInfoNames[exceptionTypesToCatch[i]]));
1250 }
1251
1252 llvm::Value *unwindException = builder.CreateExtractValue(caughtResult, 0);
1253 llvm::Value *retTypeInfoIndex =
1254 builder.CreateExtractValue(caughtResult, 1);
1255
1256 builder.CreateStore(unwindException, exceptionStorage);
1257 builder.CreateStore(ourExceptionThrownState, exceptionCaughtFlag);
1258
1259 #else
12311260 llvm::Function *ehException = module.getFunction("llvm.eh.exception");
1232
1261
12331262 // Retrieve thrown exception
12341263 llvm::Value *unwindException = builder.CreateCall(ehException);
12351264
12361265 // Store exception and flag
12371266 builder.CreateStore(unwindException, exceptionStorage);
12381267 builder.CreateStore(ourExceptionThrownState, exceptionCaughtFlag);
1239 llvm::Function *personality = module.getFunction("ourPersonality");
12401268 llvm::Value *functPtr =
1241 builder.CreatePointerCast(personality,
1242 builder.getInt8PtrTy());
1269 builder.CreatePointerCast(personality, builder.getInt8PtrTy());
12431270
12441271 args.clear();
12451272 args.push_back(unwindException);
12641291 // called either because it nees to cleanup (call finally) or a type
12651292 // info was found which matched the thrown exception.
12661293 llvm::Value *retTypeInfoIndex = builder.CreateCall(ehSelector, args);
1294 #endif
12671295
12681296 // Retrieve exception_class member from thrown exception
12691297 // (_Unwind_Exception instance). This member tells us whether or not
13421370 llvm::Type::getInt32Ty(context), i),
13431371 catchBlocks[nextTypeToCatch]);
13441372 }
1345
1346 // FIXME: This is a hack to get the demo working with the new 3.0 exception
1347 // infrastructure. As this makes the demo no longer a demo, and
1348 // especially not a demo on how to use the llvm exception mechanism,
1349 // this hack will shortly be changed to use the new 3.0 exception
1350 // infrastructure. However for the time being this demo is an
1351 // example on how to use the AutoUpgrade UpgradeExceptionHandling(...)
1352 // function on < 3.0 exception handling code.
1353 //
1354 // Must be run before verifier
1373
1374 #ifdef OLD_EXC_SYSTEM
1375 // Must be run before verifier
13551376 UpgradeExceptionHandling(&module);
1356
1377 #endif
1378
1379
13571380 llvm::verifyFunction(*ret);
13581381 fpm.run(*ret);
13591382
16601683 // Create our type info type
16611684 ourTypeInfoType = llvm::StructType::get(context,
16621685 TypeArray(builder.getInt32Ty()));
1686
1687 #ifndef OLD_EXC_SYSTEM
1688
1689 llvm::Type *caughtResultFieldTypes[] = {
1690 builder.getInt8PtrTy(),
1691 builder.getInt32Ty()
1692 };
1693
1694 // Create our landingpad result type
1695 ourCaughtResultType = llvm::StructType::get(context,
1696 TypeArray(caughtResultFieldTypes));
1697
1698 #endif
1699
16631700 // Create OurException type
16641701 ourExceptionType = llvm::StructType::get(context,
16651702 TypeArray(ourTypeInfoType));
16761713
16771714 // Calculate offset of OurException::unwindException member.
16781715 ourBaseFromUnwindOffset = ((uintptr_t) &dummyException) -
1679 ((uintptr_t) &(dummyException.unwindException));
1716 ((uintptr_t) &(dummyException.unwindException));
16801717
16811718 #ifdef DEBUG
16821719 fprintf(stderr,
19942031 // Generate test code using function throwCppException(...) as
19952032 // the function which throws foreign exceptions.
19962033 llvm::Function *toRun =
1997 createUnwindExceptionTest(*module,
1998 theBuilder,
1999 fpm,
2000 "throwCppException");
2034 createUnwindExceptionTest(*module,
2035 theBuilder,
2036 fpm,
2037 "throwCppException");
20012038
20022039 fprintf(stderr, "\nBegin module dump:\n\n");
20032040