Skip to content

Commit

Permalink
Minor cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
LunaTheFoxgirl committed Nov 20, 2024
1 parent 9cbd156 commit 4246d42
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 34 deletions.
2 changes: 1 addition & 1 deletion driver/linker-gcc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -715,14 +715,14 @@ 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:
case llvm::Triple::DragonFly:
addSoname = true;
args.push_back("-lpthread");
args.push_back("-lm");
this->addObjcStdlibLinkFlags(triple);
break;

case llvm::Triple::Solaris:
Expand Down
60 changes: 42 additions & 18 deletions gen/objcgen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -364,8 +391,8 @@ LLGlobalVariable *ObjCState::getClassRoSymbol(const ClassDeclaration& cd, bool m
// };
std::vector<llvm::Constant*> 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<ClassDeclaration&>(cd), meta))); // instanceStart
members.push_back(DtoConstUint(getInstanceSize(const_cast<ClassDeclaration&>(cd), meta))); // instanceSize
if (getPointerSizeInBits() == 64)
members.push_back(DtoConstUint(0)); // reserved
members.push_back(getNullPtr()); // ivarLayout
Expand Down Expand Up @@ -497,7 +524,7 @@ LLGlobalVariable *ObjCState::getProtocolSymbol(const InterfaceDeclaration& iface
// uint32_t flags;
// }
std::vector<llvm::Constant*> members;
size_t protocolTSize = (getPointerSize()*8)+8;
size_t protocolTSize = (getPointerSize()*9)+8;

members.push_back(getNullPtr()); // unused?
members.push_back(getProtocolName(name)); // mangledName
Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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() {
Expand Down
31 changes: 16 additions & 15 deletions gen/objcgen.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 4246d42

Please sign in to comment.