From 4246d42b4a53c6c10cc0a6393ba0277e2348c89c Mon Sep 17 00:00:00 2001 From: LunaTheFoxgirl Date: Wed, 20 Nov 2024 06:12:42 +0100 Subject: [PATCH] Minor cleanup --- driver/linker-gcc.cpp | 2 +- gen/objcgen.cpp | 60 ++++++++++++++++++++++++++++++------------- gen/objcgen.h | 31 +++++++++++----------- 3 files changed, 59 insertions(+), 34 deletions(-) diff --git a/driver/linker-gcc.cpp b/driver/linker-gcc.cpp index 9ac3691c4e..26197b3d39 100644 --- a/driver/linker-gcc.cpp +++ b/driver/linker-gcc.cpp @@ -715,6 +715,7 @@ void ArgsBuilder::addDefaultPlatformLibs() { // fallthrough case llvm::Triple::Darwin: case llvm::Triple::MacOSX: + this->addObjcStdlibLinkFlags(triple); case llvm::Triple::FreeBSD: case llvm::Triple::NetBSD: case llvm::Triple::OpenBSD: @@ -722,7 +723,6 @@ void ArgsBuilder::addDefaultPlatformLibs() { addSoname = true; args.push_back("-lpthread"); args.push_back("-lm"); - this->addObjcStdlibLinkFlags(triple); break; case llvm::Triple::Solaris: diff --git a/gen/objcgen.cpp b/gen/objcgen.cpp index 07c2e7925b..e8d90b3f71 100644 --- a/gen/objcgen.cpp +++ b/gen/objcgen.cpp @@ -196,25 +196,25 @@ std::string ObjCState::getObjcTypeEncoding(Type *t) { // std::string ObjCState::getObjcClassRoSymbol(const ClassDeclaration& cd, bool meta) { - return getObjcSymbolName(meta ? "OBJC_METACLASS_RO_$_" : "OBJC_CLASS_RO_$_", cd.toChars()); + return getObjcSymbolName(meta ? "OBJC_METACLASS_RO_$_" : "OBJC_CLASS_RO_$_", cd.ident->toChars()); } std::string ObjCState::getObjcClassSymbol(const ClassDeclaration& cd, bool meta) { - return getObjcSymbolName(meta ? "OBJC_METACLASS_$_" : "OBJC_CLASS_$_", cd.toChars()); + return getObjcSymbolName(meta ? "OBJC_METACLASS_$_" : "OBJC_CLASS_$_", cd.ident->toChars()); } std::string ObjCState::getObjcMethodListSymbol(const ClassDeclaration& cd, bool meta) { return getObjcSymbolName(meta ? "OBJC_$_CLASS_METHODS_" : "OBJC_$_INSTANCE_METHODS_", cd.objc.identifier->toChars()); } -std::string ObjCState::getObjcProtoSymbol(const InterfaceDeclaration& iface) { - return getObjcSymbolName("OBJC_PROTOCOL_$_", iface.toChars()); +std::string ObjCState::getObjcProtoSymbol(const InterfaceDeclaration& id) { + return getObjcSymbolName("OBJC_PROTOCOL_$_", id.ident->toChars()); } std::string ObjCState::getObjcIvarSymbol(const ClassDeclaration& cd, const VarDeclaration& var) { std::string tmp; tmp.append("OBJC_IVAR_$_"); - tmp.append(cd.objc.identifier->toChars()); + tmp.append(cd.ident->toChars()); tmp.append("."); tmp.append(var.ident->toChars()); return tmp; @@ -329,7 +329,7 @@ LLGlobalVariable *ObjCState::getClassSymbol(const ClassDeclaration& cd, bool met } LLGlobalVariable *ObjCState::getClassRoName(const ClassDeclaration& cd) { - llvm::StringRef name(cd.toChars()); + llvm::StringRef name(cd.ident->toChars()); auto it = classRoNameTable.find(name); if (it != classRoNameTable.end()) { return it->second; @@ -341,6 +341,33 @@ LLGlobalVariable *ObjCState::getClassRoName(const ClassDeclaration& cd) { return var; } +ptrdiff_t getInstanceStart(ClassDeclaration& cd, bool meta) { + if (meta) + return getPointerSizeInBits() == 64 ? + OBJC_METACLASS_INSTANCESTART_64 : OBJC_METACLASS_INSTANCESTART_32; + + ptrdiff_t start = cd.size(cd.loc); + if (!cd.members || cd.members->length == 0) + return start; + + for(d_size_t idx = 0; idx < cd.members->length; idx++) + { + auto var = ((*cd.members)[idx])->isVarDeclaration(); + + if (var && var->isField()) + return var->offset; + } + return start; +} + +size_t getInstanceSize(ClassDeclaration& cd, bool meta) { + if (meta) + return getPointerSizeInBits() == 64 ? + OBJC_METACLASS_INSTANCESTART_64 : OBJC_METACLASS_INSTANCESTART_32; + + return cd.size(cd.loc); +} + LLGlobalVariable *ObjCState::getClassRoSymbol(const ClassDeclaration& cd, bool meta) { auto name = getObjcClassRoSymbol(cd, meta); auto it = classRoSymbolTable.find(name); @@ -364,8 +391,8 @@ LLGlobalVariable *ObjCState::getClassRoSymbol(const ClassDeclaration& cd, bool m // }; std::vector members; members.push_back(DtoConstUint(getClassFlags(cd))); // flags - members.push_back(DtoConstUint(0)); // instanceStart - members.push_back(DtoConstUint(0)); // instanceSize + members.push_back(DtoConstUint(getInstanceStart(const_cast(cd), meta))); // instanceStart + members.push_back(DtoConstUint(getInstanceSize(const_cast(cd), meta))); // instanceSize if (getPointerSizeInBits() == 64) members.push_back(DtoConstUint(0)); // reserved members.push_back(getNullPtr()); // ivarLayout @@ -497,7 +524,7 @@ LLGlobalVariable *ObjCState::getProtocolSymbol(const InterfaceDeclaration& iface // uint32_t flags; // } std::vector members; - size_t protocolTSize = (getPointerSize()*8)+8; + size_t protocolTSize = (getPointerSize()*9)+8; members.push_back(getNullPtr()); // unused? members.push_back(getProtocolName(name)); // mangledName @@ -546,7 +573,7 @@ LLGlobalVariable *ObjCState::getProtocolReference(const InterfaceDeclaration& if LLConstant *ObjCState::getIVarListFor(const ClassDeclaration& cd) { // If there's no fields, just return null. - if (cd.fields.length > 0) { + if (cd.fields.empty()) { return getNullPtr(); } @@ -688,14 +715,13 @@ LLGlobalVariable *ObjCState::getMethodVarType(const FuncDeclaration& fd) { return getTypeEncoding(fd.type); } - LLGlobalVariable *ObjCState::getMethodVarTypeName(const llvm::StringRef& name) { auto it = methodTypeTable.find(name); if (it != methodTypeTable.end()) { return it->second; } - auto var = getCStringVar("OBJC_METH_VAR_TYPE_", name, methodTypeSection); + auto var = getCStringVar("OBJC_METH_VAR_TYPE", name, methodTypeSection); methodTypeTable[name] = var; retain(var); return var; @@ -745,7 +771,7 @@ llvm::Constant *ObjCState::finalizeClasses() { } } - auto var = getGlobalWithBytes(module, "OBJC_CLASS_$_", members); + auto var = getGlobalWithBytes(module, "L_OBJC_LABEL_CLASS_$", members); var->setSection(classListSection); return var; } @@ -762,7 +788,7 @@ llvm::Constant *ObjCState::finalizeProtocols() { } } - auto var = getGlobalWithBytes(module, "OBJC_PROTOCOL_$_", members); + auto var = getGlobalWithBytes(module, "L_OBJC_LABEL_PROTOCOL_$", members); var->setSection(protoListSection); return var; } @@ -783,12 +809,10 @@ void ObjCState::finalize() { void ObjCState::genImageInfo() { // Use LLVM to generate image info module.addModuleFlag(llvm::Module::Error, "Objective-C Version", 2); // Only support ABI 2. (Non-fragile) - module.addModuleFlag(llvm::Module::Error, "Objective-C Image Info Version", - 0u); // version + module.addModuleFlag(llvm::Module::Error, "Objective-C Image Info Version", 0u); // version module.addModuleFlag(llvm::Module::Error, "Objective-C Image Info Section", llvm::MDString::get(module.getContext(), imageInfoSection)); - module.addModuleFlag(llvm::Module::Override, "Objective-C Garbage Collection", - 0u); // flags + module.addModuleFlag(llvm::Module::Override, "Objective-C Garbage Collection", 0u); // flags } void ObjCState::retainSymbols() { diff --git a/gen/objcgen.h b/gen/objcgen.h index 7b05edcfcb..d5d7b3c6fe 100644 --- a/gen/objcgen.h +++ b/gen/objcgen.h @@ -48,6 +48,8 @@ bool objc_isSupported(const llvm::Triple &triple); // poking the Objective-C library? #define OBJC_METHOD_SIZEOF 24 #define OBJC_IVAR_ENTSIZE 32 +#define OBJC_METACLASS_INSTANCESTART_32 40 +#define OBJC_METACLASS_INSTANCESTART_64 76 // class is a metaclass #define RO_META (1<<0) @@ -78,6 +80,7 @@ class ObjCState { // Methods LLGlobalVariable *getMethodVarType(const FuncDeclaration& fd); + LLGlobalVariable *getMethodListFor(const ClassDeclaration& cd, bool meta, bool optional=false); // Selector LLGlobalVariable *getSelector(const ObjcSelector &sel); @@ -93,25 +96,25 @@ class ObjCState { void finalize(); // Section Names - const char *imageInfoSection = "__DATA,__objc_imageinfo, regular"; + const char *imageInfoSection = "__DATA,__objc_imageinfo,regular,no_dead_strip"; // Lists - const char *classListSection = "__DATA,__objc_classlist, regular"; - const char *protoListSection = "__DATA,__objc_protolist, regular"; - const char *catListSection = "__DATA,__objc_catlist, regular"; + const char *classListSection = "__DATA,__objc_classlist,regular,no_dead_strip"; + const char *protoListSection = "__DATA,__objc_protolist,regular,no_dead_strip"; + const char *catListSection = "__DATA,__objc_catlist,regular,no_dead_strip"; - const char *classNameSection = "__TEXT,__objc_classname, cstring_literals"; - const char *classStubsSection = "__DATA,__objc_stubs, regular"; + const char *classNameSection = "__TEXT,__objc_classname,cstring_literals,no_dead_strip"; + const char *classStubsSection = "__DATA,__objc_stubs,regular,no_dead_strip"; - const char *constSection = "__DATA,__objc_const, regular"; - const char *dataSection = "__DATA,__objc_data, regular"; + const char *constSection = "__DATA,__objc_const,regular,no_dead_strip"; + const char *dataSection = "__DATA,__objc_data,regular,no_dead_strip"; - const char *methodNameSection = "__TEXT,__objc_methname, cstring_literals"; - const char *methodTypeSection = "__TEXT,__objc_methtype, regular"; + const char *methodNameSection = "__TEXT,__objc_methname,cstring_literals,no_dead_strip"; + const char *methodTypeSection = "__TEXT,__objc_methtype,regular,no_dead_strip"; - const char *classRefsSection = "__DATA,__objc_classrefs, regular"; - const char *protoRefsSection = "__DATA,__objc_protorefs, regular"; - const char *selectorRefsSection = "__DATA,__objc_selrefs, regular"; + const char *classRefsSection = "__DATA,__objc_classrefs,literal_pointers,no_dead_strip"; + const char *protoRefsSection = "__DATA,__objc_protorefs,literal_pointers,no_dead_strip"; + const char *selectorRefsSection = "__DATA,__objc_selrefs,literal_pointers,no_dead_strip"; private: llvm::Module &module; @@ -175,8 +178,6 @@ class ObjCState { llvm::GlobalVariable *getEmptyCache(); - llvm::GlobalVariable *getMethodListFor(const ClassDeclaration& cd, bool meta, bool optional=false); - llvm::GlobalVariable *getGlobal(llvm::Module& module, llvm::StringRef name, llvm::Type* type = nullptr); llvm::GlobalVariable *getGlobalWithBytes(llvm::Module& module, llvm::StringRef name, ConstantList packedContents); llvm::GlobalVariable *getCStringVar(const char *symbol, const llvm::StringRef &str, const char *section = nullptr);