Skip to content

Commit

Permalink
Fix broken support for constructors and destructors (#190)
Browse files Browse the repository at this point in the history
  • Loading branch information
bcoppens committed Jan 14, 2024
1 parent c019b06 commit b1efd16
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 10 deletions.
32 changes: 22 additions & 10 deletions lib/Target/CBackend/CBackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2112,8 +2112,6 @@ static void defineNanInf(raw_ostream &Out) {
<< "#define LLVM_INFF __builtin_inff() /* Float */\n"
<< "#define LLVM_PREFETCH(addr,rw,locality) "
"__builtin_prefetch(addr,rw,locality)\n"
<< "#define __ATTRIBUTE_CTOR__ __attribute__((constructor))\n"
<< "#define __ATTRIBUTE_DTOR__ __attribute__((destructor))\n"
<< "#else\n"
<< "#define LLVM_NAN(NanStr) ((double)NAN) /* Double */\n"
<< "#define LLVM_NANF(NanStr) ((float)NAN)) /* Float */\n"
Expand All @@ -2122,10 +2120,6 @@ static void defineNanInf(raw_ostream &Out) {
<< "#define LLVM_INF ((double)INFINITY) /* Double */\n"
<< "#define LLVM_INFF ((float)INFINITY) /* Float */\n"
<< "#define LLVM_PREFETCH(addr,rw,locality) /* PREFETCH */\n"
<< "#define __ATTRIBUTE_CTOR__ \"__attribute__((constructor)) not "
"supported on this compiler\"\n"
<< "#define __ATTRIBUTE_DTOR__ \"__attribute__((destructor)) not "
"supported on this compiler\"\n"
<< "#endif\n\n";
}

Expand Down Expand Up @@ -2241,6 +2235,18 @@ static void defineTrap(raw_ostream &Out) {
<< "#endif\n\n";
}

static void defineConstructorsDestructors(raw_ostream &Out) {
Out << "#ifdef __GNUC__\n"
<< "#define __ATTRIBUTE_CTOR__ __attribute__((constructor))\n"
<< "#define __ATTRIBUTE_DTOR__ __attribute__((destructor))\n"
<< "#else\n"
<< "#define __ATTRIBUTE_CTOR__ \"__attribute__((constructor)) not "
"supported on this compiler\"\n"
<< "#define __ATTRIBUTE_DTOR__ \"__attribute__((destructor)) not "
"supported on this compiler\"\n"
<< "#endif\n\n";
}

/// FindStaticTors - Given a static ctor/dtor list, unpack its contents into
/// the StaticTors set.
static void FindStaticTors(GlobalVariable *GV,
Expand All @@ -2252,8 +2258,8 @@ static void FindStaticTors(GlobalVariable *GV,
for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
if (ConstantStruct *CS =
dyn_cast<ConstantStruct>(InitList->getOperand(i))) {
if (CS->getNumOperands() != 2)
return; // Not array of 2-element structs.
if (CS->getNumOperands() != 3)
return; // Not array of 3-element structs.

if (CS->getOperand(1)->isNullValue())
return; // Found a null terminator, exit printing.
Expand Down Expand Up @@ -2359,6 +2365,8 @@ void CWriter::generateCompilerSpecificCode(raw_ostream &Out,
defineStackSaveRestore(Out);
if (headerIncTrap())
defineTrap(Out);
if (headerIncConstructorsDestructors())
defineConstructorsDestructors(Out);
}

bool CWriter::doInitialization(Module &M) {
Expand Down Expand Up @@ -2632,10 +2640,14 @@ void CWriter::generateHeader(Module &M) {
headerUseExternalWeak();
Out << " __EXTERNAL_WEAK__";
}
if (StaticCtors.count(&*I))
if (StaticCtors.count(&*I)) {
headerUseConstructorsDestructors();
Out << " __ATTRIBUTE_CTOR__";
if (StaticDtors.count(&*I))
}
if (StaticDtors.count(&*I)) {
headerUseConstructorsDestructors();
Out << " __ATTRIBUTE_DTOR__";
}
if (I->hasHiddenVisibility()) {
headerUseHidden();
Out << " __HIDDEN__";
Expand Down
2 changes: 2 additions & 0 deletions lib/Target/CBackend/CBackend.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ class CWriter : public FunctionPass, public InstVisitor<CWriter> {
bool ConstantFP128Ty : 1;
bool ForceInline : 1;
bool Trap : 1;
bool ConstructorsDestructors : 1;
} UsedHeaders;

#define USED_HEADERS_FLAG(Name) \
Expand Down Expand Up @@ -157,6 +158,7 @@ class CWriter : public FunctionPass, public InstVisitor<CWriter> {
USED_HEADERS_FLAG(ConstantFP128Ty)
USED_HEADERS_FLAG(ForceInline)
USED_HEADERS_FLAG(Trap)
USED_HEADERS_FLAG(ConstructorsDestructors)

llvm::SmallSet<CmpInst::Predicate, 26> FCmpOps;
void headerUseFCmpOp(CmpInst::Predicate P);
Expand Down
13 changes: 13 additions & 0 deletions test/c_tests/other/test_constructor.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef _MSC_VER
int global_var = 0;

__attribute__((constructor)) static void update_global_var_in_constructor() {
global_var = 6;
}

int main() { return global_var; }
#else
int main() {
return 25; /* constructor/destructor attributes not supported on MSVC */
}
#endif

0 comments on commit b1efd16

Please sign in to comment.