]> git.saurik.com Git - apple/objc4.git/blob - test/swift-class-def.m
objc4-818.2.tar.gz
[apple/objc4.git] / test / swift-class-def.m
1 #include <sys/cdefs.h>
2
3 #if __LP64__
4 # define PTR " .quad "
5 # define PTRSIZE "8"
6 # define LOGPTRSIZE "3"
7 # define ONLY_LP64(x) x
8 #else
9 # define PTR " .long "
10 # define PTRSIZE "4"
11 # define LOGPTRSIZE "2"
12 # define ONLY_LP64(x)
13 #endif
14
15 #if __has_feature(ptrauth_calls)
16 # define SIGNED_METHOD_LIST_IMP "@AUTH(ia,0,addr) "
17 # define SIGNED_STUB_INITIALIZER "@AUTH(ia,0xc671,addr) "
18 # define SIGNED_METHOD_LIST "@AUTH(da,0xC310,addr) "
19 # define SIGNED_ISA "@AUTH(da, 0x6AE1, addr) "
20 # define SIGNED_SUPER "@AUTH(da, 0xB5AB, addr) "
21 #else
22 # define SIGNED_METHOD_LIST_IMP
23 # define SIGNED_STUB_INITIALIZER
24 # define SIGNED_METHOD_LIST
25 # define SIGNED_ISA
26 # define SIGNED_SUPER
27 #endif
28
29 #define str(x) #x
30 #define str2(x) str(x)
31
32 // Swift metadata initializers. Define these in the test.
33 EXTERN_C Class initSuper(Class cls, void *arg);
34 EXTERN_C Class initSub(Class cls, void *arg);
35
36 @interface SwiftSuper : NSObject @end
37 @interface SwiftSub : SwiftSuper @end
38
39 __BEGIN_DECLS
40 // not id to avoid ARC operations because the class doesn't implement RR methods
41 void* nop(void* self) { return self; }
42 __END_DECLS
43
44 #define SWIFT_CLASS(name, superclass, swiftInit) \
45 asm( \
46 ".globl _OBJC_CLASS_$_" #name "\n" \
47 ".section __DATA,__objc_data \n" \
48 ".align 3 \n" \
49 "_OBJC_CLASS_$_" #name ": \n" \
50 PTR "_OBJC_METACLASS_$_" #name SIGNED_ISA "\n" \
51 PTR "_OBJC_CLASS_$_" #superclass SIGNED_SUPER "\n" \
52 PTR "__objc_empty_cache \n" \
53 PTR "0 \n" \
54 PTR "L_" #name "_ro + 2 \n" \
55 /* Swift class fields. */ \
56 ".long 0 \n" /* flags */ \
57 ".long 0 \n" /* instanceAddressOffset */ \
58 ".long 16 \n" /* instanceSize */ \
59 ".short 15 \n" /* instanceAlignMask */ \
60 ".short 0 \n" /* reserved */ \
61 ".long 256 \n" /* classSize */ \
62 ".long 0 \n" /* classAddressOffset */ \
63 PTR "0 \n" /* description */ \
64 /* pad to OBJC_MAX_CLASS_SIZE */ \
65 PTR "0 \n" \
66 PTR "0 \n" \
67 PTR "0 \n" \
68 PTR "0 \n" \
69 PTR "0 \n" \
70 PTR "0 \n" \
71 PTR "0 \n" \
72 PTR "0 \n" \
73 PTR "0 \n" \
74 PTR "0 \n" \
75 PTR "0 \n" \
76 PTR "0 \n" \
77 PTR "0 \n" \
78 PTR "0 \n" \
79 PTR "0 \n" \
80 PTR "0 \n" \
81 PTR "0 \n" \
82 PTR "0 \n" \
83 PTR "0 \n" \
84 PTR "0 \n" \
85 PTR "0 \n" \
86 PTR "0 \n" \
87 PTR "0 \n" \
88 PTR "0 \n" \
89 \
90 "_OBJC_METACLASS_$_" #name ": \n" \
91 PTR "_OBJC_METACLASS_$_" #superclass SIGNED_ISA "\n" \
92 PTR "_OBJC_METACLASS_$_" #superclass SIGNED_SUPER "\n" \
93 PTR "__objc_empty_cache \n" \
94 PTR "0 \n" \
95 PTR "L_" #name "_meta_ro \n" \
96 /* pad to OBJC_MAX_CLASS_SIZE */ \
97 PTR "0 \n" \
98 PTR "0 \n" \
99 PTR "0 \n" \
100 PTR "0 \n" \
101 PTR "0 \n" \
102 PTR "0 \n" \
103 PTR "0 \n" \
104 PTR "0 \n" \
105 PTR "0 \n" \
106 PTR "0 \n" \
107 PTR "0 \n" \
108 PTR "0 \n" \
109 PTR "0 \n" \
110 PTR "0 \n" \
111 PTR "0 \n" \
112 PTR "0 \n" \
113 PTR "0 \n" \
114 PTR "0 \n" \
115 PTR "0 \n" \
116 PTR "0 \n" \
117 PTR "0 \n" \
118 PTR "0 \n" \
119 PTR "0 \n" \
120 PTR "0 \n" \
121 PTR "0 \n" \
122 PTR "0 \n" \
123 PTR "0 \n" \
124 \
125 "L_" #name "_ro: \n" \
126 ".long (1<<6) \n" \
127 ".long 0 \n" \
128 ".long " PTRSIZE " \n" \
129 ONLY_LP64(".long 0 \n") \
130 PTR "0 \n" \
131 PTR "L_" #name "_name \n" \
132 PTR "L_" #name "_methods" SIGNED_METHOD_LIST "\n" \
133 PTR "0 \n" \
134 PTR "L_" #name "_ivars \n" \
135 PTR "0 \n" \
136 PTR "0 \n" \
137 PTR "_" #swiftInit SIGNED_METHOD_LIST_IMP "\n" \
138 \
139 "L_" #name "_meta_ro: \n" \
140 ".long 1 \n" \
141 ".long 40 \n" \
142 ".long 40 \n" \
143 ONLY_LP64(".long 0 \n") \
144 PTR "0 \n" \
145 PTR "L_" #name "_name \n" \
146 PTR "L_" #name "_meta_methods" SIGNED_METHOD_LIST "\n" \
147 PTR "0 \n" \
148 PTR "0 \n" \
149 PTR "0 \n" \
150 PTR "0 \n" \
151 \
152 "L_" #name "_methods: \n" \
153 "L_" #name "_meta_methods: \n" \
154 ".long 3*" PTRSIZE "\n" \
155 ".long 1 \n" \
156 PTR "L_" #name "_self \n" \
157 PTR "L_" #name "_self \n" \
158 PTR "_nop" SIGNED_METHOD_LIST_IMP "\n" \
159 \
160 "L_" #name "_ivars: \n" \
161 ".long 4*" PTRSIZE " \n" \
162 ".long 1 \n" \
163 PTR "L_" #name "_ivar_offset \n" \
164 PTR "L_" #name "_ivar_name \n" \
165 PTR "L_" #name "_ivar_type \n" \
166 ".long " LOGPTRSIZE "\n" \
167 ".long " PTRSIZE "\n" \
168 \
169 "L_" #name "_ivar_offset: \n" \
170 ".long 0 \n" \
171 \
172 ".cstring \n" \
173 "L_" #name "_name: .ascii \"" #name "\\0\" \n" \
174 "L_" #name "_self: .ascii \"self\\0\" \n" \
175 "L_" #name "_ivar_name: " \
176 " .ascii \"" #name "_ivar\\0\" \n" \
177 "L_" #name "_ivar_type: .ascii \"c\\0\" \n" \
178 \
179 \
180 ".text \n" \
181 ); \
182 extern char OBJC_CLASS_$_ ## name; \
183 Class Raw ## name = (Class)&OBJC_CLASS_$_ ## name
184
185 #define SWIFT_STUB_CLASSREF(name) \
186 extern char OBJC_CLASS_$_ ## name; \
187 static Class name ## Classref = (Class)(&OBJC_CLASS_$_ ## name + 1)
188
189 #define SWIFT_STUB_CLASS(name, initializer) \
190 asm( \
191 ".globl _OBJC_CLASS_$_" #name "\n" \
192 ".section __DATA,__objc_data \n" \
193 ".align 3 \n" \
194 "_dummy" #name ": \n" \
195 PTR "0 \n" \
196 ".alt_entry _OBJC_CLASS_$_" #name "\n" \
197 "_OBJC_CLASS_$_" #name ": \n" \
198 PTR "1 \n" \
199 PTR "_" #initializer SIGNED_STUB_INITIALIZER "\n" \
200 ".text" \
201 ); \
202 extern char OBJC_CLASS_$_ ## name; \
203 Class Raw ## name = (Class)&OBJC_CLASS_$_ ## name; \
204 SWIFT_STUB_CLASSREF(name)
205
206
207 void fn(void) { }