llvm.org GIT mirror llvm / 10ae0ad
Add support for invoke/landingpad/resume in C API test Summary: As per title. There was a lot of part missing in the C API, so I had to extend the invoke and landingpad API. Reviewers: echristo, joker.eph, Wallbraker Subscribers: llvm-commits Differential Revision: http://reviews.llvm.org/D17359 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@261254 91177308-0d34-0410-b5e6-96231b3b80d8 Amaury Sechet 4 years ago
4 changed file(s) with 204 addition(s) and 2 deletion(s). Raw diff Collapse all Expand all
18031803 void LLVMDeleteFunction(LLVMValueRef Fn);
18041804
18051805 /**
1806 * Check whether the given function has a personality function.
1807 *
1808 * @see llvm::Function::hasPersonalityFn()
1809 */
1810 LLVMBool LLVMHasPersonalityFn(LLVMValueRef Fn);
1811
1812 /**
18061813 * Obtain the personality function attached to the function.
18071814 *
18081815 * @see llvm::Function::getPersonalityFn()
24662473 * @see llvm::CallInst::setTailCall()
24672474 */
24682475 void LLVMSetTailCall(LLVMValueRef CallInst, LLVMBool IsTailCall);
2476
2477 /**
2478 * Return the normal destination basic block.
2479 *
2480 * This only works on llvm::InvokeInst instructions.
2481 *
2482 * @see llvm::InvokeInst::getNormalDest()
2483 */
2484 LLVMBasicBlockRef LLVMGetNormalDest(LLVMValueRef InvokeInst);
2485
2486 /**
2487 * Return the unwind destination basic block.
2488 *
2489 * This only works on llvm::InvokeInst instructions.
2490 *
2491 * @see llvm::InvokeInst::getUnwindDest()
2492 */
2493 LLVMBasicBlockRef LLVMGetUnwindDest(LLVMValueRef InvokeInst);
2494
2495 /**
2496 * Set the normal destination basic block.
2497 *
2498 * This only works on llvm::InvokeInst instructions.
2499 *
2500 * @see llvm::InvokeInst::setNormalDest()
2501 */
2502 void LLVMSetNormalDest(LLVMValueRef InvokeInst, LLVMBasicBlockRef B);
2503
2504 /**
2505 * Set the unwind destination basic block.
2506 *
2507 * This only works on llvm::InvokeInst instructions.
2508 *
2509 * @see llvm::InvokeInst::setUnwindDest()
2510 */
2511 void LLVMSetUnwindDest(LLVMValueRef InvokeInst, LLVMBasicBlockRef B);
24692512
24702513 /**
24712514 * @}
27052748 /* Add a destination to the indirectbr instruction */
27062749 void LLVMAddDestination(LLVMValueRef IndirectBr, LLVMBasicBlockRef Dest);
27072750
2751 /* Get the number of clauses on the landingpad instruction */
2752 unsigned LLVMGetNumClauses(LLVMValueRef LandingPad);
2753
2754 /* Get the value of the clause at idnex Idx on the landingpad instruction */
2755 LLVMValueRef LLVMGetClause(LLVMValueRef LandingPad, unsigned Idx);
2756
27082757 /* Add a catch or filter clause to the landingpad instruction */
27092758 void LLVMAddClause(LLVMValueRef LandingPad, LLVMValueRef ClauseVal);
2759
2760 /* Get the 'cleanup' flag in the landingpad instruction */
2761 LLVMBool LLVMIsCleanup(LLVMValueRef LandingPad);
27102762
27112763 /* Set the 'cleanup' flag in the landingpad instruction */
27122764 void LLVMSetCleanup(LLVMValueRef LandingPad, LLVMBool Val);
17001700 unwrap(Fn)->eraseFromParent();
17011701 }
17021702
1703 LLVMBool LLVMHasPersonalityFn(LLVMValueRef Fn) {
1704 return unwrap(Fn)->hasPersonalityFn();
1705 }
1706
17031707 LLVMValueRef LLVMGetPersonalityFn(LLVMValueRef Fn) {
17041708 return wrap(unwrap(Fn)->getPersonalityFn());
17051709 }
21042108
21052109 void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) {
21062110 unwrap(Call)->setTailCall(isTailCall);
2111 }
2112
2113 /*--.. Operations on invoke instructions (only) ............................--*/
2114
2115 LLVMBasicBlockRef LLVMGetNormalDest(LLVMValueRef Invoke) {
2116 return wrap(unwrap(Invoke)->getNormalDest());
2117 }
2118
2119 LLVMBasicBlockRef LLVMGetUnwindDest(LLVMValueRef Invoke) {
2120 return wrap(unwrap(Invoke)->getUnwindDest());
2121 }
2122
2123 void LLVMSetNormalDest(LLVMValueRef Invoke, LLVMBasicBlockRef B) {
2124 unwrap(Invoke)->setNormalDest(unwrap(B));
2125 }
2126
2127 void LLVMSetUnwindDest(LLVMValueRef Invoke, LLVMBasicBlockRef B) {
2128 unwrap(Invoke)->setUnwindDest(unwrap(B));
21072129 }
21082130
21092131 /*--.. Operations on terminators ...........................................--*/
23412363 unwrap(IndirectBr)->addDestination(unwrap(Dest));
23422364 }
23432365
2366 unsigned LLVMGetNumClauses(LLVMValueRef LandingPad) {
2367 return unwrap(LandingPad)->getNumClauses();
2368 }
2369
2370 LLVMValueRef LLVMGetClause(LLVMValueRef LandingPad, unsigned Idx) {
2371 return wrap(unwrap(LandingPad)->getClause(Idx));
2372 }
2373
23442374 void LLVMAddClause(LLVMValueRef LandingPad, LLVMValueRef ClauseVal) {
23452375 unwrap(LandingPad)->
23462376 addClause(cast(unwrap(ClauseVal)));
2377 }
2378
2379 LLVMBool LLVMIsCleanup(LLVMValueRef LandingPad) {
2380 return unwrap(LandingPad)->isCleanup();
23472381 }
23482382
23492383 void LLVMSetCleanup(LLVMValueRef LandingPad, LLVMBool Val) {
0 ; RUN: llvm-as < %s | llvm-dis > %t.orig
1 ; RUN: llvm-as < %s | llvm-c-test --echo > %t.echo
2 ; RUN: diff -w %t.orig %t.echo
3
4 %C6object9ClassInfo = type { %C6object9ClassInfo__vtbl*, %C6object9ClassInfo* }
5 %C6object9ClassInfo__vtbl = type { %C6object9ClassInfo* }
6 %C6object9Exception__vtbl = type { %C6object9ClassInfo* }
7 %C6object6Object = type { %C6object6Object__vtbl* }
8 %C6object6Object__vtbl = type { %C6object9ClassInfo* }
9 %C6object9Throwable = type { %C6object9Throwable__vtbl* }
10 %C6object9Throwable__vtbl = type { %C6object9ClassInfo* }
11
12 @C6object9ClassInfo__ClassInfo = linkonce_odr constant %C6object9ClassInfo { %C6object9ClassInfo__vtbl* @C6object9ClassInfo__vtblZ, %C6object9ClassInfo* @C6object8TypeInfo__ClassInfo }
13 @C6object9ClassInfo__vtblZ = linkonce_odr constant %C6object9ClassInfo__vtbl { %C6object9ClassInfo* @C6object9ClassInfo__ClassInfo }
14 @C6object8TypeInfo__ClassInfo = linkonce_odr constant %C6object9ClassInfo { %C6object9ClassInfo__vtbl* @C6object9ClassInfo__vtblZ, %C6object9ClassInfo* @C6object6Object__ClassInfo }
15 @C6object6Object__ClassInfo = linkonce_odr constant %C6object9ClassInfo { %C6object9ClassInfo__vtbl* @C6object9ClassInfo__vtblZ, %C6object9ClassInfo* @C6object6Object__ClassInfo }
16 @C6object9Throwable__ClassInfo = linkonce_odr constant %C6object9ClassInfo { %C6object9ClassInfo__vtbl* @C6object9ClassInfo__vtblZ, %C6object9ClassInfo* @C6object6Object__ClassInfo }
17 @C6object9Exception__ClassInfo = linkonce_odr constant %C6object9ClassInfo { %C6object9ClassInfo__vtbl* @C6object9ClassInfo__vtblZ, %C6object9ClassInfo* @C6object9Throwable__ClassInfo }
18 @C6object9Exception__vtblZ = linkonce_odr constant %C6object9Exception__vtbl { %C6object9ClassInfo* @C6object9Exception__ClassInfo }
19 @C6object5Error__ClassInfo = linkonce_odr constant %C6object9ClassInfo { %C6object9ClassInfo__vtbl* @C6object9ClassInfo__vtblZ, %C6object9ClassInfo* @C6object9Throwable__ClassInfo }
20
21 define i32 @_D8test01494mainFMZi() personality i32 (i32, i32, i64, i8*, i8*)* @__sd_eh_personality {
22 body:
23 %0 = invoke i8* @_d_allocmemory(i64 8)
24 to label %then unwind label %landingPad
25
26 then: ; preds = %body
27 %1 = bitcast i8* %0 to i8**
28 store i8* bitcast (%C6object9Exception__vtbl* @C6object9Exception__vtblZ to i8*), i8** %1, align 8
29 %2 = bitcast i8* %0 to %C6object6Object*
30 invoke void @_D6object6Object6__ctorFMC6object6ObjectZv(%C6object6Object* %2)
31 to label %then1 unwind label %landingPad
32
33 then1: ; preds = %then
34 %3 = bitcast i8* %0 to %C6object9Throwable*
35 invoke void @__sd_eh_throw(%C6object9Throwable* %3)
36 to label %then2 unwind label %landingPad
37
38 then2: ; preds = %then1
39 unreachable
40
41 landingPad: ; preds = %then1, %then, %body
42 %4 = landingpad { i8*, i32 }
43 cleanup
44 catch %C6object9ClassInfo* @C6object5Error__ClassInfo
45 catch %C6object9ClassInfo* @C6object9Exception__ClassInfo
46 catch %C6object9ClassInfo* @C6object9Throwable__ClassInfo
47 %5 = extractvalue { i8*, i32 } %4, 1
48 %6 = tail call i32 @llvm.eh.typeid.for(i8* bitcast (%C6object9ClassInfo* @C6object5Error__ClassInfo to i8*))
49 %7 = icmp eq i32 %6, %5
50 br i1 %7, label %catch, label %unwind3
51
52 catch: ; preds = %unwind5, %unwind3, %landingPad
53 %merge = phi i32 [ 23, %landingPad ], [ 19, %unwind3 ], [ 13, %unwind5 ]
54 ret i32 %merge
55
56 unwind3: ; preds = %landingPad
57 %8 = tail call i32 @llvm.eh.typeid.for(i8* bitcast (%C6object9ClassInfo* @C6object9Exception__ClassInfo to i8*))
58 %9 = icmp eq i32 %8, %5
59 br i1 %9, label %catch, label %unwind5
60
61 unwind5: ; preds = %unwind3
62 %10 = tail call i32 @llvm.eh.typeid.for(i8* bitcast (%C6object9ClassInfo* @C6object9Throwable__ClassInfo to i8*))
63 %11 = icmp eq i32 %10, %5
64 br i1 %11, label %catch, label %unwind7
65
66 unwind7: ; preds = %unwind5
67 resume { i8*, i32 } %4
68 }
69
70 declare void @_D6object6Object6__ctorFMC6object6ObjectZv(%C6object6Object*)
71
72 declare i8* @_d_allocmemory(i64)
73
74 declare i32 @__sd_eh_personality(i32, i32, i64, i8*, i8*)
75
76 declare void @__sd_eh_throw(%C6object9Throwable*)
77
78 ; Function Attrs: nounwind readnone
79 declare i32 @llvm.eh.typeid.for(i8*) #0
80
81 attributes #0 = { nounwind readnone }
387387 }
388388 case LLVMSwitch:
389389 case LLVMIndirectBr:
390 case LLVMInvoke:
391 break;
390 break;
391 case LLVMInvoke: {
392 SmallVector Args;
393 int ArgCount = LLVMGetNumArgOperands(Src);
394 for (int i = 0; i < ArgCount; i++)
395 Args.push_back(CloneValue(LLVMGetOperand(Src, i)));
396 LLVMValueRef Fn = CloneValue(LLVMGetCalledValue(Src));
397 LLVMBasicBlockRef Then = DeclareBB(LLVMGetNormalDest(Src));
398 LLVMBasicBlockRef Unwind = DeclareBB(LLVMGetUnwindDest(Src));
399 Dst = LLVMBuildInvoke(Builder, Fn, Args.data(), ArgCount,
400 Then, Unwind, Name);
401 break;
402 }
392403 case LLVMUnreachable:
393404 Dst = LLVMBuildUnreachable(Builder);
394405 break;
535546 Args.push_back(CloneValue(LLVMGetOperand(Src, i)));
536547 LLVMValueRef Fn = CloneValue(LLVMGetCalledValue(Src));
537548 Dst = LLVMBuildCall(Builder, Fn, Args.data(), ArgCount, Name);
549 LLVMSetTailCall(Dst, LLVMIsTailCall(Src));
550 break;
551 }
552 case LLVMResume: {
553 Dst = LLVMBuildResume(Builder, CloneValue(LLVMGetOperand(Src, 0)));
554 break;
555 }
556 case LLVMLandingPad: {
557 // The landing pad API is a bit screwed up for historical reasons.
558 Dst = LLVMBuildLandingPad(Builder, CloneType(Src), nullptr, 0, Name);
559 unsigned NumClauses = LLVMGetNumClauses(Src);
560 for (unsigned i = 0; i < NumClauses; ++i)
561 LLVMAddClause(Dst, CloneValue(LLVMGetClause(Src, i)));
562 LLVMSetCleanup(Dst, LLVMIsCleanup(Src));
538563 break;
539564 }
540565 case LLVMExtractValue: {
787812 LLVMValueRef Fun = LLVMGetNamedFunction(M, Name);
788813 if (!Fun)
789814 report_fatal_error("Function must have been declared already");
815
816 if (LLVMHasPersonalityFn(Cur)) {
817 const char *FName = LLVMGetValueName(LLVMGetPersonalityFn(Cur));
818 LLVMValueRef P = LLVMGetNamedFunction(M, FName);
819 if (!P)
820 report_fatal_error("Could not find personality function");
821 LLVMSetPersonalityFn(Fun, P);
822 }
823
790824 FunCloner FC(Cur, Fun);
791825 FC.CloneBBs(Cur);
792826