1//===------- CGObjCGNU.cpp - Emit LLVM Code from ASTs for a Module --------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This provides Objective-C code generation targeting the GNU runtime. The
10// class in this file generates structures used by the GNU Objective-C runtime
11// library. These structures are defined in objc/objc.h and objc/objc-api.h in
12// the GNU runtime distribution.
13//
14//===----------------------------------------------------------------------===//
15
16#include "CGCXXABI.h"
17#include "CGCleanup.h"
18#include "CGObjCRuntime.h"
19#include "CodeGenFunction.h"
20#include "CodeGenModule.h"
21#include "clang/AST/ASTContext.h"
22#include "clang/AST/Attr.h"
23#include "clang/AST/Decl.h"
24#include "clang/AST/DeclObjC.h"
25#include "clang/AST/RecordLayout.h"
26#include "clang/AST/StmtObjC.h"
27#include "clang/Basic/FileManager.h"
28#include "clang/Basic/SourceManager.h"
29#include "clang/CodeGen/ConstantInitBuilder.h"
30#include "llvm/ADT/SmallVector.h"
31#include "llvm/ADT/StringMap.h"
32#include "llvm/IR/DataLayout.h"
33#include "llvm/IR/Intrinsics.h"
34#include "llvm/IR/LLVMContext.h"
35#include "llvm/IR/Module.h"
36#include "llvm/Support/Compiler.h"
37#include "llvm/Support/ConvertUTF.h"
38#include <cctype>
39
40using namespace clang;
41using namespace CodeGen;
42
43namespace {
44
45/// Class that lazily initialises the runtime function. Avoids inserting the
46/// types and the function declaration into a module if they're not used, and
47/// avoids constructing the type more than once if it's used more than once.
48class LazyRuntimeFunction {
49 CodeGenModule *CGM;
50 llvm::FunctionType *FTy;
51 const char *FunctionName;
52 llvm::FunctionCallee Function;
53
54public:
55 /// Constructor leaves this class uninitialized, because it is intended to
56 /// be used as a field in another class and not all of the types that are
57 /// used as arguments will necessarily be available at construction time.
58 LazyRuntimeFunction()
59 : CGM(nullptr), FunctionName(nullptr), Function(nullptr) {}
60
61 /// Initialises the lazy function with the name, return type, and the types
62 /// of the arguments.
63 template <typename... Tys>
64 void init(CodeGenModule *Mod, const char *name, llvm::Type *RetTy,
65 Tys *... Types) {
66 CGM = Mod;
67 FunctionName = name;
68 Function = nullptr;
69 if(sizeof...(Tys)) {
70 SmallVector<llvm::Type *, 8> ArgTys({Types...});
71 FTy = llvm::FunctionType::get(RetTy, ArgTys, false);
72 }
73 else {
74 FTy = llvm::FunctionType::get(RetTy, None, false);
75 }
76 }
77
78 llvm::FunctionType *getType() { return FTy; }
79
80 /// Overloaded cast operator, allows the class to be implicitly cast to an
81 /// LLVM constant.
82 operator llvm::FunctionCallee() {
83 if (!Function) {
84 if (!FunctionName)
85 return nullptr;
86 Function = CGM->CreateRuntimeFunction(FTy, FunctionName);
87 }
88 return Function;
89 }
90};
91
92
93/// GNU Objective-C runtime code generation. This class implements the parts of
94/// Objective-C support that are specific to the GNU family of runtimes (GCC,
95/// GNUstep and ObjFW).
96class CGObjCGNU : public CGObjCRuntime {
97protected:
98 /// The LLVM module into which output is inserted
99 llvm::Module &TheModule;
100 /// strut objc_super. Used for sending messages to super. This structure
101 /// contains the receiver (object) and the expected class.
102 llvm::StructType *ObjCSuperTy;
103 /// struct objc_super*. The type of the argument to the superclass message
104 /// lookup functions.
105 llvm::PointerType *PtrToObjCSuperTy;
106 /// LLVM type for selectors. Opaque pointer (i8*) unless a header declaring
107 /// SEL is included in a header somewhere, in which case it will be whatever
108 /// type is declared in that header, most likely {i8*, i8*}.
109 llvm::PointerType *SelectorTy;
110 /// LLVM i8 type. Cached here to avoid repeatedly getting it in all of the
111 /// places where it's used
112 llvm::IntegerType *Int8Ty;
113 /// Pointer to i8 - LLVM type of char*, for all of the places where the
114 /// runtime needs to deal with C strings.
115 llvm::PointerType *PtrToInt8Ty;
116 /// struct objc_protocol type
117 llvm::StructType *ProtocolTy;
118 /// Protocol * type.
119 llvm::PointerType *ProtocolPtrTy;
120 /// Instance Method Pointer type. This is a pointer to a function that takes,
121 /// at a minimum, an object and a selector, and is the generic type for
122 /// Objective-C methods. Due to differences between variadic / non-variadic
123 /// calling conventions, it must always be cast to the correct type before
124 /// actually being used.
125 llvm::PointerType *IMPTy;
126 /// Type of an untyped Objective-C object. Clang treats id as a built-in type
127 /// when compiling Objective-C code, so this may be an opaque pointer (i8*),
128 /// but if the runtime header declaring it is included then it may be a
129 /// pointer to a structure.
130 llvm::PointerType *IdTy;
131 /// Pointer to a pointer to an Objective-C object. Used in the new ABI
132 /// message lookup function and some GC-related functions.
133 llvm::PointerType *PtrToIdTy;
134 /// The clang type of id. Used when using the clang CGCall infrastructure to
135 /// call Objective-C methods.
136 CanQualType ASTIdTy;
137 /// LLVM type for C int type.
138 llvm::IntegerType *IntTy;
139 /// LLVM type for an opaque pointer. This is identical to PtrToInt8Ty, but is
140 /// used in the code to document the difference between i8* meaning a pointer
141 /// to a C string and i8* meaning a pointer to some opaque type.
142 llvm::PointerType *PtrTy;
143 /// LLVM type for C long type. The runtime uses this in a lot of places where
144 /// it should be using intptr_t, but we can't fix this without breaking
145 /// compatibility with GCC...
146 llvm::IntegerType *LongTy;
147 /// LLVM type for C size_t. Used in various runtime data structures.
148 llvm::IntegerType *SizeTy;
149 /// LLVM type for C intptr_t.
150 llvm::IntegerType *IntPtrTy;
151 /// LLVM type for C ptrdiff_t. Mainly used in property accessor functions.
152 llvm::IntegerType *PtrDiffTy;
153 /// LLVM type for C int*. Used for GCC-ABI-compatible non-fragile instance
154 /// variables.
155 llvm::PointerType *PtrToIntTy;
156 /// LLVM type for Objective-C BOOL type.
157 llvm::Type *BoolTy;
158 /// 32-bit integer type, to save us needing to look it up every time it's used.
159 llvm::IntegerType *Int32Ty;
160 /// 64-bit integer type, to save us needing to look it up every time it's used.
161 llvm::IntegerType *Int64Ty;
162 /// The type of struct objc_property.
163 llvm::StructType *PropertyMetadataTy;
164 /// Metadata kind used to tie method lookups to message sends. The GNUstep
165 /// runtime provides some LLVM passes that can use this to do things like
166 /// automatic IMP caching and speculative inlining.
167 unsigned msgSendMDKind;
168 /// Does the current target use SEH-based exceptions? False implies
169 /// Itanium-style DWARF unwinding.
170 bool usesSEHExceptions;
171
172 /// Helper to check if we are targeting a specific runtime version or later.
173 bool isRuntime(ObjCRuntime::Kind kind, unsigned major, unsigned minor=0) {
174 const ObjCRuntime &R = CGM.getLangOpts().ObjCRuntime;
175 return (R.getKind() == kind) &&
176 (R.getVersion() >= VersionTuple(major, minor));
177 }
178
179 std::string ManglePublicSymbol(StringRef Name) {
180 return (StringRef(CGM.getTriple().isOSBinFormatCOFF() ? "$_" : "._") + Name).str();
181 }
182
183 std::string SymbolForProtocol(Twine Name) {
184 return (ManglePublicSymbol("OBJC_PROTOCOL_") + Name).str();
185 }
186
187 std::string SymbolForProtocolRef(StringRef Name) {
188 return (ManglePublicSymbol("OBJC_REF_PROTOCOL_") + Name).str();
189 }
190
191
192 /// Helper function that generates a constant string and returns a pointer to
193 /// the start of the string. The result of this function can be used anywhere
194 /// where the C code specifies const char*.
195 llvm::Constant *MakeConstantString(StringRef Str, const char *Name = "") {
196 ConstantAddress Array =
197 CGM.GetAddrOfConstantCString(std::string(Str), Name);
198 return llvm::ConstantExpr::getGetElementPtr(Array.getElementType(),
199 Array.getPointer(), Zeros);
200 }
201
202 /// Emits a linkonce_odr string, whose name is the prefix followed by the
203 /// string value. This allows the linker to combine the strings between
204 /// different modules. Used for EH typeinfo names, selector strings, and a
205 /// few other things.
206 llvm::Constant *ExportUniqueString(const std::string &Str,
207 const std::string &prefix,
208 bool Private=false) {
209 std::string name = prefix + Str;
210 auto *ConstStr = TheModule.getGlobalVariable(name);
211 if (!ConstStr) {
212 llvm::Constant *value = llvm::ConstantDataArray::getString(VMContext,Str);
213 auto *GV = new llvm::GlobalVariable(TheModule, value->getType(), true,
214 llvm::GlobalValue::LinkOnceODRLinkage, value, name);
215 GV->setComdat(TheModule.getOrInsertComdat(name));
216 if (Private)
217 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
218 ConstStr = GV;
219 }
220 return llvm::ConstantExpr::getGetElementPtr(ConstStr->getValueType(),
221 ConstStr, Zeros);
222 }
223
224 /// Returns a property name and encoding string.
225 llvm::Constant *MakePropertyEncodingString(const ObjCPropertyDecl *PD,
226 const Decl *Container) {
227 assert(!isRuntime(ObjCRuntime::GNUstep, 2));
228 if (isRuntime(ObjCRuntime::GNUstep, 1, 6)) {
229 std::string NameAndAttributes;
230 std::string TypeStr =
231 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container);
232 NameAndAttributes += '\0';
233 NameAndAttributes += TypeStr.length() + 3;
234 NameAndAttributes += TypeStr;
235 NameAndAttributes += '\0';
236 NameAndAttributes += PD->getNameAsString();
237 return MakeConstantString(NameAndAttributes);
238 }
239 return MakeConstantString(PD->getNameAsString());
240 }
241
242 /// Push the property attributes into two structure fields.
243 void PushPropertyAttributes(ConstantStructBuilder &Fields,
244 const ObjCPropertyDecl *property, bool isSynthesized=true, bool
245 isDynamic=true) {
246 int attrs = property->getPropertyAttributes();
247 // For read-only properties, clear the copy and retain flags
248 if (attrs & ObjCPropertyAttribute::kind_readonly) {
249 attrs &= ~ObjCPropertyAttribute::kind_copy;
250 attrs &= ~ObjCPropertyAttribute::kind_retain;
251 attrs &= ~ObjCPropertyAttribute::kind_weak;
252 attrs &= ~ObjCPropertyAttribute::kind_strong;
253 }
254 // The first flags field has the same attribute values as clang uses internally
255 Fields.addInt(Int8Ty, attrs & 0xff);
256 attrs >>= 8;
257 attrs <<= 2;
258 // For protocol properties, synthesized and dynamic have no meaning, so we
259 // reuse these flags to indicate that this is a protocol property (both set
260 // has no meaning, as a property can't be both synthesized and dynamic)
261 attrs |= isSynthesized ? (1<<0) : 0;
262 attrs |= isDynamic ? (1<<1) : 0;
263 // The second field is the next four fields left shifted by two, with the
264 // low bit set to indicate whether the field is synthesized or dynamic.
265 Fields.addInt(Int8Ty, attrs & 0xff);
266 // Two padding fields
267 Fields.addInt(Int8Ty, 0);
268 Fields.addInt(Int8Ty, 0);
269 }
270
271 virtual llvm::Constant *GenerateCategoryProtocolList(const
272 ObjCCategoryDecl *OCD);
273 virtual ConstantArrayBuilder PushPropertyListHeader(ConstantStructBuilder &Fields,
274 int count) {
275 // int count;
276 Fields.addInt(IntTy, count);
277 // int size; (only in GNUstep v2 ABI.
278 if (isRuntime(ObjCRuntime::GNUstep, 2)) {
279 llvm::DataLayout td(&TheModule);
280 Fields.addInt(IntTy, td.getTypeSizeInBits(PropertyMetadataTy) /
281 CGM.getContext().getCharWidth());
282 }
283 // struct objc_property_list *next;
284 Fields.add(NULLPtr);
285 // struct objc_property properties[]
286 return Fields.beginArray(PropertyMetadataTy);
287 }
288 virtual void PushProperty(ConstantArrayBuilder &PropertiesArray,
289 const ObjCPropertyDecl *property,
290 const Decl *OCD,
291 bool isSynthesized=true, bool
292 isDynamic=true) {
293 auto Fields = PropertiesArray.beginStruct(PropertyMetadataTy);
294 ASTContext &Context = CGM.getContext();
295 Fields.add(MakePropertyEncodingString(property, OCD));
296 PushPropertyAttributes(Fields, property, isSynthesized, isDynamic);
297 auto addPropertyMethod = [&](const ObjCMethodDecl *accessor) {
298 if (accessor) {
299 std::string TypeStr = Context.getObjCEncodingForMethodDecl(accessor);
300 llvm::Constant *TypeEncoding = MakeConstantString(TypeStr);
301 Fields.add(MakeConstantString(accessor->getSelector().getAsString()));
302 Fields.add(TypeEncoding);
303 } else {
304 Fields.add(NULLPtr);
305 Fields.add(NULLPtr);
306 }
307 };
308 addPropertyMethod(property->getGetterMethodDecl());
309 addPropertyMethod(property->getSetterMethodDecl());
310 Fields.finishAndAddTo(PropertiesArray);
311 }
312
313 /// Ensures that the value has the required type, by inserting a bitcast if
314 /// required. This function lets us avoid inserting bitcasts that are
315 /// redundant.
316 llvm::Value* EnforceType(CGBuilderTy &B, llvm::Value *V, llvm::Type *Ty) {
317 if (V->getType() == Ty) return V;
318 return B.CreateBitCast(V, Ty);
319 }
320 Address EnforceType(CGBuilderTy &B, Address V, llvm::Type *Ty) {
321 if (V.getType() == Ty) return V;
322 return B.CreateBitCast(V, Ty);
323 }
324
325 // Some zeros used for GEPs in lots of places.
326 llvm::Constant *Zeros[2];
327 /// Null pointer value. Mainly used as a terminator in various arrays.
328 llvm::Constant *NULLPtr;
329 /// LLVM context.
330 llvm::LLVMContext &VMContext;
331
332protected:
333
334 /// Placeholder for the class. Lots of things refer to the class before we've
335 /// actually emitted it. We use this alias as a placeholder, and then replace
336 /// it with a pointer to the class structure before finally emitting the
337 /// module.
338 llvm::GlobalAlias *ClassPtrAlias;
339 /// Placeholder for the metaclass. Lots of things refer to the class before
340 /// we've / actually emitted it. We use this alias as a placeholder, and then
341 /// replace / it with a pointer to the metaclass structure before finally
342 /// emitting the / module.
343 llvm::GlobalAlias *MetaClassPtrAlias;
344 /// All of the classes that have been generated for this compilation units.
345 std::vector<llvm::Constant*> Classes;
346 /// All of the categories that have been generated for this compilation units.
347 std::vector<llvm::Constant*> Categories;
348 /// All of the Objective-C constant strings that have been generated for this
349 /// compilation units.
350 std::vector<llvm::Constant*> ConstantStrings;
351 /// Map from string values to Objective-C constant strings in the output.
352 /// Used to prevent emitting Objective-C strings more than once. This should
353 /// not be required at all - CodeGenModule should manage this list.
354 llvm::StringMap<llvm::Constant*> ObjCStrings;
355 /// All of the protocols that have been declared.
356 llvm::StringMap<llvm::Constant*> ExistingProtocols;
357 /// For each variant of a selector, we store the type encoding and a
358 /// placeholder value. For an untyped selector, the type will be the empty
359 /// string. Selector references are all done via the module's selector table,
360 /// so we create an alias as a placeholder and then replace it with the real
361 /// value later.
362 typedef std::pair<std::string, llvm::GlobalAlias*> TypedSelector;
363 /// Type of the selector map. This is roughly equivalent to the structure
364 /// used in the GNUstep runtime, which maintains a list of all of the valid
365 /// types for a selector in a table.
366 typedef llvm::DenseMap<Selector, SmallVector<TypedSelector, 2> >
367 SelectorMap;
368 /// A map from selectors to selector types. This allows us to emit all
369 /// selectors of the same name and type together.
370 SelectorMap SelectorTable;
371
372 /// Selectors related to memory management. When compiling in GC mode, we
373 /// omit these.
374 Selector RetainSel, ReleaseSel, AutoreleaseSel;
375 /// Runtime functions used for memory management in GC mode. Note that clang
376 /// supports code generation for calling these functions, but neither GNU
377 /// runtime actually supports this API properly yet.
378 LazyRuntimeFunction IvarAssignFn, StrongCastAssignFn, MemMoveFn, WeakReadFn,
379 WeakAssignFn, GlobalAssignFn;
380
381 typedef std::pair<std::string, std::string> ClassAliasPair;
382 /// All classes that have aliases set for them.
383 std::vector<ClassAliasPair> ClassAliases;
384
385protected:
386 /// Function used for throwing Objective-C exceptions.
387 LazyRuntimeFunction ExceptionThrowFn;
388 /// Function used for rethrowing exceptions, used at the end of \@finally or
389 /// \@synchronize blocks.
390 LazyRuntimeFunction ExceptionReThrowFn;
391 /// Function called when entering a catch function. This is required for
392 /// differentiating Objective-C exceptions and foreign exceptions.
393 LazyRuntimeFunction EnterCatchFn;
394 /// Function called when exiting from a catch block. Used to do exception
395 /// cleanup.
396 LazyRuntimeFunction ExitCatchFn;
397 /// Function called when entering an \@synchronize block. Acquires the lock.
398 LazyRuntimeFunction SyncEnterFn;
399 /// Function called when exiting an \@synchronize block. Releases the lock.
400 LazyRuntimeFunction SyncExitFn;
401
402private:
403 /// Function called if fast enumeration detects that the collection is
404 /// modified during the update.
405 LazyRuntimeFunction EnumerationMutationFn;
406 /// Function for implementing synthesized property getters that return an
407 /// object.
408 LazyRuntimeFunction GetPropertyFn;
409 /// Function for implementing synthesized property setters that return an
410 /// object.
411 LazyRuntimeFunction SetPropertyFn;
412 /// Function used for non-object declared property getters.
413 LazyRuntimeFunction GetStructPropertyFn;
414 /// Function used for non-object declared property setters.
415 LazyRuntimeFunction SetStructPropertyFn;
416
417protected:
418 /// The version of the runtime that this class targets. Must match the
419 /// version in the runtime.
420 int RuntimeVersion;
421 /// The version of the protocol class. Used to differentiate between ObjC1
422 /// and ObjC2 protocols. Objective-C 1 protocols can not contain optional
423 /// components and can not contain declared properties. We always emit
424 /// Objective-C 2 property structures, but we have to pretend that they're
425 /// Objective-C 1 property structures when targeting the GCC runtime or it
426 /// will abort.
427 const int ProtocolVersion;
428 /// The version of the class ABI. This value is used in the class structure
429 /// and indicates how various fields should be interpreted.
430 const int ClassABIVersion;
431 /// Generates an instance variable list structure. This is a structure
432 /// containing a size and an array of structures containing instance variable
433 /// metadata. This is used purely for introspection in the fragile ABI. In
434 /// the non-fragile ABI, it's used for instance variable fixup.
435 virtual llvm::Constant *GenerateIvarList(ArrayRef<llvm::Constant *> IvarNames,
436 ArrayRef<llvm::Constant *> IvarTypes,
437 ArrayRef<llvm::Constant *> IvarOffsets,
438 ArrayRef<llvm::Constant *> IvarAlign,
439 ArrayRef<Qualifiers::ObjCLifetime> IvarOwnership);
440
441 /// Generates a method list structure. This is a structure containing a size
442 /// and an array of structures containing method metadata.
443 ///
444 /// This structure is used by both classes and categories, and contains a next
445 /// pointer allowing them to be chained together in a linked list.
446 llvm::Constant *GenerateMethodList(StringRef ClassName,
447 StringRef CategoryName,
448 ArrayRef<const ObjCMethodDecl*> Methods,
449 bool isClassMethodList);
450
451 /// Emits an empty protocol. This is used for \@protocol() where no protocol
452 /// is found. The runtime will (hopefully) fix up the pointer to refer to the
453 /// real protocol.
454 virtual llvm::Constant *GenerateEmptyProtocol(StringRef ProtocolName);
455
456 /// Generates a list of property metadata structures. This follows the same
457 /// pattern as method and instance variable metadata lists.
458 llvm::Constant *GeneratePropertyList(const Decl *Container,
459 const ObjCContainerDecl *OCD,
460 bool isClassProperty=false,
461 bool protocolOptionalProperties=false);
462
463 /// Generates a list of referenced protocols. Classes, categories, and
464 /// protocols all use this structure.
465 llvm::Constant *GenerateProtocolList(ArrayRef<std::string> Protocols);
466
467 /// To ensure that all protocols are seen by the runtime, we add a category on
468 /// a class defined in the runtime, declaring no methods, but adopting the
469 /// protocols. This is a horribly ugly hack, but it allows us to collect all
470 /// of the protocols without changing the ABI.
471 void GenerateProtocolHolderCategory();
472
473 /// Generates a class structure.
474 llvm::Constant *GenerateClassStructure(
475 llvm::Constant *MetaClass,
476 llvm::Constant *SuperClass,
477 unsigned info,
478 const char *Name,
479 llvm::Constant *Version,
480 llvm::Constant *InstanceSize,
481 llvm::Constant *IVars,
482 llvm::Constant *Methods,
483 llvm::Constant *Protocols,
484 llvm::Constant *IvarOffsets,
485 llvm::Constant *Properties,
486 llvm::Constant *StrongIvarBitmap,
487 llvm::Constant *WeakIvarBitmap,
488 bool isMeta=false);
489
490 /// Generates a method list. This is used by protocols to define the required
491 /// and optional methods.
492 virtual llvm::Constant *GenerateProtocolMethodList(
493 ArrayRef<const ObjCMethodDecl*> Methods);
494 /// Emits optional and required method lists.
495 template<class T>
496 void EmitProtocolMethodList(T &&Methods, llvm::Constant *&Required,
497 llvm::Constant *&Optional) {
498 SmallVector<const ObjCMethodDecl*, 16> RequiredMethods;
499 SmallVector<const ObjCMethodDecl*, 16> OptionalMethods;
500 for (const auto *I : Methods)
501 if (I->isOptional())
502 OptionalMethods.push_back(I);
503 else
504 RequiredMethods.push_back(I);
505 Required = GenerateProtocolMethodList(RequiredMethods);
506 Optional = GenerateProtocolMethodList(OptionalMethods);
507 }
508
509 /// Returns a selector with the specified type encoding. An empty string is
510 /// used to return an untyped selector (with the types field set to NULL).
511 virtual llvm::Value *GetTypedSelector(CodeGenFunction &CGF, Selector Sel,
512 const std::string &TypeEncoding);
513
514 /// Returns the name of ivar offset variables. In the GNUstep v1 ABI, this
515 /// contains the class and ivar names, in the v2 ABI this contains the type
516 /// encoding as well.
517 virtual std::string GetIVarOffsetVariableName(const ObjCInterfaceDecl *ID,
518 const ObjCIvarDecl *Ivar) {
519 const std::string Name = "__objc_ivar_offset_" + ID->getNameAsString()
520 + '.' + Ivar->getNameAsString();
521 return Name;
522 }
523 /// Returns the variable used to store the offset of an instance variable.
524 llvm::GlobalVariable *ObjCIvarOffsetVariable(const ObjCInterfaceDecl *ID,
525 const ObjCIvarDecl *Ivar);
526 /// Emits a reference to a class. This allows the linker to object if there
527 /// is no class of the matching name.
528 void EmitClassRef(const std::string &className);
529
530 /// Emits a pointer to the named class
531 virtual llvm::Value *GetClassNamed(CodeGenFunction &CGF,
532 const std::string &Name, bool isWeak);
533
534 /// Looks up the method for sending a message to the specified object. This
535 /// mechanism differs between the GCC and GNU runtimes, so this method must be
536 /// overridden in subclasses.
537 virtual llvm::Value *LookupIMP(CodeGenFunction &CGF,
538 llvm::Value *&Receiver,
539 llvm::Value *cmd,
540 llvm::MDNode *node,
541 MessageSendInfo &MSI) = 0;
542
543 /// Looks up the method for sending a message to a superclass. This
544 /// mechanism differs between the GCC and GNU runtimes, so this method must
545 /// be overridden in subclasses.
546 virtual llvm::Value *LookupIMPSuper(CodeGenFunction &CGF,
547 Address ObjCSuper,
548 llvm::Value *cmd,
549 MessageSendInfo &MSI) = 0;
550
551 /// Libobjc2 uses a bitfield representation where small(ish) bitfields are
552 /// stored in a 64-bit value with the low bit set to 1 and the remaining 63
553 /// bits set to their values, LSB first, while larger ones are stored in a
554 /// structure of this / form:
555 ///
556 /// struct { int32_t length; int32_t values[length]; };
557 ///
558 /// The values in the array are stored in host-endian format, with the least
559 /// significant bit being assumed to come first in the bitfield. Therefore,
560 /// a bitfield with the 64th bit set will be (int64_t)&{ 2, [0, 1<<31] },
561 /// while a bitfield / with the 63rd bit set will be 1<<64.
562 llvm::Constant *MakeBitField(ArrayRef<bool> bits);
563
564public:
565 CGObjCGNU(CodeGenModule &cgm, unsigned runtimeABIVersion,
566 unsigned protocolClassVersion, unsigned classABI=1);
567
568 ConstantAddress GenerateConstantString(const StringLiteral *) override;
569
570 RValue
571 GenerateMessageSend(CodeGenFunction &CGF, ReturnValueSlot Return,
572 QualType ResultType, Selector Sel,
573 llvm::Value *Receiver, const CallArgList &CallArgs,
574 const ObjCInterfaceDecl *Class,
575 const ObjCMethodDecl *Method) override;
576 RValue
577 GenerateMessageSendSuper(CodeGenFunction &CGF, ReturnValueSlot Return,
578 QualType ResultType, Selector Sel,
579 const ObjCInterfaceDecl *Class,
580 bool isCategoryImpl, llvm::Value *Receiver,
581 bool IsClassMessage, const CallArgList &CallArgs,
582 const ObjCMethodDecl *Method) override;
583 llvm::Value *GetClass(CodeGenFunction &CGF,
584 const ObjCInterfaceDecl *OID) override;
585 llvm::Value *GetSelector(CodeGenFunction &CGF, Selector Sel) override;
586 Address GetAddrOfSelector(CodeGenFunction &CGF, Selector Sel) override;
587 llvm::Value *GetSelector(CodeGenFunction &CGF,
588 const ObjCMethodDecl *Method) override;
589 virtual llvm::Constant *GetConstantSelector(Selector Sel,
590 const std::string &TypeEncoding) {
591 llvm_unreachable("Runtime unable to generate constant selector");
592 }
593 llvm::Constant *GetConstantSelector(const ObjCMethodDecl *M) {
594 return GetConstantSelector(M->getSelector(),
595 CGM.getContext().getObjCEncodingForMethodDecl(M));
596 }
597 llvm::Constant *GetEHType(QualType T) override;
598
599 llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
600 const ObjCContainerDecl *CD) override;
601 void GenerateDirectMethodPrologue(CodeGenFunction &CGF, llvm::Function *Fn,
602 const ObjCMethodDecl *OMD,
603 const ObjCContainerDecl *CD) override;
604 void GenerateCategory(const ObjCCategoryImplDecl *CMD) override;
605 void GenerateClass(const ObjCImplementationDecl *ClassDecl) override;
606 void RegisterAlias(const ObjCCompatibleAliasDecl *OAD) override;
607 llvm::Value *GenerateProtocolRef(CodeGenFunction &CGF,
608 const ObjCProtocolDecl *PD) override;
609 void GenerateProtocol(const ObjCProtocolDecl *PD) override;
610
611 virtual llvm::Constant *GenerateProtocolRef(const ObjCProtocolDecl *PD);
612
613 llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD) override {
614 return GenerateProtocolRef(PD);
615 }
616
617 llvm::Function *ModuleInitFunction() override;
618 llvm::FunctionCallee GetPropertyGetFunction() override;
619 llvm::FunctionCallee GetPropertySetFunction() override;
620 llvm::FunctionCallee GetOptimizedPropertySetFunction(bool atomic,
621 bool copy) override;
622 llvm::FunctionCallee GetSetStructFunction() override;
623 llvm::FunctionCallee GetGetStructFunction() override;
624 llvm::FunctionCallee GetCppAtomicObjectGetFunction() override;
625 llvm::FunctionCallee GetCppAtomicObjectSetFunction() override;
626 llvm::FunctionCallee EnumerationMutationFunction() override;
627
628 void EmitTryStmt(CodeGenFunction &CGF,
629 const ObjCAtTryStmt &S) override;
630 void EmitSynchronizedStmt(CodeGenFunction &CGF,
631 const ObjCAtSynchronizedStmt &S) override;
632 void EmitThrowStmt(CodeGenFunction &CGF,
633 const ObjCAtThrowStmt &S,
634 bool ClearInsertionPoint=true) override;
635 llvm::Value * EmitObjCWeakRead(CodeGenFunction &CGF,
636 Address AddrWeakObj) override;
637 void EmitObjCWeakAssign(CodeGenFunction &CGF,
638 llvm::Value *src, Address dst) override;
639 void EmitObjCGlobalAssign(CodeGenFunction &CGF,
640 llvm::Value *src, Address dest,
641 bool threadlocal=false) override;
642 void EmitObjCIvarAssign(CodeGenFunction &CGF, llvm::Value *src,
643 Address dest, llvm::Value *ivarOffset) override;
644 void EmitObjCStrongCastAssign(CodeGenFunction &CGF,
645 llvm::Value *src, Address dest) override;
646 void EmitGCMemmoveCollectable(CodeGenFunction &CGF, Address DestPtr,
647 Address SrcPtr,
648 llvm::Value *Size) override;
649 LValue EmitObjCValueForIvar(CodeGenFunction &CGF, QualType ObjectTy,
650 llvm::Value *BaseValue, const ObjCIvarDecl *Ivar,
651 unsigned CVRQualifiers) override;
652 llvm::Value *EmitIvarOffset(CodeGenFunction &CGF,
653 const ObjCInterfaceDecl *Interface,
654 const ObjCIvarDecl *Ivar) override;
655 llvm::Value *EmitNSAutoreleasePoolClassRef(CodeGenFunction &CGF) override;
656 llvm::Constant *BuildGCBlockLayout(CodeGenModule &CGM,
657 const CGBlockInfo &blockInfo) override {
658 return NULLPtr;
659 }
660 llvm::Constant *BuildRCBlockLayout(CodeGenModule &CGM,
661 const CGBlockInfo &blockInfo) override {
662 return NULLPtr;
663 }
664
665 llvm::Constant *BuildByrefLayout(CodeGenModule &CGM, QualType T) override {
666 return NULLPtr;
667 }
668};
669
670/// Class representing the legacy GCC Objective-C ABI. This is the default when
671/// -fobjc-nonfragile-abi is not specified.
672///
673/// The GCC ABI target actually generates code that is approximately compatible
674/// with the new GNUstep runtime ABI, but refrains from using any features that
675/// would not work with the GCC runtime. For example, clang always generates
676/// the extended form of the class structure, and the extra fields are simply
677/// ignored by GCC libobjc.
678class CGObjCGCC : public CGObjCGNU {
679 /// The GCC ABI message lookup function. Returns an IMP pointing to the
680 /// method implementation for this message.
681 LazyRuntimeFunction MsgLookupFn;
682 /// The GCC ABI superclass message lookup function. Takes a pointer to a
683 /// structure describing the receiver and the class, and a selector as
684 /// arguments. Returns the IMP for the corresponding method.
685 LazyRuntimeFunction MsgLookupSuperFn;
686
687protected:
688 llvm::Value *LookupIMP(CodeGenFunction &CGF, llvm::Value *&Receiver,
689 llvm::Value *cmd, llvm::MDNode *node,
690 MessageSendInfo &MSI) override {
691 CGBuilderTy &Builder = CGF.Builder;
692 llvm::Value *args[] = {
693 EnforceType(Builder, Receiver, IdTy),
694 EnforceType(Builder, cmd, SelectorTy) };
695 llvm::CallBase *imp = CGF.EmitRuntimeCallOrInvoke(MsgLookupFn, args);
696 imp->setMetadata(msgSendMDKind, node);
697 return imp;
698 }
699
700 llvm::Value *LookupIMPSuper(CodeGenFunction &CGF, Address ObjCSuper,
701 llvm::Value *cmd, MessageSendInfo &MSI) override {
702 CGBuilderTy &Builder = CGF.Builder;
703 llvm::Value *lookupArgs[] = {EnforceType(Builder, ObjCSuper,
704 PtrToObjCSuperTy).getPointer(), cmd};
705 return CGF.EmitNounwindRuntimeCall(MsgLookupSuperFn, lookupArgs);
706 }
707
708public:
709 CGObjCGCC(CodeGenModule &Mod) : CGObjCGNU(Mod, 8, 2) {
710 // IMP objc_msg_lookup(id, SEL);
711 MsgLookupFn.init(&CGM, "objc_msg_lookup", IMPTy, IdTy, SelectorTy);
712 // IMP objc_msg_lookup_super(struct objc_super*, SEL);
713 MsgLookupSuperFn.init(&CGM, "objc_msg_lookup_super", IMPTy,
714 PtrToObjCSuperTy, SelectorTy);
715 }
716};
717
718/// Class used when targeting the new GNUstep runtime ABI.
719class CGObjCGNUstep : public CGObjCGNU {
720 /// The slot lookup function. Returns a pointer to a cacheable structure
721 /// that contains (among other things) the IMP.
722 LazyRuntimeFunction SlotLookupFn;
723 /// The GNUstep ABI superclass message lookup function. Takes a pointer to
724 /// a structure describing the receiver and the class, and a selector as
725 /// arguments. Returns the slot for the corresponding method. Superclass
726 /// message lookup rarely changes, so this is a good caching opportunity.
727 LazyRuntimeFunction SlotLookupSuperFn;
728 /// Specialised function for setting atomic retain properties
729 LazyRuntimeFunction SetPropertyAtomic;
730 /// Specialised function for setting atomic copy properties
731 LazyRuntimeFunction SetPropertyAtomicCopy;
732 /// Specialised function for setting nonatomic retain properties
733 LazyRuntimeFunction SetPropertyNonAtomic;
734 /// Specialised function for setting nonatomic copy properties
735 LazyRuntimeFunction SetPropertyNonAtomicCopy;
736 /// Function to perform atomic copies of C++ objects with nontrivial copy
737 /// constructors from Objective-C ivars.
738 LazyRuntimeFunction CxxAtomicObjectGetFn;
739 /// Function to perform atomic copies of C++ objects with nontrivial copy
740 /// constructors to Objective-C ivars.
741 LazyRuntimeFunction CxxAtomicObjectSetFn;
742 /// Type of an slot structure pointer. This is returned by the various
743 /// lookup functions.
744 llvm::Type *SlotTy;
745
746 public:
747 llvm::Constant *GetEHType(QualType T) override;
748
749 protected:
750 llvm::Value *LookupIMP(CodeGenFunction &CGF, llvm::Value *&Receiver,
751 llvm::Value *cmd, llvm::MDNode *node,
752 MessageSendInfo &MSI) override {
753 CGBuilderTy &Builder = CGF.Builder;
754 llvm::FunctionCallee LookupFn = SlotLookupFn;
755
756 // Store the receiver on the stack so that we can reload it later
757 Address ReceiverPtr =
758 CGF.CreateTempAlloca(Receiver->getType(), CGF.getPointerAlign());
759 Builder.CreateStore(Receiver, ReceiverPtr);
760
761 llvm::Value *self;
762
763 if (isa<ObjCMethodDecl>(CGF.CurCodeDecl)) {
764 self = CGF.LoadObjCSelf();
765 } else {
766 self = llvm::ConstantPointerNull::get(IdTy);
767 }
768
769 // The lookup function is guaranteed not to capture the receiver pointer.
770 if (auto *LookupFn2 = dyn_cast<llvm::Function>(LookupFn.getCallee()))
771 LookupFn2->addParamAttr(0, llvm::Attribute::NoCapture);
772
773 llvm::Value *args[] = {
774 EnforceType(Builder, ReceiverPtr.getPointer(), PtrToIdTy),
775 EnforceType(Builder, cmd, SelectorTy),
776 EnforceType(Builder, self, IdTy) };
777 llvm::CallBase *slot = CGF.EmitRuntimeCallOrInvoke(LookupFn, args);
778 slot->setOnlyReadsMemory();
779 slot->setMetadata(msgSendMDKind, node);
780
781 // Load the imp from the slot
782 llvm::Value *imp = Builder.CreateAlignedLoad(
783 IMPTy, Builder.CreateStructGEP(nullptr, slot, 4),
784 CGF.getPointerAlign());
785
786 // The lookup function may have changed the receiver, so make sure we use
787 // the new one.
788 Receiver = Builder.CreateLoad(ReceiverPtr, true);
789 return imp;
790 }
791
792 llvm::Value *LookupIMPSuper(CodeGenFunction &CGF, Address ObjCSuper,
793 llvm::Value *cmd,
794 MessageSendInfo &MSI) override {
795 CGBuilderTy &Builder = CGF.Builder;
796 llvm::Value *lookupArgs[] = {ObjCSuper.getPointer(), cmd};
797
798 llvm::CallInst *slot =
799 CGF.EmitNounwindRuntimeCall(SlotLookupSuperFn, lookupArgs);
800 slot->setOnlyReadsMemory();
801
802 return Builder.CreateAlignedLoad(
803 IMPTy, Builder.CreateStructGEP(nullptr, slot, 4),
804 CGF.getPointerAlign());
805 }
806
807 public:
808 CGObjCGNUstep(CodeGenModule &Mod) : CGObjCGNUstep(Mod, 9, 3, 1) {}
809 CGObjCGNUstep(CodeGenModule &Mod, unsigned ABI, unsigned ProtocolABI,
810 unsigned ClassABI) :
811 CGObjCGNU(Mod, ABI, ProtocolABI, ClassABI) {
812 const ObjCRuntime &R = CGM.getLangOpts().ObjCRuntime;
813
814 llvm::StructType *SlotStructTy =
815 llvm::StructType::get(PtrTy, PtrTy, PtrTy, IntTy, IMPTy);
816 SlotTy = llvm::PointerType::getUnqual(SlotStructTy);
817 // Slot_t objc_msg_lookup_sender(id *receiver, SEL selector, id sender);
818 SlotLookupFn.init(&CGM, "objc_msg_lookup_sender", SlotTy, PtrToIdTy,
819 SelectorTy, IdTy);
820 // Slot_t objc_slot_lookup_super(struct objc_super*, SEL);
821 SlotLookupSuperFn.init(&CGM, "objc_slot_lookup_super", SlotTy,
822 PtrToObjCSuperTy, SelectorTy);
823 // If we're in ObjC++ mode, then we want to make
824 if (usesSEHExceptions) {
825 llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
826 // void objc_exception_rethrow(void)
827 ExceptionReThrowFn.init(&CGM, "objc_exception_rethrow", VoidTy);
828 } else if (CGM.getLangOpts().CPlusPlus) {
829 llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
830 // void *__cxa_begin_catch(void *e)
831 EnterCatchFn.init(&CGM, "__cxa_begin_catch", PtrTy, PtrTy);
832 // void __cxa_end_catch(void)
833 ExitCatchFn.init(&CGM, "__cxa_end_catch", VoidTy);
834 // void _Unwind_Resume_or_Rethrow(void*)
835 ExceptionReThrowFn.init(&CGM, "_Unwind_Resume_or_Rethrow", VoidTy,
836 PtrTy);
837 } else if (R.getVersion() >= VersionTuple(1, 7)) {
838 llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
839 // id objc_begin_catch(void *e)
840 EnterCatchFn.init(&CGM, "objc_begin_catch", IdTy, PtrTy);
841 // void objc_end_catch(void)
842 ExitCatchFn.init(&CGM, "objc_end_catch", VoidTy);
843 // void _Unwind_Resume_or_Rethrow(void*)
844 ExceptionReThrowFn.init(&CGM, "objc_exception_rethrow", VoidTy, PtrTy);
845 }
846 llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
847 SetPropertyAtomic.init(&CGM, "objc_setProperty_atomic", VoidTy, IdTy,
848 SelectorTy, IdTy, PtrDiffTy);
849 SetPropertyAtomicCopy.init(&CGM, "objc_setProperty_atomic_copy", VoidTy,
850 IdTy, SelectorTy, IdTy, PtrDiffTy);
851 SetPropertyNonAtomic.init(&CGM, "objc_setProperty_nonatomic", VoidTy,
852 IdTy, SelectorTy, IdTy, PtrDiffTy);
853 SetPropertyNonAtomicCopy.init(&CGM, "objc_setProperty_nonatomic_copy",
854 VoidTy, IdTy, SelectorTy, IdTy, PtrDiffTy);
855 // void objc_setCppObjectAtomic(void *dest, const void *src, void
856 // *helper);
857 CxxAtomicObjectSetFn.init(&CGM, "objc_setCppObjectAtomic", VoidTy, PtrTy,
858 PtrTy, PtrTy);
859 // void objc_getCppObjectAtomic(void *dest, const void *src, void
860 // *helper);
861 CxxAtomicObjectGetFn.init(&CGM, "objc_getCppObjectAtomic", VoidTy, PtrTy,
862 PtrTy, PtrTy);
863 }
864
865 llvm::FunctionCallee GetCppAtomicObjectGetFunction() override {
866 // The optimised functions were added in version 1.7 of the GNUstep
867 // runtime.
868 assert (CGM.getLangOpts().ObjCRuntime.getVersion() >=
869 VersionTuple(1, 7));
870 return CxxAtomicObjectGetFn;
871 }
872
873 llvm::FunctionCallee GetCppAtomicObjectSetFunction() override {
874 // The optimised functions were added in version 1.7 of the GNUstep
875 // runtime.
876 assert (CGM.getLangOpts().ObjCRuntime.getVersion() >=
877 VersionTuple(1, 7));
878 return CxxAtomicObjectSetFn;
879 }
880
881 llvm::FunctionCallee GetOptimizedPropertySetFunction(bool atomic,
882 bool copy) override {
883 // The optimised property functions omit the GC check, and so are not
884 // safe to use in GC mode. The standard functions are fast in GC mode,
885 // so there is less advantage in using them.
886 assert ((CGM.getLangOpts().getGC() == LangOptions::NonGC));
887 // The optimised functions were added in version 1.7 of the GNUstep
888 // runtime.
889 assert (CGM.getLangOpts().ObjCRuntime.getVersion() >=
890 VersionTuple(1, 7));
891
892 if (atomic) {
893 if (copy) return SetPropertyAtomicCopy;
894 return SetPropertyAtomic;
895 }
896
897 return copy ? SetPropertyNonAtomicCopy : SetPropertyNonAtomic;
898 }
899};
900
901/// GNUstep Objective-C ABI version 2 implementation.
902/// This is the ABI that provides a clean break with the legacy GCC ABI and
903/// cleans up a number of things that were added to work around 1980s linkers.
904class CGObjCGNUstep2 : public CGObjCGNUstep {
905 enum SectionKind
906 {
907 SelectorSection = 0,
908 ClassSection,
909 ClassReferenceSection,
910 CategorySection,
911 ProtocolSection,
912 ProtocolReferenceSection,
913 ClassAliasSection,
914 ConstantStringSection
915 };
916 static const char *const SectionsBaseNames[8];
917 static const char *const PECOFFSectionsBaseNames[8];
918 template<SectionKind K>
919 std::string sectionName() {
920 if (CGM.getTriple().isOSBinFormatCOFF()) {
921 std::string name(PECOFFSectionsBaseNames[K]);
922 name += "$m";
923 return name;
924 }
925 return SectionsBaseNames[K];
926 }
927 /// The GCC ABI superclass message lookup function. Takes a pointer to a
928 /// structure describing the receiver and the class, and a selector as
929 /// arguments. Returns the IMP for the corresponding method.
930 LazyRuntimeFunction MsgLookupSuperFn;
931 /// A flag indicating if we've emitted at least one protocol.
932 /// If we haven't, then we need to emit an empty protocol, to ensure that the
933 /// __start__objc_protocols and __stop__objc_protocols sections exist.
934 bool EmittedProtocol = false;
935 /// A flag indicating if we've emitted at least one protocol reference.
936 /// If we haven't, then we need to emit an empty protocol, to ensure that the
937 /// __start__objc_protocol_refs and __stop__objc_protocol_refs sections
938 /// exist.
939 bool EmittedProtocolRef = false;
940 /// A flag indicating if we've emitted at least one class.
941 /// If we haven't, then we need to emit an empty protocol, to ensure that the
942 /// __start__objc_classes and __stop__objc_classes sections / exist.
943 bool EmittedClass = false;
944 /// Generate the name of a symbol for a reference to a class. Accesses to
945 /// classes should be indirected via this.
946
947 typedef std::pair<std::string, std::pair<llvm::Constant*, int>> EarlyInitPair;
948 std::vector<EarlyInitPair> EarlyInitList;
949
950 std::string SymbolForClassRef(StringRef Name, bool isWeak) {
951 if (isWeak)
952 return (ManglePublicSymbol("OBJC_WEAK_REF_CLASS_") + Name).str();
953 else
954 return (ManglePublicSymbol("OBJC_REF_CLASS_") + Name).str();
955 }
956 /// Generate the name of a class symbol.
957 std::string SymbolForClass(StringRef Name) {
958 return (ManglePublicSymbol("OBJC_CLASS_") + Name).str();
959 }
960 void CallRuntimeFunction(CGBuilderTy &B, StringRef FunctionName,
961 ArrayRef<llvm::Value*> Args) {
962 SmallVector<llvm::Type *,8> Types;
963 for (auto *Arg : Args)
964 Types.push_back(Arg->getType());
965 llvm::FunctionType *FT = llvm::FunctionType::get(B.getVoidTy(), Types,
966 false);
967 llvm::FunctionCallee Fn = CGM.CreateRuntimeFunction(FT, FunctionName);
968 B.CreateCall(Fn, Args);
969 }
970
971 ConstantAddress GenerateConstantString(const StringLiteral *SL) override {
972
973 auto Str = SL->getString();
974 CharUnits Align = CGM.getPointerAlign();
975
976 // Look for an existing one
977 llvm::StringMap<llvm::Constant*>::iterator old = ObjCStrings.find(Str);
978 if (old != ObjCStrings.end())
979 return ConstantAddress(old->getValue(), Align);
980
981 bool isNonASCII = SL->containsNonAscii();
982
983 auto LiteralLength = SL->getLength();
984
985 if ((CGM.getTarget().getPointerWidth(0) == 64) &&
986 (LiteralLength < 9) && !isNonASCII) {
987 // Tiny strings are only used on 64-bit platforms. They store 8 7-bit
988 // ASCII characters in the high 56 bits, followed by a 4-bit length and a
989 // 3-bit tag (which is always 4).
990 uint64_t str = 0;
991 // Fill in the characters
992 for (unsigned i=0 ; i<LiteralLength ; i++)
993 str |= ((uint64_t)SL->getCodeUnit(i)) << ((64 - 4 - 3) - (i*7));
994 // Fill in the length
995 str |= LiteralLength << 3;
996 // Set the tag
997 str |= 4;
998 auto *ObjCStr = llvm::ConstantExpr::getIntToPtr(
999 llvm::ConstantInt::get(Int64Ty, str), IdTy);
1000 ObjCStrings[Str] = ObjCStr;
1001 return ConstantAddress(ObjCStr, Align);
1002 }
1003
1004 StringRef StringClass = CGM.getLangOpts().ObjCConstantStringClass;
1005
1006 if (StringClass.empty()) StringClass = "NSConstantString";
1007
1008 std::string Sym = SymbolForClass(StringClass);
1009
1010 llvm::Constant *isa = TheModule.getNamedGlobal(Sym);
1011
1012 if (!isa) {
1013 isa = new llvm::GlobalVariable(TheModule, IdTy, /* isConstant */false,
1014 llvm::GlobalValue::ExternalLinkage, nullptr, Sym);
1015 if (CGM.getTriple().isOSBinFormatCOFF()) {
1016 cast<llvm::GlobalValue>(isa)->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass);
1017 }
1018 } else if (isa->getType() != PtrToIdTy)
1019 isa = llvm::ConstantExpr::getBitCast(isa, PtrToIdTy);
1020
1021 // struct
1022 // {
1023 // Class isa;
1024 // uint32_t flags;
1025 // uint32_t length; // Number of codepoints
1026 // uint32_t size; // Number of bytes
1027 // uint32_t hash;
1028 // const char *data;
1029 // };
1030
1031 ConstantInitBuilder Builder(CGM);
1032 auto Fields = Builder.beginStruct();
1033 if (!CGM.getTriple().isOSBinFormatCOFF()) {
1034 Fields.add(isa);
1035 } else {
1036 Fields.addNullPointer(PtrTy);
1037 }
1038 // For now, all non-ASCII strings are represented as UTF-16. As such, the
1039 // number of bytes is simply double the number of UTF-16 codepoints. In
1040 // ASCII strings, the number of bytes is equal to the number of non-ASCII
1041 // codepoints.
1042 if (isNonASCII) {
1043 unsigned NumU8CodeUnits = Str.size();
1044 // A UTF-16 representation of a unicode string contains at most the same
1045 // number of code units as a UTF-8 representation. Allocate that much
1046 // space, plus one for the final null character.
1047 SmallVector<llvm::UTF16, 128> ToBuf(NumU8CodeUnits + 1);
1048 const llvm::UTF8 *FromPtr = (const llvm::UTF8 *)Str.data();
1049 llvm::UTF16 *ToPtr = &ToBuf[0];
1050 (void)llvm::ConvertUTF8toUTF16(&FromPtr, FromPtr + NumU8CodeUnits,
1051 &ToPtr, ToPtr + NumU8CodeUnits, llvm::strictConversion);
1052 uint32_t StringLength = ToPtr - &ToBuf[0];
1053 // Add null terminator
1054 *ToPtr = 0;
1055 // Flags: 2 indicates UTF-16 encoding
1056 Fields.addInt(Int32Ty, 2);
1057 // Number of UTF-16 codepoints
1058 Fields.addInt(Int32Ty, StringLength);
1059 // Number of bytes
1060 Fields.addInt(Int32Ty, StringLength * 2);
1061 // Hash. Not currently initialised by the compiler.
1062 Fields.addInt(Int32Ty, 0);
1063 // pointer to the data string.
1064 auto Arr = llvm::makeArrayRef(&ToBuf[0], ToPtr+1);
1065 auto *C = llvm::ConstantDataArray::get(VMContext, Arr);
1066 auto *Buffer = new llvm::GlobalVariable(TheModule, C->getType(),
1067 /*isConstant=*/true, llvm::GlobalValue::PrivateLinkage, C, ".str");
1068 Buffer->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
1069 Fields.add(Buffer);
1070 } else {
1071 // Flags: 0 indicates ASCII encoding
1072 Fields.addInt(Int32Ty, 0);
1073 // Number of UTF-16 codepoints, each ASCII byte is a UTF-16 codepoint
1074 Fields.addInt(Int32Ty, Str.size());
1075 // Number of bytes
1076 Fields.addInt(Int32Ty, Str.size());
1077 // Hash. Not currently initialised by the compiler.
1078 Fields.addInt(Int32Ty, 0);
1079 // Data pointer
1080 Fields.add(MakeConstantString(Str));
1081 }
1082 std::string StringName;
1083 bool isNamed = !isNonASCII;
1084 if (isNamed) {
1085 StringName = ".objc_str_";
1086 for (int i=0,e=Str.size() ; i<e ; ++i) {
1087 unsigned char c = Str[i];
1088 if (isalnum(c))
1089 StringName += c;
1090 else if (c == ' ')
1091 StringName += '_';
1092 else {
1093 isNamed = false;
1094 break;
1095 }
1096 }
1097 }
1098 auto *ObjCStrGV =
1099 Fields.finishAndCreateGlobal(
1100 isNamed ? StringRef(StringName) : ".objc_string",
1101 Align, false, isNamed ? llvm::GlobalValue::LinkOnceODRLinkage
1102 : llvm::GlobalValue::PrivateLinkage);
1103 ObjCStrGV->setSection(sectionName<ConstantStringSection>());
1104 if (isNamed) {
1105 ObjCStrGV->setComdat(TheModule.getOrInsertComdat(StringName));
1106 ObjCStrGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
1107 }
1108 if (CGM.getTriple().isOSBinFormatCOFF()) {
1109 std::pair<llvm::Constant*, int> v{ObjCStrGV, 0};
1110 EarlyInitList.emplace_back(Sym, v);
1111 }
1112 llvm::Constant *ObjCStr = llvm::ConstantExpr::getBitCast(ObjCStrGV, IdTy);
1113 ObjCStrings[Str] = ObjCStr;
1114 ConstantStrings.push_back(ObjCStr);
1115 return ConstantAddress(ObjCStr, Align);
1116 }
1117
1118 void PushProperty(ConstantArrayBuilder &PropertiesArray,
1119 const ObjCPropertyDecl *property,
1120 const Decl *OCD,
1121 bool isSynthesized=true, bool
1122 isDynamic=true) override {
1123 // struct objc_property
1124 // {
1125 // const char *name;
1126 // const char *attributes;
1127 // const char *type;
1128 // SEL getter;
1129 // SEL setter;
1130 // };
1131 auto Fields = PropertiesArray.beginStruct(PropertyMetadataTy);
1132 ASTContext &Context = CGM.getContext();
1133 Fields.add(MakeConstantString(property->getNameAsString()));
1134 std::string TypeStr =
1135 CGM.getContext().getObjCEncodingForPropertyDecl(property, OCD);
1136 Fields.add(MakeConstantString(TypeStr));
1137 std::string typeStr;
1138 Context.getObjCEncodingForType(property->getType(), typeStr);
1139 Fields.add(MakeConstantString(typeStr));
1140 auto addPropertyMethod = [&](const ObjCMethodDecl *accessor) {
1141 if (accessor) {
1142 std::string TypeStr = Context.getObjCEncodingForMethodDecl(accessor);
1143 Fields.add(GetConstantSelector(accessor->getSelector(), TypeStr));
1144 } else {
1145 Fields.add(NULLPtr);
1146 }
1147 };
1148 addPropertyMethod(property->getGetterMethodDecl());
1149 addPropertyMethod(property->getSetterMethodDecl());
1150 Fields.finishAndAddTo(PropertiesArray);
1151 }
1152
1153 llvm::Constant *
1154 GenerateProtocolMethodList(ArrayRef<const ObjCMethodDecl*> Methods) override {
1155 // struct objc_protocol_method_description
1156 // {
1157 // SEL selector;
1158 // const char *types;
1159 // };
1160 llvm::StructType *ObjCMethodDescTy =
1161 llvm::StructType::get(CGM.getLLVMContext(),
1162 { PtrToInt8Ty, PtrToInt8Ty });
1163 ASTContext &Context = CGM.getContext();
1164 ConstantInitBuilder Builder(CGM);
1165 // struct objc_protocol_method_description_list
1166 // {
1167 // int count;
1168 // int size;
1169 // struct objc_protocol_method_description methods[];
1170 // };
1171 auto MethodList = Builder.beginStruct();
1172 // int count;
1173 MethodList.addInt(IntTy, Methods.size());
1174 // int size; // sizeof(struct objc_method_description)
1175 llvm::DataLayout td(&TheModule);
1176 MethodList.addInt(IntTy, td.getTypeSizeInBits(ObjCMethodDescTy) /
1177 CGM.getContext().getCharWidth());
1178 // struct objc_method_description[]
1179 auto MethodArray = MethodList.beginArray(ObjCMethodDescTy);
1180 for (auto *M : Methods) {
1181 auto Method = MethodArray.beginStruct(ObjCMethodDescTy);
1182 Method.add(CGObjCGNU::GetConstantSelector(M));
1183 Method.add(GetTypeString(Context.getObjCEncodingForMethodDecl(M, true)));
1184 Method.finishAndAddTo(MethodArray);
1185 }
1186 MethodArray.finishAndAddTo(MethodList);
1187 return MethodList.finishAndCreateGlobal(".objc_protocol_method_list",
1188 CGM.getPointerAlign());
1189 }
1190 llvm::Constant *GenerateCategoryProtocolList(const ObjCCategoryDecl *OCD)
1191 override {
1192 const auto &ReferencedProtocols = OCD->getReferencedProtocols();
1193 auto RuntimeProtocols = GetRuntimeProtocolList(ReferencedProtocols.begin(),
1194 ReferencedProtocols.end());
1195 SmallVector<llvm::Constant *, 16> Protocols;
1196 for (const auto *PI : RuntimeProtocols)
1197 Protocols.push_back(
1198 llvm::ConstantExpr::getBitCast(GenerateProtocolRef(PI),
1199 ProtocolPtrTy));
1200 return GenerateProtocolList(Protocols);
1201 }
1202
1203 llvm::Value *LookupIMPSuper(CodeGenFunction &CGF, Address ObjCSuper,
1204 llvm::Value *cmd, MessageSendInfo &MSI) override {
1205 // Don't access the slot unless we're trying to cache the result.
1206 CGBuilderTy &Builder = CGF.Builder;
1207 llvm::Value *lookupArgs[] = {CGObjCGNU::EnforceType(Builder, ObjCSuper,
1208 PtrToObjCSuperTy).getPointer(), cmd};
1209 return CGF.EmitNounwindRuntimeCall(MsgLookupSuperFn, lookupArgs);
1210 }
1211
1212 llvm::GlobalVariable *GetClassVar(StringRef Name, bool isWeak=false) {
1213 std::string SymbolName = SymbolForClassRef(Name, isWeak);
1214 auto *ClassSymbol = TheModule.getNamedGlobal(SymbolName);
1215 if (ClassSymbol)
1216 return ClassSymbol;
1217 ClassSymbol = new llvm::GlobalVariable(TheModule,
1218 IdTy, false, llvm::GlobalValue::ExternalLinkage,
1219 nullptr, SymbolName);
1220 // If this is a weak symbol, then we are creating a valid definition for
1221 // the symbol, pointing to a weak definition of the real class pointer. If
1222 // this is not a weak reference, then we are expecting another compilation
1223 // unit to provide the real indirection symbol.
1224 if (isWeak)
1225 ClassSymbol->setInitializer(new llvm::GlobalVariable(TheModule,
1226 Int8Ty, false, llvm::GlobalValue::ExternalWeakLinkage,
1227 nullptr, SymbolForClass(Name)));
1228 else {
1229 if (CGM.getTriple().isOSBinFormatCOFF()) {
1230 IdentifierInfo &II = CGM.getContext().Idents.get(Name);
1231 TranslationUnitDecl *TUDecl = CGM.getContext().getTranslationUnitDecl();
1232 DeclContext *DC = TranslationUnitDecl::castToDeclContext(TUDecl);
1233
1234 const ObjCInterfaceDecl *OID = nullptr;
1235 for (const auto *Result : DC->lookup(&II))
1236 if ((OID = dyn_cast<ObjCInterfaceDecl>(Result)))
1237 break;
1238
1239 // The first Interface we find may be a @class,
1240 // which should only be treated as the source of
1241 // truth in the absence of a true declaration.
1242 assert(OID && "Failed to find ObjCInterfaceDecl");
1243 const ObjCInterfaceDecl *OIDDef = OID->getDefinition();
1244 if (OIDDef != nullptr)
1245 OID = OIDDef;
1246
1247 auto Storage = llvm::GlobalValue::DefaultStorageClass;
1248 if (OID->hasAttr<DLLImportAttr>())
1249 Storage = llvm::GlobalValue::DLLImportStorageClass;
1250 else if (OID->hasAttr<DLLExportAttr>())
1251 Storage = llvm::GlobalValue::DLLExportStorageClass;
1252
1253 cast<llvm::GlobalValue>(ClassSymbol)->setDLLStorageClass(Storage);
1254 }
1255 }
1256 assert(ClassSymbol->getName() == SymbolName);
1257 return ClassSymbol;
1258 }
1259 llvm::Value *GetClassNamed(CodeGenFunction &CGF,
1260 const std::string &Name,
1261 bool isWeak) override {
1262 return CGF.Builder.CreateLoad(Address(GetClassVar(Name, isWeak),
1263 CGM.getPointerAlign()));
1264 }
1265 int32_t FlagsForOwnership(Qualifiers::ObjCLifetime Ownership) {
1266 // typedef enum {
1267 // ownership_invalid = 0,
1268 // ownership_strong = 1,
1269 // ownership_weak = 2,
1270 // ownership_unsafe = 3
1271 // } ivar_ownership;
1272 int Flag;
1273 switch (Ownership) {
1274 case Qualifiers::OCL_Strong:
1275 Flag = 1;
1276 break;
1277 case Qualifiers::OCL_Weak:
1278 Flag = 2;
1279 break;
1280 case Qualifiers::OCL_ExplicitNone:
1281 Flag = 3;
1282 break;
1283 case Qualifiers::OCL_None:
1284 case Qualifiers::OCL_Autoreleasing:
1285 assert(Ownership != Qualifiers::OCL_Autoreleasing);
1286 Flag = 0;
1287 }
1288 return Flag;
1289 }
1290 llvm::Constant *GenerateIvarList(ArrayRef<llvm::Constant *> IvarNames,
1291 ArrayRef<llvm::Constant *> IvarTypes,
1292 ArrayRef<llvm::Constant *> IvarOffsets,
1293 ArrayRef<llvm::Constant *> IvarAlign,
1294 ArrayRef<Qualifiers::ObjCLifetime> IvarOwnership) override {
1295 llvm_unreachable("Method should not be called!");
1296 }
1297
1298 llvm::Constant *GenerateEmptyProtocol(StringRef ProtocolName) override {
1299 std::string Name = SymbolForProtocol(ProtocolName);
1300 auto *GV = TheModule.getGlobalVariable(Name);
1301 if (!GV) {
1302 // Emit a placeholder symbol.
1303 GV = new llvm::GlobalVariable(TheModule, ProtocolTy, false,
1304 llvm::GlobalValue::ExternalLinkage, nullptr, Name);
1305 GV->setAlignment(CGM.getPointerAlign().getAsAlign());
1306 }
1307 return llvm::ConstantExpr::getBitCast(GV, ProtocolPtrTy);
1308 }
1309
1310 /// Existing protocol references.
1311 llvm::StringMap<llvm::Constant*> ExistingProtocolRefs;
1312
1313 llvm::Value *GenerateProtocolRef(CodeGenFunction &CGF,
1314 const ObjCProtocolDecl *PD) override {
1315 auto Name = PD->getNameAsString();
1316 auto *&Ref = ExistingProtocolRefs[Name];
1317 if (!Ref) {
1318 auto *&Protocol = ExistingProtocols[Name];
1319 if (!Protocol)
1320 Protocol = GenerateProtocolRef(PD);
1321 std::string RefName = SymbolForProtocolRef(Name);
1322 assert(!TheModule.getGlobalVariable(RefName));
1323 // Emit a reference symbol.
1324 auto GV = new llvm::GlobalVariable(TheModule, ProtocolPtrTy,
1325 false, llvm::GlobalValue::LinkOnceODRLinkage,
1326 llvm::ConstantExpr::getBitCast(Protocol, ProtocolPtrTy), RefName);
1327 GV->setComdat(TheModule.getOrInsertComdat(RefName));
1328 GV->setSection(sectionName<ProtocolReferenceSection>());
1329 GV->setAlignment(CGM.getPointerAlign().getAsAlign());
1330 Ref = GV;
1331 }
1332 EmittedProtocolRef = true;
1333 return CGF.Builder.CreateAlignedLoad(ProtocolPtrTy, Ref,
1334 CGM.getPointerAlign());
1335 }
1336
1337 llvm::Constant *GenerateProtocolList(ArrayRef<llvm::Constant*> Protocols) {
1338 llvm::ArrayType *ProtocolArrayTy = llvm::ArrayType::get(ProtocolPtrTy,
1339 Protocols.size());
1340 llvm::Constant * ProtocolArray = llvm::ConstantArray::get(ProtocolArrayTy,
1341 Protocols);
1342 ConstantInitBuilder builder(CGM);
1343 auto ProtocolBuilder = builder.beginStruct();
1344 ProtocolBuilder.addNullPointer(PtrTy);
1345 ProtocolBuilder.addInt(SizeTy, Protocols.size());
1346 ProtocolBuilder.add(ProtocolArray);
1347 return ProtocolBuilder.finishAndCreateGlobal(".objc_protocol_list",
1348 CGM.getPointerAlign(), false, llvm::GlobalValue::InternalLinkage);
1349 }
1350
1351 void GenerateProtocol(const ObjCProtocolDecl *PD) override {
1352 // Do nothing - we only emit referenced protocols.
1353 }
1354 llvm::Constant *GenerateProtocolRef(const ObjCProtocolDecl *PD) override {
1355 std::string ProtocolName = PD->getNameAsString();
1356 auto *&Protocol = ExistingProtocols[ProtocolName];
1357 if (Protocol)
1358 return Protocol;
1359
1360 EmittedProtocol = true;
1361
1362 auto SymName = SymbolForProtocol(ProtocolName);
1363 auto *OldGV = TheModule.getGlobalVariable(SymName);
1364
1365 // Use the protocol definition, if there is one.
1366 if (const ObjCProtocolDecl *Def = PD->getDefinition())
1367 PD = Def;
1368 else {
1369 // If there is no definition, then create an external linkage symbol and
1370 // hope that someone else fills it in for us (and fail to link if they
1371 // don't).
1372 assert(!OldGV);
1373 Protocol = new llvm::GlobalVariable(TheModule, ProtocolTy,
1374 /*isConstant*/false,
1375 llvm::GlobalValue::ExternalLinkage, nullptr, SymName);
1376 return Protocol;
1377 }
1378
1379 SmallVector<llvm::Constant*, 16> Protocols;
1380 auto RuntimeProtocols =
1381 GetRuntimeProtocolList(PD->protocol_begin(), PD->protocol_end());
1382 for (const auto *PI : RuntimeProtocols)
1383 Protocols.push_back(
1384 llvm::ConstantExpr::getBitCast(GenerateProtocolRef(PI),
1385 ProtocolPtrTy));
1386 llvm::Constant *ProtocolList = GenerateProtocolList(Protocols);
1387
1388 // Collect information about methods
1389 llvm::Constant *InstanceMethodList, *OptionalInstanceMethodList;
1390 llvm::Constant *ClassMethodList, *OptionalClassMethodList;
1391 EmitProtocolMethodList(PD->instance_methods(), InstanceMethodList,
1392 OptionalInstanceMethodList);
1393 EmitProtocolMethodList(PD->class_methods(), ClassMethodList,
1394 OptionalClassMethodList);
1395
1396 // The isa pointer must be set to a magic number so the runtime knows it's
1397 // the correct layout.
1398 ConstantInitBuilder builder(CGM);
1399 auto ProtocolBuilder = builder.beginStruct();
1400 ProtocolBuilder.add(llvm::ConstantExpr::getIntToPtr(
1401 llvm::ConstantInt::get(Int32Ty, ProtocolVersion), IdTy));
1402 ProtocolBuilder.add(MakeConstantString(ProtocolName));
1403 ProtocolBuilder.add(ProtocolList);
1404 ProtocolBuilder.add(InstanceMethodList);
1405 ProtocolBuilder.add(ClassMethodList);
1406 ProtocolBuilder.add(OptionalInstanceMethodList);
1407 ProtocolBuilder.add(OptionalClassMethodList);
1408 // Required instance properties
1409 ProtocolBuilder.add(GeneratePropertyList(nullptr, PD, false, false));
1410 // Optional instance properties
1411 ProtocolBuilder.add(GeneratePropertyList(nullptr, PD, false, true));
1412 // Required class properties
1413 ProtocolBuilder.add(GeneratePropertyList(nullptr, PD, true, false));
1414 // Optional class properties
1415 ProtocolBuilder.add(GeneratePropertyList(nullptr, PD, true, true));
1416
1417 auto *GV = ProtocolBuilder.finishAndCreateGlobal(SymName,
1418 CGM.getPointerAlign(), false, llvm::GlobalValue::ExternalLinkage);
1419 GV->setSection(sectionName<ProtocolSection>());
1420 GV->setComdat(TheModule.getOrInsertComdat(SymName));
1421 if (OldGV) {
1422 OldGV->replaceAllUsesWith(llvm::ConstantExpr::getBitCast(GV,
1423 OldGV->getType()));
1424 OldGV->removeFromParent();
1425 GV->setName(SymName);
1426 }
1427 Protocol = GV;
1428 return GV;
1429 }
1430 llvm::Constant *EnforceType(llvm::Constant *Val, llvm::Type *Ty) {
1431 if (Val->getType() == Ty)
1432 return Val;
1433 return llvm::ConstantExpr::getBitCast(Val, Ty);
1434 }
1435 llvm::Value *GetTypedSelector(CodeGenFunction &CGF, Selector Sel,
1436 const std::string &TypeEncoding) override {
1437 return GetConstantSelector(Sel, TypeEncoding);
1438 }
1439 llvm::Constant *GetTypeString(llvm::StringRef TypeEncoding) {
1440 if (TypeEncoding.empty())
1441 return NULLPtr;
1442 std::string MangledTypes = std::string(TypeEncoding);
1443 std::replace(MangledTypes.begin(), MangledTypes.end(),
1444 '@', '\1');
1445 std::string TypesVarName = ".objc_sel_types_" + MangledTypes;
1446 auto *TypesGlobal = TheModule.getGlobalVariable(TypesVarName);
1447 if (!TypesGlobal) {
1448 llvm::Constant *Init = llvm::ConstantDataArray::getString(VMContext,
1449 TypeEncoding);
1450 auto *GV = new llvm::GlobalVariable(TheModule, Init->getType(),
1451 true, llvm::GlobalValue::LinkOnceODRLinkage, Init, TypesVarName);
1452 GV->setComdat(TheModule.getOrInsertComdat(TypesVarName));
1453 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
1454 TypesGlobal = GV;
1455 }
1456 return llvm::ConstantExpr::getGetElementPtr(TypesGlobal->getValueType(),
1457 TypesGlobal, Zeros);
1458 }
1459 llvm::Constant *GetConstantSelector(Selector Sel,
1460 const std::string &TypeEncoding) override {
1461 // @ is used as a special character in symbol names (used for symbol
1462 // versioning), so mangle the name to not include it. Replace it with a
1463 // character that is not a valid type encoding character (and, being
1464 // non-printable, never will be!)
1465 std::string MangledTypes = TypeEncoding;
1466 std::replace(MangledTypes.begin(), MangledTypes.end(),
1467 '@', '\1');
1468 auto SelVarName = (StringRef(".objc_selector_") + Sel.getAsString() + "_" +
1469 MangledTypes).str();
1470 if (auto *GV = TheModule.getNamedGlobal(SelVarName))
1471 return EnforceType(GV, SelectorTy);
1472 ConstantInitBuilder builder(CGM);
1473 auto SelBuilder = builder.beginStruct();
1474 SelBuilder.add(ExportUniqueString(Sel.getAsString(), ".objc_sel_name_",
1475 true));
1476 SelBuilder.add(GetTypeString(TypeEncoding));
1477 auto *GV = SelBuilder.finishAndCreateGlobal(SelVarName,
1478 CGM.getPointerAlign(), false, llvm::GlobalValue::LinkOnceODRLinkage);
1479 GV->setComdat(TheModule.getOrInsertComdat(SelVarName));
1480 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
1481 GV->setSection(sectionName<SelectorSection>());
1482 auto *SelVal = EnforceType(GV, SelectorTy);
1483 return SelVal;
1484 }
1485 llvm::StructType *emptyStruct = nullptr;
1486
1487 /// Return pointers to the start and end of a section. On ELF platforms, we
1488 /// use the __start_ and __stop_ symbols that GNU-compatible linkers will set
1489 /// to the start and end of section names, as long as those section names are
1490 /// valid identifiers and the symbols are referenced but not defined. On
1491 /// Windows, we use the fact that MSVC-compatible linkers will lexically sort
1492 /// by subsections and place everything that we want to reference in a middle
1493 /// subsection and then insert zero-sized symbols in subsections a and z.
1494 std::pair<llvm::Constant*,llvm::Constant*>
1495 GetSectionBounds(StringRef Section) {
1496 if (CGM.getTriple().isOSBinFormatCOFF()) {
1497 if (emptyStruct == nullptr) {
1498 emptyStruct = llvm::StructType::create(VMContext, ".objc_section_sentinel");
1499 emptyStruct->setBody({}, /*isPacked*/true);
1500 }
1501 auto ZeroInit = llvm::Constant::getNullValue(emptyStruct);
1502 auto Sym = [&](StringRef Prefix, StringRef SecSuffix) {
1503 auto *Sym = new llvm::GlobalVariable(TheModule, emptyStruct,
1504 /*isConstant*/false,
1505 llvm::GlobalValue::LinkOnceODRLinkage, ZeroInit, Prefix +
1506 Section);
1507 Sym->setVisibility(llvm::GlobalValue::HiddenVisibility);
1508 Sym->setSection((Section + SecSuffix).str());
1509 Sym->setComdat(TheModule.getOrInsertComdat((Prefix +
1510 Section).str()));
1511 Sym->setAlignment(CGM.getPointerAlign().getAsAlign());
1512 return Sym;
1513 };
1514 return { Sym("__start_", "$a"), Sym("__stop", "$z") };
1515 }
1516 auto *Start = new llvm::GlobalVariable(TheModule, PtrTy,
1517 /*isConstant*/false,
1518 llvm::GlobalValue::ExternalLinkage, nullptr, StringRef("__start_") +
1519 Section);
1520 Start->setVisibility(llvm::GlobalValue::HiddenVisibility);
1521 auto *Stop = new llvm::GlobalVariable(TheModule, PtrTy,
1522 /*isConstant*/false,
1523 llvm::GlobalValue::ExternalLinkage, nullptr, StringRef("__stop_") +
1524 Section);
1525 Stop->setVisibility(llvm::GlobalValue::HiddenVisibility);
1526 return { Start, Stop };
1527 }
1528 CatchTypeInfo getCatchAllTypeInfo() override {
1529 return CGM.getCXXABI().getCatchAllTypeInfo();
1530 }
1531 llvm::Function *ModuleInitFunction() override {
1532 llvm::Function *LoadFunction = llvm::Function::Create(
1533 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), false),
1534 llvm::GlobalValue::LinkOnceODRLinkage, ".objcv2_load_function",
1535 &TheModule);
1536 LoadFunction->setVisibility(llvm::GlobalValue::HiddenVisibility);
1537 LoadFunction->setComdat(TheModule.getOrInsertComdat(".objcv2_load_function"));
1538
1539 llvm::BasicBlock *EntryBB =
1540 llvm::BasicBlock::Create(VMContext, "entry", LoadFunction);
1541 CGBuilderTy B(CGM, VMContext);
1542 B.SetInsertPoint(EntryBB);
1543 ConstantInitBuilder builder(CGM);
1544 auto InitStructBuilder = builder.beginStruct();
1545 InitStructBuilder.addInt(Int64Ty, 0);
1546 auto &sectionVec = CGM.getTriple().isOSBinFormatCOFF() ? PECOFFSectionsBaseNames : SectionsBaseNames;
1547 for (auto *s : sectionVec) {
1548 auto bounds = GetSectionBounds(s);
1549 InitStructBuilder.add(bounds.first);
1550 InitStructBuilder.add(bounds.second);
1551 }
1552 auto *InitStruct = InitStructBuilder.finishAndCreateGlobal(".objc_init",
1553 CGM.getPointerAlign(), false, llvm::GlobalValue::LinkOnceODRLinkage);
1554 InitStruct->setVisibility(llvm::GlobalValue::HiddenVisibility);
1555 InitStruct->setComdat(TheModule.getOrInsertComdat(".objc_init"));
1556
1557 CallRuntimeFunction(B, "__objc_load", {InitStruct});;
1558 B.CreateRetVoid();
1559 // Make sure that the optimisers don't delete this function.
1560 CGM.addCompilerUsedGlobal(LoadFunction);
1561 // FIXME: Currently ELF only!
1562 // We have to do this by hand, rather than with @llvm.ctors, so that the
1563 // linker can remove the duplicate invocations.
1564 auto *InitVar = new llvm::GlobalVariable(TheModule, LoadFunction->getType(),
1565 /*isConstant*/false, llvm::GlobalValue::LinkOnceAnyLinkage,
1566 LoadFunction, ".objc_ctor");
1567 // Check that this hasn't been renamed. This shouldn't happen, because
1568 // this function should be called precisely once.
1569 assert(InitVar->getName() == ".objc_ctor");
1570 // In Windows, initialisers are sorted by the suffix. XCL is for library
1571 // initialisers, which run before user initialisers. We are running
1572 // Objective-C loads at the end of library load. This means +load methods
1573 // will run before any other static constructors, but that static
1574 // constructors can see a fully initialised Objective-C state.
1575 if (CGM.getTriple().isOSBinFormatCOFF())
1576 InitVar->setSection(".CRT$XCLz");
1577 else
1578 {
1579 if (CGM.getCodeGenOpts().UseInitArray)
1580 InitVar->setSection(".init_array");
1581 else
1582 InitVar->setSection(".ctors");
1583 }
1584 InitVar->setVisibility(llvm::GlobalValue::HiddenVisibility);
1585 InitVar->setComdat(TheModule.getOrInsertComdat(".objc_ctor"));
1586 CGM.addUsedGlobal(InitVar);
1587 for (auto *C : Categories) {
1588 auto *Cat = cast<llvm::GlobalVariable>(C->stripPointerCasts());
1589 Cat->setSection(sectionName<CategorySection>());
1590 CGM.addUsedGlobal(Cat);
1591 }
1592 auto createNullGlobal = [&](StringRef Name, ArrayRef<llvm::Constant*> Init,
1593 StringRef Section) {
1594 auto nullBuilder = builder.beginStruct();
1595 for (auto *F : Init)
1596 nullBuilder.add(F);
1597 auto GV = nullBuilder.finishAndCreateGlobal(Name, CGM.getPointerAlign(),
1598 false, llvm::GlobalValue::LinkOnceODRLinkage);
1599 GV->setSection(Section);
1600 GV->setComdat(TheModule.getOrInsertComdat(Name));
1601 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
1602 CGM.addUsedGlobal(GV);
1603 return GV;
1604 };
1605 for (auto clsAlias : ClassAliases)
1606 createNullGlobal(std::string(".objc_class_alias") +
1607 clsAlias.second, { MakeConstantString(clsAlias.second),
1608 GetClassVar(clsAlias.first) }, sectionName<ClassAliasSection>());
1609 // On ELF platforms, add a null value for each special section so that we
1610 // can always guarantee that the _start and _stop symbols will exist and be
1611 // meaningful. This is not required on COFF platforms, where our start and
1612 // stop symbols will create the section.
1613 if (!CGM.getTriple().isOSBinFormatCOFF()) {
1614 createNullGlobal(".objc_null_selector", {NULLPtr, NULLPtr},
1615 sectionName<SelectorSection>());
1616 if (Categories.empty())
1617 createNullGlobal(".objc_null_category", {NULLPtr, NULLPtr,
1618 NULLPtr, NULLPtr, NULLPtr, NULLPtr, NULLPtr},
1619 sectionName<CategorySection>());
1620 if (!EmittedClass) {
1621 createNullGlobal(".objc_null_cls_init_ref", NULLPtr,
1622 sectionName<ClassSection>());
1623 createNullGlobal(".objc_null_class_ref", { NULLPtr, NULLPtr },
1624 sectionName<ClassReferenceSection>());
1625 }
1626 if (!EmittedProtocol)
1627 createNullGlobal(".objc_null_protocol", {NULLPtr, NULLPtr, NULLPtr,
1628 NULLPtr, NULLPtr, NULLPtr, NULLPtr, NULLPtr, NULLPtr, NULLPtr,
1629 NULLPtr}, sectionName<ProtocolSection>());
1630 if (!EmittedProtocolRef)
1631 createNullGlobal(".objc_null_protocol_ref", {NULLPtr},
1632 sectionName<ProtocolReferenceSection>());
1633 if (ClassAliases.empty())
1634 createNullGlobal(".objc_null_class_alias", { NULLPtr, NULLPtr },
1635 sectionName<ClassAliasSection>());
1636 if (ConstantStrings.empty()) {
1637 auto i32Zero = llvm::ConstantInt::get(Int32Ty, 0);
1638 createNullGlobal(".objc_null_constant_string", { NULLPtr, i32Zero,
1639 i32Zero, i32Zero, i32Zero, NULLPtr },
1640 sectionName<ConstantStringSection>());
1641 }
1642 }
1643 ConstantStrings.clear();
1644 Categories.clear();
1645 Classes.clear();
1646
1647 if (EarlyInitList.size() > 0) {
1648 auto *Init = llvm::Function::Create(llvm::FunctionType::get(CGM.VoidTy,
1649 {}), llvm::GlobalValue::InternalLinkage, ".objc_early_init",
1650 &CGM.getModule());
1651 llvm::IRBuilder<> b(llvm::BasicBlock::Create(CGM.getLLVMContext(), "entry",
1652 Init));
1653 for (const auto &lateInit : EarlyInitList) {
1654 auto *global = TheModule.getGlobalVariable(lateInit.first);
1655 if (global) {
1656 b.CreateAlignedStore(
1657 global,
1658 b.CreateStructGEP(lateInit.second.first, lateInit.second.second),
1659 CGM.getPointerAlign().getAsAlign());
1660 }
1661 }
1662 b.CreateRetVoid();
1663 // We can't use the normal LLVM global initialisation array, because we
1664 // need to specify that this runs early in library initialisation.
1665 auto *InitVar = new llvm::GlobalVariable(CGM.getModule(), Init->getType(),
1666 /*isConstant*/true, llvm::GlobalValue::InternalLinkage,
1667 Init, ".objc_early_init_ptr");
1668 InitVar->setSection(".CRT$XCLb");
1669 CGM.addUsedGlobal(InitVar);
1670 }
1671 return nullptr;
1672 }
1673 /// In the v2 ABI, ivar offset variables use the type encoding in their name
1674 /// to trigger linker failures if the types don't match.
1675 std::string GetIVarOffsetVariableName(const ObjCInterfaceDecl *ID,
1676 const ObjCIvarDecl *Ivar) override {
1677 std::string TypeEncoding;
1678 CGM.getContext().getObjCEncodingForType(Ivar->getType(), TypeEncoding);
1679 // Prevent the @ from being interpreted as a symbol version.
1680 std::replace(TypeEncoding.begin(), TypeEncoding.end(),
1681 '@', '\1');
1682 const std::string Name = "__objc_ivar_offset_" + ID->getNameAsString()
1683 + '.' + Ivar->getNameAsString() + '.' + TypeEncoding;
1684 return Name;
1685 }
1686 llvm::Value *EmitIvarOffset(CodeGenFunction &CGF,
1687 const ObjCInterfaceDecl *Interface,
1688 const ObjCIvarDecl *Ivar) override {
1689 const std::string Name = GetIVarOffsetVariableName(Ivar->getContainingInterface(), Ivar);
1690 llvm::GlobalVariable *IvarOffsetPointer = TheModule.getNamedGlobal(Name);
1691 if (!IvarOffsetPointer)
1692 IvarOffsetPointer = new llvm::GlobalVariable(TheModule, IntTy, false,
1693 llvm::GlobalValue::ExternalLinkage, nullptr, Name);
1694 CharUnits Align = CGM.getIntAlign();
1695 llvm::Value *Offset =
1696 CGF.Builder.CreateAlignedLoad(IntTy, IvarOffsetPointer, Align);
1697 if (Offset->getType() != PtrDiffTy)
1698 Offset = CGF.Builder.CreateZExtOrBitCast(Offset, PtrDiffTy);
1699 return Offset;
1700 }
1701 void GenerateClass(const ObjCImplementationDecl *OID) override {
1702 ASTContext &Context = CGM.getContext();
1703 bool IsCOFF = CGM.getTriple().isOSBinFormatCOFF();
1704
1705 // Get the class name
1706 ObjCInterfaceDecl *classDecl =
1707 const_cast<ObjCInterfaceDecl *>(OID->getClassInterface());
1708 std::string className = classDecl->getNameAsString();
1709 auto *classNameConstant = MakeConstantString(className);
1710
1711 ConstantInitBuilder builder(CGM);
1712 auto metaclassFields = builder.beginStruct();
1713 // struct objc_class *isa;
1714 metaclassFields.addNullPointer(PtrTy);
1715 // struct objc_class *super_class;
1716 metaclassFields.addNullPointer(PtrTy);
1717 // const char *name;
1718 metaclassFields.add(classNameConstant);
1719 // long version;
1720 metaclassFields.addInt(LongTy, 0);
1721 // unsigned long info;
1722 // objc_class_flag_meta
1723 metaclassFields.addInt(LongTy, 1);
1724 // long instance_size;
1725 // Setting this to zero is consistent with the older ABI, but it might be
1726 // more sensible to set this to sizeof(struct objc_class)
1727 metaclassFields.addInt(LongTy, 0);
1728 // struct objc_ivar_list *ivars;
1729 metaclassFields.addNullPointer(PtrTy);
1730 // struct objc_method_list *methods
1731 // FIXME: Almost identical code is copied and pasted below for the
1732 // class, but refactoring it cleanly requires C++14 generic lambdas.
1733 if (OID->classmeth_begin() == OID->classmeth_end())
1734 metaclassFields.addNullPointer(PtrTy);
1735 else {
1736 SmallVector<ObjCMethodDecl*, 16> ClassMethods;
1737 ClassMethods.insert(ClassMethods.begin(), OID->classmeth_begin(),
1738 OID->classmeth_end());
1739 metaclassFields.addBitCast(
1740 GenerateMethodList(className, "", ClassMethods, true),
1741 PtrTy);
1742 }
1743 // void *dtable;
1744 metaclassFields.addNullPointer(PtrTy);
1745 // IMP cxx_construct;
1746 metaclassFields.addNullPointer(PtrTy);
1747 // IMP cxx_destruct;
1748 metaclassFields.addNullPointer(PtrTy);
1749 // struct objc_class *subclass_list
1750 metaclassFields.addNullPointer(PtrTy);
1751 // struct objc_class *sibling_class
1752 metaclassFields.addNullPointer(PtrTy);
1753 // struct objc_protocol_list *protocols;
1754 metaclassFields.addNullPointer(PtrTy);
1755 // struct reference_list *extra_data;
1756 metaclassFields.addNullPointer(PtrTy);
1757 // long abi_version;
1758 metaclassFields.addInt(LongTy, 0);
1759 // struct objc_property_list *properties
1760 metaclassFields.add(GeneratePropertyList(OID, classDecl, /*isClassProperty*/true));
1761
1762 auto *metaclass = metaclassFields.finishAndCreateGlobal(
1763 ManglePublicSymbol("OBJC_METACLASS_") + className,
1764 CGM.getPointerAlign());
1765
1766 auto classFields = builder.beginStruct();
1767 // struct objc_class *isa;
1768 classFields.add(metaclass);
1769 // struct objc_class *super_class;
1770 // Get the superclass name.
1771 const ObjCInterfaceDecl * SuperClassDecl =
1772 OID->getClassInterface()->getSuperClass();
1773 llvm::Constant *SuperClass = nullptr;
1774 if (SuperClassDecl) {
1775 auto SuperClassName = SymbolForClass(SuperClassDecl->getNameAsString());
1776 SuperClass = TheModule.getNamedGlobal(SuperClassName);
1777 if (!SuperClass)
1778 {
1779 SuperClass = new llvm::GlobalVariable(TheModule, PtrTy, false,
1780 llvm::GlobalValue::ExternalLinkage, nullptr, SuperClassName);
1781 if (IsCOFF) {
1782 auto Storage = llvm::GlobalValue::DefaultStorageClass;
1783 if (SuperClassDecl->hasAttr<DLLImportAttr>())
1784 Storage = llvm::GlobalValue::DLLImportStorageClass;
1785 else if (SuperClassDecl->hasAttr<DLLExportAttr>())
1786 Storage = llvm::GlobalValue::DLLExportStorageClass;
1787
1788 cast<llvm::GlobalValue>(SuperClass)->setDLLStorageClass(Storage);
1789 }
1790 }
1791 if (!IsCOFF)
1792 classFields.add(llvm::ConstantExpr::getBitCast(SuperClass, PtrTy));
1793 else
1794 classFields.addNullPointer(PtrTy);
1795 } else
1796 classFields.addNullPointer(PtrTy);
1797 // const char *name;
1798 classFields.add(classNameConstant);
1799 // long version;
1800 classFields.addInt(LongTy, 0);
1801 // unsigned long info;
1802 // !objc_class_flag_meta
1803 classFields.addInt(LongTy, 0);
1804 // long instance_size;
1805 int superInstanceSize = !SuperClassDecl ? 0 :
1806 Context.getASTObjCInterfaceLayout(SuperClassDecl).getSize().getQuantity();
1807 // Instance size is negative for classes that have not yet had their ivar
1808 // layout calculated.
1809 classFields.addInt(LongTy,
1810 0 - (Context.getASTObjCImplementationLayout(OID).getSize().getQuantity() -
1811 superInstanceSize));
1812
1813 if (classDecl->all_declared_ivar_begin() == nullptr)
1814 classFields.addNullPointer(PtrTy);
1815 else {
1816 int ivar_count = 0;
1817 for (const ObjCIvarDecl *IVD = classDecl->all_declared_ivar_begin(); IVD;
1818 IVD = IVD->getNextIvar()) ivar_count++;
1819 llvm::DataLayout td(&TheModule);
1820 // struct objc_ivar_list *ivars;
1821 ConstantInitBuilder b(CGM);
1822 auto ivarListBuilder = b.beginStruct();
1823 // int count;
1824 ivarListBuilder.addInt(IntTy, ivar_count);
1825 // size_t size;
1826 llvm::StructType *ObjCIvarTy = llvm::StructType::get(
1827 PtrToInt8Ty,
1828 PtrToInt8Ty,
1829 PtrToInt8Ty,
1830 Int32Ty,
1831 Int32Ty);
1832 ivarListBuilder.addInt(SizeTy, td.getTypeSizeInBits(ObjCIvarTy) /
1833 CGM.getContext().getCharWidth());
1834 // struct objc_ivar ivars[]
1835 auto ivarArrayBuilder = ivarListBuilder.beginArray();
1836 for (const ObjCIvarDecl *IVD = classDecl->all_declared_ivar_begin(); IVD;
1837 IVD = IVD->getNextIvar()) {
1838 auto ivarTy = IVD->getType();
1839 auto ivarBuilder = ivarArrayBuilder.beginStruct();
1840 // const char *name;
1841 ivarBuilder.add(MakeConstantString(IVD->getNameAsString()));
1842 // const char *type;
1843 std::string TypeStr;
1844 //Context.getObjCEncodingForType(ivarTy, TypeStr, IVD, true);
1845 Context.getObjCEncodingForMethodParameter(Decl::OBJC_TQ_None, ivarTy, TypeStr, true);
1846 ivarBuilder.add(MakeConstantString(TypeStr));
1847 // int *offset;
1848 uint64_t BaseOffset = ComputeIvarBaseOffset(CGM, OID, IVD);
1849 uint64_t Offset = BaseOffset - superInstanceSize;
1850 llvm::Constant *OffsetValue = llvm::ConstantInt::get(IntTy, Offset);
1851 std::string OffsetName =