llvm.org GIT mirror llvm / 9cb5086
Modified demo to use 3.0 resume instruction vs calling _Unwine_Resume. Also conducted some reformatting. As the LLVM coding standard doc does not seem to touch on how to align function arguments, and format code longer than 80 cols in general, the confusion persists. There is the golden rule, but as this code has gone through several styles to deal with this, the golden rule seems to be ignored. The latest reformatting effort tries to match the other source files as much as possible. Tested on OS X 10.7.1 with, and without the OLD_EXC_SYSTEM defined. Have NOT tested on LINUX. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@140379 91177308-0d34-0410-b5e6-96231b3b80d8 Garrison Venn 7 years ago
1 changed file(s) with 118 addition(s) and 95 deletion(s). Raw diff Collapse all Expand all
900900 builder.CreateStore(stringConstant, stringVar);
901901 }
902902
903 llvm::Value *cast =
904 builder.CreatePointerCast(stringVar,
905 builder.getInt8PtrTy());
903 llvm::Value *cast = builder.CreatePointerCast(stringVar,
904 builder.getInt8PtrTy());
906905 builder.CreateCall(printFunct, cast);
907906 }
908907
944943 builder.CreateStore(stringConstant, stringVar);
945944 }
946945
947 llvm::Value *cast =
948 builder.CreateBitCast(stringVar,
949 builder.getInt8PtrTy());
946 llvm::Value *cast = builder.CreateBitCast(stringVar,
947 builder.getInt8PtrTy());
950948 builder.CreateCall2(&printFunct, &toPrint, cast);
951949 }
952950
969967 /// @param unwindResumeBlock unwind resume block
970968 /// @param exceptionCaughtFlag reference exception caught/thrown status storage
971969 /// @param exceptionStorage reference to exception pointer storage
970 #ifndef OLD_EXC_SYSTEM
971 /// @param caughtResultStorage reference to landingpad result storage
972 #endif
972973 /// @returns newly created block
973974 static llvm::BasicBlock *createFinallyBlock(llvm::LLVMContext &context,
974975 llvm::Module &module,
979980 llvm::BasicBlock &terminatorBlock,
980981 llvm::BasicBlock &unwindResumeBlock,
981982 llvm::Value **exceptionCaughtFlag,
982 llvm::Value **exceptionStorage) {
983 llvm::Value **exceptionStorage
984 #ifndef OLD_EXC_SYSTEM
985 ,llvm::Value **caughtResultStorage
986 #endif
987 ) {
983988 assert(exceptionCaughtFlag &&
984989 "ExceptionDemo::createFinallyBlock(...):exceptionCaughtFlag "
985990 "is NULL");
986991 assert(exceptionStorage &&
987992 "ExceptionDemo::createFinallyBlock(...):exceptionStorage "
988993 "is NULL");
989
990 *exceptionCaughtFlag =
991 createEntryBlockAlloca(toAddTo,
992 "exceptionCaught",
993 ourExceptionNotThrownState->getType(),
994 ourExceptionNotThrownState);
994
995 #ifndef OLD_EXC_SYSTEM
996 assert(caughtResultStorage &&
997 "ExceptionDemo::createFinallyBlock(...):caughtResultStorage "
998 "is NULL");
999 #endif
1000
1001 *exceptionCaughtFlag = createEntryBlockAlloca(toAddTo,
1002 "exceptionCaught",
1003 ourExceptionNotThrownState->getType(),
1004 ourExceptionNotThrownState);
9951005
9961006 llvm::PointerType *exceptionStorageType = builder.getInt8PtrTy();
997 *exceptionStorage =
998 createEntryBlockAlloca(toAddTo,
999 "exceptionStorage",
1000 exceptionStorageType,
1001 llvm::ConstantPointerNull::get(
1002 exceptionStorageType));
1007 *exceptionStorage = createEntryBlockAlloca(toAddTo,
1008 "exceptionStorage",
1009 exceptionStorageType,
1010 llvm::ConstantPointerNull::get(
1011 exceptionStorageType));
1012 #ifndef OLD_EXC_SYSTEM
1013 *caughtResultStorage = createEntryBlockAlloca(toAddTo,
1014 "caughtResultStorage",
1015 ourCaughtResultType,
1016 llvm::ConstantAggregateZero::get(
1017 ourCaughtResultType));
1018 #endif
10031019
10041020 llvm::BasicBlock *ret = llvm::BasicBlock::Create(context,
10051021 blockName,
10161032 bufferToPrint.str(),
10171033 USE_GLOBAL_STR_CONSTS);
10181034
1019 llvm::SwitchInst *theSwitch =
1020 builder.CreateSwitch(builder.CreateLoad(*exceptionCaughtFlag),
1021 &terminatorBlock,
1022 2);
1035 llvm::SwitchInst *theSwitch = builder.CreateSwitch(builder.CreateLoad(
1036 *exceptionCaughtFlag),
1037 &terminatorBlock,
1038 2);
10231039 theSwitch->addCase(ourExceptionCaughtState, &terminatorBlock);
10241040 theSwitch->addCase(ourExceptionThrownState, &unwindResumeBlock);
10251041
11271143 "normal",
11281144 ret);
11291145 // Unwind block for invoke
1130 llvm::BasicBlock *exceptionBlock =
1131 llvm::BasicBlock::Create(context, "exception", ret);
1146 llvm::BasicBlock *exceptionBlock = llvm::BasicBlock::Create(context,
1147 "exception",
1148 ret);
11321149
11331150 // Block which routes exception to correct catch handler block
1134 llvm::BasicBlock *exceptionRouteBlock =
1135 llvm::BasicBlock::Create(context, "exceptionRoute", ret);
1151 llvm::BasicBlock *exceptionRouteBlock = llvm::BasicBlock::Create(context,
1152 "exceptionRoute",
1153 ret);
11361154
11371155 // Foreign exception handler
1138 llvm::BasicBlock *externalExceptionBlock =
1139 llvm::BasicBlock::Create(context, "externalException", ret);
1156 llvm::BasicBlock *externalExceptionBlock = llvm::BasicBlock::Create(context,
1157 "externalException",
1158 ret);
11401159
11411160 // Block which calls _Unwind_Resume
1142 llvm::BasicBlock *unwindResumeBlock =
1143 llvm::BasicBlock::Create(context, "unwindResume", ret);
1161 llvm::BasicBlock *unwindResumeBlock = llvm::BasicBlock::Create(context,
1162 "unwindResume",
1163 ret);
11441164
11451165 // Clean up block which delete exception if needed
1146 llvm::BasicBlock *endBlock =
1147 llvm::BasicBlock::Create(context, "end", ret);
1166 llvm::BasicBlock *endBlock = llvm::BasicBlock::Create(context, "end", ret);
11481167
11491168 std::string nextName;
11501169 std::vector catchBlocks(numExceptionsToCatch);
11511170 llvm::Value *exceptionCaughtFlag = NULL;
11521171 llvm::Value *exceptionStorage = NULL;
1172 #ifndef OLD_EXC_SYSTEM
1173 llvm::Value *caughtResultStorage = NULL;
1174 #endif
11531175
11541176 // Finally block which will branch to unwindResumeBlock if
11551177 // exception is not caught. Initializes/allocates stack locations.
11621184 *endBlock,
11631185 *unwindResumeBlock,
11641186 &exceptionCaughtFlag,
1165 &exceptionStorage);
1187 &exceptionStorage
1188 #ifndef OLD_EXC_SYSTEM
1189 ,&caughtResultStorage
1190 #endif
1191 );
11661192
11671193 for (unsigned i = 0; i < numExceptionsToCatch; ++i) {
11681194 nextName = ourTypeInfoNames[exceptionTypesToCatch[i]];
11981224 builder,
11991225 "Gen: In end block: exiting in " + ourId + ".\n",
12001226 USE_GLOBAL_STR_CONSTS);
1201 llvm::Function *deleteOurException =
1202 module.getFunction("deleteOurException");
1227 llvm::Function *deleteOurException = module.getFunction("deleteOurException");
12031228
12041229 // Note: function handles NULL exceptions
12051230 builder.CreateCall(deleteOurException,
12231248
12241249 builder.SetInsertPoint(unwindResumeBlock);
12251250
1251
1252 #ifndef OLD_EXC_SYSTEM
1253 builder.CreateResume(builder.CreateLoad(caughtResultStorage));
1254 #else
12261255 llvm::Function *resumeOurException = module.getFunction("_Unwind_Resume");
12271256 builder.CreateCall(resumeOurException,
12281257 builder.CreateLoad(exceptionStorage));
12291258 builder.CreateUnreachable();
1259 #endif
12301260
12311261 // Exception Block
12321262
12501280 }
12511281
12521282 llvm::Value *unwindException = builder.CreateExtractValue(caughtResult, 0);
1253 llvm::Value *retTypeInfoIndex =
1254 builder.CreateExtractValue(caughtResult, 1);
1255
1283 llvm::Value *retTypeInfoIndex = builder.CreateExtractValue(caughtResult, 1);
1284
1285 // FIXME: Redundant storage which, beyond utilizing value of
1286 // caughtResultStore for unwindException storage, may be alleviated
1287 // alltogether with a block rearrangement
1288 builder.CreateStore(caughtResult, caughtResultStorage);
12561289 builder.CreateStore(unwindException, exceptionStorage);
12571290 builder.CreateStore(ourExceptionThrownState, exceptionCaughtFlag);
1258
12591291 #else
12601292 llvm::Function *ehException = module.getFunction("llvm.eh.exception");
12611293
12651297 // Store exception and flag
12661298 builder.CreateStore(unwindException, exceptionStorage);
12671299 builder.CreateStore(ourExceptionThrownState, exceptionCaughtFlag);
1268 llvm::Value *functPtr =
1269 builder.CreatePointerCast(personality, builder.getInt8PtrTy());
1300 llvm::Value *functPtr = builder.CreatePointerCast(personality,
1301 builder.getInt8PtrTy());
12701302
12711303 args.clear();
12721304 args.push_back(unwindException);
13311363 // (OurException instance).
13321364 //
13331365 // Note: ourBaseFromUnwindOffset is usually negative
1334 llvm::Value *typeInfoThrown =
1335 builder.CreatePointerCast(builder.CreateConstGEP1_64(unwindException,
1366 llvm::Value *typeInfoThrown = builder.CreatePointerCast(
1367 builder.CreateConstGEP1_64(unwindException,
13361368 ourBaseFromUnwindOffset),
1337 ourExceptionType->getPointerTo());
1369 ourExceptionType->getPointerTo());
13381370
13391371 // Retrieve thrown exception type info type
13401372 //
13571389 USE_GLOBAL_STR_CONSTS);
13581390
13591391 // Route to matched type info catch block or run cleanup finally block
1360 llvm::SwitchInst *switchToCatchBlock =
1361 builder.CreateSwitch(retTypeInfoIndex,
1362 finallyBlock,
1363 numExceptionsToCatch);
1392 llvm::SwitchInst *switchToCatchBlock = builder.CreateSwitch(retTypeInfoIndex,
1393 finallyBlock,
1394 numExceptionsToCatch);
13641395
13651396 unsigned nextTypeToCatch;
13661397
14271458 "entry",
14281459 ret);
14291460 // Throws a foreign exception
1430 llvm::BasicBlock *nativeThrowBlock =
1431 llvm::BasicBlock::Create(context,
1432 "nativeThrow",
1433 ret);
1461 llvm::BasicBlock *nativeThrowBlock = llvm::BasicBlock::Create(context,
1462 "nativeThrow",
1463 ret);
14341464 // Throws one of our Exceptions
1435 llvm::BasicBlock *generatedThrowBlock =
1436 llvm::BasicBlock::Create(context,
1437 "generatedThrow",
1438 ret);
1465 llvm::BasicBlock *generatedThrowBlock = llvm::BasicBlock::Create(context,
1466 "generatedThrow",
1467 ret);
14391468 // Retrieved runtime type info type to throw
14401469 llvm::Value *exceptionType = namedValues["exceptTypeToThrow"];
14411470
14771506
14781507 builder.SetInsertPoint(generatedThrowBlock);
14791508
1480 llvm::Function *createOurException =
1481 module.getFunction("createOurException");
1482 llvm::Function *raiseOurException =
1483 module.getFunction("_Unwind_RaiseException");
1509 llvm::Function *createOurException = module.getFunction("createOurException");
1510 llvm::Function *raiseOurException = module.getFunction(
1511 "_Unwind_RaiseException");
14841512
14851513 // Creates exception to throw with runtime type info type.
1486 llvm::Value *exception =
1487 builder.CreateCall(createOurException,
1488 namedValues["exceptTypeToThrow"]);
1514 llvm::Value *exception = builder.CreateCall(createOurException,
1515 namedValues["exceptTypeToThrow"]);
14891516
14901517 // Throw generated Exception
14911518 builder.CreateCall(raiseOurException, exception);
15331560 createStandardUtilityFunctions(numTypeInfos,
15341561 module,
15351562 builder);
1536 llvm::Function *nativeThrowFunct =
1537 module.getFunction(nativeThrowFunctName);
1563 llvm::Function *nativeThrowFunct = module.getFunction(nativeThrowFunctName);
15381564
15391565 // Create exception throw function using the value ~0 to cause
15401566 // foreign exceptions to be thrown.
1541 llvm::Function *throwFunct =
1542 createThrowExceptionFunction(module,
1543 builder,
1544 fpm,
1545 "throwFunct",
1546 ~0,
1547 *nativeThrowFunct);
1567 llvm::Function *throwFunct = createThrowExceptionFunction(module,
1568 builder,
1569 fpm,
1570 "throwFunct",
1571 ~0,
1572 *nativeThrowFunct);
15481573 // Inner function will catch even type infos
15491574 unsigned innerExceptionTypesToCatch[] = {6, 2, 4};
15501575 size_t numExceptionTypesToCatch = sizeof(innerExceptionTypesToCatch) /
1551 sizeof(unsigned);
1576 sizeof(unsigned);
15521577
15531578 // Generate inner function.
1554 llvm::Function *innerCatchFunct =
1555 createCatchWrappedInvokeFunction(module,
1556 builder,
1557 fpm,
1558 *throwFunct,
1559 "innerCatchFunct",
1560 numExceptionTypesToCatch,
1561 innerExceptionTypesToCatch);
1579 llvm::Function *innerCatchFunct = createCatchWrappedInvokeFunction(module,
1580 builder,
1581 fpm,
1582 *throwFunct,
1583 "innerCatchFunct",
1584 numExceptionTypesToCatch,
1585 innerExceptionTypesToCatch);
15621586
15631587 // Outer function will catch odd type infos
15641588 unsigned outerExceptionTypesToCatch[] = {3, 1, 5};
15661590 sizeof(unsigned);
15671591
15681592 // Generate outer function
1569 llvm::Function *outerCatchFunct =
1570 createCatchWrappedInvokeFunction(module,
1571 builder,
1572 fpm,
1573 *innerCatchFunct,
1574 "outerCatchFunct",
1575 numExceptionTypesToCatch,
1576 outerExceptionTypesToCatch);
1593 llvm::Function *outerCatchFunct = createCatchWrappedInvokeFunction(module,
1594 builder,
1595 fpm,
1596 *innerCatchFunct,
1597 "outerCatchFunct",
1598 numExceptionTypesToCatch,
1599 outerExceptionTypesToCatch);
15771600
15781601 // Return outer function to run
15791602 return(outerCatchFunct);
16721695
16731696 // Setup exception catch state
16741697 ourExceptionNotThrownState =
1675 llvm::ConstantInt::get(llvm::Type::getInt8Ty(context), 0),
1698 llvm::ConstantInt::get(llvm::Type::getInt8Ty(context), 0),
16761699 ourExceptionThrownState =
1677 llvm::ConstantInt::get(llvm::Type::getInt8Ty(context), 1),
1700 llvm::ConstantInt::get(llvm::Type::getInt8Ty(context), 1),
16781701 ourExceptionCaughtState =
1679 llvm::ConstantInt::get(llvm::Type::getInt8Ty(context), 2),
1702 llvm::ConstantInt::get(llvm::Type::getInt8Ty(context), 2),
16801703
16811704
16821705
16831706 // Create our type info type
16841707 ourTypeInfoType = llvm::StructType::get(context,
1685 TypeArray(builder.getInt32Ty()));
1708 TypeArray(builder.getInt32Ty()));
16861709
16871710 #ifndef OLD_EXC_SYSTEM
16881711
17131736
17141737 // Calculate offset of OurException::unwindException member.
17151738 ourBaseFromUnwindOffset = ((uintptr_t) &dummyException) -
1716 ((uintptr_t) &(dummyException.unwindException));
1739 ((uintptr_t) &(dummyException.unwindException));
17171740
17181741 #ifdef DEBUG
17191742 fprintf(stderr,