fragile-arc.m
7.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// RUN: %clang_cc1 -triple i386-apple-darwin10 -emit-llvm -fblocks -fobjc-arc -fobjc-exceptions -fobjc-runtime=macosx-fragile-10.10 -o - %s | FileCheck %s
// RUN: %clang_cc1 -triple i386-apple-darwin10 -emit-llvm -fblocks -fobjc-arc -fobjc-exceptions -fobjc-runtime=macosx-fragile-10.10 -o - %s | FileCheck %s -check-prefix=GLOBALS
@class Opaque;
@interface Root {
Class isa;
}
@end
@interface A : Root {
Opaque *strong;
__weak Opaque *weak;
}
@end
// GLOBALS-LABEL @OBJC_METACLASS_A
// Strong layout: scan the first word.
// GLOBALS: @OBJC_CLASS_NAME_{{.*}} = private unnamed_addr constant [2 x i8] c"\01\00"
// Weak layout: skip the first word, scan the second word.
// GLOBALS: @OBJC_CLASS_NAME_{{.*}} = private unnamed_addr constant [2 x i8] c"\11\00"
// 0x04002001
// ^ is compiled by ARC (controls interpretation of layouts)
// ^ has C++ structors (no distinction for zero-initializable)
// ^ factory (always set on non-metaclasses)
// GLOBALS: @OBJC_CLASS_A = private global {{.*}} i32 67117057
@implementation A
// CHECK-LABEL: define internal void @"\01-[A testStrong]"
// CHECK: [[SELFVAR:%.*]] = alloca [[A:%.*]]*, align 4
- (void) testStrong {
// CHECK: [[X:%.*]] = alloca [[OPAQUE:%.*]]*, align 4
// CHECK: [[SELF:%.*]] = load [[A]]*, [[A]]** [[SELFVAR]]
// CHECK-NEXT: [[T0:%.*]] = bitcast [[A]]* [[SELF]] to i8*
// CHECK-NEXT: [[T1:%.*]] = getelementptr inbounds i8, i8* [[T0]], i32 4
// CHECK-NEXT: [[IVAR:%.*]] = bitcast i8* [[T1]] to [[OPAQUE]]**
// CHECK-NEXT: [[T0:%.*]] = load [[OPAQUE]]*, [[OPAQUE]]** [[IVAR]]
// CHECK-NEXT: [[T1:%.*]] = bitcast [[OPAQUE]]* [[T0]] to i8*
// CHECK-NEXT: [[T2:%.*]] = call i8* @llvm.objc.retain(i8* [[T1]])
// CHECK-NEXT: [[T3:%.*]] = bitcast i8* [[T2]] to [[OPAQUE]]*
// CHECK-NEXT: store [[OPAQUE]]* [[T3]], [[OPAQUE]]** [[X]]
Opaque *x = strong;
// CHECK-NEXT: [[VALUE:%.*]] = load [[OPAQUE]]*, [[OPAQUE]]** [[X]]
// CHECK-NEXT: [[SELF:%.*]] = load [[A]]*, [[A]]** [[SELFVAR]]
// CHECK-NEXT: [[T0:%.*]] = bitcast [[A]]* [[SELF]] to i8*
// CHECK-NEXT: [[T1:%.*]] = getelementptr inbounds i8, i8* [[T0]], i32 4
// CHECK-NEXT: [[IVAR:%.*]] = bitcast i8* [[T1]] to [[OPAQUE]]**
// CHECK-NEXT: [[T0:%.*]] = bitcast [[OPAQUE]]** [[IVAR]] to i8**
// CHECK-NEXT: [[T1:%.*]] = bitcast [[OPAQUE]]* [[VALUE]] to i8*
// CHECK-NEXT: call void @llvm.objc.storeStrong(i8** [[T0]], i8* [[T1]])
strong = x;
// CHECK-NEXT: [[T0:%.*]] = bitcast [[OPAQUE]]** [[X]] to i8**
// CHECK-NEXT: call void @llvm.objc.storeStrong(i8** [[T0]], i8* null)
// CHECK-NEXT: ret void
}
// CHECK-LABEL: define internal void @"\01-[A testWeak]"
// CHECK: [[SELFVAR:%.*]] = alloca [[A]]*, align 4
- (void) testWeak {
// CHECK: [[X:%.*]] = alloca [[OPAQUE]]*, align 4
// CHECK: [[SELF:%.*]] = load [[A]]*, [[A]]** [[SELFVAR]]
// CHECK-NEXT: [[T0:%.*]] = bitcast [[A]]* [[SELF]] to i8*
// CHECK-NEXT: [[T1:%.*]] = getelementptr inbounds i8, i8* [[T0]], i32 8
// CHECK-NEXT: [[IVAR:%.*]] = bitcast i8* [[T1]] to [[OPAQUE]]**
// CHECK-NEXT: [[T0:%.*]] = bitcast [[OPAQUE]]** [[IVAR]] to i8**
// CHECK-NEXT: [[T1:%.*]] = call i8* @llvm.objc.loadWeakRetained(i8** [[T0]])
// CHECK-NEXT: [[T2:%.*]] = bitcast i8* [[T1]] to [[OPAQUE]]*
// CHECK-NEXT: store [[OPAQUE]]* [[T2]], [[OPAQUE]]** [[X]]
Opaque *x = weak;
// CHECK-NEXT: [[VALUE:%.*]] = load [[OPAQUE]]*, [[OPAQUE]]** [[X]]
// CHECK-NEXT: [[SELF:%.*]] = load [[A]]*, [[A]]** [[SELFVAR]]
// CHECK-NEXT: [[T0:%.*]] = bitcast [[A]]* [[SELF]] to i8*
// CHECK-NEXT: [[T1:%.*]] = getelementptr inbounds i8, i8* [[T0]], i32 8
// CHECK-NEXT: [[IVAR:%.*]] = bitcast i8* [[T1]] to [[OPAQUE]]**
// CHECK-NEXT: [[T0:%.*]] = bitcast [[OPAQUE]]** [[IVAR]] to i8**
// CHECK-NEXT: [[T1:%.*]] = bitcast [[OPAQUE]]* [[VALUE]] to i8*
// CHECK-NEXT: call i8* @llvm.objc.storeWeak(i8** [[T0]], i8* [[T1]])
weak = x;
// CHECK-NEXT: [[T0:%.*]] = bitcast [[OPAQUE]]** [[X]] to i8**
// CHECK-NEXT: call void @llvm.objc.storeStrong(i8** [[T0]], i8* null)
// CHECK-NEXT: ret void
}
// CHECK-LABEL: define internal void @"\01-[A .cxx_destruct]"
// CHECK: [[SELFVAR:%.*]] = alloca [[A]]*, align 4
// CHECK: [[SELF:%.*]] = load [[A]]*, [[A]]** [[SELFVAR]]
// CHECK-NEXT: [[T0:%.*]] = bitcast [[A]]* [[SELF]] to i8*
// CHECK-NEXT: [[T1:%.*]] = getelementptr inbounds i8, i8* [[T0]], i32 8
// CHECK-NEXT: [[IVAR:%.*]] = bitcast i8* [[T1]] to [[OPAQUE]]**
// CHECK-NEXT: [[T0:%.*]] = bitcast [[OPAQUE]]** [[IVAR]] to i8**
// CHECK-NEXT: call void @llvm.objc.destroyWeak(i8** [[T0]])
// CHECK-NEXT: [[T0:%.*]] = bitcast [[A]]* [[SELF]] to i8*
// CHECK-NEXT: [[T1:%.*]] = getelementptr inbounds i8, i8* [[T0]], i32 4
// CHECK-NEXT: [[IVAR:%.*]] = bitcast i8* [[T1]] to [[OPAQUE]]**
// CHECK-NEXT: [[T0:%.*]] = bitcast [[OPAQUE]]** [[IVAR]] to i8**
// CHECK-NEXT: call void @llvm.objc.storeStrong(i8** [[T0]], i8* null)
// CHECK-NEXT: ret void
@end
// Test case for corner case of ivar layout.
@interface B : A {
char _b_flag;
}
@end
@interface C : B {
char _c_flag;
__unsafe_unretained id c_unsafe[5];
id c_strong[4];
__weak id c_weak[3];
id c_strong2[7];
}
@end
@implementation C @end
// Note that these layouts implicitly start at the end of the previous
// class rounded up to pointer alignment.
// GLOBALS-LABEL: @OBJC_METACLASS_C
// Strong layout: skip five, scan four, skip three, scan seven
// 'T' == 0x54, '7' == 0x37
// GLOBALS: @OBJC_CLASS_NAME_{{.*}} = private unnamed_addr constant [3 x i8] c"T7\00"
// Weak layout: skip nine, scan three
// GLOBALS: @OBJC_CLASS_NAME_{{.*}} = private unnamed_addr constant [2 x i8] c"\93\00"
extern void useBlock(void (^block)(void));
// 256 == 0x100 == starts with 1 strong
// GLOBALS: @"__block_descriptor{{.*}} = linkonce_odr hidden {{.*}}, i32 256 }
void testBlockLayoutStrong(id x) {
useBlock(^{ (void) x; });
}
// 1 == 0x001 == starts with 1 weak
// GLOBALS: @"__block_descriptor{{.*}} = linkonce_odr hidden {{.*}}, i32 1 }
void testBlockLayoutWeak(__weak id x) {
useBlock(^{ (void) x; });
}
// CHECK-LABEL: define void @testCatch()
// CHECK: [[X:%.*]] = alloca [[A:%.*]]*, align 4
// CHECK: [[Y:%.*]] = alloca i8*, align 4
// CHECK: call void @objc_exception_try_enter
// CHECK: br i1
// CHECK: call void @checkpoint(i32 0)
// CHECK: call void @objc_exception_try_exit
// CHECK: br label
// CHECK: call void @checkpoint(i32 3)
// CHECK: [[EXN:%.*]] = call i8* @objc_exception_extract
// CHECK: call i32 @objc_exception_match(
// CHECK: br i1
// CHECK: [[T0:%.*]] = bitcast i8* [[EXN]] to [[A]]*
// CHECK: [[T1:%.*]] = bitcast [[A]]* [[T0]] to i8*
// CHECK: [[T2:%.*]] = call i8* @llvm.objc.retain(i8* [[T1]])
// CHECK: [[T3:%.*]] = bitcast i8* [[T2]] to [[A]]*
// CHECK: store [[A]]* [[T3]], [[A]]** [[X]]
// CHECK: call void @checkpoint(i32 1)
// CHECK: [[T0:%.*]] = bitcast [[A]]** [[X]] to i8**
// CHECK: call void @llvm.objc.storeStrong(i8** [[T0]], i8* null)
// CHECK: br label
// CHECK: [[T0:%.*]] = call i8* @llvm.objc.retain(i8* [[EXN]])
// CHECK: store i8* [[T0]], i8** [[Y]]
// CHECK: call void @checkpoint(i32 2)
// CHECK: call void @llvm.objc.storeStrong(i8** [[Y]], i8* null)
extern void checkpoint(int n);
void testCatch() {
@try {
checkpoint(0);
} @catch (A *x) {
checkpoint(1);
} @catch (id y) {
checkpoint(2);
}
checkpoint(3);
}